procops.rs 21 KB

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