xref: /linux/rust/kernel/lib.rs (revision 98f21c54f99519329c18e2625b0ea6db14524d09)
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))]
39 compile_error!("Missing kernel configuration for conditional compilation");
40 
41 // Allow proc-macros to refer to `::kernel` inside the `kernel` crate (this crate).
42 extern crate self as kernel;
43 
44 pub use ffi;
45 
46 pub mod acpi;
47 pub mod alloc;
48 #[cfg(CONFIG_AUXILIARY_BUS)]
49 pub mod auxiliary;
50 pub mod bitfield;
51 pub mod bitmap;
52 pub mod bits;
53 #[cfg(CONFIG_BLOCK)]
54 pub mod block;
55 pub mod bug;
56 pub mod build_assert;
57 pub mod clk;
58 #[cfg(CONFIG_CONFIGFS_FS)]
59 pub mod configfs;
60 pub mod cpu;
61 #[cfg(CONFIG_CPU_FREQ)]
62 pub mod cpufreq;
63 pub mod cpumask;
64 pub mod cred;
65 pub mod debugfs;
66 pub mod device;
67 pub mod device_id;
68 pub mod devres;
69 pub mod dma;
70 pub mod driver;
71 #[cfg(CONFIG_DRM = "y")]
72 pub mod drm;
73 pub mod error;
74 pub mod faux;
75 #[cfg(CONFIG_RUST_FW_LOADER_ABSTRACTIONS)]
76 pub mod firmware;
77 pub mod fmt;
78 pub mod fs;
79 #[cfg(CONFIG_RUST_FWCTL_ABSTRACTIONS)]
80 pub mod fwctl;
81 #[cfg(CONFIG_GPU_BUDDY = "y")]
82 pub mod gpu;
83 #[cfg(CONFIG_I2C = "y")]
84 pub mod i2c;
85 pub mod id_pool;
86 #[doc(hidden)]
87 pub mod impl_flags;
88 pub mod init;
89 pub mod interop;
90 pub mod interrupt;
91 pub mod io;
92 pub mod ioctl;
93 pub mod iommu;
94 pub mod iov;
95 pub mod irq;
96 pub mod jump_label;
97 #[cfg(CONFIG_KUNIT)]
98 pub mod kunit;
99 pub mod list;
100 pub mod maple_tree;
101 pub mod miscdevice;
102 pub mod mm;
103 pub mod module;
104 pub mod module_param;
105 #[cfg(CONFIG_NET)]
106 pub mod net;
107 pub mod num;
108 pub mod of;
109 #[cfg(CONFIG_PM_OPP)]
110 pub mod opp;
111 pub mod page;
112 #[cfg(CONFIG_PCI)]
113 pub mod pci;
114 pub mod pid_namespace;
115 pub mod platform;
116 pub mod prelude;
117 pub mod print;
118 pub mod processor;
119 pub mod ptr;
120 #[cfg(CONFIG_RUST_PWM_ABSTRACTIONS)]
121 pub mod pwm;
122 pub mod rbtree;
123 pub mod regulator;
124 pub mod revocable;
125 pub mod safety;
126 pub mod scatterlist;
127 pub mod security;
128 pub mod seq_file;
129 #[cfg(CONFIG_RUST_SERIAL_DEV_BUS_ABSTRACTIONS)]
130 pub mod serdev;
131 pub mod sizes;
132 #[cfg(CONFIG_SOC_BUS)]
133 pub mod soc;
134 #[doc(hidden)]
135 pub mod std_vendor;
136 pub mod str;
137 pub mod sync;
138 pub mod task;
139 pub mod time;
140 pub mod tracepoint;
141 pub mod transmute;
142 pub mod types;
143 pub mod uaccess;
144 #[cfg(CONFIG_USB = "y")]
145 pub mod usb;
146 pub mod workqueue;
147 pub mod xarray;
148 
149 #[doc(hidden)]
150 pub use bindings;
151 pub use macros;
152 pub use module::{
153     InPlaceModule,
154     Module,
155     ModuleMetadata,
156     ThisModule, //
157 };
158 pub use uapi;
159 
160 /// Prefix to appear before log messages printed from within the `kernel` crate.
161 const __LOG_PREFIX: &[u8] = b"rust_kernel\0";
162 
163 /// Dummy module type for `#[vtable]` `impl` blocks within the `kernel` crate (e.g. KUnit tests).
164 // The `allow` is needed since it may be unused (e.g. KUnit tests may be disabled).
165 #[allow(dead_code)]
166 struct LocalModule;
167 
168 impl ModuleMetadata for LocalModule {
169     const NAME: &'static str::CStr = c"rust_kernel";
170 
171     const THIS_MODULE: ThisModule = {
172         // SAFETY: `try_module_get`/`module_put` handle null module pointers gracefully.
173         unsafe { ThisModule::from_ptr(core::ptr::null_mut()) }
174     };
175 }
176 
177 #[cfg(not(testlib))]
178 #[panic_handler]
179 fn panic(info: &core::panic::PanicInfo<'_>) -> ! {
180     pr_emerg!("{}\n", info);
181     // SAFETY: FFI call.
182     unsafe { bindings::BUG() };
183 }
184 
185 /// Produces a pointer to an object from a pointer to one of its fields.
186 ///
187 /// If you encounter a type mismatch due to the [`Opaque`] type, then use [`Opaque::cast_into`] or
188 /// [`Opaque::cast_from`] to resolve the mismatch.
189 ///
190 /// [`Opaque`]: crate::types::Opaque
191 /// [`Opaque::cast_into`]: crate::types::Opaque::cast_into
192 /// [`Opaque::cast_from`]: crate::types::Opaque::cast_from
193 ///
194 /// # Safety
195 ///
196 /// The pointer passed to this macro, and the pointer returned by this macro, must both be in
197 /// bounds of the same allocation.
198 ///
199 /// # Examples
200 ///
201 /// ```
202 /// # use kernel::container_of;
203 /// struct Test {
204 ///     a: u64,
205 ///     b: u32,
206 /// }
207 ///
208 /// let test = Test { a: 10, b: 20 };
209 /// let b_ptr: *const _ = &test.b;
210 /// // SAFETY: The pointer points at the `b` field of a `Test`, so the resulting pointer will be
211 /// // in-bounds of the same allocation as `b_ptr`.
212 /// let test_alias = unsafe { container_of!(b_ptr, Test, b) };
213 /// assert!(core::ptr::eq(&test, test_alias));
214 /// ```
215 #[macro_export]
216 macro_rules! container_of {
217     ($field_ptr:expr, $Container:ty, $($fields:tt)*) => {{
218         let offset: usize = ::core::mem::offset_of!($Container, $($fields)*);
219         let field_ptr = $field_ptr;
220         let container_ptr = field_ptr.byte_sub(offset).cast::<$Container>();
221         $crate::assert_same_type(field_ptr, (&raw const (*container_ptr).$($fields)*).cast_mut());
222         container_ptr
223     }}
224 }
225 
226 /// Helper for [`container_of!`].
227 #[doc(hidden)]
228 pub fn assert_same_type<T>(_: T, _: T) {}
229 
230 /// Helper for `.rs.S` files.
231 #[doc(hidden)]
232 #[macro_export]
233 macro_rules! concat_literals {
234     ($( $asm:literal )* ) => {
235         ::core::concat!($($asm),*)
236     };
237 }
238 
239 /// Wrapper around `asm!` configured for use in the kernel.
240 ///
241 /// Uses a semicolon to avoid parsing ambiguities, even though this does not match native `asm!`
242 /// syntax.
243 // For x86, `asm!` uses intel syntax by default, but we want to use at&t syntax in the kernel.
244 #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
245 #[macro_export]
246 macro_rules! asm {
247     ($($asm:expr),* ; $($rest:tt)*) => {
248         ::core::arch::asm!( $($asm)*, options(att_syntax), $($rest)* )
249     };
250 }
251 
252 /// Wrapper around `asm!` configured for use in the kernel.
253 ///
254 /// Uses a semicolon to avoid parsing ambiguities, even though this does not match native `asm!`
255 /// syntax.
256 // For non-x86 arches we just pass through to `asm!`.
257 #[cfg(not(any(target_arch = "x86", target_arch = "x86_64")))]
258 #[macro_export]
259 macro_rules! asm {
260     ($($asm:expr),* ; $($rest:tt)*) => {
261         ::core::arch::asm!( $($asm)*, $($rest)* )
262     };
263 }
264 
265 /// Gets the C string file name of a [`Location`].
266 ///
267 /// If `Location::file_as_c_str()` is not available, returns a string that warns about it.
268 ///
269 /// [`Location`]: core::panic::Location
270 ///
271 /// # Examples
272 ///
273 /// ```
274 /// # use kernel::file_from_location;
275 ///
276 /// #[track_caller]
277 /// fn foo() {
278 ///     let caller = core::panic::Location::caller();
279 ///
280 ///     // Output:
281 ///     // - A path like "rust/kernel/example.rs" if `file_as_c_str()` is available.
282 ///     // - "<Location::file_as_c_str() not supported>" otherwise.
283 ///     let caller_file = file_from_location(caller);
284 ///
285 ///     // Prints out the message with caller's file name.
286 ///     pr_info!("foo() called in file {caller_file:?}\n");
287 ///
288 ///     # if cfg!(CONFIG_RUSTC_HAS_FILE_WITH_NUL) {
289 ///     #     assert_eq!(Ok(caller.file()), caller_file.to_str());
290 ///     # }
291 /// }
292 ///
293 /// # foo();
294 /// ```
295 #[inline]
296 pub fn file_from_location<'a>(loc: &'a core::panic::Location<'a>) -> &'a core::ffi::CStr {
297     #[cfg(CONFIG_RUSTC_HAS_FILE_AS_C_STR)]
298     {
299         loc.file_as_c_str()
300     }
301 
302     #[cfg(all(CONFIG_RUSTC_HAS_FILE_WITH_NUL, not(CONFIG_RUSTC_HAS_FILE_AS_C_STR)))]
303     {
304         loc.file_with_nul()
305     }
306 
307     #[cfg(not(CONFIG_RUSTC_HAS_FILE_WITH_NUL))]
308     {
309         let _ = loc;
310         c"<Location::file_as_c_str() not supported>"
311     }
312 }
313