Improve handling of stage 2 aborts without instruction decode
Subsystems (5)
v3 cover Improve handling of stage 2 aborts without instruction decode
When a guest accesses memory outside the memory slots, KVM usually bounces the access back to userspace with KVM_EXIT_MMIO. However, on arm/arm64 systems, certain load/store instructions did not provide decoding info for the hypervisor to emulate the instruction, and in this case KVM has rather rudely returned -ENOSYS and printed a not overly helpful error message:
load/store instruction decoding not implemented
This patch series improves the error message and allows userspace to be notified of this event instead of receiving -ENOSYS, and also allows userspace to ask KVM to inject an external abort to the guest, which it can use for any memory access that it either cannot handle.
One remaining case which this patch set does not address is if the guest accesses an in-kernel emulated device, such as the VGIC, but using a load/store instruction which doesn't provide decode info. With these patches, this will return to userspace for it to handle, but there's no way for userspace to return the decoding information to KVM and have KVM complete the access to the in-kernel emulated device. I have no plans to address this limitation.
Changes since v2:
- Cleanup inconsistent use of has_ in patch 2 and allow injecting SError and external abort simultaenously with a single call to set VCPU events.
Changes since v1:
- Rebased on v5.4-rc2
- Fixed some documentation and coding nit in review of v1
Christoffer Dall (2):
KVM: arm/arm64: Allow reporting non-ISV data aborts to userspace
KVM: arm/arm64: Allow user injection of external data aborts
Documentation/virt/kvm/api.txt | 55 +++++++++++++++++++++++++++- arch/arm/include/asm/kvm_arm.h | 1 + arch/arm/include/asm/kvm_emulate.h | 5 +++ arch/arm/include/asm/kvm_host.h | 8 ++++ arch/arm/include/uapi/asm/kvm.h | 3 +- arch/arm/kvm/guest.c | 10 +++++ arch/arm64/include/asm/kvm_emulate.h | 5 +++ arch/arm64/include/asm/kvm_host.h | 8 ++++ arch/arm64/include/uapi/asm/kvm.h | 3 +- arch/arm64/kvm/guest.c | 10 +++++ arch/arm64/kvm/inject_fault.c | 4 +- include/uapi/linux/kvm.h | 8 ++++ virt/kvm/arm/arm.c | 22 +++++++++++ virt/kvm/arm/mmio.c | 9 ++++- 14 files changed, 145 insertions(+), 6 deletions(-)
Signature · 6 lines
2.18.0 _______________________________________________ kvmarm mailing list [email protected] https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
PATCH v3 1/2 KVM: arm/arm64: Allow reporting non-ISV data aborts to userspace
For a long time, if a guest accessed memory outside of a memslot using any of the load/store instructions in the architecture which doesn't supply decoding information in the ESR_EL2 (the ISV bit is not set), the kernel would print the following message and terminate the VM as a result of returning -ENOSYS to userspace:
load/store instruction decoding not implemented
The reason behind this message is that KVM assumes that all accesses outside a memslot is an MMIO access which should be handled by userspace, and we originally expected to eventually implement some sort of decoding of load/store instructions where the ISV bit was not set.
However, it turns out that many of the instructions which don't provide decoding information on abort are not safe to use for MMIO accesses, and the remaining few that would potentially make sense to use on MMIO accesses, such as those with register writeback, are not used in practice. It also turns out that fetching an instruction from guest memory can be a pretty horrible affair, involving stopping all CPUs on SMP systems, handling multiple corner cases of address translation in software, and more. It doesn't appear likely that we'll ever implement this in the kernel.
What is much more common is that a user has misconfigured his/her guest and is actually not accessing an MMIO region, but just hitting some random hole in the IPA space. In this scenario, the error message above is almost misleading and has led to a great deal of confusion over the years.
It is, nevertheless, ABI to userspace, and we therefore need to introduce a new capability that userspace explicitly enables to change behavior.
This patch introduces KVM_CAP_ARM_NISV_TO_USER (NISV meaning Non-ISV) which does exactly that, and introduces a new exit reason to report the event to userspace. User space can then emulate an exception to the guest, restart the guest, suspend the guest, or take any other appropriate action as per the policy of the running system.
--- Documentation/virt/kvm/api.txt | 33 ++++++++++++++++++++++++++++ arch/arm/include/asm/kvm_arm.h | 1 + arch/arm/include/asm/kvm_emulate.h | 5 +++++ arch/arm/include/asm/kvm_host.h | 8 +++++++ arch/arm64/include/asm/kvm_emulate.h | 5 +++++ arch/arm64/include/asm/kvm_host.h | 8 +++++++ include/uapi/linux/kvm.h | 7 ++++++ virt/kvm/arm/arm.c | 21 ++++++++++++++++++ virt/kvm/arm/mmio.c | 9 +++++++- 9 files changed, 96 insertions(+), 1 deletion(-)
Documentation/virt/kvm/api.txt +33 -0
@@ -4468,6 +4468,39 @@ Hyper-V SynIC state change. Notification is used to remap SynIC event/message pages and to enable/disable SynIC messages/events processing in userspace. + /* KVM_EXIT_ARM_NISV */ + struct { + __u64 esr_iss; + __u64 fault_ipa; + } arm_nisv; + +Used on arm and arm64 systems. If a guest accesses memory not in a memslot, +KVM will typically return to userspace and ask it to do MMIO emulation on its +behalf. However, for certain classes of instructions, no instruction decode +(direction, length of memory access) is provided, and fetching and decoding +the instruction from the VM is overly complicated to live in the kernel. + +Historically, when this situation occurred, KVM would print a warning and kill +the VM. KVM assumed that if the guest accessed non-memslot memory, it was +trying to do I/O, which just couldn't be emulated, and the warning message was +phrased accordingly. However, what happened more often was that a guest bug +caused access outside the guest memory areas which should lead to a more +meaningful warning message and an external abort in the guest, if the access +did not fall within an I/O window. + +Userspace implementations can query for KVM_CAP_ARM_NISV_TO_USER, and enable +this capability at VM creation. Once this is done, these types of errors will +instead return to userspace with KVM_EXIT_ARM_NISV, with the valid bits from +the HSR (arm) and ESR_EL2 (arm64) in the esr_iss field, and the faulting IPA +in the fault_ipa field. Userspace can either fix up the access if it's +actually an I/O access by decoding the instruction from guest memory (if it's +very brave) and continue executing the guest, or it can decide to suspend, +dump, or restart the guest. + +Note that KVM does not skip the faulting instruction as it does for +KVM_EXIT_MMIO, but userspace has to emulate any change to the processing state +if it decides to decode and emulate the instruction. + /* Fix the size of the union. */ char padding[256]; };
arch/arm/include/asm/kvm_arm.h +1 -0
@@ -162,6 +162,7 @@#define HSR_ISV (_AC(1, UL) << HSR_ISV_SHIFT)#define HSR_SRT_SHIFT (16)#define HSR_SRT_MASK (0xf << HSR_SRT_SHIFT) +#define HSR_CM (1 << 8)#define HSR_FSC (0x3f)#define HSR_FSC_TYPE (0x3c)#define HSR_SSE (1 << 21)
arch/arm/include/asm/kvm_emulate.h +5 -0
@@ -167,6 +167,11 @@ static inline bool kvm_vcpu_dabt_isvalid(struct kvm_vcpu *vcpu)return kvm_vcpu_get_hsr (vcpu) &HSR_ISV ; } +static inline unsigned long kvm_vcpu_dabt_iss_nisv_sanitized (const struct kvm_vcpu *vcpu) +{ +return kvm_vcpu_get_hsr (vcpu) & (HSR_CM |HSR_WNR |HSR_FSC ); +} +static inline bool kvm_vcpu_dabt_iswrite (struct kvm_vcpu *vcpu) {return kvm_vcpu_get_hsr (vcpu) &HSR_WNR ;
arch/arm/include/asm/kvm_host.h +8 -0
@@ -76,6 +76,14 @@ struct kvm_arch {/* Mandated version of PSCI */ u32 psci_version; + +/* +* If we encounter a data abort without valid instruction syndrome +* information, report this to user space. User space can (and +* should) opt in to this feature if KVM_CAP_ARM_NISV_TO_USER is +* supported. +*/ +bool return_nisv_io_abort_to_user; };#define KVM_NR_MEM_OBJS 40
arch/arm64/include/asm/kvm_emulate.h +5 -0
@@ -258,6 +258,11 @@ static inline bool kvm_vcpu_dabt_isvalid(const struct kvm_vcpu *vcpu)return !!(kvm_vcpu_get_hsr (vcpu) & ESR_ELx_ISV); } +static inline unsigned long kvm_vcpu_dabt_iss_nisv_sanitized (const struct kvm_vcpu *vcpu) +{ +return kvm_vcpu_get_hsr (vcpu) & (ESR_ELx_CM | ESR_ELx_WNR | ESR_ELx_FSC); +} +static inline bool kvm_vcpu_dabt_issext (const struct kvm_vcpu *vcpu) {return !!(kvm_vcpu_get_hsr (vcpu) & ESR_ELx_SSE);
arch/arm64/include/asm/kvm_host.h +8 -0
@@ -83,6 +83,14 @@ struct kvm_arch {/* Mandated version of PSCI */ u32 psci_version; + +/* +* If we encounter a data abort without valid instruction syndrome +* information, report this to user space. User space can (and +* should) opt in to this feature if KVM_CAP_ARM_NISV_TO_USER is +* supported. +*/ +bool return_nisv_io_abort_to_user; };#define KVM_NR_MEM_OBJS 40
include/uapi/linux/kvm.h +7 -0
@@ -235,6 +235,7 @@ struct kvm_hyperv_exit {#define KVM_EXIT_S390_STSI 25#define KVM_EXIT_IOAPIC_EOI 26#define KVM_EXIT_HYPERV 27 +#define KVM_EXIT_ARM_NISV 28/* For KVM_EXIT_INTERNAL_ERROR */ /* Emulate instruction failed. */
@@ -394,6 +395,11 @@ struct kvm_run { } eoi;/* KVM_EXIT_HYPERV */ struct kvm_hyperv_exit hyperv; +/* KVM_EXIT_ARM_NISV */ +struct { +__u64 esr_iss; +__u64 fault_ipa; + } arm_nisv;/* Fix the size of the union. */ char padding[256 ]; };
@@ -1000,6 +1006,7 @@ struct kvm_ppc_resize_hpt {#define KVM_CAP_PMU_EVENT_FILTER 173#define KVM_CAP_ARM_IRQ_LINE_LAYOUT_2 174#define KVM_CAP_HYPERV_DIRECT_TLBFLUSH 175 +#define KVM_CAP_ARM_NISV_TO_USER 176#ifdef KVM_CAP_IRQ_ROUTING
virt/kvm/arm/arm.c +21 -0
@@ -98,6 +98,26 @@ int kvm_arch_check_processor_compat(void)return 0 ; } +int kvm_vm_ioctl_enable_cap (struct kvm *kvm, +struct kvm_enable_cap *cap) +{ +int r; + +if (cap->flags) +return -EINVAL ; + +switch (cap->cap) { +case KVM_CAP_ARM_NISV_TO_USER : + r =0 ; + kvm->arch.return_nisv_io_abort_to_user = true; +break ; +default : + r = -EINVAL ; +break ; + } + +return r; +}/** * kvm_arch_init_vm - initializes a VM data structure
@@ -197,6 +217,7 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)case KVM_CAP_IMMEDIATE_EXIT :case KVM_CAP_VCPU_EVENTS :case KVM_CAP_ARM_IRQ_LINE_LAYOUT_2 : +case KVM_CAP_ARM_NISV_TO_USER : r =1 ;break ;case KVM_CAP_ARM_SET_DEVICE_ADDR :
virt/kvm/arm/mmio.c +8 -1
@@ -167,7 +167,14 @@ int io_mem_abort(struct kvm_vcpu *vcpu, struct kvm_run *run,if (ret)return ret; }else { -kvm_err ("load/store instruction decoding not implemented\n" ); +if (vcpu->kvm->arch.return_nisv_io_abort_to_user) { + run->exit_reason =KVM_EXIT_ARM_NISV ; + run->arm_nisv.esr_iss =kvm_vcpu_dabt_iss_nisv_sanitized (vcpu); + run->arm_nisv.fault_ipa = fault_ipa; +return 0 ; + } + +kvm_pr_unimpl ("Data abort outside memslots with no valid syndrome info\n" );return -ENOSYS ; }
Signature · 6 lines
2.18.0 _______________________________________________ kvmarm mailing list [email protected] https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
PATCH v3 2/2 KVM: arm/arm64: Allow user injection of external data aborts
In reply to Christoffer DallIn 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.
--- Documentation/virt/kvm/api.txt | 22 +++++++++++++++++++++- arch/arm/include/uapi/asm/kvm.h | 3 ++- arch/arm/kvm/guest.c | 10 ++++++++++ arch/arm64/include/uapi/asm/kvm.h | 3 ++- arch/arm64/kvm/guest.c | 10 ++++++++++ arch/arm64/kvm/inject_fault.c | 4 ++-- include/uapi/linux/kvm.h | 1 + virt/kvm/arm/arm.c | 1 + 8 files changed, 49 insertions(+), 5 deletions(-)
Documentation/virt/kvm/api.txt +21 -1
@@ -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.
arch/arm/include/uapi/asm/kvm.h +2 -1
@@ -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 ];
arch/arm/kvm/guest.c +10 -0
@@ -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 ; }
arch/arm64/include/uapi/asm/kvm.h +2 -1
@@ -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 ];
arch/arm64/kvm/guest.c +10 -0
@@ -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 ; }
arch/arm64/kvm/inject_fault.c +2 -2
@@ -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
include/uapi/linux/kvm.h +1 -0
@@ -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
virt/kvm/arm/arm.c +1 -0
@@ -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 :
Signature · 6 lines
2.18.0 _______________________________________________ kvmarm mailing list [email protected] https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
Re: [PATCH v3 2/2] KVM: arm/arm64: Allow user injection of external data aborts
Quoted 9 lines · Christoffer Dall
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.
Quoted 4 lines
It turns out, we already have the set event functionality which we can perfectly reuse for this.
Signed-off-by Christoffer Dall <[email protected]>
Alex
Amazon Development Center Germany GmbH
Krausenstr. 38
10117 Berlin
Geschaeftsfuehrung: Christian Schlaeger, Ralf Herbrich Eingetragen am Amtsgericht Charlottenburg unter HRB 149173 B
Sitz: Berlin
Ust-ID: DE 289 237 879
_______________________________________________
kvmarm mailing list
[email protected]
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
PATCH v3 3/5 update headers: Update the KVM headers for new Arm fault reporting features
In reply to Christoffer DallIn preparation for improving our handling of guest aborts with missing decode info or outside any mapped resource, sync updated Linux header files.
NOTE: This is a development update and these headers are not yet in an upstream tree. DO NOT MERGE.
--- arm/aarch32/include/asm/kvm.h | 3 ++- arm/aarch64/include/asm/kvm.h | 3 ++- include/linux/kvm.h | 8 ++++++++ 3 files changed, 12 insertions(+), 2 deletions(-)
arm/aarch32/include/asm/kvm.h +2 -1
@@ -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 ];
arm/aarch64/include/asm/kvm.h +2 -1
@@ -160,8 +160,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 ];
include/linux/kvm.h +8 -0
@@ -235,6 +235,7 @@ struct kvm_hyperv_exit {#define KVM_EXIT_S390_STSI 25#define KVM_EXIT_IOAPIC_EOI 26#define KVM_EXIT_HYPERV 27 +#define KVM_EXIT_ARM_NISV 28/* For KVM_EXIT_INTERNAL_ERROR */ /* Emulate instruction failed. */
@@ -392,6 +393,11 @@ struct kvm_run { } eoi;/* KVM_EXIT_HYPERV */ struct kvm_hyperv_exit hyperv; +/* KVM_EXIT_ARM_NISV */ +struct { +__u64 esr_iss; +__u64 fault_ipa; + } arm_nisv;/* Fix the size of the union. */ char padding[256 ]; };
@@ -988,6 +994,8 @@ struct kvm_ppc_resize_hpt {#define KVM_CAP_ARM_VM_IPA_SIZE 165#define KVM_CAP_MANUAL_DIRTY_LOG_PROTECT 166#define KVM_CAP_HYPERV_CPUID 167 +#define KVM_CAP_ARM_NISV_TO_USER 176 +#define KVM_CAP_ARM_INJECT_EXT_DABT 177#ifdef KVM_CAP_IRQ_ROUTING
Signature · 6 lines
2.18.0 _______________________________________________ kvmarm mailing list [email protected] https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
PATCH v3 4/5 arm: Handle exits from undecoded load/store instructions
In reply to Christoffer DallKVM occasionally encounters guests that attempt to access memory outside the registered RAM memory slots using instructions that don't provide decoding information in the ESR_EL2 (the ISV bit is not set), and historically this has led to the kernel printing a confusing error message in dmesg and returning -ENOYSYS from KVM_RUN.
KVM/Arm now has KVM_CAP_ARM_NISV_TO_USER, which can be enabled from userspace, and which allows us to handle this with a little bit more helpful information to the user. For example, we can at least tell the user if the guest just hit a hole in the guest's memory map, or if this appeared to be an attempt at doing MMIO.
--- arm/kvm-cpu.c | 20 +++++++++++++++++++- arm/kvm.c | 8 ++++++++ include/kvm/kvm.h | 1 + kvm.c | 1 + mmio.c | 11 +++++++++++ 5 files changed, 40 insertions(+), 1 deletion(-)
arm/kvm-cpu.c +19 -1
@@ -136,7 +136,25 @@ void kvm_cpu__delete(struct kvm_cpu *vcpu)bool kvm_cpu__handle_exit (struct kvm_cpu *vcpu) { -return false; +switch (vcpu->kvm_run->exit_reason) { +case KVM_EXIT_ARM_NISV : { +u64 phys_addr = vcpu->kvm_run->arm_nisv.fault_ipa; + +if (!arm_addr_in_ioport_region (phys_addr) && + !kvm__mmio_exists (vcpu, phys_addr)) +die ("Guest accessed memory outside RAM and IO ranges" ); + +/* +* We cannot fetch and decode instructions from a KVM guest, +* which used a load/store instruction that doesn't get +* decoded in the ESR towards an I/O device, so we have no +* choice but to exit to the user with an error. +*/ +die ("Guest accessed I/O device with unsupported load/store instruction" ); + } +default : +return false; + } }void kvm_cpu__show_page_tables (struct kvm_cpu *vcpu)
arm/kvm.c +8 -0
@@ -59,6 +59,8 @@ void kvm__arch_set_cmdline(char *cmdline, bool video)void kvm__arch_init (struct kvm *kvm,const char *hugetlbfs_path,u64 ram_size) { +struct kvm_enable_cap enable_cap = { .flags =0 }; +/* * Allocate guest memory. We must align our buffer to 64K to * correlate with the maximum guest page size for virtio-mmio.
@@ -83,6 +85,12 @@ void kvm__arch_init(struct kvm *kvm, const char *hugetlbfs_path, u64 ram_size)madvise (kvm->arch.ram_alloc_start, kvm->arch.ram_alloc_size,MADV_HUGEPAGE ); +if (kvm__supports_extension (kvm,KVM_CAP_ARM_NISV_TO_USER )) { + enable_cap.cap =KVM_CAP_ARM_NISV_TO_USER ; +if (ioctl (kvm->vm_fd,KVM_ENABLE_CAP , &enable_cap) <0 ) +die ("unable to enable NISV_TO_USER capability" ); + } +/* Create the virtual GIC. */ if (gic__create (kvm, kvm->cfg.arch.irqchip))die ("Failed to create virtual GIC" );
include/kvm/kvm.h +1 -0
@@ -107,6 +107,7 @@ bool kvm__emulate_io(struct kvm_cpu *vcpu, u16 port, void *data, int direction,bool kvm__emulate_mmio (struct kvm_cpu *vcpu,u64 phys_addr,u8 *data,u32 len,u8 is_write);int kvm__register_mem (struct kvm *kvm,u64 guest_phys,u64 size,void *userspace_addr,enum kvm_mem_type type); +bool kvm__mmio_exists (struct kvm_cpu *vcpu,u64 phys_addr);static inline int kvm__register_ram (struct kvm *kvm,u64 guest_phys,u64 size,void *userspace_addr) {
kvm.c +1 -0
@@ -55,6 +55,7 @@ const char *kvm_exit_reasons[] = {#ifdef CONFIG_PPC64 DEFINE_KVM_EXIT_REASON (KVM_EXIT_PAPR_HCALL ), #endif + DEFINE_KVM_EXIT_REASON(KVM_EXIT_ARM_NISV), };static int pause_event;
mmio.c +11 -0
@@ -139,3 +139,14 @@ bool kvm__emulate_mmio(struct kvm_cpu *vcpu, u64 phys_addr, u8 *data, u32 len, ureturn true; } + +bool kvm__mmio_exists (struct kvm_cpu *vcpu,u64 phys_addr) +{ +struct mmio_mapping *mmio; + +br_read_lock (vcpu->kvm); + mmio =mmio_search (&mmio_tree, phys_addr,1 ); +br_read_unlock (vcpu->kvm); + +return mmio !=NULL ; +}
Signature · 6 lines
2.18.0 _______________________________________________ kvmarm mailing list [email protected] https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
PATCH v3 5/5 arm: Inject external data aborts when accessing holes in the memory map
In reply to Christoffer DallOccasionally guests will attempt to access parts of the guest memory map where there is... nothing at all. Until now, we've handled this by either forcefully killing the guest, or silently (unless a debug option was enabled) ignoring the access. Neither is very helpful to a user, who is most likely running either a broken or misconfigured guest.
A more appropriate action is to inject an external abort to the guest. Luckily, with KVM_CAP_ARM_INJECT_EXT_DABT, we can use the set event mechanism and ask KVM to do this for us.
So we add an architecture specific hook to handle accesses to MMIO regions which cannot be found, and allow them to return if the invalid access was handled or not.
--- arm/include/arm-common/kvm-cpu-arch.h | 16 ++++++++++++++++ arm/kvm-cpu.c | 2 +- mips/include/kvm/kvm-cpu-arch.h | 5 +++++ mmio.c | 3 ++- powerpc/include/kvm/kvm-cpu-arch.h | 5 +++++ x86/include/kvm/kvm-cpu-arch.h | 5 +++++ 6 files changed, 34 insertions(+), 2 deletions(-)
arm/include/arm-common/kvm-cpu-arch.h +16 -0
@@ -57,6 +57,22 @@ static inline bool kvm_cpu__emulate_mmio(struct kvm_cpu *vcpu, u64 phys_addr, return kvm__emulate_mmio(vcpu, phys_addr, data, len, is_write); } +static inline bool kvm_cpu__mmio_not_found (struct kvm_cpu *vcpu,u64 phys_addr) +{ +struct kvm_vcpu_events events = { + .exception.ext_dabt_pending =1 , + }; +int err; + +if (!kvm__supports_extension (vcpu->kvm,KVM_CAP_ARM_INJECT_EXT_DABT )) +return false; + + err =ioctl (vcpu->vcpu_fd,KVM_SET_VCPU_EVENTS , &events); +if (err) +die ("failed to inject external abort" ); +return true; +} +unsigned long kvm_cpu__get_vcpu_mpidr (struct kvm_cpu *vcpu);#endif /* ARM_COMMON__KVM_CPU_ARCH_H */
arm/kvm-cpu.c +1 -1
@@ -142,7 +142,7 @@ bool kvm_cpu__handle_exit(struct kvm_cpu *vcpu)if (!arm_addr_in_ioport_region (phys_addr) && !kvm__mmio_exists (vcpu, phys_addr)) -die ("Guest accessed memory outside RAM and IO ranges" ); +return kvm_cpu__mmio_not_found (vcpu, phys_addr);/* * We cannot fetch and decode instructions from a KVM guest,
mips/include/kvm/kvm-cpu-arch.h +5 -0
@@ -40,4 +40,9 @@ static inline bool kvm_cpu__emulate_mmio(struct kvm_cpu *vcpu, u64 phys_addr, u8 return kvm__emulate_mmio(vcpu, phys_addr, data, len, is_write); } +static inline bool kvm_cpu__mmio_not_found (struct kvm_cpu *vcpu,u64 phys_addr) +{ +return false; +} +#endif /* KVM__KVM_CPU_ARCH_H */
mmio.c +2 -1
@@ -130,7 +130,8 @@ bool kvm__emulate_mmio(struct kvm_cpu *vcpu, u64 phys_addr, u8 *data, u32 len, uif (mmio) mmio->mmio_fn (vcpu, phys_addr, data, len, is_write, mmio->ptr);else { -if (vcpu->kvm->cfg.mmio_debug) +if (!kvm_cpu__mmio_not_found (vcpu, phys_addr) && + vcpu->kvm->cfg.mmio_debug)fprintf (stderr,"Warning: Ignoring MMIO %s at %016llx (length %u)\n" ,to_direction (is_write), (unsigned long long )phys_addr, len);
powerpc/include/kvm/kvm-cpu-arch.h +5 -0
@@ -76,4 +76,9 @@ static inline bool kvm_cpu__emulate_io(struct kvm_cpu *vcpu, u16 port, void *datbool kvm_cpu__emulate_mmio (struct kvm_cpu *vcpu,u64 phys_addr,u8 *data,u32 len,u8 is_write); +static inline bool kvm_cpu__mmio_not_found (struct kvm_cpu *vcpu,u64 phys_addr) +{ +return false; +} +#endif /* KVM__KVM_CPU_ARCH_H */
x86/include/kvm/kvm-cpu-arch.h +5 -0
@@ -47,4 +47,9 @@ static inline bool kvm_cpu__emulate_mmio(struct kvm_cpu *vcpu, u64 phys_addr, u8 return kvm__emulate_mmio(vcpu, phys_addr, data, len, is_write); } +static inline bool kvm_cpu__mmio_not_found (struct kvm_cpu *vcpu,u64 phys_addr) +{ +return false; +} +#endif /* KVM__KVM_CPU_ARCH_H */
Signature · 6 lines
2.18.0 _______________________________________________ kvmarm mailing list [email protected] https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
Marc Zyngier
maintainerHi Christoffer,
Quoted 15 lines · Christoffer Dall
When a guest accesses memory outside the memory slots, KVM usually bounces the access back to userspace with
KVM_EXIT_MMIO. However, on arm/arm64 systems, certain load/store instructions did not provide decoding info for the hypervisor to emulate the instruction, and in this case KVM has rather rudely returned-ENOSYSand printed a not overly helpful error message:load/store instruction decoding not implemented
This patch series improves the error message and allows userspace to be notified of this event instead of receiving
-ENOSYS, and also allows userspace to ask KVM to inject an external abort to the guest, which it can use for any memory access that it either cannot handle.
Quoted 7 lines
One remaining case which this patch set does not address is if the guest accesses an in-kernel emulated device, such as the VGIC, but using a load/store instruction which doesn't provide decode info. With these patches, this will return to userspace for it to handle, but there's no way for userspace to return the decoding information to KVM and have KVM complete the access to the in-kernel emulated device. I have no plans to address this limitation.
You had some pending comments on patch 2, and you seem to indicate that you would respin the series. Do you have plans to do so in the coming days? I'd like to put that series into -next, but I can either wait for your respin, or queue it as it is and apply fixes on top.
Please let me know what you prefer.
Thanks,
M.
Signature · 5 lines
Jazz is not dead, it just smells funny. _______________________________________________ kvmarm mailing list [email protected] https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
Quoted 27 lines · Marc Zyngier
Hi Christoffer,
Quoted 15 lines · Christoffer Dall
When a guest accesses memory outside the memory slots, KVM usually bounces the access back to userspace with
KVM_EXIT_MMIO. However, on arm/arm64 systems, certain load/store instructions did not provide decoding info for the hypervisor to emulate the instruction, and in this case KVM has rather rudely returned-ENOSYSand printed a not overly helpful error message:load/store instruction decoding not implemented
This patch series improves the error message and allows userspace to be notified of this event instead of receiving
-ENOSYS, and also allows userspace to ask KVM to inject an external abort to the guest, which it can use for any memory access that it either cannot handle.Quoted 7 lines
One remaining case which this patch set does not address is if the guest accesses an in-kernel emulated device, such as the VGIC, but using a load/store instruction which doesn't provide decode info. With these patches, this will return to userspace for it to handle, but there's no way for userspace to return the decoding information to KVM and have KVM complete the access to the in-kernel emulated device. I have no plans to address this limitation.
Quoted 5 lines
You had some pending comments on patch 2, and you seem to indicate that you would respin the series. Do you have plans to do so in the coming days? I'd like to put that series into -next, but I can either wait for your respin, or queue it as it is and apply fixes on top.
I think those for v2 and this is v3 which should address those concerns. Did I miss something or manage to confuse mysel here?
Thanks,
Christoffer
_______________________________________________
kvmarm mailing list
[email protected]
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
Marc Zyngier
maintainerQuoted 40 lines · Christoffer Dall
Quoted 39 lines · Marc Zyngier
Hi Christoffer,
Quoted 3 lines · Christoffer Dall
When a guest accesses memory outside the memory slots, KVM usually bounces the access back to userspace with
KVM_EXIT_MMIO. However,on
Quoted 2 lines
arm/arm64 systems, certain load/store instructions did not provide decoding info for the hypervisor to emulate the instruction, and
in this
Quoted 1 line
case KVM has rather rudely returned
-ENOSYSand printed a notoverly
Quoted 5 lines
helpful error message:
load/store instruction decoding not implemented
This patch series improves the error message and allows userspace
to be
Quoted 1 line
notified of this event instead of receiving
-ENOSYS, and alsoallows
Quoted 1 line
userspace to ask KVM to inject an external abort to the guest,
which it
Quoted 3 lines
can use for any memory access that it either cannot handle.
One remaining case which this patch set does not address is if the
guest
Quoted 1 line
accesses an in-kernel emulated device, such as the VGIC, but using
a
Quoted 1 line
load/store instruction which doesn't provide decode info. With
these
Quoted 1 line
patches, this will return to userspace for it to handle, but
there's no
Quoted 1 line
way for userspace to return the decoding information to KVM and
have KVM
Quoted 1 line
complete the access to the in-kernel emulated device. I have no
plans
Quoted 1 line
to address this limitation.
Quoted 8 lines
Quoted 5 lines
You had some pending comments on patch 2, and you seem to indicate that you would respin the series. Do you have plans to do so in the coming days? I'd like to put that series into -next, but I can either wait for your respin, or queue it as it is and apply fixes on top.
I think those for v2 and this is v3 which should address those concerns. Did I miss something or manage to confuse mysel here?
Gah! No, it looks like the mistake is mine. I've mixed the two series in my Inbox, and the result is a total brain-fart. Ignore me.
I'll queue these patches ASAP.
Thanks,
M.
Signature · 5 lines
Jazz is not dead. It just smells funny... _______________________________________________ kvmarm mailing list [email protected] https://lists.cs.columbia.edu/mailman/listinfo/kvmarm