iPXE
relocate.c
Go to the documentation of this file.
00001 #include <ipxe/io.h>
00002 #include <registers.h>
00003 
00004 /*
00005  * Originally by Eric Biederman
00006  *
00007  * Heavily modified by Michael Brown 
00008  *
00009  */
00010 
00011 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
00012 
00013 /* Linker symbols */
00014 extern char _textdata[];
00015 extern char _etextdata[];
00016 
00017 /* within 1MB of 4GB is too close. 
00018  * MAX_ADDR is the maximum address we can easily do DMA to.
00019  *
00020  * Not sure where this constraint comes from, but kept it from Eric's
00021  * old code - mcb30
00022  */
00023 #define MAX_ADDR (0xfff00000UL)
00024 
00025 /* Preserve alignment to a 4kB page
00026  *
00027  * Required for x86_64, and doesn't hurt for i386.
00028  */
00029 #define ALIGN 4096
00030 
00031 /**
00032  * Relocate iPXE
00033  *
00034  * @v ebp               Maximum address to use for relocation
00035  * @ret esi             Current physical address
00036  * @ret edi             New physical address
00037  * @ret ecx             Length to copy
00038  *
00039  * This finds a suitable location for iPXE near the top of 32-bit
00040  * address space, and returns the physical address of the new location
00041  * to the prefix in %edi.
00042  */
00043 __asmcall void relocate ( struct i386_all_regs *ix86 ) {
00044         struct memory_map memmap;
00045         uint32_t start, end, size, padded_size, max;
00046         uint32_t new_start, new_end;
00047         unsigned i;
00048 
00049         /* Get memory map and current location */
00050         get_memmap ( &memmap );
00051         start = virt_to_phys ( _textdata );
00052         end = virt_to_phys ( _etextdata );
00053         size = ( end - start );
00054         padded_size = ( size + ALIGN - 1 );
00055 
00056         DBG ( "Relocate: currently at [%x,%x)\n"
00057               "...need %x bytes for %d-byte alignment\n",
00058               start, end, padded_size, ALIGN );
00059 
00060         /* Determine maximum usable address */
00061         max = MAX_ADDR;
00062         if ( ix86->regs.ebp < max ) {
00063                 max = ix86->regs.ebp;
00064                 DBG ( "Limiting relocation to [0,%x)\n", max );
00065         }
00066 
00067         /* Walk through the memory map and find the highest address
00068          * below 4GB that iPXE will fit into.
00069          */
00070         new_end = end;
00071         for ( i = 0 ; i < memmap.count ; i++ ) {
00072                 struct memory_region *region = &memmap.regions[i];
00073                 uint32_t r_start, r_end;
00074 
00075                 DBG ( "Considering [%llx,%llx)\n", region->start, region->end);
00076                 
00077                 /* Truncate block to maximum address.  This will be
00078                  * less than 4GB, which means that we can get away
00079                  * with using just 32-bit arithmetic after this stage.
00080                  */
00081                 if ( region->start > max ) {
00082                         DBG ( "...starts after max=%x\n", max );
00083                         continue;
00084                 }
00085                 r_start = region->start;
00086                 if ( region->end > max ) {
00087                         DBG ( "...end truncated to max=%x\n", max );
00088                         r_end = max;
00089                 } else {
00090                         r_end = region->end;
00091                 }
00092                 DBG ( "...usable portion is [%x,%x)\n", r_start, r_end );
00093 
00094                 /* If we have rounded down r_end below r_ start, skip
00095                  * this block.
00096                  */
00097                 if ( r_end < r_start ) {
00098                         DBG ( "...truncated to negative size\n" );
00099                         continue;
00100                 }
00101 
00102                 /* Check that there is enough space to fit in iPXE */
00103                 if ( ( r_end - r_start ) < size ) {
00104                         DBG ( "...too small (need %x bytes)\n", size );
00105                         continue;
00106                 }
00107 
00108                 /* If the start address of the iPXE we would
00109                  * place in this block is higher than the end address
00110                  * of the current highest block, use this block.
00111                  *
00112                  * Note that this avoids overlaps with the current
00113                  * iPXE, as well as choosing the highest of all viable
00114                  * blocks.
00115                  */
00116                 if ( ( r_end - size ) > new_end ) {
00117                         new_end = r_end;
00118                         DBG ( "...new best block found.\n" );
00119                 }
00120         }
00121 
00122         /* Calculate new location of iPXE, and align it to the
00123          * required alignemnt.
00124          */
00125         new_start = new_end - padded_size;
00126         new_start += ( ( start - new_start ) & ( ALIGN - 1 ) );
00127         new_end = new_start + size;
00128 
00129         DBG ( "Relocating from [%x,%x) to [%x,%x)\n",
00130               start, end, new_start, new_end );
00131         
00132         /* Let prefix know what to copy */
00133         ix86->regs.esi = start;
00134         ix86->regs.edi = new_start;
00135         ix86->regs.ecx = size;
00136 }