Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

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