macros/
module.rs

1// SPDX-License-Identifier: GPL-2.0
2
3use crate::helpers::*;
4use proc_macro::{token_stream, Delimiter, Literal, TokenStream, TokenTree};
5use std::fmt::Write;
6
7fn expect_string_array(it: &mut token_stream::IntoIter) -> Vec<String> {
8    let group = expect_group(it);
9    assert_eq!(group.delimiter(), Delimiter::Bracket);
10    let mut values = Vec::new();
11    let mut it = group.stream().into_iter();
12
13    while let Some(val) = try_string(&mut it) {
14        assert!(val.is_ascii(), "Expected ASCII string");
15        values.push(val);
16        match it.next() {
17            Some(TokenTree::Punct(punct)) => assert_eq!(punct.as_char(), ','),
18            None => break,
19            _ => panic!("Expected ',' or end of array"),
20        }
21    }
22    values
23}
24
25struct ModInfoBuilder<'a> {
26    module: &'a str,
27    counter: usize,
28    buffer: String,
29}
30
31impl<'a> ModInfoBuilder<'a> {
32    fn new(module: &'a str) -> Self {
33        ModInfoBuilder {
34            module,
35            counter: 0,
36            buffer: String::new(),
37        }
38    }
39
40    fn emit_base(&mut self, field: &str, content: &str, builtin: bool) {
41        let string = if builtin {
42            // Built-in modules prefix their modinfo strings by `module.`.
43            format!(
44                "{module}.{field}={content}\0",
45                module = self.module,
46                field = field,
47                content = content
48            )
49        } else {
50            // Loadable modules' modinfo strings go as-is.
51            format!("{field}={content}\0")
52        };
53
54        write!(
55            &mut self.buffer,
56            "
57                {cfg}
58                #[doc(hidden)]
59                #[link_section = \".modinfo\"]
60                #[used]
61                pub static __{module}_{counter}: [u8; {length}] = *{string};
62            ",
63            cfg = if builtin {
64                "#[cfg(not(MODULE))]"
65            } else {
66                "#[cfg(MODULE)]"
67            },
68            module = self.module.to_uppercase(),
69            counter = self.counter,
70            length = string.len(),
71            string = Literal::byte_string(string.as_bytes()),
72        )
73        .unwrap();
74
75        self.counter += 1;
76    }
77
78    fn emit_only_builtin(&mut self, field: &str, content: &str) {
79        self.emit_base(field, content, true)
80    }
81
82    fn emit_only_loadable(&mut self, field: &str, content: &str) {
83        self.emit_base(field, content, false)
84    }
85
86    fn emit(&mut self, field: &str, content: &str) {
87        self.emit_only_builtin(field, content);
88        self.emit_only_loadable(field, content);
89    }
90}
91
92#[derive(Debug, Default)]
93struct ModuleInfo {
94    type_: String,
95    license: String,
96    name: String,
97    author: Option<String>,
98    description: Option<String>,
99    alias: Option<Vec<String>>,
100    firmware: Option<Vec<String>>,
101}
102
103impl ModuleInfo {
104    fn parse(it: &mut token_stream::IntoIter) -> Self {
105        let mut info = ModuleInfo::default();
106
107        const EXPECTED_KEYS: &[&str] = &[
108            "type",
109            "name",
110            "author",
111            "description",
112            "license",
113            "alias",
114            "firmware",
115        ];
116        const REQUIRED_KEYS: &[&str] = &["type", "name", "license"];
117        let mut seen_keys = Vec::new();
118
119        loop {
120            let key = match it.next() {
121                Some(TokenTree::Ident(ident)) => ident.to_string(),
122                Some(_) => panic!("Expected Ident or end"),
123                None => break,
124            };
125
126            if seen_keys.contains(&key) {
127                panic!("Duplicated key \"{key}\". Keys can only be specified once.");
128            }
129
130            assert_eq!(expect_punct(it), ':');
131
132            match key.as_str() {
133                "type" => info.type_ = expect_ident(it),
134                "name" => info.name = expect_string_ascii(it),
135                "author" => info.author = Some(expect_string(it)),
136                "description" => info.description = Some(expect_string(it)),
137                "license" => info.license = expect_string_ascii(it),
138                "alias" => info.alias = Some(expect_string_array(it)),
139                "firmware" => info.firmware = Some(expect_string_array(it)),
140                _ => panic!("Unknown key \"{key}\". Valid keys are: {EXPECTED_KEYS:?}."),
141            }
142
143            assert_eq!(expect_punct(it), ',');
144
145            seen_keys.push(key);
146        }
147
148        expect_end(it);
149
150        for key in REQUIRED_KEYS {
151            if !seen_keys.iter().any(|e| e == key) {
152                panic!("Missing required key \"{key}\".");
153            }
154        }
155
156        let mut ordered_keys: Vec<&str> = Vec::new();
157        for key in EXPECTED_KEYS {
158            if seen_keys.iter().any(|e| e == key) {
159                ordered_keys.push(key);
160            }
161        }
162
163        if seen_keys != ordered_keys {
164            panic!("Keys are not ordered as expected. Order them like: {ordered_keys:?}.");
165        }
166
167        info
168    }
169}
170
171pub(crate) fn module(ts: TokenStream) -> TokenStream {
172    let mut it = ts.into_iter();
173
174    let info = ModuleInfo::parse(&mut it);
175
176    let mut modinfo = ModInfoBuilder::new(info.name.as_ref());
177    if let Some(author) = info.author {
178        modinfo.emit("author", &author);
179    }
180    if let Some(description) = info.description {
181        modinfo.emit("description", &description);
182    }
183    modinfo.emit("license", &info.license);
184    if let Some(aliases) = info.alias {
185        for alias in aliases {
186            modinfo.emit("alias", &alias);
187        }
188    }
189    if let Some(firmware) = info.firmware {
190        for fw in firmware {
191            modinfo.emit("firmware", &fw);
192        }
193    }
194
195    // Built-in modules also export the `file` modinfo string.
196    let file =
197        std::env::var("RUST_MODFILE").expect("Unable to fetch RUST_MODFILE environmental variable");
198    modinfo.emit_only_builtin("file", &file);
199
200    format!(
201        "
202            /// The module name.
203            ///
204            /// Used by the printing macros, e.g. [`info!`].
205            const __LOG_PREFIX: &[u8] = b\"{name}\\0\";
206
207            // SAFETY: `__this_module` is constructed by the kernel at load time and will not be
208            // freed until the module is unloaded.
209            #[cfg(MODULE)]
210            static THIS_MODULE: kernel::ThisModule = unsafe {{
211                extern \"C\" {{
212                    static __this_module: kernel::types::Opaque<kernel::bindings::module>;
213                }}
214
215                kernel::ThisModule::from_ptr(__this_module.get())
216            }};
217            #[cfg(not(MODULE))]
218            static THIS_MODULE: kernel::ThisModule = unsafe {{
219                kernel::ThisModule::from_ptr(core::ptr::null_mut())
220            }};
221
222            impl kernel::ModuleMetadata for {type_} {{
223                const NAME: &'static kernel::str::CStr = kernel::c_str!(\"{name}\");
224            }}
225
226            // Double nested modules, since then nobody can access the public items inside.
227            mod __module_init {{
228                mod __module_init {{
229                    use super::super::{type_};
230                    use kernel::init::PinInit;
231
232                    /// The \"Rust loadable module\" mark.
233                    //
234                    // This may be best done another way later on, e.g. as a new modinfo
235                    // key or a new section. For the moment, keep it simple.
236                    #[cfg(MODULE)]
237                    #[doc(hidden)]
238                    #[used]
239                    static __IS_RUST_MODULE: () = ();
240
241                    static mut __MOD: core::mem::MaybeUninit<{type_}> =
242                        core::mem::MaybeUninit::uninit();
243
244                    // Loadable modules need to export the `{{init,cleanup}}_module` identifiers.
245                    /// # Safety
246                    ///
247                    /// This function must not be called after module initialization, because it may be
248                    /// freed after that completes.
249                    #[cfg(MODULE)]
250                    #[doc(hidden)]
251                    #[no_mangle]
252                    #[link_section = \".init.text\"]
253                    pub unsafe extern \"C\" fn init_module() -> kernel::ffi::c_int {{
254                        // SAFETY: This function is inaccessible to the outside due to the double
255                        // module wrapping it. It is called exactly once by the C side via its
256                        // unique name.
257                        unsafe {{ __init() }}
258                    }}
259
260                    #[cfg(MODULE)]
261                    #[doc(hidden)]
262                    #[used]
263                    #[link_section = \".init.data\"]
264                    static __UNIQUE_ID___addressable_init_module: unsafe extern \"C\" fn() -> i32 = init_module;
265
266                    #[cfg(MODULE)]
267                    #[doc(hidden)]
268                    #[no_mangle]
269                    pub extern \"C\" fn cleanup_module() {{
270                        // SAFETY:
271                        // - This function is inaccessible to the outside due to the double
272                        //   module wrapping it. It is called exactly once by the C side via its
273                        //   unique name,
274                        // - furthermore it is only called after `init_module` has returned `0`
275                        //   (which delegates to `__init`).
276                        unsafe {{ __exit() }}
277                    }}
278
279                    #[cfg(MODULE)]
280                    #[doc(hidden)]
281                    #[used]
282                    #[link_section = \".exit.data\"]
283                    static __UNIQUE_ID___addressable_cleanup_module: extern \"C\" fn() = cleanup_module;
284
285                    // Built-in modules are initialized through an initcall pointer
286                    // and the identifiers need to be unique.
287                    #[cfg(not(MODULE))]
288                    #[cfg(not(CONFIG_HAVE_ARCH_PREL32_RELOCATIONS))]
289                    #[doc(hidden)]
290                    #[link_section = \"{initcall_section}\"]
291                    #[used]
292                    pub static __{name}_initcall: extern \"C\" fn() -> kernel::ffi::c_int = __{name}_init;
293
294                    #[cfg(not(MODULE))]
295                    #[cfg(CONFIG_HAVE_ARCH_PREL32_RELOCATIONS)]
296                    core::arch::global_asm!(
297                        r#\".section \"{initcall_section}\", \"a\"
298                        __{name}_initcall:
299                            .long   __{name}_init - .
300                            .previous
301                        \"#
302                    );
303
304                    #[cfg(not(MODULE))]
305                    #[doc(hidden)]
306                    #[no_mangle]
307                    pub extern \"C\" fn __{name}_init() -> kernel::ffi::c_int {{
308                        // SAFETY: This function is inaccessible to the outside due to the double
309                        // module wrapping it. It is called exactly once by the C side via its
310                        // placement above in the initcall section.
311                        unsafe {{ __init() }}
312                    }}
313
314                    #[cfg(not(MODULE))]
315                    #[doc(hidden)]
316                    #[no_mangle]
317                    pub extern \"C\" fn __{name}_exit() {{
318                        // SAFETY:
319                        // - This function is inaccessible to the outside due to the double
320                        //   module wrapping it. It is called exactly once by the C side via its
321                        //   unique name,
322                        // - furthermore it is only called after `__{name}_init` has returned `0`
323                        //   (which delegates to `__init`).
324                        unsafe {{ __exit() }}
325                    }}
326
327                    /// # Safety
328                    ///
329                    /// This function must only be called once.
330                    unsafe fn __init() -> kernel::ffi::c_int {{
331                        let initer =
332                            <{type_} as kernel::InPlaceModule>::init(&super::super::THIS_MODULE);
333                        // SAFETY: No data race, since `__MOD` can only be accessed by this module
334                        // and there only `__init` and `__exit` access it. These functions are only
335                        // called once and `__exit` cannot be called before or during `__init`.
336                        match unsafe {{ initer.__pinned_init(__MOD.as_mut_ptr()) }} {{
337                            Ok(m) => 0,
338                            Err(e) => e.to_errno(),
339                        }}
340                    }}
341
342                    /// # Safety
343                    ///
344                    /// This function must
345                    /// - only be called once,
346                    /// - be called after `__init` has been called and returned `0`.
347                    unsafe fn __exit() {{
348                        // SAFETY: No data race, since `__MOD` can only be accessed by this module
349                        // and there only `__init` and `__exit` access it. These functions are only
350                        // called once and `__init` was already called.
351                        unsafe {{
352                            // Invokes `drop()` on `__MOD`, which should be used for cleanup.
353                            __MOD.assume_init_drop();
354                        }}
355                    }}
356
357                    {modinfo}
358                }}
359            }}
360        ",
361        type_ = info.type_,
362        name = info.name,
363        modinfo = modinfo.buffer,
364        initcall_section = ".initcall6.init"
365    )
366    .parse()
367    .expect("Error parsing formatted string into token stream.")
368}