aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRaghuram Subramani <raghus2247@gmail.com>2025-01-05 13:04:24 -0500
committerRaghuram Subramani <raghus2247@gmail.com>2025-01-05 13:16:35 -0500
commitbb036c5db61937697d0f34fc24b65e1259bc6bb0 (patch)
tree7a415f4d248251de16daf02e786a1337238bf6a4
parent2ee883a8ffc1dae0ac74a1825465e0929a9f6b3e (diff)
kernel: mm: Introduce a WIP physical_mm
-rw-r--r--kernel/CMakeLists.txt1
-rw-r--r--kernel/boot/linker.ld3
-rw-r--r--kernel/include/mm/physical_mm.h37
-rw-r--r--kernel/kernel/kernel.c11
-rw-r--r--kernel/mm/physical_mm/physical_mm.c27
5 files changed, 71 insertions, 8 deletions
diff --git a/kernel/CMakeLists.txt b/kernel/CMakeLists.txt
index 733d3c0..73eea0c 100644
--- a/kernel/CMakeLists.txt
+++ b/kernel/CMakeLists.txt
@@ -12,6 +12,7 @@ set(SRC
kernel/io/io.c
mm/memory_map.c
+ mm/physical_mm/physical_mm.c
drivers/vga_text_buffer/vga_text_buffer.c
drivers/serial/serial.c
diff --git a/kernel/boot/linker.ld b/kernel/boot/linker.ld
index c90ef51..d98714f 100644
--- a/kernel/boot/linker.ld
+++ b/kernel/boot/linker.ld
@@ -36,6 +36,7 @@ SECTIONS
work around this issue. This does not use that feature, so 2M was
chosen as a safer option than the traditional 1M. */
. = 2M;
+ kernel_start = .;
/* First put the multiboot header, as it is required to be put very early
in the image or the bootloader won't recognize the file format.
@@ -68,3 +69,5 @@ SECTIONS
/* The compiler may produce other sections, by default it will put them in
a segment with the same name. Simply add stuff here as needed. */
}
+
+kernel_end = .;
diff --git a/kernel/include/mm/physical_mm.h b/kernel/include/mm/physical_mm.h
new file mode 100644
index 0000000..503a76f
--- /dev/null
+++ b/kernel/include/mm/physical_mm.h
@@ -0,0 +1,37 @@
+/*
+ * CMOS
+ * 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_physical_mm_h
+#define __mm_physical_mm_h
+
+#include <common.h>
+
+/*
+ * A byte has 8 bits, hence we can store information for up to 8 blocks at a
+ * time
+ */
+#define BLOCKS_PER_BYTE 8
+
+/* TODO: Update this to 2MiB when PAE is enabled */
+#define BLOCK_SIZE 4 * KiB
+
+#define BLOCK_ALIGN BLOCK_SIZE
+
+void physical_mm_init(void);
+
+#endif
diff --git a/kernel/kernel/kernel.c b/kernel/kernel/kernel.c
index 2e4470b..d1cc6f1 100644
--- a/kernel/kernel/kernel.c
+++ b/kernel/kernel/kernel.c
@@ -20,6 +20,7 @@
#include <mm/memory_map.h>
#include <mm/multiboot.h>
+#include <mm/physical_mm.h>
#include <kernel/halt.h>
@@ -41,15 +42,9 @@ kernel_main(uint32_t magic, multiboot_info_t *multiboot_info)
GDT_load();
memory_map_load(multiboot_info);
+ physical_mm_init();
- printk("Kernel", "Started.");
-
- free_memory_regions_t *free_memory_regions = memory_map_get_free_regions();
- for (int i = 0; i < free_memory_regions->n_regions; i++)
- printk("Kernel",
- "start: 0x%.08x | length: 0x%.08x",
- free_memory_regions->region_list[i]->addr_low,
- free_memory_regions->region_list[i]->len_low);
+ printk("\nKernel", "Started.");
exit();
halt(); /* If exit() fails (on real hardware) */
diff --git a/kernel/mm/physical_mm/physical_mm.c b/kernel/mm/physical_mm/physical_mm.c
new file mode 100644
index 0000000..ed7a9ac
--- /dev/null
+++ b/kernel/mm/physical_mm/physical_mm.c
@@ -0,0 +1,27 @@
+/* Bitmap Based Allocation:
+ * https://web.archive.org/web/20190316115948/http://www.brokenthorn.com/Resources/OSDev17.html
+ */
+
+#include <stdint.h>
+
+#include <libk/stdio.h>
+
+#include <mm/memory_map.h>
+#include <mm/physical_mm.h>
+
+extern uint32_t kernel_start;
+extern uint32_t kernel_end;
+
+void
+physical_mm_init(void)
+{
+ free_memory_regions_t *free_memory_regions = memory_map_get_free_regions();
+ for (int i = 0; i < free_memory_regions->n_regions; i++)
+ printk("Kernel",
+ "start: 0x%.08x | length: 0x%.08x",
+ free_memory_regions->region_list[i]->addr_low,
+ free_memory_regions->region_list[i]->len_low);
+
+ printk("physical_mm", "Kernel starts at: 0x%x", &kernel_start);
+ printk("physical_mm", "Kernel ends at: 0x%x", &kernel_end);
+}