Skip to contentSkip to conversation

Use virtual 8086 mode for VMX guests with CR0.PE == 0

PATCH In review Tim Deegan 2 participants 2 messages Dec 9, 2008 – Dec 10, 2008

lore
Share & tools
lore

Tim Deegan

author
#0 lore raw

PATCH 1/1 Use virtual 8086 mode for VMX guests with CR0.PE == 0

When a VMX guest tries to enter real mode, put it in virtual 8086 mode instead, if that's possible. Handle all errors and corner cases by falling back to the real-mode emulator.

This is similar to the old VMXASSIST system except it uses Xen's x86_emulate emulator instead of having a partial emulator in the guest firmware. It more than doubles the speed of real-mode operation on VMX.

tools/firmware/hvmloader/hvmloader.c +19 -0
diff -r 5535efd8e011 tools/firmware/hvmloader/hvmloader.c
@@ -536,6 +536,23 @@ static uint16_t init_xen_platform_io_bas
     return bios_info->xen_pfiob;
 }
 
+/* Set up an empty TSS area for virtual 8086 mode to use. 
+ * The only important thing is that it musn't have any bits set 
+ * in the interrupt redirection bitmap, so all zeros will do.  */
+static void init_vm86_tss(void)
+{
+    uint32_t tss;
+    struct xen_hvm_param p;
+
+    tss = e820_malloc(128, 128);
+    memset((char *)tss, 0, 128);
+    p.domid = DOMID_SELF;
+    p.index = HVM_PARAM_VM86_TSS;
+    p.value = tss;
+    hypercall_hvm_op(HVMOP_set_param, &p);
+    printf("vm86 TSS at %08x\n", tss);
+}
+
 int main(void)
 {
     int option_rom_sz = 0, vgabios_sz = 0, etherboot_sz = 0;
@@ -605,6 +622,8 @@ int main(void)
         printf("Loading ACPI ...\n");
         acpi_build_tables();
     }
+
+    init_vm86_tss();
 
     cmos_write_memory_size();
 
tools/libxc/xc_domain_restore.c +16 -0
diff -r 5535efd8e011 tools/libxc/xc_domain_restore.c
@@ -490,6 +490,22 @@ int xc_domain_restore(int xc_handle, int
             continue;
         }
 
+        if ( j == -4 )
+        {
+            uint64_t vm86_tss;
+
+            /* Skip padding 4 bytes then read the vm86 TSS location. */
+            if ( read_exact(io_fd, &vm86_tss, sizeof(uint32_t)) ||
+                 read_exact(io_fd, &vm86_tss, sizeof(uint64_t)) )
+            {
+                ERROR("error read the address of the vm86 TSS");
+                goto out;
+            }
+
+            xc_set_hvm_param(xc_handle, dom, HVM_PARAM_VM86_TSS, vm86_tss);
+            continue;
+        }
+
         if ( j == 0 )
             break;  /* our work here is done */
 
tools/libxc/xc_domain_save.c +19 -7
diff -r 5535efd8e011 tools/libxc/xc_domain_save.c
@@ -1388,18 +1388,30 @@ int xc_domain_save(int xc_handle, int io
     if ( hvm )
     {
         struct {
-            int minusthree;
+            int id;
             uint32_t pad;
-            uint64_t ident_pt;
-        } chunk = { -3, 0 };
-
+            uint64_t data;
+        } chunk = { 0, };
+
+        chunk.id = -3;
         xc_get_hvm_param(xc_handle, dom, HVM_PARAM_IDENT_PT,
-                         (unsigned long *)&chunk.ident_pt);
-
-        if ( (chunk.ident_pt != 0) &&
+                         (unsigned long *)&chunk.data);
+
+        if ( (chunk.data != 0) &&
              write_exact(io_fd, &chunk, sizeof(chunk)) )
         {
             PERROR("Error when writing the ident_pt for EPT guest");
+            goto out;
+        }
+
+        chunk.id = -4;
+        xc_get_hvm_param(xc_handle, dom, HVM_PARAM_VM86_TSS,
+                         (unsigned long *)&chunk.data);
+
+        if ( (chunk.data != 0) &&
+             write_exact(io_fd, &chunk, sizeof(chunk)) )
+        {
+            PERROR("Error when writing the vm86 TSS for guest");
             goto out;
         }
     }
xen/arch/x86/hvm/vmx/entry.S +10 -4
diff -r 5535efd8e011 xen/arch/x86/hvm/vmx/entry.S
@@ -133,9 +133,15 @@ vmx_asm_do_vmentry:
         cmpl $0,(r(dx),r(ax),1)
         jnz  .Lvmx_process_softirqs
 
-        testb $0xff,VCPU_vmx_emul(r(bx))
-        jnz  .Lvmx_goto_realmode
-
+        testb $0xff,VCPU_vmx_emulate(r(bx))
+        jnz .Lvmx_goto_emulator
+        testb $0xff,VCPU_vmx_realmode(r(bx))
+        jz .Lvmx_not_realmode
+        cmpw $0,VCPU_vm86_seg_mask(r(bx))
+        jnz .Lvmx_goto_emulator
+        call_with_regs(vmx_enter_realmode) 
+
+.Lvmx_not_realmode:
         mov  VCPU_hvm_guest_cr2(r(bx)),r(ax)
         mov  r(ax),%cr2
         call vmx_trace_vmentry
@@ -189,7 +195,7 @@ vmx_asm_do_vmentry:
         call vm_launch_fail
         ud2
 
-.Lvmx_goto_realmode:
+.Lvmx_goto_emulator:
         sti
         call_with_regs(vmx_realmode)
         jmp  vmx_asm_do_vmentry
xen/arch/x86/hvm/vmx/realmode.c +21 -24

71 lines not on this page · raw message

xen/arch/x86/hvm/vmx/vmcs.c +32 -19

69 lines not on this page · raw message

xen/arch/x86/hvm/vmx/vmx.c +216 -34

348 lines not on this page · raw message

xen/arch/x86/x86_32/asm-offsets.c +3 -1
diff -r 5535efd8e011 xen/arch/x86/x86_32/asm-offsets.c
@@ -88,7 +88,9 @@ void __dummy__(void)
     BLANK();
 
     OFFSET(VCPU_vmx_launched, struct vcpu, arch.hvm_vmx.launched);
-    OFFSET(VCPU_vmx_emul, struct vcpu, arch.hvm_vmx.vmxemul);
+    OFFSET(VCPU_vmx_realmode, struct vcpu, arch.hvm_vmx.vmx_realmode);
+    OFFSET(VCPU_vmx_emulate, struct vcpu, arch.hvm_vmx.vmx_emulate);
+    OFFSET(VCPU_vm86_seg_mask, struct vcpu, arch.hvm_vmx.vm86_segment_mask);
     OFFSET(VCPU_hvm_guest_cr2, struct vcpu, arch.hvm_vcpu.guest_cr[2]);
     BLANK();
 
xen/arch/x86/x86_64/asm-offsets.c +3 -1
diff -r 5535efd8e011 xen/arch/x86/x86_64/asm-offsets.c
@@ -107,7 +107,9 @@ void __dummy__(void)
     BLANK();
 
     OFFSET(VCPU_vmx_launched, struct vcpu, arch.hvm_vmx.launched);
-    OFFSET(VCPU_vmx_emul, struct vcpu, arch.hvm_vmx.vmxemul);
+    OFFSET(VCPU_vmx_realmode, struct vcpu, arch.hvm_vmx.vmx_realmode);
+    OFFSET(VCPU_vmx_emulate, struct vcpu, arch.hvm_vmx.vmx_emulate);
+    OFFSET(VCPU_vm86_seg_mask, struct vcpu, arch.hvm_vmx.vm86_segment_mask);
     OFFSET(VCPU_hvm_guest_cr2, struct vcpu, arch.hvm_vcpu.guest_cr[2]);
     BLANK();
 
xen/arch/x86/x86_emulate/x86_emulate.h +1 -0
diff -r 5535efd8e011 xen/arch/x86/x86_emulate/x86_emulate.h
@@ -67,6 +67,7 @@ typedef union segment_attributes {
         uint16_t l:   1;    /* 9;  Bit 53 */
         uint16_t db:  1;    /* 10; Bit 54 */
         uint16_t g:   1;    /* 11; Bit 55 */
+        uint16_t pad: 4;
     } fields;
 } __attribute__ ((packed)) segment_attributes_t;
 
xen/include/asm-x86/hvm/vmx/vmcs.h +9 -4
diff -r 5535efd8e011 xen/include/asm-x86/hvm/vmx/vmcs.h
@@ -109,11 +109,16 @@ struct arch_vmx_struct {
 
     unsigned long        host_cr0;
 
+    /* Is the guest in real mode? */
+    uint8_t              vmx_realmode;
     /* Are we emulating rather than VMENTERing? */
-#define VMXEMUL_REALMODE 1  /* Yes, because CR0.PE == 0   */
-#define VMXEMUL_BAD_CS   2  /* Yes, because CS.RPL != CPL */
-#define VMXEMUL_BAD_SS   4  /* Yes, because SS.RPL != CPL */
-    uint8_t              vmxemul;
+    uint8_t              vmx_emulate;
+    /* Bitmask of segments that we can't safely use in virtual 8086 mode */
+    uint16_t             vm86_segment_mask;
+    /* Shadow CS, SS, DS, ES, FS, GS, TR while in virtual 8086 mode */
+    struct segment_register vm86_saved_seg[x86_seg_tr + 1];
+    /* Remember EFLAGS while in virtual 8086 mode */
+    uint32_t             vm86_saved_eflags;
 };
 
 int vmx_create_vmcs(struct vcpu *v);
xen/include/asm-x86/perfc_defn.h +3 -0
diff -r 5535efd8e011 xen/include/asm-x86/perfc_defn.h
@@ -127,4 +127,7 @@ PERFCOUNTER(mshv_wrmsr_tpr,             
 PERFCOUNTER(mshv_wrmsr_tpr,             "MS Hv wrmsr tpr")
 PERFCOUNTER(mshv_wrmsr_eoi,             "MS Hv wrmsr eoi")
 
+PERFCOUNTER(realmode_emulations, "realmode instructions emulated")
+PERFCOUNTER(realmode_exits,      "vmexits from realmode")
+
 /*#endif*/ /* __XEN_PERFC_DEFN_H__ */
xen/include/public/hvm/params.h +4 -1
diff -r 5535efd8e011 xen/include/public/hvm/params.h
@@ -100,6 +100,9 @@
 /* ACPI S state: currently support S0 and S3 on x86. */
 #define HVM_PARAM_ACPI_S_STATE 14
 
-#define HVM_NR_PARAMS          15
+/* TSS used on Intel when CR0.PE=0. */
+#define HVM_PARAM_VM86_TSS     15
+
+#define HVM_NR_PARAMS          16
 
 #endif /* __XEN_PUBLIC_HVM_PARAMS_H__ */

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel

#1 lore raw

Looks good. I tested it with OS/2 and Win 98, and there's no regressions in the realmode parts of their bootup, but quite a significant speedup of the boot process in both cases.

Cheers,
Trolle

Quoted 8 lines · Tim Deegan

When a VMX guest tries to enter real mode, put it in virtual 8086 mode instead, if that's possible. Handle all errors and corner cases by falling back to the real-mode emulator.

This is similar to the old VMXASSIST system except it uses Xen's x86_emulate emulator instead of having a partial emulator in the guest firmware. It more than doubles the speed of real-mode operation on VMX.

Quoted 8 lines

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel