Skip to contentSkip to conversation

Nested Virtualization: svm specific implementation

PATCH In review Christoph Egger 2 participants 3 messages Nov 12, 2010 – Dec 2, 2010

lore
Share & tools
lore
#0 lore raw

PATCH 9/13 Nested Virtualization: svm specific implementation

Signature · 7 lines
-- 
---to satisfy European Law for business letters:
Advanced Micro Devices GmbH
Einsteinring 24, 85609 Dornach b. Muenchen
Geschaeftsfuehrer: Alberto Bozzo, Andrew Bowd
Sitz: Dornach, Gemeinde Aschheim, Landkreis Muenchen
Registergericht Muenchen, HRB Nr. 43632

# HG changeset patch
# User cegger
# Date 1289584001 -3600
Implement SVM specific part for Nested Virtualization

xen/arch/x86/hvm/svm/Makefile +2 -0
diff -r 54dd43df686b -r 3bfc06e2e41a xen/arch/x86/hvm/svm/Makefile
@@ -2,6 +2,8 @@ obj-y += asid.o
 obj-y += emulate.o
 obj-y += entry.o
 obj-y += intr.o
+obj-y += nestedsvm.o
 obj-y += svm.o
+obj-y += svmdebug.o
 obj-y += vmcb.o
 obj-y += vpmu.o
xen/arch/x86/hvm/svm/emulate.c +10 -0
diff -r 54dd43df686b -r 3bfc06e2e41a xen/arch/x86/hvm/svm/emulate.c
@@ -101,6 +101,11 @@ MAKE_INSTR(HLT,    1, 0xf4);
 MAKE_INSTR(INT3,   1, 0xcc);
 MAKE_INSTR(RDTSC,  2, 0x0f, 0x31);
 MAKE_INSTR(PAUSE,  1, 0x90);
+MAKE_INSTR(VMRUN,  3, 0x0f, 0x01, 0xd8);
+MAKE_INSTR(VMLOAD, 3, 0x0f, 0x01, 0xda);
+MAKE_INSTR(VMSAVE, 3, 0x0f, 0x01, 0xdb);
+MAKE_INSTR(STGI,   3, 0x0f, 0x01, 0xdc);
+MAKE_INSTR(CLGI,   3, 0x0f, 0x01, 0xdd);
 
 static const u8 *opc_bytes[INSTR_MAX_COUNT] = 
 {
@@ -114,6 +119,11 @@ static const u8 *opc_bytes[INSTR_MAX_COU
     [INSTR_INT3]   = OPCODE_INT3,
     [INSTR_RDTSC]  = OPCODE_RDTSC,
     [INSTR_PAUSE]  = OPCODE_PAUSE,
+    [INSTR_VMRUN]  = OPCODE_VMRUN,
+    [INSTR_VMLOAD] = OPCODE_VMLOAD,
+    [INSTR_VMSAVE] = OPCODE_VMSAVE,
+    [INSTR_STGI]   = OPCODE_STGI,
+    [INSTR_CLGI]   = OPCODE_CLGI,
 };
 
 static int fetch(struct vcpu *v, u8 *buf, unsigned long addr, int len)
xen/arch/x86/hvm/svm/entry.S +1 -0
diff -r 54dd43df686b -r 3bfc06e2e41a xen/arch/x86/hvm/svm/entry.S
@@ -54,6 +54,7 @@
 
 ENTRY(svm_asm_do_resume)
         call svm_intr_assist
+        call_with_regs(nsvm_vcpu_switch)
 
         get_current(bx)
         CLGI
xen/arch/x86/hvm/svm/nestedsvm.c new file +1185 -0

1,185 lines not on this page · raw message

xen/arch/x86/hvm/svm/svm.c +239 -27

422 lines not on this page · raw message

xen/arch/x86/hvm/svm/svmdebug.c new file +189 -0

189 lines not on this page · raw message

xen/arch/x86/hvm/svm/vmcb.c +3 -106

145 lines not on this page · raw message

xen/include/asm-x86/hvm/svm/emulate.h +5 -0
diff -r 54dd43df686b -r 3bfc06e2e41a xen/include/asm-x86/hvm/svm/emulate.h
@@ -32,6 +32,11 @@ enum instruction_index {
     INSTR_INT3,
     INSTR_RDTSC,
     INSTR_PAUSE,
+    INSTR_VMRUN,
+    INSTR_VMLOAD,
+    INSTR_VMSAVE,
+    INSTR_STGI,
+    INSTR_CLGI,
     INSTR_MAX_COUNT /* Must be last - Number of instructions supported */
 };
 
xen/include/asm-x86/hvm/svm/nestedsvm.h new file +119 -0

119 lines not on this page · raw message

xen/include/asm-x86/hvm/svm/svm.h +4 -2
diff -r 54dd43df686b -r 3bfc06e2e41a xen/include/asm-x86/hvm/svm/svm.h
@@ -29,8 +29,6 @@
 #include <asm/i387.h>
 #include <asm/hvm/vpmu.h>
 
-void svm_dump_vmcb(const char *from, struct vmcb_struct *vmcb);
-
 #define SVM_REG_EAX (0) 
 #define SVM_REG_ECX (1) 
 #define SVM_REG_EDX (2) 
@@ -62,6 +60,8 @@ static inline void svm_vmsave(void *vmcb
         : : "a" (__pa(vmcb)) : "memory" );
 }
 
+unsigned long *svm_msrbit(unsigned long *msr_bitmap, uint32_t msr);
+
 extern u32 svm_feature_flags;
 
 #define SVM_FEATURE_NPT     0
@@ -76,4 +76,6 @@ extern u32 svm_feature_flags;
 #define cpu_has_svm_nrips   test_bit(SVM_FEATURE_NRIPS, &svm_feature_flags)
 #define cpu_has_pause_filter  test_bit(SVM_FEATURE_PAUSEF, &svm_feature_flags)
 
+#define SVM_PAUSEFILTER_INIT    3000
+
 #endif /* __ASM_X86_HVM_SVM_H__ */
xen/include/asm-x86/hvm/svm/svmdebug.h new file +30 -0
diff -r 54dd43df686b -r 3bfc06e2e41a xen/include/asm-x86/hvm/svm/svmdebug.h
@@ -0,0 +1,30 @@
+/*
+ * svmdebug.h: SVM related debug defintions
+ * Copyright (c) 2010, AMD Corporation.
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms and conditions of the GNU General Public License,
+ * version 2, as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
+ * more details.
+ *
+ * You should have received a copy of the GNU General Public License along with
+ * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
+ * Place - Suite 330, Boston, MA 02111-1307 USA.
+ *
+ */
+
+#ifndef __ASM_X86_HVM_SVM_SVMDEBUG_H__
+#define __ASM_X86_HVM_SVM_SVMDEBUG_H__
+
+#include <asm/types.h>
+#include <asm/hvm/svm/vmcb.h>
+
+void svm_vmcb_dump(const char *from, struct vmcb_struct *vmcb);
+bool_t svm_vmcb_isvalid(const char *from, struct vmcb_struct *vmcb,
+                        bool_t verbose);
+
+#endif /* __ASM_X86_HVM_SVM_SVMDEBUG_H__ */
xen/include/asm-x86/hvm/svm/vmcb.h +3 -0
diff -r 54dd43df686b -r 3bfc06e2e41a xen/include/asm-x86/hvm/svm/vmcb.h
@@ -364,6 +364,9 @@ typedef union
     } fields;
 } __attribute__ ((packed)) lbrctrl_t;
 
+#define IOPM_SIZE   (12 * 1024)
+#define MSRPM_SIZE  (8  * 1024)
+
 struct vmcb_struct {
     u32 cr_intercepts;          /* offset 0x00 */
     u32 dr_intercepts;          /* offset 0x04 */
xen/include/asm-x86/hvm/vcpu.h +2 -0
diff -r 54dd43df686b -r 3bfc06e2e41a xen/include/asm-x86/hvm/vcpu.h
@@ -25,6 +25,7 @@
 #include <asm/hvm/vlapic.h>
 #include <asm/hvm/vmx/vmcs.h>
 #include <asm/hvm/svm/vmcb.h>
+#include <asm/hvm/svm/nestedsvm.h>
 #include <asm/mtrr.h>
 
 enum hvm_io_state {
@@ -45,6 +46,7 @@ struct nestedvcpu {
 
     /* SVM/VMX arch specific */
     union {
+        struct nestedsvm nsvm;
     } u;
 
     bool_t nv_flushp2m; /* True, when p2m table must be flushed */

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

#1 lore raw

Hi,

Quoted 12 lines · Christoph Egger
xen/arch/x86/hvm/svm/nestedsvm.c new file +12 -0
+static int nsvm_vmrun_permissionmap(struct vcpu *v)
+{
+    struct arch_svm_struct *arch_svm = &v->arch.hvm_svm;
+    struct nestedsvm *svm = &vcpu_nestedsvm(v);
+    struct vmcb_struct *ns_vmcb = vcpu_nestedhvm(v).nv_vmcx;
+    struct vmcb_struct *host_vmcb = arch_svm->vmcb;
+    unsigned long *ns_msrpm_ptr;
+    unsigned int i;
+    enum hvm_copy_result ret;
+
+    ns_msrpm_ptr = (unsigned long *)svm->ns_cached_msrpm;
+

xen/arch/x86/hvm/svm/nestedsvm.c line 285

Quoted 10 lines
xen/arch/x86/hvm/svm/nestedsvm.c new file +10 -0
+    ret = hvm_copy_from_guest_phys(svm->ns_cached_msrpm,
+                                   ns_vmcb->msrpm_base_pa, MSRPM_SIZE);
+    if (ret != HVMCOPY_okay) {
+        gdprintk(XENLOG_ERR, "hvm_copy_from_guest_phys msrpm %u\n", ret);
+        return 1;
+    }
+
+    /* Skip io bitmap merge since hvm_io_bitmap has all bits set but
+     * 0x80 and 0xed.
+     */

What if the L1 hypervisor wants to intercept port 0x80 or port 0xed?

Quoted 30 lines
xen/arch/x86/hvm/svm/nestedsvm.c new file +30 -0
+    /* v->arch.hvm_svm.msrpm has type unsigned long, thus
+     * BYTES_PER_LONG.
+     */
+    for (i = 0; i < MSRPM_SIZE / BYTES_PER_LONG; i++)
+        svm->ns_merged_msrpm[i] = arch_svm->msrpm[i] | ns_msrpm_ptr[i];
+
+    host_vmcb->iopm_base_pa =
+        (uint64_t)virt_to_maddr(hvm_io_bitmap);
+    host_vmcb->msrpm_base_pa =
+        (uint64_t)virt_to_maddr(svm->ns_merged_msrpm);
+
+    return 0;
+}
+
+static int nsvm_vmcb_prepare4vmrun(struct vcpu *v, struct cpu_user_regs *regs)
+{
+    struct nestedsvm *svm = &vcpu_nestedsvm(v);
+    struct vmcb_struct *ns_vmcb = vcpu_nestedhvm(v).nv_vmcx;
+    struct vmcb_struct *host_vmcb = v->arch.hvm_svm.vmcb;
+    int rc;
+
+    /* Enable nested guest intercepts */
+    svm->ns_cr_intercepts = ns_vmcb->cr_intercepts;
+    svm->ns_dr_intercepts = ns_vmcb->dr_intercepts;
+    svm->ns_exception_intercepts = ns_vmcb->exception_intercepts;
+    svm->ns_general1_intercepts = ns_vmcb->general1_intercepts;
+    svm->ns_general2_intercepts = ns_vmcb->general2_intercepts;
+
+    host_vmcb->cr_intercepts |= ns_vmcb->cr_intercepts;
+    host_vmcb->dr_intercepts |= ns_vmcb->dr_intercepts;

xen/arch/x86/hvm/svm/nestedsvm.c line 326

Quoted 10 lines
xen/arch/x86/hvm/svm/nestedsvm.c new file +10 -0
+    host_vmcb->exception_intercepts |= ns_vmcb->exception_intercepts;
+    host_vmcb->general1_intercepts |= ns_vmcb->general1_intercepts;
+    host_vmcb->general2_intercepts |= ns_vmcb->general2_intercepts;
+
+    /* Nested Pause Filter */
+    if (ns_vmcb->general1_intercepts & GENERAL1_INTERCEPT_PAUSE)
+        host_vmcb->pause_filter_count =
+            min(ns_vmcb->pause_filter_count, host_vmcb->pause_filter_count);
+    else
+        host_vmcb->pause_filter_count = SVM_PAUSEFILTER_INIT;

Why ignore the L0 count if the L1 doesn't intercept PAUSE?

xen/arch/x86/hvm/svm/nestedsvm.c line 334

Quoted 8 lines
xen/arch/x86/hvm/svm/nestedsvm.c new file +8 -0
+
+    /* Nested IO permission bitmaps */
+    rc = nsvm_vmrun_permissionmap(v);
+    if (rc)
+        return rc;
+
+    /* TSC offset */
+    hvm_set_guest_tsc(v, host_vmcb->tsc_offset + ns_vmcb->tsc_offset);

hvm_set_guest_tsc takes an absolute value, not an offset. ITYM to call the hvm_funcs pointer directly.

Quoted 72 lines
xen/arch/x86/hvm/svm/nestedsvm.c new file +72 -0
+
+    /* ASID */
+    hvm_asid_flush_vcpu(v);
+    /* host_vmcb->guest_asid = ns_vmcb->guest_asid; */
+
+    /* TLB control */
+    host_vmcb->tlb_control |= ns_vmcb->tlb_control;
+
+    /* Virtual Interrupts */
+    host_vmcb->vintr = ns_vmcb->vintr;
+    host_vmcb->vintr.fields.intr_masking = 1;
+
+    /* Shadow Mode */
+    host_vmcb->interrupt_shadow = ns_vmcb->interrupt_shadow;
+
+    /* Exit codes */
+    host_vmcb->exitcode = ns_vmcb->exitcode;
+    host_vmcb->exitinfo1 = ns_vmcb->exitinfo1;
+    host_vmcb->exitinfo2 = ns_vmcb->exitinfo2;
+    host_vmcb->exitintinfo = ns_vmcb->exitintinfo;
+
+    /* Pending Interrupts */
+    host_vmcb->eventinj = ns_vmcb->eventinj;
+
+    /* LBR virtualization */
+    svm->ns_lbr_control = ns_vmcb->lbr_control;
+    host_vmcb->lbr_control.bytes |= ns_vmcb->lbr_control.bytes;
+
+    /* NextRIP */
+    host_vmcb->nextrip = ns_vmcb->nextrip;
+
+    /*
+     * VMCB Save State Area
+     */
+
+    /* Segments */
+    host_vmcb->es = ns_vmcb->es;
+    host_vmcb->cs = ns_vmcb->cs;
+    host_vmcb->ss = ns_vmcb->ss;
+    host_vmcb->ds = ns_vmcb->ds;
+    host_vmcb->gdtr = ns_vmcb->gdtr;
+    host_vmcb->idtr = ns_vmcb->idtr;
+
+    /* CPL */
+    host_vmcb->cpl = ns_vmcb->cpl;
+
+    /* EFER */
+    v->arch.hvm_vcpu.guest_efer = ns_vmcb->efer;
+    rc = hvm_set_efer(ns_vmcb->efer);
+    if (rc != X86EMUL_OKAY)
+        gdprintk(XENLOG_ERR, "hvm_set_efer failed, rc: %u\n", rc);
+
+    /* CR4 */
+    v->arch.hvm_vcpu.guest_cr[4] = ns_vmcb->cr4;
+    rc = hvm_set_cr4(ns_vmcb->cr4);
+    if (rc != X86EMUL_OKAY)
+        gdprintk(XENLOG_ERR, "hvm_set_cr4 failed, rc: %u\n", rc);
+
+    /* CR0 */
+    v->arch.hvm_vcpu.guest_cr[0] = ns_vmcb->cr0;
+    rc = hvm_set_cr0(ns_vmcb->cr0);
+    if (rc != X86EMUL_OKAY)
+        gdprintk(XENLOG_ERR, "hvm_set_cr0 failed, rc: %u\n", rc);
+
+    /* CR2 */
+    v->arch.hvm_vcpu.guest_cr[2] = ns_vmcb->cr2;
+    hvm_update_guest_cr(v, 2);
+
+    /* Nested paging mode */
+    if (nestedhvm_paging_mode_hap(v)) {
+        /* host nested paging + guest nested paging. */
+

xen/arch/x86/hvm/svm/nestedsvm.c line 416

Quoted 10 lines
xen/arch/x86/hvm/svm/nestedsvm.c new file +10 -0
+        /* hvm_set_cr3() below sets v->arch.hvm_vcpu.guest_cr[3] for us. */
+        rc = hvm_set_cr3(ns_vmcb->cr3);
+        if (rc != X86EMUL_OKAY)
+            gdprintk(XENLOG_ERR, "hvm_set_cr3 failed, rc: %u\n", rc);
+    } else if (paging_mode_hap(v->domain)) {
+        /* host nested paging + guest shadow paging. */
+        host_vmcb->np_enable = 1;
+        /* Keep h_cr3 as it is. */
+        /* Guest shadow paging: Must intercept pagefaults. */
+        host_vmcb->exception_intercepts |= (1U << TRAP_page_fault);

No - it's the L1 hypervisor's job to intercep #PF if it wants to use shadow paging. We shouldn't special-case it here.

Quoted 135 lines
xen/arch/x86/hvm/svm/nestedsvm.c new file +134 -0
+        /* hvm_set_cr3() below sets v->arch.hvm_vcpu.guest_cr[3] for us. */
+        rc = hvm_set_cr3(ns_vmcb->cr3);
+        if (rc != X86EMUL_OKAY)
+            gdprintk(XENLOG_ERR, "hvm_set_cr3 failed, rc: %u\n", rc);
+    } else {
+        /* host shadow paging + guest shadow paging. */
+        host_vmcb->np_enable = 0;
+        host_vmcb->h_cr3 = 0x0;
+
+        /* TODO: Once shadow-shadow paging is in place come back to here
+         * and set host_vmcb->cr3 to the shadowed shadow table.
+         */
+    }
+
+    /* DRn */
+    host_vmcb->dr7 = ns_vmcb->dr7;
+    host_vmcb->dr6 = ns_vmcb->dr6;
+
+    /* RFLAGS */
+    host_vmcb->rflags = ns_vmcb->rflags;
+
+    /* RIP */
+    host_vmcb->rip = ns_vmcb->rip;
+
+    /* RSP */
+    host_vmcb->rsp = ns_vmcb->rsp;
+
+    /* RAX */
+    host_vmcb->rax = ns_vmcb->rax;
+
+    /* Keep the host values of the fs, gs, ldtr, tr, kerngsbase,
+     * star, lstar, cstar, sfmask, sysenter_cs, sysenter_esp,
+     * sysenter_eip. These are handled via VMSAVE/VMLOAD emulation.
+     */
+
+    /* Page tables */
+    host_vmcb->pdpe0 = ns_vmcb->pdpe0;
+    host_vmcb->pdpe1 = ns_vmcb->pdpe1;
+    host_vmcb->pdpe2 = ns_vmcb->pdpe2;
+    host_vmcb->pdpe3 = ns_vmcb->pdpe3;
+
+    /* PAT */
+    host_vmcb->g_pat = ns_vmcb->g_pat;
+
+    /* Debug Control MSR */
+    host_vmcb->debugctlmsr = ns_vmcb->debugctlmsr;
+
+    /* LBR MSRs */
+    host_vmcb->lastbranchfromip = ns_vmcb->lastbranchfromip;
+    host_vmcb->lastbranchtoip = ns_vmcb->lastbranchtoip;
+    host_vmcb->lastintfromip = ns_vmcb->lastintfromip;
+    host_vmcb->lastinttoip = ns_vmcb->lastinttoip;
+
+    rc = svm_vmcb_isvalid(__func__, ns_vmcb, 1);
+    if (rc) {
+        gdprintk(XENLOG_ERR, "nested vmcb invalid\n");
+        return rc;
+    }
+
+    rc = svm_vmcb_isvalid(__func__, host_vmcb, 1);
+    if (rc) {
+        gdprintk(XENLOG_ERR, "host vmcb invalid\n");
+        return rc;
+    }
+
+    /* Switch guest registers to nested guest */
+    regs->eax = ns_vmcb->rax;
+    regs->eip = ns_vmcb->rip;
+    regs->esp = ns_vmcb->rsp;
+    regs->eflags = ns_vmcb->rflags;
+
+    return 0;
+}
+
 
+int
+nsvm_vmcb_guest_intercepts_exitcode(struct vcpu *v,
+    struct cpu_user_regs *regs, uint64_t exitcode)
+{
+    uint64_t exit_bits;
+    struct nestedvcpu *nv = &vcpu_nestedhvm(v);
+    struct nestedsvm *svm = &vcpu_nestedsvm(v);
+    struct vmcb_struct *ns_vmcb = nv->nv_vmcx;
+    enum nestedhvm_vmexits vmexits;
+
+    switch (exitcode) {
+    case VMEXIT_CR0_READ ... VMEXIT_CR15_READ:
+    case VMEXIT_CR0_WRITE ... VMEXIT_CR15_WRITE:
+        exit_bits = 1ULL << (exitcode - VMEXIT_CR0_READ);
+        if (svm->ns_cr_intercepts & exit_bits)
+            break;
+        return 0;
+
+    case VMEXIT_DR0_READ ... VMEXIT_DR7_READ:
+    case VMEXIT_DR0_WRITE ... VMEXIT_DR7_WRITE:
+        exit_bits = 1ULL << (exitcode - VMEXIT_DR0_READ);
+        if (svm->ns_dr_intercepts & exit_bits)
+            break;
+        return 0;
+
+    case VMEXIT_EXCEPTION_DE ... VMEXIT_EXCEPTION_XF:
+        exit_bits = 1ULL << (exitcode - VMEXIT_EXCEPTION_DE);
+        if (svm->ns_exception_intercepts & exit_bits)
+            break;
+        return 0;
+
+    case VMEXIT_INTR ... VMEXIT_SHUTDOWN:
+        exit_bits = 1ULL << (exitcode - VMEXIT_INTR);
+        if (svm->ns_general1_intercepts & exit_bits)
+            break;
+        return 0;
+
+    case VMEXIT_VMRUN ... VMEXIT_MWAIT_CONDITIONAL:
+        exit_bits = 1ULL << (exitcode - VMEXIT_VMRUN);
+        if (svm->ns_general2_intercepts & exit_bits)
+            break;
+        return 0;
+
+    case VMEXIT_NPF:
+    case VMEXIT_INVALID:
+        /* Always intercepted */
+        break;
+
+    default:
+        gdprintk(XENLOG_ERR, "Illegal exitcode 0x%"PRIx64"\n", exitcode);
+        BUG();
+        break;
+    }
+
+    /* Special cases: Do more detailed checks */
+    switch (exitcode) {
+    case VMEXIT_MSR:
+        ASSERT(regs != NULL);
+        nestedsvm_vmcb_map(v, nv->nv_vmcxaddr);
+        ASSERT(nv->nv_vmcx != NULL);

xen/arch/x86/hvm/svm/nestedsvm.c line 760

Quoted 10 lines
xen/arch/x86/hvm/svm/nestedsvm.c new file +10 -0
+        ns_vmcb = nv->nv_vmcx;
+        vmexits = nsvm_vmcb_guest_intercepts_msr(svm->ns_cached_msrpm,
+            regs->ecx, ns_vmcb->exitinfo1 != 0);
+        if (vmexits == NESTEDHVM_VMEXIT_HOST)
+            return 0;
+        break;
+
+    case VMEXIT_IOIO:
+        /* always intercepted */
+        break;

What if the guest doesn't want to intercept it? Won't that make it crash or misbehave?

Tim.

Signature · 3 lines
Tim Deegan <[email protected]>
Principal Software Engineer, Xen Platform Team
Citrix Systems UK Ltd.  (Company #02937203, SL9 0BG)
#2 lore raw
Quoted 19 lines · On Tuesday 16 November 2010 15:54:11 Tim Deegan wrote:

Hi,

Quoted 16 lines · Christoph Egger
xen/arch/x86/hvm/svm/nestedsvm.c new file +16 -0
+static int nsvm_vmrun_permissionmap(struct vcpu *v)
+{
+    struct arch_svm_struct *arch_svm = &v->arch.hvm_svm;
+    struct nestedsvm *svm = &vcpu_nestedsvm(v);
+    struct vmcb_struct *ns_vmcb = vcpu_nestedhvm(v).nv_vmcx;
+    struct vmcb_struct *host_vmcb = arch_svm->vmcb;
+    unsigned long *ns_msrpm_ptr;
+    unsigned int i;
+    enum hvm_copy_result ret;
+
+    ns_msrpm_ptr = (unsigned long *)svm->ns_cached_msrpm;
+
+    ret = hvm_copy_from_guest_phys(svm->ns_cached_msrpm,
+                                   ns_vmcb->msrpm_base_pa, MSRPM_SIZE);
+    if (ret != HVMCOPY_okay) {
+        gdprintk(XENLOG_ERR, "hvm_copy_from_guest_phys msrpm %u\n",
Quoted 8 lines

xen/arch/x86/hvm/svm/nestedsvm.c line 285

Quoted 6 lines

ret); + return 1;

xen/arch/x86/hvm/svm/nestedsvm.c new file +5 -0
+    }
+
+    /* Skip io bitmap merge since hvm_io_bitmap has all bits set but
+     * 0x80 and 0xed.
+     */

What if the L1 hypervisor wants to intercept port 0x80 or port 0xed?

Thanks for the pointer. This item is work in progress.

Quoted 35 lines
Quoted 35 lines
xen/arch/x86/hvm/svm/nestedsvm.c new file +15 -0
+    /* v->arch.hvm_svm.msrpm has type unsigned long, thus
+     * BYTES_PER_LONG.
+     */
+    for (i = 0; i < MSRPM_SIZE / BYTES_PER_LONG; i++)
+        svm->ns_merged_msrpm[i] = arch_svm->msrpm[i] | ns_msrpm_ptr[i];
+
+    host_vmcb->iopm_base_pa =
+        (uint64_t)virt_to_maddr(hvm_io_bitmap);
+    host_vmcb->msrpm_base_pa =
+        (uint64_t)virt_to_maddr(svm->ns_merged_msrpm);
+
+    return 0;
+}
+
+static int nsvm_vmcb_prepare4vmrun(struct vcpu *v, struct cpu_user_regs

*regs) +{

xen/arch/x86/hvm/svm/nestedsvm.c new file +18 -0
+    struct nestedsvm *svm = &vcpu_nestedsvm(v);
+    struct vmcb_struct *ns_vmcb = vcpu_nestedhvm(v).nv_vmcx;
+    struct vmcb_struct *host_vmcb = v->arch.hvm_svm.vmcb;
+    int rc;
+
+    /* Enable nested guest intercepts */
+    svm->ns_cr_intercepts = ns_vmcb->cr_intercepts;
+    svm->ns_dr_intercepts = ns_vmcb->dr_intercepts;
+    svm->ns_exception_intercepts = ns_vmcb->exception_intercepts;
+    svm->ns_general1_intercepts = ns_vmcb->general1_intercepts;
+    svm->ns_general2_intercepts = ns_vmcb->general2_intercepts;
+
+    host_vmcb->cr_intercepts |= ns_vmcb->cr_intercepts;
+    host_vmcb->dr_intercepts |= ns_vmcb->dr_intercepts;
+    host_vmcb->exception_intercepts |= ns_vmcb->exception_intercepts;
+    host_vmcb->general1_intercepts |= ns_vmcb->general1_intercepts;
+    host_vmcb->general2_intercepts |= ns_vmcb->general2_intercepts;
+
Quoted 8 lines
Quoted 6 lines
xen/arch/x86/hvm/svm/nestedsvm.c new file +4 -0
+    /* Nested Pause Filter */
+    if (ns_vmcb->general1_intercepts & GENERAL1_INTERCEPT_PAUSE)
+        host_vmcb->pause_filter_count =
+            min(ns_vmcb->pause_filter_count,

host_vmcb->pause_filter_count); + else
+ host_vmcb->pause_filter_count = SVM_PAUSEFILTER_INIT;

Why ignore the L0 count if the L1 doesn't intercept PAUSE?

SVM_PAUSEFILTER_INIT is what is also used in construct_vmcb().

Quoted 12 lines

xen/arch/x86/hvm/svm/nestedsvm.c line 334

Quoted 9 lines
xen/arch/x86/hvm/svm/nestedsvm.c new file +8 -0
+
+    /* Nested IO permission bitmaps */
+    rc = nsvm_vmrun_permissionmap(v);
+    if (rc)
+        return rc;
+
+    /* TSC offset */
+    hvm_set_guest_tsc(v, host_vmcb->tsc_offset + ns_vmcb->tsc_offset);

hvm_set_guest_tsc takes an absolute value, not an offset. ITYM to call the hvm_funcs pointer directly.

Thanks. Fixed.

Quoted 78 lines
Quoted 78 lines
xen/arch/x86/hvm/svm/nestedsvm.c new file +73 -0
+
+    /* ASID */
+    hvm_asid_flush_vcpu(v);
+    /* host_vmcb->guest_asid = ns_vmcb->guest_asid; */
+
+    /* TLB control */
+    host_vmcb->tlb_control |= ns_vmcb->tlb_control;
+
+    /* Virtual Interrupts */
+    host_vmcb->vintr = ns_vmcb->vintr;
+    host_vmcb->vintr.fields.intr_masking = 1;
+
+    /* Shadow Mode */
+    host_vmcb->interrupt_shadow = ns_vmcb->interrupt_shadow;
+
+    /* Exit codes */
+    host_vmcb->exitcode = ns_vmcb->exitcode;
+    host_vmcb->exitinfo1 = ns_vmcb->exitinfo1;
+    host_vmcb->exitinfo2 = ns_vmcb->exitinfo2;
+    host_vmcb->exitintinfo = ns_vmcb->exitintinfo;
+
+    /* Pending Interrupts */
+    host_vmcb->eventinj = ns_vmcb->eventinj;
+
+    /* LBR virtualization */
+    svm->ns_lbr_control = ns_vmcb->lbr_control;
+    host_vmcb->lbr_control.bytes |= ns_vmcb->lbr_control.bytes;
+
+    /* NextRIP */
+    host_vmcb->nextrip = ns_vmcb->nextrip;
+
+    /*
+     * VMCB Save State Area
+     */
+
+    /* Segments */
+    host_vmcb->es = ns_vmcb->es;
+    host_vmcb->cs = ns_vmcb->cs;
+    host_vmcb->ss = ns_vmcb->ss;
+    host_vmcb->ds = ns_vmcb->ds;
+    host_vmcb->gdtr = ns_vmcb->gdtr;
+    host_vmcb->idtr = ns_vmcb->idtr;
+
+    /* CPL */
+    host_vmcb->cpl = ns_vmcb->cpl;
+
+    /* EFER */
+    v->arch.hvm_vcpu.guest_efer = ns_vmcb->efer;
+    rc = hvm_set_efer(ns_vmcb->efer);
+    if (rc != X86EMUL_OKAY)
+        gdprintk(XENLOG_ERR, "hvm_set_efer failed, rc: %u\n", rc);
+
+    /* CR4 */
+    v->arch.hvm_vcpu.guest_cr[4] = ns_vmcb->cr4;
+    rc = hvm_set_cr4(ns_vmcb->cr4);
+    if (rc != X86EMUL_OKAY)
+        gdprintk(XENLOG_ERR, "hvm_set_cr4 failed, rc: %u\n", rc);
+
+    /* CR0 */
+    v->arch.hvm_vcpu.guest_cr[0] = ns_vmcb->cr0;
+    rc = hvm_set_cr0(ns_vmcb->cr0);
+    if (rc != X86EMUL_OKAY)
+        gdprintk(XENLOG_ERR, "hvm_set_cr0 failed, rc: %u\n", rc);
+
+    /* CR2 */
+    v->arch.hvm_vcpu.guest_cr[2] = ns_vmcb->cr2;
+    hvm_update_guest_cr(v, 2);
+
+    /* Nested paging mode */
+    if (nestedhvm_paging_mode_hap(v)) {
+        /* host nested paging + guest nested paging. */
+
+        /* hvm_set_cr3() below sets v->arch.hvm_vcpu.guest_cr[3] for us.

*/ + rc = hvm_set_cr3(ns_vmcb->cr3);

xen/arch/x86/hvm/svm/nestedsvm.c new file +3 -0
+        if (rc != X86EMUL_OKAY)
+            gdprintk(XENLOG_ERR, "hvm_set_cr3 failed, rc: %u\n", rc);
+    } else if (paging_mode_hap(v->domain)) {
Quoted 8 lines

xen/arch/x86/hvm/svm/nestedsvm.c line 416

Quoted 5 lines
xen/arch/x86/hvm/svm/nestedsvm.c new file +5 -0
+        /* host nested paging + guest shadow paging. */
+        host_vmcb->np_enable = 1;
+        /* Keep h_cr3 as it is. */
+        /* Guest shadow paging: Must intercept pagefaults. */
+        host_vmcb->exception_intercepts |= (1U << TRAP_page_fault);

No - it's the L1 hypervisor's job to intercep #PF if it wants to use shadow paging. We shouldn't special-case it here.

Fixed.

Quoted 141 lines
Quoted 141 lines

+ /* hvm_set_cr3() below sets v->arch.hvm_vcpu.guest_cr[3] for us.
*/ + rc = hvm_set_cr3(ns_vmcb->cr3);

xen/arch/x86/hvm/svm/nestedsvm.c new file +122 -0
+        if (rc != X86EMUL_OKAY)
+            gdprintk(XENLOG_ERR, "hvm_set_cr3 failed, rc: %u\n", rc);
+    } else {
+        /* host shadow paging + guest shadow paging. */
+        host_vmcb->np_enable = 0;
+        host_vmcb->h_cr3 = 0x0;
+
+        /* TODO: Once shadow-shadow paging is in place come back to here
+         * and set host_vmcb->cr3 to the shadowed shadow table.
+         */
+    }
+
+    /* DRn */
+    host_vmcb->dr7 = ns_vmcb->dr7;
+    host_vmcb->dr6 = ns_vmcb->dr6;
+
+    /* RFLAGS */
+    host_vmcb->rflags = ns_vmcb->rflags;
+
+    /* RIP */
+    host_vmcb->rip = ns_vmcb->rip;
+
+    /* RSP */
+    host_vmcb->rsp = ns_vmcb->rsp;
+
+    /* RAX */
+    host_vmcb->rax = ns_vmcb->rax;
+
+    /* Keep the host values of the fs, gs, ldtr, tr, kerngsbase,
+     * star, lstar, cstar, sfmask, sysenter_cs, sysenter_esp,
+     * sysenter_eip. These are handled via VMSAVE/VMLOAD emulation.
+     */
+
+    /* Page tables */
+    host_vmcb->pdpe0 = ns_vmcb->pdpe0;
+    host_vmcb->pdpe1 = ns_vmcb->pdpe1;
+    host_vmcb->pdpe2 = ns_vmcb->pdpe2;
+    host_vmcb->pdpe3 = ns_vmcb->pdpe3;
+
+    /* PAT */
+    host_vmcb->g_pat = ns_vmcb->g_pat;
+
+    /* Debug Control MSR */
+    host_vmcb->debugctlmsr = ns_vmcb->debugctlmsr;
+
+    /* LBR MSRs */
+    host_vmcb->lastbranchfromip = ns_vmcb->lastbranchfromip;
+    host_vmcb->lastbranchtoip = ns_vmcb->lastbranchtoip;
+    host_vmcb->lastintfromip = ns_vmcb->lastintfromip;
+    host_vmcb->lastinttoip = ns_vmcb->lastinttoip;
+
+    rc = svm_vmcb_isvalid(__func__, ns_vmcb, 1);
+    if (rc) {
+        gdprintk(XENLOG_ERR, "nested vmcb invalid\n");
+        return rc;
+    }
+
+    rc = svm_vmcb_isvalid(__func__, host_vmcb, 1);
+    if (rc) {
+        gdprintk(XENLOG_ERR, "host vmcb invalid\n");
+        return rc;
+    }
+
+    /* Switch guest registers to nested guest */
+    regs->eax = ns_vmcb->rax;
+    regs->eip = ns_vmcb->rip;
+    regs->esp = ns_vmcb->rsp;
+    regs->eflags = ns_vmcb->rflags;
+
+    return 0;
+}
+
 
+int
+nsvm_vmcb_guest_intercepts_exitcode(struct vcpu *v,
+    struct cpu_user_regs *regs, uint64_t exitcode)
+{
+    uint64_t exit_bits;
+    struct nestedvcpu *nv = &vcpu_nestedhvm(v);
+    struct nestedsvm *svm = &vcpu_nestedsvm(v);
+    struct vmcb_struct *ns_vmcb = nv->nv_vmcx;
+    enum nestedhvm_vmexits vmexits;
+
+    switch (exitcode) {
+    case VMEXIT_CR0_READ ... VMEXIT_CR15_READ:
+    case VMEXIT_CR0_WRITE ... VMEXIT_CR15_WRITE:
+        exit_bits = 1ULL << (exitcode - VMEXIT_CR0_READ);
+        if (svm->ns_cr_intercepts & exit_bits)
+            break;
+        return 0;
+
+    case VMEXIT_DR0_READ ... VMEXIT_DR7_READ:
+    case VMEXIT_DR0_WRITE ... VMEXIT_DR7_WRITE:
+        exit_bits = 1ULL << (exitcode - VMEXIT_DR0_READ);
+        if (svm->ns_dr_intercepts & exit_bits)
+            break;
+        return 0;
+
+    case VMEXIT_EXCEPTION_DE ... VMEXIT_EXCEPTION_XF:
+        exit_bits = 1ULL << (exitcode - VMEXIT_EXCEPTION_DE);
+        if (svm->ns_exception_intercepts & exit_bits)
+            break;
+        return 0;
+
+    case VMEXIT_INTR ... VMEXIT_SHUTDOWN:
+        exit_bits = 1ULL << (exitcode - VMEXIT_INTR);
+        if (svm->ns_general1_intercepts & exit_bits)
+            break;
+        return 0;
+
+    case VMEXIT_VMRUN ... VMEXIT_MWAIT_CONDITIONAL:
+        exit_bits = 1ULL << (exitcode - VMEXIT_VMRUN);
+        if (svm->ns_general2_intercepts & exit_bits)
+            break;
+        return 0;
+
+    case VMEXIT_NPF:
+    case VMEXIT_INVALID:
+        /* Always intercepted */
+        break;
+
+    default:
+        gdprintk(XENLOG_ERR, "Illegal exitcode 0x%"PRIx64"\n",

exitcode); + BUG();

xen/arch/x86/hvm/svm/nestedsvm.c new file +14 -0
+        break;
+    }
+
+    /* Special cases: Do more detailed checks */
+    switch (exitcode) {
+    case VMEXIT_MSR:
+        ASSERT(regs != NULL);
+        nestedsvm_vmcb_map(v, nv->nv_vmcxaddr);
+        ASSERT(nv->nv_vmcx != NULL);
+        ns_vmcb = nv->nv_vmcx;
+        vmexits = nsvm_vmcb_guest_intercepts_msr(svm->ns_cached_msrpm,
+            regs->ecx, ns_vmcb->exitinfo1 != 0);
+        if (vmexits == NESTEDHVM_VMEXIT_HOST)
+            return 0;
Quoted 8 lines

xen/arch/x86/hvm/svm/nestedsvm.c line 760

Quoted 5 lines
xen/arch/x86/hvm/svm/nestedsvm.c new file +5 -0
+        break;
+
+    case VMEXIT_IOIO:
+        /* always intercepted */
+        break;

What if the guest doesn't want to intercept it? Won't that make it crash or misbehave?

Yes, right. Fixed.

Christoph

Signature · 6 lines
---to satisfy European Law for business letters:
Advanced Micro Devices GmbH
Einsteinring 24, 85609 Dornach b. Muenchen
Geschaeftsfuehrer: Alberto Bozzo, Andrew Bowd
Sitz: Dornach, Gemeinde Aschheim, Landkreis Muenchen
Registergericht Muenchen, HRB Nr. 43632