|
| 1 | +// SPDX-License-Identifier: GPL-2.0 |
| 2 | + |
| 3 | +//! Kernel page allocation and management. |
| 4 | +//! |
| 5 | +//! TODO: This module is a work in progress. |
| 6 | +
|
| 7 | +use crate::{bindings, c_types, user_ptr::UserSlicePtrReader, Error, KernelResult, PAGE_SIZE}; |
| 8 | +use core::{marker::PhantomData, ptr}; |
| 9 | + |
| 10 | +extern "C" { |
| 11 | + #[allow(improper_ctypes)] |
| 12 | + fn rust_helper_alloc_pages( |
| 13 | + gfp_mask: bindings::gfp_t, |
| 14 | + order: c_types::c_uint, |
| 15 | + ) -> *mut bindings::page; |
| 16 | + |
| 17 | + #[allow(improper_ctypes)] |
| 18 | + fn rust_helper_kmap(page: *mut bindings::page) -> *mut c_types::c_void; |
| 19 | + |
| 20 | + #[allow(improper_ctypes)] |
| 21 | + fn rust_helper_kunmap(page: *mut bindings::page); |
| 22 | +} |
| 23 | + |
| 24 | +/// A set of physical pages. |
| 25 | +/// |
| 26 | +/// `Pages` holds a reference to a set of pages of order `ORDER`. Having the order as a generic |
| 27 | +/// const allows the struct to have the same size as a pointer. |
| 28 | +/// |
| 29 | +/// # Invariants |
| 30 | +/// |
| 31 | +/// The pointer [`Pages::pages`] is valid and points to 2^ORDER pages. |
| 32 | +pub struct Pages<const ORDER: u32> { |
| 33 | + pages: *mut bindings::page, |
| 34 | +} |
| 35 | + |
| 36 | +impl<const ORDER: u32> Pages<ORDER> { |
| 37 | + /// Allocates a new set of contiguous pages. |
| 38 | + pub fn new() -> KernelResult<Self> { |
| 39 | + // TODO: Consider whether we want to allow callers to specify flags. |
| 40 | + // SAFETY: This only allocates pages. We check that it succeeds in the next statement. |
| 41 | + let pages = unsafe { |
| 42 | + rust_helper_alloc_pages( |
| 43 | + bindings::GFP_KERNEL | bindings::__GFP_ZERO | bindings::__GFP_HIGHMEM, |
| 44 | + ORDER, |
| 45 | + ) |
| 46 | + }; |
| 47 | + if pages.is_null() { |
| 48 | + return Err(Error::ENOMEM); |
| 49 | + } |
| 50 | + // INVARIANTS: We checked that the allocation above succeeded> |
| 51 | + Ok(Self { pages }) |
| 52 | + } |
| 53 | + |
| 54 | + /// Maps a single page at the given address in the given VM area. |
| 55 | + /// |
| 56 | + /// This is only meant to be used by pages of order 0. |
| 57 | + pub fn insert_page(&self, vma: &mut bindings::vm_area_struct, address: usize) -> KernelResult { |
| 58 | + if ORDER != 0 { |
| 59 | + return Err(Error::EINVAL); |
| 60 | + } |
| 61 | + |
| 62 | + // SAFETY: We check above that the allocation is of order 0. The range of `address` is |
| 63 | + // already checked by `vm_insert_page`. |
| 64 | + let ret = unsafe { bindings::vm_insert_page(vma, address as _, self.pages) }; |
| 65 | + if ret != 0 { |
| 66 | + Err(Error::from_kernel_errno(ret)) |
| 67 | + } else { |
| 68 | + Ok(()) |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + /// Copies data from the given [`UserSlicePtrReader`] into the pages. |
| 73 | + pub fn copy_into_page( |
| 74 | + &self, |
| 75 | + reader: &mut UserSlicePtrReader, |
| 76 | + offset: usize, |
| 77 | + len: usize, |
| 78 | + ) -> KernelResult { |
| 79 | + // TODO: For now this only works on the first page. |
| 80 | + let end = offset.checked_add(len).ok_or(Error::EINVAL)?; |
| 81 | + if end > PAGE_SIZE { |
| 82 | + return Err(Error::EINVAL); |
| 83 | + } |
| 84 | + |
| 85 | + let mapping = self.kmap(0).ok_or(Error::EINVAL)?; |
| 86 | + |
| 87 | + // SAFETY: We ensured that the buffer was valid with the check above. |
| 88 | + unsafe { reader.read_raw((mapping.ptr as usize + offset) as _, len) }?; |
| 89 | + Ok(()) |
| 90 | + } |
| 91 | + |
| 92 | + /// Maps the pages and reads from them into the given buffer. |
| 93 | + /// |
| 94 | + /// # Safety |
| 95 | + /// |
| 96 | + /// Callers must ensure that the destination buffer is valid for the given length. |
| 97 | + /// Additionally, if the raw buffer is intended to be recast, they must ensure that the data |
| 98 | + /// can be safely cast; [`crate::user_ptr::ReadableFromBytes`] has more details about it. |
| 99 | + pub unsafe fn read(&self, dest: *mut u8, offset: usize, len: usize) -> KernelResult { |
| 100 | + // TODO: For now this only works on the first page. |
| 101 | + let end = offset.checked_add(len).ok_or(Error::EINVAL)?; |
| 102 | + if end > PAGE_SIZE { |
| 103 | + return Err(Error::EINVAL); |
| 104 | + } |
| 105 | + |
| 106 | + let mapping = self.kmap(0).ok_or(Error::EINVAL)?; |
| 107 | + ptr::copy((mapping.ptr as *mut u8).add(offset), dest, len); |
| 108 | + Ok(()) |
| 109 | + } |
| 110 | + |
| 111 | + /// Maps the pages and writes into them from the given bufer. |
| 112 | + /// |
| 113 | + /// # Safety |
| 114 | + /// |
| 115 | + /// Callers must ensure that the buffer is valid for the given length. Additionally, if the |
| 116 | + /// page is (or will be) mapped by userspace, they must ensure that no kernel data is leaked |
| 117 | + /// through padding if it was cast from another type; [`crate::user_ptr::WritableToBytes`] has |
| 118 | + /// more details about it. |
| 119 | + pub unsafe fn write(&self, src: *const u8, offset: usize, len: usize) -> KernelResult { |
| 120 | + // TODO: For now this only works on the first page. |
| 121 | + let end = offset.checked_add(len).ok_or(Error::EINVAL)?; |
| 122 | + if end > PAGE_SIZE { |
| 123 | + return Err(Error::EINVAL); |
| 124 | + } |
| 125 | + |
| 126 | + let mapping = self.kmap(0).ok_or(Error::EINVAL)?; |
| 127 | + ptr::copy(src, (mapping.ptr as *mut u8).add(offset), len); |
| 128 | + Ok(()) |
| 129 | + } |
| 130 | + |
| 131 | + /// Maps the page at index `index`. |
| 132 | + fn kmap(&self, index: usize) -> Option<PageMapping> { |
| 133 | + if index >= 1usize << ORDER { |
| 134 | + return None; |
| 135 | + } |
| 136 | + |
| 137 | + // SAFETY: We checked above that `index` is within range. |
| 138 | + let page = unsafe { self.pages.add(index) }; |
| 139 | + |
| 140 | + // SAFETY: `page` is valid based on the checks above. |
| 141 | + let ptr = unsafe { rust_helper_kmap(page) }; |
| 142 | + if ptr.is_null() { |
| 143 | + return None; |
| 144 | + } |
| 145 | + |
| 146 | + Some(PageMapping { |
| 147 | + page, |
| 148 | + ptr, |
| 149 | + _phantom: PhantomData, |
| 150 | + }) |
| 151 | + } |
| 152 | +} |
| 153 | + |
| 154 | +impl<const ORDER: u32> Drop for Pages<ORDER> { |
| 155 | + fn drop(&mut self) { |
| 156 | + // SAFETY: By the type invariants, we know the pages are allocated with the given order. |
| 157 | + unsafe { bindings::__free_pages(self.pages, ORDER) }; |
| 158 | + } |
| 159 | +} |
| 160 | + |
| 161 | +struct PageMapping<'a> { |
| 162 | + page: *mut bindings::page, |
| 163 | + ptr: *mut c_types::c_void, |
| 164 | + _phantom: PhantomData<&'a i32>, |
| 165 | +} |
| 166 | + |
| 167 | +impl Drop for PageMapping<'_> { |
| 168 | + fn drop(&mut self) { |
| 169 | + // SAFETY: An instance of `PageMapping` is created only when `kmap` succeeded for the given |
| 170 | + // page, so it is safe to unmap it here. |
| 171 | + unsafe { rust_helper_kunmap(self.page) }; |
| 172 | + } |
| 173 | +} |
0 commit comments