1 // SPDX-License-Identifier: GPL-2.0 2 3 //! The `kernel` crate. 4 //! 5 //! This crate contains the kernel APIs that have been ported or wrapped for 6 //! usage by Rust code in the kernel and is shared by all of them. 7 //! 8 //! In other words, all the rest of the Rust code in the kernel (e.g. kernel 9 //! modules written in Rust) depends on [`core`] and this crate. 10 //! 11 //! If you need a kernel C API that is not ported or wrapped yet here, then 12 //! do so first instead of bypassing this crate. 13 14 #![no_std] 15 // 16 // Please see https://github.com/Rust-for-Linux/linux/issues/2 for details on 17 // the unstable features in use. 18 // 19 // Stable since Rust 1.89.0. 20 #![feature(generic_arg_infer)] 21 // 22 // Expected to become stable. 23 #![feature(arbitrary_self_types)] 24 #![feature(derive_coerce_pointee)] 25 // 26 // To be determined. 27 #![feature(used_with_arg)] 28 // 29 // `feature(file_with_nul)` is stable since Rust 1.92.0. Before Rust 1.89.0, it did not exist, so 30 // enable it conditionally. 31 #![cfg_attr(CONFIG_RUSTC_HAS_FILE_WITH_NUL, feature(file_with_nul))] 32 33 // Ensure conditional compilation based on the kernel configuration works; 34 // otherwise we may silently break things like initcall handling. 35 #[cfg(not(CONFIG_RUST))] 36 compile_error!("Missing kernel configuration for conditional compilation"); 37 38 // Allow proc-macros to refer to `::kernel` inside the `kernel` crate (this crate). 39 extern crate self as kernel; 40 41 pub use ffi; 42 43 pub mod acpi; 44 pub mod alloc; 45 #[cfg(CONFIG_AUXILIARY_BUS)] 46 pub mod auxiliary; 47 pub mod bitfield; 48 pub mod bitmap; 49 pub mod bits; 50 #[cfg(CONFIG_BLOCK)] 51 pub mod block; 52 pub mod bug; 53 pub mod build_assert; 54 pub mod clk; 55 #[cfg(CONFIG_CONFIGFS_FS)] 56 pub mod configfs; 57 pub mod cpu; 58 #[cfg(CONFIG_CPU_FREQ)] 59 pub mod cpufreq; 60 pub mod cpumask; 61 pub mod cred; 62 pub mod debugfs; 63 pub mod device; 64 pub mod device_id; 65 pub mod devres; 66 pub mod dma; 67 pub mod driver; 68 #[cfg(CONFIG_DRM = "y")] 69 pub mod drm; 70 pub mod error; 71 pub mod faux; 72 #[cfg(CONFIG_RUST_FW_LOADER_ABSTRACTIONS)] 73 pub mod firmware; 74 pub mod fmt; 75 pub mod fs; 76 #[cfg(CONFIG_GPU_BUDDY = "y")] 77 pub mod gpu; 78 #[cfg(CONFIG_I2C = "y")] 79 pub mod i2c; 80 pub mod id_pool; 81 #[doc(hidden)] 82 pub mod impl_flags; 83 pub mod init; 84 pub mod interop; 85 pub mod io; 86 pub mod ioctl; 87 pub mod iommu; 88 pub mod iov; 89 pub mod irq; 90 pub mod jump_label; 91 #[cfg(CONFIG_KUNIT)] 92 pub mod kunit; 93 pub mod list; 94 pub mod maple_tree; 95 pub mod miscdevice; 96 pub mod mm; 97 pub mod module; 98 pub mod module_param; 99 #[cfg(CONFIG_NET)] 100 pub mod net; 101 pub mod num; 102 pub mod of; 103 #[cfg(CONFIG_PM_OPP)] 104 pub mod opp; 105 pub mod page; 106 #[cfg(CONFIG_PCI)] 107 pub mod pci; 108 pub mod pid_namespace; 109 pub mod platform; 110 pub mod prelude; 111 pub mod print; 112 pub mod processor; 113 pub mod ptr; 114 #[cfg(CONFIG_RUST_PWM_ABSTRACTIONS)] 115 pub mod pwm; 116 pub mod rbtree; 117 pub mod regulator; 118 pub mod revocable; 119 pub mod safety; 120 pub mod scatterlist; 121 pub mod security; 122 pub mod seq_file; 123 pub mod sizes; 124 #[cfg(CONFIG_SOC_BUS)] 125 pub mod soc; 126 #[doc(hidden)] 127 pub mod std_vendor; 128 pub mod str; 129 pub mod sync; 130 pub mod task; 131 pub mod time; 132 pub mod tracepoint; 133 pub mod transmute; 134 pub mod types; 135 pub mod uaccess; 136 #[cfg(CONFIG_USB = "y")] 137 pub mod usb; 138 pub mod workqueue; 139 pub mod xarray; 140 141 #[doc(hidden)] 142 pub use bindings; 143 pub use macros; 144 pub use module::{ 145 InPlaceModule, 146 Module, 147 ModuleMetadata, 148 ThisModule, // 149 }; 150 pub use uapi; 151 152 /// Prefix to appear before log messages printed from within the `kernel` crate. 153 const __LOG_PREFIX: &[u8] = b"rust_kernel\0"; 154 155 #[cfg(not(testlib))] 156 #[panic_handler] 157 fn panic(info: &core::panic::PanicInfo<'_>) -> ! { 158 pr_emerg!("{}\n", info); 159 // SAFETY: FFI call. 160 unsafe { bindings::BUG() }; 161 } 162 163 /// Produces a pointer to an object from a pointer to one of its fields. 164 /// 165 /// If you encounter a type mismatch due to the [`Opaque`] type, then use [`Opaque::cast_into`] or 166 /// [`Opaque::cast_from`] to resolve the mismatch. 167 /// 168 /// [`Opaque`]: crate::types::Opaque 169 /// [`Opaque::cast_into`]: crate::types::Opaque::cast_into 170 /// [`Opaque::cast_from`]: crate::types::Opaque::cast_from 171 /// 172 /// # Safety 173 /// 174 /// The pointer passed to this macro, and the pointer returned by this macro, must both be in 175 /// bounds of the same allocation. 176 /// 177 /// # Examples 178 /// 179 /// ``` 180 /// # use kernel::container_of; 181 /// struct Test { 182 /// a: u64, 183 /// b: u32, 184 /// } 185 /// 186 /// let test = Test { a: 10, b: 20 }; 187 /// let b_ptr: *const _ = &test.b; 188 /// // SAFETY: The pointer points at the `b` field of a `Test`, so the resulting pointer will be 189 /// // in-bounds of the same allocation as `b_ptr`. 190 /// let test_alias = unsafe { container_of!(b_ptr, Test, b) }; 191 /// assert!(core::ptr::eq(&test, test_alias)); 192 /// ``` 193 #[macro_export] 194 macro_rules! container_of { 195 ($field_ptr:expr, $Container:ty, $($fields:tt)*) => {{ 196 let offset: usize = ::core::mem::offset_of!($Container, $($fields)*); 197 let field_ptr = $field_ptr; 198 let container_ptr = field_ptr.byte_sub(offset).cast::<$Container>(); 199 $crate::assert_same_type(field_ptr, (&raw const (*container_ptr).$($fields)*).cast_mut()); 200 container_ptr 201 }} 202 } 203 204 /// Helper for [`container_of!`]. 205 #[doc(hidden)] 206 pub fn assert_same_type<T>(_: T, _: T) {} 207 208 /// Helper for `.rs.S` files. 209 #[doc(hidden)] 210 #[macro_export] 211 macro_rules! concat_literals { 212 ($( $asm:literal )* ) => { 213 ::core::concat!($($asm),*) 214 }; 215 } 216 217 /// Wrapper around `asm!` configured for use in the kernel. 218 /// 219 /// Uses a semicolon to avoid parsing ambiguities, even though this does not match native `asm!` 220 /// syntax. 221 // For x86, `asm!` uses intel syntax by default, but we want to use at&t syntax in the kernel. 222 #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] 223 #[macro_export] 224 macro_rules! asm { 225 ($($asm:expr),* ; $($rest:tt)*) => { 226 ::core::arch::asm!( $($asm)*, options(att_syntax), $($rest)* ) 227 }; 228 } 229 230 /// Wrapper around `asm!` configured for use in the kernel. 231 /// 232 /// Uses a semicolon to avoid parsing ambiguities, even though this does not match native `asm!` 233 /// syntax. 234 // For non-x86 arches we just pass through to `asm!`. 235 #[cfg(not(any(target_arch = "x86", target_arch = "x86_64")))] 236 #[macro_export] 237 macro_rules! asm { 238 ($($asm:expr),* ; $($rest:tt)*) => { 239 ::core::arch::asm!( $($asm)*, $($rest)* ) 240 }; 241 } 242 243 /// Gets the C string file name of a [`Location`]. 244 /// 245 /// If `Location::file_as_c_str()` is not available, returns a string that warns about it. 246 /// 247 /// [`Location`]: core::panic::Location 248 /// 249 /// # Examples 250 /// 251 /// ``` 252 /// # use kernel::file_from_location; 253 /// 254 /// #[track_caller] 255 /// fn foo() { 256 /// let caller = core::panic::Location::caller(); 257 /// 258 /// // Output: 259 /// // - A path like "rust/kernel/example.rs" if `file_as_c_str()` is available. 260 /// // - "<Location::file_as_c_str() not supported>" otherwise. 261 /// let caller_file = file_from_location(caller); 262 /// 263 /// // Prints out the message with caller's file name. 264 /// pr_info!("foo() called in file {caller_file:?}\n"); 265 /// 266 /// # if cfg!(CONFIG_RUSTC_HAS_FILE_WITH_NUL) { 267 /// # assert_eq!(Ok(caller.file()), caller_file.to_str()); 268 /// # } 269 /// } 270 /// 271 /// # foo(); 272 /// ``` 273 #[inline] 274 pub fn file_from_location<'a>(loc: &'a core::panic::Location<'a>) -> &'a core::ffi::CStr { 275 #[cfg(CONFIG_RUSTC_HAS_FILE_AS_C_STR)] 276 { 277 loc.file_as_c_str() 278 } 279 280 #[cfg(all(CONFIG_RUSTC_HAS_FILE_WITH_NUL, not(CONFIG_RUSTC_HAS_FILE_AS_C_STR)))] 281 { 282 loc.file_with_nul() 283 } 284 285 #[cfg(not(CONFIG_RUSTC_HAS_FILE_WITH_NUL))] 286 { 287 let _ = loc; 288 c"<Location::file_as_c_str() not supported>" 289 } 290 } 291