Skip to content

Commit 49d0084

Browse files
Gioh Kimgurugio
Gioh Kim
authored andcommitted
ch04: create a proc file with empty handler
/ # ls /proc/rust_demo/ rust_proc_fs / # cat /proc/rust_demo/rust_proc_fs [ 43.097837] BUG: kernel NULL pointer dereference, address: 0000000000000002 [ 43.098565] #PF: supervisor instruction fetch in kernel mode [ 43.099176] #PF: error_code(0x0010) - not-present page [ 43.099832] PGD 5490067 P4D 5490067 PUD 572a067 PMD 0 [ 43.100501] Oops: 0010 [#1] PREEMPT SMP NOPTI [ 43.101051] CPU: 0 PID: 122 Comm: cat Tainted: G E 6.3.0+ Rust-for-Linux#17 [ 43.101745] Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.15.0-1 04/01/2014 [ 43.101745] RIP: 0010:0x2 [ 43.101745] Code: Unable to access opcode bytes at 0xffffffffffffffd8. [ 43.101745] RSP: 0018:ffff88800573fc68 EFLAGS: 00010202 [ 43.101745] RAX: ffff8880057339c0 RBX: 0000000000000001 RCX: 0000000000000000 [ 43.101745] RDX: 0000000000000000 RSI: ffff888005729700 RDI: ffff888004f5e308 [ 43.101745] RBP: ffff88800573fca0 R08: ffff88800573fc28 R09: ffff8880056b5ac8 [ 43.101745] R10: 0000000031a4a4f0 R11: 0000000000000002 R12: ffff8880056b5ac8 [ 43.101745] R13: ffff888005729700 R14: ffff888005469480 R15: ffff888004f5e308 [ 43.101745] FS: 00000000020ee3c0(0000) GS:ffff888007a00000(0000) knlGS:0000000000000000 [ 43.101745] CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033 [ 43.101745] CR2: ffffffffffffffd8 CR3: 000000000561e000 CR4: 00000000000006f0 [ 43.101745] Call Trace: [ 43.101745] <TASK> [ 43.101745] ? proc_reg_open+0xf1/0x1d0 [ 43.101745] ? proc_reg_mmap+0x110/0x110 [ 43.101745] do_dentry_open+0x166/0x450 [ 43.101745] vfs_open+0x2d/0x30 [ 43.101745] path_openat+0xa8b/0xc50 [ 43.101745] do_filp_open+0xa1/0x130 [ 43.101745] ? getname_flags+0x50/0x1e0 [ 43.101745] ? alloc_fd+0x146/0x190 [ 43.101745] do_sys_openat2+0x6d/0x130 [ 43.101745] __x64_sys_openat+0x71/0x80 [ 43.101745] do_syscall_64+0x35/0x50 [ 43.101745] entry_SYSCALL_64_after_hwframe+0x63/0xcd [ 43.101745] RIP: 0033:0x4ad15b [ 43.101745] Code: 25 00 00 41 00 3d 00 00 41 00 74 4b 64 8b 04 25 18 00 00 00 85 c0 75 67 44 89 e2 48 89 ee bf 9c ff ff ff b5 [ 43.101745] RSP: 002b:00007fffb2dcd4e0 EFLAGS: 00000246 ORIG_RAX: 0000000000000101 [ 43.101745] RAX: ffffffffffffffda RBX: 00007fffb2dcd830 RCX: 00000000004ad15b [ 43.101745] RDX: 0000000000000000 RSI: 00007fffb2dcefb2 RDI: 00000000ffffff9c [ 43.101745] RBP: 00007fffb2dcefb2 R08: 0000000000000001 R09: 0000000000000000 [ 43.101745] R10: 0000000000000000 R11: 0000000000000246 R12: 0000000000000000 [ 43.101745] R13: 0000000000000000 R14: 0000000000000000 R15: 0000000000000001 [ 43.101745] </TASK> [ 43.101745] Modules linked in: rust_proc(E) [ 43.101745] CR2: 0000000000000002 [ 43.101745] ---[ end trace 0000000000000000 ]--- [ 43.101745] RIP: 0010:0x2 [ 43.101745] Code: Unable to access opcode bytes at 0xffffffffffffffd8. [ 43.101745] RSP: 0018:ffff88800573fc68 EFLAGS: 00010202 [ 43.101745] RAX: ffff8880057339c0 RBX: 0000000000000001 RCX: 0000000000000000 [ 43.101745] RDX: 0000000000000000 RSI: ffff888005729700 RDI: ffff888004f5e308 [ 43.101745] RBP: ffff88800573fca0 R08: ffff88800573fc28 R09: ffff8880056b5ac8 [ 43.101745] R10: 0000000031a4a4f0 R11: 0000000000000002 R12: ffff8880056b5ac8 [ 43.101745] R13: ffff888005729700 R14: ffff888005469480 R15: ffff888004f5e308 [ 43.101745] FS: 00000000020ee3c0(0000) GS:ffff888007a00000(0000) knlGS:0000000000000000 [ 43.101745] CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033 [ 43.101745] CR2: ffffffffffffffd8 CR3: 000000000561e000 CR4: 00000000000006f0 [ 43.101745] note: cat[122] exited with irqs disabled Killed
1 parent 9171e2a commit 49d0084

File tree

1 file changed

+46
-8
lines changed

1 file changed

+46
-8
lines changed

samples/rust/rust_proc.rs

+46-8
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,11 @@ use core::ptr;
1111

1212
use kernel::bindings;
1313
use kernel::prelude::*;
14-
use kernel::{
15-
file::{self, File},
16-
io_buffer::{IoBufferReader, IoBufferWriter},
17-
str::CString,
18-
sync::{Arc, ArcBorrow, Mutex, UniqueArc},
19-
};
14+
use kernel::str::CString;
15+
16+
static SUB_DIR_NAME: &'static str = "rust_demo";
17+
static PROC_FS_NAME: &'static str = "rust_proc_fs";
18+
static _PROC_FS_NAME_MUL: &'static str = "rust_proc_fs_mul";
2019

2120
module! {
2221
type: RustProc,
@@ -28,13 +27,52 @@ module! {
2827

2928
struct RustProc {}
3029

30+
impl RustProc {
31+
unsafe extern "C" fn proc_open(
32+
_inode: *mut bindings::inode,
33+
_file: *mut bindings::file,
34+
) -> i32 {
35+
0 as i32
36+
}
37+
38+
unsafe extern "C" fn proc_read(
39+
_file: *mut bindings::file,
40+
_buf: *mut core::ffi::c_char,
41+
_len: usize,
42+
_off: *mut bindings::loff_t,
43+
) -> isize {
44+
0 as isize
45+
}
46+
}
47+
3148
impl kernel::Module for RustProc {
3249
fn init(name: &'static CStr, _module: &'static ThisModule) -> Result<Self> {
3350
pr_info!("{} is loaded\n", name.to_str()?);
3451

3552
unsafe {
36-
let parent_dir = CString::try_from_fmt(fmt!("rust_proc"))?;
37-
let proc_dir = bindings::proc_mkdir(parent_dir.as_char_ptr(), ptr::null_mut());
53+
let dir_name = CString::try_from_fmt(fmt!("{}", SUB_DIR_NAME))?;
54+
let parent = bindings::proc_mkdir(dir_name.as_char_ptr(), ptr::null_mut());
55+
56+
let proc_ops = bindings::proc_ops {
57+
proc_flags: 0, // mandatory to prevent build error
58+
proc_get_unmapped_area: None, // mandatory to prevent build error
59+
proc_read_iter: None, // mandatory to prevent build error
60+
proc_open: Some(Self::proc_open),
61+
proc_read: Some(Self::proc_read),
62+
proc_write: None,
63+
proc_lseek: None,
64+
proc_release: None,
65+
proc_poll: None,
66+
proc_ioctl: None,
67+
proc_mmap: None,
68+
};
69+
70+
bindings::proc_create(
71+
CString::try_from_fmt(fmt!("{}", PROC_FS_NAME))?.as_char_ptr(),
72+
0o644,
73+
parent,
74+
&proc_ops,
75+
);
3876
}
3977

4078
Ok(RustProc {})

0 commit comments

Comments
 (0)