Merge remote-tracking branch 'course/biscuit' into biscuit

master
WangRunji 6 years ago
commit 2a6c4554d1

@ -224,7 +224,7 @@ impl<T: InactivePageTable> MemorySet<T> {
// try each area's end address as the start
core::iter::once(addr_hint)
.chain(self.areas.iter().map(|area| area.end_addr))
.map(|addr| addr + PAGE_SIZE) // move up a page
.map(|addr| (addr + PAGE_SIZE) & ! PAGE_SIZE) // round up a page
.find(|&addr| self.test_free_area(addr, addr + len))
.expect("failed to find free area ???")
}
@ -245,6 +245,24 @@ impl<T: InactivePageTable> MemorySet<T> {
self.page_table.edit(|pt| area.map(pt));
self.areas.push(area);
}
/*
** @brief remove the memory area to the memory set
** @param area: MemoryArea the memory area to remove
** @retval none
*/
pub fn pop(&mut self, start_addr: VirtAddr, end_addr: VirtAddr) {
assert!(start_addr <= end_addr, "invalid memory area");
for i in 0..self.areas.len() {
if self.areas[i].start_addr == start_addr && self.areas[i].end_addr == end_addr {
let area = self.areas.remove(i);
self.page_table.edit(|pt| area.unmap(pt));
return;
}
}
panic!("no memory area found");
}
/*
** @brief get iterator of the memory area
** @retval impl Iterator<Item=&MemoryArea>

@ -21,7 +21,7 @@ pub enum DeviceType {
Block
}
pub trait Driver : Send + AsAny {
pub trait Driver : Send {
// if interrupt belongs to this driver, handle it and return true
// return false otherwise
fn try_handle_interrupt(&mut self) -> bool;
@ -40,14 +40,6 @@ pub trait NetDriver : Send {
fn poll(&mut self, socket: &mut SocketSet) -> Option<bool>;
}
// little hack, see https://users.rust-lang.org/t/how-to-downcast-from-a-trait-any-to-a-struct/11219/3
pub trait AsAny {
fn as_any(&self) -> &Any;
}
impl<T: Any> AsAny for T {
fn as_any(&self) -> &Any { self }
}
lazy_static! {
pub static ref DRIVERS: SpinNoIrqLock<Vec<Box<Driver>>> = SpinNoIrqLock::new(Vec::new());

@ -51,7 +51,7 @@ impl Thread {
proc: Arc::new(Mutex::new(Process {
memory_set: MemorySet::new(),
files: BTreeMap::default(),
cwd: String::new(),
cwd: String::from("/"),
sockets: SocketSet::new(vec![])
})),
})
@ -67,7 +67,7 @@ impl Thread {
proc: Arc::new(Mutex::new(Process {
memory_set,
files: BTreeMap::default(),
cwd: String::new(),
cwd: String::from("/"),
sockets: SocketSet::new(vec![])
})),
})
@ -146,7 +146,7 @@ impl Thread {
proc: Arc::new(Mutex::new(Process {
memory_set,
files,
cwd: String::new(),
cwd: String::from("/"),
sockets: SocketSet::new(vec![])
})),
})
@ -178,7 +178,7 @@ impl Thread {
proc: Arc::new(Mutex::new(Process {
memory_set,
files: self.proc.lock().files.clone(),
cwd: String::new(),
cwd: self.proc.lock().cwd.clone(),
// TODO: duplicate sockets for child process
sockets: SocketSet::new(vec![])
})),

@ -115,6 +115,21 @@ pub fn sys_close(fd: usize) -> SysResult {
}
}
pub fn sys_getcwd(buf: *mut u8, len: usize) -> SysResult {
info!("getcwd: buf: {:?}, len: {:#x}", buf, len);
let mut proc = process();
if !proc.memory_set.check_mut_array(buf, len) {
return Err(SysError::EFAULT);
}
if proc.cwd.len() + 1 > len {
return Err(SysError::ERANGE);
}
unsafe {
util::write_cstr(buf, &proc.cwd)
}
Ok(0)
}
pub fn sys_stat(path: *const u8, stat_ptr: *mut Stat) -> SysResult {
warn!("stat is partial implemented as lstat");
sys_lstat(path, stat_ptr)

@ -25,7 +25,10 @@ pub fn sys_mmap(mut addr: usize, len: usize, prot: usize, flags: usize, fd: i32,
}
pub fn sys_munmap(addr: usize, len: usize) -> SysResult {
unimplemented!()
info!("munmap addr={:#x}, size={:#x}", addr, len);
let mut proc = process();
proc.memory_set.pop(addr, addr + len);
Ok(0)
}
bitflags! {

@ -72,7 +72,7 @@ pub fn syscall(id: usize, args: [usize; 6], tf: &mut TrapFrame) -> isize {
// 074 => sys_fsync(),
// 076 => sys_trunc(),
// 077 => sys_ftrunc(),
// 079 => sys_getcwd(),
079 => sys_getcwd(args[0] as *mut u8, args[1]),
// 080 => sys_chdir(),
// 082 => sys_rename(),
// 083 => sys_mkdir(),
@ -84,9 +84,11 @@ pub fn syscall(id: usize, args: [usize; 6], tf: &mut TrapFrame) -> isize {
110 => sys_getppid(),
// 133 => sys_mknod(),
141 => sys_set_priority(args[0]),
158 => sys_arch_prctl(args[0] as i32, args[1], tf),
// 160 => sys_setrlimit(),
// 162 => sys_sync(),
// 169 => sys_reboot(),
201 => sys_time(args[0] as *mut u64),
217 => sys_getdents64(args[0], args[1] as *mut LinuxDirent64, args[2]),
// 293 => sys_pipe(),
@ -135,7 +137,6 @@ pub fn syscall(id: usize, args: [usize; 6], tf: &mut TrapFrame) -> isize {
warn!("sys_sigaltstack is unimplemented");
Ok(0)
}
158 => sys_arch_prctl(args[0] as i32, args[1], tf),
218 => {
warn!("sys_set_tid_address is unimplemented");
Ok(thread::current().id() as isize)

@ -4,4 +4,18 @@ use super::*;
pub fn sys_get_time() -> SysResult {
unsafe { Ok(crate::trap::TICK as isize) }
}
}
pub fn sys_time(time: *mut u64) -> SysResult {
let t = unsafe { crate::trap::TICK };
if time as usize != 0 {
let mut proc = process();
if !proc.memory_set.check_mut_ptr(time) {
return Err(SysError::EFAULT);
}
unsafe {
time.write(t as u64);
}
}
Ok(t as isize)
}

Loading…
Cancel
Save