From patchwork Mon Jan 9 11:03:42 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Cooper X-Patchwork-Id: 9504381 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 735A760710 for ; Mon, 9 Jan 2017 11:21:52 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 7E4532848D for ; Mon, 9 Jan 2017 11:21:52 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 7332A28494; Mon, 9 Jan 2017 11:21:52 +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=-4.2 required=2.0 tests=BAYES_00, RCVD_IN_DNSWL_MED autolearn=ham version=3.3.1 Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) (using TLSv1.2 with cipher AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 9E0162848D for ; Mon, 9 Jan 2017 11:21:51 +0000 (UTC) Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.84_2) (envelope-from ) id 1cQXzI-0005A9-16; Mon, 09 Jan 2017 11:19:40 +0000 Received: from mail6.bemta6.messagelabs.com ([193.109.254.103]) by lists.xenproject.org with esmtp (Exim 4.84_2) (envelope-from ) id 1cQXzG-00059j-U3 for xen-devel@lists.xen.org; Mon, 09 Jan 2017 11:19:39 +0000 Received: from [193.109.254.147] by server-10.bemta-6.messagelabs.com id C0/3E-13192-AC173785; Mon, 09 Jan 2017 11:19:38 +0000 X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFmphkeJIrShJLcpLzFFi42JxWrohUpexsDj CYP9tI4slHxezODB6HN39mymAMYo1My8pvyKBNePVz9XMBQ11FbeP7GNvYLwT1MXIySEh4C/R uGcVM4jNJqAvsfvFJyYQW0RAXeJ0x0XWLkYODmYBXYlVPzVAwsICMRLrT5xjB7FZBFQkzl2ZC lbOK+Al8bn1CTPESDmJ88d/gtmcQPFpf04wgthCAp4SCx++YIaw1SSu9V9ih+gVlDg58wkLiM 0sICFx8MUL5gmMvLOQpGYhSS1gZFrFqFGcWlSWWqRraKiXVJSZnlGSm5iZo2toYKaXm1pcnJi empOYVKyXnJ+7iREYOgxAsIPx07KAQ4ySHExKoryOUQURQnxJ+SmVGYnFGfFFpTmpxYcYZTg4 lCR4X+UXRwgJFqWmp1akZeYAgxgmLcHBoyTCOxskzVtckJhbnJkOkTrFqCglznsDJCEAksgoz YNrg0XOJUZZKWFeRqBDhHgKUotyM0tQ5V8xinMwKgnzLgKZwpOZVwI3/RXQYiagxZF2YItLEh FSUg2MNZMSl7sdmN33Mvtc+XW7q3enynCW2txZwxbD9UjvkNvmiRleuWeDZGM+vtGbt6Nyzh+ bysxXZycvuFS0rb6y0Efc//KNDQeOx/q1ajeHRpq7nDlsPslpcdNaXldpU2H2J76Nz/YfmLmC SUFivnrH1GzlOrVSm/1SPx5dMBRRrS758dZFs6VfiaU4I9FQi7moOBEA9FkV1pcCAAA= X-Env-Sender: prvs=175ce6123=Andrew.Cooper3@citrix.com X-Msg-Ref: server-13.tower-27.messagelabs.com!1483960575!71451767!2 X-Originating-IP: [66.165.176.89] X-SpamReason: No, hits=0.0 required=7.0 tests=sa_preprocessor: VHJ1c3RlZCBJUDogNjYuMTY1LjE3Ni44OSA9PiAyMDMwMDc=\n, received_headers: No Received headers X-StarScan-Received: X-StarScan-Version: 9.1.1; banners=-,-,- X-VirusChecked: Checked Received: (qmail 19088 invoked from network); 9 Jan 2017 11:16:17 -0000 Received: from smtp.citrix.com (HELO SMTP.CITRIX.COM) (66.165.176.89) by server-13.tower-27.messagelabs.com with RC4-SHA encrypted SMTP; 9 Jan 2017 11:16:17 -0000 X-IronPort-AV: E=Sophos;i="5.33,339,1477958400"; d="scan'208";a="398663282" From: Andrew Cooper To: Xen-devel Date: Mon, 9 Jan 2017 11:03:42 +0000 Message-ID: <1483959822-30484-26-git-send-email-andrew.cooper3@citrix.com> X-Mailer: git-send-email 2.1.4 In-Reply-To: <1483959822-30484-1-git-send-email-andrew.cooper3@citrix.com> References: <1483959822-30484-1-git-send-email-andrew.cooper3@citrix.com> MIME-Version: 1.0 Cc: Andrew Cooper Subject: [Xen-devel] [PATCH v2 25/25] x86/cpuid: Alter the legacy-path prototypes to match guest_cpuid() X-BeenThere: xen-devel@lists.xen.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xen.org Sender: "Xen-devel" X-Virus-Scanned: ClamAV using ClamSMTP This allows the compiler to have a far easier time inlining the legacy paths into guest_cpuid(), and avoids the need to have a full struct cpu_user_regs in the guest_cpuid() stack frame. Signed-off-by: Andrew Cooper Reviewed-by: Jan Beulich --- v2: * Reduce scope of guest_cpu_user_regs(). * Drop unnecessary casts. * Consistently use uint32_t. * use cpuid_count_leaf() rather than opencoding it. --- xen/arch/x86/cpuid.c | 203 +++++++++++++++++++++------------------------------ 1 file changed, 82 insertions(+), 121 deletions(-) diff --git a/xen/arch/x86/cpuid.c b/xen/arch/x86/cpuid.c index 822f109..8482ece 100644 --- a/xen/arch/x86/cpuid.c +++ b/xen/arch/x86/cpuid.c @@ -350,33 +350,29 @@ int init_domain_cpuid_policy(struct domain *d) return 0; } -static void pv_cpuid(struct cpu_user_regs *regs) +static void pv_cpuid(uint32_t leaf, uint32_t subleaf, struct cpuid_leaf *res) { - uint32_t leaf, subleaf, a, b, c, d; struct vcpu *curr = current; struct domain *currd = curr->domain; const struct cpuid_policy *p = currd->arch.cpuid; - leaf = a = regs->_eax; - b = regs->_ebx; - subleaf = c = regs->_ecx; - d = regs->_edx; - if ( !is_control_domain(currd) && !is_hardware_domain(currd) ) - domain_cpuid(currd, leaf, subleaf, &a, &b, &c, &d); + domain_cpuid(currd, leaf, subleaf, &res->a, &res->b, &res->c, &res->d); else - cpuid_count(leaf, subleaf, &a, &b, &c, &d); + cpuid_count_leaf(leaf, subleaf, res); switch ( leaf ) { uint32_t tmp; case 0x00000001: - c = p->basic._1c; - d = p->basic._1d; + res->c = p->basic._1c; + res->d = p->basic._1d; if ( !is_pvh_domain(currd) ) { + const struct cpu_user_regs *regs = guest_cpu_user_regs(); + /* * Delete the PVH condition when HVMLite formally replaces PVH, * and HVM guests no longer enter a PV codepath. @@ -437,7 +433,7 @@ static void pv_cpuid(struct cpu_user_regs *regs) (regs->entry_vector == TRAP_invalid_op && guest_kernel_mode(curr, regs) && (read_cr4() & X86_CR4_OSXSAVE)) ) - c |= cpufeat_mask(X86_FEATURE_OSXSAVE); + res->c |= cpufeat_mask(X86_FEATURE_OSXSAVE); /* * At the time of writing, a PV domain is the only viable option @@ -461,7 +457,7 @@ static void pv_cpuid(struct cpu_user_regs *regs) * of the XENPF_{add,del,read}_memtype hypercalls. */ if ( cpu_has_mtrr ) - d |= cpufeat_mask(X86_FEATURE_MTRR); + res->d |= cpufeat_mask(X86_FEATURE_MTRR); /* * MONITOR never leaked into PV guests, as PV guests cannot @@ -484,7 +480,7 @@ static void pv_cpuid(struct cpu_user_regs *regs) * fault is currently being serviced. Yuck... */ if ( cpu_has_monitor && regs->entry_vector == TRAP_gp_fault ) - c |= cpufeat_mask(X86_FEATURE_MONITOR); + res->c |= cpufeat_mask(X86_FEATURE_MONITOR); /* * While MONITOR never leaked into PV guests, EIST always used @@ -495,18 +491,18 @@ static void pv_cpuid(struct cpu_user_regs *regs) * CPUID information. */ if ( cpu_has_eist ) - c |= cpufeat_mask(X86_FEATURE_EIST); + res->c |= cpufeat_mask(X86_FEATURE_EIST); } } if ( vpmu_enabled(curr) && vpmu_is_set(vcpu_vpmu(curr), VPMU_CPU_HAS_DS) ) { - d |= cpufeat_mask(X86_FEATURE_DS); + res->d |= cpufeat_mask(X86_FEATURE_DS); if ( cpu_has(¤t_cpu_data, X86_FEATURE_DTES64) ) - c |= cpufeat_mask(X86_FEATURE_DTES64); + res->c |= cpufeat_mask(X86_FEATURE_DTES64); if ( cpu_has(¤t_cpu_data, X86_FEATURE_DSCPL) ) - c |= cpufeat_mask(X86_FEATURE_DSCPL); + res->c |= cpufeat_mask(X86_FEATURE_DSCPL); } break; @@ -516,8 +512,8 @@ static void pv_cpuid(struct cpu_user_regs *regs) goto unsupported; /* Report at most version 3 since that's all we currently emulate. */ - if ( (a & 0xff) > 3 ) - a = (a & ~0xff) | 3; + if ( (res->a & 0xff) > 3 ) + res->a = (res->a & ~0xff) | 3; break; case XSTATE_CPUID: @@ -551,33 +547,33 @@ static void pv_cpuid(struct cpu_user_regs *regs) xstate_sizes[_XSTATE_HI_ZMM]); } - a = (uint32_t)xfeature_mask; - d = (uint32_t)(xfeature_mask >> 32); - c = xstate_size; + res->a = xfeature_mask; + res->d = xfeature_mask >> 32; + res->c = xstate_size; /* * Always read CPUID.0xD[ECX=0].EBX from hardware, rather than * domain policy. It varies with enabled xstate, and the correct * xcr0 is in context. */ - cpuid_count(leaf, subleaf, &tmp, &b, &tmp, &tmp); + cpuid_count(leaf, subleaf, &tmp, &res->b, &tmp, &tmp); break; } case 1: - a = p->xstate.Da1; - b = c = d = 0; + res->a = p->xstate.Da1; + res->b = res->c = res->d = 0; break; } break; case 0x80000001: - c = p->extd.e1c; - d = p->extd.e1d; + res->c = p->extd.e1c; + res->d = p->extd.e1d; /* If not emulating AMD, clear the duplicated features in e1d. */ if ( currd->arch.x86_vendor != X86_VENDOR_AMD ) - d &= ~CPUID_COMMON_1D_FEATURES; + res->d &= ~CPUID_COMMON_1D_FEATURES; /* * MTRR used to unconditionally leak into PV guests. They cannot MTRR @@ -588,18 +584,18 @@ static void pv_cpuid(struct cpu_user_regs *regs) * the MTRR feature as an indication of the presence of the * XENPF_{add,del,read}_memtype hypercalls. */ - if ( is_hardware_domain(currd) && guest_kernel_mode(curr, regs) && - cpu_has_mtrr ) - d |= cpufeat_mask(X86_FEATURE_MTRR); + if ( is_hardware_domain(currd) && cpu_has_mtrr && + guest_kernel_mode(curr, guest_cpu_user_regs()) ) + res->d |= cpufeat_mask(X86_FEATURE_MTRR); break; case 0x80000007: - d = p->extd.e7d; + res->d = p->extd.e7d; break; case 0x80000008: - a = paddr_bits | (vaddr_bits << 8); - b = p->extd.e8b; + res->a = paddr_bits | (vaddr_bits << 8); + res->b = p->extd.e8b; break; case 0x00000005: /* MONITOR/MWAIT */ @@ -609,57 +605,42 @@ static void pv_cpuid(struct cpu_user_regs *regs) case 0x8000001c: /* Light Weight Profiling */ case 0x8000001e: /* Extended topology reporting */ unsupported: - a = b = c = d = 0; + *res = EMPTY_LEAF; break; case 0x7: ASSERT_UNREACHABLE(); /* Now handled in guest_cpuid(). */ } - - regs->rax = a; - regs->rbx = b; - regs->rcx = c; - regs->rdx = d; } -static void hvm_cpuid(unsigned int input, unsigned int *eax, unsigned int *ebx, - unsigned int *ecx, unsigned int *edx) +static void hvm_cpuid(uint32_t leaf, uint32_t subleaf, struct cpuid_leaf *res) { struct vcpu *v = current; struct domain *d = v->domain; const struct cpuid_policy *p = d->arch.cpuid; - unsigned int count, dummy = 0; - if ( !eax ) - eax = &dummy; - if ( !ebx ) - ebx = &dummy; - if ( !ecx ) - ecx = &dummy; - count = *ecx; - if ( !edx ) - edx = &dummy; + domain_cpuid(d, leaf, subleaf, &res->a, &res->b, &res->c, &res->d); - domain_cpuid(d, input, count, eax, ebx, ecx, edx); - - switch ( input ) + switch ( leaf ) { + unsigned int tmp; + case 0x1: /* Fix up VLAPIC details. */ - *ebx &= 0x00FFFFFFu; - *ebx |= (v->vcpu_id * 2) << 24; + res->b &= 0x00FFFFFFu; + res->b |= (v->vcpu_id * 2) << 24; - *ecx = p->basic._1c; - *edx = p->basic._1d; + res->c = p->basic._1c; + res->d = p->basic._1d; /* APIC exposed to guests, but Fast-forward MSR_APIC_BASE.EN back in. */ if ( vlapic_hw_disabled(vcpu_vlapic(v)) ) - *edx &= ~cpufeat_bit(X86_FEATURE_APIC); + res->d &= ~cpufeat_bit(X86_FEATURE_APIC); /* OSXSAVE clear in policy. Fast-forward CR4 back in. */ if ( v->arch.hvm_vcpu.guest_cr[4] & X86_CR4_OSXSAVE ) - *ecx |= cpufeat_mask(X86_FEATURE_OSXSAVE); + res->c |= cpufeat_mask(X86_FEATURE_OSXSAVE); /* * PSE36 is not supported in shadow mode. This bit should be @@ -676,32 +657,32 @@ static void hvm_cpuid(unsigned int input, unsigned int *eax, unsigned int *ebx, * PSE36 paging. */ if ( !hap_enabled(d) && !(hvm_pae_enabled(v) || hvm_long_mode_enabled(v)) ) - *edx &= ~cpufeat_mask(X86_FEATURE_PSE36); + res->d &= ~cpufeat_mask(X86_FEATURE_PSE36); if ( vpmu_enabled(v) && vpmu_is_set(vcpu_vpmu(v), VPMU_CPU_HAS_DS) ) { - *edx |= cpufeat_mask(X86_FEATURE_DS); + res->d |= cpufeat_mask(X86_FEATURE_DS); if ( cpu_has(¤t_cpu_data, X86_FEATURE_DTES64) ) - *ecx |= cpufeat_mask(X86_FEATURE_DTES64); + res->c |= cpufeat_mask(X86_FEATURE_DTES64); if ( cpu_has(¤t_cpu_data, X86_FEATURE_DSCPL) ) - *ecx |= cpufeat_mask(X86_FEATURE_DSCPL); + res->c |= cpufeat_mask(X86_FEATURE_DSCPL); } break; case 0xb: /* Fix the x2APIC identifier. */ - *edx = v->vcpu_id * 2; + res->d = v->vcpu_id * 2; break; case XSTATE_CPUID: - if ( !p->basic.xsave || count >= 63 ) + if ( !p->basic.xsave || subleaf >= 63 ) { - *eax = *ebx = *ecx = *edx = 0; + *res = EMPTY_LEAF; break; } - switch ( count ) + switch ( subleaf ) { case 0: { @@ -754,21 +735,21 @@ static void hvm_cpuid(unsigned int input, unsigned int *eax, unsigned int *ebx, xstate_sizes[_XSTATE_LWP]); } - *eax = (uint32_t)xfeature_mask; - *edx = (uint32_t)(xfeature_mask >> 32); - *ecx = xstate_size; + res->a = xfeature_mask; + res->d = xfeature_mask >> 32; + res->c = xstate_size; /* * Always read CPUID[0xD,0].EBX from hardware, rather than domain * policy. It varies with enabled xstate, and the correct xcr0 is * in context. */ - cpuid_count(input, count, &dummy, ebx, &dummy, &dummy); + cpuid_count(leaf, subleaf, &tmp, &res->b, &tmp, &tmp); break; } case 1: - *eax = p->xstate.Da1; + res->a = p->xstate.Da1; if ( p->xstate.xsaves ) { @@ -777,12 +758,12 @@ static void hvm_cpuid(unsigned int input, unsigned int *eax, unsigned int *ebx, * domain policy. It varies with enabled xstate, and the * correct xcr0/xss are in context. */ - cpuid_count(input, count, &dummy, ebx, &dummy, &dummy); + cpuid_count(leaf, subleaf, &tmp, &res->b, &tmp, &tmp); } else - *ebx = 0; + res->b = 0; - *ecx = *edx = 0; + res->c = res->d = 0; break; } break; @@ -790,25 +771,25 @@ static void hvm_cpuid(unsigned int input, unsigned int *eax, unsigned int *ebx, case 0x0000000a: /* Architectural Performance Monitor Features (Intel) */ if ( boot_cpu_data.x86_vendor != X86_VENDOR_INTEL || !vpmu_enabled(v) ) { - *eax = *ebx = *ecx = *edx = 0; + *res = EMPTY_LEAF; break; } /* Report at most version 3 since that's all we currently emulate */ - if ( (*eax & 0xff) > 3 ) - *eax = (*eax & ~0xff) | 3; + if ( (res->a & 0xff) > 3 ) + res->a = (res->a & ~0xff) | 3; break; case 0x80000001: - *ecx = p->extd.e1c; - *edx = p->extd.e1d; + res->c = p->extd.e1c; + res->d = p->extd.e1d; /* If not emulating AMD, clear the duplicated features in e1d. */ if ( d->arch.x86_vendor != X86_VENDOR_AMD ) - *edx &= ~CPUID_COMMON_1D_FEATURES; + res->d &= ~CPUID_COMMON_1D_FEATURES; /* fast-forward MSR_APIC_BASE.EN if it hasn't already been clobbered. */ else if ( vlapic_hw_disabled(vcpu_vlapic(v)) ) - *edx &= ~cpufeat_bit(X86_FEATURE_APIC); + res->d &= ~cpufeat_bit(X86_FEATURE_APIC); /* * PSE36 is not supported in shadow mode. This bit should be @@ -825,46 +806,46 @@ static void hvm_cpuid(unsigned int input, unsigned int *eax, unsigned int *ebx, * PSE36 paging. */ if ( !hap_enabled(d) && !(hvm_pae_enabled(v) || hvm_long_mode_enabled(v)) ) - *edx &= ~cpufeat_mask(X86_FEATURE_PSE36); + res->d &= ~cpufeat_mask(X86_FEATURE_PSE36); /* SYSCALL is hidden outside of long mode on Intel. */ if ( d->arch.x86_vendor == X86_VENDOR_INTEL && !hvm_long_mode_enabled(v)) - *edx &= ~cpufeat_mask(X86_FEATURE_SYSCALL); + res->d &= ~cpufeat_mask(X86_FEATURE_SYSCALL); break; case 0x80000007: - *edx = p->extd.e7d; + res->d = p->extd.e7d; break; case 0x80000008: - *eax &= 0xff; - count = d->arch.paging.gfn_bits + PAGE_SHIFT; - if ( *eax > count ) - *eax = count; + res->a &= 0xff; + tmp = d->arch.paging.gfn_bits + PAGE_SHIFT; + if ( res->a > tmp ) + res->a = tmp; - count = (p->basic.pae || p->basic.pse36) ? 36 : 32; - if ( *eax < count ) - *eax = count; + tmp = (p->basic.pae || p->basic.pse36) ? 36 : 32; + if ( res->a < tmp ) + res->a = tmp; - *eax |= (p->extd.lm ? vaddr_bits : 32) << 8; + res->a |= (p->extd.lm ? vaddr_bits : 32) << 8; - *ebx = p->extd.e8b; + res->b = p->extd.e8b; break; case 0x8000001c: if ( !cpu_has_svm ) { - *eax = *ebx = *ecx = *edx = 0; + *res = EMPTY_LEAF; break; } if ( cpu_has_lwp && (v->arch.xcr0 & XSTATE_LWP) ) /* Turn on available bit and other features specified in lwp_cfg. */ - *eax = (*edx & v->arch.hvm_svm.guest_lwp_cfg) | 1; + res->a = (res->d & v->arch.hvm_svm.guest_lwp_cfg) | 1; else - *eax = 0; + res->a = 0; break; case 0x7: @@ -969,27 +950,7 @@ void guest_cpuid(const struct vcpu *v, uint32_t leaf, legacy: /* {hvm,pv}_cpuid() have this expectation. */ ASSERT(v == curr); - - if ( is_hvm_domain(d) ) - { - res->c = subleaf; - - hvm_cpuid(leaf, &res->a, &res->b, &res->c, &res->d); - } - else - { - struct cpu_user_regs regs = *guest_cpu_user_regs(); - - regs._eax = leaf; - regs._ecx = subleaf; - - pv_cpuid(®s); - - res->a = regs._eax; - res->b = regs._ebx; - res->c = regs._ecx; - res->d = regs._edx; - } + (is_hvm_domain(d) ? hvm_cpuid : pv_cpuid)(leaf, subleaf, res); } static void __init __maybe_unused build_assertions(void)