diff options
| author | littleblack111 <littleblack11111@gmail.com> | 2026-09-06 13:28:08 +0800 |
|---|---|---|
| committer | littleblack111 <littleblack11111@gmail.com> | 2026-09-06 13:28:08 +0800 |
| commit | 02ea0327f11f6715ae0d34c597cc8095884f2216 (patch) | |
| tree | ee1912fbb97e9df659f86d06b2542c2b3c3a9255 /kernel | |
| parent | 922c9685e9e905a4e0f4acb6ce49e2471485bf8d (diff) | |
mm: fix translation
Diffstat (limited to 'kernel')
| -rw-r--r-- | kernel/src/mm.rs | 33 |
1 files changed, 22 insertions, 11 deletions
diff --git a/kernel/src/mm.rs b/kernel/src/mm.rs index 0a2c5ad..4f9004d 100644 --- a/kernel/src/mm.rs +++ b/kernel/src/mm.rs @@ -1,11 +1,12 @@ -use core::ops::Deref; +use core::{ops::Deref, sync::atomic::Ordering}; use atomic::Atomic; +use bytemuck::{Pod, Zeroable}; -static KERNEL_VIRTUAL_BASE: Atomic<PhysicalMemoryAddress> = - Atomic::new(PhysicalMemoryAddress(0xFFFFFFFF80000000)); +static KERNEL_VIRTUAL_BASE: Atomic<PhysicalMemoryAddress> = Atomic::new(PhysicalMemoryAddress(0)); -#[derive(Clone, Copy)] +#[derive(Clone, Copy, Pod, Zeroable)] +#[repr(transparent)] pub struct VirtualMemoryAddress(pub usize); impl Deref for VirtualMemoryAddress { @@ -18,23 +19,24 @@ impl Deref for VirtualMemoryAddress { impl From<usize> for VirtualMemoryAddress { fn from(value: usize) -> Self { - VirtualMemoryAddress(value) + Self(value) } } impl From<VirtualMemoryAddress> for usize { fn from(value: VirtualMemoryAddress) -> Self { - value.0 + *value } } impl From<PhysicalMemoryAddress> for VirtualMemoryAddress { fn from(value: PhysicalMemoryAddress) -> Self { - VirtualMemoryAddress(value.0) + VirtualMemoryAddress(*value + *KERNEL_VIRTUAL_BASE.load(Ordering::Relaxed)) } } -#[derive(Clone, Copy)] +#[derive(Clone, Copy, Pod, Zeroable)] +#[repr(transparent)] pub struct PhysicalMemoryAddress(pub usize); impl Deref for PhysicalMemoryAddress { @@ -53,13 +55,13 @@ impl From<usize> for PhysicalMemoryAddress { impl From<PhysicalMemoryAddress> for usize { fn from(value: PhysicalMemoryAddress) -> Self { - value.0 + *value } } impl From<VirtualMemoryAddress> for PhysicalMemoryAddress { fn from(value: VirtualMemoryAddress) -> Self { - PhysicalMemoryAddress(value.0) + PhysicalMemoryAddress(*value - *KERNEL_VIRTUAL_BASE.load(Ordering::Relaxed)) } } @@ -94,7 +96,16 @@ impl From<MemoryAddress> for VirtualMemoryAddress { fn from(value: MemoryAddress) -> Self { match value { MemoryAddress::Virtual(v) => v, - MemoryAddress::Physical(p) => VirtualMemoryAddress(p.0), + 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, } } } |
