Merge tag 'riscv-for-linus-6.6-mw1' of git://git.kernel.org/pub/scm/linux/kernel...
[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)]
0748424a 17#![feature(dispatch_from_dyn)]
3c01a424 18#![feature(new_uninit)]
53528772 19#![feature(receiver_trait)]
f75cb6fc 20#![feature(unsize)]
247b365d
WAF
21
22// Ensure conditional compilation based on the kernel configuration works;
23// otherwise we may silently break things like initcall handling.
24#[cfg(not(CONFIG_RUST))]
25compile_error!("Missing kernel configuration for conditional compilation");
26
90e53c5e
BL
27// Allow proc-macros to refer to `::kernel` inside the `kernel` crate (this crate).
28extern crate self as kernel;
29
247b365d
WAF
30#[cfg(not(test))]
31#[cfg(not(testlib))]
32mod allocator;
0f595bab 33mod build_assert;
247b365d 34pub mod error;
90e53c5e 35pub mod init;
ea76e08f 36pub mod ioctl;
a66d733d
MO
37#[cfg(CONFIG_KUNIT)]
38pub mod kunit;
247b365d
WAF
39pub mod prelude;
40pub mod print;
ef9e3797 41mod static_assert;
bee16889
NM
42#[doc(hidden)]
43pub mod std_vendor;
247b365d 44pub mod str;
9dc04365 45pub mod sync;
313c4281 46pub mod task;
ba20915b 47pub mod types;
247b365d
WAF
48
49#[doc(hidden)]
50pub use bindings;
51pub use macros;
4e174665 52pub use uapi;
247b365d 53
0f595bab
GG
54#[doc(hidden)]
55pub use build_error::build_error;
56
247b365d
WAF
57/// Prefix to appear before log messages printed from within the `kernel` crate.
58const __LOG_PREFIX: &[u8] = b"rust_kernel\0";
59
60/// The top level entrypoint to implementing a kernel module.
61///
62/// For any teardown or cleanup operations, your type may implement [`Drop`].
63pub trait Module: Sized + Sync {
64 /// Called at module initialization time.
65 ///
66 /// Use this method to perform whatever setup or registration your module
67 /// should do.
68 ///
69 /// Equivalent to the `module_init` macro in the C API.
70 fn init(module: &'static ThisModule) -> error::Result<Self>;
71}
72
73/// Equivalent to `THIS_MODULE` in the C API.
74///
75/// C header: `include/linux/export.h`
76pub struct ThisModule(*mut bindings::module);
77
78// SAFETY: `THIS_MODULE` may be used from all threads within a module.
79unsafe impl Sync for ThisModule {}
80
81impl ThisModule {
82 /// Creates a [`ThisModule`] given the `THIS_MODULE` pointer.
83 ///
84 /// # Safety
85 ///
86 /// The pointer must be equal to the right `THIS_MODULE`.
87 pub const unsafe fn from_ptr(ptr: *mut bindings::module) -> ThisModule {
88 ThisModule(ptr)
89 }
90}
91
92#[cfg(not(any(testlib, test)))]
93#[panic_handler]
94fn panic(info: &core::panic::PanicInfo<'_>) -> ! {
95 pr_emerg!("{}\n", info);
96 // SAFETY: FFI call.
97 unsafe { bindings::BUG() };
247b365d 98}