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::{ 91496ba7bSLoGin arch::{cpu::cpu_reset, interrupt::TrapFrame, MMArch}, 10*2dbef785SGnoCiYeH driver::base::{block::SeekFrom, device::DeviceNumber}, 11ab5c8ca4Slogin filesystem::vfs::{ 126d81180bSLoGin fcntl::FcntlCommand, 13ab5c8ca4Slogin file::FileMode, 14*2dbef785SGnoCiYeH syscall::{ModeType, PosixKstat, SEEK_CUR, SEEK_END, SEEK_MAX, SEEK_SET}, 15ab5c8ca4Slogin MAX_PATHLEN, 16ab5c8ca4Slogin }, 171496ba7bSLoGin include::bindings::bindings::{PAGE_2M_SIZE, PAGE_4K_SIZE}, 18aa0367d6SLoGin kinfo, 1940fe15e0SLoGin libs::align::page_align_up, 20be63f3b2Shoumkh mm::{verify_area, MemoryManagementArch, VirtAddr}, 21ab5c8ca4Slogin net::syscall::SockAddr, 221496ba7bSLoGin process::Pid, 2336fd0130Shoumkh time::{ 2440fe15e0SLoGin syscall::{PosixTimeZone, PosixTimeval}, 2536fd0130Shoumkh TimeSpec, 2636fd0130Shoumkh }, 27ab5c8ca4Slogin }; 28ab5c8ca4Slogin 294537ffb7SChiichen use self::user_access::UserBufferWriter; 304537ffb7SChiichen 3140fe15e0SLoGin pub mod user_access; 3240fe15e0SLoGin 334454d1a2Slogin #[repr(i32)] 34676b8ef6SMork #[derive(Debug, FromPrimitive, ToPrimitive, PartialEq, Eq, Clone)] 3579a452ceShoumkh #[allow(dead_code, non_camel_case_types)] 364454d1a2Slogin pub enum SystemError { 3779a452ceShoumkh EPERM = 1, 3879a452ceShoumkh /// 没有指定的文件或目录 No such file or directory. 3979a452ceShoumkh ENOENT = 2, 404454d1a2Slogin /// 没有这样的进程 No such process. 4179a452ceShoumkh ESRCH = 3, 4279a452ceShoumkh /// 被中断的函数 Interrupted function. 4379a452ceShoumkh EINTR = 4, 4479a452ceShoumkh /// I/O错误 I/O error. 4579a452ceShoumkh EIO = 5, 4679a452ceShoumkh /// 没有这样的设备或地址 No such device or address. 4779a452ceShoumkh ENXIO = 6, 4879a452ceShoumkh /// 参数列表过长,或者在输出buffer中缺少空间 或者参数比系统内建的最大值要大 Argument list too long. 4979a452ceShoumkh E2BIG = 7, 5079a452ceShoumkh /// 可执行文件格式错误 Executable file format error 5179a452ceShoumkh ENOEXEC = 8, 5279a452ceShoumkh /// 错误的文件描述符 Bad file descriptor. 5379a452ceShoumkh EBADF = 9, 5479a452ceShoumkh /// 没有子进程 No child processes. 5579a452ceShoumkh ECHILD = 10, 5679a452ceShoumkh /// 资源不可用,请重试。 Resource unavailable, try again.(may be the same value as [EWOULDBLOCK]) 5779a452ceShoumkh /// 5879a452ceShoumkh /// 操作将被禁止 Operation would block.(may be the same value as [EAGAIN]). 5979a452ceShoumkh EAGAIN_OR_EWOULDBLOCK = 11, 6079a452ceShoumkh /// 没有足够的空间 Not enough space. 6179a452ceShoumkh ENOMEM = 12, 6279a452ceShoumkh /// 访问被拒绝 Permission denied 6379a452ceShoumkh EACCES = 13, 6479a452ceShoumkh /// 错误的地址 Bad address 6579a452ceShoumkh EFAULT = 14, 6679a452ceShoumkh /// 需要块设备 Block device required 6779a452ceShoumkh ENOTBLK = 15, 6879a452ceShoumkh /// 设备或资源忙 Device or resource busy. 6979a452ceShoumkh EBUSY = 16, 7079a452ceShoumkh /// 文件已存在 File exists. 7179a452ceShoumkh EEXIST = 17, 724454d1a2Slogin /// 跨设备连接 Cross-device link. 7379a452ceShoumkh EXDEV = 18, 7479a452ceShoumkh /// 没有指定的设备 No such device. 7579a452ceShoumkh ENODEV = 19, 7679a452ceShoumkh /// 不是目录 Not a directory. 7779a452ceShoumkh ENOTDIR = 20, 7879a452ceShoumkh /// 是一个目录 Is a directory 7979a452ceShoumkh EISDIR = 21, 8079a452ceShoumkh /// 不可用的参数 Invalid argument. 8179a452ceShoumkh EINVAL = 22, 8279a452ceShoumkh /// 系统中打开的文件过多 Too many files open in system. 8379a452ceShoumkh ENFILE = 23, 8479a452ceShoumkh /// 文件描述符的值过大 File descriptor value too large. 8579a452ceShoumkh EMFILE = 24, 8679a452ceShoumkh /// 不正确的I/O控制操作 Inappropriate I/O control operation. 8779a452ceShoumkh ENOTTY = 25, 8879a452ceShoumkh /// 文本文件忙 Text file busy. 8979a452ceShoumkh ETXTBSY = 26, 9079a452ceShoumkh /// 文件太大 File too large. 9179a452ceShoumkh EFBIG = 27, 9279a452ceShoumkh /// 设备上没有空间 No space left on device. 9379a452ceShoumkh ENOSPC = 28, 9479a452ceShoumkh /// 错误的寻道.当前文件是pipe,不允许seek请求 Invalid seek. 9579a452ceShoumkh ESPIPE = 29, 9679a452ceShoumkh /// 只读的文件系统 Read-only file system. 9779a452ceShoumkh EROFS = 30, 9879a452ceShoumkh /// 链接数过多 Too many links. 9979a452ceShoumkh EMLINK = 31, 10079a452ceShoumkh /// 断开的管道 Broken pipe. 10179a452ceShoumkh EPIPE = 32, 10279a452ceShoumkh /// 数学参数超出作用域 Mathematics argument out of domain of function. 10379a452ceShoumkh EDOM = 33, 10479a452ceShoumkh /// 结果过大 Result too large. 10579a452ceShoumkh ERANGE = 34, 10679a452ceShoumkh /// 资源死锁将要发生 Resource deadlock would occur. 10779a452ceShoumkh EDEADLK = 35, 10879a452ceShoumkh /// 文件名过长 Filename too long. 10979a452ceShoumkh ENAMETOOLONG = 36, 11079a452ceShoumkh /// 没有可用的锁 No locks available. 11179a452ceShoumkh ENOLCK = 37, 11279a452ceShoumkh /// 功能不支持 Function not supported. 11379a452ceShoumkh ENOSYS = 38, 11479a452ceShoumkh /// 目录非空 Directory not empty. 11579a452ceShoumkh ENOTEMPTY = 39, 11679a452ceShoumkh /// 符号链接级别过多 Too many levels of symbolic links. 11779a452ceShoumkh ELOOP = 40, 11879a452ceShoumkh /// 没有期待类型的消息 No message of the desired type. 11979a452ceShoumkh ENOMSG = 41, 12079a452ceShoumkh /// 标志符被移除 Identifier removed. 12179a452ceShoumkh EIDRM = 42, 12279a452ceShoumkh /// 通道号超出范围 Channel number out of range 12379a452ceShoumkh ECHRNG = 43, 12479a452ceShoumkh /// 二级不同步 Level 2 not synchronized 12579a452ceShoumkh EL2NSYNC = 44, 12679a452ceShoumkh /// 三级暂停 Level 3 halted 12779a452ceShoumkh EL3HLT = 45, 12879a452ceShoumkh /// 三级重置 Level 3 reset 12979a452ceShoumkh EL3RST = 46, 13079a452ceShoumkh /// 链接号超出范围 Link number out of range 13179a452ceShoumkh ELNRNG = 47, 13279a452ceShoumkh /// 未连接协议驱动程序 Protocol driver not attached 13379a452ceShoumkh EUNATCH = 48, 13479a452ceShoumkh /// 没有可用的CSI结构 No CSI structure available 13579a452ceShoumkh ENOCSI = 49, 13679a452ceShoumkh /// 二级暂停 Level 2 halted 13779a452ceShoumkh EL2HLT = 50, 13879a452ceShoumkh /// 无效交换 Invalid exchange 13979a452ceShoumkh EBADE = 51, 14079a452ceShoumkh /// 无效的请求描述符 Invalid request descriptor 14179a452ceShoumkh EBADR = 52, 14279a452ceShoumkh /// 交换满 Exchange full 14379a452ceShoumkh EXFULL = 53, 14479a452ceShoumkh /// 无阳极 No anode 14579a452ceShoumkh ENOANO = 54, 14679a452ceShoumkh /// 请求码无效 Invalid request code 14779a452ceShoumkh EBADRQC = 55, 14879a452ceShoumkh /// 无效插槽 Invalid slot 14979a452ceShoumkh EBADSLT = 56, 15079a452ceShoumkh /// 资源死锁 Resource deadlock would occur 15179a452ceShoumkh EDEADLOCK = 57, 15279a452ceShoumkh /// 错误的字体文件格式 Bad font file format 15379a452ceShoumkh EBFONT = 58, 15479a452ceShoumkh /// 不是STREAM Not a STREAM 15579a452ceShoumkh ENOSTR = 59, 15679a452ceShoumkh /// 队列头没有可读取的消息 No message is available on the STREAM head read queue. 15779a452ceShoumkh ENODATA = 60, 15879a452ceShoumkh /// 流式ioctl()超时 Stream ioctl() timeout 15979a452ceShoumkh ETIME = 61, 16079a452ceShoumkh /// 没有STREAM资源 No STREAM resources. 16179a452ceShoumkh ENOSR = 62, 16279a452ceShoumkh /// 机器不在网络上 Machine is not on the network 16379a452ceShoumkh ENONET = 63, 16479a452ceShoumkh /// 未安装软件包 Package not installed 16579a452ceShoumkh ENOPKG = 64, 16679a452ceShoumkh /// 远程对象 Object is remote 16779a452ceShoumkh EREMOTE = 65, 16879a452ceShoumkh /// 保留 Reserved. 16979a452ceShoumkh ENOLINK = 66, 17079a452ceShoumkh /// 外设错误 Advertise error. 17179a452ceShoumkh EADV = 67, 17279a452ceShoumkh /// 安装错误 Srmount error 17379a452ceShoumkh ESRMNT = 68, 17479a452ceShoumkh /// 发送时发生通信错误 Communication error on send 17579a452ceShoumkh ECOMM = 69, 17679a452ceShoumkh /// 协议错误 Protocol error. 17779a452ceShoumkh EPROTO = 70, 17879a452ceShoumkh /// 保留使用 Reserved. 17979a452ceShoumkh EMULTIHOP = 71, 18079a452ceShoumkh /// RFS特定错误 RFS specific error 18179a452ceShoumkh EDOTDOT = 72, 18279a452ceShoumkh /// 错误的消息 Bad message. 18379a452ceShoumkh EBADMSG = 73, 18479a452ceShoumkh /// 数值过大,产生溢出 Value too large to be stored in data type. 18579a452ceShoumkh EOVERFLOW = 74, 18679a452ceShoumkh /// 名称在网络上不是唯一的 Name not unique on network 18779a452ceShoumkh ENOTUNIQ = 75, 18879a452ceShoumkh /// 处于不良状态的文件描述符 File descriptor in bad state 18979a452ceShoumkh EBADFD = 76, 19079a452ceShoumkh /// 远程地址已更改 Remote address changed 19179a452ceShoumkh EREMCHG = 77, 19279a452ceShoumkh /// 无法访问所需的共享库 Can not access a needed shared library 19379a452ceShoumkh ELIBACC = 78, 19479a452ceShoumkh /// 访问损坏的共享库 Accessing a corrupted shared library 19579a452ceShoumkh ELIBBAD = 79, 19679a452ceShoumkh /// a. out中的.lib部分已损坏 .lib section in a.out corrupted 19779a452ceShoumkh ELIBSCN = 80, 19879a452ceShoumkh /// 尝试链接太多共享库 Attempting to link in too many shared libraries 19979a452ceShoumkh ELIBMAX = 81, 20079a452ceShoumkh /// 无法直接执行共享库 Cannot exec a shared library directly 20179a452ceShoumkh ELIBEXEC = 82, 20279a452ceShoumkh /// 不合法的字符序列 Illegal byte sequence. 20379a452ceShoumkh EILSEQ = 83, 20479a452ceShoumkh /// 中断的系统调用应该重新启动 Interrupted system call should be restarted 20579a452ceShoumkh ERESTART = 84, 20679a452ceShoumkh /// 流管道错误 Streams pipe error 20779a452ceShoumkh ESTRPIPE = 85, 20879a452ceShoumkh /// 用户太多 Too many users 20979a452ceShoumkh EUSERS = 86, 21079a452ceShoumkh /// 不是一个套接字 Not a socket. 21179a452ceShoumkh ENOTSOCK = 87, 21279a452ceShoumkh /// 需要目标地址 Destination address required. 21379a452ceShoumkh EDESTADDRREQ = 88, 21479a452ceShoumkh /// 消息过大 Message too large. 21579a452ceShoumkh EMSGSIZE = 89, 21679a452ceShoumkh /// 对于套接字而言,错误的协议 Protocol wrong type for socket. 21779a452ceShoumkh EPROTOTYPE = 90, 21879a452ceShoumkh /// 协议不可用 Protocol not available. 21979a452ceShoumkh ENOPROTOOPT = 91, 22079a452ceShoumkh /// 协议不被支持 Protocol not supported. 22179a452ceShoumkh EPROTONOSUPPORT = 92, 22279a452ceShoumkh /// 不支持套接字类型 Socket type not supported 22379a452ceShoumkh ESOCKTNOSUPPORT = 93, 22479a452ceShoumkh /// 套接字不支持该操作 Operation not supported on socket (may be the same value as [ENOTSUP]). 22579a452ceShoumkh /// 22679a452ceShoumkh /// 不被支持 Not supported (may be the same value as [EOPNOTSUPP]). 22779a452ceShoumkh EOPNOTSUPP_OR_ENOTSUP = 94, 22879a452ceShoumkh /// 不支持协议系列 Protocol family not supported 22979a452ceShoumkh EPFNOSUPPORT = 95, 23079a452ceShoumkh /// 地址family不支持 Address family not supported. 23179a452ceShoumkh EAFNOSUPPORT = 96, 23279a452ceShoumkh /// 地址正在被使用 Address in use. 23379a452ceShoumkh EADDRINUSE = 97, 23479a452ceShoumkh /// 地址不可用 Address not available. 23579a452ceShoumkh EADDRNOTAVAIL = 98, 23679a452ceShoumkh /// 网络已关闭 Network is down. 23779a452ceShoumkh ENETDOWN = 99, 23879a452ceShoumkh /// 网络不可达 Network unreachable. 23979a452ceShoumkh ENETUNREACH = 100, 24079a452ceShoumkh /// 网络连接已断开 Connection aborted by network. 24179a452ceShoumkh ENETRESET = 101, 24279a452ceShoumkh /// 连接已断开 Connection aborted. 24379a452ceShoumkh ECONNABORTED = 102, 24479a452ceShoumkh /// 连接被重置 Connection reset. 24579a452ceShoumkh ECONNRESET = 103, 24679a452ceShoumkh /// 缓冲区空间不足 No buffer space available. 24779a452ceShoumkh ENOBUFS = 104, 24879a452ceShoumkh /// 套接字已连接 Socket is connected. 24979a452ceShoumkh EISCONN = 105, 25079a452ceShoumkh /// 套接字未连接 The socket is not connected. 25179a452ceShoumkh ENOTCONN = 106, 25279a452ceShoumkh /// 传输端点关闭后无法发送 Cannot send after transport endpoint shutdown 25379a452ceShoumkh ESHUTDOWN = 107, 25479a452ceShoumkh /// 引用太多:无法拼接 Too many references: cannot splice 25579a452ceShoumkh ETOOMANYREFS = 108, 25679a452ceShoumkh /// 连接超时 Connection timed out. 25779a452ceShoumkh ETIMEDOUT = 109, 25879a452ceShoumkh /// 连接被拒绝 Connection refused. 25979a452ceShoumkh ECONNREFUSED = 110, 26079a452ceShoumkh /// 主机已关闭 Host is down 26179a452ceShoumkh EHOSTDOWN = 111, 26279a452ceShoumkh /// 主机不可达 Host is unreachable. 26379a452ceShoumkh EHOSTUNREACH = 112, 26479a452ceShoumkh /// 连接已经在处理 Connection already in progress. 26579a452ceShoumkh EALREADY = 113, 26679a452ceShoumkh /// 操作正在处理 Operation in progress. 26779a452ceShoumkh EINPROGRESS = 114, 26879a452ceShoumkh /// 保留 Reserved. 26979a452ceShoumkh ESTALE = 115, 27079a452ceShoumkh /// 结构需要清理 Structure needs cleaning 27179a452ceShoumkh EUCLEAN = 116, 27279a452ceShoumkh /// 不是XENIX命名类型文件 Not a XENIX named type file 27379a452ceShoumkh ENOTNAM = 117, 27479a452ceShoumkh /// 没有可用的XENIX信号量 No XENIX semaphores available 27579a452ceShoumkh ENAVAIL = 118, 27679a452ceShoumkh /// 是命名类型文件 Is a named type file 27779a452ceShoumkh EISNAM = 119, 27879a452ceShoumkh /// 远程I/O错误 Remote I/O error 27979a452ceShoumkh EREMOTEIO = 120, 28079a452ceShoumkh /// 保留使用 Reserved 28179a452ceShoumkh EDQUOT = 121, 28279a452ceShoumkh /// 没有找到媒介 No medium found 28379a452ceShoumkh ENOMEDIUM = 122, 28479a452ceShoumkh /// 介质类型错误 Wrong medium type 28579a452ceShoumkh EMEDIUMTYPE = 123, 28679a452ceShoumkh /// 操作被取消 Operation canceled. 28779a452ceShoumkh ECANCELED = 124, 28879a452ceShoumkh /// 所需的密钥不可用 Required key not available 28979a452ceShoumkh ENOKEY = 125, 29079a452ceShoumkh /// 密钥已过期 Key has expired 29179a452ceShoumkh EKEYEXPIRED = 126, 29279a452ceShoumkh /// 密钥已被撤销 Key has been revoked 29379a452ceShoumkh EKEYREVOKED = 127, 29479a452ceShoumkh /// 密钥被服务拒绝 Key has been revoked 29579a452ceShoumkh EKEYREJECTED = 128, 29679a452ceShoumkh /// 之前的拥有者挂了 Previous owner died. 29779a452ceShoumkh EOWNERDEAD = 129, 29879a452ceShoumkh /// 状态不可恢复 State not recoverable. 29979a452ceShoumkh ENOTRECOVERABLE = 130, 3004454d1a2Slogin } 301676b8ef6SMork 302676b8ef6SMork impl SystemError { 303676b8ef6SMork /// @brief 把posix错误码转换为系统错误枚举类型。 304676b8ef6SMork pub fn from_posix_errno(errno: i32) -> Option<SystemError> { 305676b8ef6SMork // posix 错误码是小于0的 306676b8ef6SMork if errno >= 0 { 307676b8ef6SMork return None; 308676b8ef6SMork } 309676b8ef6SMork return <Self as FromPrimitive>::from_i32(-errno); 310676b8ef6SMork } 311676b8ef6SMork 312676b8ef6SMork /// @brief 把系统错误枚举类型转换为负数posix错误码。 313676b8ef6SMork pub fn to_posix_errno(&self) -> i32 { 314676b8ef6SMork return -<Self as ToPrimitive>::to_i32(self).unwrap(); 315676b8ef6SMork } 316676b8ef6SMork } 317ab5c8ca4Slogin 318ab5c8ca4Slogin // 定义系统调用号 319ab5c8ca4Slogin pub const SYS_PUT_STRING: usize = 1; 320ab5c8ca4Slogin pub const SYS_OPEN: usize = 2; 321ab5c8ca4Slogin pub const SYS_CLOSE: usize = 3; 322ab5c8ca4Slogin pub const SYS_READ: usize = 4; 323ab5c8ca4Slogin pub const SYS_WRITE: usize = 5; 324ab5c8ca4Slogin pub const SYS_LSEEK: usize = 6; 325ab5c8ca4Slogin pub const SYS_FORK: usize = 7; 326ab5c8ca4Slogin pub const SYS_VFORK: usize = 8; 327ab5c8ca4Slogin pub const SYS_BRK: usize = 9; 328ab5c8ca4Slogin pub const SYS_SBRK: usize = 10; 329ab5c8ca4Slogin 330ab5c8ca4Slogin pub const SYS_REBOOT: usize = 11; 331ab5c8ca4Slogin pub const SYS_CHDIR: usize = 12; 332ab5c8ca4Slogin pub const SYS_GET_DENTS: usize = 13; 333ab5c8ca4Slogin pub const SYS_EXECVE: usize = 14; 334ab5c8ca4Slogin pub const SYS_WAIT4: usize = 15; 335ab5c8ca4Slogin pub const SYS_EXIT: usize = 16; 336ab5c8ca4Slogin pub const SYS_MKDIR: usize = 17; 337ab5c8ca4Slogin pub const SYS_NANOSLEEP: usize = 18; 338ab5c8ca4Slogin /// todo: 该系统调用与Linux不一致,将来需要删除该系统调用!!! 删的时候记得改C版本的libc 339ab5c8ca4Slogin pub const SYS_CLOCK: usize = 19; 340ab5c8ca4Slogin pub const SYS_PIPE: usize = 20; 34140fe15e0SLoGin /// 系统调用21曾经是SYS_MSTAT,但是现在已经废弃 34240fe15e0SLoGin pub const __NOT_USED: usize = 21; 343ab5c8ca4Slogin pub const SYS_UNLINK_AT: usize = 22; 344ab5c8ca4Slogin pub const SYS_KILL: usize = 23; 345ab5c8ca4Slogin pub const SYS_SIGACTION: usize = 24; 346ab5c8ca4Slogin pub const SYS_RT_SIGRETURN: usize = 25; 347ab5c8ca4Slogin pub const SYS_GETPID: usize = 26; 348ab5c8ca4Slogin pub const SYS_SCHED: usize = 27; 349ab5c8ca4Slogin pub const SYS_DUP: usize = 28; 350ab5c8ca4Slogin pub const SYS_DUP2: usize = 29; 351ab5c8ca4Slogin pub const SYS_SOCKET: usize = 30; 352ab5c8ca4Slogin 353ab5c8ca4Slogin pub const SYS_SETSOCKOPT: usize = 31; 354ab5c8ca4Slogin pub const SYS_GETSOCKOPT: usize = 32; 355ab5c8ca4Slogin pub const SYS_CONNECT: usize = 33; 356ab5c8ca4Slogin pub const SYS_BIND: usize = 34; 357ab5c8ca4Slogin pub const SYS_SENDTO: usize = 35; 358ab5c8ca4Slogin pub const SYS_RECVFROM: usize = 36; 359ab5c8ca4Slogin pub const SYS_RECVMSG: usize = 37; 360ab5c8ca4Slogin pub const SYS_LISTEN: usize = 38; 361ab5c8ca4Slogin pub const SYS_SHUTDOWN: usize = 39; 362ab5c8ca4Slogin pub const SYS_ACCEPT: usize = 40; 363ab5c8ca4Slogin 364ab5c8ca4Slogin pub const SYS_GETSOCKNAME: usize = 41; 365ab5c8ca4Slogin pub const SYS_GETPEERNAME: usize = 42; 36636fd0130Shoumkh pub const SYS_GETTIMEOFDAY: usize = 43; 36740fe15e0SLoGin pub const SYS_MMAP: usize = 44; 36840fe15e0SLoGin pub const SYS_MUNMAP: usize = 45; 369ab5c8ca4Slogin 3706d81180bSLoGin pub const SYS_MPROTECT: usize = 46; 371be63f3b2Shoumkh pub const SYS_FSTAT: usize = 47; 3726d81180bSLoGin pub const SYS_GETCWD: usize = 48; 3736d81180bSLoGin pub const SYS_GETPPID: usize = 49; 3746d81180bSLoGin pub const SYS_GETPGID: usize = 50; 3756d81180bSLoGin 3766d81180bSLoGin pub const SYS_FCNTL: usize = 51; 3776d81180bSLoGin pub const SYS_FTRUNCATE: usize = 52; 378*2dbef785SGnoCiYeH pub const SYS_MKNOD: usize = 53; 379be63f3b2Shoumkh 380ab5c8ca4Slogin #[derive(Debug)] 381ab5c8ca4Slogin pub struct Syscall; 382ab5c8ca4Slogin 383ab5c8ca4Slogin extern "C" { 384ab5c8ca4Slogin fn do_put_string(s: *const u8, front_color: u32, back_color: u32) -> usize; 385ab5c8ca4Slogin } 386ab5c8ca4Slogin 387ab5c8ca4Slogin #[no_mangle] 388ab5c8ca4Slogin pub extern "C" fn syscall_init() -> i32 { 389ab5c8ca4Slogin kinfo!("Initializing syscall..."); 390ab5c8ca4Slogin Syscall::init().expect("syscall init failed"); 391ab5c8ca4Slogin kinfo!("Syscall init successfully!"); 392ab5c8ca4Slogin return 0; 393ab5c8ca4Slogin } 394ab5c8ca4Slogin 395ab5c8ca4Slogin impl Syscall { 396ab5c8ca4Slogin /// 初始化系统调用 397ab5c8ca4Slogin pub fn init() -> Result<(), SystemError> { 398ab5c8ca4Slogin static INIT_FLAG: AtomicBool = AtomicBool::new(false); 399ab5c8ca4Slogin let prev = INIT_FLAG.swap(true, Ordering::SeqCst); 400ab5c8ca4Slogin if prev { 401ab5c8ca4Slogin panic!("Cannot initialize syscall more than once!"); 402ab5c8ca4Slogin } 403ab5c8ca4Slogin return crate::arch::syscall::arch_syscall_init(); 404ab5c8ca4Slogin } 405ab5c8ca4Slogin /// @brief 系统调用分发器,用于分发系统调用。 406ab5c8ca4Slogin /// 407ab5c8ca4Slogin /// 这个函数内,需要根据系统调用号,调用对应的系统调用处理函数。 408ab5c8ca4Slogin /// 并且,对于用户态传入的指针参数,需要在本函数内进行越界检查,防止访问到内核空间。 4091496ba7bSLoGin pub fn handle(syscall_num: usize, args: &[usize], frame: &mut TrapFrame) -> usize { 410ab5c8ca4Slogin let r = match syscall_num { 411ab5c8ca4Slogin SYS_PUT_STRING => { 412ab5c8ca4Slogin Self::put_string(args[0] as *const u8, args[1] as u32, args[2] as u32) 413ab5c8ca4Slogin } 414ab5c8ca4Slogin SYS_OPEN => { 415ab5c8ca4Slogin let path: &CStr = unsafe { CStr::from_ptr(args[0] as *const c_char) }; 416ab5c8ca4Slogin let path: Result<&str, core::str::Utf8Error> = path.to_str(); 417ab5c8ca4Slogin let res = if path.is_err() { 418ab5c8ca4Slogin Err(SystemError::EINVAL) 419ab5c8ca4Slogin } else { 420ab5c8ca4Slogin let path: &str = path.unwrap(); 42134e6d6c8Syuyi2439 422ab5c8ca4Slogin let flags = args[1]; 423ab5c8ca4Slogin let open_flags: FileMode = FileMode::from_bits_truncate(flags as u32); 424ab5c8ca4Slogin Self::open(path, open_flags) 425ab5c8ca4Slogin }; 4266d81180bSLoGin 427ab5c8ca4Slogin res 428ab5c8ca4Slogin } 429ab5c8ca4Slogin SYS_CLOSE => { 430ab5c8ca4Slogin let fd = args[0]; 43134e6d6c8Syuyi2439 43234e6d6c8Syuyi2439 let res = Self::close(fd); 43334e6d6c8Syuyi2439 43434e6d6c8Syuyi2439 res 435ab5c8ca4Slogin } 436ab5c8ca4Slogin SYS_READ => { 437ab5c8ca4Slogin let fd = args[0] as i32; 438ab5c8ca4Slogin let buf_vaddr = args[1]; 439ab5c8ca4Slogin let len = args[2]; 440f5df0e79SLoGin let virt_addr: VirtAddr = VirtAddr::new(buf_vaddr); 441ab5c8ca4Slogin // 判断缓冲区是否来自用户态,进行权限校验 4421496ba7bSLoGin let res = if frame.from_user() && verify_area(virt_addr, len as usize).is_err() { 443ab5c8ca4Slogin // 来自用户态,而buffer在内核态,这样的操作不被允许 444ab5c8ca4Slogin Err(SystemError::EPERM) 445ab5c8ca4Slogin } else { 446ab5c8ca4Slogin let buf: &mut [u8] = unsafe { 447ab5c8ca4Slogin core::slice::from_raw_parts_mut::<'static, u8>(buf_vaddr as *mut u8, len) 448ab5c8ca4Slogin }; 4496d81180bSLoGin 450ab5c8ca4Slogin Self::read(fd, buf) 451ab5c8ca4Slogin }; 4526d81180bSLoGin // kdebug!("sys read, fd: {}, len: {}, res: {:?}", fd, len, res); 453ab5c8ca4Slogin res 454ab5c8ca4Slogin } 455ab5c8ca4Slogin SYS_WRITE => { 456ab5c8ca4Slogin let fd = args[0] as i32; 457ab5c8ca4Slogin let buf_vaddr = args[1]; 458ab5c8ca4Slogin let len = args[2]; 459be63f3b2Shoumkh let virt_addr = VirtAddr::new(buf_vaddr); 460ab5c8ca4Slogin // 判断缓冲区是否来自用户态,进行权限校验 4611496ba7bSLoGin let res = if frame.from_user() && verify_area(virt_addr, len as usize).is_err() { 462ab5c8ca4Slogin // 来自用户态,而buffer在内核态,这样的操作不被允许 463ab5c8ca4Slogin Err(SystemError::EPERM) 464ab5c8ca4Slogin } else { 465ab5c8ca4Slogin let buf: &[u8] = unsafe { 466ab5c8ca4Slogin core::slice::from_raw_parts::<'static, u8>(buf_vaddr as *const u8, len) 467ab5c8ca4Slogin }; 4686d81180bSLoGin 469ab5c8ca4Slogin Self::write(fd, buf) 470ab5c8ca4Slogin }; 471ab5c8ca4Slogin 4726d81180bSLoGin // kdebug!("sys write, fd: {}, len: {}, res: {:?}", fd, len, res); 4736d81180bSLoGin 474ab5c8ca4Slogin res 475ab5c8ca4Slogin } 476ab5c8ca4Slogin 477ab5c8ca4Slogin SYS_LSEEK => { 478ab5c8ca4Slogin let fd = args[0] as i32; 479ab5c8ca4Slogin let offset = args[1] as i64; 480ab5c8ca4Slogin let whence = args[2] as u32; 481ab5c8ca4Slogin 482ab5c8ca4Slogin let w = match whence { 483ab5c8ca4Slogin SEEK_SET => Ok(SeekFrom::SeekSet(offset)), 484ab5c8ca4Slogin SEEK_CUR => Ok(SeekFrom::SeekCurrent(offset)), 485ab5c8ca4Slogin SEEK_END => Ok(SeekFrom::SeekEnd(offset)), 486ab5c8ca4Slogin SEEK_MAX => Ok(SeekFrom::SeekEnd(0)), 487ab5c8ca4Slogin _ => Err(SystemError::EINVAL), 488ab5c8ca4Slogin }; 489ab5c8ca4Slogin 490ab5c8ca4Slogin let res = if w.is_err() { 491ab5c8ca4Slogin Err(w.unwrap_err()) 492ab5c8ca4Slogin } else { 493ab5c8ca4Slogin let w = w.unwrap(); 494ab5c8ca4Slogin Self::lseek(fd, w) 495ab5c8ca4Slogin }; 4966d81180bSLoGin // kdebug!("sys lseek, fd: {}, offset: {}, whence: {}, res: {:?}", fd, offset, whence, res); 497ab5c8ca4Slogin 498ab5c8ca4Slogin res 499ab5c8ca4Slogin } 500ab5c8ca4Slogin 5011496ba7bSLoGin SYS_FORK => Self::fork(frame), 5021496ba7bSLoGin SYS_VFORK => Self::vfork(frame), 5031496ba7bSLoGin 504ab5c8ca4Slogin SYS_BRK => { 50540fe15e0SLoGin let new_brk = VirtAddr::new(args[0]); 50640fe15e0SLoGin Self::brk(new_brk).map(|vaddr| vaddr.data()) 507ab5c8ca4Slogin } 508ab5c8ca4Slogin 509ab5c8ca4Slogin SYS_SBRK => { 510ab5c8ca4Slogin let increment = args[0] as isize; 5111496ba7bSLoGin Self::sbrk(increment).map(|vaddr: VirtAddr| vaddr.data()) 512ab5c8ca4Slogin } 513ab5c8ca4Slogin 514ab5c8ca4Slogin SYS_REBOOT => Self::reboot(), 515ab5c8ca4Slogin 516ab5c8ca4Slogin SYS_CHDIR => { 517ab5c8ca4Slogin // Closure for checking arguments 518ab5c8ca4Slogin let chdir_check = |arg0: usize| { 519ab5c8ca4Slogin if arg0 == 0 { 520ab5c8ca4Slogin return Err(SystemError::EFAULT); 521ab5c8ca4Slogin } 522ab5c8ca4Slogin let path_ptr = arg0 as *const c_char; 523be63f3b2Shoumkh let virt_addr = VirtAddr::new(path_ptr as usize); 524ab5c8ca4Slogin // 权限校验 525ab5c8ca4Slogin if path_ptr.is_null() 5261496ba7bSLoGin || (frame.from_user() 5271496ba7bSLoGin && verify_area(virt_addr, PAGE_2M_SIZE as usize).is_err()) 528ab5c8ca4Slogin { 529ab5c8ca4Slogin return Err(SystemError::EINVAL); 530ab5c8ca4Slogin } 531ab5c8ca4Slogin let dest_path: &CStr = unsafe { CStr::from_ptr(path_ptr) }; 532ab5c8ca4Slogin let dest_path: &str = dest_path.to_str().map_err(|_| SystemError::EINVAL)?; 533ab5c8ca4Slogin if dest_path.len() == 0 { 534ab5c8ca4Slogin return Err(SystemError::EINVAL); 5351496ba7bSLoGin } else if dest_path.len() > MAX_PATHLEN as usize { 536ab5c8ca4Slogin return Err(SystemError::ENAMETOOLONG); 537ab5c8ca4Slogin } 538ab5c8ca4Slogin 539ab5c8ca4Slogin return Ok(dest_path); 540ab5c8ca4Slogin }; 541ab5c8ca4Slogin 542ab5c8ca4Slogin let r: Result<&str, SystemError> = chdir_check(args[0]); 543ab5c8ca4Slogin if r.is_err() { 544ab5c8ca4Slogin Err(r.unwrap_err()) 545ab5c8ca4Slogin } else { 546ab5c8ca4Slogin Self::chdir(r.unwrap()) 547ab5c8ca4Slogin } 548ab5c8ca4Slogin } 549ab5c8ca4Slogin 550ab5c8ca4Slogin SYS_GET_DENTS => { 551ab5c8ca4Slogin let fd = args[0] as i32; 55234e6d6c8Syuyi2439 553ab5c8ca4Slogin let buf_vaddr = args[1]; 554ab5c8ca4Slogin let len = args[2]; 555f5df0e79SLoGin let virt_addr: VirtAddr = VirtAddr::new(buf_vaddr); 556ab5c8ca4Slogin // 判断缓冲区是否来自用户态,进行权限校验 5571496ba7bSLoGin let res = if frame.from_user() && verify_area(virt_addr, len as usize).is_err() { 558ab5c8ca4Slogin // 来自用户态,而buffer在内核态,这样的操作不被允许 559ab5c8ca4Slogin Err(SystemError::EPERM) 560ab5c8ca4Slogin } else if buf_vaddr == 0 { 561ab5c8ca4Slogin Err(SystemError::EFAULT) 562ab5c8ca4Slogin } else { 563ab5c8ca4Slogin let buf: &mut [u8] = unsafe { 564ab5c8ca4Slogin core::slice::from_raw_parts_mut::<'static, u8>(buf_vaddr as *mut u8, len) 565ab5c8ca4Slogin }; 566ab5c8ca4Slogin Self::getdents(fd, buf) 567ab5c8ca4Slogin }; 568ab5c8ca4Slogin 569ab5c8ca4Slogin res 570ab5c8ca4Slogin } 571ab5c8ca4Slogin 572ab5c8ca4Slogin SYS_EXECVE => { 573ab5c8ca4Slogin let path_ptr = args[0]; 574ab5c8ca4Slogin let argv_ptr = args[1]; 575ab5c8ca4Slogin let env_ptr = args[2]; 576be63f3b2Shoumkh let virt_path_ptr = VirtAddr::new(path_ptr); 577be63f3b2Shoumkh let virt_argv_ptr = VirtAddr::new(argv_ptr); 578be63f3b2Shoumkh let virt_env_ptr = VirtAddr::new(env_ptr); 579ab5c8ca4Slogin // 权限校验 5801496ba7bSLoGin if frame.from_user() 5811496ba7bSLoGin && (verify_area(virt_path_ptr, MAX_PATHLEN as usize).is_err() 582be63f3b2Shoumkh || verify_area(virt_argv_ptr, PAGE_4K_SIZE as usize).is_err()) 583be63f3b2Shoumkh || verify_area(virt_env_ptr, PAGE_4K_SIZE as usize).is_err() 584ab5c8ca4Slogin { 585ab5c8ca4Slogin Err(SystemError::EFAULT) 586ab5c8ca4Slogin } else { 587ab5c8ca4Slogin Self::execve( 5881496ba7bSLoGin path_ptr as *const u8, 5891496ba7bSLoGin argv_ptr as *const *const u8, 5901496ba7bSLoGin env_ptr as *const *const u8, 5911496ba7bSLoGin frame, 592ab5c8ca4Slogin ) 5931496ba7bSLoGin .map(|_| 0) 594ab5c8ca4Slogin } 595ab5c8ca4Slogin } 596ab5c8ca4Slogin SYS_WAIT4 => { 5971496ba7bSLoGin let pid = args[0] as i64; 5981496ba7bSLoGin let wstatus = args[1] as *mut i32; 599ab5c8ca4Slogin let options = args[2] as c_int; 600ab5c8ca4Slogin let rusage = args[3] as *mut c_void; 601ab5c8ca4Slogin // 权限校验 602ab5c8ca4Slogin // todo: 引入rusage之后,更正以下权限校验代码中,rusage的大小 603ab5c8ca4Slogin Self::wait4(pid, wstatus, options, rusage) 604ab5c8ca4Slogin } 605ab5c8ca4Slogin 606ab5c8ca4Slogin SYS_EXIT => { 607ab5c8ca4Slogin let exit_code = args[0]; 608ab5c8ca4Slogin Self::exit(exit_code) 609ab5c8ca4Slogin } 610ab5c8ca4Slogin SYS_MKDIR => { 611ab5c8ca4Slogin let path_ptr = args[0] as *const c_char; 612ab5c8ca4Slogin let mode = args[1]; 613be63f3b2Shoumkh let virt_path_ptr = VirtAddr::new(path_ptr as usize); 614ab5c8ca4Slogin let security_check = || { 615ab5c8ca4Slogin if path_ptr.is_null() 6161496ba7bSLoGin || (frame.from_user() 6171496ba7bSLoGin && verify_area(virt_path_ptr, PAGE_2M_SIZE as usize).is_err()) 618ab5c8ca4Slogin { 619ab5c8ca4Slogin return Err(SystemError::EINVAL); 620ab5c8ca4Slogin } 621ab5c8ca4Slogin let path: &CStr = unsafe { CStr::from_ptr(path_ptr) }; 622ab5c8ca4Slogin let path: &str = path.to_str().map_err(|_| SystemError::EINVAL)?.trim(); 623ab5c8ca4Slogin 624ab5c8ca4Slogin if path == "" { 625ab5c8ca4Slogin return Err(SystemError::EINVAL); 626ab5c8ca4Slogin } 627ab5c8ca4Slogin return Ok(path); 628ab5c8ca4Slogin }; 629ab5c8ca4Slogin 630ab5c8ca4Slogin let path = security_check(); 631ab5c8ca4Slogin if path.is_err() { 632ab5c8ca4Slogin Err(path.unwrap_err()) 633ab5c8ca4Slogin } else { 634ab5c8ca4Slogin Self::mkdir(path.unwrap(), mode) 635ab5c8ca4Slogin } 636ab5c8ca4Slogin } 637ab5c8ca4Slogin 638ab5c8ca4Slogin SYS_NANOSLEEP => { 639ab5c8ca4Slogin let req = args[0] as *const TimeSpec; 640ab5c8ca4Slogin let rem = args[1] as *mut TimeSpec; 641be63f3b2Shoumkh let virt_req = VirtAddr::new(req as usize); 642be63f3b2Shoumkh let virt_rem = VirtAddr::new(rem as usize); 6431496ba7bSLoGin if frame.from_user() 644be63f3b2Shoumkh && (verify_area(virt_req, core::mem::size_of::<TimeSpec>() as usize).is_err() 645be63f3b2Shoumkh || verify_area(virt_rem, core::mem::size_of::<TimeSpec>() as usize) 646be63f3b2Shoumkh .is_err()) 647ab5c8ca4Slogin { 648ab5c8ca4Slogin Err(SystemError::EFAULT) 649ab5c8ca4Slogin } else { 650ab5c8ca4Slogin Self::nanosleep(req, rem) 651ab5c8ca4Slogin } 652ab5c8ca4Slogin } 653ab5c8ca4Slogin 654ab5c8ca4Slogin SYS_CLOCK => Self::clock(), 655ab5c8ca4Slogin SYS_PIPE => { 65622c9db31Shanjiezhou let pipefd: *mut i32 = args[0] as *mut c_int; 65722c9db31Shanjiezhou let arg1 = args[1]; 65822c9db31Shanjiezhou if pipefd.is_null() { 65922c9db31Shanjiezhou Err(SystemError::EFAULT) 66022c9db31Shanjiezhou } else { 66122c9db31Shanjiezhou let flags = FileMode::from_bits_truncate(arg1 as u32); 66222c9db31Shanjiezhou Self::pipe2(pipefd, flags) 663ab5c8ca4Slogin } 664ab5c8ca4Slogin } 665ab5c8ca4Slogin 666ab5c8ca4Slogin SYS_UNLINK_AT => { 667ab5c8ca4Slogin let dirfd = args[0] as i32; 668ab5c8ca4Slogin let pathname = args[1] as *const c_char; 669ab5c8ca4Slogin let flags = args[2] as u32; 670be63f3b2Shoumkh let virt_pathname = VirtAddr::new(pathname as usize); 6711496ba7bSLoGin if frame.from_user() && verify_area(virt_pathname, PAGE_4K_SIZE as usize).is_err() { 672ab5c8ca4Slogin Err(SystemError::EFAULT) 673ab5c8ca4Slogin } else if pathname.is_null() { 674ab5c8ca4Slogin Err(SystemError::EFAULT) 675ab5c8ca4Slogin } else { 676ab5c8ca4Slogin let get_path = || { 677ab5c8ca4Slogin let pathname: &CStr = unsafe { CStr::from_ptr(pathname) }; 678ab5c8ca4Slogin 679ab5c8ca4Slogin let pathname: &str = pathname.to_str().map_err(|_| SystemError::EINVAL)?; 680ab5c8ca4Slogin if pathname.len() >= MAX_PATHLEN { 681ab5c8ca4Slogin return Err(SystemError::ENAMETOOLONG); 682ab5c8ca4Slogin } 683ab5c8ca4Slogin return Ok(pathname.trim()); 684ab5c8ca4Slogin }; 685ab5c8ca4Slogin let pathname = get_path(); 686ab5c8ca4Slogin if pathname.is_err() { 687ab5c8ca4Slogin Err(pathname.unwrap_err()) 688ab5c8ca4Slogin } else { 6896d81180bSLoGin // kdebug!("sys unlinkat: dirfd: {}, pathname: {}", dirfd, pathname.as_ref().unwrap()); 690ab5c8ca4Slogin Self::unlinkat(dirfd, pathname.unwrap(), flags) 691ab5c8ca4Slogin } 692ab5c8ca4Slogin } 693ab5c8ca4Slogin } 694ab5c8ca4Slogin SYS_KILL => { 6951496ba7bSLoGin let pid = Pid::new(args[0]); 696ab5c8ca4Slogin let sig = args[1] as c_int; 697ab5c8ca4Slogin 698ab5c8ca4Slogin Self::kill(pid, sig) 699ab5c8ca4Slogin } 700ab5c8ca4Slogin 701ab5c8ca4Slogin SYS_SIGACTION => { 702ab5c8ca4Slogin let sig = args[0] as c_int; 703ab5c8ca4Slogin let act = args[1]; 704ab5c8ca4Slogin let old_act = args[2]; 7051496ba7bSLoGin Self::sigaction(sig, act, old_act, frame.from_user()) 706ab5c8ca4Slogin } 707ab5c8ca4Slogin 708ab5c8ca4Slogin SYS_RT_SIGRETURN => { 709ab5c8ca4Slogin // 由于目前signal机制的实现,与x86_64强关联,因此暂时在arch/x86_64/syscall.rs中调用 710ab5c8ca4Slogin // todo: 未来需要将signal机制与平台解耦 711ab5c8ca4Slogin todo!() 712ab5c8ca4Slogin } 713ab5c8ca4Slogin 7141496ba7bSLoGin SYS_GETPID => Self::getpid().map(|pid| pid.into()), 715ab5c8ca4Slogin 7161496ba7bSLoGin SYS_SCHED => Self::sched(frame.from_user()), 717ab5c8ca4Slogin SYS_DUP => { 718ab5c8ca4Slogin let oldfd: i32 = args[0] as c_int; 719ab5c8ca4Slogin Self::dup(oldfd) 720ab5c8ca4Slogin } 721ab5c8ca4Slogin SYS_DUP2 => { 722ab5c8ca4Slogin let oldfd: i32 = args[0] as c_int; 723ab5c8ca4Slogin let newfd: i32 = args[1] as c_int; 724ab5c8ca4Slogin Self::dup2(oldfd, newfd) 725ab5c8ca4Slogin } 726ab5c8ca4Slogin 727ab5c8ca4Slogin SYS_SOCKET => Self::socket(args[0], args[1], args[2]), 728ab5c8ca4Slogin SYS_SETSOCKOPT => { 729ab5c8ca4Slogin let optval = args[3] as *const u8; 730ab5c8ca4Slogin let optlen = args[4] as usize; 731be63f3b2Shoumkh let virt_optval = VirtAddr::new(optval as usize); 732ab5c8ca4Slogin // 验证optval的地址是否合法 733be63f3b2Shoumkh if verify_area(virt_optval, optlen as usize).is_err() { 734ab5c8ca4Slogin // 地址空间超出了用户空间的范围,不合法 735ab5c8ca4Slogin Err(SystemError::EFAULT) 736ab5c8ca4Slogin } else { 737ab5c8ca4Slogin let data: &[u8] = unsafe { core::slice::from_raw_parts(optval, optlen) }; 738ab5c8ca4Slogin Self::setsockopt(args[0], args[1], args[2], data) 739ab5c8ca4Slogin } 740ab5c8ca4Slogin } 741ab5c8ca4Slogin SYS_GETSOCKOPT => { 742ab5c8ca4Slogin let optval = args[3] as *mut u8; 743ab5c8ca4Slogin let optlen = args[4] as *mut usize; 744be63f3b2Shoumkh let virt_optval = VirtAddr::new(optval as usize); 745be63f3b2Shoumkh let virt_optlen = VirtAddr::new(optlen as usize); 746ab5c8ca4Slogin let security_check = || { 747ab5c8ca4Slogin // 验证optval的地址是否合法 748be63f3b2Shoumkh if verify_area(virt_optval, PAGE_4K_SIZE as usize).is_err() { 749ab5c8ca4Slogin // 地址空间超出了用户空间的范围,不合法 750ab5c8ca4Slogin return Err(SystemError::EFAULT); 751ab5c8ca4Slogin } 752ab5c8ca4Slogin 753ab5c8ca4Slogin // 验证optlen的地址是否合法 754be63f3b2Shoumkh if verify_area(virt_optlen, core::mem::size_of::<u32>() as usize).is_err() { 755ab5c8ca4Slogin // 地址空间超出了用户空间的范围,不合法 756ab5c8ca4Slogin return Err(SystemError::EFAULT); 757ab5c8ca4Slogin } 758ab5c8ca4Slogin return Ok(()); 759ab5c8ca4Slogin }; 760ab5c8ca4Slogin let r = security_check(); 761ab5c8ca4Slogin if r.is_err() { 762ab5c8ca4Slogin Err(r.unwrap_err()) 763ab5c8ca4Slogin } else { 764ab5c8ca4Slogin Self::getsockopt(args[0], args[1], args[2], optval, optlen as *mut u32) 765ab5c8ca4Slogin } 766ab5c8ca4Slogin } 767ab5c8ca4Slogin 768ab5c8ca4Slogin SYS_CONNECT => { 769ab5c8ca4Slogin let addr = args[1] as *const SockAddr; 770ab5c8ca4Slogin let addrlen = args[2] as usize; 771be63f3b2Shoumkh let virt_addr = VirtAddr::new(addr as usize); 772ab5c8ca4Slogin // 验证addr的地址是否合法 773be63f3b2Shoumkh if verify_area(virt_addr, addrlen as usize).is_err() { 774ab5c8ca4Slogin // 地址空间超出了用户空间的范围,不合法 775ab5c8ca4Slogin Err(SystemError::EFAULT) 776ab5c8ca4Slogin } else { 777ab5c8ca4Slogin Self::connect(args[0], addr, addrlen) 778ab5c8ca4Slogin } 779ab5c8ca4Slogin } 780ab5c8ca4Slogin SYS_BIND => { 781ab5c8ca4Slogin let addr = args[1] as *const SockAddr; 782ab5c8ca4Slogin let addrlen = args[2] as usize; 783be63f3b2Shoumkh let virt_addr = VirtAddr::new(addr as usize); 784ab5c8ca4Slogin // 验证addr的地址是否合法 785be63f3b2Shoumkh if verify_area(virt_addr, addrlen as usize).is_err() { 786ab5c8ca4Slogin // 地址空间超出了用户空间的范围,不合法 787ab5c8ca4Slogin Err(SystemError::EFAULT) 788ab5c8ca4Slogin } else { 789ab5c8ca4Slogin Self::bind(args[0], addr, addrlen) 790ab5c8ca4Slogin } 791ab5c8ca4Slogin } 792ab5c8ca4Slogin 793ab5c8ca4Slogin SYS_SENDTO => { 794ab5c8ca4Slogin let buf = args[1] as *const u8; 795ab5c8ca4Slogin let len = args[2] as usize; 796ab5c8ca4Slogin let flags = args[3] as u32; 797ab5c8ca4Slogin let addr = args[4] as *const SockAddr; 798ab5c8ca4Slogin let addrlen = args[5] as usize; 799be63f3b2Shoumkh let virt_buf = VirtAddr::new(buf as usize); 800be63f3b2Shoumkh let virt_addr = VirtAddr::new(addr as usize); 801ab5c8ca4Slogin // 验证buf的地址是否合法 802be63f3b2Shoumkh if verify_area(virt_buf, len as usize).is_err() { 803ab5c8ca4Slogin // 地址空间超出了用户空间的范围,不合法 804ab5c8ca4Slogin Err(SystemError::EFAULT) 805be63f3b2Shoumkh } else if verify_area(virt_addr, addrlen as usize).is_err() { 806ab5c8ca4Slogin // 地址空间超出了用户空间的范围,不合法 807ab5c8ca4Slogin Err(SystemError::EFAULT) 808ab5c8ca4Slogin } else { 809ab5c8ca4Slogin let data: &[u8] = unsafe { core::slice::from_raw_parts(buf, len) }; 810ab5c8ca4Slogin Self::sendto(args[0], data, flags, addr, addrlen) 811ab5c8ca4Slogin } 812ab5c8ca4Slogin } 813ab5c8ca4Slogin 814ab5c8ca4Slogin SYS_RECVFROM => { 815ab5c8ca4Slogin let buf = args[1] as *mut u8; 816ab5c8ca4Slogin let len = args[2] as usize; 817ab5c8ca4Slogin let flags = args[3] as u32; 818ab5c8ca4Slogin let addr = args[4] as *mut SockAddr; 819ab5c8ca4Slogin let addrlen = args[5] as *mut usize; 820be63f3b2Shoumkh let virt_buf = VirtAddr::new(buf as usize); 821be63f3b2Shoumkh let virt_addrlen = VirtAddr::new(addrlen as usize); 822be63f3b2Shoumkh let virt_addr = VirtAddr::new(addr as usize); 823ab5c8ca4Slogin let security_check = || { 824ab5c8ca4Slogin // 验证buf的地址是否合法 825be63f3b2Shoumkh if verify_area(virt_buf, len as usize).is_err() { 826ab5c8ca4Slogin // 地址空间超出了用户空间的范围,不合法 827ab5c8ca4Slogin return Err(SystemError::EFAULT); 828ab5c8ca4Slogin } 829ab5c8ca4Slogin 830ab5c8ca4Slogin // 验证addrlen的地址是否合法 831be63f3b2Shoumkh if verify_area(virt_addrlen, core::mem::size_of::<u32>() as usize).is_err() { 832ab5c8ca4Slogin // 地址空间超出了用户空间的范围,不合法 833ab5c8ca4Slogin return Err(SystemError::EFAULT); 834ab5c8ca4Slogin } 835ab5c8ca4Slogin 836be63f3b2Shoumkh if verify_area(virt_addr, core::mem::size_of::<SockAddr>() as usize).is_err() { 837ab5c8ca4Slogin // 地址空间超出了用户空间的范围,不合法 838ab5c8ca4Slogin return Err(SystemError::EFAULT); 839ab5c8ca4Slogin } 840ab5c8ca4Slogin return Ok(()); 841ab5c8ca4Slogin }; 842ab5c8ca4Slogin let r = security_check(); 843ab5c8ca4Slogin if r.is_err() { 844ab5c8ca4Slogin Err(r.unwrap_err()) 845ab5c8ca4Slogin } else { 846ab5c8ca4Slogin let buf = unsafe { core::slice::from_raw_parts_mut(buf, len) }; 847ab5c8ca4Slogin Self::recvfrom(args[0], buf, flags, addr, addrlen as *mut u32) 848ab5c8ca4Slogin } 849ab5c8ca4Slogin } 850ab5c8ca4Slogin 851ab5c8ca4Slogin SYS_RECVMSG => { 852ab5c8ca4Slogin let msg = args[1] as *mut crate::net::syscall::MsgHdr; 853ab5c8ca4Slogin let flags = args[2] as u32; 8544537ffb7SChiichen match UserBufferWriter::new( 8554537ffb7SChiichen msg, 8564537ffb7SChiichen core::mem::size_of::<crate::net::syscall::MsgHdr>(), 8574537ffb7SChiichen true, 8584537ffb7SChiichen ) { 8594537ffb7SChiichen Err(e) => Err(e), 8604537ffb7SChiichen Ok(mut user_buffer_writer) => { 8614537ffb7SChiichen match user_buffer_writer.buffer::<crate::net::syscall::MsgHdr>(0) { 8624537ffb7SChiichen Err(e) => Err(e), 8634537ffb7SChiichen Ok(buffer) => { 8644537ffb7SChiichen let msg = &mut buffer[0]; 865ab5c8ca4Slogin Self::recvmsg(args[0], msg, flags) 866ab5c8ca4Slogin } 867ab5c8ca4Slogin } 8684537ffb7SChiichen } 8694537ffb7SChiichen } 8704537ffb7SChiichen } 871ab5c8ca4Slogin 872ab5c8ca4Slogin SYS_LISTEN => Self::listen(args[0], args[1]), 873ab5c8ca4Slogin SYS_SHUTDOWN => Self::shutdown(args[0], args[1]), 874ab5c8ca4Slogin SYS_ACCEPT => Self::accept(args[0], args[1] as *mut SockAddr, args[2] as *mut u32), 875ab5c8ca4Slogin SYS_GETSOCKNAME => { 876ab5c8ca4Slogin Self::getsockname(args[0], args[1] as *mut SockAddr, args[2] as *mut u32) 877ab5c8ca4Slogin } 878ab5c8ca4Slogin SYS_GETPEERNAME => { 879ab5c8ca4Slogin Self::getpeername(args[0], args[1] as *mut SockAddr, args[2] as *mut u32) 880ab5c8ca4Slogin } 88136fd0130Shoumkh SYS_GETTIMEOFDAY => { 88236fd0130Shoumkh let timeval = args[0] as *mut PosixTimeval; 88340fe15e0SLoGin let timezone_ptr = args[1] as *mut PosixTimeZone; 88440fe15e0SLoGin Self::gettimeofday(timeval, timezone_ptr) 8854537ffb7SChiichen } 88640fe15e0SLoGin SYS_MMAP => { 88740fe15e0SLoGin let len = page_align_up(args[1]); 888be63f3b2Shoumkh let virt_addr = VirtAddr::new(args[0] as usize); 889be63f3b2Shoumkh if verify_area(virt_addr, len as usize).is_err() { 89040fe15e0SLoGin Err(SystemError::EFAULT) 89140fe15e0SLoGin } else { 89240fe15e0SLoGin Self::mmap( 89340fe15e0SLoGin VirtAddr::new(args[0]), 89440fe15e0SLoGin len, 89540fe15e0SLoGin args[2], 89640fe15e0SLoGin args[3], 89740fe15e0SLoGin args[4] as i32, 89840fe15e0SLoGin args[5], 89940fe15e0SLoGin ) 90040fe15e0SLoGin } 90140fe15e0SLoGin } 90240fe15e0SLoGin SYS_MUNMAP => { 90340fe15e0SLoGin let addr = args[0]; 90440fe15e0SLoGin let len = page_align_up(args[1]); 90540fe15e0SLoGin if addr & MMArch::PAGE_SIZE != 0 { 90640fe15e0SLoGin // The addr argument is not a multiple of the page size 90740fe15e0SLoGin Err(SystemError::EINVAL) 90840fe15e0SLoGin } else { 90940fe15e0SLoGin Self::munmap(VirtAddr::new(addr), len) 91040fe15e0SLoGin } 91140fe15e0SLoGin } 91240fe15e0SLoGin SYS_MPROTECT => { 91340fe15e0SLoGin let addr = args[0]; 91440fe15e0SLoGin let len = page_align_up(args[1]); 91540fe15e0SLoGin if addr & MMArch::PAGE_SIZE != 0 { 91640fe15e0SLoGin // The addr argument is not a multiple of the page size 91740fe15e0SLoGin Err(SystemError::EINVAL) 91840fe15e0SLoGin } else { 91940fe15e0SLoGin Self::mprotect(VirtAddr::new(addr), len, args[2]) 92040fe15e0SLoGin } 92140fe15e0SLoGin } 92240fe15e0SLoGin 9231496ba7bSLoGin SYS_GETCWD => { 9241496ba7bSLoGin let buf = args[0] as *mut u8; 9251496ba7bSLoGin let size = args[1] as usize; 9261496ba7bSLoGin let security_check = || { 9271496ba7bSLoGin verify_area(VirtAddr::new(buf as usize), size)?; 9281496ba7bSLoGin return Ok(()); 9291496ba7bSLoGin }; 9301496ba7bSLoGin let r = security_check(); 9311496ba7bSLoGin if r.is_err() { 9321496ba7bSLoGin Err(r.unwrap_err()) 9331496ba7bSLoGin } else { 9341496ba7bSLoGin let buf = unsafe { core::slice::from_raw_parts_mut(buf, size) }; 9351496ba7bSLoGin Self::getcwd(buf).map(|ptr| ptr.data()) 9361496ba7bSLoGin } 9371496ba7bSLoGin } 9381496ba7bSLoGin 9391496ba7bSLoGin SYS_GETPGID => Self::getpgid(Pid::new(args[0])).map(|pid| pid.into()), 9401496ba7bSLoGin 9411496ba7bSLoGin SYS_GETPPID => Self::getppid().map(|pid| pid.into()), 942be63f3b2Shoumkh SYS_FSTAT => { 943be63f3b2Shoumkh let fd = args[0] as i32; 944be63f3b2Shoumkh let kstat = args[1] as *mut PosixKstat; 945be63f3b2Shoumkh let vaddr = VirtAddr::new(kstat as usize); 946be63f3b2Shoumkh // FIXME 由于c中的verify_area与rust中的verify_area重名,所以在引入时加了前缀区分 947be63f3b2Shoumkh // TODO 应该将用了c版本的verify_area都改为rust的verify_area 948be63f3b2Shoumkh match verify_area(vaddr, core::mem::size_of::<PosixKstat>()) { 949be63f3b2Shoumkh Ok(_) => Self::fstat(fd, kstat), 950be63f3b2Shoumkh Err(e) => Err(e), 951be63f3b2Shoumkh } 952be63f3b2Shoumkh } 953be63f3b2Shoumkh 9546d81180bSLoGin SYS_FCNTL => { 9556d81180bSLoGin let fd = args[0] as i32; 9566d81180bSLoGin let cmd: Option<FcntlCommand> = 9576d81180bSLoGin <FcntlCommand as FromPrimitive>::from_u32(args[1] as u32); 9586d81180bSLoGin let arg = args[2] as i32; 9596d81180bSLoGin let res = if let Some(cmd) = cmd { 9606d81180bSLoGin Self::fcntl(fd, cmd, arg) 9616d81180bSLoGin } else { 9626d81180bSLoGin Err(SystemError::EINVAL) 9636d81180bSLoGin }; 9646d81180bSLoGin 9656d81180bSLoGin // kdebug!("FCNTL: fd: {}, cmd: {:?}, arg: {}, res: {:?}", fd, cmd, arg, res); 9666d81180bSLoGin res 9676d81180bSLoGin } 9686d81180bSLoGin 9696d81180bSLoGin SYS_FTRUNCATE => { 9706d81180bSLoGin let fd = args[0] as i32; 9716d81180bSLoGin let len = args[1] as usize; 9726d81180bSLoGin let res = Self::ftruncate(fd, len); 9736d81180bSLoGin // kdebug!("FTRUNCATE: fd: {}, len: {}, res: {:?}", fd, len, res); 9746d81180bSLoGin res 9756d81180bSLoGin } 9766d81180bSLoGin 977*2dbef785SGnoCiYeH SYS_MKNOD => { 978*2dbef785SGnoCiYeH let path = args[0]; 979*2dbef785SGnoCiYeH let flags = args[1]; 980*2dbef785SGnoCiYeH let dev_t = args[2]; 981*2dbef785SGnoCiYeH let flags: ModeType = ModeType::from_bits_truncate(flags as u32); 982*2dbef785SGnoCiYeH Self::mknod(path as *const i8, flags, DeviceNumber::from(dev_t)) 983*2dbef785SGnoCiYeH } 984*2dbef785SGnoCiYeH 985ab5c8ca4Slogin _ => panic!("Unsupported syscall ID: {}", syscall_num), 986ab5c8ca4Slogin }; 987ab5c8ca4Slogin 988ab5c8ca4Slogin let r = r.unwrap_or_else(|e| e.to_posix_errno() as usize); 989ab5c8ca4Slogin return r; 990ab5c8ca4Slogin } 991ab5c8ca4Slogin 992ab5c8ca4Slogin pub fn put_string( 993ab5c8ca4Slogin s: *const u8, 994ab5c8ca4Slogin front_color: u32, 995ab5c8ca4Slogin back_color: u32, 996ab5c8ca4Slogin ) -> Result<usize, SystemError> { 997ab5c8ca4Slogin return Ok(unsafe { do_put_string(s, front_color, back_color) }); 998ab5c8ca4Slogin } 999ab5c8ca4Slogin 1000ab5c8ca4Slogin pub fn reboot() -> Result<usize, SystemError> { 1001ab5c8ca4Slogin cpu_reset(); 1002ab5c8ca4Slogin } 1003ab5c8ca4Slogin } 1004