MOS Source Code
Loading...
Searching...
No Matches
pmm.h
Go to the documentation of this file.
1// SPDX-License-Identifier: GPL-3.0-or-later
2
3#pragma once
4
6#include <mos/mos_global.h>
7#include <mos/types.h>
8
23typedef struct phyframe phyframe_t;
24
25// represents a physical frame, there will be one `phyframe_t` for each physical frame in the system
26typedef struct phyframe
27{
29 {
30 PHYFRAME_RESERVED = 0, // intentionally 0 (initially, all frames are reserved)
33 } state;
34
36
37 union
38 {
40 MOS_WARNING_DISABLE("-Wpedantic") // ISO C++ forbids anonymous structs
41
42 struct // free frame
43 {
44 as_linked_list; // for use of freelist in the buddy allocator
45 };
46
47 struct
48 {
49 // for page cache frames
50 bool dirty : 1;
51 } pagecache;
53 };
54
55 union
56 {
57 // number of times this frame is mapped, if this drops to 0, the frame is freed
59 };
61
62MOS_STATIC_ASSERT(sizeof(phyframe_t) == 32, "update phyframe_t struct size");
63
64typedef struct
65{
67 size_t nframes;
69 u32 type; // platform-specific type
71
77
78extern phyframe_t *phyframes; // array of all physical frames
79
80#define phyframe_pfn(frame) ((pfn_t) ((frame) - phyframes))
81#define pfn_phyframe(pfn) (&phyframes[(pfn)])
82
84
88void pmm_dump_lists(void);
89
90void pmm_init();
91
100void pmm_free_frames(phyframe_t *start_frame, size_t n_pages);
101
115pfn_t pmm_reserve_frames(pfn_t pfn, size_t npages);
116#define pmm_reserve_address(paddr) pmm_reserve_frames(ALIGN_DOWN_TO_PAGE(paddr) / MOS_PAGE_SIZE, 1)
117#define pmm_reserve_addresses(paddr, npages) pmm_reserve_frames(ALIGN_DOWN_TO_PAGE(paddr) / MOS_PAGE_SIZE, npages)
118
126
127// ! ref and unref frames
128
129phyframe_t *_pmm_ref_phyframes(phyframe_t *frame, size_t npages);
130void _pmm_unref_phyframes(phyframe_t *frame, size_t npages);
131
133{
134 _pmm_ref_phyframes(pfn_phyframe(pfn_start), npages);
135 return pfn_start;
136}
137
138should_inline void _pmm_unref_pfn_range(pfn_t pfn_start, size_t npages)
139{
140 _pmm_unref_phyframes(pfn_phyframe(pfn_start), npages);
141}
142
143#define pmm_ref(thing, npages) _Generic((thing), phyframe_t *: _pmm_ref_phyframes, pfn_t: _pmm_ref_pfn_range)(thing, npages)
144#define pmm_unref(thing, npages) _Generic((thing), phyframe_t *: _pmm_unref_phyframes, pfn_t: _pmm_unref_pfn_range)(thing, npages)
145
146#define pmm_ref_one(thing) pmm_ref(thing, 1)
147#define pmm_unref_one(thing) pmm_unref(thing, 1)
148
#define pfn_phyframe(pfn)
Definition pmm.h:81
void _pmm_unref_phyframes(phyframe_t *frame, size_t npages)
Definition pmm.c:162
size_t pmm_total_frames
Definition pmm.c:16
phyframe_t * _pmm_ref_phyframes(phyframe_t *frame, size_t npages)
Definition pmm.c:151
size_t pmm_reserved_frames
Definition pmm.h:83
struct phyframe phyframe_t
Definition pmm.h:23
pfn_t pmm_reserve_frames(pfn_t pfn, size_t npages)
Mark a range of physical memory as reserved.
Definition pmm.c:122
void pmm_free_frames(phyframe_t *start_frame, size_t n_pages)
Definition pmm.c:108
pmm_allocation_flags_t
Definition pmm.h:73
phyframe_t * phyframes
Definition pmm.c:15
should_inline pfn_t _pmm_ref_pfn_range(pfn_t pfn_start, size_t npages)
Definition pmm.h:132
should_inline void _pmm_unref_pfn_range(pfn_t pfn_start, size_t npages)
Definition pmm.h:138
size_t pmm_allocated_frames
Definition pmm.h:83
phyframe_t * pmm_allocate_frames(size_t n_frames, pmm_allocation_flags_t flags)
Allocate n_frames of contiguous physical memory.
Definition pmm.c:92
void pmm_init()
Definition pmm.c:20
pmm_region_t * pmm_find_reserved_region(ptr_t needle)
Find a region in the physical memory manager.
Definition pmm.c:131
void pmm_dump_lists(void)
Dump the physical memory manager's state, (i.e. the free list and the allocated list).
Definition pmm.c:84
@ PMM_ALLOC_NORMAL
allocate normal pages
Definition pmm.h:75
#define MOS_WARNING_PUSH
Definition mos_global.h:69
#define should_inline
Definition mos_global.h:37
#define MOS_WARNING_DISABLE(text)
Definition mos_global.h:71
#define MOS_WARNING_POP
Definition mos_global.h:70
#define MOS_STATIC_ASSERT
Definition mos_global.h:14
phyframe_state
Definition pmm.h:29
@ PHYFRAME_ALLOCATED
Definition pmm.h:32
@ PHYFRAME_FREE
Definition pmm.h:31
as_linked_list
Definition pmm.h:44
atomic_t allocated_refcount
Definition pmm.h:58
u8 order
Definition pmm.h:35
bool dirty
1 if the page is dirty
Definition pmm.h:50
bool reserved
Definition pmm.h:68
pfn_t pfn_start
Definition pmm.h:66
u32 type
Definition pmm.h:69
size_t nframes
Definition pmm.h:67
unsigned int u32
Definition types.h:21
unsigned long long pfn_t
Definition types.h:41
unsigned long ptr_t
Definition types.h:25
unsigned char u8
Definition types.h:19