2
0
mirror of https://github.com/xcat2/xNBA.git synced 2024-12-23 19:51:46 +00:00

Replaced memsizes.c with smaller memmap.c, taking advantage of __data16,

and creating a memory map that's easier to work with than the E820 map.
This commit is contained in:
Michael Brown 2006-05-24 01:37:46 +00:00
parent 5e80847827
commit 77a65075a6
4 changed files with 239 additions and 235 deletions

View File

@ -0,0 +1,214 @@
/*
* Copyright (C) 2006 Michael Brown <mbrown@fensystems.co.uk>.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include <stdint.h>
#include <errno.h>
#include <realmode.h>
#include <bios.h>
#include <memmap.h>
/**
* @file
*
* Memory mapping
*
*/
/** Magic value for INT 15,e820 calls */
#define SMAP ( 0x534d4150 )
/** An INT 15,e820 memory map entry */
struct e820_entry {
/** Start of region */
uint64_t start;
/** Length of region */
uint64_t len;
/** Type of region */
uint32_t type;
} __attribute__ (( packed ));
#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 */
/** Buffer for INT 15,e820 calls */
static struct e820_entry __data16 ( e820buf );
#define e820buf __use_data16 ( e820buf )
/**
* Get size of base memory from BIOS free base memory counter
*
* @ret basemem Base memory size, in kB
*/
static unsigned int basememsize ( void ) {
uint16_t basemem;
get_real ( basemem, BDA_SEG, 0x0013 );
DBG ( "Base memory size %dkB\n", basemem );
return basemem;
}
/**
* Get size of extended memory via INT 15,e801
*
* @ret extmem Extended memory size, in kB, or 0
*/
static unsigned int extmemsize_e801 ( void ) {
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;
REAL_EXEC ( rm_mem_e801,
"int $0x15\n\t"
"pushfw\n\t"
"popw %w0\n\t",
5,
OUT_CONSTRAINTS ( "=r" ( flags ),
"=a" ( extmem_1m_to_16m_k ),
"=b" ( extmem_16m_plus_64k ),
"=c" ( confmem_1m_to_16m_k ),
"=d" ( confmem_16m_plus_64k ) ),
IN_CONSTRAINTS ( "a" ( 0xe801 ) ),
CLOBBER ( "cc" ) );
if ( flags & CF )
return 0;
if ( ! ( extmem_1m_to_16m_k | extmem_16m_plus_64k ) ) {
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 ( "Extended memory size %d+64*%d=%d kB\n",
extmem_1m_to_16m_k, extmem_16m_plus_64k, extmem );
return extmem;
}
/**
* Get size of extended memory via INT 15,88
*
* @ret extmem Extended memory size, in kB
*/
static unsigned int extmemsize_88 ( void ) {
uint16_t extmem;
REAL_EXEC ( rm_mem_88,
"int $0x15\n\t"
"jnc 1f\n\t"
"xorw %%ax, %%ax\n\t"
"\n1:\n\t",
1,
OUT_CONSTRAINTS ( "=a" ( extmem ) ),
IN_CONSTRAINTS ( "a" ( 0x8800 ) ),
CLOBBER ( "cc" ) );
DBG ( "Extended memory size %d kB\n", extmem );
return extmem;
}
/**
* Get size of extended memory
*
* @ret extmem Extended memory size, in kB
*/
static unsigned int extmemsize ( void ) {
unsigned int extmem;
/* Try INT 15,e801 first, then fall back to INT 15,88 */
extmem = extmemsize_e801();
if ( ! extmem )
extmem = extmemsize_88();
return extmem;
}
/**
* Get e820 memory map
*
* @v memmap Memory map to fill in
* @v entries Maximum number of entries in memory map
* @ret rc Return status code
*/
static int meme820 ( struct memory_region *memmap, unsigned int entries ) {
unsigned int index = 0;
uint32_t next = 0;
uint32_t smap;
unsigned int flags;
unsigned int discard_c, discard_d, discard_D;
do {
REAL_EXEC ( rm_mem_e820,
"int $0x15\n\t"
"pushfw\n\t"
"popw %w0\n\t",
6,
OUT_CONSTRAINTS ( "=r" ( flags ),
"=a" ( smap ),
"=b" ( next ),
"=D" ( discard_D ),
"=c" ( discard_c ),
"=d" ( discard_d ) ),
IN_CONSTRAINTS ( "a" ( 0xe820 ),
"b" ( next ),
"D" ( &__from_data16 ( e820buf )),
"c" ( sizeof ( e820buf ) ),
"d" ( SMAP ) ),
CLOBBER ( "memory" ) );
if ( smap != SMAP )
return -ENOTSUP;
if ( flags & CF )
break;
DBG ( "E820 region [%llx,%llx) type %d\n", e820buf.start,
( e820buf.start + e820buf.len ), ( int ) e820buf.type );
if ( e820buf.type != E820_TYPE_RAM )
continue;
memmap[index].start = e820buf.start;
memmap[index].end = e820buf.start + e820buf.len;
index++;
} while ( ( index < entries ) && ( next != 0 ) );
return 0;
}
/**
* Get memory map
*
* @v memmap Memory map to fill in
* @v entries Maximum number of entries in memory map (minimum 2)
*/
void get_memmap ( struct memory_region *memmap, unsigned int entries ) {
unsigned int basemem, extmem;
int rc;
/* Clear memory map */
memset ( memmap, 0, ( entries * sizeof ( *memmap ) ) );
/* Get base and extended memory sizes */
basemem = basememsize();
extmem = extmemsize();
/* Try INT 15,e820 first */
if ( ( rc = meme820 ( memmap, entries ) ) == 0 )
return;
/* Fall back to constructing a map from basemem and extmem sizes */
memmap[0].end = ( basemem * 1024 );
memmap[1].start = 0x100000;
memmap[1].end = 0x100000 + ( extmem * 1024 );
}

View File

@ -1,235 +0,0 @@
#include "stdint.h"
#include "stddef.h"
#include "realmode.h"
#include <gpxe/init.h>
#include "etherboot.h"
#include "memsizes.h"
#define CF ( 1 << 0 )
/* by Eric Biederman */
struct meminfo meminfo;
/**************************************************************************
BASEMEMSIZE - Get size of the conventional (base) memory
**************************************************************************/
static unsigned short basememsize ( void ) {
uint16_t int12_basememsize, fbms_basememsize;
uint16_t basememsize;
/* There are two methods for retrieving the base memory size:
* INT 12 and the BIOS FBMS counter at 40:13. We read both
* and use the smaller value, to be paranoid.
*
* We then store the smaller value in the BIOS FBMS counter so
* that other code (e.g. basemem.c) can rely on it and not
* have to use INT 12. This is especially important because
* basemem.c functions can be called in a context in which
* there is no real-mode stack (e.g. when trying to allocate
* memory for a real-mode stack...)
*/
REAL_EXEC ( rm_basememsize,
"int $0x12\n\t",
1,
OUT_CONSTRAINTS ( "=a" ( int12_basememsize ) ),
IN_CONSTRAINTS (),
CLOBBER ( "ebx", "ecx", "edx", "ebp", "esi", "edi" ) );
get_real ( fbms_basememsize, 0x40, 0x13 );
basememsize = ( int12_basememsize < fbms_basememsize ?
int12_basememsize : fbms_basememsize );
put_real ( basememsize, 0x40, 0x13 );
return basememsize;
}
/**************************************************************************
MEMSIZE - Determine size of extended memory, in kB
**************************************************************************/
static unsigned int memsize ( void ) {
uint16_t extmem_1m_to_16m_k, extmem_16m_plus_64k;
uint16_t confmem_1m_to_16m_k, confmem_16m_plus_64k;
uint16_t flags;
int memsize;
/* Try INT 15,e801 first
*
* Some buggy BIOSes don't clear/set carry on pass/error of
* e801h memory size call or merely pass cx,dx through without
* changing them, so we set carry and zero cx,dx before call.
*/
REAL_EXEC ( rm_mem_e801,
"stc\n\t"
"int $0x15\n\t"
"pushfw\n\t" /* flags -> %di */
"popw %%di\n\t",
5,
OUT_CONSTRAINTS ( "=a" ( extmem_1m_to_16m_k ),
"=b" ( extmem_16m_plus_64k ),
"=c" ( confmem_1m_to_16m_k ),
"=d" ( confmem_16m_plus_64k ),
"=D" ( flags ) ),
IN_CONSTRAINTS ( "a" ( 0xe801 ),
"c" ( 0 ),
"d" ( 0 ) ),
CLOBBER ( "ebp", "esi" ) );
if ( ! ( flags & CF ) ) {
/* INT 15,e801 succeeded */
if ( confmem_1m_to_16m_k || confmem_16m_plus_64k ) {
/* Use confmem (cx,dx) values */
memsize = confmem_1m_to_16m_k +
( confmem_16m_plus_64k << 6 );
} else {
/* Use extmem (ax,bx) values */
memsize = extmem_1m_to_16m_k +
( extmem_16m_plus_64k << 6 );
}
} else {
/* INT 15,e801 failed; fall back to INT 15,88
*
* CF is apparently unreliable and should be ignored.
*/
REAL_EXEC ( rm_mem_88,
"int $0x15\n\t",
1,
OUT_CONSTRAINTS ( "=a" ( extmem_1m_to_16m_k ) ),
IN_CONSTRAINTS ( "a" ( 0x88 << 8 ) ),
CLOBBER ( "ebx", "ecx", "edx",
"ebp", "esi", "edi" ) );
memsize = extmem_1m_to_16m_k;
}
return memsize;
}
/**************************************************************************
MEME820 - Retrieve the E820 BIOS memory map
**************************************************************************/
#define SMAP ( 0x534d4150 ) /* "SMAP" */
static int meme820 ( struct e820entry *buf, int count ) {
int index;
uint16_t basemem_entry;
uint32_t smap, next;
uint16_t flags;
uint32_t discard_c, discard_d;
index = 0;
next = 0;
do {
basemem_entry = BASEMEM_PARAMETER_INIT ( buf[index] );
REAL_EXEC ( rm_mem_e820,
"int $0x15\n\t"
"pushfw\n\t" /* flags -> %di */
"popw %%di\n\t",
5,
OUT_CONSTRAINTS ( "=a" ( smap ),
"=b" ( next ),
"=c" ( discard_c ),
"=d" ( discard_d ),
"=D" ( flags ) ),
IN_CONSTRAINTS ( "a" ( 0xe820 ),
"b" ( next ),
"c" ( sizeof (struct e820entry) ),
"d" ( SMAP ),
"D" ( basemem_entry ) ),
CLOBBER ( "ebp", "esi" ) );
BASEMEM_PARAMETER_DONE ( buf[index] );
if ( smap != SMAP ) return 0;
if ( flags & CF ) break;
index++;
} while ( ( index < count ) && ( next != 0 ) );
return index;
}
/**************************************************************************
GET_MEMSIZES - Retrieve the system memory map via any available means
**************************************************************************/
void get_memsizes ( void ) {
/* Ensure we don't stomp bios data structutres.
* the interrupt table: 0x000 - 0x3ff
* the bios data area: 0x400 - 0x502
* Dos variables: 0x502 - 0x5ff
*/
static const unsigned min_addr = 0x600;
unsigned i;
unsigned basemem;
/* Retrieve memory information from the BIOS */
meminfo.basememsize = basememsize();
basemem = meminfo.basememsize << 10;
meminfo.memsize = memsize();
#ifndef IGNORE_E820_MAP
meminfo.map_count = meme820 ( meminfo.map, E820MAX );
#else
meminfo.map_count = 0;
#endif
/* If we don't have an e820 memory map fake it */
if ( meminfo.map_count == 0 ) {
meminfo.map_count = 2;
meminfo.map[0].addr = 0;
meminfo.map[0].size = meminfo.basememsize << 10;
meminfo.map[0].type = E820_RAM;
meminfo.map[1].addr = 1024*1024;
meminfo.map[1].size = meminfo.memsize << 10;
meminfo.map[1].type = E820_RAM;
}
/* Scrub the e820 map */
for ( i = 0; i < meminfo.map_count; i++ ) {
if ( meminfo.map[i].type != E820_RAM ) {
continue;
}
/* Reserve the bios data structures */
if ( meminfo.map[i].addr < min_addr ) {
unsigned long delta;
delta = min_addr - meminfo.map[i].addr;
if ( delta > meminfo.map[i].size ) {
delta = meminfo.map[i].size;
}
meminfo.map[i].addr = min_addr;
meminfo.map[i].size -= delta;
}
/* Ensure the returned e820 map is in sync with the
* actual memory state
*/
if ( ( meminfo.map[i].addr < 0xa0000 ) &&
(( meminfo.map[i].addr+meminfo.map[i].size ) > basemem )){
if ( meminfo.map[i].addr <= basemem ) {
meminfo.map[i].size = basemem
- meminfo.map[i].addr;
} else {
meminfo.map[i].addr = basemem;
meminfo.map[i].size = 0;
}
}
}
#ifdef DEBUG_MEMSIZES
printf ( "basememsize %d\n", meminfo.basememsize );
printf ( "memsize %d\n", meminfo.memsize );
printf ( "Memory regions(%d):\n", meminfo.map_count );
for ( i = 0; i < meminfo.map_count; i++ ) {
unsigned long long r_start, r_end;
r_start = meminfo.map[i].addr;
r_end = r_start + meminfo.map[i].size;
printf ( "[%X%X, %X%X) type %d\n",
( unsigned long ) ( r_start >> 32 ),
( unsigned long ) r_start,
( unsigned long ) ( r_end >> 32 ),
( unsigned long ) r_end,
meminfo.map[i].type );
}
#endif
}
INIT_FN ( INIT_MEMSIZES, get_memsizes, NULL, NULL );

View File

@ -0,0 +1,23 @@
#ifndef _MEMMAP_H
#define _MEMMAP_H
#include <stdint.h>
/**
* @file
*
* Memory mapping
*
*/
/** A usable memory region */
struct memory_region {
/** Physical start address */
uint64_t start;
/** Physical end address */
uint64_t end;
};
extern void get_memmap ( struct memory_region *memmap, unsigned int entries );
#endif /* _MEMMAP_H */

View File

@ -1,6 +1,8 @@
#ifndef MEMSIZES_H
#define MEMSIZES_H
#warning "This header is no longer functional; use memmap.h instead"
/*
* These structures seem to be very i386 (and, in fact, PCBIOS)
* specific, so I've moved them out of etherboot.h.