Commit | Line | Data |
---|---|---|
247b365d WAF |
1 | // SPDX-License-Identifier: GPL-2.0 |
2 | ||
3 | //! The `kernel` crate. | |
4 | //! | |
5 | //! This crate contains the kernel APIs that have been ported or wrapped for | |
6 | //! usage by Rust code in the kernel and is shared by all of them. | |
7 | //! | |
8 | //! In other words, all the rest of the Rust code in the kernel (e.g. kernel | |
9 | //! modules written in Rust) depends on [`core`], [`alloc`] and this crate. | |
10 | //! | |
11 | //! If you need a kernel C API that is not ported or wrapped yet here, then | |
12 | //! do so first instead of bypassing this crate. | |
13 | ||
14 | #![no_std] | |
76e2c2d9 | 15 | #![feature(allocator_api)] |
247b365d WAF |
16 | #![feature(core_ffi_c)] |
17 | ||
18 | // Ensure conditional compilation based on the kernel configuration works; | |
19 | // otherwise we may silently break things like initcall handling. | |
20 | #[cfg(not(CONFIG_RUST))] | |
21 | compile_error!("Missing kernel configuration for conditional compilation"); | |
22 | ||
23 | #[cfg(not(test))] | |
24 | #[cfg(not(testlib))] | |
25 | mod allocator; | |
26 | pub mod error; | |
27 | pub mod prelude; | |
28 | pub mod print; | |
29 | pub mod str; | |
30 | ||
31 | #[doc(hidden)] | |
32 | pub use bindings; | |
33 | pub use macros; | |
34 | ||
35 | /// Prefix to appear before log messages printed from within the `kernel` crate. | |
36 | const __LOG_PREFIX: &[u8] = b"rust_kernel\0"; | |
37 | ||
38 | /// The top level entrypoint to implementing a kernel module. | |
39 | /// | |
40 | /// For any teardown or cleanup operations, your type may implement [`Drop`]. | |
41 | pub trait Module: Sized + Sync { | |
42 | /// Called at module initialization time. | |
43 | /// | |
44 | /// Use this method to perform whatever setup or registration your module | |
45 | /// should do. | |
46 | /// | |
47 | /// Equivalent to the `module_init` macro in the C API. | |
48 | fn init(module: &'static ThisModule) -> error::Result<Self>; | |
49 | } | |
50 | ||
51 | /// Equivalent to `THIS_MODULE` in the C API. | |
52 | /// | |
53 | /// C header: `include/linux/export.h` | |
54 | pub struct ThisModule(*mut bindings::module); | |
55 | ||
56 | // SAFETY: `THIS_MODULE` may be used from all threads within a module. | |
57 | unsafe impl Sync for ThisModule {} | |
58 | ||
59 | impl ThisModule { | |
60 | /// Creates a [`ThisModule`] given the `THIS_MODULE` pointer. | |
61 | /// | |
62 | /// # Safety | |
63 | /// | |
64 | /// The pointer must be equal to the right `THIS_MODULE`. | |
65 | pub const unsafe fn from_ptr(ptr: *mut bindings::module) -> ThisModule { | |
66 | ThisModule(ptr) | |
67 | } | |
68 | } | |
69 | ||
70 | #[cfg(not(any(testlib, test)))] | |
71 | #[panic_handler] | |
72 | fn panic(info: &core::panic::PanicInfo<'_>) -> ! { | |
73 | pr_emerg!("{}\n", info); | |
74 | // SAFETY: FFI call. | |
75 | unsafe { bindings::BUG() }; | |
76 | // Bindgen currently does not recognize `__noreturn` so `BUG` returns `()` | |
77 | // instead of `!`. See <https://github.com/rust-lang/rust-bindgen/issues/2094>. | |
78 | loop {} | |
79 | } |