mirror of
https://github.com/asterinas/asterinas.git
synced 2025-06-28 11:53:24 +00:00
Optimize the latency of lat-sig-install
This commit is contained in:
committed by
Tate, Hongliang Tian
parent
cf9c71119a
commit
fb718fd440
@ -18,6 +18,7 @@ use super::{
|
||||
use crate::{
|
||||
cpu::LinuxAbi,
|
||||
fs::{file_table::FileTable, fs_resolver::FsResolver, utils::FileCreationMask},
|
||||
get_current_userspace,
|
||||
prelude::*,
|
||||
process::posix_thread::allocate_posix_tid,
|
||||
thread::{Thread, Tid},
|
||||
@ -338,7 +339,7 @@ fn clone_parent_settid(
|
||||
clone_flags: CloneFlags,
|
||||
) -> Result<()> {
|
||||
if clone_flags.contains(CloneFlags::CLONE_PARENT_SETTID) {
|
||||
CurrentUserSpace::get().write_val(parent_tidptr, &child_tid)?;
|
||||
get_current_userspace!().write_val(parent_tidptr, &child_tid)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
@ -2,6 +2,7 @@
|
||||
|
||||
use super::{futex::futex_wake, robust_list::wake_robust_futex, thread_table, PosixThread};
|
||||
use crate::{
|
||||
get_current_userspace,
|
||||
prelude::*,
|
||||
process::{do_exit_group, TermStatus},
|
||||
thread::{Thread, Tid},
|
||||
@ -25,7 +26,7 @@ pub fn do_exit(thread: &Thread, posix_thread: &PosixThread, term_status: TermSta
|
||||
if *clear_ctid != 0 {
|
||||
futex_wake(*clear_ctid, 1, None)?;
|
||||
// FIXME: the correct write length?
|
||||
CurrentUserSpace::get()
|
||||
get_current_userspace!()
|
||||
.write_val(*clear_ctid, &0u32)
|
||||
.unwrap();
|
||||
*clear_ctid = 0;
|
||||
|
@ -2,7 +2,11 @@
|
||||
|
||||
//! The implementation of robust list is from occlum.
|
||||
|
||||
use crate::{prelude::*, process::posix_thread::futex::futex_wake, thread::Tid};
|
||||
use ostd::task::Task;
|
||||
|
||||
use crate::{
|
||||
get_current_userspace, prelude::*, process::posix_thread::futex::futex_wake, thread::Tid,
|
||||
};
|
||||
|
||||
#[repr(C)]
|
||||
#[derive(Clone, Copy, Debug, Pod)]
|
||||
@ -101,7 +105,7 @@ impl<'a> Iterator for FutexIter<'a> {
|
||||
} else {
|
||||
None
|
||||
};
|
||||
let Ok(robust_list) = CurrentUserSpace::get().read_val::<RobustList>(self.entry_ptr)
|
||||
let Ok(robust_list) = get_current_userspace!().read_val::<RobustList>(self.entry_ptr)
|
||||
else {
|
||||
return None;
|
||||
};
|
||||
@ -123,7 +127,9 @@ const FUTEX_TID_MASK: u32 = 0x3FFF_FFFF;
|
||||
/// Wakeup one robust futex owned by the thread
|
||||
/// FIXME: requires atomic operations here
|
||||
pub fn wake_robust_futex(futex_addr: Vaddr, tid: Tid) -> Result<()> {
|
||||
let user_space = CurrentUserSpace::get();
|
||||
let task = Task::current().unwrap();
|
||||
let user_space = CurrentUserSpace::new(&task);
|
||||
|
||||
let futex_val = {
|
||||
if futex_addr == 0 {
|
||||
return_errno_with_message!(Errno::EINVAL, "invalid futext addr");
|
||||
|
@ -334,7 +334,7 @@ impl Process {
|
||||
.lock()
|
||||
.iter()
|
||||
.find(|task| task.tid() == self.pid)
|
||||
.map(Thread::borrow_from_task)
|
||||
.map(|task| Thread::borrow_from_task(task.as_ref()))
|
||||
.cloned()
|
||||
}
|
||||
|
||||
|
@ -20,10 +20,14 @@ use core::{
|
||||
|
||||
use align_ext::AlignExt;
|
||||
use aster_rights::Full;
|
||||
use ostd::mm::{VmIo, MAX_USERSPACE_VADDR};
|
||||
use ostd::{
|
||||
mm::{VmIo, MAX_USERSPACE_VADDR},
|
||||
task::Task,
|
||||
};
|
||||
|
||||
use self::aux_vec::{AuxKey, AuxVec};
|
||||
use crate::{
|
||||
get_current_userspace,
|
||||
prelude::*,
|
||||
util::random::getrandom,
|
||||
vm::{
|
||||
@ -372,7 +376,7 @@ impl InitStackReader {
|
||||
/// Reads argc from the process init stack
|
||||
pub fn argc(&self) -> Result<u64> {
|
||||
let stack_base = self.init_stack_bottom();
|
||||
CurrentUserSpace::get().read_val(stack_base)
|
||||
get_current_userspace!().read_val(stack_base)
|
||||
}
|
||||
|
||||
/// Reads argv from the process init stack
|
||||
@ -383,7 +387,10 @@ impl InitStackReader {
|
||||
let read_offset = self.init_stack_bottom() + size_of::<usize>();
|
||||
|
||||
let mut argv = Vec::with_capacity(argc);
|
||||
let user_space = CurrentUserSpace::get();
|
||||
|
||||
let current_task = Task::current().unwrap();
|
||||
let user_space = CurrentUserSpace::new(¤t_task);
|
||||
|
||||
let mut argv_reader = user_space.reader(read_offset, argc * size_of::<usize>())?;
|
||||
for _ in 0..argc {
|
||||
let arg = {
|
||||
@ -410,7 +417,10 @@ impl InitStackReader {
|
||||
+ size_of::<usize>();
|
||||
|
||||
let mut envp = Vec::new();
|
||||
let user_space = CurrentUserSpace::get();
|
||||
|
||||
let current_task = Task::current().unwrap();
|
||||
let user_space = CurrentUserSpace::new(¤t_task);
|
||||
|
||||
let mut envp_reader = user_space.reader(read_offset, MAX_ENVP_NUMBER)?;
|
||||
for _ in 0..MAX_ENVP_NUMBER {
|
||||
let envp_ptr = envp_reader.read_val::<Vaddr>()?;
|
||||
|
@ -26,11 +26,12 @@ use sig_mask::SigMask;
|
||||
use sig_num::SigNum;
|
||||
pub use sig_stack::{SigStack, SigStackFlags};
|
||||
|
||||
use super::posix_thread::{PosixThread, PosixThreadExt};
|
||||
use super::posix_thread::PosixThread;
|
||||
use crate::{
|
||||
get_current_userspace,
|
||||
prelude::*,
|
||||
process::{do_exit_group, TermStatus},
|
||||
thread::{status::ThreadStatus, Thread},
|
||||
thread::status::ThreadStatus,
|
||||
};
|
||||
|
||||
pub trait SignalContext {
|
||||
@ -41,12 +42,9 @@ pub trait SignalContext {
|
||||
// TODO: This interface of this method is error prone.
|
||||
// The method takes an argument for the current thread to optimize its efficiency.
|
||||
/// Handle pending signal for current process.
|
||||
pub fn handle_pending_signal(
|
||||
context: &mut UserContext,
|
||||
current_thread: &Arc<Thread>,
|
||||
) -> Result<()> {
|
||||
pub fn handle_pending_signal(user_ctx: &mut UserContext, ctx: &Context) -> Result<()> {
|
||||
// We first deal with signal in current thread, then signal in current process.
|
||||
let posix_thread = current_thread.as_posix_thread().unwrap();
|
||||
let posix_thread = ctx.posix_thread;
|
||||
let signal = {
|
||||
let sig_mask = posix_thread.sig_mask().load(Ordering::Relaxed);
|
||||
if let Some(signal) = posix_thread.dequeue_signal(&sig_mask) {
|
||||
@ -82,13 +80,13 @@ pub fn handle_pending_signal(
|
||||
drop(sig_dispositions);
|
||||
|
||||
handle_user_signal(
|
||||
posix_thread,
|
||||
ctx,
|
||||
sig_num,
|
||||
handler_addr,
|
||||
flags,
|
||||
restorer_addr,
|
||||
mask,
|
||||
context,
|
||||
user_ctx,
|
||||
signal.to_info(),
|
||||
)?
|
||||
}
|
||||
@ -109,7 +107,7 @@ pub fn handle_pending_signal(
|
||||
}
|
||||
SigDefaultAction::Ign => {}
|
||||
SigDefaultAction::Stop => {
|
||||
let _ = current_thread.atomic_status().compare_exchange(
|
||||
let _ = ctx.thread.atomic_status().compare_exchange(
|
||||
ThreadStatus::Running,
|
||||
ThreadStatus::Stopped,
|
||||
Ordering::AcqRel,
|
||||
@ -117,7 +115,7 @@ pub fn handle_pending_signal(
|
||||
);
|
||||
}
|
||||
SigDefaultAction::Cont => {
|
||||
let _ = current_thread.atomic_status().compare_exchange(
|
||||
let _ = ctx.thread.atomic_status().compare_exchange(
|
||||
ThreadStatus::Stopped,
|
||||
ThreadStatus::Running,
|
||||
Ordering::AcqRel,
|
||||
@ -132,13 +130,13 @@ pub fn handle_pending_signal(
|
||||
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
pub fn handle_user_signal(
|
||||
posix_thread: &PosixThread,
|
||||
ctx: &Context,
|
||||
sig_num: SigNum,
|
||||
handler_addr: Vaddr,
|
||||
flags: SigActionFlags,
|
||||
restorer_addr: Vaddr,
|
||||
mut mask: SigMask,
|
||||
context: &mut UserContext,
|
||||
user_ctx: &mut UserContext,
|
||||
sig_info: siginfo_t,
|
||||
) -> Result<()> {
|
||||
debug!("sig_num = {:?}, signame = {}", sig_num, sig_num.sig_name());
|
||||
@ -156,23 +154,24 @@ pub fn handle_user_signal(
|
||||
}
|
||||
|
||||
// block signals in sigmask when running signal handler
|
||||
let old_mask = posix_thread.sig_mask().load(Ordering::Relaxed);
|
||||
posix_thread
|
||||
let old_mask = ctx.posix_thread.sig_mask().load(Ordering::Relaxed);
|
||||
ctx.posix_thread
|
||||
.sig_mask()
|
||||
.store(old_mask + mask, Ordering::Relaxed);
|
||||
|
||||
// Set up signal stack.
|
||||
let mut stack_pointer = if let Some(sp) = use_alternate_signal_stack(posix_thread) {
|
||||
let mut stack_pointer = if let Some(sp) = use_alternate_signal_stack(ctx.posix_thread) {
|
||||
sp as u64
|
||||
} else {
|
||||
// just use user stack
|
||||
context.stack_pointer() as u64
|
||||
user_ctx.stack_pointer() as u64
|
||||
};
|
||||
|
||||
// To avoid corrupting signal stack, we minus 128 first.
|
||||
stack_pointer -= 128;
|
||||
|
||||
let user_space = CurrentUserSpace::get();
|
||||
let user_space = ctx.get_user_space();
|
||||
|
||||
// 1. write siginfo_t
|
||||
stack_pointer -= mem::size_of::<siginfo_t>() as u64;
|
||||
user_space.write_val(stack_pointer as _, &sig_info)?;
|
||||
@ -188,8 +187,8 @@ pub fn handle_user_signal(
|
||||
.uc_mcontext
|
||||
.inner
|
||||
.gp_regs
|
||||
.copy_from_raw(context.general_regs());
|
||||
let mut sig_context = posix_thread.sig_context().lock();
|
||||
.copy_from_raw(user_ctx.general_regs());
|
||||
let mut sig_context = ctx.posix_thread.sig_context().lock();
|
||||
if let Some(sig_context_addr) = *sig_context {
|
||||
ucontext.uc_link = sig_context_addr;
|
||||
} else {
|
||||
@ -222,13 +221,13 @@ pub fn handle_user_signal(
|
||||
}
|
||||
|
||||
// 4. Set correct register values
|
||||
context.set_instruction_pointer(handler_addr as _);
|
||||
context.set_stack_pointer(stack_pointer as usize);
|
||||
user_ctx.set_instruction_pointer(handler_addr as _);
|
||||
user_ctx.set_stack_pointer(stack_pointer as usize);
|
||||
// parameters of signal handler
|
||||
if flags.contains(SigActionFlags::SA_SIGINFO) {
|
||||
context.set_arguments(sig_num, siginfo_addr as usize, ucontext_addr as usize);
|
||||
user_ctx.set_arguments(sig_num, siginfo_addr as usize, ucontext_addr as usize);
|
||||
} else {
|
||||
context.set_arguments(sig_num, 0, 0);
|
||||
user_ctx.set_arguments(sig_num, 0, 0);
|
||||
}
|
||||
|
||||
Ok(())
|
||||
@ -261,7 +260,7 @@ fn use_alternate_signal_stack(posix_thread: &PosixThread) -> Option<usize> {
|
||||
|
||||
fn write_u64_to_user_stack(rsp: u64, value: u64) -> Result<u64> {
|
||||
let rsp = rsp - 8;
|
||||
CurrentUserSpace::get().write_val(rsp as Vaddr, &value)?;
|
||||
get_current_userspace!().write_val(rsp as Vaddr, &value)?;
|
||||
Ok(rsp)
|
||||
}
|
||||
|
||||
|
@ -26,9 +26,9 @@ impl SigDispositions {
|
||||
self.map[idx]
|
||||
}
|
||||
|
||||
pub fn set(&mut self, num: SigNum, sa: SigAction) {
|
||||
pub fn set(&mut self, num: SigNum, sa: SigAction) -> SigAction {
|
||||
let idx = Self::num_to_idx(num);
|
||||
self.map[idx] = sa;
|
||||
core::mem::replace(&mut self.map[idx], sa)
|
||||
}
|
||||
|
||||
pub fn set_default(&mut self, num: SigNum) {
|
||||
|
Reference in New Issue
Block a user