1 // SPDX-License-Identifier: GPL-2.0
3 //! Crate for all kernel procedural macros.
16 use proc_macro::TokenStream;
18 /// Declares a kernel module.
20 /// The `type` argument should be a type which implements the [`Module`]
21 /// trait. Also accepts various forms of kernel metadata.
23 /// C header: [`include/linux/moduleparam.h`](../../../include/linux/moduleparam.h)
25 /// [`Module`]: ../kernel/trait.Module.html
30 /// use kernel::prelude::*;
34 /// name: "my_kernel_module",
35 /// author: "Rust for Linux Contributors",
36 /// description: "My very own kernel module!",
41 /// permissions: 0o000,
42 /// description: "Example of i32",
44 /// writeable_i32: i32 {
46 /// permissions: 0o644,
47 /// description: "Example of i32",
54 /// impl kernel::Module for MyModule {
55 /// fn init() -> Result<Self> {
56 /// // If the parameter is writeable, then the kparam lock must be
57 /// // taken to read the parameter:
59 /// let lock = THIS_MODULE.kernel_param_lock();
60 /// pr_info!("i32 param is: {}\n", writeable_i32.read(&lock));
62 /// // If the parameter is read only, it can be read without locking
63 /// // the kernel parameters:
64 /// pr_info!("i32 param is: {}\n", my_i32.read());
70 /// # Supported argument types
71 /// - `type`: type which implements the [`Module`] trait (required).
72 /// - `name`: byte array of the name of the kernel module (required).
73 /// - `author`: byte array of the author of the kernel module.
74 /// - `description`: byte array of the description of the kernel module.
75 /// - `license`: byte array of the license of the kernel module (required).
76 /// - `alias`: byte array of alias name of the kernel module.
78 pub fn module(ts: TokenStream) -> TokenStream {
82 /// Declares or implements a vtable trait.
84 /// Linux's use of pure vtables is very close to Rust traits, but they differ
85 /// in how unimplemented functions are represented. In Rust, traits can provide
86 /// default implementation for all non-required methods (and the default
87 /// implementation could just return `Error::EINVAL`); Linux typically use C
88 /// `NULL` pointers to represent these functions.
90 /// This attribute is intended to close the gap. Traits can be declared and
91 /// implemented with the `#[vtable]` attribute, and a `HAS_*` associated constant
92 /// will be generated for each method in the trait, indicating if the implementor
93 /// has overridden a method.
95 /// This attribute is not needed if all methods are required.
100 /// use kernel::prelude::*;
102 /// // Declares a `#[vtable]` trait
104 /// pub trait Operations: Send + Sync + Sized {
105 /// fn foo(&self) -> Result<()> {
109 /// fn bar(&self) -> Result<()> {
116 /// // Implements the `#[vtable]` trait
118 /// impl Operations for Foo {
119 /// fn foo(&self) -> Result<()> {
125 /// assert_eq!(<Foo as Operations>::HAS_FOO, true);
126 /// assert_eq!(<Foo as Operations>::HAS_BAR, false);
128 #[proc_macro_attribute]
129 pub fn vtable(attr: TokenStream, ts: TokenStream) -> TokenStream {
130 vtable::vtable(attr, ts)
133 /// Concatenate two identifiers.
135 /// This is useful in macros that need to declare or reference items with names
136 /// starting with a fixed prefix and ending in a user specified name. The resulting
137 /// identifier has the span of the second argument.
142 /// use kernel::macro::concat_idents;
144 /// macro_rules! pub_no_prefix {
145 /// ($prefix:ident, $($newname:ident),+) => {
146 /// $(pub(crate) const $newname: u32 = kernel::macros::concat_idents!($prefix, $newname);)+
151 /// binder_driver_return_protocol_,
157 /// BR_TRANSACTION_COMPLETE,
165 /// BR_CLEAR_DEATH_NOTIFICATION_DONE,
169 /// assert_eq!(BR_OK, binder_driver_return_protocol_BR_OK);
172 pub fn concat_idents(ts: TokenStream) -> TokenStream {
173 concat_idents::concat_idents(ts)
176 /// Used to specify the pinning information of the fields of a struct.
178 /// This is somewhat similar in purpose as
179 /// [pin-project-lite](https://crates.io/crates/pin-project-lite).
180 /// Place this macro on a struct definition and then `#[pin]` in front of the attributes of each
181 /// field you want to structurally pin.
183 /// This macro enables the use of the [`pin_init!`] macro. When pin-initializing a `struct`,
184 /// then `#[pin]` directs the type of initializer that is required.
186 /// If your `struct` implements `Drop`, then you need to add `PinnedDrop` as arguments to this
187 /// macro, and change your `Drop` implementation to `PinnedDrop` annotated with
188 /// `#[`[`macro@pinned_drop`]`]`, since dropping pinned values requires extra care.
194 /// struct DriverData {
196 /// queue: Mutex<Vec<Command>>,
197 /// buf: Box<[u8; 1024 * 1024]>,
202 /// #[pin_data(PinnedDrop)]
203 /// struct DriverData {
205 /// queue: Mutex<Vec<Command>>,
206 /// buf: Box<[u8; 1024 * 1024]>,
207 /// raw_info: *mut Info,
211 /// impl PinnedDrop for DriverData {
212 /// fn drop(self: Pin<&mut Self>) {
213 /// unsafe { bindings::destroy_info(self.raw_info) };
218 /// [`pin_init!`]: ../kernel/macro.pin_init.html
219 // ^ cannot use direct link, since `kernel` is not a dependency of `macros`.
220 #[proc_macro_attribute]
221 pub fn pin_data(inner: TokenStream, item: TokenStream) -> TokenStream {
222 pin_data::pin_data(inner, item)
225 /// Used to implement `PinnedDrop` safely.
227 /// Only works on structs that are annotated via `#[`[`macro@pin_data`]`]`.
232 /// #[pin_data(PinnedDrop)]
233 /// struct DriverData {
235 /// queue: Mutex<Vec<Command>>,
236 /// buf: Box<[u8; 1024 * 1024]>,
237 /// raw_info: *mut Info,
241 /// impl PinnedDrop for DriverData {
242 /// fn drop(self: Pin<&mut Self>) {
243 /// unsafe { bindings::destroy_info(self.raw_info) };
247 #[proc_macro_attribute]
248 pub fn pinned_drop(args: TokenStream, input: TokenStream) -> TokenStream {
249 pinned_drop::pinned_drop(args, input)
252 /// Paste identifiers together.
254 /// Within the `paste!` macro, identifiers inside `[<` and `>]` are concatenated together to form a
255 /// single identifier.
257 /// This is similar to the [`paste`] crate, but with pasting feature limited to identifiers
258 /// (literals, lifetimes and documentation strings are not supported). There is a difference in
259 /// supported modifiers as well.
264 /// use kernel::macro::paste;
266 /// macro_rules! pub_no_prefix {
267 /// ($prefix:ident, $($newname:ident),+) => {
269 /// $(pub(crate) const $newname: u32 = [<$prefix $newname>];)+
275 /// binder_driver_return_protocol_,
281 /// BR_TRANSACTION_COMPLETE,
289 /// BR_CLEAR_DEATH_NOTIFICATION_DONE,
293 /// assert_eq!(BR_OK, binder_driver_return_protocol_BR_OK);
298 /// For each identifier, it is possible to attach one or multiple modifiers to
301 /// Currently supported modifiers are:
302 /// * `span`: change the span of concatenated identifier to the span of the specified token. By
303 /// default the span of the `[< >]` group is used.
304 /// * `lower`: change the identifier to lower case.
305 /// * `upper`: change the identifier to upper case.
308 /// use kernel::macro::paste;
310 /// macro_rules! pub_no_prefix {
311 /// ($prefix:ident, $($newname:ident),+) => {
312 /// kernel::macros::paste! {
313 /// $(pub(crate) const fn [<$newname:lower:span>]: u32 = [<$prefix $newname:span>];)+
319 /// binder_driver_return_protocol_,
325 /// BR_TRANSACTION_COMPLETE,
333 /// BR_CLEAR_DEATH_NOTIFICATION_DONE,
337 /// assert_eq!(br_ok(), binder_driver_return_protocol_BR_OK);
340 /// [`paste`]: https://docs.rs/paste/
342 pub fn paste(input: TokenStream) -> TokenStream {
343 let mut tokens = input.into_iter().collect();
344 paste::expand(&mut tokens);
345 tokens.into_iter().collect()
348 /// Derives the [`Zeroable`] trait for the given struct.
350 /// This can only be used for structs where every field implements the [`Zeroable`] trait.
355 /// #[derive(Zeroable)]
356 /// pub struct DriverData {
358 /// buf_ptr: *mut u8,
362 #[proc_macro_derive(Zeroable)]
363 pub fn derive_zeroable(input: TokenStream) -> TokenStream {
364 zeroable::derive(input)