xref: /DragonOS/kernel/src/syscall/mod.rs (revision cc5feaf67b914ecf701abcba70c01da149755491)
1 use core::{
2     ffi::{c_char, c_int, c_void, CStr},
3     sync::atomic::{AtomicBool, Ordering},
4 };
5 
6 use crate::{
7     arch::syscall::{
8         SYS_ACCESS, SYS_CHMOD, SYS_CLOCK_GETTIME, SYS_FACCESSAT, SYS_FACCESSAT2, SYS_FCHMOD,
9         SYS_FCHMODAT, SYS_LSTAT, SYS_OPENAT, SYS_PRLIMIT64, SYS_READV, SYS_SYSINFO, SYS_UMASK,
10         SYS_UNLINK,
11     },
12     libs::{futex::constant::FutexFlag, rand::GRandFlags},
13     process::{
14         fork::KernelCloneArgs,
15         resource::{RLimit64, RUsage},
16     },
17 };
18 
19 use num_traits::{FromPrimitive, ToPrimitive};
20 
21 use crate::{
22     arch::{cpu::cpu_reset, interrupt::TrapFrame, MMArch},
23     driver::base::{block::SeekFrom, device::DeviceNumber},
24     filesystem::vfs::{
25         fcntl::FcntlCommand,
26         file::FileMode,
27         syscall::{ModeType, PosixKstat, SEEK_CUR, SEEK_END, SEEK_MAX, SEEK_SET},
28         MAX_PATHLEN,
29     },
30     include::bindings::bindings::{PAGE_2M_SIZE, PAGE_4K_SIZE},
31     kinfo,
32     libs::align::page_align_up,
33     mm::{verify_area, MemoryManagementArch, VirtAddr},
34     net::syscall::SockAddr,
35     process::{fork::CloneFlags, Pid},
36     time::{
37         syscall::{PosixTimeZone, PosixTimeval},
38         TimeSpec,
39     },
40 };
41 
42 use self::{
43     misc::SysInfo,
44     user_access::{UserBufferReader, UserBufferWriter},
45 };
46 
47 pub mod misc;
48 pub mod user_access;
49 
50 #[repr(i32)]
51 #[derive(Debug, FromPrimitive, ToPrimitive, PartialEq, Eq, Clone)]
52 #[allow(dead_code, non_camel_case_types)]
53 pub enum SystemError {
54     /// 操作不被允许 Operation not permitted.
55     EPERM = 1,
56     /// 没有指定的文件或目录 No such file or directory.
57     ENOENT = 2,
58     /// 没有这样的进程 No such process.
59     ESRCH = 3,
60     /// 被中断的函数 Interrupted function.
61     EINTR = 4,
62     /// I/O错误 I/O error.
63     EIO = 5,
64     /// 没有这样的设备或地址 No such device or address.
65     ENXIO = 6,
66     /// 参数列表过长,或者在输出buffer中缺少空间 或者参数比系统内建的最大值要大 Argument list too long.
67     E2BIG = 7,
68     /// 可执行文件格式错误 Executable file format error
69     ENOEXEC = 8,
70     /// 错误的文件描述符 Bad file descriptor.
71     EBADF = 9,
72     /// 没有子进程 No child processes.
73     ECHILD = 10,
74     /// 资源不可用,请重试。 Resource unavailable, try again.(may be the same value as [EWOULDBLOCK])
75     ///
76     /// 操作将被禁止 Operation would block.(may be the same value as [EAGAIN]).
77     EAGAIN_OR_EWOULDBLOCK = 11,
78     /// 没有足够的空间 Not enough space.
79     ENOMEM = 12,
80     /// 访问被拒绝 Permission denied
81     EACCES = 13,
82     /// 错误的地址 Bad address
83     EFAULT = 14,
84     /// 需要块设备 Block device required
85     ENOTBLK = 15,
86     /// 设备或资源忙 Device or resource busy.
87     EBUSY = 16,
88     /// 文件已存在 File exists.
89     EEXIST = 17,
90     /// 跨设备连接 Cross-device link.
91     EXDEV = 18,
92     /// 没有指定的设备 No such device.
93     ENODEV = 19,
94     /// 不是目录 Not a directory.
95     ENOTDIR = 20,
96     /// 是一个目录 Is a directory
97     EISDIR = 21,
98     /// 不可用的参数 Invalid argument.
99     EINVAL = 22,
100     /// 系统中打开的文件过多 Too many files open in system.
101     ENFILE = 23,
102     /// 文件描述符的值过大 File descriptor value too large.
103     EMFILE = 24,
104     /// 不正确的I/O控制操作 Inappropriate I/O control operation.
105     ENOTTY = 25,
106     /// 文本文件忙 Text file busy.
107     ETXTBSY = 26,
108     /// 文件太大 File too large.
109     EFBIG = 27,
110     /// 设备上没有空间 No space left on device.
111     ENOSPC = 28,
112     /// 错误的寻道.当前文件是pipe,不允许seek请求  Invalid seek.
113     ESPIPE = 29,
114     /// 只读的文件系统 Read-only file system.
115     EROFS = 30,
116     /// 链接数过多 Too many links.
117     EMLINK = 31,
118     /// 断开的管道 Broken pipe.
119     EPIPE = 32,
120     /// 数学参数超出作用域 Mathematics argument out of domain of function.
121     EDOM = 33,
122     /// 结果过大 Result too large.
123     ERANGE = 34,
124     /// 资源死锁将要发生 Resource deadlock would occur.
125     EDEADLK = 35,
126     /// 文件名过长 Filename too long.
127     ENAMETOOLONG = 36,
128     /// 没有可用的锁 No locks available.
129     ENOLCK = 37,
130     /// 功能不支持 Function not supported.
131     ENOSYS = 38,
132     /// 目录非空 Directory not empty.
133     ENOTEMPTY = 39,
134     /// 符号链接级别过多 Too many levels of symbolic links.
135     ELOOP = 40,
136     /// 没有期待类型的消息 No message of the desired type.
137     ENOMSG = 41,
138     /// 标志符被移除 Identifier removed.
139     EIDRM = 42,
140     /// 通道号超出范围 Channel number out of range
141     ECHRNG = 43,
142     /// 二级不同步 Level 2 not synchronized
143     EL2NSYNC = 44,
144     /// 三级暂停 Level 3 halted
145     EL3HLT = 45,
146     /// 三级重置 Level 3 reset
147     EL3RST = 46,
148     /// 链接号超出范围 Link number out of range
149     ELNRNG = 47,
150     /// 未连接协议驱动程序 Protocol driver not attached
151     EUNATCH = 48,
152     /// 没有可用的CSI结构 No CSI structure available
153     ENOCSI = 49,
154     /// 二级暂停 Level 2 halted
155     EL2HLT = 50,
156     /// 无效交换 Invalid exchange
157     EBADE = 51,
158     /// 无效的请求描述符 Invalid request descriptor
159     EBADR = 52,
160     /// 交换满 Exchange full
161     EXFULL = 53,
162     /// 无阳极 No anode
163     ENOANO = 54,
164     /// 请求码无效 Invalid request code
165     EBADRQC = 55,
166     /// 无效插槽 Invalid slot
167     EBADSLT = 56,
168     /// 资源死锁 Resource deadlock would occur
169     EDEADLOCK = 57,
170     /// 错误的字体文件格式 Bad font file format
171     EBFONT = 58,
172     /// 不是STREAM Not a STREAM
173     ENOSTR = 59,
174     /// 队列头没有可读取的消息 No message is available on the STREAM head read queue.
175     ENODATA = 60,
176     /// 流式ioctl()超时 Stream ioctl() timeout
177     ETIME = 61,
178     /// 没有STREAM资源  No STREAM resources.
179     ENOSR = 62,
180     /// 机器不在网络上 Machine is not on the network
181     ENONET = 63,
182     /// 未安装软件包 Package not installed
183     ENOPKG = 64,
184     /// 远程对象 Object is remote
185     EREMOTE = 65,
186     /// 保留 Reserved.
187     ENOLINK = 66,
188     /// 外设错误 Advertise error.
189     EADV = 67,
190     /// 安装错误 Srmount error
191     ESRMNT = 68,
192     /// 发送时发生通信错误 Communication error on send
193     ECOMM = 69,
194     /// 协议错误 Protocol error.
195     EPROTO = 70,
196     /// 保留使用 Reserved.
197     EMULTIHOP = 71,
198     /// RFS特定错误 RFS specific error
199     EDOTDOT = 72,
200     /// 错误的消息 Bad message.
201     EBADMSG = 73,
202     /// 数值过大,产生溢出 Value too large to be stored in data type.
203     EOVERFLOW = 74,
204     /// 名称在网络上不是唯一的 Name not unique on network
205     ENOTUNIQ = 75,
206     /// 处于不良状态的文件描述符 File descriptor in bad state
207     EBADFD = 76,
208     /// 远程地址已更改 Remote address changed
209     EREMCHG = 77,
210     /// 无法访问所需的共享库 Can not access a needed shared library
211     ELIBACC = 78,
212     /// 访问损坏的共享库 Accessing a corrupted shared library
213     ELIBBAD = 79,
214     /// a. out中的.lib部分已损坏 .lib section in a.out corrupted
215     ELIBSCN = 80,
216     /// 尝试链接太多共享库 Attempting to link in too many shared libraries
217     ELIBMAX = 81,
218     /// 无法直接执行共享库 Cannot exec a shared library directly
219     ELIBEXEC = 82,
220     /// 不合法的字符序列 Illegal byte sequence.
221     EILSEQ = 83,
222     /// 中断的系统调用应该重新启动 Interrupted system call should be restarted
223     ERESTART = 84,
224     /// 流管道错误 Streams pipe error
225     ESTRPIPE = 85,
226     /// 用户太多 Too many users
227     EUSERS = 86,
228     /// 不是一个套接字 Not a socket.
229     ENOTSOCK = 87,
230     /// 需要目标地址 Destination address required.
231     EDESTADDRREQ = 88,
232     /// 消息过大 Message too large.
233     EMSGSIZE = 89,
234     /// 对于套接字而言,错误的协议 Protocol wrong type for socket.
235     EPROTOTYPE = 90,
236     /// 协议不可用 Protocol not available.
237     ENOPROTOOPT = 91,
238     /// 协议不被支持 Protocol not supported.
239     EPROTONOSUPPORT = 92,
240     /// 不支持套接字类型 Socket type not supported
241     ESOCKTNOSUPPORT = 93,
242     /// 套接字不支持该操作 Operation not supported on socket (may be the same value as [ENOTSUP]).
243     ///
244     /// 不被支持 Not supported (may be the same value as [EOPNOTSUPP]).
245     EOPNOTSUPP_OR_ENOTSUP = 94,
246     /// 不支持协议系列 Protocol family not supported
247     EPFNOSUPPORT = 95,
248     /// 地址family不支持 Address family not supported.
249     EAFNOSUPPORT = 96,
250     /// 地址正在被使用 Address in use.
251     EADDRINUSE = 97,
252     /// 地址不可用 Address  not available.
253     EADDRNOTAVAIL = 98,
254     /// 网络已关闭 Network is down.
255     ENETDOWN = 99,
256     /// 网络不可达 Network unreachable.
257     ENETUNREACH = 100,
258     /// 网络连接已断开 Connection aborted by network.
259     ENETRESET = 101,
260     /// 连接已断开 Connection aborted.
261     ECONNABORTED = 102,
262     /// 连接被重置 Connection reset.
263     ECONNRESET = 103,
264     /// 缓冲区空间不足 No buffer space available.
265     ENOBUFS = 104,
266     /// 套接字已连接 Socket is connected.
267     EISCONN = 105,
268     /// 套接字未连接 The socket is not connected.
269     ENOTCONN = 106,
270     /// 传输端点关闭后无法发送 Cannot send after transport endpoint shutdown
271     ESHUTDOWN = 107,
272     /// 引用太多:无法拼接 Too many references: cannot splice
273     ETOOMANYREFS = 108,
274     /// 连接超时 Connection timed out.
275     ETIMEDOUT = 109,
276     /// 连接被拒绝 Connection refused.
277     ECONNREFUSED = 110,
278     /// 主机已关闭 Host is down
279     EHOSTDOWN = 111,
280     /// 主机不可达 Host is unreachable.
281     EHOSTUNREACH = 112,
282     /// 连接已经在处理 Connection already in progress.
283     EALREADY = 113,
284     /// 操作正在处理 Operation in progress.
285     EINPROGRESS = 114,
286     /// 保留 Reserved.
287     ESTALE = 115,
288     /// 结构需要清理 Structure needs cleaning
289     EUCLEAN = 116,
290     /// 不是XENIX命名类型文件 Not a XENIX named type file
291     ENOTNAM = 117,
292     /// 没有可用的XENIX信号量 No XENIX semaphores available
293     ENAVAIL = 118,
294     /// 是命名类型文件 Is a named type file
295     EISNAM = 119,
296     /// 远程I/O错误 Remote I/O error
297     EREMOTEIO = 120,
298     /// 保留使用 Reserved
299     EDQUOT = 121,
300     /// 没有找到媒介 No medium found
301     ENOMEDIUM = 122,
302     /// 介质类型错误 Wrong medium type
303     EMEDIUMTYPE = 123,
304     /// 操作被取消 Operation canceled.
305     ECANCELED = 124,
306     /// 所需的密钥不可用 Required key not available
307     ENOKEY = 125,
308     /// 密钥已过期 Key has expired
309     EKEYEXPIRED = 126,
310     /// 密钥已被撤销 Key has been revoked
311     EKEYREVOKED = 127,
312     /// 密钥被服务拒绝 Key has been revoked
313     EKEYREJECTED = 128,
314     /// 之前的拥有者挂了 Previous owner died.
315     EOWNERDEAD = 129,
316     /// 状态不可恢复 State not recoverable.
317     ENOTRECOVERABLE = 130,
318     // VMX on 虚拟化开启指令出错
319     EVMXONFailed = 131,
320     // VMX off 虚拟化关闭指令出错
321     EVMXOFFFailed = 132,
322     // VMX VMWRITE 写入虚拟化VMCS内存出错
323     EVMWRITEFailed = 133,
324     EVMREADFailed = 134,
325     EVMPRTLDFailed = 135,
326     EVMLAUNCHFailed = 136,
327     KVM_HVA_ERR_BAD = 137,
328 
329     // === 以下错误码不应该被用户态程序使用 ===
330     ERESTARTSYS = 512,
331 }
332 
333 impl SystemError {
334     /// @brief 把posix错误码转换为系统错误枚举类型。
335     pub fn from_posix_errno(errno: i32) -> Option<SystemError> {
336         // posix 错误码是小于0的
337         if errno >= 0 {
338             return None;
339         }
340         return <Self as FromPrimitive>::from_i32(-errno);
341     }
342 
343     /// @brief 把系统错误枚举类型转换为负数posix错误码。
344     pub fn to_posix_errno(&self) -> i32 {
345         return -<Self as ToPrimitive>::to_i32(self).unwrap();
346     }
347 }
348 
349 // 定义系统调用号
350 pub const SYS_READ: usize = 0;
351 pub const SYS_WRITE: usize = 1;
352 pub const SYS_OPEN: usize = 2;
353 pub const SYS_CLOSE: usize = 3;
354 pub const SYS_STAT: usize = 4;
355 pub const SYS_FSTAT: usize = 5;
356 
357 pub const SYS_POLL: usize = 7;
358 pub const SYS_LSEEK: usize = 8;
359 pub const SYS_MMAP: usize = 9;
360 pub const SYS_MPROTECT: usize = 10;
361 
362 pub const SYS_MUNMAP: usize = 11;
363 pub const SYS_BRK: usize = 12;
364 pub const SYS_SIGACTION: usize = 13;
365 pub const SYS_RT_SIGPROCMASK: usize = 14;
366 pub const SYS_RT_SIGRETURN: usize = 15;
367 
368 pub const SYS_IOCTL: usize = 16;
369 
370 pub const SYS_WRITEV: usize = 20;
371 pub const SYS_PIPE: usize = 22;
372 
373 pub const SYS_MADVISE: usize = 28;
374 
375 pub const SYS_DUP: usize = 32;
376 pub const SYS_DUP2: usize = 33;
377 
378 pub const SYS_NANOSLEEP: usize = 35;
379 
380 pub const SYS_GETPID: usize = 39;
381 
382 pub const SYS_SOCKET: usize = 41;
383 pub const SYS_CONNECT: usize = 42;
384 pub const SYS_ACCEPT: usize = 43;
385 pub const SYS_SENDTO: usize = 44;
386 pub const SYS_RECVFROM: usize = 45;
387 
388 pub const SYS_RECVMSG: usize = 47;
389 pub const SYS_SHUTDOWN: usize = 48;
390 pub const SYS_BIND: usize = 49;
391 pub const SYS_LISTEN: usize = 50;
392 pub const SYS_GETSOCKNAME: usize = 51;
393 pub const SYS_GETPEERNAME: usize = 52;
394 pub const SYS_SOCKET_PAIR: usize = 53;
395 pub const SYS_SETSOCKOPT: usize = 54;
396 pub const SYS_GETSOCKOPT: usize = 55;
397 
398 pub const SYS_CLONE: usize = 56;
399 pub const SYS_FORK: usize = 57;
400 pub const SYS_VFORK: usize = 58;
401 pub const SYS_EXECVE: usize = 59;
402 pub const SYS_EXIT: usize = 60;
403 pub const SYS_WAIT4: usize = 61;
404 pub const SYS_KILL: usize = 62;
405 
406 pub const SYS_FCNTL: usize = 72;
407 
408 pub const SYS_FTRUNCATE: usize = 77;
409 pub const SYS_GET_DENTS: usize = 78;
410 
411 pub const SYS_GETCWD: usize = 79;
412 
413 pub const SYS_CHDIR: usize = 80;
414 
415 pub const SYS_MKDIR: usize = 83;
416 
417 pub const SYS_READLINK: usize = 89;
418 
419 pub const SYS_GETTIMEOFDAY: usize = 96;
420 pub const SYS_GETRUSAGE: usize = 98;
421 
422 pub const SYS_GETUID: usize = 102;
423 pub const SYS_SYSLOG: usize = 103;
424 pub const SYS_GETGID: usize = 104;
425 pub const SYS_SETUID: usize = 105;
426 
427 pub const SYS_SETGID: usize = 106;
428 pub const SYS_GETEUID: usize = 107;
429 pub const SYS_GETEGID: usize = 108;
430 
431 pub const SYS_GETPPID: usize = 110;
432 pub const SYS_GETPGID: usize = 121;
433 
434 pub const SYS_SIGALTSTACK: usize = 131;
435 pub const SYS_MKNOD: usize = 133;
436 
437 pub const SYS_ARCH_PRCTL: usize = 158;
438 
439 pub const SYS_REBOOT: usize = 169;
440 
441 pub const SYS_GETTID: usize = 186;
442 
443 #[allow(dead_code)]
444 pub const SYS_TKILL: usize = 200;
445 
446 #[allow(dead_code)]
447 pub const SYS_FUTEX: usize = 202;
448 
449 pub const SYS_GET_DENTS_64: usize = 217;
450 #[allow(dead_code)]
451 pub const SYS_SET_TID_ADDR: usize = 218;
452 
453 pub const SYS_EXIT_GROUP: usize = 231;
454 
455 pub const SYS_UNLINK_AT: usize = 263;
456 
457 pub const SYS_READLINK_AT: usize = 267;
458 
459 pub const SYS_ACCEPT4: usize = 288;
460 
461 pub const SYS_PIPE2: usize = 293;
462 
463 #[allow(dead_code)]
464 pub const SYS_GET_RANDOM: usize = 318;
465 
466 // 与linux不一致的调用,在linux基础上累加
467 pub const SYS_PUT_STRING: usize = 100000;
468 pub const SYS_SBRK: usize = 100001;
469 /// todo: 该系统调用与Linux不一致,将来需要删除该系统调用!!! 删的时候记得改C版本的libc
470 pub const SYS_CLOCK: usize = 100002;
471 pub const SYS_SCHED: usize = 100003;
472 
473 #[derive(Debug)]
474 pub struct Syscall;
475 
476 extern "C" {
477     fn do_put_string(s: *const u8, front_color: u32, back_color: u32) -> usize;
478 }
479 
480 #[no_mangle]
481 pub extern "C" fn syscall_init() -> i32 {
482     kinfo!("Initializing syscall...");
483     Syscall::init().expect("syscall init failed");
484     kinfo!("Syscall init successfully!");
485     return 0;
486 }
487 
488 impl Syscall {
489     /// 初始化系统调用
490     pub fn init() -> Result<(), SystemError> {
491         static INIT_FLAG: AtomicBool = AtomicBool::new(false);
492         let prev = INIT_FLAG.swap(true, Ordering::SeqCst);
493         if prev {
494             panic!("Cannot initialize syscall more than once!");
495         }
496         return crate::arch::syscall::arch_syscall_init();
497     }
498     /// @brief 系统调用分发器,用于分发系统调用。
499     ///
500     /// 这个函数内,需要根据系统调用号,调用对应的系统调用处理函数。
501     /// 并且,对于用户态传入的指针参数,需要在本函数内进行越界检查,防止访问到内核空间。
502     pub fn handle(
503         syscall_num: usize,
504         args: &[usize],
505         frame: &mut TrapFrame,
506     ) -> Result<usize, SystemError> {
507         let r = match syscall_num {
508             SYS_PUT_STRING => {
509                 Self::put_string(args[0] as *const u8, args[1] as u32, args[2] as u32)
510             }
511             SYS_OPEN => {
512                 let path: &CStr = unsafe { CStr::from_ptr(args[0] as *const c_char) };
513                 let path: Result<&str, core::str::Utf8Error> = path.to_str();
514                 let res = if path.is_err() {
515                     Err(SystemError::EINVAL)
516                 } else {
517                     let path: &str = path.unwrap();
518 
519                     let flags = args[1];
520                     let mode = args[2];
521 
522                     let open_flags: FileMode = FileMode::from_bits_truncate(flags as u32);
523                     let mode = ModeType::from_bits(mode as u32).ok_or(SystemError::EINVAL)?;
524                     Self::open(path, open_flags, mode, true)
525                 };
526                 res
527             }
528 
529             SYS_OPENAT => {
530                 let dirfd = args[0] as i32;
531                 let path: &CStr = unsafe { CStr::from_ptr(args[1] as *const c_char) };
532                 let flags = args[2];
533                 let mode = args[3];
534 
535                 let path: Result<&str, core::str::Utf8Error> = path.to_str();
536                 let res = if path.is_err() {
537                     Err(SystemError::EINVAL)
538                 } else {
539                     let path: &str = path.unwrap();
540 
541                     let open_flags: FileMode =
542                         FileMode::from_bits(flags as u32).ok_or(SystemError::EINVAL)?;
543                     let mode = ModeType::from_bits(mode as u32).ok_or(SystemError::EINVAL)?;
544                     Self::openat(dirfd, path, open_flags, mode, true)
545                 };
546                 res
547             }
548             SYS_CLOSE => {
549                 let fd = args[0];
550 
551                 let res = Self::close(fd);
552 
553                 res
554             }
555             SYS_READ => {
556                 let fd = args[0] as i32;
557                 let buf_vaddr = args[1];
558                 let len = args[2];
559                 let from_user = frame.from_user();
560                 let mut user_buffer_writer =
561                     UserBufferWriter::new(buf_vaddr as *mut u8, len, from_user)?;
562 
563                 let user_buf = user_buffer_writer.buffer(0)?;
564                 let res = Self::read(fd, user_buf);
565                 res
566             }
567             SYS_WRITE => {
568                 let fd = args[0] as i32;
569                 let buf_vaddr = args[1];
570                 let len = args[2];
571                 let from_user = frame.from_user();
572                 let user_buffer_reader =
573                     UserBufferReader::new(buf_vaddr as *const u8, len, from_user)?;
574 
575                 let user_buf = user_buffer_reader.read_from_user(0)?;
576                 let res = Self::write(fd, user_buf);
577                 res
578             }
579 
580             SYS_LSEEK => {
581                 let fd = args[0] as i32;
582                 let offset = args[1] as i64;
583                 let whence = args[2] as u32;
584 
585                 let w = match whence {
586                     SEEK_SET => Ok(SeekFrom::SeekSet(offset)),
587                     SEEK_CUR => Ok(SeekFrom::SeekCurrent(offset)),
588                     SEEK_END => Ok(SeekFrom::SeekEnd(offset)),
589                     SEEK_MAX => Ok(SeekFrom::SeekEnd(0)),
590                     _ => Err(SystemError::EINVAL),
591                 }?;
592 
593                 Self::lseek(fd, w)
594             }
595             SYS_IOCTL => {
596                 let fd = args[0];
597                 let cmd = args[1];
598                 let data = args[2];
599                 Self::ioctl(fd, cmd as u32, data)
600             }
601 
602             SYS_FORK => Self::fork(frame),
603             SYS_VFORK => Self::vfork(frame),
604 
605             SYS_BRK => {
606                 let new_brk = VirtAddr::new(args[0]);
607                 Self::brk(new_brk).map(|vaddr| vaddr.data())
608             }
609 
610             SYS_SBRK => {
611                 let increment = args[0] as isize;
612                 Self::sbrk(increment).map(|vaddr: VirtAddr| vaddr.data())
613             }
614 
615             SYS_REBOOT => Self::reboot(),
616 
617             SYS_CHDIR => {
618                 // Closure for checking arguments
619                 let chdir_check = |arg0: usize| {
620                     if arg0 == 0 {
621                         return Err(SystemError::EFAULT);
622                     }
623                     let path_ptr = arg0 as *const c_char;
624                     let virt_addr = VirtAddr::new(path_ptr as usize);
625                     // 权限校验
626                     if path_ptr.is_null()
627                         || (frame.from_user()
628                             && verify_area(virt_addr, PAGE_2M_SIZE as usize).is_err())
629                     {
630                         return Err(SystemError::EINVAL);
631                     }
632                     let dest_path: &CStr = unsafe { CStr::from_ptr(path_ptr) };
633                     let dest_path: &str = dest_path.to_str().map_err(|_| SystemError::EINVAL)?;
634                     if dest_path.len() == 0 {
635                         return Err(SystemError::EINVAL);
636                     } else if dest_path.len() > MAX_PATHLEN as usize {
637                         return Err(SystemError::ENAMETOOLONG);
638                     }
639 
640                     return Ok(dest_path);
641                 };
642 
643                 let r = chdir_check(args[0])?;
644                 Self::chdir(r)
645             }
646 
647             SYS_GET_DENTS | SYS_GET_DENTS_64 => {
648                 let fd = args[0] as i32;
649 
650                 let buf_vaddr = args[1];
651                 let len = args[2];
652                 let virt_addr: VirtAddr = VirtAddr::new(buf_vaddr);
653                 // 判断缓冲区是否来自用户态,进行权限校验
654                 let res = if frame.from_user() && verify_area(virt_addr, len as usize).is_err() {
655                     // 来自用户态,而buffer在内核态,这样的操作不被允许
656                     Err(SystemError::EPERM)
657                 } else if buf_vaddr == 0 {
658                     Err(SystemError::EFAULT)
659                 } else {
660                     let buf: &mut [u8] = unsafe {
661                         core::slice::from_raw_parts_mut::<'static, u8>(buf_vaddr as *mut u8, len)
662                     };
663                     Self::getdents(fd, buf)
664                 };
665 
666                 res
667             }
668 
669             SYS_EXECVE => {
670                 let path_ptr = args[0];
671                 let argv_ptr = args[1];
672                 let env_ptr = args[2];
673                 let virt_path_ptr = VirtAddr::new(path_ptr);
674                 let virt_argv_ptr = VirtAddr::new(argv_ptr);
675                 let virt_env_ptr = VirtAddr::new(env_ptr);
676                 // 权限校验
677                 if frame.from_user()
678                     && (verify_area(virt_path_ptr, MAX_PATHLEN as usize).is_err()
679                         || verify_area(virt_argv_ptr, PAGE_4K_SIZE as usize).is_err())
680                     || verify_area(virt_env_ptr, PAGE_4K_SIZE as usize).is_err()
681                 {
682                     Err(SystemError::EFAULT)
683                 } else {
684                     Self::execve(
685                         path_ptr as *const u8,
686                         argv_ptr as *const *const u8,
687                         env_ptr as *const *const u8,
688                         frame,
689                     )
690                     .map(|_| 0)
691                 }
692             }
693             SYS_WAIT4 => {
694                 let pid = args[0] as i32;
695                 let wstatus = args[1] as *mut i32;
696                 let options = args[2] as c_int;
697                 let rusage = args[3] as *mut c_void;
698                 // 权限校验
699                 // todo: 引入rusage之后,更正以下权限校验代码中,rusage的大小
700                 Self::wait4(pid.into(), wstatus, options, rusage)
701             }
702 
703             SYS_EXIT => {
704                 let exit_code = args[0];
705                 Self::exit(exit_code)
706             }
707             SYS_MKDIR => {
708                 let path_ptr = args[0] as *const c_char;
709                 let mode = args[1];
710                 let virt_path_ptr = VirtAddr::new(path_ptr as usize);
711                 let security_check = || {
712                     if path_ptr.is_null()
713                         || (frame.from_user()
714                             && verify_area(virt_path_ptr, PAGE_2M_SIZE as usize).is_err())
715                     {
716                         return Err(SystemError::EINVAL);
717                     }
718                     let path: &CStr = unsafe { CStr::from_ptr(path_ptr) };
719                     let path: &str = path.to_str().map_err(|_| SystemError::EINVAL)?.trim();
720 
721                     if path == "" {
722                         return Err(SystemError::EINVAL);
723                     }
724                     return Ok(path);
725                 };
726 
727                 let path = security_check();
728                 if path.is_err() {
729                     Err(path.unwrap_err())
730                 } else {
731                     Self::mkdir(path.unwrap(), mode)
732                 }
733             }
734 
735             SYS_NANOSLEEP => {
736                 let req = args[0] as *const TimeSpec;
737                 let rem = args[1] as *mut TimeSpec;
738                 let virt_req = VirtAddr::new(req as usize);
739                 let virt_rem = VirtAddr::new(rem as usize);
740                 if frame.from_user()
741                     && (verify_area(virt_req, core::mem::size_of::<TimeSpec>() as usize).is_err()
742                         || verify_area(virt_rem, core::mem::size_of::<TimeSpec>() as usize)
743                             .is_err())
744                 {
745                     Err(SystemError::EFAULT)
746                 } else {
747                     Self::nanosleep(req, rem)
748                 }
749             }
750 
751             SYS_CLOCK => Self::clock(),
752             SYS_PIPE => {
753                 let pipefd: *mut i32 = args[0] as *mut c_int;
754                 if pipefd.is_null() {
755                     Err(SystemError::EFAULT)
756                 } else {
757                     Self::pipe2(pipefd, FileMode::empty())
758                 }
759             }
760             SYS_PIPE2 => {
761                 let pipefd: *mut i32 = args[0] as *mut c_int;
762                 let arg1 = args[1];
763                 if pipefd.is_null() {
764                     Err(SystemError::EFAULT)
765                 } else {
766                     let flags = FileMode::from_bits_truncate(arg1 as u32);
767                     Self::pipe2(pipefd, flags)
768                 }
769             }
770 
771             SYS_UNLINK_AT => {
772                 let dirfd = args[0] as i32;
773                 let pathname = args[1] as *const c_char;
774                 let flags = args[2] as u32;
775                 let virt_pathname = VirtAddr::new(pathname as usize);
776                 if frame.from_user() && verify_area(virt_pathname, PAGE_4K_SIZE as usize).is_err() {
777                     Err(SystemError::EFAULT)
778                 } else if pathname.is_null() {
779                     Err(SystemError::EFAULT)
780                 } else {
781                     let get_path = || {
782                         let pathname: &CStr = unsafe { CStr::from_ptr(pathname) };
783 
784                         let pathname: &str = pathname.to_str().map_err(|_| SystemError::EINVAL)?;
785                         if pathname.len() >= MAX_PATHLEN {
786                             return Err(SystemError::ENAMETOOLONG);
787                         }
788                         return Ok(pathname.trim());
789                     };
790                     let pathname = get_path();
791                     if pathname.is_err() {
792                         Err(pathname.unwrap_err())
793                     } else {
794                         // kdebug!("sys unlinkat: dirfd: {}, pathname: {}", dirfd, pathname.as_ref().unwrap());
795                         Self::unlinkat(dirfd, pathname.unwrap(), flags)
796                     }
797                 }
798             }
799 
800             SYS_UNLINK => {
801                 let pathname = args[0] as *const u8;
802                 Self::unlink(pathname)
803             }
804             SYS_KILL => {
805                 let pid = Pid::new(args[0]);
806                 let sig = args[1] as c_int;
807                 // kdebug!("KILL SYSCALL RECEIVED");
808                 Self::kill(pid, sig)
809             }
810 
811             SYS_SIGACTION => {
812                 let sig = args[0] as c_int;
813                 let act = args[1];
814                 let old_act = args[2];
815                 Self::sigaction(sig, act, old_act, frame.from_user())
816             }
817 
818             SYS_RT_SIGRETURN => {
819                 // 由于目前signal机制的实现,与x86_64强关联,因此暂时在arch/x86_64/syscall.rs中调用
820                 // todo: 未来需要将signal机制与平台解耦
821                 todo!()
822             }
823 
824             SYS_GETPID => Self::getpid().map(|pid| pid.into()),
825 
826             SYS_SCHED => Self::sched(frame.from_user()),
827             SYS_DUP => {
828                 let oldfd: i32 = args[0] as c_int;
829                 Self::dup(oldfd)
830             }
831             SYS_DUP2 => {
832                 let oldfd: i32 = args[0] as c_int;
833                 let newfd: i32 = args[1] as c_int;
834                 Self::dup2(oldfd, newfd)
835             }
836 
837             SYS_SOCKET => Self::socket(args[0], args[1], args[2]),
838             SYS_SETSOCKOPT => {
839                 let optval = args[3] as *const u8;
840                 let optlen = args[4] as usize;
841                 let virt_optval = VirtAddr::new(optval as usize);
842                 // 验证optval的地址是否合法
843                 if verify_area(virt_optval, optlen as usize).is_err() {
844                     // 地址空间超出了用户空间的范围,不合法
845                     Err(SystemError::EFAULT)
846                 } else {
847                     let data: &[u8] = unsafe { core::slice::from_raw_parts(optval, optlen) };
848                     Self::setsockopt(args[0], args[1], args[2], data)
849                 }
850             }
851             SYS_GETSOCKOPT => {
852                 let optval = args[3] as *mut u8;
853                 let optlen = args[4] as *mut usize;
854                 let virt_optval = VirtAddr::new(optval as usize);
855                 let virt_optlen = VirtAddr::new(optlen as usize);
856                 let security_check = || {
857                     // 验证optval的地址是否合法
858                     if verify_area(virt_optval, PAGE_4K_SIZE as usize).is_err() {
859                         // 地址空间超出了用户空间的范围,不合法
860                         return Err(SystemError::EFAULT);
861                     }
862 
863                     // 验证optlen的地址是否合法
864                     if verify_area(virt_optlen, core::mem::size_of::<u32>() as usize).is_err() {
865                         // 地址空间超出了用户空间的范围,不合法
866                         return Err(SystemError::EFAULT);
867                     }
868                     return Ok(());
869                 };
870                 let r = security_check();
871                 if r.is_err() {
872                     Err(r.unwrap_err())
873                 } else {
874                     Self::getsockopt(args[0], args[1], args[2], optval, optlen as *mut u32)
875                 }
876             }
877 
878             SYS_CONNECT => {
879                 let addr = args[1] as *const SockAddr;
880                 let addrlen = args[2] as usize;
881                 let virt_addr = VirtAddr::new(addr as usize);
882                 // 验证addr的地址是否合法
883                 if verify_area(virt_addr, addrlen as usize).is_err() {
884                     // 地址空间超出了用户空间的范围,不合法
885                     Err(SystemError::EFAULT)
886                 } else {
887                     Self::connect(args[0], addr, addrlen)
888                 }
889             }
890             SYS_BIND => {
891                 let addr = args[1] as *const SockAddr;
892                 let addrlen = args[2] as usize;
893                 let virt_addr = VirtAddr::new(addr as usize);
894                 // 验证addr的地址是否合法
895                 if verify_area(virt_addr, addrlen as usize).is_err() {
896                     // 地址空间超出了用户空间的范围,不合法
897                     Err(SystemError::EFAULT)
898                 } else {
899                     Self::bind(args[0], addr, addrlen)
900                 }
901             }
902 
903             SYS_SENDTO => {
904                 let buf = args[1] as *const u8;
905                 let len = args[2] as usize;
906                 let flags = args[3] as u32;
907                 let addr = args[4] as *const SockAddr;
908                 let addrlen = args[5] as usize;
909                 let virt_buf = VirtAddr::new(buf as usize);
910                 let virt_addr = VirtAddr::new(addr as usize);
911                 // 验证buf的地址是否合法
912                 if verify_area(virt_buf, len as usize).is_err() {
913                     // 地址空间超出了用户空间的范围,不合法
914                     Err(SystemError::EFAULT)
915                 } else if verify_area(virt_addr, addrlen as usize).is_err() {
916                     // 地址空间超出了用户空间的范围,不合法
917                     Err(SystemError::EFAULT)
918                 } else {
919                     let data: &[u8] = unsafe { core::slice::from_raw_parts(buf, len) };
920                     Self::sendto(args[0], data, flags, addr, addrlen)
921                 }
922             }
923 
924             SYS_RECVFROM => {
925                 let buf = args[1] as *mut u8;
926                 let len = args[2] as usize;
927                 let flags = args[3] as u32;
928                 let addr = args[4] as *mut SockAddr;
929                 let addrlen = args[5] as *mut usize;
930                 let virt_buf = VirtAddr::new(buf as usize);
931                 let virt_addrlen = VirtAddr::new(addrlen as usize);
932                 let virt_addr = VirtAddr::new(addr as usize);
933                 let security_check = || {
934                     // 验证buf的地址是否合法
935                     if verify_area(virt_buf, len as usize).is_err() {
936                         // 地址空间超出了用户空间的范围,不合法
937                         return Err(SystemError::EFAULT);
938                     }
939 
940                     // 验证addrlen的地址是否合法
941                     if verify_area(virt_addrlen, core::mem::size_of::<u32>() as usize).is_err() {
942                         // 地址空间超出了用户空间的范围,不合法
943                         return Err(SystemError::EFAULT);
944                     }
945 
946                     if verify_area(virt_addr, core::mem::size_of::<SockAddr>() as usize).is_err() {
947                         // 地址空间超出了用户空间的范围,不合法
948                         return Err(SystemError::EFAULT);
949                     }
950                     return Ok(());
951                 };
952                 let r = security_check();
953                 if r.is_err() {
954                     Err(r.unwrap_err())
955                 } else {
956                     let buf = unsafe { core::slice::from_raw_parts_mut(buf, len) };
957                     Self::recvfrom(args[0], buf, flags, addr, addrlen as *mut u32)
958                 }
959             }
960 
961             SYS_RECVMSG => {
962                 let msg = args[1] as *mut crate::net::syscall::MsgHdr;
963                 let flags = args[2] as u32;
964                 match UserBufferWriter::new(
965                     msg,
966                     core::mem::size_of::<crate::net::syscall::MsgHdr>(),
967                     true,
968                 ) {
969                     Err(e) => Err(e),
970                     Ok(mut user_buffer_writer) => {
971                         match user_buffer_writer.buffer::<crate::net::syscall::MsgHdr>(0) {
972                             Err(e) => Err(e),
973                             Ok(buffer) => {
974                                 let msg = &mut buffer[0];
975                                 Self::recvmsg(args[0], msg, flags)
976                             }
977                         }
978                     }
979                 }
980             }
981 
982             SYS_LISTEN => Self::listen(args[0], args[1]),
983             SYS_SHUTDOWN => Self::shutdown(args[0], args[1]),
984             SYS_ACCEPT => Self::accept(args[0], args[1] as *mut SockAddr, args[2] as *mut u32),
985             SYS_ACCEPT4 => Self::accept4(
986                 args[0],
987                 args[1] as *mut SockAddr,
988                 args[2] as *mut u32,
989                 args[3] as u32,
990             ),
991             SYS_GETSOCKNAME => {
992                 Self::getsockname(args[0], args[1] as *mut SockAddr, args[2] as *mut u32)
993             }
994             SYS_GETPEERNAME => {
995                 Self::getpeername(args[0], args[1] as *mut SockAddr, args[2] as *mut u32)
996             }
997             SYS_GETTIMEOFDAY => {
998                 let timeval = args[0] as *mut PosixTimeval;
999                 let timezone_ptr = args[1] as *mut PosixTimeZone;
1000                 Self::gettimeofday(timeval, timezone_ptr)
1001             }
1002             SYS_MMAP => {
1003                 let len = page_align_up(args[1]);
1004                 let virt_addr = VirtAddr::new(args[0] as usize);
1005                 if verify_area(virt_addr, len as usize).is_err() {
1006                     Err(SystemError::EFAULT)
1007                 } else {
1008                     Self::mmap(
1009                         VirtAddr::new(args[0]),
1010                         len,
1011                         args[2],
1012                         args[3],
1013                         args[4] as i32,
1014                         args[5],
1015                     )
1016                 }
1017             }
1018             SYS_MUNMAP => {
1019                 let addr = args[0];
1020                 let len = page_align_up(args[1]);
1021                 if addr & (MMArch::PAGE_SIZE - 1) != 0 {
1022                     // The addr argument is not a multiple of the page size
1023                     Err(SystemError::EINVAL)
1024                 } else {
1025                     Self::munmap(VirtAddr::new(addr), len)
1026                 }
1027             }
1028             SYS_MPROTECT => {
1029                 let addr = args[0];
1030                 let len = page_align_up(args[1]);
1031                 if addr & (MMArch::PAGE_SIZE - 1) != 0 {
1032                     // The addr argument is not a multiple of the page size
1033                     Err(SystemError::EINVAL)
1034                 } else {
1035                     Self::mprotect(VirtAddr::new(addr), len, args[2])
1036                 }
1037             }
1038 
1039             SYS_GETCWD => {
1040                 let buf = args[0] as *mut u8;
1041                 let size = args[1] as usize;
1042                 let security_check = || {
1043                     verify_area(VirtAddr::new(buf as usize), size)?;
1044                     return Ok(());
1045                 };
1046                 let r = security_check();
1047                 if r.is_err() {
1048                     Err(r.unwrap_err())
1049                 } else {
1050                     let buf = unsafe { core::slice::from_raw_parts_mut(buf, size) };
1051                     Self::getcwd(buf).map(|ptr| ptr.data())
1052                 }
1053             }
1054 
1055             SYS_GETPGID => Self::getpgid(Pid::new(args[0])).map(|pid| pid.into()),
1056 
1057             SYS_GETPPID => Self::getppid().map(|pid| pid.into()),
1058             SYS_FSTAT => {
1059                 let fd = args[0] as i32;
1060                 let kstat = args[1] as *mut PosixKstat;
1061                 let vaddr = VirtAddr::new(kstat as usize);
1062                 // FIXME 由于c中的verify_area与rust中的verify_area重名,所以在引入时加了前缀区分
1063                 // TODO 应该将用了c版本的verify_area都改为rust的verify_area
1064                 match verify_area(vaddr, core::mem::size_of::<PosixKstat>()) {
1065                     Ok(_) => Self::fstat(fd, kstat),
1066                     Err(e) => Err(e),
1067                 }
1068             }
1069 
1070             SYS_FCNTL => {
1071                 let fd = args[0] as i32;
1072                 let cmd: Option<FcntlCommand> =
1073                     <FcntlCommand as FromPrimitive>::from_u32(args[1] as u32);
1074                 let arg = args[2] as i32;
1075                 let res = if let Some(cmd) = cmd {
1076                     Self::fcntl(fd, cmd, arg)
1077                 } else {
1078                     Err(SystemError::EINVAL)
1079                 };
1080 
1081                 // kdebug!("FCNTL: fd: {}, cmd: {:?}, arg: {}, res: {:?}", fd, cmd, arg, res);
1082                 res
1083             }
1084 
1085             SYS_FTRUNCATE => {
1086                 let fd = args[0] as i32;
1087                 let len = args[1] as usize;
1088                 let res = Self::ftruncate(fd, len);
1089                 // kdebug!("FTRUNCATE: fd: {}, len: {}, res: {:?}", fd, len, res);
1090                 res
1091             }
1092 
1093             SYS_MKNOD => {
1094                 let path = args[0];
1095                 let flags = args[1];
1096                 let dev_t = args[2];
1097                 let flags: ModeType = ModeType::from_bits_truncate(flags as u32);
1098                 Self::mknod(path as *const i8, flags, DeviceNumber::from(dev_t))
1099             }
1100 
1101             SYS_CLONE => {
1102                 let parent_tid = VirtAddr::new(args[2]);
1103                 let child_tid = VirtAddr::new(args[3]);
1104 
1105                 // 地址校验
1106                 verify_area(parent_tid, core::mem::size_of::<i32>())?;
1107                 verify_area(child_tid, core::mem::size_of::<i32>())?;
1108 
1109                 let mut clone_args = KernelCloneArgs::new();
1110                 clone_args.flags = CloneFlags::from_bits_truncate(args[0] as u64);
1111                 clone_args.stack = args[1];
1112                 clone_args.parent_tid = parent_tid;
1113                 clone_args.child_tid = child_tid;
1114                 clone_args.tls = args[4];
1115                 Self::clone(frame, clone_args)
1116             }
1117 
1118             SYS_FUTEX => {
1119                 let uaddr = VirtAddr::new(args[0]);
1120                 let operation = FutexFlag::from_bits(args[1] as u32).ok_or(SystemError::ENOSYS)?;
1121                 let val = args[2] as u32;
1122                 let utime = args[3];
1123                 let uaddr2 = VirtAddr::new(args[4]);
1124                 let val3 = args[5] as u32;
1125 
1126                 verify_area(uaddr, core::mem::size_of::<u32>())?;
1127                 verify_area(uaddr2, core::mem::size_of::<u32>())?;
1128 
1129                 let mut timespec = None;
1130                 if utime != 0 && operation.contains(FutexFlag::FLAGS_HAS_TIMEOUT) {
1131                     let reader = UserBufferReader::new(
1132                         utime as *const TimeSpec,
1133                         core::mem::size_of::<TimeSpec>(),
1134                         true,
1135                     )?;
1136 
1137                     timespec = Some(reader.read_one_from_user::<TimeSpec>(0)?.clone());
1138                 }
1139 
1140                 Self::do_futex(uaddr, operation, val, timespec, uaddr2, utime as u32, val3)
1141             }
1142 
1143             SYS_READV => Self::readv(args[0] as i32, args[1], args[2]),
1144             SYS_WRITEV => Self::writev(args[0] as i32, args[1], args[2]),
1145 
1146             SYS_ARCH_PRCTL => Self::arch_prctl(args[0], args[1]),
1147 
1148             SYS_SET_TID_ADDR => Self::set_tid_address(args[0]),
1149 
1150             SYS_STAT | SYS_LSTAT => {
1151                 let path: &CStr = unsafe { CStr::from_ptr(args[0] as *const c_char) };
1152                 let path: Result<&str, core::str::Utf8Error> = path.to_str();
1153                 let res = if path.is_err() {
1154                     Err(SystemError::EINVAL)
1155                 } else {
1156                     let path: &str = path.unwrap();
1157                     let kstat = args[1] as *mut PosixKstat;
1158                     let vaddr = VirtAddr::new(kstat as usize);
1159                     match verify_area(vaddr, core::mem::size_of::<PosixKstat>()) {
1160                         Ok(_) => {
1161                             if syscall_num == SYS_STAT {
1162                                 Self::stat(path, kstat)
1163                             } else {
1164                                 Self::lstat(path, kstat)
1165                             }
1166                         }
1167                         Err(e) => Err(e),
1168                     }
1169                 };
1170 
1171                 res
1172             }
1173 
1174             // 目前为了适配musl-libc,以下系统调用先这样写着
1175             SYS_GET_RANDOM => {
1176                 let flags = GRandFlags::from_bits(args[2] as u8).ok_or(SystemError::EINVAL)?;
1177                 Self::get_random(args[0] as *mut u8, args[1], flags)
1178             }
1179 
1180             SYS_SOCKET_PAIR => {
1181                 unimplemented!()
1182             }
1183 
1184             SYS_POLL => {
1185                 kwarn!("SYS_POLL has not yet been implemented");
1186                 Ok(0)
1187             }
1188 
1189             SYS_RT_SIGPROCMASK => {
1190                 kwarn!("SYS_RT_SIGPROCMASK has not yet been implemented");
1191                 Ok(0)
1192             }
1193 
1194             SYS_TKILL => {
1195                 kwarn!("SYS_TKILL has not yet been implemented");
1196                 Ok(0)
1197             }
1198 
1199             SYS_SIGALTSTACK => {
1200                 kwarn!("SYS_SIGALTSTACK has not yet been implemented");
1201                 Ok(0)
1202             }
1203 
1204             SYS_EXIT_GROUP => {
1205                 kwarn!("SYS_EXIT_GROUP has not yet been implemented");
1206                 Ok(0)
1207             }
1208 
1209             SYS_MADVISE => {
1210                 kwarn!("SYS_MADVISE has not yet been implemented");
1211                 Ok(0)
1212             }
1213             SYS_GETTID => Self::gettid().map(|tid| tid.into()),
1214             SYS_GETUID => Self::getuid().map(|uid| uid.into()),
1215             SYS_SYSLOG => {
1216                 kwarn!("SYS_SYSLOG has not yet been implemented");
1217                 Ok(0)
1218             }
1219             SYS_GETGID => Self::getgid().map(|gid| gid.into()),
1220             SYS_SETUID => {
1221                 kwarn!("SYS_SETUID has not yet been implemented");
1222                 Ok(0)
1223             }
1224             SYS_SETGID => {
1225                 kwarn!("SYS_SETGID has not yet been implemented");
1226                 Ok(0)
1227             }
1228             SYS_GETEUID => Self::geteuid().map(|euid| euid.into()),
1229             SYS_GETEGID => Self::getegid().map(|egid| egid.into()),
1230             SYS_GETRUSAGE => {
1231                 let who = args[0] as c_int;
1232                 let rusage = args[1] as *mut RUsage;
1233                 Self::get_rusage(who, rusage)
1234             }
1235 
1236             SYS_READLINK => {
1237                 let path = args[0] as *const u8;
1238                 let buf = args[1] as *mut u8;
1239                 let bufsiz = args[2] as usize;
1240                 Self::readlink(path, buf, bufsiz)
1241             }
1242 
1243             SYS_READLINK_AT => {
1244                 let dirfd = args[0] as i32;
1245                 let pathname = args[1] as *const u8;
1246                 let buf = args[2] as *mut u8;
1247                 let bufsiz = args[3] as usize;
1248                 Self::readlink_at(dirfd, pathname, buf, bufsiz)
1249             }
1250 
1251             SYS_PRLIMIT64 => {
1252                 let pid = args[0];
1253                 let pid = Pid::new(pid);
1254                 let resource = args[1];
1255                 let new_limit = args[2] as *const RLimit64;
1256                 let old_limit = args[3] as *mut RLimit64;
1257 
1258                 Self::prlimit64(pid, resource, new_limit, old_limit)
1259             }
1260 
1261             SYS_ACCESS => {
1262                 let pathname = args[0] as *const u8;
1263                 let mode = args[1] as u32;
1264                 Self::access(pathname, mode)
1265             }
1266 
1267             SYS_FACCESSAT => {
1268                 let dirfd = args[0] as i32;
1269                 let pathname = args[1] as *const u8;
1270                 let mode = args[2] as u32;
1271                 Self::faccessat2(dirfd, pathname, mode, 0)
1272             }
1273 
1274             SYS_FACCESSAT2 => {
1275                 let dirfd = args[0] as i32;
1276                 let pathname = args[1] as *const u8;
1277                 let mode = args[2] as u32;
1278                 let flags = args[3] as u32;
1279                 Self::faccessat2(dirfd, pathname, mode, flags)
1280             }
1281 
1282             SYS_CLOCK_GETTIME => {
1283                 let clockid = args[0] as i32;
1284                 let timespec = args[1] as *mut TimeSpec;
1285                 Self::clock_gettime(clockid, timespec)
1286             }
1287 
1288             SYS_SYSINFO => {
1289                 let info = args[0] as *mut SysInfo;
1290                 Self::sysinfo(info)
1291             }
1292 
1293             SYS_UMASK => {
1294                 let mask = args[0] as u32;
1295                 Self::umask(mask)
1296             }
1297 
1298             SYS_CHMOD => {
1299                 let pathname = args[0] as *const u8;
1300                 let mode = args[1] as u32;
1301                 Self::chmod(pathname, mode)
1302             }
1303             SYS_FCHMOD => {
1304                 let fd = args[0] as i32;
1305                 let mode = args[1] as u32;
1306                 Self::fchmod(fd, mode)
1307             }
1308             SYS_FCHMODAT => {
1309                 let dirfd = args[0] as i32;
1310                 let pathname = args[1] as *const u8;
1311                 let mode = args[2] as u32;
1312                 Self::fchmodat(dirfd, pathname, mode)
1313             }
1314 
1315             _ => panic!("Unsupported syscall ID: {}", syscall_num),
1316         };
1317         return r;
1318     }
1319 
1320     pub fn put_string(
1321         s: *const u8,
1322         front_color: u32,
1323         back_color: u32,
1324     ) -> Result<usize, SystemError> {
1325         return Ok(unsafe { do_put_string(s, front_color, back_color) });
1326     }
1327 
1328     pub fn reboot() -> Result<usize, SystemError> {
1329         cpu_reset();
1330     }
1331 }
1332