x86/emul: Adjust handling of CR8_LEGACY
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
@@ -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
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.
Signed-off-by Andrew Cooper <[email protected]>
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
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.
Signed-off-by Andrew Cooper <[email protected]>---
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@@ -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
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
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
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
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
-3case 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
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
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
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.
Signed-off-by Andrew Cooper <[email protected]>---
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@@ -780,12 +780,12 @@ decode_twobyte(struct x86_emulate_state *s, break;case
0x20: case0x22: /* 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;
case0x21: case0x23: /* 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
Teddy Astie
Reviewed-byLe 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.
Signed-off-by Andrew Cooper <[email protected]>---
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@@ -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 adjustmentQuoted 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.