From nobody Thu Apr 25 05:23:32 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass(p=reject dis=none) header.from=citrix.com ARC-Seal: i=1; a=rsa-sha256; t=1620056415; cv=none; d=zohomail.com; s=zohoarc; b=fHeb8QH5t1CdrZ0aC6bwO0YQ75TNsHIFPdshs1CeWrUn0BFkD9o4O+gRcl1uhh4M/dlQtHSuLdAdCtFxZTPpMoVczOooRFNZsLROUg/lTmh9Icm7aJpX/4TSpjztHm3iXfPgN7Pqr4/GiKvh7578IrUg6llCMeJlPCkIISIIWMM= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1620056415; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=G5VW+MBiRkXftSVFbiphieWQv3y7NMej8MRlAgIkqVM=; b=hmkjz+Ans6NorxMA3IrBNLrWo0QghqQiTGRiEl22zUqJR1B1eMloC5V0ZDQhKxvvo7qlBYqcCqNpaWPgYf0GUGQHz8/rPIlv8GlPSMpcPBlCbgaAsjWkjaUKJ0lmNJLnLHb/O9eYthfZgevdWpblXy5wD6380fDWNKXVd58nlGc= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass header.from= (p=reject dis=none) header.from= Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1620056415257413.8971267484618; Mon, 3 May 2021 08:40:15 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.121699.229526 (Exim 4.92) (envelope-from ) id 1ldafn-00019Y-UR; Mon, 03 May 2021 15:39:51 +0000 Received: by outflank-mailman (output) from mailman id 121699.229526; Mon, 03 May 2021 15:39:51 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1ldafn-00019R-RO; Mon, 03 May 2021 15:39:51 +0000 Received: by outflank-mailman (input) for mailman id 121699; Mon, 03 May 2021 15:39:51 +0000 Received: from us1-rack-iad1.inumbo.com ([172.99.69.81]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1ldafm-00019M-Uc for xen-devel@lists.xenproject.org; Mon, 03 May 2021 15:39:50 +0000 Received: from esa1.hc3370-68.iphmx.com (unknown [216.71.145.142]) by us1-rack-iad1.inumbo.com (Halon) with ESMTPS id 335820cc-7388-415e-bf6d-33d452fd8cb8; Mon, 03 May 2021 15:39:50 +0000 (UTC) X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: 335820cc-7388-415e-bf6d-33d452fd8cb8 DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=citrix.com; s=securemail; t=1620056390; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=w7Xo1CRX6LgQwvrOXLUtnY7XKHYy8vBzRGguf0D0ZzU=; b=V05TcB4VfeAvhuszyvDSWlXfCCwBPVr2ixm8fZ6G4uR8nSsn3k5WfUbj FNB/6G2wle/pD2af6UTqabFBpFN4P7dhS//xLdvxk8pS2ZF4pBOsvIN1T LNYnucFQsXoqRfILqHhA4TCkIOR/LK08BLHqjYKZiys6Uw/C1aYJegStK g=; Authentication-Results: esa1.hc3370-68.iphmx.com; dkim=none (message not signed) header.i=none IronPort-SDR: vAYGs1Hij7qrprczXNX/gUlo7yBa9ui7IFjWJMPWCgq/AWEMZ+Euoz2iNLBf7NBIRU3+e9qUsi BGNyXhKmtCemdtRJINHLmWFi165SmJDLpzOnkk/UmI6g/C8tVEEN52OI28sAl4pC7c+1UFuiP4 dRngxbWub4HGWauMHLjZdCQUd8w+b6gCyIliaGyFdSZk3je3Iw7NhCUqLnRfvAX1q47/UyJPJO Aqu1N+MkaT6b3Q958vwIoDcvHo3jd5mobTU/RvlQQrXziWw7SrEgCP9VSzX3SdKbVC2ni6udrv m7U= X-SBRS: 5.1 X-MesageID: 43332330 X-Ironport-Server: esa1.hc3370-68.iphmx.com X-Remote-IP: 162.221.158.21 X-Policy: $RELAYED IronPort-HdrOrdr: A9a23:ECAIYKq21n+vdPLaZJjR6okaV5rUeYIsi2QD101hICF9WMqeis yogbAnxQb54Qx8ZFgMu/ClfJOBT3TV6IJv7eAqVouKcQH6tAKTQ71KwpDlx1TbdRHW0uJGz6 9vf+xfJbTLbWRSqcb/7E2GH807wN+BmZrIuc7kw31gTR5nZshbhm8SZzqzKFF8RwVNGPMCZf mhz/dAzgDQHEg/X4CWAWQEQviGh/CjruODXTc2QyQIrCWvoFqTmdzHLyQ= X-IronPort-AV: E=Sophos;i="5.82,270,1613451600"; d="scan'208";a="43332330" From: Andrew Cooper To: Xen-devel CC: Andrew Cooper , Jan Beulich , =?UTF-8?q?Roger=20Pau=20Monn=C3=A9?= , Wei Liu Subject: [PATCH 1/5] x86/xstate: Elide redundant writes in set_xcr0() Date: Mon, 3 May 2021 16:39:34 +0100 Message-ID: <20210503153938.14109-2-andrew.cooper3@citrix.com> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20210503153938.14109-1-andrew.cooper3@citrix.com> References: <20210503153938.14109-1-andrew.cooper3@citrix.com> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @citrix.com) XSETBV is an expensive instruction as, amongst other things, it involves reconfiguring the instruction decode at the frontend of the pipeline. We have several paths which reconfigure %xcr0 in quick succession (the cont= ext switch path has 5, including the fpu save/restore helpers), and only a sing= le caller takes any care to try to skip redundant writes. Update set_xcr0() to perform amortisation automatically, and simplify the __context_switch() path as a consequence. Signed-off-by: Andrew Cooper Reviewed-by: Jan Beulich --- CC: Jan Beulich CC: Roger Pau Monn=C3=A9 CC: Wei Liu --- xen/arch/x86/domain.c | 4 +--- xen/arch/x86/xstate.c | 15 +++++++++++---- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/xen/arch/x86/domain.c b/xen/arch/x86/domain.c index 4dc27f798e..50a27197b5 100644 --- a/xen/arch/x86/domain.c +++ b/xen/arch/x86/domain.c @@ -1977,9 +1977,7 @@ static void __context_switch(void) memcpy(stack_regs, &n->arch.user_regs, CTXT_SWITCH_STACK_BYTES); if ( cpu_has_xsave ) { - u64 xcr0 =3D n->arch.xcr0 ?: XSTATE_FP_SSE; - - if ( xcr0 !=3D get_xcr0() && !set_xcr0(xcr0) ) + if ( !set_xcr0(n->arch.xcr0 ?: XSTATE_FP_SSE) ) BUG(); =20 if ( cpu_has_xsaves && is_hvm_vcpu(n) ) diff --git a/xen/arch/x86/xstate.c b/xen/arch/x86/xstate.c index 3794d9a5a5..f82dae8053 100644 --- a/xen/arch/x86/xstate.c +++ b/xen/arch/x86/xstate.c @@ -55,11 +55,18 @@ static inline bool xsetbv(u32 index, u64 xfeatures) return lo !=3D 0; } =20 -bool set_xcr0(u64 xfeatures) +bool set_xcr0(u64 val) { - if ( !xsetbv(XCR_XFEATURE_ENABLED_MASK, xfeatures) ) - return false; - this_cpu(xcr0) =3D xfeatures; + uint64_t *this_xcr0 =3D &this_cpu(xcr0); + + if ( *this_xcr0 !=3D val ) + { + if ( !xsetbv(XCR_XFEATURE_ENABLED_MASK, val) ) + return false; + + *this_xcr0 =3D val; + } + return true; } =20 --=20 2.11.0 From nobody Thu Apr 25 05:23:32 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass(p=reject dis=none) header.from=citrix.com ARC-Seal: i=1; a=rsa-sha256; t=1620056432; cv=none; d=zohomail.com; s=zohoarc; b=k1bsjkjBf+5lcWDgqy6Amh0wXJwkkcV7jbf9/U+Ri3kH3flC/SE0eeYPJt7Vyrgto6oCFz31o6WcKY7m+gi+7U/LcGCViA5DwRVTCrlAbDRAlQgHvQonLGYC+SLTFVTDhmEOdP+GBPS2IfHDuYZjcFqQxzv8OQ/adHNWtIC3ZuY= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1620056432; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=F10saV9MrvqaJxiBzc+z1Jk3cu//eDFS/mytYs/qxE8=; b=dn9Wfd45JNle4++IOF5aG/u5EH7j0xJz99QcKnUOcDzQi9NgjuoxXtnVWq2y3LzB8w/oQMzvhYpsZ6ZEL3h8RoqnWppb2KabZiK22KluW3MnR0rB40VxpREz+umeSCneTszJXF5d63YDwmoQO2rVz6fzxgx28Hsrs73adiUD5G8= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass header.from= (p=reject dis=none) header.from= Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1620056432371341.38688830627495; Mon, 3 May 2021 08:40:32 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.121708.229574 (Exim 4.92) (envelope-from ) id 1ldagG-00029M-9B; Mon, 03 May 2021 15:40:20 +0000 Received: by outflank-mailman (output) from mailman id 121708.229574; Mon, 03 May 2021 15:40:20 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1ldagG-00029B-3u; Mon, 03 May 2021 15:40:20 +0000 Received: by outflank-mailman (input) for mailman id 121708; Mon, 03 May 2021 15:40:18 +0000 Received: from us1-rack-iad1.inumbo.com ([172.99.69.81]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1ldagE-00024k-PX for xen-devel@lists.xenproject.org; Mon, 03 May 2021 15:40:18 +0000 Received: from esa2.hc3370-68.iphmx.com (unknown [216.71.145.153]) by us1-rack-iad1.inumbo.com (Halon) with ESMTPS id 6c9f735a-84bf-45b9-93ac-1f6aa835f82c; Mon, 03 May 2021 15:40:13 +0000 (UTC) X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: 6c9f735a-84bf-45b9-93ac-1f6aa835f82c DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=citrix.com; s=securemail; t=1620056413; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=jIHwbXNLvjr4f1oZ4tb+qy7o6+MtjNlDvDcd+ysvfTI=; b=BuqCEfw74JtwAkbgUHEaJ4rW7Lvf+g4qEZ2gem1LZsno/9w2T50EiQzC 1hjkaCF43UD5FvAxD5vkXiWC2hhI4BrEEo02kSn6Jqc2E8LSJeV8PSfO5 uAM8B2hUPOXRKdQvySIjKreLPUe89c/837zxvqHj5/ubatgU7Hx2dTBIB s=; Authentication-Results: esa2.hc3370-68.iphmx.com; dkim=none (message not signed) header.i=none IronPort-SDR: H8alMazpCCJaYOggVvYZDMfYy/DJMgq68K+15edzm5ilvKT9vOwyZAbK4zXMFQsGjpvIl4Ip/0 nwLK/2Zp9/y1udBRVGmlXdRyG4omwm65Jugrc0AMhyjBcJM022qsa80ghR3JcJVnUZILxfJav9 gnpBHL2P//yUwAI+4a8TTlnlUuyxAf04CVtk+J7l2zgXYRXL4S31rkABAslf3MqDY+N23PEZpe /VmAKPrEU9vp2EXqUxVFK4ByskrxZ4Io22/XvN3/gNd9OGtI1/Nx3vQrFO1WMR69GPug7arKUI t2U= X-SBRS: 5.1 X-MesageID: 42942252 X-Ironport-Server: esa2.hc3370-68.iphmx.com X-Remote-IP: 162.221.158.21 X-Policy: $RELAYED IronPort-HdrOrdr: A9a23:gYQGjKgbg5jonU+AM8TG8wVStHBQXl0ji2hD6mlwRA09T+Wzva mV/cgz/xnylToXRTUcgtiGIqaNWjfx8pRy7IkXM96ZLXHbkUGvK5xv6pan/i34F0TFh5dg/I ppbqQWMqySMXFUlsD/iTPWL/8Bx529/LmslaPiyR5WPGVXQoVByys8NQqBCE1xQ2B9dPwEPb 6R/NBOqTblWVl/VLXYOlA/U+LOp8LGmfvdCHZsbXNK1CC0gTyl87L8GRSDty1uNA9n+rs+7X PD1zXw+6TLiYDB9jbny2TR455K8eGA9vJ/AqW35PQ9G3HJggasaJ8JYczmgAwI X-IronPort-AV: E=Sophos;i="5.82,270,1613451600"; d="scan'208";a="42942252" From: Andrew Cooper To: Xen-devel CC: Andrew Cooper , Jan Beulich , =?UTF-8?q?Roger=20Pau=20Monn=C3=A9?= , Wei Liu Subject: [PATCH 2/5] x86/xstate: Rename _xstate_ctxt_size() to hw_uncompressed_size() Date: Mon, 3 May 2021 16:39:35 +0100 Message-ID: <20210503153938.14109-3-andrew.cooper3@citrix.com> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20210503153938.14109-1-andrew.cooper3@citrix.com> References: <20210503153938.14109-1-andrew.cooper3@citrix.com> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @citrix.com) The latter is a more descriptive name, as it explicitly highlights the query from hardware. Simplify the internal logic using cpuid_count_ebx(), and drop the curr/max assertion as this property is guaranteed by the x86 ISA. Signed-off-by: Andrew Cooper Reviewed-by: Jan Beulich --- CC: Jan Beulich CC: Roger Pau Monn=C3=A9 CC: Wei Liu --- xen/arch/x86/xstate.c | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/xen/arch/x86/xstate.c b/xen/arch/x86/xstate.c index f82dae8053..e6c225a16b 100644 --- a/xen/arch/x86/xstate.c +++ b/xen/arch/x86/xstate.c @@ -554,19 +554,18 @@ void xstate_free_save_area(struct vcpu *v) v->arch.xsave_area =3D NULL; } =20 -static unsigned int _xstate_ctxt_size(u64 xcr0) +static unsigned int hw_uncompressed_size(uint64_t xcr0) { u64 act_xcr0 =3D get_xcr0(); - u32 eax, ebx =3D 0, ecx, edx; + unsigned int size; bool ok =3D set_xcr0(xcr0); =20 ASSERT(ok); - cpuid_count(XSTATE_CPUID, 0, &eax, &ebx, &ecx, &edx); - ASSERT(ebx <=3D ecx); + size =3D cpuid_count_ebx(XSTATE_CPUID, 0); ok =3D set_xcr0(act_xcr0); ASSERT(ok); =20 - return ebx; + return size; } =20 /* Fastpath for common xstate size requests, avoiding reloads of xcr0. */ @@ -578,7 +577,7 @@ unsigned int xstate_ctxt_size(u64 xcr0) if ( xcr0 =3D=3D 0 ) return 0; =20 - return _xstate_ctxt_size(xcr0); + return hw_uncompressed_size(xcr0); } =20 /* Collect the information of processor's extended state */ @@ -635,14 +634,14 @@ void xstate_init(struct cpuinfo_x86 *c) * xsave_cntxt_size is the max size required by enabled features. * We know FP/SSE and YMM about eax, and nothing about edx at pres= ent. */ - xsave_cntxt_size =3D _xstate_ctxt_size(feature_mask); + xsave_cntxt_size =3D hw_uncompressed_size(feature_mask); printk("xstate: size: %#x and states: %#"PRIx64"\n", xsave_cntxt_size, xfeature_mask); } else { BUG_ON(xfeature_mask !=3D feature_mask); - BUG_ON(xsave_cntxt_size !=3D _xstate_ctxt_size(feature_mask)); + BUG_ON(xsave_cntxt_size !=3D hw_uncompressed_size(feature_mask)); } =20 if ( setup_xstate_features(bsp) && bsp ) --=20 2.11.0 From nobody Thu Apr 25 05:23:32 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass(p=reject dis=none) header.from=citrix.com ARC-Seal: i=1; a=rsa-sha256; t=1620056434; cv=none; d=zohomail.com; s=zohoarc; b=F84dtyqtxngCxt/ObFoKeisF0PSii79ZEAHpvis9Zo1/zc2GA92UQMW4XD0jSuFl7PdL6G6OavIocrIpHx6vvVbsN+ESh3dnosHV93ogdy/8NjZGVgK0v6rOexkBDNvH7kTDJm+hxmAPUjpZNx9wmqtfXxjIAZbIW9TnCS8D3qM= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1620056434; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=9yW5fSTLVZf95L0L8MpQ7+V9dFDGQnnKyfNLXbwkLAs=; b=UboJTgSzJdrbjgWbDPBd5jwStZusm4nOlde8yztjpyGJAJRbHnr1tyXoSC0wofccVPeLSi2Y0wVZNeDhZKNSAsOnp2+tGUcu2caczZ/wssW6A4X1rZntw6FCywBkDlL2YGvzd3VKvXuU1bp7UJdhgI/5kzQ32F9WVLM9peHc3oE= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass header.from= (p=reject dis=none) header.from= Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1620056434103455.3040094453585; Mon, 3 May 2021 08:40:34 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.121711.229586 (Exim 4.92) (envelope-from ) id 1ldagI-0002Br-Fo; Mon, 03 May 2021 15:40:22 +0000 Received: by outflank-mailman (output) from mailman id 121711.229586; Mon, 03 May 2021 15:40:22 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1ldagI-0002Bj-CC; Mon, 03 May 2021 15:40:22 +0000 Received: by outflank-mailman (input) for mailman id 121711; Mon, 03 May 2021 15:40:20 +0000 Received: from all-amaz-eas1.inumbo.com ([34.197.232.57] helo=us1-amaz-eas2.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1ldagG-00023Q-Jm for xen-devel@lists.xenproject.org; Mon, 03 May 2021 15:40:20 +0000 Received: from esa2.hc3370-68.iphmx.com (unknown [216.71.145.153]) by us1-amaz-eas2.inumbo.com (Halon) with ESMTPS id 503cc877-2ae4-455c-add3-a9ba24c79b18; Mon, 03 May 2021 15:40:14 +0000 (UTC) X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: 503cc877-2ae4-455c-add3-a9ba24c79b18 DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=citrix.com; s=securemail; t=1620056414; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=L7RgdbGEd1jyg+A5R9ekb1vmEPCeoLbeT25HF5YV6ys=; b=Fo6y/mfH4tilvi2rNZGdCRu3ELjtjTRKZFN75E0ja+DjNRNaU2MumzpS FYUtliEd0BmgfxJGvCegInLmKaff+DAncDGV7uJlf4DO/1PoZVI2r23YV YkJRjy4tlg8FDtW+QUUaCD/UrXVjyXYx4uH97egXIbZyWhO+lfRsveG2T Q=; Authentication-Results: esa2.hc3370-68.iphmx.com; dkim=none (message not signed) header.i=none IronPort-SDR: GmP/0IMBbxNpZqx3flufAKBbnNuepUNa6zZBqCJSu5LACseXfw6ekok5Z0rgXVko7KX9BIRgd3 mBvcUPI63l31kjEYczwPlFfJkZjevfhSbt7ca7YvmTuCzQMept3XJ4uupBCOIWMsEBIJcrYHEy L/KBJlZXSDmwHxjogGD3hRT4T/5Sc3m5NV3GGD/72f3orz9PTfpxBTAgPapVCWN/tocfcRQrnt 17bq54ACAzCZsff1MTMinMDTg+wHPOIGPe3KpdlWSOX/JHbWgqF0jdoPbANaDsIGZbWilv+/T9 NtY= X-SBRS: 5.1 X-MesageID: 42942253 X-Ironport-Server: esa2.hc3370-68.iphmx.com X-Remote-IP: 162.221.158.21 X-Policy: $RELAYED IronPort-HdrOrdr: A9a23:EOUCG6/QoiV6zIOdCWNuk+BfI+orLtY04lQ7vn1ZYzY9SK2lvu qpm+kW0gKxtS0YX2sulcvFFK6LR37d8pAd2/hoAZ6JWg76tGy0aLxz9IeK+UyYJwTS/vNQvJ 0QEJRWJ8b3CTFB4vrSwA79KNo4xcnCzabAv5a7815IbSVHL55t9B14DAHzKDwReCBjCYAiHJ SRouprzgDQG0g/VciwCnkbU+WrnbSi//iKDSIuPBIp5BKDijml8tfBYn+l9ywTTi9VxvMa+X XF+jaJnZmLie2xyRPXygboj6h+pd2J8LV+Lf3JrsAULzn24zzYAbhcZw== X-IronPort-AV: E=Sophos;i="5.82,270,1613451600"; d="scan'208";a="42942253" From: Andrew Cooper To: Xen-devel CC: Andrew Cooper , Jan Beulich , =?UTF-8?q?Roger=20Pau=20Monn=C3=A9?= , Wei Liu Subject: [PATCH 3/5] x86/xstate: Rework xstate_ctxt_size() as xstate_uncompressed_size() Date: Mon, 3 May 2021 16:39:36 +0100 Message-ID: <20210503153938.14109-4-andrew.cooper3@citrix.com> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20210503153938.14109-1-andrew.cooper3@citrix.com> References: <20210503153938.14109-1-andrew.cooper3@citrix.com> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @citrix.com) We're soon going to need a compressed helper of the same form. The size of the uncompressed image is a strictly a property of the highest user state. This can be calculated trivially with xstate_offsets/sizes, and is much faster than a CPUID instruction in the first place, let alone the t= wo XCR0 writes surrounding it. Retain the cross-check with hardware in debug builds, but forgo it normal builds. In particular, this means that the migration paths don't need to m= ess with XCR0 just to sanity check the buffer size. Signed-off-by: Andrew Cooper --- CC: Jan Beulich CC: Roger Pau Monn=C3=A9 CC: Wei Liu --- xen/arch/x86/domctl.c | 2 +- xen/arch/x86/hvm/hvm.c | 2 +- xen/arch/x86/xstate.c | 40 +++++++++++++++++++++++++++++++--------- xen/include/asm-x86/xstate.h | 2 +- 4 files changed, 34 insertions(+), 12 deletions(-) diff --git a/xen/arch/x86/domctl.c b/xen/arch/x86/domctl.c index e440bd021e..8c3552410d 100644 --- a/xen/arch/x86/domctl.c +++ b/xen/arch/x86/domctl.c @@ -899,7 +899,7 @@ long arch_do_domctl( uint32_t offset =3D 0; =20 #define PV_XSAVE_HDR_SIZE (2 * sizeof(uint64_t)) -#define PV_XSAVE_SIZE(xcr0) (PV_XSAVE_HDR_SIZE + xstate_ctxt_size(xcr0)) +#define PV_XSAVE_SIZE(xcr0) (PV_XSAVE_HDR_SIZE + xstate_uncompressed_size(= xcr0)) =20 ret =3D -ESRCH; if ( (evc->vcpu >=3D d->max_vcpus) || diff --git a/xen/arch/x86/hvm/hvm.c b/xen/arch/x86/hvm/hvm.c index 28beacc45b..e5fda6b387 100644 --- a/xen/arch/x86/hvm/hvm.c +++ b/xen/arch/x86/hvm/hvm.c @@ -1203,7 +1203,7 @@ HVM_REGISTER_SAVE_RESTORE(CPU, hvm_save_cpu_ctxt, hvm= _load_cpu_ctxt, 1, =20 #define HVM_CPU_XSAVE_SIZE(xcr0) (offsetof(struct hvm_hw_cpu_xsave, \ save_area) + \ - xstate_ctxt_size(xcr0)) + xstate_uncompressed_size(xcr0)) =20 static int hvm_save_cpu_xsave_states(struct vcpu *v, hvm_domain_context_t = *h) { diff --git a/xen/arch/x86/xstate.c b/xen/arch/x86/xstate.c index e6c225a16b..d4c01da574 100644 --- a/xen/arch/x86/xstate.c +++ b/xen/arch/x86/xstate.c @@ -184,7 +184,7 @@ void expand_xsave_states(struct vcpu *v, void *dest, un= signed int size) /* Check there is state to serialise (i.e. at least an XSAVE_HDR) */ BUG_ON(!v->arch.xcr0_accum); /* Check there is the correct room to decompress into. */ - BUG_ON(size !=3D xstate_ctxt_size(v->arch.xcr0_accum)); + BUG_ON(size !=3D xstate_uncompressed_size(v->arch.xcr0_accum)); =20 if ( !(xsave->xsave_hdr.xcomp_bv & XSTATE_COMPACTION_ENABLED) ) { @@ -246,7 +246,7 @@ void compress_xsave_states(struct vcpu *v, const void *= src, unsigned int size) u64 xstate_bv, valid; =20 BUG_ON(!v->arch.xcr0_accum); - BUG_ON(size !=3D xstate_ctxt_size(v->arch.xcr0_accum)); + BUG_ON(size !=3D xstate_uncompressed_size(v->arch.xcr0_accum)); ASSERT(!xsave_area_compressed(src)); =20 xstate_bv =3D ((const struct xsave_struct *)src)->xsave_hdr.xstate_bv; @@ -568,16 +568,38 @@ static unsigned int hw_uncompressed_size(uint64_t xcr= 0) return size; } =20 -/* Fastpath for common xstate size requests, avoiding reloads of xcr0. */ -unsigned int xstate_ctxt_size(u64 xcr0) +unsigned int xstate_uncompressed_size(uint64_t xcr0) { - if ( xcr0 =3D=3D xfeature_mask ) - return xsave_cntxt_size; + unsigned int size; + int idx =3D flsl(xcr0) - 1; =20 - if ( xcr0 =3D=3D 0 ) - return 0; + /* + * The maximum size of an uncompressed XSAVE area is determined by the + * highest user state, as the size and offset of each component is fix= ed. + */ + if ( idx >=3D 2 ) + { + ASSERT(xstate_offsets[idx] && xstate_sizes[idx]); + size =3D xstate_offsets[idx] + xstate_sizes[idx]; + } + else + size =3D XSTATE_AREA_MIN_SIZE; =20 - return hw_uncompressed_size(xcr0); + /* In debug builds, cross-check our calculation with hardware. */ + if ( IS_ENABLED(CONFIG_DEBUG) ) + { + unsigned int hwsize; + + xcr0 |=3D XSTATE_FP_SSE; + hwsize =3D hw_uncompressed_size(xcr0); + + if ( size !=3D hwsize ) + printk_once(XENLOG_ERR "%s(%#"PRIx64") size %#x !=3D hwsize %#= x\n", + __func__, xcr0, size, hwsize); + size =3D hwsize; + } + + return size; } =20 /* Collect the information of processor's extended state */ diff --git a/xen/include/asm-x86/xstate.h b/xen/include/asm-x86/xstate.h index 7ab0bdde89..02d6f171b8 100644 --- a/xen/include/asm-x86/xstate.h +++ b/xen/include/asm-x86/xstate.h @@ -107,7 +107,7 @@ void compress_xsave_states(struct vcpu *v, const void *= src, unsigned int size); void xstate_free_save_area(struct vcpu *v); int xstate_alloc_save_area(struct vcpu *v); void xstate_init(struct cpuinfo_x86 *c); -unsigned int xstate_ctxt_size(u64 xcr0); +unsigned int xstate_uncompressed_size(uint64_t xcr0); =20 static inline uint64_t xgetbv(unsigned int index) { --=20 2.11.0 From nobody Thu Apr 25 05:23:32 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass(p=reject dis=none) header.from=citrix.com ARC-Seal: i=1; a=rsa-sha256; t=1620056411; cv=none; d=zohomail.com; s=zohoarc; b=QR3AlAP41AGFDhDHltijbfjKhmerAEXayRNUsyg6PwH7Qbqip/5H79dNNrGKPAa0Xqz5FyeF3t2MkufAC/zcI5AFowQNxh33z98xWfm2tyv7GK42XNed0eGUHEd1C11EclvZW7J55A74W6PXpnpph7WZj7O9/yy/2DCuTlIdSOg= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1620056411; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=bdRuwXJKESIamgIyQcsUA3Q1omuWQ3UmWCbcyQxJuP0=; b=P2ADP92HmZvhm7UbkD5rVSKq9poTx1aRKInas3+9Z+fUdoLgTrp9lJZLUBjUdXEmdFrLueRmbYwMIJL3+lBcIU9kh+1FTwuqEL8KJq9u8I+70yzFlGBJpcN6VqskT9c4+iX/rbTuuK/XMe7PgjeJUcueCU6BBT8fsNr6ovWymjY= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass header.from= (p=reject dis=none) header.from= Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1620056411174155.78078577525332; Mon, 3 May 2021 08:40:11 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.121701.229550 (Exim 4.92) (envelope-from ) id 1ldaft-0001Do-Jf; Mon, 03 May 2021 15:39:57 +0000 Received: by outflank-mailman (output) from mailman id 121701.229550; Mon, 03 May 2021 15:39:57 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1ldaft-0001Df-GH; Mon, 03 May 2021 15:39:57 +0000 Received: by outflank-mailman (input) for mailman id 121701; Mon, 03 May 2021 15:39:55 +0000 Received: from us1-rack-iad1.inumbo.com ([172.99.69.81]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1ldafr-00019M-Qf for xen-devel@lists.xenproject.org; Mon, 03 May 2021 15:39:55 +0000 Received: from esa1.hc3370-68.iphmx.com (unknown [216.71.145.142]) by us1-rack-iad1.inumbo.com (Halon) with ESMTPS id c3bbfcf4-ecb2-4c99-a13a-5e390c53d09e; Mon, 03 May 2021 15:39:50 +0000 (UTC) X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: c3bbfcf4-ecb2-4c99-a13a-5e390c53d09e DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=citrix.com; s=securemail; t=1620056390; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=UBDI32eRY/hcnx9jebSRvWF1wyrbcX7Hg6wiMTv7Ipw=; b=S8dCWq2rP7/xXKspjjOofl+wDIrtDuFJQLS8Hk6HU38vHvzkiyA+sLwJ sN07PFNNkPZ1RmeOR6/LEKzucLuw+eS8fvOan78RU9IKbMDd56Bx1auA3 8YmqwN7RlAfJb/69VjrNucZH9Pku+4cJZ3Wpl0j6tajNwbxyFxbqr7SCO c=; Authentication-Results: esa1.hc3370-68.iphmx.com; dkim=none (message not signed) header.i=none IronPort-SDR: H5HLuDGyqEByLnCPYbYjLdx7mfF/ERASTva30yvuQ1iok75tvfyaLNmFDOJjcdrg22kapJlzVo /DYx6XvBIdcuSXrsWC2CXKgZhZxazfprOKt5dQJz0I1bNkUPTPNQvfw2zrPiN7b+3OEUuUAmYo 0MkXfJgzwAeV8myO2Kx6f7EqhHyjn6qTUc9mdRVk8FsUxNRXBKZkMNicsQxj1HdFoAfUvv5+VF YdqFWwptXKqRhyk06x/iJkSd9+S4a7zYPcFkLhITqW6X+uOX6QhwuT+79ahopfezdA9puN9XoA oDg= X-SBRS: 5.1 X-MesageID: 43332332 X-Ironport-Server: esa1.hc3370-68.iphmx.com X-Remote-IP: 162.221.158.21 X-Policy: $RELAYED IronPort-HdrOrdr: A9a23:KLSibKFGXtC7fUmdpLqEF8eALOonbusQ8zAX/mp2TgFYddHdqt C2kJ0guSPcpRQwfDUbmd6GMLSdWn+0z/VIyKQYILvKZmbbkUSyKoUK1+Xf6hntATf3+OIY9Y oISchDIfnxCVQ/ssrg+gm/FL8boeWvy6yjiefAw3oFd2gDAcxdxj1kAQWWGFAefngkObMFEv Onl696jgvlVXMLbtmqQlkpNtKzw+HjpdbdT1orJzNP0njtsQ+V X-IronPort-AV: E=Sophos;i="5.82,270,1613451600"; d="scan'208";a="43332332" From: Andrew Cooper To: Xen-devel CC: Andrew Cooper , Jan Beulich , =?UTF-8?q?Roger=20Pau=20Monn=C3=A9?= , Wei Liu Subject: [PATCH 4/5] x86/cpuid: Simplify recalculate_xstate() Date: Mon, 3 May 2021 16:39:37 +0100 Message-ID: <20210503153938.14109-5-andrew.cooper3@citrix.com> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20210503153938.14109-1-andrew.cooper3@citrix.com> References: <20210503153938.14109-1-andrew.cooper3@citrix.com> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @citrix.com) Make use of the new xstate_uncompressed_size() helper rather than maintaini= ng the running calculation while accumulating feature components. The rest of the CPUID data can come direct from the raw cpuid policy. All per-component data forms an ABI through the behaviour of the X{SAVE,RSTOR}* instructions, and are constant. Use for_each_set_bit() rather than opencoding a slightly awkward version of it. Mask the attributes in ecx down based on the visible features. This isn't actually necessary for any components or attributes defined at the ti= me of writing (up to AMX), but is added out of an abundance of caution. Signed-off-by: Andrew Cooper --- CC: Jan Beulich CC: Roger Pau Monn=C3=A9 CC: Wei Liu Using min() in for_each_set_bit() leads to awful code generation, as it prohibits the optimiations for spotting that the bitmap is <=3D BITS_PER_LO= NG. As p->xstate is long enough already, use a BUILD_BUG_ON() instead. --- xen/arch/x86/cpuid.c | 52 +++++++++++++++++-------------------------------= ---- 1 file changed, 17 insertions(+), 35 deletions(-) diff --git a/xen/arch/x86/cpuid.c b/xen/arch/x86/cpuid.c index 752bf244ea..c7f8388e5d 100644 --- a/xen/arch/x86/cpuid.c +++ b/xen/arch/x86/cpuid.c @@ -154,8 +154,7 @@ static void sanitise_featureset(uint32_t *fs) static void recalculate_xstate(struct cpuid_policy *p) { uint64_t xstates =3D XSTATE_FP_SSE; - uint32_t xstate_size =3D XSTATE_AREA_MIN_SIZE; - unsigned int i, Da1 =3D p->xstate.Da1; + unsigned int i, ecx_bits =3D 0, Da1 =3D p->xstate.Da1; =20 /* * The Da1 leaf is the only piece of information preserved in the comm= on @@ -167,61 +166,44 @@ static void recalculate_xstate(struct cpuid_policy *p) return; =20 if ( p->basic.avx ) - { xstates |=3D X86_XCR0_YMM; - xstate_size =3D max(xstate_size, - xstate_offsets[X86_XCR0_YMM_POS] + - xstate_sizes[X86_XCR0_YMM_POS]); - } =20 if ( p->feat.mpx ) - { xstates |=3D X86_XCR0_BNDREGS | X86_XCR0_BNDCSR; - xstate_size =3D max(xstate_size, - xstate_offsets[X86_XCR0_BNDCSR_POS] + - xstate_sizes[X86_XCR0_BNDCSR_POS]); - } =20 if ( p->feat.avx512f ) - { xstates |=3D X86_XCR0_OPMASK | X86_XCR0_ZMM | X86_XCR0_HI_ZMM; - xstate_size =3D max(xstate_size, - xstate_offsets[X86_XCR0_HI_ZMM_POS] + - xstate_sizes[X86_XCR0_HI_ZMM_POS]); - } =20 if ( p->feat.pku ) - { xstates |=3D X86_XCR0_PKRU; - xstate_size =3D max(xstate_size, - xstate_offsets[X86_XCR0_PKRU_POS] + - xstate_sizes[X86_XCR0_PKRU_POS]); - } =20 - p->xstate.max_size =3D xstate_size; + /* Subleaf 0 */ + p->xstate.max_size =3D + xstate_uncompressed_size(xstates & ~XSTATE_XSAVES_ONLY); p->xstate.xcr0_low =3D xstates & ~XSTATE_XSAVES_ONLY; p->xstate.xcr0_high =3D (xstates & ~XSTATE_XSAVES_ONLY) >> 32; =20 + /* Subleaf 1 */ p->xstate.Da1 =3D Da1; if ( p->xstate.xsaves ) { + ecx_bits |=3D 3; /* Align64, XSS */ p->xstate.xss_low =3D xstates & XSTATE_XSAVES_ONLY; p->xstate.xss_high =3D (xstates & XSTATE_XSAVES_ONLY) >> 32; } - else - xstates &=3D ~XSTATE_XSAVES_ONLY; =20 - for ( i =3D 2; i < min(63ul, ARRAY_SIZE(p->xstate.comp)); ++i ) + /* Subleafs 2+ */ + xstates &=3D ~XSTATE_FP_SSE; + BUILD_BUG_ON(ARRAY_SIZE(p->xstate.comp) < 63); + for_each_set_bit ( i, &xstates, 63 ) { - uint64_t curr_xstate =3D 1ul << i; - - if ( !(xstates & curr_xstate) ) - continue; - - p->xstate.comp[i].size =3D xstate_sizes[i]; - p->xstate.comp[i].offset =3D xstate_offsets[i]; - p->xstate.comp[i].xss =3D curr_xstate & XSTATE_XSAVES_ONLY; - p->xstate.comp[i].align =3D curr_xstate & xstate_align; + /* + * Pass through size (eax) and offset (ebx) directly. Visbility of + * attributes in ecx limited by visible features in Da1. + */ + p->xstate.raw[i].a =3D raw_cpuid_policy.xstate.raw[i].a; + p->xstate.raw[i].b =3D raw_cpuid_policy.xstate.raw[i].b; + p->xstate.raw[i].c =3D raw_cpuid_policy.xstate.raw[i].c & ecx_bits; } } =20 --=20 2.11.0 From nobody Thu Apr 25 05:23:32 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass(p=reject dis=none) header.from=citrix.com ARC-Seal: i=1; a=rsa-sha256; t=1620056409; cv=none; d=zohomail.com; s=zohoarc; b=cg//Fq6ajqXfl2AGONM10lG07QM8m6zplg1sDrM7su+CDbBeXsD1sAGhbpx6bLIC2Dyr7OXwD7lUre+ZtOfGNRkWBJJVebXgFfeh5oBagG1mwZ+t6lPflLc/cPmeJOLsTOBTUBw/ZanjPBF5mI6O6XNsfniW1bAfZG4Nr/6HjPQ= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1620056409; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=CNHy5LqXUzLBqJTg8TUktKayXmUFpFcKBSjGidGzcM0=; b=W7c74tqd52Y8iwmFQm9JocBJqTuDObt+sYFg1oxgl+whYvYN3GrMNvuRuai3wici5f4471iF2OZHPVWgQvdFuV+8221Rxk2wgO3gmwE43Tyjzd2dwXuZF6P10xZpI6S6YTC6mBlhhSpvy5A1OZ/qxVnXjVISi0VontXWIKKVWuk= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass header.from= (p=reject dis=none) header.from= Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1620056409753223.19144458043468; Mon, 3 May 2021 08:40:09 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.121700.229538 (Exim 4.92) (envelope-from ) id 1ldafq-0001Aj-6y; Mon, 03 May 2021 15:39:54 +0000 Received: by outflank-mailman (output) from mailman id 121700.229538; Mon, 03 May 2021 15:39:54 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1ldafq-0001Ab-3C; Mon, 03 May 2021 15:39:54 +0000 Received: by outflank-mailman (input) for mailman id 121700; Mon, 03 May 2021 15:39:52 +0000 Received: from all-amaz-eas1.inumbo.com ([34.197.232.57] helo=us1-amaz-eas2.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1ldafo-0001AE-H9 for xen-devel@lists.xenproject.org; Mon, 03 May 2021 15:39:52 +0000 Received: from esa1.hc3370-68.iphmx.com (unknown [216.71.145.142]) by us1-amaz-eas2.inumbo.com (Halon) with ESMTPS id c419d8a6-f98f-46b6-a958-3e897796841f; Mon, 03 May 2021 15:39:51 +0000 (UTC) X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: c419d8a6-f98f-46b6-a958-3e897796841f DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=citrix.com; s=securemail; t=1620056391; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=O2yVEz5u95v8j+WwtRne2yHrcHV1GGCzk+PGhtB6nEs=; b=K4hFoYpeokTeD06OFn/amVtCtKK3JmgB7QqKnmlYuT0yS+5qWtv5wKi7 ie907rwrhukH9TeY//dTd7aDxpnStZgRGCaEyKgzGIdI/mPZPGERm1wK5 lwoauB+HsqFYUYvBKG5CtAkWut/RKpLlZHkmbRaSlRCY3y1l5Lo9C11j3 M=; Authentication-Results: esa1.hc3370-68.iphmx.com; dkim=none (message not signed) header.i=none IronPort-SDR: ND6u3vdoKm4YWMNTymQAmDMkd0lpy/mP8prIe8qqAfuEtlNhe+FCyGdoS390/SYVs8FM+ek5sC GsMAwiWTB3sy/ypNi5KzHL2DcZlwucTKGSVVElcdEFExPw41PsRPe/VE6TXsJC6kWz33idZ8DO JmwATgSbNnNL6OfW1NlbdSgKpWTzVhdS2jIxs0wRHf0/m+TyTcdA+9Wq/UjNezRK/sHCy+w9tK YAc8+Fje0uwWB+rrv8vPO0u2tr9Tn4mQHSDLziD0qvZhCTrEmDeAeWsfm5EhXgQL3I6MBpc18H DpQ= X-SBRS: 5.1 X-MesageID: 43332334 X-Ironport-Server: esa1.hc3370-68.iphmx.com X-Remote-IP: 162.221.158.21 X-Policy: $RELAYED IronPort-HdrOrdr: A9a23:9sViJagXbX0eZuIcRW07O+jHQXBQXl0ji2hD6mlwRA09T+Wzva mV/cgz/xnylToXRTUcgtiGIqaNWjfx8pRy7IkXM96ZLXHbkUGvK5xv6pan/i34F0TFh5dg/I ppbqQWMqySMXFUlsD/iTPWL/8Bx529/LmslaPiyR5WPGVXQoVByys8NQqBCE1xQ2B9dPwEPb 6R/NBOqTblWVl/VLXYOlA/U+LOp8LGmfvdCHZsbXNK1CC0gTyl87L8GRSDty1uNA9n+rs+7X PD1zXw+6TLiYDB9jbny2TR455K8eGA9vJ/AqW35PQ9G3HJggasaJ8JYczmgAwI X-IronPort-AV: E=Sophos;i="5.82,270,1613451600"; d="scan'208";a="43332334" From: Andrew Cooper To: Xen-devel CC: Andrew Cooper , Jan Beulich , =?UTF-8?q?Roger=20Pau=20Monn=C3=A9?= , Wei Liu Subject: [PATCH 5/5] x86/cpuid: Fix handling of xsave dynamic leaves Date: Mon, 3 May 2021 16:39:38 +0100 Message-ID: <20210503153938.14109-6-andrew.cooper3@citrix.com> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20210503153938.14109-1-andrew.cooper3@citrix.com> References: <20210503153938.14109-1-andrew.cooper3@citrix.com> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @citrix.com) If max leaf is greater than 0xd but xsave not available to the guest, then = the current XSAVE size should not be filled in. This is a latent bug for now as the guest max leaf is 0xd, but will become problematic in the future. The comment concerning XSS state is wrong. VT-x doesn't manage host/guest state automatically, but there is provision for "host only" bits to be set,= so the implications are still accurate. Introduce {xstate,hw}_compressed_size() helpers to mirror the uncompressed ones. Signed-off-by: Andrew Cooper Reviewed-by: Jan Beulich --- CC: Jan Beulich CC: Roger Pau Monn=C3=A9 CC: Wei Liu --- xen/arch/x86/cpuid.c | 23 +++++++-------------- xen/arch/x86/xstate.c | 49 ++++++++++++++++++++++++++++++++++++++++= ++++ xen/include/asm-x86/xstate.h | 1 + 3 files changed, 57 insertions(+), 16 deletions(-) diff --git a/xen/arch/x86/cpuid.c b/xen/arch/x86/cpuid.c index c7f8388e5d..92745aa63f 100644 --- a/xen/arch/x86/cpuid.c +++ b/xen/arch/x86/cpuid.c @@ -1041,24 +1041,15 @@ void guest_cpuid(const struct vcpu *v, uint32_t lea= f, case XSTATE_CPUID: switch ( subleaf ) { + case 0: + if ( p->basic.xsave ) + res->b =3D xstate_uncompressed_size(v->arch.xcr0); + break; + case 1: if ( p->xstate.xsaves ) - { - /* - * TODO: Figure out what to do for XSS state. VT-x manages - * host vs guest MSR_XSS automatically, so as soon as we s= tart - * supporting any XSS states, the wrong XSS will be in - * context. - */ - BUILD_BUG_ON(XSTATE_XSAVES_ONLY !=3D 0); - - /* - * Read CPUID[0xD,0/1].EBX from hardware. They vary with - * enabled XSTATE, and appropraite XCR0|XSS are in context. - */ - case 0: - res->b =3D cpuid_count_ebx(leaf, subleaf); - } + res->b =3D xstate_compressed_size(v->arch.xcr0 | + v->arch.msrs->xss.raw); break; } break; diff --git a/xen/arch/x86/xstate.c b/xen/arch/x86/xstate.c index d4c01da574..03489f0cf4 100644 --- a/xen/arch/x86/xstate.c +++ b/xen/arch/x86/xstate.c @@ -602,6 +602,55 @@ unsigned int xstate_uncompressed_size(uint64_t xcr0) return size; } =20 +static unsigned int hw_compressed_size(uint64_t xstates) +{ + uint64_t curr_xcr0 =3D get_xcr0(), curr_xss =3D get_msr_xss(); + unsigned int size; + bool ok; + + ok =3D set_xcr0(xstates & ~XSTATE_XSAVES_ONLY); + ASSERT(ok); + set_msr_xss(xstates & XSTATE_XSAVES_ONLY); + + size =3D cpuid_count_ebx(XSTATE_CPUID, 1); + + ok =3D set_xcr0(curr_xcr0); + ASSERT(ok); + set_msr_xss(curr_xss); + + return size; +} + +unsigned int xstate_compressed_size(uint64_t xstates) +{ + unsigned int i, size =3D XSTATE_AREA_MIN_SIZE; + + xstates &=3D ~XSTATE_FP_SSE; + for_each_set_bit ( i, &xstates, 63 ) + { + if ( test_bit(i, &xstate_align) ) + size =3D ROUNDUP(size, 64); + + size +=3D xstate_sizes[i]; + } + + /* In debug builds, cross-check our calculation with hardware. */ + if ( IS_ENABLED(CONFIG_DEBUG) ) + { + unsigned int hwsize; + + xstates |=3D XSTATE_FP_SSE; + hwsize =3D hw_compressed_size(xstates); + + if ( size !=3D hwsize ) + printk_once(XENLOG_ERR "%s(%#"PRIx64") size %#x !=3D hwsize %#= x\n", + __func__, xstates, size, hwsize); + size =3D hwsize; + } + + return size; +} + /* Collect the information of processor's extended state */ void xstate_init(struct cpuinfo_x86 *c) { diff --git a/xen/include/asm-x86/xstate.h b/xen/include/asm-x86/xstate.h index 02d6f171b8..ecf7bbc5cd 100644 --- a/xen/include/asm-x86/xstate.h +++ b/xen/include/asm-x86/xstate.h @@ -108,6 +108,7 @@ void xstate_free_save_area(struct vcpu *v); int xstate_alloc_save_area(struct vcpu *v); void xstate_init(struct cpuinfo_x86 *c); unsigned int xstate_uncompressed_size(uint64_t xcr0); +unsigned int xstate_compressed_size(uint64_t states); =20 static inline uint64_t xgetbv(unsigned int index) { --=20 2.11.0