2008-09-10 15:04:24 +02:00
|
|
|
|
|
|
|
#include <types.h>
|
|
|
|
#include <core.h>
|
|
|
|
#include <spinlock.h>
|
|
|
|
#include <link.h>
|
|
|
|
#include <mm.h>
|
|
|
|
|
2009-04-23 14:26:47 +02:00
|
|
|
#define PG_DEMAND 0x400
|
2008-09-10 15:04:24 +02:00
|
|
|
|
2009-04-23 14:26:47 +02:00
|
|
|
#define HF_WIDTH 16
|
|
|
|
#define HF_SIZE (1 << HF_WIDTH)
|
2008-10-27 21:47:58 +01:00
|
|
|
|
2009-04-23 14:26:47 +02:00
|
|
|
#define BUDDY_SYSTEM_INNER_BLOCK 0xff
|
2008-10-27 21:47:58 +01:00
|
|
|
|
2009-04-23 14:26:47 +02:00
|
|
|
static zone_t z_heap;
|
2008-09-10 15:04:24 +02:00
|
|
|
|
2009-04-23 14:26:47 +02:00
|
|
|
static link_t shared_mmap;
|
2008-10-29 16:55:39 +01:00
|
|
|
|
2008-09-10 15:04:24 +02:00
|
|
|
|
2009-04-23 14:26:47 +02:00
|
|
|
#define heap_index( frame ) \
|
|
|
|
(index_t)( (frame) - z_heap.frames)
|
2008-10-27 21:47:58 +01:00
|
|
|
|
2009-04-23 14:26:47 +02:00
|
|
|
#define heap_index_abs( frame ) \
|
|
|
|
(index_t)( (frame) - z_heap.frames)
|
2008-09-10 15:04:24 +02:00
|
|
|
|
2008-10-27 21:47:58 +01:00
|
|
|
|
2009-04-23 14:26:47 +02:00
|
|
|
static __inline void frame_initialize(frame_t *frame)
|
|
|
|
{
|
|
|
|
frame->refcount = 1;
|
|
|
|
frame->buddy_order = 0;
|
|
|
|
}
|
2008-09-10 15:04:24 +02:00
|
|
|
|
2009-04-23 14:26:47 +02:00
|
|
|
#define buddy_get_order( block) \
|
|
|
|
((frame_t*)(block))->buddy_order
|
2008-09-10 15:04:24 +02:00
|
|
|
|
|
|
|
|
2009-04-23 14:26:47 +02:00
|
|
|
#define buddy_set_order( block, order) \
|
|
|
|
((frame_t*)(block))->buddy_order = (order)
|
2008-10-29 16:55:39 +01:00
|
|
|
|
2009-04-23 14:26:47 +02:00
|
|
|
#define buddy_mark_busy( block ) \
|
|
|
|
((frame_t*)(block))->refcount = 1
|
2008-10-29 16:55:39 +01:00
|
|
|
|
2008-09-10 15:04:24 +02:00
|
|
|
|
2009-04-23 14:26:47 +02:00
|
|
|
static __inline link_t * buddy_bisect(link_t *block)
|
2008-09-10 15:04:24 +02:00
|
|
|
{
|
2009-04-23 14:26:47 +02:00
|
|
|
frame_t *frame_l, *frame_r;
|
2008-09-10 15:04:24 +02:00
|
|
|
|
2009-04-23 14:26:47 +02:00
|
|
|
frame_l = (frame_t*)block;
|
|
|
|
frame_r = (frame_l + (1 << (frame_l->buddy_order - 1)));
|
2008-09-10 15:04:24 +02:00
|
|
|
|
2009-04-23 14:26:47 +02:00
|
|
|
return &frame_r->buddy_link;
|
|
|
|
}
|
2008-10-29 16:55:39 +01:00
|
|
|
|
2009-04-23 14:26:47 +02:00
|
|
|
static __inline link_t *buddy_coalesce(link_t *block_1, link_t *block_2)
|
|
|
|
{
|
|
|
|
frame_t *frame1, *frame2;
|
2008-10-27 21:47:58 +01:00
|
|
|
|
2009-04-23 14:26:47 +02:00
|
|
|
frame1 = (frame_t*)block_1;
|
|
|
|
frame2 = (frame_t*)block_2;
|
2008-10-27 21:47:58 +01:00
|
|
|
|
2009-04-23 14:26:47 +02:00
|
|
|
return frame1 < frame2 ? block_1 : block_2;
|
|
|
|
}
|
2008-09-10 15:04:24 +02:00
|
|
|
|
|
|
|
|
2009-04-23 14:26:47 +02:00
|
|
|
#define IS_BUDDY_LEFT_BLOCK_ABS(frame) \
|
|
|
|
(((heap_index_abs((frame)) >> (frame)->buddy_order) & 0x1) == 0)
|
2008-09-10 15:04:24 +02:00
|
|
|
|
2009-04-23 14:26:47 +02:00
|
|
|
#define IS_BUDDY_RIGHT_BLOCK_ABS(frame) \
|
|
|
|
(((heap_index_abs((frame)) >> (frame)->buddy_order) & 0x1) == 1)
|
2008-09-10 15:04:24 +02:00
|
|
|
|
|
|
|
|
2009-04-23 14:26:47 +02:00
|
|
|
static link_t *find_buddy(link_t *block)
|
2008-09-10 15:04:24 +02:00
|
|
|
{
|
2009-04-23 14:26:47 +02:00
|
|
|
frame_t *frame;
|
|
|
|
index_t index;
|
|
|
|
u32_t is_left, is_right;
|
2008-09-10 15:04:24 +02:00
|
|
|
|
2009-04-23 14:26:47 +02:00
|
|
|
frame = (frame_t*)block;
|
|
|
|
// ASSERT(IS_BUDDY_ORDER_OK(frame_index_abs(zone, frame),frame->buddy_order));
|
2008-09-10 15:04:24 +02:00
|
|
|
|
2009-04-23 14:26:47 +02:00
|
|
|
is_left = IS_BUDDY_LEFT_BLOCK_ABS( frame);
|
|
|
|
is_right = IS_BUDDY_RIGHT_BLOCK_ABS( frame);
|
2008-09-10 15:04:24 +02:00
|
|
|
|
2009-04-23 14:26:47 +02:00
|
|
|
// ASSERT(is_left ^ is_right);
|
2008-09-10 15:04:24 +02:00
|
|
|
|
2009-04-23 14:26:47 +02:00
|
|
|
if (is_left) {
|
|
|
|
index = (heap_index(frame)) + (1 << frame->buddy_order);
|
2008-10-30 07:30:13 +01:00
|
|
|
}
|
2009-04-23 14:26:47 +02:00
|
|
|
else { /* if (is_right) */
|
|
|
|
index = (heap_index(frame)) - (1 << frame->buddy_order);
|
|
|
|
};
|
2008-09-10 15:04:24 +02:00
|
|
|
|
|
|
|
|
2009-04-23 14:26:47 +02:00
|
|
|
if ( index < z_heap.count)
|
|
|
|
{
|
|
|
|
if (z_heap.frames[index].buddy_order == frame->buddy_order &&
|
|
|
|
z_heap.frames[index].refcount == 0) {
|
|
|
|
return &z_heap.frames[index].buddy_link;
|
|
|
|
}
|
|
|
|
}
|
2008-09-10 15:04:24 +02:00
|
|
|
|
2009-04-23 14:26:47 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
2008-09-10 15:04:24 +02:00
|
|
|
|
|
|
|
|
2009-04-23 14:26:47 +02:00
|
|
|
static void buddy_system_free(link_t *block)
|
|
|
|
{
|
|
|
|
link_t *buddy, *hlp;
|
|
|
|
u32_t i;
|
2008-09-10 15:04:24 +02:00
|
|
|
|
2009-04-23 14:26:47 +02:00
|
|
|
/*
|
|
|
|
* Determine block's order.
|
|
|
|
*/
|
|
|
|
i = buddy_get_order(block);
|
2008-09-10 15:04:24 +02:00
|
|
|
|
2009-04-23 14:26:47 +02:00
|
|
|
ASSERT(i <= z_heap.max_order);
|
2008-10-27 21:47:58 +01:00
|
|
|
|
2009-04-23 14:26:47 +02:00
|
|
|
if (i != z_heap.max_order)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* See if there is any buddy in the list of order i.
|
|
|
|
*/
|
|
|
|
buddy = find_buddy( block );
|
|
|
|
if (buddy)
|
|
|
|
{
|
|
|
|
|
|
|
|
ASSERT(buddy_get_order(buddy) == i);
|
|
|
|
/*
|
|
|
|
* Remove buddy from the list of order i.
|
|
|
|
*/
|
|
|
|
list_remove(buddy);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Invalidate order of both block and buddy.
|
|
|
|
*/
|
|
|
|
buddy_set_order(block, BUDDY_SYSTEM_INNER_BLOCK);
|
|
|
|
buddy_set_order(buddy, BUDDY_SYSTEM_INNER_BLOCK);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Coalesce block and buddy into one block.
|
|
|
|
*/
|
|
|
|
hlp = buddy_coalesce( block, buddy );
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Set order of the coalesced block to i + 1.
|
|
|
|
*/
|
|
|
|
buddy_set_order(hlp, i + 1);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Recursively add the coalesced block to the list of order i + 1.
|
|
|
|
*/
|
|
|
|
buddy_system_free( hlp );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Insert block into the list of order i.
|
|
|
|
*/
|
|
|
|
list_append(block, &z_heap.order[i]);
|
2008-09-10 15:04:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-04-23 14:26:47 +02:00
|
|
|
static link_t* buddy_system_alloc( u32_t i)
|
|
|
|
{
|
|
|
|
link_t *res, *hlp;
|
|
|
|
|
|
|
|
ASSERT(i <= z_heap.max_order);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If the list of order i is not empty,
|
|
|
|
* the request can be immediatelly satisfied.
|
|
|
|
*/
|
|
|
|
if (!list_empty(&z_heap.order[i])) {
|
|
|
|
res = z_heap.order[i].next;
|
|
|
|
list_remove(res);
|
|
|
|
buddy_mark_busy(res);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* If order i is already the maximal order,
|
|
|
|
* the request cannot be satisfied.
|
|
|
|
*/
|
|
|
|
if (i == z_heap.max_order)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Try to recursively satisfy the request from higher order lists.
|
|
|
|
*/
|
|
|
|
hlp = buddy_system_alloc( i + 1 );
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The request could not be satisfied
|
|
|
|
* from higher order lists.
|
|
|
|
*/
|
|
|
|
if (!hlp)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
res = hlp;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Bisect the block and set order of both of its parts to i.
|
|
|
|
*/
|
|
|
|
hlp = buddy_bisect( res );
|
|
|
|
|
|
|
|
buddy_set_order(res, i);
|
|
|
|
buddy_set_order(hlp, i);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Return the other half to buddy system. Mark the first part
|
|
|
|
* full, so that it won't coalesce again.
|
|
|
|
*/
|
|
|
|
buddy_mark_busy(res);
|
|
|
|
buddy_system_free( hlp );
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
2008-09-10 15:04:24 +02:00
|
|
|
|
|
|
|
|
2009-04-23 14:26:47 +02:00
|
|
|
int __fastcall init_heap(addr_t start, size_t size)
|
|
|
|
{
|
|
|
|
count_t i;
|
|
|
|
count_t count;
|
2008-09-10 15:04:24 +02:00
|
|
|
|
2009-04-23 14:26:47 +02:00
|
|
|
count = size >> HF_WIDTH;
|
2008-09-10 15:04:24 +02:00
|
|
|
|
2009-04-23 14:26:47 +02:00
|
|
|
ASSERT( start != 0);
|
|
|
|
ASSERT( count != 0);
|
2008-09-10 15:04:24 +02:00
|
|
|
|
2009-04-23 14:26:47 +02:00
|
|
|
spinlock_initialize(&z_heap.lock);
|
2008-09-11 22:26:49 +02:00
|
|
|
|
2009-04-23 14:26:47 +02:00
|
|
|
z_heap.base = start >> HF_WIDTH;
|
|
|
|
z_heap.count = count;
|
|
|
|
z_heap.free_count = count;
|
|
|
|
z_heap.busy_count = 0;
|
2008-10-27 21:47:58 +01:00
|
|
|
|
2009-04-23 14:26:47 +02:00
|
|
|
z_heap.max_order = fnzb(count);
|
2008-10-27 21:47:58 +01:00
|
|
|
|
2009-04-23 14:26:47 +02:00
|
|
|
DBG("create heap zone: base %x count %x\n", start, count);
|
2008-10-27 21:47:58 +01:00
|
|
|
|
2009-04-23 14:26:47 +02:00
|
|
|
ASSERT(z_heap.max_order < BUDDY_SYSTEM_INNER_BLOCK);
|
2008-10-27 21:47:58 +01:00
|
|
|
|
2009-04-23 14:26:47 +02:00
|
|
|
for (i = 0; i <= z_heap.max_order; i++)
|
|
|
|
list_initialize(&z_heap.order[i]);
|
2008-10-27 21:47:58 +01:00
|
|
|
|
|
|
|
|
2009-04-23 14:26:47 +02:00
|
|
|
DBG("count %d frame_t %d page_size %d\n",
|
|
|
|
count, sizeof(frame_t), PAGE_SIZE);
|
2008-10-27 21:47:58 +01:00
|
|
|
|
2009-04-23 14:26:47 +02:00
|
|
|
z_heap.frames = (frame_t *)PA2KA(frame_alloc( (count*sizeof(frame_t)) >> PAGE_WIDTH ));
|
2008-10-27 21:47:58 +01:00
|
|
|
|
2008-09-11 22:26:49 +02:00
|
|
|
|
2009-04-23 14:26:47 +02:00
|
|
|
if( z_heap.frames == 0 )
|
|
|
|
return 0;
|
2008-10-29 16:55:39 +01:00
|
|
|
|
2008-09-12 12:56:47 +02:00
|
|
|
|
2009-04-23 14:26:47 +02:00
|
|
|
for (i = 0; i < count; i++) {
|
|
|
|
z_heap.frames[i].buddy_order=0;
|
|
|
|
z_heap.frames[i].parent = NULL;
|
|
|
|
z_heap.frames[i].refcount=1;
|
|
|
|
}
|
2008-09-12 12:56:47 +02:00
|
|
|
|
2009-04-23 14:26:47 +02:00
|
|
|
for (i = 0; i < count; i++)
|
2008-10-27 21:47:58 +01:00
|
|
|
{
|
2009-04-23 14:26:47 +02:00
|
|
|
z_heap.frames[i].refcount = 0;
|
|
|
|
buddy_system_free(&z_heap.frames[i].buddy_link);
|
|
|
|
}
|
2008-09-10 15:04:24 +02:00
|
|
|
|
2009-04-23 14:26:47 +02:00
|
|
|
list_initialize(&shared_mmap);
|
2008-09-10 15:04:24 +02:00
|
|
|
|
2009-04-23 14:26:47 +02:00
|
|
|
return 1;
|
|
|
|
}
|
2008-09-10 15:04:24 +02:00
|
|
|
|
2009-04-23 14:26:47 +02:00
|
|
|
addr_t __fastcall mem_alloc(size_t size, u32_t flags)
|
|
|
|
{
|
|
|
|
eflags_t efl;
|
|
|
|
addr_t heap = 0;
|
2008-09-11 22:26:49 +02:00
|
|
|
|
2009-04-23 14:26:47 +02:00
|
|
|
count_t order;
|
|
|
|
frame_t *frame;
|
|
|
|
index_t v;
|
|
|
|
int i;
|
|
|
|
mmap_t *map;
|
|
|
|
count_t pages;
|
2008-09-11 22:26:49 +02:00
|
|
|
|
2009-04-23 14:26:47 +02:00
|
|
|
// __asm__ __volatile__ ("xchgw %bx, %bx");
|
2008-09-10 15:04:24 +02:00
|
|
|
|
2009-04-23 14:26:47 +02:00
|
|
|
size = (size + 4095) & ~4095;
|
2008-09-10 15:04:24 +02:00
|
|
|
|
2009-04-23 14:26:47 +02:00
|
|
|
pages = size >> PAGE_WIDTH;
|
2008-09-10 15:04:24 +02:00
|
|
|
|
2009-04-23 14:26:47 +02:00
|
|
|
// map = (mmap_t*)malloc( sizeof(mmap_t) +
|
|
|
|
// sizeof(addr_t) * pages);
|
2008-09-10 15:04:24 +02:00
|
|
|
|
2009-04-23 14:26:47 +02:00
|
|
|
map = (mmap_t*)PA2KA(frame_alloc( (sizeof(mmap_t) +
|
|
|
|
sizeof(addr_t) * pages) >> PAGE_WIDTH));
|
2008-10-29 16:55:39 +01:00
|
|
|
|
2009-04-23 14:26:47 +02:00
|
|
|
map->size = size;
|
2008-10-29 16:55:39 +01:00
|
|
|
|
2009-04-23 14:26:47 +02:00
|
|
|
if ( map )
|
|
|
|
{
|
|
|
|
order = size >> HF_WIDTH;
|
2008-10-29 16:55:39 +01:00
|
|
|
|
2009-04-23 14:26:47 +02:00
|
|
|
if( order )
|
|
|
|
order = fnzb(order - 1) + 1;
|
2008-10-29 16:55:39 +01:00
|
|
|
|
2009-04-23 14:26:47 +02:00
|
|
|
efl = safe_cli();
|
2008-10-29 16:55:39 +01:00
|
|
|
|
2009-04-23 14:26:47 +02:00
|
|
|
spinlock_lock(&z_heap.lock);
|
2008-10-29 16:55:39 +01:00
|
|
|
|
2009-04-23 14:26:47 +02:00
|
|
|
frame = (frame_t*)buddy_system_alloc(order);
|
2008-10-29 16:55:39 +01:00
|
|
|
|
2009-04-23 14:26:47 +02:00
|
|
|
ASSERT( frame );
|
2008-10-29 16:55:39 +01:00
|
|
|
|
2009-04-23 14:26:47 +02:00
|
|
|
if( frame )
|
2008-10-29 16:55:39 +01:00
|
|
|
{
|
2009-04-23 14:26:47 +02:00
|
|
|
addr_t page = 0;
|
|
|
|
addr_t mem;
|
2008-10-29 16:55:39 +01:00
|
|
|
|
2009-04-23 14:26:47 +02:00
|
|
|
z_heap.free_count -= (1 << order);
|
|
|
|
z_heap.busy_count += (1 << order);
|
2008-10-29 16:55:39 +01:00
|
|
|
|
2009-04-23 14:26:47 +02:00
|
|
|
/* get frame address */
|
2008-10-29 16:55:39 +01:00
|
|
|
|
2009-04-23 14:26:47 +02:00
|
|
|
v = z_heap.base + (index_t)(frame - z_heap.frames);
|
2008-10-29 16:55:39 +01:00
|
|
|
|
2009-04-23 14:26:47 +02:00
|
|
|
heap = v << HF_WIDTH;
|
2008-10-29 16:55:39 +01:00
|
|
|
|
2009-04-23 14:26:47 +02:00
|
|
|
map->base = heap;
|
2008-10-29 16:55:39 +01:00
|
|
|
|
2009-04-23 14:26:47 +02:00
|
|
|
for(i = 0; i < (1 << order); i++)
|
|
|
|
frame[i].parent = map;
|
2008-10-29 16:55:39 +01:00
|
|
|
|
2009-04-23 14:26:47 +02:00
|
|
|
spinlock_unlock(&z_heap.lock);
|
2008-10-29 16:55:39 +01:00
|
|
|
|
|
|
|
safe_sti(efl);
|
|
|
|
|
|
|
|
|
2009-04-23 14:26:47 +02:00
|
|
|
addr_t *pte = &((addr_t*)page_tabs)[heap >> PAGE_WIDTH];
|
|
|
|
addr_t *mpte = &map->pte[0];
|
2008-10-29 16:55:39 +01:00
|
|
|
|
2009-04-23 14:26:47 +02:00
|
|
|
mem = heap;
|
2009-12-10 20:32:56 +01:00
|
|
|
|
|
|
|
if( flags & PG_MAP )
|
2009-04-23 14:26:47 +02:00
|
|
|
{
|
2008-10-29 16:55:39 +01:00
|
|
|
|
2009-12-10 20:32:56 +01:00
|
|
|
#ifdef ALLOC_IMM
|
2008-10-29 16:55:39 +01:00
|
|
|
|
2009-12-10 20:32:56 +01:00
|
|
|
while( pages )
|
|
|
|
{
|
|
|
|
u32_t order;
|
|
|
|
addr_t page_frame;
|
2008-10-29 16:55:39 +01:00
|
|
|
|
2009-12-10 20:32:56 +01:00
|
|
|
asm volatile ("bsr %0, %1":"=&r"(order):"r"(tmp):"cc");
|
|
|
|
asm volatile ("btr %0, %1" :"=r"(tmp):"r"(order):"cc");
|
2008-10-29 16:55:39 +01:00
|
|
|
|
2009-12-10 20:32:56 +01:00
|
|
|
page_frame = frame_alloc(1 << order) | (flags & 0xFFF);
|
2008-10-29 16:55:39 +01:00
|
|
|
|
2009-12-10 20:32:56 +01:00
|
|
|
for(i = 0; i < 1 << order; i++)
|
|
|
|
{
|
|
|
|
*pte++ = 0; //page;
|
|
|
|
*mpte++ = page;
|
|
|
|
|
|
|
|
asm volatile ( "invlpg (%0)" ::"r" (mem) );
|
|
|
|
mem+= 4096;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
|
|
|
|
page = PG_DEMAND | (flags & 0xFFF);
|
|
|
|
|
|
|
|
while(pages--)
|
|
|
|
{
|
|
|
|
*pte++ = 0;
|
|
|
|
*mpte++ = page;
|
|
|
|
asm volatile ( "invlpg (%0)" ::"r" (mem) );
|
|
|
|
mem+= 4096;
|
|
|
|
};
|
2009-04-23 14:26:47 +02:00
|
|
|
#endif
|
2009-12-10 20:32:56 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
while(pages--)
|
|
|
|
{
|
|
|
|
*pte++ = 0; //page;
|
|
|
|
*mpte++ = 0;
|
|
|
|
|
|
|
|
asm volatile ( "invlpg (%0)" ::"r" (mem) );
|
|
|
|
mem+= 4096;
|
|
|
|
};
|
|
|
|
}
|
2008-10-29 16:55:39 +01:00
|
|
|
|
2009-12-10 20:32:56 +01:00
|
|
|
#endif
|
2009-04-23 14:26:47 +02:00
|
|
|
DBG("%s %x size %d order %d\n", __FUNCTION__, heap, size, order);
|
2008-10-29 16:55:39 +01:00
|
|
|
|
2009-04-23 14:26:47 +02:00
|
|
|
return heap;
|
|
|
|
}
|
2008-10-29 16:55:39 +01:00
|
|
|
|
2009-04-23 14:26:47 +02:00
|
|
|
spinlock_unlock(&z_heap.lock);
|
2008-10-29 16:55:39 +01:00
|
|
|
|
2009-04-23 14:26:47 +02:00
|
|
|
safe_sti(efl);
|
2008-10-29 16:55:39 +01:00
|
|
|
|
2009-04-23 14:26:47 +02:00
|
|
|
frame_free( KA2PA(map) );
|
2008-10-29 16:55:39 +01:00
|
|
|
};
|
|
|
|
|
2009-04-23 14:26:47 +02:00
|
|
|
return 0;
|
2008-10-29 16:55:39 +01:00
|
|
|
}
|
|
|
|
|
2009-04-23 14:26:47 +02:00
|
|
|
void __fastcall mem_free(addr_t addr)
|
2008-10-27 21:47:58 +01:00
|
|
|
{
|
2009-04-23 14:26:47 +02:00
|
|
|
eflags_t efl;
|
|
|
|
frame_t *frame;
|
|
|
|
count_t idx;
|
2008-10-29 16:55:39 +01:00
|
|
|
|
2009-04-23 14:26:47 +02:00
|
|
|
idx = (addr >> HF_WIDTH);
|
2008-10-27 21:47:58 +01:00
|
|
|
|
2009-04-23 14:26:47 +02:00
|
|
|
if( (idx < z_heap.base) ||
|
|
|
|
(idx >= z_heap.base+z_heap.count)) {
|
|
|
|
DBG("invalid address %x\n", addr);
|
|
|
|
return;
|
|
|
|
}
|
2008-10-27 21:47:58 +01:00
|
|
|
|
2009-04-23 14:26:47 +02:00
|
|
|
efl = safe_cli();
|
2008-10-27 21:47:58 +01:00
|
|
|
|
2009-04-23 14:26:47 +02:00
|
|
|
frame = &z_heap.frames[idx-z_heap.base];
|
2008-10-27 21:47:58 +01:00
|
|
|
|
2009-04-23 14:26:47 +02:00
|
|
|
u32_t order = frame->buddy_order;
|
2008-10-27 21:47:58 +01:00
|
|
|
|
2009-04-23 14:26:47 +02:00
|
|
|
DBG("%s %x order %d\n", __FUNCTION__, addr, order);
|
2008-10-27 21:47:58 +01:00
|
|
|
|
2009-04-23 14:26:47 +02:00
|
|
|
ASSERT(frame->refcount);
|
2008-10-27 21:47:58 +01:00
|
|
|
|
2009-04-23 14:26:47 +02:00
|
|
|
spinlock_lock(&z_heap.lock);
|
2008-09-10 15:04:24 +02:00
|
|
|
|
2009-04-23 14:26:47 +02:00
|
|
|
if (!--frame->refcount)
|
2008-10-29 16:55:39 +01:00
|
|
|
{
|
2009-04-23 14:26:47 +02:00
|
|
|
mmap_t *map;
|
|
|
|
count_t i;
|
2008-09-10 15:04:24 +02:00
|
|
|
|
2009-04-23 14:26:47 +02:00
|
|
|
map = frame->parent;
|
2008-09-10 15:04:24 +02:00
|
|
|
|
2009-04-23 14:26:47 +02:00
|
|
|
for(i = 0; i < (1 << order); i++)
|
|
|
|
frame[i].parent = NULL;
|
2008-10-29 16:55:39 +01:00
|
|
|
|
2009-04-23 14:26:47 +02:00
|
|
|
buddy_system_free(&frame->buddy_link);
|
2008-09-10 15:04:24 +02:00
|
|
|
|
2009-04-23 14:26:47 +02:00
|
|
|
/* Update zone information. */
|
|
|
|
z_heap.free_count += (1 << order);
|
|
|
|
z_heap.busy_count -= (1 << order);
|
2008-09-10 15:04:24 +02:00
|
|
|
|
2009-04-23 14:26:47 +02:00
|
|
|
spinlock_unlock(&z_heap.lock);
|
|
|
|
safe_sti(efl);
|
2008-09-10 15:04:24 +02:00
|
|
|
|
2009-12-10 20:32:56 +01:00
|
|
|
for( i = 0; i < (map->size >> PAGE_WIDTH); )
|
|
|
|
{
|
|
|
|
i+= frame_free(map->pte[i]);
|
|
|
|
}
|
2008-09-10 15:04:24 +02:00
|
|
|
|
2009-04-23 14:26:47 +02:00
|
|
|
frame_free( KA2PA(map) );
|
|
|
|
}
|
2008-10-29 16:55:39 +01:00
|
|
|
else
|
|
|
|
{
|
2009-04-23 14:26:47 +02:00
|
|
|
spinlock_unlock(&z_heap.lock);
|
|
|
|
safe_sti(efl);
|
2008-10-29 16:55:39 +01:00
|
|
|
};
|
2008-09-10 15:04:24 +02:00
|
|
|
};
|
2008-10-29 16:55:39 +01:00
|
|
|
|
2009-04-23 14:26:47 +02:00
|
|
|
void __fastcall heap_fault(addr_t faddr, u32_t code)
|
2008-09-10 15:04:24 +02:00
|
|
|
{
|
2009-04-23 14:26:47 +02:00
|
|
|
index_t idx;
|
|
|
|
frame_t *frame;
|
|
|
|
mmap_t *map;
|
2008-10-30 07:30:13 +01:00
|
|
|
|
2009-04-23 14:26:47 +02:00
|
|
|
idx = faddr >> HF_WIDTH;
|
2008-10-30 07:30:13 +01:00
|
|
|
|
2009-04-23 14:26:47 +02:00
|
|
|
frame = &z_heap.frames[idx-z_heap.base];
|
2008-10-30 07:30:13 +01:00
|
|
|
|
2009-04-23 14:26:47 +02:00
|
|
|
map = frame->parent;
|
2008-10-30 07:30:13 +01:00
|
|
|
|
2009-04-23 14:26:47 +02:00
|
|
|
ASSERT( faddr >= map->base);
|
2008-11-01 02:25:51 +01:00
|
|
|
|
2009-04-23 14:26:47 +02:00
|
|
|
if( faddr < map->base + map->size)
|
2008-11-01 02:25:51 +01:00
|
|
|
{
|
2009-04-23 14:26:47 +02:00
|
|
|
addr_t page;
|
2008-11-01 02:25:51 +01:00
|
|
|
|
2009-04-23 14:26:47 +02:00
|
|
|
idx = (faddr - map->base) >> PAGE_WIDTH;
|
2008-11-01 02:25:51 +01:00
|
|
|
|
2009-04-23 14:26:47 +02:00
|
|
|
page = map->pte[idx];
|
2008-11-01 02:25:51 +01:00
|
|
|
|
2009-04-23 14:26:47 +02:00
|
|
|
if( page != 0)
|
2008-11-01 02:25:51 +01:00
|
|
|
{
|
2009-04-23 14:26:47 +02:00
|
|
|
if( page & PG_DEMAND)
|
2008-11-01 02:25:51 +01:00
|
|
|
{
|
2009-04-23 14:26:47 +02:00
|
|
|
page &= ~PG_DEMAND;
|
|
|
|
page = alloc_page() | (page & 0xFFF);
|
2008-10-27 21:47:58 +01:00
|
|
|
|
2009-04-23 14:26:47 +02:00
|
|
|
map->pte[idx] = page;
|
|
|
|
};
|
|
|
|
((addr_t*)page_tabs)[faddr >> PAGE_WIDTH] = page;
|
|
|
|
};
|
2008-10-30 07:30:13 +01:00
|
|
|
};
|
2008-09-10 15:04:24 +02:00
|
|
|
};
|
|
|
|
|
2009-04-23 14:26:47 +02:00
|
|
|
//#include "mmap.inc"
|
2008-10-27 21:47:58 +01:00
|
|
|
|