xref: /freebsd/sys/amd64/vmm/intel/vmx.h (revision bc7512cc58af2e8bbe5bbf5ca0059b1daa1da897)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2011 NetApp, Inc.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  *
28  * $FreeBSD$
29  */
30 
31 #ifndef _VMX_H_
32 #define	_VMX_H_
33 
34 #include "vmcs.h"
35 #include "x86.h"
36 
37 struct pmap;
38 
39 struct vmxctx {
40 	register_t	guest_rdi;		/* Guest state */
41 	register_t	guest_rsi;
42 	register_t	guest_rdx;
43 	register_t	guest_rcx;
44 	register_t	guest_r8;
45 	register_t	guest_r9;
46 	register_t	guest_rax;
47 	register_t	guest_rbx;
48 	register_t	guest_rbp;
49 	register_t	guest_r10;
50 	register_t	guest_r11;
51 	register_t	guest_r12;
52 	register_t	guest_r13;
53 	register_t	guest_r14;
54 	register_t	guest_r15;
55 	register_t	guest_cr2;
56 	register_t	guest_dr0;
57 	register_t	guest_dr1;
58 	register_t	guest_dr2;
59 	register_t	guest_dr3;
60 	register_t	guest_dr6;
61 
62 	register_t	host_r15;		/* Host state */
63 	register_t	host_r14;
64 	register_t	host_r13;
65 	register_t	host_r12;
66 	register_t	host_rbp;
67 	register_t	host_rsp;
68 	register_t	host_rbx;
69 	register_t	host_dr0;
70 	register_t	host_dr1;
71 	register_t	host_dr2;
72 	register_t	host_dr3;
73 	register_t	host_dr6;
74 	register_t	host_dr7;
75 	uint64_t	host_debugctl;
76 	int		host_tf;
77 
78 	int		inst_fail_status;
79 
80 	/*
81 	 * The pmap needs to be deactivated in vmx_enter_guest()
82 	 * so keep a copy of the 'pmap' in each vmxctx.
83 	 */
84 	struct pmap	*pmap;
85 };
86 
87 struct vmxcap {
88 	int	set;
89 	uint32_t proc_ctls;
90 	uint32_t proc_ctls2;
91 	uint32_t exc_bitmap;
92 };
93 
94 struct vmxstate {
95 	uint64_t nextrip;	/* next instruction to be executed by guest */
96 	int	lastcpu;	/* host cpu that this 'vcpu' last ran on */
97 	uint16_t vpid;
98 };
99 
100 struct apic_page {
101 	uint32_t reg[PAGE_SIZE / 4];
102 };
103 CTASSERT(sizeof(struct apic_page) == PAGE_SIZE);
104 
105 /* Posted Interrupt Descriptor (described in section 29.6 of the Intel SDM) */
106 struct pir_desc {
107 	uint64_t	pir[4];
108 	uint64_t	pending;
109 	uint64_t	unused[3];
110 } __aligned(64);
111 CTASSERT(sizeof(struct pir_desc) == 64);
112 
113 /* Index into the 'guest_msrs[]' array */
114 enum {
115 	IDX_MSR_LSTAR,
116 	IDX_MSR_CSTAR,
117 	IDX_MSR_STAR,
118 	IDX_MSR_SF_MASK,
119 	IDX_MSR_KGSBASE,
120 	IDX_MSR_PAT,
121 	IDX_MSR_TSC_AUX,
122 	GUEST_MSR_NUM		/* must be the last enumeration */
123 };
124 
125 /* virtual machine softc */
126 struct vmx {
127 	struct vmcs	vmcs[VM_MAXCPU];	/* one vmcs per virtual cpu */
128 	struct apic_page apic_page[VM_MAXCPU];	/* one apic page per vcpu */
129 	char		msr_bitmap[PAGE_SIZE];
130 	struct pir_desc	pir_desc[VM_MAXCPU];
131 	uint64_t	guest_msrs[VM_MAXCPU][GUEST_MSR_NUM];
132 	struct vmxctx	ctx[VM_MAXCPU];
133 	struct vmxcap	cap[VM_MAXCPU];
134 	struct vmxstate	state[VM_MAXCPU];
135 	uint64_t	eptp;
136 	struct vm	*vm;
137 	long		eptgen[MAXCPU];		/* cached pmap->pm_eptgen */
138 	struct vm_mtrr  mtrr[VM_MAXCPU];
139 };
140 CTASSERT((offsetof(struct vmx, vmcs) & PAGE_MASK) == 0);
141 CTASSERT((offsetof(struct vmx, msr_bitmap) & PAGE_MASK) == 0);
142 CTASSERT((offsetof(struct vmx, pir_desc[0]) & 63) == 0);
143 
144 #define	VMX_GUEST_VMEXIT	0
145 #define	VMX_VMRESUME_ERROR	1
146 #define	VMX_VMLAUNCH_ERROR	2
147 int	vmx_enter_guest(struct vmxctx *ctx, struct vmx *vmx, int launched);
148 void	vmx_call_isr(uintptr_t entry);
149 
150 u_long	vmx_fix_cr0(u_long cr0);
151 u_long	vmx_fix_cr4(u_long cr4);
152 
153 int	vmx_set_tsc_offset(struct vmx *vmx, int vcpu, uint64_t offset);
154 
155 extern char	vmx_exit_guest[];
156 extern char	vmx_exit_guest_flush_rsb[];
157 
158 static inline bool
159 vmx_have_msr_tsc_aux(struct vmx *vmx)
160 {
161 	int rdpid_rdtscp_bits = ((1 << VM_CAP_RDPID) | (1 << VM_CAP_RDTSCP));
162 
163 	/*
164 	 * Since the values of these bits are uniform across all vCPUs
165 	 * (see discussion in vmx_modinit() and initialization of these bits
166 	 * in vmx_init()), just always use vCPU-zero's capability set and
167 	 * remove the need to require a vcpuid argument.
168 	 */
169 	return ((vmx->cap[0].set & rdpid_rdtscp_bits) != 0);
170 }
171 
172 #endif
173