phys.hpp 954 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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 =
  10. Cached ? 0xffffff0000000000ULL : 0xffffff4000000000ULL;
  11. uintptr_t m_ptr;
  12. public:
  13. explicit constexpr physaddr(uintptr_t ptr) : m_ptr{ptr} {}
  14. explicit constexpr physaddr(std::nullptr_t) : m_ptr{} {}
  15. // cast to non-pointer types is prohibited
  16. template <typename U, typename = std::enable_if_t<std::is_pointer_v<U>>>
  17. constexpr U cast_to() const noexcept
  18. {
  19. return std::bit_cast<U>(m_ptr + PHYS_OFFSET);
  20. }
  21. constexpr operator T*() const noexcept
  22. {
  23. return cast_to<T*>();
  24. }
  25. constexpr T* operator->() const noexcept
  26. {
  27. return *this;
  28. }
  29. constexpr uintptr_t phys() const noexcept
  30. {
  31. return m_ptr;
  32. }
  33. };
  34. } // namespace kernel::mem