lib.rs - source
Skip to main content kernel/ lib.rs 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.87.0. 20 #![feature(unsigned_is_multiple_of)] 21 // 22 // Stable since Rust 1.89.0. 23 #![feature(generic_arg_infer)] 24 // 25 // Expected to become stable.
26 #![feature(arbitrary_self_types)] 27 #![feature(derive_coerce_pointee)] 28 // 29 // To be determined. 30 #![feature(used_with_arg)] 31 // 32 // `feature(file_with_nul)` is stable since Rust 1.92.0. Before Rust 1.89.0, it did not exist, so 33 // enable it conditionally. 34 #![cfg_attr(CONFIG_RUSTC_HAS_FILE_WITH_NUL, feature(file_with_nul))] 35 36 // Ensure conditional compilation based on the kernel configuration works; 37 // otherwise we may silently break things like initcall handling.
38 #[cfg(not(CONFIG_RUST))] 39 compile_error! ( "Missing kernel configuration for conditional compilation" ); 40 41 // Allow proc-macros to refer to `::kernel` inside the `kernel` crate (this crate). 42 extern crate self as kernel; 43 44 pub use ffi ; 45 46 pub mod acpi ; 47 pub mod alloc ; 48 #[cfg(CONFIG_AUXILIARY_BUS)] 49 pub mod auxiliary ; 50 pub mod bitfield ; 51 pub mod bitmap ; 52 pub mod bits ; 53 #[cfg(CONFIG_BLOCK)] 54 pub mod block ; 55 pub mod bug ; 56 pub mod build_assert ; 57 pub mod clk ; 58 #[cfg(CONFIG_CONFIGFS_FS)] 59 pub mod configfs ; 60 pub mod cpu ; 61 #[cfg(CONFIG_CPU_FREQ)] 62 pub mod cpufreq ; 63 pub mod cpumask ; 64 pub mod cred ; 65 pub mod debugfs ; 66 pub mod device ; 67 pub mod device_id ; 68 pub mod devres ; 69 pub mod dma ; 70 #[cfg(CONFIG_DMA_SHARED_BUFFER)] 71 pub mod dma_buf ; 72 pub mod driver ; 73 #[cfg(CONFIG_DRM = "y" )] 74 pub mod drm ; 75 pub mod error ; 76 pub mod faux ; 77 #[cfg(CONFIG_RUST_FW_LOADER_ABSTRACTIONS)] 78 pub mod firmware ; 79 pub mod fmt ; 80 pub mod fs ; 81 #[cfg(CONFIG_RUST_FWCTL_ABSTRACTIONS)] 82 pub mod fwctl ; 83 #[cfg(CONFIG_GPU_BUDDY = "y" )] 84 pub mod gpu ; 85 #[cfg(CONFIG_I2C = "y" )] 86 pub mod i2c ; 87 pub mod id_pool ; 88 #[doc(hidden)] 89 pub mod impl_flags ; 90 pub mod init ; 91 pub mod interop ; 92 pub mod interrupt ; 93 pub mod io ; 94 pub mod ioctl ; 95 pub mod iommu ; 96 pub mod iov ; 97 pub mod irq ; 98 pub mod jump_label ; 99 #[cfg(CONFIG_RUST_KCSAN_KUNIT_TEST)] 100 mod kcsan_test; 101 #[cfg(CONFIG_KUNIT)] 102 pub mod kunit ; 103 pub mod list ; 104 pub mod maple_tree ; 105 pub mod mem ; 106 pub mod miscdevice ; 107 pub mod mm ; 108 pub mod module ; 109 pub mod module_param ; 110 #[cfg(CONFIG_NET)] 111 pub mod net ; 112 pub mod num ; 113 pub mod of ; 114 #[cfg(CONFIG_PM_OPP)] 115 pub mod opp ; 116 pub mod page ; 117 #[cfg(CONFIG_PCI)] 118 pub mod pci ; 119 pub mod pid_namespace ; 120 pub mod platform ; 121 pub mod prelude ; 122 pub mod print ; 123 pub mod processor ; 124 pub mod ptr ; 125 #[cfg(CONFIG_RUST_PWM_ABSTRACTIONS)] 126 pub mod pwm ; 127 pub mod ratelimit ; 128 pub mod rbtree ; 129 pub mod regulator ; 130 pub mod revocable ; 131 #[cfg(CONFIG_XARRAY_RUST)] 132 pub mod rxarray ; 133 pub mod safety ; 134 pub mod scatterlist ; 135 pub mod security ; 136 pub mod seq_file ; 137 #[cfg(CONFIG_RUST_SERIAL_DEV_BUS_ABSTRACTIONS)] 138 pub mod serdev ; 139 pub mod sizes ; 140 #[cfg(CONFIG_SOC_BUS)] 141 pub mod soc ; 142 #[doc(hidden)] 143 pub mod std_vendor ; 144 pub mod str ; 145 pub mod sync ; 146 pub mod task ; 147 pub mod time ; 148 pub mod tracepoint ; 149 pub mod transmute ; 150 pub mod types ; 151 pub mod uaccess ; 152 #[cfg(CONFIG_USB = "y" )] 153 pub mod usb ; 154 pub mod workqueue ; 155 pub mod xarray ; 156 157 #[doc(hidden)] 158 pub use bindings; 159 pub use macros ; 160 pub use module::{ 161 InPlaceModule , 162 Module , 163 ModuleMetadata , 164 ThisModule , // 165 }; 166 pub use uapi; 167 168 /// Prefix to appear before log messages printed from within the `kernel` crate.
169 const __LOG_PREFIX: & [ u8 ] = b"rust_kernel\0" ; 170 171 /// Dummy module type for `#[vtable]` `impl` blocks within the `kernel` crate (e.g. KUnit tests). 172 // The `allow` is needed since it may be unused (e.g. KUnit tests may be disabled). 173 #[allow(dead_code)] 174 struct LocalModule; 175 176 impl ModuleMetadata for LocalModule { 177 const NAME: & 'static str:: CStr = c"rust_kernel" ; 178 179 const THIS_MODULE: ThisModule = { 180 // SAFETY: `try_module_get`/`module_put` handle null module pointers gracefully.
181 unsafe { ThisModule :: from_ptr (core::ptr:: null_mut ()) } 182 }; 183 } 184 185 #[cfg(not(testlib))] 186 #[panic_handler] 187 fn panic(info: & core::panic:: PanicInfo < '_ >) -> ! { 188 pr_emerg! ( "{}\n" , info); 189 // SAFETY: FFI call. 190 unsafe { bindings::BUG() }; 191 } 192 193 /// Produces a pointer to an object from a pointer to one of its fields. 194 /// 195 /// If you encounter a type mismatch due to the [`Opaque`] type, then use [`Opaque::cast_into`] or 196 /// [`Opaque::cast_from`] to resolve the mismatch.
197 /// 198 /// [`Opaque`]: crate::types::Opaque 199 /// [`Opaque::cast_into`]: crate::types::Opaque::cast_into 200 /// [`Opaque::cast_from`]: crate::types::Opaque::cast_from 201 /// 202 /// # Safety 203 /// 204 /// The pointer passed to this macro, and the pointer returned by this macro, must both be in 205 /// bounds of the same allocation. 206 /// 207 /// # Examples 208 /// 209 /// ``` 210 /// # use kernel::container_of; 211 /// struct Test { 212 /// a: u64, 213 /// b: u32, 214 /// } 215 /// 216 /// let test = Test { a: 10, b: 20 }; 217 /// let b_ptr: *const _ = &test.b; 218 /// // SAFETY: The pointer points at the `b` field of a `Test`, so the resulting pointer will be 219 /// // in-bounds of the same allocation as `b_ptr`.
220 /// let test_alias = unsafe { container_of!(b_ptr, Test, b) }; 221 /// assert!(core::ptr::eq(&test, test_alias)); 222 /// ``` 223 #[macro_export] 224 macro_rules! container_of { 225 ( $field_ptr :expr, $Container :ty, $( $fields :tt) * ) => {{ 226 let offset: usize = ::core::mem::offset_of! ( $Container , $( $fields ) * ); 227 let field_ptr = $field_ptr ; 228 let container_ptr = field_ptr.byte_sub(offset).cast::< $Container >(); 229 $crate::assert_same_type (field_ptr, ( & raw const ( * container_ptr).$( $fields ) * ).cast_mut()); 230 container_ptr 231 }} 232 } 233 234 /// Helper for [`container_of!`].
235 #[doc(hidden)] 236 pub fn assert_same_type<T>( _ : T, _ : T) {} 237 238 /// Helper for `.rs.S` files. 239 #[doc(hidden)] 240 #[macro_export] 241 macro_rules! concat_literals { 242 ($( $asm :literal )* ) => { 243 ::core::concat! ($( $asm ), * ) 244 }; 245 } 246 247 /// Wrapper around `asm!` configured for use in the kernel. 248 /// 249 /// Uses a semicolon to avoid parsing ambiguities, even though this does not match native `asm!` 250 /// syntax.
251 // For x86, `asm!` uses intel syntax by default, but we want to use at&t syntax in the kernel. 252 #[cfg(any(target_arch = "x86" , target_arch = "x86_64" ))] 253 #[macro_export] 254 macro_rules! asm { 255 ($( $asm :expr),* ; $( $rest :tt) * ) => { 256 ::core::arch::asm! ( $( $asm ) * , options(att_syntax), $( $rest )* ) 257 }; 258 } 259 260 /// Wrapper around `asm!` configured for use in the kernel. 261 /// 262 /// Uses a semicolon to avoid parsing ambiguities, even though this does not match native `asm!` 263 /// syntax.
264 // For non-x86 arches we just pass through to `asm!`. 265 #[cfg(not(any(target_arch = "x86" , target_arch = "x86_64" )))] 266 #[macro_export] 267 macro_rules! asm { 268 ($( $asm :expr),* ; $( $rest :tt) * ) => { 269 ::core:
The rest of this page is on the live site.