How to make linux kernel modules with Rust

i followed the steps of fishinabarrel/linux-kernel-module-rust: Framework for writing Linux kernel modules in safe Rust (github.com)

but when i perform 'make', it outputs 【
/linux-kernel-module-rust-master/hello-world; cargo build -Z build-std=core,alloc --target=x86_64-linux-kernel
error: failed to run rustc to learn about target-specific information

Caused by:
process didn't exit successfully: rustc - --crate-name ___ --print=file-names --target x86_64-linux-kernel --crate-type bin --crate-type rlib --crate-type dylib --crate-type cdylib --crate-type staticlib --crate-type proc-macro --print=sysroot --print=split-debuginfo --print=crate-name --print=cfg (exit status: 1)
--- stderr
error: Error loading target specification: Could not find specification for target "x86_64-linux-kernel". Run rustc --print target-list for a list of built-in targets】

then I modified the Kbulid
< cd $(src); $(CARGO) build -Z build-std=core,alloc --target=x86_64-unknown-linux-gnu

cd $(src); $(CARGO) build -Z build-std=core,alloc --target=x86_64-linux-kernel

i output another new error messages 【

error: failed to run custom build command for linux-kernel-module v0.1.0 (/home/linjia/rcode/linux-kernel-module-rust-master)

Caused by:
process didn't exit successfully: `/home/linjia/rcode/linux-kernel-module-rust-master/hello-world/target/debug/build/linux-kernel-module-9c202ec5f917d
--- stdout
cargo:rerun-if-env-changed=CC
cargo:rerun-if-env-changed=KDIR
cargo:rerun-if-env-changed=c_flags
cargo:rerun-if-changed=src/bindings_helper.h

--- stderr
/lib/modules/5.4.0-135-generic/build/./arch/x86/include/asm/segment.h:266:2: error: expected '(' after 'asm'
/lib/modules/5.4.0-135-generic/build/./arch/x86/include/asm/page_64.h:49:2: error: expected '(' after 'asm'
/lib/modules/5.4.0-135-generic/build/./arch/x86/include/asm/special_insns.h:207:2: error: expected '(' after 'asm'
/lib/modules/5.4.0-135-generic/build/./arch/x86/include/asm/processor.h:786:2: error: expected '(' after 'asm'
/lib/modules/5.4.0-135-generic/build/./arch/x86/include/asm/processor.h:798:2: error: expected '(' after 'asm' 】

The repo you've linked is archived and hasn't seen any activity in two years. Have you followed the link in the archived repo's readme to the current work the Rust-for-Linux people are doing? If you follow their quick-start documentation, maybe you'll be able to fix your build problems and start writing kernel modules?

Note that even if you would follow the links and use the actual sources you couldn't do much just yet.

Here's the current statius: While the 6.3 kernel has gained more support for the Rust language, it still remains true that there is little that can be done in Rust beyond the creation of a "hello world" module. That functionality was already available in C, of course, with a level of safety similar to what Rust can provide. Interest is growing, though, in merging actually useful modules written in Rust; that will require some more capable infrastructure than is currently present. A recent discussion on the handling of time values in Rust demonstrates the challenges — and opportunities — inherent in this effort.

Also note that while I have seen quite a few people were excited because they hoped that Rust would make it possible for them to create a single module which would then be usable with different kernel versions currently the opposite is true: because Rust-for-Linux is now in process of [slow] merging into the kernel it means the APIs which were developed while it wasn't part of the kernel are changing even more rapidly then before!

I hope in a year or two Rust would be somewhat normal language to write kernel modules in, but right now it's anything but.

2 Likes

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.