]> git.baikalelectronics.ru Git - kernel.git/commitdiff
KVM: PPC: Book3S HV: Implement H_TLB_INVALIDATE hcall
authorSuraj Jitindar Singh <sjitindarsingh@gmail.com>
Mon, 8 Oct 2018 05:31:09 +0000 (16:31 +1100)
committerMichael Ellerman <mpe@ellerman.id.au>
Tue, 9 Oct 2018 05:04:27 +0000 (16:04 +1100)
When running a nested (L2) guest the guest (L1) hypervisor will use
the H_TLB_INVALIDATE hcall when it needs to change the partition
scoped page tables or the partition table which it manages.  It will
use this hcall in the situations where it would use a partition-scoped
tlbie instruction if it were running in hypervisor mode.

The H_TLB_INVALIDATE hcall can invalidate different scopes:

Invalidate TLB for a given target address:
- This invalidates a single L2 -> L1 pte
- We need to invalidate any L2 -> L0 shadow_pgtable ptes which map the L2
  address space which is being invalidated. This is because a single
  L2 -> L1 pte may have been mapped with more than one pte in the
  L2 -> L0 page tables.

Invalidate the entire TLB for a given LPID or for all LPIDs:
- Invalidate the entire shadow_pgtable for a given nested guest, or
  for all nested guests.

Invalidate the PWC (page walk cache) for a given LPID or for all LPIDs:
- We don't cache the PWC, so nothing to do.

Invalidate the entire TLB, PWC and partition table for a given/all LPIDs:
- Here we re-read the partition table entry and remove the nested state
  for any nested guest for which the first doubleword of the partition
  table entry is now zero.

The H_TLB_INVALIDATE hcall takes as parameters the tlbie instruction
word (of which only the RIC, PRS and R fields are used), the rS value
(giving the lpid, where required) and the rB value (giving the IS, AP
and EPN values).

[paulus@ozlabs.org - adapted to having the partition table in guest
memory, added the H_TLB_INVALIDATE implementation, removed tlbie
instruction emulation, reworded the commit message.]

Reviewed-by: David Gibson <david@gibson.dropbear.id.au>
Signed-off-by: Suraj Jitindar Singh <sjitindarsingh@gmail.com>
Signed-off-by: Paul Mackerras <paulus@ozlabs.org>
Signed-off-by: Michael Ellerman <mpe@ellerman.id.au>
arch/powerpc/include/asm/book3s/64/mmu-hash.h
arch/powerpc/include/asm/kvm_book3s.h
arch/powerpc/include/asm/ppc-opcode.h
arch/powerpc/kvm/book3s_emulate.c
arch/powerpc/kvm/book3s_hv.c
arch/powerpc/kvm/book3s_hv_nested.c

index b3520b549cba47087c6ec79075c25bf308585211..66db23e2f4dc0c9e4a4318a8f5752c4776adc3f7 100644 (file)
@@ -203,6 +203,18 @@ static inline unsigned int mmu_psize_to_shift(unsigned int mmu_psize)
        BUG();
 }
 
+static inline unsigned int ap_to_shift(unsigned long ap)
+{
+       int psize;
+
+       for (psize = 0; psize < MMU_PAGE_COUNT; psize++) {
+               if (mmu_psize_defs[psize].ap == ap)
+                       return mmu_psize_defs[psize].shift;
+       }
+
+       return -1;
+}
+
 static inline unsigned long get_sllp_encoding(int psize)
 {
        unsigned long sllp;
index d7aeb6f701a65127d61a9d80f295cba1080fd628..09f8e9ba69bc788c65cac1d043dcd3c4bc31f1ca 100644 (file)
@@ -301,6 +301,7 @@ long kvmhv_set_partition_table(struct kvm_vcpu *vcpu);
 void kvmhv_set_ptbl_entry(unsigned int lpid, u64 dw0, u64 dw1);
 void kvmhv_release_all_nested(struct kvm *kvm);
 long kvmhv_enter_nested_guest(struct kvm_vcpu *vcpu);
+long kvmhv_do_nested_tlbie(struct kvm_vcpu *vcpu);
 int kvmhv_run_single_vcpu(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu,
                          u64 time_limit, unsigned long lpcr);
 void kvmhv_save_hv_regs(struct kvm_vcpu *vcpu, struct hv_guest_state *hr);
index 665af14850e4249c01153c200a85eff609f1a09d..6093bc8f74e518bf225c014c25521c8a515ba013 100644 (file)
 #define OP_31_XOP_LHZUX     311
 #define OP_31_XOP_MSGSNDP   142
 #define OP_31_XOP_MSGCLRP   174
+#define OP_31_XOP_TLBIE     306
 #define OP_31_XOP_MFSPR     339
 #define OP_31_XOP_LWAX      341
 #define OP_31_XOP_LHAX      343
index 2654df220d05487cfd27394b0bb8360e2ab11c20..8c7e933e942e5b56c42faf9eacdf08d5a183cde4 100644 (file)
@@ -36,7 +36,6 @@
 #define OP_31_XOP_MTSR         210
 #define OP_31_XOP_MTSRIN       242
 #define OP_31_XOP_TLBIEL       274
-#define OP_31_XOP_TLBIE                306
 /* Opcode is officially reserved, reuse it as sc 1 when sc 1 doesn't trap */
 #define OP_31_XOP_FAKE_SC1     308
 #define OP_31_XOP_SLBMTE       402
index 6e69b4de9a9adc0fcc373464b6f2eb7922f7e7b9..2d7494140de773cfe5ca5527b23040ec1dd7f344 100644 (file)
@@ -974,6 +974,9 @@ int kvmppc_pseries_do_hcall(struct kvm_vcpu *vcpu)
                break;
        case H_TLB_INVALIDATE:
                ret = H_FUNCTION;
+               if (!vcpu->kvm->arch.nested_enable)
+                       break;
+               ret = kvmhv_do_nested_tlbie(vcpu);
                break;
 
        default:
index 3fa676b2acd995a7c33e0c898e338933513717a5..c83c13d707e2d369c11c8e97aeba670d8e7810f9 100644 (file)
@@ -486,7 +486,7 @@ void kvmhv_release_all_nested(struct kvm *kvm)
 }
 
 /* caller must hold gp->tlb_lock */
-void kvmhv_flush_nested(struct kvm_nested_guest *gp)
+static void kvmhv_flush_nested(struct kvm_nested_guest *gp)
 {
        struct kvm *kvm = gp->l1_host;
 
@@ -694,6 +694,200 @@ static bool kvmhv_invalidate_shadow_pte(struct kvm_vcpu *vcpu,
        return ret;
 }
 
+static inline int get_ric(unsigned int instr)
+{
+       return (instr >> 18) & 0x3;
+}
+
+static inline int get_prs(unsigned int instr)
+{
+       return (instr >> 17) & 0x1;
+}
+
+static inline int get_r(unsigned int instr)
+{
+       return (instr >> 16) & 0x1;
+}
+
+static inline int get_lpid(unsigned long r_val)
+{
+       return r_val & 0xffffffff;
+}
+
+static inline int get_is(unsigned long r_val)
+{
+       return (r_val >> 10) & 0x3;
+}
+
+static inline int get_ap(unsigned long r_val)
+{
+       return (r_val >> 5) & 0x7;
+}
+
+static inline long get_epn(unsigned long r_val)
+{
+       return r_val >> 12;
+}
+
+static int kvmhv_emulate_tlbie_tlb_addr(struct kvm_vcpu *vcpu, int lpid,
+                                       int ap, long epn)
+{
+       struct kvm *kvm = vcpu->kvm;
+       struct kvm_nested_guest *gp;
+       long npages;
+       int shift, shadow_shift;
+       unsigned long addr;
+
+       shift = ap_to_shift(ap);
+       addr = epn << 12;
+       if (shift < 0)
+               /* Invalid ap encoding */
+               return -EINVAL;
+
+       addr &= ~((1UL << shift) - 1);
+       npages = 1UL << (shift - PAGE_SHIFT);
+
+       gp = kvmhv_get_nested(kvm, lpid, false);
+       if (!gp) /* No such guest -> nothing to do */
+               return 0;
+       mutex_lock(&gp->tlb_lock);
+
+       /* There may be more than one host page backing this single guest pte */
+       do {
+               kvmhv_invalidate_shadow_pte(vcpu, gp, addr, &shadow_shift);
+
+               npages -= 1UL << (shadow_shift - PAGE_SHIFT);
+               addr += 1UL << shadow_shift;
+       } while (npages > 0);
+
+       mutex_unlock(&gp->tlb_lock);
+       kvmhv_put_nested(gp);
+       return 0;
+}
+
+static void kvmhv_emulate_tlbie_lpid(struct kvm_vcpu *vcpu,
+                                    struct kvm_nested_guest *gp, int ric)
+{
+       struct kvm *kvm = vcpu->kvm;
+
+       mutex_lock(&gp->tlb_lock);
+       switch (ric) {
+       case 0:
+               /* Invalidate TLB */
+               spin_lock(&kvm->mmu_lock);
+               kvmppc_free_pgtable_radix(kvm, gp->shadow_pgtable,
+                                         gp->shadow_lpid);
+               radix__flush_tlb_lpid(gp->shadow_lpid);
+               spin_unlock(&kvm->mmu_lock);
+               break;
+       case 1:
+               /*
+                * Invalidate PWC
+                * We don't cache this -> nothing to do
+                */
+               break;
+       case 2:
+               /* Invalidate TLB, PWC and caching of partition table entries */
+               kvmhv_flush_nested(gp);
+               break;
+       default:
+               break;
+       }
+       mutex_unlock(&gp->tlb_lock);
+}
+
+static void kvmhv_emulate_tlbie_all_lpid(struct kvm_vcpu *vcpu, int ric)
+{
+       struct kvm *kvm = vcpu->kvm;
+       struct kvm_nested_guest *gp;
+       int i;
+
+       spin_lock(&kvm->mmu_lock);
+       for (i = 0; i <= kvm->arch.max_nested_lpid; i++) {
+               gp = kvm->arch.nested_guests[i];
+               if (gp) {
+                       spin_unlock(&kvm->mmu_lock);
+                       kvmhv_emulate_tlbie_lpid(vcpu, gp, ric);
+                       spin_lock(&kvm->mmu_lock);
+               }
+       }
+       spin_unlock(&kvm->mmu_lock);
+}
+
+static int kvmhv_emulate_priv_tlbie(struct kvm_vcpu *vcpu, unsigned int instr,
+                                   unsigned long rsval, unsigned long rbval)
+{
+       struct kvm *kvm = vcpu->kvm;
+       struct kvm_nested_guest *gp;
+       int r, ric, prs, is, ap;
+       int lpid;
+       long epn;
+       int ret = 0;
+
+       ric = get_ric(instr);
+       prs = get_prs(instr);
+       r = get_r(instr);
+       lpid = get_lpid(rsval);
+       is = get_is(rbval);
+
+       /*
+        * These cases are invalid and are not handled:
+        * r   != 1 -> Only radix supported
+        * prs == 1 -> Not HV privileged
+        * ric == 3 -> No cluster bombs for radix
+        * is  == 1 -> Partition scoped translations not associated with pid
+        * (!is) && (ric == 1 || ric == 2) -> Not supported by ISA
+        */
+       if ((!r) || (prs) || (ric == 3) || (is == 1) ||
+           ((!is) && (ric == 1 || ric == 2)))
+               return -EINVAL;
+
+       switch (is) {
+       case 0:
+               /*
+                * We know ric == 0
+                * Invalidate TLB for a given target address
+                */
+               epn = get_epn(rbval);
+               ap = get_ap(rbval);
+               ret = kvmhv_emulate_tlbie_tlb_addr(vcpu, lpid, ap, epn);
+               break;
+       case 2:
+               /* Invalidate matching LPID */
+               gp = kvmhv_get_nested(kvm, lpid, false);
+               if (gp) {
+                       kvmhv_emulate_tlbie_lpid(vcpu, gp, ric);
+                       kvmhv_put_nested(gp);
+               }
+               break;
+       case 3:
+               /* Invalidate ALL LPIDs */
+               kvmhv_emulate_tlbie_all_lpid(vcpu, ric);
+               break;
+       default:
+               ret = -EINVAL;
+               break;
+       }
+
+       return ret;
+}
+
+/*
+ * This handles the H_TLB_INVALIDATE hcall.
+ * Parameters are (r4) tlbie instruction code, (r5) rS contents,
+ * (r6) rB contents.
+ */
+long kvmhv_do_nested_tlbie(struct kvm_vcpu *vcpu)
+{
+       int ret;
+
+       ret = kvmhv_emulate_priv_tlbie(vcpu, kvmppc_get_gpr(vcpu, 4),
+                       kvmppc_get_gpr(vcpu, 5), kvmppc_get_gpr(vcpu, 6));
+       if (ret)
+               return H_PARAMETER;
+       return H_SUCCESS;
+}
+
 /* Used to convert a nested guest real address to a L1 guest real address */
 static int kvmhv_translate_addr_nested(struct kvm_vcpu *vcpu,
                                       struct kvm_nested_guest *gp,