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 2391e9d4abSLoGin use num_traits::FromPrimitive; 2491e9d4abSLoGin use system_error::SystemError; 25676b8ef6SMork 26ab5c8ca4Slogin use crate::{ 271496ba7bSLoGin arch::{cpu::cpu_reset, interrupt::TrapFrame, MMArch}, 28ab5c8ca4Slogin filesystem::vfs::{ 299e481b3bSTTaq fcntl::{AtFlags, FcntlCommand}, 30ab5c8ca4Slogin file::FileMode, 3182df0a13Shmt syscall::{ModeType, PosixKstat}, 32ab5c8ca4Slogin MAX_PATHLEN, 33ab5c8ca4Slogin }, 34aa0367d6SLoGin kinfo, 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 } 725b59005fSLoGin kinfo!("Initializing syscall..."); 735b59005fSLoGin let r = crate::arch::syscall::arch_syscall_init(); 745b59005fSLoGin kinfo!("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; 3723c82aa56SChiichen // kdebug!("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 => { 386f0c87a89SGnoCiYeH kwarn!("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 402ab5c8ca4Slogin SYS_SOCKET => Self::socket(args[0], args[1], args[2]), 403ab5c8ca4Slogin SYS_SETSOCKOPT => { 404ab5c8ca4Slogin let optval = args[3] as *const u8; 405b5b571e0SLoGin let optlen = args[4]; 406be63f3b2Shoumkh let virt_optval = VirtAddr::new(optval as usize); 407ab5c8ca4Slogin // 验证optval的地址是否合法 408b5b571e0SLoGin if verify_area(virt_optval, optlen).is_err() { 409ab5c8ca4Slogin // 地址空间超出了用户空间的范围,不合法 410ab5c8ca4Slogin Err(SystemError::EFAULT) 411ab5c8ca4Slogin } else { 412ab5c8ca4Slogin let data: &[u8] = unsafe { core::slice::from_raw_parts(optval, optlen) }; 413ab5c8ca4Slogin Self::setsockopt(args[0], args[1], args[2], data) 414ab5c8ca4Slogin } 415ab5c8ca4Slogin } 416ab5c8ca4Slogin SYS_GETSOCKOPT => { 417ab5c8ca4Slogin let optval = args[3] as *mut u8; 418ab5c8ca4Slogin let optlen = args[4] as *mut usize; 419be63f3b2Shoumkh let virt_optval = VirtAddr::new(optval as usize); 420be63f3b2Shoumkh let virt_optlen = VirtAddr::new(optlen as usize); 421ab5c8ca4Slogin let security_check = || { 422ab5c8ca4Slogin // 验证optval的地址是否合法 423370472f7SLoGin if verify_area(virt_optval, PAGE_4K_SIZE).is_err() { 424ab5c8ca4Slogin // 地址空间超出了用户空间的范围,不合法 425ab5c8ca4Slogin return Err(SystemError::EFAULT); 426ab5c8ca4Slogin } 427ab5c8ca4Slogin 428ab5c8ca4Slogin // 验证optlen的地址是否合法 429b5b571e0SLoGin if verify_area(virt_optlen, core::mem::size_of::<u32>()).is_err() { 430ab5c8ca4Slogin // 地址空间超出了用户空间的范围,不合法 431ab5c8ca4Slogin return Err(SystemError::EFAULT); 432ab5c8ca4Slogin } 433ab5c8ca4Slogin return Ok(()); 434ab5c8ca4Slogin }; 435ab5c8ca4Slogin let r = security_check(); 436b5b571e0SLoGin if let Err(e) = r { 437b5b571e0SLoGin Err(e) 438ab5c8ca4Slogin } else { 439ab5c8ca4Slogin Self::getsockopt(args[0], args[1], args[2], optval, optlen as *mut u32) 440ab5c8ca4Slogin } 441ab5c8ca4Slogin } 442ab5c8ca4Slogin 443ab5c8ca4Slogin SYS_CONNECT => { 444ab5c8ca4Slogin let addr = args[1] as *const SockAddr; 445b5b571e0SLoGin let addrlen = args[2]; 446be63f3b2Shoumkh let virt_addr = VirtAddr::new(addr as usize); 447ab5c8ca4Slogin // 验证addr的地址是否合法 448b5b571e0SLoGin if verify_area(virt_addr, addrlen).is_err() { 449ab5c8ca4Slogin // 地址空间超出了用户空间的范围,不合法 450ab5c8ca4Slogin Err(SystemError::EFAULT) 451ab5c8ca4Slogin } else { 452ab5c8ca4Slogin Self::connect(args[0], addr, addrlen) 453ab5c8ca4Slogin } 454ab5c8ca4Slogin } 455ab5c8ca4Slogin SYS_BIND => { 456ab5c8ca4Slogin let addr = args[1] as *const SockAddr; 457b5b571e0SLoGin let addrlen = args[2]; 458be63f3b2Shoumkh let virt_addr = VirtAddr::new(addr as usize); 459ab5c8ca4Slogin // 验证addr的地址是否合法 460b5b571e0SLoGin if verify_area(virt_addr, addrlen).is_err() { 461ab5c8ca4Slogin // 地址空间超出了用户空间的范围,不合法 462ab5c8ca4Slogin Err(SystemError::EFAULT) 463ab5c8ca4Slogin } else { 464ab5c8ca4Slogin Self::bind(args[0], addr, addrlen) 465ab5c8ca4Slogin } 466ab5c8ca4Slogin } 467ab5c8ca4Slogin 468ab5c8ca4Slogin SYS_SENDTO => { 469ab5c8ca4Slogin let buf = args[1] as *const u8; 470b5b571e0SLoGin let len = args[2]; 471ab5c8ca4Slogin let flags = args[3] as u32; 472ab5c8ca4Slogin let addr = args[4] as *const SockAddr; 473b5b571e0SLoGin let addrlen = args[5]; 474be63f3b2Shoumkh let virt_buf = VirtAddr::new(buf as usize); 475be63f3b2Shoumkh let virt_addr = VirtAddr::new(addr as usize); 476ab5c8ca4Slogin // 验证buf的地址是否合法 477b5b571e0SLoGin if verify_area(virt_buf, len).is_err() || verify_area(virt_addr, addrlen).is_err() { 478ab5c8ca4Slogin // 地址空间超出了用户空间的范围,不合法 479ab5c8ca4Slogin Err(SystemError::EFAULT) 480ab5c8ca4Slogin } else { 481ab5c8ca4Slogin let data: &[u8] = unsafe { core::slice::from_raw_parts(buf, len) }; 482ab5c8ca4Slogin Self::sendto(args[0], data, flags, addr, addrlen) 483ab5c8ca4Slogin } 484ab5c8ca4Slogin } 485ab5c8ca4Slogin 486ab5c8ca4Slogin SYS_RECVFROM => { 487ab5c8ca4Slogin let buf = args[1] as *mut u8; 488b5b571e0SLoGin let len = args[2]; 489ab5c8ca4Slogin let flags = args[3] as u32; 490ab5c8ca4Slogin let addr = args[4] as *mut SockAddr; 491ab5c8ca4Slogin let addrlen = args[5] as *mut usize; 492be63f3b2Shoumkh let virt_buf = VirtAddr::new(buf as usize); 493be63f3b2Shoumkh let virt_addrlen = VirtAddr::new(addrlen as usize); 494be63f3b2Shoumkh let virt_addr = VirtAddr::new(addr as usize); 495ab5c8ca4Slogin let security_check = || { 496ab5c8ca4Slogin // 验证buf的地址是否合法 497840045afSLoGin if verify_area(virt_buf, len).is_err() { 498ab5c8ca4Slogin // 地址空间超出了用户空间的范围,不合法 499ab5c8ca4Slogin return Err(SystemError::EFAULT); 500ab5c8ca4Slogin } 501ab5c8ca4Slogin 502ab5c8ca4Slogin // 验证addrlen的地址是否合法 503840045afSLoGin if verify_area(virt_addrlen, core::mem::size_of::<u32>()).is_err() { 504ab5c8ca4Slogin // 地址空间超出了用户空间的范围,不合法 505ab5c8ca4Slogin return Err(SystemError::EFAULT); 506ab5c8ca4Slogin } 507ab5c8ca4Slogin 508840045afSLoGin if verify_area(virt_addr, core::mem::size_of::<SockAddr>()).is_err() { 509ab5c8ca4Slogin // 地址空间超出了用户空间的范围,不合法 510ab5c8ca4Slogin return Err(SystemError::EFAULT); 511ab5c8ca4Slogin } 512ab5c8ca4Slogin return Ok(()); 513ab5c8ca4Slogin }; 514ab5c8ca4Slogin let r = security_check(); 515b5b571e0SLoGin if let Err(e) = r { 516b5b571e0SLoGin Err(e) 517ab5c8ca4Slogin } else { 518ab5c8ca4Slogin let buf = unsafe { core::slice::from_raw_parts_mut(buf, len) }; 519ab5c8ca4Slogin Self::recvfrom(args[0], buf, flags, addr, addrlen as *mut u32) 520ab5c8ca4Slogin } 521ab5c8ca4Slogin } 522ab5c8ca4Slogin 523ab5c8ca4Slogin SYS_RECVMSG => { 5244ad52e57S裕依2439 let msg = args[1] as *mut MsgHdr; 525ab5c8ca4Slogin let flags = args[2] as u32; 5264ad52e57S裕依2439 527b5b571e0SLoGin let mut user_buffer_writer = UserBufferWriter::new( 528b5b571e0SLoGin msg, 529b5b571e0SLoGin core::mem::size_of::<MsgHdr>(), 530b5b571e0SLoGin frame.is_from_user(), 531b5b571e0SLoGin )?; 5324ad52e57S裕依2439 let buffer = user_buffer_writer.buffer::<MsgHdr>(0)?; 5334ad52e57S裕依2439 5344537ffb7SChiichen let msg = &mut buffer[0]; 535ab5c8ca4Slogin Self::recvmsg(args[0], msg, flags) 536ab5c8ca4Slogin } 537ab5c8ca4Slogin 538ab5c8ca4Slogin SYS_LISTEN => Self::listen(args[0], args[1]), 539ab5c8ca4Slogin SYS_SHUTDOWN => Self::shutdown(args[0], args[1]), 540ab5c8ca4Slogin SYS_ACCEPT => Self::accept(args[0], args[1] as *mut SockAddr, args[2] as *mut u32), 541c47fe904SLoGin SYS_ACCEPT4 => Self::accept4( 542c47fe904SLoGin args[0], 543c47fe904SLoGin args[1] as *mut SockAddr, 544c47fe904SLoGin args[2] as *mut u32, 545c47fe904SLoGin args[3] as u32, 546c47fe904SLoGin ), 547ab5c8ca4Slogin SYS_GETSOCKNAME => { 548ab5c8ca4Slogin Self::getsockname(args[0], args[1] as *mut SockAddr, args[2] as *mut u32) 549ab5c8ca4Slogin } 550ab5c8ca4Slogin SYS_GETPEERNAME => { 551ab5c8ca4Slogin Self::getpeername(args[0], args[1] as *mut SockAddr, args[2] as *mut u32) 552ab5c8ca4Slogin } 55336fd0130Shoumkh SYS_GETTIMEOFDAY => { 55436fd0130Shoumkh let timeval = args[0] as *mut PosixTimeval; 55540fe15e0SLoGin let timezone_ptr = args[1] as *mut PosixTimeZone; 55640fe15e0SLoGin Self::gettimeofday(timeval, timezone_ptr) 5574537ffb7SChiichen } 55840fe15e0SLoGin SYS_MMAP => { 55940fe15e0SLoGin let len = page_align_up(args[1]); 560840045afSLoGin let virt_addr = VirtAddr::new(args[0]); 561840045afSLoGin if verify_area(virt_addr, len).is_err() { 56240fe15e0SLoGin Err(SystemError::EFAULT) 56340fe15e0SLoGin } else { 56440fe15e0SLoGin Self::mmap( 56540fe15e0SLoGin VirtAddr::new(args[0]), 56640fe15e0SLoGin len, 56740fe15e0SLoGin args[2], 56840fe15e0SLoGin args[3], 56940fe15e0SLoGin args[4] as i32, 57040fe15e0SLoGin args[5], 57140fe15e0SLoGin ) 57240fe15e0SLoGin } 57340fe15e0SLoGin } 5744cfa009bSJomo SYS_MREMAP => { 5754cfa009bSJomo let old_vaddr = VirtAddr::new(args[0]); 5764cfa009bSJomo let old_len = args[1]; 5774cfa009bSJomo let new_len = args[2]; 5784cfa009bSJomo let mremap_flags = MremapFlags::from_bits_truncate(args[3] as u8); 5794cfa009bSJomo let new_vaddr = VirtAddr::new(args[4]); 5804cfa009bSJomo 5814cfa009bSJomo Self::mremap(old_vaddr, old_len, new_len, mremap_flags, new_vaddr) 5824cfa009bSJomo } 58340fe15e0SLoGin SYS_MUNMAP => { 58440fe15e0SLoGin let addr = args[0]; 58540fe15e0SLoGin let len = page_align_up(args[1]); 586971462beSGnoCiYeH if addr & (MMArch::PAGE_SIZE - 1) != 0 { 58740fe15e0SLoGin // The addr argument is not a multiple of the page size 58840fe15e0SLoGin Err(SystemError::EINVAL) 58940fe15e0SLoGin } else { 59040fe15e0SLoGin Self::munmap(VirtAddr::new(addr), len) 59140fe15e0SLoGin } 59240fe15e0SLoGin } 59340fe15e0SLoGin SYS_MPROTECT => { 59440fe15e0SLoGin let addr = args[0]; 59540fe15e0SLoGin let len = page_align_up(args[1]); 596971462beSGnoCiYeH if addr & (MMArch::PAGE_SIZE - 1) != 0 { 59740fe15e0SLoGin // The addr argument is not a multiple of the page size 59840fe15e0SLoGin Err(SystemError::EINVAL) 59940fe15e0SLoGin } else { 60040fe15e0SLoGin Self::mprotect(VirtAddr::new(addr), len, args[2]) 60140fe15e0SLoGin } 60240fe15e0SLoGin } 60340fe15e0SLoGin 6041496ba7bSLoGin SYS_GETCWD => { 6051496ba7bSLoGin let buf = args[0] as *mut u8; 606840045afSLoGin let size = args[1]; 6071496ba7bSLoGin let security_check = || { 6081496ba7bSLoGin verify_area(VirtAddr::new(buf as usize), size)?; 6091496ba7bSLoGin return Ok(()); 6101496ba7bSLoGin }; 6111496ba7bSLoGin let r = security_check(); 612b5b571e0SLoGin if let Err(e) = r { 613b5b571e0SLoGin Err(e) 6141496ba7bSLoGin } else { 6151496ba7bSLoGin let buf = unsafe { core::slice::from_raw_parts_mut(buf, size) }; 6161496ba7bSLoGin Self::getcwd(buf).map(|ptr| ptr.data()) 6171496ba7bSLoGin } 6181496ba7bSLoGin } 6191496ba7bSLoGin 6201496ba7bSLoGin SYS_GETPGID => Self::getpgid(Pid::new(args[0])).map(|pid| pid.into()), 6211496ba7bSLoGin 6221496ba7bSLoGin SYS_GETPPID => Self::getppid().map(|pid| pid.into()), 623be63f3b2Shoumkh SYS_FSTAT => { 624be63f3b2Shoumkh let fd = args[0] as i32; 625b5b571e0SLoGin let kstat: *mut PosixKstat = args[1] as *mut PosixKstat; 626be63f3b2Shoumkh let vaddr = VirtAddr::new(kstat as usize); 627be63f3b2Shoumkh // FIXME 由于c中的verify_area与rust中的verify_area重名,所以在引入时加了前缀区分 628be63f3b2Shoumkh // TODO 应该将用了c版本的verify_area都改为rust的verify_area 629be63f3b2Shoumkh match verify_area(vaddr, core::mem::size_of::<PosixKstat>()) { 630be63f3b2Shoumkh Ok(_) => Self::fstat(fd, kstat), 631be63f3b2Shoumkh Err(e) => Err(e), 632be63f3b2Shoumkh } 633be63f3b2Shoumkh } 634be63f3b2Shoumkh 6356d81180bSLoGin SYS_FCNTL => { 6366d81180bSLoGin let fd = args[0] as i32; 6376d81180bSLoGin let cmd: Option<FcntlCommand> = 6386d81180bSLoGin <FcntlCommand as FromPrimitive>::from_u32(args[1] as u32); 6396d81180bSLoGin let arg = args[2] as i32; 6406d81180bSLoGin let res = if let Some(cmd) = cmd { 6416d81180bSLoGin Self::fcntl(fd, cmd, arg) 6426d81180bSLoGin } else { 6436d81180bSLoGin Err(SystemError::EINVAL) 6446d81180bSLoGin }; 6456d81180bSLoGin 6466d81180bSLoGin // kdebug!("FCNTL: fd: {}, cmd: {:?}, arg: {}, res: {:?}", fd, cmd, arg, res); 6476d81180bSLoGin res 6486d81180bSLoGin } 6496d81180bSLoGin 6506d81180bSLoGin SYS_FTRUNCATE => { 6516d81180bSLoGin let fd = args[0] as i32; 652840045afSLoGin let len = args[1]; 6536d81180bSLoGin let res = Self::ftruncate(fd, len); 6546d81180bSLoGin // kdebug!("FTRUNCATE: fd: {}, len: {}, res: {:?}", fd, len, res); 6556d81180bSLoGin res 6566d81180bSLoGin } 6576d81180bSLoGin 6584fda81ceSLoGin #[cfg(target_arch = "x86_64")] 6592dbef785SGnoCiYeH SYS_MKNOD => { 6602dbef785SGnoCiYeH let path = args[0]; 6612dbef785SGnoCiYeH let flags = args[1]; 6622dbef785SGnoCiYeH let dev_t = args[2]; 6632dbef785SGnoCiYeH let flags: ModeType = ModeType::from_bits_truncate(flags as u32); 664*731bc2b3SLoGin Self::mknod( 665*731bc2b3SLoGin path as *const u8, 666*731bc2b3SLoGin flags, 667*731bc2b3SLoGin crate::driver::base::device::device_number::DeviceNumber::from(dev_t as u32), 668*731bc2b3SLoGin ) 6692dbef785SGnoCiYeH } 6702dbef785SGnoCiYeH 671971462beSGnoCiYeH SYS_CLONE => { 672971462beSGnoCiYeH let parent_tid = VirtAddr::new(args[2]); 673971462beSGnoCiYeH let child_tid = VirtAddr::new(args[3]); 674971462beSGnoCiYeH 675971462beSGnoCiYeH // 地址校验 676971462beSGnoCiYeH verify_area(parent_tid, core::mem::size_of::<i32>())?; 677971462beSGnoCiYeH verify_area(child_tid, core::mem::size_of::<i32>())?; 678971462beSGnoCiYeH 679971462beSGnoCiYeH let mut clone_args = KernelCloneArgs::new(); 680971462beSGnoCiYeH clone_args.flags = CloneFlags::from_bits_truncate(args[0] as u64); 681971462beSGnoCiYeH clone_args.stack = args[1]; 682971462beSGnoCiYeH clone_args.parent_tid = parent_tid; 683971462beSGnoCiYeH clone_args.child_tid = child_tid; 684971462beSGnoCiYeH clone_args.tls = args[4]; 685971462beSGnoCiYeH Self::clone(frame, clone_args) 686971462beSGnoCiYeH } 687971462beSGnoCiYeH 688971462beSGnoCiYeH SYS_FUTEX => { 689971462beSGnoCiYeH let uaddr = VirtAddr::new(args[0]); 690971462beSGnoCiYeH let operation = FutexFlag::from_bits(args[1] as u32).ok_or(SystemError::ENOSYS)?; 691971462beSGnoCiYeH let val = args[2] as u32; 692971462beSGnoCiYeH let utime = args[3]; 693971462beSGnoCiYeH let uaddr2 = VirtAddr::new(args[4]); 694971462beSGnoCiYeH let val3 = args[5] as u32; 695971462beSGnoCiYeH 696971462beSGnoCiYeH let mut timespec = None; 697971462beSGnoCiYeH if utime != 0 && operation.contains(FutexFlag::FLAGS_HAS_TIMEOUT) { 698971462beSGnoCiYeH let reader = UserBufferReader::new( 6996fc066acSJomo utime as *const PosixTimeSpec, 7006fc066acSJomo core::mem::size_of::<PosixTimeSpec>(), 701971462beSGnoCiYeH true, 702971462beSGnoCiYeH )?; 703971462beSGnoCiYeH 7046fc066acSJomo timespec = Some(*reader.read_one_from_user::<PosixTimeSpec>(0)?); 705971462beSGnoCiYeH } 706971462beSGnoCiYeH 707971462beSGnoCiYeH Self::do_futex(uaddr, operation, val, timespec, uaddr2, utime as u32, val3) 708971462beSGnoCiYeH } 709971462beSGnoCiYeH 71006560afaShmt SYS_SET_ROBUST_LIST => { 71106560afaShmt let head = args[0]; 71206560afaShmt let head_uaddr = VirtAddr::new(head); 71306560afaShmt let len = args[1]; 71406560afaShmt 71506560afaShmt let ret = Self::set_robust_list(head_uaddr, len); 71606560afaShmt return ret; 71706560afaShmt } 71806560afaShmt 71906560afaShmt SYS_GET_ROBUST_LIST => { 72006560afaShmt let pid = args[0]; 72106560afaShmt let head = args[1]; 72206560afaShmt let head_uaddr = VirtAddr::new(head); 72306560afaShmt let len_ptr = args[2]; 72406560afaShmt let len_ptr_uaddr = VirtAddr::new(len_ptr); 72506560afaShmt 72606560afaShmt let ret = Self::get_robust_list(pid, head_uaddr, len_ptr_uaddr); 72706560afaShmt return ret; 72806560afaShmt } 72906560afaShmt 730bf4a4899SLoGin SYS_READV => Self::readv(args[0] as i32, args[1], args[2]), 731971462beSGnoCiYeH SYS_WRITEV => Self::writev(args[0] as i32, args[1], args[2]), 732971462beSGnoCiYeH 7334fda81ceSLoGin SYS_SET_TID_ADDRESS => Self::set_tid_address(args[0]), 734971462beSGnoCiYeH 7354fda81ceSLoGin #[cfg(target_arch = "x86_64")] 7364fda81ceSLoGin SYS_LSTAT => { 73782df0a13Shmt let path = args[0] as *const u8; 738971462beSGnoCiYeH let kstat = args[1] as *mut PosixKstat; 73982df0a13Shmt Self::lstat(path, kstat) 7404fda81ceSLoGin } 7414fda81ceSLoGin 7424fda81ceSLoGin #[cfg(target_arch = "x86_64")] 7434fda81ceSLoGin SYS_STAT => { 74482df0a13Shmt let path = args[0] as *const u8; 7454fda81ceSLoGin let kstat = args[1] as *mut PosixKstat; 74682df0a13Shmt Self::stat(path, kstat) 747971462beSGnoCiYeH } 748971462beSGnoCiYeH 749597ecc08STTaq SYS_STATFS => { 750597ecc08STTaq let path = args[0] as *const u8; 751597ecc08STTaq let statfs = args[1] as *mut PosixStatfs; 752597ecc08STTaq Self::statfs(path, statfs) 753597ecc08STTaq } 754597ecc08STTaq 755597ecc08STTaq SYS_FSTATFS => { 756597ecc08STTaq let fd = args[0] as i32; 757597ecc08STTaq let statfs = args[1] as *mut PosixStatfs; 758597ecc08STTaq Self::fstatfs(fd, statfs) 759597ecc08STTaq } 760597ecc08STTaq 761b4eb05a1STTaq SYS_STATX => { 762b4eb05a1STTaq let fd = args[0] as i32; 763b4eb05a1STTaq let path = args[1] as *const u8; 764b4eb05a1STTaq let flags = args[2] as u32; 765b4eb05a1STTaq let mask = args[3] as u32; 766b4eb05a1STTaq let kstat = args[4] as *mut PosixStatx; 767b4eb05a1STTaq 768b4eb05a1STTaq Self::do_statx(fd, path, flags, mask, kstat) 769b4eb05a1STTaq } 770b4eb05a1STTaq 77170f159a3SLoGin #[cfg(target_arch = "x86_64")] 77240609970SGnoCiYeH SYS_EPOLL_CREATE => Self::epoll_create(args[0] as i32), 77340609970SGnoCiYeH SYS_EPOLL_CREATE1 => Self::epoll_create1(args[0]), 77440609970SGnoCiYeH 77540609970SGnoCiYeH SYS_EPOLL_CTL => Self::epoll_ctl( 77640609970SGnoCiYeH args[0] as i32, 77740609970SGnoCiYeH args[1], 77840609970SGnoCiYeH args[2] as i32, 77940609970SGnoCiYeH VirtAddr::new(args[3]), 78040609970SGnoCiYeH ), 78140609970SGnoCiYeH 78270f159a3SLoGin #[cfg(target_arch = "x86_64")] 78340609970SGnoCiYeH SYS_EPOLL_WAIT => Self::epoll_wait( 78440609970SGnoCiYeH args[0] as i32, 78540609970SGnoCiYeH VirtAddr::new(args[1]), 78640609970SGnoCiYeH args[2] as i32, 78740609970SGnoCiYeH args[3] as i32, 78840609970SGnoCiYeH ), 78940609970SGnoCiYeH 79040609970SGnoCiYeH SYS_EPOLL_PWAIT => { 79140609970SGnoCiYeH let epfd = args[0] as i32; 79240609970SGnoCiYeH let epoll_event = VirtAddr::new(args[1]); 79340609970SGnoCiYeH let max_events = args[2] as i32; 79440609970SGnoCiYeH let timespec = args[3] as i32; 79540609970SGnoCiYeH let sigmask_addr = args[4] as *mut SigSet; 79640609970SGnoCiYeH 79740609970SGnoCiYeH if sigmask_addr.is_null() { 79840609970SGnoCiYeH return Self::epoll_wait(epfd, epoll_event, max_events, timespec); 79940609970SGnoCiYeH } 80040609970SGnoCiYeH let sigmask_reader = 80140609970SGnoCiYeH UserBufferReader::new(sigmask_addr, core::mem::size_of::<SigSet>(), true)?; 802840045afSLoGin let mut sigmask = *sigmask_reader.read_one_from_user::<SigSet>(0)?; 80340609970SGnoCiYeH 80440609970SGnoCiYeH Self::epoll_pwait( 80540609970SGnoCiYeH args[0] as i32, 80640609970SGnoCiYeH VirtAddr::new(args[1]), 80740609970SGnoCiYeH args[2] as i32, 80840609970SGnoCiYeH args[3] as i32, 80940609970SGnoCiYeH &mut sigmask, 81040609970SGnoCiYeH ) 81140609970SGnoCiYeH } 81240609970SGnoCiYeH 813971462beSGnoCiYeH // 目前为了适配musl-libc,以下系统调用先这样写着 8144fda81ceSLoGin SYS_GETRANDOM => { 815971462beSGnoCiYeH let flags = GRandFlags::from_bits(args[2] as u8).ok_or(SystemError::EINVAL)?; 816971462beSGnoCiYeH Self::get_random(args[0] as *mut u8, args[1], flags) 817971462beSGnoCiYeH } 818971462beSGnoCiYeH 8194fda81ceSLoGin SYS_SOCKETPAIR => { 8204ad52e57S裕依2439 let mut user_buffer_writer = UserBufferWriter::new( 8214ad52e57S裕依2439 args[3] as *mut c_int, 8224ad52e57S裕依2439 core::mem::size_of::<[c_int; 2]>(), 823b5b571e0SLoGin frame.is_from_user(), 8244ad52e57S裕依2439 )?; 8254ad52e57S裕依2439 let fds = user_buffer_writer.buffer::<i32>(0)?; 8264ad52e57S裕依2439 Self::socketpair(args[0], args[1], args[2], fds) 827971462beSGnoCiYeH } 828971462beSGnoCiYeH 8294fda81ceSLoGin #[cfg(target_arch = "x86_64")] 830971462beSGnoCiYeH SYS_POLL => { 831971462beSGnoCiYeH kwarn!("SYS_POLL has not yet been implemented"); 832971462beSGnoCiYeH Ok(0) 833971462beSGnoCiYeH } 834971462beSGnoCiYeH 83552da9a59SGnoCiYeH SYS_SETPGID => { 83652da9a59SGnoCiYeH kwarn!("SYS_SETPGID has not yet been implemented"); 83752da9a59SGnoCiYeH Ok(0) 83852da9a59SGnoCiYeH } 83952da9a59SGnoCiYeH 840971462beSGnoCiYeH SYS_RT_SIGPROCMASK => { 841971462beSGnoCiYeH kwarn!("SYS_RT_SIGPROCMASK has not yet been implemented"); 842971462beSGnoCiYeH Ok(0) 843971462beSGnoCiYeH } 844971462beSGnoCiYeH 845971462beSGnoCiYeH SYS_TKILL => { 846971462beSGnoCiYeH kwarn!("SYS_TKILL has not yet been implemented"); 847971462beSGnoCiYeH Ok(0) 848971462beSGnoCiYeH } 849971462beSGnoCiYeH 850971462beSGnoCiYeH SYS_SIGALTSTACK => { 851971462beSGnoCiYeH kwarn!("SYS_SIGALTSTACK has not yet been implemented"); 852971462beSGnoCiYeH Ok(0) 853971462beSGnoCiYeH } 854971462beSGnoCiYeH 85516033951SGnoCiYeH SYS_EXIT_GROUP => { 85616033951SGnoCiYeH kwarn!("SYS_EXIT_GROUP has not yet been implemented"); 85716033951SGnoCiYeH Ok(0) 85816033951SGnoCiYeH } 85916033951SGnoCiYeH 86004babc3fSMemoryShore SYS_MADVISE => { 861a17651b1SMemoryShore let addr = args[0]; 862a17651b1SMemoryShore let len = page_align_up(args[1]); 863a17651b1SMemoryShore if addr & (MMArch::PAGE_SIZE - 1) != 0 { 864a17651b1SMemoryShore Err(SystemError::EINVAL) 865a17651b1SMemoryShore } else { 866a17651b1SMemoryShore Self::madvise(VirtAddr::new(addr), len, args[2]) 86704babc3fSMemoryShore } 868a17651b1SMemoryShore } 869a17651b1SMemoryShore 870393f6915SLoGin SYS_GETTID => Self::gettid().map(|tid| tid.into()), 871840045afSLoGin SYS_GETUID => Self::getuid(), 8728d72b68dSJomo 87302e249f3SLoGin SYS_SYSLOG => { 874840045afSLoGin let syslog_action_type = args[0]; 8758d72b68dSJomo let buf_vaddr = args[1]; 8768d72b68dSJomo let len = args[2]; 877b5b571e0SLoGin let from_user = frame.is_from_user(); 8788d72b68dSJomo let mut user_buffer_writer = 8798d72b68dSJomo UserBufferWriter::new(buf_vaddr as *mut u8, len, from_user)?; 8808d72b68dSJomo 8818d72b68dSJomo let user_buf = user_buffer_writer.buffer(0)?; 882840045afSLoGin Self::do_syslog(syslog_action_type, user_buf, len) 88302e249f3SLoGin } 8848d72b68dSJomo 885840045afSLoGin SYS_GETGID => Self::getgid(), 88602e249f3SLoGin SYS_SETUID => { 88702e249f3SLoGin kwarn!("SYS_SETUID has not yet been implemented"); 88802e249f3SLoGin Ok(0) 88902e249f3SLoGin } 89002e249f3SLoGin SYS_SETGID => { 89102e249f3SLoGin kwarn!("SYS_SETGID has not yet been implemented"); 89202e249f3SLoGin Ok(0) 89302e249f3SLoGin } 894dfe53cf0SGnoCiYeH SYS_SETSID => { 895dfe53cf0SGnoCiYeH kwarn!("SYS_SETSID has not yet been implemented"); 896dfe53cf0SGnoCiYeH Ok(0) 897dfe53cf0SGnoCiYeH } 898840045afSLoGin SYS_GETEUID => Self::geteuid(), 899840045afSLoGin SYS_GETEGID => Self::getegid(), 900be8cdf4bSLoGin SYS_GETRUSAGE => { 901be8cdf4bSLoGin let who = args[0] as c_int; 902be8cdf4bSLoGin let rusage = args[1] as *mut RUsage; 903be8cdf4bSLoGin Self::get_rusage(who, rusage) 904be8cdf4bSLoGin } 90504babc3fSMemoryShore 9064fda81ceSLoGin #[cfg(target_arch = "x86_64")] 907709498caSLoGin SYS_READLINK => { 908709498caSLoGin let path = args[0] as *const u8; 909709498caSLoGin let buf = args[1] as *mut u8; 910840045afSLoGin let bufsiz = args[2]; 911709498caSLoGin Self::readlink(path, buf, bufsiz) 912709498caSLoGin } 913709498caSLoGin 9144fda81ceSLoGin SYS_READLINKAT => { 915709498caSLoGin let dirfd = args[0] as i32; 91682df0a13Shmt let path = args[1] as *const u8; 917709498caSLoGin let buf = args[2] as *mut u8; 918840045afSLoGin let bufsiz = args[3]; 91982df0a13Shmt Self::readlink_at(dirfd, path, buf, bufsiz) 920709498caSLoGin } 921709498caSLoGin 9220d9b7d92SLoGin SYS_PRLIMIT64 => { 9230d9b7d92SLoGin let pid = args[0]; 9240d9b7d92SLoGin let pid = Pid::new(pid); 9250d9b7d92SLoGin let resource = args[1]; 9260d9b7d92SLoGin let new_limit = args[2] as *const RLimit64; 9270d9b7d92SLoGin let old_limit = args[3] as *mut RLimit64; 9280d9b7d92SLoGin 9290d9b7d92SLoGin Self::prlimit64(pid, resource, new_limit, old_limit) 9300d9b7d92SLoGin } 9310d9b7d92SLoGin 9324fda81ceSLoGin #[cfg(target_arch = "x86_64")] 9339b0abe6dSLoGin SYS_ACCESS => { 9349b0abe6dSLoGin let pathname = args[0] as *const u8; 9359b0abe6dSLoGin let mode = args[1] as u32; 9369b0abe6dSLoGin Self::access(pathname, mode) 9379b0abe6dSLoGin } 9389b0abe6dSLoGin 9399b0abe6dSLoGin SYS_FACCESSAT => { 9409b0abe6dSLoGin let dirfd = args[0] as i32; 9419b0abe6dSLoGin let pathname = args[1] as *const u8; 9429b0abe6dSLoGin let mode = args[2] as u32; 9439b0abe6dSLoGin Self::faccessat2(dirfd, pathname, mode, 0) 9449b0abe6dSLoGin } 9459b0abe6dSLoGin 9469b0abe6dSLoGin SYS_FACCESSAT2 => { 9479b0abe6dSLoGin let dirfd = args[0] as i32; 9489b0abe6dSLoGin let pathname = args[1] as *const u8; 9499b0abe6dSLoGin let mode = args[2] as u32; 9509b0abe6dSLoGin let flags = args[3] as u32; 9519b0abe6dSLoGin Self::faccessat2(dirfd, pathname, mode, flags) 9529b0abe6dSLoGin } 9539b0abe6dSLoGin 954bf4a4899SLoGin SYS_CLOCK_GETTIME => { 955bf4a4899SLoGin let clockid = args[0] as i32; 9566fc066acSJomo let timespec = args[1] as *mut PosixTimeSpec; 957bf4a4899SLoGin Self::clock_gettime(clockid, timespec) 958bf4a4899SLoGin } 959bf4a4899SLoGin 960bf4a4899SLoGin SYS_SYSINFO => { 961bf4a4899SLoGin let info = args[0] as *mut SysInfo; 962bf4a4899SLoGin Self::sysinfo(info) 963bf4a4899SLoGin } 964bf4a4899SLoGin 965bf4a4899SLoGin SYS_UMASK => { 966bf4a4899SLoGin let mask = args[0] as u32; 967bf4a4899SLoGin Self::umask(mask) 968bf4a4899SLoGin } 969bf4a4899SLoGin 97027b967a3S裕依 SYS_FCHOWN => { 97127b967a3S裕依 kwarn!("SYS_FCHOWN has not yet been implemented"); 97227b967a3S裕依 Ok(0) 97327b967a3S裕依 } 97427b967a3S裕依 97527b967a3S裕依 SYS_FSYNC => { 97627b967a3S裕依 kwarn!("SYS_FSYNC has not yet been implemented"); 97727b967a3S裕依 Ok(0) 97827b967a3S裕依 } 97927b967a3S裕依 980dfe53cf0SGnoCiYeH SYS_RSEQ => { 981dfe53cf0SGnoCiYeH kwarn!("SYS_RSEQ has not yet been implemented"); 982dfe53cf0SGnoCiYeH Ok(0) 983dfe53cf0SGnoCiYeH } 984dfe53cf0SGnoCiYeH 9854fda81ceSLoGin #[cfg(target_arch = "x86_64")] 986bf4a4899SLoGin SYS_CHMOD => { 987bf4a4899SLoGin let pathname = args[0] as *const u8; 988bf4a4899SLoGin let mode = args[1] as u32; 989bf4a4899SLoGin Self::chmod(pathname, mode) 990bf4a4899SLoGin } 991bf4a4899SLoGin SYS_FCHMOD => { 992bf4a4899SLoGin let fd = args[0] as i32; 993bf4a4899SLoGin let mode = args[1] as u32; 994bf4a4899SLoGin Self::fchmod(fd, mode) 995bf4a4899SLoGin } 996bf4a4899SLoGin SYS_FCHMODAT => { 997bf4a4899SLoGin let dirfd = args[0] as i32; 998bf4a4899SLoGin let pathname = args[1] as *const u8; 999bf4a4899SLoGin let mode = args[2] as u32; 1000bf4a4899SLoGin Self::fchmodat(dirfd, pathname, mode) 1001bf4a4899SLoGin } 1002bf4a4899SLoGin 100302343d0bSLoGin SYS_SCHED_GETAFFINITY => { 10049430523bSyuyi2439 let pid = args[0] as i32; 10059430523bSyuyi2439 let size = args[1]; 10069430523bSyuyi2439 let set_vaddr = args[2]; 100702343d0bSLoGin 10089430523bSyuyi2439 let mut user_buffer_writer = 10099430523bSyuyi2439 UserBufferWriter::new(set_vaddr as *mut u8, size, frame.is_from_user())?; 10109430523bSyuyi2439 let set: &mut [u8] = user_buffer_writer.buffer(0)?; 10119430523bSyuyi2439 10129430523bSyuyi2439 Self::getaffinity(pid, set) 101302343d0bSLoGin } 101402343d0bSLoGin 101552da9a59SGnoCiYeH #[cfg(target_arch = "x86_64")] 101652da9a59SGnoCiYeH SYS_GETRLIMIT => { 101752da9a59SGnoCiYeH let resource = args[0]; 101852da9a59SGnoCiYeH let rlimit = args[1] as *mut RLimit64; 101952da9a59SGnoCiYeH 102052da9a59SGnoCiYeH Self::prlimit64( 102152da9a59SGnoCiYeH ProcessManager::current_pcb().pid(), 102252da9a59SGnoCiYeH resource, 1023840045afSLoGin core::ptr::null::<RLimit64>(), 102452da9a59SGnoCiYeH rlimit, 102552da9a59SGnoCiYeH ) 102652da9a59SGnoCiYeH } 102752da9a59SGnoCiYeH 10289e481b3bSTTaq SYS_FADVISE64 => { 10299e481b3bSTTaq // todo: 这个系统调用还没有实现 10309e481b3bSTTaq 10319e481b3bSTTaq Err(SystemError::ENOSYS) 10329e481b3bSTTaq } 10331d37ca6dSDonkey Kane 10341d37ca6dSDonkey Kane SYS_MOUNT => { 10351d37ca6dSDonkey Kane let source = args[0] as *const u8; 10361d37ca6dSDonkey Kane let target = args[1] as *const u8; 10371d37ca6dSDonkey Kane let filesystemtype = args[2] as *const u8; 10381d37ca6dSDonkey Kane return Self::mount(source, target, filesystemtype, 0, null()); 10391d37ca6dSDonkey Kane } 10401074eb34SSamuel Dai 10411074eb34SSamuel Dai SYS_UMOUNT2 => { 10421074eb34SSamuel Dai let target = args[0] as *const u8; 10431074eb34SSamuel Dai let flags = args[1] as i32; 10441074eb34SSamuel Dai Self::umount2(target, flags)?; 10451074eb34SSamuel Dai return Ok(0); 10461074eb34SSamuel Dai } 10471074eb34SSamuel Dai 10489e481b3bSTTaq SYS_NEWFSTATAT => { 10499e481b3bSTTaq // todo: 这个系统调用还没有实现 10509e481b3bSTTaq 10519e481b3bSTTaq Err(SystemError::ENOSYS) 10529e481b3bSTTaq } 10539e481b3bSTTaq 1054f0c87a89SGnoCiYeH // SYS_SCHED_YIELD => Self::sched_yield(), 10558c6f2184Ssspphh SYS_UNAME => { 10568c6f2184Ssspphh let name = args[0] as *mut PosixOldUtsName; 10578c6f2184Ssspphh Self::uname(name) 10588c6f2184Ssspphh } 1059c719ddc6SSaga1718 SYS_PRCTL => { 1060c719ddc6SSaga1718 // todo: 这个系统调用还没有实现 1061c719ddc6SSaga1718 1062c719ddc6SSaga1718 Err(SystemError::EINVAL) 1063c719ddc6SSaga1718 } 10641d37ca6dSDonkey Kane 1065*731bc2b3SLoGin #[cfg(target_arch = "x86_64")] 1066fbd63a30SSMALLC SYS_ALARM => { 1067fbd63a30SSMALLC let second = args[0] as u32; 1068fbd63a30SSMALLC Self::alarm(second) 1069fbd63a30SSMALLC } 1070fbd63a30SSMALLC 10716fc066acSJomo SYS_SHMGET => { 10726fc066acSJomo let key = ShmKey::new(args[0]); 10736fc066acSJomo let size = args[1]; 10746fc066acSJomo let shmflg = ShmFlags::from_bits_truncate(args[2] as u32); 10756fc066acSJomo 10766fc066acSJomo Self::shmget(key, size, shmflg) 10776fc066acSJomo } 10786fc066acSJomo SYS_SHMAT => { 10796fc066acSJomo let id = ShmId::new(args[0]); 10806fc066acSJomo let vaddr = VirtAddr::new(args[1]); 10816fc066acSJomo let shmflg = ShmFlags::from_bits_truncate(args[2] as u32); 10826fc066acSJomo 10836fc066acSJomo Self::shmat(id, vaddr, shmflg) 10846fc066acSJomo } 10856fc066acSJomo SYS_SHMDT => { 10866fc066acSJomo let vaddr = VirtAddr::new(args[0]); 10876fc066acSJomo Self::shmdt(vaddr) 10886fc066acSJomo } 10896fc066acSJomo SYS_SHMCTL => { 10906fc066acSJomo let id = ShmId::new(args[0]); 10916fc066acSJomo let cmd = ShmCtlCmd::from(args[1]); 10926fc066acSJomo let user_buf = args[2] as *const u8; 10936fc066acSJomo let from_user = frame.is_from_user(); 10946fc066acSJomo 10956fc066acSJomo Self::shmctl(id, cmd, user_buf, from_user) 10966fc066acSJomo } 10976fc066acSJomo 1098ab5c8ca4Slogin _ => panic!("Unsupported syscall ID: {}", syscall_num), 1099ab5c8ca4Slogin }; 11004fda81ceSLoGin 1101f0c87a89SGnoCiYeH if ProcessManager::current_pcb() 1102f0c87a89SGnoCiYeH .flags() 1103f0c87a89SGnoCiYeH .contains(ProcessFlags::NEED_SCHEDULE) 1104f0c87a89SGnoCiYeH { 1105f0c87a89SGnoCiYeH schedule(SchedMode::SM_PREEMPT); 1106f0c87a89SGnoCiYeH } 1107f0c87a89SGnoCiYeH 1108ab5c8ca4Slogin return r; 1109ab5c8ca4Slogin } 1110ab5c8ca4Slogin 1111ab5c8ca4Slogin pub fn put_string( 1112ab5c8ca4Slogin s: *const u8, 1113ab5c8ca4Slogin front_color: u32, 1114ab5c8ca4Slogin back_color: u32, 1115ab5c8ca4Slogin ) -> Result<usize, SystemError> { 1116c3dc6f2fSLoGin // todo: 删除这个系统调用 1117c3dc6f2fSLoGin let s = check_and_clone_cstr(s, Some(4096))?; 1118c3dc6f2fSLoGin let fr = (front_color & 0x00ff0000) >> 16; 1119c3dc6f2fSLoGin let fg = (front_color & 0x0000ff00) >> 8; 1120c3dc6f2fSLoGin let fb = front_color & 0x000000ff; 1121c3dc6f2fSLoGin let br = (back_color & 0x00ff0000) >> 16; 1122c3dc6f2fSLoGin let bg = (back_color & 0x0000ff00) >> 8; 1123c3dc6f2fSLoGin let bb = back_color & 0x000000ff; 1124c3dc6f2fSLoGin print!("\x1B[38;2;{fr};{fg};{fb};48;2;{br};{bg};{bb}m{s}\x1B[0m"); 1125c3dc6f2fSLoGin return Ok(s.len()); 1126ab5c8ca4Slogin } 1127ab5c8ca4Slogin 1128ab5c8ca4Slogin pub fn reboot() -> Result<usize, SystemError> { 11294fda81ceSLoGin unsafe { cpu_reset() }; 1130ab5c8ca4Slogin } 1131ab5c8ca4Slogin } 1132