MOS Source Code
Loading...
Searching...
No Matches
pmm.hpp
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.hpp>
8
22
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
38 {
39 list_node_t list_node; // for use of freelist in the buddy allocator
40
41 struct pagecache_frame_info // allocated frame
42 {
43 // for page cache frames
44 bool dirty : 1;
45 } pagecache;
46 } info;
47
49 {
50 // number of times this frame is mapped, if this drops to 0, the frame is freed
52 } alloc;
54
55MOS_STATIC_ASSERT(sizeof(phyframe_t) == 32, "update phyframe_t struct size");
56
57typedef struct
58{
60 size_t nframes;
62 u32 type; // platform-specific type
64
70
71extern phyframe_t *phyframes; // array of all physical frames
72
73#define phyframe_pfn(frame) ((pfn_t) ((frame) - phyframes))
74#define pfn_phyframe(pfn) (&phyframes[(pfn)])
75
77
81void pmm_dump_lists(void);
82
83void pmm_init();
84
93void pmm_free_frames(phyframe_t *start_frame, size_t n_pages);
94
108pfn_t pmm_reserve_frames(pfn_t pfn, size_t npages);
109#define pmm_reserve_address(paddr) pmm_reserve_frames(ALIGN_DOWN_TO_PAGE(paddr) / MOS_PAGE_SIZE, 1)
110#define pmm_reserve_addresses(paddr, npages) pmm_reserve_frames(ALIGN_DOWN_TO_PAGE(paddr) / MOS_PAGE_SIZE, npages)
111
119
120// ! ref and unref frames
121
122phyframe_t *_pmm_ref_phyframes(phyframe_t *frame, size_t npages);
123void _pmm_unref_phyframes(phyframe_t *frame, size_t npages);
124
126{
127 _pmm_ref_phyframes(pfn_phyframe(pfn_start), npages);
128 return pfn_start;
129}
130
131should_inline void _pmm_unref_pfn_range(pfn_t pfn_start, size_t npages)
132{
133 _pmm_unref_phyframes(pfn_phyframe(pfn_start), npages);
134}
135
136#ifdef __cplusplus
137should_inline phyframe_t *pmm_ref(phyframe_t *frame, size_t npages = 1)
138{
139 return _pmm_ref_phyframes(frame, npages);
140}
141should_inline pfn_t pmm_ref(pfn_t pfn_start, size_t npages = 1)
142{
143 return _pmm_ref_pfn_range(pfn_start, npages);
144}
145
146should_inline void pmm_unref(phyframe_t *frame, size_t npages = 1)
147{
148 _pmm_unref_phyframes(frame, npages);
149}
150should_inline void pmm_unref(pfn_t pfn_start, size_t npages = 1)
151{
152 _pmm_unref_pfn_range(pfn_start, npages);
153}
154#else
155#define pmm_ref(thing, npages) _Generic((thing), phyframe_t *: _pmm_ref_phyframes, pfn_t: _pmm_ref_pfn_range)(thing, npages)
156#define pmm_unref(thing, npages) _Generic((thing), phyframe_t *: _pmm_unref_phyframes, pfn_t: _pmm_unref_pfn_range)(thing, npages)
157#endif
158
159#define pmm_ref_one(thing) pmm_ref(thing, 1)
160#define pmm_unref_one(thing) pmm_unref(thing, 1)
161
#define pfn_phyframe(pfn)
Definition pmm.hpp:74
#define pmm_ref(thing, npages)
Definition pmm.hpp:155
void _pmm_unref_phyframes(phyframe_t *frame, size_t npages)
Definition pmm.cpp:162
size_t pmm_total_frames
Definition pmm.cpp:16
phyframe_t * _pmm_ref_phyframes(phyframe_t *frame, size_t npages)
Definition pmm.cpp:151
size_t pmm_reserved_frames
Definition pmm.hpp:76
pfn_t pmm_reserve_frames(pfn_t pfn, size_t npages)
Mark a range of physical memory as reserved.
Definition pmm.cpp:122
void pmm_free_frames(phyframe_t *start_frame, size_t n_pages)
Definition pmm.cpp:108
pmm_allocation_flags_t
Definition pmm.hpp:66
phyframe_t * phyframes
Definition pmm.cpp:15
should_inline pfn_t _pmm_ref_pfn_range(pfn_t pfn_start, size_t npages)
Definition pmm.hpp:125
should_inline void _pmm_unref_pfn_range(pfn_t pfn_start, size_t npages)
Definition pmm.hpp:131
#define pmm_unref(thing, npages)
Definition pmm.hpp:156
size_t pmm_allocated_frames
Definition pmm.hpp:76
phyframe_t * pmm_allocate_frames(size_t n_frames, pmm_allocation_flags_t flags)
Allocate n_frames of contiguous physical memory.
Definition pmm.cpp:92
void pmm_init()
Definition pmm.cpp:20
pmm_region_t * pmm_find_reserved_region(ptr_t needle)
Find a region in the physical memory manager.
Definition pmm.cpp:131
void pmm_dump_lists(void)
Dump the physical memory manager's state, (i.e. the free list and the allocated list).
Definition pmm.cpp:84
@ PMM_ALLOC_NORMAL
allocate normal pages
Definition pmm.hpp:68
#define should_inline
Definition mos_global.h:37
#define MOS_STATIC_ASSERT
Definition mos_global.h:14
phyframe_state
Definition pmm.hpp:29
@ PHYFRAME_ALLOCATED
Definition pmm.hpp:32
@ PHYFRAME_FREE
Definition pmm.hpp:31
@ PHYFRAME_RESERVED
Definition pmm.hpp:30
u8 order
Definition pmm.hpp:35
bool reserved
Definition pmm.hpp:61
pfn_t pfn_start
Definition pmm.hpp:59
u32 type
Definition pmm.hpp:62
size_t nframes
Definition pmm.hpp:60
unsigned int u32
Definition types.h:17
unsigned long long pfn_t
Definition types.h:37
unsigned long ptr_t
Definition types.h:21
unsigned char u8
Definition types.h:15
std::atomic_size_t atomic_t
Definition types.hpp:11
list_node_t list_node
Definition pmm.hpp:39
atomic_t refcount
Definition pmm.hpp:51