Skip to content

Commit

Permalink
fw_meta: refactor parse_fw_meta_data() to take a VirtAddr
Browse files Browse the repository at this point in the history
Refactor the public function parse_fw_meta_data() so that the caller
can provide an arbitrary address where the firmware starts. This will
help building a fuzzing harness around this interface.

Signed-off-by: Carlos López <[email protected]>
  • Loading branch information
00xc committed Oct 11, 2023
1 parent c1aa81b commit 38e41aa
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 11 deletions.
14 changes: 5 additions & 9 deletions src/fw_meta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@

extern crate alloc;

use crate::address::{Address, PhysAddr};
use crate::address::{Address, PhysAddr, VirtAddr};
use crate::cpu::percpu::this_cpu_mut;
use crate::error::SvsmError;
use crate::mm::PerCPUPageMappingGuard;
use crate::mm::SIZE_1G;
use crate::sev::ghcb::PageStateChangeOp;
use crate::sev::{pvalidate, rmp_adjust, PvalidateOp, RMPFlags};
use crate::types::{PageSize, PAGE_SIZE};
Expand Down Expand Up @@ -245,15 +244,12 @@ fn find_table<'a>(uuid: &Uuid, mem: &'a [u8]) -> Option<&'a [u8]> {
None
}

pub fn parse_fw_meta_data() -> Result<SevFWMetaData, SvsmError> {
/// Parse the firmware metadata located at the memory page starting at `vstart`.
pub fn parse_fw_meta_data(vstart: VirtAddr) -> Result<SevFWMetaData, SvsmError> {
let mut meta_data = SevFWMetaData::new();
// Map meta-data location, it starts at 32 bytes below 4GiB
let pstart = PhysAddr::from((4 * SIZE_1G) - PAGE_SIZE);
let guard = PerCPUPageMappingGuard::create_4k(pstart)?;
let vstart = guard.virt_addr();

// Safety: RawMetaBuffer has a size of one page and it has no invalid
// representations.
// Safety: the caller must guarantee that the region starting at `vstart`
// has a size of one page. `RawMetaBuffer` has no invalid representations.
let raw_meta = unsafe { &*vstart.as_ptr::<RawMetaBuffer>() };

// Check the UUID
Expand Down
12 changes: 10 additions & 2 deletions src/svsm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ use svsm::mm::alloc::{memory_info, print_memory_info, root_mem_init};
use svsm::mm::memory::init_memory_map;
use svsm::mm::pagetable::paging_init;
use svsm::mm::virtualrange::virt_log_usage;
use svsm::mm::{init_kernel_mapping_info, PerCPUPageMappingGuard};
use svsm::mm::{init_kernel_mapping_info, PerCPUPageMappingGuard, SIZE_1G};
use svsm::requests::{request_loop, update_mappings};
use svsm::serial::SerialPort;
use svsm::serial::SERIAL_PORT;
Expand Down Expand Up @@ -310,6 +310,14 @@ fn mapping_info_init(launch_info: &KernelLaunchInfo) {
);
}

fn map_and_parse_fw_meta() -> Result<SevFWMetaData, SvsmError> {
// Map meta-data location, it starts at 32 bytes below 4GiB
let pstart = PhysAddr::from((4 * SIZE_1G) - PAGE_SIZE);
let guard = PerCPUPageMappingGuard::create_4k(pstart)?;
let vstart = guard.virt_addr();
parse_fw_meta_data(vstart)
}

#[no_mangle]
pub extern "C" fn svsm_start(li: &KernelLaunchInfo, vb_addr: usize) {
let launch_info: KernelLaunchInfo = *li;
Expand Down Expand Up @@ -445,7 +453,7 @@ pub extern "C" fn svsm_main() {

start_secondary_cpus(&cpus);

let fw_meta = parse_fw_meta_data()
let fw_meta = map_and_parse_fw_meta()
.unwrap_or_else(|e| panic!("Failed to parse FW SEV meta-data: {:#?}", e));

print_fw_meta(&fw_meta);
Expand Down

0 comments on commit 38e41aa

Please sign in to comment.