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`], [`alloc`] 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 #![feature(arbitrary_self_types)] 16 #![feature(coerce_unsized)] 17 #![feature(dispatch_from_dyn)] 18 #![feature(inline_const)] 19 #![feature(lint_reasons)] 20 #![feature(unsize)] 21 22 // Ensure conditional compilation based on the kernel configuration works; 23 // otherwise we may silently break things like initcall handling. 24 #[cfg(not(CONFIG_RUST))] 25 compile_error!("Missing kernel configuration for conditional compilation"); 26 27 // Allow proc-macros to refer to `::kernel` inside the `kernel` crate (this crate). 28 extern crate self as kernel; 29 30 pub use ffi; 31 32 pub mod alloc; 33 #[cfg(CONFIG_BLOCK)] 34 pub mod block; 35 mod build_assert; 36 pub mod cred; 37 pub mod device; 38 pub mod error; 39 #[cfg(CONFIG_RUST_FW_LOADER_ABSTRACTIONS)] 40 pub mod firmware; 41 pub mod fs; 42 pub mod init; 43 pub mod ioctl; 44 pub mod jump_label; 45 #[cfg(CONFIG_KUNIT)] 46 pub mod kunit; 47 pub mod list; 48 #[cfg(CONFIG_NET)] 49 pub mod net; 50 pub mod page; 51 pub mod pid_namespace; 52 pub mod prelude; 53 pub mod print; 54 pub mod rbtree; 55 pub mod security; 56 pub mod seq_file; 57 pub mod sizes; 58 mod static_assert; 59 #[doc(hidden)] 60 pub mod std_vendor; 61 pub mod str; 62 pub mod sync; 63 pub mod task; 64 pub mod time; 65 pub mod tracepoint; 66 pub mod transmute; 67 pub mod types; 68 pub mod uaccess; 69 pub mod workqueue; 70 71 #[doc(hidden)] 72 pub use bindings; 73 pub use macros; 74 pub use uapi; 75 76 #[doc(hidden)] 77 pub use build_error::build_error; 78 79 /// Prefix to appear before log messages printed from within the `kernel` crate. 80 const __LOG_PREFIX: &[u8] = b"rust_kernel\0"; 81 82 /// The top level entrypoint to implementing a kernel module. 83 /// 84 /// For any teardown or cleanup operations, your type may implement [`Drop`]. 85 pub trait Module: Sized + Sync + Send { 86 /// Called at module initialization time. 87 /// 88 /// Use this method to perform whatever setup or registration your module 89 /// should do. 90 /// 91 /// Equivalent to the `module_init` macro in the C API. 92 fn init(module: &'static ThisModule) -> error::Result<Self>; 93 } 94 95 /// Equivalent to `THIS_MODULE` in the C API. 96 /// 97 /// C header: [`include/linux/init.h`](srctree/include/linux/init.h) 98 pub struct ThisModule(*mut bindings::module); 99 100 // SAFETY: `THIS_MODULE` may be used from all threads within a module. 101 unsafe impl Sync for ThisModule {} 102 103 impl ThisModule { 104 /// Creates a [`ThisModule`] given the `THIS_MODULE` pointer. 105 /// 106 /// # Safety 107 /// 108 /// The pointer must be equal to the right `THIS_MODULE`. 109 pub const unsafe fn from_ptr(ptr: *mut bindings::module) -> ThisModule { 110 ThisModule(ptr) 111 } 112 113 /// Access the raw pointer for this module. 114 /// 115 /// It is up to the user to use it correctly. 116 pub const fn as_ptr(&self) -> *mut bindings::module { 117 self.0 118 } 119 } 120 121 #[cfg(not(any(testlib, test)))] 122 #[panic_handler] 123 fn panic(info: &core::panic::PanicInfo<'_>) -> ! { 124 pr_emerg!("{}\n", info); 125 // SAFETY: FFI call. 126 unsafe { bindings::BUG() }; 127 } 128 129 /// Produces a pointer to an object from a pointer to one of its fields. 130 /// 131 /// # Safety 132 /// 133 /// The pointer passed to this macro, and the pointer returned by this macro, must both be in 134 /// bounds of the same allocation. 135 /// 136 /// # Examples 137 /// 138 /// ``` 139 /// # use kernel::container_of; 140 /// struct Test { 141 /// a: u64, 142 /// b: u32, 143 /// } 144 /// 145 /// let test = Test { a: 10, b: 20 }; 146 /// let b_ptr = &test.b; 147 /// // SAFETY: The pointer points at the `b` field of a `Test`, so the resulting pointer will be 148 /// // in-bounds of the same allocation as `b_ptr`. 149 /// let test_alias = unsafe { container_of!(b_ptr, Test, b) }; 150 /// assert!(core::ptr::eq(&test, test_alias)); 151 /// ``` 152 #[macro_export] 153 macro_rules! container_of { 154 ($ptr:expr, $type:ty, $($f:tt)*) => {{ 155 let ptr = $ptr as *const _ as *const u8; 156 let offset: usize = ::core::mem::offset_of!($type, $($f)*); 157 ptr.sub(offset) as *const $type 158 }} 159 } 160 161 /// Helper for `.rs.S` files. 162 #[doc(hidden)] 163 #[macro_export] 164 macro_rules! concat_literals { 165 ($( $asm:literal )* ) => { 166 ::core::concat!($($asm),*) 167 }; 168 } 169 170 /// Wrapper around `asm!` configured for use in the kernel. 171 /// 172 /// Uses a semicolon to avoid parsing ambiguities, even though this does not match native `asm!` 173 /// syntax. 174 // For x86, `asm!` uses intel syntax by default, but we want to use at&t syntax in the kernel. 175 #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] 176 #[macro_export] 177 macro_rules! asm { 178 ($($asm:expr),* ; $($rest:tt)*) => { 179 ::core::arch::asm!( $($asm)*, options(att_syntax), $($rest)* ) 180 }; 181 } 182 183 /// Wrapper around `asm!` configured for use in the kernel. 184 /// 185 /// Uses a semicolon to avoid parsing ambiguities, even though this does not match native `asm!` 186 /// syntax. 187 // For non-x86 arches we just pass through to `asm!`. 188 #[cfg(not(any(target_arch = "x86", target_arch = "x86_64")))] 189 #[macro_export] 190 macro_rules! asm { 191 ($($asm:expr),* ; $($rest:tt)*) => { 192 ::core::arch::asm!( $($asm)*, $($rest)* ) 193 }; 194 } 195