diff options
| author | littleblack111 <littleblack11111@gmail.com> | 2026-09-06 11:49:20 +0800 |
|---|---|---|
| committer | littleblack111 <littleblack11111@gmail.com> | 2026-09-06 11:49:20 +0800 |
| commit | 922c9685e9e905a4e0f4acb6ce49e2471485bf8d (patch) | |
| tree | 8a5fae77d8b0d97a5c88336656a5a0573ec9c5c7 /kernel/src/mm.rs | |
| parent | 365b581ca5c37305f9583cf08d0c25c4426f47e2 (diff) | |
init: mm
just some ground work and newtype pattern
Diffstat (limited to 'kernel/src/mm.rs')
| -rw-r--r-- | kernel/src/mm.rs | 100 |
1 files changed, 100 insertions, 0 deletions
diff --git a/kernel/src/mm.rs b/kernel/src/mm.rs new file mode 100644 index 0000000..0a2c5ad --- /dev/null +++ b/kernel/src/mm.rs @@ -0,0 +1,100 @@ +use core::ops::Deref; + +use atomic::Atomic; + +static KERNEL_VIRTUAL_BASE: Atomic<PhysicalMemoryAddress> = + Atomic::new(PhysicalMemoryAddress(0xFFFFFFFF80000000)); + +#[derive(Clone, Copy)] +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 { + VirtualMemoryAddress(value) + } +} + +impl From<VirtualMemoryAddress> for usize { + fn from(value: VirtualMemoryAddress) -> Self { + value.0 + } +} + +impl From<PhysicalMemoryAddress> for VirtualMemoryAddress { + fn from(value: PhysicalMemoryAddress) -> Self { + VirtualMemoryAddress(value.0) + } +} + +#[derive(Clone, Copy)] +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.0 + } +} + +impl From<VirtualMemoryAddress> for PhysicalMemoryAddress { + fn from(value: VirtualMemoryAddress) -> Self { + PhysicalMemoryAddress(value.0) + } +} + +#[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) => VirtualMemoryAddress(p.0), + } + } +} |
