Memory Management

To use the functions described in this page, the file <hal.h> should be included.

JSLK supports memory management by the use of a virtual memory manager and a heap. There is currently no physical memory manager available, although one will be implemented before the first release. The following functions can be used to interface with the memory manager throughout the kernel:

kmalloc()
kmalloc_a()
kmalloc_p()
kmalloc_ap()

uint32_t kmalloc(uint32_t sz)

Used to allocate a block of memory.

Arguments

uint32_t sz: The size of the block to allocate.

Return value

The virtual memory address of the allocated block is returned as an uint32_t.

uint32_t kmalloc_a(uint32_t sz)

Used to allocate a page-aligned block of memory.

Arguments

uint32_t sz: The size of the block to allocate.

Return value

The virtual memory address of the allocated block is returned as an uint32_t.

uint32_t kmalloc_p(uint32_t sz, uint32_t *phys)

Used to allocate a block of memory when the physical address of the block is required.

Arguments

uint32_t sz: The size of the block to allocate.

uint32_t *phys: A pointer to the variable where the physical address of the memory block should be stored.

Return value

The virtual memory address of the allocated block is returned as an uint32_t. The physical memory address of the block is returned to the variable passed to the function as phys.

uint32_t kmalloc_ap(uint32_t sz, uint32_t *phys)

Used to allocate a page-aligned block of memory when the physical address of the block is required.

Arguments

uint32_t sz: The size of the block to allocate.

uint32_t *phys: A pointer to the variable where the physical address of the memory block should be stored.

Return value

The virtual memory address of the allocated block is returned as an uint32_t. The physical memory address of the block is returned to the variable passed to the function as phys.

x86

Currently, only the first 16 MB of available memory are used by the kernel. To manage memory on the x86, a GDT is set-up as a security measure to protect memory until paging is enabled. After paging is enabled, Once the virtual memory manager functions become available, and later in the startup when the heap is initialized, the heap integrates with the VMM and overrides its functions. The structures for the page directories, tables and pages can be found in paging.h, the paging code in paging.c.