16e59bcc9SAlice Ryhl // SPDX-License-Identifier: GPL-2.0 26e59bcc9SAlice Ryhl 36e59bcc9SAlice Ryhl // Copyright (C) 2024 Google LLC. 46e59bcc9SAlice Ryhl 56e59bcc9SAlice Ryhl //! Logic for static keys. 66e59bcc9SAlice Ryhl //! 76e59bcc9SAlice Ryhl //! C header: [`include/linux/jump_label.h`](srctree/include/linux/jump_label.h). 86e59bcc9SAlice Ryhl 96e59bcc9SAlice Ryhl /// Branch based on a static key. 106e59bcc9SAlice Ryhl /// 116e59bcc9SAlice Ryhl /// Takes three arguments: 126e59bcc9SAlice Ryhl /// 136e59bcc9SAlice Ryhl /// * `key` - the path to the static variable containing the `static_key`. 146e59bcc9SAlice Ryhl /// * `keytyp` - the type of `key`. 156e59bcc9SAlice Ryhl /// * `field` - the name of the field of `key` that contains the `static_key`. 166e59bcc9SAlice Ryhl /// 176e59bcc9SAlice Ryhl /// # Safety 186e59bcc9SAlice Ryhl /// 196e59bcc9SAlice Ryhl /// The macro must be used with a real static key defined by C. 206e59bcc9SAlice Ryhl #[macro_export] 216e59bcc9SAlice Ryhl macro_rules! static_branch_unlikely { 226e59bcc9SAlice Ryhl ($key:path, $keytyp:ty, $field:ident) => {{ 236e59bcc9SAlice Ryhl let _key: *const $keytyp = ::core::ptr::addr_of!($key); 246e59bcc9SAlice Ryhl let _key: *const $crate::bindings::static_key_false = ::core::ptr::addr_of!((*_key).$field); 256e59bcc9SAlice Ryhl let _key: *const $crate::bindings::static_key = _key.cast(); 266e59bcc9SAlice Ryhl 27169484abSAlice Ryhl #[cfg(not(CONFIG_JUMP_LABEL))] 28169484abSAlice Ryhl { 29*7643155dSAlice Ryhl $crate::bindings::static_key_count(_key.cast_mut()) > 0 30169484abSAlice Ryhl } 31169484abSAlice Ryhl 32169484abSAlice Ryhl #[cfg(CONFIG_JUMP_LABEL)] 33169484abSAlice Ryhl $crate::jump_label::arch_static_branch! { $key, $keytyp, $field, false } 346e59bcc9SAlice Ryhl }}; 356e59bcc9SAlice Ryhl } 366e59bcc9SAlice Ryhl pub use static_branch_unlikely; 37169484abSAlice Ryhl 38169484abSAlice Ryhl /// Assert that the assembly block evaluates to a string literal. 39169484abSAlice Ryhl #[cfg(CONFIG_JUMP_LABEL)] 40169484abSAlice Ryhl const _: &str = include!(concat!( 41169484abSAlice Ryhl env!("OBJTREE"), 42169484abSAlice Ryhl "/rust/kernel/arch_static_branch_asm.rs" 43169484abSAlice Ryhl )); 44169484abSAlice Ryhl 45169484abSAlice Ryhl #[macro_export] 46169484abSAlice Ryhl #[doc(hidden)] 47169484abSAlice Ryhl #[cfg(CONFIG_JUMP_LABEL)] 48169484abSAlice Ryhl macro_rules! arch_static_branch { 49169484abSAlice Ryhl ($key:path, $keytyp:ty, $field:ident, $branch:expr) => {'my_label: { 50169484abSAlice Ryhl $crate::asm!( 51169484abSAlice Ryhl include!(concat!(env!("OBJTREE"), "/rust/kernel/arch_static_branch_asm.rs")); 52169484abSAlice Ryhl l_yes = label { 53169484abSAlice Ryhl break 'my_label true; 54169484abSAlice Ryhl }, 55169484abSAlice Ryhl symb = sym $key, 56169484abSAlice Ryhl off = const ::core::mem::offset_of!($keytyp, $field), 57169484abSAlice Ryhl branch = const $crate::jump_label::bool_to_int($branch), 58169484abSAlice Ryhl ); 59169484abSAlice Ryhl 60169484abSAlice Ryhl break 'my_label false; 61169484abSAlice Ryhl }}; 62169484abSAlice Ryhl } 63169484abSAlice Ryhl 64169484abSAlice Ryhl #[cfg(CONFIG_JUMP_LABEL)] 65169484abSAlice Ryhl pub use arch_static_branch; 66169484abSAlice Ryhl 67169484abSAlice Ryhl /// A helper used by inline assembly to pass a boolean to as a `const` parameter. 68169484abSAlice Ryhl /// 69169484abSAlice Ryhl /// Using this function instead of a cast lets you assert that the input is a boolean, and not some 70169484abSAlice Ryhl /// other type that can also be cast to an integer. 71169484abSAlice Ryhl #[doc(hidden)] 72169484abSAlice Ryhl pub const fn bool_to_int(b: bool) -> i32 { 73169484abSAlice Ryhl b as i32 74169484abSAlice Ryhl } 75