From fdb4f4e8e987a0edbbe88146025b10c693ea0901 Mon Sep 17 00:00:00 2001 From: Mark Poliakov Date: Tue, 12 Oct 2021 23:59:12 +0300 Subject: [PATCH] refactor: remove address crate --- address/Cargo.toml | 9 - address/src/base/mod.rs | 0 address/src/lib.rs | 16 -- address/src/phys.rs | 183 -------------------- address/src/virt.rs | 361 ---------------------------------------- 5 files changed, 569 deletions(-) delete mode 100644 address/Cargo.toml delete mode 100644 address/src/base/mod.rs delete mode 100644 address/src/lib.rs delete mode 100644 address/src/phys.rs delete mode 100644 address/src/virt.rs diff --git a/address/Cargo.toml b/address/Cargo.toml deleted file mode 100644 index 73486c9..0000000 --- a/address/Cargo.toml +++ /dev/null @@ -1,9 +0,0 @@ -[package] -name = "address" -version = "0.1.0" -edition = "2018" - -# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html - -[dependencies] -error = { path = "../error" } diff --git a/address/src/base/mod.rs b/address/src/base/mod.rs deleted file mode 100644 index e69de29..0000000 diff --git a/address/src/lib.rs b/address/src/lib.rs deleted file mode 100644 index d92c555..0000000 --- a/address/src/lib.rs +++ /dev/null @@ -1,16 +0,0 @@ -//! Type-safe wrappers for different address kinds -#![no_std] -#![feature(step_trait, const_fn_trait_bound, const_trait_impl, const_panic)] - -#[cfg(test)] -#[macro_use] -extern crate std; - -#[deny(missing_docs)] -pub mod phys; -pub mod virt; - -trait Address {} - -pub use phys::PhysicalAddress; -pub use virt::{AddressSpace, NoTrivialConvert, TrivialConvert, VirtualAddress}; diff --git a/address/src/phys.rs b/address/src/phys.rs deleted file mode 100644 index 3099d22..0000000 --- a/address/src/phys.rs +++ /dev/null @@ -1,183 +0,0 @@ -//! Module for handling physical memory addresses - -use crate::{AddressSpace, TrivialConvert, VirtualAddress}; -use core::convert::TryFrom; -use core::fmt; -use core::iter::Step; -use core::ops::{Add, AddAssign, Sub, SubAssign}; - -/// Wrapper struct for representing a physical address -#[repr(transparent)] -#[derive(PartialEq, PartialOrd, Copy, Clone)] -pub struct PhysicalAddress(usize); - -// Arithmetic -impl const Add for PhysicalAddress { - type Output = Self; - - #[inline(always)] - fn add(self, rhs: usize) -> Self { - // Will panic on overflow - Self::from(self.0 + rhs) - } -} -impl> AddAssign for PhysicalAddress { - #[inline(always)] - fn add_assign(&mut self, rhs: A) { - // Will panic on overflow - *self = Self::from(self.0 + rhs.into()); - } -} -impl const Sub for PhysicalAddress { - type Output = Self; - - #[inline(always)] - fn sub(self, rhs: usize) -> Self { - Self::from(self.0 - rhs) - } -} -impl SubAssign for PhysicalAddress { - #[inline(always)] - fn sub_assign(&mut self, rhs: usize) { - *self = Self::from(self.0 - rhs); - } -} - -// Construction -impl const From for PhysicalAddress { - fn from(p: usize) -> Self { - Self(p) - } -} - -#[cfg(target_pointer_width = "64")] -impl const From for PhysicalAddress { - fn from(p: u64) -> Self { - Self(p as usize) - } -} - -impl PhysicalAddress { - /// Computes a signed difference between `start` and `end` - /// addresses. Will panic if the result cannot be represented due to - /// overflow. - #[inline(always)] - pub fn diff(start: PhysicalAddress, end: PhysicalAddress) -> isize { - if end >= start { - isize::try_from(end.0 - start.0).expect("Address subtraction overflowed") - } else { - -isize::try_from(start.0 - end.0).expect("Address subtraction overflowed") - } - } - - /// Computes a signed difference between `start` and `end`, does not check for - /// overflow condition. - /// - /// # Safety - /// - /// Does not check for signed integer overflow. - #[inline(always)] - pub unsafe fn diff_unchecked(start: PhysicalAddress, end: PhysicalAddress) -> isize { - end.0 as isize - start.0 as isize - } - - /// Returns `true` if the address is page-aligned - #[inline(always)] - pub const fn is_paligned(self) -> bool { - self.0 & 0xFFF == 0 - } - - /// Returns the index of the 4K page containing the address - #[inline(always)] - pub const fn page_index(self) -> usize { - self.0 >> 12 - } -} - -// Trivial conversion PhysicalAddress -> VirtualAddress -impl const From for VirtualAddress { - fn from(p: PhysicalAddress) -> Self { - VirtualAddress::from(p.0 + T::OFFSET) - } -} - -impl const From for usize { - #[inline(always)] - fn from(p: PhysicalAddress) -> Self { - p.0 as usize - } -} - -#[cfg(target_pointer_width = "64")] -impl const From for u64 { - #[inline(always)] - fn from(p: PhysicalAddress) -> Self { - p.0 as u64 - } -} - -// Formatting -impl fmt::Debug for PhysicalAddress { - #[inline] - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - write!(f, "", self.0) - } -} - -// Step -impl Step for PhysicalAddress { - #[inline] - fn steps_between(_p0: &Self, _p1: &Self) -> Option { - todo!() - } - - #[inline] - fn forward_checked(p: Self, steps: usize) -> Option { - p.0.checked_add(steps).map(Self::from) - } - - #[inline] - fn backward_checked(p: Self, steps: usize) -> Option { - p.0.checked_sub(steps).map(Self::from) - } -} - -#[cfg(test)] -mod tests { - use super::*; - use crate::{AddressSpace, NoTrivialConvert, TrivialConvert, VirtualAddress}; - - #[derive(Copy, Clone, PartialEq, PartialOrd)] - struct S0; - impl AddressSpace for S0 { - const NAME: &'static str = "S0"; - const OFFSET: usize = 0x8000; - const LIMIT: usize = Self::OFFSET + 0x4000; - } - impl TrivialConvert for S0 {} - - #[derive(Copy, Clone, PartialEq, PartialOrd)] - struct S1; - impl AddressSpace for S1 { - const NAME: &'static str = "S1"; - const OFFSET: usize = 0; - const LIMIT: usize = 0; - } - impl NoTrivialConvert for S1 {} - - #[test] - fn test_virt_convert_valid() { - let p0 = PhysicalAddress::from(0x1234usize); - assert_eq!( - VirtualAddress::::from(p0), - VirtualAddress::::from(0x9234usize) - ); - } - - #[test] - #[should_panic] - fn test_virt_convert_invalid() { - let p0 = PhysicalAddress::from(0x4321usize); - let _v = VirtualAddress::::from(p0); - } -} diff --git a/address/src/virt.rs b/address/src/virt.rs deleted file mode 100644 index f6d006f..0000000 --- a/address/src/virt.rs +++ /dev/null @@ -1,361 +0,0 @@ -use super::PhysicalAddress; -use core::convert::TryFrom; -use core::fmt; -use core::iter::Step; -use core::marker::PhantomData; -use core::ops::{Add, AddAssign, Neg, Sub, SubAssign}; - -pub trait AddressSpace: Copy + Clone + PartialEq + PartialOrd { - const NAME: &'static str; - const OFFSET: usize; - const LIMIT: usize; -} - -pub trait NoTrivialConvert {} -pub trait TrivialConvert {} - -#[repr(transparent)] -#[derive(Copy, Clone, PartialOrd, PartialEq)] -pub struct VirtualAddress(usize, PhantomData); - -// Arithmetic -impl Add for VirtualAddress { - type Output = Self; - - #[inline(always)] - fn add(self, rhs: usize) -> Self { - // Will panic on overflow - Self::from(self.0 + rhs) - } -} -impl AddAssign for VirtualAddress { - #[inline(always)] - fn add_assign(&mut self, rhs: usize) { - // Will panic on overflow - *self = Self::from(self.0 + rhs); - } -} -impl Sub for VirtualAddress { - type Output = Self; - - #[inline(always)] - fn sub(self, rhs: usize) -> Self { - // Will panic on underflow - Self::from(self.0 - rhs) - } -} -impl SubAssign for VirtualAddress { - #[inline(always)] - fn sub_assign(&mut self, rhs: usize) { - // Will panic on underflow - *self = Self::from(self.0 - rhs); - } -} - -// Trivial conversion VirtualAddress -> PhysicalAddress -impl From> for PhysicalAddress { - #[inline(always)] - fn from(virt: VirtualAddress) -> Self { - assert!(virt.0 < T::LIMIT); - PhysicalAddress::from(virt.0 - T::OFFSET) - } -} - -// Formatting -impl fmt::Debug for VirtualAddress { - #[inline] - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - write!(f, "<{} {:#018x}>", T::NAME, self.0) - } -} - -impl VirtualAddress { - #[inline(always)] - pub const fn null() -> Self { - Self(0, PhantomData) - } - - pub fn try_subtract(self, p: usize) -> Option { - let (res, overflow) = self.0.overflowing_sub(p); - if overflow || res < T::OFFSET || res >= T::LIMIT { - None - } else { - Some(Self(res, PhantomData)) - } - } - - #[inline] - pub fn diff(start: Self, end: Self) -> isize { - if end >= start { - isize::try_from(end.0 - start.0).expect("Address subtraction overflowed") - } else { - -isize::try_from(start.0 - end.0).expect("Address subtraction overflowed") - } - } - - #[inline(always)] - pub fn try_diff(start: Self, end: Self) -> Option { - if end >= start { - isize::try_from(end.0 - start.0).ok() - } else { - isize::try_from(start.0 - end.0).map(Neg::neg).ok() - } - } - - #[inline(always)] - pub unsafe fn as_slice_mut(self, count: usize) -> &'static mut [U] { - core::slice::from_raw_parts_mut(self.0 as *mut _, count) - } - - #[inline(always)] - pub fn as_mut_ptr(self) -> *mut U { - self.0 as *mut U - } - - #[inline(always)] - pub fn as_ptr(self) -> *const U { - self.0 as *const U - } - - #[inline(always)] - pub unsafe fn as_mut(self) -> Option<&'static mut U> { - (self.0 as *mut U).as_mut() - } - - #[inline(always)] - pub unsafe fn from_ptr(r: *const U) -> Self { - Self::from(r as usize) - } - - #[inline(always)] - pub unsafe fn from_ref(r: &U) -> Self { - Self(r as *const U as usize, PhantomData) - } -} - -// Step -impl Step for VirtualAddress { - #[inline] - fn steps_between(_p0: &Self, _p1: &Self) -> Option { - todo!() - } - - #[inline] - fn forward_checked(p: Self, steps: usize) -> Option { - p.0.checked_add(steps).map(Self::from) - } - - #[inline] - fn backward_checked(p: Self, steps: usize) -> Option { - p.0.checked_sub(steps).map(Self::from) - } -} - -// Conversion into VirtualAddress -impl const From for VirtualAddress { - #[inline(always)] - fn from(p: usize) -> Self { - if T::LIMIT > 0 { - assert!(p >= T::OFFSET && p < T::LIMIT); - } - Self(p, PhantomData) - } -} - -#[cfg(target_pointer_width = "64")] -impl From for VirtualAddress { - #[inline(always)] - fn from(p: u64) -> Self { - Self::from(p as usize) - } -} - -// Conversion from VirtualAddress -impl From> for usize { - #[inline(always)] - fn from(p: VirtualAddress) -> Self { - p.0 - } -} - -#[cfg(target_pointer_width = "64")] -impl From> for u64 { - #[inline(always)] - fn from(p: VirtualAddress) -> Self { - p.0 as u64 - } -} - -#[cfg(test)] -mod tests { - use super::*; - use crate::PhysicalAddress; - - #[derive(Copy, Clone, PartialEq, PartialOrd)] - struct S0; - impl AddressSpace for S0 { - const NAME: &'static str = "S0"; - const OFFSET: usize = 0x8000; - const LIMIT: usize = Self::OFFSET + 0x4000; - } - impl TrivialConvert for S0 {} - - #[derive(Copy, Clone, PartialEq, PartialOrd)] - struct S1; - impl AddressSpace for S1 { - const NAME: &'static str = "S1"; - const OFFSET: usize = 0; - const LIMIT: usize = 0; - } - impl NoTrivialConvert for S1 {} - - #[test] - fn test_trivial_construct_valid() { - for i in 0x8000usize..0xC000 { - VirtualAddress::::from(i); - } - } - - #[test] - #[should_panic] - fn test_trivial_construct_invalid_0() { - let _v = VirtualAddress::::from(0x1234usize); - } - - #[test] - #[should_panic] - fn test_trivial_construct_invalid_1() { - let _v = VirtualAddress::::from(0xD123usize); - } - - #[test] - fn test_trivial_convert() { - let v0 = VirtualAddress::::from(0x8123usize); - assert_eq!(PhysicalAddress::from(v0), PhysicalAddress::from(0x123usize)); - } - - #[test] - fn test_add_valid() { - let v0 = VirtualAddress::::from(0x8100usize); - assert_eq!(VirtualAddress::::from(0x8223usize), v0 + 0x123usize); - } - - #[test] - #[should_panic] - fn test_add_overflow() { - let v0 = VirtualAddress::::from(0x8100usize); - let _v = v0 - 0xF123usize; - } - - #[test] - fn test_subtract_valid() { - let v0 = VirtualAddress::::from(0x8100usize); - assert_eq!(VirtualAddress::::from(0x8023usize), v0 - 0xDDusize); - } - - #[test] - #[should_panic] - fn test_subtract_overflow() { - let v0 = VirtualAddress::::from(0x8100usize); - let _v = v0 - 0x1234usize; - } - - #[test] - fn test_try_subtract() { - let v0 = VirtualAddress::::from(0x8100usize); - assert_eq!(v0.try_subtract(0x1234usize), None); - assert_eq!( - v0.try_subtract(0x12usize), - Some(VirtualAddress::::from(0x80EEusize)) - ); - } - - #[test] - fn test_add_assign_valid() { - let mut v0 = VirtualAddress::::from(0x8100usize); - v0 += 0x123usize; - assert_eq!(v0, VirtualAddress::::from(0x8223usize)); - } - - #[test] - fn test_sub_assign_valid() { - let mut v0 = VirtualAddress::::from(0x8321usize); - v0 -= 0x123usize; - assert_eq!(v0, VirtualAddress::::from(0x81FEusize)); - } - - #[test] - #[should_panic] - fn test_sub_assign_overflow() { - let mut v0 = VirtualAddress::::from(0x8321usize); - v0 -= 0x1234usize; - } - - #[test] - #[should_panic] - fn test_add_assign_overflow() { - let mut v0 = VirtualAddress::::from(0x8321usize); - v0 += 0xF234usize; - } - - #[test] - fn test_format() { - let v0 = VirtualAddress::::from(0x8123usize); - assert_eq!(&format!("{:?}", v0), ""); - } - - #[test] - fn test_diff() { - let v0 = VirtualAddress::::from(0x8123usize); - let v1 = VirtualAddress::::from(0x8321usize); - - // Ok - assert_eq!(VirtualAddress::diff(v0, v1), 510); - assert_eq!(VirtualAddress::diff(v1, v0), -510); - assert_eq!(VirtualAddress::diff(v0, v0), 0); - assert_eq!(VirtualAddress::diff(v1, v1), 0); - } - - #[test] - #[should_panic] - fn test_diff_overflow() { - let v0 = VirtualAddress::::from(0usize); - let v1 = VirtualAddress::::from(usize::MAX); - - let _v = VirtualAddress::diff(v0, v1); - } - - #[test] - fn test_step() { - let mut count = 0; - for _ in VirtualAddress::::from(0x8000usize)..VirtualAddress::::from(0x8300usize) { - count += 1; - } - assert_eq!(count, 0x300); - - let mut count = 0; - for _ in (VirtualAddress::::from(0x8000usize)..VirtualAddress::::from(0x8300usize)) - .step_by(0x100) - { - count += 1; - } - assert_eq!(count, 3); - - let mut count = 0; - for _ in - (VirtualAddress::::from(0x8000usize)..VirtualAddress::::from(0x8300usize)).rev() - { - count += 1; - } - assert_eq!(count, 0x300); - - let mut count = 0; - for _ in (VirtualAddress::::from(0x8000usize)..VirtualAddress::::from(0x8300usize)) - .rev() - .step_by(0x100) - { - count += 1; - } - assert_eq!(count, 3); - } -}