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