IgrOS-Kernel 0.1.34.545
Simple C++20 Kernel for i386/x86_64
Loading...
Searching...
No Matches
direct.hpp
Go to the documentation of this file.
1
11
12
13#pragma once
14
15
16// C++
17#include <bit>
18// IgrOS-Kernel arch
19#include <arch/types.hpp>
20// IgrOS-Kernel memory
21#include <mem/phys.hpp>
22// IgrOS-Kernel platform
23#include <platform/platform.hpp>
24
25
26// Memory code zone
27namespace igros::mem {
28
29
35 class direct final {
36
38 static inline constinit phys_t mEnd {LOW_MEMORY_LIMIT};
39
40
41 public:
42
44 ~direct() noexcept = default;
45
47 direct(const direct &other) = delete;
49 auto operator=(const direct &other) -> direct & = delete;
50
53 [[nodiscard]]
54 static auto end() noexcept -> phys_t;
57 static void setEnd(const phys_t end) noexcept;
58
63 [[nodiscard]]
64 static auto isMapped(const phys_t phys, const phys_t size) noexcept -> bool;
65
71 template<class T>
72 [[nodiscard]]
73 static auto pointer(const phys_t phys, const phys_t size = sizeof(T)) noexcept -> T*;
77 [[nodiscard]]
78 static auto string(const phys_t phys) noexcept -> const char*;
79
84 template<class T>
85 [[nodiscard]]
86 static auto physOf(const T* const object) noexcept -> phys_t;
87 };
88
89
91 [[nodiscard]]
92 inline auto direct::end() noexcept -> phys_t {
93 return mEnd;
94 }
95
97 inline void direct::setEnd(const phys_t end) noexcept {
98 mEnd = end;
99 }
100
101
103 [[nodiscard]]
104 inline auto direct::isMapped(const phys_t phys, const phys_t size) noexcept -> bool {
105 return (phys < mEnd) && (size <= (mEnd - phys));
106 }
107
108
110 template<class T>
111 [[nodiscard]]
112 inline auto direct::pointer(const phys_t phys, const phys_t size) noexcept -> T* {
113 if (!direct::isMapped(phys, size)) [[unlikely]] {
114 return nullptr;
115 }
116 return std::bit_cast<T*>(static_cast<igros_usize_t>(phys) + platform::Platform::kernelOffset());
117 }
118
120 [[nodiscard]]
121 inline auto direct::string(const phys_t phys) noexcept -> const char* {
122 const auto* const text {direct::pointer<const char>(phys)};
123 if (nullptr == text) [[unlikely]] {
124 return nullptr;
125 }
126 // Never read past direct map end looking for null terminator
127 // (index, not [begin, end) range: end pointer wraps if direct map reaches end of address space)
128 const auto available {mEnd - phys};
129 for (auto length {0_u64}; length < available; ++length) {
130 if ('\0' == text[length]) {
131 return text;
132 }
133 }
134 return nullptr;
135 }
136
137
139 template<class T>
140 [[nodiscard]]
141 inline auto direct::physOf(const T* const object) noexcept -> phys_t {
142 return static_cast<phys_t>(std::bit_cast<igros_usize_t>(object) - platform::Platform::kernelOffset());
143 }
144
145
146} // namespace igros::mem
static auto physOf(const T *const object) noexcept -> phys_t
Physical address of directly mapped object.
Definition direct.hpp:141
static auto end() noexcept -> phys_t
End of directly mapped physical memory.
Definition direct.hpp:92
static void setEnd(const phys_t end) noexcept
Set end of directly mapped physical memory (paging mapped more memory).
Definition direct.hpp:97
~direct() noexcept=default
D-tor.
static auto isMapped(const phys_t phys, const phys_t size) noexcept -> bool
Check if whole physical range is directly mapped.
Definition direct.hpp:104
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 constinit phys_t mEnd
End of directly mapped physical memory.
Definition direct.hpp:38
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
direct(const direct &other)=delete
Copy c-tor.
Physical page frame allocator.
Definition phys.hpp:52
static auto kernelOffset() noexcept -> igros_usize_t
Get kernel virtual memory offset (kernel virtual address = physical address + offset).
Definition platform.hpp:346
std::size_t igros_usize_t
Unsigned size.
Definition types.hpp:51
Memory management: physical page allocator, direct map, virtual addresses.
constexpr auto LOW_MEMORY_LIMIT
Physical memory mapped by boot code at kernel virtual offset (see boot.s).
Definition phys.hpp:43
igros_quad_t phys_t
Physical address (64-bit: physical memory may lie above 4 GB).
Definition phys.hpp:36
Physical memory allocator.
IgrOS platform description.
Common C++ types redefinition.