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