Skip to contentSkip to conversation

x86/emul: Adjust handling of CR8_LEGACY

PATCH In review Andrew Cooper 3 participants 11 messages Jul 7 – Jul 8

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 x86/emul: Adjust handling of CR8_LEGACY

The APM description of the AltMovCR8 feature bit is:

"LOCK MOV CR0 means MOV CR8"

Adjust the decode logic to behave like this.

---
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Teddy Astie <[email protected]>

v3:
* Change yet again. A contact with information on the matter confirmed that it is a special case for Reg = 0, and not a general modifier to all Reg values.

---
 xen/arch/x86/x86_emulate/decode.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
xen/arch/x86/x86_emulate/decode.c +3 -3
index 2c13356c4d17..57f6baffb066 100644
@@ -780,12 +780,12 @@ decode_twobyte(struct x86_emulate_state *s,
         break;
 
     case 0x20: case 0x22: /* mov to/from cr */
-        if ( s->lock_prefix && vcpu_has_cr8_legacy() )
+        if ( s->lock_prefix && vcpu_has_cr8_legacy() && s->modrm_reg == 0 )
         {
-            s->modrm_reg += 8;
+            s->modrm_reg = 8;
             s->lock_prefix = false;
         }
-        /* fall through */
+        fallthrough;
     case 0x21: case 0x23: /* mov to/from dr */
         ASSERT(s->ea.type == OP_REG); /* Early operand adjustment ensures this. */
         generate_exception_if(s->lock_prefix, X86_EXC_UD);
Signature · 1 line
2.39.5
Reviewed-by on v3 1/1
#1 lore raw
Quoted 7 lines · Andrew Cooper

The APM description of the AltMovCR8 feature bit is:

"LOCK MOV CR0 means MOV CR8"

Adjust the decode logic to behave like this.

on the basis that ...

Quoted 4 lines

v3:
* Change yet again. A contact with information on the matter confirmed that it is a special case for Reg = 0, and not a general modifier to all Reg values.

... we simply have to trust this source, short of this being properly described in the APM.

Jan

#2 lore raw
In reply to Andrew Cooper

Le 07/07/2026 à 21:49, Andrew Cooper a écrit :

Quoted 30 lines

The APM description of the AltMovCR8 feature bit is:

"LOCK MOV CR0 means MOV CR8"

Adjust the decode logic to behave like this.

---
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Teddy Astie <[email protected]>

v3:
* Change yet again. A contact with information on the matter confirmed that it is a special case for Reg = 0, and not a general modifier to all Reg values.

---
  xen/arch/x86/x86_emulate/decode.c | 6 +++---
  1 file changed, 3 insertions(+), 3 deletions(-)
xen/arch/x86/x86_emulate/decode.c +1 -1
index 2c13356c4d17..57f6baffb066 100644
@@ -780,12 +780,12 @@ decode_twobyte(struct x86_emulate_state *s,
          break;
 
      case 0x20: case 0x22: /* mov to/from cr */
-        if ( s->lock_prefix && vcpu_has_cr8_legacy() )
+        if ( s->lock_prefix && vcpu_has_cr8_legacy() && s->modrm_reg == 0 )

xen/arch/x86/x86_emulate/decode.c decode_twobyte(struct x86_emulate_state *s, · line 791

Quoted 10 lines
xen/arch/x86/x86_emulate/decode.c +2 -2
          {
-            s->modrm_reg += 8;
+            s->modrm_reg = 8;
              s->lock_prefix = false;
          }
-        /* fall through */
+        fallthrough;
      case 0x21: case 0x23: /* mov to/from dr */
          ASSERT(s->ea.type == OP_REG); /* Early operand adjustment ensures this. */
          generate_exception_if(s->lock_prefix, X86_EXC_UD);

I'm fine with it; though we probably want to make sure we always advertise the AltMovCr8 feature bit as APM suggest users to always check for this before relying on this behavior.

Interestingly, I see this feature bit set on Intel hardware. Though, it's a reserved bit in SDM, and lock mov cr0 is defined as being #UD.

Which CPU model introduced this feature bit ? And does Intel behavior matches AMD's spec ?

Teddy

#3 lore raw
Quoted 15 lines · Teddy Astie

Le 07/07/2026 à 21:49, Andrew Cooper a écrit :

Quoted 14 lines
xen/arch/x86/x86_emulate/decode.c +2 -3
@@ -780,12 +780,12 @@ decode_twobyte(struct x86_emulate_state *s,
          break;
 
      case 0x20: case 0x22: /* mov to/from cr */
-        if ( s->lock_prefix && vcpu_has_cr8_legacy() )
+        if ( s->lock_prefix && vcpu_has_cr8_legacy() && s->modrm_reg == 0 )
          {
-            s->modrm_reg += 8;
+            s->modrm_reg = 8;
              s->lock_prefix = false;
          }
-        /* fall through */
Quoted 8 lines

xen/arch/x86/x86_emulate/decode.c decode_twobyte(struct x86_emulate_state *s, · line 791

Quoted 4 lines
xen/arch/x86/x86_emulate/decode.c +1 -0
+        fallthrough;
      case 0x21: case 0x23: /* mov to/from dr */
          ASSERT(s->ea.type == OP_REG); /* Early operand adjustment ensures this. */
          generate_exception_if(s->lock_prefix, X86_EXC_UD);

I'm fine with it; though we probably want to make sure we always advertise the AltMovCr8 feature bit as APM suggest users to always check for this before relying on this behavior.

Why would we set the flag when hardware doesn't have it? Note the vcpu_has_cr8_legacy() check there - we only engage this behavior if the guest sees the feature as available.

Jan

#4 lore raw

Le 08/07/2026 à 11:37, Jan Beulich a écrit :

Quoted 25 lines · Teddy Astie
Quoted 15 lines

Le 07/07/2026 à 21:49, Andrew Cooper a écrit :

Quoted 14 lines
xen/arch/x86/x86_emulate/decode.c +2 -3
@@ -780,12 +780,12 @@ decode_twobyte(struct x86_emulate_state *s,
           break;
 
       case 0x20: case 0x22: /* mov to/from cr */
-        if ( s->lock_prefix && vcpu_has_cr8_legacy() )
+        if ( s->lock_prefix && vcpu_has_cr8_legacy() && s->modrm_reg == 0 )
           {
-            s->modrm_reg += 8;
+            s->modrm_reg = 8;
               s->lock_prefix = false;
           }
-        /* fall through */
Quoted 8 lines

xen/arch/x86/x86_emulate/decode.c decode_twobyte(struct x86_emulate_state *s, · line 791

Quoted 4 lines
xen/arch/x86/x86_emulate/decode.c +1 -0
+        fallthrough;
       case 0x21: case 0x23: /* mov to/from dr */
           ASSERT(s->ea.type == OP_REG); /* Early operand adjustment ensures this. */
           generate_exception_if(s->lock_prefix, X86_EXC_UD);

I'm fine with it; though we probably want to make sure we always advertise the AltMovCr8 feature bit as APM suggest users to always check for this before relying on this behavior.

Quoted 4 lines

Why would we set the flag when hardware doesn't have it? Note the vcpu_has_cr8_legacy() check there - we only engage this behavior if the guest sees the feature as available.

Hmm, ok, though this bit is named as "AltMovCr8" in July 2025 APM; but we name it "cr8_legacy". Has the name of the feature bit changed in APM over time ?

Quoted 1 line

Jan

Teddy

#5 lore raw
Quoted 26 lines · Teddy Astie

Le 08/07/2026 à 11:37, Jan Beulich a écrit :

Quoted 25 lines · Teddy Astie
Quoted 23 lines

Le 07/07/2026 à 21:49, Andrew Cooper a écrit :

Quoted 8 lines
xen/arch/x86/x86_emulate/decode.c +1 -1
@@ -780,12 +780,12 @@ decode_twobyte(struct x86_emulate_state *s,
           break;
 
       case 0x20: case 0x22: /* mov to/from cr */
-        if ( s->lock_prefix && vcpu_has_cr8_legacy() )
+        if ( s->lock_prefix && vcpu_has_cr8_legacy() && s->modrm_reg == 0 )

xen/arch/x86/x86_emulate/decode.c decode_twobyte(struct x86_emulate_state *s, · line 791

Quoted 10 lines
xen/arch/x86/x86_emulate/decode.c +2 -2
           {
-            s->modrm_reg += 8;
+            s->modrm_reg = 8;
               s->lock_prefix = false;
           }
-        /* fall through */
+        fallthrough;
       case 0x21: case 0x23: /* mov to/from dr */
           ASSERT(s->ea.type == OP_REG); /* Early operand adjustment ensures this. */
           generate_exception_if(s->lock_prefix, X86_EXC_UD);

I'm fine with it; though we probably want to make sure we always advertise the AltMovCr8 feature bit as APM suggest users to always check for this before relying on this behavior.

Quoted 7 lines
Quoted 3 lines

Why would we set the flag when hardware doesn't have it? Note the vcpu_has_cr8_legacy() check there - we only engage this behavior if the guest sees the feature as available.

Hmm, ok, though this bit is named as "AltMovCr8" in July 2025 APM; but we name it "cr8_legacy". Has the name of the feature bit changed in APM over time ?

I don't know. What I do know is that imo AltMovCr8 isn't the greatest of all possible names. (Neither is cr8_legacy, first and foremost as it alludes to this being a compat-mode-only thing when it isn't.)

Jan

#6 lore raw
Quoted 28 lines · Jan Beulich
Quoted 27 lines · Teddy Astie

Le 08/07/2026 à 11:37, Jan Beulich a écrit :

Quoted 26 lines · Teddy Astie
Quoted 6 lines

Le 07/07/2026 à 21:49, Andrew Cooper a écrit :

Quoted 5 lines
xen/arch/x86/x86_emulate/decode.c +0 -0
@@ -780,12 +780,12 @@ decode_twobyte(struct x86_emulate_state *s,
           break;
 
Quoted 16 lines

xen/arch/x86/x86_emulate/decode.c decode_twobyte(struct x86_emulate_state *s, · line 791

Quoted 13 lines
xen/arch/x86/x86_emulate/decode.c +3 -3
       case 0x20: case 0x22: /* mov to/from cr */
-        if ( s->lock_prefix && vcpu_has_cr8_legacy() )
+        if ( s->lock_prefix && vcpu_has_cr8_legacy() && s->modrm_reg == 0 )
           {
-            s->modrm_reg += 8;
+            s->modrm_reg = 8;
               s->lock_prefix = false;
           }
-        /* fall through */
+        fallthrough;
       case 0x21: case 0x23: /* mov to/from dr */
           ASSERT(s->ea.type == OP_REG); /* Early operand adjustment ensures this. */
           generate_exception_if(s->lock_prefix, X86_EXC_UD);

I'm fine with it; though we probably want to make sure we always advertise the AltMovCr8 feature bit as APM suggest users to always check for this before relying on this behavior.

Why would we set the flag when hardware doesn't have it? Note the vcpu_has_cr8_legacy() check there - we only engage this behavior if the guest sees the feature as available.

Quoted 6 lines
Quoted 3 lines

Hmm, ok, though this bit is named as "AltMovCr8" in July 2025 APM; but we name it "cr8_legacy". Has the name of the feature bit changed in APM over time ?

I don't know. What I do know is that imo AltMovCr8 isn't the greatest of all possible names. (Neither is cr8_legacy, first and foremost as it alludes to this being a compat-mode-only thing when it isn't.)

We inherited the name CR8_LEGACY from Linux.

commit c458f45d2eddcafed054651363ffb5c830e5df5b
Author: Keir Fraser <[email protected]>
Date:   Sat Jan 8 10:48:09 2011
    libxc: Update AMD CPU feature flags 0x80000001:ECX for Xen tools
    This patch syncs-up AMD CPU feature flags 0x80000001:ECX in libxc with the latest Linux kernel.

renamed from ALTMOVCR to CR8_LEGACY

~Andrew

#7 lore raw
In reply to Teddy Astie
Quoted 3 lines · Teddy Astie

Interestingly, I see this feature bit set on Intel hardware. Though, it's a reserved bit in SDM, and lock mov cr0 is defined as being #UD.

On what particular piece of Intel hardware do you see the bit? I just looked over all of the Intel systems I have locally available, and none of them has the bit set. (The neighboring bit, ABM, is set on many of them.)

Jan

#8 lore raw

Le 08/07/2026 à 11:41, Jan Beulich a écrit :

Quoted 9 lines · Teddy Astie
Quoted 3 lines

Interestingly, I see this feature bit set on Intel hardware. Though, it's a reserved bit in SDM, and lock mov cr0 is defined as being #UD.

On what particular piece of Intel hardware do you see the bit? I just looked over all of the Intel systems I have locally available, and none of them has the bit set. (The neighboring bit, ABM, is set on many of them.)

Ah right, I was reading the thing wrongly; indeed, bit 4 is cleared on Intel.

Quoted 1 line

Jan

Teddy

#9 lore raw
In reply to Teddy Astie
Quoted 50 lines · Teddy Astie

Le 07/07/2026 à 21:49, Andrew Cooper a écrit :

Quoted 36 lines

The APM description of the AltMovCR8 feature bit is:

   "LOCK MOV CR0 means MOV CR8"

Adjust the decode logic to behave like this.

---
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Teddy Astie <[email protected]>

v3:
  * Change yet again.  A contact with information on the matter confirmed that
    it is a special case for Reg = 0, and not a general modifier to all Reg
    values.
---
  xen/arch/x86/x86_emulate/decode.c | 6 +++---

  1 file changed, 3 insertions(+), 3 deletions(-)
xen/arch/x86/x86_emulate/decode.c +0 -0
index 2c13356c4d17..57f6baffb066 100644
@@ -780,12 +780,12 @@ decode_twobyte(struct x86_emulate_state *s,
           break;

        case 0x20: case 0x22: /* mov to/from cr */
-        if ( s->lock_prefix && vcpu_has_cr8_legacy() )
+        if ( s->lock_prefix && vcpu_has_cr8_legacy() && s->modrm_reg == 0 )
          {
-            s->modrm_reg += 8;
+            s->modrm_reg = 8;

Quoted 8 lines

              s->lock_prefix = false;
          }
-        /* fall through */
+        fallthrough;
      case 0x21: case 0x23: /* mov to/from dr */ ASSERT(s->ea.type == OP_REG); /* Early operand adjustment ensures this. */
          generate_exception_if(s->lock_prefix, X86_EXC_UD);

I'm fine with it; though we probably want to make sure we always advertise the AltMovCr8 feature bit as APM suggest users to always check for this before relying on this behavior.

Quoted 1 line

Interestingly, I see this feature bit set on Intel hardware.

Are you mixing up the basic and extended feature leaves?  Intel does not have this behaviour and doesn't (to my knowledge) advertise the CPUID bit.

Quoted 5 lines

Though, it's a reserved bit in SDM, and lock mov cr0 is defined as being #UD.

Which CPU model introduced this feature bit ? And does Intel behavior matches AMD's spec ?

That was in the v2 commit message, but I took it out here.

AMD K8 Rev F.  Prior K8 revisions didn't have it.

~Andrew

Reviewed-by
#10 lore raw

Le 08/07/2026 à 11:57, Andrew Cooper a écrit :

Quoted 51 lines · Teddy Astie
Quoted 50 lines

Le 07/07/2026 à 21:49, Andrew Cooper a écrit :

Quoted 42 lines

The APM description of the AltMovCR8 feature bit is:

   "LOCK MOV CR0 means MOV CR8"

Adjust the decode logic to behave like this.

---
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Teddy Astie <[email protected]>

v3:
  * Change yet again.  A contact with information on the matter
confirmed that
    it is a special case for Reg = 0, and not a general modifier to
all Reg
    values.

---
   xen/arch/x86/x86_emulate/decode.c | 6 +++---
   1 file changed, 3 insertions(+), 3 deletions(-)
xen/arch/x86/x86_emulate/decode.c +3 -3
index 2c13356c4d17..57f6baffb066 100644
@@ -780,12 +780,12 @@ decode_twobyte(struct x86_emulate_state *s,
           break;
         case 0x20: case 0x22: /* mov to/from cr */
-        if ( s->lock_prefix && vcpu_has_cr8_legacy() )
+        if ( s->lock_prefix && vcpu_has_cr8_legacy() && s->modrm_reg
 == 0 )
           {
-            s->modrm_reg += 8;
+            s->modrm_reg = 8;
               s->lock_prefix = false;
           }
-        /* fall through */
+        fallthrough;
       case 0x21: case 0x23: /* mov to/from dr */
           ASSERT(s->ea.type == OP_REG); /* Early operand adjustment
Quoted 2 lines

ensures this. */
          generate_exception_if(s->lock_prefix, X86_EXC_UD);

I'm fine with it; though we probably want to make sure we always advertise the AltMovCr8 feature bit as APM suggest users to always check for this before relying on this behavior.

Quoted 5 lines
Quoted 1 line

Interestingly, I see this feature bit set on Intel hardware.

Are you mixing up the basic and extended feature leaves?  Intel does not have this behaviour and doesn't (to my knowledge) advertise the CPUID bit.

I was reading as "decimal" instead of "hexadecimal", hence things got shifted a bit. Looks like I wasn't fully woken up this morning.

Quoted 10 lines
Quoted 5 lines

Though, it's a reserved bit in SDM, and lock mov cr0 is defined as being #UD.

Which CPU model introduced this feature bit ? And does Intel behavior matches AMD's spec ?

That was in the v2 commit message, but I took it out here.

AMD K8 Rev F.  Prior K8 revisions didn't have it.

Ok thanks.

Quoted 1 line

~Andrew

That looks good to me. As a follow-up patch, it might be worth renaming cr8_legacy into alt_mov_cr8 to better match APM terminology.