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: CStr overhaul #273

Merged
merged 3 commits into from
May 26, 2021
Merged
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
4 changes: 2 additions & 2 deletions drivers/android/rust_binder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
use alloc::{boxed::Box, sync::Arc};
use core::pin::Pin;
use kernel::{
cstr,
c_str,
io_buffer::IoBufferWriter,
linked_list::{GetLinks, GetLinksWrapped, Links},
miscdev::Registration,
Expand Down Expand Up @@ -111,7 +111,7 @@ impl KernelModule for BinderModule {
let pinned_ctx = Context::new()?;
let ctx = unsafe { Pin::into_inner_unchecked(pinned_ctx) };
let reg = Registration::<Arc<Context>>::new_pinned::<process::Process>(
cstr!("rust_binder"),
c_str!("rust_binder"),
None,
ctx,
)?;
Expand Down
4 changes: 2 additions & 2 deletions drivers/char/hw_random/bcm2835_rng_rust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use alloc::boxed::Box;
use core::pin::Pin;
use kernel::of::OfMatchTable;
use kernel::prelude::*;
use kernel::{cstr, platdev};
use kernel::{c_str, platdev};

module! {
type: RngModule,
Expand All @@ -25,7 +25,7 @@ struct RngModule {

impl KernelModule for RngModule {
fn init() -> Result<Self> {
let of_match_tbl = OfMatchTable::new(&cstr!("brcm,bcm2835-rng"))?;
let of_match_tbl = OfMatchTable::new(&c_str!("brcm,bcm2835-rng"))?;

let pdev = platdev::Registration::new_pinned(
cstr!("bcm2835-rng-rust"),
Expand Down
15 changes: 0 additions & 15 deletions rust/kernel/c_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,18 +117,3 @@ mod c {
}

pub use c::*;

/// Reads string until null byte is reached and returns slice excluding the
/// terminating null.
///
/// # Safety
///
/// The data from the pointer until the null terminator must be valid for reads
/// and not mutated for all of `'a`. The length of the string must also be less
/// than `isize::MAX`. See the documentation on
/// [`core::slice::from_raw_parts()`] for further details on safety of
/// converting a pointer to a slice.
pub unsafe fn c_string_bytes<'a>(ptr: *const crate::c_types::c_char) -> &'a [u8] {
let length = crate::bindings::strlen(ptr) as usize;
&core::slice::from_raw_parts(ptr as *const u8, length)
}
10 changes: 5 additions & 5 deletions rust/kernel/chrdev.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use crate::bindings;
use crate::c_types;
use crate::error::{Error, Result};
use crate::file_operations;
use crate::types::CStr;
use crate::str::CStr;

/// Character device.
///
Expand Down Expand Up @@ -87,7 +87,7 @@ struct RegistrationInner<const N: usize> {
///
/// May contain up to a fixed number (`N`) of devices. Must be pinned.
pub struct Registration<const N: usize> {
name: CStr<'static>,
name: &'static CStr,
minors_start: u16,
this_module: &'static crate::ThisModule,
inner: Option<RegistrationInner<N>>,
Expand All @@ -104,7 +104,7 @@ impl<const N: usize> Registration<{ N }> {
/// are going to pin the registration right away, call
/// [`Self::new_pinned()`] instead.
pub fn new(
name: CStr<'static>,
name: &'static CStr,
minors_start: u16,
this_module: &'static crate::ThisModule,
) -> Self {
Expand All @@ -120,7 +120,7 @@ impl<const N: usize> Registration<{ N }> {
///
/// This does *not* register the device: see [`Self::register()`].
pub fn new_pinned(
name: CStr<'static>,
name: &'static CStr,
minors_start: u16,
this_module: &'static crate::ThisModule,
) -> Result<Pin<Box<Self>>> {
Expand All @@ -146,7 +146,7 @@ impl<const N: usize> Registration<{ N }> {
&mut dev,
this.minors_start.into(),
N.try_into()?,
this.name.as_ptr() as *const c_types::c_char,
this.name.as_char_ptr(),
)
};
if res != 0 {
Expand Down
5 changes: 4 additions & 1 deletion rust/kernel/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
const_fn,
const_mut_refs,
const_panic,
const_raw_ptr_deref,
const_unreachable_unchecked,
try_reserve
)]
#![deny(clippy::complexity)]
Expand All @@ -44,6 +46,7 @@ pub mod file;
pub mod file_operations;
pub mod miscdev;
pub mod pages;
pub mod str;

pub mod linked_list;
mod raw_list;
Expand Down Expand Up @@ -72,7 +75,7 @@ pub mod user_ptr;
pub use build_error::build_error;

pub use crate::error::{Error, Result};
pub use crate::types::{CStr, Mode};
pub use crate::types::Mode;

/// Page size defined in terms of the `PAGE_SHIFT` macro from C.
///
Expand Down
9 changes: 5 additions & 4 deletions rust/kernel/miscdev.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@
//!
//! Reference: <https://www.kernel.org/doc/html/latest/driver-api/misc_devices.html>

use crate::bindings;
use crate::error::{Error, Result};
use crate::file_operations::{FileOpenAdapter, FileOpener, FileOperationsVtable};
use crate::{bindings, c_types, CStr};
use crate::str::CStr;
use alloc::boxed::Box;
use core::marker::PhantomPinned;
use core::pin::Pin;
Expand Down Expand Up @@ -41,7 +42,7 @@ impl<T: Sync> Registration<T> {
///
/// Returns a pinned heap-allocated representation of the registration.
pub fn new_pinned<F: FileOpener<T>>(
name: CStr<'static>,
name: &'static CStr,
minor: Option<i32>,
context: T,
) -> Result<Pin<Box<Self>>> {
Expand All @@ -56,7 +57,7 @@ impl<T: Sync> Registration<T> {
/// self-referential. If a minor is not given, the kernel allocates a new one if possible.
pub fn register<F: FileOpener<T>>(
self: Pin<&mut Self>,
name: CStr<'static>,
name: &'static CStr,
minor: Option<i32>,
) -> Result {
// SAFETY: We must ensure that we never move out of `this`.
Expand All @@ -68,7 +69,7 @@ impl<T: Sync> Registration<T> {

// SAFETY: The adapter is compatible with `misc_register`.
this.mdev.fops = unsafe { FileOperationsVtable::<Self, F>::build() };
this.mdev.name = name.as_ptr() as *const c_types::c_char;
this.mdev.name = name.as_char_ptr();
this.mdev.minor = minor.unwrap_or(bindings::MISC_DYNAMIC_MINOR as i32);

let ret = unsafe { bindings::misc_register(&mut this.mdev) };
Expand Down
3 changes: 2 additions & 1 deletion rust/kernel/module_param.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
//!
//! C header: [`include/linux/moduleparam.h`](../../../include/linux/moduleparam.h)

use crate::str::CStr;
use core::fmt::Write;

/// Types that can be used for module parameters.
Expand Down Expand Up @@ -70,7 +71,7 @@ pub trait ModuleParam: core::fmt::Display + core::marker::Sized {
let arg = if val.is_null() {
None
} else {
Some(crate::c_types::c_string_bytes(val))
Some(CStr::from_char_ptr(val).as_bytes())
};
match Self::try_from_param_arg(arg) {
Some(new_value) => {
Expand Down
6 changes: 3 additions & 3 deletions rust/kernel/of.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ use alloc::boxed::Box;
use crate::{
bindings, c_types,
error::{Error, Result},
str::CStr,
types::PointerWrapper,
CStr,
};

use core::mem::transmute;
Expand All @@ -32,7 +32,7 @@ pub struct OfMatchTable(InnerTable);

impl OfMatchTable {
/// Creates a [`OfMatchTable`] from a single `compatible` string.
pub fn new(compatible: &CStr<'static>) -> Result<Self> {
pub fn new(compatible: &'static CStr) -> Result<Self> {
let tbl = Box::try_new([
Self::new_of_device_id(compatible)?,
bindings::of_device_id::default(),
Expand All @@ -43,7 +43,7 @@ impl OfMatchTable {
Ok(Self(tbl))
}

fn new_of_device_id(compatible: &CStr<'static>) -> Result<bindings::of_device_id> {
fn new_of_device_id(compatible: &'static CStr) -> Result<bindings::of_device_id> {
let mut buf = [0_u8; 128];
if compatible.len() > buf.len() {
return Err(Error::EINVAL);
Expand Down
8 changes: 4 additions & 4 deletions rust/kernel/platdev.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ use crate::{
error::{Error, Result},
of::OfMatchTable,
pr_info,
str::CStr,
types::PointerWrapper,
CStr,
};
use alloc::boxed::Box;
use core::{marker::PhantomPinned, pin::Pin};
Expand Down Expand Up @@ -43,7 +43,7 @@ extern "C" fn remove_callback(_pdev: *mut bindings::platform_device) -> c_types:
impl Registration {
fn register(
self: Pin<&mut Self>,
name: CStr<'static>,
name: &'static CStr,
of_match_table: Option<OfMatchTable>,
module: &'static crate::ThisModule,
) -> Result {
Expand All @@ -53,7 +53,7 @@ impl Registration {
// Already registered.
return Err(Error::EINVAL);
}
this.pdrv.driver.name = name.as_ptr() as *const c_types::c_char;
this.pdrv.driver.name = name.as_char_ptr();
if let Some(tbl) = of_match_table {
let ptr = tbl.into_pointer();
this.of_table = Some(ptr);
Expand Down Expand Up @@ -82,7 +82,7 @@ impl Registration {
///
/// Returns a pinned heap-allocated representation of the registration.
pub fn new_pinned(
name: CStr<'static>,
name: &'static CStr,
of_match_tbl: Option<OfMatchTable>,
module: &'static crate::ThisModule,
) -> Result<Pin<Box<Self>>> {
Expand Down
Loading