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