Skip to contentSkip to conversation

x86/hvm: Don't emulate all instructions hitting the #UD intercept

PATCH In review Andrew Cooper 2 participants 6 messages Dec 19, 2016 – Dec 20, 2016

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 x86/hvm: Don't emulate all instructions hitting the #UD intercept

Having the instruction emulator fill in all #UDs when using FEP is unhelpful when trying to test emulation behaviour against hardware.

Restrict emulation from the #UD intercept to the cross-vendor case, and when a postive Forced Emulation Prefix has been identified.

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

---
 xen/arch/x86/hvm/hvm.c | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)
xen/arch/x86/hvm/hvm.c +11 -1
index 73d24df..12a6f46 100644
@@ -4002,13 +4002,15 @@ int hvm_msr_write_intercept(unsigned int msr, uint64_t msr_content,
 
 void hvm_ud_intercept(struct cpu_user_regs *regs)
 {
+    struct vcpu *cur = current;
+    bool should_emulate =
+        cur->domain->arch.x86_vendor != boot_cpu_data.x86_vendor;
     struct hvm_emulate_ctxt ctxt;
 
     hvm_emulate_init_once(&ctxt, regs);
 
     if ( opt_hvm_fep )
     {
-        struct vcpu *cur = current;
         const struct segment_register *cs = &ctxt.seg_reg[x86_seg_cs];
         uint32_t walk = (ctxt.seg_reg[x86_seg_ss].attr.fields.dpl == 3)
             ? PFEC_user_mode : 0;
@@ -4032,9 +4034,17 @@ void hvm_ud_intercept(struct cpu_user_regs *regs)
                 regs->eip = regs->_eip;
 
             add_taint(TAINT_HVM_FEP);
+
+            should_emulate = true;
         }
     }
 
+    if ( !should_emulate )
+    {
+        hvm_inject_hw_exception(TRAP_invalid_op, X86_EVENT_NO_EC);
+        return;
+    }
+
     switch ( hvm_emulate_one(&ctxt) )
     {
     case X86EMUL_UNHANDLEABLE:
Signature · 7 lines
2.1.4


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

PATCH 2/2 x86/emul: Bugfixes to SYSCALL emulation

Introduce vendor_is() to allow emulation to have vendor-specific behaviour. Adjust the SYSCALL behaviour on Intel to raise #UD when executed outside of 64bit mode.

in_longmode() has different return semantics from rc, so use a separate integer for the purpose.

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

---
 xen/arch/x86/x86_emulate/x86_emulate.c | 54 ++++++++++++++++++++++++++++++----
 1 file changed, 49 insertions(+), 5 deletions(-)
xen/arch/x86/x86_emulate/x86_emulate.c +49 -5
index 165eebb..419c2da 100644
@@ -1327,6 +1327,45 @@ static bool vcpu_has(
 #define host_and_vcpu_must_have(feat) vcpu_must_have(feat)
 #endif
 
+#define X86_VENDOR_INTEL 0
+#define X86_VENDOR_AMD   2
+
+static bool vendor_is(
+    struct x86_emulate_ctxt *ctxt,
+    const struct x86_emulate_ops *ops,
+    int vendor)
+{
+    unsigned int eax = 0, ebx = 0, ecx = 0, edx = 0;
+    int rc = X86EMUL_OKAY;
+
+    fail_if(!ops->cpuid);
+    rc = ops->cpuid(&eax, &ebx, &ecx, &edx, ctxt);
+    if ( rc == X86EMUL_OKAY )
+    {
+        switch ( vendor )
+        {
+        case X86_VENDOR_INTEL:
+            return (ebx == 0x756e6547u && /* "GenuineIntel" */
+                    ecx == 0x6c65746eu &&
+                    edx == 0x49656e69u);
+
+        case X86_VENDOR_AMD:
+            return (ebx == 0x68747541u && /* "AuthenticAMD" */
+                    ecx == 0x444d4163u &&
+                    edx == 0x69746e65u);
+        default:
+            rc = ~X86EMUL_OKAY;
+            break;
+        }
+    }
+
+ done:
+    return rc == X86EMUL_OKAY;
+}
+
+#define vendor_is_intel() vendor_is(ctxt, ops, X86_VENDOR_INTEL)
+#define vendor_is_amd()   vendor_is(ctxt, ops, X86_VENDOR_AMD)
+
 static int
 in_longmode(
     struct x86_emulate_ctxt *ctxt,
@@ -4623,10 +4662,15 @@ x86_emulate(
         break;
     }
 
-    case X86EMUL_OPC(0x0f, 0x05): /* syscall */ {
+    case X86EMUL_OPC(0x0f, 0x05): /* syscall */
+    {
         uint64_t msr_content;
+#ifdef __x86_64__
+        int lm;
+#endif
 
-        generate_exception_if(!in_protmode(ctxt, ops), EXC_UD);
+        generate_exception_if(!in_protmode(ctxt, ops) ||
+                              (vendor_is_intel() && !mode_64bit()), EXC_UD);
 
         /* Inject #UD if syscall/sysret are disabled. */
         fail_if(ops->read_msr == NULL);
@@ -4645,10 +4689,10 @@ x86_emulate(
         sreg.attr.bytes = 0xc93; /* G+DB+P+S+Data */
 
 #ifdef __x86_64__
-        rc = in_longmode(ctxt, ops);
-        if ( rc < 0 )
+        lm = in_longmode(ctxt, ops);
+        if ( lm < 0 )
             goto cannot_emulate;
-        if ( rc )
+        if ( lm )
         {
             cs.attr.bytes = 0xa9b; /* L+DB+P+S+Code */
 
Signature · 7 lines
2.1.4


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

Re: [PATCH 2/2] x86/emul: Bugfixes to SYSCALL emulation

Quoted 4 lines
Quoted 1 line
Quoted 1 line · On 19.12.16 at 17:37, <[email protected]> wrote:

Introduce vendor_is() to allow emulation to have vendor-specific behaviour. Adjust the SYSCALL behaviour on Intel to raise #UD when executed outside of 64bit mode.

I'd rather not see us go this route. I've been carrying a patch making the vendor an input (not submitted so far due to other at least contextual prereqs missing when I last check, but sent out just a minute ago). What do you think?

Quoted 2 lines

in_longmode() has different return semantics from rc, so use a separate integer for the purpose.

The ugliness of the additional #ifdef doesn't seem to warrant this. What I've been thinking the other day though is: Why don't we put the whole SYSCALL emulation into a __XEN__ conditional (implying __x86_64__, i.e. allowing the inner ones to be removed)? That would likely also limit the impact of the pending register name changes which I hope to be able to submit soon (once enough prereqs have gone in).

Jan

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

#3 lore raw

Re: [PATCH 2/2] x86/emul: Bugfixes to SYSCALL emulation

Quoted 8 lines · Jan Beulich
Quoted 4 lines
Quoted 1 line
Quoted 1 line · On 19.12.16 at 17:37, <[email protected]> wrote:

Introduce vendor_is() to allow emulation to have vendor-specific behaviour. Adjust the SYSCALL behaviour on Intel to raise #UD when executed outside of 64bit mode.

I'd rather not see us go this route. I've been carrying a patch making the vendor an input (not submitted so far due to other at least contextual prereqs missing when I last check, but sent out just a minute ago). What do you think?

Rebasing on top of that would be far more simple.

Quoted 5 lines
Quoted 3 lines

in_longmode() has different return semantics from rc, so use a separate integer for the purpose.

The ugliness of the additional #ifdef doesn't seem to warrant this.

It is a matter of correctness. The only reason we exit from it cleanly is because the cannot_emulate path discards rc, while the other paths overwrite rc because of hitting the read_msr() or write_segment() hooks in each case.

Quoted 4 lines

What I've been thinking the other day though is: Why don't we put the whole SYSCALL emulation into a __XEN__ conditional (implying __x86_64__, i.e. allowing the inner ones to be removed)?

This depends on whether we think it is ever realistic to be able to be able to test things like this in the userspace harness. I am not sure we realistically can.

~Andrew

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

#4 lore raw

Re: [PATCH 2/2] x86/emul: Bugfixes to SYSCALL emulation

Quoted 11 lines
Quoted 1 line
Quoted 1 line · On 20.12.16 at 13:14, <[email protected]> wrote:
Quoted 5 lines · Jan Beulich
Quoted 1 line
Quoted 1 line
Quoted 1 line · On 19.12.16 at 17:37, <[email protected]> wrote:

What I've been thinking the other day though is: Why don't we put the whole SYSCALL emulation into a __XEN__ conditional (implying __x86_64__, i.e. allowing the inner ones to be removed)?

This depends on whether we think it is ever realistic to be able to be able to test things like this in the userspace harness. I am not sure we realistically can.

Well - it's all about register modifications, which we surely could test. But this would be a rather contrived test, the usefulness of which I would question.

Jan

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

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

Having the instruction emulator fill in all #UDs when using FEP is unhelpful when trying to test emulation behaviour against hardware.

Restrict emulation from the #UD intercept to the cross-vendor case, and when a postive Forced Emulation Prefix has been identified.

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