Use rust user shell instead of kernel shell and fix sys_exec

master
Jiajie Chen 6 years ago
parent 543fb971ed
commit a4c1d1231b

@ -98,6 +98,11 @@ impl Pid {
pub fn get(&self) -> usize { pub fn get(&self) -> usize {
self.0.unwrap() self.0.unwrap()
} }
/// Return whether this pid represents the init process
pub fn is_init(&self) -> bool {
self.0 == Some(0)
}
} }
impl fmt::Display for Pid { impl fmt::Display for Pid {

@ -8,7 +8,7 @@ use crate::process::*;
pub fn run_user_shell() { pub fn run_user_shell() {
//use crate::net::server; //use crate::net::server;
//processor().manager().add(Thread::new_kernel(server, 0), 0); //processor().manager().add(Thread::new_kernel(server, 0), 0);
if let Ok(inode) = ROOT_INODE.lookup("sh") { if let Ok(inode) = ROOT_INODE.lookup("rust/sh") {
println!("Going to user mode shell."); println!("Going to user mode shell.");
println!("Use 'ls' to list available programs."); println!("Use 'ls' to list available programs.");
let data = inode.read_as_vec().unwrap(); let data = inode.read_as_vec().unwrap();

@ -12,8 +12,11 @@ use crate::drivers::SOCKET_ACTIVITY;
use super::*; use super::*;
pub fn sys_read(fd: usize, base: *mut u8, len: usize) -> SysResult { pub fn sys_read(fd: usize, base: *mut u8, len: usize) -> SysResult {
info!("read: fd: {}, base: {:?}, len: {:#x}", fd, base, len);
let mut proc = process(); let mut proc = process();
if !proc.pid.is_init() {
// we trust pid 0 process
info!("read: fd: {}, base: {:?}, len: {:#x}", fd, base, len);
}
proc.memory_set.check_mut_array(base, len)?; proc.memory_set.check_mut_array(base, len)?;
match proc.files.get(&fd) { match proc.files.get(&fd) {
Some(FileLike::File(_)) => sys_read_file(&mut proc, fd, base, len), Some(FileLike::File(_)) => sys_read_file(&mut proc, fd, base, len),
@ -23,8 +26,11 @@ pub fn sys_read(fd: usize, base: *mut u8, len: usize) -> SysResult {
} }
pub fn sys_write(fd: usize, base: *const u8, len: usize) -> SysResult { pub fn sys_write(fd: usize, base: *const u8, len: usize) -> SysResult {
info!("write: fd: {}, base: {:?}, len: {:#x}", fd, base, len);
let mut proc = process(); let mut proc = process();
if !proc.pid.is_init() {
// we trust pid 0 process
info!("write: fd: {}, base: {:?}, len: {:#x}", fd, base, len);
}
proc.memory_set.check_array(base, len)?; proc.memory_set.check_array(base, len)?;
match proc.files.get(&fd) { match proc.files.get(&fd) {

@ -35,7 +35,10 @@ pub fn syscall(id: usize, args: [usize; 6], tf: &mut TrapFrame) -> isize {
process().pid.clone() process().pid.clone()
}; };
let tid = processor().tid(); let tid = processor().tid();
debug!("{}:{}:{} syscall id {} begin", cid, pid, tid, id); if !pid.is_init() {
// we trust pid 0 process
debug!("{}:{}:{} syscall id {} begin", cid, pid, tid, id);
}
let ret = match id { let ret = match id {
// file // file
000 => sys_read(args[0], args[1] as *mut u8, args[2]), 000 => sys_read(args[0], args[1] as *mut u8, args[2]),
@ -211,7 +214,10 @@ pub fn syscall(id: usize, args: [usize; 6], tf: &mut TrapFrame) -> isize {
crate::trap::error(tf); crate::trap::error(tf);
} }
}; };
debug!("{}:{}:{} syscall id {} ret with {:x?}", cid, pid, tid, id, ret); if !pid.is_init() {
// we trust pid 0 process
debug!("{}:{}:{} syscall id {} ret with {:x?}", cid, pid, tid, id, ret);
}
match ret { match ret {
Ok(code) => code as isize, Ok(code) => code as isize,
Err(err) => -(err as isize), Err(err) => -(err as isize),

@ -104,6 +104,7 @@ pub fn sys_exec(name: *const u8, argv: *const *const u8, envp: *const *const u8,
let mut args = Vec::new(); let mut args = Vec::new();
unsafe { unsafe {
let mut current_argv = argv as *const *const u8; let mut current_argv = argv as *const *const u8;
proc.memory_set.check_ptr(current_argv)?;
while !(*current_argv).is_null() { while !(*current_argv).is_null() {
let arg = proc.memory_set.check_and_clone_cstr(*current_argv)?; let arg = proc.memory_set.check_and_clone_cstr(*current_argv)?;
args.push(arg); args.push(arg);
@ -125,6 +126,9 @@ pub fn sys_exec(name: *const u8, argv: *const *const u8, envp: *const *const u8,
let mut thread = Thread::new_user(buf.as_slice(), iter); let mut thread = Thread::new_user(buf.as_slice(), iter);
thread.proc.lock().files = proc.files.clone(); thread.proc.lock().files = proc.files.clone();
thread.proc.lock().cwd = proc.cwd.clone(); thread.proc.lock().cwd = proc.cwd.clone();
thread.proc.lock().pid = proc.pid.clone();
thread.proc.lock().parent = proc.parent.clone();
thread.proc.lock().threads = proc.threads.clone();
// Activate new page table // Activate new page table
unsafe { thread.proc.lock().memory_set.activate(); } unsafe { thread.proc.lock().memory_set.activate(); }

@ -1 +1 @@
Subproject commit bae6866610fc24ca66584c1a5200648d2de7d767 Subproject commit f0441d1fe30022acfadced2224b00ed9a29db455
Loading…
Cancel
Save