sparreal_kernel/mem/
addr.rs

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
196
197
use core::{
    fmt::Display,
    ops::{Add, Sub},
};

#[derive(Clone, Copy)]
#[repr(transparent)]
pub struct Virt<T>(*const T);
unsafe impl<T> Send for Virt<T> {}
unsafe impl<T> Sync for Virt<T> {}

impl<T> From<*const T> for Virt<T> {
    fn from(value: *const T) -> Self {
        Self(value as *const T)
    }
}
impl<T> From<*mut T> for Virt<T> {
    fn from(value: *mut T) -> Self {
        Self(value as *const T)
    }
}
impl<T> From<Virt<T>> for *const T {
    fn from(value: Virt<T>) -> Self {
        value.0 as *const T
    }
}

pub type VirtAddr<T = u8> = Virt<T>;

impl<T> Virt<T> {
    pub const fn new() -> Self {
        Self(0 as *const T)
    }

    pub fn convert_to_phys(self, va_offset: usize) -> Phys<T> {
        Phys::from(self.0 as usize - va_offset)
    }

    pub fn as_mut_ptr(self) -> *mut T {
        self.0 as *mut T
    }

    pub fn as_usize(self) -> usize {
        self.0 as usize
    }
}

impl<T> From<usize> for Virt<T> {
    fn from(value: usize) -> Self {
        Self(value as *const T)
    }
}
impl<T> From<Virt<T>> for usize {
    fn from(value: Virt<T>) -> Self {
        value.0 as usize
    }
}
impl<T> From<Phys<T>> for usize {
    fn from(value: Phys<T>) -> Self {
        value.0 as usize
    }
}
#[derive(Clone, Copy)]
#[repr(transparent)]
pub struct Phys<T>(*const T);

impl<T> PartialEq for Phys<T> {
    fn eq(&self, other: &Self) -> bool {
        (self.0 as *const u8 as usize) == (other.0 as *const u8 as usize)
    }
}

unsafe impl<T> Send for Phys<T> {}
unsafe impl<T> Sync for Phys<T> {}

pub type PhysAddr<T = u8> = Phys<T>;

impl<T> From<usize> for Phys<T> {
    fn from(value: usize) -> Self {
        Self(value as _)
    }
}
impl<T> From<*const T> for Phys<T> {
    fn from(value: *const T) -> Self {
        Self(value as _)
    }
}
impl<T> Phys<T> {
    pub const fn new() -> Self {
        Self(0 as *const T)
    }

    pub fn as_usize(&self) -> usize {
        self.0 as usize
    }
}

impl<T> Sub<Phys<T>> for Phys<T> {
    type Output = usize;

    fn sub(self, rhs: Phys<T>) -> Self::Output {
        self.as_usize() - rhs.as_usize()
    }
}

pub trait Align: Clone + Copy
where
    usize: From<Self>,
{
    fn align_down(self, align: usize) -> Self
    where
        Self: From<usize>,
    {
        align_down(self.into(), align).into()
    }

    fn align_up(self, align: usize) -> Self
    where
        Self: From<usize>,
    {
        align_up(self.into(), align).into()
    }

    fn is_aligned_4k(self) -> bool {
        self.is_aligned_to(0x1000)
    }

    fn is_aligned_to(self, align: usize) -> bool {
        align_offset(self.into(), align) == 0
    }
}

impl<T> Align for T
where
    T: Into<usize> + From<usize> + Copy,
    usize: From<T>,
{
}

impl<T> Add<usize> for Virt<T> {
    type Output = Self;

    fn add(self, rhs: usize) -> Self::Output {
        let lhs: usize = self.into();
        (lhs + rhs).into()
    }
}

pub const fn align_offset(addr: usize, align: usize) -> usize {
    addr & (align - 1)
}

pub const fn align_down(addr: usize, align: usize) -> usize {
    addr & !(align - 1)
}

pub const fn align_up(addr: usize, align: usize) -> usize {
    (addr + align - 1) & !(align - 1)
}

impl<T> Add<usize> for Phys<T> {
    type Output = Self;

    fn add(self, rhs: usize) -> Self::Output {
        (self.as_usize() + rhs).into()
    }
}

impl<T> Sub<usize> for Virt<T> {
    type Output = Self;

    fn sub(self, rhs: usize) -> Self::Output {
        (self.as_usize() - rhs).into()
    }
}

impl<T> Display for Phys<T> {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        write!(f, "PA({:p})", self.0)
    }
}
impl<T> Display for Virt<T> {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        write!(f, "VA({:p})", self.0)
    }
}

impl<T> core::fmt::Debug for Phys<T> {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        write!(f, "PA({:p})", self.0)
    }
}
impl<T> core::fmt::Debug for Virt<T> {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        write!(f, "VA({:p})", self.0)
    }
}