From 6434de8ff75a9143847ef529bc209790ac4909b3 Mon Sep 17 00:00:00 2001 From: Felix Morgner Date: Wed, 29 Oct 2025 11:09:42 +0100 Subject: kapi: move frame and address to KAPI --- kapi/include/kapi/memory.hpp | 5 +- kapi/include/kapi/memory/address.hpp | 47 ++++++++++++++++++ kapi/include/kapi/memory/frame.hpp | 92 ++++++++++++++++++++++++++++++++++++ 3 files changed, 143 insertions(+), 1 deletion(-) create mode 100644 kapi/include/kapi/memory/address.hpp create mode 100644 kapi/include/kapi/memory/frame.hpp (limited to 'kapi/include') 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 +#include +#include +#include + +namespace teachos::memory +{ + + enum struct address_type : bool + { + linear, + physical, + }; + + template + struct address + { + constexpr explicit address(std::uintptr_t value) noexcept + : m_value{value} + { + } + + explicit address(std::byte * pointer) noexcept + : m_value{std::bit_cast(pointer)} + { + } + + explicit operator std::byte *() const noexcept { return std::bit_cast(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; + using physical_address = address; + +} // 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 +#include +#include +#include + +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 -- cgit v1.2.3