Skip to content

Commit 0a99f23

Browse files
committed
Remove arch/ blocker by passing CONFIG vars to emulate module_init fully
Signed-off-by: Miguel Ojeda <[email protected]>
1 parent d55f1d9 commit 0a99f23

File tree

5 files changed

+20
-7
lines changed

5 files changed

+20
-7
lines changed

Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -521,7 +521,7 @@ KBUILD_RUSTCFLAGS_KERNEL :=
521521
KBUILD_CARGOFLAGS_KERNEL :=
522522
KBUILD_AFLAGS_MODULE := -DMODULE
523523
KBUILD_CFLAGS_MODULE := -DMODULE
524-
KBUILD_RUSTCFLAGS_MODULE := --cfg module
524+
KBUILD_RUSTCFLAGS_MODULE := --cfg MODULE
525525
KBUILD_CARGOFLAGS_MODULE :=
526526
KBUILD_LDFLAGS_MODULE :=
527527
export KBUILD_LDS_MODULE := $(srctree)/scripts/module-common.lds

arch/x86/Kconfig

+1-1
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ config X86
148148
select HAVE_ARCH_MMAP_RND_BITS if MMU
149149
select HAVE_ARCH_MMAP_RND_COMPAT_BITS if MMU && COMPAT
150150
select HAVE_ARCH_COMPAT_MMAP_BASES if MMU && COMPAT
151-
#select HAVE_ARCH_PREL32_RELOCATIONS TODO: see the `kernel_module` macro
151+
select HAVE_ARCH_PREL32_RELOCATIONS
152152
select HAVE_ARCH_SECCOMP_FILTER
153153
select HAVE_ARCH_THREAD_STRUCT_WHITELIST
154154
select HAVE_ARCH_STACKLEAK

drivers/char/rust_example/src/lib.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// SPDX-License-Identifier: GPL-2.0
22

33
#![no_std]
4+
#![feature(global_asm)]
45

56
use kernel::prelude::*;
67

@@ -11,7 +12,7 @@ struct RustExample {
1112
impl KernelModule for RustExample {
1213
fn init() -> KernelResult<Self> {
1314
println!("Rust Example (init)");
14-
println!("Am I built-in? {}", !cfg!(module));
15+
println!("Am I built-in? {}", !cfg!(MODULE));
1516
Ok(RustExample {
1617
message: "on the heap!".to_owned(),
1718
})

rust/kernel/src/lib.rs

+13-3
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,23 @@ macro_rules! kernel_module {
5353

5454
// Built-in modules are initialized through an initcall pointer
5555
//
56-
// TODO: find a proper way to emulate the C macro (`module_init`),
57-
// including dealing with `HAVE_ARCH_PREL32_RELOCATIONS`
58-
#[cfg(not(module))]
56+
// TODO: should we compile a C file on the fly to avoid duplication?
57+
#[cfg(not(MODULE))]
58+
#[cfg(not(CONFIG_HAVE_ARCH_PREL32_RELOCATIONS))]
5959
#[link_section = ".initcall6.init"]
6060
#[used]
6161
pub static __initcall: extern "C" fn() -> $crate::c_types::c_int = init_module;
6262

63+
#[cfg(not(MODULE))]
64+
#[cfg(CONFIG_HAVE_ARCH_PREL32_RELOCATIONS)]
65+
global_asm!(
66+
r#".section ".initcall6.init", "a"
67+
__initcall:
68+
.long init_module - .
69+
.previous
70+
"#
71+
);
72+
6373
// TODO: pass the kernel module name here to generate a unique,
6474
// helpful symbol name (the name would also useful for the `modinfo`
6575
// issue below).

scripts/Makefile.lib

+3-1
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,9 @@ c_flags = -Wp,-MMD,$(depfile) $(NOSTDINC_FLAGS) $(LINUXINCLUDE) \
229229
RUST_BINDGEN_CFLAGS = $(c_flags) $(KBUILD_CFLAGS_MODULE)
230230
export RUST_BINDGEN_CFLAGS
231231

232-
rustc_flags = $(_rustc_flags) $(modkern_rustcflags)
232+
rustc_cfg_flags = $(shell sed -nE 's/^(CONFIG_[^=]+)=(y|m)$$/--cfg \1/p' $(srctree)/include/config/auto.conf | xargs)
233+
234+
rustc_flags = $(_rustc_flags) $(modkern_rustcflags) $(rustc_cfg_flags)
233235

234236
# Passed by cargo
235237
RUSTFLAGS = $(rustc_flags)

0 commit comments

Comments
 (0)