diff options
author | Raghuram Subramani <raghus2247@gmail.com> | 2025-02-04 15:17:55 +0530 |
---|---|---|
committer | Raghuram Subramani <raghus2247@gmail.com> | 2025-02-04 15:19:00 +0530 |
commit | 869d9bcea81b2ad439cd4498eabc1136e945d2ce (patch) | |
tree | 8dac300410569fdeadb7d18677b4626e82db3efc | |
parent | 1ac479c99f2d4548285893e76df4e0ffe9ce42cc (diff) |
mm: virtual_mm: Write a basic page table allocator
-rw-r--r-- | kernel/CMakeLists.txt | 1 | ||||
-rw-r--r-- | kernel/include/mm/page_table_allocator.h | 32 | ||||
-rw-r--r-- | kernel/include/mm/virtual_mm.h | 2 | ||||
-rw-r--r-- | kernel/kernel/kernel.cc | 18 | ||||
-rw-r--r-- | kernel/mm/physical_mm/physical_mm.cc | 4 | ||||
-rw-r--r-- | kernel/mm/virtual_mm/page_table_allocator.cc | 74 | ||||
-rw-r--r-- | kernel/mm/virtual_mm/pages.cc | 3 | ||||
-rw-r--r-- | kernel/mm/virtual_mm/virtual_mm.cc | 21 |
8 files changed, 128 insertions, 27 deletions
diff --git a/kernel/CMakeLists.txt b/kernel/CMakeLists.txt index ba0843c..cd9b187 100644 --- a/kernel/CMakeLists.txt +++ b/kernel/CMakeLists.txt @@ -28,6 +28,7 @@ set(SRC mm/physical_mm/physical_mm.cc mm/virtual_mm/pages.cc mm/virtual_mm/virtual_mm.cc + mm/virtual_mm/page_table_allocator.cc boot/init/crtn.s ) diff --git a/kernel/include/mm/page_table_allocator.h b/kernel/include/mm/page_table_allocator.h new file mode 100644 index 0000000..dc4deb6 --- /dev/null +++ b/kernel/include/mm/page_table_allocator.h @@ -0,0 +1,32 @@ +/* + * 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/>. + */ + +#ifndef __mm_page_table_allocator_h +#define __mm_page_table_allocator_h + +#include <stdint.h> + +namespace PageTableAllocator +{ + +uint32_t *allocate(void); +void prepare(void); + +} + +#endif diff --git a/kernel/include/mm/virtual_mm.h b/kernel/include/mm/virtual_mm.h index 70c9510..5230f03 100644 --- a/kernel/include/mm/virtual_mm.h +++ b/kernel/include/mm/virtual_mm.h @@ -72,6 +72,8 @@ namespace VirtualMM { +uint32_t *get_page_directory(void); + /* * Loads a given page directory into CR0 */ diff --git a/kernel/kernel/kernel.cc b/kernel/kernel/kernel.cc index c7a8fc5..a1e6856 100644 --- a/kernel/kernel/kernel.cc +++ b/kernel/kernel/kernel.cc @@ -24,6 +24,7 @@ #include <libk/stdio.h> #include <mm/memory_map.h> #include <mm/multiboot.h> +#include <mm/page_table_allocator.h> #include <mm/physical_mm.h> #include <mm/virtual_mm.h> #include <stdint.h> @@ -43,21 +44,12 @@ kernel_main(uint32_t magic, multiboot_info_t *multiboot_info) MemoryMap::load(multiboot_info); PhysicalMM::initialize(); VirtualMM::initialize(); - LibAlloc::initialize(); + // LibAlloc::initialize(); - uint32_t *page = (uint32_t *) VirtualMM::alloc_pages(1); - printk("debug", "page(0x%x)", page); - page = (uint32_t *) VirtualMM::alloc_pages(1); - printk("debug", "page(0x%x)", page); + PageTableAllocator::prepare(); - for (uint32_t i = 0; i < 1024; i++) - page[i] = i; - - for (uint32_t i = 0; i < 1024; i++) - if (page[i] != i) { - printk("debug", "i(%lu) page[i](%lu)", i, page[i]); - halt(); - } + // uint32_t *page = (uint32_t *) VirtualMM::alloc_pages(1); + // printk("debug", "page(0x%x)", page); // int *x = (int *) LibAlloc::kmalloc(sizeof(int) * 8192); // for (uint32_t i = 0; i < 8192; i++) diff --git a/kernel/mm/physical_mm/physical_mm.cc b/kernel/mm/physical_mm/physical_mm.cc index fb0dd76..12e2bed 100644 --- a/kernel/mm/physical_mm/physical_mm.cc +++ b/kernel/mm/physical_mm/physical_mm.cc @@ -112,8 +112,8 @@ initialize(void) uint32_t kernel_size = ((uint32_t) &kernel_end) - ((uint32_t) &kernel_start); deinitialize_region((uint32_t) &kernel_start, kernel_size); - /* Deinitialize first 4MiB */ - deinitialize_region(0, 4 * MiB); + /* Deinitialize first 8MiB */ + deinitialize_region(0, 8 * MiB); l_lock.release(); diff --git a/kernel/mm/virtual_mm/page_table_allocator.cc b/kernel/mm/virtual_mm/page_table_allocator.cc new file mode 100644 index 0000000..3ec4896 --- /dev/null +++ b/kernel/mm/virtual_mm/page_table_allocator.cc @@ -0,0 +1,74 @@ +/* + * 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 <common.h> +#include <kernel/halt.h> +#include <libk/stdio.h> +#include <mm/page_table_allocator.h> +#include <mm/physical_mm.h> +#include <mm/virtual_mm.h> +#include <stddef.h> + +namespace PageTableAllocator +{ + +uint32_t *l_page_directory = 0; +uint32_t *l_heap = NULL; +uint16_t l_table_index = 0; + +static void +make_table(uint32_t *table_address) +{ + uint32_t *table = table_address; + for (uint32_t i = 0; i < 1024; i++) + table[i] = PTE_FRAME((uint32_t) PhysicalMM::allocate_block()) + | PTE_PRESENT(1) | PTE_WRITABLE(1); + + void *starting_address = VirtualMM::find_free_addresses(4 * KiB); + uint32_t *pd_entry = &l_page_directory[GET_PD_INDEX(starting_address)]; + *pd_entry = PDE_FRAME((uint32_t) table) | PDE_PRESENT(1) | PDE_WRITABLE(1); + + l_heap = (uint32_t *) starting_address; + l_table_index = 0; +} + +void +prepare(void) +{ + if (l_page_directory != VirtualMM::get_page_directory()) + l_page_directory = VirtualMM::get_page_directory(); + + /* Initial table */ + if (l_heap == NULL) + make_table((uint32_t *) (7 * MiB)); + + /* If we're gonna run out of space, allocate a new table with the remaining + * page */ + if (l_table_index == 1023) + make_table(allocate()); +} + +uint32_t * +allocate(void) +{ + uint32_t *next_table = l_heap + (l_table_index * 4 * KiB); + l_table_index++; + return next_table; +} + +} diff --git a/kernel/mm/virtual_mm/pages.cc b/kernel/mm/virtual_mm/pages.cc index 55b3e91..7778418 100644 --- a/kernel/mm/virtual_mm/pages.cc +++ b/kernel/mm/virtual_mm/pages.cc @@ -18,6 +18,7 @@ #include <kernel/halt.h> #include <libk/stdio.h> +#include <mm/page_table_allocator.h> #include <mm/physical_mm.h> #include <mm/virtual_mm.h> #include <stdbool.h> @@ -29,6 +30,8 @@ namespace VirtualMM void * alloc_pages(uint32_t n_pages) { + PageTableAllocator::prepare(); + uint32_t starting_address = (uint32_t) find_free_addresses(n_pages); if (!starting_address) return NULL; diff --git a/kernel/mm/virtual_mm/virtual_mm.cc b/kernel/mm/virtual_mm/virtual_mm.cc index 12066f6..fc6bec6 100644 --- a/kernel/mm/virtual_mm/virtual_mm.cc +++ b/kernel/mm/virtual_mm/virtual_mm.cc @@ -16,9 +16,11 @@ * along with this program. If not, see <http://www.gnu.org/licenses/>. */ +#include "common.h" #include <kernel/halt.h> #include <libk/liballoc.h> #include <libk/stdio.h> +#include <mm/page_table_allocator.h> #include <mm/physical_mm.h> #include <mm/virtual_mm.h> #include <stdbool.h> @@ -39,6 +41,12 @@ uint32_t l_fourMiB_page_table[1024] ALIGNED(4096); /* Page table for the next 4 MiB */ uint32_t l_eightMiB_page_table[1024] ALIGNED(4096); +uint32_t * +get_page_directory(void) +{ + return l_current_page_directory; +} + ALWAYS_INLINE void load_page_directory(uint32_t *page_directory) { @@ -102,18 +110,7 @@ initialize(void) uint32_t * make_table(uint32_t *pd_entry) { - uint32_t *table = 0; - if (!LibAlloc::initialized()) { - /* If we don't have a dynamic memory allocator yet (this will happen only - * once, when we initialize the dynamic allocator), then we hard code the - * next page table to be at 7MiB */ - table = (uint32_t *) (7 * MiB); - printk("virtual_mm", - "Using our hard coded table; this should happen only once."); - } else - /* TODO: Uncomment this */ - // table = (uint32_t *) LibAlloc::kmalloc(sizeof(uint32_t) * 1024); - ; + uint32_t *table = PageTableAllocator::allocate(); for (uint32_t i = 0; i < 1024; i++) table[i] = 0x0; |