1#![no_std]
15#![feature(generic_nonzero)]
21#![feature(inline_const)]
22#![feature(pointer_is_aligned)]
23#![feature(slice_ptr_len)]
24#![feature(slice_flatten)]
27#![feature(lint_reasons)]
30#![feature(offset_of_nested)]
33#![feature(raw_ref_op)]
34#![feature(const_maybe_uninit_as_mut_ptr)]
37#![feature(const_mut_refs)]
38#![feature(const_option)]
39#![feature(const_ptr_write)]
40#![feature(const_refs_to_cell)]
41#![feature(const_refs_to_static)]
42#![feature(strict_provenance)]
45#![feature(generic_arg_infer)]
48#![feature(arbitrary_self_types)]
51#![feature(used_with_arg)]
54#![cfg_attr(CONFIG_RUSTC_HAS_COERCE_POINTEE, feature(derive_coerce_pointee))]
58#![cfg_attr(not(CONFIG_RUSTC_HAS_COERCE_POINTEE), feature(coerce_unsized))]
59#![cfg_attr(not(CONFIG_RUSTC_HAS_COERCE_POINTEE), feature(dispatch_from_dyn))]
60#![cfg_attr(not(CONFIG_RUSTC_HAS_COERCE_POINTEE), feature(unsize))]
61#![cfg_attr(CONFIG_RUSTC_HAS_FILE_WITH_NUL, feature(file_with_nul))]
65
66#[cfg(not(CONFIG_RUST))]
69compile_error!("Missing kernel configuration for conditional compilation");
70
71extern crate self as kernel;
73
74pub use ffi;
75
76pub mod acpi;
77pub mod alloc;
78#[cfg(CONFIG_AUXILIARY_BUS)]
79pub mod auxiliary;
80pub mod bitmap;
81pub mod bits;
82#[cfg(CONFIG_BLOCK)]
83pub mod block;
84pub mod bug;
85pub mod build_assert;
86pub mod clk;
87#[cfg(CONFIG_CONFIGFS_FS)]
88pub mod configfs;
89pub mod cpu;
90#[cfg(CONFIG_CPU_FREQ)]
91pub mod cpufreq;
92pub mod cpumask;
93pub mod cred;
94pub mod debugfs;
95pub mod device;
96pub mod device_id;
97pub mod devres;
98pub mod dma;
99pub mod driver;
100#[cfg(CONFIG_DRM = "y")]
101pub mod drm;
102pub mod error;
103pub mod faux;
104#[cfg(CONFIG_RUST_FW_LOADER_ABSTRACTIONS)]
105pub mod firmware;
106pub mod fmt;
107pub mod fs;
108#[cfg(CONFIG_GPU_BUDDY = "y")]
109pub mod gpu;
110#[cfg(CONFIG_I2C = "y")]
111pub mod i2c;
112pub mod id_pool;
113#[doc(hidden)]
114pub mod impl_flags;
115pub mod init;
116pub mod interop;
117pub mod io;
118pub mod ioctl;
119pub mod iommu;
120pub mod iov;
121pub mod irq;
122pub mod jump_label;
123#[cfg(CONFIG_KUNIT)]
124pub mod kunit;
125pub mod list;
126pub mod maple_tree;
127pub mod miscdevice;
128pub mod mm;
129pub mod module_param;
130#[cfg(CONFIG_NET)]
131pub mod net;
132pub mod num;
133pub mod of;
134#[cfg(CONFIG_PM_OPP)]
135pub mod opp;
136pub mod page;
137#[cfg(CONFIG_PCI)]
138pub mod pci;
139pub mod pid_namespace;
140pub mod platform;
141pub mod prelude;
142pub mod print;
143pub mod processor;
144pub mod ptr;
145#[cfg(CONFIG_RUST_PWM_ABSTRACTIONS)]
146pub mod pwm;
147pub mod rbtree;
148pub mod regulator;
149pub mod revocable;
150pub mod safety;
151pub mod scatterlist;
152pub mod security;
153pub mod seq_file;
154pub mod sizes;
155pub mod slice;
156#[cfg(CONFIG_SOC_BUS)]
157pub mod soc;
158#[doc(hidden)]
159pub mod std_vendor;
160pub mod str;
161pub mod sync;
162pub mod task;
163pub mod time;
164pub mod tracepoint;
165pub mod transmute;
166pub mod types;
167pub mod uaccess;
168#[cfg(CONFIG_USB = "y")]
169pub mod usb;
170pub mod workqueue;
171pub mod xarray;
172
173#[doc(hidden)]
174pub use bindings;
175pub use macros;
176pub use uapi;
177
178const __LOG_PREFIX: &[u8] = b"rust_kernel\0";
180
181pub trait Module: Sized + Sync + Send {
185 fn init(module: &'static ThisModule) -> error::Result<Self>;
192}
193
194pub trait InPlaceModule: Sync + Send {
196 fn init(module: &'static ThisModule) -> impl pin_init::PinInit<Self, error::Error>;
200}
201
202impl<T: Module> InPlaceModule for T {
203 fn init(module: &'static ThisModule) -> impl pin_init::PinInit<Self, error::Error> {
204 let initer = move |slot: *mut Self| {
205 let m = <Self as Module>::init(module)?;
206
207 unsafe { slot.write(m) };
209 Ok(())
210 };
211
212 unsafe { pin_init::pin_init_from_closure(initer) }
214 }
215}
216
217pub trait ModuleMetadata {
219 const NAME: &'static crate::str::CStr;
221}
222
223pub struct ThisModule(*mut bindings::module);
227
228unsafe impl Sync for ThisModule {}
230
231impl ThisModule {
232 pub const unsafe fn from_ptr(ptr: *mut bindings::module) -> ThisModule {
238 ThisModule(ptr)
239 }
240
241 pub const fn as_ptr(&self) -> *mut bindings::module {
245 self.0
246 }
247}
248
249#[cfg(not(testlib))]
250#[panic_handler]
251fn panic(info: &core::panic::PanicInfo<'_>) -> ! {
252 pr_emerg!("{}\n", info);
253 unsafe { bindings::BUG() };
255}
256
257#[macro_export]
288macro_rules! container_of {
289 ($field_ptr:expr, $Container:ty, $($fields:tt)*) => {{
290 let offset: usize = ::core::mem::offset_of!($Container, $($fields)*);
291 let field_ptr = $field_ptr;
292 let container_ptr = field_ptr.byte_sub(offset).cast::<$Container>();
293 $crate::assert_same_type(field_ptr, (&raw const (*container_ptr).$($fields)*).cast_mut());
294 container_ptr
295 }}
296}
297
298#[doc(hidden)]
300pub fn assert_same_type<T>(_: T, _: T) {}
301
302#[doc(hidden)]
304#[macro_export]
305macro_rules! concat_literals {
306 ($( $asm:literal )* ) => {
307 ::core::concat!($($asm),*)
308 };
309}
310
311#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
317#[macro_export]
318macro_rules! asm {
319 ($($asm:expr),* ; $($rest:tt)*) => {
320 ::core::arch::asm!( $($asm)*, options(att_syntax), $($rest)* )
321 };
322}
323
324#[cfg(not(any(target_arch = "x86", target_arch = "x86_64")))]
330#[macro_export]
331macro_rules! asm {
332 ($($asm:expr),* ; $($rest:tt)*) => {
333 ::core::arch::asm!( $($asm)*, $($rest)* )
334 };
335}
336
337#[inline]
368pub fn file_from_location<'a>(loc: &'a core::panic::Location<'a>) -> &'a core::ffi::CStr {
369 #[cfg(CONFIG_RUSTC_HAS_FILE_AS_C_STR)]
370 {
371 loc.file_as_c_str()
372 }
373
374 #[cfg(all(CONFIG_RUSTC_HAS_FILE_WITH_NUL, not(CONFIG_RUSTC_HAS_FILE_AS_C_STR)))]
375 {
376 loc.file_with_nul()
377 }
378
379 #[cfg(not(CONFIG_RUSTC_HAS_FILE_WITH_NUL))]
380 {
381 let _ = loc;
382 c"<Location::file_as_c_str() not supported>"
383 }
384}