xref: /linux/rust/kernel/lib.rs (revision 7f15c46a57c31956591f85b713d7e63cccb25556)
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`], [`alloc`] 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 #![feature(coerce_unsized)]
16 #![feature(dispatch_from_dyn)]
17 #![feature(new_uninit)]
18 #![feature(receiver_trait)]
19 #![feature(unsize)]
20 
21 // Ensure conditional compilation based on the kernel configuration works;
22 // otherwise we may silently break things like initcall handling.
23 #[cfg(not(CONFIG_RUST))]
24 compile_error!("Missing kernel configuration for conditional compilation");
25 
26 // Allow proc-macros to refer to `::kernel` inside the `kernel` crate (this crate).
27 extern crate self as kernel;
28 
29 pub mod alloc;
30 #[cfg(CONFIG_BLOCK)]
31 pub mod block;
32 mod build_assert;
33 pub mod device;
34 pub mod error;
35 #[cfg(CONFIG_RUST_FW_LOADER_ABSTRACTIONS)]
36 pub mod firmware;
37 pub mod init;
38 pub mod ioctl;
39 #[cfg(CONFIG_KUNIT)]
40 pub mod kunit;
41 pub mod list;
42 pub mod miscdevice;
43 #[cfg(CONFIG_NET)]
44 pub mod net;
45 pub mod page;
46 pub mod prelude;
47 pub mod print;
48 pub mod rbtree;
49 pub mod sizes;
50 mod static_assert;
51 #[doc(hidden)]
52 pub mod std_vendor;
53 pub mod str;
54 pub mod sync;
55 pub mod task;
56 pub mod time;
57 pub mod types;
58 pub mod uaccess;
59 pub mod workqueue;
60 
61 #[doc(hidden)]
62 pub use bindings;
63 pub use macros;
64 pub use uapi;
65 
66 #[doc(hidden)]
67 pub use build_error::build_error;
68 
69 /// Prefix to appear before log messages printed from within the `kernel` crate.
70 const __LOG_PREFIX: &[u8] = b"rust_kernel\0";
71 
72 /// The top level entrypoint to implementing a kernel module.
73 ///
74 /// For any teardown or cleanup operations, your type may implement [`Drop`].
75 pub trait Module: Sized + Sync + Send {
76     /// Called at module initialization time.
77     ///
78     /// Use this method to perform whatever setup or registration your module
79     /// should do.
80     ///
81     /// Equivalent to the `module_init` macro in the C API.
82     fn init(module: &'static ThisModule) -> error::Result<Self>;
83 }
84 
85 /// A module that is pinned and initialised in-place.
86 pub trait InPlaceModule: Sync + Send {
87     /// Creates an initialiser for the module.
88     ///
89     /// It is called when the module is loaded.
90     fn init(module: &'static ThisModule) -> impl init::PinInit<Self, error::Error>;
91 }
92 
93 impl<T: Module> InPlaceModule for T {
94     fn init(module: &'static ThisModule) -> impl init::PinInit<Self, error::Error> {
95         let initer = move |slot: *mut Self| {
96             let m = <Self as Module>::init(module)?;
97 
98             // SAFETY: `slot` is valid for write per the contract with `pin_init_from_closure`.
99             unsafe { slot.write(m) };
100             Ok(())
101         };
102 
103         // SAFETY: On success, `initer` always fully initialises an instance of `Self`.
104         unsafe { init::pin_init_from_closure(initer) }
105     }
106 }
107 
108 /// Equivalent to `THIS_MODULE` in the C API.
109 ///
110 /// C header: [`include/linux/export.h`](srctree/include/linux/export.h)
111 pub struct ThisModule(*mut bindings::module);
112 
113 // SAFETY: `THIS_MODULE` may be used from all threads within a module.
114 unsafe impl Sync for ThisModule {}
115 
116 impl ThisModule {
117     /// Creates a [`ThisModule`] given the `THIS_MODULE` pointer.
118     ///
119     /// # Safety
120     ///
121     /// The pointer must be equal to the right `THIS_MODULE`.
122     pub const unsafe fn from_ptr(ptr: *mut bindings::module) -> ThisModule {
123         ThisModule(ptr)
124     }
125 
126     /// Access the raw pointer for this module.
127     ///
128     /// It is up to the user to use it correctly.
129     pub const fn as_ptr(&self) -> *mut bindings::module {
130         self.0
131     }
132 }
133 
134 #[cfg(not(any(testlib, test)))]
135 #[panic_handler]
136 fn panic(info: &core::panic::PanicInfo<'_>) -> ! {
137     pr_emerg!("{}\n", info);
138     // SAFETY: FFI call.
139     unsafe { bindings::BUG() };
140 }
141 
142 /// Produces a pointer to an object from a pointer to one of its fields.
143 ///
144 /// # Safety
145 ///
146 /// The pointer passed to this macro, and the pointer returned by this macro, must both be in
147 /// bounds of the same allocation.
148 ///
149 /// # Examples
150 ///
151 /// ```
152 /// # use kernel::container_of;
153 /// struct Test {
154 ///     a: u64,
155 ///     b: u32,
156 /// }
157 ///
158 /// let test = Test { a: 10, b: 20 };
159 /// let b_ptr = &test.b;
160 /// // SAFETY: The pointer points at the `b` field of a `Test`, so the resulting pointer will be
161 /// // in-bounds of the same allocation as `b_ptr`.
162 /// let test_alias = unsafe { container_of!(b_ptr, Test, b) };
163 /// assert!(core::ptr::eq(&test, test_alias));
164 /// ```
165 #[macro_export]
166 macro_rules! container_of {
167     ($ptr:expr, $type:ty, $($f:tt)*) => {{
168         let ptr = $ptr as *const _ as *const u8;
169         let offset: usize = ::core::mem::offset_of!($type, $($f)*);
170         ptr.sub(offset) as *const $type
171     }}
172 }
173