|
Kernelite 0.1.0
Kernelite is a minimalist, educational operating system kernel built from scratch. The project aims to recreate core components of the Linux architecture to explore the fundamentals of operating system design and hardware-software interaction.
|
Physical page allocator implementation using a bitmap. More...
#include "allocator/page_allocator.h"#include "linker/linker_defines.h"#include "linker/symbols.h"#include "utils/kprintf.h"#include <stdbool.h>#include <stddef.h>#include <stdint.h>
Data Structures | |
| struct | memory_chunk_t |
| State for one managed physical-memory chunk. More... | |
Macros | |
| #define | MAX_MEM_CHUNKS 10 |
| Maximum number of memory chunks to manage. | |
Typedefs | |
| typedef struct memory_chunk_t | memory_chunk_t |
| State for one managed physical-memory chunk. | |
Functions | |
| static bool | check_kernel_binary_overlap (phy_addr mem_start, size_t mem_size) |
| Determine whether a region overlaps the kernel image. | |
| static bool | reserve_page_chunk (phy_addr start, size_t num_pages, memory_chunk_t *chunk) |
| Mark a range of pages as reserved inside a chunk. | |
| static bool | page_init_chunk (phy_addr mem_start, size_t mem_size, memory_chunk_t *chunk, bool check_kernel_overlap) |
| Initialize one memory chunk from a physical-memory region. | |
| static phy_addr | page_alloc_chunk (size_t num_pages, memory_chunk_t *chunk) |
| Allocate a contiguous range of pages from a single chunk. | |
| static void | page_free_chunk (phy_addr start, size_t num_pages, memory_chunk_t *chunk) |
| Mark a previously allocated range of pages as free. | |
| static void | page_dump_status_chunk (memory_chunk_t *chunk) |
| Dump the allocation state for one chunk. | |
| static memory_chunk_t * | find_chunk_for_address (phy_addr addr) |
| Find the chunk that contains a given physical address. | |
| static memory_chunk_t * | find_chunk_with_free_pages (size_t num_pages) |
| Find a chunk with a contiguous free range of the requested size. | |
| bool | page_allocator_add_region (phy_addr mem_start, size_t mem_size, bool check_kernel_overlap) |
| Registers a new physical memory region with the allocator. | |
| bool | page_allocator_remove_region (phy_addr mem_start) |
| Removes a previously registered memory region. | |
| void | fixup_page_allocator (void) |
| Rewrites allocator bitmap pointers after the switch to high virtual addresses. | |
| bool | reserve_page (phy_addr start, size_t num_pages) |
| Reserves a specific number of contiguous pages. | |
| phy_addr | page_alloc (size_t num_pages) |
| Allocates a contiguous block of physical pages. | |
| void | page_free (phy_addr start, size_t num_pages) |
| Frees a previously allocated block of physical pages. | |
| void | page_dump_status (void) |
| Scans the bitmap and prints the status of all memory regions. | |
Variables | |
| static memory_chunk_t | mem_chunks [MAX_MEM_CHUNKS] |
| Array of memory chunks managed by the allocator. | |
Physical page allocator implementation using a bitmap.
This module implements a simple physical page allocator backed by a linker-reserved bitmap region. It provides initialization, reservation, allocation, freeing and status dump helpers used by the kernel early memory management.
|
static |
Determine whether a region overlaps the kernel image.
| mem_start | Physical base address of the region to inspect. |
| mem_size | Size of the region in bytes. |
|
static |
Find the chunk that contains a given physical address.
| addr | Physical address to locate. |
|
static |
Find a chunk with a contiguous free range of the requested size.
| num_pages | Minimum contiguous free page count required. |
| phy_addr page_alloc | ( | size_t | num_pages | ) |
Allocates a contiguous block of physical pages.
Searches for a free span of memory large enough to hold num_pages.
| num_pages | The number of contiguous 4 KiB pages requested. |
|
static |
Allocate a contiguous range of pages from a single chunk.
| num_pages | Number of contiguous pages to allocate. |
| chunk | Chunk to search for free pages. |
| bool page_allocator_add_region | ( | phy_addr | mem_start, |
| size_t | mem_size, | ||
| bool | check_kernel_overlap | ||
| ) |
Registers a new physical memory region with the allocator.
Initializes a new memory chunk for the range starting at mem_start and spanning mem_size bytes. If check_kernel_overlap is true, the region is rejected when it overlaps the kernel image.
| mem_start | The physical address of the beginning of the region. |
| mem_size | Total size of the region, in bytes. |
| check_kernel_overlap | Whether to reject regions that overlap with the kernel image. |
| bool page_allocator_remove_region | ( | phy_addr | mem_start | ) |
Removes a previously registered memory region.
Marks the region that starts at mem_start as no longer managed by the allocator.
| mem_start | The physical base address of the region to remove. |
| void page_dump_status | ( | void | ) |
Scans the bitmap and prints the status of all memory regions.
Iterates through the managed page pool and groups contiguous pages with the same status (FREE or USED) into blocks for concise UART output.
|
static |
Dump the allocation state for one chunk.
| chunk | Chunk whose page state should be printed. |
| void page_free | ( | phy_addr | start, |
| size_t | num_pages | ||
| ) |
Frees a previously allocated block of physical pages.
Marks the specified range of pages as available for future allocations.
| start | The physical address of the first page to free. |
| num_pages | The number of contiguous pages to release. |
|
static |
Mark a previously allocated range of pages as free.
| start | Physical base address of the range to free. |
| num_pages | Number of contiguous pages to free. |
| chunk | Chunk that owns the range. |
|
static |
Initialize one memory chunk from a physical-memory region.
| mem_start | Physical base address of the region. |
| mem_size | Size of the region in bytes. |
| chunk | Chunk state to populate. |
| check_kernel_overlap | Whether to reject regions overlapping the kernel image. |
| bool reserve_page | ( | phy_addr | start, |
| size_t | num_pages | ||
| ) |
Reserves a specific number of contiguous pages.
Marks the specified range of pages as reserved.
| start | The physical address of the first page to reserve. |
| num_pages | The number of contiguous pages to reserve. |
|
static |
Mark a range of pages as reserved inside a chunk.
| start | Physical base address of the range to reserve. |
| num_pages | Number of contiguous pages to reserve. |
| chunk | Destination chunk that owns the range. |