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 gdt_limit: .word gdt_length - 1
57 .org gdt + VIRTUAL_CS, 0
58 virtual_cs: /* 32 bit protected mode code segment, virtual addresses */
60 .byte 0, 0x9f, 0xcf, 0
62 .org gdt + VIRTUAL_DS, 0
63 virtual_ds: /* 32 bit protected mode data segment, virtual addresses */
65 .byte 0, 0x93, 0xcf, 0
67 .org gdt + PHYSICAL_CS, 0
68 physical_cs: /* 32 bit protected mode code segment, physical addresses */
70 .byte 0, 0x9f, 0xcf, 0
72 .org gdt + PHYSICAL_DS, 0
73 physical_ds: /* 32 bit protected mode data segment, physical addresses */
75 .byte 0, 0x93, 0xcf, 0
78 real_cs: /* 16 bit real mode code segment */
80 .byte 0, 0x9b, RM_LIMIT_16_19__AVL__SIZE__GRANULARITY, 0
83 real_ds: /* 16 bit real mode data segment */
85 .byte 0, 0x93, RM_LIMIT_16_19__AVL__SIZE__GRANULARITY, 0
88 .equ gdt_length, gdt_end - gdt
90 /****************************************************************************
91 * init_librm (real-mode near call, 16-bit real-mode return address)
93 * Initialise the GDT ready for transitions to protected mode.
96 * %cs : .text16 segment
97 * %ds : .data16 segment
98 * %edi : Physical base of protected-mode code (virt_offset)
99 ****************************************************************************
105 /* Preserve registers */
109 /* Store _virt_offset and set up virtual_cs and virtual_ds segments */
111 movw $virtual_cs, %bx
113 movw $virtual_ds, %bx
115 movl %edi, _virt_offset
117 /* Negate virt_offset */
120 /* Store rm_cs and _text16, set up real_cs segment */
127 leal (%eax, %edi), %ebx
130 /* Store rm_ds and _data16, set up real_ds segment and set GDT base */
137 leal (%eax, %edi), %ebx
142 /* Restore registers */
158 /****************************************************************************
159 * real_to_prot (real-mode near call, 32-bit virtual return address)
161 * Switch from 16-bit real-mode to 32-bit protected mode with virtual
162 * addresses. The real-mode %ss:sp is stored in rm_ss and rm_sp, and
163 * the protected-mode %esp is restored from the saved pm_esp.
164 * Interrupts are disabled. All other registers may be destroyed.
166 * The return address for this function should be a 32-bit virtual
170 * %ecx : number of bytes to move from RM stack to PM stack
172 ****************************************************************************
177 /* Make sure we have our data segment available */
181 /* Add _virt_offset, _text16 and _data16 to stack to be
182 * copied, and also copy the return address.
187 addw $16, %cx /* %ecx must be less than 64kB anyway */
189 /* Real-mode %ss:%sp => %bp:%esi */
193 /* Switch to protected mode */
199 data32 ljmp $VIRTUAL_CS, $1f
203 /* Set up protected-mode data segments */
204 movw $VIRTUAL_DS, %ax
210 /* Move data from RM stack to PM stack and set up PM stack */
217 /* Record real-mode %ss:sp (after removal of data) */
221 /* Publish virt_offset, text16 and data16 for PM code to use */
226 /* Return to virtual address */
229 /****************************************************************************
230 * prot_to_real (protected-mode near call, 32-bit real-mode return address)
232 * Switch from 32-bit protected mode with virtual addresses to 16-bit
233 * real mode. The protected-mode %esp is stored in pm_esp and the
234 * real-mode %ss:sp is restored from the saved rm_ss and rm_sp. The
235 * high word of the real-mode %esp is set to zero. All real-mode data
236 * segment registers are loaded from the saved rm_ds. Interrupts are
237 * *not* enabled, since we want to be able to use prot_to_real in an
238 * ISR. All other registers may be destroyed.
240 * The return address for this function should be a 32-bit (sic)
241 * real-mode offset within .code16.
244 * %ecx : number of bytes to move from PM stack to RM stack
246 ****************************************************************************
251 /* Add return address to data to be moved to RM stack */
254 /* Real-mode %ss:sp => %ebp:edx */
259 /* Move data from PM stack to RM stack */
262 leal (%eax,%edx), %edi
263 subl virt_offset, %edi
267 /* Record protected-mode %esp (after removal of data) */
270 /* Load real-mode segment limits */
281 /* Switch to real mode */
285 ljmp *p2r_jump_vector
288 /* Set up real-mode stack */
292 /* Set up real-mode data segments */
299 /* Return to real-mode address */
303 /* Real-mode code and data segments. Assigned by the call to
304 * init_librm. rm_cs doubles as the segment part of the jump
305 * vector used by prot_to_real. rm_ds is located in .text16
306 * rather than .data16 because code needs to be able to locate
311 .word p2r_jump_target
315 .section ".text16.data"
318 /****************************************************************************
319 * prot_call (real-mode near call, 32-bit real-mode return address)
321 * Call a specific C function in the protected-mode code. The
322 * prototype of the C function must be
323 * void function ( struct i386_all_regs *ix86 );
324 * ix86 will point to a struct containing the real-mode registers
325 * at entry to prot_call.
327 * All registers will be preserved across prot_call(), unless the C
328 * function explicitly overwrites values in ix86. Interrupt status
329 * and GDT will also be preserved. Gate A20 will be enabled.
332 * function : virtual address of protected-mode function to call
335 * pushl $pxe_api_call
338 * to call in to the C function
339 * void pxe_api_call ( struct i386_all_regs *ix86 );
340 ****************************************************************************
343 #define PC_OFFSET_GDT ( 0 )
344 #define PC_OFFSET_IX86 ( PC_OFFSET_GDT + 8 /* pad to 8 to keep alignment */ )
345 #define PC_OFFSET_RETADDR ( PC_OFFSET_IX86 + SIZEOF_I386_ALL_REGS )
346 #define PC_OFFSET_FUNCTION ( PC_OFFSET_RETADDR + 4 )
347 #define PC_OFFSET_END ( PC_OFFSET_FUNCTION + 4 )
353 /* Preserve registers, flags and GDT on external RM stack */
366 /* For sanity's sake, clear the direction flag as soon as possible */
369 /* Switch to protected mode and move register dump to PM stack */
370 movl $PC_OFFSET_END, %ecx
376 /* Set up environment expected by C code */
380 leal PC_OFFSET_IX86(%esp), %eax
382 call *(PC_OFFSET_FUNCTION+4)(%esp)
383 popl %eax /* discard */
385 /* Switch to real mode and move register dump back to RM stack */
386 movl $PC_OFFSET_END, %ecx
392 /* Reload GDT, restore registers and flags and return. Note
393 * that %esp is restored manually, since popal discards it.
397 addw $12, %sp /* also skip %cs and %ss */
403 movl -20(%esp), %esp /* -20(%sp) is not a valid 80386 expression.
404 * -20(%esp) is safe because prot_to_real
405 * zeroes the high word of %esp, and interrupts
406 * are still disabled at this point. */
410 /****************************************************************************
411 * real_call (protected-mode near call, 32-bit virtual return address)
413 * Call a real-mode function from protected-mode code.
415 * The non-segment register values will be passed directly to the
416 * real-mode code. The segment registers will be set as per
417 * prot_to_real. The non-segment register values set by the real-mode
418 * function will be passed back to the protected-mode caller. A
419 * result of this is that this routine cannot be called directly from
420 * C code, since it clobbers registers that the C ABI expects the
421 * callee to preserve. Gate A20 will be re-enabled in case the
422 * real-mode routine disabled it.
424 * librm.h defines a convenient macro REAL_CODE() for using real_call.
425 * See librm.h and realmode.h for details and examples.
428 * (32-bit) near pointer to real-mode function to call
431 ****************************************************************************
434 #define RC_OFFSET_PRESERVE_REGS ( 0 )
435 #define RC_OFFSET_RETADDR ( RC_OFFSET_PRESERVE_REGS + SIZEOF_I386_REGS )
436 #define RC_OFFSET_FUNCTION ( RC_OFFSET_RETADDR + 4 )
437 #define RC_OFFSET_END ( RC_OFFSET_FUNCTION + 4 )
443 /* Create register dump on PM stack */
446 /* Switch to real mode and move register dump to RM stack */
447 movl $RC_OFFSET_END, %ecx
453 /* Construct call to real-mode function */
455 movw RC_OFFSET_FUNCTION(%bp), %ax
456 movw %ax, rc_function
458 /* Call real-mode function */
463 /* Switch to protected mode and move register dump back to PM stack */
464 movl $RC_OFFSET_END, %ecx
470 /* Set up environment expected by C code */
473 /* Restore registers and return */
478 /* Function vector, used because */
482 /****************************************************************************
483 * Stored real-mode and protected-mode stack pointers
485 * The real-mode stack pointer is stored here whenever real_to_prot
486 * is called and restored whenever prot_to_real is called. The
487 * converse happens for the protected-mode stack pointer.
489 * Despite initial appearances this scheme is, in fact re-entrant,
490 * because program flow dictates that we always return via the point
491 * we left by. For example:
495 * Print a text string
505 * At point 1, the RM mode stack value, say RPXE, is stored in
506 * rm_ss,sp. We want this value to still be present in rm_ss,sp when
509 * At point 2, the RM stack value is restored from RPXE. At point 3,
510 * the RM stack value is again stored in rm_ss,sp. This *does*
511 * overwrite the RPXE that we have stored there, but it's the same
512 * value, since the code between points 2 and 3 has managed to return
514 ****************************************************************************
522 pm_esp: .long _estack
524 /****************************************************************************
525 * Virtual address offsets
527 * These are used by the protected-mode code to map between virtual
528 * and physical addresses, and to access variables in the .text16 or
530 ****************************************************************************
532 /* Internal copies, created by init_librm (which runs in real mode) */
534 _virt_offset: .long 0
538 /* Externally-visible copies, created by real_to_prot */