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