x86/emul: Emulate %cr8 accesses
cover x86/emul: Emulate %cr8 accesses
This very quickly turned into a rats nest. I don't have time to fix %cr8 handling, so stay with a stopgap mitigation for introspection puporses, and leave some notes for later.
Andrew Cooper (2):
x86/hvm: Rearrange the logic in hvmemul_{read,write}_cr() x86/emul: Emulate %cr8 accesses
xen/arch/x86/hvm/emulate.c | 39 ++++++++++++++++++++++------ xen/arch/x86/include/asm/x86-defns.h | 2 ++ 2 files changed, 33 insertions(+), 8 deletions(-)
Signature · 1 line
2.39.5
PATCH 1/2 x86/hvm: Rearrange the logic in hvmemul_{read,write}_cr()
In hvmemul_read_cr(), make the TRACE()/X86EMUL_OKAY path common in preparation for adding a %cr8 case. Use a local 'val' variable instead of always operating on a deferenced pointer.
In both, calculate curr once.
No functional change.
---
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Petr Beneš <[email protected]>
v2:
* New
--- xen/arch/x86/hvm/emulate.c | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-)
xen/arch/x86/hvm/emulate.c +16 -8
@@ -2273,23 +2273,30 @@ static int cf_check hvmemul_write_io( static int cf_check hvmemul_read_cr(unsigned int reg, -unsigned long *val, +unsigned long *_val,struct x86_emulate_ctxt *ctxt) { +struct vcpu *curr = current; +unsigned long val; +switch ( reg ) {case 0 :case 2 :case 3 :case 4 : - *val = current->arch.hvm.guest_cr[reg]; -TRACE (TRC_HVM_CR_READ64 , reg, *val, *val >>32 ); -return X86EMUL_OKAY ; -default : + val = curr->arch.hvm.guest_cr[reg];break ; + +default : +return X86EMUL_UNHANDLEABLE ; } -return X86EMUL_UNHANDLEABLE ; +TRACE (TRC_HVM_CR_READ64 , reg, val, val >>32 ); + + *_val = val; + +return X86EMUL_OKAY ; } static int cf_check hvmemul_write_cr(
@@ -2297,6 +2304,7 @@ static int cf_check hvmemul_write_cr(unsigned long val, struct x86_emulate_ctxt *ctxt) { + struct vcpu *curr = current;int rc;TRACE (TRC_HVM_CR_WRITE64 , reg, val, val >>32 );
@@ -2307,13 +2315,13 @@ static int cf_check hvmemul_write_cr(break ;case 2 : - current->arch.hvm.guest_cr[2 ] = val; + curr->arch.hvm.guest_cr[2 ] = val; rc =X86EMUL_OKAY ;break ;case 3 : { -bool noflush =hvm_pcid_enabled (current) && (val &X86_CR3_NOFLUSH ); +bool noflush =hvm_pcid_enabled (curr) && (val &X86_CR3_NOFLUSH );if ( noflush ) val &= ~X86_CR3_NOFLUSH ;
Signature · 1 line
2.39.5
Re: [PATCH 1/2] x86/hvm: Rearrange the logic in hvmemul_{read,write}_cr()
Quoted 9 lines · Andrew Cooper
In
hvmemul_read_cr(), make the TRACE()/X86EMUL_OKAY path common in preparation for adding a %cr8 case. Use a local 'val' variable instead of always operating on a deferenced pointer.In both, calculate curr once.
No functional change.
Signed-off-by Andrew Cooper <[email protected]>
with two suggestions:
xen/arch/x86/hvm/emulate.c static int cf_check hvmemul_write_io( · line 2276
Quoted 8 lines
xen/arch/x86/hvm/emulate.c +1
-1@@ -2273,23 +2273,30 @@ static int cf_check hvmemul_write_io( static int cf_check hvmemul_read_cr( unsigned int reg, - unsigned long *val, + unsigned long *_val,
Could I talk you into avoiding the leading underscore here, by using e.g. "pval" or "valp"?
xen/arch/x86/hvm/emulate.c static int cf_check hvmemul_write_io( · line 2279
Quoted 3 lines
xen/arch/x86/hvm/emulate.c +1
-0struct x86_emulate_ctxt *ctxt) { +struct vcpu *curr = current;
This can be pointer-to-const?
Jan
PATCH 2/2 x86/emul: Emulate %cr8 accesses
In reply to Andrew CooperPetr reports:
(XEN) MMIO emulation failed (1): d12v1 64bit @ 0010:fffff8057ba7dfbf -> 45 0f 20 c2 ...
during introspection.
This is MOV %cr8, which is wired up for hvm_mov_{to,from}_cr(); the VMExit fastpaths, but not for the full emulation slowpaths.
Xen's handling of %cr8 turns out to be quite wrong. At a minimum, we need storage for %cr8 separate to APIC_TPR, and to alter intercepts based on whether the vLAPIC is enabled or not. But that's more work than there is time for in the short term, so make a stopgap fix.
Extend hvmemul_{read,write}_cr() with %cr8 cases. Unlike hvm_mov_to_cr(), hardware hasn't filtered out invalid values (#GP checks are ahead of intercepts), so introduce X86_CR8_VALID_MASK.
---
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Petr Beneš <[email protected]>
v2:
* Check for reserved bits
In CPUs, CR8 has has 4 bits of storage, and can explicitly get out of sync of APIC_TPR if the the LAPIC is hardware-disabled in MSR_APIC_BASE. Writes to APIC_TPR don't get reflected back into CR8 (according to AMD at least). Both Intel and AMD accelerate TPR differently; SVM in the base architecture, and VT-x as the ~first optional extension.
--- xen/arch/x86/hvm/emulate.c | 15 +++++++++++++++ xen/arch/x86/include/asm/x86-defns.h | 2 ++ 2 files changed, 17 insertions(+)
xen/arch/x86/hvm/emulate.c +15 -0
@@ -2288,6 +2288,10 @@ static int cf_check hvmemul_read_cr( val = curr->arch.hvm.guest_cr[reg];break ; +case 8 : + val = (vlapic_get_reg (vcpu_vlapic (curr),APIC_TASKPRI ) &0xf0 ) >>4 ; +break ; +default :return X86EMUL_UNHANDLEABLE ; }
@@ -2333,6 +2337,17 @@ static int cf_check hvmemul_write_cr( rc =hvm_set_cr4 (val, true);break ; +case 8 : +if ( val & ~X86_CR8_VALID_MASK ) + { + rc =X86EMUL_EXCEPTION ; +break ; + } + +vlapic_set_reg (vcpu_vlapic (curr),APIC_TASKPRI , val <<4 ); + rc =X86EMUL_OKAY ; +break ; +default : rc =X86EMUL_UNHANDLEABLE ;break ;
xen/arch/x86/include/asm/x86-defns.h +2 -0
@@ -76,6 +76,8 @@#define X86_CR4_CET 0x00800000/* Control-flow Enforcement Technology */ #define X86_CR4_PKS 0x01000000/* Protection Key Supervisor */ +#define X86_CR8_VALID_MASK 0xf +/* * XSTATE component flags in XCR0 | MSR_XSS */
Signature · 1 line
2.39.5
Quoted 14 lines · Andrew Cooper
Petr reports:
(XEN) MMIO emulation failed (1): d12v1 64bit @ 0010:fffff8057ba7dfbf -> 45 0f 20 c2 ...
during introspection.
This is MOV %cr8, which is wired up for hvm_mov_{to,from}_cr(); the VMExit fastpaths, but not for the full emulation slowpaths.
Xen's handling of %cr8 turns out to be quite wrong. At a minimum, we need storage for %cr8 separate to
APIC_TPR, and to alter intercepts based on whether the vLAPIC is enabled or not. But that's more work than there is time for in the short term, so make a stopgap fix.
Quoted 6 lines
Extend hvmemul_{read,write}_cr() with %cr8 cases. Unlike
hvm_mov_to_cr(), hardware hasn't filtered out invalid values (#GP checks are ahead of intercepts), so introduceX86_CR8_VALID_MASK.Reported-by Petr Beneš <[email protected]>Signed-off-by Andrew Cooper <[email protected]>
xen/arch/x86/hvm/emulate.c static int cf_check hvmemul_read_cr( · line 2292
Quoted 8 lines
xen/arch/x86/hvm/emulate.c +2
-0@@ -2288,6 +2288,10 @@ static int cf_check hvmemul_read_cr( val = curr->arch.hvm.guest_cr[reg];break ; +case 8 : + val = (vlapic_get_reg (vcpu_vlapic (curr),APIC_TASKPRI ) &0xf0 ) >>4 ;
No new #define then to use MASK_EXTR() here and ...
xen/arch/x86/hvm/emulate.c static int cf_check hvmemul_write_cr( · line 2347
Quoted 12 lines
xen/arch/x86/hvm/emulate.c +8
-0@@ -2333,6 +2337,17 @@ static int cf_check hvmemul_write_cr( rc =hvm_set_cr4 (val, true);break ; +case 8 : +if ( val & ~X86_CR8_VALID_MASK ) + { + rc =X86EMUL_EXCEPTION ; +break ; + } + +vlapic_set_reg (vcpu_vlapic (curr),APIC_TASKPRI , val <<4 );
... MASK_INSR() here?
Jan
Quoted 15 lines · Jan Beulich
Quoted 14 lines · Andrew Cooper
Petr reports:
(XEN) MMIO emulation failed (1): d12v1 64bit @ 0010:fffff8057ba7dfbf -> 45 0f 20 c2 ...
during introspection.
This is MOV %cr8, which is wired up for hvm_mov_{to,from}_cr(); the VMExit fastpaths, but not for the full emulation slowpaths.
Xen's handling of %cr8 turns out to be quite wrong. At a minimum, we need storage for %cr8 separate to
APIC_TPR, and to alter intercepts based on whether the vLAPIC is enabled or not. But that's more work than there is time for in the short term, so make a stopgap fix.
Quoted 7 lines
Quoted 6 lines
Extend hvmemul_{read,write}_cr() with %cr8 cases. Unlike
hvm_mov_to_cr(), hardware hasn't filtered out invalid values (#GP checks are ahead of intercepts), so introduceX86_CR8_VALID_MASK.Reported-by Petr Beneš <[email protected]>Signed-off-by Andrew Cooper <[email protected]>Reviewed-by Jan Beulich <[email protected]>
Thanks.
Quoted 15 lines
xen/arch/x86/hvm/emulate.c static int cf_check hvmemul_read_cr( · line 2292
Quoted 9 lines
xen/arch/x86/hvm/emulate.c +2
-0@@ -2288,6 +2288,10 @@ static int cf_check hvmemul_read_cr( val = curr->arch.hvm.guest_cr[reg];break ; +case 8 : + val = (vlapic_get_reg (vcpu_vlapic (curr),APIC_TASKPRI ) &0xf0 ) >>4 ;No new #define then to use
MASK_EXTR()here and ...Quoted 4 lines
xen/arch/x86/hvm/emulate.c +0
-0@@ -2333,6 +2337,17 @@ static int cf_check hvmemul_write_cr( rc =hvm_set_cr4 (val, true);break ;
Quoted 9 lines
xen/arch/x86/hvm/emulate.c static int cf_check hvmemul_write_cr( · line 2347
Quoted 8 lines
xen/arch/x86/hvm/emulate.c +8
-0+case 8 : +if ( val & ~X86_CR8_VALID_MASK ) + { + rc =X86EMUL_EXCEPTION ; +break ; + } + +vlapic_set_reg (vcpu_vlapic (curr),APIC_TASKPRI , val <<4 );...
MASK_INSR()here?
No. The logic wont survive fixing cr8 I don't think.
AFAICT, what we need is plain storage, and vlapic_{get,set}_tpr() accessors which account for hw-disable, and then call back into hvm_set_reg() to adjust intercepts.
~Andrew