xref: /DragonOS/kernel/src/syscall/mod.rs (revision 9b0abe6da72176086c3188e0599fda950562668f)
1ab5c8ca4Slogin use core::{
2ab5c8ca4Slogin     ffi::{c_char, c_int, c_void, CStr},
3ab5c8ca4Slogin     sync::atomic::{AtomicBool, Ordering},
4ab5c8ca4Slogin };
5ab5c8ca4Slogin 
6971462beSGnoCiYeH use crate::{
7*9b0abe6dSLoGin     arch::syscall::{SYS_ACCESS, SYS_FACCESSAT, SYS_FACCESSAT2, SYS_PRLIMIT64},
8971462beSGnoCiYeH     libs::{futex::constant::FutexFlag, rand::GRandFlags},
90d9b7d92SLoGin     process::{
100d9b7d92SLoGin         fork::KernelCloneArgs,
110d9b7d92SLoGin         resource::{RLimit64, RUsage},
120d9b7d92SLoGin     },
13971462beSGnoCiYeH };
1440314b30SXiaoye Zheng 
15676b8ef6SMork use num_traits::{FromPrimitive, ToPrimitive};
16676b8ef6SMork 
17ab5c8ca4Slogin use crate::{
181496ba7bSLoGin     arch::{cpu::cpu_reset, interrupt::TrapFrame, MMArch},
192dbef785SGnoCiYeH     driver::base::{block::SeekFrom, device::DeviceNumber},
20ab5c8ca4Slogin     filesystem::vfs::{
216d81180bSLoGin         fcntl::FcntlCommand,
22ab5c8ca4Slogin         file::FileMode,
232dbef785SGnoCiYeH         syscall::{ModeType, PosixKstat, SEEK_CUR, SEEK_END, SEEK_MAX, SEEK_SET},
24ab5c8ca4Slogin         MAX_PATHLEN,
25ab5c8ca4Slogin     },
261496ba7bSLoGin     include::bindings::bindings::{PAGE_2M_SIZE, PAGE_4K_SIZE},
27aa0367d6SLoGin     kinfo,
2840fe15e0SLoGin     libs::align::page_align_up,
29be63f3b2Shoumkh     mm::{verify_area, MemoryManagementArch, VirtAddr},
30ab5c8ca4Slogin     net::syscall::SockAddr,
31971462beSGnoCiYeH     process::{fork::CloneFlags, Pid},
3236fd0130Shoumkh     time::{
3340fe15e0SLoGin         syscall::{PosixTimeZone, PosixTimeval},
3436fd0130Shoumkh         TimeSpec,
3536fd0130Shoumkh     },
36ab5c8ca4Slogin };
37ab5c8ca4Slogin 
389e9ffedfSPlucky923 use self::user_access::{UserBufferReader, UserBufferWriter};
394537ffb7SChiichen 
4040fe15e0SLoGin pub mod user_access;
4140fe15e0SLoGin 
424454d1a2Slogin #[repr(i32)]
43676b8ef6SMork #[derive(Debug, FromPrimitive, ToPrimitive, PartialEq, Eq, Clone)]
4479a452ceShoumkh #[allow(dead_code, non_camel_case_types)]
454454d1a2Slogin pub enum SystemError {
4670a4e555SLoGin     /// 操作不被允许 Operation not permitted.
4779a452ceShoumkh     EPERM = 1,
4879a452ceShoumkh     /// 没有指定的文件或目录 No such file or directory.
4979a452ceShoumkh     ENOENT = 2,
504454d1a2Slogin     /// 没有这样的进程 No such process.
5179a452ceShoumkh     ESRCH = 3,
5279a452ceShoumkh     /// 被中断的函数 Interrupted function.
5379a452ceShoumkh     EINTR = 4,
5479a452ceShoumkh     /// I/O错误 I/O error.
5579a452ceShoumkh     EIO = 5,
5679a452ceShoumkh     /// 没有这样的设备或地址 No such device or address.
5779a452ceShoumkh     ENXIO = 6,
5879a452ceShoumkh     /// 参数列表过长,或者在输出buffer中缺少空间 或者参数比系统内建的最大值要大 Argument list too long.
5979a452ceShoumkh     E2BIG = 7,
6079a452ceShoumkh     /// 可执行文件格式错误 Executable file format error
6179a452ceShoumkh     ENOEXEC = 8,
6279a452ceShoumkh     /// 错误的文件描述符 Bad file descriptor.
6379a452ceShoumkh     EBADF = 9,
6479a452ceShoumkh     /// 没有子进程 No child processes.
6579a452ceShoumkh     ECHILD = 10,
6679a452ceShoumkh     /// 资源不可用,请重试。 Resource unavailable, try again.(may be the same value as [EWOULDBLOCK])
6779a452ceShoumkh     ///
6879a452ceShoumkh     /// 操作将被禁止 Operation would block.(may be the same value as [EAGAIN]).
6979a452ceShoumkh     EAGAIN_OR_EWOULDBLOCK = 11,
7079a452ceShoumkh     /// 没有足够的空间 Not enough space.
7179a452ceShoumkh     ENOMEM = 12,
7279a452ceShoumkh     /// 访问被拒绝 Permission denied
7379a452ceShoumkh     EACCES = 13,
7479a452ceShoumkh     /// 错误的地址 Bad address
7579a452ceShoumkh     EFAULT = 14,
7679a452ceShoumkh     /// 需要块设备 Block device required
7779a452ceShoumkh     ENOTBLK = 15,
7879a452ceShoumkh     /// 设备或资源忙 Device or resource busy.
7979a452ceShoumkh     EBUSY = 16,
8079a452ceShoumkh     /// 文件已存在 File exists.
8179a452ceShoumkh     EEXIST = 17,
824454d1a2Slogin     /// 跨设备连接 Cross-device link.
8379a452ceShoumkh     EXDEV = 18,
8479a452ceShoumkh     /// 没有指定的设备 No such device.
8579a452ceShoumkh     ENODEV = 19,
8679a452ceShoumkh     /// 不是目录 Not a directory.
8779a452ceShoumkh     ENOTDIR = 20,
8879a452ceShoumkh     /// 是一个目录 Is a directory
8979a452ceShoumkh     EISDIR = 21,
9079a452ceShoumkh     /// 不可用的参数 Invalid argument.
9179a452ceShoumkh     EINVAL = 22,
9279a452ceShoumkh     /// 系统中打开的文件过多 Too many files open in system.
9379a452ceShoumkh     ENFILE = 23,
9479a452ceShoumkh     /// 文件描述符的值过大 File descriptor value too large.
9579a452ceShoumkh     EMFILE = 24,
9679a452ceShoumkh     /// 不正确的I/O控制操作 Inappropriate I/O control operation.
9779a452ceShoumkh     ENOTTY = 25,
9879a452ceShoumkh     /// 文本文件忙 Text file busy.
9979a452ceShoumkh     ETXTBSY = 26,
10079a452ceShoumkh     /// 文件太大 File too large.
10179a452ceShoumkh     EFBIG = 27,
10279a452ceShoumkh     /// 设备上没有空间 No space left on device.
10379a452ceShoumkh     ENOSPC = 28,
10479a452ceShoumkh     /// 错误的寻道.当前文件是pipe,不允许seek请求  Invalid seek.
10579a452ceShoumkh     ESPIPE = 29,
10679a452ceShoumkh     /// 只读的文件系统 Read-only file system.
10779a452ceShoumkh     EROFS = 30,
10879a452ceShoumkh     /// 链接数过多 Too many links.
10979a452ceShoumkh     EMLINK = 31,
11079a452ceShoumkh     /// 断开的管道 Broken pipe.
11179a452ceShoumkh     EPIPE = 32,
11279a452ceShoumkh     /// 数学参数超出作用域 Mathematics argument out of domain of function.
11379a452ceShoumkh     EDOM = 33,
11479a452ceShoumkh     /// 结果过大 Result too large.
11579a452ceShoumkh     ERANGE = 34,
11679a452ceShoumkh     /// 资源死锁将要发生 Resource deadlock would occur.
11779a452ceShoumkh     EDEADLK = 35,
11879a452ceShoumkh     /// 文件名过长 Filename too long.
11979a452ceShoumkh     ENAMETOOLONG = 36,
12079a452ceShoumkh     /// 没有可用的锁 No locks available.
12179a452ceShoumkh     ENOLCK = 37,
12279a452ceShoumkh     /// 功能不支持 Function not supported.
12379a452ceShoumkh     ENOSYS = 38,
12479a452ceShoumkh     /// 目录非空 Directory not empty.
12579a452ceShoumkh     ENOTEMPTY = 39,
12679a452ceShoumkh     /// 符号链接级别过多 Too many levels of symbolic links.
12779a452ceShoumkh     ELOOP = 40,
12879a452ceShoumkh     /// 没有期待类型的消息 No message of the desired type.
12979a452ceShoumkh     ENOMSG = 41,
13079a452ceShoumkh     /// 标志符被移除 Identifier removed.
13179a452ceShoumkh     EIDRM = 42,
13279a452ceShoumkh     /// 通道号超出范围 Channel number out of range
13379a452ceShoumkh     ECHRNG = 43,
13479a452ceShoumkh     /// 二级不同步 Level 2 not synchronized
13579a452ceShoumkh     EL2NSYNC = 44,
13679a452ceShoumkh     /// 三级暂停 Level 3 halted
13779a452ceShoumkh     EL3HLT = 45,
13879a452ceShoumkh     /// 三级重置 Level 3 reset
13979a452ceShoumkh     EL3RST = 46,
14079a452ceShoumkh     /// 链接号超出范围 Link number out of range
14179a452ceShoumkh     ELNRNG = 47,
14279a452ceShoumkh     /// 未连接协议驱动程序 Protocol driver not attached
14379a452ceShoumkh     EUNATCH = 48,
14479a452ceShoumkh     /// 没有可用的CSI结构 No CSI structure available
14579a452ceShoumkh     ENOCSI = 49,
14679a452ceShoumkh     /// 二级暂停 Level 2 halted
14779a452ceShoumkh     EL2HLT = 50,
14879a452ceShoumkh     /// 无效交换 Invalid exchange
14979a452ceShoumkh     EBADE = 51,
15079a452ceShoumkh     /// 无效的请求描述符 Invalid request descriptor
15179a452ceShoumkh     EBADR = 52,
15279a452ceShoumkh     /// 交换满 Exchange full
15379a452ceShoumkh     EXFULL = 53,
15479a452ceShoumkh     /// 无阳极 No anode
15579a452ceShoumkh     ENOANO = 54,
15679a452ceShoumkh     /// 请求码无效 Invalid request code
15779a452ceShoumkh     EBADRQC = 55,
15879a452ceShoumkh     /// 无效插槽 Invalid slot
15979a452ceShoumkh     EBADSLT = 56,
16079a452ceShoumkh     /// 资源死锁 Resource deadlock would occur
16179a452ceShoumkh     EDEADLOCK = 57,
16279a452ceShoumkh     /// 错误的字体文件格式 Bad font file format
16379a452ceShoumkh     EBFONT = 58,
16479a452ceShoumkh     /// 不是STREAM Not a STREAM
16579a452ceShoumkh     ENOSTR = 59,
16679a452ceShoumkh     /// 队列头没有可读取的消息 No message is available on the STREAM head read queue.
16779a452ceShoumkh     ENODATA = 60,
16879a452ceShoumkh     /// 流式ioctl()超时 Stream ioctl() timeout
16979a452ceShoumkh     ETIME = 61,
17079a452ceShoumkh     /// 没有STREAM资源  No STREAM resources.
17179a452ceShoumkh     ENOSR = 62,
17279a452ceShoumkh     /// 机器不在网络上 Machine is not on the network
17379a452ceShoumkh     ENONET = 63,
17479a452ceShoumkh     /// 未安装软件包 Package not installed
17579a452ceShoumkh     ENOPKG = 64,
17679a452ceShoumkh     /// 远程对象 Object is remote
17779a452ceShoumkh     EREMOTE = 65,
17879a452ceShoumkh     /// 保留 Reserved.
17979a452ceShoumkh     ENOLINK = 66,
18079a452ceShoumkh     /// 外设错误 Advertise error.
18179a452ceShoumkh     EADV = 67,
18279a452ceShoumkh     /// 安装错误 Srmount error
18379a452ceShoumkh     ESRMNT = 68,
18479a452ceShoumkh     /// 发送时发生通信错误 Communication error on send
18579a452ceShoumkh     ECOMM = 69,
18679a452ceShoumkh     /// 协议错误 Protocol error.
18779a452ceShoumkh     EPROTO = 70,
18879a452ceShoumkh     /// 保留使用 Reserved.
18979a452ceShoumkh     EMULTIHOP = 71,
19079a452ceShoumkh     /// RFS特定错误 RFS specific error
19179a452ceShoumkh     EDOTDOT = 72,
19279a452ceShoumkh     /// 错误的消息 Bad message.
19379a452ceShoumkh     EBADMSG = 73,
19479a452ceShoumkh     /// 数值过大,产生溢出 Value too large to be stored in data type.
19579a452ceShoumkh     EOVERFLOW = 74,
19679a452ceShoumkh     /// 名称在网络上不是唯一的 Name not unique on network
19779a452ceShoumkh     ENOTUNIQ = 75,
19879a452ceShoumkh     /// 处于不良状态的文件描述符 File descriptor in bad state
19979a452ceShoumkh     EBADFD = 76,
20079a452ceShoumkh     /// 远程地址已更改 Remote address changed
20179a452ceShoumkh     EREMCHG = 77,
20279a452ceShoumkh     /// 无法访问所需的共享库 Can not access a needed shared library
20379a452ceShoumkh     ELIBACC = 78,
20479a452ceShoumkh     /// 访问损坏的共享库 Accessing a corrupted shared library
20579a452ceShoumkh     ELIBBAD = 79,
20679a452ceShoumkh     /// a. out中的.lib部分已损坏 .lib section in a.out corrupted
20779a452ceShoumkh     ELIBSCN = 80,
20879a452ceShoumkh     /// 尝试链接太多共享库 Attempting to link in too many shared libraries
20979a452ceShoumkh     ELIBMAX = 81,
21079a452ceShoumkh     /// 无法直接执行共享库 Cannot exec a shared library directly
21179a452ceShoumkh     ELIBEXEC = 82,
21279a452ceShoumkh     /// 不合法的字符序列 Illegal byte sequence.
21379a452ceShoumkh     EILSEQ = 83,
21479a452ceShoumkh     /// 中断的系统调用应该重新启动 Interrupted system call should be restarted
21579a452ceShoumkh     ERESTART = 84,
21679a452ceShoumkh     /// 流管道错误 Streams pipe error
21779a452ceShoumkh     ESTRPIPE = 85,
21879a452ceShoumkh     /// 用户太多 Too many users
21979a452ceShoumkh     EUSERS = 86,
22079a452ceShoumkh     /// 不是一个套接字 Not a socket.
22179a452ceShoumkh     ENOTSOCK = 87,
22279a452ceShoumkh     /// 需要目标地址 Destination address required.
22379a452ceShoumkh     EDESTADDRREQ = 88,
22479a452ceShoumkh     /// 消息过大 Message too large.
22579a452ceShoumkh     EMSGSIZE = 89,
22679a452ceShoumkh     /// 对于套接字而言,错误的协议 Protocol wrong type for socket.
22779a452ceShoumkh     EPROTOTYPE = 90,
22879a452ceShoumkh     /// 协议不可用 Protocol not available.
22979a452ceShoumkh     ENOPROTOOPT = 91,
23079a452ceShoumkh     /// 协议不被支持 Protocol not supported.
23179a452ceShoumkh     EPROTONOSUPPORT = 92,
23279a452ceShoumkh     /// 不支持套接字类型 Socket type not supported
23379a452ceShoumkh     ESOCKTNOSUPPORT = 93,
23479a452ceShoumkh     /// 套接字不支持该操作 Operation not supported on socket (may be the same value as [ENOTSUP]).
23579a452ceShoumkh     ///
23679a452ceShoumkh     /// 不被支持 Not supported (may be the same value as [EOPNOTSUPP]).
23779a452ceShoumkh     EOPNOTSUPP_OR_ENOTSUP = 94,
23879a452ceShoumkh     /// 不支持协议系列 Protocol family not supported
23979a452ceShoumkh     EPFNOSUPPORT = 95,
24079a452ceShoumkh     /// 地址family不支持 Address family not supported.
24179a452ceShoumkh     EAFNOSUPPORT = 96,
24279a452ceShoumkh     /// 地址正在被使用 Address in use.
24379a452ceShoumkh     EADDRINUSE = 97,
24479a452ceShoumkh     /// 地址不可用 Address  not available.
24579a452ceShoumkh     EADDRNOTAVAIL = 98,
24679a452ceShoumkh     /// 网络已关闭 Network is down.
24779a452ceShoumkh     ENETDOWN = 99,
24879a452ceShoumkh     /// 网络不可达 Network unreachable.
24979a452ceShoumkh     ENETUNREACH = 100,
25079a452ceShoumkh     /// 网络连接已断开 Connection aborted by network.
25179a452ceShoumkh     ENETRESET = 101,
25279a452ceShoumkh     /// 连接已断开 Connection aborted.
25379a452ceShoumkh     ECONNABORTED = 102,
25479a452ceShoumkh     /// 连接被重置 Connection reset.
25579a452ceShoumkh     ECONNRESET = 103,
25679a452ceShoumkh     /// 缓冲区空间不足 No buffer space available.
25779a452ceShoumkh     ENOBUFS = 104,
25879a452ceShoumkh     /// 套接字已连接 Socket is connected.
25979a452ceShoumkh     EISCONN = 105,
26079a452ceShoumkh     /// 套接字未连接 The socket is not connected.
26179a452ceShoumkh     ENOTCONN = 106,
26279a452ceShoumkh     /// 传输端点关闭后无法发送 Cannot send after transport endpoint shutdown
26379a452ceShoumkh     ESHUTDOWN = 107,
26479a452ceShoumkh     /// 引用太多:无法拼接 Too many references: cannot splice
26579a452ceShoumkh     ETOOMANYREFS = 108,
26679a452ceShoumkh     /// 连接超时 Connection timed out.
26779a452ceShoumkh     ETIMEDOUT = 109,
26879a452ceShoumkh     /// 连接被拒绝 Connection refused.
26979a452ceShoumkh     ECONNREFUSED = 110,
27079a452ceShoumkh     /// 主机已关闭 Host is down
27179a452ceShoumkh     EHOSTDOWN = 111,
27279a452ceShoumkh     /// 主机不可达 Host is unreachable.
27379a452ceShoumkh     EHOSTUNREACH = 112,
27479a452ceShoumkh     /// 连接已经在处理 Connection already in progress.
27579a452ceShoumkh     EALREADY = 113,
27679a452ceShoumkh     /// 操作正在处理 Operation in progress.
27779a452ceShoumkh     EINPROGRESS = 114,
27879a452ceShoumkh     /// 保留 Reserved.
27979a452ceShoumkh     ESTALE = 115,
28079a452ceShoumkh     /// 结构需要清理 Structure needs cleaning
28179a452ceShoumkh     EUCLEAN = 116,
28279a452ceShoumkh     /// 不是XENIX命名类型文件 Not a XENIX named type file
28379a452ceShoumkh     ENOTNAM = 117,
28479a452ceShoumkh     /// 没有可用的XENIX信号量 No XENIX semaphores available
28579a452ceShoumkh     ENAVAIL = 118,
28679a452ceShoumkh     /// 是命名类型文件 Is a named type file
28779a452ceShoumkh     EISNAM = 119,
28879a452ceShoumkh     /// 远程I/O错误 Remote I/O error
28979a452ceShoumkh     EREMOTEIO = 120,
29079a452ceShoumkh     /// 保留使用 Reserved
29179a452ceShoumkh     EDQUOT = 121,
29279a452ceShoumkh     /// 没有找到媒介 No medium found
29379a452ceShoumkh     ENOMEDIUM = 122,
29479a452ceShoumkh     /// 介质类型错误 Wrong medium type
29579a452ceShoumkh     EMEDIUMTYPE = 123,
29679a452ceShoumkh     /// 操作被取消 Operation canceled.
29779a452ceShoumkh     ECANCELED = 124,
29879a452ceShoumkh     /// 所需的密钥不可用 Required key not available
29979a452ceShoumkh     ENOKEY = 125,
30079a452ceShoumkh     /// 密钥已过期 Key has expired
30179a452ceShoumkh     EKEYEXPIRED = 126,
30279a452ceShoumkh     /// 密钥已被撤销 Key has been revoked
30379a452ceShoumkh     EKEYREVOKED = 127,
30479a452ceShoumkh     /// 密钥被服务拒绝 Key has been revoked
30579a452ceShoumkh     EKEYREJECTED = 128,
30679a452ceShoumkh     /// 之前的拥有者挂了 Previous owner died.
30779a452ceShoumkh     EOWNERDEAD = 129,
30879a452ceShoumkh     /// 状态不可恢复 State not recoverable.
30979a452ceShoumkh     ENOTRECOVERABLE = 130,
31040314b30SXiaoye Zheng     // VMX on 虚拟化开启指令出错
31140314b30SXiaoye Zheng     EVMXONFailed = 131,
31240314b30SXiaoye Zheng     // VMX off 虚拟化关闭指令出错
31340314b30SXiaoye Zheng     EVMXOFFFailed = 132,
31440314b30SXiaoye Zheng     // VMX VMWRITE 写入虚拟化VMCS内存出错
31540314b30SXiaoye Zheng     EVMWRITEFailed = 133,
31640314b30SXiaoye Zheng     EVMREADFailed = 134,
31740314b30SXiaoye Zheng     EVMPRTLDFailed = 135,
31840314b30SXiaoye Zheng     EVMLAUNCHFailed = 136,
31940314b30SXiaoye Zheng     KVM_HVA_ERR_BAD = 137,
3204454d1a2Slogin }
321676b8ef6SMork 
322676b8ef6SMork impl SystemError {
323676b8ef6SMork     /// @brief 把posix错误码转换为系统错误枚举类型。
324676b8ef6SMork     pub fn from_posix_errno(errno: i32) -> Option<SystemError> {
325676b8ef6SMork         // posix 错误码是小于0的
326676b8ef6SMork         if errno >= 0 {
327676b8ef6SMork             return None;
328676b8ef6SMork         }
329676b8ef6SMork         return <Self as FromPrimitive>::from_i32(-errno);
330676b8ef6SMork     }
331676b8ef6SMork 
332676b8ef6SMork     /// @brief 把系统错误枚举类型转换为负数posix错误码。
333676b8ef6SMork     pub fn to_posix_errno(&self) -> i32 {
334676b8ef6SMork         return -<Self as ToPrimitive>::to_i32(self).unwrap();
335676b8ef6SMork     }
336676b8ef6SMork }
337ab5c8ca4Slogin 
338ab5c8ca4Slogin // 定义系统调用号
339ad1d649eSGnoCiYeH pub const SYS_READ: usize = 0;
340ad1d649eSGnoCiYeH pub const SYS_WRITE: usize = 1;
341ab5c8ca4Slogin pub const SYS_OPEN: usize = 2;
342ab5c8ca4Slogin pub const SYS_CLOSE: usize = 3;
343ad1d649eSGnoCiYeH pub const SYS_STAT: usize = 4;
344ad1d649eSGnoCiYeH pub const SYS_FSTAT: usize = 5;
345ab5c8ca4Slogin 
346ad1d649eSGnoCiYeH pub const SYS_POLL: usize = 7;
347ad1d649eSGnoCiYeH pub const SYS_LSEEK: usize = 8;
348ad1d649eSGnoCiYeH pub const SYS_MMAP: usize = 9;
349ad1d649eSGnoCiYeH pub const SYS_MPROTECT: usize = 10;
350c47fe904SLoGin 
351ad1d649eSGnoCiYeH pub const SYS_MUNMAP: usize = 11;
352ad1d649eSGnoCiYeH pub const SYS_BRK: usize = 12;
353ad1d649eSGnoCiYeH pub const SYS_SIGACTION: usize = 13;
354ad1d649eSGnoCiYeH pub const SYS_RT_SIGPROCMASK: usize = 14;
355ad1d649eSGnoCiYeH pub const SYS_RT_SIGRETURN: usize = 15;
356c47fe904SLoGin 
357ad1d649eSGnoCiYeH pub const SYS_IOCTL: usize = 16;
358ad1d649eSGnoCiYeH 
359ad1d649eSGnoCiYeH pub const SYS_WRITEV: usize = 20;
360ad1d649eSGnoCiYeH 
36104babc3fSMemoryShore pub const SYS_MADVISE: usize = 28;
36204babc3fSMemoryShore 
363ad1d649eSGnoCiYeH pub const SYS_DUP: usize = 32;
364ad1d649eSGnoCiYeH pub const SYS_DUP2: usize = 33;
365ad1d649eSGnoCiYeH 
366ad1d649eSGnoCiYeH pub const SYS_NANOSLEEP: usize = 35;
367ad1d649eSGnoCiYeH 
368ad1d649eSGnoCiYeH pub const SYS_GETPID: usize = 39;
369ad1d649eSGnoCiYeH 
370ad1d649eSGnoCiYeH pub const SYS_SOCKET: usize = 41;
371ad1d649eSGnoCiYeH pub const SYS_CONNECT: usize = 42;
372ad1d649eSGnoCiYeH pub const SYS_ACCEPT: usize = 43;
373ad1d649eSGnoCiYeH pub const SYS_SENDTO: usize = 44;
374ad1d649eSGnoCiYeH pub const SYS_RECVFROM: usize = 45;
375ad1d649eSGnoCiYeH 
376ad1d649eSGnoCiYeH pub const SYS_RECVMSG: usize = 47;
377ad1d649eSGnoCiYeH pub const SYS_SHUTDOWN: usize = 48;
378ad1d649eSGnoCiYeH pub const SYS_BIND: usize = 49;
379ad1d649eSGnoCiYeH pub const SYS_LISTEN: usize = 50;
380ad1d649eSGnoCiYeH pub const SYS_GETSOCKNAME: usize = 51;
381ad1d649eSGnoCiYeH pub const SYS_GETPEERNAME: usize = 52;
382971462beSGnoCiYeH pub const SYS_SOCKET_PAIR: usize = 53;
383ad1d649eSGnoCiYeH pub const SYS_SETSOCKOPT: usize = 54;
384ad1d649eSGnoCiYeH pub const SYS_GETSOCKOPT: usize = 55;
385ad1d649eSGnoCiYeH 
386ad1d649eSGnoCiYeH #[allow(dead_code)]
387ad1d649eSGnoCiYeH pub const SYS_CLONE: usize = 56;
388ad1d649eSGnoCiYeH pub const SYS_FORK: usize = 57;
389ad1d649eSGnoCiYeH pub const SYS_VFORK: usize = 58;
390ad1d649eSGnoCiYeH pub const SYS_EXECVE: usize = 59;
391ad1d649eSGnoCiYeH pub const SYS_EXIT: usize = 60;
392ad1d649eSGnoCiYeH pub const SYS_WAIT4: usize = 61;
393ad1d649eSGnoCiYeH pub const SYS_KILL: usize = 62;
394ad1d649eSGnoCiYeH 
395ad1d649eSGnoCiYeH pub const SYS_FCNTL: usize = 72;
396ad1d649eSGnoCiYeH 
397ad1d649eSGnoCiYeH pub const SYS_FTRUNCATE: usize = 77;
398ad1d649eSGnoCiYeH pub const SYS_GET_DENTS: usize = 78;
399ad1d649eSGnoCiYeH 
400ad1d649eSGnoCiYeH pub const SYS_GETCWD: usize = 79;
401ad1d649eSGnoCiYeH 
402ad1d649eSGnoCiYeH pub const SYS_CHDIR: usize = 80;
403ad1d649eSGnoCiYeH 
404ad1d649eSGnoCiYeH pub const SYS_MKDIR: usize = 83;
405ad1d649eSGnoCiYeH 
406709498caSLoGin pub const SYS_READLINK: usize = 89;
407709498caSLoGin 
408ad1d649eSGnoCiYeH pub const SYS_GETTIMEOFDAY: usize = 96;
409be8cdf4bSLoGin pub const SYS_GETRUSAGE: usize = 98;
410ad1d649eSGnoCiYeH 
41102e249f3SLoGin pub const SYS_GETUID: usize = 102;
41202e249f3SLoGin pub const SYS_SYSLOG: usize = 103;
41302e249f3SLoGin pub const SYS_GETGID: usize = 104;
41402e249f3SLoGin pub const SYS_SETUID: usize = 105;
41502e249f3SLoGin 
41602e249f3SLoGin pub const SYS_SETGID: usize = 106;
41702e249f3SLoGin pub const SYS_GETEUID: usize = 107;
41802e249f3SLoGin pub const SYS_GETEGID: usize = 108;
41902e249f3SLoGin 
420c47fe904SLoGin pub const SYS_GETPPID: usize = 110;
421c47fe904SLoGin pub const SYS_GETPGID: usize = 121;
422ad1d649eSGnoCiYeH 
423c47fe904SLoGin pub const SYS_SIGALTSTACK: usize = 131;
424c47fe904SLoGin pub const SYS_MKNOD: usize = 133;
425c47fe904SLoGin 
426ad1d649eSGnoCiYeH pub const SYS_ARCH_PRCTL: usize = 158;
427ad1d649eSGnoCiYeH 
428ad1d649eSGnoCiYeH pub const SYS_REBOOT: usize = 169;
429ad1d649eSGnoCiYeH 
430393f6915SLoGin pub const SYS_GETTID: usize = 186;
431393f6915SLoGin 
432ad1d649eSGnoCiYeH #[allow(dead_code)]
433ad1d649eSGnoCiYeH pub const SYS_TKILL: usize = 200;
434ad1d649eSGnoCiYeH 
435ad1d649eSGnoCiYeH #[allow(dead_code)]
436ad1d649eSGnoCiYeH pub const SYS_FUTEX: usize = 202;
437ad1d649eSGnoCiYeH 
438ad1d649eSGnoCiYeH pub const SYS_GET_DENTS_64: usize = 217;
439ad1d649eSGnoCiYeH #[allow(dead_code)]
440ad1d649eSGnoCiYeH pub const SYS_SET_TID_ADDR: usize = 218;
441ad1d649eSGnoCiYeH 
44216033951SGnoCiYeH pub const SYS_EXIT_GROUP: usize = 231;
44316033951SGnoCiYeH 
444ad1d649eSGnoCiYeH pub const SYS_UNLINK_AT: usize = 263;
445ad1d649eSGnoCiYeH 
446709498caSLoGin pub const SYS_READLINK_AT: usize = 267;
447709498caSLoGin 
448c47fe904SLoGin pub const SYS_ACCEPT4: usize = 288;
449c47fe904SLoGin 
450ad1d649eSGnoCiYeH pub const SYS_PIPE: usize = 293;
451ad1d649eSGnoCiYeH 
452ad1d649eSGnoCiYeH #[allow(dead_code)]
453ad1d649eSGnoCiYeH pub const SYS_GET_RANDOM: usize = 318;
454ad1d649eSGnoCiYeH 
455ad1d649eSGnoCiYeH // 与linux不一致的调用,在linux基础上累加
456ad1d649eSGnoCiYeH pub const SYS_PUT_STRING: usize = 100000;
457ad1d649eSGnoCiYeH pub const SYS_SBRK: usize = 100001;
458ab5c8ca4Slogin /// todo: 该系统调用与Linux不一致,将来需要删除该系统调用!!! 删的时候记得改C版本的libc
459ad1d649eSGnoCiYeH pub const SYS_CLOCK: usize = 100002;
460ad1d649eSGnoCiYeH pub const SYS_SCHED: usize = 100003;
46140314b30SXiaoye Zheng 
462ab5c8ca4Slogin #[derive(Debug)]
463ab5c8ca4Slogin pub struct Syscall;
464ab5c8ca4Slogin 
465ab5c8ca4Slogin extern "C" {
466ab5c8ca4Slogin     fn do_put_string(s: *const u8, front_color: u32, back_color: u32) -> usize;
467ab5c8ca4Slogin }
468ab5c8ca4Slogin 
469ab5c8ca4Slogin #[no_mangle]
470ab5c8ca4Slogin pub extern "C" fn syscall_init() -> i32 {
471ab5c8ca4Slogin     kinfo!("Initializing syscall...");
472ab5c8ca4Slogin     Syscall::init().expect("syscall init failed");
473ab5c8ca4Slogin     kinfo!("Syscall init successfully!");
474ab5c8ca4Slogin     return 0;
475ab5c8ca4Slogin }
476ab5c8ca4Slogin 
477ab5c8ca4Slogin impl Syscall {
478ab5c8ca4Slogin     /// 初始化系统调用
479ab5c8ca4Slogin     pub fn init() -> Result<(), SystemError> {
480ab5c8ca4Slogin         static INIT_FLAG: AtomicBool = AtomicBool::new(false);
481ab5c8ca4Slogin         let prev = INIT_FLAG.swap(true, Ordering::SeqCst);
482ab5c8ca4Slogin         if prev {
483ab5c8ca4Slogin             panic!("Cannot initialize syscall more than once!");
484ab5c8ca4Slogin         }
485ab5c8ca4Slogin         return crate::arch::syscall::arch_syscall_init();
486ab5c8ca4Slogin     }
487ab5c8ca4Slogin     /// @brief 系统调用分发器,用于分发系统调用。
488ab5c8ca4Slogin     ///
489ab5c8ca4Slogin     /// 这个函数内,需要根据系统调用号,调用对应的系统调用处理函数。
490ab5c8ca4Slogin     /// 并且,对于用户态传入的指针参数,需要在本函数内进行越界检查,防止访问到内核空间。
4919e9ffedfSPlucky923     pub fn handle(
4929e9ffedfSPlucky923         syscall_num: usize,
4939e9ffedfSPlucky923         args: &[usize],
4949e9ffedfSPlucky923         frame: &mut TrapFrame,
4959e9ffedfSPlucky923     ) -> Result<usize, SystemError> {
496ab5c8ca4Slogin         let r = match syscall_num {
497ab5c8ca4Slogin             SYS_PUT_STRING => {
498ab5c8ca4Slogin                 Self::put_string(args[0] as *const u8, args[1] as u32, args[2] as u32)
499ab5c8ca4Slogin             }
500ab5c8ca4Slogin             SYS_OPEN => {
501ab5c8ca4Slogin                 let path: &CStr = unsafe { CStr::from_ptr(args[0] as *const c_char) };
502ab5c8ca4Slogin                 let path: Result<&str, core::str::Utf8Error> = path.to_str();
503ab5c8ca4Slogin                 let res = if path.is_err() {
504ab5c8ca4Slogin                     Err(SystemError::EINVAL)
505ab5c8ca4Slogin                 } else {
506ab5c8ca4Slogin                     let path: &str = path.unwrap();
50734e6d6c8Syuyi2439 
508ab5c8ca4Slogin                     let flags = args[1];
509ab5c8ca4Slogin                     let open_flags: FileMode = FileMode::from_bits_truncate(flags as u32);
510ab5c8ca4Slogin                     Self::open(path, open_flags)
511ab5c8ca4Slogin                 };
512ab5c8ca4Slogin                 res
513ab5c8ca4Slogin             }
514ab5c8ca4Slogin             SYS_CLOSE => {
515ab5c8ca4Slogin                 let fd = args[0];
51634e6d6c8Syuyi2439 
51734e6d6c8Syuyi2439                 let res = Self::close(fd);
51834e6d6c8Syuyi2439 
51934e6d6c8Syuyi2439                 res
520ab5c8ca4Slogin             }
521ab5c8ca4Slogin             SYS_READ => {
522ab5c8ca4Slogin                 let fd = args[0] as i32;
523ab5c8ca4Slogin                 let buf_vaddr = args[1];
524ab5c8ca4Slogin                 let len = args[2];
5259e9ffedfSPlucky923                 let from_user = frame.from_user();
5269e9ffedfSPlucky923                 let mut user_buffer_writer =
5279e9ffedfSPlucky923                     UserBufferWriter::new(buf_vaddr as *mut u8, len, from_user)?;
5286d81180bSLoGin 
5299e9ffedfSPlucky923                 let user_buf = user_buffer_writer.buffer(0)?;
5309e9ffedfSPlucky923                 let res = Self::read(fd, user_buf);
531ab5c8ca4Slogin                 res
532ab5c8ca4Slogin             }
533ab5c8ca4Slogin             SYS_WRITE => {
534ab5c8ca4Slogin                 let fd = args[0] as i32;
535ab5c8ca4Slogin                 let buf_vaddr = args[1];
536ab5c8ca4Slogin                 let len = args[2];
5379e9ffedfSPlucky923                 let from_user = frame.from_user();
5389e9ffedfSPlucky923                 let user_buffer_reader =
5399e9ffedfSPlucky923                     UserBufferReader::new(buf_vaddr as *const u8, len, from_user)?;
5406d81180bSLoGin 
5419e9ffedfSPlucky923                 let user_buf = user_buffer_reader.read_from_user(0)?;
5429e9ffedfSPlucky923                 let res = Self::write(fd, user_buf);
543ab5c8ca4Slogin                 res
544ab5c8ca4Slogin             }
545ab5c8ca4Slogin 
546ab5c8ca4Slogin             SYS_LSEEK => {
547ab5c8ca4Slogin                 let fd = args[0] as i32;
548ab5c8ca4Slogin                 let offset = args[1] as i64;
549ab5c8ca4Slogin                 let whence = args[2] as u32;
550ab5c8ca4Slogin 
551ab5c8ca4Slogin                 let w = match whence {
552ab5c8ca4Slogin                     SEEK_SET => Ok(SeekFrom::SeekSet(offset)),
553ab5c8ca4Slogin                     SEEK_CUR => Ok(SeekFrom::SeekCurrent(offset)),
554ab5c8ca4Slogin                     SEEK_END => Ok(SeekFrom::SeekEnd(offset)),
555ab5c8ca4Slogin                     SEEK_MAX => Ok(SeekFrom::SeekEnd(0)),
556ab5c8ca4Slogin                     _ => Err(SystemError::EINVAL),
5579e9ffedfSPlucky923                 }?;
558ab5c8ca4Slogin 
559ab5c8ca4Slogin                 Self::lseek(fd, w)
560ab5c8ca4Slogin             }
56140314b30SXiaoye Zheng             SYS_IOCTL => {
56240314b30SXiaoye Zheng                 let fd = args[0];
56340314b30SXiaoye Zheng                 let cmd = args[1];
56440314b30SXiaoye Zheng                 let data = args[2];
56540314b30SXiaoye Zheng                 Self::ioctl(fd, cmd as u32, data)
56640314b30SXiaoye Zheng             }
567ab5c8ca4Slogin 
5681496ba7bSLoGin             SYS_FORK => Self::fork(frame),
5691496ba7bSLoGin             SYS_VFORK => Self::vfork(frame),
5701496ba7bSLoGin 
571ab5c8ca4Slogin             SYS_BRK => {
57240fe15e0SLoGin                 let new_brk = VirtAddr::new(args[0]);
57340fe15e0SLoGin                 Self::brk(new_brk).map(|vaddr| vaddr.data())
574ab5c8ca4Slogin             }
575ab5c8ca4Slogin 
576ab5c8ca4Slogin             SYS_SBRK => {
577ab5c8ca4Slogin                 let increment = args[0] as isize;
5781496ba7bSLoGin                 Self::sbrk(increment).map(|vaddr: VirtAddr| vaddr.data())
579ab5c8ca4Slogin             }
580ab5c8ca4Slogin 
581ab5c8ca4Slogin             SYS_REBOOT => Self::reboot(),
582ab5c8ca4Slogin 
583ab5c8ca4Slogin             SYS_CHDIR => {
584ab5c8ca4Slogin                 // Closure for checking arguments
585ab5c8ca4Slogin                 let chdir_check = |arg0: usize| {
586ab5c8ca4Slogin                     if arg0 == 0 {
587ab5c8ca4Slogin                         return Err(SystemError::EFAULT);
588ab5c8ca4Slogin                     }
589ab5c8ca4Slogin                     let path_ptr = arg0 as *const c_char;
590be63f3b2Shoumkh                     let virt_addr = VirtAddr::new(path_ptr as usize);
591ab5c8ca4Slogin                     // 权限校验
592ab5c8ca4Slogin                     if path_ptr.is_null()
5931496ba7bSLoGin                         || (frame.from_user()
5941496ba7bSLoGin                             && verify_area(virt_addr, PAGE_2M_SIZE as usize).is_err())
595ab5c8ca4Slogin                     {
596ab5c8ca4Slogin                         return Err(SystemError::EINVAL);
597ab5c8ca4Slogin                     }
598ab5c8ca4Slogin                     let dest_path: &CStr = unsafe { CStr::from_ptr(path_ptr) };
599ab5c8ca4Slogin                     let dest_path: &str = dest_path.to_str().map_err(|_| SystemError::EINVAL)?;
600ab5c8ca4Slogin                     if dest_path.len() == 0 {
601ab5c8ca4Slogin                         return Err(SystemError::EINVAL);
6021496ba7bSLoGin                     } else if dest_path.len() > MAX_PATHLEN as usize {
603ab5c8ca4Slogin                         return Err(SystemError::ENAMETOOLONG);
604ab5c8ca4Slogin                     }
605ab5c8ca4Slogin 
606ab5c8ca4Slogin                     return Ok(dest_path);
607ab5c8ca4Slogin                 };
608ab5c8ca4Slogin 
6099e9ffedfSPlucky923                 let r = chdir_check(args[0])?;
6109e9ffedfSPlucky923                 Self::chdir(r)
611ab5c8ca4Slogin             }
612ab5c8ca4Slogin 
613ad1d649eSGnoCiYeH             SYS_GET_DENTS | SYS_GET_DENTS_64 => {
614ab5c8ca4Slogin                 let fd = args[0] as i32;
61534e6d6c8Syuyi2439 
616ab5c8ca4Slogin                 let buf_vaddr = args[1];
617ab5c8ca4Slogin                 let len = args[2];
618f5df0e79SLoGin                 let virt_addr: VirtAddr = VirtAddr::new(buf_vaddr);
619ab5c8ca4Slogin                 // 判断缓冲区是否来自用户态,进行权限校验
6201496ba7bSLoGin                 let res = if frame.from_user() && verify_area(virt_addr, len as usize).is_err() {
621ab5c8ca4Slogin                     // 来自用户态,而buffer在内核态,这样的操作不被允许
622ab5c8ca4Slogin                     Err(SystemError::EPERM)
623ab5c8ca4Slogin                 } else if buf_vaddr == 0 {
624ab5c8ca4Slogin                     Err(SystemError::EFAULT)
625ab5c8ca4Slogin                 } else {
626ab5c8ca4Slogin                     let buf: &mut [u8] = unsafe {
627ab5c8ca4Slogin                         core::slice::from_raw_parts_mut::<'static, u8>(buf_vaddr as *mut u8, len)
628ab5c8ca4Slogin                     };
629ab5c8ca4Slogin                     Self::getdents(fd, buf)
630ab5c8ca4Slogin                 };
631ab5c8ca4Slogin 
632ab5c8ca4Slogin                 res
633ab5c8ca4Slogin             }
634ab5c8ca4Slogin 
635ab5c8ca4Slogin             SYS_EXECVE => {
636ab5c8ca4Slogin                 let path_ptr = args[0];
637ab5c8ca4Slogin                 let argv_ptr = args[1];
638ab5c8ca4Slogin                 let env_ptr = args[2];
639be63f3b2Shoumkh                 let virt_path_ptr = VirtAddr::new(path_ptr);
640be63f3b2Shoumkh                 let virt_argv_ptr = VirtAddr::new(argv_ptr);
641be63f3b2Shoumkh                 let virt_env_ptr = VirtAddr::new(env_ptr);
642ab5c8ca4Slogin                 // 权限校验
6431496ba7bSLoGin                 if frame.from_user()
6441496ba7bSLoGin                     && (verify_area(virt_path_ptr, MAX_PATHLEN as usize).is_err()
645be63f3b2Shoumkh                         || verify_area(virt_argv_ptr, PAGE_4K_SIZE as usize).is_err())
646be63f3b2Shoumkh                     || verify_area(virt_env_ptr, PAGE_4K_SIZE as usize).is_err()
647ab5c8ca4Slogin                 {
648ab5c8ca4Slogin                     Err(SystemError::EFAULT)
649ab5c8ca4Slogin                 } else {
650ab5c8ca4Slogin                     Self::execve(
6511496ba7bSLoGin                         path_ptr as *const u8,
6521496ba7bSLoGin                         argv_ptr as *const *const u8,
6531496ba7bSLoGin                         env_ptr as *const *const u8,
6541496ba7bSLoGin                         frame,
655ab5c8ca4Slogin                     )
6561496ba7bSLoGin                     .map(|_| 0)
657ab5c8ca4Slogin                 }
658ab5c8ca4Slogin             }
659ab5c8ca4Slogin             SYS_WAIT4 => {
6601496ba7bSLoGin                 let pid = args[0] as i64;
6611496ba7bSLoGin                 let wstatus = args[1] as *mut i32;
662ab5c8ca4Slogin                 let options = args[2] as c_int;
663ab5c8ca4Slogin                 let rusage = args[3] as *mut c_void;
664ab5c8ca4Slogin                 // 权限校验
665ab5c8ca4Slogin                 // todo: 引入rusage之后,更正以下权限校验代码中,rusage的大小
666ab5c8ca4Slogin                 Self::wait4(pid, wstatus, options, rusage)
667ab5c8ca4Slogin             }
668ab5c8ca4Slogin 
669ab5c8ca4Slogin             SYS_EXIT => {
670ab5c8ca4Slogin                 let exit_code = args[0];
671ab5c8ca4Slogin                 Self::exit(exit_code)
672ab5c8ca4Slogin             }
673ab5c8ca4Slogin             SYS_MKDIR => {
674ab5c8ca4Slogin                 let path_ptr = args[0] as *const c_char;
675ab5c8ca4Slogin                 let mode = args[1];
676be63f3b2Shoumkh                 let virt_path_ptr = VirtAddr::new(path_ptr as usize);
677ab5c8ca4Slogin                 let security_check = || {
678ab5c8ca4Slogin                     if path_ptr.is_null()
6791496ba7bSLoGin                         || (frame.from_user()
6801496ba7bSLoGin                             && verify_area(virt_path_ptr, PAGE_2M_SIZE as usize).is_err())
681ab5c8ca4Slogin                     {
682ab5c8ca4Slogin                         return Err(SystemError::EINVAL);
683ab5c8ca4Slogin                     }
684ab5c8ca4Slogin                     let path: &CStr = unsafe { CStr::from_ptr(path_ptr) };
685ab5c8ca4Slogin                     let path: &str = path.to_str().map_err(|_| SystemError::EINVAL)?.trim();
686ab5c8ca4Slogin 
687ab5c8ca4Slogin                     if path == "" {
688ab5c8ca4Slogin                         return Err(SystemError::EINVAL);
689ab5c8ca4Slogin                     }
690ab5c8ca4Slogin                     return Ok(path);
691ab5c8ca4Slogin                 };
692ab5c8ca4Slogin 
693ab5c8ca4Slogin                 let path = security_check();
694ab5c8ca4Slogin                 if path.is_err() {
695ab5c8ca4Slogin                     Err(path.unwrap_err())
696ab5c8ca4Slogin                 } else {
697ab5c8ca4Slogin                     Self::mkdir(path.unwrap(), mode)
698ab5c8ca4Slogin                 }
699ab5c8ca4Slogin             }
700ab5c8ca4Slogin 
701ab5c8ca4Slogin             SYS_NANOSLEEP => {
702ab5c8ca4Slogin                 let req = args[0] as *const TimeSpec;
703ab5c8ca4Slogin                 let rem = args[1] as *mut TimeSpec;
704be63f3b2Shoumkh                 let virt_req = VirtAddr::new(req as usize);
705be63f3b2Shoumkh                 let virt_rem = VirtAddr::new(rem as usize);
7061496ba7bSLoGin                 if frame.from_user()
707be63f3b2Shoumkh                     && (verify_area(virt_req, core::mem::size_of::<TimeSpec>() as usize).is_err()
708be63f3b2Shoumkh                         || verify_area(virt_rem, core::mem::size_of::<TimeSpec>() as usize)
709be63f3b2Shoumkh                             .is_err())
710ab5c8ca4Slogin                 {
711ab5c8ca4Slogin                     Err(SystemError::EFAULT)
712ab5c8ca4Slogin                 } else {
713ab5c8ca4Slogin                     Self::nanosleep(req, rem)
714ab5c8ca4Slogin                 }
715ab5c8ca4Slogin             }
716ab5c8ca4Slogin 
717ab5c8ca4Slogin             SYS_CLOCK => Self::clock(),
718ab5c8ca4Slogin             SYS_PIPE => {
71922c9db31Shanjiezhou                 let pipefd: *mut i32 = args[0] as *mut c_int;
72022c9db31Shanjiezhou                 let arg1 = args[1];
72122c9db31Shanjiezhou                 if pipefd.is_null() {
72222c9db31Shanjiezhou                     Err(SystemError::EFAULT)
72322c9db31Shanjiezhou                 } else {
72422c9db31Shanjiezhou                     let flags = FileMode::from_bits_truncate(arg1 as u32);
72522c9db31Shanjiezhou                     Self::pipe2(pipefd, flags)
726ab5c8ca4Slogin                 }
727ab5c8ca4Slogin             }
728ab5c8ca4Slogin 
729ab5c8ca4Slogin             SYS_UNLINK_AT => {
730ab5c8ca4Slogin                 let dirfd = args[0] as i32;
731ab5c8ca4Slogin                 let pathname = args[1] as *const c_char;
732ab5c8ca4Slogin                 let flags = args[2] as u32;
733be63f3b2Shoumkh                 let virt_pathname = VirtAddr::new(pathname as usize);
7341496ba7bSLoGin                 if frame.from_user() && verify_area(virt_pathname, PAGE_4K_SIZE as usize).is_err() {
735ab5c8ca4Slogin                     Err(SystemError::EFAULT)
736ab5c8ca4Slogin                 } else if pathname.is_null() {
737ab5c8ca4Slogin                     Err(SystemError::EFAULT)
738ab5c8ca4Slogin                 } else {
739ab5c8ca4Slogin                     let get_path = || {
740ab5c8ca4Slogin                         let pathname: &CStr = unsafe { CStr::from_ptr(pathname) };
741ab5c8ca4Slogin 
742ab5c8ca4Slogin                         let pathname: &str = pathname.to_str().map_err(|_| SystemError::EINVAL)?;
743ab5c8ca4Slogin                         if pathname.len() >= MAX_PATHLEN {
744ab5c8ca4Slogin                             return Err(SystemError::ENAMETOOLONG);
745ab5c8ca4Slogin                         }
746ab5c8ca4Slogin                         return Ok(pathname.trim());
747ab5c8ca4Slogin                     };
748ab5c8ca4Slogin                     let pathname = get_path();
749ab5c8ca4Slogin                     if pathname.is_err() {
750ab5c8ca4Slogin                         Err(pathname.unwrap_err())
751ab5c8ca4Slogin                     } else {
7526d81180bSLoGin                         // kdebug!("sys unlinkat: dirfd: {}, pathname: {}", dirfd, pathname.as_ref().unwrap());
753ab5c8ca4Slogin                         Self::unlinkat(dirfd, pathname.unwrap(), flags)
754ab5c8ca4Slogin                     }
755ab5c8ca4Slogin                 }
756ab5c8ca4Slogin             }
757ab5c8ca4Slogin             SYS_KILL => {
7581496ba7bSLoGin                 let pid = Pid::new(args[0]);
759ab5c8ca4Slogin                 let sig = args[1] as c_int;
7603c82aa56SChiichen                 // kdebug!("KILL SYSCALL RECEIVED");
761ab5c8ca4Slogin                 Self::kill(pid, sig)
762ab5c8ca4Slogin             }
763ab5c8ca4Slogin 
764ab5c8ca4Slogin             SYS_SIGACTION => {
765ab5c8ca4Slogin                 let sig = args[0] as c_int;
766ab5c8ca4Slogin                 let act = args[1];
767ab5c8ca4Slogin                 let old_act = args[2];
7681496ba7bSLoGin                 Self::sigaction(sig, act, old_act, frame.from_user())
769ab5c8ca4Slogin             }
770ab5c8ca4Slogin 
771ab5c8ca4Slogin             SYS_RT_SIGRETURN => {
772ab5c8ca4Slogin                 // 由于目前signal机制的实现,与x86_64强关联,因此暂时在arch/x86_64/syscall.rs中调用
773ab5c8ca4Slogin                 // todo: 未来需要将signal机制与平台解耦
774ab5c8ca4Slogin                 todo!()
775ab5c8ca4Slogin             }
776ab5c8ca4Slogin 
7771496ba7bSLoGin             SYS_GETPID => Self::getpid().map(|pid| pid.into()),
778ab5c8ca4Slogin 
7791496ba7bSLoGin             SYS_SCHED => Self::sched(frame.from_user()),
780ab5c8ca4Slogin             SYS_DUP => {
781ab5c8ca4Slogin                 let oldfd: i32 = args[0] as c_int;
782ab5c8ca4Slogin                 Self::dup(oldfd)
783ab5c8ca4Slogin             }
784ab5c8ca4Slogin             SYS_DUP2 => {
785ab5c8ca4Slogin                 let oldfd: i32 = args[0] as c_int;
786ab5c8ca4Slogin                 let newfd: i32 = args[1] as c_int;
787ab5c8ca4Slogin                 Self::dup2(oldfd, newfd)
788ab5c8ca4Slogin             }
789ab5c8ca4Slogin 
790ab5c8ca4Slogin             SYS_SOCKET => Self::socket(args[0], args[1], args[2]),
791ab5c8ca4Slogin             SYS_SETSOCKOPT => {
792ab5c8ca4Slogin                 let optval = args[3] as *const u8;
793ab5c8ca4Slogin                 let optlen = args[4] as usize;
794be63f3b2Shoumkh                 let virt_optval = VirtAddr::new(optval as usize);
795ab5c8ca4Slogin                 // 验证optval的地址是否合法
796be63f3b2Shoumkh                 if verify_area(virt_optval, optlen as usize).is_err() {
797ab5c8ca4Slogin                     // 地址空间超出了用户空间的范围,不合法
798ab5c8ca4Slogin                     Err(SystemError::EFAULT)
799ab5c8ca4Slogin                 } else {
800ab5c8ca4Slogin                     let data: &[u8] = unsafe { core::slice::from_raw_parts(optval, optlen) };
801ab5c8ca4Slogin                     Self::setsockopt(args[0], args[1], args[2], data)
802ab5c8ca4Slogin                 }
803ab5c8ca4Slogin             }
804ab5c8ca4Slogin             SYS_GETSOCKOPT => {
805ab5c8ca4Slogin                 let optval = args[3] as *mut u8;
806ab5c8ca4Slogin                 let optlen = args[4] as *mut usize;
807be63f3b2Shoumkh                 let virt_optval = VirtAddr::new(optval as usize);
808be63f3b2Shoumkh                 let virt_optlen = VirtAddr::new(optlen as usize);
809ab5c8ca4Slogin                 let security_check = || {
810ab5c8ca4Slogin                     // 验证optval的地址是否合法
811be63f3b2Shoumkh                     if verify_area(virt_optval, PAGE_4K_SIZE as usize).is_err() {
812ab5c8ca4Slogin                         // 地址空间超出了用户空间的范围,不合法
813ab5c8ca4Slogin                         return Err(SystemError::EFAULT);
814ab5c8ca4Slogin                     }
815ab5c8ca4Slogin 
816ab5c8ca4Slogin                     // 验证optlen的地址是否合法
817be63f3b2Shoumkh                     if verify_area(virt_optlen, core::mem::size_of::<u32>() as usize).is_err() {
818ab5c8ca4Slogin                         // 地址空间超出了用户空间的范围,不合法
819ab5c8ca4Slogin                         return Err(SystemError::EFAULT);
820ab5c8ca4Slogin                     }
821ab5c8ca4Slogin                     return Ok(());
822ab5c8ca4Slogin                 };
823ab5c8ca4Slogin                 let r = security_check();
824ab5c8ca4Slogin                 if r.is_err() {
825ab5c8ca4Slogin                     Err(r.unwrap_err())
826ab5c8ca4Slogin                 } else {
827ab5c8ca4Slogin                     Self::getsockopt(args[0], args[1], args[2], optval, optlen as *mut u32)
828ab5c8ca4Slogin                 }
829ab5c8ca4Slogin             }
830ab5c8ca4Slogin 
831ab5c8ca4Slogin             SYS_CONNECT => {
832ab5c8ca4Slogin                 let addr = args[1] as *const SockAddr;
833ab5c8ca4Slogin                 let addrlen = args[2] as usize;
834be63f3b2Shoumkh                 let virt_addr = VirtAddr::new(addr as usize);
835ab5c8ca4Slogin                 // 验证addr的地址是否合法
836be63f3b2Shoumkh                 if verify_area(virt_addr, addrlen as usize).is_err() {
837ab5c8ca4Slogin                     // 地址空间超出了用户空间的范围,不合法
838ab5c8ca4Slogin                     Err(SystemError::EFAULT)
839ab5c8ca4Slogin                 } else {
840ab5c8ca4Slogin                     Self::connect(args[0], addr, addrlen)
841ab5c8ca4Slogin                 }
842ab5c8ca4Slogin             }
843ab5c8ca4Slogin             SYS_BIND => {
844ab5c8ca4Slogin                 let addr = args[1] as *const SockAddr;
845ab5c8ca4Slogin                 let addrlen = args[2] as usize;
846be63f3b2Shoumkh                 let virt_addr = VirtAddr::new(addr as usize);
847ab5c8ca4Slogin                 // 验证addr的地址是否合法
848be63f3b2Shoumkh                 if verify_area(virt_addr, addrlen as usize).is_err() {
849ab5c8ca4Slogin                     // 地址空间超出了用户空间的范围,不合法
850ab5c8ca4Slogin                     Err(SystemError::EFAULT)
851ab5c8ca4Slogin                 } else {
852ab5c8ca4Slogin                     Self::bind(args[0], addr, addrlen)
853ab5c8ca4Slogin                 }
854ab5c8ca4Slogin             }
855ab5c8ca4Slogin 
856ab5c8ca4Slogin             SYS_SENDTO => {
857ab5c8ca4Slogin                 let buf = args[1] as *const u8;
858ab5c8ca4Slogin                 let len = args[2] as usize;
859ab5c8ca4Slogin                 let flags = args[3] as u32;
860ab5c8ca4Slogin                 let addr = args[4] as *const SockAddr;
861ab5c8ca4Slogin                 let addrlen = args[5] as usize;
862be63f3b2Shoumkh                 let virt_buf = VirtAddr::new(buf as usize);
863be63f3b2Shoumkh                 let virt_addr = VirtAddr::new(addr as usize);
864ab5c8ca4Slogin                 // 验证buf的地址是否合法
865be63f3b2Shoumkh                 if verify_area(virt_buf, len as usize).is_err() {
866ab5c8ca4Slogin                     // 地址空间超出了用户空间的范围,不合法
867ab5c8ca4Slogin                     Err(SystemError::EFAULT)
868be63f3b2Shoumkh                 } else if verify_area(virt_addr, addrlen as usize).is_err() {
869ab5c8ca4Slogin                     // 地址空间超出了用户空间的范围,不合法
870ab5c8ca4Slogin                     Err(SystemError::EFAULT)
871ab5c8ca4Slogin                 } else {
872ab5c8ca4Slogin                     let data: &[u8] = unsafe { core::slice::from_raw_parts(buf, len) };
873ab5c8ca4Slogin                     Self::sendto(args[0], data, flags, addr, addrlen)
874ab5c8ca4Slogin                 }
875ab5c8ca4Slogin             }
876ab5c8ca4Slogin 
877ab5c8ca4Slogin             SYS_RECVFROM => {
878ab5c8ca4Slogin                 let buf = args[1] as *mut u8;
879ab5c8ca4Slogin                 let len = args[2] as usize;
880ab5c8ca4Slogin                 let flags = args[3] as u32;
881ab5c8ca4Slogin                 let addr = args[4] as *mut SockAddr;
882ab5c8ca4Slogin                 let addrlen = args[5] as *mut usize;
883be63f3b2Shoumkh                 let virt_buf = VirtAddr::new(buf as usize);
884be63f3b2Shoumkh                 let virt_addrlen = VirtAddr::new(addrlen as usize);
885be63f3b2Shoumkh                 let virt_addr = VirtAddr::new(addr as usize);
886ab5c8ca4Slogin                 let security_check = || {
887ab5c8ca4Slogin                     // 验证buf的地址是否合法
888be63f3b2Shoumkh                     if verify_area(virt_buf, len as usize).is_err() {
889ab5c8ca4Slogin                         // 地址空间超出了用户空间的范围,不合法
890ab5c8ca4Slogin                         return Err(SystemError::EFAULT);
891ab5c8ca4Slogin                     }
892ab5c8ca4Slogin 
893ab5c8ca4Slogin                     // 验证addrlen的地址是否合法
894be63f3b2Shoumkh                     if verify_area(virt_addrlen, core::mem::size_of::<u32>() as usize).is_err() {
895ab5c8ca4Slogin                         // 地址空间超出了用户空间的范围,不合法
896ab5c8ca4Slogin                         return Err(SystemError::EFAULT);
897ab5c8ca4Slogin                     }
898ab5c8ca4Slogin 
899be63f3b2Shoumkh                     if verify_area(virt_addr, core::mem::size_of::<SockAddr>() as usize).is_err() {
900ab5c8ca4Slogin                         // 地址空间超出了用户空间的范围,不合法
901ab5c8ca4Slogin                         return Err(SystemError::EFAULT);
902ab5c8ca4Slogin                     }
903ab5c8ca4Slogin                     return Ok(());
904ab5c8ca4Slogin                 };
905ab5c8ca4Slogin                 let r = security_check();
906ab5c8ca4Slogin                 if r.is_err() {
907ab5c8ca4Slogin                     Err(r.unwrap_err())
908ab5c8ca4Slogin                 } else {
909ab5c8ca4Slogin                     let buf = unsafe { core::slice::from_raw_parts_mut(buf, len) };
910ab5c8ca4Slogin                     Self::recvfrom(args[0], buf, flags, addr, addrlen as *mut u32)
911ab5c8ca4Slogin                 }
912ab5c8ca4Slogin             }
913ab5c8ca4Slogin 
914ab5c8ca4Slogin             SYS_RECVMSG => {
915ab5c8ca4Slogin                 let msg = args[1] as *mut crate::net::syscall::MsgHdr;
916ab5c8ca4Slogin                 let flags = args[2] as u32;
9174537ffb7SChiichen                 match UserBufferWriter::new(
9184537ffb7SChiichen                     msg,
9194537ffb7SChiichen                     core::mem::size_of::<crate::net::syscall::MsgHdr>(),
9204537ffb7SChiichen                     true,
9214537ffb7SChiichen                 ) {
9224537ffb7SChiichen                     Err(e) => Err(e),
9234537ffb7SChiichen                     Ok(mut user_buffer_writer) => {
9244537ffb7SChiichen                         match user_buffer_writer.buffer::<crate::net::syscall::MsgHdr>(0) {
9254537ffb7SChiichen                             Err(e) => Err(e),
9264537ffb7SChiichen                             Ok(buffer) => {
9274537ffb7SChiichen                                 let msg = &mut buffer[0];
928ab5c8ca4Slogin                                 Self::recvmsg(args[0], msg, flags)
929ab5c8ca4Slogin                             }
930ab5c8ca4Slogin                         }
9314537ffb7SChiichen                     }
9324537ffb7SChiichen                 }
9334537ffb7SChiichen             }
934ab5c8ca4Slogin 
935ab5c8ca4Slogin             SYS_LISTEN => Self::listen(args[0], args[1]),
936ab5c8ca4Slogin             SYS_SHUTDOWN => Self::shutdown(args[0], args[1]),
937ab5c8ca4Slogin             SYS_ACCEPT => Self::accept(args[0], args[1] as *mut SockAddr, args[2] as *mut u32),
938c47fe904SLoGin             SYS_ACCEPT4 => Self::accept4(
939c47fe904SLoGin                 args[0],
940c47fe904SLoGin                 args[1] as *mut SockAddr,
941c47fe904SLoGin                 args[2] as *mut u32,
942c47fe904SLoGin                 args[3] as u32,
943c47fe904SLoGin             ),
944ab5c8ca4Slogin             SYS_GETSOCKNAME => {
945ab5c8ca4Slogin                 Self::getsockname(args[0], args[1] as *mut SockAddr, args[2] as *mut u32)
946ab5c8ca4Slogin             }
947ab5c8ca4Slogin             SYS_GETPEERNAME => {
948ab5c8ca4Slogin                 Self::getpeername(args[0], args[1] as *mut SockAddr, args[2] as *mut u32)
949ab5c8ca4Slogin             }
95036fd0130Shoumkh             SYS_GETTIMEOFDAY => {
95136fd0130Shoumkh                 let timeval = args[0] as *mut PosixTimeval;
95240fe15e0SLoGin                 let timezone_ptr = args[1] as *mut PosixTimeZone;
95340fe15e0SLoGin                 Self::gettimeofday(timeval, timezone_ptr)
9544537ffb7SChiichen             }
95540fe15e0SLoGin             SYS_MMAP => {
95640fe15e0SLoGin                 let len = page_align_up(args[1]);
957be63f3b2Shoumkh                 let virt_addr = VirtAddr::new(args[0] as usize);
958be63f3b2Shoumkh                 if verify_area(virt_addr, len as usize).is_err() {
95940fe15e0SLoGin                     Err(SystemError::EFAULT)
96040fe15e0SLoGin                 } else {
96140fe15e0SLoGin                     Self::mmap(
96240fe15e0SLoGin                         VirtAddr::new(args[0]),
96340fe15e0SLoGin                         len,
96440fe15e0SLoGin                         args[2],
96540fe15e0SLoGin                         args[3],
96640fe15e0SLoGin                         args[4] as i32,
96740fe15e0SLoGin                         args[5],
96840fe15e0SLoGin                     )
96940fe15e0SLoGin                 }
97040fe15e0SLoGin             }
97140fe15e0SLoGin             SYS_MUNMAP => {
97240fe15e0SLoGin                 let addr = args[0];
97340fe15e0SLoGin                 let len = page_align_up(args[1]);
974971462beSGnoCiYeH                 if addr & (MMArch::PAGE_SIZE - 1) != 0 {
97540fe15e0SLoGin                     // The addr argument is not a multiple of the page size
97640fe15e0SLoGin                     Err(SystemError::EINVAL)
97740fe15e0SLoGin                 } else {
97840fe15e0SLoGin                     Self::munmap(VirtAddr::new(addr), len)
97940fe15e0SLoGin                 }
98040fe15e0SLoGin             }
98140fe15e0SLoGin             SYS_MPROTECT => {
98240fe15e0SLoGin                 let addr = args[0];
98340fe15e0SLoGin                 let len = page_align_up(args[1]);
984971462beSGnoCiYeH                 if addr & (MMArch::PAGE_SIZE - 1) != 0 {
98540fe15e0SLoGin                     // The addr argument is not a multiple of the page size
98640fe15e0SLoGin                     Err(SystemError::EINVAL)
98740fe15e0SLoGin                 } else {
98840fe15e0SLoGin                     Self::mprotect(VirtAddr::new(addr), len, args[2])
98940fe15e0SLoGin                 }
99040fe15e0SLoGin             }
99140fe15e0SLoGin 
9921496ba7bSLoGin             SYS_GETCWD => {
9931496ba7bSLoGin                 let buf = args[0] as *mut u8;
9941496ba7bSLoGin                 let size = args[1] as usize;
9951496ba7bSLoGin                 let security_check = || {
9961496ba7bSLoGin                     verify_area(VirtAddr::new(buf as usize), size)?;
9971496ba7bSLoGin                     return Ok(());
9981496ba7bSLoGin                 };
9991496ba7bSLoGin                 let r = security_check();
10001496ba7bSLoGin                 if r.is_err() {
10011496ba7bSLoGin                     Err(r.unwrap_err())
10021496ba7bSLoGin                 } else {
10031496ba7bSLoGin                     let buf = unsafe { core::slice::from_raw_parts_mut(buf, size) };
10041496ba7bSLoGin                     Self::getcwd(buf).map(|ptr| ptr.data())
10051496ba7bSLoGin                 }
10061496ba7bSLoGin             }
10071496ba7bSLoGin 
10081496ba7bSLoGin             SYS_GETPGID => Self::getpgid(Pid::new(args[0])).map(|pid| pid.into()),
10091496ba7bSLoGin 
10101496ba7bSLoGin             SYS_GETPPID => Self::getppid().map(|pid| pid.into()),
1011be63f3b2Shoumkh             SYS_FSTAT => {
1012be63f3b2Shoumkh                 let fd = args[0] as i32;
1013be63f3b2Shoumkh                 let kstat = args[1] as *mut PosixKstat;
1014be63f3b2Shoumkh                 let vaddr = VirtAddr::new(kstat as usize);
1015be63f3b2Shoumkh                 // FIXME 由于c中的verify_area与rust中的verify_area重名,所以在引入时加了前缀区分
1016be63f3b2Shoumkh                 // TODO 应该将用了c版本的verify_area都改为rust的verify_area
1017be63f3b2Shoumkh                 match verify_area(vaddr, core::mem::size_of::<PosixKstat>()) {
1018be63f3b2Shoumkh                     Ok(_) => Self::fstat(fd, kstat),
1019be63f3b2Shoumkh                     Err(e) => Err(e),
1020be63f3b2Shoumkh                 }
1021be63f3b2Shoumkh             }
1022be63f3b2Shoumkh 
10236d81180bSLoGin             SYS_FCNTL => {
10246d81180bSLoGin                 let fd = args[0] as i32;
10256d81180bSLoGin                 let cmd: Option<FcntlCommand> =
10266d81180bSLoGin                     <FcntlCommand as FromPrimitive>::from_u32(args[1] as u32);
10276d81180bSLoGin                 let arg = args[2] as i32;
10286d81180bSLoGin                 let res = if let Some(cmd) = cmd {
10296d81180bSLoGin                     Self::fcntl(fd, cmd, arg)
10306d81180bSLoGin                 } else {
10316d81180bSLoGin                     Err(SystemError::EINVAL)
10326d81180bSLoGin                 };
10336d81180bSLoGin 
10346d81180bSLoGin                 // kdebug!("FCNTL: fd: {}, cmd: {:?}, arg: {}, res: {:?}", fd, cmd, arg, res);
10356d81180bSLoGin                 res
10366d81180bSLoGin             }
10376d81180bSLoGin 
10386d81180bSLoGin             SYS_FTRUNCATE => {
10396d81180bSLoGin                 let fd = args[0] as i32;
10406d81180bSLoGin                 let len = args[1] as usize;
10416d81180bSLoGin                 let res = Self::ftruncate(fd, len);
10426d81180bSLoGin                 // kdebug!("FTRUNCATE: fd: {}, len: {}, res: {:?}", fd, len, res);
10436d81180bSLoGin                 res
10446d81180bSLoGin             }
10456d81180bSLoGin 
10462dbef785SGnoCiYeH             SYS_MKNOD => {
10472dbef785SGnoCiYeH                 let path = args[0];
10482dbef785SGnoCiYeH                 let flags = args[1];
10492dbef785SGnoCiYeH                 let dev_t = args[2];
10502dbef785SGnoCiYeH                 let flags: ModeType = ModeType::from_bits_truncate(flags as u32);
10512dbef785SGnoCiYeH                 Self::mknod(path as *const i8, flags, DeviceNumber::from(dev_t))
10522dbef785SGnoCiYeH             }
10532dbef785SGnoCiYeH 
1054971462beSGnoCiYeH             SYS_CLONE => {
1055971462beSGnoCiYeH                 let parent_tid = VirtAddr::new(args[2]);
1056971462beSGnoCiYeH                 let child_tid = VirtAddr::new(args[3]);
1057971462beSGnoCiYeH 
1058971462beSGnoCiYeH                 // 地址校验
1059971462beSGnoCiYeH                 verify_area(parent_tid, core::mem::size_of::<i32>())?;
1060971462beSGnoCiYeH                 verify_area(child_tid, core::mem::size_of::<i32>())?;
1061971462beSGnoCiYeH 
1062971462beSGnoCiYeH                 let mut clone_args = KernelCloneArgs::new();
1063971462beSGnoCiYeH                 clone_args.flags = CloneFlags::from_bits_truncate(args[0] as u64);
1064971462beSGnoCiYeH                 clone_args.stack = args[1];
1065971462beSGnoCiYeH                 clone_args.parent_tid = parent_tid;
1066971462beSGnoCiYeH                 clone_args.child_tid = child_tid;
1067971462beSGnoCiYeH                 clone_args.tls = args[4];
1068971462beSGnoCiYeH                 Self::clone(frame, clone_args)
1069971462beSGnoCiYeH             }
1070971462beSGnoCiYeH 
1071971462beSGnoCiYeH             SYS_FUTEX => {
1072971462beSGnoCiYeH                 let uaddr = VirtAddr::new(args[0]);
1073971462beSGnoCiYeH                 let operation = FutexFlag::from_bits(args[1] as u32).ok_or(SystemError::ENOSYS)?;
1074971462beSGnoCiYeH                 let val = args[2] as u32;
1075971462beSGnoCiYeH                 let utime = args[3];
1076971462beSGnoCiYeH                 let uaddr2 = VirtAddr::new(args[4]);
1077971462beSGnoCiYeH                 let val3 = args[5] as u32;
1078971462beSGnoCiYeH 
1079971462beSGnoCiYeH                 verify_area(uaddr, core::mem::size_of::<u32>())?;
1080971462beSGnoCiYeH                 verify_area(uaddr2, core::mem::size_of::<u32>())?;
1081971462beSGnoCiYeH 
1082971462beSGnoCiYeH                 let mut timespec = None;
1083971462beSGnoCiYeH                 if utime != 0 && operation.contains(FutexFlag::FLAGS_HAS_TIMEOUT) {
1084971462beSGnoCiYeH                     let reader = UserBufferReader::new(
1085971462beSGnoCiYeH                         utime as *const TimeSpec,
1086971462beSGnoCiYeH                         core::mem::size_of::<TimeSpec>(),
1087971462beSGnoCiYeH                         true,
1088971462beSGnoCiYeH                     )?;
1089971462beSGnoCiYeH 
1090971462beSGnoCiYeH                     timespec = Some(reader.read_one_from_user::<TimeSpec>(0)?.clone());
1091971462beSGnoCiYeH                 }
1092971462beSGnoCiYeH 
1093971462beSGnoCiYeH                 Self::do_futex(uaddr, operation, val, timespec, uaddr2, utime as u32, val3)
1094971462beSGnoCiYeH             }
1095971462beSGnoCiYeH 
1096971462beSGnoCiYeH             SYS_WRITEV => Self::writev(args[0] as i32, args[1], args[2]),
1097971462beSGnoCiYeH 
1098971462beSGnoCiYeH             SYS_ARCH_PRCTL => Self::arch_prctl(args[0], args[1]),
1099971462beSGnoCiYeH 
1100971462beSGnoCiYeH             SYS_SET_TID_ADDR => Self::set_tid_address(args[0]),
1101971462beSGnoCiYeH 
1102971462beSGnoCiYeH             SYS_STAT => {
1103971462beSGnoCiYeH                 let path: &CStr = unsafe { CStr::from_ptr(args[0] as *const c_char) };
1104971462beSGnoCiYeH                 let path: Result<&str, core::str::Utf8Error> = path.to_str();
1105971462beSGnoCiYeH                 let res = if path.is_err() {
1106971462beSGnoCiYeH                     Err(SystemError::EINVAL)
1107971462beSGnoCiYeH                 } else {
1108971462beSGnoCiYeH                     let path: &str = path.unwrap();
1109971462beSGnoCiYeH                     let kstat = args[1] as *mut PosixKstat;
1110971462beSGnoCiYeH                     let vaddr = VirtAddr::new(kstat as usize);
1111971462beSGnoCiYeH                     match verify_area(vaddr, core::mem::size_of::<PosixKstat>()) {
1112971462beSGnoCiYeH                         Ok(_) => Self::stat(path, kstat),
1113971462beSGnoCiYeH                         Err(e) => Err(e),
1114971462beSGnoCiYeH                     }
1115971462beSGnoCiYeH                 };
1116971462beSGnoCiYeH 
1117971462beSGnoCiYeH                 res
1118971462beSGnoCiYeH             }
1119971462beSGnoCiYeH 
1120971462beSGnoCiYeH             // 目前为了适配musl-libc,以下系统调用先这样写着
1121971462beSGnoCiYeH             SYS_GET_RANDOM => {
1122971462beSGnoCiYeH                 let flags = GRandFlags::from_bits(args[2] as u8).ok_or(SystemError::EINVAL)?;
1123971462beSGnoCiYeH                 Self::get_random(args[0] as *mut u8, args[1], flags)
1124971462beSGnoCiYeH             }
1125971462beSGnoCiYeH 
1126971462beSGnoCiYeH             SYS_SOCKET_PAIR => {
1127971462beSGnoCiYeH                 unimplemented!()
1128971462beSGnoCiYeH             }
1129971462beSGnoCiYeH 
1130971462beSGnoCiYeH             SYS_POLL => {
1131971462beSGnoCiYeH                 kwarn!("SYS_POLL has not yet been implemented");
1132971462beSGnoCiYeH                 Ok(0)
1133971462beSGnoCiYeH             }
1134971462beSGnoCiYeH 
1135971462beSGnoCiYeH             SYS_RT_SIGPROCMASK => {
1136971462beSGnoCiYeH                 kwarn!("SYS_RT_SIGPROCMASK has not yet been implemented");
1137971462beSGnoCiYeH                 Ok(0)
1138971462beSGnoCiYeH             }
1139971462beSGnoCiYeH 
1140971462beSGnoCiYeH             SYS_TKILL => {
1141971462beSGnoCiYeH                 kwarn!("SYS_TKILL has not yet been implemented");
1142971462beSGnoCiYeH                 Ok(0)
1143971462beSGnoCiYeH             }
1144971462beSGnoCiYeH 
1145971462beSGnoCiYeH             SYS_SIGALTSTACK => {
1146971462beSGnoCiYeH                 kwarn!("SYS_SIGALTSTACK has not yet been implemented");
1147971462beSGnoCiYeH                 Ok(0)
1148971462beSGnoCiYeH             }
1149971462beSGnoCiYeH 
115016033951SGnoCiYeH             SYS_EXIT_GROUP => {
115116033951SGnoCiYeH                 kwarn!("SYS_EXIT_GROUP has not yet been implemented");
115216033951SGnoCiYeH                 Ok(0)
115316033951SGnoCiYeH             }
115416033951SGnoCiYeH 
115504babc3fSMemoryShore             SYS_MADVISE => {
115604babc3fSMemoryShore                 kwarn!("SYS_MADVISE has not yet been implemented");
115704babc3fSMemoryShore                 Ok(0)
115804babc3fSMemoryShore             }
1159393f6915SLoGin             SYS_GETTID => Self::gettid().map(|tid| tid.into()),
116002e249f3SLoGin             SYS_GETUID => Self::getuid().map(|uid| uid.into()),
116102e249f3SLoGin             SYS_SYSLOG => {
116202e249f3SLoGin                 kwarn!("SYS_SYSLOG has not yet been implemented");
116302e249f3SLoGin                 Ok(0)
116402e249f3SLoGin             }
116502e249f3SLoGin             SYS_GETGID => Self::getgid().map(|gid| gid.into()),
116602e249f3SLoGin             SYS_SETUID => {
116702e249f3SLoGin                 kwarn!("SYS_SETUID has not yet been implemented");
116802e249f3SLoGin                 Ok(0)
116902e249f3SLoGin             }
117002e249f3SLoGin             SYS_SETGID => {
117102e249f3SLoGin                 kwarn!("SYS_SETGID has not yet been implemented");
117202e249f3SLoGin                 Ok(0)
117302e249f3SLoGin             }
117402e249f3SLoGin             SYS_GETEUID => Self::geteuid().map(|euid| euid.into()),
117502e249f3SLoGin             SYS_GETEGID => Self::getegid().map(|egid| egid.into()),
1176be8cdf4bSLoGin             SYS_GETRUSAGE => {
1177be8cdf4bSLoGin                 let who = args[0] as c_int;
1178be8cdf4bSLoGin                 let rusage = args[1] as *mut RUsage;
1179be8cdf4bSLoGin                 Self::get_rusage(who, rusage)
1180be8cdf4bSLoGin             }
118104babc3fSMemoryShore 
1182709498caSLoGin             SYS_READLINK => {
1183709498caSLoGin                 let path = args[0] as *const u8;
1184709498caSLoGin                 let buf = args[1] as *mut u8;
1185709498caSLoGin                 let bufsiz = args[2] as usize;
1186709498caSLoGin                 Self::readlink(path, buf, bufsiz)
1187709498caSLoGin             }
1188709498caSLoGin 
1189709498caSLoGin             SYS_READLINK_AT => {
1190709498caSLoGin                 let dirfd = args[0] as i32;
1191709498caSLoGin                 let pathname = args[1] as *const u8;
1192709498caSLoGin                 let buf = args[2] as *mut u8;
1193709498caSLoGin                 let bufsiz = args[3] as usize;
1194709498caSLoGin                 Self::readlink_at(dirfd, pathname, buf, bufsiz)
1195709498caSLoGin             }
1196709498caSLoGin 
11970d9b7d92SLoGin             SYS_PRLIMIT64 => {
11980d9b7d92SLoGin                 let pid = args[0];
11990d9b7d92SLoGin                 let pid = Pid::new(pid);
12000d9b7d92SLoGin                 let resource = args[1];
12010d9b7d92SLoGin                 let new_limit = args[2] as *const RLimit64;
12020d9b7d92SLoGin                 let old_limit = args[3] as *mut RLimit64;
12030d9b7d92SLoGin 
12040d9b7d92SLoGin                 Self::prlimit64(pid, resource, new_limit, old_limit)
12050d9b7d92SLoGin             }
12060d9b7d92SLoGin 
1207*9b0abe6dSLoGin             SYS_ACCESS => {
1208*9b0abe6dSLoGin                 let pathname = args[0] as *const u8;
1209*9b0abe6dSLoGin                 let mode = args[1] as u32;
1210*9b0abe6dSLoGin                 Self::access(pathname, mode)
1211*9b0abe6dSLoGin             }
1212*9b0abe6dSLoGin 
1213*9b0abe6dSLoGin             SYS_FACCESSAT => {
1214*9b0abe6dSLoGin                 let dirfd = args[0] as i32;
1215*9b0abe6dSLoGin                 let pathname = args[1] as *const u8;
1216*9b0abe6dSLoGin                 let mode = args[2] as u32;
1217*9b0abe6dSLoGin                 Self::faccessat2(dirfd, pathname, mode, 0)
1218*9b0abe6dSLoGin             }
1219*9b0abe6dSLoGin 
1220*9b0abe6dSLoGin             SYS_FACCESSAT2 => {
1221*9b0abe6dSLoGin                 let dirfd = args[0] as i32;
1222*9b0abe6dSLoGin                 let pathname = args[1] as *const u8;
1223*9b0abe6dSLoGin                 let mode = args[2] as u32;
1224*9b0abe6dSLoGin                 let flags = args[3] as u32;
1225*9b0abe6dSLoGin                 Self::faccessat2(dirfd, pathname, mode, flags)
1226*9b0abe6dSLoGin             }
1227*9b0abe6dSLoGin 
1228ab5c8ca4Slogin             _ => panic!("Unsupported syscall ID: {}", syscall_num),
1229ab5c8ca4Slogin         };
1230ab5c8ca4Slogin         return r;
1231ab5c8ca4Slogin     }
1232ab5c8ca4Slogin 
1233ab5c8ca4Slogin     pub fn put_string(
1234ab5c8ca4Slogin         s: *const u8,
1235ab5c8ca4Slogin         front_color: u32,
1236ab5c8ca4Slogin         back_color: u32,
1237ab5c8ca4Slogin     ) -> Result<usize, SystemError> {
1238ab5c8ca4Slogin         return Ok(unsafe { do_put_string(s, front_color, back_color) });
1239ab5c8ca4Slogin     }
1240ab5c8ca4Slogin 
1241ab5c8ca4Slogin     pub fn reboot() -> Result<usize, SystemError> {
1242ab5c8ca4Slogin         cpu_reset();
1243ab5c8ca4Slogin     }
1244ab5c8ca4Slogin }
1245