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}, 1552da9a59SGnoCiYeH ProcessManager, 160d9b7d92SLoGin }, 17c3dc6f2fSLoGin syscall::user_access::check_and_clone_cstr, 18971462beSGnoCiYeH }; 1940314b30SXiaoye Zheng 2091e9d4abSLoGin use num_traits::FromPrimitive; 2191e9d4abSLoGin use system_error::SystemError; 22676b8ef6SMork 23ab5c8ca4Slogin use crate::{ 241496ba7bSLoGin arch::{cpu::cpu_reset, interrupt::TrapFrame, MMArch}, 254fda81ceSLoGin driver::base::block::SeekFrom, 26ab5c8ca4Slogin filesystem::vfs::{ 27*9e481b3bSTTaq fcntl::{AtFlags, FcntlCommand}, 28ab5c8ca4Slogin file::FileMode, 292dbef785SGnoCiYeH syscall::{ModeType, PosixKstat, SEEK_CUR, SEEK_END, SEEK_MAX, SEEK_SET}, 30ab5c8ca4Slogin MAX_PATHLEN, 31ab5c8ca4Slogin }, 321496ba7bSLoGin include::bindings::bindings::{PAGE_2M_SIZE, PAGE_4K_SIZE}, 33aa0367d6SLoGin kinfo, 3440fe15e0SLoGin libs::align::page_align_up, 35be63f3b2Shoumkh mm::{verify_area, MemoryManagementArch, VirtAddr}, 36ab5c8ca4Slogin net::syscall::SockAddr, 37971462beSGnoCiYeH process::{fork::CloneFlags, Pid}, 3836fd0130Shoumkh time::{ 3940fe15e0SLoGin syscall::{PosixTimeZone, PosixTimeval}, 4036fd0130Shoumkh TimeSpec, 4136fd0130Shoumkh }, 42ab5c8ca4Slogin }; 43ab5c8ca4Slogin 44bf4a4899SLoGin use self::{ 45bf4a4899SLoGin misc::SysInfo, 46bf4a4899SLoGin user_access::{UserBufferReader, UserBufferWriter}, 47bf4a4899SLoGin }; 484537ffb7SChiichen 49bf4a4899SLoGin pub mod misc; 5040fe15e0SLoGin pub mod user_access; 5140fe15e0SLoGin 52ad1d649eSGnoCiYeH // 与linux不一致的调用,在linux基础上累加 53ad1d649eSGnoCiYeH pub const SYS_PUT_STRING: usize = 100000; 54ad1d649eSGnoCiYeH pub const SYS_SBRK: usize = 100001; 55ab5c8ca4Slogin /// todo: 该系统调用与Linux不一致,将来需要删除该系统调用!!! 删的时候记得改C版本的libc 56ad1d649eSGnoCiYeH pub const SYS_CLOCK: usize = 100002; 57ad1d649eSGnoCiYeH pub const SYS_SCHED: usize = 100003; 5840314b30SXiaoye Zheng 59ab5c8ca4Slogin #[derive(Debug)] 60ab5c8ca4Slogin pub struct Syscall; 61ab5c8ca4Slogin 62ab5c8ca4Slogin impl Syscall { 63ab5c8ca4Slogin /// 初始化系统调用 645b59005fSLoGin #[inline(never)] 65ab5c8ca4Slogin pub fn init() -> Result<(), SystemError> { 66ab5c8ca4Slogin static INIT_FLAG: AtomicBool = AtomicBool::new(false); 67ab5c8ca4Slogin let prev = INIT_FLAG.swap(true, Ordering::SeqCst); 68ab5c8ca4Slogin if prev { 69ab5c8ca4Slogin panic!("Cannot initialize syscall more than once!"); 70ab5c8ca4Slogin } 715b59005fSLoGin kinfo!("Initializing syscall..."); 725b59005fSLoGin let r = crate::arch::syscall::arch_syscall_init(); 735b59005fSLoGin kinfo!("Syscall init successfully!"); 745b59005fSLoGin 755b59005fSLoGin return r; 76ab5c8ca4Slogin } 77ab5c8ca4Slogin /// @brief 系统调用分发器,用于分发系统调用。 78ab5c8ca4Slogin /// 79ab5c8ca4Slogin /// 这个函数内,需要根据系统调用号,调用对应的系统调用处理函数。 80ab5c8ca4Slogin /// 并且,对于用户态传入的指针参数,需要在本函数内进行越界检查,防止访问到内核空间。 814fda81ceSLoGin #[inline(never)] 829e9ffedfSPlucky923 pub fn handle( 839e9ffedfSPlucky923 syscall_num: usize, 849e9ffedfSPlucky923 args: &[usize], 859e9ffedfSPlucky923 frame: &mut TrapFrame, 869e9ffedfSPlucky923 ) -> Result<usize, SystemError> { 87ab5c8ca4Slogin let r = match syscall_num { 88ab5c8ca4Slogin SYS_PUT_STRING => { 89ab5c8ca4Slogin Self::put_string(args[0] as *const u8, args[1] as u32, args[2] as u32) 90ab5c8ca4Slogin } 914fda81ceSLoGin #[cfg(target_arch = "x86_64")] 92ab5c8ca4Slogin SYS_OPEN => { 93ab5c8ca4Slogin let path: &CStr = unsafe { CStr::from_ptr(args[0] as *const c_char) }; 94ab5c8ca4Slogin let path: Result<&str, core::str::Utf8Error> = path.to_str(); 95ab5c8ca4Slogin let res = if path.is_err() { 96ab5c8ca4Slogin Err(SystemError::EINVAL) 97ab5c8ca4Slogin } else { 98ab5c8ca4Slogin let path: &str = path.unwrap(); 9934e6d6c8Syuyi2439 100ab5c8ca4Slogin let flags = args[1]; 1010fb515b0SLoGin let mode = args[2]; 1020fb515b0SLoGin 103ab5c8ca4Slogin let open_flags: FileMode = FileMode::from_bits_truncate(flags as u32); 1040fb515b0SLoGin let mode = ModeType::from_bits(mode as u32).ok_or(SystemError::EINVAL)?; 1050fb515b0SLoGin Self::open(path, open_flags, mode, true) 1060fb515b0SLoGin }; 1070fb515b0SLoGin res 1080fb515b0SLoGin } 1090fb515b0SLoGin 110*9e481b3bSTTaq SYS_RENAME => { 111*9e481b3bSTTaq let oldname: *const u8 = args[0] as *const u8; 112*9e481b3bSTTaq let newname: *const u8 = args[1] as *const u8; 113*9e481b3bSTTaq Self::do_renameat2( 114*9e481b3bSTTaq AtFlags::AT_FDCWD.bits(), 115*9e481b3bSTTaq oldname, 116*9e481b3bSTTaq AtFlags::AT_FDCWD.bits(), 117*9e481b3bSTTaq newname, 118*9e481b3bSTTaq 0, 119*9e481b3bSTTaq ) 120*9e481b3bSTTaq } 121*9e481b3bSTTaq 122*9e481b3bSTTaq SYS_RENAMEAT => { 123*9e481b3bSTTaq let oldfd = args[0] as i32; 124*9e481b3bSTTaq let oldname: *const u8 = args[1] as *const u8; 125*9e481b3bSTTaq let newfd = args[2] as i32; 126*9e481b3bSTTaq let newname: *const u8 = args[3] as *const u8; 127*9e481b3bSTTaq Self::do_renameat2(oldfd, oldname, newfd, newname, 0) 128*9e481b3bSTTaq } 129*9e481b3bSTTaq 130*9e481b3bSTTaq SYS_RENAMEAT2 => { 131*9e481b3bSTTaq let oldfd = args[0] as i32; 132*9e481b3bSTTaq let oldname: *const u8 = args[1] as *const u8; 133*9e481b3bSTTaq let newfd = args[2] as i32; 134*9e481b3bSTTaq let newname: *const u8 = args[3] as *const u8; 135*9e481b3bSTTaq let flags = args[4] as u32; 136*9e481b3bSTTaq Self::do_renameat2(oldfd, oldname, newfd, newname, flags) 137*9e481b3bSTTaq } 138*9e481b3bSTTaq 1390fb515b0SLoGin SYS_OPENAT => { 1400fb515b0SLoGin let dirfd = args[0] as i32; 1410fb515b0SLoGin let path: &CStr = unsafe { CStr::from_ptr(args[1] as *const c_char) }; 1420fb515b0SLoGin let flags = args[2]; 1430fb515b0SLoGin let mode = args[3]; 1440fb515b0SLoGin 1450fb515b0SLoGin let path: Result<&str, core::str::Utf8Error> = path.to_str(); 1460fb515b0SLoGin let res = if path.is_err() { 1470fb515b0SLoGin Err(SystemError::EINVAL) 1480fb515b0SLoGin } else { 1490fb515b0SLoGin let path: &str = path.unwrap(); 1500fb515b0SLoGin 1510fb515b0SLoGin let open_flags: FileMode = 1520fb515b0SLoGin FileMode::from_bits(flags as u32).ok_or(SystemError::EINVAL)?; 1530fb515b0SLoGin let mode = ModeType::from_bits(mode as u32).ok_or(SystemError::EINVAL)?; 1540fb515b0SLoGin Self::openat(dirfd, path, open_flags, mode, true) 155ab5c8ca4Slogin }; 156ab5c8ca4Slogin res 157ab5c8ca4Slogin } 158ab5c8ca4Slogin SYS_CLOSE => { 159ab5c8ca4Slogin let fd = args[0]; 16027b967a3S裕依 Self::close(fd) 161ab5c8ca4Slogin } 162ab5c8ca4Slogin SYS_READ => { 163ab5c8ca4Slogin let fd = args[0] as i32; 164ab5c8ca4Slogin let buf_vaddr = args[1]; 165ab5c8ca4Slogin let len = args[2]; 1669e9ffedfSPlucky923 let from_user = frame.from_user(); 1679e9ffedfSPlucky923 let mut user_buffer_writer = 1689e9ffedfSPlucky923 UserBufferWriter::new(buf_vaddr as *mut u8, len, from_user)?; 1696d81180bSLoGin 1709e9ffedfSPlucky923 let user_buf = user_buffer_writer.buffer(0)?; 17127b967a3S裕依 Self::read(fd, user_buf) 172ab5c8ca4Slogin } 173ab5c8ca4Slogin SYS_WRITE => { 174ab5c8ca4Slogin let fd = args[0] as i32; 175ab5c8ca4Slogin let buf_vaddr = args[1]; 176ab5c8ca4Slogin let len = args[2]; 1779e9ffedfSPlucky923 let from_user = frame.from_user(); 1789e9ffedfSPlucky923 let user_buffer_reader = 1799e9ffedfSPlucky923 UserBufferReader::new(buf_vaddr as *const u8, len, from_user)?; 1806d81180bSLoGin 1819e9ffedfSPlucky923 let user_buf = user_buffer_reader.read_from_user(0)?; 18227b967a3S裕依 Self::write(fd, user_buf) 183ab5c8ca4Slogin } 184ab5c8ca4Slogin 185ab5c8ca4Slogin SYS_LSEEK => { 186ab5c8ca4Slogin let fd = args[0] as i32; 187ab5c8ca4Slogin let offset = args[1] as i64; 188ab5c8ca4Slogin let whence = args[2] as u32; 189ab5c8ca4Slogin 190ab5c8ca4Slogin let w = match whence { 191ab5c8ca4Slogin SEEK_SET => Ok(SeekFrom::SeekSet(offset)), 192ab5c8ca4Slogin SEEK_CUR => Ok(SeekFrom::SeekCurrent(offset)), 193ab5c8ca4Slogin SEEK_END => Ok(SeekFrom::SeekEnd(offset)), 194ab5c8ca4Slogin SEEK_MAX => Ok(SeekFrom::SeekEnd(0)), 195ab5c8ca4Slogin _ => Err(SystemError::EINVAL), 1969e9ffedfSPlucky923 }?; 197ab5c8ca4Slogin 198ab5c8ca4Slogin Self::lseek(fd, w) 199ab5c8ca4Slogin } 20027b967a3S裕依 20127b967a3S裕依 SYS_PREAD64 => { 20227b967a3S裕依 let fd = args[0] as i32; 20327b967a3S裕依 let buf_vaddr = args[1]; 20427b967a3S裕依 let len = args[2]; 20527b967a3S裕依 let offset = args[3]; 20627b967a3S裕依 20727b967a3S裕依 let mut user_buffer_writer = 20827b967a3S裕依 UserBufferWriter::new(buf_vaddr as *mut u8, len, frame.from_user())?; 20927b967a3S裕依 let buf = user_buffer_writer.buffer(0)?; 21027b967a3S裕依 Self::pread(fd, buf, len, offset) 21127b967a3S裕依 } 21227b967a3S裕依 21327b967a3S裕依 SYS_PWRITE64 => { 21427b967a3S裕依 let fd = args[0] as i32; 21527b967a3S裕依 let buf_vaddr = args[1]; 21627b967a3S裕依 let len = args[2]; 21727b967a3S裕依 let offset = args[3]; 21827b967a3S裕依 21927b967a3S裕依 let user_buffer_reader = 22027b967a3S裕依 UserBufferReader::new(buf_vaddr as *const u8, len, frame.from_user())?; 22127b967a3S裕依 22227b967a3S裕依 let buf = user_buffer_reader.read_from_user(0)?; 22327b967a3S裕依 Self::pwrite(fd, buf, len, offset) 22427b967a3S裕依 } 22527b967a3S裕依 22640314b30SXiaoye Zheng SYS_IOCTL => { 22740314b30SXiaoye Zheng let fd = args[0]; 22840314b30SXiaoye Zheng let cmd = args[1]; 22940314b30SXiaoye Zheng let data = args[2]; 23040314b30SXiaoye Zheng Self::ioctl(fd, cmd as u32, data) 23140314b30SXiaoye Zheng } 232ab5c8ca4Slogin 2334fda81ceSLoGin #[cfg(target_arch = "x86_64")] 2341496ba7bSLoGin SYS_FORK => Self::fork(frame), 2354fda81ceSLoGin #[cfg(target_arch = "x86_64")] 2361496ba7bSLoGin SYS_VFORK => Self::vfork(frame), 2371496ba7bSLoGin 238ab5c8ca4Slogin SYS_BRK => { 23940fe15e0SLoGin let new_brk = VirtAddr::new(args[0]); 24040fe15e0SLoGin Self::brk(new_brk).map(|vaddr| vaddr.data()) 241ab5c8ca4Slogin } 242ab5c8ca4Slogin 243ab5c8ca4Slogin SYS_SBRK => { 244ab5c8ca4Slogin let increment = args[0] as isize; 2451496ba7bSLoGin Self::sbrk(increment).map(|vaddr: VirtAddr| vaddr.data()) 246ab5c8ca4Slogin } 247ab5c8ca4Slogin 248ab5c8ca4Slogin SYS_REBOOT => Self::reboot(), 249ab5c8ca4Slogin 250ab5c8ca4Slogin SYS_CHDIR => { 251ab5c8ca4Slogin // Closure for checking arguments 252ab5c8ca4Slogin let chdir_check = |arg0: usize| { 253ab5c8ca4Slogin if arg0 == 0 { 254ab5c8ca4Slogin return Err(SystemError::EFAULT); 255ab5c8ca4Slogin } 256ab5c8ca4Slogin let path_ptr = arg0 as *const c_char; 257be63f3b2Shoumkh let virt_addr = VirtAddr::new(path_ptr as usize); 258ab5c8ca4Slogin // 权限校验 259ab5c8ca4Slogin if path_ptr.is_null() 2601496ba7bSLoGin || (frame.from_user() 2611496ba7bSLoGin && verify_area(virt_addr, PAGE_2M_SIZE as usize).is_err()) 262ab5c8ca4Slogin { 263ab5c8ca4Slogin return Err(SystemError::EINVAL); 264ab5c8ca4Slogin } 265ab5c8ca4Slogin let dest_path: &CStr = unsafe { CStr::from_ptr(path_ptr) }; 266ab5c8ca4Slogin let dest_path: &str = dest_path.to_str().map_err(|_| SystemError::EINVAL)?; 267ab5c8ca4Slogin if dest_path.len() == 0 { 268ab5c8ca4Slogin return Err(SystemError::EINVAL); 2691496ba7bSLoGin } else if dest_path.len() > MAX_PATHLEN as usize { 270ab5c8ca4Slogin return Err(SystemError::ENAMETOOLONG); 271ab5c8ca4Slogin } 272ab5c8ca4Slogin 273ab5c8ca4Slogin return Ok(dest_path); 274ab5c8ca4Slogin }; 275ab5c8ca4Slogin 2769e9ffedfSPlucky923 let r = chdir_check(args[0])?; 2779e9ffedfSPlucky923 Self::chdir(r) 278ab5c8ca4Slogin } 279ab5c8ca4Slogin 2804fda81ceSLoGin #[allow(unreachable_patterns)] 2814fda81ceSLoGin SYS_GETDENTS64 | SYS_GETDENTS => { 282ab5c8ca4Slogin let fd = args[0] as i32; 28334e6d6c8Syuyi2439 284ab5c8ca4Slogin let buf_vaddr = args[1]; 285ab5c8ca4Slogin let len = args[2]; 286f5df0e79SLoGin let virt_addr: VirtAddr = VirtAddr::new(buf_vaddr); 287ab5c8ca4Slogin // 判断缓冲区是否来自用户态,进行权限校验 2881496ba7bSLoGin let res = if frame.from_user() && verify_area(virt_addr, len as usize).is_err() { 289ab5c8ca4Slogin // 来自用户态,而buffer在内核态,这样的操作不被允许 290ab5c8ca4Slogin Err(SystemError::EPERM) 291ab5c8ca4Slogin } else if buf_vaddr == 0 { 292ab5c8ca4Slogin Err(SystemError::EFAULT) 293ab5c8ca4Slogin } else { 294ab5c8ca4Slogin let buf: &mut [u8] = unsafe { 295ab5c8ca4Slogin core::slice::from_raw_parts_mut::<'static, u8>(buf_vaddr as *mut u8, len) 296ab5c8ca4Slogin }; 297ab5c8ca4Slogin Self::getdents(fd, buf) 298ab5c8ca4Slogin }; 299ab5c8ca4Slogin 300ab5c8ca4Slogin res 301ab5c8ca4Slogin } 302ab5c8ca4Slogin 303ab5c8ca4Slogin SYS_EXECVE => { 304ab5c8ca4Slogin let path_ptr = args[0]; 305ab5c8ca4Slogin let argv_ptr = args[1]; 306ab5c8ca4Slogin let env_ptr = args[2]; 307be63f3b2Shoumkh let virt_path_ptr = VirtAddr::new(path_ptr); 308be63f3b2Shoumkh let virt_argv_ptr = VirtAddr::new(argv_ptr); 309be63f3b2Shoumkh let virt_env_ptr = VirtAddr::new(env_ptr); 310ab5c8ca4Slogin // 权限校验 3111496ba7bSLoGin if frame.from_user() 3121496ba7bSLoGin && (verify_area(virt_path_ptr, MAX_PATHLEN as usize).is_err() 313be63f3b2Shoumkh || verify_area(virt_argv_ptr, PAGE_4K_SIZE as usize).is_err()) 314be63f3b2Shoumkh || verify_area(virt_env_ptr, PAGE_4K_SIZE as usize).is_err() 315ab5c8ca4Slogin { 316ab5c8ca4Slogin Err(SystemError::EFAULT) 317ab5c8ca4Slogin } else { 318ab5c8ca4Slogin Self::execve( 3191496ba7bSLoGin path_ptr as *const u8, 3201496ba7bSLoGin argv_ptr as *const *const u8, 3211496ba7bSLoGin env_ptr as *const *const u8, 3221496ba7bSLoGin frame, 323ab5c8ca4Slogin ) 3241496ba7bSLoGin .map(|_| 0) 325ab5c8ca4Slogin } 326ab5c8ca4Slogin } 327ab5c8ca4Slogin SYS_WAIT4 => { 328bf4a4899SLoGin let pid = args[0] as i32; 3291496ba7bSLoGin let wstatus = args[1] as *mut i32; 330ab5c8ca4Slogin let options = args[2] as c_int; 331ab5c8ca4Slogin let rusage = args[3] as *mut c_void; 332ab5c8ca4Slogin // 权限校验 333ab5c8ca4Slogin // todo: 引入rusage之后,更正以下权限校验代码中,rusage的大小 334bf4a4899SLoGin Self::wait4(pid.into(), wstatus, options, rusage) 335ab5c8ca4Slogin } 336ab5c8ca4Slogin 337ab5c8ca4Slogin SYS_EXIT => { 338ab5c8ca4Slogin let exit_code = args[0]; 339ab5c8ca4Slogin Self::exit(exit_code) 340ab5c8ca4Slogin } 3414fda81ceSLoGin #[cfg(target_arch = "x86_64")] 342ab5c8ca4Slogin SYS_MKDIR => { 343ab5c8ca4Slogin let path_ptr = args[0] as *const c_char; 344ab5c8ca4Slogin let mode = args[1]; 345be63f3b2Shoumkh let virt_path_ptr = VirtAddr::new(path_ptr as usize); 346ab5c8ca4Slogin let security_check = || { 347ab5c8ca4Slogin if path_ptr.is_null() 3481496ba7bSLoGin || (frame.from_user() 3491496ba7bSLoGin && verify_area(virt_path_ptr, PAGE_2M_SIZE as usize).is_err()) 350ab5c8ca4Slogin { 351ab5c8ca4Slogin return Err(SystemError::EINVAL); 352ab5c8ca4Slogin } 353ab5c8ca4Slogin let path: &CStr = unsafe { CStr::from_ptr(path_ptr) }; 354ab5c8ca4Slogin let path: &str = path.to_str().map_err(|_| SystemError::EINVAL)?.trim(); 355ab5c8ca4Slogin 356ab5c8ca4Slogin if path == "" { 357ab5c8ca4Slogin return Err(SystemError::EINVAL); 358ab5c8ca4Slogin } 359ab5c8ca4Slogin return Ok(path); 360ab5c8ca4Slogin }; 361ab5c8ca4Slogin 362ab5c8ca4Slogin let path = security_check(); 363ab5c8ca4Slogin if path.is_err() { 364ab5c8ca4Slogin Err(path.unwrap_err()) 365ab5c8ca4Slogin } else { 366ab5c8ca4Slogin Self::mkdir(path.unwrap(), mode) 367ab5c8ca4Slogin } 368ab5c8ca4Slogin } 369ab5c8ca4Slogin 370ab5c8ca4Slogin SYS_NANOSLEEP => { 371ab5c8ca4Slogin let req = args[0] as *const TimeSpec; 372ab5c8ca4Slogin let rem = args[1] as *mut TimeSpec; 373be63f3b2Shoumkh let virt_req = VirtAddr::new(req as usize); 374be63f3b2Shoumkh let virt_rem = VirtAddr::new(rem as usize); 3751496ba7bSLoGin if frame.from_user() 376be63f3b2Shoumkh && (verify_area(virt_req, core::mem::size_of::<TimeSpec>() as usize).is_err() 377be63f3b2Shoumkh || verify_area(virt_rem, core::mem::size_of::<TimeSpec>() as usize) 378be63f3b2Shoumkh .is_err()) 379ab5c8ca4Slogin { 380ab5c8ca4Slogin Err(SystemError::EFAULT) 381ab5c8ca4Slogin } else { 382ab5c8ca4Slogin Self::nanosleep(req, rem) 383ab5c8ca4Slogin } 384ab5c8ca4Slogin } 385ab5c8ca4Slogin 386ab5c8ca4Slogin SYS_CLOCK => Self::clock(), 3874fda81ceSLoGin #[cfg(target_arch = "x86_64")] 388ab5c8ca4Slogin SYS_PIPE => { 38922c9db31Shanjiezhou let pipefd: *mut i32 = args[0] as *mut c_int; 3900fb515b0SLoGin if pipefd.is_null() { 3910fb515b0SLoGin Err(SystemError::EFAULT) 3920fb515b0SLoGin } else { 3930fb515b0SLoGin Self::pipe2(pipefd, FileMode::empty()) 3940fb515b0SLoGin } 3950fb515b0SLoGin } 3964ad52e57S裕依2439 3970fb515b0SLoGin SYS_PIPE2 => { 3980fb515b0SLoGin let pipefd: *mut i32 = args[0] as *mut c_int; 39922c9db31Shanjiezhou let arg1 = args[1]; 40022c9db31Shanjiezhou if pipefd.is_null() { 40122c9db31Shanjiezhou Err(SystemError::EFAULT) 40222c9db31Shanjiezhou } else { 40322c9db31Shanjiezhou let flags = FileMode::from_bits_truncate(arg1 as u32); 40422c9db31Shanjiezhou Self::pipe2(pipefd, flags) 405ab5c8ca4Slogin } 406ab5c8ca4Slogin } 407ab5c8ca4Slogin 4084fda81ceSLoGin SYS_UNLINKAT => { 409ab5c8ca4Slogin let dirfd = args[0] as i32; 410ab5c8ca4Slogin let pathname = args[1] as *const c_char; 411ab5c8ca4Slogin let flags = args[2] as u32; 412be63f3b2Shoumkh let virt_pathname = VirtAddr::new(pathname as usize); 4131496ba7bSLoGin if frame.from_user() && verify_area(virt_pathname, PAGE_4K_SIZE as usize).is_err() { 414ab5c8ca4Slogin Err(SystemError::EFAULT) 415ab5c8ca4Slogin } else if pathname.is_null() { 416ab5c8ca4Slogin Err(SystemError::EFAULT) 417ab5c8ca4Slogin } else { 418ab5c8ca4Slogin let get_path = || { 419ab5c8ca4Slogin let pathname: &CStr = unsafe { CStr::from_ptr(pathname) }; 420ab5c8ca4Slogin 421ab5c8ca4Slogin let pathname: &str = pathname.to_str().map_err(|_| SystemError::EINVAL)?; 422ab5c8ca4Slogin if pathname.len() >= MAX_PATHLEN { 423ab5c8ca4Slogin return Err(SystemError::ENAMETOOLONG); 424ab5c8ca4Slogin } 425ab5c8ca4Slogin return Ok(pathname.trim()); 426ab5c8ca4Slogin }; 427ab5c8ca4Slogin let pathname = get_path(); 428ab5c8ca4Slogin if pathname.is_err() { 429ab5c8ca4Slogin Err(pathname.unwrap_err()) 430ab5c8ca4Slogin } else { 4316d81180bSLoGin // kdebug!("sys unlinkat: dirfd: {}, pathname: {}", dirfd, pathname.as_ref().unwrap()); 432ab5c8ca4Slogin Self::unlinkat(dirfd, pathname.unwrap(), flags) 433ab5c8ca4Slogin } 434ab5c8ca4Slogin } 435ab5c8ca4Slogin } 436bf4a4899SLoGin 4374fda81ceSLoGin #[cfg(target_arch = "x86_64")] 4385eeefb8cSChenzx SYS_RMDIR => { 4395eeefb8cSChenzx let pathname = args[0] as *const u8; 4405eeefb8cSChenzx Self::rmdir(pathname) 4415eeefb8cSChenzx } 4425eeefb8cSChenzx 4435eeefb8cSChenzx #[cfg(target_arch = "x86_64")] 444bf4a4899SLoGin SYS_UNLINK => { 445bf4a4899SLoGin let pathname = args[0] as *const u8; 446bf4a4899SLoGin Self::unlink(pathname) 447bf4a4899SLoGin } 448ab5c8ca4Slogin SYS_KILL => { 4491496ba7bSLoGin let pid = Pid::new(args[0]); 450ab5c8ca4Slogin let sig = args[1] as c_int; 4513c82aa56SChiichen // kdebug!("KILL SYSCALL RECEIVED"); 452ab5c8ca4Slogin Self::kill(pid, sig) 453ab5c8ca4Slogin } 454ab5c8ca4Slogin 4554fda81ceSLoGin SYS_RT_SIGACTION => { 456ab5c8ca4Slogin let sig = args[0] as c_int; 457ab5c8ca4Slogin let act = args[1]; 458ab5c8ca4Slogin let old_act = args[2]; 4591496ba7bSLoGin Self::sigaction(sig, act, old_act, frame.from_user()) 460ab5c8ca4Slogin } 461ab5c8ca4Slogin 4621496ba7bSLoGin SYS_GETPID => Self::getpid().map(|pid| pid.into()), 463ab5c8ca4Slogin 4641496ba7bSLoGin SYS_SCHED => Self::sched(frame.from_user()), 465ab5c8ca4Slogin SYS_DUP => { 466ab5c8ca4Slogin let oldfd: i32 = args[0] as c_int; 467ab5c8ca4Slogin Self::dup(oldfd) 468ab5c8ca4Slogin } 4694fda81ceSLoGin 4704fda81ceSLoGin #[cfg(target_arch = "x86_64")] 471ab5c8ca4Slogin SYS_DUP2 => { 472ab5c8ca4Slogin let oldfd: i32 = args[0] as c_int; 473ab5c8ca4Slogin let newfd: i32 = args[1] as c_int; 474ab5c8ca4Slogin Self::dup2(oldfd, newfd) 475ab5c8ca4Slogin } 476ab5c8ca4Slogin 477ab5c8ca4Slogin SYS_SOCKET => Self::socket(args[0], args[1], args[2]), 478ab5c8ca4Slogin SYS_SETSOCKOPT => { 479ab5c8ca4Slogin let optval = args[3] as *const u8; 480ab5c8ca4Slogin let optlen = args[4] as usize; 481be63f3b2Shoumkh let virt_optval = VirtAddr::new(optval as usize); 482ab5c8ca4Slogin // 验证optval的地址是否合法 483be63f3b2Shoumkh if verify_area(virt_optval, optlen as usize).is_err() { 484ab5c8ca4Slogin // 地址空间超出了用户空间的范围,不合法 485ab5c8ca4Slogin Err(SystemError::EFAULT) 486ab5c8ca4Slogin } else { 487ab5c8ca4Slogin let data: &[u8] = unsafe { core::slice::from_raw_parts(optval, optlen) }; 488ab5c8ca4Slogin Self::setsockopt(args[0], args[1], args[2], data) 489ab5c8ca4Slogin } 490ab5c8ca4Slogin } 491ab5c8ca4Slogin SYS_GETSOCKOPT => { 492ab5c8ca4Slogin let optval = args[3] as *mut u8; 493ab5c8ca4Slogin let optlen = args[4] as *mut usize; 494be63f3b2Shoumkh let virt_optval = VirtAddr::new(optval as usize); 495be63f3b2Shoumkh let virt_optlen = VirtAddr::new(optlen as usize); 496ab5c8ca4Slogin let security_check = || { 497ab5c8ca4Slogin // 验证optval的地址是否合法 498be63f3b2Shoumkh if verify_area(virt_optval, PAGE_4K_SIZE as usize).is_err() { 499ab5c8ca4Slogin // 地址空间超出了用户空间的范围,不合法 500ab5c8ca4Slogin return Err(SystemError::EFAULT); 501ab5c8ca4Slogin } 502ab5c8ca4Slogin 503ab5c8ca4Slogin // 验证optlen的地址是否合法 504be63f3b2Shoumkh if verify_area(virt_optlen, core::mem::size_of::<u32>() as usize).is_err() { 505ab5c8ca4Slogin // 地址空间超出了用户空间的范围,不合法 506ab5c8ca4Slogin return Err(SystemError::EFAULT); 507ab5c8ca4Slogin } 508ab5c8ca4Slogin return Ok(()); 509ab5c8ca4Slogin }; 510ab5c8ca4Slogin let r = security_check(); 511ab5c8ca4Slogin if r.is_err() { 512ab5c8ca4Slogin Err(r.unwrap_err()) 513ab5c8ca4Slogin } else { 514ab5c8ca4Slogin Self::getsockopt(args[0], args[1], args[2], optval, optlen as *mut u32) 515ab5c8ca4Slogin } 516ab5c8ca4Slogin } 517ab5c8ca4Slogin 518ab5c8ca4Slogin SYS_CONNECT => { 519ab5c8ca4Slogin let addr = args[1] as *const SockAddr; 520ab5c8ca4Slogin let addrlen = args[2] as usize; 521be63f3b2Shoumkh let virt_addr = VirtAddr::new(addr as usize); 522ab5c8ca4Slogin // 验证addr的地址是否合法 523be63f3b2Shoumkh if verify_area(virt_addr, addrlen as usize).is_err() { 524ab5c8ca4Slogin // 地址空间超出了用户空间的范围,不合法 525ab5c8ca4Slogin Err(SystemError::EFAULT) 526ab5c8ca4Slogin } else { 527ab5c8ca4Slogin Self::connect(args[0], addr, addrlen) 528ab5c8ca4Slogin } 529ab5c8ca4Slogin } 530ab5c8ca4Slogin SYS_BIND => { 531ab5c8ca4Slogin let addr = args[1] as *const SockAddr; 532ab5c8ca4Slogin let addrlen = args[2] as usize; 533be63f3b2Shoumkh let virt_addr = VirtAddr::new(addr as usize); 534ab5c8ca4Slogin // 验证addr的地址是否合法 535be63f3b2Shoumkh if verify_area(virt_addr, addrlen as usize).is_err() { 536ab5c8ca4Slogin // 地址空间超出了用户空间的范围,不合法 537ab5c8ca4Slogin Err(SystemError::EFAULT) 538ab5c8ca4Slogin } else { 539ab5c8ca4Slogin Self::bind(args[0], addr, addrlen) 540ab5c8ca4Slogin } 541ab5c8ca4Slogin } 542ab5c8ca4Slogin 543ab5c8ca4Slogin SYS_SENDTO => { 544ab5c8ca4Slogin let buf = args[1] as *const u8; 545ab5c8ca4Slogin let len = args[2] as usize; 546ab5c8ca4Slogin let flags = args[3] as u32; 547ab5c8ca4Slogin let addr = args[4] as *const SockAddr; 548ab5c8ca4Slogin let addrlen = args[5] as usize; 549be63f3b2Shoumkh let virt_buf = VirtAddr::new(buf as usize); 550be63f3b2Shoumkh let virt_addr = VirtAddr::new(addr as usize); 551ab5c8ca4Slogin // 验证buf的地址是否合法 552be63f3b2Shoumkh if verify_area(virt_buf, len as usize).is_err() { 553ab5c8ca4Slogin // 地址空间超出了用户空间的范围,不合法 554ab5c8ca4Slogin Err(SystemError::EFAULT) 555be63f3b2Shoumkh } else if verify_area(virt_addr, addrlen as usize).is_err() { 556ab5c8ca4Slogin // 地址空间超出了用户空间的范围,不合法 557ab5c8ca4Slogin Err(SystemError::EFAULT) 558ab5c8ca4Slogin } else { 559ab5c8ca4Slogin let data: &[u8] = unsafe { core::slice::from_raw_parts(buf, len) }; 560ab5c8ca4Slogin Self::sendto(args[0], data, flags, addr, addrlen) 561ab5c8ca4Slogin } 562ab5c8ca4Slogin } 563ab5c8ca4Slogin 564ab5c8ca4Slogin SYS_RECVFROM => { 565ab5c8ca4Slogin let buf = args[1] as *mut u8; 566ab5c8ca4Slogin let len = args[2] as usize; 567ab5c8ca4Slogin let flags = args[3] as u32; 568ab5c8ca4Slogin let addr = args[4] as *mut SockAddr; 569ab5c8ca4Slogin let addrlen = args[5] as *mut usize; 570be63f3b2Shoumkh let virt_buf = VirtAddr::new(buf as usize); 571be63f3b2Shoumkh let virt_addrlen = VirtAddr::new(addrlen as usize); 572be63f3b2Shoumkh let virt_addr = VirtAddr::new(addr as usize); 573ab5c8ca4Slogin let security_check = || { 574ab5c8ca4Slogin // 验证buf的地址是否合法 575840045afSLoGin if verify_area(virt_buf, len).is_err() { 576ab5c8ca4Slogin // 地址空间超出了用户空间的范围,不合法 577ab5c8ca4Slogin return Err(SystemError::EFAULT); 578ab5c8ca4Slogin } 579ab5c8ca4Slogin 580ab5c8ca4Slogin // 验证addrlen的地址是否合法 581840045afSLoGin if verify_area(virt_addrlen, core::mem::size_of::<u32>()).is_err() { 582ab5c8ca4Slogin // 地址空间超出了用户空间的范围,不合法 583ab5c8ca4Slogin return Err(SystemError::EFAULT); 584ab5c8ca4Slogin } 585ab5c8ca4Slogin 586840045afSLoGin if verify_area(virt_addr, core::mem::size_of::<SockAddr>()).is_err() { 587ab5c8ca4Slogin // 地址空间超出了用户空间的范围,不合法 588ab5c8ca4Slogin return Err(SystemError::EFAULT); 589ab5c8ca4Slogin } 590ab5c8ca4Slogin return Ok(()); 591ab5c8ca4Slogin }; 592ab5c8ca4Slogin let r = security_check(); 593ab5c8ca4Slogin if r.is_err() { 594ab5c8ca4Slogin Err(r.unwrap_err()) 595ab5c8ca4Slogin } else { 596ab5c8ca4Slogin let buf = unsafe { core::slice::from_raw_parts_mut(buf, len) }; 597ab5c8ca4Slogin Self::recvfrom(args[0], buf, flags, addr, addrlen as *mut u32) 598ab5c8ca4Slogin } 599ab5c8ca4Slogin } 600ab5c8ca4Slogin 601ab5c8ca4Slogin SYS_RECVMSG => { 6024ad52e57S裕依2439 let msg = args[1] as *mut MsgHdr; 603ab5c8ca4Slogin let flags = args[2] as u32; 6044ad52e57S裕依2439 6054ad52e57S裕依2439 let mut user_buffer_writer = 6064ad52e57S裕依2439 UserBufferWriter::new(msg, core::mem::size_of::<MsgHdr>(), frame.from_user())?; 6074ad52e57S裕依2439 let buffer = user_buffer_writer.buffer::<MsgHdr>(0)?; 6084ad52e57S裕依2439 6094537ffb7SChiichen let msg = &mut buffer[0]; 610ab5c8ca4Slogin Self::recvmsg(args[0], msg, flags) 611ab5c8ca4Slogin } 612ab5c8ca4Slogin 613ab5c8ca4Slogin SYS_LISTEN => Self::listen(args[0], args[1]), 614ab5c8ca4Slogin SYS_SHUTDOWN => Self::shutdown(args[0], args[1]), 615ab5c8ca4Slogin SYS_ACCEPT => Self::accept(args[0], args[1] as *mut SockAddr, args[2] as *mut u32), 616c47fe904SLoGin SYS_ACCEPT4 => Self::accept4( 617c47fe904SLoGin args[0], 618c47fe904SLoGin args[1] as *mut SockAddr, 619c47fe904SLoGin args[2] as *mut u32, 620c47fe904SLoGin args[3] as u32, 621c47fe904SLoGin ), 622ab5c8ca4Slogin SYS_GETSOCKNAME => { 623ab5c8ca4Slogin Self::getsockname(args[0], args[1] as *mut SockAddr, args[2] as *mut u32) 624ab5c8ca4Slogin } 625ab5c8ca4Slogin SYS_GETPEERNAME => { 626ab5c8ca4Slogin Self::getpeername(args[0], args[1] as *mut SockAddr, args[2] as *mut u32) 627ab5c8ca4Slogin } 62836fd0130Shoumkh SYS_GETTIMEOFDAY => { 62936fd0130Shoumkh let timeval = args[0] as *mut PosixTimeval; 63040fe15e0SLoGin let timezone_ptr = args[1] as *mut PosixTimeZone; 63140fe15e0SLoGin Self::gettimeofday(timeval, timezone_ptr) 6324537ffb7SChiichen } 63340fe15e0SLoGin SYS_MMAP => { 63440fe15e0SLoGin let len = page_align_up(args[1]); 635840045afSLoGin let virt_addr = VirtAddr::new(args[0]); 636840045afSLoGin if verify_area(virt_addr, len).is_err() { 63740fe15e0SLoGin Err(SystemError::EFAULT) 63840fe15e0SLoGin } else { 63940fe15e0SLoGin Self::mmap( 64040fe15e0SLoGin VirtAddr::new(args[0]), 64140fe15e0SLoGin len, 64240fe15e0SLoGin args[2], 64340fe15e0SLoGin args[3], 64440fe15e0SLoGin args[4] as i32, 64540fe15e0SLoGin args[5], 64640fe15e0SLoGin ) 64740fe15e0SLoGin } 64840fe15e0SLoGin } 6494cfa009bSJomo SYS_MREMAP => { 6504cfa009bSJomo let old_vaddr = VirtAddr::new(args[0]); 6514cfa009bSJomo let old_len = args[1]; 6524cfa009bSJomo let new_len = args[2]; 6534cfa009bSJomo let mremap_flags = MremapFlags::from_bits_truncate(args[3] as u8); 6544cfa009bSJomo let new_vaddr = VirtAddr::new(args[4]); 6554cfa009bSJomo 6564cfa009bSJomo Self::mremap(old_vaddr, old_len, new_len, mremap_flags, new_vaddr) 6574cfa009bSJomo } 65840fe15e0SLoGin SYS_MUNMAP => { 65940fe15e0SLoGin let addr = args[0]; 66040fe15e0SLoGin let len = page_align_up(args[1]); 661971462beSGnoCiYeH if addr & (MMArch::PAGE_SIZE - 1) != 0 { 66240fe15e0SLoGin // The addr argument is not a multiple of the page size 66340fe15e0SLoGin Err(SystemError::EINVAL) 66440fe15e0SLoGin } else { 66540fe15e0SLoGin Self::munmap(VirtAddr::new(addr), len) 66640fe15e0SLoGin } 66740fe15e0SLoGin } 66840fe15e0SLoGin SYS_MPROTECT => { 66940fe15e0SLoGin let addr = args[0]; 67040fe15e0SLoGin let len = page_align_up(args[1]); 671971462beSGnoCiYeH if addr & (MMArch::PAGE_SIZE - 1) != 0 { 67240fe15e0SLoGin // The addr argument is not a multiple of the page size 67340fe15e0SLoGin Err(SystemError::EINVAL) 67440fe15e0SLoGin } else { 67540fe15e0SLoGin Self::mprotect(VirtAddr::new(addr), len, args[2]) 67640fe15e0SLoGin } 67740fe15e0SLoGin } 67840fe15e0SLoGin 6791496ba7bSLoGin SYS_GETCWD => { 6801496ba7bSLoGin let buf = args[0] as *mut u8; 681840045afSLoGin let size = args[1]; 6821496ba7bSLoGin let security_check = || { 6831496ba7bSLoGin verify_area(VirtAddr::new(buf as usize), size)?; 6841496ba7bSLoGin return Ok(()); 6851496ba7bSLoGin }; 6861496ba7bSLoGin let r = security_check(); 6871496ba7bSLoGin if r.is_err() { 6881496ba7bSLoGin Err(r.unwrap_err()) 6891496ba7bSLoGin } else { 6901496ba7bSLoGin let buf = unsafe { core::slice::from_raw_parts_mut(buf, size) }; 6911496ba7bSLoGin Self::getcwd(buf).map(|ptr| ptr.data()) 6921496ba7bSLoGin } 6931496ba7bSLoGin } 6941496ba7bSLoGin 6951496ba7bSLoGin SYS_GETPGID => Self::getpgid(Pid::new(args[0])).map(|pid| pid.into()), 6961496ba7bSLoGin 6971496ba7bSLoGin SYS_GETPPID => Self::getppid().map(|pid| pid.into()), 698be63f3b2Shoumkh SYS_FSTAT => { 699be63f3b2Shoumkh let fd = args[0] as i32; 700be63f3b2Shoumkh let kstat = args[1] as *mut PosixKstat; 701be63f3b2Shoumkh let vaddr = VirtAddr::new(kstat as usize); 702be63f3b2Shoumkh // FIXME 由于c中的verify_area与rust中的verify_area重名,所以在引入时加了前缀区分 703be63f3b2Shoumkh // TODO 应该将用了c版本的verify_area都改为rust的verify_area 704be63f3b2Shoumkh match verify_area(vaddr, core::mem::size_of::<PosixKstat>()) { 705be63f3b2Shoumkh Ok(_) => Self::fstat(fd, kstat), 706be63f3b2Shoumkh Err(e) => Err(e), 707be63f3b2Shoumkh } 708be63f3b2Shoumkh } 709be63f3b2Shoumkh 7106d81180bSLoGin SYS_FCNTL => { 7116d81180bSLoGin let fd = args[0] as i32; 7126d81180bSLoGin let cmd: Option<FcntlCommand> = 7136d81180bSLoGin <FcntlCommand as FromPrimitive>::from_u32(args[1] as u32); 7146d81180bSLoGin let arg = args[2] as i32; 7156d81180bSLoGin let res = if let Some(cmd) = cmd { 7166d81180bSLoGin Self::fcntl(fd, cmd, arg) 7176d81180bSLoGin } else { 7186d81180bSLoGin Err(SystemError::EINVAL) 7196d81180bSLoGin }; 7206d81180bSLoGin 7216d81180bSLoGin // kdebug!("FCNTL: fd: {}, cmd: {:?}, arg: {}, res: {:?}", fd, cmd, arg, res); 7226d81180bSLoGin res 7236d81180bSLoGin } 7246d81180bSLoGin 7256d81180bSLoGin SYS_FTRUNCATE => { 7266d81180bSLoGin let fd = args[0] as i32; 727840045afSLoGin let len = args[1]; 7286d81180bSLoGin let res = Self::ftruncate(fd, len); 7296d81180bSLoGin // kdebug!("FTRUNCATE: fd: {}, len: {}, res: {:?}", fd, len, res); 7306d81180bSLoGin res 7316d81180bSLoGin } 7326d81180bSLoGin 7334fda81ceSLoGin #[cfg(target_arch = "x86_64")] 7342dbef785SGnoCiYeH SYS_MKNOD => { 7352dbef785SGnoCiYeH let path = args[0]; 7362dbef785SGnoCiYeH let flags = args[1]; 7372dbef785SGnoCiYeH let dev_t = args[2]; 7382dbef785SGnoCiYeH let flags: ModeType = ModeType::from_bits_truncate(flags as u32); 739c566df45SLoGin Self::mknod(path as *const i8, flags, DeviceNumber::from(dev_t as u32)) 7402dbef785SGnoCiYeH } 7412dbef785SGnoCiYeH 742971462beSGnoCiYeH SYS_CLONE => { 743971462beSGnoCiYeH let parent_tid = VirtAddr::new(args[2]); 744971462beSGnoCiYeH let child_tid = VirtAddr::new(args[3]); 745971462beSGnoCiYeH 746971462beSGnoCiYeH // 地址校验 747971462beSGnoCiYeH verify_area(parent_tid, core::mem::size_of::<i32>())?; 748971462beSGnoCiYeH verify_area(child_tid, core::mem::size_of::<i32>())?; 749971462beSGnoCiYeH 750971462beSGnoCiYeH let mut clone_args = KernelCloneArgs::new(); 751971462beSGnoCiYeH clone_args.flags = CloneFlags::from_bits_truncate(args[0] as u64); 752971462beSGnoCiYeH clone_args.stack = args[1]; 753971462beSGnoCiYeH clone_args.parent_tid = parent_tid; 754971462beSGnoCiYeH clone_args.child_tid = child_tid; 755971462beSGnoCiYeH clone_args.tls = args[4]; 756971462beSGnoCiYeH Self::clone(frame, clone_args) 757971462beSGnoCiYeH } 758971462beSGnoCiYeH 759971462beSGnoCiYeH SYS_FUTEX => { 760971462beSGnoCiYeH let uaddr = VirtAddr::new(args[0]); 761971462beSGnoCiYeH let operation = FutexFlag::from_bits(args[1] as u32).ok_or(SystemError::ENOSYS)?; 762971462beSGnoCiYeH let val = args[2] as u32; 763971462beSGnoCiYeH let utime = args[3]; 764971462beSGnoCiYeH let uaddr2 = VirtAddr::new(args[4]); 765971462beSGnoCiYeH let val3 = args[5] as u32; 766971462beSGnoCiYeH 767971462beSGnoCiYeH verify_area(uaddr, core::mem::size_of::<u32>())?; 768971462beSGnoCiYeH verify_area(uaddr2, core::mem::size_of::<u32>())?; 769971462beSGnoCiYeH 770971462beSGnoCiYeH let mut timespec = None; 771971462beSGnoCiYeH if utime != 0 && operation.contains(FutexFlag::FLAGS_HAS_TIMEOUT) { 772971462beSGnoCiYeH let reader = UserBufferReader::new( 773971462beSGnoCiYeH utime as *const TimeSpec, 774971462beSGnoCiYeH core::mem::size_of::<TimeSpec>(), 775971462beSGnoCiYeH true, 776971462beSGnoCiYeH )?; 777971462beSGnoCiYeH 778840045afSLoGin timespec = Some(*reader.read_one_from_user::<TimeSpec>(0)?); 779971462beSGnoCiYeH } 780971462beSGnoCiYeH 781971462beSGnoCiYeH Self::do_futex(uaddr, operation, val, timespec, uaddr2, utime as u32, val3) 782971462beSGnoCiYeH } 783971462beSGnoCiYeH 784bf4a4899SLoGin SYS_READV => Self::readv(args[0] as i32, args[1], args[2]), 785971462beSGnoCiYeH SYS_WRITEV => Self::writev(args[0] as i32, args[1], args[2]), 786971462beSGnoCiYeH 7874fda81ceSLoGin SYS_SET_TID_ADDRESS => Self::set_tid_address(args[0]), 788971462beSGnoCiYeH 7894fda81ceSLoGin #[cfg(target_arch = "x86_64")] 7904fda81ceSLoGin SYS_LSTAT => { 791971462beSGnoCiYeH let path: &CStr = unsafe { CStr::from_ptr(args[0] as *const c_char) }; 792971462beSGnoCiYeH let path: Result<&str, core::str::Utf8Error> = path.to_str(); 793971462beSGnoCiYeH let res = if path.is_err() { 794971462beSGnoCiYeH Err(SystemError::EINVAL) 795971462beSGnoCiYeH } else { 796971462beSGnoCiYeH let path: &str = path.unwrap(); 797971462beSGnoCiYeH let kstat = args[1] as *mut PosixKstat; 798971462beSGnoCiYeH let vaddr = VirtAddr::new(kstat as usize); 799971462beSGnoCiYeH match verify_area(vaddr, core::mem::size_of::<PosixKstat>()) { 8004fda81ceSLoGin Ok(_) => Self::lstat(path, kstat), 8014fda81ceSLoGin Err(e) => Err(e), 8024fda81ceSLoGin } 8034fda81ceSLoGin }; 8044fda81ceSLoGin 8054fda81ceSLoGin res 8064fda81ceSLoGin } 8074fda81ceSLoGin 8084fda81ceSLoGin #[cfg(target_arch = "x86_64")] 8094fda81ceSLoGin SYS_STAT => { 8104fda81ceSLoGin let path: &CStr = unsafe { CStr::from_ptr(args[0] as *const c_char) }; 8114fda81ceSLoGin let path: Result<&str, core::str::Utf8Error> = path.to_str(); 8124fda81ceSLoGin let res = if path.is_err() { 8134fda81ceSLoGin Err(SystemError::EINVAL) 814bf4a4899SLoGin } else { 8154fda81ceSLoGin let path: &str = path.unwrap(); 8164fda81ceSLoGin let kstat = args[1] as *mut PosixKstat; 8174fda81ceSLoGin let vaddr = VirtAddr::new(kstat as usize); 8184fda81ceSLoGin match verify_area(vaddr, core::mem::size_of::<PosixKstat>()) { 8194fda81ceSLoGin Ok(_) => Self::stat(path, kstat), 820971462beSGnoCiYeH Err(e) => Err(e), 821971462beSGnoCiYeH } 822971462beSGnoCiYeH }; 823971462beSGnoCiYeH 824971462beSGnoCiYeH res 825971462beSGnoCiYeH } 826971462beSGnoCiYeH 82740609970SGnoCiYeH SYS_EPOLL_CREATE => Self::epoll_create(args[0] as i32), 82840609970SGnoCiYeH SYS_EPOLL_CREATE1 => Self::epoll_create1(args[0]), 82940609970SGnoCiYeH 83040609970SGnoCiYeH SYS_EPOLL_CTL => Self::epoll_ctl( 83140609970SGnoCiYeH args[0] as i32, 83240609970SGnoCiYeH args[1], 83340609970SGnoCiYeH args[2] as i32, 83440609970SGnoCiYeH VirtAddr::new(args[3]), 83540609970SGnoCiYeH ), 83640609970SGnoCiYeH 83740609970SGnoCiYeH SYS_EPOLL_WAIT => Self::epoll_wait( 83840609970SGnoCiYeH args[0] as i32, 83940609970SGnoCiYeH VirtAddr::new(args[1]), 84040609970SGnoCiYeH args[2] as i32, 84140609970SGnoCiYeH args[3] as i32, 84240609970SGnoCiYeH ), 84340609970SGnoCiYeH 84440609970SGnoCiYeH SYS_EPOLL_PWAIT => { 84540609970SGnoCiYeH let epfd = args[0] as i32; 84640609970SGnoCiYeH let epoll_event = VirtAddr::new(args[1]); 84740609970SGnoCiYeH let max_events = args[2] as i32; 84840609970SGnoCiYeH let timespec = args[3] as i32; 84940609970SGnoCiYeH let sigmask_addr = args[4] as *mut SigSet; 85040609970SGnoCiYeH 85140609970SGnoCiYeH if sigmask_addr.is_null() { 85240609970SGnoCiYeH return Self::epoll_wait(epfd, epoll_event, max_events, timespec); 85340609970SGnoCiYeH } 85440609970SGnoCiYeH let sigmask_reader = 85540609970SGnoCiYeH UserBufferReader::new(sigmask_addr, core::mem::size_of::<SigSet>(), true)?; 856840045afSLoGin let mut sigmask = *sigmask_reader.read_one_from_user::<SigSet>(0)?; 85740609970SGnoCiYeH 85840609970SGnoCiYeH Self::epoll_pwait( 85940609970SGnoCiYeH args[0] as i32, 86040609970SGnoCiYeH VirtAddr::new(args[1]), 86140609970SGnoCiYeH args[2] as i32, 86240609970SGnoCiYeH args[3] as i32, 86340609970SGnoCiYeH &mut sigmask, 86440609970SGnoCiYeH ) 86540609970SGnoCiYeH } 86640609970SGnoCiYeH 867971462beSGnoCiYeH // 目前为了适配musl-libc,以下系统调用先这样写着 8684fda81ceSLoGin SYS_GETRANDOM => { 869971462beSGnoCiYeH let flags = GRandFlags::from_bits(args[2] as u8).ok_or(SystemError::EINVAL)?; 870971462beSGnoCiYeH Self::get_random(args[0] as *mut u8, args[1], flags) 871971462beSGnoCiYeH } 872971462beSGnoCiYeH 8734fda81ceSLoGin SYS_SOCKETPAIR => { 8744ad52e57S裕依2439 let mut user_buffer_writer = UserBufferWriter::new( 8754ad52e57S裕依2439 args[3] as *mut c_int, 8764ad52e57S裕依2439 core::mem::size_of::<[c_int; 2]>(), 8774ad52e57S裕依2439 frame.from_user(), 8784ad52e57S裕依2439 )?; 8794ad52e57S裕依2439 let fds = user_buffer_writer.buffer::<i32>(0)?; 8804ad52e57S裕依2439 Self::socketpair(args[0], args[1], args[2], fds) 881971462beSGnoCiYeH } 882971462beSGnoCiYeH 8834fda81ceSLoGin #[cfg(target_arch = "x86_64")] 884971462beSGnoCiYeH SYS_POLL => { 885971462beSGnoCiYeH kwarn!("SYS_POLL has not yet been implemented"); 886971462beSGnoCiYeH Ok(0) 887971462beSGnoCiYeH } 888971462beSGnoCiYeH 88952da9a59SGnoCiYeH SYS_SETPGID => { 89052da9a59SGnoCiYeH kwarn!("SYS_SETPGID has not yet been implemented"); 89152da9a59SGnoCiYeH Ok(0) 89252da9a59SGnoCiYeH } 89352da9a59SGnoCiYeH 894971462beSGnoCiYeH SYS_RT_SIGPROCMASK => { 895971462beSGnoCiYeH kwarn!("SYS_RT_SIGPROCMASK has not yet been implemented"); 896971462beSGnoCiYeH Ok(0) 897971462beSGnoCiYeH } 898971462beSGnoCiYeH 899971462beSGnoCiYeH SYS_TKILL => { 900971462beSGnoCiYeH kwarn!("SYS_TKILL has not yet been implemented"); 901971462beSGnoCiYeH Ok(0) 902971462beSGnoCiYeH } 903971462beSGnoCiYeH 904971462beSGnoCiYeH SYS_SIGALTSTACK => { 905971462beSGnoCiYeH kwarn!("SYS_SIGALTSTACK has not yet been implemented"); 906971462beSGnoCiYeH Ok(0) 907971462beSGnoCiYeH } 908971462beSGnoCiYeH 90916033951SGnoCiYeH SYS_EXIT_GROUP => { 91016033951SGnoCiYeH kwarn!("SYS_EXIT_GROUP has not yet been implemented"); 91116033951SGnoCiYeH Ok(0) 91216033951SGnoCiYeH } 91316033951SGnoCiYeH 91404babc3fSMemoryShore SYS_MADVISE => { 9156c7f966cSMemoryShore // 这个太吵了,总是打印,先注释掉 9166c7f966cSMemoryShore // kwarn!("SYS_MADVISE has not yet been implemented"); 91704babc3fSMemoryShore Ok(0) 91804babc3fSMemoryShore } 919393f6915SLoGin SYS_GETTID => Self::gettid().map(|tid| tid.into()), 920840045afSLoGin SYS_GETUID => Self::getuid(), 9218d72b68dSJomo 92202e249f3SLoGin SYS_SYSLOG => { 923840045afSLoGin let syslog_action_type = args[0]; 9248d72b68dSJomo let buf_vaddr = args[1]; 9258d72b68dSJomo let len = args[2]; 9268d72b68dSJomo let from_user = frame.from_user(); 9278d72b68dSJomo let mut user_buffer_writer = 9288d72b68dSJomo UserBufferWriter::new(buf_vaddr as *mut u8, len, from_user)?; 9298d72b68dSJomo 9308d72b68dSJomo let user_buf = user_buffer_writer.buffer(0)?; 931840045afSLoGin Self::do_syslog(syslog_action_type, user_buf, len) 93202e249f3SLoGin } 9338d72b68dSJomo 934840045afSLoGin SYS_GETGID => Self::getgid(), 93502e249f3SLoGin SYS_SETUID => { 93602e249f3SLoGin kwarn!("SYS_SETUID has not yet been implemented"); 93702e249f3SLoGin Ok(0) 93802e249f3SLoGin } 93902e249f3SLoGin SYS_SETGID => { 94002e249f3SLoGin kwarn!("SYS_SETGID has not yet been implemented"); 94102e249f3SLoGin Ok(0) 94202e249f3SLoGin } 943840045afSLoGin SYS_GETEUID => Self::geteuid(), 944840045afSLoGin SYS_GETEGID => Self::getegid(), 945be8cdf4bSLoGin SYS_GETRUSAGE => { 946be8cdf4bSLoGin let who = args[0] as c_int; 947be8cdf4bSLoGin let rusage = args[1] as *mut RUsage; 948be8cdf4bSLoGin Self::get_rusage(who, rusage) 949be8cdf4bSLoGin } 95004babc3fSMemoryShore 9514fda81ceSLoGin #[cfg(target_arch = "x86_64")] 952709498caSLoGin SYS_READLINK => { 953709498caSLoGin let path = args[0] as *const u8; 954709498caSLoGin let buf = args[1] as *mut u8; 955840045afSLoGin let bufsiz = args[2]; 956709498caSLoGin Self::readlink(path, buf, bufsiz) 957709498caSLoGin } 958709498caSLoGin 9594fda81ceSLoGin SYS_READLINKAT => { 960709498caSLoGin let dirfd = args[0] as i32; 961709498caSLoGin let pathname = args[1] as *const u8; 962709498caSLoGin let buf = args[2] as *mut u8; 963840045afSLoGin let bufsiz = args[3]; 964709498caSLoGin Self::readlink_at(dirfd, pathname, buf, bufsiz) 965709498caSLoGin } 966709498caSLoGin 9670d9b7d92SLoGin SYS_PRLIMIT64 => { 9680d9b7d92SLoGin let pid = args[0]; 9690d9b7d92SLoGin let pid = Pid::new(pid); 9700d9b7d92SLoGin let resource = args[1]; 9710d9b7d92SLoGin let new_limit = args[2] as *const RLimit64; 9720d9b7d92SLoGin let old_limit = args[3] as *mut RLimit64; 9730d9b7d92SLoGin 9740d9b7d92SLoGin Self::prlimit64(pid, resource, new_limit, old_limit) 9750d9b7d92SLoGin } 9760d9b7d92SLoGin 9774fda81ceSLoGin #[cfg(target_arch = "x86_64")] 9789b0abe6dSLoGin SYS_ACCESS => { 9799b0abe6dSLoGin let pathname = args[0] as *const u8; 9809b0abe6dSLoGin let mode = args[1] as u32; 9819b0abe6dSLoGin Self::access(pathname, mode) 9829b0abe6dSLoGin } 9839b0abe6dSLoGin 9849b0abe6dSLoGin SYS_FACCESSAT => { 9859b0abe6dSLoGin let dirfd = args[0] as i32; 9869b0abe6dSLoGin let pathname = args[1] as *const u8; 9879b0abe6dSLoGin let mode = args[2] as u32; 9889b0abe6dSLoGin Self::faccessat2(dirfd, pathname, mode, 0) 9899b0abe6dSLoGin } 9909b0abe6dSLoGin 9919b0abe6dSLoGin SYS_FACCESSAT2 => { 9929b0abe6dSLoGin let dirfd = args[0] as i32; 9939b0abe6dSLoGin let pathname = args[1] as *const u8; 9949b0abe6dSLoGin let mode = args[2] as u32; 9959b0abe6dSLoGin let flags = args[3] as u32; 9969b0abe6dSLoGin Self::faccessat2(dirfd, pathname, mode, flags) 9979b0abe6dSLoGin } 9989b0abe6dSLoGin 999bf4a4899SLoGin SYS_CLOCK_GETTIME => { 1000bf4a4899SLoGin let clockid = args[0] as i32; 1001bf4a4899SLoGin let timespec = args[1] as *mut TimeSpec; 1002bf4a4899SLoGin Self::clock_gettime(clockid, timespec) 1003bf4a4899SLoGin } 1004bf4a4899SLoGin 1005bf4a4899SLoGin SYS_SYSINFO => { 1006bf4a4899SLoGin let info = args[0] as *mut SysInfo; 1007bf4a4899SLoGin Self::sysinfo(info) 1008bf4a4899SLoGin } 1009bf4a4899SLoGin 1010bf4a4899SLoGin SYS_UMASK => { 1011bf4a4899SLoGin let mask = args[0] as u32; 1012bf4a4899SLoGin Self::umask(mask) 1013bf4a4899SLoGin } 1014bf4a4899SLoGin 101527b967a3S裕依 SYS_FCHOWN => { 101627b967a3S裕依 kwarn!("SYS_FCHOWN has not yet been implemented"); 101727b967a3S裕依 Ok(0) 101827b967a3S裕依 } 101927b967a3S裕依 102027b967a3S裕依 SYS_FSYNC => { 102127b967a3S裕依 kwarn!("SYS_FSYNC has not yet been implemented"); 102227b967a3S裕依 Ok(0) 102327b967a3S裕依 } 102427b967a3S裕依 10254fda81ceSLoGin #[cfg(target_arch = "x86_64")] 1026bf4a4899SLoGin SYS_CHMOD => { 1027bf4a4899SLoGin let pathname = args[0] as *const u8; 1028bf4a4899SLoGin let mode = args[1] as u32; 1029bf4a4899SLoGin Self::chmod(pathname, mode) 1030bf4a4899SLoGin } 1031bf4a4899SLoGin SYS_FCHMOD => { 1032bf4a4899SLoGin let fd = args[0] as i32; 1033bf4a4899SLoGin let mode = args[1] as u32; 1034bf4a4899SLoGin Self::fchmod(fd, mode) 1035bf4a4899SLoGin } 1036bf4a4899SLoGin SYS_FCHMODAT => { 1037bf4a4899SLoGin let dirfd = args[0] as i32; 1038bf4a4899SLoGin let pathname = args[1] as *const u8; 1039bf4a4899SLoGin let mode = args[2] as u32; 1040bf4a4899SLoGin Self::fchmodat(dirfd, pathname, mode) 1041bf4a4899SLoGin } 1042bf4a4899SLoGin 104302343d0bSLoGin SYS_SCHED_GETAFFINITY => { 104402343d0bSLoGin // todo: 这个系统调用还没有实现 104502343d0bSLoGin 104602343d0bSLoGin Err(SystemError::ENOSYS) 104702343d0bSLoGin } 104802343d0bSLoGin 104952da9a59SGnoCiYeH #[cfg(target_arch = "x86_64")] 105052da9a59SGnoCiYeH SYS_GETRLIMIT => { 105152da9a59SGnoCiYeH let resource = args[0]; 105252da9a59SGnoCiYeH let rlimit = args[1] as *mut RLimit64; 105352da9a59SGnoCiYeH 105452da9a59SGnoCiYeH Self::prlimit64( 105552da9a59SGnoCiYeH ProcessManager::current_pcb().pid(), 105652da9a59SGnoCiYeH resource, 1057840045afSLoGin core::ptr::null::<RLimit64>(), 105852da9a59SGnoCiYeH rlimit, 105952da9a59SGnoCiYeH ) 106052da9a59SGnoCiYeH } 106152da9a59SGnoCiYeH 1062*9e481b3bSTTaq SYS_FADVISE64 => { 1063*9e481b3bSTTaq // todo: 这个系统调用还没有实现 1064*9e481b3bSTTaq 1065*9e481b3bSTTaq Err(SystemError::ENOSYS) 1066*9e481b3bSTTaq } 1067*9e481b3bSTTaq SYS_NEWFSTATAT => { 1068*9e481b3bSTTaq // todo: 这个系统调用还没有实现 1069*9e481b3bSTTaq 1070*9e481b3bSTTaq Err(SystemError::ENOSYS) 1071*9e481b3bSTTaq } 1072*9e481b3bSTTaq 107302343d0bSLoGin SYS_SCHED_YIELD => Self::sched_yield(), 107402343d0bSLoGin 1075ab5c8ca4Slogin _ => panic!("Unsupported syscall ID: {}", syscall_num), 1076ab5c8ca4Slogin }; 10774fda81ceSLoGin 1078ab5c8ca4Slogin return r; 1079ab5c8ca4Slogin } 1080ab5c8ca4Slogin 1081ab5c8ca4Slogin pub fn put_string( 1082ab5c8ca4Slogin s: *const u8, 1083ab5c8ca4Slogin front_color: u32, 1084ab5c8ca4Slogin back_color: u32, 1085ab5c8ca4Slogin ) -> Result<usize, SystemError> { 1086c3dc6f2fSLoGin // todo: 删除这个系统调用 1087c3dc6f2fSLoGin let s = check_and_clone_cstr(s, Some(4096))?; 1088c3dc6f2fSLoGin let fr = (front_color & 0x00ff0000) >> 16; 1089c3dc6f2fSLoGin let fg = (front_color & 0x0000ff00) >> 8; 1090c3dc6f2fSLoGin let fb = front_color & 0x000000ff; 1091c3dc6f2fSLoGin let br = (back_color & 0x00ff0000) >> 16; 1092c3dc6f2fSLoGin let bg = (back_color & 0x0000ff00) >> 8; 1093c3dc6f2fSLoGin let bb = back_color & 0x000000ff; 1094c3dc6f2fSLoGin print!("\x1B[38;2;{fr};{fg};{fb};48;2;{br};{bg};{bb}m{s}\x1B[0m"); 1095c3dc6f2fSLoGin return Ok(s.len()); 1096ab5c8ca4Slogin } 1097ab5c8ca4Slogin 1098ab5c8ca4Slogin pub fn reboot() -> Result<usize, SystemError> { 10994fda81ceSLoGin unsafe { cpu_reset() }; 1100ab5c8ca4Slogin } 1101ab5c8ca4Slogin } 1102