xref: /freebsd/sys/x86/include/kvm.h (revision 994297b01b98816bea1abf45ae4bac1bc69ee7a0)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
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  * $FreeBSD$
31  */
32 
33 /*
34  * Linux KVM paravirtualization: common definitions
35  *
36  * References:
37  *     - [1] https://www.kernel.org/doc/html/latest/virt/kvm/cpuid.html
38  *     - [2] https://www.kernel.org/doc/html/latest/virt/kvm/msr.html
39  */
40 
41 #ifndef _X86_KVM_H_
42 #define	_X86_KVM_H_
43 
44 #include <sys/types.h>
45 #include <sys/systm.h>
46 
47 #include <machine/md_var.h>
48 
49 #define	KVM_CPUID_SIGNATURE			0x40000000
50 #define	KVM_CPUID_FEATURES_LEAF			0x40000001
51 
52 #define	KVM_FEATURE_CLOCKSOURCE			0x00000001
53 #define	KVM_FEATURE_CLOCKSOURCE2		0x00000008
54 #define	KVM_FEATURE_CLOCKSOURCE_STABLE_BIT	0x01000000
55 
56 /* Deprecated: for the CLOCKSOURCE feature. */
57 #define	KVM_MSR_WALL_CLOCK			0x11
58 #define	KVM_MSR_SYSTEM_TIME			0x12
59 
60 #define	KVM_MSR_WALL_CLOCK_NEW			0x4b564d00
61 #define	KVM_MSR_SYSTEM_TIME_NEW			0x4b564d01
62 
63 static inline bool
64 kvm_cpuid_features_leaf_supported(void)
65 {
66 	return (vm_guest == VM_GUEST_KVM &&
67 	    KVM_CPUID_FEATURES_LEAF > hv_base &&
68 	    KVM_CPUID_FEATURES_LEAF <= hv_high);
69 }
70 
71 static inline void
72 kvm_cpuid_get_features(u_int *regs)
73 {
74 	if (!kvm_cpuid_features_leaf_supported())
75 		regs[0] = regs[1] = regs[2] = regs[3] = 0;
76 	else
77 		do_cpuid(KVM_CPUID_FEATURES_LEAF, regs);
78 }
79 
80 #endif /* !_X86_KVM_H_ */
81