Skip to contentSkip to conversation

x86emul: add "unblock NMI" retire flag

PATCH Superseded Jan Beulich 2 participants 5 messages Apr 11, 2017 – Apr 12, 2017

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 x86emul: add "unblock NMI" retire flag

No matter that we emulate IRET for (guest) real more only right now, we should get its effect on (virtual) NMI delivery right.

---
v2: Adjust x86_emulate_wrapper() accordingly.

xen/arch/x86/hvm/emulate.c +25 -20
@@ -1955,25 +1955,32 @@ static int _hvm_emulate_one(struct hvm_e
         memcpy(vio->mmio_insn, hvmemul_ctxt->insn_buf, vio->mmio_insn_bytes);
     }
 
-    if ( rc != X86EMUL_OKAY )
-        return rc;
+    new_intr_shadow = hvmemul_ctxt->intr_shadow;
 
-    if ( hvmemul_ctxt->ctxt.retire.singlestep )
-        hvm_inject_hw_exception(TRAP_debug, X86_EVENT_NO_EC);
+    /*
+     * IRET, if valid in the given context, clears NMI blocking
+     * (irrespective of rc).
+     */
+    if ( hvmemul_ctxt->ctxt.retire.unblock_nmi )
+        new_intr_shadow &= ~HVM_INTR_SHADOW_NMI;
 
-    new_intr_shadow = hvmemul_ctxt->intr_shadow;
+    if ( rc == X86EMUL_OKAY )
+    {
+        if ( hvmemul_ctxt->ctxt.retire.singlestep )
+            hvm_inject_hw_exception(TRAP_debug, X86_EVENT_NO_EC);
 
-    /* MOV-SS instruction toggles MOV-SS shadow, else we just clear it. */
-    if ( hvmemul_ctxt->ctxt.retire.mov_ss )
-        new_intr_shadow ^= HVM_INTR_SHADOW_MOV_SS;
-    else
-        new_intr_shadow &= ~HVM_INTR_SHADOW_MOV_SS;
-
-    /* STI instruction toggles STI shadow, else we just clear it. */
-    if ( hvmemul_ctxt->ctxt.retire.sti )
-        new_intr_shadow ^= HVM_INTR_SHADOW_STI;
-    else
-        new_intr_shadow &= ~HVM_INTR_SHADOW_STI;
+        /* MOV-SS instruction toggles MOV-SS shadow, else we just clear it. */
+        if ( hvmemul_ctxt->ctxt.retire.mov_ss )
+            new_intr_shadow ^= HVM_INTR_SHADOW_MOV_SS;
+        else
+            new_intr_shadow &= ~HVM_INTR_SHADOW_MOV_SS;
+
+        /* STI instruction toggles STI shadow, else we just clear it. */
+        if ( hvmemul_ctxt->ctxt.retire.sti )
+            new_intr_shadow ^= HVM_INTR_SHADOW_STI;
+        else
+            new_intr_shadow &= ~HVM_INTR_SHADOW_STI;
+    }
 
     if ( hvmemul_ctxt->intr_shadow != new_intr_shadow )
     {
@@ -1981,13 +1988,11 @@ static int _hvm_emulate_one(struct hvm_e
         hvm_funcs.set_interrupt_shadow(curr, new_intr_shadow);
     }
 
-    if ( hvmemul_ctxt->ctxt.retire.hlt &&
+    if ( rc == X86EMUL_OKAY && hvmemul_ctxt->ctxt.retire.hlt &&
          !hvm_local_events_need_delivery(curr) )
-    {
         hvm_hlt(regs->eflags);
-    }
 
-    return X86EMUL_OKAY;
+    return rc;
 }
 
 int hvm_emulate_one(
xen/arch/x86/x86_emulate/x86_emulate.c +11 -2
@@ -4002,6 +4002,7 @@ x86_emulate(
         uint32_t mask = X86_EFLAGS_VIP | X86_EFLAGS_VIF | X86_EFLAGS_VM;
 
         fail_if(!in_realmode(ctxt, ops));
+        ctxt->retire.unblock_nmi = true;
         if ( (rc = read_ulong(x86_seg_ss, sp_post_inc(op_bytes),
                               &eip, op_bytes, ctxt, ops)) ||
              (rc = read_ulong(x86_seg_ss, sp_post_inc(op_bytes),
@@ -7918,9 +7919,17 @@ int x86_emulate_wrapper(
 
     rc = x86_emulate(ctxt, ops);
 
-    /* Retire flags should only be set for successful instruction emulation. */
+    /*
+     * Most retire flags should only be set for successful instruction
+     * emulation.
+     */
     if ( rc != X86EMUL_OKAY )
-        ASSERT(ctxt->retire.raw == 0);
+    {
+        typeof(ctxt->retire) retire = ctxt->retire;
+
+        retire.unblock_nmi = false;
+        ASSERT(!retire.raw);
+    }
 
     /* All cases returning X86EMUL_EXCEPTION should have fault semantics. */
     if ( rc == X86EMUL_EXCEPTION )
xen/arch/x86/x86_emulate/x86_emulate.h +1 -0
@@ -490,6 +490,7 @@ struct x86_emulate_ctxt
             bool hlt:1;          /* Instruction HLTed. */
             bool mov_ss:1;       /* Instruction sets MOV-SS irq shadow. */
             bool sti:1;          /* Instruction sets STI irq shadow. */
+            bool unblock_nmi:1;  /* Instruction clears NMI blocking. */
             bool singlestep:1;   /* Singlestepping was active. */
         };
     } retire;

x86emul: add "unblock NMI" retire flag

No matter that we emulate IRET for (guest) real more only right now, we should get its effect on (virtual) NMI delivery right.

---
v2: Adjust x86_emulate_wrapper() accordingly.

xen/arch/x86/hvm/emulate.c +25 -20

63 lines not on this page · raw message

xen/arch/x86/x86_emulate/x86_emulate.c +11 -2
@@ -4002,6 +4002,7 @@ x86_emulate(
         uint32_t mask = X86_EFLAGS_VIP | X86_EFLAGS_VIF | X86_EFLAGS_VM;
 
         fail_if(!in_realmode(ctxt, ops));
+        ctxt->retire.unblock_nmi = true;
         if ( (rc = read_ulong(x86_seg_ss, sp_post_inc(op_bytes),
                               &eip, op_bytes, ctxt, ops)) ||
              (rc = read_ulong(x86_seg_ss, sp_post_inc(op_bytes),
@@ -7918,9 +7919,17 @@ int x86_emulate_wrapper(
 
     rc = x86_emulate(ctxt, ops);
 
-    /* Retire flags should only be set for successful instruction emulation. */
+    /*
+     * Most retire flags should only be set for successful instruction
+     * emulation.
+     */
     if ( rc != X86EMUL_OKAY )
-        ASSERT(ctxt->retire.raw == 0);
+    {
+        typeof(ctxt->retire) retire = ctxt->retire;
+
+        retire.unblock_nmi = false;
+        ASSERT(!retire.raw);
+    }
 
     /* All cases returning X86EMUL_EXCEPTION should have fault semantics. */
     if ( rc == X86EMUL_EXCEPTION )
xen/arch/x86/x86_emulate/x86_emulate.h +1 -0
@@ -490,6 +490,7 @@ struct x86_emulate_ctxt
             bool hlt:1;          /* Instruction HLTed. */
             bool mov_ss:1;       /* Instruction sets MOV-SS irq shadow. */
             bool sti:1;          /* Instruction sets STI irq shadow. */
+            bool unblock_nmi:1;  /* Instruction clears NMI blocking. */
             bool singlestep:1;   /* Singlestepping was active. */
         };
     } retire;

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

#1 lore raw
Quoted 19 lines · Jan Beulich

No matter that we emulate IRET for (guest) real more only right now, we should get its effect on (virtual) NMI delivery right.

---
v2: Adjust x86_emulate_wrapper() accordingly.

xen/arch/x86/hvm/emulate.c +1 -4
@@ -1955,25 +1955,32 @@ static int _hvm_emulate_one(struct hvm_e
         memcpy(vio->mmio_insn, hvmemul_ctxt->insn_buf, vio->mmio_insn_bytes);
     }
 
-    if ( rc != X86EMUL_OKAY )
-        return rc;
+    new_intr_shadow = hvmemul_ctxt->intr_shadow;
 
-    if ( hvmemul_ctxt->ctxt.retire.singlestep )
-        hvm_inject_hw_exception(TRAP_debug, X86_EVENT_NO_EC);

xen/arch/x86/hvm/emulate.c static int _hvm_emulate_one(struct hvm_e · line 1968

Quoted 10 lines
xen/arch/x86/hvm/emulate.c +8 -1
+    /*
+     * IRET, if valid in the given context, clears NMI blocking
+     * (irrespective of rc).
+     */
+    if ( hvmemul_ctxt->ctxt.retire.unblock_nmi )
+        new_intr_shadow &= ~HVM_INTR_SHADOW_NMI;
 
-    new_intr_shadow = hvmemul_ctxt->intr_shadow;
+    if ( rc == X86EMUL_OKAY )
+    {

On further thought, given the assertion, you don't need to introduce this check, and can avoid the block indentation. It should make the patch rather smaller.

Quoted 30 lines
xen/arch/x86/hvm/emulate.c +14 -11
+        if ( hvmemul_ctxt->ctxt.retire.singlestep )
+            hvm_inject_hw_exception(TRAP_debug, X86_EVENT_NO_EC);
 
-    /* MOV-SS instruction toggles MOV-SS shadow, else we just clear it. */
-    if ( hvmemul_ctxt->ctxt.retire.mov_ss )
-        new_intr_shadow ^= HVM_INTR_SHADOW_MOV_SS;
-    else
-        new_intr_shadow &= ~HVM_INTR_SHADOW_MOV_SS;
-
-    /* STI instruction toggles STI shadow, else we just clear it. */
-    if ( hvmemul_ctxt->ctxt.retire.sti )
-        new_intr_shadow ^= HVM_INTR_SHADOW_STI;
-    else
-        new_intr_shadow &= ~HVM_INTR_SHADOW_STI;
+        /* MOV-SS instruction toggles MOV-SS shadow, else we just clear it. */
+        if ( hvmemul_ctxt->ctxt.retire.mov_ss )
+            new_intr_shadow ^= HVM_INTR_SHADOW_MOV_SS;
+        else
+            new_intr_shadow &= ~HVM_INTR_SHADOW_MOV_SS;
+
+        /* STI instruction toggles STI shadow, else we just clear it. */
+        if ( hvmemul_ctxt->ctxt.retire.sti )
+            new_intr_shadow ^= HVM_INTR_SHADOW_STI;
+        else
+            new_intr_shadow &= ~HVM_INTR_SHADOW_STI;
+    }
 
     if ( hvmemul_ctxt->intr_shadow != new_intr_shadow )
     {

xen/arch/x86/hvm/emulate.c static int _hvm_emulate_one(struct hvm_e · line 1992

Quoted 6 lines
xen/arch/x86/hvm/emulate.c +1 -1
@@ -1981,13 +1988,11 @@ static int _hvm_emulate_one(struct hvm_e
         hvm_funcs.set_interrupt_shadow(curr, new_intr_shadow);
     }
 
-    if ( hvmemul_ctxt->ctxt.retire.hlt &&
+    if ( rc == X86EMUL_OKAY && hvmemul_ctxt->ctxt.retire.hlt &&
          !hvm_local_events_need_delivery(curr) )

Similarly, you don't need the OKAY check here.

Otherwise, Reviewed-by: Andrew Cooper <[email protected]>

Quoted 44 lines
xen/arch/x86/hvm/emulate.c +1 -3
-    {
         hvm_hlt(regs->eflags);
-    }
 
-    return X86EMUL_OKAY;
+    return rc;
 }
 
 int hvm_emulate_one(
xen/arch/x86/x86_emulate/x86_emulate.c +11 -2
@@ -4002,6 +4002,7 @@ x86_emulate(
         uint32_t mask = X86_EFLAGS_VIP | X86_EFLAGS_VIF | X86_EFLAGS_VM;
 
         fail_if(!in_realmode(ctxt, ops));
+        ctxt->retire.unblock_nmi = true;
         if ( (rc = read_ulong(x86_seg_ss, sp_post_inc(op_bytes),
                               &eip, op_bytes, ctxt, ops)) ||
              (rc = read_ulong(x86_seg_ss, sp_post_inc(op_bytes),
@@ -7918,9 +7919,17 @@ int x86_emulate_wrapper(
 
     rc = x86_emulate(ctxt, ops);
 
-    /* Retire flags should only be set for successful instruction emulation. */
+    /*
+     * Most retire flags should only be set for successful instruction
+     * emulation.
+     */
     if ( rc != X86EMUL_OKAY )
-        ASSERT(ctxt->retire.raw == 0);
+    {
+        typeof(ctxt->retire) retire = ctxt->retire;
+
+        retire.unblock_nmi = false;
+        ASSERT(!retire.raw);
+    }
 
     /* All cases returning X86EMUL_EXCEPTION should have fault semantics. */
     if ( rc == X86EMUL_EXCEPTION )

xen/arch/x86/x86_emulate/x86_emulate.h struct x86_emulate_ctxt · line 496

Quoted 7 lines
xen/arch/x86/x86_emulate/x86_emulate.h +1 -0
@@ -490,6 +490,7 @@ struct x86_emulate_ctxt
             bool hlt:1;          /* Instruction HLTed. */
             bool mov_ss:1;       /* Instruction sets MOV-SS irq shadow. */
             bool sti:1;          /* Instruction sets STI irq shadow. */
+            bool unblock_nmi:1;  /* Instruction clears NMI blocking. */
             bool singlestep:1;   /* Singlestepping was active. */
         };
     } retire;

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

#2 lore raw
Quoted 21 lines
Quoted 1 line
Quoted 1 line · On 11.04.17 at 18:09, <[email protected]> wrote:
Quoted 19 lines · Jan Beulich
xen/arch/x86/hvm/emulate.c +7 -4
@@ -1955,25 +1955,32 @@ static int _hvm_emulate_one(struct hvm_e
         memcpy(vio->mmio_insn, hvmemul_ctxt->insn_buf, vio->mmio_insn_bytes);
     }
 
-    if ( rc != X86EMUL_OKAY )
-        return rc;
+    new_intr_shadow = hvmemul_ctxt->intr_shadow;
 
-    if ( hvmemul_ctxt->ctxt.retire.singlestep )
-        hvm_inject_hw_exception(TRAP_debug, X86_EVENT_NO_EC);
+    /*
+     * IRET, if valid in the given context, clears NMI blocking
+     * (irrespective of rc).
+     */
+    if ( hvmemul_ctxt->ctxt.retire.unblock_nmi )
+        new_intr_shadow &= ~HVM_INTR_SHADOW_NMI;
 
Quoted 7 lines

xen/arch/x86/hvm/emulate.c static int _hvm_emulate_one(struct hvm_e · line 1968

Quoted 3 lines
xen/arch/x86/hvm/emulate.c +2 -1
-    new_intr_shadow = hvmemul_ctxt->intr_shadow;
+    if ( rc == X86EMUL_OKAY )
+    {

On further thought, given the assertion, you don't need to introduce this check, and can avoid the block indentation. It should make the patch rather smaller.

Hmm, I did consider this, but I feel uneasy doing so, as it leaves production builds in potentially bad shape (in case we have a path we don't ever execute in any of the routine testing done, but which someone nevertheless ends up coming down in a released product). Paul, you're the maintainer of the code, do you have an opinion either way?

Jan

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

#3 lore raw
Quoted 27 lines · Jan Beulich
Quoted 21 lines
Quoted 1 line
Quoted 1 line · On 11.04.17 at 18:09, <[email protected]> wrote:
Quoted 19 lines · Jan Beulich
xen/arch/x86/hvm/emulate.c +7 -4
@@ -1955,25 +1955,32 @@ static int _hvm_emulate_one(struct hvm_e
         memcpy(vio->mmio_insn, hvmemul_ctxt->insn_buf, vio->mmio_insn_bytes);
     }
 
-    if ( rc != X86EMUL_OKAY )
-        return rc;
+    new_intr_shadow = hvmemul_ctxt->intr_shadow;
 
-    if ( hvmemul_ctxt->ctxt.retire.singlestep )
-        hvm_inject_hw_exception(TRAP_debug, X86_EVENT_NO_EC);
+    /*
+     * IRET, if valid in the given context, clears NMI blocking
+     * (irrespective of rc).
+     */
+    if ( hvmemul_ctxt->ctxt.retire.unblock_nmi )
+        new_intr_shadow &= ~HVM_INTR_SHADOW_NMI;
 
Quoted 6 lines

xen/arch/x86/hvm/emulate.c static int _hvm_emulate_one(struct hvm_e · line 1968

Quoted 3 lines
xen/arch/x86/hvm/emulate.c +2 -1
-    new_intr_shadow = hvmemul_ctxt->intr_shadow;
+    if ( rc == X86EMUL_OKAY )
+    {

On further thought, given the assertion, you don't need to introduce this check, and can avoid the block indentation. It should make the patch rather smaller.

Quoted 6 lines

Hmm, I did consider this, but I feel uneasy doing so, as it leaves production builds in potentially bad shape (in case we have a path we don't ever execute in any of the routine testing done, but which someone nevertheless ends up coming down in a released product). Paul, you're the maintainer of the code, do you have an opinion either way?

Actually, on yet further thought, the reset nature of these conditions are actually necessary on the != OKAY path.

Consider "mov %eax, %ss; ud2a". Even on raising an exception for the ud2a, the SS shadow needs dropping.

~Andrew

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

#4 lore raw
Quoted 36 lines
Quoted 1 line
Quoted 1 line · On 12.04.17 at 10:06, <[email protected]> wrote:
Quoted 27 lines · Jan Beulich
Quoted 21 lines
Quoted 1 line
Quoted 1 line · On 11.04.17 at 18:09, <[email protected]> wrote:
Quoted 19 lines · Jan Beulich
xen/arch/x86/hvm/emulate.c +7 -4
@@ -1955,25 +1955,32 @@ static int _hvm_emulate_one(struct hvm_e
         memcpy(vio->mmio_insn, hvmemul_ctxt->insn_buf, vio->mmio_insn_bytes);
     }
 
-    if ( rc != X86EMUL_OKAY )
-        return rc;
+    new_intr_shadow = hvmemul_ctxt->intr_shadow;
 
-    if ( hvmemul_ctxt->ctxt.retire.singlestep )
-        hvm_inject_hw_exception(TRAP_debug, X86_EVENT_NO_EC);
+    /*
+     * IRET, if valid in the given context, clears NMI blocking
+     * (irrespective of rc).
+     */
+    if ( hvmemul_ctxt->ctxt.retire.unblock_nmi )
+        new_intr_shadow &= ~HVM_INTR_SHADOW_NMI;
 
Quoted 6 lines

xen/arch/x86/hvm/emulate.c static int _hvm_emulate_one(struct hvm_e · line 1968

Quoted 3 lines
xen/arch/x86/hvm/emulate.c +2 -1
-    new_intr_shadow = hvmemul_ctxt->intr_shadow;
+    if ( rc == X86EMUL_OKAY )
+    {

On further thought, given the assertion, you don't need to introduce this check, and can avoid the block indentation. It should make the patch rather smaller.

Quoted 6 lines

Hmm, I did consider this, but I feel uneasy doing so, as it leaves production builds in potentially bad shape (in case we have a path we don't ever execute in any of the routine testing done, but which someone nevertheless ends up coming down in a released product). Paul, you're the maintainer of the code, do you have an opinion either way?

Quoted 5 lines

Actually, on yet further thought, the reset nature of these conditions are actually necessary on the != OKAY path.

Consider "mov %eax, %ss; ud2a". Even on raising an exception for the ud2a, the SS shadow needs dropping.

Hmm, indeed (for EXCEPTION at least, not so sure about the others; quite certainly not for RETRY). Along the lines of the above this would then call for an "else" to the newly added "if ( rc == X86EMUL_OKAY )" though.

Jan

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