0
.gitignore
vendored
Executable file → Normal file
0
.gitignore
vendored
Executable file → Normal file
3
README.md
Executable file → Normal file
3
README.md
Executable file → Normal file
@@ -13,8 +13,5 @@ To build the kernel for the x86 platform, run:
|
||||
- `./configure --host=i686-elf --prefix=<your_desired_prefix> --bindir=$(prefix)/apps CFLAGS=-ffreestanding LDFLAGS=-nostdlib`
|
||||
- `make`
|
||||
|
||||
To generate a bootable disk image, run:
|
||||
- `make quark.iso`
|
||||
|
||||
So far this code has only been tested to compile using GCC. Some modifications might be necessary to build this project using other compilers. For a guide on building
|
||||
a cross-compiler, see the article on [osdev.org](https://wiki.osdev.org/GCC_Cross-Compiler).
|
||||
@@ -1,13 +1,13 @@
|
||||
#pragma once
|
||||
|
||||
#include "avltree.h"
|
||||
#include "memmap.h"
|
||||
#include "priorityqueue.h"
|
||||
#include "queue.h"
|
||||
#include "mmgr.h"
|
||||
#include "syscalls.h"
|
||||
#include "types/syscallid.h"
|
||||
#include "types/status.h"
|
||||
#include <libmalloc/memmap.h>
|
||||
#include <stddef.h>
|
||||
|
||||
#define MAX_SYSCALL_ID 256
|
||||
@@ -38,7 +38,7 @@ struct boot_info_t
|
||||
char *bootloader;
|
||||
char *parameters;
|
||||
size_t memory_size;
|
||||
struct memory_map_t map;
|
||||
memory_map_t map;
|
||||
size_t module_count;
|
||||
struct module_t modules[module_limit];
|
||||
};
|
||||
|
||||
@@ -1,27 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include "types/physaddr.h"
|
||||
#include <stddef.h>
|
||||
|
||||
enum memory_type_t
|
||||
{
|
||||
M_AVAILABLE = 1,
|
||||
M_UNAVAILABLE = 2,
|
||||
M_DEFECTIVE = 3
|
||||
};
|
||||
|
||||
struct memory_region_t
|
||||
{
|
||||
physaddr_t location;
|
||||
size_t size;
|
||||
unsigned int type;
|
||||
};
|
||||
|
||||
struct memory_map_t
|
||||
{
|
||||
struct memory_region_t *array;
|
||||
size_t size;
|
||||
size_t capacity;
|
||||
};
|
||||
|
||||
void insert_region(struct memory_map_t *map, physaddr_t location, size_t size, enum memory_type_t type);
|
||||
@@ -1,9 +1,9 @@
|
||||
#pragma once
|
||||
|
||||
#include "memmap.h"
|
||||
#include "platform/paging.h"
|
||||
#include "types/physaddr.h"
|
||||
#include "types/status.h"
|
||||
#include "libmalloc/memmap.h"
|
||||
#include <stddef.h>
|
||||
|
||||
extern const size_t page_size;
|
||||
@@ -14,7 +14,7 @@ extern const size_t page_size;
|
||||
* @param size
|
||||
* @return physaddr_t
|
||||
*/
|
||||
physaddr_t reserve_region(size_t size);
|
||||
physaddr_t reserve_pages(size_t size);
|
||||
|
||||
/**
|
||||
* @brief
|
||||
@@ -23,7 +23,7 @@ physaddr_t reserve_region(size_t size);
|
||||
* @param size
|
||||
* @return int
|
||||
*/
|
||||
int free_region(physaddr_t location, size_t size);
|
||||
int free_pages(physaddr_t location, size_t size);
|
||||
|
||||
/**
|
||||
* @brief Reserves a single page and returns its physical address.
|
||||
@@ -72,7 +72,7 @@ void *page_map_end();
|
||||
* @param block_size
|
||||
* @return enum error_t
|
||||
*/
|
||||
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);
|
||||
|
||||
/**
|
||||
* @brief Create a new top-level page table and map the kernel in it.
|
||||
|
||||
@@ -4,4 +4,5 @@ extern int stack_top;
|
||||
extern int _kernel_pstart;
|
||||
extern int _kernel_pend;
|
||||
extern int _kernel_start;
|
||||
extern int _kernel_tend;
|
||||
extern int _kernel_end;
|
||||
|
||||
@@ -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 \
|
||||
|
||||
179
src/heap.c
179
src/heap.c
@@ -1,162 +1,85 @@
|
||||
#include <stdbool.h>
|
||||
#include <libmalloc/bitmap_alloc.h>
|
||||
#include "heap.h"
|
||||
#include "mmgr.h"
|
||||
#include "math.h"
|
||||
#include "stdio.h"
|
||||
#include "types/status.h"
|
||||
|
||||
#define AVAIL 0
|
||||
#define UNAVAIL 1
|
||||
#define ALLOCATED 2
|
||||
bitmap_heap_descriptor_t system_heap;
|
||||
|
||||
struct heap_t
|
||||
static int map_block(void *location, unsigned long size)
|
||||
{
|
||||
void *base;
|
||||
struct heap_node_t *header;
|
||||
size_t heap_size;
|
||||
size_t block_size;
|
||||
size_t tree_height;
|
||||
} system_heap;
|
||||
|
||||
struct heap_node_t
|
||||
{
|
||||
size_t mapped : 1;
|
||||
size_t state : 2;
|
||||
size_t height : 5;
|
||||
} __attribute__ ((packed));
|
||||
|
||||
size_t find_free_block(struct heap_t *heap, size_t height)
|
||||
{
|
||||
if(height > heap->tree_height)
|
||||
for(int n = 0; n < size; n += page_size)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
for(size_t index = 1 << (heap->tree_height - height); index < 1 << (heap->tree_height - height + 1); index++)
|
||||
{
|
||||
if(heap->header[index].state == AVAIL)
|
||||
if(!(page_type(location + n) & PAGE_PRESENT))
|
||||
{
|
||||
return index;
|
||||
}
|
||||
}
|
||||
size_t index = find_free_block(heap, height + 1);
|
||||
if(index)
|
||||
{
|
||||
heap->header[index].state = UNAVAIL;
|
||||
heap->header[index << 1].state = AVAIL;
|
||||
heap->header[(index << 1) ^ 1].state = AVAIL;
|
||||
}
|
||||
return index << 1;
|
||||
}
|
||||
|
||||
int map_region(struct heap_t *heap, size_t height, size_t index)
|
||||
{
|
||||
int status = 0;
|
||||
if(heap->header[index].mapped == 0)
|
||||
{
|
||||
if(height > 0)
|
||||
{
|
||||
status = map_region(heap, height - 1, index << 1);
|
||||
if(status == 0)
|
||||
{
|
||||
status = map_region(heap, height - 1, (index << 1) ^ 1);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
void *ptr = (void*) ((size_t) heap->base + (heap->block_size << height) * (index - (1 << (heap->tree_height - height))));
|
||||
if((page_type(ptr) & PAGE_PRESENT) == 0)
|
||||
{
|
||||
status = map_page(ptr, reserve_page(), PAGE_RW);
|
||||
}
|
||||
}
|
||||
heap->header[index].mapped = 1;
|
||||
}
|
||||
return status;
|
||||
}
|
||||
|
||||
int heap_contruct(struct heap_t *heap, void *base, void *start, size_t heap_size, size_t block_size)
|
||||
{
|
||||
heap->base = base;
|
||||
heap->header = (struct heap_node_t*) start;
|
||||
heap->heap_size = heap_size;
|
||||
heap->block_size = block_size;
|
||||
heap->tree_height = llog2(heap_size / block_size);
|
||||
size_t header_size = (heap_size / block_size) << 1;
|
||||
for(size_t i = 1; i <= (heap_size / block_size) * 2; i++)
|
||||
{
|
||||
int flags = page_type((void*) heap->header + i);
|
||||
if((flags & PAGE_PRESENT) == 0)
|
||||
{
|
||||
int status = map_page((void*)heap->header + i, reserve_page(), PAGE_RW);
|
||||
if(status != ENONE)
|
||||
int status = map_page(location + n, reserve_page(), PAGE_RW);
|
||||
if(status)
|
||||
{
|
||||
return status;
|
||||
}
|
||||
}
|
||||
heap->header[i].state = UNAVAIL;
|
||||
heap->header[i].mapped = 0;
|
||||
}
|
||||
for(size_t i = 0; i < heap_size / block_size; i++)
|
||||
{
|
||||
if(block_size * i >= header_size + (start - base))
|
||||
{
|
||||
size_t index = i + (1 << heap->tree_height);
|
||||
heap->header[index].state = AVAIL;
|
||||
for(; index > 1 && heap->header[index ^ 1].state == 0; index >>= 1)
|
||||
{
|
||||
heap->header[index].state = UNAVAIL;
|
||||
heap->header[index ^ 1].state = UNAVAIL;
|
||||
heap->header[index >> 1].state = AVAIL;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
heap->header[i + (1 << heap->tree_height)].state = UNAVAIL;
|
||||
}
|
||||
}
|
||||
return ENONE;
|
||||
}
|
||||
|
||||
void *heap_allocate(struct heap_t *heap, size_t size)
|
||||
static int mmap_callback(void *location, unsigned long size)
|
||||
{
|
||||
size += heap->block_size - 1;
|
||||
size -= size % heap->block_size;
|
||||
size_t height = llog2(size / heap->block_size);
|
||||
size_t index = find_free_block(heap, height);
|
||||
if(index)
|
||||
int status = ENONE;
|
||||
for(unsigned long i = 0; i < size; i += page_size)
|
||||
{
|
||||
heap->header[index].state = ALLOCATED;
|
||||
void *ptr = (void*) ((size_t) heap->base + (heap->block_size << height) * (index - (1 << (heap->tree_height - height))));
|
||||
map_region(heap, height, index);
|
||||
return ptr;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void heap_free(struct heap_t *heap, void *ptr)
|
||||
{
|
||||
size_t offset = (size_t) ptr - (size_t) heap->base;
|
||||
size_t index = (offset / heap->block_size) + (1 << heap->tree_height);
|
||||
for(; index > 0 && heap->header[index].state == UNAVAIL; index >>= 1);
|
||||
heap->header[index].state = AVAIL;
|
||||
for(; index > 1 && heap->header[index ^ 1].state == AVAIL; index >>= 1)
|
||||
{
|
||||
heap->header[index].state = UNAVAIL;
|
||||
heap->header[index ^ 1].state = UNAVAIL;
|
||||
heap->header[index >> 1].state = AVAIL;
|
||||
physaddr_t frame = reserve_page();
|
||||
if(frame == ENOMEM)
|
||||
{
|
||||
return ENOMEM;
|
||||
}
|
||||
else if((status = map_page(location + i, reserve_page(), PAGE_RW)))
|
||||
{
|
||||
return status;
|
||||
}
|
||||
}
|
||||
return status;
|
||||
}
|
||||
|
||||
int kminit(void *base, void* start, size_t heap_size, size_t block_size)
|
||||
{
|
||||
return heap_contruct(&system_heap, base, start, heap_size, block_size);
|
||||
static unsigned long heap_cache[16];
|
||||
system_heap.bitmap = NULL;
|
||||
system_heap.cache = heap_cache;
|
||||
system_heap.cache_capacity = 16;
|
||||
system_heap.block_bits = 4;
|
||||
system_heap.block_size = block_size;
|
||||
system_heap.offset = (unsigned long)base;
|
||||
memory_region_t map_array[8];
|
||||
memory_map_t map = {
|
||||
.array = map_array,
|
||||
.capacity = 8,
|
||||
.size = 0
|
||||
};
|
||||
memmap_insert_region(&map, 0, heap_size, M_AVAILABLE);
|
||||
memmap_insert_region(&map, 0, start - base, M_UNAVAILABLE);
|
||||
return initialize_heap(&system_heap, &map, mmap_callback);
|
||||
}
|
||||
|
||||
void *kmalloc(size_t size)
|
||||
{
|
||||
return heap_allocate(&system_heap, size);
|
||||
unsigned long loc = reserve_region(&system_heap, size);
|
||||
if(loc == NOMEM)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
else if(map_block((void*)loc, size))
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
else
|
||||
{
|
||||
return (void*)loc;
|
||||
}
|
||||
}
|
||||
|
||||
void kfree(void *ptr)
|
||||
{
|
||||
heap_free(&system_heap, ptr);
|
||||
free_region(&system_heap, (unsigned long)ptr, 0);
|
||||
}
|
||||
|
||||
24
src/kernel.c
24
src/kernel.c
@@ -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,22 @@ 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);
|
||||
for(void *p = &_kernel_start; p < (void*)&_kernel_tend; p += page_size)
|
||||
{
|
||||
set_pte_type(p, page_table_levels - 1, PAGE_PRESENT);
|
||||
}
|
||||
|
||||
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;
|
||||
|
||||
@@ -32,7 +32,7 @@ int llog2(unsigned long x)
|
||||
#elif (defined __GNUC__) && (__SIZEOF_LONG__ == 8)
|
||||
if(x <= 1)
|
||||
return 0;
|
||||
return 64 - __buildin_clzl(x - 1);
|
||||
return 64 - __builtin_clzl(x - 1);
|
||||
#elif __SIZEOF_LONG__ == 8
|
||||
static const int table[64] = {
|
||||
0, 58, 1, 59, 47, 53, 2, 60, 39, 48, 27, 54, 33, 42, 3, 61,
|
||||
|
||||
136
src/memmap.c
136
src/memmap.c
@@ -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);
|
||||
}
|
||||
}
|
||||
273
src/mmgr.c
273
src/mmgr.c
@@ -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,30 @@ 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);
|
||||
unsigned long loc = reserve_region(&page_map, page_size);
|
||||
if(loc == NOMEM)
|
||||
{
|
||||
return ENOMEM;
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("Reserved %08x\n", loc);
|
||||
return loc;
|
||||
}
|
||||
}
|
||||
|
||||
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 +66,23 @@ 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.block_bits = 1;
|
||||
page_map.offset = 0;
|
||||
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, 1) + 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 +90,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 +112,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_heap(&page_map, map, NULL))
|
||||
{
|
||||
return ENOMEM;
|
||||
}
|
||||
else
|
||||
{
|
||||
return ENONE;
|
||||
}
|
||||
}
|
||||
|
||||
physaddr_t create_address_space()
|
||||
|
||||
0
src/x86/entry.S
Executable file → Normal file
0
src/x86/entry.S
Executable file → Normal file
1
src/x86/linker.ld
Executable file → Normal file
1
src/x86/linker.ld
Executable file → Normal file
@@ -37,6 +37,7 @@ SECTIONS
|
||||
_kernel_pstart = PHYSICAL_BASE;
|
||||
_kernel_pend = PHYSICAL_BASE + (4096 * IMAGE_SIZE);
|
||||
_kernel_start = VIRTUAL_BASE;
|
||||
_kernel_tend = VIRTUAL_BASE + SIZEOF(.text) + SIZEOF(.rodata);
|
||||
_kernel_end = VIRTUAL_BASE + (4096 * IMAGE_SIZE);
|
||||
_entry_paddr = _start - _kernel_start + _kernel_pstart;
|
||||
}
|
||||
|
||||
@@ -4,8 +4,8 @@ void read_msr(enum msr_id_t msr_addr, uint64_t *value)
|
||||
{
|
||||
uint64_t v;
|
||||
asm volatile("rdmsr"
|
||||
: "=edx:eax" (v)
|
||||
: "ecx" (msr_addr));
|
||||
: "=A" (v)
|
||||
: "c" (msr_addr));
|
||||
*value = v;
|
||||
}
|
||||
|
||||
@@ -13,5 +13,5 @@ void write_msr(enum msr_id_t msr_addr, uint64_t *value)
|
||||
{
|
||||
uint64_t v = *value;
|
||||
asm volatile("wrmsr"
|
||||
:: "ecx"(msr_addr), "A"(v));
|
||||
:: "c"(msr_addr), "A"(v));
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -133,9 +133,9 @@ int set_pte_type(void *page, int level, int flags)
|
||||
struct page_table_entry_t *entry = get_pte_pointer(page, level);
|
||||
if(entry != NULL)
|
||||
{
|
||||
entry->present = PAGE_PRESENT ? 1 : 0;
|
||||
entry->rw = PAGE_RW ? 1 : 0;
|
||||
entry->usermode = PAGE_USERMODE ? 1 : 0;
|
||||
entry->present = (flags & PAGE_PRESENT) ? 1 : 0;
|
||||
entry->rw = (flags & PAGE_RW) ? 1 : 0;
|
||||
entry->usermode = (flags & PAGE_USERMODE) ? 1 : 0;
|
||||
return 0;
|
||||
}
|
||||
else
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
#include "platform/putc.h"
|
||||
#include "mmgr.h"
|
||||
|
||||
#define COM1 0x3f8
|
||||
|
||||
enum vga_color_t {
|
||||
VGA_COLOR_BLACK = 0,
|
||||
VGA_COLOR_BLUE = 1,
|
||||
@@ -36,18 +38,53 @@ const unsigned int line_count = 25;
|
||||
|
||||
struct cell_t screen[4096 / sizeof(struct cell_t)] __attribute__ ((aligned (4096)));
|
||||
|
||||
static inline void outb(short port, char c)
|
||||
{
|
||||
asm volatile(
|
||||
"outb %1, %0"
|
||||
:: "d" (port), "a" (c)
|
||||
);
|
||||
}
|
||||
|
||||
static inline char inb(short port)
|
||||
{
|
||||
char c;
|
||||
asm volatile(
|
||||
"inb %1, %0"
|
||||
: "=r" (c)
|
||||
: "d" (port)
|
||||
);
|
||||
return c;
|
||||
}
|
||||
|
||||
int initialize_screen()
|
||||
{
|
||||
return map_page(screen, 0x000B8000, PAGE_RW);
|
||||
int status = ENONE;
|
||||
if(status = map_page(screen, 0x000B8000, PAGE_RW))
|
||||
{
|
||||
return status;
|
||||
}
|
||||
outb(COM1 + 1, 0x00);
|
||||
outb(COM1 + 3, 0x80);
|
||||
outb(COM1 + 0, 0x03);
|
||||
outb(COM1 + 1, 0x00);
|
||||
outb(COM1 + 3, 0x03);
|
||||
outb(COM1 + 2, 0xC7);
|
||||
outb(COM1 + 4, 0x0B);
|
||||
outb(COM1 + 4, 0x0F);
|
||||
return status;
|
||||
}
|
||||
|
||||
int putchar(int c)
|
||||
{
|
||||
while((inb(COM1 + 5) & 0x20) == 0);
|
||||
outb(COM1, c);
|
||||
switch(c)
|
||||
{
|
||||
case '\n':
|
||||
cursor += line_width;
|
||||
cursor -= cursor % line_width;
|
||||
outb(COM1, '\r');
|
||||
break;
|
||||
case '\t':
|
||||
cursor += tab_width;
|
||||
|
||||
@@ -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 = {
|
||||
|
||||
Reference in New Issue
Block a user