aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRaghuram Subramani <raghus2247@gmail.com>2025-01-22 05:23:37 -0500
committerRaghuram Subramani <raghus2247@gmail.com>2025-01-22 05:23:37 -0500
commit349911f3af71f7974de583a86d2133b87aa16ea7 (patch)
tree713da711a4e861b892b88752056d9f8ceeb8ad9b
parent7a594b8bd49ee3c9809a4dda661f1ab85ad84ee6 (diff)
physical_mm: Spinlock the memory bitmap
-rw-r--r--kernel/CMakeLists.txt1
-rw-r--r--kernel/mm/physical_mm/physical_mm.c13
-rw-r--r--kernel/mm/virtual_mm/virtual_mm.c3
3 files changed, 16 insertions, 1 deletions
diff --git a/kernel/CMakeLists.txt b/kernel/CMakeLists.txt
index 7aa0889..059600d 100644
--- a/kernel/CMakeLists.txt
+++ b/kernel/CMakeLists.txt
@@ -41,6 +41,7 @@ set(C_COMPILE_OPTIONS
-pedantic
# -Werror
+ # TODO: This doesn't actually work right now because of the linker script :')
-g
)
diff --git a/kernel/mm/physical_mm/physical_mm.c b/kernel/mm/physical_mm/physical_mm.c
index 895a3fd..5b405e6 100644
--- a/kernel/mm/physical_mm/physical_mm.c
+++ b/kernel/mm/physical_mm/physical_mm.c
@@ -22,6 +22,7 @@
/* TODO: Stack based allocation? */
+#include <stdatomic.h>
#include <stdbool.h>
#include <stdint.h>
@@ -31,6 +32,7 @@
#include <mm/physical_mm.h>
#include <kernel/halt.h>
+#include <kernel/spinlock.h>
extern uint32_t kernel_start;
extern uint32_t kernel_end;
@@ -39,6 +41,8 @@ uint32_t block_count = 0;
uint32_t total_free_blocks = 0;
uint32_t memory_map[MAX_BLOCKS / BITMAP_ENTRY_SIZE];
+atomic_flag memory_map_lock;
+
ALWAYS_INLINE static void
physical_mm_log_memory_map(free_memory_regions_t *free_memory_regions)
{
@@ -93,6 +97,8 @@ physical_mm_init(void)
free_memory_regions_t *free_memory_regions = memory_map_get_free_regions();
physical_mm_log_memory_map(free_memory_regions);
+ spinlock_acquire(&memory_map_lock);
+
/* All blocks are initially used */
/* TODO: Move this block to a place after block_count is set. This is why
* using block_count instead of MAX_BLOCKS wasn't working. */
@@ -109,6 +115,8 @@ physical_mm_init(void)
uint32_t kernel_size = ((uint32_t) &kernel_end) - ((uint32_t) &kernel_start);
physical_mm_deinitialize_region((uint32_t) &kernel_start, kernel_size);
+ spinlock_release(&memory_map_lock);
+
total_free_memory -= kernel_size;
block_count = total_free_memory / BLOCK_SIZE;
printk("\nphysical_mm", "Total blocks: 0x%x", block_count);
@@ -157,10 +165,13 @@ physical_mm_allocate_block(void)
return NULL;
}
- uint32_t block = physical_mm_find_first_free_block();
+ spinlock_acquire(&memory_map_lock);
+ uint32_t block = physical_mm_find_first_free_block();
physical_mm_set_used(block, &total_free_blocks, memory_map);
+ spinlock_release(&memory_map_lock);
+
uint32_t physical_address = block * BLOCK_SIZE;
return (void *) physical_address;
}
diff --git a/kernel/mm/virtual_mm/virtual_mm.c b/kernel/mm/virtual_mm/virtual_mm.c
index c69d194..9c239a1 100644
--- a/kernel/mm/virtual_mm/virtual_mm.c
+++ b/kernel/mm/virtual_mm/virtual_mm.c
@@ -107,6 +107,9 @@ virtual_mm_map_page(void *physical_address, void *virtual_address)
table = (uint32_t *) PDE_GET_TABLE(pd_entry);
uint32_t *pt_entry = &table[GET_PTE_FRAME(virtual_address)];
+ if (PTE_IS_PRESENT(pt_entry))
+ ASSERT_NOT_REACHED(); /* TODO: Mapping previously mapped memory */
+
*pt_entry = PTE_FRAME((uint32_t) physical_address) | PTE_PRESENT(1)
| PTE_WRITABLE(1);
}