00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
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
00030
00031
00032
00033
00034
00035
00036 #define SMAP ( 0x534d4150 )
00037
00038
00039 struct e820_entry {
00040
00041 uint64_t start;
00042
00043 uint64_t len;
00044
00045 uint32_t type;
00046
00047 uint32_t attrs;
00048 } __attribute__ (( packed ));
00049
00050 #define E820_TYPE_RAM 1
00051 #define E820_TYPE_RESERVED 2
00052 #define E820_TYPE_ACPI 3
00053 #define E820_TYPE_NVS 4
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
00062 static struct e820_entry __bss16 ( e820buf );
00063 #define e820buf __use_data16 ( e820buf )
00064
00065
00066
00067
00068
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
00104
00105
00106
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
00118
00119
00120
00121 static unsigned int extmemsize_88 ( void ) {
00122 uint16_t extmem;
00123
00124
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
00135
00136
00137
00138
00139
00140
00141 unsigned int extmemsize ( void ) {
00142 unsigned int extmem;
00143
00144
00145 extmem = extmemsize_e801();
00146 if ( ! extmem )
00147 extmem = extmemsize_88();
00148 return extmem;
00149 }
00150
00151
00152
00153
00154
00155
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
00166
00167
00168
00169 memset ( &e820buf, 0, sizeof ( e820buf ) );
00170
00171 do {
00172
00173
00174
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
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
00228 if ( e820buf.type != E820_TYPE_RAM )
00229 continue;
00230
00231
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
00248
00249
00250
00251 break;
00252 }
00253 } while ( next != 0 );
00254
00255
00256
00257
00258
00259
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
00282
00283
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
00292 memset ( memmap, 0, sizeof ( *memmap ) );
00293
00294
00295 basemem = basememsize();
00296 DBG ( "FBMS base memory size %d kB [0,%x)\n",
00297 basemem, ( basemem * 1024 ) );
00298 extmem = extmemsize();
00299
00300
00301 if ( ( rc = meme820 ( memmap ) ) == 0 ) {
00302 DBG ( "Obtained system memory map via INT 15,e820\n" );
00303 return;
00304 }
00305
00306
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 }