diff --git a/src/allocator/linked_list.rs b/src/allocator/linked_list.rs index 1c8acb601b4cd8d2fc38269d6613b98b23f89058..aaa3a078bb4c85e251ff99b6d9e417aa83d441bb 100644 --- a/src/allocator/linked_list.rs +++ b/src/allocator/linked_list.rs @@ -3,7 +3,7 @@ use core::ptr::{self, NonNull}; use linked_list_allocator::Heap; use spin::Mutex; -use crate::paging::{ActivePageTable, PageTableType}; +use crate::paging::{ActivePageTable, TableKind}; static HEAP: Mutex<Option<Heap>> = Mutex::new(None); @@ -32,7 +32,7 @@ unsafe impl GlobalAlloc for Allocator { panic!("__rust_allocate: heap not initialized"); }; - super::map_heap(&mut ActivePageTable::new(PageTableType::Kernel), crate::KERNEL_HEAP_OFFSET + size, crate::KERNEL_HEAP_SIZE); + super::map_heap(&mut ActivePageTable::new(TableKind::Kernel), crate::KERNEL_HEAP_OFFSET + size, crate::KERNEL_HEAP_SIZE); if let Some(ref mut heap) = *HEAP.lock() { heap.extend(crate::KERNEL_HEAP_SIZE); diff --git a/src/arch/aarch64/interrupt/trace.rs b/src/arch/aarch64/interrupt/trace.rs index ca1d47290be39b5d9fca4434eb8e2911afc9927f..6d0796296894c0783780f4f84cf9978cc2261e91 100644 --- a/src/arch/aarch64/interrupt/trace.rs +++ b/src/arch/aarch64/interrupt/trace.rs @@ -1,7 +1,7 @@ use core::mem; use goblin::elf::sym; -use crate::paging::{ActivePageTable, PageTableType, VirtualAddress}; +use crate::paging::{ActivePageTable, TableKind, VirtualAddress}; /// Get a stack trace //TODO: Check for stack being mapped before dereferencing @@ -12,8 +12,8 @@ pub unsafe fn stack_trace() { println!("TRACE: {:>016x}", fp); //Maximum 64 frames - let active_ktable = ActivePageTable::new(PageTableType::Kernel); - let active_utable = ActivePageTable::new(PageTableType::User); + let active_ktable = ActivePageTable::new(TableKind::Kernel); + let active_utable = ActivePageTable::new(TableKind::User); let in_kernel_or_user_table = |ptr| { active_ktable.translate(VirtualAddress::new(ptr)).is_some() || active_utable.translate(VirtualAddress::new(ptr)).is_some() diff --git a/src/arch/aarch64/paging/mapper.rs b/src/arch/aarch64/paging/mapper.rs index 4768452d63e10b9a051b3e84dc99359c355b4c23..44a872a264b05ac6ad8ab1cc9ec179dedea46a20 100644 --- a/src/arch/aarch64/paging/mapper.rs +++ b/src/arch/aarch64/paging/mapper.rs @@ -111,11 +111,11 @@ impl Mapper { let mut translated_flags: PageDescriptorFlags = PageDescriptorFlags::VALID | PageDescriptorFlags::PAGE | PageDescriptorFlags::AF; if flags.contains(EntryFlags::NO_EXECUTE) { - match page.start_address().get_type() { - VirtualAddressType::User => { + match page.start_address().kind() { + TableKind::User => { translated_flags.insert(PageDescriptorFlags::UXN); }, - VirtualAddressType::Kernel => { + TableKind::Kernel => { translated_flags.insert(PageDescriptorFlags::PXN); }, } diff --git a/src/arch/aarch64/paging/mod.rs b/src/arch/aarch64/paging/mod.rs index e0e53847153e3050abc0d28b98533849d8ea1e6d..1d3f5e2aac707fda71775c8147fc8294dd7cbeb3 100644 --- a/src/arch/aarch64/paging/mod.rs +++ b/src/arch/aarch64/paging/mod.rs @@ -9,10 +9,10 @@ use crate::device::cpu::registers::{control_regs, tlb}; use crate::memory::{allocate_frames, Frame}; use self::entry::{EntryFlags, TableDescriptorFlags}; -use self::mapper::{Mapper, MapperFlushAll, MapperType}; +use self::mapper::{Mapper, MapperFlushAll}; use self::temporary_page::TemporaryPage; -pub use rmm::PhysicalAddress; +pub use rmm::{PhysicalAddress, TableKind, VirtualAddress}; pub mod entry; pub mod mapper; @@ -211,11 +211,6 @@ pub struct ActivePageTable { locked: bool, } -pub enum PageTableType { - User, - Kernel -} - impl Deref for ActivePageTable { type Target = Mapper; @@ -232,24 +227,18 @@ impl DerefMut for ActivePageTable { impl ActivePageTable { //TODO: table_type argument - pub unsafe fn new(table_type: PageTableType) -> ActivePageTable { + pub unsafe fn new(table_kind: TableKind) -> ActivePageTable { page_table_lock(); ActivePageTable { - mapper: Mapper::new(match table_type { - PageTableType::User => MapperType::User, - PageTableType::Kernel => MapperType::Kernel, - }), + mapper: Mapper::new(table_kind), locked: true, } } //TODO: table_type argument - pub unsafe fn new_unlocked(table_type: PageTableType) -> ActivePageTable { + pub unsafe fn new_unlocked(table_kind: TableKind) -> ActivePageTable { ActivePageTable { - mapper: Mapper::new(match table_type { - PageTableType::User => MapperType::User, - PageTableType::Kernel => MapperType::Kernel, - }), + mapper: Mapper::new(table_kind), locked: false, } } @@ -376,34 +365,6 @@ impl InactivePageTable { } } -/// A virtual address. -#[derive(Copy, Clone, Debug, Eq, Ord, PartialEq, PartialOrd)] -pub struct VirtualAddress(usize); - -#[derive(Debug, PartialEq)] -pub enum VirtualAddressType { - User, - Kernel -} - -impl VirtualAddress { - pub fn new(address: usize) -> Self { - VirtualAddress(address) - } - - pub fn data(&self) -> usize { - self.0 - } - - pub fn get_type(&self) -> VirtualAddressType { - if ((self.0 >> 48) & 0xffff) == 0xffff { - VirtualAddressType::Kernel - } else { - VirtualAddressType::User - } - } -} - /// Page #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)] pub struct Page { diff --git a/src/arch/x86_64/idt.rs b/src/arch/x86_64/idt.rs index 2c50c0f893efb21d7c33cbfc3407b67ad106571b..66981dc0b74cab7b2e7de71d25dcb8005f7cc176 100644 --- a/src/arch/x86_64/idt.rs +++ b/src/arch/x86_64/idt.rs @@ -177,8 +177,8 @@ pub unsafe fn init_generic(is_bsp: bool, idt: &mut Idt) { use crate::memory::{Frame, PhysicalAddress}; use crate::paging::{ActivePageTable, Page, VirtualAddress}; - let mut active_table = ActivePageTable::new(); let base_virtual_address = VirtualAddress::new(frames.start_address().data() + crate::PHYS_OFFSET); + let mut active_table = ActivePageTable::new(base_virtual_address.kind()); for i in 0..page_count { let virtual_address = VirtualAddress::new(base_virtual_address.data() + i * crate::memory::PAGE_SIZE); diff --git a/src/arch/x86_64/interrupt/trace.rs b/src/arch/x86_64/interrupt/trace.rs index 4b224d70bb9e1bec5362ba2df1ddf2acb5567c63..c8f27ae518d33f5fa2c65b3a4ef2ca826c8c825e 100644 --- a/src/arch/x86_64/interrupt/trace.rs +++ b/src/arch/x86_64/interrupt/trace.rs @@ -2,7 +2,7 @@ use core::{mem, str}; use goblin::elf::sym; use rustc_demangle::demangle; -use crate::paging::{ActivePageTable, PageTableType, VirtualAddress}; +use crate::paging::{ActivePageTable, TableKind, VirtualAddress}; /// Get a stack trace //TODO: Check for stack being mapped before dereferencing @@ -13,7 +13,7 @@ pub unsafe fn stack_trace() { println!("TRACE: {:>016X}", rbp); //Maximum 64 frames - let active_table = ActivePageTable::new(PageTableType::User); + let active_table = ActivePageTable::new(TableKind::User); for _frame in 0..64 { if let Some(rip_rbp) = rbp.checked_add(mem::size_of::<usize>()) { if active_table.translate(VirtualAddress::new(rbp)).is_some() && active_table.translate(VirtualAddress::new(rip_rbp)).is_some() { diff --git a/src/arch/x86_64/paging/mod.rs b/src/arch/x86_64/paging/mod.rs index ac4d8a4c35d098ffd4eba7fd73a59e3ebeb4a0b3..ebf1e5f2b26cdfda615d574f705e32ee5b6379e4 100644 --- a/src/arch/x86_64/paging/mod.rs +++ b/src/arch/x86_64/paging/mod.rs @@ -15,6 +15,8 @@ pub use rmm::{ Arch as RmmArch, PageFlags, PhysicalAddress, + TableKind, + VirtualAddress, X8664Arch as RmmA, }; @@ -184,7 +186,7 @@ pub unsafe fn init( init_pat(); - let mut active_table = ActivePageTable::new_unlocked(PageTableType::User); + let mut active_table = ActivePageTable::new_unlocked(TableKind::User); let flush_all = map_tss(cpu_id, &mut active_table); flush_all.flush(); @@ -198,7 +200,7 @@ pub unsafe fn init_ap( ) -> usize { init_pat(); - let mut active_table = ActivePageTable::new_unlocked(PageTableType::User); + let mut active_table = ActivePageTable::new_unlocked(TableKind::User); let mut new_table = InactivePageTable::from_address(bsp_table); @@ -225,11 +227,6 @@ pub struct ActivePageTable { locked: bool, } -pub enum PageTableType { - User, - Kernel -} - impl Deref for ActivePageTable { type Target = Mapper; @@ -245,7 +242,7 @@ impl DerefMut for ActivePageTable { } impl ActivePageTable { - pub unsafe fn new(_table_type: PageTableType) -> ActivePageTable { + pub unsafe fn new(_table_kind: TableKind) -> ActivePageTable { page_table_lock(); ActivePageTable { mapper: Mapper::new(), @@ -253,7 +250,7 @@ impl ActivePageTable { } } - pub unsafe fn new_unlocked(_table_type: PageTableType) -> ActivePageTable { + pub unsafe fn new_unlocked(_table_kind: TableKind) -> ActivePageTable { ActivePageTable { mapper: Mapper::new(), locked: false, @@ -379,34 +376,6 @@ impl InactivePageTable { } } -/// A virtual address. -#[derive(Copy, Clone, Debug, Eq, Ord, PartialEq, PartialOrd)] -pub struct VirtualAddress(usize); - -#[derive(Debug, PartialEq)] -pub enum VirtualAddressType { - User, - Kernel -} - -impl VirtualAddress { - pub fn new(address: usize) -> Self { - VirtualAddress(address) - } - - pub fn data(&self) -> usize { - self.0 - } - - pub fn get_type(&self) -> VirtualAddressType { - if ((self.0 >> 48) & 0xffff) == 0xffff { - VirtualAddressType::Kernel - } else { - VirtualAddressType::User - } - } -} - /// Page #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)] pub struct Page { diff --git a/src/context/list.rs b/src/context/list.rs index 32587248d7a289b0c6c8bc6eda56e3a0d5cbb2ae..90dd566f9a794fdd456a536138c704266bc88c0a 100644 --- a/src/context/list.rs +++ b/src/context/list.rs @@ -4,7 +4,7 @@ use alloc::collections::BTreeMap; use core::alloc::{GlobalAlloc, Layout}; use core::{iter, mem}; use core::sync::atomic::Ordering; -use crate::paging::{ActivePageTable, PageTableType}; +use crate::paging::{ActivePageTable, TableKind}; use spin::RwLock; use crate::syscall::error::{Result, Error, EAGAIN}; @@ -100,9 +100,9 @@ impl ContextList { context.arch.set_context_handle(); } - context.arch.set_page_utable(unsafe { ActivePageTable::new(PageTableType::User).address() }); + context.arch.set_page_utable(unsafe { ActivePageTable::new(TableKind::User).address() }); #[cfg(target_arch = "aarch64")] - context.arch.set_page_ktable(unsafe { ActivePageTable::new(PageTableType::Kernel).address() }); + context.arch.set_page_ktable(unsafe { ActivePageTable::new(TableKind::Kernel).address() }); context.arch.set_fx(fx.as_ptr() as usize); context.arch.set_stack(stack.as_ptr() as usize + offset); context.kfx = Some(fx); diff --git a/src/context/memory.rs b/src/context/memory.rs index 87f2e4190a6fc3479d97b422bfd72a18c5b76e56..ddd47016975569b914ac7df53eee91d2f8f05e6f 100644 --- a/src/context/memory.rs +++ b/src/context/memory.rs @@ -15,7 +15,7 @@ use crate::arch::paging::PAGE_SIZE; use crate::context::file::FileDescriptor; use crate::ipi::{ipi, IpiKind, IpiTarget}; use crate::memory::Frame; -use crate::paging::{ActivePageTable, InactivePageTable, Page, PageFlags, PageTableType, PageIter, PhysicalAddress, RmmA, VirtualAddress}; +use crate::paging::{ActivePageTable, InactivePageTable, Page, PageFlags, PageIter, PhysicalAddress, RmmA, VirtualAddress}; use crate::paging::mapper::PageFlushAll; use crate::paging::temporary_page::TemporaryPage; @@ -303,10 +303,7 @@ impl Grant { } pub fn physmap(from: PhysicalAddress, to: VirtualAddress, size: usize, flags: PageFlags<RmmA>) -> Grant { - let mut active_table = match to.get_type() { - VirtualAddressType::User => unsafe { ActivePageTable::new(PageTableType::User) }, - VirtualAddressType::Kernel => unsafe { ActivePageTable::new(PageTableType::Kernel) } - }; + let mut active_table = unsafe { ActivePageTable::new(to.kind()) }; let flush_all = PageFlushAll::new(); @@ -333,10 +330,7 @@ impl Grant { } pub fn map(to: VirtualAddress, size: usize, flags: PageFlags<RmmA>) -> Grant { - let mut active_table = match to.get_type() { - VirtualAddressType::User => unsafe { ActivePageTable::new(PageTableType::User) }, - VirtualAddressType::Kernel => unsafe { ActivePageTable::new(PageTableType::Kernel) } - }; + let mut active_table = unsafe { ActivePageTable::new(to.kind()) }; let flush_all = PageFlushAll::new(); @@ -362,10 +356,7 @@ impl Grant { } pub fn map_inactive(from: VirtualAddress, to: VirtualAddress, size: usize, flags: PageFlags<RmmA>, desc_opt: Option<FileDescriptor>, new_table: &mut InactivePageTable, temporary_page: &mut TemporaryPage) -> Grant { - let mut active_table = match from.get_type() { - VirtualAddressType::User => unsafe { ActivePageTable::new(PageTableType::User) }, - VirtualAddressType::Kernel => unsafe { ActivePageTable::new(PageTableType::Kernel) } - }; + let active_table = unsafe { ActivePageTable::new(from.kind()) }; //TODO: Do not allocate let mut frames = VecDeque::with_capacity(size/PAGE_SIZE); @@ -377,10 +368,7 @@ impl Grant { frames.push_back(frame); } - let mut active_table = match to.get_type() { - VirtualAddressType::User => unsafe { ActivePageTable::new(PageTableType::User) }, - VirtualAddressType::Kernel => unsafe { ActivePageTable::new(PageTableType::Kernel) } - }; + let mut active_table = unsafe { ActivePageTable::new(to.kind()) }; active_table.with(new_table, temporary_page, |mapper| { let start_page = Page::containing_address(to); @@ -411,10 +399,7 @@ impl Grant { pub fn secret_clone(&self, new_start: VirtualAddress) -> Grant { assert!(self.mapped); - let mut active_table = match new_start.get_type() { - VirtualAddressType::User => unsafe { ActivePageTable::new(PageTableType::User) }, - VirtualAddressType::Kernel => unsafe { ActivePageTable::new(PageTableType::Kernel) } - }; + let mut active_table = unsafe { ActivePageTable::new(new_start.kind()) }; let flush_all = PageFlushAll::new(); @@ -471,10 +456,7 @@ impl Grant { pub fn move_to(&mut self, new_start: VirtualAddress, new_table: &mut InactivePageTable, temporary_page: &mut TemporaryPage) { assert!(self.mapped); - let mut active_table = match new_start.get_type() { - VirtualAddressType::User => unsafe { ActivePageTable::new(PageTableType::User) }, - VirtualAddressType::Kernel => unsafe { ActivePageTable::new(PageTableType::Kernel) } - }; + let mut active_table = unsafe { ActivePageTable::new(new_start.kind()) }; let flush_all = PageFlushAll::new(); @@ -510,10 +492,7 @@ impl Grant { pub fn unmap(mut self) { assert!(self.mapped); - let mut active_table = match self.start_address().get_type() { - VirtualAddressType::User => unsafe { ActivePageTable::new(PageTableType::User) }, - VirtualAddressType::Kernel => unsafe { ActivePageTable::new(PageTableType::Kernel) } - }; + let mut active_table = unsafe { ActivePageTable::new(self.start_address().kind()) }; let flush_all = PageFlushAll::new(); @@ -543,10 +522,7 @@ impl Grant { pub fn unmap_inactive(mut self, new_table: &mut InactivePageTable, temporary_page: &mut TemporaryPage) { assert!(self.mapped); - let mut active_table = match self.start_address().get_type() { - VirtualAddressType::User => unsafe { ActivePageTable::new(PageTableType::User) }, - VirtualAddressType::Kernel => unsafe { ActivePageTable::new(PageTableType::Kernel) } - }; + let mut active_table = unsafe { ActivePageTable::new(self.start_address().kind()) }; active_table.with(new_table, temporary_page, |mapper| { let start_page = Page::containing_address(self.start_address()); @@ -721,10 +697,7 @@ impl Memory { } fn map(&mut self, clear: bool) { - let mut active_table = match self.start.get_type() { - VirtualAddressType::User => unsafe { ActivePageTable::new(PageTableType::User) }, - VirtualAddressType::Kernel => unsafe { ActivePageTable::new(PageTableType::Kernel) } - }; + let mut active_table = unsafe { ActivePageTable::new(self.start.kind()) }; let flush_all = PageFlushAll::new(); @@ -744,10 +717,7 @@ impl Memory { } fn unmap(&mut self) { - let mut active_table = match self.start.get_type() { - VirtualAddressType::User => unsafe { ActivePageTable::new(PageTableType::User) }, - VirtualAddressType::Kernel => unsafe { ActivePageTable::new(PageTableType::Kernel) } - }; + let mut active_table = unsafe { ActivePageTable::new(self.start.kind()) }; let flush_all = PageFlushAll::new(); @@ -762,10 +732,7 @@ impl Memory { /// A complicated operation to move a piece of memory to a new page table /// It also allows for changing the address at the same time pub fn move_to(&mut self, new_start: VirtualAddress, new_table: &mut InactivePageTable, temporary_page: &mut TemporaryPage) { - let mut active_table = match new_start.get_type() { - VirtualAddressType::User => unsafe { ActivePageTable::new(PageTableType::User) }, - VirtualAddressType::Kernel => unsafe { ActivePageTable::new(PageTableType::Kernel) } - }; + let mut active_table = unsafe { ActivePageTable::new(new_start.kind()) }; let flush_all = PageFlushAll::new(); @@ -787,10 +754,7 @@ impl Memory { } pub fn remap(&mut self, new_flags: PageFlags<RmmA>) { - let mut active_table = match self.start.get_type() { - VirtualAddressType::User => unsafe { ActivePageTable::new(PageTableType::User) }, - VirtualAddressType::Kernel => unsafe { ActivePageTable::new(PageTableType::Kernel) } - }; + let mut active_table = unsafe { ActivePageTable::new(self.start.kind()) }; let flush_all = PageFlushAll::new(); @@ -805,10 +769,7 @@ impl Memory { } pub fn resize(&mut self, new_size: usize, clear: bool) { - let mut active_table = match self.start.get_type() { - VirtualAddressType::User => unsafe { ActivePageTable::new(PageTableType::User) }, - VirtualAddressType::Kernel => unsafe { ActivePageTable::new(PageTableType::Kernel) } - }; + let mut active_table = unsafe { ActivePageTable::new(self.start.kind()) }; //TODO: Calculate page changes to minimize operations if new_size > self.size { diff --git a/src/ptrace.rs b/src/ptrace.rs index 9d7480ea0395a43f5c3ef63edc55315c5781c176..b4a3be213fff8cd87a1e74cc70f0c5a9ae5f74cb 100644 --- a/src/ptrace.rs +++ b/src/ptrace.rs @@ -8,7 +8,7 @@ use crate::{ paging::{ mapper::PageFlushAll, temporary_page::TemporaryPage, - ActivePageTable, InactivePageTable, PageTableType, Page, PAGE_SIZE, VirtualAddress + ActivePageTable, InactivePageTable, Page, PAGE_SIZE, TableKind, VirtualAddress } }, common::unique::Unique, @@ -457,7 +457,7 @@ where F: FnOnce(*mut u8) -> Result<()> // in `proc:<pid>/mem`, or return a partial read/write. let start = Page::containing_address(VirtualAddress::new(crate::USER_TMP_MISC_OFFSET)); - let mut active_page_table = unsafe { ActivePageTable::new(PageTableType::User) }; + let mut active_page_table = unsafe { ActivePageTable::new(TableKind::User) }; let mut target_page_table = unsafe { InactivePageTable::from_address(context.arch.get_page_utable()) }; diff --git a/src/scheme/acpi.rs b/src/scheme/acpi.rs index f5fb2fc4e8b0540debb8ca9b894978f9e47c2dfe..e3351a78695a89a0fadd10a5a1bc9367d800e275 100644 --- a/src/scheme/acpi.rs +++ b/src/scheme/acpi.rs @@ -17,7 +17,7 @@ use spin::{Mutex, RwLock}; use crate::acpi::sdt::Sdt; use crate::acpi::SdtSignature; -use crate::paging::ActivePageTable; +use crate::paging::{ActivePageTable, TableKind}; #[derive(Clone, Copy)] struct PhysSlice { @@ -92,7 +92,7 @@ enum Handle { impl AcpiScheme { fn get_tables() -> Vec<(SdtSignature, PhysSlice)> { - let mut active_table = unsafe { ActivePageTable::new() }; + let mut active_table = unsafe { ActivePageTable::new(TableKind::Kernel) }; let mut tables = Vec::new(); @@ -427,7 +427,7 @@ impl Scheme for AcpiScheme { ) = self.tables[index]; assert_eq!(phys_ptr, old_virt); let new_virt = - crate::acpi::get_sdt(phys_ptr, unsafe { &mut ActivePageTable::new() }) + crate::acpi::get_sdt(phys_ptr, unsafe { &mut ActivePageTable::new(TableKind::Kernel) }) as *const Sdt as usize; let table_contents = diff --git a/src/scheme/memory.rs b/src/scheme/memory.rs index 125c57392845142a6943a1467bb8dc4cc48c4fb2..2c443551260ebf2651503bbdaedaf537b45ac269 100644 --- a/src/scheme/memory.rs +++ b/src/scheme/memory.rs @@ -1,7 +1,7 @@ use crate::context; use crate::context::memory::{page_flags, Grant}; use crate::memory::{free_frames, used_frames, PAGE_SIZE}; -use crate::paging::{ActivePageTable, PageTableType, VirtualAddress, VirtualAddressType}; +use crate::paging::{ActivePageTable, VirtualAddress}; use crate::syscall::data::{Map, OldMap, StatVfs}; use crate::syscall::error::*; use crate::syscall::flag::MapFlags; @@ -48,10 +48,7 @@ impl Scheme for MemoryScheme { // Make sure it's *absolutely* not mapped already // TODO: Keep track of all allocated memory so this isn't necessary - let active_table = match VirtualAddress::new(map.address).get_type() { - VirtualAddressType::User => unsafe { ActivePageTable::new(PageTableType::User) }, - VirtualAddressType::Kernel => unsafe { ActivePageTable::new(PageTableType::Kernel) } - }; + let active_table = unsafe { ActivePageTable::new(VirtualAddress::new(map.address).kind()) }; for page in region.pages() { if active_table.translate_page(page).is_some() { diff --git a/src/syscall/driver.rs b/src/syscall/driver.rs index f943f639aed278bc14abb3e6d94a9370e9e26df0..a816f9c3379252af145c6e3aacf6156346d032b6 100644 --- a/src/syscall/driver.rs +++ b/src/syscall/driver.rs @@ -1,6 +1,6 @@ use crate::interrupt::InterruptStack; use crate::memory::{allocate_frames_complex, deallocate_frames, Frame}; -use crate::paging::{ActivePageTable, PageFlags, PageTableType, PhysicalAddress, VirtualAddress}; +use crate::paging::{ActivePageTable, PageFlags, PhysicalAddress, VirtualAddress}; use crate::paging::entry::EntryFlags; use crate::context; use crate::context::memory::{Grant, Region}; @@ -153,10 +153,7 @@ pub fn physunmap(virtual_address: usize) -> Result<usize> { pub fn virttophys(virtual_address: usize) -> Result<usize> { enforce_root()?; - let active_table = match VirtualAddress::new(virtual_address).get_type() { - VirtualAddressType::User => unsafe { ActivePageTable::new(PageTableType::User) }, - VirtualAddressType::Kernel => unsafe { ActivePageTable::new(PageTableType::Kernel) } - }; + let active_table = unsafe { ActivePageTable::new(VirtualAddress::new(virtual_address).kind()) }; match active_table.translate(VirtualAddress::new(virtual_address)) { Some(physical_address) => Ok(physical_address.data()), diff --git a/src/syscall/futex.rs b/src/syscall/futex.rs index 4bed1d0a40113882efd2eb527c872caf1aa43fe0..8d3582e5fb99c713ca46be971a46fdc365d8ea19 100644 --- a/src/syscall/futex.rs +++ b/src/syscall/futex.rs @@ -10,7 +10,7 @@ use spin::{Once, RwLock, RwLockReadGuard, RwLockWriteGuard}; use crate::context::{self, Context}; use crate::time; use crate::memory::PhysicalAddress; -use crate::paging::{ActivePageTable, VirtualAddress}; +use crate::paging::{ActivePageTable, TableKind, VirtualAddress}; use crate::syscall::data::TimeSpec; use crate::syscall::error::{Error, Result, ESRCH, EAGAIN, EFAULT, EINVAL}; use crate::syscall::flag::{FUTEX_WAIT, FUTEX_WAKE, FUTEX_REQUEUE}; @@ -45,7 +45,7 @@ pub fn futexes_mut() -> RwLockWriteGuard<'static, FutexList> { // pointee cannot be changed by another thread, which could make atomic ops useless. pub fn futex(addr: &mut i32, op: usize, val: i32, val2: usize, addr2: *mut i32) -> Result<usize> { let target_physaddr = unsafe { - let active_table = ActivePageTable::new(); + let active_table = ActivePageTable::new(TableKind::User); let virtual_address = VirtualAddress::new(addr as *mut i32 as usize); // FIXME: Already validated in syscall/mod.rs @@ -133,7 +133,7 @@ pub fn futex(addr: &mut i32, op: usize, val: i32, val2: usize, addr2: *mut i32) }, FUTEX_REQUEUE => { let addr2_physaddr = unsafe { - let active_table = ActivePageTable::new(); + let active_table = ActivePageTable::new(TableKind::User); let addr2_safe = validate_slice_mut(addr2, 1).map(|addr2_safe| &mut addr2_safe[0])?; let addr2_virt = VirtualAddress::new(addr2_safe as *mut i32 as usize); diff --git a/src/syscall/process.rs b/src/syscall/process.rs index daefc0996c45507a9bfac42a8274641e2f2d4361..db2d2bd65be0b44091e8443ded13d7cb1b6951ad 100644 --- a/src/syscall/process.rs +++ b/src/syscall/process.rs @@ -21,7 +21,7 @@ use crate::ipi::{ipi, IpiKind, IpiTarget}; use crate::memory::allocate_frames; use crate::paging::mapper::PageFlushAll; use crate::paging::temporary_page::TemporaryPage; -use crate::paging::{ActivePageTable, InactivePageTable, Page, PageFlags, PageTableType, VirtualAddress, PAGE_SIZE}; +use crate::paging::{ActivePageTable, InactivePageTable, Page, PageFlags, TableKind, VirtualAddress, PAGE_SIZE}; use crate::{ptrace, syscall}; use crate::scheme::FileHandle; use crate::start::usermode; @@ -352,8 +352,8 @@ pub fn clone(flags: CloneFlags, stack_base: usize) -> Result<ContextId> { context.arch = arch; - let mut active_utable = unsafe { ActivePageTable::new(PageTableType::User) }; - let mut active_ktable = unsafe { ActivePageTable::new(PageTableType::Kernel) }; + let mut active_utable = unsafe { ActivePageTable::new(TableKind::User) }; + let mut active_ktable = unsafe { ActivePageTable::new(TableKind::Kernel) }; let mut temporary_upage = TemporaryPage::new(Page::containing_address(VirtualAddress::new(crate::USER_TMP_MISC_OFFSET))); let mut temporary_kpage = TemporaryPage::new(Page::containing_address(VirtualAddress::new(crate::KERNEL_TMP_MISC_OFFSET))); @@ -399,9 +399,9 @@ pub fn clone(flags: CloneFlags, stack_base: usize) -> Result<ContextId> { // Copy physmap mapping { - let frame = active_table.p4()[crate::PHYS_PML4].pointed_frame().expect("physmap not mapped"); - let flags = active_table.p4()[crate::PHYS_PML4].flags(); - active_table.with(&mut new_table, &mut temporary_page, |mapper| { + let frame = active_ktable.p4()[crate::PHYS_PML4].pointed_frame().expect("physmap not mapped"); + let flags = active_ktable.p4()[crate::PHYS_PML4].flags(); + active_ktable.with(&mut new_ktable, &mut temporary_kpage, |mapper| { mapper.p4_mut()[crate::PHYS_PML4].set(frame, flags); }); } @@ -1365,7 +1365,7 @@ pub fn mprotect(address: usize, size: usize, flags: MapFlags) -> Result<usize> { let end_offset = size.checked_sub(1).ok_or(Error::new(EFAULT))?; let end_address = address.checked_add(end_offset).ok_or(Error::new(EFAULT))?; - let mut active_table = unsafe { ActivePageTable::new(PageTableType::User) }; + let mut active_table = unsafe { ActivePageTable::new(TableKind::User) }; let flush_all = PageFlushAll::new(); diff --git a/src/syscall/validate.rs b/src/syscall/validate.rs index 33f0b86f423806a1ec6c2485a4e56330f60e9d79..ae99a9503618e52aac60fbc3d479327ebd6a7e65 100644 --- a/src/syscall/validate.rs +++ b/src/syscall/validate.rs @@ -1,16 +1,13 @@ use core::{mem, slice, str}; -use crate::paging::{ActivePageTable, Page, PageTableType, VirtualAddress}; +use crate::paging::{ActivePageTable, Page, VirtualAddress}; use crate::syscall::error::*; fn validate(address: usize, size: usize, writable: bool) -> Result<()> { let end_offset = size.checked_sub(1).ok_or(Error::new(EFAULT))?; let end_address = address.checked_add(end_offset).ok_or(Error::new(EFAULT))?; - let active_table = match VirtualAddress::new(address).get_type() { - VirtualAddressType::User => unsafe { ActivePageTable::new(PageTableType::User) }, - VirtualAddressType::Kernel => unsafe { ActivePageTable::new(PageTableType::Kernel) } - }; + let active_table = unsafe { ActivePageTable::new(VirtualAddress::new(address).kind()) }; let start_page = Page::containing_address(VirtualAddress::new(address)); let end_page = Page::containing_address(VirtualAddress::new(end_address));