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 --- arch/x86_64/include/x86_64/boot/boot.hpp | 26 +++--- arch/x86_64/include/x86_64/boot/ld.hpp | 4 +- arch/x86_64/include/x86_64/cpu/registers.hpp | 4 +- arch/x86_64/include/x86_64/device_io/port_io.hpp | 4 +- arch/x86_64/include/x86_64/memory/address.hpp | 47 ----------- arch/x86_64/include/x86_64/memory/frame.hpp | 95 ---------------------- arch/x86_64/include/x86_64/memory/mmu.hpp | 6 +- .../include/x86_64/memory/region_allocator.hpp | 8 +- arch/x86_64/include/x86_64/vga/crtc.hpp | 8 +- arch/x86_64/include/x86_64/vga/text.hpp | 4 +- arch/x86_64/src/cpu/registers.cpp | 4 +- arch/x86_64/src/kapi/cio.cpp | 2 +- arch/x86_64/src/kapi/memory.cpp | 8 +- arch/x86_64/src/memory/mmu.cpp | 6 +- arch/x86_64/src/memory/region_allocator.cpp | 16 +--- arch/x86_64/src/vga/text.cpp | 6 +- kapi/include/kapi/memory.hpp | 5 +- kapi/include/kapi/memory/address.hpp | 47 +++++++++++ kapi/include/kapi/memory/frame.hpp | 92 +++++++++++++++++++++ src/main.cpp | 2 +- 20 files changed, 198 insertions(+), 196 deletions(-) delete mode 100644 arch/x86_64/include/x86_64/memory/address.hpp delete mode 100644 arch/x86_64/include/x86_64/memory/frame.hpp create mode 100644 kapi/include/kapi/memory/address.hpp create mode 100644 kapi/include/kapi/memory/frame.hpp diff --git a/arch/x86_64/include/x86_64/boot/boot.hpp b/arch/x86_64/include/x86_64/boot/boot.hpp index 1887e73..86f8ce3 100644 --- a/arch/x86_64/include/x86_64/boot/boot.hpp +++ b/arch/x86_64/include/x86_64/boot/boot.hpp @@ -48,18 +48,22 @@ #include -extern "C" +namespace teachos::boot::x86_64 { - /** - * @brief A pointer to the multiboot 2 information structure provided by the boot loader. - */ - extern kstd::asm_ptr multiboot_information_pointer; - - /** - * @brief A pointer to the VGA text mode buffer. - */ - extern kstd::asm_ptr> vga_buffer_pointer; -} + + extern "C" + { + /** + * @brief A pointer to the multiboot 2 information structure provided by the boot loader. + */ + extern kstd::asm_ptr multiboot_information_pointer; + + /** + * @brief A pointer to the VGA text mode buffer. + */ + extern kstd::asm_ptr> vga_buffer_pointer; + } +} // namespace teachos::boot::x86_64 #endif diff --git a/arch/x86_64/include/x86_64/boot/ld.hpp b/arch/x86_64/include/x86_64/boot/ld.hpp index 51eb23b..cf59c66 100644 --- a/arch/x86_64/include/x86_64/boot/ld.hpp +++ b/arch/x86_64/include/x86_64/boot/ld.hpp @@ -17,7 +17,7 @@ #include -namespace teachos::x86_64::boot +namespace teachos::boot::x86_64 { /** * @var _start_physical @@ -42,6 +42,6 @@ namespace teachos::x86_64::boot * @see _start_physical */ extern "C" std::byte _end_physical; -} // namespace teachos::x86_64::boot +} // namespace teachos::boot::x86_64 #endif diff --git a/arch/x86_64/include/x86_64/cpu/registers.hpp b/arch/x86_64/include/x86_64/cpu/registers.hpp index 607d559..d19acfc 100644 --- a/arch/x86_64/include/x86_64/cpu/registers.hpp +++ b/arch/x86_64/include/x86_64/cpu/registers.hpp @@ -3,7 +3,7 @@ #include -namespace teachos::x86_64::cpu +namespace teachos::cpu::x86_64 { /** @@ -67,6 +67,6 @@ namespace teachos::x86_64::cpu */ auto set_cr0_bit(cr0_flags flag) -> void; -} // namespace teachos::x86_64::cpu +} // namespace teachos::cpu::x86_64 #endif \ No newline at end of file diff --git a/arch/x86_64/include/x86_64/device_io/port_io.hpp b/arch/x86_64/include/x86_64/device_io/port_io.hpp index 4cf0b65..352a4d0 100644 --- a/arch/x86_64/include/x86_64/device_io/port_io.hpp +++ b/arch/x86_64/include/x86_64/device_io/port_io.hpp @@ -5,7 +5,7 @@ #include #include -namespace teachos::x86_64::io +namespace teachos::io::x86_64 { /** * @brief An I/O port of a given size at a given address. @@ -127,6 +127,6 @@ namespace teachos::x86_64::io } }; -} // namespace teachos::x86_64::io +} // namespace teachos::io::x86_64 #endif \ No newline at end of file diff --git a/arch/x86_64/include/x86_64/memory/address.hpp b/arch/x86_64/include/x86_64/memory/address.hpp deleted file mode 100644 index 20e9655..0000000 --- a/arch/x86_64/include/x86_64/memory/address.hpp +++ /dev/null @@ -1,47 +0,0 @@ -#ifndef TEACHOS_X86_64_MEMORY_ADDRESS_HPP -#define TEACHOS_X86_64_MEMORY_ADDRESS_HPP - -#include -#include -#include -#include - -namespace teachos::x86_64::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::x86_64::memory - -#endif \ No newline at end of file diff --git a/arch/x86_64/include/x86_64/memory/frame.hpp b/arch/x86_64/include/x86_64/memory/frame.hpp deleted file mode 100644 index 21565fd..0000000 --- a/arch/x86_64/include/x86_64/memory/frame.hpp +++ /dev/null @@ -1,95 +0,0 @@ -#ifndef TEACHOS_X86_64_MEMORY_FRAME_HPP -#define TEACHOS_X86_64_MEMORY_FRAME_HPP - -#include "x86_64/memory/address.hpp" - -#include -#include - -namespace teachos::x86_64::memory -{ - /** - * @brief Specific physical frame containing helper functions to determine if a specific address is in that - * physical frame or not. - */ - struct frame - { - auto static inline constexpr DEFAULT_SIZE = std::size_t{4096}; ///< Default page size of x86_84 is always 4KiB. - - /** - * @brief Defaulted constructor. - */ - constexpr frame() = default; - - /** - * @brief Constructor. - * - * @param frame_number Index number that should be assigned to this physical frame. - */ - 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() / DEFAULT_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 * DEFAULT_SIZE}; - } - - auto constexpr operator+(std::size_t offset) const noexcept -> frame { return frame{m_number + offset}; } - - /** - * @brief Post increment operator. Returns a copy of the value. - * - * @return Copy of the incremented underlying frame number. - */ - auto constexpr operator++(int) noexcept -> frame - { - auto copy = *this; - return ++copy; - } - - /** - * @brief Pre increment operator. Returns a reference to the changed value. - * - * @return Reference to the incremented underlying frame number. - */ - auto constexpr operator++() noexcept -> frame & - { - ++m_number; - return *this; - } - - /** - * @brief Defaulted equals operator. - */ - auto constexpr operator==(frame const & other) const noexcept -> bool = default; - - /** - * @brief Defaulted three-way comparison operator. - */ - auto constexpr operator<=>(frame const & other) const noexcept -> std::strong_ordering = default; - - private: - std::size_t m_number{}; ///< Index number of the current physical frame, used to distinguish it from other frames. - }; - -} // namespace teachos::x86_64::memory - -#endif // TEACHOS_ARCH_X86_64_MEMORY_ALLOCATOR_frame_HPP diff --git a/arch/x86_64/include/x86_64/memory/mmu.hpp b/arch/x86_64/include/x86_64/memory/mmu.hpp index b03ffa2..323d18a 100644 --- a/arch/x86_64/include/x86_64/memory/mmu.hpp +++ b/arch/x86_64/include/x86_64/memory/mmu.hpp @@ -1,9 +1,9 @@ #ifndef TEACHOS_X86_64_MEMORY_MMU_HPP #define TEACHOS_X86_64_MEMORY_MMU_HPP -#include "x86_64/memory/address.hpp" +#include "kapi/memory/address.hpp" -namespace teachos::x86_64::memory +namespace teachos::memory::x86_64 { /** * @brief Invalidates any translation lookaside buffer (TLB) entry for the page table the given address is cotained @@ -22,6 +22,6 @@ namespace teachos::x86_64::memory */ auto tlb_flush_all() -> void; -} // namespace teachos::x86_64::memory +} // namespace teachos::memory::x86_64 #endif \ No newline at end of file diff --git a/arch/x86_64/include/x86_64/memory/region_allocator.hpp b/arch/x86_64/include/x86_64/memory/region_allocator.hpp index 23bea10..a918195 100644 --- a/arch/x86_64/include/x86_64/memory/region_allocator.hpp +++ b/arch/x86_64/include/x86_64/memory/region_allocator.hpp @@ -1,15 +1,15 @@ #ifndef TEACHOS_X86_64_MEMORY_REGION_ALLOCATOR_HPP #define TEACHOS_X86_64_MEMORY_REGION_ALLOCATOR_HPP -#include "x86_64/memory/address.hpp" -#include "x86_64/memory/frame.hpp" +#include "kapi/memory/address.hpp" +#include "kapi/memory/frame.hpp" #include #include #include -namespace teachos::x86_64::memory +namespace teachos::memory::x86_64 { /** * @brief Allocates memory linearly using memory areas read from the multiboot2 information pointer and leaks any @@ -71,6 +71,6 @@ namespace teachos::x86_64::memory frame const m_multiboot_end; ///< The end address of the multiboot code in memory. }; -} // namespace teachos::x86_64::memory +} // namespace teachos::memory::x86_64 #endif // TEACHOS_ARCH_X86_64_MEMORY_ALLOCATOR_AREA_FRAME_ALLOCATOR_HPP diff --git a/arch/x86_64/include/x86_64/vga/crtc.hpp b/arch/x86_64/include/x86_64/vga/crtc.hpp index 4b4eac5..e5ab9f1 100644 --- a/arch/x86_64/include/x86_64/vga/crtc.hpp +++ b/arch/x86_64/include/x86_64/vga/crtc.hpp @@ -5,17 +5,17 @@ #include -namespace teachos::x86_64::vga::crtc +namespace teachos::vga::x86_64::crtc { /** * @brief The address port of the CRT Controller. */ - using address = x86_64::io::port<0x3d4, 1>; + using address = io::x86_64::port<0x3d4, 1>; /** * @brief The data port of the CRT Controller. */ - using data = x86_64::io::port<0x3d5, 1>; + using data = io::x86_64::port<0x3d5, 1>; namespace registers { @@ -30,6 +30,6 @@ namespace teachos::x86_64::vga::crtc [[maybe_unused]] auto constexpr cursor_end = std::byte{0x0b}; } // namespace registers -} // namespace teachos::x86_64::vga::crtc +} // namespace teachos::vga::x86_64::crtc #endif \ No newline at end of file diff --git a/arch/x86_64/include/x86_64/vga/text.hpp b/arch/x86_64/include/x86_64/vga/text.hpp index f8e6f1b..c193576 100644 --- a/arch/x86_64/include/x86_64/vga/text.hpp +++ b/arch/x86_64/include/x86_64/vga/text.hpp @@ -6,7 +6,7 @@ #include #include -namespace teachos::x86_64::vga::text +namespace teachos::vga::x86_64::text { /** * @brief The colors available in the standard VGA text mode. @@ -142,6 +142,6 @@ namespace teachos::x86_64::vga::text auto writeln(std::string_view code_points, attribute attribute) -> void; }; -} // namespace teachos::x86_64::vga::text +} // namespace teachos::vga::x86_64::text #endif // TEACHOS_ARCH_X86_64_VIDEO_VGA_TEXT_HPP \ No newline at end of file diff --git a/arch/x86_64/src/cpu/registers.cpp b/arch/x86_64/src/cpu/registers.cpp index 7ade98d..8646829 100644 --- a/arch/x86_64/src/cpu/registers.cpp +++ b/arch/x86_64/src/cpu/registers.cpp @@ -2,7 +2,7 @@ #include -namespace teachos::x86_64::cpu +namespace teachos::cpu::x86_64 { auto read_control_register(control_register cr) -> uint64_t { @@ -61,4 +61,4 @@ namespace teachos::x86_64::cpu auto const cr0 = read_control_register(control_register::cr0); write_control_register(control_register::cr0, static_cast::type>(flag) | cr0); } -} // namespace teachos::x86_64::cpu +} // namespace teachos::cpu::x86_64 diff --git a/arch/x86_64/src/kapi/cio.cpp b/arch/x86_64/src/kapi/cio.cpp index ac3ae39..eb0142a 100644 --- a/arch/x86_64/src/kapi/cio.cpp +++ b/arch/x86_64/src/kapi/cio.cpp @@ -5,7 +5,7 @@ namespace teachos::cio { - auto static constinit vga_device = std::optional{}; + auto static constinit vga_device = std::optional{}; auto init() -> void { diff --git a/arch/x86_64/src/kapi/memory.cpp b/arch/x86_64/src/kapi/memory.cpp index d1c1f03..55e6ba9 100644 --- a/arch/x86_64/src/kapi/memory.cpp +++ b/arch/x86_64/src/kapi/memory.cpp @@ -1,10 +1,10 @@ #include "kapi/memory.hpp" +#include "kapi/memory/frame.hpp" #include "kapi/system.hpp" #include "x86_64/boot/boot.hpp" #include "x86_64/boot/ld.hpp" -#include "x86_64/memory/address.hpp" #include "x86_64/memory/region_allocator.hpp" #include @@ -13,8 +13,10 @@ namespace teachos::memory { - using namespace x86_64::memory; - using namespace x86_64::boot; + using namespace boot::x86_64; + using namespace memory::x86_64; + + std::size_t const PLATFORM_FRAME_SIZE{4096}; namespace { diff --git a/arch/x86_64/src/memory/mmu.cpp b/arch/x86_64/src/memory/mmu.cpp index 31783fe..e573b4e 100644 --- a/arch/x86_64/src/memory/mmu.cpp +++ b/arch/x86_64/src/memory/mmu.cpp @@ -2,8 +2,10 @@ #include "x86_64/cpu/registers.hpp" -namespace teachos::x86_64::memory +namespace teachos::memory::x86_64 { + namespace cpu = cpu::x86_64; + auto tlb_flush(linear_address address) -> void { asm volatile("invlpg (%[input])" : /* no output from call */ : [input] "r"(address) : "memory"); @@ -14,4 +16,4 @@ namespace teachos::x86_64::memory auto current_value = cpu::read_control_register(cpu::control_register::cr3); cpu::write_control_register(cpu::control_register::cr3, current_value); } -} // namespace teachos::x86_64::memory +} // namespace teachos::memory::x86_64 diff --git a/arch/x86_64/src/memory/region_allocator.cpp b/arch/x86_64/src/memory/region_allocator.cpp index c9a98b4..91a5d49 100644 --- a/arch/x86_64/src/memory/region_allocator.cpp +++ b/arch/x86_64/src/memory/region_allocator.cpp @@ -1,22 +1,14 @@ -// #include "arch/memory/allocator/region_allocator.hpp" - -// #include "arch/exception_handling/assert.hpp" - -// #include -// #include -// #include - #include "x86_64/memory/region_allocator.hpp" -#include "x86_64/memory/address.hpp" -#include "x86_64/memory/frame.hpp" +#include "kapi/memory/address.hpp" +#include "kapi/memory/frame.hpp" #include #include #include -namespace teachos::x86_64::memory +namespace teachos::memory::x86_64 { namespace { @@ -92,4 +84,4 @@ namespace teachos::x86_64::memory } auto region_allocator::deallocate_frame(frame const &) -> void {} -} // namespace teachos::x86_64::memory +} // namespace teachos::memory::x86_64 diff --git a/arch/x86_64/src/vga/text.cpp b/arch/x86_64/src/vga/text.cpp index af089fd..8f78ea9 100644 --- a/arch/x86_64/src/vga/text.cpp +++ b/arch/x86_64/src/vga/text.cpp @@ -9,8 +9,10 @@ #include #include -namespace teachos::x86_64::vga::text +namespace teachos::vga::x86_64::text { + using boot::x86_64::vga_buffer_pointer; + namespace { auto constinit buffer_offset = std::ptrdiff_t{}; @@ -67,4 +69,4 @@ namespace teachos::x86_64::vga::text newline(); } -} // namespace teachos::x86_64::vga::text +} // namespace teachos::vga::x86_64::text 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 diff --git a/src/main.cpp b/src/main.cpp index 120e7e0..fc7d2f4 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -9,5 +9,5 @@ auto main() -> int teachos::memory::init(); - teachos::system::panic("Architecture specific main returned!"); + teachos::system::panic("Returning from kernel main!"); } -- cgit v1.2.3