From 452196b81fcf857f5b1af236bc0e5339ff539d01 Mon Sep 17 00:00:00 2001 From: Robin Randhawa Date: Thu, 21 Jan 2021 11:37:32 +0000 Subject: [PATCH 1/7] aarch64: consts: Use the same USER_TLS_SIZE as x86_64 --- src/arch/aarch64/consts.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/arch/aarch64/consts.rs b/src/arch/aarch64/consts.rs index 59c5f0c..a348714 100644 --- a/src/arch/aarch64/consts.rs +++ b/src/arch/aarch64/consts.rs @@ -81,7 +81,8 @@ /// Offset to user TLS pub const USER_TLS_OFFSET: usize = USER_SIGSTACK_OFFSET + PML4_SIZE; pub const USER_TLS_PML4: usize = (USER_TLS_OFFSET & PML4_MASK)/PML4_SIZE; - pub const USER_TLS_SIZE: usize = 64 * 1024; + // Maximum TLS allocated to each PID, should be approximately 8 MB + pub const USER_TLS_SIZE: usize = PML4_SIZE / 65536; /// Offset to user temporary image (used when cloning) pub const USER_TMP_OFFSET: usize = USER_TLS_OFFSET + PML4_SIZE; From 3da345867a7cd92ad2f97ba1182fe551852f228c Mon Sep 17 00:00:00 2001 From: Robin Randhawa Date: Thu, 21 Jan 2021 11:38:46 +0000 Subject: [PATCH 2/7] aarch64: paging: Derive Debug, PartialEq for VirtualAddressType This makes asserts on VirtualAddressType equality possible. --- src/arch/aarch64/paging/mod.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/arch/aarch64/paging/mod.rs b/src/arch/aarch64/paging/mod.rs index 29bf820..43c8d13 100644 --- a/src/arch/aarch64/paging/mod.rs +++ b/src/arch/aarch64/paging/mod.rs @@ -392,6 +392,7 @@ impl PhysicalAddress { #[derive(Copy, Clone, Debug, Eq, Ord, PartialEq, PartialOrd)] pub struct VirtualAddress(usize); +#[derive(Debug, PartialEq)] pub enum VirtualAddressType { User, Kernel From 75870a655feafe7a9d12827e724511f3d44b077d Mon Sep 17 00:00:00 2001 From: Robin Randhawa Date: Thu, 21 Jan 2021 11:40:02 +0000 Subject: [PATCH 3/7] aarch64: context: Add separate kspace and uspace page table getters --- src/context/arch/aarch64.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/context/arch/aarch64.rs b/src/context/arch/aarch64.rs index e759f0c..8637665 100644 --- a/src/context/arch/aarch64.rs +++ b/src/context/arch/aarch64.rs @@ -87,10 +87,14 @@ impl Context { } } - pub fn get_page_table(&self) -> usize { + pub fn get_page_utable(&self) -> usize { self.ttbr0_el1 } + pub fn get_page_ktable(&self) -> usize { + self.ttbr1_el1 + } + pub fn set_fx(&mut self, _address: usize) { } From 65448c2d4875ca5a1f1f58b79d4407c8e1923591 Mon Sep 17 00:00:00 2001 From: Robin Randhawa Date: Thu, 21 Jan 2021 11:41:26 +0000 Subject: [PATCH 4/7] aarch64: context: memory: Grant::map_inactive: Bugfix When mapping one (from) virtual address range to another (to) virtual address range, be mindful of which mapper type to use for each range. Before this, the same mapper type was used for both ranges. This meant that if from and to were different (as in not both kernel virtual addresses or user virtual addresses) then it would appear that either from or to was not mapped previously and the kernel would panic. --- src/context/memory.rs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/context/memory.rs b/src/context/memory.rs index e050467..f61c1e9 100644 --- a/src/context/memory.rs +++ b/src/context/memory.rs @@ -371,7 +371,7 @@ impl Grant { } pub fn map_inactive(from: VirtualAddress, to: VirtualAddress, size: usize, flags: EntryFlags, desc_opt: Option, new_table: &mut InactivePageTable, temporary_page: &mut TemporaryPage) -> Grant { - let mut active_table = match to.get_type() { + let mut active_table = match from.get_type() { VirtualAddressType::User => unsafe { ActivePageTable::new(PageTableType::User) }, VirtualAddressType::Kernel => unsafe { ActivePageTable::new(PageTableType::Kernel) } }; @@ -386,6 +386,11 @@ 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) } + }; + active_table.with(new_table, temporary_page, |mapper| { let start_page = Page::containing_address(to); let end_page = Page::containing_address(VirtualAddress::new(to.data() + size - 1)); From 591775874b6aae8e02cec9fb8eaa770d661c687f Mon Sep 17 00:00:00 2001 From: Robin Randhawa Date: Thu, 21 Jan 2021 11:50:56 +0000 Subject: [PATCH 5/7] ptrace: with_context_memory: use user-space specific page table --- src/ptrace.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ptrace.rs b/src/ptrace.rs index 930b10e..aeea28f 100644 --- a/src/ptrace.rs +++ b/src/ptrace.rs @@ -460,7 +460,7 @@ where F: FnOnce(*mut u8) -> Result<()> let mut active_page_table = unsafe { ActivePageTable::new(PageTableType::User) }; let mut target_page_table = unsafe { - InactivePageTable::from_address(context.arch.get_page_table()) + InactivePageTable::from_address(context.arch.get_page_utable()) }; // Find the physical frames for all pages From 6cacbb47f6457e4d4a40d23b6473ad1596a1129e Mon Sep 17 00:00:00 2001 From: Robin Randhawa Date: Thu, 21 Jan 2021 11:53:07 +0000 Subject: [PATCH 6/7] scheme: user: Use user-space specific pagt table --- src/scheme/user.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/scheme/user.rs b/src/scheme/user.rs index 18fdfc4..8ba0786 100644 --- a/src/scheme/user.rs +++ b/src/scheme/user.rs @@ -123,7 +123,7 @@ impl UserInner { let context_lock = context_weak.upgrade().ok_or(Error::new(ESRCH))?; let mut context = context_lock.write(); - let mut new_table = unsafe { InactivePageTable::from_address(context.arch.get_page_table()) }; + let mut new_table = unsafe { InactivePageTable::from_address(context.arch.get_page_utable()) }; let mut temporary_page = TemporaryPage::new(Page::containing_address(VirtualAddress::new(crate::USER_TMP_GRANT_OFFSET))); let mut grants = context.grants.lock(); @@ -154,7 +154,7 @@ impl UserInner { let context_lock = self.context.upgrade().ok_or(Error::new(ESRCH))?; let mut context = context_lock.write(); - let mut new_table = unsafe { InactivePageTable::from_address(context.arch.get_page_table()) }; + let mut new_table = unsafe { InactivePageTable::from_address(context.arch.get_page_utable()) }; let mut temporary_page = TemporaryPage::new(Page::containing_address(VirtualAddress::new(crate::USER_TMP_GRANT_OFFSET))); let mut grants = context.grants.lock(); From 78d1cd17985d8302dd18a687ab0cb488962659b7 Mon Sep 17 00:00:00 2001 From: Robin Randhawa Date: Thu, 21 Jan 2021 11:53:35 +0000 Subject: [PATCH 7/7] syscall: process: empty: Use user-space specific page table --- src/syscall/process.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/syscall/process.rs b/src/syscall/process.rs index 8518f99..05b66e2 100644 --- a/src/syscall/process.rs +++ b/src/syscall/process.rs @@ -599,7 +599,7 @@ fn empty(context: &mut context::Context, reaping: bool) { if reaping { println!("{}: {}: Grant should not exist: {:?}", context.id.into(), unsafe { ::core::str::from_utf8_unchecked(&context.name.lock()) }, grant); - let mut new_table = unsafe { InactivePageTable::from_address(context.arch.get_page_table()) }; + let mut new_table = unsafe { InactivePageTable::from_address(context.arch.get_page_utable()) }; let mut temporary_page = TemporaryPage::new(Page::containing_address(VirtualAddress::new(crate::USER_TMP_GRANT_OFFSET))); grant.unmap_inactive(&mut new_table, &mut temporary_page);