mirror of https://github.com/ipxe/ipxe.git
Rewritten in a much saner way, now that we don't have to worry about
continually reallocating the real-mode stack.pull/1/head
parent
0574136c6d
commit
48feb91a40
|
@ -1,112 +1,83 @@
|
||||||
#ifdef PCBIOS
|
#include "stdint.h"
|
||||||
|
#include "stddef.h"
|
||||||
|
#include "memsizes.h"
|
||||||
#include "etherboot.h"
|
#include "etherboot.h"
|
||||||
#include "realmode.h" /* for real_mode_stack */
|
#include "basemem.h"
|
||||||
|
|
||||||
/* Routines to allocate base memory in a BIOS-compatible way, by
|
/* Routines to allocate base memory in a BIOS-compatible way, by
|
||||||
* updating the Free Base Memory Size counter at 40:13h.
|
* updating the Free Base Memory Size counter at 40:13h.
|
||||||
*
|
*
|
||||||
* Michael Brown <mbrown@fensystems.co.uk> (mcb30)
|
* Michael Brown <mbrown@fensystems.co.uk> (mcb30)
|
||||||
* $Id$
|
*
|
||||||
|
* We no longer have anything to do with the real-mode stack. The
|
||||||
|
* only code that can end up creating a huge bubble of wasted base
|
||||||
|
* memory is the UNDI driver, so we make it the responsibility of the
|
||||||
|
* UNDI driver to reallocate the real-mode stack if required.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#define fbms ( ( uint16_t * ) phys_to_virt ( 0x413 ) )
|
/* "fbms" is an alias to the BIOS FBMS counter at 40:13, and acts just
|
||||||
#define BASE_MEMORY_MAX ( 640 )
|
* like any other uint16_t. We can't be used under -DKEEP_IT_REAL
|
||||||
|
* anyway, so we may as well be efficient.
|
||||||
|
*/
|
||||||
|
#define fbms ( * ( ( uint16_t * ) phys_to_virt ( 0x413 ) ) )
|
||||||
|
#define FBMS_MAX ( 640 )
|
||||||
|
|
||||||
|
/* Structure that we use to represent a free block of base memory
|
||||||
|
*/
|
||||||
#define FREE_BLOCK_MAGIC ( ('!'<<0) + ('F'<<8) + ('R'<<16) + ('E'<<24) )
|
#define FREE_BLOCK_MAGIC ( ('!'<<0) + ('F'<<8) + ('R'<<16) + ('E'<<24) )
|
||||||
#define FREE_BASE_MEMORY ( (uint32_t) ( *fbms << 10 ) )
|
union free_base_memory_block {
|
||||||
|
struct {
|
||||||
|
uint32_t magic;
|
||||||
|
uint16_t size_kb;
|
||||||
|
};
|
||||||
|
char bytes[1024];
|
||||||
|
};
|
||||||
|
|
||||||
/* Prototypes */
|
/* Local prototypes */
|
||||||
void * _allot_base_memory ( size_t size );
|
static void free_unused_base_memory ( void );
|
||||||
void _forget_base_memory ( void *ptr, size_t size );
|
|
||||||
|
|
||||||
typedef struct free_base_memory_block {
|
#undef DBG
|
||||||
uint32_t magic;
|
#ifdef DEBUG_BASEMEM
|
||||||
uint16_t size_kb;
|
#define DBG(...) printf ( __VA_ARGS__ )
|
||||||
} free_base_memory_block_t;
|
#else
|
||||||
|
#define DBG(...)
|
||||||
|
#endif
|
||||||
|
|
||||||
/* Return amount of free base memory in bytes
|
/*
|
||||||
|
* Return amount of free base memory in bytes
|
||||||
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
uint32_t get_free_base_memory ( void ) {
|
uint32_t get_free_base_memory ( void ) {
|
||||||
return FREE_BASE_MEMORY;
|
return fbms << 10;
|
||||||
}
|
|
||||||
|
|
||||||
/* Start of our image in base memory.
|
|
||||||
*/
|
|
||||||
#define __text16_nocompress __attribute__ ((section (".text16.nocompress")))
|
|
||||||
uint32_t image_basemem __text16_nocompress = 0;
|
|
||||||
uint32_t image_basemem_size __text16_nocompress = 0;
|
|
||||||
|
|
||||||
/* Allot/free the real-mode stack
|
|
||||||
*/
|
|
||||||
|
|
||||||
void allot_real_mode_stack ( void )
|
|
||||||
{
|
|
||||||
void *new_real_mode_stack;
|
|
||||||
|
|
||||||
if ( lock_real_mode_stack )
|
|
||||||
return;
|
|
||||||
|
|
||||||
/* This is evil hack.
|
|
||||||
* Until we have a real_mode stack use 0x7c00.
|
|
||||||
* Except for 0 - 0x600 membory below 0x7c00 is hardly every used.
|
|
||||||
* This stack should never be used unless the stack allocation fails,
|
|
||||||
* or if someone has placed a print statement in a dangerous location.
|
|
||||||
*/
|
|
||||||
if (!real_mode_stack) {
|
|
||||||
real_mode_stack = 0x7c00;
|
|
||||||
}
|
|
||||||
new_real_mode_stack = _allot_base_memory ( real_mode_stack_size );
|
|
||||||
if ( ! new_real_mode_stack ) {
|
|
||||||
printf ( "FATAL: No real-mode stack\n" );
|
|
||||||
while ( 1 ) {};
|
|
||||||
}
|
|
||||||
real_mode_stack = virt_to_phys ( new_real_mode_stack );
|
|
||||||
get_memsizes();
|
|
||||||
}
|
|
||||||
|
|
||||||
void forget_real_mode_stack ( void )
|
|
||||||
{
|
|
||||||
if ( lock_real_mode_stack )
|
|
||||||
return;
|
|
||||||
|
|
||||||
if ( real_mode_stack) {
|
|
||||||
_forget_base_memory ( phys_to_virt(real_mode_stack),
|
|
||||||
real_mode_stack_size );
|
|
||||||
/* get_memsizes() uses the real_mode stack we just freed
|
|
||||||
* for it's BIOS calls.
|
|
||||||
*/
|
|
||||||
get_memsizes();
|
|
||||||
real_mode_stack = 0;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Allocate N bytes of base memory. Amount allocated will be rounded
|
/* Allocate N bytes of base memory. Amount allocated will be rounded
|
||||||
* up to the nearest kB, since that's the granularity of the BIOS FBMS
|
* up to the nearest kB, since that's the granularity of the BIOS FBMS
|
||||||
* counter. Returns NULL if memory cannot be allocated.
|
* counter. Returns NULL if memory cannot be allocated.
|
||||||
|
*
|
||||||
*/
|
*/
|
||||||
|
void * alloc_base_memory ( size_t size ) {
|
||||||
static void * _allot_base_memory ( size_t size )
|
|
||||||
{
|
|
||||||
uint16_t size_kb = ( size + 1023 ) >> 10;
|
uint16_t size_kb = ( size + 1023 ) >> 10;
|
||||||
void *ptr = NULL;
|
void *ptr;
|
||||||
|
|
||||||
#ifdef DEBUG_BASEMEM
|
DBG ( "Trying to allocate %d bytes of base memory from %d kB free\n",
|
||||||
printf ( "Trying to allocate %d kB of base memory from %d kB free\n",
|
size, fbms );
|
||||||
size_kb, *fbms );
|
|
||||||
#endif
|
|
||||||
|
|
||||||
/* Free up any unused memory before we start */
|
/* Free up any unused memory before we start */
|
||||||
free_unused_base_memory();
|
free_unused_base_memory();
|
||||||
|
|
||||||
/* Check available base memory */
|
/* Check available base memory */
|
||||||
if ( size_kb > *fbms ) { return NULL; }
|
if ( size_kb > fbms ) {
|
||||||
|
DBG ( "Could not allocate %d kB of base memory: "
|
||||||
|
"only %d kB free\n", size_kb, fbms );
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
/* Reduce available base memory */
|
/* Reduce available base memory */
|
||||||
*fbms -= size_kb;
|
fbms -= size_kb;
|
||||||
|
|
||||||
/* Calculate address of memory allocated */
|
/* Calculate address of memory allocated */
|
||||||
ptr = phys_to_virt ( FREE_BASE_MEMORY );
|
ptr = phys_to_virt ( fbms << 10 );
|
||||||
|
|
||||||
/* Zero out memory. We do this so that allocation of
|
/* Zero out memory. We do this so that allocation of
|
||||||
* already-used space will show up in the form of a crash as
|
* already-used space will show up in the form of a crash as
|
||||||
|
@ -119,63 +90,43 @@ static void * _allot_base_memory ( size_t size )
|
||||||
*/
|
*/
|
||||||
memset ( ptr, 0, size_kb << 10 );
|
memset ( ptr, 0, size_kb << 10 );
|
||||||
|
|
||||||
#ifdef DEBUG_BASEMEM
|
DBG ( "Allocated %d kB of base memory at [%hx:0000,%hx:0000)\n",
|
||||||
printf ( "Allocated %d kB at [%x,%x)\n", size_kb,
|
size_kb, ( fbms << 6 ), ( ( fbms + size_kb ) << 6 ) );
|
||||||
virt_to_phys ( ptr ),
|
|
||||||
virt_to_phys ( ptr ) + size_kb * 1024 );
|
|
||||||
#endif
|
|
||||||
|
|
||||||
return ptr;
|
/* Update our memory map */
|
||||||
}
|
|
||||||
|
|
||||||
void * allot_base_memory ( size_t size )
|
|
||||||
{
|
|
||||||
void *ptr;
|
|
||||||
|
|
||||||
/* Free real-mode stack, allocate memory, reallocate real-mode
|
|
||||||
* stack.
|
|
||||||
*/
|
|
||||||
forget_real_mode_stack();
|
|
||||||
ptr = _allot_base_memory ( size );
|
|
||||||
get_memsizes();
|
get_memsizes();
|
||||||
|
|
||||||
return ptr;
|
return ptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Free base memory allocated by allot_base_memory. The BIOS provides
|
/* Free base memory allocated by alloc_base_memory. The BIOS provides
|
||||||
* nothing better than a LIFO mechanism for freeing memory (i.e. it
|
* nothing better than a LIFO mechanism for freeing memory (i.e. it
|
||||||
* just has the single "total free memory" counter), but we improve
|
* just has the single "total free memory" counter), but we improve
|
||||||
* upon this slightly; as long as you free all the allotted blocks, it
|
* upon this slightly; as long as you free all the allocated blocks, it
|
||||||
* doesn't matter what order you free them in. (This will only work
|
* doesn't matter what order you free them in. (This will only work
|
||||||
* for blocks that are freed via forget_base_memory()).
|
* for blocks that are freed via free_base_memory()).
|
||||||
*
|
*
|
||||||
* Yes, it's annoying that you have to remember the size of the blocks
|
* Yes, it's annoying that you have to remember the size of the blocks
|
||||||
* you've allotted. However, since our granularity of allocation is
|
* you've allocated. However, since our granularity of allocation is
|
||||||
* 1K, the alternative is to risk wasting the occasional kB of base
|
* 1K, the alternative is to risk wasting the occasional kB of base
|
||||||
* memory, which is a Bad Thing. Really, you should be using as
|
* memory, which is a Bad Thing. Really, you should be using as
|
||||||
* little base memory as possible, so consider the awkwardness of the
|
* little base memory as possible, so consider the awkwardness of the
|
||||||
* API to be a feature! :-)
|
* API to be a feature! :-)
|
||||||
|
*
|
||||||
*/
|
*/
|
||||||
|
void free_base_memory ( void *ptr, size_t size ) {
|
||||||
static void _forget_base_memory ( void *ptr, size_t size )
|
uint16_t remainder = virt_to_phys ( ptr ) & 1023;
|
||||||
{
|
|
||||||
uint16_t remainder = virt_to_phys(ptr) & 1023;
|
|
||||||
uint16_t size_kb = ( size + remainder + 1023 ) >> 10;
|
uint16_t size_kb = ( size + remainder + 1023 ) >> 10;
|
||||||
free_base_memory_block_t *free_block =
|
union free_base_memory_block *free_block =
|
||||||
( free_base_memory_block_t * ) ( ptr - remainder );
|
( ( void * ) ( ptr - remainder ) );
|
||||||
|
|
||||||
if ( ( ptr == NULL ) || ( size == 0 ) ) {
|
if ( ( ptr == NULL ) || ( size == 0 ) ) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef DEBUG_BASEMEM
|
DBG ( "Trying to free %d bytes base memory at %hx:%hx\n", size,
|
||||||
printf ( "Trying to free %d bytes base memory at 0x%x\n",
|
( virt_to_phys ( ptr - remainder ) >> 4 ),
|
||||||
size, virt_to_phys ( ptr ) );
|
( virt_to_phys ( ptr - remainder ) & 0xf ) + remainder );
|
||||||
if ( remainder > 0 ) {
|
|
||||||
printf ( "WARNING: destructively expanding free block "
|
|
||||||
"downwards to 0x%x\n",
|
|
||||||
virt_to_phys ( ptr - remainder ) );
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
/* Mark every kilobyte within this block as free. This is
|
/* Mark every kilobyte within this block as free. This is
|
||||||
* overkill for normal purposes, but helps when something has
|
* overkill for normal purposes, but helps when something has
|
||||||
|
@ -191,62 +142,51 @@ static void _forget_base_memory ( void *ptr, size_t size )
|
||||||
* keep this in so that debug messages are friendlier. It
|
* keep this in so that debug messages are friendlier. It
|
||||||
* probably adds around 8 bytes to the overall code size.
|
* probably adds around 8 bytes to the overall code size.
|
||||||
*/
|
*/
|
||||||
while ( size_kb > 0 ) {
|
for ( ; size_kb > 0 ; free_block++, size_kb-- ) {
|
||||||
/* Mark this block as unused */
|
/* Mark this block as unused */
|
||||||
free_block->magic = FREE_BLOCK_MAGIC;
|
free_block->magic = FREE_BLOCK_MAGIC;
|
||||||
free_block->size_kb = size_kb;
|
free_block->size_kb = size_kb;
|
||||||
/* Move up by 1 kB */
|
|
||||||
free_block = (void *)(((char *)free_block) + (1 << 10));
|
|
||||||
size_kb--;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Free up unused base memory */
|
/* Free up unused base memory */
|
||||||
free_unused_base_memory();
|
free_unused_base_memory();
|
||||||
}
|
|
||||||
|
|
||||||
void forget_base_memory ( void *ptr, size_t size )
|
/* Update our memory map */
|
||||||
{
|
|
||||||
/* Free memory, free real-mode stack, re-allocate real-mode
|
|
||||||
* stack. Do this so that we don't end up wasting a huge
|
|
||||||
* block of memory trapped behind the real-mode stack.
|
|
||||||
*/
|
|
||||||
_forget_base_memory ( ptr, size );
|
|
||||||
forget_real_mode_stack();
|
|
||||||
get_memsizes();
|
get_memsizes();
|
||||||
|
|
||||||
|
DBG ( "%d kB of base memory now free\n", fbms );
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Do the actual freeing of memory. This is split out from
|
/* Do the actual freeing of memory. This is split out from
|
||||||
* forget_base_memory() so that it may be called separately. It
|
* free_base_memory() so that it may be called separately. It
|
||||||
* should be called whenever base memory is deallocated by an external
|
* should be called whenever base memory is deallocated by an external
|
||||||
* entity (if we can detect that it has done so) so that we get the
|
* entity (if we can detect that it has done so) so that we get the
|
||||||
* chance to free up our own blocks.
|
* chance to free up our own blocks.
|
||||||
*/
|
*/
|
||||||
static void free_unused_base_memory ( void ) {
|
static void free_unused_base_memory ( void ) {
|
||||||
free_base_memory_block_t *free_block = NULL;
|
union free_base_memory_block *free_block;
|
||||||
|
|
||||||
/* Try to release memory back to the BIOS. Free all
|
/* Try to release memory back to the BIOS. Free all
|
||||||
* consecutive blocks marked as free.
|
* consecutive blocks marked as free.
|
||||||
*/
|
*/
|
||||||
while ( 1 ) {
|
while ( 1 ) {
|
||||||
/* Calculate address of next potential free block */
|
/* Calculate address of next potential free block */
|
||||||
free_block = ( free_base_memory_block_t * )
|
free_block = phys_to_virt ( fbms << 10 );
|
||||||
phys_to_virt ( FREE_BASE_MEMORY );
|
|
||||||
|
|
||||||
/* Stop processing if we're all the way up to 640K or
|
/* Stop processing if we're all the way up to 640K or
|
||||||
* if this is not a free block
|
* if this is not a free block
|
||||||
*/
|
*/
|
||||||
if ( ( *fbms == BASE_MEMORY_MAX ) ||
|
if ( ( fbms == FBMS_MAX ) ||
|
||||||
( free_block->magic != FREE_BLOCK_MAGIC ) ) {
|
( free_block->magic != FREE_BLOCK_MAGIC ) ) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Return memory to BIOS */
|
/* Return memory to BIOS */
|
||||||
*fbms += free_block->size_kb;
|
fbms += free_block->size_kb;
|
||||||
|
|
||||||
#ifdef DEBUG_BASEMEM
|
DBG ( "Freed %d kB of base memory at [%hx:0000,%hx:0000)\n",
|
||||||
printf ( "Freed %d kB base memory, %d kB now free\n",
|
free_block->size_kb, ( fbms << 6 ),
|
||||||
free_block->size_kb, *fbms );
|
( fbms + free_block->size_kb ) << 6 );
|
||||||
#endif
|
|
||||||
|
|
||||||
/* Zero out freed block. We do this in case
|
/* Zero out freed block. We do this in case
|
||||||
* the block contained any structures that
|
* the block contained any structures that
|
||||||
|
@ -254,64 +194,6 @@ static void free_unused_base_memory ( void ) {
|
||||||
* memory.
|
* memory.
|
||||||
*/
|
*/
|
||||||
memset ( free_block, 0, free_block->size_kb << 10 );
|
memset ( free_block, 0, free_block->size_kb << 10 );
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Free base memory used by the prefix. Called once at start of
|
|
||||||
* Etherboot by arch_main().
|
|
||||||
*/
|
|
||||||
void forget_prefix_base_memory ( void )
|
|
||||||
{
|
|
||||||
/* runtime_start_kb is _text rounded down to a physical kB boundary */
|
|
||||||
uint32_t runtime_start_kb = virt_to_phys(_text) & ~0x3ff;
|
|
||||||
/* prefix_size_kb is the prefix size excluding any portion
|
|
||||||
* that overlaps into the first kB used by the runtime image
|
|
||||||
*/
|
|
||||||
uint32_t prefix_size_kb = runtime_start_kb - image_basemem;
|
|
||||||
|
|
||||||
#ifdef DEBUG_BASEMEM
|
|
||||||
printf ( "Attempting to free base memory used by prefix\n" );
|
|
||||||
#endif
|
|
||||||
|
|
||||||
/* If the decompressor is in allocated base memory
|
|
||||||
* *and* the Etherboot text is in base
|
|
||||||
* memory, then free the decompressor.
|
|
||||||
*/
|
|
||||||
if ( ( image_basemem >= FREE_BASE_MEMORY ) &&
|
|
||||||
( runtime_start_kb >= FREE_BASE_MEMORY ) &&
|
|
||||||
( runtime_start_kb <= ( BASE_MEMORY_MAX << 10 ) ) )
|
|
||||||
{
|
|
||||||
forget_base_memory ( phys_to_virt ( image_basemem ),
|
|
||||||
prefix_size_kb );
|
|
||||||
/* Update image_basemem and image_basemem_size to
|
|
||||||
* indicate that our allocation now starts with _text
|
|
||||||
*/
|
|
||||||
image_basemem = runtime_start_kb;
|
|
||||||
image_basemem_size -= prefix_size_kb;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Free base memory used by the runtime image. Called after
|
|
||||||
* relocation by arch_relocated_from().
|
|
||||||
*/
|
|
||||||
void forget_runtime_base_memory ( unsigned long old_addr )
|
|
||||||
{
|
|
||||||
/* text_start_kb is old _text rounded down to a physical KB boundary */
|
|
||||||
uint32_t old_text_start_kb = old_addr & ~0x3ff;
|
|
||||||
|
|
||||||
#ifdef DEBUG_BASEMEM
|
|
||||||
printf ( "Attempting to free base memory used by runtime image\n" );
|
|
||||||
#endif
|
|
||||||
|
|
||||||
if ( ( image_basemem >= FREE_BASE_MEMORY ) &&
|
|
||||||
( image_basemem == old_text_start_kb ) )
|
|
||||||
{
|
|
||||||
forget_base_memory ( phys_to_virt ( image_basemem ),
|
|
||||||
image_basemem_size );
|
|
||||||
/* Update image_basemem to show no longer in use */
|
|
||||||
image_basemem = 0;
|
|
||||||
image_basemem_size = 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#endif /* PCBIOS */
|
|
||||||
|
|
Loading…
Reference in New Issue