Skip to content

Commit

Permalink
Merge pull request #158 from lhw2002426/multiprocess-loopback
Browse files Browse the repository at this point in the history
Multiprocess loopback
  • Loading branch information
ken4647 authored Dec 17, 2024
2 parents 27bcc5d + 19fa8e6 commit a67be9b
Show file tree
Hide file tree
Showing 9 changed files with 93 additions and 32 deletions.
2 changes: 1 addition & 1 deletion api/ruxos_posix_api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ multitask = ["ruxfeat/multitask", "ruxtask/multitask", "dep:ruxfutex"]
fd = ["alloc"]
fs = ["dep:ruxfs", "ruxfeat/fs", "fd"]
net = ["dep:ruxnet", "ruxfeat/net", "fd"]
signal = ["ruxruntime/signal", "ruxhal/signal"]
signal = ["ruxruntime/signal", "ruxhal/signal", "ruxtask/signal"]
pipe = ["fd"]
select = ["fd"]
epoll = ["fd"]
Expand Down
20 changes: 17 additions & 3 deletions api/ruxos_posix_api/src/imp/rt_sig.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,24 @@ pub unsafe fn sys_rt_sigaction(
old: *mut ctypes::sigaction,
_sigsetsize: ctypes::size_t,
) -> c_int {
debug!("sys_rt_sigaction <= sig: {}", sig);
debug!(
"sys_rt_sigaction <= sig: {} sa {:x} old {:x}",
sig, sa as u64, old as u64
);
syscall_body!(sys_rt_sigaction, {
if sa as u64 == 0 || old as u64 == 0 {
Err(LinuxError::EFAULT)
if sa as u64 == 0 && old as u64 == 0 {
sys_sigaction(sig as _, None, None);
Ok(0)
} else if sa as u64 != 0 && old as u64 == 0 {
let sa = unsafe { *sa };
let sa = k_sigaction::from(sa);
sys_sigaction(sig as _, Some(&sa), None);
Ok(0)
} else if sa as u64 == 0 && old as u64 != 0 {
let old = unsafe { *old };
let mut old_sa = k_sigaction::from(old);
sys_sigaction(sig as _, None, Some(&mut old_sa));
Ok(0)
} else {
let sa = unsafe { *sa };
let old = unsafe { *old };
Expand Down
2 changes: 1 addition & 1 deletion api/ruxos_posix_api/src/imp/signal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use crate::ctypes::k_sigaction;
use crate::ctypes::{self, pid_t};

use axerrno::LinuxError;
use ruxruntime::{rx_sigaction, Signal};
use ruxtask::{rx_sigaction, Signal};

/// Set signal handler
pub fn sys_sigaction(
Expand Down
4 changes: 1 addition & 3 deletions modules/ruxruntime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@ extern crate axlog;

#[cfg(all(target_os = "none", not(test)))]
mod lang_items;
#[cfg(feature = "signal")]
mod signal;

#[cfg(not(feature = "musl"))]
mod trap;
Expand All @@ -52,7 +50,7 @@ mod mp;
pub use self::mp::rust_main_secondary;

#[cfg(feature = "signal")]
pub use self::signal::{rx_sigaction, Signal};
use ruxtask::signal::{rx_sigaction, Signal};

#[cfg(feature = "alloc")]
extern crate alloc;
Expand Down
1 change: 1 addition & 0 deletions modules/ruxtask/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ musl = []
preempt = ["irq", "percpu?/preempt", "kernel_guard/preempt"]
paging = []
fs = []
signal = []

sched_fifo = ["multitask"]
sched_rr = ["multitask", "preempt"]
Expand Down
8 changes: 8 additions & 0 deletions modules/ruxtask/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ cfg_if::cfg_if! {
pub mod task;
mod api;
mod wait_queue;
#[cfg(feature = "signal")]
pub mod signal;
#[cfg(feature = "paging")]
pub mod vma;
// #[cfg(feature = "fs")]
Expand Down Expand Up @@ -79,8 +81,14 @@ cfg_if::cfg_if! {
pub use self::api::*;
pub use self::api::{sleep, sleep_until, yield_now};
pub use task::TaskState;
#[cfg(feature = "signal")]
pub use self::signal::{rx_sigaction, Signal};
} else {
mod api_s;
#[cfg(feature = "signal")]
pub mod signal;
pub use self::api_s::{sleep, sleep_until, yield_now};
#[cfg(feature = "signal")]
pub use self::signal::{rx_sigaction, Signal};
}
}
46 changes: 32 additions & 14 deletions modules/ruxruntime/src/signal.rs → modules/ruxtask/src/signal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
* See the Mulan PSL v2 for more details.
*/

use crate::current;
#[cfg(feature = "irq")]
use core::sync::atomic::AtomicI64;
use core::{
Expand Down Expand Up @@ -85,6 +86,17 @@ impl TrapHandler for SignalHandler {
}

impl Signal {
///crate new Signal struct
pub fn new() -> Self {
Self {
#[cfg(feature = "irq")]
signal: AtomicI64::new(0),
sigaction: [rx_sigaction::new(); 32],
// Default::default() is not const
timer_value: [Duration::from_nanos(0); 3],
timer_interval: [Duration::from_nanos(0); 3],
}
}
/// Set signal
/// signum: signal number, if signum < 0, just return current signal
/// on: true: enable signal, false: disable signal
Expand All @@ -94,7 +106,9 @@ impl Signal {
if signum >= 32 {
return None;
}
let mut old = unsafe { SIGNAL_IF.signal.load(Ordering::Acquire) };
let binding = current();
let mut current_signal_if = binding.signal_if.lock();
let mut old = unsafe { current_signal_if.signal.load(Ordering::Acquire) };
if signum >= 0 {
loop {
let new = if on {
Expand All @@ -104,7 +118,7 @@ impl Signal {
};

match unsafe {
SIGNAL_IF.signal.compare_exchange_weak(
current_signal_if.signal.compare_exchange_weak(
old,
new,
Ordering::AcqRel,
Expand All @@ -126,35 +140,37 @@ impl Signal {
sigaction: Option<*const rx_sigaction>,
oldact: Option<*mut rx_sigaction>,
) {
if signum >= unsafe { SIGNAL_IF.sigaction }.len() as u8 {
let binding = current();
let mut current_signal_if = binding.signal_if.lock();
if signum >= unsafe { current_signal_if.sigaction }.len() as u8 {
return;
}
if let Some(oldact) = oldact {
unsafe {
*oldact = SIGNAL_IF.sigaction[signum as usize];
*oldact = current_signal_if.sigaction[signum as usize];
}
}
match sigaction {
Some(s) => unsafe {
SIGNAL_IF.sigaction[signum as usize] = *s;
},
None => unsafe {
SIGNAL_IF.sigaction[signum as usize].sa_handler.unwrap()(signum as c_int)
current_signal_if.sigaction[signum as usize] = *s;
},
None => {},
}
}
/// Set timer
/// which: timer type
/// new_value: new timer value
/// old_value: old timer value
pub fn timer_deadline(which: usize, new_deadline: Option<u64>) -> Option<u64> {
if which >= unsafe { SIGNAL_IF.timer_value }.len() {
let binding = current();
let mut current_signal_if = binding.signal_if.lock();
if which >= unsafe { current_signal_if.timer_value }.len() {
return None;
}
let old = unsafe { SIGNAL_IF.timer_value }[which];
let old = unsafe { current_signal_if.timer_value }[which];
if let Some(s) = new_deadline {
unsafe {
SIGNAL_IF.timer_value[which] = Duration::from_nanos(s);
current_signal_if.timer_value[which] = Duration::from_nanos(s);
}
}
Some(old.as_nanos() as u64)
Expand All @@ -164,13 +180,15 @@ impl Signal {
/// new_interval: new timer interval
/// old_interval: old timer interval
pub fn timer_interval(which: usize, new_interval: Option<u64>) -> Option<u64> {
if which >= unsafe { SIGNAL_IF.timer_interval }.len() {
let binding = current();
let mut current_signal_if = binding.signal_if.lock();
if which >= unsafe { current_signal_if.timer_interval }.len() {
return None;
}
let old = unsafe { SIGNAL_IF.timer_interval }[which];
let old = unsafe { current_signal_if.timer_interval }[which];
if let Some(s) = new_interval {
unsafe {
SIGNAL_IF.timer_interval[which] = Duration::from_nanos(s);
current_signal_if.timer_interval[which] = Duration::from_nanos(s);
}
}
Some(old.as_nanos() as u64)
Expand Down
15 changes: 15 additions & 0 deletions modules/ruxtask/src/task.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ use crate::current;
use crate::tsd::{DestrFunction, KEYS, TSD};
#[cfg(feature = "paging")]
use crate::vma::MmapStruct;
#[cfg(feature = "signal")]
use crate::Signal;
use crate::{AxRunQueue, AxTask, AxTaskRef, WaitQueue};

/// A unique identifier for a thread.
Expand Down Expand Up @@ -91,6 +93,9 @@ pub struct TaskInner {
#[cfg(not(feature = "musl"))]
tsd: TSD,

#[cfg(feature = "signal")]
pub signal_if: Arc<SpinNoIrq<Signal>>,

// set tid
#[cfg(feature = "musl")]
set_tid: AtomicU64,
Expand Down Expand Up @@ -245,6 +250,8 @@ impl TaskInner {
tsd: spinlock::SpinNoIrq::new([core::ptr::null_mut(); ruxconfig::PTHREAD_KEY_MAX]),
#[cfg(feature = "musl")]
set_tid: AtomicU64::new(0),
#[cfg(feature = "signal")]
signal_if: current().signal_if.clone(),
#[cfg(feature = "musl")]
tl: AtomicU64::new(0),
#[cfg(feature = "paging")]
Expand Down Expand Up @@ -287,6 +294,8 @@ impl TaskInner {
#[cfg(feature = "tls")]
tls: TlsArea::new_with_addr(tls),
set_tid,
#[cfg(feature = "signal")]
signal_if: current().signal_if.clone(),
// clear child tid
tl,
#[cfg(feature = "paging")]
Expand Down Expand Up @@ -494,6 +503,8 @@ impl TaskInner {
tsd: spinlock::SpinNoIrq::new([core::ptr::null_mut(); ruxconfig::PTHREAD_KEY_MAX]),
#[cfg(feature = "musl")]
set_tid: AtomicU64::new(0),
#[cfg(feature = "signal")]
signal_if: Arc::new(spinlock::SpinNoIrq::new(Signal::new())),
#[cfg(feature = "musl")]
tl: AtomicU64::new(0),
#[cfg(feature = "paging")]
Expand Down Expand Up @@ -569,6 +580,8 @@ impl TaskInner {
tsd: spinlock::SpinNoIrq::new([core::ptr::null_mut(); ruxconfig::PTHREAD_KEY_MAX]),
#[cfg(feature = "musl")]
set_tid: AtomicU64::new(0),
#[cfg(feature = "signal")]
signal_if: Arc::new(spinlock::SpinNoIrq::new(Signal::new())),
#[cfg(feature = "musl")]
tl: AtomicU64::new(0),
#[cfg(feature = "paging")]
Expand Down Expand Up @@ -626,6 +639,8 @@ impl TaskInner {
tsd: spinlock::SpinNoIrq::new([core::ptr::null_mut(); ruxconfig::PTHREAD_KEY_MAX]),
#[cfg(feature = "musl")]
set_tid: AtomicU64::new(0),
#[cfg(feature = "signal")]
signal_if: task_ref.signal_if.clone(),
#[cfg(feature = "musl")]
tl: AtomicU64::new(0),
#[cfg(feature = "paging")]
Expand Down
27 changes: 17 additions & 10 deletions ulib/ruxlibc/src/signal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,24 @@ pub unsafe extern "C" fn sigaction_inner(
}
#[cfg(feature = "signal")]
{
let mut sh = (*_act).__sa_handler.sa_handler;
if let Some(h) = sh {
if h as usize == crate::ctypes::SIGIGN as usize {
sh = Some(ignore_handler as unsafe extern "C" fn(c_int));

let k_act = {
if _act.is_null() {
None
} else {
let mut sh = (*_act).__sa_handler.sa_handler;
if let Some(h) = sh {
if h as usize == crate::ctypes::SIGIGN as usize {
sh = Some(ignore_handler as unsafe extern "C" fn(c_int));
}
}
k_sigaction {
handler: sh,
flags: (*_act).sa_flags as _,
restorer: (*_act).sa_restorer,
mask: Default::default(),
}
}
}
let k_act = k_sigaction {
handler: sh,
flags: (*_act).sa_flags as _,
restorer: (*_act).sa_restorer,
mask: Default::default(),
};
let mut k_oldact = k_sigaction::default();
sys_sigaction(
Expand Down

0 comments on commit a67be9b

Please sign in to comment.