wip: change memory to have access to page_tables and allocate these dynamically

This commit is contained in:
2024-10-25 15:11:11 +02:00
parent f5147e78f9
commit a9bfb49bb8
7 changed files with 52 additions and 35 deletions

View File

@ -2,7 +2,6 @@
#include <stddef.h>
#include <stdint.h>
#include "debug.h"
#include "kprintf.h"
#include "memory.h"
#include "utils.h"
@ -10,12 +9,18 @@
#define MAX_TLB_ENTRIES 32
static uint32_t *current_page_table;
static uint16_t current_pd_index;
static int16_t find_next_block(size_t nb_pages)
{
for (uint16_t pd_index = 0; pd_index < 768; pd_index++) {
if (page_directory[pd_index] == 0x02) {
if (create_page_table(pd_index) < 0)
return -2;
}
current_pd_index = pd_index;
current_page_table =
(uint32_t *)(page_directory[pd_index] >> 12);
(uint32_t *)((PT_START + pd_index) * PAGE_SIZE);
for (uint16_t i = 0; i + nb_pages < PT_SIZE; i++) {
uint16_t j;
for (j = 0; current_page_table[i + j] >> 12 == i + j &&
@ -30,13 +35,14 @@ static int16_t find_next_block(size_t nb_pages)
return -1;
}
void *alloc_pages(size_t size, uint32_t **frame_ptr)
void *alloc_pages(size_t size)
{
const uint32_t nb_pages = CEIL(size, PAGE_SIZE);
const int16_t index = find_next_block(nb_pages);
if (index < 0) {
kprintf(KERN_CRIT "Not enough pages (max: %d)\n", PT_SIZE);
kprintf(KERN_CRIT "%d: Not enough pages (max: %d)\n", index,
PT_SIZE);
return NULL;
}
for (size_t i = index; i - (size_t)index < nb_pages; i++) {
@ -48,12 +54,10 @@ void *alloc_pages(size_t size, uint32_t **frame_ptr)
PAGE_SIZE);
return NULL;
}
if (frame_ptr)
*frame_ptr = frame;
current_page_table[i] =
((uint32_t)frame & PAGE_MASK) | INIT_FLAGS;
}
return (void *)((0 * 1024 + index) * PAGE_SIZE);
return (void *)(((current_pd_index * 1024) + index) * PAGE_SIZE);
}
int free_pages(void *page_ptr, size_t size)