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
30#[cfg(not(CONFIG_RUST))]
33compile_error!("Missing kernel configuration for conditional compilation");
34
35extern crate self as kernel;
37
38pub use ffi;
39
40pub mod alloc;
41#[cfg(CONFIG_AUXILIARY_BUS)]
42pub mod auxiliary;
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;
66pub mod mm;
67#[cfg(CONFIG_NET)]
68pub mod net;
69pub mod of;
70pub mod page;
71#[cfg(CONFIG_PCI)]
72pub mod pci;
73pub mod pid_namespace;
74pub mod platform;
75pub mod prelude;
76pub mod print;
77pub mod rbtree;
78pub mod revocable;
79pub mod security;
80pub mod seq_file;
81pub mod sizes;
82mod static_assert;
83#[doc(hidden)]
84pub mod std_vendor;
85pub mod str;
86pub mod sync;
87pub mod task;
88pub mod time;
89pub mod tracepoint;
90pub mod transmute;
91pub mod types;
92pub mod uaccess;
93pub mod workqueue;
94
95#[doc(hidden)]
96pub use bindings;
97pub use macros;
98pub use uapi;
99
100const __LOG_PREFIX: &[u8] = b"rust_kernel\0";
102
103pub trait Module: Sized + Sync + Send {
107 fn init(module: &'static ThisModule) -> error::Result<Self>;
114}
115
116pub trait InPlaceModule: Sync + Send {
118 fn init(module: &'static ThisModule) -> impl pin_init::PinInit<Self, error::Error>;
122}
123
124impl<T: Module> InPlaceModule for T {
125 fn init(module: &'static ThisModule) -> impl pin_init::PinInit<Self, error::Error> {
126 let initer = move |slot: *mut Self| {
127 let m = <Self as Module>::init(module)?;
128
129 unsafe { slot.write(m) };
131 Ok(())
132 };
133
134 unsafe { pin_init::pin_init_from_closure(initer) }
136 }
137}
138
139pub trait ModuleMetadata {
141 const NAME: &'static crate::str::CStr;
143}
144
145pub struct ThisModule(*mut bindings::module);
149
150unsafe impl Sync for ThisModule {}
152
153impl ThisModule {
154 pub const unsafe fn from_ptr(ptr: *mut bindings::module) -> ThisModule {
160 ThisModule(ptr)
161 }
162
163 pub const fn as_ptr(&self) -> *mut bindings::module {
167 self.0
168 }
169}
170
171#[cfg(not(any(testlib, test)))]
172#[panic_handler]
173fn panic(info: &core::panic::PanicInfo<'_>) -> ! {
174 pr_emerg!("{}\n", info);
175 unsafe { bindings::BUG() };
177}
178
179#[macro_export]
203macro_rules! container_of {
204 ($ptr:expr, $type:ty, $($f:tt)*) => {{
205 let ptr = $ptr as *const _ as *const u8;
206 let offset: usize = ::core::mem::offset_of!($type, $($f)*);
207 ptr.sub(offset) as *const $type
208 }}
209}
210
211#[doc(hidden)]
213#[macro_export]
214macro_rules! concat_literals {
215 ($( $asm:literal )* ) => {
216 ::core::concat!($($asm),*)
217 };
218}
219
220#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
226#[macro_export]
227macro_rules! asm {
228 ($($asm:expr),* ; $($rest:tt)*) => {
229 ::core::arch::asm!( $($asm)*, options(att_syntax), $($rest)* )
230 };
231}
232
233#[cfg(not(any(target_arch = "x86", target_arch = "x86_64")))]
239#[macro_export]
240macro_rules! asm {
241 ($($asm:expr),* ; $($rest:tt)*) => {
242 ::core::arch::asm!( $($asm)*, $($rest)* )
243 };
244}