Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

KVM: arm64: Add clock support to nVHE/pKVM hyp

In preparation for supporting tracing from the nVHE hyp, add support to
generate timestamps with a clock fed by the CNTCVT counter. The clock
can be kept in sync with the kernel's by updating the slope values. This
will be done later.

As current we do only create a trace clock, make the whole support
dependent on the upcoming CONFIG_NVHE_EL2_TRACING.

Signed-off-by: Vincent Donnefort <vdonnefort@google.com>
Link: https://patch.msgid.link/20260309162516.2623589-21-vdonnefort@google.com
Signed-off-by: Marc Zyngier <maz@kernel.org>

authored by

Vincent Donnefort and committed by
Marc Zyngier
405df5b5 9019e82c

+83 -1
+16
arch/arm64/kvm/hyp/include/nvhe/clock.h
··· 1 + /* SPDX-License-Identifier: GPL-2.0 */ 2 + #ifndef __ARM64_KVM_HYP_NVHE_CLOCK_H 3 + #define __ARM64_KVM_HYP_NVHE_CLOCK_H 4 + #include <linux/types.h> 5 + 6 + #include <asm/kvm_hyp.h> 7 + 8 + #ifdef CONFIG_NVHE_EL2_TRACING 9 + void trace_clock_update(u32 mult, u32 shift, u64 epoch_ns, u64 epoch_cyc); 10 + u64 trace_clock(void); 11 + #else 12 + static inline void 13 + trace_clock_update(u32 mult, u32 shift, u64 epoch_ns, u64 epoch_cyc) { } 14 + static inline u64 trace_clock(void) { return 0; } 15 + #endif 16 + #endif
+2 -1
arch/arm64/kvm/hyp/nvhe/Makefile
··· 17 17 hostprogs := gen-hyprel 18 18 HOST_EXTRACFLAGS += -I$(objtree)/include 19 19 20 - lib-objs := clear_page.o copy_page.o memcpy.o memset.o 20 + lib-objs := clear_page.o copy_page.o memcpy.o memset.o tishift.o 21 21 lib-objs := $(addprefix ../../../lib/, $(lib-objs)) 22 22 23 23 CFLAGS_switch.nvhe.o += -Wno-override-init ··· 29 29 ../fpsimd.o ../hyp-entry.o ../exception.o ../pgtable.o 30 30 hyp-obj-y += ../../../kernel/smccc-call.o 31 31 hyp-obj-$(CONFIG_LIST_HARDENED) += list_debug.o 32 + hyp-obj-$(CONFIG_NVHE_EL2_TRACING) += clock.o 32 33 hyp-obj-y += $(lib-objs) 33 34 34 35 ##
+65
arch/arm64/kvm/hyp/nvhe/clock.c
··· 1 + // SPDX-License-Identifier: GPL-2.0 2 + /* 3 + * Copyright (C) 2025 Google LLC 4 + * Author: Vincent Donnefort <vdonnefort@google.com> 5 + */ 6 + 7 + #include <nvhe/clock.h> 8 + 9 + #include <asm/arch_timer.h> 10 + #include <asm/div64.h> 11 + 12 + static struct clock_data { 13 + struct { 14 + u32 mult; 15 + u32 shift; 16 + u64 epoch_ns; 17 + u64 epoch_cyc; 18 + u64 cyc_overflow64; 19 + } data[2]; 20 + u64 cur; 21 + } trace_clock_data; 22 + 23 + static u64 __clock_mult_uint128(u64 cyc, u32 mult, u32 shift) 24 + { 25 + __uint128_t ns = (__uint128_t)cyc * mult; 26 + 27 + ns >>= shift; 28 + 29 + return (u64)ns; 30 + } 31 + 32 + /* Does not guarantee no reader on the modified bank. */ 33 + void trace_clock_update(u32 mult, u32 shift, u64 epoch_ns, u64 epoch_cyc) 34 + { 35 + struct clock_data *clock = &trace_clock_data; 36 + u64 bank = clock->cur ^ 1; 37 + 38 + clock->data[bank].mult = mult; 39 + clock->data[bank].shift = shift; 40 + clock->data[bank].epoch_ns = epoch_ns; 41 + clock->data[bank].epoch_cyc = epoch_cyc; 42 + clock->data[bank].cyc_overflow64 = ULONG_MAX / mult; 43 + 44 + smp_store_release(&clock->cur, bank); 45 + } 46 + 47 + /* Use untrusted host data */ 48 + u64 trace_clock(void) 49 + { 50 + struct clock_data *clock = &trace_clock_data; 51 + u64 bank = smp_load_acquire(&clock->cur); 52 + u64 cyc, ns; 53 + 54 + cyc = __arch_counter_get_cntvct() - clock->data[bank].epoch_cyc; 55 + 56 + if (likely(cyc < clock->data[bank].cyc_overflow64)) { 57 + ns = cyc * clock->data[bank].mult; 58 + ns >>= clock->data[bank].shift; 59 + } else { 60 + ns = __clock_mult_uint128(cyc, clock->data[bank].mult, 61 + clock->data[bank].shift); 62 + } 63 + 64 + return (u64)ns + clock->data[bank].epoch_ns; 65 + }