phys.hpp 895 B

123456789101112131415161718192021222324252627282930313233
  1. #pragma once
  2. #include <bit>
  3. #include <cstddef>
  4. #include <stdint.h>
  5. #include <types/types.h>
  6. namespace kernel::mem {
  7. template <typename T, bool Cached = true>
  8. class physaddr {
  9. static constexpr uintptr_t PHYS_OFFSET = Cached ? 0xffffff0000000000ULL : 0xffffff4000000000ULL;
  10. uintptr_t m_ptr;
  11. public:
  12. explicit constexpr physaddr(uintptr_t ptr) : m_ptr{ptr} {}
  13. explicit constexpr physaddr(std::nullptr_t) : m_ptr{} {}
  14. // cast to non-pointer types is prohibited
  15. template <typename U, typename = std::enable_if_t<std::is_pointer_v<U>>>
  16. constexpr U cast_to() const noexcept {
  17. return std::bit_cast<U>(m_ptr + PHYS_OFFSET);
  18. }
  19. constexpr operator T*() const noexcept { return cast_to<T*>(); }
  20. constexpr T* operator->() const noexcept { return *this; }
  21. constexpr uintptr_t phys() const noexcept { return m_ptr; }
  22. };
  23. } // namespace kernel::mem