xref: /linux/rust/macros/module.rs (revision 416f99c3b16f582a3fc6d64a1f77f39d94b76de5)
1 // SPDX-License-Identifier: GPL-2.0
2 
3 use crate::helpers::*;
4 use proc_macro::{token_stream, Delimiter, Literal, TokenStream, TokenTree};
5 use std::fmt::Write;
6 
7 fn 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 
25 struct ModInfoBuilder<'a> {
26     module: &'a str,
27     counter: usize,
28     buffer: String,
29 }
30 
31 impl<'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(compiler)]
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)]
93 struct ModuleInfo {
94     type_: String,
95     license: String,
96     name: String,
97     authors: Option<Vec<String>>,
98     description: Option<String>,
99     alias: Option<Vec<String>>,
100     firmware: Option<Vec<String>>,
101     imports_ns: Option<Vec<String>>,
102 }
103 
104 impl 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             "authors",
112             "description",
113             "license",
114             "alias",
115             "firmware",
116             "imports_ns",
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                 "authors" => info.authors = Some(expect_string_array(it)),
138                 "description" => info.description = Some(expect_string(it)),
139                 "license" => info.license = expect_string_ascii(it),
140                 "alias" => info.alias = Some(expect_string_array(it)),
141                 "firmware" => info.firmware = Some(expect_string_array(it)),
142                 "imports_ns" => info.imports_ns = 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 
174 pub(crate) fn module(ts: TokenStream) -> TokenStream {
175     let mut it = ts.into_iter();
176 
177     let info = ModuleInfo::parse(&mut it);
178 
179     // Rust does not allow hyphens in identifiers, use underscore instead.
180     let ident = info.name.replace('-', "_");
181     let mut modinfo = ModInfoBuilder::new(ident.as_ref());
182     if let Some(authors) = info.authors {
183         for author in authors {
184             modinfo.emit("author", &author);
185         }
186     }
187     if let Some(description) = info.description {
188         modinfo.emit("description", &description);
189     }
190     modinfo.emit("license", &info.license);
191     if let Some(aliases) = info.alias {
192         for alias in aliases {
193             modinfo.emit("alias", &alias);
194         }
195     }
196     if let Some(firmware) = info.firmware {
197         for fw in firmware {
198             modinfo.emit("firmware", &fw);
199         }
200     }
201     if let Some(imports) = info.imports_ns {
202         for ns in imports {
203             modinfo.emit("import_ns", &ns);
204         }
205     }
206 
207     // Built-in modules also export the `file` modinfo string.
208     let file =
209         std::env::var("RUST_MODFILE").expect("Unable to fetch RUST_MODFILE environmental variable");
210     modinfo.emit_only_builtin("file", &file);
211 
212     format!(
213         "
214             /// The module name.
215             ///
216             /// Used by the printing macros, e.g. [`info!`].
217             const __LOG_PREFIX: &[u8] = b\"{name}\\0\";
218 
219             // SAFETY: `__this_module` is constructed by the kernel at load time and will not be
220             // freed until the module is unloaded.
221             #[cfg(MODULE)]
222             static THIS_MODULE: ::kernel::ThisModule = unsafe {{
223                 extern \"C\" {{
224                     static __this_module: ::kernel::types::Opaque<::kernel::bindings::module>;
225                 }}
226 
227                 ::kernel::ThisModule::from_ptr(__this_module.get())
228             }};
229             #[cfg(not(MODULE))]
230             static THIS_MODULE: ::kernel::ThisModule = unsafe {{
231                 ::kernel::ThisModule::from_ptr(::core::ptr::null_mut())
232             }};
233 
234             /// The `LocalModule` type is the type of the module created by `module!`,
235             /// `module_pci_driver!`, `module_platform_driver!`, etc.
236             type LocalModule = {type_};
237 
238             impl ::kernel::ModuleMetadata for {type_} {{
239                 const NAME: &'static ::kernel::str::CStr = c\"{name}\";
240             }}
241 
242             // Double nested modules, since then nobody can access the public items inside.
243             mod __module_init {{
244                 mod __module_init {{
245                     use super::super::{type_};
246                     use pin_init::PinInit;
247 
248                     /// The \"Rust loadable module\" mark.
249                     //
250                     // This may be best done another way later on, e.g. as a new modinfo
251                     // key or a new section. For the moment, keep it simple.
252                     #[cfg(MODULE)]
253                     #[doc(hidden)]
254                     #[used(compiler)]
255                     static __IS_RUST_MODULE: () = ();
256 
257                     static mut __MOD: ::core::mem::MaybeUninit<{type_}> =
258                         ::core::mem::MaybeUninit::uninit();
259 
260                     // Loadable modules need to export the `{{init,cleanup}}_module` identifiers.
261                     /// # Safety
262                     ///
263                     /// This function must not be called after module initialization, because it may be
264                     /// freed after that completes.
265                     #[cfg(MODULE)]
266                     #[doc(hidden)]
267                     #[no_mangle]
268                     #[link_section = \".init.text\"]
269                     pub unsafe extern \"C\" fn init_module() -> ::kernel::ffi::c_int {{
270                         // SAFETY: This function is inaccessible to the outside due to the double
271                         // module wrapping it. It is called exactly once by the C side via its
272                         // unique name.
273                         unsafe {{ __init() }}
274                     }}
275 
276                     #[cfg(MODULE)]
277                     #[doc(hidden)]
278                     #[used(compiler)]
279                     #[link_section = \".init.data\"]
280                     static __UNIQUE_ID___addressable_init_module: unsafe extern \"C\" fn() -> i32 = init_module;
281 
282                     #[cfg(MODULE)]
283                     #[doc(hidden)]
284                     #[no_mangle]
285                     #[link_section = \".exit.text\"]
286                     pub extern \"C\" fn cleanup_module() {{
287                         // SAFETY:
288                         // - This function is inaccessible to the outside due to the double
289                         //   module wrapping it. It is called exactly once by the C side via its
290                         //   unique name,
291                         // - furthermore it is only called after `init_module` has returned `0`
292                         //   (which delegates to `__init`).
293                         unsafe {{ __exit() }}
294                     }}
295 
296                     #[cfg(MODULE)]
297                     #[doc(hidden)]
298                     #[used(compiler)]
299                     #[link_section = \".exit.data\"]
300                     static __UNIQUE_ID___addressable_cleanup_module: extern \"C\" fn() = cleanup_module;
301 
302                     // Built-in modules are initialized through an initcall pointer
303                     // and the identifiers need to be unique.
304                     #[cfg(not(MODULE))]
305                     #[cfg(not(CONFIG_HAVE_ARCH_PREL32_RELOCATIONS))]
306                     #[doc(hidden)]
307                     #[link_section = \"{initcall_section}\"]
308                     #[used(compiler)]
309                     pub static __{ident}_initcall: extern \"C\" fn() ->
310                         ::kernel::ffi::c_int = __{ident}_init;
311 
312                     #[cfg(not(MODULE))]
313                     #[cfg(CONFIG_HAVE_ARCH_PREL32_RELOCATIONS)]
314                     ::core::arch::global_asm!(
315                         r#\".section \"{initcall_section}\", \"a\"
316                         __{ident}_initcall:
317                             .long   __{ident}_init - .
318                             .previous
319                         \"#
320                     );
321 
322                     #[cfg(not(MODULE))]
323                     #[doc(hidden)]
324                     #[no_mangle]
325                     pub extern \"C\" fn __{ident}_init() -> ::kernel::ffi::c_int {{
326                         // SAFETY: This function is inaccessible to the outside due to the double
327                         // module wrapping it. It is called exactly once by the C side via its
328                         // placement above in the initcall section.
329                         unsafe {{ __init() }}
330                     }}
331 
332                     #[cfg(not(MODULE))]
333                     #[doc(hidden)]
334                     #[no_mangle]
335                     pub extern \"C\" fn __{ident}_exit() {{
336                         // SAFETY:
337                         // - This function is inaccessible to the outside due to the double
338                         //   module wrapping it. It is called exactly once by the C side via its
339                         //   unique name,
340                         // - furthermore it is only called after `__{ident}_init` has
341                         //   returned `0` (which delegates to `__init`).
342                         unsafe {{ __exit() }}
343                     }}
344 
345                     /// # Safety
346                     ///
347                     /// This function must only be called once.
348                     unsafe fn __init() -> ::kernel::ffi::c_int {{
349                         let initer =
350                             <{type_} as ::kernel::InPlaceModule>::init(&super::super::THIS_MODULE);
351                         // SAFETY: No data race, since `__MOD` can only be accessed by this module
352                         // and there only `__init` and `__exit` access it. These functions are only
353                         // called once and `__exit` cannot be called before or during `__init`.
354                         match unsafe {{ initer.__pinned_init(__MOD.as_mut_ptr()) }} {{
355                             Ok(m) => 0,
356                             Err(e) => e.to_errno(),
357                         }}
358                     }}
359 
360                     /// # Safety
361                     ///
362                     /// This function must
363                     /// - only be called once,
364                     /// - be called after `__init` has been called and returned `0`.
365                     unsafe fn __exit() {{
366                         // SAFETY: No data race, since `__MOD` can only be accessed by this module
367                         // and there only `__init` and `__exit` access it. These functions are only
368                         // called once and `__init` was already called.
369                         unsafe {{
370                             // Invokes `drop()` on `__MOD`, which should be used for cleanup.
371                             __MOD.assume_init_drop();
372                         }}
373                     }}
374 
375                     {modinfo}
376                 }}
377             }}
378         ",
379         type_ = info.type_,
380         name = info.name,
381         ident = ident,
382         modinfo = modinfo.buffer,
383         initcall_section = ".initcall6.init"
384     )
385     .parse()
386     .expect("Error parsing formatted string into token stream.")
387 }
388