| 1 | |
| 2 |
|
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 |
|
| 14 | #![no_std]
|
| 15 | |
| 16 | |
| 17 | |
| 18 | |
| 19 | |
| 20 | #![feature(generic_nonzero)]
|
| 21 | #![feature(inline_const)]
|
| 22 | #![feature(pointer_is_aligned)]
|
| 23 | #![feature(slice_ptr_len)]
|
| 24 | |
| 25 | |
| 26 | #![feature(slice_flatten)]
|
| 27 | |
| 28 | |
| 29 | #![feature(lint_reasons)]
|
| 30 | |
| 31 | |
| 32 | #![feature(raw_ref_op)]
|
| 33 | |
| 34 | |
| 35 | #![feature(const_maybe_uninit_as_mut_ptr)]
|
| 36 | #![feature(const_mut_refs)]
|
| 37 | #![feature(const_option)]
|
| 38 | #![feature(const_ptr_write)]
|
| 39 | #![feature(const_refs_to_cell)]
|
| 40 | |
| 41 | |
| 42 | #![feature(strict_provenance)]
|
| 43 | |
| 44 | |
| 45 | #![feature(arbitrary_self_types)]
|
| 46 | |
| 47 | |
| 48 | #![feature(used_with_arg)]
|
| 49 | |
| 50 | |
| 51 | |
| 52 | #![cfg_attr(CONFIG_RUSTC_HAS_COERCE_POINTEE, feature(derive_coerce_pointee))]
|
| 53 | #![cfg_attr(not(CONFIG_RUSTC_HAS_COERCE_POINTEE), feature(coerce_unsized))]
|
| 54 | #![cfg_attr(not(CONFIG_RUSTC_HAS_COERCE_POINTEE), feature(dispatch_from_dyn))]
|
| 55 | #![cfg_attr(not(CONFIG_RUSTC_HAS_COERCE_POINTEE), feature(unsize))]
|
| 56 | |
| 57 | |
| 58 | |
| 59 | #![cfg_attr(CONFIG_RUSTC_HAS_FILE_WITH_NUL, feature(file_with_nul))]
|
| 60 |
|
| 61 | |
| 62 | |
| 63 | #[cfg(not(CONFIG_RUST))]
|
| 64 | compile_error!("Missing kernel configuration for conditional compilation");
|
| 65 |
|
| 66 | |
| 67 | extern crate self as kernel;
|
| 68 |
|
| 69 | pub use ffi;
|
| 70 |
|
| 71 | pub mod acpi;
|
| 72 | pub mod alloc;
|
| 73 | #[cfg(CONFIG_AUXILIARY_BUS)]
|
| 74 | pub mod auxiliary;
|
| 75 | pub mod bitmap;
|
| 76 | pub mod bits;
|
| 77 | #[cfg(CONFIG_BLOCK)]
|
| 78 | pub mod block;
|
| 79 | pub mod bug;
|
| 80 | #[doc(hidden)]
|
| 81 | pub mod build_assert;
|
| 82 | pub mod clk;
|
| 83 | #[cfg(CONFIG_CONFIGFS_FS)]
|
| 84 | pub mod configfs;
|
| 85 | pub mod cpu;
|
| 86 | #[cfg(CONFIG_CPU_FREQ)]
|
| 87 | pub mod cpufreq;
|
| 88 | pub mod cpumask;
|
| 89 | pub mod cred;
|
| 90 | pub mod debugfs;
|
| 91 | pub mod device;
|
| 92 | pub mod device_id;
|
| 93 | pub mod devres;
|
| 94 | pub mod dma;
|
| 95 | pub mod driver;
|
| 96 | #[cfg(CONFIG_DRM = "y")]
|
| 97 | pub mod drm;
|
| 98 | pub mod error;
|
| 99 | pub mod faux;
|
| 100 | #[cfg(CONFIG_RUST_FW_LOADER_ABSTRACTIONS)]
|
| 101 | pub mod firmware;
|
| 102 | pub mod fmt;
|
| 103 | pub mod fs;
|
| 104 | #[cfg(CONFIG_I2C = "y")]
|
| 105 | pub mod i2c;
|
| 106 | pub mod id_pool;
|
| 107 | #[doc(hidden)]
|
| 108 | pub mod impl_flags;
|
| 109 | pub mod init;
|
| 110 | pub mod io;
|
| 111 | pub mod ioctl;
|
| 112 | pub mod iommu;
|
| 113 | pub mod iov;
|
| 114 | pub mod irq;
|
| 115 | pub mod jump_label;
|
| 116 | #[cfg(CONFIG_KUNIT)]
|
| 117 | pub mod kunit;
|
| 118 | pub mod list;
|
| 119 | pub mod maple_tree;
|
| 120 | pub mod miscdevice;
|
| 121 | pub mod mm;
|
| 122 | pub mod module_param;
|
| 123 | #[cfg(CONFIG_NET)]
|
| 124 | pub mod net;
|
| 125 | pub mod num;
|
| 126 | pub mod of;
|
| 127 | #[cfg(CONFIG_PM_OPP)]
|
| 128 | pub mod opp;
|
| 129 | pub mod page;
|
| 130 | #[cfg(CONFIG_PCI)]
|
| 131 | pub mod pci;
|
| 132 | pub mod pid_namespace;
|
| 133 | pub mod platform;
|
| 134 | pub mod prelude;
|
| 135 | pub mod print;
|
| 136 | pub mod processor;
|
| 137 | pub mod ptr;
|
| 138 | #[cfg(CONFIG_RUST_PWM_ABSTRACTIONS)]
|
| 139 | pub mod pwm;
|
| 140 | pub mod rbtree;
|
| 141 | pub mod regulator;
|
| 142 | pub mod revocable;
|
| 143 | pub mod safety;
|
| 144 | pub mod scatterlist;
|
| 145 | pub mod security;
|
| 146 | pub mod seq_file;
|
| 147 | pub mod sizes;
|
| 148 | pub mod slice;
|
| 149 | #[cfg(CONFIG_SOC_BUS)]
|
| 150 | pub mod soc;
|
| 151 | mod static_assert;
|
| 152 | #[doc(hidden)]
|
| 153 | pub mod std_vendor;
|
| 154 | pub mod str;
|
| 155 | pub mod sync;
|
| 156 | pub mod task;
|
| 157 | pub mod time;
|
| 158 | pub mod tracepoint;
|
| 159 | pub mod transmute;
|
| 160 | pub mod types;
|
| 161 | pub mod uaccess;
|
| 162 | #[cfg(CONFIG_USB = "y")]
|
| 163 | pub mod usb;
|
| 164 | pub mod workqueue;
|
| 165 | pub mod xarray;
|
| 166 |
|
| 167 | #[doc(hidden)]
|
| 168 | pub use bindings;
|
| 169 | pub use macros;
|
| 170 | pub use uapi;
|
| 171 |
|
| 172 | |
| 173 | const __LOG_PREFIX: &[u8] = b"rust_kernel\0";
|
| 174 |
|
| 175 | |
| 176 | |
| 177 | |
| 178 | pub trait Module: Sized + Sync + Send {
|
| 179 | |
| 180 | |
| 181 | |
| 182 | |
| 183 | |
| 184 | |
| 185 | fn init(module: &'static ThisModule) -> error::Result<Self>;
|
| 186 | }
|
| 187 |
|
| 188 | |
| 189 | pub trait InPlaceModule: Sync + Send {
|
| 190 | |
| 191 | |
| 192 | |
| 193 | fn init(module: &'static ThisModule) -> impl pin_init::PinInit<Self, error::Error>;
|
| 194 | }
|
| 195 |
|
| 196 | impl<T: Module> InPlaceModule for T {
|
| 197 | fn init(module: &'static ThisModule) -> impl pin_init::PinInit<Self, error::Error> {
|
| 198 | let initer = move |slot: *mut Self| {
|
| 199 | let m = <Self as Module>::init(module)?;
|
| 200 |
|
| 201 | |
| 202 | unsafe { slot.write(m) };
|
| 203 | Ok(())
|
| 204 | };
|
| 205 |
|
| 206 | |
| 207 | unsafe { pin_init::pin_init_from_closure(initer) }
|
| 208 | }
|
| 209 | }
|
| 210 |
|
| 211 | |
| 212 | pub trait ModuleMetadata {
|
| 213 | |
| 214 | const NAME: &'static crate::str::CStr;
|
| 215 | }
|
| 216 |
|
| 217 | |
| 218 | |
| 219 | |
| 220 | pub struct ThisModule(*mut bindings::module);
|
| 221 |
|
| 222 | |
| 223 | unsafe impl Sync for ThisModule {}
|
| 224 |
|
| 225 | impl ThisModule {
|
| 226 | |
| 227 | |
| 228 | |
| 229 | |
| 230 | |
| 231 | pub const unsafe fn from_ptr(ptr: *mut bindings::module) -> ThisModule {
|
| 232 | ThisModule(ptr)
|
| 233 | }
|
| 234 |
|
| 235 | |
| 236 | |
| 237 | |
| 238 | pub const fn as_ptr(&self) -> *mut bindings::module {
|
| 239 | self.0
|
| 240 | }
|
| 241 | }
|
| 242 |
|
| 243 | #[cfg(not(testlib))]
|
| 244 | #[panic_handler]
|
| 245 | fn panic(info: &core::panic::PanicInfo<'_>) -> ! {
|
| 246 | pr_emerg!("{}\n", info);
|
| 247 | |
| 248 | unsafe { bindings::BUG() };
|
| 249 | }
|
| 250 |
|
| 251 | |
| 252 | |
| 253 | |
| 254 | |
| 255 | |
| 256 | |
| 257 | |
| 258 | |
| 259 | |
| 260 | |
| 261 | |
| 262 | |
| 263 | |
| 264 | |
| 265 | |
| 266 | |
| 267 | |
| 268 | |
| 269 | |
| 270 | |
| 271 | |
| 272 | |
| 273 | |
| 274 | |
| 275 | |
| 276 | |
| 277 | |
| 278 | |
| 279 | |
| 280 | |
| 281 | #[macro_export]
|
| 282 | macro_rules! container_of {
|
| 283 | ($field_ptr:expr, $Container:ty, $($fields:tt)*) => {{
|
| 284 | let offset: usize = ::core::mem::offset_of!($Container, $($fields)*);
|
| 285 | let field_ptr = $field_ptr;
|
| 286 | let container_ptr = field_ptr.byte_sub(offset).cast::<$Container>();
|
| 287 | $crate::assert_same_type(field_ptr, (&raw const (*container_ptr).$($fields)*).cast_mut());
|
| 288 | container_ptr
|
| 289 | }}
|
| 290 | }
|
| 291 |
|
| 292 | |
| 293 | #[doc(hidden)]
|
| 294 | pub fn assert_same_type<T>(_: T, _: T) {}
|
| 295 |
|
| 296 | |
| 297 | #[doc(hidden)]
|
| 298 | #[macro_export]
|
| 299 | macro_rules! concat_literals {
|
| 300 | ($( $asm:literal )* ) => {
|
| 301 | ::core::concat!($($asm),*)
|
| 302 | };
|
| 303 | }
|
| 304 |
|
| 305 | |
| 306 | |
| 307 | |
| 308 | |
| 309 | |
| 310 | #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
|
| 311 | #[macro_export]
|
| 312 | macro_rules! asm {
|
| 313 | ($($asm:expr),* ; $($rest:tt)*) => {
|
| 314 | ::core::arch::asm!( $($asm)*, options(att_syntax), $($rest)* )
|
| 315 | };
|
| 316 | }
|
| 317 |
|
| 318 | |
| 319 | |
| 320 | |
| 321 | |
| 322 | |
| 323 | #[cfg(not(any(target_arch = "x86", target_arch = "x86_64")))]
|
| 324 | #[macro_export]
|
| 325 | macro_rules! asm {
|
| 326 | ($($asm:expr),* ; $($rest:tt)*) => {
|
| 327 | ::core::arch::asm!( $($asm)*, $($rest)* )
|
| 328 | };
|
| 329 | }
|
| 330 |
|
| 331 | |
| 332 | |
| 333 | |
| 334 | |
| 335 | |
| 336 | |
| 337 | |
| 338 | |
| 339 | |
| 340 | |
| 341 | |
| 342 | |
| 343 | |
| 344 | |
| 345 | |
| 346 | |
| 347 | |
| 348 | |
| 349 | |
| 350 | |
| 351 | |
| 352 | |
| 353 | |
| 354 | |
| 355 | |
| 356 | |
| 357 | |
| 358 | |
| 359 | |
| 360 | |
| 361 | #[inline]
|
| 362 | pub fn file_from_location<'a>(loc: &'a core::panic::Location<'a>) -> &'a core::ffi::CStr {
|
| 363 | #[cfg(CONFIG_RUSTC_HAS_FILE_AS_C_STR)]
|
| 364 | {
|
| 365 | loc.file_as_c_str()
|
| 366 | }
|
| 367 |
|
| 368 | #[cfg(all(CONFIG_RUSTC_HAS_FILE_WITH_NUL, not(CONFIG_RUSTC_HAS_FILE_AS_C_STR)))]
|
| 369 | {
|
| 370 | loc.file_with_nul()
|
| 371 | }
|
| 372 |
|
| 373 | #[cfg(not(CONFIG_RUSTC_HAS_FILE_WITH_NUL))]
|
| 374 | {
|
| 375 | let _ = loc;
|
| 376 | c"<Location::file_as_c_str() not supported>"
|
| 377 | }
|
| 378 | }
|