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                #[cfg_attr(not(target_os = \"macos\"), 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    authors: Option<Vec<String>>,
99    description: Option<String>,
100    alias: Option<Vec<String>>,
101    firmware: Option<Vec<String>>,
102}
103
104impl ModuleInfo {
105    fn parse(it: &mut token_stream::IntoIter) -> Self {
106        let mut info = ModuleInfo::default();
107
108        const EXPECTED_KEYS: &[&str] = &[
109            "type",
110            "name",
111            "author",
112            "authors",
113            "description",
114            "license",
115            "alias",
116            "firmware",
117        ];
118        const REQUIRED_KEYS: &[&str] = &["type", "name", "license"];
119        let mut seen_keys = Vec::new();
120
121        loop {
122            let key = match it.next() {
123                Some(TokenTree::Ident(ident)) => ident.to_string(),
124                Some(_) => panic!("Expected Ident or end"),
125                None => break,
126            };
127
128            if seen_keys.contains(&key) {
129                panic!("Duplicated key \"{key}\". Keys can only be specified once.");
130            }
131
132            assert_eq!(expect_punct(it), ':');
133
134            match key.as_str() {
135                "type" => info.type_ = expect_ident(it),
136                "name" => info.name = expect_string_ascii(it),
137                "author" => info.author = Some(expect_string(it)),
138                "authors" => info.authors = Some(expect_string_array(it)),
139                "description" => info.description = Some(expect_string(it)),
140                "license" => info.license = expect_string_ascii(it),
141                "alias" => info.alias = Some(expect_string_array(it)),
142                "firmware" => info.firmware = Some(expect_string_array(it)),
143                _ => panic!("Unknown key \"{key}\". Valid keys are: {EXPECTED_KEYS:?}."),
144            }
145
146            assert_eq!(expect_punct(it), ',');
147
148            seen_keys.push(key);
149        }
150
151        expect_end(it);
152
153        for key in REQUIRED_KEYS {
154            if !seen_keys.iter().any(|e| e == key) {
155                panic!("Missing required key \"{key}\".");
156            }
157        }
158
159        let mut ordered_keys: Vec<&str> = Vec::new();
160        for key in EXPECTED_KEYS {
161            if seen_keys.iter().any(|e| e == key) {
162                ordered_keys.push(key);
163            }
164        }
165
166        if seen_keys != ordered_keys {
167            panic!("Keys are not ordered as expected. Order them like: {ordered_keys:?}.");
168        }
169
170        info
171    }
172}
173
174pub(crate) fn module(ts: TokenStream) -> TokenStream {
175    let mut it = ts.into_iter();
176
177    let info = ModuleInfo::parse(&mut it);
178
179    let mut modinfo = ModInfoBuilder::new(info.name.as_ref());
180    if let Some(author) = info.author {
181        modinfo.emit("author", &author);
182    }
183    if let Some(authors) = info.authors {
184        for author in authors {
185            modinfo.emit("author", &author);
186        }
187    }
188    if let Some(description) = info.description {
189        modinfo.emit("description", &description);
190    }
191    modinfo.emit("license", &info.license);
192    if let Some(aliases) = info.alias {
193        for alias in aliases {
194            modinfo.emit("alias", &alias);
195        }
196    }
197    if let Some(firmware) = info.firmware {
198        for fw in firmware {
199            modinfo.emit("firmware", &fw);
200        }
201    }
202
203    // Built-in modules also export the `file` modinfo string.
204    let file =
205        std::env::var("RUST_MODFILE").expect("Unable to fetch RUST_MODFILE environmental variable");
206    modinfo.emit_only_builtin("file", &file);
207
208    format!(
209        "
210            /// The module name.
211            ///
212            /// Used by the printing macros, e.g. [`info!`].
213            const __LOG_PREFIX: &[u8] = b\"{name}\\0\";
214
215            // SAFETY: `__this_module` is constructed by the kernel at load time and will not be
216            // freed until the module is unloaded.
217            #[cfg(MODULE)]
218            static THIS_MODULE: kernel::ThisModule = unsafe {{
219                extern \"C\" {{
220                    static __this_module: kernel::types::Opaque<kernel::bindings::module>;
221                }}
222
223                kernel::ThisModule::from_ptr(__this_module.get())
224            }};
225            #[cfg(not(MODULE))]
226            static THIS_MODULE: kernel::ThisModule = unsafe {{
227                kernel::ThisModule::from_ptr(core::ptr::null_mut())
228            }};
229
230            /// The `LocalModule` type is the type of the module created by `module!`,
231            /// `module_pci_driver!`, `module_platform_driver!`, etc.
232            type LocalModule = {type_};
233
234            impl kernel::ModuleMetadata for {type_} {{
235                const NAME: &'static kernel::str::CStr = kernel::c_str!(\"{name}\");
236            }}
237
238            // Double nested modules, since then nobody can access the public items inside.
239            mod __module_init {{
240                mod __module_init {{
241                    use super::super::{type_};
242                    use pin_init::PinInit;
243
244                    /// The \"Rust loadable module\" mark.
245                    //
246                    // This may be best done another way later on, e.g. as a new modinfo
247                    // key or a new section. For the moment, keep it simple.
248                    #[cfg(MODULE)]
249                    #[doc(hidden)]
250                    #[used]
251                    static __IS_RUST_MODULE: () = ();
252
253                    static mut __MOD: core::mem::MaybeUninit<{type_}> =
254                        core::mem::MaybeUninit::uninit();
255
256                    // Loadable modules need to export the `{{init,cleanup}}_module` identifiers.
257                    /// # Safety
258                    ///
259                    /// This function must not be called after module initialization, because it may be
260                    /// freed after that completes.
261                    #[cfg(MODULE)]
262                    #[doc(hidden)]
263                    #[no_mangle]
264                    #[link_section = \".init.text\"]
265                    pub unsafe extern \"C\" fn init_module() -> kernel::ffi::c_int {{
266                        // SAFETY: This function is inaccessible to the outside due to the double
267                        // module wrapping it. It is called exactly once by the C side via its
268                        // unique name.
269                        unsafe {{ __init() }}
270                    }}
271
272                    #[cfg(MODULE)]
273                    #[doc(hidden)]
274                    #[used]
275                    #[link_section = \".init.data\"]
276                    static __UNIQUE_ID___addressable_init_module: unsafe extern \"C\" fn() -> i32 = init_module;
277
278                    #[cfg(MODULE)]
279                    #[doc(hidden)]
280                    #[no_mangle]
281                    pub extern \"C\" fn cleanup_module() {{
282                        // SAFETY:
283                        // - This function is inaccessible to the outside due to the double
284                        //   module wrapping it. It is called exactly once by the C side via its
285                        //   unique name,
286                        // - furthermore it is only called after `init_module` has returned `0`
287                        //   (which delegates to `__init`).
288                        unsafe {{ __exit() }}
289                    }}
290
291                    #[cfg(MODULE)]
292                    #[doc(hidden)]
293                    #[used]
294                    #[link_section = \".exit.data\"]
295                    static __UNIQUE_ID___addressable_cleanup_module: extern \"C\" fn() = cleanup_module;
296
297                    // Built-in modules are initialized through an initcall pointer
298                    // and the identifiers need to be unique.
299                    #[cfg(not(MODULE))]
300                    #[cfg(not(CONFIG_HAVE_ARCH_PREL32_RELOCATIONS))]
301                    #[doc(hidden)]
302                    #[link_section = \"{initcall_section}\"]
303                    #[used]
304                    pub static __{name}_initcall: extern \"C\" fn() -> kernel::ffi::c_int = __{name}_init;
305
306                    #[cfg(not(MODULE))]
307                    #[cfg(CONFIG_HAVE_ARCH_PREL32_RELOCATIONS)]
308                    core::arch::global_asm!(
309                        r#\".section \"{initcall_section}\", \"a\"
310                        __{name}_initcall:
311                            .long   __{name}_init - .
312                            .previous
313                        \"#
314                    );
315
316                    #[cfg(not(MODULE))]
317                    #[doc(hidden)]
318                    #[no_mangle]
319                    pub extern \"C\" fn __{name}_init() -> kernel::ffi::c_int {{
320                        // SAFETY: This function is inaccessible to the outside due to the double
321                        // module wrapping it. It is called exactly once by the C side via its
322                        // placement above in the initcall section.
323                        unsafe {{ __init() }}
324                    }}
325
326                    #[cfg(not(MODULE))]
327                    #[doc(hidden)]
328                    #[no_mangle]
329                    pub extern \"C\" fn __{name}_exit() {{
330                        // SAFETY:
331                        // - This function is inaccessible to the outside due to the double
332                        //   module wrapping it. It is called exactly once by the C side via its
333                        //   unique name,
334                        // - furthermore it is only called after `__{name}_init` has returned `0`
335                        //   (which delegates to `__init`).
336                        unsafe {{ __exit() }}
337                    }}
338
339                    /// # Safety
340                    ///
341                    /// This function must only be called once.
342                    unsafe fn __init() -> kernel::ffi::c_int {{
343                        let initer =
344                            <{type_} as kernel::InPlaceModule>::init(&super::super::THIS_MODULE);
345                        // SAFETY: No data race, since `__MOD` can only be accessed by this module
346                        // and there only `__init` and `__exit` access it. These functions are only
347                        // called once and `__exit` cannot be called before or during `__init`.
348                        match unsafe {{ initer.__pinned_init(__MOD.as_mut_ptr()) }} {{
349                            Ok(m) => 0,
350                            Err(e) => e.to_errno(),
351                        }}
352                    }}
353
354                    /// # Safety
355                    ///
356                    /// This function must
357                    /// - only be called once,
358                    /// - be called after `__init` has been called and returned `0`.
359                    unsafe fn __exit() {{
360                        // SAFETY: No data race, since `__MOD` can only be accessed by this module
361                        // and there only `__init` and `__exit` access it. These functions are only
362                        // called once and `__init` was already called.
363                        unsafe {{
364                            // Invokes `drop()` on `__MOD`, which should be used for cleanup.
365                            __MOD.assume_init_drop();
366                        }}
367                    }}
368
369                    {modinfo}
370                }}
371            }}
372        ",
373        type_ = info.type_,
374        name = info.name,
375        modinfo = modinfo.buffer,
376        initcall_section = ".initcall6.init"
377    )
378    .parse()
379    .expect("Error parsing formatted string into token stream.")
380}