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