From patchwork Mon Aug 22 23:28:02 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Brijesh Singh X-Patchwork-Id: 9294817 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 7FDFD608A7 for ; Tue, 23 Aug 2016 00:01:50 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 6EFFF28A9C for ; Tue, 23 Aug 2016 00:01:50 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 6234B28AAA; Tue, 23 Aug 2016 00:01:50 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.8 required=2.0 tests=BAYES_00,DKIM_SIGNED, RCVD_IN_DNSWL_HI,T_DKIM_INVALID autolearn=unavailable version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id A781B28AA0 for ; Tue, 23 Aug 2016 00:01:49 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1757298AbcHWAB3 (ORCPT ); Mon, 22 Aug 2016 20:01:29 -0400 Received: from mail-sn1nam01on0083.outbound.protection.outlook.com ([104.47.32.83]:62399 "EHLO NAM01-SN1-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1757292AbcHWAB0 (ORCPT ); Mon, 22 Aug 2016 20:01:26 -0400 X-Greylist: delayed 1298 seconds by postgrey-1.27 at vger.kernel.org; Mon, 22 Aug 2016 20:01:25 EDT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amdcloud.onmicrosoft.com; s=selector1-amd-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version; bh=ZMWPpfxUfjgRNzIl6stUkf/lyoK0zA6EuEauEePa0a0=; b=A2IvVWkCgX6fkEcbkXIWlKCQA5O23sry91HZGyR6krqArthPNRN3Qt/MQriM2tY9x1qY40lxYyKjMPifj8Zaix1296P8iwZFexnYXlhobTKeQbOPRSBlQZOPfkPxvtAvRYF2O6pec9sOKyWcGlCrJ7CP6X1gd9OV+dPAFRQ/N+E= Authentication-Results: spf=none (sender IP is ) smtp.mailfrom=brijesh.singh@amd.com; Received: from [127.0.1.1] (165.204.77.1) by CY1PR12MB0667.namprd12.prod.outlook.com (10.163.238.152) with Microsoft SMTP Server (version=TLS1_0, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA_P384) id 15.1.557.21; Mon, 22 Aug 2016 23:28:06 +0000 Subject: [RFC PATCH v1 20/28] KVM: SVM: prepare for SEV guest management API support From: Brijesh Singh To: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Date: Mon, 22 Aug 2016 19:28:02 -0400 Message-ID: <147190848221.9523.931142742439444357.stgit@brijesh-build-machine> In-Reply-To: <147190820782.9523.4967724730957229273.stgit@brijesh-build-machine> References: <147190820782.9523.4967724730957229273.stgit@brijesh-build-machine> User-Agent: StGit/0.17.1-dirty MIME-Version: 1.0 X-Originating-IP: [165.204.77.1] X-ClientProxiedBy: BN6PR17CA0029.namprd17.prod.outlook.com (10.175.189.15) To CY1PR12MB0667.namprd12.prod.outlook.com (10.163.238.152) X-MS-Office365-Filtering-Correlation-Id: 8a4fe64d-99d6-4d1f-6c6c-08d3cae3fafb X-Microsoft-Exchange-Diagnostics: 1; CY1PR12MB0667; 2:yxrefCUd3cwTcwVbO5W0c0v8Mcdrn5DUPB/A7QRDRoOccJzEPtm7QGUFIWa5OjdU7Wa+wHd+lSbXNL9gcHkWOUMdsWY0A+szfLOfWiRulAqrCyHxPRccU4Bl7a0kI7QWGaiLoqFACtOVdOfoc7o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k1ocPirbelyQw5u8eyQ2iWP4FmcjKm6YS0MbE2XIASmU8DW+zF9ZU+MPavqqXN5ZsLAiEIJ2xkvVZw/GQIePLyGVUPjEc/XATYNc2CD2yezk4G+Sn58MRKoKvAnrMPYEuWSNhEZk46yB6SgejA6q7t7RLIOLD7mqIp5eiHyXGR249zYJ9P+gL3uzqpKIAcd3mT6fTbavzWY2YA8yQMPEAHc6ASdBflcskWO5nYuTObNJT1CKWxd+p2c2jqdNXseIdkT0t9MIR1GemuYdYF8xrOeYhz3+OgD1kzE0+015XRIZL4KVUf+KPfQCyTCHW56sY51p0rlCeXiZATS/Q4peeqcf6+e0zx+Tq7m6q6v6A82jDrtaIygfKHQbdPuGIxzKkfCuARJZR X-Microsoft-Antispam-PRVS: X-Exchange-Antispam-Report-Test: UriScan:(767451399110); X-Exchange-Antispam-Report-CFA-Test: BCL:0; PCL:0; RULEID:(6040176)(601004)(2401047)(5005006)(8121501046)(10201501046)(3002001)(6055026); SRVR:CY1PR12MB0667; BCL:0; PCL:0; RULEID:; SRVR:CY1PR12MB0667; X-Microsoft-Exchange-Diagnostics: 1; CY1PR12MB0667; 4:UHZUmt7XFWoaugQMLnBLnCcirZBXEpqOfBu6uxSAxwsD8qCIknjtm5c936UjicZ7jmMT9MZ3vP7+iSEb1XgO/RANE6/yrJVwrKoa8DQS6VxH7d3NQeJjlhJ65NBS3nbGx5MuEgsvpadsaDHKURZmExyc2L4Na9+lRtzdbs09WIrYRYgBY8TdEeYggMtihU/V3afK4n48/kpLEVcUmWx03JOzGEdSVvmHjjPBIJk9imhmqOu8oTV66I+8yfCtskX2tN6rw/NOR6Rq+k77TUijOJzEgxnFmuIULPpITe0McfDfoyZ+HfSJ+68X4xuDZoNq8QXa7vTP9CAtB35P1+PRttRNwLUpHFoCqCiEY67PY4nO4FFHnfjtKUDPYBP5qei7JBAn+j//tOMphsJveWGFnqJweWmEU8MvkOeVXfd3KT5hnljulxl6FdlaeryTOgFK X-Forefront-PRVS: 00429279BA X-Forefront-Antispam-Report: SFV:NSPM; SFS:(10009020)(4630300001)(6049001)(6009001)(7916002)(189002)(199003)(2201001)(19580395003)(230700001)(33716001)(23676002)(66066001)(19580405001)(107886002)(189998001)(83506001)(97736004)(86362001)(229853001)(5001770100001)(77096005)(106356001)(4001350100001)(42186005)(5660300001)(7846002)(305945005)(7736002)(586003)(9686002)(68736007)(33646002)(50466002)(105586002)(81166006)(81156014)(8676002)(47776003)(2906002)(101416001)(6116002)(7406005)(3846002)(7416002)(103116003)(2950100001)(50986999)(76176999)(54356999)(92566002)(921003)(83996005)(217873001)(2101003)(1121003); DIR:OUT; SFP:1101; SCL:1; SRVR:CY1PR12MB0667; H:[127.0.1.1]; FPR:; SPF:None; PTR:InfoNoRecords; MX:1; A:1; LANG:en; Received-SPF: None (protection.outlook.com: amd.com does not designate permitted sender hosts) X-Microsoft-Exchange-Diagnostics: =?utf-8?B?MTtDWTFQUjEyTUIwNjY3OzIzOndjVG41UksxMlA1YnZ0SWd1cEozaWZES3NS?= =?utf-8?B?czJEcmxmVnR1REtYT1ZjUWxwYVBMdXpCemh4dmZYSU5uNFBBWW9sckRDNEI3?= =?utf-8?B?dm9Zd2pISUxScVFYN3BsSUtqTm1jdEpDY2IzQWhMZk5ycnpQRVhudkljczc4?= =?utf-8?B?b2Q0UEhPR3g1T3BjM0JLYU0rL3FpUjYxbVBQa0lNWHdTYWsrbzMwUlpCNFQ4?= =?utf-8?B?VllhMEJpSG51Yk9GbXc4T3haaHZQeVJKYVl1b0NXTDc2T0hyTDhhRXBxM1FJ?= =?utf-8?B?eXJHalh3TUhLckUranJXazVIU2lVM1hCOGxXTC9qV3M1OXd0QnZlYUdyR0Js?= =?utf-8?B?WnI5a2RwMlhVdE9ZeGZnSDd6VHdFUkxaUVVVby9YS2pPeWRmeDBQWUZmOFlr?= =?utf-8?B?NGNaTTBUMHBLejgwSmdsZC9UWGtYbnluUDQyQ1M2dlY0dUkxNTJ6NGsvZ0lF?= =?utf-8?B?Rmorcjl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; 24:Ncf13hTnfyk3kGu5ZHu4GBVjef+ZsLrT7YbpCZ/BHgW07ByX3+cqde2rMOBYnOF6XAor7HGGfDbDQII+SK4oj4PuVHopuv072eJ/xT1m1s8=; 7:BDt3sCfQIRY5Sfstz2geBApx8B2qxhh3e074VWtUUOf5icHcHE8TyebQktRR5o1NVd2qei+88mfwoG6c9j88QSjWujvIUIbq7ReEon8N0Z6XKNY74S36XFic1EESlCMzi+tn+2vA+yyR7sLE2Kae6lahVI3MyqigfSCrLP4Ymfpq/5EoqJehr75Azb5mNA6LWwgwKUCK4aCcOk8aFvI5YonHGTjGHo5gwIrfvQkN9ct7UvK+wfw58Pltcf2PW0eZ SpamDiagnosticOutput: 1:99 SpamDiagnosticMetadata: NSPM X-Microsoft-Exchange-Diagnostics: 1; CY1PR12MB0667; 20:U96DXnh+V7SdMB9wRiQRP2U8Py1eddCz9tJ0cwz3zDfrhoWuOjXiy6kFA7byoukZYVhISxfjkp0aZDkUMfFR/8kljQkZ/TCUsepZzZGDGoRa4axUopDr122pkQeGtnMZ4VJkppciUs6j2QIng6xp6Vrz1Ng2fEADEtSK9/EEttjJCkije98N+F6ut6gcBk8HH41H2DADxPZxiL9hvZ6N3pFf3b2Fcu3TZPoEuTZ0705ru9EOwGZiAFxeqQ7yLN1b X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 22 Aug 2016 23:28:06.3845 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-Transport-CrossTenantHeadersStamped: CY1PR12MB0667 Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP The patch adds initial support required for Secure Encrypted Virtualization (SEV) guest management API's. ASID management: - Reserve asid range for SEV guest, SEV asid range is obtained through CPUID Fn8000_001f[ECX]. A non-SEV guest can use any asid outside the SEV asid range. - SEV guest must have asid value within asid range obtained through CPUID. - SEV guest must have the same asid for all vcpu's. A TLB flush is required if different vcpu for the same ASID is to be run on the same host CPU. - save SEV private structure in kvm_arch. - If SEV is available then initialize PSP firmware during hardware probe Signed-off-by: Brijesh Singh --- arch/x86/include/asm/kvm_host.h | 9 ++ arch/x86/kvm/svm.c | 213 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 221 insertions(+), 1 deletion(-) -- To unsubscribe from this list: send the line "unsubscribe kvm" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h index b1dd673..9b885fc 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -715,6 +715,12 @@ struct kvm_hv { u64 hv_crash_ctl; }; +struct kvm_sev_info { + unsigned int asid; /* asid for this guest */ + unsigned int handle; /* firmware handle */ + unsigned int ref_count; /* number of active vcpus */ +}; + struct kvm_arch { unsigned int n_used_mmu_pages; unsigned int n_requested_mmu_pages; @@ -799,6 +805,9 @@ struct kvm_arch { bool x2apic_format; bool x2apic_broadcast_quirk_disabled; + + /* struct for SEV guest */ + struct kvm_sev_info sev_info; }; struct kvm_vm_stat { diff --git a/arch/x86/kvm/svm.c b/arch/x86/kvm/svm.c index f010b23..dcee635 100644 --- a/arch/x86/kvm/svm.c +++ b/arch/x86/kvm/svm.c @@ -34,6 +34,7 @@ #include #include #include +#include #include #include @@ -186,6 +187,9 @@ struct vcpu_svm { struct page *avic_backing_page; u64 *avic_physical_id_cache; bool avic_is_running; + + /* which host cpu was used for running this vcpu */ + bool last_cpuid; }; #define AVIC_LOGICAL_ID_ENTRY_GUEST_PHYSICAL_ID_MASK (0xFF) @@ -243,6 +247,25 @@ static int avic; module_param(avic, int, S_IRUGO); #endif +/* Secure Encrypted Virtualization */ +static bool sev_enabled; +static unsigned long max_sev_asid; +static unsigned long *sev_asid_bitmap; + +#define kvm_sev_guest() (kvm->arch.sev_info.handle) +#define kvm_sev_handle() (kvm->arch.sev_info.handle) +#define kvm_sev_ref() (kvm->arch.sev_info.ref_count++) +#define kvm_sev_unref() (kvm->arch.sev_info.ref_count--) +#define svm_sev_handle() (svm->vcpu.kvm->arch.sev_info.handle) +#define svm_sev_asid() (svm->vcpu.kvm->arch.sev_info.asid) +#define svm_sev_ref() (svm->vcpu.kvm->arch.sev_info.ref_count++) +#define svm_sev_unref() (svm->vcpu.kvm->arch.sev_info.ref_count--) +#define svm_sev_guest() (svm->vcpu.kvm->arch.sev_info.handle) +#define svm_sev_ref_count() (svm->vcpu.kvm->arch.sev_info.ref_count) + +static int sev_asid_new(void); +static void sev_asid_free(int asid); + static void svm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0); static void svm_flush_tlb(struct kvm_vcpu *vcpu); static void svm_complete_interrupts(struct vcpu_svm *svm); @@ -474,6 +497,8 @@ struct svm_cpu_data { struct kvm_ldttss_desc *tss_desc; struct page *save_area; + + void **sev_vmcb; /* index = sev_asid, value = vmcb pointer */ }; static DEFINE_PER_CPU(struct svm_cpu_data *, svm_data); @@ -727,7 +752,10 @@ static int svm_hardware_enable(void) sd->asid_generation = 1; sd->max_asid = cpuid_ebx(SVM_CPUID_FUNC) - 1; sd->next_asid = sd->max_asid + 1; - sd->min_asid = 1; + sd->min_asid = max_sev_asid + 1; + + if (sev_enabled) + memset(sd->sev_vmcb, 0, (max_sev_asid + 1) * sizeof(void *)); native_store_gdt(&gdt_descr); gdt = (struct desc_struct *)gdt_descr.address; @@ -788,6 +816,7 @@ static void svm_cpu_uninit(int cpu) per_cpu(svm_data, raw_smp_processor_id()) = NULL; __free_page(sd->save_area); + kfree(sd->sev_vmcb); kfree(sd); } @@ -805,6 +834,14 @@ static int svm_cpu_init(int cpu) if (!sd->save_area) goto err_1; + if (sev_enabled) { + sd->sev_vmcb = kmalloc((max_sev_asid + 1) * sizeof(void *), + GFP_KERNEL); + r = -ENOMEM; + if (!sd->sev_vmcb) + goto err_1; + } + per_cpu(svm_data, cpu) = sd; return 0; @@ -931,6 +968,74 @@ static void svm_disable_lbrv(struct vcpu_svm *svm) set_msr_interception(msrpm, MSR_IA32_LASTINTTOIP, 0, 0); } +static __init void sev_hardware_setup(void) +{ + int ret, psp_ret; + struct psp_data_init *init; + struct psp_data_status *status; + + /* + * Check SEV Feature Support: Fn8001_001F[EAX] + * Bit 1: Secure Memory Virtualization supported + */ + if (!(cpuid_eax(0x8000001F) & 0x2)) + return; + + /* + * Get maximum number of encrypted guest supported: Fn8001_001F[ECX] + * Bit 31:0: Number of supported guest + */ + max_sev_asid = cpuid_ecx(0x8000001F); + if (!max_sev_asid) + return; + + init = kzalloc(sizeof(*init), GFP_KERNEL); + if (!init) + return; + + status = kzalloc(sizeof(*status), GFP_KERNEL); + if (!status) + goto err_1; + + /* Initialize PSP firmware */ + init->hdr.buffer_len = sizeof(*init); + init->flags = 0; + ret = psp_platform_init(init, &psp_ret); + if (ret) { + printk(KERN_ERR "SEV: PSP_INIT ret=%d (%#x)\n", ret, psp_ret); + goto err_2; + } + + /* Initialize SEV ASID bitmap */ + sev_asid_bitmap = kmalloc(max(sizeof(unsigned long), + max_sev_asid/8 + 1), GFP_KERNEL); + if (IS_ERR(sev_asid_bitmap)) { + psp_platform_shutdown(&psp_ret); + goto err_2; + } + bitmap_zero(sev_asid_bitmap, max_sev_asid); + set_bit(0, sev_asid_bitmap); /* mark ASID 0 as used */ + + sev_enabled = 1; + printk(KERN_INFO "kvm: SEV enabled\n"); + + /* Query the platform status and print API version */ + status->hdr.buffer_len = sizeof(*status); + ret = psp_platform_status(status, &psp_ret); + if (ret) { + printk(KERN_ERR "SEV: PLATFORM_STATUS ret=%#x\n", psp_ret); + goto err_2; + } + + printk(KERN_INFO "SEV API: %d.%d\n", + status->api_major, status->api_minor); +err_2: + kfree(status); +err_1: + kfree(init); + return; +} + static __init int svm_hardware_setup(void) { int cpu; @@ -966,6 +1071,8 @@ static __init int svm_hardware_setup(void) kvm_enable_efer_bits(EFER_SVME | EFER_LMSLE); } + sev_hardware_setup(); + for_each_possible_cpu(cpu) { r = svm_cpu_init(cpu); if (r) @@ -1003,10 +1110,25 @@ err: return r; } +static __exit void sev_hardware_unsetup(void) +{ + int ret, psp_ret; + + ret = psp_platform_shutdown(&psp_ret); + if (ret) + printk(KERN_ERR "failed to shutdown PSP rc=%d (%#0x10x)\n", + ret, psp_ret); + + kfree(sev_asid_bitmap); +} + static __exit void svm_hardware_unsetup(void) { int cpu; + if (sev_enabled) + sev_hardware_unsetup(); + for_each_possible_cpu(cpu) svm_cpu_uninit(cpu); @@ -1088,6 +1210,11 @@ static void avic_init_vmcb(struct vcpu_svm *svm) svm->vcpu.arch.apicv_active = true; } +static void sev_init_vmcb(struct vcpu_svm *svm) +{ + svm->vmcb->control.nested_ctl |= SVM_NESTED_CTL_SEV_ENABLE; +} + static void init_vmcb(struct vcpu_svm *svm) { struct vmcb_control_area *control = &svm->vmcb->control; @@ -1202,6 +1329,10 @@ static void init_vmcb(struct vcpu_svm *svm) if (avic) avic_init_vmcb(svm); + if (svm_sev_guest()) + sev_init_vmcb(svm); + + mark_all_dirty(svm->vmcb); enable_gif(svm); @@ -1413,6 +1544,14 @@ static void svm_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event) avic_update_vapic_bar(svm, APIC_DEFAULT_PHYS_BASE); } +static void sev_init_vcpu(struct vcpu_svm *svm) +{ + if (!svm_sev_guest()) + return; + + svm_sev_ref(); +} + static struct kvm_vcpu *svm_create_vcpu(struct kvm *kvm, unsigned int id) { struct vcpu_svm *svm; @@ -1475,6 +1614,7 @@ static struct kvm_vcpu *svm_create_vcpu(struct kvm *kvm, unsigned int id) init_vmcb(svm); svm_init_osvw(&svm->vcpu); + sev_init_vcpu(svm); return &svm->vcpu; @@ -1494,6 +1634,23 @@ out: return ERR_PTR(err); } +static void sev_uninit_vcpu(struct vcpu_svm *svm) +{ + int cpu; + int asid = svm_sev_asid(); + struct svm_cpu_data *sd; + + if (!svm_sev_guest()) + return; + + svm_sev_unref(); + + for_each_possible_cpu(cpu) { + sd = per_cpu(svm_data, cpu); + sd->sev_vmcb[asid] = NULL; + } +} + static void svm_free_vcpu(struct kvm_vcpu *vcpu) { struct vcpu_svm *svm = to_svm(vcpu); @@ -1502,6 +1659,7 @@ static void svm_free_vcpu(struct kvm_vcpu *vcpu) __free_pages(virt_to_page(svm->msrpm), MSRPM_ALLOC_ORDER); __free_page(virt_to_page(svm->nested.hsave)); __free_pages(virt_to_page(svm->nested.msrpm), MSRPM_ALLOC_ORDER); + sev_uninit_vcpu(svm); kvm_vcpu_uninit(vcpu); kmem_cache_free(kvm_vcpu_cache, svm); } @@ -1945,6 +2103,11 @@ static int pf_interception(struct vcpu_svm *svm) default: error_code = svm->vmcb->control.exit_info_1; + /* In SEV mode, the guest physical address will have C-bit + * set. C-bit must be cleared before handling the fault. + */ + if (svm_sev_guest()) + fault_address &= ~sme_me_mask; trace_kvm_page_fault(fault_address, error_code); if (!npt_enabled && kvm_event_needs_reinjection(&svm->vcpu)) kvm_mmu_unprotect_page_virt(&svm->vcpu, fault_address); @@ -4131,12 +4294,40 @@ static void reload_tss(struct kvm_vcpu *vcpu) load_TR_desc(); } +static void pre_sev_run(struct vcpu_svm *svm) +{ + int asid = svm_sev_asid(); + int cpu = raw_smp_processor_id(); + struct svm_cpu_data *sd = per_cpu(svm_data, cpu); + + /* Assign the asid allocated for this SEV guest */ + svm->vmcb->control.asid = svm_sev_asid(); + + /* Flush guest TLB: + * - when different VMCB for the same ASID is to be run on the + * same host CPU + * or + * - this VMCB was executed on different host cpu in previous VMRUNs. + */ + if (sd->sev_vmcb[asid] != (void *)svm->vmcb || + svm->last_cpuid != cpu) + svm->vmcb->control.tlb_ctl = TLB_CONTROL_FLUSH_ALL_ASID; + + svm->last_cpuid = cpu; + sd->sev_vmcb[asid] = (void *)svm->vmcb; + + mark_dirty(svm->vmcb, VMCB_ASID); +} + static void pre_svm_run(struct vcpu_svm *svm) { int cpu = raw_smp_processor_id(); struct svm_cpu_data *sd = per_cpu(svm_data, cpu); + if (svm_sev_guest()) + return pre_sev_run(svm); + /* FIXME: handle wraparound of asid_generation */ if (svm->asid_generation != sd->asid_generation) new_asid(svm, sd); @@ -4985,6 +5176,26 @@ static inline void avic_post_state_restore(struct kvm_vcpu *vcpu) avic_handle_ldr_update(vcpu); } +static int sev_asid_new(void) +{ + int pos; + + if (!sev_enabled) + return -ENOTTY; + + pos = find_first_zero_bit(sev_asid_bitmap, max_sev_asid); + if (pos >= max_sev_asid) + return -EBUSY; + + set_bit(pos, sev_asid_bitmap); + return pos; +} + +static void sev_asid_free(int asid) +{ + clear_bit(asid, sev_asid_bitmap); +} + static struct kvm_x86_ops svm_x86_ops __ro_after_init = { .cpu_has_kvm_support = has_svm, .disabled_by_bios = is_disabled,