From a084a9a8dba4c26f8f51765958fa71daace2cc57 Mon Sep 17 00:00:00 2001 From: Tom Dohrmann Date: Fri, 30 Aug 2024 07:02:05 +0000 Subject: [PATCH] ghcb: move shutdown code into Drop impl 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 --- kernel/src/sev/ghcb.rs | 47 ++++++++++++++++++++---------------------- 1 file changed, 22 insertions(+), 25 deletions(-) diff --git a/kernel/src/sev/ghcb.rs b/kernel/src/sev/ghcb.rs index 34113624d..7b3dc3a26 100644 --- a/kernel/src/sev/ghcb.rs +++ b/kernel/src/sev/ghcb.rs @@ -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); + } } } @@ -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);