relocator16.S 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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. /* The code segment of the protected mode. */
  19. #define CODE_SEGMENT 0x08
  20. /* The data segment of the protected mode. */
  21. #define DATA_SEGMENT 0x10
  22. #define PSEUDO_REAL_CSEG 0x18
  23. #define PSEUDO_REAL_DSEG 0x20
  24. #include <grub/i386/relocator_private.h>
  25. #include "relocator_common.S"
  26. .p2align 4 /* force 16-byte alignment */
  27. VARIABLE(grub_relocator16_start)
  28. PREAMBLE
  29. #ifdef __APPLE__
  30. LOCAL(cs_base_bytes12_offset) = LOCAL (cs_base_bytes12) - LOCAL (base)
  31. LOCAL(cs_base_byte3_offset) = LOCAL (cs_base_byte3) - LOCAL (base)
  32. LOCAL(ds_base_bytes12_offset) = LOCAL (ds_base_bytes12) - LOCAL (base)
  33. LOCAL(ds_base_byte3_offset) = LOCAL (ds_base_byte3) - LOCAL (base)
  34. movl %esi, %eax
  35. movw %ax, (LOCAL(cs_base_bytes12_offset)) (RSI, 1)
  36. movw %ax, (LOCAL(ds_base_bytes12_offset)) (RSI, 1)
  37. shrl $16, %eax
  38. movb %al, (LOCAL (cs_base_byte3_offset)) (RSI, 1)
  39. movb %al, (LOCAL (ds_base_byte3_offset)) (RSI, 1)
  40. #else
  41. movl %esi, %eax
  42. movw %ax, (LOCAL (cs_base_bytes12) - LOCAL (base)) (RSI, 1)
  43. movw %ax, (LOCAL (ds_base_bytes12) - LOCAL (base)) (RSI, 1)
  44. shrl $16, %eax
  45. movb %al, (LOCAL (cs_base_byte3) - LOCAL (base)) (RSI, 1)
  46. movb %al, (LOCAL (ds_base_byte3) - LOCAL (base)) (RSI, 1)
  47. #endif
  48. RELOAD_GDT
  49. .code32
  50. /* Update other registers. */
  51. movl $DATA_SEGMENT, %eax
  52. movl %eax, %ds
  53. movl %eax, %es
  54. movl %eax, %fs
  55. movl %eax, %gs
  56. movl %eax, %ss
  57. DISABLE_PAGING
  58. #ifdef __x86_64__
  59. /* Disable amd64. */
  60. movl $GRUB_MEMORY_CPU_AMD64_MSR, %ecx
  61. rdmsr
  62. andl $(~GRUB_MEMORY_CPU_AMD64_MSR_ON), %eax
  63. wrmsr
  64. #endif
  65. /* Turn off PAE. */
  66. movl %cr4, %eax
  67. andl $(~GRUB_MEMORY_CPU_CR4_PAE_ON), %eax
  68. movl %eax, %cr4
  69. /* Update other registers. */
  70. movl $PSEUDO_REAL_DSEG, %eax
  71. movl %eax, %ds
  72. movl %eax, %es
  73. movl %eax, %fs
  74. movl %eax, %gs
  75. movl %eax, %ss
  76. movl %esi, %eax
  77. shrl $4, %eax
  78. #ifdef __APPLE__
  79. LOCAL(segment_offset) = LOCAL (segment) - LOCAL (base)
  80. LOCAL(idt_offset) = LOCAL(relocator16_idt) - LOCAL (base)
  81. LOCAL(cont2_offset) = LOCAL (cont2) - LOCAL(base)
  82. movw %ax, (LOCAL(segment_offset))
  83. lidt (LOCAL(idt_offset))
  84. /* jump to a 16 bit segment */
  85. ljmp $PSEUDO_REAL_CSEG, $(LOCAL(cont2_offset))
  86. #else
  87. movw %ax, (LOCAL (segment) - LOCAL (base))
  88. lidt (EXT_C(grub_relocator16_idt) - LOCAL (base))
  89. /* jump to a 16 bit segment */
  90. ljmp $PSEUDO_REAL_CSEG, $(LOCAL (cont2) - LOCAL(base))
  91. #endif
  92. LOCAL(cont2):
  93. .code16
  94. /* clear the PE bit of CR0 */
  95. movl %cr0, %eax
  96. andl $(~GRUB_MEMORY_CPU_CR0_PE_ON), %eax
  97. movl %eax, %cr0
  98. /* flush prefetch queue, reload %cs */
  99. /* ljmp */
  100. .byte 0xea
  101. #ifdef __APPLE__
  102. LOCAL(cont3_offset) = LOCAL(cont3) - LOCAL(base)
  103. .word LOCAL(cont3_offset)
  104. #else
  105. .word LOCAL(cont3)-LOCAL(base)
  106. #endif
  107. LOCAL(segment):
  108. .word 0
  109. LOCAL(cont3):
  110. /* movw imm16, %ax. */
  111. .byte 0xb8
  112. VARIABLE(grub_relocator16_keep_a20_enabled)
  113. .word 0
  114. test %ax, %ax
  115. jnz LOCAL(gate_a20_done)
  116. movw %cs, %ax
  117. movw %ax, %ss
  118. #ifdef __APPLE__
  119. LOCAL(relocator16_end_offset) = LOCAL(relocator16_end) - LOCAL(base)
  120. leaw LOCAL(relocator16_end_offset), %sp
  121. #else
  122. leaw LOCAL(relocator16_end) - LOCAL(base), %sp
  123. #endif
  124. addw $GRUB_RELOCATOR16_STACK_SIZE, %sp
  125. /* second, try a BIOS call */
  126. movw $0x2400, %ax
  127. int $0x15
  128. call LOCAL(gate_a20_check_state)
  129. testb %al, %al
  130. jz LOCAL(gate_a20_done)
  131. /*
  132. * In macbook, the keyboard test would hang the machine, so we move
  133. * this forward.
  134. */
  135. /* fourth, try the system control port A */
  136. inb $0x92
  137. andb $(~0x03), %al
  138. outb $0x92
  139. /* When turning off Gate A20, do not check the state strictly,
  140. because a failure is not fatal usually, and Gate A20 is always
  141. on some modern machines. */
  142. jmp LOCAL(gate_a20_done)
  143. LOCAL(gate_a20_check_state):
  144. /* iterate the checking for a while */
  145. movw $100, %cx
  146. 1:
  147. xorw %ax, %ax
  148. movw %ax, %ds
  149. decw %ax
  150. movw %ax, %es
  151. xorw %ax, %ax
  152. movw $0x8000, %ax
  153. /* compare the byte at ADDR with that at 0x100000 + ADDR */
  154. movw %ax, %si
  155. addw $0x10, %ax
  156. movw %ax, %di
  157. /* save the original byte in DL */
  158. movb %ds:(%si), %dl
  159. movb %es:(%di), %al
  160. /* try to set one less value at ADDR */
  161. movb %al, %dh
  162. decb %dh
  163. movb %dh, %ds:(%si)
  164. /* serialize */
  165. outb %al, $0x80
  166. outb %al, $0x80
  167. /* obtain the value at 0x100000 + ADDR in CH */
  168. movb %es:(%di), %dh
  169. /* this result is 1 if A20 is on or 0 if it is off */
  170. subb %dh, %al
  171. xorb $1, %al
  172. /* restore the original */
  173. movb %dl, %ds:(%si)
  174. testb %al, %al
  175. jz LOCAL(gate_a20_done)
  176. loop 1b
  177. 2:
  178. ret
  179. LOCAL(gate_a20_done):
  180. /*
  181. * We are in real mode now. Set up the real mode segment registers and
  182. * all the other general purpose registers. cs is updated with ljmp.
  183. */
  184. /* movw imm16, %ax. */
  185. .byte 0xb8
  186. VARIABLE(grub_relocator16_ds)
  187. .word 0
  188. movw %ax, %ds
  189. /* movw imm16, %ax. */
  190. .byte 0xb8
  191. VARIABLE(grub_relocator16_es)
  192. .word 0
  193. movw %ax, %es
  194. /* movw imm16, %ax. */
  195. .byte 0xb8
  196. VARIABLE(grub_relocator16_fs)
  197. .word 0
  198. movw %ax, %fs
  199. /* movw imm16, %ax. */
  200. .byte 0xb8
  201. VARIABLE(grub_relocator16_gs)
  202. .word 0
  203. movw %ax, %gs
  204. /* movw imm16, %ax. */
  205. .byte 0xb8
  206. VARIABLE(grub_relocator16_ss)
  207. .word 0
  208. movw %ax, %ss
  209. /* movw imm16, %ax. */
  210. .byte 0xb8
  211. VARIABLE(grub_relocator16_sp)
  212. .word 0
  213. movzwl %ax, %esp
  214. /* movw imm32, %eax. */
  215. .byte 0x66, 0xb8
  216. VARIABLE(grub_relocator16_esi)
  217. .long 0
  218. movl %eax, %esi
  219. /* movw imm32, %edx. */
  220. .byte 0x66, 0xba
  221. VARIABLE(grub_relocator16_edx)
  222. .long 0
  223. /* movw imm32, %ebx. */
  224. .byte 0x66, 0xbb
  225. VARIABLE(grub_relocator16_ebx)
  226. .long 0
  227. /* movl imm32, %ebp. */
  228. .byte 0x66, 0xbd
  229. VARIABLE(grub_relocator16_ebp)
  230. .long 0
  231. /* Cleared direction flag is of no problem with any current
  232. payload and makes this implementation easier. */
  233. cld
  234. /* ljmp */
  235. .byte 0xea
  236. VARIABLE(grub_relocator16_ip)
  237. .word 0
  238. VARIABLE(grub_relocator16_cs)
  239. .word 0
  240. .code32
  241. /* GDT. Copied from loader/i386/linux.c. */
  242. .p2align 4
  243. LOCAL(gdt):
  244. .word 0, 0
  245. .byte 0, 0, 0, 0
  246. /* -- code segment --
  247. * base = 0x00000000, limit = 0xFFFFF (4 KiB Granularity), present
  248. * type = 32bit code execute/read, DPL = 0
  249. */
  250. .word 0xFFFF, 0
  251. .byte 0, 0x9A, 0xCF, 0
  252. /* -- data segment --
  253. * base = 0x00000000, limit 0xFFFFF (4 KiB Granularity), present
  254. * type = 32 bit data read/write, DPL = 0
  255. */
  256. .word 0xFFFF, 0
  257. .byte 0, 0x92, 0xCF, 0
  258. /* -- 16 bit real mode CS --
  259. * base = filled by code, limit 0x0FFFF (1 B Granularity), present
  260. * type = 16 bit code execute/read only/conforming, DPL = 0
  261. */
  262. .word 0xFFFF
  263. LOCAL(cs_base_bytes12):
  264. .word 0
  265. LOCAL(cs_base_byte3):
  266. .byte 0
  267. .byte 0x9E, 0, 0
  268. /* -- 16 bit real mode DS --
  269. * base = filled by code, limit 0x0FFFF (1 B Granularity), present
  270. * type = 16 bit data read/write, DPL = 0
  271. */
  272. .word 0xFFFF
  273. LOCAL(ds_base_bytes12):
  274. .word 0
  275. LOCAL(ds_base_byte3):
  276. .byte 0
  277. .byte 0x92, 0, 0
  278. LOCAL(gdt_end):
  279. #ifdef __APPLE__
  280. LOCAL(relocator16_idt):
  281. #endif
  282. VARIABLE(grub_relocator16_idt)
  283. .word 0
  284. .long 0
  285. LOCAL(relocator16_end):
  286. VARIABLE(grub_relocator16_end)
  287. .byte 0