iPXE
relocate.c
Go to the documentation of this file.
1#include <ipxe/uaccess.h>
2#include <ipxe/memmap.h>
3#include <registers.h>
4
5/*
6 * Originally by Eric Biederman
7 *
8 * Heavily modified by Michael Brown
9 *
10 */
11
12FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
13
14/* Linker symbols */
15extern char _textdata[];
16extern char _etextdata[];
17
18/* within 1MB of 4GB is too close.
19 * MAX_ADDR is the maximum address we can easily do DMA to.
20 *
21 * Not sure where this constraint comes from, but kept it from Eric's
22 * old code - mcb30
23 */
24#define MAX_ADDR (0xfff00000UL)
25
26/* Preserve alignment to a 4kB page
27 *
28 * Required for x86_64, and doesn't hurt for i386.
29 */
30#define ALIGN 4096
31
32/**
33 * Relocate iPXE
34 *
35 * @v ebp Maximum address to use for relocation
36 * @ret esi Current physical address
37 * @ret edi New physical address
38 * @ret ecx Length to copy
39 *
40 * This finds a suitable location for iPXE near the top of 32-bit
41 * address space, and returns the physical address of the new location
42 * to the prefix in %edi.
43 */
44__asmcall void relocate ( struct i386_all_regs *ix86 ) {
45 struct memmap_region region;
47 physaddr_t new_start, new_end;
48 physaddr_t r_start, r_end;
49 size_t size, padded_size;
50
51 /* Show whole memory map (for debugging) */
52 memmap_dump_all ( 0 );
53
54 /* Get current location */
55 start = virt_to_phys ( _textdata );
56 end = virt_to_phys ( _etextdata );
57 size = ( end - start );
58 padded_size = ( size + ALIGN - 1 );
59
60 DBGC ( &region, "Relocate: currently at [%#08lx,%#08lx)\n"
61 "...need %#zx bytes for %d-byte alignment\n",
62 start, end, padded_size, ALIGN );
63
64 /* Determine maximum usable address */
65 max = MAX_ADDR;
66 if ( ix86->regs.ebp < max ) {
67 max = ix86->regs.ebp;
68 DBGC ( &region, "Limiting relocation to [0,%#08lx)\n", max );
69 }
70
71 /* Walk through the memory map and find the highest address
72 * above the current iPXE and below 4GB that iPXE will fit
73 * into.
74 */
75 new_end = end;
76 for_each_memmap_from ( &region, end, 0 ) {
77
78 /* Truncate block to maximum address. This will be
79 * strictly less than 4GB, which means that we can get
80 * away with using just 32-bit arithmetic after this
81 * stage.
82 */
83 DBGC_MEMMAP ( &region, &region );
84 if ( region.min > max ) {
85 DBGC ( &region, "...starts after max=%#08lx\n", max );
86 break;
87 }
88 r_start = region.min;
89 if ( ! memmap_is_usable ( &region ) ) {
90 DBGC ( &region, "...not usable\n" );
91 continue;
92 }
93 r_end = ( r_start + memmap_size ( &region ) );
94 if ( ( r_end == 0 ) || ( r_end > max ) ) {
95 DBGC ( &region, "...end truncated to max=%#08lx\n",
96 max );
97 r_end = max;
98 }
99 DBGC ( &region, "...usable portion is [%#08lx,%#08lx)\n",
100 r_start, r_end );
101
102 /* Check that there is enough space to fit in iPXE */
103 if ( ( r_end - r_start ) < padded_size ) {
104 DBGC ( &region, "...too small (need %#zx bytes)\n",
105 padded_size );
106 continue;
107 }
108
109 /* Use highest block with enough space */
110 new_end = r_end;
111 DBGC ( &region, "...new best block found.\n" );
112 }
113
114 /* Calculate new location of iPXE, and align it to the
115 * required alignemnt.
116 */
117 new_start = new_end - padded_size;
118 new_start += ( ( start - new_start ) & ( ALIGN - 1 ) );
119 new_end = new_start + size;
120
121 DBGC ( &region, "Relocating from [%#08lx,%#08lx) to [%#08lx,%#08lx)\n",
122 start, end, new_start, new_end );
123
124 /* Let prefix know what to copy */
125 ix86->regs.esi = start;
126 ix86->regs.edi = new_start;
127 ix86->regs.ecx = size;
128}
#define __asmcall
Declare a function with standard calling conventions.
Definition compiler.h:15
unsigned long physaddr_t
Definition stdint.h:20
#define max(x, y)
Definition ath.h:41
#define DBGC(...)
Definition compiler.h:505
uint32_t start
Starting offset.
Definition netvsc.h:1
uint16_t size
Buffer size.
Definition dwmac.h:3
#define FILE_LICENCE(_licence)
Declare a particular licence as applying to a file.
Definition compiler.h:896
System memory map.
#define for_each_memmap_from(region, start, hide)
Iterate over memory regions from a given starting address.
Definition memmap.h:171
static int memmap_is_usable(const struct memmap_region *region)
Check if memory region is usable.
Definition memmap.h:87
static uint64_t memmap_size(const struct memmap_region *region)
Get remaining size of memory region (from the described address upwards)
Definition memmap.h:99
static void memmap_dump_all(int hide)
Dump system memory map (for debugging)
Definition memmap.h:216
#define DBGC_MEMMAP(...)
Definition memmap.h:207
Access to external ("user") memory.
uint32_t end
Ending offset.
Definition netvsc.h:7
i386 registers.
#define MAX_ADDR
Definition relocate.c:24
char _textdata[]
char _etextdata[]
__asmcall void relocate(struct i386_all_regs *ix86)
Relocate iPXE.
Definition relocate.c:44
#define ALIGN
Definition relocate.c:30
A full register dump.
Definition registers.h:174
struct i386_regs regs
Definition registers.h:176
uint32_t edi
Definition registers.h:65
uint32_t ecx
Definition registers.h:101
uint32_t ebp
Definition registers.h:73
uint32_t esi
Definition registers.h:69
A memory region descriptor.
Definition memmap.h:49
uint64_t min
Minimum address in region.
Definition memmap.h:51