feat: introduced tasking, added paging (no vm), moved malloc, added
syscalls, other stuff
This commit is contained in:
119
kernel/mem/mem.c
119
kernel/mem/mem.c
@@ -2,9 +2,12 @@
|
||||
// Created by rick on 22-04-20.
|
||||
//
|
||||
|
||||
#include "pmm.h"
|
||||
#include <types.h>
|
||||
#include <mem/mem.h>
|
||||
#include <libc/kprintf.h>
|
||||
#include <libk.h>
|
||||
#include <attributes.h>
|
||||
|
||||
#define MEMMAP_ENTRIES 16
|
||||
|
||||
@@ -15,26 +18,13 @@
|
||||
#define MMAP_TYPE_NVS 4
|
||||
#define MMAP_TYPE_BADRAM 5
|
||||
#define MMAP_TYPE_KERNEL 6
|
||||
#define MMAP_TYPE_MALLOC 7
|
||||
|
||||
#define MALLOC_TYPE_FREE 0
|
||||
#define MALLOC_TYPE_USED 1
|
||||
|
||||
#define kilobyte (1024)
|
||||
//#define kernel_start (one_meg)
|
||||
#define kernel_size (32 * kilobyte)
|
||||
#define kernel_end (kernel_start + kernel_size)
|
||||
extern void *kernel_start;
|
||||
//extern void *kernel_end;
|
||||
#define MMAP_TYPE_PAGING 7
|
||||
|
||||
typedef struct {
|
||||
void *address;
|
||||
uint32_t address;
|
||||
uint32_t length;
|
||||
uint32_t type;
|
||||
} __attribute((packed)) mmap_entry;
|
||||
|
||||
int malloc_entries = 0;
|
||||
int malloc_used = 0;
|
||||
} packed mmap_entry;
|
||||
|
||||
int last_memmap_entry = 0;
|
||||
mmap_entry memmap[MEMMAP_ENTRIES] = {
|
||||
@@ -56,114 +46,51 @@ mmap_entry memmap[MEMMAP_ENTRIES] = {
|
||||
{0, 0, 0},
|
||||
};
|
||||
|
||||
typedef struct malloc_map {
|
||||
struct malloc_map *next;
|
||||
struct malloc_map *pref;
|
||||
uint32_t size;
|
||||
int type;
|
||||
} malloc_map;
|
||||
|
||||
void use_mmap_entry(struct multiboot_mmap_entry *entry) {
|
||||
mmap_entry *mm_entry = &memmap[last_memmap_entry++];
|
||||
mm_entry->address = (void *) entry->addr;
|
||||
mm_entry->length = entry->len;
|
||||
mm_entry->type = entry->type;
|
||||
if (last_memmap_entry == 1) {
|
||||
// not using first map entry for now
|
||||
return;
|
||||
}
|
||||
// check if the entry overlaps with the kernel address space
|
||||
if (kernel_start >= mm_entry->address && kernel_start <= mm_entry->address + mm_entry->length) {
|
||||
uint32_t page_after_kernel = kernel_end + PAGE_SIZE & ~(PAGE_SIZE - 1);
|
||||
uint32_t kernel_size_full_pages = page_after_kernel - mm_entry->address;
|
||||
// todo make this something proper
|
||||
struct multiboot_mmap_entry extra_entry;
|
||||
extra_entry.size = entry->size;
|
||||
extra_entry.type = entry->type;
|
||||
extra_entry.addr = entry->addr + kernel_size;
|
||||
extra_entry.len = entry->size - kernel_size;
|
||||
extra_entry.addr = page_after_kernel;
|
||||
extra_entry.len = entry->size - kernel_size_full_pages;
|
||||
use_mmap_entry(&extra_entry);
|
||||
|
||||
mm_entry->length = kernel_size;
|
||||
mm_entry->length = kernel_size_full_pages;
|
||||
mm_entry->type = MMAP_TYPE_KERNEL;
|
||||
}
|
||||
if (mm_entry->type == MMAP_TYPE_AVAILABLE && (unsigned int) mm_entry->length > sizeof(malloc_map)) {
|
||||
mm_entry->type = MMAP_TYPE_MALLOC;
|
||||
malloc_map *map = (malloc_map *) mm_entry->address;
|
||||
map->type = MALLOC_TYPE_FREE;
|
||||
map->size = mm_entry->length - sizeof(malloc_map);
|
||||
map->pref = map;
|
||||
map->next = map;
|
||||
malloc_entries++;
|
||||
}
|
||||
}
|
||||
|
||||
void init_mmap_multiboot(struct multiboot_mmap_entry *entries, uint32_t count) {
|
||||
void mmap_init_multiboot(struct multiboot_mmap_entry *entries, uint32_t count) {
|
||||
for (uint32_t i = 0; i < count; ++i) {
|
||||
use_mmap_entry(&entries[i]);
|
||||
}
|
||||
}
|
||||
|
||||
void split_malloc_map(malloc_map *entry, unsigned int size) {
|
||||
malloc_entries++;
|
||||
malloc_map *new_entry = entry + sizeof(malloc_map) + size;
|
||||
new_entry->size = entry->size - size - sizeof(malloc_map);
|
||||
new_entry->next = entry->next;
|
||||
new_entry->pref = entry;
|
||||
new_entry->type = MALLOC_TYPE_FREE;
|
||||
entry->next->pref = new_entry;
|
||||
entry->next = new_entry;
|
||||
entry->size = size;
|
||||
}
|
||||
|
||||
void *malloc(size_t size) {
|
||||
// todo replace this horrible mess!
|
||||
// this lacks any page alignment and what so ever
|
||||
for (int i = 0; i < MEMMAP_ENTRIES; ++i) {
|
||||
if (memmap[i].type != MMAP_TYPE_MALLOC) {
|
||||
// init page manager
|
||||
for (int i = 0; i < last_memmap_entry; ++i) {
|
||||
mmap_entry *entry = &memmap[i];
|
||||
if (entry->type != MMAP_TYPE_AVAILABLE) {
|
||||
continue;
|
||||
}
|
||||
// get first first_map of address
|
||||
malloc_map *first_map = (malloc_map *) memmap[i].address;
|
||||
malloc_map *current_map = first_map;
|
||||
// iterate through maps
|
||||
do {
|
||||
if (current_map->type == MALLOC_TYPE_USED) {
|
||||
goto malloc_find_next;
|
||||
}
|
||||
if ((unsigned int) current_map->size < size) {
|
||||
goto malloc_find_next;
|
||||
}
|
||||
if ((unsigned int) current_map->size > (size + sizeof(malloc_map))) {
|
||||
// big enough to split
|
||||
split_malloc_map(current_map, size);
|
||||
}
|
||||
malloc_used++;
|
||||
current_map->type = MALLOC_TYPE_USED;
|
||||
return ((void *) current_map) + sizeof(malloc_map);
|
||||
if (entry->address < kernel_start) {
|
||||
continue; // skip for now
|
||||
}
|
||||
|
||||
malloc_find_next:
|
||||
current_map = current_map->next;
|
||||
} while (current_map != first_map);
|
||||
pmm_init((void *) entry->address, entry->length);
|
||||
entry->type = MMAP_TYPE_PAGING;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void free(void *mem) {
|
||||
if (mem == NULL) {
|
||||
return;
|
||||
}
|
||||
malloc_used--;
|
||||
malloc_map *map = (mem - sizeof(malloc_map));
|
||||
map->type = MALLOC_TYPE_FREE;
|
||||
};
|
||||
|
||||
|
||||
void print_malloc_info() {
|
||||
printf("Malloc avail entries: %d\n"
|
||||
"Malloc used entries: %d\n", malloc_entries, malloc_used);
|
||||
}
|
||||
|
||||
|
||||
void print_mmap_info() {
|
||||
printf("Kernel start: %8x\n"
|
||||
"Kernel end: %8x\n", kernel_start, kernel_end);
|
||||
for (int i = 0; i < 16; ++i) {
|
||||
if (memmap[i].type == 0) {
|
||||
break;
|
||||
|
||||
Reference in New Issue
Block a user