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", "awx", @progbits
24 /****************************************************************************
25 * Global descriptor table
27 * Call init_librm to set up the GDT before attempting to use any
28 * protected-mode code.
30 * Define FLATTEN_REAL_MODE if you want to use so-called "flat real
31 * mode" with 4GB limits instead.
33 * NOTE: This must be located before prot_to_real, otherwise gas
34 * throws a "can't handle non absolute segment in `ljmp'" error due to
35 * not knowing the value of REAL_CS when the ljmp is encountered.
37 * Note also that putting ".word gdt_end - gdt - 1" directly into
38 * gdt_limit, rather than going via gdt_length, will also produce the
39 * "non absolute segment" error. This is most probably a bug in gas.
40 ****************************************************************************
43 #ifdef FLATTEN_REAL_MODE
44 #define RM_LIMIT_16_19__AVL__SIZE__GRANULARITY 0x8f
46 #define RM_LIMIT_16_19__AVL__SIZE__GRANULARITY 0x00
48 .section ".data16", "aw", @progbits
51 gdt_limit: .word gdt_length - 1
55 .org gdt + VIRTUAL_CS, 0
56 virtual_cs: /* 32 bit protected mode code segment, virtual addresses */
58 .byte 0, 0x9f, 0xcf, 0
60 .org gdt + VIRTUAL_DS, 0
61 virtual_ds: /* 32 bit protected mode data segment, virtual addresses */
63 .byte 0, 0x93, 0xcf, 0
65 .org gdt + PHYSICAL_CS, 0
66 physical_cs: /* 32 bit protected mode code segment, physical addresses */
68 .byte 0, 0x9f, 0xcf, 0
70 .org gdt + PHYSICAL_DS, 0
71 physical_ds: /* 32 bit protected mode data segment, physical addresses */
73 .byte 0, 0x93, 0xcf, 0
76 real_cs: /* 16 bit real mode code segment */
78 .byte 0, 0x9b, RM_LIMIT_16_19__AVL__SIZE__GRANULARITY, 0
81 real_ds: /* 16 bit real mode data segment */
83 .byte 0, 0x93, RM_LIMIT_16_19__AVL__SIZE__GRANULARITY, 0
86 .equ gdt_length, gdt_end - gdt
88 /****************************************************************************
89 * init_librm (real-mode near call, 16-bit real-mode return address)
91 * Initialise the GDT ready for transitions to protected mode.
94 * %cs : .text16 segment
95 * %ds : .data16 segment
96 * %edi : Physical base of protected-mode code (virt_offset)
97 ****************************************************************************
103 /* Preserve registers */
107 /* Store _virt_offset and set up virtual_cs and virtual_ds segments */
109 movw $virtual_cs, %bx
111 movw $virtual_ds, %bx
113 movl %edi, _virt_offset
115 /* Negate virt_offset */
118 /* Store rm_cs and _text16, set up real_cs segment */
125 leal (%eax, %edi), %ebx
128 /* Store rm_ds and _data16, set up real_ds segment and set GDT base */
135 leal (%eax, %edi), %ebx
140 /* Restore registers */
156 /****************************************************************************
157 * real_to_prot (real-mode near call, 32-bit virtual return address)
159 * Switch from 16-bit real-mode to 32-bit protected mode with virtual
160 * addresses. The real-mode %ss:sp is stored in rm_ss and rm_sp, and
161 * the protected-mode %esp is restored from the saved pm_esp.
162 * Interrupts are disabled. All other registers may be destroyed.
164 * The return address for this function should be a 32-bit virtual
168 * %ecx : number of bytes to move from RM stack to PM stack
170 ****************************************************************************
175 /* Make sure we have our data segment available */
179 /* Add _virt_offset, _text16 and _data16 to stack to be
180 * copied, and also copy the return address.
185 addw $16, %cx /* %ecx must be less than 64kB anyway */
187 /* Real-mode %ss:%sp => %bp:%esi */
191 /* Switch to protected mode */
197 data32 ljmp $VIRTUAL_CS, $1f
201 /* Set up protected-mode data segments */
202 movw $VIRTUAL_DS, %ax
208 /* Move data from RM stack to PM stack and set up PM stack */
215 /* Record real-mode %ss:sp (after removal of data) */
219 /* Publish virt_offset, text16 and data16 for PM code to use */
224 /* Return to virtual address */
227 /****************************************************************************
228 * prot_to_real (protected-mode near call, 32-bit real-mode return address)
230 * Switch from 32-bit protected mode with virtual addresses to 16-bit
231 * real mode. The protected-mode %esp is stored in pm_esp and the
232 * real-mode %ss:sp is restored from the saved rm_ss and rm_sp. All
233 * real-mode data segment registers are loaded from the saved rm_ds.
234 * Interrupts are *not* enabled, since we want to be able to use
235 * prot_to_real in an ISR. All other registers may be destroyed.
237 * The return address for this function should be a 32-bit (sic)
238 * real-mode offset within .code16.
241 * %ecx : number of bytes to move from PM stack to RM stack
243 ****************************************************************************
248 /* Add return address to data to be moved to RM stack */
251 /* Real-mode %ss:sp => %ebp:edx */
256 /* Move data from PM stack to RM stack */
259 leal (%eax,%edx), %edi
260 subl virt_offset, %edi
264 /* Record protected-mode %esp (after removal of data) */
267 /* Load real-mode segment limits */
278 /* Switch to real mode */
282 ljmp *p2r_jump_vector
285 /* Set up real-mode stack */
289 /* Set up real-mode data segments */
296 /* Return to real-mode address */
300 /* Real-mode code and data segments. Assigned by the call to
301 * init_librm. rm_cs doubles as the segment part of the jump
302 * vector used by prot_to_real. rm_ds is located in .text16
303 * rather than .data16 because code needs to be able to locate
308 .word p2r_jump_target
313 /****************************************************************************
314 * prot_call (real-mode near call, 32-bit real-mode return address)
316 * Call a specific C function in the protected-mode code. The
317 * prototype of the C function must be
318 * void function ( struct i386_all_regs *ix86 );
319 * ix86 will point to a struct containing the real-mode registers
320 * at entry to prot_call.
322 * All registers will be preserved across prot_call(), unless the C
323 * function explicitly overwrites values in ix86. Interrupt status
324 * will also be preserved. Gate A20 will be enabled.
327 * function : virtual address of protected-mode function to call
330 * pushl $pxe_api_call
333 * to call in to the C function
334 * void pxe_api_call ( struct i386_all_regs *ix86 );
335 ****************************************************************************
338 #define PC_OFFSET_IX86 ( 0 )
339 #define PC_OFFSET_RETADDR ( PC_OFFSET_IX86 + SIZEOF_I386_ALL_REGS )
340 #define PC_OFFSET_FUNCTION ( PC_OFFSET_RETADDR + 4 )
341 #define PC_OFFSET_END ( PC_OFFSET_FUNCTION + 4 )
347 /* Preserve registers and flags on external RM stack */
357 /* For sanity's sake, clear the direction flag as soon as possible */
360 /* Switch to protected mode and move register dump to PM stack */
361 movl $PC_OFFSET_END, %ecx
367 /* Set up environment expected by C code */
372 call *(PC_OFFSET_FUNCTION+4)(%esp)
373 popl %eax /* discard */
375 /* Switch to real mode and move register dump back to RM stack */
376 movl $PC_OFFSET_END, %ecx
382 /* Restore registers and flags and return */
383 popw %ax /* skip %cs - it is already set */
384 popw %ax /* skip %ss - it is already set */
393 /****************************************************************************
394 * real_call (protected-mode near call, 32-bit virtual return address)
396 * Call a real-mode function from protected-mode code.
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 be re-enabled in case the
405 * real-mode routine disabled it.
407 * librm.h defines two convenient macros for using real_call:
408 * REAL_CALL and REAL_EXEC. See librm.h and realmode.h for details
412 * (32-bit) near pointer to real-mode function to call
415 ****************************************************************************
418 #define RC_OFFSET_PRESERVE_REGS ( 0 )
419 #define RC_OFFSET_RETADDR ( RC_OFFSET_PRESERVE_REGS + SIZEOF_I386_REGS )
420 #define RC_OFFSET_FUNCTION ( RC_OFFSET_RETADDR + 4 )
421 #define RC_OFFSET_END ( RC_OFFSET_FUNCTION + 4 )
427 /* Create register dump on PM stack */
430 /* Switch to real mode and move register dump to RM stack */
431 movl $RC_OFFSET_END, %ecx
437 /* Construct call to real-mode function */
439 movw RC_OFFSET_FUNCTION(%bp), %ax
440 movw %ax, rc_function
442 /* Call real-mode function */
444 call *RC_OFFSET_FUNCTION(%esp)
448 /* Switch to protected mode and move register dump back to PM stack */
449 movl $RC_OFFSET_END, %ecx
455 /* Set up environment expected by C code */
458 /* Restore registers and return */
463 /* Function vector, used because */
467 /****************************************************************************
468 * Stored real-mode and protected-mode stack pointers
470 * The real-mode stack pointer is stored here whenever real_to_prot
471 * is called and restored whenever prot_to_real is called. The
472 * converse happens for the protected-mode stack pointer.
474 * Despite initial appearances this scheme is, in fact re-entrant,
475 * because program flow dictates that we always return via the point
476 * we left by. For example:
480 * Print a text string
490 * At point 1, the RM mode stack value, say RPXE, is stored in
491 * rm_ss,sp. We want this value to still be present in rm_ss,sp when
494 * At point 2, the RM stack value is restored from RPXE. At point 3,
495 * the RM stack value is again stored in rm_ss,sp. This *does*
496 * overwrite the RPXE that we have stored there, but it's the same
497 * value, since the code between points 2 and 3 has managed to return
499 ****************************************************************************
507 pm_esp: .long _estack
509 /****************************************************************************
510 * Virtual address offsets
512 * These are used by the protected-mode code to map between virtual
513 * and physical addresses, and to access variables in the .text16 or
515 ****************************************************************************
517 /* Internal copies, created by init_librm (which runs in real mode) */
519 _virt_offset: .long 0
523 /* Externally-visible copies, created by real_to_prot */