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::*}, 9597ecc08STTaq filesystem::vfs::syscall::{PosixStatfs, PosixStatx}, 106fc066acSJomo ipc::shm::{ShmCtlCmd, ShmFlags, ShmId, ShmKey}, 11971462beSGnoCiYeH libs::{futex::constant::FutexFlag, rand::GRandFlags}, 12370472f7SLoGin mm::{page::PAGE_4K_SIZE, syscall::MremapFlags}, 134ad52e57S裕依2439 net::syscall::MsgHdr, 140d9b7d92SLoGin process::{ 150d9b7d92SLoGin fork::KernelCloneArgs, 160d9b7d92SLoGin resource::{RLimit64, RUsage}, 17f0c87a89SGnoCiYeH ProcessFlags, ProcessManager, 180d9b7d92SLoGin }, 19f0c87a89SGnoCiYeH sched::{schedule, SchedMode}, 20c3dc6f2fSLoGin syscall::user_access::check_and_clone_cstr, 21971462beSGnoCiYeH }; 2240314b30SXiaoye Zheng 232eab6dd7S曾俊 use log::{info, warn}; 2491e9d4abSLoGin use num_traits::FromPrimitive; 2591e9d4abSLoGin use system_error::SystemError; 26676b8ef6SMork 27ab5c8ca4Slogin use crate::{ 281496ba7bSLoGin arch::{cpu::cpu_reset, interrupt::TrapFrame, MMArch}, 29ab5c8ca4Slogin filesystem::vfs::{ 309e481b3bSTTaq fcntl::{AtFlags, FcntlCommand}, 31ab5c8ca4Slogin file::FileMode, 32*6f189d27Slinfeng syscall::{ModeType, PosixKstat, UtimensFlags}, 33ab5c8ca4Slogin MAX_PATHLEN, 34ab5c8ca4Slogin }, 3540fe15e0SLoGin libs::align::page_align_up, 36be63f3b2Shoumkh mm::{verify_area, MemoryManagementArch, VirtAddr}, 37ab5c8ca4Slogin net::syscall::SockAddr, 388c6f2184Ssspphh process::{fork::CloneFlags, syscall::PosixOldUtsName, Pid}, 3936fd0130Shoumkh time::{ 4040fe15e0SLoGin syscall::{PosixTimeZone, PosixTimeval}, 416fc066acSJomo PosixTimeSpec, 4236fd0130Shoumkh }, 43ab5c8ca4Slogin }; 44ab5c8ca4Slogin 45bf4a4899SLoGin use self::{ 46bf4a4899SLoGin misc::SysInfo, 47bf4a4899SLoGin user_access::{UserBufferReader, UserBufferWriter}, 48bf4a4899SLoGin }; 494537ffb7SChiichen 50bf4a4899SLoGin pub mod misc; 5140fe15e0SLoGin pub mod user_access; 5240fe15e0SLoGin 53ad1d649eSGnoCiYeH // 与linux不一致的调用,在linux基础上累加 54ad1d649eSGnoCiYeH pub const SYS_PUT_STRING: usize = 100000; 55ad1d649eSGnoCiYeH pub const SYS_SBRK: usize = 100001; 56ab5c8ca4Slogin /// todo: 该系统调用与Linux不一致,将来需要删除该系统调用!!! 删的时候记得改C版本的libc 57ad1d649eSGnoCiYeH pub const SYS_CLOCK: usize = 100002; 58ad1d649eSGnoCiYeH pub const SYS_SCHED: usize = 100003; 5940314b30SXiaoye Zheng 60ab5c8ca4Slogin #[derive(Debug)] 61ab5c8ca4Slogin pub struct Syscall; 62ab5c8ca4Slogin 63ab5c8ca4Slogin impl Syscall { 64ab5c8ca4Slogin /// 初始化系统调用 655b59005fSLoGin #[inline(never)] 66ab5c8ca4Slogin pub fn init() -> Result<(), SystemError> { 67ab5c8ca4Slogin static INIT_FLAG: AtomicBool = AtomicBool::new(false); 68ab5c8ca4Slogin let prev = INIT_FLAG.swap(true, Ordering::SeqCst); 69ab5c8ca4Slogin if prev { 70ab5c8ca4Slogin panic!("Cannot initialize syscall more than once!"); 71ab5c8ca4Slogin } 722eab6dd7S曾俊 info!("Initializing syscall..."); 735b59005fSLoGin let r = crate::arch::syscall::arch_syscall_init(); 742eab6dd7S曾俊 info!("Syscall init successfully!"); 755b59005fSLoGin 765b59005fSLoGin return r; 77ab5c8ca4Slogin } 78ab5c8ca4Slogin /// @brief 系统调用分发器,用于分发系统调用。 79ab5c8ca4Slogin /// 80ab5c8ca4Slogin /// 这个函数内,需要根据系统调用号,调用对应的系统调用处理函数。 81ab5c8ca4Slogin /// 并且,对于用户态传入的指针参数,需要在本函数内进行越界检查,防止访问到内核空间。 824fda81ceSLoGin #[inline(never)] 839e9ffedfSPlucky923 pub fn handle( 849e9ffedfSPlucky923 syscall_num: usize, 859e9ffedfSPlucky923 args: &[usize], 869e9ffedfSPlucky923 frame: &mut TrapFrame, 879e9ffedfSPlucky923 ) -> Result<usize, SystemError> { 88ab5c8ca4Slogin let r = match syscall_num { 89ab5c8ca4Slogin SYS_PUT_STRING => { 90ab5c8ca4Slogin Self::put_string(args[0] as *const u8, args[1] as u32, args[2] as u32) 91ab5c8ca4Slogin } 924fda81ceSLoGin #[cfg(target_arch = "x86_64")] 93ab5c8ca4Slogin SYS_OPEN => { 9482df0a13Shmt let path = args[0] as *const u8; 9582df0a13Shmt let flags = args[1] as u32; 9682df0a13Shmt let mode = args[2] as u32; 9734e6d6c8Syuyi2439 9882df0a13Shmt Self::open(path, flags, mode, true) 990fb515b0SLoGin } 1000fb515b0SLoGin 10170f159a3SLoGin #[cfg(target_arch = "x86_64")] 1029e481b3bSTTaq SYS_RENAME => { 1039e481b3bSTTaq let oldname: *const u8 = args[0] as *const u8; 1049e481b3bSTTaq let newname: *const u8 = args[1] as *const u8; 1059e481b3bSTTaq Self::do_renameat2( 1069e481b3bSTTaq AtFlags::AT_FDCWD.bits(), 1079e481b3bSTTaq oldname, 1089e481b3bSTTaq AtFlags::AT_FDCWD.bits(), 1099e481b3bSTTaq newname, 1109e481b3bSTTaq 0, 1119e481b3bSTTaq ) 1129e481b3bSTTaq } 1139e481b3bSTTaq 11470f159a3SLoGin #[cfg(target_arch = "x86_64")] 1159e481b3bSTTaq SYS_RENAMEAT => { 1169e481b3bSTTaq let oldfd = args[0] as i32; 1179e481b3bSTTaq let oldname: *const u8 = args[1] as *const u8; 1189e481b3bSTTaq let newfd = args[2] as i32; 1199e481b3bSTTaq let newname: *const u8 = args[3] as *const u8; 1209e481b3bSTTaq Self::do_renameat2(oldfd, oldname, newfd, newname, 0) 1219e481b3bSTTaq } 1229e481b3bSTTaq 1239e481b3bSTTaq SYS_RENAMEAT2 => { 1249e481b3bSTTaq let oldfd = args[0] as i32; 1259e481b3bSTTaq let oldname: *const u8 = args[1] as *const u8; 1269e481b3bSTTaq let newfd = args[2] as i32; 1279e481b3bSTTaq let newname: *const u8 = args[3] as *const u8; 1289e481b3bSTTaq let flags = args[4] as u32; 1299e481b3bSTTaq Self::do_renameat2(oldfd, oldname, newfd, newname, flags) 1309e481b3bSTTaq } 1319e481b3bSTTaq 1320fb515b0SLoGin SYS_OPENAT => { 1330fb515b0SLoGin let dirfd = args[0] as i32; 13482df0a13Shmt let path = args[1] as *const u8; 13582df0a13Shmt let flags = args[2] as u32; 13682df0a13Shmt let mode = args[3] as u32; 1370fb515b0SLoGin 13882df0a13Shmt Self::openat(dirfd, path, flags, mode, true) 139ab5c8ca4Slogin } 140ab5c8ca4Slogin SYS_CLOSE => { 141ab5c8ca4Slogin let fd = args[0]; 14227b967a3S裕依 Self::close(fd) 143ab5c8ca4Slogin } 144ab5c8ca4Slogin SYS_READ => { 145ab5c8ca4Slogin let fd = args[0] as i32; 146ab5c8ca4Slogin let buf_vaddr = args[1]; 147ab5c8ca4Slogin let len = args[2]; 148b5b571e0SLoGin let from_user = frame.is_from_user(); 1499e9ffedfSPlucky923 let mut user_buffer_writer = 1509e9ffedfSPlucky923 UserBufferWriter::new(buf_vaddr as *mut u8, len, from_user)?; 1516d81180bSLoGin 1529e9ffedfSPlucky923 let user_buf = user_buffer_writer.buffer(0)?; 15327b967a3S裕依 Self::read(fd, user_buf) 154ab5c8ca4Slogin } 155ab5c8ca4Slogin SYS_WRITE => { 156ab5c8ca4Slogin let fd = args[0] as i32; 157ab5c8ca4Slogin let buf_vaddr = args[1]; 158ab5c8ca4Slogin let len = args[2]; 159b5b571e0SLoGin let from_user = frame.is_from_user(); 1609e9ffedfSPlucky923 let user_buffer_reader = 1619e9ffedfSPlucky923 UserBufferReader::new(buf_vaddr as *const u8, len, from_user)?; 1626d81180bSLoGin 1639e9ffedfSPlucky923 let user_buf = user_buffer_reader.read_from_user(0)?; 16427b967a3S裕依 Self::write(fd, user_buf) 165ab5c8ca4Slogin } 166ab5c8ca4Slogin 167ab5c8ca4Slogin SYS_LSEEK => { 168ab5c8ca4Slogin let fd = args[0] as i32; 169ab5c8ca4Slogin let offset = args[1] as i64; 170ab5c8ca4Slogin let whence = args[2] as u32; 171ab5c8ca4Slogin 17282df0a13Shmt Self::lseek(fd, offset, whence) 173ab5c8ca4Slogin } 17427b967a3S裕依 17527b967a3S裕依 SYS_PREAD64 => { 17627b967a3S裕依 let fd = args[0] as i32; 17727b967a3S裕依 let buf_vaddr = args[1]; 17827b967a3S裕依 let len = args[2]; 17927b967a3S裕依 let offset = args[3]; 18027b967a3S裕依 18127b967a3S裕依 let mut user_buffer_writer = 182b5b571e0SLoGin UserBufferWriter::new(buf_vaddr as *mut u8, len, frame.is_from_user())?; 18327b967a3S裕依 let buf = user_buffer_writer.buffer(0)?; 18427b967a3S裕依 Self::pread(fd, buf, len, offset) 18527b967a3S裕依 } 18627b967a3S裕依 18727b967a3S裕依 SYS_PWRITE64 => { 18827b967a3S裕依 let fd = args[0] as i32; 18927b967a3S裕依 let buf_vaddr = args[1]; 19027b967a3S裕依 let len = args[2]; 19127b967a3S裕依 let offset = args[3]; 19227b967a3S裕依 19327b967a3S裕依 let user_buffer_reader = 194b5b571e0SLoGin UserBufferReader::new(buf_vaddr as *const u8, len, frame.is_from_user())?; 19527b967a3S裕依 19627b967a3S裕依 let buf = user_buffer_reader.read_from_user(0)?; 19727b967a3S裕依 Self::pwrite(fd, buf, len, offset) 19827b967a3S裕依 } 19927b967a3S裕依 20040314b30SXiaoye Zheng SYS_IOCTL => { 20140314b30SXiaoye Zheng let fd = args[0]; 20240314b30SXiaoye Zheng let cmd = args[1]; 20340314b30SXiaoye Zheng let data = args[2]; 20440314b30SXiaoye Zheng Self::ioctl(fd, cmd as u32, data) 20540314b30SXiaoye Zheng } 206ab5c8ca4Slogin 2074fda81ceSLoGin #[cfg(target_arch = "x86_64")] 2081496ba7bSLoGin SYS_FORK => Self::fork(frame), 2094fda81ceSLoGin #[cfg(target_arch = "x86_64")] 2101496ba7bSLoGin SYS_VFORK => Self::vfork(frame), 2111496ba7bSLoGin 212ab5c8ca4Slogin SYS_BRK => { 21340fe15e0SLoGin let new_brk = VirtAddr::new(args[0]); 21440fe15e0SLoGin Self::brk(new_brk).map(|vaddr| vaddr.data()) 215ab5c8ca4Slogin } 216ab5c8ca4Slogin 217ab5c8ca4Slogin SYS_SBRK => { 218ab5c8ca4Slogin let increment = args[0] as isize; 2191496ba7bSLoGin Self::sbrk(increment).map(|vaddr: VirtAddr| vaddr.data()) 220ab5c8ca4Slogin } 221ab5c8ca4Slogin 222ab5c8ca4Slogin SYS_REBOOT => Self::reboot(), 223ab5c8ca4Slogin 224ab5c8ca4Slogin SYS_CHDIR => { 22582df0a13Shmt let r = args[0] as *const u8; 2269e9ffedfSPlucky923 Self::chdir(r) 227ab5c8ca4Slogin } 228ab5c8ca4Slogin 2294fda81ceSLoGin #[allow(unreachable_patterns)] 2304fda81ceSLoGin SYS_GETDENTS64 | SYS_GETDENTS => { 231ab5c8ca4Slogin let fd = args[0] as i32; 23234e6d6c8Syuyi2439 233ab5c8ca4Slogin let buf_vaddr = args[1]; 234ab5c8ca4Slogin let len = args[2]; 235f5df0e79SLoGin let virt_addr: VirtAddr = VirtAddr::new(buf_vaddr); 236ab5c8ca4Slogin // 判断缓冲区是否来自用户态,进行权限校验 237b5b571e0SLoGin let res = if frame.is_from_user() && verify_area(virt_addr, len).is_err() { 238ab5c8ca4Slogin // 来自用户态,而buffer在内核态,这样的操作不被允许 239ab5c8ca4Slogin Err(SystemError::EPERM) 240ab5c8ca4Slogin } else if buf_vaddr == 0 { 241ab5c8ca4Slogin Err(SystemError::EFAULT) 242ab5c8ca4Slogin } else { 243ab5c8ca4Slogin let buf: &mut [u8] = unsafe { 244ab5c8ca4Slogin core::slice::from_raw_parts_mut::<'static, u8>(buf_vaddr as *mut u8, len) 245ab5c8ca4Slogin }; 246ab5c8ca4Slogin Self::getdents(fd, buf) 247ab5c8ca4Slogin }; 248ab5c8ca4Slogin 249ab5c8ca4Slogin res 250ab5c8ca4Slogin } 251ab5c8ca4Slogin 252ab5c8ca4Slogin SYS_EXECVE => { 253ab5c8ca4Slogin let path_ptr = args[0]; 254ab5c8ca4Slogin let argv_ptr = args[1]; 255ab5c8ca4Slogin let env_ptr = args[2]; 256be63f3b2Shoumkh let virt_path_ptr = VirtAddr::new(path_ptr); 257be63f3b2Shoumkh let virt_argv_ptr = VirtAddr::new(argv_ptr); 258be63f3b2Shoumkh let virt_env_ptr = VirtAddr::new(env_ptr); 259ab5c8ca4Slogin // 权限校验 260b5b571e0SLoGin if frame.is_from_user() 261b5b571e0SLoGin && (verify_area(virt_path_ptr, MAX_PATHLEN).is_err() 262370472f7SLoGin || verify_area(virt_argv_ptr, PAGE_4K_SIZE).is_err()) 263370472f7SLoGin || verify_area(virt_env_ptr, PAGE_4K_SIZE).is_err() 264ab5c8ca4Slogin { 265ab5c8ca4Slogin Err(SystemError::EFAULT) 266ab5c8ca4Slogin } else { 267ab5c8ca4Slogin Self::execve( 2681496ba7bSLoGin path_ptr as *const u8, 2691496ba7bSLoGin argv_ptr as *const *const u8, 2701496ba7bSLoGin env_ptr as *const *const u8, 2711496ba7bSLoGin frame, 272ab5c8ca4Slogin ) 2731496ba7bSLoGin .map(|_| 0) 274ab5c8ca4Slogin } 275ab5c8ca4Slogin } 276ab5c8ca4Slogin SYS_WAIT4 => { 277bf4a4899SLoGin let pid = args[0] as i32; 2781496ba7bSLoGin let wstatus = args[1] as *mut i32; 279ab5c8ca4Slogin let options = args[2] as c_int; 280ab5c8ca4Slogin let rusage = args[3] as *mut c_void; 281ab5c8ca4Slogin // 权限校验 282ab5c8ca4Slogin // todo: 引入rusage之后,更正以下权限校验代码中,rusage的大小 283bf4a4899SLoGin Self::wait4(pid.into(), wstatus, options, rusage) 284ab5c8ca4Slogin } 285ab5c8ca4Slogin 286ab5c8ca4Slogin SYS_EXIT => { 287ab5c8ca4Slogin let exit_code = args[0]; 288ab5c8ca4Slogin Self::exit(exit_code) 289ab5c8ca4Slogin } 2904fda81ceSLoGin #[cfg(target_arch = "x86_64")] 291ab5c8ca4Slogin SYS_MKDIR => { 29282df0a13Shmt let path = args[0] as *const u8; 293ab5c8ca4Slogin let mode = args[1]; 294ab5c8ca4Slogin 29582df0a13Shmt Self::mkdir(path, mode) 296ab5c8ca4Slogin } 297ab5c8ca4Slogin 298ab5c8ca4Slogin SYS_NANOSLEEP => { 2996fc066acSJomo let req = args[0] as *const PosixTimeSpec; 3006fc066acSJomo let rem = args[1] as *mut PosixTimeSpec; 301be63f3b2Shoumkh let virt_req = VirtAddr::new(req as usize); 302be63f3b2Shoumkh let virt_rem = VirtAddr::new(rem as usize); 303b5b571e0SLoGin if frame.is_from_user() 3046fc066acSJomo && (verify_area(virt_req, core::mem::size_of::<PosixTimeSpec>()).is_err() 3056fc066acSJomo || verify_area(virt_rem, core::mem::size_of::<PosixTimeSpec>()).is_err()) 306ab5c8ca4Slogin { 307ab5c8ca4Slogin Err(SystemError::EFAULT) 308ab5c8ca4Slogin } else { 309ab5c8ca4Slogin Self::nanosleep(req, rem) 310ab5c8ca4Slogin } 311ab5c8ca4Slogin } 312ab5c8ca4Slogin 313ab5c8ca4Slogin SYS_CLOCK => Self::clock(), 3144fda81ceSLoGin #[cfg(target_arch = "x86_64")] 315ab5c8ca4Slogin SYS_PIPE => { 31622c9db31Shanjiezhou let pipefd: *mut i32 = args[0] as *mut c_int; 3170fb515b0SLoGin if pipefd.is_null() { 3180fb515b0SLoGin Err(SystemError::EFAULT) 3190fb515b0SLoGin } else { 3200fb515b0SLoGin Self::pipe2(pipefd, FileMode::empty()) 3210fb515b0SLoGin } 3220fb515b0SLoGin } 3234ad52e57S裕依2439 3240fb515b0SLoGin SYS_PIPE2 => { 3250fb515b0SLoGin let pipefd: *mut i32 = args[0] as *mut c_int; 32622c9db31Shanjiezhou let arg1 = args[1]; 32722c9db31Shanjiezhou if pipefd.is_null() { 32822c9db31Shanjiezhou Err(SystemError::EFAULT) 32922c9db31Shanjiezhou } else { 33022c9db31Shanjiezhou let flags = FileMode::from_bits_truncate(arg1 as u32); 33122c9db31Shanjiezhou Self::pipe2(pipefd, flags) 332ab5c8ca4Slogin } 333ab5c8ca4Slogin } 334ab5c8ca4Slogin 3354fda81ceSLoGin SYS_UNLINKAT => { 336ab5c8ca4Slogin let dirfd = args[0] as i32; 33782df0a13Shmt let path = args[1] as *const u8; 338ab5c8ca4Slogin let flags = args[2] as u32; 33982df0a13Shmt Self::unlinkat(dirfd, path, flags) 340ab5c8ca4Slogin } 341bf4a4899SLoGin 3424fda81ceSLoGin #[cfg(target_arch = "x86_64")] 3435eeefb8cSChenzx SYS_RMDIR => { 34482df0a13Shmt let path = args[0] as *const u8; 34582df0a13Shmt Self::rmdir(path) 3465eeefb8cSChenzx } 3475eeefb8cSChenzx 3489b96c5b5SLoGin #[cfg(target_arch = "x86_64")] 3494695947eSChenzx SYS_LINK => { 3504695947eSChenzx let old = args[0] as *const u8; 3514695947eSChenzx let new = args[1] as *const u8; 3524695947eSChenzx return Self::link(old, new); 3534695947eSChenzx } 3544695947eSChenzx 3554695947eSChenzx SYS_LINKAT => { 3564695947eSChenzx let oldfd = args[0] as i32; 3574695947eSChenzx let old = args[1] as *const u8; 3584695947eSChenzx let newfd = args[2] as i32; 3594695947eSChenzx let new = args[3] as *const u8; 3604695947eSChenzx let flags = args[4] as i32; 3614695947eSChenzx return Self::linkat(oldfd, old, newfd, new, flags); 3624695947eSChenzx } 3634695947eSChenzx 3645eeefb8cSChenzx #[cfg(target_arch = "x86_64")] 365bf4a4899SLoGin SYS_UNLINK => { 36682df0a13Shmt let path = args[0] as *const u8; 36782df0a13Shmt Self::unlink(path) 368bf4a4899SLoGin } 369ab5c8ca4Slogin SYS_KILL => { 3701496ba7bSLoGin let pid = Pid::new(args[0]); 371ab5c8ca4Slogin let sig = args[1] as c_int; 3722eab6dd7S曾俊 // debug!("KILL SYSCALL RECEIVED"); 373ab5c8ca4Slogin Self::kill(pid, sig) 374ab5c8ca4Slogin } 375ab5c8ca4Slogin 3764fda81ceSLoGin SYS_RT_SIGACTION => { 377ab5c8ca4Slogin let sig = args[0] as c_int; 378ab5c8ca4Slogin let act = args[1]; 379ab5c8ca4Slogin let old_act = args[2]; 380b5b571e0SLoGin Self::sigaction(sig, act, old_act, frame.is_from_user()) 381ab5c8ca4Slogin } 382ab5c8ca4Slogin 3831496ba7bSLoGin SYS_GETPID => Self::getpid().map(|pid| pid.into()), 384ab5c8ca4Slogin 385f0c87a89SGnoCiYeH SYS_SCHED => { 3862eab6dd7S曾俊 warn!("syscall sched"); 387f0c87a89SGnoCiYeH schedule(SchedMode::SM_NONE); 388f0c87a89SGnoCiYeH Ok(0) 389f0c87a89SGnoCiYeH } 390ab5c8ca4Slogin SYS_DUP => { 391ab5c8ca4Slogin let oldfd: i32 = args[0] as c_int; 392ab5c8ca4Slogin Self::dup(oldfd) 393ab5c8ca4Slogin } 3944fda81ceSLoGin 3954fda81ceSLoGin #[cfg(target_arch = "x86_64")] 396ab5c8ca4Slogin SYS_DUP2 => { 397ab5c8ca4Slogin let oldfd: i32 = args[0] as c_int; 398ab5c8ca4Slogin let newfd: i32 = args[1] as c_int; 399ab5c8ca4Slogin Self::dup2(oldfd, newfd) 400ab5c8ca4Slogin } 401ab5c8ca4Slogin 40240348dd8Szwb0x00 SYS_DUP3 => { 40340348dd8Szwb0x00 let oldfd: i32 = args[0] as c_int; 40440348dd8Szwb0x00 let newfd: i32 = args[1] as c_int; 40540348dd8Szwb0x00 let flags: u32 = args[2] as u32; 40640348dd8Szwb0x00 Self::dup3(oldfd, newfd, flags) 40740348dd8Szwb0x00 } 40840348dd8Szwb0x00 409ab5c8ca4Slogin SYS_SOCKET => Self::socket(args[0], args[1], args[2]), 410ab5c8ca4Slogin SYS_SETSOCKOPT => { 411ab5c8ca4Slogin let optval = args[3] as *const u8; 412b5b571e0SLoGin let optlen = args[4]; 413be63f3b2Shoumkh let virt_optval = VirtAddr::new(optval as usize); 414ab5c8ca4Slogin // 验证optval的地址是否合法 415b5b571e0SLoGin if verify_area(virt_optval, optlen).is_err() { 416ab5c8ca4Slogin // 地址空间超出了用户空间的范围,不合法 417ab5c8ca4Slogin Err(SystemError::EFAULT) 418ab5c8ca4Slogin } else { 419ab5c8ca4Slogin let data: &[u8] = unsafe { core::slice::from_raw_parts(optval, optlen) }; 420ab5c8ca4Slogin Self::setsockopt(args[0], args[1], args[2], data) 421ab5c8ca4Slogin } 422ab5c8ca4Slogin } 423ab5c8ca4Slogin SYS_GETSOCKOPT => { 424ab5c8ca4Slogin let optval = args[3] as *mut u8; 425ab5c8ca4Slogin let optlen = args[4] as *mut usize; 426be63f3b2Shoumkh let virt_optval = VirtAddr::new(optval as usize); 427be63f3b2Shoumkh let virt_optlen = VirtAddr::new(optlen as usize); 428ab5c8ca4Slogin let security_check = || { 429ab5c8ca4Slogin // 验证optval的地址是否合法 430370472f7SLoGin if verify_area(virt_optval, PAGE_4K_SIZE).is_err() { 431ab5c8ca4Slogin // 地址空间超出了用户空间的范围,不合法 432ab5c8ca4Slogin return Err(SystemError::EFAULT); 433ab5c8ca4Slogin } 434ab5c8ca4Slogin 435ab5c8ca4Slogin // 验证optlen的地址是否合法 436b5b571e0SLoGin if verify_area(virt_optlen, core::mem::size_of::<u32>()).is_err() { 437ab5c8ca4Slogin // 地址空间超出了用户空间的范围,不合法 438ab5c8ca4Slogin return Err(SystemError::EFAULT); 439ab5c8ca4Slogin } 440ab5c8ca4Slogin return Ok(()); 441ab5c8ca4Slogin }; 442ab5c8ca4Slogin let r = security_check(); 443b5b571e0SLoGin if let Err(e) = r { 444b5b571e0SLoGin Err(e) 445ab5c8ca4Slogin } else { 446ab5c8ca4Slogin Self::getsockopt(args[0], args[1], args[2], optval, optlen as *mut u32) 447ab5c8ca4Slogin } 448ab5c8ca4Slogin } 449ab5c8ca4Slogin 450ab5c8ca4Slogin SYS_CONNECT => { 451ab5c8ca4Slogin let addr = args[1] as *const SockAddr; 452b5b571e0SLoGin let addrlen = args[2]; 453be63f3b2Shoumkh let virt_addr = VirtAddr::new(addr as usize); 454ab5c8ca4Slogin // 验证addr的地址是否合法 455b5b571e0SLoGin if verify_area(virt_addr, addrlen).is_err() { 456ab5c8ca4Slogin // 地址空间超出了用户空间的范围,不合法 457ab5c8ca4Slogin Err(SystemError::EFAULT) 458ab5c8ca4Slogin } else { 459ab5c8ca4Slogin Self::connect(args[0], addr, addrlen) 460ab5c8ca4Slogin } 461ab5c8ca4Slogin } 462ab5c8ca4Slogin SYS_BIND => { 463ab5c8ca4Slogin let addr = args[1] as *const SockAddr; 464b5b571e0SLoGin let addrlen = args[2]; 465be63f3b2Shoumkh let virt_addr = VirtAddr::new(addr as usize); 466ab5c8ca4Slogin // 验证addr的地址是否合法 467b5b571e0SLoGin if verify_area(virt_addr, addrlen).is_err() { 468ab5c8ca4Slogin // 地址空间超出了用户空间的范围,不合法 469ab5c8ca4Slogin Err(SystemError::EFAULT) 470ab5c8ca4Slogin } else { 471ab5c8ca4Slogin Self::bind(args[0], addr, addrlen) 472ab5c8ca4Slogin } 473ab5c8ca4Slogin } 474ab5c8ca4Slogin 475ab5c8ca4Slogin SYS_SENDTO => { 476ab5c8ca4Slogin let buf = args[1] as *const u8; 477b5b571e0SLoGin let len = args[2]; 478ab5c8ca4Slogin let flags = args[3] as u32; 479ab5c8ca4Slogin let addr = args[4] as *const SockAddr; 480b5b571e0SLoGin let addrlen = args[5]; 481be63f3b2Shoumkh let virt_buf = VirtAddr::new(buf as usize); 482be63f3b2Shoumkh let virt_addr = VirtAddr::new(addr as usize); 483ab5c8ca4Slogin // 验证buf的地址是否合法 484b5b571e0SLoGin if verify_area(virt_buf, len).is_err() || verify_area(virt_addr, addrlen).is_err() { 485ab5c8ca4Slogin // 地址空间超出了用户空间的范围,不合法 486ab5c8ca4Slogin Err(SystemError::EFAULT) 487ab5c8ca4Slogin } else { 488ab5c8ca4Slogin let data: &[u8] = unsafe { core::slice::from_raw_parts(buf, len) }; 489ab5c8ca4Slogin Self::sendto(args[0], data, flags, addr, addrlen) 490ab5c8ca4Slogin } 491ab5c8ca4Slogin } 492ab5c8ca4Slogin 493ab5c8ca4Slogin SYS_RECVFROM => { 494ab5c8ca4Slogin let buf = args[1] as *mut u8; 495b5b571e0SLoGin let len = args[2]; 496ab5c8ca4Slogin let flags = args[3] as u32; 497ab5c8ca4Slogin let addr = args[4] as *mut SockAddr; 498ab5c8ca4Slogin let addrlen = args[5] as *mut usize; 499be63f3b2Shoumkh let virt_buf = VirtAddr::new(buf as usize); 500be63f3b2Shoumkh let virt_addrlen = VirtAddr::new(addrlen as usize); 501be63f3b2Shoumkh let virt_addr = VirtAddr::new(addr as usize); 502ab5c8ca4Slogin let security_check = || { 503ab5c8ca4Slogin // 验证buf的地址是否合法 504840045afSLoGin if verify_area(virt_buf, len).is_err() { 505ab5c8ca4Slogin // 地址空间超出了用户空间的范围,不合法 506ab5c8ca4Slogin return Err(SystemError::EFAULT); 507ab5c8ca4Slogin } 508ab5c8ca4Slogin 509ab5c8ca4Slogin // 验证addrlen的地址是否合法 510840045afSLoGin if verify_area(virt_addrlen, core::mem::size_of::<u32>()).is_err() { 511ab5c8ca4Slogin // 地址空间超出了用户空间的范围,不合法 512ab5c8ca4Slogin return Err(SystemError::EFAULT); 513ab5c8ca4Slogin } 514ab5c8ca4Slogin 515840045afSLoGin if verify_area(virt_addr, core::mem::size_of::<SockAddr>()).is_err() { 516ab5c8ca4Slogin // 地址空间超出了用户空间的范围,不合法 517ab5c8ca4Slogin return Err(SystemError::EFAULT); 518ab5c8ca4Slogin } 519ab5c8ca4Slogin return Ok(()); 520ab5c8ca4Slogin }; 521ab5c8ca4Slogin let r = security_check(); 522b5b571e0SLoGin if let Err(e) = r { 523b5b571e0SLoGin Err(e) 524ab5c8ca4Slogin } else { 525ab5c8ca4Slogin let buf = unsafe { core::slice::from_raw_parts_mut(buf, len) }; 526ab5c8ca4Slogin Self::recvfrom(args[0], buf, flags, addr, addrlen as *mut u32) 527ab5c8ca4Slogin } 528ab5c8ca4Slogin } 529ab5c8ca4Slogin 530ab5c8ca4Slogin SYS_RECVMSG => { 5314ad52e57S裕依2439 let msg = args[1] as *mut MsgHdr; 532ab5c8ca4Slogin let flags = args[2] as u32; 5334ad52e57S裕依2439 534b5b571e0SLoGin let mut user_buffer_writer = UserBufferWriter::new( 535b5b571e0SLoGin msg, 536b5b571e0SLoGin core::mem::size_of::<MsgHdr>(), 537b5b571e0SLoGin frame.is_from_user(), 538b5b571e0SLoGin )?; 5394ad52e57S裕依2439 let buffer = user_buffer_writer.buffer::<MsgHdr>(0)?; 5404ad52e57S裕依2439 5414537ffb7SChiichen let msg = &mut buffer[0]; 542ab5c8ca4Slogin Self::recvmsg(args[0], msg, flags) 543ab5c8ca4Slogin } 544ab5c8ca4Slogin 545ab5c8ca4Slogin SYS_LISTEN => Self::listen(args[0], args[1]), 546ab5c8ca4Slogin SYS_SHUTDOWN => Self::shutdown(args[0], args[1]), 547ab5c8ca4Slogin SYS_ACCEPT => Self::accept(args[0], args[1] as *mut SockAddr, args[2] as *mut u32), 548c47fe904SLoGin SYS_ACCEPT4 => Self::accept4( 549c47fe904SLoGin args[0], 550c47fe904SLoGin args[1] as *mut SockAddr, 551c47fe904SLoGin args[2] as *mut u32, 552c47fe904SLoGin args[3] as u32, 553c47fe904SLoGin ), 554ab5c8ca4Slogin SYS_GETSOCKNAME => { 555ab5c8ca4Slogin Self::getsockname(args[0], args[1] as *mut SockAddr, args[2] as *mut u32) 556ab5c8ca4Slogin } 557ab5c8ca4Slogin SYS_GETPEERNAME => { 558ab5c8ca4Slogin Self::getpeername(args[0], args[1] as *mut SockAddr, args[2] as *mut u32) 559ab5c8ca4Slogin } 56036fd0130Shoumkh SYS_GETTIMEOFDAY => { 56136fd0130Shoumkh let timeval = args[0] as *mut PosixTimeval; 56240fe15e0SLoGin let timezone_ptr = args[1] as *mut PosixTimeZone; 56340fe15e0SLoGin Self::gettimeofday(timeval, timezone_ptr) 5644537ffb7SChiichen } 56540fe15e0SLoGin SYS_MMAP => { 56640fe15e0SLoGin let len = page_align_up(args[1]); 567840045afSLoGin let virt_addr = VirtAddr::new(args[0]); 568840045afSLoGin if verify_area(virt_addr, len).is_err() { 56940fe15e0SLoGin Err(SystemError::EFAULT) 57040fe15e0SLoGin } else { 57140fe15e0SLoGin Self::mmap( 57240fe15e0SLoGin VirtAddr::new(args[0]), 57340fe15e0SLoGin len, 57440fe15e0SLoGin args[2], 57540fe15e0SLoGin args[3], 57640fe15e0SLoGin args[4] as i32, 57740fe15e0SLoGin args[5], 57840fe15e0SLoGin ) 57940fe15e0SLoGin } 58040fe15e0SLoGin } 5814cfa009bSJomo SYS_MREMAP => { 5824cfa009bSJomo let old_vaddr = VirtAddr::new(args[0]); 5834cfa009bSJomo let old_len = args[1]; 5844cfa009bSJomo let new_len = args[2]; 5854cfa009bSJomo let mremap_flags = MremapFlags::from_bits_truncate(args[3] as u8); 5864cfa009bSJomo let new_vaddr = VirtAddr::new(args[4]); 5874cfa009bSJomo 5884cfa009bSJomo Self::mremap(old_vaddr, old_len, new_len, mremap_flags, new_vaddr) 5894cfa009bSJomo } 59040fe15e0SLoGin SYS_MUNMAP => { 59140fe15e0SLoGin let addr = args[0]; 59240fe15e0SLoGin let len = page_align_up(args[1]); 593971462beSGnoCiYeH if addr & (MMArch::PAGE_SIZE - 1) != 0 { 59440fe15e0SLoGin // The addr argument is not a multiple of the page size 59540fe15e0SLoGin Err(SystemError::EINVAL) 59640fe15e0SLoGin } else { 59740fe15e0SLoGin Self::munmap(VirtAddr::new(addr), len) 59840fe15e0SLoGin } 59940fe15e0SLoGin } 60040fe15e0SLoGin SYS_MPROTECT => { 60140fe15e0SLoGin let addr = args[0]; 60240fe15e0SLoGin let len = page_align_up(args[1]); 603971462beSGnoCiYeH if addr & (MMArch::PAGE_SIZE - 1) != 0 { 60440fe15e0SLoGin // The addr argument is not a multiple of the page size 60540fe15e0SLoGin Err(SystemError::EINVAL) 60640fe15e0SLoGin } else { 60740fe15e0SLoGin Self::mprotect(VirtAddr::new(addr), len, args[2]) 60840fe15e0SLoGin } 60940fe15e0SLoGin } 61040fe15e0SLoGin 6111496ba7bSLoGin SYS_GETCWD => { 6121496ba7bSLoGin let buf = args[0] as *mut u8; 613840045afSLoGin let size = args[1]; 6141496ba7bSLoGin let security_check = || { 6151496ba7bSLoGin verify_area(VirtAddr::new(buf as usize), size)?; 6161496ba7bSLoGin return Ok(()); 6171496ba7bSLoGin }; 6181496ba7bSLoGin let r = security_check(); 619b5b571e0SLoGin if let Err(e) = r { 620b5b571e0SLoGin Err(e) 6211496ba7bSLoGin } else { 6221496ba7bSLoGin let buf = unsafe { core::slice::from_raw_parts_mut(buf, size) }; 6231496ba7bSLoGin Self::getcwd(buf).map(|ptr| ptr.data()) 6241496ba7bSLoGin } 6251496ba7bSLoGin } 6261496ba7bSLoGin 6271496ba7bSLoGin SYS_GETPGID => Self::getpgid(Pid::new(args[0])).map(|pid| pid.into()), 6281496ba7bSLoGin 6291496ba7bSLoGin SYS_GETPPID => Self::getppid().map(|pid| pid.into()), 630be63f3b2Shoumkh SYS_FSTAT => { 631be63f3b2Shoumkh let fd = args[0] as i32; 632b5b571e0SLoGin let kstat: *mut PosixKstat = args[1] as *mut PosixKstat; 633be63f3b2Shoumkh let vaddr = VirtAddr::new(kstat as usize); 634be63f3b2Shoumkh // FIXME 由于c中的verify_area与rust中的verify_area重名,所以在引入时加了前缀区分 635be63f3b2Shoumkh // TODO 应该将用了c版本的verify_area都改为rust的verify_area 636be63f3b2Shoumkh match verify_area(vaddr, core::mem::size_of::<PosixKstat>()) { 637be63f3b2Shoumkh Ok(_) => Self::fstat(fd, kstat), 638be63f3b2Shoumkh Err(e) => Err(e), 639be63f3b2Shoumkh } 640be63f3b2Shoumkh } 641be63f3b2Shoumkh 6426d81180bSLoGin SYS_FCNTL => { 6436d81180bSLoGin let fd = args[0] as i32; 6446d81180bSLoGin let cmd: Option<FcntlCommand> = 6456d81180bSLoGin <FcntlCommand as FromPrimitive>::from_u32(args[1] as u32); 6466d81180bSLoGin let arg = args[2] as i32; 6476d81180bSLoGin let res = if let Some(cmd) = cmd { 6486d81180bSLoGin Self::fcntl(fd, cmd, arg) 6496d81180bSLoGin } else { 6506d81180bSLoGin Err(SystemError::EINVAL) 6516d81180bSLoGin }; 6526d81180bSLoGin 6532eab6dd7S曾俊 // debug!("FCNTL: fd: {}, cmd: {:?}, arg: {}, res: {:?}", fd, cmd, arg, res); 6546d81180bSLoGin res 6556d81180bSLoGin } 6566d81180bSLoGin 6576d81180bSLoGin SYS_FTRUNCATE => { 6586d81180bSLoGin let fd = args[0] as i32; 659840045afSLoGin let len = args[1]; 6606d81180bSLoGin let res = Self::ftruncate(fd, len); 6612eab6dd7S曾俊 // debug!("FTRUNCATE: fd: {}, len: {}, res: {:?}", fd, len, res); 6626d81180bSLoGin res 6636d81180bSLoGin } 6646d81180bSLoGin 6654fda81ceSLoGin #[cfg(target_arch = "x86_64")] 6662dbef785SGnoCiYeH SYS_MKNOD => { 6672dbef785SGnoCiYeH let path = args[0]; 6682dbef785SGnoCiYeH let flags = args[1]; 6692dbef785SGnoCiYeH let dev_t = args[2]; 6702dbef785SGnoCiYeH let flags: ModeType = ModeType::from_bits_truncate(flags as u32); 671731bc2b3SLoGin Self::mknod( 672731bc2b3SLoGin path as *const u8, 673731bc2b3SLoGin flags, 674731bc2b3SLoGin crate::driver::base::device::device_number::DeviceNumber::from(dev_t as u32), 675731bc2b3SLoGin ) 6762dbef785SGnoCiYeH } 6772dbef785SGnoCiYeH 678971462beSGnoCiYeH SYS_CLONE => { 679971462beSGnoCiYeH let parent_tid = VirtAddr::new(args[2]); 680971462beSGnoCiYeH let child_tid = VirtAddr::new(args[3]); 681971462beSGnoCiYeH 682971462beSGnoCiYeH // 地址校验 683971462beSGnoCiYeH verify_area(parent_tid, core::mem::size_of::<i32>())?; 684971462beSGnoCiYeH verify_area(child_tid, core::mem::size_of::<i32>())?; 685971462beSGnoCiYeH 686971462beSGnoCiYeH let mut clone_args = KernelCloneArgs::new(); 687971462beSGnoCiYeH clone_args.flags = CloneFlags::from_bits_truncate(args[0] as u64); 688971462beSGnoCiYeH clone_args.stack = args[1]; 689971462beSGnoCiYeH clone_args.parent_tid = parent_tid; 690971462beSGnoCiYeH clone_args.child_tid = child_tid; 691971462beSGnoCiYeH clone_args.tls = args[4]; 692971462beSGnoCiYeH Self::clone(frame, clone_args) 693971462beSGnoCiYeH } 694971462beSGnoCiYeH 695971462beSGnoCiYeH SYS_FUTEX => { 696971462beSGnoCiYeH let uaddr = VirtAddr::new(args[0]); 697971462beSGnoCiYeH let operation = FutexFlag::from_bits(args[1] as u32).ok_or(SystemError::ENOSYS)?; 698971462beSGnoCiYeH let val = args[2] as u32; 699971462beSGnoCiYeH let utime = args[3]; 700971462beSGnoCiYeH let uaddr2 = VirtAddr::new(args[4]); 701971462beSGnoCiYeH let val3 = args[5] as u32; 702971462beSGnoCiYeH 703971462beSGnoCiYeH let mut timespec = None; 704971462beSGnoCiYeH if utime != 0 && operation.contains(FutexFlag::FLAGS_HAS_TIMEOUT) { 705971462beSGnoCiYeH let reader = UserBufferReader::new( 7066fc066acSJomo utime as *const PosixTimeSpec, 7076fc066acSJomo core::mem::size_of::<PosixTimeSpec>(), 708971462beSGnoCiYeH true, 709971462beSGnoCiYeH )?; 710971462beSGnoCiYeH 7116fc066acSJomo timespec = Some(*reader.read_one_from_user::<PosixTimeSpec>(0)?); 712971462beSGnoCiYeH } 713971462beSGnoCiYeH 714971462beSGnoCiYeH Self::do_futex(uaddr, operation, val, timespec, uaddr2, utime as u32, val3) 715971462beSGnoCiYeH } 716971462beSGnoCiYeH 71706560afaShmt SYS_SET_ROBUST_LIST => { 71806560afaShmt let head = args[0]; 71906560afaShmt let head_uaddr = VirtAddr::new(head); 72006560afaShmt let len = args[1]; 72106560afaShmt 72206560afaShmt let ret = Self::set_robust_list(head_uaddr, len); 72306560afaShmt return ret; 72406560afaShmt } 72506560afaShmt 72606560afaShmt SYS_GET_ROBUST_LIST => { 72706560afaShmt let pid = args[0]; 72806560afaShmt let head = args[1]; 72906560afaShmt let head_uaddr = VirtAddr::new(head); 73006560afaShmt let len_ptr = args[2]; 73106560afaShmt let len_ptr_uaddr = VirtAddr::new(len_ptr); 73206560afaShmt 73306560afaShmt let ret = Self::get_robust_list(pid, head_uaddr, len_ptr_uaddr); 73406560afaShmt return ret; 73506560afaShmt } 73606560afaShmt 737bf4a4899SLoGin SYS_READV => Self::readv(args[0] as i32, args[1], args[2]), 738971462beSGnoCiYeH SYS_WRITEV => Self::writev(args[0] as i32, args[1], args[2]), 739971462beSGnoCiYeH 7404fda81ceSLoGin SYS_SET_TID_ADDRESS => Self::set_tid_address(args[0]), 741971462beSGnoCiYeH 7424fda81ceSLoGin #[cfg(target_arch = "x86_64")] 7434fda81ceSLoGin SYS_LSTAT => { 74482df0a13Shmt let path = args[0] as *const u8; 745971462beSGnoCiYeH let kstat = args[1] as *mut PosixKstat; 74682df0a13Shmt Self::lstat(path, kstat) 7474fda81ceSLoGin } 7484fda81ceSLoGin 7494fda81ceSLoGin #[cfg(target_arch = "x86_64")] 7504fda81ceSLoGin SYS_STAT => { 75182df0a13Shmt let path = args[0] as *const u8; 7524fda81ceSLoGin let kstat = args[1] as *mut PosixKstat; 75382df0a13Shmt Self::stat(path, kstat) 754971462beSGnoCiYeH } 755971462beSGnoCiYeH 756597ecc08STTaq SYS_STATFS => { 757597ecc08STTaq let path = args[0] as *const u8; 758597ecc08STTaq let statfs = args[1] as *mut PosixStatfs; 759597ecc08STTaq Self::statfs(path, statfs) 760597ecc08STTaq } 761597ecc08STTaq 762597ecc08STTaq SYS_FSTATFS => { 763597ecc08STTaq let fd = args[0] as i32; 764597ecc08STTaq let statfs = args[1] as *mut PosixStatfs; 765597ecc08STTaq Self::fstatfs(fd, statfs) 766597ecc08STTaq } 767597ecc08STTaq 768b4eb05a1STTaq SYS_STATX => { 769b4eb05a1STTaq let fd = args[0] as i32; 770b4eb05a1STTaq let path = args[1] as *const u8; 771b4eb05a1STTaq let flags = args[2] as u32; 772b4eb05a1STTaq let mask = args[3] as u32; 773b4eb05a1STTaq let kstat = args[4] as *mut PosixStatx; 774b4eb05a1STTaq 775b4eb05a1STTaq Self::do_statx(fd, path, flags, mask, kstat) 776b4eb05a1STTaq } 777b4eb05a1STTaq 77870f159a3SLoGin #[cfg(target_arch = "x86_64")] 77940609970SGnoCiYeH SYS_EPOLL_CREATE => Self::epoll_create(args[0] as i32), 78040609970SGnoCiYeH SYS_EPOLL_CREATE1 => Self::epoll_create1(args[0]), 78140609970SGnoCiYeH 78240609970SGnoCiYeH SYS_EPOLL_CTL => Self::epoll_ctl( 78340609970SGnoCiYeH args[0] as i32, 78440609970SGnoCiYeH args[1], 78540609970SGnoCiYeH args[2] as i32, 78640609970SGnoCiYeH VirtAddr::new(args[3]), 78740609970SGnoCiYeH ), 78840609970SGnoCiYeH 78970f159a3SLoGin #[cfg(target_arch = "x86_64")] 79040609970SGnoCiYeH SYS_EPOLL_WAIT => Self::epoll_wait( 79140609970SGnoCiYeH args[0] as i32, 79240609970SGnoCiYeH VirtAddr::new(args[1]), 79340609970SGnoCiYeH args[2] as i32, 79440609970SGnoCiYeH args[3] as i32, 79540609970SGnoCiYeH ), 79640609970SGnoCiYeH 79740609970SGnoCiYeH SYS_EPOLL_PWAIT => { 79840609970SGnoCiYeH let epfd = args[0] as i32; 79940609970SGnoCiYeH let epoll_event = VirtAddr::new(args[1]); 80040609970SGnoCiYeH let max_events = args[2] as i32; 80140609970SGnoCiYeH let timespec = args[3] as i32; 80240609970SGnoCiYeH let sigmask_addr = args[4] as *mut SigSet; 80340609970SGnoCiYeH 80440609970SGnoCiYeH if sigmask_addr.is_null() { 80540609970SGnoCiYeH return Self::epoll_wait(epfd, epoll_event, max_events, timespec); 80640609970SGnoCiYeH } 80740609970SGnoCiYeH let sigmask_reader = 80840609970SGnoCiYeH UserBufferReader::new(sigmask_addr, core::mem::size_of::<SigSet>(), true)?; 809840045afSLoGin let mut sigmask = *sigmask_reader.read_one_from_user::<SigSet>(0)?; 81040609970SGnoCiYeH 81140609970SGnoCiYeH Self::epoll_pwait( 81240609970SGnoCiYeH args[0] as i32, 81340609970SGnoCiYeH VirtAddr::new(args[1]), 81440609970SGnoCiYeH args[2] as i32, 81540609970SGnoCiYeH args[3] as i32, 81640609970SGnoCiYeH &mut sigmask, 81740609970SGnoCiYeH ) 81840609970SGnoCiYeH } 81940609970SGnoCiYeH 820971462beSGnoCiYeH // 目前为了适配musl-libc,以下系统调用先这样写着 8214fda81ceSLoGin SYS_GETRANDOM => { 822971462beSGnoCiYeH let flags = GRandFlags::from_bits(args[2] as u8).ok_or(SystemError::EINVAL)?; 823971462beSGnoCiYeH Self::get_random(args[0] as *mut u8, args[1], flags) 824971462beSGnoCiYeH } 825971462beSGnoCiYeH 8264fda81ceSLoGin SYS_SOCKETPAIR => { 8274ad52e57S裕依2439 let mut user_buffer_writer = UserBufferWriter::new( 8284ad52e57S裕依2439 args[3] as *mut c_int, 8294ad52e57S裕依2439 core::mem::size_of::<[c_int; 2]>(), 830b5b571e0SLoGin frame.is_from_user(), 8314ad52e57S裕依2439 )?; 8324ad52e57S裕依2439 let fds = user_buffer_writer.buffer::<i32>(0)?; 8334ad52e57S裕依2439 Self::socketpair(args[0], args[1], args[2], fds) 834971462beSGnoCiYeH } 835971462beSGnoCiYeH 8364fda81ceSLoGin #[cfg(target_arch = "x86_64")] 837971462beSGnoCiYeH SYS_POLL => { 8382eab6dd7S曾俊 warn!("SYS_POLL has not yet been implemented"); 839971462beSGnoCiYeH Ok(0) 840971462beSGnoCiYeH } 841971462beSGnoCiYeH 84252da9a59SGnoCiYeH SYS_SETPGID => { 8432eab6dd7S曾俊 warn!("SYS_SETPGID has not yet been implemented"); 84452da9a59SGnoCiYeH Ok(0) 84552da9a59SGnoCiYeH } 84652da9a59SGnoCiYeH 847971462beSGnoCiYeH SYS_RT_SIGPROCMASK => { 8482eab6dd7S曾俊 warn!("SYS_RT_SIGPROCMASK has not yet been implemented"); 849971462beSGnoCiYeH Ok(0) 850971462beSGnoCiYeH } 851971462beSGnoCiYeH 852971462beSGnoCiYeH SYS_TKILL => { 8532eab6dd7S曾俊 warn!("SYS_TKILL has not yet been implemented"); 854971462beSGnoCiYeH Ok(0) 855971462beSGnoCiYeH } 856971462beSGnoCiYeH 857971462beSGnoCiYeH SYS_SIGALTSTACK => { 8582eab6dd7S曾俊 warn!("SYS_SIGALTSTACK has not yet been implemented"); 859971462beSGnoCiYeH Ok(0) 860971462beSGnoCiYeH } 861971462beSGnoCiYeH 86216033951SGnoCiYeH SYS_EXIT_GROUP => { 8632eab6dd7S曾俊 warn!("SYS_EXIT_GROUP has not yet been implemented"); 86416033951SGnoCiYeH Ok(0) 86516033951SGnoCiYeH } 86616033951SGnoCiYeH 86704babc3fSMemoryShore SYS_MADVISE => { 868a17651b1SMemoryShore let addr = args[0]; 869a17651b1SMemoryShore let len = page_align_up(args[1]); 870a17651b1SMemoryShore if addr & (MMArch::PAGE_SIZE - 1) != 0 { 871a17651b1SMemoryShore Err(SystemError::EINVAL) 872a17651b1SMemoryShore } else { 873a17651b1SMemoryShore Self::madvise(VirtAddr::new(addr), len, args[2]) 87404babc3fSMemoryShore } 875a17651b1SMemoryShore } 876a17651b1SMemoryShore 877393f6915SLoGin SYS_GETTID => Self::gettid().map(|tid| tid.into()), 878840045afSLoGin SYS_GETUID => Self::getuid(), 8798d72b68dSJomo 88002e249f3SLoGin SYS_SYSLOG => { 881840045afSLoGin let syslog_action_type = args[0]; 8828d72b68dSJomo let buf_vaddr = args[1]; 8838d72b68dSJomo let len = args[2]; 884b5b571e0SLoGin let from_user = frame.is_from_user(); 8858d72b68dSJomo let mut user_buffer_writer = 8868d72b68dSJomo UserBufferWriter::new(buf_vaddr as *mut u8, len, from_user)?; 8878d72b68dSJomo 8888d72b68dSJomo let user_buf = user_buffer_writer.buffer(0)?; 889840045afSLoGin Self::do_syslog(syslog_action_type, user_buf, len) 89002e249f3SLoGin } 8918d72b68dSJomo 892840045afSLoGin SYS_GETGID => Self::getgid(), 89302e249f3SLoGin SYS_SETUID => { 8942eab6dd7S曾俊 warn!("SYS_SETUID has not yet been implemented"); 89502e249f3SLoGin Ok(0) 89602e249f3SLoGin } 89702e249f3SLoGin SYS_SETGID => { 8982eab6dd7S曾俊 warn!("SYS_SETGID has not yet been implemented"); 89902e249f3SLoGin Ok(0) 90002e249f3SLoGin } 901dfe53cf0SGnoCiYeH SYS_SETSID => { 9022eab6dd7S曾俊 warn!("SYS_SETSID has not yet been implemented"); 903dfe53cf0SGnoCiYeH Ok(0) 904dfe53cf0SGnoCiYeH } 905840045afSLoGin SYS_GETEUID => Self::geteuid(), 906840045afSLoGin SYS_GETEGID => Self::getegid(), 907be8cdf4bSLoGin SYS_GETRUSAGE => { 908be8cdf4bSLoGin let who = args[0] as c_int; 909be8cdf4bSLoGin let rusage = args[1] as *mut RUsage; 910be8cdf4bSLoGin Self::get_rusage(who, rusage) 911be8cdf4bSLoGin } 91204babc3fSMemoryShore 9134fda81ceSLoGin #[cfg(target_arch = "x86_64")] 914709498caSLoGin SYS_READLINK => { 915709498caSLoGin let path = args[0] as *const u8; 916709498caSLoGin let buf = args[1] as *mut u8; 917840045afSLoGin let bufsiz = args[2]; 918709498caSLoGin Self::readlink(path, buf, bufsiz) 919709498caSLoGin } 920709498caSLoGin 9214fda81ceSLoGin SYS_READLINKAT => { 922709498caSLoGin let dirfd = args[0] as i32; 92382df0a13Shmt let path = args[1] as *const u8; 924709498caSLoGin let buf = args[2] as *mut u8; 925840045afSLoGin let bufsiz = args[3]; 92682df0a13Shmt Self::readlink_at(dirfd, path, buf, bufsiz) 927709498caSLoGin } 928709498caSLoGin 9290d9b7d92SLoGin SYS_PRLIMIT64 => { 9300d9b7d92SLoGin let pid = args[0]; 9310d9b7d92SLoGin let pid = Pid::new(pid); 9320d9b7d92SLoGin let resource = args[1]; 9330d9b7d92SLoGin let new_limit = args[2] as *const RLimit64; 9340d9b7d92SLoGin let old_limit = args[3] as *mut RLimit64; 9350d9b7d92SLoGin 9360d9b7d92SLoGin Self::prlimit64(pid, resource, new_limit, old_limit) 9370d9b7d92SLoGin } 9380d9b7d92SLoGin 9394fda81ceSLoGin #[cfg(target_arch = "x86_64")] 9409b0abe6dSLoGin SYS_ACCESS => { 9419b0abe6dSLoGin let pathname = args[0] as *const u8; 9429b0abe6dSLoGin let mode = args[1] as u32; 9439b0abe6dSLoGin Self::access(pathname, mode) 9449b0abe6dSLoGin } 9459b0abe6dSLoGin 9469b0abe6dSLoGin SYS_FACCESSAT => { 9479b0abe6dSLoGin let dirfd = args[0] as i32; 9489b0abe6dSLoGin let pathname = args[1] as *const u8; 9499b0abe6dSLoGin let mode = args[2] as u32; 9509b0abe6dSLoGin Self::faccessat2(dirfd, pathname, mode, 0) 9519b0abe6dSLoGin } 9529b0abe6dSLoGin 9539b0abe6dSLoGin SYS_FACCESSAT2 => { 9549b0abe6dSLoGin let dirfd = args[0] as i32; 9559b0abe6dSLoGin let pathname = args[1] as *const u8; 9569b0abe6dSLoGin let mode = args[2] as u32; 9579b0abe6dSLoGin let flags = args[3] as u32; 9589b0abe6dSLoGin Self::faccessat2(dirfd, pathname, mode, flags) 9599b0abe6dSLoGin } 9609b0abe6dSLoGin 961bf4a4899SLoGin SYS_CLOCK_GETTIME => { 962bf4a4899SLoGin let clockid = args[0] as i32; 9636fc066acSJomo let timespec = args[1] as *mut PosixTimeSpec; 964bf4a4899SLoGin Self::clock_gettime(clockid, timespec) 965bf4a4899SLoGin } 966bf4a4899SLoGin 967bf4a4899SLoGin SYS_SYSINFO => { 968bf4a4899SLoGin let info = args[0] as *mut SysInfo; 969bf4a4899SLoGin Self::sysinfo(info) 970bf4a4899SLoGin } 971bf4a4899SLoGin 972bf4a4899SLoGin SYS_UMASK => { 973bf4a4899SLoGin let mask = args[0] as u32; 974bf4a4899SLoGin Self::umask(mask) 975bf4a4899SLoGin } 976bf4a4899SLoGin 97727b967a3S裕依 SYS_FCHOWN => { 9782eab6dd7S曾俊 warn!("SYS_FCHOWN has not yet been implemented"); 97927b967a3S裕依 Ok(0) 98027b967a3S裕依 } 98127b967a3S裕依 98227b967a3S裕依 SYS_FSYNC => { 9832eab6dd7S曾俊 warn!("SYS_FSYNC has not yet been implemented"); 98427b967a3S裕依 Ok(0) 98527b967a3S裕依 } 98627b967a3S裕依 987dfe53cf0SGnoCiYeH SYS_RSEQ => { 9882eab6dd7S曾俊 warn!("SYS_RSEQ has not yet been implemented"); 989dfe53cf0SGnoCiYeH Ok(0) 990dfe53cf0SGnoCiYeH } 991dfe53cf0SGnoCiYeH 9924fda81ceSLoGin #[cfg(target_arch = "x86_64")] 993bf4a4899SLoGin SYS_CHMOD => { 994bf4a4899SLoGin let pathname = args[0] as *const u8; 995bf4a4899SLoGin let mode = args[1] as u32; 996bf4a4899SLoGin Self::chmod(pathname, mode) 997bf4a4899SLoGin } 998bf4a4899SLoGin SYS_FCHMOD => { 999bf4a4899SLoGin let fd = args[0] as i32; 1000bf4a4899SLoGin let mode = args[1] as u32; 1001bf4a4899SLoGin Self::fchmod(fd, mode) 1002bf4a4899SLoGin } 1003bf4a4899SLoGin SYS_FCHMODAT => { 1004bf4a4899SLoGin let dirfd = args[0] as i32; 1005bf4a4899SLoGin let pathname = args[1] as *const u8; 1006bf4a4899SLoGin let mode = args[2] as u32; 1007bf4a4899SLoGin Self::fchmodat(dirfd, pathname, mode) 1008bf4a4899SLoGin } 1009bf4a4899SLoGin 1010173c4567Szwb0x00 SYS_SCHED_YIELD => Self::do_sched_yield(), 1011173c4567Szwb0x00 101202343d0bSLoGin SYS_SCHED_GETAFFINITY => { 10139430523bSyuyi2439 let pid = args[0] as i32; 10149430523bSyuyi2439 let size = args[1]; 10159430523bSyuyi2439 let set_vaddr = args[2]; 101602343d0bSLoGin 10179430523bSyuyi2439 let mut user_buffer_writer = 10189430523bSyuyi2439 UserBufferWriter::new(set_vaddr as *mut u8, size, frame.is_from_user())?; 10199430523bSyuyi2439 let set: &mut [u8] = user_buffer_writer.buffer(0)?; 10209430523bSyuyi2439 10219430523bSyuyi2439 Self::getaffinity(pid, set) 102202343d0bSLoGin } 102302343d0bSLoGin 102452da9a59SGnoCiYeH #[cfg(target_arch = "x86_64")] 102552da9a59SGnoCiYeH SYS_GETRLIMIT => { 102652da9a59SGnoCiYeH let resource = args[0]; 102752da9a59SGnoCiYeH let rlimit = args[1] as *mut RLimit64; 102852da9a59SGnoCiYeH 102952da9a59SGnoCiYeH Self::prlimit64( 103052da9a59SGnoCiYeH ProcessManager::current_pcb().pid(), 103152da9a59SGnoCiYeH resource, 1032840045afSLoGin core::ptr::null::<RLimit64>(), 103352da9a59SGnoCiYeH rlimit, 103452da9a59SGnoCiYeH ) 103552da9a59SGnoCiYeH } 103652da9a59SGnoCiYeH 10379e481b3bSTTaq SYS_FADVISE64 => { 10389e481b3bSTTaq // todo: 这个系统调用还没有实现 10399e481b3bSTTaq 10409e481b3bSTTaq Err(SystemError::ENOSYS) 10419e481b3bSTTaq } 10421d37ca6dSDonkey Kane 10431d37ca6dSDonkey Kane SYS_MOUNT => { 10441d37ca6dSDonkey Kane let source = args[0] as *const u8; 10451d37ca6dSDonkey Kane let target = args[1] as *const u8; 10461d37ca6dSDonkey Kane let filesystemtype = args[2] as *const u8; 10471d37ca6dSDonkey Kane return Self::mount(source, target, filesystemtype, 0, null()); 10481d37ca6dSDonkey Kane } 10491074eb34SSamuel Dai 10501074eb34SSamuel Dai SYS_UMOUNT2 => { 10511074eb34SSamuel Dai let target = args[0] as *const u8; 10521074eb34SSamuel Dai let flags = args[1] as i32; 10531074eb34SSamuel Dai Self::umount2(target, flags)?; 10541074eb34SSamuel Dai return Ok(0); 10551074eb34SSamuel Dai } 10561074eb34SSamuel Dai 10579e481b3bSTTaq SYS_NEWFSTATAT => { 10589e481b3bSTTaq // todo: 这个系统调用还没有实现 10599e481b3bSTTaq 10609e481b3bSTTaq Err(SystemError::ENOSYS) 10619e481b3bSTTaq } 10629e481b3bSTTaq 1063f0c87a89SGnoCiYeH // SYS_SCHED_YIELD => Self::sched_yield(), 10648c6f2184Ssspphh SYS_UNAME => { 10658c6f2184Ssspphh let name = args[0] as *mut PosixOldUtsName; 10668c6f2184Ssspphh Self::uname(name) 10678c6f2184Ssspphh } 1068c719ddc6SSaga1718 SYS_PRCTL => { 1069c719ddc6SSaga1718 // todo: 这个系统调用还没有实现 1070c719ddc6SSaga1718 1071c719ddc6SSaga1718 Err(SystemError::EINVAL) 1072c719ddc6SSaga1718 } 10731d37ca6dSDonkey Kane 1074731bc2b3SLoGin #[cfg(target_arch = "x86_64")] 1075fbd63a30SSMALLC SYS_ALARM => { 1076fbd63a30SSMALLC let second = args[0] as u32; 1077fbd63a30SSMALLC Self::alarm(second) 1078fbd63a30SSMALLC } 1079fbd63a30SSMALLC 10806fc066acSJomo SYS_SHMGET => { 10816fc066acSJomo let key = ShmKey::new(args[0]); 10826fc066acSJomo let size = args[1]; 10836fc066acSJomo let shmflg = ShmFlags::from_bits_truncate(args[2] as u32); 10846fc066acSJomo 10856fc066acSJomo Self::shmget(key, size, shmflg) 10866fc066acSJomo } 10876fc066acSJomo SYS_SHMAT => { 10886fc066acSJomo let id = ShmId::new(args[0]); 10896fc066acSJomo let vaddr = VirtAddr::new(args[1]); 10906fc066acSJomo let shmflg = ShmFlags::from_bits_truncate(args[2] as u32); 10916fc066acSJomo 10926fc066acSJomo Self::shmat(id, vaddr, shmflg) 10936fc066acSJomo } 10946fc066acSJomo SYS_SHMDT => { 10956fc066acSJomo let vaddr = VirtAddr::new(args[0]); 10966fc066acSJomo Self::shmdt(vaddr) 10976fc066acSJomo } 10986fc066acSJomo SYS_SHMCTL => { 10996fc066acSJomo let id = ShmId::new(args[0]); 11006fc066acSJomo let cmd = ShmCtlCmd::from(args[1]); 11016fc066acSJomo let user_buf = args[2] as *const u8; 11026fc066acSJomo let from_user = frame.is_from_user(); 11036fc066acSJomo 11046fc066acSJomo Self::shmctl(id, cmd, user_buf, from_user) 11056fc066acSJomo } 1106*6f189d27Slinfeng SYS_UTIMENSAT => Self::sys_utimensat( 1107*6f189d27Slinfeng args[0] as i32, 1108*6f189d27Slinfeng args[1] as *const u8, 1109*6f189d27Slinfeng args[2] as *const PosixTimeSpec, 1110*6f189d27Slinfeng args[3] as u32, 1111*6f189d27Slinfeng ), 1112*6f189d27Slinfeng #[cfg(target_arch = "x86_64")] 1113*6f189d27Slinfeng SYS_FUTIMESAT => { 1114*6f189d27Slinfeng let flags = UtimensFlags::empty(); 1115*6f189d27Slinfeng Self::sys_utimensat( 1116*6f189d27Slinfeng args[0] as i32, 1117*6f189d27Slinfeng args[1] as *const u8, 1118*6f189d27Slinfeng args[2] as *const PosixTimeSpec, 1119*6f189d27Slinfeng flags.bits(), 1120*6f189d27Slinfeng ) 1121*6f189d27Slinfeng } 1122*6f189d27Slinfeng #[cfg(target_arch = "x86_64")] 1123*6f189d27Slinfeng SYS_UTIMES => Self::sys_utimes(args[0] as *const u8, args[1] as *const PosixTimeval), 1124ab5c8ca4Slogin _ => panic!("Unsupported syscall ID: {}", syscall_num), 1125ab5c8ca4Slogin }; 11264fda81ceSLoGin 1127f0c87a89SGnoCiYeH if ProcessManager::current_pcb() 1128f0c87a89SGnoCiYeH .flags() 1129f0c87a89SGnoCiYeH .contains(ProcessFlags::NEED_SCHEDULE) 1130f0c87a89SGnoCiYeH { 1131f0c87a89SGnoCiYeH schedule(SchedMode::SM_PREEMPT); 1132f0c87a89SGnoCiYeH } 1133f0c87a89SGnoCiYeH 1134ab5c8ca4Slogin return r; 1135ab5c8ca4Slogin } 1136ab5c8ca4Slogin 1137ab5c8ca4Slogin pub fn put_string( 1138ab5c8ca4Slogin s: *const u8, 1139ab5c8ca4Slogin front_color: u32, 1140ab5c8ca4Slogin back_color: u32, 1141ab5c8ca4Slogin ) -> Result<usize, SystemError> { 1142c3dc6f2fSLoGin // todo: 删除这个系统调用 1143c3dc6f2fSLoGin let s = check_and_clone_cstr(s, Some(4096))?; 1144c3dc6f2fSLoGin let fr = (front_color & 0x00ff0000) >> 16; 1145c3dc6f2fSLoGin let fg = (front_color & 0x0000ff00) >> 8; 1146c3dc6f2fSLoGin let fb = front_color & 0x000000ff; 1147c3dc6f2fSLoGin let br = (back_color & 0x00ff0000) >> 16; 1148c3dc6f2fSLoGin let bg = (back_color & 0x0000ff00) >> 8; 1149c3dc6f2fSLoGin let bb = back_color & 0x000000ff; 1150c3dc6f2fSLoGin print!("\x1B[38;2;{fr};{fg};{fb};48;2;{br};{bg};{bb}m{s}\x1B[0m"); 1151c3dc6f2fSLoGin return Ok(s.len()); 1152ab5c8ca4Slogin } 1153ab5c8ca4Slogin 1154ab5c8ca4Slogin pub fn reboot() -> Result<usize, SystemError> { 11554fda81ceSLoGin unsafe { cpu_reset() }; 1156ab5c8ca4Slogin } 1157ab5c8ca4Slogin } 1158