lib.rs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. // SPDX-License-Identifier: GPL-2.0
  2. //! The `kernel` crate.
  3. //!
  4. //! This crate contains the kernel APIs that have been ported or wrapped for
  5. //! usage by Rust code in the kernel and is shared by all of them.
  6. //!
  7. //! In other words, all the rest of the Rust code in the kernel (e.g. kernel
  8. //! modules written in Rust) depends on [`core`] and this crate.
  9. //!
  10. //! If you need a kernel C API that is not ported or wrapped yet here, then
  11. //! do so first instead of bypassing this crate.
  12. #![no_std]
  13. #![feature(arbitrary_self_types)]
  14. #![feature(coerce_unsized)]
  15. #![feature(dispatch_from_dyn)]
  16. #![feature(inline_const)]
  17. #![feature(lint_reasons)]
  18. #![feature(unsize)]
  19. #![feature(used_with_arg)]
  20. // Ensure conditional compilation based on the kernel configuration works;
  21. // otherwise we may silently break things like initcall handling.
  22. #[cfg(not(CONFIG_RUST))]
  23. compile_error!("Missing kernel configuration for conditional compilation");
  24. // Allow proc-macros to refer to `::kernel` inside the `kernel` crate (this crate).
  25. extern crate self as kernel;
  26. pub use ffi;
  27. pub mod alloc;
  28. #[cfg(CONFIG_BLOCK)]
  29. pub mod block;
  30. mod build_assert;
  31. pub mod device;
  32. pub mod error;
  33. #[cfg(CONFIG_RUST_FW_LOADER_ABSTRACTIONS)]
  34. pub mod firmware;
  35. pub mod init;
  36. pub mod ioctl;
  37. #[cfg(CONFIG_KUNIT)]
  38. pub mod kunit;
  39. pub mod list;
  40. #[cfg(CONFIG_NET)]
  41. pub mod net;
  42. pub mod page;
  43. pub mod prelude;
  44. pub mod print;
  45. pub mod rbtree;
  46. pub mod sizes;
  47. mod static_assert;
  48. #[doc(hidden)]
  49. pub mod std_vendor;
  50. pub mod str;
  51. pub mod sync;
  52. pub mod task;
  53. pub mod time;
  54. pub mod types;
  55. pub mod uaccess;
  56. pub mod workqueue;
  57. #[doc(hidden)]
  58. pub use bindings;
  59. pub use macros;
  60. pub use uapi;
  61. #[doc(hidden)]
  62. pub use build_error::build_error;
  63. /// Prefix to appear before log messages printed from within the `kernel` crate.
  64. const __LOG_PREFIX: &[u8] = b"rust_kernel\0";
  65. /// The top level entrypoint to implementing a kernel module.
  66. ///
  67. /// For any teardown or cleanup operations, your type may implement [`Drop`].
  68. pub trait Module: Sized + Sync + Send {
  69. /// Called at module initialization time.
  70. ///
  71. /// Use this method to perform whatever setup or registration your module
  72. /// should do.
  73. ///
  74. /// Equivalent to the `module_init` macro in the C API.
  75. fn init(module: &'static ThisModule) -> error::Result<Self>;
  76. }
  77. /// Equivalent to `THIS_MODULE` in the C API.
  78. ///
  79. /// C header: [`include/linux/init.h`](srctree/include/linux/init.h)
  80. pub struct ThisModule(*mut bindings::module);
  81. // SAFETY: `THIS_MODULE` may be used from all threads within a module.
  82. unsafe impl Sync for ThisModule {}
  83. impl ThisModule {
  84. /// Creates a [`ThisModule`] given the `THIS_MODULE` pointer.
  85. ///
  86. /// # Safety
  87. ///
  88. /// The pointer must be equal to the right `THIS_MODULE`.
  89. pub const unsafe fn from_ptr(ptr: *mut bindings::module) -> ThisModule {
  90. ThisModule(ptr)
  91. }
  92. /// Access the raw pointer for this module.
  93. ///
  94. /// It is up to the user to use it correctly.
  95. pub const fn as_ptr(&self) -> *mut bindings::module {
  96. self.0
  97. }
  98. }
  99. #[cfg(not(any(testlib, test)))]
  100. #[panic_handler]
  101. fn panic(info: &core::panic::PanicInfo<'_>) -> ! {
  102. pr_emerg!("{}\n", info);
  103. // SAFETY: FFI call.
  104. unsafe { bindings::BUG() };
  105. }
  106. /// Produces a pointer to an object from a pointer to one of its fields.
  107. ///
  108. /// # Safety
  109. ///
  110. /// The pointer passed to this macro, and the pointer returned by this macro, must both be in
  111. /// bounds of the same allocation.
  112. ///
  113. /// # Examples
  114. ///
  115. /// ```
  116. /// # use kernel::container_of;
  117. /// struct Test {
  118. /// a: u64,
  119. /// b: u32,
  120. /// }
  121. ///
  122. /// let test = Test { a: 10, b: 20 };
  123. /// let b_ptr = &test.b;
  124. /// // SAFETY: The pointer points at the `b` field of a `Test`, so the resulting pointer will be
  125. /// // in-bounds of the same allocation as `b_ptr`.
  126. /// let test_alias = unsafe { container_of!(b_ptr, Test, b) };
  127. /// assert!(core::ptr::eq(&test, test_alias));
  128. /// ```
  129. #[macro_export]
  130. macro_rules! container_of {
  131. ($ptr:expr, $type:ty, $($f:tt)*) => {{
  132. let ptr = $ptr as *const _ as *const u8;
  133. let offset: usize = ::core::mem::offset_of!($type, $($f)*);
  134. ptr.sub(offset) as *const $type
  135. }}
  136. }