Linux Audio

Check our new training course

Loading...
Note: File does not exist in v4.6.
  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))]
 28compile_error!("Missing kernel configuration for conditional compilation");
 29
 30// Allow proc-macros to refer to `::kernel` inside the `kernel` crate (this crate).
 31extern crate self as kernel;
 32
 33#[cfg(not(test))]
 34#[cfg(not(testlib))]
 35mod allocator;
 36mod build_assert;
 37pub mod error;
 38pub mod init;
 39pub mod ioctl;
 40#[cfg(CONFIG_KUNIT)]
 41pub mod kunit;
 42#[cfg(CONFIG_NET)]
 43pub mod net;
 44pub mod prelude;
 45pub mod print;
 46mod static_assert;
 47#[doc(hidden)]
 48pub mod std_vendor;
 49pub mod str;
 50pub mod sync;
 51pub mod task;
 52pub mod types;
 53pub mod workqueue;
 54
 55#[doc(hidden)]
 56pub use bindings;
 57pub use macros;
 58pub use uapi;
 59
 60#[doc(hidden)]
 61pub use build_error::build_error;
 62
 63/// Prefix to appear before log messages printed from within the `kernel` crate.
 64const __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`].
 69pub 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`
 82pub struct ThisModule(*mut bindings::module);
 83
 84// SAFETY: `THIS_MODULE` may be used from all threads within a module.
 85unsafe impl Sync for ThisModule {}
 86
 87impl 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]
100fn panic(info: &core::panic::PanicInfo<'_>) -> ! {
101    pr_emerg!("{}\n", info);
102    // SAFETY: FFI call.
103    unsafe { bindings::BUG() };
104}