Skip to contentSkip to conversation

Virtualize architectural LBRs

PATCH Superseded [email protected] 3 participants 18 messages Nov 18, 2024 – Dec 11, 2024

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover Virtualize architectural LBRs

From: Tu Dinh <[email protected]>

Intel model-specific last branch records (LBRs) were replaced by architectural LBRs (see Chapter 20 of Intel SDM volume 3B). This patchset implements virtual LBRs for HVM guests using Intel's "load guest IA32_LBR_CTL" and "clear IA32_LBR_CTL" VMX controls.

Add the necessary CPUID and VMX feature checks into Xen. Note that in this patchset, MSR_IA32_LASTBRANCH_DEPTH is only allowed to be equal to that of the host's.

Tu Dinh (4):
x86: Add Intel architectural LBR featureset bits
x86: Add architectural LBR declarations
x86: Adjust arch LBR CPU policy
x86/vmx: Virtualize architectural LBRs

 tools/libs/guest/xg_cpuid_x86.c             |   2 +-
 tools/misc/xen-cpuid.c                      |   3 +
 xen/arch/x86/cpu-policy.c                   |  39 +++
 xen/arch/x86/cpu/common.c                   |   7 +
 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 +
 xen/arch/x86/include/asm/msr-index.h        |  11 +
 xen/include/public/arch-x86/cpufeatureset.h |  28 +-
 xen/include/xen/lib/x86/cpu-policy.h        |  38 ++-
 xen/lib/x86/cpuid.c                         |   6 +
 11 files changed, 339 insertions(+), 83 deletions(-)
Signature · 9 lines
2.43.0



Ngoc Tu Dinh | Vates XCP-ng Developer

XCP-ng & Xen Orchestra - Vates solutions

web: https://vates.tech
#1 lore raw

PATCH 1/4 x86: Add Intel architectural LBR featureset bits

From: Tu Dinh <[email protected]>

Expose ARCH_LBR feature to guests.

Extend CPU featureset with 3 words for CPUID leaf 0x1c.

---
 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(-)
tools/libs/guest/xg_cpuid_x86.c +1 -1
index 4453178100..64d9baa538 100644
@@ -656,7 +656,7 @@ int xc_cpuid_apply_policy(xc_interface *xch, uint32_t domid, bool restore,
             p->policy.feat.mpx = test_bit(X86_FEATURE_MPX, host_featureset);
         }
 
-        p->policy.basic.max_leaf = min(p->policy.basic.max_leaf, 0xdu);
+        p->policy.basic.max_leaf = min(p->policy.basic.max_leaf, 0x1cu);
         p->policy.feat.max_subleaf = 0;
         p->policy.extd.max_leaf = min(p->policy.extd.max_leaf, 0x8000001c);
     }
tools/misc/xen-cpuid.c +3 -0
index 4c4593528d..4f0fb0a6ea 100644
@@ -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" },
 };
 
 #define COL_ALIGN "24"
xen/arch/x86/cpu-policy.c +3 -0
index 78bc9872b0..cf6b212fb6 100644
@@ -271,6 +271,8 @@ static void recalculate_misc(struct cpu_policy *p)
 
     p->basic.raw[0xc] = EMPTY_LEAF;
 
+    zero_leaves(p->basic.raw, 0xe, 0x1b);
+
     p->extd.e1d &= ~CPUID_COMMON_1D_FEATURES;
 
     /* 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] = EMPTY_LEAF;
 
         p->extd.vendor_ebx = p->basic.vendor_ebx;
         p->extd.vendor_ecx = p->basic.vendor_ecx;
xen/arch/x86/cpu/common.c +7 -0
index 067d855bad..4c8eb188e9 100644
@@ -505,6 +505,13 @@ static void generic_identify(struct cpuinfo_x86 *c)
 			    &c->x86_capability[FEATURESET_Da1],
 			    &tmp, &tmp, &tmp);
 
+	if (c->cpuid_level >= 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],
xen/include/public/arch-x86/cpufeatureset.h +27 -1
index 8fa3fb711a..9304856fba 100644
@@ -284,7 +284,7 @@ XEN_CPUFEATURE(SERIALIZE,     9*32+14) /*A  SERIALIZE insn */
 XEN_CPUFEATURE(HYBRID,        9*32+15) /*   Heterogeneous platform */
 XEN_CPUFEATURE(TSXLDTRK,      9*32+16) /*a  TSX load tracking suspend/resume insns */
 XEN_CPUFEATURE(PCONFIG,       9*32+18) /*   PCONFIG instruction */
-XEN_CPUFEATURE(ARCH_LBR,      9*32+19) /*   Architectural Last Branch Record */
+XEN_CPUFEATURE(ARCH_LBR,      9*32+19) /*S  Architectural Last Branch Record */
 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| Register File(s) cleared by V
 
 /* Intel-defined CPU features, MSR_ARCH_CAPS 0x10a.edx, word 17 */
 
+/* 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 */
 
 /* Clean up from a default include.  Close the enum (for C). */
xen/include/xen/lib/x86/cpu-policy.h +37 -1
index f43e1a3b21..afd66b4158 100644
@@ -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      */
 
 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);
 
-#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;
 
xen/lib/x86/cpuid.c +6 -0
index eb7698dc73..4d19349b17 100644
@@ -81,6 +81,9 @@ void x86_cpu_policy_to_featureset(
     fs[FEATURESET_7d1]       = p->feat._7d1;
     fs[FEATURESET_m10Al]     = p->arch_caps.lo;
     fs[FEATURESET_m10Ah]     = p->arch_caps.hi;
+    fs[FEATURESET_1Ca]       = p->basic._1Ca;
+    fs[FEATURESET_1Cb]       = p->basic._1Cb;
+    fs[FEATURESET_1Cc]       = p->basic._1Cc;
 }
 
 void x86_cpu_featureset_to_policy(
@@ -104,6 +107,9 @@ void x86_cpu_featureset_to_policy(
     p->feat._7d1             = fs[FEATURESET_7d1];
     p->arch_caps.lo          = fs[FEATURESET_m10Al];
     p->arch_caps.hi          = fs[FEATURESET_m10Ah];
+    p->basic._1Ca            = fs[FEATURESET_1Ca];
+    p->basic._1Cb            = fs[FEATURESET_1Cb];
+    p->basic._1Cc            = fs[FEATURESET_1Cc];
 }
 
 void x86_cpu_policy_recalc_synth(struct cpu_policy *p)
Signature · 9 lines
2.43.0



Ngoc Tu Dinh | Vates XCP-ng Developer

XCP-ng & Xen Orchestra - Vates solutions

web: https://vates.tech
#2 lore raw

Re: [PATCH 1/4] x86: Add Intel architectural LBR featureset bits

Quoted 7 lines · On 18.11.2024 09:49, [email protected] wrote:

From: Tu Dinh <[email protected]>

Expose ARCH_LBR feature to guests.

Extend CPU featureset with 3 words for CPUID leaf 0x1c.

First a general remark: Please Cc maintainers on patch submissions.

tools/libs/guest/xg_cpuid_x86.c int xc_cpuid_apply_policy(xc_interface *xch, uint32_t domid, bool restore, · line 659

Quoted 8 lines
tools/libs/guest/xg_cpuid_x86.c +1 -1
@@ -656,7 +656,7 @@ int xc_cpuid_apply_policy(xc_interface *xch, uint32_t domid, bool restore,
             p->policy.feat.mpx = test_bit(X86_FEATURE_MPX, host_featureset);
         }
 
-        p->policy.basic.max_leaf = min(p->policy.basic.max_leaf, 0xdu);
+        p->policy.basic.max_leaf = min(p->policy.basic.max_leaf, 0x1cu);

We'll need to update this again and again (AMXC, AVX10) if we continue using literal numbers here. This wants switching to ARRAY_SIZE(), and imo ideally in a separate prereq patch (which could likely go straight in).

Quoted 13 lines
tools/libs/guest/xg_cpuid_x86.c +0 -1
--- a/tools/misc/xen-cpuid.c
xen/arch/x86/cpu-policy.c +2 -0
@@ -271,6 +271,8 @@ static void recalculate_misc(struct cpu_policy *p)
 
     p->basic.raw[0xc] = EMPTY_LEAF;
 
+    zero_leaves(p->basic.raw, 0xe, 0x1b);
+
     p->extd.e1d &= ~CPUID_COMMON_1D_FEATURES;
 
     /* Most of Power/RAS hidden from guests. */

xen/arch/x86/cpu-policy.c static void recalculate_misc(struct cpu_policy *p) · line 308

Quoted 4 lines
xen/arch/x86/cpu-policy.c +1 -0
@@ -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] = EMPTY_LEAF;

I'm not convinced this wants to live here. The leaf rather wants clearing when the feature isn't there in the policy. See e.g. [1] and [2].

xen/arch/x86/cpu/common.c static void generic_identify(struct cpuinfo_x86 *c) · line 513

Quoted 12 lines
xen/arch/x86/cpu/common.c +6 -0
@@ -505,6 +505,13 @@ static void generic_identify(struct cpuinfo_x86 *c)
 			    &c->x86_capability[FEATURESET_Da1],
 			    &tmp, &tmp, &tmp);
 
+	if (c->cpuid_level >= 0x1c)
+		cpuid(0x1c,
+			  &c->x86_capability[FEATURESET_1Ca],
+			  &c->x86_capability[FEATURESET_1Cb],
+			  &c->x86_capability[FEATURESET_1Cc],
+			  &tmp);

Nit: Indentation (arguments want to align vertically).

xen/include/public/arch-x86/cpufeatureset.h XEN_CPUFEATURE(SERIALIZE, 9*32+14) /*A SERIALIZE insn */ · line 287

Quoted 8 lines
xen/include/public/arch-x86/cpufeatureset.h +1 -1
@@ -284,7 +284,7 @@ XEN_CPUFEATURE(SERIALIZE,     9*32+14) /*A  SERIALIZE insn */
 XEN_CPUFEATURE(HYBRID,        9*32+15) /*   Heterogeneous platform */
 XEN_CPUFEATURE(TSXLDTRK,      9*32+16) /*a  TSX load tracking suspend/resume insns */
 XEN_CPUFEATURE(PCONFIG,       9*32+18) /*   PCONFIG instruction */
-XEN_CPUFEATURE(ARCH_LBR,      9*32+19) /*   Architectural Last Branch Record */
+XEN_CPUFEATURE(ARCH_LBR,      9*32+19) /*S  Architectural Last Branch Record */

'S' is too early. You first need to implement everything, and only then you can make the feature visible to guests. It'll also only be by that time that it'll be (halfway) clear whether 'S' is appropriate, or whether for starters it wouldn't better be 's'. Same for the other markers below.

Jan

#3 lore raw

Re: [PATCH 1/4] x86: Add Intel architectural LBR featureset bits

In reply to [email protected]

tools/libs/guest/xg_cpuid_x86.c int xc_cpuid_apply_policy(xc_interface *xch, uint32_t domid, bool restore, · line 662

Quoted 11 lines · On 18.11.2024 09:49, [email protected] wrote:
tools/libs/guest/xg_cpuid_x86.c +1 -1
@@ -656,7 +656,7 @@ int xc_cpuid_apply_policy(xc_interface *xch, uint32_t domid, bool restore,
             p->policy.feat.mpx = test_bit(X86_FEATURE_MPX, host_featureset);
         }
 
-        p->policy.basic.max_leaf = min(p->policy.basic.max_leaf, 0xdu);
+        p->policy.basic.max_leaf = min(p->policy.basic.max_leaf, 0x1cu);
         p->policy.feat.max_subleaf = 0;
         p->policy.extd.max_leaf = min(p->policy.extd.max_leaf, 0x8000001c);
     }

When I saw this change I took note of there being an omission in the AVX10 series I'm carrying. Yet now that I wanted to make the adjustment there, I found that this code needs leaving alone (also in your case). Please see the comment at the top of the enclosing if().

Jan

#4 lore raw

PATCH 2/4 x86: Add architectural LBR declarations

In reply to [email protected]

From: Tu Dinh <[email protected]>

---
 xen/arch/x86/include/asm/msr-index.h | 11 +++++++++++
 1 file changed, 11 insertions(+)
xen/arch/x86/include/asm/msr-index.h +11 -0
index 9cdb5b2625..867deab3c6 100644
@@ -304,6 +304,17 @@
 #define MSR_IA32_LASTINTFROMIP		0x000001dd
 #define MSR_IA32_LASTINTTOIP		0x000001de
 
+/* 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
 
 #define MSR_IA32_MTRR_PHYSBASE(n)   (0x00000200 + 2 * (n))
Signature · 9 lines
2.43.0



Ngoc Tu Dinh | Vates XCP-ng Developer

XCP-ng & Xen Orchestra - Vates solutions

web: https://vates.tech
#5 lore raw

Re: [PATCH 2/4] x86: Add architectural LBR declarations

xen/arch/x86/include/asm/msr-index.h

Quoted 3 lines · On 18.11.2024 09:49, [email protected] wrote:
xen/arch/x86/include/asm/msr-index.h +0 -0
@@ -304,6 +304,17 @@

Please note this comment around line 250:

/*
* Legacy MSR constants in need of cleanup. No new MSRs below this comment. */

You want to ...

xen/arch/x86/include/asm/msr-index.h line 316

Quoted 13 lines
xen/arch/x86/include/asm/msr-index.h +10 -0
 #define MSR_IA32_LASTINTFROMIP		0x000001dd
 #define MSR_IA32_LASTINTTOIP		0x000001de
 
+/* 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

... move your addition up, omit the IA32 infixes, format according to how other entries there are formatted, and sort numerically (implying these can't all stay together).

Jan

#6 lore raw

Re: [PATCH 2/4] x86: Add architectural LBR declarations

In reply to [email protected]
Quoted 6 lines · On 18.11.2024 09:49, [email protected] wrote:
xen/arch/x86/include/asm/msr-index.h +0 -0
@@ -304,6 +304,17 @@
 #define MSR_IA32_LASTINTFROMIP		0x000001dd
 #define MSR_IA32_LASTINTTOIP		0x000001de
 

xen/arch/x86/include/asm/msr-index.h line 316

Quoted 10 lines
xen/arch/x86/include/asm/msr-index.h +10 -0
+/* 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

This is rather NUM than MAX; MAX would be 63.

Jan

#7 lore raw

Re: [PATCH 2/4] x86: Add architectural LBR declarations

In reply to [email protected]
Quoted 12 lines · On 18.11.2024 09:49, [email protected] wrote:

From: Tu Dinh <[email protected]>

---
 xen/arch/x86/include/asm/msr-index.h | 11 +++++++++++
 1 file changed, 11 insertions(+)
xen/arch/x86/include/asm/msr-index.h +0 -0
index 9cdb5b2625..867deab3c6 100644

xen/arch/x86/include/asm/msr-index.h line 308

Quoted 5 lines
xen/arch/x86/include/asm/msr-index.h +2 -0
@@ -304,6 +304,17 @@
 #define MSR_IA32_LASTINTFROMIP		0x000001dd
 #define MSR_IA32_LASTINTTOIP		0x000001de
 
+/* Architectural LBR state MSRs */
+#define MSR_IA32_LASTBRANCH_CTL		0x000014ce

The SDM calls this IA32_LBR_CTL, so we want it to be MSR_LBR_CTL, ...

xen/arch/x86/include/asm/msr-index.h line 311

Quoted 3 lines
xen/arch/x86/include/asm/msr-index.h +3 -0
+#define  LASTBRANCH_CTL_LBREN		(1<<0) /* Enable LBR recording */
+#define  LASTBRANCH_CTL_VALID		_AC(0x7f000f, ULL)
+#define MSR_IA32_LASTBRANCH_DEPTH	0x000014cf

... MSR_LBR_DEPTH, and ...

xen/arch/x86/include/asm/msr-index.h line 315

Quoted 4 lines
xen/arch/x86/include/asm/msr-index.h +4 -0
+#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

... similarly *_LBR_* here.

For these three, as they actually cover groups, in other similar cases we use function like macros, to ease access to any of the MSRs in the group. I.e.

#define MSR_LBR_INFO(n) (0x00001200 + (n))

xen/arch/x86/include/asm/msr-index.h line 316

Quoted 1 line
xen/arch/x86/include/asm/msr-index.h +1 -0
+#define MAX_MSR_ARCH_LASTBRANCH_FROM_TO	64

Where's this 64 coming from? I understand CPUID permits up to 64, but the XSAVES area layout covers only up to 32. Is the SDM flawed here?

Jan

#8 lore raw

PATCH 3/4 x86: Adjust arch LBR CPU policy

In reply to [email protected]

From: Tu Dinh <[email protected]>

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.

---
 xen/arch/x86/cpu-policy.c | 33 +++++++++++++++++++++++++++++++++
 1 file changed, 33 insertions(+)
xen/arch/x86/cpu-policy.c +33 -0
index cf6b212fb6..2ac76eb058 100644
@@ -638,6 +638,36 @@ static void __init calculate_pv_max_policy(void)
     p->extd.raw[0xa] = EMPTY_LEAF; /* No SVM for PV guests. */
 }
 
+/*
+ * 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 = true;
+
+    rdmsrl(MSR_IA32_LASTBRANCH_DEPTH, host_lbr_depth);
+    if ((host_lbr_depth == 0) ||
+        (host_lbr_depth % 8) ||
+        (host_lbr_depth > 64))
+        lbr_supported = false;
+
+    host_lbr_depth = 1ul << ((host_lbr_depth / 8) - 1);
+    if ((host_lbr_depth & fs[FEATURESET_1Ca] & 0xff) == 0)
+        lbr_supported = false;
+
+    if (lbr_supported)
+    {
+        fs[FEATURESET_1Ca] = (fs[FEATURESET_1Ca] & ~0xffu) | host_lbr_depth;
+    }
+    else
+    {
+        __clear_bit(X86_FEATURE_ARCH_LBR, fs);
+        fs[FEATURESET_1Ca] = fs[FEATURESET_1Cb] = fs[FEATURESET_1Cc] = 0;
+    }
+}
+
 static void __init calculate_pv_def_policy(void)
 {
     struct cpu_policy *p = &pv_def_cpu_policy;
@@ -760,6 +790,9 @@ static void __init calculate_hvm_max_policy(void)
             __clear_bit(X86_FEATURE_XSAVES, fs);
     }
 
+    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 use
      * the VMCS load/save controls for efficiency reasons.  This depends on
Signature · 9 lines
2.43.0



Ngoc Tu Dinh | Vates XCP-ng Developer

XCP-ng & Xen Orchestra - Vates solutions

web: https://vates.tech
#9 lore raw

Re: [PATCH 3/4] x86: Adjust arch LBR CPU policy

Quoted 4 lines · On 18.11.2024 09:49, [email protected] wrote:

From: Tu Dinh <[email protected]>

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.

How will this work across migration?

What does "single depth that's equal to that of the host" mean, when multiple depths can be advertised as supported? Perhaps I'm irritated by ...

Quoted 6 lines
xen/arch/x86/cpu-policy.c +0 -0
@@ -638,6 +638,36 @@ static void __init calculate_pv_max_policy(void)
     p->extd.raw[0xa] = EMPTY_LEAF; /* No SVM for PV guests. */
 }
 

xen/arch/x86/cpu-policy.c static void __init calculate_pv_max_policy(void) · line 650

Quoted 10 lines
xen/arch/x86/cpu-policy.c +10 -0
+/*
+ * 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 = true;
+
+    rdmsrl(MSR_IA32_LASTBRANCH_DEPTH, host_lbr_depth);

... you reading an MSR here which was never set by Xen. Whatever the firmware left there should not be relevant to us.

xen/arch/x86/cpu-policy.c static void __init calculate_pv_max_policy(void) · line 654

Quoted 4 lines
xen/arch/x86/cpu-policy.c +4 -0
+    if ((host_lbr_depth == 0) ||
+        (host_lbr_depth % 8) ||
+        (host_lbr_depth > 64))
+        lbr_supported = false;

Here and below: Please familiarize yourself with Xen coding style. if() and alike want blanks immediately inside the parentheses.

Quoted 4 lines
xen/arch/x86/cpu-policy.c +4 -0
+    host_lbr_depth = 1ul << ((host_lbr_depth / 8) - 1);
+    if ((host_lbr_depth & fs[FEATURESET_1Ca] & 0xff) == 0)
+        lbr_supported = false;
+

xen/arch/x86/cpu-policy.c static void __init calculate_pv_max_policy(void) · line 669

Quoted 10 lines
xen/arch/x86/cpu-policy.c +10 -0
+    if (lbr_supported)
+    {
+        fs[FEATURESET_1Ca] = (fs[FEATURESET_1Ca] & ~0xffu) | host_lbr_depth;
+    }
+    else
+    {
+        __clear_bit(X86_FEATURE_ARCH_LBR, fs);
+        fs[FEATURESET_1Ca] = fs[FEATURESET_1Cb] = fs[FEATURESET_1Cc] = 0;
+    }
+}

Hmm, is it really a good idea to fiddle with the featureset, rather than (after conversion back) with the policy? Cc-ing Andrew. This would then also avoid use of several plain integer literals.

Jan

#10 lore raw

PATCH 4/4 x86/vmx: Virtualize architectural LBRs

In reply to [email protected]

From: Tu Dinh <[email protected]>

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.

---
 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(-)
xen/arch/x86/cpu-policy.c +3 -0
index 2ac76eb058..9e78273a79 100644
@@ -788,6 +788,9 @@ static void __init calculate_hvm_max_policy(void)
 
         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);
     }
 
     if ( test_bit(X86_FEATURE_ARCH_LBR, fs) )
xen/arch/x86/hvm/vmx/vmcs.c +9 -2
index 147e998371..a16daad78a 100644
@@ -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
 
     if ( !printed )
@@ -448,7 +449,8 @@ static int vmx_init_vmcs_config(bool bsp)
 
     min = VM_EXIT_ACK_INTR_ON_EXIT;
     opt = (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 |= VM_EXIT_IA32E_MODE;
     _vmx_vmexit_control = 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)
 
     min = 0;
     opt = (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 = adjust_vmx_controls(
         "VMEntry Control", min, opt, MSR_IA32_VMX_ENTRY_CTLS, &mismatch);
 
@@ -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));
 
+    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);
 
@@ -2087,6 +2092,8 @@ void vmcs_dump_vcpu(struct vcpu *v)
            vmr32(GUEST_PREEMPTION_TIMER), vmr32(GUEST_SMBASE));
     printk("DebugCtl = 0x%016lx  DebugExceptions = 0x%016lx\n",
            vmr(GUEST_IA32_DEBUGCTL), vmr(GUEST_PENDING_DBG_EXCEPTIONS));
+    if ( vmentry_ctl & VM_ENTRY_LOAD_GUEST_LBR_CTL )
+        printk("LbrCtl = 0x%016lx\n", vmr(GUEST_LBR_CTL));
     if ( vmentry_ctl & (VM_ENTRY_LOAD_PERF_GLOBAL_CTRL | VM_ENTRY_LOAD_BNDCFGS) )
         printk("PerfGlobCtl = 0x%016lx  BndCfgS = 0x%016lx\n",
                vmr(GUEST_PERF_GLOBAL_CTRL), vmr(GUEST_BNDCFGS));
xen/arch/x86/hvm/vmx/vmx.c +191 -78

351 lines not on this page · raw message

xen/arch/x86/include/asm/hvm/vmx/vmcs.h +8 -0
index 939b87eb50..2b265e583f 100644
@@ -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;
 
 #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;
 
 #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))
 
 #define VMCS_RID_TYPE_MASK              0x80000000U
 
@@ -480,6 +486,8 @@ enum vmcs_field {
     GUEST_PDPTE0                    = 0x0000280a,
 #define GUEST_PDPTE(n) (GUEST_PDPTE0 + (n) * 2) /* n = 0...3 */
     GUEST_BNDCFGS                   = 0x00002812,
+    GUEST_RTIT_CTL                  = 0x00002814,
+    GUEST_LBR_CTL                   = 0x00002816,
     HOST_PAT                        = 0x00002c00,
     HOST_EFER                       = 0x00002c02,
     HOST_PERF_GLOBAL_CTRL           = 0x00002c04,
Signature · 9 lines
2.43.0



Ngoc Tu Dinh | Vates XCP-ng Developer

XCP-ng & Xen Orchestra - Vates solutions

web: https://vates.tech
#11 lore raw

Re: [PATCH 4/4] x86/vmx: Virtualize architectural LBRs

xen/arch/x86/cpu-policy.c static void __init calculate_hvm_max_policy(void) · line 793

Quoted 9 lines · On 18.11.2024 09:49, [email protected] wrote:
xen/arch/x86/cpu-policy.c +3 -0
@@ -788,6 +788,9 @@ static void __init calculate_hvm_max_policy(void)
 
         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);

How will this be reflected onto leaf 1C? Patch 3 doesn't check this bit. In fact I wonder whether patch 1 shouldn't introduce dependencies of all leaf 1C bits upon this one bit (in tools/gen-cpuid.py).

Quoted 7 lines
xen/arch/x86/hvm/vmx/vmx.c +0 -1
@@ -423,65 +423,96 @@ static int cf_check vmx_pi_update_irte(const struct vcpu *v,
     return rc;
 }
 
-static const struct lbr_info {

xen/arch/x86/hvm/vmx/vmx.c static int cf_check vmx_pi_update_irte(const struct vcpu *v, · line 428

Quoted 10 lines
xen/arch/x86/hvm/vmx/vmx.c +2 -7
+struct lbr_info {
     u32 base, count;
-} p4_lbr[] = {
-    { 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;

uint64_t please in new code.

xen/arch/x86/hvm/vmx/vmx.c static int cf_check vmx_pi_update_irte(const struct vcpu *v, · line 437

Quoted 9 lines
xen/arch/x86/hvm/vmx/vmx.c +9 -0
+};
+
+static const struct lbr_info p4_lbr[] = {
+    { 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 }

If these adjustments are really needed, I'd wonder whether we wouldn't be better of switching to C99 initializers instead.

xen/arch/x86/hvm/vmx/vmx.c static int cf_check vmx_pi_update_irte(const struct vcpu *v, · line 492

Quoted 5 lines
xen/arch/x86/hvm/vmx/vmx.c +5 -0
+static struct lbr_info __ro_after_init architectural_lbr[] = {
+    { MSR_IA32_LASTINTFROMIP,        1, 0 },
+    { MSR_IA32_LASTINTTOIP,          1, 0 },
+    { MSR_IA32_LER_INFO,             1, 0 },
+    /* to be updated by update_arch_lbr */

Nit: Comment style (start with a capital letter).

xen/arch/x86/hvm/vmx/vmx.c static int cf_check vmx_pi_update_irte(const struct vcpu *v, · line 498

Quoted 6 lines
xen/arch/x86/hvm/vmx/vmx.c +6 -0
+    { 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 = 0;

No need for the initializer.

xen/arch/x86/hvm/vmx/vmx.c static int cf_check vmx_pi_update_irte(const struct vcpu *v, · line 505

Quoted 6 lines
xen/arch/x86/hvm/vmx/vmx.c +6 -0
+static void __init update_arch_lbr(void)
+{
+    struct lbr_info *lbr = architectural_lbr;
+
+    if ( boot_cpu_has(X86_FEATURE_ARCH_LBR) )
+        rdmsrl(MSR_IA32_LASTBRANCH_DEPTH, host_lbr_depth);

Again you're reading an MSR here which was never written. Are you perhaps assuming that the reset value is still in place?

xen/arch/x86/hvm/vmx/vmx.c static int cf_check vmx_pi_update_irte(const struct vcpu *v, · line 512

Quoted 7 lines
xen/arch/x86/hvm/vmx/vmx.c +7 -0
+    ASSERT((host_lbr_depth % 8) == 0 && (host_lbr_depth <= 64));
+
+    for ( ; lbr->count; lbr++ ) {
+        if ( lbr->base == MSR_IA32_LASTBRANCH_0_INFO ||
+             lbr->base == MSR_IA32_LASTBRANCH_0_FROM_IP ||
+             lbr->base == MSR_IA32_LASTBRANCH_0_TO_IP )
+            lbr->count = (u32)host_lbr_depth;

You don't want to use presently undefined bits here which may happen to become defined on future hardware. IOW a cast is insufficient here. (Comment applies to patch 2 as well.)

xen/arch/x86/hvm/vmx/vmx.c static void __init ler_to_fixup_check(void) · line 3339

Quoted 6 lines
xen/arch/x86/hvm/vmx/vmx.c +1 -1
@@ -3303,25 +3336,36 @@ static void __init ler_to_fixup_check(void)
     }
 }
 
-static int is_last_branch_msr(u32 ecx)
+static const struct lbr_info * find_last_branch_msr(struct vcpu *v, u32 ecx)

Nit: Excess blank after the first *, and please take the opportunity to switch to uint32_t.

xen/arch/x86/hvm/vmx/vmx.c static void __init ler_to_fixup_check(void) · line 3350

Quoted 13 lines
xen/arch/x86/hvm/vmx/vmx.c +8 -2
 {
+    /*
+     * Model-specific and architectural LBRs are mutually exclusive.
+     * It's not necessary to check both lbr_info lists.
+     */
     const struct lbr_info *lbr = model_specific_lbr;
+    const struct cpu_policy *cp = v->domain->arch.cpu_policy;
 
-    if ( lbr == NULL )
-        return 0;
+    if ( lbr == NULL ) {
+        if ( cp->feat.arch_lbr )
+            lbr = architectural_lbr;

I'm inclined to think that this should be independent of lbr being NULL. That would then also eliminate the style issue (with the placement of the figure brace).

By the end of the patch / series, what I'm missing are context switch and migration handling. You want to engage XSAVES to cover both (it being the first XSS component we support, there'll be prereq work necessary, as I think Andrew did already point out). Or did I overlook anything?

Jan

#12 lore raw
In reply to [email protected]
Quoted 17 lines · On 18.11.2024 09:49, [email protected] wrote:

From: Tu Dinh <[email protected]>

Intel model-specific last branch records (LBRs) were replaced by architectural LBRs (see Chapter 20 of Intel SDM volume 3B). This patchset implements virtual LBRs for HVM guests using Intel's "load guest IA32_LBR_CTL" and "clear IA32_LBR_CTL" VMX controls.

Add the necessary CPUID and VMX feature checks into Xen. Note that in this patchset, MSR_IA32_LASTBRANCH_DEPTH is only allowed to be equal to that of the host's.

Tu Dinh (4):
x86: Add Intel architectural LBR featureset bits
x86: Add architectural LBR declarations
x86: Adjust arch LBR CPU policy
x86/vmx: Virtualize architectural LBRs

Quoted 12 lines
 tools/libs/guest/xg_cpuid_x86.c             |   2 +-
 tools/misc/xen-cpuid.c                      |   3 +
 xen/arch/x86/cpu-policy.c                   |  39 +++
 xen/arch/x86/cpu/common.c                   |   7 +
 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 +
 xen/arch/x86/include/asm/msr-index.h        |  11 +
 xen/include/public/arch-x86/cpufeatureset.h |  28 +-
 xen/include/xen/lib/x86/cpu-policy.h        |  38 ++-
 xen/lib/x86/cpuid.c                         |   6 +
 11 files changed, 339 insertions(+), 83 deletions(-)

Looking over just the files touched: No change to XSAVE logic at all?

Jan

author
#13 lore raw
Quoted 1 line · Jan Beulich

Looking over just the files touched: No change to XSAVE logic at all?

XSAVE is hidden behind a new IA32_XSS bit. I'll try to implement that next.

Ngoc Tu Dinh | Vates XCP-ng Developer

XCP-ng & Xen Orchestra - Vates solutions

web: https://vates.tech

#14 lore raw
Quoted 3 lines · Tu Dinh
Quoted 1 line · Jan Beulich

Looking over just the files touched: No change to XSAVE logic at all?

XSAVE is hidden behind a new IA32_XSS bit. I'll try to implement that next.

It's rather more severe than that.

Without XSAVE support, Xen can't context-switch the LBR state when vCPUs are scheduled in and out.  (In patch 4 you seem to have copied the legacy way, which is extremely expensive.)

Architecturally, ARCH_LBR depends on XSAVES so OSes can context switch it easily(ish) per thread.

There's also a reason why we haven't got this working yet.  There are a couple of areas of prerequisite work which need addressing before XSS can be enabled properly.

If you're willing to tackle this, then I can explain what needs doing, and in roughly which order.

~Andrew

author
#15 lore raw
Quoted 6 lines · Andrew Cooper

There's also a reason why we haven't got this working yet.  There are a couple of areas of prerequisite work which need addressing before XSS can be enabled properly.

If you're willing to tackle this, then I can explain what needs doing, and in roughly which order.

I would appreciate explanations of the pending XSS issues.

Ngoc Tu Dinh | Vates XCP-ng Developer

XCP-ng & Xen Orchestra - Vates solutions

web: https://vates.tech

author
#16 lore raw
In reply to Andrew Cooper

Hi Andrew,

Quoted 14 lines · Andrew Cooper
Quoted 3 lines · Tu Dinh
Quoted 1 line · Jan Beulich

Looking over just the files touched: No change to XSAVE logic at all?

XSAVE is hidden behind a new IA32_XSS bit. I'll try to implement that next.

It's rather more severe than that.

Without XSAVE support, Xen can't context-switch the LBR state when vCPUs are scheduled in and out.  (In patch 4 you seem to have copied the legacy way, which is extremely expensive.)

Architecturally, ARCH_LBR depends on XSAVES so OSes can context switch it easily(ish) per thread.

Quoted 8 lines

There's also a reason why we haven't got this working yet.  There are a couple of areas of prerequisite work which need addressing before XSS can be enabled properly.

If you're willing to tackle this, then I can explain what needs doing, and in roughly which order.

~Andrew

Following the community call yesterday, I'd like to clarify my understanding of the issue:

- Firstly, virtual XSS support for architectural LBR must be enabled. I noticed that XSS is already implemented, just not enabled; barring the LBR format issues below, are there any other issues with the current XSS implementation?

- There are LBR format differences between some cores of the same CPU (e.g. in Intel hybrid CPUs: P-cores use effective IP while E-cores use linear IP). These differences are expected to be handled by XSAVES/XRSTORS. However, Xen would have to make sure that LBR MSRs are saved/restored by XSS instead of by manually poking MSRs.

- A related issue is handling the compressed XSAVE format for migration streams. Xen currently expands/compacts XSAVE format manually during migration; are there any concerns with arch LBR breaking the XSAVE migration logic?

My understanding is that as long as we don't manually poke the LBR state component, and that LBR state size remains consistent across hybrid cores in the same CPU (which it should be for XSAVE compatibility), there should be no concern with the XSAVE state itself. However, Xen must check CPU features of both sides during migration to make sure that XSAVE states are compatible, which is more complex in migrations involving hosts with hybrid CPUs.

Please tell me if I'm missing any potential issues.

Thanks,

Ngoc Tu Dinh | Vates XCP-ng Developer

XCP-ng & Xen Orchestra - Vates solutions

web: https://vates.tech

#17 lore raw
Quoted 22 lines · Tu Dinh
Quoted 14 lines · Andrew Cooper
Quoted 3 lines · Tu Dinh
Quoted 1 line · Jan Beulich

Looking over just the files touched: No change to XSAVE logic at all?

XSAVE is hidden behind a new IA32_XSS bit. I'll try to implement that next.

It's rather more severe than that.

Without XSAVE support, Xen can't context-switch the LBR state when vCPUs are scheduled in and out.  (In patch 4 you seem to have copied the legacy way, which is extremely expensive.)

Architecturally, ARCH_LBR depends on XSAVES so OSes can context switch it easily(ish) per thread.

Quoted 6 lines

There's also a reason why we haven't got this working yet.  There are a couple of areas of prerequisite work which need addressing before XSS can be enabled properly.

If you're willing to tackle this, then I can explain what needs doing, and in roughly which order.

Quoted 7 lines

Following the community call yesterday, I'd like to clarify my understanding of the issue:

- Firstly, virtual XSS support for architectural LBR must be enabled. I noticed that XSS is already implemented, just not enabled; barring the LBR format issues below, are there any other issues with the current XSS implementation?

At the very least, as with any code that exists be is effectively unused (and iiuc even DCE-ed by the compiler in at least some of the cases) it'll need to be determined whether what we have actually works. It seems more likely than not that some corrections here and there may be needed.

Quoted 5 lines

- There are LBR format differences between some cores of the same CPU (e.g. in Intel hybrid CPUs: P-cores use effective IP while E-cores use linear IP). These differences are expected to be handled by XSAVES/XRSTORS. However, Xen would have to make sure that LBR MSRs are saved/restored by XSS instead of by manually poking MSRs.

How could XSAVES/XRSTORS take care of format differences? Something that was saved on one type of core and is to be restored on the other type of core would require CS.base at the time the LBR was originally written to be able to convert effective <=> linear IP.

Quoted 4 lines

- A related issue is handling the compressed XSAVE format for migration streams. Xen currently expands/compacts XSAVE format manually during migration; are there any concerns with arch LBR breaking the XSAVE migration logic?

That ought to be fine, I think, barring the first point I made above.

Jan