Skip to content

Commit

Permalink
+ | Kget_size and fixed panic issue
Browse files Browse the repository at this point in the history
  • Loading branch information
BolvicBolvicovic committed Sep 11, 2024
1 parent bb3e4a9 commit f425d78
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 7 deletions.
2 changes: 1 addition & 1 deletion drivers/descriptor/descriptor.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion drivers/descriptor/isr.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
6 changes: 4 additions & 2 deletions kernel/kernel.c
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
15 changes: 14 additions & 1 deletion memory/vmm/kmalloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand All @@ -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;
Expand All @@ -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;
}
20 changes: 18 additions & 2 deletions memory/vmm/vmm.c
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
Expand Down Expand Up @@ -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]);
Expand Down Expand Up @@ -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;

Expand Down
2 changes: 2 additions & 0 deletions memory/vmm/vmm.h
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit f425d78

Please sign in to comment.