1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
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
260
261
262
263
264
265
266
267
#![no_std]
#![feature(allocator_api)]
#![feature(associated_type_defaults)]
#![feature(coerce_unsized)]
#![feature(const_mut_refs)]
#![feature(const_ptr_offset_from)]
#![feature(const_refs_to_cell)]
#![feature(const_trait_impl)]
#![feature(core_ffi_c)]
#![feature(c_size_t)]
#![feature(dispatch_from_dyn)]
#![feature(doc_cfg)]
#![feature(duration_constants)]
#![feature(generic_associated_types)]
#![feature(ptr_metadata)]
#![feature(receiver_trait)]
#![feature(unsize)]
#[cfg(not(CONFIG_RUST))]
compile_error!("Missing kernel configuration for conditional compilation");
#[cfg(not(test))]
#[cfg(not(testlib))]
mod allocator;
#[doc(hidden)]
pub use bindings;
pub use macros;
#[cfg(CONFIG_ARM_AMBA)]
pub mod amba;
pub mod chrdev;
#[cfg(CONFIG_COMMON_CLK)]
pub mod clk;
pub mod cred;
pub mod delay;
pub mod device;
pub mod driver;
pub mod error;
pub mod file;
pub mod fs;
pub mod gpio;
pub mod hwrng;
pub mod irq;
pub mod kasync;
pub mod miscdev;
pub mod mm;
#[cfg(CONFIG_NET)]
pub mod net;
pub mod pages;
pub mod power;
pub mod revocable;
pub mod security;
pub mod str;
pub mod task;
pub mod workqueue;
pub mod linked_list;
mod raw_list;
pub mod rbtree;
pub mod unsafe_list;
#[doc(hidden)]
pub mod module_param;
mod build_assert;
pub mod prelude;
pub mod print;
pub mod random;
mod static_assert;
#[doc(hidden)]
pub mod std_vendor;
pub mod sync;
#[cfg(any(CONFIG_SYSCTL, doc))]
#[doc(cfg(CONFIG_SYSCTL))]
pub mod sysctl;
pub mod io_buffer;
#[cfg(CONFIG_HAS_IOMEM)]
pub mod io_mem;
pub mod iov_iter;
pub mod of;
pub mod platform;
mod types;
pub mod user_ptr;
#[cfg(CONFIG_KUNIT)]
pub mod kunit;
#[doc(hidden)]
pub use build_error::build_error;
pub use crate::error::{to_result, Error, Result};
pub use crate::types::{
bit, bits_iter, ARef, AlwaysRefCounted, Bool, Either, Either::Left, Either::Right, False, Mode,
Opaque, PointerWrapper, ScopeGuard, True,
};
use core::marker::PhantomData;
pub const PAGE_SIZE: usize = 1 << bindings::PAGE_SHIFT;
const __LOG_PREFIX: &[u8] = b"rust_kernel\0";
pub trait Module: Sized + Sync {
fn init(name: &'static str::CStr, module: &'static ThisModule) -> Result<Self>;
}
pub struct ThisModule(*mut bindings::module);
unsafe impl Sync for ThisModule {}
impl ThisModule {
pub const unsafe fn from_ptr(ptr: *mut bindings::module) -> ThisModule {
ThisModule(ptr)
}
pub fn kernel_param_lock(&self) -> KParamGuard<'_> {
#[cfg(CONFIG_SYSFS)]
unsafe {
bindings::kernel_param_lock(self.0)
}
KParamGuard {
#[cfg(CONFIG_SYSFS)]
this_module: self,
phantom: PhantomData,
}
}
}
pub struct KParamGuard<'a> {
#[cfg(CONFIG_SYSFS)]
this_module: &'a ThisModule,
phantom: PhantomData<&'a ()>,
}
#[cfg(CONFIG_SYSFS)]
impl<'a> Drop for KParamGuard<'a> {
fn drop(&mut self) {
unsafe { bindings::kernel_param_unlock(self.this_module.0) }
}
}
#[macro_export]
macro_rules! offset_of {
($type:ty, $($f:tt)*) => {{
let tmp = core::mem::MaybeUninit::<$type>::uninit();
let outer = tmp.as_ptr();
#[allow(unused_unsafe)]
let inner = unsafe { core::ptr::addr_of!((*outer).$($f)*) } as *const u8;
#[allow(unused_unsafe)]
unsafe { inner.offset_from(outer as *const u8) }
}}
}
#[macro_export]
macro_rules! container_of {
($ptr:expr, $type:ty, $($f:tt)*) => {{
let ptr = $ptr as *const _ as *const u8;
let offset = $crate::offset_of!($type, $($f)*);
ptr.wrapping_offset(-offset) as *const $type
}}
}
#[cfg(not(any(testlib, test)))]
#[panic_handler]
fn panic(info: &core::panic::PanicInfo<'_>) -> ! {
pr_emerg!("{}\n", info);
unsafe { bindings::BUG() };
loop {}
}