david/ipxe
david
/
ipxe
Archived
1
0
Fork 0

Update relocate() to work with get_memmap().

Change semantics; relocate() now just finds a suitable location; it
doesn't actually perform the relocation itself.  Code in libprefix does
the copy in flat real mode.
This commit is contained in:
Michael Brown 2006-05-25 00:04:13 +00:00
parent f939ff7cb5
commit 1966945a5d
1 changed files with 44 additions and 66 deletions

View File

@ -1,6 +1,6 @@
#include <virtaddr.h> #include <io.h>
#include <registers.h> #include <registers.h>
#include <memsizes.h> #include <memmap.h>
/* /*
* Originally by Eric Biederman * Originally by Eric Biederman
@ -35,14 +35,27 @@ extern char _end[];
* @v ix86 x86 register dump from prefix * @v ix86 x86 register dump from prefix
* @ret ix86 x86 registers to return to prefix * @ret ix86 x86 registers to return to prefix
* *
* This copies Etherboot to a suitable location near the top of 32-bit * This finds a suitable location for Etherboot near the top of 32-bit
* address space, and returns the physical address of the new location * address space, and returns the physical address of the new location
* to the prefix in %edi. * to the prefix in %edi.
*/ */
void relocate ( struct i386_all_regs *ix86 ) { void relocate ( struct i386_all_regs *ix86 ) {
unsigned long addr, eaddr, size; struct memory_map memmap;
unsigned long start, end, size, padded_size;
unsigned long new_start, new_end;
unsigned i; unsigned i;
/* Get memory map and current location */
get_memmap ( &memmap );
start = virt_to_phys ( _text );
end = virt_to_phys ( _end );
size = ( end - start );
padded_size = ( size + max_align - 1 );
DBG ( "Relocate: currently at [%lx,%lx)\n"
"...need %lx bytes for %d-byte alignment\n",
start, end, padded_size, max_align );
/* Walk through the memory map and find the highest address /* Walk through the memory map and find the highest address
* below 4GB that etherboot will fit into. Ensure etherboot * below 4GB that etherboot will fit into. Ensure etherboot
* lies entirely within a range with A20=0. This means that * lies entirely within a range with A20=0. This means that
@ -50,58 +63,29 @@ void relocate ( struct i386_all_regs *ix86 ) {
* etherboot code is still visible and we have a chance to * etherboot code is still visible and we have a chance to
* diagnose the problem. * diagnose the problem.
*/ */
new_end = end;
/* First find the size of etherboot, including enough space to for ( i = 0 ; i < memmap.count ; i++ ) {
* pad it to the required alignment struct memory_region *region = &memmap.regions[i];
*/
size = _end - _text + max_align - 1;
/* Current end address of Etherboot. If the current etherboot
* is beyond MAX_ADDR pretend it is at the lowest possible
* address.
*/
eaddr = virt_to_phys(_end);
if ( eaddr > MAX_ADDR ) {
eaddr = 0;
}
DBG ( "Relocate: currently at [%x,%x)\n"
"...need %x bytes for %d-byte alignment\n",
virt_to_phys ( _text ), eaddr, size, max_align );
for ( i = 0; i < meminfo.map_count; i++ ) {
unsigned long r_start, r_end; unsigned long r_start, r_end;
DBG ( "Considering [%x%x,%x%x)\n", DBG ( "Considering [%llx,%llx)\n", region->start, region->end);
( unsigned long ) ( meminfo.map[i].addr >> 32 ),
( unsigned long ) meminfo.map[i].addr,
( unsigned long )
( ( meminfo.map[i].addr + meminfo.map[i].size ) >> 32 ),
( unsigned long )
( meminfo.map[i].addr + meminfo.map[i].size ) );
/* Check block is usable memory */
if (meminfo.map[i].type != E820_RAM) {
DBG ( "...not RAM\n" );
continue;
}
/* Truncate block to MAX_ADDR. This will be less than /* Truncate block to MAX_ADDR. This will be less than
* 4GB, which means that we can get away with using * 4GB, which means that we can get away with using
* just 32-bit arithmetic after this stage. * just 32-bit arithmetic after this stage.
*/ */
if ( meminfo.map[i].addr > MAX_ADDR ) { if ( region->start > MAX_ADDR ) {
DBG ( "...starts after MAX_ADDR=%x\n", MAX_ADDR ); DBG ( "...starts after MAX_ADDR=%lx\n", MAX_ADDR );
continue; continue;
} }
r_start = meminfo.map[i].addr; r_start = region->start;
if ( meminfo.map[i].addr + meminfo.map[i].size > MAX_ADDR ) { if ( region->end > MAX_ADDR ) {
DBG ( "...end truncated to MAX_ADDR=%lx\n", MAX_ADDR );
r_end = MAX_ADDR; r_end = MAX_ADDR;
DBG ( "...end truncated to MAX_ADDR=%x\n", MAX_ADDR );
} else { } else {
r_end = meminfo.map[i].addr + meminfo.map[i].size; r_end = region->end;
} }
/* Shrink the range down to use only even megabytes /* Shrink the range down to use only even megabytes
* (i.e. A20=0). * (i.e. A20=0).
*/ */
@ -111,7 +95,7 @@ void relocate ( struct i386_all_regs *ix86 ) {
* the top of the next even megabyte. * the top of the next even megabyte.
*/ */
r_end = ( r_end - 1 ) & ~0xfffff; r_end = ( r_end - 1 ) & ~0xfffff;
DBG ( "...end truncated to %x " DBG ( "...end truncated to %lx "
"(avoid ending in odd megabyte)\n", "(avoid ending in odd megabyte)\n",
r_end ); r_end );
} else if ( ( r_end - size ) & 0x100000 ) { } else if ( ( r_end - size ) & 0x100000 ) {
@ -126,13 +110,13 @@ void relocate ( struct i386_all_regs *ix86 ) {
*/ */
if ( r_end > 0x100000 ) { if ( r_end > 0x100000 ) {
r_end = ( r_end - 0x100000 ) & ~0xfffff; r_end = ( r_end - 0x100000 ) & ~0xfffff;
DBG ( "...end truncated to %x " DBG ( "...end truncated to %lx "
"(avoid starting in odd megabyte)\n", "(avoid starting in odd megabyte)\n",
r_end ); r_end );
} }
} }
DBG ( "...usable portion is [%x,%x)\n", r_start, r_end ); DBG ( "...usable portion is [%lx,%lx)\n", r_start, r_end );
/* If we have rounded down r_end below r_ start, skip /* If we have rounded down r_end below r_ start, skip
* this block. * this block.
@ -143,8 +127,8 @@ void relocate ( struct i386_all_regs *ix86 ) {
} }
/* Check that there is enough space to fit in Etherboot */ /* Check that there is enough space to fit in Etherboot */
if ( r_end - r_start < size ) { if ( ( r_end - r_start ) < size ) {
DBG ( "...too small (need %x bytes)\n", size ); DBG ( "...too small (need %lx bytes)\n", size );
continue; continue;
} }
@ -156,30 +140,24 @@ void relocate ( struct i386_all_regs *ix86 ) {
* Etherboot, as well as choosing the highest of all * Etherboot, as well as choosing the highest of all
* viable blocks. * viable blocks.
*/ */
if ( r_end - size > eaddr ) { if ( ( r_end - size ) > new_end ) {
eaddr = r_end; new_end = r_end;
DBG ( "...new best block found.\n" ); DBG ( "...new best block found.\n" );
} }
} }
DBG ( "New location will be in [%x,%x)\n", eaddr - size, eaddr );
/* Calculate new location of Etherboot, and align it to the /* Calculate new location of Etherboot, and align it to the
* required alignemnt. * required alignemnt.
*/ */
addr = eaddr - size; new_start = new_end - padded_size;
addr += ( virt_to_phys ( _text ) - addr ) & ( max_align - 1 ); new_start += ( start - new_start ) & ( max_align - 1 );
DBG ( "After alignment, new location is [%x,%x)\n", new_end = new_start + size;
addr, addr + _end - _text );
if ( addr != virt_to_phys ( _text ) ) { DBG ( "Relocating from [%lx,%lx) to [%lx,%lx)\n",
DBG ( "Relocating _text from: [%lx,%lx) to [%lx,%lx)\n", start, end, new_start, new_end );
virt_to_phys ( _text ), virt_to_phys ( _end ),
addr, addr + _end - _text );
memcpy ( phys_to_virt ( addr ), _text, _end - _text );
}
/* Let prefix know where the new copy is */ /* Let prefix know what to copy */
ix86->regs.edi = addr; ix86->regs.esi = start;
ix86->regs.edi = new_start;
ix86->regs.ecx = size;
} }