Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rust: Add rust_buffer module example #1

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions samples/rust/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,16 @@ config SAMPLE_RUST_MISCDEV

If unsure, say N.

config SAMPLE_RUST_BUFFER
tristate "File buffer"
help
This option builds the Rust file buffer sample.

To compile this as a module, choose M here:
the module will be called rust_buffer.

If unsure, say N.

config SAMPLE_RUST_STACK_PROBING
tristate "Stack probing"
help
Expand Down
1 change: 1 addition & 0 deletions samples/rust/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ obj-$(CONFIG_SAMPLE_RUST_MODULE_PARAMETERS) += rust_module_parameters.o
obj-$(CONFIG_SAMPLE_RUST_SYNC) += rust_sync.o
obj-$(CONFIG_SAMPLE_RUST_CHRDEV) += rust_chrdev.o
obj-$(CONFIG_SAMPLE_RUST_MISCDEV) += rust_miscdev.o
obj-$(CONFIG_SAMPLE_RUST_BUFFER) += rust_buffer.o
obj-$(CONFIG_SAMPLE_RUST_STACK_PROBING) += rust_stack_probing.o
obj-$(CONFIG_SAMPLE_RUST_SEMAPHORE) += rust_semaphore.o
obj-$(CONFIG_SAMPLE_RUST_SEMAPHORE_C) += rust_semaphore_c.o
Expand Down
74 changes: 74 additions & 0 deletions samples/rust/rust_buffer.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// SPDX-License-Identifier: GPL-2.0

//! Rust memory-backed file.
//!
//! Allocate a buffer of shared kernel memory and expose this buffer to user-space using a misc
//! file device interface.

use kernel::{
file::{self, File},
io_buffer::{IoBufferReader, IoBufferWriter},
prelude::*,
};

module_misc_device! {
type: RustBuffer,
name: "rust_buffer",
author: "Andrea Righi <[email protected]>",
description: "Memory-backed file implemented in Rust",
license: "GPL v2",
}

// Size of the shared memory buffer (4K by default)
const BUFSIZE : usize = 4096usize;

// Shared memory buffer
static mut BUFFER: [u8; BUFSIZE] = [0u8; BUFSIZE];

struct RustBuffer {
}

#[vtable]
impl file::Operations for RustBuffer {
type Data = Box<Self>;

fn open(_context: &Self::OpenData, _file: &File) -> Result<Self::Data> {
Ok(Box::try_new(Self { })?)
}

fn read(_this: &Self, _: &File, buf: &mut impl IoBufferWriter, offset: u64) -> Result<usize> {
let mut total_len = 0;
let off : usize = offset.try_into().unwrap();

while !buf.is_empty() {
let start : usize = off + total_len;
let len = buf.len().min(BUFSIZE - start);
if len <= 0 {
break;
}
unsafe {
buf.write_slice(&BUFFER[start .. start + len])?;
}
total_len += len;
}
Ok(total_len)
}

fn write(_this: &Self, _: &File, buf: &mut impl IoBufferReader, offset: u64) -> Result<usize> {
let mut total_len = 0;
let off : usize = offset.try_into().unwrap();

while !buf.is_empty() {
let start : usize = off + total_len;
let len = buf.len().min(BUFSIZE - start);
if len <= 0 {
break;
}
unsafe {
buf.read_slice(&mut BUFFER[start .. start + len])?;
}
total_len += len;
}
Ok(total_len)
}
}