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