Skip to contentSkip to conversation

x86/emul: Make POPF emulation easier to follow

PATCH In review Andrew Cooper 2 participants 2 messages Dec 3, 2018 – Dec 4, 2018

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 x86/emul: Make POPF emulation easier to follow

---
CC: Jan Beulich <[email protected]>
CC: Wei Liu <[email protected]>
CC: Roger Pau Monné <[email protected]>

It's taken me nearly an hour with the vendor manuals to convince myself that the emulation is correct. The code is definitely too complicated to follow without some comments.

---
 xen/arch/x86/x86_emulate/x86_emulate.c | 11 +++++++++++
 1 file changed, 11 insertions(+)
xen/arch/x86/x86_emulate/x86_emulate.c +11 -0
index 6523d65..de8dae8 100644
@@ -4193,6 +4193,10 @@ x86_emulate(
         goto push;
 
     case 0x9d: /* popf */ {
+        /*
+         * Bits which may not be modified by this POP instruction. RF is
+         * handled uniformly during instruction retirement.
+         */
         uint32_t mask = X86_EFLAGS_VIP | X86_EFLAGS_VIF | X86_EFLAGS_VM;
 
         cr4 = 0;
@@ -4206,10 +4210,15 @@ x86_emulate(
                     if ( rc != X86EMUL_OKAY )
                         goto done;
                 }
+                /* All IOPL != 3 POPs fail, except a 16-bit POP in vm86 mode. */
                 generate_exception_if(!(cr4 & X86_CR4_VME) &&
                                       MASK_EXTR(_regs.eflags, X86_EFLAGS_IOPL) != 3,
                                       EXC_GP, 0);
             }
+            /*
+             * IOPL cannot be modified outside of CPL 0.  IF cannot be
+             * modified if IOPL < CPL.
+             */
             mask |= X86_EFLAGS_IOPL;
             if ( !mode_iopl() )
                 mask |= X86_EFLAGS_IF;
@@ -4222,7 +4231,9 @@ x86_emulate(
             goto done;
         if ( op_bytes == 2 )
         {
+            /* 16-bit POP preserves the upper 16 bits of EFLAGS. */
             dst.val = (uint16_t)dst.val | (_regs.eflags & 0xffff0000u);
+            /* VME processing only applies at IOPL != 3. */
             if ( (cr4 & X86_CR4_VME) &&
                  MASK_EXTR(_regs.eflags, X86_EFLAGS_IOPL) != 3 )
             {
Signature · 7 lines
2.1.4


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Reviewed-by on v1 1/1
#1 lore raw
Quoted 2 lines
Quoted 1 line
Quoted 1 line · On 03.12.18 at 20:23, <[email protected]> wrote:

With s/POP/POPF/g in all comments you add (in the first of them an alternative would be to drop the mnemonic altogether)

Jan

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