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.

librm.S 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571
  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 )
  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. .arch i386
  19. /****************************************************************************
  20. * Global descriptor table
  21. *
  22. * Call init_librm to set up the GDT before attempting to use any
  23. * protected-mode code.
  24. *
  25. * NOTE: This must be located before prot_to_real, otherwise gas
  26. * throws a "can't handle non absolute segment in `ljmp'" error due to
  27. * not knowing the value of REAL_CS when the ljmp is encountered.
  28. *
  29. * Note also that putting ".word gdt_end - gdt - 1" directly into
  30. * gdt_limit, rather than going via gdt_length, will also produce the
  31. * "non absolute segment" error. This is most probably a bug in gas.
  32. ****************************************************************************
  33. */
  34. .section ".data16", "aw", @progbits
  35. .align 16
  36. gdt:
  37. gdtr: /* The first GDT entry is unused, the GDTR can fit here. */
  38. gdt_limit: .word gdt_length - 1
  39. gdt_base: .long 0
  40. .word 0 /* padding */
  41. .org gdt + VIRTUAL_CS, 0
  42. virtual_cs: /* 32 bit protected mode code segment, virtual addresses */
  43. .word 0xffff, 0
  44. .byte 0, 0x9f, 0xcf, 0
  45. .org gdt + VIRTUAL_DS, 0
  46. virtual_ds: /* 32 bit protected mode data segment, virtual addresses */
  47. .word 0xffff, 0
  48. .byte 0, 0x93, 0xcf, 0
  49. .org gdt + PHYSICAL_CS, 0
  50. physical_cs: /* 32 bit protected mode code segment, physical addresses */
  51. .word 0xffff, 0
  52. .byte 0, 0x9f, 0xcf, 0
  53. .org gdt + PHYSICAL_DS, 0
  54. physical_ds: /* 32 bit protected mode data segment, physical addresses */
  55. .word 0xffff, 0
  56. .byte 0, 0x93, 0xcf, 0
  57. .org gdt + REAL_CS, 0
  58. real_cs: /* 16 bit flat real mode code segment */
  59. .word 0xffff, 0
  60. .byte 0, 0x9b, 0x8f, 0
  61. .org gdt + REAL_DS
  62. real_ds: /* 16 bit flat real mode data segment */
  63. .word 0xffff, 0
  64. .byte 0, 0x93, 0x8f, 0
  65. gdt_end:
  66. .equ gdt_length, gdt_end - gdt
  67. /****************************************************************************
  68. * init_librm (real-mode far call, 16-bit real-mode far return address)
  69. *
  70. * Initialise the GDT ready for transitions to protected mode.
  71. *
  72. * Parameters:
  73. * %cs : .text16 segment
  74. * %ds : .data16 segment
  75. * %edi : Physical base of protected-mode code (virt_offset)
  76. ****************************************************************************
  77. */
  78. .section ".text16", "ax", @progbits
  79. .code16
  80. .globl init_librm
  81. init_librm:
  82. /* Preserve registers */
  83. pushl %eax
  84. pushl %ebx
  85. /* Store virt_offset and set up virtual_cs and virtual_ds segments */
  86. movl %edi, %eax
  87. movw $virtual_cs, %bx
  88. call set_seg_base
  89. movw $virtual_ds, %bx
  90. call set_seg_base
  91. movl %edi, rm_virt_offset
  92. /* Negate virt_offset */
  93. negl %edi
  94. /* Store rm_cs and text16, set up real_cs segment */
  95. xorl %eax, %eax
  96. movw %cs, %ax
  97. movw %ax, rm_cs
  98. shll $4, %eax
  99. movw $real_cs, %bx
  100. call set_seg_base
  101. addr32 leal (%eax, %edi), %ebx
  102. movl %ebx, rm_text16
  103. /* Store rm_ds and data16, set up real_ds segment */
  104. xorl %eax, %eax
  105. movw %ds, %ax
  106. movw %ax, %cs:rm_ds
  107. shll $4, %eax
  108. movw $real_ds, %bx
  109. call set_seg_base
  110. addr32 leal (%eax, %edi), %ebx
  111. movl %ebx, rm_data16
  112. /* Set GDT and IDT base */
  113. movl %eax, gdt_base
  114. addl $gdt, gdt_base
  115. call idt_init
  116. /* Restore registers */
  117. negl %edi
  118. popl %ebx
  119. popl %eax
  120. lret
  121. .section ".text16", "ax", @progbits
  122. .code16
  123. .weak idt_init
  124. set_seg_base:
  125. 1: movw %ax, 2(%bx)
  126. rorl $16, %eax
  127. movb %al, 4(%bx)
  128. movb %ah, 7(%bx)
  129. roll $16, %eax
  130. idt_init: /* Reuse the return opcode here */
  131. ret
  132. /****************************************************************************
  133. * real_to_prot (real-mode near call, 32-bit virtual return address)
  134. *
  135. * Switch from 16-bit real-mode to 32-bit protected mode with virtual
  136. * addresses. The real-mode %ss:sp is stored in rm_ss and rm_sp, and
  137. * the protected-mode %esp is restored from the saved pm_esp.
  138. * Interrupts are disabled. All other registers may be destroyed.
  139. *
  140. * The return address for this function should be a 32-bit virtual
  141. * address.
  142. *
  143. * Parameters:
  144. * %ecx : number of bytes to move from RM stack to PM stack
  145. *
  146. ****************************************************************************
  147. */
  148. .section ".text16", "ax", @progbits
  149. .code16
  150. real_to_prot:
  151. /* Enable A20 line */
  152. call enable_a20
  153. /* A failure at this point is fatal, and there's nothing we
  154. * can do about it other than lock the machine to make the
  155. * problem immediately visible.
  156. */
  157. 1: jc 1b
  158. /* Make sure we have our data segment available */
  159. movw %cs:rm_ds, %ax
  160. movw %ax, %ds
  161. /* Add virt_offset, text16 and data16 to stack to be
  162. * copied, and also copy the return address.
  163. */
  164. pushl rm_virt_offset
  165. pushl rm_text16
  166. pushl rm_data16
  167. addw $16, %cx /* %ecx must be less than 64kB anyway */
  168. /* Real-mode %ss:%sp => %ebp:%edx and virtual address => %esi */
  169. xorl %ebp, %ebp
  170. movw %ss, %bp
  171. movzwl %sp, %edx
  172. movl %ebp, %eax
  173. shll $4, %eax
  174. addr32 leal (%eax,%edx), %esi
  175. subl rm_virt_offset, %esi
  176. /* Switch to protected mode */
  177. cli
  178. data32 lgdt gdtr
  179. data32 lidt idtr
  180. movl %cr0, %eax
  181. orb $CR0_PE, %al
  182. movl %eax, %cr0
  183. data32 ljmp $VIRTUAL_CS, $1f
  184. .section ".text", "ax", @progbits
  185. .code32
  186. 1:
  187. /* Set up protected-mode data segments and stack pointer */
  188. movw $VIRTUAL_DS, %ax
  189. movw %ax, %ds
  190. movw %ax, %es
  191. movw %ax, %fs
  192. movw %ax, %gs
  193. movw %ax, %ss
  194. movl pm_esp, %esp
  195. /* Record real-mode %ss:sp (after removal of data) */
  196. movw %bp, rm_ss
  197. addl %ecx, %edx
  198. movw %dx, rm_sp
  199. /* Move data from RM stack to PM stack */
  200. subl %ecx, %esp
  201. movl %esp, %edi
  202. rep movsb
  203. /* Publish virt_offset, text16 and data16 for PM code to use */
  204. popl data16
  205. popl text16
  206. popl virt_offset
  207. /* Return to virtual address */
  208. ret
  209. /* Default IDTR with no interrupts */
  210. .section ".data16", "aw", @progbits
  211. .weak idtr
  212. idtr:
  213. rm_idtr:
  214. .word 0xffff /* limit */
  215. .long 0 /* base */
  216. /****************************************************************************
  217. * prot_to_real (protected-mode near call, 32-bit real-mode return address)
  218. *
  219. * Switch from 32-bit protected mode with virtual addresses to 16-bit
  220. * real mode. The protected-mode %esp is stored in pm_esp and the
  221. * real-mode %ss:sp is restored from the saved rm_ss and rm_sp. The
  222. * high word of the real-mode %esp is set to zero. All real-mode data
  223. * segment registers are loaded from the saved rm_ds. Interrupts are
  224. * *not* enabled, since we want to be able to use prot_to_real in an
  225. * ISR. All other registers may be destroyed.
  226. *
  227. * The return address for this function should be a 32-bit (sic)
  228. * real-mode offset within .code16.
  229. *
  230. * Parameters:
  231. * %ecx : number of bytes to move from PM stack to RM stack
  232. *
  233. ****************************************************************************
  234. */
  235. .section ".text", "ax", @progbits
  236. .code32
  237. prot_to_real:
  238. /* Add return address to data to be moved to RM stack */
  239. addl $4, %ecx
  240. /* Real-mode %ss:sp => %ebp:edx and virtual address => %edi */
  241. movzwl rm_ss, %ebp
  242. movzwl rm_sp, %edx
  243. subl %ecx, %edx
  244. movl %ebp, %eax
  245. shll $4, %eax
  246. leal (%eax,%edx), %edi
  247. subl virt_offset, %edi
  248. /* Move data from PM stack to RM stack */
  249. movl %esp, %esi
  250. rep movsb
  251. /* Record protected-mode %esp (after removal of data) */
  252. movl %esi, pm_esp
  253. /* Load real-mode segment limits */
  254. movw $REAL_DS, %ax
  255. movw %ax, %ds
  256. movw %ax, %es
  257. movw %ax, %fs
  258. movw %ax, %gs
  259. movw %ax, %ss
  260. ljmp $REAL_CS, $1f
  261. .section ".text16", "ax", @progbits
  262. .code16
  263. 1:
  264. /* Switch to real mode */
  265. movl %cr0, %eax
  266. andb $0!CR0_PE, %al
  267. movl %eax, %cr0
  268. ljmp *p2r_jump_vector
  269. p2r_jump_target:
  270. /* Set up real-mode data segments and stack pointer */
  271. movw %cs:rm_ds, %ax
  272. movw %ax, %ds
  273. movw %ax, %es
  274. movw %ax, %fs
  275. movw %ax, %gs
  276. movw %bp, %ss
  277. movl %edx, %esp
  278. /* Reset IDTR to the real-mode defaults */
  279. data32 lidt rm_idtr
  280. /* Return to real-mode address */
  281. data32 ret
  282. /* Real-mode code and data segments. Assigned by the call to
  283. * init_librm. rm_cs doubles as the segment part of the jump
  284. * vector used by prot_to_real. rm_ds is located in .text16
  285. * rather than .data16 because code needs to be able to locate
  286. * the data segment.
  287. */
  288. .section ".data16", "aw", @progbits
  289. p2r_jump_vector:
  290. .word p2r_jump_target
  291. .globl rm_cs
  292. rm_cs: .word 0
  293. .globl rm_ds
  294. .section ".text16.data", "aw", @progbits
  295. rm_ds: .word 0
  296. /****************************************************************************
  297. * prot_call (real-mode far call, 16-bit real-mode far return address)
  298. *
  299. * Call a specific C function in the protected-mode code. The
  300. * prototype of the C function must be
  301. * void function ( struct i386_all_regs *ix86 );
  302. * ix86 will point to a struct containing the real-mode registers
  303. * at entry to prot_call.
  304. *
  305. * All registers will be preserved across prot_call(), unless the C
  306. * function explicitly overwrites values in ix86. Interrupt status
  307. * and GDT will also be preserved. Gate A20 will be enabled.
  308. *
  309. * Note that prot_call() does not rely on the real-mode stack
  310. * remaining intact in order to return, since everything relevant is
  311. * copied to the protected-mode stack for the duration of the call.
  312. * In particular, this means that a real-mode prefix can make a call
  313. * to main() which will return correctly even if the prefix's stack
  314. * gets vapourised during the Etherboot run. (The prefix cannot rely
  315. * on anything else on the stack being preserved, so should move any
  316. * critical data to registers before calling main()).
  317. *
  318. * Parameters:
  319. * function : virtual address of protected-mode function to call
  320. *
  321. * Example usage:
  322. * pushl $pxe_api_call
  323. * call prot_call
  324. * addw $4, %sp
  325. * to call in to the C function
  326. * void pxe_api_call ( struct i386_all_regs *ix86 );
  327. ****************************************************************************
  328. */
  329. #define PC_OFFSET_GDT ( 0 )
  330. #define PC_OFFSET_IDT ( PC_OFFSET_GDT + 8 /* pad to 8 to keep alignment */ )
  331. #define PC_OFFSET_IX86 ( PC_OFFSET_IDT + 8 /* pad to 8 to keep alignment */ )
  332. #define PC_OFFSET_RETADDR ( PC_OFFSET_IX86 + SIZEOF_I386_ALL_REGS )
  333. #define PC_OFFSET_FUNCTION ( PC_OFFSET_RETADDR + 4 )
  334. #define PC_OFFSET_END ( PC_OFFSET_FUNCTION + 4 )
  335. .section ".text16", "ax", @progbits
  336. .code16
  337. .globl prot_call
  338. prot_call:
  339. /* Preserve registers, flags and GDT on external RM stack */
  340. pushfl
  341. pushal
  342. pushw %gs
  343. pushw %fs
  344. pushw %es
  345. pushw %ds
  346. pushw %ss
  347. pushw %cs
  348. subw $16, %sp
  349. movw %sp, %bp
  350. sidt 8(%bp)
  351. sgdt (%bp)
  352. /* For sanity's sake, clear the direction flag as soon as possible */
  353. cld
  354. /* Switch to protected mode and move register dump to PM stack */
  355. movl $PC_OFFSET_END, %ecx
  356. pushl $1f
  357. jmp real_to_prot
  358. .section ".text", "ax", @progbits
  359. .code32
  360. 1:
  361. /* Call function */
  362. leal PC_OFFSET_IX86(%esp), %eax
  363. pushl %eax
  364. call *(PC_OFFSET_FUNCTION+4)(%esp)
  365. popl %eax /* discard */
  366. /* Switch to real mode and move register dump back to RM stack */
  367. movl $PC_OFFSET_END, %ecx
  368. pushl $1f
  369. jmp prot_to_real
  370. .section ".text16", "ax", @progbits
  371. .code16
  372. 1:
  373. /* Reload GDT and IDT, restore registers and flags and return */
  374. movw %sp, %bp
  375. data32 lgdt (%bp)
  376. data32 lidt 8(%bp)
  377. addw $20, %sp /* also skip %cs and %ss */
  378. popw %ds
  379. popw %es
  380. popw %fs
  381. popw %gs
  382. popal
  383. /* popal skips %esp. We therefore want to do "movl -20(%sp),
  384. * %esp", but -20(%sp) is not a valid 80386 expression.
  385. * Fortunately, prot_to_real() zeroes the high word of %esp, so
  386. * we can just use -20(%esp) instead.
  387. */
  388. addr32 movl -20(%esp), %esp
  389. popfl
  390. lret
  391. /****************************************************************************
  392. * real_call (protected-mode near call, 32-bit virtual return address)
  393. *
  394. * Call a real-mode function from protected-mode code.
  395. *
  396. * The non-segment register values will be passed directly to the
  397. * real-mode code. The segment registers will be set as per
  398. * prot_to_real. The non-segment register values set by the real-mode
  399. * function will be passed back to the protected-mode caller. A
  400. * result of this is that this routine cannot be called directly from
  401. * C code, since it clobbers registers that the C ABI expects the
  402. * callee to preserve.
  403. *
  404. * librm.h defines a convenient macro REAL_CODE() for using real_call.
  405. * See librm.h and realmode.h for details and examples.
  406. *
  407. * Parameters:
  408. * (32-bit) near pointer to real-mode function to call
  409. *
  410. * Returns: none
  411. ****************************************************************************
  412. */
  413. #define RC_OFFSET_PRESERVE_REGS ( 0 )
  414. #define RC_OFFSET_RETADDR ( RC_OFFSET_PRESERVE_REGS + SIZEOF_I386_REGS )
  415. #define RC_OFFSET_FUNCTION ( RC_OFFSET_RETADDR + 4 )
  416. #define RC_OFFSET_END ( RC_OFFSET_FUNCTION + 4 )
  417. .section ".text", "ax", @progbits
  418. .code32
  419. .globl real_call
  420. real_call:
  421. /* Create register dump and function pointer copy on PM stack */
  422. pushal
  423. pushl RC_OFFSET_FUNCTION(%esp)
  424. /* Switch to real mode and move register dump to RM stack */
  425. movl $( RC_OFFSET_RETADDR + 4 /* function pointer copy */ ), %ecx
  426. pushl $1f
  427. jmp prot_to_real
  428. .section ".text16", "ax", @progbits
  429. .code16
  430. 1:
  431. /* Call real-mode function */
  432. popl rc_function
  433. popal
  434. call *rc_function
  435. pushal
  436. /* For sanity's sake, clear the direction flag as soon as possible */
  437. cld
  438. /* Switch to protected mode and move register dump back to PM stack */
  439. movl $RC_OFFSET_RETADDR, %ecx
  440. pushl $1f
  441. jmp real_to_prot
  442. .section ".text", "ax", @progbits
  443. .code32
  444. 1:
  445. /* Restore registers and return */
  446. popal
  447. ret
  448. /* Function vector, used because "call xx(%sp)" is not a valid
  449. * 16-bit expression.
  450. */
  451. .section ".data16", "aw", @progbits
  452. rc_function: .word 0, 0
  453. /****************************************************************************
  454. * Stored real-mode and protected-mode stack pointers
  455. *
  456. * The real-mode stack pointer is stored here whenever real_to_prot
  457. * is called and restored whenever prot_to_real is called. The
  458. * converse happens for the protected-mode stack pointer.
  459. *
  460. * Despite initial appearances this scheme is, in fact re-entrant,
  461. * because program flow dictates that we always return via the point
  462. * we left by. For example:
  463. * PXE API call entry
  464. * 1 real => prot
  465. * ...
  466. * Print a text string
  467. * ...
  468. * 2 prot => real
  469. * INT 10
  470. * 3 real => prot
  471. * ...
  472. * ...
  473. * 4 prot => real
  474. * PXE API call exit
  475. *
  476. * At point 1, the RM mode stack value, say RPXE, is stored in
  477. * rm_ss,sp. We want this value to still be present in rm_ss,sp when
  478. * we reach point 4.
  479. *
  480. * At point 2, the RM stack value is restored from RPXE. At point 3,
  481. * the RM stack value is again stored in rm_ss,sp. This *does*
  482. * overwrite the RPXE that we have stored there, but it's the same
  483. * value, since the code between points 2 and 3 has managed to return
  484. * to us.
  485. ****************************************************************************
  486. */
  487. .section ".data", "aw", @progbits
  488. .globl rm_sp
  489. rm_sp: .word 0
  490. .globl rm_ss
  491. rm_ss: .word 0
  492. pm_esp: .long _estack
  493. /****************************************************************************
  494. * Virtual address offsets
  495. *
  496. * These are used by the protected-mode code to map between virtual
  497. * and physical addresses, and to access variables in the .text16 or
  498. * .data16 segments.
  499. ****************************************************************************
  500. */
  501. /* Internal copies, created by init_librm (which runs in real mode) */
  502. .section ".data16", "aw", @progbits
  503. rm_virt_offset: .long 0
  504. rm_text16: .long 0
  505. rm_data16: .long 0
  506. /* Externally-visible copies, created by real_to_prot */
  507. .section ".data", "aw", @progbits
  508. .globl virt_offset
  509. virt_offset: .long 0
  510. .globl text16
  511. text16: .long 0
  512. .globl data16
  513. data16: .long 0