Skip to content

Commit

Permalink
mm/vm/mapping: replace Box with GlobalBox
Browse files Browse the repository at this point in the history
Replace uses of Box in the mapping API with the new
fallible-allocation-aware GlobalBox type.

Signed-off-by: Carlos López <[email protected]>
  • Loading branch information
00xc committed Feb 19, 2024
1 parent 90793d2 commit ef3e78c
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 22 deletions.
12 changes: 6 additions & 6 deletions kernel/src/cpu/percpu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ impl PerCpu {
fn allocate_stack(&mut self, base: VirtAddr) -> Result<VirtAddr, SvsmError> {
let stack = VMKernelStack::new()?;
let top_of_stack = stack.top_of_stack(base);
let mapping = Arc::new(Mapping::new(stack));
let mapping = Arc::new(Mapping::new(stack)?);

self.vm_range.insert_at(base, mapping)?;

Expand Down Expand Up @@ -406,20 +406,20 @@ impl PerCpu {
let vaddr = VirtAddr::from(self as *const PerCpu);
let paddr = virt_to_phys(vaddr);

let self_mapping = Arc::new(VMPhysMem::new_mapping(paddr, PAGE_SIZE, true));
let self_mapping = Arc::new(VMPhysMem::new_mapping(paddr, PAGE_SIZE, true)?);
self.vm_range.insert_at(SVSM_PERCPU_BASE, self_mapping)?;

Ok(())
}

fn initialize_vm_ranges(&mut self) -> Result<(), SvsmError> {
let size_4k = SVSM_PERCPU_TEMP_END_4K - SVSM_PERCPU_TEMP_BASE_4K;
let temp_mapping_4k = Arc::new(VMReserved::new_mapping(size_4k));
let temp_mapping_4k = Arc::new(VMReserved::new_mapping(size_4k)?);
self.vm_range
.insert_at(SVSM_PERCPU_TEMP_BASE_4K, temp_mapping_4k)?;

let size_2m = SVSM_PERCPU_TEMP_END_2M - SVSM_PERCPU_TEMP_BASE_2M;
let temp_mapping_2m = Arc::new(VMReserved::new_mapping(size_2m));
let temp_mapping_2m = Arc::new(VMReserved::new_mapping(size_2m)?);
self.vm_range
.insert_at(SVSM_PERCPU_TEMP_BASE_2M, temp_mapping_2m)?;

Expand Down Expand Up @@ -534,7 +534,7 @@ impl PerCpu {

pub fn map_guest_vmsa(&self, paddr: PhysAddr) -> Result<(), SvsmError> {
assert!(self.apic_id == this_cpu().get_apic_id());
let vmsa_mapping = Arc::new(VMPhysMem::new_mapping(paddr, PAGE_SIZE, true));
let vmsa_mapping = Arc::new(VMPhysMem::new_mapping(paddr, PAGE_SIZE, true)?);
self.vm_range
.insert_at(SVSM_PERCPU_VMSA_BASE, vmsa_mapping)?;

Expand Down Expand Up @@ -565,7 +565,7 @@ impl PerCpu {
pub fn map_guest_caa(&self, paddr: PhysAddr) -> Result<(), SvsmError> {
self.unmap_caa();

let caa_mapping = Arc::new(VMPhysMem::new_mapping(paddr, PAGE_SIZE, true));
let caa_mapping = Arc::new(VMPhysMem::new_mapping(paddr, PAGE_SIZE, true)?);
self.vm_range.insert_at(SVSM_PERCPU_CAA_BASE, caa_mapping)?;

Ok(())
Expand Down
21 changes: 11 additions & 10 deletions kernel/src/mm/vm/mapping/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use crate::address::{PhysAddr, VirtAddr};
use crate::error::SvsmError;
use crate::globalbox_upcast;
use crate::locking::{RWLock, ReadLockGuard, WriteLockGuard};
use crate::mm::pagetable::PTEntryFlags;
use crate::mm::vm::VMR;
Expand All @@ -22,7 +23,6 @@ use intrusive_collections::{
use core::ops::Range;

extern crate alloc;
use alloc::boxed::Box;
use alloc::sync::Arc;

/// Information required to resolve a page fault within a virtual mapping
Expand Down Expand Up @@ -144,27 +144,28 @@ pub trait VirtualMapping: fmt::Debug {

#[derive(Debug)]
pub struct Mapping {
mapping: RWLock<Box<dyn VirtualMapping>>,
mapping: RWLock<GlobalBox<dyn VirtualMapping>>,
}

unsafe impl Send for Mapping {}
unsafe impl Sync for Mapping {}

impl Mapping {
pub fn new<T>(mapping: T) -> Self
pub fn new<T>(mapping: T) -> Result<Self, SvsmError>
where
T: VirtualMapping + 'static,
{
Mapping {
mapping: RWLock::new(Box::new(mapping)),
}
let boxed = globalbox_upcast!(GlobalBox::try_new(mapping)?, VirtualMapping);
Ok(Self {
mapping: RWLock::new(boxed),
})
}

pub fn get(&self) -> ReadLockGuard<'_, Box<dyn VirtualMapping>> {
pub fn get(&self) -> ReadLockGuard<'_, GlobalBox<dyn VirtualMapping>> {
self.mapping.lock_read()
}

pub fn get_mut(&self) -> WriteLockGuard<'_, Box<dyn VirtualMapping>> {
pub fn get_mut(&self) -> WriteLockGuard<'_, GlobalBox<dyn VirtualMapping>> {
self.mapping.lock_write()
}
}
Expand Down Expand Up @@ -232,11 +233,11 @@ impl VMM {
)
}

pub fn get_mapping(&self) -> ReadLockGuard<'_, Box<dyn VirtualMapping>> {
pub fn get_mapping(&self) -> ReadLockGuard<'_, GlobalBox<dyn VirtualMapping>> {
self.mapping.get()
}

pub fn get_mapping_mut(&self) -> WriteLockGuard<'_, Box<dyn VirtualMapping>> {
pub fn get_mapping_mut(&self) -> WriteLockGuard<'_, GlobalBox<dyn VirtualMapping>> {
self.mapping.get_mut()
}

Expand Down
2 changes: 1 addition & 1 deletion kernel/src/mm/vm/mapping/file_mapping.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ fn copy_page(
) -> Result<(), SvsmError> {
let page_size = usize::from(page_size);
let temp_map = VMPhysMem::new(paddr_dst, page_size, true);
let vaddr_new_page = vmr.insert(Arc::new(Mapping::new(temp_map)))?;
let vaddr_new_page = vmr.insert(Arc::new(Mapping::new(temp_map)?))?;
let slice = unsafe { from_raw_parts_mut(vaddr_new_page.as_mut_ptr::<u8>(), page_size) };
file.seek(offset);
file.read(slice)?;
Expand Down
2 changes: 1 addition & 1 deletion kernel/src/mm/vm/mapping/kernel_stack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ impl VMKernelStack {
///
/// Initialized Mapping to stack on success, Err(SvsmError::Mem) on error
pub fn new_mapping() -> Result<Mapping, SvsmError> {
Ok(Mapping::new(Self::new()?))
Mapping::new(Self::new()?)
}

fn alloc_pages(&mut self) -> Result<(), SvsmError> {
Expand Down
3 changes: 2 additions & 1 deletion kernel/src/mm/vm/mapping/phys_mem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
// Author: Joerg Roedel <[email protected]>

use crate::address::{Address, PhysAddr};
use crate::error::SvsmError;
use crate::mm::pagetable::PTEntryFlags;

use super::{Mapping, VirtualMapping};
Expand Down Expand Up @@ -51,7 +52,7 @@ impl VMPhysMem {
/// # Returns
///
/// New [`Mapping`] containing [`VMPhysMem`]
pub fn new_mapping(base: PhysAddr, size: usize, writable: bool) -> Mapping {
pub fn new_mapping(base: PhysAddr, size: usize, writable: bool) -> Result<Mapping, SvsmError> {
Mapping::new(Self::new(base, size, writable))
}
}
Expand Down
3 changes: 2 additions & 1 deletion kernel/src/mm/vm/mapping/reserved.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
// Author: Joerg Roedel <[email protected]>

use crate::address::PhysAddr;
use crate::error::SvsmError;
use crate::mm::pagetable::PTEntryFlags;

use super::{Mapping, VirtualMapping};
Expand Down Expand Up @@ -41,7 +42,7 @@ impl VMReserved {
/// # Returns
///
/// New Mapping of VMReserved
pub fn new_mapping(size: usize) -> Mapping {
pub fn new_mapping(size: usize) -> Result<Mapping, SvsmError> {
Mapping::new(Self::new(size))
}
}
Expand Down
2 changes: 1 addition & 1 deletion kernel/src/mm/vm/mapping/vmalloc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ impl VMalloc {
///
/// New [`Mapping`] on success, Err(SvsmError::Mem) on error
pub fn new_mapping(size: usize) -> Result<Mapping, SvsmError> {
Ok(Mapping::new(Self::new(size)?))
Mapping::new(Self::new(size)?)
}

fn alloc_pages(&mut self) -> Result<(), SvsmError> {
Expand Down
2 changes: 1 addition & 1 deletion kernel/src/task/tasks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ impl Task {
let stack = VMKernelStack::new()?;
let bounds = stack.bounds(VirtAddr::from(0u64));

let mapping = Arc::new(Mapping::new(stack));
let mapping = Arc::new(Mapping::new(stack)?);
let percpu_mapping = cpu.new_mapping(mapping.clone())?;

// We need to setup a context on the stack that matches the stack layout
Expand Down

0 comments on commit ef3e78c

Please sign in to comment.