aboutsummaryrefslogtreecommitdiff
path: root/kernel/mm/virtual_mm
diff options
context:
space:
mode:
authorRaghuram Subramani <raghus2247@gmail.com>2025-02-04 15:17:55 +0530
committerRaghuram Subramani <raghus2247@gmail.com>2025-02-04 15:19:00 +0530
commit869d9bcea81b2ad439cd4498eabc1136e945d2ce (patch)
tree8dac300410569fdeadb7d18677b4626e82db3efc /kernel/mm/virtual_mm
parent1ac479c99f2d4548285893e76df4e0ffe9ce42cc (diff)
mm: virtual_mm: Write a basic page table allocator
Diffstat (limited to 'kernel/mm/virtual_mm')
-rw-r--r--kernel/mm/virtual_mm/page_table_allocator.cc74
-rw-r--r--kernel/mm/virtual_mm/pages.cc3
-rw-r--r--kernel/mm/virtual_mm/virtual_mm.cc21
3 files changed, 86 insertions, 12 deletions
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;