IgrOS-Kernel 0.1.34.545
Simple C++20 Kernel for i386/x86_64
Loading...
Searching...
No Matches
paging.hpp
Go to the documentation of this file.
1
11
12
13#pragma once
14
15
16// IgrOS-Kernel arch
17#include <arch/types.hpp>
18// IgrOS-Kernel arch x86
19#include <arch/x86/native.hpp>
20// IgrOS-Kernel library
21#include <klib/kFlags.hpp>
22// IgrOS-Kernel memory
23#include <mem/phys.hpp>
24#include <mem/virt.hpp>
25
26
27// x86 namespace
28namespace igros::x86 {
29
30
37 class paging final {
38
39 public:
40
42 ~paging() noexcept = default;
43
45 paging(const paging &other) = delete;
47 auto operator=(const paging &other) -> paging & = delete;
48
51
53 enum class FLAGS : entry_t {
54 PRESENT = 0x001,
55 WRITABLE = 0x002,
56 USER = 0x004,
57 WRITE_THROUGH = 0x008,
58 NON_CACHED = 0x010,
59 ACCESSED = 0x020,
60 DIRTY = 0x040,
61 HUGE = 0x080,
62#if defined(IGROS_ARCH_x86_64)
63 NO_EXECUTE = 0x8000000000000000,
64#else
65 NO_EXECUTE = 0x000,
66#endif
67 GLOBAL = 0x100,
68 };
69
70 // Page size
71 constexpr static auto PAGE_SHIFT {mem::PAGE_SHIFT};
72 constexpr static auto PAGE_SIZE {mem::PAGE_SIZE};
73
74#if defined(IGROS_ARCH_i386)
75
76 // Page directory -> page table (4 KB pages, no PAE)
77 constexpr static auto LEVELS {2_usize};
78 constexpr static auto INDEX_BITS {10_usize};
80 constexpr static auto ADDRESS_MASK {entry_t {0xFFFFF000}};
82 constexpr static auto DIRECT_MAP_LIMIT {mem::phys_t {0x30000000}};
83
84#elif defined(IGROS_ARCH_x86_64)
85
86 // PML4 -> PDPT -> page directory -> page table
87 constexpr static auto LEVELS {4_usize};
88 constexpr static auto INDEX_BITS {9_usize};
90 constexpr static auto ADDRESS_MASK {entry_t {0x000FFFFFFFFFF000}};
92 constexpr static auto DIRECT_MAP_LIMIT {mem::phys_t {0x40000000}};
93
94#endif
95
97 constexpr static auto ENTRIES {1_usize << INDEX_BITS};
99 constexpr static auto LARGE_PAGE_SIZE {PAGE_SIZE << INDEX_BITS};
100
102 constexpr static auto INVALID {~mem::phys_t {0}};
103
104
113 static void init() noexcept;
114
120 [[nodiscard]]
121 static auto map(const mem::phys_t phys, const mem::virt_t virt, const klib::kFlags<FLAGS> flags) noexcept -> bool;
124 static void unmap(const mem::virt_t virt) noexcept;
125
129 [[nodiscard]]
130 static auto translate(const mem::virt_t virt) noexcept -> mem::phys_t;
131
134 [[nodiscard]]
135 static auto root() noexcept -> mem::phys_t;
136
139 [[nodiscard]]
140 static auto directMapEnd() noexcept -> mem::phys_t;
143 [[nodiscard]]
144 static auto dynamicStart() noexcept -> igros_byte_t*;
145
148 [[noreturn]]
149 static void exHandler(const register_t* regs) noexcept;
150
151
152 private:
153
155 enum class FAULT : igros_usize_t {
156 PROTECTED = 0x01,
157 WRITE = 0x02,
158 USER = 0x04,
159 RESERVED = 0x08,
160 FETCH = 0x10,
161 };
162
166 [[nodiscard]]
167 static auto table(const mem::phys_t phys) noexcept -> entry_t*;
170 [[nodiscard]]
171 static auto allocTable() noexcept -> mem::phys_t;
176 [[nodiscard]]
177 static auto index(const mem::virt_t virt, const igros_usize_t level) noexcept -> igros_usize_t;
187 [[nodiscard]]
188 static auto entry(const mem::phys_t top, const mem::virt_t virt, const bool create, const bool user, const igros_usize_t level = 0_usize) noexcept -> entry_t*;
193 [[nodiscard]]
194 static auto makeEntry(const mem::phys_t phys, const klib::kFlags<FLAGS> flags) noexcept -> entry_t;
201 [[nodiscard]]
202 static auto map(const mem::phys_t top, const mem::phys_t phys, const mem::virt_t virt, const klib::kFlags<FLAGS> flags) noexcept -> bool;
209 [[nodiscard]]
210 static auto mapLarge(const mem::phys_t top, const mem::phys_t phys, const mem::virt_t virt, const klib::kFlags<FLAGS> flags) noexcept -> bool;
214 [[nodiscard]]
215 static auto kernelFlags(const mem::phys_t phys) noexcept -> klib::kFlags<FLAGS>;
216
219 [[nodiscard]]
220 static auto selfTest() noexcept -> bool;
221 };
222
223
224} // namespace igros::x86
225
static void unmap(const mem::virt_t virt) noexcept
Unmap 4 KB page.
Definition paging.cpp:187
igros_usize_t entry_t
Page table entry (register-wide).
Definition paging.hpp:50
static auto table(const mem::phys_t phys) noexcept -> entry_t *
Table at physical address (through direct map).
Definition paging.cpp:76
static auto selfTest() noexcept -> bool
Check mapping on a scratch page.
Definition paging.cpp:322
static auto root() noexcept -> mem::phys_t
Current top level table physical address.
Definition paging.cpp:223
static auto allocTable() noexcept -> mem::phys_t
Allocate empty table (reachable through direct map, 0 if no memory).
Definition paging.cpp:83
static auto directMapEnd() noexcept -> mem::phys_t
End of physical memory reachable through direct map.
Definition paging.cpp:230
static auto kernelFlags(const mem::phys_t phys) noexcept -> klib::kFlags< FLAGS >
Rights of kernel offset mapping of low memory physical page (kernel sections).
Definition paging.cpp:243
static auto entry(const mem::phys_t top, const mem::virt_t virt, const bool create, const bool user, const igros_usize_t level=0_usize) noexcept -> entry_t *
Entry of virtual address at given level (0 - page table) of given hierarchy.
Definition paging.cpp:100
FAULT
Page fault error code flags.
Definition paging.hpp:155
@ PROTECTED
Protection violation (else page not present).
Definition paging.hpp:156
@ FETCH
Instruction fetch.
Definition paging.hpp:160
@ RESERVED
Reserved bit set in entry.
Definition paging.hpp:159
@ WRITE
Write access (else read).
Definition paging.hpp:157
static constexpr auto ENTRIES
Entries per table.
Definition paging.hpp:97
static auto map(const mem::phys_t phys, const mem::virt_t virt, const klib::kFlags< FLAGS > flags) noexcept -> bool
Map 4 KB page (false if structures can't be allocated or address is in a large page).
Definition paging.cpp:182
static constexpr auto LEVELS
Table levels.
Definition paging.hpp:87
static void exHandler(const register_t *regs) noexcept
Page fault handler.
Definition paging.cpp:357
static constexpr auto LARGE_PAGE_SIZE
Large page size (4 MB on i386, 2 MB on x86_64).
Definition paging.hpp:99
~paging() noexcept=default
D-tor.
static constexpr auto PAGE_SIZE
Page size in bytes.
Definition paging.hpp:72
static auto makeEntry(const mem::phys_t phys, const klib::kFlags< FLAGS > flags) noexcept -> entry_t
Entry value for physical address with flags (drops NX if not enabled).
Definition paging.cpp:134
static auto dynamicStart() noexcept -> igros_byte_t *
Start of kernel dynamic mappings (right above direct map).
Definition paging.cpp:236
static void init() noexcept
Build kernel page tables and switch to them:
Definition paging.cpp:260
paging(const paging &other)=delete
Copy c-tor.
static auto index(const mem::virt_t virt, const igros_usize_t level) noexcept -> igros_usize_t
Index of virtual address in table of given level (0 - page table).
Definition paging.cpp:93
static constexpr auto INVALID
Address isn't mapped.
Definition paging.hpp:102
static constexpr auto INDEX_BITS
Definition paging.hpp:88
static constexpr auto DIRECT_MAP_LIMIT
Physical memory mapped at kernel offset (-2 GB .. -1 GB), rest is for dynamic mappings.
Definition paging.hpp:92
static constexpr auto ADDRESS_MASK
Physical address bits of entry (52-bit physical addresses).
Definition paging.hpp:90
static constexpr auto PAGE_SHIFT
Page size bits.
Definition paging.hpp:71
FLAGS
Page table entry flags.
Definition paging.hpp:53
@ NO_EXECUTE
Instruction fetch not allowed (EFER.NXE).
Definition paging.hpp:63
@ USER
User mode access.
Definition paging.hpp:56
@ WRITABLE
Write access.
Definition paging.hpp:55
@ WRITE_THROUGH
Write-through caching.
Definition paging.hpp:57
@ DIRTY
Set by CPU on write (pages).
Definition paging.hpp:60
@ GLOBAL
Not flushed on CR3 change.
Definition paging.hpp:67
@ PRESENT
Entry is valid.
Definition paging.hpp:54
@ ACCESSED
Set by CPU on access.
Definition paging.hpp:59
@ HUGE
Large page (directory levels).
Definition paging.hpp:61
@ NON_CACHED
Caching disabled.
Definition paging.hpp:58
static auto translate(const mem::virt_t virt) noexcept -> mem::phys_t
Physical address of virtual address (INVALID if not mapped).
Definition paging.cpp:198
static auto mapLarge(const mem::phys_t top, const mem::phys_t phys, const mem::virt_t virt, const klib::kFlags< FLAGS > flags) noexcept -> bool
Map large page in given hierarchy (fresh directory entry only).
Definition paging.cpp:163
std::size_t igros_usize_t
Unsigned size.
Definition types.hpp:51
std::uint8_t igros_byte_t
8-bit unsigned integer
Definition types.hpp:41
Bit flags template datatype.
Freestanding kernel library: printing, math, alignment, flags, strings.
Memory management: physical page allocator, direct map, virtual addresses.
constexpr auto PAGE_SHIFT
Page size bits.
Definition phys.hpp:39
igros_quad_t phys_t
Physical address (64-bit: physical memory may lie above 4 GB).
Definition phys.hpp:36
constexpr auto PAGE_SIZE
Page size in bytes.
Definition phys.hpp:40
Code shared by i386 and x86_64: ports, interrupts, exceptions, paging.
x86 family current (native) architecture selection
Physical memory allocator.
Common C++ types redefinition.
Virtual address type.