1ab5c8ca4Slogin use core::{ 2ab5c8ca4Slogin ffi::{c_char, c_int, c_void, CStr}, 3ab5c8ca4Slogin sync::atomic::{AtomicBool, Ordering}, 4ab5c8ca4Slogin }; 5ab5c8ca4Slogin 6971462beSGnoCiYeH use crate::{ 740609970SGnoCiYeH arch::{ipc::signal::SigSet, syscall::nr::*}, 8c566df45SLoGin driver::base::device::device_number::DeviceNumber, 9971462beSGnoCiYeH libs::{futex::constant::FutexFlag, rand::GRandFlags}, 104cfa009bSJomo mm::syscall::MremapFlags, 114ad52e57S裕依2439 net::syscall::MsgHdr, 120d9b7d92SLoGin process::{ 130d9b7d92SLoGin fork::KernelCloneArgs, 140d9b7d92SLoGin resource::{RLimit64, RUsage}, 15*52da9a59SGnoCiYeH ProcessManager, 160d9b7d92SLoGin }, 17971462beSGnoCiYeH }; 1840314b30SXiaoye Zheng 1991e9d4abSLoGin use num_traits::FromPrimitive; 2091e9d4abSLoGin use system_error::SystemError; 21676b8ef6SMork 22ab5c8ca4Slogin use crate::{ 231496ba7bSLoGin arch::{cpu::cpu_reset, interrupt::TrapFrame, MMArch}, 244fda81ceSLoGin driver::base::block::SeekFrom, 25ab5c8ca4Slogin filesystem::vfs::{ 266d81180bSLoGin fcntl::FcntlCommand, 27ab5c8ca4Slogin file::FileMode, 282dbef785SGnoCiYeH syscall::{ModeType, PosixKstat, SEEK_CUR, SEEK_END, SEEK_MAX, SEEK_SET}, 29ab5c8ca4Slogin MAX_PATHLEN, 30ab5c8ca4Slogin }, 311496ba7bSLoGin include::bindings::bindings::{PAGE_2M_SIZE, PAGE_4K_SIZE}, 32aa0367d6SLoGin kinfo, 3340fe15e0SLoGin libs::align::page_align_up, 34be63f3b2Shoumkh mm::{verify_area, MemoryManagementArch, VirtAddr}, 35ab5c8ca4Slogin net::syscall::SockAddr, 36971462beSGnoCiYeH process::{fork::CloneFlags, Pid}, 3736fd0130Shoumkh time::{ 3840fe15e0SLoGin syscall::{PosixTimeZone, PosixTimeval}, 3936fd0130Shoumkh TimeSpec, 4036fd0130Shoumkh }, 41ab5c8ca4Slogin }; 42ab5c8ca4Slogin 43bf4a4899SLoGin use self::{ 44bf4a4899SLoGin misc::SysInfo, 45bf4a4899SLoGin user_access::{UserBufferReader, UserBufferWriter}, 46bf4a4899SLoGin }; 474537ffb7SChiichen 48bf4a4899SLoGin pub mod misc; 4940fe15e0SLoGin pub mod user_access; 5040fe15e0SLoGin 51ad1d649eSGnoCiYeH // 与linux不一致的调用,在linux基础上累加 52ad1d649eSGnoCiYeH pub const SYS_PUT_STRING: usize = 100000; 53ad1d649eSGnoCiYeH pub const SYS_SBRK: usize = 100001; 54ab5c8ca4Slogin /// todo: 该系统调用与Linux不一致,将来需要删除该系统调用!!! 删的时候记得改C版本的libc 55ad1d649eSGnoCiYeH pub const SYS_CLOCK: usize = 100002; 56ad1d649eSGnoCiYeH pub const SYS_SCHED: usize = 100003; 5740314b30SXiaoye Zheng 58ab5c8ca4Slogin #[derive(Debug)] 59ab5c8ca4Slogin pub struct Syscall; 60ab5c8ca4Slogin 61ab5c8ca4Slogin extern "C" { 62ab5c8ca4Slogin fn do_put_string(s: *const u8, front_color: u32, back_color: u32) -> usize; 63ab5c8ca4Slogin } 64ab5c8ca4Slogin 65ab5c8ca4Slogin impl Syscall { 66ab5c8ca4Slogin /// 初始化系统调用 675b59005fSLoGin #[inline(never)] 68ab5c8ca4Slogin pub fn init() -> Result<(), SystemError> { 69ab5c8ca4Slogin static INIT_FLAG: AtomicBool = AtomicBool::new(false); 70ab5c8ca4Slogin let prev = INIT_FLAG.swap(true, Ordering::SeqCst); 71ab5c8ca4Slogin if prev { 72ab5c8ca4Slogin panic!("Cannot initialize syscall more than once!"); 73ab5c8ca4Slogin } 745b59005fSLoGin kinfo!("Initializing syscall..."); 755b59005fSLoGin let r = crate::arch::syscall::arch_syscall_init(); 765b59005fSLoGin kinfo!("Syscall init successfully!"); 775b59005fSLoGin 785b59005fSLoGin return r; 79ab5c8ca4Slogin } 80ab5c8ca4Slogin /// @brief 系统调用分发器,用于分发系统调用。 81ab5c8ca4Slogin /// 82ab5c8ca4Slogin /// 这个函数内,需要根据系统调用号,调用对应的系统调用处理函数。 83ab5c8ca4Slogin /// 并且,对于用户态传入的指针参数,需要在本函数内进行越界检查,防止访问到内核空间。 844fda81ceSLoGin #[inline(never)] 859e9ffedfSPlucky923 pub fn handle( 869e9ffedfSPlucky923 syscall_num: usize, 879e9ffedfSPlucky923 args: &[usize], 889e9ffedfSPlucky923 frame: &mut TrapFrame, 899e9ffedfSPlucky923 ) -> Result<usize, SystemError> { 90ab5c8ca4Slogin let r = match syscall_num { 91ab5c8ca4Slogin SYS_PUT_STRING => { 92ab5c8ca4Slogin Self::put_string(args[0] as *const u8, args[1] as u32, args[2] as u32) 93ab5c8ca4Slogin } 944fda81ceSLoGin #[cfg(target_arch = "x86_64")] 95ab5c8ca4Slogin SYS_OPEN => { 96ab5c8ca4Slogin let path: &CStr = unsafe { CStr::from_ptr(args[0] as *const c_char) }; 97ab5c8ca4Slogin let path: Result<&str, core::str::Utf8Error> = path.to_str(); 98ab5c8ca4Slogin let res = if path.is_err() { 99ab5c8ca4Slogin Err(SystemError::EINVAL) 100ab5c8ca4Slogin } else { 101ab5c8ca4Slogin let path: &str = path.unwrap(); 10234e6d6c8Syuyi2439 103ab5c8ca4Slogin let flags = args[1]; 1040fb515b0SLoGin let mode = args[2]; 1050fb515b0SLoGin 106ab5c8ca4Slogin let open_flags: FileMode = FileMode::from_bits_truncate(flags as u32); 1070fb515b0SLoGin let mode = ModeType::from_bits(mode as u32).ok_or(SystemError::EINVAL)?; 1080fb515b0SLoGin Self::open(path, open_flags, mode, true) 1090fb515b0SLoGin }; 1100fb515b0SLoGin res 1110fb515b0SLoGin } 1120fb515b0SLoGin 1130fb515b0SLoGin SYS_OPENAT => { 1140fb515b0SLoGin let dirfd = args[0] as i32; 1150fb515b0SLoGin let path: &CStr = unsafe { CStr::from_ptr(args[1] as *const c_char) }; 1160fb515b0SLoGin let flags = args[2]; 1170fb515b0SLoGin let mode = args[3]; 1180fb515b0SLoGin 1190fb515b0SLoGin let path: Result<&str, core::str::Utf8Error> = path.to_str(); 1200fb515b0SLoGin let res = if path.is_err() { 1210fb515b0SLoGin Err(SystemError::EINVAL) 1220fb515b0SLoGin } else { 1230fb515b0SLoGin let path: &str = path.unwrap(); 1240fb515b0SLoGin 1250fb515b0SLoGin let open_flags: FileMode = 1260fb515b0SLoGin FileMode::from_bits(flags as u32).ok_or(SystemError::EINVAL)?; 1270fb515b0SLoGin let mode = ModeType::from_bits(mode as u32).ok_or(SystemError::EINVAL)?; 1280fb515b0SLoGin Self::openat(dirfd, path, open_flags, mode, true) 129ab5c8ca4Slogin }; 130ab5c8ca4Slogin res 131ab5c8ca4Slogin } 132ab5c8ca4Slogin SYS_CLOSE => { 133ab5c8ca4Slogin let fd = args[0]; 13427b967a3S裕依 Self::close(fd) 135ab5c8ca4Slogin } 136ab5c8ca4Slogin SYS_READ => { 137ab5c8ca4Slogin let fd = args[0] as i32; 138ab5c8ca4Slogin let buf_vaddr = args[1]; 139ab5c8ca4Slogin let len = args[2]; 1409e9ffedfSPlucky923 let from_user = frame.from_user(); 1419e9ffedfSPlucky923 let mut user_buffer_writer = 1429e9ffedfSPlucky923 UserBufferWriter::new(buf_vaddr as *mut u8, len, from_user)?; 1436d81180bSLoGin 1449e9ffedfSPlucky923 let user_buf = user_buffer_writer.buffer(0)?; 14527b967a3S裕依 Self::read(fd, user_buf) 146ab5c8ca4Slogin } 147ab5c8ca4Slogin SYS_WRITE => { 148ab5c8ca4Slogin let fd = args[0] as i32; 149ab5c8ca4Slogin let buf_vaddr = args[1]; 150ab5c8ca4Slogin let len = args[2]; 1519e9ffedfSPlucky923 let from_user = frame.from_user(); 1529e9ffedfSPlucky923 let user_buffer_reader = 1539e9ffedfSPlucky923 UserBufferReader::new(buf_vaddr as *const u8, len, from_user)?; 1546d81180bSLoGin 1559e9ffedfSPlucky923 let user_buf = user_buffer_reader.read_from_user(0)?; 15627b967a3S裕依 Self::write(fd, user_buf) 157ab5c8ca4Slogin } 158ab5c8ca4Slogin 159ab5c8ca4Slogin SYS_LSEEK => { 160ab5c8ca4Slogin let fd = args[0] as i32; 161ab5c8ca4Slogin let offset = args[1] as i64; 162ab5c8ca4Slogin let whence = args[2] as u32; 163ab5c8ca4Slogin 164ab5c8ca4Slogin let w = match whence { 165ab5c8ca4Slogin SEEK_SET => Ok(SeekFrom::SeekSet(offset)), 166ab5c8ca4Slogin SEEK_CUR => Ok(SeekFrom::SeekCurrent(offset)), 167ab5c8ca4Slogin SEEK_END => Ok(SeekFrom::SeekEnd(offset)), 168ab5c8ca4Slogin SEEK_MAX => Ok(SeekFrom::SeekEnd(0)), 169ab5c8ca4Slogin _ => Err(SystemError::EINVAL), 1709e9ffedfSPlucky923 }?; 171ab5c8ca4Slogin 172ab5c8ca4Slogin Self::lseek(fd, w) 173ab5c8ca4Slogin } 17427b967a3S裕依 17527b967a3S裕依 SYS_PREAD64 => { 17627b967a3S裕依 let fd = args[0] as i32; 17727b967a3S裕依 let buf_vaddr = args[1]; 17827b967a3S裕依 let len = args[2]; 17927b967a3S裕依 let offset = args[3]; 18027b967a3S裕依 18127b967a3S裕依 let mut user_buffer_writer = 18227b967a3S裕依 UserBufferWriter::new(buf_vaddr as *mut u8, len, frame.from_user())?; 18327b967a3S裕依 let buf = user_buffer_writer.buffer(0)?; 18427b967a3S裕依 Self::pread(fd, buf, len, offset) 18527b967a3S裕依 } 18627b967a3S裕依 18727b967a3S裕依 SYS_PWRITE64 => { 18827b967a3S裕依 let fd = args[0] as i32; 18927b967a3S裕依 let buf_vaddr = args[1]; 19027b967a3S裕依 let len = args[2]; 19127b967a3S裕依 let offset = args[3]; 19227b967a3S裕依 19327b967a3S裕依 let user_buffer_reader = 19427b967a3S裕依 UserBufferReader::new(buf_vaddr as *const u8, len, frame.from_user())?; 19527b967a3S裕依 19627b967a3S裕依 let buf = user_buffer_reader.read_from_user(0)?; 19727b967a3S裕依 Self::pwrite(fd, buf, len, offset) 19827b967a3S裕依 } 19927b967a3S裕依 20040314b30SXiaoye Zheng SYS_IOCTL => { 20140314b30SXiaoye Zheng let fd = args[0]; 20240314b30SXiaoye Zheng let cmd = args[1]; 20340314b30SXiaoye Zheng let data = args[2]; 20440314b30SXiaoye Zheng Self::ioctl(fd, cmd as u32, data) 20540314b30SXiaoye Zheng } 206ab5c8ca4Slogin 2074fda81ceSLoGin #[cfg(target_arch = "x86_64")] 2081496ba7bSLoGin SYS_FORK => Self::fork(frame), 2094fda81ceSLoGin #[cfg(target_arch = "x86_64")] 2101496ba7bSLoGin SYS_VFORK => Self::vfork(frame), 2111496ba7bSLoGin 212ab5c8ca4Slogin SYS_BRK => { 21340fe15e0SLoGin let new_brk = VirtAddr::new(args[0]); 21440fe15e0SLoGin Self::brk(new_brk).map(|vaddr| vaddr.data()) 215ab5c8ca4Slogin } 216ab5c8ca4Slogin 217ab5c8ca4Slogin SYS_SBRK => { 218ab5c8ca4Slogin let increment = args[0] as isize; 2191496ba7bSLoGin Self::sbrk(increment).map(|vaddr: VirtAddr| vaddr.data()) 220ab5c8ca4Slogin } 221ab5c8ca4Slogin 222ab5c8ca4Slogin SYS_REBOOT => Self::reboot(), 223ab5c8ca4Slogin 224ab5c8ca4Slogin SYS_CHDIR => { 225ab5c8ca4Slogin // Closure for checking arguments 226ab5c8ca4Slogin let chdir_check = |arg0: usize| { 227ab5c8ca4Slogin if arg0 == 0 { 228ab5c8ca4Slogin return Err(SystemError::EFAULT); 229ab5c8ca4Slogin } 230ab5c8ca4Slogin let path_ptr = arg0 as *const c_char; 231be63f3b2Shoumkh let virt_addr = VirtAddr::new(path_ptr as usize); 232ab5c8ca4Slogin // 权限校验 233ab5c8ca4Slogin if path_ptr.is_null() 2341496ba7bSLoGin || (frame.from_user() 2351496ba7bSLoGin && verify_area(virt_addr, PAGE_2M_SIZE as usize).is_err()) 236ab5c8ca4Slogin { 237ab5c8ca4Slogin return Err(SystemError::EINVAL); 238ab5c8ca4Slogin } 239ab5c8ca4Slogin let dest_path: &CStr = unsafe { CStr::from_ptr(path_ptr) }; 240ab5c8ca4Slogin let dest_path: &str = dest_path.to_str().map_err(|_| SystemError::EINVAL)?; 241ab5c8ca4Slogin if dest_path.len() == 0 { 242ab5c8ca4Slogin return Err(SystemError::EINVAL); 2431496ba7bSLoGin } else if dest_path.len() > MAX_PATHLEN as usize { 244ab5c8ca4Slogin return Err(SystemError::ENAMETOOLONG); 245ab5c8ca4Slogin } 246ab5c8ca4Slogin 247ab5c8ca4Slogin return Ok(dest_path); 248ab5c8ca4Slogin }; 249ab5c8ca4Slogin 2509e9ffedfSPlucky923 let r = chdir_check(args[0])?; 2519e9ffedfSPlucky923 Self::chdir(r) 252ab5c8ca4Slogin } 253ab5c8ca4Slogin 2544fda81ceSLoGin #[allow(unreachable_patterns)] 2554fda81ceSLoGin SYS_GETDENTS64 | SYS_GETDENTS => { 256ab5c8ca4Slogin let fd = args[0] as i32; 25734e6d6c8Syuyi2439 258ab5c8ca4Slogin let buf_vaddr = args[1]; 259ab5c8ca4Slogin let len = args[2]; 260f5df0e79SLoGin let virt_addr: VirtAddr = VirtAddr::new(buf_vaddr); 261ab5c8ca4Slogin // 判断缓冲区是否来自用户态,进行权限校验 2621496ba7bSLoGin let res = if frame.from_user() && verify_area(virt_addr, len as usize).is_err() { 263ab5c8ca4Slogin // 来自用户态,而buffer在内核态,这样的操作不被允许 264ab5c8ca4Slogin Err(SystemError::EPERM) 265ab5c8ca4Slogin } else if buf_vaddr == 0 { 266ab5c8ca4Slogin Err(SystemError::EFAULT) 267ab5c8ca4Slogin } else { 268ab5c8ca4Slogin let buf: &mut [u8] = unsafe { 269ab5c8ca4Slogin core::slice::from_raw_parts_mut::<'static, u8>(buf_vaddr as *mut u8, len) 270ab5c8ca4Slogin }; 271ab5c8ca4Slogin Self::getdents(fd, buf) 272ab5c8ca4Slogin }; 273ab5c8ca4Slogin 274ab5c8ca4Slogin res 275ab5c8ca4Slogin } 276ab5c8ca4Slogin 277ab5c8ca4Slogin SYS_EXECVE => { 278ab5c8ca4Slogin let path_ptr = args[0]; 279ab5c8ca4Slogin let argv_ptr = args[1]; 280ab5c8ca4Slogin let env_ptr = args[2]; 281be63f3b2Shoumkh let virt_path_ptr = VirtAddr::new(path_ptr); 282be63f3b2Shoumkh let virt_argv_ptr = VirtAddr::new(argv_ptr); 283be63f3b2Shoumkh let virt_env_ptr = VirtAddr::new(env_ptr); 284ab5c8ca4Slogin // 权限校验 2851496ba7bSLoGin if frame.from_user() 2861496ba7bSLoGin && (verify_area(virt_path_ptr, MAX_PATHLEN as usize).is_err() 287be63f3b2Shoumkh || verify_area(virt_argv_ptr, PAGE_4K_SIZE as usize).is_err()) 288be63f3b2Shoumkh || verify_area(virt_env_ptr, PAGE_4K_SIZE as usize).is_err() 289ab5c8ca4Slogin { 290ab5c8ca4Slogin Err(SystemError::EFAULT) 291ab5c8ca4Slogin } else { 292ab5c8ca4Slogin Self::execve( 2931496ba7bSLoGin path_ptr as *const u8, 2941496ba7bSLoGin argv_ptr as *const *const u8, 2951496ba7bSLoGin env_ptr as *const *const u8, 2961496ba7bSLoGin frame, 297ab5c8ca4Slogin ) 2981496ba7bSLoGin .map(|_| 0) 299ab5c8ca4Slogin } 300ab5c8ca4Slogin } 301ab5c8ca4Slogin SYS_WAIT4 => { 302bf4a4899SLoGin let pid = args[0] as i32; 3031496ba7bSLoGin let wstatus = args[1] as *mut i32; 304ab5c8ca4Slogin let options = args[2] as c_int; 305ab5c8ca4Slogin let rusage = args[3] as *mut c_void; 306ab5c8ca4Slogin // 权限校验 307ab5c8ca4Slogin // todo: 引入rusage之后,更正以下权限校验代码中,rusage的大小 308bf4a4899SLoGin Self::wait4(pid.into(), wstatus, options, rusage) 309ab5c8ca4Slogin } 310ab5c8ca4Slogin 311ab5c8ca4Slogin SYS_EXIT => { 312ab5c8ca4Slogin let exit_code = args[0]; 313ab5c8ca4Slogin Self::exit(exit_code) 314ab5c8ca4Slogin } 3154fda81ceSLoGin #[cfg(target_arch = "x86_64")] 316ab5c8ca4Slogin SYS_MKDIR => { 317ab5c8ca4Slogin let path_ptr = args[0] as *const c_char; 318ab5c8ca4Slogin let mode = args[1]; 319be63f3b2Shoumkh let virt_path_ptr = VirtAddr::new(path_ptr as usize); 320ab5c8ca4Slogin let security_check = || { 321ab5c8ca4Slogin if path_ptr.is_null() 3221496ba7bSLoGin || (frame.from_user() 3231496ba7bSLoGin && verify_area(virt_path_ptr, PAGE_2M_SIZE as usize).is_err()) 324ab5c8ca4Slogin { 325ab5c8ca4Slogin return Err(SystemError::EINVAL); 326ab5c8ca4Slogin } 327ab5c8ca4Slogin let path: &CStr = unsafe { CStr::from_ptr(path_ptr) }; 328ab5c8ca4Slogin let path: &str = path.to_str().map_err(|_| SystemError::EINVAL)?.trim(); 329ab5c8ca4Slogin 330ab5c8ca4Slogin if path == "" { 331ab5c8ca4Slogin return Err(SystemError::EINVAL); 332ab5c8ca4Slogin } 333ab5c8ca4Slogin return Ok(path); 334ab5c8ca4Slogin }; 335ab5c8ca4Slogin 336ab5c8ca4Slogin let path = security_check(); 337ab5c8ca4Slogin if path.is_err() { 338ab5c8ca4Slogin Err(path.unwrap_err()) 339ab5c8ca4Slogin } else { 340ab5c8ca4Slogin Self::mkdir(path.unwrap(), mode) 341ab5c8ca4Slogin } 342ab5c8ca4Slogin } 343ab5c8ca4Slogin 344ab5c8ca4Slogin SYS_NANOSLEEP => { 345ab5c8ca4Slogin let req = args[0] as *const TimeSpec; 346ab5c8ca4Slogin let rem = args[1] as *mut TimeSpec; 347be63f3b2Shoumkh let virt_req = VirtAddr::new(req as usize); 348be63f3b2Shoumkh let virt_rem = VirtAddr::new(rem as usize); 3491496ba7bSLoGin if frame.from_user() 350be63f3b2Shoumkh && (verify_area(virt_req, core::mem::size_of::<TimeSpec>() as usize).is_err() 351be63f3b2Shoumkh || verify_area(virt_rem, core::mem::size_of::<TimeSpec>() as usize) 352be63f3b2Shoumkh .is_err()) 353ab5c8ca4Slogin { 354ab5c8ca4Slogin Err(SystemError::EFAULT) 355ab5c8ca4Slogin } else { 356ab5c8ca4Slogin Self::nanosleep(req, rem) 357ab5c8ca4Slogin } 358ab5c8ca4Slogin } 359ab5c8ca4Slogin 360ab5c8ca4Slogin SYS_CLOCK => Self::clock(), 3614fda81ceSLoGin #[cfg(target_arch = "x86_64")] 362ab5c8ca4Slogin SYS_PIPE => { 36322c9db31Shanjiezhou let pipefd: *mut i32 = args[0] as *mut c_int; 3640fb515b0SLoGin if pipefd.is_null() { 3650fb515b0SLoGin Err(SystemError::EFAULT) 3660fb515b0SLoGin } else { 3670fb515b0SLoGin Self::pipe2(pipefd, FileMode::empty()) 3680fb515b0SLoGin } 3690fb515b0SLoGin } 3704ad52e57S裕依2439 3710fb515b0SLoGin SYS_PIPE2 => { 3720fb515b0SLoGin let pipefd: *mut i32 = args[0] as *mut c_int; 37322c9db31Shanjiezhou let arg1 = args[1]; 37422c9db31Shanjiezhou if pipefd.is_null() { 37522c9db31Shanjiezhou Err(SystemError::EFAULT) 37622c9db31Shanjiezhou } else { 37722c9db31Shanjiezhou let flags = FileMode::from_bits_truncate(arg1 as u32); 37822c9db31Shanjiezhou Self::pipe2(pipefd, flags) 379ab5c8ca4Slogin } 380ab5c8ca4Slogin } 381ab5c8ca4Slogin 3824fda81ceSLoGin SYS_UNLINKAT => { 383ab5c8ca4Slogin let dirfd = args[0] as i32; 384ab5c8ca4Slogin let pathname = args[1] as *const c_char; 385ab5c8ca4Slogin let flags = args[2] as u32; 386be63f3b2Shoumkh let virt_pathname = VirtAddr::new(pathname as usize); 3871496ba7bSLoGin if frame.from_user() && verify_area(virt_pathname, PAGE_4K_SIZE as usize).is_err() { 388ab5c8ca4Slogin Err(SystemError::EFAULT) 389ab5c8ca4Slogin } else if pathname.is_null() { 390ab5c8ca4Slogin Err(SystemError::EFAULT) 391ab5c8ca4Slogin } else { 392ab5c8ca4Slogin let get_path = || { 393ab5c8ca4Slogin let pathname: &CStr = unsafe { CStr::from_ptr(pathname) }; 394ab5c8ca4Slogin 395ab5c8ca4Slogin let pathname: &str = pathname.to_str().map_err(|_| SystemError::EINVAL)?; 396ab5c8ca4Slogin if pathname.len() >= MAX_PATHLEN { 397ab5c8ca4Slogin return Err(SystemError::ENAMETOOLONG); 398ab5c8ca4Slogin } 399ab5c8ca4Slogin return Ok(pathname.trim()); 400ab5c8ca4Slogin }; 401ab5c8ca4Slogin let pathname = get_path(); 402ab5c8ca4Slogin if pathname.is_err() { 403ab5c8ca4Slogin Err(pathname.unwrap_err()) 404ab5c8ca4Slogin } else { 4056d81180bSLoGin // kdebug!("sys unlinkat: dirfd: {}, pathname: {}", dirfd, pathname.as_ref().unwrap()); 406ab5c8ca4Slogin Self::unlinkat(dirfd, pathname.unwrap(), flags) 407ab5c8ca4Slogin } 408ab5c8ca4Slogin } 409ab5c8ca4Slogin } 410bf4a4899SLoGin 4114fda81ceSLoGin #[cfg(target_arch = "x86_64")] 412bf4a4899SLoGin SYS_UNLINK => { 413bf4a4899SLoGin let pathname = args[0] as *const u8; 414bf4a4899SLoGin Self::unlink(pathname) 415bf4a4899SLoGin } 416ab5c8ca4Slogin SYS_KILL => { 4171496ba7bSLoGin let pid = Pid::new(args[0]); 418ab5c8ca4Slogin let sig = args[1] as c_int; 4193c82aa56SChiichen // kdebug!("KILL SYSCALL RECEIVED"); 420ab5c8ca4Slogin Self::kill(pid, sig) 421ab5c8ca4Slogin } 422ab5c8ca4Slogin 4234fda81ceSLoGin SYS_RT_SIGACTION => { 424ab5c8ca4Slogin let sig = args[0] as c_int; 425ab5c8ca4Slogin let act = args[1]; 426ab5c8ca4Slogin let old_act = args[2]; 4271496ba7bSLoGin Self::sigaction(sig, act, old_act, frame.from_user()) 428ab5c8ca4Slogin } 429ab5c8ca4Slogin 4301496ba7bSLoGin SYS_GETPID => Self::getpid().map(|pid| pid.into()), 431ab5c8ca4Slogin 4321496ba7bSLoGin SYS_SCHED => Self::sched(frame.from_user()), 433ab5c8ca4Slogin SYS_DUP => { 434ab5c8ca4Slogin let oldfd: i32 = args[0] as c_int; 435ab5c8ca4Slogin Self::dup(oldfd) 436ab5c8ca4Slogin } 4374fda81ceSLoGin 4384fda81ceSLoGin #[cfg(target_arch = "x86_64")] 439ab5c8ca4Slogin SYS_DUP2 => { 440ab5c8ca4Slogin let oldfd: i32 = args[0] as c_int; 441ab5c8ca4Slogin let newfd: i32 = args[1] as c_int; 442ab5c8ca4Slogin Self::dup2(oldfd, newfd) 443ab5c8ca4Slogin } 444ab5c8ca4Slogin 445ab5c8ca4Slogin SYS_SOCKET => Self::socket(args[0], args[1], args[2]), 446ab5c8ca4Slogin SYS_SETSOCKOPT => { 447ab5c8ca4Slogin let optval = args[3] as *const u8; 448ab5c8ca4Slogin let optlen = args[4] as usize; 449be63f3b2Shoumkh let virt_optval = VirtAddr::new(optval as usize); 450ab5c8ca4Slogin // 验证optval的地址是否合法 451be63f3b2Shoumkh if verify_area(virt_optval, optlen as usize).is_err() { 452ab5c8ca4Slogin // 地址空间超出了用户空间的范围,不合法 453ab5c8ca4Slogin Err(SystemError::EFAULT) 454ab5c8ca4Slogin } else { 455ab5c8ca4Slogin let data: &[u8] = unsafe { core::slice::from_raw_parts(optval, optlen) }; 456ab5c8ca4Slogin Self::setsockopt(args[0], args[1], args[2], data) 457ab5c8ca4Slogin } 458ab5c8ca4Slogin } 459ab5c8ca4Slogin SYS_GETSOCKOPT => { 460ab5c8ca4Slogin let optval = args[3] as *mut u8; 461ab5c8ca4Slogin let optlen = args[4] as *mut usize; 462be63f3b2Shoumkh let virt_optval = VirtAddr::new(optval as usize); 463be63f3b2Shoumkh let virt_optlen = VirtAddr::new(optlen as usize); 464ab5c8ca4Slogin let security_check = || { 465ab5c8ca4Slogin // 验证optval的地址是否合法 466be63f3b2Shoumkh if verify_area(virt_optval, PAGE_4K_SIZE as usize).is_err() { 467ab5c8ca4Slogin // 地址空间超出了用户空间的范围,不合法 468ab5c8ca4Slogin return Err(SystemError::EFAULT); 469ab5c8ca4Slogin } 470ab5c8ca4Slogin 471ab5c8ca4Slogin // 验证optlen的地址是否合法 472be63f3b2Shoumkh if verify_area(virt_optlen, core::mem::size_of::<u32>() as usize).is_err() { 473ab5c8ca4Slogin // 地址空间超出了用户空间的范围,不合法 474ab5c8ca4Slogin return Err(SystemError::EFAULT); 475ab5c8ca4Slogin } 476ab5c8ca4Slogin return Ok(()); 477ab5c8ca4Slogin }; 478ab5c8ca4Slogin let r = security_check(); 479ab5c8ca4Slogin if r.is_err() { 480ab5c8ca4Slogin Err(r.unwrap_err()) 481ab5c8ca4Slogin } else { 482ab5c8ca4Slogin Self::getsockopt(args[0], args[1], args[2], optval, optlen as *mut u32) 483ab5c8ca4Slogin } 484ab5c8ca4Slogin } 485ab5c8ca4Slogin 486ab5c8ca4Slogin SYS_CONNECT => { 487ab5c8ca4Slogin let addr = args[1] as *const SockAddr; 488ab5c8ca4Slogin let addrlen = args[2] as usize; 489be63f3b2Shoumkh let virt_addr = VirtAddr::new(addr as usize); 490ab5c8ca4Slogin // 验证addr的地址是否合法 491be63f3b2Shoumkh if verify_area(virt_addr, addrlen as usize).is_err() { 492ab5c8ca4Slogin // 地址空间超出了用户空间的范围,不合法 493ab5c8ca4Slogin Err(SystemError::EFAULT) 494ab5c8ca4Slogin } else { 495ab5c8ca4Slogin Self::connect(args[0], addr, addrlen) 496ab5c8ca4Slogin } 497ab5c8ca4Slogin } 498ab5c8ca4Slogin SYS_BIND => { 499ab5c8ca4Slogin let addr = args[1] as *const SockAddr; 500ab5c8ca4Slogin let addrlen = args[2] as usize; 501be63f3b2Shoumkh let virt_addr = VirtAddr::new(addr as usize); 502ab5c8ca4Slogin // 验证addr的地址是否合法 503be63f3b2Shoumkh if verify_area(virt_addr, addrlen as usize).is_err() { 504ab5c8ca4Slogin // 地址空间超出了用户空间的范围,不合法 505ab5c8ca4Slogin Err(SystemError::EFAULT) 506ab5c8ca4Slogin } else { 507ab5c8ca4Slogin Self::bind(args[0], addr, addrlen) 508ab5c8ca4Slogin } 509ab5c8ca4Slogin } 510ab5c8ca4Slogin 511ab5c8ca4Slogin SYS_SENDTO => { 512ab5c8ca4Slogin let buf = args[1] as *const u8; 513ab5c8ca4Slogin let len = args[2] as usize; 514ab5c8ca4Slogin let flags = args[3] as u32; 515ab5c8ca4Slogin let addr = args[4] as *const SockAddr; 516ab5c8ca4Slogin let addrlen = args[5] as usize; 517be63f3b2Shoumkh let virt_buf = VirtAddr::new(buf as usize); 518be63f3b2Shoumkh let virt_addr = VirtAddr::new(addr as usize); 519ab5c8ca4Slogin // 验证buf的地址是否合法 520be63f3b2Shoumkh if verify_area(virt_buf, len as usize).is_err() { 521ab5c8ca4Slogin // 地址空间超出了用户空间的范围,不合法 522ab5c8ca4Slogin Err(SystemError::EFAULT) 523be63f3b2Shoumkh } else if verify_area(virt_addr, addrlen as usize).is_err() { 524ab5c8ca4Slogin // 地址空间超出了用户空间的范围,不合法 525ab5c8ca4Slogin Err(SystemError::EFAULT) 526ab5c8ca4Slogin } else { 527ab5c8ca4Slogin let data: &[u8] = unsafe { core::slice::from_raw_parts(buf, len) }; 528ab5c8ca4Slogin Self::sendto(args[0], data, flags, addr, addrlen) 529ab5c8ca4Slogin } 530ab5c8ca4Slogin } 531ab5c8ca4Slogin 532ab5c8ca4Slogin SYS_RECVFROM => { 533ab5c8ca4Slogin let buf = args[1] as *mut u8; 534ab5c8ca4Slogin let len = args[2] as usize; 535ab5c8ca4Slogin let flags = args[3] as u32; 536ab5c8ca4Slogin let addr = args[4] as *mut SockAddr; 537ab5c8ca4Slogin let addrlen = args[5] as *mut usize; 538be63f3b2Shoumkh let virt_buf = VirtAddr::new(buf as usize); 539be63f3b2Shoumkh let virt_addrlen = VirtAddr::new(addrlen as usize); 540be63f3b2Shoumkh let virt_addr = VirtAddr::new(addr as usize); 541ab5c8ca4Slogin let security_check = || { 542ab5c8ca4Slogin // 验证buf的地址是否合法 543be63f3b2Shoumkh if verify_area(virt_buf, len as usize).is_err() { 544ab5c8ca4Slogin // 地址空间超出了用户空间的范围,不合法 545ab5c8ca4Slogin return Err(SystemError::EFAULT); 546ab5c8ca4Slogin } 547ab5c8ca4Slogin 548ab5c8ca4Slogin // 验证addrlen的地址是否合法 549be63f3b2Shoumkh if verify_area(virt_addrlen, core::mem::size_of::<u32>() as usize).is_err() { 550ab5c8ca4Slogin // 地址空间超出了用户空间的范围,不合法 551ab5c8ca4Slogin return Err(SystemError::EFAULT); 552ab5c8ca4Slogin } 553ab5c8ca4Slogin 554be63f3b2Shoumkh if verify_area(virt_addr, core::mem::size_of::<SockAddr>() as usize).is_err() { 555ab5c8ca4Slogin // 地址空间超出了用户空间的范围,不合法 556ab5c8ca4Slogin return Err(SystemError::EFAULT); 557ab5c8ca4Slogin } 558ab5c8ca4Slogin return Ok(()); 559ab5c8ca4Slogin }; 560ab5c8ca4Slogin let r = security_check(); 561ab5c8ca4Slogin if r.is_err() { 562ab5c8ca4Slogin Err(r.unwrap_err()) 563ab5c8ca4Slogin } else { 564ab5c8ca4Slogin let buf = unsafe { core::slice::from_raw_parts_mut(buf, len) }; 565ab5c8ca4Slogin Self::recvfrom(args[0], buf, flags, addr, addrlen as *mut u32) 566ab5c8ca4Slogin } 567ab5c8ca4Slogin } 568ab5c8ca4Slogin 569ab5c8ca4Slogin SYS_RECVMSG => { 5704ad52e57S裕依2439 let msg = args[1] as *mut MsgHdr; 571ab5c8ca4Slogin let flags = args[2] as u32; 5724ad52e57S裕依2439 5734ad52e57S裕依2439 let mut user_buffer_writer = 5744ad52e57S裕依2439 UserBufferWriter::new(msg, core::mem::size_of::<MsgHdr>(), frame.from_user())?; 5754ad52e57S裕依2439 let buffer = user_buffer_writer.buffer::<MsgHdr>(0)?; 5764ad52e57S裕依2439 5774537ffb7SChiichen let msg = &mut buffer[0]; 578ab5c8ca4Slogin Self::recvmsg(args[0], msg, flags) 579ab5c8ca4Slogin } 580ab5c8ca4Slogin 581ab5c8ca4Slogin SYS_LISTEN => Self::listen(args[0], args[1]), 582ab5c8ca4Slogin SYS_SHUTDOWN => Self::shutdown(args[0], args[1]), 583ab5c8ca4Slogin SYS_ACCEPT => Self::accept(args[0], args[1] as *mut SockAddr, args[2] as *mut u32), 584c47fe904SLoGin SYS_ACCEPT4 => Self::accept4( 585c47fe904SLoGin args[0], 586c47fe904SLoGin args[1] as *mut SockAddr, 587c47fe904SLoGin args[2] as *mut u32, 588c47fe904SLoGin args[3] as u32, 589c47fe904SLoGin ), 590ab5c8ca4Slogin SYS_GETSOCKNAME => { 591ab5c8ca4Slogin Self::getsockname(args[0], args[1] as *mut SockAddr, args[2] as *mut u32) 592ab5c8ca4Slogin } 593ab5c8ca4Slogin SYS_GETPEERNAME => { 594ab5c8ca4Slogin Self::getpeername(args[0], args[1] as *mut SockAddr, args[2] as *mut u32) 595ab5c8ca4Slogin } 59636fd0130Shoumkh SYS_GETTIMEOFDAY => { 59736fd0130Shoumkh let timeval = args[0] as *mut PosixTimeval; 59840fe15e0SLoGin let timezone_ptr = args[1] as *mut PosixTimeZone; 59940fe15e0SLoGin Self::gettimeofday(timeval, timezone_ptr) 6004537ffb7SChiichen } 60140fe15e0SLoGin SYS_MMAP => { 60240fe15e0SLoGin let len = page_align_up(args[1]); 603be63f3b2Shoumkh let virt_addr = VirtAddr::new(args[0] as usize); 604be63f3b2Shoumkh if verify_area(virt_addr, len as usize).is_err() { 60540fe15e0SLoGin Err(SystemError::EFAULT) 60640fe15e0SLoGin } else { 60740fe15e0SLoGin Self::mmap( 60840fe15e0SLoGin VirtAddr::new(args[0]), 60940fe15e0SLoGin len, 61040fe15e0SLoGin args[2], 61140fe15e0SLoGin args[3], 61240fe15e0SLoGin args[4] as i32, 61340fe15e0SLoGin args[5], 61440fe15e0SLoGin ) 61540fe15e0SLoGin } 61640fe15e0SLoGin } 6174cfa009bSJomo SYS_MREMAP => { 6184cfa009bSJomo let old_vaddr = VirtAddr::new(args[0]); 6194cfa009bSJomo let old_len = args[1]; 6204cfa009bSJomo let new_len = args[2]; 6214cfa009bSJomo let mremap_flags = MremapFlags::from_bits_truncate(args[3] as u8); 6224cfa009bSJomo let new_vaddr = VirtAddr::new(args[4]); 6234cfa009bSJomo 6244cfa009bSJomo Self::mremap(old_vaddr, old_len, new_len, mremap_flags, new_vaddr) 6254cfa009bSJomo } 62640fe15e0SLoGin SYS_MUNMAP => { 62740fe15e0SLoGin let addr = args[0]; 62840fe15e0SLoGin let len = page_align_up(args[1]); 629971462beSGnoCiYeH if addr & (MMArch::PAGE_SIZE - 1) != 0 { 63040fe15e0SLoGin // The addr argument is not a multiple of the page size 63140fe15e0SLoGin Err(SystemError::EINVAL) 63240fe15e0SLoGin } else { 63340fe15e0SLoGin Self::munmap(VirtAddr::new(addr), len) 63440fe15e0SLoGin } 63540fe15e0SLoGin } 63640fe15e0SLoGin SYS_MPROTECT => { 63740fe15e0SLoGin let addr = args[0]; 63840fe15e0SLoGin let len = page_align_up(args[1]); 639971462beSGnoCiYeH if addr & (MMArch::PAGE_SIZE - 1) != 0 { 64040fe15e0SLoGin // The addr argument is not a multiple of the page size 64140fe15e0SLoGin Err(SystemError::EINVAL) 64240fe15e0SLoGin } else { 64340fe15e0SLoGin Self::mprotect(VirtAddr::new(addr), len, args[2]) 64440fe15e0SLoGin } 64540fe15e0SLoGin } 64640fe15e0SLoGin 6471496ba7bSLoGin SYS_GETCWD => { 6481496ba7bSLoGin let buf = args[0] as *mut u8; 6491496ba7bSLoGin let size = args[1] as usize; 6501496ba7bSLoGin let security_check = || { 6511496ba7bSLoGin verify_area(VirtAddr::new(buf as usize), size)?; 6521496ba7bSLoGin return Ok(()); 6531496ba7bSLoGin }; 6541496ba7bSLoGin let r = security_check(); 6551496ba7bSLoGin if r.is_err() { 6561496ba7bSLoGin Err(r.unwrap_err()) 6571496ba7bSLoGin } else { 6581496ba7bSLoGin let buf = unsafe { core::slice::from_raw_parts_mut(buf, size) }; 6591496ba7bSLoGin Self::getcwd(buf).map(|ptr| ptr.data()) 6601496ba7bSLoGin } 6611496ba7bSLoGin } 6621496ba7bSLoGin 6631496ba7bSLoGin SYS_GETPGID => Self::getpgid(Pid::new(args[0])).map(|pid| pid.into()), 6641496ba7bSLoGin 6651496ba7bSLoGin SYS_GETPPID => Self::getppid().map(|pid| pid.into()), 666be63f3b2Shoumkh SYS_FSTAT => { 667be63f3b2Shoumkh let fd = args[0] as i32; 668be63f3b2Shoumkh let kstat = args[1] as *mut PosixKstat; 669be63f3b2Shoumkh let vaddr = VirtAddr::new(kstat as usize); 670be63f3b2Shoumkh // FIXME 由于c中的verify_area与rust中的verify_area重名,所以在引入时加了前缀区分 671be63f3b2Shoumkh // TODO 应该将用了c版本的verify_area都改为rust的verify_area 672be63f3b2Shoumkh match verify_area(vaddr, core::mem::size_of::<PosixKstat>()) { 673be63f3b2Shoumkh Ok(_) => Self::fstat(fd, kstat), 674be63f3b2Shoumkh Err(e) => Err(e), 675be63f3b2Shoumkh } 676be63f3b2Shoumkh } 677be63f3b2Shoumkh 6786d81180bSLoGin SYS_FCNTL => { 6796d81180bSLoGin let fd = args[0] as i32; 6806d81180bSLoGin let cmd: Option<FcntlCommand> = 6816d81180bSLoGin <FcntlCommand as FromPrimitive>::from_u32(args[1] as u32); 6826d81180bSLoGin let arg = args[2] as i32; 6836d81180bSLoGin let res = if let Some(cmd) = cmd { 6846d81180bSLoGin Self::fcntl(fd, cmd, arg) 6856d81180bSLoGin } else { 6866d81180bSLoGin Err(SystemError::EINVAL) 6876d81180bSLoGin }; 6886d81180bSLoGin 6896d81180bSLoGin // kdebug!("FCNTL: fd: {}, cmd: {:?}, arg: {}, res: {:?}", fd, cmd, arg, res); 6906d81180bSLoGin res 6916d81180bSLoGin } 6926d81180bSLoGin 6936d81180bSLoGin SYS_FTRUNCATE => { 6946d81180bSLoGin let fd = args[0] as i32; 6956d81180bSLoGin let len = args[1] as usize; 6966d81180bSLoGin let res = Self::ftruncate(fd, len); 6976d81180bSLoGin // kdebug!("FTRUNCATE: fd: {}, len: {}, res: {:?}", fd, len, res); 6986d81180bSLoGin res 6996d81180bSLoGin } 7006d81180bSLoGin 7014fda81ceSLoGin #[cfg(target_arch = "x86_64")] 7022dbef785SGnoCiYeH SYS_MKNOD => { 7032dbef785SGnoCiYeH let path = args[0]; 7042dbef785SGnoCiYeH let flags = args[1]; 7052dbef785SGnoCiYeH let dev_t = args[2]; 7062dbef785SGnoCiYeH let flags: ModeType = ModeType::from_bits_truncate(flags as u32); 707c566df45SLoGin Self::mknod(path as *const i8, flags, DeviceNumber::from(dev_t as u32)) 7082dbef785SGnoCiYeH } 7092dbef785SGnoCiYeH 710971462beSGnoCiYeH SYS_CLONE => { 711971462beSGnoCiYeH let parent_tid = VirtAddr::new(args[2]); 712971462beSGnoCiYeH let child_tid = VirtAddr::new(args[3]); 713971462beSGnoCiYeH 714971462beSGnoCiYeH // 地址校验 715971462beSGnoCiYeH verify_area(parent_tid, core::mem::size_of::<i32>())?; 716971462beSGnoCiYeH verify_area(child_tid, core::mem::size_of::<i32>())?; 717971462beSGnoCiYeH 718971462beSGnoCiYeH let mut clone_args = KernelCloneArgs::new(); 719971462beSGnoCiYeH clone_args.flags = CloneFlags::from_bits_truncate(args[0] as u64); 720971462beSGnoCiYeH clone_args.stack = args[1]; 721971462beSGnoCiYeH clone_args.parent_tid = parent_tid; 722971462beSGnoCiYeH clone_args.child_tid = child_tid; 723971462beSGnoCiYeH clone_args.tls = args[4]; 724971462beSGnoCiYeH Self::clone(frame, clone_args) 725971462beSGnoCiYeH } 726971462beSGnoCiYeH 727971462beSGnoCiYeH SYS_FUTEX => { 728971462beSGnoCiYeH let uaddr = VirtAddr::new(args[0]); 729971462beSGnoCiYeH let operation = FutexFlag::from_bits(args[1] as u32).ok_or(SystemError::ENOSYS)?; 730971462beSGnoCiYeH let val = args[2] as u32; 731971462beSGnoCiYeH let utime = args[3]; 732971462beSGnoCiYeH let uaddr2 = VirtAddr::new(args[4]); 733971462beSGnoCiYeH let val3 = args[5] as u32; 734971462beSGnoCiYeH 735971462beSGnoCiYeH verify_area(uaddr, core::mem::size_of::<u32>())?; 736971462beSGnoCiYeH verify_area(uaddr2, core::mem::size_of::<u32>())?; 737971462beSGnoCiYeH 738971462beSGnoCiYeH let mut timespec = None; 739971462beSGnoCiYeH if utime != 0 && operation.contains(FutexFlag::FLAGS_HAS_TIMEOUT) { 740971462beSGnoCiYeH let reader = UserBufferReader::new( 741971462beSGnoCiYeH utime as *const TimeSpec, 742971462beSGnoCiYeH core::mem::size_of::<TimeSpec>(), 743971462beSGnoCiYeH true, 744971462beSGnoCiYeH )?; 745971462beSGnoCiYeH 746971462beSGnoCiYeH timespec = Some(reader.read_one_from_user::<TimeSpec>(0)?.clone()); 747971462beSGnoCiYeH } 748971462beSGnoCiYeH 749971462beSGnoCiYeH Self::do_futex(uaddr, operation, val, timespec, uaddr2, utime as u32, val3) 750971462beSGnoCiYeH } 751971462beSGnoCiYeH 752bf4a4899SLoGin SYS_READV => Self::readv(args[0] as i32, args[1], args[2]), 753971462beSGnoCiYeH SYS_WRITEV => Self::writev(args[0] as i32, args[1], args[2]), 754971462beSGnoCiYeH 7554fda81ceSLoGin SYS_SET_TID_ADDRESS => Self::set_tid_address(args[0]), 756971462beSGnoCiYeH 7574fda81ceSLoGin #[cfg(target_arch = "x86_64")] 7584fda81ceSLoGin SYS_LSTAT => { 759971462beSGnoCiYeH let path: &CStr = unsafe { CStr::from_ptr(args[0] as *const c_char) }; 760971462beSGnoCiYeH let path: Result<&str, core::str::Utf8Error> = path.to_str(); 761971462beSGnoCiYeH let res = if path.is_err() { 762971462beSGnoCiYeH Err(SystemError::EINVAL) 763971462beSGnoCiYeH } else { 764971462beSGnoCiYeH let path: &str = path.unwrap(); 765971462beSGnoCiYeH let kstat = args[1] as *mut PosixKstat; 766971462beSGnoCiYeH let vaddr = VirtAddr::new(kstat as usize); 767971462beSGnoCiYeH match verify_area(vaddr, core::mem::size_of::<PosixKstat>()) { 7684fda81ceSLoGin Ok(_) => Self::lstat(path, kstat), 7694fda81ceSLoGin Err(e) => Err(e), 7704fda81ceSLoGin } 7714fda81ceSLoGin }; 7724fda81ceSLoGin 7734fda81ceSLoGin res 7744fda81ceSLoGin } 7754fda81ceSLoGin 7764fda81ceSLoGin #[cfg(target_arch = "x86_64")] 7774fda81ceSLoGin SYS_STAT => { 7784fda81ceSLoGin let path: &CStr = unsafe { CStr::from_ptr(args[0] as *const c_char) }; 7794fda81ceSLoGin let path: Result<&str, core::str::Utf8Error> = path.to_str(); 7804fda81ceSLoGin let res = if path.is_err() { 7814fda81ceSLoGin Err(SystemError::EINVAL) 782bf4a4899SLoGin } else { 7834fda81ceSLoGin let path: &str = path.unwrap(); 7844fda81ceSLoGin let kstat = args[1] as *mut PosixKstat; 7854fda81ceSLoGin let vaddr = VirtAddr::new(kstat as usize); 7864fda81ceSLoGin match verify_area(vaddr, core::mem::size_of::<PosixKstat>()) { 7874fda81ceSLoGin Ok(_) => Self::stat(path, kstat), 788971462beSGnoCiYeH Err(e) => Err(e), 789971462beSGnoCiYeH } 790971462beSGnoCiYeH }; 791971462beSGnoCiYeH 792971462beSGnoCiYeH res 793971462beSGnoCiYeH } 794971462beSGnoCiYeH 79540609970SGnoCiYeH SYS_EPOLL_CREATE => Self::epoll_create(args[0] as i32), 79640609970SGnoCiYeH SYS_EPOLL_CREATE1 => Self::epoll_create1(args[0]), 79740609970SGnoCiYeH 79840609970SGnoCiYeH SYS_EPOLL_CTL => Self::epoll_ctl( 79940609970SGnoCiYeH args[0] as i32, 80040609970SGnoCiYeH args[1], 80140609970SGnoCiYeH args[2] as i32, 80240609970SGnoCiYeH VirtAddr::new(args[3]), 80340609970SGnoCiYeH ), 80440609970SGnoCiYeH 80540609970SGnoCiYeH SYS_EPOLL_WAIT => Self::epoll_wait( 80640609970SGnoCiYeH args[0] as i32, 80740609970SGnoCiYeH VirtAddr::new(args[1]), 80840609970SGnoCiYeH args[2] as i32, 80940609970SGnoCiYeH args[3] as i32, 81040609970SGnoCiYeH ), 81140609970SGnoCiYeH 81240609970SGnoCiYeH SYS_EPOLL_PWAIT => { 81340609970SGnoCiYeH let epfd = args[0] as i32; 81440609970SGnoCiYeH let epoll_event = VirtAddr::new(args[1]); 81540609970SGnoCiYeH let max_events = args[2] as i32; 81640609970SGnoCiYeH let timespec = args[3] as i32; 81740609970SGnoCiYeH let sigmask_addr = args[4] as *mut SigSet; 81840609970SGnoCiYeH 81940609970SGnoCiYeH if sigmask_addr.is_null() { 82040609970SGnoCiYeH return Self::epoll_wait(epfd, epoll_event, max_events, timespec); 82140609970SGnoCiYeH } 82240609970SGnoCiYeH let sigmask_reader = 82340609970SGnoCiYeH UserBufferReader::new(sigmask_addr, core::mem::size_of::<SigSet>(), true)?; 82440609970SGnoCiYeH let mut sigmask = sigmask_reader.read_one_from_user::<SigSet>(0)?.clone(); 82540609970SGnoCiYeH 82640609970SGnoCiYeH Self::epoll_pwait( 82740609970SGnoCiYeH args[0] as i32, 82840609970SGnoCiYeH VirtAddr::new(args[1]), 82940609970SGnoCiYeH args[2] as i32, 83040609970SGnoCiYeH args[3] as i32, 83140609970SGnoCiYeH &mut sigmask, 83240609970SGnoCiYeH ) 83340609970SGnoCiYeH } 83440609970SGnoCiYeH 835971462beSGnoCiYeH // 目前为了适配musl-libc,以下系统调用先这样写着 8364fda81ceSLoGin SYS_GETRANDOM => { 837971462beSGnoCiYeH let flags = GRandFlags::from_bits(args[2] as u8).ok_or(SystemError::EINVAL)?; 838971462beSGnoCiYeH Self::get_random(args[0] as *mut u8, args[1], flags) 839971462beSGnoCiYeH } 840971462beSGnoCiYeH 8414fda81ceSLoGin SYS_SOCKETPAIR => { 8424ad52e57S裕依2439 let mut user_buffer_writer = UserBufferWriter::new( 8434ad52e57S裕依2439 args[3] as *mut c_int, 8444ad52e57S裕依2439 core::mem::size_of::<[c_int; 2]>(), 8454ad52e57S裕依2439 frame.from_user(), 8464ad52e57S裕依2439 )?; 8474ad52e57S裕依2439 let fds = user_buffer_writer.buffer::<i32>(0)?; 8484ad52e57S裕依2439 Self::socketpair(args[0], args[1], args[2], fds) 849971462beSGnoCiYeH } 850971462beSGnoCiYeH 8514fda81ceSLoGin #[cfg(target_arch = "x86_64")] 852971462beSGnoCiYeH SYS_POLL => { 853971462beSGnoCiYeH kwarn!("SYS_POLL has not yet been implemented"); 854971462beSGnoCiYeH Ok(0) 855971462beSGnoCiYeH } 856971462beSGnoCiYeH 857*52da9a59SGnoCiYeH SYS_SETPGID => { 858*52da9a59SGnoCiYeH kwarn!("SYS_SETPGID has not yet been implemented"); 859*52da9a59SGnoCiYeH Ok(0) 860*52da9a59SGnoCiYeH } 861*52da9a59SGnoCiYeH 862971462beSGnoCiYeH SYS_RT_SIGPROCMASK => { 863971462beSGnoCiYeH kwarn!("SYS_RT_SIGPROCMASK has not yet been implemented"); 864971462beSGnoCiYeH Ok(0) 865971462beSGnoCiYeH } 866971462beSGnoCiYeH 867971462beSGnoCiYeH SYS_TKILL => { 868971462beSGnoCiYeH kwarn!("SYS_TKILL has not yet been implemented"); 869971462beSGnoCiYeH Ok(0) 870971462beSGnoCiYeH } 871971462beSGnoCiYeH 872971462beSGnoCiYeH SYS_SIGALTSTACK => { 873971462beSGnoCiYeH kwarn!("SYS_SIGALTSTACK has not yet been implemented"); 874971462beSGnoCiYeH Ok(0) 875971462beSGnoCiYeH } 876971462beSGnoCiYeH 87716033951SGnoCiYeH SYS_EXIT_GROUP => { 87816033951SGnoCiYeH kwarn!("SYS_EXIT_GROUP has not yet been implemented"); 87916033951SGnoCiYeH Ok(0) 88016033951SGnoCiYeH } 88116033951SGnoCiYeH 88204babc3fSMemoryShore SYS_MADVISE => { 8836c7f966cSMemoryShore // 这个太吵了,总是打印,先注释掉 8846c7f966cSMemoryShore // kwarn!("SYS_MADVISE has not yet been implemented"); 88504babc3fSMemoryShore Ok(0) 88604babc3fSMemoryShore } 887393f6915SLoGin SYS_GETTID => Self::gettid().map(|tid| tid.into()), 88802e249f3SLoGin SYS_GETUID => Self::getuid().map(|uid| uid.into()), 8898d72b68dSJomo 89002e249f3SLoGin SYS_SYSLOG => { 8918d72b68dSJomo let syslog_action_type = args[0] as usize; 8928d72b68dSJomo let buf_vaddr = args[1]; 8938d72b68dSJomo let len = args[2]; 8948d72b68dSJomo let from_user = frame.from_user(); 8958d72b68dSJomo let mut user_buffer_writer = 8968d72b68dSJomo UserBufferWriter::new(buf_vaddr as *mut u8, len, from_user)?; 8978d72b68dSJomo 8988d72b68dSJomo let user_buf = user_buffer_writer.buffer(0)?; 8998d72b68dSJomo let res = Self::do_syslog(syslog_action_type, user_buf, len); 9008d72b68dSJomo res 90102e249f3SLoGin } 9028d72b68dSJomo 90302e249f3SLoGin SYS_GETGID => Self::getgid().map(|gid| gid.into()), 90402e249f3SLoGin SYS_SETUID => { 90502e249f3SLoGin kwarn!("SYS_SETUID has not yet been implemented"); 90602e249f3SLoGin Ok(0) 90702e249f3SLoGin } 90802e249f3SLoGin SYS_SETGID => { 90902e249f3SLoGin kwarn!("SYS_SETGID has not yet been implemented"); 91002e249f3SLoGin Ok(0) 91102e249f3SLoGin } 91202e249f3SLoGin SYS_GETEUID => Self::geteuid().map(|euid| euid.into()), 91302e249f3SLoGin SYS_GETEGID => Self::getegid().map(|egid| egid.into()), 914be8cdf4bSLoGin SYS_GETRUSAGE => { 915be8cdf4bSLoGin let who = args[0] as c_int; 916be8cdf4bSLoGin let rusage = args[1] as *mut RUsage; 917be8cdf4bSLoGin Self::get_rusage(who, rusage) 918be8cdf4bSLoGin } 91904babc3fSMemoryShore 9204fda81ceSLoGin #[cfg(target_arch = "x86_64")] 921709498caSLoGin SYS_READLINK => { 922709498caSLoGin let path = args[0] as *const u8; 923709498caSLoGin let buf = args[1] as *mut u8; 924709498caSLoGin let bufsiz = args[2] as usize; 925709498caSLoGin Self::readlink(path, buf, bufsiz) 926709498caSLoGin } 927709498caSLoGin 9284fda81ceSLoGin SYS_READLINKAT => { 929709498caSLoGin let dirfd = args[0] as i32; 930709498caSLoGin let pathname = args[1] as *const u8; 931709498caSLoGin let buf = args[2] as *mut u8; 932709498caSLoGin let bufsiz = args[3] as usize; 933709498caSLoGin Self::readlink_at(dirfd, pathname, buf, bufsiz) 934709498caSLoGin } 935709498caSLoGin 9360d9b7d92SLoGin SYS_PRLIMIT64 => { 9370d9b7d92SLoGin let pid = args[0]; 9380d9b7d92SLoGin let pid = Pid::new(pid); 9390d9b7d92SLoGin let resource = args[1]; 9400d9b7d92SLoGin let new_limit = args[2] as *const RLimit64; 9410d9b7d92SLoGin let old_limit = args[3] as *mut RLimit64; 9420d9b7d92SLoGin 9430d9b7d92SLoGin Self::prlimit64(pid, resource, new_limit, old_limit) 9440d9b7d92SLoGin } 9450d9b7d92SLoGin 9464fda81ceSLoGin #[cfg(target_arch = "x86_64")] 9479b0abe6dSLoGin SYS_ACCESS => { 9489b0abe6dSLoGin let pathname = args[0] as *const u8; 9499b0abe6dSLoGin let mode = args[1] as u32; 9509b0abe6dSLoGin Self::access(pathname, mode) 9519b0abe6dSLoGin } 9529b0abe6dSLoGin 9539b0abe6dSLoGin SYS_FACCESSAT => { 9549b0abe6dSLoGin let dirfd = args[0] as i32; 9559b0abe6dSLoGin let pathname = args[1] as *const u8; 9569b0abe6dSLoGin let mode = args[2] as u32; 9579b0abe6dSLoGin Self::faccessat2(dirfd, pathname, mode, 0) 9589b0abe6dSLoGin } 9599b0abe6dSLoGin 9609b0abe6dSLoGin SYS_FACCESSAT2 => { 9619b0abe6dSLoGin let dirfd = args[0] as i32; 9629b0abe6dSLoGin let pathname = args[1] as *const u8; 9639b0abe6dSLoGin let mode = args[2] as u32; 9649b0abe6dSLoGin let flags = args[3] as u32; 9659b0abe6dSLoGin Self::faccessat2(dirfd, pathname, mode, flags) 9669b0abe6dSLoGin } 9679b0abe6dSLoGin 968bf4a4899SLoGin SYS_CLOCK_GETTIME => { 969bf4a4899SLoGin let clockid = args[0] as i32; 970bf4a4899SLoGin let timespec = args[1] as *mut TimeSpec; 971bf4a4899SLoGin Self::clock_gettime(clockid, timespec) 972bf4a4899SLoGin } 973bf4a4899SLoGin 974bf4a4899SLoGin SYS_SYSINFO => { 975bf4a4899SLoGin let info = args[0] as *mut SysInfo; 976bf4a4899SLoGin Self::sysinfo(info) 977bf4a4899SLoGin } 978bf4a4899SLoGin 979bf4a4899SLoGin SYS_UMASK => { 980bf4a4899SLoGin let mask = args[0] as u32; 981bf4a4899SLoGin Self::umask(mask) 982bf4a4899SLoGin } 983bf4a4899SLoGin 98427b967a3S裕依 SYS_FCHOWN => { 98527b967a3S裕依 kwarn!("SYS_FCHOWN has not yet been implemented"); 98627b967a3S裕依 Ok(0) 98727b967a3S裕依 } 98827b967a3S裕依 98927b967a3S裕依 SYS_FSYNC => { 99027b967a3S裕依 kwarn!("SYS_FSYNC has not yet been implemented"); 99127b967a3S裕依 Ok(0) 99227b967a3S裕依 } 99327b967a3S裕依 9944fda81ceSLoGin #[cfg(target_arch = "x86_64")] 995bf4a4899SLoGin SYS_CHMOD => { 996bf4a4899SLoGin let pathname = args[0] as *const u8; 997bf4a4899SLoGin let mode = args[1] as u32; 998bf4a4899SLoGin Self::chmod(pathname, mode) 999bf4a4899SLoGin } 1000bf4a4899SLoGin SYS_FCHMOD => { 1001bf4a4899SLoGin let fd = args[0] as i32; 1002bf4a4899SLoGin let mode = args[1] as u32; 1003bf4a4899SLoGin Self::fchmod(fd, mode) 1004bf4a4899SLoGin } 1005bf4a4899SLoGin SYS_FCHMODAT => { 1006bf4a4899SLoGin let dirfd = args[0] as i32; 1007bf4a4899SLoGin let pathname = args[1] as *const u8; 1008bf4a4899SLoGin let mode = args[2] as u32; 1009bf4a4899SLoGin Self::fchmodat(dirfd, pathname, mode) 1010bf4a4899SLoGin } 1011bf4a4899SLoGin 101202343d0bSLoGin SYS_SCHED_GETAFFINITY => { 101302343d0bSLoGin // todo: 这个系统调用还没有实现 101402343d0bSLoGin 101502343d0bSLoGin Err(SystemError::ENOSYS) 101602343d0bSLoGin } 101702343d0bSLoGin 1018*52da9a59SGnoCiYeH #[cfg(target_arch = "x86_64")] 1019*52da9a59SGnoCiYeH SYS_GETRLIMIT => { 1020*52da9a59SGnoCiYeH let resource = args[0]; 1021*52da9a59SGnoCiYeH let rlimit = args[1] as *mut RLimit64; 1022*52da9a59SGnoCiYeH 1023*52da9a59SGnoCiYeH Self::prlimit64( 1024*52da9a59SGnoCiYeH ProcessManager::current_pcb().pid(), 1025*52da9a59SGnoCiYeH resource, 1026*52da9a59SGnoCiYeH 0 as *const RLimit64, 1027*52da9a59SGnoCiYeH rlimit, 1028*52da9a59SGnoCiYeH ) 1029*52da9a59SGnoCiYeH } 1030*52da9a59SGnoCiYeH 103102343d0bSLoGin SYS_SCHED_YIELD => Self::sched_yield(), 103202343d0bSLoGin 1033ab5c8ca4Slogin _ => panic!("Unsupported syscall ID: {}", syscall_num), 1034ab5c8ca4Slogin }; 10354fda81ceSLoGin 1036ab5c8ca4Slogin return r; 1037ab5c8ca4Slogin } 1038ab5c8ca4Slogin 1039ab5c8ca4Slogin pub fn put_string( 1040ab5c8ca4Slogin s: *const u8, 1041ab5c8ca4Slogin front_color: u32, 1042ab5c8ca4Slogin back_color: u32, 1043ab5c8ca4Slogin ) -> Result<usize, SystemError> { 1044ab5c8ca4Slogin return Ok(unsafe { do_put_string(s, front_color, back_color) }); 1045ab5c8ca4Slogin } 1046ab5c8ca4Slogin 1047ab5c8ca4Slogin pub fn reboot() -> Result<usize, SystemError> { 10484fda81ceSLoGin unsafe { cpu_reset() }; 1049ab5c8ca4Slogin } 1050ab5c8ca4Slogin } 1051