diff --git a/src/arch/aarch64/kernel/scheduler.rs b/src/arch/aarch64/kernel/scheduler.rs index 96e4d1f8aa..eb850c1800 100644 --- a/src/arch/aarch64/kernel/scheduler.rs +++ b/src/arch/aarch64/kernel/scheduler.rs @@ -338,7 +338,7 @@ extern "C" fn task_start(_f: extern "C" fn(usize), _arg: usize) -> ! { } impl TaskFrame for Task { - fn create_stack_frame(&mut self, func: extern "C" fn(usize), arg: usize) { + fn create_stack_frame(&mut self, func: unsafe extern "C" fn(usize), arg: usize) { // Check if TLS is allocated already and if the task uses thread-local storage. if self.tls.is_none() { self.tls = TaskTLS::from_environment(); diff --git a/src/arch/aarch64/kernel/switch.rs b/src/arch/aarch64/kernel/switch.rs index 9c3102e111..66c80573ae 100644 --- a/src/arch/aarch64/kernel/switch.rs +++ b/src/arch/aarch64/kernel/switch.rs @@ -5,7 +5,7 @@ macro_rules! kernel_function_impl { ($kernel_function:ident($($arg:ident: $A:ident),*) { $($operands:tt)* }) => { /// Executes `f` on the kernel stack. #[allow(dead_code)] - pub fn $kernel_function(f: unsafe extern "C" fn($($A),*) -> R, $($arg: $A),*) -> R { + pub unsafe fn $kernel_function(f: unsafe extern "C" fn($($A),*) -> R, $($arg: $A),*) -> R { unsafe { assert!(mem::size_of::() <= mem::size_of::()); diff --git a/src/arch/riscv64/kernel/scheduler.rs b/src/arch/riscv64/kernel/scheduler.rs index 611b9466fd..64bd1a5a94 100644 --- a/src/arch/riscv64/kernel/scheduler.rs +++ b/src/arch/riscv64/kernel/scheduler.rs @@ -379,7 +379,7 @@ extern "C" fn task_entry(func: extern "C" fn(usize), arg: usize) { } impl TaskFrame for Task { - fn create_stack_frame(&mut self, func: extern "C" fn(usize), arg: usize) { + fn create_stack_frame(&mut self, func: unsafe extern "C" fn(usize), arg: usize) { // Check if the task (process or thread) uses Thread-Local-Storage. // check is TLS is already allocated if self.tls.is_none() { diff --git a/src/arch/x86_64/kernel/scheduler.rs b/src/arch/x86_64/kernel/scheduler.rs index f6d39ea40d..80b38e006e 100644 --- a/src/arch/x86_64/kernel/scheduler.rs +++ b/src/arch/x86_64/kernel/scheduler.rs @@ -336,7 +336,7 @@ extern "C" fn task_entry(func: extern "C" fn(usize), arg: usize) -> ! { } impl TaskFrame for Task { - fn create_stack_frame(&mut self, func: extern "C" fn(usize), arg: usize) { + fn create_stack_frame(&mut self, func: unsafe extern "C" fn(usize), arg: usize) { // Check if TLS is allocated already and if the task uses thread-local storage. #[cfg(not(feature = "common-os"))] if self.tls.is_none() { diff --git a/src/arch/x86_64/kernel/switch.rs b/src/arch/x86_64/kernel/switch.rs index 49368aeae2..a970845cf8 100644 --- a/src/arch/x86_64/kernel/switch.rs +++ b/src/arch/x86_64/kernel/switch.rs @@ -220,7 +220,7 @@ macro_rules! kernel_function_impl { ($kernel_function:ident($($arg:ident: $A:ident),*) { $($operands:tt)* }) => { /// Executes `f` on the kernel stack. #[allow(dead_code)] - pub fn $kernel_function(f: unsafe extern "C" fn($($A),*) -> R, $($arg: $A),*) -> R { + pub unsafe fn $kernel_function(f: unsafe extern "C" fn($($A),*) -> R, $($arg: $A),*) -> R { unsafe { assert!(mem::size_of::() <= mem::size_of::()); diff --git a/src/lib.rs b/src/lib.rs index 54aef2c46b..0e8992a1ac 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -239,7 +239,15 @@ fn boot_processor_main() -> ! { } // Start the initd task. - scheduler::PerCoreScheduler::spawn(initd, 0, scheduler::task::NORMAL_PRIO, 0, USER_STACK_SIZE); + unsafe { + scheduler::PerCoreScheduler::spawn( + initd, + 0, + scheduler::task::NORMAL_PRIO, + 0, + USER_STACK_SIZE, + ) + }; // Run the scheduler loop. PerCoreScheduler::run(); diff --git a/src/macros.rs b/src/macros.rs index 221b9475ff..b63426156d 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -80,45 +80,109 @@ macro_rules! dbg { macro_rules! kernel_function { ($f:ident()) => {{ use $crate::errno::ToErrno; + + // This propagates any unsafety requirements of `f` to the caller. + if false { + $f(); + } + #[allow(unreachable_code)] - $crate::arch::switch::kernel_function0($f).set_errno() + #[allow(unused_unsafe)] + unsafe { + $crate::arch::switch::kernel_function0($f).set_errno() + } }}; ($f:ident($arg1:expr)) => {{ use $crate::errno::ToErrno; + + // This propagates any unsafety requirements of `f` to the caller. + if false { + $f($arg1); + } + #[allow(unreachable_code)] - $crate::arch::switch::kernel_function1($f, $arg1).set_errno() + #[allow(unused_unsafe)] + unsafe { + $crate::arch::switch::kernel_function1($f, $arg1).set_errno() + } }}; ($f:ident($arg1:expr, $arg2:expr)) => {{ use $crate::errno::ToErrno; + + // This propagates any unsafety requirements of `f` to the caller. + if false { + $f($arg1, $arg2); + } + #[allow(unreachable_code)] - $crate::arch::switch::kernel_function2($f, $arg1, $arg2).set_errno() + #[allow(unused_unsafe)] + unsafe { + $crate::arch::switch::kernel_function2($f, $arg1, $arg2).set_errno() + } }}; ($f:ident($arg1:expr, $arg2:expr, $arg3:expr)) => {{ use $crate::errno::ToErrno; + + // This propagates any unsafety requirements of `f` to the caller. + if false { + $f($arg1, $arg2, $arg3); + } + #[allow(unreachable_code)] - $crate::arch::switch::kernel_function3($f, $arg1, $arg2, $arg3).set_errno() + #[allow(unused_unsafe)] + unsafe { + $crate::arch::switch::kernel_function3($f, $arg1, $arg2, $arg3).set_errno() + } }}; ($f:ident($arg1:expr, $arg2:expr, $arg3:expr, $arg4:expr)) => {{ use $crate::errno::ToErrno; + + // This propagates any unsafety requirements of `f` to the caller. + if false { + $f($arg1, $arg2, $arg3, $arg4); + } + #[allow(unreachable_code)] - $crate::arch::switch::kernel_function4($f, $arg1, $arg2, $arg3, $arg4).set_errno() + #[allow(unused_unsafe)] + unsafe { + $crate::arch::switch::kernel_function4($f, $arg1, $arg2, $arg3, $arg4).set_errno() + } }}; ($f:ident($arg1:expr, $arg2:expr, $arg3:expr, $arg4:expr, $arg5:expr)) => {{ use $crate::errno::ToErrno; + + // This propagates any unsafety requirements of `f` to the caller. + if false { + $f($arg1, $arg2, $arg3, $arg4, $arg5); + } + #[allow(unreachable_code)] - $crate::arch::switch::kernel_function5($f, $arg1, $arg2, $arg3, $arg4, $arg5).set_errno() + #[allow(unused_unsafe)] + unsafe { + $crate::arch::switch::kernel_function5($f, $arg1, $arg2, $arg3, $arg4, $arg5) + .set_errno() + } }}; ($f:ident($arg1:expr, $arg2:expr, $arg3:expr, $arg4:expr, $arg5:expr, $arg6:expr)) => {{ use $crate::errno::ToErrno; + + // This propagates any unsafety requirements of `f` to the caller. + if false { + $f($arg1, $arg2, $arg3, $arg4, $arg5, $arg6); + } + #[allow(unreachable_code)] - $crate::arch::switch::kernel_function6($f, $arg1, $arg2, $arg3, $arg4, $arg5, $arg6) - .set_errno() + #[allow(unused_unsafe)] + unsafe { + $crate::arch::switch::kernel_function6($f, $arg1, $arg2, $arg3, $arg4, $arg5, $arg6) + .set_errno() + } }}; } diff --git a/src/scheduler/mod.rs b/src/scheduler/mod.rs index 0847db7f2c..fd2c774701 100644 --- a/src/scheduler/mod.rs +++ b/src/scheduler/mod.rs @@ -203,7 +203,7 @@ impl PerCoreSchedulerExt for &mut PerCoreScheduler { struct NewTask { tid: TaskId, - func: extern "C" fn(usize), + func: unsafe extern "C" fn(usize), arg: usize, prio: Priority, core_id: CoreId, @@ -231,8 +231,8 @@ impl From for Task { impl PerCoreScheduler { /// Spawn a new task. - pub fn spawn( - func: extern "C" fn(usize), + pub unsafe fn spawn( + func: unsafe extern "C" fn(usize), arg: usize, prio: Priority, core_id: CoreId, @@ -941,8 +941,8 @@ fn get_scheduler_input(core_id: CoreId) -> &'static InterruptTicketMutex TaskId { diff --git a/src/scheduler/task.rs b/src/scheduler/task.rs index 6b31a97956..d6ff54061c 100644 --- a/src/scheduler/task.rs +++ b/src/scheduler/task.rs @@ -406,7 +406,7 @@ pub(crate) struct Task { pub(crate) trait TaskFrame { /// Create the initial stack frame for a new task - fn create_stack_frame(&mut self, func: extern "C" fn(usize), arg: usize); + fn create_stack_frame(&mut self, func: unsafe extern "C" fn(usize), arg: usize); } impl Task { diff --git a/src/syscalls/condvar.rs b/src/syscalls/condvar.rs index 1ec6f6e994..0474a7e7bd 100644 --- a/src/syscalls/condvar.rs +++ b/src/syscalls/condvar.rs @@ -23,7 +23,7 @@ impl CondQueue { } } -extern "C" fn __sys_destroy_queue(ptr: usize) -> i32 { +unsafe extern "C" fn __sys_destroy_queue(ptr: usize) -> i32 { unsafe { let id = ptr::from_exposed_addr_mut::(ptr); if id.is_null() { @@ -42,10 +42,10 @@ extern "C" fn __sys_destroy_queue(ptr: usize) -> i32 { #[no_mangle] pub unsafe extern "C" fn sys_destroy_queue(ptr: usize) -> i32 { - kernel_function!(__sys_destroy_queue(ptr)) + unsafe { kernel_function!(__sys_destroy_queue(ptr)) } } -extern "C" fn __sys_notify(ptr: usize, count: i32) -> i32 { +unsafe extern "C" fn __sys_notify(ptr: usize, count: i32) -> i32 { unsafe { let id = ptr::from_exposed_addr::(ptr); @@ -83,10 +83,10 @@ extern "C" fn __sys_notify(ptr: usize, count: i32) -> i32 { #[no_mangle] pub unsafe extern "C" fn sys_notify(ptr: usize, count: i32) -> i32 { - kernel_function!(__sys_notify(ptr, count)) + unsafe { kernel_function!(__sys_notify(ptr, count)) } } -extern "C" fn __sys_init_queue(ptr: usize) -> i32 { +unsafe extern "C" fn __sys_init_queue(ptr: usize) -> i32 { unsafe { let id = ptr::from_exposed_addr_mut::(ptr); if id.is_null() { @@ -106,10 +106,10 @@ extern "C" fn __sys_init_queue(ptr: usize) -> i32 { #[no_mangle] pub unsafe extern "C" fn sys_init_queue(ptr: usize) -> i32 { - kernel_function!(__sys_init_queue(ptr)) + unsafe { kernel_function!(__sys_init_queue(ptr)) } } -extern "C" fn __sys_add_queue(ptr: usize, timeout_ns: i64) -> i32 { +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() { @@ -138,10 +138,10 @@ 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 { - kernel_function!(__sys_add_queue(ptr, timeout_ns)) + unsafe { kernel_function!(__sys_add_queue(ptr, timeout_ns)) } } -extern "C" fn __sys_wait(ptr: usize) -> i32 { +unsafe extern "C" fn __sys_wait(ptr: usize) -> i32 { unsafe { let id = ptr::from_exposed_addr_mut::(ptr); if id.is_null() { @@ -164,5 +164,5 @@ extern "C" fn __sys_wait(ptr: usize) -> i32 { #[no_mangle] pub unsafe extern "C" fn sys_wait(ptr: usize) -> i32 { - kernel_function!(__sys_wait(ptr)) + unsafe { kernel_function!(__sys_wait(ptr)) } } diff --git a/src/syscalls/entropy.rs b/src/syscalls/entropy.rs index dc3cbd8726..5d61f9b644 100644 --- a/src/syscalls/entropy.rs +++ b/src/syscalls/entropy.rs @@ -53,11 +53,9 @@ unsafe extern "C" fn __sys_read_entropy(buf: *mut u8, len: usize, flags: u32) -> /// Returns either the number of bytes written to buf (a positive value) or /// * `-EINVAL` if `flags` contains unknown flags. /// * `-ENOSYS` if the system does not support random data generation. -#[allow(unsafe_op_in_unsafe_fn)] #[no_mangle] -#[cfg_attr(target_arch = "riscv64", allow(unsafe_op_in_unsafe_fn))] // FIXME pub unsafe extern "C" fn sys_read_entropy(buf: *mut u8, len: usize, flags: u32) -> isize { - kernel_function!(__sys_read_entropy(buf, len, flags)) + unsafe { kernel_function!(__sys_read_entropy(buf, len, flags)) } } #[cfg(not(feature = "newlib"))] @@ -84,7 +82,7 @@ unsafe extern "C" fn __sys_secure_rand32(value: *mut u32) -> i32 { #[cfg(not(feature = "newlib"))] #[no_mangle] pub unsafe extern "C" fn sys_secure_rand32(value: *mut u32) -> i32 { - kernel_function!(__sys_secure_rand32(value)) + unsafe { kernel_function!(__sys_secure_rand32(value)) } } #[cfg(not(feature = "newlib"))] @@ -111,7 +109,7 @@ unsafe extern "C" fn __sys_secure_rand64(value: *mut u64) -> i32 { #[cfg(not(feature = "newlib"))] #[no_mangle] pub unsafe extern "C" fn sys_secure_rand64(value: *mut u64) -> i32 { - kernel_function!(__sys_secure_rand64(value)) + unsafe { kernel_function!(__sys_secure_rand64(value)) } } extern "C" fn __sys_rand() -> u32 { diff --git a/src/syscalls/futex.rs b/src/syscalls/futex.rs index 649aa46750..fc942d756e 100644 --- a/src/syscalls/futex.rs +++ b/src/syscalls/futex.rs @@ -10,7 +10,7 @@ use crate::time::timespec; /// * `address` is null /// * `timeout` is negative /// * `flags` contains unknown flags -extern "C" fn __sys_futex_wait( +unsafe extern "C" fn __sys_futex_wait( address: *mut u32, expected: u32, timeout: *const timespec, @@ -38,19 +38,19 @@ extern "C" fn __sys_futex_wait( } #[no_mangle] -pub extern "C" fn sys_futex_wait( +pub unsafe extern "C" fn sys_futex_wait( address: *mut u32, expected: u32, timeout: *const timespec, flags: u32, ) -> i32 { - kernel_function!(__sys_futex_wait(address, expected, timeout, flags)) + 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. -extern "C" fn __sys_futex_wake(address: *mut u32, count: i32) -> i32 { +unsafe extern "C" fn __sys_futex_wake(address: *mut u32, count: i32) -> i32 { if address.is_null() { return -EINVAL; } @@ -60,6 +60,6 @@ extern "C" fn __sys_futex_wake(address: *mut u32, count: i32) -> i32 { } #[no_mangle] -pub extern "C" fn sys_futex_wake(address: *mut u32, count: i32) -> i32 { - kernel_function!(__sys_futex_wake(address, count)) +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 b905853d0e..03ce3699af 100644 --- a/src/syscalls/lwip.rs +++ b/src/syscalls/lwip.rs @@ -42,7 +42,7 @@ pub extern "C" fn sys_putchar(character: u8) { kernel_function!(__sys_putchar(character)) } -extern "C" fn __sys_release_putchar_lock() { +unsafe extern "C" fn __sys_release_putchar_lock() { unsafe { console::CONSOLE.force_unlock(); } @@ -50,5 +50,5 @@ extern "C" fn __sys_release_putchar_lock() { #[no_mangle] pub extern "C" fn sys_release_putchar_lock() { - kernel_function!(__sys_release_putchar_lock()) + unsafe { kernel_function!(__sys_release_putchar_lock()) } } diff --git a/src/syscalls/mod.rs b/src/syscalls/mod.rs index 4c052a02ec..07f7fe4f56 100644 --- a/src/syscalls/mod.rs +++ b/src/syscalls/mod.rs @@ -129,7 +129,7 @@ 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) extern "C" fn __sys_realloc( +pub(crate) unsafe extern "C" fn __sys_realloc( ptr: *mut u8, size: usize, align: usize, @@ -165,8 +165,13 @@ pub(crate) extern "C" fn __sys_realloc( #[cfg(all(target_os = "none", not(feature = "common-os")))] #[no_mangle] -pub extern "C" fn sys_realloc(ptr: *mut u8, size: usize, align: usize, new_size: usize) -> *mut u8 { - kernel_function!(__sys_realloc(ptr, size, align, new_size)) +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 @@ -180,7 +185,7 @@ pub extern "C" fn sys_realloc(ptr: *mut u8, size: usize, align: usize, new_size: /// # 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) extern "C" fn __sys_free(ptr: *mut u8, size: usize, align: usize) { +pub(crate) 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 { @@ -204,8 +209,8 @@ pub(crate) extern "C" fn __sys_free(ptr: *mut u8, size: usize, align: usize) { #[cfg(all(target_os = "none", not(feature = "common-os")))] #[no_mangle] -pub extern "C" fn sys_free(ptr: *mut u8, size: usize, align: usize) { - kernel_function!(__sys_free(ptr, size, align)) +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) { @@ -228,7 +233,7 @@ pub extern "C" fn sys_shutdown(arg: i32) -> ! { kernel_function!(__sys_shutdown(arg)) } -extern "C" fn __sys_unlink(name: *const u8) -> i32 { +unsafe extern "C" fn __sys_unlink(name: *const u8) -> i32 { let name = unsafe { CStr::from_ptr(name as _) }.to_str().unwrap(); fs::FILESYSTEM @@ -239,11 +244,11 @@ extern "C" fn __sys_unlink(name: *const u8) -> i32 { } #[no_mangle] -pub extern "C" fn sys_unlink(name: *const u8) -> i32 { - kernel_function!(__sys_unlink(name)) +pub unsafe extern "C" fn sys_unlink(name: *const u8) -> i32 { + unsafe { kernel_function!(__sys_unlink(name)) } } -extern "C" fn __sys_mkdir(name: *const u8, mode: u32) -> i32 { +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 @@ -259,11 +264,11 @@ extern "C" fn __sys_mkdir(name: *const u8, mode: u32) -> i32 { } #[no_mangle] -pub extern "C" fn sys_mkdir(name: *const u8, mode: u32) -> i32 { - kernel_function!(__sys_mkdir(name, mode)) +pub unsafe extern "C" fn sys_mkdir(name: *const u8, mode: u32) -> i32 { + unsafe { kernel_function!(__sys_mkdir(name, mode)) } } -extern "C" fn __sys_rmdir(name: *const u8) -> i32 { +unsafe extern "C" fn __sys_rmdir(name: *const u8) -> i32 { let name = unsafe { CStr::from_ptr(name as _) }.to_str().unwrap(); fs::FILESYSTEM @@ -274,11 +279,11 @@ extern "C" fn __sys_rmdir(name: *const u8) -> i32 { } #[no_mangle] -pub extern "C" fn sys_rmdir(name: *const u8) -> i32 { - kernel_function!(__sys_rmdir(name)) +pub unsafe extern "C" fn sys_rmdir(name: *const u8) -> i32 { + unsafe { kernel_function!(__sys_rmdir(name)) } } -extern "C" fn __sys_stat(name: *const u8, stat: *mut FileAttr) -> i32 { +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) { @@ -291,11 +296,11 @@ extern "C" fn __sys_stat(name: *const u8, stat: *mut FileAttr) -> i32 { } #[no_mangle] -pub extern "C" fn sys_stat(name: *const u8, stat: *mut FileAttr) -> i32 { - kernel_function!(__sys_stat(name, stat)) +pub unsafe extern "C" fn sys_stat(name: *const u8, stat: *mut FileAttr) -> i32 { + unsafe { kernel_function!(__sys_stat(name, stat)) } } -extern "C" fn __sys_lstat(name: *const u8, stat: *mut FileAttr) -> i32 { +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) { @@ -308,11 +313,11 @@ extern "C" fn __sys_lstat(name: *const u8, stat: *mut FileAttr) -> i32 { } #[no_mangle] -pub extern "C" fn sys_lstat(name: *const u8, stat: *mut FileAttr) -> i32 { - kernel_function!(__sys_lstat(name, stat)) +pub unsafe extern "C" fn sys_lstat(name: *const u8, stat: *mut FileAttr) -> i32 { + unsafe { kernel_function!(__sys_lstat(name, stat)) } } -extern "C" fn __sys_fstat(fd: FileDescriptor, stat: *mut FileAttr) -> i32 { +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( @@ -325,11 +330,11 @@ extern "C" fn __sys_fstat(fd: FileDescriptor, stat: *mut FileAttr) -> i32 { } #[no_mangle] -pub extern "C" fn sys_fstat(fd: FileDescriptor, stat: *mut FileAttr) -> i32 { - kernel_function!(__sys_fstat(fd, stat)) +pub unsafe extern "C" fn sys_fstat(fd: FileDescriptor, stat: *mut FileAttr) -> i32 { + unsafe { kernel_function!(__sys_fstat(fd, stat)) } } -extern "C" fn __sys_opendir(name: *const u8) -> FileDescriptor { +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 { @@ -338,11 +343,11 @@ extern "C" fn __sys_opendir(name: *const u8) -> FileDescriptor { } #[no_mangle] -pub extern "C" fn sys_opendir(name: *const u8) -> FileDescriptor { - kernel_function!(__sys_opendir(name)) +pub unsafe extern "C" fn sys_opendir(name: *const u8) -> FileDescriptor { + unsafe { kernel_function!(__sys_opendir(name)) } } -extern "C" fn __sys_open(name: *const u8, flags: i32, mode: u32) -> FileDescriptor { +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 { @@ -363,8 +368,8 @@ extern "C" fn __sys_open(name: *const u8, flags: i32, mode: u32) -> FileDescript } #[no_mangle] -pub extern "C" fn sys_open(name: *const u8, flags: i32, mode: u32) -> FileDescriptor { - kernel_function!(__sys_open(name, flags, mode)) +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 { @@ -377,7 +382,7 @@ pub extern "C" fn sys_close(fd: FileDescriptor) -> i32 { kernel_function!(__sys_close(fd)) } -extern "C" fn __sys_read(fd: FileDescriptor, buf: *mut u8, len: usize) -> isize { +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(), @@ -386,11 +391,11 @@ extern "C" fn __sys_read(fd: FileDescriptor, buf: *mut u8, len: usize) -> isize } #[no_mangle] -pub extern "C" fn sys_read(fd: FileDescriptor, buf: *mut u8, len: usize) -> isize { - kernel_function!(__sys_read(fd, buf, len)) +pub unsafe extern "C" fn sys_read(fd: FileDescriptor, buf: *mut u8, len: usize) -> isize { + unsafe { kernel_function!(__sys_read(fd, buf, len)) } } -fn write(fd: FileDescriptor, buf: *const u8, len: usize) -> isize { +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( |e| -num::ToPrimitive::to_isize(&e).unwrap(), @@ -398,16 +403,20 @@ fn write(fd: FileDescriptor, buf: *const u8, len: usize) -> isize { ) } -extern "C" fn __sys_write(fd: FileDescriptor, buf: *const u8, len: usize) -> isize { - write(fd, buf, len) +unsafe extern "C" fn __sys_write(fd: FileDescriptor, buf: *const u8, len: usize) -> isize { + unsafe { 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)) +pub unsafe extern "C" fn sys_write(fd: FileDescriptor, buf: *const u8, len: usize) -> isize { + unsafe { kernel_function!(__sys_write(fd, buf, len)) } } -extern "C" fn __sys_ioctl(fd: FileDescriptor, cmd: i32, argp: *mut core::ffi::c_void) -> i32 { +unsafe extern "C" fn __sys_ioctl( + fd: FileDescriptor, + cmd: i32, + argp: *mut core::ffi::c_void, +) -> i32 { const FIONBIO: i32 = 0x8008667eu32 as i32; if cmd == FIONBIO { @@ -427,8 +436,12 @@ extern "C" fn __sys_ioctl(fd: FileDescriptor, cmd: i32, argp: *mut core::ffi::c_ } #[no_mangle] -pub extern "C" fn sys_ioctl(fd: FileDescriptor, cmd: i32, argp: *mut core::ffi::c_void) -> i32 { - kernel_function!(__sys_ioctl(fd, cmd, argp)) +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 { @@ -490,7 +503,11 @@ pub struct Dirent64 { pub d_name: PhantomData, } -extern "C" fn __sys_getdents64(fd: FileDescriptor, dirp: *mut Dirent64, count: usize) -> i64 { +unsafe extern "C" fn __sys_getdents64( + fd: FileDescriptor, + dirp: *mut Dirent64, + count: usize, +) -> i64 { if dirp.is_null() || count == 0 { return -crate::errno::EINVAL as i64; } @@ -541,8 +558,12 @@ extern "C" fn __sys_getdents64(fd: FileDescriptor, dirp: *mut Dirent64, count: u } #[no_mangle] -pub extern "C" fn sys_getdents64(fd: FileDescriptor, dirp: *mut Dirent64, count: usize) -> i64 { - kernel_function!(__sys_getdents64(fd, dirp, count)) +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 { @@ -554,7 +575,7 @@ pub extern "C" fn sys_dup(fd: i32) -> i32 { kernel_function!(__sys_dup(fd)) } -extern "C" fn __sys_poll(fds: *mut PollFd, nfds: usize, timeout: i32) -> i32 { +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( @@ -577,8 +598,8 @@ extern "C" fn __sys_poll(fds: *mut PollFd, nfds: usize, timeout: i32) -> i32 { } #[no_mangle] -pub extern "C" fn sys_poll(fds: *mut PollFd, nfds: usize, timeout: i32) -> i32 { - kernel_function!(__sys_poll(fds, nfds, timeout)) +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 { diff --git a/src/syscalls/semaphore.rs b/src/syscalls/semaphore.rs index 03f1da86f3..16f3a748e4 100644 --- a/src/syscalls/semaphore.rs +++ b/src/syscalls/semaphore.rs @@ -9,7 +9,7 @@ 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. -extern "C" fn __sys_sem_init(sem: *mut *mut Semaphore, value: u32) -> i32 { +unsafe extern "C" fn __sys_sem_init(sem: *mut *mut Semaphore, value: u32) -> i32 { if sem.is_null() { return -EINVAL; } @@ -23,8 +23,8 @@ extern "C" fn __sys_sem_init(sem: *mut *mut Semaphore, value: u32) -> i32 { } #[no_mangle] -pub extern "C" fn sys_sem_init(sem: *mut *mut Semaphore, value: u32) -> i32 { - kernel_function!(__sys_sem_init(sem, value)) +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. @@ -32,7 +32,7 @@ pub extern "C" fn sys_sem_init(sem: *mut *mut Semaphore, value: u32) -> i32 { /// This function can be used to manually deallocate a semaphore via a reference. /// /// Returns `0` on success, `-EINVAL` if `sem` is null. -extern "C" fn __sys_sem_destroy(sem: *mut Semaphore) -> i32 { +unsafe extern "C" fn __sys_sem_destroy(sem: *mut Semaphore) -> i32 { if sem.is_null() { return -EINVAL; } @@ -46,8 +46,8 @@ extern "C" fn __sys_sem_destroy(sem: *mut Semaphore) -> i32 { } #[no_mangle] -pub extern "C" fn sys_sem_destroy(sem: *mut Semaphore) -> i32 { - kernel_function!(__sys_sem_destroy(sem)) +pub unsafe extern "C" fn sys_sem_destroy(sem: *mut Semaphore) -> i32 { + unsafe { kernel_function!(__sys_sem_destroy(sem)) } } /// Release a semaphore. @@ -57,7 +57,7 @@ pub 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. -extern "C" fn __sys_sem_post(sem: *const Semaphore) -> i32 { +unsafe extern "C" fn __sys_sem_post(sem: *const Semaphore) -> i32 { if sem.is_null() { return -EINVAL; } @@ -69,8 +69,8 @@ extern "C" fn __sys_sem_post(sem: *const Semaphore) -> i32 { } #[no_mangle] -pub extern "C" fn sys_sem_post(sem: *const Semaphore) -> i32 { - kernel_function!(__sys_sem_post(sem)) +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. @@ -79,7 +79,7 @@ pub extern "C" fn sys_sem_post(sem: *const Semaphore) -> i32 { /// 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. -extern "C" fn __sys_sem_trywait(sem: *const Semaphore) -> i32 { +unsafe extern "C" fn __sys_sem_trywait(sem: *const Semaphore) -> i32 { if sem.is_null() { return -EINVAL; } @@ -94,11 +94,11 @@ extern "C" fn __sys_sem_trywait(sem: *const Semaphore) -> i32 { } #[no_mangle] -pub extern "C" fn sys_sem_trywait(sem: *const Semaphore) -> i32 { - kernel_function!(__sys_sem_trywait(sem)) +pub unsafe extern "C" fn sys_sem_trywait(sem: *const Semaphore) -> i32 { + unsafe { kernel_function!(__sys_sem_trywait(sem)) } } -fn sem_timedwait(sem: *const Semaphore, ms: u32) -> i32 { +unsafe fn sem_timedwait(sem: *const Semaphore, ms: u32) -> i32 { if sem.is_null() { return -EINVAL; } @@ -119,20 +119,20 @@ 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. -extern "C" fn __sys_sem_timedwait(sem: *const Semaphore, ms: u32) -> i32 { - sem_timedwait(sem, ms) +unsafe extern "C" fn __sys_sem_timedwait(sem: *const Semaphore, ms: u32) -> i32 { + unsafe { 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)) +pub unsafe extern "C" fn sys_sem_timedwait(sem: *const Semaphore, ms: u32) -> i32 { + unsafe { kernel_function!(__sys_sem_timedwait(sem, ms)) } } -extern "C" fn __sys_sem_cancelablewait(sem: *const Semaphore, ms: u32) -> i32 { - sem_timedwait(sem, ms) +unsafe extern "C" fn __sys_sem_cancelablewait(sem: *const Semaphore, ms: u32) -> i32 { + unsafe { sem_timedwait(sem, ms) } } #[no_mangle] -pub extern "C" fn sys_sem_cancelablewait(sem: *const Semaphore, ms: u32) -> i32 { - kernel_function!(__sys_sem_cancelablewait(sem, ms)) +pub unsafe extern "C" fn sys_sem_cancelablewait(sem: *const Semaphore, ms: u32) -> i32 { + unsafe { kernel_function!(__sys_sem_cancelablewait(sem, ms)) } } diff --git a/src/syscalls/socket.rs b/src/syscalls/socket.rs index 513106196f..8f9d7c8a68 100644 --- a/src/syscalls/socket.rs +++ b/src/syscalls/socket.rs @@ -308,7 +308,7 @@ extern "C" fn __sys_socket(domain: i32, type_: SockType, protocol: i32) -> i32 { } } -extern "C" fn __sys_accept(fd: i32, addr: *mut sockaddr, addrlen: *mut socklen_t) -> i32 { +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(), @@ -359,7 +359,7 @@ extern "C" fn __sys_listen(fd: i32, backlog: i32) -> i32 { ) } -extern "C" fn __sys_bind(fd: i32, name: *const sockaddr, namelen: socklen_t) -> i32 { +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 +378,7 @@ extern "C" fn __sys_bind(fd: i32, name: *const sockaddr, namelen: socklen_t) -> ) } -extern "C" fn __sys_connect(fd: i32, name: *const sockaddr, namelen: socklen_t) -> i32 { +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 +397,11 @@ extern "C" fn __sys_connect(fd: i32, name: *const sockaddr, namelen: socklen_t) ) } -extern "C" fn __sys_getsockname(fd: i32, addr: *mut sockaddr, addrlen: *mut socklen_t) -> i32 { +unsafe extern "C" fn __sys_getsockname( + 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(), @@ -436,7 +440,7 @@ extern "C" fn __sys_getsockname(fd: i32, addr: *mut sockaddr, addrlen: *mut sock ) } -extern "C" fn __sys_setsockopt( +unsafe extern "C" fn __sys_setsockopt( fd: i32, level: i32, optname: i32, @@ -472,7 +476,7 @@ extern "C" fn __sys_setsockopt( } } -extern "C" fn __sys_getsockopt( +unsafe extern "C" fn __sys_getsockopt( fd: i32, level: i32, optname: i32, @@ -515,7 +519,11 @@ extern "C" fn __sys_getsockopt( } } -extern "C" fn __sys_getpeername(fd: i32, addr: *mut sockaddr, addrlen: *mut socklen_t) -> i32 { +unsafe extern "C" fn __sys_getpeername( + 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(), @@ -554,9 +562,9 @@ extern "C" fn __sys_getpeername(fd: i32, addr: *mut sockaddr, addrlen: *mut sock ) } -extern "C" fn __sys_freeaddrinfo(_ai: *mut addrinfo) {} +unsafe extern "C" fn __sys_freeaddrinfo(_ai: *mut addrinfo) {} -extern "C" fn __sys_getaddrinfo( +unsafe extern "C" fn __sys_getaddrinfo( _nodename: *const u8, _servname: *const u8, _hints: *const addrinfo, @@ -565,8 +573,8 @@ 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) +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 { @@ -580,7 +588,7 @@ extern "C" fn __sys_shutdown_socket(fd: i32, how: i32) -> i32 { ) } -extern "C" fn __sys_recv(fd: i32, buf: *mut u8, len: usize, flags: i32) -> isize { +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( @@ -592,7 +600,7 @@ extern "C" fn __sys_recv(fd: i32, buf: *mut u8, len: usize, flags: i32) -> isize } } -extern "C" fn __sys_sendto( +unsafe extern "C" fn __sys_sendto( fd: i32, buf: *const u8, len: usize, @@ -621,7 +629,7 @@ extern "C" fn __sys_sendto( ) } -extern "C" fn __sys_recvfrom( +unsafe extern "C" fn __sys_recvfrom( fd: i32, buf: *mut u8, len: usize, @@ -675,8 +683,8 @@ pub extern "C" fn sys_socket(domain: i32, type_: SockType, protocol: i32) -> i32 } #[no_mangle] -pub extern "C" fn sys_accept(s: i32, addr: *mut sockaddr, addrlen: *mut socklen_t) -> i32 { - kernel_function!(__sys_accept(s, addr, addrlen)) +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] @@ -685,65 +693,73 @@ pub extern "C" fn sys_listen(s: i32, backlog: i32) -> i32 { } #[no_mangle] -pub extern "C" fn sys_bind(s: i32, name: *const sockaddr, namelen: socklen_t) -> i32 { - kernel_function!(__sys_bind(s, name, namelen)) +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 extern "C" fn sys_connect(s: i32, name: *const sockaddr, namelen: socklen_t) -> i32 { - kernel_function!(__sys_connect(s, name, namelen)) +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 extern "C" fn sys_getsockname(s: i32, name: *mut sockaddr, namelen: *mut socklen_t) -> i32 { - kernel_function!(__sys_getsockname(s, name, namelen)) +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 extern "C" fn sys_setsockopt( +pub unsafe extern "C" fn sys_setsockopt( s: i32, level: i32, optname: i32, optval: *const c_void, optlen: socklen_t, ) -> i32 { - kernel_function!(__sys_setsockopt(s, level, optname, optval, optlen)) + unsafe { kernel_function!(__sys_setsockopt(s, level, optname, optval, optlen)) } } #[no_mangle] -pub extern "C" fn sys_getsockopt( +pub unsafe extern "C" fn sys_getsockopt( s: i32, level: i32, optname: i32, optval: *mut c_void, optlen: *mut socklen_t, ) -> i32 { - kernel_function!(__sys_getsockopt(s, level, optname, optval, optlen)) + unsafe { kernel_function!(__sys_getsockopt(s, level, optname, optval, optlen)) } } #[no_mangle] -pub extern "C" fn sys_getpeername(s: i32, name: *mut sockaddr, namelen: *mut socklen_t) -> i32 { - kernel_function!(__sys_getpeername(s, name, namelen)) +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 extern "C" fn sys_freeaddrinfo(ai: *mut addrinfo) { - kernel_function!(__sys_freeaddrinfo(ai)) +pub unsafe extern "C" fn sys_freeaddrinfo(ai: *mut addrinfo) { + unsafe { kernel_function!(__sys_freeaddrinfo(ai)) } } #[no_mangle] -pub extern "C" fn sys_getaddrinfo( +pub unsafe extern "C" fn sys_getaddrinfo( nodename: *const u8, servname: *const u8, hints: *const addrinfo, res: *mut *mut addrinfo, ) -> i32 { - kernel_function!(__sys_getaddrinfo(nodename, servname, hints, res)) + unsafe { kernel_function!(__sys_getaddrinfo(nodename, servname, hints, res)) } } #[no_mangle] -pub extern "C" fn sys_send(s: i32, mem: *const c_void, len: usize, flags: i32) -> isize { - kernel_function!(__sys_send(s, mem, len, flags)) +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] @@ -752,12 +768,12 @@ 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 { - kernel_function!(__sys_recv(fd, buf, len, flags)) +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 extern "C" fn sys_sendto( +pub unsafe extern "C" fn sys_sendto( socket: i32, buf: *const u8, len: usize, @@ -765,11 +781,11 @@ pub extern "C" fn sys_sendto( addr: *const sockaddr, addrlen: socklen_t, ) -> isize { - kernel_function!(__sys_sendto(socket, buf, len, flags, addr, addrlen)) + unsafe { kernel_function!(__sys_sendto(socket, buf, len, flags, addr, addrlen)) } } #[no_mangle] -pub extern "C" fn sys_recvfrom( +pub unsafe extern "C" fn sys_recvfrom( socket: i32, buf: *mut u8, len: usize, @@ -777,5 +793,5 @@ pub extern "C" fn sys_recvfrom( addr: *mut sockaddr, addrlen: *mut socklen_t, ) -> isize { - kernel_function!(__sys_recvfrom(socket, buf, len, flags, addr, addrlen)) + unsafe { kernel_function!(__sys_recvfrom(socket, buf, len, flags, addr, addrlen)) } } diff --git a/src/syscalls/spinlock.rs b/src/syscalls/spinlock.rs index 9551c8de1a..515bc8bc6e 100644 --- a/src/syscalls/spinlock.rs +++ b/src/syscalls/spinlock.rs @@ -14,7 +14,7 @@ pub struct SpinlockIrqSaveContainer<'a> { guard: Option>, } -extern "C" fn __sys_spinlock_init(lock: *mut *mut SpinlockContainer<'_>) -> i32 { +unsafe extern "C" fn __sys_spinlock_init(lock: *mut *mut SpinlockContainer<'_>) -> i32 { if lock.is_null() { return -EINVAL; } @@ -30,11 +30,11 @@ extern "C" fn __sys_spinlock_init(lock: *mut *mut SpinlockContainer<'_>) -> i32 } #[no_mangle] -pub extern "C" fn sys_spinlock_init(lock: *mut *mut SpinlockContainer<'_>) -> i32 { - kernel_function!(__sys_spinlock_init(lock)) +pub unsafe extern "C" fn sys_spinlock_init(lock: *mut *mut SpinlockContainer<'_>) -> i32 { + unsafe { kernel_function!(__sys_spinlock_init(lock)) } } -extern "C" fn __sys_spinlock_destroy(lock: *mut SpinlockContainer<'_>) -> i32 { +unsafe extern "C" fn __sys_spinlock_destroy(lock: *mut SpinlockContainer<'_>) -> i32 { if lock.is_null() { return -EINVAL; } @@ -47,11 +47,11 @@ extern "C" fn __sys_spinlock_destroy(lock: *mut SpinlockContainer<'_>) -> i32 { } #[no_mangle] -pub extern "C" fn sys_spinlock_destroy(lock: *mut SpinlockContainer<'_>) -> i32 { - kernel_function!(__sys_spinlock_destroy(lock)) +pub unsafe extern "C" fn sys_spinlock_destroy(lock: *mut SpinlockContainer<'_>) -> i32 { + unsafe { kernel_function!(__sys_spinlock_destroy(lock)) } } -extern "C" fn __sys_spinlock_lock(lock: *mut SpinlockContainer<'_>) -> i32 { +unsafe extern "C" fn __sys_spinlock_lock(lock: *mut SpinlockContainer<'_>) -> i32 { if lock.is_null() { return -EINVAL; } @@ -66,11 +66,11 @@ extern "C" fn __sys_spinlock_lock(lock: *mut SpinlockContainer<'_>) -> i32 { } #[no_mangle] -pub extern "C" fn sys_spinlock_lock(lock: *mut SpinlockContainer<'_>) -> i32 { - kernel_function!(__sys_spinlock_lock(lock)) +pub unsafe extern "C" fn sys_spinlock_lock(lock: *mut SpinlockContainer<'_>) -> i32 { + unsafe { kernel_function!(__sys_spinlock_lock(lock)) } } -extern "C" fn __sys_spinlock_unlock(lock: *mut SpinlockContainer<'_>) -> i32 { +unsafe extern "C" fn __sys_spinlock_unlock(lock: *mut SpinlockContainer<'_>) -> i32 { if lock.is_null() { return -EINVAL; } @@ -85,11 +85,13 @@ extern "C" fn __sys_spinlock_unlock(lock: *mut SpinlockContainer<'_>) -> i32 { } #[no_mangle] -pub extern "C" fn sys_spinlock_unlock(lock: *mut SpinlockContainer<'_>) -> i32 { - kernel_function!(__sys_spinlock_unlock(lock)) +pub unsafe extern "C" fn sys_spinlock_unlock(lock: *mut SpinlockContainer<'_>) -> i32 { + unsafe { kernel_function!(__sys_spinlock_unlock(lock)) } } -extern "C" fn __sys_spinlock_irqsave_init(lock: *mut *mut SpinlockIrqSaveContainer<'_>) -> i32 { +unsafe extern "C" fn __sys_spinlock_irqsave_init( + lock: *mut *mut SpinlockIrqSaveContainer<'_>, +) -> i32 { if lock.is_null() { return -EINVAL; } @@ -105,11 +107,15 @@ extern "C" fn __sys_spinlock_irqsave_init(lock: *mut *mut SpinlockIrqSaveContain } #[no_mangle] -pub extern "C" fn sys_spinlock_irqsave_init(lock: *mut *mut SpinlockIrqSaveContainer<'_>) -> i32 { - kernel_function!(__sys_spinlock_irqsave_init(lock)) +pub unsafe extern "C" fn sys_spinlock_irqsave_init( + lock: *mut *mut SpinlockIrqSaveContainer<'_>, +) -> i32 { + unsafe { kernel_function!(__sys_spinlock_irqsave_init(lock)) } } -extern "C" fn __sys_spinlock_irqsave_destroy(lock: *mut SpinlockIrqSaveContainer<'_>) -> i32 { +unsafe extern "C" fn __sys_spinlock_irqsave_destroy( + lock: *mut SpinlockIrqSaveContainer<'_>, +) -> i32 { if lock.is_null() { return -EINVAL; } @@ -122,11 +128,13 @@ extern "C" fn __sys_spinlock_irqsave_destroy(lock: *mut SpinlockIrqSaveContainer } #[no_mangle] -pub extern "C" fn sys_spinlock_irqsave_destroy(lock: *mut SpinlockIrqSaveContainer<'_>) -> i32 { - kernel_function!(__sys_spinlock_irqsave_destroy(lock)) +pub unsafe extern "C" fn sys_spinlock_irqsave_destroy( + lock: *mut SpinlockIrqSaveContainer<'_>, +) -> i32 { + unsafe { kernel_function!(__sys_spinlock_irqsave_destroy(lock)) } } -extern "C" fn __sys_spinlock_irqsave_lock(lock: *mut SpinlockIrqSaveContainer<'_>) -> i32 { +unsafe extern "C" fn __sys_spinlock_irqsave_lock(lock: *mut SpinlockIrqSaveContainer<'_>) -> i32 { if lock.is_null() { return -EINVAL; } @@ -141,11 +149,11 @@ extern "C" fn __sys_spinlock_irqsave_lock(lock: *mut SpinlockIrqSaveContainer<'_ } #[no_mangle] -pub extern "C" fn sys_spinlock_irqsave_lock(lock: *mut SpinlockIrqSaveContainer<'_>) -> i32 { - kernel_function!(__sys_spinlock_irqsave_lock(lock)) +pub unsafe extern "C" fn sys_spinlock_irqsave_lock(lock: *mut SpinlockIrqSaveContainer<'_>) -> i32 { + unsafe { kernel_function!(__sys_spinlock_irqsave_lock(lock)) } } -extern "C" fn __sys_spinlock_irqsave_unlock(lock: *mut SpinlockIrqSaveContainer<'_>) -> i32 { +unsafe extern "C" fn __sys_spinlock_irqsave_unlock(lock: *mut SpinlockIrqSaveContainer<'_>) -> i32 { if lock.is_null() { return -EINVAL; } @@ -160,6 +168,8 @@ extern "C" fn __sys_spinlock_irqsave_unlock(lock: *mut SpinlockIrqSaveContainer< } #[no_mangle] -pub extern "C" fn sys_spinlock_irqsave_unlock(lock: *mut SpinlockIrqSaveContainer<'_>) -> i32 { - kernel_function!(__sys_spinlock_irqsave_unlock(lock)) +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/tasks.rs b/src/syscalls/tasks.rs index e5ce8bf02a..d02be3038c 100644 --- a/src/syscalls/tasks.rs +++ b/src/syscalls/tasks.rs @@ -154,7 +154,7 @@ pub extern "C" fn sys_msleep(ms: u32) { kernel_function!(__sys_msleep(ms)) } -extern "C" fn __sys_nanosleep(rqtp: *const timespec, _rmtp: *mut timespec) -> i32 { +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" @@ -176,8 +176,8 @@ extern "C" fn __sys_nanosleep(rqtp: *const timespec, _rmtp: *mut timespec) -> i3 } #[no_mangle] -pub extern "C" fn sys_nanosleep(rqtp: *const timespec, rmtp: *mut timespec) -> i32 { - kernel_function!(__sys_nanosleep(rqtp, rmtp)) +pub unsafe extern "C" fn sys_nanosleep(rqtp: *const timespec, rmtp: *mut timespec) -> i32 { + unsafe { kernel_function!(__sys_nanosleep(rqtp, rmtp)) } } #[cfg(feature = "newlib")] @@ -236,36 +236,37 @@ pub extern "C" fn sys_signal(handler: SignalHandler) -> i32 { kernel_function!(__sys_signal(handler)) } -extern "C" fn __sys_spawn2( - func: extern "C" fn(usize), +unsafe extern "C" fn __sys_spawn2( + func: unsafe extern "C" fn(usize), arg: usize, prio: u8, stack_size: usize, selector: isize, ) -> Tid { - scheduler::spawn(func, arg, Priority::from(prio), stack_size, selector).into() + unsafe { scheduler::spawn(func, arg, Priority::from(prio), stack_size, selector).into() } } #[no_mangle] -pub extern "C" fn sys_spawn2( - func: extern "C" fn(usize), +pub unsafe extern "C" fn sys_spawn2( + func: unsafe extern "C" fn(usize), arg: usize, prio: u8, stack_size: usize, selector: isize, ) -> Tid { - kernel_function!(__sys_spawn2(func, arg, prio, stack_size, selector)) + unsafe { kernel_function!(__sys_spawn2(func, arg, prio, stack_size, selector)) } } -extern "C" fn __sys_spawn( +unsafe extern "C" fn __sys_spawn( id: *mut Tid, - func: extern "C" fn(usize), + func: unsafe extern "C" fn(usize), arg: usize, prio: u8, selector: isize, ) -> i32 { - let new_id = - scheduler::spawn(func, arg, Priority::from(prio), USER_STACK_SIZE, selector).into(); + let new_id = unsafe { + scheduler::spawn(func, arg, Priority::from(prio), USER_STACK_SIZE, selector).into() + }; if !id.is_null() { unsafe { @@ -277,14 +278,14 @@ extern "C" fn __sys_spawn( } #[no_mangle] -pub extern "C" fn sys_spawn( +pub unsafe extern "C" fn sys_spawn( id: *mut Tid, - func: extern "C" fn(usize), + func: unsafe extern "C" fn(usize), arg: usize, prio: u8, selector: isize, ) -> i32 { - kernel_function!(__sys_spawn(id, func, arg, prio, selector)) + unsafe { kernel_function!(__sys_spawn(id, func, arg, prio, selector)) } } extern "C" fn __sys_join(id: Tid) -> i32 { diff --git a/src/syscalls/timer.rs b/src/syscalls/timer.rs index b2dacbd747..7bee508e92 100644 --- a/src/syscalls/timer.rs +++ b/src/syscalls/timer.rs @@ -19,7 +19,7 @@ pub(crate) const TIMER_ABSTIME: i32 = 4; /// - `CLOCK_PROCESS_CPUTIME_ID` /// - `CLOCK_THREAD_CPUTIME_ID` /// - `CLOCK_MONOTONIC` -extern "C" fn __sys_clock_getres(clock_id: u64, res: *mut timespec) -> i32 { +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" @@ -40,8 +40,8 @@ extern "C" fn __sys_clock_getres(clock_id: u64, res: *mut timespec) -> i32 { } #[no_mangle] -pub extern "C" fn sys_clock_getres(clock_id: u64, res: *mut timespec) -> i32 { - kernel_function!(__sys_clock_getres(clock_id, res)) +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. @@ -52,7 +52,7 @@ pub extern "C" fn sys_clock_getres(clock_id: u64, res: *mut timespec) -> i32 { /// Supported clocks: /// - `CLOCK_REALTIME` /// - `CLOCK_MONOTONIC` -extern "C" fn __sys_clock_gettime(clock_id: u64, tp: *mut timespec) -> i32 { +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" @@ -79,8 +79,8 @@ extern "C" fn __sys_clock_gettime(clock_id: u64, tp: *mut timespec) -> i32 { } #[no_mangle] -pub extern "C" fn sys_clock_gettime(clock_id: u64, tp: *mut timespec) -> i32 { - kernel_function!(__sys_clock_gettime(clock_id, tp)) +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. @@ -92,7 +92,7 @@ pub extern "C" fn sys_clock_gettime(clock_id: u64, tp: *mut timespec) -> i32 { /// Supported clocks: /// - `CLOCK_REALTIME` /// - `CLOCK_MONOTONIC` -extern "C" fn __sys_clock_nanosleep( +unsafe extern "C" fn __sys_clock_nanosleep( clock_id: u64, flags: i32, rqtp: *const timespec, @@ -132,24 +132,24 @@ extern "C" fn __sys_clock_nanosleep( } #[no_mangle] -pub extern "C" fn sys_clock_nanosleep( +pub unsafe extern "C" fn sys_clock_nanosleep( clock_id: u64, flags: i32, rqtp: *const timespec, rmtp: *mut timespec, ) -> i32 { - kernel_function!(__sys_clock_nanosleep(clock_id, flags, rqtp, rmtp)) + unsafe { kernel_function!(__sys_clock_nanosleep(clock_id, flags, rqtp, rmtp)) } } -extern "C" fn __sys_clock_settime(_clock_id: u64, _tp: *const timespec) -> i32 { +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 extern "C" fn sys_clock_settime(clock_id: u64, tp: *const timespec) -> i32 { - kernel_function!(__sys_clock_settime(clock_id, tp)) +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. @@ -158,7 +158,7 @@ pub extern "C" fn sys_clock_settime(clock_id: u64, tp: *const timespec) -> i32 { /// Returns `0` on success, `-EINVAL` otherwise. /// /// **Parameter `tz` should be set to `0` since tz is obsolete.** -extern "C" fn __sys_gettimeofday(tp: *mut timeval, tz: usize) -> i32 { +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. @@ -175,12 +175,12 @@ extern "C" fn __sys_gettimeofday(tp: *mut timeval, tz: usize) -> i32 { } #[no_mangle] -pub extern "C" fn sys_gettimeofday(tp: *mut timeval, tz: usize) -> i32 { - kernel_function!(__sys_gettimeofday(tp, tz)) +pub unsafe extern "C" fn sys_gettimeofday(tp: *mut timeval, tz: usize) -> i32 { + unsafe { kernel_function!(__sys_gettimeofday(tp, tz)) } } #[no_mangle] -extern "C" fn __sys_setitimer( +unsafe extern "C" fn __sys_setitimer( _which: i32, _value: *const itimerval, _ovalue: *mut itimerval, @@ -190,10 +190,10 @@ extern "C" fn __sys_setitimer( } #[no_mangle] -pub extern "C" fn sys_setitimer( +pub unsafe extern "C" fn sys_setitimer( which: i32, value: *const itimerval, ovalue: *mut itimerval, ) -> i32 { - kernel_function!(__sys_setitimer(which, value, ovalue)) + unsafe { kernel_function!(__sys_setitimer(which, value, ovalue)) } } diff --git a/tests/thread.rs b/tests/thread.rs index 9a624b1e9b..776bba5b97 100644 --- a/tests/thread.rs +++ b/tests/thread.rs @@ -37,7 +37,7 @@ pub fn thread_test() { let threadnum = 5; for i in 0..threadnum { println!("SPAWNING THREAD {}", i); - let id = sys_spawn2(thread_func, i, NORMAL_PRIO, USER_STACK_SIZE, -1); + let id = unsafe { sys_spawn2(thread_func, i, NORMAL_PRIO, USER_STACK_SIZE, -1) }; children.push(id); } println!("SPAWNED THREADS"); @@ -47,13 +47,13 @@ pub fn thread_test() { } } -extern "C" fn waker_func(futex: usize) { +unsafe extern "C" fn waker_func(futex: usize) { let futex = unsafe { &*(futex as *const AtomicU32) }; sys_usleep(100_000); futex.store(1, Relaxed); - let ret = sys_futex_wake(futex as *const AtomicU32 as *mut u32, i32::MAX); + let ret = unsafe { sys_futex_wake(futex as *const AtomicU32 as *mut u32, i32::MAX) }; assert_eq!(ret, 1); } @@ -62,26 +62,28 @@ pub fn test_futex() { let futex = AtomicU32::new(0); let futex_ptr = &futex as *const AtomicU32 as *mut u32; - let ret = sys_futex_wait(futex_ptr, 1, ptr::null(), 0); + let ret = unsafe { sys_futex_wait(futex_ptr, 1, ptr::null(), 0) }; assert_eq!(ret, -EAGAIN); let timeout = timespec { tv_sec: 0, tv_nsec: 100_000_000, }; - let ret = sys_futex_wait(futex_ptr, 0, &timeout, 1); + let ret = unsafe { sys_futex_wait(futex_ptr, 0, &timeout, 1) }; assert_eq!(ret, -ETIMEDOUT); - let waker = sys_spawn2( - waker_func, - futex_ptr as usize, - NORMAL_PRIO, - USER_STACK_SIZE, - -1, - ); + let waker = unsafe { + sys_spawn2( + waker_func, + futex_ptr as usize, + NORMAL_PRIO, + USER_STACK_SIZE, + -1, + ) + }; assert!(waker >= 0); - let ret = sys_futex_wait(futex_ptr, 0, ptr::null(), 0); + let ret = unsafe { sys_futex_wait(futex_ptr, 0, ptr::null(), 0) }; assert_eq!(ret, 0); assert_eq!(futex.load(Relaxed), 1);