From patchwork Thu Sep 16 08:32: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: 12498209 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 C22CEC433EF for ; Thu, 16 Sep 2021 08:32:46 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id ABD6161242 for ; Thu, 16 Sep 2021 08:32:46 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235130AbhIPIeF (ORCPT ); Thu, 16 Sep 2021 04:34:05 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49530 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235069AbhIPIeD (ORCPT ); Thu, 16 Sep 2021 04:34:03 -0400 Received: from mail-wr1-x42b.google.com (mail-wr1-x42b.google.com [IPv6:2a00:1450:4864:20::42b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4DF9EC061764 for ; Thu, 16 Sep 2021 01:32:43 -0700 (PDT) Received: by mail-wr1-x42b.google.com with SMTP id x6so8080053wrv.13 for ; Thu, 16 Sep 2021 01:32:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=dme-org.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=vkRPZ+VoJSi1c4vsO7PDtPst60HFkVEdoBfrAZpPA+U=; b=OmownnwxO6qXGAWRHKXnr2momFl7urRPOI1MxTgwlioC4OKhcp1r/YAHSl7/17PGBT Akhj2qokQbVkZC6rDl7YqQ9zZo0ttV1463OeN/2dbW8jT7/dpiwE3ZuL/06U+ex2BzFz A2/dCJo/l7NX5bAv0fwnfXWcO337boFd8vR4FAi23REkSTNpUHnGC6bYUQAUIWd7aV6N IHxklLpHz1lBLBVsc7TobG8aptHLZ+W2kcdN5/6tYNj6J9ZQhcQVrgt1vhoIwl0xinJM boXvlhUoEqwuH25BU3oSAq8W1iD0LzggbsQM2GbejrAZnuE9elgBogmr79/NSW8weRmU F9XA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=vkRPZ+VoJSi1c4vsO7PDtPst60HFkVEdoBfrAZpPA+U=; b=nx+bzabVEvD8Xp/TRkOTaH+Ev/3h+tvUkm3vIpJUM2St04Pxu8StOw8ehbhzwrdn4P 8E4JfQrEux+YjgHLsMFC1aLUDIHfqGb3LLylSrmq2LwFjqWPZb8LGTKUStpeH+ETO6J3 jA63Haq9ehwX26Z/ZZHE3QNDGnt8EF3G4fhDKpQMzCb1UIIHEMidZWtzGyzix9omBFCX ghbaSIFwtYVLjTtcoBJOWMQGsq1cVUFyZxTygPnciY4cd9eYplpSSbASw/nShrk4DrJg OtDE2mBImdcHcbLJ4AK8mHgSODLdDNFgNozMYowKjhUSXWWhxGK0btltxd2+9yecxo+L nVaw== X-Gm-Message-State: AOAM533WmxX6pN5swlI2heYyexMuy/DIR+QhlvgtKGaIMCDfWugb+pFW /ZmStj0MWiklEv8Ls5JDlI9LNA== X-Google-Smtp-Source: ABdhPJzbqo9kku8xpT2kbo5mvvN0Xf3S6dZbE4kjD+XAajPvezZoS6shIkZO1MuPlYwpVT7Pufdfeg== X-Received: by 2002:adf:fc8f:: with SMTP id g15mr4559119wrr.227.1631781161789; Thu, 16 Sep 2021 01:32:41 -0700 (PDT) Received: from disaster-area.hh.sledj.net ([2001:8b0:bb71:7140:64::1]) by smtp.gmail.com with ESMTPSA id c135sm6760024wme.6.2021.09.16.01.32.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 16 Sep 2021 01:32:41 -0700 (PDT) From: David Edmondson X-Google-Original-From: David Edmondson Received: from localhost (disaster-area.hh.sledj.net [local]) by disaster-area.hh.sledj.net (OpenSMTPD) with ESMTPA id 9ce9abab; Thu, 16 Sep 2021 08:32:39 +0000 (UTC) To: linux-kernel@vger.kernel.org Cc: Wanpeng Li , Joerg Roedel , kvm@vger.kernel.org, Paolo Bonzini , Vitaly Kuznetsov , Sean Christopherson , David Matlack , Borislav Petkov , Ingo Molnar , x86@kernel.org, "H. Peter Anvin" , Thomas Gleixner , Jim Mattson , David Edmondson Subject: [PATCH v5 1/4] KVM: x86: Clarify the kvm_run.emulation_failure structure layout Date: Thu, 16 Sep 2021 09:32:36 +0100 Message-Id: <20210916083239.2168281-2-david.edmondson@oracle.com> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20210916083239.2168281-1-david.edmondson@oracle.com> References: <20210916083239.2168281-1-david.edmondson@oracle.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org Until more flags for kvm_run.emulation_failure flags are defined, it is undetermined whether new payload elements corresponding to those flags will be additive or alternative. As a hint to userspace that an alternative is possible, wrap the current payload elements in a union. Suggested-by: Sean Christopherson Signed-off-by: David Edmondson --- include/uapi/linux/kvm.h | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/include/uapi/linux/kvm.h b/include/uapi/linux/kvm.h index a067410ebea5..8618fe973215 100644 --- a/include/uapi/linux/kvm.h +++ b/include/uapi/linux/kvm.h @@ -402,8 +402,12 @@ struct kvm_run { __u32 suberror; __u32 ndata; __u64 flags; - __u8 insn_size; - __u8 insn_bytes[15]; + union { + struct { + __u8 insn_size; + __u8 insn_bytes[15]; + }; + }; } emulation_failure; /* KVM_EXIT_OSI */ struct { From patchwork Thu Sep 16 08:32: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: 12498211 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 97D83C433FE for ; Thu, 16 Sep 2021 08:32:47 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 7DF9B61241 for ; Thu, 16 Sep 2021 08:32:47 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235140AbhIPIeG (ORCPT ); Thu, 16 Sep 2021 04:34:06 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49538 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235102AbhIPIeE (ORCPT ); Thu, 16 Sep 2021 04:34:04 -0400 Received: from mail-wr1-x42c.google.com (mail-wr1-x42c.google.com [IPv6:2a00:1450:4864:20::42c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1DCB3C061767 for ; Thu, 16 Sep 2021 01:32:44 -0700 (PDT) Received: by mail-wr1-x42c.google.com with SMTP id s16so1493211wra.10 for ; Thu, 16 Sep 2021 01:32:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=dme-org.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=oprU1kxT1w+DveU6YstsbHcV6buDq4T0SEU6UEPXwXk=; b=dWP7giWss8w4Nf4bfLGks7tEKukq+zNQndkbn8VaE2ayRDnqpWB8papY0UgiSPyQz8 5Bwqs4FZAvbaivxYjssEq6K6TSv1XzI8LP7MK0RHljzmtcFPtkx02BUXcvuKR8gg9NO3 2/TzgGGvxlsYM33DkDUXuesTbTsoplpMQRERYlN3zQmv2NK5H2DjrHT0lGnjzfONGPPb coRaa43WHNg/ZtiIQKU/5jVKLiYXoZTz69/oJ3E2kWlfgM4ZGVyjAmkkutKrDXaUl6/K PlUK1XTpnro9+GEoJTgwf7e5JjahuqORl+AmpvLrzzohFail1R1CgCmX6tFD36pKoIGQ AjTg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=oprU1kxT1w+DveU6YstsbHcV6buDq4T0SEU6UEPXwXk=; b=3CV5OlvAKl+RHAzOyAPYerBbWutJHWlZdRud7eZSMWmUF/WbZVIN3MbXWX8OEnvFgY y/Hi1mHizZ5M0umlwqMhkCkZUhlUvhM8N0FuARBZSzc7xoiNYFdn2JMaH7OC/kZAid1w X16zXpSpLgouM5e1Nc5C4kR8815MVC1fGYsMGH7z9EDE9V4yWrrkGuoXOMOVy3gLxk0a srsqzRfblSP9QhdBEVjdrlE+2ThfQejE99UPkRr3mb9WiXo4sX1PnHw2/Y1MvpsjpZqF EMyHXOnQJThWUUei5NJNz/9hNMWONMzVLyDS5BcsbCC4LVWoY1MRPykGm016JTGHEU0m +LlQ== X-Gm-Message-State: AOAM533ZpS9GxxQ6XKzIq0Zr6XMJyAOkp7PVwegtwWMJRZWKnBaakRhG 1TUdgOR13lgTu9nPMp7QAQcQEg== X-Google-Smtp-Source: ABdhPJy0XJHLfFqtq0nt9PUm1XzqvVUR6OWx7pgxtH3ReT/m9lyKNev+Tzf6qrgmE+ELMOlP1lROvw== X-Received: by 2002:a5d:6b46:: with SMTP id x6mr4610888wrw.192.1631781162579; Thu, 16 Sep 2021 01:32:42 -0700 (PDT) Received: from disaster-area.hh.sledj.net ([2001:8b0:bb71:7140:64::1]) by smtp.gmail.com with ESMTPSA id c135sm6760024wme.6.2021.09.16.01.32.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 16 Sep 2021 01:32:42 -0700 (PDT) From: David Edmondson X-Google-Original-From: David Edmondson Received: from localhost (disaster-area.hh.sledj.net [local]) by disaster-area.hh.sledj.net (OpenSMTPD) with ESMTPA id 5640c2e7; Thu, 16 Sep 2021 08:32:39 +0000 (UTC) To: linux-kernel@vger.kernel.org Cc: Wanpeng Li , Joerg Roedel , kvm@vger.kernel.org, Paolo Bonzini , Vitaly Kuznetsov , Sean Christopherson , David Matlack , Borislav Petkov , Ingo Molnar , x86@kernel.org, "H. Peter Anvin" , Thomas Gleixner , Jim Mattson , David Edmondson Subject: [PATCH v5 2/4] KVM: x86: Get exit_reason as part of kvm_x86_ops.get_exit_info Date: Thu, 16 Sep 2021 09:32:37 +0100 Message-Id: <20210916083239.2168281-3-david.edmondson@oracle.com> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20210916083239.2168281-1-david.edmondson@oracle.com> References: <20210916083239.2168281-1-david.edmondson@oracle.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org Extend the get_exit_info static call to provide the reason for the VM exit. Modify relevant trace points to use this rather than extracting the reason in the caller. Signed-off-by: David Edmondson Reviewed-by: Sean Christopherson --- arch/x86/include/asm/kvm_host.h | 7 ++++--- arch/x86/kvm/svm/svm.c | 8 +++++--- arch/x86/kvm/trace.h | 9 +++++---- arch/x86/kvm/vmx/nested.c | 2 +- arch/x86/kvm/vmx/vmx.c | 6 ++++-- 5 files changed, 19 insertions(+), 13 deletions(-) diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h index f8f48a7ec577..d22bbeb48f66 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -1405,10 +1405,11 @@ struct kvm_x86_ops { void (*write_tsc_multiplier)(struct kvm_vcpu *vcpu, u64 multiplier); /* - * Retrieve somewhat arbitrary exit information. Intended to be used - * only from within tracepoints to avoid VMREADs when tracing is off. + * Retrieve somewhat arbitrary exit information. Intended to + * be used only from within tracepoints or error paths. */ - void (*get_exit_info)(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2, + void (*get_exit_info)(struct kvm_vcpu *vcpu, u32 *reason, + u64 *info1, u64 *info2, u32 *exit_int_info, u32 *exit_int_info_err_code); int (*check_intercept)(struct kvm_vcpu *vcpu, diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c index 05e8d4d27969..a902a767f722 100644 --- a/arch/x86/kvm/svm/svm.c +++ b/arch/x86/kvm/svm/svm.c @@ -3272,11 +3272,13 @@ int svm_invoke_exit_handler(struct kvm_vcpu *vcpu, u64 exit_code) return svm_exit_handlers[exit_code](vcpu); } -static void svm_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2, +static void svm_get_exit_info(struct kvm_vcpu *vcpu, u32 *reason, + u64 *info1, u64 *info2, u32 *intr_info, u32 *error_code) { struct vmcb_control_area *control = &to_svm(vcpu)->vmcb->control; + *reason = control->exit_code; *info1 = control->exit_info_1; *info2 = control->exit_info_2; *intr_info = control->exit_int_info; @@ -3293,7 +3295,7 @@ static int handle_exit(struct kvm_vcpu *vcpu, fastpath_t exit_fastpath) struct kvm_run *kvm_run = vcpu->run; u32 exit_code = svm->vmcb->control.exit_code; - trace_kvm_exit(exit_code, vcpu, KVM_ISA_SVM); + trace_kvm_exit(vcpu, KVM_ISA_SVM); /* SEV-ES guests must use the CR write traps to track CR registers. */ if (!sev_es_guest(vcpu->kvm)) { @@ -3306,7 +3308,7 @@ static int handle_exit(struct kvm_vcpu *vcpu, fastpath_t exit_fastpath) if (is_guest_mode(vcpu)) { int vmexit; - trace_kvm_nested_vmexit(exit_code, vcpu, KVM_ISA_SVM); + trace_kvm_nested_vmexit(vcpu, KVM_ISA_SVM); vmexit = nested_svm_exit_special(svm); diff --git a/arch/x86/kvm/trace.h b/arch/x86/kvm/trace.h index 03ebe368333e..953b0fcb21ee 100644 --- a/arch/x86/kvm/trace.h +++ b/arch/x86/kvm/trace.h @@ -288,8 +288,8 @@ TRACE_EVENT(kvm_apic, #define TRACE_EVENT_KVM_EXIT(name) \ TRACE_EVENT(name, \ - TP_PROTO(unsigned int exit_reason, struct kvm_vcpu *vcpu, u32 isa), \ - TP_ARGS(exit_reason, vcpu, isa), \ + TP_PROTO(struct kvm_vcpu *vcpu, u32 isa), \ + TP_ARGS(vcpu, isa), \ \ TP_STRUCT__entry( \ __field( unsigned int, exit_reason ) \ @@ -303,11 +303,12 @@ TRACE_EVENT(name, \ ), \ \ TP_fast_assign( \ - __entry->exit_reason = exit_reason; \ __entry->guest_rip = kvm_rip_read(vcpu); \ __entry->isa = isa; \ __entry->vcpu_id = vcpu->vcpu_id; \ - static_call(kvm_x86_get_exit_info)(vcpu, &__entry->info1, \ + static_call(kvm_x86_get_exit_info)(vcpu, \ + &__entry->exit_reason, \ + &__entry->info1, \ &__entry->info2, \ &__entry->intr_info, \ &__entry->error_code); \ diff --git a/arch/x86/kvm/vmx/nested.c b/arch/x86/kvm/vmx/nested.c index ccb03d69546c..43ea97b3f8e6 100644 --- a/arch/x86/kvm/vmx/nested.c +++ b/arch/x86/kvm/vmx/nested.c @@ -6059,7 +6059,7 @@ bool nested_vmx_reflect_vmexit(struct kvm_vcpu *vcpu) goto reflect_vmexit; } - trace_kvm_nested_vmexit(exit_reason.full, vcpu, KVM_ISA_VMX); + trace_kvm_nested_vmexit(vcpu, KVM_ISA_VMX); /* If L0 (KVM) wants the exit, it trumps L1's desires. */ if (nested_vmx_l0_wants_exit(vcpu, exit_reason)) diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c index 0c2c0d5ae873..99f8f7c4a510 100644 --- a/arch/x86/kvm/vmx/vmx.c +++ b/arch/x86/kvm/vmx/vmx.c @@ -5628,11 +5628,13 @@ static int (*kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu) = { static const int kvm_vmx_max_exit_handlers = ARRAY_SIZE(kvm_vmx_exit_handlers); -static void vmx_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2, +static void vmx_get_exit_info(struct kvm_vcpu *vcpu, u32 *reason, + u64 *info1, u64 *info2, u32 *intr_info, u32 *error_code) { struct vcpu_vmx *vmx = to_vmx(vcpu); + *reason = vmx->exit_reason.full; *info1 = vmx_get_exit_qual(vcpu); if (!(vmx->exit_reason.failed_vmentry)) { *info2 = vmx->idt_vectoring_info; @@ -6769,7 +6771,7 @@ static fastpath_t vmx_vcpu_run(struct kvm_vcpu *vcpu) if (likely(!vmx->exit_reason.failed_vmentry)) vmx->idt_vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD); - trace_kvm_exit(vmx->exit_reason.full, vcpu, KVM_ISA_VMX); + trace_kvm_exit(vcpu, KVM_ISA_VMX); if (unlikely(vmx->exit_reason.failed_vmentry)) return EXIT_FASTPATH_NONE; From patchwork Thu Sep 16 08:32:38 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Edmondson X-Patchwork-Id: 12498213 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 0251EC4332F for ; Thu, 16 Sep 2021 08:32:49 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id E1C6C61248 for ; Thu, 16 Sep 2021 08:32:48 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235175AbhIPIeH (ORCPT ); Thu, 16 Sep 2021 04:34:07 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49546 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234998AbhIPIeE (ORCPT ); Thu, 16 Sep 2021 04:34:04 -0400 Received: from mail-wm1-x333.google.com (mail-wm1-x333.google.com [IPv6:2a00:1450:4864:20::333]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6B13EC061574 for ; Thu, 16 Sep 2021 01:32:44 -0700 (PDT) Received: by mail-wm1-x333.google.com with SMTP id u19-20020a7bc053000000b002f8d045b2caso3903635wmc.1 for ; Thu, 16 Sep 2021 01:32:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=dme-org.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=Hovwpv8/OQn6x9tDTolJyyVnUo6SvlNiP0jevbAylog=; b=erClmfqNEUMOObMe1K7GB/9ZDK/Bpn0jIKjRgVTUrwxecbSf4ennBcY6kGVG5uujyF nRDl+MMcaEq6Q2u2IFxhTN4j/CWFR1tDy59C6QtgBiaCekMVqclEz6DVZHiF5vywCPd8 hNABUniImH2WmyAq/i02c7mXx8XRqxhFI6BM4r1V/1jYSlwhrI5LUgBSXuweKmKBPKQL JVwioVd9Mc0i1bvu1ft0FQfhn/1o/NAaU24LLzAdRN9ZYmTNcld17etIolKiSUM47vvJ 4X5gxB3+jrkcXDgiRn5DfnTRX/hcHpbYigWAbNnvHbJw78NbTWklR+bmVwgMSA6+sQeY 52jA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=Hovwpv8/OQn6x9tDTolJyyVnUo6SvlNiP0jevbAylog=; b=6eMn/mmPVf7P6jbEt7OBEtHsuCnnnJVztEAqpOiEV7PRGsBu+HVEEpt51FWGC0HZ1m C8Qs5BlTq8yySbMvXJndJZs+vCUuTTdzYWk2p8+8Fh6CuRKj7sxZKwVNOyeBU5Gk+GER OXg7iFtSvZ8mkPEvnk0464jOgoJOE7p2NaqgFqbIvkwIyqJoPcSJmdSJV1BYvHTmR1kB npalv24YY0ygDmML94xzny+5gzfZhOm+HMWUVk++dBngyuf41U65/Om3ziSttRQxCGIi 2yJXRWZ+TIxF5DTVOHCrf+Wq6Ogx92xkkvkXLLKvKzAlKAXulURkOblVZMqls5hePGU3 6f3w== X-Gm-Message-State: AOAM532xHorDkueHsfj2FBkXSHQjSYPcjVUBFj3btIq0oBwWRvleIAuk nJSYWxmtbNiLRTMUE+ceImP+T8+QoBwZA88Q X-Google-Smtp-Source: ABdhPJxMaeryzao83xZ/CCWFrq3FEf6R928NnwHGmexrP11CQvmmCMWxY8XIeY8EA1DvDa1jEdt0fw== X-Received: by 2002:a1c:4406:: with SMTP id r6mr8718890wma.150.1631781163001; Thu, 16 Sep 2021 01:32:43 -0700 (PDT) Received: from disaster-area.hh.sledj.net (disaster-area.hh.sledj.net. [81.187.26.238]) by smtp.gmail.com with ESMTPSA id u16sm2745555wmc.41.2021.09.16.01.32.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 16 Sep 2021 01:32:42 -0700 (PDT) From: David Edmondson X-Google-Original-From: David Edmondson Received: from localhost (disaster-area.hh.sledj.net [local]) by disaster-area.hh.sledj.net (OpenSMTPD) with ESMTPA id 1109c181; Thu, 16 Sep 2021 08:32:39 +0000 (UTC) To: linux-kernel@vger.kernel.org Cc: Wanpeng Li , Joerg Roedel , kvm@vger.kernel.org, Paolo Bonzini , Vitaly Kuznetsov , Sean Christopherson , David Matlack , Borislav Petkov , Ingo Molnar , x86@kernel.org, "H. Peter Anvin" , Thomas Gleixner , Jim Mattson , David Edmondson , Joao Martins Subject: [PATCH v5 3/4] KVM: x86: On emulation failure, convey the exit reason, etc. to userspace Date: Thu, 16 Sep 2021 09:32:38 +0100 Message-Id: <20210916083239.2168281-4-david.edmondson@oracle.com> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20210916083239.2168281-1-david.edmondson@oracle.com> References: <20210916083239.2168281-1-david.edmondson@oracle.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org 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 Signed-off-by: David Edmondson Reviewed-by: Sean Christopherson --- arch/x86/include/asm/kvm_host.h | 3 ++ arch/x86/kvm/vmx/vmx.c | 5 +-- arch/x86/kvm/x86.c | 73 ++++++++++++++++++++++++++------- include/uapi/linux/kvm.h | 6 +++ 4 files changed, 69 insertions(+), 18 deletions(-) diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h index d22bbeb48f66..297581046460 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -1658,6 +1658,9 @@ extern u64 kvm_mce_cap_supported; int kvm_emulate_instruction(struct kvm_vcpu *vcpu, int emulation_type); int kvm_emulate_instruction_from_buffer(struct kvm_vcpu *vcpu, 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); bool kvm_valid_efer(struct kvm_vcpu *vcpu, u64 efer); diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c index 99f8f7c4a510..e71f6ccafa5f 100644 --- a/arch/x86/kvm/vmx/vmx.c +++ b/arch/x86/kvm/vmx/vmx.c @@ -5378,10 +5378,7 @@ static int handle_invalid_guest_state(struct kvm_vcpu *vcpu) if (vmx->emulation_required && !vmx->rmode.vm86_active && vcpu->arch.exception.pending) { - vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR; - vcpu->run->internal.suberror = - KVM_INTERNAL_ERROR_EMULATION; - vcpu->run->internal.ndata = 0; + kvm_prepare_emulation_failure_exit(vcpu); return 0; } diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 28ef14155726..55fe3203a3c1 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -7468,29 +7468,78 @@ void kvm_inject_realmode_interrupt(struct kvm_vcpu *vcpu, int irq, int inc_eip) } 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; + 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->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; 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 |= KVM_INTERNAL_ERROR_EMULATION_FLAG_INSTRUCTION_BYTES; run->emulation_failure.insn_size = insn_size; memset(run->emulation_failure.insn_bytes, 0x90, sizeof(run->emulation_failure.insn_bytes)); - memcpy(run->emulation_failure.insn_bytes, - ctxt->fetch.data, insn_size); + memcpy(run->emulation_failure.insn_bytes, insn_bytes, 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) { struct kvm *kvm = vcpu->kvm; @@ -7505,16 +7554,14 @@ static int handle_emulation_failure(struct kvm_vcpu *vcpu, int emulation_type) if (kvm->arch.exit_on_emulation_error || (emulation_type & EMULTYPE_SKIP)) { - prepare_emulation_failure_exit(vcpu); + prepare_emulation_ctxt_failure_exit(vcpu); return 0; } kvm_queue_exception(vcpu, UD_VECTOR); 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; + prepare_emulation_ctxt_failure_exit(vcpu); return 0; } @@ -12153,9 +12200,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 * KVM_EXIT_INTERNAL_ERROR for now. */ - vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR; - vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION; - vcpu->run->internal.ndata = 0; + kvm_prepare_emulation_failure_exit(vcpu); return 0; } diff --git a/include/uapi/linux/kvm.h b/include/uapi/linux/kvm.h index 8618fe973215..cb032a95aca2 100644 --- a/include/uapi/linux/kvm.h +++ b/include/uapi/linux/kvm.h @@ -397,6 +397,11 @@ struct kvm_run { * "ndata" is correct, that new fields are enumerated in "flags", * and that each flag enumerates fields that are 64-bit aligned * 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 { __u32 suberror; @@ -408,6 +413,7 @@ struct kvm_run { __u8 insn_bytes[15]; }; }; + /* Arbitrary debug data may follow. */ } emulation_failure; /* KVM_EXIT_OSI */ struct { From patchwork Thu Sep 16 08:32:39 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Edmondson X-Patchwork-Id: 12498215 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 AD68BC433EF for ; Thu, 16 Sep 2021 08:32:51 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 9360961246 for ; Thu, 16 Sep 2021 08:32:51 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234998AbhIPIeJ (ORCPT ); Thu, 16 Sep 2021 04:34:09 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49550 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235120AbhIPIeF (ORCPT ); Thu, 16 Sep 2021 04:34:05 -0400 Received: from mail-wr1-x432.google.com (mail-wr1-x432.google.com [IPv6:2a00:1450:4864:20::432]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0B507C061767 for ; Thu, 16 Sep 2021 01:32:45 -0700 (PDT) Received: by mail-wr1-x432.google.com with SMTP id s16so1493278wra.10 for ; Thu, 16 Sep 2021 01:32:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=dme-org.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=jxlL0lGtv5ZlNrPQbf90760XLEjt/UFew72/NT7P6ZA=; b=TkTWRVF+5Q7Su3IUaesuhgf4CtLDdMSqoQgis6/0kVEgCS2xqDGymVIOELO3gYGYTB e2SxJlu5+fKJqBBEjqHq2o1+9vd+KD8skQfo5Rpb/nlUSzaPqRW8Lei0/HUwgFkSdIAG oEdHGSp1kHWO3MrUUi4vBU+/qkp49b26WEEMww/cNRsoBZ22a929EP/U3gQkvEAyAMqE CB0T6twNdt6LcikTrYASFmN4ztePGAtLJH0SgTbcRyh1EM3v7UecYNhsS/gBe7nt+Ivc ITPu5MHfPqsM96Bt2I2GKNLDo3Nuwq1zv+C3BIJP2RT3pVfAjH3BQUfsAvcLwi0W4Qad faGA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=jxlL0lGtv5ZlNrPQbf90760XLEjt/UFew72/NT7P6ZA=; b=R005tU1+mPJOJM8poLw/YxqBQ7gfQjFBzWPxg3eaUD70DHavwV2sLNUETNuu1p4A71 GSygilMmHPwQSjdhAhARy0yUovt1iB5KAI3GSfzDfe5dNxhHGtmcnXInnrZkFztGwEUT PZmAbm7/tjhkKmRKcER8f/Amkyek5bkqGZxQYhfyN8OAa2xy2739eH9ALCNxivhI0hhi Ut2ePSqy37GGRysZu9DBgIcSQhm+RgtQDE29N1pogo/zNt8sT8jwko1Mo7euwAgLf1AE G+gI7tdyN9wCKGrwghYqN8yBxKtKPCE8agHBzA6gowEBSCrH3lnxQoFDWXezgzPoYEvb N2zQ== X-Gm-Message-State: AOAM531IT1rMcleZmJ7bs3/mKL1X+Y/A/Kgo7YvrujNuzt7QbUBvhSsX rClst2o8Y3ClobP9u/03di93UKIUYM/Z9ZJcC7Y= X-Google-Smtp-Source: ABdhPJyfE8/wyEQRvmpiR7oOPNOL4J2PzWMdBSBvGwpu/s4Fd6Im8ZXwX9obCHi1pvvQC+8rA7nVsw== X-Received: by 2002:adf:e384:: with SMTP id e4mr4705046wrm.64.1631781163393; Thu, 16 Sep 2021 01:32:43 -0700 (PDT) Received: from disaster-area.hh.sledj.net ([2001:8b0:bb71:7140:64::1]) by smtp.gmail.com with ESMTPSA id c135sm6760024wme.6.2021.09.16.01.32.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 16 Sep 2021 01:32:43 -0700 (PDT) From: David Edmondson X-Google-Original-From: David Edmondson Received: from localhost (disaster-area.hh.sledj.net [local]) by disaster-area.hh.sledj.net (OpenSMTPD) with ESMTPA id 6aff296b; Thu, 16 Sep 2021 08:32:39 +0000 (UTC) To: linux-kernel@vger.kernel.org Cc: Wanpeng Li , Joerg Roedel , kvm@vger.kernel.org, Paolo Bonzini , Vitaly Kuznetsov , Sean Christopherson , David Matlack , Borislav Petkov , Ingo Molnar , x86@kernel.org, "H. Peter Anvin" , Thomas Gleixner , Jim Mattson , David Edmondson Subject: [PATCH v5 4/4] KVM: x86: SGX must obey the KVM_INTERNAL_ERROR_EMULATION protocol Date: Thu, 16 Sep 2021 09:32:39 +0100 Message-Id: <20210916083239.2168281-5-david.edmondson@oracle.com> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20210916083239.2168281-1-david.edmondson@oracle.com> References: <20210916083239.2168281-1-david.edmondson@oracle.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org When passing the failing address and size out to user space, SGX must ensure not to trample on the earlier fields of the emulation_failure sub-union of struct kvm_run. Signed-off-by: David Edmondson Reviewed-by: Sean Christopherson --- arch/x86/kvm/vmx/sgx.c | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) diff --git a/arch/x86/kvm/vmx/sgx.c b/arch/x86/kvm/vmx/sgx.c index 6693ebdc0770..35e7ec91ae86 100644 --- a/arch/x86/kvm/vmx/sgx.c +++ b/arch/x86/kvm/vmx/sgx.c @@ -53,11 +53,9 @@ static int sgx_get_encls_gva(struct kvm_vcpu *vcpu, unsigned long offset, static void sgx_handle_emulation_failure(struct kvm_vcpu *vcpu, u64 addr, unsigned int size) { - vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR; - vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION; - vcpu->run->internal.ndata = 2; - vcpu->run->internal.data[0] = addr; - vcpu->run->internal.data[1] = size; + uint64_t data[2] = { addr, size }; + + __kvm_prepare_emulation_failure_exit(vcpu, data, ARRAY_SIZE(data)); } static int sgx_read_hva(struct kvm_vcpu *vcpu, unsigned long hva, void *data, @@ -112,9 +110,7 @@ static int sgx_inject_fault(struct kvm_vcpu *vcpu, gva_t gva, int trapnr) * but the error code isn't (yet) plumbed through the ENCLS helpers. */ if (trapnr == PF_VECTOR && !boot_cpu_has(X86_FEATURE_SGX2)) { - vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR; - vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION; - vcpu->run->internal.ndata = 0; + kvm_prepare_emulation_failure_exit(vcpu); return 0; } @@ -155,9 +151,7 @@ static int __handle_encls_ecreate(struct kvm_vcpu *vcpu, sgx_12_0 = kvm_find_cpuid_entry(vcpu, 0x12, 0); sgx_12_1 = kvm_find_cpuid_entry(vcpu, 0x12, 1); if (!sgx_12_0 || !sgx_12_1) { - vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR; - vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION; - vcpu->run->internal.ndata = 0; + kvm_prepare_emulation_failure_exit(vcpu); return 0; }