Skip to contentSkip to conversation

x86/emul: Adjust handling of CR8_LEGACY

PATCH Superseded Andrew Cooper 3 participants 3 messages May 13 – May 15

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

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

The CR8_LEGACY feature was introduced in the K8 Revision F. It doesn't exist in prior revisions of the K8.

Furthermore, from APM Vol2 3.1.5 CR8 (Task Priority Register, TPR):

The AMD64 architecture introduces a new control register, CR8, defined as the task priority register (TPR).

Additionally, from APM Vol3 4 System Instructions MOV CRn:

CR8 can be read and written in 64-bit mode, using a REX prefix. CR8 can be read and written in all modes using a LOCK prefix instead of a REX prefix to specify the additional opcode bit.

i.e. the LOCK prefix serves as an alternative encoding for REX.R.

Switch decode_twobyte() from += 8 to |= 8 to better match the description given. Other indications that the encoding isn't additive are that the CR intercepts stop at 15, and that LOCK MOV CR8 generates #UD rather than becoming a CR0 access.

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

v2:
* No dependency on LM.

Also, designers never put an ADD into silicon if they can possibly avoid it, because it's large and slow compared to the single OR gate needed in this case.

---
 xen/arch/x86/x86_emulate/decode.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
xen/arch/x86/x86_emulate/decode.c +2 -2
index 2c13356c4d17..51c89ddfc893 100644
@@ -782,10 +782,10 @@ decode_twobyte(struct x86_emulate_state *s,
     case 0x20: case 0x22: /* mov to/from cr */
         if ( s->lock_prefix && vcpu_has_cr8_legacy() )
         {
-            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
Acked-by on v2 1/1
#1 lore raw
Quoted 14 lines · Andrew Cooper

The CR8_LEGACY feature was introduced in the K8 Revision F. It doesn't exist in prior revisions of the K8.

Furthermore, from APM Vol2 3.1.5 CR8 (Task Priority Register, TPR):

The AMD64 architecture introduces a new control register, CR8, defined as the task priority register (TPR).

Additionally, from APM Vol3 4 System Instructions MOV CRn:

CR8 can be read and written in 64-bit mode, using a REX prefix. CR8 can be read and written in all modes using a LOCK prefix instead of a REX prefix to specify the additional opcode bit.

Quoted 8 lines

i.e. the LOCK prefix serves as an alternative encoding for REX.R.

Switch decode_twobyte() from += 8 to |= 8 to better match the description given. Other indications that the encoding isn't additive are that the CR intercepts stop at 15, and that LOCK MOV CR8 generates #UD rather than becoming a CR0 access.

Reviewed-by on v2 1/1
#2 lore raw
In reply to Andrew Cooper

Le 13/05/2026 à 19:18, Andrew Cooper a écrit :

Quoted 43 lines

The CR8_LEGACY feature was introduced in the K8 Revision F. It doesn't exist in prior revisions of the K8.

Furthermore, from APM Vol2 3.1.5 CR8 (Task Priority Register, TPR):

The AMD64 architecture introduces a new control register, CR8, defined as the task priority register (TPR).

Additionally, from APM Vol3 4 System Instructions MOV CRn:

CR8 can be read and written in 64-bit mode, using a REX prefix. CR8 can be read and written in all modes using a LOCK prefix instead of a REX prefix to specify the additional opcode bit.

i.e. the LOCK prefix serves as an alternative encoding for REX.R.

Switch decode_twobyte() from += 8 to |= 8 to better match the description given. Other indications that the encoding isn't additive are that the CR intercepts stop at 15, and that LOCK MOV CR8 generates #UD rather than becoming a CR0 access.

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

v2:
* No dependency on LM.

Also, designers never put an ADD into silicon if they can possibly avoid it, because it's large and slow compared to the single OR gate needed in this case.

---
  xen/arch/x86/x86_emulate/decode.c | 4 ++--
  1 file changed, 2 insertions(+), 2 deletions(-)
xen/arch/x86/x86_emulate/decode.c +0 -0
index 2c13356c4d17..51c89ddfc893 100644
@@ -782,10 +782,10 @@ decode_twobyte(struct x86_emulate_state *s,
      case 0x20: case 0x22: /* mov to/from cr */
          if ( s->lock_prefix && vcpu_has_cr8_legacy() )

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);

Teddy