From 4339d5e3496b4e9d02a75e244ab1d403241367bf Mon Sep 17 00:00:00 2001 From: "brice.boisson" Date: Mon, 11 Sep 2023 22:46:02 +0900 Subject: [PATCH] Add: starting to allow dynamic page allocation --- src/kernel.lds | 29 ++++++++++++++--------------- src/paging.c | 30 ++++++++++++++++++++++++++++-- 2 files changed, 42 insertions(+), 17 deletions(-) diff --git a/src/kernel.lds b/src/kernel.lds index 2bb7afc..49f1a4a 100644 --- a/src/kernel.lds +++ b/src/kernel.lds @@ -3,21 +3,20 @@ OUTPUT_FORMAT("elf32-i386") SECTIONS { - .text : ALIGN(CONSTANT(MAXPAGESIZE)) { - _TEXT_START_ = .; - *(.multiboot) *(.text) - _TEXT_END_ = .; - } + .text : ALIGN(CONSTANT(MAXPAGESIZE)) { + _TEXT_START_ = .; + *(.multiboot) *(.text) + _TEXT_END_ = .; + } - .data : ALIGN(CONSTANT(MAXPAGESIZE)) { - _DATA_START_ = .; - *(.data) - _DATA_END_ = .; - } + .data : ALIGN(CONSTANT(MAXPAGESIZE)) { + _DATA_START_ = .; + *(.data) + } - .bss : ALIGN(CONSTANT(MAXPAGESIZE)) { - _BSS_START_ = .; - *(.bss) - _BSS_END_ = .; - } + .bss : ALIGN(CONSTANT(MAXPAGESIZE)) { + _BSS_START_ = .; + *(.bss) + _BSS_END_ = .; + } } diff --git a/src/paging.c b/src/paging.c index 98098c9..02d4299 100644 --- a/src/paging.c +++ b/src/paging.c @@ -3,8 +3,30 @@ #include "debug.h" #include "serial.h" -struct page_directory_entry *page_dir = 0x5000; //__attribute__((aligned(4096))); -struct page_table_entry *page_table = 0x6000; //__attribute__((aligned(4096))); +struct page_directory_entry *page_dir; // = 0x3000000; //__attribute__((aligned(4096))); +struct page_table_entry *page_table; // = 0x3010000; //__attribute__((aligned(4096))); +u8 page_avl[131072] = { 0 }; +int page_avl_index = 0x300000 / 8; + +int find_page_avl(void) +{ + for (int i = page_avl_index; i < 131071; i++) { + for (int j = 0; j < 8; j++) { + if ((page_avl[i] & (1 << j)) == 0) { + page_avl_index = i; + return i * 8 + j; + } + } + } + + // TODO : Add memory deallocation policies + return -1; +} + +void set_page(int address) +{ + page_avl[address / 8] |= (1 << (address % 8)); +} struct page_directory_param { u8 P; @@ -75,6 +97,9 @@ struct page_table_entry create_page_table_entry(struct page_table_param param) int make_page(void) { + page_dir = (struct page_directory_entry *) 0x3000000; + page_table = (struct page_table_entry *) 0x3010000; + for (int i = 0; i < 1024; i++) { page_table[i] = create_page_table_entry((struct page_table_param) { @@ -132,6 +157,7 @@ int make_page(void) DEBUG_INFO("address of Page Directory Array: %d", page_dir); DEBUG_INFO("address of the first Page Table array: %d", page_table); + // load page directory and enable paging (cr0 bit 31) asm volatile (" \ mov %0, %%eax \n \ mov %%eax, %%cr3 \n \