aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRaghuram Subramani <raghus2247@gmail.com>2025-01-08 13:23:04 -0500
committerRaghuram Subramani <raghus2247@gmail.com>2025-01-08 13:23:04 -0500
commit843c0f75b92d2471315f160d2ad62284bec11378 (patch)
tree7b80334d65384bbf046354897dbf471fa447a69d
parent973a7e37feba88f7e0a94372d6e54d9243b32d21 (diff)
kernel: mm: physical_mm: Implement allocation & freeing of blocks
-rw-r--r--kernel/include/mm/physical_mm.h4
-rw-r--r--kernel/mm/physical_mm/physical_mm.c36
2 files changed, 35 insertions, 5 deletions
diff --git a/kernel/include/mm/physical_mm.h b/kernel/include/mm/physical_mm.h
index 8438286..d90495b 100644
--- a/kernel/include/mm/physical_mm.h
+++ b/kernel/include/mm/physical_mm.h
@@ -32,6 +32,10 @@
#define MAX_BLOCKS 1048576
void physical_mm_init(void);
+
uint32_t physical_mm_find_first_free_block(void);
+void *physical_mm_allocate_block(void);
+void physical_mm_free_block(void *physical_address);
+
#endif
diff --git a/kernel/mm/physical_mm/physical_mm.c b/kernel/mm/physical_mm/physical_mm.c
index 5bbe1f7..f971623 100644
--- a/kernel/mm/physical_mm/physical_mm.c
+++ b/kernel/mm/physical_mm/physical_mm.c
@@ -41,7 +41,7 @@ physical_mm_log_memory_map(free_memory_regions_t *free_memory_regions)
/* Marks the block as 'used' */
ALWAYS_INLINE static void
-physical_mm_set_unusable(const uint32_t bit)
+physical_mm_set_used(const uint32_t bit)
{
uint32_t memory_map_index = bit / BITMAP_ENTRY_SIZE;
uint32_t bitmask = 1 << (bit % BITMAP_ENTRY_SIZE);
@@ -69,8 +69,8 @@ physical_mm_initialize_region(uint32_t start, uint32_t length)
total_free_blocks++;
}
- /* First block is always unusable (first 64 KiB) */
- physical_mm_set_unusable(0);
+ /* First block is always used (first 64 KiB) */
+ physical_mm_set_used(0);
}
ALWAYS_INLINE static void
@@ -83,7 +83,7 @@ physical_mm_deinitialize_region(uint32_t start, uint32_t length)
n_blocks++;
for (; n_blocks > 0; n_blocks--) {
- physical_mm_set_unusable(align++);
+ physical_mm_set_used(align++);
total_free_blocks--;
}
}
@@ -94,7 +94,7 @@ physical_mm_init(void)
free_memory_regions_t *free_memory_regions = memory_map_get_free_regions();
physical_mm_log_memory_map(free_memory_regions);
- /* All blocks are initially unusable */
+ /* All blocks are initially used */
for (uint32_t i = 0; i < MAX_BLOCKS; i++)
memory_map[i] = 0xffffffff;
@@ -137,5 +137,31 @@ physical_mm_find_first_free_block(void)
if (physical_mm_test_bit(i * BITMAP_ENTRY_SIZE + j))
return i * BITMAP_ENTRY_SIZE + j;
+ /* Shouldn't be reached, since we're keeping track of the number of free
+ * blocks */
+ ASSERT_NOT_REACHED();
return -1;
}
+
+void *
+physical_mm_allocate_block(void)
+{
+ if (total_free_blocks <= 0)
+ return NULL;
+
+ uint32_t block = physical_mm_find_first_free_block();
+
+ physical_mm_set_used(block);
+ total_free_blocks--;
+
+ uint32_t physical_address = block * BLOCK_SIZE;
+ return (void *) physical_address;
+}
+
+void
+physical_mm_free_block(void *physical_address)
+{
+ uint32_t block = ((uint32_t) physical_address) / BLOCK_SIZE;
+ physical_mm_set_usable(block);
+ total_free_blocks++;
+}