• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0
2 
3 // Copyright (C) 2024 Google LLC.
4 
5 //! Logic for static keys.
6 //!
7 //! C header: [`include/linux/jump_label.h`](srctree/include/linux/jump_label.h).
8 
9 /// Branch based on a static key.
10 ///
11 /// Takes three arguments:
12 ///
13 /// * `key` - the path to the static variable containing the `static_key`.
14 /// * `keytyp` - the type of `key`.
15 /// * `field` - the name of the field of `key` that contains the `static_key`.
16 ///
17 /// # Safety
18 ///
19 /// The macro must be used with a real static key defined by C.
20 #[macro_export]
21 macro_rules! static_branch_unlikely {
22     ($key:path, $keytyp:ty, $field:ident) => {{
23         let _key: *const $keytyp = ::core::ptr::addr_of!($key);
24         let _key: *const $crate::bindings::static_key = ::core::ptr::addr_of!((*_key).$field);
25 
26         #[cfg(not(CONFIG_JUMP_LABEL))]
27         {
28             $crate::bindings::static_key_count(_key.cast_mut()) > 0
29         }
30 
31         #[cfg(CONFIG_JUMP_LABEL)]
32         $crate::jump_label::arch_static_branch! { $key, $keytyp, $field, false }
33     }};
34 }
35 pub use static_branch_unlikely;
36 
37 /// Assert that the assembly block evaluates to a string literal.
38 #[cfg(CONFIG_JUMP_LABEL)]
39 const _: &str = include!(concat!(
40     env!("OBJTREE"),
41     "/rust/kernel/generated_arch_static_branch_asm.rs"
42 ));
43 
44 #[macro_export]
45 #[doc(hidden)]
46 #[cfg(CONFIG_JUMP_LABEL)]
47 macro_rules! arch_static_branch {
48     ($key:path, $keytyp:ty, $field:ident, $branch:expr) => {'my_label: {
49         $crate::asm!(
50             include!(concat!(env!("OBJTREE"), "/rust/kernel/generated_arch_static_branch_asm.rs"));
51             l_yes = label {
52                 break 'my_label true;
53             },
54             symb = sym $key,
55             off = const ::core::mem::offset_of!($keytyp, $field),
56             branch = const $crate::jump_label::bool_to_int($branch),
57         );
58 
59         break 'my_label false;
60     }};
61 }
62 
63 #[cfg(CONFIG_JUMP_LABEL)]
64 pub use arch_static_branch;
65 
66 /// A helper used by inline assembly to pass a boolean to as a `const` parameter.
67 ///
68 /// Using this function instead of a cast lets you assert that the input is a boolean, and not some
69 /// other type that can also be cast to an integer.
70 #[doc(hidden)]
bool_to_int(b: bool) -> i3271 pub const fn bool_to_int(b: bool) -> i32 {
72     b as i32
73 }
74