Page allocation uses new external library, libmalloc

This commit is contained in:
2023-01-09 01:56:09 -06:00
parent 1be6de1a26
commit 6af84b247f
9 changed files with 63 additions and 411 deletions

View File

@@ -1,8 +1,8 @@
bin_PROGRAMS = quark-kernel
quark_kernel_SOURCES = kernel.c mmgr.c priorityqueue.c stdio.c string.c elf.c syscalls.c heap.c memmap.c avltree.c queue.c math.c
quark_kernel_LDADD = -lgcc
quark_kernel_CFLAGS = -I$(top_srcdir)/include -ffreestanding -mgeneral-regs-only -O0 -Wall -ggdb
quark_kernel_LDFLAGS = -nostdlib
quark_kernel_SOURCES = kernel.c mmgr.c priorityqueue.c stdio.c string.c elf.c syscalls.c heap.c avltree.c queue.c math.c
quark_kernel_LDADD = -lgcc -lmalloc
quark_kernel_CFLAGS = -I$(top_srcdir)/include -I$(prefix)/include -ffreestanding -mgeneral-regs-only -O0 -Wall -ggdb
quark_kernel_LDFLAGS = -L$(prefix)/lib -nostdlib
if x86
quark_kernel_SOURCES += x86/paging.c \

View File

@@ -17,7 +17,10 @@ struct kernel_t kernel;
void kernel_initialize(struct boot_info_t *boot_info)
{
initialize_screen();
if(initialize_screen())
{
asm("hlt");
}
printf("***%s***\n", PACKAGE_STRING);
printf("Total memory: %08x\n", boot_info->memory_size);
printf("kernel: %08x ... %08x\n", &_kernel_pstart, &_kernel_pend);
@@ -27,9 +30,17 @@ void kernel_initialize(struct boot_info_t *boot_info)
printf("%i\t\t\t%08x\t\t%u\n", boot_info->map.array[i].type, boot_info->map.array[i].location, boot_info->map.array[i].size);
}
insert_region(&boot_info->map, (physaddr_t)&_kernel_pstart, (physaddr_t)&_kernel_pend - (physaddr_t)&_kernel_pstart, M_UNAVAILABLE);
initialize_page_map(&boot_info->map, (physaddr_t*)&_kernel_end, boot_info->memory_size, page_size);
kminit(&_kernel_start, page_map_end(), 0xFFC00000 - (size_t)&_kernel_start, 64);
memmap_insert_region(&boot_info->map, (physaddr_t)&_kernel_pstart, (physaddr_t)&_kernel_pend - (physaddr_t)&_kernel_pstart, M_UNAVAILABLE);
if(initialize_page_map(&boot_info->map, (physaddr_t*)&_kernel_end, boot_info->memory_size, page_size))
{
panic("Failed to initialize page allocator.");
}
if(kminit(&_kernel_start, page_map_end(), 0xFFC00000 - (size_t)&_kernel_start, 64))
{
panic("Failed to initialize heap.");
}
kernel.active_process = NULL;
kernel.next_pid = 1;
kernel.process_table = NULL;

View File

@@ -1,136 +0,0 @@
#include "memmap.h"
#include <stdbool.h>
int compare_regions(struct memory_region_t *lhs, struct memory_region_t *rhs)
{
if(lhs->location == rhs->location)
{
return lhs->size > rhs->size ? 1
: (lhs->size == rhs->size ? 0
: -1);
}
else
{
return lhs->location > rhs->location ? 1 : -1;
}
}
bool region_overlaps(struct memory_region_t *lhs, struct memory_region_t *rhs)
{
if(rhs->location < lhs->location)
{
return rhs->location + rhs->size > lhs->location;
}
return rhs->location < lhs->location + lhs->size;
}
bool region_contains(struct memory_region_t *lhs, struct memory_region_t *rhs)
{
return (rhs->location >= lhs->location) &&
(rhs->location + rhs->size <= lhs->location + lhs->size);
}
void insert_map_entry(struct memory_map_t *map, physaddr_t location, size_t size, unsigned int type)
{
struct memory_region_t new_region = {.location = location, .size = size, .type = type};
unsigned int i = 0;
while(i < map->size)
{
if(compare_regions(&new_region, &map->array[i]) < 0)
{
struct memory_region_t buffer = new_region;
new_region = map->array[i];
map->array[i] = buffer;
}
i++;
}
map->array[i] = new_region;
map->size++;
}
void remove_map_entry(struct memory_map_t *map, int index)
{
if(index >= 0 && index < map->size)
{
for(int i = index; i < map->size - 1; i++)
{
map->array[i] = map->array[i + 1];
}
map->size--;
}
}
int trim_map(struct memory_map_t *map, int index)
{
if(index + 1 >= map->size)
{
return -1;
}
struct memory_region_t *left = &map->array[index];
struct memory_region_t *right = &map->array[index + 1];
if(region_overlaps(left, right))
{
if(left->type == right->type)
{
left->size = (right->location + right->size > left->location + left->size ? right->location + right->size : left->location + left->size) - left->location;
remove_map_entry(map, index + 1);
return index;
}
else if(left->type < right->type)
{
if(region_contains(right, left))
{
remove_map_entry(map, index);
return index;
}
else if(left->location + left->size <= right->location + right->size)
{
left->size = (right->location > left->location) ? right->location - left->location : 0;
return index + 1;
}
else
{
struct memory_region_t new_right = {
.location = right->location + right->size,
.size = (left->location + left->size) - (right->location + right->size),
.type = left->type};
left->size = (right->location > left->location) ? right->location - left->location : 0;
if(left->size == 0)
remove_map_entry(map, index);
insert_map_entry(map, new_right.location, new_right.size, new_right.type);
return index + 2;
}
}
else
{
if(region_contains(left, right))
{
remove_map_entry(map, index + 1);
return index;
}
else
{
right->size = (right->location + right->size) - (left->location + left->size);
right->location = left->location + left->size;
return index + 1;
}
}
}
else if((left->location + left->size == right->location) && left->type == right->type)
{
left->size = right->location + right->size - left->location;
remove_map_entry(map, index + 1);
return index;
}
return index + 1;
}
void insert_region(struct memory_map_t *map, physaddr_t location, size_t size, unsigned int type)
{
insert_map_entry(map, location, size, type);
int i = 0;
while(i >= 0)
{
i = trim_map(map, i);
}
}

View File

@@ -4,151 +4,20 @@
#include "platform/paging.h"
#include "types/status.h"
#include "stdio.h"
#include <libmalloc/bitmap_alloc.h>
#include <stdint.h>
#include <stdbool.h>
#define MAX_CACHE_SIZE 32
struct page_map_t
bitmap_heap_descriptor_t page_map;
physaddr_t reserve_pages(size_t size)
{
/**
* @brief The underlying bitmap representing the availability of chunks of
* physical memory.
*
*/
unsigned long *bitmap;
/**
* @brief Stores a list of available blocks of memory to speed up allocation.
*
*/
unsigned long *cache;
/**
* @brief The size of the bitmap in bytes.
*
*/
unsigned long bitmap_size;
/**
* @brief The size in bytes of the smallest unit of allocation.
*
* This value should either be the size of a page on the host system, or
* possibly some number of pages.
*
*/
unsigned long block_size;
/**
* @brief
*
*/
unsigned long height;
/**
* @brief The number of available blocks of memory.
*
* Due to memory fragmentation, it may not be possible to allocate all
* available memory at once.
*
*/
unsigned long free_block_count;
} page_map;
static const int bitmap_word_size = 8 * sizeof(*page_map.bitmap);
static void clear_cache()
{
for(int i = 0; i < MAX_CACHE_SIZE; i++)
unsigned long location = reserve_region(&page_map, size);
if(location != NOMEM)
{
page_map.cache[i] = 0;
}
}
static void clear_bitmap()
{
for(int i = 0; i < page_map.bitmap_size / sizeof(*page_map.bitmap); i++)
{
page_map.bitmap[i] = 0;
}
}
int split_block(int index)
{
if (index)
{
unsigned long bitmap_index = index / bitmap_word_size;
unsigned long bitmap_offset = index % bitmap_word_size;
page_map.bitmap[bitmap_index] &= ~((unsigned long)1 << bitmap_offset);
index *= 2;
bitmap_index = index / bitmap_word_size;
bitmap_offset = index % bitmap_word_size;
page_map.bitmap[bitmap_index] |= (unsigned long)1 << bitmap_offset;
page_map.bitmap[bitmap_index] |= (unsigned long)1 << (bitmap_offset ^ 1);
unsigned long depth = llog2(index + 1) - 1;
unsigned long cache_start = llog2(bitmap_word_size);
if(depth >= cache_start && page_map.cache[depth - cache_start] == 0)
{
page_map.cache[depth - cache_start] = index + 1;
}
}
return index;
}
int find_free_region(int height)
{
if (height > page_map.height || height < 0)
{
return 0;
}
else if (height <= page_map.height - ilog2(bitmap_word_size))
{
if(page_map.cache[page_map.height - height - llog2(bitmap_word_size)])
{
unsigned long index = page_map.cache[page_map.height - height - llog2(bitmap_word_size)];
page_map.cache[page_map.height - height - llog2(bitmap_word_size)] = 0;
return index;
}
unsigned long start = (1 << (page_map.height - height)) / bitmap_word_size;
unsigned long end = ((1 << (page_map.height - height + 1)) / bitmap_word_size);
for (int index = start; index < end; index++)
{
if (page_map.bitmap[index] != 0)
{
return bitmap_word_size * index + __builtin_ctzl(page_map.bitmap[index]);
}
}
}
else
{
#if __SIZEOF_LONG__ == 8
static const unsigned long bitmasks[] = {0x00000002, 0x0000000C, 0x000000F0, 0x0000FF00, 0xFFFF0000, 0xFFFFFFFF00000000};
#else
static const unsigned long bitmasks[] = {0x00000002, 0x0000000C, 0x000000F0, 0x0000FF00, 0xFFFF0000};
#endif
int depth = page_map.height - height;
if (page_map.bitmap[0] & bitmasks[depth])
{
return __builtin_ctzl(page_map.bitmap[0] & bitmasks[depth]);
}
}
return split_block(find_free_region(height + 1));
}
physaddr_t reserve_region(size_t size)
{
int height = llog2(size / page_map.block_size);
int index = find_free_region(height);
if (index)
{
int bitmap_index = index / bitmap_word_size;
int bitmap_offset = index % bitmap_word_size;
page_map.bitmap[bitmap_index] &= ~((unsigned long)1 << bitmap_offset);
physaddr_t page = (page_map.block_size << height) * (index - ((unsigned long)1 << (page_map.height - height)));
if(page < 0x10000)
;//printf("Reserved %08x\n", page);
return page;
return location;
}
else
{
@@ -156,44 +25,21 @@ physaddr_t reserve_region(size_t size)
}
}
int free_region(physaddr_t location, size_t size)
int free_pages(physaddr_t location, size_t size)
{
int height = llog2(size / page_map.block_size);
int index = (location / (page_map.block_size * ((unsigned long)1 << height))) + (1 << (page_map.height - height));
int bitmap_index = index / bitmap_word_size;
int bitmap_offset = index % bitmap_word_size;
page_map.bitmap[bitmap_index] |= (unsigned long)1 << bitmap_offset;
unsigned long cache_start = llog2(bitmap_word_size);
while (page_map.bitmap[bitmap_index] & ((unsigned long)1 << (bitmap_offset ^ 1)))
{
unsigned long depth = llog2(index + 1) - 1;
if(page_map.cache[depth - cache_start] == (index ^ 1))
{
page_map.cache[depth - cache_start] = 0;
}
page_map.bitmap[bitmap_index] &= ~((unsigned long)1 << bitmap_offset);
page_map.bitmap[bitmap_index] &= ~((unsigned long)1 << (bitmap_offset ^ 1));
index /= 2;
bitmap_index = index / bitmap_word_size;
bitmap_offset = index % bitmap_word_size;
page_map.bitmap[bitmap_index] |= (unsigned long)1 << bitmap_offset;
}
unsigned long depth = llog2(index + 1) - 1;
if (depth >= cache_start && page_map.cache[depth - cache_start] == 0)
{
page_map.cache[depth - cache_start] = index;
}
free_region(&page_map, location, size);
return ENONE;
}
physaddr_t reserve_page()
{
return reserve_region(page_size);
return reserve_region(&page_map, page_size);
}
int free_page(physaddr_t location)
{
return free_region(location, page_size);
free_region(&page_map, location, page_size);
return ENONE;
}
size_t free_page_count()
@@ -211,22 +57,21 @@ void *page_map_end()
return (void*)page_map.bitmap + page_map.bitmap_size;
}
error_t initialize_page_map(struct memory_map_t *map, void *base, size_t memory_size, unsigned long block_size)
error_t initialize_page_map(memory_map_t *map, void *base, size_t memory_size, unsigned long block_size)
{
static unsigned long page_map_cache[MAX_CACHE_SIZE];
// Round memory_size up to nearest power of 2
memory_size = 1 << llog2(memory_size);
page_map.bitmap = (unsigned long*) base;
page_map.cache = page_map_cache;
page_map.bitmap_size = (memory_size / page_size) / 4;
page_map.block_size = block_size;
page_map.height = llog2(memory_size / block_size);
page_map.free_block_count = 0;
int block_log = llog2(block_size);
page_map.cache = page_map_cache;
page_map.cache_capacity = MAX_CACHE_SIZE;
page_map.bitmap = (unsigned long*) base;
/* Allocate pages for bitmap */
int pages_mapped = 0;
bool initialized = false;
printf("bmpsz: %08x bmploc: %08x\n", page_map.bitmap_size, page_map.bitmap);
for(int i = 0; i < map->size; i++)
int pages_needed = (bitmap_size(map, block_size) + page_size - 1) / page_size;
for(int i = 0; i < map->size && (pages_mapped < pages_needed); i++)
{
if(map->array[i].type != M_AVAILABLE)
{
@@ -234,7 +79,7 @@ error_t initialize_page_map(struct memory_map_t *map, void *base, size_t memory_
}
physaddr_t location = (map->array[i].location + page_size - 1) & ~(page_size - 1);
physaddr_t region_end = map->array[i].location + map->array[i].size;
while(location + block_size <= region_end && pages_mapped < (page_map.bitmap_size + page_size - 1) / page_size)
while(location + page_size <= region_end && (pages_mapped < pages_needed))
{
void *page = (void*)page_map.bitmap + pages_mapped * page_size;
for(int level = 0; level < page_table_levels; level++)
@@ -256,63 +101,20 @@ error_t initialize_page_map(struct memory_map_t *map, void *base, size_t memory_
pages_mapped++;
}
}
memmap_insert_region(map, location, page_size, M_UNAVAILABLE);
location += page_size;
continue;
}
if(!initialized)
{
clear_bitmap();
initialized = true;
}
while(location + block_size <= region_end)
{
int bit_offset = (location / block_size) % bitmap_word_size;
int bitmap_index = ((1 << (page_map.height - 0)) / bitmap_word_size) + (location / block_size) / bitmap_word_size;
size_t chunk_size = (bitmap_word_size - bit_offset) * block_size;
// BUG: bitmap entries are uninitialized at first, never zeroed out
if(location < 0x180000)
printf("%08x ", page_map.bitmap[bitmap_index]);
if(bit_offset == 0 && (region_end - location) >= chunk_size)
{
if(location < 0x180000)
printf("a ");
// Set all bits in the word
page_map.bitmap[bitmap_index] = ~0;
}
else if(bit_offset == 0)
{
if(location < 0x180000)
printf("b ");
// Set the first 'count' bits
int count = (region_end - location) >> block_log;
page_map.bitmap[bitmap_index] |= (1 << count) - 1;
}
else if((region_end - location) >= chunk_size)
{
if(location < 0x180000)
printf("c ");
// Set all bits starting at 'bit_offset'
page_map.bitmap[bitmap_index] |= ~((1 << bit_offset) - 1);
}
else
{
if(location < 0x180000)
printf("d ");
// Set all bits starting at 'bit_offset' up to 'count'
int count = (region_end - location) >> block_log;
page_map.bitmap[bitmap_index] |= ((1 << count) - 1) & ~((1 << bit_offset) - 1);
}
if(location < 0x180000) {
printf("bmp: %08x loc: %08x end: %08x off: %i idx: %i cnt: %i\n", page_map.bitmap[bitmap_index], location, region_end, bit_offset, bitmap_index, (region_end - location) >> block_log);
}
location += chunk_size;
}
}
clear_cache();
return ENONE;
if(initialize_physical_heap(&page_map, map))
{
return ENOMEM;
}
else
{
return ENONE;
}
}
physaddr_t create_address_space()

View File

@@ -84,7 +84,9 @@ void *read_multiboot_table_entry(struct boot_info_t *boot_info, void *table)
case MB_END_TAG:
return NULL;
case MB_MEMORY_INFO:
boot_info->memory_size = ((struct multiboot2_memory_info_t*) table)->high_memory * 1024;
boot_info->memory_size =
((struct multiboot2_memory_info_t*) table)->high_memory * 1024
+ ((struct multiboot2_memory_info_t*) table)->low_memory * 1024;
break;
case MB_MEMORY_MAP: ;
unsigned int tag_size = ((struct multiboot2_memory_map_t*) table)->size - 16;
@@ -96,7 +98,7 @@ void *read_multiboot_table_entry(struct boot_info_t *boot_info, void *table)
entry->type == MB_AVAILABLE ? M_AVAILABLE
: (entry->type == MB_DEFECTIVE ? M_DEFECTIVE
: M_UNAVAILABLE);
insert_region(&boot_info->map, entry->base, entry->length, entry_type);
memmap_insert_region(&boot_info->map, entry->base, entry->length, entry_type);
entry = (struct multiboot2_map_entry_t*) ((void*) entry + entry_size);
tag_size -= entry_size;
}
@@ -110,7 +112,7 @@ void *read_multiboot_table_entry(struct boot_info_t *boot_info, void *table)
unsigned long size = ((struct multiboot2_module_t*) table)->end - ((struct multiboot2_module_t*) table)->start;
size += 4095;
size &= ~4095;
insert_region(&boot_info->map,
memmap_insert_region(&boot_info->map,
((struct multiboot2_module_t*) table)->start,
size,
M_UNAVAILABLE);

View File

@@ -6,7 +6,7 @@
void x86_startup(void *multiboot_info)
{
struct memory_region_t map_array[24];
memory_region_t map_array[24];
char bootloader_name[64];
char kernel_parameters[64];
struct boot_info_t boot_info = {