Skip to content

Commit 9171e2a

Browse files
Gioh Kimgurugio
Gioh Kim
authored andcommitted
ch04: rust_proc WIP
/ # insmod share/rust_proc.ko [ 7.457539] rust_proc: module verification failed: signature and/or required key missing - tainting kernel [ 7.459266] rust_proc: rust_proc is loaded / # ls /proc 1 27 7 iomem pressure 10 28 8 ioports rust_proc 11 29 9 irq schedstat 111 3 bootconfig kallsyms self 12 30 buddyinfo kcore slabinfo 120 31 bus key-users softirqs 13 32 cgroups keys stat 14 33 cmdline kmsg swaps 15 34 consoles kpagecgroup sys 16 35 cpuinfo kpagecount sysrq-trigger 17 36 crypto kpageflags sysvipc 18 39 devices loadavg thread-self 19 4 diskstats locks timer_list 2 40 dma meminfo tty 20 41 driver misc uptime 21 42 dynamic_debug modules version 22 5 execdomains mounts vmallocinfo 23 53 fb mtrr vmstat 24 6 filesystems net zoneinfo 25 61 fs pagetypeinfo 26 62 interrupts partitions / # ls /proc/rust_proc/ / #
1 parent 9feb825 commit 9171e2a

File tree

4 files changed

+57
-0
lines changed

4 files changed

+57
-0
lines changed

rust/bindings/bindings_helper.h

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include <linux/uaccess.h>
2121
#include <linux/delay.h>
2222
#include <linux/miscdevice.h>
23+
#include <linux/proc_fs.h>
2324

2425
/* `bindgen` gets confused at certain things. */
2526
const size_t BINDINGS_ARCH_SLAB_MINALIGN = ARCH_SLAB_MINALIGN;

samples/rust/Kconfig

+7
Original file line numberDiff line numberDiff line change
@@ -57,4 +57,11 @@ config SAMPLE_RUST_LDD03
5757
help
5858
This option builds the Rust Linux Device Drivers examples
5959

60+
config SAMPLE_RUST_LDD_CH04
61+
tristate "Linux Device Driver proc in Rust"
62+
help
63+
This option builds the Linux Device Driver examples in Rust.
64+
65+
If unsure, say N.
66+
6067
endif # SAMPLES_RUST

samples/rust/Makefile

+1
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@ obj-$(CONFIG_SAMPLE_RUST_PRINT) += rust_print.o
55
obj-$(CONFIG_SAMPLE_RUST_FS) += rust_fs.o
66
obj-$(CONFIG_SAMPLE_RUST_LDD02) += rust_ldd02.o
77
obj-$(CONFIG_SAMPLE_RUST_LDD03) += rust_ldd03.o
8+
obj-$(CONFIG_SAMPLE_RUST_LDD04) += rust_proc.o
89

910
subdir-$(CONFIG_SAMPLE_RUST_HOSTPROGS) += hostprogs

samples/rust/rust_proc.rs

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
3+
//! Rust LDD
4+
//! reference: https://github.com/d0u9/Linux-Device-Driver/tree/master/
5+
//!
6+
//! How to build only modules:
7+
//! make LLVM=1 M=samples/rust
8+
9+
// core is from Rust compiler, not from kernel
10+
use core::ptr;
11+
12+
use kernel::bindings;
13+
use kernel::prelude::*;
14+
use kernel::{
15+
file::{self, File},
16+
io_buffer::{IoBufferReader, IoBufferWriter},
17+
str::CString,
18+
sync::{Arc, ArcBorrow, Mutex, UniqueArc},
19+
};
20+
21+
module! {
22+
type: RustProc,
23+
name: "rust_proc",
24+
author: "Rust for Linux Contributors",
25+
description: "Rust LDD ch04 proc_fs_basic",
26+
license: "GPL",
27+
}
28+
29+
struct RustProc {}
30+
31+
impl kernel::Module for RustProc {
32+
fn init(name: &'static CStr, _module: &'static ThisModule) -> Result<Self> {
33+
pr_info!("{} is loaded\n", name.to_str()?);
34+
35+
unsafe {
36+
let parent_dir = CString::try_from_fmt(fmt!("rust_proc"))?;
37+
let proc_dir = bindings::proc_mkdir(parent_dir.as_char_ptr(), ptr::null_mut());
38+
}
39+
40+
Ok(RustProc {})
41+
}
42+
}
43+
44+
impl Drop for RustProc {
45+
fn drop(&mut self) {
46+
pr_info!("rust_proc is unloaded\n");
47+
}
48+
}

0 commit comments

Comments
 (0)