Skip to contentSkip to conversation

x86/amd: Enable TCE in Xen

PATCH Superseded Teddy Astie 2 participants 4 messages Apr 4, 2025 1 message not in the archive

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#3 lore raw

PATCH v2 2/2 x86/amd: Enable TCE in Xen

In reply to a message not in the archive

Aside exposing this flag to guests, Xen can also make use of it to reduce the cost of some TLB flushes. Enable this flag if supported by hardware.

---
v2:
- Add changelog entry
- use trampoline_efer
- use cpu_has_tce instead of opencoded boot_cpu_has(X86_FEATURE_TCE)

---
 CHANGELOG.md                          | 2 +-
 xen/arch/x86/include/asm/cpufeature.h | 1 +
 xen/arch/x86/setup.c                  | 8 ++++++++
 3 files changed, 10 insertions(+), 1 deletion(-)
CHANGELOG.md +1 -1
index dbfecefbd4..375905e68a 100644
@@ -18,7 +18,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
    - Support PCI passthrough for HVM domUs when dom0 is PVH (note SR-IOV
      capability usage is not yet supported on PVH dom0).
    - Smoke tests for the FreeBSD Xen builds in Cirrus CI.
-   - Guest support for AMD Translation Cache Extension feature.
+   - Guest and Xen support for AMD Translation Cache Extension feature.
 
 ### Removed
 
xen/arch/x86/include/asm/cpufeature.h +1 -0
index 05399fb9c9..ab6d07b767 100644
@@ -114,6 +114,7 @@ static inline bool boot_cpu_has(unsigned int feat)
 #define cpu_has_xop             boot_cpu_has(X86_FEATURE_XOP)
 #define cpu_has_skinit          boot_cpu_has(X86_FEATURE_SKINIT)
 #define cpu_has_fma4            boot_cpu_has(X86_FEATURE_FMA4)
+#define cpu_has_tce             boot_cpu_has(X86_FEATURE_TCE)
 #define cpu_has_tbm             boot_cpu_has(X86_FEATURE_TBM)
 
 /* CPUID level 0x0000000D:1.eax */
xen/arch/x86/setup.c +8 -0
index d70abb7e0c..0e2e7d012f 100644
@@ -2008,6 +2008,14 @@ void asmlinkage __init noreturn __start_xen(void)
     if ( cpu_has_pku )
         set_in_cr4(X86_CR4_PKE);
 
+    if ( cpu_has_tce )
+    {
+        printk("Enabling AMD TCE\n");
+
+        write_efer(read_efer() | EFER_TCE);
+        trampoline_efer |= EFER_TCE;
+    }
+
     if ( opt_invpcid && cpu_has_invpcid )
         use_invpcid = true;
 
Signature · 9 lines
2.47.2



Teddy Astie | Vates XCP-ng Developer

XCP-ng & Xen Orchestra - Vates solutions

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

PATCH v2 1/2 x86/amd: Add guest support for AMD TCE

In reply to a message not in the archive

AMD Translation Cache Extension is a flag that can be enabled in the EFER MSR to optimize some TLB flushes. Expose this flag to guest if supported by hardware.

AMD Architecture Developer Manual describe this feature as follow

Quoted 7 lines

Setting this bit to 1 changes how the INVLPG, INVLPGB, and INVPCID instructions operate on TLB entries. When this bit is 0, these instructions remove the target PTE from the TLB as well as all upper-level table entries that are cached in the TLB, whether or not they are associated with the target PTE. When this bit is set, these instructions will remove the target PTE and only those upper-level entries that lead to the target PTE in the page table hierarchy, leaving unrelated upper-level entries intact. This may provide a performance benefit.

---
 CHANGELOG.md                                | 1 +
 xen/arch/x86/hvm/hvm.c                      | 3 +++
 xen/arch/x86/include/asm/msr-index.h        | 3 ++-
 xen/include/public/arch-x86/cpufeatureset.h | 1 +
 4 files changed, 7 insertions(+), 1 deletion(-)
CHANGELOG.md +1 -0
index 8f6afa5c85..dbfecefbd4 100644
@@ -18,6 +18,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
    - Support PCI passthrough for HVM domUs when dom0 is PVH (note SR-IOV
      capability usage is not yet supported on PVH dom0).
    - Smoke tests for the FreeBSD Xen builds in Cirrus CI.
+   - Guest support for AMD Translation Cache Extension feature.
 
 ### Removed
 
xen/arch/x86/hvm/hvm.c +3 -0
index 5950f3160f..184357b042 100644
@@ -959,6 +959,9 @@ const char *hvm_efer_valid(const struct vcpu *v, uint64_t value,
     if ( (value & EFER_FFXSE) && !p->extd.ffxsr )
         return "FFXSE without feature";
 
+    if ( (value & EFER_TCE) && !p->extd.tce )
+        return "TCE without feature";
+
     if ( (value & EFER_AIBRSE) && !p->extd.auto_ibrs )
         return "AutoIBRS without feature";
 
xen/arch/x86/include/asm/msr-index.h +2 -1
index 22d9e76e55..d8576aec1c 100644
@@ -200,11 +200,12 @@
 #define  EFER_NXE                           (_AC(1, ULL) << 11) /* No Execute Enable */
 #define  EFER_SVME                          (_AC(1, ULL) << 12) /* Secure Virtual Machine Enable */
 #define  EFER_FFXSE                         (_AC(1, ULL) << 14) /* Fast FXSAVE/FXRSTOR */
+#define  EFER_TCE                           (_AC(1, ULL) << 15) /* Translation Cache Extensions */
 #define  EFER_AIBRSE                        (_AC(1, ULL) << 21) /* Automatic IBRS Enable */
 
 #define EFER_KNOWN_MASK \
     (EFER_SCE | EFER_LME | EFER_LMA | EFER_NXE | EFER_SVME | EFER_FFXSE | \
-     EFER_AIBRSE)
+     EFER_TCE | EFER_AIBRSE)
 
 #define MSR_STAR                            _AC(0xc0000081, U) /* legacy mode SYSCALL target */
 #define MSR_LSTAR                           _AC(0xc0000082, U) /* long mode SYSCALL target */
xen/include/public/arch-x86/cpufeatureset.h +1 -0
index cc6e984a88..8182d2dbed 100644
@@ -170,6 +170,7 @@ XEN_CPUFEATURE(SKINIT,        3*32+12) /*   SKINIT/STGI instructions */
 XEN_CPUFEATURE(WDT,           3*32+13) /*   Watchdog timer */
 XEN_CPUFEATURE(LWP,           3*32+15) /*   Light Weight Profiling */
 XEN_CPUFEATURE(FMA4,          3*32+16) /*A  4 operands MAC instructions */
+XEN_CPUFEATURE(TCE,           3*32+17) /*H  Translation Cache Extension support */
 XEN_CPUFEATURE(NODEID_MSR,    3*32+19) /*   NodeId MSR */
 XEN_CPUFEATURE(TBM,           3*32+21) /*A  trailing bit manipulations */
 XEN_CPUFEATURE(TOPOEXT,       3*32+22) /*   topology extensions CPUID leafs */
Signature · 9 lines
2.47.2



 | Vates

XCP-ng & Xen Orchestra - Vates solutions

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

Re: [PATCH v2 1/2] x86/amd: Add guest support for AMD TCE

Quoted 4 lines · Teddy Astie

AMD Translation Cache Extension is a flag that can be enabled in the EFER MSR to optimize some TLB flushes. Expose this flag to guest if supported by hardware.

AMD Architecture Developer Manual describe this feature as follow

Quoted 8 lines
Quoted 7 lines

Setting this bit to 1 changes how the INVLPG, INVLPGB, and INVPCID instructions operate on TLB entries. When this bit is 0, these instructions remove the target PTE from the TLB as well as all upper-level table entries that are cached in the TLB, whether or not they are associated with the target PTE. When this bit is set, these instructions will remove the target PTE and only those upper-level entries that lead to the target PTE in the page table hierarchy, leaving unrelated upper-level entries intact. This may provide a performance benefit.

You probably want to note that Linux 6.14(?) now uses this feature.

You definitely need to discuss that it's only in HAP VMs.  i.e. no PV, and no Shadow.

Personally, I'd drop the quote from the APM.  You've already said "to optimise some TLB flushes".  Anyone wanting to know details about the feature needs to read the APM anyway.

Quoted 24 lines
---
 CHANGELOG.md                                | 1 +
 xen/arch/x86/hvm/hvm.c                      | 3 +++
 xen/arch/x86/include/asm/msr-index.h        | 3 ++-
 xen/include/public/arch-x86/cpufeatureset.h | 1 +
 4 files changed, 7 insertions(+), 1 deletion(-)
CHANGELOG.md +1 -0
index 8f6afa5c85..dbfecefbd4 100644
@@ -18,6 +18,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
    - Support PCI passthrough for HVM domUs when dom0 is PVH (note SR-IOV
      capability usage is not yet supported on PVH dom0).
    - Smoke tests for the FreeBSD Xen builds in Cirrus CI.
+   - Guest support for AMD Translation Cache Extension feature.
 
 ### Removed
 
xen/arch/x86/hvm/hvm.c +0 -0
index 5950f3160f..184357b042 100644

xen/arch/x86/hvm/hvm.c const char *hvm_efer_valid(const struct vcpu *v, uint64_t value, · line 967

Quoted 9 lines
xen/arch/x86/hvm/hvm.c +3 -0
@@ -959,6 +959,9 @@ const char *hvm_efer_valid(const struct vcpu *v, uint64_t value,
     if ( (value & EFER_FFXSE) && !p->extd.ffxsr )
         return "FFXSE without feature";
 
+    if ( (value & EFER_TCE) && !p->extd.tce )
+        return "TCE without feature";
+
     if ( (value & EFER_AIBRSE) && !p->extd.auto_ibrs )
         return "AutoIBRS without feature";
 

Not for this patch, but it's probably time we made hvm_efer_valid() work more like hvm_cr4_guest_valid_bits().

The only interesting bit is the specific diagnostic for LMA/LME/CR0 mismatch, and I don't think the complexity is worth it for just that.

Quoted 5 lines
xen/arch/x86/include/asm/msr-index.h +0 -0
index 22d9e76e55..d8576aec1c 100644

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

Quoted 10 lines
xen/arch/x86/include/asm/msr-index.h +2 -1
@@ -200,11 +200,12 @@
 #define  EFER_NXE                           (_AC(1, ULL) << 11) /* No Execute Enable */
 #define  EFER_SVME                          (_AC(1, ULL) << 12) /* Secure Virtual Machine Enable */
 #define  EFER_FFXSE                         (_AC(1, ULL) << 14) /* Fast FXSAVE/FXRSTOR */
+#define  EFER_TCE                           (_AC(1, ULL) << 15) /* Translation Cache Extensions */
 #define  EFER_AIBRSE                        (_AC(1, ULL) << 21) /* Automatic IBRS Enable */
 
 #define EFER_KNOWN_MASK \
     (EFER_SCE | EFER_LME | EFER_LMA | EFER_NXE | EFER_SVME | EFER_FFXSE | \
-     EFER_AIBRSE)
+     EFER_TCE | EFER_AIBRSE)

Sorry, missed one aspect.  You need to filter TSE out in guest_efer() for PV guests.

xen/include/public/arch-x86/cpufeatureset.h XEN_CPUFEATURE(SKINIT, 3*32+12) /* SKINIT/STGI instructions */ · line 173

Quoted 12 lines
xen/arch/x86/include/asm/msr-index.h +0 -0
 #define MSR_STAR                            _AC(0xc0000081, U) /* legacy mode SYSCALL target */
 #define MSR_LSTAR                           _AC(0xc0000082, U) /* long mode SYSCALL target */
xen/include/public/arch-x86/cpufeatureset.h +1 -0
index cc6e984a88..8182d2dbed 100644
@@ -170,6 +170,7 @@ XEN_CPUFEATURE(SKINIT,        3*32+12) /*   SKINIT/STGI instructions */
 XEN_CPUFEATURE(WDT,           3*32+13) /*   Watchdog timer */
 XEN_CPUFEATURE(LWP,           3*32+15) /*   Light Weight Profiling */
 XEN_CPUFEATURE(FMA4,          3*32+16) /*A  4 operands MAC instructions */
+XEN_CPUFEATURE(TCE,           3*32+17) /*H  Translation Cache Extension support */

I'd suggest dropping the "support".  It doesn't add anything here.

~Andrew

#4 lore raw
In reply to Teddy Astie
Quoted 5 lines · Teddy Astie
xen/arch/x86/setup.c +0 -0
index d70abb7e0c..0e2e7d012f 100644

xen/arch/x86/setup.c void asmlinkage __init noreturn __start_xen(void) · line 2016

Quoted 9 lines
xen/arch/x86/setup.c +6 -0
@@ -2008,6 +2008,14 @@ void asmlinkage __init noreturn __start_xen(void)
     if ( cpu_has_pku )
         set_in_cr4(X86_CR4_PKE);
 
+    if ( cpu_has_tce )
+    {
+        printk("Enabling AMD TCE\n");
+
+        write_efer(read_efer() | EFER_TCE);
+        trampoline_efer |= EFER_TCE;

This doesn't do what you think it does. (it writes into the copy of the trampoline in .init, not the one placed in low memory).

You need to use bootsym() to get to the real trampoline.

I'm also not sure the prinkt() is useful.  It's about universal on AMD systems.

~Andrew