File tree Expand file tree Collapse file tree 11 files changed +399
-14
lines changed Expand file tree Collapse file tree 11 files changed +399
-14
lines changed Original file line number Diff line number Diff line change @@ -156,7 +156,7 @@ jobs:
156
156
# Run
157
157
- run : ${{ env.BUILD_DIR }}usr/gen_init_cpio .github/workflows/qemu-initramfs.desc > qemu-initramfs.img
158
158
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
160
160
161
161
# Check
162
162
- run : grep -F '] Rust Example (init)' qemu-stdout.log
@@ -169,6 +169,11 @@ jobs:
169
169
- run : " grep -F '] [3] my_i32: 345543' qemu-stdout.log"
170
170
- run : " grep -F '] [4] my_i32: 456654' qemu-stdout.log"
171
171
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
+
172
177
- run : " grep '\\ ] my_str: 🦀mod\\ s*$' qemu-stdout.log"
173
178
- run : " grep '\\ ] \\ [2\\ ] my_str: default str val\\ s*$' qemu-stdout.log"
174
179
- run : " grep '\\ ] \\ [3\\ ] my_str: 🦀mod\\ s*$' qemu-stdout.log"
Original file line number Diff line number Diff line change 1
1
#! /bin/sh
2
2
3
3
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
5
5
busybox rmmod rust_example_3.ko
6
6
busybox rmmod rust_example_4.ko
7
7
Original file line number Diff line number Diff line change @@ -33,6 +33,11 @@ module! {
33
33
permissions: 0o644 ,
34
34
description: b"Example of a string param" ,
35
35
} ,
36
+ my_usize: usize {
37
+ default : 42 ,
38
+ permissions: 0o644 ,
39
+ description: b"Example of usize" ,
40
+ } ,
36
41
} ,
37
42
}
38
43
@@ -66,6 +71,7 @@ impl KernelModule for RustExample {
66
71
" my_str: {}" ,
67
72
core:: str :: from_utf8( my_str. read( & lock) ) ?
68
73
) ;
74
+ println ! ( " my_usize: {}" , my_usize. read( & lock) ) ;
69
75
}
70
76
71
77
// Including this large variable on the stack will trigger
Original file line number Diff line number Diff line change @@ -28,6 +28,11 @@ module! {
28
28
permissions: 0o644 ,
29
29
description: b"Example of a string param" ,
30
30
} ,
31
+ my_usize: usize {
32
+ default : 42 ,
33
+ permissions: 0o644 ,
34
+ description: b"Example of usize" ,
35
+ } ,
31
36
} ,
32
37
}
33
38
@@ -48,6 +53,7 @@ impl KernelModule for RustExample2 {
48
53
"[2] my_str: {}" ,
49
54
core:: str :: from_utf8( my_str. read( & lock) ) ?
50
55
) ;
56
+ println ! ( "[2] my_usize: {}" , my_usize. read( & lock) ) ;
51
57
}
52
58
53
59
// Including this large variable on the stack will trigger
Original file line number Diff line number Diff line change @@ -28,6 +28,11 @@ module! {
28
28
permissions: 0o644 ,
29
29
description: b"Example of a string param" ,
30
30
} ,
31
+ my_usize: usize {
32
+ default : 42 ,
33
+ permissions: 0o644 ,
34
+ description: b"Example of usize" ,
35
+ } ,
31
36
} ,
32
37
}
33
38
@@ -48,6 +53,7 @@ impl KernelModule for RustExample3 {
48
53
"[3] my_str: {}" ,
49
54
core:: str :: from_utf8( my_str. read( & lock) ) ?
50
55
) ;
56
+ println ! ( "[3] my_usize: {}" , my_usize. read( & lock) ) ;
51
57
}
52
58
53
59
// Including this large variable on the stack will trigger
Original file line number Diff line number Diff line change @@ -28,6 +28,11 @@ module! {
28
28
permissions: 0o644 ,
29
29
description: b"Example of a string param" ,
30
30
} ,
31
+ my_usize: usize {
32
+ default : 42 ,
33
+ permissions: 0o644 ,
34
+ description: b"Example of usize" ,
35
+ } ,
31
36
} ,
32
37
}
33
38
@@ -48,6 +53,7 @@ impl KernelModule for RustExample4 {
48
53
"[4] my_str: {}" ,
49
54
core:: str :: from_utf8( my_str. read( & lock) ) ?
50
55
) ;
56
+ println ! ( "[4] my_usize: {}" , my_usize. read( & lock) ) ;
51
57
}
52
58
53
59
// Including this large variable on the stack will trigger
Original file line number Diff line number Diff line change @@ -72,7 +72,7 @@ $(objtree)/rust/bindings_generated.rs: $(srctree)/rust/kernel/bindings_helper.h
72
72
quiet_cmd_exports = EXPORTS $@
73
73
cmd_exports = \
74
74
$(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)' \
76
76
| xargs -n1 -Isymbol \
77
77
echo 'EXPORT_SYMBOL$(exports_target_type ) (symbol);' > $@
78
78
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change @@ -30,11 +30,13 @@ mod allocator;
30
30
#[ doc( hidden) ]
31
31
pub mod bindings;
32
32
33
+ pub mod buffer;
33
34
pub mod c_types;
34
35
pub mod chrdev;
35
36
mod error;
36
37
pub mod file_operations;
37
38
pub mod miscdev;
39
+ pub mod module_param;
38
40
pub mod prelude;
39
41
pub mod printk;
40
42
pub mod random;
@@ -48,6 +50,11 @@ pub mod user_ptr;
48
50
pub use crate :: error:: { Error , KernelResult } ;
49
51
pub use crate :: types:: { CStr , Mode } ;
50
52
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
+
51
58
/// The top level entrypoint to implementing a kernel module.
52
59
///
53
60
/// For any teardown or cleanup operations, your type may implement [`Drop`].
You can’t perform that action at this time.
0 commit comments