Skip to content

Commit 984eebe

Browse files
tls: cleanup the FS base restoration
Signed-off-by: Andy-Python-Programmer <[email protected]>
1 parent 659ac94 commit 984eebe

File tree

5 files changed

+17
-27
lines changed

5 files changed

+17
-27
lines changed

src/aero_kernel/src/arch/x86_64/task.asm

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,12 @@ global task_spinup
2020
global iretq_init
2121
global fork_init
2222

23-
extern restore_user_tls
24-
2523
jump_userland_exec:
2624
push rdi ; Param: stack
2725
push rsi ; Param: RIP
2826
push rdx ; Param: RFLAGS
2927

3028
cli
31-
call restore_user_tls
3229

3330
pop r11
3431
pop rcx
@@ -38,10 +35,7 @@ jump_userland_exec:
3835
o64 sysret
3936

4037
fork_init:
41-
cli
42-
call restore_user_tls
4338
swapgs
44-
4539
jmp iretq_init
4640

4741
iretq_init:

src/aero_kernel/src/arch/x86_64/task.rs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ use crate::fs::cache::DirCacheItem;
4545
use crate::mem::paging::*;
4646
use crate::syscall::ExecArgs;
4747
use crate::userland::vm::Vm;
48-
use crate::utils::StackHelper;
48+
use crate::utils::{io, StackHelper};
4949

5050
use super::controlregs;
5151

@@ -327,13 +327,19 @@ impl ArchTask {
327327
Ok(())
328328
}
329329

330-
#[inline]
330+
/// Returns the FS base for this instance.
331331
pub fn get_fs_base(&self) -> VirtAddr {
332332
self.fs_base
333333
}
334334

335-
#[inline]
336-
pub fn set_fs_base(&mut self, base: VirtAddr) {
335+
/// Sets the FS base to the provided `base`.
336+
///
337+
/// ## Safety
338+
/// This function **must** be called by the process that this [`ArchTask`] instance
339+
/// belongs to. This is required since we also update the FS base register with the
340+
/// `base` immediately (not waiting for a switch).
341+
pub unsafe fn set_fs_base(&mut self, base: VirtAddr) {
342+
io::wrmsr(io::IA32_FS_BASE, base.as_u64());
337343
self.fs_base = base;
338344
}
339345
}
@@ -347,5 +353,10 @@ pub fn arch_task_spinup(from: &mut ArchTask, to: &ArchTask) {
347353
unsafe {
348354
super::gdt::get_task_state_segement().rsp[0] = to.context_switch_rsp.as_u64();
349355
task_spinup(&mut from.context, to.context.as_ref());
356+
357+
// make a restore point for the current FS base.
358+
from.fs_base = VirtAddr::new(io::rdmsr(io::IA32_FS_BASE));
359+
// switch to the new FS base.
360+
io::wrmsr(io::IA32_FS_BASE, to.fs_base.as_u64());
350361
}
351362
}

src/aero_kernel/src/arch/x86_64/tls.rs

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ use alloc::vec::Vec;
3333

3434
use super::gdt::*;
3535

36-
use crate::userland::scheduler;
3736
use crate::utils::io;
3837
use crate::utils::sync::Mutex;
3938

@@ -191,15 +190,3 @@ where
191190
f(&info);
192191
}
193192
}
194-
195-
#[no_mangle]
196-
extern "C" fn restore_user_tls() {
197-
unsafe {
198-
let base = scheduler::get_scheduler()
199-
.current_task()
200-
.arch_task_mut()
201-
.get_fs_base();
202-
203-
io::wrmsr(io::IA32_FS_BASE, base.as_u64());
204-
}
205-
}

src/aero_kernel/src/syscall/handler.asm

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,7 @@ syscall_handler:
8686

8787
cld
8888
call __inner_syscall
89-
9089
cli
91-
call restore_user_tls
9290

9391
; pop the preserved registers
9492
pop r15

src/aero_kernel/src/syscall/process.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,14 +59,14 @@ const ARCH_GET_GS: usize = 0x1004;
5959

6060
pub fn arch_prctl(command: usize, address: usize) -> Result<usize, AeroSyscallError> {
6161
match command {
62-
ARCH_SET_FS => {
62+
ARCH_SET_FS => unsafe {
6363
scheduler::get_scheduler()
6464
.current_task()
6565
.arch_task_mut()
6666
.set_fs_base(VirtAddr::new(address as u64));
6767

6868
Ok(0x00)
69-
}
69+
},
7070

7171
ARCH_GET_FS => Ok(scheduler::get_scheduler()
7272
.current_task()

0 commit comments

Comments
 (0)