Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 9e44590

Browse files
committedJan 23, 2021
Implement THIS_MODULE.
Closes #15 Fixes #10
1 parent d8e8a16 commit 9e44590

File tree

4 files changed

+20
-4
lines changed

4 files changed

+20
-4
lines changed
 

‎rust/Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ quiet_cmd_bindgen = BINDGEN $@
3838
cmd_bindgen = \
3939
$(BINDGEN) $< $(addprefix --opaque-type , $(bindgen_opaque_types)) \
4040
--use-core --with-derive-default --ctypes-prefix c_types \
41-
--size_t-is-usize -o $@ -- $(bindgen_c_flags)
41+
--size_t-is-usize -o $@ -- $(bindgen_c_flags) -DMODULE
4242

4343
$(objtree)/rust/bindings_generated.rs: $(srctree)/rust/kernel/bindings_helper.h FORCE
4444
$(call if_changed_dep,bindgen)

‎rust/kernel/chrdev.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ impl Builder {
3838
self
3939
}
4040

41-
pub fn build(self) -> KernelResult<Registration> {
41+
pub fn build(self, this_module: &'static crate::ThisModule) -> KernelResult<Registration> {
4242
let mut dev: bindings::dev_t = 0;
4343
let res = unsafe {
4444
bindings::alloc_chrdev_region(
@@ -58,8 +58,7 @@ impl Builder {
5858
for (i, file_op) in self.file_ops.iter().enumerate() {
5959
unsafe {
6060
bindings::cdev_init(&mut cdevs[i], *file_op);
61-
// TODO: proper `THIS_MODULE` handling
62-
cdevs[i].owner = core::ptr::null_mut();
61+
cdevs[i].owner = this_module.0;
6362
let rc = bindings::cdev_add(&mut cdevs[i], dev + i as bindings::dev_t, 1);
6463
if rc != 0 {
6564
// Clean up the ones that were allocated.

‎rust/kernel/lib.rs

+12
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,18 @@ pub trait KernelModule: Sized + Sync {
4444
fn init() -> KernelResult<Self>;
4545
}
4646

47+
/// An instance equivalent to `THIS_MODULE` in C code.
48+
pub struct ThisModule(*mut bindings::module);
49+
50+
// Safe because THIS_MODULE may be used from all threads within a module.
51+
unsafe impl Sync for ThisModule {}
52+
53+
impl ThisModule {
54+
pub const unsafe fn from_ptr(ptr: *mut bindings::module) -> ThisModule {
55+
ThisModule(ptr)
56+
}
57+
}
58+
4759
extern "C" {
4860
fn rust_helper_BUG() -> !;
4961
}

‎rust/module.rs

+5
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,11 @@ pub fn module(ts: TokenStream) -> TokenStream {
309309
"
310310
static mut __MOD: Option<{type_}> = None;
311311
312+
#[cfg(MODULE)]
313+
static THIS_MODULE: kernel::ThisModule = unsafe {{ kernel::ThisModule::from_ptr(&kernel::bindings::__this_module as *const _ as *mut _) }};
314+
#[cfg(not(MODULE))]
315+
static THIS_MODULE: kernel::ThisModule = unsafe {{ kernel::ThisModule::from_ptr(core::ptr::null_mut()) }};
316+
312317
// Loadable modules need to export the `{{init,cleanup}}_module` identifiers
313318
#[cfg(MODULE)]
314319
#[no_mangle]

0 commit comments

Comments
 (0)
Please sign in to comment.