Skip to content

Commit

Permalink
fix(syscalls): don't call foreign __sys functions
Browse files Browse the repository at this point in the history
Signed-off-by: Martin Kröning <[email protected]>
  • Loading branch information
mkroening committed Mar 22, 2024
1 parent 7a374ae commit 6089d53
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 29 deletions.
12 changes: 10 additions & 2 deletions src/syscalls/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,13 +212,17 @@ pub(crate) fn get_application_parameters() -> (i32, *const *const u8, *const *co
SYS.get_application_parameters()
}

pub(crate) extern "C" fn __sys_shutdown(arg: i32) -> ! {
fn shutdown(arg: i32) -> ! {
// print some performance statistics
crate::arch::kernel::print_statistics();

SYS.shutdown(arg)
}

pub(crate) extern "C" fn __sys_shutdown(arg: i32) -> ! {
shutdown(arg)
}

#[no_mangle]
pub extern "C" fn sys_shutdown(arg: i32) -> ! {
kernel_function!(__sys_shutdown(arg))
Expand Down Expand Up @@ -386,14 +390,18 @@ pub extern "C" fn sys_read(fd: FileDescriptor, buf: *mut u8, len: usize) -> isiz
kernel_function!(__sys_read(fd, buf, len))
}

extern "C" fn __sys_write(fd: FileDescriptor, buf: *const u8, len: usize) -> isize {
fn write(fd: FileDescriptor, buf: *const u8, len: usize) -> isize {
let slice = unsafe { core::slice::from_raw_parts(buf, len) };
crate::fd::write(fd, slice).map_or_else(
|e| -num::ToPrimitive::to_isize(&e).unwrap(),
|v| v.try_into().unwrap(),
)
}

extern "C" fn __sys_write(fd: FileDescriptor, buf: *const u8, len: usize) -> isize {
write(fd, buf, len)
}

#[no_mangle]
pub extern "C" fn sys_write(fd: FileDescriptor, buf: *const u8, len: usize) -> isize {
kernel_function!(__sys_write(fd, buf, len))
Expand Down
22 changes: 15 additions & 7 deletions src/syscalls/semaphore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,12 +98,7 @@ pub extern "C" fn sys_sem_trywait(sem: *const Semaphore) -> i32 {
kernel_function!(__sys_sem_trywait(sem))
}

/// Try to acquire a lock on a semaphore, blocking for a given amount of milliseconds.
///
/// Blocks until semaphore is acquired or until wake-up time has elapsed.
///
/// Returns `0` on lock acquire, `-EINVAL` if sem is null, or `-ETIME` on timeout.
extern "C" fn __sys_sem_timedwait(sem: *const Semaphore, ms: u32) -> i32 {
fn sem_timedwait(sem: *const Semaphore, ms: u32) -> i32 {
if sem.is_null() {
return -EINVAL;
}
Expand All @@ -119,12 +114,25 @@ extern "C" fn __sys_sem_timedwait(sem: *const Semaphore, ms: u32) -> i32 {
}
}

/// Try to acquire a lock on a semaphore, blocking for a given amount of milliseconds.
///
/// Blocks until semaphore is acquired or until wake-up time has elapsed.
///
/// Returns `0` on lock acquire, `-EINVAL` if sem is null, or `-ETIME` on timeout.
extern "C" fn __sys_sem_timedwait(sem: *const Semaphore, ms: u32) -> i32 {
sem_timedwait(sem, ms)
}

#[no_mangle]
pub extern "C" fn sys_sem_timedwait(sem: *const Semaphore, ms: u32) -> i32 {
kernel_function!(__sys_sem_timedwait(sem, ms))
}

extern "C" fn __sys_sem_cancelablewait(sem: *const Semaphore, ms: u32) -> i32 {
sem_timedwait(sem, ms)
}

#[no_mangle]
pub extern "C" fn sys_sem_cancelablewait(sem: *const Semaphore, ms: u32) -> i32 {
kernel_function!(__sys_sem_timedwait(sem, ms))
kernel_function!(__sys_sem_cancelablewait(sem, ms))
}
26 changes: 15 additions & 11 deletions src/syscalls/socket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -565,6 +565,10 @@ extern "C" fn __sys_getaddrinfo(
-EINVAL
}

extern "C" fn __sys_send(s: i32, mem: *const c_void, len: usize, _flags: i32) -> isize {
super::write(s, mem.cast(), len)
}

extern "C" fn __sys_shutdown_socket(fd: i32, how: i32) -> i32 {
let obj = get_object(fd);
obj.map_or_else(
Expand All @@ -576,12 +580,16 @@ extern "C" fn __sys_shutdown_socket(fd: i32, how: i32) -> i32 {
)
}

extern "C" fn __sys_recv(fd: i32, buf: *mut u8, len: usize) -> isize {
let slice = unsafe { core::slice::from_raw_parts_mut(buf, len) };
crate::fd::read(fd, slice).map_or_else(
|e| -num::ToPrimitive::to_isize(&e).unwrap(),
|v| v.try_into().unwrap(),
)
extern "C" fn __sys_recv(fd: i32, buf: *mut u8, len: usize, flags: i32) -> isize {
if flags == 0 {
let slice = unsafe { core::slice::from_raw_parts_mut(buf, len) };
crate::fd::read(fd, slice).map_or_else(
|e| -num::ToPrimitive::to_isize(&e).unwrap(),
|v| v.try_into().unwrap(),
)
} else {
(-crate::errno::EINVAL).try_into().unwrap()
}
}

extern "C" fn __sys_sendto(
Expand Down Expand Up @@ -745,11 +753,7 @@ pub extern "C" fn sys_shutdown_socket(s: i32, how: i32) -> i32 {

#[no_mangle]
pub extern "C" fn sys_recv(fd: i32, buf: *mut u8, len: usize, flags: i32) -> isize {
if flags == 0 {
kernel_function!(__sys_recv(fd, buf, len))
} else {
(-crate::errno::EINVAL).try_into().unwrap()
}
kernel_function!(__sys_recv(fd, buf, len, flags))
}

#[no_mangle]
Expand Down
42 changes: 33 additions & 9 deletions src/syscalls/tasks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use crate::mm::{task_heap_end, task_heap_start};
use crate::scheduler::task::{Priority, TaskHandle, TaskId};
use crate::scheduler::PerCoreSchedulerExt;
use crate::time::timespec;
use crate::{arch, scheduler, syscalls};
use crate::{arch, scheduler};

#[cfg(feature = "newlib")]
pub type SignalHandler = extern "C" fn(i32);
Expand Down Expand Up @@ -52,9 +52,13 @@ pub extern "C" fn sys_setprio(_id: *const Tid, _prio: i32) -> i32 {
-ENOSYS
}

extern "C" fn __sys_exit(arg: i32) -> ! {
fn exit(arg: i32) -> ! {
debug!("Exit program with error code {}!", arg);
syscalls::__sys_shutdown(arg)
super::shutdown(arg)
}

extern "C" fn __sys_exit(arg: i32) -> ! {
exit(arg)
}

#[no_mangle]
Expand All @@ -72,9 +76,13 @@ pub extern "C" fn sys_thread_exit(arg: i32) -> ! {
kernel_function!(__sys_thread_exit(arg))
}

extern "C" fn __sys_abort() -> ! {
exit(-1)
}

#[no_mangle]
pub extern "C" fn sys_abort() -> ! {
kernel_function!(__sys_exit(-1))
kernel_function!(__sys_abort())
}

#[cfg(feature = "newlib")]
Expand Down Expand Up @@ -109,7 +117,7 @@ pub extern "C" fn sys_sbrk(incr: isize) -> usize {
kernel_function!(__sys_sbrk(incr))
}

pub(crate) extern "C" fn __sys_usleep(usecs: u64) {
fn usleep(usecs: u64) {
if usecs >= 10_000 {
// Enough time to set a wakeup timer and block the current task.
debug!("sys_usleep blocking the task for {} microseconds", usecs);
Expand All @@ -128,14 +136,22 @@ pub(crate) extern "C" fn __sys_usleep(usecs: u64) {
}
}

pub(crate) extern "C" fn __sys_usleep(usecs: u64) {
usleep(usecs)
}

#[no_mangle]
pub extern "C" fn sys_usleep(usecs: u64) {
kernel_function!(__sys_usleep(usecs))
}

pub(crate) extern "C" fn __sys_msleep(ms: u32) {
usleep(u64::from(ms) * 1000)
}

#[no_mangle]
pub extern "C" fn sys_msleep(ms: u32) {
kernel_function!(__sys_usleep(u64::from(ms) * 1000))
kernel_function!(__sys_msleep(ms))
}

extern "C" fn __sys_nanosleep(rqtp: *const timespec, _rmtp: *mut timespec) -> i32 {
Expand Down Expand Up @@ -286,7 +302,7 @@ pub extern "C" fn sys_join(id: Tid) -> i32 {
static BLOCKED_TASKS: InterruptTicketMutex<BTreeMap<TaskId, TaskHandle>> =
InterruptTicketMutex::new(BTreeMap::new());

extern "C" fn __sys_block_current_task(timeout: &Option<u64>) {
fn block_current_task(timeout: &Option<u64>) {
let wakeup_time = timeout.map(|t| arch::processor::get_timer_ticks() + t * 1000);
let core_scheduler = core_scheduler();
let handle = core_scheduler.get_current_task_handle();
Expand All @@ -296,16 +312,24 @@ extern "C" fn __sys_block_current_task(timeout: &Option<u64>) {
core_scheduler.block_current_task(wakeup_time);
}

extern "C" fn __sys_block_current_task() {
block_current_task(&None)
}

/// Set the current task state to `blocked`
#[no_mangle]
pub extern "C" fn sys_block_current_task() {
kernel_function!(__sys_block_current_task(&None))
kernel_function!(__sys_block_current_task())
}

extern "C" fn __sys_block_current_task_with_timeout(timeout: u64) {
block_current_task(&Some(timeout))
}

/// Set the current task state to `blocked`
#[no_mangle]
pub extern "C" fn sys_block_current_task_with_timeout(timeout: u64) {
kernel_function!(__sys_block_current_task(&Some(timeout)))
kernel_function!(__sys_block_current_task_with_timeout(timeout))
}

extern "C" fn __sys_wakeup_task(id: Tid) {
Expand Down

0 comments on commit 6089d53

Please sign in to comment.