Skip to content
This repository was archived by the owner on Mar 7, 2021. It is now read-only.

Commit 589f395

Browse files
geofftalex
authored andcommitted
Add custom target, switch to cargo xbuild, add README
1 parent 45b25dc commit 589f395

File tree

6 files changed

+67
-16
lines changed

6 files changed

+67
-16
lines changed

README.md

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,34 @@
1-
# linux-kernel-module-rust
1+
# Linux kernel modules in safe Rust
2+
3+
1. Install [cargo-xbuild](https://github.com/rust-osdev/cargo-xbuild):
4+
5+
```
6+
cargo install cargo-xbuild
7+
```
8+
9+
2. cd to one of the examples
10+
11+
```
12+
cd hello-world
13+
```
14+
15+
3. Build the static object with cargo xbuild, pointing it at our custom target
16+
17+
```
18+
RUST_TARGET_PATH=~/linux-kernel-module-rust cargo xbuild --target x86_64-linux-kernel-module
19+
20+
```
21+
22+
4. Build the kernel module using the Linux kernel build system (kbuild)
23+
24+
```
25+
make
26+
```
27+
28+
5. Load and unload the module!
29+
30+
```
31+
sudo insmod helloworld.ko
32+
sudo rmmod helloworld
33+
dmesg | tail
34+
```

hello-world/Cargo.toml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,3 @@ crate-type = ["staticlib"]
88

99
[dependencies]
1010
linux-kernel-module = { path = ".." }
11-
12-
[profile.dev]
13-
# TODO: can be removed once we have a custom target.json
14-
panic = "abort"

hello-world/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
obj-m := helloworld.o
2-
helloworld-objs := target/debug/libhello_world.a ../src/printk_helper.o
2+
helloworld-objs := target/x86_64-linux-kernel-module/debug/libhello_world.a ../src/printk_helper.o
33
EXTRA_LDFLAGS += --gc-sections --entry=init_module --undefined=cleanup_module
44

55
all:

src/lib.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,5 @@ extern "C" fn panic_fmt() -> ! {
6868
loop {}
6969
}
7070

71-
// TODO: we expect this to go away once we have a target.json and we rebuild libcore.
72-
#[no_mangle]
73-
pub extern "C" fn _Unwind_Resume() -> ! {
74-
loop {}
75-
}
76-
7771
#[global_allocator]
7872
static ALLOCATOR: allocator::KernelAllocator = allocator::KernelAllocator;

static-filesystem/Cargo.toml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,3 @@ crate-type = ["staticlib"]
88

99
[dependencies]
1010
linux-kernel-module = { path = ".." }
11-
12-
[profile.dev]
13-
# TODO: can be removed once we have a custom target.json
14-
panic = "abort"

x86_64-linux-kernel-module.json

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
{
2+
"arch": "x86_64",
3+
"cpu": "x86-64",
4+
"code-model": "kernel",
5+
"data-layout": "e-m:e-i64:64-f80:128-n8:16:32:64-S128",
6+
"disable-redzone": true,
7+
"eliminate-frame-pointer": false,
8+
"env": "gnu",
9+
"features": "-mmx,-sse,-sse2,-sse3,-ssse3,-sse4.1,-sse4.2,-3dnow,-3dnowa,-avx,-avx2,+soft-float",
10+
"linker-flavor": "gcc",
11+
"linker-is-gnu": true,
12+
"llvm-target": "x86_64-unknown-linux-gnu",
13+
"max-atomic-width": 64,
14+
"no-compiler-rt": true,
15+
"os": "none",
16+
"panic-strategy": "abort",
17+
"position-independent-executables": true,
18+
"pre-link-args": {
19+
"gcc": [
20+
"-Wl,--as-needed",
21+
"-Wl,-z,noexecstack",
22+
"-m64"
23+
]
24+
},
25+
"relocation-model": "static",
26+
"relro-level": "full",
27+
"target-c-int-width": "32",
28+
"target-endian": "little",
29+
"target-family": "unix",
30+
"target-pointer-width": "64",
31+
"vendor": "unknown"
32+
}

0 commit comments

Comments
 (0)