diff options
Diffstat (limited to 'kernel')
-rw-r--r-- | kernel/CMakeLists.txt | 1 | ||||
-rw-r--r-- | kernel/kernel/kernel.cc | 26 | ||||
-rw-r--r-- | kernel/mm/virtual_mm/pages.cc | 52 | ||||
-rw-r--r-- | kernel/mm/virtual_mm/virtual_mm.cc | 68 |
4 files changed, 100 insertions, 47 deletions
diff --git a/kernel/CMakeLists.txt b/kernel/CMakeLists.txt index c0903fd..cde48b6 100644 --- a/kernel/CMakeLists.txt +++ b/kernel/CMakeLists.txt @@ -24,6 +24,7 @@ set(SRC mm/physical_mm/bitmap.cc mm/physical_mm/physical_mm.cc mm/virtual_mm/virtual_mm.cc + mm/virtual_mm/pages.cc ) add_executable(kernel ${SRC}) diff --git a/kernel/kernel/kernel.cc b/kernel/kernel/kernel.cc index 54eb0e5..72a182d 100644 --- a/kernel/kernel/kernel.cc +++ b/kernel/kernel/kernel.cc @@ -44,20 +44,24 @@ kernel_main(uint32_t magic, multiboot_info_t *multiboot_info) PhysicalMM::initialize(); VirtualMM::initialize(); - void *x = VirtualMM::find_free_addresses(1046999); - printk("debug", "x(0x%x)", x); + uint32_t *x = (uint32_t *) (5 * MiB); + *x = 8; + printk("debug", "x(0x%x)", *x); -#if 0 - int *x = PhysicalMM::allocate_block(); - /* *x = 20; */ - printk("debug", "x(0x%x)", x); + // void *x = VirtualMM::find_free_addresses(1046999); + // printk("debug", "x(0x%x)", x); - /* virtual_mm_alloc_pages(1); */ - /* void *x = kmalloc(12); */ - /* printk("debug", "x(0x%x)", x); */ + // #if 0 + // int *x = PhysicalMM::allocate_block(); + // /* *x = 20; */ + // printk("debug", "x(0x%x)", x); - printk("\nKernel", "Started."); -#endif + // /* virtual_mm_alloc_pages(1); */ + // /* void *x = kmalloc(12); */ + // /* printk("debug", "x(0x%x)", x); */ + + // printk("\nKernel", "Started."); + // #endif exit(); halt(); /* If exit() fails (on real hardware) */ diff --git a/kernel/mm/virtual_mm/pages.cc b/kernel/mm/virtual_mm/pages.cc new file mode 100644 index 0000000..0f27087 --- /dev/null +++ b/kernel/mm/virtual_mm/pages.cc @@ -0,0 +1,52 @@ +/* + * bubbl + * Copyright (C) 2025 Raghuram Subramani <raghus2247@gmail.com> + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +#include <kernel/halt.h> +#include <libk/stdio.h> +#include <mm/physical_mm.h> +#include <mm/virtual_mm.h> +#include <stdbool.h> +#include <stdint.h> + +namespace VirtualMM +{ + +void * +alloc_pages(uint32_t n_pages) +{ + uint32_t starting_address = (uint32_t) find_free_addresses(n_pages); + if (starting_address == 0) + return 0; + + for (uint32_t i = 0; i < n_pages; i++) { + void *virtual_address = (void *) (starting_address + (i * PAGE_SIZE)); + void *physical_address = PhysicalMM::allocate_block(); + map_page(physical_address, virtual_address); + } + + return (void *) starting_address; +} + +void +free_pages(void *starting_address, uint32_t n_pages) +{ + for (uint32_t i = 0; i < n_pages; i++) + unmap_page((void *) (((uint32_t) starting_address) + (i * 4096))); +} + +} diff --git a/kernel/mm/virtual_mm/virtual_mm.cc b/kernel/mm/virtual_mm/virtual_mm.cc index a3e9fee..80930c8 100644 --- a/kernel/mm/virtual_mm/virtual_mm.cc +++ b/kernel/mm/virtual_mm/virtual_mm.cc @@ -34,7 +34,14 @@ uint32_t *current_page_directory = 0; /* Kernel's page directory */ uint32_t page_directory[1024] ALIGNED(4096); /* Page table for the first 4 MiB */ -uint32_t page_table[1024] ALIGNED(4096); +uint32_t fourMiB_page_table[1024] ALIGNED(4096); +/* Page table for the next 4 MiB */ +uint32_t eightMiB_page_table[1024] ALIGNED(4096); + +/* Let's reserve a page table at the end of each new page table we allocate, so + * that we know that at any point time, we will always have space for a new + * page table */ +uint32_t *next_page_table; ALWAYS_INLINE void load_page_directory(uint32_t *page_directory) @@ -65,26 +72,37 @@ enable_paging(void) void initialize(void) { - for (uint32_t i = 0; i < 1024; i++) - page_table[i] = 0; + /* Zero out the page tables and directories */ + for (uint32_t i = 0; i < 1024; i++) { + fourMiB_page_table[i] = 0; + eightMiB_page_table[i] = 0; + page_directory[i] = 0; + } /* Identity map the first 4MiB, excluding the 4th MiB * (maps 4KiB 1024 times) */ for (uint32_t i = 0; i < 1024; i++) - page_table[i] = PTE_FRAME(i) | PTE_PRESENT(1) | PTE_WRITABLE(1); + fourMiB_page_table[i] = PTE_FRAME(i) | PTE_PRESENT(1) | PTE_WRITABLE(1); + /* Identity map the next 4MiB */ for (uint32_t i = 0; i < 1024; i++) - page_directory[i] = 0; + eightMiB_page_table[i] + = PTE_FRAME(i + 1024) | PTE_PRESENT(1) | PTE_WRITABLE(1); + + /* Set up the page directory entries */ + uint32_t *fourMiB_pd_entry = &page_directory[0]; + *fourMiB_pd_entry = PDE_FRAME((uint32_t) fourMiB_page_table) | PDE_PRESENT(1) + | PDE_WRITABLE(1); - uint32_t *pd_entry = &page_directory[0]; - *pd_entry - = PDE_FRAME((uint32_t) page_table) | PDE_PRESENT(1) | PDE_WRITABLE(1); + uint32_t *eightMiB_pd_entry = &page_directory[1]; + *eightMiB_pd_entry = PDE_FRAME((uint32_t) eightMiB_page_table) + | PDE_PRESENT(1) | PDE_WRITABLE(1); switch_page_directory(page_directory); enable_paging(); } -ALWAYS_INLINE uint32_t * +uint32_t * get_or_make_table(uint32_t *pd_entry) { uint32_t *table; @@ -170,7 +188,8 @@ find_free_addresses(uint32_t n) pt_index = 0; } - if (table_is_present) { + /* If the table is present, and if the PTE is present, then break */ + if (table_is_present) if (PTE_IS_PRESENT(&table[pt_index])) { /* Since we have some used address at some point between j and * count, we can't find n consecutive free addresses in between j @@ -179,9 +198,9 @@ find_free_addresses(uint32_t n) break; } - count++; - } else - count++; + /* TODO: This can be easily optimized if the table is not present. + * (count += 4096, since we know that the table is not present) */ + count++; if (count == n) return (void *) VIRTUAL_ADDRESS(starting_pd_index, @@ -194,27 +213,4 @@ find_free_addresses(uint32_t n) return 0; } -void * -alloc_pages(uint32_t n_pages) -{ - uint32_t starting_address = (uint32_t) find_free_addresses(n_pages); - if (starting_address == 0) - return 0; - - for (uint32_t i = 0; i < n_pages; i++) { - void *virtual_address = (void *) (starting_address + (i * PAGE_SIZE)); - void *physical_address = PhysicalMM::allocate_block(); - map_page(physical_address, virtual_address); - } - - return (void *) starting_address; -} - -void -free_pages(void *starting_address, uint32_t n_pages) -{ - for (uint32_t i = 0; i < n_pages; i++) - unmap_page((void *) (((uint32_t) starting_address) + (i * 4096))); -} - } |