1 use core::{ 2 ffi::{c_int, c_void}, 3 ptr::null, 4 sync::atomic::{AtomicBool, Ordering}, 5 }; 6 7 use crate::{ 8 arch::{ipc::signal::SigSet, syscall::nr::*}, 9 driver::base::device::device_number::DeviceNumber, 10 filesystem::vfs::syscall::{PosixStatfs, PosixStatx}, 11 ipc::shm::{ShmCtlCmd, ShmFlags, ShmId, ShmKey}, 12 libs::{futex::constant::FutexFlag, rand::GRandFlags}, 13 mm::syscall::MremapFlags, 14 net::syscall::MsgHdr, 15 process::{ 16 fork::KernelCloneArgs, 17 resource::{RLimit64, RUsage}, 18 ProcessFlags, ProcessManager, 19 }, 20 sched::{schedule, SchedMode}, 21 syscall::user_access::check_and_clone_cstr, 22 }; 23 24 use num_traits::FromPrimitive; 25 use system_error::SystemError; 26 27 use crate::{ 28 arch::{cpu::cpu_reset, interrupt::TrapFrame, MMArch}, 29 filesystem::vfs::{ 30 fcntl::{AtFlags, FcntlCommand}, 31 file::FileMode, 32 syscall::{ModeType, PosixKstat}, 33 MAX_PATHLEN, 34 }, 35 include::bindings::bindings::PAGE_4K_SIZE, 36 kinfo, 37 libs::align::page_align_up, 38 mm::{verify_area, MemoryManagementArch, VirtAddr}, 39 net::syscall::SockAddr, 40 process::{fork::CloneFlags, syscall::PosixOldUtsName, Pid}, 41 time::{ 42 syscall::{PosixTimeZone, PosixTimeval}, 43 PosixTimeSpec, 44 }, 45 }; 46 47 use self::{ 48 misc::SysInfo, 49 user_access::{UserBufferReader, UserBufferWriter}, 50 }; 51 52 pub mod misc; 53 pub mod user_access; 54 55 // 与linux不一致的调用,在linux基础上累加 56 pub const SYS_PUT_STRING: usize = 100000; 57 pub const SYS_SBRK: usize = 100001; 58 /// todo: 该系统调用与Linux不一致,将来需要删除该系统调用!!! 删的时候记得改C版本的libc 59 pub const SYS_CLOCK: usize = 100002; 60 pub const SYS_SCHED: usize = 100003; 61 62 #[derive(Debug)] 63 pub struct Syscall; 64 65 impl Syscall { 66 /// 初始化系统调用 67 #[inline(never)] 68 pub fn init() -> Result<(), SystemError> { 69 static INIT_FLAG: AtomicBool = AtomicBool::new(false); 70 let prev = INIT_FLAG.swap(true, Ordering::SeqCst); 71 if prev { 72 panic!("Cannot initialize syscall more than once!"); 73 } 74 kinfo!("Initializing syscall..."); 75 let r = crate::arch::syscall::arch_syscall_init(); 76 kinfo!("Syscall init successfully!"); 77 78 return r; 79 } 80 /// @brief 系统调用分发器,用于分发系统调用。 81 /// 82 /// 这个函数内,需要根据系统调用号,调用对应的系统调用处理函数。 83 /// 并且,对于用户态传入的指针参数,需要在本函数内进行越界检查,防止访问到内核空间。 84 #[inline(never)] 85 pub fn handle( 86 syscall_num: usize, 87 args: &[usize], 88 frame: &mut TrapFrame, 89 ) -> Result<usize, SystemError> { 90 let r = match syscall_num { 91 SYS_PUT_STRING => { 92 Self::put_string(args[0] as *const u8, args[1] as u32, args[2] as u32) 93 } 94 #[cfg(target_arch = "x86_64")] 95 SYS_OPEN => { 96 let path = args[0] as *const u8; 97 let flags = args[1] as u32; 98 let mode = args[2] as u32; 99 100 Self::open(path, flags, mode, true) 101 } 102 103 #[cfg(target_arch = "x86_64")] 104 SYS_RENAME => { 105 let oldname: *const u8 = args[0] as *const u8; 106 let newname: *const u8 = args[1] as *const u8; 107 Self::do_renameat2( 108 AtFlags::AT_FDCWD.bits(), 109 oldname, 110 AtFlags::AT_FDCWD.bits(), 111 newname, 112 0, 113 ) 114 } 115 116 #[cfg(target_arch = "x86_64")] 117 SYS_RENAMEAT => { 118 let oldfd = args[0] as i32; 119 let oldname: *const u8 = args[1] as *const u8; 120 let newfd = args[2] as i32; 121 let newname: *const u8 = args[3] as *const u8; 122 Self::do_renameat2(oldfd, oldname, newfd, newname, 0) 123 } 124 125 SYS_RENAMEAT2 => { 126 let oldfd = args[0] as i32; 127 let oldname: *const u8 = args[1] as *const u8; 128 let newfd = args[2] as i32; 129 let newname: *const u8 = args[3] as *const u8; 130 let flags = args[4] as u32; 131 Self::do_renameat2(oldfd, oldname, newfd, newname, flags) 132 } 133 134 SYS_OPENAT => { 135 let dirfd = args[0] as i32; 136 let path = args[1] as *const u8; 137 let flags = args[2] as u32; 138 let mode = args[3] as u32; 139 140 Self::openat(dirfd, path, flags, mode, true) 141 } 142 SYS_CLOSE => { 143 let fd = args[0]; 144 Self::close(fd) 145 } 146 SYS_READ => { 147 let fd = args[0] as i32; 148 let buf_vaddr = args[1]; 149 let len = args[2]; 150 let from_user = frame.is_from_user(); 151 let mut user_buffer_writer = 152 UserBufferWriter::new(buf_vaddr as *mut u8, len, from_user)?; 153 154 let user_buf = user_buffer_writer.buffer(0)?; 155 Self::read(fd, user_buf) 156 } 157 SYS_WRITE => { 158 let fd = args[0] as i32; 159 let buf_vaddr = args[1]; 160 let len = args[2]; 161 let from_user = frame.is_from_user(); 162 let user_buffer_reader = 163 UserBufferReader::new(buf_vaddr as *const u8, len, from_user)?; 164 165 let user_buf = user_buffer_reader.read_from_user(0)?; 166 Self::write(fd, user_buf) 167 } 168 169 SYS_LSEEK => { 170 let fd = args[0] as i32; 171 let offset = args[1] as i64; 172 let whence = args[2] as u32; 173 174 Self::lseek(fd, offset, whence) 175 } 176 177 SYS_PREAD64 => { 178 let fd = args[0] as i32; 179 let buf_vaddr = args[1]; 180 let len = args[2]; 181 let offset = args[3]; 182 183 let mut user_buffer_writer = 184 UserBufferWriter::new(buf_vaddr as *mut u8, len, frame.is_from_user())?; 185 let buf = user_buffer_writer.buffer(0)?; 186 Self::pread(fd, buf, len, offset) 187 } 188 189 SYS_PWRITE64 => { 190 let fd = args[0] as i32; 191 let buf_vaddr = args[1]; 192 let len = args[2]; 193 let offset = args[3]; 194 195 let user_buffer_reader = 196 UserBufferReader::new(buf_vaddr as *const u8, len, frame.is_from_user())?; 197 198 let buf = user_buffer_reader.read_from_user(0)?; 199 Self::pwrite(fd, buf, len, offset) 200 } 201 202 SYS_IOCTL => { 203 let fd = args[0]; 204 let cmd = args[1]; 205 let data = args[2]; 206 Self::ioctl(fd, cmd as u32, data) 207 } 208 209 #[cfg(target_arch = "x86_64")] 210 SYS_FORK => Self::fork(frame), 211 #[cfg(target_arch = "x86_64")] 212 SYS_VFORK => Self::vfork(frame), 213 214 SYS_BRK => { 215 let new_brk = VirtAddr::new(args[0]); 216 Self::brk(new_brk).map(|vaddr| vaddr.data()) 217 } 218 219 SYS_SBRK => { 220 let increment = args[0] as isize; 221 Self::sbrk(increment).map(|vaddr: VirtAddr| vaddr.data()) 222 } 223 224 SYS_REBOOT => Self::reboot(), 225 226 SYS_CHDIR => { 227 let r = args[0] as *const u8; 228 Self::chdir(r) 229 } 230 231 #[allow(unreachable_patterns)] 232 SYS_GETDENTS64 | SYS_GETDENTS => { 233 let fd = args[0] as i32; 234 235 let buf_vaddr = args[1]; 236 let len = args[2]; 237 let virt_addr: VirtAddr = VirtAddr::new(buf_vaddr); 238 // 判断缓冲区是否来自用户态,进行权限校验 239 let res = if frame.is_from_user() && verify_area(virt_addr, len).is_err() { 240 // 来自用户态,而buffer在内核态,这样的操作不被允许 241 Err(SystemError::EPERM) 242 } else if buf_vaddr == 0 { 243 Err(SystemError::EFAULT) 244 } else { 245 let buf: &mut [u8] = unsafe { 246 core::slice::from_raw_parts_mut::<'static, u8>(buf_vaddr as *mut u8, len) 247 }; 248 Self::getdents(fd, buf) 249 }; 250 251 res 252 } 253 254 SYS_EXECVE => { 255 let path_ptr = args[0]; 256 let argv_ptr = args[1]; 257 let env_ptr = args[2]; 258 let virt_path_ptr = VirtAddr::new(path_ptr); 259 let virt_argv_ptr = VirtAddr::new(argv_ptr); 260 let virt_env_ptr = VirtAddr::new(env_ptr); 261 // 权限校验 262 if frame.is_from_user() 263 && (verify_area(virt_path_ptr, MAX_PATHLEN).is_err() 264 || verify_area(virt_argv_ptr, PAGE_4K_SIZE as usize).is_err()) 265 || verify_area(virt_env_ptr, PAGE_4K_SIZE as usize).is_err() 266 { 267 Err(SystemError::EFAULT) 268 } else { 269 Self::execve( 270 path_ptr as *const u8, 271 argv_ptr as *const *const u8, 272 env_ptr as *const *const u8, 273 frame, 274 ) 275 .map(|_| 0) 276 } 277 } 278 SYS_WAIT4 => { 279 let pid = args[0] as i32; 280 let wstatus = args[1] as *mut i32; 281 let options = args[2] as c_int; 282 let rusage = args[3] as *mut c_void; 283 // 权限校验 284 // todo: 引入rusage之后,更正以下权限校验代码中,rusage的大小 285 Self::wait4(pid.into(), wstatus, options, rusage) 286 } 287 288 SYS_EXIT => { 289 let exit_code = args[0]; 290 Self::exit(exit_code) 291 } 292 #[cfg(target_arch = "x86_64")] 293 SYS_MKDIR => { 294 let path = args[0] as *const u8; 295 let mode = args[1]; 296 297 Self::mkdir(path, mode) 298 } 299 300 SYS_NANOSLEEP => { 301 let req = args[0] as *const PosixTimeSpec; 302 let rem = args[1] as *mut PosixTimeSpec; 303 let virt_req = VirtAddr::new(req as usize); 304 let virt_rem = VirtAddr::new(rem as usize); 305 if frame.is_from_user() 306 && (verify_area(virt_req, core::mem::size_of::<PosixTimeSpec>()).is_err() 307 || verify_area(virt_rem, core::mem::size_of::<PosixTimeSpec>()).is_err()) 308 { 309 Err(SystemError::EFAULT) 310 } else { 311 Self::nanosleep(req, rem) 312 } 313 } 314 315 SYS_CLOCK => Self::clock(), 316 #[cfg(target_arch = "x86_64")] 317 SYS_PIPE => { 318 let pipefd: *mut i32 = args[0] as *mut c_int; 319 if pipefd.is_null() { 320 Err(SystemError::EFAULT) 321 } else { 322 Self::pipe2(pipefd, FileMode::empty()) 323 } 324 } 325 326 SYS_PIPE2 => { 327 let pipefd: *mut i32 = args[0] as *mut c_int; 328 let arg1 = args[1]; 329 if pipefd.is_null() { 330 Err(SystemError::EFAULT) 331 } else { 332 let flags = FileMode::from_bits_truncate(arg1 as u32); 333 Self::pipe2(pipefd, flags) 334 } 335 } 336 337 SYS_UNLINKAT => { 338 let dirfd = args[0] as i32; 339 let path = args[1] as *const u8; 340 let flags = args[2] as u32; 341 Self::unlinkat(dirfd, path, flags) 342 } 343 344 #[cfg(target_arch = "x86_64")] 345 SYS_RMDIR => { 346 let path = args[0] as *const u8; 347 Self::rmdir(path) 348 } 349 350 #[cfg(target_arch = "x86_64")] 351 SYS_LINK => { 352 let old = args[0] as *const u8; 353 let new = args[1] as *const u8; 354 return Self::link(old, new); 355 } 356 357 SYS_LINKAT => { 358 let oldfd = args[0] as i32; 359 let old = args[1] as *const u8; 360 let newfd = args[2] as i32; 361 let new = args[3] as *const u8; 362 let flags = args[4] as i32; 363 return Self::linkat(oldfd, old, newfd, new, flags); 364 } 365 366 #[cfg(target_arch = "x86_64")] 367 SYS_UNLINK => { 368 let path = args[0] as *const u8; 369 Self::unlink(path) 370 } 371 SYS_KILL => { 372 let pid = Pid::new(args[0]); 373 let sig = args[1] as c_int; 374 // kdebug!("KILL SYSCALL RECEIVED"); 375 Self::kill(pid, sig) 376 } 377 378 SYS_RT_SIGACTION => { 379 let sig = args[0] as c_int; 380 let act = args[1]; 381 let old_act = args[2]; 382 Self::sigaction(sig, act, old_act, frame.is_from_user()) 383 } 384 385 SYS_GETPID => Self::getpid().map(|pid| pid.into()), 386 387 SYS_SCHED => { 388 kwarn!("syscall sched"); 389 schedule(SchedMode::SM_NONE); 390 Ok(0) 391 } 392 SYS_DUP => { 393 let oldfd: i32 = args[0] as c_int; 394 Self::dup(oldfd) 395 } 396 397 #[cfg(target_arch = "x86_64")] 398 SYS_DUP2 => { 399 let oldfd: i32 = args[0] as c_int; 400 let newfd: i32 = args[1] as c_int; 401 Self::dup2(oldfd, newfd) 402 } 403 404 SYS_SOCKET => Self::socket(args[0], args[1], args[2]), 405 SYS_SETSOCKOPT => { 406 let optval = args[3] as *const u8; 407 let optlen = args[4]; 408 let virt_optval = VirtAddr::new(optval as usize); 409 // 验证optval的地址是否合法 410 if verify_area(virt_optval, optlen).is_err() { 411 // 地址空间超出了用户空间的范围,不合法 412 Err(SystemError::EFAULT) 413 } else { 414 let data: &[u8] = unsafe { core::slice::from_raw_parts(optval, optlen) }; 415 Self::setsockopt(args[0], args[1], args[2], data) 416 } 417 } 418 SYS_GETSOCKOPT => { 419 let optval = args[3] as *mut u8; 420 let optlen = args[4] as *mut usize; 421 let virt_optval = VirtAddr::new(optval as usize); 422 let virt_optlen = VirtAddr::new(optlen as usize); 423 let security_check = || { 424 // 验证optval的地址是否合法 425 if verify_area(virt_optval, PAGE_4K_SIZE as usize).is_err() { 426 // 地址空间超出了用户空间的范围,不合法 427 return Err(SystemError::EFAULT); 428 } 429 430 // 验证optlen的地址是否合法 431 if verify_area(virt_optlen, core::mem::size_of::<u32>()).is_err() { 432 // 地址空间超出了用户空间的范围,不合法 433 return Err(SystemError::EFAULT); 434 } 435 return Ok(()); 436 }; 437 let r = security_check(); 438 if let Err(e) = r { 439 Err(e) 440 } else { 441 Self::getsockopt(args[0], args[1], args[2], optval, optlen as *mut u32) 442 } 443 } 444 445 SYS_CONNECT => { 446 let addr = args[1] as *const SockAddr; 447 let addrlen = args[2]; 448 let virt_addr = VirtAddr::new(addr as usize); 449 // 验证addr的地址是否合法 450 if verify_area(virt_addr, addrlen).is_err() { 451 // 地址空间超出了用户空间的范围,不合法 452 Err(SystemError::EFAULT) 453 } else { 454 Self::connect(args[0], addr, addrlen) 455 } 456 } 457 SYS_BIND => { 458 let addr = args[1] as *const SockAddr; 459 let addrlen = args[2]; 460 let virt_addr = VirtAddr::new(addr as usize); 461 // 验证addr的地址是否合法 462 if verify_area(virt_addr, addrlen).is_err() { 463 // 地址空间超出了用户空间的范围,不合法 464 Err(SystemError::EFAULT) 465 } else { 466 Self::bind(args[0], addr, addrlen) 467 } 468 } 469 470 SYS_SENDTO => { 471 let buf = args[1] as *const u8; 472 let len = args[2]; 473 let flags = args[3] as u32; 474 let addr = args[4] as *const SockAddr; 475 let addrlen = args[5]; 476 let virt_buf = VirtAddr::new(buf as usize); 477 let virt_addr = VirtAddr::new(addr as usize); 478 // 验证buf的地址是否合法 479 if verify_area(virt_buf, len).is_err() || verify_area(virt_addr, addrlen).is_err() { 480 // 地址空间超出了用户空间的范围,不合法 481 Err(SystemError::EFAULT) 482 } else { 483 let data: &[u8] = unsafe { core::slice::from_raw_parts(buf, len) }; 484 Self::sendto(args[0], data, flags, addr, addrlen) 485 } 486 } 487 488 SYS_RECVFROM => { 489 let buf = args[1] as *mut u8; 490 let len = args[2]; 491 let flags = args[3] as u32; 492 let addr = args[4] as *mut SockAddr; 493 let addrlen = args[5] as *mut usize; 494 let virt_buf = VirtAddr::new(buf as usize); 495 let virt_addrlen = VirtAddr::new(addrlen as usize); 496 let virt_addr = VirtAddr::new(addr as usize); 497 let security_check = || { 498 // 验证buf的地址是否合法 499 if verify_area(virt_buf, len).is_err() { 500 // 地址空间超出了用户空间的范围,不合法 501 return Err(SystemError::EFAULT); 502 } 503 504 // 验证addrlen的地址是否合法 505 if verify_area(virt_addrlen, core::mem::size_of::<u32>()).is_err() { 506 // 地址空间超出了用户空间的范围,不合法 507 return Err(SystemError::EFAULT); 508 } 509 510 if verify_area(virt_addr, core::mem::size_of::<SockAddr>()).is_err() { 511 // 地址空间超出了用户空间的范围,不合法 512 return Err(SystemError::EFAULT); 513 } 514 return Ok(()); 515 }; 516 let r = security_check(); 517 if let Err(e) = r { 518 Err(e) 519 } else { 520 let buf = unsafe { core::slice::from_raw_parts_mut(buf, len) }; 521 Self::recvfrom(args[0], buf, flags, addr, addrlen as *mut u32) 522 } 523 } 524 525 SYS_RECVMSG => { 526 let msg = args[1] as *mut MsgHdr; 527 let flags = args[2] as u32; 528 529 let mut user_buffer_writer = UserBufferWriter::new( 530 msg, 531 core::mem::size_of::<MsgHdr>(), 532 frame.is_from_user(), 533 )?; 534 let buffer = user_buffer_writer.buffer::<MsgHdr>(0)?; 535 536 let msg = &mut buffer[0]; 537 Self::recvmsg(args[0], msg, flags) 538 } 539 540 SYS_LISTEN => Self::listen(args[0], args[1]), 541 SYS_SHUTDOWN => Self::shutdown(args[0], args[1]), 542 SYS_ACCEPT => Self::accept(args[0], args[1] as *mut SockAddr, args[2] as *mut u32), 543 SYS_ACCEPT4 => Self::accept4( 544 args[0], 545 args[1] as *mut SockAddr, 546 args[2] as *mut u32, 547 args[3] as u32, 548 ), 549 SYS_GETSOCKNAME => { 550 Self::getsockname(args[0], args[1] as *mut SockAddr, args[2] as *mut u32) 551 } 552 SYS_GETPEERNAME => { 553 Self::getpeername(args[0], args[1] as *mut SockAddr, args[2] as *mut u32) 554 } 555 SYS_GETTIMEOFDAY => { 556 let timeval = args[0] as *mut PosixTimeval; 557 let timezone_ptr = args[1] as *mut PosixTimeZone; 558 Self::gettimeofday(timeval, timezone_ptr) 559 } 560 SYS_MMAP => { 561 let len = page_align_up(args[1]); 562 let virt_addr = VirtAddr::new(args[0]); 563 if verify_area(virt_addr, len).is_err() { 564 Err(SystemError::EFAULT) 565 } else { 566 Self::mmap( 567 VirtAddr::new(args[0]), 568 len, 569 args[2], 570 args[3], 571 args[4] as i32, 572 args[5], 573 ) 574 } 575 } 576 SYS_MREMAP => { 577 let old_vaddr = VirtAddr::new(args[0]); 578 let old_len = args[1]; 579 let new_len = args[2]; 580 let mremap_flags = MremapFlags::from_bits_truncate(args[3] as u8); 581 let new_vaddr = VirtAddr::new(args[4]); 582 583 Self::mremap(old_vaddr, old_len, new_len, mremap_flags, new_vaddr) 584 } 585 SYS_MUNMAP => { 586 let addr = args[0]; 587 let len = page_align_up(args[1]); 588 if addr & (MMArch::PAGE_SIZE - 1) != 0 { 589 // The addr argument is not a multiple of the page size 590 Err(SystemError::EINVAL) 591 } else { 592 Self::munmap(VirtAddr::new(addr), len) 593 } 594 } 595 SYS_MPROTECT => { 596 let addr = args[0]; 597 let len = page_align_up(args[1]); 598 if addr & (MMArch::PAGE_SIZE - 1) != 0 { 599 // The addr argument is not a multiple of the page size 600 Err(SystemError::EINVAL) 601 } else { 602 Self::mprotect(VirtAddr::new(addr), len, args[2]) 603 } 604 } 605 606 SYS_GETCWD => { 607 let buf = args[0] as *mut u8; 608 let size = args[1]; 609 let security_check = || { 610 verify_area(VirtAddr::new(buf as usize), size)?; 611 return Ok(()); 612 }; 613 let r = security_check(); 614 if let Err(e) = r { 615 Err(e) 616 } else { 617 let buf = unsafe { core::slice::from_raw_parts_mut(buf, size) }; 618 Self::getcwd(buf).map(|ptr| ptr.data()) 619 } 620 } 621 622 SYS_GETPGID => Self::getpgid(Pid::new(args[0])).map(|pid| pid.into()), 623 624 SYS_GETPPID => Self::getppid().map(|pid| pid.into()), 625 SYS_FSTAT => { 626 let fd = args[0] as i32; 627 let kstat: *mut PosixKstat = args[1] as *mut PosixKstat; 628 let vaddr = VirtAddr::new(kstat as usize); 629 // FIXME 由于c中的verify_area与rust中的verify_area重名,所以在引入时加了前缀区分 630 // TODO 应该将用了c版本的verify_area都改为rust的verify_area 631 match verify_area(vaddr, core::mem::size_of::<PosixKstat>()) { 632 Ok(_) => Self::fstat(fd, kstat), 633 Err(e) => Err(e), 634 } 635 } 636 637 SYS_FCNTL => { 638 let fd = args[0] as i32; 639 let cmd: Option<FcntlCommand> = 640 <FcntlCommand as FromPrimitive>::from_u32(args[1] as u32); 641 let arg = args[2] as i32; 642 let res = if let Some(cmd) = cmd { 643 Self::fcntl(fd, cmd, arg) 644 } else { 645 Err(SystemError::EINVAL) 646 }; 647 648 // kdebug!("FCNTL: fd: {}, cmd: {:?}, arg: {}, res: {:?}", fd, cmd, arg, res); 649 res 650 } 651 652 SYS_FTRUNCATE => { 653 let fd = args[0] as i32; 654 let len = args[1]; 655 let res = Self::ftruncate(fd, len); 656 // kdebug!("FTRUNCATE: fd: {}, len: {}, res: {:?}", fd, len, res); 657 res 658 } 659 660 #[cfg(target_arch = "x86_64")] 661 SYS_MKNOD => { 662 let path = args[0]; 663 let flags = args[1]; 664 let dev_t = args[2]; 665 let flags: ModeType = ModeType::from_bits_truncate(flags as u32); 666 Self::mknod(path as *const u8, flags, DeviceNumber::from(dev_t as u32)) 667 } 668 669 SYS_CLONE => { 670 let parent_tid = VirtAddr::new(args[2]); 671 let child_tid = VirtAddr::new(args[3]); 672 673 // 地址校验 674 verify_area(parent_tid, core::mem::size_of::<i32>())?; 675 verify_area(child_tid, core::mem::size_of::<i32>())?; 676 677 let mut clone_args = KernelCloneArgs::new(); 678 clone_args.flags = CloneFlags::from_bits_truncate(args[0] as u64); 679 clone_args.stack = args[1]; 680 clone_args.parent_tid = parent_tid; 681 clone_args.child_tid = child_tid; 682 clone_args.tls = args[4]; 683 Self::clone(frame, clone_args) 684 } 685 686 SYS_FUTEX => { 687 let uaddr = VirtAddr::new(args[0]); 688 let operation = FutexFlag::from_bits(args[1] as u32).ok_or(SystemError::ENOSYS)?; 689 let val = args[2] as u32; 690 let utime = args[3]; 691 let uaddr2 = VirtAddr::new(args[4]); 692 let val3 = args[5] as u32; 693 694 let mut timespec = None; 695 if utime != 0 && operation.contains(FutexFlag::FLAGS_HAS_TIMEOUT) { 696 let reader = UserBufferReader::new( 697 utime as *const PosixTimeSpec, 698 core::mem::size_of::<PosixTimeSpec>(), 699 true, 700 )?; 701 702 timespec = Some(*reader.read_one_from_user::<PosixTimeSpec>(0)?); 703 } 704 705 Self::do_futex(uaddr, operation, val, timespec, uaddr2, utime as u32, val3) 706 } 707 708 SYS_SET_ROBUST_LIST => { 709 let head = args[0]; 710 let head_uaddr = VirtAddr::new(head); 711 let len = args[1]; 712 713 let ret = Self::set_robust_list(head_uaddr, len); 714 return ret; 715 } 716 717 SYS_GET_ROBUST_LIST => { 718 let pid = args[0]; 719 let head = args[1]; 720 let head_uaddr = VirtAddr::new(head); 721 let len_ptr = args[2]; 722 let len_ptr_uaddr = VirtAddr::new(len_ptr); 723 724 let ret = Self::get_robust_list(pid, head_uaddr, len_ptr_uaddr); 725 return ret; 726 } 727 728 SYS_READV => Self::readv(args[0] as i32, args[1], args[2]), 729 SYS_WRITEV => Self::writev(args[0] as i32, args[1], args[2]), 730 731 SYS_SET_TID_ADDRESS => Self::set_tid_address(args[0]), 732 733 #[cfg(target_arch = "x86_64")] 734 SYS_LSTAT => { 735 let path = args[0] as *const u8; 736 let kstat = args[1] as *mut PosixKstat; 737 Self::lstat(path, kstat) 738 } 739 740 #[cfg(target_arch = "x86_64")] 741 SYS_STAT => { 742 let path = args[0] as *const u8; 743 let kstat = args[1] as *mut PosixKstat; 744 Self::stat(path, kstat) 745 } 746 747 SYS_STATFS => { 748 let path = args[0] as *const u8; 749 let statfs = args[1] as *mut PosixStatfs; 750 Self::statfs(path, statfs) 751 } 752 753 SYS_FSTATFS => { 754 let fd = args[0] as i32; 755 let statfs = args[1] as *mut PosixStatfs; 756 Self::fstatfs(fd, statfs) 757 } 758 759 SYS_STATX => { 760 let fd = args[0] as i32; 761 let path = args[1] as *const u8; 762 let flags = args[2] as u32; 763 let mask = args[3] as u32; 764 let kstat = args[4] as *mut PosixStatx; 765 766 Self::do_statx(fd, path, flags, mask, kstat) 767 } 768 769 #[cfg(target_arch = "x86_64")] 770 SYS_EPOLL_CREATE => Self::epoll_create(args[0] as i32), 771 SYS_EPOLL_CREATE1 => Self::epoll_create1(args[0]), 772 773 SYS_EPOLL_CTL => Self::epoll_ctl( 774 args[0] as i32, 775 args[1], 776 args[2] as i32, 777 VirtAddr::new(args[3]), 778 ), 779 780 #[cfg(target_arch = "x86_64")] 781 SYS_EPOLL_WAIT => Self::epoll_wait( 782 args[0] as i32, 783 VirtAddr::new(args[1]), 784 args[2] as i32, 785 args[3] as i32, 786 ), 787 788 SYS_EPOLL_PWAIT => { 789 let epfd = args[0] as i32; 790 let epoll_event = VirtAddr::new(args[1]); 791 let max_events = args[2] as i32; 792 let timespec = args[3] as i32; 793 let sigmask_addr = args[4] as *mut SigSet; 794 795 if sigmask_addr.is_null() { 796 return Self::epoll_wait(epfd, epoll_event, max_events, timespec); 797 } 798 let sigmask_reader = 799 UserBufferReader::new(sigmask_addr, core::mem::size_of::<SigSet>(), true)?; 800 let mut sigmask = *sigmask_reader.read_one_from_user::<SigSet>(0)?; 801 802 Self::epoll_pwait( 803 args[0] as i32, 804 VirtAddr::new(args[1]), 805 args[2] as i32, 806 args[3] as i32, 807 &mut sigmask, 808 ) 809 } 810 811 // 目前为了适配musl-libc,以下系统调用先这样写着 812 SYS_GETRANDOM => { 813 let flags = GRandFlags::from_bits(args[2] as u8).ok_or(SystemError::EINVAL)?; 814 Self::get_random(args[0] as *mut u8, args[1], flags) 815 } 816 817 SYS_SOCKETPAIR => { 818 let mut user_buffer_writer = UserBufferWriter::new( 819 args[3] as *mut c_int, 820 core::mem::size_of::<[c_int; 2]>(), 821 frame.is_from_user(), 822 )?; 823 let fds = user_buffer_writer.buffer::<i32>(0)?; 824 Self::socketpair(args[0], args[1], args[2], fds) 825 } 826 827 #[cfg(target_arch = "x86_64")] 828 SYS_POLL => { 829 kwarn!("SYS_POLL has not yet been implemented"); 830 Ok(0) 831 } 832 833 SYS_SETPGID => { 834 kwarn!("SYS_SETPGID has not yet been implemented"); 835 Ok(0) 836 } 837 838 SYS_RT_SIGPROCMASK => { 839 kwarn!("SYS_RT_SIGPROCMASK has not yet been implemented"); 840 Ok(0) 841 } 842 843 SYS_TKILL => { 844 kwarn!("SYS_TKILL has not yet been implemented"); 845 Ok(0) 846 } 847 848 SYS_SIGALTSTACK => { 849 kwarn!("SYS_SIGALTSTACK has not yet been implemented"); 850 Ok(0) 851 } 852 853 SYS_EXIT_GROUP => { 854 kwarn!("SYS_EXIT_GROUP has not yet been implemented"); 855 Ok(0) 856 } 857 858 SYS_MADVISE => { 859 // 这个太吵了,总是打印,先注释掉 860 // kwarn!("SYS_MADVISE has not yet been implemented"); 861 Ok(0) 862 } 863 SYS_GETTID => Self::gettid().map(|tid| tid.into()), 864 SYS_GETUID => Self::getuid(), 865 866 SYS_SYSLOG => { 867 let syslog_action_type = args[0]; 868 let buf_vaddr = args[1]; 869 let len = args[2]; 870 let from_user = frame.is_from_user(); 871 let mut user_buffer_writer = 872 UserBufferWriter::new(buf_vaddr as *mut u8, len, from_user)?; 873 874 let user_buf = user_buffer_writer.buffer(0)?; 875 Self::do_syslog(syslog_action_type, user_buf, len) 876 } 877 878 SYS_GETGID => Self::getgid(), 879 SYS_SETUID => { 880 kwarn!("SYS_SETUID has not yet been implemented"); 881 Ok(0) 882 } 883 SYS_SETGID => { 884 kwarn!("SYS_SETGID has not yet been implemented"); 885 Ok(0) 886 } 887 SYS_SETSID => { 888 kwarn!("SYS_SETSID has not yet been implemented"); 889 Ok(0) 890 } 891 SYS_GETEUID => Self::geteuid(), 892 SYS_GETEGID => Self::getegid(), 893 SYS_GETRUSAGE => { 894 let who = args[0] as c_int; 895 let rusage = args[1] as *mut RUsage; 896 Self::get_rusage(who, rusage) 897 } 898 899 #[cfg(target_arch = "x86_64")] 900 SYS_READLINK => { 901 let path = args[0] as *const u8; 902 let buf = args[1] as *mut u8; 903 let bufsiz = args[2]; 904 Self::readlink(path, buf, bufsiz) 905 } 906 907 SYS_READLINKAT => { 908 let dirfd = args[0] as i32; 909 let path = args[1] as *const u8; 910 let buf = args[2] as *mut u8; 911 let bufsiz = args[3]; 912 Self::readlink_at(dirfd, path, buf, bufsiz) 913 } 914 915 SYS_PRLIMIT64 => { 916 let pid = args[0]; 917 let pid = Pid::new(pid); 918 let resource = args[1]; 919 let new_limit = args[2] as *const RLimit64; 920 let old_limit = args[3] as *mut RLimit64; 921 922 Self::prlimit64(pid, resource, new_limit, old_limit) 923 } 924 925 #[cfg(target_arch = "x86_64")] 926 SYS_ACCESS => { 927 let pathname = args[0] as *const u8; 928 let mode = args[1] as u32; 929 Self::access(pathname, mode) 930 } 931 932 SYS_FACCESSAT => { 933 let dirfd = args[0] as i32; 934 let pathname = args[1] as *const u8; 935 let mode = args[2] as u32; 936 Self::faccessat2(dirfd, pathname, mode, 0) 937 } 938 939 SYS_FACCESSAT2 => { 940 let dirfd = args[0] as i32; 941 let pathname = args[1] as *const u8; 942 let mode = args[2] as u32; 943 let flags = args[3] as u32; 944 Self::faccessat2(dirfd, pathname, mode, flags) 945 } 946 947 SYS_CLOCK_GETTIME => { 948 let clockid = args[0] as i32; 949 let timespec = args[1] as *mut PosixTimeSpec; 950 Self::clock_gettime(clockid, timespec) 951 } 952 953 SYS_SYSINFO => { 954 let info = args[0] as *mut SysInfo; 955 Self::sysinfo(info) 956 } 957 958 SYS_UMASK => { 959 let mask = args[0] as u32; 960 Self::umask(mask) 961 } 962 963 SYS_FCHOWN => { 964 kwarn!("SYS_FCHOWN has not yet been implemented"); 965 Ok(0) 966 } 967 968 SYS_FSYNC => { 969 kwarn!("SYS_FSYNC has not yet been implemented"); 970 Ok(0) 971 } 972 973 SYS_RSEQ => { 974 kwarn!("SYS_RSEQ has not yet been implemented"); 975 Ok(0) 976 } 977 978 #[cfg(target_arch = "x86_64")] 979 SYS_CHMOD => { 980 let pathname = args[0] as *const u8; 981 let mode = args[1] as u32; 982 Self::chmod(pathname, mode) 983 } 984 SYS_FCHMOD => { 985 let fd = args[0] as i32; 986 let mode = args[1] as u32; 987 Self::fchmod(fd, mode) 988 } 989 SYS_FCHMODAT => { 990 let dirfd = args[0] as i32; 991 let pathname = args[1] as *const u8; 992 let mode = args[2] as u32; 993 Self::fchmodat(dirfd, pathname, mode) 994 } 995 996 SYS_SCHED_GETAFFINITY => { 997 let pid = args[0] as i32; 998 let size = args[1]; 999 let set_vaddr = args[2]; 1000 1001 let mut user_buffer_writer = 1002 UserBufferWriter::new(set_vaddr as *mut u8, size, frame.is_from_user())?; 1003 let set: &mut [u8] = user_buffer_writer.buffer(0)?; 1004 1005 Self::getaffinity(pid, set) 1006 } 1007 1008 #[cfg(target_arch = "x86_64")] 1009 SYS_GETRLIMIT => { 1010 let resource = args[0]; 1011 let rlimit = args[1] as *mut RLimit64; 1012 1013 Self::prlimit64( 1014 ProcessManager::current_pcb().pid(), 1015 resource, 1016 core::ptr::null::<RLimit64>(), 1017 rlimit, 1018 ) 1019 } 1020 1021 SYS_FADVISE64 => { 1022 // todo: 这个系统调用还没有实现 1023 1024 Err(SystemError::ENOSYS) 1025 } 1026 1027 SYS_MOUNT => { 1028 let source = args[0] as *const u8; 1029 let target = args[1] as *const u8; 1030 let filesystemtype = args[2] as *const u8; 1031 return Self::mount(source, target, filesystemtype, 0, null()); 1032 } 1033 SYS_NEWFSTATAT => { 1034 // todo: 这个系统调用还没有实现 1035 1036 Err(SystemError::ENOSYS) 1037 } 1038 1039 // SYS_SCHED_YIELD => Self::sched_yield(), 1040 SYS_UNAME => { 1041 let name = args[0] as *mut PosixOldUtsName; 1042 Self::uname(name) 1043 } 1044 SYS_PRCTL => { 1045 // todo: 这个系统调用还没有实现 1046 1047 Err(SystemError::EINVAL) 1048 } 1049 1050 SYS_SHMGET => { 1051 let key = ShmKey::new(args[0]); 1052 let size = args[1]; 1053 let shmflg = ShmFlags::from_bits_truncate(args[2] as u32); 1054 1055 Self::shmget(key, size, shmflg) 1056 } 1057 SYS_SHMAT => { 1058 let id = ShmId::new(args[0]); 1059 let vaddr = VirtAddr::new(args[1]); 1060 let shmflg = ShmFlags::from_bits_truncate(args[2] as u32); 1061 1062 Self::shmat(id, vaddr, shmflg) 1063 } 1064 SYS_SHMDT => { 1065 let vaddr = VirtAddr::new(args[0]); 1066 Self::shmdt(vaddr) 1067 } 1068 SYS_SHMCTL => { 1069 let id = ShmId::new(args[0]); 1070 let cmd = ShmCtlCmd::from(args[1]); 1071 let user_buf = args[2] as *const u8; 1072 let from_user = frame.is_from_user(); 1073 1074 Self::shmctl(id, cmd, user_buf, from_user) 1075 } 1076 1077 _ => panic!("Unsupported syscall ID: {}", syscall_num), 1078 }; 1079 1080 if ProcessManager::current_pcb() 1081 .flags() 1082 .contains(ProcessFlags::NEED_SCHEDULE) 1083 { 1084 schedule(SchedMode::SM_PREEMPT); 1085 } 1086 1087 return r; 1088 } 1089 1090 pub fn put_string( 1091 s: *const u8, 1092 front_color: u32, 1093 back_color: u32, 1094 ) -> Result<usize, SystemError> { 1095 // todo: 删除这个系统调用 1096 let s = check_and_clone_cstr(s, Some(4096))?; 1097 let fr = (front_color & 0x00ff0000) >> 16; 1098 let fg = (front_color & 0x0000ff00) >> 8; 1099 let fb = front_color & 0x000000ff; 1100 let br = (back_color & 0x00ff0000) >> 16; 1101 let bg = (back_color & 0x0000ff00) >> 8; 1102 let bb = back_color & 0x000000ff; 1103 print!("\x1B[38;2;{fr};{fg};{fb};48;2;{br};{bg};{bb}m{s}\x1B[0m"); 1104 return Ok(s.len()); 1105 } 1106 1107 pub fn reboot() -> Result<usize, SystemError> { 1108 unsafe { cpu_reset() }; 1109 } 1110 } 1111