Skip to contentSkip to conversation

cross-vendor migration: Allow 32bit-apps run in 64bit guests

PATCH New Christoph Egger 1 participant 1 message Apr 6, 2009

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 cross-vendor migration: Allow 32bit-apps run in 64bit guests

Hi!

Attached patch makes cross-vendor migration demoable with 32bit applications in 64bit guests which use sysenter/sysexit/syscall instructions.

Changes the patch includes:
- SVM: intercept SYSENTER MSRs and emulate them
- SVM: use permament 64bit storage for SYSENTER MSR emulation. SYSENTER fields in VMCB are not "permament" since upper 32bits are undefined and get cleared when a VMRUN/VMEXIT is between a write and read access.
- SVM: improve debugging aid: dump CPU registers
- x86_emulate: emulate sysenter/sysexit/syscall instructions
- SVM/VMX: Intercept #UD and invoke instruction emulation
- x86_emulate: cache current vcpu through the whole emulation path

Signature · 6 lines
---to satisfy European Law for business letters:
Advanced Micro Devices GmbH
Karl-Hammerschmidt-Str. 34, 85609 Dornach b. Muenchen
Geschaeftsfuehrer: Jochen Polster, Thomas M. McCoy, Giuliano Meroni
Sitz: Dornach, Gemeinde Aschheim, Landkreis Muenchen
Registergericht Muenchen, HRB Nr. 43632
xen/arch/x86/hvm/emulate.c +7 -4
diff -r 5a60eb7fad79 xen/arch/x86/hvm/emulate.c
@@ -942,10 +942,10 @@ static struct x86_emulate_ops hvm_emulat
 };
 
 int hvm_emulate_one(
+    struct vcpu *curr,
     struct hvm_emulate_ctxt *hvmemul_ctxt)
 {
     struct cpu_user_regs *regs = hvmemul_ctxt->ctxt.regs;
-    struct vcpu *curr = current;
     uint32_t new_intr_shadow, pfec = PFEC_page_present;
     unsigned long addr;
     int rc;
@@ -1018,10 +1018,11 @@ int hvm_emulate_one(
 }
 
 void hvm_emulate_prepare(
+    struct vcpu *v,
     struct hvm_emulate_ctxt *hvmemul_ctxt,
     struct cpu_user_regs *regs)
 {
-    hvmemul_ctxt->intr_shadow = hvm_funcs.get_interrupt_shadow(current);
+    hvmemul_ctxt->intr_shadow = hvm_funcs.get_interrupt_shadow(v);
     hvmemul_ctxt->ctxt.regs = regs;
     hvmemul_ctxt->ctxt.force_writeback = 1;
     hvmemul_ctxt->seg_reg_accessed = 0;
@@ -1030,17 +1031,19 @@ void hvm_emulate_prepare(
     hvmemul_get_seg_reg(x86_seg_ss, hvmemul_ctxt);
 }
 
-void hvm_emulate_writeback(
+void hvm_emulate_writeback(struct vcpu *v,
     struct hvm_emulate_ctxt *hvmemul_ctxt)
 {
     enum x86_segment seg;
 
+    ASSERT((v == current) || !vcpu_runnable(v));
+
     seg = find_first_bit(&hvmemul_ctxt->seg_reg_dirty,
                          ARRAY_SIZE(hvmemul_ctxt->seg_reg));
 
     while ( seg < ARRAY_SIZE(hvmemul_ctxt->seg_reg) )
     {
-        hvm_set_segment_register(current, seg, &hvmemul_ctxt->seg_reg[seg]);
+        hvm_set_segment_register(v, seg, &hvmemul_ctxt->seg_reg[seg]);
         seg = find_next_bit(&hvmemul_ctxt->seg_reg_dirty,
                             ARRAY_SIZE(hvmemul_ctxt->seg_reg),
                             seg+1);
xen/arch/x86/hvm/io.c +3 -3
diff -r 5a60eb7fad79 xen/arch/x86/hvm/io.c
@@ -177,9 +177,9 @@ int handle_mmio(void)
     struct vcpu *curr = current;
     int rc;
 
-    hvm_emulate_prepare(&ctxt, guest_cpu_user_regs());
+    hvm_emulate_prepare(curr, &ctxt, guest_cpu_user_regs());
 
-    rc = hvm_emulate_one(&ctxt);
+    rc = hvm_emulate_one(curr, &ctxt);
 
     if ( curr->arch.hvm_vcpu.io_state == HVMIO_awaiting_completion )
         curr->arch.hvm_vcpu.io_state = HVMIO_handle_mmio_awaiting_completion;
@@ -206,7 +206,7 @@ int handle_mmio(void)
         break;
     }
 
-    hvm_emulate_writeback(&ctxt);
+    hvm_emulate_writeback(curr, &ctxt);
 
     return 1;
 }
xen/arch/x86/hvm/svm/svm.c +92 -7

153 lines not on this page · raw message

xen/arch/x86/hvm/svm/vmcb.c +3 -4
diff -r 5a60eb7fad79 xen/arch/x86/hvm/svm/vmcb.c
@@ -150,9 +150,6 @@ static int construct_vmcb(struct vcpu *v
     svm_disable_intercept_for_msr(v, MSR_LSTAR);
     svm_disable_intercept_for_msr(v, MSR_STAR);
     svm_disable_intercept_for_msr(v, MSR_SYSCALL_MASK);
-    svm_disable_intercept_for_msr(v, MSR_IA32_SYSENTER_CS);
-    svm_disable_intercept_for_msr(v, MSR_IA32_SYSENTER_ESP);
-    svm_disable_intercept_for_msr(v, MSR_IA32_SYSENTER_EIP);
 
     vmcb->msrpm_base_pa = (u64)virt_to_maddr(arch_svm->msrpm);
     vmcb->iopm_base_pa  = (u64)virt_to_maddr(hvm_io_bitmap);
@@ -222,7 +219,9 @@ static int construct_vmcb(struct vcpu *v
 
     paging_update_paging_modes(v);
 
-    vmcb->exception_intercepts = HVM_TRAP_MASK | (1U << TRAP_no_device);
+    vmcb->exception_intercepts = HVM_TRAP_MASK
+		| (1U << TRAP_no_device)
+		| (1U << TRAP_invalid_op);
 
     if ( paging_mode_hap(v->domain) )
     {
xen/arch/x86/hvm/vmx/realmode.c +3 -3
diff -r 5a60eb7fad79 xen/arch/x86/hvm/vmx/realmode.c
@@ -108,7 +108,7 @@ static void realmode_emulate_one(struct 
 
     perfc_incr(realmode_emulations);
 
-    rc = hvm_emulate_one(hvmemul_ctxt);
+    rc = hvm_emulate_one(curr, hvmemul_ctxt);
 
     if ( rc == X86EMUL_UNHANDLEABLE )
     {
@@ -179,7 +179,7 @@ void vmx_realmode(struct cpu_user_regs *
     if ( intr_info & INTR_INFO_VALID_MASK )
         __vmwrite(VM_ENTRY_INTR_INFO, 0);
 
-    hvm_emulate_prepare(&hvmemul_ctxt, regs);
+    hvm_emulate_prepare(curr, &hvmemul_ctxt, regs);
 
     if ( curr->arch.hvm_vcpu.io_state == HVMIO_completed )
         realmode_emulate_one(&hvmemul_ctxt);
@@ -243,7 +243,7 @@ void vmx_realmode(struct cpu_user_regs *
             (1ul << x86_seg_fs) | (1ul << x86_seg_gs);
     }
 
-    hvm_emulate_writeback(&hvmemul_ctxt);
+    hvm_emulate_writeback(curr, &hvmemul_ctxt);
 
     /* Re-instate VM_ENTRY_INTR_INFO if we did not discharge it. */
     if ( intr_info & INTR_INFO_VALID_MASK )
xen/arch/x86/hvm/vmx/vmcs.c +2 -1
diff -r 5a60eb7fad79 xen/arch/x86/hvm/vmx/vmcs.c
@@ -668,7 +668,8 @@ static int construct_vmcs(struct vcpu *v
     __vmwrite(EXCEPTION_BITMAP,
               HVM_TRAP_MASK
               | (paging_mode_hap(d) ? 0 : (1U << TRAP_page_fault))
-              | (1U << TRAP_no_device));
+              | (1U << TRAP_no_device)
+              | (1U << TRAP_invalid_op));
 
     v->arch.hvm_vcpu.guest_cr[0] = X86_CR0_PE | X86_CR0_ET;
     hvm_update_guest_cr(v, 0);
xen/arch/x86/hvm/vmx/vmx.c +35 -0
diff -r 5a60eb7fad79 xen/arch/x86/hvm/vmx/vmx.c
@@ -37,6 +37,7 @@
 #include <asm/spinlock.h>
 #include <asm/paging.h>
 #include <asm/p2m.h>
+#include <asm/hvm/emulate.h>
 #include <asm/hvm/hvm.h>
 #include <asm/hvm/support.h>
 #include <asm/hvm/vmx/vmx.h>
@@ -2229,6 +2230,37 @@ asmlinkage void vmx_enter_realmode(struc
     regs->eflags |= (X86_EFLAGS_VM | X86_EFLAGS_IOPL);
 }
 
+static void vmx_vmexit_ud_intercept(struct vcpu *v, struct cpu_user_regs *regs)
+{
+     struct hvm_emulate_ctxt ctxt;
+     int rc;
+ 
+     hvm_emulate_prepare(v, &ctxt, regs);
+ 
+     rc = hvm_emulate_one(v, &ctxt);
+ 
+     switch (rc) {
+     case X86EMUL_UNHANDLEABLE:
+         gdprintk(XENLOG_WARNING,
+                  "instruction emulation failed @ %04x:%lx: "
+                  "%02x %02x %02x %02x %02x %02x\n",
+                  hvmemul_get_seg_reg(x86_seg_cs, &ctxt)->sel,
+                  ctxt.insn_buf_eip,
+                  ctxt.insn_buf[0], ctxt.insn_buf[1],
+                  ctxt.insn_buf[2], ctxt.insn_buf[3],
+                  ctxt.insn_buf[4], ctxt.insn_buf[5]);
+          return;
+     case X86EMUL_EXCEPTION:
+         if ( ctxt.exn_pending )
+             hvm_inject_exception(ctxt.exn_vector, ctxt.exn_error_code, 0);
+         break;
+     default:
+         break;
+     }
+ 
+     hvm_emulate_writeback(v, &ctxt);
+}
+
 asmlinkage void vmx_vmexit_handler(struct cpu_user_regs *regs)
 {
     unsigned int exit_reason, idtv_info;
@@ -2410,6 +2442,9 @@ asmlinkage void vmx_vmexit_handler(struc
             HVMTRACE_0D(MCE);
             do_machine_check(regs);
             break;
+	case TRAP_invalid_op:
+            vmx_vmexit_ud_intercept(v, regs);
+            break;
         default:
             goto exit_and_crash;
         }
xen/arch/x86/x86_emulate.c +1 -0
diff -r 5a60eb7fad79 xen/arch/x86/x86_emulate.c
@@ -10,6 +10,7 @@
  */
 
 #include <asm/x86_emulate.h>
+#include <asm/msr-index.h>
 
 /* Avoid namespace pollution. */
 #undef cmpxchg
xen/arch/x86/x86_emulate/x86_emulate.c +375 -2

413 lines not on this page · raw message

xen/include/asm-x86/hvm/emulate.h +3 -1
diff -r 5a60eb7fad79 xen/include/asm-x86/hvm/emulate.h
@@ -36,11 +36,13 @@ struct hvm_emulate_ctxt {
 };
 
 int hvm_emulate_one(
+    struct vcpu *curr,
     struct hvm_emulate_ctxt *hvmemul_ctxt);
 void hvm_emulate_prepare(
+    struct vcpu *v,
     struct hvm_emulate_ctxt *hvmemul_ctxt,
     struct cpu_user_regs *regs);
-void hvm_emulate_writeback(
+void hvm_emulate_writeback(struct vcpu *v,
     struct hvm_emulate_ctxt *hvmemul_ctxt);
 struct segment_register *hvmemul_get_seg_reg(
     enum x86_segment seg,
xen/include/asm-x86/hvm/vcpu.h +9 -0
diff -r 5a60eb7fad79 xen/include/asm-x86/hvm/vcpu.h
@@ -39,6 +39,15 @@ struct hvm_vcpu {
     unsigned long       guest_cr[5];
     unsigned long       guest_efer;
 
+    /* On AMD: Upper four bytes are undefined in the VMCB, therefore we can't
+     * use the fields in the VMCB. Write a 64bit value and then read a 64bit
+     * value is fine unless there's a VMRUN/VMEXIT in between which clears
+     * the upper four bytes.
+     */
+    uint64_t guest_sysenter_cs;
+    uint64_t guest_sysenter_esp;
+    uint64_t guest_sysenter_eip;
+
     /*
      * Processor-visible control-register values, while guest executes.
      *  CR0, CR4: Used as a cache of VMCS contents by VMX only.
xen/include/public/arch-x86/hvm/save.h +1 -3
diff -r 5a60eb7fad79 xen/include/public/arch-x86/hvm/save.h
@@ -123,9 +123,7 @@ struct hvm_hw_cpu {
     uint32_t tr_arbytes;
     uint32_t ldtr_arbytes;
 
-    uint32_t sysenter_cs;
-    uint32_t padding0;
-
+    uint64_t sysenter_cs;
     uint64_t sysenter_esp;
     uint64_t sysenter_eip;
 

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