bios_asm.S 6.5 KB

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