From patchwork Wed Mar 2 09:46:22 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Shuai Ruan X-Patchwork-Id: 8479601 Return-Path: X-Original-To: patchwork-xen-devel@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork2.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork2.web.kernel.org (Postfix) with ESMTP id 88214C0553 for ; Wed, 2 Mar 2016 09:51:57 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 945F92011E for ; Wed, 2 Mar 2016 09:51:56 +0000 (UTC) Received: from lists.xen.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.kernel.org (Postfix) with ESMTPS id A1A5A2010B for ; Wed, 2 Mar 2016 09:51:55 +0000 (UTC) Received: from localhost ([127.0.0.1] helo=lists.xen.org) by lists.xen.org with esmtp (Exim 4.84) (envelope-from ) id 1ab3P5-000052-Sg; Wed, 02 Mar 2016 09:49:11 +0000 Received: from mail6.bemta5.messagelabs.com ([195.245.231.135]) by lists.xen.org with esmtp (Exim 4.84) (envelope-from ) id 1ab3P4-00004w-BI for xen-devel@lists.xen.org; Wed, 02 Mar 2016 09:49:10 +0000 Received: from [85.158.139.211] by server-14.bemta-5.messagelabs.com id 5A/01-21430-517B6D65; Wed, 02 Mar 2016 09:49:09 +0000 X-Env-Sender: shuai.ruan@linux.intel.com X-Msg-Ref: server-14.tower-206.messagelabs.com!1456912147!25888683!1 X-Originating-IP: [192.55.52.93] X-SpamReason: No, hits=0.0 required=7.0 tests=sa_preprocessor: VHJ1c3RlZCBJUDogMTkyLjU1LjUyLjkzID0+IDMyNDY2NQ==\n X-StarScan-Received: X-StarScan-Version: 8.11; banners=-,-,- X-VirusChecked: Checked Received: (qmail 8111 invoked from network); 2 Mar 2016 09:49:08 -0000 Received: from mga11.intel.com (HELO mga11.intel.com) (192.55.52.93) by server-14.tower-206.messagelabs.com with SMTP; 2 Mar 2016 09:49:08 -0000 Received: from fmsmga001.fm.intel.com ([10.253.24.23]) by fmsmga102.fm.intel.com with ESMTP; 02 Mar 2016 01:49:07 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.22,528,1449561600"; d="scan'208";a="915121897" Received: from rs-vmm.bj.intel.com ([10.238.135.71]) by fmsmga001.fm.intel.com with ESMTP; 02 Mar 2016 01:49:06 -0800 From: Shuai Ruan To: xen-devel@lists.xen.org Date: Wed, 2 Mar 2016 17:46:22 +0800 Message-Id: <1456911982-24357-1-git-send-email-shuai.ruan@linux.intel.com> X-Mailer: git-send-email 1.9.1 Cc: andrew.cooper3@citrix.com, keir@xen.org, jbeulich@suse.com Subject: [Xen-devel] [V2] x86/xsaves: calculate the xstate_comp_offsets base on xcomp_bv 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: , MIME-Version: 1.0 Errors-To: xen-devel-bounces@lists.xen.org Sender: "Xen-devel" X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Previous patch using all available features calculate xstate_comp_offsets. This is wrong.This patch fix this bug by calculating the xstate_comp_offset based on xcomp_bv of current guest. Also, the xstate_comp_offset should take alignment into consideration. V2: Address comments from Jan: 1. code style fix 2. setup_xstate_comp take xcomp_bv as param. Signed-off-by: Shuai Ruan Reported-by: Jan Beulich --- xen/arch/x86/xstate.c | 34 ++++++++++++++++++++++++---------- xen/include/asm-x86/xstate.h | 2 ++ 2 files changed, 26 insertions(+), 10 deletions(-) diff --git a/xen/arch/x86/xstate.c b/xen/arch/x86/xstate.c index 8316bd9..dd6d37a 100644 --- a/xen/arch/x86/xstate.c +++ b/xen/arch/x86/xstate.c @@ -26,6 +26,7 @@ u64 __read_mostly xfeature_mask; static unsigned int *__read_mostly xstate_offsets; unsigned int *__read_mostly xstate_sizes; +static unsigned int *__read_mostly xstate_align; static unsigned int __read_mostly xstate_features; static unsigned int __read_mostly xstate_comp_offsets[sizeof(xfeature_mask)*8]; @@ -94,7 +95,7 @@ static bool_t xsave_area_compressed(const struct xsave_struct *xsave_area) static int setup_xstate_features(bool_t bsp) { - unsigned int leaf, tmp, eax, ebx; + unsigned int leaf, eax, ebx, ecx, edx; if ( bsp ) { @@ -106,26 +107,34 @@ static int setup_xstate_features(bool_t bsp) xstate_sizes = xzalloc_array(unsigned int, xstate_features); if ( !xstate_sizes ) return -ENOMEM; + + xstate_align = xzalloc_array(unsigned int, xstate_features); + if ( !xstate_align ) + return -ENOMEM; } for ( leaf = 2; leaf < xstate_features; leaf++ ) { if ( bsp ) + { cpuid_count(XSTATE_CPUID, leaf, &xstate_sizes[leaf], - &xstate_offsets[leaf], &tmp, &tmp); + &xstate_offsets[leaf], &ecx, &edx); + xstate_align[leaf] = ecx & XSTATE_ALIGN64; + } else { cpuid_count(XSTATE_CPUID, leaf, &eax, - &ebx, &tmp, &tmp); + &ebx, &ecx, &edx); BUG_ON(eax != xstate_sizes[leaf]); BUG_ON(ebx != xstate_offsets[leaf]); + BUG_ON((ecx & XSTATE_ALIGN64) != xstate_align[leaf]); } } return 0; } -static void __init setup_xstate_comp(void) +static void setup_xstate_comp(const u64 xcomp_bv) { unsigned int i; @@ -134,16 +143,19 @@ static void __init setup_xstate_comp(void) * in the fixed offsets in the xsave area in either compacted form * or standard form. */ - xstate_comp_offsets[0] = 0; + memset(xstate_comp_offsets, 0, sizeof(xstate_comp_offsets)); + xstate_comp_offsets[1] = XSAVE_SSE_OFFSET; xstate_comp_offsets[2] = FXSAVE_SIZE + XSAVE_HDR_SIZE; for ( i = 3; i < xstate_features; i++ ) { - xstate_comp_offsets[i] = xstate_comp_offsets[i - 1] + - (((1ul << i) & xfeature_mask) - ? xstate_sizes[i - 1] : 0); + xstate_comp_offsets[i] = (xstate_align[i] ? + ROUNDUP(xstate_comp_offsets[i-1], 64) : + xstate_comp_offsets[i - 1]) + + (((1ul << i) & xcomp_bv) + ? xstate_sizes[i - 1] : 0); ASSERT(xstate_comp_offsets[i] + xstate_sizes[i] <= xsave_cntxt_size); } } @@ -172,6 +184,8 @@ void expand_xsave_states(struct vcpu *v, void *dest, unsigned int size) } ASSERT(xsave_area_compressed(xsave)); + setup_xstate_comp(xsave->xsave_hdr.xcomp_bv); + /* * Copy legacy XSAVE area and XSAVE hdr area. */ @@ -223,6 +237,8 @@ void compress_xsave_states(struct vcpu *v, const void *src, unsigned int size) xsave->xsave_hdr.xstate_bv = xstate_bv; xsave->xsave_hdr.xcomp_bv = v->arch.xcr0_accum | XSTATE_COMPACTION_ENABLED; + setup_xstate_comp(xsave->xsave_hdr.xcomp_bv); + /* * Copy each region from the non-compacted offset to the * possibly compacted offset. @@ -575,8 +591,6 @@ void xstate_init(struct cpuinfo_x86 *c) if ( setup_xstate_features(bsp) && bsp ) BUG(); - if ( bsp && (cpu_has_xsaves || cpu_has_xsavec) ) - setup_xstate_comp(); } static bool_t valid_xcr0(u64 xcr0) diff --git a/xen/include/asm-x86/xstate.h b/xen/include/asm-x86/xstate.h index c28cea5..61d1ddb 100644 --- a/xen/include/asm-x86/xstate.h +++ b/xen/include/asm-x86/xstate.h @@ -46,6 +46,8 @@ #define XSTATE_LAZY (XSTATE_ALL & ~XSTATE_NONLAZY) #define XSTATE_COMPACTION_ENABLED (1ULL << 63) +#define XSTATE_ALIGN64 (1ULL << 1) + extern u64 xfeature_mask; extern unsigned int *xstate_sizes;