PatchworkOS  c9fea19
A non-POSIX operating system.
Loading...
Searching...
No Matches
pmm.h
Go to the documentation of this file.
1#pragma once
2
3#include <boot/boot_info.h>
4
5/**
6 * @brief Physical Memory Manager (PMM).
7 * @defgroup kernel_mem_pmm PMM
8 * @ingroup kernel_mem
9 *
10 * The Physical Memory Manager (PMM) is responsible for managing physical memory pages. It uses a free stack for
11 * allocating single pages in constant-time, and for more specialized allocations (requiring a specific address range
12 * or alignment) a bitmap allocator is used. The bitmap allocator should only be used when no other option is available.
13 *
14 * All physical memory is identity mapped to the beginning of the higher half of the address space. This means that for
15 * example `NULL` is always an invalid address and that the PMM returns addresses in the higher half.
16 *
17 * The free stack provides some advantages over for instance a free list, mainly due to cache improvements.
18 *
19 * @{
20 */
21
22/**
23 * @brief Initializes the Physical Memory Manager.
24 */
25void pmm_init(void);
26
27/**
28 * @brief Allocates a single physical page.
29 *
30 * The returned page will not be zeroed.
31 *
32 * @return On success, returns the higher half physical address of the allocated page. On failure, returns `NULL`.
33 */
34void* pmm_alloc(void);
35
36/**
37 * @brief Allocates multiple physical pages.
38 *
39 * The `pmm_alloc_pages` function allocates `count` non-contiguous physical pages from the free stack, by using this
40 * function its possible to avoid holding the lock for each page allocation, improving performance when allocating many
41 * pages at once.
42 *
43 * The returned pages will not be zeroed.
44 *
45 * @param addresses An array where the higher half physical addresses of the allocated pages will be stored.
46 * @param count The number of pages to allocate.
47 * @return On success, `0`. On failure, `ERR` and `errno` is set.
48 */
49uint64_t pmm_alloc_pages(void** addresses, uint64_t count);
50
51/**
52 * @brief Allocates a contiguous region of physical pages managed by the bitmap.
53 *
54 * The `pmm_alloc_bitmap` function allocates a contiguous block of `count` physical pages from the memory region
55 * managed by the bitmap. It also enforces a maximum address and alignment for the allocation.
56 *
57 * The returned pages will not be zeroed.
58 *
59 * @param count The number of contiguous pages to allocate.
60 * @param maxAddr The maximum physical address (exclusive) for the allocation.
61 * @param alignment The required alignment for the allocated region, in bytes.
62 * @return On success, returns the higher half physical address of the allocated region. On failure, returns `NULL`.
63 */
64void* pmm_alloc_bitmap(uint64_t count, uintptr_t maxAddr, uint64_t alignment);
65
66/**
67 * @brief Frees a single physical page.
68 *
69 * The `pmm_free` function frees a page returning ownership of it to the PMM. The PMM will determine based on the
70 * address if it's owned by the bitmap or the free stack.
71 *
72 * @param address The higher half physical address of the page to free.
73 */
74void pmm_free(void* address);
75
76/**
77 * @brief Frees multiple physical pages.
78 *
79 * The `pmm_free_pages` function frees `count` physical pages returning ownership of them to the PMM. The PMM will
80 * determine based on the addresses if they're owned by the bitmap or the free stack.
81 *
82 * @param addresses An array containing the higher half physical addresses of the pages to free.
83 * @param count The number of pages to free.
84 */
85void pmm_free_pages(void** addresses, uint64_t count);
86
87/**
88 * @brief Frees a contiguous region of physical pages.
89 *
90 * The `pmm_free_region` function frees a contiguous block of `count` physical pages, returning ownership of them to the
91 * PMM. The PMM will determine based on the address if it's owned by the bitmap or the free stack.
92 *
93 * @param address The higher half physical address of the first page in the region to free.
94 * @param count The number of pages to free.
95 */
97
98/**
99 * @brief Retrieves the total amount of physical memory managed by the PMM.
100 *
101 * @return The total amount of physical memory in pages.
102 */
104
105/**
106 * @brief Retrieves the amount of free physical memory.
107 *
108 * @return The amount of currently free physical memory in pages.
109 */
111
112/**
113 * @brief Retrieves the amount of reserved physical memory.
114 *
115 * Reserved memory includes memory that is not available for allocation (e.g., kernel code, hardware regions).
116 *
117 * @return The amount of reserved physical memory in pages.
118 */
120
121/** @} */
uint64_t pmm_alloc_pages(void **addresses, uint64_t count)
Allocates multiple physical pages.
Definition pmm.c:184
void pmm_free(void *address)
Frees a single physical page.
Definition pmm.c:220
void pmm_free_pages(void **addresses, uint64_t count)
Frees multiple physical pages.
Definition pmm.c:226
void pmm_free_region(void *address, uint64_t count)
Frees a contiguous region of physical pages.
Definition pmm.c:235
uint64_t pmm_free_amount(void)
Retrieves the amount of free physical memory.
Definition pmm.c:247
void * pmm_alloc_bitmap(uint64_t count, uintptr_t maxAddr, uint64_t alignment)
Allocates a contiguous region of physical pages managed by the bitmap.
Definition pmm.c:207
void * pmm_alloc(void)
Allocates a single physical page.
Definition pmm.c:171
uint64_t pmm_used_amount(void)
Retrieves the amount of reserved physical memory.
Definition pmm.c:253
uint64_t pmm_total_amount(void)
Retrieves the total amount of physical memory managed by the PMM.
Definition pmm.c:241
void pmm_init(void)
Initializes the Physical Memory Manager.
Definition pmm.c:158
static uintptr_t address
Mapped virtual address of the HPET registers.
Definition hpet.c:95
static atomic_long count
Definition main.c:10
__UINT64_TYPE__ uint64_t
Definition stdint.h:17
__UINTPTR_TYPE__ uintptr_t
Definition stdint.h:43