Skip to content

Commit

Permalink
mm/vm: replace Arc with GlobalArc
Browse files Browse the repository at this point in the history
Replace the uses of Arc with the new fallible-allocation-aware
GlobalArc type. Changes in this case are trivial.

Signed-off-by: Carlos López <[email protected]>
  • Loading branch information
00xc committed Feb 8, 2024
1 parent 3b31a11 commit cd3b6ee
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 30 deletions.
17 changes: 8 additions & 9 deletions kernel/src/cpu/percpu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use crate::mm::pagetable::{get_init_pgtable_locked, PTEntryFlags, PageTableRef};
use crate::mm::virtualrange::VirtualRange;
use crate::mm::vm::{Mapping, VMKernelStack, VMPhysMem, VMRMapping, VMReserved, VMR};
use crate::mm::{
virt_to_phys, SVSM_PERCPU_BASE, SVSM_PERCPU_CAA_BASE, SVSM_PERCPU_END,
virt_to_phys, GlobalArc, SVSM_PERCPU_BASE, SVSM_PERCPU_CAA_BASE, SVSM_PERCPU_END,
SVSM_PERCPU_TEMP_BASE_2M, SVSM_PERCPU_TEMP_BASE_4K, SVSM_PERCPU_TEMP_END_2M,
SVSM_PERCPU_TEMP_END_4K, SVSM_PERCPU_VMSA_BASE, SVSM_STACKS_INIT_TASK, SVSM_STACK_IST_DF_BASE,
};
Expand All @@ -31,7 +31,6 @@ use crate::task::{
};
use crate::types::{PAGE_SHIFT, PAGE_SHIFT_2M, PAGE_SIZE, PAGE_SIZE_2M, SVSM_TR_FLAGS, SVSM_TSS};
use crate::utils::MemoryRegion;
use alloc::sync::Arc;
use alloc::vec::Vec;
use core::cell::UnsafeCell;
use core::mem::size_of;
Expand Down Expand Up @@ -347,7 +346,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 = GlobalArc::try_new(Mapping::new(stack)?)?;

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

Expand Down Expand Up @@ -406,20 +405,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 = GlobalArc::try_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 = GlobalArc::try_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 = GlobalArc::try_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 +533,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 = GlobalArc::try_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 +564,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 = GlobalArc::try_new(VMPhysMem::new_mapping(paddr, PAGE_SIZE, true)?)?;
self.vm_range.insert_at(SVSM_PERCPU_CAA_BASE, caa_mapping)?;

Ok(())
Expand Down Expand Up @@ -606,7 +605,7 @@ impl PerCpu {
/// the mapping which remains valid until the ['VRMapping'] is dropped.
///
/// On error, an ['SvsmError'].
pub fn new_mapping(&mut self, mapping: Arc<Mapping>) -> Result<VMRMapping<'_>, SvsmError> {
pub fn new_mapping(&mut self, mapping: GlobalArc<Mapping>) -> Result<VMRMapping<'_>, SvsmError> {
VMRMapping::new(&mut self.vm_range, mapping)
}

Expand Down
11 changes: 4 additions & 7 deletions kernel/src/mm/vm/mapping/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::globalbox_upcast;
use crate::locking::{RWLock, ReadLockGuard, WriteLockGuard};
use crate::mm::pagetable::PTEntryFlags;
use crate::mm::vm::VMR;
use crate::mm::GlobalBox;
use crate::mm::{GlobalArc, GlobalBox};
use crate::types::{PageSize, PAGE_SHIFT};
use core::fmt;
use core::marker::PhantomData;
Expand All @@ -22,9 +22,6 @@ use intrusive_collections::{

use core::ops::Range;

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

/// Information required to resolve a page fault within a virtual mapping
#[derive(Debug, Copy, Clone)]
pub struct VMPageFaultResolution {
Expand Down Expand Up @@ -185,7 +182,7 @@ pub struct VMM {

/// Pointer to the actual mapping
/// It is protected by an RWLock to serialize concurent accesses.
mapping: Arc<Mapping>,
mapping: GlobalArc<Mapping>,
}

impl VMM {
Expand All @@ -199,7 +196,7 @@ impl VMM {
/// # Returns
///
/// New instance of VMM
pub fn new(start_pfn: usize, mapping: Arc<Mapping>) -> Self {
pub fn new(start_pfn: usize, mapping: GlobalArc<Mapping>) -> Self {
let size = mapping.get().mapping_size() >> PAGE_SHIFT;
VMM {
link: Link::new(),
Expand Down Expand Up @@ -241,7 +238,7 @@ impl VMM {
self.mapping.get_mut()
}

pub fn get_mapping_clone(&self) -> Arc<Mapping> {
pub fn get_mapping_clone(&self) -> GlobalArc<Mapping> {
self.mapping.clone()
}
}
Expand Down
7 changes: 3 additions & 4 deletions kernel/src/mm/vm/mapping/file_mapping.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@ extern crate alloc;

use core::slice::from_raw_parts_mut;

#[cfg(not(test))]
use alloc::sync::Arc;

use alloc::vec::Vec;

#[cfg(not(test))]
Expand All @@ -23,6 +20,8 @@ use crate::address::PhysAddr;
use crate::error::SvsmError;
use crate::fs::FileHandle;
use crate::mm::vm::VMR;
#[cfg(not(test))]
use crate::mm::GlobalArc;
use crate::mm::PageRef;
use crate::mm::{pagetable::PTEntryFlags, PAGE_SIZE};
use crate::types::{PageSize, PAGE_SHIFT};
Expand Down Expand Up @@ -153,7 +152,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(GlobalArc::try_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
17 changes: 10 additions & 7 deletions kernel/src/mm/vm/range.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::cpu::flush_tlb_global_sync;
use crate::error::SvsmError;
use crate::locking::RWLock;
use crate::mm::pagetable::{PTEntryFlags, PageTable, PageTablePart, PageTableRef};
use crate::mm::GlobalBox;
use crate::mm::{GlobalArc, GlobalBox};
use crate::types::{PageSize, PAGE_SHIFT, PAGE_SIZE};
use crate::utils::{align_down, align_up};

Expand All @@ -21,7 +21,6 @@ use intrusive_collections::Bound;
use super::{Mapping, VMMAdapter, VMM};

extern crate alloc;
use alloc::sync::Arc;
use alloc::vec::Vec;

/// Granularity of ranges mapped by [`struct VMR`]. The mapped region of a
Expand Down Expand Up @@ -213,7 +212,7 @@ impl VMR {

fn do_insert(
&self,
mapping: Arc<Mapping>,
mapping: GlobalArc<Mapping>,
start_pfn: usize,
cursor: &mut CursorMut<'_, VMMAdapter>,
) -> Result<(), SvsmError> {
Expand All @@ -238,7 +237,11 @@ impl VMR {
/// # Returns
///
/// Base address where the [`VMM`] was inserted on success or SvsmError::Mem on error
pub fn insert_at(&self, vaddr: VirtAddr, mapping: Arc<Mapping>) -> Result<VirtAddr, SvsmError> {
pub fn insert_at(
&self,
vaddr: VirtAddr,
mapping: GlobalArc<Mapping>,
) -> Result<VirtAddr, SvsmError> {
// mapping-size needs to be page-aligned
let size = mapping.get().mapping_size() >> PAGE_SHIFT;
let start_pfn = vaddr.pfn();
Expand Down Expand Up @@ -283,7 +286,7 @@ impl VMR {
/// Base address where the [`VMM`] was inserted on success or SvsmError::Mem on error
pub fn insert_aligned(
&self,
mapping: Arc<Mapping>,
mapping: GlobalArc<Mapping>,
align: usize,
) -> Result<VirtAddr, SvsmError> {
assert!(align.is_power_of_two());
Expand Down Expand Up @@ -341,7 +344,7 @@ impl VMR {
/// # Returns
///
/// Base address where the [`VMM`] was inserted on success or SvsmError::Mem on error
pub fn insert(&self, mapping: Arc<Mapping>) -> Result<VirtAddr, SvsmError> {
pub fn insert(&self, mapping: GlobalArc<Mapping>) -> Result<VirtAddr, SvsmError> {
let align = mapping.get().mapping_size().next_power_of_two();
self.insert_aligned(mapping, align)
}
Expand Down Expand Up @@ -446,7 +449,7 @@ pub struct VMRMapping<'a> {
}

impl<'a> VMRMapping<'a> {
pub fn new(vmr: &'a mut VMR, mapping: Arc<Mapping>) -> Result<Self, SvsmError> {
pub fn new(vmr: &'a mut VMR, mapping: GlobalArc<Mapping>) -> Result<Self, SvsmError> {
let va = vmr.insert(mapping)?;
Ok(Self { vmr, va })
}
Expand Down
6 changes: 3 additions & 3 deletions kernel/src/task/tasks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use crate::error::SvsmError;
use crate::locking::{RWLock, SpinLock};
use crate::mm::pagetable::{get_init_pgtable_locked, PTEntryFlags, PageTableRef};
use crate::mm::vm::{Mapping, VMKernelStack, VMR};
use crate::mm::{SVSM_PERTASK_BASE, SVSM_PERTASK_END, SVSM_PERTASK_STACK_BASE};
use crate::mm::{GlobalArc, SVSM_PERTASK_BASE, SVSM_PERTASK_END, SVSM_PERTASK_STACK_BASE};
use crate::utils::MemoryRegion;
use intrusive_collections::{intrusive_adapter, LinkedListAtomicLink};

Expand Down Expand Up @@ -264,11 +264,11 @@ impl Task {
fn allocate_stack(
cpu: &mut PerCpu,
entry: extern "C" fn(),
) -> Result<(Arc<Mapping>, MemoryRegion<VirtAddr>, usize), SvsmError> {
) -> Result<(GlobalArc<Mapping>, MemoryRegion<VirtAddr>, usize), SvsmError> {
let stack = VMKernelStack::new()?;
let bounds = stack.bounds(VirtAddr::from(0u64));

let mapping = Arc::new(Mapping::new(stack)?);
let mapping = GlobalArc::try_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 cd3b6ee

Please sign in to comment.