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

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