2 * librm: a library for interfacing to real-mode code
4 * Michael Brown <mbrown@fensystems.co.uk>
8 /* Drag in local definitions */
11 /* For switches to/from protected mode */
14 /* Size of various C data structures */
15 #define SIZEOF_I386_SEG_REGS 12
16 #define SIZEOF_I386_REGS 32
17 #define SIZEOF_REAL_MODE_REGS ( SIZEOF_I386_SEG_REGS + SIZEOF_I386_REGS )
18 #define SIZEOF_I386_FLAGS 4
19 #define SIZEOF_I386_ALL_REGS ( SIZEOF_REAL_MODE_REGS + SIZEOF_I386_FLAGS )
22 .section ".text16", "ax", @progbits
23 .section ".text16.data", "aw", @progbits
24 .section ".data16", "aw", @progbits
26 /****************************************************************************
27 * Global descriptor table
29 * Call init_librm to set up the GDT before attempting to use any
30 * protected-mode code.
32 * Define FLATTEN_REAL_MODE if you want to use so-called "flat real
33 * mode" with 4GB limits instead.
35 * NOTE: This must be located before prot_to_real, otherwise gas
36 * throws a "can't handle non absolute segment in `ljmp'" error due to
37 * not knowing the value of REAL_CS when the ljmp is encountered.
39 * Note also that putting ".word gdt_end - gdt - 1" directly into
40 * gdt_limit, rather than going via gdt_length, will also produce the
41 * "non absolute segment" error. This is most probably a bug in gas.
42 ****************************************************************************
45 #ifdef FLATTEN_REAL_MODE
46 #define RM_LIMIT_16_19__AVL__SIZE__GRANULARITY 0x8f
48 #define RM_LIMIT_16_19__AVL__SIZE__GRANULARITY 0x00
53 gdtr: /* The first GDT entry is unused, the GDTR can fit here. */
54 gdt_limit: .word gdt_length - 1
58 .org gdt + VIRTUAL_CS, 0
59 virtual_cs: /* 32 bit protected mode code segment, virtual addresses */
61 .byte 0, 0x9f, 0xcf, 0
63 .org gdt + VIRTUAL_DS, 0
64 virtual_ds: /* 32 bit protected mode data segment, virtual addresses */
66 .byte 0, 0x93, 0xcf, 0
68 .org gdt + PHYSICAL_CS, 0
69 physical_cs: /* 32 bit protected mode code segment, physical addresses */
71 .byte 0, 0x9f, 0xcf, 0
73 .org gdt + PHYSICAL_DS, 0
74 physical_ds: /* 32 bit protected mode data segment, physical addresses */
76 .byte 0, 0x93, 0xcf, 0
79 real_cs: /* 16 bit real mode code segment */
81 .byte 0, 0x9b, RM_LIMIT_16_19__AVL__SIZE__GRANULARITY, 0
84 real_ds: /* 16 bit real mode data segment */
86 .byte 0, 0x93, RM_LIMIT_16_19__AVL__SIZE__GRANULARITY, 0
89 .equ gdt_length, gdt_end - gdt
91 /****************************************************************************
92 * init_librm (real-mode far call, 16-bit real-mode far return address)
94 * Initialise the GDT ready for transitions to protected mode.
97 * %cs : .text16 segment
98 * %ds : .data16 segment
99 * %edi : Physical base of protected-mode code (virt_offset)
100 ****************************************************************************
106 /* Preserve registers */
110 /* Store _virt_offset and set up virtual_cs and virtual_ds segments */
112 movw $virtual_cs, %bx
114 movw $virtual_ds, %bx
116 movl %edi, _virt_offset
118 /* Negate virt_offset */
121 /* Store rm_cs and _text16, set up real_cs segment */
128 addr32 leal (%eax, %edi), %ebx
131 /* Store rm_ds and _data16, set up real_ds segment */
138 addr32 leal (%eax, %edi), %ebx
141 /* Set GDT and IDT base */
146 /* Restore registers */
161 idt_init: /* Reuse the return opcode here */
164 /****************************************************************************
165 * real_to_prot (real-mode near call, 32-bit virtual return address)
167 * Switch from 16-bit real-mode to 32-bit protected mode with virtual
168 * addresses. The real-mode %ss:sp is stored in rm_ss and rm_sp, and
169 * the protected-mode %esp is restored from the saved pm_esp.
170 * Interrupts are disabled. All other registers may be destroyed.
172 * The return address for this function should be a 32-bit virtual
176 * %ecx : number of bytes to move from RM stack to PM stack
178 ****************************************************************************
183 /* Make sure we have our data segment available */
187 /* Add _virt_offset, _text16 and _data16 to stack to be
188 * copied, and also copy the return address.
193 addw $16, %cx /* %ecx must be less than 64kB anyway */
195 /* Real-mode %ss:%sp => %ebp:%edx and virtual address => %esi */
201 addr32 leal (%eax,%edx), %esi
202 subl _virt_offset, %esi
204 /* Switch to protected mode */
211 data32 ljmp $VIRTUAL_CS, $1f
215 /* Set up protected-mode data segments and stack pointer */
216 movw $VIRTUAL_DS, %ax
224 /* Record real-mode %ss:sp (after removal of data) */
229 /* Move data from RM stack to PM stack */
234 /* Publish virt_offset, text16 and data16 for PM code to use */
239 /* Return to virtual address */
242 /* Default IDTR with no interrupts */
247 .word 0xffff /* limit */
250 /****************************************************************************
251 * prot_to_real (protected-mode near call, 32-bit real-mode return address)
253 * Switch from 32-bit protected mode with virtual addresses to 16-bit
254 * real mode. The protected-mode %esp is stored in pm_esp and the
255 * real-mode %ss:sp is restored from the saved rm_ss and rm_sp. The
256 * high word of the real-mode %esp is set to zero. All real-mode data
257 * segment registers are loaded from the saved rm_ds. Interrupts are
258 * *not* enabled, since we want to be able to use prot_to_real in an
259 * ISR. All other registers may be destroyed.
261 * The return address for this function should be a 32-bit (sic)
262 * real-mode offset within .code16.
265 * %ecx : number of bytes to move from PM stack to RM stack
267 ****************************************************************************
272 /* Add return address to data to be moved to RM stack */
275 /* Real-mode %ss:sp => %ebp:edx and virtual address => %edi */
281 leal (%eax,%edx), %edi
282 subl virt_offset, %edi
284 /* Move data from PM stack to RM stack */
288 /* Record protected-mode %esp (after removal of data) */
291 /* Load real-mode segment limits */
302 /* Switch to real mode */
306 ljmp *p2r_jump_vector
309 /* Set up real-mode data segments and stack pointer */
318 /* Reset IDTR to the real-mode defaults */
321 /* Return to real-mode address */
325 /* Real-mode code and data segments. Assigned by the call to
326 * init_librm. rm_cs doubles as the segment part of the jump
327 * vector used by prot_to_real. rm_ds is located in .text16
328 * rather than .data16 because code needs to be able to locate
333 .word p2r_jump_target
337 .section ".text16.data"
340 /****************************************************************************
341 * prot_call (real-mode far call, 16-bit real-mode far return address)
343 * Call a specific C function in the protected-mode code. The
344 * prototype of the C function must be
345 * void function ( struct i386_all_regs *ix86 );
346 * ix86 will point to a struct containing the real-mode registers
347 * at entry to prot_call.
349 * All registers will be preserved across prot_call(), unless the C
350 * function explicitly overwrites values in ix86. Interrupt status
351 * and GDT will also be preserved. Gate A20 will be enabled.
353 * Note that prot_call() does not rely on the real-mode stack
354 * remaining intact in order to return, since everything relevant is
355 * copied to the protected-mode stack for the duration of the call.
356 * In particular, this means that a real-mode prefix can make a call
357 * to main() which will return correctly even if the prefix's stack
358 * gets vapourised during the Etherboot run. (The prefix cannot rely
359 * on anything else on the stack being preserved, so should move any
360 * critical data to registers before calling main()).
363 * function : virtual address of protected-mode function to call
366 * pushl $pxe_api_call
369 * to call in to the C function
370 * void pxe_api_call ( struct i386_all_regs *ix86 );
371 ****************************************************************************
374 #define PC_OFFSET_GDT ( 0 )
375 #define PC_OFFSET_IDT ( PC_OFFSET_GDT + 8 /* pad to 8 to keep alignment */ )
376 #define PC_OFFSET_IX86 ( PC_OFFSET_IDT + 8 /* pad to 8 to keep alignment */ )
377 #define PC_OFFSET_RETADDR ( PC_OFFSET_IX86 + SIZEOF_I386_ALL_REGS )
378 #define PC_OFFSET_FUNCTION ( PC_OFFSET_RETADDR + 4 )
379 #define PC_OFFSET_END ( PC_OFFSET_FUNCTION + 4 )
385 /* Preserve registers, flags and GDT on external RM stack */
399 /* For sanity's sake, clear the direction flag as soon as possible */
402 /* Switch to protected mode and move register dump to PM stack */
403 movl $PC_OFFSET_END, %ecx
409 /* Set up environment expected by C code */
413 leal PC_OFFSET_IX86(%esp), %eax
415 call *(PC_OFFSET_FUNCTION+4)(%esp)
416 popl %eax /* discard */
418 /* Switch to real mode and move register dump back to RM stack */
419 movl $PC_OFFSET_END, %ecx
425 /* Reload GDT and IDT, restore registers and flags and return */
429 addw $20, %sp /* also skip %cs and %ss */
435 /* popal skips %esp. We therefore want to do "movl -20(%sp),
436 * %esp", but -20(%sp) is not a valid 80386 expression.
437 * Fortunately, prot_to_real() zeroes the high word of %esp, so
438 * we can just use -20(%esp) instead.
440 addr32 movl -20(%esp), %esp
444 /****************************************************************************
445 * real_call (protected-mode near call, 32-bit virtual return address)
447 * Call a real-mode function from protected-mode code.
449 * The non-segment register values will be passed directly to the
450 * real-mode code. The segment registers will be set as per
451 * prot_to_real. The non-segment register values set by the real-mode
452 * function will be passed back to the protected-mode caller. A
453 * result of this is that this routine cannot be called directly from
454 * C code, since it clobbers registers that the C ABI expects the
455 * callee to preserve. Gate A20 will *not* be automatically
456 * re-enabled. Since we always run from an even megabyte of memory,
457 * we are guaranteed to return successfully to the protected-mode
458 * code, which should then call gateA20_set() if it suspects that gate
459 * A20 may have been disabled. Note that enabling gate A20 is a
460 * potentially slow operation that may also cause keyboard input to be
461 * lost; this is why it is not done automatically.
463 * librm.h defines a convenient macro REAL_CODE() for using real_call.
464 * See librm.h and realmode.h for details and examples.
467 * (32-bit) near pointer to real-mode function to call
470 ****************************************************************************
473 #define RC_OFFSET_PRESERVE_REGS ( 0 )
474 #define RC_OFFSET_RETADDR ( RC_OFFSET_PRESERVE_REGS + SIZEOF_I386_REGS )
475 #define RC_OFFSET_FUNCTION ( RC_OFFSET_RETADDR + 4 )
476 #define RC_OFFSET_END ( RC_OFFSET_FUNCTION + 4 )
482 /* Create register dump and function pointer copy on PM stack */
484 pushl RC_OFFSET_FUNCTION(%esp)
486 /* Switch to real mode and move register dump to RM stack */
487 movl $( RC_OFFSET_RETADDR + 4 /* function pointer copy */ ), %ecx
493 /* Call real-mode function */
499 /* For sanity's sake, clear the direction flag as soon as possible */
502 /* Switch to protected mode and move register dump back to PM stack */
503 movl $RC_OFFSET_RETADDR, %ecx
509 /* Restore registers and return */
514 /* Function vector, used because "call xx(%sp)" is not a valid
518 rc_function: .word 0, 0
520 /****************************************************************************
521 * Stored real-mode and protected-mode stack pointers
523 * The real-mode stack pointer is stored here whenever real_to_prot
524 * is called and restored whenever prot_to_real is called. The
525 * converse happens for the protected-mode stack pointer.
527 * Despite initial appearances this scheme is, in fact re-entrant,
528 * because program flow dictates that we always return via the point
529 * we left by. For example:
533 * Print a text string
543 * At point 1, the RM mode stack value, say RPXE, is stored in
544 * rm_ss,sp. We want this value to still be present in rm_ss,sp when
547 * At point 2, the RM stack value is restored from RPXE. At point 3,
548 * the RM stack value is again stored in rm_ss,sp. This *does*
549 * overwrite the RPXE that we have stored there, but it's the same
550 * value, since the code between points 2 and 3 has managed to return
552 ****************************************************************************
559 pm_esp: .long _estack
561 /****************************************************************************
562 * Virtual address offsets
564 * These are used by the protected-mode code to map between virtual
565 * and physical addresses, and to access variables in the .text16 or
567 ****************************************************************************
569 /* Internal copies, created by init_librm (which runs in real mode) */
571 _virt_offset: .long 0
575 /* Externally-visible copies, created by real_to_prot */