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;
61#[cfg(CONFIG_COMMON_CLK)]
62pub mod clk;
63#[cfg(CONFIG_CONFIGFS_FS)]
64pub mod configfs;
65pub mod cpu;
66#[cfg(CONFIG_CPU_FREQ)]
67pub mod cpufreq;
68pub mod cpumask;
69pub mod cred;
70pub mod device;
71pub mod device_id;
72pub mod devres;
73pub mod dma;
74pub mod driver;
75#[cfg(CONFIG_DRM = "y")]
76pub mod drm;
77pub mod error;
78pub mod faux;
79#[cfg(CONFIG_RUST_FW_LOADER_ABSTRACTIONS)]
80pub mod firmware;
81pub mod fs;
82pub mod init;
83pub mod io;
84pub mod ioctl;
85pub mod jump_label;
86#[cfg(CONFIG_KUNIT)]
87pub mod kunit;
88pub mod list;
89pub mod miscdevice;
90pub mod mm;
91#[cfg(CONFIG_NET)]
92pub mod net;
93pub mod of;
94#[cfg(CONFIG_PM_OPP)]
95pub mod opp;
96pub mod page;
97#[cfg(CONFIG_PCI)]
98pub mod pci;
99pub mod pid_namespace;
100pub mod platform;
101pub mod prelude;
102pub mod print;
103pub mod rbtree;
104pub mod revocable;
105pub mod security;
106pub mod seq_file;
107pub mod sizes;
108mod static_assert;
109#[doc(hidden)]
110pub mod std_vendor;
111pub mod str;
112pub mod sync;
113pub mod task;
114pub mod time;
115pub mod tracepoint;
116pub mod transmute;
117pub mod types;
118pub mod uaccess;
119pub mod workqueue;
120pub mod xarray;
121
122#[doc(hidden)]
123pub use bindings;
124pub use macros;
125pub use uapi;
126
127const __LOG_PREFIX: &[u8] = b"rust_kernel\0";
129
130pub trait Module: Sized + Sync + Send {
134 fn init(module: &'static ThisModule) -> error::Result<Self>;
141}
142
143pub trait InPlaceModule: Sync + Send {
145 fn init(module: &'static ThisModule) -> impl pin_init::PinInit<Self, error::Error>;
149}
150
151impl<T: Module> InPlaceModule for T {
152 fn init(module: &'static ThisModule) -> impl pin_init::PinInit<Self, error::Error> {
153 let initer = move |slot: *mut Self| {
154 let m = <Self as Module>::init(module)?;
155
156 unsafe { slot.write(m) };
158 Ok(())
159 };
160
161 unsafe { pin_init::pin_init_from_closure(initer) }
163 }
164}
165
166pub trait ModuleMetadata {
168 const NAME: &'static crate::str::CStr;
170}
171
172pub struct ThisModule(*mut bindings::module);
176
177unsafe impl Sync for ThisModule {}
179
180impl ThisModule {
181 pub const unsafe fn from_ptr(ptr: *mut bindings::module) -> ThisModule {
187 ThisModule(ptr)
188 }
189
190 pub const fn as_ptr(&self) -> *mut bindings::module {
194 self.0
195 }
196}
197
198#[cfg(not(any(testlib, test)))]
199#[panic_handler]
200fn panic(info: &core::panic::PanicInfo<'_>) -> ! {
201 pr_emerg!("{}\n", info);
202 unsafe { bindings::BUG() };
204}
205
206#[macro_export]
230macro_rules! container_of {
231 ($ptr:expr, $type:ty, $($f:tt)*) => {{
232 let ptr = $ptr as *const _ as *const u8;
233 let offset: usize = ::core::mem::offset_of!($type, $($f)*);
234 ptr.sub(offset) as *const $type
235 }}
236}
237
238#[doc(hidden)]
240#[macro_export]
241macro_rules! concat_literals {
242 ($( $asm:literal )* ) => {
243 ::core::concat!($($asm),*)
244 };
245}
246
247#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
253#[macro_export]
254macro_rules! asm {
255 ($($asm:expr),* ; $($rest:tt)*) => {
256 ::core::arch::asm!( $($asm)*, options(att_syntax), $($rest)* )
257 };
258}
259
260#[cfg(not(any(target_arch = "x86", target_arch = "x86_64")))]
266#[macro_export]
267macro_rules! asm {
268 ($($asm:expr),* ; $($rest:tt)*) => {
269 ::core::arch::asm!( $($asm)*, $($rest)* )
270 };
271}