xref: /DragonOS/kernel/src/syscall/mod.rs (revision 36fd013004ee0bd5fc7cfb452ba22531a83a859c)
1ab5c8ca4Slogin use core::{
2ab5c8ca4Slogin     ffi::{c_char, c_int, c_void, CStr},
3ab5c8ca4Slogin     sync::atomic::{AtomicBool, Ordering},
4ab5c8ca4Slogin };
5ab5c8ca4Slogin 
6676b8ef6SMork use num_traits::{FromPrimitive, ToPrimitive};
7676b8ef6SMork 
8ab5c8ca4Slogin use crate::{
9ab5c8ca4Slogin     arch::cpu::cpu_reset,
10ab5c8ca4Slogin     filesystem::vfs::{
11ab5c8ca4Slogin         file::FileMode,
12ab5c8ca4Slogin         syscall::{SEEK_CUR, SEEK_END, SEEK_MAX, SEEK_SET},
13ab5c8ca4Slogin         MAX_PATHLEN,
14ab5c8ca4Slogin     },
15ab5c8ca4Slogin     include::bindings::bindings::{mm_stat_t, pid_t, verify_area, PAGE_2M_SIZE, PAGE_4K_SIZE},
16aa0367d6SLoGin     io::SeekFrom,
17aa0367d6SLoGin     kinfo,
18ab5c8ca4Slogin     net::syscall::SockAddr,
19*36fd0130Shoumkh     time::{
20*36fd0130Shoumkh         syscall::{PosixTimeZone, PosixTimeval, SYS_TIMEZONE},
21*36fd0130Shoumkh         TimeSpec,
22*36fd0130Shoumkh     },
23ab5c8ca4Slogin };
24ab5c8ca4Slogin 
254454d1a2Slogin #[repr(i32)]
26676b8ef6SMork #[derive(Debug, FromPrimitive, ToPrimitive, PartialEq, Eq, Clone)]
2779a452ceShoumkh #[allow(dead_code, non_camel_case_types)]
284454d1a2Slogin pub enum SystemError {
294454d1a2Slogin     /// 操作不被允许 Operation not permitted.
3079a452ceShoumkh     EPERM = 1,
3179a452ceShoumkh     /// 没有指定的文件或目录 No such file or directory.
3279a452ceShoumkh     ENOENT = 2,
334454d1a2Slogin     /// 没有这样的进程 No such process.
3479a452ceShoumkh     ESRCH = 3,
3579a452ceShoumkh     /// 被中断的函数 Interrupted function.
3679a452ceShoumkh     EINTR = 4,
3779a452ceShoumkh     /// I/O错误 I/O error.
3879a452ceShoumkh     EIO = 5,
3979a452ceShoumkh     /// 没有这样的设备或地址 No such device or address.
4079a452ceShoumkh     ENXIO = 6,
4179a452ceShoumkh     /// 参数列表过长,或者在输出buffer中缺少空间 或者参数比系统内建的最大值要大 Argument list too long.
4279a452ceShoumkh     E2BIG = 7,
4379a452ceShoumkh     /// 可执行文件格式错误 Executable file format error
4479a452ceShoumkh     ENOEXEC = 8,
4579a452ceShoumkh     /// 错误的文件描述符 Bad file descriptor.
4679a452ceShoumkh     EBADF = 9,
4779a452ceShoumkh     /// 没有子进程 No child processes.
4879a452ceShoumkh     ECHILD = 10,
4979a452ceShoumkh     /// 资源不可用,请重试。 Resource unavailable, try again.(may be the same value as [EWOULDBLOCK])
5079a452ceShoumkh     ///
5179a452ceShoumkh     /// 操作将被禁止 Operation would block.(may be the same value as [EAGAIN]).
5279a452ceShoumkh     EAGAIN_OR_EWOULDBLOCK = 11,
5379a452ceShoumkh     /// 没有足够的空间 Not enough space.
5479a452ceShoumkh     ENOMEM = 12,
5579a452ceShoumkh     /// 访问被拒绝 Permission denied
5679a452ceShoumkh     EACCES = 13,
5779a452ceShoumkh     /// 错误的地址 Bad address
5879a452ceShoumkh     EFAULT = 14,
5979a452ceShoumkh     /// 需要块设备 Block device required
6079a452ceShoumkh     ENOTBLK = 15,
6179a452ceShoumkh     /// 设备或资源忙 Device or resource busy.
6279a452ceShoumkh     EBUSY = 16,
6379a452ceShoumkh     /// 文件已存在 File exists.
6479a452ceShoumkh     EEXIST = 17,
654454d1a2Slogin     /// 跨设备连接 Cross-device link.
6679a452ceShoumkh     EXDEV = 18,
6779a452ceShoumkh     /// 没有指定的设备 No such device.
6879a452ceShoumkh     ENODEV = 19,
6979a452ceShoumkh     /// 不是目录 Not a directory.
7079a452ceShoumkh     ENOTDIR = 20,
7179a452ceShoumkh     /// 是一个目录 Is a directory
7279a452ceShoumkh     EISDIR = 21,
7379a452ceShoumkh     /// 不可用的参数 Invalid argument.
7479a452ceShoumkh     EINVAL = 22,
7579a452ceShoumkh     /// 系统中打开的文件过多 Too many files open in system.
7679a452ceShoumkh     ENFILE = 23,
7779a452ceShoumkh     /// 文件描述符的值过大 File descriptor value too large.
7879a452ceShoumkh     EMFILE = 24,
7979a452ceShoumkh     /// 不正确的I/O控制操作 Inappropriate I/O control operation.
8079a452ceShoumkh     ENOTTY = 25,
8179a452ceShoumkh     /// 文本文件忙 Text file busy.
8279a452ceShoumkh     ETXTBSY = 26,
8379a452ceShoumkh     /// 文件太大 File too large.
8479a452ceShoumkh     EFBIG = 27,
8579a452ceShoumkh     /// 设备上没有空间 No space left on device.
8679a452ceShoumkh     ENOSPC = 28,
8779a452ceShoumkh     /// 错误的寻道.当前文件是pipe,不允许seek请求  Invalid seek.
8879a452ceShoumkh     ESPIPE = 29,
8979a452ceShoumkh     /// 只读的文件系统 Read-only file system.
9079a452ceShoumkh     EROFS = 30,
9179a452ceShoumkh     /// 链接数过多 Too many links.
9279a452ceShoumkh     EMLINK = 31,
9379a452ceShoumkh     /// 断开的管道 Broken pipe.
9479a452ceShoumkh     EPIPE = 32,
9579a452ceShoumkh     /// 数学参数超出作用域 Mathematics argument out of domain of function.
9679a452ceShoumkh     EDOM = 33,
9779a452ceShoumkh     /// 结果过大 Result too large.
9879a452ceShoumkh     ERANGE = 34,
9979a452ceShoumkh     /// 资源死锁将要发生 Resource deadlock would occur.
10079a452ceShoumkh     EDEADLK = 35,
10179a452ceShoumkh     /// 文件名过长 Filename too long.
10279a452ceShoumkh     ENAMETOOLONG = 36,
10379a452ceShoumkh     /// 没有可用的锁 No locks available.
10479a452ceShoumkh     ENOLCK = 37,
10579a452ceShoumkh     /// 功能不支持 Function not supported.
10679a452ceShoumkh     ENOSYS = 38,
10779a452ceShoumkh     /// 目录非空 Directory not empty.
10879a452ceShoumkh     ENOTEMPTY = 39,
10979a452ceShoumkh     /// 符号链接级别过多 Too many levels of symbolic links.
11079a452ceShoumkh     ELOOP = 40,
11179a452ceShoumkh     /// 没有期待类型的消息 No message of the desired type.
11279a452ceShoumkh     ENOMSG = 41,
11379a452ceShoumkh     /// 标志符被移除 Identifier removed.
11479a452ceShoumkh     EIDRM = 42,
11579a452ceShoumkh     /// 通道号超出范围 Channel number out of range
11679a452ceShoumkh     ECHRNG = 43,
11779a452ceShoumkh     /// 二级不同步 Level 2 not synchronized
11879a452ceShoumkh     EL2NSYNC = 44,
11979a452ceShoumkh     /// 三级暂停 Level 3 halted
12079a452ceShoumkh     EL3HLT = 45,
12179a452ceShoumkh     /// 三级重置 Level 3 reset
12279a452ceShoumkh     EL3RST = 46,
12379a452ceShoumkh     /// 链接号超出范围 Link number out of range
12479a452ceShoumkh     ELNRNG = 47,
12579a452ceShoumkh     /// 未连接协议驱动程序 Protocol driver not attached
12679a452ceShoumkh     EUNATCH = 48,
12779a452ceShoumkh     /// 没有可用的CSI结构 No CSI structure available
12879a452ceShoumkh     ENOCSI = 49,
12979a452ceShoumkh     /// 二级暂停 Level 2 halted
13079a452ceShoumkh     EL2HLT = 50,
13179a452ceShoumkh     /// 无效交换 Invalid exchange
13279a452ceShoumkh     EBADE = 51,
13379a452ceShoumkh     /// 无效的请求描述符 Invalid request descriptor
13479a452ceShoumkh     EBADR = 52,
13579a452ceShoumkh     /// 交换满 Exchange full
13679a452ceShoumkh     EXFULL = 53,
13779a452ceShoumkh     /// 无阳极 No anode
13879a452ceShoumkh     ENOANO = 54,
13979a452ceShoumkh     /// 请求码无效 Invalid request code
14079a452ceShoumkh     EBADRQC = 55,
14179a452ceShoumkh     /// 无效插槽 Invalid slot
14279a452ceShoumkh     EBADSLT = 56,
14379a452ceShoumkh     /// 资源死锁 Resource deadlock would occur
14479a452ceShoumkh     EDEADLOCK = 57,
14579a452ceShoumkh     /// 错误的字体文件格式 Bad font file format
14679a452ceShoumkh     EBFONT = 58,
14779a452ceShoumkh     /// 不是STREAM Not a STREAM
14879a452ceShoumkh     ENOSTR = 59,
14979a452ceShoumkh     /// 队列头没有可读取的消息 No message is available on the STREAM head read queue.
15079a452ceShoumkh     ENODATA = 60,
15179a452ceShoumkh     /// 流式ioctl()超时 Stream ioctl() timeout
15279a452ceShoumkh     ETIME = 61,
15379a452ceShoumkh     /// 没有STREAM资源  No STREAM resources.
15479a452ceShoumkh     ENOSR = 62,
15579a452ceShoumkh     /// 机器不在网络上 Machine is not on the network
15679a452ceShoumkh     ENONET = 63,
15779a452ceShoumkh     /// 未安装软件包 Package not installed
15879a452ceShoumkh     ENOPKG = 64,
15979a452ceShoumkh     /// 远程对象 Object is remote
16079a452ceShoumkh     EREMOTE = 65,
16179a452ceShoumkh     /// 保留 Reserved.
16279a452ceShoumkh     ENOLINK = 66,
16379a452ceShoumkh     /// 外设错误 Advertise error.
16479a452ceShoumkh     EADV = 67,
16579a452ceShoumkh     /// 安装错误 Srmount error
16679a452ceShoumkh     ESRMNT = 68,
16779a452ceShoumkh     /// 发送时发生通信错误 Communication error on send
16879a452ceShoumkh     ECOMM = 69,
16979a452ceShoumkh     /// 协议错误 Protocol error.
17079a452ceShoumkh     EPROTO = 70,
17179a452ceShoumkh     /// 保留使用 Reserved.
17279a452ceShoumkh     EMULTIHOP = 71,
17379a452ceShoumkh     /// RFS特定错误 RFS specific error
17479a452ceShoumkh     EDOTDOT = 72,
17579a452ceShoumkh     /// 错误的消息 Bad message.
17679a452ceShoumkh     EBADMSG = 73,
17779a452ceShoumkh     /// 数值过大,产生溢出 Value too large to be stored in data type.
17879a452ceShoumkh     EOVERFLOW = 74,
17979a452ceShoumkh     /// 名称在网络上不是唯一的 Name not unique on network
18079a452ceShoumkh     ENOTUNIQ = 75,
18179a452ceShoumkh     /// 处于不良状态的文件描述符 File descriptor in bad state
18279a452ceShoumkh     EBADFD = 76,
18379a452ceShoumkh     /// 远程地址已更改 Remote address changed
18479a452ceShoumkh     EREMCHG = 77,
18579a452ceShoumkh     /// 无法访问所需的共享库 Can not access a needed shared library
18679a452ceShoumkh     ELIBACC = 78,
18779a452ceShoumkh     /// 访问损坏的共享库 Accessing a corrupted shared library
18879a452ceShoumkh     ELIBBAD = 79,
18979a452ceShoumkh     /// a. out中的.lib部分已损坏 .lib section in a.out corrupted
19079a452ceShoumkh     ELIBSCN = 80,
19179a452ceShoumkh     /// 尝试链接太多共享库 Attempting to link in too many shared libraries
19279a452ceShoumkh     ELIBMAX = 81,
19379a452ceShoumkh     /// 无法直接执行共享库 Cannot exec a shared library directly
19479a452ceShoumkh     ELIBEXEC = 82,
19579a452ceShoumkh     /// 不合法的字符序列 Illegal byte sequence.
19679a452ceShoumkh     EILSEQ = 83,
19779a452ceShoumkh     /// 中断的系统调用应该重新启动 Interrupted system call should be restarted
19879a452ceShoumkh     ERESTART = 84,
19979a452ceShoumkh     /// 流管道错误 Streams pipe error
20079a452ceShoumkh     ESTRPIPE = 85,
20179a452ceShoumkh     /// 用户太多 Too many users
20279a452ceShoumkh     EUSERS = 86,
20379a452ceShoumkh     /// 不是一个套接字 Not a socket.
20479a452ceShoumkh     ENOTSOCK = 87,
20579a452ceShoumkh     /// 需要目标地址 Destination address required.
20679a452ceShoumkh     EDESTADDRREQ = 88,
20779a452ceShoumkh     /// 消息过大 Message too large.
20879a452ceShoumkh     EMSGSIZE = 89,
20979a452ceShoumkh     /// 对于套接字而言,错误的协议 Protocol wrong type for socket.
21079a452ceShoumkh     EPROTOTYPE = 90,
21179a452ceShoumkh     /// 协议不可用 Protocol not available.
21279a452ceShoumkh     ENOPROTOOPT = 91,
21379a452ceShoumkh     /// 协议不被支持 Protocol not supported.
21479a452ceShoumkh     EPROTONOSUPPORT = 92,
21579a452ceShoumkh     /// 不支持套接字类型 Socket type not supported
21679a452ceShoumkh     ESOCKTNOSUPPORT = 93,
21779a452ceShoumkh     /// 套接字不支持该操作 Operation not supported on socket (may be the same value as [ENOTSUP]).
21879a452ceShoumkh     ///
21979a452ceShoumkh     /// 不被支持 Not supported (may be the same value as [EOPNOTSUPP]).
22079a452ceShoumkh     EOPNOTSUPP_OR_ENOTSUP = 94,
22179a452ceShoumkh     /// 不支持协议系列 Protocol family not supported
22279a452ceShoumkh     EPFNOSUPPORT = 95,
22379a452ceShoumkh     /// 地址family不支持 Address family not supported.
22479a452ceShoumkh     EAFNOSUPPORT = 96,
22579a452ceShoumkh     /// 地址正在被使用 Address in use.
22679a452ceShoumkh     EADDRINUSE = 97,
22779a452ceShoumkh     /// 地址不可用 Address  not available.
22879a452ceShoumkh     EADDRNOTAVAIL = 98,
22979a452ceShoumkh     /// 网络已关闭 Network is down.
23079a452ceShoumkh     ENETDOWN = 99,
23179a452ceShoumkh     /// 网络不可达 Network unreachable.
23279a452ceShoumkh     ENETUNREACH = 100,
23379a452ceShoumkh     /// 网络连接已断开 Connection aborted by network.
23479a452ceShoumkh     ENETRESET = 101,
23579a452ceShoumkh     /// 连接已断开 Connection aborted.
23679a452ceShoumkh     ECONNABORTED = 102,
23779a452ceShoumkh     /// 连接被重置 Connection reset.
23879a452ceShoumkh     ECONNRESET = 103,
23979a452ceShoumkh     /// 缓冲区空间不足 No buffer space available.
24079a452ceShoumkh     ENOBUFS = 104,
24179a452ceShoumkh     /// 套接字已连接 Socket is connected.
24279a452ceShoumkh     EISCONN = 105,
24379a452ceShoumkh     /// 套接字未连接 The socket is not connected.
24479a452ceShoumkh     ENOTCONN = 106,
24579a452ceShoumkh     /// 传输端点关闭后无法发送 Cannot send after transport endpoint shutdown
24679a452ceShoumkh     ESHUTDOWN = 107,
24779a452ceShoumkh     /// 引用太多:无法拼接 Too many references: cannot splice
24879a452ceShoumkh     ETOOMANYREFS = 108,
24979a452ceShoumkh     /// 连接超时 Connection timed out.
25079a452ceShoumkh     ETIMEDOUT = 109,
25179a452ceShoumkh     /// 连接被拒绝 Connection refused.
25279a452ceShoumkh     ECONNREFUSED = 110,
25379a452ceShoumkh     /// 主机已关闭 Host is down
25479a452ceShoumkh     EHOSTDOWN = 111,
25579a452ceShoumkh     /// 主机不可达 Host is unreachable.
25679a452ceShoumkh     EHOSTUNREACH = 112,
25779a452ceShoumkh     /// 连接已经在处理 Connection already in progress.
25879a452ceShoumkh     EALREADY = 113,
25979a452ceShoumkh     /// 操作正在处理 Operation in progress.
26079a452ceShoumkh     EINPROGRESS = 114,
26179a452ceShoumkh     /// 保留 Reserved.
26279a452ceShoumkh     ESTALE = 115,
26379a452ceShoumkh     /// 结构需要清理 Structure needs cleaning
26479a452ceShoumkh     EUCLEAN = 116,
26579a452ceShoumkh     /// 不是XENIX命名类型文件 Not a XENIX named type file
26679a452ceShoumkh     ENOTNAM = 117,
26779a452ceShoumkh     /// 没有可用的XENIX信号量 No XENIX semaphores available
26879a452ceShoumkh     ENAVAIL = 118,
26979a452ceShoumkh     /// 是命名类型文件 Is a named type file
27079a452ceShoumkh     EISNAM = 119,
27179a452ceShoumkh     /// 远程I/O错误 Remote I/O error
27279a452ceShoumkh     EREMOTEIO = 120,
27379a452ceShoumkh     /// 保留使用 Reserved
27479a452ceShoumkh     EDQUOT = 121,
27579a452ceShoumkh     /// 没有找到媒介 No medium found
27679a452ceShoumkh     ENOMEDIUM = 122,
27779a452ceShoumkh     /// 介质类型错误 Wrong medium type
27879a452ceShoumkh     EMEDIUMTYPE = 123,
27979a452ceShoumkh     /// 操作被取消 Operation canceled.
28079a452ceShoumkh     ECANCELED = 124,
28179a452ceShoumkh     /// 所需的密钥不可用 Required key not available
28279a452ceShoumkh     ENOKEY = 125,
28379a452ceShoumkh     /// 密钥已过期 Key has expired
28479a452ceShoumkh     EKEYEXPIRED = 126,
28579a452ceShoumkh     /// 密钥已被撤销 Key has been revoked
28679a452ceShoumkh     EKEYREVOKED = 127,
28779a452ceShoumkh     /// 密钥被服务拒绝 Key has been revoked
28879a452ceShoumkh     EKEYREJECTED = 128,
28979a452ceShoumkh     /// 之前的拥有者挂了 Previous owner died.
29079a452ceShoumkh     EOWNERDEAD = 129,
29179a452ceShoumkh     /// 状态不可恢复 State not recoverable.
29279a452ceShoumkh     ENOTRECOVERABLE = 130,
2934454d1a2Slogin }
294676b8ef6SMork 
295676b8ef6SMork impl SystemError {
296676b8ef6SMork     /// @brief 把posix错误码转换为系统错误枚举类型。
297676b8ef6SMork     pub fn from_posix_errno(errno: i32) -> Option<SystemError> {
298676b8ef6SMork         // posix 错误码是小于0的
299676b8ef6SMork         if errno >= 0 {
300676b8ef6SMork             return None;
301676b8ef6SMork         }
302676b8ef6SMork         return <Self as FromPrimitive>::from_i32(-errno);
303676b8ef6SMork     }
304676b8ef6SMork 
305676b8ef6SMork     /// @brief 把系统错误枚举类型转换为负数posix错误码。
306676b8ef6SMork     pub fn to_posix_errno(&self) -> i32 {
307676b8ef6SMork         return -<Self as ToPrimitive>::to_i32(self).unwrap();
308676b8ef6SMork     }
309676b8ef6SMork }
310ab5c8ca4Slogin 
311ab5c8ca4Slogin // 定义系统调用号
312ab5c8ca4Slogin pub const SYS_PUT_STRING: usize = 1;
313ab5c8ca4Slogin pub const SYS_OPEN: usize = 2;
314ab5c8ca4Slogin pub const SYS_CLOSE: usize = 3;
315ab5c8ca4Slogin pub const SYS_READ: usize = 4;
316ab5c8ca4Slogin pub const SYS_WRITE: usize = 5;
317ab5c8ca4Slogin pub const SYS_LSEEK: usize = 6;
318ab5c8ca4Slogin pub const SYS_FORK: usize = 7;
319ab5c8ca4Slogin pub const SYS_VFORK: usize = 8;
320ab5c8ca4Slogin pub const SYS_BRK: usize = 9;
321ab5c8ca4Slogin pub const SYS_SBRK: usize = 10;
322ab5c8ca4Slogin 
323ab5c8ca4Slogin pub const SYS_REBOOT: usize = 11;
324ab5c8ca4Slogin pub const SYS_CHDIR: usize = 12;
325ab5c8ca4Slogin pub const SYS_GET_DENTS: usize = 13;
326ab5c8ca4Slogin pub const SYS_EXECVE: usize = 14;
327ab5c8ca4Slogin pub const SYS_WAIT4: usize = 15;
328ab5c8ca4Slogin pub const SYS_EXIT: usize = 16;
329ab5c8ca4Slogin pub const SYS_MKDIR: usize = 17;
330ab5c8ca4Slogin pub const SYS_NANOSLEEP: usize = 18;
331ab5c8ca4Slogin /// todo: 该系统调用与Linux不一致,将来需要删除该系统调用!!! 删的时候记得改C版本的libc
332ab5c8ca4Slogin pub const SYS_CLOCK: usize = 19;
333ab5c8ca4Slogin pub const SYS_PIPE: usize = 20;
334ab5c8ca4Slogin 
335ab5c8ca4Slogin /// todo: 该系统调用不是符合POSIX标准的,在将来需要删除!!!
336ab5c8ca4Slogin pub const SYS_MSTAT: usize = 21;
337ab5c8ca4Slogin pub const SYS_UNLINK_AT: usize = 22;
338ab5c8ca4Slogin pub const SYS_KILL: usize = 23;
339ab5c8ca4Slogin pub const SYS_SIGACTION: usize = 24;
340ab5c8ca4Slogin pub const SYS_RT_SIGRETURN: usize = 25;
341ab5c8ca4Slogin pub const SYS_GETPID: usize = 26;
342ab5c8ca4Slogin pub const SYS_SCHED: usize = 27;
343ab5c8ca4Slogin pub const SYS_DUP: usize = 28;
344ab5c8ca4Slogin pub const SYS_DUP2: usize = 29;
345ab5c8ca4Slogin pub const SYS_SOCKET: usize = 30;
346ab5c8ca4Slogin 
347ab5c8ca4Slogin pub const SYS_SETSOCKOPT: usize = 31;
348ab5c8ca4Slogin pub const SYS_GETSOCKOPT: usize = 32;
349ab5c8ca4Slogin pub const SYS_CONNECT: usize = 33;
350ab5c8ca4Slogin pub const SYS_BIND: usize = 34;
351ab5c8ca4Slogin pub const SYS_SENDTO: usize = 35;
352ab5c8ca4Slogin pub const SYS_RECVFROM: usize = 36;
353ab5c8ca4Slogin pub const SYS_RECVMSG: usize = 37;
354ab5c8ca4Slogin pub const SYS_LISTEN: usize = 38;
355ab5c8ca4Slogin pub const SYS_SHUTDOWN: usize = 39;
356ab5c8ca4Slogin pub const SYS_ACCEPT: usize = 40;
357ab5c8ca4Slogin 
358ab5c8ca4Slogin pub const SYS_GETSOCKNAME: usize = 41;
359ab5c8ca4Slogin pub const SYS_GETPEERNAME: usize = 42;
360*36fd0130Shoumkh pub const SYS_GETTIMEOFDAY: usize = 43;
361ab5c8ca4Slogin 
362ab5c8ca4Slogin #[derive(Debug)]
363ab5c8ca4Slogin pub struct Syscall;
364ab5c8ca4Slogin 
365ab5c8ca4Slogin extern "C" {
366ab5c8ca4Slogin     fn do_put_string(s: *const u8, front_color: u32, back_color: u32) -> usize;
367ab5c8ca4Slogin }
368ab5c8ca4Slogin 
369ab5c8ca4Slogin #[no_mangle]
370ab5c8ca4Slogin pub extern "C" fn syscall_init() -> i32 {
371ab5c8ca4Slogin     kinfo!("Initializing syscall...");
372ab5c8ca4Slogin     Syscall::init().expect("syscall init failed");
373ab5c8ca4Slogin     kinfo!("Syscall init successfully!");
374ab5c8ca4Slogin     return 0;
375ab5c8ca4Slogin }
376ab5c8ca4Slogin 
377ab5c8ca4Slogin impl Syscall {
378ab5c8ca4Slogin     /// 初始化系统调用
379ab5c8ca4Slogin     pub fn init() -> Result<(), SystemError> {
380ab5c8ca4Slogin         static INIT_FLAG: AtomicBool = AtomicBool::new(false);
381ab5c8ca4Slogin         let prev = INIT_FLAG.swap(true, Ordering::SeqCst);
382ab5c8ca4Slogin         if prev {
383ab5c8ca4Slogin             panic!("Cannot initialize syscall more than once!");
384ab5c8ca4Slogin         }
385ab5c8ca4Slogin         return crate::arch::syscall::arch_syscall_init();
386ab5c8ca4Slogin     }
387ab5c8ca4Slogin     /// @brief 系统调用分发器,用于分发系统调用。
388ab5c8ca4Slogin     ///
389ab5c8ca4Slogin     /// 这个函数内,需要根据系统调用号,调用对应的系统调用处理函数。
390ab5c8ca4Slogin     /// 并且,对于用户态传入的指针参数,需要在本函数内进行越界检查,防止访问到内核空间。
391ab5c8ca4Slogin     pub fn handle(syscall_num: usize, args: &[usize], from_user: bool) -> usize {
392ab5c8ca4Slogin         let r = match syscall_num {
393ab5c8ca4Slogin             SYS_PUT_STRING => {
394ab5c8ca4Slogin                 Self::put_string(args[0] as *const u8, args[1] as u32, args[2] as u32)
395ab5c8ca4Slogin             }
396ab5c8ca4Slogin             SYS_OPEN => {
397ab5c8ca4Slogin                 let path: &CStr = unsafe { CStr::from_ptr(args[0] as *const c_char) };
398ab5c8ca4Slogin                 let path: Result<&str, core::str::Utf8Error> = path.to_str();
399ab5c8ca4Slogin                 let res = if path.is_err() {
400ab5c8ca4Slogin                     Err(SystemError::EINVAL)
401ab5c8ca4Slogin                 } else {
402ab5c8ca4Slogin                     let path: &str = path.unwrap();
403ab5c8ca4Slogin                     let flags = args[1];
404ab5c8ca4Slogin                     let open_flags: FileMode = FileMode::from_bits_truncate(flags as u32);
405ab5c8ca4Slogin 
406ab5c8ca4Slogin                     Self::open(path, open_flags)
407ab5c8ca4Slogin                 };
408ab5c8ca4Slogin                 // kdebug!("open: {:?}, res: {:?}", path, res);
409ab5c8ca4Slogin                 res
410ab5c8ca4Slogin             }
411ab5c8ca4Slogin             SYS_CLOSE => {
412ab5c8ca4Slogin                 let fd = args[0];
413ab5c8ca4Slogin                 Self::close(fd)
414ab5c8ca4Slogin             }
415ab5c8ca4Slogin             SYS_READ => {
416ab5c8ca4Slogin                 let fd = args[0] as i32;
417ab5c8ca4Slogin                 let buf_vaddr = args[1];
418ab5c8ca4Slogin                 let len = args[2];
419ab5c8ca4Slogin 
420ab5c8ca4Slogin                 // 判断缓冲区是否来自用户态,进行权限校验
421ab5c8ca4Slogin                 let res = if from_user && unsafe { !verify_area(buf_vaddr as u64, len as u64) } {
422ab5c8ca4Slogin                     // 来自用户态,而buffer在内核态,这样的操作不被允许
423ab5c8ca4Slogin                     Err(SystemError::EPERM)
424ab5c8ca4Slogin                 } else {
425ab5c8ca4Slogin                     let buf: &mut [u8] = unsafe {
426ab5c8ca4Slogin                         core::slice::from_raw_parts_mut::<'static, u8>(buf_vaddr as *mut u8, len)
427ab5c8ca4Slogin                     };
428ab5c8ca4Slogin                     Self::read(fd, buf)
429ab5c8ca4Slogin                 };
430ab5c8ca4Slogin 
431ab5c8ca4Slogin                 res
432ab5c8ca4Slogin             }
433ab5c8ca4Slogin             SYS_WRITE => {
434ab5c8ca4Slogin                 let fd = args[0] as i32;
435ab5c8ca4Slogin                 let buf_vaddr = args[1];
436ab5c8ca4Slogin                 let len = args[2];
437ab5c8ca4Slogin 
438ab5c8ca4Slogin                 // 判断缓冲区是否来自用户态,进行权限校验
439ab5c8ca4Slogin                 let res = if from_user && unsafe { !verify_area(buf_vaddr as u64, len as u64) } {
440ab5c8ca4Slogin                     // 来自用户态,而buffer在内核态,这样的操作不被允许
441ab5c8ca4Slogin                     Err(SystemError::EPERM)
442ab5c8ca4Slogin                 } else {
443ab5c8ca4Slogin                     let buf: &[u8] = unsafe {
444ab5c8ca4Slogin                         core::slice::from_raw_parts::<'static, u8>(buf_vaddr as *const u8, len)
445ab5c8ca4Slogin                     };
446ab5c8ca4Slogin                     Self::write(fd, buf)
447ab5c8ca4Slogin                 };
448ab5c8ca4Slogin 
449ab5c8ca4Slogin                 res
450ab5c8ca4Slogin             }
451ab5c8ca4Slogin 
452ab5c8ca4Slogin             SYS_LSEEK => {
453ab5c8ca4Slogin                 let fd = args[0] as i32;
454ab5c8ca4Slogin                 let offset = args[1] as i64;
455ab5c8ca4Slogin                 let whence = args[2] as u32;
456ab5c8ca4Slogin 
457ab5c8ca4Slogin                 let w = match whence {
458ab5c8ca4Slogin                     SEEK_SET => Ok(SeekFrom::SeekSet(offset)),
459ab5c8ca4Slogin                     SEEK_CUR => Ok(SeekFrom::SeekCurrent(offset)),
460ab5c8ca4Slogin                     SEEK_END => Ok(SeekFrom::SeekEnd(offset)),
461ab5c8ca4Slogin                     SEEK_MAX => Ok(SeekFrom::SeekEnd(0)),
462ab5c8ca4Slogin                     _ => Err(SystemError::EINVAL),
463ab5c8ca4Slogin                 };
464ab5c8ca4Slogin 
465ab5c8ca4Slogin                 let res = if w.is_err() {
466ab5c8ca4Slogin                     Err(w.unwrap_err())
467ab5c8ca4Slogin                 } else {
468ab5c8ca4Slogin                     let w = w.unwrap();
469ab5c8ca4Slogin                     Self::lseek(fd, w)
470ab5c8ca4Slogin                 };
471ab5c8ca4Slogin 
472ab5c8ca4Slogin                 res
473ab5c8ca4Slogin             }
474ab5c8ca4Slogin 
475ab5c8ca4Slogin             SYS_BRK => {
476ab5c8ca4Slogin                 let new_brk = args[0];
477ab5c8ca4Slogin                 Self::brk(new_brk)
478ab5c8ca4Slogin             }
479ab5c8ca4Slogin 
480ab5c8ca4Slogin             SYS_SBRK => {
481ab5c8ca4Slogin                 let increment = args[0] as isize;
482ab5c8ca4Slogin                 Self::sbrk(increment)
483ab5c8ca4Slogin             }
484ab5c8ca4Slogin 
485ab5c8ca4Slogin             SYS_REBOOT => Self::reboot(),
486ab5c8ca4Slogin 
487ab5c8ca4Slogin             SYS_CHDIR => {
488ab5c8ca4Slogin                 // Closure for checking arguments
489ab5c8ca4Slogin                 let chdir_check = |arg0: usize| {
490ab5c8ca4Slogin                     if arg0 == 0 {
491ab5c8ca4Slogin                         return Err(SystemError::EFAULT);
492ab5c8ca4Slogin                     }
493ab5c8ca4Slogin                     let path_ptr = arg0 as *const c_char;
494ab5c8ca4Slogin                     // 权限校验
495ab5c8ca4Slogin                     if path_ptr.is_null()
496ab5c8ca4Slogin                         || (from_user
497ab5c8ca4Slogin                             && unsafe { !verify_area(path_ptr as u64, PAGE_2M_SIZE as u64) })
498ab5c8ca4Slogin                     {
499ab5c8ca4Slogin                         return Err(SystemError::EINVAL);
500ab5c8ca4Slogin                     }
501ab5c8ca4Slogin                     let dest_path: &CStr = unsafe { CStr::from_ptr(path_ptr) };
502ab5c8ca4Slogin                     let dest_path: &str = dest_path.to_str().map_err(|_| SystemError::EINVAL)?;
503ab5c8ca4Slogin                     if dest_path.len() == 0 {
504ab5c8ca4Slogin                         return Err(SystemError::EINVAL);
505ab5c8ca4Slogin                     } else if dest_path.len() > PAGE_4K_SIZE as usize {
506ab5c8ca4Slogin                         return Err(SystemError::ENAMETOOLONG);
507ab5c8ca4Slogin                     }
508ab5c8ca4Slogin 
509ab5c8ca4Slogin                     return Ok(dest_path);
510ab5c8ca4Slogin                 };
511ab5c8ca4Slogin 
512ab5c8ca4Slogin                 let r: Result<&str, SystemError> = chdir_check(args[0]);
513ab5c8ca4Slogin                 if r.is_err() {
514ab5c8ca4Slogin                     Err(r.unwrap_err())
515ab5c8ca4Slogin                 } else {
516ab5c8ca4Slogin                     Self::chdir(r.unwrap())
517ab5c8ca4Slogin                 }
518ab5c8ca4Slogin             }
519ab5c8ca4Slogin 
520ab5c8ca4Slogin             SYS_GET_DENTS => {
521ab5c8ca4Slogin                 let fd = args[0] as i32;
522ab5c8ca4Slogin                 let buf_vaddr = args[1];
523ab5c8ca4Slogin                 let len = args[2];
524ab5c8ca4Slogin 
525ab5c8ca4Slogin                 // 判断缓冲区是否来自用户态,进行权限校验
526ab5c8ca4Slogin                 let res = if from_user && unsafe { !verify_area(buf_vaddr as u64, len as u64) } {
527ab5c8ca4Slogin                     // 来自用户态,而buffer在内核态,这样的操作不被允许
528ab5c8ca4Slogin                     Err(SystemError::EPERM)
529ab5c8ca4Slogin                 } else if buf_vaddr == 0 {
530ab5c8ca4Slogin                     Err(SystemError::EFAULT)
531ab5c8ca4Slogin                 } else {
532ab5c8ca4Slogin                     let buf: &mut [u8] = unsafe {
533ab5c8ca4Slogin                         core::slice::from_raw_parts_mut::<'static, u8>(buf_vaddr as *mut u8, len)
534ab5c8ca4Slogin                     };
535ab5c8ca4Slogin                     Self::getdents(fd, buf)
536ab5c8ca4Slogin                 };
537ab5c8ca4Slogin 
538ab5c8ca4Slogin                 res
539ab5c8ca4Slogin             }
540ab5c8ca4Slogin 
541ab5c8ca4Slogin             SYS_EXECVE => {
542ab5c8ca4Slogin                 let path_ptr = args[0];
543ab5c8ca4Slogin                 let argv_ptr = args[1];
544ab5c8ca4Slogin                 let env_ptr = args[2];
545ab5c8ca4Slogin 
546ab5c8ca4Slogin                 // 权限校验
547ab5c8ca4Slogin                 if from_user
548ab5c8ca4Slogin                     && (unsafe { !verify_area(path_ptr as u64, PAGE_4K_SIZE as u64) }
549ab5c8ca4Slogin                         || unsafe { !verify_area(argv_ptr as u64, PAGE_4K_SIZE as u64) })
550ab5c8ca4Slogin                     || unsafe { !verify_area(env_ptr as u64, PAGE_4K_SIZE as u64) }
551ab5c8ca4Slogin                 {
552ab5c8ca4Slogin                     Err(SystemError::EFAULT)
553ab5c8ca4Slogin                 } else {
554ab5c8ca4Slogin                     Self::execve(
555ab5c8ca4Slogin                         path_ptr as *const c_void,
556ab5c8ca4Slogin                         argv_ptr as *const *const c_void,
557ab5c8ca4Slogin                         env_ptr as *const *const c_void,
558ab5c8ca4Slogin                     )
559ab5c8ca4Slogin                 }
560ab5c8ca4Slogin             }
561ab5c8ca4Slogin             SYS_WAIT4 => {
562ab5c8ca4Slogin                 let pid = args[0] as pid_t;
563ab5c8ca4Slogin                 let wstatus = args[1] as *mut c_int;
564ab5c8ca4Slogin                 let options = args[2] as c_int;
565ab5c8ca4Slogin                 let rusage = args[3] as *mut c_void;
566ab5c8ca4Slogin 
567ab5c8ca4Slogin                 // 权限校验
568ab5c8ca4Slogin                 // todo: 引入rusage之后,更正以下权限校验代码中,rusage的大小
569ab5c8ca4Slogin                 if from_user
570ab5c8ca4Slogin                     && (unsafe {
571ab5c8ca4Slogin                         !verify_area(wstatus as u64, core::mem::size_of::<c_int>() as u64)
572ab5c8ca4Slogin                     } || unsafe { !verify_area(rusage as u64, PAGE_4K_SIZE as u64) })
573ab5c8ca4Slogin                 {
574ab5c8ca4Slogin                     Err(SystemError::EFAULT)
575ab5c8ca4Slogin                 } else {
576ab5c8ca4Slogin                     Self::wait4(pid, wstatus, options, rusage)
577ab5c8ca4Slogin                 }
578ab5c8ca4Slogin             }
579ab5c8ca4Slogin 
580ab5c8ca4Slogin             SYS_EXIT => {
581ab5c8ca4Slogin                 let exit_code = args[0];
582ab5c8ca4Slogin                 Self::exit(exit_code)
583ab5c8ca4Slogin             }
584ab5c8ca4Slogin             SYS_MKDIR => {
585ab5c8ca4Slogin                 let path_ptr = args[0] as *const c_char;
586ab5c8ca4Slogin                 let mode = args[1];
587ab5c8ca4Slogin 
588ab5c8ca4Slogin                 let security_check = || {
589ab5c8ca4Slogin                     if path_ptr.is_null()
590ab5c8ca4Slogin                         || (from_user
591ab5c8ca4Slogin                             && unsafe { !verify_area(path_ptr as u64, PAGE_2M_SIZE as u64) })
592ab5c8ca4Slogin                     {
593ab5c8ca4Slogin                         return Err(SystemError::EINVAL);
594ab5c8ca4Slogin                     }
595ab5c8ca4Slogin                     let path: &CStr = unsafe { CStr::from_ptr(path_ptr) };
596ab5c8ca4Slogin                     let path: &str = path.to_str().map_err(|_| SystemError::EINVAL)?.trim();
597ab5c8ca4Slogin 
598ab5c8ca4Slogin                     if path == "" {
599ab5c8ca4Slogin                         return Err(SystemError::EINVAL);
600ab5c8ca4Slogin                     }
601ab5c8ca4Slogin                     return Ok(path);
602ab5c8ca4Slogin                 };
603ab5c8ca4Slogin 
604ab5c8ca4Slogin                 let path = security_check();
605ab5c8ca4Slogin                 if path.is_err() {
606ab5c8ca4Slogin                     Err(path.unwrap_err())
607ab5c8ca4Slogin                 } else {
608ab5c8ca4Slogin                     Self::mkdir(path.unwrap(), mode)
609ab5c8ca4Slogin                 }
610ab5c8ca4Slogin             }
611ab5c8ca4Slogin 
612ab5c8ca4Slogin             SYS_NANOSLEEP => {
613ab5c8ca4Slogin                 let req = args[0] as *const TimeSpec;
614ab5c8ca4Slogin                 let rem = args[1] as *mut TimeSpec;
615ab5c8ca4Slogin                 if from_user
616ab5c8ca4Slogin                     && (unsafe {
617ab5c8ca4Slogin                         !verify_area(req as u64, core::mem::size_of::<TimeSpec>() as u64)
618ab5c8ca4Slogin                     } || unsafe {
619ab5c8ca4Slogin                         !verify_area(rem as u64, core::mem::size_of::<TimeSpec>() as u64)
620ab5c8ca4Slogin                     })
621ab5c8ca4Slogin                 {
622ab5c8ca4Slogin                     Err(SystemError::EFAULT)
623ab5c8ca4Slogin                 } else {
624ab5c8ca4Slogin                     Self::nanosleep(req, rem)
625ab5c8ca4Slogin                 }
626ab5c8ca4Slogin             }
627ab5c8ca4Slogin 
628ab5c8ca4Slogin             SYS_CLOCK => Self::clock(),
629ab5c8ca4Slogin             SYS_PIPE => {
630ab5c8ca4Slogin                 let pipefd = args[0] as *mut c_int;
631ab5c8ca4Slogin                 if from_user
632ab5c8ca4Slogin                     && unsafe {
633ab5c8ca4Slogin                         !verify_area(pipefd as u64, core::mem::size_of::<[c_int; 2]>() as u64)
634ab5c8ca4Slogin                     }
635ab5c8ca4Slogin                 {
636ab5c8ca4Slogin                     Err(SystemError::EFAULT)
637ab5c8ca4Slogin                 } else if pipefd.is_null() {
638ab5c8ca4Slogin                     Err(SystemError::EFAULT)
639ab5c8ca4Slogin                 } else {
640ab5c8ca4Slogin                     let pipefd = unsafe { core::slice::from_raw_parts_mut(pipefd, 2) };
641ab5c8ca4Slogin                     Self::pipe(pipefd)
642ab5c8ca4Slogin                 }
643ab5c8ca4Slogin             }
644ab5c8ca4Slogin 
645ab5c8ca4Slogin             SYS_MSTAT => {
646ab5c8ca4Slogin                 let dst = args[0] as *mut mm_stat_t;
647ab5c8ca4Slogin                 if from_user
648ab5c8ca4Slogin                     && unsafe { !verify_area(dst as u64, core::mem::size_of::<mm_stat_t>() as u64) }
649ab5c8ca4Slogin                 {
650ab5c8ca4Slogin                     Err(SystemError::EFAULT)
651ab5c8ca4Slogin                 } else if dst.is_null() {
652ab5c8ca4Slogin                     Err(SystemError::EFAULT)
653ab5c8ca4Slogin                 } else {
654ab5c8ca4Slogin                     Self::mstat(dst, from_user)
655ab5c8ca4Slogin                 }
656ab5c8ca4Slogin             }
657ab5c8ca4Slogin             SYS_UNLINK_AT => {
658ab5c8ca4Slogin                 let dirfd = args[0] as i32;
659ab5c8ca4Slogin                 let pathname = args[1] as *const c_char;
660ab5c8ca4Slogin                 let flags = args[2] as u32;
661ab5c8ca4Slogin                 if from_user && unsafe { !verify_area(pathname as u64, PAGE_4K_SIZE as u64) } {
662ab5c8ca4Slogin                     Err(SystemError::EFAULT)
663ab5c8ca4Slogin                 } else if pathname.is_null() {
664ab5c8ca4Slogin                     Err(SystemError::EFAULT)
665ab5c8ca4Slogin                 } else {
666ab5c8ca4Slogin                     let get_path = || {
667ab5c8ca4Slogin                         let pathname: &CStr = unsafe { CStr::from_ptr(pathname) };
668ab5c8ca4Slogin 
669ab5c8ca4Slogin                         let pathname: &str = pathname.to_str().map_err(|_| SystemError::EINVAL)?;
670ab5c8ca4Slogin                         if pathname.len() >= MAX_PATHLEN {
671ab5c8ca4Slogin                             return Err(SystemError::ENAMETOOLONG);
672ab5c8ca4Slogin                         }
673ab5c8ca4Slogin                         return Ok(pathname.trim());
674ab5c8ca4Slogin                     };
675ab5c8ca4Slogin                     let pathname = get_path();
676ab5c8ca4Slogin                     if pathname.is_err() {
677ab5c8ca4Slogin                         Err(pathname.unwrap_err())
678ab5c8ca4Slogin                     } else {
679ab5c8ca4Slogin                         Self::unlinkat(dirfd, pathname.unwrap(), flags)
680ab5c8ca4Slogin                     }
681ab5c8ca4Slogin                 }
682ab5c8ca4Slogin             }
683ab5c8ca4Slogin             SYS_KILL => {
684ab5c8ca4Slogin                 let pid = args[0] as pid_t;
685ab5c8ca4Slogin                 let sig = args[1] as c_int;
686ab5c8ca4Slogin 
687ab5c8ca4Slogin                 Self::kill(pid, sig)
688ab5c8ca4Slogin             }
689ab5c8ca4Slogin 
690ab5c8ca4Slogin             SYS_SIGACTION => {
691ab5c8ca4Slogin                 let sig = args[0] as c_int;
692ab5c8ca4Slogin                 let act = args[1];
693ab5c8ca4Slogin                 let old_act = args[2];
694ab5c8ca4Slogin                 Self::sigaction(sig, act, old_act, from_user)
695ab5c8ca4Slogin             }
696ab5c8ca4Slogin 
697ab5c8ca4Slogin             SYS_RT_SIGRETURN => {
698ab5c8ca4Slogin                 // 由于目前signal机制的实现,与x86_64强关联,因此暂时在arch/x86_64/syscall.rs中调用
699ab5c8ca4Slogin                 // todo: 未来需要将signal机制与平台解耦
700ab5c8ca4Slogin                 todo!()
701ab5c8ca4Slogin             }
702ab5c8ca4Slogin 
703ab5c8ca4Slogin             SYS_GETPID => Self::getpid(),
704ab5c8ca4Slogin 
705ab5c8ca4Slogin             SYS_SCHED => Self::sched(from_user),
706ab5c8ca4Slogin             SYS_DUP => {
707ab5c8ca4Slogin                 let oldfd: i32 = args[0] as c_int;
708ab5c8ca4Slogin                 Self::dup(oldfd)
709ab5c8ca4Slogin             }
710ab5c8ca4Slogin             SYS_DUP2 => {
711ab5c8ca4Slogin                 let oldfd: i32 = args[0] as c_int;
712ab5c8ca4Slogin                 let newfd: i32 = args[1] as c_int;
713ab5c8ca4Slogin                 Self::dup2(oldfd, newfd)
714ab5c8ca4Slogin             }
715ab5c8ca4Slogin 
716ab5c8ca4Slogin             SYS_SOCKET => Self::socket(args[0], args[1], args[2]),
717ab5c8ca4Slogin             SYS_SETSOCKOPT => {
718ab5c8ca4Slogin                 let optval = args[3] as *const u8;
719ab5c8ca4Slogin                 let optlen = args[4] as usize;
720ab5c8ca4Slogin                 // 验证optval的地址是否合法
721ab5c8ca4Slogin                 if unsafe { verify_area(optval as u64, optlen as u64) } == false {
722ab5c8ca4Slogin                     // 地址空间超出了用户空间的范围,不合法
723ab5c8ca4Slogin                     Err(SystemError::EFAULT)
724ab5c8ca4Slogin                 } else {
725ab5c8ca4Slogin                     let data: &[u8] = unsafe { core::slice::from_raw_parts(optval, optlen) };
726ab5c8ca4Slogin                     Self::setsockopt(args[0], args[1], args[2], data)
727ab5c8ca4Slogin                 }
728ab5c8ca4Slogin             }
729ab5c8ca4Slogin             SYS_GETSOCKOPT => {
730ab5c8ca4Slogin                 let optval = args[3] as *mut u8;
731ab5c8ca4Slogin                 let optlen = args[4] as *mut usize;
732ab5c8ca4Slogin 
733ab5c8ca4Slogin                 let security_check = || {
734ab5c8ca4Slogin                     // 验证optval的地址是否合法
735ab5c8ca4Slogin                     if unsafe { verify_area(optval as u64, PAGE_4K_SIZE as u64) } == false {
736ab5c8ca4Slogin                         // 地址空间超出了用户空间的范围,不合法
737ab5c8ca4Slogin                         return Err(SystemError::EFAULT);
738ab5c8ca4Slogin                     }
739ab5c8ca4Slogin 
740ab5c8ca4Slogin                     // 验证optlen的地址是否合法
741ab5c8ca4Slogin                     if unsafe { verify_area(optlen as u64, core::mem::size_of::<u32>() as u64) }
742ab5c8ca4Slogin                         == false
743ab5c8ca4Slogin                     {
744ab5c8ca4Slogin                         // 地址空间超出了用户空间的范围,不合法
745ab5c8ca4Slogin                         return Err(SystemError::EFAULT);
746ab5c8ca4Slogin                     }
747ab5c8ca4Slogin                     return Ok(());
748ab5c8ca4Slogin                 };
749ab5c8ca4Slogin                 let r = security_check();
750ab5c8ca4Slogin                 if r.is_err() {
751ab5c8ca4Slogin                     Err(r.unwrap_err())
752ab5c8ca4Slogin                 } else {
753ab5c8ca4Slogin                     Self::getsockopt(args[0], args[1], args[2], optval, optlen as *mut u32)
754ab5c8ca4Slogin                 }
755ab5c8ca4Slogin             }
756ab5c8ca4Slogin 
757ab5c8ca4Slogin             SYS_CONNECT => {
758ab5c8ca4Slogin                 let addr = args[1] as *const SockAddr;
759ab5c8ca4Slogin                 let addrlen = args[2] as usize;
760ab5c8ca4Slogin                 // 验证addr的地址是否合法
761ab5c8ca4Slogin                 if unsafe { verify_area(addr as u64, addrlen as u64) } == false {
762ab5c8ca4Slogin                     // 地址空间超出了用户空间的范围,不合法
763ab5c8ca4Slogin                     Err(SystemError::EFAULT)
764ab5c8ca4Slogin                 } else {
765ab5c8ca4Slogin                     Self::connect(args[0], addr, addrlen)
766ab5c8ca4Slogin                 }
767ab5c8ca4Slogin             }
768ab5c8ca4Slogin             SYS_BIND => {
769ab5c8ca4Slogin                 let addr = args[1] as *const SockAddr;
770ab5c8ca4Slogin                 let addrlen = args[2] as usize;
771ab5c8ca4Slogin                 // 验证addr的地址是否合法
772ab5c8ca4Slogin                 if unsafe { verify_area(addr as u64, addrlen as u64) } == false {
773ab5c8ca4Slogin                     // 地址空间超出了用户空间的范围,不合法
774ab5c8ca4Slogin                     Err(SystemError::EFAULT)
775ab5c8ca4Slogin                 } else {
776ab5c8ca4Slogin                     Self::bind(args[0], addr, addrlen)
777ab5c8ca4Slogin                 }
778ab5c8ca4Slogin             }
779ab5c8ca4Slogin 
780ab5c8ca4Slogin             SYS_SENDTO => {
781ab5c8ca4Slogin                 let buf = args[1] as *const u8;
782ab5c8ca4Slogin                 let len = args[2] as usize;
783ab5c8ca4Slogin                 let flags = args[3] as u32;
784ab5c8ca4Slogin                 let addr = args[4] as *const SockAddr;
785ab5c8ca4Slogin                 let addrlen = args[5] as usize;
786ab5c8ca4Slogin                 // 验证buf的地址是否合法
787ab5c8ca4Slogin                 if unsafe { verify_area(buf as u64, len as u64) } == false {
788ab5c8ca4Slogin                     // 地址空间超出了用户空间的范围,不合法
789ab5c8ca4Slogin                     Err(SystemError::EFAULT)
790ab5c8ca4Slogin                 } else if unsafe { verify_area(addr as u64, addrlen as u64) } == false {
791ab5c8ca4Slogin                     // 地址空间超出了用户空间的范围,不合法
792ab5c8ca4Slogin                     Err(SystemError::EFAULT)
793ab5c8ca4Slogin                 } else {
794ab5c8ca4Slogin                     let data: &[u8] = unsafe { core::slice::from_raw_parts(buf, len) };
795ab5c8ca4Slogin                     Self::sendto(args[0], data, flags, addr, addrlen)
796ab5c8ca4Slogin                 }
797ab5c8ca4Slogin             }
798ab5c8ca4Slogin 
799ab5c8ca4Slogin             SYS_RECVFROM => {
800ab5c8ca4Slogin                 let buf = args[1] as *mut u8;
801ab5c8ca4Slogin                 let len = args[2] as usize;
802ab5c8ca4Slogin                 let flags = args[3] as u32;
803ab5c8ca4Slogin                 let addr = args[4] as *mut SockAddr;
804ab5c8ca4Slogin                 let addrlen = args[5] as *mut usize;
805ab5c8ca4Slogin 
806ab5c8ca4Slogin                 let security_check = || {
807ab5c8ca4Slogin                     // 验证buf的地址是否合法
808ab5c8ca4Slogin                     if unsafe { verify_area(buf as u64, len as u64) } == false {
809ab5c8ca4Slogin                         // 地址空间超出了用户空间的范围,不合法
810ab5c8ca4Slogin                         return Err(SystemError::EFAULT);
811ab5c8ca4Slogin                     }
812ab5c8ca4Slogin 
813ab5c8ca4Slogin                     // 验证addrlen的地址是否合法
814ab5c8ca4Slogin                     if unsafe { verify_area(addrlen as u64, core::mem::size_of::<u32>() as u64) }
815ab5c8ca4Slogin                         == false
816ab5c8ca4Slogin                     {
817ab5c8ca4Slogin                         // 地址空间超出了用户空间的范围,不合法
818ab5c8ca4Slogin                         return Err(SystemError::EFAULT);
819ab5c8ca4Slogin                     }
820ab5c8ca4Slogin 
821ab5c8ca4Slogin                     if unsafe { verify_area(addr as u64, core::mem::size_of::<SockAddr>() as u64) }
822ab5c8ca4Slogin                         == false
823ab5c8ca4Slogin                     {
824ab5c8ca4Slogin                         // 地址空间超出了用户空间的范围,不合法
825ab5c8ca4Slogin                         return Err(SystemError::EFAULT);
826ab5c8ca4Slogin                     }
827ab5c8ca4Slogin                     return Ok(());
828ab5c8ca4Slogin                 };
829ab5c8ca4Slogin                 let r = security_check();
830ab5c8ca4Slogin                 if r.is_err() {
831ab5c8ca4Slogin                     Err(r.unwrap_err())
832ab5c8ca4Slogin                 } else {
833ab5c8ca4Slogin                     let buf = unsafe { core::slice::from_raw_parts_mut(buf, len) };
834ab5c8ca4Slogin                     Self::recvfrom(args[0], buf, flags, addr, addrlen as *mut u32)
835ab5c8ca4Slogin                 }
836ab5c8ca4Slogin             }
837ab5c8ca4Slogin 
838ab5c8ca4Slogin             SYS_RECVMSG => {
839ab5c8ca4Slogin                 let msg = args[1] as *mut crate::net::syscall::MsgHdr;
840ab5c8ca4Slogin                 let flags = args[2] as u32;
841ab5c8ca4Slogin                 let security_check = || {
842ab5c8ca4Slogin                     // 验证msg的地址是否合法
843ab5c8ca4Slogin                     if unsafe {
844ab5c8ca4Slogin                         verify_area(
845ab5c8ca4Slogin                             msg as u64,
846ab5c8ca4Slogin                             core::mem::size_of::<crate::net::syscall::MsgHdr>() as u64,
847ab5c8ca4Slogin                         )
848ab5c8ca4Slogin                     } == false
849ab5c8ca4Slogin                     {
850ab5c8ca4Slogin                         // 地址空间超出了用户空间的范围,不合法
851ab5c8ca4Slogin                         return Err(SystemError::EFAULT);
852ab5c8ca4Slogin                     }
853ab5c8ca4Slogin                     let msg = unsafe { msg.as_mut() }.ok_or(SystemError::EFAULT)?;
854ab5c8ca4Slogin                     return Ok(msg);
855ab5c8ca4Slogin                 };
856ab5c8ca4Slogin                 let r = security_check();
857ab5c8ca4Slogin                 if r.is_err() {
858ab5c8ca4Slogin                     Err(r.unwrap_err())
859ab5c8ca4Slogin                 } else {
860ab5c8ca4Slogin                     let msg = r.unwrap();
861ab5c8ca4Slogin                     Self::recvmsg(args[0], msg, flags)
862ab5c8ca4Slogin                 }
863ab5c8ca4Slogin             }
864ab5c8ca4Slogin 
865ab5c8ca4Slogin             SYS_LISTEN => Self::listen(args[0], args[1]),
866ab5c8ca4Slogin             SYS_SHUTDOWN => Self::shutdown(args[0], args[1]),
867ab5c8ca4Slogin             SYS_ACCEPT => Self::accept(args[0], args[1] as *mut SockAddr, args[2] as *mut u32),
868ab5c8ca4Slogin             SYS_GETSOCKNAME => {
869ab5c8ca4Slogin                 Self::getsockname(args[0], args[1] as *mut SockAddr, args[2] as *mut u32)
870ab5c8ca4Slogin             }
871ab5c8ca4Slogin             SYS_GETPEERNAME => {
872ab5c8ca4Slogin                 Self::getpeername(args[0], args[1] as *mut SockAddr, args[2] as *mut u32)
873ab5c8ca4Slogin             }
874*36fd0130Shoumkh             SYS_GETTIMEOFDAY => {
875*36fd0130Shoumkh                 let timeval = args[0] as *mut PosixTimeval;
876*36fd0130Shoumkh                 let timezone_ptr = args[1] as *const PosixTimeZone;
877*36fd0130Shoumkh                 let security_check = || {
878*36fd0130Shoumkh                     if unsafe {
879*36fd0130Shoumkh                         verify_area(timeval as u64, core::mem::size_of::<PosixTimeval>() as u64)
880*36fd0130Shoumkh                     } == false
881*36fd0130Shoumkh                     {
882*36fd0130Shoumkh                         return Err(SystemError::EFAULT);
883*36fd0130Shoumkh                     }
884*36fd0130Shoumkh                     if unsafe {
885*36fd0130Shoumkh                         verify_area(
886*36fd0130Shoumkh                             timezone_ptr as u64,
887*36fd0130Shoumkh                             core::mem::size_of::<PosixTimeZone>() as u64,
888*36fd0130Shoumkh                         )
889*36fd0130Shoumkh                     } == false
890*36fd0130Shoumkh                     {
891*36fd0130Shoumkh                         return Err(SystemError::EFAULT);
892*36fd0130Shoumkh                     }
893*36fd0130Shoumkh                     return Ok(());
894*36fd0130Shoumkh                 };
895*36fd0130Shoumkh                 let r = security_check();
896*36fd0130Shoumkh                 if r.is_err() {
897*36fd0130Shoumkh                     Err(r.unwrap_err())
898*36fd0130Shoumkh                 } else {
899*36fd0130Shoumkh                     let timezone = if !timezone_ptr.is_null() {
900*36fd0130Shoumkh                         &SYS_TIMEZONE
901*36fd0130Shoumkh                     } else {
902*36fd0130Shoumkh                         unsafe { timezone_ptr.as_ref().unwrap() }
903*36fd0130Shoumkh                     };
904*36fd0130Shoumkh                     if !timeval.is_null() {
905*36fd0130Shoumkh                         Self::gettimeofday(timeval, timezone)
906*36fd0130Shoumkh                     } else {
907*36fd0130Shoumkh                         Err(SystemError::EFAULT)
908*36fd0130Shoumkh                     }
909*36fd0130Shoumkh                 }
910*36fd0130Shoumkh             }
911ab5c8ca4Slogin             _ => panic!("Unsupported syscall ID: {}", syscall_num),
912ab5c8ca4Slogin         };
913ab5c8ca4Slogin 
914ab5c8ca4Slogin         let r = r.unwrap_or_else(|e| e.to_posix_errno() as usize);
915ab5c8ca4Slogin         return r;
916ab5c8ca4Slogin     }
917ab5c8ca4Slogin 
918ab5c8ca4Slogin     pub fn put_string(
919ab5c8ca4Slogin         s: *const u8,
920ab5c8ca4Slogin         front_color: u32,
921ab5c8ca4Slogin         back_color: u32,
922ab5c8ca4Slogin     ) -> Result<usize, SystemError> {
923ab5c8ca4Slogin         return Ok(unsafe { do_put_string(s, front_color, back_color) });
924ab5c8ca4Slogin     }
925ab5c8ca4Slogin 
926ab5c8ca4Slogin     pub fn reboot() -> Result<usize, SystemError> {
927ab5c8ca4Slogin         cpu_reset();
928ab5c8ca4Slogin     }
929ab5c8ca4Slogin }
930