2005-04-09 15:01:38 +00:00
|
|
|
#include "stdint.h"
|
|
|
|
#include "stddef.h"
|
|
|
|
#include "memsizes.h"
|
2005-03-08 18:53:11 +00:00
|
|
|
#include "etherboot.h"
|
2005-04-09 15:01:38 +00:00
|
|
|
#include "basemem.h"
|
2005-03-08 18:53:11 +00:00
|
|
|
|
|
|
|
/* Routines to allocate base memory in a BIOS-compatible way, by
|
|
|
|
* updating the Free Base Memory Size counter at 40:13h.
|
|
|
|
*
|
|
|
|
* Michael Brown <mbrown@fensystems.co.uk> (mcb30)
|
2005-04-09 15:01:38 +00:00
|
|
|
*
|
|
|
|
* 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.
|
2005-03-08 18:53:11 +00:00
|
|
|
*/
|
|
|
|
|
2005-04-09 15:01:38 +00:00
|
|
|
/* "fbms" is an alias to the BIOS FBMS counter at 40:13, and acts just
|
|
|
|
* like any other uint16_t. We can't be used under -DKEEP_IT_REAL
|
|
|
|
* anyway, so we may as well be efficient.
|
2005-03-08 18:53:11 +00:00
|
|
|
*/
|
2005-04-09 15:01:38 +00:00
|
|
|
#define fbms ( * ( ( uint16_t * ) phys_to_virt ( 0x413 ) ) )
|
|
|
|
#define FBMS_MAX ( 640 )
|
2005-03-08 18:53:11 +00:00
|
|
|
|
2005-04-09 15:01:38 +00:00
|
|
|
/* Local prototypes */
|
|
|
|
static void free_unused_base_memory ( void );
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Return amount of free base memory in bytes
|
|
|
|
*
|
2005-03-08 18:53:11 +00:00
|
|
|
*/
|
2005-04-18 09:17:27 +00:00
|
|
|
unsigned int get_free_base_memory ( void ) {
|
2005-04-09 15:01:38 +00:00
|
|
|
return fbms << 10;
|
2005-03-08 18:53:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* 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
|
|
|
|
* counter. Returns NULL if memory cannot be allocated.
|
2005-04-09 15:01:38 +00:00
|
|
|
*
|
2005-03-08 18:53:11 +00:00
|
|
|
*/
|
2005-04-09 15:01:38 +00:00
|
|
|
void * alloc_base_memory ( size_t size ) {
|
2005-04-18 09:17:27 +00:00
|
|
|
unsigned int size_kb = ( size + 1023 ) >> 10;
|
2005-04-09 15:01:38 +00:00
|
|
|
void *ptr;
|
2005-03-08 18:53:11 +00:00
|
|
|
|
2005-04-09 15:01:38 +00:00
|
|
|
DBG ( "Trying to allocate %d bytes of base memory from %d kB free\n",
|
|
|
|
size, fbms );
|
2005-03-08 18:53:11 +00:00
|
|
|
|
|
|
|
/* Free up any unused memory before we start */
|
|
|
|
free_unused_base_memory();
|
|
|
|
|
|
|
|
/* Check available base memory */
|
2005-04-09 15:01:38 +00:00
|
|
|
if ( size_kb > fbms ) {
|
|
|
|
DBG ( "Could not allocate %d kB of base memory: "
|
|
|
|
"only %d kB free\n", size_kb, fbms );
|
|
|
|
return NULL;
|
|
|
|
}
|
2005-03-08 18:53:11 +00:00
|
|
|
|
|
|
|
/* Reduce available base memory */
|
2005-04-09 15:01:38 +00:00
|
|
|
fbms -= size_kb;
|
2005-03-08 18:53:11 +00:00
|
|
|
|
|
|
|
/* Calculate address of memory allocated */
|
2005-04-09 15:01:38 +00:00
|
|
|
ptr = phys_to_virt ( fbms << 10 );
|
2005-03-08 18:53:11 +00:00
|
|
|
|
|
|
|
/* Zero out memory. We do this so that allocation of
|
|
|
|
* already-used space will show up in the form of a crash as
|
|
|
|
* soon as possible.
|
|
|
|
*
|
|
|
|
* Update: there's another reason for doing this. If we don't
|
|
|
|
* zero the contents, then they could still retain our "free
|
|
|
|
* block" markers and be liable to being freed whenever a
|
|
|
|
* base-memory allocation routine is next called.
|
|
|
|
*/
|
|
|
|
memset ( ptr, 0, size_kb << 10 );
|
|
|
|
|
2006-09-14 20:54:05 +00:00
|
|
|
DBG ( "Allocated %d kB of base memory at [%4.4lx:0000,%4.4lx:0000), "
|
|
|
|
"%d kB now free\n", size_kb,
|
|
|
|
( virt_to_phys ( ptr ) >> 4 ),
|
2005-04-10 16:58:19 +00:00
|
|
|
( ( virt_to_phys ( ptr ) + ( size_kb << 10 ) ) >> 4 ), fbms );
|
2005-03-08 18:53:11 +00:00
|
|
|
|
2005-04-09 15:01:38 +00:00
|
|
|
/* Update our memory map */
|
2005-03-08 18:53:11 +00:00
|
|
|
get_memsizes();
|
2005-04-09 15:01:38 +00:00
|
|
|
|
2005-03-08 18:53:11 +00:00
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
2005-04-09 15:01:38 +00:00
|
|
|
/* Free base memory allocated by alloc_base_memory. The BIOS provides
|
2005-03-08 18:53:11 +00:00
|
|
|
* nothing better than a LIFO mechanism for freeing memory (i.e. it
|
|
|
|
* just has the single "total free memory" counter), but we improve
|
2005-04-09 15:01:38 +00:00
|
|
|
* upon this slightly; as long as you free all the allocated blocks, it
|
2005-03-08 18:53:11 +00:00
|
|
|
* doesn't matter what order you free them in. (This will only work
|
2005-04-09 15:01:38 +00:00
|
|
|
* for blocks that are freed via free_base_memory()).
|
2005-03-08 18:53:11 +00:00
|
|
|
*
|
|
|
|
* Yes, it's annoying that you have to remember the size of the blocks
|
2005-04-09 15:01:38 +00:00
|
|
|
* you've allocated. However, since our granularity of allocation is
|
2005-03-08 18:53:11 +00:00
|
|
|
* 1K, the alternative is to risk wasting the occasional kB of base
|
|
|
|
* memory, which is a Bad Thing. Really, you should be using as
|
|
|
|
* little base memory as possible, so consider the awkwardness of the
|
|
|
|
* API to be a feature! :-)
|
2005-04-09 15:01:38 +00:00
|
|
|
*
|
2005-03-08 18:53:11 +00:00
|
|
|
*/
|
2005-04-09 15:01:38 +00:00
|
|
|
void free_base_memory ( void *ptr, size_t size ) {
|
2005-04-18 09:17:27 +00:00
|
|
|
unsigned int remainder = virt_to_phys ( ptr ) & 1023;
|
|
|
|
unsigned int size_kb = ( size + remainder + 1023 ) >> 10;
|
2005-04-09 15:01:38 +00:00
|
|
|
union free_base_memory_block *free_block =
|
|
|
|
( ( void * ) ( ptr - remainder ) );
|
2005-03-08 18:53:11 +00:00
|
|
|
|
|
|
|
if ( ( ptr == NULL ) || ( size == 0 ) ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2006-09-14 20:54:05 +00:00
|
|
|
DBG ( "Trying to free %d bytes base memory at %4.4lx:%4.4lx "
|
2005-04-10 16:58:19 +00:00
|
|
|
"from %d kB free\n", size,
|
2005-04-09 15:01:38 +00:00
|
|
|
( virt_to_phys ( ptr - remainder ) >> 4 ),
|
2005-04-10 16:58:19 +00:00
|
|
|
( virt_to_phys ( ptr - remainder ) & 0xf ) + remainder,
|
|
|
|
fbms );
|
2005-03-08 18:53:11 +00:00
|
|
|
|
|
|
|
/* Mark every kilobyte within this block as free. This is
|
|
|
|
* overkill for normal purposes, but helps when something has
|
|
|
|
* allocated base memory with a granularity finer than the
|
|
|
|
* BIOS granularity of 1kB. PXE ROMs tend to do this when
|
|
|
|
* they allocate their own memory. This method allows us to
|
|
|
|
* free their blocks (admittedly in a rather dangerous,
|
|
|
|
* tread-on-anything-either-side sort of way, but there's no
|
|
|
|
* other way to do it).
|
|
|
|
*
|
|
|
|
* Since we're marking every kB as free, there's actually no
|
|
|
|
* need for recording the size of the blocks. However, we
|
|
|
|
* keep this in so that debug messages are friendlier. It
|
|
|
|
* probably adds around 8 bytes to the overall code size.
|
|
|
|
*/
|
2005-04-09 15:01:38 +00:00
|
|
|
for ( ; size_kb > 0 ; free_block++, size_kb-- ) {
|
2005-03-08 18:53:11 +00:00
|
|
|
/* Mark this block as unused */
|
2005-05-23 23:47:54 +00:00
|
|
|
free_block->header.magic = FREE_BLOCK_MAGIC;
|
|
|
|
free_block->header.size_kb = size_kb;
|
2005-03-08 18:53:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Free up unused base memory */
|
|
|
|
free_unused_base_memory();
|
|
|
|
|
2005-04-09 15:01:38 +00:00
|
|
|
/* Update our memory map */
|
2005-03-08 18:53:11 +00:00
|
|
|
get_memsizes();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Do the actual freeing of memory. This is split out from
|
2005-04-09 15:01:38 +00:00
|
|
|
* free_base_memory() so that it may be called separately. It
|
2005-03-08 18:53:11 +00:00
|
|
|
* 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
|
|
|
|
* chance to free up our own blocks.
|
|
|
|
*/
|
|
|
|
static void free_unused_base_memory ( void ) {
|
2005-04-09 15:01:38 +00:00
|
|
|
union free_base_memory_block *free_block;
|
2005-03-08 18:53:11 +00:00
|
|
|
|
|
|
|
/* Try to release memory back to the BIOS. Free all
|
|
|
|
* consecutive blocks marked as free.
|
|
|
|
*/
|
|
|
|
while ( 1 ) {
|
|
|
|
/* Calculate address of next potential free block */
|
2005-04-09 15:01:38 +00:00
|
|
|
free_block = phys_to_virt ( fbms << 10 );
|
2005-03-08 18:53:11 +00:00
|
|
|
|
|
|
|
/* Stop processing if we're all the way up to 640K or
|
|
|
|
* if this is not a free block
|
|
|
|
*/
|
2005-04-09 15:01:38 +00:00
|
|
|
if ( ( fbms == FBMS_MAX ) ||
|
2005-05-23 23:47:54 +00:00
|
|
|
( free_block->header.magic != FREE_BLOCK_MAGIC ) ) {
|
2005-03-08 18:53:11 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Return memory to BIOS */
|
2005-05-23 23:47:54 +00:00
|
|
|
fbms += free_block->header.size_kb;
|
2005-03-08 18:53:11 +00:00
|
|
|
|
2006-09-14 20:54:05 +00:00
|
|
|
DBG ( "Freed %ld kB of base memory at [%4.4lx:0000,%4.4lx:0000), "
|
2005-04-10 16:58:19 +00:00
|
|
|
"%d kB now free\n",
|
2006-09-14 20:34:53 +00:00
|
|
|
free_block->header.size_kb,
|
2005-04-10 16:58:19 +00:00
|
|
|
( virt_to_phys ( free_block ) >> 4 ),
|
|
|
|
( ( virt_to_phys ( free_block ) +
|
2006-09-14 20:34:53 +00:00
|
|
|
( free_block->header.size_kb << 10 ) ) >> 4 ),
|
2005-04-10 16:58:19 +00:00
|
|
|
fbms );
|
2005-03-08 18:53:11 +00:00
|
|
|
|
2005-04-09 16:14:37 +00:00
|
|
|
/* Do not zero out the freed block, because it might
|
|
|
|
* be the one containing librm, in which case we're
|
|
|
|
* going to have severe problems the next time we use
|
|
|
|
* DBG() or, failing that, call get_memsizes().
|
2005-03-08 18:53:11 +00:00
|
|
|
*/
|
|
|
|
}
|
|
|
|
}
|