Skip to content

Commit 304ee69

Browse files
authored
Merge pull request #87 from adamrk/rust-int-params
Add Rust int param types
2 parents 0440a1c + 3ec22aa commit 304ee69

File tree

11 files changed

+399
-14
lines changed

11 files changed

+399
-14
lines changed

.github/workflows/ci.yaml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ jobs:
156156
# Run
157157
- run: ${{ env.BUILD_DIR }}usr/gen_init_cpio .github/workflows/qemu-initramfs.desc > qemu-initramfs.img
158158

159-
- 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
159+
- 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_2.my_i32=234432' | tee qemu-stdout.log
160160

161161
# Check
162162
- run: grep -F '] Rust Example (init)' qemu-stdout.log
@@ -169,6 +169,11 @@ jobs:
169169
- run: "grep -F '] [3] my_i32: 345543' qemu-stdout.log"
170170
- run: "grep -F '] [4] my_i32: 456654' qemu-stdout.log"
171171

172+
- run: "grep -F '] my_usize: 42' qemu-stdout.log"
173+
- run: "grep -F '] [2] my_usize: 42' qemu-stdout.log"
174+
- run: "grep -F '] [3] my_usize: 42' qemu-stdout.log"
175+
- run: "grep -F '] [4] my_usize: 84' qemu-stdout.log"
176+
172177
- run: "grep '\\] my_str: 🦀mod\\s*$' qemu-stdout.log"
173178
- run: "grep '\\] \\[2\\] my_str: default str val\\s*$' qemu-stdout.log"
174179
- run: "grep '\\] \\[3\\] my_str: 🦀mod\\s*$' qemu-stdout.log"

.github/workflows/qemu-init.sh

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

33
busybox insmod rust_example_3.ko my_i32=345543 my_str=🦀mod
4-
busybox insmod rust_example_4.ko my_i32=456654
4+
busybox insmod rust_example_4.ko my_i32=456654 my_usize=84
55
busybox rmmod rust_example_3.ko
66
busybox rmmod rust_example_4.ko
77

drivers/char/rust_example.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ module! {
3333
permissions: 0o644,
3434
description: b"Example of a string param",
3535
},
36+
my_usize: usize {
37+
default: 42,
38+
permissions: 0o644,
39+
description: b"Example of usize",
40+
},
3641
},
3742
}
3843

@@ -66,6 +71,7 @@ impl KernelModule for RustExample {
6671
" my_str: {}",
6772
core::str::from_utf8(my_str.read(&lock))?
6873
);
74+
println!(" my_usize: {}", my_usize.read(&lock));
6975
}
7076

7177
// Including this large variable on the stack will trigger

drivers/char/rust_example_2.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ module! {
2828
permissions: 0o644,
2929
description: b"Example of a string param",
3030
},
31+
my_usize: usize {
32+
default: 42,
33+
permissions: 0o644,
34+
description: b"Example of usize",
35+
},
3136
},
3237
}
3338

@@ -48,6 +53,7 @@ impl KernelModule for RustExample2 {
4853
"[2] my_str: {}",
4954
core::str::from_utf8(my_str.read(&lock))?
5055
);
56+
println!("[2] my_usize: {}", my_usize.read(&lock));
5157
}
5258

5359
// Including this large variable on the stack will trigger

drivers/char/rust_example_3.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ module! {
2828
permissions: 0o644,
2929
description: b"Example of a string param",
3030
},
31+
my_usize: usize {
32+
default: 42,
33+
permissions: 0o644,
34+
description: b"Example of usize",
35+
},
3136
},
3237
}
3338

@@ -48,6 +53,7 @@ impl KernelModule for RustExample3 {
4853
"[3] my_str: {}",
4954
core::str::from_utf8(my_str.read(&lock))?
5055
);
56+
println!("[3] my_usize: {}", my_usize.read(&lock));
5157
}
5258

5359
// Including this large variable on the stack will trigger

drivers/char/rust_example_4.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ module! {
2828
permissions: 0o644,
2929
description: b"Example of a string param",
3030
},
31+
my_usize: usize {
32+
default: 42,
33+
permissions: 0o644,
34+
description: b"Example of usize",
35+
},
3136
},
3237
}
3338

@@ -48,6 +53,7 @@ impl KernelModule for RustExample4 {
4853
"[4] my_str: {}",
4954
core::str::from_utf8(my_str.read(&lock))?
5055
);
56+
println!("[4] my_usize: {}", my_usize.read(&lock));
5157
}
5258

5359
// Including this large variable on the stack will trigger

rust/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ $(objtree)/rust/bindings_generated.rs: $(srctree)/rust/kernel/bindings_helper.h
7272
quiet_cmd_exports = EXPORTS $@
7373
cmd_exports = \
7474
$(NM) -p --defined-only $< \
75-
| grep -F ' T ' | cut -d ' ' -f 3 | grep -E '^(__rust_|_R)' \
75+
| grep -E ' (T|R) ' | cut -d ' ' -f 3 | grep -E '^(__rust_|_R)' \
7676
| xargs -n1 -Isymbol \
7777
echo 'EXPORT_SYMBOL$(exports_target_type)(symbol);' > $@
7878

rust/kernel/buffer.rs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
3+
//! Struct for writing to a pre-allocated buffer with the [`write!`] macro.
4+
5+
use core::fmt;
6+
7+
/// A pre-allocated buffer that implements [`core::fmt::Write`].
8+
///
9+
/// Consequtive writes will append to what has already been written.
10+
/// Writes that don't fit in the buffer will fail.
11+
pub struct Buffer<'a> {
12+
slice: &'a mut [u8],
13+
pos: usize,
14+
}
15+
16+
impl<'a> Buffer<'a> {
17+
/// Create a new buffer from an existing array.
18+
pub fn new(slice: &'a mut [u8]) -> Self {
19+
Buffer { slice, pos: 0 }
20+
}
21+
22+
/// Number of bytes that have already been written to the buffer.
23+
/// This will always be less than the length of the original array.
24+
pub fn bytes_written(&self) -> usize {
25+
self.pos
26+
}
27+
}
28+
29+
impl<'a> fmt::Write for Buffer<'a> {
30+
fn write_str(&mut self, s: &str) -> fmt::Result {
31+
if s.len() > self.slice.len() - self.pos {
32+
Err(fmt::Error)
33+
} else {
34+
self.slice[self.pos..self.pos + s.len()].copy_from_slice(s.as_bytes());
35+
self.pos += s.len();
36+
Ok(())
37+
}
38+
}
39+
}

rust/kernel/lib.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,13 @@ mod allocator;
3030
#[doc(hidden)]
3131
pub mod bindings;
3232

33+
pub mod buffer;
3334
pub mod c_types;
3435
pub mod chrdev;
3536
mod error;
3637
pub mod file_operations;
3738
pub mod miscdev;
39+
pub mod module_param;
3840
pub mod prelude;
3941
pub mod printk;
4042
pub mod random;
@@ -48,6 +50,11 @@ pub mod user_ptr;
4850
pub use crate::error::{Error, KernelResult};
4951
pub use crate::types::{CStr, Mode};
5052

53+
/// Page size defined in terms of the `PAGE_SHIFT` macro from C.
54+
///
55+
/// [`PAGE_SHIFT`]: ../../../include/asm-generic/page.h
56+
pub const PAGE_SIZE: usize = 1 << bindings::PAGE_SHIFT;
57+
5158
/// The top level entrypoint to implementing a kernel module.
5259
///
5360
/// For any teardown or cleanup operations, your type may implement [`Drop`].

0 commit comments

Comments
 (0)