iPXE
Data Structures | Macros | Functions
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)   SINGLE_API_INLINE ( MEMMAP_PREFIX_ ## _subsys, _api_func )
 Calculate static inline memory map API function name. More...
 
#define PROVIDE_MEMMAP(_subsys, _api_func, _func)   PROVIDE_SINGLE_API ( MEMMAP_PREFIX_ ## _subsys, _api_func, _func )
 Provide a memory map API implementation. More...
 
#define PROVIDE_MEMMAP_INLINE(_subsys, _api_func)   PROVIDE_SINGLE_API_INLINE ( MEMMAP_PREFIX_ ## _subsys, _api_func )
 Provide a static inline memory map API implementation. More...
 
#define MEMMAP_FL_MEMORY   0x0001
 Contains memory. More...
 
#define MEMMAP_FL_RESERVED   0x0002
 Is reserved. More...
 
#define MEMMAP_FL_USED   0x0004
 Is in use by iPXE. More...
 
#define MEMMAP_FL_INACCESSIBLE   0x0008
 Outside of addressable range. More...
 
#define USED_REGIONS   __table ( struct used_region, "used_regions" )
 In-use memory region table. More...
 
#define __used_region   __table_entry ( USED_REGIONS, 01 )
 Declare an in-use memory region. More...
 
#define for_each_memmap_from(region, start, hide)
 Iterate over memory regions from a given starting address. More...
 
#define for_each_memmap(region, hide)   for_each_memmap_from ( (region), 0, (hide) )
 Iterate over memory regions. More...
 
#define DBG_MEMMAP_IF(level, region)
 
#define DBGC_MEMMAP_IF(level, id, ...)
 
#define DBGC_MEMMAP(...)   DBGC_MEMMAP_IF ( LOG, ##__VA_ARGS__ )
 
#define DBGC2_MEMMAP(...)   DBGC_MEMMAP_IF ( EXTRA, ##__VA_ARGS__ )
 
#define DBGCP_MEMMAP(...)   DBGC_MEMMAP_IF ( PROFILE, ##__VA_ARGS__ )
 

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

Detailed Description

System memory map.

Definition in file memmap.h.

Macro Definition Documentation

◆ MEMMAP_INLINE

#define MEMMAP_INLINE (   _subsys,
  _api_func 
)    SINGLE_API_INLINE ( MEMMAP_PREFIX_ ## _subsys, _api_func )

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.

◆ PROVIDE_MEMMAP

#define PROVIDE_MEMMAP (   _subsys,
  _api_func,
  _func 
)    PROVIDE_SINGLE_API ( MEMMAP_PREFIX_ ## _subsys, _api_func, _func )

Provide a memory map API implementation.

Parameters
_prefixSubsystem prefix
_api_funcAPI function
_funcImplementing function

Definition at line 36 of file memmap.h.

◆ PROVIDE_MEMMAP_INLINE

#define PROVIDE_MEMMAP_INLINE (   _subsys,
  _api_func 
)    PROVIDE_SINGLE_API_INLINE ( MEMMAP_PREFIX_ ## _subsys, _api_func )

Provide a static inline memory map API implementation.

Parameters
_prefixSubsystem prefix
_api_funcAPI function

Definition at line 45 of file memmap.h.

◆ MEMMAP_FL_MEMORY

#define MEMMAP_FL_MEMORY   0x0001

Contains memory.

Definition at line 60 of file memmap.h.

◆ MEMMAP_FL_RESERVED

#define MEMMAP_FL_RESERVED   0x0002

Is reserved.

Definition at line 61 of file memmap.h.

◆ MEMMAP_FL_USED

#define MEMMAP_FL_USED   0x0004

Is in use by iPXE.

Definition at line 62 of file memmap.h.

◆ MEMMAP_FL_INACCESSIBLE

#define MEMMAP_FL_INACCESSIBLE   0x0008

Outside of addressable range.

Definition at line 63 of file memmap.h.

◆ USED_REGIONS

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

In-use memory region table.

Definition at line 116 of file memmap.h.

◆ __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 max(x, y)
Definition: ath.h:41
#define min(x, y)
Definition: ath.h:36
uint32_t start
Starting offset.
Definition: netvsc.h:12
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.

◆ for_each_memmap

#define for_each_memmap (   region,
  hide 
)    for_each_memmap_from ( (region), 0, (hide) )

Iterate over memory regions.

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

Definition at line 184 of file memmap.h.

◆ 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
#define max(x, y)
Definition: ath.h:41
#define DBG_IF(level,...)
Print debugging message if we are at a certain debug level.
Definition: compiler.h:331
#define min(x, y)
Definition: ath.h:36
#define MEMMAP_FL_USED
Is in use by iPXE.
Definition: memmap.h:62
uint8_t flags
Flags.
Definition: ena.h:18
#define MEMMAP_FL_RESERVED
Is reserved.
Definition: memmap.h:61
#define MEMMAP_FL_MEMORY
Contains memory.
Definition: memmap.h:60
#define MEMMAP_FL_INACCESSIBLE
Outside of addressable range.
Definition: memmap.h:63

Definition at line 187 of file memmap.h.

◆ 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.

◆ DBGC_MEMMAP

#define DBGC_MEMMAP (   ...)    DBGC_MEMMAP_IF ( LOG, ##__VA_ARGS__ )

Definition at line 207 of file memmap.h.

◆ DBGC2_MEMMAP

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

Definition at line 208 of file memmap.h.

◆ DBGCP_MEMMAP

#define DBGCP_MEMMAP (   ...)    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()

static 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 }
uint64_t max
Maximum address in region.
Definition: memmap.h:53
#define min(x, y)
Definition: ath.h:36
unsigned long long uint64_t
Definition: stdint.h:13
unsigned int flags
Region flags.
Definition: memmap.h:55
const char * name
Region name (for debug messages)
Definition: memmap.h:57
uint64_t min
Minimum address in region.
Definition: memmap.h:51
#define NULL
NULL pointer (VOID *)
Definition: Base.h:322

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

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

◆ memmap_is_usable()

static 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 }
unsigned int flags
Region flags.
Definition: memmap.h:55
#define MEMMAP_FL_MEMORY
Contains memory.
Definition: memmap.h:60

References memmap_region::flags, and MEMMAP_FL_MEMORY.

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

◆ memmap_size()

static 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 }
uint64_t max
Maximum address in region.
Definition: memmap.h:53
uint64_t min
Minimum address in region.
Definition: memmap.h:51

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 }
#define min(x, y)
Definition: ath.h:36
static void memmap_init(uint64_t min, struct memmap_region *region)
Initialise memory region descriptor.
Definition: memmap.h:72

References memmap_init(), and min.

Referenced by com32_exec_loop(), memmap_dump_all(), and prep_segment().

◆ 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 }

Referenced by memmap_use().

◆ memmap_use()

static 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 }
size_t size
Length of region.
Definition: memmap.h:112
uint16_t size
Buffer size.
Definition: dwmac.h:14
uint32_t start
Starting offset.
Definition: netvsc.h:12
physaddr_t start
Start address.
Definition: memmap.h:110
void memmap_sync(void)
Synchronise in-use regions with the externally visible system memory map.
Definition: fdtmem.h:26

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

Referenced by fdtmem_register(), and uheap_resize().

◆ memmap_dump_all()

static 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 DBGC_MEMMAP(...)
Definition: memmap.h:207
void memmap_describe(uint64_t min, int hide, struct memmap_region *region)
Describe memory region from system memory map.
Definition: null_memmap.h:29
#define for_each_memmap(region, hide)
Iterate over memory regions.
Definition: memmap.h:184
A memory region descriptor.
Definition: memmap.h:49
#define DBG_LOG
Definition: compiler.h:317

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 
)

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  {
49  uint64_t max;
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 */
70  if ( max < region->min )
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 */
78  if ( start <= region->min ) {
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 */
86  if ( max < region->max )
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 }
const char * name
Definition: ath9k_hw.c:1986
#define max(x, y)
Definition: ath.h:41
uint64_t max
Maximum address in region.
Definition: memmap.h:53
uint16_t size
Buffer size.
Definition: dwmac.h:14
#define DBGC(...)
Definition: compiler.h:505
#define min(x, y)
Definition: ath.h:36
unsigned long long uint64_t
Definition: stdint.h:13
uint32_t start
Starting offset.
Definition: netvsc.h:12
assert((readw(&hdr->flags) &(GTF_reading|GTF_writing))==0)
uint8_t flags
Flags.
Definition: ena.h:18
unsigned int flags
Region flags.
Definition: memmap.h:55
const char * name
Region name (for debug messages)
Definition: memmap.h:57
uint64_t min
Minimum address in region.
Definition: memmap.h:51

References assert(), DBGC, flags, memmap_region::flags, max, memmap_region::max, min, memmap_region::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)

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 }
size_t size
Length of region.
Definition: memmap.h:112
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
#define USED_REGIONS
In-use memory region table.
Definition: memmap.h:116
#define MEMMAP_FL_USED
Is in use by iPXE.
Definition: memmap.h:62
physaddr_t start
Start address.
Definition: memmap.h:110
An in-use memory region.
Definition: memmap.h:106
#define for_each_table_entry(pointer, table)
Iterate through all entries within a linker table.
Definition: tables.h:386
const char * name
Region name.
Definition: memmap.h:108

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)

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
uint16_t size
Buffer size.
Definition: dwmac.h:14
#define DBGC(...)
Definition: compiler.h:505
#define DBGC_MEMMAP(...)
Definition: memmap.h:207
uint32_t start
Starting offset.
Definition: netvsc.h:12
#define for_each_memmap(region, hide)
Iterate over memory regions.
Definition: memmap.h:184
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
A memory region descriptor.
Definition: memmap.h:49

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

Referenced by uheap_find().