KVM: PPC: Book3S HV: Allow passthrough of an emulated device to an L2 guest
authorSuraj Jitindar Singh <sjitindarsingh@gmail.com>
Fri, 14 Dec 2018 05:29:08 +0000 (16:29 +1100)
committerPaul Mackerras <paulus@ozlabs.org>
Mon, 17 Dec 2018 00:33:50 +0000 (11:33 +1100)
Allow for a device which is being emulated at L0 (the host) for an L1
guest to be passed through to a nested (L2) guest.

The existing kvmppc_hv_emulate_mmio function can be used here. The main
challenge is that for a load the result must be stored into the L2 gpr,
not an L1 gpr as would normally be the case after going out to qemu to
complete the operation. This presents a challenge as at this point the
L2 gpr state has been written back into L1 memory.

To work around this we store the address in L1 memory of the L2 gpr
where the result of the load is to be stored and use the new io_gpr
value KVM_MMIO_REG_NESTED_GPR to indicate that this is a nested load for
which completion must be done when returning back into the kernel. Then
in kvmppc_complete_mmio_load() the resultant value is written into L1
memory at the location of the indicated L2 gpr.

Note that we don't currently let an L1 guest emulate a device for an L2
guest which is then passed through to an L3 guest.

Signed-off-by: Suraj Jitindar Singh <sjitindarsingh@gmail.com>
Signed-off-by: Paul Mackerras <paulus@ozlabs.org>
arch/powerpc/include/asm/kvm_book3s.h
arch/powerpc/include/asm/kvm_host.h
arch/powerpc/kvm/book3s_hv.c
arch/powerpc/kvm/book3s_hv_nested.c
arch/powerpc/kvm/powerpc.c

index b25a3f18b301cfbaf57e4c54b79d0c86609efb00..616b28802a198056b17ab25d8c170cc2e4ff903f 100644 (file)
@@ -310,7 +310,7 @@ int kvmhv_run_single_vcpu(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu,
 void kvmhv_save_hv_regs(struct kvm_vcpu *vcpu, struct hv_guest_state *hr);
 void kvmhv_restore_hv_return_state(struct kvm_vcpu *vcpu,
                                   struct hv_guest_state *hr);
-long int kvmhv_nested_page_fault(struct kvm_vcpu *vcpu);
+long int kvmhv_nested_page_fault(struct kvm_run *run, struct kvm_vcpu *vcpu);
 
 void kvmppc_giveup_fac(struct kvm_vcpu *vcpu, ulong fac);
 
index fac6f631ed29076ad5d5599d75c867d1325aede6..7a2483a139cffec24401255980101dac65f31961 100644 (file)
@@ -793,6 +793,7 @@ struct kvm_vcpu_arch {
        /* For support of nested guests */
        struct kvm_nested_guest *nested;
        u32 nested_vcpu_id;
+       gpa_t nested_io_gpr;
 #endif
 
 #ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
@@ -827,6 +828,8 @@ struct kvm_vcpu_arch {
 #define KVM_MMIO_REG_FQPR      0x00c0
 #define KVM_MMIO_REG_VSX       0x0100
 #define KVM_MMIO_REG_VMX       0x0180
+#define KVM_MMIO_REG_NESTED_GPR        0xffc0
+
 
 #define __KVM_HAVE_ARCH_WQP
 #define __KVM_HAVE_CREATE_DEVICE
index cef57b68dda1862603f9821fc021ca13638e8ea2..ca7de15f4a70bec7b049f76f56565ec09a2785d9 100644 (file)
@@ -985,6 +985,10 @@ int kvmppc_pseries_do_hcall(struct kvm_vcpu *vcpu)
                        kvmppc_set_gpr(vcpu, 3, 0);
                        vcpu->arch.hcall_needed = 0;
                        return -EINTR;
+               } else if (ret == H_TOO_HARD) {
+                       kvmppc_set_gpr(vcpu, 3, 0);
+                       vcpu->arch.hcall_needed = 0;
+                       return RESUME_HOST;
                }
                break;
        case H_TLB_INVALIDATE:
@@ -1336,7 +1340,7 @@ static int kvmppc_handle_exit_hv(struct kvm_run *run, struct kvm_vcpu *vcpu,
        return r;
 }
 
-static int kvmppc_handle_nested_exit(struct kvm_vcpu *vcpu)
+static int kvmppc_handle_nested_exit(struct kvm_run *run, struct kvm_vcpu *vcpu)
 {
        int r;
        int srcu_idx;
@@ -1394,7 +1398,7 @@ static int kvmppc_handle_nested_exit(struct kvm_vcpu *vcpu)
         */
        case BOOK3S_INTERRUPT_H_DATA_STORAGE:
                srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
-               r = kvmhv_nested_page_fault(vcpu);
+               r = kvmhv_nested_page_fault(run, vcpu);
                srcu_read_unlock(&vcpu->kvm->srcu, srcu_idx);
                break;
        case BOOK3S_INTERRUPT_H_INST_STORAGE:
@@ -1404,7 +1408,7 @@ static int kvmppc_handle_nested_exit(struct kvm_vcpu *vcpu)
                if (vcpu->arch.shregs.msr & HSRR1_HISI_WRITE)
                        vcpu->arch.fault_dsisr |= DSISR_ISSTORE;
                srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
-               r = kvmhv_nested_page_fault(vcpu);
+               r = kvmhv_nested_page_fault(run, vcpu);
                srcu_read_unlock(&vcpu->kvm->srcu, srcu_idx);
                break;
 
@@ -4059,7 +4063,7 @@ int kvmhv_run_single_vcpu(struct kvm_run *kvm_run,
                if (!nested)
                        r = kvmppc_handle_exit_hv(kvm_run, vcpu, current);
                else
-                       r = kvmppc_handle_nested_exit(vcpu);
+                       r = kvmppc_handle_nested_exit(kvm_run, vcpu);
        }
        vcpu->arch.ret = r;
 
index 6f9e1ad6052798265e50f3a7f7d24091f359bcb2..aeb0bb2075fad308063dd5b41a24ab71425e32ee 100644 (file)
@@ -195,6 +195,26 @@ void kvmhv_restore_hv_return_state(struct kvm_vcpu *vcpu,
        vcpu->arch.ppr = hr->ppr;
 }
 
+static void kvmhv_nested_mmio_needed(struct kvm_vcpu *vcpu, u64 regs_ptr)
+{
+       /* No need to reflect the page fault to L1, we've handled it */
+       vcpu->arch.trap = 0;
+
+       /*
+        * Since the L2 gprs have already been written back into L1 memory when
+        * we complete the mmio, store the L1 memory location of the L2 gpr
+        * being loaded into by the mmio so that the loaded value can be
+        * written there in kvmppc_complete_mmio_load()
+        */
+       if (((vcpu->arch.io_gpr & KVM_MMIO_REG_EXT_MASK) == KVM_MMIO_REG_GPR)
+           && (vcpu->mmio_is_write == 0)) {
+               vcpu->arch.nested_io_gpr = (gpa_t) regs_ptr +
+                                          offsetof(struct pt_regs,
+                                                   gpr[vcpu->arch.io_gpr]);
+               vcpu->arch.io_gpr = KVM_MMIO_REG_NESTED_GPR;
+       }
+}
+
 long kvmhv_enter_nested_guest(struct kvm_vcpu *vcpu)
 {
        long int err, r;
@@ -316,6 +336,11 @@ long kvmhv_enter_nested_guest(struct kvm_vcpu *vcpu)
        if (r == -EINTR)
                return H_INTERRUPT;
 
+       if (vcpu->mmio_needed) {
+               kvmhv_nested_mmio_needed(vcpu, regs_ptr);
+               return H_TOO_HARD;
+       }
+
        return vcpu->arch.trap;
 }
 
@@ -1100,7 +1125,8 @@ static inline int kvmppc_radix_shift_to_level(int shift)
 }
 
 /* called with gp->tlb_lock held */
-static long int __kvmhv_nested_page_fault(struct kvm_vcpu *vcpu,
+static long int __kvmhv_nested_page_fault(struct kvm_run *run,
+                                         struct kvm_vcpu *vcpu,
                                          struct kvm_nested_guest *gp)
 {
        struct kvm *kvm = vcpu->kvm;
@@ -1181,9 +1207,14 @@ static long int __kvmhv_nested_page_fault(struct kvm_vcpu *vcpu,
                        kvmppc_core_queue_data_storage(vcpu, ea, dsisr);
                        return RESUME_GUEST;
                }
-               /* passthrough of emulated MMIO case... */
-               pr_err("emulated MMIO passthrough?\n");
-               return -EINVAL;
+
+               /* passthrough of emulated MMIO case */
+               if (kvmhv_on_pseries()) {
+                       pr_err("emulated MMIO passthrough?\n");
+                       return -EINVAL;
+               }
+
+               return kvmppc_hv_emulate_mmio(run, vcpu, gpa, ea, writing);
        }
        if (memslot->flags & KVM_MEM_READONLY) {
                if (writing) {
@@ -1265,13 +1296,13 @@ static long int __kvmhv_nested_page_fault(struct kvm_vcpu *vcpu,
        return RESUME_GUEST;
 }
 
-long int kvmhv_nested_page_fault(struct kvm_vcpu *vcpu)
+long int kvmhv_nested_page_fault(struct kvm_run *run, struct kvm_vcpu *vcpu)
 {
        struct kvm_nested_guest *gp = vcpu->arch.nested;
        long int ret;
 
        mutex_lock(&gp->tlb_lock);
-       ret = __kvmhv_nested_page_fault(vcpu, gp);
+       ret = __kvmhv_nested_page_fault(run, vcpu, gp);
        mutex_unlock(&gp->tlb_lock);
        return ret;
 }
index ec9cbf9db3645e5526fa68e5c41a8c22586172da..db61096ffcf7c69238b05f978fe280abe08dc8bc 100644 (file)
@@ -1208,6 +1208,14 @@ static void kvmppc_complete_mmio_load(struct kvm_vcpu *vcpu,
                                KVMPPC_VMX_COPY_BYTE)
                        kvmppc_set_vmx_byte(vcpu, gpr);
                break;
+#endif
+#ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
+       case KVM_MMIO_REG_NESTED_GPR:
+               if (kvmppc_need_byteswap(vcpu))
+                       gpr = swab64(gpr);
+               kvm_vcpu_write_guest(vcpu, vcpu->arch.nested_io_gpr, &gpr,
+                                    sizeof(gpr));
+               break;
 #endif
        default:
                BUG();