xref: /DragonOS/kernel/src/syscall/mod.rs (revision 4cfa009b87c8431a41fab740ffdbd7b008965c9a)
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*4cfa009bSJomo     mm::syscall::MremapFlags,
114ad52e57S裕依2439     net::syscall::MsgHdr,
120d9b7d92SLoGin     process::{
130d9b7d92SLoGin         fork::KernelCloneArgs,
140d9b7d92SLoGin         resource::{RLimit64, RUsage},
150d9b7d92SLoGin     },
16971462beSGnoCiYeH };
1740314b30SXiaoye Zheng 
1891e9d4abSLoGin use num_traits::FromPrimitive;
1991e9d4abSLoGin use system_error::SystemError;
20676b8ef6SMork 
21ab5c8ca4Slogin use crate::{
221496ba7bSLoGin     arch::{cpu::cpu_reset, interrupt::TrapFrame, MMArch},
234fda81ceSLoGin     driver::base::block::SeekFrom,
24ab5c8ca4Slogin     filesystem::vfs::{
256d81180bSLoGin         fcntl::FcntlCommand,
26ab5c8ca4Slogin         file::FileMode,
272dbef785SGnoCiYeH         syscall::{ModeType, PosixKstat, SEEK_CUR, SEEK_END, SEEK_MAX, SEEK_SET},
28ab5c8ca4Slogin         MAX_PATHLEN,
29ab5c8ca4Slogin     },
301496ba7bSLoGin     include::bindings::bindings::{PAGE_2M_SIZE, PAGE_4K_SIZE},
31aa0367d6SLoGin     kinfo,
3240fe15e0SLoGin     libs::align::page_align_up,
33be63f3b2Shoumkh     mm::{verify_area, MemoryManagementArch, VirtAddr},
34ab5c8ca4Slogin     net::syscall::SockAddr,
35971462beSGnoCiYeH     process::{fork::CloneFlags, Pid},
3636fd0130Shoumkh     time::{
3740fe15e0SLoGin         syscall::{PosixTimeZone, PosixTimeval},
3836fd0130Shoumkh         TimeSpec,
3936fd0130Shoumkh     },
40ab5c8ca4Slogin };
41ab5c8ca4Slogin 
42bf4a4899SLoGin use self::{
43bf4a4899SLoGin     misc::SysInfo,
44bf4a4899SLoGin     user_access::{UserBufferReader, UserBufferWriter},
45bf4a4899SLoGin };
464537ffb7SChiichen 
47bf4a4899SLoGin pub mod misc;
4840fe15e0SLoGin pub mod user_access;
4940fe15e0SLoGin 
50ad1d649eSGnoCiYeH // 与linux不一致的调用,在linux基础上累加
51ad1d649eSGnoCiYeH pub const SYS_PUT_STRING: usize = 100000;
52ad1d649eSGnoCiYeH pub const SYS_SBRK: usize = 100001;
53ab5c8ca4Slogin /// todo: 该系统调用与Linux不一致,将来需要删除该系统调用!!! 删的时候记得改C版本的libc
54ad1d649eSGnoCiYeH pub const SYS_CLOCK: usize = 100002;
55ad1d649eSGnoCiYeH pub const SYS_SCHED: usize = 100003;
5640314b30SXiaoye Zheng 
57ab5c8ca4Slogin #[derive(Debug)]
58ab5c8ca4Slogin pub struct Syscall;
59ab5c8ca4Slogin 
60ab5c8ca4Slogin extern "C" {
61ab5c8ca4Slogin     fn do_put_string(s: *const u8, front_color: u32, back_color: u32) -> usize;
62ab5c8ca4Slogin }
63ab5c8ca4Slogin 
64ab5c8ca4Slogin impl Syscall {
65ab5c8ca4Slogin     /// 初始化系统调用
665b59005fSLoGin     #[inline(never)]
67ab5c8ca4Slogin     pub fn init() -> Result<(), SystemError> {
68ab5c8ca4Slogin         static INIT_FLAG: AtomicBool = AtomicBool::new(false);
69ab5c8ca4Slogin         let prev = INIT_FLAG.swap(true, Ordering::SeqCst);
70ab5c8ca4Slogin         if prev {
71ab5c8ca4Slogin             panic!("Cannot initialize syscall more than once!");
72ab5c8ca4Slogin         }
735b59005fSLoGin         kinfo!("Initializing syscall...");
745b59005fSLoGin         let r = crate::arch::syscall::arch_syscall_init();
755b59005fSLoGin         kinfo!("Syscall init successfully!");
765b59005fSLoGin 
775b59005fSLoGin         return r;
78ab5c8ca4Slogin     }
79ab5c8ca4Slogin     /// @brief 系统调用分发器,用于分发系统调用。
80ab5c8ca4Slogin     ///
81ab5c8ca4Slogin     /// 这个函数内,需要根据系统调用号,调用对应的系统调用处理函数。
82ab5c8ca4Slogin     /// 并且,对于用户态传入的指针参数,需要在本函数内进行越界检查,防止访问到内核空间。
834fda81ceSLoGin     #[inline(never)]
849e9ffedfSPlucky923     pub fn handle(
859e9ffedfSPlucky923         syscall_num: usize,
869e9ffedfSPlucky923         args: &[usize],
879e9ffedfSPlucky923         frame: &mut TrapFrame,
889e9ffedfSPlucky923     ) -> Result<usize, SystemError> {
89ab5c8ca4Slogin         let r = match syscall_num {
90ab5c8ca4Slogin             SYS_PUT_STRING => {
91ab5c8ca4Slogin                 Self::put_string(args[0] as *const u8, args[1] as u32, args[2] as u32)
92ab5c8ca4Slogin             }
934fda81ceSLoGin             #[cfg(target_arch = "x86_64")]
94ab5c8ca4Slogin             SYS_OPEN => {
95ab5c8ca4Slogin                 let path: &CStr = unsafe { CStr::from_ptr(args[0] as *const c_char) };
96ab5c8ca4Slogin                 let path: Result<&str, core::str::Utf8Error> = path.to_str();
97ab5c8ca4Slogin                 let res = if path.is_err() {
98ab5c8ca4Slogin                     Err(SystemError::EINVAL)
99ab5c8ca4Slogin                 } else {
100ab5c8ca4Slogin                     let path: &str = path.unwrap();
10134e6d6c8Syuyi2439 
102ab5c8ca4Slogin                     let flags = args[1];
1030fb515b0SLoGin                     let mode = args[2];
1040fb515b0SLoGin 
105ab5c8ca4Slogin                     let open_flags: FileMode = FileMode::from_bits_truncate(flags as u32);
1060fb515b0SLoGin                     let mode = ModeType::from_bits(mode as u32).ok_or(SystemError::EINVAL)?;
1070fb515b0SLoGin                     Self::open(path, open_flags, mode, true)
1080fb515b0SLoGin                 };
1090fb515b0SLoGin                 res
1100fb515b0SLoGin             }
1110fb515b0SLoGin 
1120fb515b0SLoGin             SYS_OPENAT => {
1130fb515b0SLoGin                 let dirfd = args[0] as i32;
1140fb515b0SLoGin                 let path: &CStr = unsafe { CStr::from_ptr(args[1] as *const c_char) };
1150fb515b0SLoGin                 let flags = args[2];
1160fb515b0SLoGin                 let mode = args[3];
1170fb515b0SLoGin 
1180fb515b0SLoGin                 let path: Result<&str, core::str::Utf8Error> = path.to_str();
1190fb515b0SLoGin                 let res = if path.is_err() {
1200fb515b0SLoGin                     Err(SystemError::EINVAL)
1210fb515b0SLoGin                 } else {
1220fb515b0SLoGin                     let path: &str = path.unwrap();
1230fb515b0SLoGin 
1240fb515b0SLoGin                     let open_flags: FileMode =
1250fb515b0SLoGin                         FileMode::from_bits(flags as u32).ok_or(SystemError::EINVAL)?;
1260fb515b0SLoGin                     let mode = ModeType::from_bits(mode as u32).ok_or(SystemError::EINVAL)?;
1270fb515b0SLoGin                     Self::openat(dirfd, path, open_flags, mode, true)
128ab5c8ca4Slogin                 };
129ab5c8ca4Slogin                 res
130ab5c8ca4Slogin             }
131ab5c8ca4Slogin             SYS_CLOSE => {
132ab5c8ca4Slogin                 let fd = args[0];
13327b967a3S裕依                 Self::close(fd)
134ab5c8ca4Slogin             }
135ab5c8ca4Slogin             SYS_READ => {
136ab5c8ca4Slogin                 let fd = args[0] as i32;
137ab5c8ca4Slogin                 let buf_vaddr = args[1];
138ab5c8ca4Slogin                 let len = args[2];
1399e9ffedfSPlucky923                 let from_user = frame.from_user();
1409e9ffedfSPlucky923                 let mut user_buffer_writer =
1419e9ffedfSPlucky923                     UserBufferWriter::new(buf_vaddr as *mut u8, len, from_user)?;
1426d81180bSLoGin 
1439e9ffedfSPlucky923                 let user_buf = user_buffer_writer.buffer(0)?;
14427b967a3S裕依                 Self::read(fd, user_buf)
145ab5c8ca4Slogin             }
146ab5c8ca4Slogin             SYS_WRITE => {
147ab5c8ca4Slogin                 let fd = args[0] as i32;
148ab5c8ca4Slogin                 let buf_vaddr = args[1];
149ab5c8ca4Slogin                 let len = args[2];
1509e9ffedfSPlucky923                 let from_user = frame.from_user();
1519e9ffedfSPlucky923                 let user_buffer_reader =
1529e9ffedfSPlucky923                     UserBufferReader::new(buf_vaddr as *const u8, len, from_user)?;
1536d81180bSLoGin 
1549e9ffedfSPlucky923                 let user_buf = user_buffer_reader.read_from_user(0)?;
15527b967a3S裕依                 Self::write(fd, user_buf)
156ab5c8ca4Slogin             }
157ab5c8ca4Slogin 
158ab5c8ca4Slogin             SYS_LSEEK => {
159ab5c8ca4Slogin                 let fd = args[0] as i32;
160ab5c8ca4Slogin                 let offset = args[1] as i64;
161ab5c8ca4Slogin                 let whence = args[2] as u32;
162ab5c8ca4Slogin 
163ab5c8ca4Slogin                 let w = match whence {
164ab5c8ca4Slogin                     SEEK_SET => Ok(SeekFrom::SeekSet(offset)),
165ab5c8ca4Slogin                     SEEK_CUR => Ok(SeekFrom::SeekCurrent(offset)),
166ab5c8ca4Slogin                     SEEK_END => Ok(SeekFrom::SeekEnd(offset)),
167ab5c8ca4Slogin                     SEEK_MAX => Ok(SeekFrom::SeekEnd(0)),
168ab5c8ca4Slogin                     _ => Err(SystemError::EINVAL),
1699e9ffedfSPlucky923                 }?;
170ab5c8ca4Slogin 
171ab5c8ca4Slogin                 Self::lseek(fd, w)
172ab5c8ca4Slogin             }
17327b967a3S裕依 
17427b967a3S裕依             SYS_PREAD64 => {
17527b967a3S裕依                 let fd = args[0] as i32;
17627b967a3S裕依                 let buf_vaddr = args[1];
17727b967a3S裕依                 let len = args[2];
17827b967a3S裕依                 let offset = args[3];
17927b967a3S裕依 
18027b967a3S裕依                 let mut user_buffer_writer =
18127b967a3S裕依                     UserBufferWriter::new(buf_vaddr as *mut u8, len, frame.from_user())?;
18227b967a3S裕依                 let buf = user_buffer_writer.buffer(0)?;
18327b967a3S裕依                 Self::pread(fd, buf, len, offset)
18427b967a3S裕依             }
18527b967a3S裕依 
18627b967a3S裕依             SYS_PWRITE64 => {
18727b967a3S裕依                 let fd = args[0] as i32;
18827b967a3S裕依                 let buf_vaddr = args[1];
18927b967a3S裕依                 let len = args[2];
19027b967a3S裕依                 let offset = args[3];
19127b967a3S裕依 
19227b967a3S裕依                 let user_buffer_reader =
19327b967a3S裕依                     UserBufferReader::new(buf_vaddr as *const u8, len, frame.from_user())?;
19427b967a3S裕依 
19527b967a3S裕依                 let buf = user_buffer_reader.read_from_user(0)?;
19627b967a3S裕依                 Self::pwrite(fd, buf, len, offset)
19727b967a3S裕依             }
19827b967a3S裕依 
19940314b30SXiaoye Zheng             SYS_IOCTL => {
20040314b30SXiaoye Zheng                 let fd = args[0];
20140314b30SXiaoye Zheng                 let cmd = args[1];
20240314b30SXiaoye Zheng                 let data = args[2];
20340314b30SXiaoye Zheng                 Self::ioctl(fd, cmd as u32, data)
20440314b30SXiaoye Zheng             }
205ab5c8ca4Slogin 
2064fda81ceSLoGin             #[cfg(target_arch = "x86_64")]
2071496ba7bSLoGin             SYS_FORK => Self::fork(frame),
2084fda81ceSLoGin             #[cfg(target_arch = "x86_64")]
2091496ba7bSLoGin             SYS_VFORK => Self::vfork(frame),
2101496ba7bSLoGin 
211ab5c8ca4Slogin             SYS_BRK => {
21240fe15e0SLoGin                 let new_brk = VirtAddr::new(args[0]);
21340fe15e0SLoGin                 Self::brk(new_brk).map(|vaddr| vaddr.data())
214ab5c8ca4Slogin             }
215ab5c8ca4Slogin 
216ab5c8ca4Slogin             SYS_SBRK => {
217ab5c8ca4Slogin                 let increment = args[0] as isize;
2181496ba7bSLoGin                 Self::sbrk(increment).map(|vaddr: VirtAddr| vaddr.data())
219ab5c8ca4Slogin             }
220ab5c8ca4Slogin 
221ab5c8ca4Slogin             SYS_REBOOT => Self::reboot(),
222ab5c8ca4Slogin 
223ab5c8ca4Slogin             SYS_CHDIR => {
224ab5c8ca4Slogin                 // Closure for checking arguments
225ab5c8ca4Slogin                 let chdir_check = |arg0: usize| {
226ab5c8ca4Slogin                     if arg0 == 0 {
227ab5c8ca4Slogin                         return Err(SystemError::EFAULT);
228ab5c8ca4Slogin                     }
229ab5c8ca4Slogin                     let path_ptr = arg0 as *const c_char;
230be63f3b2Shoumkh                     let virt_addr = VirtAddr::new(path_ptr as usize);
231ab5c8ca4Slogin                     // 权限校验
232ab5c8ca4Slogin                     if path_ptr.is_null()
2331496ba7bSLoGin                         || (frame.from_user()
2341496ba7bSLoGin                             && verify_area(virt_addr, PAGE_2M_SIZE as usize).is_err())
235ab5c8ca4Slogin                     {
236ab5c8ca4Slogin                         return Err(SystemError::EINVAL);
237ab5c8ca4Slogin                     }
238ab5c8ca4Slogin                     let dest_path: &CStr = unsafe { CStr::from_ptr(path_ptr) };
239ab5c8ca4Slogin                     let dest_path: &str = dest_path.to_str().map_err(|_| SystemError::EINVAL)?;
240ab5c8ca4Slogin                     if dest_path.len() == 0 {
241ab5c8ca4Slogin                         return Err(SystemError::EINVAL);
2421496ba7bSLoGin                     } else if dest_path.len() > MAX_PATHLEN as usize {
243ab5c8ca4Slogin                         return Err(SystemError::ENAMETOOLONG);
244ab5c8ca4Slogin                     }
245ab5c8ca4Slogin 
246ab5c8ca4Slogin                     return Ok(dest_path);
247ab5c8ca4Slogin                 };
248ab5c8ca4Slogin 
2499e9ffedfSPlucky923                 let r = chdir_check(args[0])?;
2509e9ffedfSPlucky923                 Self::chdir(r)
251ab5c8ca4Slogin             }
252ab5c8ca4Slogin 
2534fda81ceSLoGin             #[allow(unreachable_patterns)]
2544fda81ceSLoGin             SYS_GETDENTS64 | SYS_GETDENTS => {
255ab5c8ca4Slogin                 let fd = args[0] as i32;
25634e6d6c8Syuyi2439 
257ab5c8ca4Slogin                 let buf_vaddr = args[1];
258ab5c8ca4Slogin                 let len = args[2];
259f5df0e79SLoGin                 let virt_addr: VirtAddr = VirtAddr::new(buf_vaddr);
260ab5c8ca4Slogin                 // 判断缓冲区是否来自用户态,进行权限校验
2611496ba7bSLoGin                 let res = if frame.from_user() && verify_area(virt_addr, len as usize).is_err() {
262ab5c8ca4Slogin                     // 来自用户态,而buffer在内核态,这样的操作不被允许
263ab5c8ca4Slogin                     Err(SystemError::EPERM)
264ab5c8ca4Slogin                 } else if buf_vaddr == 0 {
265ab5c8ca4Slogin                     Err(SystemError::EFAULT)
266ab5c8ca4Slogin                 } else {
267ab5c8ca4Slogin                     let buf: &mut [u8] = unsafe {
268ab5c8ca4Slogin                         core::slice::from_raw_parts_mut::<'static, u8>(buf_vaddr as *mut u8, len)
269ab5c8ca4Slogin                     };
270ab5c8ca4Slogin                     Self::getdents(fd, buf)
271ab5c8ca4Slogin                 };
272ab5c8ca4Slogin 
273ab5c8ca4Slogin                 res
274ab5c8ca4Slogin             }
275ab5c8ca4Slogin 
276ab5c8ca4Slogin             SYS_EXECVE => {
277ab5c8ca4Slogin                 let path_ptr = args[0];
278ab5c8ca4Slogin                 let argv_ptr = args[1];
279ab5c8ca4Slogin                 let env_ptr = args[2];
280be63f3b2Shoumkh                 let virt_path_ptr = VirtAddr::new(path_ptr);
281be63f3b2Shoumkh                 let virt_argv_ptr = VirtAddr::new(argv_ptr);
282be63f3b2Shoumkh                 let virt_env_ptr = VirtAddr::new(env_ptr);
283ab5c8ca4Slogin                 // 权限校验
2841496ba7bSLoGin                 if frame.from_user()
2851496ba7bSLoGin                     && (verify_area(virt_path_ptr, MAX_PATHLEN as usize).is_err()
286be63f3b2Shoumkh                         || verify_area(virt_argv_ptr, PAGE_4K_SIZE as usize).is_err())
287be63f3b2Shoumkh                     || verify_area(virt_env_ptr, PAGE_4K_SIZE as usize).is_err()
288ab5c8ca4Slogin                 {
289ab5c8ca4Slogin                     Err(SystemError::EFAULT)
290ab5c8ca4Slogin                 } else {
291ab5c8ca4Slogin                     Self::execve(
2921496ba7bSLoGin                         path_ptr as *const u8,
2931496ba7bSLoGin                         argv_ptr as *const *const u8,
2941496ba7bSLoGin                         env_ptr as *const *const u8,
2951496ba7bSLoGin                         frame,
296ab5c8ca4Slogin                     )
2971496ba7bSLoGin                     .map(|_| 0)
298ab5c8ca4Slogin                 }
299ab5c8ca4Slogin             }
300ab5c8ca4Slogin             SYS_WAIT4 => {
301bf4a4899SLoGin                 let pid = args[0] as i32;
3021496ba7bSLoGin                 let wstatus = args[1] as *mut i32;
303ab5c8ca4Slogin                 let options = args[2] as c_int;
304ab5c8ca4Slogin                 let rusage = args[3] as *mut c_void;
305ab5c8ca4Slogin                 // 权限校验
306ab5c8ca4Slogin                 // todo: 引入rusage之后,更正以下权限校验代码中,rusage的大小
307bf4a4899SLoGin                 Self::wait4(pid.into(), wstatus, options, rusage)
308ab5c8ca4Slogin             }
309ab5c8ca4Slogin 
310ab5c8ca4Slogin             SYS_EXIT => {
311ab5c8ca4Slogin                 let exit_code = args[0];
312ab5c8ca4Slogin                 Self::exit(exit_code)
313ab5c8ca4Slogin             }
3144fda81ceSLoGin             #[cfg(target_arch = "x86_64")]
315ab5c8ca4Slogin             SYS_MKDIR => {
316ab5c8ca4Slogin                 let path_ptr = args[0] as *const c_char;
317ab5c8ca4Slogin                 let mode = args[1];
318be63f3b2Shoumkh                 let virt_path_ptr = VirtAddr::new(path_ptr as usize);
319ab5c8ca4Slogin                 let security_check = || {
320ab5c8ca4Slogin                     if path_ptr.is_null()
3211496ba7bSLoGin                         || (frame.from_user()
3221496ba7bSLoGin                             && verify_area(virt_path_ptr, PAGE_2M_SIZE as usize).is_err())
323ab5c8ca4Slogin                     {
324ab5c8ca4Slogin                         return Err(SystemError::EINVAL);
325ab5c8ca4Slogin                     }
326ab5c8ca4Slogin                     let path: &CStr = unsafe { CStr::from_ptr(path_ptr) };
327ab5c8ca4Slogin                     let path: &str = path.to_str().map_err(|_| SystemError::EINVAL)?.trim();
328ab5c8ca4Slogin 
329ab5c8ca4Slogin                     if path == "" {
330ab5c8ca4Slogin                         return Err(SystemError::EINVAL);
331ab5c8ca4Slogin                     }
332ab5c8ca4Slogin                     return Ok(path);
333ab5c8ca4Slogin                 };
334ab5c8ca4Slogin 
335ab5c8ca4Slogin                 let path = security_check();
336ab5c8ca4Slogin                 if path.is_err() {
337ab5c8ca4Slogin                     Err(path.unwrap_err())
338ab5c8ca4Slogin                 } else {
339ab5c8ca4Slogin                     Self::mkdir(path.unwrap(), mode)
340ab5c8ca4Slogin                 }
341ab5c8ca4Slogin             }
342ab5c8ca4Slogin 
343ab5c8ca4Slogin             SYS_NANOSLEEP => {
344ab5c8ca4Slogin                 let req = args[0] as *const TimeSpec;
345ab5c8ca4Slogin                 let rem = args[1] as *mut TimeSpec;
346be63f3b2Shoumkh                 let virt_req = VirtAddr::new(req as usize);
347be63f3b2Shoumkh                 let virt_rem = VirtAddr::new(rem as usize);
3481496ba7bSLoGin                 if frame.from_user()
349be63f3b2Shoumkh                     && (verify_area(virt_req, core::mem::size_of::<TimeSpec>() as usize).is_err()
350be63f3b2Shoumkh                         || verify_area(virt_rem, core::mem::size_of::<TimeSpec>() as usize)
351be63f3b2Shoumkh                             .is_err())
352ab5c8ca4Slogin                 {
353ab5c8ca4Slogin                     Err(SystemError::EFAULT)
354ab5c8ca4Slogin                 } else {
355ab5c8ca4Slogin                     Self::nanosleep(req, rem)
356ab5c8ca4Slogin                 }
357ab5c8ca4Slogin             }
358ab5c8ca4Slogin 
359ab5c8ca4Slogin             SYS_CLOCK => Self::clock(),
3604fda81ceSLoGin             #[cfg(target_arch = "x86_64")]
361ab5c8ca4Slogin             SYS_PIPE => {
36222c9db31Shanjiezhou                 let pipefd: *mut i32 = args[0] as *mut c_int;
3630fb515b0SLoGin                 if pipefd.is_null() {
3640fb515b0SLoGin                     Err(SystemError::EFAULT)
3650fb515b0SLoGin                 } else {
3660fb515b0SLoGin                     Self::pipe2(pipefd, FileMode::empty())
3670fb515b0SLoGin                 }
3680fb515b0SLoGin             }
3694ad52e57S裕依2439 
3700fb515b0SLoGin             SYS_PIPE2 => {
3710fb515b0SLoGin                 let pipefd: *mut i32 = args[0] as *mut c_int;
37222c9db31Shanjiezhou                 let arg1 = args[1];
37322c9db31Shanjiezhou                 if pipefd.is_null() {
37422c9db31Shanjiezhou                     Err(SystemError::EFAULT)
37522c9db31Shanjiezhou                 } else {
37622c9db31Shanjiezhou                     let flags = FileMode::from_bits_truncate(arg1 as u32);
37722c9db31Shanjiezhou                     Self::pipe2(pipefd, flags)
378ab5c8ca4Slogin                 }
379ab5c8ca4Slogin             }
380ab5c8ca4Slogin 
3814fda81ceSLoGin             SYS_UNLINKAT => {
382ab5c8ca4Slogin                 let dirfd = args[0] as i32;
383ab5c8ca4Slogin                 let pathname = args[1] as *const c_char;
384ab5c8ca4Slogin                 let flags = args[2] as u32;
385be63f3b2Shoumkh                 let virt_pathname = VirtAddr::new(pathname as usize);
3861496ba7bSLoGin                 if frame.from_user() && verify_area(virt_pathname, PAGE_4K_SIZE as usize).is_err() {
387ab5c8ca4Slogin                     Err(SystemError::EFAULT)
388ab5c8ca4Slogin                 } else if pathname.is_null() {
389ab5c8ca4Slogin                     Err(SystemError::EFAULT)
390ab5c8ca4Slogin                 } else {
391ab5c8ca4Slogin                     let get_path = || {
392ab5c8ca4Slogin                         let pathname: &CStr = unsafe { CStr::from_ptr(pathname) };
393ab5c8ca4Slogin 
394ab5c8ca4Slogin                         let pathname: &str = pathname.to_str().map_err(|_| SystemError::EINVAL)?;
395ab5c8ca4Slogin                         if pathname.len() >= MAX_PATHLEN {
396ab5c8ca4Slogin                             return Err(SystemError::ENAMETOOLONG);
397ab5c8ca4Slogin                         }
398ab5c8ca4Slogin                         return Ok(pathname.trim());
399ab5c8ca4Slogin                     };
400ab5c8ca4Slogin                     let pathname = get_path();
401ab5c8ca4Slogin                     if pathname.is_err() {
402ab5c8ca4Slogin                         Err(pathname.unwrap_err())
403ab5c8ca4Slogin                     } else {
4046d81180bSLoGin                         // kdebug!("sys unlinkat: dirfd: {}, pathname: {}", dirfd, pathname.as_ref().unwrap());
405ab5c8ca4Slogin                         Self::unlinkat(dirfd, pathname.unwrap(), flags)
406ab5c8ca4Slogin                     }
407ab5c8ca4Slogin                 }
408ab5c8ca4Slogin             }
409bf4a4899SLoGin 
4104fda81ceSLoGin             #[cfg(target_arch = "x86_64")]
411bf4a4899SLoGin             SYS_UNLINK => {
412bf4a4899SLoGin                 let pathname = args[0] as *const u8;
413bf4a4899SLoGin                 Self::unlink(pathname)
414bf4a4899SLoGin             }
415ab5c8ca4Slogin             SYS_KILL => {
4161496ba7bSLoGin                 let pid = Pid::new(args[0]);
417ab5c8ca4Slogin                 let sig = args[1] as c_int;
4183c82aa56SChiichen                 // kdebug!("KILL SYSCALL RECEIVED");
419ab5c8ca4Slogin                 Self::kill(pid, sig)
420ab5c8ca4Slogin             }
421ab5c8ca4Slogin 
4224fda81ceSLoGin             SYS_RT_SIGACTION => {
423ab5c8ca4Slogin                 let sig = args[0] as c_int;
424ab5c8ca4Slogin                 let act = args[1];
425ab5c8ca4Slogin                 let old_act = args[2];
4261496ba7bSLoGin                 Self::sigaction(sig, act, old_act, frame.from_user())
427ab5c8ca4Slogin             }
428ab5c8ca4Slogin 
4291496ba7bSLoGin             SYS_GETPID => Self::getpid().map(|pid| pid.into()),
430ab5c8ca4Slogin 
4311496ba7bSLoGin             SYS_SCHED => Self::sched(frame.from_user()),
432ab5c8ca4Slogin             SYS_DUP => {
433ab5c8ca4Slogin                 let oldfd: i32 = args[0] as c_int;
434ab5c8ca4Slogin                 Self::dup(oldfd)
435ab5c8ca4Slogin             }
4364fda81ceSLoGin 
4374fda81ceSLoGin             #[cfg(target_arch = "x86_64")]
438ab5c8ca4Slogin             SYS_DUP2 => {
439ab5c8ca4Slogin                 let oldfd: i32 = args[0] as c_int;
440ab5c8ca4Slogin                 let newfd: i32 = args[1] as c_int;
441ab5c8ca4Slogin                 Self::dup2(oldfd, newfd)
442ab5c8ca4Slogin             }
443ab5c8ca4Slogin 
444ab5c8ca4Slogin             SYS_SOCKET => Self::socket(args[0], args[1], args[2]),
445ab5c8ca4Slogin             SYS_SETSOCKOPT => {
446ab5c8ca4Slogin                 let optval = args[3] as *const u8;
447ab5c8ca4Slogin                 let optlen = args[4] as usize;
448be63f3b2Shoumkh                 let virt_optval = VirtAddr::new(optval as usize);
449ab5c8ca4Slogin                 // 验证optval的地址是否合法
450be63f3b2Shoumkh                 if verify_area(virt_optval, optlen as usize).is_err() {
451ab5c8ca4Slogin                     // 地址空间超出了用户空间的范围,不合法
452ab5c8ca4Slogin                     Err(SystemError::EFAULT)
453ab5c8ca4Slogin                 } else {
454ab5c8ca4Slogin                     let data: &[u8] = unsafe { core::slice::from_raw_parts(optval, optlen) };
455ab5c8ca4Slogin                     Self::setsockopt(args[0], args[1], args[2], data)
456ab5c8ca4Slogin                 }
457ab5c8ca4Slogin             }
458ab5c8ca4Slogin             SYS_GETSOCKOPT => {
459ab5c8ca4Slogin                 let optval = args[3] as *mut u8;
460ab5c8ca4Slogin                 let optlen = args[4] as *mut usize;
461be63f3b2Shoumkh                 let virt_optval = VirtAddr::new(optval as usize);
462be63f3b2Shoumkh                 let virt_optlen = VirtAddr::new(optlen as usize);
463ab5c8ca4Slogin                 let security_check = || {
464ab5c8ca4Slogin                     // 验证optval的地址是否合法
465be63f3b2Shoumkh                     if verify_area(virt_optval, PAGE_4K_SIZE as usize).is_err() {
466ab5c8ca4Slogin                         // 地址空间超出了用户空间的范围,不合法
467ab5c8ca4Slogin                         return Err(SystemError::EFAULT);
468ab5c8ca4Slogin                     }
469ab5c8ca4Slogin 
470ab5c8ca4Slogin                     // 验证optlen的地址是否合法
471be63f3b2Shoumkh                     if verify_area(virt_optlen, core::mem::size_of::<u32>() as usize).is_err() {
472ab5c8ca4Slogin                         // 地址空间超出了用户空间的范围,不合法
473ab5c8ca4Slogin                         return Err(SystemError::EFAULT);
474ab5c8ca4Slogin                     }
475ab5c8ca4Slogin                     return Ok(());
476ab5c8ca4Slogin                 };
477ab5c8ca4Slogin                 let r = security_check();
478ab5c8ca4Slogin                 if r.is_err() {
479ab5c8ca4Slogin                     Err(r.unwrap_err())
480ab5c8ca4Slogin                 } else {
481ab5c8ca4Slogin                     Self::getsockopt(args[0], args[1], args[2], optval, optlen as *mut u32)
482ab5c8ca4Slogin                 }
483ab5c8ca4Slogin             }
484ab5c8ca4Slogin 
485ab5c8ca4Slogin             SYS_CONNECT => {
486ab5c8ca4Slogin                 let addr = args[1] as *const SockAddr;
487ab5c8ca4Slogin                 let addrlen = args[2] as usize;
488be63f3b2Shoumkh                 let virt_addr = VirtAddr::new(addr as usize);
489ab5c8ca4Slogin                 // 验证addr的地址是否合法
490be63f3b2Shoumkh                 if verify_area(virt_addr, addrlen as usize).is_err() {
491ab5c8ca4Slogin                     // 地址空间超出了用户空间的范围,不合法
492ab5c8ca4Slogin                     Err(SystemError::EFAULT)
493ab5c8ca4Slogin                 } else {
494ab5c8ca4Slogin                     Self::connect(args[0], addr, addrlen)
495ab5c8ca4Slogin                 }
496ab5c8ca4Slogin             }
497ab5c8ca4Slogin             SYS_BIND => {
498ab5c8ca4Slogin                 let addr = args[1] as *const SockAddr;
499ab5c8ca4Slogin                 let addrlen = args[2] as usize;
500be63f3b2Shoumkh                 let virt_addr = VirtAddr::new(addr as usize);
501ab5c8ca4Slogin                 // 验证addr的地址是否合法
502be63f3b2Shoumkh                 if verify_area(virt_addr, addrlen as usize).is_err() {
503ab5c8ca4Slogin                     // 地址空间超出了用户空间的范围,不合法
504ab5c8ca4Slogin                     Err(SystemError::EFAULT)
505ab5c8ca4Slogin                 } else {
506ab5c8ca4Slogin                     Self::bind(args[0], addr, addrlen)
507ab5c8ca4Slogin                 }
508ab5c8ca4Slogin             }
509ab5c8ca4Slogin 
510ab5c8ca4Slogin             SYS_SENDTO => {
511ab5c8ca4Slogin                 let buf = args[1] as *const u8;
512ab5c8ca4Slogin                 let len = args[2] as usize;
513ab5c8ca4Slogin                 let flags = args[3] as u32;
514ab5c8ca4Slogin                 let addr = args[4] as *const SockAddr;
515ab5c8ca4Slogin                 let addrlen = args[5] as usize;
516be63f3b2Shoumkh                 let virt_buf = VirtAddr::new(buf as usize);
517be63f3b2Shoumkh                 let virt_addr = VirtAddr::new(addr as usize);
518ab5c8ca4Slogin                 // 验证buf的地址是否合法
519be63f3b2Shoumkh                 if verify_area(virt_buf, len as usize).is_err() {
520ab5c8ca4Slogin                     // 地址空间超出了用户空间的范围,不合法
521ab5c8ca4Slogin                     Err(SystemError::EFAULT)
522be63f3b2Shoumkh                 } else if verify_area(virt_addr, addrlen as usize).is_err() {
523ab5c8ca4Slogin                     // 地址空间超出了用户空间的范围,不合法
524ab5c8ca4Slogin                     Err(SystemError::EFAULT)
525ab5c8ca4Slogin                 } else {
526ab5c8ca4Slogin                     let data: &[u8] = unsafe { core::slice::from_raw_parts(buf, len) };
527ab5c8ca4Slogin                     Self::sendto(args[0], data, flags, addr, addrlen)
528ab5c8ca4Slogin                 }
529ab5c8ca4Slogin             }
530ab5c8ca4Slogin 
531ab5c8ca4Slogin             SYS_RECVFROM => {
532ab5c8ca4Slogin                 let buf = args[1] as *mut u8;
533ab5c8ca4Slogin                 let len = args[2] as usize;
534ab5c8ca4Slogin                 let flags = args[3] as u32;
535ab5c8ca4Slogin                 let addr = args[4] as *mut SockAddr;
536ab5c8ca4Slogin                 let addrlen = args[5] as *mut usize;
537be63f3b2Shoumkh                 let virt_buf = VirtAddr::new(buf as usize);
538be63f3b2Shoumkh                 let virt_addrlen = VirtAddr::new(addrlen as usize);
539be63f3b2Shoumkh                 let virt_addr = VirtAddr::new(addr as usize);
540ab5c8ca4Slogin                 let security_check = || {
541ab5c8ca4Slogin                     // 验证buf的地址是否合法
542be63f3b2Shoumkh                     if verify_area(virt_buf, len as usize).is_err() {
543ab5c8ca4Slogin                         // 地址空间超出了用户空间的范围,不合法
544ab5c8ca4Slogin                         return Err(SystemError::EFAULT);
545ab5c8ca4Slogin                     }
546ab5c8ca4Slogin 
547ab5c8ca4Slogin                     // 验证addrlen的地址是否合法
548be63f3b2Shoumkh                     if verify_area(virt_addrlen, core::mem::size_of::<u32>() as usize).is_err() {
549ab5c8ca4Slogin                         // 地址空间超出了用户空间的范围,不合法
550ab5c8ca4Slogin                         return Err(SystemError::EFAULT);
551ab5c8ca4Slogin                     }
552ab5c8ca4Slogin 
553be63f3b2Shoumkh                     if verify_area(virt_addr, core::mem::size_of::<SockAddr>() as usize).is_err() {
554ab5c8ca4Slogin                         // 地址空间超出了用户空间的范围,不合法
555ab5c8ca4Slogin                         return Err(SystemError::EFAULT);
556ab5c8ca4Slogin                     }
557ab5c8ca4Slogin                     return Ok(());
558ab5c8ca4Slogin                 };
559ab5c8ca4Slogin                 let r = security_check();
560ab5c8ca4Slogin                 if r.is_err() {
561ab5c8ca4Slogin                     Err(r.unwrap_err())
562ab5c8ca4Slogin                 } else {
563ab5c8ca4Slogin                     let buf = unsafe { core::slice::from_raw_parts_mut(buf, len) };
564ab5c8ca4Slogin                     Self::recvfrom(args[0], buf, flags, addr, addrlen as *mut u32)
565ab5c8ca4Slogin                 }
566ab5c8ca4Slogin             }
567ab5c8ca4Slogin 
568ab5c8ca4Slogin             SYS_RECVMSG => {
5694ad52e57S裕依2439                 let msg = args[1] as *mut MsgHdr;
570ab5c8ca4Slogin                 let flags = args[2] as u32;
5714ad52e57S裕依2439 
5724ad52e57S裕依2439                 let mut user_buffer_writer =
5734ad52e57S裕依2439                     UserBufferWriter::new(msg, core::mem::size_of::<MsgHdr>(), frame.from_user())?;
5744ad52e57S裕依2439                 let buffer = user_buffer_writer.buffer::<MsgHdr>(0)?;
5754ad52e57S裕依2439 
5764537ffb7SChiichen                 let msg = &mut buffer[0];
577ab5c8ca4Slogin                 Self::recvmsg(args[0], msg, flags)
578ab5c8ca4Slogin             }
579ab5c8ca4Slogin 
580ab5c8ca4Slogin             SYS_LISTEN => Self::listen(args[0], args[1]),
581ab5c8ca4Slogin             SYS_SHUTDOWN => Self::shutdown(args[0], args[1]),
582ab5c8ca4Slogin             SYS_ACCEPT => Self::accept(args[0], args[1] as *mut SockAddr, args[2] as *mut u32),
583c47fe904SLoGin             SYS_ACCEPT4 => Self::accept4(
584c47fe904SLoGin                 args[0],
585c47fe904SLoGin                 args[1] as *mut SockAddr,
586c47fe904SLoGin                 args[2] as *mut u32,
587c47fe904SLoGin                 args[3] as u32,
588c47fe904SLoGin             ),
589ab5c8ca4Slogin             SYS_GETSOCKNAME => {
590ab5c8ca4Slogin                 Self::getsockname(args[0], args[1] as *mut SockAddr, args[2] as *mut u32)
591ab5c8ca4Slogin             }
592ab5c8ca4Slogin             SYS_GETPEERNAME => {
593ab5c8ca4Slogin                 Self::getpeername(args[0], args[1] as *mut SockAddr, args[2] as *mut u32)
594ab5c8ca4Slogin             }
59536fd0130Shoumkh             SYS_GETTIMEOFDAY => {
59636fd0130Shoumkh                 let timeval = args[0] as *mut PosixTimeval;
59740fe15e0SLoGin                 let timezone_ptr = args[1] as *mut PosixTimeZone;
59840fe15e0SLoGin                 Self::gettimeofday(timeval, timezone_ptr)
5994537ffb7SChiichen             }
60040fe15e0SLoGin             SYS_MMAP => {
60140fe15e0SLoGin                 let len = page_align_up(args[1]);
602be63f3b2Shoumkh                 let virt_addr = VirtAddr::new(args[0] as usize);
603be63f3b2Shoumkh                 if verify_area(virt_addr, len as usize).is_err() {
60440fe15e0SLoGin                     Err(SystemError::EFAULT)
60540fe15e0SLoGin                 } else {
60640fe15e0SLoGin                     Self::mmap(
60740fe15e0SLoGin                         VirtAddr::new(args[0]),
60840fe15e0SLoGin                         len,
60940fe15e0SLoGin                         args[2],
61040fe15e0SLoGin                         args[3],
61140fe15e0SLoGin                         args[4] as i32,
61240fe15e0SLoGin                         args[5],
61340fe15e0SLoGin                     )
61440fe15e0SLoGin                 }
61540fe15e0SLoGin             }
616*4cfa009bSJomo             SYS_MREMAP => {
617*4cfa009bSJomo                 let old_vaddr = VirtAddr::new(args[0]);
618*4cfa009bSJomo                 let old_len = args[1];
619*4cfa009bSJomo                 let new_len = args[2];
620*4cfa009bSJomo                 let mremap_flags = MremapFlags::from_bits_truncate(args[3] as u8);
621*4cfa009bSJomo                 let new_vaddr = VirtAddr::new(args[4]);
622*4cfa009bSJomo 
623*4cfa009bSJomo                 Self::mremap(old_vaddr, old_len, new_len, mremap_flags, new_vaddr)
624*4cfa009bSJomo             }
62540fe15e0SLoGin             SYS_MUNMAP => {
62640fe15e0SLoGin                 let addr = args[0];
62740fe15e0SLoGin                 let len = page_align_up(args[1]);
628971462beSGnoCiYeH                 if addr & (MMArch::PAGE_SIZE - 1) != 0 {
62940fe15e0SLoGin                     // The addr argument is not a multiple of the page size
63040fe15e0SLoGin                     Err(SystemError::EINVAL)
63140fe15e0SLoGin                 } else {
63240fe15e0SLoGin                     Self::munmap(VirtAddr::new(addr), len)
63340fe15e0SLoGin                 }
63440fe15e0SLoGin             }
63540fe15e0SLoGin             SYS_MPROTECT => {
63640fe15e0SLoGin                 let addr = args[0];
63740fe15e0SLoGin                 let len = page_align_up(args[1]);
638971462beSGnoCiYeH                 if addr & (MMArch::PAGE_SIZE - 1) != 0 {
63940fe15e0SLoGin                     // The addr argument is not a multiple of the page size
64040fe15e0SLoGin                     Err(SystemError::EINVAL)
64140fe15e0SLoGin                 } else {
64240fe15e0SLoGin                     Self::mprotect(VirtAddr::new(addr), len, args[2])
64340fe15e0SLoGin                 }
64440fe15e0SLoGin             }
64540fe15e0SLoGin 
6461496ba7bSLoGin             SYS_GETCWD => {
6471496ba7bSLoGin                 let buf = args[0] as *mut u8;
6481496ba7bSLoGin                 let size = args[1] as usize;
6491496ba7bSLoGin                 let security_check = || {
6501496ba7bSLoGin                     verify_area(VirtAddr::new(buf as usize), size)?;
6511496ba7bSLoGin                     return Ok(());
6521496ba7bSLoGin                 };
6531496ba7bSLoGin                 let r = security_check();
6541496ba7bSLoGin                 if r.is_err() {
6551496ba7bSLoGin                     Err(r.unwrap_err())
6561496ba7bSLoGin                 } else {
6571496ba7bSLoGin                     let buf = unsafe { core::slice::from_raw_parts_mut(buf, size) };
6581496ba7bSLoGin                     Self::getcwd(buf).map(|ptr| ptr.data())
6591496ba7bSLoGin                 }
6601496ba7bSLoGin             }
6611496ba7bSLoGin 
6621496ba7bSLoGin             SYS_GETPGID => Self::getpgid(Pid::new(args[0])).map(|pid| pid.into()),
6631496ba7bSLoGin 
6641496ba7bSLoGin             SYS_GETPPID => Self::getppid().map(|pid| pid.into()),
665be63f3b2Shoumkh             SYS_FSTAT => {
666be63f3b2Shoumkh                 let fd = args[0] as i32;
667be63f3b2Shoumkh                 let kstat = args[1] as *mut PosixKstat;
668be63f3b2Shoumkh                 let vaddr = VirtAddr::new(kstat as usize);
669be63f3b2Shoumkh                 // FIXME 由于c中的verify_area与rust中的verify_area重名,所以在引入时加了前缀区分
670be63f3b2Shoumkh                 // TODO 应该将用了c版本的verify_area都改为rust的verify_area
671be63f3b2Shoumkh                 match verify_area(vaddr, core::mem::size_of::<PosixKstat>()) {
672be63f3b2Shoumkh                     Ok(_) => Self::fstat(fd, kstat),
673be63f3b2Shoumkh                     Err(e) => Err(e),
674be63f3b2Shoumkh                 }
675be63f3b2Shoumkh             }
676be63f3b2Shoumkh 
6776d81180bSLoGin             SYS_FCNTL => {
6786d81180bSLoGin                 let fd = args[0] as i32;
6796d81180bSLoGin                 let cmd: Option<FcntlCommand> =
6806d81180bSLoGin                     <FcntlCommand as FromPrimitive>::from_u32(args[1] as u32);
6816d81180bSLoGin                 let arg = args[2] as i32;
6826d81180bSLoGin                 let res = if let Some(cmd) = cmd {
6836d81180bSLoGin                     Self::fcntl(fd, cmd, arg)
6846d81180bSLoGin                 } else {
6856d81180bSLoGin                     Err(SystemError::EINVAL)
6866d81180bSLoGin                 };
6876d81180bSLoGin 
6886d81180bSLoGin                 // kdebug!("FCNTL: fd: {}, cmd: {:?}, arg: {}, res: {:?}", fd, cmd, arg, res);
6896d81180bSLoGin                 res
6906d81180bSLoGin             }
6916d81180bSLoGin 
6926d81180bSLoGin             SYS_FTRUNCATE => {
6936d81180bSLoGin                 let fd = args[0] as i32;
6946d81180bSLoGin                 let len = args[1] as usize;
6956d81180bSLoGin                 let res = Self::ftruncate(fd, len);
6966d81180bSLoGin                 // kdebug!("FTRUNCATE: fd: {}, len: {}, res: {:?}", fd, len, res);
6976d81180bSLoGin                 res
6986d81180bSLoGin             }
6996d81180bSLoGin 
7004fda81ceSLoGin             #[cfg(target_arch = "x86_64")]
7012dbef785SGnoCiYeH             SYS_MKNOD => {
7022dbef785SGnoCiYeH                 let path = args[0];
7032dbef785SGnoCiYeH                 let flags = args[1];
7042dbef785SGnoCiYeH                 let dev_t = args[2];
7052dbef785SGnoCiYeH                 let flags: ModeType = ModeType::from_bits_truncate(flags as u32);
706c566df45SLoGin                 Self::mknod(path as *const i8, flags, DeviceNumber::from(dev_t as u32))
7072dbef785SGnoCiYeH             }
7082dbef785SGnoCiYeH 
709971462beSGnoCiYeH             SYS_CLONE => {
710971462beSGnoCiYeH                 let parent_tid = VirtAddr::new(args[2]);
711971462beSGnoCiYeH                 let child_tid = VirtAddr::new(args[3]);
712971462beSGnoCiYeH 
713971462beSGnoCiYeH                 // 地址校验
714971462beSGnoCiYeH                 verify_area(parent_tid, core::mem::size_of::<i32>())?;
715971462beSGnoCiYeH                 verify_area(child_tid, core::mem::size_of::<i32>())?;
716971462beSGnoCiYeH 
717971462beSGnoCiYeH                 let mut clone_args = KernelCloneArgs::new();
718971462beSGnoCiYeH                 clone_args.flags = CloneFlags::from_bits_truncate(args[0] as u64);
719971462beSGnoCiYeH                 clone_args.stack = args[1];
720971462beSGnoCiYeH                 clone_args.parent_tid = parent_tid;
721971462beSGnoCiYeH                 clone_args.child_tid = child_tid;
722971462beSGnoCiYeH                 clone_args.tls = args[4];
723971462beSGnoCiYeH                 Self::clone(frame, clone_args)
724971462beSGnoCiYeH             }
725971462beSGnoCiYeH 
726971462beSGnoCiYeH             SYS_FUTEX => {
727971462beSGnoCiYeH                 let uaddr = VirtAddr::new(args[0]);
728971462beSGnoCiYeH                 let operation = FutexFlag::from_bits(args[1] as u32).ok_or(SystemError::ENOSYS)?;
729971462beSGnoCiYeH                 let val = args[2] as u32;
730971462beSGnoCiYeH                 let utime = args[3];
731971462beSGnoCiYeH                 let uaddr2 = VirtAddr::new(args[4]);
732971462beSGnoCiYeH                 let val3 = args[5] as u32;
733971462beSGnoCiYeH 
734971462beSGnoCiYeH                 verify_area(uaddr, core::mem::size_of::<u32>())?;
735971462beSGnoCiYeH                 verify_area(uaddr2, core::mem::size_of::<u32>())?;
736971462beSGnoCiYeH 
737971462beSGnoCiYeH                 let mut timespec = None;
738971462beSGnoCiYeH                 if utime != 0 && operation.contains(FutexFlag::FLAGS_HAS_TIMEOUT) {
739971462beSGnoCiYeH                     let reader = UserBufferReader::new(
740971462beSGnoCiYeH                         utime as *const TimeSpec,
741971462beSGnoCiYeH                         core::mem::size_of::<TimeSpec>(),
742971462beSGnoCiYeH                         true,
743971462beSGnoCiYeH                     )?;
744971462beSGnoCiYeH 
745971462beSGnoCiYeH                     timespec = Some(reader.read_one_from_user::<TimeSpec>(0)?.clone());
746971462beSGnoCiYeH                 }
747971462beSGnoCiYeH 
748971462beSGnoCiYeH                 Self::do_futex(uaddr, operation, val, timespec, uaddr2, utime as u32, val3)
749971462beSGnoCiYeH             }
750971462beSGnoCiYeH 
751bf4a4899SLoGin             SYS_READV => Self::readv(args[0] as i32, args[1], args[2]),
752971462beSGnoCiYeH             SYS_WRITEV => Self::writev(args[0] as i32, args[1], args[2]),
753971462beSGnoCiYeH 
7544fda81ceSLoGin             SYS_SET_TID_ADDRESS => Self::set_tid_address(args[0]),
755971462beSGnoCiYeH 
7564fda81ceSLoGin             #[cfg(target_arch = "x86_64")]
7574fda81ceSLoGin             SYS_LSTAT => {
758971462beSGnoCiYeH                 let path: &CStr = unsafe { CStr::from_ptr(args[0] as *const c_char) };
759971462beSGnoCiYeH                 let path: Result<&str, core::str::Utf8Error> = path.to_str();
760971462beSGnoCiYeH                 let res = if path.is_err() {
761971462beSGnoCiYeH                     Err(SystemError::EINVAL)
762971462beSGnoCiYeH                 } else {
763971462beSGnoCiYeH                     let path: &str = path.unwrap();
764971462beSGnoCiYeH                     let kstat = args[1] as *mut PosixKstat;
765971462beSGnoCiYeH                     let vaddr = VirtAddr::new(kstat as usize);
766971462beSGnoCiYeH                     match verify_area(vaddr, core::mem::size_of::<PosixKstat>()) {
7674fda81ceSLoGin                         Ok(_) => Self::lstat(path, kstat),
7684fda81ceSLoGin                         Err(e) => Err(e),
7694fda81ceSLoGin                     }
7704fda81ceSLoGin                 };
7714fda81ceSLoGin 
7724fda81ceSLoGin                 res
7734fda81ceSLoGin             }
7744fda81ceSLoGin 
7754fda81ceSLoGin             #[cfg(target_arch = "x86_64")]
7764fda81ceSLoGin             SYS_STAT => {
7774fda81ceSLoGin                 let path: &CStr = unsafe { CStr::from_ptr(args[0] as *const c_char) };
7784fda81ceSLoGin                 let path: Result<&str, core::str::Utf8Error> = path.to_str();
7794fda81ceSLoGin                 let res = if path.is_err() {
7804fda81ceSLoGin                     Err(SystemError::EINVAL)
781bf4a4899SLoGin                 } else {
7824fda81ceSLoGin                     let path: &str = path.unwrap();
7834fda81ceSLoGin                     let kstat = args[1] as *mut PosixKstat;
7844fda81ceSLoGin                     let vaddr = VirtAddr::new(kstat as usize);
7854fda81ceSLoGin                     match verify_area(vaddr, core::mem::size_of::<PosixKstat>()) {
7864fda81ceSLoGin                         Ok(_) => Self::stat(path, kstat),
787971462beSGnoCiYeH                         Err(e) => Err(e),
788971462beSGnoCiYeH                     }
789971462beSGnoCiYeH                 };
790971462beSGnoCiYeH 
791971462beSGnoCiYeH                 res
792971462beSGnoCiYeH             }
793971462beSGnoCiYeH 
79440609970SGnoCiYeH             SYS_EPOLL_CREATE => Self::epoll_create(args[0] as i32),
79540609970SGnoCiYeH             SYS_EPOLL_CREATE1 => Self::epoll_create1(args[0]),
79640609970SGnoCiYeH 
79740609970SGnoCiYeH             SYS_EPOLL_CTL => Self::epoll_ctl(
79840609970SGnoCiYeH                 args[0] as i32,
79940609970SGnoCiYeH                 args[1],
80040609970SGnoCiYeH                 args[2] as i32,
80140609970SGnoCiYeH                 VirtAddr::new(args[3]),
80240609970SGnoCiYeH             ),
80340609970SGnoCiYeH 
80440609970SGnoCiYeH             SYS_EPOLL_WAIT => Self::epoll_wait(
80540609970SGnoCiYeH                 args[0] as i32,
80640609970SGnoCiYeH                 VirtAddr::new(args[1]),
80740609970SGnoCiYeH                 args[2] as i32,
80840609970SGnoCiYeH                 args[3] as i32,
80940609970SGnoCiYeH             ),
81040609970SGnoCiYeH 
81140609970SGnoCiYeH             SYS_EPOLL_PWAIT => {
81240609970SGnoCiYeH                 let epfd = args[0] as i32;
81340609970SGnoCiYeH                 let epoll_event = VirtAddr::new(args[1]);
81440609970SGnoCiYeH                 let max_events = args[2] as i32;
81540609970SGnoCiYeH                 let timespec = args[3] as i32;
81640609970SGnoCiYeH                 let sigmask_addr = args[4] as *mut SigSet;
81740609970SGnoCiYeH 
81840609970SGnoCiYeH                 if sigmask_addr.is_null() {
81940609970SGnoCiYeH                     return Self::epoll_wait(epfd, epoll_event, max_events, timespec);
82040609970SGnoCiYeH                 }
82140609970SGnoCiYeH                 let sigmask_reader =
82240609970SGnoCiYeH                     UserBufferReader::new(sigmask_addr, core::mem::size_of::<SigSet>(), true)?;
82340609970SGnoCiYeH                 let mut sigmask = sigmask_reader.read_one_from_user::<SigSet>(0)?.clone();
82440609970SGnoCiYeH 
82540609970SGnoCiYeH                 Self::epoll_pwait(
82640609970SGnoCiYeH                     args[0] as i32,
82740609970SGnoCiYeH                     VirtAddr::new(args[1]),
82840609970SGnoCiYeH                     args[2] as i32,
82940609970SGnoCiYeH                     args[3] as i32,
83040609970SGnoCiYeH                     &mut sigmask,
83140609970SGnoCiYeH                 )
83240609970SGnoCiYeH             }
83340609970SGnoCiYeH 
834971462beSGnoCiYeH             // 目前为了适配musl-libc,以下系统调用先这样写着
8354fda81ceSLoGin             SYS_GETRANDOM => {
836971462beSGnoCiYeH                 let flags = GRandFlags::from_bits(args[2] as u8).ok_or(SystemError::EINVAL)?;
837971462beSGnoCiYeH                 Self::get_random(args[0] as *mut u8, args[1], flags)
838971462beSGnoCiYeH             }
839971462beSGnoCiYeH 
8404fda81ceSLoGin             SYS_SOCKETPAIR => {
8414ad52e57S裕依2439                 let mut user_buffer_writer = UserBufferWriter::new(
8424ad52e57S裕依2439                     args[3] as *mut c_int,
8434ad52e57S裕依2439                     core::mem::size_of::<[c_int; 2]>(),
8444ad52e57S裕依2439                     frame.from_user(),
8454ad52e57S裕依2439                 )?;
8464ad52e57S裕依2439                 let fds = user_buffer_writer.buffer::<i32>(0)?;
8474ad52e57S裕依2439                 Self::socketpair(args[0], args[1], args[2], fds)
848971462beSGnoCiYeH             }
849971462beSGnoCiYeH 
8504fda81ceSLoGin             #[cfg(target_arch = "x86_64")]
851971462beSGnoCiYeH             SYS_POLL => {
852971462beSGnoCiYeH                 kwarn!("SYS_POLL has not yet been implemented");
853971462beSGnoCiYeH                 Ok(0)
854971462beSGnoCiYeH             }
855971462beSGnoCiYeH 
856971462beSGnoCiYeH             SYS_RT_SIGPROCMASK => {
857971462beSGnoCiYeH                 kwarn!("SYS_RT_SIGPROCMASK has not yet been implemented");
858971462beSGnoCiYeH                 Ok(0)
859971462beSGnoCiYeH             }
860971462beSGnoCiYeH 
861971462beSGnoCiYeH             SYS_TKILL => {
862971462beSGnoCiYeH                 kwarn!("SYS_TKILL has not yet been implemented");
863971462beSGnoCiYeH                 Ok(0)
864971462beSGnoCiYeH             }
865971462beSGnoCiYeH 
866971462beSGnoCiYeH             SYS_SIGALTSTACK => {
867971462beSGnoCiYeH                 kwarn!("SYS_SIGALTSTACK has not yet been implemented");
868971462beSGnoCiYeH                 Ok(0)
869971462beSGnoCiYeH             }
870971462beSGnoCiYeH 
87116033951SGnoCiYeH             SYS_EXIT_GROUP => {
87216033951SGnoCiYeH                 kwarn!("SYS_EXIT_GROUP has not yet been implemented");
87316033951SGnoCiYeH                 Ok(0)
87416033951SGnoCiYeH             }
87516033951SGnoCiYeH 
87604babc3fSMemoryShore             SYS_MADVISE => {
8776c7f966cSMemoryShore                 // 这个太吵了,总是打印,先注释掉
8786c7f966cSMemoryShore                 // kwarn!("SYS_MADVISE has not yet been implemented");
87904babc3fSMemoryShore                 Ok(0)
88004babc3fSMemoryShore             }
881393f6915SLoGin             SYS_GETTID => Self::gettid().map(|tid| tid.into()),
88202e249f3SLoGin             SYS_GETUID => Self::getuid().map(|uid| uid.into()),
8838d72b68dSJomo 
88402e249f3SLoGin             SYS_SYSLOG => {
8858d72b68dSJomo                 let syslog_action_type = args[0] as usize;
8868d72b68dSJomo                 let buf_vaddr = args[1];
8878d72b68dSJomo                 let len = args[2];
8888d72b68dSJomo                 let from_user = frame.from_user();
8898d72b68dSJomo                 let mut user_buffer_writer =
8908d72b68dSJomo                     UserBufferWriter::new(buf_vaddr as *mut u8, len, from_user)?;
8918d72b68dSJomo 
8928d72b68dSJomo                 let user_buf = user_buffer_writer.buffer(0)?;
8938d72b68dSJomo                 let res = Self::do_syslog(syslog_action_type, user_buf, len);
8948d72b68dSJomo                 res
89502e249f3SLoGin             }
8968d72b68dSJomo 
89702e249f3SLoGin             SYS_GETGID => Self::getgid().map(|gid| gid.into()),
89802e249f3SLoGin             SYS_SETUID => {
89902e249f3SLoGin                 kwarn!("SYS_SETUID has not yet been implemented");
90002e249f3SLoGin                 Ok(0)
90102e249f3SLoGin             }
90202e249f3SLoGin             SYS_SETGID => {
90302e249f3SLoGin                 kwarn!("SYS_SETGID has not yet been implemented");
90402e249f3SLoGin                 Ok(0)
90502e249f3SLoGin             }
90602e249f3SLoGin             SYS_GETEUID => Self::geteuid().map(|euid| euid.into()),
90702e249f3SLoGin             SYS_GETEGID => Self::getegid().map(|egid| egid.into()),
908be8cdf4bSLoGin             SYS_GETRUSAGE => {
909be8cdf4bSLoGin                 let who = args[0] as c_int;
910be8cdf4bSLoGin                 let rusage = args[1] as *mut RUsage;
911be8cdf4bSLoGin                 Self::get_rusage(who, rusage)
912be8cdf4bSLoGin             }
91304babc3fSMemoryShore 
9144fda81ceSLoGin             #[cfg(target_arch = "x86_64")]
915709498caSLoGin             SYS_READLINK => {
916709498caSLoGin                 let path = args[0] as *const u8;
917709498caSLoGin                 let buf = args[1] as *mut u8;
918709498caSLoGin                 let bufsiz = args[2] as usize;
919709498caSLoGin                 Self::readlink(path, buf, bufsiz)
920709498caSLoGin             }
921709498caSLoGin 
9224fda81ceSLoGin             SYS_READLINKAT => {
923709498caSLoGin                 let dirfd = args[0] as i32;
924709498caSLoGin                 let pathname = args[1] as *const u8;
925709498caSLoGin                 let buf = args[2] as *mut u8;
926709498caSLoGin                 let bufsiz = args[3] as usize;
927709498caSLoGin                 Self::readlink_at(dirfd, pathname, buf, bufsiz)
928709498caSLoGin             }
929709498caSLoGin 
9300d9b7d92SLoGin             SYS_PRLIMIT64 => {
9310d9b7d92SLoGin                 let pid = args[0];
9320d9b7d92SLoGin                 let pid = Pid::new(pid);
9330d9b7d92SLoGin                 let resource = args[1];
9340d9b7d92SLoGin                 let new_limit = args[2] as *const RLimit64;
9350d9b7d92SLoGin                 let old_limit = args[3] as *mut RLimit64;
9360d9b7d92SLoGin 
9370d9b7d92SLoGin                 Self::prlimit64(pid, resource, new_limit, old_limit)
9380d9b7d92SLoGin             }
9390d9b7d92SLoGin 
9404fda81ceSLoGin             #[cfg(target_arch = "x86_64")]
9419b0abe6dSLoGin             SYS_ACCESS => {
9429b0abe6dSLoGin                 let pathname = args[0] as *const u8;
9439b0abe6dSLoGin                 let mode = args[1] as u32;
9449b0abe6dSLoGin                 Self::access(pathname, mode)
9459b0abe6dSLoGin             }
9469b0abe6dSLoGin 
9479b0abe6dSLoGin             SYS_FACCESSAT => {
9489b0abe6dSLoGin                 let dirfd = args[0] as i32;
9499b0abe6dSLoGin                 let pathname = args[1] as *const u8;
9509b0abe6dSLoGin                 let mode = args[2] as u32;
9519b0abe6dSLoGin                 Self::faccessat2(dirfd, pathname, mode, 0)
9529b0abe6dSLoGin             }
9539b0abe6dSLoGin 
9549b0abe6dSLoGin             SYS_FACCESSAT2 => {
9559b0abe6dSLoGin                 let dirfd = args[0] as i32;
9569b0abe6dSLoGin                 let pathname = args[1] as *const u8;
9579b0abe6dSLoGin                 let mode = args[2] as u32;
9589b0abe6dSLoGin                 let flags = args[3] as u32;
9599b0abe6dSLoGin                 Self::faccessat2(dirfd, pathname, mode, flags)
9609b0abe6dSLoGin             }
9619b0abe6dSLoGin 
962bf4a4899SLoGin             SYS_CLOCK_GETTIME => {
963bf4a4899SLoGin                 let clockid = args[0] as i32;
964bf4a4899SLoGin                 let timespec = args[1] as *mut TimeSpec;
965bf4a4899SLoGin                 Self::clock_gettime(clockid, timespec)
966bf4a4899SLoGin             }
967bf4a4899SLoGin 
968bf4a4899SLoGin             SYS_SYSINFO => {
969bf4a4899SLoGin                 let info = args[0] as *mut SysInfo;
970bf4a4899SLoGin                 Self::sysinfo(info)
971bf4a4899SLoGin             }
972bf4a4899SLoGin 
973bf4a4899SLoGin             SYS_UMASK => {
974bf4a4899SLoGin                 let mask = args[0] as u32;
975bf4a4899SLoGin                 Self::umask(mask)
976bf4a4899SLoGin             }
977bf4a4899SLoGin 
97827b967a3S裕依             SYS_FCHOWN => {
97927b967a3S裕依                 kwarn!("SYS_FCHOWN has not yet been implemented");
98027b967a3S裕依                 Ok(0)
98127b967a3S裕依             }
98227b967a3S裕依 
98327b967a3S裕依             SYS_FSYNC => {
98427b967a3S裕依                 kwarn!("SYS_FSYNC has not yet been implemented");
98527b967a3S裕依                 Ok(0)
98627b967a3S裕依             }
98727b967a3S裕依 
9884fda81ceSLoGin             #[cfg(target_arch = "x86_64")]
989bf4a4899SLoGin             SYS_CHMOD => {
990bf4a4899SLoGin                 let pathname = args[0] as *const u8;
991bf4a4899SLoGin                 let mode = args[1] as u32;
992bf4a4899SLoGin                 Self::chmod(pathname, mode)
993bf4a4899SLoGin             }
994bf4a4899SLoGin             SYS_FCHMOD => {
995bf4a4899SLoGin                 let fd = args[0] as i32;
996bf4a4899SLoGin                 let mode = args[1] as u32;
997bf4a4899SLoGin                 Self::fchmod(fd, mode)
998bf4a4899SLoGin             }
999bf4a4899SLoGin             SYS_FCHMODAT => {
1000bf4a4899SLoGin                 let dirfd = args[0] as i32;
1001bf4a4899SLoGin                 let pathname = args[1] as *const u8;
1002bf4a4899SLoGin                 let mode = args[2] as u32;
1003bf4a4899SLoGin                 Self::fchmodat(dirfd, pathname, mode)
1004bf4a4899SLoGin             }
1005bf4a4899SLoGin 
100602343d0bSLoGin             SYS_SCHED_GETAFFINITY => {
100702343d0bSLoGin                 // todo: 这个系统调用还没有实现
100802343d0bSLoGin 
100902343d0bSLoGin                 Err(SystemError::ENOSYS)
101002343d0bSLoGin             }
101102343d0bSLoGin 
101202343d0bSLoGin             SYS_SCHED_YIELD => Self::sched_yield(),
101302343d0bSLoGin 
1014ab5c8ca4Slogin             _ => panic!("Unsupported syscall ID: {}", syscall_num),
1015ab5c8ca4Slogin         };
10164fda81ceSLoGin 
1017ab5c8ca4Slogin         return r;
1018ab5c8ca4Slogin     }
1019ab5c8ca4Slogin 
1020ab5c8ca4Slogin     pub fn put_string(
1021ab5c8ca4Slogin         s: *const u8,
1022ab5c8ca4Slogin         front_color: u32,
1023ab5c8ca4Slogin         back_color: u32,
1024ab5c8ca4Slogin     ) -> Result<usize, SystemError> {
1025ab5c8ca4Slogin         return Ok(unsafe { do_put_string(s, front_color, back_color) });
1026ab5c8ca4Slogin     }
1027ab5c8ca4Slogin 
1028ab5c8ca4Slogin     pub fn reboot() -> Result<usize, SystemError> {
10294fda81ceSLoGin         unsafe { cpu_reset() };
1030ab5c8ca4Slogin     }
1031ab5c8ca4Slogin }
1032