xref: /linux/rust/macros/lib.rs (revision 99676aed1fec109d62822e21a06760eb098dc5f4)
1 // SPDX-License-Identifier: GPL-2.0
2 
3 //! Crate for all kernel procedural macros.
4 
5 // When fixdep scans this, it will find this string `CONFIG_RUSTC_VERSION_TEXT`
6 // and thus add a dependency on `include/config/RUSTC_VERSION_TEXT`, which is
7 // touched by Kconfig when the version string from the compiler changes.
8 
9 // Stable since Rust 1.87.0.
10 #![feature(extract_if)]
11 //
12 // Stable since Rust 1.88.0 under a different name, `proc_macro_span_file`,
13 // which was added in Rust 1.88.0. This is why `cfg_attr` is used here, i.e.
14 // to avoid depending on the full `proc_macro_span` on Rust >= 1.88.0.
15 #![cfg_attr(not(CONFIG_RUSTC_HAS_SPAN_FILE), feature(proc_macro_span))]
16 
17 mod concat_idents;
18 mod export;
19 mod fmt;
20 mod for_lt;
21 mod helpers;
22 mod kunit;
23 mod module;
24 mod paste;
25 mod vtable;
26 
27 use proc_macro::TokenStream;
28 
29 use syn::parse_macro_input;
30 
31 /// Declares a kernel module.
32 ///
33 /// The `type` argument should be a type which implements the [`Module`]
34 /// trait. Also accepts various forms of kernel metadata.
35 ///
36 /// The `params` field describe module parameters. Each entry has the form
37 ///
38 /// ```ignore
39 /// parameter_name: type {
40 ///     default: default_value,
41 ///     description: "Description",
42 /// }
43 /// ```
44 ///
45 /// `type` may be one of
46 ///
47 /// - [`i8`]
48 /// - [`u8`]
49 /// - [`i8`]
50 /// - [`u8`]
51 /// - [`i16`]
52 /// - [`u16`]
53 /// - [`i32`]
54 /// - [`u32`]
55 /// - [`i64`]
56 /// - [`u64`]
57 /// - [`isize`]
58 /// - [`usize`]
59 ///
60 /// C header: [`include/linux/moduleparam.h`](srctree/include/linux/moduleparam.h)
61 ///
62 /// [`Module`]: ../kernel/trait.Module.html
63 ///
64 /// # Examples
65 ///
66 /// ```ignore
67 /// use kernel::prelude::*;
68 ///
69 /// module!{
70 ///     type: MyModule,
71 ///     name: "my_kernel_module",
72 ///     authors: ["Rust for Linux Contributors"],
73 ///     description: "My very own kernel module!",
74 ///     license: "GPL",
75 ///     alias: ["alternate_module_name"],
76 ///     params: {
77 ///         my_parameter: i64 {
78 ///             default: 1,
79 ///             description: "This parameter has a default of 1",
80 ///         },
81 ///     },
82 /// }
83 ///
84 /// struct MyModule(i32);
85 ///
86 /// impl kernel::Module for MyModule {
87 ///     fn init(_module: &'static ThisModule) -> Result<Self> {
88 ///         let foo: i32 = 42;
89 ///         pr_info!("I contain:  {}\n", foo);
90 ///         pr_info!("i32 param is:  {}\n", module_parameters::my_parameter.read());
91 ///         Ok(Self(foo))
92 ///     }
93 /// }
94 /// # fn main() {}
95 /// ```
96 ///
97 /// ## Firmware
98 ///
99 /// The following example shows how to declare a kernel module that needs
100 /// to load binary firmware files. You need to specify the file names of
101 /// the firmware in the `firmware` field. The information is embedded
102 /// in the `modinfo` section of the kernel module. For example, a tool to
103 /// build an initramfs uses this information to put the firmware files into
104 /// the initramfs image.
105 ///
106 /// ```
107 /// use kernel::prelude::*;
108 ///
109 /// module!{
110 ///     type: MyDeviceDriverModule,
111 ///     name: "my_device_driver_module",
112 ///     authors: ["Rust for Linux Contributors"],
113 ///     description: "My device driver requires firmware",
114 ///     license: "GPL",
115 ///     firmware: ["my_device_firmware1.bin", "my_device_firmware2.bin"],
116 /// }
117 ///
118 /// struct MyDeviceDriverModule;
119 ///
120 /// impl kernel::Module for MyDeviceDriverModule {
121 ///     fn init(_module: &'static ThisModule) -> Result<Self> {
122 ///         Ok(Self)
123 ///     }
124 /// }
125 /// # fn main() {}
126 /// ```
127 ///
128 /// # Supported argument types
129 ///   - `type`: type which implements the [`Module`] trait (required).
130 ///   - `name`: ASCII string literal of the name of the kernel module (required).
131 ///   - `authors`: array of ASCII string literals of the authors of the kernel module.
132 ///   - `description`: string literal of the description of the kernel module.
133 ///   - `license`: ASCII string literal of the license of the kernel module (required).
134 ///   - `alias`: array of ASCII string literals of the alias names of the kernel module.
135 ///   - `firmware`: array of ASCII string literals of the firmware files of
136 ///     the kernel module.
137 #[proc_macro]
138 pub fn module(input: TokenStream) -> TokenStream {
139     module::module(parse_macro_input!(input))
140         .unwrap_or_else(|e| e.into_compile_error())
141         .into()
142 }
143 
144 /// Declares or implements a vtable trait.
145 ///
146 /// Linux's use of pure vtables is very close to Rust traits, but they differ
147 /// in how unimplemented functions are represented. In Rust, traits can provide
148 /// default implementation for all non-required methods (and the default
149 /// implementation could just return `Error::EINVAL`); Linux typically use C
150 /// `NULL` pointers to represent these functions.
151 ///
152 /// This attribute closes that gap. A trait can be annotated with the
153 /// `#[vtable]` attribute. Implementers of the trait will then also have to
154 /// annotate the trait with `#[vtable]`. This attribute generates a `HAS_*`
155 /// associated constant bool for each method in the trait that is set to true if
156 /// the implementer has overridden the associated method.
157 ///
158 /// For a trait method to be optional, it must have a default implementation.
159 /// This is also the case for traits annotated with `#[vtable]`, but in this
160 /// case the default implementation will never be executed. The reason for this
161 /// is that the functions will be called through function pointers installed in
162 /// C side vtables. When an optional method is not implemented on a `#[vtable]`
163 /// trait, a `NULL` entry is installed in the vtable. Thus the default
164 /// implementation is never called. Since these traits are not designed to be
165 /// used on the Rust side, it should not be possible to call the default
166 /// implementation. This is done to ensure that we call the vtable methods
167 /// through the C vtable, and not through the Rust vtable. Therefore, the
168 /// default implementation should call `build_error!`, which prevents
169 /// calls to this function at compile time:
170 ///
171 /// ```compile_fail
172 /// # // Intentionally missing `use`s to simplify `rusttest`.
173 /// build_error!(VTABLE_DEFAULT_ERROR)
174 /// ```
175 ///
176 /// Note that you might need to import [`kernel::error::VTABLE_DEFAULT_ERROR`].
177 ///
178 /// This macro should not be used when all functions are required.
179 ///
180 /// # Examples
181 ///
182 /// ```
183 /// use kernel::error::VTABLE_DEFAULT_ERROR;
184 /// use kernel::prelude::*;
185 ///
186 /// // Declares a `#[vtable]` trait
187 /// #[vtable]
188 /// pub trait Operations: Send + Sync + Sized {
189 ///     fn foo(&self) -> Result<()> {
190 ///         build_error!(VTABLE_DEFAULT_ERROR)
191 ///     }
192 ///
193 ///     fn bar(&self) -> Result<()> {
194 ///         build_error!(VTABLE_DEFAULT_ERROR)
195 ///     }
196 /// }
197 ///
198 /// struct Foo;
199 ///
200 /// // Implements the `#[vtable]` trait
201 /// #[vtable]
202 /// impl Operations for Foo {
203 ///     fn foo(&self) -> Result<()> {
204 /// #        Err(EINVAL)
205 ///         // ...
206 ///     }
207 /// }
208 ///
209 /// assert_eq!(<Foo as Operations>::HAS_FOO, true);
210 /// assert_eq!(<Foo as Operations>::HAS_BAR, false);
211 /// ```
212 ///
213 /// [`kernel::error::VTABLE_DEFAULT_ERROR`]: ../kernel/error/constant.VTABLE_DEFAULT_ERROR.html
214 #[proc_macro_attribute]
215 pub fn vtable(attr: TokenStream, input: TokenStream) -> TokenStream {
216     parse_macro_input!(attr as syn::parse::Nothing);
217     vtable::vtable(parse_macro_input!(input))
218         .unwrap_or_else(|e| e.into_compile_error())
219         .into()
220 }
221 
222 /// Export a function so that C code can call it via a header file.
223 ///
224 /// Functions exported using this macro can be called from C code using the declaration in the
225 /// appropriate header file. It should only be used in cases where C calls the function through a
226 /// header file; cases where C calls into Rust via a function pointer in a vtable (such as
227 /// `file_operations`) should not use this macro.
228 ///
229 /// This macro has the following effect:
230 ///
231 /// * Disables name mangling for this function.
232 /// * Verifies at compile-time that the function signature matches the declaration in the header
233 ///   file.
234 ///
235 /// You must declare the signature of the Rust function in a header file that is included by
236 /// `rust/bindings/bindings_helper.h`.
237 ///
238 /// This macro is *not* the same as the C macros `EXPORT_SYMBOL_*`. All Rust symbols are currently
239 /// automatically exported with `EXPORT_SYMBOL_GPL`.
240 #[proc_macro_attribute]
241 pub fn export(attr: TokenStream, input: TokenStream) -> TokenStream {
242     parse_macro_input!(attr as syn::parse::Nothing);
243     export::export(parse_macro_input!(input)).into()
244 }
245 
246 /// Like [`core::format_args!`], but automatically wraps arguments in [`kernel::fmt::Adapter`].
247 ///
248 /// This macro allows generating `fmt::Arguments` while ensuring that each argument is wrapped with
249 /// `::kernel::fmt::Adapter`, which customizes formatting behavior for kernel logging.
250 ///
251 /// Named arguments used in the format string (e.g. `{foo}`) are detected and resolved from local
252 /// bindings. All positional and named arguments are automatically wrapped.
253 ///
254 /// This macro is an implementation detail of other kernel logging macros like [`pr_info!`] and
255 /// should not typically be used directly.
256 ///
257 /// [`kernel::fmt::Adapter`]: ../kernel/fmt/struct.Adapter.html
258 /// [`pr_info!`]: ../kernel/macro.pr_info.html
259 #[proc_macro]
260 pub fn fmt(input: TokenStream) -> TokenStream {
261     fmt::fmt(input.into()).into()
262 }
263 
264 /// Concatenate two identifiers.
265 ///
266 /// This is useful in macros that need to declare or reference items with names
267 /// starting with a fixed prefix and ending in a user specified name. The resulting
268 /// identifier has the span of the second argument.
269 ///
270 /// # Examples
271 ///
272 /// ```
273 /// # const binder_driver_return_protocol_BR_OK: u32 = 0;
274 /// # const binder_driver_return_protocol_BR_ERROR: u32 = 1;
275 /// # const binder_driver_return_protocol_BR_TRANSACTION: u32 = 2;
276 /// # const binder_driver_return_protocol_BR_REPLY: u32 = 3;
277 /// # const binder_driver_return_protocol_BR_DEAD_REPLY: u32 = 4;
278 /// # const binder_driver_return_protocol_BR_TRANSACTION_COMPLETE: u32 = 5;
279 /// # const binder_driver_return_protocol_BR_INCREFS: u32 = 6;
280 /// # const binder_driver_return_protocol_BR_ACQUIRE: u32 = 7;
281 /// # const binder_driver_return_protocol_BR_RELEASE: u32 = 8;
282 /// # const binder_driver_return_protocol_BR_DECREFS: u32 = 9;
283 /// # const binder_driver_return_protocol_BR_NOOP: u32 = 10;
284 /// # const binder_driver_return_protocol_BR_SPAWN_LOOPER: u32 = 11;
285 /// # const binder_driver_return_protocol_BR_DEAD_BINDER: u32 = 12;
286 /// # const binder_driver_return_protocol_BR_CLEAR_DEATH_NOTIFICATION_DONE: u32 = 13;
287 /// # const binder_driver_return_protocol_BR_FAILED_REPLY: u32 = 14;
288 /// use kernel::macros::concat_idents;
289 ///
290 /// macro_rules! pub_no_prefix {
291 ///     ($prefix:ident, $($newname:ident),+) => {
292 ///         $(pub(crate) const $newname: u32 = concat_idents!($prefix, $newname);)+
293 ///     };
294 /// }
295 ///
296 /// pub_no_prefix!(
297 ///     binder_driver_return_protocol_,
298 ///     BR_OK,
299 ///     BR_ERROR,
300 ///     BR_TRANSACTION,
301 ///     BR_REPLY,
302 ///     BR_DEAD_REPLY,
303 ///     BR_TRANSACTION_COMPLETE,
304 ///     BR_INCREFS,
305 ///     BR_ACQUIRE,
306 ///     BR_RELEASE,
307 ///     BR_DECREFS,
308 ///     BR_NOOP,
309 ///     BR_SPAWN_LOOPER,
310 ///     BR_DEAD_BINDER,
311 ///     BR_CLEAR_DEATH_NOTIFICATION_DONE,
312 ///     BR_FAILED_REPLY
313 /// );
314 ///
315 /// assert_eq!(BR_OK, binder_driver_return_protocol_BR_OK);
316 /// ```
317 #[proc_macro]
318 pub fn concat_idents(input: TokenStream) -> TokenStream {
319     concat_idents::concat_idents(parse_macro_input!(input)).into()
320 }
321 
322 /// Paste identifiers together.
323 ///
324 /// Within the `paste!` macro, identifiers inside `[<` and `>]` are concatenated together to form a
325 /// single identifier.
326 ///
327 /// This is similar to the [`paste`] crate, but with pasting feature limited to identifiers and
328 /// literals (lifetimes and documentation strings are not supported). There is a difference in
329 /// supported modifiers as well.
330 ///
331 /// # Examples
332 ///
333 /// ```
334 /// # const binder_driver_return_protocol_BR_OK: u32 = 0;
335 /// # const binder_driver_return_protocol_BR_ERROR: u32 = 1;
336 /// # const binder_driver_return_protocol_BR_TRANSACTION: u32 = 2;
337 /// # const binder_driver_return_protocol_BR_REPLY: u32 = 3;
338 /// # const binder_driver_return_protocol_BR_DEAD_REPLY: u32 = 4;
339 /// # const binder_driver_return_protocol_BR_TRANSACTION_COMPLETE: u32 = 5;
340 /// # const binder_driver_return_protocol_BR_INCREFS: u32 = 6;
341 /// # const binder_driver_return_protocol_BR_ACQUIRE: u32 = 7;
342 /// # const binder_driver_return_protocol_BR_RELEASE: u32 = 8;
343 /// # const binder_driver_return_protocol_BR_DECREFS: u32 = 9;
344 /// # const binder_driver_return_protocol_BR_NOOP: u32 = 10;
345 /// # const binder_driver_return_protocol_BR_SPAWN_LOOPER: u32 = 11;
346 /// # const binder_driver_return_protocol_BR_DEAD_BINDER: u32 = 12;
347 /// # const binder_driver_return_protocol_BR_CLEAR_DEATH_NOTIFICATION_DONE: u32 = 13;
348 /// # const binder_driver_return_protocol_BR_FAILED_REPLY: u32 = 14;
349 /// macro_rules! pub_no_prefix {
350 ///     ($prefix:ident, $($newname:ident),+) => {
351 ///         ::kernel::macros::paste! {
352 ///             $(pub(crate) const $newname: u32 = [<$prefix $newname>];)+
353 ///         }
354 ///     };
355 /// }
356 ///
357 /// pub_no_prefix!(
358 ///     binder_driver_return_protocol_,
359 ///     BR_OK,
360 ///     BR_ERROR,
361 ///     BR_TRANSACTION,
362 ///     BR_REPLY,
363 ///     BR_DEAD_REPLY,
364 ///     BR_TRANSACTION_COMPLETE,
365 ///     BR_INCREFS,
366 ///     BR_ACQUIRE,
367 ///     BR_RELEASE,
368 ///     BR_DECREFS,
369 ///     BR_NOOP,
370 ///     BR_SPAWN_LOOPER,
371 ///     BR_DEAD_BINDER,
372 ///     BR_CLEAR_DEATH_NOTIFICATION_DONE,
373 ///     BR_FAILED_REPLY
374 /// );
375 ///
376 /// assert_eq!(BR_OK, binder_driver_return_protocol_BR_OK);
377 /// ```
378 ///
379 /// # Modifiers
380 ///
381 /// For each identifier, it is possible to attach one or multiple modifiers to
382 /// it.
383 ///
384 /// Currently supported modifiers are:
385 /// * `span`: change the span of concatenated identifier to the span of the specified token. By
386 ///   default the span of the `[< >]` group is used.
387 /// * `lower`: change the identifier to lower case.
388 /// * `upper`: change the identifier to upper case.
389 ///
390 /// ```
391 /// # const binder_driver_return_protocol_BR_OK: u32 = 0;
392 /// # const binder_driver_return_protocol_BR_ERROR: u32 = 1;
393 /// # const binder_driver_return_protocol_BR_TRANSACTION: u32 = 2;
394 /// # const binder_driver_return_protocol_BR_REPLY: u32 = 3;
395 /// # const binder_driver_return_protocol_BR_DEAD_REPLY: u32 = 4;
396 /// # const binder_driver_return_protocol_BR_TRANSACTION_COMPLETE: u32 = 5;
397 /// # const binder_driver_return_protocol_BR_INCREFS: u32 = 6;
398 /// # const binder_driver_return_protocol_BR_ACQUIRE: u32 = 7;
399 /// # const binder_driver_return_protocol_BR_RELEASE: u32 = 8;
400 /// # const binder_driver_return_protocol_BR_DECREFS: u32 = 9;
401 /// # const binder_driver_return_protocol_BR_NOOP: u32 = 10;
402 /// # const binder_driver_return_protocol_BR_SPAWN_LOOPER: u32 = 11;
403 /// # const binder_driver_return_protocol_BR_DEAD_BINDER: u32 = 12;
404 /// # const binder_driver_return_protocol_BR_CLEAR_DEATH_NOTIFICATION_DONE: u32 = 13;
405 /// # const binder_driver_return_protocol_BR_FAILED_REPLY: u32 = 14;
406 /// macro_rules! pub_no_prefix {
407 ///     ($prefix:ident, $($newname:ident),+) => {
408 ///         ::kernel::macros::paste! {
409 ///             $(pub(crate) const fn [<$newname:lower:span>]() -> u32 { [<$prefix $newname:span>] })+
410 ///         }
411 ///     };
412 /// }
413 ///
414 /// pub_no_prefix!(
415 ///     binder_driver_return_protocol_,
416 ///     BR_OK,
417 ///     BR_ERROR,
418 ///     BR_TRANSACTION,
419 ///     BR_REPLY,
420 ///     BR_DEAD_REPLY,
421 ///     BR_TRANSACTION_COMPLETE,
422 ///     BR_INCREFS,
423 ///     BR_ACQUIRE,
424 ///     BR_RELEASE,
425 ///     BR_DECREFS,
426 ///     BR_NOOP,
427 ///     BR_SPAWN_LOOPER,
428 ///     BR_DEAD_BINDER,
429 ///     BR_CLEAR_DEATH_NOTIFICATION_DONE,
430 ///     BR_FAILED_REPLY
431 /// );
432 ///
433 /// assert_eq!(br_ok(), binder_driver_return_protocol_BR_OK);
434 /// ```
435 ///
436 /// # Literals
437 ///
438 /// Literals can also be concatenated with other identifiers:
439 ///
440 /// ```
441 /// macro_rules! create_numbered_fn {
442 ///     ($name:literal, $val:literal) => {
443 ///         ::kernel::macros::paste! {
444 ///             fn [<some_ $name _fn $val>]() -> u32 { $val }
445 ///         }
446 ///     };
447 /// }
448 ///
449 /// create_numbered_fn!("foo", 100);
450 ///
451 /// assert_eq!(some_foo_fn100(), 100)
452 /// ```
453 ///
454 /// [`paste`]: https://docs.rs/paste/
455 #[proc_macro]
456 pub fn paste(input: TokenStream) -> TokenStream {
457     let mut tokens = proc_macro2::TokenStream::from(input).into_iter().collect();
458     paste::expand(&mut tokens);
459     tokens
460         .into_iter()
461         .collect::<proc_macro2::TokenStream>()
462         .into()
463 }
464 
465 /// Registers a KUnit test suite and its test cases using a user-space like syntax.
466 ///
467 /// This macro should be used on modules. If `CONFIG_KUNIT` (in `.config`) is `n`, the target module
468 /// is ignored.
469 ///
470 /// # Examples
471 ///
472 /// ```ignore
473 /// # use kernel::prelude::*;
474 /// #[kunit_tests(kunit_test_suit_name)]
475 /// mod tests {
476 ///     #[test]
477 ///     fn foo() {
478 ///         assert_eq!(1, 1);
479 ///     }
480 ///
481 ///     #[test]
482 ///     fn bar() {
483 ///         assert_eq!(2, 2);
484 ///     }
485 /// }
486 /// ```
487 #[proc_macro_attribute]
488 pub fn kunit_tests(attr: TokenStream, input: TokenStream) -> TokenStream {
489     kunit::kunit_tests(parse_macro_input!(attr), parse_macro_input!(input))
490         .unwrap_or_else(|e| e.into_compile_error())
491         .into()
492 }
493 
494 /// Obtain a type that implements [`ForLt`] for the given higher-ranked type.
495 ///
496 /// Please refer to the documentation of the [`ForLt`] trait.
497 ///
498 /// [`ForLt`]: trait.ForLt.html
499 #[proc_macro]
500 // The macro shares the name with the trait.
501 #[allow(non_snake_case)]
502 pub fn ForLt(input: TokenStream) -> TokenStream {
503     for_lt::for_lt(parse_macro_input!(input)).into()
504 }
505