wip: frame_allocator with multiboot zones

This commit is contained in:
0x35c 2024-11-18 15:12:46 +01:00
parent d8d31d959f
commit e8fd6c55eb
4 changed files with 70 additions and 0 deletions

View File

@ -21,6 +21,14 @@
#define GET_PAGE_ADDR(pd_index, pt_index) \ #define GET_PAGE_ADDR(pd_index, pt_index) \
((((uint32_t)pd_index * 1024) + (uint32_t)pt_index) * 4096) ((((uint32_t)pd_index * 1024) + (uint32_t)pt_index) * 4096)
struct frame_zone {
void *addr;
uint32_t *frame_table;
uint32_t size;
uint32_t remaining_frames;
struct frame_zone *next;
};
extern uint32_t _kernel_end; extern uint32_t _kernel_end;
extern uint32_t boot_page_directory; extern uint32_t boot_page_directory;
extern uint32_t *page_directory; extern uint32_t *page_directory;
@ -28,6 +36,7 @@ extern uint32_t page_table_default[1024];
extern uint32_t mem_size; extern uint32_t mem_size;
extern multiboot_memory_map_t *mmap_addr; extern multiboot_memory_map_t *mmap_addr;
extern multiboot_uint32_t mmap_length; extern multiboot_uint32_t mmap_length;
extern struct frame_zone *head;
uint32_t *virt_to_phys(uint32_t *virt_addr); uint32_t *virt_to_phys(uint32_t *virt_addr);
void init_memory(multiboot_info_t *mbd, uint32_t magic); void init_memory(multiboot_info_t *mbd, uint32_t magic);

View File

@ -17,6 +17,9 @@ void kpanic(const char *format, ...)
va_start(va, format); va_start(va, format);
kvprintf(format, &va); kvprintf(format, &va);
va_end(va); va_end(va);
uint32_t faulting_address;
__asm__ __volatile__("mov %%cr2, %0" : "=r"(faulting_address));
kprintf("fault at address: %p\n", faulting_address);
/* for (int i = 16; i < 32; i++) */ /* for (int i = 16; i < 32; i++) */
/* kprintf("%p\n", page_table1[i]); */ /* kprintf("%p\n", page_table1[i]); */
/* show_valloc_mem(); */ /* show_valloc_mem(); */

View File

@ -20,6 +20,12 @@ void *alloc_frames(size_t size)
} }
PRINT_PTR(mmmt); PRINT_PTR(mmmt);
} }
PRINT_PTR(head);
PRINT_PTR(head->addr);
PRINT_PTR(head->frame_table);
PRINT_UINT(head->size);
PRINT_UINT(head->remaining_frames);
PRINT_PTR(head->next);
return NULL; return NULL;
} }

View File

@ -1,6 +1,7 @@
#include "memory.h" #include "memory.h"
#include "debug.h" #include "debug.h"
#include "kprintf.h" #include "kprintf.h"
#include "string.h"
#include "utils.h" #include "utils.h"
#include <stdint.h> #include <stdint.h>
@ -8,9 +9,11 @@
uint32_t *page_directory = &boot_page_directory; uint32_t *page_directory = &boot_page_directory;
uint32_t page_table_default[1024] __attribute__((aligned(PAGE_SIZE))); uint32_t page_table_default[1024] __attribute__((aligned(PAGE_SIZE)));
uint32_t multiboot_page_table[1024] __attribute__((aligned(PAGE_SIZE))); uint32_t multiboot_page_table[1024] __attribute__((aligned(PAGE_SIZE)));
uint32_t frame_zones_page_table[1024] __attribute__((aligned(PAGE_SIZE)));
uint32_t mem_size; uint32_t mem_size;
multiboot_memory_map_t *mmap_addr; multiboot_memory_map_t *mmap_addr;
multiboot_uint32_t mmap_length; multiboot_uint32_t mmap_length;
struct frame_zone *head;
static void init_multiboot(multiboot_info_t *mbd, uint32_t magic) static void init_multiboot(multiboot_info_t *mbd, uint32_t magic)
{ {
@ -42,6 +45,54 @@ static void init_multiboot(multiboot_info_t *mbd, uint32_t magic)
mmap_length = mbd_virt->mmap_length / sizeof(multiboot_memory_map_t); mmap_length = mbd_virt->mmap_length / sizeof(multiboot_memory_map_t);
} }
static void add_frame_node(multiboot_memory_map_t *mmmt)
{
static uint32_t index;
void *zone = (void *)mmmt->addr;
init_page_table(frame_zones_page_table, 0);
page_directory[1022] =
((uint32_t)frame_zones_page_table - HEAP_END) | 0x03;
frame_zones_page_table[index++] =
((uint32_t)zone & PAGE_MASK) | INIT_FLAGS;
struct frame_zone *current =
(struct frame_zone *)GET_PAGE_ADDR(1022, 0);
memset(current, 0, sizeof(struct frame_zone));
current->addr = (void *)mmmt->addr;
current->frame_table = (uint32_t *)current + sizeof(struct frame_zone);
const size_t frame_table_size =
CEIL(current->size - sizeof(struct frame_zone), PAGE_SIZE * 32);
for (uint32_t i = index; index - i < CEIL(frame_table_size, PAGE_SIZE);
index++)
frame_zones_page_table[index] =
((uint32_t)zone + PAGE_SIZE & PAGE_MASK) | INIT_FLAGS;
// glhf reading this bozo
current->size = (mmmt->len - (sizeof(struct frame_zone) +
(mmmt->len / PAGE_SIZE) / 32));
current->remaining_frames = current->size / PAGE_SIZE;
current->next = NULL;
struct frame_zone *it = head;
if (!it) {
head = current;
return;
}
while (it->next)
it = it->next;
it->next = current;
}
static void init_frame_zones(void)
{
for (uint32_t i = 0; i < mmap_length; i++) {
multiboot_memory_map_t *mmmt =
(multiboot_memory_map_t *)mmap_addr + i;
if (mmmt->type == MULTIBOOT_MEMORY_AVAILABLE)
add_frame_node(mmmt);
}
}
void init_memory(multiboot_info_t *mbd, uint32_t magic) void init_memory(multiboot_info_t *mbd, uint32_t magic)
{ {
assert(page_directory); assert(page_directory);
@ -50,4 +101,5 @@ void init_memory(multiboot_info_t *mbd, uint32_t magic)
init_page_table(page_table_default, 0); init_page_table(page_table_default, 0);
page_directory[0] = ((uint32_t)page_table_default - HEAP_END) | 0x03; page_directory[0] = ((uint32_t)page_table_default - HEAP_END) | 0x03;
init_multiboot(mbd, magic); init_multiboot(mbd, magic);
init_frame_zones();
} }