Skip to content
This repository was archived by the owner on Nov 26, 2025. It is now read-only.

Commit 58a40b1

Browse files
committed
fix: tls
1 parent 0c84473 commit 58a40b1

3 files changed

Lines changed: 35 additions & 45 deletions

File tree

api/src/imp/task/clone.rs

Lines changed: 7 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ bitflags! {
8484

8585
#[apply(syscall_instrument)]
8686
pub fn sys_clone(
87+
tf: &TrapFrame,
8788
flags: u32,
8889
stack: usize,
8990
parent_tid: usize,
@@ -103,23 +104,12 @@ pub fn sys_clone(
103104
return Err(LinuxError::EINVAL);
104105
}
105106

106-
let curr = current();
107-
108-
let trap_frame = read_trapframe_from_kstack(curr.get_kernel_stack_top().unwrap());
109-
let mut new_uctx = UspaceContext::from(&trap_frame);
107+
let mut new_uctx = UspaceContext::from(tf);
110108
if stack != 0 {
111109
new_uctx.set_sp(stack);
112110
}
113-
// Skip current instruction
114-
// FIXME: we should do this in arceos before calling `handle_syscall`.
115-
// See: https://github.com/oscomp/arceos/commit/13ff3f58bd825c37ea5eef3393a4f8c0bb5b4f41
116-
#[cfg(any(target_arch = "riscv64", target_arch = "loongarch64"))]
117-
{
118-
let new_uctx_ip = new_uctx.ip();
119-
new_uctx.set_ip(new_uctx_ip + 4);
120-
}
121111
if flags.contains(CloneFlags::SETTLS) {
122-
warn!("sys_clone: CLONE_SETTLS is not supported yet");
112+
new_uctx.set_tls(tls);
123113
}
124114
new_uctx.set_retval(0);
125115

@@ -128,13 +118,9 @@ pub fn sys_clone(
128118
} else {
129119
None
130120
};
131-
let mut new_task = new_user_task(curr.name(), new_uctx, set_child_tid);
132121

133-
// FIXME: remove this when we fix the tls issue
134-
#[cfg(any(target_arch = "x86_64", target_arch = "aarch64"))]
135-
new_task
136-
.ctx_mut()
137-
.set_tls(axhal::arch::read_thread_pointer().into());
122+
let curr = current();
123+
let mut new_task = new_user_task(curr.name(), new_uctx, set_child_tid);
138124

139125
let tid = new_task.id().as_u64() as Pid;
140126
if flags.contains(CloneFlags::PARENT_SETTID) {
@@ -218,12 +204,6 @@ pub fn sys_clone(
218204
Ok(tid as _)
219205
}
220206

221-
fn read_trapframe_from_kstack(kstack_top: usize) -> TrapFrame {
222-
let trap_frame_size = core::mem::size_of::<TrapFrame>();
223-
let trap_frame_ptr = (kstack_top - trap_frame_size) as *mut TrapFrame;
224-
unsafe { *trap_frame_ptr }
225-
}
226-
227-
pub fn sys_fork() -> LinuxResult<isize> {
228-
sys_clone(SIGCHLD, 0, 0, 0, 0)
207+
pub fn sys_fork(tf: &TrapFrame) -> LinuxResult<isize> {
208+
sys_clone(tf, SIGCHLD, 0, 0, 0, 0)
229209
}

api/src/imp/task/thread.rs

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -61,33 +61,36 @@ pub fn sys_set_tid_address(clear_child_tid: usize) -> LinuxResult<isize> {
6161

6262
#[cfg(target_arch = "x86_64")]
6363
#[apply(syscall_instrument)]
64-
pub fn sys_arch_prctl(code: i32, addr: crate::ptr::UserPtr<u64>) -> LinuxResult<isize> {
65-
use crate::ptr::PtrWrapper;
66-
match ArchPrctlCode::try_from(code).map_err(|_| axerrno::LinuxError::EINVAL)? {
64+
pub fn sys_arch_prctl(tf: &mut axhal::arch::TrapFrame) -> LinuxResult<isize> {
65+
use crate::ptr::{PtrWrapper, UserPtr};
66+
67+
let code =
68+
ArchPrctlCode::try_from(tf.arg0() as i32).map_err(|_| axerrno::LinuxError::EINVAL)?;
69+
let addr = tf.arg1();
70+
debug!("sys_arch_prctl: code = {:?}, addr = {:#x}", code, addr);
71+
72+
match code {
6773
// According to Linux implementation, SetFs & SetGs does not return
6874
// error at all
69-
ArchPrctlCode::SetFs => {
75+
ArchPrctlCode::GetFs => {
7076
unsafe {
71-
axhal::arch::write_thread_pointer(addr.address().as_usize());
77+
*UserPtr::from(addr).get()? = tf.tls();
7278
}
7379
Ok(0)
7480
}
75-
ArchPrctlCode::SetGs => {
76-
unsafe {
77-
x86::msr::wrmsr(x86::msr::IA32_KERNEL_GSBASE, addr.address().as_usize() as _);
78-
}
81+
ArchPrctlCode::SetFs => {
82+
tf.set_tls(addr);
7983
Ok(0)
8084
}
81-
ArchPrctlCode::GetFs => {
85+
ArchPrctlCode::GetGs => {
8286
unsafe {
83-
*addr.get()? = axhal::arch::read_thread_pointer() as u64;
87+
*UserPtr::from(addr).get()? = x86::msr::rdmsr(x86::msr::IA32_KERNEL_GSBASE);
8488
}
8589
Ok(0)
8690
}
87-
88-
ArchPrctlCode::GetGs => {
91+
ArchPrctlCode::SetGs => {
8992
unsafe {
90-
*addr.get()? = x86::msr::rdmsr(x86::msr::IA32_KERNEL_GSBASE);
93+
x86::msr::wrmsr(x86::msr::IA32_KERNEL_GSBASE, addr as _);
9194
}
9295
Ok(0)
9396
}

src/syscall.rs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use starry_core::task::{time_stat_from_kernel_to_user, time_stat_from_user_to_ke
88
use syscalls::Sysno;
99

1010
#[register_trap_handler(SYSCALL)]
11-
fn handle_syscall(tf: &TrapFrame, syscall_num: usize) -> isize {
11+
fn handle_syscall(tf: &mut TrapFrame, syscall_num: usize) -> isize {
1212
let sysno = Sysno::from(syscall_num as u32);
1313
info!("Syscall {}", sysno);
1414
time_stat_from_user_to_kernel();
@@ -44,10 +44,17 @@ fn handle_syscall(tf: &TrapFrame, syscall_num: usize) -> isize {
4444
} else {
4545
(tf.arg4() as _, tf.arg3() as _)
4646
};
47-
sys_clone(tf.arg0() as _, tf.arg1() as _, tf.arg2(), child_tid, tls)
47+
sys_clone(
48+
tf,
49+
tf.arg0() as _,
50+
tf.arg1() as _,
51+
tf.arg2(),
52+
child_tid,
53+
tls,
54+
)
4855
}
4956
#[cfg(target_arch = "x86_64")]
50-
Sysno::fork => sys_fork(),
57+
Sysno::fork => sys_fork(tf),
5158
Sysno::wait4 => sys_waitpid(tf.arg0() as _, tf.arg1().into(), tf.arg2() as _),
5259
Sysno::pipe2 => sys_pipe2(tf.arg0().into()),
5360
Sysno::close => sys_close(tf.arg0() as _),
@@ -107,7 +114,7 @@ fn handle_syscall(tf: &TrapFrame, syscall_num: usize) -> isize {
107114
Sysno::times => sys_times(tf.arg0().into()),
108115
Sysno::brk => sys_brk(tf.arg0() as _),
109116
#[cfg(target_arch = "x86_64")]
110-
Sysno::arch_prctl => sys_arch_prctl(tf.arg0() as _, tf.arg1().into()),
117+
Sysno::arch_prctl => sys_arch_prctl(tf),
111118
Sysno::set_tid_address => sys_set_tid_address(tf.arg0()),
112119
Sysno::clock_gettime => sys_clock_gettime(tf.arg0() as _, tf.arg1().into()),
113120
Sysno::getuid => sys_getuid(),

0 commit comments

Comments
 (0)