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}, 100d9b7d92SLoGin process::{ 110d9b7d92SLoGin fork::KernelCloneArgs, 120d9b7d92SLoGin resource::{RLimit64, RUsage}, 130d9b7d92SLoGin }, 14971462beSGnoCiYeH }; 1540314b30SXiaoye Zheng 1691e9d4abSLoGin use num_traits::FromPrimitive; 1791e9d4abSLoGin use system_error::SystemError; 18676b8ef6SMork 19ab5c8ca4Slogin use crate::{ 201496ba7bSLoGin arch::{cpu::cpu_reset, interrupt::TrapFrame, MMArch}, 214fda81ceSLoGin driver::base::block::SeekFrom, 22ab5c8ca4Slogin filesystem::vfs::{ 236d81180bSLoGin fcntl::FcntlCommand, 24ab5c8ca4Slogin file::FileMode, 252dbef785SGnoCiYeH syscall::{ModeType, PosixKstat, SEEK_CUR, SEEK_END, SEEK_MAX, SEEK_SET}, 26ab5c8ca4Slogin MAX_PATHLEN, 27ab5c8ca4Slogin }, 281496ba7bSLoGin include::bindings::bindings::{PAGE_2M_SIZE, PAGE_4K_SIZE}, 29aa0367d6SLoGin kinfo, 3040fe15e0SLoGin libs::align::page_align_up, 31be63f3b2Shoumkh mm::{verify_area, MemoryManagementArch, VirtAddr}, 32ab5c8ca4Slogin net::syscall::SockAddr, 33971462beSGnoCiYeH process::{fork::CloneFlags, Pid}, 3436fd0130Shoumkh time::{ 3540fe15e0SLoGin syscall::{PosixTimeZone, PosixTimeval}, 3636fd0130Shoumkh TimeSpec, 3736fd0130Shoumkh }, 38ab5c8ca4Slogin }; 39ab5c8ca4Slogin 40bf4a4899SLoGin use self::{ 41bf4a4899SLoGin misc::SysInfo, 42bf4a4899SLoGin user_access::{UserBufferReader, UserBufferWriter}, 43bf4a4899SLoGin }; 444537ffb7SChiichen 45bf4a4899SLoGin pub mod misc; 4640fe15e0SLoGin pub mod user_access; 4740fe15e0SLoGin 48ad1d649eSGnoCiYeH // 与linux不一致的调用,在linux基础上累加 49ad1d649eSGnoCiYeH pub const SYS_PUT_STRING: usize = 100000; 50ad1d649eSGnoCiYeH pub const SYS_SBRK: usize = 100001; 51ab5c8ca4Slogin /// todo: 该系统调用与Linux不一致,将来需要删除该系统调用!!! 删的时候记得改C版本的libc 52ad1d649eSGnoCiYeH pub const SYS_CLOCK: usize = 100002; 53ad1d649eSGnoCiYeH pub const SYS_SCHED: usize = 100003; 5440314b30SXiaoye Zheng 55ab5c8ca4Slogin #[derive(Debug)] 56ab5c8ca4Slogin pub struct Syscall; 57ab5c8ca4Slogin 58ab5c8ca4Slogin extern "C" { 59ab5c8ca4Slogin fn do_put_string(s: *const u8, front_color: u32, back_color: u32) -> usize; 60ab5c8ca4Slogin } 61ab5c8ca4Slogin 62ab5c8ca4Slogin impl Syscall { 63ab5c8ca4Slogin /// 初始化系统调用 64*5b59005fSLoGin #[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 } 71*5b59005fSLoGin kinfo!("Initializing syscall..."); 72*5b59005fSLoGin let r = crate::arch::syscall::arch_syscall_init(); 73*5b59005fSLoGin kinfo!("Syscall init successfully!"); 74*5b59005fSLoGin 75*5b59005fSLoGin 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 1100fb515b0SLoGin SYS_OPENAT => { 1110fb515b0SLoGin let dirfd = args[0] as i32; 1120fb515b0SLoGin let path: &CStr = unsafe { CStr::from_ptr(args[1] as *const c_char) }; 1130fb515b0SLoGin let flags = args[2]; 1140fb515b0SLoGin let mode = args[3]; 1150fb515b0SLoGin 1160fb515b0SLoGin let path: Result<&str, core::str::Utf8Error> = path.to_str(); 1170fb515b0SLoGin let res = if path.is_err() { 1180fb515b0SLoGin Err(SystemError::EINVAL) 1190fb515b0SLoGin } else { 1200fb515b0SLoGin let path: &str = path.unwrap(); 1210fb515b0SLoGin 1220fb515b0SLoGin let open_flags: FileMode = 1230fb515b0SLoGin FileMode::from_bits(flags as u32).ok_or(SystemError::EINVAL)?; 1240fb515b0SLoGin let mode = ModeType::from_bits(mode as u32).ok_or(SystemError::EINVAL)?; 1250fb515b0SLoGin Self::openat(dirfd, path, open_flags, mode, true) 126ab5c8ca4Slogin }; 127ab5c8ca4Slogin res 128ab5c8ca4Slogin } 129ab5c8ca4Slogin SYS_CLOSE => { 130ab5c8ca4Slogin let fd = args[0]; 13134e6d6c8Syuyi2439 13234e6d6c8Syuyi2439 let res = Self::close(fd); 13334e6d6c8Syuyi2439 13434e6d6c8Syuyi2439 res 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)?; 1459e9ffedfSPlucky923 let res = Self::read(fd, user_buf); 146ab5c8ca4Slogin res 147ab5c8ca4Slogin } 148ab5c8ca4Slogin SYS_WRITE => { 149ab5c8ca4Slogin let fd = args[0] as i32; 150ab5c8ca4Slogin let buf_vaddr = args[1]; 151ab5c8ca4Slogin let len = args[2]; 1529e9ffedfSPlucky923 let from_user = frame.from_user(); 1539e9ffedfSPlucky923 let user_buffer_reader = 1549e9ffedfSPlucky923 UserBufferReader::new(buf_vaddr as *const u8, len, from_user)?; 1556d81180bSLoGin 1569e9ffedfSPlucky923 let user_buf = user_buffer_reader.read_from_user(0)?; 1579e9ffedfSPlucky923 let res = Self::write(fd, user_buf); 158ab5c8ca4Slogin res 159ab5c8ca4Slogin } 160ab5c8ca4Slogin 161ab5c8ca4Slogin SYS_LSEEK => { 162ab5c8ca4Slogin let fd = args[0] as i32; 163ab5c8ca4Slogin let offset = args[1] as i64; 164ab5c8ca4Slogin let whence = args[2] as u32; 165ab5c8ca4Slogin 166ab5c8ca4Slogin let w = match whence { 167ab5c8ca4Slogin SEEK_SET => Ok(SeekFrom::SeekSet(offset)), 168ab5c8ca4Slogin SEEK_CUR => Ok(SeekFrom::SeekCurrent(offset)), 169ab5c8ca4Slogin SEEK_END => Ok(SeekFrom::SeekEnd(offset)), 170ab5c8ca4Slogin SEEK_MAX => Ok(SeekFrom::SeekEnd(0)), 171ab5c8ca4Slogin _ => Err(SystemError::EINVAL), 1729e9ffedfSPlucky923 }?; 173ab5c8ca4Slogin 174ab5c8ca4Slogin Self::lseek(fd, w) 175ab5c8ca4Slogin } 17640314b30SXiaoye Zheng SYS_IOCTL => { 17740314b30SXiaoye Zheng let fd = args[0]; 17840314b30SXiaoye Zheng let cmd = args[1]; 17940314b30SXiaoye Zheng let data = args[2]; 18040314b30SXiaoye Zheng Self::ioctl(fd, cmd as u32, data) 18140314b30SXiaoye Zheng } 182ab5c8ca4Slogin 1834fda81ceSLoGin #[cfg(target_arch = "x86_64")] 1841496ba7bSLoGin SYS_FORK => Self::fork(frame), 1854fda81ceSLoGin #[cfg(target_arch = "x86_64")] 1861496ba7bSLoGin SYS_VFORK => Self::vfork(frame), 1871496ba7bSLoGin 188ab5c8ca4Slogin SYS_BRK => { 18940fe15e0SLoGin let new_brk = VirtAddr::new(args[0]); 19040fe15e0SLoGin Self::brk(new_brk).map(|vaddr| vaddr.data()) 191ab5c8ca4Slogin } 192ab5c8ca4Slogin 193ab5c8ca4Slogin SYS_SBRK => { 194ab5c8ca4Slogin let increment = args[0] as isize; 1951496ba7bSLoGin Self::sbrk(increment).map(|vaddr: VirtAddr| vaddr.data()) 196ab5c8ca4Slogin } 197ab5c8ca4Slogin 198ab5c8ca4Slogin SYS_REBOOT => Self::reboot(), 199ab5c8ca4Slogin 200ab5c8ca4Slogin SYS_CHDIR => { 201ab5c8ca4Slogin // Closure for checking arguments 202ab5c8ca4Slogin let chdir_check = |arg0: usize| { 203ab5c8ca4Slogin if arg0 == 0 { 204ab5c8ca4Slogin return Err(SystemError::EFAULT); 205ab5c8ca4Slogin } 206ab5c8ca4Slogin let path_ptr = arg0 as *const c_char; 207be63f3b2Shoumkh let virt_addr = VirtAddr::new(path_ptr as usize); 208ab5c8ca4Slogin // 权限校验 209ab5c8ca4Slogin if path_ptr.is_null() 2101496ba7bSLoGin || (frame.from_user() 2111496ba7bSLoGin && verify_area(virt_addr, PAGE_2M_SIZE as usize).is_err()) 212ab5c8ca4Slogin { 213ab5c8ca4Slogin return Err(SystemError::EINVAL); 214ab5c8ca4Slogin } 215ab5c8ca4Slogin let dest_path: &CStr = unsafe { CStr::from_ptr(path_ptr) }; 216ab5c8ca4Slogin let dest_path: &str = dest_path.to_str().map_err(|_| SystemError::EINVAL)?; 217ab5c8ca4Slogin if dest_path.len() == 0 { 218ab5c8ca4Slogin return Err(SystemError::EINVAL); 2191496ba7bSLoGin } else if dest_path.len() > MAX_PATHLEN as usize { 220ab5c8ca4Slogin return Err(SystemError::ENAMETOOLONG); 221ab5c8ca4Slogin } 222ab5c8ca4Slogin 223ab5c8ca4Slogin return Ok(dest_path); 224ab5c8ca4Slogin }; 225ab5c8ca4Slogin 2269e9ffedfSPlucky923 let r = chdir_check(args[0])?; 2279e9ffedfSPlucky923 Self::chdir(r) 228ab5c8ca4Slogin } 229ab5c8ca4Slogin 2304fda81ceSLoGin #[allow(unreachable_patterns)] 2314fda81ceSLoGin SYS_GETDENTS64 | SYS_GETDENTS => { 232ab5c8ca4Slogin let fd = args[0] as i32; 23334e6d6c8Syuyi2439 234ab5c8ca4Slogin let buf_vaddr = args[1]; 235ab5c8ca4Slogin let len = args[2]; 236f5df0e79SLoGin let virt_addr: VirtAddr = VirtAddr::new(buf_vaddr); 237ab5c8ca4Slogin // 判断缓冲区是否来自用户态,进行权限校验 2381496ba7bSLoGin let res = if frame.from_user() && verify_area(virt_addr, len as usize).is_err() { 239ab5c8ca4Slogin // 来自用户态,而buffer在内核态,这样的操作不被允许 240ab5c8ca4Slogin Err(SystemError::EPERM) 241ab5c8ca4Slogin } else if buf_vaddr == 0 { 242ab5c8ca4Slogin Err(SystemError::EFAULT) 243ab5c8ca4Slogin } else { 244ab5c8ca4Slogin let buf: &mut [u8] = unsafe { 245ab5c8ca4Slogin core::slice::from_raw_parts_mut::<'static, u8>(buf_vaddr as *mut u8, len) 246ab5c8ca4Slogin }; 247ab5c8ca4Slogin Self::getdents(fd, buf) 248ab5c8ca4Slogin }; 249ab5c8ca4Slogin 250ab5c8ca4Slogin res 251ab5c8ca4Slogin } 252ab5c8ca4Slogin 253ab5c8ca4Slogin SYS_EXECVE => { 254ab5c8ca4Slogin let path_ptr = args[0]; 255ab5c8ca4Slogin let argv_ptr = args[1]; 256ab5c8ca4Slogin let env_ptr = args[2]; 257be63f3b2Shoumkh let virt_path_ptr = VirtAddr::new(path_ptr); 258be63f3b2Shoumkh let virt_argv_ptr = VirtAddr::new(argv_ptr); 259be63f3b2Shoumkh let virt_env_ptr = VirtAddr::new(env_ptr); 260ab5c8ca4Slogin // 权限校验 2611496ba7bSLoGin if frame.from_user() 2621496ba7bSLoGin && (verify_area(virt_path_ptr, MAX_PATHLEN as usize).is_err() 263be63f3b2Shoumkh || verify_area(virt_argv_ptr, PAGE_4K_SIZE as usize).is_err()) 264be63f3b2Shoumkh || verify_area(virt_env_ptr, PAGE_4K_SIZE as usize).is_err() 265ab5c8ca4Slogin { 266ab5c8ca4Slogin Err(SystemError::EFAULT) 267ab5c8ca4Slogin } else { 268ab5c8ca4Slogin Self::execve( 2691496ba7bSLoGin path_ptr as *const u8, 2701496ba7bSLoGin argv_ptr as *const *const u8, 2711496ba7bSLoGin env_ptr as *const *const u8, 2721496ba7bSLoGin frame, 273ab5c8ca4Slogin ) 2741496ba7bSLoGin .map(|_| 0) 275ab5c8ca4Slogin } 276ab5c8ca4Slogin } 277ab5c8ca4Slogin SYS_WAIT4 => { 278bf4a4899SLoGin let pid = args[0] as i32; 2791496ba7bSLoGin let wstatus = args[1] as *mut i32; 280ab5c8ca4Slogin let options = args[2] as c_int; 281ab5c8ca4Slogin let rusage = args[3] as *mut c_void; 282ab5c8ca4Slogin // 权限校验 283ab5c8ca4Slogin // todo: 引入rusage之后,更正以下权限校验代码中,rusage的大小 284bf4a4899SLoGin Self::wait4(pid.into(), wstatus, options, rusage) 285ab5c8ca4Slogin } 286ab5c8ca4Slogin 287ab5c8ca4Slogin SYS_EXIT => { 288ab5c8ca4Slogin let exit_code = args[0]; 289ab5c8ca4Slogin Self::exit(exit_code) 290ab5c8ca4Slogin } 2914fda81ceSLoGin #[cfg(target_arch = "x86_64")] 292ab5c8ca4Slogin SYS_MKDIR => { 293ab5c8ca4Slogin let path_ptr = args[0] as *const c_char; 294ab5c8ca4Slogin let mode = args[1]; 295be63f3b2Shoumkh let virt_path_ptr = VirtAddr::new(path_ptr as usize); 296ab5c8ca4Slogin let security_check = || { 297ab5c8ca4Slogin if path_ptr.is_null() 2981496ba7bSLoGin || (frame.from_user() 2991496ba7bSLoGin && verify_area(virt_path_ptr, PAGE_2M_SIZE as usize).is_err()) 300ab5c8ca4Slogin { 301ab5c8ca4Slogin return Err(SystemError::EINVAL); 302ab5c8ca4Slogin } 303ab5c8ca4Slogin let path: &CStr = unsafe { CStr::from_ptr(path_ptr) }; 304ab5c8ca4Slogin let path: &str = path.to_str().map_err(|_| SystemError::EINVAL)?.trim(); 305ab5c8ca4Slogin 306ab5c8ca4Slogin if path == "" { 307ab5c8ca4Slogin return Err(SystemError::EINVAL); 308ab5c8ca4Slogin } 309ab5c8ca4Slogin return Ok(path); 310ab5c8ca4Slogin }; 311ab5c8ca4Slogin 312ab5c8ca4Slogin let path = security_check(); 313ab5c8ca4Slogin if path.is_err() { 314ab5c8ca4Slogin Err(path.unwrap_err()) 315ab5c8ca4Slogin } else { 316ab5c8ca4Slogin Self::mkdir(path.unwrap(), mode) 317ab5c8ca4Slogin } 318ab5c8ca4Slogin } 319ab5c8ca4Slogin 320ab5c8ca4Slogin SYS_NANOSLEEP => { 321ab5c8ca4Slogin let req = args[0] as *const TimeSpec; 322ab5c8ca4Slogin let rem = args[1] as *mut TimeSpec; 323be63f3b2Shoumkh let virt_req = VirtAddr::new(req as usize); 324be63f3b2Shoumkh let virt_rem = VirtAddr::new(rem as usize); 3251496ba7bSLoGin if frame.from_user() 326be63f3b2Shoumkh && (verify_area(virt_req, core::mem::size_of::<TimeSpec>() as usize).is_err() 327be63f3b2Shoumkh || verify_area(virt_rem, core::mem::size_of::<TimeSpec>() as usize) 328be63f3b2Shoumkh .is_err()) 329ab5c8ca4Slogin { 330ab5c8ca4Slogin Err(SystemError::EFAULT) 331ab5c8ca4Slogin } else { 332ab5c8ca4Slogin Self::nanosleep(req, rem) 333ab5c8ca4Slogin } 334ab5c8ca4Slogin } 335ab5c8ca4Slogin 336ab5c8ca4Slogin SYS_CLOCK => Self::clock(), 3374fda81ceSLoGin #[cfg(target_arch = "x86_64")] 338ab5c8ca4Slogin SYS_PIPE => { 33922c9db31Shanjiezhou let pipefd: *mut i32 = args[0] as *mut c_int; 3400fb515b0SLoGin if pipefd.is_null() { 3410fb515b0SLoGin Err(SystemError::EFAULT) 3420fb515b0SLoGin } else { 3430fb515b0SLoGin Self::pipe2(pipefd, FileMode::empty()) 3440fb515b0SLoGin } 3450fb515b0SLoGin } 3460fb515b0SLoGin SYS_PIPE2 => { 3470fb515b0SLoGin let pipefd: *mut i32 = args[0] as *mut c_int; 34822c9db31Shanjiezhou let arg1 = args[1]; 34922c9db31Shanjiezhou if pipefd.is_null() { 35022c9db31Shanjiezhou Err(SystemError::EFAULT) 35122c9db31Shanjiezhou } else { 35222c9db31Shanjiezhou let flags = FileMode::from_bits_truncate(arg1 as u32); 35322c9db31Shanjiezhou Self::pipe2(pipefd, flags) 354ab5c8ca4Slogin } 355ab5c8ca4Slogin } 356ab5c8ca4Slogin 3574fda81ceSLoGin SYS_UNLINKAT => { 358ab5c8ca4Slogin let dirfd = args[0] as i32; 359ab5c8ca4Slogin let pathname = args[1] as *const c_char; 360ab5c8ca4Slogin let flags = args[2] as u32; 361be63f3b2Shoumkh let virt_pathname = VirtAddr::new(pathname as usize); 3621496ba7bSLoGin if frame.from_user() && verify_area(virt_pathname, PAGE_4K_SIZE as usize).is_err() { 363ab5c8ca4Slogin Err(SystemError::EFAULT) 364ab5c8ca4Slogin } else if pathname.is_null() { 365ab5c8ca4Slogin Err(SystemError::EFAULT) 366ab5c8ca4Slogin } else { 367ab5c8ca4Slogin let get_path = || { 368ab5c8ca4Slogin let pathname: &CStr = unsafe { CStr::from_ptr(pathname) }; 369ab5c8ca4Slogin 370ab5c8ca4Slogin let pathname: &str = pathname.to_str().map_err(|_| SystemError::EINVAL)?; 371ab5c8ca4Slogin if pathname.len() >= MAX_PATHLEN { 372ab5c8ca4Slogin return Err(SystemError::ENAMETOOLONG); 373ab5c8ca4Slogin } 374ab5c8ca4Slogin return Ok(pathname.trim()); 375ab5c8ca4Slogin }; 376ab5c8ca4Slogin let pathname = get_path(); 377ab5c8ca4Slogin if pathname.is_err() { 378ab5c8ca4Slogin Err(pathname.unwrap_err()) 379ab5c8ca4Slogin } else { 3806d81180bSLoGin // kdebug!("sys unlinkat: dirfd: {}, pathname: {}", dirfd, pathname.as_ref().unwrap()); 381ab5c8ca4Slogin Self::unlinkat(dirfd, pathname.unwrap(), flags) 382ab5c8ca4Slogin } 383ab5c8ca4Slogin } 384ab5c8ca4Slogin } 385bf4a4899SLoGin 3864fda81ceSLoGin #[cfg(target_arch = "x86_64")] 387bf4a4899SLoGin SYS_UNLINK => { 388bf4a4899SLoGin let pathname = args[0] as *const u8; 389bf4a4899SLoGin Self::unlink(pathname) 390bf4a4899SLoGin } 391ab5c8ca4Slogin SYS_KILL => { 3921496ba7bSLoGin let pid = Pid::new(args[0]); 393ab5c8ca4Slogin let sig = args[1] as c_int; 3943c82aa56SChiichen // kdebug!("KILL SYSCALL RECEIVED"); 395ab5c8ca4Slogin Self::kill(pid, sig) 396ab5c8ca4Slogin } 397ab5c8ca4Slogin 3984fda81ceSLoGin SYS_RT_SIGACTION => { 399ab5c8ca4Slogin let sig = args[0] as c_int; 400ab5c8ca4Slogin let act = args[1]; 401ab5c8ca4Slogin let old_act = args[2]; 4021496ba7bSLoGin Self::sigaction(sig, act, old_act, frame.from_user()) 403ab5c8ca4Slogin } 404ab5c8ca4Slogin 4051496ba7bSLoGin SYS_GETPID => Self::getpid().map(|pid| pid.into()), 406ab5c8ca4Slogin 4071496ba7bSLoGin SYS_SCHED => Self::sched(frame.from_user()), 408ab5c8ca4Slogin SYS_DUP => { 409ab5c8ca4Slogin let oldfd: i32 = args[0] as c_int; 410ab5c8ca4Slogin Self::dup(oldfd) 411ab5c8ca4Slogin } 4124fda81ceSLoGin 4134fda81ceSLoGin #[cfg(target_arch = "x86_64")] 414ab5c8ca4Slogin SYS_DUP2 => { 415ab5c8ca4Slogin let oldfd: i32 = args[0] as c_int; 416ab5c8ca4Slogin let newfd: i32 = args[1] as c_int; 417ab5c8ca4Slogin Self::dup2(oldfd, newfd) 418ab5c8ca4Slogin } 419ab5c8ca4Slogin 420ab5c8ca4Slogin SYS_SOCKET => Self::socket(args[0], args[1], args[2]), 421ab5c8ca4Slogin SYS_SETSOCKOPT => { 422ab5c8ca4Slogin let optval = args[3] as *const u8; 423ab5c8ca4Slogin let optlen = args[4] as usize; 424be63f3b2Shoumkh let virt_optval = VirtAddr::new(optval as usize); 425ab5c8ca4Slogin // 验证optval的地址是否合法 426be63f3b2Shoumkh if verify_area(virt_optval, optlen as usize).is_err() { 427ab5c8ca4Slogin // 地址空间超出了用户空间的范围,不合法 428ab5c8ca4Slogin Err(SystemError::EFAULT) 429ab5c8ca4Slogin } else { 430ab5c8ca4Slogin let data: &[u8] = unsafe { core::slice::from_raw_parts(optval, optlen) }; 431ab5c8ca4Slogin Self::setsockopt(args[0], args[1], args[2], data) 432ab5c8ca4Slogin } 433ab5c8ca4Slogin } 434ab5c8ca4Slogin SYS_GETSOCKOPT => { 435ab5c8ca4Slogin let optval = args[3] as *mut u8; 436ab5c8ca4Slogin let optlen = args[4] as *mut usize; 437be63f3b2Shoumkh let virt_optval = VirtAddr::new(optval as usize); 438be63f3b2Shoumkh let virt_optlen = VirtAddr::new(optlen as usize); 439ab5c8ca4Slogin let security_check = || { 440ab5c8ca4Slogin // 验证optval的地址是否合法 441be63f3b2Shoumkh if verify_area(virt_optval, PAGE_4K_SIZE as usize).is_err() { 442ab5c8ca4Slogin // 地址空间超出了用户空间的范围,不合法 443ab5c8ca4Slogin return Err(SystemError::EFAULT); 444ab5c8ca4Slogin } 445ab5c8ca4Slogin 446ab5c8ca4Slogin // 验证optlen的地址是否合法 447be63f3b2Shoumkh if verify_area(virt_optlen, core::mem::size_of::<u32>() as usize).is_err() { 448ab5c8ca4Slogin // 地址空间超出了用户空间的范围,不合法 449ab5c8ca4Slogin return Err(SystemError::EFAULT); 450ab5c8ca4Slogin } 451ab5c8ca4Slogin return Ok(()); 452ab5c8ca4Slogin }; 453ab5c8ca4Slogin let r = security_check(); 454ab5c8ca4Slogin if r.is_err() { 455ab5c8ca4Slogin Err(r.unwrap_err()) 456ab5c8ca4Slogin } else { 457ab5c8ca4Slogin Self::getsockopt(args[0], args[1], args[2], optval, optlen as *mut u32) 458ab5c8ca4Slogin } 459ab5c8ca4Slogin } 460ab5c8ca4Slogin 461ab5c8ca4Slogin SYS_CONNECT => { 462ab5c8ca4Slogin let addr = args[1] as *const SockAddr; 463ab5c8ca4Slogin let addrlen = args[2] as usize; 464be63f3b2Shoumkh let virt_addr = VirtAddr::new(addr as usize); 465ab5c8ca4Slogin // 验证addr的地址是否合法 466be63f3b2Shoumkh if verify_area(virt_addr, addrlen as usize).is_err() { 467ab5c8ca4Slogin // 地址空间超出了用户空间的范围,不合法 468ab5c8ca4Slogin Err(SystemError::EFAULT) 469ab5c8ca4Slogin } else { 470ab5c8ca4Slogin Self::connect(args[0], addr, addrlen) 471ab5c8ca4Slogin } 472ab5c8ca4Slogin } 473ab5c8ca4Slogin SYS_BIND => { 474ab5c8ca4Slogin let addr = args[1] as *const SockAddr; 475ab5c8ca4Slogin let addrlen = args[2] as usize; 476be63f3b2Shoumkh let virt_addr = VirtAddr::new(addr as usize); 477ab5c8ca4Slogin // 验证addr的地址是否合法 478be63f3b2Shoumkh if verify_area(virt_addr, addrlen as usize).is_err() { 479ab5c8ca4Slogin // 地址空间超出了用户空间的范围,不合法 480ab5c8ca4Slogin Err(SystemError::EFAULT) 481ab5c8ca4Slogin } else { 482ab5c8ca4Slogin Self::bind(args[0], addr, addrlen) 483ab5c8ca4Slogin } 484ab5c8ca4Slogin } 485ab5c8ca4Slogin 486ab5c8ca4Slogin SYS_SENDTO => { 487ab5c8ca4Slogin let buf = args[1] as *const u8; 488ab5c8ca4Slogin let len = args[2] as usize; 489ab5c8ca4Slogin let flags = args[3] as u32; 490ab5c8ca4Slogin let addr = args[4] as *const SockAddr; 491ab5c8ca4Slogin let addrlen = args[5] as usize; 492be63f3b2Shoumkh let virt_buf = VirtAddr::new(buf as usize); 493be63f3b2Shoumkh let virt_addr = VirtAddr::new(addr as usize); 494ab5c8ca4Slogin // 验证buf的地址是否合法 495be63f3b2Shoumkh if verify_area(virt_buf, len as usize).is_err() { 496ab5c8ca4Slogin // 地址空间超出了用户空间的范围,不合法 497ab5c8ca4Slogin Err(SystemError::EFAULT) 498be63f3b2Shoumkh } else if verify_area(virt_addr, addrlen as usize).is_err() { 499ab5c8ca4Slogin // 地址空间超出了用户空间的范围,不合法 500ab5c8ca4Slogin Err(SystemError::EFAULT) 501ab5c8ca4Slogin } else { 502ab5c8ca4Slogin let data: &[u8] = unsafe { core::slice::from_raw_parts(buf, len) }; 503ab5c8ca4Slogin Self::sendto(args[0], data, flags, addr, addrlen) 504ab5c8ca4Slogin } 505ab5c8ca4Slogin } 506ab5c8ca4Slogin 507ab5c8ca4Slogin SYS_RECVFROM => { 508ab5c8ca4Slogin let buf = args[1] as *mut u8; 509ab5c8ca4Slogin let len = args[2] as usize; 510ab5c8ca4Slogin let flags = args[3] as u32; 511ab5c8ca4Slogin let addr = args[4] as *mut SockAddr; 512ab5c8ca4Slogin let addrlen = args[5] as *mut usize; 513be63f3b2Shoumkh let virt_buf = VirtAddr::new(buf as usize); 514be63f3b2Shoumkh let virt_addrlen = VirtAddr::new(addrlen as usize); 515be63f3b2Shoumkh let virt_addr = VirtAddr::new(addr as usize); 516ab5c8ca4Slogin let security_check = || { 517ab5c8ca4Slogin // 验证buf的地址是否合法 518be63f3b2Shoumkh if verify_area(virt_buf, len as usize).is_err() { 519ab5c8ca4Slogin // 地址空间超出了用户空间的范围,不合法 520ab5c8ca4Slogin return Err(SystemError::EFAULT); 521ab5c8ca4Slogin } 522ab5c8ca4Slogin 523ab5c8ca4Slogin // 验证addrlen的地址是否合法 524be63f3b2Shoumkh if verify_area(virt_addrlen, core::mem::size_of::<u32>() as usize).is_err() { 525ab5c8ca4Slogin // 地址空间超出了用户空间的范围,不合法 526ab5c8ca4Slogin return Err(SystemError::EFAULT); 527ab5c8ca4Slogin } 528ab5c8ca4Slogin 529be63f3b2Shoumkh if verify_area(virt_addr, core::mem::size_of::<SockAddr>() as usize).is_err() { 530ab5c8ca4Slogin // 地址空间超出了用户空间的范围,不合法 531ab5c8ca4Slogin return Err(SystemError::EFAULT); 532ab5c8ca4Slogin } 533ab5c8ca4Slogin return Ok(()); 534ab5c8ca4Slogin }; 535ab5c8ca4Slogin let r = security_check(); 536ab5c8ca4Slogin if r.is_err() { 537ab5c8ca4Slogin Err(r.unwrap_err()) 538ab5c8ca4Slogin } else { 539ab5c8ca4Slogin let buf = unsafe { core::slice::from_raw_parts_mut(buf, len) }; 540ab5c8ca4Slogin Self::recvfrom(args[0], buf, flags, addr, addrlen as *mut u32) 541ab5c8ca4Slogin } 542ab5c8ca4Slogin } 543ab5c8ca4Slogin 544ab5c8ca4Slogin SYS_RECVMSG => { 545ab5c8ca4Slogin let msg = args[1] as *mut crate::net::syscall::MsgHdr; 546ab5c8ca4Slogin let flags = args[2] as u32; 5474537ffb7SChiichen match UserBufferWriter::new( 5484537ffb7SChiichen msg, 5494537ffb7SChiichen core::mem::size_of::<crate::net::syscall::MsgHdr>(), 5504537ffb7SChiichen true, 5514537ffb7SChiichen ) { 5524537ffb7SChiichen Err(e) => Err(e), 5534537ffb7SChiichen Ok(mut user_buffer_writer) => { 5544537ffb7SChiichen match user_buffer_writer.buffer::<crate::net::syscall::MsgHdr>(0) { 5554537ffb7SChiichen Err(e) => Err(e), 5564537ffb7SChiichen Ok(buffer) => { 5574537ffb7SChiichen let msg = &mut buffer[0]; 558ab5c8ca4Slogin Self::recvmsg(args[0], msg, flags) 559ab5c8ca4Slogin } 560ab5c8ca4Slogin } 5614537ffb7SChiichen } 5624537ffb7SChiichen } 5634537ffb7SChiichen } 564ab5c8ca4Slogin 565ab5c8ca4Slogin SYS_LISTEN => Self::listen(args[0], args[1]), 566ab5c8ca4Slogin SYS_SHUTDOWN => Self::shutdown(args[0], args[1]), 567ab5c8ca4Slogin SYS_ACCEPT => Self::accept(args[0], args[1] as *mut SockAddr, args[2] as *mut u32), 568c47fe904SLoGin SYS_ACCEPT4 => Self::accept4( 569c47fe904SLoGin args[0], 570c47fe904SLoGin args[1] as *mut SockAddr, 571c47fe904SLoGin args[2] as *mut u32, 572c47fe904SLoGin args[3] as u32, 573c47fe904SLoGin ), 574ab5c8ca4Slogin SYS_GETSOCKNAME => { 575ab5c8ca4Slogin Self::getsockname(args[0], args[1] as *mut SockAddr, args[2] as *mut u32) 576ab5c8ca4Slogin } 577ab5c8ca4Slogin SYS_GETPEERNAME => { 578ab5c8ca4Slogin Self::getpeername(args[0], args[1] as *mut SockAddr, args[2] as *mut u32) 579ab5c8ca4Slogin } 58036fd0130Shoumkh SYS_GETTIMEOFDAY => { 58136fd0130Shoumkh let timeval = args[0] as *mut PosixTimeval; 58240fe15e0SLoGin let timezone_ptr = args[1] as *mut PosixTimeZone; 58340fe15e0SLoGin Self::gettimeofday(timeval, timezone_ptr) 5844537ffb7SChiichen } 58540fe15e0SLoGin SYS_MMAP => { 58640fe15e0SLoGin let len = page_align_up(args[1]); 587be63f3b2Shoumkh let virt_addr = VirtAddr::new(args[0] as usize); 588be63f3b2Shoumkh if verify_area(virt_addr, len as usize).is_err() { 58940fe15e0SLoGin Err(SystemError::EFAULT) 59040fe15e0SLoGin } else { 59140fe15e0SLoGin Self::mmap( 59240fe15e0SLoGin VirtAddr::new(args[0]), 59340fe15e0SLoGin len, 59440fe15e0SLoGin args[2], 59540fe15e0SLoGin args[3], 59640fe15e0SLoGin args[4] as i32, 59740fe15e0SLoGin args[5], 59840fe15e0SLoGin ) 59940fe15e0SLoGin } 60040fe15e0SLoGin } 60140fe15e0SLoGin SYS_MUNMAP => { 60240fe15e0SLoGin let addr = args[0]; 60340fe15e0SLoGin let len = page_align_up(args[1]); 604971462beSGnoCiYeH if addr & (MMArch::PAGE_SIZE - 1) != 0 { 60540fe15e0SLoGin // The addr argument is not a multiple of the page size 60640fe15e0SLoGin Err(SystemError::EINVAL) 60740fe15e0SLoGin } else { 60840fe15e0SLoGin Self::munmap(VirtAddr::new(addr), len) 60940fe15e0SLoGin } 61040fe15e0SLoGin } 61140fe15e0SLoGin SYS_MPROTECT => { 61240fe15e0SLoGin let addr = args[0]; 61340fe15e0SLoGin let len = page_align_up(args[1]); 614971462beSGnoCiYeH if addr & (MMArch::PAGE_SIZE - 1) != 0 { 61540fe15e0SLoGin // The addr argument is not a multiple of the page size 61640fe15e0SLoGin Err(SystemError::EINVAL) 61740fe15e0SLoGin } else { 61840fe15e0SLoGin Self::mprotect(VirtAddr::new(addr), len, args[2]) 61940fe15e0SLoGin } 62040fe15e0SLoGin } 62140fe15e0SLoGin 6221496ba7bSLoGin SYS_GETCWD => { 6231496ba7bSLoGin let buf = args[0] as *mut u8; 6241496ba7bSLoGin let size = args[1] as usize; 6251496ba7bSLoGin let security_check = || { 6261496ba7bSLoGin verify_area(VirtAddr::new(buf as usize), size)?; 6271496ba7bSLoGin return Ok(()); 6281496ba7bSLoGin }; 6291496ba7bSLoGin let r = security_check(); 6301496ba7bSLoGin if r.is_err() { 6311496ba7bSLoGin Err(r.unwrap_err()) 6321496ba7bSLoGin } else { 6331496ba7bSLoGin let buf = unsafe { core::slice::from_raw_parts_mut(buf, size) }; 6341496ba7bSLoGin Self::getcwd(buf).map(|ptr| ptr.data()) 6351496ba7bSLoGin } 6361496ba7bSLoGin } 6371496ba7bSLoGin 6381496ba7bSLoGin SYS_GETPGID => Self::getpgid(Pid::new(args[0])).map(|pid| pid.into()), 6391496ba7bSLoGin 6401496ba7bSLoGin SYS_GETPPID => Self::getppid().map(|pid| pid.into()), 641be63f3b2Shoumkh SYS_FSTAT => { 642be63f3b2Shoumkh let fd = args[0] as i32; 643be63f3b2Shoumkh let kstat = args[1] as *mut PosixKstat; 644be63f3b2Shoumkh let vaddr = VirtAddr::new(kstat as usize); 645be63f3b2Shoumkh // FIXME 由于c中的verify_area与rust中的verify_area重名,所以在引入时加了前缀区分 646be63f3b2Shoumkh // TODO 应该将用了c版本的verify_area都改为rust的verify_area 647be63f3b2Shoumkh match verify_area(vaddr, core::mem::size_of::<PosixKstat>()) { 648be63f3b2Shoumkh Ok(_) => Self::fstat(fd, kstat), 649be63f3b2Shoumkh Err(e) => Err(e), 650be63f3b2Shoumkh } 651be63f3b2Shoumkh } 652be63f3b2Shoumkh 6536d81180bSLoGin SYS_FCNTL => { 6546d81180bSLoGin let fd = args[0] as i32; 6556d81180bSLoGin let cmd: Option<FcntlCommand> = 6566d81180bSLoGin <FcntlCommand as FromPrimitive>::from_u32(args[1] as u32); 6576d81180bSLoGin let arg = args[2] as i32; 6586d81180bSLoGin let res = if let Some(cmd) = cmd { 6596d81180bSLoGin Self::fcntl(fd, cmd, arg) 6606d81180bSLoGin } else { 6616d81180bSLoGin Err(SystemError::EINVAL) 6626d81180bSLoGin }; 6636d81180bSLoGin 6646d81180bSLoGin // kdebug!("FCNTL: fd: {}, cmd: {:?}, arg: {}, res: {:?}", fd, cmd, arg, res); 6656d81180bSLoGin res 6666d81180bSLoGin } 6676d81180bSLoGin 6686d81180bSLoGin SYS_FTRUNCATE => { 6696d81180bSLoGin let fd = args[0] as i32; 6706d81180bSLoGin let len = args[1] as usize; 6716d81180bSLoGin let res = Self::ftruncate(fd, len); 6726d81180bSLoGin // kdebug!("FTRUNCATE: fd: {}, len: {}, res: {:?}", fd, len, res); 6736d81180bSLoGin res 6746d81180bSLoGin } 6756d81180bSLoGin 6764fda81ceSLoGin #[cfg(target_arch = "x86_64")] 6772dbef785SGnoCiYeH SYS_MKNOD => { 6782dbef785SGnoCiYeH let path = args[0]; 6792dbef785SGnoCiYeH let flags = args[1]; 6802dbef785SGnoCiYeH let dev_t = args[2]; 6812dbef785SGnoCiYeH let flags: ModeType = ModeType::from_bits_truncate(flags as u32); 682c566df45SLoGin Self::mknod(path as *const i8, flags, DeviceNumber::from(dev_t as u32)) 6832dbef785SGnoCiYeH } 6842dbef785SGnoCiYeH 685971462beSGnoCiYeH SYS_CLONE => { 686971462beSGnoCiYeH let parent_tid = VirtAddr::new(args[2]); 687971462beSGnoCiYeH let child_tid = VirtAddr::new(args[3]); 688971462beSGnoCiYeH 689971462beSGnoCiYeH // 地址校验 690971462beSGnoCiYeH verify_area(parent_tid, core::mem::size_of::<i32>())?; 691971462beSGnoCiYeH verify_area(child_tid, core::mem::size_of::<i32>())?; 692971462beSGnoCiYeH 693971462beSGnoCiYeH let mut clone_args = KernelCloneArgs::new(); 694971462beSGnoCiYeH clone_args.flags = CloneFlags::from_bits_truncate(args[0] as u64); 695971462beSGnoCiYeH clone_args.stack = args[1]; 696971462beSGnoCiYeH clone_args.parent_tid = parent_tid; 697971462beSGnoCiYeH clone_args.child_tid = child_tid; 698971462beSGnoCiYeH clone_args.tls = args[4]; 699971462beSGnoCiYeH Self::clone(frame, clone_args) 700971462beSGnoCiYeH } 701971462beSGnoCiYeH 702971462beSGnoCiYeH SYS_FUTEX => { 703971462beSGnoCiYeH let uaddr = VirtAddr::new(args[0]); 704971462beSGnoCiYeH let operation = FutexFlag::from_bits(args[1] as u32).ok_or(SystemError::ENOSYS)?; 705971462beSGnoCiYeH let val = args[2] as u32; 706971462beSGnoCiYeH let utime = args[3]; 707971462beSGnoCiYeH let uaddr2 = VirtAddr::new(args[4]); 708971462beSGnoCiYeH let val3 = args[5] as u32; 709971462beSGnoCiYeH 710971462beSGnoCiYeH verify_area(uaddr, core::mem::size_of::<u32>())?; 711971462beSGnoCiYeH verify_area(uaddr2, core::mem::size_of::<u32>())?; 712971462beSGnoCiYeH 713971462beSGnoCiYeH let mut timespec = None; 714971462beSGnoCiYeH if utime != 0 && operation.contains(FutexFlag::FLAGS_HAS_TIMEOUT) { 715971462beSGnoCiYeH let reader = UserBufferReader::new( 716971462beSGnoCiYeH utime as *const TimeSpec, 717971462beSGnoCiYeH core::mem::size_of::<TimeSpec>(), 718971462beSGnoCiYeH true, 719971462beSGnoCiYeH )?; 720971462beSGnoCiYeH 721971462beSGnoCiYeH timespec = Some(reader.read_one_from_user::<TimeSpec>(0)?.clone()); 722971462beSGnoCiYeH } 723971462beSGnoCiYeH 724971462beSGnoCiYeH Self::do_futex(uaddr, operation, val, timespec, uaddr2, utime as u32, val3) 725971462beSGnoCiYeH } 726971462beSGnoCiYeH 727bf4a4899SLoGin SYS_READV => Self::readv(args[0] as i32, args[1], args[2]), 728971462beSGnoCiYeH SYS_WRITEV => Self::writev(args[0] as i32, args[1], args[2]), 729971462beSGnoCiYeH 7304fda81ceSLoGin SYS_SET_TID_ADDRESS => Self::set_tid_address(args[0]), 731971462beSGnoCiYeH 7324fda81ceSLoGin #[cfg(target_arch = "x86_64")] 7334fda81ceSLoGin SYS_LSTAT => { 734971462beSGnoCiYeH let path: &CStr = unsafe { CStr::from_ptr(args[0] as *const c_char) }; 735971462beSGnoCiYeH let path: Result<&str, core::str::Utf8Error> = path.to_str(); 736971462beSGnoCiYeH let res = if path.is_err() { 737971462beSGnoCiYeH Err(SystemError::EINVAL) 738971462beSGnoCiYeH } else { 739971462beSGnoCiYeH let path: &str = path.unwrap(); 740971462beSGnoCiYeH let kstat = args[1] as *mut PosixKstat; 741971462beSGnoCiYeH let vaddr = VirtAddr::new(kstat as usize); 742971462beSGnoCiYeH match verify_area(vaddr, core::mem::size_of::<PosixKstat>()) { 7434fda81ceSLoGin Ok(_) => Self::lstat(path, kstat), 7444fda81ceSLoGin Err(e) => Err(e), 7454fda81ceSLoGin } 7464fda81ceSLoGin }; 7474fda81ceSLoGin 7484fda81ceSLoGin res 7494fda81ceSLoGin } 7504fda81ceSLoGin 7514fda81ceSLoGin #[cfg(target_arch = "x86_64")] 7524fda81ceSLoGin SYS_STAT => { 7534fda81ceSLoGin let path: &CStr = unsafe { CStr::from_ptr(args[0] as *const c_char) }; 7544fda81ceSLoGin let path: Result<&str, core::str::Utf8Error> = path.to_str(); 7554fda81ceSLoGin let res = if path.is_err() { 7564fda81ceSLoGin Err(SystemError::EINVAL) 757bf4a4899SLoGin } else { 7584fda81ceSLoGin let path: &str = path.unwrap(); 7594fda81ceSLoGin let kstat = args[1] as *mut PosixKstat; 7604fda81ceSLoGin let vaddr = VirtAddr::new(kstat as usize); 7614fda81ceSLoGin match verify_area(vaddr, core::mem::size_of::<PosixKstat>()) { 7624fda81ceSLoGin Ok(_) => Self::stat(path, kstat), 763971462beSGnoCiYeH Err(e) => Err(e), 764971462beSGnoCiYeH } 765971462beSGnoCiYeH }; 766971462beSGnoCiYeH 767971462beSGnoCiYeH res 768971462beSGnoCiYeH } 769971462beSGnoCiYeH 77040609970SGnoCiYeH SYS_EPOLL_CREATE => Self::epoll_create(args[0] as i32), 77140609970SGnoCiYeH SYS_EPOLL_CREATE1 => Self::epoll_create1(args[0]), 77240609970SGnoCiYeH 77340609970SGnoCiYeH SYS_EPOLL_CTL => Self::epoll_ctl( 77440609970SGnoCiYeH args[0] as i32, 77540609970SGnoCiYeH args[1], 77640609970SGnoCiYeH args[2] as i32, 77740609970SGnoCiYeH VirtAddr::new(args[3]), 77840609970SGnoCiYeH ), 77940609970SGnoCiYeH 78040609970SGnoCiYeH SYS_EPOLL_WAIT => Self::epoll_wait( 78140609970SGnoCiYeH args[0] as i32, 78240609970SGnoCiYeH VirtAddr::new(args[1]), 78340609970SGnoCiYeH args[2] as i32, 78440609970SGnoCiYeH args[3] as i32, 78540609970SGnoCiYeH ), 78640609970SGnoCiYeH 78740609970SGnoCiYeH SYS_EPOLL_PWAIT => { 78840609970SGnoCiYeH let epfd = args[0] as i32; 78940609970SGnoCiYeH let epoll_event = VirtAddr::new(args[1]); 79040609970SGnoCiYeH let max_events = args[2] as i32; 79140609970SGnoCiYeH let timespec = args[3] as i32; 79240609970SGnoCiYeH let sigmask_addr = args[4] as *mut SigSet; 79340609970SGnoCiYeH 79440609970SGnoCiYeH if sigmask_addr.is_null() { 79540609970SGnoCiYeH return Self::epoll_wait(epfd, epoll_event, max_events, timespec); 79640609970SGnoCiYeH } 79740609970SGnoCiYeH let sigmask_reader = 79840609970SGnoCiYeH UserBufferReader::new(sigmask_addr, core::mem::size_of::<SigSet>(), true)?; 79940609970SGnoCiYeH let mut sigmask = sigmask_reader.read_one_from_user::<SigSet>(0)?.clone(); 80040609970SGnoCiYeH 80140609970SGnoCiYeH Self::epoll_pwait( 80240609970SGnoCiYeH args[0] as i32, 80340609970SGnoCiYeH VirtAddr::new(args[1]), 80440609970SGnoCiYeH args[2] as i32, 80540609970SGnoCiYeH args[3] as i32, 80640609970SGnoCiYeH &mut sigmask, 80740609970SGnoCiYeH ) 80840609970SGnoCiYeH } 80940609970SGnoCiYeH 810971462beSGnoCiYeH // 目前为了适配musl-libc,以下系统调用先这样写着 8114fda81ceSLoGin SYS_GETRANDOM => { 812971462beSGnoCiYeH let flags = GRandFlags::from_bits(args[2] as u8).ok_or(SystemError::EINVAL)?; 813971462beSGnoCiYeH Self::get_random(args[0] as *mut u8, args[1], flags) 814971462beSGnoCiYeH } 815971462beSGnoCiYeH 8164fda81ceSLoGin SYS_SOCKETPAIR => { 817971462beSGnoCiYeH unimplemented!() 818971462beSGnoCiYeH } 819971462beSGnoCiYeH 8204fda81ceSLoGin #[cfg(target_arch = "x86_64")] 821971462beSGnoCiYeH SYS_POLL => { 822971462beSGnoCiYeH kwarn!("SYS_POLL has not yet been implemented"); 823971462beSGnoCiYeH Ok(0) 824971462beSGnoCiYeH } 825971462beSGnoCiYeH 826971462beSGnoCiYeH SYS_RT_SIGPROCMASK => { 827971462beSGnoCiYeH kwarn!("SYS_RT_SIGPROCMASK has not yet been implemented"); 828971462beSGnoCiYeH Ok(0) 829971462beSGnoCiYeH } 830971462beSGnoCiYeH 831971462beSGnoCiYeH SYS_TKILL => { 832971462beSGnoCiYeH kwarn!("SYS_TKILL has not yet been implemented"); 833971462beSGnoCiYeH Ok(0) 834971462beSGnoCiYeH } 835971462beSGnoCiYeH 836971462beSGnoCiYeH SYS_SIGALTSTACK => { 837971462beSGnoCiYeH kwarn!("SYS_SIGALTSTACK has not yet been implemented"); 838971462beSGnoCiYeH Ok(0) 839971462beSGnoCiYeH } 840971462beSGnoCiYeH 84116033951SGnoCiYeH SYS_EXIT_GROUP => { 84216033951SGnoCiYeH kwarn!("SYS_EXIT_GROUP has not yet been implemented"); 84316033951SGnoCiYeH Ok(0) 84416033951SGnoCiYeH } 84516033951SGnoCiYeH 84604babc3fSMemoryShore SYS_MADVISE => { 8476c7f966cSMemoryShore // 这个太吵了,总是打印,先注释掉 8486c7f966cSMemoryShore // kwarn!("SYS_MADVISE has not yet been implemented"); 84904babc3fSMemoryShore Ok(0) 85004babc3fSMemoryShore } 851393f6915SLoGin SYS_GETTID => Self::gettid().map(|tid| tid.into()), 85202e249f3SLoGin SYS_GETUID => Self::getuid().map(|uid| uid.into()), 8538d72b68dSJomo 85402e249f3SLoGin SYS_SYSLOG => { 8558d72b68dSJomo let syslog_action_type = args[0] as usize; 8568d72b68dSJomo let buf_vaddr = args[1]; 8578d72b68dSJomo let len = args[2]; 8588d72b68dSJomo let from_user = frame.from_user(); 8598d72b68dSJomo let mut user_buffer_writer = 8608d72b68dSJomo UserBufferWriter::new(buf_vaddr as *mut u8, len, from_user)?; 8618d72b68dSJomo 8628d72b68dSJomo let user_buf = user_buffer_writer.buffer(0)?; 8638d72b68dSJomo let res = Self::do_syslog(syslog_action_type, user_buf, len); 8648d72b68dSJomo res 86502e249f3SLoGin } 8668d72b68dSJomo 86702e249f3SLoGin SYS_GETGID => Self::getgid().map(|gid| gid.into()), 86802e249f3SLoGin SYS_SETUID => { 86902e249f3SLoGin kwarn!("SYS_SETUID has not yet been implemented"); 87002e249f3SLoGin Ok(0) 87102e249f3SLoGin } 87202e249f3SLoGin SYS_SETGID => { 87302e249f3SLoGin kwarn!("SYS_SETGID has not yet been implemented"); 87402e249f3SLoGin Ok(0) 87502e249f3SLoGin } 87602e249f3SLoGin SYS_GETEUID => Self::geteuid().map(|euid| euid.into()), 87702e249f3SLoGin SYS_GETEGID => Self::getegid().map(|egid| egid.into()), 878be8cdf4bSLoGin SYS_GETRUSAGE => { 879be8cdf4bSLoGin let who = args[0] as c_int; 880be8cdf4bSLoGin let rusage = args[1] as *mut RUsage; 881be8cdf4bSLoGin Self::get_rusage(who, rusage) 882be8cdf4bSLoGin } 88304babc3fSMemoryShore 8844fda81ceSLoGin #[cfg(target_arch = "x86_64")] 885709498caSLoGin SYS_READLINK => { 886709498caSLoGin let path = args[0] as *const u8; 887709498caSLoGin let buf = args[1] as *mut u8; 888709498caSLoGin let bufsiz = args[2] as usize; 889709498caSLoGin Self::readlink(path, buf, bufsiz) 890709498caSLoGin } 891709498caSLoGin 8924fda81ceSLoGin SYS_READLINKAT => { 893709498caSLoGin let dirfd = args[0] as i32; 894709498caSLoGin let pathname = args[1] as *const u8; 895709498caSLoGin let buf = args[2] as *mut u8; 896709498caSLoGin let bufsiz = args[3] as usize; 897709498caSLoGin Self::readlink_at(dirfd, pathname, buf, bufsiz) 898709498caSLoGin } 899709498caSLoGin 9000d9b7d92SLoGin SYS_PRLIMIT64 => { 9010d9b7d92SLoGin let pid = args[0]; 9020d9b7d92SLoGin let pid = Pid::new(pid); 9030d9b7d92SLoGin let resource = args[1]; 9040d9b7d92SLoGin let new_limit = args[2] as *const RLimit64; 9050d9b7d92SLoGin let old_limit = args[3] as *mut RLimit64; 9060d9b7d92SLoGin 9070d9b7d92SLoGin Self::prlimit64(pid, resource, new_limit, old_limit) 9080d9b7d92SLoGin } 9090d9b7d92SLoGin 9104fda81ceSLoGin #[cfg(target_arch = "x86_64")] 9119b0abe6dSLoGin SYS_ACCESS => { 9129b0abe6dSLoGin let pathname = args[0] as *const u8; 9139b0abe6dSLoGin let mode = args[1] as u32; 9149b0abe6dSLoGin Self::access(pathname, mode) 9159b0abe6dSLoGin } 9169b0abe6dSLoGin 9179b0abe6dSLoGin SYS_FACCESSAT => { 9189b0abe6dSLoGin let dirfd = args[0] as i32; 9199b0abe6dSLoGin let pathname = args[1] as *const u8; 9209b0abe6dSLoGin let mode = args[2] as u32; 9219b0abe6dSLoGin Self::faccessat2(dirfd, pathname, mode, 0) 9229b0abe6dSLoGin } 9239b0abe6dSLoGin 9249b0abe6dSLoGin SYS_FACCESSAT2 => { 9259b0abe6dSLoGin let dirfd = args[0] as i32; 9269b0abe6dSLoGin let pathname = args[1] as *const u8; 9279b0abe6dSLoGin let mode = args[2] as u32; 9289b0abe6dSLoGin let flags = args[3] as u32; 9299b0abe6dSLoGin Self::faccessat2(dirfd, pathname, mode, flags) 9309b0abe6dSLoGin } 9319b0abe6dSLoGin 932bf4a4899SLoGin SYS_CLOCK_GETTIME => { 933bf4a4899SLoGin let clockid = args[0] as i32; 934bf4a4899SLoGin let timespec = args[1] as *mut TimeSpec; 935bf4a4899SLoGin Self::clock_gettime(clockid, timespec) 936bf4a4899SLoGin } 937bf4a4899SLoGin 938bf4a4899SLoGin SYS_SYSINFO => { 939bf4a4899SLoGin let info = args[0] as *mut SysInfo; 940bf4a4899SLoGin Self::sysinfo(info) 941bf4a4899SLoGin } 942bf4a4899SLoGin 943bf4a4899SLoGin SYS_UMASK => { 944bf4a4899SLoGin let mask = args[0] as u32; 945bf4a4899SLoGin Self::umask(mask) 946bf4a4899SLoGin } 947bf4a4899SLoGin 9484fda81ceSLoGin #[cfg(target_arch = "x86_64")] 949bf4a4899SLoGin SYS_CHMOD => { 950bf4a4899SLoGin let pathname = args[0] as *const u8; 951bf4a4899SLoGin let mode = args[1] as u32; 952bf4a4899SLoGin Self::chmod(pathname, mode) 953bf4a4899SLoGin } 954bf4a4899SLoGin SYS_FCHMOD => { 955bf4a4899SLoGin let fd = args[0] as i32; 956bf4a4899SLoGin let mode = args[1] as u32; 957bf4a4899SLoGin Self::fchmod(fd, mode) 958bf4a4899SLoGin } 959bf4a4899SLoGin SYS_FCHMODAT => { 960bf4a4899SLoGin let dirfd = args[0] as i32; 961bf4a4899SLoGin let pathname = args[1] as *const u8; 962bf4a4899SLoGin let mode = args[2] as u32; 963bf4a4899SLoGin Self::fchmodat(dirfd, pathname, mode) 964bf4a4899SLoGin } 965bf4a4899SLoGin 96602343d0bSLoGin SYS_SCHED_GETAFFINITY => { 96702343d0bSLoGin // todo: 这个系统调用还没有实现 96802343d0bSLoGin 96902343d0bSLoGin Err(SystemError::ENOSYS) 97002343d0bSLoGin } 97102343d0bSLoGin 97202343d0bSLoGin SYS_SCHED_YIELD => Self::sched_yield(), 97302343d0bSLoGin 974ab5c8ca4Slogin _ => panic!("Unsupported syscall ID: {}", syscall_num), 975ab5c8ca4Slogin }; 9764fda81ceSLoGin 977ab5c8ca4Slogin return r; 978ab5c8ca4Slogin } 979ab5c8ca4Slogin 980ab5c8ca4Slogin pub fn put_string( 981ab5c8ca4Slogin s: *const u8, 982ab5c8ca4Slogin front_color: u32, 983ab5c8ca4Slogin back_color: u32, 984ab5c8ca4Slogin ) -> Result<usize, SystemError> { 985ab5c8ca4Slogin return Ok(unsafe { do_put_string(s, front_color, back_color) }); 986ab5c8ca4Slogin } 987ab5c8ca4Slogin 988ab5c8ca4Slogin pub fn reboot() -> Result<usize, SystemError> { 9894fda81ceSLoGin unsafe { cpu_reset() }; 990ab5c8ca4Slogin } 991ab5c8ca4Slogin } 992