starry_api/imp/task/
thread.rs1use axerrno::LinuxResult;
2use axtask::{TaskExtRef, current};
3use num_enum::TryFromPrimitive;
4
5pub fn sys_getpid() -> LinuxResult<isize> {
6 Ok(axtask::current().task_ext().thread.process().pid() as _)
7}
8
9pub fn sys_getppid() -> LinuxResult<isize> {
10 Ok(axtask::current()
11 .task_ext()
12 .thread
13 .process()
14 .parent()
15 .unwrap()
16 .pid() as _)
17}
18
19pub fn sys_gettid() -> LinuxResult<isize> {
20 Ok(axtask::current().id().as_u64() as _)
21}
22
23#[derive(Debug, Eq, PartialEq, TryFromPrimitive)]
28#[repr(i32)]
29enum ArchPrctlCode {
30 SetGs = 0x1001,
32 SetFs = 0x1002,
34 GetFs = 0x1003,
36 GetGs = 0x1004,
38 GetCpuid = 0x1011,
40 SetCpuid = 0x1012,
42}
43
44pub fn sys_set_tid_address(clear_child_tid: usize) -> LinuxResult<isize> {
48 let curr = current();
49 curr.task_ext()
50 .thread_data()
51 .set_clear_child_tid(clear_child_tid);
52 Ok(curr.id().as_u64() as isize)
53}
54
55#[cfg(target_arch = "x86_64")]
56pub fn sys_arch_prctl(
57 tf: &mut axhal::arch::TrapFrame,
58 code: i32,
59 addr: usize,
60) -> LinuxResult<isize> {
61 use crate::ptr::UserPtr;
62
63 let code = ArchPrctlCode::try_from(code).map_err(|_| axerrno::LinuxError::EINVAL)?;
64 debug!("sys_arch_prctl: code = {:?}, addr = {:#x}", code, addr);
65
66 match code {
67 ArchPrctlCode::GetFs => {
70 *UserPtr::from(addr).get_as_mut()? = tf.tls();
71 Ok(0)
72 }
73 ArchPrctlCode::SetFs => {
74 tf.set_tls(addr);
75 Ok(0)
76 }
77 ArchPrctlCode::GetGs => {
78 *UserPtr::from(addr).get_as_mut()? =
79 unsafe { x86::msr::rdmsr(x86::msr::IA32_KERNEL_GSBASE) };
80 Ok(0)
81 }
82 ArchPrctlCode::SetGs => {
83 unsafe {
84 x86::msr::wrmsr(x86::msr::IA32_KERNEL_GSBASE, addr as _);
85 }
86 Ok(0)
87 }
88 ArchPrctlCode::GetCpuid => Ok(0),
89 ArchPrctlCode::SetCpuid => Err(axerrno::LinuxError::ENODEV),
90 }
91}