Skip to content

Commit 62efc5e

Browse files
dma: deallocate
Signed-off-by: Anhad Singh <[email protected]>
1 parent 43a71aa commit 62efc5e

File tree

3 files changed

+16
-2
lines changed

3 files changed

+16
-2
lines changed

src/aero_kernel/src/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
const_ptr_is_null, // https://github.com/rust-lang/rust/issues/74939
4848
trait_upcasting, // https://github.com/rust-lang/rust/issues/65991
4949
naked_functions, // https://github.com/rust-lang/rust/issues/32408
50+
strict_provenance
5051
)]
5152
#![deny(trivial_numeric_casts, unused_allocation)]
5253
#![test_runner(crate::tests::test_runner)]

src/aero_kernel/src/mem/paging/frame.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,13 @@ impl LockedFrameAllocator {
7777
.call_once(|| Mutex::new(GlobalFrameAllocator::new(memory_map)));
7878
}
7979

80+
pub fn dealloc(&self, addr: PhysAddr, size_bytes: usize) {
81+
let order = order_from_size(size_bytes as u64);
82+
83+
let mut allocator = self.0.get().unwrap().lock_irq();
84+
allocator.deallocate_frame_inner(addr, order);
85+
}
86+
8087
pub fn alloc(&self, size_bytes: usize) -> Option<PhysAddr> {
8188
let order = order_from_size(size_bytes as u64);
8289

src/aero_kernel/src/utils/dma.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ pub struct DmaAllocator;
3030
// the data using the ISA or PCI system bus (which carry physical addresses).
3131
unsafe impl Allocator for DmaAllocator {
3232
fn allocate(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError> {
33-
// XXX: The DMA buffer must be aligned to a page boundary.
3433
let size_bytes = layout.size();
3534

3635
let phys = FRAME_ALLOCATOR.alloc_zeroed(size_bytes).ok_or(AllocError)?;
@@ -41,7 +40,14 @@ unsafe impl Allocator for DmaAllocator {
4140
Ok(NonNull::slice_from_raw_parts(ptr, size_bytes))
4241
}
4342

44-
unsafe fn deallocate(&self, _ptr: NonNull<u8>, _layout: Layout) {}
43+
unsafe fn deallocate(&self, ptr: NonNull<u8>, layout: Layout) {
44+
let size_bytes = layout.size();
45+
46+
let addr: usize = ptr.addr().into();
47+
let addr = VirtAddr::new(addr as u64);
48+
49+
FRAME_ALLOCATOR.dealloc(addr.as_hhdm_phys(), size_bytes);
50+
}
4551
}
4652

4753
pub type DmaBuffer<T> = Box<T, DmaAllocator>;

0 commit comments

Comments
 (0)