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