Skip to content

Commit

Permalink
ch04: create a proc file with empty handler
Browse files Browse the repository at this point in the history
/ # 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
  • Loading branch information
Gioh Kim authored and gurugio committed Oct 14, 2023
1 parent 9171e2a commit 49d0084
Showing 1 changed file with 46 additions and 8 deletions.
54 changes: 46 additions & 8 deletions samples/rust/rust_proc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,11 @@ use core::ptr;

use kernel::bindings;
use kernel::prelude::*;
use kernel::{
file::{self, File},
io_buffer::{IoBufferReader, IoBufferWriter},
str::CString,
sync::{Arc, ArcBorrow, Mutex, UniqueArc},
};
use kernel::str::CString;

static SUB_DIR_NAME: &'static str = "rust_demo";
static PROC_FS_NAME: &'static str = "rust_proc_fs";
static _PROC_FS_NAME_MUL: &'static str = "rust_proc_fs_mul";

module! {
type: RustProc,
Expand All @@ -28,13 +27,52 @@ module! {

struct RustProc {}

impl RustProc {
unsafe extern "C" fn proc_open(
_inode: *mut bindings::inode,
_file: *mut bindings::file,
) -> i32 {
0 as i32
}

unsafe extern "C" fn proc_read(
_file: *mut bindings::file,
_buf: *mut core::ffi::c_char,
_len: usize,
_off: *mut bindings::loff_t,
) -> isize {
0 as isize
}
}

impl kernel::Module for RustProc {
fn init(name: &'static CStr, _module: &'static ThisModule) -> Result<Self> {
pr_info!("{} is loaded\n", name.to_str()?);

unsafe {
let parent_dir = CString::try_from_fmt(fmt!("rust_proc"))?;
let proc_dir = bindings::proc_mkdir(parent_dir.as_char_ptr(), ptr::null_mut());
let dir_name = CString::try_from_fmt(fmt!("{}", SUB_DIR_NAME))?;
let parent = bindings::proc_mkdir(dir_name.as_char_ptr(), ptr::null_mut());

let proc_ops = bindings::proc_ops {
proc_flags: 0, // mandatory to prevent build error
proc_get_unmapped_area: None, // mandatory to prevent build error
proc_read_iter: None, // mandatory to prevent build error
proc_open: Some(Self::proc_open),
proc_read: Some(Self::proc_read),
proc_write: None,
proc_lseek: None,
proc_release: None,
proc_poll: None,
proc_ioctl: None,
proc_mmap: None,
};

bindings::proc_create(
CString::try_from_fmt(fmt!("{}", PROC_FS_NAME))?.as_char_ptr(),
0o644,
parent,
&proc_ops,
);
}

Ok(RustProc {})
Expand Down

0 comments on commit 49d0084

Please sign in to comment.