[PATCH 07/12] x86: Have x86_emulate/ implement the single-vendor optimisation

Alejandro Vallejo posted 12 patches 3 days, 17 hours ago
[PATCH 07/12] x86: Have x86_emulate/ implement the single-vendor optimisation
Posted by Alejandro Vallejo 3 days, 17 hours ago
Open code the vendor check through the policy as a one-off. The emulator
embeds amd_like() in macros and is called in MANY places. Using a
local variable (cp->x86_vendor) makes it a lot smaller (300-400 bytes
smaller). So treat this as the exception it is and let it use the policy
rather than boot_cpu_data.

Also keep the current behaviour of using the policy vendor when
compiled for userspace, where cross-vendor configurations are expected.

Not a functional change.

Signed-off-by: Alejandro Vallejo <alejandro.garciavallejo@amd.com>
---
 xen/arch/x86/x86_emulate/private.h     | 10 +++++++++-
 xen/arch/x86/x86_emulate/x86_emulate.c |  2 +-
 2 files changed, 10 insertions(+), 2 deletions(-)

diff --git a/xen/arch/x86/x86_emulate/private.h b/xen/arch/x86/x86_emulate/private.h
index 24c79c4e8f..95f2015c44 100644
--- a/xen/arch/x86/x86_emulate/private.h
+++ b/xen/arch/x86/x86_emulate/private.h
@@ -15,6 +15,7 @@
 # include <xen/kernel.h>
 
 # include <asm/cpu-user-regs.h>
+# include <asm/cpufeature.h>
 # include <asm/endbr.h>
 # include <asm/msr-index.h>
 # include <asm/stubs.h>
@@ -30,8 +31,15 @@ void BUG(void);
 #  define X86EMUL_NO_SIMD
 # endif
 
+/* intentionally avoid cpu_vendor(), as it produces much worse codegen */
+# define x86emul_cpu(cp) ((X86_ENABLED_VENDORS ==            \
+                           ISOLATE_LSB(X86_ENABLED_VENDORS)) \
+                               ? X86_ENABLED_VENDORS         \
+                               : ((cp)->x86_vendor & X86_ENABLED_VENDORS))
+
 #else /* !__XEN__ */
 # include "x86-emulate.h"
+# define x86emul_cpu(cp) ((cp)->x86_vendor)
 #endif
 
 #ifdef __i386__
@@ -520,7 +528,7 @@ in_protmode(
 static inline bool
 _amd_like(const struct cpu_policy *cp)
 {
-    return cp->x86_vendor & (X86_VENDOR_AMD | X86_VENDOR_HYGON);
+    return x86emul_cpu(cp) & (X86_VENDOR_AMD | X86_VENDOR_HYGON);
 }
 
 static inline bool
diff --git a/xen/arch/x86/x86_emulate/x86_emulate.c b/xen/arch/x86/x86_emulate/x86_emulate.c
index 7751a67130..308ec3579f 100644
--- a/xen/arch/x86/x86_emulate/x86_emulate.c
+++ b/xen/arch/x86/x86_emulate/x86_emulate.c
@@ -3104,7 +3104,7 @@ x86_emulate(
          * in fact risking to make guest OSes vulnerable to the equivalent of
          * XSA-7 (CVE-2012-0217).
          */
-        generate_exception_if(cp->x86_vendor == X86_VENDOR_INTEL &&
+        generate_exception_if((x86emul_cpu(cp) & X86_VENDOR_INTEL) &&
                               op_bytes == 8 && !is_canonical_address(_regs.rcx),
                               X86_EXC_GP, 0);
 #endif
-- 
2.43.0