From patchwork Fri Mar 18 06:09:42 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Suthikulpanit, Suravee" X-Patchwork-Id: 64017 Delivered-To: patch@linaro.org Received: by 10.112.199.169 with SMTP id jl9csp869684lbc; Thu, 17 Mar 2016 23:11:48 -0700 (PDT) X-Received: by 10.98.74.200 with SMTP id c69mr20758640pfj.129.1458281508263; Thu, 17 Mar 2016 23:11:48 -0700 (PDT) Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id r6si6844178pap.212.2016.03.17.23.11.47; Thu, 17 Mar 2016 23:11:48 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dkim=pass header.i=@amdcloud.onmicrosoft.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1756879AbcCRGLc (ORCPT + 30 others); Fri, 18 Mar 2016 02:11:32 -0400 Received: from mail-bn1on0058.outbound.protection.outlook.com ([157.56.110.58]:33350 "EHLO na01-bn1-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1752236AbcCRGLP (ORCPT ); Fri, 18 Mar 2016 02:11:15 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amdcloud.onmicrosoft.com; s=selector1-amd-com; h=From:To:Date:Subject:Message-ID:Content-Type:MIME-Version; bh=Zzut8/0R9ANGQi3V7NUS2fxPnyl4+pGicJOINBDTfVk=; b=XH4BxXDp/wHpXnh33XIcUxtM0pPv26BzgEVD+T0iMp0zkHf+KM2UiLVYBGU5OOJ3CTnVERK3yUNv8K+JEsusoBQJaAv8d0912gGeekFktsSITfmbKPf9lG+eOBnbx5ggphjhElHKJtUWrzLftna1ET9L2yoo37LBPnzTQ+Px+v0= Authentication-Results: redhat.com; dkim=none (message not signed) header.d=none; redhat.com; dmarc=none action=none header.from=amd.com; Received: from localhost.localdomain (124.121.8.20) by BY1PR12MB0437.namprd12.prod.outlook.com (10.162.147.139) with Microsoft SMTP Server (TLS) id 15.1.443.12; Fri, 18 Mar 2016 06:11:03 +0000 From: Suravee Suthikulpanit To: , , , , , CC: , , , , Suravee Suthikulpanit , Suravee Suthikulpanit Subject: [PART1 RFC v3 06/12] KVM: x86: Detect and Initialize AVIC support Date: Fri, 18 Mar 2016 01:09:42 -0500 Message-ID: <1458281388-14452-7-git-send-email-Suravee.Suthikulpanit@amd.com> X-Mailer: git-send-email 1.9.1 In-Reply-To: <1458281388-14452-1-git-send-email-Suravee.Suthikulpanit@amd.com> References: <1458281388-14452-1-git-send-email-Suravee.Suthikulpanit@amd.com> MIME-Version: 1.0 X-Originating-IP: [124.121.8.20] X-ClientProxiedBy: SG2PR04CA0047.apcprd04.prod.outlook.com (25.169.49.143) To BY1PR12MB0437.namprd12.prod.outlook.com (25.162.147.139) X-MS-Office365-Filtering-Correlation-Id: dee09f0c-118c-47e2-1e62-08d34ef41aa1 X-Microsoft-Exchange-Diagnostics: 1; BY1PR12MB0437; 2:4HhxtaoXOUf5Ob1aAtAp0CiFxVhPP7UIZTFkSkkisStkt3VZJIhO9qe/HBeWrbDfQdX50uPCVZWpgD41lH8ixA3Kt77sG7SLiFUTBDRsMkE8c4G0zzPu+qoDHcPB6wIQcI+1P7EgMiFnHzTLClnp8AvXLInXKVv+Ed5fj/+Z0CMdWWEStq1yd28EaCwt8AP6; 3:2bwGOkVFLhTwh5c7XXzHDheVUj1/+fDlvEgRAb6FNAQHYSK5BI+8Z2pMKg3Wx7R8QmBTQ3uu8QRnr2FPqsdrPD0kMQC2Aav3LTsqP/K2pkwf4VpHO32GZA+EQXlDksoF; 25:yUqAZ2kk8QbZh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JQwVwc1hsUaSY0lSGgU0W9dYkvi93iC6d8ePPBXrz3y3AqxfeMeMia7; 4:ZO44PHJ43O848eDvaZqQ40bDcrmUoil8RNmqHhX/fFX1FvB5qxzIm54+65zO3fOQD/+qwezQ6XkHZF0ktl2tINUfM74kU+wu2DZDnsErzJoJ11VbrKjzXlOBbGWKS0iV3NwMB6ZCsqkAwcZPfjX374S/px7H3TMvpgP1kdwIR8M8PKSZ6ksG5JbyDNhnlcgjDSI9MhOtdH6AVuj093PHT7vEteKYx8zC/ORq//untFl/TzbL8Q9cz5MYXfJofKVshtJlB9S/PraYdFZltgi5VbADpFd6tgFS5BNrp6LY8YI5gI9VdRdKIyGOam7xufXM1OLYEV+Fkn7q8vTfcX/D8nzGpP4aYUGqz/criPoCA+7CIMHgel1dQgKKmZJBIUw9 X-Microsoft-Antispam-PRVS: X-Exchange-Antispam-Report-Test: UriScan:; X-Exchange-Antispam-Report-CFA-Test: BCL:0; PCL:0; RULEID:(601004)(2401047)(8121501046)(5005006)(10201501046)(3002001); SRVR:BY1PR12MB0437; BCL:0; PCL:0; RULEID:; SRVR:BY1PR12MB0437; X-Forefront-PRVS: 088552DE73 X-Forefront-Antispam-Report: SFV:NSPM; SFS:(10009020)(4630300001)(6009001)(6069001)(189998001)(66066001)(19580395003)(19580405001)(47776003)(42186005)(3846002)(5001770100001)(50226001)(6116002)(2201001)(86362001)(586003)(229853001)(2950100001)(36756003)(5004730100002)(77096005)(81166005)(5003940100001)(4326007)(50466002)(92566002)(48376002)(5008740100001)(76176999)(50986999); DIR:OUT; SFP:1101; SCL:1; SRVR:BY1PR12MB0437; H:localhost.localdomain; FPR:; SPF:None; MLV:sfv; LANG:en; X-Microsoft-Exchange-Diagnostics: 1; BY1PR12MB0437; 23:eYID8eKwezZcJDIklu65gKSR1htkJP+2vXOODShRhXz3tLzCDi696o1OwEneXz0G/868Pr6LCefvLIgzOFc6hHJpZtZP9cT1mT6uYfCMMkJpv5IL/0zDSsWxcrmFiaDWvSo7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4c6oM1PH0/j9ZLlKC493aoM/kEdlkgmBvMFnFo68AuGOjIn+0OosQ9ngtlMZG33NB6oIbA8N8mNzrwkEiM/s87UUiCqFsEsRs0/J6clRJWDmHZCOM0xTPtWr2RZGcrjVeZv+Zw3FVRL0HpgjJqgSK49XkIxMKOKmu8bP7AiBwowTDsX60J/KIr0FF90JylaWZqiXrOTM58PfPwGP/ISJI0zMS+7MJn8SNlg X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 18 Mar 2016 06:11:03.3091 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-Transport-CrossTenantHeadersStamped: BY1PR12MB0437 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org This patch introduces AVIC-related data structure, and AVIC initialization code. There are three main data structures for AVIC: * Virtual APIC (vAPIC) backing page (per-VCPU) * Physical APIC ID table (per-VM) * Logical APIC ID table (per-VM) Currently, AVIC is disabled by default. Users can manually enable AVIC via kernel boot option kvm-amd.avic=1 or during kvm-amd module loading with parameter avic=1. Signed-off-by: Suravee Suthikulpanit --- arch/x86/include/asm/kvm_host.h | 8 ++ arch/x86/include/asm/svm.h | 3 + arch/x86/kvm/svm.c | 232 +++++++++++++++++++++++++++++++++++++++- 3 files changed, 242 insertions(+), 1 deletion(-) -- 1.9.1 diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h index 87eac2a..2698e40 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -754,6 +754,14 @@ struct kvm_arch { bool irqchip_split; u8 nr_reserved_ioapic_pins; + + /* Struct members for AVIC */ + struct hlist_node hnode; + struct kvm *kvm; + u32 ldr_mode; + u32 avic_tag; + struct page *avic_log_apic_id_table_page; + struct page *avic_phy_apic_id_table_page; }; struct kvm_vm_stat { diff --git a/arch/x86/include/asm/svm.h b/arch/x86/include/asm/svm.h index 66e26a0..287e635 100644 --- a/arch/x86/include/asm/svm.h +++ b/arch/x86/include/asm/svm.h @@ -116,6 +116,9 @@ struct __attribute__ ((__packed__)) vmcb_control_area { #define V_INTR_MASKING_SHIFT 24 #define V_INTR_MASKING_MASK (1 << V_INTR_MASKING_SHIFT) +#define AVIC_ENABLE_SHIFT 31 +#define AVIC_ENABLE_MASK (1 << AVIC_ENABLE_SHIFT) + #define SVM_INTERRUPT_SHADOW_MASK 1 #define SVM_IOIO_STR_SHIFT 2 diff --git a/arch/x86/kvm/svm.c b/arch/x86/kvm/svm.c index c13a64b..621c948 100644 --- a/arch/x86/kvm/svm.c +++ b/arch/x86/kvm/svm.c @@ -14,6 +14,9 @@ * the COPYING file in the top-level directory. * */ + +#define pr_fmt(fmt) "SVM: " fmt + #include #include "irq.h" @@ -78,6 +81,11 @@ MODULE_DEVICE_TABLE(x86cpu, svm_cpu_id); #define TSC_RATIO_MIN 0x0000000000000001ULL #define TSC_RATIO_MAX 0x000000ffffffffffULL +#define AVIC_HPA_MASK ~((0xFFFULL << 52) || 0xFFF) + +/* NOTE: Current max index allowed for physical APIC ID table is 255 */ +#define AVIC_PHY_APIC_ID_MAX 0xFF + static bool erratum_383_found __read_mostly; static const u32 host_save_user_msrs[] = { @@ -162,8 +170,20 @@ struct vcpu_svm { /* cached guest cpuid flags for faster access */ bool nrips_enabled : 1; + + struct page *avic_backing_page; + u64 *avic_phy_apic_id_cache; + bool avic_was_running; }; +#define AVIC_LOG_APIC_ID__GUEST_PHY_APIC_ID_MSK (0xFF) +#define AVIC_LOG_APIC_ID__VALID_MSK (1 << 31) + +#define AVIC_PHY_APIC_ID__HOST_PHY_APIC_ID_MSK (0xFFULL) +#define AVIC_PHY_APIC_ID__BACKING_PAGE_MSK (0xFFFFFFFFFFULL << 12) +#define AVIC_PHY_APIC_ID__IS_RUN_MSK (1ULL << 62) +#define AVIC_PHY_APIC_ID__VALID_MSK (1ULL << 63) + static DEFINE_PER_CPU(u64, current_tsc_ratio); #define TSC_RATIO_DEFAULT 0x0100000000ULL @@ -205,6 +225,10 @@ module_param(npt, int, S_IRUGO); static int nested = true; module_param(nested, int, S_IRUGO); +/* enable / disable AVIC */ +static int avic; +module_param(avic, int, S_IRUGO); + 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); @@ -234,6 +258,18 @@ enum { /* TPR and CR2 are always written before VMRUN */ #define VMCB_ALWAYS_DIRTY_MASK ((1U << VMCB_INTR) | (1U << VMCB_CR2)) +#define VMCB_AVIC_APIC_BAR_MASK 0xFFFFFFFFFF000ULL + +static inline bool svm_vcpu_avic_enabled(struct vcpu_svm *svm) +{ + return (avic && (svm->vmcb->control.int_ctl & AVIC_ENABLE_MASK)); +} + +static inline void avic_update_vapic_bar(struct vcpu_svm *svm, u64 data) +{ + svm->vmcb->control.avic_vapic_bar = data & VMCB_AVIC_APIC_BAR_MASK; +} + static inline void mark_all_dirty(struct vmcb *vmcb) { vmcb->control.clean = 0; @@ -923,6 +959,13 @@ static __init int svm_hardware_setup(void) } else kvm_disable_tdp(); + if (avic && (!npt_enabled || !boot_cpu_has(X86_FEATURE_AVIC))) + avic = false; + + if (avic) { + printk(KERN_INFO "kvm: AVIC enabled\n"); + } + return 0; err: @@ -1000,6 +1043,24 @@ static void svm_adjust_tsc_offset_guest(struct kvm_vcpu *vcpu, s64 adjustment) mark_dirty(svm->vmcb, VMCB_INTERCEPTS); } +static void avic_init_vmcb(struct vcpu_svm *svm) +{ + struct vmcb *vmcb = svm->vmcb; + struct kvm_arch *vm_data = &svm->vcpu.kvm->arch; + phys_addr_t bpa = page_to_phys(svm->avic_backing_page); + phys_addr_t lpa = page_to_phys(vm_data->avic_log_apic_id_table_page); + phys_addr_t ppa = page_to_phys(vm_data->avic_phy_apic_id_table_page); + + if (!vmcb) + return; + + vmcb->control.avic_backing_page = bpa & AVIC_HPA_MASK; + vmcb->control.avic_log_apic_id = lpa & AVIC_HPA_MASK; + vmcb->control.avic_phy_apic_id = ppa & AVIC_HPA_MASK; + vmcb->control.avic_phy_apic_id |= AVIC_PHY_APIC_ID_MAX; + vmcb->control.int_ctl |= AVIC_ENABLE_MASK; +} + static void init_vmcb(struct vcpu_svm *svm) { struct vmcb_control_area *control = &svm->vmcb->control; @@ -1113,6 +1174,139 @@ static void init_vmcb(struct vcpu_svm *svm) mark_all_dirty(svm->vmcb); enable_gif(svm); + + if (avic) + avic_init_vmcb(svm); +} + +static u64 *avic_get_phy_apic_id_entry(struct kvm_vcpu *vcpu, int index) +{ + u64 *avic_phy_ait; + struct kvm_arch *vm_data = &vcpu->kvm->arch; + + /* Note: APIC ID = 0xff is used for broadcast. + * APIC ID > 0xff is reserved. + */ + if (index >= 0xff) + return NULL; + + avic_phy_ait = page_address(vm_data->avic_phy_apic_id_table_page); + + return &avic_phy_ait[index]; +} + +static int avic_init_backing_page(struct kvm_vcpu *vcpu) +{ + u64 *entry, new_entry; + int id = vcpu->vcpu_id; + struct vcpu_svm *svm = to_svm(vcpu); + + if (id >= AVIC_PHY_APIC_ID_MAX) + return -EINVAL; + + if (!svm->vcpu.arch.apic->regs) + return -EINVAL; + + svm->avic_backing_page = virt_to_page(svm->vcpu.arch.apic->regs); + + avic_init_vmcb(svm); + + /* Setting AVIC backing page address in the phy APIC ID table */ + entry = avic_get_phy_apic_id_entry(vcpu, id); + if (!entry) + return -EINVAL; + + new_entry = READ_ONCE(*entry); + new_entry = (page_to_phys(svm->avic_backing_page) & + AVIC_PHY_APIC_ID__BACKING_PAGE_MSK) | + AVIC_PHY_APIC_ID__VALID_MSK; + WRITE_ONCE(*entry, new_entry); + + svm->avic_phy_apic_id_cache = entry; + + return 0; +} + +static void avic_vm_uninit(struct kvm *kvm) +{ + unsigned long flags; + struct kvm_arch *vm_data = &kvm->arch; + + if (vm_data->avic_log_apic_id_table_page) + __free_page(vm_data->avic_log_apic_id_table_page); + if (vm_data->avic_phy_apic_id_table_page) + __free_page(vm_data->avic_phy_apic_id_table_page); +} + +static void avic_vcpu_uninit(struct kvm_vcpu *vcpu) +{ + struct vcpu_svm *svm = to_svm(vcpu); + + if (!avic) + return; + + if (svm->avic_phy_apic_id_cache) + svm->avic_phy_apic_id_cache = NULL; +} + +static atomic_t avic_tag_gen = ATOMIC_INIT(1); + +static inline u32 avic_get_next_tag(void) +{ + u32 tag = atomic_read(&avic_tag_gen); + + atomic_inc(&avic_tag_gen); + return tag; +} + +static int avic_vm_init(struct kvm *kvm) +{ + int err = -ENOMEM; + struct kvm_arch *vm_data = &kvm->arch; + struct page *p_page; + struct page *l_page; + + /** + * Note: + * AVIC hardware walks the nested page table to check permissions, + * but does not use the SPA address specified in the leaf page + * table entry since it uses address in the AVIC_BACKING_PAGE pointer + * field of the VMCB. Therefore, we set up the + * APIC_ACCESS_PAGE_PRIVATE_MEMSLOT (4KB) here. + */ + mutex_lock(&kvm->slots_lock); + err = __x86_set_memory_region(kvm, + APIC_ACCESS_PAGE_PRIVATE_MEMSLOT, + APIC_DEFAULT_PHYS_BASE, + PAGE_SIZE); + mutex_unlock(&kvm->slots_lock); + if (err) + goto free_avic; + kvm->arch.apic_access_page_done = true; + + /* Allocating physical APIC ID table (4KB) */ + p_page = alloc_page(GFP_KERNEL); + if (!p_page) + goto free_avic; + + vm_data->avic_phy_apic_id_table_page = p_page; + clear_page(page_address(p_page)); + + /* Allocating logical APIC ID table (4KB) */ + l_page = alloc_page(GFP_KERNEL); + if (!l_page) + goto free_avic; + + vm_data->avic_log_apic_id_table_page = l_page; + clear_page(page_address(l_page)); + + vm_data->avic_tag = avic_get_next_tag(); + + return 0; + +free_avic: + avic_vm_uninit(kvm); + return err; } static void svm_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event) @@ -1131,6 +1325,9 @@ static void svm_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event) kvm_cpuid(vcpu, &eax, &dummy, &dummy, &dummy); kvm_register_write(vcpu, VCPU_REGS_RDX, eax); + + if (svm_vcpu_avic_enabled(svm) && !init_event) + avic_update_vapic_bar(svm, APIC_DEFAULT_PHYS_BASE); } static struct kvm_vcpu *svm_create_vcpu(struct kvm *kvm, unsigned int id) @@ -1169,6 +1366,14 @@ static struct kvm_vcpu *svm_create_vcpu(struct kvm *kvm, unsigned int id) if (!hsave_page) goto free_page3; + if (avic) { + err = avic_init_backing_page(&svm->vcpu); + if (err) { + avic_vcpu_uninit(&svm->vcpu); + goto free_page4; + } + } + svm->nested.hsave = page_address(hsave_page); svm->msrpm = page_address(msrpm_pages); @@ -1187,6 +1392,8 @@ static struct kvm_vcpu *svm_create_vcpu(struct kvm *kvm, unsigned int id) return &svm->vcpu; +free_page4: + __free_page(hsave_page); free_page3: __free_pages(nested_msrpm_pages, MSRPM_ALLOC_ORDER); free_page2: @@ -1209,6 +1416,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); + avic_vcpu_uninit(vcpu); kvm_vcpu_uninit(vcpu); kmem_cache_free(kvm_vcpu_cache, svm); } @@ -3371,6 +3579,7 @@ static void dump_vmcb(struct kvm_vcpu *vcpu) pr_err("%-20s%08x\n", "exit_int_info_err:", control->exit_int_info_err); pr_err("%-20s%lld\n", "nested_ctl:", control->nested_ctl); pr_err("%-20s%016llx\n", "nested_cr3:", control->nested_cr3); + pr_err("%-20s%016llx\n", "avic_vapic_bar:", control->avic_vapic_bar); pr_err("%-20s%08x\n", "event_inj:", control->event_inj); pr_err("%-20s%08x\n", "event_inj_err:", control->event_inj_err); pr_err("%-20s%lld\n", "lbr_ctl:", control->lbr_ctl); @@ -3602,11 +3811,27 @@ static void svm_set_virtual_x2apic_mode(struct kvm_vcpu *vcpu, bool set) static bool svm_get_enable_apicv(void) { - return false; + return avic; } +static void svm_hwapic_irr_update(struct kvm_vcpu *vcpu, int max_irr) +{ +} + +static void svm_hwapic_isr_update(struct kvm *kvm, int isr) +{ +} + +/* Note: Currently only used by Hyper-V. */ static void svm_refresh_apicv_exec_ctrl(struct kvm_vcpu *vcpu) { + struct vcpu_svm *svm = to_svm(vcpu); + struct vmcb *vmcb = svm->vmcb; + + if (!avic) + return; + + vmcb->control.int_ctl &= ~AVIC_ENABLE_MASK; } static void svm_load_eoi_exitmap(struct kvm_vcpu *vcpu, u64 *eoi_exit_bitmap) @@ -4318,6 +4543,9 @@ static struct kvm_x86_ops svm_x86_ops = { .vcpu_free = svm_free_vcpu, .vcpu_reset = svm_vcpu_reset, + .vm_init = avic_vm_init, + .vm_uninit = avic_vm_uninit, + .prepare_guest_switch = svm_prepare_guest_switch, .vcpu_load = svm_vcpu_load, .vcpu_put = svm_vcpu_put, @@ -4375,6 +4603,8 @@ static struct kvm_x86_ops svm_x86_ops = { .refresh_apicv_exec_ctrl = svm_refresh_apicv_exec_ctrl, .load_eoi_exitmap = svm_load_eoi_exitmap, .sync_pir_to_irr = svm_sync_pir_to_irr, + .hwapic_irr_update = svm_hwapic_irr_update, + .hwapic_isr_update = svm_hwapic_isr_update, .set_tss_addr = svm_set_tss_addr, .get_tdp_level = get_npt_level,