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_BLOCK)]
42pub mod block;
43#[doc(hidden)]
44pub mod build_assert;
45pub mod cred;
46pub mod device;
47pub mod device_id;
48pub mod devres;
49pub mod dma;
50pub mod driver;
51pub mod error;
52pub mod faux;
53#[cfg(CONFIG_RUST_FW_LOADER_ABSTRACTIONS)]
54pub mod firmware;
55pub mod fs;
56pub mod init;
57pub mod io;
58pub mod ioctl;
59pub mod jump_label;
60#[cfg(CONFIG_KUNIT)]
61pub mod kunit;
62pub mod list;
63pub mod miscdevice;
64#[cfg(CONFIG_NET)]
65pub mod net;
66pub mod of;
67pub mod page;
68#[cfg(CONFIG_PCI)]
69pub mod pci;
70pub mod pid_namespace;
71pub mod platform;
72pub mod prelude;
73pub mod print;
74pub mod rbtree;
75pub mod revocable;
76pub mod security;
77pub mod seq_file;
78pub mod sizes;
79mod static_assert;
80#[doc(hidden)]
81pub mod std_vendor;
82pub mod str;
83pub mod sync;
84pub mod task;
85pub mod time;
86pub mod tracepoint;
87pub mod transmute;
88pub mod types;
89pub mod uaccess;
90pub mod workqueue;
91
92#[doc(hidden)]
93pub use bindings;
94pub use macros;
95pub use uapi;
96
97const __LOG_PREFIX: &[u8] = b"rust_kernel\0";
99
100pub trait Module: Sized + Sync + Send {
104    fn init(module: &'static ThisModule) -> error::Result<Self>;
111}
112
113pub trait InPlaceModule: Sync + Send {
115    fn init(module: &'static ThisModule) -> impl pin_init::PinInit<Self, error::Error>;
119}
120
121impl<T: Module> InPlaceModule for T {
122    fn init(module: &'static ThisModule) -> impl pin_init::PinInit<Self, error::Error> {
123        let initer = move |slot: *mut Self| {
124            let m = <Self as Module>::init(module)?;
125
126            unsafe { slot.write(m) };
128            Ok(())
129        };
130
131        unsafe { pin_init::pin_init_from_closure(initer) }
133    }
134}
135
136pub trait ModuleMetadata {
138    const NAME: &'static crate::str::CStr;
140}
141
142pub struct ThisModule(*mut bindings::module);
146
147unsafe impl Sync for ThisModule {}
149
150impl ThisModule {
151    pub const unsafe fn from_ptr(ptr: *mut bindings::module) -> ThisModule {
157        ThisModule(ptr)
158    }
159
160    pub const fn as_ptr(&self) -> *mut bindings::module {
164        self.0
165    }
166}
167
168#[cfg(not(any(testlib, test)))]
169#[panic_handler]
170fn panic(info: &core::panic::PanicInfo<'_>) -> ! {
171    pr_emerg!("{}\n", info);
172    unsafe { bindings::BUG() };
174}
175
176#[macro_export]
200macro_rules! container_of {
201    ($ptr:expr, $type:ty, $($f:tt)*) => {{
202        let ptr = $ptr as *const _ as *const u8;
203        let offset: usize = ::core::mem::offset_of!($type, $($f)*);
204        ptr.sub(offset) as *const $type
205    }}
206}
207
208#[doc(hidden)]
210#[macro_export]
211macro_rules! concat_literals {
212    ($( $asm:literal )* ) => {
213        ::core::concat!($($asm),*)
214    };
215}
216
217#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
223#[macro_export]
224macro_rules! asm {
225    ($($asm:expr),* ; $($rest:tt)*) => {
226        ::core::arch::asm!( $($asm)*, options(att_syntax), $($rest)* )
227    };
228}
229
230#[cfg(not(any(target_arch = "x86", target_arch = "x86_64")))]
236#[macro_export]
237macro_rules! asm {
238    ($($asm:expr),* ; $($rest:tt)*) => {
239        ::core::arch::asm!( $($asm)*, $($rest)* )
240    };
241}