From f425d78e4d55b8459cbb8ae609de9fa40ebf622e Mon Sep 17 00:00:00 2001 From: Victor Cornille Date: Wed, 11 Sep 2024 18:28:01 +0200 Subject: [PATCH] + | Kget_size and fixed panic issue --- drivers/descriptor/descriptor.h | 2 +- drivers/descriptor/isr.c | 2 +- kernel/kernel.c | 6 ++++-- memory/vmm/kmalloc.c | 15 ++++++++++++++- memory/vmm/vmm.c | 20 ++++++++++++++++++-- memory/vmm/vmm.h | 2 ++ 6 files changed, 40 insertions(+), 7 deletions(-) diff --git a/drivers/descriptor/descriptor.h b/drivers/descriptor/descriptor.h index 18a3ce9..c591194 100644 --- a/drivers/descriptor/descriptor.h +++ b/drivers/descriptor/descriptor.h @@ -5,7 +5,7 @@ #include "../../lib/stdlib/stdlib.h" #include "../../lib/stdio/stdio.h" #include "../../lib/string/string.h" -#include "../../memory/pmm/pmm.h" +#include "../../memory/vmm/vmm.h" extern void isr0(); extern void isr1(); diff --git a/drivers/descriptor/isr.c b/drivers/descriptor/isr.c index 797c491..83ea2c3 100644 --- a/drivers/descriptor/isr.c +++ b/drivers/descriptor/isr.c @@ -44,7 +44,7 @@ void panic(registers_t* r) { asm volatile("cli"); uint32_t stack = r->esp; printf("Number: %d | Message: %s\n", r->int_no, exception_msg[r->int_no]); - uint32_t* cp_stack = (uint32_t*)pmm_alloc_block(); + uint32_t* cp_stack = (uint32_t*)kmalloc(0x1000); for (uint32_t* ptr = (uint32_t*)stack; *ptr && ptr < (uint32_t*)stack + 0x1000; ptr++) { *cp_stack++ = *ptr; } diff --git a/kernel/kernel.c b/kernel/kernel.c index 29bd618..0c3356b 100644 --- a/kernel/kernel.c +++ b/kernel/kernel.c @@ -59,9 +59,11 @@ void kernel_main(uint32_t magic, uint32_t addr) { asm volatile("mov %%cr0, %0" : "=r" (cr0)); if (cr0 & 0x80000000) printf("Paging enabled: cr0 == %p\n", cr0); else printf("Paging disabled: cr0 == %p\n", cr0); - void* test = kmalloc(0x1001); + uint32_t* test = kmalloc(0x1001); void* test2 = kmalloc(1); kfree(test); void* test3 = kmalloc(0x1001); - printf("%p | %p | %p\n", test, test2, test3); + *test = 5; + printf("test %p | test2 %p | test3 %p\n", test, test2, test3); + printf("sizeof(test3) == %d bytes\n", kget_size(test3)); } diff --git a/memory/vmm/kmalloc.c b/memory/vmm/kmalloc.c index 50b635c..39b7388 100644 --- a/memory/vmm/kmalloc.c +++ b/memory/vmm/kmalloc.c @@ -21,6 +21,7 @@ void* kmalloc(size_t size) { } if (memory_map[i].free && memory_map[i].nb_blocks == total_pages_needed) { memory_map[i].free = 1; + vmm_set_flags_pages(memory_map[i].virt_addr, memory_map[i].nb_blocks, I86_PTE_WRITABLE, 1); return (void*)memory_map[i].virt_addr; } } @@ -34,6 +35,7 @@ void* kmalloc(size_t size) { } } } + // vmm_alloc_blocks sets the flags, no need to do it again memory_map[i].virt_addr = (uint32_t)block_virt_addr; memory_map[i].nb_blocks = total_pages_needed; memory_map[i].free = 0; @@ -42,12 +44,23 @@ void* kmalloc(size_t size) { } void kfree(void* virt_addr) { - for (size_t i = 0; i < DTABLE_ADDR_SPACE_SIZE / 2; i++) { + for (size_t i = 0; i < MAX_ALLOC_SAME_TIME; i++) { if (!memory_map[i].free && !memory_map[i].nb_blocks && !memory_map[i].virt_addr) break; if (memory_map[i].virt_addr == (uint32_t)virt_addr) { + vmm_set_flags_pages(memory_map[i].virt_addr, memory_map[i].nb_blocks, I86_PTE_WRITABLE, 0); memory_map[i].free = 1; return; } } printf("ERROR: Invalid free\n"); } + +uint32_t kget_size(void* virt_addr) { + for (size_t i = 0; i < MAX_ALLOC_SAME_TIME; i++) { + if (!memory_map[i].free && !memory_map[i].nb_blocks && !memory_map[i].virt_addr) break; + if (memory_map[i].virt_addr == (uint32_t)virt_addr) { + return memory_map[i].nb_blocks * PAGE_SIZE; + } + } + return 0; +} diff --git a/memory/vmm/vmm.c b/memory/vmm/vmm.c index 58cbf80..bf15308 100644 --- a/memory/vmm/vmm.c +++ b/memory/vmm/vmm.c @@ -78,7 +78,7 @@ uint32_t vmm_find_next_free() { pt_entry new_page = 0; pt_entry* new_page_p = &new_page; if (!vmm_alloc_page(new_page_p)) continue; - if (i > 0) pt_entry_del_attrib(new_page_p, I86_PDE_PRESENT); + if (i > 0) pt_entry_del_attrib(new_page_p, I86_PDE_PRESENT | I86_PTE_WRITABLE); new_table->m_entries[i] = new_page; } pd_entry* new_entry = &(_current_dir->m_entries[i]); @@ -114,7 +114,7 @@ uint32_t vmm_find_next_free_s(size_t nb_blocks) { pt_entry new_page = 0; pt_entry* new_page_p = &new_page; if (!vmm_alloc_page(new_page_p)) continue; - if (i > nb_blocks - 1) pt_entry_del_attrib(new_page_p, I86_PDE_PRESENT); + if (i > nb_blocks - 1) pt_entry_del_attrib(new_page_p, I86_PDE_PRESENT | I86_PTE_WRITABLE); new_table->m_entries[i] = new_page; } pd_entry* new_entry = &(_current_dir->m_entries[i]); @@ -147,6 +147,22 @@ void vmm_free_blocks(uint32_t virtual_addr, uint32_t nb_blocks) { } } +void vmm_set_flags_pages(uint32_t virt_addr, uint32_t nb_blocks, uint32_t flags, uint8_t set) { + uint32_t pd_index = PAGE_DIR_INDEX(virt_addr); + uint32_t pt_index = PAGE_TAB_INDEX(virt_addr); + pt_entry* page_table = (pt_entry*)pd_entry_pfn(_current_dir->m_entries[pd_index]); + if (set) + for (size_t i = 0; i/PAGE_SIZE < nb_blocks; i+=PAGE_SIZE) { + pt_entry_add_attrib(page_table + i + pt_index, flags); + } + else { + for (size_t i = 0; i/PAGE_SIZE < nb_blocks; i+=PAGE_SIZE) { + pt_entry_del_attrib(page_table + i + pt_index, flags); + } + } +} + + extern uint32_t start_kernel_virt; extern uint32_t start_kernel; diff --git a/memory/vmm/vmm.h b/memory/vmm/vmm.h index 67b01ee..f874a24 100644 --- a/memory/vmm/vmm.h +++ b/memory/vmm/vmm.h @@ -25,9 +25,11 @@ typedef struct { } p_dir; void vmm_init(); +void vmm_set_flags_pages(uint32_t virt_addr, uint32_t nb_blocks, uint32_t flags, uint8_t set); void* vmm_alloc_blocks(size_t size); void vmm_free_blocks(uint32_t virtual_addr, uint32_t nb_blocks); void* kmalloc(size_t size); void kfree(void* virt_addr); +uint32_t kget_size(void* virt_addr); #endif