IgrOS-Kernel 0.1.34.545
Simple C++20 Kernel for i386/x86_64
Loading...
Searching...
No Matches
phys.cpp
Go to the documentation of this file.
1
11
12
13// C++
14#include <algorithm>
15#include <array>
16#include <bit>
17#include <initializer_list>
18#include <limits>
19#include <span>
20// IgrOS-Kernel library
21#include <klib/kAlign.hpp>
22#include <klib/kString.hpp>
23// IgrOS-Kernel memory
24#include <mem/direct.hpp>
25#include <mem/phys.hpp>
26// IgrOS-Kernel multiboot
28// IgrOS-Kernel platform
29#include <platform/platform.hpp>
30
31
32// Memory code zone
33namespace igros::mem {
34
35
37 namespace {
38
39
42
44 constexpr auto WORD_BITS {static_cast<igros_usize_t>(std::numeric_limits<word_t>::digits)};
46 constexpr auto PAGES {static_cast<igros_usize_t>(phys::MEMORY_LIMIT >> PAGE_SHIFT)};
47
51 constinit std::array<word_t, PAGES / WORD_BITS> freeMap {};
53 constinit igros_usize_t freeCount {0_usize};
55 constinit igros_usize_t searchHint {0_usize};
57 constinit phys_t memoryTop {0_u64};
58
59
63 [[nodiscard]]
64 auto isFree(const igros_usize_t page) noexcept -> bool {
65 return 0_u32 != (freeMap[page / WORD_BITS] & (1_u32 << (page % WORD_BITS)));
66 }
67
70 void setFree(const igros_usize_t page) noexcept {
71 freeMap[page / WORD_BITS] |= (1_u32 << (page % WORD_BITS));
72 ++freeCount;
73 searchHint = std::min(searchHint, page / WORD_BITS);
74 }
75
78 void setUsed(const igros_usize_t page) noexcept {
79 freeMap[page / WORD_BITS] &= ~(1_u32 << (page % WORD_BITS));
80 --freeCount;
81 }
82
85 void markUsedString(const phys_t address) noexcept {
86 const auto* const text {direct::string(address)};
87 phys::markUsed(address, address + klib::kstrview(text).size() + 1_u64);
88 }
89
92 void markUsedModules(const multiboot::info_t &info) noexcept {
93 if (!info.hasInfoModules()) {
94 return;
95 }
96 const auto modulesSize {static_cast<phys_t>(info.modulesCount) * sizeof(multiboot::moduleEntry)};
97 phys::markUsed(info.modulesAddr, info.modulesAddr + modulesSize);
98 // Modules can't be found if modules list is not directly mapped
99 const auto* const modules {direct::pointer<const multiboot::moduleEntry>(info.modulesAddr, modulesSize)};
100 if (nullptr == modules) [[unlikely]] {
101 return;
102 }
103 for (const auto &bootModule : std::span {modules, info.modulesCount}) {
104 phys::markUsed(bootModule.start, bootModule.end);
105 if (0_u32 != bootModule.name) {
106 markUsedString(bootModule.name);
107 }
108 }
109 }
110
111
112 } // namespace
113
114
115 // Initialize from bootloader memory map (reserves kernel and boot data)
116 void phys::init(const multiboot::info_t &info) noexcept {
117
118 // Start from scratch: all memory is used
119 freeMap.fill(0_u32);
120 freeCount = 0_usize;
121 searchHint = freeMap.size();
122 memoryTop = 0_u64;
123
124 // Available memory from bootloader memory map
125 auto hasAvailable {false};
126 for (const auto &entry : info.memoryMap()) {
127 if (multiboot::MEMORY_MAP_TYPE::AVAILABLE == entry.type) {
128 phys::markFree(entry.address, entry.address + entry.length);
129 hasAvailable = true;
130 }
131 }
132 // Basic memory info as fallback (upper memory in KB, starting at 1 MB)
133 if (!hasAvailable && info.hasInfoMemory()) {
134 phys::markFree(0x100000_u64, 0x100000_u64 + (static_cast<phys_t>(info.memHigh) << 10));
135 }
136
137 // Low memory (real mode IVT, BIOS data, EBDA, video memory, ROMs)
139 // Kernel image (loaded right above low memory)
141
142 // Multiboot info structure
143 phys::markUsed(direct::physOf(&info), direct::physOf(&info) + sizeof(info));
144 // Memory map
145 if (info.hasInfoMemoryMap()) {
146 phys::markUsed(info.mmapAddr, static_cast<phys_t>(info.mmapAddr) + info.mmapLength);
147 }
148 // Command line and bootloader name
149 if (info.hasInfoCommandLine()) {
150 markUsedString(info.cmdLine);
151 }
152 if (info.hasInfoBootloaderName()) {
153 markUsedString(info.bootloaderName);
154 }
155 // Modules list, modules and their names
156 markUsedModules(info);
157 }
158
159
160 // Allocate physical page below limit (0 if no memory left)
161 [[nodiscard]]
162 auto phys::alloc(const phys_t limit) noexcept -> phys_t {
163 // Pages below limit
164 const auto limitPages {static_cast<igros_usize_t>(std::min(limit, MEMORY_LIMIT) >> PAGE_SHIFT)};
165 const auto limitWords {(limitPages + WORD_BITS - 1_usize) / WORD_BITS};
166 // Find first word with free page
167 for (auto word {searchHint}; word < limitWords; ++word) {
168 if (0_u32 != freeMap[word]) {
169 const auto page {(word * WORD_BITS) + static_cast<igros_usize_t>(std::countr_zero(freeMap[word]))};
170 // Free page is above limit
171 if (page >= limitPages) [[unlikely]] {
172 break;
173 }
174 // Allocate page
175 setUsed(page);
176 searchHint = word;
177 return static_cast<phys_t>(page) << PAGE_SHIFT;
178 }
179 }
180 // Out of memory
181 return 0_u64;
182 }
183
184 // Free physical page
185 void phys::free(const phys_t page) noexcept {
186 // Ignore null, unaligned and untracked pages
187 if ((0_u64 == page) || !klib::kAlign::check<PAGE_SIZE>(page) || (page >= MEMORY_LIMIT)) [[unlikely]] {
188 return;
189 }
190 // Ignore double free
191 const auto index {static_cast<igros_usize_t>(page >> PAGE_SHIFT)};
192 if (isFree(index)) [[unlikely]] {
193 return;
194 }
195 // Return page
196 setFree(index);
197 }
198
199
200 // Number of free pages
201 [[nodiscard]]
202 auto phys::freePages() noexcept -> igros_usize_t {
203 return freeCount;
204 }
205
206
207 // End of highest memory ever marked free
208 [[nodiscard]]
209 auto phys::top() noexcept -> phys_t {
210 return memoryTop;
211 }
212
213
214 // Mark physical memory range [start, end) as free (only whole pages)
215 void phys::markFree(const phys_t start, const phys_t end) noexcept {
216 // Clamp to tracked memory
217 const auto from {std::min(start, MEMORY_LIMIT)};
218 const auto to {std::min(end, MEMORY_LIMIT)};
219 // Whole pages inside range
220 const auto first {static_cast<igros_usize_t>(klib::kAlign::up<PAGE_SIZE>(from) >> PAGE_SHIFT)};
221 const auto last {static_cast<igros_usize_t>(to >> PAGE_SHIFT)};
222 // Top of usable memory (whole pages only)
223 if (first < last) {
224 memoryTop = std::max(memoryTop, static_cast<phys_t>(last) << PAGE_SHIFT);
225 }
226 for (auto page {first}; page < last; ++page) {
227 if (!isFree(page)) {
228 setFree(page);
229 }
230 }
231 }
232
233 // Mark physical memory range [start, end) as used (every touched page)
234 void phys::markUsed(const phys_t start, const phys_t end) noexcept {
235 // Clamp to tracked memory
236 const auto from {std::min(start, MEMORY_LIMIT)};
237 const auto to {std::min(end, MEMORY_LIMIT)};
238 // Every page touched by range
239 const auto first {static_cast<igros_usize_t>(from >> PAGE_SHIFT)};
240 const auto last {static_cast<igros_usize_t>(klib::kAlign::up<PAGE_SIZE>(to) >> PAGE_SHIFT)};
241 for (auto page {first}; page < last; ++page) {
242 if (isFree(page)) {
243 setUsed(page);
244 }
245 }
246 }
247
248
249} // namespace igros::mem
250
static constexpr auto check(const T value) noexcept -> bool
Check alignment.
Definition kAlign.hpp:155
static constexpr auto up(const T value) noexcept -> T
Align up.
Definition kAlign.hpp:163
static auto physOf(const T *const object) noexcept -> phys_t
Physical address of directly mapped object.
Definition direct.hpp:141
static auto string(const phys_t phys) noexcept -> const char *
String at physical memory (nullptr if not directly mapped up to null terminator).
Definition direct.hpp:121
static auto pointer(const phys_t phys, const phys_t size=sizeof(T)) noexcept -> T *
Pointer to physical memory (nullptr if range is not directly mapped).
Definition direct.hpp:112
static void markUsed(const phys_t start, const phys_t end) noexcept
Mark physical memory range [start, end) as used (every touched page).
Definition phys.cpp:234
static void free(const phys_t page) noexcept
Free physical page.
Definition phys.cpp:185
static constexpr auto MEMORY_LIMIT
Physical memory tracked by allocator (bitmap of 128 KB).
Definition phys.hpp:65
static auto freePages() noexcept -> igros_usize_t
Number of free pages.
Definition phys.cpp:202
static auto alloc(const phys_t limit=MEMORY_LIMIT) noexcept -> phys_t
Allocate physical page below limit (0 if no memory left).
Definition phys.cpp:162
static void init(const multiboot::info_t &info) noexcept
Initialize from bootloader memory map (reserves kernel and boot data).
Definition phys.cpp:116
static auto top() noexcept -> phys_t
End of highest memory ever marked free (top of usable RAM, clamped to MEMORY_LIMIT).
Definition phys.cpp:209
static void markFree(const phys_t start, const phys_t end) noexcept
Mark physical memory range [start, end) as free (only whole pages).
Definition phys.cpp:215
static constexpr auto kernelEnd() noexcept -> const igros_byte_t *
Get kernel end address.
Definition platform.hpp:302
Direct map of physical memory.
std::size_t igros_usize_t
Unsigned size.
Definition types.hpp:51
std::uint32_t igros_dword_t
32-bit unsigned integer
Definition types.hpp:43
Kernel alignment functions definitions.
Kernel string functions.
Multiboot 1 header info.
constexpr auto kstrview(const char *const src) noexcept -> std::string_view
View of null-terminated string (null pointer is empty string).
Definition kString.hpp:28
void setFree(const igros_usize_t page) noexcept
Mark page as free.
Definition phys.cpp:70
igros_dword_t word_t
Bitmap word.
Definition phys.cpp:41
constinit igros_usize_t searchHint
No free pages in words below this one.
Definition phys.cpp:55
constinit std::array< word_t, PAGES/WORD_BITS > freeMap
Free pages bitmap (bit set - page is free).
Definition phys.cpp:51
void setUsed(const igros_usize_t page) noexcept
Mark page as used.
Definition phys.cpp:78
constexpr auto PAGES
Number of tracked pages.
Definition phys.cpp:46
auto isFree(const igros_usize_t page) noexcept -> bool
Check if page is free.
Definition phys.cpp:64
constinit phys_t memoryTop
End of highest memory marked free.
Definition phys.cpp:57
constinit igros_usize_t freeCount
Number of free pages.
Definition phys.cpp:53
constexpr auto WORD_BITS
Pages per bitmap word.
Definition phys.cpp:44
void markUsedString(const phys_t address) noexcept
Mark boot string as used (only first byte if string is not directly mapped).
Definition phys.cpp:85
void markUsedModules(const multiboot::info_t &info) noexcept
Mark modules list, modules and their names as used.
Definition phys.cpp:92
Memory management: physical page allocator, direct map, virtual addresses.
constexpr auto PAGE_SHIFT
Page size bits.
Definition phys.hpp:39
constexpr auto LOW_MEMORY_RESERVED
Physical memory never given out (BIOS, video memory, ROMs).
Definition phys.hpp:45
igros_quad_t phys_t
Physical address (64-bit: physical memory may lie above 4 GB).
Definition phys.hpp:36
constexpr auto address(const virt_t virt) noexcept -> igros_usize_t
Numeric value of virtual address.
Definition virt.hpp:47
Physical memory allocator.
IgrOS platform description.
Multiboot 1 information from bootloader.
Definition multiboot.hpp:97
Multiboot 1 module entry.