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 15KB

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