1/*-
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright (c) 2014 Bryan Venteicher <bryanv@FreeBSD.org>
5 * Copyright (c) 2021 Mathieu Chouquet-Stringer
6 * Copyright (c) 2021 Juniper Networks, Inc.
7 * Copyright (c) 2021 Klara, Inc.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 *    notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 *    notice, this list of conditions and the following disclaimer in the
16 *    documentation and/or other materials provided with the distribution.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 */
30
31/*
32 * Linux KVM paravirtualization: common definitions
33 *
34 * References:
35 *     - [1] https://www.kernel.org/doc/html/latest/virt/kvm/cpuid.html
36 *     - [2] https://www.kernel.org/doc/html/latest/virt/kvm/msr.html
37 */
38
39#ifndef _X86_KVM_H_
40#define	_X86_KVM_H_
41
42#include <sys/types.h>
43#include <sys/systm.h>
44
45#include <machine/md_var.h>
46
47#define	KVM_CPUID_SIGNATURE			0x40000000
48#define	KVM_CPUID_FEATURES_LEAF			0x40000001
49
50#define	KVM_FEATURE_CLOCKSOURCE			0x00000001
51#define	KVM_FEATURE_CLOCKSOURCE2		0x00000008
52#define	KVM_FEATURE_CLOCKSOURCE_STABLE_BIT	0x01000000
53
54/* Deprecated: for the CLOCKSOURCE feature. */
55#define	KVM_MSR_WALL_CLOCK			0x11
56#define	KVM_MSR_SYSTEM_TIME			0x12
57
58#define	KVM_MSR_WALL_CLOCK_NEW			0x4b564d00
59#define	KVM_MSR_SYSTEM_TIME_NEW			0x4b564d01
60
61static inline bool
62kvm_cpuid_features_leaf_supported(void)
63{
64	return (vm_guest == VM_GUEST_KVM &&
65	    KVM_CPUID_FEATURES_LEAF > hv_base &&
66	    KVM_CPUID_FEATURES_LEAF <= hv_high);
67}
68
69static inline void
70kvm_cpuid_get_features(u_int *regs)
71{
72	if (!kvm_cpuid_features_leaf_supported())
73		regs[0] = regs[1] = regs[2] = regs[3] = 0;
74	else
75		do_cpuid(KVM_CPUID_FEATURES_LEAF, regs);
76}
77
78#endif /* !_X86_KVM_H_ */
79