1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
use cfg_if::cfg_if;
use core::{
    fmt,
    ops::{Add, AddAssign, Sub, SubAssign},
};

/// Represents a physical address. If the target architecture has any requirements for valid physical addresses,
/// they are always enforced.
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Default)]
#[repr(transparent)]
pub struct PAddr(usize);

impl PAddr {
    cfg_if! {
        if #[cfg(target_arch = "x86_64")] {
            /// Construct a new `PAddr`. On x86_64, physical addresses must be less than `2^52`; if this is
            /// not the case, this will return `None`.
            pub const fn new(address: usize) -> Option<PAddr> {
                const MAX_PHYSICAL_ADDRESS: usize = (1 << 52) - 1;
                match address {
                    0..=MAX_PHYSICAL_ADDRESS => Some(PAddr(address)),
                    _ => None
                }
            }
        } else {
            /// Construct a new `PAddr`. The target architecture does not have any requirements on valid
            /// physical addresses, so this always succeeds.
            pub const fn new(address: usize) -> Option<PAddr> {
                Some(PAddr(address))
            }
        }
    }

    /// Align this address to the given alignment, moving downwards if this is not already aligned.
    /// `align` must be `0` or a power-of-two.
    pub fn align_down(self, align: usize) -> PAddr {
        if align.is_power_of_two() {
            /*
             * E.g.
             *      align       =   0b00001000
             *      align-1     =   0b00000111
             *      !(align-1)  =   0b11111000
             *                             ^^^ Masks the address to the value below it with the
             *                                 correct alignment
             */
            PAddr(self.0 & !(align - 1))
        } else {
            assert!(align == 0);
            self
        }
    }

    pub fn align_up(self, align: usize) -> PAddr {
        PAddr(self.0 + align - 1).align_down(align)
    }

    pub fn is_aligned(self, align: usize) -> bool {
        self.0 % align == 0
    }

    pub fn checked_add(self, rhs: usize) -> Option<Self> {
        PAddr::new(self.0.checked_add(rhs)?)
    }

    pub fn checked_sub(self, rhs: usize) -> Option<Self> {
        PAddr::new(self.0.checked_sub(rhs)?)
    }
}

impl fmt::LowerHex for PAddr {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{:#x}", self.0)
    }
}

impl fmt::UpperHex for PAddr {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{:#X}", self.0)
    }
}

impl fmt::Debug for PAddr {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "PAddr({:#x})", self)
    }
}

impl From<PAddr> for usize {
    fn from(address: PAddr) -> usize {
        address.0
    }
}

impl Add<usize> for PAddr {
    type Output = PAddr;

    fn add(self, rhs: usize) -> Self::Output {
        match PAddr::new(self.0 + rhs) {
            Some(address) => address,
            None => panic!("Physical address arithmetic led to invalid address: {:#x} + {:#x}", self, rhs),
        }
    }
}

impl AddAssign<usize> for PAddr {
    fn add_assign(&mut self, rhs: usize) {
        // XXX: this ensures correctness as it goes through the `Add` implementation
        *self = *self + rhs;
    }
}

impl Sub<usize> for PAddr {
    type Output = PAddr;

    fn sub(self, rhs: usize) -> Self::Output {
        match PAddr::new(self.0 - rhs) {
            Some(address) => address,
            None => panic!("Physical address arithmetic led to invalid address: {:#x} - {:#x}", self, rhs),
        }
    }
}

impl SubAssign<usize> for PAddr {
    fn sub_assign(&mut self, rhs: usize) {
        // XXX: this ensures correctness as it goes through the `Sub` implementation
        *self = *self - rhs;
    }
}