From cb14c4b28e8052b57d31d76ec3b92b1cd8e06568 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Kr=C3=B6ning?= Date: Sat, 23 Mar 2024 13:07:28 +0100 Subject: [PATCH] feat: implement system calls with attribute proc macro MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Martin Kröning --- src/syscalls/condvar.rs | 40 ++------ src/syscalls/entropy.rs | 46 +++------ src/syscalls/futex.rs | 21 +--- src/syscalls/lwip.rs | 40 ++------ src/syscalls/mod.rs | 202 +++++++++---------------------------- src/syscalls/processor.rs | 16 +-- src/syscalls/recmutex.rs | 32 ++---- src/syscalls/semaphore.rs | 46 +++------ src/syscalls/socket.rs | 167 ++++++------------------------- src/syscalls/spinlock.rs | 72 ++++---------- src/syscalls/system.rs | 8 +- src/syscalls/tasks.rs | 203 +++++++++----------------------------- src/syscalls/timer.rs | 58 +++-------- 13 files changed, 220 insertions(+), 731 deletions(-) diff --git a/src/syscalls/condvar.rs b/src/syscalls/condvar.rs index 0474a7e7bd..ef70b0c664 100644 --- a/src/syscalls/condvar.rs +++ b/src/syscalls/condvar.rs @@ -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::(ptr); if id.is_null() { @@ -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::(ptr); @@ -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::(ptr); if id.is_null() { @@ -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::(ptr); if id.is_null() { @@ -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::(ptr); if id.is_null() { @@ -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)) } -} diff --git a/src/syscalls/entropy.rs b/src/syscalls/entropy.rs index 5d61f9b644..365d69e513 100644 --- a/src/syscalls/entropy.rs +++ b/src/syscalls/entropy.rs @@ -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::(); while len != 0 { @@ -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::(); while len != 0 { @@ -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() { diff --git a/src/syscalls/futex.rs b/src/syscalls/futex.rs index fc942d756e..c2be9aac05 100644 --- a/src/syscalls/futex.rs +++ b/src/syscalls/futex.rs @@ -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, @@ -37,20 +38,11 @@ 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; } @@ -58,8 +50,3 @@ unsafe extern "C" fn __sys_futex_wake(address: *mut u32, count: i32) -> i32 { 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)) } -} diff --git a/src/syscalls/lwip.rs b/src/syscalls/lwip.rs index 2747056115..812f8c6e82 100644 --- a/src/syscalls/lwip.rs +++ b/src/syscalls/lwip.rs @@ -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()) } -} diff --git a/src/syscalls/mod.rs b/src/syscalls/mod.rs index 07f7fe4f56..c0e80ca64c 100644 --- a/src/syscalls/mod.rs +++ b/src/syscalls/mod.rs @@ -81,7 +81,8 @@ pub(crate) fn init() { /// `size` and `align` do not meet this allocator's size or alignment constraints. /// #[cfg(all(target_os = "none", not(feature = "common-os")))] -pub(crate) extern "C" fn __sys_malloc(size: usize, align: usize) -> *mut u8 { +#[hermit_macro::system] +pub extern "C" fn sys_malloc(size: usize, align: usize) -> *mut u8 { let layout_res = Layout::from_size_align(size, align); if layout_res.is_err() || size == 0 { warn!( @@ -103,12 +104,6 @@ pub(crate) extern "C" fn __sys_malloc(size: usize, align: usize) -> *mut u8 { ptr } -#[cfg(all(target_os = "none", not(feature = "common-os")))] -#[no_mangle] -pub extern "C" fn sys_malloc(size: usize, align: usize) -> *mut u8 { - kernel_function!(__sys_malloc(size, align)) -} - /// Shrink or grow a block of memory to the given `new_size`. The block is described by the given /// ptr pointer and layout. If this returns a non-null pointer, then ownership of the memory block /// referenced by ptr has been transferred to this allocator. The memory may or may not have been @@ -129,7 +124,8 @@ pub extern "C" fn sys_malloc(size: usize, align: usize) -> *mut u8 { /// Returns null if the new layout does not meet the size and alignment constraints of the /// allocator, or if reallocation otherwise fails. #[cfg(all(target_os = "none", not(feature = "common-os")))] -pub(crate) unsafe extern "C" fn __sys_realloc( +#[hermit_macro::system] +pub unsafe extern "C" fn sys_realloc( ptr: *mut u8, size: usize, align: usize, @@ -163,17 +159,6 @@ pub(crate) unsafe extern "C" fn __sys_realloc( } } -#[cfg(all(target_os = "none", not(feature = "common-os")))] -#[no_mangle] -pub unsafe extern "C" fn sys_realloc( - ptr: *mut u8, - size: usize, - align: usize, - new_size: usize, -) -> *mut u8 { - unsafe { kernel_function!(__sys_realloc(ptr, size, align, new_size)) } -} - /// Interface to deallocate a memory region from the system heap /// /// # Safety @@ -185,7 +170,8 @@ pub unsafe extern "C" fn sys_realloc( /// # Errors /// May panic if debug assertions are enabled and invalid parameters `size` or `align` where passed. #[cfg(all(target_os = "none", not(feature = "common-os")))] -pub(crate) unsafe extern "C" fn __sys_free(ptr: *mut u8, size: usize, align: usize) { +#[hermit_macro::system] +pub unsafe extern "C" fn sys_free(ptr: *mut u8, size: usize, align: usize) { unsafe { let layout_res = Layout::from_size_align(size, align); if layout_res.is_err() || size == 0 { @@ -207,12 +193,6 @@ pub(crate) unsafe extern "C" fn __sys_free(ptr: *mut u8, size: usize, align: usi } } -#[cfg(all(target_os = "none", not(feature = "common-os")))] -#[no_mangle] -pub unsafe extern "C" fn sys_free(ptr: *mut u8, size: usize, align: usize) { - unsafe { kernel_function!(__sys_free(ptr, size, align)) } -} - pub(crate) fn get_application_parameters() -> (i32, *const *const u8, *const *const u8) { SYS.get_application_parameters() } @@ -224,16 +204,13 @@ pub(crate) fn shutdown(arg: i32) -> ! { SYS.shutdown(arg) } -pub(crate) extern "C" fn __sys_shutdown(arg: i32) -> ! { - shutdown(arg) -} - -#[no_mangle] +#[hermit_macro::system] pub extern "C" fn sys_shutdown(arg: i32) -> ! { - kernel_function!(__sys_shutdown(arg)) + shutdown(arg) } -unsafe extern "C" fn __sys_unlink(name: *const u8) -> i32 { +#[hermit_macro::system] +pub unsafe extern "C" fn sys_unlink(name: *const u8) -> i32 { let name = unsafe { CStr::from_ptr(name as _) }.to_str().unwrap(); fs::FILESYSTEM @@ -243,12 +220,8 @@ unsafe extern "C" fn __sys_unlink(name: *const u8) -> i32 { .map_or_else(|e| -num::ToPrimitive::to_i32(&e).unwrap(), |_| 0) } -#[no_mangle] -pub unsafe extern "C" fn sys_unlink(name: *const u8) -> i32 { - unsafe { kernel_function!(__sys_unlink(name)) } -} - -unsafe extern "C" fn __sys_mkdir(name: *const u8, mode: u32) -> i32 { +#[hermit_macro::system] +pub unsafe extern "C" fn sys_mkdir(name: *const u8, mode: u32) -> i32 { let name = unsafe { CStr::from_ptr(name as _) }.to_str().unwrap(); let mode = if let Some(mode) = AccessPermission::from_bits(mode) { mode @@ -263,12 +236,8 @@ unsafe extern "C" fn __sys_mkdir(name: *const u8, mode: u32) -> i32 { .map_or_else(|e| -num::ToPrimitive::to_i32(&e).unwrap(), |_| 0) } -#[no_mangle] -pub unsafe extern "C" fn sys_mkdir(name: *const u8, mode: u32) -> i32 { - unsafe { kernel_function!(__sys_mkdir(name, mode)) } -} - -unsafe extern "C" fn __sys_rmdir(name: *const u8) -> i32 { +#[hermit_macro::system] +pub unsafe extern "C" fn sys_rmdir(name: *const u8) -> i32 { let name = unsafe { CStr::from_ptr(name as _) }.to_str().unwrap(); fs::FILESYSTEM @@ -278,12 +247,8 @@ unsafe extern "C" fn __sys_rmdir(name: *const u8) -> i32 { .map_or_else(|e| -num::ToPrimitive::to_i32(&e).unwrap(), |_| 0) } -#[no_mangle] -pub unsafe extern "C" fn sys_rmdir(name: *const u8) -> i32 { - unsafe { kernel_function!(__sys_rmdir(name)) } -} - -unsafe extern "C" fn __sys_stat(name: *const u8, stat: *mut FileAttr) -> i32 { +#[hermit_macro::system] +pub unsafe extern "C" fn sys_stat(name: *const u8, stat: *mut FileAttr) -> i32 { let name = unsafe { CStr::from_ptr(name as _) }.to_str().unwrap(); match fs::FILESYSTEM.get().unwrap().stat(name) { @@ -295,12 +260,8 @@ unsafe extern "C" fn __sys_stat(name: *const u8, stat: *mut FileAttr) -> i32 { } } -#[no_mangle] -pub unsafe extern "C" fn sys_stat(name: *const u8, stat: *mut FileAttr) -> i32 { - unsafe { kernel_function!(__sys_stat(name, stat)) } -} - -unsafe extern "C" fn __sys_lstat(name: *const u8, stat: *mut FileAttr) -> i32 { +#[hermit_macro::system] +pub unsafe extern "C" fn sys_lstat(name: *const u8, stat: *mut FileAttr) -> i32 { let name = unsafe { CStr::from_ptr(name as _) }.to_str().unwrap(); match fs::FILESYSTEM.get().unwrap().lstat(name) { @@ -312,12 +273,8 @@ unsafe extern "C" fn __sys_lstat(name: *const u8, stat: *mut FileAttr) -> i32 { } } -#[no_mangle] -pub unsafe extern "C" fn sys_lstat(name: *const u8, stat: *mut FileAttr) -> i32 { - unsafe { kernel_function!(__sys_lstat(name, stat)) } -} - -unsafe extern "C" fn __sys_fstat(fd: FileDescriptor, stat: *mut FileAttr) -> i32 { +#[hermit_macro::system] +pub unsafe extern "C" fn sys_fstat(fd: FileDescriptor, stat: *mut FileAttr) -> i32 { let stat = unsafe { &mut *stat }; let obj = get_object(fd); obj.map_or_else( @@ -329,12 +286,8 @@ unsafe extern "C" fn __sys_fstat(fd: FileDescriptor, stat: *mut FileAttr) -> i32 ) } -#[no_mangle] -pub unsafe extern "C" fn sys_fstat(fd: FileDescriptor, stat: *mut FileAttr) -> i32 { - unsafe { kernel_function!(__sys_fstat(fd, stat)) } -} - -unsafe extern "C" fn __sys_opendir(name: *const u8) -> FileDescriptor { +#[hermit_macro::system] +pub unsafe extern "C" fn sys_opendir(name: *const u8) -> FileDescriptor { if let Ok(name) = unsafe { CStr::from_ptr(name as _) }.to_str() { crate::fs::opendir(name).unwrap_or_else(|e| -num::ToPrimitive::to_i32(&e).unwrap()) } else { @@ -342,12 +295,8 @@ unsafe extern "C" fn __sys_opendir(name: *const u8) -> FileDescriptor { } } -#[no_mangle] -pub unsafe extern "C" fn sys_opendir(name: *const u8) -> FileDescriptor { - unsafe { kernel_function!(__sys_opendir(name)) } -} - -unsafe extern "C" fn __sys_open(name: *const u8, flags: i32, mode: u32) -> FileDescriptor { +#[hermit_macro::system] +pub unsafe extern "C" fn sys_open(name: *const u8, flags: i32, mode: u32) -> FileDescriptor { let flags = if let Some(flags) = OpenOption::from_bits(flags) { flags } else { @@ -367,22 +316,14 @@ unsafe extern "C" fn __sys_open(name: *const u8, flags: i32, mode: u32) -> FileD } } -#[no_mangle] -pub unsafe extern "C" fn sys_open(name: *const u8, flags: i32, mode: u32) -> FileDescriptor { - unsafe { kernel_function!(__sys_open(name, flags, mode)) } -} - -extern "C" fn __sys_close(fd: FileDescriptor) -> i32 { +#[hermit_macro::system] +pub extern "C" fn sys_close(fd: FileDescriptor) -> i32 { let obj = remove_object(fd); obj.map_or_else(|e| -num::ToPrimitive::to_i32(&e).unwrap(), |_| 0) } -#[no_mangle] -pub extern "C" fn sys_close(fd: FileDescriptor) -> i32 { - kernel_function!(__sys_close(fd)) -} - -unsafe extern "C" fn __sys_read(fd: FileDescriptor, buf: *mut u8, len: usize) -> isize { +#[hermit_macro::system] +pub unsafe extern "C" fn sys_read(fd: FileDescriptor, 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(), @@ -390,11 +331,6 @@ unsafe extern "C" fn __sys_read(fd: FileDescriptor, buf: *mut u8, len: usize) -> ) } -#[no_mangle] -pub unsafe extern "C" fn sys_read(fd: FileDescriptor, buf: *mut u8, len: usize) -> isize { - unsafe { kernel_function!(__sys_read(fd, buf, len)) } -} - unsafe 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( @@ -403,16 +339,13 @@ unsafe fn write(fd: FileDescriptor, buf: *const u8, len: usize) -> isize { ) } -unsafe extern "C" fn __sys_write(fd: FileDescriptor, buf: *const u8, len: usize) -> isize { - unsafe { write(fd, buf, len) } -} - -#[no_mangle] +#[hermit_macro::system] pub unsafe extern "C" fn sys_write(fd: FileDescriptor, buf: *const u8, len: usize) -> isize { - unsafe { kernel_function!(__sys_write(fd, buf, len)) } + unsafe { write(fd, buf, len) } } -unsafe extern "C" fn __sys_ioctl( +#[hermit_macro::system] +pub unsafe extern "C" fn sys_ioctl( fd: FileDescriptor, cmd: i32, argp: *mut core::ffi::c_void, @@ -435,16 +368,9 @@ unsafe extern "C" fn __sys_ioctl( } } -#[no_mangle] -pub unsafe extern "C" fn sys_ioctl( - fd: FileDescriptor, - cmd: i32, - argp: *mut core::ffi::c_void, -) -> i32 { - unsafe { kernel_function!(__sys_ioctl(fd, cmd, argp)) } -} - -extern "C" fn __sys_fcntl(fd: i32, cmd: i32, arg: i32) -> i32 { +/// manipulate file descriptor +#[hermit_macro::system] +pub extern "C" fn sys_fcntl(fd: i32, cmd: i32, arg: i32) -> i32 { const F_SETFD: i32 = 2; const F_SETFL: i32 = 4; const FD_CLOEXEC: i32 = 1; @@ -466,13 +392,8 @@ extern "C" fn __sys_fcntl(fd: i32, cmd: i32, arg: i32) -> i32 { } } -/// manipulate file descriptor -#[no_mangle] -pub extern "C" fn sys_fcntl(fd: i32, cmd: i32, arg: i32) -> i32 { - kernel_function!(__sys_fcntl(fd, cmd, arg)) -} - -extern "C" fn __sys_lseek(fd: FileDescriptor, offset: isize, whence: i32) -> isize { +#[hermit_macro::system] +pub extern "C" fn sys_lseek(fd: FileDescriptor, offset: isize, whence: i32) -> isize { let obj = get_object(fd); obj.map_or_else( |e| -num::ToPrimitive::to_isize(&e).unwrap(), @@ -483,11 +404,6 @@ extern "C" fn __sys_lseek(fd: FileDescriptor, offset: isize, whence: i32) -> isi ) } -#[no_mangle] -pub extern "C" fn sys_lseek(fd: FileDescriptor, offset: isize, whence: i32) -> isize { - kernel_function!(__sys_lseek(fd, offset, whence)) -} - #[repr(C)] #[derive(Debug, Clone, Copy)] pub struct Dirent64 { @@ -503,7 +419,8 @@ pub struct Dirent64 { pub d_name: PhantomData, } -unsafe extern "C" fn __sys_getdents64( +#[hermit_macro::system] +pub unsafe extern "C" fn sys_getdents64( fd: FileDescriptor, dirp: *mut Dirent64, count: usize, @@ -557,25 +474,13 @@ unsafe extern "C" fn __sys_getdents64( ) } -#[no_mangle] -pub unsafe extern "C" fn sys_getdents64( - fd: FileDescriptor, - dirp: *mut Dirent64, - count: usize, -) -> i64 { - unsafe { kernel_function!(__sys_getdents64(fd, dirp, count)) } -} - -extern "C" fn __sys_dup(fd: i32) -> i32 { - dup_object(fd).unwrap_or_else(|e| -num::ToPrimitive::to_i32(&e).unwrap()) -} - -#[no_mangle] +#[hermit_macro::system] pub extern "C" fn sys_dup(fd: i32) -> i32 { - kernel_function!(__sys_dup(fd)) + dup_object(fd).unwrap_or_else(|e| -num::ToPrimitive::to_i32(&e).unwrap()) } -unsafe extern "C" fn __sys_poll(fds: *mut PollFd, nfds: usize, timeout: i32) -> i32 { +#[hermit_macro::system] +pub unsafe extern "C" fn sys_poll(fds: *mut PollFd, nfds: usize, timeout: i32) -> i32 { let slice = unsafe { core::slice::from_raw_parts_mut(fds, nfds) }; let timeout = if timeout >= 0 { Some(core::time::Duration::from_millis( @@ -597,12 +502,8 @@ unsafe extern "C" fn __sys_poll(fds: *mut PollFd, nfds: usize, timeout: i32) -> ) } -#[no_mangle] -pub unsafe extern "C" fn sys_poll(fds: *mut PollFd, nfds: usize, timeout: i32) -> i32 { - unsafe { kernel_function!(__sys_poll(fds, nfds, timeout)) } -} - -extern "C" fn __sys_eventfd(initval: u64, flags: i16) -> i32 { +#[hermit_macro::system] +pub extern "C" fn sys_eventfd(initval: u64, flags: i16) -> i32 { if let Some(flags) = EventFlags::from_bits(flags) { crate::fd::eventfd(initval, flags) .unwrap_or_else(|e| -num::ToPrimitive::to_i32(&e).unwrap()) @@ -611,16 +512,7 @@ extern "C" fn __sys_eventfd(initval: u64, flags: i16) -> i32 { } } -#[no_mangle] -pub extern "C" fn sys_eventfd(initval: u64, flags: i16) -> i32 { - kernel_function!(__sys_eventfd(initval, flags)) -} - -extern "C" fn __sys_image_start_addr() -> usize { - crate::mm::kernel_start_address().0.try_into().unwrap() -} - -#[no_mangle] +#[hermit_macro::system] pub extern "C" fn sys_image_start_addr() -> usize { - kernel_function!(__sys_image_start_addr()) + crate::mm::kernel_start_address().0.try_into().unwrap() } diff --git a/src/syscalls/processor.rs b/src/syscalls/processor.rs index af94f5687d..c15e8ad527 100644 --- a/src/syscalls/processor.rs +++ b/src/syscalls/processor.rs @@ -1,21 +1,13 @@ use crate::arch::get_processor_count; -extern "C" fn __sys_get_processor_count() -> usize { - get_processor_count().try_into().unwrap() -} - /// Returns the number of processors currently online. -#[no_mangle] +#[hermit_macro::system] pub extern "C" fn sys_get_processor_count() -> usize { - kernel_function!(__sys_get_processor_count()) -} - -extern "C" fn __sys_get_processor_frequency() -> u16 { - crate::arch::processor::get_frequency() + get_processor_count().try_into().unwrap() } /// Returns the processor frequency in MHz. -#[no_mangle] +#[hermit_macro::system] pub extern "C" fn sys_get_processor_frequency() -> u16 { - kernel_function!(__sys_get_processor_frequency()) + crate::arch::processor::get_frequency() } diff --git a/src/syscalls/recmutex.rs b/src/syscalls/recmutex.rs index 639e2dd1ae..2b2021d9e9 100644 --- a/src/syscalls/recmutex.rs +++ b/src/syscalls/recmutex.rs @@ -3,7 +3,8 @@ use alloc::boxed::Box; use crate::errno::*; use crate::synch::recmutex::RecursiveMutex; -extern "C" fn __sys_recmutex_init(recmutex: *mut *mut RecursiveMutex) -> i32 { +#[hermit_macro::system] +pub extern "C" fn sys_recmutex_init(recmutex: *mut *mut RecursiveMutex) -> i32 { if recmutex.is_null() { return -EINVAL; } @@ -17,12 +18,8 @@ extern "C" fn __sys_recmutex_init(recmutex: *mut *mut RecursiveMutex) -> i32 { 0 } -#[no_mangle] -pub extern "C" fn sys_recmutex_init(recmutex: *mut *mut RecursiveMutex) -> i32 { - kernel_function!(__sys_recmutex_init(recmutex)) -} - -extern "C" fn __sys_recmutex_destroy(recmutex: *mut RecursiveMutex) -> i32 { +#[hermit_macro::system] +pub extern "C" fn sys_recmutex_destroy(recmutex: *mut RecursiveMutex) -> i32 { if recmutex.is_null() { return -EINVAL; } @@ -36,12 +33,8 @@ extern "C" fn __sys_recmutex_destroy(recmutex: *mut RecursiveMutex) -> i32 { 0 } -#[no_mangle] -pub extern "C" fn sys_recmutex_destroy(recmutex: *mut RecursiveMutex) -> i32 { - kernel_function!(__sys_recmutex_destroy(recmutex)) -} - -extern "C" fn __sys_recmutex_lock(recmutex: *mut RecursiveMutex) -> i32 { +#[hermit_macro::system] +pub extern "C" fn sys_recmutex_lock(recmutex: *mut RecursiveMutex) -> i32 { if recmutex.is_null() { return -EINVAL; } @@ -52,12 +45,8 @@ extern "C" fn __sys_recmutex_lock(recmutex: *mut RecursiveMutex) -> i32 { 0 } -#[no_mangle] -pub extern "C" fn sys_recmutex_lock(recmutex: *mut RecursiveMutex) -> i32 { - kernel_function!(__sys_recmutex_lock(recmutex)) -} - -extern "C" fn __sys_recmutex_unlock(recmutex: *mut RecursiveMutex) -> i32 { +#[hermit_macro::system] +pub extern "C" fn sys_recmutex_unlock(recmutex: *mut RecursiveMutex) -> i32 { if recmutex.is_null() { return -EINVAL; } @@ -67,8 +56,3 @@ extern "C" fn __sys_recmutex_unlock(recmutex: *mut RecursiveMutex) -> i32 { 0 } - -#[no_mangle] -pub extern "C" fn sys_recmutex_unlock(recmutex: *mut RecursiveMutex) -> i32 { - kernel_function!(__sys_recmutex_unlock(recmutex)) -} diff --git a/src/syscalls/semaphore.rs b/src/syscalls/semaphore.rs index 16f3a748e4..fcc66c57ab 100644 --- a/src/syscalls/semaphore.rs +++ b/src/syscalls/semaphore.rs @@ -9,7 +9,8 @@ use crate::synch::semaphore::Semaphore; /// /// Stores the raw memory location of the new semaphore in parameter `sem`. /// Returns `0` on success, `-EINVAL` if `sem` is null. -unsafe extern "C" fn __sys_sem_init(sem: *mut *mut Semaphore, value: u32) -> i32 { +#[hermit_macro::system] +pub unsafe extern "C" fn sys_sem_init(sem: *mut *mut Semaphore, value: u32) -> i32 { if sem.is_null() { return -EINVAL; } @@ -22,17 +23,13 @@ unsafe extern "C" fn __sys_sem_init(sem: *mut *mut Semaphore, value: u32) -> i32 0 } -#[no_mangle] -pub unsafe extern "C" fn sys_sem_init(sem: *mut *mut Semaphore, value: u32) -> i32 { - unsafe { kernel_function!(__sys_sem_init(sem, value)) } -} - /// Destroy and deallocate a semaphore. /// /// This function can be used to manually deallocate a semaphore via a reference. /// /// Returns `0` on success, `-EINVAL` if `sem` is null. -unsafe extern "C" fn __sys_sem_destroy(sem: *mut Semaphore) -> i32 { +#[hermit_macro::system] +pub unsafe extern "C" fn sys_sem_destroy(sem: *mut Semaphore) -> i32 { if sem.is_null() { return -EINVAL; } @@ -45,11 +42,6 @@ unsafe extern "C" fn __sys_sem_destroy(sem: *mut Semaphore) -> i32 { 0 } -#[no_mangle] -pub unsafe extern "C" fn sys_sem_destroy(sem: *mut Semaphore) -> i32 { - unsafe { kernel_function!(__sys_sem_destroy(sem)) } -} - /// Release a semaphore. /// /// This function can be used to allow the next blocked waiter to access this semaphore. @@ -57,7 +49,8 @@ pub unsafe extern "C" fn sys_sem_destroy(sem: *mut Semaphore) -> i32 { /// The semaphore is not deallocated after being released. /// /// Returns `0` on success, or `-EINVAL` if `sem` is null. -unsafe extern "C" fn __sys_sem_post(sem: *const Semaphore) -> i32 { +#[hermit_macro::system] +pub unsafe extern "C" fn sys_sem_post(sem: *const Semaphore) -> i32 { if sem.is_null() { return -EINVAL; } @@ -68,18 +61,14 @@ unsafe extern "C" fn __sys_sem_post(sem: *const Semaphore) -> i32 { 0 } -#[no_mangle] -pub unsafe extern "C" fn sys_sem_post(sem: *const Semaphore) -> i32 { - unsafe { kernel_function!(__sys_sem_post(sem)) } -} - /// Try to acquire a lock on a semaphore. /// /// This function does not block if the acquire fails. /// If the acquire fails (i.e. the semaphore's count is already 0), the function returns immediately. /// /// Returns `0` on lock acquire, `-EINVAL` if `sem` is null, or `-ECANCELED` if the decrement fails. -unsafe extern "C" fn __sys_sem_trywait(sem: *const Semaphore) -> i32 { +#[hermit_macro::system] +pub unsafe extern "C" fn sys_sem_trywait(sem: *const Semaphore) -> i32 { if sem.is_null() { return -EINVAL; } @@ -93,11 +82,6 @@ unsafe extern "C" fn __sys_sem_trywait(sem: *const Semaphore) -> i32 { } } -#[no_mangle] -pub unsafe extern "C" fn sys_sem_trywait(sem: *const Semaphore) -> i32 { - unsafe { kernel_function!(__sys_sem_trywait(sem)) } -} - unsafe fn sem_timedwait(sem: *const Semaphore, ms: u32) -> i32 { if sem.is_null() { return -EINVAL; @@ -119,20 +103,12 @@ unsafe fn sem_timedwait(sem: *const Semaphore, ms: u32) -> i32 { /// 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. -unsafe extern "C" fn __sys_sem_timedwait(sem: *const Semaphore, ms: u32) -> i32 { - unsafe { sem_timedwait(sem, ms) } -} - -#[no_mangle] +#[hermit_macro::system] pub unsafe extern "C" fn sys_sem_timedwait(sem: *const Semaphore, ms: u32) -> i32 { - unsafe { kernel_function!(__sys_sem_timedwait(sem, ms)) } -} - -unsafe extern "C" fn __sys_sem_cancelablewait(sem: *const Semaphore, ms: u32) -> i32 { unsafe { sem_timedwait(sem, ms) } } -#[no_mangle] +#[hermit_macro::system] pub unsafe extern "C" fn sys_sem_cancelablewait(sem: *const Semaphore, ms: u32) -> i32 { - unsafe { kernel_function!(__sys_sem_cancelablewait(sem, ms)) } + unsafe { sem_timedwait(sem, ms) } } diff --git a/src/syscalls/socket.rs b/src/syscalls/socket.rs index 8f9d7c8a68..2ba35ca085 100644 --- a/src/syscalls/socket.rs +++ b/src/syscalls/socket.rs @@ -256,7 +256,8 @@ pub struct linger { pub l_linger: i32, } -extern "C" fn __sys_socket(domain: i32, type_: SockType, protocol: i32) -> i32 { +#[hermit_macro::system] +pub extern "C" fn sys_socket(domain: i32, type_: SockType, protocol: i32) -> i32 { debug!( "sys_socket: domain {}, type {:?}, protocol {}", domain, type_, protocol @@ -308,7 +309,8 @@ extern "C" fn __sys_socket(domain: i32, type_: SockType, protocol: i32) -> i32 { } } -unsafe extern "C" fn __sys_accept(fd: i32, addr: *mut sockaddr, addrlen: *mut socklen_t) -> i32 { +#[hermit_macro::system] +pub unsafe extern "C" fn sys_accept(fd: i32, addr: *mut sockaddr, addrlen: *mut socklen_t) -> i32 { let obj = get_object(fd); obj.map_or_else( |e| -num::ToPrimitive::to_i32(&e).unwrap(), @@ -348,7 +350,8 @@ unsafe extern "C" fn __sys_accept(fd: i32, addr: *mut sockaddr, addrlen: *mut so ) } -extern "C" fn __sys_listen(fd: i32, backlog: i32) -> i32 { +#[hermit_macro::system] +pub extern "C" fn sys_listen(fd: i32, backlog: i32) -> i32 { let obj = get_object(fd); obj.map_or_else( |e| -num::ToPrimitive::to_i32(&e).unwrap(), @@ -359,7 +362,8 @@ extern "C" fn __sys_listen(fd: i32, backlog: i32) -> i32 { ) } -unsafe extern "C" fn __sys_bind(fd: i32, name: *const sockaddr, namelen: socklen_t) -> i32 { +#[hermit_macro::system] +pub unsafe extern "C" fn sys_bind(fd: i32, name: *const sockaddr, namelen: socklen_t) -> i32 { let endpoint = if namelen == size_of::().try_into().unwrap() { IpListenEndpoint::from(unsafe { *(name as *const sockaddr_in) }) } else if namelen == size_of::().try_into().unwrap() { @@ -378,7 +382,8 @@ unsafe extern "C" fn __sys_bind(fd: i32, name: *const sockaddr, namelen: socklen ) } -unsafe extern "C" fn __sys_connect(fd: i32, name: *const sockaddr, namelen: socklen_t) -> i32 { +#[hermit_macro::system] +pub unsafe extern "C" fn sys_connect(fd: i32, name: *const sockaddr, namelen: socklen_t) -> i32 { let endpoint = if namelen == size_of::().try_into().unwrap() { IpEndpoint::from(unsafe { *(name as *const sockaddr_in) }) } else if namelen == size_of::().try_into().unwrap() { @@ -397,7 +402,8 @@ unsafe extern "C" fn __sys_connect(fd: i32, name: *const sockaddr, namelen: sock ) } -unsafe extern "C" fn __sys_getsockname( +#[hermit_macro::system] +pub unsafe extern "C" fn sys_getsockname( fd: i32, addr: *mut sockaddr, addrlen: *mut socklen_t, @@ -440,7 +446,8 @@ unsafe extern "C" fn __sys_getsockname( ) } -unsafe extern "C" fn __sys_setsockopt( +#[hermit_macro::system] +pub unsafe extern "C" fn sys_setsockopt( fd: i32, level: i32, optname: i32, @@ -476,7 +483,8 @@ unsafe extern "C" fn __sys_setsockopt( } } -unsafe extern "C" fn __sys_getsockopt( +#[hermit_macro::system] +pub unsafe extern "C" fn sys_getsockopt( fd: i32, level: i32, optname: i32, @@ -519,7 +527,8 @@ unsafe extern "C" fn __sys_getsockopt( } } -unsafe extern "C" fn __sys_getpeername( +#[hermit_macro::system] +pub unsafe extern "C" fn sys_getpeername( fd: i32, addr: *mut sockaddr, addrlen: *mut socklen_t, @@ -562,9 +571,11 @@ unsafe extern "C" fn __sys_getpeername( ) } -unsafe extern "C" fn __sys_freeaddrinfo(_ai: *mut addrinfo) {} +#[hermit_macro::system] +pub unsafe extern "C" fn sys_freeaddrinfo(_ai: *mut addrinfo) {} -unsafe extern "C" fn __sys_getaddrinfo( +#[hermit_macro::system] +pub unsafe extern "C" fn sys_getaddrinfo( _nodename: *const u8, _servname: *const u8, _hints: *const addrinfo, @@ -573,11 +584,13 @@ unsafe extern "C" fn __sys_getaddrinfo( -EINVAL } -unsafe extern "C" fn __sys_send(s: i32, mem: *const c_void, len: usize, _flags: i32) -> isize { +#[hermit_macro::system] +pub unsafe extern "C" fn sys_send(s: i32, mem: *const c_void, len: usize, _flags: i32) -> isize { unsafe { super::write(s, mem.cast(), len) } } -extern "C" fn __sys_shutdown_socket(fd: i32, how: i32) -> i32 { +#[hermit_macro::system] +pub extern "C" fn sys_shutdown_socket(fd: i32, how: i32) -> i32 { let obj = get_object(fd); obj.map_or_else( |e| -num::ToPrimitive::to_i32(&e).unwrap(), @@ -588,7 +601,8 @@ extern "C" fn __sys_shutdown_socket(fd: i32, how: i32) -> i32 { ) } -unsafe extern "C" fn __sys_recv(fd: i32, buf: *mut u8, len: usize, flags: i32) -> isize { +#[hermit_macro::system] +pub unsafe 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( @@ -600,7 +614,8 @@ unsafe extern "C" fn __sys_recv(fd: i32, buf: *mut u8, len: usize, flags: i32) - } } -unsafe extern "C" fn __sys_sendto( +#[hermit_macro::system] +pub unsafe extern "C" fn sys_sendto( fd: i32, buf: *const u8, len: usize, @@ -629,7 +644,8 @@ unsafe extern "C" fn __sys_sendto( ) } -unsafe extern "C" fn __sys_recvfrom( +#[hermit_macro::system] +pub unsafe extern "C" fn sys_recvfrom( fd: i32, buf: *mut u8, len: usize, @@ -676,122 +692,3 @@ unsafe extern "C" fn __sys_recvfrom( }, ) } - -#[no_mangle] -pub extern "C" fn sys_socket(domain: i32, type_: SockType, protocol: i32) -> i32 { - kernel_function!(__sys_socket(domain, type_, protocol)) -} - -#[no_mangle] -pub unsafe extern "C" fn sys_accept(s: i32, addr: *mut sockaddr, addrlen: *mut socklen_t) -> i32 { - unsafe { kernel_function!(__sys_accept(s, addr, addrlen)) } -} - -#[no_mangle] -pub extern "C" fn sys_listen(s: i32, backlog: i32) -> i32 { - kernel_function!(__sys_listen(s, backlog)) -} - -#[no_mangle] -pub unsafe extern "C" fn sys_bind(s: i32, name: *const sockaddr, namelen: socklen_t) -> i32 { - unsafe { kernel_function!(__sys_bind(s, name, namelen)) } -} - -#[no_mangle] -pub unsafe extern "C" fn sys_connect(s: i32, name: *const sockaddr, namelen: socklen_t) -> i32 { - unsafe { kernel_function!(__sys_connect(s, name, namelen)) } -} - -#[no_mangle] -pub unsafe extern "C" fn sys_getsockname( - s: i32, - name: *mut sockaddr, - namelen: *mut socklen_t, -) -> i32 { - unsafe { kernel_function!(__sys_getsockname(s, name, namelen)) } -} - -#[no_mangle] -pub unsafe extern "C" fn sys_setsockopt( - s: i32, - level: i32, - optname: i32, - optval: *const c_void, - optlen: socklen_t, -) -> i32 { - unsafe { kernel_function!(__sys_setsockopt(s, level, optname, optval, optlen)) } -} - -#[no_mangle] -pub unsafe extern "C" fn sys_getsockopt( - s: i32, - level: i32, - optname: i32, - optval: *mut c_void, - optlen: *mut socklen_t, -) -> i32 { - unsafe { kernel_function!(__sys_getsockopt(s, level, optname, optval, optlen)) } -} - -#[no_mangle] -pub unsafe extern "C" fn sys_getpeername( - s: i32, - name: *mut sockaddr, - namelen: *mut socklen_t, -) -> i32 { - unsafe { kernel_function!(__sys_getpeername(s, name, namelen)) } -} - -#[no_mangle] -pub unsafe extern "C" fn sys_freeaddrinfo(ai: *mut addrinfo) { - unsafe { kernel_function!(__sys_freeaddrinfo(ai)) } -} - -#[no_mangle] -pub unsafe extern "C" fn sys_getaddrinfo( - nodename: *const u8, - servname: *const u8, - hints: *const addrinfo, - res: *mut *mut addrinfo, -) -> i32 { - unsafe { kernel_function!(__sys_getaddrinfo(nodename, servname, hints, res)) } -} - -#[no_mangle] -pub unsafe extern "C" fn sys_send(s: i32, mem: *const c_void, len: usize, flags: i32) -> isize { - unsafe { kernel_function!(__sys_send(s, mem, len, flags)) } -} - -#[no_mangle] -pub extern "C" fn sys_shutdown_socket(s: i32, how: i32) -> i32 { - kernel_function!(__sys_shutdown_socket(s, how)) -} - -#[no_mangle] -pub unsafe extern "C" fn sys_recv(fd: i32, buf: *mut u8, len: usize, flags: i32) -> isize { - unsafe { kernel_function!(__sys_recv(fd, buf, len, flags)) } -} - -#[no_mangle] -pub unsafe extern "C" fn sys_sendto( - socket: i32, - buf: *const u8, - len: usize, - flags: i32, - addr: *const sockaddr, - addrlen: socklen_t, -) -> isize { - unsafe { kernel_function!(__sys_sendto(socket, buf, len, flags, addr, addrlen)) } -} - -#[no_mangle] -pub unsafe extern "C" fn sys_recvfrom( - socket: i32, - buf: *mut u8, - len: usize, - flags: i32, - addr: *mut sockaddr, - addrlen: *mut socklen_t, -) -> isize { - unsafe { kernel_function!(__sys_recvfrom(socket, buf, len, flags, addr, addrlen)) } -} diff --git a/src/syscalls/spinlock.rs b/src/syscalls/spinlock.rs index 515bc8bc6e..b398664bb0 100644 --- a/src/syscalls/spinlock.rs +++ b/src/syscalls/spinlock.rs @@ -14,7 +14,8 @@ pub struct SpinlockIrqSaveContainer<'a> { guard: Option>, } -unsafe extern "C" fn __sys_spinlock_init(lock: *mut *mut SpinlockContainer<'_>) -> i32 { +#[hermit_macro::system] +pub unsafe extern "C" fn sys_spinlock_init(lock: *mut *mut SpinlockContainer<'_>) -> i32 { if lock.is_null() { return -EINVAL; } @@ -29,12 +30,8 @@ unsafe extern "C" fn __sys_spinlock_init(lock: *mut *mut SpinlockContainer<'_>) 0 } -#[no_mangle] -pub unsafe extern "C" fn sys_spinlock_init(lock: *mut *mut SpinlockContainer<'_>) -> i32 { - unsafe { kernel_function!(__sys_spinlock_init(lock)) } -} - -unsafe extern "C" fn __sys_spinlock_destroy(lock: *mut SpinlockContainer<'_>) -> i32 { +#[hermit_macro::system] +pub unsafe extern "C" fn sys_spinlock_destroy(lock: *mut SpinlockContainer<'_>) -> i32 { if lock.is_null() { return -EINVAL; } @@ -46,12 +43,8 @@ unsafe extern "C" fn __sys_spinlock_destroy(lock: *mut SpinlockContainer<'_>) -> 0 } -#[no_mangle] -pub unsafe extern "C" fn sys_spinlock_destroy(lock: *mut SpinlockContainer<'_>) -> i32 { - unsafe { kernel_function!(__sys_spinlock_destroy(lock)) } -} - -unsafe extern "C" fn __sys_spinlock_lock(lock: *mut SpinlockContainer<'_>) -> i32 { +#[hermit_macro::system] +pub unsafe extern "C" fn sys_spinlock_lock(lock: *mut SpinlockContainer<'_>) -> i32 { if lock.is_null() { return -EINVAL; } @@ -65,12 +58,8 @@ unsafe extern "C" fn __sys_spinlock_lock(lock: *mut SpinlockContainer<'_>) -> i3 0 } -#[no_mangle] -pub unsafe extern "C" fn sys_spinlock_lock(lock: *mut SpinlockContainer<'_>) -> i32 { - unsafe { kernel_function!(__sys_spinlock_lock(lock)) } -} - -unsafe extern "C" fn __sys_spinlock_unlock(lock: *mut SpinlockContainer<'_>) -> i32 { +#[hermit_macro::system] +pub unsafe extern "C" fn sys_spinlock_unlock(lock: *mut SpinlockContainer<'_>) -> i32 { if lock.is_null() { return -EINVAL; } @@ -84,12 +73,8 @@ unsafe extern "C" fn __sys_spinlock_unlock(lock: *mut SpinlockContainer<'_>) -> 0 } -#[no_mangle] -pub unsafe extern "C" fn sys_spinlock_unlock(lock: *mut SpinlockContainer<'_>) -> i32 { - unsafe { kernel_function!(__sys_spinlock_unlock(lock)) } -} - -unsafe extern "C" fn __sys_spinlock_irqsave_init( +#[hermit_macro::system] +pub unsafe extern "C" fn sys_spinlock_irqsave_init( lock: *mut *mut SpinlockIrqSaveContainer<'_>, ) -> i32 { if lock.is_null() { @@ -106,14 +91,8 @@ unsafe extern "C" fn __sys_spinlock_irqsave_init( 0 } -#[no_mangle] -pub unsafe extern "C" fn sys_spinlock_irqsave_init( - lock: *mut *mut SpinlockIrqSaveContainer<'_>, -) -> i32 { - unsafe { kernel_function!(__sys_spinlock_irqsave_init(lock)) } -} - -unsafe extern "C" fn __sys_spinlock_irqsave_destroy( +#[hermit_macro::system] +pub unsafe extern "C" fn sys_spinlock_irqsave_destroy( lock: *mut SpinlockIrqSaveContainer<'_>, ) -> i32 { if lock.is_null() { @@ -127,14 +106,8 @@ unsafe extern "C" fn __sys_spinlock_irqsave_destroy( 0 } -#[no_mangle] -pub unsafe extern "C" fn sys_spinlock_irqsave_destroy( - lock: *mut SpinlockIrqSaveContainer<'_>, -) -> i32 { - unsafe { kernel_function!(__sys_spinlock_irqsave_destroy(lock)) } -} - -unsafe extern "C" fn __sys_spinlock_irqsave_lock(lock: *mut SpinlockIrqSaveContainer<'_>) -> i32 { +#[hermit_macro::system] +pub unsafe extern "C" fn sys_spinlock_irqsave_lock(lock: *mut SpinlockIrqSaveContainer<'_>) -> i32 { if lock.is_null() { return -EINVAL; } @@ -148,12 +121,10 @@ unsafe extern "C" fn __sys_spinlock_irqsave_lock(lock: *mut SpinlockIrqSaveConta 0 } -#[no_mangle] -pub unsafe extern "C" fn sys_spinlock_irqsave_lock(lock: *mut SpinlockIrqSaveContainer<'_>) -> i32 { - unsafe { kernel_function!(__sys_spinlock_irqsave_lock(lock)) } -} - -unsafe extern "C" fn __sys_spinlock_irqsave_unlock(lock: *mut SpinlockIrqSaveContainer<'_>) -> i32 { +#[hermit_macro::system] +pub unsafe extern "C" fn sys_spinlock_irqsave_unlock( + lock: *mut SpinlockIrqSaveContainer<'_>, +) -> i32 { if lock.is_null() { return -EINVAL; } @@ -166,10 +137,3 @@ unsafe extern "C" fn __sys_spinlock_irqsave_unlock(lock: *mut SpinlockIrqSaveCon container.guard = None; 0 } - -#[no_mangle] -pub unsafe extern "C" fn sys_spinlock_irqsave_unlock( - lock: *mut SpinlockIrqSaveContainer<'_>, -) -> i32 { - unsafe { kernel_function!(__sys_spinlock_irqsave_unlock(lock)) } -} diff --git a/src/syscalls/system.rs b/src/syscalls/system.rs index 85ecebc6ce..36fee902a4 100644 --- a/src/syscalls/system.rs +++ b/src/syscalls/system.rs @@ -1,8 +1,4 @@ -extern "C" fn __sys_getpagesize() -> i32 { - crate::arch::mm::paging::get_application_page_size() as i32 -} - -#[no_mangle] +#[hermit_macro::system] pub extern "C" fn sys_getpagesize() -> i32 { - kernel_function!(__sys_getpagesize()) + crate::arch::mm::paging::get_application_page_size() as i32 } diff --git a/src/syscalls/tasks.rs b/src/syscalls/tasks.rs index d02be3038c..20a859dad7 100644 --- a/src/syscalls/tasks.rs +++ b/src/syscalls/tasks.rs @@ -20,17 +20,14 @@ use crate::{arch, scheduler}; pub type SignalHandler = extern "C" fn(i32); pub type Tid = u32; -extern "C" fn __sys_getpid() -> Tid { - core_scheduler().get_current_task_id().into() -} - -#[no_mangle] +#[hermit_macro::system] pub extern "C" fn sys_getpid() -> Tid { - kernel_function!(__sys_getpid()) + core_scheduler().get_current_task_id().into() } #[cfg(feature = "newlib")] -extern "C" fn __sys_getprio(id: *const Tid) -> i32 { +#[hermit_macro::system] +pub extern "C" fn sys_getprio(id: *const Tid) -> i32 { let task = core_scheduler().get_current_task_handle(); if id.is_null() || unsafe { *id } == task.get_id().into() { @@ -41,13 +38,7 @@ extern "C" fn __sys_getprio(id: *const Tid) -> i32 { } #[cfg(feature = "newlib")] -#[no_mangle] -pub extern "C" fn sys_getprio(id: *const Tid) -> i32 { - kernel_function!(__sys_getprio(id)) -} - -#[cfg(feature = "newlib")] -#[no_mangle] +#[hermit_macro::system] pub extern "C" fn sys_setprio(_id: *const Tid, _prio: i32) -> i32 { -ENOSYS } @@ -57,32 +48,20 @@ fn exit(arg: i32) -> ! { super::shutdown(arg) } -extern "C" fn __sys_exit(arg: i32) -> ! { - exit(arg) -} - -#[no_mangle] +#[hermit_macro::system] pub extern "C" fn sys_exit(arg: i32) -> ! { - kernel_function!(__sys_exit(arg)) + exit(arg) } -extern "C" fn __sys_thread_exit(arg: i32) -> ! { +#[hermit_macro::system] +pub extern "C" fn sys_thread_exit(arg: i32) -> ! { debug!("Exit thread with error code {}!", arg); core_scheduler().exit(arg) } -#[no_mangle] -pub extern "C" fn sys_thread_exit(arg: i32) -> ! { - kernel_function!(__sys_thread_exit(arg)) -} - -extern "C" fn __sys_abort() -> ! { - exit(-1) -} - -#[no_mangle] +#[hermit_macro::system] pub extern "C" fn sys_abort() -> ! { - kernel_function!(__sys_abort()) + exit(-1) } #[cfg(feature = "newlib")] @@ -94,7 +73,8 @@ pub fn sbrk_init() { } #[cfg(feature = "newlib")] -extern "C" fn __sys_sbrk(incr: isize) -> usize { +#[hermit_macro::system] +pub extern "C" fn sys_sbrk(incr: isize) -> usize { // Get the boundaries of the task heap and verify that they are suitable for sbrk. let task_heap_start = task_heap_start(); let task_heap_end = task_heap_end(); @@ -111,12 +91,6 @@ extern "C" fn __sys_sbrk(incr: isize) -> usize { old_end } -#[cfg(feature = "newlib")] -#[no_mangle] -pub extern "C" fn sys_sbrk(incr: isize) -> usize { - kernel_function!(__sys_sbrk(incr)) -} - pub(super) fn usleep(usecs: u64) { if usecs >= 10_000 { // Enough time to set a wakeup timer and block the current task. @@ -136,25 +110,18 @@ pub(super) fn usleep(usecs: u64) { } } -pub(crate) extern "C" fn __sys_usleep(usecs: u64) { - usleep(usecs) -} - -#[no_mangle] +#[hermit_macro::system] 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) + usleep(usecs) } -#[no_mangle] +#[hermit_macro::system] pub extern "C" fn sys_msleep(ms: u32) { - kernel_function!(__sys_msleep(ms)) + usleep(u64::from(ms) * 1000) } -unsafe extern "C" fn __sys_nanosleep(rqtp: *const timespec, _rmtp: *mut timespec) -> i32 { +#[hermit_macro::system] +pub unsafe extern "C" fn sys_nanosleep(rqtp: *const timespec, _rmtp: *mut timespec) -> i32 { assert!( !rqtp.is_null(), "sys_nanosleep called with a zero rqtp parameter" @@ -175,13 +142,10 @@ unsafe extern "C" fn __sys_nanosleep(rqtp: *const timespec, _rmtp: *mut timespec 0 } -#[no_mangle] -pub unsafe extern "C" fn sys_nanosleep(rqtp: *const timespec, rmtp: *mut timespec) -> i32 { - unsafe { kernel_function!(__sys_nanosleep(rqtp, rmtp)) } -} - +/// Creates a new thread based on the configuration of the current thread. #[cfg(feature = "newlib")] -extern "C" fn __sys_clone(id: *mut Tid, func: extern "C" fn(usize), arg: usize) -> i32 { +#[hermit_macro::system] +pub extern "C" fn sys_clone(id: *mut Tid, func: extern "C" fn(usize), arg: usize) -> i32 { let task_id = core_scheduler().clone(func, arg); if !id.is_null() { @@ -193,24 +157,14 @@ extern "C" fn __sys_clone(id: *mut Tid, func: extern "C" fn(usize), arg: usize) 0 } -/// Creates a new thread based on the configuration of the current thread. -#[cfg(feature = "newlib")] -#[no_mangle] -pub extern "C" fn sys_clone(id: *mut Tid, func: extern "C" fn(usize), arg: usize) -> i32 { - kernel_function!(__sys_clone(id, func, arg)) -} - -extern "C" fn __sys_yield() { - core_scheduler().reschedule(); -} - -#[no_mangle] +#[hermit_macro::system] pub extern "C" fn sys_yield() { - kernel_function!(__sys_yield()) + core_scheduler().reschedule(); } #[cfg(feature = "newlib")] -extern "C" fn __sys_kill(dest: Tid, signum: i32) -> i32 { +#[hermit_macro::system] +pub extern "C" fn sys_kill(dest: Tid, signum: i32) -> i32 { debug!( "sys_kill is unimplemented, returning -ENOSYS for killing {} with signal {}", dest, signum @@ -219,34 +173,13 @@ extern "C" fn __sys_kill(dest: Tid, signum: i32) -> i32 { } #[cfg(feature = "newlib")] -#[no_mangle] -pub extern "C" fn sys_kill(dest: Tid, signum: i32) -> i32 { - kernel_function!(__sys_kill(dest, signum)) -} - -#[cfg(feature = "newlib")] -extern "C" fn __sys_signal(_handler: SignalHandler) -> i32 { +#[hermit_macro::system] +pub extern "C" fn sys_signal(_handler: SignalHandler) -> i32 { debug!("sys_signal is unimplemented"); 0 } -#[cfg(feature = "newlib")] -#[no_mangle] -pub extern "C" fn sys_signal(handler: SignalHandler) -> i32 { - kernel_function!(__sys_signal(handler)) -} - -unsafe extern "C" fn __sys_spawn2( - func: unsafe extern "C" fn(usize), - arg: usize, - prio: u8, - stack_size: usize, - selector: isize, -) -> Tid { - unsafe { scheduler::spawn(func, arg, Priority::from(prio), stack_size, selector).into() } -} - -#[no_mangle] +#[hermit_macro::system] pub unsafe extern "C" fn sys_spawn2( func: unsafe extern "C" fn(usize), arg: usize, @@ -254,10 +187,11 @@ pub unsafe extern "C" fn sys_spawn2( stack_size: usize, selector: isize, ) -> Tid { - unsafe { kernel_function!(__sys_spawn2(func, arg, prio, stack_size, selector)) } + unsafe { scheduler::spawn(func, arg, Priority::from(prio), stack_size, selector).into() } } -unsafe extern "C" fn __sys_spawn( +#[hermit_macro::system] +pub unsafe extern "C" fn sys_spawn( id: *mut Tid, func: unsafe extern "C" fn(usize), arg: usize, @@ -277,29 +211,14 @@ unsafe extern "C" fn __sys_spawn( 0 } -#[no_mangle] -pub unsafe extern "C" fn sys_spawn( - id: *mut Tid, - func: unsafe extern "C" fn(usize), - arg: usize, - prio: u8, - selector: isize, -) -> i32 { - unsafe { kernel_function!(__sys_spawn(id, func, arg, prio, selector)) } -} - -extern "C" fn __sys_join(id: Tid) -> i32 { +#[hermit_macro::system] +pub extern "C" fn sys_join(id: Tid) -> i32 { match scheduler::join(TaskId::from(id)) { Ok(()) => 0, _ => -EINVAL, } } -#[no_mangle] -pub extern "C" fn sys_join(id: Tid) -> i32 { - kernel_function!(__sys_join(id)) -} - /// Mapping between blocked tasks and their TaskHandle static BLOCKED_TASKS: InterruptTicketMutex> = InterruptTicketMutex::new(BTreeMap::new()); @@ -314,27 +233,21 @@ fn 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] +#[hermit_macro::system] pub extern "C" fn sys_block_current_task() { - kernel_function!(__sys_block_current_task()) -} - -extern "C" fn __sys_block_current_task_with_timeout(timeout: u64) { - block_current_task(&Some(timeout)) + block_current_task(&None) } /// Set the current task state to `blocked` -#[no_mangle] +#[hermit_macro::system] pub extern "C" fn sys_block_current_task_with_timeout(timeout: u64) { - kernel_function!(__sys_block_current_task_with_timeout(timeout)) + block_current_task(&Some(timeout)) } -extern "C" fn __sys_wakeup_task(id: Tid) { +/// Wake up the task with the identifier `id` +#[hermit_macro::system] +pub extern "C" fn sys_wakeup_task(id: Tid) { let task_id = TaskId::from(id); if let Some(handle) = BLOCKED_TASKS.lock().remove(&task_id) { @@ -342,23 +255,15 @@ extern "C" fn __sys_wakeup_task(id: Tid) { } } -/// Wake up the task with the identifier `id` -#[no_mangle] -pub extern "C" fn sys_wakeup_task(id: Tid) { - kernel_function!(__sys_wakeup_task(id)) -} - -extern "C" fn __sys_get_priority() -> u8 { - core_scheduler().get_current_task_prio().into() -} - /// Determine the priority of the current thread -#[no_mangle] +#[hermit_macro::system] pub extern "C" fn sys_get_priority() -> u8 { - kernel_function!(__sys_get_priority()) + core_scheduler().get_current_task_prio().into() } -extern "C" fn __sys_set_priority(id: Tid, prio: u8) { +/// Set priority of the thread with the identifier `id` +#[hermit_macro::system] +pub extern "C" fn sys_set_priority(id: Tid, prio: u8) { if prio > 0 { core_scheduler() .set_priority(TaskId::from(id), Priority::from(prio)) @@ -368,22 +273,12 @@ extern "C" fn __sys_set_priority(id: Tid, prio: u8) { } } -/// Set priority of the thread with the identifier `id` -#[no_mangle] -pub extern "C" fn sys_set_priority(id: Tid, prio: u8) { - kernel_function!(__sys_set_priority(id, prio)) -} - -extern "C" fn __sys_set_current_task_priority(prio: u8) { +/// Set priority of the current thread +#[hermit_macro::system] +pub extern "C" fn sys_set_current_task_priority(prio: u8) { if prio > 0 { core_scheduler().set_current_task_priority(Priority::from(prio)); } else { panic!("Invalid priority {}", prio); } } - -/// Set priority of the current thread -#[no_mangle] -pub extern "C" fn sys_set_current_task_priority(prio: u8) { - kernel_function!(__sys_set_current_task_priority(prio)) -} diff --git a/src/syscalls/timer.rs b/src/syscalls/timer.rs index 7bee508e92..56603665b7 100644 --- a/src/syscalls/timer.rs +++ b/src/syscalls/timer.rs @@ -19,7 +19,8 @@ pub(crate) const TIMER_ABSTIME: i32 = 4; /// - `CLOCK_PROCESS_CPUTIME_ID` /// - `CLOCK_THREAD_CPUTIME_ID` /// - `CLOCK_MONOTONIC` -unsafe extern "C" fn __sys_clock_getres(clock_id: u64, res: *mut timespec) -> i32 { +#[hermit_macro::system] +pub unsafe extern "C" fn sys_clock_getres(clock_id: u64, res: *mut timespec) -> i32 { assert!( !res.is_null(), "sys_clock_getres called with a zero res parameter" @@ -39,11 +40,6 @@ unsafe extern "C" fn __sys_clock_getres(clock_id: u64, res: *mut timespec) -> i3 } } -#[no_mangle] -pub unsafe extern "C" fn sys_clock_getres(clock_id: u64, res: *mut timespec) -> i32 { - unsafe { kernel_function!(__sys_clock_getres(clock_id, res)) } -} - /// Get the current time of a clock. /// /// Get the current time of the clock with `clock_id` and stores result in parameter `res`. @@ -52,7 +48,8 @@ pub unsafe extern "C" fn sys_clock_getres(clock_id: u64, res: *mut timespec) -> /// Supported clocks: /// - `CLOCK_REALTIME` /// - `CLOCK_MONOTONIC` -unsafe extern "C" fn __sys_clock_gettime(clock_id: u64, tp: *mut timespec) -> i32 { +#[hermit_macro::system] +pub unsafe extern "C" fn sys_clock_gettime(clock_id: u64, tp: *mut timespec) -> i32 { assert!( !tp.is_null(), "sys_clock_gettime called with a zero tp parameter" @@ -78,11 +75,6 @@ unsafe extern "C" fn __sys_clock_gettime(clock_id: u64, tp: *mut timespec) -> i3 } } -#[no_mangle] -pub unsafe extern "C" fn sys_clock_gettime(clock_id: u64, tp: *mut timespec) -> i32 { - unsafe { kernel_function!(__sys_clock_gettime(clock_id, tp)) } -} - /// Sleep a clock for a specified number of nanoseconds. /// /// The requested time (in nanoseconds) must be greater than 0 and less than 1,000,000. @@ -92,7 +84,8 @@ pub unsafe extern "C" fn sys_clock_gettime(clock_id: u64, tp: *mut timespec) -> /// Supported clocks: /// - `CLOCK_REALTIME` /// - `CLOCK_MONOTONIC` -unsafe extern "C" fn __sys_clock_nanosleep( +#[hermit_macro::system] +pub unsafe extern "C" fn sys_clock_nanosleep( clock_id: u64, flags: i32, rqtp: *const timespec, @@ -131,34 +124,21 @@ unsafe extern "C" fn __sys_clock_nanosleep( } } -#[no_mangle] -pub unsafe extern "C" fn sys_clock_nanosleep( - clock_id: u64, - flags: i32, - rqtp: *const timespec, - rmtp: *mut timespec, -) -> i32 { - unsafe { kernel_function!(__sys_clock_nanosleep(clock_id, flags, rqtp, rmtp)) } -} - -unsafe extern "C" fn __sys_clock_settime(_clock_id: u64, _tp: *const timespec) -> i32 { +#[hermit_macro::system] +pub unsafe extern "C" fn sys_clock_settime(_clock_id: u64, _tp: *const timespec) -> i32 { // We don't support setting any clocks yet. debug!("sys_clock_settime is unimplemented, returning -EINVAL"); -EINVAL } -#[no_mangle] -pub unsafe extern "C" fn sys_clock_settime(clock_id: u64, tp: *const timespec) -> i32 { - unsafe { kernel_function!(__sys_clock_settime(clock_id, tp)) } -} - /// Get the system's clock time. /// /// This function gets the current time based on the wallclock time when booted up, plus current timer ticks. /// Returns `0` on success, `-EINVAL` otherwise. /// /// **Parameter `tz` should be set to `0` since tz is obsolete.** -unsafe extern "C" fn __sys_gettimeofday(tp: *mut timeval, tz: usize) -> i32 { +#[hermit_macro::system] +pub unsafe extern "C" fn sys_gettimeofday(tp: *mut timeval, tz: usize) -> i32 { if let Some(result) = unsafe { tp.as_mut() } { // Return the current time based on the wallclock time when we were booted up // plus the current timer ticks. @@ -174,13 +154,8 @@ unsafe extern "C" fn __sys_gettimeofday(tp: *mut timeval, tz: usize) -> i32 { 0 } -#[no_mangle] -pub unsafe extern "C" fn sys_gettimeofday(tp: *mut timeval, tz: usize) -> i32 { - unsafe { kernel_function!(__sys_gettimeofday(tp, tz)) } -} - -#[no_mangle] -unsafe extern "C" fn __sys_setitimer( +#[hermit_macro::system] +pub unsafe extern "C" fn sys_setitimer( _which: i32, _value: *const itimerval, _ovalue: *mut itimerval, @@ -188,12 +163,3 @@ unsafe extern "C" fn __sys_setitimer( debug!("Called sys_setitimer, which is unimplemented and always returns 0"); 0 } - -#[no_mangle] -pub unsafe extern "C" fn sys_setitimer( - which: i32, - value: *const itimerval, - ovalue: *mut itimerval, -) -> i32 { - unsafe { kernel_function!(__sys_setitimer(which, value, ovalue)) } -}