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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
const VA_TO_PA_OFF: usize = 0xffff000000000000 | (510 << 39);
pub const PAGE_SIZE: usize = 0x1000;
pub const PAGE_SHIFT: usize = 12;
pub const PAGE_MASK: usize = 0xfff;
#[repr(transparent)]
#[derive(Clone, Copy, Eq, PartialEq, PartialOrd, Ord)]
pub struct Pa(usize);
impl Pa {
pub const ZERO: Self = Self(0);
#[inline]
pub const fn new(addr: usize) -> Option<Self> {
if addr < 0xffff_0000_0000_0000 {
Some(Self(addr))
} else {
None
}
}
#[inline]
pub const unsafe fn into_usize(self) -> usize {
self.0
}
#[inline]
pub const fn into_va(self) -> Va {
Va(self.0 + VA_TO_PA_OFF)
}
}
#[repr(transparent)]
#[derive(Clone, Copy, Eq, PartialEq, PartialOrd, Ord)]
pub struct Va(usize);
impl Va {
#[inline(always)]
pub const fn new(addr: usize) -> Option<Self> {
match addr & 0xffff_8000_0000_0000 {
m if m == 0xffff_8000_0000_0000 || m == 0 => Some(Self(addr)),
_ => None,
}
}
#[inline]
pub const unsafe fn into_usize(self) -> usize {
self.0
}
#[inline]
pub const fn into_pa(self) -> Pa {
Pa(self.0 - VA_TO_PA_OFF)
}
#[inline]
pub unsafe fn as_ref<'a, T>(&self) -> Option<&'a T> {
(self.into_usize() as *const T).as_ref()
}
#[inline]
pub unsafe fn as_mut<'a, T>(&self) -> Option<&'a mut T> {
(self.into_usize() as *mut T).as_mut()
}
}
macro_rules! impl_arith {
($t: ty) => {
impl core::ops::Add<usize> for $t {
type Output = Self;
fn add(self, other: usize) -> Self::Output {
Self(self.0 + other)
}
}
impl core::ops::AddAssign<usize> for $t {
fn add_assign(&mut self, other: usize) {
self.0 = self.0 + other
}
}
impl core::ops::Sub<usize> for $t {
type Output = Self;
fn sub(self, other: usize) -> Self::Output {
Self(self.0 - other)
}
}
impl core::ops::SubAssign<usize> for $t {
fn sub_assign(&mut self, other: usize) {
self.0 = self.0 - other
}
}
impl core::ops::BitOr<usize> for $t {
type Output = Self;
fn bitor(self, other: usize) -> Self {
Self(self.0 | other)
}
}
impl core::ops::BitOrAssign<usize> for $t {
fn bitor_assign(&mut self, other: usize) {
self.0 = self.0 | other;
}
}
impl core::ops::BitAnd<usize> for $t {
type Output = Self;
fn bitand(self, other: usize) -> Self {
Self(self.0 & other)
}
}
impl core::ops::BitAndAssign<usize> for $t {
fn bitand_assign(&mut self, other: usize) {
self.0 = self.0 & other;
}
}
};
}
impl_arith!(Va);
impl_arith!(Pa);
impl core::fmt::Debug for Va {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(f, "Va(0x{:x})", self.0)
}
}
impl core::fmt::Display for Va {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(f, "Va(0x{:x})", self.0)
}
}
impl core::fmt::Debug for Pa {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(f, "Pa(0x{:x})", self.0)
}
}
impl core::fmt::Display for Pa {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(f, "Pa(0x{:x})", self.0)
}
}