KVM: arm/arm64: Allow user injection of external data aborts
authorChristoffer Dall <christoffer.dall@arm.com>
Fri, 11 Oct 2019 11:07:06 +0000 (13:07 +0200)
committerMarc Zyngier <maz@kernel.org>
Mon, 21 Oct 2019 17:59:51 +0000 (18:59 +0100)
In some scenarios, such as buggy guest or incorrect configuration of the
VMM and firmware description data, userspace will detect a memory access
to a portion of the IPA, which is not mapped to any MMIO region.

For this purpose, the appropriate action is to inject an external abort
to the guest.  The kernel already has functionality to inject an
external abort, but we need to wire up a signal from user space that
lets user space tell the kernel to do this.

It turns out, we already have the set event functionality which we can
perfectly reuse for this.

Signed-off-by: Christoffer Dall <christoffer.dall@arm.com>
Signed-off-by: Marc Zyngier <maz@kernel.org>
Documentation/virt/kvm/api.txt
arch/arm/include/uapi/asm/kvm.h
arch/arm/kvm/guest.c
arch/arm64/include/uapi/asm/kvm.h
arch/arm64/kvm/guest.c
arch/arm64/kvm/inject_fault.c
include/uapi/linux/kvm.h
virt/kvm/arm/arm.c

index 7403f15657c2d238ed03bcc29eee51697bdebd6e..bd29d44af32ba8cd864d0c0d9de34227c6ace860 100644 (file)
@@ -1002,12 +1002,18 @@ Specifying exception.has_esr on a system that does not support it will return
 -EINVAL. Setting anything other than the lower 24bits of exception.serror_esr
 will return -EINVAL.
 
+It is not possible to read back a pending external abort (injected via
+KVM_SET_VCPU_EVENTS or otherwise) because such an exception is always delivered
+directly to the virtual CPU).
+
+
 struct kvm_vcpu_events {
        struct {
                __u8 serror_pending;
                __u8 serror_has_esr;
+               __u8 ext_dabt_pending;
                /* Align it to 8 bytes */
-               __u8 pad[6];
+               __u8 pad[5];
                __u64 serror_esr;
        } exception;
        __u32 reserved[12];
@@ -1051,9 +1057,23 @@ contain a valid state and shall be written into the VCPU.
 
 ARM/ARM64:
 
+User space may need to inject several types of events to the guest.
+
 Set the pending SError exception state for this VCPU. It is not possible to
 'cancel' an Serror that has been made pending.
 
+If the guest performed an access to I/O memory which could not be handled by
+userspace, for example because of missing instruction syndrome decode
+information or because there is no device mapped at the accessed IPA, then
+userspace can ask the kernel to inject an external abort using the address
+from the exiting fault on the VCPU. It is a programming error to set
+ext_dabt_pending after an exit which was not either KVM_EXIT_MMIO or
+KVM_EXIT_ARM_NISV. This feature is only available if the system supports
+KVM_CAP_ARM_INJECT_EXT_DABT. This is a helper which provides commonality in
+how userspace reports accesses for the above cases to guests, across different
+userspace implementations. Nevertheless, userspace can still emulate all Arm
+exceptions by manipulating individual registers using the KVM_SET_ONE_REG API.
+
 See KVM_GET_VCPU_EVENTS for the data structure.
 
 
index 2769360f195cafe1ead78ea917495778a7a52c21..03cd7c19a683bb809ab7ad78e9e4916d7a2113ac 100644 (file)
@@ -131,8 +131,9 @@ struct kvm_vcpu_events {
        struct {
                __u8 serror_pending;
                __u8 serror_has_esr;
+               __u8 ext_dabt_pending;
                /* Align it to 8 bytes */
-               __u8 pad[6];
+               __u8 pad[5];
                __u64 serror_esr;
        } exception;
        __u32 reserved[12];
index 684cf64b4033e640282d50cd291537a875fed4a7..735f9b007e58dbe8395d1649dcf9d0a10ae7deb1 100644 (file)
@@ -255,6 +255,12 @@ int __kvm_arm_vcpu_get_events(struct kvm_vcpu *vcpu,
 {
        events->exception.serror_pending = !!(*vcpu_hcr(vcpu) & HCR_VA);
 
+       /*
+        * We never return a pending ext_dabt here because we deliver it to
+        * the virtual CPU directly when setting the event and it's no longer
+        * 'pending' at this point.
+        */
+
        return 0;
 }
 
@@ -263,12 +269,16 @@ int __kvm_arm_vcpu_set_events(struct kvm_vcpu *vcpu,
 {
        bool serror_pending = events->exception.serror_pending;
        bool has_esr = events->exception.serror_has_esr;
+       bool ext_dabt_pending = events->exception.ext_dabt_pending;
 
        if (serror_pending && has_esr)
                return -EINVAL;
        else if (serror_pending)
                kvm_inject_vabt(vcpu);
 
+       if (ext_dabt_pending)
+               kvm_inject_dabt(vcpu, kvm_vcpu_get_hfar(vcpu));
+
        return 0;
 }
 
index 67c21f9bdbad2556be4114dfa09b8006a11e708e..d49c17a804910c1af1f27975fe55d0d9350bf70c 100644 (file)
@@ -164,8 +164,9 @@ struct kvm_vcpu_events {
        struct {
                __u8 serror_pending;
                __u8 serror_has_esr;
+               __u8 ext_dabt_pending;
                /* Align it to 8 bytes */
-               __u8 pad[6];
+               __u8 pad[5];
                __u64 serror_esr;
        } exception;
        __u32 reserved[12];
index dfd626447482eb8c1bacc5cc91af6217a22117cb..ca613a44c6ec272167138907f808c90b26e74a1b 100644 (file)
@@ -712,6 +712,12 @@ int __kvm_arm_vcpu_get_events(struct kvm_vcpu *vcpu,
        if (events->exception.serror_pending && events->exception.serror_has_esr)
                events->exception.serror_esr = vcpu_get_vsesr(vcpu);
 
+       /*
+        * We never return a pending ext_dabt here because we deliver it to
+        * the virtual CPU directly when setting the event and it's no longer
+        * 'pending' at this point.
+        */
+
        return 0;
 }
 
@@ -720,6 +726,7 @@ int __kvm_arm_vcpu_set_events(struct kvm_vcpu *vcpu,
 {
        bool serror_pending = events->exception.serror_pending;
        bool has_esr = events->exception.serror_has_esr;
+       bool ext_dabt_pending = events->exception.ext_dabt_pending;
 
        if (serror_pending && has_esr) {
                if (!cpus_have_const_cap(ARM64_HAS_RAS_EXTN))
@@ -733,6 +740,9 @@ int __kvm_arm_vcpu_set_events(struct kvm_vcpu *vcpu,
                kvm_inject_vabt(vcpu);
        }
 
+       if (ext_dabt_pending)
+               kvm_inject_dabt(vcpu, kvm_vcpu_get_hfar(vcpu));
+
        return 0;
 }
 
index a9d25a305af5916740fbfaf4634e18f66386a0ff..ccdb6a051ab21623d8ba8ea520fca36b0fb0c6b8 100644 (file)
@@ -109,7 +109,7 @@ static void inject_undef64(struct kvm_vcpu *vcpu)
 
 /**
  * kvm_inject_dabt - inject a data abort into the guest
- * @vcpu: The VCPU to receive the undefined exception
+ * @vcpu: The VCPU to receive the data abort
  * @addr: The address to report in the DFAR
  *
  * It is assumed that this code is called from the VCPU thread and that the
@@ -125,7 +125,7 @@ void kvm_inject_dabt(struct kvm_vcpu *vcpu, unsigned long addr)
 
 /**
  * kvm_inject_pabt - inject a prefetch abort into the guest
- * @vcpu: The VCPU to receive the undefined exception
+ * @vcpu: The VCPU to receive the prefetch abort
  * @addr: The address to report in the DFAR
  *
  * It is assumed that this code is called from the VCPU thread and that the
index 7336ee8d98d7454f9b998bbd09fa786b6e270ae3..65db5a4257ec41a309c7d550ee68457cc4fcc8af 100644 (file)
@@ -1007,6 +1007,7 @@ struct kvm_ppc_resize_hpt {
 #define KVM_CAP_ARM_IRQ_LINE_LAYOUT_2 174
 #define KVM_CAP_HYPERV_DIRECT_TLBFLUSH 175
 #define KVM_CAP_ARM_NISV_TO_USER 176
+#define KVM_CAP_ARM_INJECT_EXT_DABT 177
 
 #ifdef KVM_CAP_IRQ_ROUTING
 
index e6d56f60e4b63477a71cf7e70af4e3cf8924ccaf..12064780f1d8105d982d971c8c54b9fe9d1a7c08 100644 (file)
@@ -218,6 +218,7 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
        case KVM_CAP_VCPU_EVENTS:
        case KVM_CAP_ARM_IRQ_LINE_LAYOUT_2:
        case KVM_CAP_ARM_NISV_TO_USER:
+       case KVM_CAP_ARM_INJECT_EXT_DABT:
                r = 1;
                break;
        case KVM_CAP_ARM_SET_DEVICE_ADDR: