From nobody Sat Nov 23 09:53:55 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 header.i=ngoc-tu.dinh@vates.tech; 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=quarantine dis=none) header.from=vates.tech ARC-Seal: i=1; a=rsa-sha256; t=1731919797; cv=none; d=zohomail.com; s=zohoarc; b=OIAxWgOklui30DIZjRIQt89Vwc4D7znxOHQBFTefYLIr7ImdEONWpirkyTbny/KjPcskptGxZQlcjBDakt7Z47o5hV7rm3+1PJPlaGFRvrMHvR4WnVpSRKsDeLd2Gm5S+dh4B0xvmeMMdZO96QfH22qkP36lSxEvOiPaF5ba/4c= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1731919797; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=r0skW4u++Ca1L9bVDD6J0qBy4MGffGcnZfM3V9qqDH4=; b=bDOfraCu/hjKn6Fts/BFzm0BQ3H6b62U+X5JFG9ndCqXh85CxQO8Ye6q0kEq7RHWMGOeyiEtLvfuztk4AEnJd0AEjp2TwKWj2yGkDEuQbpGEmrtdxNj995mM6P0GEBHSPD234VxvFk1QsQd1dDWrjqK7D9Vn5fZJXCNOLQgulKQ= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass header.i=ngoc-tu.dinh@vates.tech; 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=quarantine dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1731919797759355.1737602738365; Mon, 18 Nov 2024 00:49:57 -0800 (PST) Received: from list by lists.xenproject.org with outflank-mailman.839117.1254936 (Exim 4.92) (envelope-from ) id 1tCxRx-0005XH-TX; Mon, 18 Nov 2024 08:49:37 +0000 Received: by outflank-mailman (output) from mailman id 839117.1254936; Mon, 18 Nov 2024 08:49:37 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1tCxRx-0005VK-N5; Mon, 18 Nov 2024 08:49:37 +0000 Received: by outflank-mailman (input) for mailman id 839117; Mon, 18 Nov 2024 08:49:36 +0000 Received: from se1-gles-flk1-in.inumbo.com ([94.247.172.50] helo=se1-gles-flk1.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1tCxRw-0005D3-Nn for xen-devel@lists.xenproject.org; Mon, 18 Nov 2024 08:49:36 +0000 Received: from mail136-26.atl41.mandrillapp.com (mail136-26.atl41.mandrillapp.com [198.2.136.26]) by se1-gles-flk1.inumbo.com (Halon) with ESMTPS id 063fdedf-a58a-11ef-99a3-01e77a169b0f; Mon, 18 Nov 2024 09:49:30 +0100 (CET) Received: from pmta11.mandrill.prod.atl01.rsglab.com (localhost [127.0.0.1]) by mail136-26.atl41.mandrillapp.com (Mailchimp) with ESMTP id 4XsLsP1tG0zJKF3ZY for ; Mon, 18 Nov 2024 08:49:29 +0000 (GMT) Received: from [37.26.189.201] by mandrillapp.com id b69a6f8cf02f44949a0860350e2ff77a; Mon, 18 Nov 2024 08:49:29 +0000 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: 063fdedf-a58a-11ef-99a3-01e77a169b0f X-Custom-Connection: eyJyZW1vdGVpcCI6IjE5OC4yLjEzNi4yNiIsImhlbG8iOiJtYWlsMTM2LTI2LmF0bDQxLm1hbmRyaWxsYXBwLmNvbSJ9 X-Custom-Transaction: eyJpZCI6IjA2M2ZkZWRmLWE1OGEtMTFlZi05OWEzLTAxZTc3YTE2OWIwZiIsInRzIjoxNzMxOTE5NzcxLjA3NzE4LCJzZW5kZXIiOiJib3VuY2UtbWRfMzA1MDQ5NjIuNjczYWZmOTkudjEtYjY5YTZmOGNmMDJmNDQ5NDlhMDg2MDM1MGUyZmY3N2FAYm91bmNlLnZhdGVzLnRlY2giLCJyZWNpcGllbnQiOiJ4ZW4tZGV2ZWxAbGlzdHMueGVucHJvamVjdC5vcmcifQ== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=mandrillapp.com; s=mte1; t=1731919769; x=1732180269; bh=r0skW4u++Ca1L9bVDD6J0qBy4MGffGcnZfM3V9qqDH4=; h=From:Subject:To:Cc:Message-Id:In-Reply-To:References:Feedback-ID: Date:MIME-Version:Content-Type:Content-Transfer-Encoding:CC:Date: Subject:From; b=CUSe+Ir4k3tMdpAj/Dd/Rbeja0nVh2sGHOLkPgH0I6pObb/A2ojIVqpI155JNEJTN mu9kMzEHdWGdxgy7vhEHQdoM8XK0pvqKw/dvWDvmhiB+ke4cmT7cBtwYEBBeG6jL+9 hnsQyXfjzwsx1oy2ZpI5Ywx21CKQfYS+JciuVP3DetsCiz6BBlNxOBRsLm+/DoECri rvZgw4bmGQuARrTy5TN3G/PdjMNJs44ljPN20UcIPgtdIbhDGRBFRMWpjyCWrOdkvb cn5Wrno7RFSCYaTrYb6WUrydRvQClvcwctR6Olh7tm1eP2MA+1ovCdsPG3SYtGuc2q mVlvtwq1h7yog== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=vates.tech; s=mte1; t=1731919769; x=1732180269; i=ngoc-tu.dinh@vates.tech; bh=r0skW4u++Ca1L9bVDD6J0qBy4MGffGcnZfM3V9qqDH4=; h=From:Subject:To:Cc:Message-Id:In-Reply-To:References:Feedback-ID: Date:MIME-Version:Content-Type:Content-Transfer-Encoding:CC:Date: Subject:From; b=I1Rjv4A+RIHq82t//DCkW/wl3q9n90J8I02rPWxgjhLlT59UALbxKz9A43kr80FA3 Hg0OUCVD9eCSMhwtc0a0MjEHQMfhZG6CQZCPwuYEbEVz7iNljd81OCf6VrQoqzN8LI 06Nla8fgKOgO6aLi2vLdjSv1mTyZlidpU0NMM8pPde1sJFFRAVop22d3jFcxX+bTYI maEi5WBjZgjhYYgVv0ig2F9rBy+bQT14gSlSFZH3k1cU51m4zLT1HzzQYwnzCz47f1 a0Y/shOW4MrLYai7/Pos21hMaxK8jpmI6xu8fnKzZAi2DEfPSixhRNL2DBb6xcA4wU UWkAPaImYTWfA== From: ngoc-tu.dinh@vates.tech Subject: =?utf-8?Q?[PATCH=201/4]=20x86:=20Add=20Intel=20architectural=20LBR=20featureset=20bits?= X-Mailer: git-send-email 2.43.0 X-Bm-Disclaimer: Yes X-Bm-Milter-Handled: 4ffbd6c1-ee69-4e1b-aabd-f977039bd3e2 X-Bm-Transport-Timestamp: 1731919768581 To: xen-devel@lists.xenproject.org Cc: "Tu Dinh" Message-Id: <20241118084914.22268-2-ngoc-tu.dinh@vates.tech> In-Reply-To: <20241118084914.22268-1-ngoc-tu.dinh@vates.tech> References: <20241118084914.22268-1-ngoc-tu.dinh@vates.tech> X-Native-Encoded: 1 X-Report-Abuse: =?UTF-8?Q?Please=20forward=20a=20copy=20of=20this=20message,=20including=20all=20headers,=20to=20abuse@mandrill.com.=20You=20can=20also=20report=20abuse=20here:=20https://mandrillapp.com/contact/abuse=3Fid=3D30504962.b69a6f8cf02f44949a0860350e2ff77a?= X-Mandrill-User: md_30504962 Feedback-ID: 30504962:30504962.20241118:md Date: Mon, 18 Nov 2024 08:49:29 +0000 MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity ngoc-tu.dinh@vates.tech) (identity @mandrillapp.com) X-ZM-MESSAGEID: 1731919799595116600 Content-Type: text/plain; charset="utf-8" From: Tu Dinh Expose ARCH_LBR feature to guests. Extend CPU featureset with 3 words for CPUID leaf 0x1c. Signed-off-by: Tu Dinh --- tools/libs/guest/xg_cpuid_x86.c | 2 +- tools/misc/xen-cpuid.c | 3 ++ xen/arch/x86/cpu-policy.c | 3 ++ xen/arch/x86/cpu/common.c | 7 ++++ xen/include/public/arch-x86/cpufeatureset.h | 28 ++++++++++++++- xen/include/xen/lib/x86/cpu-policy.h | 38 ++++++++++++++++++++- xen/lib/x86/cpuid.c | 6 ++++ 7 files changed, 84 insertions(+), 3 deletions(-) diff --git a/tools/libs/guest/xg_cpuid_x86.c b/tools/libs/guest/xg_cpuid_x8= 6.c index 4453178100..64d9baa538 100644 --- a/tools/libs/guest/xg_cpuid_x86.c +++ b/tools/libs/guest/xg_cpuid_x86.c @@ -656,7 +656,7 @@ int xc_cpuid_apply_policy(xc_interface *xch, uint32_t d= omid, bool restore, p->policy.feat.mpx =3D test_bit(X86_FEATURE_MPX, host_features= et); } =20 - p->policy.basic.max_leaf =3D min(p->policy.basic.max_leaf, 0xdu); + p->policy.basic.max_leaf =3D min(p->policy.basic.max_leaf, 0x1cu); p->policy.feat.max_subleaf =3D 0; p->policy.extd.max_leaf =3D min(p->policy.extd.max_leaf, 0x8000001= c); } diff --git a/tools/misc/xen-cpuid.c b/tools/misc/xen-cpuid.c index 4c4593528d..4f0fb0a6ea 100644 --- a/tools/misc/xen-cpuid.c +++ b/tools/misc/xen-cpuid.c @@ -37,6 +37,9 @@ static const struct { { "CPUID 0x00000007:1.edx", "7d1" }, { "MSR_ARCH_CAPS.lo", "m10Al" }, { "MSR_ARCH_CAPS.hi", "m10Ah" }, + { "CPUID 0x0000001c.eax", "1Ca" }, + { "CPUID 0x0000001c.ebx", "1Cb" }, + { "CPUID 0x0000001c.ecx", "1Cc" }, }; =20 #define COL_ALIGN "24" diff --git a/xen/arch/x86/cpu-policy.c b/xen/arch/x86/cpu-policy.c index 78bc9872b0..cf6b212fb6 100644 --- a/xen/arch/x86/cpu-policy.c +++ b/xen/arch/x86/cpu-policy.c @@ -271,6 +271,8 @@ static void recalculate_misc(struct cpu_policy *p) =20 p->basic.raw[0xc] =3D EMPTY_LEAF; =20 + zero_leaves(p->basic.raw, 0xe, 0x1b); + p->extd.e1d &=3D ~CPUID_COMMON_1D_FEATURES; =20 /* Most of Power/RAS hidden from guests. */ @@ -303,6 +305,7 @@ static void recalculate_misc(struct cpu_policy *p) zero_leaves(p->basic.raw, 0x2, 0x3); memset(p->cache.raw, 0, sizeof(p->cache.raw)); zero_leaves(p->basic.raw, 0x9, 0xa); + p->basic.raw[0x1c] =3D EMPTY_LEAF; =20 p->extd.vendor_ebx =3D p->basic.vendor_ebx; p->extd.vendor_ecx =3D p->basic.vendor_ecx; diff --git a/xen/arch/x86/cpu/common.c b/xen/arch/x86/cpu/common.c index 067d855bad..4c8eb188e9 100644 --- a/xen/arch/x86/cpu/common.c +++ b/xen/arch/x86/cpu/common.c @@ -505,6 +505,13 @@ static void generic_identify(struct cpuinfo_x86 *c) &c->x86_capability[FEATURESET_Da1], &tmp, &tmp, &tmp); =20 + if (c->cpuid_level >=3D 0x1c) + cpuid(0x1c, + &c->x86_capability[FEATURESET_1Ca], + &c->x86_capability[FEATURESET_1Cb], + &c->x86_capability[FEATURESET_1Cc], + &tmp); + if (test_bit(X86_FEATURE_ARCH_CAPS, c->x86_capability)) rdmsr(MSR_ARCH_CAPABILITIES, c->x86_capability[FEATURESET_m10Al], diff --git a/xen/include/public/arch-x86/cpufeatureset.h b/xen/include/publ= ic/arch-x86/cpufeatureset.h index 8fa3fb711a..9304856fba 100644 --- a/xen/include/public/arch-x86/cpufeatureset.h +++ b/xen/include/public/arch-x86/cpufeatureset.h @@ -284,7 +284,7 @@ XEN_CPUFEATURE(SERIALIZE, 9*32+14) /*A SERIALIZE i= nsn */ XEN_CPUFEATURE(HYBRID, 9*32+15) /* Heterogeneous platform */ XEN_CPUFEATURE(TSXLDTRK, 9*32+16) /*a TSX load tracking suspend/resu= me insns */ XEN_CPUFEATURE(PCONFIG, 9*32+18) /* PCONFIG instruction */ -XEN_CPUFEATURE(ARCH_LBR, 9*32+19) /* Architectural Last Branch Reco= rd */ +XEN_CPUFEATURE(ARCH_LBR, 9*32+19) /*S Architectural Last Branch Reco= rd */ XEN_CPUFEATURE(CET_IBT, 9*32+20) /* CET - Indirect Branch Tracking= */ XEN_CPUFEATURE(AMX_BF16, 9*32+22) /* AMX BFloat16 instruction */ XEN_CPUFEATURE(AVX512_FP16, 9*32+23) /*A AVX512 FP16 instructions */ @@ -379,6 +379,32 @@ XEN_CPUFEATURE(RFDS_CLEAR, 16*32+28) /*!A| Reg= ister File(s) cleared by V =20 /* Intel-defined CPU features, MSR_ARCH_CAPS 0x10a.edx, word 17 */ =20 +/* Intel-defined CPU features, CPUID level 0x0000001c.eax, word 18 */ +XEN_CPUFEATURE(LBR_DEPTH_8, 18*32+ 0) /*S Depth 8 */ +XEN_CPUFEATURE(LBR_DEPTH_16, 18*32+ 1) /*S Depth 16 */ +XEN_CPUFEATURE(LBR_DEPTH_24, 18*32+ 2) /*S Depth 24 */ +XEN_CPUFEATURE(LBR_DEPTH_32, 18*32+ 3) /*S Depth 32 */ +XEN_CPUFEATURE(LBR_DEPTH_40, 18*32+ 4) /*S Depth 40 */ +XEN_CPUFEATURE(LBR_DEPTH_48, 18*32+ 5) /*S Depth 48 */ +XEN_CPUFEATURE(LBR_DEPTH_56, 18*32+ 6) /*S Depth 56 */ +XEN_CPUFEATURE(LBR_DEPTH_64, 18*32+ 7) /*S Depth 64 */ +XEN_CPUFEATURE(LBR_DCST_RST, 18*32+30) /*S Deep C-state reset */ +XEN_CPUFEATURE(LBR_LIP, 18*32+31) /*S IP is linear IP */ + +/* Intel-defined CPU features, CPUID level 0x0000001c.ebx, word 19 */ +XEN_CPUFEATURE(LBR_CPL_FILTER, 19*32+ 0) /*S CPL filtering */ +XEN_CPUFEATURE(LBR_BR_FILTER, 19*32+ 1) /*S Branch filtering */ +XEN_CPUFEATURE(LBR_CALL_STACK_MODE, 19*32+ 2) /*S Call stack mode */ + +/* Intel-defined CPU features, CPUID level 0x0000001c.ecx, word 20 */ +XEN_CPUFEATURE(LBR_MISPRED, 20*32+ 0) /*S Mispredict mode */ +XEN_CPUFEATURE(LBR_TIMED, 20*32+ 1) /*S Timed mode */ +XEN_CPUFEATURE(LBR_BR_TYPE, 20*32+ 2) /*S Branch type */ +XEN_CPUFEATURE(LBR_EVT_FMT0, 20*32+16) /*!S Event type format bit 0 = */ +XEN_CPUFEATURE(LBR_EVT_FMT1, 20*32+17) /*!S Event type format bit 1 = */ +XEN_CPUFEATURE(LBR_EVT_FMT2, 20*32+18) /*!S Event type format bit 2 = */ +XEN_CPUFEATURE(LBR_EVT_FMT3, 20*32+19) /*!S Event type format bit 3 = */ + #endif /* XEN_CPUFEATURE */ =20 /* Clean up from a default include. Close the enum (for C). */ diff --git a/xen/include/xen/lib/x86/cpu-policy.h b/xen/include/xen/lib/x86= /cpu-policy.h index f43e1a3b21..afd66b4158 100644 --- a/xen/include/xen/lib/x86/cpu-policy.h +++ b/xen/include/xen/lib/x86/cpu-policy.h @@ -22,6 +22,9 @@ #define FEATURESET_7d1 15 /* 0x00000007:1.edx */ #define FEATURESET_m10Al 16 /* 0x0000010a.eax */ #define FEATURESET_m10Ah 17 /* 0x0000010a.edx */ +#define FEATURESET_1Ca 18 /* 0x0000001c.eax */ +#define FEATURESET_1Cb 19 /* 0x0000001c.ebx */ +#define FEATURESET_1Cc 20 /* 0x0000001c.ecx */ =20 struct cpuid_leaf { @@ -85,7 +88,7 @@ unsigned int x86_cpuid_lookup_vendor(uint32_t ebx, uint32= _t ecx, uint32_t edx); */ const char *x86_cpuid_vendor_to_str(unsigned int vendor); =20 -#define CPUID_GUEST_NR_BASIC (0xdu + 1) +#define CPUID_GUEST_NR_BASIC (0x1cu + 1) #define CPUID_GUEST_NR_CACHE (5u + 1) #define CPUID_GUEST_NR_FEAT (2u + 1) #define CPUID_GUEST_NR_TOPO (1u + 1) @@ -158,6 +161,39 @@ struct cpu_policy uint64_t :64, :64; /* Leaf 0xb - Topology. */ uint64_t :64, :64; /* Leaf 0xc - rsvd */ uint64_t :64, :64; /* Leaf 0xd - XSTATE. */ + + uint64_t :64, :64; /* Leaf 0xe - rsvd */ + uint64_t :64, :64; /* Leaf 0xf - rsvd */ + uint64_t :64, :64; /* Leaf 0x10 - rsvd */ + uint64_t :64, :64; /* Leaf 0x11 - rsvd */ + uint64_t :64, :64; /* Leaf 0x12 - rsvd */ + uint64_t :64, :64; /* Leaf 0x13 - rsvd */ + uint64_t :64, :64; /* Leaf 0x14 - rsvd */ + uint64_t :64, :64; /* Leaf 0x15 - rsvd */ + uint64_t :64, :64; /* Leaf 0x16 - rsvd */ + uint64_t :64, :64; /* Leaf 0x17 - rsvd */ + uint64_t :64, :64; /* Leaf 0x18 - rsvd */ + uint64_t :64, :64; /* Leaf 0x19 - rsvd */ + uint64_t :64, :64; /* Leaf 0x1a - rsvd */ + uint64_t :64, :64; /* Leaf 0x1b - rsvd */ + + union { + uint32_t _1Ca; + struct { + uint32_t supported_depths:8; + uint32_t :22; + uint32_t deep_cstate_reset:1; + uint32_t ip_contains_lip:1; + }; + }; + union { + uint32_t _1Cb; + struct { DECL_BITFIELD(1Cb); }; + }; + union { + uint32_t _1Cc; + struct { DECL_BITFIELD(1Cc); }; + }; }; } basic; =20 diff --git a/xen/lib/x86/cpuid.c b/xen/lib/x86/cpuid.c index eb7698dc73..4d19349b17 100644 --- a/xen/lib/x86/cpuid.c +++ b/xen/lib/x86/cpuid.c @@ -81,6 +81,9 @@ void x86_cpu_policy_to_featureset( fs[FEATURESET_7d1] =3D p->feat._7d1; fs[FEATURESET_m10Al] =3D p->arch_caps.lo; fs[FEATURESET_m10Ah] =3D p->arch_caps.hi; + fs[FEATURESET_1Ca] =3D p->basic._1Ca; + fs[FEATURESET_1Cb] =3D p->basic._1Cb; + fs[FEATURESET_1Cc] =3D p->basic._1Cc; } =20 void x86_cpu_featureset_to_policy( @@ -104,6 +107,9 @@ void x86_cpu_featureset_to_policy( p->feat._7d1 =3D fs[FEATURESET_7d1]; p->arch_caps.lo =3D fs[FEATURESET_m10Al]; p->arch_caps.hi =3D fs[FEATURESET_m10Ah]; + p->basic._1Ca =3D fs[FEATURESET_1Ca]; + p->basic._1Cb =3D fs[FEATURESET_1Cb]; + p->basic._1Cc =3D fs[FEATURESET_1Cc]; } =20 void x86_cpu_policy_recalc_synth(struct cpu_policy *p) --=20 2.43.0 Ngoc Tu Dinh | Vates XCP-ng Developer XCP-ng & Xen Orchestra - Vates solutions web: https://vates.tech From nobody Sat Nov 23 09:53:55 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 header.i=ngoc-tu.dinh@vates.tech; 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=quarantine dis=none) header.from=vates.tech ARC-Seal: i=1; a=rsa-sha256; t=1731919801; cv=none; d=zohomail.com; s=zohoarc; b=HhN3KznzShk9cOn+wL1Poo4e57UW2KFYyhTlJpVitkULPXCm2vsu6sDwdzHglmsqQ5Pb6LSHy8E+JP0mfz+oYBkhAupsnMvpFc1Qofsn2CWzqcdTFAOEdEHZvb0Z8SwQ2t0Vvw49fTpWO57h6Nhe44vrGSNfzbrwc1jJuxjcmJE= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1731919801; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=OHbPZUdflYInf3pACcvlIdJaYJKwR1NVIIEYSmzoDXE=; b=bhG/wV9IGnB1ty/+Lf+T+vDq/OCcu/tFbwBC/grrJ/sk71HTZvkv6i+8A5wVlRp1TvYf5H+EJ7ueYKp1mLPSfpRVFhl0bjF/afkIgEw5c/Q2sxcPa3but6j4IRRLCjygO1DpE0nPLG5X9wztMAXenZK8XqpGKPCrOystfheS+AQ= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass header.i=ngoc-tu.dinh@vates.tech; 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=quarantine dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1731919801714353.89411276401347; Mon, 18 Nov 2024 00:50:01 -0800 (PST) Received: from list by lists.xenproject.org with outflank-mailman.839115.1254923 (Exim 4.92) (envelope-from ) id 1tCxRx-0005H2-8H; Mon, 18 Nov 2024 08:49:37 +0000 Received: by outflank-mailman (output) from mailman id 839115.1254923; Mon, 18 Nov 2024 08:49:37 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1tCxRx-0005FZ-0T; Mon, 18 Nov 2024 08:49:37 +0000 Received: by outflank-mailman (input) for mailman id 839115; Mon, 18 Nov 2024 08:49:35 +0000 Received: from se1-gles-flk1-in.inumbo.com ([94.247.172.50] helo=se1-gles-flk1.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1tCxRv-0005D3-2Z for xen-devel@lists.xenproject.org; Mon, 18 Nov 2024 08:49:35 +0000 Received: from mail180-8.suw31.mandrillapp.com (mail180-8.suw31.mandrillapp.com [198.2.180.8]) by se1-gles-flk1.inumbo.com (Halon) with ESMTPS id 061e5204-a58a-11ef-99a3-01e77a169b0f; Mon, 18 Nov 2024 09:49:30 +0100 (CET) Received: from pmta11.mandrill.prod.suw01.rsglab.com (localhost [127.0.0.1]) by mail180-8.suw31.mandrillapp.com (Mailchimp) with ESMTP id 4XsLsP1JPtz3sNTdQ for ; Mon, 18 Nov 2024 08:49:29 +0000 (GMT) Received: from [37.26.189.201] by mandrillapp.com id 5969053c717d4bfb859f5f7a4154c65f; Mon, 18 Nov 2024 08:49:29 +0000 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: 061e5204-a58a-11ef-99a3-01e77a169b0f X-Custom-Connection: eyJyZW1vdGVpcCI6IjE5OC4yLjE4MC44IiwiaGVsbyI6Im1haWwxODAtOC5zdXczMS5tYW5kcmlsbGFwcC5jb20ifQ== X-Custom-Transaction: eyJpZCI6IjA2MWU1MjA0LWE1OGEtMTFlZi05OWEzLTAxZTc3YTE2OWIwZiIsInRzIjoxNzMxOTE5NzcwLjc0ODE5Niwic2VuZGVyIjoiYm91bmNlLW1kXzMwNTA0OTYyLjY3M2FmZjk5LnYxLTU5NjkwNTNjNzE3ZDRiZmI4NTlmNWY3YTQxNTRjNjVmQGJvdW5jZS52YXRlcy50ZWNoIiwicmVjaXBpZW50IjoieGVuLWRldmVsQGxpc3RzLnhlbnByb2plY3Qub3JnIn0= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=mandrillapp.com; s=mte1; t=1731919769; x=1732180269; bh=OHbPZUdflYInf3pACcvlIdJaYJKwR1NVIIEYSmzoDXE=; h=From:Subject:To:Cc:Message-Id:In-Reply-To:References:Feedback-ID: Date:MIME-Version:Content-Type:Content-Transfer-Encoding:CC:Date: Subject:From; b=IkMzdBlK5IyGcizR/Wre563ZV3WIhZujNCHxh5ZW+zM4psW0C5Gxb0Wd4N0tGhPy1 oE9JzC9y/F/5FUxS1Kj1E4OwNWMvn7MzYpJBfTdmloLJtpah9bIMVps/B/91jxZiPj nItJp68f3bFyx4Rzo7jv3sOjV1WggGueqrrBm7yTDBt4Fkri05y6kDU/tlIYIeeoVa a+5ofvkvbJWrnCtC9b9tZXOmO+sMKEdF2hb60lJZpV4h1a52XYq+uxmROTd9541PWj S2hn3+6U7AAbisGtcVLJNX65Uy41UBx01jFzgw2M8qESTDzaATIMYlixUa9C/eRF6X SlRRoLTiXGnCA== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=vates.tech; s=mte1; t=1731919769; x=1732180269; i=ngoc-tu.dinh@vates.tech; bh=OHbPZUdflYInf3pACcvlIdJaYJKwR1NVIIEYSmzoDXE=; h=From:Subject:To:Cc:Message-Id:In-Reply-To:References:Feedback-ID: Date:MIME-Version:Content-Type:Content-Transfer-Encoding:CC:Date: Subject:From; b=0q5lwBBXvABTG7aqF6HlA5nNdc7EXhJF+ILJay9j09VfEXZLTidj2SCqsMOwkyRtO GeZe2A1c90XGuPy0mSpMTF/SzOiZ8dl9TrmrNiNiKRYNnqPRmlhLO5vJvhNEDjWQCb 0L08ZQzY+2wsPxg4QbwtNiIuMcWVPFVAd4yblizRbD0jlQpajj/SYEwMnS8Lor2uN1 +BhGqXqVtjcaF+vdYd9OuoWUwTWPDgSxPjDoogU1J5swfUbmWn3Z51t7D8sBmBI+4T p8sg3qVjOFZpENO8QAR4zRBBqb7rfVNLQbcSu6vPAJF247yBgnlDM9JnC73jHS2YlF cU0mYKSAJcX+A== From: ngoc-tu.dinh@vates.tech Subject: =?utf-8?Q?[PATCH=202/4]=20x86:=20Add=20architectural=20LBR=20declarations?= X-Mailer: git-send-email 2.43.0 X-Bm-Disclaimer: Yes X-Bm-Milter-Handled: 4ffbd6c1-ee69-4e1b-aabd-f977039bd3e2 X-Bm-Transport-Timestamp: 1731919768728 To: xen-devel@lists.xenproject.org Cc: "Tu Dinh" Message-Id: <20241118084914.22268-3-ngoc-tu.dinh@vates.tech> In-Reply-To: <20241118084914.22268-1-ngoc-tu.dinh@vates.tech> References: <20241118084914.22268-1-ngoc-tu.dinh@vates.tech> X-Native-Encoded: 1 X-Report-Abuse: =?UTF-8?Q?Please=20forward=20a=20copy=20of=20this=20message,=20including=20all=20headers,=20to=20abuse@mandrill.com.=20You=20can=20also=20report=20abuse=20here:=20https://mandrillapp.com/contact/abuse=3Fid=3D30504962.5969053c717d4bfb859f5f7a4154c65f?= X-Mandrill-User: md_30504962 Feedback-ID: 30504962:30504962.20241118:md Date: Mon, 18 Nov 2024 08:49:29 +0000 MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @mandrillapp.com) (identity ngoc-tu.dinh@vates.tech) X-ZM-MESSAGEID: 1731919803529116600 Content-Type: text/plain; charset="utf-8" From: Tu Dinh Signed-off-by: Tu Dinh --- xen/arch/x86/include/asm/msr-index.h | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/xen/arch/x86/include/asm/msr-index.h b/xen/arch/x86/include/as= m/msr-index.h index 9cdb5b2625..867deab3c6 100644 --- a/xen/arch/x86/include/asm/msr-index.h +++ b/xen/arch/x86/include/asm/msr-index.h @@ -304,6 +304,17 @@ #define MSR_IA32_LASTINTFROMIP 0x000001dd #define MSR_IA32_LASTINTTOIP 0x000001de =20 +/* Architectural LBR state MSRs */ +#define MSR_IA32_LASTBRANCH_CTL 0x000014ce +#define LASTBRANCH_CTL_LBREN (1<<0) /* Enable LBR recording */ +#define LASTBRANCH_CTL_VALID _AC(0x7f000f, ULL) +#define MSR_IA32_LASTBRANCH_DEPTH 0x000014cf +#define MSR_IA32_LER_INFO 0x000001e0 +#define MSR_IA32_LASTBRANCH_0_INFO 0x00001200 +#define MSR_IA32_LASTBRANCH_0_FROM_IP 0x00001500 +#define MSR_IA32_LASTBRANCH_0_TO_IP 0x00001600 +#define MAX_MSR_ARCH_LASTBRANCH_FROM_TO 64 + #define MSR_IA32_POWER_CTL 0x000001fc =20 #define MSR_IA32_MTRR_PHYSBASE(n) (0x00000200 + 2 * (n)) --=20 2.43.0 Ngoc Tu Dinh | Vates XCP-ng Developer XCP-ng & Xen Orchestra - Vates solutions web: https://vates.tech From nobody Sat Nov 23 09:53:55 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 header.i=ngoc-tu.dinh@vates.tech; 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=quarantine dis=none) header.from=vates.tech ARC-Seal: i=1; a=rsa-sha256; t=1731919798; cv=none; d=zohomail.com; s=zohoarc; b=NG8celDnO4Qk7hdEASCr7vVeZulw5bu8DjcTVEjtXpMHuinWJJcRuOKgvkEVwuwVIQF3Y2mknul/65SqaXk+jENu+AaTPj3viUYuaXwoA/JHSoo6/uKdl6G1ZVDNHTMUQhoPGWDqJvRkWyFBiLzXHcNShyoUy7SMFUi1TtSOkVg= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1731919798; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=1vvA+0VSIAC8nQ8VY1SX3ncz60BGkrkcakZs3orNshc=; b=IslYkeKrX+H2HK30cKqN8qsg4A1i2RcVLhlXwY9FhcD4pJnahHZLfC+qkw6vdnxqYeeCl8KVahrzfTejlZZKHWsCMcUL4ZSX+ZOj8Q3ACgFkFjKwaVrxmkZFSonlvHR9uXHLFvclUwNvASK5JFdZftOjoECghJAUNgpx1JAgJl4= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass header.i=ngoc-tu.dinh@vates.tech; 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=quarantine dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1731919798427756.7116098976628; Mon, 18 Nov 2024 00:49:58 -0800 (PST) Received: from list by lists.xenproject.org with outflank-mailman.839114.1254916 (Exim 4.92) (envelope-from ) id 1tCxRw-0005DZ-TI; Mon, 18 Nov 2024 08:49:36 +0000 Received: by outflank-mailman (output) from mailman id 839114.1254916; Mon, 18 Nov 2024 08:49:36 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1tCxRw-0005DS-PU; Mon, 18 Nov 2024 08:49:36 +0000 Received: by outflank-mailman (input) for mailman id 839114; Mon, 18 Nov 2024 08:49:34 +0000 Received: from se1-gles-sth1-in.inumbo.com ([159.253.27.254] helo=se1-gles-sth1.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1tCxRu-0005D2-MX for xen-devel@lists.xenproject.org; Mon, 18 Nov 2024 08:49:34 +0000 Received: from mail180-8.suw31.mandrillapp.com (mail180-8.suw31.mandrillapp.com [198.2.180.8]) by se1-gles-sth1.inumbo.com (Halon) with ESMTPS id 063f0764-a58a-11ef-a0c8-8be0dac302b0; Mon, 18 Nov 2024 09:49:30 +0100 (CET) Received: from pmta11.mandrill.prod.suw01.rsglab.com (localhost [127.0.0.1]) by mail180-8.suw31.mandrillapp.com (Mailchimp) with ESMTP id 4XsLsP61FMz3sNN6f for ; Mon, 18 Nov 2024 08:49:29 +0000 (GMT) Received: from [37.26.189.201] by mandrillapp.com id 8f6556b918f44805a2f1fdef3b3f8a34; Mon, 18 Nov 2024 08:49:29 +0000 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: 063f0764-a58a-11ef-a0c8-8be0dac302b0 X-Custom-Connection: eyJyZW1vdGVpcCI6IjE5OC4yLjE4MC44IiwiaGVsbyI6Im1haWwxODAtOC5zdXczMS5tYW5kcmlsbGFwcC5jb20ifQ== X-Custom-Transaction: eyJpZCI6IjA2M2YwNzY0LWE1OGEtMTFlZi1hMGM4LThiZTBkYWMzMDJiMCIsInRzIjoxNzMxOTE5NzcxLjEzMDc0NSwic2VuZGVyIjoiYm91bmNlLW1kXzMwNTA0OTYyLjY3M2FmZjk5LnYxLThmNjU1NmI5MThmNDQ4MDVhMmYxZmRlZjNiM2Y4YTM0QGJvdW5jZS52YXRlcy50ZWNoIiwicmVjaXBpZW50IjoieGVuLWRldmVsQGxpc3RzLnhlbnByb2plY3Qub3JnIn0= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=mandrillapp.com; s=mte1; t=1731919769; x=1732180269; bh=1vvA+0VSIAC8nQ8VY1SX3ncz60BGkrkcakZs3orNshc=; h=From:Subject:To:Cc:Message-Id:In-Reply-To:References:Feedback-ID: Date:MIME-Version:Content-Type:Content-Transfer-Encoding:CC:Date: Subject:From; b=f/Q8M/9F9PGC6dOfh17uwvqf16ENb8scYDqCfgQuRaWcld2VTZ/s/aVOLLS20ykda fGmYgaSVDOMsqNEvFppx7A3OAJ4BywwSHkVrNMg5jKd8MYjR9vXTf+EnNbWItGRzvS Cl6KfXEEg5ADqMHO0u1siXx5l37hi7HYazhJ4wE01QhmejKh+F3erPNqBGd9Bb2eMT lGzQStE4kSgM6MY5p1S0M0oFHxtMA1rGBibEnd5raPNIF341S2ftQoq/rGxG+QGxnD ltziP7TfyTa0CdGYgKHPvWUyBge2pBXpzM8Ud3kZFM1v6U4nVOXivFMfamGEPrCbZ3 1y1DoHo/KMkdg== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=vates.tech; s=mte1; t=1731919769; x=1732180269; i=ngoc-tu.dinh@vates.tech; bh=1vvA+0VSIAC8nQ8VY1SX3ncz60BGkrkcakZs3orNshc=; h=From:Subject:To:Cc:Message-Id:In-Reply-To:References:Feedback-ID: Date:MIME-Version:Content-Type:Content-Transfer-Encoding:CC:Date: Subject:From; b=zb/cnjvGLubBrj0nPtEIWXP6DjRr91YCpGBVEG6t7l2XxeoBmt7gU2NAyV3SQnVEL HmvYXcuiLIP/MUlV2YHApX0lduyDok9ZR8uqoUKfRQwK5yXfFpI/AXaNcL+KXh+6x8 S3Kn3iD5qiXrv+OW/y75MaeeCigNlyASYj7yaDXn11ZnMoizS4l07eHtH0lmdJADgX Ubu8OJF7jg7RSPe/wTz8Eamzj0R/2uTyPVGDD0Aiy8CKWP15gIhj3rEFEpZGu5wLLD LmsE+eRgGfHQ1FTGqc/eTguYrLHkauRm8XhupVVIRFt4V3R4l+g5bClwuuSeKCeo/h juLYOwLTMTm+A== From: ngoc-tu.dinh@vates.tech Subject: =?utf-8?Q?[PATCH=203/4]=20x86:=20Adjust=20arch=20LBR=20CPU=20policy?= X-Mailer: git-send-email 2.43.0 X-Bm-Disclaimer: Yes X-Bm-Milter-Handled: 4ffbd6c1-ee69-4e1b-aabd-f977039bd3e2 X-Bm-Transport-Timestamp: 1731919768866 To: xen-devel@lists.xenproject.org Cc: "Tu Dinh" Message-Id: <20241118084914.22268-4-ngoc-tu.dinh@vates.tech> In-Reply-To: <20241118084914.22268-1-ngoc-tu.dinh@vates.tech> References: <20241118084914.22268-1-ngoc-tu.dinh@vates.tech> X-Native-Encoded: 1 X-Report-Abuse: =?UTF-8?Q?Please=20forward=20a=20copy=20of=20this=20message,=20including=20all=20headers,=20to=20abuse@mandrill.com.=20You=20can=20also=20report=20abuse=20here:=20https://mandrillapp.com/contact/abuse=3Fid=3D30504962.8f6556b918f44805a2f1fdef3b3f8a34?= X-Mandrill-User: md_30504962 Feedback-ID: 30504962:30504962.20241118:md Date: Mon, 18 Nov 2024 08:49:29 +0000 MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @mandrillapp.com) (identity ngoc-tu.dinh@vates.tech) X-ZM-MESSAGEID: 1731919799632116600 Content-Type: text/plain; charset="utf-8" From: Tu Dinh Allow virtual arch LBR with a single depth that's equal to that of the host. If this is not possible, disable arch LBR altogether. Signed-off-by: Tu Dinh --- xen/arch/x86/cpu-policy.c | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/xen/arch/x86/cpu-policy.c b/xen/arch/x86/cpu-policy.c index cf6b212fb6..2ac76eb058 100644 --- a/xen/arch/x86/cpu-policy.c +++ b/xen/arch/x86/cpu-policy.c @@ -638,6 +638,36 @@ static void __init calculate_pv_max_policy(void) p->extd.raw[0xa] =3D EMPTY_LEAF; /* No SVM for PV guests. */ } =20 +/* + * Allow virtual arch LBR with a single depth that's equal to that of the + * host. If this is not possible, disable arch LBR altogether. + */ +static void adjust_arch_lbr_depth(uint32_t fs[FEATURESET_NR_ENTRIES]) +{ + uint64_t host_lbr_depth; + bool lbr_supported =3D true; + + rdmsrl(MSR_IA32_LASTBRANCH_DEPTH, host_lbr_depth); + if ((host_lbr_depth =3D=3D 0) || + (host_lbr_depth % 8) || + (host_lbr_depth > 64)) + lbr_supported =3D false; + + host_lbr_depth =3D 1ul << ((host_lbr_depth / 8) - 1); + if ((host_lbr_depth & fs[FEATURESET_1Ca] & 0xff) =3D=3D 0) + lbr_supported =3D false; + + if (lbr_supported) + { + fs[FEATURESET_1Ca] =3D (fs[FEATURESET_1Ca] & ~0xffu) | host_lbr_de= pth; + } + else + { + __clear_bit(X86_FEATURE_ARCH_LBR, fs); + fs[FEATURESET_1Ca] =3D fs[FEATURESET_1Cb] =3D fs[FEATURESET_1Cc] = =3D 0; + } +} + static void __init calculate_pv_def_policy(void) { struct cpu_policy *p =3D &pv_def_cpu_policy; @@ -760,6 +790,9 @@ static void __init calculate_hvm_max_policy(void) __clear_bit(X86_FEATURE_XSAVES, fs); } =20 + if ( test_bit(X86_FEATURE_ARCH_LBR, fs) ) + adjust_arch_lbr_depth(fs); + /* * Xen doesn't use PKS, so the guest support for it has opted to not u= se * the VMCS load/save controls for efficiency reasons. This depends on --=20 2.43.0 Ngoc Tu Dinh | Vates XCP-ng Developer XCP-ng & Xen Orchestra - Vates solutions web: https://vates.tech From nobody Sat Nov 23 09:53:55 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 header.i=ngoc-tu.dinh@vates.tech; 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=quarantine dis=none) header.from=vates.tech ARC-Seal: i=1; a=rsa-sha256; t=1731919802; cv=none; d=zohomail.com; s=zohoarc; b=gaMqbfPzoTr5MnSXOVvNsKwNDu01ZbG9JL1pqY2ULCPySPVGBAEFrRJn8TPlIXhEev26tca+ywCV3HDvfU/TOfRm4FB+zvIO0kI/QDmqyffrJULpfHzP/XzhBnKspLJashqYyXAQ8a1EsHb3DrOT4/rGJfuuNfgdwzTc5HMQTvE= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1731919802; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=jQsMIdFlhvKVHyeb3pcbSs0+jZjMBHrB3oPFmg+d7hA=; b=c59FInb5MRIQpfz9m/h+G9w5gMR+8ouMdP+fJQq0b37KAdXiqyReEFYIOmdOYRC/WRAvoLSyHi01Hz52ifHKesS4sDBbJjhx4HPiJ4AKK32MLunRqBe6LWToCueSvd2m700HkPcOdfz2I/poJi4RHJECZf8ebmoIVsIdzpriSUU= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass header.i=ngoc-tu.dinh@vates.tech; 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=quarantine dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 17319198024471018.583607932822; Mon, 18 Nov 2024 00:50:02 -0800 (PST) Received: from list by lists.xenproject.org with outflank-mailman.839118.1254956 (Exim 4.92) (envelope-from ) id 1tCxS1-0006B9-1v; Mon, 18 Nov 2024 08:49:41 +0000 Received: by outflank-mailman (output) from mailman id 839118.1254956; Mon, 18 Nov 2024 08:49:41 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1tCxS0-0006B4-UT; Mon, 18 Nov 2024 08:49:40 +0000 Received: by outflank-mailman (input) for mailman id 839118; Mon, 18 Nov 2024 08:49:39 +0000 Received: from se1-gles-flk1-in.inumbo.com ([94.247.172.50] helo=se1-gles-flk1.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1tCxRz-0005D3-FH for xen-devel@lists.xenproject.org; Mon, 18 Nov 2024 08:49:39 +0000 Received: from mail136-26.atl41.mandrillapp.com (mail136-26.atl41.mandrillapp.com [198.2.136.26]) by se1-gles-flk1.inumbo.com (Halon) with ESMTPS id 0904e8da-a58a-11ef-99a3-01e77a169b0f; Mon, 18 Nov 2024 09:49:35 +0100 (CET) Received: from pmta11.mandrill.prod.atl01.rsglab.com (localhost [127.0.0.1]) by mail136-26.atl41.mandrillapp.com (Mailchimp) with ESMTP id 4XsLsQ1YgyzJKF3Zk for ; Mon, 18 Nov 2024 08:49:30 +0000 (GMT) Received: from [37.26.189.201] by mandrillapp.com id 6f3315d8734646bca3549145d22f803a; Mon, 18 Nov 2024 08:49:30 +0000 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: 0904e8da-a58a-11ef-99a3-01e77a169b0f X-Custom-Connection: eyJyZW1vdGVpcCI6IjE5OC4yLjEzNi4yNiIsImhlbG8iOiJtYWlsMTM2LTI2LmF0bDQxLm1hbmRyaWxsYXBwLmNvbSJ9 X-Custom-Transaction: eyJpZCI6IjA5MDRlOGRhLWE1OGEtMTFlZi05OWEzLTAxZTc3YTE2OWIwZiIsInRzIjoxNzMxOTE5Nzc1LjY3NDU0NCwic2VuZGVyIjoiYm91bmNlLW1kXzMwNTA0OTYyLjY3M2FmZjlhLnYxLTZmMzMxNWQ4NzM0NjQ2YmNhMzU0OTE0NWQyMmY4MDNhQGJvdW5jZS52YXRlcy50ZWNoIiwicmVjaXBpZW50IjoieGVuLWRldmVsQGxpc3RzLnhlbnByb2plY3Qub3JnIn0= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=mandrillapp.com; s=mte1; t=1731919770; x=1732180270; bh=jQsMIdFlhvKVHyeb3pcbSs0+jZjMBHrB3oPFmg+d7hA=; h=From:Subject:To:Cc:Message-Id:In-Reply-To:References:Feedback-ID: Date:MIME-Version:Content-Type:Content-Transfer-Encoding:CC:Date: Subject:From; b=B2BS6yCKc3UQ9TtjZditDUEb4c26+eAeP+macp0aqdsMI6kG9wC041qCvitirGoVr mOt3/Th8GwKN16K7tWwwPcMVnKzCd3UmuSd5pvSvd46OqwWuvx+G4em1+s9THkP9nv 9ET1TK8gWU76eS3Ckf3OcjTts36NoNGFnSDRFF25xW8rzhX+vh1cVybW4G2hGp5gXa fPho6sNbF9fFAk2O0PxTOBZydF/0JkRIc1Ryuxg7iIqEv1Az/Q2cMGu6oV998myk/B 994zdLBNaDN1xeWxI7A3oElneFm/mALssU/8F3Gry8Fi+jbIhb80rIDrL1XTkW6i1F juZc+3wI+88Gg== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=vates.tech; s=mte1; t=1731919770; x=1732180270; i=ngoc-tu.dinh@vates.tech; bh=jQsMIdFlhvKVHyeb3pcbSs0+jZjMBHrB3oPFmg+d7hA=; h=From:Subject:To:Cc:Message-Id:In-Reply-To:References:Feedback-ID: Date:MIME-Version:Content-Type:Content-Transfer-Encoding:CC:Date: Subject:From; b=d2IKE3PLiO71PgI1Nr5BdvHpaFXKEqZipsvlFV/kjGx2YXSraExqN38KYcmiYWKvF GltDqabLk3zJeV2cak0SJ2a8ReQQWqi56Xc8xMT8AbZplvNJZtfET6TCsea26fD6UT 1hHHyZB+OtORqbMLvKpmS3J9yg9R42+E6LqIOhboWLSs9puVDAUzyuBfXMt0JsXWnA PwLQkeKf5AcjL3Zpdhas74QXnbeZUKiq9ANOUuv7Z/vR/zvzBpLeRA7idZ11e/JA/m Yhjo5iRXySGEY0PrU65/UqsIFdRTOvk0sainQ9klrkDmMfBx2O64B7eeSWvq88qDlZ LcqXRblPkKNLg== From: ngoc-tu.dinh@vates.tech Subject: =?utf-8?Q?[PATCH=204/4]=20x86/vmx:=20Virtualize=20architectural=20LBRs?= X-Mailer: git-send-email 2.43.0 X-Bm-Disclaimer: Yes X-Bm-Milter-Handled: 4ffbd6c1-ee69-4e1b-aabd-f977039bd3e2 X-Bm-Transport-Timestamp: 1731919769554 To: xen-devel@lists.xenproject.org Cc: "Tu Dinh" Message-Id: <20241118084914.22268-5-ngoc-tu.dinh@vates.tech> In-Reply-To: <20241118084914.22268-1-ngoc-tu.dinh@vates.tech> References: <20241118084914.22268-1-ngoc-tu.dinh@vates.tech> X-Native-Encoded: 1 X-Report-Abuse: =?UTF-8?Q?Please=20forward=20a=20copy=20of=20this=20message,=20including=20all=20headers,=20to=20abuse@mandrill.com.=20You=20can=20also=20report=20abuse=20here:=20https://mandrillapp.com/contact/abuse=3Fid=3D30504962.6f3315d8734646bca3549145d22f803a?= X-Mandrill-User: md_30504962 Feedback-ID: 30504962:30504962.20241118:md Date: Mon, 18 Nov 2024 08:49:30 +0000 MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @mandrillapp.com) (identity ngoc-tu.dinh@vates.tech) X-ZM-MESSAGEID: 1731919803810116600 Content-Type: text/plain; charset="utf-8" From: Tu Dinh Virtual architectural LBRs work in guest mode only, using the "load guest IA32_LBR_CTL" and "clear IA32_LBR_CTL" VMX controls. Intercept writes to MSR_IA32_LASTBRANCH_{CTL,DEPTH} to inject LBR MSRs into guest. MSR_IA32_LASTBRANCH_DEPTH is only allowed to be equal to that of the host's. Signed-off-by: Tu Dinh --- xen/arch/x86/cpu-policy.c | 3 + xen/arch/x86/hvm/vmx/vmcs.c | 11 +- xen/arch/x86/hvm/vmx/vmx.c | 269 +++++++++++++++++------- xen/arch/x86/include/asm/hvm/vmx/vmcs.h | 8 + 4 files changed, 211 insertions(+), 80 deletions(-) diff --git a/xen/arch/x86/cpu-policy.c b/xen/arch/x86/cpu-policy.c index 2ac76eb058..9e78273a79 100644 --- a/xen/arch/x86/cpu-policy.c +++ b/xen/arch/x86/cpu-policy.c @@ -788,6 +788,9 @@ static void __init calculate_hvm_max_policy(void) =20 if ( !cpu_has_vmx_xsaves ) __clear_bit(X86_FEATURE_XSAVES, fs); + + if ( !cpu_has_vmx_guest_lbr_ctl ) + __clear_bit(X86_FEATURE_ARCH_LBR, fs); } =20 if ( test_bit(X86_FEATURE_ARCH_LBR, fs) ) diff --git a/xen/arch/x86/hvm/vmx/vmcs.c b/xen/arch/x86/hvm/vmx/vmcs.c index 147e998371..a16daad78a 100644 --- a/xen/arch/x86/hvm/vmx/vmcs.c +++ b/xen/arch/x86/hvm/vmx/vmcs.c @@ -203,6 +203,7 @@ static void __init vmx_display_features(void) P(cpu_has_vmx_bus_lock_detection, "Bus Lock Detection"); P(cpu_has_vmx_notify_vm_exiting, "Notify VM Exit"); P(cpu_has_vmx_virt_spec_ctrl, "Virtualize SPEC_CTRL"); + P(cpu_has_vmx_guest_lbr_ctl, "Architectural LBR virtualization"); #undef P =20 if ( !printed ) @@ -448,7 +449,8 @@ static int vmx_init_vmcs_config(bool bsp) =20 min =3D VM_EXIT_ACK_INTR_ON_EXIT; opt =3D (VM_EXIT_SAVE_GUEST_PAT | VM_EXIT_LOAD_HOST_PAT | - VM_EXIT_LOAD_HOST_EFER | VM_EXIT_CLEAR_BNDCFGS); + VM_EXIT_LOAD_HOST_EFER | VM_EXIT_CLEAR_BNDCFGS | + VM_EXIT_CLEAR_GUEST_LBR_CTL); min |=3D VM_EXIT_IA32E_MODE; _vmx_vmexit_control =3D adjust_vmx_controls( "VMExit Control", min, opt, MSR_IA32_VMX_EXIT_CTLS, &mismatch); @@ -489,7 +491,7 @@ static int vmx_init_vmcs_config(bool bsp) =20 min =3D 0; opt =3D (VM_ENTRY_LOAD_GUEST_PAT | VM_ENTRY_LOAD_GUEST_EFER | - VM_ENTRY_LOAD_BNDCFGS); + VM_ENTRY_LOAD_BNDCFGS | VM_ENTRY_LOAD_GUEST_LBR_CTL); _vmx_vmentry_control =3D adjust_vmx_controls( "VMEntry Control", min, opt, MSR_IA32_VMX_ENTRY_CTLS, &mismatch); =20 @@ -1329,6 +1331,9 @@ static int construct_vmcs(struct vcpu *v) | (paging_mode_hap(d) ? 0 : (1U << X86_EXC_PF)) | (v->arch.fully_eager_fpu ? 0 : (1U << X86_EXC_NM)); =20 + if ( cpu_has_vmx_guest_lbr_ctl ) + __vmwrite(GUEST_LBR_CTL, 0); + if ( cpu_has_vmx_notify_vm_exiting ) __vmwrite(NOTIFY_WINDOW, vm_notify_window); =20 @@ -2087,6 +2092,8 @@ void vmcs_dump_vcpu(struct vcpu *v) vmr32(GUEST_PREEMPTION_TIMER), vmr32(GUEST_SMBASE)); printk("DebugCtl =3D 0x%016lx DebugExceptions =3D 0x%016lx\n", vmr(GUEST_IA32_DEBUGCTL), vmr(GUEST_PENDING_DBG_EXCEPTIONS)); + if ( vmentry_ctl & VM_ENTRY_LOAD_GUEST_LBR_CTL ) + printk("LbrCtl =3D 0x%016lx\n", vmr(GUEST_LBR_CTL)); if ( vmentry_ctl & (VM_ENTRY_LOAD_PERF_GLOBAL_CTRL | VM_ENTRY_LOAD_BND= CFGS) ) printk("PerfGlobCtl =3D 0x%016lx BndCfgS =3D 0x%016lx\n", vmr(GUEST_PERF_GLOBAL_CTRL), vmr(GUEST_BNDCFGS)); diff --git a/xen/arch/x86/hvm/vmx/vmx.c b/xen/arch/x86/hvm/vmx/vmx.c index b6885d0e27..d417ae17d3 100644 --- a/xen/arch/x86/hvm/vmx/vmx.c +++ b/xen/arch/x86/hvm/vmx/vmx.c @@ -423,65 +423,96 @@ static int cf_check vmx_pi_update_irte(const struct v= cpu *v, return rc; } =20 -static const struct lbr_info { +struct lbr_info { u32 base, count; -} p4_lbr[] =3D { - { MSR_P4_LER_FROM_LIP, 1 }, - { MSR_P4_LER_TO_LIP, 1 }, - { MSR_P4_LASTBRANCH_TOS, 1 }, - { MSR_P4_LASTBRANCH_0_FROM_LIP, NUM_MSR_P4_LASTBRANCH_FROM_TO }, - { MSR_P4_LASTBRANCH_0_TO_LIP, NUM_MSR_P4_LASTBRANCH_FROM_TO }, - { 0, 0 } + u64 initial; +}; + +static const struct lbr_info p4_lbr[] =3D { + { MSR_P4_LER_FROM_LIP, 1, 0 }, + { MSR_P4_LER_TO_LIP, 1, 0 }, + { MSR_P4_LASTBRANCH_TOS, 1, 0 }, + { MSR_P4_LASTBRANCH_0_FROM_LIP, NUM_MSR_P4_LASTBRANCH_FROM_TO, 0 }, + { MSR_P4_LASTBRANCH_0_TO_LIP, NUM_MSR_P4_LASTBRANCH_FROM_TO, 0 }, + { 0, 0, 0 } }, c2_lbr[] =3D { - { MSR_IA32_LASTINTFROMIP, 1 }, - { MSR_IA32_LASTINTTOIP, 1 }, - { MSR_C2_LASTBRANCH_TOS, 1 }, - { MSR_C2_LASTBRANCH_0_FROM_IP, NUM_MSR_C2_LASTBRANCH_FROM_TO }, - { MSR_C2_LASTBRANCH_0_TO_IP, NUM_MSR_C2_LASTBRANCH_FROM_TO }, - { 0, 0 } + { MSR_IA32_LASTINTFROMIP, 1, 0 }, + { MSR_IA32_LASTINTTOIP, 1, 0 }, + { MSR_C2_LASTBRANCH_TOS, 1, 0 }, + { MSR_C2_LASTBRANCH_0_FROM_IP, NUM_MSR_C2_LASTBRANCH_FROM_TO, 0 }, + { MSR_C2_LASTBRANCH_0_TO_IP, NUM_MSR_C2_LASTBRANCH_FROM_TO, 0 }, + { 0, 0, 0 } }, nh_lbr[] =3D { - { MSR_IA32_LASTINTFROMIP, 1 }, - { MSR_IA32_LASTINTTOIP, 1 }, - { MSR_NHL_LBR_SELECT, 1 }, - { MSR_NHL_LASTBRANCH_TOS, 1 }, - { MSR_P4_LASTBRANCH_0_FROM_LIP, NUM_MSR_P4_LASTBRANCH_FROM_TO }, - { MSR_P4_LASTBRANCH_0_TO_LIP, NUM_MSR_P4_LASTBRANCH_FROM_TO }, - { 0, 0 } + { MSR_IA32_LASTINTFROMIP, 1, 0 }, + { MSR_IA32_LASTINTTOIP, 1, 0 }, + { MSR_NHL_LBR_SELECT, 1, 0 }, + { MSR_NHL_LASTBRANCH_TOS, 1, 0 }, + { MSR_P4_LASTBRANCH_0_FROM_LIP, NUM_MSR_P4_LASTBRANCH_FROM_TO, 0 }, + { MSR_P4_LASTBRANCH_0_TO_LIP, NUM_MSR_P4_LASTBRANCH_FROM_TO, 0 }, + { 0, 0, 0 } }, sk_lbr[] =3D { - { MSR_IA32_LASTINTFROMIP, 1 }, - { MSR_IA32_LASTINTTOIP, 1 }, - { MSR_NHL_LBR_SELECT, 1 }, - { MSR_NHL_LASTBRANCH_TOS, 1 }, - { MSR_SKL_LASTBRANCH_0_FROM_IP, NUM_MSR_SKL_LASTBRANCH }, - { MSR_SKL_LASTBRANCH_0_TO_IP, NUM_MSR_SKL_LASTBRANCH }, - { MSR_SKL_LASTBRANCH_0_INFO, NUM_MSR_SKL_LASTBRANCH }, - { 0, 0 } + { MSR_IA32_LASTINTFROMIP, 1, 0 }, + { MSR_IA32_LASTINTTOIP, 1, 0 }, + { MSR_NHL_LBR_SELECT, 1, 0 }, + { MSR_NHL_LASTBRANCH_TOS, 1, 0 }, + { MSR_SKL_LASTBRANCH_0_FROM_IP, NUM_MSR_SKL_LASTBRANCH, 0 }, + { MSR_SKL_LASTBRANCH_0_TO_IP, NUM_MSR_SKL_LASTBRANCH, 0 }, + { MSR_SKL_LASTBRANCH_0_INFO, NUM_MSR_SKL_LASTBRANCH, 0 }, + { 0, 0, 0 } }, at_lbr[] =3D { - { MSR_IA32_LASTINTFROMIP, 1 }, - { MSR_IA32_LASTINTTOIP, 1 }, - { MSR_C2_LASTBRANCH_TOS, 1 }, - { MSR_C2_LASTBRANCH_0_FROM_IP, NUM_MSR_ATOM_LASTBRANCH_FROM_TO }, - { MSR_C2_LASTBRANCH_0_TO_IP, NUM_MSR_ATOM_LASTBRANCH_FROM_TO }, - { 0, 0 } + { MSR_IA32_LASTINTFROMIP, 1, 0 }, + { MSR_IA32_LASTINTTOIP, 1, 0 }, + { MSR_C2_LASTBRANCH_TOS, 1, 0 }, + { MSR_C2_LASTBRANCH_0_FROM_IP, NUM_MSR_ATOM_LASTBRANCH_FROM_TO, 0 }, + { MSR_C2_LASTBRANCH_0_TO_IP, NUM_MSR_ATOM_LASTBRANCH_FROM_TO, 0 }, + { 0, 0, 0 } }, sm_lbr[] =3D { - { MSR_IA32_LASTINTFROMIP, 1 }, - { MSR_IA32_LASTINTTOIP, 1 }, - { MSR_SM_LBR_SELECT, 1 }, - { MSR_SM_LASTBRANCH_TOS, 1 }, - { MSR_C2_LASTBRANCH_0_FROM_IP, NUM_MSR_ATOM_LASTBRANCH_FROM_TO }, - { MSR_C2_LASTBRANCH_0_TO_IP, NUM_MSR_ATOM_LASTBRANCH_FROM_TO }, - { 0, 0 } + { MSR_IA32_LASTINTFROMIP, 1, 0 }, + { MSR_IA32_LASTINTTOIP, 1, 0 }, + { MSR_SM_LBR_SELECT, 1, 0 }, + { MSR_SM_LASTBRANCH_TOS, 1, 0 }, + { MSR_C2_LASTBRANCH_0_FROM_IP, NUM_MSR_ATOM_LASTBRANCH_FROM_TO, 0 }, + { MSR_C2_LASTBRANCH_0_TO_IP, NUM_MSR_ATOM_LASTBRANCH_FROM_TO, 0 }, + { 0, 0, 0 } }, gm_lbr[] =3D { - { MSR_IA32_LASTINTFROMIP, 1 }, - { MSR_IA32_LASTINTTOIP, 1 }, - { MSR_SM_LBR_SELECT, 1 }, - { MSR_SM_LASTBRANCH_TOS, 1 }, - { MSR_GM_LASTBRANCH_0_FROM_IP, NUM_MSR_GM_LASTBRANCH_FROM_TO }, - { MSR_GM_LASTBRANCH_0_TO_IP, NUM_MSR_GM_LASTBRANCH_FROM_TO }, - { 0, 0 } + { MSR_IA32_LASTINTFROMIP, 1, 0 }, + { MSR_IA32_LASTINTTOIP, 1, 0 }, + { MSR_SM_LBR_SELECT, 1, 0 }, + { MSR_SM_LASTBRANCH_TOS, 1, 0 }, + { MSR_GM_LASTBRANCH_0_FROM_IP, NUM_MSR_GM_LASTBRANCH_FROM_TO, 0 }, + { MSR_GM_LASTBRANCH_0_TO_IP, NUM_MSR_GM_LASTBRANCH_FROM_TO, 0 }, + { 0, 0, 0 } }; static const struct lbr_info *__ro_after_init model_specific_lbr; =20 +static struct lbr_info __ro_after_init architectural_lbr[] =3D { + { MSR_IA32_LASTINTFROMIP, 1, 0 }, + { MSR_IA32_LASTINTTOIP, 1, 0 }, + { MSR_IA32_LER_INFO, 1, 0 }, + /* to be updated by update_arch_lbr */ + { MSR_IA32_LASTBRANCH_0_INFO, MAX_MSR_ARCH_LASTBRANCH_FROM_TO, 0 }, + { MSR_IA32_LASTBRANCH_0_FROM_IP, MAX_MSR_ARCH_LASTBRANCH_FROM_TO, 0 }, + { MSR_IA32_LASTBRANCH_0_TO_IP, MAX_MSR_ARCH_LASTBRANCH_FROM_TO, 0 }, + { 0, 0, 0 } +}; +static uint64_t __ro_after_init host_lbr_depth =3D 0; + +static void __init update_arch_lbr(void) +{ + struct lbr_info *lbr =3D architectural_lbr; + + if ( boot_cpu_has(X86_FEATURE_ARCH_LBR) ) + rdmsrl(MSR_IA32_LASTBRANCH_DEPTH, host_lbr_depth); + ASSERT((host_lbr_depth % 8) =3D=3D 0 && (host_lbr_depth <=3D 64)); + + for ( ; lbr->count; lbr++ ) { + if ( lbr->base =3D=3D MSR_IA32_LASTBRANCH_0_INFO || + lbr->base =3D=3D MSR_IA32_LASTBRANCH_0_FROM_IP || + lbr->base =3D=3D MSR_IA32_LASTBRANCH_0_TO_IP ) + lbr->count =3D (u32)host_lbr_depth; + } +} + static const struct lbr_info *__init get_model_specific_lbr(void) { switch ( boot_cpu_data.x86 ) @@ -3056,6 +3087,8 @@ const struct hvm_function_table * __init start_vmx(vo= id) lbr_tsx_fixup_check(); ler_to_fixup_check(); =20 + update_arch_lbr(); + return &vmx_function_table; } =20 @@ -3303,25 +3336,36 @@ static void __init ler_to_fixup_check(void) } } =20 -static int is_last_branch_msr(u32 ecx) +static const struct lbr_info * find_last_branch_msr(struct vcpu *v, u32 ec= x) { + /* + * Model-specific and architectural LBRs are mutually exclusive. + * It's not necessary to check both lbr_info lists. + */ const struct lbr_info *lbr =3D model_specific_lbr; + const struct cpu_policy *cp =3D v->domain->arch.cpu_policy; =20 - if ( lbr =3D=3D NULL ) - return 0; + if ( lbr =3D=3D NULL ) { + if ( cp->feat.arch_lbr ) + lbr =3D architectural_lbr; + else + return NULL; + } =20 for ( ; lbr->count; lbr++ ) if ( (ecx >=3D lbr->base) && (ecx < (lbr->base + lbr->count)) ) - return 1; + return lbr; =20 - return 0; + return NULL; } =20 static int cf_check vmx_msr_read_intercept( unsigned int msr, uint64_t *msr_content) { struct vcpu *curr =3D current; + const struct cpu_policy *cp =3D curr->domain->arch.cpu_policy; uint64_t tmp; + const struct lbr_info *lbr =3D NULL; =20 HVM_DBG_LOG(DBG_LEVEL_MSR, "ecx=3D%#x", msr); =20 @@ -3369,6 +3413,18 @@ static int cf_check vmx_msr_read_intercept( __vmread(GUEST_IA32_DEBUGCTL, msr_content); break; =20 + case MSR_IA32_LASTBRANCH_CTL: + if ( !cp->feat.arch_lbr ) + goto gp_fault; + __vmread(GUEST_LBR_CTL, msr_content); + break; + + case MSR_IA32_LASTBRANCH_DEPTH: + if ( !cp->feat.arch_lbr ) + goto gp_fault; + *msr_content =3D host_lbr_depth; + break; + case MSR_IA32_VMX_BASIC...MSR_IA32_VMX_VMFUNC: if ( !nvmx_msr_read_intercept(msr, msr_content) ) goto gp_fault; @@ -3397,9 +3453,10 @@ static int cf_check vmx_msr_read_intercept( if ( vmx_read_guest_msr(curr, msr, msr_content) =3D=3D 0 ) break; =20 - if ( is_last_branch_msr(msr) ) + lbr =3D find_last_branch_msr(curr, msr); + if ( lbr !=3D NULL ) { - *msr_content =3D 0; + *msr_content =3D lbr->initial; break; } =20 @@ -3540,6 +3597,34 @@ void cf_check vmx_vlapic_msr_changed(struct vcpu *v) vmx_vmcs_exit(v); } =20 +static int vmx_lbr_insert(struct vcpu *v, const struct lbr_info *lbr) +{ + for ( ; lbr->count; lbr++ ) + { + unsigned int i; + + for ( i =3D 0; i < lbr->count; i++ ) + { + int rc =3D vmx_add_guest_msr(v, lbr->base + i, lbr->initial); + + if ( unlikely(rc) ) + { + return rc; + } + + vmx_clear_msr_intercept(v, lbr->base + i, VMX_MSR_RW); + } + } + + v->arch.hvm.vmx.lbr_flags |=3D LBR_MSRS_INSERTED; + if ( lbr_tsx_fixup_needed ) + v->arch.hvm.vmx.lbr_flags |=3D LBR_FIXUP_TSX; + if ( ler_to_fixup_needed ) + v->arch.hvm.vmx.lbr_flags |=3D LBR_FIXUP_LER_TO; + + return 0; +} + static int cf_check vmx_msr_write_intercept( unsigned int msr, uint64_t msr_content) { @@ -3652,36 +3737,64 @@ static int cf_check vmx_msr_write_intercept( if ( !(v->arch.hvm.vmx.lbr_flags & LBR_MSRS_INSERTED) && (msr_content & IA32_DEBUGCTLMSR_LBR) ) { - const struct lbr_info *lbr =3D model_specific_lbr; + int rc =3D vmx_lbr_insert(v, model_specific_lbr); =20 - for ( ; lbr->count; lbr++ ) + if ( unlikely(rc) ) { - unsigned int i; + gprintk(XENLOG_ERR, + "Guest load/save list error %d\n", rc); + domain_crash(v->domain); + return X86EMUL_OKAY; + } + } =20 - for ( i =3D 0; i < lbr->count; i++ ) - { - int rc =3D vmx_add_guest_msr(v, lbr->base + i, 0); + __vmwrite(GUEST_IA32_DEBUGCTL, msr_content); + break; =20 - if ( unlikely(rc) ) - { - gprintk(XENLOG_ERR, - "Guest load/save list error %d\n", rc); - domain_crash(v->domain); - return X86EMUL_OKAY; - } + case MSR_IA32_LASTBRANCH_CTL: + if ( !cp->feat.arch_lbr ) + goto gp_fault; =20 - vmx_clear_msr_intercept(v, lbr->base + i, VMX_MSR_RW); - } + if ( msr_content & ~LASTBRANCH_CTL_VALID ) + goto gp_fault; + + if ( !(v->arch.hvm.vmx.lbr_flags & LBR_MSRS_INSERTED) && + (msr_content & LASTBRANCH_CTL_LBREN) ) + { + int rc; + + rc =3D vmx_lbr_insert(v, architectural_lbr); + if ( unlikely(rc) ) + { + gprintk(XENLOG_ERR, + "Guest load/save list error %d\n", rc); + domain_crash(v->domain); + return X86EMUL_OKAY; } + } + + __vmwrite(GUEST_LBR_CTL, msr_content); + break; =20 - v->arch.hvm.vmx.lbr_flags |=3D LBR_MSRS_INSERTED; - if ( lbr_tsx_fixup_needed ) - v->arch.hvm.vmx.lbr_flags |=3D LBR_FIXUP_TSX; - if ( ler_to_fixup_needed ) - v->arch.hvm.vmx.lbr_flags |=3D LBR_FIXUP_LER_TO; + case MSR_IA32_LASTBRANCH_DEPTH: + if ( !cp->feat.arch_lbr ) + goto gp_fault; + + if ( msr_content !=3D host_lbr_depth ) + goto gp_fault; + + if ( v->arch.hvm.vmx.lbr_flags & LBR_MSRS_INSERTED ) + { + /* writes to MSR_IA32_LASTBRANCH_DEPTH zero LBR state */ + int i; + for (i =3D 0; i < host_lbr_depth; i++) + { + vmx_write_guest_msr(v, MSR_IA32_LASTBRANCH_0_INFO + i, 0); + vmx_write_guest_msr(v, MSR_IA32_LASTBRANCH_0_FROM_IP + i, = 0); + vmx_write_guest_msr(v, MSR_IA32_LASTBRANCH_0_TO_IP + i, 0); + } } =20 - __vmwrite(GUEST_IA32_DEBUGCTL, msr_content); break; =20 case MSR_IA32_MISC_ENABLE: @@ -3710,7 +3823,7 @@ static int cf_check vmx_msr_write_intercept( return X86EMUL_OKAY; =20 if ( vmx_write_guest_msr(v, msr, msr_content) =3D=3D 0 || - is_last_branch_msr(msr) ) + find_last_branch_msr(v, msr) !=3D NULL ) break; =20 if ( v->domain->arch.msr_relaxed && !rdmsr_safe(msr, msr_content) ) diff --git a/xen/arch/x86/include/asm/hvm/vmx/vmcs.h b/xen/arch/x86/include= /asm/hvm/vmx/vmcs.h index 939b87eb50..2b265e583f 100644 --- a/xen/arch/x86/include/asm/hvm/vmx/vmcs.h +++ b/xen/arch/x86/include/asm/hvm/vmx/vmcs.h @@ -229,6 +229,7 @@ extern u32 vmx_pin_based_exec_control; #define VM_EXIT_LOAD_HOST_EFER 0x00200000 #define VM_EXIT_SAVE_PREEMPT_TIMER 0x00400000 #define VM_EXIT_CLEAR_BNDCFGS 0x00800000 +#define VM_EXIT_CLEAR_GUEST_LBR_CTL 0x04000000 extern u32 vmx_vmexit_control; =20 #define VM_ENTRY_IA32E_MODE 0x00000200 @@ -238,6 +239,7 @@ extern u32 vmx_vmexit_control; #define VM_ENTRY_LOAD_GUEST_PAT 0x00004000 #define VM_ENTRY_LOAD_GUEST_EFER 0x00008000 #define VM_ENTRY_LOAD_BNDCFGS 0x00010000 +#define VM_ENTRY_LOAD_GUEST_LBR_CTL 0x00200000 extern u32 vmx_vmentry_control; =20 #define SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES 0x00000001U @@ -391,6 +393,10 @@ extern u64 vmx_ept_vpid_cap; #define cpu_has_vmx_notify_vm_exiting \ (IS_ENABLED(CONFIG_INTEL_VMX) && \ vmx_secondary_exec_control & SECONDARY_EXEC_NOTIFY_VM_EXITING) +#define cpu_has_vmx_guest_lbr_ctl \ + (IS_ENABLED(CONFIG_INTEL_VMX) && \ + (vmx_vmexit_control & VM_EXIT_CLEAR_GUEST_LBR_CTL) && \ + (vmx_vmentry_control & VM_ENTRY_LOAD_GUEST_LBR_CTL)) =20 #define VMCS_RID_TYPE_MASK 0x80000000U =20 @@ -480,6 +486,8 @@ enum vmcs_field { GUEST_PDPTE0 =3D 0x0000280a, #define GUEST_PDPTE(n) (GUEST_PDPTE0 + (n) * 2) /* n =3D 0...3 */ GUEST_BNDCFGS =3D 0x00002812, + GUEST_RTIT_CTL =3D 0x00002814, + GUEST_LBR_CTL =3D 0x00002816, HOST_PAT =3D 0x00002c00, HOST_EFER =3D 0x00002c02, HOST_PERF_GLOBAL_CTRL =3D 0x00002c04, --=20 2.43.0 Ngoc Tu Dinh | Vates XCP-ng Developer XCP-ng & Xen Orchestra - Vates solutions web: https://vates.tech