From: Vincent Donnefort <vdonnefort@google.com>
To: rostedt@goodmis.org, mhiramat@kernel.org,
linux-trace-kernel@vger.kernel.org, maz@kernel.org,
oliver.upton@linux.dev
Cc: kvmarm@lists.linux.dev, will@kernel.org, qperret@google.com,
kernel-team@android.com, linux-kernel@vger.kernel.org,
Vincent Donnefort <vdonnefort@google.com>
Subject: [PATCH 06/13] KVM: arm64: Add clock support in the nVHE hyp
Date: Wed, 11 Sep 2024 10:30:22 +0100 [thread overview]
Message-ID: <20240911093029.3279154-7-vdonnefort@google.com> (raw)
In-Reply-To: <20240911093029.3279154-1-vdonnefort@google.com>
By default, the arm64 host kernel is using the arch timer as a source
for sched_clock. Conveniently, EL2 has access to that same counter,
allowing to generate clock values that are synchronized.
The clock needs nonetheless to be setup with the same slope values as
the kernel. Introducing at the same time trace_clock() which is expected
to be later configured by the hypervisor tracing.
Signed-off-by: Vincent Donnefort <vdonnefort@google.com>
diff --git a/arch/arm64/include/asm/kvm_hyp.h b/arch/arm64/include/asm/kvm_hyp.h
index c838309e4ec4..355bae0056f0 100644
--- a/arch/arm64/include/asm/kvm_hyp.h
+++ b/arch/arm64/include/asm/kvm_hyp.h
@@ -144,5 +144,4 @@ extern u64 kvm_nvhe_sym(id_aa64smfr0_el1_sys_val);
extern unsigned long kvm_nvhe_sym(__icache_flags);
extern unsigned int kvm_nvhe_sym(kvm_arm_vmid_bits);
extern unsigned int kvm_nvhe_sym(kvm_host_sve_max_vl);
-
#endif /* __ARM64_KVM_HYP_H__ */
diff --git a/arch/arm64/kvm/hyp/include/nvhe/clock.h b/arch/arm64/kvm/hyp/include/nvhe/clock.h
new file mode 100644
index 000000000000..2bd05b3b89f9
--- /dev/null
+++ b/arch/arm64/kvm/hyp/include/nvhe/clock.h
@@ -0,0 +1,16 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef __ARM64_KVM_HYP_NVHE_CLOCK_H
+#define __ARM64_KVM_HYP_NVHE_CLOCK_H
+#include <linux/types.h>
+
+#include <asm/kvm_hyp.h>
+
+#ifdef CONFIG_TRACING
+void trace_clock_update(u32 mult, u32 shift, u64 epoch_ns, u64 epoch_cyc);
+u64 trace_clock(void);
+#else
+static inline void
+trace_clock_update(u32 mult, u32 shift, u64 epoch_ns, u64 epoch_cyc) { }
+static inline u64 trace_clock(void) { return 0; }
+#endif
+#endif
diff --git a/arch/arm64/kvm/hyp/nvhe/Makefile b/arch/arm64/kvm/hyp/nvhe/Makefile
index b43426a493df..323e992089bd 100644
--- a/arch/arm64/kvm/hyp/nvhe/Makefile
+++ b/arch/arm64/kvm/hyp/nvhe/Makefile
@@ -28,6 +28,7 @@ hyp-obj-y := timer-sr.o sysreg-sr.o debug-sr.o switch.o tlb.o hyp-init.o host.o
hyp-obj-y += ../vgic-v3-sr.o ../aarch32.o ../vgic-v2-cpuif-proxy.o ../entry.o \
../fpsimd.o ../hyp-entry.o ../exception.o ../pgtable.o
hyp-obj-$(CONFIG_LIST_HARDENED) += list_debug.o
+hyp-obj-$(CONFIG_TRACING) += clock.o
hyp-obj-y += $(lib-objs)
##
diff --git a/arch/arm64/kvm/hyp/nvhe/clock.c b/arch/arm64/kvm/hyp/nvhe/clock.c
new file mode 100644
index 000000000000..0d1f74bc2e11
--- /dev/null
+++ b/arch/arm64/kvm/hyp/nvhe/clock.c
@@ -0,0 +1,49 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Copyright (C) 2024 Google LLC
+ * Author: Vincent Donnefort <vdonnefort@google.com>
+ */
+
+#include <nvhe/clock.h>
+
+#include <asm/arch_timer.h>
+#include <asm/div64.h>
+
+static struct clock_data {
+ struct {
+ u32 mult;
+ u32 shift;
+ u64 epoch_ns;
+ u64 epoch_cyc;
+ } data[2];
+ u64 cur;
+} trace_clock_data;
+
+/* Does not guarantee no reader on the modified bank. */
+void trace_clock_update(u32 mult, u32 shift, u64 epoch_ns, u64 epoch_cyc)
+{
+ struct clock_data *clock = &trace_clock_data;
+ u64 bank = clock->cur ^ 1;
+
+ clock->data[bank].mult = mult;
+ clock->data[bank].shift = shift;
+ clock->data[bank].epoch_ns = epoch_ns;
+ clock->data[bank].epoch_cyc = epoch_cyc;
+
+ smp_store_release(&clock->cur, bank);
+}
+
+/* Using host provided data. Do not use for anything else than debugging. */
+u64 trace_clock(void)
+{
+ struct clock_data *clock = &trace_clock_data;
+ u64 bank = smp_load_acquire(&clock->cur);
+ u64 cyc, ns;
+
+ cyc = __arch_counter_get_cntpct() - clock->data[bank].epoch_cyc;
+
+ ns = cyc * clock->data[bank].mult;
+ ns >>= clock->data[bank].shift;
+
+ return (u64)ns + clock->data[bank].epoch_ns;
+}
--
2.46.0.598.g6f2099f65c-goog
next prev parent reply other threads:[~2024-09-11 9:30 UTC|newest]
Thread overview: 23+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-09-11 9:30 [PATCH 00/13] Tracefs support for pKVM Vincent Donnefort
2024-09-11 9:30 ` [PATCH 01/13] ring-buffer: Check for empty ring-buffer with rb_num_of_entries() Vincent Donnefort
2024-09-11 9:30 ` [PATCH 02/13] ring-buffer: Introducing ring-buffer writer Vincent Donnefort
2024-09-12 12:55 ` kernel test robot
2024-09-11 9:30 ` [PATCH 03/13] ring-buffer: Expose buffer_data_page material Vincent Donnefort
2024-09-11 9:30 ` [PATCH 04/13] timekeeping: Add the boot clock to system time snapshot Vincent Donnefort
2024-09-13 22:28 ` John Stultz
2024-10-02 15:15 ` Thomas Gleixner
2024-09-11 9:30 ` [PATCH 05/13] KVM: arm64: Support unaligned fixmap in the nVHE hyp Vincent Donnefort
2024-09-11 9:30 ` Vincent Donnefort [this message]
2024-09-13 22:41 ` [PATCH 06/13] KVM: arm64: Add clock support " John Stultz
2024-09-16 12:26 ` Vincent Donnefort
2024-09-11 9:30 ` [PATCH 07/13] KVM: arm64: Add tracing support for the pKVM hyp Vincent Donnefort
2024-09-11 9:30 ` [PATCH 08/13] KVM: arm64: Add hyp tracing to tracefs Vincent Donnefort
2024-09-11 9:30 ` [PATCH 09/13] KVM: arm64: Add clock for hyp tracefs Vincent Donnefort
2024-09-13 23:21 ` John Stultz
2024-09-16 12:39 ` Vincent Donnefort
2024-09-11 9:30 ` [PATCH 10/13] KVM: arm64: Add raw interface " Vincent Donnefort
2024-09-11 9:30 ` [PATCH 11/13] KVM: arm64: Add trace " Vincent Donnefort
2024-09-11 9:30 ` [PATCH 12/13] KVM: arm64: Add support for hyp events Vincent Donnefort
2024-09-11 9:30 ` [PATCH 13/13] KVM: arm64: Add kselftest for tracefs hyp tracefs Vincent Donnefort
2025-01-07 19:54 ` [PATCH 00/13] Tracefs support for pKVM Steven Rostedt
2025-01-07 21:46 ` Vincent Donnefort
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20240911093029.3279154-7-vdonnefort@google.com \
--to=vdonnefort@google.com \
--cc=kernel-team@android.com \
--cc=kvmarm@lists.linux.dev \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-trace-kernel@vger.kernel.org \
--cc=maz@kernel.org \
--cc=mhiramat@kernel.org \
--cc=oliver.upton@linux.dev \
--cc=qperret@google.com \
--cc=rostedt@goodmis.org \
--cc=will@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox