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::*}, 9c566df45SLoGin driver::base::device::device_number::DeviceNumber, 10*b4eb05a1STTaq filesystem::vfs::syscall::PosixStatx, 11971462beSGnoCiYeH libs::{futex::constant::FutexFlag, rand::GRandFlags}, 124cfa009bSJomo mm::syscall::MremapFlags, 134ad52e57S裕依2439 net::syscall::MsgHdr, 140d9b7d92SLoGin process::{ 150d9b7d92SLoGin fork::KernelCloneArgs, 160d9b7d92SLoGin resource::{RLimit64, RUsage}, 1752da9a59SGnoCiYeH ProcessManager, 180d9b7d92SLoGin }, 19c3dc6f2fSLoGin syscall::user_access::check_and_clone_cstr, 20971462beSGnoCiYeH }; 2140314b30SXiaoye Zheng 2291e9d4abSLoGin use num_traits::FromPrimitive; 2391e9d4abSLoGin use system_error::SystemError; 24*b4eb05a1STTaq use uefi::proto::debug; 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 }, 3482df0a13Shmt include::bindings::bindings::PAGE_4K_SIZE, 35aa0367d6SLoGin kinfo, 3640fe15e0SLoGin libs::align::page_align_up, 37be63f3b2Shoumkh mm::{verify_area, MemoryManagementArch, VirtAddr}, 38ab5c8ca4Slogin net::syscall::SockAddr, 398c6f2184Ssspphh process::{fork::CloneFlags, syscall::PosixOldUtsName, Pid}, 4036fd0130Shoumkh time::{ 4140fe15e0SLoGin syscall::{PosixTimeZone, PosixTimeval}, 4236fd0130Shoumkh TimeSpec, 4336fd0130Shoumkh }, 44ab5c8ca4Slogin }; 45ab5c8ca4Slogin 46bf4a4899SLoGin use self::{ 47bf4a4899SLoGin misc::SysInfo, 48bf4a4899SLoGin user_access::{UserBufferReader, UserBufferWriter}, 49bf4a4899SLoGin }; 504537ffb7SChiichen 51bf4a4899SLoGin pub mod misc; 5240fe15e0SLoGin pub mod user_access; 5340fe15e0SLoGin 54ad1d649eSGnoCiYeH // 与linux不一致的调用,在linux基础上累加 55ad1d649eSGnoCiYeH pub const SYS_PUT_STRING: usize = 100000; 56ad1d649eSGnoCiYeH pub const SYS_SBRK: usize = 100001; 57ab5c8ca4Slogin /// todo: 该系统调用与Linux不一致,将来需要删除该系统调用!!! 删的时候记得改C版本的libc 58ad1d649eSGnoCiYeH pub const SYS_CLOCK: usize = 100002; 59ad1d649eSGnoCiYeH pub const SYS_SCHED: usize = 100003; 6040314b30SXiaoye Zheng 61ab5c8ca4Slogin #[derive(Debug)] 62ab5c8ca4Slogin pub struct Syscall; 63ab5c8ca4Slogin 64ab5c8ca4Slogin impl Syscall { 65ab5c8ca4Slogin /// 初始化系统调用 665b59005fSLoGin #[inline(never)] 67ab5c8ca4Slogin pub fn init() -> Result<(), SystemError> { 68ab5c8ca4Slogin static INIT_FLAG: AtomicBool = AtomicBool::new(false); 69ab5c8ca4Slogin let prev = INIT_FLAG.swap(true, Ordering::SeqCst); 70ab5c8ca4Slogin if prev { 71ab5c8ca4Slogin panic!("Cannot initialize syscall more than once!"); 72ab5c8ca4Slogin } 735b59005fSLoGin kinfo!("Initializing syscall..."); 745b59005fSLoGin let r = crate::arch::syscall::arch_syscall_init(); 755b59005fSLoGin kinfo!("Syscall init successfully!"); 765b59005fSLoGin 775b59005fSLoGin return r; 78ab5c8ca4Slogin } 79ab5c8ca4Slogin /// @brief 系统调用分发器,用于分发系统调用。 80ab5c8ca4Slogin /// 81ab5c8ca4Slogin /// 这个函数内,需要根据系统调用号,调用对应的系统调用处理函数。 82ab5c8ca4Slogin /// 并且,对于用户态传入的指针参数,需要在本函数内进行越界检查,防止访问到内核空间。 834fda81ceSLoGin #[inline(never)] 849e9ffedfSPlucky923 pub fn handle( 859e9ffedfSPlucky923 syscall_num: usize, 869e9ffedfSPlucky923 args: &[usize], 879e9ffedfSPlucky923 frame: &mut TrapFrame, 889e9ffedfSPlucky923 ) -> Result<usize, SystemError> { 89ab5c8ca4Slogin let r = match syscall_num { 90ab5c8ca4Slogin SYS_PUT_STRING => { 91ab5c8ca4Slogin Self::put_string(args[0] as *const u8, args[1] as u32, args[2] as u32) 92ab5c8ca4Slogin } 934fda81ceSLoGin #[cfg(target_arch = "x86_64")] 94ab5c8ca4Slogin SYS_OPEN => { 9582df0a13Shmt let path = args[0] as *const u8; 9682df0a13Shmt let flags = args[1] as u32; 9782df0a13Shmt let mode = args[2] as u32; 9834e6d6c8Syuyi2439 9982df0a13Shmt Self::open(path, flags, mode, true) 1000fb515b0SLoGin } 1010fb515b0SLoGin 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 1149e481b3bSTTaq SYS_RENAMEAT => { 1159e481b3bSTTaq let oldfd = args[0] as i32; 1169e481b3bSTTaq let oldname: *const u8 = args[1] as *const u8; 1179e481b3bSTTaq let newfd = args[2] as i32; 1189e481b3bSTTaq let newname: *const u8 = args[3] as *const u8; 1199e481b3bSTTaq Self::do_renameat2(oldfd, oldname, newfd, newname, 0) 1209e481b3bSTTaq } 1219e481b3bSTTaq 1229e481b3bSTTaq SYS_RENAMEAT2 => { 1239e481b3bSTTaq let oldfd = args[0] as i32; 1249e481b3bSTTaq let oldname: *const u8 = args[1] as *const u8; 1259e481b3bSTTaq let newfd = args[2] as i32; 1269e481b3bSTTaq let newname: *const u8 = args[3] as *const u8; 1279e481b3bSTTaq let flags = args[4] as u32; 1289e481b3bSTTaq Self::do_renameat2(oldfd, oldname, newfd, newname, flags) 1299e481b3bSTTaq } 1309e481b3bSTTaq 1310fb515b0SLoGin SYS_OPENAT => { 1320fb515b0SLoGin let dirfd = args[0] as i32; 13382df0a13Shmt let path = args[1] as *const u8; 13482df0a13Shmt let flags = args[2] as u32; 13582df0a13Shmt let mode = args[3] as u32; 1360fb515b0SLoGin 13782df0a13Shmt Self::openat(dirfd, path, flags, mode, true) 138ab5c8ca4Slogin } 139ab5c8ca4Slogin SYS_CLOSE => { 140ab5c8ca4Slogin let fd = args[0]; 14127b967a3S裕依 Self::close(fd) 142ab5c8ca4Slogin } 143ab5c8ca4Slogin SYS_READ => { 144ab5c8ca4Slogin let fd = args[0] as i32; 145ab5c8ca4Slogin let buf_vaddr = args[1]; 146ab5c8ca4Slogin let len = args[2]; 1479e9ffedfSPlucky923 let from_user = frame.from_user(); 1489e9ffedfSPlucky923 let mut user_buffer_writer = 1499e9ffedfSPlucky923 UserBufferWriter::new(buf_vaddr as *mut u8, len, from_user)?; 1506d81180bSLoGin 1519e9ffedfSPlucky923 let user_buf = user_buffer_writer.buffer(0)?; 15227b967a3S裕依 Self::read(fd, user_buf) 153ab5c8ca4Slogin } 154ab5c8ca4Slogin SYS_WRITE => { 155ab5c8ca4Slogin let fd = args[0] as i32; 156ab5c8ca4Slogin let buf_vaddr = args[1]; 157ab5c8ca4Slogin let len = args[2]; 1589e9ffedfSPlucky923 let from_user = frame.from_user(); 1599e9ffedfSPlucky923 let user_buffer_reader = 1609e9ffedfSPlucky923 UserBufferReader::new(buf_vaddr as *const u8, len, from_user)?; 1616d81180bSLoGin 1629e9ffedfSPlucky923 let user_buf = user_buffer_reader.read_from_user(0)?; 16327b967a3S裕依 Self::write(fd, user_buf) 164ab5c8ca4Slogin } 165ab5c8ca4Slogin 166ab5c8ca4Slogin SYS_LSEEK => { 167ab5c8ca4Slogin let fd = args[0] as i32; 168ab5c8ca4Slogin let offset = args[1] as i64; 169ab5c8ca4Slogin let whence = args[2] as u32; 170ab5c8ca4Slogin 17182df0a13Shmt Self::lseek(fd, offset, whence) 172ab5c8ca4Slogin } 17327b967a3S裕依 17427b967a3S裕依 SYS_PREAD64 => { 17527b967a3S裕依 let fd = args[0] as i32; 17627b967a3S裕依 let buf_vaddr = args[1]; 17727b967a3S裕依 let len = args[2]; 17827b967a3S裕依 let offset = args[3]; 17927b967a3S裕依 18027b967a3S裕依 let mut user_buffer_writer = 18127b967a3S裕依 UserBufferWriter::new(buf_vaddr as *mut u8, len, frame.from_user())?; 18227b967a3S裕依 let buf = user_buffer_writer.buffer(0)?; 18327b967a3S裕依 Self::pread(fd, buf, len, offset) 18427b967a3S裕依 } 18527b967a3S裕依 18627b967a3S裕依 SYS_PWRITE64 => { 18727b967a3S裕依 let fd = args[0] as i32; 18827b967a3S裕依 let buf_vaddr = args[1]; 18927b967a3S裕依 let len = args[2]; 19027b967a3S裕依 let offset = args[3]; 19127b967a3S裕依 19227b967a3S裕依 let user_buffer_reader = 19327b967a3S裕依 UserBufferReader::new(buf_vaddr as *const u8, len, frame.from_user())?; 19427b967a3S裕依 19527b967a3S裕依 let buf = user_buffer_reader.read_from_user(0)?; 19627b967a3S裕依 Self::pwrite(fd, buf, len, offset) 19727b967a3S裕依 } 19827b967a3S裕依 19940314b30SXiaoye Zheng SYS_IOCTL => { 20040314b30SXiaoye Zheng let fd = args[0]; 20140314b30SXiaoye Zheng let cmd = args[1]; 20240314b30SXiaoye Zheng let data = args[2]; 20340314b30SXiaoye Zheng Self::ioctl(fd, cmd as u32, data) 20440314b30SXiaoye Zheng } 205ab5c8ca4Slogin 2064fda81ceSLoGin #[cfg(target_arch = "x86_64")] 2071496ba7bSLoGin SYS_FORK => Self::fork(frame), 2084fda81ceSLoGin #[cfg(target_arch = "x86_64")] 2091496ba7bSLoGin SYS_VFORK => Self::vfork(frame), 2101496ba7bSLoGin 211ab5c8ca4Slogin SYS_BRK => { 21240fe15e0SLoGin let new_brk = VirtAddr::new(args[0]); 21340fe15e0SLoGin Self::brk(new_brk).map(|vaddr| vaddr.data()) 214ab5c8ca4Slogin } 215ab5c8ca4Slogin 216ab5c8ca4Slogin SYS_SBRK => { 217ab5c8ca4Slogin let increment = args[0] as isize; 2181496ba7bSLoGin Self::sbrk(increment).map(|vaddr: VirtAddr| vaddr.data()) 219ab5c8ca4Slogin } 220ab5c8ca4Slogin 221ab5c8ca4Slogin SYS_REBOOT => Self::reboot(), 222ab5c8ca4Slogin 223ab5c8ca4Slogin SYS_CHDIR => { 22482df0a13Shmt let r = args[0] as *const u8; 2259e9ffedfSPlucky923 Self::chdir(r) 226ab5c8ca4Slogin } 227ab5c8ca4Slogin 2284fda81ceSLoGin #[allow(unreachable_patterns)] 2294fda81ceSLoGin SYS_GETDENTS64 | SYS_GETDENTS => { 230ab5c8ca4Slogin let fd = args[0] as i32; 23134e6d6c8Syuyi2439 232ab5c8ca4Slogin let buf_vaddr = args[1]; 233ab5c8ca4Slogin let len = args[2]; 234f5df0e79SLoGin let virt_addr: VirtAddr = VirtAddr::new(buf_vaddr); 235ab5c8ca4Slogin // 判断缓冲区是否来自用户态,进行权限校验 2361496ba7bSLoGin let res = if frame.from_user() && verify_area(virt_addr, len as usize).is_err() { 237ab5c8ca4Slogin // 来自用户态,而buffer在内核态,这样的操作不被允许 238ab5c8ca4Slogin Err(SystemError::EPERM) 239ab5c8ca4Slogin } else if buf_vaddr == 0 { 240ab5c8ca4Slogin Err(SystemError::EFAULT) 241ab5c8ca4Slogin } else { 242ab5c8ca4Slogin let buf: &mut [u8] = unsafe { 243ab5c8ca4Slogin core::slice::from_raw_parts_mut::<'static, u8>(buf_vaddr as *mut u8, len) 244ab5c8ca4Slogin }; 245ab5c8ca4Slogin Self::getdents(fd, buf) 246ab5c8ca4Slogin }; 247ab5c8ca4Slogin 248ab5c8ca4Slogin res 249ab5c8ca4Slogin } 250ab5c8ca4Slogin 251ab5c8ca4Slogin SYS_EXECVE => { 252ab5c8ca4Slogin let path_ptr = args[0]; 253ab5c8ca4Slogin let argv_ptr = args[1]; 254ab5c8ca4Slogin let env_ptr = args[2]; 255be63f3b2Shoumkh let virt_path_ptr = VirtAddr::new(path_ptr); 256be63f3b2Shoumkh let virt_argv_ptr = VirtAddr::new(argv_ptr); 257be63f3b2Shoumkh let virt_env_ptr = VirtAddr::new(env_ptr); 258ab5c8ca4Slogin // 权限校验 2591496ba7bSLoGin if frame.from_user() 2601496ba7bSLoGin && (verify_area(virt_path_ptr, MAX_PATHLEN as usize).is_err() 261be63f3b2Shoumkh || verify_area(virt_argv_ptr, PAGE_4K_SIZE as usize).is_err()) 262be63f3b2Shoumkh || verify_area(virt_env_ptr, PAGE_4K_SIZE as usize).is_err() 263ab5c8ca4Slogin { 264ab5c8ca4Slogin Err(SystemError::EFAULT) 265ab5c8ca4Slogin } else { 266ab5c8ca4Slogin Self::execve( 2671496ba7bSLoGin path_ptr as *const u8, 2681496ba7bSLoGin argv_ptr as *const *const u8, 2691496ba7bSLoGin env_ptr as *const *const u8, 2701496ba7bSLoGin frame, 271ab5c8ca4Slogin ) 2721496ba7bSLoGin .map(|_| 0) 273ab5c8ca4Slogin } 274ab5c8ca4Slogin } 275ab5c8ca4Slogin SYS_WAIT4 => { 276bf4a4899SLoGin let pid = args[0] as i32; 2771496ba7bSLoGin let wstatus = args[1] as *mut i32; 278ab5c8ca4Slogin let options = args[2] as c_int; 279ab5c8ca4Slogin let rusage = args[3] as *mut c_void; 280ab5c8ca4Slogin // 权限校验 281ab5c8ca4Slogin // todo: 引入rusage之后,更正以下权限校验代码中,rusage的大小 282bf4a4899SLoGin Self::wait4(pid.into(), wstatus, options, rusage) 283ab5c8ca4Slogin } 284ab5c8ca4Slogin 285ab5c8ca4Slogin SYS_EXIT => { 286ab5c8ca4Slogin let exit_code = args[0]; 287ab5c8ca4Slogin Self::exit(exit_code) 288ab5c8ca4Slogin } 2894fda81ceSLoGin #[cfg(target_arch = "x86_64")] 290ab5c8ca4Slogin SYS_MKDIR => { 29182df0a13Shmt let path = args[0] as *const u8; 292ab5c8ca4Slogin let mode = args[1]; 293ab5c8ca4Slogin 29482df0a13Shmt Self::mkdir(path, mode) 295ab5c8ca4Slogin } 296ab5c8ca4Slogin 297ab5c8ca4Slogin SYS_NANOSLEEP => { 298ab5c8ca4Slogin let req = args[0] as *const TimeSpec; 299ab5c8ca4Slogin let rem = args[1] as *mut TimeSpec; 300be63f3b2Shoumkh let virt_req = VirtAddr::new(req as usize); 301be63f3b2Shoumkh let virt_rem = VirtAddr::new(rem as usize); 3021496ba7bSLoGin if frame.from_user() 303be63f3b2Shoumkh && (verify_area(virt_req, core::mem::size_of::<TimeSpec>() as usize).is_err() 304be63f3b2Shoumkh || verify_area(virt_rem, core::mem::size_of::<TimeSpec>() as usize) 305be63f3b2Shoumkh .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 3485eeefb8cSChenzx #[cfg(target_arch = "x86_64")] 349bf4a4899SLoGin SYS_UNLINK => { 35082df0a13Shmt let path = args[0] as *const u8; 35182df0a13Shmt Self::unlink(path) 352bf4a4899SLoGin } 353ab5c8ca4Slogin SYS_KILL => { 3541496ba7bSLoGin let pid = Pid::new(args[0]); 355ab5c8ca4Slogin let sig = args[1] as c_int; 3563c82aa56SChiichen // kdebug!("KILL SYSCALL RECEIVED"); 357ab5c8ca4Slogin Self::kill(pid, sig) 358ab5c8ca4Slogin } 359ab5c8ca4Slogin 3604fda81ceSLoGin SYS_RT_SIGACTION => { 361ab5c8ca4Slogin let sig = args[0] as c_int; 362ab5c8ca4Slogin let act = args[1]; 363ab5c8ca4Slogin let old_act = args[2]; 3641496ba7bSLoGin Self::sigaction(sig, act, old_act, frame.from_user()) 365ab5c8ca4Slogin } 366ab5c8ca4Slogin 3671496ba7bSLoGin SYS_GETPID => Self::getpid().map(|pid| pid.into()), 368ab5c8ca4Slogin 3691496ba7bSLoGin SYS_SCHED => Self::sched(frame.from_user()), 370ab5c8ca4Slogin SYS_DUP => { 371ab5c8ca4Slogin let oldfd: i32 = args[0] as c_int; 372ab5c8ca4Slogin Self::dup(oldfd) 373ab5c8ca4Slogin } 3744fda81ceSLoGin 3754fda81ceSLoGin #[cfg(target_arch = "x86_64")] 376ab5c8ca4Slogin SYS_DUP2 => { 377ab5c8ca4Slogin let oldfd: i32 = args[0] as c_int; 378ab5c8ca4Slogin let newfd: i32 = args[1] as c_int; 379ab5c8ca4Slogin Self::dup2(oldfd, newfd) 380ab5c8ca4Slogin } 381ab5c8ca4Slogin 382ab5c8ca4Slogin SYS_SOCKET => Self::socket(args[0], args[1], args[2]), 383ab5c8ca4Slogin SYS_SETSOCKOPT => { 384ab5c8ca4Slogin let optval = args[3] as *const u8; 385ab5c8ca4Slogin let optlen = args[4] as usize; 386be63f3b2Shoumkh let virt_optval = VirtAddr::new(optval as usize); 387ab5c8ca4Slogin // 验证optval的地址是否合法 388be63f3b2Shoumkh if verify_area(virt_optval, optlen as usize).is_err() { 389ab5c8ca4Slogin // 地址空间超出了用户空间的范围,不合法 390ab5c8ca4Slogin Err(SystemError::EFAULT) 391ab5c8ca4Slogin } else { 392ab5c8ca4Slogin let data: &[u8] = unsafe { core::slice::from_raw_parts(optval, optlen) }; 393ab5c8ca4Slogin Self::setsockopt(args[0], args[1], args[2], data) 394ab5c8ca4Slogin } 395ab5c8ca4Slogin } 396ab5c8ca4Slogin SYS_GETSOCKOPT => { 397ab5c8ca4Slogin let optval = args[3] as *mut u8; 398ab5c8ca4Slogin let optlen = args[4] as *mut usize; 399be63f3b2Shoumkh let virt_optval = VirtAddr::new(optval as usize); 400be63f3b2Shoumkh let virt_optlen = VirtAddr::new(optlen as usize); 401ab5c8ca4Slogin let security_check = || { 402ab5c8ca4Slogin // 验证optval的地址是否合法 403be63f3b2Shoumkh if verify_area(virt_optval, PAGE_4K_SIZE as usize).is_err() { 404ab5c8ca4Slogin // 地址空间超出了用户空间的范围,不合法 405ab5c8ca4Slogin return Err(SystemError::EFAULT); 406ab5c8ca4Slogin } 407ab5c8ca4Slogin 408ab5c8ca4Slogin // 验证optlen的地址是否合法 409be63f3b2Shoumkh if verify_area(virt_optlen, core::mem::size_of::<u32>() as usize).is_err() { 410ab5c8ca4Slogin // 地址空间超出了用户空间的范围,不合法 411ab5c8ca4Slogin return Err(SystemError::EFAULT); 412ab5c8ca4Slogin } 413ab5c8ca4Slogin return Ok(()); 414ab5c8ca4Slogin }; 415ab5c8ca4Slogin let r = security_check(); 416ab5c8ca4Slogin if r.is_err() { 417ab5c8ca4Slogin Err(r.unwrap_err()) 418ab5c8ca4Slogin } else { 419ab5c8ca4Slogin Self::getsockopt(args[0], args[1], args[2], optval, optlen as *mut u32) 420ab5c8ca4Slogin } 421ab5c8ca4Slogin } 422ab5c8ca4Slogin 423ab5c8ca4Slogin SYS_CONNECT => { 424ab5c8ca4Slogin let addr = args[1] as *const SockAddr; 425ab5c8ca4Slogin let addrlen = args[2] as usize; 426be63f3b2Shoumkh let virt_addr = VirtAddr::new(addr as usize); 427ab5c8ca4Slogin // 验证addr的地址是否合法 428be63f3b2Shoumkh if verify_area(virt_addr, addrlen as usize).is_err() { 429ab5c8ca4Slogin // 地址空间超出了用户空间的范围,不合法 430ab5c8ca4Slogin Err(SystemError::EFAULT) 431ab5c8ca4Slogin } else { 432ab5c8ca4Slogin Self::connect(args[0], addr, addrlen) 433ab5c8ca4Slogin } 434ab5c8ca4Slogin } 435ab5c8ca4Slogin SYS_BIND => { 436ab5c8ca4Slogin let addr = args[1] as *const SockAddr; 437ab5c8ca4Slogin let addrlen = args[2] as usize; 438be63f3b2Shoumkh let virt_addr = VirtAddr::new(addr as usize); 439ab5c8ca4Slogin // 验证addr的地址是否合法 440be63f3b2Shoumkh if verify_area(virt_addr, addrlen as usize).is_err() { 441ab5c8ca4Slogin // 地址空间超出了用户空间的范围,不合法 442ab5c8ca4Slogin Err(SystemError::EFAULT) 443ab5c8ca4Slogin } else { 444ab5c8ca4Slogin Self::bind(args[0], addr, addrlen) 445ab5c8ca4Slogin } 446ab5c8ca4Slogin } 447ab5c8ca4Slogin 448ab5c8ca4Slogin SYS_SENDTO => { 449ab5c8ca4Slogin let buf = args[1] as *const u8; 450ab5c8ca4Slogin let len = args[2] as usize; 451ab5c8ca4Slogin let flags = args[3] as u32; 452ab5c8ca4Slogin let addr = args[4] as *const SockAddr; 453ab5c8ca4Slogin let addrlen = args[5] as usize; 454be63f3b2Shoumkh let virt_buf = VirtAddr::new(buf as usize); 455be63f3b2Shoumkh let virt_addr = VirtAddr::new(addr as usize); 456ab5c8ca4Slogin // 验证buf的地址是否合法 457be63f3b2Shoumkh if verify_area(virt_buf, len as usize).is_err() { 458ab5c8ca4Slogin // 地址空间超出了用户空间的范围,不合法 459ab5c8ca4Slogin Err(SystemError::EFAULT) 460be63f3b2Shoumkh } else if verify_area(virt_addr, addrlen as usize).is_err() { 461ab5c8ca4Slogin // 地址空间超出了用户空间的范围,不合法 462ab5c8ca4Slogin Err(SystemError::EFAULT) 463ab5c8ca4Slogin } else { 464ab5c8ca4Slogin let data: &[u8] = unsafe { core::slice::from_raw_parts(buf, len) }; 465ab5c8ca4Slogin Self::sendto(args[0], data, flags, addr, addrlen) 466ab5c8ca4Slogin } 467ab5c8ca4Slogin } 468ab5c8ca4Slogin 469ab5c8ca4Slogin SYS_RECVFROM => { 470ab5c8ca4Slogin let buf = args[1] as *mut u8; 471ab5c8ca4Slogin let len = args[2] as usize; 472ab5c8ca4Slogin let flags = args[3] as u32; 473ab5c8ca4Slogin let addr = args[4] as *mut SockAddr; 474ab5c8ca4Slogin let addrlen = args[5] as *mut usize; 475be63f3b2Shoumkh let virt_buf = VirtAddr::new(buf as usize); 476be63f3b2Shoumkh let virt_addrlen = VirtAddr::new(addrlen as usize); 477be63f3b2Shoumkh let virt_addr = VirtAddr::new(addr as usize); 478ab5c8ca4Slogin let security_check = || { 479ab5c8ca4Slogin // 验证buf的地址是否合法 480840045afSLoGin if verify_area(virt_buf, len).is_err() { 481ab5c8ca4Slogin // 地址空间超出了用户空间的范围,不合法 482ab5c8ca4Slogin return Err(SystemError::EFAULT); 483ab5c8ca4Slogin } 484ab5c8ca4Slogin 485ab5c8ca4Slogin // 验证addrlen的地址是否合法 486840045afSLoGin if verify_area(virt_addrlen, core::mem::size_of::<u32>()).is_err() { 487ab5c8ca4Slogin // 地址空间超出了用户空间的范围,不合法 488ab5c8ca4Slogin return Err(SystemError::EFAULT); 489ab5c8ca4Slogin } 490ab5c8ca4Slogin 491840045afSLoGin if verify_area(virt_addr, core::mem::size_of::<SockAddr>()).is_err() { 492ab5c8ca4Slogin // 地址空间超出了用户空间的范围,不合法 493ab5c8ca4Slogin return Err(SystemError::EFAULT); 494ab5c8ca4Slogin } 495ab5c8ca4Slogin return Ok(()); 496ab5c8ca4Slogin }; 497ab5c8ca4Slogin let r = security_check(); 498ab5c8ca4Slogin if r.is_err() { 499ab5c8ca4Slogin Err(r.unwrap_err()) 500ab5c8ca4Slogin } else { 501ab5c8ca4Slogin let buf = unsafe { core::slice::from_raw_parts_mut(buf, len) }; 502ab5c8ca4Slogin Self::recvfrom(args[0], buf, flags, addr, addrlen as *mut u32) 503ab5c8ca4Slogin } 504ab5c8ca4Slogin } 505ab5c8ca4Slogin 506ab5c8ca4Slogin SYS_RECVMSG => { 5074ad52e57S裕依2439 let msg = args[1] as *mut MsgHdr; 508ab5c8ca4Slogin let flags = args[2] as u32; 5094ad52e57S裕依2439 5104ad52e57S裕依2439 let mut user_buffer_writer = 5114ad52e57S裕依2439 UserBufferWriter::new(msg, core::mem::size_of::<MsgHdr>(), frame.from_user())?; 5124ad52e57S裕依2439 let buffer = user_buffer_writer.buffer::<MsgHdr>(0)?; 5134ad52e57S裕依2439 5144537ffb7SChiichen let msg = &mut buffer[0]; 515ab5c8ca4Slogin Self::recvmsg(args[0], msg, flags) 516ab5c8ca4Slogin } 517ab5c8ca4Slogin 518ab5c8ca4Slogin SYS_LISTEN => Self::listen(args[0], args[1]), 519ab5c8ca4Slogin SYS_SHUTDOWN => Self::shutdown(args[0], args[1]), 520ab5c8ca4Slogin SYS_ACCEPT => Self::accept(args[0], args[1] as *mut SockAddr, args[2] as *mut u32), 521c47fe904SLoGin SYS_ACCEPT4 => Self::accept4( 522c47fe904SLoGin args[0], 523c47fe904SLoGin args[1] as *mut SockAddr, 524c47fe904SLoGin args[2] as *mut u32, 525c47fe904SLoGin args[3] as u32, 526c47fe904SLoGin ), 527ab5c8ca4Slogin SYS_GETSOCKNAME => { 528ab5c8ca4Slogin Self::getsockname(args[0], args[1] as *mut SockAddr, args[2] as *mut u32) 529ab5c8ca4Slogin } 530ab5c8ca4Slogin SYS_GETPEERNAME => { 531ab5c8ca4Slogin Self::getpeername(args[0], args[1] as *mut SockAddr, args[2] as *mut u32) 532ab5c8ca4Slogin } 53336fd0130Shoumkh SYS_GETTIMEOFDAY => { 53436fd0130Shoumkh let timeval = args[0] as *mut PosixTimeval; 53540fe15e0SLoGin let timezone_ptr = args[1] as *mut PosixTimeZone; 53640fe15e0SLoGin Self::gettimeofday(timeval, timezone_ptr) 5374537ffb7SChiichen } 53840fe15e0SLoGin SYS_MMAP => { 53940fe15e0SLoGin let len = page_align_up(args[1]); 540840045afSLoGin let virt_addr = VirtAddr::new(args[0]); 541840045afSLoGin if verify_area(virt_addr, len).is_err() { 54240fe15e0SLoGin Err(SystemError::EFAULT) 54340fe15e0SLoGin } else { 54440fe15e0SLoGin Self::mmap( 54540fe15e0SLoGin VirtAddr::new(args[0]), 54640fe15e0SLoGin len, 54740fe15e0SLoGin args[2], 54840fe15e0SLoGin args[3], 54940fe15e0SLoGin args[4] as i32, 55040fe15e0SLoGin args[5], 55140fe15e0SLoGin ) 55240fe15e0SLoGin } 55340fe15e0SLoGin } 5544cfa009bSJomo SYS_MREMAP => { 5554cfa009bSJomo let old_vaddr = VirtAddr::new(args[0]); 5564cfa009bSJomo let old_len = args[1]; 5574cfa009bSJomo let new_len = args[2]; 5584cfa009bSJomo let mremap_flags = MremapFlags::from_bits_truncate(args[3] as u8); 5594cfa009bSJomo let new_vaddr = VirtAddr::new(args[4]); 5604cfa009bSJomo 5614cfa009bSJomo Self::mremap(old_vaddr, old_len, new_len, mremap_flags, new_vaddr) 5624cfa009bSJomo } 56340fe15e0SLoGin SYS_MUNMAP => { 56440fe15e0SLoGin let addr = args[0]; 56540fe15e0SLoGin let len = page_align_up(args[1]); 566971462beSGnoCiYeH if addr & (MMArch::PAGE_SIZE - 1) != 0 { 56740fe15e0SLoGin // The addr argument is not a multiple of the page size 56840fe15e0SLoGin Err(SystemError::EINVAL) 56940fe15e0SLoGin } else { 57040fe15e0SLoGin Self::munmap(VirtAddr::new(addr), len) 57140fe15e0SLoGin } 57240fe15e0SLoGin } 57340fe15e0SLoGin SYS_MPROTECT => { 57440fe15e0SLoGin let addr = args[0]; 57540fe15e0SLoGin let len = page_align_up(args[1]); 576971462beSGnoCiYeH if addr & (MMArch::PAGE_SIZE - 1) != 0 { 57740fe15e0SLoGin // The addr argument is not a multiple of the page size 57840fe15e0SLoGin Err(SystemError::EINVAL) 57940fe15e0SLoGin } else { 58040fe15e0SLoGin Self::mprotect(VirtAddr::new(addr), len, args[2]) 58140fe15e0SLoGin } 58240fe15e0SLoGin } 58340fe15e0SLoGin 5841496ba7bSLoGin SYS_GETCWD => { 5851496ba7bSLoGin let buf = args[0] as *mut u8; 586840045afSLoGin let size = args[1]; 5871496ba7bSLoGin let security_check = || { 5881496ba7bSLoGin verify_area(VirtAddr::new(buf as usize), size)?; 5891496ba7bSLoGin return Ok(()); 5901496ba7bSLoGin }; 5911496ba7bSLoGin let r = security_check(); 5921496ba7bSLoGin if r.is_err() { 5931496ba7bSLoGin Err(r.unwrap_err()) 5941496ba7bSLoGin } else { 5951496ba7bSLoGin let buf = unsafe { core::slice::from_raw_parts_mut(buf, size) }; 5961496ba7bSLoGin Self::getcwd(buf).map(|ptr| ptr.data()) 5971496ba7bSLoGin } 5981496ba7bSLoGin } 5991496ba7bSLoGin 6001496ba7bSLoGin SYS_GETPGID => Self::getpgid(Pid::new(args[0])).map(|pid| pid.into()), 6011496ba7bSLoGin 6021496ba7bSLoGin SYS_GETPPID => Self::getppid().map(|pid| pid.into()), 603be63f3b2Shoumkh SYS_FSTAT => { 604be63f3b2Shoumkh let fd = args[0] as i32; 605be63f3b2Shoumkh let kstat = args[1] as *mut PosixKstat; 606be63f3b2Shoumkh let vaddr = VirtAddr::new(kstat as usize); 607be63f3b2Shoumkh // FIXME 由于c中的verify_area与rust中的verify_area重名,所以在引入时加了前缀区分 608be63f3b2Shoumkh // TODO 应该将用了c版本的verify_area都改为rust的verify_area 609be63f3b2Shoumkh match verify_area(vaddr, core::mem::size_of::<PosixKstat>()) { 610be63f3b2Shoumkh Ok(_) => Self::fstat(fd, kstat), 611be63f3b2Shoumkh Err(e) => Err(e), 612be63f3b2Shoumkh } 613be63f3b2Shoumkh } 614be63f3b2Shoumkh 6156d81180bSLoGin SYS_FCNTL => { 6166d81180bSLoGin let fd = args[0] as i32; 6176d81180bSLoGin let cmd: Option<FcntlCommand> = 6186d81180bSLoGin <FcntlCommand as FromPrimitive>::from_u32(args[1] as u32); 6196d81180bSLoGin let arg = args[2] as i32; 6206d81180bSLoGin let res = if let Some(cmd) = cmd { 6216d81180bSLoGin Self::fcntl(fd, cmd, arg) 6226d81180bSLoGin } else { 6236d81180bSLoGin Err(SystemError::EINVAL) 6246d81180bSLoGin }; 6256d81180bSLoGin 6266d81180bSLoGin // kdebug!("FCNTL: fd: {}, cmd: {:?}, arg: {}, res: {:?}", fd, cmd, arg, res); 6276d81180bSLoGin res 6286d81180bSLoGin } 6296d81180bSLoGin 6306d81180bSLoGin SYS_FTRUNCATE => { 6316d81180bSLoGin let fd = args[0] as i32; 632840045afSLoGin let len = args[1]; 6336d81180bSLoGin let res = Self::ftruncate(fd, len); 6346d81180bSLoGin // kdebug!("FTRUNCATE: fd: {}, len: {}, res: {:?}", fd, len, res); 6356d81180bSLoGin res 6366d81180bSLoGin } 6376d81180bSLoGin 6384fda81ceSLoGin #[cfg(target_arch = "x86_64")] 6392dbef785SGnoCiYeH SYS_MKNOD => { 6402dbef785SGnoCiYeH let path = args[0]; 6412dbef785SGnoCiYeH let flags = args[1]; 6422dbef785SGnoCiYeH let dev_t = args[2]; 6432dbef785SGnoCiYeH let flags: ModeType = ModeType::from_bits_truncate(flags as u32); 64482df0a13Shmt Self::mknod(path as *const u8, flags, DeviceNumber::from(dev_t as u32)) 6452dbef785SGnoCiYeH } 6462dbef785SGnoCiYeH 647971462beSGnoCiYeH SYS_CLONE => { 648971462beSGnoCiYeH let parent_tid = VirtAddr::new(args[2]); 649971462beSGnoCiYeH let child_tid = VirtAddr::new(args[3]); 650971462beSGnoCiYeH 651971462beSGnoCiYeH // 地址校验 652971462beSGnoCiYeH verify_area(parent_tid, core::mem::size_of::<i32>())?; 653971462beSGnoCiYeH verify_area(child_tid, core::mem::size_of::<i32>())?; 654971462beSGnoCiYeH 655971462beSGnoCiYeH let mut clone_args = KernelCloneArgs::new(); 656971462beSGnoCiYeH clone_args.flags = CloneFlags::from_bits_truncate(args[0] as u64); 657971462beSGnoCiYeH clone_args.stack = args[1]; 658971462beSGnoCiYeH clone_args.parent_tid = parent_tid; 659971462beSGnoCiYeH clone_args.child_tid = child_tid; 660971462beSGnoCiYeH clone_args.tls = args[4]; 661971462beSGnoCiYeH Self::clone(frame, clone_args) 662971462beSGnoCiYeH } 663971462beSGnoCiYeH 664971462beSGnoCiYeH SYS_FUTEX => { 665971462beSGnoCiYeH let uaddr = VirtAddr::new(args[0]); 666971462beSGnoCiYeH let operation = FutexFlag::from_bits(args[1] as u32).ok_or(SystemError::ENOSYS)?; 667971462beSGnoCiYeH let val = args[2] as u32; 668971462beSGnoCiYeH let utime = args[3]; 669971462beSGnoCiYeH let uaddr2 = VirtAddr::new(args[4]); 670971462beSGnoCiYeH let val3 = args[5] as u32; 671971462beSGnoCiYeH 672971462beSGnoCiYeH verify_area(uaddr, core::mem::size_of::<u32>())?; 673971462beSGnoCiYeH verify_area(uaddr2, core::mem::size_of::<u32>())?; 674971462beSGnoCiYeH 675971462beSGnoCiYeH let mut timespec = None; 676971462beSGnoCiYeH if utime != 0 && operation.contains(FutexFlag::FLAGS_HAS_TIMEOUT) { 677971462beSGnoCiYeH let reader = UserBufferReader::new( 678971462beSGnoCiYeH utime as *const TimeSpec, 679971462beSGnoCiYeH core::mem::size_of::<TimeSpec>(), 680971462beSGnoCiYeH true, 681971462beSGnoCiYeH )?; 682971462beSGnoCiYeH 683840045afSLoGin timespec = Some(*reader.read_one_from_user::<TimeSpec>(0)?); 684971462beSGnoCiYeH } 685971462beSGnoCiYeH 686971462beSGnoCiYeH Self::do_futex(uaddr, operation, val, timespec, uaddr2, utime as u32, val3) 687971462beSGnoCiYeH } 688971462beSGnoCiYeH 689bf4a4899SLoGin SYS_READV => Self::readv(args[0] as i32, args[1], args[2]), 690971462beSGnoCiYeH SYS_WRITEV => Self::writev(args[0] as i32, args[1], args[2]), 691971462beSGnoCiYeH 6924fda81ceSLoGin SYS_SET_TID_ADDRESS => Self::set_tid_address(args[0]), 693971462beSGnoCiYeH 6944fda81ceSLoGin #[cfg(target_arch = "x86_64")] 6954fda81ceSLoGin SYS_LSTAT => { 69682df0a13Shmt let path = args[0] as *const u8; 697971462beSGnoCiYeH let kstat = args[1] as *mut PosixKstat; 69882df0a13Shmt Self::lstat(path, kstat) 6994fda81ceSLoGin } 7004fda81ceSLoGin 7014fda81ceSLoGin #[cfg(target_arch = "x86_64")] 7024fda81ceSLoGin SYS_STAT => { 70382df0a13Shmt let path = args[0] as *const u8; 7044fda81ceSLoGin let kstat = args[1] as *mut PosixKstat; 70582df0a13Shmt Self::stat(path, kstat) 706971462beSGnoCiYeH } 707971462beSGnoCiYeH 708*b4eb05a1STTaq SYS_STATX => { 709*b4eb05a1STTaq let fd = args[0] as i32; 710*b4eb05a1STTaq let path = args[1] as *const u8; 711*b4eb05a1STTaq let flags = args[2] as u32; 712*b4eb05a1STTaq let mask = args[3] as u32; 713*b4eb05a1STTaq let kstat = args[4] as *mut PosixStatx; 714*b4eb05a1STTaq 715*b4eb05a1STTaq Self::do_statx(fd, path, flags, mask, kstat) 716*b4eb05a1STTaq } 717*b4eb05a1STTaq 71840609970SGnoCiYeH SYS_EPOLL_CREATE => Self::epoll_create(args[0] as i32), 71940609970SGnoCiYeH SYS_EPOLL_CREATE1 => Self::epoll_create1(args[0]), 72040609970SGnoCiYeH 72140609970SGnoCiYeH SYS_EPOLL_CTL => Self::epoll_ctl( 72240609970SGnoCiYeH args[0] as i32, 72340609970SGnoCiYeH args[1], 72440609970SGnoCiYeH args[2] as i32, 72540609970SGnoCiYeH VirtAddr::new(args[3]), 72640609970SGnoCiYeH ), 72740609970SGnoCiYeH 72840609970SGnoCiYeH SYS_EPOLL_WAIT => Self::epoll_wait( 72940609970SGnoCiYeH args[0] as i32, 73040609970SGnoCiYeH VirtAddr::new(args[1]), 73140609970SGnoCiYeH args[2] as i32, 73240609970SGnoCiYeH args[3] as i32, 73340609970SGnoCiYeH ), 73440609970SGnoCiYeH 73540609970SGnoCiYeH SYS_EPOLL_PWAIT => { 73640609970SGnoCiYeH let epfd = args[0] as i32; 73740609970SGnoCiYeH let epoll_event = VirtAddr::new(args[1]); 73840609970SGnoCiYeH let max_events = args[2] as i32; 73940609970SGnoCiYeH let timespec = args[3] as i32; 74040609970SGnoCiYeH let sigmask_addr = args[4] as *mut SigSet; 74140609970SGnoCiYeH 74240609970SGnoCiYeH if sigmask_addr.is_null() { 74340609970SGnoCiYeH return Self::epoll_wait(epfd, epoll_event, max_events, timespec); 74440609970SGnoCiYeH } 74540609970SGnoCiYeH let sigmask_reader = 74640609970SGnoCiYeH UserBufferReader::new(sigmask_addr, core::mem::size_of::<SigSet>(), true)?; 747840045afSLoGin let mut sigmask = *sigmask_reader.read_one_from_user::<SigSet>(0)?; 74840609970SGnoCiYeH 74940609970SGnoCiYeH Self::epoll_pwait( 75040609970SGnoCiYeH args[0] as i32, 75140609970SGnoCiYeH VirtAddr::new(args[1]), 75240609970SGnoCiYeH args[2] as i32, 75340609970SGnoCiYeH args[3] as i32, 75440609970SGnoCiYeH &mut sigmask, 75540609970SGnoCiYeH ) 75640609970SGnoCiYeH } 75740609970SGnoCiYeH 758971462beSGnoCiYeH // 目前为了适配musl-libc,以下系统调用先这样写着 7594fda81ceSLoGin SYS_GETRANDOM => { 760971462beSGnoCiYeH let flags = GRandFlags::from_bits(args[2] as u8).ok_or(SystemError::EINVAL)?; 761971462beSGnoCiYeH Self::get_random(args[0] as *mut u8, args[1], flags) 762971462beSGnoCiYeH } 763971462beSGnoCiYeH 7644fda81ceSLoGin SYS_SOCKETPAIR => { 7654ad52e57S裕依2439 let mut user_buffer_writer = UserBufferWriter::new( 7664ad52e57S裕依2439 args[3] as *mut c_int, 7674ad52e57S裕依2439 core::mem::size_of::<[c_int; 2]>(), 7684ad52e57S裕依2439 frame.from_user(), 7694ad52e57S裕依2439 )?; 7704ad52e57S裕依2439 let fds = user_buffer_writer.buffer::<i32>(0)?; 7714ad52e57S裕依2439 Self::socketpair(args[0], args[1], args[2], fds) 772971462beSGnoCiYeH } 773971462beSGnoCiYeH 7744fda81ceSLoGin #[cfg(target_arch = "x86_64")] 775971462beSGnoCiYeH SYS_POLL => { 776971462beSGnoCiYeH kwarn!("SYS_POLL has not yet been implemented"); 777971462beSGnoCiYeH Ok(0) 778971462beSGnoCiYeH } 779971462beSGnoCiYeH 78052da9a59SGnoCiYeH SYS_SETPGID => { 78152da9a59SGnoCiYeH kwarn!("SYS_SETPGID has not yet been implemented"); 78252da9a59SGnoCiYeH Ok(0) 78352da9a59SGnoCiYeH } 78452da9a59SGnoCiYeH 785971462beSGnoCiYeH SYS_RT_SIGPROCMASK => { 786971462beSGnoCiYeH kwarn!("SYS_RT_SIGPROCMASK has not yet been implemented"); 787971462beSGnoCiYeH Ok(0) 788971462beSGnoCiYeH } 789971462beSGnoCiYeH 790971462beSGnoCiYeH SYS_TKILL => { 791971462beSGnoCiYeH kwarn!("SYS_TKILL has not yet been implemented"); 792971462beSGnoCiYeH Ok(0) 793971462beSGnoCiYeH } 794971462beSGnoCiYeH 795971462beSGnoCiYeH SYS_SIGALTSTACK => { 796971462beSGnoCiYeH kwarn!("SYS_SIGALTSTACK has not yet been implemented"); 797971462beSGnoCiYeH Ok(0) 798971462beSGnoCiYeH } 799971462beSGnoCiYeH 80016033951SGnoCiYeH SYS_EXIT_GROUP => { 80116033951SGnoCiYeH kwarn!("SYS_EXIT_GROUP has not yet been implemented"); 80216033951SGnoCiYeH Ok(0) 80316033951SGnoCiYeH } 80416033951SGnoCiYeH 80504babc3fSMemoryShore SYS_MADVISE => { 8066c7f966cSMemoryShore // 这个太吵了,总是打印,先注释掉 8076c7f966cSMemoryShore // kwarn!("SYS_MADVISE has not yet been implemented"); 80804babc3fSMemoryShore Ok(0) 80904babc3fSMemoryShore } 810393f6915SLoGin SYS_GETTID => Self::gettid().map(|tid| tid.into()), 811840045afSLoGin SYS_GETUID => Self::getuid(), 8128d72b68dSJomo 81302e249f3SLoGin SYS_SYSLOG => { 814840045afSLoGin let syslog_action_type = args[0]; 8158d72b68dSJomo let buf_vaddr = args[1]; 8168d72b68dSJomo let len = args[2]; 8178d72b68dSJomo let from_user = frame.from_user(); 8188d72b68dSJomo let mut user_buffer_writer = 8198d72b68dSJomo UserBufferWriter::new(buf_vaddr as *mut u8, len, from_user)?; 8208d72b68dSJomo 8218d72b68dSJomo let user_buf = user_buffer_writer.buffer(0)?; 822840045afSLoGin Self::do_syslog(syslog_action_type, user_buf, len) 82302e249f3SLoGin } 8248d72b68dSJomo 825840045afSLoGin SYS_GETGID => Self::getgid(), 82602e249f3SLoGin SYS_SETUID => { 82702e249f3SLoGin kwarn!("SYS_SETUID has not yet been implemented"); 82802e249f3SLoGin Ok(0) 82902e249f3SLoGin } 83002e249f3SLoGin SYS_SETGID => { 83102e249f3SLoGin kwarn!("SYS_SETGID has not yet been implemented"); 83202e249f3SLoGin Ok(0) 83302e249f3SLoGin } 834840045afSLoGin SYS_GETEUID => Self::geteuid(), 835840045afSLoGin SYS_GETEGID => Self::getegid(), 836be8cdf4bSLoGin SYS_GETRUSAGE => { 837be8cdf4bSLoGin let who = args[0] as c_int; 838be8cdf4bSLoGin let rusage = args[1] as *mut RUsage; 839be8cdf4bSLoGin Self::get_rusage(who, rusage) 840be8cdf4bSLoGin } 84104babc3fSMemoryShore 8424fda81ceSLoGin #[cfg(target_arch = "x86_64")] 843709498caSLoGin SYS_READLINK => { 844709498caSLoGin let path = args[0] as *const u8; 845709498caSLoGin let buf = args[1] as *mut u8; 846840045afSLoGin let bufsiz = args[2]; 847709498caSLoGin Self::readlink(path, buf, bufsiz) 848709498caSLoGin } 849709498caSLoGin 8504fda81ceSLoGin SYS_READLINKAT => { 851709498caSLoGin let dirfd = args[0] as i32; 85282df0a13Shmt let path = args[1] as *const u8; 853709498caSLoGin let buf = args[2] as *mut u8; 854840045afSLoGin let bufsiz = args[3]; 85582df0a13Shmt Self::readlink_at(dirfd, path, buf, bufsiz) 856709498caSLoGin } 857709498caSLoGin 8580d9b7d92SLoGin SYS_PRLIMIT64 => { 8590d9b7d92SLoGin let pid = args[0]; 8600d9b7d92SLoGin let pid = Pid::new(pid); 8610d9b7d92SLoGin let resource = args[1]; 8620d9b7d92SLoGin let new_limit = args[2] as *const RLimit64; 8630d9b7d92SLoGin let old_limit = args[3] as *mut RLimit64; 8640d9b7d92SLoGin 8650d9b7d92SLoGin Self::prlimit64(pid, resource, new_limit, old_limit) 8660d9b7d92SLoGin } 8670d9b7d92SLoGin 8684fda81ceSLoGin #[cfg(target_arch = "x86_64")] 8699b0abe6dSLoGin SYS_ACCESS => { 8709b0abe6dSLoGin let pathname = args[0] as *const u8; 8719b0abe6dSLoGin let mode = args[1] as u32; 8729b0abe6dSLoGin Self::access(pathname, mode) 8739b0abe6dSLoGin } 8749b0abe6dSLoGin 8759b0abe6dSLoGin SYS_FACCESSAT => { 8769b0abe6dSLoGin let dirfd = args[0] as i32; 8779b0abe6dSLoGin let pathname = args[1] as *const u8; 8789b0abe6dSLoGin let mode = args[2] as u32; 8799b0abe6dSLoGin Self::faccessat2(dirfd, pathname, mode, 0) 8809b0abe6dSLoGin } 8819b0abe6dSLoGin 8829b0abe6dSLoGin SYS_FACCESSAT2 => { 8839b0abe6dSLoGin let dirfd = args[0] as i32; 8849b0abe6dSLoGin let pathname = args[1] as *const u8; 8859b0abe6dSLoGin let mode = args[2] as u32; 8869b0abe6dSLoGin let flags = args[3] as u32; 8879b0abe6dSLoGin Self::faccessat2(dirfd, pathname, mode, flags) 8889b0abe6dSLoGin } 8899b0abe6dSLoGin 890bf4a4899SLoGin SYS_CLOCK_GETTIME => { 891bf4a4899SLoGin let clockid = args[0] as i32; 892bf4a4899SLoGin let timespec = args[1] as *mut TimeSpec; 893bf4a4899SLoGin Self::clock_gettime(clockid, timespec) 894bf4a4899SLoGin } 895bf4a4899SLoGin 896bf4a4899SLoGin SYS_SYSINFO => { 897bf4a4899SLoGin let info = args[0] as *mut SysInfo; 898bf4a4899SLoGin Self::sysinfo(info) 899bf4a4899SLoGin } 900bf4a4899SLoGin 901bf4a4899SLoGin SYS_UMASK => { 902bf4a4899SLoGin let mask = args[0] as u32; 903bf4a4899SLoGin Self::umask(mask) 904bf4a4899SLoGin } 905bf4a4899SLoGin 90627b967a3S裕依 SYS_FCHOWN => { 90727b967a3S裕依 kwarn!("SYS_FCHOWN has not yet been implemented"); 90827b967a3S裕依 Ok(0) 90927b967a3S裕依 } 91027b967a3S裕依 91127b967a3S裕依 SYS_FSYNC => { 91227b967a3S裕依 kwarn!("SYS_FSYNC has not yet been implemented"); 91327b967a3S裕依 Ok(0) 91427b967a3S裕依 } 91527b967a3S裕依 9164fda81ceSLoGin #[cfg(target_arch = "x86_64")] 917bf4a4899SLoGin SYS_CHMOD => { 918bf4a4899SLoGin let pathname = args[0] as *const u8; 919bf4a4899SLoGin let mode = args[1] as u32; 920bf4a4899SLoGin Self::chmod(pathname, mode) 921bf4a4899SLoGin } 922bf4a4899SLoGin SYS_FCHMOD => { 923bf4a4899SLoGin let fd = args[0] as i32; 924bf4a4899SLoGin let mode = args[1] as u32; 925bf4a4899SLoGin Self::fchmod(fd, mode) 926bf4a4899SLoGin } 927bf4a4899SLoGin SYS_FCHMODAT => { 928bf4a4899SLoGin let dirfd = args[0] as i32; 929bf4a4899SLoGin let pathname = args[1] as *const u8; 930bf4a4899SLoGin let mode = args[2] as u32; 931bf4a4899SLoGin Self::fchmodat(dirfd, pathname, mode) 932bf4a4899SLoGin } 933bf4a4899SLoGin 93402343d0bSLoGin SYS_SCHED_GETAFFINITY => { 93502343d0bSLoGin // todo: 这个系统调用还没有实现 93602343d0bSLoGin 93702343d0bSLoGin Err(SystemError::ENOSYS) 93802343d0bSLoGin } 93902343d0bSLoGin 94052da9a59SGnoCiYeH #[cfg(target_arch = "x86_64")] 94152da9a59SGnoCiYeH SYS_GETRLIMIT => { 94252da9a59SGnoCiYeH let resource = args[0]; 94352da9a59SGnoCiYeH let rlimit = args[1] as *mut RLimit64; 94452da9a59SGnoCiYeH 94552da9a59SGnoCiYeH Self::prlimit64( 94652da9a59SGnoCiYeH ProcessManager::current_pcb().pid(), 94752da9a59SGnoCiYeH resource, 948840045afSLoGin core::ptr::null::<RLimit64>(), 94952da9a59SGnoCiYeH rlimit, 95052da9a59SGnoCiYeH ) 95152da9a59SGnoCiYeH } 95252da9a59SGnoCiYeH 9539e481b3bSTTaq SYS_FADVISE64 => { 9549e481b3bSTTaq // todo: 这个系统调用还没有实现 9559e481b3bSTTaq 9569e481b3bSTTaq Err(SystemError::ENOSYS) 9579e481b3bSTTaq } 9581d37ca6dSDonkey Kane 9591d37ca6dSDonkey Kane SYS_MOUNT => { 9601d37ca6dSDonkey Kane let source = args[0] as *const u8; 9611d37ca6dSDonkey Kane let target = args[1] as *const u8; 9621d37ca6dSDonkey Kane let filesystemtype = args[2] as *const u8; 9631d37ca6dSDonkey Kane return Self::mount(source, target, filesystemtype, 0, null()); 9641d37ca6dSDonkey Kane } 9659e481b3bSTTaq SYS_NEWFSTATAT => { 9669e481b3bSTTaq // todo: 这个系统调用还没有实现 9679e481b3bSTTaq 9689e481b3bSTTaq Err(SystemError::ENOSYS) 9699e481b3bSTTaq } 9709e481b3bSTTaq 97102343d0bSLoGin SYS_SCHED_YIELD => Self::sched_yield(), 9728c6f2184Ssspphh SYS_UNAME => { 9738c6f2184Ssspphh let name = args[0] as *mut PosixOldUtsName; 9748c6f2184Ssspphh Self::uname(name) 9758c6f2184Ssspphh } 9761d37ca6dSDonkey Kane 977ab5c8ca4Slogin _ => panic!("Unsupported syscall ID: {}", syscall_num), 978ab5c8ca4Slogin }; 9794fda81ceSLoGin 980ab5c8ca4Slogin return r; 981ab5c8ca4Slogin } 982ab5c8ca4Slogin 983ab5c8ca4Slogin pub fn put_string( 984ab5c8ca4Slogin s: *const u8, 985ab5c8ca4Slogin front_color: u32, 986ab5c8ca4Slogin back_color: u32, 987ab5c8ca4Slogin ) -> Result<usize, SystemError> { 988c3dc6f2fSLoGin // todo: 删除这个系统调用 989c3dc6f2fSLoGin let s = check_and_clone_cstr(s, Some(4096))?; 990c3dc6f2fSLoGin let fr = (front_color & 0x00ff0000) >> 16; 991c3dc6f2fSLoGin let fg = (front_color & 0x0000ff00) >> 8; 992c3dc6f2fSLoGin let fb = front_color & 0x000000ff; 993c3dc6f2fSLoGin let br = (back_color & 0x00ff0000) >> 16; 994c3dc6f2fSLoGin let bg = (back_color & 0x0000ff00) >> 8; 995c3dc6f2fSLoGin let bb = back_color & 0x000000ff; 996c3dc6f2fSLoGin print!("\x1B[38;2;{fr};{fg};{fb};48;2;{br};{bg};{bb}m{s}\x1B[0m"); 997c3dc6f2fSLoGin return Ok(s.len()); 998ab5c8ca4Slogin } 999ab5c8ca4Slogin 1000ab5c8ca4Slogin pub fn reboot() -> Result<usize, SystemError> { 10014fda81ceSLoGin unsafe { cpu_reset() }; 1002ab5c8ca4Slogin } 1003ab5c8ca4Slogin } 1004