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