2 * librm: a library for interfacing to real-mode code
4 * Michael Brown <mbrown@fensystems.co.uk>
8 FILE_LICENCE ( GPL2_OR_LATER )
10 /* Drag in local definitions */
13 /* For switches to/from protected mode */
16 /* Size of various C data structures */
17 #define SIZEOF_I386_SEG_REGS 12
18 #define SIZEOF_I386_REGS 32
19 #define SIZEOF_REAL_MODE_REGS ( SIZEOF_I386_SEG_REGS + SIZEOF_I386_REGS )
20 #define SIZEOF_I386_FLAGS 4
21 #define SIZEOF_I386_ALL_REGS ( SIZEOF_REAL_MODE_REGS + SIZEOF_I386_FLAGS )
25 /****************************************************************************
26 * Global descriptor table
28 * Call init_librm to set up the GDT before attempting to use any
29 * protected-mode code.
31 * NOTE: This must be located before prot_to_real, otherwise gas
32 * throws a "can't handle non absolute segment in `ljmp'" error due to
33 * not knowing the value of REAL_CS when the ljmp is encountered.
35 * Note also that putting ".word gdt_end - gdt - 1" directly into
36 * gdt_limit, rather than going via gdt_length, will also produce the
37 * "non absolute segment" error. This is most probably a bug in gas.
38 ****************************************************************************
40 .section ".data16", "aw", @progbits
43 gdtr: /* The first GDT entry is unused, the GDTR can fit here. */
44 gdt_limit: .word gdt_length - 1
48 .org gdt + VIRTUAL_CS, 0
49 virtual_cs: /* 32 bit protected mode code segment, virtual addresses */
51 .byte 0, 0x9f, 0xcf, 0
53 .org gdt + VIRTUAL_DS, 0
54 virtual_ds: /* 32 bit protected mode data segment, virtual addresses */
56 .byte 0, 0x93, 0xcf, 0
58 .org gdt + PHYSICAL_CS, 0
59 physical_cs: /* 32 bit protected mode code segment, physical addresses */
61 .byte 0, 0x9f, 0xcf, 0
63 .org gdt + PHYSICAL_DS, 0
64 physical_ds: /* 32 bit protected mode data segment, physical addresses */
66 .byte 0, 0x93, 0xcf, 0
69 real_cs: /* 16 bit real mode code segment */
71 .byte 0, 0x9b, 0x00, 0
74 real_ds: /* 16 bit real mode data segment */
75 .word 0xffff, ( REAL_DS << 4 )
76 .byte 0, 0x93, 0x00, 0
79 .equ gdt_length, gdt_end - gdt
81 /****************************************************************************
82 * init_librm (real-mode far call, 16-bit real-mode far return address)
84 * Initialise the GDT ready for transitions to protected mode.
87 * %cs : .text16 segment
88 * %ds : .data16 segment
89 * %edi : Physical base of protected-mode code (virt_offset)
90 ****************************************************************************
92 .section ".text16", "ax", @progbits
96 /* Preserve registers */
100 /* Store virt_offset and set up virtual_cs and virtual_ds segments */
102 movw $virtual_cs, %bx
104 movw $virtual_ds, %bx
106 movl %edi, rm_virt_offset
108 /* Negate virt_offset */
111 /* Store rm_cs and text16, set up real_cs segment */
118 addr32 leal (%eax, %edi), %ebx
121 /* Store rm_ds and data16 */
126 addr32 leal (%eax, %edi), %ebx
137 popl %eax /* discard */
139 /* Restore registers */
145 .section ".text16", "ax", @progbits
155 /****************************************************************************
156 * real_to_prot (real-mode near call, 32-bit virtual return address)
158 * Switch from 16-bit real-mode to 32-bit protected mode with virtual
159 * addresses. The real-mode %ss:sp is stored in rm_ss and rm_sp, and
160 * the protected-mode %esp is restored from the saved pm_esp.
161 * Interrupts are disabled. All other registers may be destroyed.
163 * The return address for this function should be a 32-bit virtual
167 * %ecx : number of bytes to move from RM stack to PM stack
169 ****************************************************************************
171 .section ".text16", "ax", @progbits
174 /* Enable A20 line */
176 /* A failure at this point is fatal, and there's nothing we
177 * can do about it other than lock the machine to make the
178 * problem immediately visible.
182 /* Make sure we have our data segment available */
186 /* Add virt_offset, text16 and data16 to stack to be
187 * copied, and also copy the return address.
192 addw $16, %cx /* %ecx must be less than 64kB anyway */
194 /* Real-mode %ss:%sp => %ebp:%edx and virtual address => %esi */
200 addr32 leal (%eax,%edx), %esi
201 subl rm_virt_offset, %esi
203 /* Load protected-mode global descriptor table */
206 /* Zero segment registers. This wastes around 12 cycles on
207 * real hardware, but saves a substantial number of emulated
208 * instructions under KVM.
217 /* Switch to protected mode */
222 data32 ljmp $VIRTUAL_CS, $r2p_pmode
223 .section ".text", "ax", @progbits
226 /* Set up protected-mode data segments and stack pointer */
227 movw $VIRTUAL_DS, %ax
235 /* Load protected-mode interrupt descriptor table */
238 /* Record real-mode %ss:sp (after removal of data) */
243 /* Move data from RM stack to PM stack */
248 /* Publish virt_offset, text16 and data16 for PM code to use */
253 /* Return to virtual address */
256 /****************************************************************************
257 * prot_to_real (protected-mode near call, 32-bit real-mode return address)
259 * Switch from 32-bit protected mode with virtual addresses to 16-bit
260 * real mode. The protected-mode %esp is stored in pm_esp and the
261 * real-mode %ss:sp is restored from the saved rm_ss and rm_sp. The
262 * high word of the real-mode %esp is set to zero. All real-mode data
263 * segment registers are loaded from the saved rm_ds. Interrupts are
264 * *not* enabled, since we want to be able to use prot_to_real in an
265 * ISR. All other registers may be destroyed.
267 * The return address for this function should be a 32-bit (sic)
268 * real-mode offset within .code16.
271 * %ecx : number of bytes to move from PM stack to RM stack
272 * %esi : real-mode global and interrupt descriptor table registers
274 ****************************************************************************
276 .section ".text", "ax", @progbits
279 /* Copy real-mode global descriptor table register to RM code segment */
281 leal rm_gdtr(%edi), %edi
285 /* Load real-mode interrupt descriptor table register */
288 /* Add return address to data to be moved to RM stack */
291 /* Real-mode %ss:sp => %ebp:edx and virtual address => %edi */
297 leal (%eax,%edx), %edi
298 subl virt_offset, %edi
300 /* Move data from PM stack to RM stack */
304 /* Record protected-mode %esp (after removal of data) */
307 /* Load real-mode segment limits */
314 ljmp $REAL_CS, $p2r_rmode
315 .section ".text16", "ax", @progbits
318 /* Load real-mode GDT */
319 data32 lgdt %cs:rm_gdtr
320 /* Switch to real mode */
327 /* Set up real-mode data segments and stack pointer */
336 /* Return to real-mode address */
340 /* Real-mode code and data segments. Assigned by the call to
341 * init_librm. rm_cs doubles as the segment part of the jump
342 * instruction used by prot_to_real. Both are located in
343 * .text16 rather than .data16: rm_cs since it forms part of
344 * the jump instruction within the code segment, and rm_ds
345 * since real-mode code needs to be able to locate the data
346 * segment with no other reference available.
349 .equ rm_cs, ( p2r_ljmp_rm_cs + 3 )
351 .section ".text16.data", "aw", @progbits
355 /* Real-mode global and interrupt descriptor table registers */
356 .section ".text16.data", "aw", @progbits
361 /****************************************************************************
362 * prot_call (real-mode far call, 16-bit real-mode far return address)
364 * Call a specific C function in the protected-mode code. The
365 * prototype of the C function must be
366 * void function ( struct i386_all_regs *ix86 );
367 * ix86 will point to a struct containing the real-mode registers
368 * at entry to prot_call.
370 * All registers will be preserved across prot_call(), unless the C
371 * function explicitly overwrites values in ix86. Interrupt status
372 * and GDT will also be preserved. Gate A20 will be enabled.
374 * Note that prot_call() does not rely on the real-mode stack
375 * remaining intact in order to return, since everything relevant is
376 * copied to the protected-mode stack for the duration of the call.
377 * In particular, this means that a real-mode prefix can make a call
378 * to main() which will return correctly even if the prefix's stack
379 * gets vapourised during the Etherboot run. (The prefix cannot rely
380 * on anything else on the stack being preserved, so should move any
381 * critical data to registers before calling main()).
384 * function : virtual address of protected-mode function to call
387 * pushl $pxe_api_call
390 * to call in to the C function
391 * void pxe_api_call ( struct i386_all_regs *ix86 );
392 ****************************************************************************
395 #define PC_OFFSET_GDT ( 0 )
396 #define PC_OFFSET_IDT ( PC_OFFSET_GDT + 6 )
397 #define PC_OFFSET_IX86 ( PC_OFFSET_IDT + 6 )
398 #define PC_OFFSET_RETADDR ( PC_OFFSET_IX86 + SIZEOF_I386_ALL_REGS )
399 #define PC_OFFSET_FUNCTION ( PC_OFFSET_RETADDR + 4 )
400 #define PC_OFFSET_END ( PC_OFFSET_FUNCTION + 4 )
402 .section ".text16", "ax", @progbits
406 /* Preserve registers, flags and GDT on external RM stack */
415 subw $PC_OFFSET_IX86, %sp
417 sidt PC_OFFSET_IDT(%bp)
418 sgdt PC_OFFSET_GDT(%bp)
420 /* For sanity's sake, clear the direction flag as soon as possible */
423 /* Switch to protected mode and move register dump to PM stack */
424 movl $PC_OFFSET_END, %ecx
427 .section ".text", "ax", @progbits
431 leal PC_OFFSET_IX86(%esp), %eax
433 call *(PC_OFFSET_FUNCTION+4)(%esp)
434 popl %eax /* discard */
436 /* Switch to real mode and move register dump back to RM stack */
437 movl $PC_OFFSET_END, %ecx
441 .section ".text16", "ax", @progbits
444 /* Restore registers and flags and return */
445 addw $( PC_OFFSET_IX86 + 4 /* also skip %cs and %ss */ ), %sp
451 /* popal skips %esp. We therefore want to do "movl -20(%sp),
452 * %esp", but -20(%sp) is not a valid 80386 expression.
453 * Fortunately, prot_to_real() zeroes the high word of %esp, so
454 * we can just use -20(%esp) instead.
456 addr32 movl -20(%esp), %esp
460 /****************************************************************************
461 * real_call (protected-mode near call, 32-bit virtual return address)
463 * Call a real-mode function from protected-mode code.
465 * The non-segment register values will be passed directly to the
466 * real-mode code. The segment registers will be set as per
467 * prot_to_real. The non-segment register values set by the real-mode
468 * function will be passed back to the protected-mode caller. A
469 * result of this is that this routine cannot be called directly from
470 * C code, since it clobbers registers that the C ABI expects the
471 * callee to preserve.
473 * librm.h defines a convenient macro REAL_CODE() for using real_call.
474 * See librm.h and realmode.h for details and examples.
477 * (32-bit) near pointer to real-mode function to call
480 ****************************************************************************
483 #define RC_OFFSET_PRESERVE_REGS ( 0 )
484 #define RC_OFFSET_RETADDR ( RC_OFFSET_PRESERVE_REGS + SIZEOF_I386_REGS )
485 #define RC_OFFSET_FUNCTION ( RC_OFFSET_RETADDR + 4 )
486 #define RC_OFFSET_END ( RC_OFFSET_FUNCTION + 4 )
488 .section ".text", "ax", @progbits
492 /* Create register dump and function pointer copy on PM stack */
494 pushl RC_OFFSET_FUNCTION(%esp)
496 /* Switch to real mode and move register dump to RM stack */
497 movl $( RC_OFFSET_RETADDR + 4 /* function pointer copy */ ), %ecx
499 movl $rm_default_gdtr_idtr, %esi
501 .section ".text16", "ax", @progbits
504 /* Call real-mode function */
510 /* For sanity's sake, clear the direction flag as soon as possible */
513 /* Switch to protected mode and move register dump back to PM stack */
514 movl $RC_OFFSET_RETADDR, %ecx
517 .section ".text", "ax", @progbits
520 /* Restore registers and return */
525 /* Function vector, used because "call xx(%sp)" is not a valid
528 .section ".data16", "aw", @progbits
529 rc_function: .word 0, 0
531 /* Default real-mode global and interrupt descriptor table registers */
532 .section ".data", "aw", @progbits
533 rm_default_gdtr_idtr:
534 .word 0 /* Global descriptor table limit */
535 .long 0 /* Global descriptor table base */
536 .word 0x03ff /* Interrupt descriptor table limit */
537 .long 0 /* Interrupt descriptor table base */
539 /****************************************************************************
540 * flatten_real_mode (real-mode near call)
542 * Switch to flat real mode
544 ****************************************************************************
546 .section ".text16", "ax", @progbits
548 .globl flatten_real_mode
550 /* Modify GDT to use flat real mode */
551 movb $0x8f, real_cs + 6
552 movb $0x8f, real_ds + 6
553 /* Call dummy protected-mode function */
559 movb $0x00, real_cs + 6
560 movb $0x00, real_ds + 6
564 .section ".text", "ax", @progbits
569 /****************************************************************************
572 * Used by the protected-mode interrupt vectors to call the
573 * interrupt() function.
575 * May be entered with either physical or virtual stack segment.
576 ****************************************************************************
578 .globl interrupt_wrapper
580 /* Preserve segment registers and original %esp */
588 /* Switch to virtual addressing */
591 /* Expand IRQ number to whole %eax register */
594 /* Call interrupt handler */
597 /* Restore original stack and segment registers */
605 /* Restore registers and return */
609 /****************************************************************************
610 * Stored real-mode and protected-mode stack pointers
612 * The real-mode stack pointer is stored here whenever real_to_prot
613 * is called and restored whenever prot_to_real is called. The
614 * converse happens for the protected-mode stack pointer.
616 * Despite initial appearances this scheme is, in fact re-entrant,
617 * because program flow dictates that we always return via the point
618 * we left by. For example:
622 * Print a text string
632 * At point 1, the RM mode stack value, say RPXE, is stored in
633 * rm_ss,sp. We want this value to still be present in rm_ss,sp when
636 * At point 2, the RM stack value is restored from RPXE. At point 3,
637 * the RM stack value is again stored in rm_ss,sp. This *does*
638 * overwrite the RPXE that we have stored there, but it's the same
639 * value, since the code between points 2 and 3 has managed to return
641 ****************************************************************************
643 .section ".data", "aw", @progbits
648 pm_esp: .long _estack
650 /****************************************************************************
651 * Virtual address offsets
653 * These are used by the protected-mode code to map between virtual
654 * and physical addresses, and to access variables in the .text16 or
656 ****************************************************************************
658 /* Internal copies, created by init_librm (which runs in real mode) */
659 .section ".data16", "aw", @progbits
660 rm_virt_offset: .long 0
664 /* Externally-visible copies, created by real_to_prot */
665 .section ".data", "aw", @progbits