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