1 // SPDX-License-Identifier: GPL-2.0
4 use proc_macro::{token_stream, Delimiter, Literal, TokenStream, TokenTree};
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();
13 while let Some(val) = try_string(&mut it) {
14 assert!(val.is_ascii(), "Expected ASCII string");
17 Some(TokenTree::Punct(punct)) => assert_eq!(punct.as_char(), ','),
19 _ => panic!("Expected ',' or end of array"),
25 struct ModInfoBuilder<'a> {
31 impl<'a> ModInfoBuilder<'a> {
32 fn new(module: &'a str) -> Self {
36 buffer: String::new(),
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.`.
44 "{module}.{field}={content}\0",
50 // Loadable modules' modinfo strings go as-is.
51 format!("{field}={content}\0", field = field, content = content)
59 #[link_section = \".modinfo\"]
61 pub static __{module}_{counter}: [u8; {length}] = *{string};
68 module = self.module.to_uppercase(),
69 counter = self.counter,
70 length = string.len(),
71 string = Literal::byte_string(string.as_bytes()),
78 fn emit_only_builtin(&mut self, field: &str, content: &str) {
79 self.emit_base(field, content, true)
82 fn emit_only_loadable(&mut self, field: &str, content: &str) {
83 self.emit_base(field, content, false)
86 fn emit(&mut self, field: &str, content: &str) {
87 self.emit_only_builtin(field, content);
88 self.emit_only_loadable(field, content);
92 #[derive(Debug, Default)]
97 author: Option<String>,
98 description: Option<String>,
99 alias: Option<Vec<String>>,
100 firmware: Option<Vec<String>>,
104 fn parse(it: &mut token_stream::IntoIter) -> Self {
105 let mut info = ModuleInfo::default();
107 const EXPECTED_KEYS: &[&str] = &[
116 const REQUIRED_KEYS: &[&str] = &["type", "name", "license"];
117 let mut seen_keys = Vec::new();
120 let key = match it.next() {
121 Some(TokenTree::Ident(ident)) => ident.to_string(),
122 Some(_) => panic!("Expected Ident or end"),
126 if seen_keys.contains(&key) {
128 "Duplicated key \"{}\". Keys can only be specified once.",
133 assert_eq!(expect_punct(it), ':');
136 "type" => info.type_ = expect_ident(it),
137 "name" => info.name = expect_string_ascii(it),
138 "author" => info.author = Some(expect_string(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)),
144 "Unknown key \"{}\". Valid keys are: {:?}.",
149 assert_eq!(expect_punct(it), ',');
156 for key in REQUIRED_KEYS {
157 if !seen_keys.iter().any(|e| e == key) {
158 panic!("Missing required key \"{}\".", key);
162 let mut ordered_keys: Vec<&str> = Vec::new();
163 for key in EXPECTED_KEYS {
164 if seen_keys.iter().any(|e| e == key) {
165 ordered_keys.push(key);
169 if seen_keys != ordered_keys {
171 "Keys are not ordered as expected. Order them like: {:?}.",
180 pub(crate) fn module(ts: TokenStream) -> TokenStream {
181 let mut it = ts.into_iter();
183 let info = ModuleInfo::parse(&mut it);
185 let mut modinfo = ModInfoBuilder::new(info.name.as_ref());
186 if let Some(author) = info.author {
187 modinfo.emit("author", &author);
189 if let Some(description) = info.description {
190 modinfo.emit("description", &description);
192 modinfo.emit("license", &info.license);
193 if let Some(aliases) = info.alias {
194 for alias in aliases {
195 modinfo.emit("alias", &alias);
198 if let Some(firmware) = info.firmware {
200 modinfo.emit("firmware", &fw);
204 // Built-in modules also export the `file` modinfo string.
206 std::env::var("RUST_MODFILE").expect("Unable to fetch RUST_MODFILE environmental variable");
207 modinfo.emit_only_builtin("file", &file);
213 /// Used by the printing macros, e.g. [`info!`].
214 const __LOG_PREFIX: &[u8] = b\"{name}\\0\";
216 // SAFETY: `__this_module` is constructed by the kernel at load time and will not be
217 // freed until the module is unloaded.
219 static THIS_MODULE: kernel::ThisModule = unsafe {{
221 static __this_module: kernel::types::Opaque<kernel::bindings::module>;
224 kernel::ThisModule::from_ptr(__this_module.get())
227 static THIS_MODULE: kernel::ThisModule = unsafe {{
228 kernel::ThisModule::from_ptr(core::ptr::null_mut())
231 // Double nested modules, since then nobody can access the public items inside.
234 use super::super::{type_};
235 use kernel::init::PinInit;
237 /// The \"Rust loadable module\" mark.
239 // This may be best done another way later on, e.g. as a new modinfo
240 // key or a new section. For the moment, keep it simple.
244 static __IS_RUST_MODULE: () = ();
246 static mut __MOD: core::mem::MaybeUninit<{type_}> =
247 core::mem::MaybeUninit::uninit();
249 // Loadable modules need to export the `{{init,cleanup}}_module` identifiers.
252 /// This function must not be called after module initialization, because it may be
253 /// freed after that completes.
257 #[link_section = \".init.text\"]
258 pub unsafe extern \"C\" fn init_module() -> kernel::ffi::c_int {{
259 // SAFETY: This function is inaccessible to the outside due to the double
260 // module wrapping it. It is called exactly once by the C side via its
262 unsafe {{ __init() }}
268 #[link_section = \".init.data\"]
269 static __UNIQUE_ID___addressable_init_module: unsafe extern \"C\" fn() -> i32 = init_module;
274 pub extern \"C\" fn cleanup_module() {{
276 // - This function is inaccessible to the outside due to the double
277 // module wrapping it. It is called exactly once by the C side via its
279 // - furthermore it is only called after `init_module` has returned `0`
280 // (which delegates to `__init`).
281 unsafe {{ __exit() }}
287 #[link_section = \".exit.data\"]
288 static __UNIQUE_ID___addressable_cleanup_module: extern \"C\" fn() = cleanup_module;
290 // Built-in modules are initialized through an initcall pointer
291 // and the identifiers need to be unique.
293 #[cfg(not(CONFIG_HAVE_ARCH_PREL32_RELOCATIONS))]
295 #[link_section = \"{initcall_section}\"]
297 pub static __{name}_initcall: extern \"C\" fn() -> kernel::ffi::c_int = __{name}_init;
300 #[cfg(CONFIG_HAVE_ARCH_PREL32_RELOCATIONS)]
301 core::arch::global_asm!(
302 r#\".section \"{initcall_section}\", \"a\"
304 .long __{name}_init - .
312 pub extern \"C\" fn __{name}_init() -> kernel::ffi::c_int {{
313 // SAFETY: This function is inaccessible to the outside due to the double
314 // module wrapping it. It is called exactly once by the C side via its
315 // placement above in the initcall section.
316 unsafe {{ __init() }}
322 pub extern \"C\" fn __{name}_exit() {{
324 // - This function is inaccessible to the outside due to the double
325 // module wrapping it. It is called exactly once by the C side via its
327 // - furthermore it is only called after `__{name}_init` has returned `0`
328 // (which delegates to `__init`).
329 unsafe {{ __exit() }}
334 /// This function must only be called once.
335 unsafe fn __init() -> kernel::ffi::c_int {{
337 <{type_} as kernel::InPlaceModule>::init(&super::super::THIS_MODULE);
338 // SAFETY: No data race, since `__MOD` can only be accessed by this module
339 // and there only `__init` and `__exit` access it. These functions are only
340 // called once and `__exit` cannot be called before or during `__init`.
341 match unsafe {{ initer.__pinned_init(__MOD.as_mut_ptr()) }} {{
343 Err(e) => e.to_errno(),
349 /// This function must
350 /// - only be called once,
351 /// - be called after `__init` has been called and returned `0`.
352 unsafe fn __exit() {{
353 // SAFETY: No data race, since `__MOD` can only be accessed by this module
354 // and there only `__init` and `__exit` access it. These functions are only
355 // called once and `__init` was already called.
357 // Invokes `drop()` on `__MOD`, which should be used for cleanup.
358 __MOD.assume_init_drop();
368 modinfo = modinfo.buffer,
369 initcall_section = ".initcall6.init"
372 .expect("Error parsing formatted string into token stream.")