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 filesystem::vfs::syscall::{PosixStatfs, PosixStatx}, 9 ipc::shm::{ShmCtlCmd, ShmFlags, ShmId, ShmKey}, 10 libs::{futex::constant::FutexFlag, rand::GRandFlags}, 11 mm::{page::PAGE_4K_SIZE, syscall::MremapFlags}, 12 net::syscall::MsgHdr, 13 process::{ 14 fork::KernelCloneArgs, 15 resource::{RLimit64, RUsage}, 16 ProcessFlags, ProcessManager, 17 }, 18 sched::{schedule, SchedMode}, 19 syscall::user_access::check_and_clone_cstr, 20 }; 21 22 use log::{info, warn}; 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, UtimensFlags}, 32 MAX_PATHLEN, 33 }, 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 PosixTimeSpec, 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)] init() -> Result<(), SystemError>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 info!("Initializing syscall..."); 72 let r = crate::arch::syscall::arch_syscall_init(); 73 info!("Syscall init successfully!"); 74 75 return r; 76 } 77 /// @brief 系统调用分发器,用于分发系统调用。 78 /// 79 /// 这个函数内,需要根据系统调用号,调用对应的系统调用处理函数。 80 /// 并且,对于用户态传入的指针参数,需要在本函数内进行越界检查,防止访问到内核空间。 81 #[inline(never)] handle( syscall_num: usize, args: &[usize], frame: &mut TrapFrame, ) -> Result<usize, SystemError>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).is_err()) 262 || verify_area(virt_env_ptr, PAGE_4K_SIZE).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_MKDIRAT => { 298 let dirfd = args[0] as i32; 299 let path = args[1] as *const u8; 300 let mode = args[2]; 301 Self::mkdir_at(dirfd, path, mode) 302 } 303 304 SYS_NANOSLEEP => { 305 let req = args[0] as *const PosixTimeSpec; 306 let rem = args[1] as *mut PosixTimeSpec; 307 let virt_req = VirtAddr::new(req as usize); 308 let virt_rem = VirtAddr::new(rem as usize); 309 if frame.is_from_user() 310 && (verify_area(virt_req, core::mem::size_of::<PosixTimeSpec>()).is_err() 311 || verify_area(virt_rem, core::mem::size_of::<PosixTimeSpec>()).is_err()) 312 { 313 Err(SystemError::EFAULT) 314 } else { 315 Self::nanosleep(req, rem) 316 } 317 } 318 319 SYS_CLOCK => Self::clock(), 320 #[cfg(target_arch = "x86_64")] 321 SYS_PIPE => { 322 let pipefd: *mut i32 = args[0] as *mut c_int; 323 if pipefd.is_null() { 324 Err(SystemError::EFAULT) 325 } else { 326 Self::pipe2(pipefd, FileMode::empty()) 327 } 328 } 329 330 SYS_PIPE2 => { 331 let pipefd: *mut i32 = args[0] as *mut c_int; 332 let arg1 = args[1]; 333 if pipefd.is_null() { 334 Err(SystemError::EFAULT) 335 } else { 336 let flags = FileMode::from_bits_truncate(arg1 as u32); 337 Self::pipe2(pipefd, flags) 338 } 339 } 340 341 SYS_UNLINKAT => { 342 let dirfd = args[0] as i32; 343 let path = args[1] as *const u8; 344 let flags = args[2] as u32; 345 Self::unlinkat(dirfd, path, flags) 346 } 347 348 #[cfg(target_arch = "x86_64")] 349 SYS_SYMLINK => { 350 let oldname = args[0] as *const u8; 351 let newname = args[1] as *const u8; 352 Self::symlink(oldname, newname) 353 } 354 355 SYS_SYMLINKAT => { 356 let oldname = args[0] as *const u8; 357 let newdfd = args[1] as i32; 358 let newname = args[2] as *const u8; 359 Self::symlinkat(oldname, newdfd, newname) 360 } 361 362 #[cfg(target_arch = "x86_64")] 363 SYS_RMDIR => { 364 let path = args[0] as *const u8; 365 Self::rmdir(path) 366 } 367 368 #[cfg(target_arch = "x86_64")] 369 SYS_LINK => { 370 let old = args[0] as *const u8; 371 let new = args[1] as *const u8; 372 return Self::link(old, new); 373 } 374 375 SYS_LINKAT => { 376 let oldfd = args[0] as i32; 377 let old = args[1] as *const u8; 378 let newfd = args[2] as i32; 379 let new = args[3] as *const u8; 380 let flags = args[4] as i32; 381 return Self::linkat(oldfd, old, newfd, new, flags); 382 } 383 384 #[cfg(target_arch = "x86_64")] 385 SYS_UNLINK => { 386 let path = args[0] as *const u8; 387 Self::unlink(path) 388 } 389 SYS_KILL => { 390 let pid = Pid::new(args[0]); 391 let sig = args[1] as c_int; 392 // debug!("KILL SYSCALL RECEIVED"); 393 Self::kill(pid, sig) 394 } 395 396 SYS_RT_SIGACTION => { 397 let sig = args[0] as c_int; 398 let act = args[1]; 399 let old_act = args[2]; 400 Self::sigaction(sig, act, old_act, frame.is_from_user()) 401 } 402 403 SYS_GETPID => Self::getpid().map(|pid| pid.into()), 404 405 SYS_SCHED => { 406 warn!("syscall sched"); 407 schedule(SchedMode::SM_NONE); 408 Ok(0) 409 } 410 SYS_DUP => { 411 let oldfd: i32 = args[0] as c_int; 412 Self::dup(oldfd) 413 } 414 415 #[cfg(target_arch = "x86_64")] 416 SYS_DUP2 => { 417 let oldfd: i32 = args[0] as c_int; 418 let newfd: i32 = args[1] as c_int; 419 Self::dup2(oldfd, newfd) 420 } 421 422 SYS_DUP3 => { 423 let oldfd: i32 = args[0] as c_int; 424 let newfd: i32 = args[1] as c_int; 425 let flags: u32 = args[2] as u32; 426 Self::dup3(oldfd, newfd, flags) 427 } 428 429 SYS_SOCKET => Self::socket(args[0], args[1], args[2]), 430 SYS_SETSOCKOPT => { 431 let optval = args[3] as *const u8; 432 let optlen = args[4]; 433 let virt_optval = VirtAddr::new(optval as usize); 434 // 验证optval的地址是否合法 435 if verify_area(virt_optval, optlen).is_err() { 436 // 地址空间超出了用户空间的范围,不合法 437 Err(SystemError::EFAULT) 438 } else { 439 let data: &[u8] = unsafe { core::slice::from_raw_parts(optval, optlen) }; 440 Self::setsockopt(args[0], args[1], args[2], data) 441 } 442 } 443 SYS_GETSOCKOPT => { 444 let optval = args[3] as *mut u8; 445 let optlen = args[4] as *mut usize; 446 let virt_optval = VirtAddr::new(optval as usize); 447 let virt_optlen = VirtAddr::new(optlen as usize); 448 let security_check = || { 449 // 验证optval的地址是否合法 450 if verify_area(virt_optval, PAGE_4K_SIZE).is_err() { 451 // 地址空间超出了用户空间的范围,不合法 452 return Err(SystemError::EFAULT); 453 } 454 455 // 验证optlen的地址是否合法 456 if verify_area(virt_optlen, core::mem::size_of::<u32>()).is_err() { 457 // 地址空间超出了用户空间的范围,不合法 458 return Err(SystemError::EFAULT); 459 } 460 return Ok(()); 461 }; 462 let r = security_check(); 463 if let Err(e) = r { 464 Err(e) 465 } else { 466 Self::getsockopt(args[0], args[1], args[2], optval, optlen as *mut u32) 467 } 468 } 469 470 SYS_CONNECT => { 471 let addr = args[1] as *const SockAddr; 472 let addrlen = args[2]; 473 let virt_addr = VirtAddr::new(addr as usize); 474 // 验证addr的地址是否合法 475 if verify_area(virt_addr, addrlen).is_err() { 476 // 地址空间超出了用户空间的范围,不合法 477 Err(SystemError::EFAULT) 478 } else { 479 Self::connect(args[0], addr, addrlen) 480 } 481 } 482 SYS_BIND => { 483 let addr = args[1] as *const SockAddr; 484 let addrlen = args[2]; 485 let virt_addr = VirtAddr::new(addr as usize); 486 // 验证addr的地址是否合法 487 if verify_area(virt_addr, addrlen).is_err() { 488 // 地址空间超出了用户空间的范围,不合法 489 Err(SystemError::EFAULT) 490 } else { 491 Self::bind(args[0], addr, addrlen) 492 } 493 } 494 495 SYS_SENDTO => { 496 let buf = args[1] as *const u8; 497 let len = args[2]; 498 let flags = args[3] as u32; 499 let addr = args[4] as *const SockAddr; 500 let addrlen = args[5]; 501 let virt_buf = VirtAddr::new(buf as usize); 502 let virt_addr = VirtAddr::new(addr as usize); 503 // 验证buf的地址是否合法 504 if verify_area(virt_buf, len).is_err() || verify_area(virt_addr, addrlen).is_err() { 505 // 地址空间超出了用户空间的范围,不合法 506 Err(SystemError::EFAULT) 507 } else { 508 let data: &[u8] = unsafe { core::slice::from_raw_parts(buf, len) }; 509 Self::sendto(args[0], data, flags, addr, addrlen) 510 } 511 } 512 513 SYS_RECVFROM => { 514 let buf = args[1] as *mut u8; 515 let len = args[2]; 516 let flags = args[3] as u32; 517 let addr = args[4] as *mut SockAddr; 518 let addrlen = args[5] as *mut usize; 519 let virt_buf = VirtAddr::new(buf as usize); 520 let virt_addrlen = VirtAddr::new(addrlen as usize); 521 let virt_addr = VirtAddr::new(addr as usize); 522 let security_check = || { 523 // 验证buf的地址是否合法 524 if verify_area(virt_buf, len).is_err() { 525 // 地址空间超出了用户空间的范围,不合法 526 return Err(SystemError::EFAULT); 527 } 528 529 // 验证addrlen的地址是否合法 530 if verify_area(virt_addrlen, core::mem::size_of::<u32>()).is_err() { 531 // 地址空间超出了用户空间的范围,不合法 532 return Err(SystemError::EFAULT); 533 } 534 535 if verify_area(virt_addr, core::mem::size_of::<SockAddr>()).is_err() { 536 // 地址空间超出了用户空间的范围,不合法 537 return Err(SystemError::EFAULT); 538 } 539 return Ok(()); 540 }; 541 let r = security_check(); 542 if let Err(e) = r { 543 Err(e) 544 } else { 545 let buf = unsafe { core::slice::from_raw_parts_mut(buf, len) }; 546 Self::recvfrom(args[0], buf, flags, addr, addrlen as *mut u32) 547 } 548 } 549 550 SYS_RECVMSG => { 551 let msg = args[1] as *mut MsgHdr; 552 let flags = args[2] as u32; 553 554 let mut user_buffer_writer = UserBufferWriter::new( 555 msg, 556 core::mem::size_of::<MsgHdr>(), 557 frame.is_from_user(), 558 )?; 559 let buffer = user_buffer_writer.buffer::<MsgHdr>(0)?; 560 561 let msg = &mut buffer[0]; 562 Self::recvmsg(args[0], msg, flags) 563 } 564 565 SYS_LISTEN => Self::listen(args[0], args[1]), 566 SYS_SHUTDOWN => Self::shutdown(args[0], args[1]), 567 SYS_ACCEPT => Self::accept(args[0], args[1] as *mut SockAddr, args[2] as *mut u32), 568 SYS_ACCEPT4 => Self::accept4( 569 args[0], 570 args[1] as *mut SockAddr, 571 args[2] as *mut u32, 572 args[3] as u32, 573 ), 574 SYS_GETSOCKNAME => { 575 Self::getsockname(args[0], args[1] as *mut SockAddr, args[2] as *mut u32) 576 } 577 SYS_GETPEERNAME => { 578 Self::getpeername(args[0], args[1] as *mut SockAddr, args[2] as *mut u32) 579 } 580 SYS_GETTIMEOFDAY => { 581 let timeval = args[0] as *mut PosixTimeval; 582 let timezone_ptr = args[1] as *mut PosixTimeZone; 583 Self::gettimeofday(timeval, timezone_ptr) 584 } 585 SYS_MMAP => { 586 let len = page_align_up(args[1]); 587 let virt_addr = VirtAddr::new(args[0]); 588 if verify_area(virt_addr, len).is_err() { 589 Err(SystemError::EFAULT) 590 } else { 591 Self::mmap( 592 VirtAddr::new(args[0]), 593 len, 594 args[2], 595 args[3], 596 args[4] as i32, 597 args[5], 598 ) 599 } 600 } 601 SYS_MREMAP => { 602 let old_vaddr = VirtAddr::new(args[0]); 603 let old_len = args[1]; 604 let new_len = args[2]; 605 let mremap_flags = MremapFlags::from_bits_truncate(args[3] as u8); 606 let new_vaddr = VirtAddr::new(args[4]); 607 608 Self::mremap(old_vaddr, old_len, new_len, mremap_flags, new_vaddr) 609 } 610 SYS_MUNMAP => { 611 let addr = args[0]; 612 let len = page_align_up(args[1]); 613 if addr & (MMArch::PAGE_SIZE - 1) != 0 { 614 // The addr argument is not a multiple of the page size 615 Err(SystemError::EINVAL) 616 } else { 617 Self::munmap(VirtAddr::new(addr), len) 618 } 619 } 620 SYS_MPROTECT => { 621 let addr = args[0]; 622 let len = page_align_up(args[1]); 623 if addr & (MMArch::PAGE_SIZE - 1) != 0 { 624 // The addr argument is not a multiple of the page size 625 Err(SystemError::EINVAL) 626 } else { 627 Self::mprotect(VirtAddr::new(addr), len, args[2]) 628 } 629 } 630 631 SYS_GETCWD => { 632 let buf = args[0] as *mut u8; 633 let size = args[1]; 634 let security_check = || { 635 verify_area(VirtAddr::new(buf as usize), size)?; 636 return Ok(()); 637 }; 638 let r = security_check(); 639 if let Err(e) = r { 640 Err(e) 641 } else { 642 let buf = unsafe { core::slice::from_raw_parts_mut(buf, size) }; 643 Self::getcwd(buf).map(|ptr| ptr.data()) 644 } 645 } 646 647 SYS_GETPGID => Self::getpgid(Pid::new(args[0])).map(|pid| pid.into()), 648 649 SYS_GETPPID => Self::getppid().map(|pid| pid.into()), 650 SYS_FSTAT => { 651 let fd = args[0] as i32; 652 let kstat: *mut PosixKstat = args[1] as *mut PosixKstat; 653 let vaddr = VirtAddr::new(kstat as usize); 654 // FIXME 由于c中的verify_area与rust中的verify_area重名,所以在引入时加了前缀区分 655 // TODO 应该将用了c版本的verify_area都改为rust的verify_area 656 match verify_area(vaddr, core::mem::size_of::<PosixKstat>()) { 657 Ok(_) => Self::fstat(fd, kstat), 658 Err(e) => Err(e), 659 } 660 } 661 662 SYS_FCNTL => { 663 let fd = args[0] as i32; 664 let cmd: Option<FcntlCommand> = 665 <FcntlCommand as FromPrimitive>::from_u32(args[1] as u32); 666 let arg = args[2] as i32; 667 let res = if let Some(cmd) = cmd { 668 Self::fcntl(fd, cmd, arg) 669 } else { 670 Err(SystemError::EINVAL) 671 }; 672 673 // debug!("FCNTL: fd: {}, cmd: {:?}, arg: {}, res: {:?}", fd, cmd, arg, res); 674 res 675 } 676 677 SYS_FTRUNCATE => { 678 let fd = args[0] as i32; 679 let len = args[1]; 680 let res = Self::ftruncate(fd, len); 681 // debug!("FTRUNCATE: fd: {}, len: {}, res: {:?}", fd, len, res); 682 res 683 } 684 685 #[cfg(target_arch = "x86_64")] 686 SYS_MKNOD => { 687 let path = args[0]; 688 let flags = args[1]; 689 let dev_t = args[2]; 690 let flags: ModeType = ModeType::from_bits_truncate(flags as u32); 691 Self::mknod( 692 path as *const u8, 693 flags, 694 crate::driver::base::device::device_number::DeviceNumber::from(dev_t as u32), 695 ) 696 } 697 698 SYS_CLONE => { 699 let parent_tid = VirtAddr::new(args[2]); 700 let child_tid = VirtAddr::new(args[3]); 701 702 // 地址校验 703 verify_area(parent_tid, core::mem::size_of::<i32>())?; 704 verify_area(child_tid, core::mem::size_of::<i32>())?; 705 706 let mut clone_args = KernelCloneArgs::new(); 707 clone_args.flags = CloneFlags::from_bits_truncate(args[0] as u64); 708 clone_args.stack = args[1]; 709 clone_args.parent_tid = parent_tid; 710 clone_args.child_tid = child_tid; 711 clone_args.tls = args[4]; 712 Self::clone(frame, clone_args) 713 } 714 715 SYS_FUTEX => { 716 let uaddr = VirtAddr::new(args[0]); 717 let operation = FutexFlag::from_bits(args[1] as u32).ok_or(SystemError::ENOSYS)?; 718 let val = args[2] as u32; 719 let utime = args[3]; 720 let uaddr2 = VirtAddr::new(args[4]); 721 let val3 = args[5] as u32; 722 723 let mut timespec = None; 724 if utime != 0 && operation.contains(FutexFlag::FLAGS_HAS_TIMEOUT) { 725 let reader = UserBufferReader::new( 726 utime as *const PosixTimeSpec, 727 core::mem::size_of::<PosixTimeSpec>(), 728 true, 729 )?; 730 731 timespec = Some(*reader.read_one_from_user::<PosixTimeSpec>(0)?); 732 } 733 734 Self::do_futex(uaddr, operation, val, timespec, uaddr2, utime as u32, val3) 735 } 736 737 SYS_SET_ROBUST_LIST => { 738 let head = args[0]; 739 let head_uaddr = VirtAddr::new(head); 740 let len = args[1]; 741 742 let ret = Self::set_robust_list(head_uaddr, len); 743 return ret; 744 } 745 746 SYS_GET_ROBUST_LIST => { 747 let pid = args[0]; 748 let head = args[1]; 749 let head_uaddr = VirtAddr::new(head); 750 let len_ptr = args[2]; 751 let len_ptr_uaddr = VirtAddr::new(len_ptr); 752 753 let ret = Self::get_robust_list(pid, head_uaddr, len_ptr_uaddr); 754 return ret; 755 } 756 757 SYS_READV => Self::readv(args[0] as i32, args[1], args[2]), 758 SYS_WRITEV => Self::writev(args[0] as i32, args[1], args[2]), 759 760 SYS_SET_TID_ADDRESS => Self::set_tid_address(args[0]), 761 762 #[cfg(target_arch = "x86_64")] 763 SYS_LSTAT => { 764 let path = args[0] as *const u8; 765 let kstat = args[1] as *mut PosixKstat; 766 Self::lstat(path, kstat) 767 } 768 769 #[cfg(target_arch = "x86_64")] 770 SYS_STAT => { 771 let path = args[0] as *const u8; 772 let kstat = args[1] as *mut PosixKstat; 773 Self::stat(path, kstat) 774 } 775 776 SYS_STATFS => { 777 let path = args[0] as *const u8; 778 let statfs = args[1] as *mut PosixStatfs; 779 Self::statfs(path, statfs) 780 } 781 782 SYS_FSTATFS => { 783 let fd = args[0] as i32; 784 let statfs = args[1] as *mut PosixStatfs; 785 Self::fstatfs(fd, statfs) 786 } 787 788 SYS_STATX => { 789 let fd = args[0] as i32; 790 let path = args[1] as *const u8; 791 let flags = args[2] as u32; 792 let mask = args[3] as u32; 793 let kstat = args[4] as *mut PosixStatx; 794 795 Self::do_statx(fd, path, flags, mask, kstat) 796 } 797 798 #[cfg(target_arch = "x86_64")] 799 SYS_EPOLL_CREATE => Self::epoll_create(args[0] as i32), 800 SYS_EPOLL_CREATE1 => Self::epoll_create1(args[0]), 801 802 SYS_EPOLL_CTL => Self::epoll_ctl( 803 args[0] as i32, 804 args[1], 805 args[2] as i32, 806 VirtAddr::new(args[3]), 807 ), 808 809 #[cfg(target_arch = "x86_64")] 810 SYS_EPOLL_WAIT => Self::epoll_wait( 811 args[0] as i32, 812 VirtAddr::new(args[1]), 813 args[2] as i32, 814 args[3] as i32, 815 ), 816 817 SYS_EPOLL_PWAIT => { 818 let epfd = args[0] as i32; 819 let epoll_event = VirtAddr::new(args[1]); 820 let max_events = args[2] as i32; 821 let timespec = args[3] as i32; 822 let sigmask_addr = args[4] as *mut SigSet; 823 824 if sigmask_addr.is_null() { 825 return Self::epoll_wait(epfd, epoll_event, max_events, timespec); 826 } 827 let sigmask_reader = 828 UserBufferReader::new(sigmask_addr, core::mem::size_of::<SigSet>(), true)?; 829 let mut sigmask = *sigmask_reader.read_one_from_user::<SigSet>(0)?; 830 831 Self::epoll_pwait( 832 args[0] as i32, 833 VirtAddr::new(args[1]), 834 args[2] as i32, 835 args[3] as i32, 836 &mut sigmask, 837 ) 838 } 839 840 // 目前为了适配musl-libc,以下系统调用先这样写着 841 SYS_GETRANDOM => { 842 let flags = GRandFlags::from_bits(args[2] as u8).ok_or(SystemError::EINVAL)?; 843 Self::get_random(args[0] as *mut u8, args[1], flags) 844 } 845 846 SYS_SOCKETPAIR => { 847 let mut user_buffer_writer = UserBufferWriter::new( 848 args[3] as *mut c_int, 849 core::mem::size_of::<[c_int; 2]>(), 850 frame.is_from_user(), 851 )?; 852 let fds = user_buffer_writer.buffer::<i32>(0)?; 853 Self::socketpair(args[0], args[1], args[2], fds) 854 } 855 856 #[cfg(target_arch = "x86_64")] 857 SYS_POLL => { 858 warn!("SYS_POLL has not yet been implemented"); 859 Ok(0) 860 } 861 862 SYS_SETPGID => { 863 warn!("SYS_SETPGID has not yet been implemented"); 864 Ok(0) 865 } 866 867 SYS_RT_SIGPROCMASK => { 868 warn!("SYS_RT_SIGPROCMASK has not yet been implemented"); 869 Ok(0) 870 } 871 872 SYS_TKILL => { 873 warn!("SYS_TKILL has not yet been implemented"); 874 Ok(0) 875 } 876 877 SYS_SIGALTSTACK => { 878 warn!("SYS_SIGALTSTACK has not yet been implemented"); 879 Ok(0) 880 } 881 882 SYS_EXIT_GROUP => { 883 warn!("SYS_EXIT_GROUP has not yet been implemented"); 884 Ok(0) 885 } 886 887 SYS_MADVISE => { 888 let addr = args[0]; 889 let len = page_align_up(args[1]); 890 if addr & (MMArch::PAGE_SIZE - 1) != 0 { 891 Err(SystemError::EINVAL) 892 } else { 893 Self::madvise(VirtAddr::new(addr), len, args[2]) 894 } 895 } 896 897 SYS_GETTID => Self::gettid().map(|tid| tid.into()), 898 899 SYS_SYSLOG => { 900 let syslog_action_type = args[0]; 901 let buf_vaddr = args[1]; 902 let len = args[2]; 903 let from_user = frame.is_from_user(); 904 let mut user_buffer_writer = 905 UserBufferWriter::new(buf_vaddr as *mut u8, len, from_user)?; 906 907 let user_buf = user_buffer_writer.buffer(0)?; 908 Self::do_syslog(syslog_action_type, user_buf, len) 909 } 910 911 SYS_GETUID => Self::getuid(), 912 SYS_GETGID => Self::getgid(), 913 SYS_SETUID => Self::setuid(args[0]), 914 SYS_SETGID => Self::setgid(args[0]), 915 916 SYS_GETEUID => Self::geteuid(), 917 SYS_GETEGID => Self::getegid(), 918 SYS_SETRESUID => Self::seteuid(args[1]), 919 SYS_SETRESGID => Self::setegid(args[1]), 920 921 SYS_SETFSUID => Self::setfsuid(args[0]), 922 SYS_SETFSGID => Self::setfsgid(args[0]), 923 924 SYS_SETSID => { 925 warn!("SYS_SETSID has not yet been implemented"); 926 Ok(0) 927 } 928 929 SYS_GETRUSAGE => { 930 let who = args[0] as c_int; 931 let rusage = args[1] as *mut RUsage; 932 Self::get_rusage(who, rusage) 933 } 934 #[cfg(target_arch = "x86_64")] 935 SYS_READLINK => { 936 let path = args[0] as *const u8; 937 let buf = args[1] as *mut u8; 938 let bufsiz = args[2]; 939 Self::readlink(path, buf, bufsiz) 940 } 941 942 SYS_READLINKAT => { 943 let dirfd = args[0] as i32; 944 let path = args[1] as *const u8; 945 let buf = args[2] as *mut u8; 946 let bufsiz = args[3]; 947 Self::readlink_at(dirfd, path, buf, bufsiz) 948 } 949 950 SYS_PRLIMIT64 => { 951 let pid = args[0]; 952 let pid = Pid::new(pid); 953 let resource = args[1]; 954 let new_limit = args[2] as *const RLimit64; 955 let old_limit = args[3] as *mut RLimit64; 956 957 Self::prlimit64(pid, resource, new_limit, old_limit) 958 } 959 960 #[cfg(target_arch = "x86_64")] 961 SYS_ACCESS => { 962 let pathname = args[0] as *const u8; 963 let mode = args[1] as u32; 964 Self::access(pathname, mode) 965 } 966 967 SYS_FACCESSAT => { 968 let dirfd = args[0] as i32; 969 let pathname = args[1] as *const u8; 970 let mode = args[2] as u32; 971 Self::faccessat2(dirfd, pathname, mode, 0) 972 } 973 974 SYS_FACCESSAT2 => { 975 let dirfd = args[0] as i32; 976 let pathname = args[1] as *const u8; 977 let mode = args[2] as u32; 978 let flags = args[3] as u32; 979 Self::faccessat2(dirfd, pathname, mode, flags) 980 } 981 982 SYS_CLOCK_GETTIME => { 983 let clockid = args[0] as i32; 984 let timespec = args[1] as *mut PosixTimeSpec; 985 Self::clock_gettime(clockid, timespec) 986 } 987 988 SYS_SYSINFO => { 989 let info = args[0] as *mut SysInfo; 990 Self::sysinfo(info) 991 } 992 993 SYS_UMASK => { 994 let mask = args[0] as u32; 995 Self::umask(mask) 996 } 997 998 SYS_FCHOWN => { 999 let dirfd = args[0] as i32; 1000 let uid = args[1]; 1001 let gid = args[2]; 1002 Self::fchown(dirfd, uid, gid) 1003 } 1004 #[cfg(target_arch = "x86_64")] 1005 SYS_CHOWN => { 1006 let pathname = args[0] as *const u8; 1007 let uid = args[1]; 1008 let gid = args[2]; 1009 Self::chown(pathname, uid, gid) 1010 } 1011 #[cfg(target_arch = "x86_64")] 1012 SYS_LCHOWN => { 1013 let pathname = args[0] as *const u8; 1014 let uid = args[1]; 1015 let gid = args[2]; 1016 Self::lchown(pathname, uid, gid) 1017 } 1018 SYS_FCHOWNAT => { 1019 let dirfd = args[0] as i32; 1020 let pathname = args[1] as *const u8; 1021 let uid = args[2]; 1022 let gid = args[3]; 1023 let flag = args[4] as i32; 1024 Self::fchownat(dirfd, pathname, uid, gid, flag) 1025 } 1026 1027 SYS_FSYNC => { 1028 warn!("SYS_FSYNC has not yet been implemented"); 1029 Ok(0) 1030 } 1031 1032 SYS_RSEQ => { 1033 warn!("SYS_RSEQ has not yet been implemented"); 1034 Ok(0) 1035 } 1036 1037 #[cfg(target_arch = "x86_64")] 1038 SYS_CHMOD => { 1039 let pathname = args[0] as *const u8; 1040 let mode = args[1] as u32; 1041 Self::chmod(pathname, mode) 1042 } 1043 SYS_FCHMOD => { 1044 let fd = args[0] as i32; 1045 let mode = args[1] as u32; 1046 Self::fchmod(fd, mode) 1047 } 1048 SYS_FCHMODAT => { 1049 let dirfd = args[0] as i32; 1050 let pathname = args[1] as *const u8; 1051 let mode = args[2] as u32; 1052 Self::fchmodat(dirfd, pathname, mode) 1053 } 1054 1055 SYS_SCHED_YIELD => Self::do_sched_yield(), 1056 1057 SYS_SCHED_GETAFFINITY => { 1058 let pid = args[0] as i32; 1059 let size = args[1]; 1060 let set_vaddr = args[2]; 1061 1062 let mut user_buffer_writer = 1063 UserBufferWriter::new(set_vaddr as *mut u8, size, frame.is_from_user())?; 1064 let set: &mut [u8] = user_buffer_writer.buffer(0)?; 1065 1066 Self::getaffinity(pid, set) 1067 } 1068 1069 #[cfg(target_arch = "x86_64")] 1070 SYS_GETRLIMIT => { 1071 let resource = args[0]; 1072 let rlimit = args[1] as *mut RLimit64; 1073 1074 Self::prlimit64( 1075 ProcessManager::current_pcb().pid(), 1076 resource, 1077 core::ptr::null::<RLimit64>(), 1078 rlimit, 1079 ) 1080 } 1081 1082 SYS_FADVISE64 => { 1083 // todo: 这个系统调用还没有实现 1084 1085 Err(SystemError::ENOSYS) 1086 } 1087 1088 SYS_MOUNT => { 1089 let source = args[0] as *const u8; 1090 let target = args[1] as *const u8; 1091 let filesystemtype = args[2] as *const u8; 1092 let mountflags = args[3]; 1093 let data = args[4] as *const u8; // 额外的mount参数,实现自己的mountdata来获取 1094 return Self::mount(source, target, filesystemtype, mountflags, data); 1095 } 1096 1097 SYS_UMOUNT2 => { 1098 let target = args[0] as *const u8; 1099 let flags = args[1] as i32; 1100 Self::umount2(target, flags)?; 1101 return Ok(0); 1102 } 1103 1104 SYS_NEWFSTATAT => { 1105 // todo: 这个系统调用还没有实现 1106 1107 Err(SystemError::ENOSYS) 1108 } 1109 1110 // SYS_SCHED_YIELD => Self::sched_yield(), 1111 SYS_UNAME => { 1112 let name = args[0] as *mut PosixOldUtsName; 1113 Self::uname(name) 1114 } 1115 SYS_PRCTL => { 1116 // todo: 这个系统调用还没有实现 1117 1118 Err(SystemError::EINVAL) 1119 } 1120 1121 #[cfg(target_arch = "x86_64")] 1122 SYS_ALARM => { 1123 let second = args[0] as u32; 1124 Self::alarm(second) 1125 } 1126 1127 SYS_SHMGET => { 1128 let key = ShmKey::new(args[0]); 1129 let size = args[1]; 1130 let shmflg = ShmFlags::from_bits_truncate(args[2] as u32); 1131 1132 Self::shmget(key, size, shmflg) 1133 } 1134 SYS_SHMAT => { 1135 let id = ShmId::new(args[0]); 1136 let vaddr = VirtAddr::new(args[1]); 1137 let shmflg = ShmFlags::from_bits_truncate(args[2] as u32); 1138 1139 Self::shmat(id, vaddr, shmflg) 1140 } 1141 SYS_SHMDT => { 1142 let vaddr = VirtAddr::new(args[0]); 1143 Self::shmdt(vaddr) 1144 } 1145 SYS_SHMCTL => { 1146 let id = ShmId::new(args[0]); 1147 let cmd = ShmCtlCmd::from(args[1]); 1148 let user_buf = args[2] as *const u8; 1149 let from_user = frame.is_from_user(); 1150 1151 Self::shmctl(id, cmd, user_buf, from_user) 1152 } 1153 SYS_MSYNC => { 1154 let start = page_align_up(args[0]); 1155 let len = page_align_up(args[1]); 1156 let flags = args[2]; 1157 Self::msync(VirtAddr::new(start), len, flags) 1158 } 1159 SYS_UTIMENSAT => Self::sys_utimensat( 1160 args[0] as i32, 1161 args[1] as *const u8, 1162 args[2] as *const PosixTimeSpec, 1163 args[3] as u32, 1164 ), 1165 #[cfg(target_arch = "x86_64")] 1166 SYS_FUTIMESAT => { 1167 let flags = UtimensFlags::empty(); 1168 Self::sys_utimensat( 1169 args[0] as i32, 1170 args[1] as *const u8, 1171 args[2] as *const PosixTimeSpec, 1172 flags.bits(), 1173 ) 1174 } 1175 #[cfg(target_arch = "x86_64")] 1176 SYS_UTIMES => Self::sys_utimes(args[0] as *const u8, args[1] as *const PosixTimeval), 1177 #[cfg(target_arch = "x86_64")] 1178 SYS_EVENTFD => { 1179 let initval = args[0] as u32; 1180 Self::sys_eventfd(initval, 0) 1181 } 1182 SYS_EVENTFD2 => { 1183 let initval = args[0] as u32; 1184 let flags = args[1] as u32; 1185 Self::sys_eventfd(initval, flags) 1186 } 1187 SYS_UNSHARE => Self::sys_unshare(args[0] as u64), 1188 SYS_BPF => { 1189 let cmd = args[0] as u32; 1190 let attr = args[1] as *mut u8; 1191 let size = args[2] as u32; 1192 Self::sys_bpf(cmd, attr, size) 1193 } 1194 SYS_PERF_EVENT_OPEN => { 1195 let attr = args[0] as *const u8; 1196 let pid = args[1] as i32; 1197 let cpu = args[2] as i32; 1198 let group_fd = args[3] as i32; 1199 let flags = args[4] as u32; 1200 Self::sys_perf_event_open(attr, pid, cpu, group_fd, flags) 1201 } 1202 _ => panic!("Unsupported syscall ID: {}", syscall_num), 1203 }; 1204 1205 if ProcessManager::current_pcb() 1206 .flags() 1207 .contains(ProcessFlags::NEED_SCHEDULE) 1208 { 1209 schedule(SchedMode::SM_PREEMPT); 1210 } 1211 1212 return r; 1213 } 1214 put_string( s: *const u8, front_color: u32, back_color: u32, ) -> Result<usize, SystemError>1215 pub fn put_string( 1216 s: *const u8, 1217 front_color: u32, 1218 back_color: u32, 1219 ) -> Result<usize, SystemError> { 1220 // todo: 删除这个系统调用 1221 let s = check_and_clone_cstr(s, Some(4096))? 1222 .into_string() 1223 .map_err(|_| SystemError::EINVAL)?; 1224 let fr = (front_color & 0x00ff0000) >> 16; 1225 let fg = (front_color & 0x0000ff00) >> 8; 1226 let fb = front_color & 0x000000ff; 1227 let br = (back_color & 0x00ff0000) >> 16; 1228 let bg = (back_color & 0x0000ff00) >> 8; 1229 let bb = back_color & 0x000000ff; 1230 print!("\x1B[38;2;{fr};{fg};{fb};48;2;{br};{bg};{bb}m{s}\x1B[0m"); 1231 return Ok(s.len()); 1232 } 1233 reboot() -> Result<usize, SystemError>1234 pub fn reboot() -> Result<usize, SystemError> { 1235 unsafe { cpu_reset() }; 1236 } 1237 } 1238