iPXE
memmap.h File Reference

System memory map. More...

#include <stddef.h>
#include <stdint.h>
#include <ipxe/api.h>
#include <ipxe/tables.h>
#include <config/ioapi.h>
#include <ipxe/null_memmap.h>
#include <ipxe/fdtmem.h>
#include <bits/memmap.h>

Go to the source code of this file.

Data Structures

struct  memmap_region
 A memory region descriptor. More...
struct  used_region
 An in-use memory region. More...

Macros

#define MEMMAP_INLINE(_subsys, _api_func)
 Calculate static inline memory map API function name.
#define PROVIDE_MEMMAP(_subsys, _api_func, _func)
 Provide a memory map API implementation.
#define PROVIDE_MEMMAP_INLINE(_subsys, _api_func)
 Provide a static inline memory map API implementation.
#define MEMMAP_FL_MEMORY   0x0001
 Contains memory.
#define MEMMAP_FL_RESERVED   0x0002
 Is reserved.
#define MEMMAP_FL_USED   0x0004
 Is in use by iPXE.
#define MEMMAP_FL_INACCESSIBLE   0x0008
 Outside of addressable range.
#define USED_REGIONS   __table ( struct used_region, "used_regions" )
 In-use memory region table.
#define __used_region   __table_entry ( USED_REGIONS, 01 )
 Declare an in-use memory region.
#define for_each_memmap_from(region, start, hide)
 Iterate over memory regions from a given starting address.
#define for_each_memmap(region, hide)
 Iterate over memory regions.
#define DBG_MEMMAP_IF(level, region)
#define DBGC_MEMMAP_IF(level, id, ...)
#define DBGC_MEMMAP(...)
#define DBGC2_MEMMAP(...)
#define DBGCP_MEMMAP(...)

Functions

 FILE_LICENCE (GPL2_OR_LATER_OR_UBDL)
 FILE_SECBOOT (PERMITTED)
static void memmap_init (uint64_t min, struct memmap_region *region)
 Initialise memory region descriptor.
static int memmap_is_usable (const struct memmap_region *region)
 Check if memory region is usable.
static uint64_t memmap_size (const struct memmap_region *region)
 Get remaining size of memory region (from the described address upwards)
void memmap_describe (uint64_t min, int hide, struct memmap_region *region)
 Describe memory region from system memory map.
void memmap_sync (void)
 Synchronise in-use regions with the externally visible system memory map.
static void memmap_use (struct used_region *used, physaddr_t start, size_t size)
 Update an in-use memory region.
static void memmap_dump_all (int hide)
 Dump system memory map (for debugging)
void memmap_update (struct memmap_region *region, uint64_t start, uint64_t size, unsigned int flags, const char *name)
 Update memory region descriptor.
void memmap_update_used (struct memmap_region *region)
 Update memory region descriptor based on all in-use memory regions.
size_t memmap_largest (physaddr_t *start)
 Find largest usable memory region.

Detailed Description

System memory map.

Definition in file memmap.h.

Macro Definition Documentation

◆ MEMMAP_INLINE

#define MEMMAP_INLINE ( _subsys,
_api_func )
Value:
SINGLE_API_INLINE ( MEMMAP_PREFIX_ ## _subsys, _api_func )
#define SINGLE_API_INLINE(_prefix, _api_func)
Calculate static inline function name.
Definition api.h:45

Calculate static inline memory map API function name.

Parameters
_prefixSubsystem prefix
_api_funcAPI function
Return values
_subsys_funcSubsystem API function

Definition at line 26 of file memmap.h.

26#define MEMMAP_INLINE( _subsys, _api_func ) \
27 SINGLE_API_INLINE ( MEMMAP_PREFIX_ ## _subsys, _api_func )

◆ PROVIDE_MEMMAP

#define PROVIDE_MEMMAP ( _subsys,
_api_func,
_func )
Value:
PROVIDE_SINGLE_API ( MEMMAP_PREFIX_ ## _subsys, _api_func, _func )
#define PROVIDE_SINGLE_API(_prefix, _api_func, _func)
Provide an API implementation.
Definition api.h:55

Provide a memory map API implementation.

Parameters
_prefixSubsystem prefix
_api_funcAPI function
_funcImplementing function

Definition at line 36 of file memmap.h.

36#define PROVIDE_MEMMAP( _subsys, _api_func, _func ) \
37 PROVIDE_SINGLE_API ( MEMMAP_PREFIX_ ## _subsys, _api_func, _func )

◆ PROVIDE_MEMMAP_INLINE

#define PROVIDE_MEMMAP_INLINE ( _subsys,
_api_func )
Value:
PROVIDE_SINGLE_API_INLINE ( MEMMAP_PREFIX_ ## _subsys, _api_func )
#define PROVIDE_SINGLE_API_INLINE(_prefix, _api_func)
Provide a static inline API implementation.
Definition api.h:74

Provide a static inline memory map API implementation.

Parameters
_prefixSubsystem prefix
_api_funcAPI function

Definition at line 45 of file memmap.h.

45#define PROVIDE_MEMMAP_INLINE( _subsys, _api_func ) \
46 PROVIDE_SINGLE_API_INLINE ( MEMMAP_PREFIX_ ## _subsys, _api_func )

◆ MEMMAP_FL_MEMORY

#define MEMMAP_FL_MEMORY   0x0001

◆ MEMMAP_FL_RESERVED

#define MEMMAP_FL_RESERVED   0x0002

Is reserved.

Definition at line 61 of file memmap.h.

Referenced by fdtmem_relocate(), fdtmem_update_node(), and fdtmem_update_tree().

◆ MEMMAP_FL_USED

#define MEMMAP_FL_USED   0x0004

Is in use by iPXE.

Definition at line 62 of file memmap.h.

Referenced by fdtmem_relocate(), and memmap_update_used().

◆ MEMMAP_FL_INACCESSIBLE

#define MEMMAP_FL_INACCESSIBLE   0x0008

Outside of addressable range.

Definition at line 63 of file memmap.h.

Referenced by fdtmem_describe(), and int15_describe().

◆ USED_REGIONS

#define USED_REGIONS   __table ( struct used_region, "used_regions" )

In-use memory region table.

Definition at line 116 of file memmap.h.

Referenced by memmap_update_used().

◆ __used_region

#define __used_region   __table_entry ( USED_REGIONS, 01 )

Declare an in-use memory region.

Definition at line 119 of file memmap.h.

◆ for_each_memmap_from

#define for_each_memmap_from ( region,
start,
hide )
Value:
for ( (region)->min = (start), (region)->max = 0 ; \
( ( ( (region)->max + 1 ) != 0 ) && \
( memmap_describe ( (region)->min, (hide), \
(region) ), 1 ) ) ; \
(region)->min = ( (region)->max + 1 ) )
#define min(x, y)
Definition ath.h:36
#define max(x, y)
Definition ath.h:41
uint32_t start
Starting offset.
Definition netvsc.h:1
void memmap_describe(uint64_t min, int hide, struct memmap_region *region)
Describe memory region from system memory map.
Definition null_memmap.h:29

Iterate over memory regions from a given starting address.

Parameters
regionRegion descriptor
startStarting address
hideHide in-use regions from the memory map

Definition at line 171 of file memmap.h.

171#define for_each_memmap_from( region, start, hide ) \
172 for ( (region)->min = (start), (region)->max = 0 ; \
173 ( ( ( (region)->max + 1 ) != 0 ) && \
174 ( memmap_describe ( (region)->min, (hide), \
175 (region) ), 1 ) ) ; \
176 (region)->min = ( (region)->max + 1 ) )

Referenced by relocate().

◆ for_each_memmap

#define for_each_memmap ( region,
hide )
Value:
for_each_memmap_from ( (region), 0, (hide) )
#define for_each_memmap_from(region, start, hide)
Iterate over memory regions from a given starting address.
Definition memmap.h:171

Iterate over memory regions.

Parameters
regionRegion descriptor
hideHide in-use regions from the memory map

Definition at line 184 of file memmap.h.

184#define for_each_memmap( region, hide ) \
185 for_each_memmap_from ( (region), 0, (hide) )

Referenced by lkrn_ram(), memmap_dump_all(), memmap_largest(), memmap_settings_fetch(), and multiboot_build_memmap().

◆ DBG_MEMMAP_IF

#define DBG_MEMMAP_IF ( level,
region )
Value:
do { \
const char *name = (region)->name; \
unsigned int flags = (region)->flags; \
\
DBG_IF ( level, "MEMMAP (%s%s%s%s) [%#08llx,%#08llx]%s%s\n", \
( ( flags & MEMMAP_FL_MEMORY ) ? "M" : "-" ), \
( ( flags & MEMMAP_FL_RESERVED ) ? "R" : "-" ), \
( ( flags & MEMMAP_FL_USED ) ? "U" : "-" ), \
( ( flags & MEMMAP_FL_INACCESSIBLE ) ? "X" : "-" ), \
( ( unsigned long long ) (region)->min ), \
( ( unsigned long long ) (region)->max ), \
( name ? " " : "" ), ( name ? name : "" ) ); \
} while ( 0 )
const char * name
Definition ath9k_hw.c:1986
uint8_t flags
Flags.
Definition ena.h:7
#define MEMMAP_FL_INACCESSIBLE
Outside of addressable range.
Definition memmap.h:63
#define MEMMAP_FL_USED
Is in use by iPXE.
Definition memmap.h:62
#define MEMMAP_FL_MEMORY
Contains memory.
Definition memmap.h:60
#define MEMMAP_FL_RESERVED
Is reserved.
Definition memmap.h:61

Definition at line 187 of file memmap.h.

187#define DBG_MEMMAP_IF( level, region ) do { \
188 const char *name = (region)->name; \
189 unsigned int flags = (region)->flags; \
190 \
191 DBG_IF ( level, "MEMMAP (%s%s%s%s) [%#08llx,%#08llx]%s%s\n", \
192 ( ( flags & MEMMAP_FL_MEMORY ) ? "M" : "-" ), \
193 ( ( flags & MEMMAP_FL_RESERVED ) ? "R" : "-" ), \
194 ( ( flags & MEMMAP_FL_USED ) ? "U" : "-" ), \
195 ( ( flags & MEMMAP_FL_INACCESSIBLE ) ? "X" : "-" ), \
196 ( ( unsigned long long ) (region)->min ), \
197 ( ( unsigned long long ) (region)->max ), \
198 ( name ? " " : "" ), ( name ? name : "" ) ); \
199 } while ( 0 )

◆ DBGC_MEMMAP_IF

#define DBGC_MEMMAP_IF ( level,
id,
... )
Value:
do { \
DBG_AC_IF ( level, id ); \
DBG_MEMMAP_IF ( level, __VA_ARGS__ ); \
DBG_DC_IF ( level ); \
} while ( 0 )

Definition at line 201 of file memmap.h.

201#define DBGC_MEMMAP_IF( level, id, ... ) do { \
202 DBG_AC_IF ( level, id ); \
203 DBG_MEMMAP_IF ( level, __VA_ARGS__ ); \
204 DBG_DC_IF ( level ); \
205 } while ( 0 )

◆ DBGC_MEMMAP

#define DBGC_MEMMAP ( ...)
Value:
DBGC_MEMMAP_IF ( LOG, ##__VA_ARGS__ )
#define DBGC_MEMMAP_IF(level, id,...)
Definition memmap.h:201

Definition at line 207 of file memmap.h.

Referenced by fdtmem_relocate(), lkrn_ram(), memmap_dump_all(), memmap_largest(), multiboot_build_memmap(), prep_segment(), and relocate().

◆ DBGC2_MEMMAP

#define DBGC2_MEMMAP ( ...)
Value:
DBGC_MEMMAP_IF ( EXTRA, ##__VA_ARGS__ )

Definition at line 208 of file memmap.h.

◆ DBGCP_MEMMAP

#define DBGCP_MEMMAP ( ...)
Value:
DBGC_MEMMAP_IF ( PROFILE, ##__VA_ARGS__ )

Definition at line 209 of file memmap.h.

Function Documentation

◆ FILE_LICENCE()

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL )

◆ FILE_SECBOOT()

FILE_SECBOOT ( PERMITTED )

◆ memmap_init()

void memmap_init ( uint64_t min,
struct memmap_region * region )
inlinestatic

Initialise memory region descriptor.

Parameters
minMinimum address
regionRegion descriptor to fill in

Definition at line 72 of file memmap.h.

72 {
73
74 region->min = min;
75 region->max = ~( ( uint64_t ) 0 );
76 region->flags = 0;
77 region->name = NULL;
78}
#define NULL
NULL pointer (VOID *)
Definition Base.h:322
unsigned long long uint64_t
Definition stdint.h:13
const char * name
Region name (for debug messages)
Definition memmap.h:57
uint64_t min
Minimum address in region.
Definition memmap.h:51
unsigned int flags
Region flags.
Definition memmap.h:55
uint64_t max
Maximum address in region.
Definition memmap.h:53

References memmap_region::flags, memmap_region::max, memmap_region::min, min, memmap_region::name, and NULL.

Referenced by fdtmem_describe(), int15_describe(), and memmap_describe().

◆ memmap_is_usable()

int memmap_is_usable ( const struct memmap_region * region)
inlinestatic

Check if memory region is usable.

Parameters
regionRegion descriptor
Return values
is_usableMemory region is usable

Definition at line 87 of file memmap.h.

87 {
88
89 return ( region->flags == MEMMAP_FL_MEMORY );
90}

References memmap_region::flags, and MEMMAP_FL_MEMORY.

Referenced by com32_exec_loop(), fdtmem_relocate(), memmap_largest(), prep_segment(), and relocate().

◆ memmap_size()

uint64_t memmap_size ( const struct memmap_region * region)
inlinestatic

Get remaining size of memory region (from the described address upwards)

Parameters
regionRegion descriptor
Return values
sizeSize of memory region

Definition at line 99 of file memmap.h.

99 {
100
101 /* Calculate size, assuming overflow is known to be impossible */
102 return ( region->max - region->min + 1 );
103}

References memmap_region::max, and memmap_region::min.

Referenced by com32_exec_loop(), lkrn_exec(), memmap_largest(), memmap_settings_fetch(), multiboot_build_memmap(), and relocate().

◆ memmap_describe()

void memmap_describe ( uint64_t min,
int hide,
struct memmap_region * region )

Describe memory region from system memory map.

Parameters
minMinimum address
hideHide in-use regions from the memory map
regionRegion descriptor to fill in

Definition at line 29 of file null_memmap.h.

30 {
31
32 /* Initialise region as empty */
33 memmap_init ( min, region );
34}
static void memmap_init(uint64_t min, struct memmap_region *region)
Initialise memory region descriptor.
Definition memmap.h:72

References __unused, memmap_describe(), memmap_init(), and min.

Referenced by com32_exec_loop(), memmap_describe(), memmap_dump_all(), prep_segment(), PROVIDE_MEMMAP(), PROVIDE_MEMMAP(), and PROVIDE_MEMMAP_INLINE().

◆ memmap_sync()

void memmap_sync ( void )

Synchronise in-use regions with the externally visible system memory map.

In environments such as x86 BIOS, we need to patch the global system memory map to hide our in-use regions, since there is no other way to communicate this information to external code.

Definition at line 26 of file fdtmem.h.

26 {
27 /* Nothing to do */
28}

References memmap_sync().

Referenced by memmap_sync(), memmap_use(), PROVIDE_MEMMAP(), PROVIDE_MEMMAP_INLINE(), and PROVIDE_MEMMAP_INLINE().

◆ memmap_use()

void memmap_use ( struct used_region * used,
physaddr_t start,
size_t size )
inlinestatic

Update an in-use memory region.

Parameters
usedIn-use memory region
startStart address
sizeLength of region

Definition at line 154 of file memmap.h.

154 {
155
156 /* Record region */
157 used->start = start;
158 used->size = size;
159
160 /* Synchronise externally visible memory map */
161 memmap_sync();
162}
uint16_t size
Buffer size.
Definition dwmac.h:3
void memmap_sync(void)
Synchronise in-use regions with the externally visible system memory map.
Definition fdtmem.h:26
size_t size
Length of region.
Definition memmap.h:112
physaddr_t start
Start address.
Definition memmap.h:110

References memmap_sync(), size, used_region::size, start, and used_region::start.

Referenced by fdtmem_register(), and uheap_resize().

◆ memmap_dump_all()

void memmap_dump_all ( int hide)
inlinestatic

Dump system memory map (for debugging)

Parameters
hideHide in-use regions from the memory map

Definition at line 216 of file memmap.h.

216 {
217 struct memmap_region region;
218
219 /* Do nothing unless debugging is enabled */
220 if ( ! DBG_LOG )
221 return;
222
223 /* Describe all memory regions */
224 DBGC ( &memmap_describe, "MEMMAP with in-use regions %s:\n",
225 ( hide ? "hidden" : "ignored" ) );
226 for_each_memmap ( &region, hide )
227 DBGC_MEMMAP ( &memmap_describe, &region );
228}
#define DBGC(...)
Definition compiler.h:505
#define DBG_LOG
Definition compiler.h:317
#define for_each_memmap(region, hide)
Iterate over memory regions.
Definition memmap.h:184
#define DBGC_MEMMAP(...)
Definition memmap.h:207
A memory region descriptor.
Definition memmap.h:49

References DBG_LOG, DBGC, DBGC_MEMMAP, for_each_memmap, and memmap_describe().

Referenced by fdtmem_register(), hide_etherboot(), int13_boot(), relocate(), uheap_resize(), and unhide_etherboot().

◆ memmap_update()

void memmap_update ( struct memmap_region * region,
uint64_t start,
uint64_t size,
unsigned int flags,
const char * name )
extern

Update memory region descriptor.

Parameters
regionMemory region of interest to be updated
startStart address of known region
sizeSize of known region
flagsFlags for known region
nameName of known region (for debugging)

Update a memory region descriptor based on a known existent region.

Definition at line 47 of file memmap.c.

48 {
50
51 /* Sanity check */
52 assert ( region->max >= region->min );
53
54 /* Ignore empty regions */
55 if ( ! size )
56 return;
57
58 /* Calculate max addresses (and truncate if necessary) */
59 max = ( start + size - 1 );
60 if ( max < start ) {
61 max = ~( ( uint64_t ) 0 );
62 DBGC ( region, "MEMMAP [%#08llx,%#08llx] %s truncated "
63 "(invalid size %#08llx)\n",
64 ( ( unsigned long long ) start ),
65 ( ( unsigned long long ) max ), name,
66 ( ( unsigned long long ) size ) );
67 }
68
69 /* Ignore regions entirely below the region of interest */
71 return;
72
73 /* Ignore regions entirely above the region of interest */
74 if ( start > region->max )
75 return;
76
77 /* Update region of interest as applicable */
79
80 /* Record this region as covering the region of interest */
81 region->flags |= flags;
82 if ( name )
83 region->name = name;
84
85 /* Update max address if no closer boundary exists */
87 region->max = max;
88
89 } else if ( start < region->max ) {
90
91 /* Update max address if no closer boundary exists */
92 region->max = ( start - 1 );
93 }
94
95 /* Sanity check */
96 assert ( region->max >= region->min );
97}
#define assert(condition)
Assert a condition at run-time.
Definition assert.h:50

References assert, DBGC, flags, memmap_region::flags, max, memmap_region::max, memmap_region::min, min, memmap_region::name, name, size, and start.

Referenced by fdtmem_describe(), fdtmem_relocate(), fdtmem_update_node(), fdtmem_update_tree(), int15_describe(), meme820(), and memmap_update_used().

◆ memmap_update_used()

void memmap_update_used ( struct memmap_region * region)
extern

Update memory region descriptor based on all in-use memory regions.

Parameters
regionMemory region of interest to be updated

Definition at line 104 of file memmap.c.

104 {
105 struct used_region *used;
106
107 /* Update descriptor to hide all in-use regions */
109 memmap_update ( region, used->start, used->size,
110 MEMMAP_FL_USED, used->name );
111 }
112}
#define USED_REGIONS
In-use memory region table.
Definition memmap.h:116
void memmap_update(struct memmap_region *region, uint64_t start, uint64_t size, unsigned int flags, const char *name)
Update memory region descriptor.
Definition memmap.c:47
An in-use memory region.
Definition memmap.h:106
const char * name
Region name.
Definition memmap.h:108
#define for_each_table_entry(pointer, table)
Iterate through all entries within a linker table.
Definition tables.h:386

References for_each_table_entry, MEMMAP_FL_USED, memmap_update(), used_region::name, used_region::size, used_region::start, and USED_REGIONS.

Referenced by fdtmem_describe_region().

◆ memmap_largest()

size_t memmap_largest ( physaddr_t * start)
extern

Find largest usable memory region.

Parameters
startStart address to fill in
Return values
lenLength of region

Definition at line 120 of file memmap.c.

120 {
121 struct memmap_region region;
122 size_t largest;
123 size_t size;
124
125 /* Find largest usable region */
126 DBGC ( &region, "MEMMAP finding largest usable region\n" );
127 *start = 0;
128 largest = 0;
129 for_each_memmap ( &region, 1 ) {
130 DBGC_MEMMAP ( &region, &region );
131 if ( ! memmap_is_usable ( &region ) )
132 continue;
133 size = memmap_size ( &region );
134 if ( size > largest ) {
135 DBGC ( &region, "...new largest region found\n" );
136 largest = size;
137 *start = region.min;
138 }
139 }
140 return largest;
141}
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

References DBGC, DBGC_MEMMAP, for_each_memmap, memmap_is_usable(), memmap_size(), memmap_region::min, size, and start.

Referenced by uheap_find().