kernel/lib.rs
1// SPDX-License-Identifier: GPL-2.0
2
3//! The `kernel` crate.
4//!
5//! This crate contains the kernel APIs that have been ported or wrapped for
6//! usage by Rust code in the kernel and is shared by all of them.
7//!
8//! In other words, all the rest of the Rust code in the kernel (e.g. kernel
9//! modules written in Rust) depends on [`core`] and this crate.
10//!
11//! If you need a kernel C API that is not ported or wrapped yet here, then
12//! do so first instead of bypassing this crate.
13
14#![no_std]
15//
16// Please see https://github.com/Rust-for-Linux/linux/issues/2 for details on
17// the unstable features in use.
18//
19// Stable since Rust 1.87.0.
20#![feature(unsigned_is_multiple_of)]
21//
22// Stable since Rust 1.89.0.
23#![feature(generic_arg_infer)]
24//
25// Expected to become stable.
26#![feature(arbitrary_self_types)]
27#![feature(derive_coerce_pointee)]
28//
29// To be determined.
30#![feature(used_with_arg)]
31//
32// `feature(file_with_nul)` is stable since Rust 1.92.0. Before Rust 1.89.0, it did not exist, so
33// enable it conditionally.
34#![cfg_attr(CONFIG_RUSTC_HAS_FILE_WITH_NUL, feature(file_with_nul))]
35
36// Ensure conditional compilation based on the kernel configuration works;
37// otherwise we may silently break things like initcall handling.
38#[cfg(not(CONFIG_RUST))]
39compile_error!("Missing kernel configuration for conditional compilation");
40
41// Allow proc-macros to refer to `::kernel` inside the `kernel` crate (this crate).
42extern crate self as kernel;
43
44pub use ffi;
45
46pub mod acpi;
47pub mod alloc;
48#[cfg(CONFIG_AUXILIARY_BUS)]
49pub mod auxiliary;
50pub mod bitfield;
51pub mod bitmap;
52pub mod bits;
53#[cfg(CONFIG_BLOCK)]
54pub mod block;
55pub mod bug;
56pub mod build_assert;
57pub mod clk;
58#[cfg(CONFIG_CONFIGFS_FS)]
59pub mod configfs;
60pub mod cpu;
61#[cfg(CONFIG_CPU_FREQ)]
62pub mod cpufreq;
63pub mod cpumask;
64pub mod cred;
65pub mod debugfs;
66pub mod device;
67pub mod device_id;
68pub mod devres;
69pub mod dma;
70pub mod driver;
71#[cfg(CONFIG_DRM = "y")]
72pub mod drm;
73pub mod error;
74pub mod faux;
75#[cfg(CONFIG_RUST_FW_LOADER_ABSTRACTIONS)]
76pub mod firmware;
77pub mod fmt;
78pub mod fs;
79#[cfg(CONFIG_GPU_BUDDY = "y")]
80pub mod gpu;
81#[cfg(CONFIG_I2C = "y")]
82pub mod i2c;
83pub mod id_pool;
84#[doc(hidden)]
85pub mod impl_flags;
86pub mod init;
87pub mod interop;
88pub mod io;
89pub mod ioctl;
90pub mod iommu;
91pub mod iov;
92pub mod irq;
93pub mod jump_label;
94#[cfg(CONFIG_KUNIT)]
95pub mod kunit;
96pub mod list;
97pub mod maple_tree;
98pub mod miscdevice;
99pub mod mm;
100pub mod module_param;
101#[cfg(CONFIG_NET)]
102pub mod net;
103pub mod num;
104pub mod of;
105#[cfg(CONFIG_PM_OPP)]
106pub mod opp;
107pub mod page;
108#[cfg(CONFIG_PCI)]
109pub mod pci;
110pub mod pid_namespace;
111pub mod platform;
112pub mod prelude;
113pub mod print;
114pub mod processor;
115pub mod ptr;
116#[cfg(CONFIG_RUST_PWM_ABSTRACTIONS)]
117pub mod pwm;
118pub mod rbtree;
119pub mod regulator;
120pub mod revocable;
121pub mod safety;
122pub mod scatterlist;
123pub mod security;
124pub mod seq_file;
125#[cfg(CONFIG_RUST_SERIAL_DEV_BUS_ABSTRACTIONS)]
126pub mod serdev;
127pub mod sizes;
128#[cfg(CONFIG_SOC_BUS)]
129pub mod soc;
130#[doc(hidden)]
131pub mod std_vendor;
132pub mod str;
133pub mod sync;
134pub mod task;
135pub mod time;
136pub mod tracepoint;
137pub mod transmute;
138pub mod types;
139pub mod uaccess;
140#[cfg(CONFIG_USB = "y")]
141pub mod usb;
142pub mod workqueue;
143pub mod xarray;
144
145#[doc(hidden)]
146pub use bindings;
147pub use macros;
148pub use uapi;
149
150/// Prefix to appear before log messages printed from within the `kernel` crate.
151const __LOG_PREFIX: &[u8] = b"rust_kernel\0";
152
153/// The top level entrypoint to implementing a kernel module.
154///
155/// For any teardown or cleanup operations, your type may implement [`Drop`].
156pub trait Module: Sized + Sync + Send {
157 /// Called at module initialization time.
158 ///
159 /// Use this method to perform whatever setup or registration your module
160 /// should do.
161 ///
162 /// Equivalent to the `module_init` macro in the C API.
163 fn init(module: &'static ThisModule) -> error::Result<Self>;
164}
165
166/// A module that is pinned and initialised in-place.
167pub trait InPlaceModule: Sync + Send {
168 /// Creates an initialiser for the module.
169 ///
170 /// It is called when the module is loaded.
171 fn init(module: &'static ThisModule) -> impl pin_init::PinInit<Self, error::Error>;
172}
173
174impl<T: Module> InPlaceModule for T {
175 fn init(module: &'static ThisModule) -> impl pin_init::PinInit<Self, error::Error> {
176 let initer = move |slot: *mut Self| {
177 let m = <Self as Module>::init(module)?;
178
179 // SAFETY: `slot` is valid for write per the contract with `pin_init_from_closure`.
180 unsafe { slot.write(m) };
181 Ok(())
182 };
183
184 // SAFETY: On success, `initer` always fully initialises an instance of `Self`.
185 unsafe { pin_init::pin_init_from_closure(initer) }
186 }
187}
188
189/// Metadata attached to a [`Module`] or [`InPlaceModule`].
190pub trait ModuleMetadata {
191 /// The name of the module as specified in the `module!` macro.
192 const NAME: &'static crate::str::CStr;
193}
194
195/// Equivalent to `THIS_MODULE` in the C API.
196///
197/// C header: [`include/linux/init.h`](srctree/include/linux/init.h)
198pub struct ThisModule(*mut bindings::module);
199
200// SAFETY: `THIS_MODULE` may be used from all threads within a module.
201unsafe impl Sync for ThisModule {}
202
203impl ThisModule {
204 /// Creates a [`ThisModule`] given the `THIS_MODULE` pointer.
205 ///
206 /// # Safety
207 ///
208 /// The pointer must be equal to the right `THIS_MODULE`.
209 pub const unsafe fn from_ptr(ptr: *mut bindings::module) -> ThisModule {
210 ThisModule(ptr)
211 }
212
213 /// Access the raw pointer for this module.
214 ///
215 /// It is up to the user to use it correctly.
216 pub const fn as_ptr(&self) -> *mut bindings::module {
217 self.0
218 }
219}
220
221#[cfg(not(testlib))]
222#[panic_handler]
223fn panic(info: &core::panic::PanicInfo<'_>) -> ! {
224 pr_emerg!("{}\n", info);
225 // SAFETY: FFI call.
226 unsafe { bindings::BUG() };
227}
228
229/// Produces a pointer to an object from a pointer to one of its fields.
230///
231/// If you encounter a type mismatch due to the [`Opaque`] type, then use [`Opaque::cast_into`] or
232/// [`Opaque::cast_from`] to resolve the mismatch.
233///
234/// [`Opaque`]: crate::types::Opaque
235/// [`Opaque::cast_into`]: crate::types::Opaque::cast_into
236/// [`Opaque::cast_from`]: crate::types::Opaque::cast_from
237///
238/// # Safety
239///
240/// The pointer passed to this macro, and the pointer returned by this macro, must both be in
241/// bounds of the same allocation.
242///
243/// # Examples
244///
245/// ```
246/// # use kernel::container_of;
247/// struct Test {
248/// a: u64,
249/// b: u32,
250/// }
251///
252/// let test = Test { a: 10, b: 20 };
253/// let b_ptr: *const _ = &test.b;
254/// // SAFETY: The pointer points at the `b` field of a `Test`, so the resulting pointer will be
255/// // in-bounds of the same allocation as `b_ptr`.
256/// let test_alias = unsafe { container_of!(b_ptr, Test, b) };
257/// assert!(core::ptr::eq(&test, test_alias));
258/// ```
259#[macro_export]
260macro_rules! container_of {
261 ($field_ptr:expr, $Container:ty, $($fields:tt)*) => {{
262 let offset: usize = ::core::mem::offset_of!($Container, $($fields)*);
263 let field_ptr = $field_ptr;
264 let container_ptr = field_ptr.byte_sub(offset).cast::<$Container>();
265 $crate::assert_same_type(field_ptr, (&raw const (*container_ptr).$($fields)*).cast_mut());
266 container_ptr
267 }}
268}
269
270/// Helper for [`container_of!`].
271#[doc(hidden)]
272pub fn assert_same_type<T>(_: T, _: T) {}
273
274/// Helper for `.rs.S` files.
275#[doc(hidden)]
276#[macro_export]
277macro_rules! concat_literals {
278 ($( $asm:literal )* ) => {
279 ::core::concat!($($asm),*)
280 };
281}
282
283/// Wrapper around `asm!` configured for use in the kernel.
284///
285/// Uses a semicolon to avoid parsing ambiguities, even though this does not match native `asm!`
286/// syntax.
287// For x86, `asm!` uses intel syntax by default, but we want to use at&t syntax in the kernel.
288#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
289#[macro_export]
290macro_rules! asm {
291 ($($asm:expr),* ; $($rest:tt)*) => {
292 ::core::arch::asm!( $($asm)*, options(att_syntax), $($rest)* )
293 };
294}
295
296/// Wrapper around `asm!` configured for use in the kernel.
297///
298/// Uses a semicolon to avoid parsing ambiguities, even though this does not match native `asm!`
299/// syntax.
300// For non-x86 arches we just pass through to `asm!`.
301#[cfg(not(any(target_arch = "x86", target_arch = "x86_64")))]
302#[macro_export]
303macro_rules! asm {
304 ($($asm:expr),* ; $($rest:tt)*) => {
305 ::core::arch::asm!( $($asm)*, $($rest)* )
306 };
307}
308
309/// Gets the C string file name of a [`Location`].
310///
311/// If `Location::file_as_c_str()` is not available, returns a string that warns about it.
312///
313/// [`Location`]: core::panic::Location
314///
315/// # Examples
316///
317/// ```
318/// # use kernel::file_from_location;
319///
320/// #[track_caller]
321/// fn foo() {
322/// let caller = core::panic::Location::caller();
323///
324/// // Output:
325/// // - A path like "rust/kernel/example.rs" if `file_as_c_str()` is available.
326/// // - "<Location::file_as_c_str() not supported>" otherwise.
327/// let caller_file = file_from_location(caller);
328///
329/// // Prints out the message with caller's file name.
330/// pr_info!("foo() called in file {caller_file:?}\n");
331///
332/// # if cfg!(CONFIG_RUSTC_HAS_FILE_WITH_NUL) {
333/// # assert_eq!(Ok(caller.file()), caller_file.to_str());
334/// # }
335/// }
336///
337/// # foo();
338/// ```
339#[inline]
340pub fn file_from_location<'a>(loc: &'a core::panic::Location<'a>) -> &'a core::ffi::CStr {
341 #[cfg(CONFIG_RUSTC_HAS_FILE_AS_C_STR)]
342 {
343 loc.file_as_c_str()
344 }
345
346 #[cfg(all(CONFIG_RUSTC_HAS_FILE_WITH_NUL, not(CONFIG_RUSTC_HAS_FILE_AS_C_STR)))]
347 {
348 loc.file_with_nul()
349 }
350
351 #[cfg(not(CONFIG_RUSTC_HAS_FILE_WITH_NUL))]
352 {
353 let _ = loc;
354 c"<Location::file_as_c_str() not supported>"
355 }
356}