[memmap] Define an API for managing the system memory map

Define a generic system memory map API, based on the abstraction
created for parsing the FDT memory map and adding a concept of hidden
in-use memory regions as required to support patching the BIOS INT 15
memory map.

Signed-off-by: Michael Brown <mcb30@ipxe.org>
This commit is contained in:
Michael Brown
2025-05-14 22:19:54 +01:00
parent f6f11c101c
commit bab3d76717
8 changed files with 412 additions and 0 deletions

15
src/include/bits/memmap.h Normal file
View File

@@ -0,0 +1,15 @@
#ifndef _BITS_MEMMAP_H
#define _BITS_MEMMAP_H
/** @file
*
* Dummy architecture-specific system memory map
*
* This file is included only if the architecture does not provide its
* own version of this file.
*
*/
FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
#endif /* _BITS_MEMMAP_H */

235
src/include/ipxe/memmap.h Normal file
View File

@@ -0,0 +1,235 @@
#ifndef _IPXE_MEMMAP_H
#define _IPXE_MEMMAP_H
/** @file
*
* System memory map
*
*/
FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
#include <stddef.h>
#include <stdint.h>
#include <ipxe/api.h>
#include <ipxe/tables.h>
#include <config/ioapi.h>
/**
* Calculate static inline memory map API function name
*
* @v _prefix Subsystem prefix
* @v _api_func API function
* @ret _subsys_func Subsystem API function
*/
#define MEMMAP_INLINE( _subsys, _api_func ) \
SINGLE_API_INLINE ( MEMMAP_PREFIX_ ## _subsys, _api_func )
/**
* Provide a memory map API implementation
*
* @v _prefix Subsystem prefix
* @v _api_func API function
* @v _func Implementing function
*/
#define PROVIDE_MEMMAP( _subsys, _api_func, _func ) \
PROVIDE_SINGLE_API ( MEMMAP_PREFIX_ ## _subsys, _api_func, _func )
/**
* Provide a static inline memory map API implementation
*
* @v _prefix Subsystem prefix
* @v _api_func API function
*/
#define PROVIDE_MEMMAP_INLINE( _subsys, _api_func ) \
PROVIDE_SINGLE_API_INLINE ( MEMMAP_PREFIX_ ## _subsys, _api_func )
/** A memory region descriptor */
struct memmap_region {
/** The address being described */
uint64_t addr;
/** Last address with the same description
*
* Note that this is the last address with the same
* description as the address being described, not the first
* address with a different description.
*/
uint64_t last;
/** Region flags */
unsigned int flags;
/** Region name (for debug messages) */
const char *name;
};
#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 */
/**
* Initialise memory region descriptor
*
* @v addr Address within region
* @v region Region descriptor to fill in
*/
static inline __attribute__ (( always_inline )) void
memmap_init ( uint64_t addr, struct memmap_region *region ) {
region->addr = addr;
region->last = ~( ( uint64_t ) 0 );
region->flags = 0;
region->name = NULL;
}
/**
* Check if memory region is usable
*
* @v region Region descriptor
* @ret is_usable Memory region is usable
*/
static inline __attribute__ (( always_inline )) int
memmap_is_usable ( const struct memmap_region *region ) {
return ( region->flags == MEMMAP_FL_MEMORY );
}
/**
* Get remaining size of memory region (from the described address upwards)
*
* @v region Region descriptor
* @ret size Size of memory region
*/
static inline __attribute__ (( always_inline )) uint64_t
memmap_size ( const struct memmap_region *region ) {
/* Calculate size, assuming overflow is known to be impossible */
return ( ( region->last + 1 ) - region->addr );
}
/** An in-use memory region */
struct used_region {
/** Region name */
const char *name;
/** Start address */
physaddr_t start;
/** Length of region */
size_t size;
};
/** In-use memory region table */
#define USED_REGIONS __table ( struct used_region, "used_regions" )
/** Declare an in-use memory region */
#define __used_region __table_entry ( USED_REGIONS, 01 )
/* Include all architecture-independent ACPI API headers */
#include <ipxe/null_memmap.h>
/* Include all architecture-dependent ACPI API headers */
#include <bits/memmap.h>
/**
* Describe memory region from system memory map
*
* @v addr Address within region
* @v hide Hide in-use regions from the memory map
* @v region Region descriptor to fill in
*/
void memmap_describe ( uint64_t addr, int hide, struct memmap_region *region );
/**
* 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.
*/
void memmap_sync ( void );
/**
* Update an in-use memory region
*
* @v used In-use memory region
* @v start Start address
* @v size Length of region
*/
static inline __attribute__ (( always_inline )) void
memmap_use ( struct used_region *used, physaddr_t start, size_t size ) {
/* Record region */
used->start = start;
used->size = size;
/* Synchronise externally visible memory map */
memmap_sync();
}
/**
* Iterate over memory regions from a given starting address
*
* @v region Region descriptor
* @v start Starting address
* @v hide Hide in-use regions from the memory map
*/
#define for_each_memmap_from( region, start, hide ) \
for ( (region)->addr = (start), (region)->last = 0 ; \
( ( ( (region)->last + 1 ) != 0 ) && \
( memmap_describe ( (region)->addr, (hide), \
(region) ), 1 ) ) ; \
(region)->addr = ( (region)->last + 1 ) )
/**
* Iterate over memory regions
*
* @v region Region descriptor
* @v hide Hide in-use regions from the memory map
*/
#define for_each_memmap( region, hide ) \
for_each_memmap_from ( (region), 0, (hide) )
/**
* Dump memory region descriptor (for debugging)
*
* @v region Region descriptor
*/
static inline void memmap_dump ( const struct memmap_region *region ) {
const char *name = region->name;
unsigned int flags = region->flags;
/* Dump region information */
DBGC ( region, "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->addr ),
( ( unsigned long long ) region->last ),
( name ? " " : "" ), ( name ? name : "" ) );
}
/**
* Dump system memory map (for debugging)
*
* @v hide Hide in-use regions from the memory map
*/
static inline void memmap_dump_all ( int hide ) {
struct memmap_region region;
/* Do nothing unless debugging is enabled */
if ( ! DBG_LOG )
return;
/* Describe all memory regions */
DBGC ( &region, "MEMMAP with in-use regions %s:\n",
( hide ? "hidden" : "ignored" ) );
for_each_memmap ( &region, hide )
memmap_dump ( &region );
}
extern void memmap_update ( struct memmap_region *region, uint64_t start,
uint64_t size, unsigned int flags,
const char *name );
extern void memmap_update_used ( struct memmap_region *region );
#endif /* _IPXE_MEMMAP_H */

View File

@@ -0,0 +1,44 @@
#ifndef _IPXE_NULL_MEMMAP_H
#define _IPXE_NULL_MEMMAP_H
#include <stdint.h>
/** @file
*
* Null system memory map API
*
*/
FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
#ifdef MEMMAP_NULL
#define MEMMAP_PREFIX_null
#else
#define MEMMAP_PREFIX_null __null_
#endif
/**
* Describe memory region from system memory map
*
* @v addr Address within region
* @v hide Hide in-use regions from the memory map
* @v region Region descriptor to fill in
*/
static inline __attribute__ (( always_inline )) void
MEMMAP_INLINE ( null, memmap_describe ) ( uint64_t addr, int hide __unused,
struct memmap_region *region ) {
/* Initialise region as empty */
memmap_init ( addr, region );
}
/**
* Synchronise in-use regions with the externally visible system memory map
*
*/
static inline __attribute__ (( always_inline )) void
MEMMAP_INLINE ( null, memmap_sync ) ( void ) {
/* Nothing to do */
}
#endif /* _IPXE_NULL_MEMMAP_H */