Received: by 2002:a05:6a10:5bc5:0:0:0:0 with SMTP id os5csp4002877pxb; Tue, 2 Nov 2021 02:16:59 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxnm1//F4x+zw0EjNdrmMAQCfdj139MW9J34UFAokeODYT8UzOYq6eqcsitvyFT4zLDI3BT X-Received: by 2002:a05:6638:d90:: with SMTP id l16mr26536198jaj.36.1635844619490; Tue, 02 Nov 2021 02:16:59 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1635844619; cv=none; d=google.com; s=arc-20160816; b=SmdazlHkoc1uO4/5vbkReNxSygRwIaDZNYpv1tF1CnTE5RcEYLOgxEF0LyGMbhwfs9 3/1qlDwZzUdft/ATFJKzJoPI/NvBwTEbynFK+8mzwOUOhamzQGINg61BdJ6K0LRV1ix8 UVT0t32S/D7ae2oyjL+FrzScSvQWK7B7Ef03N/kqCzWfLekK+GGYB8hrPwee3Zz40Y6p BtgIZMoTOlWQ0NnzZRvm9LAxazofL/a1AqW4LMHJMmlPorA5eLqsI20pjwIIJHlSGM4V SRzHkPnOFjxx/jWh6IoIkN7Gf4huxlUCX7/sHK3j3V1W1E877t82HiEMfclMpNXvpzVM VJHA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=O09pQymUNCTL7bI/qsT9un4BwBuJ5ASAV5ybPG9d4ms=; b=xfkUIDESLvMnrB9Gt6vXhq8/ILYGeITPG1DpyugSw4V1IzoHKt/YcPZfiB+QORe9Ri N0U1dIWPj8UX/iyq2MiDMpE65a9yp7OipsPcqRTTtqeal7tx9Gjr9BHSQRsfDmVPWV/T Pp9F6V+2FZBYmijngFmkiljiLncez8G3HrR1xq6mFZOlReIYoGUn9XP7gUa2BxeYD8As TJzjDyLJcvGGfeRd8ic906xawTSbDj1h9I487uNiNOy7EwzGmslVZzc2HfAwybcg/7Tz sxOGxo60iXmzEGq6Hzk8GWKUokMxloBRVlgtHJSwLpt7z5fFivmOPOwlvmegoO0EylHL XRxg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=alibaba.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id a5si7230830jat.39.2021.11.02.02.16.47; Tue, 02 Nov 2021 02:16:59 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=alibaba.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231189AbhKBJSP (ORCPT + 99 others); Tue, 2 Nov 2021 05:18:15 -0400 Received: from out30-131.freemail.mail.aliyun.com ([115.124.30.131]:34012 "EHLO out30-131.freemail.mail.aliyun.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229720AbhKBJSL (ORCPT ); Tue, 2 Nov 2021 05:18:11 -0400 X-Alimail-AntiSpam: AC=PASS;BC=-1|-1;BR=01201311R111e4;CH=green;DM=||false|;DS=||;FP=0|-1|-1|-1|0|-1|-1|-1;HT=e01e04423;MF=houwenlong93@linux.alibaba.com;NM=1;PH=DS;RN=13;SR=0;TI=SMTPD_---0UujaDv1_1635844534; Received: from localhost(mailfrom:houwenlong93@linux.alibaba.com fp:SMTPD_---0UujaDv1_1635844534) by smtp.aliyun-inc.com(127.0.0.1); Tue, 02 Nov 2021 17:15:35 +0800 From: Hou Wenlong To: kvm@vger.kernel.org Cc: Sean Christopherson , Paolo Bonzini , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Joerg Roedel , Thomas Gleixner , Ingo Molnar , Borislav Petkov , x86@kernel.org, "H. Peter Anvin" , linux-kernel@vger.kernel.org Subject: [PATCH v2 2/4] KVM: x86: Add an emulation type to handle completion of user exits Date: Tue, 2 Nov 2021 17:15:30 +0800 Message-Id: <8f8c8e268b65f31d55c2881a4b30670946ecfa0d.1635842679.git.houwenlong93@linux.alibaba.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org The next patch would use kvm_emulate_instruction() with EMULTYPE_SKIP in complete_userspace_io callback to fix a problem in msr access emulation. However, EMULTYPE_SKIP only updates RIP, more things like updating interruptibility state and injecting single-step #DBs would be done in the callback. Since the emulator also does those things after x86_emulate_insn(), add a new emulation type to pair with EMULTYPE_SKIP to do those things for completion of user exits within the emulator. Suggested-by: Sean Christopherson Signed-off-by: Hou Wenlong --- arch/x86/include/asm/kvm_host.h | 8 +++++++- arch/x86/kvm/x86.c | 13 ++++++++++--- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h index 88fce6ab4bbd..b99e62b39097 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -1635,7 +1635,8 @@ extern u64 kvm_mce_cap_supported; * * EMULTYPE_SKIP - Set when emulating solely to skip an instruction, i.e. to * decode the instruction length. For use *only* by - * kvm_x86_ops.skip_emulated_instruction() implementations. + * kvm_x86_ops.skip_emulated_instruction() implementations if + * EMULTYPE_COMPLETE_USER_EXIT is not set. * * EMULTYPE_ALLOW_RETRY_PF - Set when the emulator should resume the guest to * retry native execution under certain conditions, @@ -1655,6 +1656,10 @@ extern u64 kvm_mce_cap_supported; * * EMULTYPE_PF - Set when emulating MMIO by way of an intercepted #PF, in which * case the CR2/GPA value pass on the stack is valid. + * + * EMULTYPE_COMPLETE_USER_EXIT - Set when the emulator should update interruptibility + * state and inject single-step #DBs after skipping + * an instruction (after completing userspace I/O). */ #define EMULTYPE_NO_DECODE (1 << 0) #define EMULTYPE_TRAP_UD (1 << 1) @@ -1663,6 +1668,7 @@ extern u64 kvm_mce_cap_supported; #define EMULTYPE_TRAP_UD_FORCED (1 << 4) #define EMULTYPE_VMWARE_GP (1 << 5) #define EMULTYPE_PF (1 << 6) +#define EMULTYPE_COMPLETE_USER_EXIT (1 << 7) int kvm_emulate_instruction(struct kvm_vcpu *vcpu, int emulation_type); int kvm_emulate_instruction_from_buffer(struct kvm_vcpu *vcpu, diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 3d7fc5c21ceb..a961b49c8c44 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -8119,9 +8119,10 @@ int x86_emulate_instruction(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa, } /* - * Note, EMULTYPE_SKIP is intended for use *only* by vendor callbacks - * for kvm_skip_emulated_instruction(). The caller is responsible for - * updating interruptibility state and injecting single-step #DBs. + * EMULTYPE_SKIP without EMULTYPE_COMPLETE_USER_EXIT is intended for + * use *only* by vendor callbacks for kvm_skip_emulated_instruction(). + * The caller is responsible for updating interruptibility state and + * injecting single-step #DBs. */ if (emulation_type & EMULTYPE_SKIP) { if (ctxt->mode != X86EMUL_MODE_PROT64) @@ -8129,6 +8130,11 @@ int x86_emulate_instruction(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa, else ctxt->eip = ctxt->_eip; + if (emulation_type & EMULTYPE_COMPLETE_USER_EXIT) { + r = 1; + goto writeback; + } + kvm_rip_write(vcpu, ctxt->eip); if (ctxt->eflags & X86_EFLAGS_RF) kvm_set_rflags(vcpu, ctxt->eflags & ~X86_EFLAGS_RF); @@ -8198,6 +8204,7 @@ int x86_emulate_instruction(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa, else r = 1; +writeback: if (writeback) { unsigned long rflags = static_call(kvm_x86_get_rflags)(vcpu); toggle_interruptibility(vcpu, ctxt->interruptibility); -- 2.31.1