Skip to content

Commit 5295a1b

Browse files
committed
Add charp and some prim params
1 parent f823a22 commit 5295a1b

File tree

10 files changed

+274
-34
lines changed

10 files changed

+274
-34
lines changed

.github/workflows/ci.yaml

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -153,18 +153,26 @@ jobs:
153153
# Run
154154
- run: ${{ env.BUILD_DIR }}usr/gen_init_cpio .github/workflows/qemu-initramfs.desc > qemu-initramfs.img
155155

156-
- run: qemu-system-${{ env.QEMU_ARCH }} -kernel ${{ env.BUILD_DIR }}${{ env.IMAGE_PATH }} -initrd qemu-initramfs.img -M ${{ env.QEMU_MACHINE }} -cpu ${{ env.QEMU_CPU }} -smp 2 -nographic -no-reboot -append '${{ env.QEMU_APPEND }} rust_example.my_i32=123321 rust_example_2.my_i32=234432' | tee qemu-stdout.log
156+
- run: qemu-system-${{ env.QEMU_ARCH }} -kernel ${{ env.BUILD_DIR }}${{ env.IMAGE_PATH }} -initrd qemu-initramfs.img -M ${{ env.QEMU_MACHINE }} -cpu ${{ env.QEMU_CPU }} -smp 2 -nographic -no-reboot -append '${{ env.QEMU_APPEND }} rust_example.my_i32=123321 rust_example.my_str=🦀mod rust_example.my_invbool=y rust_example_2.my_i32=234432' | tee qemu-stdout.log
157157

158158
# Check
159159
- run: grep -F '] Rust Example (init)' qemu-stdout.log
160160
- run: grep -F '] [2] Rust Example (init)' qemu-stdout.log
161161
- run: grep -F '] [3] Rust Example (init)' qemu-stdout.log
162162
- run: grep -F '] [4] Rust Example (init)' qemu-stdout.log
163163

164-
- run: "grep -F '] my_i32: 123321' qemu-stdout.log"
165-
- run: "grep -F '] [2] my_i32: 234432' qemu-stdout.log"
166-
- run: "grep -F '] [3] my_i32: 345543' qemu-stdout.log"
167-
- run: "grep -F '] [4] my_i32: 456654' qemu-stdout.log"
164+
- run: "grep -F '] my_i32: 123321' qemu-stdout.log"
165+
- run: "grep -F '] [2] my_i32: 234432' qemu-stdout.log"
166+
- run: "grep -F '] [3] my_i32: 345543' qemu-stdout.log"
167+
- run: "grep -F '] [4] my_i32: 456654' qemu-stdout.log"
168+
169+
- run: "grep -F '] my_invbool: false' qemu-stdout.log"
170+
- run: "grep -F '] [2] my_invbool: true' qemu-stdout.log"
171+
172+
- run: "grep '\\] my_str: 🦀mod\\s*$' qemu-stdout.log"
173+
- run: "grep '\\] \\[2\\] my_str: default str val\\s*$' qemu-stdout.log"
174+
- run: "grep '\\] \\[3\\] my_str: 🦀mod\\s*$' qemu-stdout.log"
175+
- run: "grep '\\] \\[4\\] my_str: default str val\\s*$' qemu-stdout.log"
168176

169177
- run: grep -F '] [3] Rust Example (exit)' qemu-stdout.log
170178
- run: grep -F '] [4] Rust Example (exit)' qemu-stdout.log

.github/workflows/qemu-init.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/bin/sh
22

3-
busybox insmod rust_example_3.ko my_i32=345543
3+
busybox insmod rust_example_3.ko my_i32=345543 my_str=🦀mod
44
busybox insmod rust_example_4.ko my_i32=456654
55
busybox rmmod rust_example_3.ko
66
busybox rmmod rust_example_4.ko

drivers/char/rust_example.rs

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,16 @@ module! {
2626
permissions: 0o644,
2727
description: b"Example of i32",
2828
},
29+
my_str: str {
30+
default: b"default str val",
31+
permissions: 0o644,
32+
description: b"Example of a string param",
33+
},
34+
my_invbool: invbool {
35+
default: true,
36+
permissions: 0o644,
37+
description: b"Example of an inverted bool",
38+
},
2939
},
3040
}
3141

@@ -49,9 +59,17 @@ impl KernelModule for RustExample {
4959
fn init() -> KernelResult<Self> {
5060
println!("Rust Example (init)");
5161
println!("Am I built-in? {}", !cfg!(MODULE));
52-
println!("Parameters:");
53-
println!(" my_bool: {}", my_bool.read());
54-
println!(" my_i32: {}", my_i32.read());
62+
{
63+
let lock = THIS_MODULE.kernel_param_lock();
64+
println!("Parameters:");
65+
println!(" my_bool: {}", my_bool.read());
66+
println!(" my_i32: {}", my_i32.read(&lock));
67+
println!(
68+
" my_str: {}",
69+
core::str::from_utf8(my_str.read(&lock))?
70+
);
71+
println!(" my_invbool: {}", my_invbool.read(&lock));
72+
}
5573

5674
// Including this large variable on the stack will trigger
5775
// stack probing on the supported archs.

drivers/char/rust_example_2.rs

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,16 @@ module! {
2323
permissions: 0o644,
2424
description: b"Example of i32",
2525
},
26+
my_str: str {
27+
default: b"default str val",
28+
permissions: 0o644,
29+
description: b"Example of a string param",
30+
},
31+
my_invbool: invbool {
32+
default: true,
33+
permissions: 0o644,
34+
description: b"Example of an inverted bool",
35+
},
2636
},
2737
}
2838

@@ -34,9 +44,17 @@ impl KernelModule for RustExample2 {
3444
fn init() -> KernelResult<Self> {
3545
println!("[2] Rust Example (init)");
3646
println!("[2] Am I built-in? {}", !cfg!(MODULE));
37-
println!("[2] Parameters:");
38-
println!("[2] my_bool: {}", my_bool.read());
39-
println!("[2] my_i32: {}", my_i32.read());
47+
{
48+
let lock = THIS_MODULE.kernel_param_lock();
49+
println!("[2] Parameters:");
50+
println!("[2] my_bool: {}", my_bool.read());
51+
println!("[2] my_i32: {}", my_i32.read(&lock));
52+
println!(
53+
"[2] my_str: {}",
54+
core::str::from_utf8(my_str.read(&lock))?
55+
);
56+
println!("[2] my_invbool: {}", my_invbool.read(&lock));
57+
}
4058

4159
// Including this large variable on the stack will trigger
4260
// stack probing on the supported archs.

drivers/char/rust_example_3.rs

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,16 @@ module! {
2323
permissions: 0o644,
2424
description: b"Example of i32",
2525
},
26+
my_str: str {
27+
default: b"default str val",
28+
permissions: 0o644,
29+
description: b"Example of a string param",
30+
},
31+
my_invbool: invbool {
32+
default: true,
33+
permissions: 0o644,
34+
description: b"Example of an inverted bool",
35+
},
2636
},
2737
}
2838

@@ -34,9 +44,17 @@ impl KernelModule for RustExample3 {
3444
fn init() -> KernelResult<Self> {
3545
println!("[3] Rust Example (init)");
3646
println!("[3] Am I built-in? {}", !cfg!(MODULE));
37-
println!("[3] Parameters:");
38-
println!("[3] my_bool: {}", my_bool.read());
39-
println!("[3] my_i32: {}", my_i32.read());
47+
{
48+
let lock = THIS_MODULE.kernel_param_lock();
49+
println!("[3] Parameters:");
50+
println!("[3] my_bool: {}", my_bool.read());
51+
println!("[3] my_i32: {}", my_i32.read(&lock));
52+
println!(
53+
"[3] my_str: {}",
54+
core::str::from_utf8(my_str.read(&lock))?
55+
);
56+
println!("[3] my_invbool: {}", my_invbool.read(&lock));
57+
}
4058

4159
// Including this large variable on the stack will trigger
4260
// stack probing on the supported archs.

drivers/char/rust_example_4.rs

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,16 @@ module! {
2323
permissions: 0o644,
2424
description: b"Example of i32",
2525
},
26+
my_str: str {
27+
default: b"default str val",
28+
permissions: 0o644,
29+
description: b"Example of a string param",
30+
},
31+
my_invbool: invbool {
32+
default: true,
33+
permissions: 0o644,
34+
description: b"Example of an inverted bool",
35+
},
2636
},
2737
}
2838

@@ -34,9 +44,17 @@ impl KernelModule for RustExample4 {
3444
fn init() -> KernelResult<Self> {
3545
println!("[4] Rust Example (init)");
3646
println!("[4] Am I built-in? {}", !cfg!(MODULE));
37-
println!("[4] Parameters:");
38-
println!("[4] my_bool: {}", my_bool.read());
39-
println!("[4] my_i32: {}", my_i32.read());
47+
{
48+
let lock = THIS_MODULE.kernel_param_lock();
49+
println!("[4] Parameters:");
50+
println!("[4] my_bool: {}", my_bool.read());
51+
println!("[4] my_i32: {}", my_i32.read(&lock));
52+
println!(
53+
"[4] my_str: {}",
54+
core::str::from_utf8(my_str.read(&lock))?
55+
);
56+
println!("[4] my_invbool: {}", my_invbool.read(&lock));
57+
}
4058

4159
// Including this large variable on the stack will trigger
4260
// stack probing on the supported archs.

rust/kernel/c_types.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,9 @@ mod c {
5151
}
5252

5353
pub use c::*;
54+
55+
/// Reads string until null byte is reached and returns slice excluding the terminating null.
56+
pub unsafe fn c_string_bytes(ptr: *const crate::c_types::c_char) -> &'static [u8] {
57+
let length = crate::bindings::strlen(ptr) as usize;
58+
&core::slice::from_raw_parts(ptr as *const u8, length)
59+
}

rust/kernel/error.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// SPDX-License-Identifier: GPL-2.0
22

33
use core::num::TryFromIntError;
4+
use core::str::Utf8Error;
45

56
use alloc::alloc::AllocError;
67

@@ -31,6 +32,12 @@ impl From<TryFromIntError> for Error {
3132
}
3233
}
3334

35+
impl From<Utf8Error> for Error {
36+
fn from(_: Utf8Error) -> Error {
37+
Error::EINVAL
38+
}
39+
}
40+
3441
pub type KernelResult<T> = Result<T, Error>;
3542

3643
impl From<AllocError> for Error {

rust/kernel/lib.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,30 @@ impl ThisModule {
5454
pub const unsafe fn from_ptr(ptr: *mut bindings::module) -> ThisModule {
5555
ThisModule(ptr)
5656
}
57+
58+
pub fn kernel_param_lock(&self) -> KParamGuard<'_> {
59+
// SAFETY: `kernel_param_lock` will check if the pointer is null and use the built-in mutex
60+
// in that case.
61+
#[cfg(CONFIG_SYSFS)]
62+
unsafe { bindings::kernel_param_lock(self.0) }
63+
64+
KParamGuard { this_module: self }
65+
}
66+
}
67+
68+
/// Scoped lock on the kernel parameters of `ThisModule`. Lock will be released
69+
/// when this struct is dropped.
70+
pub struct KParamGuard<'a> {
71+
this_module: &'a ThisModule
72+
}
73+
74+
#[cfg(CONFIG_SYSFS)]
75+
impl<'a> Drop for KParamGuard<'a> {
76+
fn drop(&mut self) {
77+
// SAFETY: `kernel_param_lock` will check if the pointer is null and use the built-in mutex
78+
// in that case. The existance of `self` guarantees that the lock is held.
79+
unsafe { bindings::kernel_param_unlock(self.this_module.0) }
80+
}
5781
}
5882

5983
extern "C" {

0 commit comments

Comments
 (0)