File tree Expand file tree Collapse file tree 4 files changed +20
-4
lines changed Expand file tree Collapse file tree 4 files changed +20
-4
lines changed Original file line number Diff line number Diff 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)
Original file line number Diff line number Diff 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.
Original file line number Diff line number Diff 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+ // SAFETY: `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+
4759extern "C" {
4860 fn rust_helper_BUG ( ) -> !;
4961}
Original file line number Diff line number Diff 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]
You can’t perform that action at this time.
0 commit comments