[PATCH 2/5] kvmtrace: make cycle calculation architecture aware

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



From: Christian Ehrhardt <ehrhardt@xxxxxxxxxxxxxxxxxx>

The current implementation of kvmtrace uses always a 64 bit cycle variable,
but get_cycles() which is used to fill it is "unsigned long" which might be 32
bit.
This reduces the accuracy e.g. on embedded powerpc since we would have a 64bit
value but get_cycle() only returns the low 32 bit.
To solve that this patch introduces kvm_arch_trace_cycles() which allows us
to make this calculation architecture aware. That way every architecture can
insert whatever fits best for their "kvmtrace cycle counter".

Signed-off-by: Christian Ehrhardt <ehrhardt@xxxxxxxxxxxxxxxxxx>
---

[diffstat]
 arch/powerpc/kvm/powerpc.c |   25 +++++++++++++++++++++++++
 arch/x86/kvm/x86.c         |    5 +++++
 include/linux/kvm_host.h   |    2 ++
 virt/kvm/kvm_trace.c       |    2 +-
 4 files changed, 33 insertions(+), 1 deletion(-)

[diff]

diff --git a/arch/powerpc/kvm/powerpc.c b/arch/powerpc/kvm/powerpc.c
--- a/arch/powerpc/kvm/powerpc.c
+++ b/arch/powerpc/kvm/powerpc.c
@@ -521,6 +521,31 @@
 	return r;
 }
 
+/*
+ * We need a 64 bit value here and the default get_cycles has only 32bit (tbl)
+ * on 32bit ppc. Since we have a 64bit counter for that we provide it here in
+ * full resolution for the trace records.
+*/
+__u64 kvm_arch_trace_cycles()
+{
+	unsigned long ruval;
+	unsigned long ruval2;
+	unsigned long rlval;
+
+	/* get a consistant pair of upper/lower timebase (no wrap occured) */
+	asm volatile(
+		"loop:\n"
+		  "mftbu %0\n"
+		  "mftbl %1\n"
+		  "mftbu %2\n"
+		  "cmpw %0, %2\n"
+		  "bne loop"
+		: "=r" (ruval), "=r" (rlval), "=r" (ruval2)
+	);
+
+	return (((__u64)ruval) << 32) | rlval;
+}
+
 int kvm_arch_init(void *opaque)
 {
 	return 0;
diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
--- a/arch/x86/kvm/x86.c
+++ b/arch/x86/kvm/x86.c
@@ -2458,6 +2458,11 @@
 }
 EXPORT_SYMBOL_GPL(kvm_emulate_pio_string);
 
+__u64 kvm_arch_trace_cycles()
+{
+	return get_cycles();
+}
+
 int kvm_arch_init(void *opaque)
 {
 	int r;
diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h
--- a/include/linux/kvm_host.h
+++ b/include/linux/kvm_host.h
@@ -278,6 +278,8 @@
 int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu);
 void kvm_vcpu_kick(struct kvm_vcpu *vcpu);
 
+__u64 kvm_arch_trace_cycles(void);
+
 static inline void kvm_guest_enter(void)
 {
 	account_system_vtime(current);
diff --git a/virt/kvm/kvm_trace.c b/virt/kvm/kvm_trace.c
--- a/virt/kvm/kvm_trace.c
+++ b/virt/kvm/kvm_trace.c
@@ -74,7 +74,7 @@
 				| TRACE_REC_NUM_DATA_ARGS(extra);
 
 	if (p->cycle_in) {
-		rec.u.cycle.cycle_u64 = get_cycles();
+		rec.u.cycle.cycle_u64 = kvm_arch_trace_cycles();
 
 		for (i = 0; i < extra; i++)
 			rec.u.cycle.extra_u32[i] = va_arg(*args, u32);
--
To unsubscribe from this list: send the line "unsubscribe kvm-ppc" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html

[Index of Archives]     [KVM Development]     [KVM ARM]     [KVM ia64]     [Linux Virtualization]     [Linux USB Devel]     [Linux Video]     [Linux Audio Users]     [Linux Kernel]     [Linux SCSI]     [Big List of Linux Books]

  Powered by Linux