The manual
ginnOS
The reference: libc, the libraries and the kernel
Loading...
Searching...
No Matches
pmm.h
Go to the documentation of this file.
1#pragma once
2
3/**
4 * physical memory manager: a bitmap of 4 KiB page frames.
5 *
6 * every function is safe to call from preemptible kernel code: the bitmap
7 * and counters are guarded by a spinlock that masks interrupts, so a
8 * process switch can never land in the middle of an update.
9 */
10
11#include "common/stdint.h"
13#include "kernel/memory/phys.h"
14
15/**
16 * initialize the physical memory manager.
17 * must be called after pmm_layout_init() and all memory reservations.
18 * walks the E820 map and marks usable, non-reserved pages as free.
19 * @param boot pointer to boot info containing the E820 memory map.
20 */
21void pmm_init(boot_info_t *boot);
22
23/**
24 * allocate a single 4 KiB physical page frame.
25 * @return direct-map address of the page (one reference), or NULL on
26 * failure.
27 */
28void *pmm_alloc_page(void);
29
30/**
31 * allocate a run of physically contiguous 4 KiB page frames.
32 * pages are freed individually with pmm_free_page().
33 * @param count number of pages (must be > 0).
34 * @return a kernel pointer (direct map) to the first page, or NULL if no
35 * free run of that length exists.
36 */
38
39/**
40 * drop one reference to a page frame. the frame becomes free when its last
41 * reference goes. a frame from an allocation has one reference.
42 * @param address direct-map address of the page (must be page-aligned).
43 */
44void pmm_free_page(void *address);
45
46/**
47 * add a reference to an allocated page frame, so that it stays allocated
48 * until pmm_free_page has been called once more. used to share a frame
49 * between address spaces (copy-on-write).
50 * @param address direct-map address of the page (must be page-aligned).
51 */
52void pmm_page_ref(void *address);
53
54/**
55 * read the reference count of a page frame.
56 * @param address direct-map address of the page (must be page-aligned).
57 * @return the number of references, 0 for a free or reserved frame.
58 */
59uint32_t pmm_page_refcount(void *address);
60
61/**
62 * return the number of free page frames available for allocation.
63 * @returns the number of free page frames.
64 */
66
67/**
68 * return the total number of page frames managed by the PMM.
69 * @returns the total number of page frames.
70 */
72
73/**
74 * check whether a physical address falls within the RAM the PMM manages.
75 * device memory (e.g. a linear framebuffer mapped high) is not owned by the
76 * PMM, so it must not be handed to pmm_free_page. address-space teardown uses
77 * this to skip freeing such mappings.
78 * @param phys the physical address to test.
79 * @return true if the address is a PMM-managed frame, false otherwise.
80 */
81bool pmm_owns(phys_addr_t phys);
82
83/**
84 * mark all page frames within a physical address range as used.
85 * used by subsystems that claim memory after pmm_init() (e.g., kernel heap).
86 * @param start first byte of the region (inclusive, page-aligned).
87 * @param end first byte past the region (exclusive, page-aligned).
88 */
90
91/**
92 * check whether a specific page frame is free. the answer can be stale as
93 * soon as it returns; to take pages, use pmm_claim_pages().
94 * @param page_index index of the page frame to check.
95 * @return true if the page is free, false if used or out of range.
96 */
97bool pmm_is_page_free(uint32_t page_index);
98
99/**
100 * claim a run of specific page frames, only if every one of them is free.
101 * the check and the claim are one atomic step.
102 * @param first_page index of the first page frame.
103 * @param count number of page frames.
104 * @return true if all pages were free and are now used; false (nothing
105 * claimed) otherwise.
106 */
107bool pmm_claim_pages(uint32_t first_page, uint32_t count);
unsigned int uint32_t
Definition fb.h:17
uintptr_t phys_addr_t
physical addresses and the conversion between physical and virtual.
Definition phys.h:18
void * pmm_alloc_page(void)
allocate a single 4 KiB physical page frame.
Definition pmm.c:135
void pmm_mark_region_used(phys_addr_t start, phys_addr_t end)
mark all page frames within a physical address range as used.
Definition pmm.c:286
uint32_t pmm_total_count(void)
return the total number of page frames managed by the PMM.
Definition pmm.c:276
bool pmm_claim_pages(uint32_t first_page, uint32_t count)
claim a run of specific page frames, only if every one of them is free.
Definition pmm.c:314
void pmm_free_page(void *address)
drop one reference to a page frame.
Definition pmm.c:228
void pmm_init(boot_info_t *boot)
physical memory manager: a bitmap of 4 KiB page frames.
Definition pmm.c:61
bool pmm_is_page_free(uint32_t page_index)
check whether a specific page frame is free.
Definition pmm.c:304
uint32_t pmm_page_refcount(void *address)
read the reference count of a page frame.
Definition pmm.c:262
void pmm_page_ref(void *address)
add a reference to an allocated page frame, so that it stays allocated until pmm_free_page has been c...
Definition pmm.c:248
bool pmm_owns(phys_addr_t phys)
check whether a physical address falls within the RAM the PMM manages.
Definition pmm.c:281
void * pmm_alloc_contiguous(uint32_t count)
allocate a run of physically contiguous 4 KiB page frames.
Definition pmm.c:160
uint32_t pmm_free_count(void)
return the number of free page frames available for allocation.
Definition pmm.c:271
information the bootloader hands to the kernel.
Definition boot_info.h:21