Skip to content

Commit

Permalink
Remove arch/ blocker by passing CONFIG vars to emulate module_init fully
Browse files Browse the repository at this point in the history
Signed-off-by: Miguel Ojeda <miguel.ojeda.sandonis@gmail.com>
  • Loading branch information
ojeda committed Sep 8, 2020
1 parent d55f1d9 commit 0a99f23
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 7 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -521,7 +521,7 @@ KBUILD_RUSTCFLAGS_KERNEL :=
KBUILD_CARGOFLAGS_KERNEL :=
KBUILD_AFLAGS_MODULE := -DMODULE
KBUILD_CFLAGS_MODULE := -DMODULE
KBUILD_RUSTCFLAGS_MODULE := --cfg module
KBUILD_RUSTCFLAGS_MODULE := --cfg MODULE
KBUILD_CARGOFLAGS_MODULE :=
KBUILD_LDFLAGS_MODULE :=
export KBUILD_LDS_MODULE := $(srctree)/scripts/module-common.lds
Expand Down
2 changes: 1 addition & 1 deletion arch/x86/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ config X86
select HAVE_ARCH_MMAP_RND_BITS if MMU
select HAVE_ARCH_MMAP_RND_COMPAT_BITS if MMU && COMPAT
select HAVE_ARCH_COMPAT_MMAP_BASES if MMU && COMPAT
#select HAVE_ARCH_PREL32_RELOCATIONS TODO: see the `kernel_module` macro
select HAVE_ARCH_PREL32_RELOCATIONS
select HAVE_ARCH_SECCOMP_FILTER
select HAVE_ARCH_THREAD_STRUCT_WHITELIST
select HAVE_ARCH_STACKLEAK
Expand Down
3 changes: 2 additions & 1 deletion drivers/char/rust_example/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// SPDX-License-Identifier: GPL-2.0

#![no_std]
#![feature(global_asm)]

use kernel::prelude::*;

Expand All @@ -11,7 +12,7 @@ struct RustExample {
impl KernelModule for RustExample {
fn init() -> KernelResult<Self> {
println!("Rust Example (init)");
println!("Am I built-in? {}", !cfg!(module));
println!("Am I built-in? {}", !cfg!(MODULE));
Ok(RustExample {
message: "on the heap!".to_owned(),
})
Expand Down
16 changes: 13 additions & 3 deletions rust/kernel/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,23 @@ macro_rules! kernel_module {

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

#[cfg(not(MODULE))]
#[cfg(CONFIG_HAVE_ARCH_PREL32_RELOCATIONS)]
global_asm!(
r#".section ".initcall6.init", "a"
__initcall:
.long init_module - .
.previous
"#
);

// TODO: pass the kernel module name here to generate a unique,
// helpful symbol name (the name would also useful for the `modinfo`
// issue below).
Expand Down
4 changes: 3 additions & 1 deletion scripts/Makefile.lib
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,9 @@ c_flags = -Wp,-MMD,$(depfile) $(NOSTDINC_FLAGS) $(LINUXINCLUDE) \
RUST_BINDGEN_CFLAGS = $(c_flags) $(KBUILD_CFLAGS_MODULE)
export RUST_BINDGEN_CFLAGS

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

rustc_flags = $(_rustc_flags) $(modkern_rustcflags) $(rustc_cfg_flags)

# Passed by cargo
RUSTFLAGS = $(rustc_flags)
Expand Down

0 comments on commit 0a99f23

Please sign in to comment.