xref: /DragonOS/kernel/src/syscall/mod.rs (revision 4ad52e57e612a88ab09413c7ac0072db96a93632)
1ab5c8ca4Slogin use core::{
2ab5c8ca4Slogin     ffi::{c_char, c_int, c_void, CStr},
3ab5c8ca4Slogin     sync::atomic::{AtomicBool, Ordering},
4ab5c8ca4Slogin };
5ab5c8ca4Slogin 
6971462beSGnoCiYeH use crate::{
740609970SGnoCiYeH     arch::{ipc::signal::SigSet, syscall::nr::*},
8c566df45SLoGin     driver::base::device::device_number::DeviceNumber,
9971462beSGnoCiYeH     libs::{futex::constant::FutexFlag, rand::GRandFlags},
10*4ad52e57S裕依2439     net::syscall::MsgHdr,
110d9b7d92SLoGin     process::{
120d9b7d92SLoGin         fork::KernelCloneArgs,
130d9b7d92SLoGin         resource::{RLimit64, RUsage},
140d9b7d92SLoGin     },
15971462beSGnoCiYeH };
1640314b30SXiaoye Zheng 
1791e9d4abSLoGin use num_traits::FromPrimitive;
1891e9d4abSLoGin use system_error::SystemError;
19676b8ef6SMork 
20ab5c8ca4Slogin use crate::{
211496ba7bSLoGin     arch::{cpu::cpu_reset, interrupt::TrapFrame, MMArch},
224fda81ceSLoGin     driver::base::block::SeekFrom,
23ab5c8ca4Slogin     filesystem::vfs::{
246d81180bSLoGin         fcntl::FcntlCommand,
25ab5c8ca4Slogin         file::FileMode,
262dbef785SGnoCiYeH         syscall::{ModeType, PosixKstat, SEEK_CUR, SEEK_END, SEEK_MAX, SEEK_SET},
27ab5c8ca4Slogin         MAX_PATHLEN,
28ab5c8ca4Slogin     },
291496ba7bSLoGin     include::bindings::bindings::{PAGE_2M_SIZE, PAGE_4K_SIZE},
30aa0367d6SLoGin     kinfo,
3140fe15e0SLoGin     libs::align::page_align_up,
32be63f3b2Shoumkh     mm::{verify_area, MemoryManagementArch, VirtAddr},
33ab5c8ca4Slogin     net::syscall::SockAddr,
34971462beSGnoCiYeH     process::{fork::CloneFlags, Pid},
3536fd0130Shoumkh     time::{
3640fe15e0SLoGin         syscall::{PosixTimeZone, PosixTimeval},
3736fd0130Shoumkh         TimeSpec,
3836fd0130Shoumkh     },
39ab5c8ca4Slogin };
40ab5c8ca4Slogin 
41bf4a4899SLoGin use self::{
42bf4a4899SLoGin     misc::SysInfo,
43bf4a4899SLoGin     user_access::{UserBufferReader, UserBufferWriter},
44bf4a4899SLoGin };
454537ffb7SChiichen 
46bf4a4899SLoGin pub mod misc;
4740fe15e0SLoGin pub mod user_access;
4840fe15e0SLoGin 
49ad1d649eSGnoCiYeH // 与linux不一致的调用,在linux基础上累加
50ad1d649eSGnoCiYeH pub const SYS_PUT_STRING: usize = 100000;
51ad1d649eSGnoCiYeH pub const SYS_SBRK: usize = 100001;
52ab5c8ca4Slogin /// todo: 该系统调用与Linux不一致,将来需要删除该系统调用!!! 删的时候记得改C版本的libc
53ad1d649eSGnoCiYeH pub const SYS_CLOCK: usize = 100002;
54ad1d649eSGnoCiYeH pub const SYS_SCHED: usize = 100003;
5540314b30SXiaoye Zheng 
56ab5c8ca4Slogin #[derive(Debug)]
57ab5c8ca4Slogin pub struct Syscall;
58ab5c8ca4Slogin 
59ab5c8ca4Slogin extern "C" {
60ab5c8ca4Slogin     fn do_put_string(s: *const u8, front_color: u32, back_color: u32) -> usize;
61ab5c8ca4Slogin }
62ab5c8ca4Slogin 
63ab5c8ca4Slogin impl Syscall {
64ab5c8ca4Slogin     /// 初始化系统调用
655b59005fSLoGin     #[inline(never)]
66ab5c8ca4Slogin     pub fn init() -> Result<(), SystemError> {
67ab5c8ca4Slogin         static INIT_FLAG: AtomicBool = AtomicBool::new(false);
68ab5c8ca4Slogin         let prev = INIT_FLAG.swap(true, Ordering::SeqCst);
69ab5c8ca4Slogin         if prev {
70ab5c8ca4Slogin             panic!("Cannot initialize syscall more than once!");
71ab5c8ca4Slogin         }
725b59005fSLoGin         kinfo!("Initializing syscall...");
735b59005fSLoGin         let r = crate::arch::syscall::arch_syscall_init();
745b59005fSLoGin         kinfo!("Syscall init successfully!");
755b59005fSLoGin 
765b59005fSLoGin         return r;
77ab5c8ca4Slogin     }
78ab5c8ca4Slogin     /// @brief 系统调用分发器,用于分发系统调用。
79ab5c8ca4Slogin     ///
80ab5c8ca4Slogin     /// 这个函数内,需要根据系统调用号,调用对应的系统调用处理函数。
81ab5c8ca4Slogin     /// 并且,对于用户态传入的指针参数,需要在本函数内进行越界检查,防止访问到内核空间。
824fda81ceSLoGin     #[inline(never)]
839e9ffedfSPlucky923     pub fn handle(
849e9ffedfSPlucky923         syscall_num: usize,
859e9ffedfSPlucky923         args: &[usize],
869e9ffedfSPlucky923         frame: &mut TrapFrame,
879e9ffedfSPlucky923     ) -> Result<usize, SystemError> {
88ab5c8ca4Slogin         let r = match syscall_num {
89ab5c8ca4Slogin             SYS_PUT_STRING => {
90ab5c8ca4Slogin                 Self::put_string(args[0] as *const u8, args[1] as u32, args[2] as u32)
91ab5c8ca4Slogin             }
924fda81ceSLoGin             #[cfg(target_arch = "x86_64")]
93ab5c8ca4Slogin             SYS_OPEN => {
94ab5c8ca4Slogin                 let path: &CStr = unsafe { CStr::from_ptr(args[0] as *const c_char) };
95ab5c8ca4Slogin                 let path: Result<&str, core::str::Utf8Error> = path.to_str();
96ab5c8ca4Slogin                 let res = if path.is_err() {
97ab5c8ca4Slogin                     Err(SystemError::EINVAL)
98ab5c8ca4Slogin                 } else {
99ab5c8ca4Slogin                     let path: &str = path.unwrap();
10034e6d6c8Syuyi2439 
101ab5c8ca4Slogin                     let flags = args[1];
1020fb515b0SLoGin                     let mode = args[2];
1030fb515b0SLoGin 
104ab5c8ca4Slogin                     let open_flags: FileMode = FileMode::from_bits_truncate(flags as u32);
1050fb515b0SLoGin                     let mode = ModeType::from_bits(mode as u32).ok_or(SystemError::EINVAL)?;
1060fb515b0SLoGin                     Self::open(path, open_flags, mode, true)
1070fb515b0SLoGin                 };
1080fb515b0SLoGin                 res
1090fb515b0SLoGin             }
1100fb515b0SLoGin 
1110fb515b0SLoGin             SYS_OPENAT => {
1120fb515b0SLoGin                 let dirfd = args[0] as i32;
1130fb515b0SLoGin                 let path: &CStr = unsafe { CStr::from_ptr(args[1] as *const c_char) };
1140fb515b0SLoGin                 let flags = args[2];
1150fb515b0SLoGin                 let mode = args[3];
1160fb515b0SLoGin 
1170fb515b0SLoGin                 let path: Result<&str, core::str::Utf8Error> = path.to_str();
1180fb515b0SLoGin                 let res = if path.is_err() {
1190fb515b0SLoGin                     Err(SystemError::EINVAL)
1200fb515b0SLoGin                 } else {
1210fb515b0SLoGin                     let path: &str = path.unwrap();
1220fb515b0SLoGin 
1230fb515b0SLoGin                     let open_flags: FileMode =
1240fb515b0SLoGin                         FileMode::from_bits(flags as u32).ok_or(SystemError::EINVAL)?;
1250fb515b0SLoGin                     let mode = ModeType::from_bits(mode as u32).ok_or(SystemError::EINVAL)?;
1260fb515b0SLoGin                     Self::openat(dirfd, path, open_flags, mode, true)
127ab5c8ca4Slogin                 };
128ab5c8ca4Slogin                 res
129ab5c8ca4Slogin             }
130ab5c8ca4Slogin             SYS_CLOSE => {
131ab5c8ca4Slogin                 let fd = args[0];
13234e6d6c8Syuyi2439                 let res = Self::close(fd);
13334e6d6c8Syuyi2439 
13434e6d6c8Syuyi2439                 res
135ab5c8ca4Slogin             }
136ab5c8ca4Slogin             SYS_READ => {
137ab5c8ca4Slogin                 let fd = args[0] as i32;
138ab5c8ca4Slogin                 let buf_vaddr = args[1];
139ab5c8ca4Slogin                 let len = args[2];
1409e9ffedfSPlucky923                 let from_user = frame.from_user();
1419e9ffedfSPlucky923                 let mut user_buffer_writer =
1429e9ffedfSPlucky923                     UserBufferWriter::new(buf_vaddr as *mut u8, len, from_user)?;
1436d81180bSLoGin 
1449e9ffedfSPlucky923                 let user_buf = user_buffer_writer.buffer(0)?;
1459e9ffedfSPlucky923                 let res = Self::read(fd, user_buf);
146ab5c8ca4Slogin                 res
147ab5c8ca4Slogin             }
148ab5c8ca4Slogin             SYS_WRITE => {
149ab5c8ca4Slogin                 let fd = args[0] as i32;
150ab5c8ca4Slogin                 let buf_vaddr = args[1];
151ab5c8ca4Slogin                 let len = args[2];
1529e9ffedfSPlucky923                 let from_user = frame.from_user();
1539e9ffedfSPlucky923                 let user_buffer_reader =
1549e9ffedfSPlucky923                     UserBufferReader::new(buf_vaddr as *const u8, len, from_user)?;
1556d81180bSLoGin 
1569e9ffedfSPlucky923                 let user_buf = user_buffer_reader.read_from_user(0)?;
1579e9ffedfSPlucky923                 let res = Self::write(fd, user_buf);
158ab5c8ca4Slogin                 res
159ab5c8ca4Slogin             }
160ab5c8ca4Slogin 
161ab5c8ca4Slogin             SYS_LSEEK => {
162ab5c8ca4Slogin                 let fd = args[0] as i32;
163ab5c8ca4Slogin                 let offset = args[1] as i64;
164ab5c8ca4Slogin                 let whence = args[2] as u32;
165ab5c8ca4Slogin 
166ab5c8ca4Slogin                 let w = match whence {
167ab5c8ca4Slogin                     SEEK_SET => Ok(SeekFrom::SeekSet(offset)),
168ab5c8ca4Slogin                     SEEK_CUR => Ok(SeekFrom::SeekCurrent(offset)),
169ab5c8ca4Slogin                     SEEK_END => Ok(SeekFrom::SeekEnd(offset)),
170ab5c8ca4Slogin                     SEEK_MAX => Ok(SeekFrom::SeekEnd(0)),
171ab5c8ca4Slogin                     _ => Err(SystemError::EINVAL),
1729e9ffedfSPlucky923                 }?;
173ab5c8ca4Slogin 
174ab5c8ca4Slogin                 Self::lseek(fd, w)
175ab5c8ca4Slogin             }
17640314b30SXiaoye Zheng             SYS_IOCTL => {
17740314b30SXiaoye Zheng                 let fd = args[0];
17840314b30SXiaoye Zheng                 let cmd = args[1];
17940314b30SXiaoye Zheng                 let data = args[2];
18040314b30SXiaoye Zheng                 Self::ioctl(fd, cmd as u32, data)
18140314b30SXiaoye Zheng             }
182ab5c8ca4Slogin 
1834fda81ceSLoGin             #[cfg(target_arch = "x86_64")]
1841496ba7bSLoGin             SYS_FORK => Self::fork(frame),
1854fda81ceSLoGin             #[cfg(target_arch = "x86_64")]
1861496ba7bSLoGin             SYS_VFORK => Self::vfork(frame),
1871496ba7bSLoGin 
188ab5c8ca4Slogin             SYS_BRK => {
18940fe15e0SLoGin                 let new_brk = VirtAddr::new(args[0]);
19040fe15e0SLoGin                 Self::brk(new_brk).map(|vaddr| vaddr.data())
191ab5c8ca4Slogin             }
192ab5c8ca4Slogin 
193ab5c8ca4Slogin             SYS_SBRK => {
194ab5c8ca4Slogin                 let increment = args[0] as isize;
1951496ba7bSLoGin                 Self::sbrk(increment).map(|vaddr: VirtAddr| vaddr.data())
196ab5c8ca4Slogin             }
197ab5c8ca4Slogin 
198ab5c8ca4Slogin             SYS_REBOOT => Self::reboot(),
199ab5c8ca4Slogin 
200ab5c8ca4Slogin             SYS_CHDIR => {
201ab5c8ca4Slogin                 // Closure for checking arguments
202ab5c8ca4Slogin                 let chdir_check = |arg0: usize| {
203ab5c8ca4Slogin                     if arg0 == 0 {
204ab5c8ca4Slogin                         return Err(SystemError::EFAULT);
205ab5c8ca4Slogin                     }
206ab5c8ca4Slogin                     let path_ptr = arg0 as *const c_char;
207be63f3b2Shoumkh                     let virt_addr = VirtAddr::new(path_ptr as usize);
208ab5c8ca4Slogin                     // 权限校验
209ab5c8ca4Slogin                     if path_ptr.is_null()
2101496ba7bSLoGin                         || (frame.from_user()
2111496ba7bSLoGin                             && verify_area(virt_addr, PAGE_2M_SIZE as usize).is_err())
212ab5c8ca4Slogin                     {
213ab5c8ca4Slogin                         return Err(SystemError::EINVAL);
214ab5c8ca4Slogin                     }
215ab5c8ca4Slogin                     let dest_path: &CStr = unsafe { CStr::from_ptr(path_ptr) };
216ab5c8ca4Slogin                     let dest_path: &str = dest_path.to_str().map_err(|_| SystemError::EINVAL)?;
217ab5c8ca4Slogin                     if dest_path.len() == 0 {
218ab5c8ca4Slogin                         return Err(SystemError::EINVAL);
2191496ba7bSLoGin                     } else if dest_path.len() > MAX_PATHLEN as usize {
220ab5c8ca4Slogin                         return Err(SystemError::ENAMETOOLONG);
221ab5c8ca4Slogin                     }
222ab5c8ca4Slogin 
223ab5c8ca4Slogin                     return Ok(dest_path);
224ab5c8ca4Slogin                 };
225ab5c8ca4Slogin 
2269e9ffedfSPlucky923                 let r = chdir_check(args[0])?;
2279e9ffedfSPlucky923                 Self::chdir(r)
228ab5c8ca4Slogin             }
229ab5c8ca4Slogin 
2304fda81ceSLoGin             #[allow(unreachable_patterns)]
2314fda81ceSLoGin             SYS_GETDENTS64 | SYS_GETDENTS => {
232ab5c8ca4Slogin                 let fd = args[0] as i32;
23334e6d6c8Syuyi2439 
234ab5c8ca4Slogin                 let buf_vaddr = args[1];
235ab5c8ca4Slogin                 let len = args[2];
236f5df0e79SLoGin                 let virt_addr: VirtAddr = VirtAddr::new(buf_vaddr);
237ab5c8ca4Slogin                 // 判断缓冲区是否来自用户态,进行权限校验
2381496ba7bSLoGin                 let res = if frame.from_user() && verify_area(virt_addr, len as usize).is_err() {
239ab5c8ca4Slogin                     // 来自用户态,而buffer在内核态,这样的操作不被允许
240ab5c8ca4Slogin                     Err(SystemError::EPERM)
241ab5c8ca4Slogin                 } else if buf_vaddr == 0 {
242ab5c8ca4Slogin                     Err(SystemError::EFAULT)
243ab5c8ca4Slogin                 } else {
244ab5c8ca4Slogin                     let buf: &mut [u8] = unsafe {
245ab5c8ca4Slogin                         core::slice::from_raw_parts_mut::<'static, u8>(buf_vaddr as *mut u8, len)
246ab5c8ca4Slogin                     };
247ab5c8ca4Slogin                     Self::getdents(fd, buf)
248ab5c8ca4Slogin                 };
249ab5c8ca4Slogin 
250ab5c8ca4Slogin                 res
251ab5c8ca4Slogin             }
252ab5c8ca4Slogin 
253ab5c8ca4Slogin             SYS_EXECVE => {
254ab5c8ca4Slogin                 let path_ptr = args[0];
255ab5c8ca4Slogin                 let argv_ptr = args[1];
256ab5c8ca4Slogin                 let env_ptr = args[2];
257be63f3b2Shoumkh                 let virt_path_ptr = VirtAddr::new(path_ptr);
258be63f3b2Shoumkh                 let virt_argv_ptr = VirtAddr::new(argv_ptr);
259be63f3b2Shoumkh                 let virt_env_ptr = VirtAddr::new(env_ptr);
260ab5c8ca4Slogin                 // 权限校验
2611496ba7bSLoGin                 if frame.from_user()
2621496ba7bSLoGin                     && (verify_area(virt_path_ptr, MAX_PATHLEN as usize).is_err()
263be63f3b2Shoumkh                         || verify_area(virt_argv_ptr, PAGE_4K_SIZE as usize).is_err())
264be63f3b2Shoumkh                     || verify_area(virt_env_ptr, PAGE_4K_SIZE as usize).is_err()
265ab5c8ca4Slogin                 {
266ab5c8ca4Slogin                     Err(SystemError::EFAULT)
267ab5c8ca4Slogin                 } else {
268ab5c8ca4Slogin                     Self::execve(
2691496ba7bSLoGin                         path_ptr as *const u8,
2701496ba7bSLoGin                         argv_ptr as *const *const u8,
2711496ba7bSLoGin                         env_ptr as *const *const u8,
2721496ba7bSLoGin                         frame,
273ab5c8ca4Slogin                     )
2741496ba7bSLoGin                     .map(|_| 0)
275ab5c8ca4Slogin                 }
276ab5c8ca4Slogin             }
277ab5c8ca4Slogin             SYS_WAIT4 => {
278bf4a4899SLoGin                 let pid = args[0] as i32;
2791496ba7bSLoGin                 let wstatus = args[1] as *mut i32;
280ab5c8ca4Slogin                 let options = args[2] as c_int;
281ab5c8ca4Slogin                 let rusage = args[3] as *mut c_void;
282ab5c8ca4Slogin                 // 权限校验
283ab5c8ca4Slogin                 // todo: 引入rusage之后,更正以下权限校验代码中,rusage的大小
284bf4a4899SLoGin                 Self::wait4(pid.into(), wstatus, options, rusage)
285ab5c8ca4Slogin             }
286ab5c8ca4Slogin 
287ab5c8ca4Slogin             SYS_EXIT => {
288ab5c8ca4Slogin                 let exit_code = args[0];
289ab5c8ca4Slogin                 Self::exit(exit_code)
290ab5c8ca4Slogin             }
2914fda81ceSLoGin             #[cfg(target_arch = "x86_64")]
292ab5c8ca4Slogin             SYS_MKDIR => {
293ab5c8ca4Slogin                 let path_ptr = args[0] as *const c_char;
294ab5c8ca4Slogin                 let mode = args[1];
295be63f3b2Shoumkh                 let virt_path_ptr = VirtAddr::new(path_ptr as usize);
296ab5c8ca4Slogin                 let security_check = || {
297ab5c8ca4Slogin                     if path_ptr.is_null()
2981496ba7bSLoGin                         || (frame.from_user()
2991496ba7bSLoGin                             && verify_area(virt_path_ptr, PAGE_2M_SIZE as usize).is_err())
300ab5c8ca4Slogin                     {
301ab5c8ca4Slogin                         return Err(SystemError::EINVAL);
302ab5c8ca4Slogin                     }
303ab5c8ca4Slogin                     let path: &CStr = unsafe { CStr::from_ptr(path_ptr) };
304ab5c8ca4Slogin                     let path: &str = path.to_str().map_err(|_| SystemError::EINVAL)?.trim();
305ab5c8ca4Slogin 
306ab5c8ca4Slogin                     if path == "" {
307ab5c8ca4Slogin                         return Err(SystemError::EINVAL);
308ab5c8ca4Slogin                     }
309ab5c8ca4Slogin                     return Ok(path);
310ab5c8ca4Slogin                 };
311ab5c8ca4Slogin 
312ab5c8ca4Slogin                 let path = security_check();
313ab5c8ca4Slogin                 if path.is_err() {
314ab5c8ca4Slogin                     Err(path.unwrap_err())
315ab5c8ca4Slogin                 } else {
316ab5c8ca4Slogin                     Self::mkdir(path.unwrap(), mode)
317ab5c8ca4Slogin                 }
318ab5c8ca4Slogin             }
319ab5c8ca4Slogin 
320ab5c8ca4Slogin             SYS_NANOSLEEP => {
321ab5c8ca4Slogin                 let req = args[0] as *const TimeSpec;
322ab5c8ca4Slogin                 let rem = args[1] as *mut TimeSpec;
323be63f3b2Shoumkh                 let virt_req = VirtAddr::new(req as usize);
324be63f3b2Shoumkh                 let virt_rem = VirtAddr::new(rem as usize);
3251496ba7bSLoGin                 if frame.from_user()
326be63f3b2Shoumkh                     && (verify_area(virt_req, core::mem::size_of::<TimeSpec>() as usize).is_err()
327be63f3b2Shoumkh                         || verify_area(virt_rem, core::mem::size_of::<TimeSpec>() as usize)
328be63f3b2Shoumkh                             .is_err())
329ab5c8ca4Slogin                 {
330ab5c8ca4Slogin                     Err(SystemError::EFAULT)
331ab5c8ca4Slogin                 } else {
332ab5c8ca4Slogin                     Self::nanosleep(req, rem)
333ab5c8ca4Slogin                 }
334ab5c8ca4Slogin             }
335ab5c8ca4Slogin 
336ab5c8ca4Slogin             SYS_CLOCK => Self::clock(),
3374fda81ceSLoGin             #[cfg(target_arch = "x86_64")]
338ab5c8ca4Slogin             SYS_PIPE => {
33922c9db31Shanjiezhou                 let pipefd: *mut i32 = args[0] as *mut c_int;
3400fb515b0SLoGin                 if pipefd.is_null() {
3410fb515b0SLoGin                     Err(SystemError::EFAULT)
3420fb515b0SLoGin                 } else {
3430fb515b0SLoGin                     Self::pipe2(pipefd, FileMode::empty())
3440fb515b0SLoGin                 }
3450fb515b0SLoGin             }
346*4ad52e57S裕依2439 
3470fb515b0SLoGin             SYS_PIPE2 => {
3480fb515b0SLoGin                 let pipefd: *mut i32 = args[0] as *mut c_int;
34922c9db31Shanjiezhou                 let arg1 = args[1];
35022c9db31Shanjiezhou                 if pipefd.is_null() {
35122c9db31Shanjiezhou                     Err(SystemError::EFAULT)
35222c9db31Shanjiezhou                 } else {
35322c9db31Shanjiezhou                     let flags = FileMode::from_bits_truncate(arg1 as u32);
35422c9db31Shanjiezhou                     Self::pipe2(pipefd, flags)
355ab5c8ca4Slogin                 }
356ab5c8ca4Slogin             }
357ab5c8ca4Slogin 
3584fda81ceSLoGin             SYS_UNLINKAT => {
359ab5c8ca4Slogin                 let dirfd = args[0] as i32;
360ab5c8ca4Slogin                 let pathname = args[1] as *const c_char;
361ab5c8ca4Slogin                 let flags = args[2] as u32;
362be63f3b2Shoumkh                 let virt_pathname = VirtAddr::new(pathname as usize);
3631496ba7bSLoGin                 if frame.from_user() && verify_area(virt_pathname, PAGE_4K_SIZE as usize).is_err() {
364ab5c8ca4Slogin                     Err(SystemError::EFAULT)
365ab5c8ca4Slogin                 } else if pathname.is_null() {
366ab5c8ca4Slogin                     Err(SystemError::EFAULT)
367ab5c8ca4Slogin                 } else {
368ab5c8ca4Slogin                     let get_path = || {
369ab5c8ca4Slogin                         let pathname: &CStr = unsafe { CStr::from_ptr(pathname) };
370ab5c8ca4Slogin 
371ab5c8ca4Slogin                         let pathname: &str = pathname.to_str().map_err(|_| SystemError::EINVAL)?;
372ab5c8ca4Slogin                         if pathname.len() >= MAX_PATHLEN {
373ab5c8ca4Slogin                             return Err(SystemError::ENAMETOOLONG);
374ab5c8ca4Slogin                         }
375ab5c8ca4Slogin                         return Ok(pathname.trim());
376ab5c8ca4Slogin                     };
377ab5c8ca4Slogin                     let pathname = get_path();
378ab5c8ca4Slogin                     if pathname.is_err() {
379ab5c8ca4Slogin                         Err(pathname.unwrap_err())
380ab5c8ca4Slogin                     } else {
3816d81180bSLoGin                         // kdebug!("sys unlinkat: dirfd: {}, pathname: {}", dirfd, pathname.as_ref().unwrap());
382ab5c8ca4Slogin                         Self::unlinkat(dirfd, pathname.unwrap(), flags)
383ab5c8ca4Slogin                     }
384ab5c8ca4Slogin                 }
385ab5c8ca4Slogin             }
386bf4a4899SLoGin 
3874fda81ceSLoGin             #[cfg(target_arch = "x86_64")]
388bf4a4899SLoGin             SYS_UNLINK => {
389bf4a4899SLoGin                 let pathname = args[0] as *const u8;
390bf4a4899SLoGin                 Self::unlink(pathname)
391bf4a4899SLoGin             }
392ab5c8ca4Slogin             SYS_KILL => {
3931496ba7bSLoGin                 let pid = Pid::new(args[0]);
394ab5c8ca4Slogin                 let sig = args[1] as c_int;
3953c82aa56SChiichen                 // kdebug!("KILL SYSCALL RECEIVED");
396ab5c8ca4Slogin                 Self::kill(pid, sig)
397ab5c8ca4Slogin             }
398ab5c8ca4Slogin 
3994fda81ceSLoGin             SYS_RT_SIGACTION => {
400ab5c8ca4Slogin                 let sig = args[0] as c_int;
401ab5c8ca4Slogin                 let act = args[1];
402ab5c8ca4Slogin                 let old_act = args[2];
4031496ba7bSLoGin                 Self::sigaction(sig, act, old_act, frame.from_user())
404ab5c8ca4Slogin             }
405ab5c8ca4Slogin 
4061496ba7bSLoGin             SYS_GETPID => Self::getpid().map(|pid| pid.into()),
407ab5c8ca4Slogin 
4081496ba7bSLoGin             SYS_SCHED => Self::sched(frame.from_user()),
409ab5c8ca4Slogin             SYS_DUP => {
410ab5c8ca4Slogin                 let oldfd: i32 = args[0] as c_int;
411ab5c8ca4Slogin                 Self::dup(oldfd)
412ab5c8ca4Slogin             }
4134fda81ceSLoGin 
4144fda81ceSLoGin             #[cfg(target_arch = "x86_64")]
415ab5c8ca4Slogin             SYS_DUP2 => {
416ab5c8ca4Slogin                 let oldfd: i32 = args[0] as c_int;
417ab5c8ca4Slogin                 let newfd: i32 = args[1] as c_int;
418ab5c8ca4Slogin                 Self::dup2(oldfd, newfd)
419ab5c8ca4Slogin             }
420ab5c8ca4Slogin 
421ab5c8ca4Slogin             SYS_SOCKET => Self::socket(args[0], args[1], args[2]),
422ab5c8ca4Slogin             SYS_SETSOCKOPT => {
423ab5c8ca4Slogin                 let optval = args[3] as *const u8;
424ab5c8ca4Slogin                 let optlen = args[4] as usize;
425be63f3b2Shoumkh                 let virt_optval = VirtAddr::new(optval as usize);
426ab5c8ca4Slogin                 // 验证optval的地址是否合法
427be63f3b2Shoumkh                 if verify_area(virt_optval, optlen as usize).is_err() {
428ab5c8ca4Slogin                     // 地址空间超出了用户空间的范围,不合法
429ab5c8ca4Slogin                     Err(SystemError::EFAULT)
430ab5c8ca4Slogin                 } else {
431ab5c8ca4Slogin                     let data: &[u8] = unsafe { core::slice::from_raw_parts(optval, optlen) };
432ab5c8ca4Slogin                     Self::setsockopt(args[0], args[1], args[2], data)
433ab5c8ca4Slogin                 }
434ab5c8ca4Slogin             }
435ab5c8ca4Slogin             SYS_GETSOCKOPT => {
436ab5c8ca4Slogin                 let optval = args[3] as *mut u8;
437ab5c8ca4Slogin                 let optlen = args[4] as *mut usize;
438be63f3b2Shoumkh                 let virt_optval = VirtAddr::new(optval as usize);
439be63f3b2Shoumkh                 let virt_optlen = VirtAddr::new(optlen as usize);
440ab5c8ca4Slogin                 let security_check = || {
441ab5c8ca4Slogin                     // 验证optval的地址是否合法
442be63f3b2Shoumkh                     if verify_area(virt_optval, PAGE_4K_SIZE as usize).is_err() {
443ab5c8ca4Slogin                         // 地址空间超出了用户空间的范围,不合法
444ab5c8ca4Slogin                         return Err(SystemError::EFAULT);
445ab5c8ca4Slogin                     }
446ab5c8ca4Slogin 
447ab5c8ca4Slogin                     // 验证optlen的地址是否合法
448be63f3b2Shoumkh                     if verify_area(virt_optlen, core::mem::size_of::<u32>() as usize).is_err() {
449ab5c8ca4Slogin                         // 地址空间超出了用户空间的范围,不合法
450ab5c8ca4Slogin                         return Err(SystemError::EFAULT);
451ab5c8ca4Slogin                     }
452ab5c8ca4Slogin                     return Ok(());
453ab5c8ca4Slogin                 };
454ab5c8ca4Slogin                 let r = security_check();
455ab5c8ca4Slogin                 if r.is_err() {
456ab5c8ca4Slogin                     Err(r.unwrap_err())
457ab5c8ca4Slogin                 } else {
458ab5c8ca4Slogin                     Self::getsockopt(args[0], args[1], args[2], optval, optlen as *mut u32)
459ab5c8ca4Slogin                 }
460ab5c8ca4Slogin             }
461ab5c8ca4Slogin 
462ab5c8ca4Slogin             SYS_CONNECT => {
463ab5c8ca4Slogin                 let addr = args[1] as *const SockAddr;
464ab5c8ca4Slogin                 let addrlen = args[2] as usize;
465be63f3b2Shoumkh                 let virt_addr = VirtAddr::new(addr as usize);
466ab5c8ca4Slogin                 // 验证addr的地址是否合法
467be63f3b2Shoumkh                 if verify_area(virt_addr, addrlen as usize).is_err() {
468ab5c8ca4Slogin                     // 地址空间超出了用户空间的范围,不合法
469ab5c8ca4Slogin                     Err(SystemError::EFAULT)
470ab5c8ca4Slogin                 } else {
471ab5c8ca4Slogin                     Self::connect(args[0], addr, addrlen)
472ab5c8ca4Slogin                 }
473ab5c8ca4Slogin             }
474ab5c8ca4Slogin             SYS_BIND => {
475ab5c8ca4Slogin                 let addr = args[1] as *const SockAddr;
476ab5c8ca4Slogin                 let addrlen = args[2] as usize;
477be63f3b2Shoumkh                 let virt_addr = VirtAddr::new(addr as usize);
478ab5c8ca4Slogin                 // 验证addr的地址是否合法
479be63f3b2Shoumkh                 if verify_area(virt_addr, addrlen as usize).is_err() {
480ab5c8ca4Slogin                     // 地址空间超出了用户空间的范围,不合法
481ab5c8ca4Slogin                     Err(SystemError::EFAULT)
482ab5c8ca4Slogin                 } else {
483ab5c8ca4Slogin                     Self::bind(args[0], addr, addrlen)
484ab5c8ca4Slogin                 }
485ab5c8ca4Slogin             }
486ab5c8ca4Slogin 
487ab5c8ca4Slogin             SYS_SENDTO => {
488ab5c8ca4Slogin                 let buf = args[1] as *const u8;
489ab5c8ca4Slogin                 let len = args[2] as usize;
490ab5c8ca4Slogin                 let flags = args[3] as u32;
491ab5c8ca4Slogin                 let addr = args[4] as *const SockAddr;
492ab5c8ca4Slogin                 let addrlen = args[5] as usize;
493be63f3b2Shoumkh                 let virt_buf = VirtAddr::new(buf as usize);
494be63f3b2Shoumkh                 let virt_addr = VirtAddr::new(addr as usize);
495ab5c8ca4Slogin                 // 验证buf的地址是否合法
496be63f3b2Shoumkh                 if verify_area(virt_buf, len as usize).is_err() {
497ab5c8ca4Slogin                     // 地址空间超出了用户空间的范围,不合法
498ab5c8ca4Slogin                     Err(SystemError::EFAULT)
499be63f3b2Shoumkh                 } else if verify_area(virt_addr, addrlen as usize).is_err() {
500ab5c8ca4Slogin                     // 地址空间超出了用户空间的范围,不合法
501ab5c8ca4Slogin                     Err(SystemError::EFAULT)
502ab5c8ca4Slogin                 } else {
503ab5c8ca4Slogin                     let data: &[u8] = unsafe { core::slice::from_raw_parts(buf, len) };
504ab5c8ca4Slogin                     Self::sendto(args[0], data, flags, addr, addrlen)
505ab5c8ca4Slogin                 }
506ab5c8ca4Slogin             }
507ab5c8ca4Slogin 
508ab5c8ca4Slogin             SYS_RECVFROM => {
509ab5c8ca4Slogin                 let buf = args[1] as *mut u8;
510ab5c8ca4Slogin                 let len = args[2] as usize;
511ab5c8ca4Slogin                 let flags = args[3] as u32;
512ab5c8ca4Slogin                 let addr = args[4] as *mut SockAddr;
513ab5c8ca4Slogin                 let addrlen = args[5] as *mut usize;
514be63f3b2Shoumkh                 let virt_buf = VirtAddr::new(buf as usize);
515be63f3b2Shoumkh                 let virt_addrlen = VirtAddr::new(addrlen as usize);
516be63f3b2Shoumkh                 let virt_addr = VirtAddr::new(addr as usize);
517ab5c8ca4Slogin                 let security_check = || {
518ab5c8ca4Slogin                     // 验证buf的地址是否合法
519be63f3b2Shoumkh                     if verify_area(virt_buf, len as usize).is_err() {
520ab5c8ca4Slogin                         // 地址空间超出了用户空间的范围,不合法
521ab5c8ca4Slogin                         return Err(SystemError::EFAULT);
522ab5c8ca4Slogin                     }
523ab5c8ca4Slogin 
524ab5c8ca4Slogin                     // 验证addrlen的地址是否合法
525be63f3b2Shoumkh                     if verify_area(virt_addrlen, core::mem::size_of::<u32>() as usize).is_err() {
526ab5c8ca4Slogin                         // 地址空间超出了用户空间的范围,不合法
527ab5c8ca4Slogin                         return Err(SystemError::EFAULT);
528ab5c8ca4Slogin                     }
529ab5c8ca4Slogin 
530be63f3b2Shoumkh                     if verify_area(virt_addr, core::mem::size_of::<SockAddr>() as usize).is_err() {
531ab5c8ca4Slogin                         // 地址空间超出了用户空间的范围,不合法
532ab5c8ca4Slogin                         return Err(SystemError::EFAULT);
533ab5c8ca4Slogin                     }
534ab5c8ca4Slogin                     return Ok(());
535ab5c8ca4Slogin                 };
536ab5c8ca4Slogin                 let r = security_check();
537ab5c8ca4Slogin                 if r.is_err() {
538ab5c8ca4Slogin                     Err(r.unwrap_err())
539ab5c8ca4Slogin                 } else {
540ab5c8ca4Slogin                     let buf = unsafe { core::slice::from_raw_parts_mut(buf, len) };
541ab5c8ca4Slogin                     Self::recvfrom(args[0], buf, flags, addr, addrlen as *mut u32)
542ab5c8ca4Slogin                 }
543ab5c8ca4Slogin             }
544ab5c8ca4Slogin 
545ab5c8ca4Slogin             SYS_RECVMSG => {
546*4ad52e57S裕依2439                 let msg = args[1] as *mut MsgHdr;
547ab5c8ca4Slogin                 let flags = args[2] as u32;
548*4ad52e57S裕依2439 
549*4ad52e57S裕依2439                 let mut user_buffer_writer =
550*4ad52e57S裕依2439                     UserBufferWriter::new(msg, core::mem::size_of::<MsgHdr>(), frame.from_user())?;
551*4ad52e57S裕依2439                 let buffer = user_buffer_writer.buffer::<MsgHdr>(0)?;
552*4ad52e57S裕依2439 
5534537ffb7SChiichen                 let msg = &mut buffer[0];
554ab5c8ca4Slogin                 Self::recvmsg(args[0], msg, flags)
555ab5c8ca4Slogin             }
556ab5c8ca4Slogin 
557ab5c8ca4Slogin             SYS_LISTEN => Self::listen(args[0], args[1]),
558ab5c8ca4Slogin             SYS_SHUTDOWN => Self::shutdown(args[0], args[1]),
559ab5c8ca4Slogin             SYS_ACCEPT => Self::accept(args[0], args[1] as *mut SockAddr, args[2] as *mut u32),
560c47fe904SLoGin             SYS_ACCEPT4 => Self::accept4(
561c47fe904SLoGin                 args[0],
562c47fe904SLoGin                 args[1] as *mut SockAddr,
563c47fe904SLoGin                 args[2] as *mut u32,
564c47fe904SLoGin                 args[3] as u32,
565c47fe904SLoGin             ),
566ab5c8ca4Slogin             SYS_GETSOCKNAME => {
567ab5c8ca4Slogin                 Self::getsockname(args[0], args[1] as *mut SockAddr, args[2] as *mut u32)
568ab5c8ca4Slogin             }
569ab5c8ca4Slogin             SYS_GETPEERNAME => {
570ab5c8ca4Slogin                 Self::getpeername(args[0], args[1] as *mut SockAddr, args[2] as *mut u32)
571ab5c8ca4Slogin             }
57236fd0130Shoumkh             SYS_GETTIMEOFDAY => {
57336fd0130Shoumkh                 let timeval = args[0] as *mut PosixTimeval;
57440fe15e0SLoGin                 let timezone_ptr = args[1] as *mut PosixTimeZone;
57540fe15e0SLoGin                 Self::gettimeofday(timeval, timezone_ptr)
5764537ffb7SChiichen             }
57740fe15e0SLoGin             SYS_MMAP => {
57840fe15e0SLoGin                 let len = page_align_up(args[1]);
579be63f3b2Shoumkh                 let virt_addr = VirtAddr::new(args[0] as usize);
580be63f3b2Shoumkh                 if verify_area(virt_addr, len as usize).is_err() {
58140fe15e0SLoGin                     Err(SystemError::EFAULT)
58240fe15e0SLoGin                 } else {
58340fe15e0SLoGin                     Self::mmap(
58440fe15e0SLoGin                         VirtAddr::new(args[0]),
58540fe15e0SLoGin                         len,
58640fe15e0SLoGin                         args[2],
58740fe15e0SLoGin                         args[3],
58840fe15e0SLoGin                         args[4] as i32,
58940fe15e0SLoGin                         args[5],
59040fe15e0SLoGin                     )
59140fe15e0SLoGin                 }
59240fe15e0SLoGin             }
59340fe15e0SLoGin             SYS_MUNMAP => {
59440fe15e0SLoGin                 let addr = args[0];
59540fe15e0SLoGin                 let len = page_align_up(args[1]);
596971462beSGnoCiYeH                 if addr & (MMArch::PAGE_SIZE - 1) != 0 {
59740fe15e0SLoGin                     // The addr argument is not a multiple of the page size
59840fe15e0SLoGin                     Err(SystemError::EINVAL)
59940fe15e0SLoGin                 } else {
60040fe15e0SLoGin                     Self::munmap(VirtAddr::new(addr), len)
60140fe15e0SLoGin                 }
60240fe15e0SLoGin             }
60340fe15e0SLoGin             SYS_MPROTECT => {
60440fe15e0SLoGin                 let addr = args[0];
60540fe15e0SLoGin                 let len = page_align_up(args[1]);
606971462beSGnoCiYeH                 if addr & (MMArch::PAGE_SIZE - 1) != 0 {
60740fe15e0SLoGin                     // The addr argument is not a multiple of the page size
60840fe15e0SLoGin                     Err(SystemError::EINVAL)
60940fe15e0SLoGin                 } else {
61040fe15e0SLoGin                     Self::mprotect(VirtAddr::new(addr), len, args[2])
61140fe15e0SLoGin                 }
61240fe15e0SLoGin             }
61340fe15e0SLoGin 
6141496ba7bSLoGin             SYS_GETCWD => {
6151496ba7bSLoGin                 let buf = args[0] as *mut u8;
6161496ba7bSLoGin                 let size = args[1] as usize;
6171496ba7bSLoGin                 let security_check = || {
6181496ba7bSLoGin                     verify_area(VirtAddr::new(buf as usize), size)?;
6191496ba7bSLoGin                     return Ok(());
6201496ba7bSLoGin                 };
6211496ba7bSLoGin                 let r = security_check();
6221496ba7bSLoGin                 if r.is_err() {
6231496ba7bSLoGin                     Err(r.unwrap_err())
6241496ba7bSLoGin                 } else {
6251496ba7bSLoGin                     let buf = unsafe { core::slice::from_raw_parts_mut(buf, size) };
6261496ba7bSLoGin                     Self::getcwd(buf).map(|ptr| ptr.data())
6271496ba7bSLoGin                 }
6281496ba7bSLoGin             }
6291496ba7bSLoGin 
6301496ba7bSLoGin             SYS_GETPGID => Self::getpgid(Pid::new(args[0])).map(|pid| pid.into()),
6311496ba7bSLoGin 
6321496ba7bSLoGin             SYS_GETPPID => Self::getppid().map(|pid| pid.into()),
633be63f3b2Shoumkh             SYS_FSTAT => {
634be63f3b2Shoumkh                 let fd = args[0] as i32;
635be63f3b2Shoumkh                 let kstat = args[1] as *mut PosixKstat;
636be63f3b2Shoumkh                 let vaddr = VirtAddr::new(kstat as usize);
637be63f3b2Shoumkh                 // FIXME 由于c中的verify_area与rust中的verify_area重名,所以在引入时加了前缀区分
638be63f3b2Shoumkh                 // TODO 应该将用了c版本的verify_area都改为rust的verify_area
639be63f3b2Shoumkh                 match verify_area(vaddr, core::mem::size_of::<PosixKstat>()) {
640be63f3b2Shoumkh                     Ok(_) => Self::fstat(fd, kstat),
641be63f3b2Shoumkh                     Err(e) => Err(e),
642be63f3b2Shoumkh                 }
643be63f3b2Shoumkh             }
644be63f3b2Shoumkh 
6456d81180bSLoGin             SYS_FCNTL => {
6466d81180bSLoGin                 let fd = args[0] as i32;
6476d81180bSLoGin                 let cmd: Option<FcntlCommand> =
6486d81180bSLoGin                     <FcntlCommand as FromPrimitive>::from_u32(args[1] as u32);
6496d81180bSLoGin                 let arg = args[2] as i32;
6506d81180bSLoGin                 let res = if let Some(cmd) = cmd {
6516d81180bSLoGin                     Self::fcntl(fd, cmd, arg)
6526d81180bSLoGin                 } else {
6536d81180bSLoGin                     Err(SystemError::EINVAL)
6546d81180bSLoGin                 };
6556d81180bSLoGin 
6566d81180bSLoGin                 // kdebug!("FCNTL: fd: {}, cmd: {:?}, arg: {}, res: {:?}", fd, cmd, arg, res);
6576d81180bSLoGin                 res
6586d81180bSLoGin             }
6596d81180bSLoGin 
6606d81180bSLoGin             SYS_FTRUNCATE => {
6616d81180bSLoGin                 let fd = args[0] as i32;
6626d81180bSLoGin                 let len = args[1] as usize;
6636d81180bSLoGin                 let res = Self::ftruncate(fd, len);
6646d81180bSLoGin                 // kdebug!("FTRUNCATE: fd: {}, len: {}, res: {:?}", fd, len, res);
6656d81180bSLoGin                 res
6666d81180bSLoGin             }
6676d81180bSLoGin 
6684fda81ceSLoGin             #[cfg(target_arch = "x86_64")]
6692dbef785SGnoCiYeH             SYS_MKNOD => {
6702dbef785SGnoCiYeH                 let path = args[0];
6712dbef785SGnoCiYeH                 let flags = args[1];
6722dbef785SGnoCiYeH                 let dev_t = args[2];
6732dbef785SGnoCiYeH                 let flags: ModeType = ModeType::from_bits_truncate(flags as u32);
674c566df45SLoGin                 Self::mknod(path as *const i8, flags, DeviceNumber::from(dev_t as u32))
6752dbef785SGnoCiYeH             }
6762dbef785SGnoCiYeH 
677971462beSGnoCiYeH             SYS_CLONE => {
678971462beSGnoCiYeH                 let parent_tid = VirtAddr::new(args[2]);
679971462beSGnoCiYeH                 let child_tid = VirtAddr::new(args[3]);
680971462beSGnoCiYeH 
681971462beSGnoCiYeH                 // 地址校验
682971462beSGnoCiYeH                 verify_area(parent_tid, core::mem::size_of::<i32>())?;
683971462beSGnoCiYeH                 verify_area(child_tid, core::mem::size_of::<i32>())?;
684971462beSGnoCiYeH 
685971462beSGnoCiYeH                 let mut clone_args = KernelCloneArgs::new();
686971462beSGnoCiYeH                 clone_args.flags = CloneFlags::from_bits_truncate(args[0] as u64);
687971462beSGnoCiYeH                 clone_args.stack = args[1];
688971462beSGnoCiYeH                 clone_args.parent_tid = parent_tid;
689971462beSGnoCiYeH                 clone_args.child_tid = child_tid;
690971462beSGnoCiYeH                 clone_args.tls = args[4];
691971462beSGnoCiYeH                 Self::clone(frame, clone_args)
692971462beSGnoCiYeH             }
693971462beSGnoCiYeH 
694971462beSGnoCiYeH             SYS_FUTEX => {
695971462beSGnoCiYeH                 let uaddr = VirtAddr::new(args[0]);
696971462beSGnoCiYeH                 let operation = FutexFlag::from_bits(args[1] as u32).ok_or(SystemError::ENOSYS)?;
697971462beSGnoCiYeH                 let val = args[2] as u32;
698971462beSGnoCiYeH                 let utime = args[3];
699971462beSGnoCiYeH                 let uaddr2 = VirtAddr::new(args[4]);
700971462beSGnoCiYeH                 let val3 = args[5] as u32;
701971462beSGnoCiYeH 
702971462beSGnoCiYeH                 verify_area(uaddr, core::mem::size_of::<u32>())?;
703971462beSGnoCiYeH                 verify_area(uaddr2, core::mem::size_of::<u32>())?;
704971462beSGnoCiYeH 
705971462beSGnoCiYeH                 let mut timespec = None;
706971462beSGnoCiYeH                 if utime != 0 && operation.contains(FutexFlag::FLAGS_HAS_TIMEOUT) {
707971462beSGnoCiYeH                     let reader = UserBufferReader::new(
708971462beSGnoCiYeH                         utime as *const TimeSpec,
709971462beSGnoCiYeH                         core::mem::size_of::<TimeSpec>(),
710971462beSGnoCiYeH                         true,
711971462beSGnoCiYeH                     )?;
712971462beSGnoCiYeH 
713971462beSGnoCiYeH                     timespec = Some(reader.read_one_from_user::<TimeSpec>(0)?.clone());
714971462beSGnoCiYeH                 }
715971462beSGnoCiYeH 
716971462beSGnoCiYeH                 Self::do_futex(uaddr, operation, val, timespec, uaddr2, utime as u32, val3)
717971462beSGnoCiYeH             }
718971462beSGnoCiYeH 
719bf4a4899SLoGin             SYS_READV => Self::readv(args[0] as i32, args[1], args[2]),
720971462beSGnoCiYeH             SYS_WRITEV => Self::writev(args[0] as i32, args[1], args[2]),
721971462beSGnoCiYeH 
7224fda81ceSLoGin             SYS_SET_TID_ADDRESS => Self::set_tid_address(args[0]),
723971462beSGnoCiYeH 
7244fda81ceSLoGin             #[cfg(target_arch = "x86_64")]
7254fda81ceSLoGin             SYS_LSTAT => {
726971462beSGnoCiYeH                 let path: &CStr = unsafe { CStr::from_ptr(args[0] as *const c_char) };
727971462beSGnoCiYeH                 let path: Result<&str, core::str::Utf8Error> = path.to_str();
728971462beSGnoCiYeH                 let res = if path.is_err() {
729971462beSGnoCiYeH                     Err(SystemError::EINVAL)
730971462beSGnoCiYeH                 } else {
731971462beSGnoCiYeH                     let path: &str = path.unwrap();
732971462beSGnoCiYeH                     let kstat = args[1] as *mut PosixKstat;
733971462beSGnoCiYeH                     let vaddr = VirtAddr::new(kstat as usize);
734971462beSGnoCiYeH                     match verify_area(vaddr, core::mem::size_of::<PosixKstat>()) {
7354fda81ceSLoGin                         Ok(_) => Self::lstat(path, kstat),
7364fda81ceSLoGin                         Err(e) => Err(e),
7374fda81ceSLoGin                     }
7384fda81ceSLoGin                 };
7394fda81ceSLoGin 
7404fda81ceSLoGin                 res
7414fda81ceSLoGin             }
7424fda81ceSLoGin 
7434fda81ceSLoGin             #[cfg(target_arch = "x86_64")]
7444fda81ceSLoGin             SYS_STAT => {
7454fda81ceSLoGin                 let path: &CStr = unsafe { CStr::from_ptr(args[0] as *const c_char) };
7464fda81ceSLoGin                 let path: Result<&str, core::str::Utf8Error> = path.to_str();
7474fda81ceSLoGin                 let res = if path.is_err() {
7484fda81ceSLoGin                     Err(SystemError::EINVAL)
749bf4a4899SLoGin                 } else {
7504fda81ceSLoGin                     let path: &str = path.unwrap();
7514fda81ceSLoGin                     let kstat = args[1] as *mut PosixKstat;
7524fda81ceSLoGin                     let vaddr = VirtAddr::new(kstat as usize);
7534fda81ceSLoGin                     match verify_area(vaddr, core::mem::size_of::<PosixKstat>()) {
7544fda81ceSLoGin                         Ok(_) => Self::stat(path, kstat),
755971462beSGnoCiYeH                         Err(e) => Err(e),
756971462beSGnoCiYeH                     }
757971462beSGnoCiYeH                 };
758971462beSGnoCiYeH 
759971462beSGnoCiYeH                 res
760971462beSGnoCiYeH             }
761971462beSGnoCiYeH 
76240609970SGnoCiYeH             SYS_EPOLL_CREATE => Self::epoll_create(args[0] as i32),
76340609970SGnoCiYeH             SYS_EPOLL_CREATE1 => Self::epoll_create1(args[0]),
76440609970SGnoCiYeH 
76540609970SGnoCiYeH             SYS_EPOLL_CTL => Self::epoll_ctl(
76640609970SGnoCiYeH                 args[0] as i32,
76740609970SGnoCiYeH                 args[1],
76840609970SGnoCiYeH                 args[2] as i32,
76940609970SGnoCiYeH                 VirtAddr::new(args[3]),
77040609970SGnoCiYeH             ),
77140609970SGnoCiYeH 
77240609970SGnoCiYeH             SYS_EPOLL_WAIT => Self::epoll_wait(
77340609970SGnoCiYeH                 args[0] as i32,
77440609970SGnoCiYeH                 VirtAddr::new(args[1]),
77540609970SGnoCiYeH                 args[2] as i32,
77640609970SGnoCiYeH                 args[3] as i32,
77740609970SGnoCiYeH             ),
77840609970SGnoCiYeH 
77940609970SGnoCiYeH             SYS_EPOLL_PWAIT => {
78040609970SGnoCiYeH                 let epfd = args[0] as i32;
78140609970SGnoCiYeH                 let epoll_event = VirtAddr::new(args[1]);
78240609970SGnoCiYeH                 let max_events = args[2] as i32;
78340609970SGnoCiYeH                 let timespec = args[3] as i32;
78440609970SGnoCiYeH                 let sigmask_addr = args[4] as *mut SigSet;
78540609970SGnoCiYeH 
78640609970SGnoCiYeH                 if sigmask_addr.is_null() {
78740609970SGnoCiYeH                     return Self::epoll_wait(epfd, epoll_event, max_events, timespec);
78840609970SGnoCiYeH                 }
78940609970SGnoCiYeH                 let sigmask_reader =
79040609970SGnoCiYeH                     UserBufferReader::new(sigmask_addr, core::mem::size_of::<SigSet>(), true)?;
79140609970SGnoCiYeH                 let mut sigmask = sigmask_reader.read_one_from_user::<SigSet>(0)?.clone();
79240609970SGnoCiYeH 
79340609970SGnoCiYeH                 Self::epoll_pwait(
79440609970SGnoCiYeH                     args[0] as i32,
79540609970SGnoCiYeH                     VirtAddr::new(args[1]),
79640609970SGnoCiYeH                     args[2] as i32,
79740609970SGnoCiYeH                     args[3] as i32,
79840609970SGnoCiYeH                     &mut sigmask,
79940609970SGnoCiYeH                 )
80040609970SGnoCiYeH             }
80140609970SGnoCiYeH 
802971462beSGnoCiYeH             // 目前为了适配musl-libc,以下系统调用先这样写着
8034fda81ceSLoGin             SYS_GETRANDOM => {
804971462beSGnoCiYeH                 let flags = GRandFlags::from_bits(args[2] as u8).ok_or(SystemError::EINVAL)?;
805971462beSGnoCiYeH                 Self::get_random(args[0] as *mut u8, args[1], flags)
806971462beSGnoCiYeH             }
807971462beSGnoCiYeH 
8084fda81ceSLoGin             SYS_SOCKETPAIR => {
809*4ad52e57S裕依2439                 let mut user_buffer_writer = UserBufferWriter::new(
810*4ad52e57S裕依2439                     args[3] as *mut c_int,
811*4ad52e57S裕依2439                     core::mem::size_of::<[c_int; 2]>(),
812*4ad52e57S裕依2439                     frame.from_user(),
813*4ad52e57S裕依2439                 )?;
814*4ad52e57S裕依2439                 let fds = user_buffer_writer.buffer::<i32>(0)?;
815*4ad52e57S裕依2439                 Self::socketpair(args[0], args[1], args[2], fds)
816971462beSGnoCiYeH             }
817971462beSGnoCiYeH 
8184fda81ceSLoGin             #[cfg(target_arch = "x86_64")]
819971462beSGnoCiYeH             SYS_POLL => {
820971462beSGnoCiYeH                 kwarn!("SYS_POLL has not yet been implemented");
821971462beSGnoCiYeH                 Ok(0)
822971462beSGnoCiYeH             }
823971462beSGnoCiYeH 
824971462beSGnoCiYeH             SYS_RT_SIGPROCMASK => {
825971462beSGnoCiYeH                 kwarn!("SYS_RT_SIGPROCMASK has not yet been implemented");
826971462beSGnoCiYeH                 Ok(0)
827971462beSGnoCiYeH             }
828971462beSGnoCiYeH 
829971462beSGnoCiYeH             SYS_TKILL => {
830971462beSGnoCiYeH                 kwarn!("SYS_TKILL has not yet been implemented");
831971462beSGnoCiYeH                 Ok(0)
832971462beSGnoCiYeH             }
833971462beSGnoCiYeH 
834971462beSGnoCiYeH             SYS_SIGALTSTACK => {
835971462beSGnoCiYeH                 kwarn!("SYS_SIGALTSTACK has not yet been implemented");
836971462beSGnoCiYeH                 Ok(0)
837971462beSGnoCiYeH             }
838971462beSGnoCiYeH 
83916033951SGnoCiYeH             SYS_EXIT_GROUP => {
84016033951SGnoCiYeH                 kwarn!("SYS_EXIT_GROUP has not yet been implemented");
84116033951SGnoCiYeH                 Ok(0)
84216033951SGnoCiYeH             }
84316033951SGnoCiYeH 
84404babc3fSMemoryShore             SYS_MADVISE => {
8456c7f966cSMemoryShore                 // 这个太吵了,总是打印,先注释掉
8466c7f966cSMemoryShore                 // kwarn!("SYS_MADVISE has not yet been implemented");
84704babc3fSMemoryShore                 Ok(0)
84804babc3fSMemoryShore             }
849393f6915SLoGin             SYS_GETTID => Self::gettid().map(|tid| tid.into()),
85002e249f3SLoGin             SYS_GETUID => Self::getuid().map(|uid| uid.into()),
8518d72b68dSJomo 
85202e249f3SLoGin             SYS_SYSLOG => {
8538d72b68dSJomo                 let syslog_action_type = args[0] as usize;
8548d72b68dSJomo                 let buf_vaddr = args[1];
8558d72b68dSJomo                 let len = args[2];
8568d72b68dSJomo                 let from_user = frame.from_user();
8578d72b68dSJomo                 let mut user_buffer_writer =
8588d72b68dSJomo                     UserBufferWriter::new(buf_vaddr as *mut u8, len, from_user)?;
8598d72b68dSJomo 
8608d72b68dSJomo                 let user_buf = user_buffer_writer.buffer(0)?;
8618d72b68dSJomo                 let res = Self::do_syslog(syslog_action_type, user_buf, len);
8628d72b68dSJomo                 res
86302e249f3SLoGin             }
8648d72b68dSJomo 
86502e249f3SLoGin             SYS_GETGID => Self::getgid().map(|gid| gid.into()),
86602e249f3SLoGin             SYS_SETUID => {
86702e249f3SLoGin                 kwarn!("SYS_SETUID has not yet been implemented");
86802e249f3SLoGin                 Ok(0)
86902e249f3SLoGin             }
87002e249f3SLoGin             SYS_SETGID => {
87102e249f3SLoGin                 kwarn!("SYS_SETGID has not yet been implemented");
87202e249f3SLoGin                 Ok(0)
87302e249f3SLoGin             }
87402e249f3SLoGin             SYS_GETEUID => Self::geteuid().map(|euid| euid.into()),
87502e249f3SLoGin             SYS_GETEGID => Self::getegid().map(|egid| egid.into()),
876be8cdf4bSLoGin             SYS_GETRUSAGE => {
877be8cdf4bSLoGin                 let who = args[0] as c_int;
878be8cdf4bSLoGin                 let rusage = args[1] as *mut RUsage;
879be8cdf4bSLoGin                 Self::get_rusage(who, rusage)
880be8cdf4bSLoGin             }
88104babc3fSMemoryShore 
8824fda81ceSLoGin             #[cfg(target_arch = "x86_64")]
883709498caSLoGin             SYS_READLINK => {
884709498caSLoGin                 let path = args[0] as *const u8;
885709498caSLoGin                 let buf = args[1] as *mut u8;
886709498caSLoGin                 let bufsiz = args[2] as usize;
887709498caSLoGin                 Self::readlink(path, buf, bufsiz)
888709498caSLoGin             }
889709498caSLoGin 
8904fda81ceSLoGin             SYS_READLINKAT => {
891709498caSLoGin                 let dirfd = args[0] as i32;
892709498caSLoGin                 let pathname = args[1] as *const u8;
893709498caSLoGin                 let buf = args[2] as *mut u8;
894709498caSLoGin                 let bufsiz = args[3] as usize;
895709498caSLoGin                 Self::readlink_at(dirfd, pathname, buf, bufsiz)
896709498caSLoGin             }
897709498caSLoGin 
8980d9b7d92SLoGin             SYS_PRLIMIT64 => {
8990d9b7d92SLoGin                 let pid = args[0];
9000d9b7d92SLoGin                 let pid = Pid::new(pid);
9010d9b7d92SLoGin                 let resource = args[1];
9020d9b7d92SLoGin                 let new_limit = args[2] as *const RLimit64;
9030d9b7d92SLoGin                 let old_limit = args[3] as *mut RLimit64;
9040d9b7d92SLoGin 
9050d9b7d92SLoGin                 Self::prlimit64(pid, resource, new_limit, old_limit)
9060d9b7d92SLoGin             }
9070d9b7d92SLoGin 
9084fda81ceSLoGin             #[cfg(target_arch = "x86_64")]
9099b0abe6dSLoGin             SYS_ACCESS => {
9109b0abe6dSLoGin                 let pathname = args[0] as *const u8;
9119b0abe6dSLoGin                 let mode = args[1] as u32;
9129b0abe6dSLoGin                 Self::access(pathname, mode)
9139b0abe6dSLoGin             }
9149b0abe6dSLoGin 
9159b0abe6dSLoGin             SYS_FACCESSAT => {
9169b0abe6dSLoGin                 let dirfd = args[0] as i32;
9179b0abe6dSLoGin                 let pathname = args[1] as *const u8;
9189b0abe6dSLoGin                 let mode = args[2] as u32;
9199b0abe6dSLoGin                 Self::faccessat2(dirfd, pathname, mode, 0)
9209b0abe6dSLoGin             }
9219b0abe6dSLoGin 
9229b0abe6dSLoGin             SYS_FACCESSAT2 => {
9239b0abe6dSLoGin                 let dirfd = args[0] as i32;
9249b0abe6dSLoGin                 let pathname = args[1] as *const u8;
9259b0abe6dSLoGin                 let mode = args[2] as u32;
9269b0abe6dSLoGin                 let flags = args[3] as u32;
9279b0abe6dSLoGin                 Self::faccessat2(dirfd, pathname, mode, flags)
9289b0abe6dSLoGin             }
9299b0abe6dSLoGin 
930bf4a4899SLoGin             SYS_CLOCK_GETTIME => {
931bf4a4899SLoGin                 let clockid = args[0] as i32;
932bf4a4899SLoGin                 let timespec = args[1] as *mut TimeSpec;
933bf4a4899SLoGin                 Self::clock_gettime(clockid, timespec)
934bf4a4899SLoGin             }
935bf4a4899SLoGin 
936bf4a4899SLoGin             SYS_SYSINFO => {
937bf4a4899SLoGin                 let info = args[0] as *mut SysInfo;
938bf4a4899SLoGin                 Self::sysinfo(info)
939bf4a4899SLoGin             }
940bf4a4899SLoGin 
941bf4a4899SLoGin             SYS_UMASK => {
942bf4a4899SLoGin                 let mask = args[0] as u32;
943bf4a4899SLoGin                 Self::umask(mask)
944bf4a4899SLoGin             }
945bf4a4899SLoGin 
9464fda81ceSLoGin             #[cfg(target_arch = "x86_64")]
947bf4a4899SLoGin             SYS_CHMOD => {
948bf4a4899SLoGin                 let pathname = args[0] as *const u8;
949bf4a4899SLoGin                 let mode = args[1] as u32;
950bf4a4899SLoGin                 Self::chmod(pathname, mode)
951bf4a4899SLoGin             }
952bf4a4899SLoGin             SYS_FCHMOD => {
953bf4a4899SLoGin                 let fd = args[0] as i32;
954bf4a4899SLoGin                 let mode = args[1] as u32;
955bf4a4899SLoGin                 Self::fchmod(fd, mode)
956bf4a4899SLoGin             }
957bf4a4899SLoGin             SYS_FCHMODAT => {
958bf4a4899SLoGin                 let dirfd = args[0] as i32;
959bf4a4899SLoGin                 let pathname = args[1] as *const u8;
960bf4a4899SLoGin                 let mode = args[2] as u32;
961bf4a4899SLoGin                 Self::fchmodat(dirfd, pathname, mode)
962bf4a4899SLoGin             }
963bf4a4899SLoGin 
96402343d0bSLoGin             SYS_SCHED_GETAFFINITY => {
96502343d0bSLoGin                 // todo: 这个系统调用还没有实现
96602343d0bSLoGin 
96702343d0bSLoGin                 Err(SystemError::ENOSYS)
96802343d0bSLoGin             }
96902343d0bSLoGin 
97002343d0bSLoGin             SYS_SCHED_YIELD => Self::sched_yield(),
97102343d0bSLoGin 
972ab5c8ca4Slogin             _ => panic!("Unsupported syscall ID: {}", syscall_num),
973ab5c8ca4Slogin         };
9744fda81ceSLoGin 
975ab5c8ca4Slogin         return r;
976ab5c8ca4Slogin     }
977ab5c8ca4Slogin 
978ab5c8ca4Slogin     pub fn put_string(
979ab5c8ca4Slogin         s: *const u8,
980ab5c8ca4Slogin         front_color: u32,
981ab5c8ca4Slogin         back_color: u32,
982ab5c8ca4Slogin     ) -> Result<usize, SystemError> {
983ab5c8ca4Slogin         return Ok(unsafe { do_put_string(s, front_color, back_color) });
984ab5c8ca4Slogin     }
985ab5c8ca4Slogin 
986ab5c8ca4Slogin     pub fn reboot() -> Result<usize, SystemError> {
9874fda81ceSLoGin         unsafe { cpu_reset() };
988ab5c8ca4Slogin     }
989ab5c8ca4Slogin }
990