Replace most of the current_thread! usages via Context

This commit is contained in:
Zhang Junyang 2024-08-11 14:32:47 +00:00 committed by Tate, Hongliang Tian
parent 9200538175
commit 8cf7063150
10 changed files with 52 additions and 85 deletions

View File

@ -11,7 +11,7 @@ use crate::{
utils::CreationFlags,
},
prelude::*,
process::{posix_thread::PosixThreadExt, signal::sig_mask::SigMask},
process::signal::sig_mask::SigMask,
};
pub fn sys_epoll_create(size: i32, ctx: &Context) -> Result<SyscallReturn> {
@ -152,29 +152,26 @@ pub fn sys_epoll_wait(
Ok(SyscallReturn::Return(epoll_events.len() as _))
}
fn set_signal_mask(set_ptr: Vaddr) -> Result<SigMask> {
fn set_signal_mask(set_ptr: Vaddr, ctx: &Context) -> Result<SigMask> {
let new_mask: Option<SigMask> = if set_ptr != 0 {
Some(CurrentUserSpace::get().read_val::<u64>(set_ptr)?.into())
} else {
None
};
let current_thread = current_thread!();
let posix_thread = current_thread.as_posix_thread().unwrap();
let old_sig_mask_value = posix_thread.sig_mask().load(Ordering::Relaxed);
let old_sig_mask_value = ctx.posix_thread.sig_mask().load(Ordering::Relaxed);
if let Some(new_mask) = new_mask {
posix_thread.sig_mask().store(new_mask, Ordering::Relaxed);
ctx.posix_thread
.sig_mask()
.store(new_mask, Ordering::Relaxed);
}
Ok(old_sig_mask_value)
}
fn restore_signal_mask(sig_mask_val: SigMask) {
let current_thread = current_thread!();
let posix_thread = current_thread.as_posix_thread().unwrap();
posix_thread
fn restore_signal_mask(sig_mask_val: SigMask, ctx: &Context) {
ctx.posix_thread
.sig_mask()
.store(sig_mask_val, Ordering::Relaxed);
}
@ -197,16 +194,16 @@ pub fn sys_epoll_pwait(
error!("sigset size is not equal to 8");
}
let old_sig_mask_value = set_signal_mask(sigmask)?;
let old_sig_mask_value = set_signal_mask(sigmask, ctx)?;
let ready_events = match do_epoll_wait(epfd, max_events, timeout, ctx) {
Ok(events) => {
restore_signal_mask(old_sig_mask_value);
restore_signal_mask(old_sig_mask_value, ctx);
events
}
Err(e) => {
// Restore the signal mask even if an error occurs
restore_signal_mask(old_sig_mask_value);
restore_signal_mask(old_sig_mask_value, ctx);
return Err(e);
}
};

View File

@ -16,7 +16,7 @@ pub fn sys_futex(
utime_addr: u64,
futex_new_addr: u64,
bitset: u64,
_ctx: &Context,
ctx: &Context,
) -> Result<SyscallReturn> {
// FIXME: we current ignore futex flags
let (futex_op, futex_flags) = futex_op_and_flags_from_u32(futex_op as _).unwrap();
@ -72,6 +72,6 @@ pub fn sys_futex(
}
.unwrap();
debug!("futex returns, tid= {} ", current_thread!().tid());
debug!("futex returns, tid= {} ", ctx.thread.tid());
Ok(SyscallReturn::Return(res as _))
}

View File

@ -3,8 +3,7 @@
use super::SyscallReturn;
use crate::prelude::*;
pub fn sys_gettid(_ctx: &Context) -> Result<SyscallReturn> {
let current_thread = current_thread!();
let tid = current_thread.tid();
pub fn sys_gettid(ctx: &Context) -> Result<SyscallReturn> {
let tid = ctx.thread.tid();
Ok(SyscallReturn::Return(tid as _))
}

View File

@ -3,10 +3,7 @@
use super::SyscallReturn;
use crate::{
prelude::*,
process::{
posix_thread::{PosixThreadExt, MAX_THREAD_NAME_LEN},
signal::sig_num::SigNum,
},
process::{posix_thread::MAX_THREAD_NAME_LEN, signal::sig_num::SigNum},
};
pub fn sys_prctl(
@ -19,8 +16,6 @@ pub fn sys_prctl(
) -> Result<SyscallReturn> {
let prctl_cmd = PrctlCmd::from_args(option, arg2, arg3, arg4, arg5)?;
debug!("prctl cmd = {:x?}", prctl_cmd);
let current_thread = current_thread!();
let posix_thread = current_thread.as_posix_thread().unwrap();
match prctl_cmd {
PrctlCmd::PR_SET_PDEATHSIG(signum) => {
ctx.process.set_parent_death_signal(signum);
@ -47,7 +42,7 @@ pub fn sys_prctl(
// TODO: implement coredump
}
PrctlCmd::PR_GET_NAME(write_to_addr) => {
let thread_name = posix_thread.thread_name().lock();
let thread_name = ctx.posix_thread.thread_name().lock();
if let Some(thread_name) = &*thread_name {
if let Some(thread_name) = thread_name.name()? {
CurrentUserSpace::get().write_bytes(
@ -58,7 +53,7 @@ pub fn sys_prctl(
}
}
PrctlCmd::PR_SET_NAME(read_addr) => {
let mut thread_name = posix_thread.thread_name().lock();
let mut thread_name = ctx.posix_thread.thread_name().lock();
if let Some(thread_name) = &mut *thread_name {
let new_thread_name =
CurrentUserSpace::get().read_cstring(read_addr, MAX_THREAD_NAME_LEN)?;

View File

@ -3,12 +3,12 @@
use core::sync::atomic::Ordering;
use super::SyscallReturn;
use crate::{prelude::*, process::posix_thread::PosixThreadExt};
use crate::prelude::*;
pub fn sys_rt_sigpending(
u_set_ptr: Vaddr,
sigset_size: usize,
_ctx: &Context,
ctx: &Context,
) -> Result<SyscallReturn> {
debug!(
"u_set_ptr = 0x{:x}, sigset_size = {}",
@ -17,17 +17,14 @@ pub fn sys_rt_sigpending(
if sigset_size != 8 {
return_errno_with_message!(Errno::EINVAL, "sigset size is not equal to 8")
}
do_rt_sigpending(u_set_ptr)?;
do_rt_sigpending(u_set_ptr, ctx)?;
Ok(SyscallReturn::Return(0))
}
fn do_rt_sigpending(set_ptr: Vaddr) -> Result<()> {
let current_thread = current_thread!();
let posix_thread = current_thread.as_posix_thread().unwrap();
fn do_rt_sigpending(set_ptr: Vaddr, ctx: &Context) -> Result<()> {
let combined_signals = {
let sig_mask_value = posix_thread.sig_mask().load(Ordering::Relaxed);
let sig_pending_value = posix_thread.sig_pending();
let sig_mask_value = ctx.posix_thread.sig_mask().load(Ordering::Relaxed);
let sig_pending_value = ctx.posix_thread.sig_pending();
sig_mask_value & sig_pending_value
};

View File

@ -5,12 +5,9 @@ use core::sync::atomic::Ordering;
use super::SyscallReturn;
use crate::{
prelude::*,
process::{
posix_thread::PosixThreadExt,
signal::{
constants::{SIGKILL, SIGSTOP},
sig_mask::SigMask,
},
process::signal::{
constants::{SIGKILL, SIGSTOP},
sig_mask::SigMask,
},
};
@ -19,7 +16,7 @@ pub fn sys_rt_sigprocmask(
set_ptr: Vaddr,
oldset_ptr: Vaddr,
sigset_size: usize,
_ctx: &Context,
ctx: &Context,
) -> Result<SyscallReturn> {
let mask_op = MaskOp::try_from(how).unwrap();
debug!(
@ -29,21 +26,23 @@ pub fn sys_rt_sigprocmask(
if sigset_size != 8 {
error!("sigset size is not equal to 8");
}
do_rt_sigprocmask(mask_op, set_ptr, oldset_ptr).unwrap();
do_rt_sigprocmask(mask_op, set_ptr, oldset_ptr, ctx).unwrap();
Ok(SyscallReturn::Return(0))
}
fn do_rt_sigprocmask(mask_op: MaskOp, set_ptr: Vaddr, oldset_ptr: Vaddr) -> Result<()> {
let current_thread = current_thread!();
let posix_thread = current_thread.as_posix_thread().unwrap();
let old_sig_mask_value = posix_thread.sig_mask().load(Ordering::Relaxed);
fn do_rt_sigprocmask(
mask_op: MaskOp,
set_ptr: Vaddr,
oldset_ptr: Vaddr,
ctx: &Context,
) -> Result<()> {
let old_sig_mask_value = ctx.posix_thread.sig_mask().load(Ordering::Relaxed);
debug!("old sig mask value: 0x{:x}", old_sig_mask_value);
if oldset_ptr != 0 {
CurrentUserSpace::get().write_val(oldset_ptr, &old_sig_mask_value)?;
}
let sig_mask_ref = posix_thread.sig_mask();
let sig_mask_ref = ctx.posix_thread.sig_mask();
if set_ptr != 0 {
let mut read_mask = CurrentUserSpace::get().read_val::<SigMask>(set_ptr)?;
match mask_op {

View File

@ -1,15 +1,12 @@
// SPDX-License-Identifier: MPL-2.0
use super::SyscallReturn;
use crate::{
prelude::*,
process::posix_thread::{PosixThreadExt, RobustListHead},
};
use crate::{prelude::*, process::posix_thread::RobustListHead};
pub fn sys_set_robust_list(
robust_list_head_ptr: Vaddr,
len: usize,
_ctx: &Context,
ctx: &Context,
) -> Result<SyscallReturn> {
debug!(
"robust list head ptr: 0x{:x}, len = {}",
@ -24,9 +21,7 @@ pub fn sys_set_robust_list(
let robust_list_head: RobustListHead =
CurrentUserSpace::get().read_val(robust_list_head_ptr)?;
debug!("{:x?}", robust_list_head);
let current_thread = current_thread!();
let posix_thread = current_thread.as_posix_thread().unwrap();
let mut robust_list = posix_thread.robust_list().lock();
let mut robust_list = ctx.posix_thread.robust_list().lock();
*robust_list = Some(robust_list_head);
Ok(SyscallReturn::Return(0))
}

View File

@ -1,13 +1,11 @@
// SPDX-License-Identifier: MPL-2.0
use super::SyscallReturn;
use crate::{prelude::*, process::posix_thread::PosixThreadExt};
use crate::prelude::*;
pub fn sys_set_tid_address(tidptr: Vaddr, _ctx: &Context) -> Result<SyscallReturn> {
pub fn sys_set_tid_address(tidptr: Vaddr, ctx: &Context) -> Result<SyscallReturn> {
debug!("tidptr = 0x{:x}", tidptr);
let current_thread = current_thread!();
let posix_thread = current_thread.as_posix_thread().unwrap();
let mut clear_child_tid = posix_thread.clear_child_tid().lock();
let mut clear_child_tid = ctx.posix_thread.clear_child_tid().lock();
if *clear_child_tid != 0 {
// According to manuals at https://man7.org/linux/man-pages/man2/set_tid_address.2.html
// We need to write 0 to clear_child_tid and do futex wake
@ -15,6 +13,6 @@ pub fn sys_set_tid_address(tidptr: Vaddr, _ctx: &Context) -> Result<SyscallRetur
} else {
*clear_child_tid = tidptr;
}
let tid = current_thread.tid();
let tid = ctx.thread.tid();
Ok(SyscallReturn::Return(tid as _))
}

View File

@ -3,16 +3,13 @@
use super::SyscallReturn;
use crate::{
prelude::*,
process::{
posix_thread::PosixThreadExt,
signal::{SigStack, SigStackFlags},
},
process::signal::{SigStack, SigStackFlags},
};
pub fn sys_sigaltstack(
sig_stack_addr: Vaddr,
old_sig_stack_addr: Vaddr,
_ctx: &Context,
ctx: &Context,
) -> Result<SyscallReturn> {
debug!(
"sig_stack_addr = 0x{:x}, old_sig_stack_addr: 0x{:x}",
@ -20,14 +17,12 @@ pub fn sys_sigaltstack(
);
let old_stack = {
let current_thread = current_thread!();
let posix_thread = current_thread.as_posix_thread().unwrap();
let sig_stack = posix_thread.sig_stack().lock();
let sig_stack = ctx.posix_thread.sig_stack().lock();
sig_stack.clone()
};
get_old_stack(old_sig_stack_addr, old_stack.as_ref())?;
set_new_stack(sig_stack_addr, old_stack.as_ref())?;
set_new_stack(sig_stack_addr, old_stack.as_ref(), ctx)?;
Ok(SyscallReturn::Return(0))
}
@ -47,7 +42,7 @@ fn get_old_stack(old_sig_stack_addr: Vaddr, old_stack: Option<&SigStack>) -> Res
CurrentUserSpace::get().write_val(old_sig_stack_addr, &stack)
}
fn set_new_stack(sig_stack_addr: Vaddr, old_stack: Option<&SigStack>) -> Result<()> {
fn set_new_stack(sig_stack_addr: Vaddr, old_stack: Option<&SigStack>, ctx: &Context) -> Result<()> {
if sig_stack_addr == 0 {
return Ok(());
}
@ -65,9 +60,7 @@ fn set_new_stack(sig_stack_addr: Vaddr, old_stack: Option<&SigStack>) -> Result<
debug!("new_stack = {:?}", new_stack);
let current_thread = current_thread!();
let posix_thread = current_thread.as_posix_thread().unwrap();
*posix_thread.sig_stack().lock() = Some(new_stack);
*ctx.posix_thread.sig_stack().lock() = Some(new_stack);
Ok(())
}

View File

@ -31,7 +31,7 @@ pub fn sys_timer_create(
clockid: clockid_t,
sigevent_addr: Vaddr,
timer_id_addr: Vaddr,
_ctx: &Context,
ctx: &Context,
) -> Result<SyscallReturn> {
if timer_id_addr == 0 {
return_errno_with_message!(
@ -111,13 +111,7 @@ pub fn sys_timer_create(
let clock_id = ClockId::try_from(clockid)?;
match clock_id {
ClockId::CLOCK_PROCESS_CPUTIME_ID => process_timer_manager.create_prof_timer(func),
ClockId::CLOCK_THREAD_CPUTIME_ID => {
let current_thread = current_thread!();
current_thread
.as_posix_thread()
.unwrap()
.create_prof_timer(func)
}
ClockId::CLOCK_THREAD_CPUTIME_ID => ctx.posix_thread.create_prof_timer(func),
ClockId::CLOCK_REALTIME => RealTimeClock::timer_manager().create_timer(func),
ClockId::CLOCK_MONOTONIC => MonotonicClock::timer_manager().create_timer(func),
ClockId::CLOCK_BOOTTIME => BootTimeClock::timer_manager().create_timer(func),