Re: [PATCH RESEND] kvm/x86: propagate fetch fault into guest

From: Paolo Bonzini
Date: Thu Sep 20 2018 - 12:32:21 EST


On 19/09/2018 16:33, Sean Christopherson wrote:
> On Tue, 2018-09-18 at 19:03 -0400, Peng Hao wrote:
>> From: Peng Hao <peng.hao2@xxxxxxxxxx>
>>
>> When handling ept misconfig exit, it will call emulate instruction
>> with insn_len = 0. The decode instruction function may return a fetch
>> fault and should propagate to guest.
>
> What fault is encountered, e.g. #PF, #UD, etc...? ÂSomething like a
> #UD at this point should result in an exit to userspace as it means
> KVM was unable to emulate an instruction that was successfully
> decoded by hardware.

If it's the same bug as https://lore.kernel.org/patchwork/patch/850077/
it would be a #PF.

Paolo

>> The problem will result to emulation fail.
>> KVM internal error. Suberror: 1
>> emulation failure
>> EAX=f81a0024 EBX=f6a07000 ECX=f6a0737c EDX=f8be0118
>> ESI=f6a0737c EDI=00000021 EBP=f6929f98 ESP=f6929f98
>> EIP=f8bdd141 EFL=00010086 [--S--P-] CPL=0 II=0 A20=1 SMM=0 HLT=0
>> ES =007b 00000000 ffffffff 00c0f300 DPL=3 DSÂÂÂ[-WA]
>> CS =0060 00000000 ffffffff 00c09b00 DPL=0 CS32 [-RA]
>> SS =0068 00000000 ffffffff 00c09300 DPL=0 DSÂÂÂ[-WA]
>> DS =007b 00000000 ffffffff 00c0f300 DPL=3 DSÂÂÂ[-WA]
>> FS =00d8 2c044000 ffffffff 00809300 DPL=0 DS16 [-WA]
>> GS =0033 081a44c8 01000fff 00d0f300 DPL=3 DSÂÂÂ[-WA]
>> LDT=0000 00000000 ffffffff 00000000
>> TR =0080 f6ea0c80 0000206b 00008b00 DPL=0 TSS32-busy
>> GDT=ÂÂÂÂÂf6e99000 000000ff
>> IDT=ÂÂÂÂÂfffbb000 000007ff
>> CR0=80050033 CR2=b757d000 CR3=35d31000 CR4=001406d0
>>
>> Signed-off-by: Peng Hao <peng.hao2@xxxxxxxxxx>
>> Reviewed-by: Jiang Biao <jiang.biao2@xxxxxxxxxx>
>> ---
>> Âarch/x86/kvm/emulate.c | 5 ++++-
>> Âarch/x86/kvm/x86.cÂÂÂÂÂ| 4 +++-
>> Â2 files changed, 7 insertions(+), 2 deletions(-)
>>
>> diff --git a/arch/x86/kvm/emulate.c b/arch/x86/kvm/emulate.c
>> index 106482d..aecf9a72 100644
>> --- a/arch/x86/kvm/emulate.c
>> +++ b/arch/x86/kvm/emulate.c
>> @@ -5105,8 +5105,11 @@ int x86_decode_insn(struct x86_emulate_ctxt *ctxt, void *insn, int insn_len)
>> Â memcpy(ctxt->fetch.data, insn, insn_len);
>> Â else {
>> Â rc = __do_insn_fetch_bytes(ctxt, 1);
>> - if (rc != X86EMUL_CONTINUE)
>> + if (rc != X86EMUL_CONTINUE) {
>> + if (rc == X86EMUL_PROPAGATE_FAULT)
>> + ctxt->have_exception = true;
>> Â return rc;
>> + }
>> Â }
>> Â
>> Â switch (mode) {
>> diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
>> index 542f631..27072e1 100644
>> --- a/arch/x86/kvm/x86.c
>> +++ b/arch/x86/kvm/x86.c
>> @@ -6188,8 +6188,10 @@ int x86_emulate_instruction(struct kvm_vcpu *vcpu,
>> Â if (reexecute_instruction(vcpu, cr2, write_fault_to_spt,
>> Â emulation_type))
>> Â return EMULATE_DONE;
>> - if (ctxt->have_exception && inject_emulated_exception(vcpu))
>> + if (ctxt->have_exception) {
>> + inject_emulated_exception(vcpu);
>> Â return EMULATE_DONE;
>> + }
>> Â if (emulation_type & EMULTYPE_SKIP)
>> Â return EMULATE_FAIL;
>> Â return handle_emulation_failure(vcpu, emulation_type);