xref: /DragonOS/kernel/src/syscall/mod.rs (revision 1d37ca6d172e01a98fa6785d2b3e07fb8202a4a9)
1ab5c8ca4Slogin use core::{
282df0a13Shmt     ffi::{c_int, c_void},
3*1d37ca6dSDonkey Kane     ptr::null,
4ab5c8ca4Slogin     sync::atomic::{AtomicBool, Ordering},
5ab5c8ca4Slogin };
6ab5c8ca4Slogin 
7971462beSGnoCiYeH use crate::{
840609970SGnoCiYeH     arch::{ipc::signal::SigSet, syscall::nr::*},
9c566df45SLoGin     driver::base::device::device_number::DeviceNumber,
10971462beSGnoCiYeH     libs::{futex::constant::FutexFlag, rand::GRandFlags},
114cfa009bSJomo     mm::syscall::MremapFlags,
124ad52e57S裕依2439     net::syscall::MsgHdr,
130d9b7d92SLoGin     process::{
140d9b7d92SLoGin         fork::KernelCloneArgs,
150d9b7d92SLoGin         resource::{RLimit64, RUsage},
1652da9a59SGnoCiYeH         ProcessManager,
170d9b7d92SLoGin     },
18c3dc6f2fSLoGin     syscall::user_access::check_and_clone_cstr,
19971462beSGnoCiYeH };
2040314b30SXiaoye Zheng 
2191e9d4abSLoGin use num_traits::FromPrimitive;
2291e9d4abSLoGin use system_error::SystemError;
23676b8ef6SMork 
24ab5c8ca4Slogin use crate::{
251496ba7bSLoGin     arch::{cpu::cpu_reset, interrupt::TrapFrame, MMArch},
26ab5c8ca4Slogin     filesystem::vfs::{
279e481b3bSTTaq         fcntl::{AtFlags, FcntlCommand},
28ab5c8ca4Slogin         file::FileMode,
2982df0a13Shmt         syscall::{ModeType, PosixKstat},
30ab5c8ca4Slogin         MAX_PATHLEN,
31ab5c8ca4Slogin     },
3282df0a13Shmt     include::bindings::bindings::PAGE_4K_SIZE,
33aa0367d6SLoGin     kinfo,
3440fe15e0SLoGin     libs::align::page_align_up,
35be63f3b2Shoumkh     mm::{verify_area, MemoryManagementArch, VirtAddr},
36ab5c8ca4Slogin     net::syscall::SockAddr,
378c6f2184Ssspphh     process::{fork::CloneFlags, syscall::PosixOldUtsName, Pid},
3836fd0130Shoumkh     time::{
3940fe15e0SLoGin         syscall::{PosixTimeZone, PosixTimeval},
4036fd0130Shoumkh         TimeSpec,
4136fd0130Shoumkh     },
42ab5c8ca4Slogin };
43ab5c8ca4Slogin 
44bf4a4899SLoGin use self::{
45bf4a4899SLoGin     misc::SysInfo,
46bf4a4899SLoGin     user_access::{UserBufferReader, UserBufferWriter},
47bf4a4899SLoGin };
484537ffb7SChiichen 
49bf4a4899SLoGin pub mod misc;
5040fe15e0SLoGin pub mod user_access;
5140fe15e0SLoGin 
52ad1d649eSGnoCiYeH // 与linux不一致的调用,在linux基础上累加
53ad1d649eSGnoCiYeH pub const SYS_PUT_STRING: usize = 100000;
54ad1d649eSGnoCiYeH pub const SYS_SBRK: usize = 100001;
55ab5c8ca4Slogin /// todo: 该系统调用与Linux不一致,将来需要删除该系统调用!!! 删的时候记得改C版本的libc
56ad1d649eSGnoCiYeH pub const SYS_CLOCK: usize = 100002;
57ad1d649eSGnoCiYeH pub const SYS_SCHED: usize = 100003;
5840314b30SXiaoye Zheng 
59ab5c8ca4Slogin #[derive(Debug)]
60ab5c8ca4Slogin pub struct Syscall;
61ab5c8ca4Slogin 
62ab5c8ca4Slogin impl Syscall {
63ab5c8ca4Slogin     /// 初始化系统调用
645b59005fSLoGin     #[inline(never)]
65ab5c8ca4Slogin     pub fn init() -> Result<(), SystemError> {
66ab5c8ca4Slogin         static INIT_FLAG: AtomicBool = AtomicBool::new(false);
67ab5c8ca4Slogin         let prev = INIT_FLAG.swap(true, Ordering::SeqCst);
68ab5c8ca4Slogin         if prev {
69ab5c8ca4Slogin             panic!("Cannot initialize syscall more than once!");
70ab5c8ca4Slogin         }
715b59005fSLoGin         kinfo!("Initializing syscall...");
725b59005fSLoGin         let r = crate::arch::syscall::arch_syscall_init();
735b59005fSLoGin         kinfo!("Syscall init successfully!");
745b59005fSLoGin 
755b59005fSLoGin         return r;
76ab5c8ca4Slogin     }
77ab5c8ca4Slogin     /// @brief 系统调用分发器,用于分发系统调用。
78ab5c8ca4Slogin     ///
79ab5c8ca4Slogin     /// 这个函数内,需要根据系统调用号,调用对应的系统调用处理函数。
80ab5c8ca4Slogin     /// 并且,对于用户态传入的指针参数,需要在本函数内进行越界检查,防止访问到内核空间。
814fda81ceSLoGin     #[inline(never)]
829e9ffedfSPlucky923     pub fn handle(
839e9ffedfSPlucky923         syscall_num: usize,
849e9ffedfSPlucky923         args: &[usize],
859e9ffedfSPlucky923         frame: &mut TrapFrame,
869e9ffedfSPlucky923     ) -> Result<usize, SystemError> {
87ab5c8ca4Slogin         let r = match syscall_num {
88ab5c8ca4Slogin             SYS_PUT_STRING => {
89ab5c8ca4Slogin                 Self::put_string(args[0] as *const u8, args[1] as u32, args[2] as u32)
90ab5c8ca4Slogin             }
914fda81ceSLoGin             #[cfg(target_arch = "x86_64")]
92ab5c8ca4Slogin             SYS_OPEN => {
9382df0a13Shmt                 let path = args[0] as *const u8;
9482df0a13Shmt                 let flags = args[1] as u32;
9582df0a13Shmt                 let mode = args[2] as u32;
9634e6d6c8Syuyi2439 
9782df0a13Shmt                 Self::open(path, flags, mode, true)
980fb515b0SLoGin             }
990fb515b0SLoGin 
1009e481b3bSTTaq             SYS_RENAME => {
1019e481b3bSTTaq                 let oldname: *const u8 = args[0] as *const u8;
1029e481b3bSTTaq                 let newname: *const u8 = args[1] as *const u8;
1039e481b3bSTTaq                 Self::do_renameat2(
1049e481b3bSTTaq                     AtFlags::AT_FDCWD.bits(),
1059e481b3bSTTaq                     oldname,
1069e481b3bSTTaq                     AtFlags::AT_FDCWD.bits(),
1079e481b3bSTTaq                     newname,
1089e481b3bSTTaq                     0,
1099e481b3bSTTaq                 )
1109e481b3bSTTaq             }
1119e481b3bSTTaq 
1129e481b3bSTTaq             SYS_RENAMEAT => {
1139e481b3bSTTaq                 let oldfd = args[0] as i32;
1149e481b3bSTTaq                 let oldname: *const u8 = args[1] as *const u8;
1159e481b3bSTTaq                 let newfd = args[2] as i32;
1169e481b3bSTTaq                 let newname: *const u8 = args[3] as *const u8;
1179e481b3bSTTaq                 Self::do_renameat2(oldfd, oldname, newfd, newname, 0)
1189e481b3bSTTaq             }
1199e481b3bSTTaq 
1209e481b3bSTTaq             SYS_RENAMEAT2 => {
1219e481b3bSTTaq                 let oldfd = args[0] as i32;
1229e481b3bSTTaq                 let oldname: *const u8 = args[1] as *const u8;
1239e481b3bSTTaq                 let newfd = args[2] as i32;
1249e481b3bSTTaq                 let newname: *const u8 = args[3] as *const u8;
1259e481b3bSTTaq                 let flags = args[4] as u32;
1269e481b3bSTTaq                 Self::do_renameat2(oldfd, oldname, newfd, newname, flags)
1279e481b3bSTTaq             }
1289e481b3bSTTaq 
1290fb515b0SLoGin             SYS_OPENAT => {
1300fb515b0SLoGin                 let dirfd = args[0] as i32;
13182df0a13Shmt                 let path = args[1] as *const u8;
13282df0a13Shmt                 let flags = args[2] as u32;
13382df0a13Shmt                 let mode = args[3] as u32;
1340fb515b0SLoGin 
13582df0a13Shmt                 Self::openat(dirfd, path, flags, mode, true)
136ab5c8ca4Slogin             }
137ab5c8ca4Slogin             SYS_CLOSE => {
138ab5c8ca4Slogin                 let fd = args[0];
13927b967a3S裕依                 Self::close(fd)
140ab5c8ca4Slogin             }
141ab5c8ca4Slogin             SYS_READ => {
142ab5c8ca4Slogin                 let fd = args[0] as i32;
143ab5c8ca4Slogin                 let buf_vaddr = args[1];
144ab5c8ca4Slogin                 let len = args[2];
1459e9ffedfSPlucky923                 let from_user = frame.from_user();
1469e9ffedfSPlucky923                 let mut user_buffer_writer =
1479e9ffedfSPlucky923                     UserBufferWriter::new(buf_vaddr as *mut u8, len, from_user)?;
1486d81180bSLoGin 
1499e9ffedfSPlucky923                 let user_buf = user_buffer_writer.buffer(0)?;
15027b967a3S裕依                 Self::read(fd, user_buf)
151ab5c8ca4Slogin             }
152ab5c8ca4Slogin             SYS_WRITE => {
153ab5c8ca4Slogin                 let fd = args[0] as i32;
154ab5c8ca4Slogin                 let buf_vaddr = args[1];
155ab5c8ca4Slogin                 let len = args[2];
1569e9ffedfSPlucky923                 let from_user = frame.from_user();
1579e9ffedfSPlucky923                 let user_buffer_reader =
1589e9ffedfSPlucky923                     UserBufferReader::new(buf_vaddr as *const u8, len, from_user)?;
1596d81180bSLoGin 
1609e9ffedfSPlucky923                 let user_buf = user_buffer_reader.read_from_user(0)?;
16127b967a3S裕依                 Self::write(fd, user_buf)
162ab5c8ca4Slogin             }
163ab5c8ca4Slogin 
164ab5c8ca4Slogin             SYS_LSEEK => {
165ab5c8ca4Slogin                 let fd = args[0] as i32;
166ab5c8ca4Slogin                 let offset = args[1] as i64;
167ab5c8ca4Slogin                 let whence = args[2] as u32;
168ab5c8ca4Slogin 
16982df0a13Shmt                 Self::lseek(fd, offset, whence)
170ab5c8ca4Slogin             }
17127b967a3S裕依 
17227b967a3S裕依             SYS_PREAD64 => {
17327b967a3S裕依                 let fd = args[0] as i32;
17427b967a3S裕依                 let buf_vaddr = args[1];
17527b967a3S裕依                 let len = args[2];
17627b967a3S裕依                 let offset = args[3];
17727b967a3S裕依 
17827b967a3S裕依                 let mut user_buffer_writer =
17927b967a3S裕依                     UserBufferWriter::new(buf_vaddr as *mut u8, len, frame.from_user())?;
18027b967a3S裕依                 let buf = user_buffer_writer.buffer(0)?;
18127b967a3S裕依                 Self::pread(fd, buf, len, offset)
18227b967a3S裕依             }
18327b967a3S裕依 
18427b967a3S裕依             SYS_PWRITE64 => {
18527b967a3S裕依                 let fd = args[0] as i32;
18627b967a3S裕依                 let buf_vaddr = args[1];
18727b967a3S裕依                 let len = args[2];
18827b967a3S裕依                 let offset = args[3];
18927b967a3S裕依 
19027b967a3S裕依                 let user_buffer_reader =
19127b967a3S裕依                     UserBufferReader::new(buf_vaddr as *const u8, len, frame.from_user())?;
19227b967a3S裕依 
19327b967a3S裕依                 let buf = user_buffer_reader.read_from_user(0)?;
19427b967a3S裕依                 Self::pwrite(fd, buf, len, offset)
19527b967a3S裕依             }
19627b967a3S裕依 
19740314b30SXiaoye Zheng             SYS_IOCTL => {
19840314b30SXiaoye Zheng                 let fd = args[0];
19940314b30SXiaoye Zheng                 let cmd = args[1];
20040314b30SXiaoye Zheng                 let data = args[2];
20140314b30SXiaoye Zheng                 Self::ioctl(fd, cmd as u32, data)
20240314b30SXiaoye Zheng             }
203ab5c8ca4Slogin 
2044fda81ceSLoGin             #[cfg(target_arch = "x86_64")]
2051496ba7bSLoGin             SYS_FORK => Self::fork(frame),
2064fda81ceSLoGin             #[cfg(target_arch = "x86_64")]
2071496ba7bSLoGin             SYS_VFORK => Self::vfork(frame),
2081496ba7bSLoGin 
209ab5c8ca4Slogin             SYS_BRK => {
21040fe15e0SLoGin                 let new_brk = VirtAddr::new(args[0]);
21140fe15e0SLoGin                 Self::brk(new_brk).map(|vaddr| vaddr.data())
212ab5c8ca4Slogin             }
213ab5c8ca4Slogin 
214ab5c8ca4Slogin             SYS_SBRK => {
215ab5c8ca4Slogin                 let increment = args[0] as isize;
2161496ba7bSLoGin                 Self::sbrk(increment).map(|vaddr: VirtAddr| vaddr.data())
217ab5c8ca4Slogin             }
218ab5c8ca4Slogin 
219ab5c8ca4Slogin             SYS_REBOOT => Self::reboot(),
220ab5c8ca4Slogin 
221ab5c8ca4Slogin             SYS_CHDIR => {
22282df0a13Shmt                 let r = args[0] as *const u8;
2239e9ffedfSPlucky923                 Self::chdir(r)
224ab5c8ca4Slogin             }
225ab5c8ca4Slogin 
2264fda81ceSLoGin             #[allow(unreachable_patterns)]
2274fda81ceSLoGin             SYS_GETDENTS64 | SYS_GETDENTS => {
228ab5c8ca4Slogin                 let fd = args[0] as i32;
22934e6d6c8Syuyi2439 
230ab5c8ca4Slogin                 let buf_vaddr = args[1];
231ab5c8ca4Slogin                 let len = args[2];
232f5df0e79SLoGin                 let virt_addr: VirtAddr = VirtAddr::new(buf_vaddr);
233ab5c8ca4Slogin                 // 判断缓冲区是否来自用户态,进行权限校验
2341496ba7bSLoGin                 let res = if frame.from_user() && verify_area(virt_addr, len as usize).is_err() {
235ab5c8ca4Slogin                     // 来自用户态,而buffer在内核态,这样的操作不被允许
236ab5c8ca4Slogin                     Err(SystemError::EPERM)
237ab5c8ca4Slogin                 } else if buf_vaddr == 0 {
238ab5c8ca4Slogin                     Err(SystemError::EFAULT)
239ab5c8ca4Slogin                 } else {
240ab5c8ca4Slogin                     let buf: &mut [u8] = unsafe {
241ab5c8ca4Slogin                         core::slice::from_raw_parts_mut::<'static, u8>(buf_vaddr as *mut u8, len)
242ab5c8ca4Slogin                     };
243ab5c8ca4Slogin                     Self::getdents(fd, buf)
244ab5c8ca4Slogin                 };
245ab5c8ca4Slogin 
246ab5c8ca4Slogin                 res
247ab5c8ca4Slogin             }
248ab5c8ca4Slogin 
249ab5c8ca4Slogin             SYS_EXECVE => {
250ab5c8ca4Slogin                 let path_ptr = args[0];
251ab5c8ca4Slogin                 let argv_ptr = args[1];
252ab5c8ca4Slogin                 let env_ptr = args[2];
253be63f3b2Shoumkh                 let virt_path_ptr = VirtAddr::new(path_ptr);
254be63f3b2Shoumkh                 let virt_argv_ptr = VirtAddr::new(argv_ptr);
255be63f3b2Shoumkh                 let virt_env_ptr = VirtAddr::new(env_ptr);
256ab5c8ca4Slogin                 // 权限校验
2571496ba7bSLoGin                 if frame.from_user()
2581496ba7bSLoGin                     && (verify_area(virt_path_ptr, MAX_PATHLEN as usize).is_err()
259be63f3b2Shoumkh                         || verify_area(virt_argv_ptr, PAGE_4K_SIZE as usize).is_err())
260be63f3b2Shoumkh                     || verify_area(virt_env_ptr, PAGE_4K_SIZE as usize).is_err()
261ab5c8ca4Slogin                 {
262ab5c8ca4Slogin                     Err(SystemError::EFAULT)
263ab5c8ca4Slogin                 } else {
264ab5c8ca4Slogin                     Self::execve(
2651496ba7bSLoGin                         path_ptr as *const u8,
2661496ba7bSLoGin                         argv_ptr as *const *const u8,
2671496ba7bSLoGin                         env_ptr as *const *const u8,
2681496ba7bSLoGin                         frame,
269ab5c8ca4Slogin                     )
2701496ba7bSLoGin                     .map(|_| 0)
271ab5c8ca4Slogin                 }
272ab5c8ca4Slogin             }
273ab5c8ca4Slogin             SYS_WAIT4 => {
274bf4a4899SLoGin                 let pid = args[0] as i32;
2751496ba7bSLoGin                 let wstatus = args[1] as *mut i32;
276ab5c8ca4Slogin                 let options = args[2] as c_int;
277ab5c8ca4Slogin                 let rusage = args[3] as *mut c_void;
278ab5c8ca4Slogin                 // 权限校验
279ab5c8ca4Slogin                 // todo: 引入rusage之后,更正以下权限校验代码中,rusage的大小
280bf4a4899SLoGin                 Self::wait4(pid.into(), wstatus, options, rusage)
281ab5c8ca4Slogin             }
282ab5c8ca4Slogin 
283ab5c8ca4Slogin             SYS_EXIT => {
284ab5c8ca4Slogin                 let exit_code = args[0];
285ab5c8ca4Slogin                 Self::exit(exit_code)
286ab5c8ca4Slogin             }
2874fda81ceSLoGin             #[cfg(target_arch = "x86_64")]
288ab5c8ca4Slogin             SYS_MKDIR => {
28982df0a13Shmt                 let path = args[0] as *const u8;
290ab5c8ca4Slogin                 let mode = args[1];
291ab5c8ca4Slogin 
29282df0a13Shmt                 Self::mkdir(path, mode)
293ab5c8ca4Slogin             }
294ab5c8ca4Slogin 
295ab5c8ca4Slogin             SYS_NANOSLEEP => {
296ab5c8ca4Slogin                 let req = args[0] as *const TimeSpec;
297ab5c8ca4Slogin                 let rem = args[1] as *mut TimeSpec;
298be63f3b2Shoumkh                 let virt_req = VirtAddr::new(req as usize);
299be63f3b2Shoumkh                 let virt_rem = VirtAddr::new(rem as usize);
3001496ba7bSLoGin                 if frame.from_user()
301be63f3b2Shoumkh                     && (verify_area(virt_req, core::mem::size_of::<TimeSpec>() as usize).is_err()
302be63f3b2Shoumkh                         || verify_area(virt_rem, core::mem::size_of::<TimeSpec>() as usize)
303be63f3b2Shoumkh                             .is_err())
304ab5c8ca4Slogin                 {
305ab5c8ca4Slogin                     Err(SystemError::EFAULT)
306ab5c8ca4Slogin                 } else {
307ab5c8ca4Slogin                     Self::nanosleep(req, rem)
308ab5c8ca4Slogin                 }
309ab5c8ca4Slogin             }
310ab5c8ca4Slogin 
311ab5c8ca4Slogin             SYS_CLOCK => Self::clock(),
3124fda81ceSLoGin             #[cfg(target_arch = "x86_64")]
313ab5c8ca4Slogin             SYS_PIPE => {
31422c9db31Shanjiezhou                 let pipefd: *mut i32 = args[0] as *mut c_int;
3150fb515b0SLoGin                 if pipefd.is_null() {
3160fb515b0SLoGin                     Err(SystemError::EFAULT)
3170fb515b0SLoGin                 } else {
3180fb515b0SLoGin                     Self::pipe2(pipefd, FileMode::empty())
3190fb515b0SLoGin                 }
3200fb515b0SLoGin             }
3214ad52e57S裕依2439 
3220fb515b0SLoGin             SYS_PIPE2 => {
3230fb515b0SLoGin                 let pipefd: *mut i32 = args[0] as *mut c_int;
32422c9db31Shanjiezhou                 let arg1 = args[1];
32522c9db31Shanjiezhou                 if pipefd.is_null() {
32622c9db31Shanjiezhou                     Err(SystemError::EFAULT)
32722c9db31Shanjiezhou                 } else {
32822c9db31Shanjiezhou                     let flags = FileMode::from_bits_truncate(arg1 as u32);
32922c9db31Shanjiezhou                     Self::pipe2(pipefd, flags)
330ab5c8ca4Slogin                 }
331ab5c8ca4Slogin             }
332ab5c8ca4Slogin 
3334fda81ceSLoGin             SYS_UNLINKAT => {
334ab5c8ca4Slogin                 let dirfd = args[0] as i32;
33582df0a13Shmt                 let path = args[1] as *const u8;
336ab5c8ca4Slogin                 let flags = args[2] as u32;
33782df0a13Shmt                 Self::unlinkat(dirfd, path, flags)
338ab5c8ca4Slogin             }
339bf4a4899SLoGin 
3404fda81ceSLoGin             #[cfg(target_arch = "x86_64")]
3415eeefb8cSChenzx             SYS_RMDIR => {
34282df0a13Shmt                 let path = args[0] as *const u8;
34382df0a13Shmt                 Self::rmdir(path)
3445eeefb8cSChenzx             }
3455eeefb8cSChenzx 
3465eeefb8cSChenzx             #[cfg(target_arch = "x86_64")]
347bf4a4899SLoGin             SYS_UNLINK => {
34882df0a13Shmt                 let path = args[0] as *const u8;
34982df0a13Shmt                 Self::unlink(path)
350bf4a4899SLoGin             }
351ab5c8ca4Slogin             SYS_KILL => {
3521496ba7bSLoGin                 let pid = Pid::new(args[0]);
353ab5c8ca4Slogin                 let sig = args[1] as c_int;
3543c82aa56SChiichen                 // kdebug!("KILL SYSCALL RECEIVED");
355ab5c8ca4Slogin                 Self::kill(pid, sig)
356ab5c8ca4Slogin             }
357ab5c8ca4Slogin 
3584fda81ceSLoGin             SYS_RT_SIGACTION => {
359ab5c8ca4Slogin                 let sig = args[0] as c_int;
360ab5c8ca4Slogin                 let act = args[1];
361ab5c8ca4Slogin                 let old_act = args[2];
3621496ba7bSLoGin                 Self::sigaction(sig, act, old_act, frame.from_user())
363ab5c8ca4Slogin             }
364ab5c8ca4Slogin 
3651496ba7bSLoGin             SYS_GETPID => Self::getpid().map(|pid| pid.into()),
366ab5c8ca4Slogin 
3671496ba7bSLoGin             SYS_SCHED => Self::sched(frame.from_user()),
368ab5c8ca4Slogin             SYS_DUP => {
369ab5c8ca4Slogin                 let oldfd: i32 = args[0] as c_int;
370ab5c8ca4Slogin                 Self::dup(oldfd)
371ab5c8ca4Slogin             }
3724fda81ceSLoGin 
3734fda81ceSLoGin             #[cfg(target_arch = "x86_64")]
374ab5c8ca4Slogin             SYS_DUP2 => {
375ab5c8ca4Slogin                 let oldfd: i32 = args[0] as c_int;
376ab5c8ca4Slogin                 let newfd: i32 = args[1] as c_int;
377ab5c8ca4Slogin                 Self::dup2(oldfd, newfd)
378ab5c8ca4Slogin             }
379ab5c8ca4Slogin 
380ab5c8ca4Slogin             SYS_SOCKET => Self::socket(args[0], args[1], args[2]),
381ab5c8ca4Slogin             SYS_SETSOCKOPT => {
382ab5c8ca4Slogin                 let optval = args[3] as *const u8;
383ab5c8ca4Slogin                 let optlen = args[4] as usize;
384be63f3b2Shoumkh                 let virt_optval = VirtAddr::new(optval as usize);
385ab5c8ca4Slogin                 // 验证optval的地址是否合法
386be63f3b2Shoumkh                 if verify_area(virt_optval, optlen as usize).is_err() {
387ab5c8ca4Slogin                     // 地址空间超出了用户空间的范围,不合法
388ab5c8ca4Slogin                     Err(SystemError::EFAULT)
389ab5c8ca4Slogin                 } else {
390ab5c8ca4Slogin                     let data: &[u8] = unsafe { core::slice::from_raw_parts(optval, optlen) };
391ab5c8ca4Slogin                     Self::setsockopt(args[0], args[1], args[2], data)
392ab5c8ca4Slogin                 }
393ab5c8ca4Slogin             }
394ab5c8ca4Slogin             SYS_GETSOCKOPT => {
395ab5c8ca4Slogin                 let optval = args[3] as *mut u8;
396ab5c8ca4Slogin                 let optlen = args[4] as *mut usize;
397be63f3b2Shoumkh                 let virt_optval = VirtAddr::new(optval as usize);
398be63f3b2Shoumkh                 let virt_optlen = VirtAddr::new(optlen as usize);
399ab5c8ca4Slogin                 let security_check = || {
400ab5c8ca4Slogin                     // 验证optval的地址是否合法
401be63f3b2Shoumkh                     if verify_area(virt_optval, PAGE_4K_SIZE as usize).is_err() {
402ab5c8ca4Slogin                         // 地址空间超出了用户空间的范围,不合法
403ab5c8ca4Slogin                         return Err(SystemError::EFAULT);
404ab5c8ca4Slogin                     }
405ab5c8ca4Slogin 
406ab5c8ca4Slogin                     // 验证optlen的地址是否合法
407be63f3b2Shoumkh                     if verify_area(virt_optlen, core::mem::size_of::<u32>() as usize).is_err() {
408ab5c8ca4Slogin                         // 地址空间超出了用户空间的范围,不合法
409ab5c8ca4Slogin                         return Err(SystemError::EFAULT);
410ab5c8ca4Slogin                     }
411ab5c8ca4Slogin                     return Ok(());
412ab5c8ca4Slogin                 };
413ab5c8ca4Slogin                 let r = security_check();
414ab5c8ca4Slogin                 if r.is_err() {
415ab5c8ca4Slogin                     Err(r.unwrap_err())
416ab5c8ca4Slogin                 } else {
417ab5c8ca4Slogin                     Self::getsockopt(args[0], args[1], args[2], optval, optlen as *mut u32)
418ab5c8ca4Slogin                 }
419ab5c8ca4Slogin             }
420ab5c8ca4Slogin 
421ab5c8ca4Slogin             SYS_CONNECT => {
422ab5c8ca4Slogin                 let addr = args[1] as *const SockAddr;
423ab5c8ca4Slogin                 let addrlen = args[2] as usize;
424be63f3b2Shoumkh                 let virt_addr = VirtAddr::new(addr as usize);
425ab5c8ca4Slogin                 // 验证addr的地址是否合法
426be63f3b2Shoumkh                 if verify_area(virt_addr, addrlen as usize).is_err() {
427ab5c8ca4Slogin                     // 地址空间超出了用户空间的范围,不合法
428ab5c8ca4Slogin                     Err(SystemError::EFAULT)
429ab5c8ca4Slogin                 } else {
430ab5c8ca4Slogin                     Self::connect(args[0], addr, addrlen)
431ab5c8ca4Slogin                 }
432ab5c8ca4Slogin             }
433ab5c8ca4Slogin             SYS_BIND => {
434ab5c8ca4Slogin                 let addr = args[1] as *const SockAddr;
435ab5c8ca4Slogin                 let addrlen = args[2] as usize;
436be63f3b2Shoumkh                 let virt_addr = VirtAddr::new(addr as usize);
437ab5c8ca4Slogin                 // 验证addr的地址是否合法
438be63f3b2Shoumkh                 if verify_area(virt_addr, addrlen as usize).is_err() {
439ab5c8ca4Slogin                     // 地址空间超出了用户空间的范围,不合法
440ab5c8ca4Slogin                     Err(SystemError::EFAULT)
441ab5c8ca4Slogin                 } else {
442ab5c8ca4Slogin                     Self::bind(args[0], addr, addrlen)
443ab5c8ca4Slogin                 }
444ab5c8ca4Slogin             }
445ab5c8ca4Slogin 
446ab5c8ca4Slogin             SYS_SENDTO => {
447ab5c8ca4Slogin                 let buf = args[1] as *const u8;
448ab5c8ca4Slogin                 let len = args[2] as usize;
449ab5c8ca4Slogin                 let flags = args[3] as u32;
450ab5c8ca4Slogin                 let addr = args[4] as *const SockAddr;
451ab5c8ca4Slogin                 let addrlen = args[5] as usize;
452be63f3b2Shoumkh                 let virt_buf = VirtAddr::new(buf as usize);
453be63f3b2Shoumkh                 let virt_addr = VirtAddr::new(addr as usize);
454ab5c8ca4Slogin                 // 验证buf的地址是否合法
455be63f3b2Shoumkh                 if verify_area(virt_buf, len as usize).is_err() {
456ab5c8ca4Slogin                     // 地址空间超出了用户空间的范围,不合法
457ab5c8ca4Slogin                     Err(SystemError::EFAULT)
458be63f3b2Shoumkh                 } else if verify_area(virt_addr, addrlen as usize).is_err() {
459ab5c8ca4Slogin                     // 地址空间超出了用户空间的范围,不合法
460ab5c8ca4Slogin                     Err(SystemError::EFAULT)
461ab5c8ca4Slogin                 } else {
462ab5c8ca4Slogin                     let data: &[u8] = unsafe { core::slice::from_raw_parts(buf, len) };
463ab5c8ca4Slogin                     Self::sendto(args[0], data, flags, addr, addrlen)
464ab5c8ca4Slogin                 }
465ab5c8ca4Slogin             }
466ab5c8ca4Slogin 
467ab5c8ca4Slogin             SYS_RECVFROM => {
468ab5c8ca4Slogin                 let buf = args[1] as *mut u8;
469ab5c8ca4Slogin                 let len = args[2] as usize;
470ab5c8ca4Slogin                 let flags = args[3] as u32;
471ab5c8ca4Slogin                 let addr = args[4] as *mut SockAddr;
472ab5c8ca4Slogin                 let addrlen = args[5] as *mut usize;
473be63f3b2Shoumkh                 let virt_buf = VirtAddr::new(buf as usize);
474be63f3b2Shoumkh                 let virt_addrlen = VirtAddr::new(addrlen as usize);
475be63f3b2Shoumkh                 let virt_addr = VirtAddr::new(addr as usize);
476ab5c8ca4Slogin                 let security_check = || {
477ab5c8ca4Slogin                     // 验证buf的地址是否合法
478840045afSLoGin                     if verify_area(virt_buf, len).is_err() {
479ab5c8ca4Slogin                         // 地址空间超出了用户空间的范围,不合法
480ab5c8ca4Slogin                         return Err(SystemError::EFAULT);
481ab5c8ca4Slogin                     }
482ab5c8ca4Slogin 
483ab5c8ca4Slogin                     // 验证addrlen的地址是否合法
484840045afSLoGin                     if verify_area(virt_addrlen, core::mem::size_of::<u32>()).is_err() {
485ab5c8ca4Slogin                         // 地址空间超出了用户空间的范围,不合法
486ab5c8ca4Slogin                         return Err(SystemError::EFAULT);
487ab5c8ca4Slogin                     }
488ab5c8ca4Slogin 
489840045afSLoGin                     if verify_area(virt_addr, core::mem::size_of::<SockAddr>()).is_err() {
490ab5c8ca4Slogin                         // 地址空间超出了用户空间的范围,不合法
491ab5c8ca4Slogin                         return Err(SystemError::EFAULT);
492ab5c8ca4Slogin                     }
493ab5c8ca4Slogin                     return Ok(());
494ab5c8ca4Slogin                 };
495ab5c8ca4Slogin                 let r = security_check();
496ab5c8ca4Slogin                 if r.is_err() {
497ab5c8ca4Slogin                     Err(r.unwrap_err())
498ab5c8ca4Slogin                 } else {
499ab5c8ca4Slogin                     let buf = unsafe { core::slice::from_raw_parts_mut(buf, len) };
500ab5c8ca4Slogin                     Self::recvfrom(args[0], buf, flags, addr, addrlen as *mut u32)
501ab5c8ca4Slogin                 }
502ab5c8ca4Slogin             }
503ab5c8ca4Slogin 
504ab5c8ca4Slogin             SYS_RECVMSG => {
5054ad52e57S裕依2439                 let msg = args[1] as *mut MsgHdr;
506ab5c8ca4Slogin                 let flags = args[2] as u32;
5074ad52e57S裕依2439 
5084ad52e57S裕依2439                 let mut user_buffer_writer =
5094ad52e57S裕依2439                     UserBufferWriter::new(msg, core::mem::size_of::<MsgHdr>(), frame.from_user())?;
5104ad52e57S裕依2439                 let buffer = user_buffer_writer.buffer::<MsgHdr>(0)?;
5114ad52e57S裕依2439 
5124537ffb7SChiichen                 let msg = &mut buffer[0];
513ab5c8ca4Slogin                 Self::recvmsg(args[0], msg, flags)
514ab5c8ca4Slogin             }
515ab5c8ca4Slogin 
516ab5c8ca4Slogin             SYS_LISTEN => Self::listen(args[0], args[1]),
517ab5c8ca4Slogin             SYS_SHUTDOWN => Self::shutdown(args[0], args[1]),
518ab5c8ca4Slogin             SYS_ACCEPT => Self::accept(args[0], args[1] as *mut SockAddr, args[2] as *mut u32),
519c47fe904SLoGin             SYS_ACCEPT4 => Self::accept4(
520c47fe904SLoGin                 args[0],
521c47fe904SLoGin                 args[1] as *mut SockAddr,
522c47fe904SLoGin                 args[2] as *mut u32,
523c47fe904SLoGin                 args[3] as u32,
524c47fe904SLoGin             ),
525ab5c8ca4Slogin             SYS_GETSOCKNAME => {
526ab5c8ca4Slogin                 Self::getsockname(args[0], args[1] as *mut SockAddr, args[2] as *mut u32)
527ab5c8ca4Slogin             }
528ab5c8ca4Slogin             SYS_GETPEERNAME => {
529ab5c8ca4Slogin                 Self::getpeername(args[0], args[1] as *mut SockAddr, args[2] as *mut u32)
530ab5c8ca4Slogin             }
53136fd0130Shoumkh             SYS_GETTIMEOFDAY => {
53236fd0130Shoumkh                 let timeval = args[0] as *mut PosixTimeval;
53340fe15e0SLoGin                 let timezone_ptr = args[1] as *mut PosixTimeZone;
53440fe15e0SLoGin                 Self::gettimeofday(timeval, timezone_ptr)
5354537ffb7SChiichen             }
53640fe15e0SLoGin             SYS_MMAP => {
53740fe15e0SLoGin                 let len = page_align_up(args[1]);
538840045afSLoGin                 let virt_addr = VirtAddr::new(args[0]);
539840045afSLoGin                 if verify_area(virt_addr, len).is_err() {
54040fe15e0SLoGin                     Err(SystemError::EFAULT)
54140fe15e0SLoGin                 } else {
54240fe15e0SLoGin                     Self::mmap(
54340fe15e0SLoGin                         VirtAddr::new(args[0]),
54440fe15e0SLoGin                         len,
54540fe15e0SLoGin                         args[2],
54640fe15e0SLoGin                         args[3],
54740fe15e0SLoGin                         args[4] as i32,
54840fe15e0SLoGin                         args[5],
54940fe15e0SLoGin                     )
55040fe15e0SLoGin                 }
55140fe15e0SLoGin             }
5524cfa009bSJomo             SYS_MREMAP => {
5534cfa009bSJomo                 let old_vaddr = VirtAddr::new(args[0]);
5544cfa009bSJomo                 let old_len = args[1];
5554cfa009bSJomo                 let new_len = args[2];
5564cfa009bSJomo                 let mremap_flags = MremapFlags::from_bits_truncate(args[3] as u8);
5574cfa009bSJomo                 let new_vaddr = VirtAddr::new(args[4]);
5584cfa009bSJomo 
5594cfa009bSJomo                 Self::mremap(old_vaddr, old_len, new_len, mremap_flags, new_vaddr)
5604cfa009bSJomo             }
56140fe15e0SLoGin             SYS_MUNMAP => {
56240fe15e0SLoGin                 let addr = args[0];
56340fe15e0SLoGin                 let len = page_align_up(args[1]);
564971462beSGnoCiYeH                 if addr & (MMArch::PAGE_SIZE - 1) != 0 {
56540fe15e0SLoGin                     // The addr argument is not a multiple of the page size
56640fe15e0SLoGin                     Err(SystemError::EINVAL)
56740fe15e0SLoGin                 } else {
56840fe15e0SLoGin                     Self::munmap(VirtAddr::new(addr), len)
56940fe15e0SLoGin                 }
57040fe15e0SLoGin             }
57140fe15e0SLoGin             SYS_MPROTECT => {
57240fe15e0SLoGin                 let addr = args[0];
57340fe15e0SLoGin                 let len = page_align_up(args[1]);
574971462beSGnoCiYeH                 if addr & (MMArch::PAGE_SIZE - 1) != 0 {
57540fe15e0SLoGin                     // The addr argument is not a multiple of the page size
57640fe15e0SLoGin                     Err(SystemError::EINVAL)
57740fe15e0SLoGin                 } else {
57840fe15e0SLoGin                     Self::mprotect(VirtAddr::new(addr), len, args[2])
57940fe15e0SLoGin                 }
58040fe15e0SLoGin             }
58140fe15e0SLoGin 
5821496ba7bSLoGin             SYS_GETCWD => {
5831496ba7bSLoGin                 let buf = args[0] as *mut u8;
584840045afSLoGin                 let size = args[1];
5851496ba7bSLoGin                 let security_check = || {
5861496ba7bSLoGin                     verify_area(VirtAddr::new(buf as usize), size)?;
5871496ba7bSLoGin                     return Ok(());
5881496ba7bSLoGin                 };
5891496ba7bSLoGin                 let r = security_check();
5901496ba7bSLoGin                 if r.is_err() {
5911496ba7bSLoGin                     Err(r.unwrap_err())
5921496ba7bSLoGin                 } else {
5931496ba7bSLoGin                     let buf = unsafe { core::slice::from_raw_parts_mut(buf, size) };
5941496ba7bSLoGin                     Self::getcwd(buf).map(|ptr| ptr.data())
5951496ba7bSLoGin                 }
5961496ba7bSLoGin             }
5971496ba7bSLoGin 
5981496ba7bSLoGin             SYS_GETPGID => Self::getpgid(Pid::new(args[0])).map(|pid| pid.into()),
5991496ba7bSLoGin 
6001496ba7bSLoGin             SYS_GETPPID => Self::getppid().map(|pid| pid.into()),
601be63f3b2Shoumkh             SYS_FSTAT => {
602be63f3b2Shoumkh                 let fd = args[0] as i32;
603be63f3b2Shoumkh                 let kstat = args[1] as *mut PosixKstat;
604be63f3b2Shoumkh                 let vaddr = VirtAddr::new(kstat as usize);
605be63f3b2Shoumkh                 // FIXME 由于c中的verify_area与rust中的verify_area重名,所以在引入时加了前缀区分
606be63f3b2Shoumkh                 // TODO 应该将用了c版本的verify_area都改为rust的verify_area
607be63f3b2Shoumkh                 match verify_area(vaddr, core::mem::size_of::<PosixKstat>()) {
608be63f3b2Shoumkh                     Ok(_) => Self::fstat(fd, kstat),
609be63f3b2Shoumkh                     Err(e) => Err(e),
610be63f3b2Shoumkh                 }
611be63f3b2Shoumkh             }
612be63f3b2Shoumkh 
6136d81180bSLoGin             SYS_FCNTL => {
6146d81180bSLoGin                 let fd = args[0] as i32;
6156d81180bSLoGin                 let cmd: Option<FcntlCommand> =
6166d81180bSLoGin                     <FcntlCommand as FromPrimitive>::from_u32(args[1] as u32);
6176d81180bSLoGin                 let arg = args[2] as i32;
6186d81180bSLoGin                 let res = if let Some(cmd) = cmd {
6196d81180bSLoGin                     Self::fcntl(fd, cmd, arg)
6206d81180bSLoGin                 } else {
6216d81180bSLoGin                     Err(SystemError::EINVAL)
6226d81180bSLoGin                 };
6236d81180bSLoGin 
6246d81180bSLoGin                 // kdebug!("FCNTL: fd: {}, cmd: {:?}, arg: {}, res: {:?}", fd, cmd, arg, res);
6256d81180bSLoGin                 res
6266d81180bSLoGin             }
6276d81180bSLoGin 
6286d81180bSLoGin             SYS_FTRUNCATE => {
6296d81180bSLoGin                 let fd = args[0] as i32;
630840045afSLoGin                 let len = args[1];
6316d81180bSLoGin                 let res = Self::ftruncate(fd, len);
6326d81180bSLoGin                 // kdebug!("FTRUNCATE: fd: {}, len: {}, res: {:?}", fd, len, res);
6336d81180bSLoGin                 res
6346d81180bSLoGin             }
6356d81180bSLoGin 
6364fda81ceSLoGin             #[cfg(target_arch = "x86_64")]
6372dbef785SGnoCiYeH             SYS_MKNOD => {
6382dbef785SGnoCiYeH                 let path = args[0];
6392dbef785SGnoCiYeH                 let flags = args[1];
6402dbef785SGnoCiYeH                 let dev_t = args[2];
6412dbef785SGnoCiYeH                 let flags: ModeType = ModeType::from_bits_truncate(flags as u32);
64282df0a13Shmt                 Self::mknod(path as *const u8, flags, DeviceNumber::from(dev_t as u32))
6432dbef785SGnoCiYeH             }
6442dbef785SGnoCiYeH 
645971462beSGnoCiYeH             SYS_CLONE => {
646971462beSGnoCiYeH                 let parent_tid = VirtAddr::new(args[2]);
647971462beSGnoCiYeH                 let child_tid = VirtAddr::new(args[3]);
648971462beSGnoCiYeH 
649971462beSGnoCiYeH                 // 地址校验
650971462beSGnoCiYeH                 verify_area(parent_tid, core::mem::size_of::<i32>())?;
651971462beSGnoCiYeH                 verify_area(child_tid, core::mem::size_of::<i32>())?;
652971462beSGnoCiYeH 
653971462beSGnoCiYeH                 let mut clone_args = KernelCloneArgs::new();
654971462beSGnoCiYeH                 clone_args.flags = CloneFlags::from_bits_truncate(args[0] as u64);
655971462beSGnoCiYeH                 clone_args.stack = args[1];
656971462beSGnoCiYeH                 clone_args.parent_tid = parent_tid;
657971462beSGnoCiYeH                 clone_args.child_tid = child_tid;
658971462beSGnoCiYeH                 clone_args.tls = args[4];
659971462beSGnoCiYeH                 Self::clone(frame, clone_args)
660971462beSGnoCiYeH             }
661971462beSGnoCiYeH 
662971462beSGnoCiYeH             SYS_FUTEX => {
663971462beSGnoCiYeH                 let uaddr = VirtAddr::new(args[0]);
664971462beSGnoCiYeH                 let operation = FutexFlag::from_bits(args[1] as u32).ok_or(SystemError::ENOSYS)?;
665971462beSGnoCiYeH                 let val = args[2] as u32;
666971462beSGnoCiYeH                 let utime = args[3];
667971462beSGnoCiYeH                 let uaddr2 = VirtAddr::new(args[4]);
668971462beSGnoCiYeH                 let val3 = args[5] as u32;
669971462beSGnoCiYeH 
670971462beSGnoCiYeH                 verify_area(uaddr, core::mem::size_of::<u32>())?;
671971462beSGnoCiYeH                 verify_area(uaddr2, core::mem::size_of::<u32>())?;
672971462beSGnoCiYeH 
673971462beSGnoCiYeH                 let mut timespec = None;
674971462beSGnoCiYeH                 if utime != 0 && operation.contains(FutexFlag::FLAGS_HAS_TIMEOUT) {
675971462beSGnoCiYeH                     let reader = UserBufferReader::new(
676971462beSGnoCiYeH                         utime as *const TimeSpec,
677971462beSGnoCiYeH                         core::mem::size_of::<TimeSpec>(),
678971462beSGnoCiYeH                         true,
679971462beSGnoCiYeH                     )?;
680971462beSGnoCiYeH 
681840045afSLoGin                     timespec = Some(*reader.read_one_from_user::<TimeSpec>(0)?);
682971462beSGnoCiYeH                 }
683971462beSGnoCiYeH 
684971462beSGnoCiYeH                 Self::do_futex(uaddr, operation, val, timespec, uaddr2, utime as u32, val3)
685971462beSGnoCiYeH             }
686971462beSGnoCiYeH 
687bf4a4899SLoGin             SYS_READV => Self::readv(args[0] as i32, args[1], args[2]),
688971462beSGnoCiYeH             SYS_WRITEV => Self::writev(args[0] as i32, args[1], args[2]),
689971462beSGnoCiYeH 
6904fda81ceSLoGin             SYS_SET_TID_ADDRESS => Self::set_tid_address(args[0]),
691971462beSGnoCiYeH 
6924fda81ceSLoGin             #[cfg(target_arch = "x86_64")]
6934fda81ceSLoGin             SYS_LSTAT => {
69482df0a13Shmt                 let path = args[0] as *const u8;
695971462beSGnoCiYeH                 let kstat = args[1] as *mut PosixKstat;
69682df0a13Shmt                 Self::lstat(path, kstat)
6974fda81ceSLoGin             }
6984fda81ceSLoGin 
6994fda81ceSLoGin             #[cfg(target_arch = "x86_64")]
7004fda81ceSLoGin             SYS_STAT => {
70182df0a13Shmt                 let path = args[0] as *const u8;
7024fda81ceSLoGin                 let kstat = args[1] as *mut PosixKstat;
70382df0a13Shmt                 Self::stat(path, kstat)
704971462beSGnoCiYeH             }
705971462beSGnoCiYeH 
70640609970SGnoCiYeH             SYS_EPOLL_CREATE => Self::epoll_create(args[0] as i32),
70740609970SGnoCiYeH             SYS_EPOLL_CREATE1 => Self::epoll_create1(args[0]),
70840609970SGnoCiYeH 
70940609970SGnoCiYeH             SYS_EPOLL_CTL => Self::epoll_ctl(
71040609970SGnoCiYeH                 args[0] as i32,
71140609970SGnoCiYeH                 args[1],
71240609970SGnoCiYeH                 args[2] as i32,
71340609970SGnoCiYeH                 VirtAddr::new(args[3]),
71440609970SGnoCiYeH             ),
71540609970SGnoCiYeH 
71640609970SGnoCiYeH             SYS_EPOLL_WAIT => Self::epoll_wait(
71740609970SGnoCiYeH                 args[0] as i32,
71840609970SGnoCiYeH                 VirtAddr::new(args[1]),
71940609970SGnoCiYeH                 args[2] as i32,
72040609970SGnoCiYeH                 args[3] as i32,
72140609970SGnoCiYeH             ),
72240609970SGnoCiYeH 
72340609970SGnoCiYeH             SYS_EPOLL_PWAIT => {
72440609970SGnoCiYeH                 let epfd = args[0] as i32;
72540609970SGnoCiYeH                 let epoll_event = VirtAddr::new(args[1]);
72640609970SGnoCiYeH                 let max_events = args[2] as i32;
72740609970SGnoCiYeH                 let timespec = args[3] as i32;
72840609970SGnoCiYeH                 let sigmask_addr = args[4] as *mut SigSet;
72940609970SGnoCiYeH 
73040609970SGnoCiYeH                 if sigmask_addr.is_null() {
73140609970SGnoCiYeH                     return Self::epoll_wait(epfd, epoll_event, max_events, timespec);
73240609970SGnoCiYeH                 }
73340609970SGnoCiYeH                 let sigmask_reader =
73440609970SGnoCiYeH                     UserBufferReader::new(sigmask_addr, core::mem::size_of::<SigSet>(), true)?;
735840045afSLoGin                 let mut sigmask = *sigmask_reader.read_one_from_user::<SigSet>(0)?;
73640609970SGnoCiYeH 
73740609970SGnoCiYeH                 Self::epoll_pwait(
73840609970SGnoCiYeH                     args[0] as i32,
73940609970SGnoCiYeH                     VirtAddr::new(args[1]),
74040609970SGnoCiYeH                     args[2] as i32,
74140609970SGnoCiYeH                     args[3] as i32,
74240609970SGnoCiYeH                     &mut sigmask,
74340609970SGnoCiYeH                 )
74440609970SGnoCiYeH             }
74540609970SGnoCiYeH 
746971462beSGnoCiYeH             // 目前为了适配musl-libc,以下系统调用先这样写着
7474fda81ceSLoGin             SYS_GETRANDOM => {
748971462beSGnoCiYeH                 let flags = GRandFlags::from_bits(args[2] as u8).ok_or(SystemError::EINVAL)?;
749971462beSGnoCiYeH                 Self::get_random(args[0] as *mut u8, args[1], flags)
750971462beSGnoCiYeH             }
751971462beSGnoCiYeH 
7524fda81ceSLoGin             SYS_SOCKETPAIR => {
7534ad52e57S裕依2439                 let mut user_buffer_writer = UserBufferWriter::new(
7544ad52e57S裕依2439                     args[3] as *mut c_int,
7554ad52e57S裕依2439                     core::mem::size_of::<[c_int; 2]>(),
7564ad52e57S裕依2439                     frame.from_user(),
7574ad52e57S裕依2439                 )?;
7584ad52e57S裕依2439                 let fds = user_buffer_writer.buffer::<i32>(0)?;
7594ad52e57S裕依2439                 Self::socketpair(args[0], args[1], args[2], fds)
760971462beSGnoCiYeH             }
761971462beSGnoCiYeH 
7624fda81ceSLoGin             #[cfg(target_arch = "x86_64")]
763971462beSGnoCiYeH             SYS_POLL => {
764971462beSGnoCiYeH                 kwarn!("SYS_POLL has not yet been implemented");
765971462beSGnoCiYeH                 Ok(0)
766971462beSGnoCiYeH             }
767971462beSGnoCiYeH 
76852da9a59SGnoCiYeH             SYS_SETPGID => {
76952da9a59SGnoCiYeH                 kwarn!("SYS_SETPGID has not yet been implemented");
77052da9a59SGnoCiYeH                 Ok(0)
77152da9a59SGnoCiYeH             }
77252da9a59SGnoCiYeH 
773971462beSGnoCiYeH             SYS_RT_SIGPROCMASK => {
774971462beSGnoCiYeH                 kwarn!("SYS_RT_SIGPROCMASK has not yet been implemented");
775971462beSGnoCiYeH                 Ok(0)
776971462beSGnoCiYeH             }
777971462beSGnoCiYeH 
778971462beSGnoCiYeH             SYS_TKILL => {
779971462beSGnoCiYeH                 kwarn!("SYS_TKILL has not yet been implemented");
780971462beSGnoCiYeH                 Ok(0)
781971462beSGnoCiYeH             }
782971462beSGnoCiYeH 
783971462beSGnoCiYeH             SYS_SIGALTSTACK => {
784971462beSGnoCiYeH                 kwarn!("SYS_SIGALTSTACK has not yet been implemented");
785971462beSGnoCiYeH                 Ok(0)
786971462beSGnoCiYeH             }
787971462beSGnoCiYeH 
78816033951SGnoCiYeH             SYS_EXIT_GROUP => {
78916033951SGnoCiYeH                 kwarn!("SYS_EXIT_GROUP has not yet been implemented");
79016033951SGnoCiYeH                 Ok(0)
79116033951SGnoCiYeH             }
79216033951SGnoCiYeH 
79304babc3fSMemoryShore             SYS_MADVISE => {
7946c7f966cSMemoryShore                 // 这个太吵了,总是打印,先注释掉
7956c7f966cSMemoryShore                 // kwarn!("SYS_MADVISE has not yet been implemented");
79604babc3fSMemoryShore                 Ok(0)
79704babc3fSMemoryShore             }
798393f6915SLoGin             SYS_GETTID => Self::gettid().map(|tid| tid.into()),
799840045afSLoGin             SYS_GETUID => Self::getuid(),
8008d72b68dSJomo 
80102e249f3SLoGin             SYS_SYSLOG => {
802840045afSLoGin                 let syslog_action_type = args[0];
8038d72b68dSJomo                 let buf_vaddr = args[1];
8048d72b68dSJomo                 let len = args[2];
8058d72b68dSJomo                 let from_user = frame.from_user();
8068d72b68dSJomo                 let mut user_buffer_writer =
8078d72b68dSJomo                     UserBufferWriter::new(buf_vaddr as *mut u8, len, from_user)?;
8088d72b68dSJomo 
8098d72b68dSJomo                 let user_buf = user_buffer_writer.buffer(0)?;
810840045afSLoGin                 Self::do_syslog(syslog_action_type, user_buf, len)
81102e249f3SLoGin             }
8128d72b68dSJomo 
813840045afSLoGin             SYS_GETGID => Self::getgid(),
81402e249f3SLoGin             SYS_SETUID => {
81502e249f3SLoGin                 kwarn!("SYS_SETUID has not yet been implemented");
81602e249f3SLoGin                 Ok(0)
81702e249f3SLoGin             }
81802e249f3SLoGin             SYS_SETGID => {
81902e249f3SLoGin                 kwarn!("SYS_SETGID has not yet been implemented");
82002e249f3SLoGin                 Ok(0)
82102e249f3SLoGin             }
822840045afSLoGin             SYS_GETEUID => Self::geteuid(),
823840045afSLoGin             SYS_GETEGID => Self::getegid(),
824be8cdf4bSLoGin             SYS_GETRUSAGE => {
825be8cdf4bSLoGin                 let who = args[0] as c_int;
826be8cdf4bSLoGin                 let rusage = args[1] as *mut RUsage;
827be8cdf4bSLoGin                 Self::get_rusage(who, rusage)
828be8cdf4bSLoGin             }
82904babc3fSMemoryShore 
8304fda81ceSLoGin             #[cfg(target_arch = "x86_64")]
831709498caSLoGin             SYS_READLINK => {
832709498caSLoGin                 let path = args[0] as *const u8;
833709498caSLoGin                 let buf = args[1] as *mut u8;
834840045afSLoGin                 let bufsiz = args[2];
835709498caSLoGin                 Self::readlink(path, buf, bufsiz)
836709498caSLoGin             }
837709498caSLoGin 
8384fda81ceSLoGin             SYS_READLINKAT => {
839709498caSLoGin                 let dirfd = args[0] as i32;
84082df0a13Shmt                 let path = args[1] as *const u8;
841709498caSLoGin                 let buf = args[2] as *mut u8;
842840045afSLoGin                 let bufsiz = args[3];
84382df0a13Shmt                 Self::readlink_at(dirfd, path, buf, bufsiz)
844709498caSLoGin             }
845709498caSLoGin 
8460d9b7d92SLoGin             SYS_PRLIMIT64 => {
8470d9b7d92SLoGin                 let pid = args[0];
8480d9b7d92SLoGin                 let pid = Pid::new(pid);
8490d9b7d92SLoGin                 let resource = args[1];
8500d9b7d92SLoGin                 let new_limit = args[2] as *const RLimit64;
8510d9b7d92SLoGin                 let old_limit = args[3] as *mut RLimit64;
8520d9b7d92SLoGin 
8530d9b7d92SLoGin                 Self::prlimit64(pid, resource, new_limit, old_limit)
8540d9b7d92SLoGin             }
8550d9b7d92SLoGin 
8564fda81ceSLoGin             #[cfg(target_arch = "x86_64")]
8579b0abe6dSLoGin             SYS_ACCESS => {
8589b0abe6dSLoGin                 let pathname = args[0] as *const u8;
8599b0abe6dSLoGin                 let mode = args[1] as u32;
8609b0abe6dSLoGin                 Self::access(pathname, mode)
8619b0abe6dSLoGin             }
8629b0abe6dSLoGin 
8639b0abe6dSLoGin             SYS_FACCESSAT => {
8649b0abe6dSLoGin                 let dirfd = args[0] as i32;
8659b0abe6dSLoGin                 let pathname = args[1] as *const u8;
8669b0abe6dSLoGin                 let mode = args[2] as u32;
8679b0abe6dSLoGin                 Self::faccessat2(dirfd, pathname, mode, 0)
8689b0abe6dSLoGin             }
8699b0abe6dSLoGin 
8709b0abe6dSLoGin             SYS_FACCESSAT2 => {
8719b0abe6dSLoGin                 let dirfd = args[0] as i32;
8729b0abe6dSLoGin                 let pathname = args[1] as *const u8;
8739b0abe6dSLoGin                 let mode = args[2] as u32;
8749b0abe6dSLoGin                 let flags = args[3] as u32;
8759b0abe6dSLoGin                 Self::faccessat2(dirfd, pathname, mode, flags)
8769b0abe6dSLoGin             }
8779b0abe6dSLoGin 
878bf4a4899SLoGin             SYS_CLOCK_GETTIME => {
879bf4a4899SLoGin                 let clockid = args[0] as i32;
880bf4a4899SLoGin                 let timespec = args[1] as *mut TimeSpec;
881bf4a4899SLoGin                 Self::clock_gettime(clockid, timespec)
882bf4a4899SLoGin             }
883bf4a4899SLoGin 
884bf4a4899SLoGin             SYS_SYSINFO => {
885bf4a4899SLoGin                 let info = args[0] as *mut SysInfo;
886bf4a4899SLoGin                 Self::sysinfo(info)
887bf4a4899SLoGin             }
888bf4a4899SLoGin 
889bf4a4899SLoGin             SYS_UMASK => {
890bf4a4899SLoGin                 let mask = args[0] as u32;
891bf4a4899SLoGin                 Self::umask(mask)
892bf4a4899SLoGin             }
893bf4a4899SLoGin 
89427b967a3S裕依             SYS_FCHOWN => {
89527b967a3S裕依                 kwarn!("SYS_FCHOWN has not yet been implemented");
89627b967a3S裕依                 Ok(0)
89727b967a3S裕依             }
89827b967a3S裕依 
89927b967a3S裕依             SYS_FSYNC => {
90027b967a3S裕依                 kwarn!("SYS_FSYNC has not yet been implemented");
90127b967a3S裕依                 Ok(0)
90227b967a3S裕依             }
90327b967a3S裕依 
9044fda81ceSLoGin             #[cfg(target_arch = "x86_64")]
905bf4a4899SLoGin             SYS_CHMOD => {
906bf4a4899SLoGin                 let pathname = args[0] as *const u8;
907bf4a4899SLoGin                 let mode = args[1] as u32;
908bf4a4899SLoGin                 Self::chmod(pathname, mode)
909bf4a4899SLoGin             }
910bf4a4899SLoGin             SYS_FCHMOD => {
911bf4a4899SLoGin                 let fd = args[0] as i32;
912bf4a4899SLoGin                 let mode = args[1] as u32;
913bf4a4899SLoGin                 Self::fchmod(fd, mode)
914bf4a4899SLoGin             }
915bf4a4899SLoGin             SYS_FCHMODAT => {
916bf4a4899SLoGin                 let dirfd = args[0] as i32;
917bf4a4899SLoGin                 let pathname = args[1] as *const u8;
918bf4a4899SLoGin                 let mode = args[2] as u32;
919bf4a4899SLoGin                 Self::fchmodat(dirfd, pathname, mode)
920bf4a4899SLoGin             }
921bf4a4899SLoGin 
92202343d0bSLoGin             SYS_SCHED_GETAFFINITY => {
92302343d0bSLoGin                 // todo: 这个系统调用还没有实现
92402343d0bSLoGin 
92502343d0bSLoGin                 Err(SystemError::ENOSYS)
92602343d0bSLoGin             }
92702343d0bSLoGin 
92852da9a59SGnoCiYeH             #[cfg(target_arch = "x86_64")]
92952da9a59SGnoCiYeH             SYS_GETRLIMIT => {
93052da9a59SGnoCiYeH                 let resource = args[0];
93152da9a59SGnoCiYeH                 let rlimit = args[1] as *mut RLimit64;
93252da9a59SGnoCiYeH 
93352da9a59SGnoCiYeH                 Self::prlimit64(
93452da9a59SGnoCiYeH                     ProcessManager::current_pcb().pid(),
93552da9a59SGnoCiYeH                     resource,
936840045afSLoGin                     core::ptr::null::<RLimit64>(),
93752da9a59SGnoCiYeH                     rlimit,
93852da9a59SGnoCiYeH                 )
93952da9a59SGnoCiYeH             }
94052da9a59SGnoCiYeH 
9419e481b3bSTTaq             SYS_FADVISE64 => {
9429e481b3bSTTaq                 // todo: 这个系统调用还没有实现
9439e481b3bSTTaq 
9449e481b3bSTTaq                 Err(SystemError::ENOSYS)
9459e481b3bSTTaq             }
946*1d37ca6dSDonkey Kane 
947*1d37ca6dSDonkey Kane             SYS_MOUNT => {
948*1d37ca6dSDonkey Kane                 let source = args[0] as *const u8;
949*1d37ca6dSDonkey Kane                 let target = args[1] as *const u8;
950*1d37ca6dSDonkey Kane                 let filesystemtype = args[2] as *const u8;
951*1d37ca6dSDonkey Kane                 return Self::mount(source, target, filesystemtype, 0, null());
952*1d37ca6dSDonkey Kane             }
9539e481b3bSTTaq             SYS_NEWFSTATAT => {
9549e481b3bSTTaq                 // todo: 这个系统调用还没有实现
9559e481b3bSTTaq 
9569e481b3bSTTaq                 Err(SystemError::ENOSYS)
9579e481b3bSTTaq             }
9589e481b3bSTTaq 
95902343d0bSLoGin             SYS_SCHED_YIELD => Self::sched_yield(),
9608c6f2184Ssspphh             SYS_UNAME => {
9618c6f2184Ssspphh                 let name = args[0] as *mut PosixOldUtsName;
9628c6f2184Ssspphh                 Self::uname(name)
9638c6f2184Ssspphh             }
964*1d37ca6dSDonkey Kane 
965ab5c8ca4Slogin             _ => panic!("Unsupported syscall ID: {}", syscall_num),
966ab5c8ca4Slogin         };
9674fda81ceSLoGin 
968ab5c8ca4Slogin         return r;
969ab5c8ca4Slogin     }
970ab5c8ca4Slogin 
971ab5c8ca4Slogin     pub fn put_string(
972ab5c8ca4Slogin         s: *const u8,
973ab5c8ca4Slogin         front_color: u32,
974ab5c8ca4Slogin         back_color: u32,
975ab5c8ca4Slogin     ) -> Result<usize, SystemError> {
976c3dc6f2fSLoGin         // todo: 删除这个系统调用
977c3dc6f2fSLoGin         let s = check_and_clone_cstr(s, Some(4096))?;
978c3dc6f2fSLoGin         let fr = (front_color & 0x00ff0000) >> 16;
979c3dc6f2fSLoGin         let fg = (front_color & 0x0000ff00) >> 8;
980c3dc6f2fSLoGin         let fb = front_color & 0x000000ff;
981c3dc6f2fSLoGin         let br = (back_color & 0x00ff0000) >> 16;
982c3dc6f2fSLoGin         let bg = (back_color & 0x0000ff00) >> 8;
983c3dc6f2fSLoGin         let bb = back_color & 0x000000ff;
984c3dc6f2fSLoGin         print!("\x1B[38;2;{fr};{fg};{fb};48;2;{br};{bg};{bb}m{s}\x1B[0m");
985c3dc6f2fSLoGin         return Ok(s.len());
986ab5c8ca4Slogin     }
987ab5c8ca4Slogin 
988ab5c8ca4Slogin     pub fn reboot() -> Result<usize, SystemError> {
9894fda81ceSLoGin         unsafe { cpu_reset() };
990ab5c8ca4Slogin     }
991ab5c8ca4Slogin }
992