#include "arch/memory/paging.hpp" #include "arch/exception_handling/assert.hpp" namespace teachos::arch::memory { auto entry::is_unused() const -> bool { return flags == 0U; } auto entry::set_unused() -> void { flags = 0U; } auto entry::calculate_pointed_to_frame() const -> std::optional { if (contains_flags(PRESENT)) { auto physical_address = calculate_physical_address(); return physical_frame::containing_address(physical_address); } return std::nullopt; } auto entry::calculate_physical_address() const -> std::size_t { constexpr std::size_t start_bit = 12U; constexpr std::size_t end_bit = 52U; size_t value = 0U; for (auto i = start_bit; i < end_bit; i++) { value |= (flags[i] ? (1 << (i - start_bit)) : 0); } return value; } auto entry::contains_flags(std::bitset<64U> b) const -> bool { return (flags & b) == b; } auto entry::set_address(physical_frame frame) -> void { arch::exception_handling::assert((frame.start_address() & ~0x000fffff'fffff000) == 0, "Start address is not aligned with Page"); flags = std::bitset<64U>(frame.start_address()) | flags; } auto page_table::zero_entries() -> void { auto begin = &entries[0]; auto end = &entries[PAGE_TABLE_ENTRY_COUNT]; for (auto entry = begin; entry < end; ++entry) { entry->set_unused(); } } } // namespace teachos::arch::memory