1 /*
2  * Copyright (c) 2024 Google Inc. All rights reserved
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining
5  * a copy of this software and associated documentation files
6  * (the "Software"), to deal in the Software without restriction,
7  * including without limitation the rights to use, copy, modify, merge,
8  * publish, distribute, sublicense, and/or sell copies of the Software,
9  * and to permit persons to whom the Software is furnished to do so,
10  * subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be
13  * included in all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
19  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
20  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
21  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22  */
23 
24 use crate::sys::uint;
25 use core::ffi::c_char;
26 
27 pub use crate::sys::lk_init_flags;
28 pub use crate::sys::lk_init_level;
29 pub use crate::sys::lk_init_struct;
30 
31 // SAFETY: lk_init_struct does not use interior mutability.
32 unsafe impl Sync for lk_init_struct {}
33 
34 // lk_init_level constants have large gaps between them and some modules
35 // add or subtract from these constants to indicate that it wants to run
36 // right before or after other init hooks at a given level. Add add and sub
37 // functions to lk_init_level to allow this for rust init hooks as well.
38 impl lk_init_level {
add(mut self, rhs: uint) -> Self39     pub const fn add(mut self, rhs: uint) -> Self {
40         self.0 += rhs;
41         self
42     }
sub(mut self, rhs: uint) -> Self43     pub const fn sub(mut self, rhs: uint) -> Self {
44         self.0 -= rhs;
45         self
46     }
47 }
48 
49 impl lk_init_struct {
new( level: lk_init_level, flags: lk_init_flags, hook: unsafe extern "C" fn(uint), name: *const c_char, ) -> Self50     pub const fn new(
51         level: lk_init_level,
52         flags: lk_init_flags,
53         hook: unsafe extern "C" fn(uint),
54         name: *const c_char,
55     ) -> Self {
56         lk_init_struct { level: level.0, flags: flags.0, hook: Option::Some(hook), name }
57     }
58 }
59 
60 #[macro_export]
61 macro_rules! LK_INIT_HOOK_FLAGS {
62     ($name:ident, $hook:expr, $level:expr, $flags:expr) => {
63         #[link_section = ".lk_init"]
64         #[no_mangle]
65         #[used]
66         static $name: $crate::init::lk_init_struct = $crate::init::lk_init_struct::new(
67             $level,
68             $flags,
69             $hook,
70             (concat!(stringify!($name), "\0").as_bytes()).as_ptr().cast(),
71         );
72     };
73 }
74 
75 #[macro_export]
76 macro_rules! LK_INIT_HOOK {
77     ($name:ident, $hook:expr, $level:expr) => {
78         $crate::LK_INIT_HOOK_FLAGS!(
79             $name,
80             $hook,
81             $level,
82             $crate::init::lk_init_flags::LK_INIT_FLAG_PRIMARY_CPU
83         );
84     };
85 }
86