You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

lmelf_dprefix.S 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. #include "elf.h"
  2. .arch sledgehammer
  3. .code32
  4. .equ FLAT_CODE_SEG,_pmcs-_gdt
  5. .equ FLAT_DATA_SEG,_pmds-_gdt
  6. .equ MSR_K6_EFER, 0xC0000080
  7. .equ EFER_LME, 0x00000100
  8. .equ X86_CR4_PAE, 0x00000020
  9. .equ CR0_PG, 0x80000000
  10. .section ".prefix", "ax", @progbits
  11. #define LOAD_ADDR 0x10000
  12. /* ELF Header */
  13. .globl elf_header
  14. elf_header:
  15. e_ident: .byte 0x7f, 'E', 'L', 'F', 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0
  16. e_type: .short ET_DYN
  17. e_machine: .short EM_X86_64
  18. e_version: .long 1
  19. e_entry: .long LOAD_ADDR + elf_start - elf_header
  20. e_phoff: .long elf_program_header - elf_header
  21. e_shoff: .long 0
  22. e_flags: .long 0
  23. e_ehsize: .short elf_header_end - elf_header
  24. e_phentsize: .short ELF32_PHDR_SIZE
  25. e_phnum: .short (elf_program_header_end - elf_program_header)/ELF32_PHDR_SIZE
  26. e_shentsize: .short 0
  27. e_shnum: .short 0
  28. e_shstrndx: .short 0
  29. elf_header_end:
  30. elf_program_header:
  31. phdr1_p_type: .long PT_NOTE
  32. phdr1_p_offset: .long elf_note - elf_header
  33. phdr1_p_vaddr: .long elf_note
  34. phdr1_p_paddr: .long elf_note
  35. phdr1_p_filesz: .long elf_note_end - elf_note
  36. phdr1_p_memsz: .long elf_note_end - elf_note
  37. phdr1_p_flags: .long PF_R | PF_W | PF_X
  38. phdr1_p_align: .long 0
  39. /* The decompressor */
  40. phdr2_p_type: .long PT_LOAD
  41. phdr2_p_offset: .long 0
  42. phdr2_p_vaddr: .long elf_header
  43. phdr2_p_paddr: .long LOAD_ADDR
  44. phdr2_p_filesz: .long _verbatim_size
  45. phdr2_p_memsz: .long _image_size
  46. phdr2_p_flags: .long PF_R | PF_W | PF_X
  47. phdr2_p_align: .long 16
  48. elf_program_header_end:
  49. .globl elf_note
  50. elf_note:
  51. .balign 4
  52. .int 2f - 1f
  53. .int 4f - 3f
  54. .int EIN_PROGRAM_NAME
  55. 1: .asciz "ELFBoot"
  56. 2:
  57. .balign 4
  58. 3:
  59. .asciz "Etherboot"
  60. 4:
  61. .balign 4
  62. .int 2f - 1f
  63. .int 4f - 3f
  64. .int EIN_PROGRAM_VERSION
  65. 1: .asciz "ELFBoot"
  66. 2:
  67. .balign 4
  68. 3:
  69. .asciz VERSION
  70. 4:
  71. #if 0
  72. .balign 4
  73. .int 2f - 1f
  74. .int 4f - 3f
  75. .int EIN_PROGRAM_CHECKSUM
  76. 1: .asciz "ELFBoot"
  77. 2:
  78. .balign 4
  79. 3:
  80. .word 0
  81. 4:
  82. #endif
  83. .balign 4
  84. elf_note_end:
  85. elf_start:
  86. .code64
  87. /* Reload the gdt to something I know */
  88. leaq _gdt(%rip), %rax
  89. movq %rax, 0x02 + gdtptr(%rip)
  90. lgdt gdtptr(%rip)
  91. /* Enter 32bit compatibility mode */
  92. leaq elf_start32(%rip), %rax
  93. movl %eax, 0x00 + elf_start32_addr(%rip)
  94. ljmp *elf_start32_addr(%rip)
  95. elf_start32:
  96. .code32
  97. /* Reload the data segments */
  98. movl $FLAT_DATA_SEG, %eax
  99. movl %eax, %ds
  100. movl %eax, %es
  101. movl %eax, %ss
  102. /* Disable paging */
  103. movl %cr0, %eax
  104. andl $~CR0_PG, %eax
  105. movl %eax, %cr0
  106. /* Disable long mode */
  107. movl $MSR_K6_EFER, %ecx
  108. rdmsr
  109. andl $~EFER_LME, %eax
  110. wrmsr
  111. /* Disable PAE */
  112. movl %cr4, %eax
  113. andl $~X86_CR4_PAE, %eax
  114. movl %eax, %cr4
  115. /* Save the first argument */
  116. pushl %ebx
  117. jmp _start
  118. gdtptr:
  119. .word _gdt_end - _gdt -1
  120. .long _gdt
  121. .long 0
  122. _gdt:
  123. elf_start32_addr:
  124. .long elf_start32
  125. .long FLAT_CODE_SEG
  126. _pmcs:
  127. /* 32 bit protected mode code segment, base 0 */
  128. .word 0xffff,0
  129. .byte 0,0x9f,0xcf,0
  130. _pmds:
  131. /* 32 bit protected mode data segment, base 0 */
  132. .word 0xffff,0
  133. .byte 0,0x93,0xcf,0
  134. _gdt_end:
  135. /* Dummy routines to satisfy the build */
  136. .section ".text16", "ax", @progbits
  137. prefix_exit:
  138. prefix_exit_end:
  139. .previous