Rust-for-Linux / linux

Adding support for the Rust language to the Linux kernel.
https://rust-for-linux.com
Other
3.83k stars 399 forks source link

Integrating Rust with C Kernel Modules #1070

Closed r4ve1 closed 3 months ago

r4ve1 commented 3 months ago

Hi,

I'm delving into Linux kernel module development with Rust and am curious about cross-language integration. Specifically, can existing C kernel modules (like the KVM driver) invoke Rust code? If possible, how should I do so? I've tried add an empty my_code.rs under the kvm's folder and append my_code.o to the objs to be compiled. But the compilation failed with:

image

Really appreciate for any help, thanks!

vincenzopalazzo commented 3 months ago

Hi, what the command make LLVM=1 rustavailable is telling you?

Looks like that the code that you are compiling required a different version of the rust compiler

bjorn3 commented 3 months ago

This is likely because the new_uninit feature is unknown to rustc unless an (in)direct dependency on liballoc exists. Try adding use kernel; to my_code.rs. This will pull in liballoc as indirect dependency. There was a report about this on zulip too recently.

Edit: https://rust-for-linux.zulipchat.com/#narrow/stream/288089-General/topic/What.20is.20this.3F.20-.3E.20error.5BE0635.5D.3A.20unknown.20feature.20.60new_uninit.60 was the thread about this.

r4ve1 commented 3 months ago

Thanks for the support! The integration works well, and I'm excited about the possibilities.

image

I have two quick questions:

  1. How can I enable rust-analyzer features (syntax highlighting, IntelliSense) for these custom Rust files (i.e. my file locate at arch/x86/kvm/my_code.rs?
  2. Is there an automated way to generate C bindings? After exporting functions from Rust to C and build successfully, I noticed no .h files are generated.

my_code.rs:

#![allow(unused_imports)]
#![allow(missing_docs)]

use kernel::prelude::*;

const __LOG_PREFIX: &[u8] = b"KVM: ";

#[no_mangle]
pub extern "C" fn hello_from_rust() {
    pr_info!("Hello from rust!");
}
bjorn3 commented 3 months ago

How can I enable rust-analyzer features (syntax highlighting, IntelliSense) for these custom Rust files (i.e. my file locate at arch/x86/kvm/my_code.rs?

You will have to modify https://github.com/Rust-for-Linux/linux/blob/rust-next/scripts/generate_rust_analyzer.py to detect your new crate and generate the corresponding entry in the generated rust-project.json file. After that make LLVM=1 rust-analyzer should work as expected.

https://github.com/Rust-for-Linux/linux/blob/4cece764965020c22cff7665b18a012006359095/scripts/generate_rust_analyzer.py#L116-L138 is where it generates entries for all detected rust drivers.

bjorn3 commented 3 months ago

Is there an automated way to generate C bindings? After exporting functions from Rust to C and build successfully, I noticed no .h files are generated.

Currently there is only support for generating Rust bindings from C headers using rust-bindgen. Generating C headers from Rust code is not supported. You could try using cbindgen. See also https://rust-for-linux.zulipchat.com/#narrow/stream/288089-General/topic/What.20is.20the.20status.20on.20C.20APIs.20for.20Rust.20kernel.20code.3F

r4ve1 commented 3 months ago

All your suggestions worked perfectly! I can now extend KVM with Rust—amazing!

Big thanks and I'll close the issue. Great project!