KVM: x86: On emulation failure, convey the exit reason, etc. to userspace
Should instruction emulation fail, include the VM exit reason, etc. in the emulation_failure data passed to userspace, in order that the VMM can report it as a debugging aid when describing the failure. Suggested-by: Joao Martins <joao.m.martins@oracle.com> Signed-off-by: David Edmondson <david.edmondson@oracle.com> Reviewed-by: Sean Christopherson <seanjc@google.com> Message-Id: <20210920103737.2696756-4-david.edmondson@oracle.com> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
This commit is contained in:
parent
0a62a0319a
commit
e615e35589
@ -1667,6 +1667,9 @@ extern u64 kvm_mce_cap_supported;
|
|||||||
int kvm_emulate_instruction(struct kvm_vcpu *vcpu, int emulation_type);
|
int kvm_emulate_instruction(struct kvm_vcpu *vcpu, int emulation_type);
|
||||||
int kvm_emulate_instruction_from_buffer(struct kvm_vcpu *vcpu,
|
int kvm_emulate_instruction_from_buffer(struct kvm_vcpu *vcpu,
|
||||||
void *insn, int insn_len);
|
void *insn, int insn_len);
|
||||||
|
void __kvm_prepare_emulation_failure_exit(struct kvm_vcpu *vcpu,
|
||||||
|
u64 *data, u8 ndata);
|
||||||
|
void kvm_prepare_emulation_failure_exit(struct kvm_vcpu *vcpu);
|
||||||
|
|
||||||
void kvm_enable_efer_bits(u64);
|
void kvm_enable_efer_bits(u64);
|
||||||
bool kvm_valid_efer(struct kvm_vcpu *vcpu, u64 efer);
|
bool kvm_valid_efer(struct kvm_vcpu *vcpu, u64 efer);
|
||||||
|
@ -5408,10 +5408,7 @@ static int handle_invalid_guest_state(struct kvm_vcpu *vcpu)
|
|||||||
|
|
||||||
if (vmx->emulation_required && !vmx->rmode.vm86_active &&
|
if (vmx->emulation_required && !vmx->rmode.vm86_active &&
|
||||||
vcpu->arch.exception.pending) {
|
vcpu->arch.exception.pending) {
|
||||||
vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
|
kvm_prepare_emulation_failure_exit(vcpu);
|
||||||
vcpu->run->internal.suberror =
|
|
||||||
KVM_INTERNAL_ERROR_EMULATION;
|
|
||||||
vcpu->run->internal.ndata = 0;
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -7664,29 +7664,78 @@ void kvm_inject_realmode_interrupt(struct kvm_vcpu *vcpu, int irq, int inc_eip)
|
|||||||
}
|
}
|
||||||
EXPORT_SYMBOL_GPL(kvm_inject_realmode_interrupt);
|
EXPORT_SYMBOL_GPL(kvm_inject_realmode_interrupt);
|
||||||
|
|
||||||
static void prepare_emulation_failure_exit(struct kvm_vcpu *vcpu)
|
static void prepare_emulation_failure_exit(struct kvm_vcpu *vcpu, u64 *data,
|
||||||
|
u8 ndata, u8 *insn_bytes, u8 insn_size)
|
||||||
{
|
{
|
||||||
struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt;
|
|
||||||
u32 insn_size = ctxt->fetch.end - ctxt->fetch.data;
|
|
||||||
struct kvm_run *run = vcpu->run;
|
struct kvm_run *run = vcpu->run;
|
||||||
|
u64 info[5];
|
||||||
|
u8 info_start;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Zero the whole array used to retrieve the exit info, as casting to
|
||||||
|
* u32 for select entries will leave some chunks uninitialized.
|
||||||
|
*/
|
||||||
|
memset(&info, 0, sizeof(info));
|
||||||
|
|
||||||
|
static_call(kvm_x86_get_exit_info)(vcpu, (u32 *)&info[0], &info[1],
|
||||||
|
&info[2], (u32 *)&info[3],
|
||||||
|
(u32 *)&info[4]);
|
||||||
|
|
||||||
run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
|
run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
|
||||||
run->emulation_failure.suberror = KVM_INTERNAL_ERROR_EMULATION;
|
run->emulation_failure.suberror = KVM_INTERNAL_ERROR_EMULATION;
|
||||||
run->emulation_failure.ndata = 0;
|
|
||||||
|
/*
|
||||||
|
* There's currently space for 13 entries, but 5 are used for the exit
|
||||||
|
* reason and info. Restrict to 4 to reduce the maintenance burden
|
||||||
|
* when expanding kvm_run.emulation_failure in the future.
|
||||||
|
*/
|
||||||
|
if (WARN_ON_ONCE(ndata > 4))
|
||||||
|
ndata = 4;
|
||||||
|
|
||||||
|
/* Always include the flags as a 'data' entry. */
|
||||||
|
info_start = 1;
|
||||||
run->emulation_failure.flags = 0;
|
run->emulation_failure.flags = 0;
|
||||||
|
|
||||||
if (insn_size) {
|
if (insn_size) {
|
||||||
run->emulation_failure.ndata = 3;
|
BUILD_BUG_ON((sizeof(run->emulation_failure.insn_size) +
|
||||||
|
sizeof(run->emulation_failure.insn_bytes) != 16));
|
||||||
|
info_start += 2;
|
||||||
run->emulation_failure.flags |=
|
run->emulation_failure.flags |=
|
||||||
KVM_INTERNAL_ERROR_EMULATION_FLAG_INSTRUCTION_BYTES;
|
KVM_INTERNAL_ERROR_EMULATION_FLAG_INSTRUCTION_BYTES;
|
||||||
run->emulation_failure.insn_size = insn_size;
|
run->emulation_failure.insn_size = insn_size;
|
||||||
memset(run->emulation_failure.insn_bytes, 0x90,
|
memset(run->emulation_failure.insn_bytes, 0x90,
|
||||||
sizeof(run->emulation_failure.insn_bytes));
|
sizeof(run->emulation_failure.insn_bytes));
|
||||||
memcpy(run->emulation_failure.insn_bytes,
|
memcpy(run->emulation_failure.insn_bytes, insn_bytes, insn_size);
|
||||||
ctxt->fetch.data, insn_size);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
memcpy(&run->internal.data[info_start], info, sizeof(info));
|
||||||
|
memcpy(&run->internal.data[info_start + ARRAY_SIZE(info)], data,
|
||||||
|
ndata * sizeof(data[0]));
|
||||||
|
|
||||||
|
run->emulation_failure.ndata = info_start + ARRAY_SIZE(info) + ndata;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void prepare_emulation_ctxt_failure_exit(struct kvm_vcpu *vcpu)
|
||||||
|
{
|
||||||
|
struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt;
|
||||||
|
|
||||||
|
prepare_emulation_failure_exit(vcpu, NULL, 0, ctxt->fetch.data,
|
||||||
|
ctxt->fetch.end - ctxt->fetch.data);
|
||||||
|
}
|
||||||
|
|
||||||
|
void __kvm_prepare_emulation_failure_exit(struct kvm_vcpu *vcpu, u64 *data,
|
||||||
|
u8 ndata)
|
||||||
|
{
|
||||||
|
prepare_emulation_failure_exit(vcpu, data, ndata, NULL, 0);
|
||||||
|
}
|
||||||
|
EXPORT_SYMBOL_GPL(__kvm_prepare_emulation_failure_exit);
|
||||||
|
|
||||||
|
void kvm_prepare_emulation_failure_exit(struct kvm_vcpu *vcpu)
|
||||||
|
{
|
||||||
|
__kvm_prepare_emulation_failure_exit(vcpu, NULL, 0);
|
||||||
|
}
|
||||||
|
EXPORT_SYMBOL_GPL(kvm_prepare_emulation_failure_exit);
|
||||||
|
|
||||||
static int handle_emulation_failure(struct kvm_vcpu *vcpu, int emulation_type)
|
static int handle_emulation_failure(struct kvm_vcpu *vcpu, int emulation_type)
|
||||||
{
|
{
|
||||||
struct kvm *kvm = vcpu->kvm;
|
struct kvm *kvm = vcpu->kvm;
|
||||||
@ -7701,16 +7750,14 @@ static int handle_emulation_failure(struct kvm_vcpu *vcpu, int emulation_type)
|
|||||||
|
|
||||||
if (kvm->arch.exit_on_emulation_error ||
|
if (kvm->arch.exit_on_emulation_error ||
|
||||||
(emulation_type & EMULTYPE_SKIP)) {
|
(emulation_type & EMULTYPE_SKIP)) {
|
||||||
prepare_emulation_failure_exit(vcpu);
|
prepare_emulation_ctxt_failure_exit(vcpu);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
kvm_queue_exception(vcpu, UD_VECTOR);
|
kvm_queue_exception(vcpu, UD_VECTOR);
|
||||||
|
|
||||||
if (!is_guest_mode(vcpu) && static_call(kvm_x86_get_cpl)(vcpu) == 0) {
|
if (!is_guest_mode(vcpu) && static_call(kvm_x86_get_cpl)(vcpu) == 0) {
|
||||||
vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
|
prepare_emulation_ctxt_failure_exit(vcpu);
|
||||||
vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
|
|
||||||
vcpu->run->internal.ndata = 0;
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -12336,9 +12383,7 @@ int kvm_handle_memory_failure(struct kvm_vcpu *vcpu, int r,
|
|||||||
* doesn't seem to be a real use-case behind such requests, just return
|
* doesn't seem to be a real use-case behind such requests, just return
|
||||||
* KVM_EXIT_INTERNAL_ERROR for now.
|
* KVM_EXIT_INTERNAL_ERROR for now.
|
||||||
*/
|
*/
|
||||||
vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
|
kvm_prepare_emulation_failure_exit(vcpu);
|
||||||
vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
|
|
||||||
vcpu->run->internal.ndata = 0;
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -398,6 +398,11 @@ struct kvm_run {
|
|||||||
* "ndata" is correct, that new fields are enumerated in "flags",
|
* "ndata" is correct, that new fields are enumerated in "flags",
|
||||||
* and that each flag enumerates fields that are 64-bit aligned
|
* and that each flag enumerates fields that are 64-bit aligned
|
||||||
* and sized (so that ndata+internal.data[] is valid/accurate).
|
* and sized (so that ndata+internal.data[] is valid/accurate).
|
||||||
|
*
|
||||||
|
* Space beyond the defined fields may be used to store arbitrary
|
||||||
|
* debug information relating to the emulation failure. It is
|
||||||
|
* accounted for in "ndata" but the format is unspecified and is
|
||||||
|
* not represented in "flags". Any such information is *not* ABI!
|
||||||
*/
|
*/
|
||||||
struct {
|
struct {
|
||||||
__u32 suberror;
|
__u32 suberror;
|
||||||
@ -409,6 +414,7 @@ struct kvm_run {
|
|||||||
__u8 insn_bytes[15];
|
__u8 insn_bytes[15];
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
/* Arbitrary debug data may follow. */
|
||||||
} emulation_failure;
|
} emulation_failure;
|
||||||
/* KVM_EXIT_OSI */
|
/* KVM_EXIT_OSI */
|
||||||
struct {
|
struct {
|
||||||
|
Loading…
Reference in New Issue
Block a user