|
| 1 | +// SPDX-License-Identifier: GPL-2.0 |
| 2 | + |
| 3 | +// Copyright (C) 2024 Google LLC. |
| 4 | + |
| 5 | +//! Logic for static calls. |
| 6 | +
|
| 7 | +#[macro_export] |
| 8 | +#[doc(hidden)] |
| 9 | +macro_rules! ty_underscore_for { |
| 10 | + ($arg:expr) => { |
| 11 | + _ |
| 12 | + }; |
| 13 | +} |
| 14 | + |
| 15 | +#[doc(hidden)] |
| 16 | +#[repr(transparent)] |
| 17 | +pub struct AddressableStaticCallKey { |
| 18 | + _ptr: *const bindings::static_call_key, |
| 19 | +} |
| 20 | +unsafe impl Sync for AddressableStaticCallKey {} |
| 21 | +impl AddressableStaticCallKey { |
| 22 | + pub const fn new(ptr: *const bindings::static_call_key) -> Self { |
| 23 | + Self { _ptr: ptr } |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +#[cfg(CONFIG_HAVE_STATIC_CALL)] |
| 28 | +#[doc(hidden)] |
| 29 | +#[macro_export] |
| 30 | +macro_rules! _static_call { |
| 31 | + ($name:ident($($args:expr),* $(,)?)) => {{ |
| 32 | + // Symbol mangling will give this symbol a unique name. |
| 33 | + #[cfg(CONFIG_HAVE_STATIC_CALL_INLINE)] |
| 34 | + #[link_section = ".discard.addressable"] |
| 35 | + #[used] |
| 36 | + static __ADDRESSABLE: $crate::static_call::AddressableStaticCallKey = unsafe { |
| 37 | + $crate::static_call::AddressableStaticCallKey::new(::core::ptr::addr_of!( |
| 38 | + $crate::macros::paste! { $crate::bindings:: [<__SCK__ $name >]; } |
| 39 | + )) |
| 40 | + }; |
| 41 | + |
| 42 | + let fn_ptr: unsafe extern "C" fn($($crate::static_call::ty_underscore_for!($args)),*) -> _ = |
| 43 | + $crate::macros::paste! { $crate::bindings:: [<__SCT__ $name >]; }; |
| 44 | + (fn_ptr)($($args),*) |
| 45 | + }}; |
| 46 | +} |
| 47 | + |
| 48 | +#[cfg(not(CONFIG_HAVE_STATIC_CALL))] |
| 49 | +#[doc(hidden)] |
| 50 | +#[macro_export] |
| 51 | +macro_rules! _static_call { |
| 52 | + ($name:ident($($args:expr),* $(,)?)) => {{ |
| 53 | + let void_ptr_fn: *mut ::core::ffi::c_void = $crate::macros::paste! { $crate::bindings:: [<__SCK__ $name >]; }.func; |
| 54 | + |
| 55 | + let fn_ptr: unsafe extern "C" fn($($crate::static_call::ty_underscore_for!($args)),*) -> _ = if true { |
| 56 | + ::core::mem::transmute(void_ptr_fn) |
| 57 | + } else { |
| 58 | + // This is dead code, but it influences type inference on `fn_ptr` so that we transmute |
| 59 | + // the function pointer to the right type. |
| 60 | + $crate::macros::paste! { $crate::bindings:: [<__SCT__ $name >]; } |
| 61 | + }; |
| 62 | + |
| 63 | + (fn_ptr)($($args),*) |
| 64 | + }}; |
| 65 | +} |
| 66 | + |
| 67 | +/// Statically call a global function. |
| 68 | +/// |
| 69 | +/// # Safety |
| 70 | +/// |
| 71 | +/// This macro will call the provided function. It is up to the caller to uphold the safety |
| 72 | +/// guarantees of the function. |
| 73 | +/// |
| 74 | +/// # Examples |
| 75 | +/// |
| 76 | +/// ```ignore |
| 77 | +/// fn call_static() { |
| 78 | +/// unsafe { |
| 79 | +/// static_call! { your_static_call() }; |
| 80 | +/// } |
| 81 | +/// } |
| 82 | +/// ``` |
| 83 | +#[macro_export] |
| 84 | +macro_rules! static_call { |
| 85 | + // Forward to the real implementation. Separated like this so that we don't have to duplicate |
| 86 | + // the documentation. |
| 87 | + ($($args:tt)*) => { $crate::static_call::_static_call! { $($args)* } }; |
| 88 | +} |
| 89 | + |
| 90 | +pub use {_static_call, static_call, ty_underscore_for}; |
0 commit comments