Index: sys/amd64/conf/GENERIC =================================================================== --- sys/amd64/conf/GENERIC +++ sys/amd64/conf/GENERIC @@ -362,6 +362,9 @@ device virtio_scsi # VirtIO SCSI device device virtio_balloon # VirtIO Memory Balloon device +# Linux KVM support +device kvm_clock # KVM paravirtualized clock + # HyperV drivers and enhancement support device hyperv # HyperV drivers Index: sys/amd64/conf/NOTES =================================================================== --- sys/amd64/conf/NOTES +++ sys/amd64/conf/NOTES @@ -501,6 +501,9 @@ device virtio_random # VirtIO Entropy device device virtio_console # VirtIO Console device +# Linux KVM support +device kvm_clock # KVM paravirtualized clock + # Microsoft Hyper-V enhancement support device hyperv # HyperV drivers Index: sys/conf/files.x86 =================================================================== --- sys/conf/files.x86 +++ sys/conf/files.x86 @@ -335,6 +335,7 @@ x86/x86/fdt_machdep.c optional fdt x86/x86/identcpu.c standard x86/x86/intr_machdep.c standard +x86/x86/kvm_clock.c optional kvm_clock x86/x86/legacy.c standard x86/x86/mca.c standard x86/x86/x86_mem.c optional mem Index: sys/i386/conf/GENERIC =================================================================== --- sys/i386/conf/GENERIC +++ sys/i386/conf/GENERIC @@ -327,6 +327,9 @@ device virtio_scsi # VirtIO SCSI device device virtio_balloon # VirtIO Memory Balloon device +# Linux KVM support +device kvm_clock # KVM paravirtualized clock + # HyperV drivers and enhancement support device hyperv # HyperV drivers Index: sys/i386/conf/NOTES =================================================================== --- sys/i386/conf/NOTES +++ sys/i386/conf/NOTES @@ -723,6 +723,9 @@ device virtio_random # VirtIO Entropy device device virtio_console # VirtIO Console device +# Linux KVM support +device kvm_clock # KVM paravirtualized clock + device hyperv # HyperV drivers ##################################################################### Index: sys/x86/include/kvm.h =================================================================== --- /dev/null +++ sys/x86/include/kvm.h @@ -0,0 +1,44 @@ +/*- + * SPDX-License-Identifier: BSD-2-Clause-FreeBSD + * + * Copyright (c) 2014 Bryan Venteicher + * Copyright (c) 2021 Mathieu Chouquet-Stringer + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#ifndef _X86_KVM_H_ +#define _X86_KVM_H_ + +#define KVM_CPUID_FEATURES_LEAF 0x40000001 + +#define KVM_FEATURE_CLOCKSOURCE 0x00000001 +#define KVM_FEATURE_CLOCKSOURCE2 0x00000008 + +/* Deprecated: for the CLOCKSOURCE feature. */ +#define KVM_MSR_WALL_CLOCK 0x11 +#define KVM_MSR_SYSTEM_TIME 0x12 + +#define KVM_MSR_WALL_CLOCK_NEW 0x4b564d00 +#define KVM_MSR_SYSTEM_TIME_NEW 0x4b564d01 + +#endif /* !_X86_KVM_H_ */ Index: sys/x86/x86/kvm_clock.c =================================================================== --- /dev/null +++ sys/x86/x86/kvm_clock.c @@ -0,0 +1,136 @@ +/*- + * SPDX-License-Identifier: BSD-2-Clause-FreeBSD + * + * Copyright (c) 2014 Bryan Venteicher + * Copyright (c) 2021 Mathieu Chouquet-Stringer + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include +__FBSDID("$FreeBSD$"); + +#include +#include +#include +#include +#include +#include +#include + +#include +#include + +#include +#include +#include + +static u_int kvm_clock_get_timecounter(struct timecounter *); +static void kvm_clock_pcpu_system_time(void *); + +DPCPU_DEFINE(struct pvclock_vcpu_time_info, kvm_clock_vcpu_time_info); + +static struct timecounter kvm_clock_timecounter = { + .tc_get_timecount = kvm_clock_get_timecounter, + .tc_poll_pps = NULL, + .tc_counter_mask = ~0u, + .tc_frequency = 1000000000ULL, + .tc_name = "KVMCLOCK", + .tc_quality = 2000, +}; + +static uint32_t kvm_clock_wall_clock_msr; +static uint32_t kvm_clock_system_time_msr; + +static inline int +kvm_paravirt_supported(void) +{ + return (hv_base > 0); +} + +static u_int +kvm_get_features(void) +{ + u_int regs[4]; + + if (kvm_paravirt_supported()) { + do_cpuid(hv_base | KVM_CPUID_FEATURES_LEAF, regs); + return (regs[0]); + } + return (0); +} + +static u_int +kvm_clock_get_timecounter(struct timecounter *tc) +{ + struct pvclock_vcpu_time_info *ti; + uint64_t time; + + critical_enter(); + ti = DPCPU_PTR(kvm_clock_vcpu_time_info); + time = pvclock_get_timecount(ti); + critical_exit(); + + return (time & UINT_MAX); +} + +static void +kvm_clock_pcpu_system_time(void *arg) +{ + uint64_t data; + + /* + * The magical 1 is the enable bit to tell kvm to turn on this feature. + * It is internally cleared by kvm after being checked to get the proper + * address. + */ + data = vtophys(DPCPU_PTR(kvm_clock_vcpu_time_info)) | 1; + + wrmsr(kvm_clock_system_time_msr, data); +} + +static void +kvm_clock_init(void) +{ + u_int features; + + if (vm_guest != VM_GUEST_KVM || !kvm_paravirt_supported()) + return; + + features = kvm_get_features(); + + if (features & KVM_FEATURE_CLOCKSOURCE2) { + kvm_clock_wall_clock_msr = KVM_MSR_WALL_CLOCK_NEW; + kvm_clock_system_time_msr = KVM_MSR_SYSTEM_TIME_NEW; + } else if (hv_high & KVM_FEATURE_CLOCKSOURCE) { + kvm_clock_wall_clock_msr = KVM_MSR_WALL_CLOCK; + kvm_clock_system_time_msr = KVM_MSR_SYSTEM_TIME; + } else + return; + + smp_rendezvous(smp_no_rendezvous_barrier, kvm_clock_pcpu_system_time, + smp_no_rendezvous_barrier, NULL); + + tc_init(&kvm_clock_timecounter); +} + +SYSINIT(kvm_clock, SI_SUB_SMP, SI_ORDER_ANY, kvm_clock_init, NULL);