~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/rust/macros/module.rs

Version: ~ [ linux-6.12-rc7 ] ~ [ linux-6.11.7 ] ~ [ linux-6.10.14 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.60 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.116 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.171 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.229 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.285 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.323 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.12 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

Diff markup

Differences between /rust/macros/module.rs (Version linux-6.12-rc7) and /rust/macros/module.rs (Version linux-4.19.323)


  1 // SPDX-License-Identifier: GPL-2.0               
  2                                                   
  3 use crate::helpers::*;                            
  4 use proc_macro::{token_stream, Delimiter, Lite    
  5 use std::fmt::Write;                              
  6                                                   
  7 fn expect_string_array(it: &mut token_stream::    
  8     let group = expect_group(it);                 
  9     assert_eq!(group.delimiter(), Delimiter::B    
 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 ASCI    
 15         values.push(val);                         
 16         match it.next() {                         
 17             Some(TokenTree::Punct(punct)) => a    
 18             None => break,                        
 19             _ => panic!("Expected ',' or end o    
 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, conte    
 41         let string = if builtin {                 
 42             // Built-in modules prefix their m    
 43             format!(                              
 44                 "{module}.{field}={content}\0"    
 45                 module = self.module,             
 46                 field = field,                    
 47                 content = content                 
 48             )                                     
 49         } else {                                  
 50             // Loadable modules' modinfo strin    
 51             format!("{field}={content}\0", fie    
 52         };                                        
 53                                                   
 54         write!(                                   
 55             &mut self.buffer,                     
 56             "                                     
 57                 {cfg}                             
 58                 #[doc(hidden)]                    
 59                 #[link_section = \".modinfo\"]    
 60                 #[used]                           
 61                 pub static __{module}_{counter    
 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(stri    
 72         )                                         
 73         .unwrap();                                
 74                                                   
 75         self.counter += 1;                        
 76     }                                             
 77                                                   
 78     fn emit_only_builtin(&mut self, field: &st    
 79         self.emit_base(field, content, true)      
 80     }                                             
 81                                                   
 82     fn emit_only_loadable(&mut self, field: &s    
 83         self.emit_base(field, content, false)     
 84     }                                             
 85                                                   
 86     fn emit(&mut self, field: &str, content: &    
 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     author: Option<String>,                       
 98     description: Option<String>,                  
 99     alias: Option<Vec<String>>,                   
100     firmware: Option<Vec<String>>,                
101 }                                                 
102                                                   
103 impl ModuleInfo {                                 
104     fn parse(it: &mut token_stream::IntoIter)     
105         let mut info = ModuleInfo::default();     
106                                                   
107         const EXPECTED_KEYS: &[&str] = &[         
108             "type",                               
109             "name",                               
110             "author",                             
111             "description",                        
112             "license",                            
113             "alias",                              
114             "firmware",                           
115         ];                                        
116         const REQUIRED_KEYS: &[&str] = &["type    
117         let mut seen_keys = Vec::new();           
118                                                   
119         loop {                                    
120             let key = match it.next() {           
121                 Some(TokenTree::Ident(ident))     
122                 Some(_) => panic!("Expected Id    
123                 None => break,                    
124             };                                    
125                                                   
126             if seen_keys.contains(&key) {         
127                 panic!(                           
128                     "Duplicated key \"{}\". Ke    
129                     key                           
130                 );                                
131             }                                     
132                                                   
133             assert_eq!(expect_punct(it), ':');    
134                                                   
135             match key.as_str() {                  
136                 "type" => info.type_ = expect_    
137                 "name" => info.name = expect_s    
138                 "author" => info.author = Some    
139                 "description" => info.descript    
140                 "license" => info.license = ex    
141                 "alias" => info.alias = Some(e    
142                 "firmware" => info.firmware =     
143                 _ => panic!(                      
144                     "Unknown key \"{}\". Valid    
145                     key, EXPECTED_KEYS            
146                 ),                                
147             }                                     
148                                                   
149             assert_eq!(expect_punct(it), ',');    
150                                                   
151             seen_keys.push(key);                  
152         }                                         
153                                                   
154         expect_end(it);                           
155                                                   
156         for key in REQUIRED_KEYS {                
157             if !seen_keys.iter().any(|e| e ==     
158                 panic!("Missing required key \    
159             }                                     
160         }                                         
161                                                   
162         let mut ordered_keys: Vec<&str> = Vec:    
163         for key in EXPECTED_KEYS {                
164             if seen_keys.iter().any(|e| e == k    
165                 ordered_keys.push(key);           
166             }                                     
167         }                                         
168                                                   
169         if seen_keys != ordered_keys {            
170             panic!(                               
171                 "Keys are not ordered as expec    
172                 ordered_keys                      
173             );                                    
174         }                                         
175                                                   
176         info                                      
177     }                                             
178 }                                                 
179                                                   
180 pub(crate) fn module(ts: TokenStream) -> Token    
181     let mut it = ts.into_iter();                  
182                                                   
183     let info = ModuleInfo::parse(&mut it);        
184                                                   
185     let mut modinfo = ModInfoBuilder::new(info    
186     if let Some(author) = info.author {           
187         modinfo.emit("author", &author);          
188     }                                             
189     if let Some(description) = info.descriptio    
190         modinfo.emit("description", &descripti    
191     }                                             
192     modinfo.emit("license", &info.license);       
193     if let Some(aliases) = info.alias {           
194         for alias in aliases {                    
195             modinfo.emit("alias", &alias);        
196         }                                         
197     }                                             
198     if let Some(firmware) = info.firmware {       
199         for fw in firmware {                      
200             modinfo.emit("firmware", &fw);        
201         }                                         
202     }                                             
203                                                   
204     // Built-in modules also export the `file`    
205     let file =                                    
206         std::env::var("RUST_MODFILE").expect("    
207     modinfo.emit_only_builtin("file", &file);     
208                                                   
209     format!(                                      
210         "                                         
211             /// The module name.                  
212             ///                                   
213             /// Used by the printing macros, e    
214             const __LOG_PREFIX: &[u8] = b\"{na    
215                                                   
216             // SAFETY: `__this_module` is cons    
217             // freed until the module is unloa    
218             #[cfg(MODULE)]                        
219             static THIS_MODULE: kernel::ThisMo    
220                 extern \"C\" {{                   
221                     static __this_module: kern    
222                 }}                                
223                                                   
224                 kernel::ThisModule::from_ptr(_    
225             }};                                   
226             #[cfg(not(MODULE))]                   
227             static THIS_MODULE: kernel::ThisMo    
228                 kernel::ThisModule::from_ptr(c    
229             }};                                   
230                                                   
231             // Double nested modules, since th    
232             mod __module_init {{                  
233                 mod __module_init {{              
234                     use super::super::{type_};    
235                                                   
236                     /// The \"Rust loadable mo    
237                     //                            
238                     // This may be best done a    
239                     // key or a new section. F    
240                     #[cfg(MODULE)]                
241                     #[doc(hidden)]                
242                     #[used]                       
243                     static __IS_RUST_MODULE: (    
244                                                   
245                     static mut __MOD: Option<{    
246                                                   
247                     // Loadable modules need t    
248                     /// # Safety                  
249                     ///                           
250                     /// This function must not    
251                     /// freed after that compl    
252                     #[cfg(MODULE)]                
253                     #[doc(hidden)]                
254                     #[no_mangle]                  
255                     #[link_section = \".init.t    
256                     pub unsafe extern \"C\" fn    
257                         // SAFETY: This functi    
258                         // module wrapping it.    
259                         // unique name.           
260                         unsafe {{ __init() }}     
261                     }}                            
262                                                   
263                     #[cfg(MODULE)]                
264                     #[doc(hidden)]                
265                     #[used]                       
266                     #[link_section = \".init.d    
267                     static __UNIQUE_ID___addre    
268                                                   
269                     #[cfg(MODULE)]                
270                     #[doc(hidden)]                
271                     #[no_mangle]                  
272                     pub extern \"C\" fn cleanu    
273                         // SAFETY:                
274                         // - This function is     
275                         //   module wrapping i    
276                         //   unique name,         
277                         // - furthermore it is    
278                         //   (which delegates     
279                         unsafe {{ __exit() }}     
280                     }}                            
281                                                   
282                     #[cfg(MODULE)]                
283                     #[doc(hidden)]                
284                     #[used]                       
285                     #[link_section = \".exit.d    
286                     static __UNIQUE_ID___addre    
287                                                   
288                     // Built-in modules are in    
289                     // and the identifiers nee    
290                     #[cfg(not(MODULE))]           
291                     #[cfg(not(CONFIG_HAVE_ARCH    
292                     #[doc(hidden)]                
293                     #[link_section = \"{initca    
294                     #[used]                       
295                     pub static __{name}_initca    
296                                                   
297                     #[cfg(not(MODULE))]           
298                     #[cfg(CONFIG_HAVE_ARCH_PRE    
299                     core::arch::global_asm!(      
300                         r#\".section \"{initca    
301                         __{name}_initcall:        
302                             .long   __{name}_i    
303                             .previous             
304                         \"#                       
305                     );                            
306                                                   
307                     #[cfg(not(MODULE))]           
308                     #[doc(hidden)]                
309                     #[no_mangle]                  
310                     pub extern \"C\" fn __{nam    
311                         // SAFETY: This functi    
312                         // module wrapping it.    
313                         // placement above in     
314                         unsafe {{ __init() }}     
315                     }}                            
316                                                   
317                     #[cfg(not(MODULE))]           
318                     #[doc(hidden)]                
319                     #[no_mangle]                  
320                     pub extern \"C\" fn __{nam    
321                         // SAFETY:                
322                         // - This function is     
323                         //   module wrapping i    
324                         //   unique name,         
325                         // - furthermore it is    
326                         //   (which delegates     
327                         unsafe {{ __exit() }}     
328                     }}                            
329                                                   
330                     /// # Safety                  
331                     ///                           
332                     /// This function must onl    
333                     unsafe fn __init() -> core    
334                         match <{type_} as kern    
335                             Ok(m) => {{           
336                                 // SAFETY: No     
337                                 // module and     
338                                 // functions a    
339                                 // before or d    
340                                 unsafe {{         
341                                     __MOD = So    
342                                 }}                
343                                 return 0;         
344                             }}                    
345                             Err(e) => {{          
346                                 return e.to_er    
347                             }}                    
348                         }}                        
349                     }}                            
350                                                   
351                     /// # Safety                  
352                     ///                           
353                     /// This function must        
354                     /// - only be called once,    
355                     /// - be called after `__i    
356                     unsafe fn __exit() {{         
357                         // SAFETY: No data rac    
358                         // and there only `__i    
359                         // called once and `__    
360                         unsafe {{                 
361                             // Invokes `drop()    
362                             __MOD = None;         
363                         }}                        
364                     }}                            
365                                                   
366                     {modinfo}                     
367                 }}                                
368             }}                                    
369         ",                                        
370         type_ = info.type_,                       
371         name = info.name,                         
372         modinfo = modinfo.buffer,                 
373         initcall_section = ".initcall6.init"      
374     )                                             
375     .parse()                                      
376     .expect("Error parsing formatted string in    
377 }                                                 
                                                      

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

sflogo.php