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 #![feature(arbitrary_self_types)]
16 #![feature(asm_goto)]
17 #![feature(coerce_unsized)]
18 #![feature(dispatch_from_dyn)]
19 #![feature(inline_const)]
20 #![feature(lint_reasons)]
21 #![feature(unsize)]
22 #![feature(used_with_arg)]
23 
24 // Ensure conditional compilation based on the kernel configuration works;
25 // otherwise we may silently break things like initcall handling.
26 #[cfg(not(CONFIG_RUST))]
27 compile_error!("Missing kernel configuration for conditional compilation");
28 
29 // Allow proc-macros to refer to `::kernel` inside the `kernel` crate (this crate).
30 extern crate self as kernel;
31 
32 pub use ffi;
33 
34 pub mod alloc;
35 #[cfg(CONFIG_BLOCK)]
36 pub mod block;
37 mod build_assert;
38 pub mod cred;
39 pub mod device;
40 pub mod error;
41 #[cfg(CONFIG_RUST_FW_LOADER_ABSTRACTIONS)]
42 pub mod firmware;
43 pub mod fs;
44 pub mod init;
45 pub mod ioctl;
46 pub mod jump_label;
47 #[cfg(CONFIG_KUNIT)]
48 pub mod kunit;
49 pub mod list;
50 pub mod miscdevice;
51 pub mod mm;
52 #[cfg(CONFIG_NET)]
53 pub mod net;
54 pub mod page;
55 pub mod page_size_compat;
56 pub mod pid_namespace;
57 pub mod prelude;
58 pub mod print;
59 pub mod rbtree;
60 pub mod security;
61 pub mod seq_file;
62 pub mod sizes;
63 mod static_assert;
64 #[doc(hidden)]
65 pub mod std_vendor;
66 pub mod str;
67 pub mod sync;
68 pub mod task;
69 pub mod time;
70 pub mod tracepoint;
71 pub mod types;
72 pub mod uaccess;
73 pub mod workqueue;
74 
75 #[doc(hidden)]
76 pub use bindings;
77 pub use macros;
78 pub use uapi;
79 
80 #[doc(hidden)]
81 pub use build_error::build_error;
82 
83 /// Prefix to appear before log messages printed from within the `kernel` crate.
84 const __LOG_PREFIX: &[u8] = b"rust_kernel\0";
85 
86 /// The top level entrypoint to implementing a kernel module.
87 ///
88 /// For any teardown or cleanup operations, your type may implement [`Drop`].
89 pub trait Module: Sized + Sync + Send {
90     /// Called at module initialization time.
91     ///
92     /// Use this method to perform whatever setup or registration your module
93     /// should do.
94     ///
95     /// Equivalent to the `module_init` macro in the C API.
init(module: &'static ThisModule) -> error::Result<Self>96     fn init(module: &'static ThisModule) -> error::Result<Self>;
97 }
98 
99 /// Equivalent to `THIS_MODULE` in the C API.
100 ///
101 /// C header: [`include/linux/init.h`](srctree/include/linux/init.h)
102 pub struct ThisModule(*mut bindings::module);
103 
104 // SAFETY: `THIS_MODULE` may be used from all threads within a module.
105 unsafe impl Sync for ThisModule {}
106 
107 impl ThisModule {
108     /// Creates a [`ThisModule`] given the `THIS_MODULE` pointer.
109     ///
110     /// # Safety
111     ///
112     /// The pointer must be equal to the right `THIS_MODULE`.
from_ptr(ptr: *mut bindings::module) -> ThisModule113     pub const unsafe fn from_ptr(ptr: *mut bindings::module) -> ThisModule {
114         ThisModule(ptr)
115     }
116 
117     /// Access the raw pointer for this module.
118     ///
119     /// It is up to the user to use it correctly.
as_ptr(&self) -> *mut bindings::module120     pub const fn as_ptr(&self) -> *mut bindings::module {
121         self.0
122     }
123 }
124 
125 #[cfg(not(any(testlib, test)))]
126 #[panic_handler]
panic(info: &core::panic::PanicInfo<'_>) -> !127 fn panic(info: &core::panic::PanicInfo<'_>) -> ! {
128     pr_emerg!("{}\n", info);
129     // SAFETY: FFI call.
130     unsafe { bindings::BUG() };
131 }
132 
133 /// Produces a pointer to an object from a pointer to one of its fields.
134 ///
135 /// # Safety
136 ///
137 /// The pointer passed to this macro, and the pointer returned by this macro, must both be in
138 /// bounds of the same allocation.
139 ///
140 /// # Examples
141 ///
142 /// ```
143 /// # use kernel::container_of;
144 /// struct Test {
145 ///     a: u64,
146 ///     b: u32,
147 /// }
148 ///
149 /// let test = Test { a: 10, b: 20 };
150 /// let b_ptr = &test.b;
151 /// // SAFETY: The pointer points at the `b` field of a `Test`, so the resulting pointer will be
152 /// // in-bounds of the same allocation as `b_ptr`.
153 /// let test_alias = unsafe { container_of!(b_ptr, Test, b) };
154 /// assert!(core::ptr::eq(&test, test_alias));
155 /// ```
156 #[macro_export]
157 macro_rules! container_of {
158     ($ptr:expr, $type:ty, $($f:tt)*) => {{
159         let ptr = $ptr as *const _ as *const u8;
160         let offset: usize = ::core::mem::offset_of!($type, $($f)*);
161         ptr.sub(offset) as *const $type
162     }}
163 }
164 
165 /// Helper for `.rs.S` files.
166 #[doc(hidden)]
167 #[macro_export]
168 macro_rules! concat_literals {
169     ($( $asm:literal )* ) => {
170         ::core::concat!($($asm),*)
171     };
172 }
173 
174 /// Wrapper around `asm!` configured for use in the kernel.
175 ///
176 /// Uses a semicolon to avoid parsing ambiguities, even though this does not match native `asm!`
177 /// syntax.
178 // For x86, `asm!` uses intel syntax by default, but we want to use at&t syntax in the kernel.
179 #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
180 #[macro_export]
181 macro_rules! asm {
182     ($($asm:expr),* ; $($rest:tt)*) => {
183         ::core::arch::asm!( $($asm)*, options(att_syntax), $($rest)* )
184     };
185 }
186 
187 /// Wrapper around `asm!` configured for use in the kernel.
188 ///
189 /// Uses a semicolon to avoid parsing ambiguities, even though this does not match native `asm!`
190 /// syntax.
191 // For non-x86 arches we just pass through to `asm!`.
192 #[cfg(not(any(target_arch = "x86", target_arch = "x86_64")))]
193 #[macro_export]
194 macro_rules! asm {
195     ($($asm:expr),* ; $($rest:tt)*) => {
196         ::core::arch::asm!( $($asm)*, $($rest)* )
197     };
198 }
199