relocator64.S 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /*
  2. * GRUB -- GRand Unified Bootloader
  3. * Copyright (C) 2009,2010 Free Software Foundation, Inc.
  4. *
  5. * GRUB is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * GRUB is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #define CODE32_SEGMENT 0x18
  19. #define CODE_SEGMENT 0x08
  20. /* The data segment of the protected mode. */
  21. #define DATA_SEGMENT 0x10
  22. #include "relocator_common.S"
  23. .p2align 4 /* force 16-byte alignment */
  24. VARIABLE(grub_relocator64_start)
  25. PREAMBLE
  26. #ifndef __x86_64__
  27. DISABLE_PAGING
  28. /* Turn on PAE. */
  29. movl %cr4, %eax
  30. orl $(GRUB_MEMORY_CPU_CR4_PAE_ON | GRUB_MEMORY_CPU_CR4_PSE_ON), %eax
  31. movl %eax, %cr4
  32. /* mov imm32, %eax */
  33. .byte 0xb8
  34. VARIABLE(grub_relocator64_cr3)
  35. .long 0
  36. movl %eax, %cr3
  37. /* Turn on amd64. */
  38. movl $GRUB_MEMORY_CPU_AMD64_MSR, %ecx
  39. rdmsr
  40. orl $GRUB_MEMORY_CPU_AMD64_MSR_ON, %eax
  41. wrmsr
  42. /* Enable paging. */
  43. movl %cr0, %eax
  44. orl $GRUB_MEMORY_CPU_CR0_PAGING_ON, %eax
  45. movl %eax, %cr0
  46. RELOAD_GDT
  47. #else
  48. /* mov imm64, %rax */
  49. .byte 0x48
  50. .byte 0xb8
  51. VARIABLE(grub_relocator64_cr3)
  52. .quad 0
  53. movq %rax, %cr3
  54. #endif
  55. #ifdef __x86_64__
  56. .code64
  57. #endif
  58. /* mov imm64, %rax */
  59. .byte 0x48
  60. .byte 0xb8
  61. VARIABLE(grub_relocator64_rsp)
  62. .quad 0
  63. #ifdef __x86_64__
  64. movq %rax, %rsp
  65. #else
  66. /* movq %rax, %rsp */
  67. .byte 0x48
  68. .byte 0x89
  69. .byte 0xc4
  70. #endif
  71. #ifdef GRUB_MACHINE_EFI
  72. jmp LOCAL(skip_efi_stack_align)
  73. /*
  74. * Here is grub_relocator64_efi_start() entry point. Most of the
  75. * code below is shared between grub_relocator64_efi_start()
  76. * and grub_relocator64_start().
  77. *
  78. * Think twice before changing anything there!!!
  79. */
  80. VARIABLE(grub_relocator64_efi_start)
  81. /* Align the stack as UEFI spec requires. */
  82. #ifdef __x86_64__
  83. andq $~15, %rsp
  84. #else
  85. /* andq $~15, %rsp */
  86. .byte 0x48
  87. .byte 0x83
  88. .byte 0xe4
  89. .byte 0xf0
  90. #endif
  91. LOCAL(skip_efi_stack_align):
  92. #endif
  93. /* mov imm64, %rax */
  94. .byte 0x48
  95. .byte 0xb8
  96. VARIABLE(grub_relocator64_rsi)
  97. .quad 0
  98. #ifdef __x86_64__
  99. movq %rax, %rsi
  100. #else
  101. /* movq %rax, %rsi */
  102. .byte 0x48
  103. .byte 0x89
  104. .byte 0xc6
  105. #endif
  106. /* mov imm64, %rax */
  107. .byte 0x48
  108. .byte 0xb8
  109. VARIABLE(grub_relocator64_rax)
  110. .quad 0
  111. /* mov imm64, %rbx */
  112. .byte 0x48
  113. .byte 0xbb
  114. VARIABLE(grub_relocator64_rbx)
  115. .quad 0
  116. /* mov imm64, %rcx */
  117. .byte 0x48
  118. .byte 0xb9
  119. VARIABLE(grub_relocator64_rcx)
  120. .quad 0
  121. /* mov imm64, %rdx */
  122. .byte 0x48
  123. .byte 0xba
  124. VARIABLE(grub_relocator64_rdx)
  125. .quad 0
  126. /* Cleared direction flag is of no problem with any current
  127. payload and makes this implementation easier. */
  128. cld
  129. #if defined (__APPLE__) || !defined (__x86_64__)
  130. .byte 0xff, 0x25
  131. .long 0
  132. #else
  133. jmp *LOCAL(jump_addr) (%rip)
  134. #endif
  135. LOCAL(jump_addr):
  136. VARIABLE(grub_relocator64_rip)
  137. .quad 0
  138. #ifdef GRUB_MACHINE_EFI
  139. /* Here grub_relocator64_efi_start() ends. Ufff... */
  140. VARIABLE(grub_relocator64_efi_end)
  141. #endif
  142. #ifndef __x86_64__
  143. .p2align 4
  144. LOCAL(gdt):
  145. /* NULL. */
  146. .byte 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  147. /* 64-bit segment. */
  148. .word 0xffff /* Limit xffff. */
  149. .word 0x0000 /* Base xxxx0000. */
  150. .byte 0x00 /* Base xx00xxxx. */
  151. .byte (0x8 /* Type 8. */ | (1 << 4) /* Code. */ \
  152. | (0 << 5) /* Ring 0. */ | (1 << 7) /* Present. */)
  153. .byte (0xf /* Limit fxxxx. */ | (0 << 4) /* AVL flag. */ \
  154. | (1 << 5) /* 64-bit. */ | (0 << 6) \
  155. | (1 << 7) /* 4K granular. */)
  156. .byte 0x00 /* Base 00xxxxxx. */
  157. /* Data segment*/
  158. .word 0xffff /* Limit xffff. */
  159. .word 0x0000 /* Base xxxx0000. */
  160. .byte 0x00 /* Base xx00xxxx. */
  161. .byte (0x0 /* Type 0. */ | (0 << 4) /* Data. */ \
  162. | (0 << 5) /* Ring 0. */ | (1 << 7) /* Present. */)
  163. .byte (0xf /* Limit fxxxx. */ | (0 << 4) /* AVL flag. */ \
  164. | (0 << 5) /* Data. */ | (0 << 6) \
  165. | (1 << 7) /* 4K granular. */)
  166. .byte 0x00 /* Base 00xxxxxx. */
  167. /* Compatibility segment. */
  168. .word 0xffff /* Limit xffff. */
  169. .word 0x0000 /* Base xxxx0000. */
  170. .byte 0x00 /* Base xx00xxxx. */
  171. .byte (0x8 /* Type 8. */ | (1 << 4) /* Code. */ \
  172. | (0 << 5) /* Ring 0. */ | (1 << 7) /* Present. */)
  173. .byte (0xf /* Limit fxxxx. */ | (0 << 4) /* AVL flag. */ \
  174. | (0 << 5) /* 32-bit. */ | (1 << 6) /* 32-bit. */ \
  175. | (1 << 7) /* 4K granular. */)
  176. .byte 0x00 /* Base 00xxxxxx. */
  177. LOCAL(gdt_end):
  178. #endif
  179. VARIABLE(grub_relocator64_end)