procops.rs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795
  1. use core::time::Duration;
  2. use super::sysinfo::TimeVal;
  3. use super::SyscallNoReturn;
  4. use crate::io::Buffer;
  5. use crate::kernel::constants::{EINVAL, ENOENT, ENOTDIR, ERANGE, ESRCH};
  6. use crate::kernel::constants::{
  7. ENOSYS, PR_GET_NAME, PR_SET_NAME, RLIMIT_STACK, SIG_BLOCK, SIG_SETMASK, SIG_UNBLOCK,
  8. };
  9. use crate::kernel::mem::PageBuffer;
  10. use crate::kernel::task::{
  11. do_clone, futex_wait, futex_wake, FutexFlags, FutexOp, ProcessList, ProgramLoader,
  12. SignalAction, Thread, WaitType,
  13. };
  14. use crate::kernel::task::{parse_futexop, CloneArgs};
  15. use crate::kernel::timer::sleep;
  16. use crate::kernel::user::dataflow::{CheckedUserPointer, UserString};
  17. use crate::kernel::user::{UserPointer, UserPointerMut};
  18. use crate::kernel::vfs::{self, dentry::Dentry};
  19. use crate::path::Path;
  20. use crate::{kernel::user::dataflow::UserBuffer, prelude::*};
  21. use alloc::borrow::ToOwned;
  22. use alloc::ffi::CString;
  23. use bitflags::bitflags;
  24. use core::ptr::NonNull;
  25. use eonix_hal::processor::UserTLS;
  26. use eonix_hal::traits::trap::RawTrapContext;
  27. use eonix_mm::address::Addr as _;
  28. use eonix_runtime::task::Task;
  29. use eonix_sync::AsProof as _;
  30. use posix_types::constants::{P_ALL, P_PID};
  31. use posix_types::ctypes::PtrT;
  32. use posix_types::signal::{SigAction, SigInfo, SigSet, Signal};
  33. use posix_types::{syscall_no::*, SIGNAL_NOW};
  34. #[repr(C)]
  35. #[derive(Debug, Clone, Copy)]
  36. struct RLimit {
  37. rlim_cur: u64,
  38. rlim_max: u64,
  39. }
  40. bitflags! {
  41. pub struct UserWaitOptions: u32 {
  42. const WNOHANG = 1;
  43. const WUNTRACED = 2;
  44. const WCONTINUED = 8;
  45. }
  46. }
  47. #[eonix_macros::define_syscall(SYS_NANOSLEEP)]
  48. fn nanosleep(req: *const (u32, u32), rem: *mut (u32, u32)) -> KResult<usize> {
  49. let req = UserPointer::new(req)?.read()?;
  50. let rem = if rem.is_null() {
  51. None
  52. } else {
  53. Some(UserPointerMut::new(rem)?)
  54. };
  55. let duration = Duration::from_secs(req.0 as u64) + Duration::from_nanos(req.1 as u64);
  56. Task::block_on(sleep(duration));
  57. if let Some(rem) = rem {
  58. rem.write((0, 0))?;
  59. }
  60. Ok(0)
  61. }
  62. #[eonix_macros::define_syscall(SYS_UMASK)]
  63. fn umask(mask: u32) -> KResult<u32> {
  64. let mut umask = thread.fs_context.umask.lock();
  65. let old = *umask;
  66. *umask = mask & 0o777;
  67. Ok(old)
  68. }
  69. #[eonix_macros::define_syscall(SYS_GETCWD)]
  70. fn getcwd(buffer: *mut u8, bufsize: usize) -> KResult<usize> {
  71. let mut user_buffer = UserBuffer::new(buffer, bufsize)?;
  72. let mut buffer = PageBuffer::new();
  73. thread
  74. .fs_context
  75. .cwd
  76. .lock()
  77. .get_path(&thread.fs_context, &mut buffer)?;
  78. user_buffer.fill(buffer.data())?.ok_or(ERANGE)?;
  79. Ok(buffer.wrote())
  80. }
  81. #[eonix_macros::define_syscall(SYS_CHDIR)]
  82. fn chdir(path: *const u8) -> KResult<()> {
  83. let path = UserString::new(path)?;
  84. let path = Path::new(path.as_cstr().to_bytes())?;
  85. let dentry = Dentry::open(&thread.fs_context, path, true)?;
  86. if !dentry.is_valid() {
  87. return Err(ENOENT);
  88. }
  89. if !dentry.is_directory() {
  90. return Err(ENOTDIR);
  91. }
  92. *thread.fs_context.cwd.lock() = dentry;
  93. Ok(())
  94. }
  95. #[eonix_macros::define_syscall(SYS_MOUNT)]
  96. fn mount(source: *const u8, target: *const u8, fstype: *const u8, flags: usize) -> KResult<()> {
  97. let source = UserString::new(source)?;
  98. let target = UserString::new(target)?;
  99. let fstype = UserString::new(fstype)?;
  100. let mountpoint = Dentry::open(
  101. &thread.fs_context,
  102. Path::new(target.as_cstr().to_bytes())?,
  103. true,
  104. )?;
  105. if !mountpoint.is_valid() {
  106. return Err(ENOENT);
  107. }
  108. vfs::mount::do_mount(
  109. &mountpoint,
  110. source.as_cstr().to_str().map_err(|_| EINVAL)?,
  111. target.as_cstr().to_str().map_err(|_| EINVAL)?,
  112. fstype.as_cstr().to_str().map_err(|_| EINVAL)?,
  113. flags as u64,
  114. )
  115. }
  116. fn get_strings(mut ptr_strings: UserPointer<'_, PtrT>) -> KResult<Vec<CString>> {
  117. let mut strings = Vec::new();
  118. loop {
  119. let ptr = ptr_strings.read()?;
  120. if ptr.is_null() {
  121. break;
  122. }
  123. let user_string = UserString::new(ptr.addr() as *const u8)?;
  124. strings.push(user_string.as_cstr().to_owned());
  125. ptr_strings = ptr_strings.offset(1)?;
  126. }
  127. Ok(strings)
  128. }
  129. #[eonix_macros::define_syscall(SYS_EXECVE)]
  130. fn execve(exec: *const u8, argv: *const PtrT, envp: *const PtrT) -> KResult<SyscallNoReturn> {
  131. let exec = UserString::new(exec)?;
  132. let argv = get_strings(UserPointer::new(argv)?)?;
  133. let envp = get_strings(UserPointer::new(envp)?)?;
  134. let dentry = Dentry::open(
  135. &thread.fs_context,
  136. Path::new(exec.as_cstr().to_bytes())?,
  137. true,
  138. )?;
  139. if !dentry.is_valid() {
  140. Err(ENOENT)?;
  141. }
  142. // TODO: When `execve` is called by one of the threads in a process, the other threads
  143. // should be terminated and `execve` is performed in the thread group leader.
  144. let load_info = ProgramLoader::parse(dentry.clone())?.load(argv, envp)?;
  145. unsafe {
  146. // SAFETY: We are doing execve, all other threads are terminated.
  147. thread.process.mm_list.replace(Some(load_info.mm_list));
  148. }
  149. thread.files.on_exec();
  150. thread.signal_list.clear_non_ignore();
  151. thread.set_name(dentry.name().clone());
  152. let mut trap_ctx = thread.trap_ctx.borrow();
  153. trap_ctx.set_program_counter(load_info.entry_ip.addr());
  154. trap_ctx.set_stack_pointer(load_info.sp.addr());
  155. Ok(SyscallNoReturn)
  156. }
  157. #[eonix_macros::define_syscall(SYS_EXIT)]
  158. fn exit(status: u32) -> SyscallNoReturn {
  159. unsafe {
  160. let mut procs = Task::block_on(ProcessList::get().write());
  161. Task::block_on(procs.do_exit(&thread, WaitType::Exited(status), false));
  162. }
  163. SyscallNoReturn
  164. }
  165. #[eonix_macros::define_syscall(SYS_EXIT_GROUP)]
  166. fn exit_group(status: u32) -> SyscallNoReturn {
  167. unsafe {
  168. let mut procs = Task::block_on(ProcessList::get().write());
  169. Task::block_on(procs.do_exit(&thread, WaitType::Exited(status), true));
  170. }
  171. SyscallNoReturn
  172. }
  173. enum WaitInfo {
  174. SigInfo(NonNull<SigInfo>),
  175. Status(NonNull<u32>),
  176. None,
  177. }
  178. fn do_waitid(
  179. thread: &Thread,
  180. id_type: u32,
  181. _id: u32,
  182. info: WaitInfo,
  183. options: u32,
  184. rusage: *mut RUsage,
  185. ) -> KResult<u32> {
  186. if id_type != P_ALL {
  187. unimplemented!("waitid with id_type {id_type}");
  188. }
  189. if !rusage.is_null() {
  190. unimplemented!("waitid with rusage pointer");
  191. }
  192. let options = match UserWaitOptions::from_bits(options) {
  193. None => unimplemented!("waitpid with options {options}"),
  194. Some(options) => options,
  195. };
  196. let Some(wait_object) = Task::block_on(thread.process.wait(
  197. options.contains(UserWaitOptions::WNOHANG),
  198. options.contains(UserWaitOptions::WUNTRACED),
  199. options.contains(UserWaitOptions::WCONTINUED),
  200. ))?
  201. else {
  202. return Ok(0);
  203. };
  204. match info {
  205. WaitInfo::SigInfo(siginfo_ptr) => {
  206. let (status, code) = wait_object.code.to_status_code();
  207. let mut siginfo = SigInfo::default();
  208. siginfo.si_pid = wait_object.pid;
  209. siginfo.si_uid = 0; // All users are root for now.
  210. siginfo.si_signo = Signal::SIGCHLD.into_raw();
  211. siginfo.si_status = status;
  212. siginfo.si_code = code;
  213. UserPointerMut::new(siginfo_ptr.as_ptr())?.write(siginfo)?;
  214. Ok(0)
  215. }
  216. WaitInfo::Status(status_ptr) => {
  217. UserPointerMut::new(status_ptr.as_ptr())?.write(wait_object.code.to_wstatus())?;
  218. Ok(wait_object.pid)
  219. }
  220. WaitInfo::None => Ok(wait_object.pid),
  221. }
  222. }
  223. #[eonix_macros::define_syscall(SYS_WAITID)]
  224. fn waitid(
  225. id_type: u32,
  226. id: u32,
  227. info: *mut SigInfo,
  228. options: u32,
  229. rusage: *mut RUsage,
  230. ) -> KResult<u32> {
  231. if let Some(info) = NonNull::new(info) {
  232. do_waitid(
  233. thread,
  234. id_type,
  235. id,
  236. WaitInfo::SigInfo(info),
  237. options,
  238. rusage,
  239. )
  240. } else {
  241. /*
  242. * According to POSIX.1-2008, an application calling waitid() must
  243. * ensure that infop points to a siginfo_t structure (i.e., that it
  244. * is a non-null pointer). On Linux, if infop is NULL, waitid()
  245. * succeeds, and returns the process ID of the waited-for child.
  246. * Applications should avoid relying on this inconsistent,
  247. * nonstandard, and unnecessary feature.
  248. */
  249. unimplemented!("waitid with null info pointer");
  250. }
  251. }
  252. #[eonix_macros::define_syscall(SYS_WAIT4)]
  253. fn wait4(waitpid: u32, arg1: *mut u32, options: u32, rusage: *mut RUsage) -> KResult<u32> {
  254. let waitinfo = if let Some(status) = NonNull::new(arg1) {
  255. WaitInfo::Status(status)
  256. } else {
  257. WaitInfo::None
  258. };
  259. let idtype = match waitpid {
  260. u32::MAX => P_ALL,
  261. _ => P_PID,
  262. };
  263. do_waitid(thread, idtype, waitpid, waitinfo, options, rusage)
  264. }
  265. #[cfg(target_arch = "x86_64")]
  266. #[eonix_macros::define_syscall(SYS_WAITPID)]
  267. fn waitpid(waitpid: u32, arg1: *mut u32, options: u32) -> KResult<u32> {
  268. sys_wait4(thread, waitpid, arg1, options, core::ptr::null_mut())
  269. }
  270. #[eonix_macros::define_syscall(SYS_SETSID)]
  271. fn setsid() -> KResult<u32> {
  272. thread.process.setsid()
  273. }
  274. #[eonix_macros::define_syscall(SYS_SETPGID)]
  275. fn setpgid(pid: u32, pgid: i32) -> KResult<()> {
  276. let pid = if pid == 0 { thread.process.pid } else { pid };
  277. let pgid = match pgid {
  278. 0 => pid,
  279. 1.. => pgid as u32,
  280. _ => return Err(EINVAL),
  281. };
  282. thread.process.setpgid(pid, pgid)
  283. }
  284. #[eonix_macros::define_syscall(SYS_GETSID)]
  285. fn getsid(pid: u32) -> KResult<u32> {
  286. if pid == 0 {
  287. Ok(thread.process.session_rcu().sid)
  288. } else {
  289. let procs = Task::block_on(ProcessList::get().read());
  290. procs
  291. .try_find_process(pid)
  292. .map(|proc| proc.session(procs.prove()).sid)
  293. .ok_or(ESRCH)
  294. }
  295. }
  296. #[eonix_macros::define_syscall(SYS_GETPGID)]
  297. fn getpgid(pid: u32) -> KResult<u32> {
  298. if pid == 0 {
  299. Ok(thread.process.pgroup_rcu().pgid)
  300. } else {
  301. let procs = Task::block_on(ProcessList::get().read());
  302. procs
  303. .try_find_process(pid)
  304. .map(|proc| proc.pgroup(procs.prove()).pgid)
  305. .ok_or(ESRCH)
  306. }
  307. }
  308. #[eonix_macros::define_syscall(SYS_GETPID)]
  309. fn getpid() -> KResult<u32> {
  310. Ok(thread.process.pid)
  311. }
  312. #[eonix_macros::define_syscall(SYS_GETPPID)]
  313. fn getppid() -> KResult<u32> {
  314. Ok(thread.process.parent_rcu().map_or(0, |x| x.pid))
  315. }
  316. fn do_geteuid(_thread: &Thread) -> KResult<u32> {
  317. // All users are root for now.
  318. Ok(0)
  319. }
  320. fn do_getuid(_thread: &Thread) -> KResult<u32> {
  321. // All users are root for now.
  322. Ok(0)
  323. }
  324. #[cfg(target_arch = "x86_64")]
  325. #[eonix_macros::define_syscall(SYS_GETUID32)]
  326. fn getuid32() -> KResult<u32> {
  327. do_getuid(thread)
  328. }
  329. #[eonix_macros::define_syscall(SYS_GETUID)]
  330. fn getuid() -> KResult<u32> {
  331. do_getuid(thread)
  332. }
  333. #[cfg(target_arch = "x86_64")]
  334. #[eonix_macros::define_syscall(SYS_GETEUID32)]
  335. fn geteuid32() -> KResult<u32> {
  336. do_geteuid(thread)
  337. }
  338. #[eonix_macros::define_syscall(SYS_GETEUID)]
  339. fn geteuid() -> KResult<u32> {
  340. do_geteuid(thread)
  341. }
  342. #[eonix_macros::define_syscall(SYS_GETGID)]
  343. fn getgid() -> KResult<u32> {
  344. // All users are root for now.
  345. Ok(0)
  346. }
  347. #[cfg(target_arch = "x86_64")]
  348. #[eonix_macros::define_syscall(SYS_GETGID32)]
  349. fn getgid32() -> KResult<u32> {
  350. sys_getgid(thread)
  351. }
  352. #[eonix_macros::define_syscall(SYS_GETTID)]
  353. fn gettid() -> KResult<u32> {
  354. Ok(thread.tid)
  355. }
  356. pub fn parse_user_tls(arch_tls: usize) -> KResult<UserTLS> {
  357. #[cfg(target_arch = "x86_64")]
  358. {
  359. let desc = arch_tls as *mut posix_types::x86_64::UserDescriptor;
  360. let desc_pointer = UserPointerMut::new(desc)?;
  361. let mut desc = desc_pointer.read()?;
  362. // Clear the TLS area if it is not present.
  363. if desc.flags.is_read_exec_only() && !desc.flags.is_present() {
  364. if desc.limit != 0 && desc.base != 0 {
  365. let len = if desc.flags.is_limit_in_pages() {
  366. (desc.limit as usize) << 12
  367. } else {
  368. desc.limit as usize
  369. };
  370. CheckedUserPointer::new(desc.base as _, len)?.zero()?;
  371. }
  372. }
  373. let (new_tls, entry) =
  374. UserTLS::new32(desc.base, desc.limit, desc.flags.is_limit_in_pages());
  375. desc.entry = entry;
  376. desc_pointer.write(desc)?;
  377. Ok(new_tls)
  378. }
  379. #[cfg(target_arch = "riscv64")]
  380. {
  381. Ok(UserTLS::new(arch_tls as u64))
  382. }
  383. }
  384. #[cfg(target_arch = "x86_64")]
  385. #[eonix_macros::define_syscall(SYS_SET_THREAD_AREA)]
  386. fn set_thread_area(arch_tls: usize) -> KResult<()> {
  387. thread.set_user_tls(parse_user_tls(arch_tls)?)?;
  388. // SAFETY: Preemption is disabled on calling `load_thread_area32()`.
  389. unsafe {
  390. eonix_preempt::disable();
  391. thread.load_thread_area32();
  392. eonix_preempt::enable();
  393. }
  394. Ok(())
  395. }
  396. #[eonix_macros::define_syscall(SYS_SET_TID_ADDRESS)]
  397. fn set_tid_address(tidptr: usize) -> KResult<u32> {
  398. thread.clear_child_tid(Some(tidptr));
  399. Ok(thread.tid)
  400. }
  401. #[eonix_macros::define_syscall(SYS_PRCTL)]
  402. fn prctl(option: u32, arg2: usize) -> KResult<()> {
  403. match option {
  404. PR_SET_NAME => {
  405. let name = UserPointer::new(arg2 as *mut [u8; 16])?.read()?;
  406. let len = name.iter().position(|&c| c == 0).unwrap_or(15);
  407. thread.set_name(name[..len].into());
  408. Ok(())
  409. }
  410. PR_GET_NAME => {
  411. let name = thread.get_name();
  412. let len = name.len().min(15);
  413. let name: [u8; 16] = core::array::from_fn(|i| if i < len { name[i] } else { 0 });
  414. UserPointerMut::new(arg2 as *mut [u8; 16])?.write(name)?;
  415. Ok(())
  416. }
  417. _ => Err(EINVAL),
  418. }
  419. }
  420. #[eonix_macros::define_syscall(SYS_KILL)]
  421. fn kill(pid: i32, sig: u32) -> KResult<()> {
  422. let procs = Task::block_on(ProcessList::get().read());
  423. match pid {
  424. // Send signal to every process for which the calling process has
  425. // permission to send signals.
  426. -1 => unimplemented!("kill with pid -1"),
  427. // Send signal to every process in the process group.
  428. 0 => thread
  429. .process
  430. .pgroup(procs.prove())
  431. .raise(Signal::try_from_raw(sig)?, procs.prove()),
  432. // Send signal to the process with the specified pid.
  433. 1.. => procs
  434. .try_find_process(pid as u32)
  435. .ok_or(ESRCH)?
  436. .raise(Signal::try_from_raw(sig)?, procs.prove()),
  437. // Send signal to the process group with the specified pgid equals to `-pid`.
  438. ..-1 => procs
  439. .try_find_pgroup((-pid) as u32)
  440. .ok_or(ESRCH)?
  441. .raise(Signal::try_from_raw(sig)?, procs.prove()),
  442. }
  443. Ok(())
  444. }
  445. #[eonix_macros::define_syscall(SYS_TKILL)]
  446. fn tkill(tid: u32, sig: u32) -> KResult<()> {
  447. Task::block_on(ProcessList::get().read())
  448. .try_find_thread(tid)
  449. .ok_or(ESRCH)?
  450. .raise(Signal::try_from_raw(sig)?);
  451. Ok(())
  452. }
  453. #[eonix_macros::define_syscall(SYS_RT_SIGPROCMASK)]
  454. fn rt_sigprocmask(
  455. how: u32,
  456. set: *mut SigSet,
  457. oldset: *mut SigSet,
  458. sigsetsize: usize,
  459. ) -> KResult<()> {
  460. if sigsetsize != size_of::<SigSet>() {
  461. return Err(EINVAL);
  462. }
  463. let old_mask = thread.signal_list.get_mask();
  464. if !oldset.is_null() {
  465. UserPointerMut::new(oldset)?.write(old_mask)?;
  466. }
  467. let new_mask = if !set.is_null() {
  468. UserPointer::new(set)?.read()?
  469. } else {
  470. return Ok(());
  471. };
  472. match how {
  473. SIG_BLOCK => thread.signal_list.mask(new_mask),
  474. SIG_UNBLOCK => thread.signal_list.unmask(new_mask),
  475. SIG_SETMASK => thread.signal_list.set_mask(new_mask),
  476. _ => return Err(EINVAL),
  477. }
  478. Ok(())
  479. }
  480. #[eonix_macros::define_syscall(SYS_RT_SIGACTION)]
  481. fn rt_sigaction(
  482. signum: u32,
  483. act: *const SigAction,
  484. oldact: *mut SigAction,
  485. sigsetsize: usize,
  486. ) -> KResult<()> {
  487. let signal = Signal::try_from_raw(signum)?;
  488. if sigsetsize != size_of::<u64>() {
  489. return Err(EINVAL);
  490. }
  491. // SIGKILL and SIGSTOP MUST not be set for a handler.
  492. if matches!(signal, SIGNAL_NOW!()) {
  493. return Err(EINVAL);
  494. }
  495. let old_action = thread.signal_list.get_action(signal);
  496. if !oldact.is_null() {
  497. UserPointerMut::new(oldact)?.write(old_action.into())?;
  498. }
  499. if !act.is_null() {
  500. let new_action = UserPointer::new(act)?.read()?;
  501. let action: SignalAction = new_action.try_into()?;
  502. thread.signal_list.set_action(signal, action)?;
  503. }
  504. Ok(())
  505. }
  506. #[eonix_macros::define_syscall(SYS_PRLIMIT64)]
  507. fn prlimit64(
  508. pid: u32,
  509. resource: u32,
  510. new_limit: *const RLimit,
  511. old_limit: *mut RLimit,
  512. ) -> KResult<()> {
  513. if pid != 0 {
  514. return Err(ENOSYS);
  515. }
  516. match resource {
  517. RLIMIT_STACK => {
  518. if !old_limit.is_null() {
  519. let old_limit = UserPointerMut::new(old_limit)?;
  520. let rlimit = RLimit {
  521. rlim_cur: 8 * 1024 * 1024,
  522. rlim_max: 8 * 1024 * 1024,
  523. };
  524. old_limit.write(rlimit)?;
  525. }
  526. if !new_limit.is_null() {
  527. return Err(ENOSYS);
  528. }
  529. Ok(())
  530. }
  531. _ => Err(ENOSYS),
  532. }
  533. }
  534. #[eonix_macros::define_syscall(SYS_GETRLIMIT)]
  535. fn getrlimit(resource: u32, rlimit: *mut RLimit) -> KResult<()> {
  536. sys_prlimit64(thread, 0, resource, core::ptr::null(), rlimit)
  537. }
  538. #[repr(C)]
  539. #[derive(Clone, Copy)]
  540. struct RUsage {
  541. ru_utime: TimeVal,
  542. ru_stime: TimeVal,
  543. ru_maxrss: u32,
  544. ru_ixrss: u32,
  545. ru_idrss: u32,
  546. ru_isrss: u32,
  547. ru_minflt: u32,
  548. ru_majflt: u32,
  549. ru_nswap: u32,
  550. ru_inblock: u32,
  551. ru_oublock: u32,
  552. ru_msgsnd: u32,
  553. ru_msgrcv: u32,
  554. ru_nsignals: u32,
  555. ru_nvcsw: u32,
  556. ru_nivcsw: u32,
  557. }
  558. #[eonix_macros::define_syscall(SYS_GETRUSAGE)]
  559. fn getrusage(who: u32, rusage: *mut RUsage) -> KResult<()> {
  560. if who != 0 {
  561. return Err(ENOSYS);
  562. }
  563. let rusage = UserPointerMut::new(rusage)?;
  564. rusage.write(RUsage {
  565. ru_utime: TimeVal::default(),
  566. ru_stime: TimeVal::default(),
  567. ru_maxrss: 0,
  568. ru_ixrss: 0,
  569. ru_idrss: 0,
  570. ru_isrss: 0,
  571. ru_minflt: 0,
  572. ru_majflt: 0,
  573. ru_nswap: 0,
  574. ru_inblock: 0,
  575. ru_oublock: 0,
  576. ru_msgsnd: 0,
  577. ru_msgrcv: 0,
  578. ru_nsignals: 0,
  579. ru_nvcsw: 0,
  580. ru_nivcsw: 0,
  581. })?;
  582. Ok(())
  583. }
  584. #[cfg(target_arch = "x86_64")]
  585. #[eonix_macros::define_syscall(SYS_VFORK)]
  586. fn vfork() -> KResult<u32> {
  587. let clone_args = CloneArgs::for_vfork();
  588. do_clone(thread, clone_args)
  589. }
  590. #[cfg(target_arch = "x86_64")]
  591. #[eonix_macros::define_syscall(SYS_FORK)]
  592. fn fork() -> KResult<u32> {
  593. let clone_args = CloneArgs::for_fork();
  594. do_clone(thread, clone_args)
  595. }
  596. #[eonix_macros::define_syscall(SYS_CLONE)]
  597. fn clone(
  598. clone_flags: usize,
  599. new_sp: usize,
  600. parent_tidptr: usize,
  601. tls: usize,
  602. child_tidptr: usize,
  603. ) -> KResult<u32> {
  604. let clone_args = CloneArgs::for_clone(clone_flags, new_sp, child_tidptr, parent_tidptr, tls)?;
  605. do_clone(thread, clone_args)
  606. }
  607. #[eonix_macros::define_syscall(SYS_FUTEX)]
  608. fn futex(
  609. uaddr: usize,
  610. op: u32,
  611. val: u32,
  612. _time_out: usize,
  613. _uaddr2: usize,
  614. _val3: u32,
  615. ) -> KResult<usize> {
  616. let (futex_op, futex_flag) = parse_futexop(op)?;
  617. let pid = if futex_flag.contains(FutexFlags::FUTEX_PRIVATE) {
  618. Some(thread.process.pid)
  619. } else {
  620. None
  621. };
  622. match futex_op {
  623. FutexOp::FUTEX_WAIT => {
  624. Task::block_on(futex_wait(uaddr, pid, val as u32, None))?;
  625. return Ok(0);
  626. }
  627. FutexOp::FUTEX_WAKE => {
  628. return Task::block_on(futex_wake(uaddr, pid, val as u32));
  629. }
  630. FutexOp::FUTEX_REQUEUE => {
  631. todo!()
  632. }
  633. _ => {
  634. todo!()
  635. }
  636. }
  637. }
  638. #[eonix_macros::define_syscall(SYS_RT_SIGRETURN)]
  639. fn rt_sigreturn() -> KResult<SyscallNoReturn> {
  640. thread
  641. .signal_list
  642. .restore(
  643. &mut thread.trap_ctx.borrow(),
  644. &mut thread.fpu_state.borrow(),
  645. false,
  646. )
  647. .inspect_err(|err| {
  648. println_warn!(
  649. "`rt_sigreturn` failed in thread {} with error {err}!",
  650. thread.tid
  651. );
  652. Task::block_on(thread.force_kill(Signal::SIGSEGV));
  653. })?;
  654. Ok(SyscallNoReturn)
  655. }
  656. #[cfg(target_arch = "x86_64")]
  657. #[eonix_macros::define_syscall(SYS_SIGRETURN)]
  658. fn sigreturn() -> KResult<SyscallNoReturn> {
  659. thread
  660. .signal_list
  661. .restore(
  662. &mut thread.trap_ctx.borrow(),
  663. &mut thread.fpu_state.borrow(),
  664. true,
  665. )
  666. .inspect_err(|err| {
  667. println_warn!(
  668. "`sigreturn` failed in thread {} with error {err}!",
  669. thread.tid
  670. );
  671. Task::block_on(thread.force_kill(Signal::SIGSEGV));
  672. })?;
  673. Ok(SyscallNoReturn)
  674. }
  675. #[cfg(target_arch = "x86_64")]
  676. #[eonix_macros::define_syscall(SYS_ARCH_PRCTL)]
  677. fn arch_prctl(option: u32, addr: u32) -> KResult<u32> {
  678. sys_arch_prctl(thread, option, addr)
  679. }
  680. pub fn keep_alive() {}