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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716
  1. /*
  2. * librm: a library for interfacing to real-mode code
  3. *
  4. * Michael Brown <mbrown@fensystems.co.uk>
  5. *
  6. */
  7. FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL )
  8. /* Drag in local definitions */
  9. #include "librm.h"
  10. /* For switches to/from protected mode */
  11. #define CR0_PE 1
  12. /* Size of various C data structures */
  13. #define SIZEOF_I386_SEG_REGS 12
  14. #define SIZEOF_I386_REGS 32
  15. #define SIZEOF_REAL_MODE_REGS ( SIZEOF_I386_SEG_REGS + SIZEOF_I386_REGS )
  16. #define SIZEOF_I386_FLAGS 4
  17. #define SIZEOF_I386_ALL_REGS ( SIZEOF_REAL_MODE_REGS + SIZEOF_I386_FLAGS )
  18. /* Size of an address */
  19. #ifdef __x86_64__
  20. #define SIZEOF_ADDR 8
  21. #else
  22. #define SIZEOF_ADDR 4
  23. #endif
  24. /* Selectively assemble code for 32-bit/64-bit builds */
  25. #ifdef __x86_64__
  26. #define if32 if 0
  27. #define if64 if 1
  28. #else
  29. #define if32 if 1
  30. #define if64 if 0
  31. #endif
  32. /****************************************************************************
  33. * Global descriptor table
  34. *
  35. * Call init_librm to set up the GDT before attempting to use any
  36. * protected-mode code.
  37. *
  38. * NOTE: This must be located before prot_to_real, otherwise gas
  39. * throws a "can't handle non absolute segment in `ljmp'" error due to
  40. * not knowing the value of REAL_CS when the ljmp is encountered.
  41. *
  42. * Note also that putting ".word gdt_end - gdt - 1" directly into
  43. * gdt_limit, rather than going via gdt_length, will also produce the
  44. * "non absolute segment" error. This is most probably a bug in gas.
  45. ****************************************************************************
  46. */
  47. .section ".data16.gdt", "aw", @progbits
  48. .align 16
  49. gdt:
  50. gdtr: /* The first GDT entry is unused, the GDTR can fit here. */
  51. gdt_limit: .word gdt_length - 1
  52. gdt_base: .long 0
  53. .word 0 /* padding */
  54. .org gdt + VIRTUAL_CS, 0
  55. virtual_cs: /* 32 bit protected mode code segment, virtual addresses */
  56. .word 0xffff, 0
  57. .byte 0, 0x9f, 0xcf, 0
  58. .org gdt + VIRTUAL_DS, 0
  59. virtual_ds: /* 32 bit protected mode data segment, virtual addresses */
  60. .word 0xffff, 0
  61. .byte 0, 0x93, 0xcf, 0
  62. .org gdt + PHYSICAL_CS, 0
  63. physical_cs: /* 32 bit protected mode code segment, physical addresses */
  64. .word 0xffff, 0
  65. .byte 0, 0x9f, 0xcf, 0
  66. .org gdt + PHYSICAL_DS, 0
  67. physical_ds: /* 32 bit protected mode data segment, physical addresses */
  68. .word 0xffff, 0
  69. .byte 0, 0x93, 0xcf, 0
  70. .org gdt + REAL_CS, 0
  71. real_cs: /* 16 bit real mode code segment */
  72. .word 0xffff, 0
  73. .byte 0, 0x9b, 0x00, 0
  74. .org gdt + REAL_DS, 0
  75. real_ds: /* 16 bit real mode data segment */
  76. .word 0xffff, 0
  77. .byte 0, 0x93, 0x00, 0
  78. .org gdt + P2R_DS, 0
  79. p2r_ds: /* 16 bit real mode data segment for prot_to_real transition */
  80. .word 0xffff, ( P2R_DS << 4 )
  81. .byte 0, 0x93, 0x00, 0
  82. gdt_end:
  83. .equ gdt_length, gdt_end - gdt
  84. /****************************************************************************
  85. * Stored real-mode and protected-mode stack pointers
  86. *
  87. * The real-mode stack pointer is stored here whenever real_to_prot
  88. * is called and restored whenever prot_to_real is called. The
  89. * converse happens for the protected-mode stack pointer.
  90. *
  91. * Despite initial appearances this scheme is, in fact re-entrant,
  92. * because program flow dictates that we always return via the point
  93. * we left by. For example:
  94. * PXE API call entry
  95. * 1 real => prot
  96. * ...
  97. * Print a text string
  98. * ...
  99. * 2 prot => real
  100. * INT 10
  101. * 3 real => prot
  102. * ...
  103. * ...
  104. * 4 prot => real
  105. * PXE API call exit
  106. *
  107. * At point 1, the RM mode stack value, say RPXE, is stored in
  108. * rm_ss,sp. We want this value to still be present in rm_ss,sp when
  109. * we reach point 4.
  110. *
  111. * At point 2, the RM stack value is restored from RPXE. At point 3,
  112. * the RM stack value is again stored in rm_ss,sp. This *does*
  113. * overwrite the RPXE that we have stored there, but it's the same
  114. * value, since the code between points 2 and 3 has managed to return
  115. * to us.
  116. ****************************************************************************
  117. */
  118. .section ".bss.rm_sp", "aw", @nobits
  119. .globl rm_sp
  120. rm_sp: .word 0
  121. .section ".bss.rm_ss", "aw", @nobits
  122. .globl rm_ss
  123. rm_ss: .word 0
  124. .section ".data.pm_esp", "aw", @progbits
  125. pm_esp: .long VIRTUAL(_estack)
  126. /****************************************************************************
  127. * Virtual address offsets
  128. *
  129. * These are used by the protected-mode code to map between virtual
  130. * and physical addresses, and to access variables in the .text16 or
  131. * .data16 segments.
  132. ****************************************************************************
  133. */
  134. .struct 0
  135. VA_VIRT_OFFSET: .space SIZEOF_ADDR
  136. VA_TEXT16: .space SIZEOF_ADDR
  137. VA_DATA16: .space SIZEOF_ADDR
  138. VA_SIZE:
  139. .previous
  140. /* Internal copies, used only by librm itself */
  141. .section ".bss16.rm_virt_addrs", "aw", @nobits
  142. rm_virt_addrs: .space VA_SIZE
  143. .equ rm_virt_offset, ( rm_virt_addrs + VA_VIRT_OFFSET )
  144. .equ rm_text16, ( rm_virt_addrs + VA_TEXT16 )
  145. .equ rm_data16, ( rm_virt_addrs + VA_DATA16 )
  146. /* Externally visible variables, used by C code */
  147. .section ".bss.virt_addrs", "aw", @nobits
  148. virt_addrs: .space VA_SIZE
  149. .globl virt_offset
  150. .equ virt_offset, ( virt_addrs + VA_VIRT_OFFSET )
  151. .globl text16
  152. .equ text16, ( virt_addrs + VA_TEXT16 )
  153. .globl data16
  154. .equ data16, ( virt_addrs + VA_DATA16 )
  155. /****************************************************************************
  156. * init_librm (real-mode far call, 16-bit real-mode far return address)
  157. *
  158. * Initialise the GDT ready for transitions to protected mode.
  159. *
  160. * Parameters:
  161. * %cs : .text16 segment
  162. * %ds : .data16 segment
  163. * %edi : Physical base of protected-mode code
  164. ****************************************************************************
  165. */
  166. .section ".text16.init_librm", "ax", @progbits
  167. .code16
  168. .globl init_librm
  169. init_librm:
  170. /* Preserve registers */
  171. pushl %eax
  172. pushl %ebx
  173. pushl %edi
  174. /* Store rm_virt_offset and set up virtual_cs and virtual_ds segments */
  175. subl $VIRTUAL(_textdata), %edi
  176. movl %edi, rm_virt_offset
  177. .if64 ; setae (rm_virt_offset+4) ; .endif
  178. movl %edi, %eax
  179. movw $virtual_cs, %bx
  180. call set_seg_base
  181. movw $virtual_ds, %bx
  182. call set_seg_base
  183. /* Store rm_cs and rm_text16, set up real_cs segment */
  184. xorl %eax, %eax
  185. movw %cs, %ax
  186. movw %ax, %cs:rm_cs
  187. shll $4, %eax
  188. movw $real_cs, %bx
  189. call set_seg_base
  190. .if32 ; subl %edi, %eax ; .endif
  191. movl %eax, rm_text16
  192. /* Store rm_ds and rm_data16, set up real_ds segment and GDT base */
  193. xorl %eax, %eax
  194. movw %ds, %ax
  195. movw %ax, %cs:rm_ds
  196. shll $4, %eax
  197. movw $real_ds, %bx
  198. call set_seg_base
  199. movl %eax, gdt_base
  200. addl $gdt, gdt_base
  201. .if32 ; subl %edi, %eax ; .endif
  202. movl %eax, rm_data16
  203. /* Switch to protected mode */
  204. virtcall init_librm_pmode
  205. .section ".text.init_librm", "ax", @progbits
  206. .code32
  207. init_librm_pmode:
  208. /* Store virt_offset, text16, and data16 */
  209. pushw %ds
  210. movw $REAL_DS, %ax
  211. movw %ax, %ds
  212. movl $rm_virt_addrs, %esi
  213. movl $VIRTUAL(virt_addrs), %edi
  214. movl $( VA_SIZE / 4 ), %ecx
  215. rep movsl
  216. popw %ds
  217. /* Return to real mode */
  218. ret
  219. .section ".text16.init_librm", "ax", @progbits
  220. .code16
  221. init_librm_rmode:
  222. /* Initialise IDT */
  223. virtcall init_idt
  224. /* Restore registers */
  225. popl %edi
  226. popl %ebx
  227. popl %eax
  228. lret
  229. .section ".text16.set_seg_base", "ax", @progbits
  230. .code16
  231. set_seg_base:
  232. 1: movw %ax, 2(%bx)
  233. rorl $16, %eax
  234. movb %al, 4(%bx)
  235. movb %ah, 7(%bx)
  236. roll $16, %eax
  237. ret
  238. /****************************************************************************
  239. * real_to_prot (real-mode near call, 32-bit virtual return address)
  240. *
  241. * Switch from 16-bit real-mode to 32-bit protected mode with virtual
  242. * addresses. The real-mode %ss:sp is stored in rm_ss and rm_sp, and
  243. * the protected-mode %esp is restored from the saved pm_esp.
  244. * Interrupts are disabled. All other registers may be destroyed.
  245. *
  246. * The return address for this function should be a 32-bit virtual
  247. * address.
  248. *
  249. * Parameters:
  250. * %ecx : number of bytes to move from RM stack to PM stack
  251. *
  252. ****************************************************************************
  253. */
  254. .section ".text16.real_to_prot", "ax", @progbits
  255. .code16
  256. real_to_prot:
  257. /* Enable A20 line */
  258. call enable_a20
  259. /* A failure at this point is fatal, and there's nothing we
  260. * can do about it other than lock the machine to make the
  261. * problem immediately visible.
  262. */
  263. 1: jc 1b
  264. /* Make sure we have our data segment available */
  265. movw %cs:rm_ds, %ds
  266. /* Add protected-mode return address to length of data to be copied */
  267. addw $4, %cx /* %ecx must be less than 64kB anyway */
  268. /* Real-mode %ss:%sp => %ebp:%edx and virtual address => %esi */
  269. xorl %ebp, %ebp
  270. movw %ss, %bp
  271. movzwl %sp, %edx
  272. movl %ebp, %eax
  273. shll $4, %eax
  274. addr32 leal (%eax,%edx), %esi
  275. subl rm_virt_offset, %esi
  276. /* Load protected-mode global descriptor table */
  277. data32 lgdt gdtr
  278. /* Zero segment registers. This wastes around 12 cycles on
  279. * real hardware, but saves a substantial number of emulated
  280. * instructions under KVM.
  281. */
  282. xorw %ax, %ax
  283. movw %ax, %ds
  284. movw %ax, %es
  285. movw %ax, %fs
  286. movw %ax, %gs
  287. movw %ax, %ss
  288. /* Switch to protected mode */
  289. cli
  290. movl %cr0, %eax
  291. orb $CR0_PE, %al
  292. movl %eax, %cr0
  293. data32 ljmp $VIRTUAL_CS, $VIRTUAL(r2p_pmode)
  294. .section ".text.real_to_prot", "ax", @progbits
  295. .code32
  296. r2p_pmode:
  297. /* Set up protected-mode data segments and stack pointer */
  298. movw $VIRTUAL_DS, %ax
  299. movw %ax, %ds
  300. movw %ax, %es
  301. movw %ax, %fs
  302. movw %ax, %gs
  303. movw %ax, %ss
  304. movl VIRTUAL(pm_esp), %esp
  305. /* Load protected-mode interrupt descriptor table */
  306. lidt VIRTUAL(idtr)
  307. /* Record real-mode %ss:sp (after removal of data) */
  308. movw %bp, VIRTUAL(rm_ss)
  309. addl %ecx, %edx
  310. movw %dx, VIRTUAL(rm_sp)
  311. /* Move data from RM stack to PM stack */
  312. subl %ecx, %esp
  313. movl %esp, %edi
  314. rep movsb
  315. /* Return to virtual address */
  316. ret
  317. /****************************************************************************
  318. * prot_to_real (protected-mode near call, 32-bit real-mode return address)
  319. *
  320. * Switch from 32-bit protected mode with virtual addresses to 16-bit
  321. * real mode. The protected-mode %esp is stored in pm_esp and the
  322. * real-mode %ss:sp is restored from the saved rm_ss and rm_sp. The
  323. * high word of the real-mode %esp is set to zero. All real-mode data
  324. * segment registers are loaded from the saved rm_ds. Interrupts are
  325. * *not* enabled, since we want to be able to use prot_to_real in an
  326. * ISR. All other registers may be destroyed.
  327. *
  328. * The return address for this function should be a 32-bit (sic)
  329. * real-mode offset within .code16.
  330. *
  331. * Parameters:
  332. * %ecx : number of bytes to move from PM stack to RM stack
  333. * %esi : real-mode global and interrupt descriptor table registers
  334. *
  335. ****************************************************************************
  336. */
  337. .section ".text.prot_to_real", "ax", @progbits
  338. .code32
  339. prot_to_real:
  340. /* Copy real-mode global descriptor table register to RM code segment */
  341. movl VIRTUAL(text16), %edi
  342. .if64 ; subl VIRTUAL(virt_offset), %edi ; .endif
  343. leal rm_gdtr(%edi), %edi
  344. movsw
  345. movsl
  346. /* Load real-mode interrupt descriptor table register */
  347. lidt (%esi)
  348. /* Add return address to data to be moved to RM stack */
  349. addl $4, %ecx
  350. /* Real-mode %ss:sp => %ebp:edx and virtual address => %edi */
  351. movzwl VIRTUAL(rm_ss), %ebp
  352. movzwl VIRTUAL(rm_sp), %edx
  353. subl %ecx, %edx
  354. movl %ebp, %eax
  355. shll $4, %eax
  356. leal (%eax,%edx), %edi
  357. subl VIRTUAL(virt_offset), %edi
  358. /* Move data from PM stack to RM stack */
  359. movl %esp, %esi
  360. rep movsb
  361. /* Record protected-mode %esp (after removal of data) */
  362. movl %esi, VIRTUAL(pm_esp)
  363. /* Load real-mode segment limits */
  364. movw $P2R_DS, %ax
  365. movw %ax, %ds
  366. movw %ax, %es
  367. movw %ax, %fs
  368. movw %ax, %gs
  369. movw %ax, %ss
  370. ljmp $REAL_CS, $p2r_rmode
  371. .section ".text16.prot_to_real", "ax", @progbits
  372. .code16
  373. p2r_rmode:
  374. /* Load real-mode GDT */
  375. data32 lgdt %cs:rm_gdtr
  376. /* Switch to real mode */
  377. movl %cr0, %eax
  378. andb $0!CR0_PE, %al
  379. movl %eax, %cr0
  380. p2r_ljmp_rm_cs:
  381. ljmp $0, $1f
  382. 1:
  383. /* Set up real-mode data segments and stack pointer */
  384. movw %cs:rm_ds, %ax
  385. movw %ax, %ds
  386. movw %ax, %es
  387. movw %ax, %fs
  388. movw %ax, %gs
  389. movw %bp, %ss
  390. movl %edx, %esp
  391. /* Return to real-mode address */
  392. data32 ret
  393. /* Real-mode code and data segments. Assigned by the call to
  394. * init_librm. rm_cs doubles as the segment part of the jump
  395. * instruction used by prot_to_real. Both are located in
  396. * .text16 rather than .data16: rm_cs since it forms part of
  397. * the jump instruction within the code segment, and rm_ds
  398. * since real-mode code needs to be able to locate the data
  399. * segment with no other reference available.
  400. */
  401. .globl rm_cs
  402. .equ rm_cs, ( p2r_ljmp_rm_cs + 3 )
  403. .section ".text16.data.rm_ds", "aw", @progbits
  404. .globl rm_ds
  405. rm_ds: .word 0
  406. /* Real-mode global and interrupt descriptor table registers */
  407. .section ".text16.data.rm_gdtr", "aw", @progbits
  408. rm_gdtr:
  409. .word 0 /* Limit */
  410. .long 0 /* Base */
  411. /****************************************************************************
  412. * prot_call (real-mode near call, 16-bit real-mode near return address)
  413. *
  414. * Call a specific C function in the protected-mode code. The
  415. * prototype of the C function must be
  416. * void function ( struct i386_all_regs *ix86 );
  417. * ix86 will point to a struct containing the real-mode registers
  418. * at entry to prot_call.
  419. *
  420. * All registers will be preserved across prot_call(), unless the C
  421. * function explicitly overwrites values in ix86. Interrupt status
  422. * and GDT will also be preserved. Gate A20 will be enabled.
  423. *
  424. * Note that prot_call() does not rely on the real-mode stack
  425. * remaining intact in order to return, since everything relevant is
  426. * copied to the protected-mode stack for the duration of the call.
  427. * In particular, this means that a real-mode prefix can make a call
  428. * to main() which will return correctly even if the prefix's stack
  429. * gets vapourised during the Etherboot run. (The prefix cannot rely
  430. * on anything else on the stack being preserved, so should move any
  431. * critical data to registers before calling main()).
  432. *
  433. * Parameters:
  434. * function : virtual address of protected-mode function to call
  435. *
  436. * Example usage:
  437. * pushl $pxe_api_call
  438. * call prot_call
  439. * to call in to the C function
  440. * void pxe_api_call ( struct i386_all_regs *ix86 );
  441. ****************************************************************************
  442. */
  443. .struct 0
  444. PC_OFFSET_GDT: .space 6
  445. PC_OFFSET_IDT: .space 6
  446. PC_OFFSET_IX86: .space SIZEOF_I386_ALL_REGS
  447. PC_OFFSET_PADDING: .space 2 /* for alignment */
  448. PC_OFFSET_RETADDR: .space 2
  449. PC_OFFSET_FUNCTION: .space 4
  450. PC_OFFSET_END:
  451. .previous
  452. .section ".text16.prot_call", "ax", @progbits
  453. .code16
  454. .globl prot_call
  455. prot_call:
  456. /* Preserve registers, flags and GDT on external RM stack */
  457. pushfw /* padding */
  458. pushfl
  459. pushal
  460. pushw %gs
  461. pushw %fs
  462. pushw %es
  463. pushw %ds
  464. pushw %ss
  465. pushw %cs
  466. subw $PC_OFFSET_IX86, %sp
  467. movw %sp, %bp
  468. sidt PC_OFFSET_IDT(%bp)
  469. sgdt PC_OFFSET_GDT(%bp)
  470. /* For sanity's sake, clear the direction flag as soon as possible */
  471. cld
  472. /* Switch to protected mode and move register dump to PM stack */
  473. movl $PC_OFFSET_END, %ecx
  474. pushl $VIRTUAL(pc_pmode)
  475. jmp real_to_prot
  476. .section ".text.prot_call", "ax", @progbits
  477. .code32
  478. pc_pmode:
  479. /* Call function */
  480. leal PC_OFFSET_IX86(%esp), %eax
  481. pushl %eax
  482. call *(PC_OFFSET_FUNCTION+4)(%esp)
  483. popl %eax /* discard */
  484. /* Switch to real mode and move register dump back to RM stack */
  485. movl $PC_OFFSET_END, %ecx
  486. movl %esp, %esi
  487. pushl $pc_rmode
  488. jmp prot_to_real
  489. .section ".text16.prot_call", "ax", @progbits
  490. .code16
  491. pc_rmode:
  492. /* Restore registers and flags and return */
  493. addw $( PC_OFFSET_IX86 + 4 /* also skip %cs and %ss */ ), %sp
  494. popw %ds
  495. popw %es
  496. popw %fs
  497. popw %gs
  498. popal
  499. /* popal skips %esp. We therefore want to do "movl -20(%sp),
  500. * %esp", but -20(%sp) is not a valid 80386 expression.
  501. * Fortunately, prot_to_real() zeroes the high word of %esp, so
  502. * we can just use -20(%esp) instead.
  503. */
  504. addr32 movl -20(%esp), %esp
  505. popfl
  506. popfw /* padding */
  507. ret $4
  508. /****************************************************************************
  509. * real_call (protected-mode near call, 32-bit virtual return address)
  510. *
  511. * Call a real-mode function from protected-mode code.
  512. *
  513. * The non-segment register values will be passed directly to the
  514. * real-mode code. The segment registers will be set as per
  515. * prot_to_real. The non-segment register values set by the real-mode
  516. * function will be passed back to the protected-mode caller. A
  517. * result of this is that this routine cannot be called directly from
  518. * C code, since it clobbers registers that the C ABI expects the
  519. * callee to preserve.
  520. *
  521. * librm.h defines a convenient macro REAL_CODE() for using real_call.
  522. * See librm.h and realmode.h for details and examples.
  523. *
  524. * Parameters:
  525. * (32-bit) near pointer to real-mode function to call
  526. *
  527. * Returns: none
  528. ****************************************************************************
  529. */
  530. .struct 0
  531. RC_OFFSET_REGS: .space SIZEOF_I386_REGS
  532. RC_OFFSET_REGS_END:
  533. RC_OFFSET_RETADDR: .space 4
  534. RC_OFFSET_FUNCTION: .space 4
  535. RC_OFFSET_END:
  536. .previous
  537. .section ".text.real_call", "ax", @progbits
  538. .code32
  539. .globl real_call
  540. real_call:
  541. /* Create register dump and function pointer copy on PM stack */
  542. pushal
  543. pushl RC_OFFSET_FUNCTION(%esp)
  544. /* Switch to real mode and move register dump to RM stack */
  545. movl $( RC_OFFSET_REGS_END + 4 /* function pointer copy */ ), %ecx
  546. pushl $rc_rmode
  547. movl $VIRTUAL(rm_default_gdtr_idtr), %esi
  548. jmp prot_to_real
  549. .section ".text16.real_call", "ax", @progbits
  550. .code16
  551. rc_rmode:
  552. /* Call real-mode function */
  553. popl rc_function
  554. popal
  555. call *rc_function
  556. pushal
  557. /* For sanity's sake, clear the direction flag as soon as possible */
  558. cld
  559. /* Switch to protected mode and move register dump back to PM stack */
  560. movl $RC_OFFSET_REGS_END, %ecx
  561. pushl $VIRTUAL(rc_pmode)
  562. jmp real_to_prot
  563. .section ".text.real_call", "ax", @progbits
  564. .code32
  565. rc_pmode:
  566. /* Restore registers and return */
  567. popal
  568. ret $4
  569. /* Function vector, used because "call xx(%sp)" is not a valid
  570. * 16-bit expression.
  571. */
  572. .section ".bss16.rc_function", "aw", @nobits
  573. rc_function: .word 0, 0
  574. /* Default real-mode global and interrupt descriptor table registers */
  575. .section ".data.rm_default_gdtr_idtr", "aw", @progbits
  576. rm_default_gdtr_idtr:
  577. .word 0 /* Global descriptor table limit */
  578. .long 0 /* Global descriptor table base */
  579. .word 0x03ff /* Interrupt descriptor table limit */
  580. .long 0 /* Interrupt descriptor table base */
  581. /****************************************************************************
  582. * flatten_real_mode (real-mode near call)
  583. *
  584. * Switch to flat real mode
  585. *
  586. ****************************************************************************
  587. */
  588. .section ".text16.flatten_real_mode", "ax", @progbits
  589. .code16
  590. .globl flatten_real_mode
  591. flatten_real_mode:
  592. /* Modify GDT to use flat real mode */
  593. movb $0x8f, real_cs + 6
  594. movb $0x8f, real_ds + 6
  595. /* Call dummy protected-mode function */
  596. virtcall flatten_dummy
  597. /* Restore GDT */
  598. movb $0x00, real_cs + 6
  599. movb $0x00, real_ds + 6
  600. /* Return */
  601. ret
  602. .section ".text.flatten_dummy", "ax", @progbits
  603. .code32
  604. flatten_dummy:
  605. ret
  606. /****************************************************************************
  607. * Interrupt wrapper
  608. *
  609. * Used by the protected-mode interrupt vectors to call the
  610. * interrupt() function.
  611. *
  612. * May be entered with either physical or virtual stack segment.
  613. ****************************************************************************
  614. */
  615. .section ".text.interrupt_wrapper", "ax", @progbits
  616. .code32
  617. .globl interrupt_wrapper
  618. interrupt_wrapper:
  619. /* Preserve segment registers and original %esp */
  620. pushl %ds
  621. pushl %es
  622. pushl %fs
  623. pushl %gs
  624. pushl %ss
  625. pushl %esp
  626. /* Switch to virtual addressing */
  627. call _intr_to_virt
  628. /* Expand IRQ number to whole %eax register */
  629. movzbl %al, %eax
  630. /* Call interrupt handler */
  631. call interrupt
  632. /* Restore original stack and segment registers */
  633. lss (%esp), %esp
  634. popl %ss
  635. popl %gs
  636. popl %fs
  637. popl %es
  638. popl %ds
  639. /* Restore registers and return */
  640. popal
  641. iret