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