Skip to contentSkip to conversation

x86/emul: Correct the return value handling of VMFUNC

PATCH In review Andrew Cooper 2 participants 5 messages Dec 21, 2016 – Dec 22, 2016

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 x86/emul: Correct the return value handling of VMFUNC

The bracketing of x86_emulate() calling the ops->vmfunc() hook is wrong with respect to the assignment to rc, which can trip the new assertions in x86_emulate_wrapper().

The hvmemul_vmfunc() hook should only raise #UD if X86EMUL_EXCEPTION is returned. This is only a latent bug at the moment.

---
CC: Jan Beulich <[email protected]>
CC: Paul Durrant <[email protected]>

---
 xen/arch/x86/hvm/emulate.c             | 2 +-
 xen/arch/x86/x86_emulate/x86_emulate.c | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
xen/arch/x86/hvm/emulate.c +1 -1
index 24754d3..aa1b716 100644
@@ -1653,7 +1653,7 @@ static int hvmemul_vmfunc(
     if ( !hvm_funcs.altp2m_vcpu_emulate_vmfunc )
         return X86EMUL_UNHANDLEABLE;
     rc = hvm_funcs.altp2m_vcpu_emulate_vmfunc(ctxt->regs);
-    if ( rc != X86EMUL_OKAY )
+    if ( rc == X86EMUL_EXCEPTION )
         x86_emul_hw_exception(TRAP_invalid_op, X86_EVENT_NO_EC, ctxt);
 
     return rc;
xen/arch/x86/x86_emulate/x86_emulate.c +1 -1
index b6d7358..3076c0c 100644
@@ -4463,7 +4463,7 @@ x86_emulate(
             generate_exception_if(lock_prefix | rep_prefix() | (vex.pfx == vex_66),
                                   EXC_UD);
             fail_if(!ops->vmfunc);
-            if ( (rc = ops->vmfunc(ctxt) != X86EMUL_OKAY) )
+            if ( (rc = ops->vmfunc(ctxt)) != X86EMUL_OKAY )
                 goto done;
             goto no_writeback;
 
Signature · 7 lines
1.9.1


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
#1 lore raw

PATCH 2/2 x86/emul: Pass shadow register state to the vmfunc() hook

vmfunc can in principle modify register state, so should operate on the shadow register state rather than the starting state of emulation.

---
CC: Jan Beulich <[email protected]>
CC: Paul Durrant <[email protected]>

---
 xen/arch/x86/hvm/emulate.c             | 3 ++-
 xen/arch/x86/x86_emulate/x86_emulate.c | 2 +-
 xen/arch/x86/x86_emulate/x86_emulate.h | 1 +
 3 files changed, 4 insertions(+), 2 deletions(-)
xen/arch/x86/hvm/emulate.c +2 -1
index aa1b716..fae666a 100644
@@ -1646,13 +1646,14 @@ static int hvmemul_invlpg(
 }
 
 static int hvmemul_vmfunc(
+    struct cpu_user_regs *regs,
     struct x86_emulate_ctxt *ctxt)
 {
     int rc;
 
     if ( !hvm_funcs.altp2m_vcpu_emulate_vmfunc )
         return X86EMUL_UNHANDLEABLE;
-    rc = hvm_funcs.altp2m_vcpu_emulate_vmfunc(ctxt->regs);
+    rc = hvm_funcs.altp2m_vcpu_emulate_vmfunc(regs);
     if ( rc == X86EMUL_EXCEPTION )
         x86_emul_hw_exception(TRAP_invalid_op, X86_EVENT_NO_EC, ctxt);
 
xen/arch/x86/x86_emulate/x86_emulate.c +1 -1
index 3076c0c..c9ffc56 100644
@@ -4463,7 +4463,7 @@ x86_emulate(
             generate_exception_if(lock_prefix | rep_prefix() | (vex.pfx == vex_66),
                                   EXC_UD);
             fail_if(!ops->vmfunc);
-            if ( (rc = ops->vmfunc(ctxt)) != X86EMUL_OKAY )
+            if ( (rc = ops->vmfunc(&_regs, ctxt)) != X86EMUL_OKAY )
                 goto done;
             goto no_writeback;
 
xen/arch/x86/x86_emulate/x86_emulate.h +1 -0
index 75f57ba..d70b534 100644
@@ -448,6 +448,7 @@ struct x86_emulate_ops
 
     /* vmfunc: Emulate VMFUNC via given set of EAX ECX inputs */
     int (*vmfunc)(
+        struct cpu_user_regs *regs,
         struct x86_emulate_ctxt *ctxt);
 };
 
Signature · 7 lines
1.9.1


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
#2 lore raw

Re: [PATCH 2/2] x86/emul: Pass shadow register state to the vmfunc() hook

Quoted 5 lines
Quoted 1 line
Quoted 1 line · On 21.12.16 at 17:32, <[email protected]> wrote:

vmfunc can in principle modify register state, so should operate on the shadow register state rather than the starting state of emulation.

While in principle this is fine, I'd rather see the register state constified for now, to demonstrate it is not being modified. I'll submit my two remaining follow-up patches in a minute, and we can then discuss which of the two to take.

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#3 lore raw

Re: [PATCH 2/2] x86/emul: Pass shadow register state to the vmfunc() hook

Quoted 9 lines · Jan Beulich
Quoted 5 lines
Quoted 1 line
Quoted 1 line · On 21.12.16 at 17:32, <[email protected]> wrote:

vmfunc can in principle modify register state, so should operate on the shadow register state rather than the starting state of emulation.

While in principle this is fine, I'd rather see the register state constified for now, to demonstrate it is not being modified. I'll submit my two remaining follow-up patches in a minute, and we can then discuss which of the two to take.

The question here is how likely it is that new functionality for VMFUNC will be defined, which starts mutating the values.

I am not aware of anything new, so lets go with the const version for now (as it is one fewer parameters). If this changes in the future, we can easily switch back to passing the shadow register block.

~Andrew

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

Reviewed-by on v1 1/2
#4 lore raw
In reply to Andrew Cooper
Quoted 9 lines
Quoted 1 line
Quoted 1 line · On 21.12.16 at 17:32, <[email protected]> wrote:

The bracketing of x86_emulate() calling the ops->vmfunc() hook is wrong with respect to the assignment to rc, which can trip the new assertions in x86_emulate_wrapper().

The hvmemul_vmfunc() hook should only raise #UD if X86EMUL_EXCEPTION is returned. This is only a latent bug at the moment.

xen/arch/x86/hvm/emulate.c static int hvmemul_vmfunc( · line 1657

Quoted 9 lines
xen/arch/x86/hvm/emulate.c +1 -1
@@ -1653,7 +1653,7 @@ static int hvmemul_vmfunc(
     if ( !hvm_funcs.altp2m_vcpu_emulate_vmfunc )
         return X86EMUL_UNHANDLEABLE;
     rc = hvm_funcs.altp2m_vcpu_emulate_vmfunc(ctxt->regs);
-    if ( rc != X86EMUL_OKAY )
+    if ( rc == X86EMUL_EXCEPTION )
         x86_emul_hw_exception(TRAP_invalid_op, X86_EVENT_NO_EC, ctxt);

Great - saves me from submitting one of my follow-up patches.

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel