From ff058d0ae1e63994ffc9df77101644a54bb35b65 Mon Sep 17 00:00:00 2001 From: Starnakin Date: Tue, 29 Oct 2024 15:17:59 +0100 Subject: [PATCH] fix: memory: use rigth index and addr, add page to pt before write it --- headers/memory.h | 5 ++++- src/kernel.c | 3 --- src/memory/page.c | 8 +++++--- src/memory/page_table.c | 12 ++++++++---- 4 files changed, 17 insertions(+), 11 deletions(-) diff --git a/headers/memory.h b/headers/memory.h index dbb1500..10b7f3f 100644 --- a/headers/memory.h +++ b/headers/memory.h @@ -16,6 +16,9 @@ #define HEAP_START ((uint32_t) & _kernel_end - HEAP_END) #define PT_START 256 +#define GET_PAGE_ADDR(pd_index, pt_index) \ + (((pd_index * 1024) + pt_index) * 4096) + extern uint32_t _kernel_end; extern uint32_t boot_page_directory; extern uint32_t *page_directory; @@ -28,4 +31,4 @@ int free_frames(void *frame_ptr, size_t size); void *alloc_pages(size_t size); int free_pages(void *page_ptr, size_t size); void init_page_table(uint32_t page_table[1024], uint16_t start); -int16_t create_page_table(uint16_t pd_index); +int16_t add_page_table(uint16_t pd_index); diff --git a/src/kernel.c b/src/kernel.c index 90219f0..8ec87eb 100644 --- a/src/kernel.c +++ b/src/kernel.c @@ -36,9 +36,6 @@ void kernel_main(void) "I see no way to confuse an array of 256 seg:off pairs with a " "complex 8*unknown quantity -byte descriptor table. -- Troy " "Martin 03:50, 22 March 2009 (UTC)\n"); - char *str = vmalloc(10); - if (str) - strcpy(str, "hello"); /* int i = 0; */ /* while (vmalloc(10)) */ /* ; */ diff --git a/src/memory/page.c b/src/memory/page.c index 0fac9c8..3172bb6 100644 --- a/src/memory/page.c +++ b/src/memory/page.c @@ -2,6 +2,7 @@ #include #include +#include "debug.h" #include "kprintf.h" #include "memory.h" #include "utils.h" @@ -15,11 +16,12 @@ static int16_t find_next_block(size_t nb_pages) { for (uint16_t pd_index = 1; pd_index < 768; pd_index++) { if (page_directory[pd_index] == 0x02) { - if (create_page_table(pd_index) < 0) + if (add_page_table(pd_index) < 0) return -2; } current_pd_index = pd_index; - current_page_table = (uint32_t *)((PT_START + pd_index) * 1024); + current_page_table = + (uint32_t *)GET_PAGE_ADDR(0, pd_index + PT_START); 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 && @@ -56,7 +58,7 @@ void *alloc_pages(size_t size) current_page_table[i] = ((uint32_t)frame & PAGE_MASK) | INIT_FLAGS; } - return (void *)(((current_pd_index * 1024) + index) * 1024); + return (void *)GET_PAGE_ADDR(current_pd_index, index); } int free_pages(void *page_ptr, size_t size) diff --git a/src/memory/page_table.c b/src/memory/page_table.c index b7e4b09..bc23224 100644 --- a/src/memory/page_table.c +++ b/src/memory/page_table.c @@ -1,18 +1,22 @@ +#include "debug.h" +#include "kprintf.h" #include "memory.h" - void init_page_table(uint32_t page_table[1024], uint16_t start) { for (uint16_t i = start; i < 1024; i++) page_table[i] = (i << 12) | 0x03; } -int16_t create_page_table(uint16_t pd_index) +int16_t add_page_table(uint16_t pd_index) { void *frame = alloc_frames(PAGE_SIZE); if (!frame) return -1; - page_directory[pd_index] = ((uint32_t)frame & PAGE_MASK) | 0x03; - uint32_t *page_table = (uint32_t *)((PT_START + pd_index) * 1024); + page_table_default[PT_START + pd_index] = + ((uint32_t)frame & PAGE_MASK) | 0x03; + uint32_t *page_table = + (uint32_t *)GET_PAGE_ADDR(0, PT_START + pd_index); init_page_table(page_table, 0); + page_directory[pd_index] = ((uint32_t)frame & PAGE_MASK) | 0x03; return 0; }