From 6089d53062d9c562995d73f03a2a54cfe06a88b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Kr=C3=B6ning?= Date: Fri, 22 Mar 2024 17:37:39 +0100 Subject: [PATCH] fix(syscalls): don't call foreign `__sys` functions MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Martin Kröning --- src/syscalls/mod.rs | 12 +++++++++-- src/syscalls/semaphore.rs | 22 +++++++++++++------- src/syscalls/socket.rs | 26 ++++++++++++++---------- src/syscalls/tasks.rs | 42 ++++++++++++++++++++++++++++++--------- 4 files changed, 73 insertions(+), 29 deletions(-) diff --git a/src/syscalls/mod.rs b/src/syscalls/mod.rs index bac0a774fe..d179f9d740 100644 --- a/src/syscalls/mod.rs +++ b/src/syscalls/mod.rs @@ -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)) @@ -386,7 +390,7 @@ 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(), @@ -394,6 +398,10 @@ extern "C" fn __sys_write(fd: FileDescriptor, buf: *const u8, len: usize) -> isi ) } +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)) diff --git a/src/syscalls/semaphore.rs b/src/syscalls/semaphore.rs index e493bb706d..03f1da86f3 100644 --- a/src/syscalls/semaphore.rs +++ b/src/syscalls/semaphore.rs @@ -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; } @@ -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)) } diff --git a/src/syscalls/socket.rs b/src/syscalls/socket.rs index ef785823fd..4f4845772a 100644 --- a/src/syscalls/socket.rs +++ b/src/syscalls/socket.rs @@ -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( @@ -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( @@ -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] diff --git a/src/syscalls/tasks.rs b/src/syscalls/tasks.rs index dfe07097d7..f9a7f55949 100644 --- a/src/syscalls/tasks.rs +++ b/src/syscalls/tasks.rs @@ -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); @@ -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] @@ -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")] @@ -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); @@ -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 { @@ -286,7 +302,7 @@ pub extern "C" fn sys_join(id: Tid) -> i32 { static BLOCKED_TASKS: InterruptTicketMutex> = InterruptTicketMutex::new(BTreeMap::new()); -extern "C" fn __sys_block_current_task(timeout: &Option) { +fn block_current_task(timeout: &Option) { 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(); @@ -296,16 +312,24 @@ extern "C" fn __sys_block_current_task(timeout: &Option) { 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) {