From 88bfe18930981d1c7d38cd31f2ea9c4b9f639a72 Mon Sep 17 00:00:00 2001 From: Jonathan Klimt Date: Thu, 22 Feb 2024 12:11:43 +0100 Subject: [PATCH 1/2] Added new function with same definition as the x86_64 types --- src/paging.rs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/paging.rs b/src/paging.rs index c552ef4..aa75801 100644 --- a/src/paging.rs +++ b/src/paging.rs @@ -31,6 +31,11 @@ fn align_up(addr: u64, align: u64) -> u64 { pub struct PhysAddr(pub u64); impl PhysAddr { + #[inline] + pub const fn new(addr: u64) -> Self { + Self(addr) + } + /// Convert to `u64` pub fn as_u64(self) -> u64 { self.0 @@ -287,6 +292,11 @@ impl fmt::Pointer for PhysAddr { pub struct VirtAddr(pub u64); impl VirtAddr { + #[inline] + pub const fn new(addr: u64) -> Self { + Self(addr) + } + /// Convert from `u64` pub const fn from_u64(v: u64) -> Self { VirtAddr(v) From 8bcc80f66d27ea2fa5293cd142a7529936ad5344 Mon Sep 17 00:00:00 2001 From: Jonathan Klimt Date: Thu, 22 Feb 2024 12:14:37 +0100 Subject: [PATCH 2/2] BREAKING CHANGE: fixed arithmetics for VirtAddr & PhysAddr Inspired by x86_64: - Addition of two addresses makes no sense - Difference of two addresses should yield a u64, not an address - Opinionated: Arithmetics with usize shall not exist for addresses to avoid inconsistent behaviour on 32/64-bit systems. Be explicit when using usizes. --- src/paging.rs | 74 +++------------------------------------------------ 1 file changed, 4 insertions(+), 70 deletions(-) diff --git a/src/paging.rs b/src/paging.rs index aa75801..2689499 100644 --- a/src/paging.rs +++ b/src/paging.rs @@ -118,14 +118,6 @@ impl Into for PhysAddr { } } -impl ops::Add for PhysAddr { - type Output = PhysAddr; - - fn add(self, rhs: PhysAddr) -> Self::Output { - PhysAddr(self.0 + rhs.0) - } -} - impl ops::Add for PhysAddr { type Output = PhysAddr; @@ -134,20 +126,6 @@ impl ops::Add for PhysAddr { } } -impl ops::Add for PhysAddr { - type Output = PhysAddr; - - fn add(self, rhs: usize) -> Self::Output { - PhysAddr::from(self.0 + rhs as u64) - } -} - -impl ops::AddAssign for PhysAddr { - fn add_assign(&mut self, other: PhysAddr) { - *self = PhysAddr::from(self.0 + other.0); - } -} - impl ops::AddAssign for PhysAddr { fn add_assign(&mut self, offset: u64) { *self = PhysAddr::from(self.0 + offset); @@ -155,10 +133,10 @@ impl ops::AddAssign for PhysAddr { } impl ops::Sub for PhysAddr { - type Output = PhysAddr; + type Output = u64; fn sub(self, rhs: PhysAddr) -> Self::Output { - PhysAddr::from(self.0 - rhs.0) + self.as_u64().checked_sub(rhs.as_u64()).unwrap() } } @@ -170,14 +148,6 @@ impl ops::Sub for PhysAddr { } } -impl ops::Sub for PhysAddr { - type Output = PhysAddr; - - fn sub(self, rhs: usize) -> Self::Output { - PhysAddr::from(self.0 - rhs as u64) - } -} - impl ops::Rem for PhysAddr { type Output = PhysAddr; @@ -439,14 +409,6 @@ impl Into for VirtAddr { } } -impl ops::Add for VirtAddr { - type Output = VirtAddr; - - fn add(self, rhs: VirtAddr) -> Self::Output { - VirtAddr(self.0 + rhs.0) - } -} - impl ops::Add for VirtAddr { type Output = VirtAddr; @@ -455,37 +417,17 @@ impl ops::Add for VirtAddr { } } -impl ops::Add for VirtAddr { - type Output = VirtAddr; - - fn add(self, rhs: usize) -> Self::Output { - VirtAddr::from(self.0 + rhs as u64) - } -} - -impl ops::AddAssign for VirtAddr { - fn add_assign(&mut self, other: VirtAddr) { - *self = VirtAddr::from(self.0 + other.0); - } -} - impl ops::AddAssign for VirtAddr { fn add_assign(&mut self, offset: u64) { *self = VirtAddr::from(self.0 + offset); } } -impl ops::AddAssign for VirtAddr { - fn add_assign(&mut self, offset: usize) { - *self = VirtAddr::from(self.0 + offset as u64); - } -} - impl ops::Sub for VirtAddr { - type Output = VirtAddr; + type Output = u64; fn sub(self, rhs: VirtAddr) -> Self::Output { - VirtAddr::from(self.0 - rhs.0) + self.as_u64().checked_sub(rhs.as_u64()).unwrap() } } @@ -497,14 +439,6 @@ impl ops::Sub for VirtAddr { } } -impl ops::Sub for VirtAddr { - type Output = VirtAddr; - - fn sub(self, rhs: usize) -> Self::Output { - VirtAddr::from(self.0 - rhs as u64) - } -} - impl ops::Rem for VirtAddr { type Output = VirtAddr;