KVM: arm64: Expose unshare hypercall to the host
authorWill Deacon <will@kernel.org>
Wed, 15 Dec 2021 16:12:30 +0000 (16:12 +0000)
committerMarc Zyngier <maz@kernel.org>
Thu, 16 Dec 2021 12:58:57 +0000 (12:58 +0000)
Introduce an unshare hypercall which can be used to unmap memory from
the hypervisor stage-1 in nVHE protected mode. This will be useful to
update the EL2 ownership state of pages during guest teardown, and
avoids keeping dangling mappings to unreferenced portions of memory.

Signed-off-by: Will Deacon <will@kernel.org>
Signed-off-by: Quentin Perret <qperret@google.com>
Signed-off-by: Marc Zyngier <maz@kernel.org>
Link: https://lore.kernel.org/r/20211215161232.1480836-14-qperret@google.com
arch/arm64/include/asm/kvm_asm.h
arch/arm64/kvm/hyp/include/nvhe/mem_protect.h
arch/arm64/kvm/hyp/nvhe/hyp-main.c
arch/arm64/kvm/hyp/nvhe/mem_protect.c

index 50d5e4d..d5b0386 100644 (file)
@@ -63,6 +63,7 @@ enum __kvm_host_smccc_func {
 
        /* Hypercalls available after pKVM finalisation */
        __KVM_HOST_SMCCC_FUNC___pkvm_host_share_hyp,
+       __KVM_HOST_SMCCC_FUNC___pkvm_host_unshare_hyp,
        __KVM_HOST_SMCCC_FUNC___kvm_adjust_pc,
        __KVM_HOST_SMCCC_FUNC___kvm_vcpu_run,
        __KVM_HOST_SMCCC_FUNC___kvm_flush_vm_context,
index 5644558..80e9983 100644 (file)
@@ -55,6 +55,7 @@ extern const u8 pkvm_hyp_id;
 
 int __pkvm_prot_finalize(void);
 int __pkvm_host_share_hyp(u64 pfn);
+int __pkvm_host_unshare_hyp(u64 pfn);
 
 bool addr_is_memory(phys_addr_t phys);
 int host_stage2_idmap_locked(phys_addr_t addr, u64 size, enum kvm_pgtable_prot prot);
index b096bf0..5e2197d 100644 (file)
@@ -147,6 +147,13 @@ static void handle___pkvm_host_share_hyp(struct kvm_cpu_context *host_ctxt)
        cpu_reg(host_ctxt, 1) = __pkvm_host_share_hyp(pfn);
 }
 
+static void handle___pkvm_host_unshare_hyp(struct kvm_cpu_context *host_ctxt)
+{
+       DECLARE_REG(u64, pfn, host_ctxt, 1);
+
+       cpu_reg(host_ctxt, 1) = __pkvm_host_unshare_hyp(pfn);
+}
+
 static void handle___pkvm_create_private_mapping(struct kvm_cpu_context *host_ctxt)
 {
        DECLARE_REG(phys_addr_t, phys, host_ctxt, 1);
@@ -184,6 +191,7 @@ static const hcall_t host_hcall[] = {
        HANDLE_FUNC(__pkvm_prot_finalize),
 
        HANDLE_FUNC(__pkvm_host_share_hyp),
+       HANDLE_FUNC(__pkvm_host_unshare_hyp),
        HANDLE_FUNC(__kvm_adjust_pc),
        HANDLE_FUNC(__kvm_vcpu_run),
        HANDLE_FUNC(__kvm_flush_vm_context),
index 06973a9..33c105d 100644 (file)
@@ -768,3 +768,36 @@ int __pkvm_host_share_hyp(u64 pfn)
 
        return ret;
 }
+
+int __pkvm_host_unshare_hyp(u64 pfn)
+{
+       int ret;
+       u64 host_addr = hyp_pfn_to_phys(pfn);
+       u64 hyp_addr = (u64)__hyp_va(host_addr);
+       struct pkvm_mem_share share = {
+               .tx     = {
+                       .nr_pages       = 1,
+                       .initiator      = {
+                               .id     = PKVM_ID_HOST,
+                               .addr   = host_addr,
+                               .host   = {
+                                       .completer_addr = hyp_addr,
+                               },
+                       },
+                       .completer      = {
+                               .id     = PKVM_ID_HYP,
+                       },
+               },
+               .completer_prot = PAGE_HYP,
+       };
+
+       host_lock_component();
+       hyp_lock_component();
+
+       ret = do_unshare(&share);
+
+       hyp_unlock_component();
+       host_unlock_component();
+
+       return ret;
+}