iPXE
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., 51 Franklin Street, Fifth Floor, Boston, MA
00017  * 02110-1301, USA.
00018  *
00019  * You can also choose to distribute this program under the terms of
00020  * the Unmodified Binary Distribution Licence (as given in the file
00021  * COPYING.UBDL), provided that you have satisfied its requirements.
00022  */
00023 
00024 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
00025 
00026 #include <stdint.h>
00027 #include <errno.h>
00028 #include <realmode.h>
00029 #include <bios.h>
00030 #include <memsizes.h>
00031 #include <ipxe/io.h>
00032 
00033 /**
00034  * @file
00035  *
00036  * Memory mapping
00037  *
00038  */
00039 
00040 /** Magic value for INT 15,e820 calls */
00041 #define SMAP ( 0x534d4150 )
00042 
00043 /** An INT 15,e820 memory map entry */
00044 struct e820_entry {
00045         /** Start of region */
00046         uint64_t start;
00047         /** Length of region */
00048         uint64_t len;
00049         /** Type of region */
00050         uint32_t type;
00051         /** Extended attributes (optional) */
00052         uint32_t attrs;
00053 } __attribute__ (( packed ));
00054 
00055 #define E820_TYPE_RAM           1 /**< Normal memory */
00056 #define E820_TYPE_RESERVED      2 /**< Reserved and unavailable */
00057 #define E820_TYPE_ACPI          3 /**< ACPI reclaim memory */
00058 #define E820_TYPE_NVS           4 /**< ACPI NVS memory */
00059 
00060 #define E820_ATTR_ENABLED       0x00000001UL
00061 #define E820_ATTR_NONVOLATILE   0x00000002UL
00062 #define E820_ATTR_UNKNOWN       0xfffffffcUL
00063 
00064 #define E820_MIN_SIZE           20
00065 
00066 /** Buffer for INT 15,e820 calls */
00067 static struct e820_entry __bss16 ( e820buf );
00068 #define e820buf __use_data16 ( e820buf )
00069 
00070 /** We are running during POST; inhibit INT 15,e820 and INT 15,e801 */
00071 uint8_t __bss16 ( memmap_post );
00072 #define memmap_post __use_data16 ( memmap_post )
00073 
00074 /**
00075  * Get size of extended memory via INT 15,e801
00076  *
00077  * @ret extmem          Extended memory size, in kB, or 0
00078  */
00079 static unsigned int extmemsize_e801 ( void ) {
00080         uint16_t extmem_1m_to_16m_k, extmem_16m_plus_64k;
00081         uint16_t confmem_1m_to_16m_k, confmem_16m_plus_64k;
00082         unsigned int flags;
00083         unsigned int extmem;
00084 
00085         /* Inhibit INT 15,e801 during POST */
00086         if ( memmap_post ) {
00087                 DBG ( "INT 15,e801 not available during POST\n" );
00088                 return 0;
00089         }
00090 
00091         __asm__ __volatile__ ( REAL_CODE ( "stc\n\t"
00092                                            "int $0x15\n\t"
00093                                            "pushfw\n\t"
00094                                            "popw %w0\n\t" )
00095                                : "=R" ( flags ),
00096                                  "=a" ( extmem_1m_to_16m_k ),
00097                                  "=b" ( extmem_16m_plus_64k ),
00098                                  "=c" ( confmem_1m_to_16m_k ),
00099                                  "=d" ( confmem_16m_plus_64k )
00100                                : "a" ( 0xe801 ) );
00101 
00102         if ( flags & CF ) {
00103                 DBG ( "INT 15,e801 failed with CF set\n" );
00104                 return 0;
00105         }
00106 
00107         if ( ! ( extmem_1m_to_16m_k | extmem_16m_plus_64k ) ) {
00108                 DBG ( "INT 15,e801 extmem=0, using confmem\n" );
00109                 extmem_1m_to_16m_k = confmem_1m_to_16m_k;
00110                 extmem_16m_plus_64k = confmem_16m_plus_64k;
00111         }
00112 
00113         extmem = ( extmem_1m_to_16m_k + ( extmem_16m_plus_64k * 64 ) );
00114         DBG ( "INT 15,e801 extended memory size %d+64*%d=%d kB "
00115               "[100000,%llx)\n", extmem_1m_to_16m_k, extmem_16m_plus_64k,
00116               extmem, ( 0x100000 + ( ( ( uint64_t ) extmem ) * 1024 ) ) );
00117 
00118         /* Sanity check.  Some BIOSes report the entire 4GB address
00119          * space as available, which cannot be correct (since that
00120          * would leave no address space available for 32-bit PCI
00121          * BARs).
00122          */
00123         if ( extmem == ( 0x400000 - 0x400 ) ) {
00124                 DBG ( "INT 15,e801 reported whole 4GB; assuming insane\n" );
00125                 return 0;
00126         }
00127 
00128         return extmem;
00129 }
00130 
00131 /**
00132  * Get size of extended memory via INT 15,88
00133  *
00134  * @ret extmem          Extended memory size, in kB
00135  */
00136 static unsigned int extmemsize_88 ( void ) {
00137         uint16_t extmem;
00138 
00139         /* Ignore CF; it is not reliable for this call */
00140         __asm__ __volatile__ ( REAL_CODE ( "int $0x15" )
00141                                : "=a" ( extmem ) : "a" ( 0x8800 ) );
00142 
00143         DBG ( "INT 15,88 extended memory size %d kB [100000, %x)\n",
00144               extmem, ( 0x100000 + ( extmem * 1024 ) ) );
00145         return extmem;
00146 }
00147 
00148 /**
00149  * Get size of extended memory
00150  *
00151  * @ret extmem          Extended memory size, in kB
00152  *
00153  * Note that this is only an approximation; for an accurate picture,
00154  * use the E820 memory map obtained via get_memmap();
00155  */
00156 unsigned int extmemsize ( void ) {
00157         unsigned int extmem_e801;
00158         unsigned int extmem_88;
00159 
00160         /* Try INT 15,e801 first, then fall back to INT 15,88 */
00161         extmem_88 = extmemsize_88();
00162         extmem_e801 = extmemsize_e801();
00163         return ( extmem_e801 ? extmem_e801 : extmem_88 );
00164 }
00165 
00166 /**
00167  * Get e820 memory map
00168  *
00169  * @v memmap            Memory map to fill in
00170  * @ret rc              Return status code
00171  */
00172 static int meme820 ( struct memory_map *memmap ) {
00173         struct memory_region *region = memmap->regions;
00174         struct memory_region *prev_region = NULL;
00175         uint32_t next = 0;
00176         uint32_t smap;
00177         uint32_t size;
00178         unsigned int flags;
00179         unsigned int discard_D;
00180 
00181         /* Inhibit INT 15,e820 during POST */
00182         if ( memmap_post ) {
00183                 DBG ( "INT 15,e820 not available during POST\n" );
00184                 return -ENOTTY;
00185         }
00186 
00187         /* Clear the E820 buffer.  Do this once before starting,
00188          * rather than on each call; some BIOSes rely on the contents
00189          * being preserved between calls.
00190          */
00191         memset ( &e820buf, 0, sizeof ( e820buf ) );
00192 
00193         do {
00194                 /* Some BIOSes corrupt %esi for fun. Guard against
00195                  * this by telling gcc that all non-output registers
00196                  * may be corrupted.
00197                  */
00198                 __asm__ __volatile__ ( REAL_CODE ( "pushl %%ebp\n\t"
00199                                                    "stc\n\t"
00200                                                    "int $0x15\n\t"
00201                                                    "pushfw\n\t"
00202                                                    "popw %%dx\n\t"
00203                                                    "popl %%ebp\n\t" )
00204                                        : "=a" ( smap ), "=b" ( next ),
00205                                          "=c" ( size ), "=d" ( flags ),
00206                                          "=D" ( discard_D )
00207                                        : "a" ( 0xe820 ), "b" ( next ),
00208                                          "D" ( __from_data16 ( &e820buf ) ),
00209                                          "c" ( sizeof ( e820buf ) ),
00210                                          "d" ( SMAP )
00211                                        : "esi", "memory" );
00212 
00213                 if ( smap != SMAP ) {
00214                         DBG ( "INT 15,e820 failed SMAP signature check\n" );
00215                         return -ENOTSUP;
00216                 }
00217 
00218                 if ( size < E820_MIN_SIZE ) {
00219                         DBG ( "INT 15,e820 returned only %d bytes\n", size );
00220                         return -EINVAL;
00221                 }
00222 
00223                 if ( flags & CF ) {
00224                         DBG ( "INT 15,e820 terminated on CF set\n" );
00225                         break;
00226                 }
00227 
00228                 /* If first region is not RAM, assume map is invalid */
00229                 if ( ( memmap->count == 0 ) &&
00230                      ( e820buf.type != E820_TYPE_RAM ) ) {
00231                        DBG ( "INT 15,e820 failed, first entry not RAM\n" );
00232                        return -EINVAL;
00233                 }
00234 
00235                 DBG ( "INT 15,e820 region [%llx,%llx) type %d",
00236                       e820buf.start, ( e820buf.start + e820buf.len ),
00237                       ( int ) e820buf.type );
00238                 if ( size > offsetof ( typeof ( e820buf ), attrs ) ) {
00239                         DBG ( " (%s", ( ( e820buf.attrs & E820_ATTR_ENABLED )
00240                                         ? "enabled" : "disabled" ) );
00241                         if ( e820buf.attrs & E820_ATTR_NONVOLATILE )
00242                                 DBG ( ", non-volatile" );
00243                         if ( e820buf.attrs & E820_ATTR_UNKNOWN )
00244                                 DBG ( ", other [%08x]", e820buf.attrs );
00245                         DBG ( ")" );
00246                 }
00247                 DBG ( "\n" );
00248 
00249                 /* Discard non-RAM regions */
00250                 if ( e820buf.type != E820_TYPE_RAM )
00251                         continue;
00252 
00253                 /* Check extended attributes, if present */
00254                 if ( size > offsetof ( typeof ( e820buf ), attrs ) ) {
00255                         if ( ! ( e820buf.attrs & E820_ATTR_ENABLED ) )
00256                                 continue;
00257                         if ( e820buf.attrs & E820_ATTR_NONVOLATILE )
00258                                 continue;
00259                 }
00260 
00261                 region->start = e820buf.start;
00262                 region->end = e820buf.start + e820buf.len;
00263 
00264                 /* Check for adjacent regions and merge them */
00265                 if ( prev_region && ( region->start == prev_region->end ) ) {
00266                         prev_region->end = region->end;
00267                 } else {
00268                         prev_region = region;
00269                         region++;
00270                         memmap->count++;
00271                 }
00272 
00273                 if ( memmap->count >= ( sizeof ( memmap->regions ) /
00274                                         sizeof ( memmap->regions[0] ) ) ) {
00275                         DBG ( "INT 15,e820 too many regions returned\n" );
00276                         /* Not a fatal error; what we've got so far at
00277                          * least represents valid regions of memory,
00278                          * even if we couldn't get them all.
00279                          */
00280                         break;
00281                 }
00282         } while ( next != 0 );
00283 
00284         /* Sanity checks.  Some BIOSes report complete garbage via INT
00285          * 15,e820 (especially at POST time), despite passing the
00286          * signature checks.  We currently check for a base memory
00287          * region (starting at 0) and at least one high memory region
00288          * (starting at 0x100000).
00289          */
00290         if ( memmap->count < 2 ) {
00291                 DBG ( "INT 15,e820 returned only %d regions; assuming "
00292                       "insane\n", memmap->count );
00293                 return -EINVAL;
00294         }
00295         if ( memmap->regions[0].start != 0 ) {
00296                 DBG ( "INT 15,e820 region 0 starts at %llx (expected 0); "
00297                       "assuming insane\n", memmap->regions[0].start );
00298                 return -EINVAL;
00299         }
00300         if ( memmap->regions[1].start != 0x100000 ) {
00301                 DBG ( "INT 15,e820 region 1 starts at %llx (expected 100000); "
00302                       "assuming insane\n", memmap->regions[0].start );
00303                 return -EINVAL;
00304         }
00305 
00306         return 0;
00307 }
00308 
00309 /**
00310  * Get memory map
00311  *
00312  * @v memmap            Memory map to fill in
00313  */
00314 void x86_get_memmap ( struct memory_map *memmap ) {
00315         unsigned int basemem, extmem;
00316         int rc;
00317 
00318         DBG ( "Fetching system memory map\n" );
00319 
00320         /* Clear memory map */
00321         memset ( memmap, 0, sizeof ( *memmap ) );
00322 
00323         /* Get base and extended memory sizes */
00324         basemem = basememsize();
00325         DBG ( "FBMS base memory size %d kB [0,%x)\n",
00326               basemem, ( basemem * 1024 ) );
00327         extmem = extmemsize();
00328         
00329         /* Try INT 15,e820 first */
00330         if ( ( rc = meme820 ( memmap ) ) == 0 ) {
00331                 DBG ( "Obtained system memory map via INT 15,e820\n" );
00332                 return;
00333         }
00334 
00335         /* Fall back to constructing a map from basemem and extmem sizes */
00336         DBG ( "INT 15,e820 failed; constructing map\n" );
00337         memmap->regions[0].end = ( basemem * 1024 );
00338         memmap->regions[1].start = 0x100000;
00339         memmap->regions[1].end = 0x100000 + ( extmem * 1024 );
00340         memmap->count = 2;
00341 }
00342 
00343 PROVIDE_IOAPI ( x86, get_memmap, x86_get_memmap );