rename, simplify

master
WangRunji 6 years ago
parent abd93e49e3
commit 5ddd5ece8d

@ -13,7 +13,6 @@ impl<T: FrameAllocator> MemoryHandler for ByFrame<T> {
fn map(&self, pt: &mut PageTable, addr: VirtAddr, attr: &MemoryAttr) {
let target = self.allocator.alloc().expect("failed to allocate frame");
let entry = pt.map(addr, target);
entry.set_present(true);
attr.apply(entry);
}

@ -13,7 +13,6 @@ impl MemoryHandler for Linear {
fn map(&self, pt: &mut PageTable, addr: VirtAddr, attr: &MemoryAttr) {
let target = (addr as isize + self.offset) as PhysAddr;
let entry = pt.map(addr, target);
entry.set_present(true);
attr.apply(entry);
}

@ -48,24 +48,14 @@ impl MemoryArea {
pub fn contains(&self, addr: VirtAddr) -> bool {
addr >= self.start_addr && addr < self.end_addr
}
/// Check the pointer is within the readable memory
pub fn check_ptr<T>(&self, ptr: *const T) -> bool {
self.check_array(ptr, 1)
}
/// Check the pointer is within the writable memory
pub fn check_mut_ptr<T>(&self, ptr: *mut T) -> bool {
self.check_mut_array(ptr, 1)
}
/// Check the array is within the readable memory
pub fn check_array<S>(&self, ptr: *const S, count: usize) -> bool {
fn check_read_array<S>(&self, ptr: *const S, count: usize) -> bool {
ptr as usize >= self.start_addr &&
unsafe { ptr.offset(count as isize) as usize } <= self.end_addr
unsafe { ptr.add(count) as usize } <= self.end_addr
}
/// Check the array is within the writable memory
pub fn check_mut_array<S>(&self, ptr: *mut S, count: usize) -> bool {
!self.attr.readonly &&
ptr as usize >= self.start_addr &&
unsafe { ptr.offset(count as isize) as usize } <= self.end_addr
fn check_write_array<S>(&self, ptr: *mut S, count: usize) -> bool {
!self.attr.readonly && self.check_read_array(ptr, count)
}
/// Check the null-end C string is within the readable memory, and is valid.
/// If so, clone it to a String.
@ -207,27 +197,23 @@ impl<T: InactivePageTable> MemorySet<T> {
}
}
/// Check the pointer is within the readable memory
pub fn check_ptr<S>(&self, ptr: *const S) -> VMResult<()> {
self.areas.iter()
.find(|area| area.check_ptr(ptr))
.map(|_|()).ok_or(VMError::InvalidPtr)
pub fn check_read_ptr<S>(&self, ptr: *const S) -> VMResult<()> {
self.check_read_array(ptr, 1)
}
/// Check the pointer is within the writable memory
pub fn check_mut_ptr<S>(&self, ptr: *mut S) -> VMResult<()> {
self.areas.iter()
.find(|area| area.check_mut_ptr(ptr))
.map(|_|()).ok_or(VMError::InvalidPtr)
pub fn check_write_ptr<S>(&self, ptr: *mut S) -> VMResult<()> {
self.check_write_array(ptr, 1)
}
/// Check the array is within the readable memory
pub fn check_array<S>(&self, ptr: *const S, count: usize) -> VMResult<()> {
pub fn check_read_array<S>(&self, ptr: *const S, count: usize) -> VMResult<()> {
self.areas.iter()
.find(|area| area.check_array(ptr, count))
.find(|area| area.check_read_array(ptr, count))
.map(|_|()).ok_or(VMError::InvalidPtr)
}
/// Check the array is within the writable memory
pub fn check_mut_array<S>(&self, ptr: *mut S, count: usize) -> VMResult<()> {
pub fn check_write_array<S>(&self, ptr: *mut S, count: usize) -> VMResult<()> {
self.areas.iter()
.find(|area| area.check_mut_array(ptr, count))
.find(|area| area.check_write_array(ptr, count))
.map(|_|()).ok_or(VMError::InvalidPtr)
}
/// Check the null-end C string is within the readable memory, and is valid.

@ -43,23 +43,15 @@ pub trait PageTable {
unsafe { (addr as *mut u8).write(data) }
}
/// When 'vaddr' is not mapped, map it to 'paddr'.
/// When `vaddr` is not mapped, map it to `paddr`.
fn map_if_not_exists(&mut self, vaddr: VirtAddr, paddr: usize) -> bool {
let entry = self.get_entry(vaddr);
match entry {
None => {
self.map(vaddr, paddr);
true
},
Some(page_table_entry) => {
if page_table_entry.present() {
false
} else {
self.map(vaddr, paddr);
true
}
if let Some(entry) = self.get_entry(vaddr) {
if entry.present() {
return false;
}
}
self.map(vaddr, paddr);
true
}
}

@ -102,7 +102,7 @@ impl Drop for KernelStack {
#[cfg(not(feature = "no_mmu"))]
pub fn handle_page_fault(addr: usize) -> bool {
debug!("page fault @ {:#x}", addr);
process().memory_set.handle_page_fault(addr)
process().vm.handle_page_fault(addr)
}
pub fn init_heap() {

@ -85,7 +85,7 @@ impl fmt::Display for Pid {
pub struct Process {
// resources
pub memory_set: MemorySet,
pub vm: MemorySet,
pub files: BTreeMap<usize, FileLike>,
pub cwd: String,
futexes: BTreeMap<usize, Arc<Condvar>>,
@ -140,7 +140,7 @@ impl Thread {
kstack: KernelStack::new(),
clear_child_tid: 0,
proc: Arc::new(Mutex::new(Process {
memory_set: MemorySet::new(),
vm: MemorySet::new(),
files: BTreeMap::default(),
cwd: String::from("/"),
futexes: BTreeMap::default(),
@ -156,15 +156,15 @@ impl Thread {
/// Make a new kernel thread starting from `entry` with `arg`
pub fn new_kernel(entry: extern fn(usize) -> !, arg: usize) -> Box<Thread> {
let memory_set = MemorySet::new();
let vm = MemorySet::new();
let kstack = KernelStack::new();
Box::new(Thread {
context: unsafe { Context::new_kernel_thread(entry, arg, kstack.top(), memory_set.token()) },
context: unsafe { Context::new_kernel_thread(entry, arg, kstack.top(), vm.token()) },
kstack,
clear_child_tid: 0,
// TODO: kernel thread should not have a process
proc: Arc::new(Mutex::new(Process {
memory_set,
vm,
files: BTreeMap::default(),
cwd: String::from("/"),
futexes: BTreeMap::default(),
@ -199,7 +199,7 @@ impl Thread {
}
// Make page table
let (mut memory_set, entry_addr) = memory_set_from(&elf);
let (mut vm, entry_addr) = memory_set_from(&elf);
// User stack
use crate::consts::{USER_STACK_OFFSET, USER_STACK_SIZE, USER32_STACK_OFFSET};
@ -209,11 +209,11 @@ impl Thread {
true => (USER32_STACK_OFFSET, USER32_STACK_OFFSET + USER_STACK_SIZE),
false => (USER_STACK_OFFSET, USER_STACK_OFFSET + USER_STACK_SIZE),
};
memory_set.push(ustack_buttom, ustack_top, MemoryAttr::default().user(), ByFrame::new(GlobalFrameAlloc), "user_stack");
vm.push(ustack_buttom, ustack_top, MemoryAttr::default().user(), ByFrame::new(GlobalFrameAlloc), "user_stack");
ustack_top
};
#[cfg(feature = "no_mmu")]
let mut ustack_top = memory_set.push(USER_STACK_SIZE).as_ptr() as usize + USER_STACK_SIZE;
let mut ustack_top = vm.push(USER_STACK_SIZE).as_ptr() as usize + USER_STACK_SIZE;
let init_info = ProcInitInfo {
args: args.map(|s| String::from(s)).collect(),
@ -237,10 +237,10 @@ impl Thread {
},
};
unsafe {
memory_set.with(|| { ustack_top = init_info.push_at(ustack_top) });
vm.with(|| { ustack_top = init_info.push_at(ustack_top) });
}
trace!("{:#x?}", memory_set);
trace!("{:#x?}", vm);
let kstack = KernelStack::new();
@ -252,12 +252,12 @@ impl Thread {
Box::new(Thread {
context: unsafe {
Context::new_user_thread(
entry_addr, ustack_top, kstack.top(), is32, memory_set.token())
entry_addr, ustack_top, kstack.top(), is32, vm.token())
},
kstack,
clear_child_tid: 0,
proc: Arc::new(Mutex::new(Process {
memory_set,
vm,
files,
cwd: String::from("/"),
futexes: BTreeMap::default(),
@ -274,18 +274,18 @@ impl Thread {
/// Fork a new process from current one
pub fn fork(&self, tf: &TrapFrame) -> Box<Thread> {
// Clone memory set, make a new page table
let memory_set = self.proc.lock().memory_set.clone();
let vm = self.proc.lock().vm.clone();
let files = self.proc.lock().files.clone();
let cwd = self.proc.lock().cwd.clone();
let parent = Some(self.proc.clone());
debug!("fork: finish clone MemorySet");
// MMU: copy data to the new space
// NoMMU: coping data has been done in `memory_set.clone()`
// NoMMU: coping data has been done in `vm.clone()`
#[cfg(not(feature = "no_mmu"))]
for area in memory_set.iter() {
for area in vm.iter() {
let data = Vec::<u8>::from(unsafe { area.as_slice() });
unsafe { memory_set.with(|| {
unsafe { vm.with(|| {
area.as_slice_mut().copy_from_slice(data.as_slice())
}) }
}
@ -302,11 +302,11 @@ impl Thread {
Box::new(Thread {
context: unsafe { Context::new_fork(tf, kstack.top(), memory_set.token()) },
context: unsafe { Context::new_fork(tf, kstack.top(), vm.token()) },
kstack,
clear_child_tid: 0,
proc: Arc::new(Mutex::new(Process {
memory_set,
vm,
files,
cwd,
futexes: BTreeMap::default(),
@ -323,7 +323,7 @@ impl Thread {
/// Create a new thread in the same process.
pub fn clone(&self, tf: &TrapFrame, stack_top: usize, tls: usize, clear_child_tid: usize) -> Box<Thread> {
let kstack = KernelStack::new();
let token = self.proc.lock().memory_set.token();
let token = self.proc.lock().vm.token();
Box::new(Thread {
context: unsafe { Context::new_clone(tf, stack_top, kstack.top(), token, tls) },
kstack,

@ -24,9 +24,9 @@ pub fn sys_map_pci_device(vendor: usize, product: usize) -> SysResult {
.ok_or(SysError::ENOENT)?;
let mut proc = process();
let virt_addr = proc.memory_set.find_free_area(0, len);
let virt_addr = proc.vm.find_free_area(0, len);
let attr = MemoryAttr::default().user();
proc.memory_set.push(
proc.vm.push(
virt_addr,
virt_addr + len,
attr,
@ -45,12 +45,12 @@ pub fn sys_map_pci_device(vendor: usize, product: usize) -> SysResult {
/// 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)?;
proc.vm.check_read_array(vaddrs, count)?;
proc.vm.check_write_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);
let paddr = proc.vm.translate(vaddrs[i] as usize).unwrap_or(0);
paddrs[i] = paddr as u64;
}
Ok(0)

@ -17,7 +17,7 @@ pub fn sys_read(fd: usize, base: *mut u8, len: usize) -> SysResult {
// we trust pid 0 process
info!("read: fd: {}, base: {:?}, len: {:#x}", fd, base, len);
}
proc.memory_set.check_mut_array(base, len)?;
proc.vm.check_write_array(base, len)?;
match proc.files.get(&fd) {
Some(FileLike::File(_)) => sys_read_file(&mut proc, fd, base, len),
Some(FileLike::Socket(_)) => sys_read_socket(&mut proc, fd, base, len),
@ -31,7 +31,7 @@ pub fn sys_write(fd: usize, base: *const u8, len: usize) -> SysResult {
// we trust pid 0 process
info!("write: fd: {}, base: {:?}, len: {:#x}", fd, base, len);
}
proc.memory_set.check_array(base, len)?;
proc.vm.check_read_array(base, len)?;
match proc.files.get(&fd) {
Some(FileLike::File(_)) => sys_write_file(&mut proc, fd, base, len),
@ -43,7 +43,7 @@ pub fn sys_write(fd: usize, base: *const u8, len: usize) -> SysResult {
pub fn sys_pread(fd: usize, base: *mut u8, len: usize, offset: usize) -> SysResult {
info!("pread: fd: {}, base: {:?}, len: {}, offset: {}", fd, base, len, offset);
let mut proc = process();
proc.memory_set.check_mut_array(base, len)?;
proc.vm.check_write_array(base, len)?;
let slice = unsafe { slice::from_raw_parts_mut(base, len) };
let len = proc.get_file(fd)?.read_at(offset, slice)?;
@ -53,7 +53,7 @@ pub fn sys_pread(fd: usize, base: *mut u8, len: usize, offset: usize) -> SysResu
pub fn sys_pwrite(fd: usize, base: *const u8, len: usize, offset: usize) -> SysResult {
info!("pwrite: fd: {}, base: {:?}, len: {}, offset: {}", fd, base, len, offset);
let mut proc = process();
proc.memory_set.check_array(base, len)?;
proc.vm.check_read_array(base, len)?;
let slice = unsafe { slice::from_raw_parts(base, len) };
let len = proc.get_file(fd)?.write_at(offset, slice)?;
@ -75,7 +75,7 @@ pub fn sys_write_file(proc: &mut Process, fd: usize, base: *const u8, len: usize
pub fn sys_poll(ufds: *mut PollFd, nfds: usize, timeout_msecs: usize) -> SysResult {
info!("poll: ufds: {:?}, nfds: {}, timeout_msecs: {:#x}", ufds, nfds, timeout_msecs);
let proc = process();
proc.memory_set.check_mut_array(ufds, nfds)?;
proc.vm.check_write_array(ufds, nfds)?;
let polls = unsafe { slice::from_raw_parts_mut(ufds, nfds) };
for poll in polls.iter() {
@ -148,11 +148,11 @@ struct FdSet {
impl FdSet {
/// Initialize a `FdSet` from pointer and number of fds
/// Check if the array is large enough
fn new(proc: &Process, addr: *mut u32, nfds: usize) -> Result<FdSet, SysError> {
fn new(vm: &MemorySet, addr: *mut u32, nfds: usize) -> Result<FdSet, SysError> {
let mut saved = [0u32; MAX_FDSET_SIZE];
if addr as usize != 0 {
let len = (nfds + FD_PER_ITEM - 1) / FD_PER_ITEM;
proc.memory_set.check_mut_array(addr, len)?;
vm.check_write_array(addr, len)?;
if len > MAX_FDSET_SIZE {
return Err(SysError::EINVAL);
}
@ -199,11 +199,11 @@ pub fn sys_select(nfds: usize, read: *mut u32, write: *mut u32, err: *mut u32, t
info!("select: nfds: {}, read: {:?}, write: {:?}, err: {:?}, timeout: {:?}", nfds, read, write, err, timeout);
let proc = process();
let mut read_fds = FdSet::new(&proc, read, nfds)?;
let mut write_fds = FdSet::new(&proc, write, nfds)?;
let mut err_fds = FdSet::new(&proc, err, nfds)?;
let mut read_fds = FdSet::new(&proc.vm, read, nfds)?;
let mut write_fds = FdSet::new(&proc.vm, write, nfds)?;
let mut err_fds = FdSet::new(&proc.vm, err, nfds)?;
let timeout_msecs = if timeout as usize != 0 {
proc.memory_set.check_ptr(timeout)?;
proc.vm.check_read_ptr(timeout)?;
unsafe { *timeout }.to_msec()
} else {
// infinity
@ -269,7 +269,7 @@ pub fn sys_select(nfds: usize, read: *mut u32, write: *mut u32, err: *mut u32, t
pub fn sys_readv(fd: usize, iov_ptr: *const IoVec, iov_count: usize) -> SysResult {
info!("readv: fd: {}, iov: {:?}, count: {}", fd, iov_ptr, iov_count);
let mut proc = process();
let mut iovs = IoVecs::check_and_new(iov_ptr, iov_count, &proc.memory_set, true)?;
let mut iovs = IoVecs::check_and_new(iov_ptr, iov_count, &proc.vm, true)?;
// read all data to a buf
let mut file = proc.get_file(fd)?.clone();
@ -284,7 +284,7 @@ pub fn sys_readv(fd: usize, iov_ptr: *const IoVec, iov_count: usize) -> SysResul
pub fn sys_writev(fd: usize, iov_ptr: *const IoVec, iov_count: usize) -> SysResult {
info!("writev: fd: {}, iov: {:?}, count: {}", fd, iov_ptr, iov_count);
let mut proc = process();
let iovs = IoVecs::check_and_new(iov_ptr, iov_count, &proc.memory_set, false)?;
let iovs = IoVecs::check_and_new(iov_ptr, iov_count, &proc.vm, false)?;
let buf = iovs.read_all_to_vec();
let len = buf.len();
@ -298,7 +298,7 @@ pub fn sys_writev(fd: usize, iov_ptr: *const IoVec, iov_count: usize) -> SysResu
pub fn sys_open(path: *const u8, flags: usize, mode: usize) -> SysResult {
let mut proc = process();
let path = unsafe { proc.memory_set.check_and_clone_cstr(path)? };
let path = unsafe { proc.vm.check_and_clone_cstr(path)? };
let flags = OpenFlags::from_bits_truncate(flags);
info!("open: path: {:?}, flags: {:?}, mode: {:#o}", path, flags, mode);
@ -345,7 +345,7 @@ pub fn sys_access(path: *const u8, mode: usize) -> SysResult {
pub fn sys_getcwd(buf: *mut u8, len: usize) -> SysResult {
info!("getcwd: buf: {:?}, len: {:#x}", buf, len);
let proc = process();
proc.memory_set.check_mut_array(buf, len)?;
proc.vm.check_write_array(buf, len)?;
if proc.cwd.len() + 1 > len {
return Err(SysError::ERANGE);
}
@ -361,9 +361,9 @@ pub fn sys_stat(path: *const u8, stat_ptr: *mut Stat) -> SysResult {
}
pub fn sys_fstat(fd: usize, stat_ptr: *mut Stat) -> SysResult {
info!("fstat: fd: {}", fd);
info!("fstat: fd: {}, stat_ptr: {:?}", fd, stat_ptr);
let mut proc = process();
proc.memory_set.check_mut_ptr(stat_ptr)?;
proc.vm.check_write_ptr(stat_ptr)?;
let file = proc.get_file(fd)?;
let stat = Stat::from(file.metadata()?);
// TODO: handle symlink
@ -373,9 +373,9 @@ pub fn sys_fstat(fd: usize, stat_ptr: *mut Stat) -> SysResult {
pub fn sys_lstat(path: *const u8, stat_ptr: *mut Stat) -> SysResult {
let proc = process();
let path = unsafe { proc.memory_set.check_and_clone_cstr(path)? };
proc.memory_set.check_mut_ptr(stat_ptr)?;
info!("lstat: path: {}", path);
let path = unsafe { proc.vm.check_and_clone_cstr(path)? };
proc.vm.check_write_ptr(stat_ptr)?;
info!("lstat: path: {:?}, stat_ptr: {:?}", path, stat_ptr);
let inode = proc.lookup_inode(&path)?;
let stat = Stat::from(inode.metadata()?);
@ -412,7 +412,7 @@ pub fn sys_fdatasync(fd: usize) -> SysResult {
pub fn sys_truncate(path: *const u8, len: usize) -> SysResult {
let proc = process();
let path = unsafe { proc.memory_set.check_and_clone_cstr(path)? };
let path = unsafe { proc.vm.check_and_clone_cstr(path)? };
info!("truncate: path: {:?}, len: {}", path, len);
proc.lookup_inode(&path)?.resize(len)?;
Ok(0)
@ -427,7 +427,7 @@ pub fn sys_ftruncate(fd: usize, len: usize) -> SysResult {
pub fn sys_getdents64(fd: usize, buf: *mut LinuxDirent64, buf_size: usize) -> SysResult {
info!("getdents64: fd: {}, ptr: {:?}, buf_size: {}", fd, buf, buf_size);
let mut proc = process();
proc.memory_set.check_mut_array(buf as *mut u8, buf_size)?;
proc.vm.check_write_array(buf as *mut u8, buf_size)?;
let file = proc.get_file(fd)?;
let info = file.metadata()?;
if info.type_ != FileType::Dir {
@ -468,7 +468,7 @@ pub fn sys_dup2(fd1: usize, fd2: usize) -> SysResult {
pub fn sys_chdir(path: *const u8) -> SysResult {
let mut proc = process();
let path = unsafe { proc.memory_set.check_and_clone_cstr(path)? };
let path = unsafe { proc.vm.check_and_clone_cstr(path)? };
info!("chdir: path: {:?}", path);
let inode = proc.lookup_inode(&path)?;
@ -489,8 +489,8 @@ pub fn sys_chdir(path: *const u8) -> SysResult {
pub fn sys_rename(oldpath: *const u8, newpath: *const u8) -> SysResult {
let proc = process();
let oldpath = unsafe { proc.memory_set.check_and_clone_cstr(oldpath)? };
let newpath = unsafe { proc.memory_set.check_and_clone_cstr(newpath)? };
let oldpath = unsafe { proc.vm.check_and_clone_cstr(oldpath)? };
let newpath = unsafe { proc.vm.check_and_clone_cstr(newpath)? };
info!("rename: oldpath: {:?}, newpath: {:?}", oldpath, newpath);
let (old_dir_path, old_file_name) = split_path(&oldpath);
@ -503,7 +503,7 @@ pub fn sys_rename(oldpath: *const u8, newpath: *const u8) -> SysResult {
pub fn sys_mkdir(path: *const u8, mode: usize) -> SysResult {
let proc = process();
let path = unsafe { proc.memory_set.check_and_clone_cstr(path)? };
let path = unsafe { proc.vm.check_and_clone_cstr(path)? };
// TODO: check pathname
info!("mkdir: path: {:?}, mode: {:#o}", path, mode);
@ -518,7 +518,7 @@ pub fn sys_mkdir(path: *const u8, mode: usize) -> SysResult {
pub fn sys_rmdir(path: *const u8) -> SysResult {
let proc = process();
let path = unsafe { proc.memory_set.check_and_clone_cstr(path)? };
let path = unsafe { proc.vm.check_and_clone_cstr(path)? };
info!("rmdir: path: {:?}", path);
let (dir_path, file_name) = split_path(&path);
@ -533,8 +533,8 @@ pub fn sys_rmdir(path: *const u8) -> SysResult {
pub fn sys_link(oldpath: *const u8, newpath: *const u8) -> SysResult {
let proc = process();
let oldpath = unsafe { proc.memory_set.check_and_clone_cstr(oldpath)? };
let newpath = unsafe { proc.memory_set.check_and_clone_cstr(newpath)? };
let oldpath = unsafe { proc.vm.check_and_clone_cstr(oldpath)? };
let newpath = unsafe { proc.vm.check_and_clone_cstr(newpath)? };
info!("link: oldpath: {:?}, newpath: {:?}", oldpath, newpath);
let (new_dir_path, new_file_name) = split_path(&newpath);
@ -546,7 +546,7 @@ pub fn sys_link(oldpath: *const u8, newpath: *const u8) -> SysResult {
pub fn sys_unlink(path: *const u8) -> SysResult {
let proc = process();
let path = unsafe { proc.memory_set.check_and_clone_cstr(path)? };
let path = unsafe { proc.vm.check_and_clone_cstr(path)? };
info!("unlink: path: {:?}", path);
let (dir_path, file_name) = split_path(&path);
@ -563,7 +563,7 @@ pub fn sys_pipe(fds: *mut u32) -> SysResult {
info!("pipe: fds: {:?}", fds);
let mut proc = process();
proc.memory_set.check_mut_array(fds, 2)?;
proc.vm.check_write_array(fds, 2)?;
let (read, write) = Pipe::create_pair();
let read_fd = proc.get_free_fd();
@ -940,16 +940,16 @@ struct IoVecs(Vec<&'static mut [u8]>);
impl IoVecs {
fn check_and_new(iov_ptr: *const IoVec, iov_count: usize, vm: &MemorySet, readv: bool) -> Result<Self, SysError> {
vm.check_array(iov_ptr, iov_count)?;
vm.check_read_array(iov_ptr, iov_count)?;
let iovs = unsafe { slice::from_raw_parts(iov_ptr, iov_count) }.to_vec();
// check all bufs in iov
for iov in iovs.iter() {
if iov.len > 0 {
// skip empty iov
if readv {
vm.check_mut_array(iov.base, iov.len as usize)?;
vm.check_write_array(iov.base, iov.len as usize)?;
} else {
vm.check_array(iov.base, iov.len as usize)?;
vm.check_read_array(iov.base, iov.len as usize)?;
}
}
}

@ -33,16 +33,16 @@ pub fn sys_mmap(
if flags.contains(MmapFlags::FIXED) {
// we have to map it to addr, so remove the old mapping first
proc.memory_set.pop_with_split(addr, addr + len);
proc.vm.pop_with_split(addr, addr + len);
} else {
addr = proc.memory_set.find_free_area(addr, len);
addr = proc.vm.find_free_area(addr, len);
}
if flags.contains(MmapFlags::ANONYMOUS) {
if flags.contains(MmapFlags::SHARED) {
return Err(SysError::EINVAL);
}
proc.memory_set.push(
proc.vm.push(
addr,
addr + len,
prot.to_attr(),
@ -66,11 +66,11 @@ pub fn sys_mprotect(addr: usize, len: usize, prot: usize) -> SysResult {
// FIXME: properly set the attribute of the area
// now some mut ptr check is fault
let memory_area = proc.memory_set.iter().find(|area| area.contains(addr));
let memory_area = proc.vm.iter().find(|area| area.contains(addr));
if memory_area.is_none() {
return Err(SysError::ENOMEM);
}
proc.memory_set.edit(|pt| {
proc.vm.edit(|pt| {
for page in Page::range_of(addr, addr + len) {
let entry = pt
.get_entry(page.start_address())
@ -84,7 +84,7 @@ pub fn sys_mprotect(addr: usize, len: usize, prot: usize) -> SysResult {
pub fn sys_munmap(addr: usize, len: usize) -> SysResult {
info!("munmap addr={:#x}, size={:#x}", addr, len);
let mut proc = process();
proc.memory_set.pop_with_split(addr, addr + len);
proc.vm.pop_with_split(addr, addr + len);
Ok(0)
}

@ -22,8 +22,8 @@ pub fn sys_uname(buf: *mut u8) -> SysResult {
let offset = 65;
let strings = ["rCore", "orz", "0.1.0", "1", "machine", "domain"];
let proc = process();
proc.memory_set
.check_mut_array(buf, strings.len() * offset)?;
proc.vm
.check_write_array(buf, strings.len() * offset)?;
for i in 0..strings.len() {
unsafe {
@ -39,8 +39,8 @@ pub fn sys_sched_getaffinity(pid: usize, size: usize, mask: *mut u32) -> SysResu
pid, size, mask
);
let proc = process();
proc.memory_set
.check_mut_array(mask, size / size_of::<u32>())?;
proc.vm
.check_write_array(mask, size / size_of::<u32>())?;
// we only have 4 cpu at most.
// so just set it.
@ -52,7 +52,7 @@ pub fn sys_sched_getaffinity(pid: usize, size: usize, mask: *mut u32) -> SysResu
pub fn sys_sysinfo(sys_info: *mut SysInfo) -> SysResult {
let proc = process();
proc.memory_set.check_mut_ptr(sys_info)?;
proc.vm.check_write_ptr(sys_info)?;
let sysinfo = SysInfo::default();
unsafe { *sys_info = sysinfo };
@ -76,13 +76,13 @@ pub fn sys_futex(uaddr: usize, op: u32, val: i32, timeout: *const TimeSpec) -> S
return Err(SysError::EINVAL);
}
process()
.memory_set
.check_mut_ptr(uaddr as *mut AtomicI32)?;
.vm
.check_write_ptr(uaddr as *mut AtomicI32)?;
let atomic = unsafe { &mut *(uaddr as *mut AtomicI32) };
let timeout = if timeout.is_null() {
None
} else {
process().memory_set.check_ptr(timeout)?;
process().vm.check_read_ptr(timeout)?;
Some(unsafe { *timeout })
};

@ -141,11 +141,11 @@ pub fn sys_getsockopt(
fd, level, optname, optval, optlen
);
let proc = process();
proc.memory_set.check_mut_ptr(optlen)?;
proc.vm.check_write_ptr(optlen)?;
match level {
SOL_SOCKET => match optname {
SO_SNDBUF => {
proc.memory_set.check_mut_array(optval, 4)?;
proc.vm.check_write_array(optval, 4)?;
unsafe {
*(optval as *mut u32) = TCP_SENDBUF as u32;
*optlen = 4;
@ -153,7 +153,7 @@ pub fn sys_getsockopt(
Ok(0)
}
SO_RCVBUF => {
proc.memory_set.check_mut_array(optval, 4)?;
proc.vm.check_write_array(optval, 4)?;
unsafe {
*(optval as *mut u32) = TCP_RECVBUF as u32;
*optlen = 4;
@ -269,7 +269,7 @@ pub fn sys_sendto(
);
let mut proc = process();
proc.memory_set.check_array(base, len)?;
proc.vm.check_read_array(base, len)?;
let wrapper = proc.get_socket(fd)?;
let endpoint = sockaddr_to_endpoint(&mut proc, addr, addr_len)?;
@ -291,7 +291,7 @@ pub fn sys_recvfrom(
);
let mut proc = process();
proc.memory_set.check_mut_array(base, len)?;
proc.vm.check_write_array(base, len)?;
let wrapper = proc.get_socket(fd)?;
let mut slice = unsafe { slice::from_raw_parts_mut(base, len) };
@ -674,7 +674,7 @@ fn sockaddr_to_endpoint(
if len < size_of::<u16>() {
return Err(SysError::EINVAL);
}
proc.memory_set.check_array(addr as *const u8, len)?;
proc.vm.check_read_array(addr as *const u8, len)?;
unsafe {
match (*addr).family as usize {
AF_INET => {
@ -707,7 +707,7 @@ impl SockAddr {
return Ok(0);
}
proc.memory_set.check_mut_ptr(addr_len)?;
proc.vm.check_write_ptr(addr_len)?;
let max_addr_len = *addr_len as usize;
let full_len = match self.family as usize {
AF_INET => size_of::<u16>() + size_of::<SockAddrIn>(),
@ -717,8 +717,7 @@ impl SockAddr {
let written_len = min(max_addr_len, full_len);
if written_len > 0 {
proc.memory_set
.check_mut_array(addr as *mut u8, written_len)?;
proc.vm.check_write_array(addr as *mut u8, written_len)?;
let source = slice::from_raw_parts(&self as *const SockAddr as *const u8, written_len);
let target = slice::from_raw_parts_mut(addr as *mut u8, written_len);
target.copy_from_slice(source);

@ -29,8 +29,8 @@ pub fn sys_clone(flags: usize, newsp: usize, parent_tid: *mut u32, child_tid: *m
}
{
let proc = process();
proc.memory_set.check_mut_ptr(parent_tid)?;
proc.memory_set.check_mut_ptr(child_tid)?;
proc.vm.check_write_ptr(parent_tid)?;
proc.vm.check_write_ptr(child_tid)?;
}
let new_thread = current_thread().clone(tf, newsp, newtls, child_tid as usize);
// FIXME: parent pid
@ -48,7 +48,7 @@ pub fn sys_clone(flags: usize, newsp: usize, parent_tid: *mut u32, child_tid: *m
pub fn sys_wait4(pid: isize, wstatus: *mut i32) -> SysResult {
info!("wait4: pid: {}, code: {:?}", pid, wstatus);
if !wstatus.is_null() {
process().memory_set.check_mut_ptr(wstatus)?;
process().vm.check_write_ptr(wstatus)?;
}
#[derive(Debug)]
enum WaitFor {
@ -56,7 +56,7 @@ pub fn sys_wait4(pid: isize, wstatus: *mut i32) -> SysResult {
Pid(usize),
}
let target = match pid {
-1 => WaitFor::AnyChild,
-1 | 0 => WaitFor::AnyChild,
p if p > 0 => WaitFor::Pid(p as usize),
_ => unimplemented!(),
};
@ -99,7 +99,7 @@ pub fn sys_exec(name: *const u8, argv: *const *const u8, envp: *const *const u8,
info!("exec: name: {:?}, argv: {:?} envp: {:?}", name, argv, envp);
let proc = process();
let name = if name.is_null() { String::from("") } else {
unsafe { proc.memory_set.check_and_clone_cstr(name)? }
unsafe { proc.vm.check_and_clone_cstr(name)? }
};
if argv.is_null() {
@ -109,9 +109,9 @@ pub fn sys_exec(name: *const u8, argv: *const *const u8, envp: *const *const u8,
let mut args = Vec::new();
unsafe {
let mut current_argv = argv as *const *const u8;
proc.memory_set.check_ptr(current_argv)?;
proc.vm.check_read_ptr(current_argv)?;
while !(*current_argv).is_null() {
let arg = proc.memory_set.check_and_clone_cstr(*current_argv)?;
let arg = proc.vm.check_and_clone_cstr(*current_argv)?;
args.push(arg);
current_argv = current_argv.add(1);
}
@ -129,7 +129,7 @@ pub fn sys_exec(name: *const u8, argv: *const *const u8, envp: *const *const u8,
thread.proc.lock().clone_for_exec(&proc);
// Activate new page table
unsafe { thread.proc.lock().memory_set.activate(); }
unsafe { thread.proc.lock().vm.activate(); }
// Modify the TrapFrame
*tf = unsafe { thread.context.get_init_tf() };
@ -259,7 +259,7 @@ pub fn sys_exit_group(exit_code: usize) -> ! {
}
pub fn sys_nanosleep(req: *const TimeSpec) -> SysResult {
process().memory_set.check_ptr(req)?;
process().vm.check_read_ptr(req)?;
let time = unsafe { req.read() };
info!("nanosleep: time: {:#?}", time);
thread::sleep(time.to_duration());

@ -78,7 +78,7 @@ pub fn sys_gettimeofday(tv: *mut TimeVal, tz: *const u8) -> SysResult {
}
let proc = process();
proc.memory_set.check_mut_ptr(tv)?;
proc.vm.check_write_ptr(tv)?;
let timeval = TimeVal::get_epoch();
unsafe {
@ -91,7 +91,7 @@ pub fn sys_clock_gettime(clock: usize, ts: *mut TimeSpec) -> SysResult {
info!("clock_gettime: clock: {:?}, ts: {:?}", clock, ts);
let proc = process();
proc.memory_set.check_mut_ptr(ts)?;
proc.vm.check_write_ptr(ts)?;
let timespec = TimeSpec::get_epoch();
unsafe {
@ -104,7 +104,7 @@ pub fn sys_time(time: *mut u64) -> SysResult {
let sec = get_epoch_usec() / USEC_PER_SEC;
if time as usize != 0 {
let proc = process();
proc.memory_set.check_mut_ptr(time)?;
proc.vm.check_write_ptr(time)?;
unsafe {
time.write(sec as u64);
}
@ -122,7 +122,7 @@ pub struct RUsage {
pub fn sys_getrusage(who: usize, rusage: *mut RUsage) -> SysResult {
info!("getrusage: who: {}, rusage: {:?}", who, rusage);
let proc = process();
proc.memory_set.check_mut_ptr(rusage)?;
proc.vm.check_write_ptr(rusage)?;
let tick_base = *TICK_BASE;
let tick = unsafe { crate::trap::TICK as u64 };

Loading…
Cancel
Save