1#![no_std]
15#![feature(inline_const)]
21#![feature(lint_reasons)]
24#![feature(raw_ref_op)]
27#![feature(const_maybe_uninit_as_mut_ptr)]
30#![feature(const_mut_refs)]
31#![feature(const_ptr_write)]
32#![feature(const_refs_to_cell)]
33#![feature(arbitrary_self_types)]
36#![cfg_attr(CONFIG_RUSTC_HAS_COERCE_POINTEE, feature(derive_coerce_pointee))]
40#![cfg_attr(not(CONFIG_RUSTC_HAS_COERCE_POINTEE), feature(coerce_unsized))]
41#![cfg_attr(not(CONFIG_RUSTC_HAS_COERCE_POINTEE), feature(dispatch_from_dyn))]
42#![cfg_attr(not(CONFIG_RUSTC_HAS_COERCE_POINTEE), feature(unsize))]
43
44#[cfg(not(CONFIG_RUST))]
47compile_error!("Missing kernel configuration for conditional compilation");
48
49extern crate self as kernel;
51
52pub use ffi;
53
54pub mod alloc;
55#[cfg(CONFIG_AUXILIARY_BUS)]
56pub mod auxiliary;
57#[cfg(CONFIG_BLOCK)]
58pub mod block;
59#[doc(hidden)]
60pub mod build_assert;
61pub mod clk;
62#[cfg(CONFIG_CONFIGFS_FS)]
63pub mod configfs;
64pub mod cpu;
65#[cfg(CONFIG_CPU_FREQ)]
66pub mod cpufreq;
67pub mod cpumask;
68pub mod cred;
69pub mod device;
70pub mod device_id;
71pub mod devres;
72pub mod dma;
73pub mod driver;
74#[cfg(CONFIG_DRM = "y")]
75pub mod drm;
76pub mod error;
77pub mod faux;
78#[cfg(CONFIG_RUST_FW_LOADER_ABSTRACTIONS)]
79pub mod firmware;
80pub mod fs;
81pub mod init;
82pub mod io;
83pub mod ioctl;
84pub mod jump_label;
85#[cfg(CONFIG_KUNIT)]
86pub mod kunit;
87pub mod list;
88pub mod miscdevice;
89pub mod mm;
90#[cfg(CONFIG_NET)]
91pub mod net;
92pub mod of;
93#[cfg(CONFIG_PM_OPP)]
94pub mod opp;
95pub mod page;
96#[cfg(CONFIG_PCI)]
97pub mod pci;
98pub mod pid_namespace;
99pub mod platform;
100pub mod prelude;
101pub mod print;
102pub mod rbtree;
103pub mod revocable;
104pub mod security;
105pub mod seq_file;
106pub mod sizes;
107mod static_assert;
108#[doc(hidden)]
109pub mod std_vendor;
110pub mod str;
111pub mod sync;
112pub mod task;
113pub mod time;
114pub mod tracepoint;
115pub mod transmute;
116pub mod types;
117pub mod uaccess;
118pub mod workqueue;
119pub mod xarray;
120
121#[doc(hidden)]
122pub use bindings;
123pub use macros;
124pub use uapi;
125
126const __LOG_PREFIX: &[u8] = b"rust_kernel\0";
128
129pub trait Module: Sized + Sync + Send {
133 fn init(module: &'static ThisModule) -> error::Result<Self>;
140}
141
142pub trait InPlaceModule: Sync + Send {
144 fn init(module: &'static ThisModule) -> impl pin_init::PinInit<Self, error::Error>;
148}
149
150impl<T: Module> InPlaceModule for T {
151 fn init(module: &'static ThisModule) -> impl pin_init::PinInit<Self, error::Error> {
152 let initer = move |slot: *mut Self| {
153 let m = <Self as Module>::init(module)?;
154
155 unsafe { slot.write(m) };
157 Ok(())
158 };
159
160 unsafe { pin_init::pin_init_from_closure(initer) }
162 }
163}
164
165pub trait ModuleMetadata {
167 const NAME: &'static crate::str::CStr;
169}
170
171pub struct ThisModule(*mut bindings::module);
175
176unsafe impl Sync for ThisModule {}
178
179impl ThisModule {
180 pub const unsafe fn from_ptr(ptr: *mut bindings::module) -> ThisModule {
186 ThisModule(ptr)
187 }
188
189 pub const fn as_ptr(&self) -> *mut bindings::module {
193 self.0
194 }
195}
196
197#[cfg(not(any(testlib, test)))]
198#[panic_handler]
199fn panic(info: &core::panic::PanicInfo<'_>) -> ! {
200 pr_emerg!("{}\n", info);
201 unsafe { bindings::BUG() };
203}
204
205#[macro_export]
229macro_rules! container_of {
230 ($field_ptr:expr, $Container:ty, $($fields:tt)*) => {{
231 let offset: usize = ::core::mem::offset_of!($Container, $($fields)*);
232 let field_ptr = $field_ptr;
233 let container_ptr = field_ptr.byte_sub(offset).cast::<$Container>();
234 $crate::assert_same_type(field_ptr, (&raw const (*container_ptr).$($fields)*).cast_mut());
235 container_ptr
236 }}
237}
238
239#[doc(hidden)]
241pub fn assert_same_type<T>(_: T, _: T) {}
242
243#[doc(hidden)]
245#[macro_export]
246macro_rules! concat_literals {
247 ($( $asm:literal )* ) => {
248 ::core::concat!($($asm),*)
249 };
250}
251
252#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
258#[macro_export]
259macro_rules! asm {
260 ($($asm:expr),* ; $($rest:tt)*) => {
261 ::core::arch::asm!( $($asm)*, options(att_syntax), $($rest)* )
262 };
263}
264
265#[cfg(not(any(target_arch = "x86", target_arch = "x86_64")))]
271#[macro_export]
272macro_rules! asm {
273 ($($asm:expr),* ; $($rest:tt)*) => {
274 ::core::arch::asm!( $($asm)*, $($rest)* )
275 };
276}