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