memmap.c File Reference

Memory mapping. More...

#include <stdint.h>
#include <errno.h>
#include <realmode.h>
#include <bios.h>
#include <memsizes.h>
#include <gpxe/memmap.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 )

Functions

 FILE_LICENCE (GPL2_OR_LATER)
static struct e820_entry __bss16 (e820buf)
 Buffer for INT 15,e820 calls.
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 get_memmap (struct memory_map *memmap)
 Get memory map.

Variables

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


Detailed Description

Memory mapping.

Definition in file memmap.c.


Define Documentation

#define SMAP   ( 0x534d4150 )

Magic value for INT 15,e820 calls.

Definition at line 36 of file memmap.c.

Referenced by meme820().

#define E820_TYPE_RAM   1

Normal memory.

Definition at line 50 of file memmap.c.

#define E820_TYPE_RESERVED   2

Reserved and unavailable.

Definition at line 51 of file memmap.c.

#define E820_TYPE_ACPI   3

ACPI reclaim memory.

Definition at line 52 of file memmap.c.

#define E820_TYPE_NVS   4

ACPI NVS memory.

Definition at line 53 of file memmap.c.

#define E820_ATTR_ENABLED   0x00000001UL

Definition at line 55 of file memmap.c.

Referenced by meme820().

#define E820_ATTR_NONVOLATILE   0x00000002UL

Definition at line 56 of file memmap.c.

Referenced by meme820().

#define E820_ATTR_UNKNOWN   0xfffffffcUL

Definition at line 57 of file memmap.c.

Referenced by meme820().

#define E820_MIN_SIZE   20

Definition at line 59 of file memmap.c.

Referenced by meme820().

#define e820buf   __use_data16 ( e820buf )

Definition at line 63 of file memmap.c.

Referenced by meme820().


Function Documentation

FILE_LICENCE ( GPL2_OR_LATER   ) 

static struct e820_entry __bss16 ( e820buf   )  [static, read]

Buffer for INT 15,e820 calls.

static unsigned int extmemsize_e801 ( void   )  [static]

Get size of extended memory via INT 15,e801.

Return values:
extmem Extended memory size, in kB, or 0

Definition at line 70 of file memmap.c.

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

Referenced by extmemsize().

00070                                              {
00071         uint16_t extmem_1m_to_16m_k, extmem_16m_plus_64k;
00072         uint16_t confmem_1m_to_16m_k, confmem_16m_plus_64k;
00073         unsigned int flags;
00074         unsigned int extmem;
00075 
00076         __asm__ __volatile__ ( REAL_CODE ( "stc\n\t"
00077                                            "int $0x15\n\t"
00078                                            "pushfw\n\t"
00079                                            "popw %w0\n\t" )
00080                                : "=r" ( flags ),
00081                                  "=a" ( extmem_1m_to_16m_k ),
00082                                  "=b" ( extmem_16m_plus_64k ),
00083                                  "=c" ( confmem_1m_to_16m_k ),
00084                                  "=d" ( confmem_16m_plus_64k )
00085                                : "a" ( 0xe801 ) );
00086 
00087         if ( flags & CF ) {
00088                 DBG ( "INT 15,e801 failed with CF set\n" );
00089                 return 0;
00090         }
00091 
00092         if ( ! ( extmem_1m_to_16m_k | extmem_16m_plus_64k ) ) {
00093                 DBG ( "INT 15,e801 extmem=0, using confmem\n" );
00094                 extmem_1m_to_16m_k = confmem_1m_to_16m_k;
00095                 extmem_16m_plus_64k = confmem_16m_plus_64k;
00096         }
00097 
00098         extmem = ( extmem_1m_to_16m_k + ( extmem_16m_plus_64k * 64 ) );
00099         DBG ( "INT 15,e801 extended memory size %d+64*%d=%d kB "
00100               "[100000,%llx)\n", extmem_1m_to_16m_k, extmem_16m_plus_64k,
00101               extmem, ( 0x100000 + ( ( ( uint64_t ) extmem ) * 1024 ) ) );
00102 
00103         /* Sanity check.  Some BIOSes report the entire 4GB address
00104          * space as available, which cannot be correct (since that
00105          * would leave no address space available for 32-bit PCI
00106          * BARs).
00107          */
00108         if ( extmem == ( 0x400000 - 0x400 ) ) {
00109                 DBG ( "INT 15,e801 reported whole 4GB; assuming insane\n" );
00110                 return 0;
00111         }
00112 
00113         return extmem;
00114 }

static unsigned int extmemsize_88 ( void   )  [static]

Get size of extended memory via INT 15,88.

Return values:
extmem Extended memory size, in kB

Definition at line 121 of file memmap.c.

References __asm__(), DBG, and REAL_CODE.

Referenced by extmemsize().

00121                                            {
00122         uint16_t extmem;
00123 
00124         /* Ignore CF; it is not reliable for this call */
00125         __asm__ __volatile__ ( REAL_CODE ( "int $0x15" )
00126                                : "=a" ( extmem ) : "a" ( 0x8800 ) );
00127 
00128         DBG ( "INT 15,88 extended memory size %d kB [100000, %x)\n",
00129               extmem, ( 0x100000 + ( extmem * 1024 ) ) );
00130         return extmem;
00131 }

unsigned int extmemsize ( void   ) 

Get size of extended memory.

Return values:
extmem Extended 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 141 of file memmap.c.

References extmemsize_88(), and extmemsize_e801().

Referenced by get_memmap(), and nbi_process_segments().

00141                                  {
00142         unsigned int extmem;
00143 
00144         /* Try INT 15,e801 first, then fall back to INT 15,88 */
00145         extmem = extmemsize_e801();
00146         if ( ! extmem )
00147                 extmem = extmemsize_88();
00148         return extmem;
00149 }

static int meme820 ( struct memory_map memmap  )  [static]

Get e820 memory map.

Parameters:
memmap Memory map to fill in
Return values:
rc Return status code

Definition at line 157 of file memmap.c.

References __asm__(), __from_data16, 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, memset(), offsetof, REAL_CODE, memory_map::regions, size, SMAP, and memory_region::start.

Referenced by get_memmap().

00157                                                  {
00158         struct memory_region *region = memmap->regions;
00159         uint32_t next = 0;
00160         uint32_t smap;
00161         size_t size;
00162         unsigned int flags;
00163         unsigned int discard_D;
00164 
00165         /* Clear the E820 buffer.  Do this once before starting,
00166          * rather than on each call; some BIOSes rely on the contents
00167          * being preserved between calls.
00168          */
00169         memset ( &e820buf, 0, sizeof ( e820buf ) );
00170 
00171         do {
00172                 /* Some BIOSes corrupt %esi for fun. Guard against
00173                  * this by telling gcc that all non-output registers
00174                  * may be corrupted.
00175                  */
00176                 __asm__ __volatile__ ( REAL_CODE ( "pushl %%ebp\n\t"
00177                                                    "stc\n\t"
00178                                                    "int $0x15\n\t"
00179                                                    "pushfw\n\t"
00180                                                    "popw %%dx\n\t"
00181                                                    "popl %%ebp\n\t" )
00182                                        : "=a" ( smap ), "=b" ( next ),
00183                                          "=c" ( size ), "=d" ( flags ),
00184                                          "=D" ( discard_D )
00185                                        : "a" ( 0xe820 ), "b" ( next ),
00186                                          "D" ( __from_data16 ( &e820buf ) ),
00187                                          "c" ( sizeof ( e820buf ) ),
00188                                          "d" ( SMAP )
00189                                        : "esi", "memory" );
00190 
00191                 if ( smap != SMAP ) {
00192                         DBG ( "INT 15,e820 failed SMAP signature check\n" );
00193                         return -ENOTSUP;
00194                 }
00195 
00196                 if ( size < E820_MIN_SIZE ) {
00197                         DBG ( "INT 15,e820 returned only %zd bytes\n", size );
00198                         return -EINVAL;
00199                 }
00200 
00201                 if ( flags & CF ) {
00202                         DBG ( "INT 15,e820 terminated on CF set\n" );
00203                         break;
00204                 }
00205 
00206                 /* If first region is not RAM, assume map is invalid */
00207                 if ( ( memmap->count == 0 ) &&
00208                      ( e820buf.type != E820_TYPE_RAM ) ) {
00209                        DBG ( "INT 15,e820 failed, first entry not RAM\n" );
00210                        return -EINVAL;
00211                 }
00212 
00213                 DBG ( "INT 15,e820 region [%llx,%llx) type %d",
00214                       e820buf.start, ( e820buf.start + e820buf.len ),
00215                       ( int ) e820buf.type );
00216                 if ( size > offsetof ( typeof ( e820buf ), attrs ) ) {
00217                         DBG ( " (%s", ( ( e820buf.attrs & E820_ATTR_ENABLED )
00218                                         ? "enabled" : "disabled" ) );
00219                         if ( e820buf.attrs & E820_ATTR_NONVOLATILE )
00220                                 DBG ( ", non-volatile" );
00221                         if ( e820buf.attrs & E820_ATTR_UNKNOWN )
00222                                 DBG ( ", other [%08x]", e820buf.attrs );
00223                         DBG ( ")" );
00224                 }
00225                 DBG ( "\n" );
00226 
00227                 /* Discard non-RAM regions */
00228                 if ( e820buf.type != E820_TYPE_RAM )
00229                         continue;
00230 
00231                 /* Check extended attributes, if present */
00232                 if ( size > offsetof ( typeof ( e820buf ), attrs ) ) {
00233                         if ( ! ( e820buf.attrs & E820_ATTR_ENABLED ) )
00234                                 continue;
00235                         if ( e820buf.attrs & E820_ATTR_NONVOLATILE )
00236                                 continue;
00237                 }
00238 
00239                 region->start = e820buf.start;
00240                 region->end = e820buf.start + e820buf.len;
00241                 region++;
00242                 memmap->count++;
00243 
00244                 if ( memmap->count >= ( sizeof ( memmap->regions ) /
00245                                         sizeof ( memmap->regions[0] ) ) ) {
00246                         DBG ( "INT 15,e820 too many regions returned\n" );
00247                         /* Not a fatal error; what we've got so far at
00248                          * least represents valid regions of memory,
00249                          * even if we couldn't get them all.
00250                          */
00251                         break;
00252                 }
00253         } while ( next != 0 );
00254 
00255         /* Sanity checks.  Some BIOSes report complete garbage via INT
00256          * 15,e820 (especially at POST time), despite passing the
00257          * signature checks.  We currently check for a base memory
00258          * region (starting at 0) and at least one high memory region
00259          * (starting at 0x100000).
00260          */
00261         if ( memmap->count < 2 ) {
00262                 DBG ( "INT 15,e820 returned only %d regions; assuming "
00263                       "insane\n", memmap->count );
00264                 return -EINVAL;
00265         }
00266         if ( memmap->regions[0].start != 0 ) {
00267                 DBG ( "INT 15,e820 region 0 starts at %llx (expected 0); "
00268                       "assuming insane\n", memmap->regions[0].start );
00269                 return -EINVAL;
00270         }
00271         if ( memmap->regions[1].start != 0x100000 ) {
00272                 DBG ( "INT 15,e820 region 1 starts at %llx (expected 100000); "
00273                       "assuming insane\n", memmap->regions[0].start );
00274                 return -EINVAL;
00275         }
00276 
00277         return 0;
00278 }

void get_memmap ( struct memory_map memmap  ) 

Get memory map.

Parameters:
memmap Memory map to fill in

Definition at line 285 of file memmap.c.

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

Referenced by com32_exec(), hide_etherboot(), init_eheap(), int13_boot(), multiboot_build_memmap(), phys_ram_within_limit(), prep_segment(), relocate(), and umalloc_test().

00285                                               {
00286         unsigned int basemem, extmem;
00287         int rc;
00288 
00289         DBG ( "Fetching system memory map\n" );
00290 
00291         /* Clear memory map */
00292         memset ( memmap, 0, sizeof ( *memmap ) );
00293 
00294         /* Get base and extended memory sizes */
00295         basemem = basememsize();
00296         DBG ( "FBMS base memory size %d kB [0,%x)\n",
00297               basemem, ( basemem * 1024 ) );
00298         extmem = extmemsize();
00299         
00300         /* Try INT 15,e820 first */
00301         if ( ( rc = meme820 ( memmap ) ) == 0 ) {
00302                 DBG ( "Obtained system memory map via INT 15,e820\n" );
00303                 return;
00304         }
00305 
00306         /* Fall back to constructing a map from basemem and extmem sizes */
00307         DBG ( "INT 15,e820 failed; constructing map\n" );
00308         memmap->regions[0].end = ( basemem * 1024 );
00309         memmap->regions[1].start = 0x100000;
00310         memmap->regions[1].end = 0x100000 + ( extmem * 1024 );
00311         memmap->count = 2;
00312 }


Variable Documentation

An INT 15,e820 memory map entry.


Generated on Tue Apr 6 20:01:12 2010 for gPXE by  doxygen 1.5.7.1