Re: [PATCH v2] KVM: x86: Add tracepoint for dr_write/dr_read

From: Haiwei Li
Date: Thu Nov 05 2020 - 19:32:04 EST


Kindly ping. :)

On 20/10/9 11:21, lihaiwei.kernel@xxxxxxxxx wrote:
From: Haiwei Li <lihaiwei@xxxxxxxxxxx>

When vmexit occurs caused by accessing dr, there is no tracepoint to track
this action. Add tracepoint for this on x86 kvm.

Signed-off-by: Haiwei Li <lihaiwei@xxxxxxxxxxx>
---
v1 -> v2:
* Improve the changelog

arch/x86/kvm/svm/svm.c | 2 ++
arch/x86/kvm/trace.h | 27 +++++++++++++++++++++++++++
arch/x86/kvm/vmx/vmx.c | 10 ++++++++--
arch/x86/kvm/x86.c | 1 +
4 files changed, 38 insertions(+), 2 deletions(-)

diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c
index 4f401fc6a05d..52c69551aea4 100644
--- a/arch/x86/kvm/svm/svm.c
+++ b/arch/x86/kvm/svm/svm.c
@@ -2423,12 +2423,14 @@ static int dr_interception(struct vcpu_svm *svm)
if (!kvm_require_dr(&svm->vcpu, dr - 16))
return 1;
val = kvm_register_read(&svm->vcpu, reg);
+ trace_kvm_dr_write(dr - 16, val);
kvm_set_dr(&svm->vcpu, dr - 16, val);
} else {
if (!kvm_require_dr(&svm->vcpu, dr))
return 1;
kvm_get_dr(&svm->vcpu, dr, &val);
kvm_register_write(&svm->vcpu, reg, val);
+ trace_kvm_dr_read(dr, val);
}
return kvm_skip_emulated_instruction(&svm->vcpu);
diff --git a/arch/x86/kvm/trace.h b/arch/x86/kvm/trace.h
index aef960f90f26..b3bf54405862 100644
--- a/arch/x86/kvm/trace.h
+++ b/arch/x86/kvm/trace.h
@@ -405,6 +405,33 @@ TRACE_EVENT(kvm_cr,
#define trace_kvm_cr_read(cr, val) trace_kvm_cr(0, cr, val)
#define trace_kvm_cr_write(cr, val) trace_kvm_cr(1, cr, val)
+/*
+ * Tracepoint for guest DR access.
+ */
+TRACE_EVENT(kvm_dr,
+ TP_PROTO(unsigned int rw, unsigned int dr, unsigned long val),
+ TP_ARGS(rw, dr, val),
+
+ TP_STRUCT__entry(
+ __field( unsigned int, rw )
+ __field( unsigned int, dr )
+ __field( unsigned long, val )
+ ),
+
+ TP_fast_assign(
+ __entry->rw = rw;
+ __entry->dr = dr;
+ __entry->val = val;
+ ),
+
+ TP_printk("dr_%s %x = 0x%lx",
+ __entry->rw ? "write" : "read",
+ __entry->dr, __entry->val)
+);
+
+#define trace_kvm_dr_read(dr, val) trace_kvm_dr(0, dr, val)
+#define trace_kvm_dr_write(dr, val) trace_kvm_dr(1, dr, val)
+
TRACE_EVENT(kvm_pic_set_irq,
TP_PROTO(__u8 chip, __u8 pin, __u8 elcr, __u8 imr, bool coalesced),
TP_ARGS(chip, pin, elcr, imr, coalesced),
diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c
index 4551a7e80ebc..f78fd297d51e 100644
--- a/arch/x86/kvm/vmx/vmx.c
+++ b/arch/x86/kvm/vmx/vmx.c
@@ -5091,10 +5091,16 @@ static int handle_dr(struct kvm_vcpu *vcpu)
if (kvm_get_dr(vcpu, dr, &val))
return 1;
+ trace_kvm_dr_read(dr, val);
kvm_register_write(vcpu, reg, val);
- } else
- if (kvm_set_dr(vcpu, dr, kvm_register_readl(vcpu, reg)))
+ } else {
+ unsigned long val;
+
+ val = kvm_register_readl(vcpu, reg);
+ trace_kvm_dr_write(dr, val);
+ if (kvm_set_dr(vcpu, dr, val))
return 1;
+ }
return kvm_skip_emulated_instruction(vcpu);
}
diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
index c4015a43cc8a..68cb7b331324 100644
--- a/arch/x86/kvm/x86.c
+++ b/arch/x86/kvm/x86.c
@@ -11153,6 +11153,7 @@ EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_inj_virq);
EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_page_fault);
EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_msr);
EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_cr);
+EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_dr);
EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_vmrun);
EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_vmexit);
EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_vmexit_inject);