diff options
author | Raghuram Subramani <raghus2247@gmail.com> | 2025-01-09 12:24:41 -0500 |
---|---|---|
committer | Raghuram Subramani <raghus2247@gmail.com> | 2025-01-09 12:24:41 -0500 |
commit | c39920c3a3ef0544feef6c325673506c0e3fac88 (patch) | |
tree | b6c160045708755332aba51cfa7fd815a7804a8a | |
parent | 19a69c5f6249c8e2fdadd1217de75f6ba5496838 (diff) |
kernel: mm: physical: Refactor into separate files
-rw-r--r-- | kernel/CMakeLists.txt | 3 | ||||
-rw-r--r-- | kernel/include/mm/physical_mm.h | 9 | ||||
-rw-r--r-- | kernel/mm/physical_mm/allocation.c | 51 | ||||
-rw-r--r-- | kernel/mm/physical_mm/initialization.c (renamed from kernel/mm/physical_mm/physical_mm.c) | 41 |
4 files changed, 59 insertions, 45 deletions
diff --git a/kernel/CMakeLists.txt b/kernel/CMakeLists.txt index 973abb2..6535f04 100644 --- a/kernel/CMakeLists.txt +++ b/kernel/CMakeLists.txt @@ -12,7 +12,8 @@ set(SRC kernel/io/io.c mm/memory_map.c - mm/physical_mm/physical_mm.c + mm/physical_mm/initialization.c + mm/physical_mm/allocation.c mm/physical_mm/memory_map.c drivers/vga_text_buffer/vga_text_buffer.c diff --git a/kernel/include/mm/physical_mm.h b/kernel/include/mm/physical_mm.h index 6befcb0..06039eb 100644 --- a/kernel/include/mm/physical_mm.h +++ b/kernel/include/mm/physical_mm.h @@ -34,10 +34,13 @@ void physical_mm_init(void); -uint32_t physical_mm_find_first_free_block(void); +uint32_t physical_mm_find_first_free_block(uint32_t *memory_map); -void *physical_mm_allocate_block(void); -void physical_mm_free_block(void *physical_address); +void *physical_mm_allocate_block(uint32_t *total_free_blocks, + uint32_t *memory_map); +void physical_mm_free_block(void *physical_address, + uint32_t *total_free_blocks, + uint32_t *memory_map); void physical_mm_set_used(const uint32_t bit, uint32_t *total_free_blocks, diff --git a/kernel/mm/physical_mm/allocation.c b/kernel/mm/physical_mm/allocation.c new file mode 100644 index 0000000..85d945f --- /dev/null +++ b/kernel/mm/physical_mm/allocation.c @@ -0,0 +1,51 @@ +#include <stdbool.h> +#include <stdint.h> + +#include <libk/stdio.h> + +#include <mm/physical_mm.h> + +#include <kernel/halt.h> + +uint32_t +physical_mm_find_first_free_block(uint32_t *memory_map) +{ + /* TODO: Why doesn't using block_count instead of MAX_BLOCKS work? */ + for (uint32_t i = 0; i < MAX_BLOCKS / BITMAP_ENTRY_SIZE; i++) + /* At least one block in the entry isn'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, memory_map)) + 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(uint32_t *total_free_blocks, uint32_t *memory_map) +{ + if (total_free_blocks == 0) { + printk("physical_mm", "No more free blocks!"); + return NULL; + } + + uint32_t block = physical_mm_find_first_free_block(memory_map); + + physical_mm_set_used(block, total_free_blocks, memory_map); + + uint32_t physical_address = block * BLOCK_SIZE; + return (void *) physical_address; +} + +void +physical_mm_free_block(void *physical_address, + uint32_t *total_free_blocks, + uint32_t *memory_map) +{ + uint32_t block = ((uint32_t) physical_address) / BLOCK_SIZE; + physical_mm_set_usable(block, total_free_blocks, memory_map); +} diff --git a/kernel/mm/physical_mm/physical_mm.c b/kernel/mm/physical_mm/initialization.c index 2040fbd..779760c 100644 --- a/kernel/mm/physical_mm/physical_mm.c +++ b/kernel/mm/physical_mm/initialization.c @@ -112,44 +112,3 @@ physical_mm_init(void) free_blcks); #endif } - -uint32_t -physical_mm_find_first_free_block(void) -{ - /* TODO: Why doesn't using block_count instead of MAX_BLOCKS work? */ - for (uint32_t i = 0; i < MAX_BLOCKS / BITMAP_ENTRY_SIZE; i++) - /* At least one block in the entry isn'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, memory_map)) - 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) { - printk("physical_mm", "No more free blocks!"); - return NULL; - } - - uint32_t block = physical_mm_find_first_free_block(); - - physical_mm_set_used(block, &total_free_blocks, memory_map); - - 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, memory_map); -} |