-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: move the definition of signal to axruntime
- Loading branch information
Showing
11 changed files
with
249 additions
and
197 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -66,7 +66,6 @@ typedef struct {{ | |
"rlimit", | ||
"aibuf", | ||
"sysinfo", | ||
"sighandler_t", | ||
"sigaction", | ||
]; | ||
let allow_vars = [ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,7 @@ mod stdio; | |
|
||
pub mod io; | ||
pub mod resources; | ||
pub mod signal; | ||
pub mod sys; | ||
pub mod task; | ||
pub mod time; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
/* Copyright (c) [2023] [Syswonder Community] | ||
* [Rukos] is licensed under Mulan PSL v2. | ||
* You can use this software according to the terms and conditions of the Mulan PSL v2. | ||
* You may obtain a copy of Mulan PSL v2 at: | ||
* http://license.coscl.org.cn/MulanPSL2 | ||
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. | ||
* See the Mulan PSL v2 for more details. | ||
*/ | ||
|
||
use axruntime::{k_sigaction, Signal}; | ||
|
||
/// Set signal handler | ||
pub fn sys_sigaction( | ||
signum: u8, | ||
sigaction: Option<*const k_sigaction>, | ||
oldact: Option<*mut k_sigaction>, | ||
) { | ||
Signal::sigaction(signum as u8, sigaction, oldact); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,163 @@ | ||
/* Copyright (c) [2023] [Syswonder Community] | ||
* [Rukos] is licensed under Mulan PSL v2. | ||
* You can use this software according to the terms and conditions of the Mulan PSL v2. | ||
* You may obtain a copy of Mulan PSL v2 at: | ||
* http://license.coscl.org.cn/MulanPSL2 | ||
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. | ||
* See the Mulan PSL v2 for more details. | ||
*/ | ||
|
||
#[cfg(feature = "irq")] | ||
use core::sync::atomic::AtomicI64; | ||
use core::{ffi::c_int, time::Duration}; | ||
|
||
#[allow(non_camel_case_types)] | ||
#[derive(Copy, Clone, Debug)] | ||
struct itimerval { | ||
it_interval: Duration, | ||
it_value: Duration, | ||
} | ||
|
||
/// sigset_t in kernel | ||
#[allow(non_camel_case_types)] | ||
#[derive(Copy, Clone, Debug, Default)] | ||
pub struct sigset_t { | ||
__bits: [u64; 16], | ||
} | ||
|
||
/// sigaction in kernel | ||
#[allow(non_camel_case_types)] | ||
#[allow(dead_code)] | ||
#[derive(Copy, Clone, Debug, Default)] | ||
pub struct k_sigaction { | ||
/// signal handler | ||
pub sa_handler: Option<unsafe extern "C" fn(c_int)>, | ||
/// signal mask | ||
pub sa_mask: sigset_t, | ||
/// signal flags | ||
pub sa_flags: i32, | ||
/// signal restorer | ||
pub sa_restorer: Option<unsafe extern "C" fn()>, | ||
} | ||
|
||
/// Signal struct | ||
pub struct Signal { | ||
#[cfg(feature = "irq")] | ||
signal: AtomicI64, | ||
sigaction: [k_sigaction; 32], | ||
timer: [itimerval; 3], | ||
} | ||
|
||
unsafe extern "C" fn default_handler(signum: c_int) { | ||
panic!("default_handler, signum: {}", signum); | ||
} | ||
|
||
static mut SIGNAL_IF: Signal = Signal { | ||
#[cfg(feature = "irq")] | ||
signal: AtomicI64::new(0), | ||
sigaction: [k_sigaction { | ||
sa_handler: Some(default_handler), | ||
sa_mask: sigset_t { __bits: [0; 16] }, | ||
sa_flags: 0, | ||
sa_restorer: None, | ||
}; 32], | ||
// Default::default() is not const | ||
timer: [itimerval { | ||
it_interval: Duration::new(0, 0), | ||
it_value: Duration::new(0, 0), | ||
}; 3], | ||
}; | ||
|
||
impl Signal { | ||
/// Set signal | ||
/// signum: signal number, if signum < 0, just return current signal | ||
/// on: true: enable signal, false: disable signal | ||
#[cfg(feature = "irq")] | ||
pub fn signal(signum: i8, on: bool) -> Option<u32> { | ||
use core::sync::atomic::Ordering; | ||
if signum >= 32 { | ||
return None; | ||
} | ||
let mut old = unsafe { SIGNAL_IF.signal.load(Ordering::Acquire) }; | ||
if signum >= 0 { | ||
loop { | ||
let new; | ||
if on { | ||
new = old | (1 << signum); | ||
} else { | ||
new = old & !(1 << signum); | ||
} | ||
|
||
match unsafe { | ||
SIGNAL_IF.signal.compare_exchange_weak( | ||
old, | ||
new, | ||
Ordering::AcqRel, | ||
Ordering::Acquire, | ||
) | ||
} { | ||
Ok(_) => break, | ||
Err(x) => old = x, | ||
} | ||
} | ||
} | ||
Some(old.try_into().unwrap()) | ||
} | ||
/// Set signal action | ||
/// signum: signal number | ||
/// sigaction: signal action, if sigaction == None, call the handler | ||
pub fn sigaction( | ||
signum: u8, | ||
sigaction: Option<*const k_sigaction>, | ||
oldact: Option<*mut k_sigaction>, | ||
) { | ||
if signum >= unsafe { SIGNAL_IF.sigaction }.len() as u8 { | ||
return; | ||
} | ||
if let Some(oldact) = oldact { | ||
if !oldact.is_null() { | ||
unsafe { *oldact = 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) | ||
}, | ||
} | ||
} | ||
/// 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 }.len() { | ||
return None; | ||
} | ||
let old = unsafe { SIGNAL_IF.timer }[which].it_value; | ||
if let Some(s) = new_deadline { | ||
unsafe { | ||
SIGNAL_IF.timer[which].it_value = Duration::from_nanos(s); | ||
} | ||
} | ||
Some(old.as_nanos() as u64) | ||
} | ||
/// Set timer interval | ||
/// which: timer type | ||
/// 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 }.len() { | ||
return None; | ||
} | ||
let old = unsafe { SIGNAL_IF.timer }[which].it_interval; | ||
if let Some(s) = new_interval { | ||
unsafe { | ||
SIGNAL_IF.timer[which].it_interval = Duration::from_nanos(s); | ||
} | ||
} | ||
Some(old.as_nanos() as u64) | ||
} | ||
} |
Oops, something went wrong.