Re: [PATCH 1/2] KVM: x86: Add emulation support for #GP triggered by VM instructions

Paolo Bonzini
2021-01-17T18:24:53Z

On 15/01/21 08:00, Wei Huang wrote:
> If the whole body inside if-statement is moved out, do you expect the
> interface of x86_emulate_decoded_instruction to be something like:
>
> int x86_emulate_decoded_instruction(struct kvm_vcpu *vcpu,
> gpa_t cr2_or_gpa,
> int emulation_type, void *insn,
> int insn_len,
> bool write_fault_to_spt)

An idea is to making the body of the new function just

init_emulate_ctxt(vcpu);

/*
* We will reenter on the same instruction since
* we do not set complete_userspace_io. This does not
* handle watchpoints yet, those would be handled in
* the emulate_ops.
*/
if (!(emulation_type & EMULTYPE_SKIP) &&
kvm_vcpu_check_breakpoint(vcpu, &r))
return r;

ctxt->interruptibility = 0;
ctxt->have_exception = false;
ctxt->exception.vector = -1;
ctxt->exception.error_code_valid = false;

ctxt->perm_ok = false;

ctxt->ud = emulation_type & EMULTYPE_TRAP_UD;

r = x86_decode_insn(ctxt, insn, insn_len);

trace_kvm_emulate_insn_start(vcpu);
++vcpu->stat.insn_emulation;
return r;

because for the new caller, on EMULATION_FAILED you can just re-enter
the guest.

> And if so, what is the emulation type to use when calling this function
> from svm.c? EMULTYPE_VMWARE_GP?

Just 0 I think.

Paolo