Skip to content

Commit

Permalink
ghcb: move shutdown code into Drop impl
Browse files Browse the repository at this point in the history
Now that the shutdown code is only called from the Drop impl we might
as well move it in there. This also makes it impossible to call
shutdown more than once (or to call shutdown and the Drop the
GhcbPage).

Signed-off-by: Tom Dohrmann <[email protected]>
  • Loading branch information
Freax13 committed Aug 30, 2024
1 parent 668512f commit a084a9a
Showing 1 changed file with 22 additions and 25 deletions.
47 changes: 22 additions & 25 deletions kernel/src/sev/ghcb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,28 @@ impl GhcbPage {

impl Drop for GhcbPage {
fn drop(&mut self) {
self.0.shutdown().expect("Could not shut down GHCB");
let vaddr = self.0.vaddr();
let paddr = virt_to_phys(vaddr);

// Re-encrypt page
this_cpu()
.get_pgtable()
.set_encrypted_4k(vaddr)
.expect("Could not re-encrypt page");

// Unregister GHCB PA
register_ghcb_gpa_msr(PhysAddr::null()).expect("Could not unregister GHCB");

// Ask the hypervisor to change the page back to the private page state.
validate_page_msr(paddr).expect("Could not change page state");

// Make page guest-valid
pvalidate(vaddr, PageSize::Regular, PvalidateOp::Valid).expect("Could not pvalidate page");

// Needs guarding for Stage2 GHCB
if valid_bitmap_valid_addr(paddr) {
valid_bitmap_set_valid_4k(paddr);
}
}
}

Expand Down Expand Up @@ -313,30 +334,6 @@ impl GHCB {
Ok(register_ghcb_gpa_msr(paddr)?)
}

pub fn shutdown(&self) -> Result<(), SvsmError> {
let vaddr = VirtAddr::from(ptr::from_ref(self));
let paddr = virt_to_phys(vaddr);

// Re-encrypt page
this_cpu().get_pgtable().set_encrypted_4k(vaddr)?;

// Unregister GHCB PA
register_ghcb_gpa_msr(PhysAddr::null())?;

// Make page guest-invalid
validate_page_msr(paddr)?;

// Make page guest-valid
pvalidate(vaddr, PageSize::Regular, PvalidateOp::Valid)?;

// Needs guarding for Stage2 GHCB
if valid_bitmap_valid_addr(paddr) {
valid_bitmap_set_valid_4k(paddr);
}

Ok(())
}

pub fn clear(&self) {
// Clear valid bitmap
self.valid_bitmap[0].store(0, Ordering::SeqCst);
Expand Down

0 comments on commit a084a9a

Please sign in to comment.