Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support signal and compelte setitimer, alarm, etc #14

Merged
merged 1 commit into from
Oct 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
# - `ENVS`: Environment variables, separated by comma between key value pairs. Only available when feature `alloc` is enabled.
# * App options:
# - `A` or `APP`: Path to the application
# - `FEATURES`: Features os ArceOS modules to be enabled.
# - `FEATURES`: Features of ArceOS modules to be enabled.
# - `APP_FEATURES`: Features of (rust) apps to be enabled.
# * QEMU options:
# - `BLK`: Enable storage devices (virtio-blk)
Expand Down
1 change: 1 addition & 0 deletions api/arceos_posix_api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ multitask = ["axtask/multitask", "axfeat/multitask", "axsync/multitask"]
fd = ["alloc"]
fs = ["dep:axfs", "axfeat/fs", "fd"]
net = ["dep:axnet", "axfeat/net", "fd"]
signal = ["axfeat/signal"]
pipe = ["fd"]
select = ["fd"]
epoll = ["fd"]
Expand Down
6 changes: 6 additions & 0 deletions api/arceos_posix_api/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ typedef struct {{
"sock.*",
"fd_set",
"timeval",
"itimerval",
"pthread_t",
"pthread_attr_t",
"pthread_mutex_t",
Expand All @@ -95,6 +96,8 @@ typedef struct {{
"pthread_cond_t",
"pthread_condattr_t",
"sysinfo",
"sigaction",
"k_sigaction",
];
let allow_vars = [
"O_.*",
Expand All @@ -109,6 +112,9 @@ typedef struct {{
"RLIMIT_.*",
"EAI_.*",
"MAXADDRS",
"ITIMER_.*",
"SIG.*",
"EINVAL",
];

#[derive(Debug)]
Expand Down
5 changes: 4 additions & 1 deletion api/arceos_posix_api/ctypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,15 @@
* See the Mulan PSL v2 for more details.
*/

#include <errno.h>
#include <fcntl.h>
#include <ksigaction.h>
#include <netdb.h>
#include <netinet/in.h>
#include <poll.h>
#include <pthread.h>
#include <signal.h>
#include <stddef.h>
#include <poll.h>
#include <sys/epoll.h>
#include <sys/resource.h>
#include <sys/select.h>
Expand Down
2 changes: 2 additions & 0 deletions api/arceos_posix_api/src/imp/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ mod stdio;

pub mod io;
pub mod resources;
#[cfg(feature = "signal")]
pub mod signal;
pub mod sys;
pub mod task;
pub mod time;
Expand Down
24 changes: 24 additions & 0 deletions api/arceos_posix_api/src/imp/signal.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/* 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 crate::ctypes::k_sigaction;
use axruntime::{rx_sigaction, Signal};

/// Set signal handler
pub fn sys_sigaction(
signum: u8,
sigaction: Option<&k_sigaction>,
oldact: Option<&mut k_sigaction>,
) {
Signal::sigaction(
signum,
sigaction.map(|act| act as *const k_sigaction as *const rx_sigaction),
oldact.map(|old| old as *mut k_sigaction as *mut rx_sigaction),
);
}
41 changes: 41 additions & 0 deletions api/arceos_posix_api/src/imp/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
*/

use axerrno::LinuxError;
#[cfg(feature = "signal")]
use axruntime::Signal;
AuYang261 marked this conversation as resolved.
Show resolved Hide resolved
use core::ffi::{c_int, c_long};
use core::time::Duration;

Expand Down Expand Up @@ -108,3 +110,42 @@ pub unsafe fn sys_nanosleep(req: *const ctypes::timespec, rem: *mut ctypes::time
Ok(0)
})
}

#[cfg(feature = "signal")]
/// Set a timer to send a signal to the current process after a specified time
pub unsafe fn sys_setitimer(which: c_int, new: *const ctypes::itimerval) -> c_int {
syscall_body!(sys_setitimer, {
let which = which as usize;
let new_interval = Duration::from((*new).it_interval).as_nanos() as u64;
Signal::timer_interval(which, Some(new_interval));

let new_ddl =
axhal::time::current_time_nanos() + Duration::from((*new).it_value).as_nanos() as u64;
Signal::timer_deadline(which, Some(new_ddl));
Ok(0)
})
}

#[cfg(feature = "signal")]
/// Get timer to send signal after some time
pub unsafe fn sys_getitimer(which: c_int, curr_value: *mut ctypes::itimerval) -> c_int {
syscall_body!(sys_getitimer, {
let ddl = Duration::from_nanos(Signal::timer_deadline(which as usize, None).unwrap());
if ddl.as_nanos() == 0 {
return Err(LinuxError::EINVAL);
}
let mut now: ctypes::timespec = ctypes::timespec::default();
unsafe {
sys_clock_gettime(0, &mut now);
}
let now = Duration::from(now);
if ddl > now {
(*curr_value).it_value = ctypes::timeval::from(ddl - now);
} else {
(*curr_value).it_value = ctypes::timeval::from(Duration::new(0, 0));
}
(*curr_value).it_interval =
Duration::from_nanos(Signal::timer_interval(which as usize, None).unwrap()).into();
Ok(0)
})
}
4 changes: 4 additions & 0 deletions api/arceos_posix_api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,13 @@ pub mod ctypes;

pub use imp::io::{sys_read, sys_write, sys_writev};
pub use imp::resources::{sys_getrlimit, sys_setrlimit};
#[cfg(feature = "signal")]
pub use imp::signal::sys_sigaction;
pub use imp::sys::sys_sysinfo;
pub use imp::task::{sys_exit, sys_getpid, sys_sched_yield};
pub use imp::time::{sys_clock_gettime, sys_clock_settime, sys_nanosleep};
#[cfg(feature = "signal")]
pub use imp::time::{sys_getitimer, sys_setitimer};

#[cfg(feature = "fd")]
pub use imp::fd_ops::{sys_close, sys_dup, sys_dup2, sys_fcntl};
Expand Down
3 changes: 3 additions & 0 deletions api/axfeat/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ net = ["alloc", "paging", "axdriver/virtio-net", "dep:axnet", "axruntime/net"]
# Display
display = ["alloc", "paging", "axdriver/virtio-gpu", "dep:axdisplay", "axruntime/display"]

# Signal
signal = ["axruntime/signal"]

# Device drivers
bus-mmio = ["axdriver?/bus-mmio"]
bus-pci = ["axdriver?/bus-pci"]
Expand Down
1 change: 1 addition & 0 deletions api/axfeat/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
//! - `myfs`: Allow users to define their custom filesystems to override the default.
//! - `net`: Enable networking support.
//! - `display`: Enable graphics support.
//! - `signal`: Enable signal support.
//! - Device drivers
//! - `bus-mmio`: Use device tree to probe all MMIO devices.
//! - `bus-pci`: Use PCI bus to probe all PCI devices.
Expand Down
1 change: 1 addition & 0 deletions modules/axruntime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ multitask = ["axtask/multitask"]
fs = ["axdriver", "axfs"]
net = ["axdriver", "axnet"]
display = ["axdriver", "axdisplay"]
signal = []

[dependencies]
cfg-if = "1.0"
Expand Down
38 changes: 38 additions & 0 deletions modules/axruntime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ extern crate axlog;

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

#[cfg(feature = "smp")]
Expand All @@ -41,6 +43,9 @@ mod mp;
#[cfg(feature = "smp")]
pub use self::mp::rust_main_secondary;

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

#[cfg(feature = "alloc")]
extern crate alloc;
#[cfg(feature = "alloc")]
Expand Down Expand Up @@ -355,8 +360,41 @@ fn init_interrupt() {
axhal::time::set_oneshot_timer(deadline);
}

#[cfg(feature = "signal")]
fn do_signal() {
let now_ns = axhal::time::current_time_nanos();
// timer signal num
let timers = [14, 26, 27];
for (which, timer) in timers.iter().enumerate() {
let mut ddl = Signal::timer_deadline(which, None).unwrap();
let interval = Signal::timer_interval(which, None).unwrap();
if ddl != 0 && now_ns >= ddl {
Signal::signal(*timer, true);
if interval == 0 {
ddl = 0;
} else {
ddl += interval;
}
Signal::timer_deadline(which, Some(ddl));
}
}
let signal = Signal::signal(-1, true).unwrap();
for signum in 0..32 {
if signal & (1 << signum) != 0
/* TODO: && support mask */
{
Signal::sigaction(signum as u8, None, None);
Signal::signal(signum as i8, false);
}
}
}

axhal::irq::register_handler(TIMER_IRQ_NUM, || {
update_timer();
#[cfg(feature = "signal")]
if axhal::cpu::this_cpu_is_bsp() {
do_signal();
}
#[cfg(feature = "multitask")]
axtask::on_timer_tick();
});
Expand Down
Loading
Loading