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