iPXE
relocate.c
Go to the documentation of this file.
1 #include <ipxe/io.h>
2 #include <registers.h>
3 
4 /*
5  * Originally by Eric Biederman
6  *
7  * Heavily modified by Michael Brown
8  *
9  */
10 
11 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
12 
13 /* Linker symbols */
14 extern char _textdata[];
15 extern char _etextdata[];
16 
17 /* within 1MB of 4GB is too close.
18  * MAX_ADDR is the maximum address we can easily do DMA to.
19  *
20  * Not sure where this constraint comes from, but kept it from Eric's
21  * old code - mcb30
22  */
23 #define MAX_ADDR (0xfff00000UL)
24 
25 /* Preserve alignment to a 4kB page
26  *
27  * Required for x86_64, and doesn't hurt for i386.
28  */
29 #define ALIGN 4096
30 
31 /**
32  * Relocate iPXE
33  *
34  * @v ebp Maximum address to use for relocation
35  * @ret esi Current physical address
36  * @ret edi New physical address
37  * @ret ecx Length to copy
38  *
39  * This finds a suitable location for iPXE near the top of 32-bit
40  * address space, and returns the physical address of the new location
41  * to the prefix in %edi.
42  */
43 __asmcall void relocate ( struct i386_all_regs *ix86 ) {
44  struct memory_map memmap;
45  uint32_t start, end, size, padded_size, max;
46  uint32_t new_start, new_end;
47  unsigned i;
48 
49  /* Get memory map and current location */
50  get_memmap ( &memmap );
53  size = ( end - start );
54  padded_size = ( size + ALIGN - 1 );
55 
56  DBG ( "Relocate: currently at [%x,%x)\n"
57  "...need %x bytes for %d-byte alignment\n",
58  start, end, padded_size, ALIGN );
59 
60  /* Determine maximum usable address */
61  max = MAX_ADDR;
62  if ( ix86->regs.ebp < max ) {
63  max = ix86->regs.ebp;
64  DBG ( "Limiting relocation to [0,%x)\n", max );
65  }
66 
67  /* Walk through the memory map and find the highest address
68  * below 4GB that iPXE will fit into.
69  */
70  new_end = end;
71  for ( i = 0 ; i < memmap.count ; i++ ) {
72  struct memory_region *region = &memmap.regions[i];
73  uint32_t r_start, r_end;
74 
75  DBG ( "Considering [%llx,%llx)\n", region->start, region->end);
76 
77  /* Truncate block to maximum address. This will be
78  * less than 4GB, which means that we can get away
79  * with using just 32-bit arithmetic after this stage.
80  */
81  if ( region->start > max ) {
82  DBG ( "...starts after max=%x\n", max );
83  continue;
84  }
85  r_start = region->start;
86  if ( region->end > max ) {
87  DBG ( "...end truncated to max=%x\n", max );
88  r_end = max;
89  } else {
90  r_end = region->end;
91  }
92  DBG ( "...usable portion is [%x,%x)\n", r_start, r_end );
93 
94  /* If we have rounded down r_end below r_ start, skip
95  * this block.
96  */
97  if ( r_end < r_start ) {
98  DBG ( "...truncated to negative size\n" );
99  continue;
100  }
101 
102  /* Check that there is enough space to fit in iPXE */
103  if ( ( r_end - r_start ) < size ) {
104  DBG ( "...too small (need %x bytes)\n", size );
105  continue;
106  }
107 
108  /* If the start address of the iPXE we would
109  * place in this block is higher than the end address
110  * of the current highest block, use this block.
111  *
112  * Note that this avoids overlaps with the current
113  * iPXE, as well as choosing the highest of all viable
114  * blocks.
115  */
116  if ( ( r_end - size ) > new_end ) {
117  new_end = r_end;
118  DBG ( "...new best block found.\n" );
119  }
120  }
121 
122  /* Calculate new location of iPXE, and align it to the
123  * required alignemnt.
124  */
125  new_start = new_end - padded_size;
126  new_start += ( ( start - new_start ) & ( ALIGN - 1 ) );
127  new_end = new_start + size;
128 
129  DBG ( "Relocating from [%x,%x) to [%x,%x)\n",
130  start, end, new_start, new_end );
131 
132  /* Let prefix know what to copy */
133  ix86->regs.esi = start;
134  ix86->regs.edi = new_start;
135  ix86->regs.ecx = size;
136 }
iPXE I/O API
void get_memmap(struct memory_map *memmap)
Get memory map.
__asmcall void relocate(struct i386_all_regs *ix86)
Relocate iPXE.
Definition: relocate.c:43
uint32_t ebp
Definition: registers.h:73
#define max(x, y)
Definition: ath.h:39
unsigned int count
Number of used regions.
Definition: io.h:503
i386 registers.
#define MAX_ADDR
Definition: relocate.c:23
A memory map.
Definition: io.h:499
A full register dump.
Definition: registers.h:174
static __always_inline unsigned long virt_to_phys(volatile const void *addr)
Convert virtual address to a physical address.
Definition: uaccess.h:287
uint32_t start
Starting offset.
Definition: netvsc.h:12
struct memory_region regions[MAX_MEMORY_REGIONS]
Memory regions.
Definition: io.h:501
#define __asmcall
Declare a function with standard calling conventions.
Definition: compiler.h:15
uint32_t edi
Definition: registers.h:65
uint32_t esi
Definition: registers.h:69
struct i386_regs regs
Definition: registers.h:176
#define ALIGN
Definition: relocate.c:29
A usable memory region.
Definition: io.h:488
unsigned int uint32_t
Definition: stdint.h:12
uint32_t ecx
Definition: registers.h:101
char _etextdata[]
uint64_t start
Physical start address.
Definition: io.h:490
uint32_t end
Ending offset.
Definition: netvsc.h:18
uint8_t size
Entry size (in 32-bit words)
Definition: ena.h:16
#define DBG(...)
Print a debugging message.
Definition: compiler.h:498
FILE_LICENCE(GPL2_OR_LATER_OR_UBDL)
uint64_t end
Physical end address.
Definition: io.h:492
char _textdata[]