From patchwork Tue Nov 9 17:44:25 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Vipin Sharma X-Patchwork-Id: 12610937 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 43C5FC433F5 for ; Tue, 9 Nov 2021 17:44:38 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 2851F6102A for ; Tue, 9 Nov 2021 17:44:38 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S241248AbhKIRrV (ORCPT ); Tue, 9 Nov 2021 12:47:21 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45312 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S241230AbhKIRrU (ORCPT ); Tue, 9 Nov 2021 12:47:20 -0500 Received: from mail-pl1-x649.google.com (mail-pl1-x649.google.com [IPv6:2607:f8b0:4864:20::649]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3B9C3C061764 for ; Tue, 9 Nov 2021 09:44:34 -0800 (PST) Received: by mail-pl1-x649.google.com with SMTP id i3-20020a170902c94300b0014287dc7dcbso3315608pla.16 for ; Tue, 09 Nov 2021 09:44:34 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=azJvZCYQiOEvzpk5HIvvri7jM+tzmkHzZTh2NDFW5hc=; b=KIp5poo5TTGtQDEuzmQQXTpR8ugcfA7uD3t1HUw0igbgihdAPDiT8J9ASMZUrbF+UX tGXbZ9q/btSjGyv6JANX/kkq8qlgIx6rDVSCNxDwhDFk1FcOHntTr+UXuULU0ezylVE0 q5CgQeu0u6mibuY1Bzs+jZxHnMIqC5qNigSAkon6T7RphOO+eGyvQQgo676wsXt09Qmv PdLny/Kv2SgkYSg09Cs8PmusC8e1R/zScjtf7j1fT6qnJndCKB/fOLiQ6f/6GnEB9hb9 ztJJNETMyetRBfnnUDn0qA5IK1Z4B4qSrqKCwYyFW5R5JTANX46W/8bA2yPYSysQMJhD hOYw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=azJvZCYQiOEvzpk5HIvvri7jM+tzmkHzZTh2NDFW5hc=; b=cJcSLZR9nERTu+01kD/QxqTpdQt0ZpoER3g+eQjbMwF0QaYLOc+KP8UgWqJQp/7Z8Q MJotQ9S5Ab+KehY02nybWwaES0BwVxq8X6mEB9KH7mELCb0QLZNPtsDOmN+Errqx69i/ fnNwnFV0VPnenSX/7gamzp8nqVgYTN8U5bc/EkvlEXDPtkubQO5wrgnkqE56fggul03L Z/u82YlIHvQBuNpT//HdYjNt4ldiGg7PYRyLeJ/V9mByFK4s5LLg7Ce2GSS7SN/0aWhV Ww/7/6jbHRd7hr491iVZ9qrYPWybPl59ftjY5y0kmdKbDh3ge0H6NDNGlr4msuec4AuS lnIg== X-Gm-Message-State: AOAM530kz6+xCbxZeaIDc7AI++8iQmSAwJMoW/bmgBlbpzfSDbp+vSU1 q6IcYNRc3xFNYxM/6i4KIx6JQQ6s75r0 X-Google-Smtp-Source: ABdhPJw/dFG+OXFojjhEYYutAIBtZJNpp/MW1Wc8cfjsfPdmBRdkxZkqcheb/tNRcmCa2wKcuQqFQo9kleIa X-Received: from vipinsh.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:36b0]) (user=vipinsh job=sendgmr) by 2002:a17:902:e849:b0:142:c85:4d3d with SMTP id t9-20020a170902e84900b001420c854d3dmr8740313plg.75.1636479873707; Tue, 09 Nov 2021 09:44:33 -0800 (PST) Date: Tue, 9 Nov 2021 17:44:25 +0000 In-Reply-To: <20211109174426.2350547-1-vipinsh@google.com> Message-Id: <20211109174426.2350547-2-vipinsh@google.com> Mime-Version: 1.0 References: <20211109174426.2350547-1-vipinsh@google.com> X-Mailer: git-send-email 2.34.0.rc0.344.g81b53c2807-goog Subject: [PATCH v4 1/2] KVM: VMX: Add a helper function to retrieve the GPR index for INVPCID, INVVPID, and INVEPT From: Vipin Sharma To: pbonzini@redhat.com, seanjc@google.com, jmattson@google.com Cc: dmatlack@google.com, kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Vipin Sharma Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org handle_invept(), handle_invvpid(), handle_invpcid() read the same reg2 field in vmcs.VMX_INSTRUCTION_INFO to get the index of the GPR that holds the invalidation type. Add a helper to retrieve reg2 from VMX instruction info to consolidate and document the shift+mask magic. Signed-off-by: Vipin Sharma Reviewed-by: Sean Christopherson --- arch/x86/kvm/vmx/nested.c | 10 ++++++---- arch/x86/kvm/vmx/vmx.c | 4 +++- arch/x86/kvm/vmx/vmx.h | 5 +++++ 3 files changed, 14 insertions(+), 5 deletions(-) diff --git a/arch/x86/kvm/vmx/nested.c b/arch/x86/kvm/vmx/nested.c index b4ee5e9f9e20..f73d4e31dd99 100644 --- a/arch/x86/kvm/vmx/nested.c +++ b/arch/x86/kvm/vmx/nested.c @@ -5379,7 +5379,7 @@ static int handle_invept(struct kvm_vcpu *vcpu) struct { u64 eptp, gpa; } operand; - int i, r; + int i, r, gpr_index; if (!(vmx->nested.msrs.secondary_ctls_high & SECONDARY_EXEC_ENABLE_EPT) || @@ -5392,7 +5392,8 @@ static int handle_invept(struct kvm_vcpu *vcpu) return 1; vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO); - type = kvm_register_read(vcpu, (vmx_instruction_info >> 28) & 0xf); + gpr_index = vmx_get_instr_info_reg2(vmx_instruction_info); + type = kvm_register_read(vcpu, gpr_index); types = (vmx->nested.msrs.ept_caps >> VMX_EPT_EXTENT_SHIFT) & 6; @@ -5459,7 +5460,7 @@ static int handle_invvpid(struct kvm_vcpu *vcpu) u64 gla; } operand; u16 vpid02; - int r; + int r, gpr_index; if (!(vmx->nested.msrs.secondary_ctls_high & SECONDARY_EXEC_ENABLE_VPID) || @@ -5472,7 +5473,8 @@ static int handle_invvpid(struct kvm_vcpu *vcpu) return 1; vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO); - type = kvm_register_read(vcpu, (vmx_instruction_info >> 28) & 0xf); + gpr_index = vmx_get_instr_info_reg2(vmx_instruction_info); + type = kvm_register_read(vcpu, gpr_index); types = (vmx->nested.msrs.vpid_caps & VMX_VPID_EXTENT_SUPPORTED_MASK) >> 8; diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c index 71f54d85f104..e41d207e3298 100644 --- a/arch/x86/kvm/vmx/vmx.c +++ b/arch/x86/kvm/vmx/vmx.c @@ -5494,6 +5494,7 @@ static int handle_invpcid(struct kvm_vcpu *vcpu) u64 pcid; u64 gla; } operand; + int gpr_index; if (!guest_cpuid_has(vcpu, X86_FEATURE_INVPCID)) { kvm_queue_exception(vcpu, UD_VECTOR); @@ -5501,7 +5502,8 @@ static int handle_invpcid(struct kvm_vcpu *vcpu) } vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO); - type = kvm_register_read(vcpu, (vmx_instruction_info >> 28) & 0xf); + gpr_index = vmx_get_instr_info_reg2(vmx_instruction_info); + type = kvm_register_read(vcpu, gpr_index); if (type > 3) { kvm_inject_gp(vcpu, 0); diff --git a/arch/x86/kvm/vmx/vmx.h b/arch/x86/kvm/vmx/vmx.h index e7db42e3b0ce..95c9bca45cdd 100644 --- a/arch/x86/kvm/vmx/vmx.h +++ b/arch/x86/kvm/vmx/vmx.h @@ -522,4 +522,9 @@ static inline bool vmx_guest_state_valid(struct kvm_vcpu *vcpu) void dump_vmcs(struct kvm_vcpu *vcpu); +static inline int vmx_get_instr_info_reg2(u32 vmx_instr_info) +{ + return (vmx_instr_info >> 28) & 0xf; +} + #endif /* __KVM_X86_VMX_H */ From patchwork Tue Nov 9 17:44:26 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Vipin Sharma X-Patchwork-Id: 12610939 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 4500EC433F5 for ; Tue, 9 Nov 2021 17:44:43 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 2203961211 for ; Tue, 9 Nov 2021 17:44:43 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S241338AbhKIRr0 (ORCPT ); Tue, 9 Nov 2021 12:47:26 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45334 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S241247AbhKIRrY (ORCPT ); Tue, 9 Nov 2021 12:47:24 -0500 Received: from mail-pl1-x649.google.com (mail-pl1-x649.google.com [IPv6:2607:f8b0:4864:20::649]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 08C7EC061764 for ; Tue, 9 Nov 2021 09:44:38 -0800 (PST) Received: by mail-pl1-x649.google.com with SMTP id e10-20020a17090301ca00b00141fbe2569dso9382837plh.14 for ; Tue, 09 Nov 2021 09:44:38 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=3dSOMYowFmLGHL/9NT4zbRgDO3sEDt3CzFQDAO9NWgk=; b=SObRjv/OhHv0ur9cwKHlMrW4qPTrppoQnwfJzoOSb+z5vBnzFs24Qo4WHK0N5B/D1f 0jr+e59SrrS2GpYHgQr2UhsUXkVtUD3fUNKKtStgYmE62BgETzYJVZgn+nGM4EUQW5Tk nT7w9cKsVRwkxV3vVhtBYa65QUs+uUE9FQyWeS5Ru5e/l1GNtMoc8NnU1xbOlghpw5Vp yQ9GzjlmSJJRflFIDh3uJFls5gFN5pGMZMxXzhtvTwSBQfKmi0+21K6Dg22NHZtQ4epd saQKLParOtNg87FdMLaCP/zT7D/LgcCAKJNgI76AJ+1sIX5O97ZldhuodjRIkw1VMQBV G2Jw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=3dSOMYowFmLGHL/9NT4zbRgDO3sEDt3CzFQDAO9NWgk=; b=fIQZ5PZjk2D/DJ0Ymsuc6iuhoqK7Nihesmr0fuITNOGDhKE9JTqUeWeWAIJYM7dE+j tE/d+Cy/wMCpgnRkPotQZu1bg8vIgU7k5JK61Jl++dhdYtLylkQwyQiy8He4+9ThgXGq RDpzK0m3VUTzO/mSz0m1lANLgZC3E2mJmd3bKRwMgV6vv9d9JcQ/pKh5gjbYVFQoZRqs cH+3Gn13UB9sh4Zrc7XTZERgPcJrXS01dAu8wSA0deKwTlBHTSFZnypkPe794phKRYx8 kBut19Kq7O9ELFTAd4ZmoojWKa4B01tX58pgPczQVjNY14Me6+IuxrTY/r91jikxbns7 lNrQ== X-Gm-Message-State: AOAM533Zr6Yau7dg3Kds3rFi96tpAirqooT8zI1VfQHNPPz6EP6m6tag YUW9nWGCF/cs9b1Rr8Jkfysf/RciqaFJ X-Google-Smtp-Source: ABdhPJyE5IYejljYQWvewvDju+mOIcTWCplOjKcPhZmhCnDPFJOZ2vkepEEZAEdcspVGrpHnJGbjN1oo5GIK X-Received: from vipinsh.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:36b0]) (user=vipinsh job=sendgmr) by 2002:a17:902:c407:b0:142:28fe:668e with SMTP id k7-20020a170902c40700b0014228fe668emr8947697plk.31.1636479877545; Tue, 09 Nov 2021 09:44:37 -0800 (PST) Date: Tue, 9 Nov 2021 17:44:26 +0000 In-Reply-To: <20211109174426.2350547-1-vipinsh@google.com> Message-Id: <20211109174426.2350547-3-vipinsh@google.com> Mime-Version: 1.0 References: <20211109174426.2350547-1-vipinsh@google.com> X-Mailer: git-send-email 2.34.0.rc0.344.g81b53c2807-goog Subject: [PATCH v4 2/2] KVM: Move INVPCID type check from vmx and svm to the common kvm_handle_invpcid() From: Vipin Sharma To: pbonzini@redhat.com, seanjc@google.com, jmattson@google.com Cc: dmatlack@google.com, kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Vipin Sharma Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org Handle #GP on INVPCID due to an invalid type in the common switch statement instead of relying on the callers (VMX and SVM) to manually validate the type. Unlike INVVPID and INVEPT, INVPCID is not explicitly documented to check the type before reading the operand from memory, so deferring the type validity check until after that point is architecturally allowed. Signed-off-by: Vipin Sharma Reviewed-by: Sean Christopherson --- arch/x86/kvm/svm/svm.c | 5 ----- arch/x86/kvm/vmx/vmx.c | 5 ----- arch/x86/kvm/x86.c | 3 ++- 3 files changed, 2 insertions(+), 11 deletions(-) diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c index 21bb81710e0f..ccbf96876ec6 100644 --- a/arch/x86/kvm/svm/svm.c +++ b/arch/x86/kvm/svm/svm.c @@ -3119,11 +3119,6 @@ static int invpcid_interception(struct kvm_vcpu *vcpu) type = svm->vmcb->control.exit_info_2; gva = svm->vmcb->control.exit_info_1; - if (type > 3) { - kvm_inject_gp(vcpu, 0); - return 1; - } - return kvm_handle_invpcid(vcpu, type, gva); } diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c index e41d207e3298..a3bb9854f4d2 100644 --- a/arch/x86/kvm/vmx/vmx.c +++ b/arch/x86/kvm/vmx/vmx.c @@ -5505,11 +5505,6 @@ static int handle_invpcid(struct kvm_vcpu *vcpu) gpr_index = vmx_get_instr_info_reg2(vmx_instruction_info); type = kvm_register_read(vcpu, gpr_index); - if (type > 3) { - kvm_inject_gp(vcpu, 0); - return 1; - } - /* According to the Intel instruction reference, the memory operand * is read even if it isn't needed (e.g., for type==all) */ diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index ac83d873d65b..134585027e92 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -12443,7 +12443,8 @@ int kvm_handle_invpcid(struct kvm_vcpu *vcpu, unsigned long type, gva_t gva) return kvm_skip_emulated_instruction(vcpu); default: - BUG(); /* We have already checked above that type <= 3 */ + kvm_inject_gp(vcpu, 0); + return 1; } } EXPORT_SYMBOL_GPL(kvm_handle_invpcid);