Skip to contentSkip to conversation

AMD Nested Virt Preparation

PATCH In review George Dunlap 2 participants 17 messages Jun 26, 2024 – Jul 31, 2024

lore
Share & tools
lore
#0 lore raw

cover AMD Nested Virt Preparation

This is my work-in-progress series for getting nested virt working again on AMD.

The first patch is an early draft to integrate the SVM bits into the CPUID framework. It will be partially superseded by a series Andrew has posted but which has not yet been checked in.

The second patch is a workaround which hides the PCID bit from the L1 when nested HVM is enabled. Long-term, nested support for PCID will be necessary for performance.

Most of the rest of the patches involve improving tracing: Either fixing what's bitrotted in xenalyze, what's bitrotted specifically on the SVM side, or what could be improved in the upstream tracing.

The last patch is a placeholder for future work, commenting a place where behavior is wrong and another where more work needs to be done to assure safety.

George Dunlap (14):
x86/cpuid-policy: Add AMD SVM CPUID leaf to featureset x86/cpu-policy: HACK Disable PCID when nested virt is enabled
xenalyze: Basic nested virt processing
xenalyze: Track generic event information when not in summary mode
xenalyze: Ignore vmexits where an HVM_HANDLER traces would be
redundant
xen/svm: Remove redundant HVM_HANDLER trace for EXCEPTION_AC xen/hvm: Don't skip MSR_READ trace record
svm: Do NPF trace before calling hvm_hap_nested_page_fault x86/emulate: Don't trace cr reads during emulation
xenalyze: Quiet warnings about VMEXIT_IOIO
x86/trace: Add trace to xsetbv svm/vmx handler path
xenalyze: Basic processing for XSETBV exits and handlers x86/svm: Add a trace for VMEXIT_VMRUN
x86/nestedsvm: Note some places for improvement

 tools/libs/light/libxl_cpuid.c              |   1 +
 tools/misc/xen-cpuid.c                      |   1 +
 tools/xentrace/xenalyze.c                   | 133 +++++++++++++++-----
 xen/arch/x86/cpu-policy.c                   |  24 ++--
 xen/arch/x86/cpu/common.c                   |   2 +
 xen/arch/x86/hvm/emulate.c                  |   1 -
 xen/arch/x86/hvm/hvm.c                      |   4 +-
 xen/arch/x86/hvm/svm/nestedsvm.c            |  13 ++
 xen/arch/x86/hvm/svm/svm.c                  |   7 +-
 xen/include/public/arch-x86/cpufeatureset.h |  16 +++
 xen/include/public/trace.h                  |   1 +
 xen/include/xen/lib/x86/cpu-policy.h        |  10 +-
 xen/lib/x86/cpuid.c                         |   4 +-
 13 files changed, 167 insertions(+), 50 deletions(-)
Signature · 1 line
2.25.1
#1 lore raw

PATCH 1/14 x86/cpuid-policy: Add AMD SVM CPUID leaf to featureset

NOTE: This patch is be partially superseded by Andrew Cooper's "x86:
AMD CPUID handling improvements" series.

Currently, the CPUID leaf for SVM features (extd 0xa.edx) is manually twiddled:

- hvm_max_policy takes host_policy and clamps it to supported features (with some features unilaterally enabled because they're always emulated

- hvm_default_policy is copied from there

- When recalculate_policy() is called for a guest, if SVM is clear, then the entire leaf is zeroed out.

Move to a mode where the extended features are off by default, and enabled when nested_virt is enabled.

In cpufeatureset.h, define a new featureset word for the AMD SVM features, and declare all of the bits defined in x86/include/asm/hvm/svm/svm.h. Mark the ones we currently pass through to the "max policy" as HAP-only and optional.

In cpu-policy.h, define FEATURESET_ead, and convert the un-named space in struct_cpu_policy into the appropriate union. FIXME: Do this in a prerequisite patch, and change all references to p->extd.raw[0xa].

Update x86_cpu_X_to_Y and Y_to_X to copy this into and out of the appropriate leaf.

Populate this during boot in generic_identify().

Add the new featureset definition into libxl_cpuid.c.

Update the code in calculate_hvm_max_policy() to do nothing with the "normal" CPUID bits, and use the feature bit to unconditionally enable VMCBCLEAN. FIXME Move this to a follow-up patch.

In recalculate_cpuid_policy(), enable max_fs when nested_hvm() is true.

---
CC: Andrew Cooper <[email protected]>
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monne <[email protected]>

---
 tools/libs/light/libxl_cpuid.c              |  1 +
 tools/misc/xen-cpuid.c                      |  1 +
 xen/arch/x86/cpu-policy.c                   | 23 ++++++++++-----------
 xen/arch/x86/cpu/common.c                   |  2 ++
 xen/include/public/arch-x86/cpufeatureset.h | 16 ++++++++++++++
 xen/include/xen/lib/x86/cpu-policy.h        | 10 ++++++++-
 xen/lib/x86/cpuid.c                         |  4 +++-
 7 files changed, 43 insertions(+), 14 deletions(-)
tools/libs/light/libxl_cpuid.c +1 -0
index 063fe86eb7..e8000615ab 100644
@@ -342,6 +342,7 @@ int libxl_cpuid_parse_config(libxl_cpuid_policy_list *policy, const char* str)
         CPUID_ENTRY(0x00000007,  1, CPUID_REG_EDX),
         MSR_ENTRY(0x10a, CPUID_REG_EAX),
         MSR_ENTRY(0x10a, CPUID_REG_EDX),
+        CPUID_ENTRY(0x8000000a, NA, CPUID_REG_EDX),
 #undef MSR_ENTRY
 #undef CPUID_ENTRY
     };
tools/misc/xen-cpuid.c +1 -0
index 4c4593528d..460acec46c 100644
@@ -37,6 +37,7 @@ static const struct {
     { "CPUID 0x00000007:1.edx",     "7d1" },
     { "MSR_ARCH_CAPS.lo",         "m10Al" },
     { "MSR_ARCH_CAPS.hi",         "m10Ah" },
+    { "CPUID 0x8000000a.edx",       "ead" },
 };
 
 #define COL_ALIGN "24"
xen/arch/x86/cpu-policy.c +11 -12
index 304dc20cfa..d3ba177dac 100644
@@ -751,22 +751,16 @@ static void __init calculate_hvm_max_policy(void)
     if ( !cpu_has_vmx )
         __clear_bit(X86_FEATURE_PKS, fs);
 
-    /* 
+    /*
      * Make adjustments to possible (nested) virtualization features exposed
      * to the guest
      */
     if ( p->extd.svm )
     {
-        /* Clamp to implemented features which require hardware support. */
-        p->extd.raw[0xa].d &= ((1u << SVM_FEATURE_NPT) |
-                               (1u << SVM_FEATURE_LBRV) |
-                               (1u << SVM_FEATURE_NRIPS) |
-                               (1u << SVM_FEATURE_PAUSEFILTER) |
-                               (1u << SVM_FEATURE_DECODEASSISTS));
         /* Enable features which are always emulated. */
-        p->extd.raw[0xa].d |= (1u << SVM_FEATURE_VMCBCLEAN);
+        __set_bit(X86_FEATURE_VMCBCLEAN, fs);
     }
-    
+
     guest_common_max_feature_adjustments(fs);
     guest_common_feature_adjustments(fs);
 
@@ -915,6 +909,14 @@ void recalculate_cpuid_policy(struct domain *d)
             __clear_bit(X86_FEATURE_VMX, max_fs);
             __clear_bit(X86_FEATURE_SVM, max_fs);
         }
+        else
+        {
+            /*
+             * Enable SVM features.  This will be empty on VMX
+             * hosts.
+             */
+            fs[FEATURESET_ead] = max_fs[FEATURESET_ead];
+        }
     }
 
     /*
@@ -981,9 +983,6 @@ void recalculate_cpuid_policy(struct domain *d)
          ((vpmu_mode & XENPMU_MODE_ALL) && !is_hardware_domain(d)) )
         p->basic.raw[0xa] = EMPTY_LEAF;
 
-    if ( !p->extd.svm )
-        p->extd.raw[0xa] = EMPTY_LEAF;
-
     if ( !p->extd.page1gb )
         p->extd.raw[0x19] = EMPTY_LEAF;
 }
xen/arch/x86/cpu/common.c +2 -0
index ff4cd22897..da5ad10516 100644
@@ -478,6 +478,8 @@ static void generic_identify(struct cpuinfo_x86 *c)
 		c->x86_capability[FEATURESET_e7d] = cpuid_edx(0x80000007);
 	if (c->extended_cpuid_level >= 0x80000008)
 		c->x86_capability[FEATURESET_e8b] = cpuid_ebx(0x80000008);
+	if (c->extended_cpuid_level >= 0x8000000a)
+		c->x86_capability[FEATURESET_ead] = cpuid_edx(0x8000000a);
 	if (c->extended_cpuid_level >= 0x80000021)
 		c->x86_capability[FEATURESET_e21a] = cpuid_eax(0x80000021);
 
xen/include/public/arch-x86/cpufeatureset.h +16 -0
index d9eba5e9a7..88d5d0054b 100644
@@ -365,6 +365,22 @@ XEN_CPUFEATURE(RFDS_CLEAR,         16*32+28) /*!A| Register File(s) cleared by V
 
 /* Intel-defined CPU features, MSR_ARCH_CAPS 0x10a.edx, word 17 */
 
+/* AMD-defined CPU features, CPUID level 0x8000000a.edx, word 18 */
+XEN_CPUFEATURE(NPT,                 18*32+ 0) /*h  Nested page table support */
+XEN_CPUFEATURE(LBRV,                18*32+ 1) /*h  LBR virtualization support */
+XEN_CPUFEATURE(SVML,                18*32+ 2) /*   SVM locking MSR support */
+XEN_CPUFEATURE(NRIPS,               18*32+ 3) /*h  Next RIP save on VMEXIT support */
+XEN_CPUFEATURE(TSCRATEMSR,          18*32+ 4) /*   TSC ratio MSR support */
+XEN_CPUFEATURE(VMCBCLEAN,           18*32+ 5) /*h  VMCB clean bits support */
+XEN_CPUFEATURE(FLUSHBYASID,         18*32+ 6) /*   TLB flush by ASID support */
+XEN_CPUFEATURE(DECODEASSISTS,       18*32+ 7) /*h  Decode assists support */
+XEN_CPUFEATURE(PAUSEFILTER,         18*32+10) /*h  Pause intercept filter support */
+XEN_CPUFEATURE(PAUSETHRESH,         18*32+12) /*   Pause intercept filter threshold */
+XEN_CPUFEATURE(VLOADSAVE,           18*32+15) /*   virtual vmload/vmsave */
+XEN_CPUFEATURE(VGIF,                18*32+16) /*   Virtual GIF */
+XEN_CPUFEATURE(SSS,                 18*32+19) /*   NPT Supervisor Shadow Stacks */
+XEN_CPUFEATURE(SPEC_CTRL,           18*32+20) /*   MSR_SPEC_CTRL virtualisation */
+
 #endif /* XEN_CPUFEATURE */
 
 /* Clean up from a default include.  Close the enum (for C). */
xen/include/xen/lib/x86/cpu-policy.h +9 -1
index d26012c6da..445325a5b5 100644
@@ -22,6 +22,7 @@
 #define FEATURESET_7d1       15 /* 0x00000007:1.edx    */
 #define FEATURESET_m10Al     16 /* 0x0000010a.eax      */
 #define FEATURESET_m10Ah     17 /* 0x0000010a.edx      */
+#define FEATURESET_ead       18 /* 0x8000000a.edx      */
 
 struct cpuid_leaf
 {
@@ -296,7 +297,14 @@ struct cpu_policy
             uint32_t /* d */:32;
 
             uint64_t :64, :64; /* Leaf 0x80000009. */
-            uint64_t :64, :64; /* Leaf 0x8000000a - SVM rev and features. */
+
+            /* Leaf 0x8000000a - SVM rev and features. */
+            uint64_t /* a, b */:64, /* c */:32;
+            union {
+                uint32_t ead;
+                struct { DECL_BITFIELD(ead); };
+            };
+
             uint64_t :64, :64; /* Leaf 0x8000000b. */
             uint64_t :64, :64; /* Leaf 0x8000000c. */
             uint64_t :64, :64; /* Leaf 0x8000000d. */
xen/lib/x86/cpuid.c +3 -1
index eb7698dc73..d68f442d4e 100644
@@ -81,7 +81,8 @@ void x86_cpu_policy_to_featureset(
     fs[FEATURESET_7d1]       = p->feat._7d1;
     fs[FEATURESET_m10Al]     = p->arch_caps.lo;
     fs[FEATURESET_m10Ah]     = p->arch_caps.hi;
-}
+    fs[FEATURESET_ead]       = p->extd.ead;
+ }
 
 void x86_cpu_featureset_to_policy(
     const uint32_t fs[FEATURESET_NR_ENTRIES], struct cpu_policy *p)
@@ -104,6 +105,7 @@ void x86_cpu_featureset_to_policy(
     p->feat._7d1             = fs[FEATURESET_7d1];
     p->arch_caps.lo          = fs[FEATURESET_m10Al];
     p->arch_caps.hi          = fs[FEATURESET_m10Ah];
+    p->extd.ead              = fs[FEATURESET_ead];
 }
 
 void x86_cpu_policy_recalc_synth(struct cpu_policy *p)
Signature · 1 line
2.25.1
#2 lore raw

PATCH 2/14 x86/cpu-policy: HACK Disable PCID when nested virt is enabled

In reply to George Dunlap

The non-nested HVM code knows how to provide PCID functionality (non-zero values in the lower 12 bits of CR3 when running in 64-bit mode), but the nested code doesn't. If the L2 decides to use the PCID functionality, the L0 will fail the next L1 VMENTRY.

Long term we definitely want to enable this feature, but for now, just hide it from guests when nested HVM is enabled.

---
 xen/arch/x86/cpu-policy.c | 1 +
 1 file changed, 1 insertion(+)
xen/arch/x86/cpu-policy.c +1 -0
index d3ba177dac..91281b44b0 100644
@@ -916,6 +916,7 @@ void recalculate_cpuid_policy(struct domain *d)
              * hosts.
              */
             fs[FEATURESET_ead] = max_fs[FEATURESET_ead];
+            __clear_bit(X86_FEATURE_PCID, max_fs);
         }
     }
 
Signature · 1 line
2.25.1
#3 lore raw

PATCH 3/14 xenalyze: Basic nested virt processing

In reply to George Dunlap

On SVM, VMEXIT that occur in when an L1 is in non-root mode ("nested exits") are tagged with the TRC_HVM_NESTEDFLAG flag. Expand xenalyze to do basic handling of these records:

- Refactor hvm_process to filter out both TRC_HVM_NESTEDFLAG and TRC_64_FLAG when deciding how to process

- Create separate "trace volume" sub-categories for them (HVM_VOL_*), tweaking layout so things continue to align

- Visually distinquish nested from non-nested vmexits and vmentries in dump mode

At the moment, nested VMEXITs which are passed through to the L1 hypervisor won't have any HVM handlers; note in hvm_data when a vmexit was nested, and don't warn when no handlers occur.

While here:
- Expand one of the error messages with a bit more information
- Replace repeated ri->event & TRC_64_FLAG instances with flag64
- Remove a stray whitespace at the end of a line
- Add a missing newline to a warning statement

---
 tools/xentrace/xenalyze.c | 66 ++++++++++++++++++++++++++-------------
 1 file changed, 45 insertions(+), 21 deletions(-)
tools/xentrace/xenalyze.c +45 -21
index d95e52695f..52ee7a5f9f 100644
@@ -986,7 +986,9 @@ const char * hvm_event_handler_name[HVM_EVENT_HANDLER_MAX] = {
 
 enum {
     HVM_VOL_VMENTRY,
+    HVM_VOL_VMENTRY_NESTED,
     HVM_VOL_VMEXIT,
+    HVM_VOL_VMEXIT_NESTED,
     HVM_VOL_HANDLER,
     HVM_VOL_EMUL,
     HVM_VOL_MAX
@@ -1014,6 +1016,8 @@ const char *guest_interrupt_case_name[] = {
 const char *hvm_vol_name[HVM_VOL_MAX] = {
     [HVM_VOL_VMENTRY]="vmentry",
     [HVM_VOL_VMEXIT] ="vmexit",
+    [HVM_VOL_VMENTRY_NESTED]="vmentry(n)",
+    [HVM_VOL_VMEXIT_NESTED] ="vmexit(n)",
     [HVM_VOL_HANDLER]="handler",
     [HVM_VOL_EMUL]="emul",
 };
@@ -1380,7 +1384,7 @@ struct hvm_data {
     tsc_t exit_tsc, arc_cycles, entry_tsc;
     unsigned long long rip;
     unsigned exit_reason, event_handler;
-    unsigned int short_summary_done:1, prealloc_unpin:1, wrmap_bf:1;
+    bool nested_vmexit, short_summary_done, prealloc_unpin, wrmap_bf;
 
     /* Immediate processing */
     void *d;
@@ -1807,7 +1811,7 @@ void volume_summary(struct trace_volume *vol)
             case TOPLEVEL_HVM:
                 for(k=0; k<HVM_VOL_MAX; k++) {
                     if(vol->hvm[k])
-                        printf(" +-%-7s: %10lld\n",
+                        printf(" +-%-10s: %10lld\n",
                                hvm_vol_name[k], vol->hvm[k]);
                 }
 
@@ -4633,6 +4637,9 @@ void hvm_generic_postprocess(struct hvm_data *h)
         /* Some exits we don't expect a handler; just return */
         if(opt.svm_mode)
         {
+            /* Nested vmexits passed on to L1's have no handlers for now */
+            if(h->nested_vmexit)
+                return;
             switch(h->exit_reason)
             {
             case VMEXIT_VINTR: /* Equivalent of PENDING_VIRT_INTR */
@@ -4660,8 +4667,10 @@ void hvm_generic_postprocess(struct hvm_data *h)
         if ( !warned[h->exit_reason] )
         {
             /* If we aren't a known exception, warn and log results */
-            fprintf(warn, "%s: Strange, exit %x(%s) missing a handler\n",
-                    __func__, h->exit_reason,
+            fprintf(warn, "%s: d%dv%d Strange, exit %x(%s) missing a handler\n",
+                    __func__,
+                    h->v->d->did, h->v->vid,
+                    h->exit_reason,
                     (h->exit_reason > h->exit_reason_max)
                       ? "[clipped]"
                       : h->exit_reason_name[h->exit_reason]);
@@ -5067,6 +5076,9 @@ static inline void runstate_update(struct vcpu_data *v, int new_runstate,
 
 void hvm_vmexit_process(struct record_info *ri, struct hvm_data *h,
                         struct vcpu_data *v) {
+    bool flag64 = ri->event & TRC_64_FLAG;
+    bool nested = ri->event & TRC_HVM_NESTEDFLAG;
+
     struct {
         union {
             struct {
@@ -5080,7 +5092,7 @@ void hvm_vmexit_process(struct record_info *ri, struct hvm_data *h,
         };
     } *r;
 
-    if ( ri->event & TRC_64_FLAG )
+    if(flag64)
     {
         if (check_extra_words(ri, sizeof(r->x64), "vmexit"))
             return;
@@ -5097,9 +5109,10 @@ void hvm_vmexit_process(struct record_info *ri, struct hvm_data *h,
         set_hvm_exit_reason_data(h, ri->event);
 
     h->vmexit_valid=1;
+    h->nested_vmexit=nested;
     bzero(&h->inflight, sizeof(h->inflight));
 
-    if(ri->event & TRC_64_FLAG) {
+    if(flag64) {
         if(v->guest_paging_levels != 4)
         {
             if ( verbosity >= 6 )
@@ -5145,14 +5158,16 @@ void hvm_vmexit_process(struct record_info *ri, struct hvm_data *h,
     if(opt.dump_all) {
         if ( h->exit_reason < h->exit_reason_max
              && h->exit_reason_name[h->exit_reason] != NULL)
-            printf("]%s vmexit exit_reason %s eip %llx%s\n",
+            printf("]%s vmexit%s exit_reason %s eip %llx%s\n",
                    ri->dump_header,
+                   nested ? "(n)" : "",
                    h->exit_reason_name[h->exit_reason],
                    h->rip,
                    find_symbol(h->rip));
         else
-            printf("]%s vmexit exit_reason %x eip %llx%s\n",
+            printf("]%s vmexit%s exit_reason %x eip %llx%s\n",
                    ri->dump_header,
+                   nested ? "(n)" : "",
                    h->exit_reason,
                    h->rip,
                    find_symbol(h->rip));
@@ -5244,11 +5259,13 @@ void hvm_close_vmexit(struct hvm_data *h, tsc_t tsc) {
 }
 
 void hvm_vmentry_process(struct record_info *ri, struct hvm_data *h) {
+    bool nested = ri->event & TRC_HVM_NESTEDFLAG;
     if(!h->init)
     {
         if(opt.dump_all)
-            printf("!%s vmentry\n",
-                   ri->dump_header);
+            printf("!%s vmentry%s\n",
+                   ri->dump_header,
+                   nested ? "(n)" : "");
         return;
     }
 
@@ -5283,15 +5300,18 @@ void hvm_vmentry_process(struct record_info *ri, struct hvm_data *h) {
     if(!h->vmexit_valid)
     {
         if(opt.dump_all)
-            printf("!%s vmentry\n",
-                   ri->dump_header);
+            printf("!%s vmentry%s\n",
+                   ri->dump_header,
+                   nested ? "(n)" : "");
         return;
     }
 
     if(opt.dump_all) {
         unsigned long long arc_cycles = ri->tsc - h->exit_tsc;
-        printf("]%s vmentry cycles %lld %s\n",
-               ri->dump_header, arc_cycles, (arc_cycles>10000)?"!":"");
+        printf("]%s vmentry%s cycles %lld %s\n",
+               ri->dump_header,
+               nested ? "(n)" : "",
+               arc_cycles, (arc_cycles>10000)?"!":"");
     }
 
     hvm_close_vmexit(h, ri->tsc);
@@ -5323,16 +5343,20 @@ void hvm_process(struct pcpu_info *p)
         /* FIXME: Collect analysis on this */
         break;
     default:
-        switch(ri->event) {
+        switch(ri->event & ~(TRC_HVM_NESTEDFLAG|TRC_64_FLAG)) {
         case TRC_HVM_VMX_EXIT:
-        case TRC_HVM_VMX_EXIT64:
         case TRC_HVM_SVM_EXIT:
-        case TRC_HVM_SVM_EXIT64:
-            UPDATE_VOLUME(p, hvm[HVM_VOL_VMEXIT], ri->size);
+            if (ri->event & TRC_HVM_NESTEDFLAG)
+                UPDATE_VOLUME(p, hvm[HVM_VOL_VMEXIT_NESTED], ri->size);
+            else
+                UPDATE_VOLUME(p, hvm[HVM_VOL_VMEXIT], ri->size);
             hvm_vmexit_process(ri, h, v);
             break;
         case TRC_HVM_VMENTRY:
-            UPDATE_VOLUME(p, hvm[HVM_VOL_VMENTRY], ri->size);
+            if (ri->event & TRC_HVM_NESTEDFLAG)
+                UPDATE_VOLUME(p, hvm[HVM_VOL_VMENTRY_NESTED], ri->size);
+            else
+                UPDATE_VOLUME(p, hvm[HVM_VOL_VMENTRY], ri->size);
             hvm_vmentry_process(ri, &p->current->hvm);
             break;
         default:
@@ -6977,7 +7001,7 @@ void vcpu_start(struct pcpu_info *p, struct vcpu_data *v,
         /* Change default domain to 'queued' */
         runstate_update(p->current, RUNSTATE_QUEUED, p->first_tsc);
 
-        /* 
+        /*
          * Set current to NULL, so that if another vcpu (not in INIT)
          * is scheduled here, we don't trip over the check in
          * vcpu_next_update()
@@ -6997,7 +7021,7 @@ void vcpu_start(struct pcpu_info *p, struct vcpu_data *v,
 
         return;
     } else if ( v->delayed_init ) {
-        fprintf(warn, "d%dv%d RUNSTATE_RUNNING detected, leaving INIT",
+        fprintf(warn, "d%dv%d RUNSTATE_RUNNING detected, leaving INIT\n",
                 v->d->did, v->vid);
         v->delayed_init = 0;
     }
Signature · 1 line
2.25.1
#4 lore raw

PATCH 4/14 xenalyze: Track generic event information when not in summary mode

In reply to George Dunlap

Generally speaking, a VMEXIT/VMENTRY cycle should have at least three trace records: the VMEXIT trace (which contains the processor-specific exit code), a more generic Xen-based Xen event (an HVM_HANDLER trace record), and a VMEXIT trace; and any given VMEXIT exit reason should only have a single HVM_HANDLER trace type. Having duplicate or missing HVM_HANDLER traces is generally indicative of a problem that's crept in in the hypervisor tracing scheme.

This is property is checked in hvm_generic_postprocess(), and violations are flagged with a warning. In order to do this, when an HVM trace record that doesn't have a specific post-processor happens, information about the HVM trace record is stored in hvm_data->inflight.generic.

Unfortunately, while the check was being done in all "modes", the relevant information was only being copied into inflight.generic in summary mode. This resulted in spurious warnings about missing HVM_HANDLER traces when running in dump mode.

Since running in dump mode is often critical to understanding how the warnings came about, just collect the information always as well.

That said, the data from the trace doesn't appear to be used by anyone; so to save some time, don't bother copying it.

---
 tools/xentrace/xenalyze.c | 13 ++++---------
 1 file changed, 4 insertions(+), 9 deletions(-)
tools/xentrace/xenalyze.c +4 -9
index 52ee7a5f9f..46248e9a70 100644
@@ -1367,7 +1367,6 @@ struct hvm_data {
             } msr;
             struct {
                 unsigned int event;
-                uint32_t d[4];
             } generic;
             struct {
                 unsigned eax;
@@ -4572,8 +4571,7 @@ void hvm_npf_process(struct record_info *ri, struct hvm_data *h)
                (unsigned long long)r->gpa, r->qualification,
                (unsigned long long)r->mfn, r->p2mt);
 
-    if ( opt.summary_info )
-        hvm_generic_postprocess_init(ri, h);
+    hvm_generic_postprocess_init(ri, h);
 }
 
 void hvm_rdtsc_process(struct record_info *ri, struct hvm_data *h)
@@ -4621,7 +4619,6 @@ void hvm_generic_postprocess_init(struct record_info *ri, struct hvm_data *h)
         fprintf(warn, "%s: Strange, h->postprocess set!\n",
                 __func__);
     h->inflight.generic.event = ri->event;
-    bcopy(h->d, h->inflight.generic.d, sizeof(unsigned int) * 4);
 }
 
 void hvm_generic_postprocess(struct hvm_data *h)
@@ -4899,8 +4896,7 @@ needs_vmexit:
     default:
         if(opt.dump_all)
             hvm_generic_dump(ri, "]");
-        if(opt.summary_info)
-            hvm_generic_postprocess_init(ri, h);
+        hvm_generic_postprocess_init(ri, h);
         break;
     }
 }
@@ -6166,11 +6162,10 @@ void shadow_fault_generic_process(struct record_info *ri, struct hvm_data *h)
 
     /* pf-case traces, vs others */
     h->inflight.generic.event = ri->event;
-    bcopy(ri->d, h->inflight.generic.d, sizeof(unsigned int) * 4);
 
     if(opt.dump_all)
-        shadow_fault_generic_dump(h->inflight.generic.event,
-                                  h->inflight.generic.d,
+        shadow_fault_generic_dump(ri->event,
+                                  ri->d,
                                   "]", ri->dump_header);
 
     h->inflight.pf_xen.pf_case = sevt.minor;
Signature · 1 line
2.25.1
#5 lore raw

PATCH 5/14 xenalyze: Ignore vmexits where an HVM_HANDLER traces would be redundant

In reply to George Dunlap

VMX combines all exceptions into a single VMEXIT exit reason, and so needs a separate HVM_HANDLER trace record (HVM_TRAP) to say which exception happened; but for a number of exceptions, no further information is put into the trace log.

SVM, by contrast, has a separate VMEXIT exit reason for each exception vector, so HVM_TRAP would be redundant.

NB that VMEXIT_EXCEPTION_DB doesn't have an HVM_HANDLER for SVM yet; but for VMX, there's a specific HVM_HANDLER trace record which includes more information; so SVM really should record information as well.

---
 tools/xentrace/xenalyze.c | 8 ++++++++
 1 file changed, 8 insertions(+)
tools/xentrace/xenalyze.c +8 -0
index 46248e9a70..19b99dc66d 100644
@@ -4641,6 +4641,14 @@ void hvm_generic_postprocess(struct hvm_data *h)
             {
             case VMEXIT_VINTR: /* Equivalent of PENDING_VIRT_INTR */
             case VMEXIT_PAUSE:
+                /*
+                 * VMX just has TRC_HVM_TRAP for these, which would be
+                 * redundant on SVM
+                 */
+            case VMEXIT_EXCEPTION_BP:
+            case VMEXIT_EXCEPTION_NM:
+            case VMEXIT_EXCEPTION_AC:
+            case VMEXIT_EXCEPTION_UD:
                 return;
             default:
                 break;
Signature · 1 line
2.25.1
#6 lore raw

PATCH 6/14 xen/svm: Remove redundant HVM_HANDLER trace for EXCEPTION_AC

In reply to George Dunlap

Adding an HVM_TRAP trace record is redundant for EXCEPTION_AC: it adds trace volume without adding any information, and xenalyze already knows not to expect it. Remove it.

---
 xen/arch/x86/hvm/svm/svm.c | 1 -
 1 file changed, 1 deletion(-)
xen/arch/x86/hvm/svm/svm.c +0 -1
index 988250dbc1..abe665ee43 100644
@@ -2779,7 +2779,6 @@ void asmlinkage svm_vmexit_handler(void)
     }
 
     case VMEXIT_EXCEPTION_AC:
-        TRACE(TRC_HVM_TRAP, X86_EXC_AC);
         hvm_inject_hw_exception(X86_EXC_AC, vmcb->ei.exc.ec);
         break;
 
Signature · 1 line
2.25.1
#7 lore raw

PATCH 7/14 xen/hvm: Don't skip MSR_READ trace record

In reply to George Dunlap

Commit 37f074a3383 ("x86/msr: introduce guest_rdmsr()") introduced a function to combine the MSR_READ handling between PV and HVM. Unfortunately, by returning directly, it skipped the trace generation, leading to gaps in the trace record, as well as xenalyze errors like this:

hvm_generic_postprocess: d2v0 Strange, exit 7c(VMEXIT_MSR) missing a handler

Replace the return with goto out.

Fixes 37f074a3383 ("x86/msr: introduce guest_rdmsr()")
---
 xen/arch/x86/hvm/hvm.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
xen/arch/x86/hvm/hvm.c +1 -1
index 7f4b627b1f..0fe2b85b16 100644
@@ -3557,7 +3557,7 @@ int hvm_msr_read_intercept(unsigned int msr, uint64_t *msr_content)
     fixed_range_base = (uint64_t *)v->arch.hvm.mtrr.fixed_ranges;
 
     if ( (ret = guest_rdmsr(v, msr, msr_content)) != X86EMUL_UNHANDLEABLE )
-        return ret;
+        goto out;
 
     ret = X86EMUL_OKAY;
 
Signature · 1 line
2.25.1
Reviewed-by on v1 7/14
#8 lore raw

Re: [PATCH WIP 07/14] xen/hvm: Don't skip MSR_READ trace record

Quoted 6 lines · George Dunlap

Commit 37f074a3383 ("x86/msr: introduce guest_rdmsr()") introduced a function to combine the MSR_READ handling between PV and HVM. Unfortunately, by returning directly, it skipped the trace generation, leading to gaps in the trace record, as well as xenalyze errors like this:

Quoted 6 lines

hvm_generic_postprocess: d2v0 Strange, exit 7c(VMEXIT_MSR) missing a handler

Replace the return with goto out.

Fixes 37f074a3383 ("x86/msr: introduce guest_rdmsr()")

Noticed only now that there's a bug fix hidden in this series.

Jan

#9 lore raw

PATCH 8/14 svm: Do NPF trace before calling hvm_hap_nested_page_fault

In reply to George Dunlap

Unfortunately I've forgotten exactly why I made this change. I suspect that there were other traces (like MMIO traces) which were being put before the NPF trace; but to understand the trace record you'd want to have the NPF information first.

---
 xen/arch/x86/hvm/svm/svm.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
xen/arch/x86/hvm/svm/svm.c +2 -2
index abe665ee43..240401dc77 100644
@@ -1614,8 +1614,6 @@ static void svm_do_nested_pgfault(struct vcpu *v,
     else if ( pfec & NPT_PFEC_in_gpt )
         npfec.kind = npfec_kind_in_gpt;
 
-    ret = hvm_hap_nested_page_fault(gpa, ~0UL, npfec);
-
     if ( tb_init_done )
     {
         struct {
@@ -1636,6 +1634,8 @@ static void svm_do_nested_pgfault(struct vcpu *v,
         trace(TRC_HVM_NPF, sizeof(_d), &_d);
     }
 
+    ret = hvm_hap_nested_page_fault(gpa, ~0UL, npfec);
+
     switch ( ret )
     {
     case 1:
Signature · 1 line
2.25.1
#10 lore raw

PATCH 9/14 x86/emulate: Don't trace cr reads during emulation

In reply to George Dunlap

hvmemul_read_cr ends up being called fairly freqently during emulation; these are generally not necessary for understanding a trace, and make processing more complicated (because they show up as extra trace records within a vmexit/entry "arc" that must be classified).

Leave the trace in write_cr, as any hypothetical writes to CRs *will* be necessary to understand traces.

---
 xen/arch/x86/hvm/emulate.c | 1 -
 1 file changed, 1 deletion(-)
xen/arch/x86/hvm/emulate.c +0 -1
index 02e378365b..e78f06cf3f 100644
@@ -2180,7 +2180,6 @@ static int cf_check hvmemul_read_cr(
     case 3:
     case 4:
         *val = current->arch.hvm.guest_cr[reg];
-        TRACE(TRC_HVM_CR_READ64, reg, *val, *val >> 32);
         return X86EMUL_OKAY;
     default:
         break;
Signature · 1 line
2.25.1
#11 lore raw

PATCH 10/14 xenalyze: Quiet warnings about VMEXIT_IOIO

In reply to George Dunlap

There's a general issue with both PIO and MMIO reads (as detailed in the comment); do a work-around for now.

---
 tools/xentrace/xenalyze.c | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)
tools/xentrace/xenalyze.c +18 -0
index 19b99dc66d..eb0e60e6ef 100644
@@ -4650,6 +4650,24 @@ void hvm_generic_postprocess(struct hvm_data *h)
             case VMEXIT_EXCEPTION_AC:
             case VMEXIT_EXCEPTION_UD:
                 return;
+            case VMEXIT_IOIO:
+                /*
+                 * FIXME: Both IO and MMIO reads which have gone out
+                 * to the emulator and back typically have the
+                 * [mm]io_assist trace happen on resume, just before
+                 * the subsequent VMENTRY.
+                 *
+                 * However, when a VM has blocked, we call
+                 * hvm_vmexit_close() when it becomes RUNNABLE again,
+                 * rather than when it actually runs again; meaning
+                 * that when hvm_vmexit_close() is called, no HVM
+                 * handler has been seen.
+                 *
+                 * What we really need is some sort of intermediate
+                 * state for [mm]io reads; but for now just ignore
+                 * VMEXIT_IOIO exits without a handler.
+                 */
+                return;
             default:
                 break;
             }
Signature · 1 line
2.25.1
#12 lore raw

PATCH 11/14 x86/trace: Add trace to xsetbv svm/vmx handler path

In reply to George Dunlap

There are already "HVM handler" trace records for writing to XCRs in the context of an HVM guest. This trace is currently taken in hvmemul_write_xcr.

However, both VMX and SVM vmexits call hvm_handle_xsetbv as a result of an XSETBV vmexit, and hvm_handle_xsetbv calls x86emul_write_xcr directly, bypassing the trace, resulting in no "HVM handler" trace record for that VMEXIT.

For maximal DRY-ness, we would want hvm_handle_xsetbv to call hvmemul_write_xcr; but since the intent seems to be for hvmemul_* to be only accesible via hvm_emulate(), just duplicate the trace.

---
CC: Andrew Cooper <[email protected]>
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monne <[email protected]>

---
 xen/arch/x86/hvm/hvm.c | 2 ++
 1 file changed, 2 insertions(+)
xen/arch/x86/hvm/hvm.c +2 -0
index 0fe2b85b16..628a131399 100644
@@ -2073,6 +2073,8 @@ int hvm_handle_xsetbv(u32 index, u64 new_bv)
     if ( index == 0 )
         hvm_monitor_crX(XCR0, new_bv, current->arch.xcr0);
 
+    TRACE(TRC_HVM_XCR_WRITE64, index, new_bv, new_bv >> 32);
+
     rc = x86emul_write_xcr(index, new_bv, NULL);
     if ( rc != X86EMUL_OKAY )
         hvm_inject_hw_exception(X86_EXC_GP, 0);
Signature · 1 line
2.25.1
#13 lore raw

PATCH 12/14 xenalyze: Basic processing for XSETBV exits and handlers

In reply to George Dunlap

Basically this means adding VMEXIT strings for XSETBV exit, and adding the handlers and strings for them.

---
 tools/xentrace/xenalyze.c | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)
tools/xentrace/xenalyze.c +9 -1
index eb0e60e6ef..d0e2788727 100644
@@ -694,6 +694,8 @@ enum VMEXIT_EXITCODE
     VMEXIT_MONITOR          = 138,
     VMEXIT_MWAIT            = 139,
     VMEXIT_MWAIT_CONDITIONAL= 140,
+    VMEXIT_XSETBV           = 141, /* 0x8d */
+    VMEXIT_RDPRU            = 142, /* 0x8e */
     VMEXIT_NPF              = 1024, /* nested paging fault */
     VMEXIT_INVALID          =  -1
 };
@@ -853,6 +855,8 @@ const char * hvm_svm_exit_reason_name[HVM_SVM_EXIT_REASON_MAX] = {
     "VMEXIT_MWAIT",
     /* 140 */
     "VMEXIT_MWAIT_CONDITIONAL",
+    "VMEXIT_XSETBV",
+    "VMEXIT_RDPRU",
     [VMEXIT_NPF] = "VMEXIT_NPF", /* nested paging fault */
 };
 
@@ -946,6 +950,8 @@ enum {
     HVM_EVENT_TRAP,
     HVM_EVENT_TRAP_DEBUG,
     HVM_EVENT_VLAPIC,
+    HVM_EVENT_XCR_READ,
+    HVM_EVENT_XCR_WRITE,
     HVM_EVENT_HANDLER_MAX
 };
 const char * hvm_event_handler_name[HVM_EVENT_HANDLER_MAX] = {
@@ -981,7 +987,9 @@ const char * hvm_event_handler_name[HVM_EVENT_HANDLER_MAX] = {
     "realmode_emulate",
     "trap",
     "trap_debug",
-    "vlapic"
+    "vlapic",
+    "xcr_read",
+    "xcr_write"
 };
 
 enum {
Signature · 1 line
2.25.1
#14 lore raw

PATCH 13/14 x86/svm: Add a trace for VMEXIT_VMRUN

In reply to George Dunlap

Note that this trace is SVM-specific. Most HVM handler traces are shared between VMX and SVM because the underlying instruction set is largely the equivalent; but in this case, the instructions are different enough that there's no sensible way to share HVM handler traces between them.

Keeping the target VMCB address should allow future analysis of which L2 vcpu within an L1 is running.

---
 tools/xentrace/xenalyze.c  | 20 +++++++++++++++++++-
 xen/arch/x86/hvm/svm/svm.c |  2 ++
 xen/include/public/trace.h |  1 +
 3 files changed, 22 insertions(+), 1 deletion(-)
tools/xentrace/xenalyze.c +19 -1
index d0e2788727..d1988b4232 100644
@@ -952,6 +952,7 @@ enum {
     HVM_EVENT_VLAPIC,
     HVM_EVENT_XCR_READ,
     HVM_EVENT_XCR_WRITE,
+    HVM_EVENT_VMRUN,
     HVM_EVENT_HANDLER_MAX
 };
 const char * hvm_event_handler_name[HVM_EVENT_HANDLER_MAX] = {
@@ -989,7 +990,8 @@ const char * hvm_event_handler_name[HVM_EVENT_HANDLER_MAX] = {
     "trap_debug",
     "vlapic",
     "xcr_read",
-    "xcr_write"
+    "xcr_write",
+    "vmrun"
 };
 
 enum {
@@ -4610,6 +4612,19 @@ void hvm_rdtsc_process(struct record_info *ri, struct hvm_data *h)
     h->last_rdtsc = r->tsc;
 }
 
+
+void hvm_vmrun_process(struct record_info *ri, struct hvm_data *h)
+{
+    struct {
+        uint64_t vmcbaddr;
+    } *r = (typeof(r))h->d;
+
+    if ( opt.dump_all )
+        printf(" %s vmrun %llx\n",
+               ri->dump_header,
+               (unsigned long long)r->vmcbaddr);
+}
+
 void hvm_generic_summary(struct hvm_data *h, void *data)
 {
     long evt = (long)data;
@@ -4910,6 +4925,9 @@ needs_vmexit:
     case TRC_HVM_RDTSC:
         hvm_rdtsc_process(ri, h);
         break;
+    case TRC_HVM_VMRUN:
+        hvm_vmrun_process(ri, h);
+        break;
     case TRC_HVM_DR_READ:
     case TRC_HVM_DR_WRITE:
     case TRC_HVM_CPUID:
xen/arch/x86/hvm/svm/svm.c +2 -0
index 240401dc77..39af0ffdd6 100644
@@ -2177,6 +2177,8 @@ svm_vmexit_do_vmrun(struct cpu_user_regs *regs,
         return;
     }
 
+    TRACE(TRC_HVM_VMRUN, vmcbaddr, vmcbaddr >> 32);
+
     vcpu_nestedhvm(v).nv_vmentry_pending = 1;
     return;
 }
xen/include/public/trace.h +1 -0
index 141efa0ea7..19a29a5929 100644
@@ -222,6 +222,7 @@
 #define TRC_HVM_VLAPIC           (TRC_HVM_HANDLER + 0x25)
 #define TRC_HVM_XCR_READ64      (TRC_HVM_HANDLER + TRC_64_FLAG + 0x26)
 #define TRC_HVM_XCR_WRITE64     (TRC_HVM_HANDLER + TRC_64_FLAG + 0x27)
+#define TRC_HVM_VMRUN           (TRC_HVM_HANDLER + 0x28)
 
 #define TRC_HVM_IOPORT_WRITE    (TRC_HVM_HANDLER + 0x216)
 #define TRC_HVM_IOMEM_WRITE     (TRC_HVM_HANDLER + 0x217)
Signature · 1 line
2.25.1
#15 lore raw

PATCH 14/14 x86/nestedsvm: Note some places for improvement

In reply to George Dunlap
---
 xen/arch/x86/hvm/svm/nestedsvm.c | 13 +++++++++++++
 1 file changed, 13 insertions(+)
xen/arch/x86/hvm/svm/nestedsvm.c +13 -0
index 35a2cbfd7d..dca06f2a6c 100644
@@ -1349,8 +1349,21 @@ nestedsvm_check_intercepts(struct vcpu *v, struct cpu_user_regs *regs,
         return NESTEDHVM_VMEXIT_INJECT;
     case VMEXIT_VMMCALL:
         /* Always let the guest handle VMMCALL/VMCALL */
+        /*
+         * FIXME: This is wrong; if the L1 hasn't set the VMMCALL
+         * intercept and the L2 executes a VMMACALL, it should result
+         * in a #UD, not a VMMCALL exception.
+         */
         return NESTEDHVM_VMEXIT_INJECT;
     default:
+        /*
+         * FIXME: It's not true that any VMEXIT not intercepted by L1
+         * can safely be handled "safely" by L0; VMCALL above is one
+         * such example, but there may be more.  We either need to
+         * combine this switch statement into the one in
+         * nsvm_vmcb_guest_intercepts_exitcode(), or explicitly list
+         * known-safe exits here.
+         */
         break;
     }
 
Signature · 1 line
2.25.1