From 638a2215b202fb3c8d8cbdcebeb77d9a629b4cd3 Mon Sep 17 00:00:00 2001 From: WangRunji Date: Fri, 22 Mar 2019 23:58:19 +0800 Subject: [PATCH] impl eager mmap file --- kernel/src/syscall/fs.rs | 2 +- kernel/src/syscall/mem.rs | 24 +++++++++++++++++++++--- kernel/src/syscall/mod.rs | 2 +- 3 files changed, 23 insertions(+), 5 deletions(-) diff --git a/kernel/src/syscall/fs.rs b/kernel/src/syscall/fs.rs index 96b11d5..7fa708c 100644 --- a/kernel/src/syscall/fs.rs +++ b/kernel/src/syscall/fs.rs @@ -588,7 +588,7 @@ pub fn sys_sync() -> SysResult { } impl Process { - fn get_file(&mut self, fd: usize) -> Result<&mut FileHandle, SysError> { + pub fn get_file(&mut self, fd: usize) -> Result<&mut FileHandle, SysError> { self.files.get_mut(&fd).ok_or(SysError::EBADF).and_then(|f| { match f { FileLike::File(file) => Ok(file), diff --git a/kernel/src/syscall/mem.rs b/kernel/src/syscall/mem.rs index f21a9cb..986e56c 100644 --- a/kernel/src/syscall/mem.rs +++ b/kernel/src/syscall/mem.rs @@ -1,4 +1,4 @@ -use rcore_memory::memory_set::handler::Delay; +use rcore_memory::memory_set::handler::{Delay, ByFrame}; use rcore_memory::memory_set::MemoryAttr; use rcore_memory::paging::PageTable; use rcore_memory::Page; @@ -13,7 +13,7 @@ pub fn sys_mmap( len: usize, prot: usize, flags: usize, - fd: i32, + fd: usize, offset: usize, ) -> SysResult { let prot = MmapProt::from_bits_truncate(prot); @@ -50,8 +50,26 @@ pub fn sys_mmap( "mmap", ); return Ok(addr); + } else { + // only check + let _ = proc.get_file(fd)?; + + // TODO: delay mmap file + proc.vm.push( + addr, + addr + len, + prot.to_attr(), + ByFrame::new(GlobalFrameAlloc), + "mmap", + ); + let data = unsafe { slice::from_raw_parts_mut(addr as *mut u8, len) }; + let file = proc.get_file(fd)?; + let read_len = file.read_at(offset, data)?; + if read_len != data.len() { + data[read_len..].iter_mut().map(|x| *x = 0); + } + return Ok(addr); } - unimplemented!() } pub fn sys_mprotect(addr: usize, len: usize, prot: usize) -> SysResult { diff --git a/kernel/src/syscall/mod.rs b/kernel/src/syscall/mod.rs index 09e57ea..6768a1c 100644 --- a/kernel/src/syscall/mod.rs +++ b/kernel/src/syscall/mod.rs @@ -56,7 +56,7 @@ pub fn syscall(id: usize, args: [usize; 6], tf: &mut TrapFrame) -> isize { SYS_CLOSE => sys_close(args[0]), SYS_FSTAT => sys_fstat(args[0], args[1] as *mut Stat), SYS_LSEEK => sys_lseek(args[0], args[1] as i64, args[2] as u8), - SYS_MMAP => sys_mmap(args[0], args[1], args[2], args[3], args[4] as i32, args[5]), + SYS_MMAP => sys_mmap(args[0], args[1], args[2], args[3], args[4], args[5]), SYS_MPROTECT => sys_mprotect(args[0], args[1], args[2]), SYS_MUNMAP => sys_munmap(args[0], args[1]), SYS_PREAD64 => sys_pread(args[0], args[1] as *mut u8, args[2], args[3]),