From patchwork Mon Apr 12 13:09:32 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Edmondson X-Patchwork-Id: 12198021 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,UNPARSEABLE_RELAY,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 53042C433ED for ; Mon, 12 Apr 2021 13:09:53 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 2EE206128E for ; Mon, 12 Apr 2021 13:09:53 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S241805AbhDLNKH (ORCPT ); Mon, 12 Apr 2021 09:10:07 -0400 Received: from wforward5-smtp.messagingengine.com ([64.147.123.35]:38679 "EHLO wforward5-smtp.messagingengine.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S241764AbhDLNKE (ORCPT ); Mon, 12 Apr 2021 09:10:04 -0400 Received: from compute7.internal (compute7.nyi.internal [10.202.2.47]) by mailforward.west.internal (Postfix) with ESMTP id 388C41646; Mon, 12 Apr 2021 09:09:44 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute7.internal (MEProxy); Mon, 12 Apr 2021 09:09:45 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-transfer-encoding:date:from :in-reply-to:message-id:mime-version:references:subject:to :x-me-proxy:x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s= fm2; bh=rxrObZQedU/z7tCgm77WVlD0YetrXNOgn2ee61sfp4A=; b=joNyhsO/ 3wrGui5X+5el2vu/d76fkIdTYIgjhtWpcos5V8fuUWdrIJtIBsItrBWU3ZNRBqVP BQLJngzpyReV6PH+1j9ezqNaeU0M/lIYD4fyVYF9URb9xR5NTiyU/0eCq13SI3H3 sVqBAz0S12xkRrfIwx1chSdrj6CSiSreSYfadhNzSNh22nbCb84TkRIkA2iUQ5Ki z2Yeu2uaE8BVr+sOquxppdjbO9f0Ff6e5SfOjHqcSJOX77vn/oc/usKrKheNeNRd tcllnQPaiH89VjrzdcSu8Ez8RHLyVdkONUq/EqmntnLDO0HQC9xN9em/gGf9Knt2 Z0P1SSI15HsCbQ== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeduledrudekjedgiedvucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhephffvufffkffojghfggfgsedtkeertdertddtnecuhfhrohhmpeffrghvihgu ucfgughmohhnughsohhnuceouggrvhhiugdrvggumhhonhgushhonhesohhrrggtlhgvrd gtohhmqeenucggtffrrghtthgvrhhnpedufeetjefgfefhtdejhfehtdfftefhteekhefg leehfffhiefhgeelgfejtdehkeenucfkphepkedurddukeejrddviedrvdefkeenucevlh hushhtvghrufhiiigvpedtnecurfgrrhgrmhepmhgrihhlfhhrohhmpegurghvihgurdgv ughmohhnughsohhnsehorhgrtghlvgdrtghomh X-ME-Proxy: Received: from disaster-area.hh.sledj.net (disaster-area.hh.sledj.net [81.187.26.238]) by mail.messagingengine.com (Postfix) with ESMTPA id 02AEE1080066; Mon, 12 Apr 2021 09:09:40 -0400 (EDT) Received: from localhost (disaster-area.hh.sledj.net [local]) by disaster-area.hh.sledj.net (OpenSMTPD) with ESMTPA id f21f3cb5; Mon, 12 Apr 2021 13:09:38 +0000 (UTC) From: David Edmondson To: linux-kernel@vger.kernel.org Cc: kvm@vger.kernel.org, Paolo Bonzini , Vitaly Kuznetsov , "H. Peter Anvin" , x86@kernel.org, Sean Christopherson , Thomas Gleixner , Joerg Roedel , Wanpeng Li , Borislav Petkov , Jim Mattson , Ingo Molnar , Joao Martins , David Edmondson Subject: [PATCH 1/6] KVM: x86: add an emulation_reason to x86_emulate_instruction() Date: Mon, 12 Apr 2021 14:09:32 +0100 Message-Id: <20210412130938.68178-2-david.edmondson@oracle.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20210412130938.68178-1-david.edmondson@oracle.com> References: <20210412130938.68178-1-david.edmondson@oracle.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org From: Joao Martins Emulation can happen for a variety of reasons, yet on error we have no idea exactly what triggered it. Expand x86_emulate_instruction() to carry an @emulation_reason argument. Signed-off-by: Joao Martins Signed-off-by: David Edmondson --- arch/x86/kvm/mmu/mmu.c | 4 ++-- arch/x86/kvm/x86.c | 7 ++++--- arch/x86/kvm/x86.h | 3 ++- 3 files changed, 8 insertions(+), 6 deletions(-) diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c index 951dae4e7175..515ff790b7c5 100644 --- a/arch/x86/kvm/mmu/mmu.c +++ b/arch/x86/kvm/mmu/mmu.c @@ -5056,8 +5056,8 @@ int kvm_mmu_page_fault(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa, u64 error_code, if (!mmio_info_in_cache(vcpu, cr2_or_gpa, direct) && !is_guest_mode(vcpu)) emulation_type |= EMULTYPE_ALLOW_RETRY_PF; emulate: - return x86_emulate_instruction(vcpu, cr2_or_gpa, emulation_type, insn, - insn_len); + return x86_emulate_instruction(vcpu, cr2_or_gpa, emulation_type, 0, + insn, insn_len); } EXPORT_SYMBOL_GPL(kvm_mmu_page_fault); diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index eca63625aee4..87e76f3aee64 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -7445,7 +7445,8 @@ int x86_decode_emulated_instruction(struct kvm_vcpu *vcpu, int emulation_type, EXPORT_SYMBOL_GPL(x86_decode_emulated_instruction); int x86_emulate_instruction(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa, - int emulation_type, void *insn, int insn_len) + int emulation_type, int emulation_reason, + void *insn, int insn_len) { int r; struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt; @@ -7604,14 +7605,14 @@ int x86_emulate_instruction(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa, int kvm_emulate_instruction(struct kvm_vcpu *vcpu, int emulation_type) { - return x86_emulate_instruction(vcpu, 0, emulation_type, NULL, 0); + return x86_emulate_instruction(vcpu, 0, emulation_type, 0, NULL, 0); } EXPORT_SYMBOL_GPL(kvm_emulate_instruction); int kvm_emulate_instruction_from_buffer(struct kvm_vcpu *vcpu, void *insn, int insn_len) { - return x86_emulate_instruction(vcpu, 0, 0, insn, insn_len); + return x86_emulate_instruction(vcpu, 0, 0, 0, insn, insn_len); } EXPORT_SYMBOL_GPL(kvm_emulate_instruction_from_buffer); diff --git a/arch/x86/kvm/x86.h b/arch/x86/kvm/x86.h index 9035e34aa156..5686436c99da 100644 --- a/arch/x86/kvm/x86.h +++ b/arch/x86/kvm/x86.h @@ -276,7 +276,8 @@ void kvm_fixup_and_inject_pf_error(struct kvm_vcpu *vcpu, gva_t gva, u16 error_c int x86_decode_emulated_instruction(struct kvm_vcpu *vcpu, int emulation_type, void *insn, int insn_len); int x86_emulate_instruction(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa, - int emulation_type, void *insn, int insn_len); + int emulation_type, int emulation_reason, + void *insn, int insn_len); fastpath_t handle_fastpath_set_msr_irqoff(struct kvm_vcpu *vcpu); extern u64 host_xcr0; From patchwork Mon Apr 12 13:09:33 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Edmondson X-Patchwork-Id: 12198019 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,UNPARSEABLE_RELAY,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 1CEC6C43460 for ; Mon, 12 Apr 2021 13:09:50 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id E842C6128E for ; Mon, 12 Apr 2021 13:09:49 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S241791AbhDLNKF (ORCPT ); Mon, 12 Apr 2021 09:10:05 -0400 Received: from wforward5-smtp.messagingengine.com ([64.147.123.35]:55157 "EHLO wforward5-smtp.messagingengine.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S241746AbhDLNKD (ORCPT ); Mon, 12 Apr 2021 09:10:03 -0400 Received: from compute5.internal (compute5.nyi.internal [10.202.2.45]) by mailforward.west.internal (Postfix) with ESMTP id CA7041664; Mon, 12 Apr 2021 09:09:43 -0400 (EDT) Received: from mailfrontend1 ([10.202.2.162]) by compute5.internal (MEProxy); Mon, 12 Apr 2021 09:09:44 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-transfer-encoding:date:from :in-reply-to:message-id:mime-version:references:subject:to :x-me-proxy:x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s= fm2; bh=XPQNUQKEXupnV63KWZ9Hhwvi3ppqpPrjyh3rpylALHE=; b=aM0WZ6Oq GYhemC17mMmRqflfeG+bRPqUL3JAwGcaMQP75i0iYjXJdoH0x/1MKrRBeNj5xJL0 qkmo7XzwxBh5Y+RsAaLj6VhvtB+oEw0ZuRp4EPmfxZf+Nqw70jOMfQ7JBnDp7T7i 2OjPBjUlLRhOwar2fVCewM+xZ4D1W256pRPOY1r87EY4ljB9ThxKsCq7aRED+YXr 4woCEEgD2m2qPTuyxyYhI+W0JzvfJ/MOzN5Z5Ws3+TOOvEXJXBld4Di7huHLtYiS jLodJWKtQOu0Aa3cbGSTEacx0OzzSz97eI38igykUBmxgHmbZAEzB73+aBtOmNgj OFuSFurOTH064g== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeduledrudekjedgiedvucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhephffvufffkffojghfggfgsedtkeertdertddtnecuhfhrohhmpeffrghvihgu ucfgughmohhnughsohhnuceouggrvhhiugdrvggumhhonhgushhonhesohhrrggtlhgvrd gtohhmqeenucggtffrrghtthgvrhhnpedufeetjefgfefhtdejhfehtdfftefhteekhefg leehfffhiefhgeelgfejtdehkeenucfkphepkedurddukeejrddviedrvdefkeenucevlh hushhtvghrufhiiigvpedtnecurfgrrhgrmhepmhgrihhlfhhrohhmpegurghvihgurdgv ughmohhnughsohhnsehorhgrtghlvgdrtghomh X-ME-Proxy: Received: from disaster-area.hh.sledj.net (disaster-area.hh.sledj.net [81.187.26.238]) by mail.messagingengine.com (Postfix) with ESMTPA id 3A00F240066; Mon, 12 Apr 2021 09:09:41 -0400 (EDT) Received: from localhost (disaster-area.hh.sledj.net [local]) by disaster-area.hh.sledj.net (OpenSMTPD) with ESMTPA id d79b2990; Mon, 12 Apr 2021 13:09:38 +0000 (UTC) From: David Edmondson To: linux-kernel@vger.kernel.org Cc: kvm@vger.kernel.org, Paolo Bonzini , Vitaly Kuznetsov , "H. Peter Anvin" , x86@kernel.org, Sean Christopherson , Thomas Gleixner , Joerg Roedel , Wanpeng Li , Borislav Petkov , Jim Mattson , Ingo Molnar , Joao Martins , David Edmondson Subject: [PATCH 2/6] KVM: x86: pass emulation_reason to handle_emulation_failure() Date: Mon, 12 Apr 2021 14:09:33 +0100 Message-Id: <20210412130938.68178-3-david.edmondson@oracle.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20210412130938.68178-1-david.edmondson@oracle.com> References: <20210412130938.68178-1-david.edmondson@oracle.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org From: Joao Martins Make the emulation_reason available up stack when reporting an emulation failure. Signed-off-by: Joao Martins Signed-off-by: David Edmondson --- arch/x86/kvm/x86.c | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 87e76f3aee64..b9225012ebd2 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -7119,7 +7119,8 @@ void kvm_inject_realmode_interrupt(struct kvm_vcpu *vcpu, int irq, int inc_eip) } EXPORT_SYMBOL_GPL(kvm_inject_realmode_interrupt); -static int handle_emulation_failure(struct kvm_vcpu *vcpu, int emulation_type) +static int handle_emulation_failure(struct kvm_vcpu *vcpu, int emulation_type, + int emulation_reason) { ++vcpu->stat.insn_emulation_fail; trace_kvm_emulate_insn_failed(vcpu); @@ -7132,7 +7133,8 @@ static int handle_emulation_failure(struct kvm_vcpu *vcpu, int emulation_type) if (emulation_type & EMULTYPE_SKIP) { vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR; vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION; - vcpu->run->internal.ndata = 0; + vcpu->run->internal.data[0] = emulation_reason; + vcpu->run->internal.ndata = 1; return 0; } @@ -7141,7 +7143,8 @@ static int handle_emulation_failure(struct kvm_vcpu *vcpu, int emulation_type) if (!is_guest_mode(vcpu) && static_call(kvm_x86_get_cpl)(vcpu) == 0) { vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR; vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION; - vcpu->run->internal.ndata = 0; + vcpu->run->internal.data[0] = emulation_reason; + vcpu->run->internal.ndata = 1; return 0; } @@ -7490,7 +7493,8 @@ int x86_emulate_instruction(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa, inject_emulated_exception(vcpu); return 1; } - return handle_emulation_failure(vcpu, emulation_type); + return handle_emulation_failure(vcpu, emulation_type, + emulation_reason); } } @@ -7547,7 +7551,8 @@ int x86_emulate_instruction(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa, emulation_type)) return 1; - return handle_emulation_failure(vcpu, emulation_type); + return handle_emulation_failure(vcpu, emulation_type, + emulation_reason); } if (ctxt->have_exception) { From patchwork Mon Apr 12 13:09:34 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Edmondson X-Patchwork-Id: 12198025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,UNPARSEABLE_RELAY,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id C18C0C43462 for ; Mon, 12 Apr 2021 13:09:55 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 7A3E06128E for ; Mon, 12 Apr 2021 13:09:55 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S241811AbhDLNKM (ORCPT ); Mon, 12 Apr 2021 09:10:12 -0400 Received: from forward3-smtp.messagingengine.com ([66.111.4.237]:34335 "EHLO forward3-smtp.messagingengine.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S241780AbhDLNKG (ORCPT ); Mon, 12 Apr 2021 09:10:06 -0400 Received: from compute2.internal (compute2.nyi.internal [10.202.2.42]) by mailforward.nyi.internal (Postfix) with ESMTP id E0CF61940D17; Mon, 12 Apr 2021 09:09:46 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute2.internal (MEProxy); Mon, 12 Apr 2021 09:09:46 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-transfer-encoding:date:from :in-reply-to:message-id:mime-version:references:subject:to :x-me-proxy:x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s= fm2; bh=PNPHfpaBHzXgU4hdFtTS8uZfK8FmZhnLclHnBghCsfo=; b=Svz0+dXo UwdN98T7fwKmqgNjKwpM+ifVjaK22nmQDZ7Q9iGXRztZOZNj9T3dcMSyIQibYPBI 4WW1QPTTrsInZuoO7FfHDIQosFg0nOorMkT9qRIZ+zQkqjQrSHouTb4pTs0GZJvH eacW1K0vZQi0Ud/1PutZ41+7BUVPTVuAnb3fAj+pKDl7S8q7XN3zLLcx+YZk0GIF r/DhOFtF1v4xFhY+eeS8YNVF/7rF5ch1RUw+bsiEzbNilnunKFxQ/RKnNGlzSVBS 7R72tFtSHQhnhPSi+6uKwy3ilVKLI1uDiCK0tJXAky25F9PuzvioVWCX6C6dkhvy 7HobhbYYm1XsMQ== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeduledrudekjedgieduucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhephffvufffkffojghfggfgsedtkeertdertddtnecuhfhrohhmpeffrghvihgu ucfgughmohhnughsohhnuceouggrvhhiugdrvggumhhonhgushhonhesohhrrggtlhgvrd gtohhmqeenucggtffrrghtthgvrhhnpedufeetjefgfefhtdejhfehtdfftefhteekhefg leehfffhiefhgeelgfejtdehkeenucfkphepkedurddukeejrddviedrvdefkeenucevlh hushhtvghrufhiiigvpedtnecurfgrrhgrmhepmhgrihhlfhhrohhmpegurghvihgurdgv ughmohhnughsohhnsehorhgrtghlvgdrtghomh X-ME-Proxy: Received: from disaster-area.hh.sledj.net (disaster-area.hh.sledj.net [81.187.26.238]) by mail.messagingengine.com (Postfix) with ESMTPA id 5A20C108005F; Mon, 12 Apr 2021 09:09:42 -0400 (EDT) Received: from localhost (disaster-area.hh.sledj.net [local]) by disaster-area.hh.sledj.net (OpenSMTPD) with ESMTPA id 213529a7; Mon, 12 Apr 2021 13:09:38 +0000 (UTC) From: David Edmondson To: linux-kernel@vger.kernel.org Cc: kvm@vger.kernel.org, Paolo Bonzini , Vitaly Kuznetsov , "H. Peter Anvin" , x86@kernel.org, Sean Christopherson , Thomas Gleixner , Joerg Roedel , Wanpeng Li , Borislav Petkov , Jim Mattson , Ingo Molnar , Joao Martins , David Edmondson Subject: [PATCH 3/6] KVM: x86: add emulation_reason to kvm_emulate_instruction() Date: Mon, 12 Apr 2021 14:09:34 +0100 Message-Id: <20210412130938.68178-4-david.edmondson@oracle.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20210412130938.68178-1-david.edmondson@oracle.com> References: <20210412130938.68178-1-david.edmondson@oracle.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org From: Joao Martins Allow kvm_emulate_instruction() callers to provide the cause of instruction emulation, which is then passed along to x86_emulation_instruction(). Signed-off-by: Joao Martins Signed-off-by: David Edmondson --- arch/x86/include/asm/kvm_host.h | 10 ++++++++-- arch/x86/kvm/svm/avic.c | 2 +- arch/x86/kvm/svm/svm.c | 12 ++++++------ arch/x86/kvm/vmx/vmx.c | 16 ++++++++-------- arch/x86/kvm/x86.c | 16 ++++++++++------ 5 files changed, 33 insertions(+), 23 deletions(-) diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h index 3768819693e5..556dc51e322a 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -1527,9 +1527,15 @@ extern u64 kvm_mce_cap_supported; #define EMULTYPE_VMWARE_GP (1 << 5) #define EMULTYPE_PF (1 << 6) -int kvm_emulate_instruction(struct kvm_vcpu *vcpu, int emulation_type); +enum { + EMULREASON_UNKNOWN = 0, +}; + +int kvm_emulate_instruction(struct kvm_vcpu *vcpu, int emulation_type, + int emulation_reason); int kvm_emulate_instruction_from_buffer(struct kvm_vcpu *vcpu, - void *insn, int insn_len); + void *insn, int insn_len, + int emulation_reason); void kvm_enable_efer_bits(u64); bool kvm_valid_efer(struct kvm_vcpu *vcpu, u64 efer); diff --git a/arch/x86/kvm/svm/avic.c b/arch/x86/kvm/svm/avic.c index 78bdcfac4e40..31a17fa6a37c 100644 --- a/arch/x86/kvm/svm/avic.c +++ b/arch/x86/kvm/svm/avic.c @@ -558,7 +558,7 @@ int avic_unaccelerated_access_interception(struct vcpu_svm *svm) ret = avic_unaccel_trap_write(svm); } else { /* Handling Fault */ - ret = kvm_emulate_instruction(&svm->vcpu, 0); + ret = kvm_emulate_instruction(&svm->vcpu, 0, 0); } return ret; diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c index 58a45bb139f8..bba3b72390a8 100644 --- a/arch/x86/kvm/svm/svm.c +++ b/arch/x86/kvm/svm/svm.c @@ -344,7 +344,7 @@ static int skip_emulated_instruction(struct kvm_vcpu *vcpu) } if (!svm->next_rip) { - if (!kvm_emulate_instruction(vcpu, EMULTYPE_SKIP)) + if (!kvm_emulate_instruction(vcpu, EMULTYPE_SKIP, 0)) return 0; } else { kvm_rip_write(vcpu, svm->next_rip); @@ -2077,7 +2077,7 @@ static int io_interception(struct vcpu_svm *svm) if (sev_es_guest(vcpu->kvm)) return sev_es_string_io(svm, size, port, in); else - return kvm_emulate_instruction(vcpu, 0); + return kvm_emulate_instruction(vcpu, 0, 0); } svm->next_rip = svm->vmcb->control.exit_info_2; @@ -2263,7 +2263,7 @@ static int gp_interception(struct vcpu_svm *svm) */ if (!is_guest_mode(vcpu)) return kvm_emulate_instruction(vcpu, - EMULTYPE_VMWARE_GP | EMULTYPE_NO_DECODE); + EMULTYPE_VMWARE_GP | EMULTYPE_NO_DECODE, 0); } else return emulate_svm_instr(vcpu, opcode); @@ -2459,7 +2459,7 @@ static int invd_interception(struct vcpu_svm *svm) static int invlpg_interception(struct vcpu_svm *svm) { if (!static_cpu_has(X86_FEATURE_DECODEASSISTS)) - return kvm_emulate_instruction(&svm->vcpu, 0); + return kvm_emulate_instruction(&svm->vcpu, 0, 0); kvm_mmu_invlpg(&svm->vcpu, svm->vmcb->control.exit_info_1); return kvm_skip_emulated_instruction(&svm->vcpu); @@ -2467,12 +2467,12 @@ static int invlpg_interception(struct vcpu_svm *svm) static int emulate_on_interception(struct vcpu_svm *svm) { - return kvm_emulate_instruction(&svm->vcpu, 0); + return kvm_emulate_instruction(&svm->vcpu, 0, 0); } static int rsm_interception(struct vcpu_svm *svm) { - return kvm_emulate_instruction_from_buffer(&svm->vcpu, rsm_ins_bytes, 2); + return kvm_emulate_instruction_from_buffer(&svm->vcpu, rsm_ins_bytes, 2, 0); } static int rdpmc_interception(struct vcpu_svm *svm) diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c index 32cf8287d4a7..037b01b5a54b 100644 --- a/arch/x86/kvm/vmx/vmx.c +++ b/arch/x86/kvm/vmx/vmx.c @@ -1600,7 +1600,7 @@ static int skip_emulated_instruction(struct kvm_vcpu *vcpu) #endif kvm_rip_write(vcpu, rip); } else { - if (!kvm_emulate_instruction(vcpu, EMULTYPE_SKIP)) + if (!kvm_emulate_instruction(vcpu, EMULTYPE_SKIP, 0)) return 0; } @@ -4738,7 +4738,7 @@ static int handle_rmode_exception(struct kvm_vcpu *vcpu, * Cause the #SS fault with 0 error code in VM86 mode. */ if (((vec == GP_VECTOR) || (vec == SS_VECTOR)) && err_code == 0) { - if (kvm_emulate_instruction(vcpu, 0)) { + if (kvm_emulate_instruction(vcpu, 0, 0)) { if (vcpu->arch.halt_request) { vcpu->arch.halt_request = 0; return kvm_vcpu_halt(vcpu); @@ -4816,7 +4816,7 @@ static int handle_exception_nmi(struct kvm_vcpu *vcpu) kvm_queue_exception_e(vcpu, GP_VECTOR, error_code); return 1; } - return kvm_emulate_instruction(vcpu, EMULTYPE_VMWARE_GP); + return kvm_emulate_instruction(vcpu, EMULTYPE_VMWARE_GP, 0); } /* @@ -4930,7 +4930,7 @@ static int handle_io(struct kvm_vcpu *vcpu) ++vcpu->stat.io_exits; if (string) - return kvm_emulate_instruction(vcpu, 0); + return kvm_emulate_instruction(vcpu, 0, 0); port = exit_qualification >> 16; size = (exit_qualification & 7) + 1; @@ -5004,7 +5004,7 @@ static int handle_set_cr4(struct kvm_vcpu *vcpu, unsigned long val) static int handle_desc(struct kvm_vcpu *vcpu) { WARN_ON(!(vcpu->arch.cr4 & X86_CR4_UMIP)); - return kvm_emulate_instruction(vcpu, 0); + return kvm_emulate_instruction(vcpu, 0, 0); } static int handle_cr(struct kvm_vcpu *vcpu) @@ -5244,7 +5244,7 @@ static int handle_apic_access(struct kvm_vcpu *vcpu) return kvm_skip_emulated_instruction(vcpu); } } - return kvm_emulate_instruction(vcpu, 0); + return kvm_emulate_instruction(vcpu, 0, 0); } static int handle_apic_eoi_induced(struct kvm_vcpu *vcpu) @@ -5375,7 +5375,7 @@ static int handle_ept_violation(struct kvm_vcpu *vcpu) * reconstruct the page fault error code. */ if (unlikely(allow_smaller_maxphyaddr && kvm_vcpu_is_illegal_gpa(vcpu, gpa))) - return kvm_emulate_instruction(vcpu, 0); + return kvm_emulate_instruction(vcpu, 0, 0); return kvm_mmu_page_fault(vcpu, gpa, error_code, NULL, 0); } @@ -5424,7 +5424,7 @@ static int handle_invalid_guest_state(struct kvm_vcpu *vcpu) if (kvm_test_request(KVM_REQ_EVENT, vcpu)) return 1; - if (!kvm_emulate_instruction(vcpu, 0)) + if (!kvm_emulate_instruction(vcpu, 0, 0)) return 0; if (vmx->emulation_required && !vmx->rmode.vm86_active && diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index b9225012ebd2..88519bf6bd00 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -6197,7 +6197,7 @@ int handle_ud(struct kvm_vcpu *vcpu) emul_type = EMULTYPE_TRAP_UD_FORCED; } - return kvm_emulate_instruction(vcpu, emul_type); + return kvm_emulate_instruction(vcpu, emul_type, 0); } EXPORT_SYMBOL_GPL(handle_ud); @@ -7608,16 +7608,20 @@ int x86_emulate_instruction(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa, return r; } -int kvm_emulate_instruction(struct kvm_vcpu *vcpu, int emulation_type) +int kvm_emulate_instruction(struct kvm_vcpu *vcpu, int emulation_type, + int emulation_reason) { - return x86_emulate_instruction(vcpu, 0, emulation_type, 0, NULL, 0); + return x86_emulate_instruction(vcpu, 0, emulation_type, + emulation_reason, NULL, 0); } EXPORT_SYMBOL_GPL(kvm_emulate_instruction); int kvm_emulate_instruction_from_buffer(struct kvm_vcpu *vcpu, - void *insn, int insn_len) + void *insn, int insn_len, + int emulation_reason) { - return x86_emulate_instruction(vcpu, 0, 0, 0, insn, insn_len); + return x86_emulate_instruction(vcpu, 0, 0, + emulation_reason, insn, insn_len); } EXPORT_SYMBOL_GPL(kvm_emulate_instruction_from_buffer); @@ -9339,7 +9343,7 @@ static inline int complete_emulated_io(struct kvm_vcpu *vcpu) int r; vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu); - r = kvm_emulate_instruction(vcpu, EMULTYPE_NO_DECODE); + r = kvm_emulate_instruction(vcpu, EMULTYPE_NO_DECODE, 0); srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx); return r; } From patchwork Mon Apr 12 13:09:35 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Edmondson X-Patchwork-Id: 12198023 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,UNPARSEABLE_RELAY,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 9259AC433B4 for ; Mon, 12 Apr 2021 13:09:54 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 63F6A6128E for ; Mon, 12 Apr 2021 13:09:54 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S241833AbhDLNKK (ORCPT ); Mon, 12 Apr 2021 09:10:10 -0400 Received: from wforward5-smtp.messagingengine.com ([64.147.123.35]:51081 "EHLO wforward5-smtp.messagingengine.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S241768AbhDLNKE (ORCPT ); Mon, 12 Apr 2021 09:10:04 -0400 Received: from compute4.internal (compute4.nyi.internal [10.202.2.44]) by mailforward.west.internal (Postfix) with ESMTP id 66D4B16A9; Mon, 12 Apr 2021 09:09:44 -0400 (EDT) Received: from mailfrontend1 ([10.202.2.162]) by compute4.internal (MEProxy); Mon, 12 Apr 2021 09:09:45 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-transfer-encoding:date:from :in-reply-to:message-id:mime-version:references:subject:to :x-me-proxy:x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s= fm2; bh=1K0iw42KNFYBkLsYKyjD7325njwHj63G+RvjjAS+EuE=; b=R/IakVPV utKtTSlbR0+xhb25rcCLvulXpHR/hFfF3pvjb/uGYY7Kp9bxL00Iz35pdGilK65i J6pS7y2UxVqx6LqdM6IjpzpEs16qVcGDRfHF7gkm8Scc7uL7pJVDBTt7Oq116EH+ UKodGf2pyc2DDSfTTjiKo/Y7v+n1I1qsuOfWOWUGx9PfySM0x2x8lcwXn0L1uZV/ 4j2saZCOT5PQwZDf8Ry+BKd3IntfCGnaBSK4F3311JbMV97WdZ0uqYZaksBfxNgX mp0XYPfn0dAsYD6YypG8a6Wvhv2N5ItLRqOjyZ9tTzsjtaJsQx3z+Jd4EfWnvNVu h3+rHPvDH+EvKA== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeduledrudekjedgiedvucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhephffvufffkffojghfggfgsedtkeertdertddtnecuhfhrohhmpeffrghvihgu ucfgughmohhnughsohhnuceouggrvhhiugdrvggumhhonhgushhonhesohhrrggtlhgvrd gtohhmqeenucggtffrrghtthgvrhhnpedufeetjefgfefhtdejhfehtdfftefhteekhefg leehfffhiefhgeelgfejtdehkeenucfkphepkedurddukeejrddviedrvdefkeenucevlh hushhtvghrufhiiigvpedtnecurfgrrhgrmhepmhgrihhlfhhrohhmpegurghvihgurdgv ughmohhnughsohhnsehorhgrtghlvgdrtghomh X-ME-Proxy: Received: from disaster-area.hh.sledj.net (disaster-area.hh.sledj.net [81.187.26.238]) by mail.messagingengine.com (Postfix) with ESMTPA id 9333B24005D; Mon, 12 Apr 2021 09:09:42 -0400 (EDT) Received: from localhost (disaster-area.hh.sledj.net [local]) by disaster-area.hh.sledj.net (OpenSMTPD) with ESMTPA id db9db3bc; Mon, 12 Apr 2021 13:09:38 +0000 (UTC) From: David Edmondson To: linux-kernel@vger.kernel.org Cc: kvm@vger.kernel.org, Paolo Bonzini , Vitaly Kuznetsov , "H. Peter Anvin" , x86@kernel.org, Sean Christopherson , Thomas Gleixner , Joerg Roedel , Wanpeng Li , Borislav Petkov , Jim Mattson , Ingo Molnar , Joao Martins , David Edmondson Subject: [PATCH 4/6] KVM: x86: pass a proper reason to kvm_emulate_instruction() Date: Mon, 12 Apr 2021 14:09:35 +0100 Message-Id: <20210412130938.68178-5-david.edmondson@oracle.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20210412130938.68178-1-david.edmondson@oracle.com> References: <20210412130938.68178-1-david.edmondson@oracle.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org From: Joao Martins Declare various causes of emulation and use them as appropriate. Signed-off-by: Joao Martins Signed-off-by: David Edmondson --- arch/x86/include/asm/kvm_host.h | 6 ++++++ arch/x86/kvm/mmu/mmu.c | 4 ++-- arch/x86/kvm/x86.c | 5 +++-- 3 files changed, 11 insertions(+), 4 deletions(-) diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h index 556dc51e322a..79e9ca756742 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -1529,6 +1529,12 @@ extern u64 kvm_mce_cap_supported; enum { EMULREASON_UNKNOWN = 0, + EMULREASON_SKIP, + EMULREASON_GP, + EMULREASON_IO, + EMULREASON_IO_COMPLETE, + EMULREASON_UD, + EMULREASON_PF, }; int kvm_emulate_instruction(struct kvm_vcpu *vcpu, int emulation_type, diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c index 515ff790b7c5..14ddf1a5ac12 100644 --- a/arch/x86/kvm/mmu/mmu.c +++ b/arch/x86/kvm/mmu/mmu.c @@ -5056,8 +5056,8 @@ int kvm_mmu_page_fault(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa, u64 error_code, if (!mmio_info_in_cache(vcpu, cr2_or_gpa, direct) && !is_guest_mode(vcpu)) emulation_type |= EMULTYPE_ALLOW_RETRY_PF; emulate: - return x86_emulate_instruction(vcpu, cr2_or_gpa, emulation_type, 0, - insn, insn_len); + return x86_emulate_instruction(vcpu, cr2_or_gpa, emulation_type, + EMULREASON_PF, insn, insn_len); } EXPORT_SYMBOL_GPL(kvm_mmu_page_fault); diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 88519bf6bd00..41020eba8e21 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -6197,7 +6197,7 @@ int handle_ud(struct kvm_vcpu *vcpu) emul_type = EMULTYPE_TRAP_UD_FORCED; } - return kvm_emulate_instruction(vcpu, emul_type, 0); + return kvm_emulate_instruction(vcpu, emul_type, EMULREASON_UD); } EXPORT_SYMBOL_GPL(handle_ud); @@ -9343,7 +9343,8 @@ static inline int complete_emulated_io(struct kvm_vcpu *vcpu) int r; vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu); - r = kvm_emulate_instruction(vcpu, EMULTYPE_NO_DECODE, 0); + r = kvm_emulate_instruction(vcpu, EMULTYPE_NO_DECODE, + EMULREASON_IO_COMPLETE); srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx); return r; } From patchwork Mon Apr 12 13:09:36 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Edmondson X-Patchwork-Id: 12198027 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,UNPARSEABLE_RELAY,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id BB5F3C433ED for ; Mon, 12 Apr 2021 13:09:56 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 858F061355 for ; Mon, 12 Apr 2021 13:09:56 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S241855AbhDLNKN (ORCPT ); Mon, 12 Apr 2021 09:10:13 -0400 Received: from wforward5-smtp.messagingengine.com ([64.147.123.35]:47791 "EHLO wforward5-smtp.messagingengine.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S241785AbhDLNKH (ORCPT ); Mon, 12 Apr 2021 09:10:07 -0400 Received: from compute3.internal (compute3.nyi.internal [10.202.2.43]) by mailforward.west.internal (Postfix) with ESMTP id 466AC16AE; Mon, 12 Apr 2021 09:09:46 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute3.internal (MEProxy); Mon, 12 Apr 2021 09:09:47 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-transfer-encoding:date:from :in-reply-to:message-id:mime-version:references:subject:to :x-me-proxy:x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s= fm2; bh=dN/QNHiyGToQXM6hoRPaubgFTy6JlFGJ5tVi33Nr9Qk=; b=IjPqjmdc fQr6R0d27isL50Bc8F3cCWghdQ9ZYZhM4o6dQEWWTO/BKvW9wbwizTw+NYsk8DTZ b1RrcQf8Q5CdYo5fpMwWkq5LGPwyOnWL7N4n3lBEf70a0s+fNdkvEqAAvov0Rsr3 5sGBrXvfcKXuki1CPACTJAva5xHUmP8R0C6yk7qyIS41LR/WC9nmwsaAXpDEMq0w 7dlI0lAZaylhBJR1GwK+3u2SHI0gQhsIodb2bRqisz5yFQlZeY1vHGnlNeXkN8O3 TvSfYb4yGV7QUeeX22sG04D3/mGI5BXhp01CGyO7MGzXuOzJPdW0I1tjyPFVqOLH mahahAu9RsXL3g== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeduledrudekjedgiedvucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhephffvufffkffojghfggfgsedtkeertdertddtnecuhfhrohhmpeffrghvihgu ucfgughmohhnughsohhnuceouggrvhhiugdrvggumhhonhgushhonhesohhrrggtlhgvrd gtohhmqeenucggtffrrghtthgvrhhnpedufeetjefgfefhtdejhfehtdfftefhteekhefg leehfffhiefhgeelgfejtdehkeenucfkphepkedurddukeejrddviedrvdefkeenucevlh hushhtvghrufhiiigvpedtnecurfgrrhgrmhepmhgrihhlfhhrohhmpegurghvihgurdgv ughmohhnughsohhnsehorhgrtghlvgdrtghomh X-ME-Proxy: Received: from disaster-area.hh.sledj.net (disaster-area.hh.sledj.net [81.187.26.238]) by mail.messagingengine.com (Postfix) with ESMTPA id A921E1080068; Mon, 12 Apr 2021 09:09:43 -0400 (EDT) Received: from localhost (disaster-area.hh.sledj.net [local]) by disaster-area.hh.sledj.net (OpenSMTPD) with ESMTPA id abdc0c66; Mon, 12 Apr 2021 13:09:38 +0000 (UTC) From: David Edmondson To: linux-kernel@vger.kernel.org Cc: kvm@vger.kernel.org, Paolo Bonzini , Vitaly Kuznetsov , "H. Peter Anvin" , x86@kernel.org, Sean Christopherson , Thomas Gleixner , Joerg Roedel , Wanpeng Li , Borislav Petkov , Jim Mattson , Ingo Molnar , Joao Martins , David Edmondson Subject: [PATCH 5/6] KVM: SVM: pass a proper reason in kvm_emulate_instruction() Date: Mon, 12 Apr 2021 14:09:36 +0100 Message-Id: <20210412130938.68178-6-david.edmondson@oracle.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20210412130938.68178-1-david.edmondson@oracle.com> References: <20210412130938.68178-1-david.edmondson@oracle.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org From: Joao Martins Declare various causes of emulation and use them as appropriate. Signed-off-by: Joao Martins Signed-off-by: David Edmondson --- arch/x86/include/asm/kvm_host.h | 6 ++++++ arch/x86/kvm/svm/avic.c | 3 ++- arch/x86/kvm/svm/svm.c | 26 +++++++++++++++----------- 3 files changed, 23 insertions(+), 12 deletions(-) diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h index 79e9ca756742..e1284680cbdc 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -1535,6 +1535,12 @@ enum { EMULREASON_IO_COMPLETE, EMULREASON_UD, EMULREASON_PF, + EMULREASON_SVM_NOASSIST, + EMULREASON_SVM_RSM, + EMULREASON_SVM_RDPMC, + EMULREASON_SVM_CR, + EMULREASON_SVM_DR, + EMULREASON_SVM_AVIC_UNACCEL, }; int kvm_emulate_instruction(struct kvm_vcpu *vcpu, int emulation_type, diff --git a/arch/x86/kvm/svm/avic.c b/arch/x86/kvm/svm/avic.c index 31a17fa6a37c..faa5d4db7ccc 100644 --- a/arch/x86/kvm/svm/avic.c +++ b/arch/x86/kvm/svm/avic.c @@ -558,7 +558,8 @@ int avic_unaccelerated_access_interception(struct vcpu_svm *svm) ret = avic_unaccel_trap_write(svm); } else { /* Handling Fault */ - ret = kvm_emulate_instruction(&svm->vcpu, 0, 0); + ret = kvm_emulate_instruction(&svm->vcpu, 0, + EMULREASON_SVM_AVIC_UNACCEL); } return ret; diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c index bba3b72390a8..2646aa2eae22 100644 --- a/arch/x86/kvm/svm/svm.c +++ b/arch/x86/kvm/svm/svm.c @@ -344,7 +344,8 @@ static int skip_emulated_instruction(struct kvm_vcpu *vcpu) } if (!svm->next_rip) { - if (!kvm_emulate_instruction(vcpu, EMULTYPE_SKIP, 0)) + if (!kvm_emulate_instruction(vcpu, EMULTYPE_SKIP, + EMULREASON_SKIP)) return 0; } else { kvm_rip_write(vcpu, svm->next_rip); @@ -2077,7 +2078,8 @@ static int io_interception(struct vcpu_svm *svm) if (sev_es_guest(vcpu->kvm)) return sev_es_string_io(svm, size, port, in); else - return kvm_emulate_instruction(vcpu, 0, 0); + return kvm_emulate_instruction(vcpu, 0, + EMULREASON_IO); } svm->next_rip = svm->vmcb->control.exit_info_2; @@ -2263,7 +2265,8 @@ static int gp_interception(struct vcpu_svm *svm) */ if (!is_guest_mode(vcpu)) return kvm_emulate_instruction(vcpu, - EMULTYPE_VMWARE_GP | EMULTYPE_NO_DECODE, 0); + EMULTYPE_VMWARE_GP | EMULTYPE_NO_DECODE, + EMULREASON_GP); } else return emulate_svm_instr(vcpu, opcode); @@ -2459,20 +2462,21 @@ static int invd_interception(struct vcpu_svm *svm) static int invlpg_interception(struct vcpu_svm *svm) { if (!static_cpu_has(X86_FEATURE_DECODEASSISTS)) - return kvm_emulate_instruction(&svm->vcpu, 0, 0); + return kvm_emulate_instruction(&svm->vcpu, 0, EMULREASON_SVM_NOASSIST); kvm_mmu_invlpg(&svm->vcpu, svm->vmcb->control.exit_info_1); return kvm_skip_emulated_instruction(&svm->vcpu); } -static int emulate_on_interception(struct vcpu_svm *svm) +static int emulate_on_interception(struct vcpu_svm *svm, int emulation_reason) { - return kvm_emulate_instruction(&svm->vcpu, 0, 0); + return kvm_emulate_instruction(&svm->vcpu, 0, emulation_reason); } static int rsm_interception(struct vcpu_svm *svm) { - return kvm_emulate_instruction_from_buffer(&svm->vcpu, rsm_ins_bytes, 2, 0); + return kvm_emulate_instruction_from_buffer(&svm->vcpu, rsm_ins_bytes, 2, + EMULREASON_SVM_RSM); } static int rdpmc_interception(struct vcpu_svm *svm) @@ -2480,7 +2484,7 @@ static int rdpmc_interception(struct vcpu_svm *svm) int err; if (!nrips) - return emulate_on_interception(svm); + return emulate_on_interception(svm, EMULREASON_SVM_RDPMC); err = kvm_rdpmc(&svm->vcpu); return kvm_complete_insn_gp(&svm->vcpu, err); @@ -2516,10 +2520,10 @@ static int cr_interception(struct vcpu_svm *svm) int err; if (!static_cpu_has(X86_FEATURE_DECODEASSISTS)) - return emulate_on_interception(svm); + return emulate_on_interception(svm, EMULREASON_SVM_NOASSIST); if (unlikely((svm->vmcb->control.exit_info_1 & CR_VALID) == 0)) - return emulate_on_interception(svm); + return emulate_on_interception(svm, EMULREASON_SVM_CR); reg = svm->vmcb->control.exit_info_1 & SVM_EXITINFO_REG_MASK; if (svm->vmcb->control.exit_code == SVM_EXIT_CR0_SEL_WRITE) @@ -2635,7 +2639,7 @@ static int dr_interception(struct vcpu_svm *svm) } if (!boot_cpu_has(X86_FEATURE_DECODEASSISTS)) - return emulate_on_interception(svm); + return emulate_on_interception(svm, EMULREASON_SVM_NOASSIST); reg = svm->vmcb->control.exit_info_1 & SVM_EXITINFO_REG_MASK; dr = svm->vmcb->control.exit_code - SVM_EXIT_READ_DR0; From patchwork Mon Apr 12 13:09:37 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Edmondson X-Patchwork-Id: 12198029 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,UNPARSEABLE_RELAY,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 55EDFC43600 for ; Mon, 12 Apr 2021 13:09:58 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 231916128A for ; Mon, 12 Apr 2021 13:09:58 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S241864AbhDLNKO (ORCPT ); Mon, 12 Apr 2021 09:10:14 -0400 Received: from wforward5-smtp.messagingengine.com ([64.147.123.35]:43285 "EHLO wforward5-smtp.messagingengine.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S241792AbhDLNKI (ORCPT ); Mon, 12 Apr 2021 09:10:08 -0400 Received: from compute3.internal (compute3.nyi.internal [10.202.2.43]) by mailforward.west.internal (Postfix) with ESMTP id 2D49216CB; Mon, 12 Apr 2021 09:09:47 -0400 (EDT) Received: from mailfrontend1 ([10.202.2.162]) by compute3.internal (MEProxy); Mon, 12 Apr 2021 09:09:48 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-transfer-encoding:date:from :in-reply-to:message-id:mime-version:references:subject:to :x-me-proxy:x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s= fm2; bh=pP3D8CfwuZ5Kl25pMI94SawFWsdT3QpkB6qg7VJ6HYk=; b=KqbW9iMC UA2yDlHut02bEfBXwAoWdZe230Poru68UG+lAS7NdgXUXolqnHKAHI5N1HiIx5Eb Nrvc2lbOpuCRflO3QJxCSL2QqDihw073CX1ImLsx9g3P1TJpf3ftk/RHU4FhTdAM 7yyxFDAwiXBpi532A9L8gW9QPSu+wsH5PsqXw3xd9fUQB4ht0l3aTzm8u0q9SJDr EIvJRZQqg73Zo/EI9GMfoAs+7M+llqrA5EQti0+Gik5VlJTijgCkEbJA23l0RUNQ O1TyO354pscXs9bYvw7IH3o8jqjww7N9KZsjjYYEYbZD3ZYYIECoyeCf6nx2gxrp l/wWW48f5yuiIw== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeduledrudekjedgiedvucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhephffvufffkffojghfggfgsedtkeertdertddtnecuhfhrohhmpeffrghvihgu ucfgughmohhnughsohhnuceouggrvhhiugdrvggumhhonhgushhonhesohhrrggtlhgvrd gtohhmqeenucggtffrrghtthgvrhhnpedufeetjefgfefhtdejhfehtdfftefhteekhefg leehfffhiefhgeelgfejtdehkeenucfkphepkedurddukeejrddviedrvdefkeenucevlh hushhtvghrufhiiigvpedtnecurfgrrhgrmhepmhgrihhlfhhrohhmpegurghvihgurdgv ughmohhnughsohhnsehorhgrtghlvgdrtghomh X-ME-Proxy: Received: from disaster-area.hh.sledj.net (disaster-area.hh.sledj.net [81.187.26.238]) by mail.messagingengine.com (Postfix) with ESMTPA id EB3C9240068; Mon, 12 Apr 2021 09:09:43 -0400 (EDT) Received: from localhost (disaster-area.hh.sledj.net [local]) by disaster-area.hh.sledj.net (OpenSMTPD) with ESMTPA id aaae85f0; Mon, 12 Apr 2021 13:09:38 +0000 (UTC) From: David Edmondson To: linux-kernel@vger.kernel.org Cc: kvm@vger.kernel.org, Paolo Bonzini , Vitaly Kuznetsov , "H. Peter Anvin" , x86@kernel.org, Sean Christopherson , Thomas Gleixner , Joerg Roedel , Wanpeng Li , Borislav Petkov , Jim Mattson , Ingo Molnar , Joao Martins , David Edmondson Subject: [PATCH 6/6] KVM: VMX: pass a proper reason in kvm_emulate_instruction() Date: Mon, 12 Apr 2021 14:09:37 +0100 Message-Id: <20210412130938.68178-7-david.edmondson@oracle.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20210412130938.68178-1-david.edmondson@oracle.com> References: <20210412130938.68178-1-david.edmondson@oracle.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org From: Joao Martins Declare various causes of emulation and use them as appropriate. Signed-off-by: Joao Martins Signed-off-by: David Edmondson --- arch/x86/include/asm/kvm_host.h | 5 +++++ arch/x86/kvm/vmx/vmx.c | 17 +++++++++-------- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h index e1284680cbdc..f401e7c79ded 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -1541,6 +1541,11 @@ enum { EMULREASON_SVM_CR, EMULREASON_SVM_DR, EMULREASON_SVM_AVIC_UNACCEL, + EMULREASON_VMX_APIC_ACCESS, + EMULREASON_VMX_EPT_VIOLATION, + EMULREASON_VMX_DESC, + EMULREASON_VMX_INV_GUEST, + EMULREASON_VMX_RMODE_EX, }; int kvm_emulate_instruction(struct kvm_vcpu *vcpu, int emulation_type, diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c index 037b01b5a54b..799eb0713b76 100644 --- a/arch/x86/kvm/vmx/vmx.c +++ b/arch/x86/kvm/vmx/vmx.c @@ -1600,7 +1600,7 @@ static int skip_emulated_instruction(struct kvm_vcpu *vcpu) #endif kvm_rip_write(vcpu, rip); } else { - if (!kvm_emulate_instruction(vcpu, EMULTYPE_SKIP, 0)) + if (!kvm_emulate_instruction(vcpu, EMULTYPE_SKIP, EMULREASON_SKIP)) return 0; } @@ -4738,7 +4738,7 @@ static int handle_rmode_exception(struct kvm_vcpu *vcpu, * Cause the #SS fault with 0 error code in VM86 mode. */ if (((vec == GP_VECTOR) || (vec == SS_VECTOR)) && err_code == 0) { - if (kvm_emulate_instruction(vcpu, 0, 0)) { + if (kvm_emulate_instruction(vcpu, 0, EMULREASON_VMX_RMODE_EX)) { if (vcpu->arch.halt_request) { vcpu->arch.halt_request = 0; return kvm_vcpu_halt(vcpu); @@ -4816,7 +4816,8 @@ static int handle_exception_nmi(struct kvm_vcpu *vcpu) kvm_queue_exception_e(vcpu, GP_VECTOR, error_code); return 1; } - return kvm_emulate_instruction(vcpu, EMULTYPE_VMWARE_GP, 0); + return kvm_emulate_instruction(vcpu, EMULTYPE_VMWARE_GP, + EMULREASON_GP); } /* @@ -4930,7 +4931,7 @@ static int handle_io(struct kvm_vcpu *vcpu) ++vcpu->stat.io_exits; if (string) - return kvm_emulate_instruction(vcpu, 0, 0); + return kvm_emulate_instruction(vcpu, 0, EMULREASON_IO); port = exit_qualification >> 16; size = (exit_qualification & 7) + 1; @@ -5004,7 +5005,7 @@ static int handle_set_cr4(struct kvm_vcpu *vcpu, unsigned long val) static int handle_desc(struct kvm_vcpu *vcpu) { WARN_ON(!(vcpu->arch.cr4 & X86_CR4_UMIP)); - return kvm_emulate_instruction(vcpu, 0, 0); + return kvm_emulate_instruction(vcpu, 0, EMULREASON_VMX_DESC); } static int handle_cr(struct kvm_vcpu *vcpu) @@ -5244,7 +5245,7 @@ static int handle_apic_access(struct kvm_vcpu *vcpu) return kvm_skip_emulated_instruction(vcpu); } } - return kvm_emulate_instruction(vcpu, 0, 0); + return kvm_emulate_instruction(vcpu, 0, EMULREASON_VMX_APIC_ACCESS); } static int handle_apic_eoi_induced(struct kvm_vcpu *vcpu) @@ -5375,7 +5376,7 @@ static int handle_ept_violation(struct kvm_vcpu *vcpu) * reconstruct the page fault error code. */ if (unlikely(allow_smaller_maxphyaddr && kvm_vcpu_is_illegal_gpa(vcpu, gpa))) - return kvm_emulate_instruction(vcpu, 0, 0); + return kvm_emulate_instruction(vcpu, 0, EMULREASON_VMX_EPT_VIOLATION); return kvm_mmu_page_fault(vcpu, gpa, error_code, NULL, 0); } @@ -5424,7 +5425,7 @@ static int handle_invalid_guest_state(struct kvm_vcpu *vcpu) if (kvm_test_request(KVM_REQ_EVENT, vcpu)) return 1; - if (!kvm_emulate_instruction(vcpu, 0, 0)) + if (!kvm_emulate_instruction(vcpu, 0, EMULREASON_VMX_INV_GUEST)) return 0; if (vmx->emulation_required && !vmx->rmode.vm86_active &&