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(const_maybe_uninit_as_mut_ptr)]
24#![feature(const_mut_refs)]
25#![feature(const_ptr_write)]
26#![feature(const_refs_to_cell)]
27
28#[cfg(not(CONFIG_RUST))]
31compile_error!("Missing kernel configuration for conditional compilation");
32
33extern crate self as kernel;
35
36pub use ffi;
37
38pub mod alloc;
39#[cfg(CONFIG_BLOCK)]
40pub mod block;
41#[doc(hidden)]
42pub mod build_assert;
43pub mod cred;
44pub mod device;
45pub mod device_id;
46pub mod devres;
47pub mod driver;
48pub mod error;
49pub mod faux;
50#[cfg(CONFIG_RUST_FW_LOADER_ABSTRACTIONS)]
51pub mod firmware;
52pub mod fs;
53pub mod init;
54pub mod io;
55pub mod ioctl;
56pub mod jump_label;
57#[cfg(CONFIG_KUNIT)]
58pub mod kunit;
59pub mod list;
60pub mod miscdevice;
61#[cfg(CONFIG_NET)]
62pub mod net;
63pub mod of;
64pub mod page;
65#[cfg(CONFIG_PCI)]
66pub mod pci;
67pub mod pid_namespace;
68pub mod platform;
69pub mod prelude;
70pub mod print;
71pub mod rbtree;
72pub mod revocable;
73pub mod security;
74pub mod seq_file;
75pub mod sizes;
76mod static_assert;
77#[doc(hidden)]
78pub mod std_vendor;
79pub mod str;
80pub mod sync;
81pub mod task;
82pub mod time;
83pub mod tracepoint;
84pub mod transmute;
85pub mod types;
86pub mod uaccess;
87pub mod workqueue;
88
89#[doc(hidden)]
90pub use bindings;
91pub use macros;
92pub use uapi;
93
94const __LOG_PREFIX: &[u8] = b"rust_kernel\0";
96
97pub trait Module: Sized + Sync + Send {
101 fn init(module: &'static ThisModule) -> error::Result<Self>;
108}
109
110pub trait InPlaceModule: Sync + Send {
112 fn init(module: &'static ThisModule) -> impl init::PinInit<Self, error::Error>;
116}
117
118impl<T: Module> InPlaceModule for T {
119 fn init(module: &'static ThisModule) -> impl init::PinInit<Self, error::Error> {
120 let initer = move |slot: *mut Self| {
121 let m = <Self as Module>::init(module)?;
122
123 unsafe { slot.write(m) };
125 Ok(())
126 };
127
128 unsafe { init::pin_init_from_closure(initer) }
130 }
131}
132
133pub trait ModuleMetadata {
135 const NAME: &'static crate::str::CStr;
137}
138
139pub struct ThisModule(*mut bindings::module);
143
144unsafe impl Sync for ThisModule {}
146
147impl ThisModule {
148 pub const unsafe fn from_ptr(ptr: *mut bindings::module) -> ThisModule {
154 ThisModule(ptr)
155 }
156
157 pub const fn as_ptr(&self) -> *mut bindings::module {
161 self.0
162 }
163}
164
165#[cfg(not(any(testlib, test)))]
166#[panic_handler]
167fn panic(info: &core::panic::PanicInfo<'_>) -> ! {
168 pr_emerg!("{}\n", info);
169 unsafe { bindings::BUG() };
171}
172
173#[macro_export]
197macro_rules! container_of {
198 ($ptr:expr, $type:ty, $($f:tt)*) => {{
199 let ptr = $ptr as *const _ as *const u8;
200 let offset: usize = ::core::mem::offset_of!($type, $($f)*);
201 ptr.sub(offset) as *const $type
202 }}
203}
204
205#[doc(hidden)]
207#[macro_export]
208macro_rules! concat_literals {
209 ($( $asm:literal )* ) => {
210 ::core::concat!($($asm),*)
211 };
212}
213
214#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
220#[macro_export]
221macro_rules! asm {
222 ($($asm:expr),* ; $($rest:tt)*) => {
223 ::core::arch::asm!( $($asm)*, options(att_syntax), $($rest)* )
224 };
225}
226
227#[cfg(not(any(target_arch = "x86", target_arch = "x86_64")))]
233#[macro_export]
234macro_rules! asm {
235 ($($asm:expr),* ; $($rest:tt)*) => {
236 ::core::arch::asm!( $($asm)*, $($rest)* )
237 };
238}