Skip to content

Commit 58ffd76

Browse files
authored
Merge pull request #7 from Rust-for-Linux/rust-params
Parameter support
2 parents 33c80fe + 60567cf commit 58ffd76

File tree

11 files changed

+407
-133
lines changed

11 files changed

+407
-133
lines changed

Cargo.lock

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
[workspace]
55
members = [
66
"rust/shlex",
7+
"rust/module",
78
"rust/kernel",
89
"drivers/char/rust_example",
910
]

drivers/char/rust_example/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# SPDX-License-Identifier: GPL-2.0
22

33
# No need for details like `authors` here -- that goes in the modinfo
4-
# via the `kernel_module!` macro
4+
# via the `module!` macro
55
[package]
66
name = "rust_example"
77
version = "0.1.0"

drivers/char/rust_example/src/lib.rs

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,26 @@
55

66
use kernel::prelude::*;
77

8+
module!{
9+
type: RustExample,
10+
name: b"rust_example",
11+
author: b"Rust for Linux Contributors",
12+
description: b"An example kernel module written in Rust",
13+
license: b"GPL v2",
14+
params: {
15+
my_bool: bool {
16+
default: true,
17+
permissions: 0,
18+
description: b"Example of bool",
19+
},
20+
my_i32: i32 {
21+
default: 42,
22+
permissions: 0o644,
23+
description: b"Example of i32",
24+
},
25+
},
26+
}
27+
828
struct RustExample {
929
message: String,
1030
}
@@ -13,6 +33,9 @@ impl KernelModule for RustExample {
1333
fn init() -> KernelResult<Self> {
1434
println!("Rust Example (init)");
1535
println!("Am I built-in? {}", !cfg!(MODULE));
36+
println!("Parameters:");
37+
println!(" my_bool: {}", my_bool.read());
38+
println!(" my_i32: {}", my_i32.read());
1639
Ok(RustExample {
1740
message: "on the heap!".to_owned(),
1841
})
@@ -26,9 +49,3 @@ impl Drop for RustExample {
2649
}
2750
}
2851

29-
kernel_module!(
30-
RustExample,
31-
author: b"Rust for Linux Contributors",
32-
description: b"An example kernel module written in Rust",
33-
license: b"GPL v2"
34-
);

rust/kernel/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ publish = false
99

1010
[dependencies]
1111
bitflags = "1"
12+
module = { path = "../module" }
1213

1314
[build-dependencies]
1415
bindgen = "0.54"

rust/kernel/build.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ const INCLUDED_VARS: &[&str] = &[
4747
"SEEK_CUR",
4848
"SEEK_END",
4949
"O_NONBLOCK",
50+
"param_ops_bool",
51+
"param_ops_int",
5052
];
5153
const OPAQUE_TYPES: &[&str] = &[
5254
// These need to be opaque because they're both packed and aligned, which rustc

rust/kernel/src/lib.rs

Lines changed: 0 additions & 125 deletions
Original file line numberDiff line numberDiff line change
@@ -25,131 +25,6 @@ pub mod user_ptr;
2525
pub use crate::error::{Error, KernelResult};
2626
pub use crate::types::{CStr, Mode};
2727

28-
/// Declares the entrypoint for a kernel module. The first argument should be a type which
29-
/// implements the [`KernelModule`] trait. Also accepts various forms of kernel metadata.
30-
///
31-
/// Example:
32-
/// ```rust,no_run
33-
/// use kernel::prelude::*;
34-
///
35-
/// struct MyKernelModule;
36-
/// impl KernelModule for MyKernelModule {
37-
/// fn init() -> KernelResult<Self> {
38-
/// Ok(MyKernelModule)
39-
/// }
40-
/// }
41-
///
42-
/// kernel_module!(
43-
/// MyKernelModule,
44-
/// author: b"Rust for Linux Contributors",
45-
/// description: b"My very own kernel module!",
46-
/// license: b"GPL"
47-
/// );
48-
#[macro_export]
49-
macro_rules! kernel_module {
50-
($module:ty, $($name:ident : $value:expr),*) => {
51-
static mut __MOD: Option<$module> = None;
52-
53-
// Built-in modules are initialized through an initcall pointer
54-
//
55-
// TODO: should we compile a C file on the fly to avoid duplication?
56-
#[cfg(not(MODULE))]
57-
#[cfg(not(CONFIG_HAVE_ARCH_PREL32_RELOCATIONS))]
58-
#[link_section = ".initcall6.init"]
59-
#[used]
60-
pub static __initcall: extern "C" fn() -> $crate::c_types::c_int = init_module;
61-
62-
#[cfg(not(MODULE))]
63-
#[cfg(CONFIG_HAVE_ARCH_PREL32_RELOCATIONS)]
64-
global_asm!(
65-
r#".section ".initcall6.init", "a"
66-
__initcall:
67-
.long init_module - .
68-
.previous
69-
"#
70-
);
71-
72-
// TODO: pass the kernel module name here to generate a unique,
73-
// helpful symbol name (the name would also useful for the `modinfo`
74-
// issue below).
75-
#[no_mangle]
76-
pub extern "C" fn init_module() -> $crate::c_types::c_int {
77-
match <$module as $crate::KernelModule>::init() {
78-
Ok(m) => {
79-
unsafe {
80-
__MOD = Some(m);
81-
}
82-
return 0;
83-
}
84-
Err(e) => {
85-
return e.to_kernel_errno();
86-
}
87-
}
88-
}
89-
90-
#[no_mangle]
91-
pub extern "C" fn cleanup_module() {
92-
unsafe {
93-
// Invokes drop() on __MOD, which should be used for cleanup.
94-
__MOD = None;
95-
}
96-
}
97-
98-
$(
99-
$crate::kernel_module!(@attribute $name, $value);
100-
)*
101-
};
102-
103-
// TODO: The modinfo attributes below depend on the compiler placing
104-
// the variables in order in the .modinfo section, so that you end up
105-
// with b"key=value\0" in order in the section. This is a reasonably
106-
// standard trick in C, but I'm not sure that rustc guarantees it.
107-
//
108-
// Ideally we'd be able to use concat_bytes! + stringify_bytes! +
109-
// some way of turning a string literal (or at least a string
110-
// literal token) into a bytes literal, and get a single static
111-
// [u8; * N] with the whole thing, but those don't really exist yet.
112-
// Most of the alternatives (e.g. .as_bytes() as a const fn) give
113-
// you a pointer, not an array, which isn't right.
114-
115-
// TODO: `modules.builtin.modinfo` etc. is missing the prefix (module name)
116-
(@attribute author, $value:expr) => {
117-
#[link_section = ".modinfo"]
118-
#[used]
119-
pub static AUTHOR_KEY: [u8; 7] = *b"author=";
120-
#[link_section = ".modinfo"]
121-
#[used]
122-
pub static AUTHOR_VALUE: [u8; $value.len()] = *$value;
123-
#[link_section = ".modinfo"]
124-
#[used]
125-
pub static AUTHOR_NUL: [u8; 1] = *b"\0";
126-
};
127-
128-
(@attribute description, $value:expr) => {
129-
#[link_section = ".modinfo"]
130-
#[used]
131-
pub static DESCRIPTION_KEY: [u8; 12] = *b"description=";
132-
#[link_section = ".modinfo"]
133-
#[used]
134-
pub static DESCRIPTION_VALUE: [u8; $value.len()] = *$value;
135-
#[link_section = ".modinfo"]
136-
#[used]
137-
pub static DESCRIPTION_NUL: [u8; 1] = *b"\0";
138-
};
139-
140-
(@attribute license, $value:expr) => {
141-
#[link_section = ".modinfo"]
142-
#[used]
143-
pub static LICENSE_KEY: [u8; 8] = *b"license=";
144-
#[link_section = ".modinfo"]
145-
#[used]
146-
pub static LICENSE_VALUE: [u8; $value.len()] = *$value;
147-
#[link_section = ".modinfo"]
148-
#[used]
149-
pub static LICENSE_NUL: [u8; 1] = *b"\0";
150-
};
151-
}
152-
15328
/// KernelModule is the top level entrypoint to implementing a kernel module. Your kernel module
15429
/// should implement the `init` method on it, which maps to the `module_init` macro in Linux C API.
15530
/// You can use this method to do whatever setup or registration your module should do. For any

rust/kernel/src/prelude.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@ pub use alloc::{
77
borrow::ToOwned,
88
};
99

10+
pub use module::module;
11+
1012
pub use super::{
11-
kernel_module,
1213
println,
1314
KernelResult,
1415
KernelModule,

rust/module/Cargo.toml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# SPDX-License-Identifier: GPL-2.0
2+
3+
[package]
4+
name = "module"
5+
version = "0.1.0"
6+
authors = ["Rust for Linux Contributors"]
7+
edition = "2018"
8+
publish = false
9+
10+
[lib]
11+
proc-macro = true
12+

0 commit comments

Comments
 (0)