phys.hpp 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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 =
  11. Cached ? 0xffffff0000000000ULL : 0xffffff4000000000ULL;
  12. uintptr_t m_ptr;
  13. public:
  14. explicit constexpr physaddr(uintptr_t ptr) : m_ptr{ptr} {}
  15. explicit constexpr physaddr(std::nullptr_t) : m_ptr{} {}
  16. // cast to non-pointer types is prohibited
  17. template <typename U, typename = std::enable_if_t<std::is_pointer_v<U>>>
  18. constexpr U cast_to() const noexcept
  19. {
  20. return std::bit_cast<U>(m_ptr + PHYS_OFFSET);
  21. }
  22. constexpr operator T*() const noexcept
  23. {
  24. return cast_to<T*>();
  25. }
  26. constexpr T* operator->() const noexcept
  27. {
  28. return *this;
  29. }
  30. constexpr uintptr_t phys() const noexcept
  31. {
  32. return m_ptr;
  33. }
  34. };
  35. // gdt[0]: null
  36. // gdt[1]: kernel code
  37. // gdt[2]: kernel data
  38. // gdt[3]: user code
  39. // gdt[4]: user data
  40. // gdt[5]: user code compability mode
  41. // gdt[6]: user data compability mode
  42. // gdt[7]: reserved
  43. // gdt[8]: tss descriptor low
  44. // gdt[9]: tss descriptor high
  45. // gdt[10]: ldt descriptor low
  46. // gdt[11]: ldt descriptor high
  47. // gdt[12]: thread local(in ldt)
  48. // gdt[13]: thread local(in ldt)
  49. // &gdt[14]: tss of 0x68 bytes from here
  50. constexpr physaddr<uint64_t> gdt{0x00000000 + 1 - 1};
  51. } // namespace kernel::mem