Skip to contentSkip to conversation

x86/emul: Replace opencoded extraction of IOPL from eflags

PATCH In review Andrew Cooper 2 participants 6 messages Jan 6, 2017 – Jan 9, 2017

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 x86/emul: Replace opencoded extraction of IOPL from eflags

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

---
 xen/arch/x86/x86_emulate/x86_emulate.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
xen/arch/x86/x86_emulate/x86_emulate.c +1 -1
index 98ba7c5..c4ba79b 100644
@@ -1157,7 +1157,7 @@ _mode_iopl(
     int cpl = get_cpl(ctxt, ops);
     if ( cpl == -1 )
         return -1;
-    return (cpl <= ((ctxt->regs->_eflags >> 12) & 3));
+    return cpl <= MASK_EXTR(ctxt->regs->_eflags, EFLG_IOPL);
 }
 
 #define mode_ring0() ({                         \
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/pv: Check that emulate_privileged_op() didn't change TF

As the comment says, the guest shouldn't be able to change X86_EFLAGS_TF, although we don't care which value it currently has.

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

---
 xen/arch/x86/traps.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
xen/arch/x86/traps.c +2 -1
index e45ff71..6dbdaa0 100644
@@ -3395,7 +3395,8 @@ static int emulate_privileged_op(struct cpu_user_regs *regs)
      * Un-mirror virtualized state from EFLAGS.
      * Nothing we allow to be emulated can change TF, IF, or IOPL.
      */
-    ASSERT(!((regs->_eflags ^ eflags) & (X86_EFLAGS_IF | X86_EFLAGS_IOPL)));
+    ASSERT(!((regs->_eflags ^ eflags) &
+             (X86_EFLAGS_TF | X86_EFLAGS_IF | X86_EFLAGS_IOPL)));
     regs->_eflags |= X86_EFLAGS_IF;
     regs->_eflags &= ~X86_EFLAGS_IOPL;
 
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/pv: Check that emulate_privileged_op() didn't change TF

In reply to Andrew Cooper
Quoted 3 lines
Quoted 1 line
Quoted 1 line · On 06.01.17 at 21:11, <[email protected]> wrote:

As the comment says, the guest shouldn't be able to change X86_EFLAGS_TF, although we don't care which value it currently has.

If we don't care about its value, why bother checking? IF and IOPL are different, see XSA-202 (albeit with that workaround, strictly speaking we don't need those checks here anymore - the code was written long before XSA-202 was found).

xen/arch/x86/traps.c static int emulate_privileged_op(struct cpu_user_regs *regs) · line 3399

Quoted 9 lines
xen/arch/x86/traps.c +2 -1
@@ -3395,7 +3395,8 @@ static int emulate_privileged_op(struct cpu_user_regs *regs)
      * Un-mirror virtualized state from EFLAGS.
      * Nothing we allow to be emulated can change TF, IF, or IOPL.
      */
-    ASSERT(!((regs->_eflags ^ eflags) & (X86_EFLAGS_IF | X86_EFLAGS_IOPL)));
+    ASSERT(!((regs->_eflags ^ eflags) &
+             (X86_EFLAGS_TF | X86_EFLAGS_IF | X86_EFLAGS_IOPL)));

Actually I did intentionally remove the TF check (together with quite a bit of special handling of TF, as I think was present in early versions of the patch) during the re-base on top of 1d60efc574 ("x86/emul: Implement singlestep as a retire flag"), so I'd rather say the missing comment adjustment is what we want.

Or if we were to check flags the values of which we don't care about, then perhaps that should cover all flags which can't legitimately change?

Jan

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

#3 lore raw

Re: [PATCH 2/2] x86/pv: Check that emulate_privileged_op() didn't change TF

Quoted 23 lines · Jan Beulich
Quoted 3 lines
Quoted 1 line
Quoted 1 line · On 06.01.17 at 21:11, <[email protected]> wrote:

As the comment says, the guest shouldn't be able to change X86_EFLAGS_TF, although we don't care which value it currently has.

If we don't care about its value, why bother checking? IF and IOPL are different, see XSA-202 (albeit with that workaround, strictly speaking we don't need those checks here anymore - the code was written long before XSA-202 was found).

xen/arch/x86/traps.c static int emulate_privileged_op(struct cpu_user_regs *regs) · line 3399

Quoted 9 lines
xen/arch/x86/traps.c +2 -1
@@ -3395,7 +3395,8 @@ static int emulate_privileged_op(struct cpu_user_regs *regs)
      * Un-mirror virtualized state from EFLAGS.
      * Nothing we allow to be emulated can change TF, IF, or IOPL.
      */
-    ASSERT(!((regs->_eflags ^ eflags) & (X86_EFLAGS_IF | X86_EFLAGS_IOPL)));
+    ASSERT(!((regs->_eflags ^ eflags) &
+             (X86_EFLAGS_TF | X86_EFLAGS_IF | X86_EFLAGS_IOPL)));

Actually I did intentionally remove the TF check (together with quite a bit of special handling of TF, as I think was present in early versions of the patch) during the re-base on top of 1d60efc574 ("x86/emul: Implement singlestep as a retire flag"), so I'd rather say the missing comment adjustment is what we want.

Quoted 3 lines

Or if we were to check flags the values of which we don't care about, then perhaps that should cover all flags which can't legitimately change?

So something like

ASSERT(!((regs->_eflags ^ eflags) & ~X86_EFLAGS_ARITH_MASK));

Might as well check as much as we can, seeing as it is entirely free for us to do so here.

~Andrew

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

#4 lore raw

Re: [PATCH 2/2] x86/pv: Check that emulate_privileged_op() didn't change TF

Quoted 22 lines
Quoted 1 line
Quoted 1 line · On 09.01.17 at 11:48, <[email protected]> wrote:
Quoted 16 lines · Jan Beulich

xen/arch/x86/traps.c static int emulate_privileged_op(struct cpu_user_regs *regs) · line 3399

Quoted 10 lines
Quoted 1 line
Quoted 1 line · On 06.01.17 at 21:11, <[email protected]> wrote:
xen/arch/x86/traps.c +2 -1
@@ -3395,7 +3395,8 @@ static int emulate_privileged_op(struct cpu_user_regs *regs)
      * Un-mirror virtualized state from EFLAGS.
      * Nothing we allow to be emulated can change TF, IF, or IOPL.
      */
-    ASSERT(!((regs->_eflags ^ eflags) & (X86_EFLAGS_IF | X86_EFLAGS_IOPL)));
+    ASSERT(!((regs->_eflags ^ eflags) &
+             (X86_EFLAGS_TF | X86_EFLAGS_IF | X86_EFLAGS_IOPL)));

Actually I did intentionally remove the TF check (together with quite a bit of special handling of TF, as I think was present in early versions of the patch) during the re-base on top of 1d60efc574 ("x86/emul: Implement singlestep as a retire flag"), so I'd rather say the missing comment adjustment is what we want.

Quoted 3 lines

Or if we were to check flags the values of which we don't care about, then perhaps that should cover all flags which can't legitimately change?

Quoted 6 lines

So something like

ASSERT(!((regs->_eflags ^ eflags) & ~X86_EFLAGS_ARITH_MASK));

Might as well check as much as we can, seeing as it is entirely free for us to do so here.

That's a good idea I think, let's use this (together with a comment adjustment).

Jan

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