Struct BorrowedPage

Source
pub struct BorrowedPage<'a>(/* private fields */);
Expand description

Representation of a non-owning reference to a Page.

This type provides a borrowed version of a Page that is owned by some other entity, e.g. a Vmalloc allocation such as VBox.

§Example

use kernel::page::{BorrowedPage, Page, PAGE_SIZE};

fn borrow_page<'a>(vbox: &'a mut VBox<MaybeUninit<[u8; PAGE_SIZE]>>) -> BorrowedPage<'a> {
    let ptr = ptr::from_ref(&**vbox);

    // SAFETY: `ptr` is a valid pointer to `Vmalloc` memory.
    let page = unsafe { bindings::vmalloc_to_page(ptr.cast()) };

    // SAFETY: `vmalloc_to_page` returns a valid pointer to a `struct page` for a valid
    // pointer to `Vmalloc` memory.
    let page = unsafe { NonNull::new_unchecked(page) };

    // SAFETY:
    // - `self.0` is a valid pointer to a `struct page`.
    // - `self.0` is valid for the entire lifetime of `self`.
    unsafe { BorrowedPage::from_raw(page) }
}

let mut vbox = VBox::<[u8; PAGE_SIZE]>::new_uninit(GFP_KERNEL)?;
let page = borrow_page(&mut vbox);

// SAFETY: There is no concurrent read or write to this page.
unsafe { page.fill_zero_raw(0, PAGE_SIZE)? };

§Invariants

The borrowed underlying pointer to a struct page is valid for the entire lifetime 'a.

Implementations§

Source§

impl<'a> BorrowedPage<'a>

Source

pub unsafe fn from_raw(ptr: NonNull<page>) -> Self

Constructs a BorrowedPage from a raw pointer to a struct page.

§Safety
  • ptr must point to a valid bindings::page.
  • ptr must remain valid for the entire lifetime 'a.

Methods from Deref<Target = Page>§

Source

pub fn as_ptr(&self) -> *mut page

Returns a raw pointer to the page.

Source

pub unsafe fn read_raw(&self, dst: *mut u8, offset: usize, len: usize) -> Result

Maps the page and reads from it into the given buffer.

This method will perform bounds checks on the page offset. If offset .. offset+len goes outside of the page, then this call returns EINVAL.

§Safety
  • Callers must ensure that dst is valid for writing len bytes.
  • Callers must ensure that this call does not race with a write to the same page that overlaps with this read.
Source

pub unsafe fn write_raw( &self, src: *const u8, offset: usize, len: usize, ) -> Result

Maps the page and writes into it from the given buffer.

This method will perform bounds checks on the page offset. If offset .. offset+len goes outside of the page, then this call returns EINVAL.

§Safety
  • Callers must ensure that src is valid for reading len bytes.
  • Callers must ensure that this call does not race with a read or write to the same page that overlaps with this write.
Source

pub unsafe fn fill_zero_raw(&self, offset: usize, len: usize) -> Result

Maps the page and zeroes the given slice.

This method will perform bounds checks on the page offset. If offset .. offset+len goes outside of the page, then this call returns EINVAL.

§Safety

Callers must ensure that this call does not race with a read or write to the same page that overlaps with this write.

Source

pub unsafe fn copy_from_user_slice_raw( &self, reader: &mut UserSliceReader, offset: usize, len: usize, ) -> Result

Copies data from userspace into this page.

This method will perform bounds checks on the page offset. If offset .. offset+len goes outside of the page, then this call returns EINVAL.

Like the other UserSliceReader methods, data races are allowed on the userspace address. However, they are not allowed on the page you are copying into.

§Safety

Callers must ensure that this call does not race with a read or write to the same page that overlaps with this write.

Trait Implementations§

Source§

impl<'a> Deref for BorrowedPage<'a>

Source§

type Target = Page

The resulting type after dereferencing.
Source§

fn deref(&self) -> &Self::Target

Dereferences the value.

Auto Trait Implementations§

§

impl<'a> Freeze for BorrowedPage<'a>

§

impl<'a> RefUnwindSafe for BorrowedPage<'a>

§

impl<'a> Send for BorrowedPage<'a>

§

impl<'a> Sync for BorrowedPage<'a>

§

impl<'a> Unpin for BorrowedPage<'a>

§

impl<'a> UnwindSafe for BorrowedPage<'a>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Init<T> for T

Source§

unsafe fn __init(self, slot: *mut T) -> Result<(), Infallible>

Initializes slot. Read more
Source§

fn chain<F>(self, f: F) -> ChainInit<Self, F, T, E>
where F: FnOnce(&mut T) -> Result<(), E>,

First initializes the value using self then calls the function f with the initialized value. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> PinInit<T> for T

Source§

unsafe fn __pinned_init(self, slot: *mut T) -> Result<(), Infallible>

Initializes slot. Read more
Source§

fn pin_chain<F>(self, f: F) -> ChainPinInit<Self, F, T, E>
where F: FnOnce(Pin<&mut T>) -> Result<(), E>,

First initializes the value using self then calls the function f with the initialized value. Read more
Source§

impl<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.