aboutsummaryrefslogtreecommitdiff
path: root/kapi
diff options
context:
space:
mode:
authorFelix Morgner <felix.morgner@ost.ch>2025-10-29 11:09:42 +0100
committerFelix Morgner <felix.morgner@ost.ch>2025-10-29 11:09:42 +0100
commit6434de8ff75a9143847ef529bc209790ac4909b3 (patch)
treea66902303f99f10897cb04e294c55fabf814c49f /kapi
parentacabbacdee68ad80e829bda56ae5363d04646d2d (diff)
downloadteachos-6434de8ff75a9143847ef529bc209790ac4909b3.tar.xz
teachos-6434de8ff75a9143847ef529bc209790ac4909b3.zip
kapi: move frame and address to KAPI
Diffstat (limited to 'kapi')
-rw-r--r--kapi/include/kapi/memory.hpp5
-rw-r--r--kapi/include/kapi/memory/address.hpp47
-rw-r--r--kapi/include/kapi/memory/frame.hpp92
3 files changed, 143 insertions, 1 deletions
diff --git a/kapi/include/kapi/memory.hpp b/kapi/include/kapi/memory.hpp
index 842a2fa..3ad5ac5 100644
--- a/kapi/include/kapi/memory.hpp
+++ b/kapi/include/kapi/memory.hpp
@@ -1,9 +1,12 @@
#ifndef TEACHOS_KAPI_MEMORY_HPP
#define TEACHOS_KAPI_MEMORY_HPP
+#include "kapi/memory/address.hpp" // IWYU pragma: export
+#include "kapi/memory/frame.hpp" // IWYU pragma: export
+
namespace teachos::memory
{
auto init() -> void;
-}
+} // namespace teachos::memory
#endif
diff --git a/kapi/include/kapi/memory/address.hpp b/kapi/include/kapi/memory/address.hpp
new file mode 100644
index 0000000..585807a
--- /dev/null
+++ b/kapi/include/kapi/memory/address.hpp
@@ -0,0 +1,47 @@
+#ifndef TEACHOS_KAPI_MEMORY_ADDRESS_HPP
+#define TEACHOS_KAPI_MEMORY_ADDRESS_HPP
+
+#include <bit>
+#include <compare>
+#include <cstddef>
+#include <cstdint>
+
+namespace teachos::memory
+{
+
+ enum struct address_type : bool
+ {
+ linear,
+ physical,
+ };
+
+ template<address_type Type>
+ struct address
+ {
+ constexpr explicit address(std::uintptr_t value) noexcept
+ : m_value{value}
+ {
+ }
+
+ explicit address(std::byte * pointer) noexcept
+ : m_value{std::bit_cast<std::uintptr_t>(pointer)}
+ {
+ }
+
+ explicit operator std::byte *() const noexcept { return std::bit_cast<std::byte *>(m_value); }
+
+ auto constexpr operator<=>(address const &) const noexcept -> std::strong_ordering = default;
+ auto constexpr operator==(address const &) const noexcept -> bool = default;
+
+ auto constexpr raw() const noexcept -> std::uintptr_t { return m_value; }
+
+ private:
+ std::uintptr_t m_value{};
+ };
+
+ using linear_address = address<address_type::linear>;
+ using physical_address = address<address_type::physical>;
+
+} // namespace teachos::memory
+
+#endif \ No newline at end of file
diff --git a/kapi/include/kapi/memory/frame.hpp b/kapi/include/kapi/memory/frame.hpp
new file mode 100644
index 0000000..1251e51
--- /dev/null
+++ b/kapi/include/kapi/memory/frame.hpp
@@ -0,0 +1,92 @@
+#ifndef TEACHOS_KAPI_MEMORY_FRAME_HPP
+#define TEACHOS_KAPI_MEMORY_FRAME_HPP
+
+#include "kapi/memory/address.hpp"
+
+#include <bit>
+#include <compare>
+#include <cstddef>
+#include <cstdint>
+
+namespace teachos::memory
+{
+
+ extern std::size_t const PLATFORM_FRAME_SIZE;
+
+ struct frame
+ {
+ constexpr frame() = default;
+
+ explicit constexpr frame(std::size_t number)
+ : m_number{number}
+ {
+ }
+
+ /**
+ * @brief Returns the physical frame the given address is contained in.
+ *
+ * @param address Physical address we want to get the corresponding physical frame for.
+ * @return Frame the given address is contained in.
+ */
+ auto constexpr static containing(physical_address address) noexcept -> frame
+ {
+ return frame{address.raw() / PLATFORM_FRAME_SIZE};
+ }
+
+ /**
+ * @brief Get the start address of this physical frame.
+ *
+ * @return Start address of the physical frame.
+ */
+ auto constexpr start_address() const noexcept -> physical_address
+ {
+ return physical_address{m_number * PLATFORM_FRAME_SIZE};
+ }
+
+ /**
+ * @brief Get the nth next frame.
+ *
+ * @param offset
+ * @return A new frame n frames after this one.
+ */
+ auto constexpr operator+(std::size_t n) const noexcept -> frame { return frame{m_number + n}; }
+
+ /**
+ * @brief Increment this frame to refer to the next one, returning a copy.
+ *
+ * @return Copy of the incremented underlying frame number.
+ */
+ auto constexpr operator++(int) noexcept -> frame
+ {
+ auto copy = *this;
+ return ++copy;
+ }
+
+ /**
+ * @brief Increment this frame to refer to the next one, returning a this frame.
+ *
+ * @return Reference to the incremented underlying frame number.
+ */
+ auto constexpr operator++() noexcept -> frame &
+ {
+ ++m_number;
+ return *this;
+ }
+
+ /**
+ * @brief Check if this frame refers to the same frame as @p other.
+ */
+ auto constexpr operator==(frame const & other) const noexcept -> bool = default;
+
+ /**
+ * @brief Lexicographically compare this frame to @p other.
+ */
+ auto constexpr operator<=>(frame const & other) const noexcept -> std::strong_ordering = default;
+
+ private:
+ std::size_t m_number{};
+ };
+
+} // namespace teachos::memory
+
+#endif \ No newline at end of file