您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

librm.S 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579
  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. /****************************************************************************
  19. * Global descriptor table
  20. *
  21. * Call init_librm to set up the GDT before attempting to use any
  22. * protected-mode code.
  23. *
  24. * Define FLATTEN_REAL_MODE if you want to use so-called "flat real
  25. * mode" with 4GB limits instead.
  26. *
  27. * NOTE: This must be located before prot_to_real, otherwise gas
  28. * throws a "can't handle non absolute segment in `ljmp'" error due to
  29. * not knowing the value of REAL_CS when the ljmp is encountered.
  30. *
  31. * Note also that putting ".word gdt_end - gdt - 1" directly into
  32. * gdt_limit, rather than going via gdt_length, will also produce the
  33. * "non absolute segment" error. This is most probably a bug in gas.
  34. ****************************************************************************
  35. */
  36. #ifdef FLATTEN_REAL_MODE
  37. #define RM_LIMIT_16_19__AVL__SIZE__GRANULARITY 0x8f
  38. #else
  39. #define RM_LIMIT_16_19__AVL__SIZE__GRANULARITY 0x00
  40. #endif
  41. .section ".data16", "aw", @progbits
  42. .align 16
  43. gdt:
  44. gdtr: /* The first GDT entry is unused, the GDTR can fit here. */
  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 far call, 16-bit real-mode far 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", "ax", @progbits
  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. addr32 leal (%eax, %edi), %ebx
  109. movl %ebx, _text16
  110. /* Store rm_ds and _data16, set up real_ds segment */
  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. addr32 leal (%eax, %edi), %ebx
  118. movl %ebx, _data16
  119. /* Set GDT and IDT base */
  120. movl %eax, gdt_base
  121. addl $gdt, gdt_base
  122. call idt_init
  123. /* Restore registers */
  124. negl %edi
  125. popl %ebx
  126. popl %eax
  127. lret
  128. .section ".text16", "ax", @progbits
  129. .code16
  130. .weak idt_init
  131. set_seg_base:
  132. 1: movw %ax, 2(%bx)
  133. rorl $16, %eax
  134. movb %al, 4(%bx)
  135. movb %ah, 7(%bx)
  136. roll $16, %eax
  137. idt_init: /* Reuse the return opcode here */
  138. ret
  139. /****************************************************************************
  140. * real_to_prot (real-mode near call, 32-bit virtual return address)
  141. *
  142. * Switch from 16-bit real-mode to 32-bit protected mode with virtual
  143. * addresses. The real-mode %ss:sp is stored in rm_ss and rm_sp, and
  144. * the protected-mode %esp is restored from the saved pm_esp.
  145. * Interrupts are disabled. All other registers may be destroyed.
  146. *
  147. * The return address for this function should be a 32-bit virtual
  148. * address.
  149. *
  150. * Parameters:
  151. * %ecx : number of bytes to move from RM stack to PM stack
  152. *
  153. ****************************************************************************
  154. */
  155. .section ".text16", "ax", @progbits
  156. .code16
  157. real_to_prot:
  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 _virt_offset
  165. pushl _text16
  166. pushl _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 _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. /* Set up environment expected by C code */
  362. call gateA20_set
  363. /* Call function */
  364. leal PC_OFFSET_IX86(%esp), %eax
  365. pushl %eax
  366. call *(PC_OFFSET_FUNCTION+4)(%esp)
  367. popl %eax /* discard */
  368. /* Switch to real mode and move register dump back to RM stack */
  369. movl $PC_OFFSET_END, %ecx
  370. pushl $1f
  371. jmp prot_to_real
  372. .section ".text16", "ax", @progbits
  373. .code16
  374. 1:
  375. /* Reload GDT and IDT, restore registers and flags and return */
  376. movw %sp, %bp
  377. data32 lgdt (%bp)
  378. data32 lidt 8(%bp)
  379. addw $20, %sp /* also skip %cs and %ss */
  380. popw %ds
  381. popw %es
  382. popw %fs
  383. popw %gs
  384. popal
  385. /* popal skips %esp. We therefore want to do "movl -20(%sp),
  386. * %esp", but -20(%sp) is not a valid 80386 expression.
  387. * Fortunately, prot_to_real() zeroes the high word of %esp, so
  388. * we can just use -20(%esp) instead.
  389. */
  390. addr32 movl -20(%esp), %esp
  391. popfl
  392. lret
  393. /****************************************************************************
  394. * real_call (protected-mode near call, 32-bit virtual return address)
  395. *
  396. * Call a real-mode function from protected-mode code.
  397. *
  398. * The non-segment register values will be passed directly to the
  399. * real-mode code. The segment registers will be set as per
  400. * prot_to_real. The non-segment register values set by the real-mode
  401. * function will be passed back to the protected-mode caller. A
  402. * result of this is that this routine cannot be called directly from
  403. * C code, since it clobbers registers that the C ABI expects the
  404. * callee to preserve. Gate A20 will *not* be automatically
  405. * re-enabled. Since we always run from an even megabyte of memory,
  406. * we are guaranteed to return successfully to the protected-mode
  407. * code, which should then call gateA20_set() if it suspects that gate
  408. * A20 may have been disabled. Note that enabling gate A20 is a
  409. * potentially slow operation that may also cause keyboard input to be
  410. * lost; this is why it is not done automatically.
  411. *
  412. * librm.h defines a convenient macro REAL_CODE() for using real_call.
  413. * See librm.h and realmode.h for details and examples.
  414. *
  415. * Parameters:
  416. * (32-bit) near pointer to real-mode function to call
  417. *
  418. * Returns: none
  419. ****************************************************************************
  420. */
  421. #define RC_OFFSET_PRESERVE_REGS ( 0 )
  422. #define RC_OFFSET_RETADDR ( RC_OFFSET_PRESERVE_REGS + SIZEOF_I386_REGS )
  423. #define RC_OFFSET_FUNCTION ( RC_OFFSET_RETADDR + 4 )
  424. #define RC_OFFSET_END ( RC_OFFSET_FUNCTION + 4 )
  425. .section ".text", "ax", @progbits
  426. .code32
  427. .globl real_call
  428. real_call:
  429. /* Create register dump and function pointer copy on PM stack */
  430. pushal
  431. pushl RC_OFFSET_FUNCTION(%esp)
  432. /* Switch to real mode and move register dump to RM stack */
  433. movl $( RC_OFFSET_RETADDR + 4 /* function pointer copy */ ), %ecx
  434. pushl $1f
  435. jmp prot_to_real
  436. .section ".text16", "ax", @progbits
  437. .code16
  438. 1:
  439. /* Call real-mode function */
  440. popl rc_function
  441. popal
  442. call *rc_function
  443. pushal
  444. /* For sanity's sake, clear the direction flag as soon as possible */
  445. cld
  446. /* Switch to protected mode and move register dump back to PM stack */
  447. movl $RC_OFFSET_RETADDR, %ecx
  448. pushl $1f
  449. jmp real_to_prot
  450. .section ".text", "ax", @progbits
  451. .code32
  452. 1:
  453. /* Restore registers and return */
  454. popal
  455. ret
  456. /* Function vector, used because "call xx(%sp)" is not a valid
  457. * 16-bit expression.
  458. */
  459. .section ".data16", "aw", @progbits
  460. rc_function: .word 0, 0
  461. /****************************************************************************
  462. * Stored real-mode and protected-mode stack pointers
  463. *
  464. * The real-mode stack pointer is stored here whenever real_to_prot
  465. * is called and restored whenever prot_to_real is called. The
  466. * converse happens for the protected-mode stack pointer.
  467. *
  468. * Despite initial appearances this scheme is, in fact re-entrant,
  469. * because program flow dictates that we always return via the point
  470. * we left by. For example:
  471. * PXE API call entry
  472. * 1 real => prot
  473. * ...
  474. * Print a text string
  475. * ...
  476. * 2 prot => real
  477. * INT 10
  478. * 3 real => prot
  479. * ...
  480. * ...
  481. * 4 prot => real
  482. * PXE API call exit
  483. *
  484. * At point 1, the RM mode stack value, say RPXE, is stored in
  485. * rm_ss,sp. We want this value to still be present in rm_ss,sp when
  486. * we reach point 4.
  487. *
  488. * At point 2, the RM stack value is restored from RPXE. At point 3,
  489. * the RM stack value is again stored in rm_ss,sp. This *does*
  490. * overwrite the RPXE that we have stored there, but it's the same
  491. * value, since the code between points 2 and 3 has managed to return
  492. * to us.
  493. ****************************************************************************
  494. */
  495. .section ".data", "aw", @progbits
  496. .globl rm_sp
  497. rm_sp: .word 0
  498. .globl rm_ss
  499. rm_ss: .word 0
  500. pm_esp: .long _estack
  501. /****************************************************************************
  502. * Virtual address offsets
  503. *
  504. * These are used by the protected-mode code to map between virtual
  505. * and physical addresses, and to access variables in the .text16 or
  506. * .data16 segments.
  507. ****************************************************************************
  508. */
  509. /* Internal copies, created by init_librm (which runs in real mode) */
  510. .section ".data16", "aw", @progbits
  511. _virt_offset: .long 0
  512. _text16: .long 0
  513. _data16: .long 0
  514. /* Externally-visible copies, created by real_to_prot */
  515. .section ".data", "aw", @progbits
  516. .globl virt_offset
  517. virt_offset: .long 0
  518. .globl text16
  519. text16: .long 0
  520. .globl data16
  521. data16: .long 0