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
|
use core::{ops::Deref, sync::atomic::Ordering};
use atomic::Atomic;
use bytemuck::{Pod, Zeroable};
static KERNEL_VIRTUAL_BASE: Atomic<PhysicalMemoryAddress> = Atomic::new(PhysicalMemoryAddress(0));
pub fn init(kernel_virtual_base: PhysicalMemoryAddress) {
KERNEL_VIRTUAL_BASE.store(kernel_virtual_base, Ordering::Relaxed);
}
#[derive(Clone, Copy, Pod, Zeroable)]
#[repr(transparent)]
pub struct VirtualMemoryAddress(pub usize);
impl Deref for VirtualMemoryAddress {
type Target = usize;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl From<usize> for VirtualMemoryAddress {
fn from(value: usize) -> Self {
Self(value)
}
}
impl From<VirtualMemoryAddress> for usize {
fn from(value: VirtualMemoryAddress) -> Self {
*value
}
}
impl From<PhysicalMemoryAddress> for VirtualMemoryAddress {
fn from(value: PhysicalMemoryAddress) -> Self {
VirtualMemoryAddress(*value + *KERNEL_VIRTUAL_BASE.load(Ordering::Relaxed))
}
}
#[derive(Clone, Copy, Pod, Zeroable)]
#[repr(transparent)]
pub struct PhysicalMemoryAddress(pub usize);
impl Deref for PhysicalMemoryAddress {
type Target = usize;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl From<usize> for PhysicalMemoryAddress {
fn from(value: usize) -> Self {
PhysicalMemoryAddress(value)
}
}
impl From<PhysicalMemoryAddress> for usize {
fn from(value: PhysicalMemoryAddress) -> Self {
*value
}
}
impl From<VirtualMemoryAddress> for PhysicalMemoryAddress {
fn from(value: VirtualMemoryAddress) -> Self {
PhysicalMemoryAddress(*value - *KERNEL_VIRTUAL_BASE.load(Ordering::Relaxed))
}
}
#[derive(Clone, Copy)]
pub enum MemoryAddress {
Virtual(VirtualMemoryAddress),
Physical(PhysicalMemoryAddress),
}
impl From<VirtualMemoryAddress> for MemoryAddress {
fn from(value: VirtualMemoryAddress) -> Self {
MemoryAddress::Virtual(value)
}
}
impl From<PhysicalMemoryAddress> for MemoryAddress {
fn from(value: PhysicalMemoryAddress) -> Self {
MemoryAddress::Physical(value)
}
}
impl From<MemoryAddress> for usize {
fn from(value: MemoryAddress) -> Self {
match value {
MemoryAddress::Virtual(v) => *v,
MemoryAddress::Physical(p) => *p,
}
}
}
impl From<MemoryAddress> for VirtualMemoryAddress {
fn from(value: MemoryAddress) -> Self {
match value {
MemoryAddress::Virtual(v) => v,
MemoryAddress::Physical(p) => p.into(),
}
}
}
impl From<MemoryAddress> for PhysicalMemoryAddress {
fn from(value: MemoryAddress) -> Self {
match value {
MemoryAddress::Virtual(v) => v.into(),
MemoryAddress::Physical(p) => p,
}
}
}
|