| 1 | |
| 2 |
|
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 |
|
| 14 | #![no_std]
|
| 15 | #![feature(coerce_unsized)]
|
| 16 | #![feature(dispatch_from_dyn)]
|
| 17 | #![feature(new_uninit)]
|
| 18 | #![feature(receiver_trait)]
|
| 19 | #![feature(unsize)]
|
| 20 |
|
| 21 | |
| 22 | |
| 23 | #[cfg(not(CONFIG_RUST))]
|
| 24 | compile_error!("Missing kernel configuration for conditional compilation");
|
| 25 |
|
| 26 | |
| 27 | extern crate self as kernel;
|
| 28 |
|
| 29 | pub mod alloc;
|
| 30 | #[cfg(CONFIG_BLOCK)]
|
| 31 | pub mod block;
|
| 32 | mod build_assert;
|
| 33 | pub mod device;
|
| 34 | pub mod error;
|
| 35 | #[cfg(CONFIG_RUST_FW_LOADER_ABSTRACTIONS)]
|
| 36 | pub mod firmware;
|
| 37 | pub mod init;
|
| 38 | pub mod ioctl;
|
| 39 | #[cfg(CONFIG_KUNIT)]
|
| 40 | pub mod kunit;
|
| 41 | pub mod list;
|
| 42 | #[cfg(CONFIG_NET)]
|
| 43 | pub mod net;
|
| 44 | pub mod page;
|
| 45 | pub mod prelude;
|
| 46 | pub mod print;
|
| 47 | pub mod rbtree;
|
| 48 | pub mod sizes;
|
| 49 | mod static_assert;
|
| 50 | #[doc(hidden)]
|
| 51 | pub mod std_vendor;
|
| 52 | pub mod str;
|
| 53 | pub mod sync;
|
| 54 | pub mod task;
|
| 55 | pub mod time;
|
| 56 | pub mod types;
|
| 57 | pub mod uaccess;
|
| 58 | pub mod workqueue;
|
| 59 |
|
| 60 | #[doc(hidden)]
|
| 61 | pub use bindings;
|
| 62 | pub use macros;
|
| 63 | pub use uapi;
|
| 64 |
|
| 65 | #[doc(hidden)]
|
| 66 | pub use build_error::build_error;
|
| 67 |
|
| 68 | |
| 69 | const __LOG_PREFIX: &[u8] = b"rust_kernel\0";
|
| 70 |
|
| 71 | |
| 72 | |
| 73 | |
| 74 | pub trait Module: Sized + Sync + Send {
|
| 75 | |
| 76 | |
| 77 | |
| 78 | |
| 79 | |
| 80 | |
| 81 | fn init(module: &'static ThisModule) -> error::Result<Self>;
|
| 82 | }
|
| 83 |
|
| 84 | |
| 85 | |
| 86 | |
| 87 | pub struct ThisModule(*mut bindings::module);
|
| 88 |
|
| 89 | |
| 90 | unsafe impl Sync for ThisModule {}
|
| 91 |
|
| 92 | impl ThisModule {
|
| 93 | |
| 94 | |
| 95 | |
| 96 | |
| 97 | |
| 98 | pub const unsafe fn from_ptr(ptr: *mut bindings::module) -> ThisModule {
|
| 99 | ThisModule(ptr)
|
| 100 | }
|
| 101 |
|
| 102 | |
| 103 | |
| 104 | |
| 105 | pub const fn as_ptr(&self) -> *mut bindings::module {
|
| 106 | self.0
|
| 107 | }
|
| 108 | }
|
| 109 |
|
| 110 | #[cfg(not(any(testlib, test)))]
|
| 111 | #[panic_handler]
|
| 112 | fn panic(info: &core::panic::PanicInfo<'_>) -> ! {
|
| 113 | pr_emerg!("{}\n", info);
|
| 114 | |
| 115 | unsafe { bindings::BUG() };
|
| 116 | }
|
| 117 |
|
| 118 | |
| 119 | |
| 120 | |
| 121 | |
| 122 | |
| 123 | |
| 124 | |
| 125 | |
| 126 | |
| 127 | |
| 128 | |
| 129 | |
| 130 | |
| 131 | |
| 132 | |
| 133 | |
| 134 | |
| 135 | |
| 136 | |
| 137 | |
| 138 | |
| 139 | |
| 140 | |
| 141 | #[macro_export]
|
| 142 | macro_rules! container_of {
|
| 143 | ($ptr:expr, $type:ty, $($f:tt)*) => {{
|
| 144 | let ptr = $ptr as *const _ as *const u8;
|
| 145 | let offset: usize = ::core::mem::offset_of!($type, $($f)*);
|
| 146 | ptr.sub(offset) as *const $type
|
| 147 | }}
|
| 148 | }
|