x86/emul: Move CPUID Faulting fault generation into the emulator
PATCH 1/1 x86/emul: Move CPUID Faulting fault generation into the emulator
In hindsight, this is a better position for it, as it avoids opencoding hvmemul_inject_hw_exception() in hvmemul_cpuid(), and reduces the requirements on other ops->cpuid() hooks wanting to implement cpuid faulting in the future.
---
CC: Jan Beulich <[email protected]>
CC: Wei Liu <[email protected]>
--- xen/arch/x86/hvm/emulate.c | 11 ----------- xen/arch/x86/x86_emulate/x86_emulate.c | 10 +++++++++- 2 files changed, 9 insertions(+), 12 deletions(-)
xen/arch/x86/hvm/emulate.c +0 -11
@@ -1556,18 +1556,7 @@ static int hvmemul_cpuid(*/ if ( ctxt->opcode ==X86EMUL_OPC (0x0f ,0xa2 ) &&hvm_check_cpuid_faulting (current) ) - { -struct hvm_emulate_ctxt *hvmemul_ctxt = - container_of(ctxt, struct hvm_emulate_ctxt, ctxt); - - hvmemul_ctxt->exn_pending =1 ; - hvmemul_ctxt->trap.vector = TRAP_gp_fault; - hvmemul_ctxt->trap.type =X86_EVENTTYPE_HW_EXCEPTION ; - hvmemul_ctxt->trap.error_code =0 ; - hvmemul_ctxt->trap.insn_len =0 ; -return X86EMUL_EXCEPTION ; - } hvm_funcs.cpuid_intercept (eax, ebx, ecx, edx);return X86EMUL_OKAY ;
xen/arch/x86/x86_emulate/x86_emulate.c +9 -1
@@ -5011,8 +5011,16 @@ x86_emulate(unsigned int eax = _regs.eax, ebx = _regs.ebx;unsigned int ecx = _regs.ecx, edx = _regs.edx;fail_if (ops->cpuid ==NULL ); -if ( (rc = ops->cpuid (&eax, &ebx, &ecx, &edx, ctxt)) !=0 ) +switch ( rc = ops->cpuid (&eax, &ebx, &ecx, &edx, ctxt) ) + { +case X86EMUL_OKAY : +break ; +case X86EMUL_EXCEPTION :/* CPUID Faulting active. */ +generate_exception_if (true,EXC_GP ,0 ); +/* unreachable */ +default : goto done; + } _regs.eax = eax; _regs.ebx = ebx; _regs.ecx = ecx; _regs.edx = edx;break ;
Signature · 7 lines
2.1.4 _______________________________________________ Xen-devel mailing list [email protected] https://lists.xen.org/xen-devel
Quoted 8 lines
Quoted 1 line
Quoted 1 line · On 26.10.16 at 13:09, <[email protected]> wrote:
xen/arch/x86/x86_emulate/x86_emulate.c +0
-1@@ -5011,8 +5011,16 @@ x86_emulate(unsigned int eax = _regs.eax, ebx = _regs.ebx;unsigned int ecx = _regs.ecx, edx = _regs.edx;fail_if (ops->cpuid ==NULL ); -if ( (rc = ops->cpuid (&eax, &ebx, &ecx, &edx, ctxt)) !=0 )
xen/arch/x86/x86_emulate/x86_emulate.c x86_emulate( · line 5023
Quoted 10 lines
xen/arch/x86/x86_emulate/x86_emulate.c +9
-0+switch ( rc = ops->cpuid (&eax, &ebx, &ecx, &edx, ctxt) ) + { +case X86EMUL_OKAY : +break ; +case X86EMUL_EXCEPTION :/* CPUID Faulting active. */ +generate_exception_if (true,EXC_GP ,0 ); +/* unreachable */ +default : goto done; + }
Since this makes the cpuid hook different from other hooks (where a return of X86EMUL_EXCEPTION means an exception was raised, not that the emulator should raise one), this should be accompanied by a comment adjustment in x86_emulate.h. Also I think this would end up easier to read as
rc = ops->cpuid(&eax, &ebx, &ecx, &edx, ctxt);
generate_exception_if(rc == X86EMUL_EXCEPTION, EXC_GP, 0);
if ( rc != X86EMUL_OKAY )
goto done;And then I think we should eventually alter X86EMUL_EXCEPTION:
This should be a macro taking both exception type and error code as arguments, at which point the raising of exceptions could be done solely by the emulator.
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
PATCH v2 1/1 x86/emul: Move CPUID Faulting fault generation into the emulator
In hindsight, this is a better position for it, as it avoids opencoding hvmemul_inject_hw_exception() in hvmemul_cpuid(), and reduces the requirements on other ops->cpuid() hooks wanting to implement cpuid faulting in the future.
---
CC: Jan Beulich <[email protected]>
CC: Wei Liu <[email protected]>
v2:
* Expand the cpuid() hook comment in x86_emulate.h
* Adjust how the exception is generated
--- xen/arch/x86/hvm/emulate.c | 11 ----------- xen/arch/x86/x86_emulate/x86_emulate.c | 5 ++++- xen/arch/x86/x86_emulate/x86_emulate.h | 7 ++++++- 3 files changed, 10 insertions(+), 13 deletions(-)
xen/arch/x86/hvm/emulate.c +0 -11
@@ -1556,18 +1556,7 @@ static int hvmemul_cpuid(*/ if ( ctxt->opcode ==X86EMUL_OPC (0x0f ,0xa2 ) &&hvm_check_cpuid_faulting (current) ) - { -struct hvm_emulate_ctxt *hvmemul_ctxt = - container_of(ctxt, struct hvm_emulate_ctxt, ctxt); - - hvmemul_ctxt->exn_pending =1 ; - hvmemul_ctxt->trap.vector = TRAP_gp_fault; - hvmemul_ctxt->trap.type =X86_EVENTTYPE_HW_EXCEPTION ; - hvmemul_ctxt->trap.error_code =0 ; - hvmemul_ctxt->trap.insn_len =0 ; -return X86EMUL_EXCEPTION ; - } hvm_funcs.cpuid_intercept (eax, ebx, ecx, edx);return X86EMUL_OKAY ;
xen/arch/x86/x86_emulate/x86_emulate.c +4 -1
@@ -5011,7 +5011,10 @@ x86_emulate(unsigned int eax = _regs.eax, ebx = _regs.ebx;unsigned int ecx = _regs.ecx, edx = _regs.edx;fail_if (ops->cpuid ==NULL ); -if ( (rc = ops->cpuid (&eax, &ebx, &ecx, &edx, ctxt)) !=0 ) + rc = ops->cpuid (&eax, &ebx, &ecx, &edx, ctxt); +generate_exception_if (rc ==X86EMUL_EXCEPTION , +EXC_GP ,0 );/* CPUID Faulting? */ +if ( rc !=X86EMUL_OKAY ) goto done; _regs.eax = eax; _regs.ebx = ebx; _regs.ecx = ecx; _regs.edx = edx;
xen/arch/x86/x86_emulate/x86_emulate.h +6 -1
@@ -352,7 +352,12 @@ struct x86_emulate_opsint (*wbinvd)(struct x86_emulate_ctxt *ctxt); -/* cpuid: Emulate CPUID via given set of EAX-EDX inputs/outputs. */ +/* +* cpuid: Emulate CPUID via given set of EAX-EDX inputs/outputs. +* +* May return X86EMUL_EXCEPTION, which causes the emulator to inject +* #GP[0]. Used to implement CPUID faulting. +*/ int (*cpuid)( unsigned int *eax, unsigned int *ebx,
Signature · 7 lines
2.1.4 _______________________________________________ Xen-devel mailing list [email protected] https://lists.xen.org/xen-devel
Quoted 5 lines · Andrew Cooper
In hindsight, this is a better position for it, as it avoids opencoding
hvmemul_inject_hw_exception()inhvmemul_cpuid(), and reduces the requirements on otherops->cpuid()hooks wanting to implement cpuid faulting in the future.Signed-off-by Andrew Cooper <[email protected]>
Subject to Jan's review / ack:
Quoted 73 lines
---
CC: Jan Beulich <[email protected]>
CC: Wei Liu <[email protected]>v2:
* Expand thecpuid()hook comment inx86_emulate.h
* Adjust how the exception is generated--- xen/arch/x86/hvm/emulate.c | 11 ----------- xen/arch/x86/x86_emulate/x86_emulate.c | 5 ++++- xen/arch/x86/x86_emulate/x86_emulate.h | 7 ++++++- 3 files changed, 10 insertions(+), 13 deletions(-)xen/arch/x86/hvm/emulate.c +0
-11@@ -1556,18 +1556,7 @@ static int hvmemul_cpuid(*/ if ( ctxt->opcode ==X86EMUL_OPC (0x0f ,0xa2 ) &&hvm_check_cpuid_faulting (current) ) - { -struct hvm_emulate_ctxt *hvmemul_ctxt = - container_of(ctxt, struct hvm_emulate_ctxt, ctxt); - - hvmemul_ctxt->exn_pending =1 ; - hvmemul_ctxt->trap.vector = TRAP_gp_fault; - hvmemul_ctxt->trap.type =X86_EVENTTYPE_HW_EXCEPTION ; - hvmemul_ctxt->trap.error_code =0 ; - hvmemul_ctxt->trap.insn_len =0 ; -return X86EMUL_EXCEPTION ; - } hvm_funcs.cpuid_intercept (eax, ebx, ecx, edx);return X86EMUL_OKAY ;xen/arch/x86/x86_emulate/x86_emulate.c +4
-1@@ -5011,7 +5011,10 @@ x86_emulate(unsigned int eax = _regs.eax, ebx = _regs.ebx;unsigned int ecx = _regs.ecx, edx = _regs.edx;fail_if (ops->cpuid ==NULL ); -if ( (rc = ops->cpuid (&eax, &ebx, &ecx, &edx, ctxt)) !=0 ) + rc = ops->cpuid (&eax, &ebx, &ecx, &edx, ctxt); +generate_exception_if (rc ==X86EMUL_EXCEPTION , +EXC_GP ,0 );/* CPUID Faulting? */ +if ( rc !=X86EMUL_OKAY ) goto done; _regs.eax = eax; _regs.ebx = ebx; _regs.ecx = ecx; _regs.edx = edx;xen/arch/x86/x86_emulate/x86_emulate.h +6
-1@@ -352,7 +352,12 @@ struct x86_emulate_opsint (*wbinvd)(struct x86_emulate_ctxt *ctxt); -/* cpuid: Emulate CPUID via given set of EAX-EDX inputs/outputs. */ +/* +* cpuid: Emulate CPUID via given set of EAX-EDX inputs/outputs. +* +* May return X86EMUL_EXCEPTION, which causes the emulator to inject +* #GP[0]. Used to implement CPUID faulting. +*/ int (*cpuid)( unsigned int *eax, unsigned int *ebx,Signature · 1 line
2.1.4
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
Quoted 7 lines
Quoted 1 line
Quoted 1 line · On 26.10.16 at 14:58, <[email protected]> wrote:
In hindsight, this is a better position for it, as it avoids opencoding
hvmemul_inject_hw_exception()inhvmemul_cpuid(), and reduces the requirements
on otherops->cpuid()hooks wanting to implement cpuid faulting in the future.Signed-off-by Andrew Cooper <[email protected]>
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel