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