kernel/lib/addr/
phys_addr.rs1use crate::state::kstate::KSTATE;
7use core::fmt::{Debug, Display, LowerHex, UpperHex};
8use core::ops::{Add, AddAssign, Sub, SubAssign};
9
10#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
12#[repr(transparent)]
13pub struct PhysAddr(u64);
14
15impl PhysAddr {
16 pub const fn new(addr: u64) -> Self {
18 Self(addr)
19 }
20
21 pub const fn null() -> Self {
23 Self(0)
24 }
25
26 pub fn as_u64(self) -> u64 {
28 self.0
29 }
30
31 pub fn as_usize(self) -> usize {
33 self.0 as usize
34 }
35
36 pub fn as_ptr<T>(self) -> *const T {
38 self.0 as *const T
39 }
40
41 pub fn as_mut_ptr<T>(self) -> *mut T {
43 self.0 as *mut T
44 }
45
46 pub fn as_hhdm_ptr<T>(self) -> *const T {
48 (self.0 + KSTATE.mm.hhdm_offset()) as *const T
49 }
50
51 pub fn as_mut_hhdm_ptr<T>(self) -> *mut T {
53 (self.0 + KSTATE.mm.hhdm_offset()) as *mut T
54 }
55
56 pub fn align_up(self, align: u64) -> Self {
61 debug_assert!(align.is_power_of_two());
62 Self((self.0 + (align - 1)) & !(align - 1))
63 }
64
65 pub fn align_down(self, align: u64) -> Self {
70 debug_assert!(align.is_power_of_two());
71 Self(self.0 & !(align - 1))
72 }
73
74 pub fn is_aligned(self, align: u64) -> bool {
76 self.0 & (align - 1) == 0
77 }
78}
79
80#[cfg(target_arch = "x86_64")]
81impl PhysAddr {
82 pub fn as_x86_64(self) -> x86_64::PhysAddr {
84 x86_64::PhysAddr::new(self.0)
85 }
86}
87
88impl Add<u64> for PhysAddr {
89 type Output = Self;
90
91 fn add(self, rhs: u64) -> Self::Output {
93 PhysAddr::new(self.0 + rhs)
94 }
95}
96impl Add<PhysAddr> for PhysAddr {
97 type Output = Self;
98
99 fn add(self, rhs: PhysAddr) -> Self::Output {
101 PhysAddr::new(self.0 + rhs.0)
102 }
103}
104impl AddAssign<u64> for PhysAddr {
105 fn add_assign(&mut self, rhs: u64) {
107 self.0 += rhs
108 }
109}
110impl AddAssign<PhysAddr> for PhysAddr {
111 fn add_assign(&mut self, rhs: PhysAddr) {
113 self.0 += rhs.0
114 }
115}
116
117impl Sub<u64> for PhysAddr {
118 type Output = Self;
119
120 fn sub(self, rhs: u64) -> Self::Output {
122 PhysAddr::new(self.0 - rhs)
123 }
124}
125impl Sub<PhysAddr> for PhysAddr {
126 type Output = Self;
127
128 fn sub(self, rhs: PhysAddr) -> Self::Output {
130 PhysAddr::new(self.0 - rhs.0)
131 }
132}
133impl SubAssign<u64> for PhysAddr {
134 fn sub_assign(&mut self, rhs: u64) {
136 self.0 -= rhs
137 }
138}
139impl SubAssign<PhysAddr> for PhysAddr {
140 fn sub_assign(&mut self, rhs: PhysAddr) {
142 self.0 -= rhs.0
143 }
144}
145
146impl Display for PhysAddr {
147 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
149 write!(f, "{:#x}", self.0)
150 }
151}
152impl Debug for PhysAddr {
153 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
155 write!(f, "PhysAddr({:#x})", self.0)
156 }
157}
158impl LowerHex for PhysAddr {
159 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
161 LowerHex::fmt(&self.0, f)
162 }
163}
164impl UpperHex for PhysAddr {
165 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
167 UpperHex::fmt(&self.0, f)
168 }
169}