KVM: MIPS/VZ: Trace guest mode changes
authorJames Hogan <james.hogan@imgtec.com>
Tue, 14 Mar 2017 10:15:40 +0000 (10:15 +0000)
committerJames Hogan <james.hogan@imgtec.com>
Tue, 28 Mar 2017 13:54:00 +0000 (14:54 +0100)
Create a trace event for guest mode changes, and enable VZ's
GuestCtl0.MC bit after the trace event is enabled to trap all guest mode
changes.

The MC bit causes Guest Hardware Field Change (GHFC) exceptions whenever
a guest mode change occurs (such as an exception entry or return from
exception), so we need to handle this exception now. The MC bit is only
enabled when restoring register state, so enabling the trace event won't
take immediate effect.

Tracing guest mode changes can be particularly handy when trying to work
out what a guest OS gets up to before something goes wrong, especially
if the problem occurs as a result of some previous guest userland
exception which would otherwise be invisible in the trace.

Signed-off-by: James Hogan <james.hogan@imgtec.com>
Cc: Paolo Bonzini <pbonzini@redhat.com>
Cc: "Radim Krčmář" <rkrcmar@redhat.com>
Cc: Steven Rostedt <rostedt@goodmis.org>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Ralf Baechle <ralf@linux-mips.org>
Cc: linux-mips@linux-mips.org
Cc: kvm@vger.kernel.org
arch/mips/kvm/mips.c
arch/mips/kvm/trace.h
arch/mips/kvm/vz.c

index e8ddb128b8adc8472b41c0bd4811f16cb0ac16aa..1fc6fef463db15641dff38842d0dd8dce7dc7c34 100644 (file)
@@ -76,6 +76,19 @@ struct kvm_stats_debugfs_item debugfs_entries[] = {
        {NULL}
 };
 
+bool kvm_trace_guest_mode_change;
+
+int kvm_guest_mode_change_trace_reg(void)
+{
+       kvm_trace_guest_mode_change = 1;
+       return 0;
+}
+
+void kvm_guest_mode_change_trace_unreg(void)
+{
+       kvm_trace_guest_mode_change = 0;
+}
+
 /*
  * XXXKYMA: We are simulatoring a processor that has the WII bit set in
  * Config7, so we are "runnable" if interrupts are pending
index affde8a2c584baf7ba7e84890cde04cff4543ed5..a8c7fd7bf6d267bccfbba172f58ae300826330c1 100644 (file)
 #define TRACE_INCLUDE_PATH .
 #define TRACE_INCLUDE_FILE trace
 
+/*
+ * arch/mips/kvm/mips.c
+ */
+extern bool kvm_trace_guest_mode_change;
+int kvm_guest_mode_change_trace_reg(void);
+void kvm_guest_mode_change_trace_unreg(void);
+
 /*
  * Tracepoints for VM enters
  */
@@ -303,6 +310,36 @@ TRACE_EVENT(kvm_guestid_change,
                      __entry->guestid)
 );
 
+TRACE_EVENT_FN(kvm_guest_mode_change,
+           TP_PROTO(struct kvm_vcpu *vcpu),
+           TP_ARGS(vcpu),
+           TP_STRUCT__entry(
+                       __field(unsigned long, epc)
+                       __field(unsigned long, pc)
+                       __field(unsigned long, badvaddr)
+                       __field(unsigned int, status)
+                       __field(unsigned int, cause)
+           ),
+
+           TP_fast_assign(
+                       __entry->epc = kvm_read_c0_guest_epc(vcpu->arch.cop0);
+                       __entry->pc = vcpu->arch.pc;
+                       __entry->badvaddr = kvm_read_c0_guest_badvaddr(vcpu->arch.cop0);
+                       __entry->status = kvm_read_c0_guest_status(vcpu->arch.cop0);
+                       __entry->cause = kvm_read_c0_guest_cause(vcpu->arch.cop0);
+           ),
+
+           TP_printk("EPC: 0x%08lx PC: 0x%08lx Status: 0x%08x Cause: 0x%08x BadVAddr: 0x%08lx",
+                     __entry->epc,
+                     __entry->pc,
+                     __entry->status,
+                     __entry->cause,
+                     __entry->badvaddr),
+
+           kvm_guest_mode_change_trace_reg,
+           kvm_guest_mode_change_trace_unreg
+);
+
 #endif /* _TRACE_KVM_H */
 
 /* This part must be outside protection */
index 44dad9ae5d029398d4bd2565ada5e6f70f3d1249..33bb8c6e1b0526fa04bec6129ddc5a39937d4024 100644 (file)
@@ -1322,6 +1322,18 @@ static enum emulation_result kvm_trap_vz_handle_gsfc(u32 cause, u32 *opc,
        return er;
 }
 
+static enum emulation_result kvm_trap_vz_handle_ghfc(u32 cause, u32 *opc,
+                                                    struct kvm_vcpu *vcpu)
+{
+       /*
+        * Presumably this is due to MC (guest mode change), so lets trace some
+        * relevant info.
+        */
+       trace_kvm_guest_mode_change(vcpu);
+
+       return EMULATE_DONE;
+}
+
 static enum emulation_result kvm_trap_vz_handle_hc(u32 cause, u32 *opc,
                                                   struct kvm_vcpu *vcpu)
 {
@@ -1407,8 +1419,7 @@ static int kvm_trap_vz_handle_guest_exit(struct kvm_vcpu *vcpu)
                break;
        case MIPS_GCTL0_GEXC_GHFC:
                ++vcpu->stat.vz_ghfc_exits;
-               er = kvm_trap_vz_no_handler_guest_exit(gexccode, cause, opc,
-                                                      vcpu);
+               er = kvm_trap_vz_handle_ghfc(cause, opc, vcpu);
                break;
        case MIPS_GCTL0_GEXC_GPA:
                ++vcpu->stat.vz_gpa_exits;
@@ -2459,6 +2470,12 @@ static int kvm_vz_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
         */
        kvm_vz_restore_timer(vcpu);
 
+       /* Set MC bit if we want to trace guest mode changes */
+       if (kvm_trace_guest_mode_change)
+               set_c0_guestctl0(MIPS_GCTL0_MC);
+       else
+               clear_c0_guestctl0(MIPS_GCTL0_MC);
+
        /* Don't bother restoring registers multiple times unless necessary */
        if (!all)
                return 0;