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