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(strict_provenance)]
44#![feature(generic_arg_infer)]
47#![feature(arbitrary_self_types)]
50#![feature(used_with_arg)]
53#![cfg_attr(CONFIG_RUSTC_HAS_COERCE_POINTEE, feature(derive_coerce_pointee))]
57#![cfg_attr(not(CONFIG_RUSTC_HAS_COERCE_POINTEE), feature(coerce_unsized))]
58#![cfg_attr(not(CONFIG_RUSTC_HAS_COERCE_POINTEE), feature(dispatch_from_dyn))]
59#![cfg_attr(not(CONFIG_RUSTC_HAS_COERCE_POINTEE), feature(unsize))]
60#![cfg_attr(CONFIG_RUSTC_HAS_FILE_WITH_NUL, feature(file_with_nul))]
64
65#[cfg(not(CONFIG_RUST))]
68compile_error!("Missing kernel configuration for conditional compilation");
69
70extern crate self as kernel;
72
73pub use ffi;
74
75pub mod acpi;
76pub mod alloc;
77#[cfg(CONFIG_AUXILIARY_BUS)]
78pub mod auxiliary;
79pub mod bitmap;
80pub mod bits;
81#[cfg(CONFIG_BLOCK)]
82pub mod block;
83pub mod bug;
84#[doc(hidden)]
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;
158mod static_assert;
159#[doc(hidden)]
160pub mod std_vendor;
161pub mod str;
162pub mod sync;
163pub mod task;
164pub mod time;
165pub mod tracepoint;
166pub mod transmute;
167pub mod types;
168pub mod uaccess;
169#[cfg(CONFIG_USB = "y")]
170pub mod usb;
171pub mod workqueue;
172pub mod xarray;
173
174#[doc(hidden)]
175pub use bindings;
176pub use macros;
177pub use uapi;
178
179const __LOG_PREFIX: &[u8] = b"rust_kernel\0";
181
182pub trait Module: Sized + Sync + Send {
186 fn init(module: &'static ThisModule) -> error::Result<Self>;
193}
194
195pub trait InPlaceModule: Sync + Send {
197 fn init(module: &'static ThisModule) -> impl pin_init::PinInit<Self, error::Error>;
201}
202
203impl<T: Module> InPlaceModule for T {
204 fn init(module: &'static ThisModule) -> impl pin_init::PinInit<Self, error::Error> {
205 let initer = move |slot: *mut Self| {
206 let m = <Self as Module>::init(module)?;
207
208 unsafe { slot.write(m) };
210 Ok(())
211 };
212
213 unsafe { pin_init::pin_init_from_closure(initer) }
215 }
216}
217
218pub trait ModuleMetadata {
220 const NAME: &'static crate::str::CStr;
222}
223
224pub struct ThisModule(*mut bindings::module);
228
229unsafe impl Sync for ThisModule {}
231
232impl ThisModule {
233 pub const unsafe fn from_ptr(ptr: *mut bindings::module) -> ThisModule {
239 ThisModule(ptr)
240 }
241
242 pub const fn as_ptr(&self) -> *mut bindings::module {
246 self.0
247 }
248}
249
250#[cfg(not(testlib))]
251#[panic_handler]
252fn panic(info: &core::panic::PanicInfo<'_>) -> ! {
253 pr_emerg!("{}\n", info);
254 unsafe { bindings::BUG() };
256}
257
258#[macro_export]
289macro_rules! container_of {
290 ($field_ptr:expr, $Container:ty, $($fields:tt)*) => {{
291 let offset: usize = ::core::mem::offset_of!($Container, $($fields)*);
292 let field_ptr = $field_ptr;
293 let container_ptr = field_ptr.byte_sub(offset).cast::<$Container>();
294 $crate::assert_same_type(field_ptr, (&raw const (*container_ptr).$($fields)*).cast_mut());
295 container_ptr
296 }}
297}
298
299#[doc(hidden)]
301pub fn assert_same_type<T>(_: T, _: T) {}
302
303#[doc(hidden)]
305#[macro_export]
306macro_rules! concat_literals {
307 ($( $asm:literal )* ) => {
308 ::core::concat!($($asm),*)
309 };
310}
311
312#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
318#[macro_export]
319macro_rules! asm {
320 ($($asm:expr),* ; $($rest:tt)*) => {
321 ::core::arch::asm!( $($asm)*, options(att_syntax), $($rest)* )
322 };
323}
324
325#[cfg(not(any(target_arch = "x86", target_arch = "x86_64")))]
331#[macro_export]
332macro_rules! asm {
333 ($($asm:expr),* ; $($rest:tt)*) => {
334 ::core::arch::asm!( $($asm)*, $($rest)* )
335 };
336}
337
338#[inline]
369pub fn file_from_location<'a>(loc: &'a core::panic::Location<'a>) -> &'a core::ffi::CStr {
370 #[cfg(CONFIG_RUSTC_HAS_FILE_AS_C_STR)]
371 {
372 loc.file_as_c_str()
373 }
374
375 #[cfg(all(CONFIG_RUSTC_HAS_FILE_WITH_NUL, not(CONFIG_RUSTC_HAS_FILE_AS_C_STR)))]
376 {
377 loc.file_with_nul()
378 }
379
380 #[cfg(not(CONFIG_RUSTC_HAS_FILE_WITH_NUL))]
381 {
382 let _ = loc;
383 c"<Location::file_as_c_str() not supported>"
384 }
385}