iPXE
Data Structures | Defines | Functions | Variables
memmap.c File Reference

Memory mapping. More...

#include <stdint.h>
#include <errno.h>
#include <realmode.h>
#include <bios.h>
#include <memsizes.h>
#include <ipxe/io.h>

Go to the source code of this file.

Data Structures

struct  e820_entry
 An INT 15,e820 memory map entry. More...

Defines

#define SMAP   ( 0x534d4150 )
 Magic value for INT 15,e820 calls.
#define E820_TYPE_RAM   1
 Normal memory.
#define E820_TYPE_RESERVED   2
 Reserved and unavailable.
#define E820_TYPE_ACPI   3
 ACPI reclaim memory.
#define E820_TYPE_NVS   4
 ACPI NVS memory.
#define E820_ATTR_ENABLED   0x00000001UL
#define E820_ATTR_NONVOLATILE   0x00000002UL
#define E820_ATTR_UNKNOWN   0xfffffffcUL
#define E820_MIN_SIZE   20
#define e820buf   __use_data16 ( e820buf )
#define memmap_post   __use_data16 ( memmap_post )

Functions

 FILE_LICENCE (GPL2_OR_LATER_OR_UBDL)
struct e820_entry __attribute__ ((packed))
static struct e820_entry __bss16 (e820buf)
 Buffer for INT 15,e820 calls.
uint8_t __bss16 (memmap_post)
 We are running during POST; inhibit INT 15,e820 and INT 15,e801.
static unsigned int extmemsize_e801 (void)
 Get size of extended memory via INT 15,e801.
static unsigned int extmemsize_88 (void)
 Get size of extended memory via INT 15,88.
unsigned int extmemsize (void)
 Get size of extended memory.
static int meme820 (struct memory_map *memmap)
 Get e820 memory map.
void x86_get_memmap (struct memory_map *memmap)
 Get memory map.
 PROVIDE_IOAPI (x86, get_memmap, x86_get_memmap)

Variables

uint64_t start
 Start of region.
uint64_t len
 Length of region.
uint32_t type
 Type of region.
uint32_t attrs
 Extended attributes (optional)

Detailed Description

Memory mapping.

Definition in file memmap.c.


Define Documentation

#define SMAP   ( 0x534d4150 )

Magic value for INT 15,e820 calls.

Definition at line 41 of file memmap.c.

Referenced by meme820().

#define E820_TYPE_RAM   1

Normal memory.

Definition at line 55 of file memmap.c.

Referenced by meme820().

#define E820_TYPE_RESERVED   2

Reserved and unavailable.

Definition at line 56 of file memmap.c.

#define E820_TYPE_ACPI   3

ACPI reclaim memory.

Definition at line 57 of file memmap.c.

#define E820_TYPE_NVS   4

ACPI NVS memory.

Definition at line 58 of file memmap.c.

#define E820_ATTR_ENABLED   0x00000001UL

Definition at line 60 of file memmap.c.

Referenced by meme820().

#define E820_ATTR_NONVOLATILE   0x00000002UL

Definition at line 61 of file memmap.c.

Referenced by meme820().

#define E820_ATTR_UNKNOWN   0xfffffffcUL

Definition at line 62 of file memmap.c.

Referenced by meme820().

#define E820_MIN_SIZE   20

Definition at line 64 of file memmap.c.

Referenced by meme820().

#define e820buf   __use_data16 ( e820buf )

Definition at line 68 of file memmap.c.

Referenced by meme820().

Definition at line 72 of file memmap.c.

Referenced by extmemsize_e801(), and meme820().


Function Documentation

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL  )
struct e820_entry __attribute__ ( (packed)  )
static struct e820_entry __bss16 ( e820buf  ) [static, read]

Buffer for INT 15,e820 calls.

We are running during POST; inhibit INT 15,e820 and INT 15,e801.

static unsigned int extmemsize_e801 ( void  ) [static]

Get size of extended memory via INT 15,e801.

Return values:
extmemExtended memory size, in kB, or 0

Definition at line 79 of file memmap.c.

References __asm__(), CF, DBG, flags, memmap_post, and REAL_CODE.

Referenced by extmemsize().

                                             {
        uint16_t extmem_1m_to_16m_k, extmem_16m_plus_64k;
        uint16_t confmem_1m_to_16m_k, confmem_16m_plus_64k;
        unsigned int flags;
        unsigned int extmem;

        /* Inhibit INT 15,e801 during POST */
        if ( memmap_post ) {
                DBG ( "INT 15,e801 not available during POST\n" );
                return 0;
        }

        __asm__ __volatile__ ( REAL_CODE ( "stc\n\t"
                                           "int $0x15\n\t"
                                           "pushfw\n\t"
                                           "popw %w0\n\t" )
                               : "=R" ( flags ),
                                 "=a" ( extmem_1m_to_16m_k ),
                                 "=b" ( extmem_16m_plus_64k ),
                                 "=c" ( confmem_1m_to_16m_k ),
                                 "=d" ( confmem_16m_plus_64k )
                               : "a" ( 0xe801 ) );

        if ( flags & CF ) {
                DBG ( "INT 15,e801 failed with CF set\n" );
                return 0;
        }

        if ( ! ( extmem_1m_to_16m_k | extmem_16m_plus_64k ) ) {
                DBG ( "INT 15,e801 extmem=0, using confmem\n" );
                extmem_1m_to_16m_k = confmem_1m_to_16m_k;
                extmem_16m_plus_64k = confmem_16m_plus_64k;
        }

        extmem = ( extmem_1m_to_16m_k + ( extmem_16m_plus_64k * 64 ) );
        DBG ( "INT 15,e801 extended memory size %d+64*%d=%d kB "
              "[100000,%llx)\n", extmem_1m_to_16m_k, extmem_16m_plus_64k,
              extmem, ( 0x100000 + ( ( ( uint64_t ) extmem ) * 1024 ) ) );

        /* Sanity check.  Some BIOSes report the entire 4GB address
         * space as available, which cannot be correct (since that
         * would leave no address space available for 32-bit PCI
         * BARs).
         */
        if ( extmem == ( 0x400000 - 0x400 ) ) {
                DBG ( "INT 15,e801 reported whole 4GB; assuming insane\n" );
                return 0;
        }

        return extmem;
}
static unsigned int extmemsize_88 ( void  ) [static]

Get size of extended memory via INT 15,88.

Return values:
extmemExtended memory size, in kB

Definition at line 136 of file memmap.c.

References __asm__(), DBG, and REAL_CODE.

Referenced by extmemsize().

                                           {
        uint16_t extmem;

        /* Ignore CF; it is not reliable for this call */
        __asm__ __volatile__ ( REAL_CODE ( "int $0x15" )
                               : "=a" ( extmem ) : "a" ( 0x8800 ) );

        DBG ( "INT 15,88 extended memory size %d kB [100000, %x)\n",
              extmem, ( 0x100000 + ( extmem * 1024 ) ) );
        return extmem;
}
unsigned int extmemsize ( void  )

Get size of extended memory.

Return values:
extmemExtended memory size, in kB

Note that this is only an approximation; for an accurate picture, use the E820 memory map obtained via get_memmap();

Definition at line 156 of file memmap.c.

References extmemsize_88(), and extmemsize_e801().

Referenced by nbi_process_segments(), and x86_get_memmap().

                                 {
        unsigned int extmem_e801;
        unsigned int extmem_88;

        /* Try INT 15,e801 first, then fall back to INT 15,88 */
        extmem_88 = extmemsize_88();
        extmem_e801 = extmemsize_e801();
        return ( extmem_e801 ? extmem_e801 : extmem_88 );
}
static int meme820 ( struct memory_map memmap) [static]

Get e820 memory map.

Parameters:
memmapMemory map to fill in
Return values:
rcReturn status code

Definition at line 172 of file memmap.c.

References __asm__(), __from_data16, attrs, CF, memory_map::count, DBG, E820_ATTR_ENABLED, E820_ATTR_NONVOLATILE, E820_ATTR_UNKNOWN, E820_MIN_SIZE, E820_TYPE_RAM, e820buf, EINVAL, memory_region::end, ENOTSUP, ENOTTY, flags, memmap_post, memset(), next, NULL, offsetof, REAL_CODE, memory_map::regions, size, SMAP, and memory_region::start.

Referenced by x86_get_memmap().

                                                 {
        struct memory_region *region = memmap->regions;
        struct memory_region *prev_region = NULL;
        uint32_t next = 0;
        uint32_t smap;
        uint32_t size;
        unsigned int flags;
        unsigned int discard_D;

        /* Inhibit INT 15,e820 during POST */
        if ( memmap_post ) {
                DBG ( "INT 15,e820 not available during POST\n" );
                return -ENOTTY;
        }

        /* Clear the E820 buffer.  Do this once before starting,
         * rather than on each call; some BIOSes rely on the contents
         * being preserved between calls.
         */
        memset ( &e820buf, 0, sizeof ( e820buf ) );

        do {
                /* Some BIOSes corrupt %esi for fun. Guard against
                 * this by telling gcc that all non-output registers
                 * may be corrupted.
                 */
                __asm__ __volatile__ ( REAL_CODE ( "pushl %%ebp\n\t"
                                                   "stc\n\t"
                                                   "int $0x15\n\t"
                                                   "pushfw\n\t"
                                                   "popw %%dx\n\t"
                                                   "popl %%ebp\n\t" )
                                       : "=a" ( smap ), "=b" ( next ),
                                         "=c" ( size ), "=d" ( flags ),
                                         "=D" ( discard_D )
                                       : "a" ( 0xe820 ), "b" ( next ),
                                         "D" ( __from_data16 ( &e820buf ) ),
                                         "c" ( sizeof ( e820buf ) ),
                                         "d" ( SMAP )
                                       : "esi", "memory" );

                if ( smap != SMAP ) {
                        DBG ( "INT 15,e820 failed SMAP signature check\n" );
                        return -ENOTSUP;
                }

                if ( size < E820_MIN_SIZE ) {
                        DBG ( "INT 15,e820 returned only %d bytes\n", size );
                        return -EINVAL;
                }

                if ( flags & CF ) {
                        DBG ( "INT 15,e820 terminated on CF set\n" );
                        break;
                }

                /* If first region is not RAM, assume map is invalid */
                if ( ( memmap->count == 0 ) &&
                     ( e820buf.type != E820_TYPE_RAM ) ) {
                       DBG ( "INT 15,e820 failed, first entry not RAM\n" );
                       return -EINVAL;
                }

                DBG ( "INT 15,e820 region [%llx,%llx) type %d",
                      e820buf.start, ( e820buf.start + e820buf.len ),
                      ( int ) e820buf.type );
                if ( size > offsetof ( typeof ( e820buf ), attrs ) ) {
                        DBG ( " (%s", ( ( e820buf.attrs & E820_ATTR_ENABLED )
                                        ? "enabled" : "disabled" ) );
                        if ( e820buf.attrs & E820_ATTR_NONVOLATILE )
                                DBG ( ", non-volatile" );
                        if ( e820buf.attrs & E820_ATTR_UNKNOWN )
                                DBG ( ", other [%08x]", e820buf.attrs );
                        DBG ( ")" );
                }
                DBG ( "\n" );

                /* Discard non-RAM regions */
                if ( e820buf.type != E820_TYPE_RAM )
                        continue;

                /* Check extended attributes, if present */
                if ( size > offsetof ( typeof ( e820buf ), attrs ) ) {
                        if ( ! ( e820buf.attrs & E820_ATTR_ENABLED ) )
                                continue;
                        if ( e820buf.attrs & E820_ATTR_NONVOLATILE )
                                continue;
                }

                region->start = e820buf.start;
                region->end = e820buf.start + e820buf.len;

                /* Check for adjacent regions and merge them */
                if ( prev_region && ( region->start == prev_region->end ) ) {
                        prev_region->end = region->end;
                } else {
                        prev_region = region;
                        region++;
                        memmap->count++;
                }

                if ( memmap->count >= ( sizeof ( memmap->regions ) /
                                        sizeof ( memmap->regions[0] ) ) ) {
                        DBG ( "INT 15,e820 too many regions returned\n" );
                        /* Not a fatal error; what we've got so far at
                         * least represents valid regions of memory,
                         * even if we couldn't get them all.
                         */
                        break;
                }
        } while ( next != 0 );

        /* Sanity checks.  Some BIOSes report complete garbage via INT
         * 15,e820 (especially at POST time), despite passing the
         * signature checks.  We currently check for a base memory
         * region (starting at 0) and at least one high memory region
         * (starting at 0x100000).
         */
        if ( memmap->count < 2 ) {
                DBG ( "INT 15,e820 returned only %d regions; assuming "
                      "insane\n", memmap->count );
                return -EINVAL;
        }
        if ( memmap->regions[0].start != 0 ) {
                DBG ( "INT 15,e820 region 0 starts at %llx (expected 0); "
                      "assuming insane\n", memmap->regions[0].start );
                return -EINVAL;
        }
        if ( memmap->regions[1].start != 0x100000 ) {
                DBG ( "INT 15,e820 region 1 starts at %llx (expected 100000); "
                      "assuming insane\n", memmap->regions[0].start );
                return -EINVAL;
        }

        return 0;
}
void x86_get_memmap ( struct memory_map memmap)

Get memory map.

Parameters:
memmapMemory map to fill in

Definition at line 314 of file memmap.c.

References basememsize(), memory_map::count, DBG, memory_region::end, extmemsize(), meme820(), memset(), rc, memory_map::regions, and memory_region::start.

                                                  {
        unsigned int basemem, extmem;
        int rc;

        DBG ( "Fetching system memory map\n" );

        /* Clear memory map */
        memset ( memmap, 0, sizeof ( *memmap ) );

        /* Get base and extended memory sizes */
        basemem = basememsize();
        DBG ( "FBMS base memory size %d kB [0,%x)\n",
              basemem, ( basemem * 1024 ) );
        extmem = extmemsize();
        
        /* Try INT 15,e820 first */
        if ( ( rc = meme820 ( memmap ) ) == 0 ) {
                DBG ( "Obtained system memory map via INT 15,e820\n" );
                return;
        }

        /* Fall back to constructing a map from basemem and extmem sizes */
        DBG ( "INT 15,e820 failed; constructing map\n" );
        memmap->regions[0].end = ( basemem * 1024 );
        memmap->regions[1].start = 0x100000;
        memmap->regions[1].end = 0x100000 + ( extmem * 1024 );
        memmap->count = 2;
}
PROVIDE_IOAPI ( x86  ,
get_memmap  ,
x86_get_memmap   
)

Variable Documentation

Start of region.

Definition at line 55 of file memmap.c.

Length of region.

Definition at line 57 of file memmap.c.

Type of region.

Definition at line 59 of file memmap.c.

Extended attributes (optional)

Definition at line 61 of file memmap.c.

Referenced by ansiscr_attrs(), ansiscr_putc(), meme820(), and wattron().