rust: sync: allow type of `self` to be `Arc<T>` or variants
[linux-block.git] / rust / kernel / lib.rs
CommitLineData
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 16#![feature(core_ffi_c)]
53528772 17#![feature(receiver_trait)]
247b365d
WAF
18
19// Ensure conditional compilation based on the kernel configuration works;
20// otherwise we may silently break things like initcall handling.
21#[cfg(not(CONFIG_RUST))]
22compile_error!("Missing kernel configuration for conditional compilation");
23
24#[cfg(not(test))]
25#[cfg(not(testlib))]
26mod allocator;
0f595bab 27mod build_assert;
247b365d
WAF
28pub mod error;
29pub mod prelude;
30pub mod print;
ef9e3797 31mod static_assert;
bee16889
NM
32#[doc(hidden)]
33pub mod std_vendor;
247b365d 34pub mod str;
9dc04365 35pub mod sync;
ba20915b 36pub mod types;
247b365d
WAF
37
38#[doc(hidden)]
39pub use bindings;
40pub use macros;
41
0f595bab
GG
42#[doc(hidden)]
43pub use build_error::build_error;
44
247b365d
WAF
45/// Prefix to appear before log messages printed from within the `kernel` crate.
46const __LOG_PREFIX: &[u8] = b"rust_kernel\0";
47
48/// The top level entrypoint to implementing a kernel module.
49///
50/// For any teardown or cleanup operations, your type may implement [`Drop`].
51pub trait Module: Sized + Sync {
52 /// Called at module initialization time.
53 ///
54 /// Use this method to perform whatever setup or registration your module
55 /// should do.
56 ///
57 /// Equivalent to the `module_init` macro in the C API.
58 fn init(module: &'static ThisModule) -> error::Result<Self>;
59}
60
61/// Equivalent to `THIS_MODULE` in the C API.
62///
63/// C header: `include/linux/export.h`
64pub struct ThisModule(*mut bindings::module);
65
66// SAFETY: `THIS_MODULE` may be used from all threads within a module.
67unsafe impl Sync for ThisModule {}
68
69impl ThisModule {
70 /// Creates a [`ThisModule`] given the `THIS_MODULE` pointer.
71 ///
72 /// # Safety
73 ///
74 /// The pointer must be equal to the right `THIS_MODULE`.
75 pub const unsafe fn from_ptr(ptr: *mut bindings::module) -> ThisModule {
76 ThisModule(ptr)
77 }
78}
79
80#[cfg(not(any(testlib, test)))]
81#[panic_handler]
82fn panic(info: &core::panic::PanicInfo<'_>) -> ! {
83 pr_emerg!("{}\n", info);
84 // SAFETY: FFI call.
85 unsafe { bindings::BUG() };
86 // Bindgen currently does not recognize `__noreturn` so `BUG` returns `()`
87 // instead of `!`. See <https://github.com/rust-lang/rust-bindgen/issues/2094>.
88 loop {}
89}