From patchwork Tue Jun 16 22:03:36 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Babu Moger X-Patchwork-Id: 11608723 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 8C23F618 for ; Tue, 16 Jun 2020 22:03:48 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 672BB214F1 for ; Tue, 16 Jun 2020 22:03:48 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=amdcloud.onmicrosoft.com header.i=@amdcloud.onmicrosoft.com header.b="EIaZJQUf" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726414AbgFPWDo (ORCPT ); Tue, 16 Jun 2020 18:03:44 -0400 Received: from mail-dm6nam10on2087.outbound.protection.outlook.com ([40.107.93.87]:26966 "EHLO NAM10-DM6-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1726397AbgFPWDo (ORCPT ); Tue, 16 Jun 2020 18:03:44 -0400 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=jv+it1B7pbkP4rfp4CYvt+sSlq4v17xZF8PicswCvQDt+G/NuVTh5Y+c6z8BHuk9XGIJhY2T7N5is/FXs48Sg2ldVQQ75E7vGHpeEIrA4/15ENvXMqgpYTvH2BbiE7IQnyU2A13LlDrYRk9yz0FowNwnL+UgYpoehyO3YTV+zLOXtuZFdcSG/ntMIaKWUBbpLxZXkjGrguoYQfNHWOMTRTTm+VYk3SXnimvDKu0qQz1pK0prq1vjFj0Q43qulfJMBomSZbVtyv253UibLn5k0e6IB4/oTbtLlqIP1cj8haS/i2cpnW7MdiXwK6wAuDS1Q2BafCJ0AMqzdg81uFo3gA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=YEBUmA5sOctOQ7RAonG8lN4Opt2pnHURJXk/mUicfDA=; b=eWN9Vlhju6aRsoTxENKs/0YRawduubL9dap9oBpWB8qi42tGJIobTeM1CdK3YfWPsKtxFp5moLGXyYdb5B2hS46ED5CVpdECSPedJg+cV38jHatnnrya6qY3e63/Cz7JxfTHXOZMcUM/p105+FOVnXUoziZIpua4aKGbcY5pZY4Q4WVcQ/PryqKiDQHM17TCZoiVthLYy0MBc3gHMDWgb7NCVJiLfefEZMIaxqtEkga3mMbZZww2lXqoQLRglA+B77gX1jKzZSwxBBmvKnwLNWPmjCjKewKBHPbMytPCZrGxoWJCqseuq2vxQfGD8NbchnJo3wBqTkhWswAYQd9Vkw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=amd.com; dmarc=pass action=none header.from=amd.com; dkim=pass header.d=amd.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amdcloud.onmicrosoft.com; s=selector2-amdcloud-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=YEBUmA5sOctOQ7RAonG8lN4Opt2pnHURJXk/mUicfDA=; b=EIaZJQUfI9OJ6YE3kN9LZZzKSMy3nGPZQoZvHayJJTg8euhOPFpaWQs2KEPcX6ezapLjN6XDOjYwbsnBvR8OpoIT7gDj7IuQv3s0kw9N+XzNFvubJoP/UqdUc8JUG88dJIlW3tPx3T/JO0+sy+DpoB33MY3WxrFEoehX52K57vk= Authentication-Results: tencent.com; dkim=none (message not signed) header.d=none;tencent.com; dmarc=none action=none header.from=amd.com; Received: from SN1PR12MB2560.namprd12.prod.outlook.com (2603:10b6:802:26::19) by SN6PR12MB2622.namprd12.prod.outlook.com (2603:10b6:805:72::15) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3088.18; Tue, 16 Jun 2020 22:03:38 +0000 Received: from SN1PR12MB2560.namprd12.prod.outlook.com ([fe80::2102:cc6b:b2db:4c2]) by SN1PR12MB2560.namprd12.prod.outlook.com ([fe80::2102:cc6b:b2db:4c2%3]) with mapi id 15.20.3088.029; Tue, 16 Jun 2020 22:03:38 +0000 Subject: [PATCH v2 1/3] KVM: X86: Move handling of INVPCID types to x86 From: Babu Moger To: wanpengli@tencent.com, joro@8bytes.org, x86@kernel.org, sean.j.christopherson@intel.com, mingo@redhat.com, bp@alien8.de, hpa@zytor.com, pbonzini@redhat.com, vkuznets@redhat.com, tglx@linutronix.de, jmattson@google.com Cc: linux-kernel@vger.kernel.org, kvm@vger.kernel.org Date: Tue, 16 Jun 2020 17:03:36 -0500 Message-ID: <159234501692.6230.5105866433978454983.stgit@bmoger-ubuntu> In-Reply-To: <159234483706.6230.13753828995249423191.stgit@bmoger-ubuntu> References: <159234483706.6230.13753828995249423191.stgit@bmoger-ubuntu> User-Agent: StGit/0.17.1-dirty X-ClientProxiedBy: DM5PR16CA0001.namprd16.prod.outlook.com (2603:10b6:3:c0::11) To SN1PR12MB2560.namprd12.prod.outlook.com (2603:10b6:802:26::19) MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from [127.0.1.1] (165.204.77.1) by DM5PR16CA0001.namprd16.prod.outlook.com (2603:10b6:3:c0::11) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3088.19 via Frontend Transport; Tue, 16 Jun 2020 22:03:37 +0000 X-Originating-IP: [165.204.77.1] X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-HT: Tenant X-MS-Office365-Filtering-Correlation-Id: d1145439-1343-4908-229c-08d812411faf X-MS-TrafficTypeDiagnostic: SN6PR12MB2622: X-MS-Exchange-Transport-Forked: True X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:7691; X-Forefront-PRVS: 04362AC73B X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: MLnamlxHXmaOFNRX4fuqqSmCg0zo5SeJiF8jYUX/uyw5zdN2OQwRmnn0+ljRmEbjIfVpytc/o7qFNP82S/T2+mMpE4ZEvXH/FNiAygpiO4ohb6Q5KV9fg/2qYTR1Q9BmJGM06q6ezRfkPx4R0FhwKyevLb1p+xrBDLUsXKwKqw/fdF2JZ+rv0MPn9LNpMwQKFOKxfqLZqGyNcLT4cJnz28hl2Ikb0cWdhe6mgpx08vGxZsN2M/AzWkx0H0QzG3HWAHTDWPWk6WpVbWuk22joFx7UL5b9ygh15QxlSqax+s49RRbeUDj1dpJ0bVWH1XDLuE+AZestZSXp9z2zwef85VG9v9d2uEmYErF0Zhx+o1HTOdWXN+JBBEor5RR0/K3i X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:SN1PR12MB2560.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFTY:;SFS:(4636009)(7916004)(366004)(39860400002)(136003)(396003)(376002)(346002)(7416002)(8676002)(83380400001)(478600001)(5660300002)(16576012)(8936002)(186003)(4326008)(103116003)(16526019)(2906002)(316002)(26005)(66476007)(66946007)(66556008)(956004)(33716001)(6486002)(9686003)(44832011)(52116002)(86362001)(921003);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData: 6QNrCNPV6HWFmUrbEKNG4+JgkhHkYqzhHlYZQkmtPYe27kDFMnEwvhvahOu1yMPrwZS+4I0TnI1edzlYMmxuVYfLKoALc/LlTU4h0hszbReoLvNHoTjn+W6ETghVB2xw94CRsHsIInhk13yhm5QPP4kLgFDgodtsWObQuyFbpKDqyy8MPTogFvZwM1Y7yPS2sTRdZYfghJxcS7iOcfeDzGBStsP9NMMwBmaSnpsTRzia/SsbW2MkAjJNdISNUOjdZNUHIPwfpvNRxgdO7AdGXKxqIp+RuLns7T3sFVkXeedFM3g49sW2UjCPhxzZL52ZUKGndidfr5VrrXVvsn+a+NOJuuhLnUX2D3fUKkK5UKIyl7TdLS3BiOFyg83XA2SbM0aRdKC8D/EFhstaRZL6AmSMAxgQmK9FH4teRrFDvXUks+El5EF89uGzWeR7o0iENCV8ar8lZSKOZN/XpZeFzvcPMGrLlKFSzzNoCoaT36k= X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-Network-Message-Id: d1145439-1343-4908-229c-08d812411faf X-MS-Exchange-CrossTenant-OriginalArrivalTime: 16 Jun 2020 22:03:38.7550 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: vmj55qUTecJ3H62FZMvbyFX8cvZf02LgZjYpDeCq292LHKQGKvmVGd3PyXkgagTX X-MS-Exchange-Transport-CrossTenantHeadersStamped: SN6PR12MB2622 Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org INVPCID instruction handling is mostly same across both VMX and SVM. So, move the code to common x86.c. Signed-off-by: Babu Moger --- arch/x86/kvm/vmx/vmx.c | 68 +---------------------------------------- arch/x86/kvm/x86.c | 79 ++++++++++++++++++++++++++++++++++++++++++++++++ arch/x86/kvm/x86.h | 3 +- 3 files changed, 82 insertions(+), 68 deletions(-) diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c index 170cc76a581f..b4140cfd15fd 100644 --- a/arch/x86/kvm/vmx/vmx.c +++ b/arch/x86/kvm/vmx/vmx.c @@ -5477,11 +5477,7 @@ static int handle_invpcid(struct kvm_vcpu *vcpu) { u32 vmx_instruction_info; unsigned long type; - bool pcid_enabled; gva_t gva; - struct x86_exception e; - unsigned i; - unsigned long roots_to_free = 0; struct { u64 pcid; u64 gla; @@ -5508,69 +5504,7 @@ static int handle_invpcid(struct kvm_vcpu *vcpu) sizeof(operand), &gva)) return 1; - if (kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e)) { - kvm_inject_emulated_page_fault(vcpu, &e); - return 1; - } - - if (operand.pcid >> 12 != 0) { - kvm_inject_gp(vcpu, 0); - return 1; - } - - pcid_enabled = kvm_read_cr4_bits(vcpu, X86_CR4_PCIDE); - - switch (type) { - case INVPCID_TYPE_INDIV_ADDR: - if ((!pcid_enabled && (operand.pcid != 0)) || - is_noncanonical_address(operand.gla, vcpu)) { - kvm_inject_gp(vcpu, 0); - return 1; - } - kvm_mmu_invpcid_gva(vcpu, operand.gla, operand.pcid); - return kvm_skip_emulated_instruction(vcpu); - - case INVPCID_TYPE_SINGLE_CTXT: - if (!pcid_enabled && (operand.pcid != 0)) { - kvm_inject_gp(vcpu, 0); - return 1; - } - - if (kvm_get_active_pcid(vcpu) == operand.pcid) { - kvm_mmu_sync_roots(vcpu); - kvm_make_request(KVM_REQ_TLB_FLUSH_CURRENT, vcpu); - } - - for (i = 0; i < KVM_MMU_NUM_PREV_ROOTS; i++) - if (kvm_get_pcid(vcpu, vcpu->arch.mmu->prev_roots[i].pgd) - == operand.pcid) - roots_to_free |= KVM_MMU_ROOT_PREVIOUS(i); - - kvm_mmu_free_roots(vcpu, vcpu->arch.mmu, roots_to_free); - /* - * If neither the current cr3 nor any of the prev_roots use the - * given PCID, then nothing needs to be done here because a - * resync will happen anyway before switching to any other CR3. - */ - - return kvm_skip_emulated_instruction(vcpu); - - case INVPCID_TYPE_ALL_NON_GLOBAL: - /* - * Currently, KVM doesn't mark global entries in the shadow - * page tables, so a non-global flush just degenerates to a - * global flush. If needed, we could optimize this later by - * keeping track of global entries in shadow page tables. - */ - - /* fall-through */ - case INVPCID_TYPE_ALL_INCL_GLOBAL: - kvm_mmu_unload(vcpu); - return kvm_skip_emulated_instruction(vcpu); - - default: - BUG(); /* We have already checked above that type <= 3 */ - } + return kvm_handle_invpcid_types(vcpu, gva, type); } static int handle_pml_full(struct kvm_vcpu *vcpu) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 9e41b5135340..9c858ca0e592 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -70,6 +70,7 @@ #include #include #include +#include #include #include #include @@ -10714,6 +10715,84 @@ u64 kvm_spec_ctrl_valid_bits(struct kvm_vcpu *vcpu) } EXPORT_SYMBOL_GPL(kvm_spec_ctrl_valid_bits); +int kvm_handle_invpcid_types(struct kvm_vcpu *vcpu, gva_t gva, + unsigned long type) +{ + unsigned long roots_to_free = 0; + struct x86_exception e; + bool pcid_enabled; + unsigned int i; + struct { + u64 pcid; + u64 gla; + } operand; + + if (kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e)) { + kvm_inject_emulated_page_fault(vcpu, &e); + return 1; + } + + if (operand.pcid >> 12 != 0) { + kvm_inject_gp(vcpu, 0); + return 1; + } + + pcid_enabled = kvm_read_cr4_bits(vcpu, X86_CR4_PCIDE); + + switch (type) { + case INVPCID_TYPE_INDIV_ADDR: + if ((!pcid_enabled && (operand.pcid != 0)) || + is_noncanonical_address(operand.gla, vcpu)) { + kvm_inject_gp(vcpu, 0); + return 1; + } + kvm_mmu_invpcid_gva(vcpu, operand.gla, operand.pcid); + return kvm_skip_emulated_instruction(vcpu); + + case INVPCID_TYPE_SINGLE_CTXT: + if (!pcid_enabled && (operand.pcid != 0)) { + kvm_inject_gp(vcpu, 0); + return 1; + } + + if (kvm_get_active_pcid(vcpu) == operand.pcid) { + kvm_mmu_sync_roots(vcpu); + kvm_make_request(KVM_REQ_TLB_FLUSH_CURRENT, vcpu); + } + + for (i = 0; i < KVM_MMU_NUM_PREV_ROOTS; i++) + if (kvm_get_pcid(vcpu, vcpu->arch.mmu->prev_roots[i].pgd) + == operand.pcid) + roots_to_free |= KVM_MMU_ROOT_PREVIOUS(i); + + kvm_mmu_free_roots(vcpu, vcpu->arch.mmu, roots_to_free); + /* + * If neither the current cr3 nor any of the prev_roots use the + * given PCID, then nothing needs to be done here because a + * resync will happen anyway before switching to any other CR3. + */ + + return kvm_skip_emulated_instruction(vcpu); + + case INVPCID_TYPE_ALL_NON_GLOBAL: + /* + * Currently, KVM doesn't mark global entries in the shadow + * page tables, so a non-global flush just degenerates to a + * global flush. If needed, we could optimize this later by + * keeping track of global entries in shadow page tables. + */ + + /* fall-through */ + case INVPCID_TYPE_ALL_INCL_GLOBAL: + kvm_mmu_unload(vcpu); + return kvm_skip_emulated_instruction(vcpu); + + default: + BUG(); /* We have already checked above that type <= 3 */ + } +} +EXPORT_SYMBOL_GPL(kvm_handle_invpcid_types); + EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_exit); EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_fast_mmio); EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_inj_virq); diff --git a/arch/x86/kvm/x86.h b/arch/x86/kvm/x86.h index 6eb62e97e59f..f706f6f7196d 100644 --- a/arch/x86/kvm/x86.h +++ b/arch/x86/kvm/x86.h @@ -365,5 +365,6 @@ void kvm_load_guest_xsave_state(struct kvm_vcpu *vcpu); void kvm_load_host_xsave_state(struct kvm_vcpu *vcpu); u64 kvm_spec_ctrl_valid_bits(struct kvm_vcpu *vcpu); bool kvm_vcpu_exit_request(struct kvm_vcpu *vcpu); - +int kvm_handle_invpcid_types(struct kvm_vcpu *vcpu, gva_t gva, + unsigned long type); #endif From patchwork Tue Jun 16 22:03:43 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Babu Moger X-Patchwork-Id: 11608725 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 10224138C for ; Tue, 16 Jun 2020 22:03:57 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id E72C321508 for ; Tue, 16 Jun 2020 22:03:56 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=amdcloud.onmicrosoft.com header.i=@amdcloud.onmicrosoft.com header.b="s/NuqB+t" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726466AbgFPWDw (ORCPT ); Tue, 16 Jun 2020 18:03:52 -0400 Received: from mail-mw2nam12on2089.outbound.protection.outlook.com ([40.107.244.89]:29120 "EHLO NAM12-MW2-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1726397AbgFPWDv (ORCPT ); Tue, 16 Jun 2020 18:03:51 -0400 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=KQZGQw1+i+2QmeUOi3VeDbhFcPROhlXDlkpTRCh8cynG71Cc29xvT3EQkJosP/KvkrNjJ3nAVikcNDMRWsWhZdDZ5AeAOZIxSpCjkFNXe2+p+tvPCcjn6bBsR6AgJGyi4pU8X1XoANBWHQ6s3q3dRiYZi5L1cOYQqTaN30175M3Oiw6Ehtx3H8Wp2n6XuuuIIKBnFXgSgQBzx4vL9QkYMwLPvNwak1gOZKFwr+oTklgamRACYEiI1o0EqU/uCelffUP3DI5ludGqeHq6YDTMdoWX4g2yIIT2wr5uQ6K6VuXIU2/IG2YPCgCi3+liSTPcrVdFoXxKXsSFd0AKdSH1pg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=GkCbFx1uj/us6VMaPGyeUXTCIk1Md1eRoxx9Pkn6b2s=; b=G+qgGW1iuRGuyQs/ofckXpbjgo4DtpezyxSFA+bwk6pNW6ZxbouJeVaklghbr1thHbVx3IkZrVAMTvZXbVsrksO9JgEMc4ruQgdi5W1SZEDMvsEgdOQ92BRT7vTECxuGjNMZwxhx6zVZBtqMetC1MIxfY5JpZEij9ndEMTF+Eq5FVCTn4rm5pi5I8cEfCqcwUAt0Fe3ctwwoouuetD8O/HH1z+9iOgvEAyqnvx5Evzz8mu4eYZMrISHDp3l0OeEbHYAtZafJtsYMC5/5Xn9XjRAv9qCIBke+vkTJo+dSOkVzx0NMYWo+/eCfddQJnKfvdOqtFqooqYj5Xow7bDD3Lw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=amd.com; dmarc=pass action=none header.from=amd.com; dkim=pass header.d=amd.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amdcloud.onmicrosoft.com; s=selector2-amdcloud-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=GkCbFx1uj/us6VMaPGyeUXTCIk1Md1eRoxx9Pkn6b2s=; b=s/NuqB+tCZmklBJwQyfqIFHJx+3MJBTZSnM45AnZylFYtMVI5W8Iw8KPRtFFKS93+ElO5NhoNRg/4oiUFmMygVcT23E1o6GOuCWqBlyBj+jcQooMrzWFJXT+jmQzBehavtFzCyUMmoK9hdiM3aXGCqkxJPdT1j/ApE8ELeLH5jM= Authentication-Results: tencent.com; dkim=none (message not signed) header.d=none;tencent.com; dmarc=none action=none header.from=amd.com; Received: from SN1PR12MB2560.namprd12.prod.outlook.com (2603:10b6:802:26::19) by SN6PR12MB2622.namprd12.prod.outlook.com (2603:10b6:805:72::15) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3088.18; Tue, 16 Jun 2020 22:03:46 +0000 Received: from SN1PR12MB2560.namprd12.prod.outlook.com ([fe80::2102:cc6b:b2db:4c2]) by SN1PR12MB2560.namprd12.prod.outlook.com ([fe80::2102:cc6b:b2db:4c2%3]) with mapi id 15.20.3088.029; Tue, 16 Jun 2020 22:03:46 +0000 Subject: [PATCH v2 2/3] KVM:SVM: Add extended intercept support From: Babu Moger To: wanpengli@tencent.com, joro@8bytes.org, x86@kernel.org, sean.j.christopherson@intel.com, mingo@redhat.com, bp@alien8.de, hpa@zytor.com, pbonzini@redhat.com, vkuznets@redhat.com, tglx@linutronix.de, jmattson@google.com Cc: linux-kernel@vger.kernel.org, kvm@vger.kernel.org Date: Tue, 16 Jun 2020 17:03:43 -0500 Message-ID: <159234502394.6230.5169466123693241678.stgit@bmoger-ubuntu> In-Reply-To: <159234483706.6230.13753828995249423191.stgit@bmoger-ubuntu> References: <159234483706.6230.13753828995249423191.stgit@bmoger-ubuntu> User-Agent: StGit/0.17.1-dirty X-ClientProxiedBy: DM5PR18CA0092.namprd18.prod.outlook.com (2603:10b6:3:3::30) To SN1PR12MB2560.namprd12.prod.outlook.com (2603:10b6:802:26::19) MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from [127.0.1.1] (165.204.77.1) by DM5PR18CA0092.namprd18.prod.outlook.com (2603:10b6:3:3::30) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3109.21 via Frontend Transport; Tue, 16 Jun 2020 22:03:44 +0000 X-Originating-IP: [165.204.77.1] X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-HT: Tenant X-MS-Office365-Filtering-Correlation-Id: 8b7975fd-1a4e-4de1-1dd2-08d8124123f0 X-MS-TrafficTypeDiagnostic: SN6PR12MB2622: X-MS-Exchange-Transport-Forked: True X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:8273; X-Forefront-PRVS: 04362AC73B X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: yIaZbOXue1Gcp0CGUzNlzcB/WvM3tqNqKZtiFSfRvlFwqCo08W08efsACostdB0CBDN7nGA6pQWqFQLJAQH7LxpvmXIxXuQieH17R2ae6ie6KCRTQEcSwkpaJA16QP+35zpO7Fv0INI9xQeVcfi2Fs02r1THiNeKoqKS5JVTjPeOr5ahp08FaGT7OXx8vV0V6/1X/3ifxychvEk0GKrbCnXPQ881f+sf8PGAMxmUSgOtL6IEMEI9b5YmFhpPZCp3BrelQpTyoWTd64yU3XQtOy04bIEi/ZnPSPXUdYrw9DirH5dG7fgdfQChyo3CtvRBtC1vlWkpk42fus5oIquyVPC1pRHVPRdcWbNsI+Wit9+C4HwoUNAxFd/cnWHe1XFzOu2Sc8RNannCzMrkbI3NaiTu4XAYUidlI5iGFhQ8tlpGG85QIS0xpsawZTV0IQP6U3b36XSM2aldkfxGUYz2oA== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:SN1PR12MB2560.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFTY:;SFS:(4636009)(7916004)(366004)(39860400002)(136003)(396003)(376002)(346002)(7416002)(8676002)(83380400001)(478600001)(5660300002)(16576012)(966005)(8936002)(186003)(4326008)(103116003)(16526019)(2906002)(316002)(26005)(66476007)(66946007)(66556008)(956004)(33716001)(6486002)(9686003)(44832011)(52116002)(86362001)(921003);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData: 033vGGlvudT4Z1kVY1ajuYakNTn2Qgdb9o6HXkIhZGz4prREk4Y5RM3K0FQoUpJxnQJzvX7fPwjxltXeSwkBRbU65BLqpmKu10S1u3hCWrF9m4cfLRMCX2nvqrR72IAblKjiW2kiPOuBsiXfD/0ij3qlJgNMK7Pna27qU2ENeNalnvbqr5QXtavEEN5QAdZFalAOu95vjE1Bnjs01bXLZOzz60/rK5/bt8Y3vwZtqQE3V7+9ahXS6QgMRgK1F8MrHDQREo3qPt6K5kHE9M8W8FDf8l0BYptZMLerPGQrcUk79oRV5VN0EuI+bTA+przCx7Zvz/jxHZJuNRfQIDD1NXJwXvtZfs5z13JQTIr87/T8jgNZ/OwqJtIxE2rmil07sTmKltCZlF2WmZ6Yygy8VbNMD5vT/VbJhVYzzN8moawlqmQTycrWJ7hN55jQ12MNWhFhUem2JD48KrOCZq1oAW92T4NL1ile3XV7EQ5itjs= X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-Network-Message-Id: 8b7975fd-1a4e-4de1-1dd2-08d8124123f0 X-MS-Exchange-CrossTenant-OriginalArrivalTime: 16 Jun 2020 22:03:45.8927 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: 9LVqI2U3NbPHiLD1Yj1tkiv9U0eJzcKW9XE540ziENhS+/fFu50aG+G9yHzblKsZ X-MS-Exchange-Transport-CrossTenantHeadersStamped: SN6PR12MB2622 Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org The new intercept bits have been added in vmcb control area to support the interception of INVPCID instruction. The following bit is added to the VMCB layout control area to control intercept of INVPCID: Byte Offset Bit(s) Function 14h 2 intercept INVPCID Add the interfaces to support these extended interception. Also update the tracing for extended intercepts. AMD documentation for INVPCID feature is available at "AMD64 Architecture Programmer’s Manual Volume 2: System Programming, Pub. 24593 Rev. 3.34(or later)" The documentation can be obtained at the links below: Link: https://www.amd.com/system/files/TechDocs/24593.pdf Link: https://bugzilla.kernel.org/show_bug.cgi?id=206537 Signed-off-by: Babu Moger --- arch/x86/include/asm/svm.h | 3 ++- arch/x86/kvm/svm/nested.c | 6 +++++- arch/x86/kvm/svm/svm.c | 1 + arch/x86/kvm/svm/svm.h | 18 ++++++++++++++++++ arch/x86/kvm/trace.h | 12 ++++++++---- 5 files changed, 34 insertions(+), 6 deletions(-) diff --git a/arch/x86/include/asm/svm.h b/arch/x86/include/asm/svm.h index 8a1f5382a4ea..62649fba8908 100644 --- a/arch/x86/include/asm/svm.h +++ b/arch/x86/include/asm/svm.h @@ -61,7 +61,8 @@ struct __attribute__ ((__packed__)) vmcb_control_area { u32 intercept_dr; u32 intercept_exceptions; u64 intercept; - u8 reserved_1[40]; + u32 intercept_extended; + u8 reserved_1[36]; u16 pause_filter_thresh; u16 pause_filter_count; u64 iopm_base_pa; diff --git a/arch/x86/kvm/svm/nested.c b/arch/x86/kvm/svm/nested.c index 8a6db11dcb43..7f6d0f2533e2 100644 --- a/arch/x86/kvm/svm/nested.c +++ b/arch/x86/kvm/svm/nested.c @@ -121,6 +121,7 @@ void recalc_intercepts(struct vcpu_svm *svm) c->intercept_dr = h->intercept_dr; c->intercept_exceptions = h->intercept_exceptions; c->intercept = h->intercept; + c->intercept_extended = h->intercept_extended; if (g->int_ctl & V_INTR_MASKING_MASK) { /* We only want the cr8 intercept bits of L1 */ @@ -142,6 +143,7 @@ void recalc_intercepts(struct vcpu_svm *svm) c->intercept_dr |= g->intercept_dr; c->intercept_exceptions |= g->intercept_exceptions; c->intercept |= g->intercept; + c->intercept_extended |= g->intercept_extended; } static void copy_vmcb_control_area(struct vmcb_control_area *dst, @@ -151,6 +153,7 @@ static void copy_vmcb_control_area(struct vmcb_control_area *dst, dst->intercept_dr = from->intercept_dr; dst->intercept_exceptions = from->intercept_exceptions; dst->intercept = from->intercept; + dst->intercept_extended = from->intercept_extended; dst->iopm_base_pa = from->iopm_base_pa; dst->msrpm_base_pa = from->msrpm_base_pa; dst->tsc_offset = from->tsc_offset; @@ -433,7 +436,8 @@ int nested_svm_vmrun(struct vcpu_svm *svm) trace_kvm_nested_intercepts(nested_vmcb->control.intercept_cr & 0xffff, nested_vmcb->control.intercept_cr >> 16, nested_vmcb->control.intercept_exceptions, - nested_vmcb->control.intercept); + nested_vmcb->control.intercept, + nested_vmcb->control.intercept_extended); /* Clear internal status */ kvm_clear_exception_queue(&svm->vcpu); diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c index 9e333b91ff78..285e5e1ff518 100644 --- a/arch/x86/kvm/svm/svm.c +++ b/arch/x86/kvm/svm/svm.c @@ -2801,6 +2801,7 @@ static void dump_vmcb(struct kvm_vcpu *vcpu) pr_err("%-20s%04x\n", "dr_write:", control->intercept_dr >> 16); pr_err("%-20s%08x\n", "exceptions:", control->intercept_exceptions); pr_err("%-20s%016llx\n", "intercepts:", control->intercept); + pr_err("%-20s%08x\n", "intercepts (extended):", control->intercept_extended); pr_err("%-20s%d\n", "pause filter count:", control->pause_filter_count); pr_err("%-20s%d\n", "pause filter threshold:", control->pause_filter_thresh); diff --git a/arch/x86/kvm/svm/svm.h b/arch/x86/kvm/svm/svm.h index 6ac4c00a5d82..935d08fac03d 100644 --- a/arch/x86/kvm/svm/svm.h +++ b/arch/x86/kvm/svm/svm.h @@ -311,6 +311,24 @@ static inline void clr_intercept(struct vcpu_svm *svm, int bit) recalc_intercepts(svm); } +static inline void set_extended_intercept(struct vcpu_svm *svm, int bit) +{ + struct vmcb *vmcb = get_host_vmcb(svm); + + vmcb->control.intercept_extended |= (1U << bit); + + recalc_intercepts(svm); +} + +static inline void clr_extended_intercept(struct vcpu_svm *svm, int bit) +{ + struct vmcb *vmcb = get_host_vmcb(svm); + + vmcb->control.intercept_extended &= ~(1U << bit); + + recalc_intercepts(svm); +} + static inline bool is_intercept(struct vcpu_svm *svm, int bit) { return (svm->vmcb->control.intercept & (1ULL << bit)) != 0; diff --git a/arch/x86/kvm/trace.h b/arch/x86/kvm/trace.h index b66432b015d2..5c841c42b33d 100644 --- a/arch/x86/kvm/trace.h +++ b/arch/x86/kvm/trace.h @@ -544,14 +544,16 @@ TRACE_EVENT(kvm_nested_vmrun, ); TRACE_EVENT(kvm_nested_intercepts, - TP_PROTO(__u16 cr_read, __u16 cr_write, __u32 exceptions, __u64 intercept), - TP_ARGS(cr_read, cr_write, exceptions, intercept), + TP_PROTO(__u16 cr_read, __u16 cr_write, __u32 exceptions, __u64 intercept, + __u32 extended), + TP_ARGS(cr_read, cr_write, exceptions, intercept, extended), TP_STRUCT__entry( __field( __u16, cr_read ) __field( __u16, cr_write ) __field( __u32, exceptions ) __field( __u64, intercept ) + __field( __u32, extended ) ), TP_fast_assign( @@ -559,11 +561,13 @@ TRACE_EVENT(kvm_nested_intercepts, __entry->cr_write = cr_write; __entry->exceptions = exceptions; __entry->intercept = intercept; + __entry->extended = extended; ), - TP_printk("cr_read: %04x cr_write: %04x excp: %08x intercept: %016llx", + TP_printk("cr_read: %04x cr_write: %04x excp: %08x intercept: %016llx" + "intercept (extended): %08x", __entry->cr_read, __entry->cr_write, __entry->exceptions, - __entry->intercept) + __entry->intercept, __entry->extended) ); /* * Tracepoint for #VMEXIT while nested From patchwork Tue Jun 16 22:03:51 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Babu Moger X-Patchwork-Id: 11608727 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 3E9A3138C for ; Tue, 16 Jun 2020 22:04:03 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 202CF21508 for ; Tue, 16 Jun 2020 22:04:03 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=amdcloud.onmicrosoft.com header.i=@amdcloud.onmicrosoft.com header.b="wpuXIEV+" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726488AbgFPWD7 (ORCPT ); Tue, 16 Jun 2020 18:03:59 -0400 Received: from mail-mw2nam12on2053.outbound.protection.outlook.com ([40.107.244.53]:32786 "EHLO NAM12-MW2-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1726486AbgFPWD5 (ORCPT ); Tue, 16 Jun 2020 18:03:57 -0400 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=bF2Z2SmEU0iWiyIhr9+RYQqoQVWlnX5i8r4OUFOl7/ssQl9k3LhTgJ/p34Ij36dP/QzM56HgbhDbPlfxgCCGerroVONvXGkNJy5DzugYKy/zFWp3KJC511zv6dRaddcafKk91lMjOJuCDRwr4hkypV/DkY8rZNLn6gJnrMghsme2kPp0M6dlqb70hDg5Mb/yQ1Ti57qEV0FY9amw0sfeykgq0J01rG9WDbJMnLMxa340v9Enq4LKBa9SJ2zV0elg6dGd8NJDq5ImzJ320t7Zvn8awc+wZ3PWMd6O7/cGkidkugawtINLRAwE+mUr4MdIZ555UOPh8LHryg584J+icw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=cBcsGm0vC7Z2IO3DTw8IOPKKrr9TX7ShDPyM1NY1mTA=; b=Qud/jcrCxjnlQ7P3X6+NlWPZSmHPM7zJnQtjl9KThiVPR4U/fIYYDmBzbrNmbbDslXraunZg2T/Ax2oNjK47rvx8UqCmcXsb35uagkO/1Qe8DETFDyW16EzArJCKmzXFRPBAwYUn95sf7RdhQTzoqDhHUoXcjZhUaoNGCtO4B5mzK711dOs3CIGe5GyCJPS02pkvyKSSRF997hdYniOVQn+f31EeqXPzs5oxvlCpDL5v2qiVrPbvnuqm8rWHq3XRpjGB4GfHfXRxIv2hPOH7Yc57Hb61/d31+Pwgy3D8kQZDFgaOf2/MGkwh3mrh2YF0WD/xLNu1wfxofw0+9jd7nw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=amd.com; dmarc=pass action=none header.from=amd.com; dkim=pass header.d=amd.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amdcloud.onmicrosoft.com; s=selector2-amdcloud-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=cBcsGm0vC7Z2IO3DTw8IOPKKrr9TX7ShDPyM1NY1mTA=; b=wpuXIEV+NGTDv5sT5oA854gmRGIziWTNLNbxt1AKTI1pNgBEArk1c6jMnXfukpd1zJ9YFmWwJWB6+Lcsj2oSOLnbZz8PqzxmMPAFcLaS+ok6JsCzJICC4orTtDA801pJ+eOloCM11LW8pBXdqUg9wA/z9kSenY+4ISs6r+CVtWY= Authentication-Results: tencent.com; dkim=none (message not signed) header.d=none;tencent.com; dmarc=none action=none header.from=amd.com; Received: from SN1PR12MB2560.namprd12.prod.outlook.com (2603:10b6:802:26::19) by SN6PR12MB2622.namprd12.prod.outlook.com (2603:10b6:805:72::15) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3088.18; Tue, 16 Jun 2020 22:03:53 +0000 Received: from SN1PR12MB2560.namprd12.prod.outlook.com ([fe80::2102:cc6b:b2db:4c2]) by SN1PR12MB2560.namprd12.prod.outlook.com ([fe80::2102:cc6b:b2db:4c2%3]) with mapi id 15.20.3088.029; Tue, 16 Jun 2020 22:03:53 +0000 Subject: [PATCH v2 3/3] KVM:SVM: Enable INVPCID feature on AMD From: Babu Moger To: wanpengli@tencent.com, joro@8bytes.org, x86@kernel.org, sean.j.christopherson@intel.com, mingo@redhat.com, bp@alien8.de, hpa@zytor.com, pbonzini@redhat.com, vkuznets@redhat.com, tglx@linutronix.de, jmattson@google.com Cc: linux-kernel@vger.kernel.org, kvm@vger.kernel.org Date: Tue, 16 Jun 2020 17:03:51 -0500 Message-ID: <159234503110.6230.9885185856732531454.stgit@bmoger-ubuntu> In-Reply-To: <159234483706.6230.13753828995249423191.stgit@bmoger-ubuntu> References: <159234483706.6230.13753828995249423191.stgit@bmoger-ubuntu> User-Agent: StGit/0.17.1-dirty X-ClientProxiedBy: DM5PR15CA0048.namprd15.prod.outlook.com (2603:10b6:4:4b::34) To SN1PR12MB2560.namprd12.prod.outlook.com (2603:10b6:802:26::19) MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from [127.0.1.1] (165.204.77.1) by DM5PR15CA0048.namprd15.prod.outlook.com (2603:10b6:4:4b::34) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3088.21 via Frontend Transport; Tue, 16 Jun 2020 22:03:52 +0000 X-Originating-IP: [165.204.77.1] X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-HT: Tenant X-MS-Office365-Filtering-Correlation-Id: 1ed9f680-b485-45f2-103a-08d812412835 X-MS-TrafficTypeDiagnostic: SN6PR12MB2622: X-MS-Exchange-Transport-Forked: True X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:9508; X-Forefront-PRVS: 04362AC73B X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: yo7Xy1jQbcEXm/AqObZ01Fg2z8KAV0s1YP1da9XYpCsRoVq27y4HporIWOAl1Us94XKKAtJJcgwDtlPGa/hBxLm0Nnck0K7kVc5Q52gP+cVFvoFBLqLITfN4SA8HHoe+dQUL4J4bseNHruHus/QnrY8d4tkRKlO6yAi84kFD8eozxbhg+O8BQjqkB1uy/Lmq8UL+8GcGlpORMQrGCqzPQLxUskSekuhQWyK3JQ7Qc/Ol0PzWtsoyoyJlc0RbA76idKCgiAgMxH7hukzqPx6iChkUGDZ9e3TMQw5pqgWgI3IQg/REvzgWQKs131Jvc4WTyoF0Jw/CAgw19T3UwCfosjRZmDpx5H+6aE4WbWaQ9MvISEJwdoM5WiTBBO9Njg8hTW5lQEeDLnzuPSC3UjKyh4oLiIL5ob0pPnq8U+juvlPDhH6JOPCo0N0ZZv4tEuMN5wMkUduh2hcPPKSa1d1/LQ== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:SN1PR12MB2560.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFTY:;SFS:(4636009)(7916004)(366004)(39860400002)(136003)(396003)(376002)(346002)(7416002)(8676002)(83380400001)(478600001)(5660300002)(16576012)(966005)(8936002)(186003)(4326008)(103116003)(16526019)(2906002)(316002)(26005)(66476007)(66946007)(66556008)(956004)(33716001)(6486002)(9686003)(44832011)(52116002)(86362001)(921003);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData: MAaSjSo6OjmDsejwNLqMr+ouEbaRUT2DsXA/DfX9o6Cw2CPmeBpxuSHQD81K1RLKbd01KxndCIZhoWTTwe6rq1WwaFVQReJUVJ6mNX47BOn3w/RA2q/V2tv0YscswLKSbktWmizbbzksjQ1YIFo8EERs15+p3nz8hR5I3j+4aAJsd/llnt8kXVCnNtIDyOt8CfvjQ42+hyTI5wcXqz6hc8rFkwk84n9ITf8pxVTCAPeUN3ePO+vM1jVyWnx0eFBWxrss4p8EzuIG1kTvHNTGdJogBb53XlCvPoaKlc433bbfbq5tLPyS6WrgfvLzRAXjB+orHH7dbHgD0xZqcZipKX6IcKet80U8lHOEmkBdemr7W/LXeGxhraXMWn9R2wgKxnT98YypnKZ0xRbP/OL2sBSDOkQST1dcO8fhGF8FcsBPtlp818Fa7f2DESBqVV29kM0yFFawfzxR92B5ka4viOKYEO/pGT/nBYtzyqssQJ8= X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-Network-Message-Id: 1ed9f680-b485-45f2-103a-08d812412835 X-MS-Exchange-CrossTenant-OriginalArrivalTime: 16 Jun 2020 22:03:53.0674 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: TOR5FTm3CF7r0LkYPFc5HB00w021s3uxFueMzxMaLp85iXQTAsw2fGuTzuqAq2yQ X-MS-Exchange-Transport-CrossTenantHeadersStamped: SN6PR12MB2622 Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org The following intercept is added for INVPCID instruction: Code Name Cause A2h VMEXIT_INVPCID INVPCID instruction The following bit is added to the VMCB layout control area to control intercept of INVPCID: Byte Offset Bit(s) Function 14h 2 intercept INVPCID For the guests with nested page table (NPT) support, the INVPCID feature works as running it natively. KVM does not need to do any special handling in this case. Enable the interceptions when the the guest is running with shadow page table enabled and handle the tlbflush based on the type of invpcid instruction type. AMD documentation for INVPCID feature is available at "AMD64 Architecture Programmer’s Manual Volume 2: System Programming, Pub. 24593 Rev. 3.34(or later)" The documentation can be obtained at the links below: Link: https://www.amd.com/system/files/TechDocs/24593.pdf Link: https://bugzilla.kernel.org/show_bug.cgi?id=206537 Signed-off-by: Babu Moger --- arch/x86/include/asm/svm.h | 4 +++ arch/x86/include/uapi/asm/svm.h | 2 + arch/x86/kvm/svm/svm.c | 54 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 60 insertions(+) diff --git a/arch/x86/include/asm/svm.h b/arch/x86/include/asm/svm.h index 62649fba8908..6488094f67fa 100644 --- a/arch/x86/include/asm/svm.h +++ b/arch/x86/include/asm/svm.h @@ -55,6 +55,10 @@ enum { INTERCEPT_RDPRU, }; +/* Extended Intercept bits */ +enum { + INTERCEPT_INVPCID = 2, +}; struct __attribute__ ((__packed__)) vmcb_control_area { u32 intercept_cr; diff --git a/arch/x86/include/uapi/asm/svm.h b/arch/x86/include/uapi/asm/svm.h index 2e8a30f06c74..522d42dfc28c 100644 --- a/arch/x86/include/uapi/asm/svm.h +++ b/arch/x86/include/uapi/asm/svm.h @@ -76,6 +76,7 @@ #define SVM_EXIT_MWAIT_COND 0x08c #define SVM_EXIT_XSETBV 0x08d #define SVM_EXIT_RDPRU 0x08e +#define SVM_EXIT_INVPCID 0x0a2 #define SVM_EXIT_NPF 0x400 #define SVM_EXIT_AVIC_INCOMPLETE_IPI 0x401 #define SVM_EXIT_AVIC_UNACCELERATED_ACCESS 0x402 @@ -171,6 +172,7 @@ { SVM_EXIT_MONITOR, "monitor" }, \ { SVM_EXIT_MWAIT, "mwait" }, \ { SVM_EXIT_XSETBV, "xsetbv" }, \ + { SVM_EXIT_INVPCID, "invpcid" }, \ { SVM_EXIT_NPF, "npf" }, \ { SVM_EXIT_AVIC_INCOMPLETE_IPI, "avic_incomplete_ipi" }, \ { SVM_EXIT_AVIC_UNACCELERATED_ACCESS, "avic_unaccelerated_access" }, \ diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c index 285e5e1ff518..5d598a7a0289 100644 --- a/arch/x86/kvm/svm/svm.c +++ b/arch/x86/kvm/svm/svm.c @@ -813,6 +813,11 @@ static __init void svm_set_cpu_caps(void) if (boot_cpu_has(X86_FEATURE_LS_CFG_SSBD) || boot_cpu_has(X86_FEATURE_AMD_SSBD)) kvm_cpu_cap_set(X86_FEATURE_VIRT_SSBD); + + /* Enable INVPCID if both PCID and INVPCID enabled */ + if (boot_cpu_has(X86_FEATURE_PCID) && + boot_cpu_has(X86_FEATURE_INVPCID)) + kvm_cpu_cap_set(X86_FEATURE_INVPCID); } static __init int svm_hardware_setup(void) @@ -1099,6 +1104,18 @@ static void init_vmcb(struct vcpu_svm *svm) clr_intercept(svm, INTERCEPT_PAUSE); } + /* + * Intercept INVPCID instruction only if shadow page table is + * enabled. Interception is not required with nested page table + * enabled. + */ + if (boot_cpu_has(X86_FEATURE_INVPCID)) { + if (!npt_enabled) + set_extended_intercept(svm, INTERCEPT_INVPCID); + else + clr_extended_intercept(svm, INTERCEPT_INVPCID); + } + if (kvm_vcpu_apicv_active(&svm->vcpu)) avic_init_vmcb(svm); @@ -2715,6 +2732,33 @@ static int mwait_interception(struct vcpu_svm *svm) return nop_interception(svm); } +static int invpcid_interception(struct vcpu_svm *svm) +{ + struct kvm_vcpu *vcpu = &svm->vcpu; + unsigned long type; + gva_t gva; + + if (!guest_cpuid_has(vcpu, X86_FEATURE_INVPCID)) { + kvm_queue_exception(vcpu, UD_VECTOR); + return 1; + } + + /* + * For an INVPCID intercept: + * EXITINFO1 provides the linear address of the memory operand. + * EXITINFO2 provides the contents of the register operand. + */ + 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_types(vcpu, gva, type); +} + static int (*const svm_exit_handlers[])(struct vcpu_svm *svm) = { [SVM_EXIT_READ_CR0] = cr_interception, [SVM_EXIT_READ_CR3] = cr_interception, @@ -2777,6 +2821,7 @@ static int (*const svm_exit_handlers[])(struct vcpu_svm *svm) = { [SVM_EXIT_MWAIT] = mwait_interception, [SVM_EXIT_XSETBV] = xsetbv_interception, [SVM_EXIT_RDPRU] = rdpru_interception, + [SVM_EXIT_INVPCID] = invpcid_interception, [SVM_EXIT_NPF] = npf_interception, [SVM_EXIT_RSM] = rsm_interception, [SVM_EXIT_AVIC_INCOMPLETE_IPI] = avic_incomplete_ipi_interception, @@ -3562,6 +3607,15 @@ static void svm_cpuid_update(struct kvm_vcpu *vcpu) svm->nrips_enabled = kvm_cpu_cap_has(X86_FEATURE_NRIPS) && guest_cpuid_has(&svm->vcpu, X86_FEATURE_NRIPS); + /* Check again if INVPCID interception if required */ + if (boot_cpu_has(X86_FEATURE_INVPCID) && + guest_cpuid_has(vcpu, X86_FEATURE_INVPCID)) { + if (!npt_enabled) + set_extended_intercept(svm, INTERCEPT_INVPCID); + else + clr_extended_intercept(svm, INTERCEPT_INVPCID); + } + if (!kvm_vcpu_apicv_active(vcpu)) return;