Skip to main content

macros/
lib.rs

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
17mod concat_idents;
18mod export;
19mod fmt;
20mod helpers;
21mod kunit;
22mod module;
23mod paste;
24mod vtable;
25
26use proc_macro::TokenStream;
27
28use syn::parse_macro_input;
29
30/// Declares a kernel module.
31///
32/// The `type` argument should be a type which implements the [`Module`]
33/// trait. Also accepts various forms of kernel metadata.
34///
35/// The `params` field describe module parameters. Each entry has the form
36///
37/// ```ignore
38/// parameter_name: type {
39///     default: default_value,
40///     description: "Description",
41/// }
42/// ```
43///
44/// `type` may be one of
45///
46/// - [`i8`]
47/// - [`u8`]
48/// - [`i8`]
49/// - [`u8`]
50/// - [`i16`]
51/// - [`u16`]
52/// - [`i32`]
53/// - [`u32`]
54/// - [`i64`]
55/// - [`u64`]
56/// - [`isize`]
57/// - [`usize`]
58///
59/// C header: [`include/linux/moduleparam.h`](srctree/include/linux/moduleparam.h)
60///
61/// [`Module`]: ../kernel/trait.Module.html
62///
63/// # Examples
64///
65/// ```ignore
66/// use kernel::prelude::*;
67///
68/// module!{
69///     type: MyModule,
70///     name: "my_kernel_module",
71///     authors: ["Rust for Linux Contributors"],
72///     description: "My very own kernel module!",
73///     license: "GPL",
74///     alias: ["alternate_module_name"],
75///     params: {
76///         my_parameter: i64 {
77///             default: 1,
78///             description: "This parameter has a default of 1",
79///         },
80///     },
81/// }
82///
83/// struct MyModule(i32);
84///
85/// impl kernel::Module for MyModule {
86///     fn init(_module: &'static ThisModule) -> Result<Self> {
87///         let foo: i32 = 42;
88///         pr_info!("I contain:  {}\n", foo);
89///         pr_info!("i32 param is:  {}\n", module_parameters::my_parameter.read());
90///         Ok(Self(foo))
91///     }
92/// }
93/// # fn main() {}
94/// ```
95///
96/// ## Firmware
97///
98/// The following example shows how to declare a kernel module that needs
99/// to load binary firmware files. You need to specify the file names of
100/// the firmware in the `firmware` field. The information is embedded
101/// in the `modinfo` section of the kernel module. For example, a tool to
102/// build an initramfs uses this information to put the firmware files into
103/// the initramfs image.
104///
105/// ```
106/// use kernel::prelude::*;
107///
108/// module!{
109///     type: MyDeviceDriverModule,
110///     name: "my_device_driver_module",
111///     authors: ["Rust for Linux Contributors"],
112///     description: "My device driver requires firmware",
113///     license: "GPL",
114///     firmware: ["my_device_firmware1.bin", "my_device_firmware2.bin"],
115/// }
116///
117/// struct MyDeviceDriverModule;
118///
119/// impl kernel::Module for MyDeviceDriverModule {
120///     fn init(_module: &'static ThisModule) -> Result<Self> {
121///         Ok(Self)
122///     }
123/// }
124/// # fn main() {}
125/// ```
126///
127/// # Supported argument types
128///   - `type`: type which implements the [`Module`] trait (required).
129///   - `name`: ASCII string literal of the name of the kernel module (required).
130///   - `authors`: array of ASCII string literals of the authors of the kernel module.
131///   - `description`: string literal of the description of the kernel module.
132///   - `license`: ASCII string literal of the license of the kernel module (required).
133///   - `alias`: array of ASCII string literals of the alias names of the kernel module.
134///   - `firmware`: array of ASCII string literals of the firmware files of
135///     the kernel module.
136#[proc_macro]
137pub fn module(input: TokenStream) -> TokenStream {
138    module::module(parse_macro_input!(input))
139        .unwrap_or_else(|e| e.into_compile_error())
140        .into()
141}
142
143/// Declares or implements a vtable trait.
144///
145/// Linux's use of pure vtables is very close to Rust traits, but they differ
146/// in how unimplemented functions are represented. In Rust, traits can provide
147/// default implementation for all non-required methods (and the default
148/// implementation could just return `Error::EINVAL`); Linux typically use C
149/// `NULL` pointers to represent these functions.
150///
151/// This attribute closes that gap. A trait can be annotated with the
152/// `#[vtable]` attribute. Implementers of the trait will then also have to
153/// annotate the trait with `#[vtable]`. This attribute generates a `HAS_*`
154/// associated constant bool for each method in the trait that is set to true if
155/// the implementer has overridden the associated method.
156///
157/// For a trait method to be optional, it must have a default implementation.
158/// This is also the case for traits annotated with `#[vtable]`, but in this
159/// case the default implementation will never be executed. The reason for this
160/// is that the functions will be called through function pointers installed in
161/// C side vtables. When an optional method is not implemented on a `#[vtable]`
162/// trait, a `NULL` entry is installed in the vtable. Thus the default
163/// implementation is never called. Since these traits are not designed to be
164/// used on the Rust side, it should not be possible to call the default
165/// implementation. This is done to ensure that we call the vtable methods
166/// through the C vtable, and not through the Rust vtable. Therefore, the
167/// default implementation should call `build_error!`, which prevents
168/// calls to this function at compile time:
169///
170/// ```compile_fail
171/// # // Intentionally missing `use`s to simplify `rusttest`.
172/// build_error!(VTABLE_DEFAULT_ERROR)
173/// ```
174///
175/// Note that you might need to import [`kernel::error::VTABLE_DEFAULT_ERROR`].
176///
177/// This macro should not be used when all functions are required.
178///
179/// # Examples
180///
181/// ```
182/// use kernel::error::VTABLE_DEFAULT_ERROR;
183/// use kernel::prelude::*;
184///
185/// // Declares a `#[vtable]` trait
186/// #[vtable]
187/// pub trait Operations: Send + Sync + Sized {
188///     fn foo(&self) -> Result<()> {
189///         build_error!(VTABLE_DEFAULT_ERROR)
190///     }
191///
192///     fn bar(&self) -> Result<()> {
193///         build_error!(VTABLE_DEFAULT_ERROR)
194///     }
195/// }
196///
197/// struct Foo;
198///
199/// // Implements the `#[vtable]` trait
200/// #[vtable]
201/// impl Operations for Foo {
202///     fn foo(&self) -> Result<()> {
203/// #        Err(EINVAL)
204///         // ...
205///     }
206/// }
207///
208/// assert_eq!(<Foo as Operations>::HAS_FOO, true);
209/// assert_eq!(<Foo as Operations>::HAS_BAR, false);
210/// ```
211///
212/// [`kernel::error::VTABLE_DEFAULT_ERROR`]: ../kernel/error/constant.VTABLE_DEFAULT_ERROR.html
213#[proc_macro_attribute]
214pub 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]
240pub 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]
259pub 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]
317pub 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]
455pub 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]
487pub 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}