From c6c76147d6be6f5beb29e3fd51ccdde3a63f0fac Mon Sep 17 00:00:00 2001 From: WangRunji Date: Sat, 16 Mar 2019 21:24:41 +0800 Subject: [PATCH] impl custom syscall: sys_get_paddr --- crate/memory/src/memory_set/mod.rs | 13 +++++++++++++ kernel/src/memory.rs | 4 ++-- kernel/src/syscall/custom.rs | 15 +++++++++++++++ kernel/src/syscall/mod.rs | 1 + 4 files changed, 31 insertions(+), 2 deletions(-) diff --git a/crate/memory/src/memory_set/mod.rs b/crate/memory/src/memory_set/mod.rs index 46c2ae9..3b75a91 100644 --- a/crate/memory/src/memory_set/mod.rs +++ b/crate/memory/src/memory_set/mod.rs @@ -369,6 +369,19 @@ impl MemorySet { areas.clear(); } + /// Get physical address of the page of given virtual `addr` + pub fn translate(&mut self, addr: VirtAddr) -> Option { + self.page_table.edit(|pt| { + pt.get_entry(addr).and_then(|entry| { + if entry.user() { + Some(entry.target()) + } else { + None + } + }) + }) + } + /* ** @brief get the mutable reference for the inactive page table ** @retval: &mut T the mutable reference of the inactive page table diff --git a/kernel/src/memory.rs b/kernel/src/memory.rs index fc35d30..a133c0e 100644 --- a/kernel/src/memory.rs +++ b/kernel/src/memory.rs @@ -16,9 +16,9 @@ pub type MemorySet = rcore_memory::memory_set::MemorySet; #[cfg(feature = "no_mmu")] pub type MemorySet = rcore_memory::no_mmu::MemorySet; -// x86_64 support up to 256M memory +// x86_64 support up to 64G memory #[cfg(target_arch = "x86_64")] -pub type FrameAlloc = bit_allocator::BitAlloc64K; +pub type FrameAlloc = bit_allocator::BitAlloc16M; // RISCV has 8M memory #[cfg(any(target_arch = "riscv32", target_arch = "riscv64"))] diff --git a/kernel/src/syscall/custom.rs b/kernel/src/syscall/custom.rs index 8af1adf..e23f8e6 100644 --- a/kernel/src/syscall/custom.rs +++ b/kernel/src/syscall/custom.rs @@ -33,3 +33,18 @@ pub fn sys_map_pci_device(vendor: usize, product: usize) -> SysResult { pub fn sys_map_pci_device(vendor: usize, product: usize) -> SysResult { Err(SysError::ENOSYS) } + +/// Get start physical addresses of frames +/// mapped to a list of virtual addresses. +pub fn sys_get_paddr(vaddrs: *const u64, paddrs: *mut u64, count: usize) -> SysResult { + let mut proc = process(); + proc.memory_set.check_array(vaddrs, count)?; + proc.memory_set.check_mut_array(paddrs, count)?; + let vaddrs = unsafe { slice::from_raw_parts(vaddrs, count) }; + let paddrs = unsafe { slice::from_raw_parts_mut(paddrs, count) }; + for i in 0..count { + let paddr = proc.memory_set.translate(vaddrs[i] as usize).unwrap_or(0); + paddrs[i] = paddr as u64; + } + Ok(0) +} diff --git a/kernel/src/syscall/mod.rs b/kernel/src/syscall/mod.rs index e4ae899..ae897c7 100644 --- a/kernel/src/syscall/mod.rs +++ b/kernel/src/syscall/mod.rs @@ -128,6 +128,7 @@ pub fn syscall(id: usize, args: [usize; 6], tf: &mut TrapFrame) -> isize { 288 => sys_accept(args[0], args[1] as *mut SockAddr, args[2] as *mut u32), // use accept for accept4 // custom temporary syscall 999 => sys_map_pci_device(args[0], args[1]), + 998 => sys_get_paddr(args[0] as *const u64, args[1] as *mut u64, args[2]), // 293 => sys_pipe(), // for musl: empty impl