From 23f49414bd007c2aba6ed7b7b9a08f6019641ed7 Mon Sep 17 00:00:00 2001 From: 4lDO2 <4lDO2@protonmail.com> Date: Fri, 10 Jun 2022 11:43:50 +0200 Subject: [PATCH] Fix phys offset, lock grants correctly. --- src/acpi/mod.rs | 12 ++++++------ src/context/memory.rs | 3 ++- src/syscall/process.rs | 6 +++--- 3 files changed, 11 insertions(+), 10 deletions(-) diff --git a/src/acpi/mod.rs b/src/acpi/mod.rs index 45c384d..d65696e 100644 --- a/src/acpi/mod.rs +++ b/src/acpi/mod.rs @@ -30,7 +30,7 @@ mod rsdp; pub fn get_sdt(sdt_address: usize, active_table: &mut ActivePageTable) -> &'static Sdt { { - let page = Page::containing_address(VirtualAddress::new(sdt_address + crate::KERNEL_OFFSET)); + let page = Page::containing_address(VirtualAddress::new(sdt_address + crate::PHYS_OFFSET)); if active_table.translate_page(page).is_none() { let frame = Frame::containing_address(PhysicalAddress::new(sdt_address)); let result = active_table.map_to(page, frame, PageFlags::new()); @@ -38,15 +38,15 @@ pub fn get_sdt(sdt_address: usize, active_table: &mut ActivePageTable) -> &'stat } } - let sdt = unsafe { &*((sdt_address + crate::KERNEL_OFFSET) as *const Sdt) }; + let sdt = unsafe { &*((sdt_address + crate::PHYS_OFFSET) as *const Sdt) }; // Map extra SDT frames if required { - let start_page = Page::containing_address(VirtualAddress::new(sdt_address + 4096 + crate::KERNEL_OFFSET)); - let end_page = Page::containing_address(VirtualAddress::new(sdt_address + sdt.length as usize + crate::KERNEL_OFFSET)); + let start_page = Page::containing_address(VirtualAddress::new(sdt_address + 4096 + crate::PHYS_OFFSET)); + let end_page = Page::containing_address(VirtualAddress::new(sdt_address + sdt.length as usize + crate::PHYS_OFFSET)); for page in Page::range_inclusive(start_page, end_page) { if active_table.translate_page(page).is_none() { - let frame = Frame::containing_address(PhysicalAddress::new(page.start_address().data() - crate::KERNEL_OFFSET)); + let frame = Frame::containing_address(PhysicalAddress::new(page.start_address().data() - crate::PHYS_OFFSET)); let result = active_table.map_to(page, frame, PageFlags::new()); result.flush(); } @@ -125,7 +125,7 @@ pub unsafe fn init(active_table: &mut ActivePageTable, already_supplied_rsdps: O rxsdt.map_all(active_table); for sdt_address in rxsdt.iter() { - let sdt = &*((sdt_address + crate::KERNEL_OFFSET) as *const Sdt); + let sdt = &*((sdt_address + crate::PHYS_OFFSET) as *const Sdt); let signature = get_sdt_signature(sdt); if let Some(ref mut ptrs) = *(SDT_POINTERS.write()) { diff --git a/src/context/memory.rs b/src/context/memory.rs index 802c49b..244d51e 100644 --- a/src/context/memory.rs +++ b/src/context/memory.rs @@ -94,9 +94,10 @@ impl UserGrants { // TODO: Alignment (x86_64: 4 KiB, 2 MiB, or 1 GiB). pub fn find_free(&self, size: usize) -> Option { // Get first available hole, but do reserve the page starting from zero as most compiled - // language cannot handle null pointers safely even if they do point to valid memory. If an + // languages cannot handle null pointers safely even if they point to valid memory. If an // application absolutely needs to map the 0th page, they will have to do so explicitly via // MAP_FIXED/MAP_FIXED_NOREPLACE. + // TODO: Allow explicitly allocating guard pages? let (hole_start, hole_size) = self.holes.iter().find(|(hole_offset, hole_size)| size <= if hole_offset.data() == 0 { hole_size.saturating_sub(PAGE_SIZE) } else { **hole_size })?; // Create new region diff --git a/src/syscall/process.rs b/src/syscall/process.rs index 3df8164..243ca48 100644 --- a/src/syscall/process.rs +++ b/src/syscall/process.rs @@ -191,8 +191,8 @@ pub fn clone(flags: CloneFlags, stack_base: usize, info: Option<&CloneInfo>) -> // If not cloning virtual memory, use fmap to re-obtain every grant where possible if !flags.contains(CLONE_VM) { - let grants = Arc::get_mut(&mut grants).ok_or(Error::new(EBUSY))?.get_mut(); - let old_grants = mem::take(grants); + let mut grants = grants.write(); + let old_grants = mem::take(&mut *grants); // TODO: Find some way to do this without having to allocate. @@ -1018,7 +1018,7 @@ pub fn usermode_bootstrap(mut data: Box<[u8]>) -> ! { for (index, page) in grant.pages().enumerate() { let len = if data.len() - index * PAGE_SIZE < PAGE_SIZE { data.len() % PAGE_SIZE } else { PAGE_SIZE }; let frame = active_table.translate_page(page).expect("expected mapped init memory to have a corresponding frame"); - unsafe { ((frame.start_address().data() + crate::KERNEL_OFFSET) as *mut u8).copy_from_nonoverlapping(data.as_ptr().add(index * PAGE_SIZE), len); } + unsafe { ((frame.start_address().data() + crate::PHYS_OFFSET) as *mut u8).copy_from_nonoverlapping(data.as_ptr().add(index * PAGE_SIZE), len); } } context::contexts().current().expect("expected a context to exist when executing init").read().grants.write().insert(grant);