| 1 | |
| 2 |
|
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 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 | |
| 23 | |
| 24 | #[cfg(not(CONFIG_RUST))]
|
| 25 | compile_error!("Missing kernel configuration for conditional compilation");
|
| 26 |
|
| 27 | |
| 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 | pub mod miscdevice;
|
| 49 | #[cfg(CONFIG_NET)]
|
| 50 | pub mod net;
|
| 51 | pub mod page;
|
| 52 | pub mod pid_namespace;
|
| 53 | pub mod prelude;
|
| 54 | pub mod print;
|
| 55 | pub mod rbtree;
|
| 56 | pub mod security;
|
| 57 | pub mod seq_file;
|
| 58 | pub mod sizes;
|
| 59 | mod static_assert;
|
| 60 | #[doc(hidden)]
|
| 61 | pub mod std_vendor;
|
| 62 | pub mod str;
|
| 63 | pub mod sync;
|
| 64 | pub mod task;
|
| 65 | pub mod time;
|
| 66 | pub mod tracepoint;
|
| 67 | pub mod transmute;
|
| 68 | pub mod types;
|
| 69 | pub mod uaccess;
|
| 70 | pub mod workqueue;
|
| 71 |
|
| 72 | #[doc(hidden)]
|
| 73 | pub use bindings;
|
| 74 | pub use macros;
|
| 75 | pub use uapi;
|
| 76 |
|
| 77 | #[doc(hidden)]
|
| 78 | pub use build_error::build_error;
|
| 79 |
|
| 80 | |
| 81 | const __LOG_PREFIX: &[u8] = b"rust_kernel\0";
|
| 82 |
|
| 83 | |
| 84 | |
| 85 | |
| 86 | pub trait Module: Sized + Sync + Send {
|
| 87 | |
| 88 | |
| 89 | |
| 90 | |
| 91 | |
| 92 | |
| 93 | fn init(module: &'static ThisModule) -> error::Result<Self>;
|
| 94 | }
|
| 95 |
|
| 96 | |
| 97 | pub trait InPlaceModule: Sync + Send {
|
| 98 | |
| 99 | |
| 100 | |
| 101 | fn init(module: &'static ThisModule) -> impl init::PinInit<Self, error::Error>;
|
| 102 | }
|
| 103 |
|
| 104 | impl<T: Module> InPlaceModule for T {
|
| 105 | fn init(module: &'static ThisModule) -> impl init::PinInit<Self, error::Error> {
|
| 106 | let initer = move |slot: *mut Self| {
|
| 107 | let m = <Self as Module>::init(module)?;
|
| 108 |
|
| 109 | |
| 110 | unsafe { slot.write(m) };
|
| 111 | Ok(())
|
| 112 | };
|
| 113 |
|
| 114 | |
| 115 | unsafe { init::pin_init_from_closure(initer) }
|
| 116 | }
|
| 117 | }
|
| 118 |
|
| 119 | |
| 120 | |
| 121 | |
| 122 | pub struct ThisModule(*mut bindings::module);
|
| 123 |
|
| 124 | |
| 125 | unsafe impl Sync for ThisModule {}
|
| 126 |
|
| 127 | impl ThisModule {
|
| 128 | |
| 129 | |
| 130 | |
| 131 | |
| 132 | |
| 133 | pub const unsafe fn from_ptr(ptr: *mut bindings::module) -> ThisModule {
|
| 134 | ThisModule(ptr)
|
| 135 | }
|
| 136 |
|
| 137 | |
| 138 | |
| 139 | |
| 140 | pub const fn as_ptr(&self) -> *mut bindings::module {
|
| 141 | self.0
|
| 142 | }
|
| 143 | }
|
| 144 |
|
| 145 | #[cfg(not(any(testlib, test)))]
|
| 146 | #[panic_handler]
|
| 147 | fn panic(info: &core::panic::PanicInfo<'_>) -> ! {
|
| 148 | pr_emerg!("{}\n", info);
|
| 149 | |
| 150 | unsafe { bindings::BUG() };
|
| 151 | }
|
| 152 |
|
| 153 | |
| 154 | |
| 155 | |
| 156 | |
| 157 | |
| 158 | |
| 159 | |
| 160 | |
| 161 | |
| 162 | |
| 163 | |
| 164 | |
| 165 | |
| 166 | |
| 167 | |
| 168 | |
| 169 | |
| 170 | |
| 171 | |
| 172 | |
| 173 | |
| 174 | |
| 175 | |
| 176 | #[macro_export]
|
| 177 | macro_rules! container_of {
|
| 178 | ($ptr:expr, $type:ty, $($f:tt)*) => {{
|
| 179 | let ptr = $ptr as *const _ as *const u8;
|
| 180 | let offset: usize = ::core::mem::offset_of!($type, $($f)*);
|
| 181 | ptr.sub(offset) as *const $type
|
| 182 | }}
|
| 183 | }
|
| 184 |
|
| 185 | |
| 186 | #[doc(hidden)]
|
| 187 | #[macro_export]
|
| 188 | macro_rules! concat_literals {
|
| 189 | ($( $asm:literal )* ) => {
|
| 190 | ::core::concat!($($asm),*)
|
| 191 | };
|
| 192 | }
|
| 193 |
|
| 194 | |
| 195 | |
| 196 | |
| 197 | |
| 198 | |
| 199 | #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
|
| 200 | #[macro_export]
|
| 201 | macro_rules! asm {
|
| 202 | ($($asm:expr),* ; $($rest:tt)*) => {
|
| 203 | ::core::arch::asm!( $($asm)*, options(att_syntax), $($rest)* )
|
| 204 | };
|
| 205 | }
|
| 206 |
|
| 207 | |
| 208 | |
| 209 | |
| 210 | |
| 211 | |
| 212 | #[cfg(not(any(target_arch = "x86", target_arch = "x86_64")))]
|
| 213 | #[macro_export]
|
| 214 | macro_rules! asm {
|
| 215 | ($($asm:expr),* ; $($rest:tt)*) => {
|
| 216 | ::core::arch::asm!( $($asm)*, $($rest)* )
|
| 217 | };
|
| 218 | }
|