forked from Rust-for-Linux/linux
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrust_example_4.rs
85 lines (76 loc) · 2.43 KB
/
rust_example_4.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
// SPDX-License-Identifier: GPL-2.0
#![no_std]
#![feature(allocator_api, global_asm)]
#![feature(test)]
use kernel::prelude::*;
module! {
type: RustExample4,
name: b"rust_example_4",
author: b"Rust for Linux Contributors",
description: b"An example kernel module written in Rust",
license: b"GPL v2",
params: {
my_bool: bool {
default: true,
permissions: 0,
description: b"Example of bool",
},
my_i32: i32 {
default: 42,
permissions: 0o644,
description: b"Example of i32",
},
my_str: str {
default: b"default str val",
permissions: 0o644,
description: b"Example of a string param",
},
my_usize: usize {
default: 42,
permissions: 0o644,
description: b"Example of usize",
},
my_array: ArrayParam<i32, 3> {
default: [0, 1],
permissions: 0,
description: b"Example of array",
},
},
}
struct RustExample4 {
message: String,
}
impl KernelModule for RustExample4 {
fn init() -> KernelResult<Self> {
println!("[4] Rust Example (init)");
println!("[4] Am I built-in? {}", !cfg!(MODULE));
{
let lock = THIS_MODULE.kernel_param_lock();
println!("[4] Parameters:");
println!("[4] my_bool: {}", my_bool.read());
println!("[4] my_i32: {}", my_i32.read(&lock));
println!(
"[4] my_str: {}",
core::str::from_utf8(my_str.read(&lock))?
);
println!("[4] my_usize: {}", my_usize.read(&lock));
println!("[4] my_array: {:?}", my_array.read());
}
// Including this large variable on the stack will trigger
// stack probing on the supported archs.
// This will verify that stack probing does not lead to
// any errors if we need to link `__rust_probestack`.
let x: [u64; 1028] = core::hint::black_box([5; 1028]);
println!("Large array has length: {}", x.len());
Ok(RustExample4 {
message: "on the heap!".to_owned(),
})
}
}
impl Drop for RustExample4 {
fn drop(&mut self) {
println!("[4] My message is {}", self.message);
println!("[4] Rust Example (exit)");
}
}
// XXX: Only for GitHub -- do not commit into mainline