Skip to content

Commit

Permalink
feat: implement system calls with attribute proc macro
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 23, 2024
1 parent f9500d7 commit cb14c4b
Show file tree
Hide file tree
Showing 13 changed files with 220 additions and 731 deletions.
40 changes: 10 additions & 30 deletions src/syscalls/condvar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ impl CondQueue {
}
}

unsafe extern "C" fn __sys_destroy_queue(ptr: usize) -> i32 {
#[hermit_macro::system]
pub unsafe extern "C" fn sys_destroy_queue(ptr: usize) -> i32 {
unsafe {
let id = ptr::from_exposed_addr_mut::<usize>(ptr);
if id.is_null() {
Expand All @@ -40,12 +41,8 @@ unsafe extern "C" fn __sys_destroy_queue(ptr: usize) -> i32 {
}
}

#[no_mangle]
pub unsafe extern "C" fn sys_destroy_queue(ptr: usize) -> i32 {
unsafe { kernel_function!(__sys_destroy_queue(ptr)) }
}

unsafe extern "C" fn __sys_notify(ptr: usize, count: i32) -> i32 {
#[hermit_macro::system]
pub unsafe extern "C" fn sys_notify(ptr: usize, count: i32) -> i32 {
unsafe {
let id = ptr::from_exposed_addr::<usize>(ptr);

Expand Down Expand Up @@ -81,12 +78,8 @@ unsafe extern "C" fn __sys_notify(ptr: usize, count: i32) -> i32 {
}
}

#[no_mangle]
pub unsafe extern "C" fn sys_notify(ptr: usize, count: i32) -> i32 {
unsafe { kernel_function!(__sys_notify(ptr, count)) }
}

unsafe extern "C" fn __sys_init_queue(ptr: usize) -> i32 {
#[hermit_macro::system]
pub unsafe extern "C" fn sys_init_queue(ptr: usize) -> i32 {
unsafe {
let id = ptr::from_exposed_addr_mut::<usize>(ptr);
if id.is_null() {
Expand All @@ -104,12 +97,8 @@ unsafe extern "C" fn __sys_init_queue(ptr: usize) -> i32 {
}
}

#[no_mangle]
pub unsafe extern "C" fn sys_init_queue(ptr: usize) -> i32 {
unsafe { kernel_function!(__sys_init_queue(ptr)) }
}

unsafe extern "C" fn __sys_add_queue(ptr: usize, timeout_ns: i64) -> i32 {
#[hermit_macro::system]
pub unsafe extern "C" fn sys_add_queue(ptr: usize, timeout_ns: i64) -> i32 {
unsafe {
let id = ptr::from_exposed_addr_mut::<usize>(ptr);
if id.is_null() {
Expand All @@ -136,12 +125,8 @@ unsafe extern "C" fn __sys_add_queue(ptr: usize, timeout_ns: i64) -> i32 {
}
}

#[no_mangle]
pub unsafe extern "C" fn sys_add_queue(ptr: usize, timeout_ns: i64) -> i32 {
unsafe { kernel_function!(__sys_add_queue(ptr, timeout_ns)) }
}

unsafe extern "C" fn __sys_wait(ptr: usize) -> i32 {
#[hermit_macro::system]
pub unsafe extern "C" fn sys_wait(ptr: usize) -> i32 {
unsafe {
let id = ptr::from_exposed_addr_mut::<usize>(ptr);
if id.is_null() {
Expand All @@ -161,8 +146,3 @@ unsafe extern "C" fn __sys_wait(ptr: usize) -> i32 {
0
}
}

#[no_mangle]
pub unsafe extern "C" fn sys_wait(ptr: usize) -> i32 {
unsafe { kernel_function!(__sys_wait(ptr)) }
}
46 changes: 13 additions & 33 deletions src/syscalls/entropy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,12 @@ pub unsafe extern "C" fn sys_read_entropy(buf: *mut u8, len: usize, flags: u32)
unsafe { kernel_function!(__sys_read_entropy(buf, len, flags)) }
}

/// Create a cryptographicly secure 32bit random number with the support of
/// the underlying hardware. If the required hardware isn't available,
/// the function returns `-1`.
#[cfg(not(feature = "newlib"))]
#[no_mangle]
unsafe extern "C" fn __sys_secure_rand32(value: *mut u32) -> i32 {
#[hermit_macro::system]
pub unsafe extern "C" fn sys_secure_rand32(value: *mut u32) -> i32 {
let mut buf = value.cast();
let mut len = size_of::<u32>();
while len != 0 {
Expand All @@ -76,18 +79,12 @@ unsafe extern "C" fn __sys_secure_rand32(value: *mut u32) -> i32 {
0
}

/// Create a cryptographicly secure 32bit random number with the support of
/// Create a cryptographicly secure 64bit random number with the support of
/// the underlying hardware. If the required hardware isn't available,
/// the function returns `-1`.
#[cfg(not(feature = "newlib"))]
#[no_mangle]
pub unsafe extern "C" fn sys_secure_rand32(value: *mut u32) -> i32 {
unsafe { kernel_function!(__sys_secure_rand32(value)) }
}

/// the function returns -1.
#[cfg(not(feature = "newlib"))]
#[no_mangle]
unsafe extern "C" fn __sys_secure_rand64(value: *mut u64) -> i32 {
#[hermit_macro::system]
pub unsafe extern "C" fn sys_secure_rand64(value: *mut u64) -> i32 {
let mut buf = value.cast();
let mut len = size_of::<u64>();
while len != 0 {
Expand All @@ -103,35 +100,18 @@ unsafe extern "C" fn __sys_secure_rand64(value: *mut u64) -> i32 {
0
}

/// Create a cryptographicly secure 64bit random number with the support of
/// the underlying hardware. If the required hardware isn't available,
/// the function returns -1.
#[cfg(not(feature = "newlib"))]
#[no_mangle]
pub unsafe extern "C" fn sys_secure_rand64(value: *mut u64) -> i32 {
unsafe { kernel_function!(__sys_secure_rand64(value)) }
}

extern "C" fn __sys_rand() -> u32 {
generate_park_miller_lehmer_random_number()
}

/// The function computes a sequence of pseudo-random integers
/// in the range of 0 to RAND_MAX
#[no_mangle]
#[hermit_macro::system]
pub extern "C" fn sys_rand() -> u32 {
kernel_function!(__sys_rand())
}

extern "C" fn __sys_srand(seed: u32) {
*(PARK_MILLER_LEHMER_SEED.lock()) = seed;
generate_park_miller_lehmer_random_number()
}

/// The function sets its argument as the seed for a new sequence
/// of pseudo-random numbers to be returned by rand()
#[no_mangle]
#[hermit_macro::system]
pub extern "C" fn sys_srand(seed: u32) {
kernel_function!(__sys_srand(seed))
*(PARK_MILLER_LEHMER_SEED.lock()) = seed;
}

pub(crate) fn init_entropy() {
Expand Down
21 changes: 4 additions & 17 deletions src/syscalls/futex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ use crate::time::timespec;
/// * `address` is null
/// * `timeout` is negative
/// * `flags` contains unknown flags
unsafe extern "C" fn __sys_futex_wait(
#[hermit_macro::system]
pub unsafe extern "C" fn sys_futex_wait(
address: *mut u32,
expected: u32,
timeout: *const timespec,
Expand All @@ -37,29 +38,15 @@ unsafe extern "C" fn __sys_futex_wait(
synch::futex_wait(address, expected, timeout, flags)
}

#[no_mangle]
pub unsafe extern "C" fn sys_futex_wait(
address: *mut u32,
expected: u32,
timeout: *const timespec,
flags: u32,
) -> i32 {
unsafe { kernel_function!(__sys_futex_wait(address, expected, timeout, flags)) }
}

/// Like `synch::futex_wake`, but does extra sanity checks.
///
/// Returns -EINVAL if `address` is null.
unsafe extern "C" fn __sys_futex_wake(address: *mut u32, count: i32) -> i32 {
#[hermit_macro::system]
pub unsafe extern "C" fn sys_futex_wake(address: *mut u32, count: i32) -> i32 {
if address.is_null() {
return -EINVAL;
}

let address = unsafe { &*(address as *const AtomicU32) };
synch::futex_wake(address, count)
}

#[no_mangle]
pub unsafe extern "C" fn sys_futex_wake(address: *mut u32, count: i32) -> i32 {
unsafe { kernel_function!(__sys_futex_wake(address, count)) }
}
40 changes: 10 additions & 30 deletions src/syscalls/lwip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,51 +4,31 @@ use lock_api::MutexGuard;
use crate::arch::core_local::core_scheduler;
use crate::{arch, console};

extern "C" fn __sys_lwip_get_errno() -> i32 {
core_scheduler().get_lwip_errno()
}

#[no_mangle]
#[hermit_macro::system]
pub extern "C" fn sys_lwip_get_errno() -> i32 {
kernel_function!(__sys_lwip_get_errno())
}

extern "C" fn __sys_lwip_set_errno(errno: i32) {
core_scheduler().set_lwip_errno(errno);
core_scheduler().get_lwip_errno()
}

#[no_mangle]
#[hermit_macro::system]
pub extern "C" fn sys_lwip_set_errno(errno: i32) {
kernel_function!(__sys_lwip_set_errno(errno))
core_scheduler().set_lwip_errno(errno);
}

extern "C" fn __sys_acquire_putchar_lock() {
#[hermit_macro::system]
pub extern "C" fn sys_acquire_putchar_lock() {
// FIXME: use core-local storage instead
// better yet: remove and replace all of this
MutexGuard::leak(console::CONSOLE.lock());
}

#[no_mangle]
pub extern "C" fn sys_acquire_putchar_lock() {
kernel_function!(__sys_acquire_putchar_lock())
}

extern "C" fn __sys_putchar(character: u8) {
arch::output_message_buf(&[character]);
}

#[no_mangle]
#[hermit_macro::system]
pub extern "C" fn sys_putchar(character: u8) {
kernel_function!(__sys_putchar(character))
arch::output_message_buf(&[character]);
}

unsafe extern "C" fn __sys_release_putchar_lock() {
#[hermit_macro::system]
pub unsafe extern "C" fn sys_release_putchar_lock() {
unsafe {
console::CONSOLE.force_unlock();
}
}

#[no_mangle]
pub unsafe extern "C" fn sys_release_putchar_lock() {
unsafe { kernel_function!(__sys_release_putchar_lock()) }
}
Loading

0 comments on commit cb14c4b

Please sign in to comment.