Skip to content

Commit

Permalink
Merge pull request #20 from arkivm/fix/reuse-helpers
Browse files Browse the repository at this point in the history
Fix return value of helpers vc_terminate_<reason>(), use them for Err case
in match statements.

Reviewed-by: Carlos Bilbao <carlos.bilbao@amd.com>
  • Loading branch information
Zildj1an authored Nov 28, 2022
2 parents 454f26e + 901b4fc commit 19d421a
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 40 deletions.
8 changes: 4 additions & 4 deletions src/bios.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ fn find_bios_guid_entry(bios_info: &mut BiosInfo, guid: &str) -> Option<u64> {

let target_guid: Uuid = match Uuid::parse_str(guid) {
Ok(g) => g,
Err(_e) => vc_terminate(SVSM_REASON_CODE_SET, SVSM_TERM_BIOS_FORMAT),
Err(_e) => vc_terminate_svsm_bios(),
};

unsafe { __find_bios_guid_entry(bios_info, target_guid, &mut avail_len, &mut p) }
Expand Down Expand Up @@ -216,7 +216,7 @@ unsafe fn __find_snp_section(bios_info: &mut BiosInfo, stype: u32, p: u64) -> Op
fn find_snp_section(bios_info: &mut BiosInfo, stype: u32) -> Option<SnpSection> {
let p: u64 = match find_bios_guid_entry(bios_info, OVMF_SNP_ENTRY_GUID) {
Some(p) => p,
None => vc_terminate(SVSM_REASON_CODE_SET, SVSM_TERM_BIOS_FORMAT),
None => vc_terminate_svsm_bios(),
};

unsafe { __find_snp_section(bios_info, stype, p) }
Expand Down Expand Up @@ -328,7 +328,7 @@ fn parse_bios_guid_table(bios_info: &mut BiosInfo) -> bool {
pub fn start_bios() {
let (bios_va, bios_size) = match fwcfg_map_bios() {
Some(t) => t,
None => vc_terminate(SVSM_REASON_CODE_SET, SVSM_TERM_FW_CFG_ERROR),
None => vc_terminate_svsm_fwcfg(),
};

let mut bios_info: BiosInfo = BiosInfo::new(bios_va, bios_size);
Expand All @@ -338,7 +338,7 @@ pub fn start_bios() {

let caa: PhysAddr = match locate_bios_ca_page(&mut bios_info) {
Some(p) => p,
None => vc_terminate(SVSM_REASON_CODE_SET, SVSM_TERM_BIOS_FORMAT),
None => vc_terminate_svsm_bios(),
};

unsafe {
Expand Down
4 changes: 2 additions & 2 deletions src/cpu/percpu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,7 @@ unsafe fn __percpu_init(init_frame: PhysFrame, init_count: u64) -> u64 {
if count != init_count {
frame = match mem_allocate_frames(count) {
Some(f) => f,
None => vc_terminate(SVSM_REASON_CODE_SET, SVSM_TERM_ENOMEM),
None => vc_terminate_svsm_enomem(),
};
} else {
frame = init_frame;
Expand Down Expand Up @@ -456,7 +456,7 @@ pub fn percpu_init() {
let init_count: u64 = PAGE_COUNT!(PERCPU_SIZE);
let init_frame: PhysFrame = match mem_allocate_frames(init_count) {
Some(f) => f,
None => vc_terminate(SVSM_REASON_CODE_SET, SVSM_TERM_ENOMEM),
None => vc_terminate_svsm_enomem(),
};
let count: u64;

Expand Down
6 changes: 3 additions & 3 deletions src/cpu/smp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ fn alloc_vmsa() -> PhysFrame {
// Allocate one frame
let mut frame: PhysFrame = match mem_allocate_frames(1) {
Some(f) => f,
None => vc_terminate(SVSM_REASON_CODE_SET, SVSM_TERM_ENOMEM),
None => vc_terminate_svsm_enomem(),
};

// VMSA pages must not be 2MB aligned, check for that
Expand All @@ -104,7 +104,7 @@ fn alloc_vmsa() -> PhysFrame {
// Allocate two frames and ...
frame = match mem_allocate_frames(2) {
Some(f) => f,
None => vc_terminate(SVSM_REASON_CODE_SET, SVSM_TERM_ENOMEM),
None => vc_terminate_svsm_enomem(),
};

// ... chose a frame which is not 2MB aligned
Expand Down Expand Up @@ -156,7 +156,7 @@ fn create_bios_vmsa() -> VirtAddr {
fn create_svsm_stack() -> VirtAddr {
let frame: PhysFrame = match mem_allocate_frames(SVSM_STACK_PAGES) {
Some(f) => f,
None => vc_terminate(SVSM_REASON_CODE_SET, SVSM_TERM_ENOMEM),
None => vc_terminate_svsm_enomem(),
};

let guard_va: VirtAddr = pgtable_pa_to_va(frame.start_address());
Expand Down
4 changes: 2 additions & 2 deletions src/cpu/tss.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const IST_STACK_PAGES: u64 = 3;
unsafe fn create_tss() -> VirtAddr {
let tss_va: VirtAddr = match mem_allocate(size_of::<TaskStateSegment>()) {
Ok(f) => f,
Err(()) => vc_terminate(SVSM_REASON_CODE_SET, SVSM_TERM_ENOMEM),
Err(()) => vc_terminate_svsm_enomem(),
};

let tss: *mut TaskStateSegment = tss_va.as_mut_ptr();
Expand All @@ -37,7 +37,7 @@ unsafe fn create_tss() -> VirtAddr {

let frame: PhysFrame = match mem_allocate_frames(IST_STACK_PAGES) {
Some(f) => f,
None => vc_terminate(SVSM_REASON_CODE_SET, SVSM_TERM_ENOMEM),
None => vc_terminate_svsm_enomem(),
};

let guard_va: VirtAddr = pgtable_pa_to_va(frame.start_address());
Expand Down
32 changes: 16 additions & 16 deletions src/cpu/vc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,85 +168,85 @@ pub fn vc_terminate(reason_set: u64, reason_code: u64) -> ! {

/// Terminate SVSM with generic SVSM reason
#[inline]
pub fn vc_terminate_svsm_general() {
pub fn vc_terminate_svsm_general() -> ! {
vc_terminate(SVSM_REASON_CODE_SET, SVSM_TERM_GENERAL);
}

/// Terminate SVSM due to lack of memory
#[inline]
pub fn vc_terminate_svsm_enomem() {
pub fn vc_terminate_svsm_enomem() -> ! {
vc_terminate(SVSM_REASON_CODE_SET, SVSM_TERM_ENOMEM);
}

/// Terminate SVSM due to firmware configuration error
#[inline]
pub fn vc_terminate_svsm_fwcfg() {
pub fn vc_terminate_svsm_fwcfg() -> ! {
vc_terminate(SVSM_REASON_CODE_SET, SVSM_TERM_FW_CFG_ERROR);
}

/// Terminate SVSM due to invalid GHCB response
#[inline]
pub fn vc_terminate_svsm_resp_invalid() {
pub fn vc_terminate_svsm_resp_invalid() -> ! {
vc_terminate(SVSM_REASON_CODE_SET, SVSM_TERM_GHCB_RESP_INVALID);
}

/// Terminate SVSM due to a page-related error
#[inline]
pub fn vc_terminate_svsm_page_err() {
pub fn vc_terminate_svsm_page_err() -> ! {
vc_terminate(SVSM_REASON_CODE_SET, SVSM_TERM_SET_PAGE_ERROR);
}

/// Terminate SVSM due to a PSC-related error
#[inline]
pub fn vc_terminate_svsm_psc() {
pub fn vc_terminate_svsm_psc() -> ! {
vc_terminate(SVSM_REASON_CODE_SET, SVSM_TERM_PSC_ERROR);
}

/// Terminate SVSM due to a BIOS-format related error
#[inline]
pub fn vc_terminate_svsm_bios() {
pub fn vc_terminate_svsm_bios() -> ! {
vc_terminate(SVSM_REASON_CODE_SET, SVSM_TERM_BIOS_FORMAT);
}

/// Terminate SVSM due to an unhandled #VC exception
#[inline]
pub fn vc_terminate_unhandled_vc() {
pub fn vc_terminate_unhandled_vc() -> ! {
vc_terminate(SVSM_REASON_CODE_SET, SVSM_TERM_UNHANDLED_VC);
}

/// Terminate SVSM with generic GHCB reason
#[inline]
pub fn vc_terminate_ghcb_general() {
pub fn vc_terminate_ghcb_general() -> ! {
vc_terminate(GHCB_REASON_CODE_SET, GHCB_TERM_GENERAL);
}

/// Terminate SVSM due to unsupported GHCB protocol
#[inline]
pub fn vc_terminate_ghcb_unsupported_protocol() {
pub fn vc_terminate_ghcb_unsupported_protocol() -> ! {
vc_terminate(GHCB_REASON_CODE_SET, GHCB_TERM_UNSUPPORTED_PROTOCOL);
}

/// Terminate SVSM due to error related with feature support
#[inline]
pub fn vc_terminate_ghcb_feature() {
pub fn vc_terminate_ghcb_feature() -> ! {
vc_terminate(GHCB_REASON_CODE_SET, GHCB_TERM_FEATURE_SUPPORT);
}

/// Terminate SVSM due to incorrect SEV features for VMPL1
#[inline]
pub fn vc_terminate_vmpl1_sev_features() {
pub fn vc_terminate_vmpl1_sev_features() -> ! {
vc_terminate(SVSM_REASON_CODE_SET, SVSM_TERM_VMPL1_SEV_FEATURES);
}

/// Terminate SVSM due to incorrect SEV features for VMPL0
#[inline]
pub fn vc_terminate_vmpl0_sev_features() {
pub fn vc_terminate_vmpl0_sev_features() -> ! {
vc_terminate(SVSM_REASON_CODE_SET, SVSM_TERM_VMPL0_SEV_FEATURES);
}

/// Terminate SVSM due to incorrect VMPL level on VMSA
#[inline]
pub fn vc_terminate_svsm_incorrect_vmpl() {
pub fn vc_terminate_svsm_incorrect_vmpl() -> ! {
vc_terminate(SVSM_REASON_CODE_SET, SVSM_TERM_INCORRECT_VMPL);
}

Expand Down Expand Up @@ -397,7 +397,7 @@ pub fn vc_get_apic_ids(bsp_apic_id: u32) -> Vec<u32> {

let frame: PhysFrame = match mem_allocate_frames(pages) {
Some(f) => f,
None => vc_terminate(SVSM_REASON_CODE_SET, SVSM_TERM_ENOMEM),
None => vc_terminate_svsm_enomem(),
};
let pa: PhysAddr = frame.start_address();
let va: VirtAddr = pgtable_pa_to_va(pa);
Expand Down Expand Up @@ -955,7 +955,7 @@ fn perform_page_state_change(ghcb: *mut Ghcb, begin: PhysFrame, end: PhysFrame,
while op.header.cur_entry <= last_entry {
vc_perform_vmgexit(ghcb, GHCB_NAE_PSC, 0, 0);
if !(*ghcb).is_sw_exit_info_2_valid() || (*ghcb).sw_exit_info_2() != 0 {
vc_terminate(SVSM_REASON_CODE_SET, SVSM_TERM_PSC_ERROR);
vc_terminate_svsm_psc();
}

(*ghcb).shared_buffer(get_bytes, size);
Expand Down
6 changes: 3 additions & 3 deletions src/mem/fwcfg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ lazy_static! {
static ref FW_CFG_DMA: SpinLock<&'static mut FwCfgDma> = {
let frame: PhysFrame = match mem_allocate_frame() {
Some(f) => f,
None => vc_terminate(SVSM_REASON_CODE_SET, SVSM_TERM_ENOMEM),
None => vc_terminate_svsm_enomem(),
};
let va: VirtAddr = pgtable_pa_to_va(frame.start_address());

Expand Down Expand Up @@ -219,11 +219,11 @@ fn find_file_selector(fname: &str) -> Option<u16> {
for f in files.iter() {
let nul: usize = match memchr(0, &f.name) {
Some(n) => n,
None => vc_terminate(SVSM_REASON_CODE_SET, SVSM_TERM_FW_CFG_ERROR),
None => vc_terminate_svsm_fwcfg(),
};
let n: &str = match core::str::from_utf8(&f.name[0..nul]) {
Ok(n) => n,
Err(_e) => vc_terminate(SVSM_REASON_CODE_SET, SVSM_TERM_FW_CFG_ERROR),
Err(_e) => vc_terminate_svsm_fwcfg(),
};

if n.eq(fname) {
Expand Down
4 changes: 2 additions & 2 deletions src/mem/ghcb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use crate::cpu::percpu::PERCPU;
use crate::cpu::percpu_count;
use crate::cpu::vc_register_ghcb;
use crate::cpu::vc_terminate;
use crate::cpu::vc_terminate_svsm_enomem;
use crate::globals::*;
use crate::mem::mem_allocate_frames;
use crate::mem::pgtable_make_pages_shared;
Expand Down Expand Up @@ -146,7 +146,7 @@ pub fn ghcb_init() {
let count: usize = percpu_count();
let frame: PhysFrame = match mem_allocate_frames(count as u64) {
Some(f) => f,
None => vc_terminate(SVSM_REASON_CODE_SET, SVSM_TERM_ENOMEM),
None => vc_terminate_svsm_enomem(),
};
let mut va: VirtAddr = pgtable_pa_to_va(frame.start_address());

Expand Down
12 changes: 4 additions & 8 deletions src/mem/pgtable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use x86_64::addr::{PhysAddr, VirtAddr};
use x86_64::registers::control::{Cr3, Cr3Flags};
use x86_64::structures::paging::frame::PhysFrame;
use x86_64::structures::paging::mapper::{
FlagUpdateError, MapToError, MapperFlush, TranslateResult, UnmapError,
FlagUpdateError, MapToError, MapperFlush, TranslateResult,
};
use x86_64::structures::paging::page::Page;
use x86_64::structures::paging::page::{PageRange, Size4KiB};
Expand Down Expand Up @@ -70,14 +70,10 @@ fn remap_page(page: Page, page_type: PageType, flush: bool) {
let mut allocator: PageTableAllocator = PageTableAllocator::new();

unsafe {
let mut pa: PhysAddr = PhysAddr::new(0);

let result: Result<(PhysFrame<Size4KiB>, MapperFlush<Size4KiB>), UnmapError> =
PGTABLE.lock().unmap(page);
match result {
Ok(r) => pa = r.0.start_address(),
let pa: PhysAddr = match PGTABLE.lock().unmap(page) {
Ok(r) => r.0.start_address(),
Err(_e) => vc_terminate_svsm_page_err(),
}
};

let map_pa: PhysAddr;
if page_type == PageType::Private {
Expand Down

0 comments on commit 19d421a

Please sign in to comment.