[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH] x86emul: Cache the amd_like() output on x86_emulate() entry


  • To: <xen-devel@xxxxxxxxxxxxxxxxxxxx>
  • From: Alejandro Vallejo <alejandro.garciavallejo@xxxxxxx>
  • Date: Mon, 21 Sep 2026 11:25:44 +0200
  • Arc-authentication-results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=lists.xenproject.org smtp.mailfrom=amd.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=amd.com; dkim=none (message not signed); arc=none (0)
  • Arc-message-signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=Sgk6kE+hr6M+rgcsXPhKjfl2VOBlqviynvvQD3lGOLA=; b=PtJhh96/IirjzwsFLyHizmkO1X25m5M8HLuAGXN99rL1roFOeknmd3yysdCB2nzkgA4cOhkRy8WCOPbAbM5YR+A08ntQNOyKHqyGBj8OYFkYgZkChVlsAHJpDE6IG3xIxHCA/XSWeDjPXAjsKPiF2cdt13OhCEVUfaEGIpHzIJ8P9hSJ1LHhBqb8k0+5LwBxXPhEJEINPADFihlvkVpY85J1/iR97ipgosvJPZazpDzKcayHsdShhtBfzUpUJ6lqmgF/BXBCy8ZDTkrW4FccHQIJR+GDJ9kPc7rBgfTptLQ/TX9pe44YMFzZKpiRMnw2nK27DfErWZDgPmVhZeEMZA==
  • Arc-seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=kYXUJf7ROEIPC9cdTJCqdCh6uZiTRSx233tw0ONufMntRQ/Z/rLSSvIlsNXcRrLOZ03JcMJtzCx/ZMlDS/TTiXtRV5Fa+x4MqW2HdjUPmKxtJ1EM886LFq4J4KZS0Q/mBV29zNW+MgoFvBv2gV7z/1xtQWz/UtzPWbqJDt6fK4Qil5iDH7yvQabkMiH1H5RRDyI/Z7FvgWZMvFS1gWnIiHqw43IbIhlo3eWF/ZUYrmBushz5HxqGhu9MTjpVca4W/MyBHY735QLBqX3d8gW6c3fcM7rMZ5lfC7nYLeyYTVSRni4DaHn56Q51cbz1rCWLxOQdoJHliWtyIL2yz3zfLA==
  • Authentication-results: eu.smtp.expurgate.cloud; dkim=pass header.s=selector1 header.d=amd.com header.i="@amd.com" header.h="From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck"
  • Cc: Alejandro Vallejo <alejandro.garciavallejo@xxxxxxx>, Jan Beulich <jbeulich@xxxxxxxx>, Andrew Cooper <andrew.cooper3@xxxxxxxxxx>, Roger Pau Monné <roger@xxxxxxxxxxxxxx>, Teddy Astie <teddy.astie@xxxxxxxxxx>
  • Delivery-date: Mon, 21 Sep 2026 09:26:13 +0000
  • List-id: Xen developer discussion <xen-devel.lists.xenproject.org>

The current code instantiates amd_like() way too many times, leading to
codegen explosion. Unconditionally call it early on, and use that
variable everywhere. This shrinks the emulator by ~3KiB.

No a functional change.

Signed-off-by: Alejandro Vallejo <alejandro.garciavallejo@xxxxxxx>
---
pipeline: 
https://gitlab.com/xen-project/people/agvallejo/xen/-/pipelines/2860748404
          (it's red because of an caching issue and personal branch name
           conventions it's just arm. x86 passes in full)

bloat-o-meter before-after patch.

add/remove: 0/0 grow/shrink: 0/1 up/down: 0/-3366 (-3366)
Function                                     old     new   delta
x86_emulate                               209351  205985   -3366
Total: Before=3617694, After=3614328, chg -0.09%
---
 xen/arch/x86/x86_emulate/x86_emulate.c | 19 ++++++++++---------
 1 file changed, 10 insertions(+), 9 deletions(-)

diff --git a/xen/arch/x86/x86_emulate/x86_emulate.c 
b/xen/arch/x86/x86_emulate/x86_emulate.c
index 89c37fea2c..c69ef0781e 100644
--- a/xen/arch/x86/x86_emulate/x86_emulate.c
+++ b/xen/arch/x86/x86_emulate/x86_emulate.c
@@ -363,7 +363,7 @@ do {                                                        
            \
 #define jmp_rel(rel)                                                    \
 do {                                                                    \
     unsigned long ip = _regs.r(ip) + (int)(rel);                        \
-    if ( op_bytes == 2 && (amd_like(ctxt) || !mode_64bit()) )           \
+    if ( op_bytes == 2 && (is_amd_like || !mode_64bit()) )              \
         ip = (uint16_t)ip;                                              \
     else if ( !mode_64bit() )                                           \
         ip = (uint32_t)ip;                                              \
@@ -576,7 +576,7 @@ static inline void put_loop_count(
          * zero extend relevant registers first when using 32-bit       \
          * addressing in 64-bit mode.                                   \
          */                                                             \
-        if ( !amd_like(ctxt) && mode_64bit() && ad_bytes == 4 )         \
+        if ( !is_amd_like && mode_64bit() && ad_bytes == 4 )            \
         {                                                               \
             _regs.r(cx) = 0;                                            \
             if ( extend_si ) _regs.r(si) = (uint32_t)_regs.r(si);       \
@@ -1310,6 +1310,7 @@ x86_emulate(
     /* Shadow copy of register state. Committed on successful emulation. */
     struct cpu_user_regs _regs = *ctxt->regs;
     const struct cpu_policy *__maybe_unused cp = ctxt->cpu_policy;
+    bool is_amd_like = amd_like(ctxt);
     struct x86_emulate_state state;
     int rc;
     uint8_t b, d, *opc = NULL;
@@ -1810,7 +1811,7 @@ x86_emulate(
             if ( ea.type == OP_REG )
                 src.val = *ea.reg;
             else if ( (rc = read_ulong(ea.mem.seg, ea.mem.off, &src.val,
-                                       (op_bytes == 2 && !amd_like(ctxt)
+                                       (op_bytes == 2 && !is_amd_like
                                         ? 2 : 4),
                                        ctxt, ops)) )
                 goto done;
@@ -2356,7 +2357,7 @@ x86_emulate(
 
     case 0xc2: /* ret imm16 (near) */
     case 0xc3: /* ret (near) */
-        op_bytes = (op_bytes == 4 || !amd_like(ctxt)) && mode_64bit()
+        op_bytes = (op_bytes == 4 || !is_amd_like) && mode_64bit()
                    ? 8 : op_bytes;
         if ( (rc = read_ulong(x86_seg_ss, sp_post_inc(op_bytes + src.val),
                               &dst.val, op_bytes, ctxt, ops)) != 0 ||
@@ -3089,7 +3090,7 @@ x86_emulate(
         if ( (rc = ops->read_msr(MSR_EFER, &msr_val, ctxt)) != X86EMUL_OKAY )
             goto done;
         generate_exception_if((msr_val & EFER_SCE) == 0, X86_EXC_UD);
-        generate_exception_if(!amd_like(ctxt) && !mode_64bit(), X86_EXC_UD);
+        generate_exception_if(!is_amd_like && !mode_64bit(), X86_EXC_UD);
 
         if ( (rc = ops->read_msr(MSR_STAR, &msr_val, ctxt)) != X86EMUL_OKAY )
             goto done;
@@ -3174,7 +3175,7 @@ x86_emulate(
         if ( (rc = ops->read_msr(MSR_EFER, &msr_val, ctxt)) != X86EMUL_OKAY )
             goto done;
         generate_exception_if(!(msr_val & EFER_SCE), X86_EXC_UD);
-        generate_exception_if(!amd_like(ctxt) && !mode_64bit(), X86_EXC_UD);
+        generate_exception_if(!is_amd_like && !mode_64bit(), X86_EXC_UD);
         generate_exception_if(!mode_ring0(), X86_EXC_GP, 0);
         generate_exception_if(!in_protmode(ctxt, ops), X86_EXC_GP, 0);
 #ifdef __x86_64__
@@ -3200,7 +3201,7 @@ x86_emulate(
         sreg.attr = 0xcf3; /* G+DB+P+DPL3+S+Data */
 
         /* Only the selector part of SS gets updated by AMD and alike. */
-        if ( amd_like(ctxt) )
+        if ( is_amd_like )
         {
             fail_if(!ops->read_segment);
             if ( (rc = ops->read_segment(x86_seg_ss, &sreg,
@@ -3924,7 +3925,7 @@ x86_emulate(
 
     case X86EMUL_OPC(0x0f, 0x34): /* sysenter */
         vcpu_must_have(sep);
-        generate_exception_if(amd_like(ctxt) && ctxt->lma, X86_EXC_UD);
+        generate_exception_if(is_amd_like && ctxt->lma, X86_EXC_UD);
         generate_exception_if(!in_protmode(ctxt, ops), X86_EXC_GP, 0);
 
         fail_if(ops->read_msr == NULL);
@@ -3973,7 +3974,7 @@ x86_emulate(
 
     case X86EMUL_OPC(0x0f, 0x35): /* sysexit */
         vcpu_must_have(sep);
-        generate_exception_if(amd_like(ctxt) && ctxt->lma, X86_EXC_UD);
+        generate_exception_if(is_amd_like && ctxt->lma, X86_EXC_UD);
         generate_exception_if(!mode_ring0(), X86_EXC_GP, 0);
         generate_exception_if(!in_protmode(ctxt, ops), X86_EXC_GP, 0);
 

base-commit: adbbbd47a1fad8e3bc1ab65c555f11d831fd6681
-- 
2.43.0




 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.