From fc98719559ba03c6a93cf9a5603453fa46d2312e Mon Sep 17 00:00:00 2001 From: WangRunji Date: Fri, 22 Feb 2019 23:48:05 +0800 Subject: [PATCH] impl sys_mmap for annonymous --- kernel/src/syscall/mem.rs | 60 +++++++++++++++++++++++++++++++++++++++ kernel/src/syscall/mod.rs | 25 +++++++++++++--- 2 files changed, 81 insertions(+), 4 deletions(-) create mode 100644 kernel/src/syscall/mem.rs diff --git a/kernel/src/syscall/mem.rs b/kernel/src/syscall/mem.rs new file mode 100644 index 0000000..71e8ad3 --- /dev/null +++ b/kernel/src/syscall/mem.rs @@ -0,0 +1,60 @@ +use rcore_memory::memory_set::handler::Delay; +use rcore_memory::memory_set::MemoryAttr; + +use crate::memory::GlobalFrameAlloc; + +use super::*; + +pub fn sys_mmap(mut addr: usize, len: usize, prot: MmapProt, flags: MmapFlags, fd: i32, offset: usize) -> SysResult { + info!("mmap addr={:#x}, size={:#x}, prot={:?}, flags={:?}, fd={}, offset={:#x}", addr, len, prot, flags, fd, offset); + if addr == 0 { + // FIXME: choose an address to place + addr = 0x1000000; + } + if flags.contains(MmapFlags::ANONYMOUS) { + if flags.contains(MmapFlags::SHARED) { + return Err(SysError::Inval); + } + let mut proc = process(); + let handler = Delay::new(prot_to_attr(prot), GlobalFrameAlloc); + proc.memory_set.push(addr, addr + len, handler, "mmap"); + return Ok(addr as isize); + } + unimplemented!() +} + +pub fn sys_munmap(addr: usize, len: usize) -> SysResult { + unimplemented!() +} + +bitflags! { + pub struct MmapProt: usize { + /// Data cannot be accessed + const NONE = 0; + /// Data can be read + const READ = 1 << 0; + /// Data can be written + const WRITE = 1 << 1; + /// Data can be executed + const EXEC = 1 << 2; + } +} + +bitflags! { + pub struct MmapFlags: usize { + /// Changes are shared. + const SHARED = 1 << 0; + /// Changes are private. + const PRIVATE = 1 << 1; + /// The mapping is not backed by any file. (non-POSIX) + const ANONYMOUS = 1 << 5; + } +} + +fn prot_to_attr(prot: MmapProt) -> MemoryAttr { + let mut attr = MemoryAttr::default().user(); + if prot.contains(MmapProt::EXEC) { attr = attr.execute(); } + if !prot.contains(MmapProt::WRITE) { attr = attr.readonly(); } + assert!(prot.contains(MmapProt::READ)); + attr +} \ No newline at end of file diff --git a/kernel/src/syscall/mod.rs b/kernel/src/syscall/mod.rs index d3b0e8a..267d338 100644 --- a/kernel/src/syscall/mod.rs +++ b/kernel/src/syscall/mod.rs @@ -4,7 +4,7 @@ use alloc::{string::String, sync::Arc, vec::Vec}; use core::{slice, str}; use bitflags::bitflags; -use rcore_fs::vfs::{Metadata, FileType, FsError, INode}; +use rcore_fs::vfs::{FileType, FsError, INode, Metadata}; use spin::{Mutex, MutexGuard}; use crate::arch::interrupt::TrapFrame; @@ -14,10 +14,12 @@ use crate::thread; use crate::util; use self::fs::*; +use self::mem::*; use self::proc::*; use self::time::*; mod fs; +mod mem; mod proc; mod time; @@ -33,9 +35,8 @@ pub fn syscall(id: usize, args: [usize; 6], tf: &mut TrapFrame) -> isize { 005 => sys_fstat(args[0], args[1] as *mut Stat), // 007 => sys_poll(), // 008 => sys_lseek(), -// 009 => sys_mmap(), -// 011 => sys_munmap(), -// 013 => sys_sigaction(), + 009 => sys_mmap(args[0], args[1], MmapProt::from_bits_truncate(args[2]), MmapFlags::from_bits_truncate(args[3]), args[4] as i32, args[5]), + 011 => sys_munmap(args[0], args[1]), // 019 => sys_readv(), // 020 => sys_writev(), // 021 => sys_access(), @@ -85,6 +86,22 @@ pub fn syscall(id: usize, args: [usize; 6], tf: &mut TrapFrame) -> isize { // 293 => sys_pipe(), // for musl: empty impl + 012 => { + warn!("sys_brk is unimplemented"); + Ok(0) + } + 013 => { + warn!("sys_sigaction is unimplemented"); + Ok(0) + } + 014 => { + warn!("sys_sigprocmask is unimplemented"); + Ok(0) + } + 131 => { + warn!("sys_sigaltstack is unimplemented"); + Ok(0) + } 158 => { warn!("sys_arch_prctl is unimplemented"); Ok(0)