aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRaghuram Subramani <raghus2247@gmail.com>2025-01-05 23:45:54 -0500
committerRaghuram Subramani <raghus2247@gmail.com>2025-01-05 23:45:54 -0500
commitcb5ab0723319e6b2a56cb2e1a1c1ead1f1a82c22 (patch)
tree4ae7bba6dadc7c9bc664252a470d7de00a1891d4
parent712095095f3141f709273730808a682591552735 (diff)
kernel: mm: physical_mm: Implement bitmap memory management
-rw-r--r--kernel/include/mm/physical_mm.h5
-rw-r--r--kernel/mm/physical_mm/physical_mm.c47
2 files changed, 52 insertions, 0 deletions
diff --git a/kernel/include/mm/physical_mm.h b/kernel/include/mm/physical_mm.h
index 503a76f..461016c 100644
--- a/kernel/include/mm/physical_mm.h
+++ b/kernel/include/mm/physical_mm.h
@@ -19,6 +19,8 @@
#ifndef __mm_physical_mm_h
#define __mm_physical_mm_h
+#include <stdint.h>
+
#include <common.h>
/*
@@ -32,6 +34,9 @@
#define BLOCK_ALIGN BLOCK_SIZE
+#define BITMAP_ENTRY_SIZE 32
+
void physical_mm_init(void);
+uint32_t mmap_find_first_free(void);
#endif
diff --git a/kernel/mm/physical_mm/physical_mm.c b/kernel/mm/physical_mm/physical_mm.c
index ed7a9ac..dca04a5 100644
--- a/kernel/mm/physical_mm/physical_mm.c
+++ b/kernel/mm/physical_mm/physical_mm.c
@@ -2,6 +2,7 @@
* https://web.archive.org/web/20190316115948/http://www.brokenthorn.com/Resources/OSDev17.html
*/
+#include <stdbool.h>
#include <stdint.h>
#include <libk/stdio.h>
@@ -12,6 +13,9 @@
extern uint32_t kernel_start;
extern uint32_t kernel_end;
+uint32_t block_count;
+uint32_t *memory_map;
+
void
physical_mm_init(void)
{
@@ -25,3 +29,46 @@ physical_mm_init(void)
printk("physical_mm", "Kernel starts at: 0x%x", &kernel_start);
printk("physical_mm", "Kernel ends at: 0x%x", &kernel_end);
}
+
+ALWAYS_INLINE void
+physical_mm_set(const uint32_t bit)
+{
+ uint32_t memory_map_index = bit / BITMAP_ENTRY_SIZE;
+ uint32_t bitmask = 1 << (bit % BITMAP_ENTRY_SIZE);
+ memory_map[memory_map_index] |= bitmask;
+}
+
+ALWAYS_INLINE void
+physical_mm_unset(const uint32_t bit)
+{
+ uint32_t memory_map_index = bit / BITMAP_ENTRY_SIZE;
+ uint32_t bitmask = 1 << (bit % BITMAP_ENTRY_SIZE);
+ memory_map[memory_map_index] &= ~bitmask;
+}
+
+/* Returns:
+ * True if the bit is unset (block isn't in use)
+ * False if the bit is used (block is in use)
+ */
+ALWAYS_INLINE bool
+physical_mm_test_bit(const uint32_t bit)
+{
+ uint32_t memory_map_index = bit / BITMAP_ENTRY_SIZE;
+ uint32_t bitmask = 1 << (bit % BITMAP_ENTRY_SIZE);
+ return memory_map[memory_map_index] & bitmask;
+}
+
+uint32_t
+physical_mm_find_first_free(void)
+{
+ for (uint32_t i = 0; i < block_count / BITMAP_ENTRY_SIZE; i++)
+ /* All blocks in the entry aren't in use */
+ if (memory_map[i] != 0xffffffff)
+ /* Test each bit to see if it's zero */
+ for (uint32_t j = 0; j < BITMAP_ENTRY_SIZE; j++)
+ if (physical_mm_test_bit(i * BITMAP_ENTRY_SIZE + j))
+ return i * BITMAP_ENTRY_SIZE + j;
+
+ /* TODO: Assert not reached, or something similar */
+ return -1;
+}