From 017702d3d27e81177ecfb377ee7296c25161f590 Mon Sep 17 00:00:00 2001 From: bjorn3 <17426603+bjorn3@users.noreply.github.com> Date: Tue, 3 Oct 2023 13:15:54 +0200 Subject: [PATCH] Remove a lot of dead code --- src/arch/aarch64/consts.rs | 3 -- src/arch/x86/consts.rs | 3 -- src/arch/x86/paging/entry.rs | 80 --------------------------------- src/arch/x86/paging/mod.rs | 11 ++++- src/arch/x86_64/consts.rs | 3 -- src/arch/x86_64/paging/entry.rs | 80 --------------------------------- src/arch/x86_64/paging/mod.rs | 14 ++++-- src/context/context.rs | 77 ++----------------------------- src/context/memory.rs | 25 ++--------- src/context/mod.rs | 2 +- src/elf.rs | 24 ---------- src/memory/mod.rs | 12 ----- src/scheme/mod.rs | 4 -- src/sync/wait_queue.rs | 56 ----------------------- 14 files changed, 27 insertions(+), 367 deletions(-) delete mode 100644 src/arch/x86/paging/entry.rs delete mode 100644 src/arch/x86_64/paging/entry.rs diff --git a/src/arch/aarch64/consts.rs b/src/arch/aarch64/consts.rs index 22ad2f0a59..ec27a3fbc8 100644 --- a/src/arch/aarch64/consts.rs +++ b/src/arch/aarch64/consts.rs @@ -35,8 +35,5 @@ pub const KERNEL_PERCPU_SIZE: usize = 1_usize << KERNEL_PERCPU_SHIFT; pub const PHYS_OFFSET: usize = 0xFFFF_8000_0000_0000; pub const PHYS_PML4: usize = (PHYS_OFFSET & PML4_MASK)/PML4_SIZE; -/// Offset to user image -pub const USER_OFFSET: usize = 0; - /// End offset of the user image, i.e. kernel start pub const USER_END_OFFSET: usize = 256 * PML4_SIZE; diff --git a/src/arch/x86/consts.rs b/src/arch/x86/consts.rs index bcd6df173a..7c5f92583d 100644 --- a/src/arch/x86/consts.rs +++ b/src/arch/x86/consts.rs @@ -28,8 +28,5 @@ // This needs to match RMM's PHYS_OFFSET pub const PHYS_OFFSET: usize = 0x8000_0000; - /// Offset to user image - pub const USER_OFFSET: usize = 0; - /// End offset of the user image, i.e. kernel start pub const USER_END_OFFSET: usize = 0x8000_0000; diff --git a/src/arch/x86/paging/entry.rs b/src/arch/x86/paging/entry.rs deleted file mode 100644 index 4092ab18b2..0000000000 --- a/src/arch/x86/paging/entry.rs +++ /dev/null @@ -1,80 +0,0 @@ -//! # Page table entry -//! Some code borrowed from [Phil Opp's Blog](http://os.phil-opp.com/modifying-page-tables.html) - -use crate::memory::Frame; - -use super::{PageFlags, PhysicalAddress, RmmA, RmmArch}; - -/// A page table entry -#[repr(packed(8))] -pub struct Entry(u64); - -bitflags! { - pub struct EntryFlags: usize { - const NO_CACHE = 1 << 4; - const HUGE_PAGE = 1 << 7; - const GLOBAL = 1 << 8; - } -} - -pub const COUNTER_MASK: u64 = 0x3ff0_0000_0000_0000; - -impl Entry { - /// Clear entry - pub fn set_zero(&mut self) { - self.0 = 0; - } - - /// Is the entry unused? - pub fn is_unused(&self) -> bool { - self.0 == (self.0 & COUNTER_MASK) - } - - /// Make the entry unused - pub fn set_unused(&mut self) { - self.0 &= COUNTER_MASK; - } - - /// Get the address this page references - pub fn address(&self) -> PhysicalAddress { - PhysicalAddress::new(self.0 as usize & RmmA::PAGE_ADDRESS_MASK) - } - - /// Get the current entry flags - pub fn flags(&self) -> PageFlags { - unsafe { PageFlags::from_data((self.0 as usize & RmmA::ENTRY_FLAGS_MASK) & !(COUNTER_MASK as usize)) } - } - - /// Get the associated frame, if available - pub fn pointed_frame(&self) -> Option { - if self.flags().has_present() { - Some(Frame::containing_address(self.address())) - } else { - None - } - } - - pub fn set(&mut self, frame: Frame, flags: PageFlags) { - debug_assert!(frame.start_address().data() & !RmmA::PAGE_ADDRESS_MASK == 0); - self.0 = (frame.start_address().data() as u64) | (flags.data() as u64) | (self.0 & COUNTER_MASK); - } - - /// Get bits 52-61 in entry, used as counter for page table - pub fn counter_bits(&self) -> u64 { - (self.0 & COUNTER_MASK) >> 52 - } - - /// Set bits 52-61 in entry, used as counter for page table - pub fn set_counter_bits(&mut self, count: u64) { - self.0 = (self.0 & !COUNTER_MASK) | (count << 52); - } -} - -#[cfg(test)] -mod tests { - #[test] - fn entry_has_required_arch_alignment() { - use super::Entry; - assert!(core::mem::align_of::() >= core::mem::align_of::(), "alignment of Entry is less than the required alignment of u64 ({} < {})", core::mem::align_of::(), core::mem::align_of::()); - } -} diff --git a/src/arch/x86/paging/mod.rs b/src/arch/x86/paging/mod.rs index 97a66d20e3..59de9519de 100644 --- a/src/arch/x86/paging/mod.rs +++ b/src/arch/x86/paging/mod.rs @@ -19,7 +19,16 @@ pub use super::CurrentRmmArch as RmmA; pub type PageMapper = rmm::PageMapper; pub use crate::rmm::KernelMapper; -pub mod entry; +pub mod entry { + bitflags! { + pub struct EntryFlags: usize { + const NO_CACHE = 1 << 4; + const HUGE_PAGE = 1 << 7; + const GLOBAL = 1 << 8; + } + } +} + pub mod mapper; /// Number of entries per page table diff --git a/src/arch/x86_64/consts.rs b/src/arch/x86_64/consts.rs index 7b49548b3f..760f883c7d 100644 --- a/src/arch/x86_64/consts.rs +++ b/src/arch/x86_64/consts.rs @@ -28,8 +28,5 @@ pub const KERNEL_HEAP_SIZE: usize = 1 * 1024 * 1024; // 1 MB pub const PHYS_OFFSET: usize = 0xFFFF_8000_0000_0000; pub const PHYS_PML4: usize = (PHYS_OFFSET & PML4_MASK)/PML4_SIZE; -/// Offset to user image -pub const USER_OFFSET: usize = 0; - /// End offset of the user image, i.e. kernel start pub const USER_END_OFFSET: usize = 256 * PML4_SIZE; diff --git a/src/arch/x86_64/paging/entry.rs b/src/arch/x86_64/paging/entry.rs deleted file mode 100644 index 4092ab18b2..0000000000 --- a/src/arch/x86_64/paging/entry.rs +++ /dev/null @@ -1,80 +0,0 @@ -//! # Page table entry -//! Some code borrowed from [Phil Opp's Blog](http://os.phil-opp.com/modifying-page-tables.html) - -use crate::memory::Frame; - -use super::{PageFlags, PhysicalAddress, RmmA, RmmArch}; - -/// A page table entry -#[repr(packed(8))] -pub struct Entry(u64); - -bitflags! { - pub struct EntryFlags: usize { - const NO_CACHE = 1 << 4; - const HUGE_PAGE = 1 << 7; - const GLOBAL = 1 << 8; - } -} - -pub const COUNTER_MASK: u64 = 0x3ff0_0000_0000_0000; - -impl Entry { - /// Clear entry - pub fn set_zero(&mut self) { - self.0 = 0; - } - - /// Is the entry unused? - pub fn is_unused(&self) -> bool { - self.0 == (self.0 & COUNTER_MASK) - } - - /// Make the entry unused - pub fn set_unused(&mut self) { - self.0 &= COUNTER_MASK; - } - - /// Get the address this page references - pub fn address(&self) -> PhysicalAddress { - PhysicalAddress::new(self.0 as usize & RmmA::PAGE_ADDRESS_MASK) - } - - /// Get the current entry flags - pub fn flags(&self) -> PageFlags { - unsafe { PageFlags::from_data((self.0 as usize & RmmA::ENTRY_FLAGS_MASK) & !(COUNTER_MASK as usize)) } - } - - /// Get the associated frame, if available - pub fn pointed_frame(&self) -> Option { - if self.flags().has_present() { - Some(Frame::containing_address(self.address())) - } else { - None - } - } - - pub fn set(&mut self, frame: Frame, flags: PageFlags) { - debug_assert!(frame.start_address().data() & !RmmA::PAGE_ADDRESS_MASK == 0); - self.0 = (frame.start_address().data() as u64) | (flags.data() as u64) | (self.0 & COUNTER_MASK); - } - - /// Get bits 52-61 in entry, used as counter for page table - pub fn counter_bits(&self) -> u64 { - (self.0 & COUNTER_MASK) >> 52 - } - - /// Set bits 52-61 in entry, used as counter for page table - pub fn set_counter_bits(&mut self, count: u64) { - self.0 = (self.0 & !COUNTER_MASK) | (count << 52); - } -} - -#[cfg(test)] -mod tests { - #[test] - fn entry_has_required_arch_alignment() { - use super::Entry; - assert!(core::mem::align_of::() >= core::mem::align_of::(), "alignment of Entry is less than the required alignment of u64 ({} < {})", core::mem::align_of::(), core::mem::align_of::()); - } -} diff --git a/src/arch/x86_64/paging/mod.rs b/src/arch/x86_64/paging/mod.rs index d523dd0a78..c1d3f1430b 100644 --- a/src/arch/x86_64/paging/mod.rs +++ b/src/arch/x86_64/paging/mod.rs @@ -18,11 +18,17 @@ pub use super::CurrentRmmArch as RmmA; pub type PageMapper = rmm::PageMapper; pub use crate::rmm::KernelMapper; -pub mod entry; -pub mod mapper; +pub mod entry { + bitflags! { + pub struct EntryFlags: usize { + const NO_CACHE = 1 << 4; + const HUGE_PAGE = 1 << 7; + const GLOBAL = 1 << 8; + } + } +} -/// Number of entries per page table -pub const ENTRY_COUNT: usize = RmmA::PAGE_ENTRIES; +pub mod mapper; /// Size of pages pub const PAGE_SIZE: usize = RmmA::PAGE_SIZE; diff --git a/src/context/context.rs b/src/context/context.rs index 23819cff1b..28c1959a47 100644 --- a/src/context/context.rs +++ b/src/context/context.rs @@ -15,7 +15,7 @@ use crate::arch::{interrupt::InterruptStack, paging::PAGE_SIZE}; use crate::common::aligned_box::AlignedBox; use crate::common::unique::Unique; use crate::context::{self, arch}; -use crate::context::file::{FileDescriptor, FileDescription}; +use crate::context::file::FileDescriptor; use crate::context::memory::AddrSpace; use crate::ipi::{ipi, IpiKind, IpiTarget}; use crate::paging::{RmmA, RmmArch}; @@ -24,7 +24,7 @@ use crate::scheme::{SchemeNamespace, FileHandle, CallerCtx}; use crate::sync::WaitMap; use crate::syscall::data::SigAction; -use crate::syscall::error::{Result, Error, EAGAIN, EINVAL, ESRCH}; +use crate::syscall::error::{Result, Error, EAGAIN, ESRCH}; use crate::syscall::flag::{SIG_DFL, SigActionFlags}; /// Unique identifier for a context (i.e. `pid`). @@ -129,77 +129,6 @@ impl PartialEq for WaitpidKey { impl Eq for WaitpidKey {} -pub struct ContextSnapshot { - // Copy fields - pub id: ContextId, - pub pgid: ContextId, - pub ppid: ContextId, - pub ruid: u32, - pub rgid: u32, - pub rns: SchemeNamespace, - pub euid: u32, - pub egid: u32, - pub ens: SchemeNamespace, - pub sigmask: [u64; 2], - pub umask: usize, - pub status: Status, - pub status_reason: &'static str, - pub running: bool, - pub cpu_id: Option, - pub cpu_time: u128, - pub sched_affinity: LogicalCpuSet, - pub syscall: Option<(usize, usize, usize, usize, usize, usize)>, - // Clone fields - //TODO: is there a faster way than allocation? - pub name: Box, - pub files: Vec>, -} - -impl ContextSnapshot { - //TODO: Should this accept &mut Context to ensure name/files will not change? - pub fn new(context: &Context) -> Self { - let name = context.name.clone().into_owned().into_boxed_str(); - let mut files = Vec::new(); - for descriptor_opt in context.files.read().iter() { - let description = if let Some(descriptor) = descriptor_opt { - let description = descriptor.description.read(); - Some(FileDescription { - namespace: description.namespace, - scheme: description.scheme, - number: description.number, - flags: description.flags, - }) - } else { - None - }; - files.push(description); - } - - Self { - id: context.id, - pgid: context.pgid, - ppid: context.ppid, - ruid: context.ruid, - rgid: context.rgid, - rns: context.rns, - euid: context.euid, - egid: context.egid, - ens: context.ens, - sigmask: context.sigmask, - umask: context.umask, - status: context.status.clone(), - status_reason: context.status_reason, - running: context.running, - cpu_id: context.cpu_id, - cpu_time: context.cpu_time, - sched_affinity: context.sched_affinity, - syscall: context.syscall, - name, - files, - } - } -} - /// A context, which identifies either a process or a thread #[derive(Debug)] pub struct Context { @@ -517,7 +446,6 @@ impl BorrowedHtBuf { let slice = self.use_for_slice(raw)?.ok_or(Error::new(ENAMETOOLONG))?; core::str::from_utf8(slice).map_err(|_| Error::new(EINVAL)) } - */ pub unsafe fn use_for_struct(&mut self) -> Result<&mut T> { if mem::size_of::() > PAGE_SIZE || mem::align_of::() > PAGE_SIZE { return Err(Error::new(EINVAL)); @@ -525,6 +453,7 @@ impl BorrowedHtBuf { self.buf_mut().fill(0_u8); Ok(unsafe { &mut *self.buf_mut().as_mut_ptr().cast() }) } + */ } impl Drop for BorrowedHtBuf { fn drop(&mut self) { diff --git a/src/context/memory.rs b/src/context/memory.rs index 8e3761a362..90f1f0be2c 100644 --- a/src/context/memory.rs +++ b/src/context/memory.rs @@ -443,9 +443,6 @@ impl PageSpan { pub fn intersects(&self, with: PageSpan) -> bool { !self.intersection(with).is_empty() } - pub fn contains(&self, page: Page) -> bool { - self.intersects(Self::new(page, 1)) - } pub fn slice(&self, inner_span: PageSpan) -> (Option, PageSpan, Option) { (self.before(inner_span), inner_span, self.after(inner_span)) } @@ -481,11 +478,6 @@ impl PageSpan { end.start_address().data().saturating_sub(start.start_address().data()) / PAGE_SIZE, ) } - - pub fn rebase(self, new_base: Self, page: Page) -> Page { - let offset = page.offset_from(self.base); - new_base.base.next_by(offset) - } } impl Default for UserGrants { @@ -515,14 +507,6 @@ impl UserGrants { .filter(|(base, info)| (**base..base.next_by(info.page_count)).contains(&page)) .map(|(base, info)| (*base, info)) } - // TODO: Deduplicate code? - pub fn contains_mut(&mut self, page: Page) -> Option<(Page, &mut GrantInfo)> { - self.inner - .range_mut(..=page) - .next_back() - .filter(|(base, info)| (**base..base.next_by(info.page_count)).contains(&page)) - .map(|(base, info)| (*base, info)) - } /// Returns an iterator over all grants that occupy some part of the /// requested region pub fn conflicts(&self, span: PageSpan) -> impl Iterator + '_ { @@ -897,7 +881,6 @@ impl Grant { new_cow_frame } Err(AddRefError::SharedToCow) => unreachable!(), - Err(AddRefError::RcOverflow) => return Err(Error::new(ENOMEM)), } } else { frame }; @@ -1062,7 +1045,6 @@ impl Grant { match src_page_info.add_ref(rk) { Ok(()) => src_frame, - Err(AddRefError::RcOverflow) => return Err(Enomem), Err(AddRefError::CowToShared) => { let new_frame = cow(src_frame, src_page_info, rk).map_err(|_| Enomem)?; @@ -1286,7 +1268,7 @@ impl Grant { description: Arc::clone(&file_ref.description), }, pin_refcount: 0, - }, + }, } }, }); @@ -1444,12 +1426,12 @@ pub fn setup_new_utable() -> Result { pub fn setup_new_utable() -> Result
{ use crate::paging::KernelMapper; - let mut utable = unsafe { PageMapper::create(TableKind::User, crate::rmm::FRAME_ALLOCATOR).ok_or(Error::new(ENOMEM))? }; + let utable = unsafe { PageMapper::create(TableKind::User, crate::rmm::FRAME_ALLOCATOR).ok_or(Error::new(ENOMEM))? }; { let active_ktable = KernelMapper::lock(); - let mut copy_mapping = |p4_no| unsafe { + let copy_mapping = |p4_no| unsafe { let entry = active_ktable.table().entry(p4_no) .unwrap_or_else(|| panic!("expected kernel PML {} to be mapped", p4_no)); @@ -1710,7 +1692,6 @@ fn correct_inner<'l>(addr_space_lock: &'l Arc>, mut addr_space match info.add_ref(RefKind::Shared) { Ok(()) => src_frame, - Err(AddRefError::RcOverflow) => return Err(PfError::Oom), Err(AddRefError::CowToShared) => { let new_frame = cow(src_frame, info, RefKind::Shared)?; diff --git a/src/context/mod.rs b/src/context/mod.rs index fa1c61b57a..00b80050d1 100644 --- a/src/context/mod.rs +++ b/src/context/mod.rs @@ -12,7 +12,7 @@ use crate::paging::{RmmA, RmmArch, TableKind}; use crate::percpu::PercpuBlock; use crate::syscall::error::{Error, ESRCH, Result}; -pub use self::context::{BorrowedHtBuf, Context, ContextId, ContextSnapshot, Status, WaitpidKey}; +pub use self::context::{BorrowedHtBuf, Context, ContextId, Status, WaitpidKey}; pub use self::list::ContextList; pub use self::switch::switch; diff --git a/src/elf.rs b/src/elf.rs index 2a74ea9226..70e4610713 100644 --- a/src/elf.rs +++ b/src/elf.rs @@ -45,14 +45,6 @@ impl<'a> Elf<'a> { } } - pub fn segments(&'a self) -> ElfSegments<'a> { - ElfSegments { - data: self.data, - header: self.header, - i: 0 - } - } - pub fn symbols(&'a self) -> Option> { let mut symtab_opt = None; for section in self.sections() { @@ -72,22 +64,6 @@ impl<'a> Elf<'a> { None } } - - /// Get the entry field of the header - pub fn entry(&self) -> usize { - self.header.e_entry as usize - } - - /// Get the program header offset - pub fn program_headers(&self) -> usize { - self.header.e_phoff as usize - } - pub fn program_header_count(&self) -> usize { - self.header.e_phnum as usize - } - pub fn program_headers_size(&self) -> usize { - self.header.e_phentsize as usize - } } pub struct ElfSections<'a> { diff --git a/src/memory/mod.rs b/src/memory/mod.rs index 5d292ea1b6..abe078a153 100644 --- a/src/memory/mod.rs +++ b/src/memory/mod.rs @@ -157,12 +157,6 @@ pub struct RaiiFrame { inner: Frame, } impl RaiiFrame { - // TODO: Unsafe? - pub fn new(frame: Frame) -> Self { - Self { - inner: frame, - } - } pub fn allocate() -> Result { // TODO: Use special tag? init_frame(RefCount::One).map_err(|_| Enomem).map(|inner| Self { inner }) @@ -170,11 +164,6 @@ impl RaiiFrame { pub fn get(&self) -> Frame { self.inner } - pub fn take_ownership(self) -> Frame { - let frame = self.inner.clone(); - core::mem::forget(self); - frame - } } impl Drop for RaiiFrame { @@ -336,7 +325,6 @@ pub fn init_mm() { } #[derive(Debug)] pub enum AddRefError { - RcOverflow, CowToShared, SharedToCow, } diff --git a/src/scheme/mod.rs b/src/scheme/mod.rs index a006d60d06..5d690b1653 100644 --- a/src/scheme/mod.rs +++ b/src/scheme/mod.rs @@ -199,10 +199,6 @@ impl SchemeList { Ok(to) } - pub fn iter(&self) -> ::alloc::collections::btree_map::Iter> { - self.map.iter() - } - pub fn iter_name(&self, ns: SchemeNamespace) -> SchemeIter { SchemeIter { inner: self.names.get(&ns).map(|names| names.iter()) diff --git a/src/sync/wait_queue.rs b/src/sync/wait_queue.rs index b63c59374c..83a98265cc 100644 --- a/src/sync/wait_queue.rs +++ b/src/sync/wait_queue.rs @@ -20,29 +20,6 @@ impl WaitQueue { } } - pub fn clone(&self) -> WaitQueue where T: Clone { - WaitQueue { - inner: Mutex::new(self.inner.lock().clone()), - condition: WaitCondition::new() - } - } - - pub fn is_empty(&self) -> bool { - self.inner.lock().is_empty() - } - - pub fn receive(&self, reason: &'static str) -> Option { - loop { - let mut inner = self.inner.lock(); - if let Some(value) = inner.pop_front() { - return Some(value); - } - if ! self.condition.wait(inner, reason) { - return None; - } - } - } - pub fn receive_into_user(&self, buf: UserSliceWo, block: bool, reason: &'static str) -> Result { loop { let mut inner = self.inner.lock(); @@ -79,29 +56,6 @@ impl WaitQueue { } } - pub fn receive_into(&self, buf: &mut [T], block: bool, reason: &'static str) -> Option { - let mut i = 0; - - if i < buf.len() && block { - buf[i] = self.receive(reason)?; - i += 1; - } - - { - let mut inner = self.inner.lock(); - while i < buf.len() { - if let Some(value) = inner.pop_front() { - buf[i] = value; - i += 1; - } else { - break; - } - } - } - - Some(i) - } - pub fn send(&self, value: T) -> usize { let len = { let mut inner = self.inner.lock(); @@ -111,14 +65,4 @@ impl WaitQueue { self.condition.notify(); len } - - pub fn send_from(&self, buf: &[T]) -> usize where T: Copy { - let len = { - let mut inner = self.inner.lock(); - inner.extend(buf.iter()); - inner.len() - }; - self.condition.notify(); - len - } }