iPXE
Macros | Functions | Variables
relocate.c File Reference
#include <ipxe/uaccess.h>
#include <ipxe/memmap.h>
#include <registers.h>

Go to the source code of this file.

Macros

#define MAX_ADDR   (0xfff00000UL)
 
#define ALIGN   4096
 

Functions

 FILE_LICENCE (GPL2_OR_LATER_OR_UBDL)
 
__asmcall void relocate (struct i386_all_regs *ix86)
 Relocate iPXE. More...
 

Variables

char _textdata []
 
char _etextdata []
 

Macro Definition Documentation

◆ MAX_ADDR

#define MAX_ADDR   (0xfff00000UL)

Definition at line 24 of file relocate.c.

◆ ALIGN

#define ALIGN   4096

Definition at line 30 of file relocate.c.

Function Documentation

◆ FILE_LICENCE()

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL  )

◆ relocate()

__asmcall void relocate ( struct i386_all_regs ix86)

Relocate iPXE.

Parameters
ebpMaximum address to use for relocation
Return values
esiCurrent physical address
ediNew physical address
ecxLength to copy

This finds a suitable location for iPXE near the top of 32-bit address space, and returns the physical address of the new location to the prefix in edi.

Definition at line 44 of file relocate.c.

44  {
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 }
static int memmap_is_usable(const struct memmap_region *region)
Check if memory region is usable.
Definition: memmap.h:86
uint32_t ebp
Definition: registers.h:73
#define max(x, y)
Definition: ath.h:40
uint16_t size
Buffer size.
Definition: dwmac.h:14
#define DBGC(...)
Definition: compiler.h:505
#define MAX_ADDR
Definition: relocate.c:24
#define DBGC_MEMMAP(...)
Definition: memmap.h:206
uint32_t start
Starting offset.
Definition: netvsc.h:12
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:30
uint32_t ecx
Definition: registers.h:101
char _etextdata[]
unsigned long physaddr_t
Definition: stdint.h:20
static void memmap_dump_all(int hide)
Dump system memory map (for debugging)
Definition: memmap.h:215
#define for_each_memmap_from(region, start, hide)
Iterate over memory regions from a given starting address.
Definition: memmap.h:170
static uint64_t memmap_size(const struct memmap_region *region)
Get remaining size of memory region (from the described address upwards)
Definition: memmap.h:98
uint32_t end
Ending offset.
Definition: netvsc.h:18
A memory region descriptor.
Definition: memmap.h:48
char _textdata[]

References _etextdata, _textdata, ALIGN, DBGC, DBGC_MEMMAP, i386_regs::ebp, i386_regs::ecx, i386_regs::edi, end, i386_regs::esi, for_each_memmap_from, max, MAX_ADDR, memmap_dump_all(), memmap_is_usable(), memmap_size(), memmap_region::min, i386_all_regs::regs, size, and start.

Variable Documentation

◆ _textdata

char _textdata[]

Referenced by relocate().

◆ _etextdata

char _etextdata[]

Referenced by relocate().