phys.hpp 928 B

1234567891011121314151617181920212223242526272829303132333435
  1. #pragma once
  2. #include <bit>
  3. #include <cstddef>
  4. #include <stdint.h>
  5. #include <types/types.h>
  6. #include <kernel/mem/types.hpp>
  7. namespace kernel::mem {
  8. template <typename T, bool Cached = true>
  9. class physaddr {
  10. static constexpr uintptr_t PHYS_OFFSET = 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. return std::bit_cast<U>(m_ptr + PHYS_OFFSET);
  19. }
  20. constexpr operator T*() const noexcept { return cast_to<T*>(); }
  21. constexpr T* operator->() const noexcept { return *this; }
  22. constexpr uintptr_t phys() const noexcept { return m_ptr; }
  23. };
  24. } // namespace kernel::mem