Skip to content

Commit

Permalink
feat(mm): add DeviceAlloc for communicating with devices
Browse files Browse the repository at this point in the history
Signed-off-by: Martin Kröning <martin.kroening@eonerc.rwth-aachen.de>
  • Loading branch information
mkroening committed May 2, 2024
1 parent e403aa0 commit 38f8fd3
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
28 changes: 28 additions & 0 deletions src/mm/device_alloc.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
use core::alloc::{AllocError, Allocator, Layout};
use core::ptr::{self, NonNull};

use align_address::Align;

use crate::arch::mm::paging::{BasePageSize, PageSize};

/// An [`Allocator`] for memory that is used to communicate with devices.
///
/// Allocations from this allocator always correspond to contiguous physical memory.
pub struct DeviceAlloc;

unsafe impl Allocator for DeviceAlloc {
fn allocate(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError> {
assert!(layout.align() <= BasePageSize::SIZE as usize);
let size = layout.size().align_up(BasePageSize::SIZE as usize);
let ptr = super::allocate(size, true).as_mut_ptr::<u8>();
let slice = ptr::slice_from_raw_parts_mut(ptr, size);
Ok(NonNull::new(slice).unwrap())
}

unsafe fn deallocate(&self, ptr: NonNull<u8>, layout: Layout) {
assert!(layout.align() <= BasePageSize::SIZE as usize);
let size = layout.size().align_up(BasePageSize::SIZE as usize);
let addr = ptr.as_ptr().expose_provenance().into();
super::deallocate(addr, size);
}
}
5 changes: 3 additions & 2 deletions src/mm/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pub mod allocator;
pub mod device_alloc;
pub mod freelist;

use core::mem;
Expand Down Expand Up @@ -307,7 +308,7 @@ pub(crate) fn print_information() {
arch::mm::virtualmem::print_information();
}

#[allow(dead_code)]
/// Soft-deprecated in favor of `DeviceAlloc`
pub(crate) fn allocate(sz: usize, no_execution: bool) -> VirtAddr {
let size = sz.align_up(BasePageSize::SIZE as usize);
let physical_address = arch::mm::physicalmem::allocate(size).unwrap();
Expand All @@ -324,7 +325,7 @@ pub(crate) fn allocate(sz: usize, no_execution: bool) -> VirtAddr {
virtual_address
}

#[allow(dead_code)]
/// Soft-deprecated in favor of `DeviceAlloc`
pub(crate) fn deallocate(virtual_address: VirtAddr, sz: usize) {
let size = sz.align_up(BasePageSize::SIZE as usize);

Expand Down

0 comments on commit 38f8fd3

Please sign in to comment.