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.
Loading...
Searching...
No Matches
Data Structures | Macros | Typedefs | Functions | Variables
page_allocator.c File Reference

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>
Include dependency graph for page_allocator.c:

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_tfind_chunk_for_address (phy_addr addr)
 Find the chunk that contains a given physical address.
 
static memory_chunk_tfind_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.
 

Detailed Description

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.

Author
Abhin Parekadan Jose
Date
2026-05-16

Function Documentation

◆ check_kernel_binary_overlap()

static bool check_kernel_binary_overlap ( phy_addr  mem_start,
size_t  mem_size 
)
static

Determine whether a region overlaps the kernel image.

Parameters
mem_startPhysical base address of the region to inspect.
mem_sizeSize of the region in bytes.
Returns
true when the region overlaps the linked kernel image, false otherwise.

◆ find_chunk_for_address()

static memory_chunk_t * find_chunk_for_address ( phy_addr  addr)
static

Find the chunk that contains a given physical address.

Parameters
addrPhysical address to locate.
Returns
Pointer to the owning chunk, or NULL if none matches.

◆ find_chunk_with_free_pages()

static memory_chunk_t * find_chunk_with_free_pages ( size_t  num_pages)
static

Find a chunk with a contiguous free range of the requested size.

Parameters
num_pagesMinimum contiguous free page count required.
Returns
Pointer to a suitable chunk, or NULL if none was found.

◆ page_alloc()

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.

Parameters
num_pagesThe number of contiguous 4 KiB pages requested.
Returns
phy_addr start address of the allocated block, or PHYS_ADDR_NULL if insufficient contiguous memory exists.

◆ page_alloc_chunk()

static phy_addr page_alloc_chunk ( size_t  num_pages,
memory_chunk_t chunk 
)
static

Allocate a contiguous range of pages from a single chunk.

Parameters
num_pagesNumber of contiguous pages to allocate.
chunkChunk to search for free pages.
Returns
The physical base address of the allocation, or PHYS_ADDR_NULL if none was found.

◆ page_allocator_add_region()

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.

Parameters
mem_startThe physical address of the beginning of the region.
mem_sizeTotal size of the region, in bytes.
check_kernel_overlapWhether to reject regions that overlap with the kernel image.
Returns
true if the region was added successfully, false otherwise.

◆ page_allocator_remove_region()

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.

Parameters
mem_startThe physical base address of the region to remove.
Returns
true if the region was found and removed, false otherwise.

◆ page_dump_status()

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.

◆ page_dump_status_chunk()

static void page_dump_status_chunk ( memory_chunk_t chunk)
static

Dump the allocation state for one chunk.

Parameters
chunkChunk whose page state should be printed.

◆ page_free()

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.

Parameters
startThe physical address of the first page to free.
num_pagesThe number of contiguous pages to release.

◆ page_free_chunk()

static void page_free_chunk ( phy_addr  start,
size_t  num_pages,
memory_chunk_t chunk 
)
static

Mark a previously allocated range of pages as free.

Parameters
startPhysical base address of the range to free.
num_pagesNumber of contiguous pages to free.
chunkChunk that owns the range.

◆ page_init_chunk()

static bool page_init_chunk ( phy_addr  mem_start,
size_t  mem_size,
memory_chunk_t chunk,
bool  check_kernel_overlap 
)
static

Initialize one memory chunk from a physical-memory region.

Parameters
mem_startPhysical base address of the region.
mem_sizeSize of the region in bytes.
chunkChunk state to populate.
check_kernel_overlapWhether to reject regions overlapping the kernel image.
Returns
true when the chunk was initialized successfully, false otherwise.

◆ reserve_page()

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.

Parameters
startThe physical address of the first page to reserve.
num_pagesThe number of contiguous pages to reserve.
Returns
bool True if the pages were successfully reserved, false otherwise.

◆ reserve_page_chunk()

static bool reserve_page_chunk ( phy_addr  start,
size_t  num_pages,
memory_chunk_t chunk 
)
static

Mark a range of pages as reserved inside a chunk.

Parameters
startPhysical base address of the range to reserve.
num_pagesNumber of contiguous pages to reserve.
chunkDestination chunk that owns the range.
Returns
true when the reservation succeeded, false otherwise.