PhysicalAddress.h 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/Checked.h>
  8. #include <AK/Format.h>
  9. #include <AK/Types.h>
  10. typedef u64 PhysicalPtr;
  11. typedef u64 PhysicalSize;
  12. class PhysicalAddress {
  13. public:
  14. ALWAYS_INLINE static PhysicalPtr physical_page_base(PhysicalPtr page_address) { return page_address & ~(PhysicalPtr)0xfff; }
  15. ALWAYS_INLINE static size_t physical_page_index(PhysicalPtr page_address)
  16. {
  17. auto page_index = page_address >> 12;
  18. if constexpr (sizeof(size_t) < sizeof(PhysicalPtr))
  19. VERIFY(!(page_index & ~(PhysicalPtr)((size_t)-1)));
  20. return (size_t)(page_index);
  21. }
  22. PhysicalAddress() = default;
  23. explicit PhysicalAddress(PhysicalPtr address)
  24. : m_address(address)
  25. {
  26. }
  27. [[nodiscard]] PhysicalAddress offset(PhysicalPtr o) const { return PhysicalAddress(m_address + o); }
  28. [[nodiscard]] bool offset_addition_would_overflow(PhysicalPtr o) const { return Checked<PhysicalPtr>::addition_would_overflow(m_address, o); }
  29. [[nodiscard]] PhysicalPtr get() const { return m_address; }
  30. void set(PhysicalPtr address) { m_address = address; }
  31. void mask(PhysicalPtr m) { m_address &= m; }
  32. [[nodiscard]] bool is_null() const { return m_address == 0; }
  33. // NOLINTNEXTLINE(readability-make-member-function-const) const PhysicalAddress shouldn't be allowed to modify the underlying memory
  34. [[nodiscard]] u8* as_ptr() { return reinterpret_cast<u8*>(m_address); }
  35. [[nodiscard]] const u8* as_ptr() const { return reinterpret_cast<u8 const*>(m_address); }
  36. [[nodiscard]] PhysicalAddress page_base() const { return PhysicalAddress(physical_page_base(m_address)); }
  37. [[nodiscard]] PhysicalPtr offset_in_page() const { return PhysicalAddress(m_address & 0xfff).get(); }
  38. bool operator==(const PhysicalAddress& other) const { return m_address == other.m_address; }
  39. bool operator!=(const PhysicalAddress& other) const { return m_address != other.m_address; }
  40. bool operator>(const PhysicalAddress& other) const { return m_address > other.m_address; }
  41. bool operator>=(const PhysicalAddress& other) const { return m_address >= other.m_address; }
  42. bool operator<(const PhysicalAddress& other) const { return m_address < other.m_address; }
  43. bool operator<=(const PhysicalAddress& other) const { return m_address <= other.m_address; }
  44. private:
  45. PhysicalPtr m_address { 0 };
  46. };
  47. template<>
  48. struct AK::Formatter<PhysicalAddress> : AK::Formatter<FormatString> {
  49. ErrorOr<void> format(FormatBuilder& builder, PhysicalAddress value)
  50. {
  51. if constexpr (sizeof(PhysicalPtr) == sizeof(u64))
  52. return AK::Formatter<FormatString>::format(builder, "P{:016x}", value.get());
  53. else
  54. return AK::Formatter<FormatString>::format(builder, "P{}", value.as_ptr());
  55. }
  56. };