File tree 3 files changed +19
-3
lines changed
3 files changed +19
-3
lines changed Original file line number Diff line number Diff line change @@ -38,7 +38,7 @@ impl Builder {
38
38
self
39
39
}
40
40
41
- pub fn build ( self ) -> KernelResult < Registration > {
41
+ pub fn build ( self , this_module : & ' static crate :: ThisModule ) -> KernelResult < Registration > {
42
42
let mut dev: bindings:: dev_t = 0 ;
43
43
let res = unsafe {
44
44
bindings:: alloc_chrdev_region (
@@ -58,8 +58,7 @@ impl Builder {
58
58
for ( i, file_op) in self . file_ops . iter ( ) . enumerate ( ) {
59
59
unsafe {
60
60
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 ;
63
62
let rc = bindings:: cdev_add ( & mut cdevs[ i] , dev + i as bindings:: dev_t , 1 ) ;
64
63
if rc != 0 {
65
64
// Clean up the ones that were allocated.
Original file line number Diff line number Diff line change @@ -44,6 +44,18 @@ pub trait KernelModule: Sized + Sync {
44
44
fn init ( ) -> KernelResult < Self > ;
45
45
}
46
46
47
+ /// An instance equivalent to `THIS_MODULE` in C code.
48
+ 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
+ const unsafe fn from_ptr ( ptr : * mut bindings:: module ) -> ThisModule {
55
+ ThisModule ( ptr)
56
+ }
57
+ }
58
+
47
59
extern "C" {
48
60
fn rust_helper_BUG ( ) -> !;
49
61
}
Original file line number Diff line number Diff line change @@ -309,6 +309,11 @@ pub fn module(ts: TokenStream) -> TokenStream {
309
309
"
310
310
static mut __MOD: Option<{type_}> = None;
311
311
312
+ #[cfg(MODULE)]
313
+ static THIS_MODULE: kernel::ThisModule = kernel::ThisModule::from_ptr(&mut kernel::bindings::__this_module);
314
+ #[cfg(not(MODULE))]
315
+ static THIS_MODULE: kernel::ThisModule = kernel::ThisModule::from_ptr(core::ptr::null_mut());
316
+
312
317
// Loadable modules need to export the `{{init,cleanup}}_module` identifiers
313
318
#[cfg(MODULE)]
314
319
#[no_mangle]
You can’t perform that action at this time.
0 commit comments