--- /dev/null
+/*
+ * Copyright (C) 2025 Michael Brown <mbrown@fensystems.co.uk>.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ * 02110-1301, USA.
+ *
+ * You can also choose to distribute this program under the terms of
+ * the Unmodified Binary Distribution Licence (as given in the file
+ * COPYING.UBDL), provided that you have satisfied its requirements.
+ */
+
+FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
+
+#include <assert.h>
+#include <ipxe/memmap.h>
+
+/** @file
+ *
+ * System memory map
+ *
+ */
+
+/**
+ * Update memory region descriptor
+ *
+ * @v region Memory region of interest to be updated
+ * @v start Start address of known region
+ * @v size Size of known region
+ * @v flags Flags for known region
+ * @v name Name of known region (for debugging)
+ *
+ * Update a memory region descriptor based on a known existent region.
+ */
+void memmap_update ( struct memmap_region *region, uint64_t start,
+ uint64_t size, unsigned int flags, const char *name ) {
+ uint64_t last;
+
+ /* Sanity check */
+ assert ( region->last >= region->addr );
+
+ /* Ignore empty regions */
+ if ( ! size )
+ return;
+
+ /* Calculate last addresses (and truncate if necessary) */
+ last = ( start + size - 1 );
+ if ( last < start ) {
+ last = ~( ( uint64_t ) 0 );
+ DBGC ( region, "MEMMAP [%#08llx,%#08llx] %s truncated "
+ "(invalid size %#08llx)\n",
+ ( ( unsigned long long ) start ),
+ ( ( unsigned long long ) last ), name,
+ ( ( unsigned long long ) size ) );
+ }
+
+ /* Ignore regions entirely below the region of interest */
+ if ( last < region->addr )
+ return;
+
+ /* Ignore regions entirely above the region of interest */
+ if ( start > region->last )
+ return;
+
+ /* Update region of interest as applicable */
+ if ( start <= region->addr ) {
+
+ /* Record this region as covering the region of interest */
+ region->flags |= flags;
+ if ( name )
+ region->name = name;
+
+ /* Update last address if no closer boundary exists */
+ if ( last < region->last )
+ region->last = last;
+
+ } else if ( start < region->last ) {
+
+ /* Update last address if no closer boundary exists */
+ region->last = ( start - 1 );
+ }
+
+ /* Sanity check */
+ assert ( region->last >= region->addr );
+}
+
+/**
+ * Update memory region descriptor based on all in-use memory regions
+ *
+ * @v region Memory region of interest to be updated
+ */
+void memmap_update_used ( struct memmap_region *region ) {
+ struct used_region *used;
+
+ /* Update descriptor to hide all in-use regions */
+ for_each_table_entry ( used, USED_REGIONS ) {
+ memmap_update ( region, used->start, used->size,
+ MEMMAP_FL_USED, used->name );
+ }
+}
+
+PROVIDE_MEMMAP_INLINE ( null, memmap_describe );
+PROVIDE_MEMMAP_INLINE ( null, memmap_sync );
--- /dev/null
+#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 ( ®ion, "MEMMAP with in-use regions %s:\n",
+ ( hide ? "hidden" : "ignored" ) );
+ for_each_memmap ( ®ion, hide )
+ memmap_dump ( ®ion );
+}
+
+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 */