1#![no_std]
15#![feature(arbitrary_self_types)]
16#![cfg_attr(CONFIG_RUSTC_HAS_COERCE_POINTEE, feature(derive_coerce_pointee))]
17#![cfg_attr(not(CONFIG_RUSTC_HAS_COERCE_POINTEE), feature(coerce_unsized))]
18#![cfg_attr(not(CONFIG_RUSTC_HAS_COERCE_POINTEE), feature(dispatch_from_dyn))]
19#![cfg_attr(not(CONFIG_RUSTC_HAS_COERCE_POINTEE), feature(unsize))]
20#![feature(inline_const)]
21#![feature(lint_reasons)]
22#![feature(raw_ref_op)]
24#![feature(const_maybe_uninit_as_mut_ptr)]
26#![feature(const_mut_refs)]
27#![feature(const_ptr_write)]
28#![feature(const_refs_to_cell)]
29#![feature(used_with_arg)]
31
32#[cfg(not(CONFIG_RUST))]
35compile_error!("Missing kernel configuration for conditional compilation");
36
37extern crate self as kernel;
39
40pub use ffi;
41
42pub mod alloc;
43#[cfg(CONFIG_BLOCK)]
44pub mod block;
45#[doc(hidden)]
46pub mod build_assert;
47pub mod cred;
48pub mod device;
49pub mod device_id;
50pub mod devres;
51pub mod dma;
52pub mod driver;
53pub mod error;
54pub mod faux;
55#[cfg(CONFIG_RUST_FW_LOADER_ABSTRACTIONS)]
56pub mod firmware;
57pub mod fs;
58pub mod init;
59pub mod io;
60pub mod ioctl;
61pub mod jump_label;
62#[cfg(CONFIG_KUNIT)]
63pub mod kunit;
64pub mod list;
65pub mod miscdevice;
66#[cfg(CONFIG_NET)]
67pub mod net;
68pub mod of;
69pub mod page;
70#[cfg(CONFIG_PCI)]
71pub mod pci;
72pub mod pid_namespace;
73pub mod platform;
74pub mod prelude;
75pub mod print;
76pub mod rbtree;
77pub mod revocable;
78pub mod security;
79pub mod seq_file;
80pub mod sizes;
81mod static_assert;
82#[doc(hidden)]
83pub mod std_vendor;
84pub mod str;
85pub mod sync;
86pub mod task;
87pub mod time;
88pub mod tracepoint;
89pub mod transmute;
90pub mod types;
91pub mod uaccess;
92pub mod workqueue;
93
94#[doc(hidden)]
95pub use bindings;
96pub use macros;
97pub use uapi;
98
99const __LOG_PREFIX: &[u8] = b"rust_kernel\0";
101
102pub trait Module: Sized + Sync + Send {
106 fn init(module: &'static ThisModule) -> error::Result<Self>;
113}
114
115pub trait InPlaceModule: Sync + Send {
117 fn init(module: &'static ThisModule) -> impl pin_init::PinInit<Self, error::Error>;
121}
122
123impl<T: Module> InPlaceModule for T {
124 fn init(module: &'static ThisModule) -> impl pin_init::PinInit<Self, error::Error> {
125 let initer = move |slot: *mut Self| {
126 let m = <Self as Module>::init(module)?;
127
128 unsafe { slot.write(m) };
130 Ok(())
131 };
132
133 unsafe { pin_init::pin_init_from_closure(initer) }
135 }
136}
137
138pub trait ModuleMetadata {
140 const NAME: &'static crate::str::CStr;
142}
143
144pub struct ThisModule(*mut bindings::module);
148
149unsafe impl Sync for ThisModule {}
151
152impl ThisModule {
153 pub const unsafe fn from_ptr(ptr: *mut bindings::module) -> ThisModule {
159 ThisModule(ptr)
160 }
161
162 pub const fn as_ptr(&self) -> *mut bindings::module {
166 self.0
167 }
168}
169
170#[cfg(not(any(testlib, test)))]
171#[panic_handler]
172fn panic(info: &core::panic::PanicInfo<'_>) -> ! {
173 pr_emerg!("{}\n", info);
174 unsafe { bindings::BUG() };
176}
177
178#[macro_export]
202macro_rules! container_of {
203 ($ptr:expr, $type:ty, $($f:tt)*) => {{
204 let ptr = $ptr as *const _ as *const u8;
205 let offset: usize = ::core::mem::offset_of!($type, $($f)*);
206 ptr.sub(offset) as *const $type
207 }}
208}
209
210#[doc(hidden)]
212#[macro_export]
213macro_rules! concat_literals {
214 ($( $asm:literal )* ) => {
215 ::core::concat!($($asm),*)
216 };
217}
218
219#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
225#[macro_export]
226macro_rules! asm {
227 ($($asm:expr),* ; $($rest:tt)*) => {
228 ::core::arch::asm!( $($asm)*, options(att_syntax), $($rest)* )
229 };
230}
231
232#[cfg(not(any(target_arch = "x86", target_arch = "x86_64")))]
238#[macro_export]
239macro_rules! asm {
240 ($($asm:expr),* ; $($rest:tt)*) => {
241 ::core::arch::asm!( $($asm)*, $($rest)* )
242 };
243}