xref: /DragonOS/kernel/src/syscall/mod.rs (revision 1074eb34e784aa2adfc5b9e0d89fa4b7e6ea03ef)
1ab5c8ca4Slogin use core::{
282df0a13Shmt     ffi::{c_int, c_void},
31d37ca6dSDonkey Kane     ptr::null,
4ab5c8ca4Slogin     sync::atomic::{AtomicBool, Ordering},
5ab5c8ca4Slogin };
6ab5c8ca4Slogin 
7971462beSGnoCiYeH use crate::{
840609970SGnoCiYeH     arch::{ipc::signal::SigSet, syscall::nr::*},
9f0c87a89SGnoCiYeH     driver::base::device::device_number::DeviceNumber,
10597ecc08STTaq     filesystem::vfs::syscall::{PosixStatfs, PosixStatx},
116fc066acSJomo     ipc::shm::{ShmCtlCmd, ShmFlags, ShmId, ShmKey},
12971462beSGnoCiYeH     libs::{futex::constant::FutexFlag, rand::GRandFlags},
134cfa009bSJomo     mm::syscall::MremapFlags,
144ad52e57S裕依2439     net::syscall::MsgHdr,
150d9b7d92SLoGin     process::{
160d9b7d92SLoGin         fork::KernelCloneArgs,
170d9b7d92SLoGin         resource::{RLimit64, RUsage},
18f0c87a89SGnoCiYeH         ProcessFlags, ProcessManager,
190d9b7d92SLoGin     },
20f0c87a89SGnoCiYeH     sched::{schedule, SchedMode},
21c3dc6f2fSLoGin     syscall::user_access::check_and_clone_cstr,
22971462beSGnoCiYeH };
2340314b30SXiaoye Zheng 
2491e9d4abSLoGin use num_traits::FromPrimitive;
2591e9d4abSLoGin use system_error::SystemError;
26676b8ef6SMork 
27ab5c8ca4Slogin use crate::{
281496ba7bSLoGin     arch::{cpu::cpu_reset, interrupt::TrapFrame, MMArch},
29ab5c8ca4Slogin     filesystem::vfs::{
309e481b3bSTTaq         fcntl::{AtFlags, FcntlCommand},
31ab5c8ca4Slogin         file::FileMode,
3282df0a13Shmt         syscall::{ModeType, PosixKstat},
33ab5c8ca4Slogin         MAX_PATHLEN,
34ab5c8ca4Slogin     },
3582df0a13Shmt     include::bindings::bindings::PAGE_4K_SIZE,
36aa0367d6SLoGin     kinfo,
3740fe15e0SLoGin     libs::align::page_align_up,
38be63f3b2Shoumkh     mm::{verify_area, MemoryManagementArch, VirtAddr},
39ab5c8ca4Slogin     net::syscall::SockAddr,
408c6f2184Ssspphh     process::{fork::CloneFlags, syscall::PosixOldUtsName, Pid},
4136fd0130Shoumkh     time::{
4240fe15e0SLoGin         syscall::{PosixTimeZone, PosixTimeval},
436fc066acSJomo         PosixTimeSpec,
4436fd0130Shoumkh     },
45ab5c8ca4Slogin };
46ab5c8ca4Slogin 
47bf4a4899SLoGin use self::{
48bf4a4899SLoGin     misc::SysInfo,
49bf4a4899SLoGin     user_access::{UserBufferReader, UserBufferWriter},
50bf4a4899SLoGin };
514537ffb7SChiichen 
52bf4a4899SLoGin pub mod misc;
5340fe15e0SLoGin pub mod user_access;
5440fe15e0SLoGin 
55ad1d649eSGnoCiYeH // 与linux不一致的调用,在linux基础上累加
56ad1d649eSGnoCiYeH pub const SYS_PUT_STRING: usize = 100000;
57ad1d649eSGnoCiYeH pub const SYS_SBRK: usize = 100001;
58ab5c8ca4Slogin /// todo: 该系统调用与Linux不一致,将来需要删除该系统调用!!! 删的时候记得改C版本的libc
59ad1d649eSGnoCiYeH pub const SYS_CLOCK: usize = 100002;
60ad1d649eSGnoCiYeH pub const SYS_SCHED: usize = 100003;
6140314b30SXiaoye Zheng 
62ab5c8ca4Slogin #[derive(Debug)]
63ab5c8ca4Slogin pub struct Syscall;
64ab5c8ca4Slogin 
65ab5c8ca4Slogin impl Syscall {
66ab5c8ca4Slogin     /// 初始化系统调用
675b59005fSLoGin     #[inline(never)]
68ab5c8ca4Slogin     pub fn init() -> Result<(), SystemError> {
69ab5c8ca4Slogin         static INIT_FLAG: AtomicBool = AtomicBool::new(false);
70ab5c8ca4Slogin         let prev = INIT_FLAG.swap(true, Ordering::SeqCst);
71ab5c8ca4Slogin         if prev {
72ab5c8ca4Slogin             panic!("Cannot initialize syscall more than once!");
73ab5c8ca4Slogin         }
745b59005fSLoGin         kinfo!("Initializing syscall...");
755b59005fSLoGin         let r = crate::arch::syscall::arch_syscall_init();
765b59005fSLoGin         kinfo!("Syscall init successfully!");
775b59005fSLoGin 
785b59005fSLoGin         return r;
79ab5c8ca4Slogin     }
80ab5c8ca4Slogin     /// @brief 系统调用分发器,用于分发系统调用。
81ab5c8ca4Slogin     ///
82ab5c8ca4Slogin     /// 这个函数内,需要根据系统调用号,调用对应的系统调用处理函数。
83ab5c8ca4Slogin     /// 并且,对于用户态传入的指针参数,需要在本函数内进行越界检查,防止访问到内核空间。
844fda81ceSLoGin     #[inline(never)]
859e9ffedfSPlucky923     pub fn handle(
869e9ffedfSPlucky923         syscall_num: usize,
879e9ffedfSPlucky923         args: &[usize],
889e9ffedfSPlucky923         frame: &mut TrapFrame,
899e9ffedfSPlucky923     ) -> Result<usize, SystemError> {
90ab5c8ca4Slogin         let r = match syscall_num {
91ab5c8ca4Slogin             SYS_PUT_STRING => {
92ab5c8ca4Slogin                 Self::put_string(args[0] as *const u8, args[1] as u32, args[2] as u32)
93ab5c8ca4Slogin             }
944fda81ceSLoGin             #[cfg(target_arch = "x86_64")]
95ab5c8ca4Slogin             SYS_OPEN => {
9682df0a13Shmt                 let path = args[0] as *const u8;
9782df0a13Shmt                 let flags = args[1] as u32;
9882df0a13Shmt                 let mode = args[2] as u32;
9934e6d6c8Syuyi2439 
10082df0a13Shmt                 Self::open(path, flags, mode, true)
1010fb515b0SLoGin             }
1020fb515b0SLoGin 
10370f159a3SLoGin             #[cfg(target_arch = "x86_64")]
1049e481b3bSTTaq             SYS_RENAME => {
1059e481b3bSTTaq                 let oldname: *const u8 = args[0] as *const u8;
1069e481b3bSTTaq                 let newname: *const u8 = args[1] as *const u8;
1079e481b3bSTTaq                 Self::do_renameat2(
1089e481b3bSTTaq                     AtFlags::AT_FDCWD.bits(),
1099e481b3bSTTaq                     oldname,
1109e481b3bSTTaq                     AtFlags::AT_FDCWD.bits(),
1119e481b3bSTTaq                     newname,
1129e481b3bSTTaq                     0,
1139e481b3bSTTaq                 )
1149e481b3bSTTaq             }
1159e481b3bSTTaq 
11670f159a3SLoGin             #[cfg(target_arch = "x86_64")]
1179e481b3bSTTaq             SYS_RENAMEAT => {
1189e481b3bSTTaq                 let oldfd = args[0] as i32;
1199e481b3bSTTaq                 let oldname: *const u8 = args[1] as *const u8;
1209e481b3bSTTaq                 let newfd = args[2] as i32;
1219e481b3bSTTaq                 let newname: *const u8 = args[3] as *const u8;
1229e481b3bSTTaq                 Self::do_renameat2(oldfd, oldname, newfd, newname, 0)
1239e481b3bSTTaq             }
1249e481b3bSTTaq 
1259e481b3bSTTaq             SYS_RENAMEAT2 => {
1269e481b3bSTTaq                 let oldfd = args[0] as i32;
1279e481b3bSTTaq                 let oldname: *const u8 = args[1] as *const u8;
1289e481b3bSTTaq                 let newfd = args[2] as i32;
1299e481b3bSTTaq                 let newname: *const u8 = args[3] as *const u8;
1309e481b3bSTTaq                 let flags = args[4] as u32;
1319e481b3bSTTaq                 Self::do_renameat2(oldfd, oldname, newfd, newname, flags)
1329e481b3bSTTaq             }
1339e481b3bSTTaq 
1340fb515b0SLoGin             SYS_OPENAT => {
1350fb515b0SLoGin                 let dirfd = args[0] as i32;
13682df0a13Shmt                 let path = args[1] as *const u8;
13782df0a13Shmt                 let flags = args[2] as u32;
13882df0a13Shmt                 let mode = args[3] as u32;
1390fb515b0SLoGin 
14082df0a13Shmt                 Self::openat(dirfd, path, flags, mode, true)
141ab5c8ca4Slogin             }
142ab5c8ca4Slogin             SYS_CLOSE => {
143ab5c8ca4Slogin                 let fd = args[0];
14427b967a3S裕依                 Self::close(fd)
145ab5c8ca4Slogin             }
146ab5c8ca4Slogin             SYS_READ => {
147ab5c8ca4Slogin                 let fd = args[0] as i32;
148ab5c8ca4Slogin                 let buf_vaddr = args[1];
149ab5c8ca4Slogin                 let len = args[2];
150b5b571e0SLoGin                 let from_user = frame.is_from_user();
1519e9ffedfSPlucky923                 let mut user_buffer_writer =
1529e9ffedfSPlucky923                     UserBufferWriter::new(buf_vaddr as *mut u8, len, from_user)?;
1536d81180bSLoGin 
1549e9ffedfSPlucky923                 let user_buf = user_buffer_writer.buffer(0)?;
15527b967a3S裕依                 Self::read(fd, user_buf)
156ab5c8ca4Slogin             }
157ab5c8ca4Slogin             SYS_WRITE => {
158ab5c8ca4Slogin                 let fd = args[0] as i32;
159ab5c8ca4Slogin                 let buf_vaddr = args[1];
160ab5c8ca4Slogin                 let len = args[2];
161b5b571e0SLoGin                 let from_user = frame.is_from_user();
1629e9ffedfSPlucky923                 let user_buffer_reader =
1639e9ffedfSPlucky923                     UserBufferReader::new(buf_vaddr as *const u8, len, from_user)?;
1646d81180bSLoGin 
1659e9ffedfSPlucky923                 let user_buf = user_buffer_reader.read_from_user(0)?;
16627b967a3S裕依                 Self::write(fd, user_buf)
167ab5c8ca4Slogin             }
168ab5c8ca4Slogin 
169ab5c8ca4Slogin             SYS_LSEEK => {
170ab5c8ca4Slogin                 let fd = args[0] as i32;
171ab5c8ca4Slogin                 let offset = args[1] as i64;
172ab5c8ca4Slogin                 let whence = args[2] as u32;
173ab5c8ca4Slogin 
17482df0a13Shmt                 Self::lseek(fd, offset, whence)
175ab5c8ca4Slogin             }
17627b967a3S裕依 
17727b967a3S裕依             SYS_PREAD64 => {
17827b967a3S裕依                 let fd = args[0] as i32;
17927b967a3S裕依                 let buf_vaddr = args[1];
18027b967a3S裕依                 let len = args[2];
18127b967a3S裕依                 let offset = args[3];
18227b967a3S裕依 
18327b967a3S裕依                 let mut user_buffer_writer =
184b5b571e0SLoGin                     UserBufferWriter::new(buf_vaddr as *mut u8, len, frame.is_from_user())?;
18527b967a3S裕依                 let buf = user_buffer_writer.buffer(0)?;
18627b967a3S裕依                 Self::pread(fd, buf, len, offset)
18727b967a3S裕依             }
18827b967a3S裕依 
18927b967a3S裕依             SYS_PWRITE64 => {
19027b967a3S裕依                 let fd = args[0] as i32;
19127b967a3S裕依                 let buf_vaddr = args[1];
19227b967a3S裕依                 let len = args[2];
19327b967a3S裕依                 let offset = args[3];
19427b967a3S裕依 
19527b967a3S裕依                 let user_buffer_reader =
196b5b571e0SLoGin                     UserBufferReader::new(buf_vaddr as *const u8, len, frame.is_from_user())?;
19727b967a3S裕依 
19827b967a3S裕依                 let buf = user_buffer_reader.read_from_user(0)?;
19927b967a3S裕依                 Self::pwrite(fd, buf, len, offset)
20027b967a3S裕依             }
20127b967a3S裕依 
20240314b30SXiaoye Zheng             SYS_IOCTL => {
20340314b30SXiaoye Zheng                 let fd = args[0];
20440314b30SXiaoye Zheng                 let cmd = args[1];
20540314b30SXiaoye Zheng                 let data = args[2];
20640314b30SXiaoye Zheng                 Self::ioctl(fd, cmd as u32, data)
20740314b30SXiaoye Zheng             }
208ab5c8ca4Slogin 
2094fda81ceSLoGin             #[cfg(target_arch = "x86_64")]
2101496ba7bSLoGin             SYS_FORK => Self::fork(frame),
2114fda81ceSLoGin             #[cfg(target_arch = "x86_64")]
2121496ba7bSLoGin             SYS_VFORK => Self::vfork(frame),
2131496ba7bSLoGin 
214ab5c8ca4Slogin             SYS_BRK => {
21540fe15e0SLoGin                 let new_brk = VirtAddr::new(args[0]);
21640fe15e0SLoGin                 Self::brk(new_brk).map(|vaddr| vaddr.data())
217ab5c8ca4Slogin             }
218ab5c8ca4Slogin 
219ab5c8ca4Slogin             SYS_SBRK => {
220ab5c8ca4Slogin                 let increment = args[0] as isize;
2211496ba7bSLoGin                 Self::sbrk(increment).map(|vaddr: VirtAddr| vaddr.data())
222ab5c8ca4Slogin             }
223ab5c8ca4Slogin 
224ab5c8ca4Slogin             SYS_REBOOT => Self::reboot(),
225ab5c8ca4Slogin 
226ab5c8ca4Slogin             SYS_CHDIR => {
22782df0a13Shmt                 let r = args[0] as *const u8;
2289e9ffedfSPlucky923                 Self::chdir(r)
229ab5c8ca4Slogin             }
230ab5c8ca4Slogin 
2314fda81ceSLoGin             #[allow(unreachable_patterns)]
2324fda81ceSLoGin             SYS_GETDENTS64 | SYS_GETDENTS => {
233ab5c8ca4Slogin                 let fd = args[0] as i32;
23434e6d6c8Syuyi2439 
235ab5c8ca4Slogin                 let buf_vaddr = args[1];
236ab5c8ca4Slogin                 let len = args[2];
237f5df0e79SLoGin                 let virt_addr: VirtAddr = VirtAddr::new(buf_vaddr);
238ab5c8ca4Slogin                 // 判断缓冲区是否来自用户态,进行权限校验
239b5b571e0SLoGin                 let res = if frame.is_from_user() && verify_area(virt_addr, len).is_err() {
240ab5c8ca4Slogin                     // 来自用户态,而buffer在内核态,这样的操作不被允许
241ab5c8ca4Slogin                     Err(SystemError::EPERM)
242ab5c8ca4Slogin                 } else if buf_vaddr == 0 {
243ab5c8ca4Slogin                     Err(SystemError::EFAULT)
244ab5c8ca4Slogin                 } else {
245ab5c8ca4Slogin                     let buf: &mut [u8] = unsafe {
246ab5c8ca4Slogin                         core::slice::from_raw_parts_mut::<'static, u8>(buf_vaddr as *mut u8, len)
247ab5c8ca4Slogin                     };
248ab5c8ca4Slogin                     Self::getdents(fd, buf)
249ab5c8ca4Slogin                 };
250ab5c8ca4Slogin 
251ab5c8ca4Slogin                 res
252ab5c8ca4Slogin             }
253ab5c8ca4Slogin 
254ab5c8ca4Slogin             SYS_EXECVE => {
255ab5c8ca4Slogin                 let path_ptr = args[0];
256ab5c8ca4Slogin                 let argv_ptr = args[1];
257ab5c8ca4Slogin                 let env_ptr = args[2];
258be63f3b2Shoumkh                 let virt_path_ptr = VirtAddr::new(path_ptr);
259be63f3b2Shoumkh                 let virt_argv_ptr = VirtAddr::new(argv_ptr);
260be63f3b2Shoumkh                 let virt_env_ptr = VirtAddr::new(env_ptr);
261ab5c8ca4Slogin                 // 权限校验
262b5b571e0SLoGin                 if frame.is_from_user()
263b5b571e0SLoGin                     && (verify_area(virt_path_ptr, MAX_PATHLEN).is_err()
264be63f3b2Shoumkh                         || verify_area(virt_argv_ptr, PAGE_4K_SIZE as usize).is_err())
265be63f3b2Shoumkh                     || verify_area(virt_env_ptr, PAGE_4K_SIZE as usize).is_err()
266ab5c8ca4Slogin                 {
267ab5c8ca4Slogin                     Err(SystemError::EFAULT)
268ab5c8ca4Slogin                 } else {
269ab5c8ca4Slogin                     Self::execve(
2701496ba7bSLoGin                         path_ptr as *const u8,
2711496ba7bSLoGin                         argv_ptr as *const *const u8,
2721496ba7bSLoGin                         env_ptr as *const *const u8,
2731496ba7bSLoGin                         frame,
274ab5c8ca4Slogin                     )
2751496ba7bSLoGin                     .map(|_| 0)
276ab5c8ca4Slogin                 }
277ab5c8ca4Slogin             }
278ab5c8ca4Slogin             SYS_WAIT4 => {
279bf4a4899SLoGin                 let pid = args[0] as i32;
2801496ba7bSLoGin                 let wstatus = args[1] as *mut i32;
281ab5c8ca4Slogin                 let options = args[2] as c_int;
282ab5c8ca4Slogin                 let rusage = args[3] as *mut c_void;
283ab5c8ca4Slogin                 // 权限校验
284ab5c8ca4Slogin                 // todo: 引入rusage之后,更正以下权限校验代码中,rusage的大小
285bf4a4899SLoGin                 Self::wait4(pid.into(), wstatus, options, rusage)
286ab5c8ca4Slogin             }
287ab5c8ca4Slogin 
288ab5c8ca4Slogin             SYS_EXIT => {
289ab5c8ca4Slogin                 let exit_code = args[0];
290ab5c8ca4Slogin                 Self::exit(exit_code)
291ab5c8ca4Slogin             }
2924fda81ceSLoGin             #[cfg(target_arch = "x86_64")]
293ab5c8ca4Slogin             SYS_MKDIR => {
29482df0a13Shmt                 let path = args[0] as *const u8;
295ab5c8ca4Slogin                 let mode = args[1];
296ab5c8ca4Slogin 
29782df0a13Shmt                 Self::mkdir(path, mode)
298ab5c8ca4Slogin             }
299ab5c8ca4Slogin 
300ab5c8ca4Slogin             SYS_NANOSLEEP => {
3016fc066acSJomo                 let req = args[0] as *const PosixTimeSpec;
3026fc066acSJomo                 let rem = args[1] as *mut PosixTimeSpec;
303be63f3b2Shoumkh                 let virt_req = VirtAddr::new(req as usize);
304be63f3b2Shoumkh                 let virt_rem = VirtAddr::new(rem as usize);
305b5b571e0SLoGin                 if frame.is_from_user()
3066fc066acSJomo                     && (verify_area(virt_req, core::mem::size_of::<PosixTimeSpec>()).is_err()
3076fc066acSJomo                         || verify_area(virt_rem, core::mem::size_of::<PosixTimeSpec>()).is_err())
308ab5c8ca4Slogin                 {
309ab5c8ca4Slogin                     Err(SystemError::EFAULT)
310ab5c8ca4Slogin                 } else {
311ab5c8ca4Slogin                     Self::nanosleep(req, rem)
312ab5c8ca4Slogin                 }
313ab5c8ca4Slogin             }
314ab5c8ca4Slogin 
315ab5c8ca4Slogin             SYS_CLOCK => Self::clock(),
3164fda81ceSLoGin             #[cfg(target_arch = "x86_64")]
317ab5c8ca4Slogin             SYS_PIPE => {
31822c9db31Shanjiezhou                 let pipefd: *mut i32 = args[0] as *mut c_int;
3190fb515b0SLoGin                 if pipefd.is_null() {
3200fb515b0SLoGin                     Err(SystemError::EFAULT)
3210fb515b0SLoGin                 } else {
3220fb515b0SLoGin                     Self::pipe2(pipefd, FileMode::empty())
3230fb515b0SLoGin                 }
3240fb515b0SLoGin             }
3254ad52e57S裕依2439 
3260fb515b0SLoGin             SYS_PIPE2 => {
3270fb515b0SLoGin                 let pipefd: *mut i32 = args[0] as *mut c_int;
32822c9db31Shanjiezhou                 let arg1 = args[1];
32922c9db31Shanjiezhou                 if pipefd.is_null() {
33022c9db31Shanjiezhou                     Err(SystemError::EFAULT)
33122c9db31Shanjiezhou                 } else {
33222c9db31Shanjiezhou                     let flags = FileMode::from_bits_truncate(arg1 as u32);
33322c9db31Shanjiezhou                     Self::pipe2(pipefd, flags)
334ab5c8ca4Slogin                 }
335ab5c8ca4Slogin             }
336ab5c8ca4Slogin 
3374fda81ceSLoGin             SYS_UNLINKAT => {
338ab5c8ca4Slogin                 let dirfd = args[0] as i32;
33982df0a13Shmt                 let path = args[1] as *const u8;
340ab5c8ca4Slogin                 let flags = args[2] as u32;
34182df0a13Shmt                 Self::unlinkat(dirfd, path, flags)
342ab5c8ca4Slogin             }
343bf4a4899SLoGin 
3444fda81ceSLoGin             #[cfg(target_arch = "x86_64")]
3455eeefb8cSChenzx             SYS_RMDIR => {
34682df0a13Shmt                 let path = args[0] as *const u8;
34782df0a13Shmt                 Self::rmdir(path)
3485eeefb8cSChenzx             }
3495eeefb8cSChenzx 
3509b96c5b5SLoGin             #[cfg(target_arch = "x86_64")]
3514695947eSChenzx             SYS_LINK => {
3524695947eSChenzx                 let old = args[0] as *const u8;
3534695947eSChenzx                 let new = args[1] as *const u8;
3544695947eSChenzx                 return Self::link(old, new);
3554695947eSChenzx             }
3564695947eSChenzx 
3574695947eSChenzx             SYS_LINKAT => {
3584695947eSChenzx                 let oldfd = args[0] as i32;
3594695947eSChenzx                 let old = args[1] as *const u8;
3604695947eSChenzx                 let newfd = args[2] as i32;
3614695947eSChenzx                 let new = args[3] as *const u8;
3624695947eSChenzx                 let flags = args[4] as i32;
3634695947eSChenzx                 return Self::linkat(oldfd, old, newfd, new, flags);
3644695947eSChenzx             }
3654695947eSChenzx 
3665eeefb8cSChenzx             #[cfg(target_arch = "x86_64")]
367bf4a4899SLoGin             SYS_UNLINK => {
36882df0a13Shmt                 let path = args[0] as *const u8;
36982df0a13Shmt                 Self::unlink(path)
370bf4a4899SLoGin             }
371ab5c8ca4Slogin             SYS_KILL => {
3721496ba7bSLoGin                 let pid = Pid::new(args[0]);
373ab5c8ca4Slogin                 let sig = args[1] as c_int;
3743c82aa56SChiichen                 // kdebug!("KILL SYSCALL RECEIVED");
375ab5c8ca4Slogin                 Self::kill(pid, sig)
376ab5c8ca4Slogin             }
377ab5c8ca4Slogin 
3784fda81ceSLoGin             SYS_RT_SIGACTION => {
379ab5c8ca4Slogin                 let sig = args[0] as c_int;
380ab5c8ca4Slogin                 let act = args[1];
381ab5c8ca4Slogin                 let old_act = args[2];
382b5b571e0SLoGin                 Self::sigaction(sig, act, old_act, frame.is_from_user())
383ab5c8ca4Slogin             }
384ab5c8ca4Slogin 
3851496ba7bSLoGin             SYS_GETPID => Self::getpid().map(|pid| pid.into()),
386ab5c8ca4Slogin 
387f0c87a89SGnoCiYeH             SYS_SCHED => {
388f0c87a89SGnoCiYeH                 kwarn!("syscall sched");
389f0c87a89SGnoCiYeH                 schedule(SchedMode::SM_NONE);
390f0c87a89SGnoCiYeH                 Ok(0)
391f0c87a89SGnoCiYeH             }
392ab5c8ca4Slogin             SYS_DUP => {
393ab5c8ca4Slogin                 let oldfd: i32 = args[0] as c_int;
394ab5c8ca4Slogin                 Self::dup(oldfd)
395ab5c8ca4Slogin             }
3964fda81ceSLoGin 
3974fda81ceSLoGin             #[cfg(target_arch = "x86_64")]
398ab5c8ca4Slogin             SYS_DUP2 => {
399ab5c8ca4Slogin                 let oldfd: i32 = args[0] as c_int;
400ab5c8ca4Slogin                 let newfd: i32 = args[1] as c_int;
401ab5c8ca4Slogin                 Self::dup2(oldfd, newfd)
402ab5c8ca4Slogin             }
403ab5c8ca4Slogin 
404ab5c8ca4Slogin             SYS_SOCKET => Self::socket(args[0], args[1], args[2]),
405ab5c8ca4Slogin             SYS_SETSOCKOPT => {
406ab5c8ca4Slogin                 let optval = args[3] as *const u8;
407b5b571e0SLoGin                 let optlen = args[4];
408be63f3b2Shoumkh                 let virt_optval = VirtAddr::new(optval as usize);
409ab5c8ca4Slogin                 // 验证optval的地址是否合法
410b5b571e0SLoGin                 if verify_area(virt_optval, optlen).is_err() {
411ab5c8ca4Slogin                     // 地址空间超出了用户空间的范围,不合法
412ab5c8ca4Slogin                     Err(SystemError::EFAULT)
413ab5c8ca4Slogin                 } else {
414ab5c8ca4Slogin                     let data: &[u8] = unsafe { core::slice::from_raw_parts(optval, optlen) };
415ab5c8ca4Slogin                     Self::setsockopt(args[0], args[1], args[2], data)
416ab5c8ca4Slogin                 }
417ab5c8ca4Slogin             }
418ab5c8ca4Slogin             SYS_GETSOCKOPT => {
419ab5c8ca4Slogin                 let optval = args[3] as *mut u8;
420ab5c8ca4Slogin                 let optlen = args[4] as *mut usize;
421be63f3b2Shoumkh                 let virt_optval = VirtAddr::new(optval as usize);
422be63f3b2Shoumkh                 let virt_optlen = VirtAddr::new(optlen as usize);
423ab5c8ca4Slogin                 let security_check = || {
424ab5c8ca4Slogin                     // 验证optval的地址是否合法
425be63f3b2Shoumkh                     if verify_area(virt_optval, PAGE_4K_SIZE as usize).is_err() {
426ab5c8ca4Slogin                         // 地址空间超出了用户空间的范围,不合法
427ab5c8ca4Slogin                         return Err(SystemError::EFAULT);
428ab5c8ca4Slogin                     }
429ab5c8ca4Slogin 
430ab5c8ca4Slogin                     // 验证optlen的地址是否合法
431b5b571e0SLoGin                     if verify_area(virt_optlen, core::mem::size_of::<u32>()).is_err() {
432ab5c8ca4Slogin                         // 地址空间超出了用户空间的范围,不合法
433ab5c8ca4Slogin                         return Err(SystemError::EFAULT);
434ab5c8ca4Slogin                     }
435ab5c8ca4Slogin                     return Ok(());
436ab5c8ca4Slogin                 };
437ab5c8ca4Slogin                 let r = security_check();
438b5b571e0SLoGin                 if let Err(e) = r {
439b5b571e0SLoGin                     Err(e)
440ab5c8ca4Slogin                 } else {
441ab5c8ca4Slogin                     Self::getsockopt(args[0], args[1], args[2], optval, optlen as *mut u32)
442ab5c8ca4Slogin                 }
443ab5c8ca4Slogin             }
444ab5c8ca4Slogin 
445ab5c8ca4Slogin             SYS_CONNECT => {
446ab5c8ca4Slogin                 let addr = args[1] as *const SockAddr;
447b5b571e0SLoGin                 let addrlen = args[2];
448be63f3b2Shoumkh                 let virt_addr = VirtAddr::new(addr as usize);
449ab5c8ca4Slogin                 // 验证addr的地址是否合法
450b5b571e0SLoGin                 if verify_area(virt_addr, addrlen).is_err() {
451ab5c8ca4Slogin                     // 地址空间超出了用户空间的范围,不合法
452ab5c8ca4Slogin                     Err(SystemError::EFAULT)
453ab5c8ca4Slogin                 } else {
454ab5c8ca4Slogin                     Self::connect(args[0], addr, addrlen)
455ab5c8ca4Slogin                 }
456ab5c8ca4Slogin             }
457ab5c8ca4Slogin             SYS_BIND => {
458ab5c8ca4Slogin                 let addr = args[1] as *const SockAddr;
459b5b571e0SLoGin                 let addrlen = args[2];
460be63f3b2Shoumkh                 let virt_addr = VirtAddr::new(addr as usize);
461ab5c8ca4Slogin                 // 验证addr的地址是否合法
462b5b571e0SLoGin                 if verify_area(virt_addr, addrlen).is_err() {
463ab5c8ca4Slogin                     // 地址空间超出了用户空间的范围,不合法
464ab5c8ca4Slogin                     Err(SystemError::EFAULT)
465ab5c8ca4Slogin                 } else {
466ab5c8ca4Slogin                     Self::bind(args[0], addr, addrlen)
467ab5c8ca4Slogin                 }
468ab5c8ca4Slogin             }
469ab5c8ca4Slogin 
470ab5c8ca4Slogin             SYS_SENDTO => {
471ab5c8ca4Slogin                 let buf = args[1] as *const u8;
472b5b571e0SLoGin                 let len = args[2];
473ab5c8ca4Slogin                 let flags = args[3] as u32;
474ab5c8ca4Slogin                 let addr = args[4] as *const SockAddr;
475b5b571e0SLoGin                 let addrlen = args[5];
476be63f3b2Shoumkh                 let virt_buf = VirtAddr::new(buf as usize);
477be63f3b2Shoumkh                 let virt_addr = VirtAddr::new(addr as usize);
478ab5c8ca4Slogin                 // 验证buf的地址是否合法
479b5b571e0SLoGin                 if verify_area(virt_buf, len).is_err() || verify_area(virt_addr, addrlen).is_err() {
480ab5c8ca4Slogin                     // 地址空间超出了用户空间的范围,不合法
481ab5c8ca4Slogin                     Err(SystemError::EFAULT)
482ab5c8ca4Slogin                 } else {
483ab5c8ca4Slogin                     let data: &[u8] = unsafe { core::slice::from_raw_parts(buf, len) };
484ab5c8ca4Slogin                     Self::sendto(args[0], data, flags, addr, addrlen)
485ab5c8ca4Slogin                 }
486ab5c8ca4Slogin             }
487ab5c8ca4Slogin 
488ab5c8ca4Slogin             SYS_RECVFROM => {
489ab5c8ca4Slogin                 let buf = args[1] as *mut u8;
490b5b571e0SLoGin                 let len = args[2];
491ab5c8ca4Slogin                 let flags = args[3] as u32;
492ab5c8ca4Slogin                 let addr = args[4] as *mut SockAddr;
493ab5c8ca4Slogin                 let addrlen = args[5] as *mut usize;
494be63f3b2Shoumkh                 let virt_buf = VirtAddr::new(buf as usize);
495be63f3b2Shoumkh                 let virt_addrlen = VirtAddr::new(addrlen as usize);
496be63f3b2Shoumkh                 let virt_addr = VirtAddr::new(addr as usize);
497ab5c8ca4Slogin                 let security_check = || {
498ab5c8ca4Slogin                     // 验证buf的地址是否合法
499840045afSLoGin                     if verify_area(virt_buf, len).is_err() {
500ab5c8ca4Slogin                         // 地址空间超出了用户空间的范围,不合法
501ab5c8ca4Slogin                         return Err(SystemError::EFAULT);
502ab5c8ca4Slogin                     }
503ab5c8ca4Slogin 
504ab5c8ca4Slogin                     // 验证addrlen的地址是否合法
505840045afSLoGin                     if verify_area(virt_addrlen, core::mem::size_of::<u32>()).is_err() {
506ab5c8ca4Slogin                         // 地址空间超出了用户空间的范围,不合法
507ab5c8ca4Slogin                         return Err(SystemError::EFAULT);
508ab5c8ca4Slogin                     }
509ab5c8ca4Slogin 
510840045afSLoGin                     if verify_area(virt_addr, core::mem::size_of::<SockAddr>()).is_err() {
511ab5c8ca4Slogin                         // 地址空间超出了用户空间的范围,不合法
512ab5c8ca4Slogin                         return Err(SystemError::EFAULT);
513ab5c8ca4Slogin                     }
514ab5c8ca4Slogin                     return Ok(());
515ab5c8ca4Slogin                 };
516ab5c8ca4Slogin                 let r = security_check();
517b5b571e0SLoGin                 if let Err(e) = r {
518b5b571e0SLoGin                     Err(e)
519ab5c8ca4Slogin                 } else {
520ab5c8ca4Slogin                     let buf = unsafe { core::slice::from_raw_parts_mut(buf, len) };
521ab5c8ca4Slogin                     Self::recvfrom(args[0], buf, flags, addr, addrlen as *mut u32)
522ab5c8ca4Slogin                 }
523ab5c8ca4Slogin             }
524ab5c8ca4Slogin 
525ab5c8ca4Slogin             SYS_RECVMSG => {
5264ad52e57S裕依2439                 let msg = args[1] as *mut MsgHdr;
527ab5c8ca4Slogin                 let flags = args[2] as u32;
5284ad52e57S裕依2439 
529b5b571e0SLoGin                 let mut user_buffer_writer = UserBufferWriter::new(
530b5b571e0SLoGin                     msg,
531b5b571e0SLoGin                     core::mem::size_of::<MsgHdr>(),
532b5b571e0SLoGin                     frame.is_from_user(),
533b5b571e0SLoGin                 )?;
5344ad52e57S裕依2439                 let buffer = user_buffer_writer.buffer::<MsgHdr>(0)?;
5354ad52e57S裕依2439 
5364537ffb7SChiichen                 let msg = &mut buffer[0];
537ab5c8ca4Slogin                 Self::recvmsg(args[0], msg, flags)
538ab5c8ca4Slogin             }
539ab5c8ca4Slogin 
540ab5c8ca4Slogin             SYS_LISTEN => Self::listen(args[0], args[1]),
541ab5c8ca4Slogin             SYS_SHUTDOWN => Self::shutdown(args[0], args[1]),
542ab5c8ca4Slogin             SYS_ACCEPT => Self::accept(args[0], args[1] as *mut SockAddr, args[2] as *mut u32),
543c47fe904SLoGin             SYS_ACCEPT4 => Self::accept4(
544c47fe904SLoGin                 args[0],
545c47fe904SLoGin                 args[1] as *mut SockAddr,
546c47fe904SLoGin                 args[2] as *mut u32,
547c47fe904SLoGin                 args[3] as u32,
548c47fe904SLoGin             ),
549ab5c8ca4Slogin             SYS_GETSOCKNAME => {
550ab5c8ca4Slogin                 Self::getsockname(args[0], args[1] as *mut SockAddr, args[2] as *mut u32)
551ab5c8ca4Slogin             }
552ab5c8ca4Slogin             SYS_GETPEERNAME => {
553ab5c8ca4Slogin                 Self::getpeername(args[0], args[1] as *mut SockAddr, args[2] as *mut u32)
554ab5c8ca4Slogin             }
55536fd0130Shoumkh             SYS_GETTIMEOFDAY => {
55636fd0130Shoumkh                 let timeval = args[0] as *mut PosixTimeval;
55740fe15e0SLoGin                 let timezone_ptr = args[1] as *mut PosixTimeZone;
55840fe15e0SLoGin                 Self::gettimeofday(timeval, timezone_ptr)
5594537ffb7SChiichen             }
56040fe15e0SLoGin             SYS_MMAP => {
56140fe15e0SLoGin                 let len = page_align_up(args[1]);
562840045afSLoGin                 let virt_addr = VirtAddr::new(args[0]);
563840045afSLoGin                 if verify_area(virt_addr, len).is_err() {
56440fe15e0SLoGin                     Err(SystemError::EFAULT)
56540fe15e0SLoGin                 } else {
56640fe15e0SLoGin                     Self::mmap(
56740fe15e0SLoGin                         VirtAddr::new(args[0]),
56840fe15e0SLoGin                         len,
56940fe15e0SLoGin                         args[2],
57040fe15e0SLoGin                         args[3],
57140fe15e0SLoGin                         args[4] as i32,
57240fe15e0SLoGin                         args[5],
57340fe15e0SLoGin                     )
57440fe15e0SLoGin                 }
57540fe15e0SLoGin             }
5764cfa009bSJomo             SYS_MREMAP => {
5774cfa009bSJomo                 let old_vaddr = VirtAddr::new(args[0]);
5784cfa009bSJomo                 let old_len = args[1];
5794cfa009bSJomo                 let new_len = args[2];
5804cfa009bSJomo                 let mremap_flags = MremapFlags::from_bits_truncate(args[3] as u8);
5814cfa009bSJomo                 let new_vaddr = VirtAddr::new(args[4]);
5824cfa009bSJomo 
5834cfa009bSJomo                 Self::mremap(old_vaddr, old_len, new_len, mremap_flags, new_vaddr)
5844cfa009bSJomo             }
58540fe15e0SLoGin             SYS_MUNMAP => {
58640fe15e0SLoGin                 let addr = args[0];
58740fe15e0SLoGin                 let len = page_align_up(args[1]);
588971462beSGnoCiYeH                 if addr & (MMArch::PAGE_SIZE - 1) != 0 {
58940fe15e0SLoGin                     // The addr argument is not a multiple of the page size
59040fe15e0SLoGin                     Err(SystemError::EINVAL)
59140fe15e0SLoGin                 } else {
59240fe15e0SLoGin                     Self::munmap(VirtAddr::new(addr), len)
59340fe15e0SLoGin                 }
59440fe15e0SLoGin             }
59540fe15e0SLoGin             SYS_MPROTECT => {
59640fe15e0SLoGin                 let addr = args[0];
59740fe15e0SLoGin                 let len = page_align_up(args[1]);
598971462beSGnoCiYeH                 if addr & (MMArch::PAGE_SIZE - 1) != 0 {
59940fe15e0SLoGin                     // The addr argument is not a multiple of the page size
60040fe15e0SLoGin                     Err(SystemError::EINVAL)
60140fe15e0SLoGin                 } else {
60240fe15e0SLoGin                     Self::mprotect(VirtAddr::new(addr), len, args[2])
60340fe15e0SLoGin                 }
60440fe15e0SLoGin             }
60540fe15e0SLoGin 
6061496ba7bSLoGin             SYS_GETCWD => {
6071496ba7bSLoGin                 let buf = args[0] as *mut u8;
608840045afSLoGin                 let size = args[1];
6091496ba7bSLoGin                 let security_check = || {
6101496ba7bSLoGin                     verify_area(VirtAddr::new(buf as usize), size)?;
6111496ba7bSLoGin                     return Ok(());
6121496ba7bSLoGin                 };
6131496ba7bSLoGin                 let r = security_check();
614b5b571e0SLoGin                 if let Err(e) = r {
615b5b571e0SLoGin                     Err(e)
6161496ba7bSLoGin                 } else {
6171496ba7bSLoGin                     let buf = unsafe { core::slice::from_raw_parts_mut(buf, size) };
6181496ba7bSLoGin                     Self::getcwd(buf).map(|ptr| ptr.data())
6191496ba7bSLoGin                 }
6201496ba7bSLoGin             }
6211496ba7bSLoGin 
6221496ba7bSLoGin             SYS_GETPGID => Self::getpgid(Pid::new(args[0])).map(|pid| pid.into()),
6231496ba7bSLoGin 
6241496ba7bSLoGin             SYS_GETPPID => Self::getppid().map(|pid| pid.into()),
625be63f3b2Shoumkh             SYS_FSTAT => {
626be63f3b2Shoumkh                 let fd = args[0] as i32;
627b5b571e0SLoGin                 let kstat: *mut PosixKstat = args[1] as *mut PosixKstat;
628be63f3b2Shoumkh                 let vaddr = VirtAddr::new(kstat as usize);
629be63f3b2Shoumkh                 // FIXME 由于c中的verify_area与rust中的verify_area重名,所以在引入时加了前缀区分
630be63f3b2Shoumkh                 // TODO 应该将用了c版本的verify_area都改为rust的verify_area
631be63f3b2Shoumkh                 match verify_area(vaddr, core::mem::size_of::<PosixKstat>()) {
632be63f3b2Shoumkh                     Ok(_) => Self::fstat(fd, kstat),
633be63f3b2Shoumkh                     Err(e) => Err(e),
634be63f3b2Shoumkh                 }
635be63f3b2Shoumkh             }
636be63f3b2Shoumkh 
6376d81180bSLoGin             SYS_FCNTL => {
6386d81180bSLoGin                 let fd = args[0] as i32;
6396d81180bSLoGin                 let cmd: Option<FcntlCommand> =
6406d81180bSLoGin                     <FcntlCommand as FromPrimitive>::from_u32(args[1] as u32);
6416d81180bSLoGin                 let arg = args[2] as i32;
6426d81180bSLoGin                 let res = if let Some(cmd) = cmd {
6436d81180bSLoGin                     Self::fcntl(fd, cmd, arg)
6446d81180bSLoGin                 } else {
6456d81180bSLoGin                     Err(SystemError::EINVAL)
6466d81180bSLoGin                 };
6476d81180bSLoGin 
6486d81180bSLoGin                 // kdebug!("FCNTL: fd: {}, cmd: {:?}, arg: {}, res: {:?}", fd, cmd, arg, res);
6496d81180bSLoGin                 res
6506d81180bSLoGin             }
6516d81180bSLoGin 
6526d81180bSLoGin             SYS_FTRUNCATE => {
6536d81180bSLoGin                 let fd = args[0] as i32;
654840045afSLoGin                 let len = args[1];
6556d81180bSLoGin                 let res = Self::ftruncate(fd, len);
6566d81180bSLoGin                 // kdebug!("FTRUNCATE: fd: {}, len: {}, res: {:?}", fd, len, res);
6576d81180bSLoGin                 res
6586d81180bSLoGin             }
6596d81180bSLoGin 
6604fda81ceSLoGin             #[cfg(target_arch = "x86_64")]
6612dbef785SGnoCiYeH             SYS_MKNOD => {
6622dbef785SGnoCiYeH                 let path = args[0];
6632dbef785SGnoCiYeH                 let flags = args[1];
6642dbef785SGnoCiYeH                 let dev_t = args[2];
6652dbef785SGnoCiYeH                 let flags: ModeType = ModeType::from_bits_truncate(flags as u32);
66682df0a13Shmt                 Self::mknod(path as *const u8, flags, DeviceNumber::from(dev_t as u32))
6672dbef785SGnoCiYeH             }
6682dbef785SGnoCiYeH 
669971462beSGnoCiYeH             SYS_CLONE => {
670971462beSGnoCiYeH                 let parent_tid = VirtAddr::new(args[2]);
671971462beSGnoCiYeH                 let child_tid = VirtAddr::new(args[3]);
672971462beSGnoCiYeH 
673971462beSGnoCiYeH                 // 地址校验
674971462beSGnoCiYeH                 verify_area(parent_tid, core::mem::size_of::<i32>())?;
675971462beSGnoCiYeH                 verify_area(child_tid, core::mem::size_of::<i32>())?;
676971462beSGnoCiYeH 
677971462beSGnoCiYeH                 let mut clone_args = KernelCloneArgs::new();
678971462beSGnoCiYeH                 clone_args.flags = CloneFlags::from_bits_truncate(args[0] as u64);
679971462beSGnoCiYeH                 clone_args.stack = args[1];
680971462beSGnoCiYeH                 clone_args.parent_tid = parent_tid;
681971462beSGnoCiYeH                 clone_args.child_tid = child_tid;
682971462beSGnoCiYeH                 clone_args.tls = args[4];
683971462beSGnoCiYeH                 Self::clone(frame, clone_args)
684971462beSGnoCiYeH             }
685971462beSGnoCiYeH 
686971462beSGnoCiYeH             SYS_FUTEX => {
687971462beSGnoCiYeH                 let uaddr = VirtAddr::new(args[0]);
688971462beSGnoCiYeH                 let operation = FutexFlag::from_bits(args[1] as u32).ok_or(SystemError::ENOSYS)?;
689971462beSGnoCiYeH                 let val = args[2] as u32;
690971462beSGnoCiYeH                 let utime = args[3];
691971462beSGnoCiYeH                 let uaddr2 = VirtAddr::new(args[4]);
692971462beSGnoCiYeH                 let val3 = args[5] as u32;
693971462beSGnoCiYeH 
694971462beSGnoCiYeH                 let mut timespec = None;
695971462beSGnoCiYeH                 if utime != 0 && operation.contains(FutexFlag::FLAGS_HAS_TIMEOUT) {
696971462beSGnoCiYeH                     let reader = UserBufferReader::new(
6976fc066acSJomo                         utime as *const PosixTimeSpec,
6986fc066acSJomo                         core::mem::size_of::<PosixTimeSpec>(),
699971462beSGnoCiYeH                         true,
700971462beSGnoCiYeH                     )?;
701971462beSGnoCiYeH 
7026fc066acSJomo                     timespec = Some(*reader.read_one_from_user::<PosixTimeSpec>(0)?);
703971462beSGnoCiYeH                 }
704971462beSGnoCiYeH 
705971462beSGnoCiYeH                 Self::do_futex(uaddr, operation, val, timespec, uaddr2, utime as u32, val3)
706971462beSGnoCiYeH             }
707971462beSGnoCiYeH 
70806560afaShmt             SYS_SET_ROBUST_LIST => {
70906560afaShmt                 let head = args[0];
71006560afaShmt                 let head_uaddr = VirtAddr::new(head);
71106560afaShmt                 let len = args[1];
71206560afaShmt 
71306560afaShmt                 let ret = Self::set_robust_list(head_uaddr, len);
71406560afaShmt                 return ret;
71506560afaShmt             }
71606560afaShmt 
71706560afaShmt             SYS_GET_ROBUST_LIST => {
71806560afaShmt                 let pid = args[0];
71906560afaShmt                 let head = args[1];
72006560afaShmt                 let head_uaddr = VirtAddr::new(head);
72106560afaShmt                 let len_ptr = args[2];
72206560afaShmt                 let len_ptr_uaddr = VirtAddr::new(len_ptr);
72306560afaShmt 
72406560afaShmt                 let ret = Self::get_robust_list(pid, head_uaddr, len_ptr_uaddr);
72506560afaShmt                 return ret;
72606560afaShmt             }
72706560afaShmt 
728bf4a4899SLoGin             SYS_READV => Self::readv(args[0] as i32, args[1], args[2]),
729971462beSGnoCiYeH             SYS_WRITEV => Self::writev(args[0] as i32, args[1], args[2]),
730971462beSGnoCiYeH 
7314fda81ceSLoGin             SYS_SET_TID_ADDRESS => Self::set_tid_address(args[0]),
732971462beSGnoCiYeH 
7334fda81ceSLoGin             #[cfg(target_arch = "x86_64")]
7344fda81ceSLoGin             SYS_LSTAT => {
73582df0a13Shmt                 let path = args[0] as *const u8;
736971462beSGnoCiYeH                 let kstat = args[1] as *mut PosixKstat;
73782df0a13Shmt                 Self::lstat(path, kstat)
7384fda81ceSLoGin             }
7394fda81ceSLoGin 
7404fda81ceSLoGin             #[cfg(target_arch = "x86_64")]
7414fda81ceSLoGin             SYS_STAT => {
74282df0a13Shmt                 let path = args[0] as *const u8;
7434fda81ceSLoGin                 let kstat = args[1] as *mut PosixKstat;
74482df0a13Shmt                 Self::stat(path, kstat)
745971462beSGnoCiYeH             }
746971462beSGnoCiYeH 
747597ecc08STTaq             SYS_STATFS => {
748597ecc08STTaq                 let path = args[0] as *const u8;
749597ecc08STTaq                 let statfs = args[1] as *mut PosixStatfs;
750597ecc08STTaq                 Self::statfs(path, statfs)
751597ecc08STTaq             }
752597ecc08STTaq 
753597ecc08STTaq             SYS_FSTATFS => {
754597ecc08STTaq                 let fd = args[0] as i32;
755597ecc08STTaq                 let statfs = args[1] as *mut PosixStatfs;
756597ecc08STTaq                 Self::fstatfs(fd, statfs)
757597ecc08STTaq             }
758597ecc08STTaq 
759b4eb05a1STTaq             SYS_STATX => {
760b4eb05a1STTaq                 let fd = args[0] as i32;
761b4eb05a1STTaq                 let path = args[1] as *const u8;
762b4eb05a1STTaq                 let flags = args[2] as u32;
763b4eb05a1STTaq                 let mask = args[3] as u32;
764b4eb05a1STTaq                 let kstat = args[4] as *mut PosixStatx;
765b4eb05a1STTaq 
766b4eb05a1STTaq                 Self::do_statx(fd, path, flags, mask, kstat)
767b4eb05a1STTaq             }
768b4eb05a1STTaq 
76970f159a3SLoGin             #[cfg(target_arch = "x86_64")]
77040609970SGnoCiYeH             SYS_EPOLL_CREATE => Self::epoll_create(args[0] as i32),
77140609970SGnoCiYeH             SYS_EPOLL_CREATE1 => Self::epoll_create1(args[0]),
77240609970SGnoCiYeH 
77340609970SGnoCiYeH             SYS_EPOLL_CTL => Self::epoll_ctl(
77440609970SGnoCiYeH                 args[0] as i32,
77540609970SGnoCiYeH                 args[1],
77640609970SGnoCiYeH                 args[2] as i32,
77740609970SGnoCiYeH                 VirtAddr::new(args[3]),
77840609970SGnoCiYeH             ),
77940609970SGnoCiYeH 
78070f159a3SLoGin             #[cfg(target_arch = "x86_64")]
78140609970SGnoCiYeH             SYS_EPOLL_WAIT => Self::epoll_wait(
78240609970SGnoCiYeH                 args[0] as i32,
78340609970SGnoCiYeH                 VirtAddr::new(args[1]),
78440609970SGnoCiYeH                 args[2] as i32,
78540609970SGnoCiYeH                 args[3] as i32,
78640609970SGnoCiYeH             ),
78740609970SGnoCiYeH 
78840609970SGnoCiYeH             SYS_EPOLL_PWAIT => {
78940609970SGnoCiYeH                 let epfd = args[0] as i32;
79040609970SGnoCiYeH                 let epoll_event = VirtAddr::new(args[1]);
79140609970SGnoCiYeH                 let max_events = args[2] as i32;
79240609970SGnoCiYeH                 let timespec = args[3] as i32;
79340609970SGnoCiYeH                 let sigmask_addr = args[4] as *mut SigSet;
79440609970SGnoCiYeH 
79540609970SGnoCiYeH                 if sigmask_addr.is_null() {
79640609970SGnoCiYeH                     return Self::epoll_wait(epfd, epoll_event, max_events, timespec);
79740609970SGnoCiYeH                 }
79840609970SGnoCiYeH                 let sigmask_reader =
79940609970SGnoCiYeH                     UserBufferReader::new(sigmask_addr, core::mem::size_of::<SigSet>(), true)?;
800840045afSLoGin                 let mut sigmask = *sigmask_reader.read_one_from_user::<SigSet>(0)?;
80140609970SGnoCiYeH 
80240609970SGnoCiYeH                 Self::epoll_pwait(
80340609970SGnoCiYeH                     args[0] as i32,
80440609970SGnoCiYeH                     VirtAddr::new(args[1]),
80540609970SGnoCiYeH                     args[2] as i32,
80640609970SGnoCiYeH                     args[3] as i32,
80740609970SGnoCiYeH                     &mut sigmask,
80840609970SGnoCiYeH                 )
80940609970SGnoCiYeH             }
81040609970SGnoCiYeH 
811971462beSGnoCiYeH             // 目前为了适配musl-libc,以下系统调用先这样写着
8124fda81ceSLoGin             SYS_GETRANDOM => {
813971462beSGnoCiYeH                 let flags = GRandFlags::from_bits(args[2] as u8).ok_or(SystemError::EINVAL)?;
814971462beSGnoCiYeH                 Self::get_random(args[0] as *mut u8, args[1], flags)
815971462beSGnoCiYeH             }
816971462beSGnoCiYeH 
8174fda81ceSLoGin             SYS_SOCKETPAIR => {
8184ad52e57S裕依2439                 let mut user_buffer_writer = UserBufferWriter::new(
8194ad52e57S裕依2439                     args[3] as *mut c_int,
8204ad52e57S裕依2439                     core::mem::size_of::<[c_int; 2]>(),
821b5b571e0SLoGin                     frame.is_from_user(),
8224ad52e57S裕依2439                 )?;
8234ad52e57S裕依2439                 let fds = user_buffer_writer.buffer::<i32>(0)?;
8244ad52e57S裕依2439                 Self::socketpair(args[0], args[1], args[2], fds)
825971462beSGnoCiYeH             }
826971462beSGnoCiYeH 
8274fda81ceSLoGin             #[cfg(target_arch = "x86_64")]
828971462beSGnoCiYeH             SYS_POLL => {
829971462beSGnoCiYeH                 kwarn!("SYS_POLL has not yet been implemented");
830971462beSGnoCiYeH                 Ok(0)
831971462beSGnoCiYeH             }
832971462beSGnoCiYeH 
83352da9a59SGnoCiYeH             SYS_SETPGID => {
83452da9a59SGnoCiYeH                 kwarn!("SYS_SETPGID has not yet been implemented");
83552da9a59SGnoCiYeH                 Ok(0)
83652da9a59SGnoCiYeH             }
83752da9a59SGnoCiYeH 
838971462beSGnoCiYeH             SYS_RT_SIGPROCMASK => {
839971462beSGnoCiYeH                 kwarn!("SYS_RT_SIGPROCMASK has not yet been implemented");
840971462beSGnoCiYeH                 Ok(0)
841971462beSGnoCiYeH             }
842971462beSGnoCiYeH 
843971462beSGnoCiYeH             SYS_TKILL => {
844971462beSGnoCiYeH                 kwarn!("SYS_TKILL has not yet been implemented");
845971462beSGnoCiYeH                 Ok(0)
846971462beSGnoCiYeH             }
847971462beSGnoCiYeH 
848971462beSGnoCiYeH             SYS_SIGALTSTACK => {
849971462beSGnoCiYeH                 kwarn!("SYS_SIGALTSTACK has not yet been implemented");
850971462beSGnoCiYeH                 Ok(0)
851971462beSGnoCiYeH             }
852971462beSGnoCiYeH 
85316033951SGnoCiYeH             SYS_EXIT_GROUP => {
85416033951SGnoCiYeH                 kwarn!("SYS_EXIT_GROUP has not yet been implemented");
85516033951SGnoCiYeH                 Ok(0)
85616033951SGnoCiYeH             }
85716033951SGnoCiYeH 
85804babc3fSMemoryShore             SYS_MADVISE => {
8596c7f966cSMemoryShore                 // 这个太吵了,总是打印,先注释掉
8606c7f966cSMemoryShore                 // kwarn!("SYS_MADVISE has not yet been implemented");
86104babc3fSMemoryShore                 Ok(0)
86204babc3fSMemoryShore             }
863393f6915SLoGin             SYS_GETTID => Self::gettid().map(|tid| tid.into()),
864840045afSLoGin             SYS_GETUID => Self::getuid(),
8658d72b68dSJomo 
86602e249f3SLoGin             SYS_SYSLOG => {
867840045afSLoGin                 let syslog_action_type = args[0];
8688d72b68dSJomo                 let buf_vaddr = args[1];
8698d72b68dSJomo                 let len = args[2];
870b5b571e0SLoGin                 let from_user = frame.is_from_user();
8718d72b68dSJomo                 let mut user_buffer_writer =
8728d72b68dSJomo                     UserBufferWriter::new(buf_vaddr as *mut u8, len, from_user)?;
8738d72b68dSJomo 
8748d72b68dSJomo                 let user_buf = user_buffer_writer.buffer(0)?;
875840045afSLoGin                 Self::do_syslog(syslog_action_type, user_buf, len)
87602e249f3SLoGin             }
8778d72b68dSJomo 
878840045afSLoGin             SYS_GETGID => Self::getgid(),
87902e249f3SLoGin             SYS_SETUID => {
88002e249f3SLoGin                 kwarn!("SYS_SETUID has not yet been implemented");
88102e249f3SLoGin                 Ok(0)
88202e249f3SLoGin             }
88302e249f3SLoGin             SYS_SETGID => {
88402e249f3SLoGin                 kwarn!("SYS_SETGID has not yet been implemented");
88502e249f3SLoGin                 Ok(0)
88602e249f3SLoGin             }
887dfe53cf0SGnoCiYeH             SYS_SETSID => {
888dfe53cf0SGnoCiYeH                 kwarn!("SYS_SETSID has not yet been implemented");
889dfe53cf0SGnoCiYeH                 Ok(0)
890dfe53cf0SGnoCiYeH             }
891840045afSLoGin             SYS_GETEUID => Self::geteuid(),
892840045afSLoGin             SYS_GETEGID => Self::getegid(),
893be8cdf4bSLoGin             SYS_GETRUSAGE => {
894be8cdf4bSLoGin                 let who = args[0] as c_int;
895be8cdf4bSLoGin                 let rusage = args[1] as *mut RUsage;
896be8cdf4bSLoGin                 Self::get_rusage(who, rusage)
897be8cdf4bSLoGin             }
89804babc3fSMemoryShore 
8994fda81ceSLoGin             #[cfg(target_arch = "x86_64")]
900709498caSLoGin             SYS_READLINK => {
901709498caSLoGin                 let path = args[0] as *const u8;
902709498caSLoGin                 let buf = args[1] as *mut u8;
903840045afSLoGin                 let bufsiz = args[2];
904709498caSLoGin                 Self::readlink(path, buf, bufsiz)
905709498caSLoGin             }
906709498caSLoGin 
9074fda81ceSLoGin             SYS_READLINKAT => {
908709498caSLoGin                 let dirfd = args[0] as i32;
90982df0a13Shmt                 let path = args[1] as *const u8;
910709498caSLoGin                 let buf = args[2] as *mut u8;
911840045afSLoGin                 let bufsiz = args[3];
91282df0a13Shmt                 Self::readlink_at(dirfd, path, buf, bufsiz)
913709498caSLoGin             }
914709498caSLoGin 
9150d9b7d92SLoGin             SYS_PRLIMIT64 => {
9160d9b7d92SLoGin                 let pid = args[0];
9170d9b7d92SLoGin                 let pid = Pid::new(pid);
9180d9b7d92SLoGin                 let resource = args[1];
9190d9b7d92SLoGin                 let new_limit = args[2] as *const RLimit64;
9200d9b7d92SLoGin                 let old_limit = args[3] as *mut RLimit64;
9210d9b7d92SLoGin 
9220d9b7d92SLoGin                 Self::prlimit64(pid, resource, new_limit, old_limit)
9230d9b7d92SLoGin             }
9240d9b7d92SLoGin 
9254fda81ceSLoGin             #[cfg(target_arch = "x86_64")]
9269b0abe6dSLoGin             SYS_ACCESS => {
9279b0abe6dSLoGin                 let pathname = args[0] as *const u8;
9289b0abe6dSLoGin                 let mode = args[1] as u32;
9299b0abe6dSLoGin                 Self::access(pathname, mode)
9309b0abe6dSLoGin             }
9319b0abe6dSLoGin 
9329b0abe6dSLoGin             SYS_FACCESSAT => {
9339b0abe6dSLoGin                 let dirfd = args[0] as i32;
9349b0abe6dSLoGin                 let pathname = args[1] as *const u8;
9359b0abe6dSLoGin                 let mode = args[2] as u32;
9369b0abe6dSLoGin                 Self::faccessat2(dirfd, pathname, mode, 0)
9379b0abe6dSLoGin             }
9389b0abe6dSLoGin 
9399b0abe6dSLoGin             SYS_FACCESSAT2 => {
9409b0abe6dSLoGin                 let dirfd = args[0] as i32;
9419b0abe6dSLoGin                 let pathname = args[1] as *const u8;
9429b0abe6dSLoGin                 let mode = args[2] as u32;
9439b0abe6dSLoGin                 let flags = args[3] as u32;
9449b0abe6dSLoGin                 Self::faccessat2(dirfd, pathname, mode, flags)
9459b0abe6dSLoGin             }
9469b0abe6dSLoGin 
947bf4a4899SLoGin             SYS_CLOCK_GETTIME => {
948bf4a4899SLoGin                 let clockid = args[0] as i32;
9496fc066acSJomo                 let timespec = args[1] as *mut PosixTimeSpec;
950bf4a4899SLoGin                 Self::clock_gettime(clockid, timespec)
951bf4a4899SLoGin             }
952bf4a4899SLoGin 
953bf4a4899SLoGin             SYS_SYSINFO => {
954bf4a4899SLoGin                 let info = args[0] as *mut SysInfo;
955bf4a4899SLoGin                 Self::sysinfo(info)
956bf4a4899SLoGin             }
957bf4a4899SLoGin 
958bf4a4899SLoGin             SYS_UMASK => {
959bf4a4899SLoGin                 let mask = args[0] as u32;
960bf4a4899SLoGin                 Self::umask(mask)
961bf4a4899SLoGin             }
962bf4a4899SLoGin 
96327b967a3S裕依             SYS_FCHOWN => {
96427b967a3S裕依                 kwarn!("SYS_FCHOWN has not yet been implemented");
96527b967a3S裕依                 Ok(0)
96627b967a3S裕依             }
96727b967a3S裕依 
96827b967a3S裕依             SYS_FSYNC => {
96927b967a3S裕依                 kwarn!("SYS_FSYNC has not yet been implemented");
97027b967a3S裕依                 Ok(0)
97127b967a3S裕依             }
97227b967a3S裕依 
973dfe53cf0SGnoCiYeH             SYS_RSEQ => {
974dfe53cf0SGnoCiYeH                 kwarn!("SYS_RSEQ has not yet been implemented");
975dfe53cf0SGnoCiYeH                 Ok(0)
976dfe53cf0SGnoCiYeH             }
977dfe53cf0SGnoCiYeH 
9784fda81ceSLoGin             #[cfg(target_arch = "x86_64")]
979bf4a4899SLoGin             SYS_CHMOD => {
980bf4a4899SLoGin                 let pathname = args[0] as *const u8;
981bf4a4899SLoGin                 let mode = args[1] as u32;
982bf4a4899SLoGin                 Self::chmod(pathname, mode)
983bf4a4899SLoGin             }
984bf4a4899SLoGin             SYS_FCHMOD => {
985bf4a4899SLoGin                 let fd = args[0] as i32;
986bf4a4899SLoGin                 let mode = args[1] as u32;
987bf4a4899SLoGin                 Self::fchmod(fd, mode)
988bf4a4899SLoGin             }
989bf4a4899SLoGin             SYS_FCHMODAT => {
990bf4a4899SLoGin                 let dirfd = args[0] as i32;
991bf4a4899SLoGin                 let pathname = args[1] as *const u8;
992bf4a4899SLoGin                 let mode = args[2] as u32;
993bf4a4899SLoGin                 Self::fchmodat(dirfd, pathname, mode)
994bf4a4899SLoGin             }
995bf4a4899SLoGin 
99602343d0bSLoGin             SYS_SCHED_GETAFFINITY => {
9979430523bSyuyi2439                 let pid = args[0] as i32;
9989430523bSyuyi2439                 let size = args[1];
9999430523bSyuyi2439                 let set_vaddr = args[2];
100002343d0bSLoGin 
10019430523bSyuyi2439                 let mut user_buffer_writer =
10029430523bSyuyi2439                     UserBufferWriter::new(set_vaddr as *mut u8, size, frame.is_from_user())?;
10039430523bSyuyi2439                 let set: &mut [u8] = user_buffer_writer.buffer(0)?;
10049430523bSyuyi2439 
10059430523bSyuyi2439                 Self::getaffinity(pid, set)
100602343d0bSLoGin             }
100702343d0bSLoGin 
100852da9a59SGnoCiYeH             #[cfg(target_arch = "x86_64")]
100952da9a59SGnoCiYeH             SYS_GETRLIMIT => {
101052da9a59SGnoCiYeH                 let resource = args[0];
101152da9a59SGnoCiYeH                 let rlimit = args[1] as *mut RLimit64;
101252da9a59SGnoCiYeH 
101352da9a59SGnoCiYeH                 Self::prlimit64(
101452da9a59SGnoCiYeH                     ProcessManager::current_pcb().pid(),
101552da9a59SGnoCiYeH                     resource,
1016840045afSLoGin                     core::ptr::null::<RLimit64>(),
101752da9a59SGnoCiYeH                     rlimit,
101852da9a59SGnoCiYeH                 )
101952da9a59SGnoCiYeH             }
102052da9a59SGnoCiYeH 
10219e481b3bSTTaq             SYS_FADVISE64 => {
10229e481b3bSTTaq                 // todo: 这个系统调用还没有实现
10239e481b3bSTTaq 
10249e481b3bSTTaq                 Err(SystemError::ENOSYS)
10259e481b3bSTTaq             }
10261d37ca6dSDonkey Kane 
10271d37ca6dSDonkey Kane             SYS_MOUNT => {
10281d37ca6dSDonkey Kane                 let source = args[0] as *const u8;
10291d37ca6dSDonkey Kane                 let target = args[1] as *const u8;
10301d37ca6dSDonkey Kane                 let filesystemtype = args[2] as *const u8;
10311d37ca6dSDonkey Kane                 return Self::mount(source, target, filesystemtype, 0, null());
10321d37ca6dSDonkey Kane             }
1033*1074eb34SSamuel Dai 
1034*1074eb34SSamuel Dai             SYS_UMOUNT2 => {
1035*1074eb34SSamuel Dai                 let target = args[0] as *const u8;
1036*1074eb34SSamuel Dai                 let flags = args[1] as i32;
1037*1074eb34SSamuel Dai                 Self::umount2(target, flags)?;
1038*1074eb34SSamuel Dai                 return Ok(0);
1039*1074eb34SSamuel Dai             }
1040*1074eb34SSamuel Dai 
10419e481b3bSTTaq             SYS_NEWFSTATAT => {
10429e481b3bSTTaq                 // todo: 这个系统调用还没有实现
10439e481b3bSTTaq 
10449e481b3bSTTaq                 Err(SystemError::ENOSYS)
10459e481b3bSTTaq             }
10469e481b3bSTTaq 
1047f0c87a89SGnoCiYeH             // SYS_SCHED_YIELD => Self::sched_yield(),
10488c6f2184Ssspphh             SYS_UNAME => {
10498c6f2184Ssspphh                 let name = args[0] as *mut PosixOldUtsName;
10508c6f2184Ssspphh                 Self::uname(name)
10518c6f2184Ssspphh             }
1052c719ddc6SSaga1718             SYS_PRCTL => {
1053c719ddc6SSaga1718                 // todo: 这个系统调用还没有实现
1054c719ddc6SSaga1718 
1055c719ddc6SSaga1718                 Err(SystemError::EINVAL)
1056c719ddc6SSaga1718             }
10571d37ca6dSDonkey Kane 
10586fc066acSJomo             SYS_SHMGET => {
10596fc066acSJomo                 let key = ShmKey::new(args[0]);
10606fc066acSJomo                 let size = args[1];
10616fc066acSJomo                 let shmflg = ShmFlags::from_bits_truncate(args[2] as u32);
10626fc066acSJomo 
10636fc066acSJomo                 Self::shmget(key, size, shmflg)
10646fc066acSJomo             }
10656fc066acSJomo             SYS_SHMAT => {
10666fc066acSJomo                 let id = ShmId::new(args[0]);
10676fc066acSJomo                 let vaddr = VirtAddr::new(args[1]);
10686fc066acSJomo                 let shmflg = ShmFlags::from_bits_truncate(args[2] as u32);
10696fc066acSJomo 
10706fc066acSJomo                 Self::shmat(id, vaddr, shmflg)
10716fc066acSJomo             }
10726fc066acSJomo             SYS_SHMDT => {
10736fc066acSJomo                 let vaddr = VirtAddr::new(args[0]);
10746fc066acSJomo                 Self::shmdt(vaddr)
10756fc066acSJomo             }
10766fc066acSJomo             SYS_SHMCTL => {
10776fc066acSJomo                 let id = ShmId::new(args[0]);
10786fc066acSJomo                 let cmd = ShmCtlCmd::from(args[1]);
10796fc066acSJomo                 let user_buf = args[2] as *const u8;
10806fc066acSJomo                 let from_user = frame.is_from_user();
10816fc066acSJomo 
10826fc066acSJomo                 Self::shmctl(id, cmd, user_buf, from_user)
10836fc066acSJomo             }
10846fc066acSJomo 
1085ab5c8ca4Slogin             _ => panic!("Unsupported syscall ID: {}", syscall_num),
1086ab5c8ca4Slogin         };
10874fda81ceSLoGin 
1088f0c87a89SGnoCiYeH         if ProcessManager::current_pcb()
1089f0c87a89SGnoCiYeH             .flags()
1090f0c87a89SGnoCiYeH             .contains(ProcessFlags::NEED_SCHEDULE)
1091f0c87a89SGnoCiYeH         {
1092f0c87a89SGnoCiYeH             schedule(SchedMode::SM_PREEMPT);
1093f0c87a89SGnoCiYeH         }
1094f0c87a89SGnoCiYeH 
1095ab5c8ca4Slogin         return r;
1096ab5c8ca4Slogin     }
1097ab5c8ca4Slogin 
1098ab5c8ca4Slogin     pub fn put_string(
1099ab5c8ca4Slogin         s: *const u8,
1100ab5c8ca4Slogin         front_color: u32,
1101ab5c8ca4Slogin         back_color: u32,
1102ab5c8ca4Slogin     ) -> Result<usize, SystemError> {
1103c3dc6f2fSLoGin         // todo: 删除这个系统调用
1104c3dc6f2fSLoGin         let s = check_and_clone_cstr(s, Some(4096))?;
1105c3dc6f2fSLoGin         let fr = (front_color & 0x00ff0000) >> 16;
1106c3dc6f2fSLoGin         let fg = (front_color & 0x0000ff00) >> 8;
1107c3dc6f2fSLoGin         let fb = front_color & 0x000000ff;
1108c3dc6f2fSLoGin         let br = (back_color & 0x00ff0000) >> 16;
1109c3dc6f2fSLoGin         let bg = (back_color & 0x0000ff00) >> 8;
1110c3dc6f2fSLoGin         let bb = back_color & 0x000000ff;
1111c3dc6f2fSLoGin         print!("\x1B[38;2;{fr};{fg};{fb};48;2;{br};{bg};{bb}m{s}\x1B[0m");
1112c3dc6f2fSLoGin         return Ok(s.len());
1113ab5c8ca4Slogin     }
1114ab5c8ca4Slogin 
1115ab5c8ca4Slogin     pub fn reboot() -> Result<usize, SystemError> {
11164fda81ceSLoGin         unsafe { cpu_reset() };
1117ab5c8ca4Slogin     }
1118ab5c8ca4Slogin }
1119