| 1 | |
| 2 |
|
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 |
|
| 14 | #![no_std]
|
| 15 | |
| 16 | |
| 17 | |
| 18 | |
| 19 | |
| 20 | #![feature(inline_const)]
|
| 21 | |
| 22 | |
| 23 | #![feature(lint_reasons)]
|
| 24 | |
| 25 | |
| 26 | #![feature(raw_ref_op)]
|
| 27 | |
| 28 | |
| 29 | #![feature(const_maybe_uninit_as_mut_ptr)]
|
| 30 | #![feature(const_mut_refs)]
|
| 31 | #![feature(const_ptr_write)]
|
| 32 | #![feature(const_refs_to_cell)]
|
| 33 | |
| 34 | |
| 35 | #![feature(arbitrary_self_types)]
|
| 36 | |
| 37 | |
| 38 | #![feature(used_with_arg)]
|
| 39 | |
| 40 | |
| 41 | |
| 42 | #![cfg_attr(CONFIG_RUSTC_HAS_COERCE_POINTEE, feature(derive_coerce_pointee))]
|
| 43 | #![cfg_attr(not(CONFIG_RUSTC_HAS_COERCE_POINTEE), feature(coerce_unsized))]
|
| 44 | #![cfg_attr(not(CONFIG_RUSTC_HAS_COERCE_POINTEE), feature(dispatch_from_dyn))]
|
| 45 | #![cfg_attr(not(CONFIG_RUSTC_HAS_COERCE_POINTEE), feature(unsize))]
|
| 46 |
|
| 47 | |
| 48 | |
| 49 | #[cfg(not(CONFIG_RUST))]
|
| 50 | compile_error!("Missing kernel configuration for conditional compilation");
|
| 51 |
|
| 52 | |
| 53 | extern crate self as kernel;
|
| 54 |
|
| 55 | pub use ffi;
|
| 56 |
|
| 57 | pub mod alloc;
|
| 58 | #[cfg(CONFIG_AUXILIARY_BUS)]
|
| 59 | pub mod auxiliary;
|
| 60 | #[cfg(CONFIG_BLOCK)]
|
| 61 | pub mod block;
|
| 62 | #[doc(hidden)]
|
| 63 | pub mod build_assert;
|
| 64 | pub mod clk;
|
| 65 | #[cfg(CONFIG_CONFIGFS_FS)]
|
| 66 | pub mod configfs;
|
| 67 | pub mod cpu;
|
| 68 | #[cfg(CONFIG_CPU_FREQ)]
|
| 69 | pub mod cpufreq;
|
| 70 | pub mod cpumask;
|
| 71 | pub mod cred;
|
| 72 | pub mod device;
|
| 73 | pub mod device_id;
|
| 74 | pub mod devres;
|
| 75 | pub mod dma;
|
| 76 | pub mod driver;
|
| 77 | #[cfg(CONFIG_DRM = "y")]
|
| 78 | pub mod drm;
|
| 79 | pub mod error;
|
| 80 | pub mod faux;
|
| 81 | #[cfg(CONFIG_RUST_FW_LOADER_ABSTRACTIONS)]
|
| 82 | pub mod firmware;
|
| 83 | pub mod fs;
|
| 84 | pub mod init;
|
| 85 | pub mod io;
|
| 86 | pub mod ioctl;
|
| 87 | pub mod jump_label;
|
| 88 | #[cfg(CONFIG_KUNIT)]
|
| 89 | pub mod kunit;
|
| 90 | pub mod list;
|
| 91 | pub mod miscdevice;
|
| 92 | pub mod mm;
|
| 93 | #[cfg(CONFIG_NET)]
|
| 94 | pub mod net;
|
| 95 | pub mod of;
|
| 96 | #[cfg(CONFIG_PM_OPP)]
|
| 97 | pub mod opp;
|
| 98 | pub mod page;
|
| 99 | #[cfg(CONFIG_PCI)]
|
| 100 | pub mod pci;
|
| 101 | pub mod pid_namespace;
|
| 102 | pub mod platform;
|
| 103 | pub mod prelude;
|
| 104 | pub mod print;
|
| 105 | pub mod rbtree;
|
| 106 | pub mod revocable;
|
| 107 | pub mod security;
|
| 108 | pub mod seq_file;
|
| 109 | pub mod sizes;
|
| 110 | mod static_assert;
|
| 111 | #[doc(hidden)]
|
| 112 | pub mod std_vendor;
|
| 113 | pub mod str;
|
| 114 | pub mod sync;
|
| 115 | pub mod task;
|
| 116 | pub mod time;
|
| 117 | pub mod tracepoint;
|
| 118 | pub mod transmute;
|
| 119 | pub mod types;
|
| 120 | pub mod uaccess;
|
| 121 | pub mod workqueue;
|
| 122 | pub mod xarray;
|
| 123 |
|
| 124 | #[doc(hidden)]
|
| 125 | pub use bindings;
|
| 126 | pub use macros;
|
| 127 | pub use uapi;
|
| 128 |
|
| 129 | |
| 130 | const __LOG_PREFIX: &[u8] = b"rust_kernel\0";
|
| 131 |
|
| 132 | |
| 133 | |
| 134 | |
| 135 | pub trait Module: Sized + Sync + Send {
|
| 136 | |
| 137 | |
| 138 | |
| 139 | |
| 140 | |
| 141 | |
| 142 | fn init(module: &'static ThisModule) -> error::Result<Self>;
|
| 143 | }
|
| 144 |
|
| 145 | |
| 146 | pub trait InPlaceModule: Sync + Send {
|
| 147 | |
| 148 | |
| 149 | |
| 150 | fn init(module: &'static ThisModule) -> impl pin_init::PinInit<Self, error::Error>;
|
| 151 | }
|
| 152 |
|
| 153 | impl<T: Module> InPlaceModule for T {
|
| 154 | fn init(module: &'static ThisModule) -> impl pin_init::PinInit<Self, error::Error> {
|
| 155 | let initer = move |slot: *mut Self| {
|
| 156 | let m = <Self as Module>::init(module)?;
|
| 157 |
|
| 158 | |
| 159 | unsafe { slot.write(m) };
|
| 160 | Ok(())
|
| 161 | };
|
| 162 |
|
| 163 | |
| 164 | unsafe { pin_init::pin_init_from_closure(initer) }
|
| 165 | }
|
| 166 | }
|
| 167 |
|
| 168 | |
| 169 | pub trait ModuleMetadata {
|
| 170 | |
| 171 | const NAME: &'static crate::str::CStr;
|
| 172 | }
|
| 173 |
|
| 174 | |
| 175 | |
| 176 | |
| 177 | pub struct ThisModule(*mut bindings::module);
|
| 178 |
|
| 179 | |
| 180 | unsafe impl Sync for ThisModule {}
|
| 181 |
|
| 182 | impl ThisModule {
|
| 183 | |
| 184 | |
| 185 | |
| 186 | |
| 187 | |
| 188 | pub const unsafe fn from_ptr(ptr: *mut bindings::module) -> ThisModule {
|
| 189 | ThisModule(ptr)
|
| 190 | }
|
| 191 |
|
| 192 | |
| 193 | |
| 194 | |
| 195 | pub const fn as_ptr(&self) -> *mut bindings::module {
|
| 196 | self.0
|
| 197 | }
|
| 198 | }
|
| 199 |
|
| 200 | #[cfg(not(any(testlib, test)))]
|
| 201 | #[panic_handler]
|
| 202 | fn panic(info: &core::panic::PanicInfo<'_>) -> ! {
|
| 203 | pr_emerg!("{}\n", info);
|
| 204 | |
| 205 | unsafe { bindings::BUG() };
|
| 206 | }
|
| 207 |
|
| 208 | |
| 209 | |
| 210 | |
| 211 | |
| 212 | |
| 213 | |
| 214 | |
| 215 | |
| 216 | |
| 217 | |
| 218 | |
| 219 | |
| 220 | |
| 221 | |
| 222 | |
| 223 | |
| 224 | |
| 225 | |
| 226 | |
| 227 | |
| 228 | |
| 229 | |
| 230 | |
| 231 | #[macro_export]
|
| 232 | macro_rules! container_of {
|
| 233 | ($field_ptr:expr, $Container:ty, $($fields:tt)*) => {{
|
| 234 | let offset: usize = ::core::mem::offset_of!($Container, $($fields)*);
|
| 235 | let field_ptr = $field_ptr;
|
| 236 | let container_ptr = field_ptr.byte_sub(offset).cast::<$Container>();
|
| 237 | $crate::assert_same_type(field_ptr, (&raw const (*container_ptr).$($fields)*).cast_mut());
|
| 238 | container_ptr
|
| 239 | }}
|
| 240 | }
|
| 241 |
|
| 242 | |
| 243 | #[doc(hidden)]
|
| 244 | pub fn assert_same_type<T>(_: T, _: T) {}
|
| 245 |
|
| 246 | |
| 247 | #[doc(hidden)]
|
| 248 | #[macro_export]
|
| 249 | macro_rules! concat_literals {
|
| 250 | ($( $asm:literal )* ) => {
|
| 251 | ::core::concat!($($asm),*)
|
| 252 | };
|
| 253 | }
|
| 254 |
|
| 255 | |
| 256 | |
| 257 | |
| 258 | |
| 259 | |
| 260 | #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
|
| 261 | #[macro_export]
|
| 262 | macro_rules! asm {
|
| 263 | ($($asm:expr),* ; $($rest:tt)*) => {
|
| 264 | ::core::arch::asm!( $($asm)*, options(att_syntax), $($rest)* )
|
| 265 | };
|
| 266 | }
|
| 267 |
|
| 268 | |
| 269 | |
| 270 | |
| 271 | |
| 272 | |
| 273 | #[cfg(not(any(target_arch = "x86", target_arch = "x86_64")))]
|
| 274 | #[macro_export]
|
| 275 | macro_rules! asm {
|
| 276 | ($($asm:expr),* ; $($rest:tt)*) => {
|
| 277 | ::core::arch::asm!( $($asm)*, $($rest)* )
|
| 278 | };
|
| 279 | }
|