2 * librm: a library for interfacing to real-mode code
4 * Michael Brown <mbrown@fensystems.co.uk>
8 /* Drag in local definitions */
11 /* Drag in FREE_BASEMEM_HEADER_SIZE */
14 /****************************************************************************
15 * This file defines librm: a block of code that is designed to reside
16 * permanently in base memory and provide the interface between
17 * real-mode code running in base memory and protected-mode code
18 * running in high memory. It provides the following functions:
20 * real_to_prot & switch between real and protected mode
21 * prot_to_real while running in base memory, preserving
22 * all non-segment registers
24 * real_call issue a call to a real-mode routine from
25 * protected-mode code running in high memory
27 * prot_call issue a call to a protected-mode routine from
28 * real-mode code running in base memory
30 * librm requires the following functions to be present in the
31 * protected-mode code:
33 * _phys_to_virt Switch from physical to virtual addressing. This
34 * routine must be position-independent and must
35 * *not* assume that it is genuinely running with
36 * flat physical addresses
38 * _virt_to_phys Switch from virtual to physical addresses.
40 * gateA20_set Enable the A20 line to permit access to the odd
41 * megabytes of RAM. (This function will be called
42 * with virtual addresses set up).
44 * librm needs to be linked against the protected-mode binary so that
45 * it can import the symbols for these functions.
47 * librm requires that the protected-mode code set up the following
50 * PHYSICAL_CS 32-bit pmode code and data segments with flat
51 * PHYSICAL_DS physical addresses.
53 * VIRTUAL_CS 32-bit pmode code segment with virtual
54 * addressing, such that a protected-mode routine
55 * can always be found at $VIRTUAL_CS:routine.
57 * These segments must be set as #define constants when compiling
58 * librm. Edit librm.h to change the values.
60 * librm does not know the location of the code executing in high
61 * memory. It relies on the code running in high memory setting up a
62 * GDT such that the high-memory code is accessible at virtual
63 * addresses fixed at compile-time.
65 * librm symbols are exported as absolute values and represent offsets
66 * into librm. This is the most useful form of the symbols, since
67 * librm is basically a binary blob that you place somewhere in base
70 * librm.h provides convenient ways to use these symbols: you simply
71 * set the pointer ( char * ) installed_librm to point to wherever
72 * librm is installed, and can then use e.g. inst_rm_stack just like
73 * any other variable and have it automatically refer to the value of
74 * rm_stack in the installed librm. Macro trickery makes this
75 * completely transparent, and the resulting assembler code is
76 * amazingly efficient.
78 * Note that librm must be called in genuine real mode, not 16:16 or
79 * 16:32 protected mode. It makes the assumption that
80 * physical_address = 16*segment+offset, and also that it can use
81 * OFFSET(%bp) to access stack variables. The former assumption will
82 * break in either protected mode, the latter may break in 16:32
84 ****************************************************************************
88 * Default values for pmode segments if not defined
91 #warning "Assuming PHYSICAL_CS = 0x08"
92 #define PHYSICAL_CS 0x08
95 #warning "Assuming PHYSICAL_DS = 0x10"
96 #define PHYSICAL_DS 0x10
99 #warning "Assuming VIRTUAL_CS = 0x18"
100 #define VIRTUAL_CS 0x18
103 /* For switches to/from protected mode */
106 /* Size of various C data structures */
107 #define SIZEOF_I386_SEG_REGS 12
108 #define SIZEOF_I386_REGS 32
109 #define SIZEOF_REAL_MODE_REGS ( SIZEOF_I386_SEG_REGS + SIZEOF_I386_REGS )
110 #define SIZEOF_I386_FLAGS 4
111 #define SIZEOF_I386_ALL_REGS ( SIZEOF_REAL_MODE_REGS + SIZEOF_I386_FLAGS )
112 #define SIZEOF_SEGOFF_T 4
113 #define SIZEOF_REAL_CALL_PARAMS ( SIZEOF_REAL_MODE_REGS + 2 * SIZEOF_SEGOFF_T )
117 .section ".librm", "awx", @progbits
126 #define OFFSET(sym) ( sym - _librm_start )
129 #define EXPORT(sym) \
132 .equ _ ## sym, OFFSET(sym) ; \
135 /****************************************************************************
136 * Note that the first sizeof(struct free_base_memory_header) bytes of
137 * librm will get vapourised by free_base_memory(). Since we need
138 * librm to continue working even when this happens, we put some
141 * We must also ensure that the total size of librm is <1kB, otherwise
142 * free_base_memory() will stomp somewhere in the middle of us as
144 ****************************************************************************
146 .fill FREE_BASEMEM_HEADER_SIZE, 1, 0
148 /****************************************************************************
149 * Record of the current physical location of the installed copy.
150 * Used by prot_call in order to return via the current installed copy
151 * even if Etherboot has been relocated during the protected-mode
153 ****************************************************************************
158 /****************************************************************************
159 * GDT for initial transition to protected mode
161 * PHYSICAL_CS and PHYSICAL_DS are defined in an external header file.
162 * We use only those selectors, and construct our GDT to match the
163 * selector values we're asked to use. Use PHYSICAL_CS=0x08 and
164 * PHYSICAL_DS=0x10 to minimise the space occupied by this GDT.
166 * Note: pm_gdt is also used to store the location of the
167 * protected-mode GDT as recorded on entry to prot_to_real.
168 ****************************************************************************
172 pm_gdt_limit: .word pm_gdt_length - 1
174 .word 0 /* padding */
176 .org pm_gdt + PHYSICAL_CS
178 /* 32 bit protected mode code segment, physical addresses */
180 .byte 0, 0x9f, 0xcf, 0
182 .org pm_gdt + PHYSICAL_DS
184 /* 32 bit protected mode data segment, physical addresses */
189 .equ pm_gdt_length, pm_gdt_end - pm_gdt
191 /****************************************************************************
192 * GDT for transition to real mode
194 * This is used primarily to set 64kB segment limits. Define
195 * FLATTEN_REAL_MODE if you want to use so-called "flat real mode"
196 * with 4GB limits instead. The base address of each of the segments
197 * will be adjusted at run-time.
199 * NOTE: This must be located before prot_to_real, otherwise gas
200 * throws a "can't handle non absolute segment in `ljmp'" error due to
201 * not knowing the value of RM_CS when the ljmp is encountered.
203 * Note also that putting ".word rm_gdt_end - rm_gdt - 1" directly
204 * into rm_gdt_limit, rather than going via rm_gdt_length, will also
205 * produce the "non absolute segment" error. This is most probably a
207 ****************************************************************************
210 #ifdef FLATTEN_REAL_MODE
211 #define RM_LIMIT_16_19__AVL__SIZE__GRANULARITY 0x8f
213 #define RM_LIMIT_16_19__AVL__SIZE__GRANULARITY 0x00
217 rm_gdt_limit: .word rm_gdt_length - 1
219 .word 0 /* padding */
221 rm_gdt_rm_cs: /* 16 bit real mode code segment */
222 .equ RM_CS, rm_gdt_rm_cs - rm_gdt
223 .word 0xffff,(0&0xffff)
224 .byte (0>>16),0x9b,RM_LIMIT_16_19__AVL__SIZE__GRANULARITY,(0>>24)
226 rm_gdt_rm_ds: /* 16 bit real mode data segment */
227 .equ RM_DS, rm_gdt_rm_ds - rm_gdt
228 .word 0xffff,(0&0xffff)
229 .byte (0>>16),0x93,RM_LIMIT_16_19__AVL__SIZE__GRANULARITY,(0>>24)
232 .equ rm_gdt_length, rm_gdt_end - rm_gdt
234 /****************************************************************************
235 * real_to_prot (real-mode far call)
237 * Switch from 16-bit real-mode to 32-bit protected mode with flat
238 * physical addresses. %esp is restored from the saved pm_esp. All
239 * segment registers are set to flat physical-mode values. All other
240 * registers are preserved. Interrupts are disabled.
242 * Note that this routine can be called *without* having first set up
243 * a stored pm_esp or stored GDT. If you do this, real_to_prot will
244 * return with a temporary stack that is only *FOUR BYTES* in size.
245 * This is just enough to enable you to do a "call 1f; popl %ebp"
246 * sequence in order to find out your physical address and then load a
247 * proper 32-bit protected-mode stack pointer. Do *NOT* use more than
248 * four bytes since this will overwrite code in librm!
251 ****************************************************************************
255 EXPORT(real_to_prot):
256 /* Disable interrupts */
259 /* Set %ds = %cs, for easier access to variables */
263 /* Preserve registers */
264 movl %eax, %ds:OFFSET(save_eax)
265 movl %ebx, %ds:OFFSET(save_ebx)
267 /* Extract real-mode far return address from stack */
268 popl %ds:OFFSET(save_retaddr)
270 /* Record real-mode stack pointer */
271 movw %sp, %ds:OFFSET(rm_sp)
273 popw %ds:OFFSET(rm_ss)
275 /* Physical base address of librm to %ebx */
280 /* Record physical base address of librm */
281 movl %ebx, %ds:OFFSET(librm_base)
283 /* Check base address of stored protected-mode GDT. If it's
284 * zero, set it up to use our internal GDT (with physical
287 movl %ds:OFFSET(pm_gdt_addr), %eax
290 /* Use internal GDT */
292 addl $OFFSET(pm_gdt), %eax
293 movl %eax, %ds:OFFSET(pm_gdt_addr)
296 /* Set up protected-mode continuation address on real-mode stack */
299 addl $OFFSET(1f), %eax
302 /* Restore protected-mode GDT */
303 data32 lgdt %ds:OFFSET(pm_gdt)
305 /* Switch to protected mode */
310 /* Flush prefetch queue and reload %cs:eip */
314 /* Set up protected-mode stack and data segments */
315 movw $PHYSICAL_DS, %ax
322 /* Switch to saved protected-mode stack. Note that there may
323 * not actually *be* a saved protected-mode stack.
325 movl OFFSET(pm_esp)(%ebx), %esp
328 /* No stack - use save_retaddr as a 4-byte temporary stack */
329 leal OFFSET(save_retaddr+4)(%ebx), %esp
332 /* Convert real-mode far return address to physical address
335 pushl OFFSET(save_retaddr)(%ebx)
341 /* Restore registers and return */
342 movl OFFSET(save_eax)(%ebx), %eax
343 movl OFFSET(save_ebx)(%ebx), %ebx
346 /****************************************************************************
347 * prot_to_real (protected-mode near call, physical addresses)
349 * Switch from 32-bit protected mode with flat physical addresses to
350 * 16-bit real mode. %ss:sp is restored from the saved rm_ss and
351 * rm_sp. %cs is set such that %cs:0000 is the start of librm. All
352 * other segment registers are set to %ss. All other registers are
353 * preserved. Interrupts are *not* enabled, since we want to be able
354 * to use this routine inside an ISR.
356 * Note that since %cs:0000 points to the start of librm on exit, it
357 * follows that the code calling prot_to_real must be located within
358 * 64kB of the start of librm.
361 ****************************************************************************
365 EXPORT(prot_to_real):
366 /* Calculate physical base address of librm in %ebx, preserve
367 * original %eax and %ebx in save_eax and save_ebx
372 subl $OFFSET(1b), %ebx
373 popl OFFSET(save_ebx)(%ebx)
374 movl %eax, OFFSET(save_eax)(%ebx)
376 /* Record physical base address of librm */
377 movl %ebx, OFFSET(librm_base)(%ebx)
379 /* Extract return address from the stack, convert to offset
380 * within librm and save in save_retaddr
384 movl %eax, OFFSET(save_retaddr)(%ebx)
386 /* Record protected-mode stack pointer */
387 movl %esp, OFFSET(pm_esp)(%ebx)
389 /* Record protected-mode GDT */
390 sgdt OFFSET(pm_gdt)(%ebx)
392 /* Set up real-mode GDT */
393 leal OFFSET(rm_gdt)(%ebx), %eax
394 movl %eax, OFFSET(rm_gdt_base)(%ebx)
397 movw %bx, OFFSET(rm_gdt_rm_cs+2)(%ebx)
398 movb %al, OFFSET(rm_gdt_rm_cs+4)(%ebx)
399 movw %bx, OFFSET(rm_gdt_rm_ds+2)(%ebx)
400 movb %al, OFFSET(rm_gdt_rm_ds+4)(%ebx)
402 /* Switch to real-mode GDT and reload segment registers to get
403 * 64kB limits. Stack is invalidated by this process.
405 lgdt OFFSET(rm_gdt)(%ebx)
415 /* Calculate real-mode code segment in %ax and store in ljmp
420 movw %ax, OFFSET(p2r_ljmp) + 3
422 /* Switch to real mode */
427 /* Intersegment jump to flush prefetch queue and reload
428 * %cs:eip. The segment gets filled in by the above code. We
429 * can't just use lret to achieve this, because we have no
430 * stack at the moment.
436 /* Set %ds to point to code segment for easier data access */
439 /* Restore registers */
440 movl OFFSET(save_eax), %eax
441 movl OFFSET(save_ebx), %ebx
443 /* Set up real-mode data segments and stack */
444 movw OFFSET(rm_ss), %ss
445 movw OFFSET(rm_sp), %sp
455 /* Set up return address on stack and return */
456 pushw %cs:OFFSET(save_retaddr)
459 /****************************************************************************
460 * prot_call (real-mode far call)
462 * Call a specific C function in the protected-mode code. The
463 * prototype of the C function must be
464 * void function ( struct i386_all_regs *ix86 );
465 * ix86 will point to a struct containing the real-mode registers
466 * at entry to prot_call.
468 * All registers will be preserved across prot_call(), unless the C
469 * function explicitly overwrites values in ix86. Interrupt status
470 * will also be preserved. Gate A20 will be enabled.
472 * The protected-mode code may install librm to a new location. If it
473 * does so, it must update librm_base in *this* copy of librm to point
474 * to the new physical location. prot_call will then return via the
475 * newly installed copy.
477 * Note that when Etherboot performs its initial relocation, "*this*"
478 * copy in the above paragraph will refer to the "master" copy, since
479 * that is the initial installed copy. Etherboot will return to
480 * prot_call using a virtual address, so will return to the master
481 * copy in high memory (rather than the original copy in base memory).
482 * The master copy in high memory will have the physical address of
483 * the newly installed copy in librm_base, since install_librm()
484 * writes it there. Thus, Etherboot's initialise() function will
485 * return to the master copy of prot_call(), which will then jump to
486 * the installed copy.
488 * It works, trust me.
491 * function : virtual address of protected-mode function to call
494 * pushl $pxe_api_call
495 * lcall $LIBRM_SEGMENT, $prot_call
497 * to call in to the C function
498 * void pxe_api_call ( struct i386_all_regs *ix86 );
499 ****************************************************************************
502 #define PC_OFFSET_IX86 ( 0 )
503 #define PC_OFFSET_RETADDR ( PC_OFFSET_IX86 + SIZEOF_I386_ALL_REGS )
504 #define PC_OFFSET_FUNCTION ( PC_OFFSET_RETADDR + 4 )
508 /* Preserve registers and flags on RM stack */
518 /* Record RM stack pointer */
522 /* Physical address of RM stack pointer to %esi */
529 /* Address of pmode function to %ebx */
530 movl %ss:(PC_OFFSET_FUNCTION)(%bp), %ebx
532 /* Switch to protected mode */
537 /* Copy ix86 from RM stack to PM stack */
538 movl $SIZEOF_I386_ALL_REGS, %ecx
544 popl %edi /* %edi = phys addr of RM copy of ix86 */
546 /* Switch to virtual addresses. */
549 1: ljmp $VIRTUAL_CS, $_phys_to_virt
552 /* Enable A20 line */
554 lcall $VIRTUAL_CS, $gateA20_set
555 popl %eax /* discard */
558 /* Push &ix86 on the stack, and call function */
561 popl %eax /* discard */
563 /* Switch to physical addresses, discard PM register store */
564 lcall $VIRTUAL_CS, $_virt_to_phys
565 popl %eax /* discard */
567 /* Copy ix86 from PM stack to RM stack, and remove ix86
568 * from PM stack. (%edi still contains physical address of
569 * ix86 on RM stack from earlier, since C code preserves
573 movl $SIZEOF_I386_ALL_REGS, %ecx
576 movl %esi, %esp /* remove ix86 from PM stack */
578 /* Obtain physical base address of installed copy of librm in
579 * %ebx. (It's possible that this *isn't* the physical base
580 * address of the copy we're currently executing in, because
581 * the protected-mode call could have moved librm. If it does
582 * so, it must update librm_base in our copy to reflect the
587 movl OFFSET(librm_base-1b)(%ebp), %ebx
589 /* Jump to running in installed copy of librm */
590 addl $OFFSET(1f), %ebx
594 /* Switch to real mode */
598 /* Restore registers and flags, and return */
599 popw %ax /* skip %cs */
600 popw %ax /* skip %ss */
609 /****************************************************************************
610 * real_call (protected-mode near call, virtual addresses)
612 * Call a real-mode function from protected-mode code.
614 * The non-segment register values will be passed directly to the
615 * real-mode code. The segment registers will be set as per
616 * prot_to_real. The non-segment register values set by the real-mode
617 * function will be passed back to the protected-mode caller. A
618 * result of this is that this routine cannot be called directly from
619 * C code, since it clobbers registers that the C ABI expects the
620 * callee to preserve. Gate A20 will be re-enabled in case the
621 * real-mode routine disabled it.
623 * librm.h defines two convenient macros for using real_call:
624 * REAL_CALL and REAL_EXEC. See librm.h and realmode.h for details
628 * far pointer to real-mode function to call
631 ****************************************************************************
634 #define RC_OFFSET_PRESERVE_REGS ( 0 )
635 #define RC_OFFSET_RETADDR ( RC_OFFSET_PRESERVE_REGS + 8 )
636 #define RC_OFFSET_RM_FUNCTION ( RC_OFFSET_RETADDR + 4 )
640 /* Preserve registers */
644 /* Switch to physical addresses */
645 lcall $VIRTUAL_CS, $_virt_to_phys
648 /* Extract real-mode function address and store in ljmp instruction */
651 movl RC_OFFSET_RM_FUNCTION(%esp), %eax
652 movl %eax, (rc_ljmp + 1 - 1b)(%ebp)
654 /* Restore registers */
658 /* Switch to real mode, preserving non-segment registers */
662 /* Far call to real-mode routine */
667 ljmp $0, $0 /* address filled in by above code */
670 /* Switch to protected mode */
675 /* Switch to virtual addresses */
678 1: ljmp $VIRTUAL_CS, $_phys_to_virt
681 /* Enable A20 line */
683 lcall $VIRTUAL_CS, $gateA20_set
684 popl %eax /* discard */
690 /****************************************************************************
691 * Relocation lock counter
693 * librm may be moved in base memory only when this counter is zero.
694 * The counter gets incremented whenever a reference to librm is
695 * generated (e.g. a real_call is made, resulting in a return address
696 * pointing to librm being placed on the stack), and decremented when
697 * the reference goes out of scope (e.g. the real_call returns).
698 ****************************************************************************
700 EXPORT(librm_ref_count): .byte 0
702 /****************************************************************************
703 * Stored real-mode and protected-mode stack pointers
705 * The real-mode stack pointer is stored here whenever real_to_prot
706 * is called and restored whenever prot_to_real is called. The
707 * converse happens for the protected-mode stack pointer.
709 * Despite initial appearances this scheme is, in fact re-entrant,
710 * because program flow dictates that we always return via the point
711 * we left by. For example:
715 * Print a text string
725 * At point 1, the RM mode stack value, say RPXE, is stored in
726 * rm_ss,sp. We want this value to still be present in rm_ss,sp when
729 * At point 2, the RM stack value is restored from RPXE. At point 3,
730 * the RM stack value is again stored in rm_ss,sp. This *does*
731 * overwrite the RPXE that we have stored there, but it's the same
732 * value, since the code between points 2 and 3 has managed to return
734 ****************************************************************************
737 EXPORT(rm_stack): /* comprises rm_ss and rm_sp */
744 /****************************************************************************
745 * Temporary variables
746 ****************************************************************************
750 save_retaddr: .long 0
752 /****************************************************************************
754 ****************************************************************************
758 .equ _librm_size, _librm_end - _librm_start