11fbde52bSMiguel Ojeda // SPDX-License-Identifier: GPL-2.0
21fbde52bSMiguel Ojeda
31fbde52bSMiguel Ojeda use crate::helpers::*;
439867fecSAsahi Lina use proc_macro::{token_stream, Delimiter, Literal, TokenStream, TokenTree};
51fbde52bSMiguel Ojeda use std::fmt::Write;
61fbde52bSMiguel Ojeda
expect_string_array(it: &mut token_stream::IntoIter) -> Vec<String>739867fecSAsahi Lina fn expect_string_array(it: &mut token_stream::IntoIter) -> Vec<String> {
839867fecSAsahi Lina let group = expect_group(it);
939867fecSAsahi Lina assert_eq!(group.delimiter(), Delimiter::Bracket);
1039867fecSAsahi Lina let mut values = Vec::new();
1139867fecSAsahi Lina let mut it = group.stream().into_iter();
1239867fecSAsahi Lina
1339867fecSAsahi Lina while let Some(val) = try_string(&mut it) {
1439867fecSAsahi Lina assert!(val.is_ascii(), "Expected ASCII string");
1539867fecSAsahi Lina values.push(val);
1639867fecSAsahi Lina match it.next() {
1739867fecSAsahi Lina Some(TokenTree::Punct(punct)) => assert_eq!(punct.as_char(), ','),
1839867fecSAsahi Lina None => break,
1939867fecSAsahi Lina _ => panic!("Expected ',' or end of array"),
2039867fecSAsahi Lina }
2139867fecSAsahi Lina }
2239867fecSAsahi Lina values
2339867fecSAsahi Lina }
2439867fecSAsahi Lina
251fbde52bSMiguel Ojeda struct ModInfoBuilder<'a> {
261fbde52bSMiguel Ojeda module: &'a str,
271fbde52bSMiguel Ojeda counter: usize,
281fbde52bSMiguel Ojeda buffer: String,
291fbde52bSMiguel Ojeda }
301fbde52bSMiguel Ojeda
311fbde52bSMiguel Ojeda impl<'a> ModInfoBuilder<'a> {
new(module: &'a str) -> Self321fbde52bSMiguel Ojeda fn new(module: &'a str) -> Self {
331fbde52bSMiguel Ojeda ModInfoBuilder {
341fbde52bSMiguel Ojeda module,
351fbde52bSMiguel Ojeda counter: 0,
361fbde52bSMiguel Ojeda buffer: String::new(),
371fbde52bSMiguel Ojeda }
381fbde52bSMiguel Ojeda }
391fbde52bSMiguel Ojeda
emit_base(&mut self, field: &str, content: &str, builtin: bool)401fbde52bSMiguel Ojeda fn emit_base(&mut self, field: &str, content: &str, builtin: bool) {
411fbde52bSMiguel Ojeda let string = if builtin {
421fbde52bSMiguel Ojeda // Built-in modules prefix their modinfo strings by `module.`.
431fbde52bSMiguel Ojeda format!(
441fbde52bSMiguel Ojeda "{module}.{field}={content}\0",
451fbde52bSMiguel Ojeda module = self.module,
461fbde52bSMiguel Ojeda field = field,
471fbde52bSMiguel Ojeda content = content
481fbde52bSMiguel Ojeda )
491fbde52bSMiguel Ojeda } else {
501fbde52bSMiguel Ojeda // Loadable modules' modinfo strings go as-is.
511fbde52bSMiguel Ojeda format!("{field}={content}\0", field = field, content = content)
521fbde52bSMiguel Ojeda };
531fbde52bSMiguel Ojeda
541fbde52bSMiguel Ojeda write!(
551fbde52bSMiguel Ojeda &mut self.buffer,
561fbde52bSMiguel Ojeda "
571fbde52bSMiguel Ojeda {cfg}
581fbde52bSMiguel Ojeda #[doc(hidden)]
591fbde52bSMiguel Ojeda #[link_section = \".modinfo\"]
601fbde52bSMiguel Ojeda #[used]
611fbde52bSMiguel Ojeda pub static __{module}_{counter}: [u8; {length}] = *{string};
621fbde52bSMiguel Ojeda ",
631fbde52bSMiguel Ojeda cfg = if builtin {
641fbde52bSMiguel Ojeda "#[cfg(not(MODULE))]"
651fbde52bSMiguel Ojeda } else {
661fbde52bSMiguel Ojeda "#[cfg(MODULE)]"
671fbde52bSMiguel Ojeda },
681fbde52bSMiguel Ojeda module = self.module.to_uppercase(),
691fbde52bSMiguel Ojeda counter = self.counter,
701fbde52bSMiguel Ojeda length = string.len(),
711fbde52bSMiguel Ojeda string = Literal::byte_string(string.as_bytes()),
721fbde52bSMiguel Ojeda )
731fbde52bSMiguel Ojeda .unwrap();
741fbde52bSMiguel Ojeda
751fbde52bSMiguel Ojeda self.counter += 1;
761fbde52bSMiguel Ojeda }
771fbde52bSMiguel Ojeda
emit_only_builtin(&mut self, field: &str, content: &str)781fbde52bSMiguel Ojeda fn emit_only_builtin(&mut self, field: &str, content: &str) {
791fbde52bSMiguel Ojeda self.emit_base(field, content, true)
801fbde52bSMiguel Ojeda }
811fbde52bSMiguel Ojeda
emit_only_loadable(&mut self, field: &str, content: &str)821fbde52bSMiguel Ojeda fn emit_only_loadable(&mut self, field: &str, content: &str) {
831fbde52bSMiguel Ojeda self.emit_base(field, content, false)
841fbde52bSMiguel Ojeda }
851fbde52bSMiguel Ojeda
emit(&mut self, field: &str, content: &str)861fbde52bSMiguel Ojeda fn emit(&mut self, field: &str, content: &str) {
871fbde52bSMiguel Ojeda self.emit_only_builtin(field, content);
881fbde52bSMiguel Ojeda self.emit_only_loadable(field, content);
891fbde52bSMiguel Ojeda }
901fbde52bSMiguel Ojeda }
911fbde52bSMiguel Ojeda
921fbde52bSMiguel Ojeda #[derive(Debug, Default)]
931fbde52bSMiguel Ojeda struct ModuleInfo {
941fbde52bSMiguel Ojeda type_: String,
951fbde52bSMiguel Ojeda license: String,
961fbde52bSMiguel Ojeda name: String,
971fbde52bSMiguel Ojeda author: Option<String>,
981fbde52bSMiguel Ojeda description: Option<String>,
9939867fecSAsahi Lina alias: Option<Vec<String>>,
1001fbde52bSMiguel Ojeda }
1011fbde52bSMiguel Ojeda
1021fbde52bSMiguel Ojeda impl ModuleInfo {
parse(it: &mut token_stream::IntoIter) -> Self1031fbde52bSMiguel Ojeda fn parse(it: &mut token_stream::IntoIter) -> Self {
1041fbde52bSMiguel Ojeda let mut info = ModuleInfo::default();
1051fbde52bSMiguel Ojeda
1061fbde52bSMiguel Ojeda const EXPECTED_KEYS: &[&str] =
1071fbde52bSMiguel Ojeda &["type", "name", "author", "description", "license", "alias"];
1081fbde52bSMiguel Ojeda const REQUIRED_KEYS: &[&str] = &["type", "name", "license"];
1091fbde52bSMiguel Ojeda let mut seen_keys = Vec::new();
1101fbde52bSMiguel Ojeda
1111fbde52bSMiguel Ojeda loop {
1121fbde52bSMiguel Ojeda let key = match it.next() {
1131fbde52bSMiguel Ojeda Some(TokenTree::Ident(ident)) => ident.to_string(),
1141fbde52bSMiguel Ojeda Some(_) => panic!("Expected Ident or end"),
1151fbde52bSMiguel Ojeda None => break,
1161fbde52bSMiguel Ojeda };
1171fbde52bSMiguel Ojeda
1181fbde52bSMiguel Ojeda if seen_keys.contains(&key) {
1191fbde52bSMiguel Ojeda panic!(
1201fbde52bSMiguel Ojeda "Duplicated key \"{}\". Keys can only be specified once.",
1211fbde52bSMiguel Ojeda key
1221fbde52bSMiguel Ojeda );
1231fbde52bSMiguel Ojeda }
1241fbde52bSMiguel Ojeda
1251fbde52bSMiguel Ojeda assert_eq!(expect_punct(it), ':');
1261fbde52bSMiguel Ojeda
1271fbde52bSMiguel Ojeda match key.as_str() {
1281fbde52bSMiguel Ojeda "type" => info.type_ = expect_ident(it),
129b13c9880SGary Guo "name" => info.name = expect_string_ascii(it),
130b13c9880SGary Guo "author" => info.author = Some(expect_string(it)),
131b13c9880SGary Guo "description" => info.description = Some(expect_string(it)),
132b13c9880SGary Guo "license" => info.license = expect_string_ascii(it),
13339867fecSAsahi Lina "alias" => info.alias = Some(expect_string_array(it)),
1341fbde52bSMiguel Ojeda _ => panic!(
1351fbde52bSMiguel Ojeda "Unknown key \"{}\". Valid keys are: {:?}.",
1361fbde52bSMiguel Ojeda key, EXPECTED_KEYS
1371fbde52bSMiguel Ojeda ),
1381fbde52bSMiguel Ojeda }
1391fbde52bSMiguel Ojeda
1401fbde52bSMiguel Ojeda assert_eq!(expect_punct(it), ',');
1411fbde52bSMiguel Ojeda
1421fbde52bSMiguel Ojeda seen_keys.push(key);
1431fbde52bSMiguel Ojeda }
1441fbde52bSMiguel Ojeda
1451fbde52bSMiguel Ojeda expect_end(it);
1461fbde52bSMiguel Ojeda
1471fbde52bSMiguel Ojeda for key in REQUIRED_KEYS {
1481fbde52bSMiguel Ojeda if !seen_keys.iter().any(|e| e == key) {
1491fbde52bSMiguel Ojeda panic!("Missing required key \"{}\".", key);
1501fbde52bSMiguel Ojeda }
1511fbde52bSMiguel Ojeda }
1521fbde52bSMiguel Ojeda
1531fbde52bSMiguel Ojeda let mut ordered_keys: Vec<&str> = Vec::new();
1541fbde52bSMiguel Ojeda for key in EXPECTED_KEYS {
1551fbde52bSMiguel Ojeda if seen_keys.iter().any(|e| e == key) {
1561fbde52bSMiguel Ojeda ordered_keys.push(key);
1571fbde52bSMiguel Ojeda }
1581fbde52bSMiguel Ojeda }
1591fbde52bSMiguel Ojeda
1601fbde52bSMiguel Ojeda if seen_keys != ordered_keys {
1611fbde52bSMiguel Ojeda panic!(
1621fbde52bSMiguel Ojeda "Keys are not ordered as expected. Order them like: {:?}.",
1631fbde52bSMiguel Ojeda ordered_keys
1641fbde52bSMiguel Ojeda );
1651fbde52bSMiguel Ojeda }
1661fbde52bSMiguel Ojeda
1671fbde52bSMiguel Ojeda info
1681fbde52bSMiguel Ojeda }
1691fbde52bSMiguel Ojeda }
1701fbde52bSMiguel Ojeda
module(ts: TokenStream) -> TokenStream1711fbde52bSMiguel Ojeda pub(crate) fn module(ts: TokenStream) -> TokenStream {
1721fbde52bSMiguel Ojeda let mut it = ts.into_iter();
1731fbde52bSMiguel Ojeda
1741fbde52bSMiguel Ojeda let info = ModuleInfo::parse(&mut it);
1751fbde52bSMiguel Ojeda
1761fbde52bSMiguel Ojeda let mut modinfo = ModInfoBuilder::new(info.name.as_ref());
1771fbde52bSMiguel Ojeda if let Some(author) = info.author {
1781fbde52bSMiguel Ojeda modinfo.emit("author", &author);
1791fbde52bSMiguel Ojeda }
1801fbde52bSMiguel Ojeda if let Some(description) = info.description {
1811fbde52bSMiguel Ojeda modinfo.emit("description", &description);
1821fbde52bSMiguel Ojeda }
1831fbde52bSMiguel Ojeda modinfo.emit("license", &info.license);
18439867fecSAsahi Lina if let Some(aliases) = info.alias {
18539867fecSAsahi Lina for alias in aliases {
1861fbde52bSMiguel Ojeda modinfo.emit("alias", &alias);
1871fbde52bSMiguel Ojeda }
18839867fecSAsahi Lina }
1891fbde52bSMiguel Ojeda
1901fbde52bSMiguel Ojeda // Built-in modules also export the `file` modinfo string.
1911fbde52bSMiguel Ojeda let file =
1921fbde52bSMiguel Ojeda std::env::var("RUST_MODFILE").expect("Unable to fetch RUST_MODFILE environmental variable");
1931fbde52bSMiguel Ojeda modinfo.emit_only_builtin("file", &file);
1941fbde52bSMiguel Ojeda
1951fbde52bSMiguel Ojeda format!(
1961fbde52bSMiguel Ojeda "
1971fbde52bSMiguel Ojeda /// The module name.
1981fbde52bSMiguel Ojeda ///
1991fbde52bSMiguel Ojeda /// Used by the printing macros, e.g. [`info!`].
2001fbde52bSMiguel Ojeda const __LOG_PREFIX: &[u8] = b\"{name}\\0\";
2011fbde52bSMiguel Ojeda
2021fbde52bSMiguel Ojeda // SAFETY: `__this_module` is constructed by the kernel at load time and will not be
2031fbde52bSMiguel Ojeda // freed until the module is unloaded.
2041fbde52bSMiguel Ojeda #[cfg(MODULE)]
2051fbde52bSMiguel Ojeda static THIS_MODULE: kernel::ThisModule = unsafe {{
206*f9275893SBoqun Feng extern \"C\" {{
207*f9275893SBoqun Feng static __this_module: kernel::types::Opaque<kernel::bindings::module>;
208*f9275893SBoqun Feng }}
209*f9275893SBoqun Feng
210*f9275893SBoqun Feng kernel::ThisModule::from_ptr(__this_module.get())
2111fbde52bSMiguel Ojeda }};
2121fbde52bSMiguel Ojeda #[cfg(not(MODULE))]
2131fbde52bSMiguel Ojeda static THIS_MODULE: kernel::ThisModule = unsafe {{
2141fbde52bSMiguel Ojeda kernel::ThisModule::from_ptr(core::ptr::null_mut())
2151fbde52bSMiguel Ojeda }};
2161fbde52bSMiguel Ojeda
217a4dec33dSBenno Lossin // Double nested modules, since then nobody can access the public items inside.
218a4dec33dSBenno Lossin mod __module_init {{
219a4dec33dSBenno Lossin mod __module_init {{
220a4dec33dSBenno Lossin use super::super::{type_};
221a4dec33dSBenno Lossin
222a4dec33dSBenno Lossin /// The \"Rust loadable module\" mark.
223a4dec33dSBenno Lossin //
224a4dec33dSBenno Lossin // This may be best done another way later on, e.g. as a new modinfo
225a4dec33dSBenno Lossin // key or a new section. For the moment, keep it simple.
226a4dec33dSBenno Lossin #[cfg(MODULE)]
227a4dec33dSBenno Lossin #[doc(hidden)]
228a4dec33dSBenno Lossin #[used]
229a4dec33dSBenno Lossin static __IS_RUST_MODULE: () = ();
230a4dec33dSBenno Lossin
231a4dec33dSBenno Lossin static mut __MOD: Option<{type_}> = None;
232a4dec33dSBenno Lossin
2331fbde52bSMiguel Ojeda // Loadable modules need to export the `{{init,cleanup}}_module` identifiers.
234738ea6d9SThomas Bertschinger /// # Safety
235738ea6d9SThomas Bertschinger ///
236738ea6d9SThomas Bertschinger /// This function must not be called after module initialization, because it may be
237738ea6d9SThomas Bertschinger /// freed after that completes.
2381fbde52bSMiguel Ojeda #[cfg(MODULE)]
2391fbde52bSMiguel Ojeda #[doc(hidden)]
2401fbde52bSMiguel Ojeda #[no_mangle]
241738ea6d9SThomas Bertschinger #[link_section = \".init.text\"]
242738ea6d9SThomas Bertschinger pub unsafe extern \"C\" fn init_module() -> core::ffi::c_int {{
243a4dec33dSBenno Lossin // SAFETY: This function is inaccessible to the outside due to the double
244a4dec33dSBenno Lossin // module wrapping it. It is called exactly once by the C side via its
245a4dec33dSBenno Lossin // unique name.
246a4dec33dSBenno Lossin unsafe {{ __init() }}
2471fbde52bSMiguel Ojeda }}
2481fbde52bSMiguel Ojeda
2491fbde52bSMiguel Ojeda #[cfg(MODULE)]
2501fbde52bSMiguel Ojeda #[doc(hidden)]
2511fbde52bSMiguel Ojeda #[no_mangle]
2521fbde52bSMiguel Ojeda pub extern \"C\" fn cleanup_module() {{
253a4dec33dSBenno Lossin // SAFETY:
254a4dec33dSBenno Lossin // - This function is inaccessible to the outside due to the double
255a4dec33dSBenno Lossin // module wrapping it. It is called exactly once by the C side via its
256a4dec33dSBenno Lossin // unique name,
257a4dec33dSBenno Lossin // - furthermore it is only called after `init_module` has returned `0`
258a4dec33dSBenno Lossin // (which delegates to `__init`).
259a4dec33dSBenno Lossin unsafe {{ __exit() }}
2601fbde52bSMiguel Ojeda }}
2611fbde52bSMiguel Ojeda
2621fbde52bSMiguel Ojeda // Built-in modules are initialized through an initcall pointer
2631fbde52bSMiguel Ojeda // and the identifiers need to be unique.
2641fbde52bSMiguel Ojeda #[cfg(not(MODULE))]
2651fbde52bSMiguel Ojeda #[cfg(not(CONFIG_HAVE_ARCH_PREL32_RELOCATIONS))]
2661fbde52bSMiguel Ojeda #[doc(hidden)]
2671fbde52bSMiguel Ojeda #[link_section = \"{initcall_section}\"]
2681fbde52bSMiguel Ojeda #[used]
2691fbde52bSMiguel Ojeda pub static __{name}_initcall: extern \"C\" fn() -> core::ffi::c_int = __{name}_init;
2701fbde52bSMiguel Ojeda
2711fbde52bSMiguel Ojeda #[cfg(not(MODULE))]
2721fbde52bSMiguel Ojeda #[cfg(CONFIG_HAVE_ARCH_PREL32_RELOCATIONS)]
2731fbde52bSMiguel Ojeda core::arch::global_asm!(
2741fbde52bSMiguel Ojeda r#\".section \"{initcall_section}\", \"a\"
2751fbde52bSMiguel Ojeda __{name}_initcall:
2761fbde52bSMiguel Ojeda .long __{name}_init - .
2771fbde52bSMiguel Ojeda .previous
2781fbde52bSMiguel Ojeda \"#
2791fbde52bSMiguel Ojeda );
2801fbde52bSMiguel Ojeda
2811fbde52bSMiguel Ojeda #[cfg(not(MODULE))]
2821fbde52bSMiguel Ojeda #[doc(hidden)]
2831fbde52bSMiguel Ojeda #[no_mangle]
2841fbde52bSMiguel Ojeda pub extern \"C\" fn __{name}_init() -> core::ffi::c_int {{
285a4dec33dSBenno Lossin // SAFETY: This function is inaccessible to the outside due to the double
286a4dec33dSBenno Lossin // module wrapping it. It is called exactly once by the C side via its
287a4dec33dSBenno Lossin // placement above in the initcall section.
288a4dec33dSBenno Lossin unsafe {{ __init() }}
2891fbde52bSMiguel Ojeda }}
2901fbde52bSMiguel Ojeda
2911fbde52bSMiguel Ojeda #[cfg(not(MODULE))]
2921fbde52bSMiguel Ojeda #[doc(hidden)]
2931fbde52bSMiguel Ojeda #[no_mangle]
2941fbde52bSMiguel Ojeda pub extern \"C\" fn __{name}_exit() {{
295a4dec33dSBenno Lossin // SAFETY:
296a4dec33dSBenno Lossin // - This function is inaccessible to the outside due to the double
297a4dec33dSBenno Lossin // module wrapping it. It is called exactly once by the C side via its
298a4dec33dSBenno Lossin // unique name,
299a4dec33dSBenno Lossin // - furthermore it is only called after `__{name}_init` has returned `0`
300a4dec33dSBenno Lossin // (which delegates to `__init`).
301a4dec33dSBenno Lossin unsafe {{ __exit() }}
3021fbde52bSMiguel Ojeda }}
3031fbde52bSMiguel Ojeda
304a4dec33dSBenno Lossin /// # Safety
305a4dec33dSBenno Lossin ///
306a4dec33dSBenno Lossin /// This function must only be called once.
307a4dec33dSBenno Lossin unsafe fn __init() -> core::ffi::c_int {{
308a4dec33dSBenno Lossin match <{type_} as kernel::Module>::init(&super::super::THIS_MODULE) {{
3091fbde52bSMiguel Ojeda Ok(m) => {{
310a4dec33dSBenno Lossin // SAFETY: No data race, since `__MOD` can only be accessed by this
311a4dec33dSBenno Lossin // module and there only `__init` and `__exit` access it. These
312a4dec33dSBenno Lossin // functions are only called once and `__exit` cannot be called
313a4dec33dSBenno Lossin // before or during `__init`.
3141fbde52bSMiguel Ojeda unsafe {{
3151fbde52bSMiguel Ojeda __MOD = Some(m);
3161fbde52bSMiguel Ojeda }}
3171fbde52bSMiguel Ojeda return 0;
3181fbde52bSMiguel Ojeda }}
3191fbde52bSMiguel Ojeda Err(e) => {{
32046384d09SAsahi Lina return e.to_errno();
3211fbde52bSMiguel Ojeda }}
3221fbde52bSMiguel Ojeda }}
3231fbde52bSMiguel Ojeda }}
3241fbde52bSMiguel Ojeda
325a4dec33dSBenno Lossin /// # Safety
326a4dec33dSBenno Lossin ///
327a4dec33dSBenno Lossin /// This function must
328a4dec33dSBenno Lossin /// - only be called once,
329a4dec33dSBenno Lossin /// - be called after `__init` has been called and returned `0`.
330a4dec33dSBenno Lossin unsafe fn __exit() {{
331a4dec33dSBenno Lossin // SAFETY: No data race, since `__MOD` can only be accessed by this module
332a4dec33dSBenno Lossin // and there only `__init` and `__exit` access it. These functions are only
333a4dec33dSBenno Lossin // called once and `__init` was already called.
3341fbde52bSMiguel Ojeda unsafe {{
3351fbde52bSMiguel Ojeda // Invokes `drop()` on `__MOD`, which should be used for cleanup.
3361fbde52bSMiguel Ojeda __MOD = None;
3371fbde52bSMiguel Ojeda }}
3381fbde52bSMiguel Ojeda }}
3391fbde52bSMiguel Ojeda
3401fbde52bSMiguel Ojeda {modinfo}
341a4dec33dSBenno Lossin }}
342a4dec33dSBenno Lossin }}
3431fbde52bSMiguel Ojeda ",
3441fbde52bSMiguel Ojeda type_ = info.type_,
3451fbde52bSMiguel Ojeda name = info.name,
3461fbde52bSMiguel Ojeda modinfo = modinfo.buffer,
3471fbde52bSMiguel Ojeda initcall_section = ".initcall6.init"
3481fbde52bSMiguel Ojeda )
3491fbde52bSMiguel Ojeda .parse()
3501fbde52bSMiguel Ojeda .expect("Error parsing formatted string into token stream.")
3511fbde52bSMiguel Ojeda }
352