wip: page_tables allocation

This commit is contained in:
0x35c 2024-10-21 16:29:50 +02:00
parent 4337bc11c8
commit f5147e78f9
6 changed files with 67 additions and 42 deletions

View File

@ -1,6 +1,7 @@
#pragma once #pragma once
#include <stddef.h> #include <stddef.h>
#include <stdint.h>
#define PRESENT (1 << 0) #define PRESENT (1 << 0)
#define RW (1 << 1) #define RW (1 << 1)
@ -8,11 +9,18 @@
#define ACCESSED (1 << 4) #define ACCESSED (1 << 4)
#define INIT_FLAGS (PRESENT | RW | SUPERVISOR) #define INIT_FLAGS (PRESENT | RW | SUPERVISOR)
#define PAGE_SIZE 4096 #define PAGE_SIZE 4096
#define PT_SIZE 1024
#define PD_SIZE 1024
#define PAGE_MASK 0xFFFFF000 #define PAGE_MASK 0xFFFFF000
#define HEAP_END 0xC0000000 #define HEAP_END 0xC0000000
extern uint32_t boot_page_directory;
extern uint32_t *page_directory;
extern uint32_t page_table_default[1024];
uint32_t *virt_to_phys(uint32_t *virt_addr);
void init_memory(void); void init_memory(void);
void *alloc_frames(size_t size); void *alloc_frames(size_t size);
int free_frames(void *frame_ptr, size_t size); int free_frames(void *frame_ptr, size_t size);
void *alloc_pages(size_t size); void *alloc_pages(size_t size, uint32_t **frame_ptr);
int free_pages(void *page_ptr, size_t size); int free_pages(void *page_ptr, size_t size);

View File

@ -40,19 +40,21 @@ void kernel_main(void)
/* strcpy(str, "hello"); */ /* strcpy(str, "hello"); */
/* int i = 0; */ /* int i = 0; */
/* while (vmalloc(10)) */ /* while (vmalloc(10)) */
/* if (i++ > 11000) */ /* ; */
/* kprintf("str: %s at %d\n", str, i); */ /* if (i++ > 70000) */
void *tab[1024]; /* kprintf("%d\n", i); */
for (int i = 0; i < 1024; i++) { /* void *tab[1024]; */
tab[i] = alloc_pages(1); /* for (int i = 0; i < 1023; i++) { */
/* PRINT_INT(i); */ /* tab[i] = alloc_pages(1); */
/* PRINT_PTR(tab[i]); */ /* PRINT_INT(i); */
if (tab[i]) /* PRINT_PTR(tab[i]); */
memset(tab[i], i, 4096); /* if (!tab[i]) */
} /* break; */
/* memset(tab[i], i, 4096); */
/* } */
/* PRINT_UINT(((uint8_t *)tab[0])[0]); */ /* PRINT_UINT(((uint8_t *)tab[0])[0]); */
for (int i = 0; i < 10; i++) /* for (int i = 0; i < 10; i++) */
PRINT_PTR(tab[i]); /* PRINT_UINT(((uint8_t *)tab[i])[0]); */
/* PRINT_UINT(((uint8_t *)tab[i])[0]); */ /* PRINT_PTR(tab[i]); */
shell_init(); /* shell_init(); */
} }

View File

@ -14,7 +14,7 @@ static uint32_t remaining_frames = MAX_FRAMES;
static int32_t find_next_block(size_t nb_frames) static int32_t find_next_block(size_t nb_frames)
{ {
for (uint32_t i = 1; i + nb_frames < MAX_FRAMES - 1; i++) { for (uint32_t i = 1024; i + nb_frames < MAX_FRAMES - 1; i++) {
uint32_t j; uint32_t j;
for (j = 0; frame_table[i + j] == 0 && j < nb_frames; j++) for (j = 0; frame_table[i + j] == 0 && j < nb_frames; j++)
; ;

View File

@ -1,13 +1,22 @@
#include "memory.h" #include "memory.h"
#include "debug.h" #include "debug.h"
#include "kprintf.h" #include "kprintf.h"
#include "string.h"
#include <stdint.h> #include <stdint.h>
extern uint32_t boot_page_directory;
uint32_t *page_directory = &boot_page_directory; uint32_t *page_directory = &boot_page_directory;
uint32_t page_table1[1024] __attribute__((aligned(4096))); uint32_t page_table_default[1024] __attribute__((aligned(4096)));
static void alloc_page_tables(void)
{
uint32_t *frame_ptr = NULL;
for (int16_t i = 1; i < 768; i++) {
uint32_t *page_table = alloc_pages(PAGE_SIZE, &frame_ptr);
for (int16_t j = 0; j < 1024; j++)
page_table[j] = j << 12 | 0x03;
page_directory[i] = ((uint32_t)frame_ptr & PAGE_MASK) | 0x03;
}
}
void init_memory(void) void init_memory(void)
{ {
@ -15,6 +24,7 @@ void init_memory(void)
for (int16_t i = 0; i < 0x300; i++) for (int16_t i = 0; i < 0x300; i++)
page_directory[i] = 0x02; page_directory[i] = 0x02;
for (int16_t i = 0; i < 1024; i++) for (int16_t i = 0; i < 1024; i++)
page_table1[i] = (i << 12) | 0x03; page_table_default[i] = (i << 12) | 0x03;
page_directory[342] = ((uint32_t)page_table1 - HEAP_END) | 0x03; page_directory[0] = ((uint32_t)page_table_default - HEAP_END) | 0x03;
alloc_page_tables();
} }

View File

@ -7,26 +7,30 @@
#include "memory.h" #include "memory.h"
#include "utils.h" #include "utils.h"
#define PT_SIZE 1024
#define MAX_TLB_ENTRIES 32 #define MAX_TLB_ENTRIES 32
extern uint32_t page_table1[PT_SIZE]; static uint32_t *current_page_table;
static int16_t find_next_block(size_t nb_pages) static int16_t find_next_block(size_t nb_pages)
{ {
for (uint16_t i = 1; i + nb_pages < PT_SIZE - 1; i++) { for (uint16_t pd_index = 0; pd_index < 768; pd_index++) {
uint16_t j; current_page_table =
for (j = 0; page_table1[i + j] >> 12 == i + j && j < nb_pages; (uint32_t *)(page_directory[pd_index] >> 12);
j++) for (uint16_t i = 0; i + nb_pages < PT_SIZE; i++) {
; uint16_t j;
if (j == nb_pages) for (j = 0; current_page_table[i + j] >> 12 == i + j &&
return i; j < nb_pages;
i += j; j++)
;
if (j == nb_pages)
return i;
i += j;
}
} }
return -1; return -1;
} }
void *alloc_pages(size_t size) void *alloc_pages(size_t size, uint32_t **frame_ptr)
{ {
const uint32_t nb_pages = CEIL(size, PAGE_SIZE); const uint32_t nb_pages = CEIL(size, PAGE_SIZE);
const int16_t index = find_next_block(nb_pages); const int16_t index = find_next_block(nb_pages);
@ -39,15 +43,17 @@ void *alloc_pages(size_t size)
void *frame = alloc_frames(PAGE_SIZE); void *frame = alloc_frames(PAGE_SIZE);
if (!frame) { if (!frame) {
for (size_t j = index; j < i; j++) for (size_t j = index; j < i; j++)
free_frames((void *)(page_table1[j] >> 12), free_frames(
PAGE_SIZE); (void *)(current_page_table[j] >> 12),
PAGE_SIZE);
return NULL; return NULL;
} }
/* assert(page_table1[i] >> 12 == i); */ if (frame_ptr)
page_table1[i] = ((uint32_t)frame & PAGE_MASK) | INIT_FLAGS; *frame_ptr = frame;
current_page_table[i] =
((uint32_t)frame & PAGE_MASK) | INIT_FLAGS;
} }
/* PRINT_PTR(page_table1[index]); */ return (void *)((0 * 1024 + index) * PAGE_SIZE);
return (void *)(index * PAGE_SIZE);
} }
int free_pages(void *page_ptr, size_t size) int free_pages(void *page_ptr, size_t size)
@ -66,8 +72,8 @@ int free_pages(void *page_ptr, size_t size)
return -1; return -1;
} }
for (size_t i = page_index; i < page_index + nb_pages; i++) { for (size_t i = page_index; i < page_index + nb_pages; i++) {
free_frames((void *)(page_table1[i] >> 12), PAGE_SIZE); free_frames((void *)(current_page_table[i] >> 12), PAGE_SIZE);
page_table1[i] = i << 12 | INIT_FLAGS; current_page_table[i] = i << 12 | INIT_FLAGS;
} }
return 0; return 0;
} }

View File

@ -49,12 +49,11 @@ static void new_block(Zone *zone, uint32_t zone_size)
int new_vzone(block_type_t type, uint32_t size) int new_vzone(block_type_t type, uint32_t size)
{ {
void *heap = alloc_pages(size); void *heap = alloc_pages(size, NULL);
if (heap == NULL) { if (heap == NULL) {
kprintf(KERN_ERR "error: alloc_pages failed\n"); kprintf(KERN_ERR "error: alloc_pages failed\n");
return (-1); return (-1);
} }
PRINT_PTR(heap);
Zone *zone = (Zone *)heap; Zone *zone = (Zone *)heap;
zone->type = type; zone->type = type;