memmap.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2006 Michael Brown <mbrown@fensystems.co.uk>.
00003  *
00004  * This program is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU General Public License as
00006  * published by the Free Software Foundation; either version 2 of the
00007  * License, or any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful, but
00010  * WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  * General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software
00016  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00017  */
00018 
00019 FILE_LICENCE ( GPL2_OR_LATER );
00020 
00021 #include <stdint.h>
00022 #include <errno.h>
00023 #include <realmode.h>
00024 #include <bios.h>
00025 #include <memsizes.h>
00026 #include <gpxe/memmap.h>
00027 
00028 /**
00029  * @file
00030  *
00031  * Memory mapping
00032  *
00033  */
00034 
00035 /** Magic value for INT 15,e820 calls */
00036 #define SMAP ( 0x534d4150 )
00037 
00038 /** An INT 15,e820 memory map entry */
00039 struct e820_entry {
00040         /** Start of region */
00041         uint64_t start;
00042         /** Length of region */
00043         uint64_t len;
00044         /** Type of region */
00045         uint32_t type;
00046         /** Extended attributes (optional) */
00047         uint32_t attrs;
00048 } __attribute__ (( packed ));
00049 
00050 #define E820_TYPE_RAM           1 /**< Normal memory */
00051 #define E820_TYPE_RESERVED      2 /**< Reserved and unavailable */
00052 #define E820_TYPE_ACPI          3 /**< ACPI reclaim memory */
00053 #define E820_TYPE_NVS           4 /**< ACPI NVS memory */
00054 
00055 #define E820_ATTR_ENABLED       0x00000001UL
00056 #define E820_ATTR_NONVOLATILE   0x00000002UL
00057 #define E820_ATTR_UNKNOWN       0xfffffffcUL
00058 
00059 #define E820_MIN_SIZE           20
00060 
00061 /** Buffer for INT 15,e820 calls */
00062 static struct e820_entry __bss16 ( e820buf );
00063 #define e820buf __use_data16 ( e820buf )
00064 
00065 /**
00066  * Get size of extended memory via INT 15,e801
00067  *
00068  * @ret extmem          Extended memory size, in kB, or 0
00069  */
00070 static unsigned int extmemsize_e801 ( void ) {
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 }
00115 
00116 /**
00117  * Get size of extended memory via INT 15,88
00118  *
00119  * @ret extmem          Extended memory size, in kB
00120  */
00121 static unsigned int extmemsize_88 ( void ) {
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 }
00132 
00133 /**
00134  * Get size of extended memory
00135  *
00136  * @ret extmem          Extended memory size, in kB
00137  *
00138  * Note that this is only an approximation; for an accurate picture,
00139  * use the E820 memory map obtained via get_memmap();
00140  */
00141 unsigned int extmemsize ( void ) {
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 }
00150 
00151 /**
00152  * Get e820 memory map
00153  *
00154  * @v memmap            Memory map to fill in
00155  * @ret rc              Return status code
00156  */
00157 static int meme820 ( struct memory_map *memmap ) {
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 }
00279 
00280 /**
00281  * Get memory map
00282  *
00283  * @v memmap            Memory map to fill in
00284  */
00285 void get_memmap ( struct memory_map *memmap ) {
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 }

Generated on Tue Apr 6 20:00:49 2010 for gPXE by  doxygen 1.5.7.1