summaryrefslogtreecommitdiff
path: root/kernel/src/mm.rs
diff options
context:
space:
mode:
Diffstat (limited to 'kernel/src/mm.rs')
-rw-r--r--kernel/src/mm.rs100
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),
+ }
+ }
+}