bios_asm.S 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. /*
  2. * From coreboot x86_asm.S, cleaned up substantially
  3. *
  4. * Copyright (C) 2009-2010 coresystems GmbH
  5. *
  6. * SPDX-License-Identifier: GPL-2.0
  7. */
  8. #include <asm/processor.h>
  9. #include <asm/processor-flags.h>
  10. #include "bios.h"
  11. #define SEG(segment) $segment * X86_GDT_ENTRY_SIZE
  12. /*
  13. * This is the interrupt handler stub code. It gets copied to the IDT and
  14. * to some fixed addresses in the F segment. Before the code can used,
  15. * it gets patched up by the C function copying it: byte 3 (the $0 in
  16. * movb $0, %al) is overwritten with the interrupt numbers.
  17. */
  18. .code16
  19. .globl __idt_handler
  20. __idt_handler:
  21. pushal
  22. movb $0, %al /* This instruction gets modified */
  23. ljmp $0, $__interrupt_handler_16bit
  24. .globl __idt_handler_size
  25. __idt_handler_size:
  26. .long . - __idt_handler
  27. .macro setup_registers
  28. /* initial register values */
  29. movl 44(%ebp), %eax
  30. movl %eax, __registers + 0 /* eax */
  31. movl 48(%ebp), %eax
  32. movl %eax, __registers + 4 /* ebx */
  33. movl 52(%ebp), %eax
  34. movl %eax, __registers + 8 /* ecx */
  35. movl 56(%ebp), %eax
  36. movl %eax, __registers + 12 /* edx */
  37. movl 60(%ebp), %eax
  38. movl %eax, __registers + 16 /* esi */
  39. movl 64(%ebp), %eax
  40. movl %eax, __registers + 20 /* edi */
  41. .endm
  42. .macro enter_real_mode
  43. /* Activate the right segment descriptor real mode. */
  44. ljmp SEG(X86_GDT_ENTRY_16BIT_CS), $PTR_TO_REAL_MODE(1f)
  45. 1:
  46. .code16
  47. /*
  48. * Load the segment registers with properly configured segment
  49. * descriptors. They will retain these configurations (limits,
  50. * writability, etc.) once protected mode is turned off.
  51. */
  52. mov SEG(X86_GDT_ENTRY_16BIT_DS), %ax
  53. mov %ax, %ds
  54. mov %ax, %es
  55. mov %ax, %fs
  56. mov %ax, %gs
  57. mov %ax, %ss
  58. /* Turn off protection */
  59. movl %cr0, %eax
  60. andl $~X86_CR0_PE, %eax
  61. movl %eax, %cr0
  62. /* Now really going into real mode */
  63. ljmp $0, $PTR_TO_REAL_MODE(1f)
  64. 1:
  65. /*
  66. * Set up a stack: Put the stack at the end of page zero. That way
  67. * we can easily share it between real and protected, since the
  68. * 16-bit ESP at segment 0 will work for any case.
  69. */
  70. mov $0x0, %ax
  71. mov %ax, %ss
  72. /* Load 16 bit IDT */
  73. xor %ax, %ax
  74. mov %ax, %ds
  75. lidt __realmode_idt
  76. .endm
  77. .macro prepare_for_irom
  78. movl $0x1000, %eax
  79. movl %eax, %esp
  80. /* Initialise registers for option rom lcall */
  81. movl __registers + 0, %eax
  82. movl __registers + 4, %ebx
  83. movl __registers + 8, %ecx
  84. movl __registers + 12, %edx
  85. movl __registers + 16, %esi
  86. movl __registers + 20, %edi
  87. /* Set all segments to 0x0000, ds to 0x0040 */
  88. push %ax
  89. xor %ax, %ax
  90. mov %ax, %es
  91. mov %ax, %fs
  92. mov %ax, %gs
  93. mov SEG(X86_GDT_ENTRY_16BIT_FLAT_DS), %ax
  94. mov %ax, %ds
  95. pop %ax
  96. .endm
  97. .macro enter_protected_mode
  98. /* Go back to protected mode */
  99. movl %cr0, %eax
  100. orl $X86_CR0_PE, %eax
  101. movl %eax, %cr0
  102. /* Now that we are in protected mode jump to a 32 bit code segment */
  103. data32 ljmp SEG(X86_GDT_ENTRY_32BIT_CS), $PTR_TO_REAL_MODE(1f)
  104. 1:
  105. .code32
  106. mov SEG(X86_GDT_ENTRY_32BIT_DS), %ax
  107. mov %ax, %ds
  108. mov %ax, %es
  109. mov %ax, %gs
  110. mov %ax, %ss
  111. mov SEG(X86_GDT_ENTRY_32BIT_FS), %ax
  112. mov %ax, %fs
  113. /* restore proper idt */
  114. lidt idt_ptr
  115. .endm
  116. /*
  117. * In order to be independent of U-Boot's position in RAM we relocate a part
  118. * of the code to the first megabyte of RAM, so the CPU can use it in
  119. * real-mode. This code lives at asm_realmode_code.
  120. */
  121. .globl asm_realmode_code
  122. asm_realmode_code:
  123. /* Realmode IDT pointer structure. */
  124. __realmode_idt = PTR_TO_REAL_MODE(.)
  125. .word 1023 /* 16 bit limit */
  126. .long 0 /* 24 bit base */
  127. .word 0
  128. /* Preserve old stack */
  129. __stack = PTR_TO_REAL_MODE(.)
  130. .long 0
  131. /* Register store for realmode_call and realmode_interrupt */
  132. __registers = PTR_TO_REAL_MODE(.)
  133. .long 0 /* 0 - EAX */
  134. .long 0 /* 4 - EBX */
  135. .long 0 /* 8 - ECX */
  136. .long 0 /* 12 - EDX */
  137. .long 0 /* 16 - ESI */
  138. .long 0 /* 20 - EDI */
  139. /* 256 byte buffer, used by int10 */
  140. .globl asm_realmode_buffer
  141. asm_realmode_buffer:
  142. .skip 256
  143. .code32
  144. .globl asm_realmode_call
  145. asm_realmode_call:
  146. /* save all registers to the stack */
  147. pusha
  148. pushf
  149. movl %esp, __stack
  150. movl %esp, %ebp
  151. /*
  152. * This function is called with regparm=0 and we have to skip the
  153. * 36 bytes from pushf+pusha. Hence start at 40.
  154. * Set up our call instruction.
  155. */
  156. movl 40(%ebp), %eax
  157. mov %ax, __lcall_instr + 1
  158. andl $0xffff0000, %eax
  159. shrl $4, %eax
  160. mov %ax, __lcall_instr + 3
  161. wbinvd
  162. setup_registers
  163. enter_real_mode
  164. prepare_for_irom
  165. __lcall_instr = PTR_TO_REAL_MODE(.)
  166. .byte 0x9a
  167. .word 0x0000, 0x0000
  168. enter_protected_mode
  169. /* restore stack pointer, eflags and register values and exit */
  170. movl __stack, %esp
  171. popf
  172. popa
  173. ret
  174. .globl __realmode_interrupt
  175. __realmode_interrupt:
  176. /* save all registers to the stack and store the stack pointer */
  177. pusha
  178. pushf
  179. movl %esp, __stack
  180. movl %esp, %ebp
  181. /*
  182. * This function is called with regparm=0 and we have to skip the
  183. * 36 bytes from pushf+pusha. Hence start at 40.
  184. * Prepare interrupt calling code.
  185. */
  186. movl 40(%ebp), %eax
  187. movb %al, __intXX_instr + 1 /* intno */
  188. setup_registers
  189. enter_real_mode
  190. prepare_for_irom
  191. __intXX_instr = PTR_TO_REAL_MODE(.)
  192. .byte 0xcd, 0x00 /* This becomes intXX */
  193. enter_protected_mode
  194. /* restore stack pointer, eflags and register values and exit */
  195. movl __stack, %esp
  196. popf
  197. popa
  198. ret
  199. /*
  200. * This is the 16-bit interrupt entry point called by the IDT stub code.
  201. *
  202. * Before this code code is called, %eax is pushed to the stack, and the
  203. * interrupt number is loaded into %al. On return this function cleans up
  204. * for its caller.
  205. */
  206. .code16
  207. __interrupt_handler_16bit = PTR_TO_REAL_MODE(.)
  208. push %ds
  209. push %es
  210. push %fs
  211. push %gs
  212. /* Save real mode SS */
  213. movw %ss, %cs:__realmode_ss
  214. /* Clear DF to not break ABI assumptions */
  215. cld
  216. /*
  217. * Clean up the interrupt number. We could do this in the stub, but
  218. * it would cost two more bytes per stub entry.
  219. */
  220. andl $0xff, %eax
  221. pushl %eax /* ... and make it the first parameter */
  222. enter_protected_mode
  223. /*
  224. * Now we are in protected mode. We need compute the right ESP based
  225. * on saved real mode SS otherwise interrupt_handler() won't get
  226. * correct parameters from the stack.
  227. */
  228. movzwl %cs:__realmode_ss, %ecx
  229. shll $4, %ecx
  230. addl %ecx, %esp
  231. /* Call the C interrupt handler */
  232. movl $interrupt_handler, %eax
  233. call *%eax
  234. /* Restore real mode ESP based on saved SS */
  235. movzwl %cs:__realmode_ss, %ecx
  236. shll $4, %ecx
  237. subl %ecx, %esp
  238. enter_real_mode
  239. /* Restore real mode SS */
  240. movw %cs:__realmode_ss, %ss
  241. /*
  242. * Restore all registers, including those manipulated by the C
  243. * handler
  244. */
  245. popl %eax
  246. pop %gs
  247. pop %fs
  248. pop %es
  249. pop %ds
  250. popal
  251. iret
  252. __realmode_ss = PTR_TO_REAL_MODE(.)
  253. .word 0
  254. .globl asm_realmode_code_size
  255. asm_realmode_code_size:
  256. .long . - asm_realmode_code