1ab5c8ca4Slogin use core::{ 282df0a13Shmt ffi::{c_int, c_void}, 31d37ca6dSDonkey Kane ptr::null, 4ab5c8ca4Slogin sync::atomic::{AtomicBool, Ordering}, 5ab5c8ca4Slogin }; 6ab5c8ca4Slogin 7971462beSGnoCiYeH use crate::{ 840609970SGnoCiYeH arch::{ipc::signal::SigSet, syscall::nr::*}, 9b4eb05a1STTaq filesystem::vfs::syscall::PosixStatx, 10971462beSGnoCiYeH libs::{futex::constant::FutexFlag, rand::GRandFlags}, 114cfa009bSJomo mm::syscall::MremapFlags, 124ad52e57S裕依2439 net::syscall::MsgHdr, 130d9b7d92SLoGin process::{ 140d9b7d92SLoGin fork::KernelCloneArgs, 150d9b7d92SLoGin resource::{RLimit64, RUsage}, 1652da9a59SGnoCiYeH ProcessManager, 170d9b7d92SLoGin }, 18c3dc6f2fSLoGin syscall::user_access::check_and_clone_cstr, 19971462beSGnoCiYeH }; 2040314b30SXiaoye Zheng 2191e9d4abSLoGin use num_traits::FromPrimitive; 2291e9d4abSLoGin use system_error::SystemError; 23676b8ef6SMork 24ab5c8ca4Slogin use crate::{ 251496ba7bSLoGin arch::{cpu::cpu_reset, interrupt::TrapFrame, MMArch}, 26ab5c8ca4Slogin filesystem::vfs::{ 279e481b3bSTTaq fcntl::{AtFlags, FcntlCommand}, 28ab5c8ca4Slogin file::FileMode, 2982df0a13Shmt syscall::{ModeType, PosixKstat}, 30ab5c8ca4Slogin MAX_PATHLEN, 31ab5c8ca4Slogin }, 3282df0a13Shmt include::bindings::bindings::PAGE_4K_SIZE, 33aa0367d6SLoGin kinfo, 3440fe15e0SLoGin libs::align::page_align_up, 35be63f3b2Shoumkh mm::{verify_area, MemoryManagementArch, VirtAddr}, 36ab5c8ca4Slogin net::syscall::SockAddr, 378c6f2184Ssspphh process::{fork::CloneFlags, syscall::PosixOldUtsName, 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 => { 9382df0a13Shmt let path = args[0] as *const u8; 9482df0a13Shmt let flags = args[1] as u32; 9582df0a13Shmt let mode = args[2] as u32; 9634e6d6c8Syuyi2439 9782df0a13Shmt Self::open(path, flags, mode, true) 980fb515b0SLoGin } 990fb515b0SLoGin 10070f159a3SLoGin #[cfg(target_arch = "x86_64")] 1019e481b3bSTTaq SYS_RENAME => { 1029e481b3bSTTaq let oldname: *const u8 = args[0] as *const u8; 1039e481b3bSTTaq let newname: *const u8 = args[1] as *const u8; 1049e481b3bSTTaq Self::do_renameat2( 1059e481b3bSTTaq AtFlags::AT_FDCWD.bits(), 1069e481b3bSTTaq oldname, 1079e481b3bSTTaq AtFlags::AT_FDCWD.bits(), 1089e481b3bSTTaq newname, 1099e481b3bSTTaq 0, 1109e481b3bSTTaq ) 1119e481b3bSTTaq } 1129e481b3bSTTaq 11370f159a3SLoGin #[cfg(target_arch = "x86_64")] 1149e481b3bSTTaq SYS_RENAMEAT => { 1159e481b3bSTTaq let oldfd = args[0] as i32; 1169e481b3bSTTaq let oldname: *const u8 = args[1] as *const u8; 1179e481b3bSTTaq let newfd = args[2] as i32; 1189e481b3bSTTaq let newname: *const u8 = args[3] as *const u8; 1199e481b3bSTTaq Self::do_renameat2(oldfd, oldname, newfd, newname, 0) 1209e481b3bSTTaq } 1219e481b3bSTTaq 1229e481b3bSTTaq SYS_RENAMEAT2 => { 1239e481b3bSTTaq let oldfd = args[0] as i32; 1249e481b3bSTTaq let oldname: *const u8 = args[1] as *const u8; 1259e481b3bSTTaq let newfd = args[2] as i32; 1269e481b3bSTTaq let newname: *const u8 = args[3] as *const u8; 1279e481b3bSTTaq let flags = args[4] as u32; 1289e481b3bSTTaq Self::do_renameat2(oldfd, oldname, newfd, newname, flags) 1299e481b3bSTTaq } 1309e481b3bSTTaq 1310fb515b0SLoGin SYS_OPENAT => { 1320fb515b0SLoGin let dirfd = args[0] as i32; 13382df0a13Shmt let path = args[1] as *const u8; 13482df0a13Shmt let flags = args[2] as u32; 13582df0a13Shmt let mode = args[3] as u32; 1360fb515b0SLoGin 13782df0a13Shmt Self::openat(dirfd, path, flags, mode, true) 138ab5c8ca4Slogin } 139ab5c8ca4Slogin SYS_CLOSE => { 140ab5c8ca4Slogin let fd = args[0]; 14127b967a3S裕依 Self::close(fd) 142ab5c8ca4Slogin } 143ab5c8ca4Slogin SYS_READ => { 144ab5c8ca4Slogin let fd = args[0] as i32; 145ab5c8ca4Slogin let buf_vaddr = args[1]; 146ab5c8ca4Slogin let len = args[2]; 147*b5b571e0SLoGin let from_user = frame.is_from_user(); 1489e9ffedfSPlucky923 let mut user_buffer_writer = 1499e9ffedfSPlucky923 UserBufferWriter::new(buf_vaddr as *mut u8, len, from_user)?; 1506d81180bSLoGin 1519e9ffedfSPlucky923 let user_buf = user_buffer_writer.buffer(0)?; 15227b967a3S裕依 Self::read(fd, user_buf) 153ab5c8ca4Slogin } 154ab5c8ca4Slogin SYS_WRITE => { 155ab5c8ca4Slogin let fd = args[0] as i32; 156ab5c8ca4Slogin let buf_vaddr = args[1]; 157ab5c8ca4Slogin let len = args[2]; 158*b5b571e0SLoGin let from_user = frame.is_from_user(); 1599e9ffedfSPlucky923 let user_buffer_reader = 1609e9ffedfSPlucky923 UserBufferReader::new(buf_vaddr as *const u8, len, from_user)?; 1616d81180bSLoGin 1629e9ffedfSPlucky923 let user_buf = user_buffer_reader.read_from_user(0)?; 16327b967a3S裕依 Self::write(fd, user_buf) 164ab5c8ca4Slogin } 165ab5c8ca4Slogin 166ab5c8ca4Slogin SYS_LSEEK => { 167ab5c8ca4Slogin let fd = args[0] as i32; 168ab5c8ca4Slogin let offset = args[1] as i64; 169ab5c8ca4Slogin let whence = args[2] as u32; 170ab5c8ca4Slogin 17182df0a13Shmt Self::lseek(fd, offset, whence) 172ab5c8ca4Slogin } 17327b967a3S裕依 17427b967a3S裕依 SYS_PREAD64 => { 17527b967a3S裕依 let fd = args[0] as i32; 17627b967a3S裕依 let buf_vaddr = args[1]; 17727b967a3S裕依 let len = args[2]; 17827b967a3S裕依 let offset = args[3]; 17927b967a3S裕依 18027b967a3S裕依 let mut user_buffer_writer = 181*b5b571e0SLoGin UserBufferWriter::new(buf_vaddr as *mut u8, len, frame.is_from_user())?; 18227b967a3S裕依 let buf = user_buffer_writer.buffer(0)?; 18327b967a3S裕依 Self::pread(fd, buf, len, offset) 18427b967a3S裕依 } 18527b967a3S裕依 18627b967a3S裕依 SYS_PWRITE64 => { 18727b967a3S裕依 let fd = args[0] as i32; 18827b967a3S裕依 let buf_vaddr = args[1]; 18927b967a3S裕依 let len = args[2]; 19027b967a3S裕依 let offset = args[3]; 19127b967a3S裕依 19227b967a3S裕依 let user_buffer_reader = 193*b5b571e0SLoGin UserBufferReader::new(buf_vaddr as *const u8, len, frame.is_from_user())?; 19427b967a3S裕依 19527b967a3S裕依 let buf = user_buffer_reader.read_from_user(0)?; 19627b967a3S裕依 Self::pwrite(fd, buf, len, offset) 19727b967a3S裕依 } 19827b967a3S裕依 19940314b30SXiaoye Zheng SYS_IOCTL => { 20040314b30SXiaoye Zheng let fd = args[0]; 20140314b30SXiaoye Zheng let cmd = args[1]; 20240314b30SXiaoye Zheng let data = args[2]; 20340314b30SXiaoye Zheng Self::ioctl(fd, cmd as u32, data) 20440314b30SXiaoye Zheng } 205ab5c8ca4Slogin 2064fda81ceSLoGin #[cfg(target_arch = "x86_64")] 2071496ba7bSLoGin SYS_FORK => Self::fork(frame), 2084fda81ceSLoGin #[cfg(target_arch = "x86_64")] 2091496ba7bSLoGin SYS_VFORK => Self::vfork(frame), 2101496ba7bSLoGin 211ab5c8ca4Slogin SYS_BRK => { 21240fe15e0SLoGin let new_brk = VirtAddr::new(args[0]); 21340fe15e0SLoGin Self::brk(new_brk).map(|vaddr| vaddr.data()) 214ab5c8ca4Slogin } 215ab5c8ca4Slogin 216ab5c8ca4Slogin SYS_SBRK => { 217ab5c8ca4Slogin let increment = args[0] as isize; 2181496ba7bSLoGin Self::sbrk(increment).map(|vaddr: VirtAddr| vaddr.data()) 219ab5c8ca4Slogin } 220ab5c8ca4Slogin 221ab5c8ca4Slogin SYS_REBOOT => Self::reboot(), 222ab5c8ca4Slogin 223ab5c8ca4Slogin SYS_CHDIR => { 22482df0a13Shmt let r = args[0] as *const u8; 2259e9ffedfSPlucky923 Self::chdir(r) 226ab5c8ca4Slogin } 227ab5c8ca4Slogin 2284fda81ceSLoGin #[allow(unreachable_patterns)] 2294fda81ceSLoGin SYS_GETDENTS64 | SYS_GETDENTS => { 230ab5c8ca4Slogin let fd = args[0] as i32; 23134e6d6c8Syuyi2439 232ab5c8ca4Slogin let buf_vaddr = args[1]; 233ab5c8ca4Slogin let len = args[2]; 234f5df0e79SLoGin let virt_addr: VirtAddr = VirtAddr::new(buf_vaddr); 235ab5c8ca4Slogin // 判断缓冲区是否来自用户态,进行权限校验 236*b5b571e0SLoGin let res = if frame.is_from_user() && verify_area(virt_addr, len).is_err() { 237ab5c8ca4Slogin // 来自用户态,而buffer在内核态,这样的操作不被允许 238ab5c8ca4Slogin Err(SystemError::EPERM) 239ab5c8ca4Slogin } else if buf_vaddr == 0 { 240ab5c8ca4Slogin Err(SystemError::EFAULT) 241ab5c8ca4Slogin } else { 242ab5c8ca4Slogin let buf: &mut [u8] = unsafe { 243ab5c8ca4Slogin core::slice::from_raw_parts_mut::<'static, u8>(buf_vaddr as *mut u8, len) 244ab5c8ca4Slogin }; 245ab5c8ca4Slogin Self::getdents(fd, buf) 246ab5c8ca4Slogin }; 247ab5c8ca4Slogin 248ab5c8ca4Slogin res 249ab5c8ca4Slogin } 250ab5c8ca4Slogin 251ab5c8ca4Slogin SYS_EXECVE => { 252ab5c8ca4Slogin let path_ptr = args[0]; 253ab5c8ca4Slogin let argv_ptr = args[1]; 254ab5c8ca4Slogin let env_ptr = args[2]; 255be63f3b2Shoumkh let virt_path_ptr = VirtAddr::new(path_ptr); 256be63f3b2Shoumkh let virt_argv_ptr = VirtAddr::new(argv_ptr); 257be63f3b2Shoumkh let virt_env_ptr = VirtAddr::new(env_ptr); 258ab5c8ca4Slogin // 权限校验 259*b5b571e0SLoGin if frame.is_from_user() 260*b5b571e0SLoGin && (verify_area(virt_path_ptr, MAX_PATHLEN).is_err() 261be63f3b2Shoumkh || verify_area(virt_argv_ptr, PAGE_4K_SIZE as usize).is_err()) 262be63f3b2Shoumkh || verify_area(virt_env_ptr, PAGE_4K_SIZE as usize).is_err() 263ab5c8ca4Slogin { 264ab5c8ca4Slogin Err(SystemError::EFAULT) 265ab5c8ca4Slogin } else { 266ab5c8ca4Slogin Self::execve( 2671496ba7bSLoGin path_ptr as *const u8, 2681496ba7bSLoGin argv_ptr as *const *const u8, 2691496ba7bSLoGin env_ptr as *const *const u8, 2701496ba7bSLoGin frame, 271ab5c8ca4Slogin ) 2721496ba7bSLoGin .map(|_| 0) 273ab5c8ca4Slogin } 274ab5c8ca4Slogin } 275ab5c8ca4Slogin SYS_WAIT4 => { 276bf4a4899SLoGin let pid = args[0] as i32; 2771496ba7bSLoGin let wstatus = args[1] as *mut i32; 278ab5c8ca4Slogin let options = args[2] as c_int; 279ab5c8ca4Slogin let rusage = args[3] as *mut c_void; 280ab5c8ca4Slogin // 权限校验 281ab5c8ca4Slogin // todo: 引入rusage之后,更正以下权限校验代码中,rusage的大小 282bf4a4899SLoGin Self::wait4(pid.into(), wstatus, options, rusage) 283ab5c8ca4Slogin } 284ab5c8ca4Slogin 285ab5c8ca4Slogin SYS_EXIT => { 286ab5c8ca4Slogin let exit_code = args[0]; 287ab5c8ca4Slogin Self::exit(exit_code) 288ab5c8ca4Slogin } 2894fda81ceSLoGin #[cfg(target_arch = "x86_64")] 290ab5c8ca4Slogin SYS_MKDIR => { 29182df0a13Shmt let path = args[0] as *const u8; 292ab5c8ca4Slogin let mode = args[1]; 293ab5c8ca4Slogin 29482df0a13Shmt Self::mkdir(path, mode) 295ab5c8ca4Slogin } 296ab5c8ca4Slogin 297ab5c8ca4Slogin SYS_NANOSLEEP => { 298ab5c8ca4Slogin let req = args[0] as *const TimeSpec; 299ab5c8ca4Slogin let rem = args[1] as *mut TimeSpec; 300be63f3b2Shoumkh let virt_req = VirtAddr::new(req as usize); 301be63f3b2Shoumkh let virt_rem = VirtAddr::new(rem as usize); 302*b5b571e0SLoGin if frame.is_from_user() 303*b5b571e0SLoGin && (verify_area(virt_req, core::mem::size_of::<TimeSpec>()).is_err() 304*b5b571e0SLoGin || verify_area(virt_rem, core::mem::size_of::<TimeSpec>()).is_err()) 305ab5c8ca4Slogin { 306ab5c8ca4Slogin Err(SystemError::EFAULT) 307ab5c8ca4Slogin } else { 308ab5c8ca4Slogin Self::nanosleep(req, rem) 309ab5c8ca4Slogin } 310ab5c8ca4Slogin } 311ab5c8ca4Slogin 312ab5c8ca4Slogin SYS_CLOCK => Self::clock(), 3134fda81ceSLoGin #[cfg(target_arch = "x86_64")] 314ab5c8ca4Slogin SYS_PIPE => { 31522c9db31Shanjiezhou let pipefd: *mut i32 = args[0] as *mut c_int; 3160fb515b0SLoGin if pipefd.is_null() { 3170fb515b0SLoGin Err(SystemError::EFAULT) 3180fb515b0SLoGin } else { 3190fb515b0SLoGin Self::pipe2(pipefd, FileMode::empty()) 3200fb515b0SLoGin } 3210fb515b0SLoGin } 3224ad52e57S裕依2439 3230fb515b0SLoGin SYS_PIPE2 => { 3240fb515b0SLoGin let pipefd: *mut i32 = args[0] as *mut c_int; 32522c9db31Shanjiezhou let arg1 = args[1]; 32622c9db31Shanjiezhou if pipefd.is_null() { 32722c9db31Shanjiezhou Err(SystemError::EFAULT) 32822c9db31Shanjiezhou } else { 32922c9db31Shanjiezhou let flags = FileMode::from_bits_truncate(arg1 as u32); 33022c9db31Shanjiezhou Self::pipe2(pipefd, flags) 331ab5c8ca4Slogin } 332ab5c8ca4Slogin } 333ab5c8ca4Slogin 3344fda81ceSLoGin SYS_UNLINKAT => { 335ab5c8ca4Slogin let dirfd = args[0] as i32; 33682df0a13Shmt let path = args[1] as *const u8; 337ab5c8ca4Slogin let flags = args[2] as u32; 33882df0a13Shmt Self::unlinkat(dirfd, path, flags) 339ab5c8ca4Slogin } 340bf4a4899SLoGin 3414fda81ceSLoGin #[cfg(target_arch = "x86_64")] 3425eeefb8cSChenzx SYS_RMDIR => { 34382df0a13Shmt let path = args[0] as *const u8; 34482df0a13Shmt Self::rmdir(path) 3455eeefb8cSChenzx } 3465eeefb8cSChenzx 3474695947eSChenzx SYS_LINK => { 3484695947eSChenzx let old = args[0] as *const u8; 3494695947eSChenzx let new = args[1] as *const u8; 3504695947eSChenzx return Self::link(old, new); 3514695947eSChenzx } 3524695947eSChenzx 3534695947eSChenzx SYS_LINKAT => { 3544695947eSChenzx let oldfd = args[0] as i32; 3554695947eSChenzx let old = args[1] as *const u8; 3564695947eSChenzx let newfd = args[2] as i32; 3574695947eSChenzx let new = args[3] as *const u8; 3584695947eSChenzx let flags = args[4] as i32; 3594695947eSChenzx return Self::linkat(oldfd, old, newfd, new, flags); 3604695947eSChenzx } 3614695947eSChenzx 3625eeefb8cSChenzx #[cfg(target_arch = "x86_64")] 363bf4a4899SLoGin SYS_UNLINK => { 36482df0a13Shmt let path = args[0] as *const u8; 36582df0a13Shmt Self::unlink(path) 366bf4a4899SLoGin } 367ab5c8ca4Slogin SYS_KILL => { 3681496ba7bSLoGin let pid = Pid::new(args[0]); 369ab5c8ca4Slogin let sig = args[1] as c_int; 3703c82aa56SChiichen // kdebug!("KILL SYSCALL RECEIVED"); 371ab5c8ca4Slogin Self::kill(pid, sig) 372ab5c8ca4Slogin } 373ab5c8ca4Slogin 3744fda81ceSLoGin SYS_RT_SIGACTION => { 375ab5c8ca4Slogin let sig = args[0] as c_int; 376ab5c8ca4Slogin let act = args[1]; 377ab5c8ca4Slogin let old_act = args[2]; 378*b5b571e0SLoGin Self::sigaction(sig, act, old_act, frame.is_from_user()) 379ab5c8ca4Slogin } 380ab5c8ca4Slogin 3811496ba7bSLoGin SYS_GETPID => Self::getpid().map(|pid| pid.into()), 382ab5c8ca4Slogin 383*b5b571e0SLoGin SYS_SCHED => Self::sched(frame.is_from_user()), 384ab5c8ca4Slogin SYS_DUP => { 385ab5c8ca4Slogin let oldfd: i32 = args[0] as c_int; 386ab5c8ca4Slogin Self::dup(oldfd) 387ab5c8ca4Slogin } 3884fda81ceSLoGin 3894fda81ceSLoGin #[cfg(target_arch = "x86_64")] 390ab5c8ca4Slogin SYS_DUP2 => { 391ab5c8ca4Slogin let oldfd: i32 = args[0] as c_int; 392ab5c8ca4Slogin let newfd: i32 = args[1] as c_int; 393ab5c8ca4Slogin Self::dup2(oldfd, newfd) 394ab5c8ca4Slogin } 395ab5c8ca4Slogin 396ab5c8ca4Slogin SYS_SOCKET => Self::socket(args[0], args[1], args[2]), 397ab5c8ca4Slogin SYS_SETSOCKOPT => { 398ab5c8ca4Slogin let optval = args[3] as *const u8; 399*b5b571e0SLoGin let optlen = args[4]; 400be63f3b2Shoumkh let virt_optval = VirtAddr::new(optval as usize); 401ab5c8ca4Slogin // 验证optval的地址是否合法 402*b5b571e0SLoGin if verify_area(virt_optval, optlen).is_err() { 403ab5c8ca4Slogin // 地址空间超出了用户空间的范围,不合法 404ab5c8ca4Slogin Err(SystemError::EFAULT) 405ab5c8ca4Slogin } else { 406ab5c8ca4Slogin let data: &[u8] = unsafe { core::slice::from_raw_parts(optval, optlen) }; 407ab5c8ca4Slogin Self::setsockopt(args[0], args[1], args[2], data) 408ab5c8ca4Slogin } 409ab5c8ca4Slogin } 410ab5c8ca4Slogin SYS_GETSOCKOPT => { 411ab5c8ca4Slogin let optval = args[3] as *mut u8; 412ab5c8ca4Slogin let optlen = args[4] as *mut usize; 413be63f3b2Shoumkh let virt_optval = VirtAddr::new(optval as usize); 414be63f3b2Shoumkh let virt_optlen = VirtAddr::new(optlen as usize); 415ab5c8ca4Slogin let security_check = || { 416ab5c8ca4Slogin // 验证optval的地址是否合法 417be63f3b2Shoumkh if verify_area(virt_optval, PAGE_4K_SIZE as usize).is_err() { 418ab5c8ca4Slogin // 地址空间超出了用户空间的范围,不合法 419ab5c8ca4Slogin return Err(SystemError::EFAULT); 420ab5c8ca4Slogin } 421ab5c8ca4Slogin 422ab5c8ca4Slogin // 验证optlen的地址是否合法 423*b5b571e0SLoGin if verify_area(virt_optlen, core::mem::size_of::<u32>()).is_err() { 424ab5c8ca4Slogin // 地址空间超出了用户空间的范围,不合法 425ab5c8ca4Slogin return Err(SystemError::EFAULT); 426ab5c8ca4Slogin } 427ab5c8ca4Slogin return Ok(()); 428ab5c8ca4Slogin }; 429ab5c8ca4Slogin let r = security_check(); 430*b5b571e0SLoGin if let Err(e) = r { 431*b5b571e0SLoGin Err(e) 432ab5c8ca4Slogin } else { 433ab5c8ca4Slogin Self::getsockopt(args[0], args[1], args[2], optval, optlen as *mut u32) 434ab5c8ca4Slogin } 435ab5c8ca4Slogin } 436ab5c8ca4Slogin 437ab5c8ca4Slogin SYS_CONNECT => { 438ab5c8ca4Slogin let addr = args[1] as *const SockAddr; 439*b5b571e0SLoGin let addrlen = args[2]; 440be63f3b2Shoumkh let virt_addr = VirtAddr::new(addr as usize); 441ab5c8ca4Slogin // 验证addr的地址是否合法 442*b5b571e0SLoGin if verify_area(virt_addr, addrlen).is_err() { 443ab5c8ca4Slogin // 地址空间超出了用户空间的范围,不合法 444ab5c8ca4Slogin Err(SystemError::EFAULT) 445ab5c8ca4Slogin } else { 446ab5c8ca4Slogin Self::connect(args[0], addr, addrlen) 447ab5c8ca4Slogin } 448ab5c8ca4Slogin } 449ab5c8ca4Slogin SYS_BIND => { 450ab5c8ca4Slogin let addr = args[1] as *const SockAddr; 451*b5b571e0SLoGin let addrlen = args[2]; 452be63f3b2Shoumkh let virt_addr = VirtAddr::new(addr as usize); 453ab5c8ca4Slogin // 验证addr的地址是否合法 454*b5b571e0SLoGin if verify_area(virt_addr, addrlen).is_err() { 455ab5c8ca4Slogin // 地址空间超出了用户空间的范围,不合法 456ab5c8ca4Slogin Err(SystemError::EFAULT) 457ab5c8ca4Slogin } else { 458ab5c8ca4Slogin Self::bind(args[0], addr, addrlen) 459ab5c8ca4Slogin } 460ab5c8ca4Slogin } 461ab5c8ca4Slogin 462ab5c8ca4Slogin SYS_SENDTO => { 463ab5c8ca4Slogin let buf = args[1] as *const u8; 464*b5b571e0SLoGin let len = args[2]; 465ab5c8ca4Slogin let flags = args[3] as u32; 466ab5c8ca4Slogin let addr = args[4] as *const SockAddr; 467*b5b571e0SLoGin let addrlen = args[5]; 468be63f3b2Shoumkh let virt_buf = VirtAddr::new(buf as usize); 469be63f3b2Shoumkh let virt_addr = VirtAddr::new(addr as usize); 470ab5c8ca4Slogin // 验证buf的地址是否合法 471*b5b571e0SLoGin if verify_area(virt_buf, len).is_err() || verify_area(virt_addr, addrlen).is_err() { 472ab5c8ca4Slogin // 地址空间超出了用户空间的范围,不合法 473ab5c8ca4Slogin Err(SystemError::EFAULT) 474ab5c8ca4Slogin } else { 475ab5c8ca4Slogin let data: &[u8] = unsafe { core::slice::from_raw_parts(buf, len) }; 476ab5c8ca4Slogin Self::sendto(args[0], data, flags, addr, addrlen) 477ab5c8ca4Slogin } 478ab5c8ca4Slogin } 479ab5c8ca4Slogin 480ab5c8ca4Slogin SYS_RECVFROM => { 481ab5c8ca4Slogin let buf = args[1] as *mut u8; 482*b5b571e0SLoGin let len = args[2]; 483ab5c8ca4Slogin let flags = args[3] as u32; 484ab5c8ca4Slogin let addr = args[4] as *mut SockAddr; 485ab5c8ca4Slogin let addrlen = args[5] as *mut usize; 486be63f3b2Shoumkh let virt_buf = VirtAddr::new(buf as usize); 487be63f3b2Shoumkh let virt_addrlen = VirtAddr::new(addrlen as usize); 488be63f3b2Shoumkh let virt_addr = VirtAddr::new(addr as usize); 489ab5c8ca4Slogin let security_check = || { 490ab5c8ca4Slogin // 验证buf的地址是否合法 491840045afSLoGin if verify_area(virt_buf, len).is_err() { 492ab5c8ca4Slogin // 地址空间超出了用户空间的范围,不合法 493ab5c8ca4Slogin return Err(SystemError::EFAULT); 494ab5c8ca4Slogin } 495ab5c8ca4Slogin 496ab5c8ca4Slogin // 验证addrlen的地址是否合法 497840045afSLoGin if verify_area(virt_addrlen, core::mem::size_of::<u32>()).is_err() { 498ab5c8ca4Slogin // 地址空间超出了用户空间的范围,不合法 499ab5c8ca4Slogin return Err(SystemError::EFAULT); 500ab5c8ca4Slogin } 501ab5c8ca4Slogin 502840045afSLoGin if verify_area(virt_addr, core::mem::size_of::<SockAddr>()).is_err() { 503ab5c8ca4Slogin // 地址空间超出了用户空间的范围,不合法 504ab5c8ca4Slogin return Err(SystemError::EFAULT); 505ab5c8ca4Slogin } 506ab5c8ca4Slogin return Ok(()); 507ab5c8ca4Slogin }; 508ab5c8ca4Slogin let r = security_check(); 509*b5b571e0SLoGin if let Err(e) = r { 510*b5b571e0SLoGin Err(e) 511ab5c8ca4Slogin } else { 512ab5c8ca4Slogin let buf = unsafe { core::slice::from_raw_parts_mut(buf, len) }; 513ab5c8ca4Slogin Self::recvfrom(args[0], buf, flags, addr, addrlen as *mut u32) 514ab5c8ca4Slogin } 515ab5c8ca4Slogin } 516ab5c8ca4Slogin 517ab5c8ca4Slogin SYS_RECVMSG => { 5184ad52e57S裕依2439 let msg = args[1] as *mut MsgHdr; 519ab5c8ca4Slogin let flags = args[2] as u32; 5204ad52e57S裕依2439 521*b5b571e0SLoGin let mut user_buffer_writer = UserBufferWriter::new( 522*b5b571e0SLoGin msg, 523*b5b571e0SLoGin core::mem::size_of::<MsgHdr>(), 524*b5b571e0SLoGin frame.is_from_user(), 525*b5b571e0SLoGin )?; 5264ad52e57S裕依2439 let buffer = user_buffer_writer.buffer::<MsgHdr>(0)?; 5274ad52e57S裕依2439 5284537ffb7SChiichen let msg = &mut buffer[0]; 529ab5c8ca4Slogin Self::recvmsg(args[0], msg, flags) 530ab5c8ca4Slogin } 531ab5c8ca4Slogin 532ab5c8ca4Slogin SYS_LISTEN => Self::listen(args[0], args[1]), 533ab5c8ca4Slogin SYS_SHUTDOWN => Self::shutdown(args[0], args[1]), 534ab5c8ca4Slogin SYS_ACCEPT => Self::accept(args[0], args[1] as *mut SockAddr, args[2] as *mut u32), 535c47fe904SLoGin SYS_ACCEPT4 => Self::accept4( 536c47fe904SLoGin args[0], 537c47fe904SLoGin args[1] as *mut SockAddr, 538c47fe904SLoGin args[2] as *mut u32, 539c47fe904SLoGin args[3] as u32, 540c47fe904SLoGin ), 541ab5c8ca4Slogin SYS_GETSOCKNAME => { 542ab5c8ca4Slogin Self::getsockname(args[0], args[1] as *mut SockAddr, args[2] as *mut u32) 543ab5c8ca4Slogin } 544ab5c8ca4Slogin SYS_GETPEERNAME => { 545ab5c8ca4Slogin Self::getpeername(args[0], args[1] as *mut SockAddr, args[2] as *mut u32) 546ab5c8ca4Slogin } 54736fd0130Shoumkh SYS_GETTIMEOFDAY => { 54836fd0130Shoumkh let timeval = args[0] as *mut PosixTimeval; 54940fe15e0SLoGin let timezone_ptr = args[1] as *mut PosixTimeZone; 55040fe15e0SLoGin Self::gettimeofday(timeval, timezone_ptr) 5514537ffb7SChiichen } 55240fe15e0SLoGin SYS_MMAP => { 55340fe15e0SLoGin let len = page_align_up(args[1]); 554840045afSLoGin let virt_addr = VirtAddr::new(args[0]); 555840045afSLoGin if verify_area(virt_addr, len).is_err() { 55640fe15e0SLoGin Err(SystemError::EFAULT) 55740fe15e0SLoGin } else { 55840fe15e0SLoGin Self::mmap( 55940fe15e0SLoGin VirtAddr::new(args[0]), 56040fe15e0SLoGin len, 56140fe15e0SLoGin args[2], 56240fe15e0SLoGin args[3], 56340fe15e0SLoGin args[4] as i32, 56440fe15e0SLoGin args[5], 56540fe15e0SLoGin ) 56640fe15e0SLoGin } 56740fe15e0SLoGin } 5684cfa009bSJomo SYS_MREMAP => { 5694cfa009bSJomo let old_vaddr = VirtAddr::new(args[0]); 5704cfa009bSJomo let old_len = args[1]; 5714cfa009bSJomo let new_len = args[2]; 5724cfa009bSJomo let mremap_flags = MremapFlags::from_bits_truncate(args[3] as u8); 5734cfa009bSJomo let new_vaddr = VirtAddr::new(args[4]); 5744cfa009bSJomo 5754cfa009bSJomo Self::mremap(old_vaddr, old_len, new_len, mremap_flags, new_vaddr) 5764cfa009bSJomo } 57740fe15e0SLoGin SYS_MUNMAP => { 57840fe15e0SLoGin let addr = args[0]; 57940fe15e0SLoGin let len = page_align_up(args[1]); 580971462beSGnoCiYeH if addr & (MMArch::PAGE_SIZE - 1) != 0 { 58140fe15e0SLoGin // The addr argument is not a multiple of the page size 58240fe15e0SLoGin Err(SystemError::EINVAL) 58340fe15e0SLoGin } else { 58440fe15e0SLoGin Self::munmap(VirtAddr::new(addr), len) 58540fe15e0SLoGin } 58640fe15e0SLoGin } 58740fe15e0SLoGin SYS_MPROTECT => { 58840fe15e0SLoGin let addr = args[0]; 58940fe15e0SLoGin let len = page_align_up(args[1]); 590971462beSGnoCiYeH if addr & (MMArch::PAGE_SIZE - 1) != 0 { 59140fe15e0SLoGin // The addr argument is not a multiple of the page size 59240fe15e0SLoGin Err(SystemError::EINVAL) 59340fe15e0SLoGin } else { 59440fe15e0SLoGin Self::mprotect(VirtAddr::new(addr), len, args[2]) 59540fe15e0SLoGin } 59640fe15e0SLoGin } 59740fe15e0SLoGin 5981496ba7bSLoGin SYS_GETCWD => { 5991496ba7bSLoGin let buf = args[0] as *mut u8; 600840045afSLoGin let size = args[1]; 6011496ba7bSLoGin let security_check = || { 6021496ba7bSLoGin verify_area(VirtAddr::new(buf as usize), size)?; 6031496ba7bSLoGin return Ok(()); 6041496ba7bSLoGin }; 6051496ba7bSLoGin let r = security_check(); 606*b5b571e0SLoGin if let Err(e) = r { 607*b5b571e0SLoGin Err(e) 6081496ba7bSLoGin } else { 6091496ba7bSLoGin let buf = unsafe { core::slice::from_raw_parts_mut(buf, size) }; 6101496ba7bSLoGin Self::getcwd(buf).map(|ptr| ptr.data()) 6111496ba7bSLoGin } 6121496ba7bSLoGin } 6131496ba7bSLoGin 6141496ba7bSLoGin SYS_GETPGID => Self::getpgid(Pid::new(args[0])).map(|pid| pid.into()), 6151496ba7bSLoGin 6161496ba7bSLoGin SYS_GETPPID => Self::getppid().map(|pid| pid.into()), 617be63f3b2Shoumkh SYS_FSTAT => { 618be63f3b2Shoumkh let fd = args[0] as i32; 619*b5b571e0SLoGin let kstat: *mut PosixKstat = args[1] as *mut PosixKstat; 620be63f3b2Shoumkh let vaddr = VirtAddr::new(kstat as usize); 621be63f3b2Shoumkh // FIXME 由于c中的verify_area与rust中的verify_area重名,所以在引入时加了前缀区分 622be63f3b2Shoumkh // TODO 应该将用了c版本的verify_area都改为rust的verify_area 623be63f3b2Shoumkh match verify_area(vaddr, core::mem::size_of::<PosixKstat>()) { 624be63f3b2Shoumkh Ok(_) => Self::fstat(fd, kstat), 625be63f3b2Shoumkh Err(e) => Err(e), 626be63f3b2Shoumkh } 627be63f3b2Shoumkh } 628be63f3b2Shoumkh 6296d81180bSLoGin SYS_FCNTL => { 6306d81180bSLoGin let fd = args[0] as i32; 6316d81180bSLoGin let cmd: Option<FcntlCommand> = 6326d81180bSLoGin <FcntlCommand as FromPrimitive>::from_u32(args[1] as u32); 6336d81180bSLoGin let arg = args[2] as i32; 6346d81180bSLoGin let res = if let Some(cmd) = cmd { 6356d81180bSLoGin Self::fcntl(fd, cmd, arg) 6366d81180bSLoGin } else { 6376d81180bSLoGin Err(SystemError::EINVAL) 6386d81180bSLoGin }; 6396d81180bSLoGin 6406d81180bSLoGin // kdebug!("FCNTL: fd: {}, cmd: {:?}, arg: {}, res: {:?}", fd, cmd, arg, res); 6416d81180bSLoGin res 6426d81180bSLoGin } 6436d81180bSLoGin 6446d81180bSLoGin SYS_FTRUNCATE => { 6456d81180bSLoGin let fd = args[0] as i32; 646840045afSLoGin let len = args[1]; 6476d81180bSLoGin let res = Self::ftruncate(fd, len); 6486d81180bSLoGin // kdebug!("FTRUNCATE: fd: {}, len: {}, res: {:?}", fd, len, res); 6496d81180bSLoGin res 6506d81180bSLoGin } 6516d81180bSLoGin 6524fda81ceSLoGin #[cfg(target_arch = "x86_64")] 6532dbef785SGnoCiYeH SYS_MKNOD => { 65470f159a3SLoGin use crate::driver::base::device::device_number::DeviceNumber; 65570f159a3SLoGin 6562dbef785SGnoCiYeH let path = args[0]; 6572dbef785SGnoCiYeH let flags = args[1]; 6582dbef785SGnoCiYeH let dev_t = args[2]; 6592dbef785SGnoCiYeH let flags: ModeType = ModeType::from_bits_truncate(flags as u32); 66082df0a13Shmt Self::mknod(path as *const u8, flags, DeviceNumber::from(dev_t as u32)) 6612dbef785SGnoCiYeH } 6622dbef785SGnoCiYeH 663971462beSGnoCiYeH SYS_CLONE => { 664971462beSGnoCiYeH let parent_tid = VirtAddr::new(args[2]); 665971462beSGnoCiYeH let child_tid = VirtAddr::new(args[3]); 666971462beSGnoCiYeH 667971462beSGnoCiYeH // 地址校验 668971462beSGnoCiYeH verify_area(parent_tid, core::mem::size_of::<i32>())?; 669971462beSGnoCiYeH verify_area(child_tid, core::mem::size_of::<i32>())?; 670971462beSGnoCiYeH 671971462beSGnoCiYeH let mut clone_args = KernelCloneArgs::new(); 672971462beSGnoCiYeH clone_args.flags = CloneFlags::from_bits_truncate(args[0] as u64); 673971462beSGnoCiYeH clone_args.stack = args[1]; 674971462beSGnoCiYeH clone_args.parent_tid = parent_tid; 675971462beSGnoCiYeH clone_args.child_tid = child_tid; 676971462beSGnoCiYeH clone_args.tls = args[4]; 677971462beSGnoCiYeH Self::clone(frame, clone_args) 678971462beSGnoCiYeH } 679971462beSGnoCiYeH 680971462beSGnoCiYeH SYS_FUTEX => { 681971462beSGnoCiYeH let uaddr = VirtAddr::new(args[0]); 682971462beSGnoCiYeH let operation = FutexFlag::from_bits(args[1] as u32).ok_or(SystemError::ENOSYS)?; 683971462beSGnoCiYeH let val = args[2] as u32; 684971462beSGnoCiYeH let utime = args[3]; 685971462beSGnoCiYeH let uaddr2 = VirtAddr::new(args[4]); 686971462beSGnoCiYeH let val3 = args[5] as u32; 687971462beSGnoCiYeH 688971462beSGnoCiYeH verify_area(uaddr, core::mem::size_of::<u32>())?; 689971462beSGnoCiYeH verify_area(uaddr2, core::mem::size_of::<u32>())?; 690971462beSGnoCiYeH 691971462beSGnoCiYeH let mut timespec = None; 692971462beSGnoCiYeH if utime != 0 && operation.contains(FutexFlag::FLAGS_HAS_TIMEOUT) { 693971462beSGnoCiYeH let reader = UserBufferReader::new( 694971462beSGnoCiYeH utime as *const TimeSpec, 695971462beSGnoCiYeH core::mem::size_of::<TimeSpec>(), 696971462beSGnoCiYeH true, 697971462beSGnoCiYeH )?; 698971462beSGnoCiYeH 699840045afSLoGin timespec = Some(*reader.read_one_from_user::<TimeSpec>(0)?); 700971462beSGnoCiYeH } 701971462beSGnoCiYeH 702971462beSGnoCiYeH Self::do_futex(uaddr, operation, val, timespec, uaddr2, utime as u32, val3) 703971462beSGnoCiYeH } 704971462beSGnoCiYeH 705bf4a4899SLoGin SYS_READV => Self::readv(args[0] as i32, args[1], args[2]), 706971462beSGnoCiYeH SYS_WRITEV => Self::writev(args[0] as i32, args[1], args[2]), 707971462beSGnoCiYeH 7084fda81ceSLoGin SYS_SET_TID_ADDRESS => Self::set_tid_address(args[0]), 709971462beSGnoCiYeH 7104fda81ceSLoGin #[cfg(target_arch = "x86_64")] 7114fda81ceSLoGin SYS_LSTAT => { 71282df0a13Shmt let path = args[0] as *const u8; 713971462beSGnoCiYeH let kstat = args[1] as *mut PosixKstat; 71482df0a13Shmt Self::lstat(path, kstat) 7154fda81ceSLoGin } 7164fda81ceSLoGin 7174fda81ceSLoGin #[cfg(target_arch = "x86_64")] 7184fda81ceSLoGin SYS_STAT => { 71982df0a13Shmt let path = args[0] as *const u8; 7204fda81ceSLoGin let kstat = args[1] as *mut PosixKstat; 72182df0a13Shmt Self::stat(path, kstat) 722971462beSGnoCiYeH } 723971462beSGnoCiYeH 724b4eb05a1STTaq SYS_STATX => { 725b4eb05a1STTaq let fd = args[0] as i32; 726b4eb05a1STTaq let path = args[1] as *const u8; 727b4eb05a1STTaq let flags = args[2] as u32; 728b4eb05a1STTaq let mask = args[3] as u32; 729b4eb05a1STTaq let kstat = args[4] as *mut PosixStatx; 730b4eb05a1STTaq 731b4eb05a1STTaq Self::do_statx(fd, path, flags, mask, kstat) 732b4eb05a1STTaq } 733b4eb05a1STTaq 73470f159a3SLoGin #[cfg(target_arch = "x86_64")] 73540609970SGnoCiYeH SYS_EPOLL_CREATE => Self::epoll_create(args[0] as i32), 73640609970SGnoCiYeH SYS_EPOLL_CREATE1 => Self::epoll_create1(args[0]), 73740609970SGnoCiYeH 73840609970SGnoCiYeH SYS_EPOLL_CTL => Self::epoll_ctl( 73940609970SGnoCiYeH args[0] as i32, 74040609970SGnoCiYeH args[1], 74140609970SGnoCiYeH args[2] as i32, 74240609970SGnoCiYeH VirtAddr::new(args[3]), 74340609970SGnoCiYeH ), 74440609970SGnoCiYeH 74570f159a3SLoGin #[cfg(target_arch = "x86_64")] 74640609970SGnoCiYeH SYS_EPOLL_WAIT => Self::epoll_wait( 74740609970SGnoCiYeH args[0] as i32, 74840609970SGnoCiYeH VirtAddr::new(args[1]), 74940609970SGnoCiYeH args[2] as i32, 75040609970SGnoCiYeH args[3] as i32, 75140609970SGnoCiYeH ), 75240609970SGnoCiYeH 75340609970SGnoCiYeH SYS_EPOLL_PWAIT => { 75440609970SGnoCiYeH let epfd = args[0] as i32; 75540609970SGnoCiYeH let epoll_event = VirtAddr::new(args[1]); 75640609970SGnoCiYeH let max_events = args[2] as i32; 75740609970SGnoCiYeH let timespec = args[3] as i32; 75840609970SGnoCiYeH let sigmask_addr = args[4] as *mut SigSet; 75940609970SGnoCiYeH 76040609970SGnoCiYeH if sigmask_addr.is_null() { 76140609970SGnoCiYeH return Self::epoll_wait(epfd, epoll_event, max_events, timespec); 76240609970SGnoCiYeH } 76340609970SGnoCiYeH let sigmask_reader = 76440609970SGnoCiYeH UserBufferReader::new(sigmask_addr, core::mem::size_of::<SigSet>(), true)?; 765840045afSLoGin let mut sigmask = *sigmask_reader.read_one_from_user::<SigSet>(0)?; 76640609970SGnoCiYeH 76740609970SGnoCiYeH Self::epoll_pwait( 76840609970SGnoCiYeH args[0] as i32, 76940609970SGnoCiYeH VirtAddr::new(args[1]), 77040609970SGnoCiYeH args[2] as i32, 77140609970SGnoCiYeH args[3] as i32, 77240609970SGnoCiYeH &mut sigmask, 77340609970SGnoCiYeH ) 77440609970SGnoCiYeH } 77540609970SGnoCiYeH 776971462beSGnoCiYeH // 目前为了适配musl-libc,以下系统调用先这样写着 7774fda81ceSLoGin SYS_GETRANDOM => { 778971462beSGnoCiYeH let flags = GRandFlags::from_bits(args[2] as u8).ok_or(SystemError::EINVAL)?; 779971462beSGnoCiYeH Self::get_random(args[0] as *mut u8, args[1], flags) 780971462beSGnoCiYeH } 781971462beSGnoCiYeH 7824fda81ceSLoGin SYS_SOCKETPAIR => { 7834ad52e57S裕依2439 let mut user_buffer_writer = UserBufferWriter::new( 7844ad52e57S裕依2439 args[3] as *mut c_int, 7854ad52e57S裕依2439 core::mem::size_of::<[c_int; 2]>(), 786*b5b571e0SLoGin frame.is_from_user(), 7874ad52e57S裕依2439 )?; 7884ad52e57S裕依2439 let fds = user_buffer_writer.buffer::<i32>(0)?; 7894ad52e57S裕依2439 Self::socketpair(args[0], args[1], args[2], fds) 790971462beSGnoCiYeH } 791971462beSGnoCiYeH 7924fda81ceSLoGin #[cfg(target_arch = "x86_64")] 793971462beSGnoCiYeH SYS_POLL => { 794971462beSGnoCiYeH kwarn!("SYS_POLL has not yet been implemented"); 795971462beSGnoCiYeH Ok(0) 796971462beSGnoCiYeH } 797971462beSGnoCiYeH 79852da9a59SGnoCiYeH SYS_SETPGID => { 79952da9a59SGnoCiYeH kwarn!("SYS_SETPGID has not yet been implemented"); 80052da9a59SGnoCiYeH Ok(0) 80152da9a59SGnoCiYeH } 80252da9a59SGnoCiYeH 803971462beSGnoCiYeH SYS_RT_SIGPROCMASK => { 804971462beSGnoCiYeH kwarn!("SYS_RT_SIGPROCMASK has not yet been implemented"); 805971462beSGnoCiYeH Ok(0) 806971462beSGnoCiYeH } 807971462beSGnoCiYeH 808971462beSGnoCiYeH SYS_TKILL => { 809971462beSGnoCiYeH kwarn!("SYS_TKILL has not yet been implemented"); 810971462beSGnoCiYeH Ok(0) 811971462beSGnoCiYeH } 812971462beSGnoCiYeH 813971462beSGnoCiYeH SYS_SIGALTSTACK => { 814971462beSGnoCiYeH kwarn!("SYS_SIGALTSTACK has not yet been implemented"); 815971462beSGnoCiYeH Ok(0) 816971462beSGnoCiYeH } 817971462beSGnoCiYeH 81816033951SGnoCiYeH SYS_EXIT_GROUP => { 81916033951SGnoCiYeH kwarn!("SYS_EXIT_GROUP has not yet been implemented"); 82016033951SGnoCiYeH Ok(0) 82116033951SGnoCiYeH } 82216033951SGnoCiYeH 82304babc3fSMemoryShore SYS_MADVISE => { 8246c7f966cSMemoryShore // 这个太吵了,总是打印,先注释掉 8256c7f966cSMemoryShore // kwarn!("SYS_MADVISE has not yet been implemented"); 82604babc3fSMemoryShore Ok(0) 82704babc3fSMemoryShore } 828393f6915SLoGin SYS_GETTID => Self::gettid().map(|tid| tid.into()), 829840045afSLoGin SYS_GETUID => Self::getuid(), 8308d72b68dSJomo 83102e249f3SLoGin SYS_SYSLOG => { 832840045afSLoGin let syslog_action_type = args[0]; 8338d72b68dSJomo let buf_vaddr = args[1]; 8348d72b68dSJomo let len = args[2]; 835*b5b571e0SLoGin let from_user = frame.is_from_user(); 8368d72b68dSJomo let mut user_buffer_writer = 8378d72b68dSJomo UserBufferWriter::new(buf_vaddr as *mut u8, len, from_user)?; 8388d72b68dSJomo 8398d72b68dSJomo let user_buf = user_buffer_writer.buffer(0)?; 840840045afSLoGin Self::do_syslog(syslog_action_type, user_buf, len) 84102e249f3SLoGin } 8428d72b68dSJomo 843840045afSLoGin SYS_GETGID => Self::getgid(), 84402e249f3SLoGin SYS_SETUID => { 84502e249f3SLoGin kwarn!("SYS_SETUID has not yet been implemented"); 84602e249f3SLoGin Ok(0) 84702e249f3SLoGin } 84802e249f3SLoGin SYS_SETGID => { 84902e249f3SLoGin kwarn!("SYS_SETGID has not yet been implemented"); 85002e249f3SLoGin Ok(0) 85102e249f3SLoGin } 852840045afSLoGin SYS_GETEUID => Self::geteuid(), 853840045afSLoGin SYS_GETEGID => Self::getegid(), 854be8cdf4bSLoGin SYS_GETRUSAGE => { 855be8cdf4bSLoGin let who = args[0] as c_int; 856be8cdf4bSLoGin let rusage = args[1] as *mut RUsage; 857be8cdf4bSLoGin Self::get_rusage(who, rusage) 858be8cdf4bSLoGin } 85904babc3fSMemoryShore 8604fda81ceSLoGin #[cfg(target_arch = "x86_64")] 861709498caSLoGin SYS_READLINK => { 862709498caSLoGin let path = args[0] as *const u8; 863709498caSLoGin let buf = args[1] as *mut u8; 864840045afSLoGin let bufsiz = args[2]; 865709498caSLoGin Self::readlink(path, buf, bufsiz) 866709498caSLoGin } 867709498caSLoGin 8684fda81ceSLoGin SYS_READLINKAT => { 869709498caSLoGin let dirfd = args[0] as i32; 87082df0a13Shmt let path = args[1] as *const u8; 871709498caSLoGin let buf = args[2] as *mut u8; 872840045afSLoGin let bufsiz = args[3]; 87382df0a13Shmt Self::readlink_at(dirfd, path, buf, bufsiz) 874709498caSLoGin } 875709498caSLoGin 8760d9b7d92SLoGin SYS_PRLIMIT64 => { 8770d9b7d92SLoGin let pid = args[0]; 8780d9b7d92SLoGin let pid = Pid::new(pid); 8790d9b7d92SLoGin let resource = args[1]; 8800d9b7d92SLoGin let new_limit = args[2] as *const RLimit64; 8810d9b7d92SLoGin let old_limit = args[3] as *mut RLimit64; 8820d9b7d92SLoGin 8830d9b7d92SLoGin Self::prlimit64(pid, resource, new_limit, old_limit) 8840d9b7d92SLoGin } 8850d9b7d92SLoGin 8864fda81ceSLoGin #[cfg(target_arch = "x86_64")] 8879b0abe6dSLoGin SYS_ACCESS => { 8889b0abe6dSLoGin let pathname = args[0] as *const u8; 8899b0abe6dSLoGin let mode = args[1] as u32; 8909b0abe6dSLoGin Self::access(pathname, mode) 8919b0abe6dSLoGin } 8929b0abe6dSLoGin 8939b0abe6dSLoGin SYS_FACCESSAT => { 8949b0abe6dSLoGin let dirfd = args[0] as i32; 8959b0abe6dSLoGin let pathname = args[1] as *const u8; 8969b0abe6dSLoGin let mode = args[2] as u32; 8979b0abe6dSLoGin Self::faccessat2(dirfd, pathname, mode, 0) 8989b0abe6dSLoGin } 8999b0abe6dSLoGin 9009b0abe6dSLoGin SYS_FACCESSAT2 => { 9019b0abe6dSLoGin let dirfd = args[0] as i32; 9029b0abe6dSLoGin let pathname = args[1] as *const u8; 9039b0abe6dSLoGin let mode = args[2] as u32; 9049b0abe6dSLoGin let flags = args[3] as u32; 9059b0abe6dSLoGin Self::faccessat2(dirfd, pathname, mode, flags) 9069b0abe6dSLoGin } 9079b0abe6dSLoGin 908bf4a4899SLoGin SYS_CLOCK_GETTIME => { 909bf4a4899SLoGin let clockid = args[0] as i32; 910bf4a4899SLoGin let timespec = args[1] as *mut TimeSpec; 911bf4a4899SLoGin Self::clock_gettime(clockid, timespec) 912bf4a4899SLoGin } 913bf4a4899SLoGin 914bf4a4899SLoGin SYS_SYSINFO => { 915bf4a4899SLoGin let info = args[0] as *mut SysInfo; 916bf4a4899SLoGin Self::sysinfo(info) 917bf4a4899SLoGin } 918bf4a4899SLoGin 919bf4a4899SLoGin SYS_UMASK => { 920bf4a4899SLoGin let mask = args[0] as u32; 921bf4a4899SLoGin Self::umask(mask) 922bf4a4899SLoGin } 923bf4a4899SLoGin 92427b967a3S裕依 SYS_FCHOWN => { 92527b967a3S裕依 kwarn!("SYS_FCHOWN has not yet been implemented"); 92627b967a3S裕依 Ok(0) 92727b967a3S裕依 } 92827b967a3S裕依 92927b967a3S裕依 SYS_FSYNC => { 93027b967a3S裕依 kwarn!("SYS_FSYNC has not yet been implemented"); 93127b967a3S裕依 Ok(0) 93227b967a3S裕依 } 93327b967a3S裕依 9344fda81ceSLoGin #[cfg(target_arch = "x86_64")] 935bf4a4899SLoGin SYS_CHMOD => { 936bf4a4899SLoGin let pathname = args[0] as *const u8; 937bf4a4899SLoGin let mode = args[1] as u32; 938bf4a4899SLoGin Self::chmod(pathname, mode) 939bf4a4899SLoGin } 940bf4a4899SLoGin SYS_FCHMOD => { 941bf4a4899SLoGin let fd = args[0] as i32; 942bf4a4899SLoGin let mode = args[1] as u32; 943bf4a4899SLoGin Self::fchmod(fd, mode) 944bf4a4899SLoGin } 945bf4a4899SLoGin SYS_FCHMODAT => { 946bf4a4899SLoGin let dirfd = args[0] as i32; 947bf4a4899SLoGin let pathname = args[1] as *const u8; 948bf4a4899SLoGin let mode = args[2] as u32; 949bf4a4899SLoGin Self::fchmodat(dirfd, pathname, mode) 950bf4a4899SLoGin } 951bf4a4899SLoGin 95202343d0bSLoGin SYS_SCHED_GETAFFINITY => { 95302343d0bSLoGin // todo: 这个系统调用还没有实现 95402343d0bSLoGin 95502343d0bSLoGin Err(SystemError::ENOSYS) 95602343d0bSLoGin } 95702343d0bSLoGin 95852da9a59SGnoCiYeH #[cfg(target_arch = "x86_64")] 95952da9a59SGnoCiYeH SYS_GETRLIMIT => { 96052da9a59SGnoCiYeH let resource = args[0]; 96152da9a59SGnoCiYeH let rlimit = args[1] as *mut RLimit64; 96252da9a59SGnoCiYeH 96352da9a59SGnoCiYeH Self::prlimit64( 96452da9a59SGnoCiYeH ProcessManager::current_pcb().pid(), 96552da9a59SGnoCiYeH resource, 966840045afSLoGin core::ptr::null::<RLimit64>(), 96752da9a59SGnoCiYeH rlimit, 96852da9a59SGnoCiYeH ) 96952da9a59SGnoCiYeH } 97052da9a59SGnoCiYeH 9719e481b3bSTTaq SYS_FADVISE64 => { 9729e481b3bSTTaq // todo: 这个系统调用还没有实现 9739e481b3bSTTaq 9749e481b3bSTTaq Err(SystemError::ENOSYS) 9759e481b3bSTTaq } 9761d37ca6dSDonkey Kane 9771d37ca6dSDonkey Kane SYS_MOUNT => { 9781d37ca6dSDonkey Kane let source = args[0] as *const u8; 9791d37ca6dSDonkey Kane let target = args[1] as *const u8; 9801d37ca6dSDonkey Kane let filesystemtype = args[2] as *const u8; 9811d37ca6dSDonkey Kane return Self::mount(source, target, filesystemtype, 0, null()); 9821d37ca6dSDonkey Kane } 9839e481b3bSTTaq SYS_NEWFSTATAT => { 9849e481b3bSTTaq // todo: 这个系统调用还没有实现 9859e481b3bSTTaq 9869e481b3bSTTaq Err(SystemError::ENOSYS) 9879e481b3bSTTaq } 9889e481b3bSTTaq 98902343d0bSLoGin SYS_SCHED_YIELD => Self::sched_yield(), 9908c6f2184Ssspphh SYS_UNAME => { 9918c6f2184Ssspphh let name = args[0] as *mut PosixOldUtsName; 9928c6f2184Ssspphh Self::uname(name) 9938c6f2184Ssspphh } 9941d37ca6dSDonkey Kane 995ab5c8ca4Slogin _ => panic!("Unsupported syscall ID: {}", syscall_num), 996ab5c8ca4Slogin }; 9974fda81ceSLoGin 998ab5c8ca4Slogin return r; 999ab5c8ca4Slogin } 1000ab5c8ca4Slogin 1001ab5c8ca4Slogin pub fn put_string( 1002ab5c8ca4Slogin s: *const u8, 1003ab5c8ca4Slogin front_color: u32, 1004ab5c8ca4Slogin back_color: u32, 1005ab5c8ca4Slogin ) -> Result<usize, SystemError> { 1006c3dc6f2fSLoGin // todo: 删除这个系统调用 1007c3dc6f2fSLoGin let s = check_and_clone_cstr(s, Some(4096))?; 1008c3dc6f2fSLoGin let fr = (front_color & 0x00ff0000) >> 16; 1009c3dc6f2fSLoGin let fg = (front_color & 0x0000ff00) >> 8; 1010c3dc6f2fSLoGin let fb = front_color & 0x000000ff; 1011c3dc6f2fSLoGin let br = (back_color & 0x00ff0000) >> 16; 1012c3dc6f2fSLoGin let bg = (back_color & 0x0000ff00) >> 8; 1013c3dc6f2fSLoGin let bb = back_color & 0x000000ff; 1014c3dc6f2fSLoGin print!("\x1B[38;2;{fr};{fg};{fb};48;2;{br};{bg};{bb}m{s}\x1B[0m"); 1015c3dc6f2fSLoGin return Ok(s.len()); 1016ab5c8ca4Slogin } 1017ab5c8ca4Slogin 1018ab5c8ca4Slogin pub fn reboot() -> Result<usize, SystemError> { 10194fda81ceSLoGin unsafe { cpu_reset() }; 1020ab5c8ca4Slogin } 1021ab5c8ca4Slogin } 1022