nvmm_x86_svm.c revision 1.6.2.4 1 1.6.2.4 pgoyette /* $NetBSD: nvmm_x86_svm.c,v 1.6.2.4 2019/01/18 08:50:26 pgoyette Exp $ */
2 1.6.2.2 pgoyette
3 1.6.2.2 pgoyette /*
4 1.6.2.2 pgoyette * Copyright (c) 2018 The NetBSD Foundation, Inc.
5 1.6.2.2 pgoyette * All rights reserved.
6 1.6.2.2 pgoyette *
7 1.6.2.2 pgoyette * This code is derived from software contributed to The NetBSD Foundation
8 1.6.2.2 pgoyette * by Maxime Villard.
9 1.6.2.2 pgoyette *
10 1.6.2.2 pgoyette * Redistribution and use in source and binary forms, with or without
11 1.6.2.2 pgoyette * modification, are permitted provided that the following conditions
12 1.6.2.2 pgoyette * are met:
13 1.6.2.2 pgoyette * 1. Redistributions of source code must retain the above copyright
14 1.6.2.2 pgoyette * notice, this list of conditions and the following disclaimer.
15 1.6.2.2 pgoyette * 2. Redistributions in binary form must reproduce the above copyright
16 1.6.2.2 pgoyette * notice, this list of conditions and the following disclaimer in the
17 1.6.2.2 pgoyette * documentation and/or other materials provided with the distribution.
18 1.6.2.2 pgoyette *
19 1.6.2.2 pgoyette * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.6.2.2 pgoyette * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.6.2.2 pgoyette * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.6.2.2 pgoyette * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.6.2.2 pgoyette * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.6.2.2 pgoyette * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.6.2.2 pgoyette * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.6.2.2 pgoyette * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.6.2.2 pgoyette * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.6.2.2 pgoyette * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.6.2.2 pgoyette * POSSIBILITY OF SUCH DAMAGE.
30 1.6.2.2 pgoyette */
31 1.6.2.2 pgoyette
32 1.6.2.2 pgoyette #include <sys/cdefs.h>
33 1.6.2.4 pgoyette __KERNEL_RCSID(0, "$NetBSD: nvmm_x86_svm.c,v 1.6.2.4 2019/01/18 08:50:26 pgoyette Exp $");
34 1.6.2.2 pgoyette
35 1.6.2.2 pgoyette #include <sys/param.h>
36 1.6.2.2 pgoyette #include <sys/systm.h>
37 1.6.2.2 pgoyette #include <sys/kernel.h>
38 1.6.2.2 pgoyette #include <sys/kmem.h>
39 1.6.2.2 pgoyette #include <sys/cpu.h>
40 1.6.2.2 pgoyette #include <sys/xcall.h>
41 1.6.2.2 pgoyette
42 1.6.2.2 pgoyette #include <uvm/uvm.h>
43 1.6.2.2 pgoyette #include <uvm/uvm_page.h>
44 1.6.2.2 pgoyette
45 1.6.2.2 pgoyette #include <x86/cputypes.h>
46 1.6.2.2 pgoyette #include <x86/specialreg.h>
47 1.6.2.2 pgoyette #include <x86/pmap.h>
48 1.6.2.2 pgoyette #include <x86/dbregs.h>
49 1.6.2.2 pgoyette #include <machine/cpuvar.h>
50 1.6.2.2 pgoyette
51 1.6.2.2 pgoyette #include <dev/nvmm/nvmm.h>
52 1.6.2.2 pgoyette #include <dev/nvmm/nvmm_internal.h>
53 1.6.2.2 pgoyette #include <dev/nvmm/x86/nvmm_x86.h>
54 1.6.2.2 pgoyette
55 1.6.2.2 pgoyette int svm_vmrun(paddr_t, uint64_t *);
56 1.6.2.2 pgoyette
57 1.6.2.2 pgoyette #define MSR_VM_HSAVE_PA 0xC0010117
58 1.6.2.2 pgoyette
59 1.6.2.2 pgoyette /* -------------------------------------------------------------------------- */
60 1.6.2.2 pgoyette
61 1.6.2.2 pgoyette #define VMCB_EXITCODE_CR0_READ 0x0000
62 1.6.2.2 pgoyette #define VMCB_EXITCODE_CR1_READ 0x0001
63 1.6.2.2 pgoyette #define VMCB_EXITCODE_CR2_READ 0x0002
64 1.6.2.2 pgoyette #define VMCB_EXITCODE_CR3_READ 0x0003
65 1.6.2.2 pgoyette #define VMCB_EXITCODE_CR4_READ 0x0004
66 1.6.2.2 pgoyette #define VMCB_EXITCODE_CR5_READ 0x0005
67 1.6.2.2 pgoyette #define VMCB_EXITCODE_CR6_READ 0x0006
68 1.6.2.2 pgoyette #define VMCB_EXITCODE_CR7_READ 0x0007
69 1.6.2.2 pgoyette #define VMCB_EXITCODE_CR8_READ 0x0008
70 1.6.2.2 pgoyette #define VMCB_EXITCODE_CR9_READ 0x0009
71 1.6.2.2 pgoyette #define VMCB_EXITCODE_CR10_READ 0x000A
72 1.6.2.2 pgoyette #define VMCB_EXITCODE_CR11_READ 0x000B
73 1.6.2.2 pgoyette #define VMCB_EXITCODE_CR12_READ 0x000C
74 1.6.2.2 pgoyette #define VMCB_EXITCODE_CR13_READ 0x000D
75 1.6.2.2 pgoyette #define VMCB_EXITCODE_CR14_READ 0x000E
76 1.6.2.2 pgoyette #define VMCB_EXITCODE_CR15_READ 0x000F
77 1.6.2.2 pgoyette #define VMCB_EXITCODE_CR0_WRITE 0x0010
78 1.6.2.2 pgoyette #define VMCB_EXITCODE_CR1_WRITE 0x0011
79 1.6.2.2 pgoyette #define VMCB_EXITCODE_CR2_WRITE 0x0012
80 1.6.2.2 pgoyette #define VMCB_EXITCODE_CR3_WRITE 0x0013
81 1.6.2.2 pgoyette #define VMCB_EXITCODE_CR4_WRITE 0x0014
82 1.6.2.2 pgoyette #define VMCB_EXITCODE_CR5_WRITE 0x0015
83 1.6.2.2 pgoyette #define VMCB_EXITCODE_CR6_WRITE 0x0016
84 1.6.2.2 pgoyette #define VMCB_EXITCODE_CR7_WRITE 0x0017
85 1.6.2.2 pgoyette #define VMCB_EXITCODE_CR8_WRITE 0x0018
86 1.6.2.2 pgoyette #define VMCB_EXITCODE_CR9_WRITE 0x0019
87 1.6.2.2 pgoyette #define VMCB_EXITCODE_CR10_WRITE 0x001A
88 1.6.2.2 pgoyette #define VMCB_EXITCODE_CR11_WRITE 0x001B
89 1.6.2.2 pgoyette #define VMCB_EXITCODE_CR12_WRITE 0x001C
90 1.6.2.2 pgoyette #define VMCB_EXITCODE_CR13_WRITE 0x001D
91 1.6.2.2 pgoyette #define VMCB_EXITCODE_CR14_WRITE 0x001E
92 1.6.2.2 pgoyette #define VMCB_EXITCODE_CR15_WRITE 0x001F
93 1.6.2.2 pgoyette #define VMCB_EXITCODE_DR0_READ 0x0020
94 1.6.2.2 pgoyette #define VMCB_EXITCODE_DR1_READ 0x0021
95 1.6.2.2 pgoyette #define VMCB_EXITCODE_DR2_READ 0x0022
96 1.6.2.2 pgoyette #define VMCB_EXITCODE_DR3_READ 0x0023
97 1.6.2.2 pgoyette #define VMCB_EXITCODE_DR4_READ 0x0024
98 1.6.2.2 pgoyette #define VMCB_EXITCODE_DR5_READ 0x0025
99 1.6.2.2 pgoyette #define VMCB_EXITCODE_DR6_READ 0x0026
100 1.6.2.2 pgoyette #define VMCB_EXITCODE_DR7_READ 0x0027
101 1.6.2.2 pgoyette #define VMCB_EXITCODE_DR8_READ 0x0028
102 1.6.2.2 pgoyette #define VMCB_EXITCODE_DR9_READ 0x0029
103 1.6.2.2 pgoyette #define VMCB_EXITCODE_DR10_READ 0x002A
104 1.6.2.2 pgoyette #define VMCB_EXITCODE_DR11_READ 0x002B
105 1.6.2.2 pgoyette #define VMCB_EXITCODE_DR12_READ 0x002C
106 1.6.2.2 pgoyette #define VMCB_EXITCODE_DR13_READ 0x002D
107 1.6.2.2 pgoyette #define VMCB_EXITCODE_DR14_READ 0x002E
108 1.6.2.2 pgoyette #define VMCB_EXITCODE_DR15_READ 0x002F
109 1.6.2.2 pgoyette #define VMCB_EXITCODE_DR0_WRITE 0x0030
110 1.6.2.2 pgoyette #define VMCB_EXITCODE_DR1_WRITE 0x0031
111 1.6.2.2 pgoyette #define VMCB_EXITCODE_DR2_WRITE 0x0032
112 1.6.2.2 pgoyette #define VMCB_EXITCODE_DR3_WRITE 0x0033
113 1.6.2.2 pgoyette #define VMCB_EXITCODE_DR4_WRITE 0x0034
114 1.6.2.2 pgoyette #define VMCB_EXITCODE_DR5_WRITE 0x0035
115 1.6.2.2 pgoyette #define VMCB_EXITCODE_DR6_WRITE 0x0036
116 1.6.2.2 pgoyette #define VMCB_EXITCODE_DR7_WRITE 0x0037
117 1.6.2.2 pgoyette #define VMCB_EXITCODE_DR8_WRITE 0x0038
118 1.6.2.2 pgoyette #define VMCB_EXITCODE_DR9_WRITE 0x0039
119 1.6.2.2 pgoyette #define VMCB_EXITCODE_DR10_WRITE 0x003A
120 1.6.2.2 pgoyette #define VMCB_EXITCODE_DR11_WRITE 0x003B
121 1.6.2.2 pgoyette #define VMCB_EXITCODE_DR12_WRITE 0x003C
122 1.6.2.2 pgoyette #define VMCB_EXITCODE_DR13_WRITE 0x003D
123 1.6.2.2 pgoyette #define VMCB_EXITCODE_DR14_WRITE 0x003E
124 1.6.2.2 pgoyette #define VMCB_EXITCODE_DR15_WRITE 0x003F
125 1.6.2.2 pgoyette #define VMCB_EXITCODE_EXCP0 0x0040
126 1.6.2.2 pgoyette #define VMCB_EXITCODE_EXCP1 0x0041
127 1.6.2.2 pgoyette #define VMCB_EXITCODE_EXCP2 0x0042
128 1.6.2.2 pgoyette #define VMCB_EXITCODE_EXCP3 0x0043
129 1.6.2.2 pgoyette #define VMCB_EXITCODE_EXCP4 0x0044
130 1.6.2.2 pgoyette #define VMCB_EXITCODE_EXCP5 0x0045
131 1.6.2.2 pgoyette #define VMCB_EXITCODE_EXCP6 0x0046
132 1.6.2.2 pgoyette #define VMCB_EXITCODE_EXCP7 0x0047
133 1.6.2.2 pgoyette #define VMCB_EXITCODE_EXCP8 0x0048
134 1.6.2.2 pgoyette #define VMCB_EXITCODE_EXCP9 0x0049
135 1.6.2.2 pgoyette #define VMCB_EXITCODE_EXCP10 0x004A
136 1.6.2.2 pgoyette #define VMCB_EXITCODE_EXCP11 0x004B
137 1.6.2.2 pgoyette #define VMCB_EXITCODE_EXCP12 0x004C
138 1.6.2.2 pgoyette #define VMCB_EXITCODE_EXCP13 0x004D
139 1.6.2.2 pgoyette #define VMCB_EXITCODE_EXCP14 0x004E
140 1.6.2.2 pgoyette #define VMCB_EXITCODE_EXCP15 0x004F
141 1.6.2.2 pgoyette #define VMCB_EXITCODE_EXCP16 0x0050
142 1.6.2.2 pgoyette #define VMCB_EXITCODE_EXCP17 0x0051
143 1.6.2.2 pgoyette #define VMCB_EXITCODE_EXCP18 0x0052
144 1.6.2.2 pgoyette #define VMCB_EXITCODE_EXCP19 0x0053
145 1.6.2.2 pgoyette #define VMCB_EXITCODE_EXCP20 0x0054
146 1.6.2.2 pgoyette #define VMCB_EXITCODE_EXCP21 0x0055
147 1.6.2.2 pgoyette #define VMCB_EXITCODE_EXCP22 0x0056
148 1.6.2.2 pgoyette #define VMCB_EXITCODE_EXCP23 0x0057
149 1.6.2.2 pgoyette #define VMCB_EXITCODE_EXCP24 0x0058
150 1.6.2.2 pgoyette #define VMCB_EXITCODE_EXCP25 0x0059
151 1.6.2.2 pgoyette #define VMCB_EXITCODE_EXCP26 0x005A
152 1.6.2.2 pgoyette #define VMCB_EXITCODE_EXCP27 0x005B
153 1.6.2.2 pgoyette #define VMCB_EXITCODE_EXCP28 0x005C
154 1.6.2.2 pgoyette #define VMCB_EXITCODE_EXCP29 0x005D
155 1.6.2.2 pgoyette #define VMCB_EXITCODE_EXCP30 0x005E
156 1.6.2.2 pgoyette #define VMCB_EXITCODE_EXCP31 0x005F
157 1.6.2.2 pgoyette #define VMCB_EXITCODE_INTR 0x0060
158 1.6.2.2 pgoyette #define VMCB_EXITCODE_NMI 0x0061
159 1.6.2.2 pgoyette #define VMCB_EXITCODE_SMI 0x0062
160 1.6.2.2 pgoyette #define VMCB_EXITCODE_INIT 0x0063
161 1.6.2.2 pgoyette #define VMCB_EXITCODE_VINTR 0x0064
162 1.6.2.2 pgoyette #define VMCB_EXITCODE_CR0_SEL_WRITE 0x0065
163 1.6.2.2 pgoyette #define VMCB_EXITCODE_IDTR_READ 0x0066
164 1.6.2.2 pgoyette #define VMCB_EXITCODE_GDTR_READ 0x0067
165 1.6.2.2 pgoyette #define VMCB_EXITCODE_LDTR_READ 0x0068
166 1.6.2.2 pgoyette #define VMCB_EXITCODE_TR_READ 0x0069
167 1.6.2.2 pgoyette #define VMCB_EXITCODE_IDTR_WRITE 0x006A
168 1.6.2.2 pgoyette #define VMCB_EXITCODE_GDTR_WRITE 0x006B
169 1.6.2.2 pgoyette #define VMCB_EXITCODE_LDTR_WRITE 0x006C
170 1.6.2.2 pgoyette #define VMCB_EXITCODE_TR_WRITE 0x006D
171 1.6.2.2 pgoyette #define VMCB_EXITCODE_RDTSC 0x006E
172 1.6.2.2 pgoyette #define VMCB_EXITCODE_RDPMC 0x006F
173 1.6.2.2 pgoyette #define VMCB_EXITCODE_PUSHF 0x0070
174 1.6.2.2 pgoyette #define VMCB_EXITCODE_POPF 0x0071
175 1.6.2.2 pgoyette #define VMCB_EXITCODE_CPUID 0x0072
176 1.6.2.2 pgoyette #define VMCB_EXITCODE_RSM 0x0073
177 1.6.2.2 pgoyette #define VMCB_EXITCODE_IRET 0x0074
178 1.6.2.2 pgoyette #define VMCB_EXITCODE_SWINT 0x0075
179 1.6.2.2 pgoyette #define VMCB_EXITCODE_INVD 0x0076
180 1.6.2.2 pgoyette #define VMCB_EXITCODE_PAUSE 0x0077
181 1.6.2.2 pgoyette #define VMCB_EXITCODE_HLT 0x0078
182 1.6.2.2 pgoyette #define VMCB_EXITCODE_INVLPG 0x0079
183 1.6.2.2 pgoyette #define VMCB_EXITCODE_INVLPGA 0x007A
184 1.6.2.2 pgoyette #define VMCB_EXITCODE_IOIO 0x007B
185 1.6.2.2 pgoyette #define VMCB_EXITCODE_MSR 0x007C
186 1.6.2.2 pgoyette #define VMCB_EXITCODE_TASK_SWITCH 0x007D
187 1.6.2.2 pgoyette #define VMCB_EXITCODE_FERR_FREEZE 0x007E
188 1.6.2.2 pgoyette #define VMCB_EXITCODE_SHUTDOWN 0x007F
189 1.6.2.2 pgoyette #define VMCB_EXITCODE_VMRUN 0x0080
190 1.6.2.2 pgoyette #define VMCB_EXITCODE_VMMCALL 0x0081
191 1.6.2.2 pgoyette #define VMCB_EXITCODE_VMLOAD 0x0082
192 1.6.2.2 pgoyette #define VMCB_EXITCODE_VMSAVE 0x0083
193 1.6.2.2 pgoyette #define VMCB_EXITCODE_STGI 0x0084
194 1.6.2.2 pgoyette #define VMCB_EXITCODE_CLGI 0x0085
195 1.6.2.2 pgoyette #define VMCB_EXITCODE_SKINIT 0x0086
196 1.6.2.2 pgoyette #define VMCB_EXITCODE_RDTSCP 0x0087
197 1.6.2.2 pgoyette #define VMCB_EXITCODE_ICEBP 0x0088
198 1.6.2.2 pgoyette #define VMCB_EXITCODE_WBINVD 0x0089
199 1.6.2.2 pgoyette #define VMCB_EXITCODE_MONITOR 0x008A
200 1.6.2.2 pgoyette #define VMCB_EXITCODE_MWAIT 0x008B
201 1.6.2.2 pgoyette #define VMCB_EXITCODE_MWAIT_CONDITIONAL 0x008C
202 1.6.2.2 pgoyette #define VMCB_EXITCODE_XSETBV 0x008D
203 1.6.2.2 pgoyette #define VMCB_EXITCODE_EFER_WRITE_TRAP 0x008F
204 1.6.2.2 pgoyette #define VMCB_EXITCODE_CR0_WRITE_TRAP 0x0090
205 1.6.2.2 pgoyette #define VMCB_EXITCODE_CR1_WRITE_TRAP 0x0091
206 1.6.2.2 pgoyette #define VMCB_EXITCODE_CR2_WRITE_TRAP 0x0092
207 1.6.2.2 pgoyette #define VMCB_EXITCODE_CR3_WRITE_TRAP 0x0093
208 1.6.2.2 pgoyette #define VMCB_EXITCODE_CR4_WRITE_TRAP 0x0094
209 1.6.2.2 pgoyette #define VMCB_EXITCODE_CR5_WRITE_TRAP 0x0095
210 1.6.2.2 pgoyette #define VMCB_EXITCODE_CR6_WRITE_TRAP 0x0096
211 1.6.2.2 pgoyette #define VMCB_EXITCODE_CR7_WRITE_TRAP 0x0097
212 1.6.2.2 pgoyette #define VMCB_EXITCODE_CR8_WRITE_TRAP 0x0098
213 1.6.2.2 pgoyette #define VMCB_EXITCODE_CR9_WRITE_TRAP 0x0099
214 1.6.2.2 pgoyette #define VMCB_EXITCODE_CR10_WRITE_TRAP 0x009A
215 1.6.2.2 pgoyette #define VMCB_EXITCODE_CR11_WRITE_TRAP 0x009B
216 1.6.2.2 pgoyette #define VMCB_EXITCODE_CR12_WRITE_TRAP 0x009C
217 1.6.2.2 pgoyette #define VMCB_EXITCODE_CR13_WRITE_TRAP 0x009D
218 1.6.2.2 pgoyette #define VMCB_EXITCODE_CR14_WRITE_TRAP 0x009E
219 1.6.2.2 pgoyette #define VMCB_EXITCODE_CR15_WRITE_TRAP 0x009F
220 1.6.2.2 pgoyette #define VMCB_EXITCODE_NPF 0x0400
221 1.6.2.2 pgoyette #define VMCB_EXITCODE_AVIC_INCOMP_IPI 0x0401
222 1.6.2.2 pgoyette #define VMCB_EXITCODE_AVIC_NOACCEL 0x0402
223 1.6.2.2 pgoyette #define VMCB_EXITCODE_VMGEXIT 0x0403
224 1.6.2.2 pgoyette #define VMCB_EXITCODE_INVALID -1
225 1.6.2.2 pgoyette
226 1.6.2.2 pgoyette /* -------------------------------------------------------------------------- */
227 1.6.2.2 pgoyette
228 1.6.2.2 pgoyette struct vmcb_ctrl {
229 1.6.2.2 pgoyette uint32_t intercept_cr;
230 1.6.2.2 pgoyette #define VMCB_CTRL_INTERCEPT_RCR(x) __BIT( 0 + x)
231 1.6.2.2 pgoyette #define VMCB_CTRL_INTERCEPT_WCR(x) __BIT(16 + x)
232 1.6.2.2 pgoyette
233 1.6.2.2 pgoyette uint32_t intercept_dr;
234 1.6.2.2 pgoyette #define VMCB_CTRL_INTERCEPT_RDR(x) __BIT( 0 + x)
235 1.6.2.2 pgoyette #define VMCB_CTRL_INTERCEPT_WDR(x) __BIT(16 + x)
236 1.6.2.2 pgoyette
237 1.6.2.2 pgoyette uint32_t intercept_vec;
238 1.6.2.2 pgoyette #define VMCB_CTRL_INTERCEPT_VEC(x) __BIT(x)
239 1.6.2.2 pgoyette
240 1.6.2.2 pgoyette uint32_t intercept_misc1;
241 1.6.2.2 pgoyette #define VMCB_CTRL_INTERCEPT_INTR __BIT(0)
242 1.6.2.2 pgoyette #define VMCB_CTRL_INTERCEPT_NMI __BIT(1)
243 1.6.2.2 pgoyette #define VMCB_CTRL_INTERCEPT_SMI __BIT(2)
244 1.6.2.2 pgoyette #define VMCB_CTRL_INTERCEPT_INIT __BIT(3)
245 1.6.2.2 pgoyette #define VMCB_CTRL_INTERCEPT_VINTR __BIT(4)
246 1.6.2.2 pgoyette #define VMCB_CTRL_INTERCEPT_CR0_SPEC __BIT(5)
247 1.6.2.2 pgoyette #define VMCB_CTRL_INTERCEPT_RIDTR __BIT(6)
248 1.6.2.2 pgoyette #define VMCB_CTRL_INTERCEPT_RGDTR __BIT(7)
249 1.6.2.2 pgoyette #define VMCB_CTRL_INTERCEPT_RLDTR __BIT(8)
250 1.6.2.2 pgoyette #define VMCB_CTRL_INTERCEPT_RTR __BIT(9)
251 1.6.2.2 pgoyette #define VMCB_CTRL_INTERCEPT_WIDTR __BIT(10)
252 1.6.2.2 pgoyette #define VMCB_CTRL_INTERCEPT_WGDTR __BIT(11)
253 1.6.2.2 pgoyette #define VMCB_CTRL_INTERCEPT_WLDTR __BIT(12)
254 1.6.2.2 pgoyette #define VMCB_CTRL_INTERCEPT_WTR __BIT(13)
255 1.6.2.2 pgoyette #define VMCB_CTRL_INTERCEPT_RDTSC __BIT(14)
256 1.6.2.2 pgoyette #define VMCB_CTRL_INTERCEPT_RDPMC __BIT(15)
257 1.6.2.2 pgoyette #define VMCB_CTRL_INTERCEPT_PUSHF __BIT(16)
258 1.6.2.2 pgoyette #define VMCB_CTRL_INTERCEPT_POPF __BIT(17)
259 1.6.2.2 pgoyette #define VMCB_CTRL_INTERCEPT_CPUID __BIT(18)
260 1.6.2.2 pgoyette #define VMCB_CTRL_INTERCEPT_RSM __BIT(19)
261 1.6.2.2 pgoyette #define VMCB_CTRL_INTERCEPT_IRET __BIT(20)
262 1.6.2.2 pgoyette #define VMCB_CTRL_INTERCEPT_INTN __BIT(21)
263 1.6.2.2 pgoyette #define VMCB_CTRL_INTERCEPT_INVD __BIT(22)
264 1.6.2.2 pgoyette #define VMCB_CTRL_INTERCEPT_PAUSE __BIT(23)
265 1.6.2.2 pgoyette #define VMCB_CTRL_INTERCEPT_HLT __BIT(24)
266 1.6.2.2 pgoyette #define VMCB_CTRL_INTERCEPT_INVLPG __BIT(25)
267 1.6.2.2 pgoyette #define VMCB_CTRL_INTERCEPT_INVLPGA __BIT(26)
268 1.6.2.2 pgoyette #define VMCB_CTRL_INTERCEPT_IOIO_PROT __BIT(27)
269 1.6.2.2 pgoyette #define VMCB_CTRL_INTERCEPT_MSR_PROT __BIT(28)
270 1.6.2.2 pgoyette #define VMCB_CTRL_INTERCEPT_TASKSW __BIT(29)
271 1.6.2.2 pgoyette #define VMCB_CTRL_INTERCEPT_FERR_FREEZE __BIT(30)
272 1.6.2.2 pgoyette #define VMCB_CTRL_INTERCEPT_SHUTDOWN __BIT(31)
273 1.6.2.2 pgoyette
274 1.6.2.2 pgoyette uint32_t intercept_misc2;
275 1.6.2.2 pgoyette #define VMCB_CTRL_INTERCEPT_VMRUN __BIT(0)
276 1.6.2.2 pgoyette #define VMCB_CTRL_INTERCEPT_VMMCALL __BIT(1)
277 1.6.2.2 pgoyette #define VMCB_CTRL_INTERCEPT_VMLOAD __BIT(2)
278 1.6.2.2 pgoyette #define VMCB_CTRL_INTERCEPT_VMSAVE __BIT(3)
279 1.6.2.2 pgoyette #define VMCB_CTRL_INTERCEPT_STGI __BIT(4)
280 1.6.2.2 pgoyette #define VMCB_CTRL_INTERCEPT_CLGI __BIT(5)
281 1.6.2.2 pgoyette #define VMCB_CTRL_INTERCEPT_SKINIT __BIT(6)
282 1.6.2.2 pgoyette #define VMCB_CTRL_INTERCEPT_RDTSCP __BIT(7)
283 1.6.2.2 pgoyette #define VMCB_CTRL_INTERCEPT_ICEBP __BIT(8)
284 1.6.2.2 pgoyette #define VMCB_CTRL_INTERCEPT_WBINVD __BIT(9)
285 1.6.2.2 pgoyette #define VMCB_CTRL_INTERCEPT_MONITOR __BIT(10)
286 1.6.2.2 pgoyette #define VMCB_CTRL_INTERCEPT_MWAIT __BIT(12)
287 1.6.2.2 pgoyette #define VMCB_CTRL_INTERCEPT_XSETBV __BIT(13)
288 1.6.2.2 pgoyette #define VMCB_CTRL_INTERCEPT_EFER_SPEC __BIT(15)
289 1.6.2.2 pgoyette #define VMCB_CTRL_INTERCEPT_WCR_SPEC(x) __BIT(16 + x)
290 1.6.2.2 pgoyette
291 1.6.2.2 pgoyette uint8_t rsvd1[40];
292 1.6.2.2 pgoyette uint16_t pause_filt_thresh;
293 1.6.2.2 pgoyette uint16_t pause_filt_cnt;
294 1.6.2.2 pgoyette uint64_t iopm_base_pa;
295 1.6.2.2 pgoyette uint64_t msrpm_base_pa;
296 1.6.2.2 pgoyette uint64_t tsc_offset;
297 1.6.2.2 pgoyette uint32_t guest_asid;
298 1.6.2.2 pgoyette
299 1.6.2.2 pgoyette uint32_t tlb_ctrl;
300 1.6.2.2 pgoyette #define VMCB_CTRL_TLB_CTRL_FLUSH_ALL 0x01
301 1.6.2.2 pgoyette #define VMCB_CTRL_TLB_CTRL_FLUSH_GUEST 0x03
302 1.6.2.2 pgoyette #define VMCB_CTRL_TLB_CTRL_FLUSH_GUEST_NONGLOBAL 0x07
303 1.6.2.2 pgoyette
304 1.6.2.2 pgoyette uint64_t v;
305 1.6.2.2 pgoyette #define VMCB_CTRL_V_TPR __BITS(7,0)
306 1.6.2.2 pgoyette #define VMCB_CTRL_V_IRQ __BIT(8)
307 1.6.2.2 pgoyette #define VMCB_CTRL_V_VGIF __BIT(9)
308 1.6.2.2 pgoyette #define VMCB_CTRL_V_INTR_PRIO __BITS(19,16)
309 1.6.2.2 pgoyette #define VMCB_CTRL_V_IGN_TPR __BIT(20)
310 1.6.2.2 pgoyette #define VMCB_CTRL_V_INTR_MASKING __BIT(24)
311 1.6.2.2 pgoyette #define VMCB_CTRL_V_GUEST_VGIF __BIT(25)
312 1.6.2.2 pgoyette #define VMCB_CTRL_V_AVIC_EN __BIT(31)
313 1.6.2.2 pgoyette #define VMCB_CTRL_V_INTR_VECTOR __BITS(39,32)
314 1.6.2.2 pgoyette
315 1.6.2.2 pgoyette uint64_t intr;
316 1.6.2.2 pgoyette #define VMCB_CTRL_INTR_SHADOW __BIT(0)
317 1.6.2.2 pgoyette
318 1.6.2.2 pgoyette uint64_t exitcode;
319 1.6.2.2 pgoyette uint64_t exitinfo1;
320 1.6.2.2 pgoyette uint64_t exitinfo2;
321 1.6.2.2 pgoyette
322 1.6.2.2 pgoyette uint64_t exitintinfo;
323 1.6.2.2 pgoyette #define VMCB_CTRL_EXITINTINFO_VECTOR __BITS(7,0)
324 1.6.2.2 pgoyette #define VMCB_CTRL_EXITINTINFO_TYPE __BITS(10,8)
325 1.6.2.2 pgoyette #define VMCB_CTRL_EXITINTINFO_EV __BIT(11)
326 1.6.2.2 pgoyette #define VMCB_CTRL_EXITINTINFO_V __BIT(31)
327 1.6.2.2 pgoyette #define VMCB_CTRL_EXITINTINFO_ERRORCODE __BITS(63,32)
328 1.6.2.2 pgoyette
329 1.6.2.2 pgoyette uint64_t enable1;
330 1.6.2.2 pgoyette #define VMCB_CTRL_ENABLE_NP __BIT(0)
331 1.6.2.2 pgoyette #define VMCB_CTRL_ENABLE_SEV __BIT(1)
332 1.6.2.2 pgoyette #define VMCB_CTRL_ENABLE_ES_SEV __BIT(2)
333 1.6.2.2 pgoyette
334 1.6.2.2 pgoyette uint64_t avic;
335 1.6.2.2 pgoyette #define VMCB_CTRL_AVIC_APIC_BAR __BITS(51,0)
336 1.6.2.2 pgoyette
337 1.6.2.2 pgoyette uint64_t ghcb;
338 1.6.2.2 pgoyette
339 1.6.2.2 pgoyette uint64_t eventinj;
340 1.6.2.2 pgoyette #define VMCB_CTRL_EVENTINJ_VECTOR __BITS(7,0)
341 1.6.2.2 pgoyette #define VMCB_CTRL_EVENTINJ_TYPE __BITS(10,8)
342 1.6.2.2 pgoyette #define VMCB_CTRL_EVENTINJ_EV __BIT(11)
343 1.6.2.2 pgoyette #define VMCB_CTRL_EVENTINJ_V __BIT(31)
344 1.6.2.2 pgoyette #define VMCB_CTRL_EVENTINJ_ERRORCODE __BITS(63,32)
345 1.6.2.2 pgoyette
346 1.6.2.2 pgoyette uint64_t n_cr3;
347 1.6.2.2 pgoyette
348 1.6.2.2 pgoyette uint64_t enable2;
349 1.6.2.2 pgoyette #define VMCB_CTRL_ENABLE_LBR __BIT(0)
350 1.6.2.2 pgoyette #define VMCB_CTRL_ENABLE_VVMSAVE __BIT(1)
351 1.6.2.2 pgoyette
352 1.6.2.2 pgoyette uint32_t vmcb_clean;
353 1.6.2.2 pgoyette #define VMCB_CTRL_VMCB_CLEAN_I __BIT(0)
354 1.6.2.2 pgoyette #define VMCB_CTRL_VMCB_CLEAN_IOPM __BIT(1)
355 1.6.2.2 pgoyette #define VMCB_CTRL_VMCB_CLEAN_ASID __BIT(2)
356 1.6.2.2 pgoyette #define VMCB_CTRL_VMCB_CLEAN_TPR __BIT(3)
357 1.6.2.2 pgoyette #define VMCB_CTRL_VMCB_CLEAN_NP __BIT(4)
358 1.6.2.2 pgoyette #define VMCB_CTRL_VMCB_CLEAN_CR __BIT(5)
359 1.6.2.2 pgoyette #define VMCB_CTRL_VMCB_CLEAN_DR __BIT(6)
360 1.6.2.2 pgoyette #define VMCB_CTRL_VMCB_CLEAN_DT __BIT(7)
361 1.6.2.2 pgoyette #define VMCB_CTRL_VMCB_CLEAN_SEG __BIT(8)
362 1.6.2.2 pgoyette #define VMCB_CTRL_VMCB_CLEAN_CR2 __BIT(9)
363 1.6.2.2 pgoyette #define VMCB_CTRL_VMCB_CLEAN_LBR __BIT(10)
364 1.6.2.2 pgoyette #define VMCB_CTRL_VMCB_CLEAN_AVIC __BIT(11)
365 1.6.2.2 pgoyette
366 1.6.2.2 pgoyette uint32_t rsvd2;
367 1.6.2.2 pgoyette uint64_t nrip;
368 1.6.2.2 pgoyette uint8_t inst_len;
369 1.6.2.2 pgoyette uint8_t inst_bytes[15];
370 1.6.2.4 pgoyette uint64_t avic_abpp;
371 1.6.2.4 pgoyette uint64_t rsvd3;
372 1.6.2.4 pgoyette uint64_t avic_ltp;
373 1.6.2.4 pgoyette
374 1.6.2.4 pgoyette uint64_t avic_phys;
375 1.6.2.4 pgoyette #define VMCB_CTRL_AVIC_PHYS_TABLE_PTR __BITS(51,12)
376 1.6.2.4 pgoyette #define VMCB_CTRL_AVIC_PHYS_MAX_INDEX __BITS(7,0)
377 1.6.2.4 pgoyette
378 1.6.2.4 pgoyette uint64_t rsvd4;
379 1.6.2.4 pgoyette uint64_t vmcb_ptr;
380 1.6.2.4 pgoyette
381 1.6.2.4 pgoyette uint8_t pad[752];
382 1.6.2.2 pgoyette } __packed;
383 1.6.2.2 pgoyette
384 1.6.2.2 pgoyette CTASSERT(sizeof(struct vmcb_ctrl) == 1024);
385 1.6.2.2 pgoyette
386 1.6.2.2 pgoyette struct vmcb_segment {
387 1.6.2.2 pgoyette uint16_t selector;
388 1.6.2.2 pgoyette uint16_t attrib; /* hidden */
389 1.6.2.2 pgoyette uint32_t limit; /* hidden */
390 1.6.2.2 pgoyette uint64_t base; /* hidden */
391 1.6.2.2 pgoyette } __packed;
392 1.6.2.2 pgoyette
393 1.6.2.2 pgoyette CTASSERT(sizeof(struct vmcb_segment) == 16);
394 1.6.2.2 pgoyette
395 1.6.2.2 pgoyette struct vmcb_state {
396 1.6.2.2 pgoyette struct vmcb_segment es;
397 1.6.2.2 pgoyette struct vmcb_segment cs;
398 1.6.2.2 pgoyette struct vmcb_segment ss;
399 1.6.2.2 pgoyette struct vmcb_segment ds;
400 1.6.2.2 pgoyette struct vmcb_segment fs;
401 1.6.2.2 pgoyette struct vmcb_segment gs;
402 1.6.2.2 pgoyette struct vmcb_segment gdt;
403 1.6.2.2 pgoyette struct vmcb_segment ldt;
404 1.6.2.2 pgoyette struct vmcb_segment idt;
405 1.6.2.2 pgoyette struct vmcb_segment tr;
406 1.6.2.2 pgoyette uint8_t rsvd1[43];
407 1.6.2.2 pgoyette uint8_t cpl;
408 1.6.2.2 pgoyette uint8_t rsvd2[4];
409 1.6.2.2 pgoyette uint64_t efer;
410 1.6.2.2 pgoyette uint8_t rsvd3[112];
411 1.6.2.2 pgoyette uint64_t cr4;
412 1.6.2.2 pgoyette uint64_t cr3;
413 1.6.2.2 pgoyette uint64_t cr0;
414 1.6.2.2 pgoyette uint64_t dr7;
415 1.6.2.2 pgoyette uint64_t dr6;
416 1.6.2.2 pgoyette uint64_t rflags;
417 1.6.2.2 pgoyette uint64_t rip;
418 1.6.2.2 pgoyette uint8_t rsvd4[88];
419 1.6.2.2 pgoyette uint64_t rsp;
420 1.6.2.2 pgoyette uint8_t rsvd5[24];
421 1.6.2.2 pgoyette uint64_t rax;
422 1.6.2.2 pgoyette uint64_t star;
423 1.6.2.2 pgoyette uint64_t lstar;
424 1.6.2.2 pgoyette uint64_t cstar;
425 1.6.2.2 pgoyette uint64_t sfmask;
426 1.6.2.2 pgoyette uint64_t kernelgsbase;
427 1.6.2.2 pgoyette uint64_t sysenter_cs;
428 1.6.2.2 pgoyette uint64_t sysenter_esp;
429 1.6.2.2 pgoyette uint64_t sysenter_eip;
430 1.6.2.2 pgoyette uint64_t cr2;
431 1.6.2.2 pgoyette uint8_t rsvd6[32];
432 1.6.2.2 pgoyette uint64_t g_pat;
433 1.6.2.2 pgoyette uint64_t dbgctl;
434 1.6.2.2 pgoyette uint64_t br_from;
435 1.6.2.2 pgoyette uint64_t br_to;
436 1.6.2.2 pgoyette uint64_t int_from;
437 1.6.2.2 pgoyette uint64_t int_to;
438 1.6.2.2 pgoyette uint8_t pad[2408];
439 1.6.2.2 pgoyette } __packed;
440 1.6.2.2 pgoyette
441 1.6.2.2 pgoyette CTASSERT(sizeof(struct vmcb_state) == 0xC00);
442 1.6.2.2 pgoyette
443 1.6.2.2 pgoyette struct vmcb {
444 1.6.2.2 pgoyette struct vmcb_ctrl ctrl;
445 1.6.2.2 pgoyette struct vmcb_state state;
446 1.6.2.2 pgoyette } __packed;
447 1.6.2.2 pgoyette
448 1.6.2.2 pgoyette CTASSERT(sizeof(struct vmcb) == PAGE_SIZE);
449 1.6.2.2 pgoyette CTASSERT(offsetof(struct vmcb, state) == 0x400);
450 1.6.2.2 pgoyette
451 1.6.2.2 pgoyette /* -------------------------------------------------------------------------- */
452 1.6.2.2 pgoyette
453 1.6.2.2 pgoyette struct svm_hsave {
454 1.6.2.2 pgoyette paddr_t pa;
455 1.6.2.2 pgoyette };
456 1.6.2.2 pgoyette
457 1.6.2.2 pgoyette static struct svm_hsave hsave[MAXCPUS];
458 1.6.2.2 pgoyette
459 1.6.2.2 pgoyette static uint8_t *svm_asidmap __read_mostly;
460 1.6.2.2 pgoyette static uint32_t svm_maxasid __read_mostly;
461 1.6.2.2 pgoyette static kmutex_t svm_asidlock __cacheline_aligned;
462 1.6.2.2 pgoyette
463 1.6.2.2 pgoyette static bool svm_decode_assist __read_mostly;
464 1.6.2.2 pgoyette static uint32_t svm_ctrl_tlb_flush __read_mostly;
465 1.6.2.2 pgoyette
466 1.6.2.2 pgoyette #define SVM_XCR0_MASK_DEFAULT (XCR0_X87|XCR0_SSE)
467 1.6.2.2 pgoyette static uint64_t svm_xcr0_mask __read_mostly;
468 1.6.2.2 pgoyette
469 1.6.2.2 pgoyette #define SVM_NCPUIDS 32
470 1.6.2.2 pgoyette
471 1.6.2.2 pgoyette #define VMCB_NPAGES 1
472 1.6.2.2 pgoyette
473 1.6.2.2 pgoyette #define MSRBM_NPAGES 2
474 1.6.2.2 pgoyette #define MSRBM_SIZE (MSRBM_NPAGES * PAGE_SIZE)
475 1.6.2.2 pgoyette
476 1.6.2.2 pgoyette #define IOBM_NPAGES 3
477 1.6.2.2 pgoyette #define IOBM_SIZE (IOBM_NPAGES * PAGE_SIZE)
478 1.6.2.2 pgoyette
479 1.6.2.2 pgoyette /* Does not include EFER_LMSLE. */
480 1.6.2.2 pgoyette #define EFER_VALID \
481 1.6.2.2 pgoyette (EFER_SCE|EFER_LME|EFER_LMA|EFER_NXE|EFER_SVME|EFER_FFXSR|EFER_TCE)
482 1.6.2.2 pgoyette
483 1.6.2.2 pgoyette #define EFER_TLB_FLUSH \
484 1.6.2.2 pgoyette (EFER_NXE|EFER_LMA|EFER_LME)
485 1.6.2.2 pgoyette #define CR0_TLB_FLUSH \
486 1.6.2.2 pgoyette (CR0_PG|CR0_WP|CR0_CD|CR0_NW)
487 1.6.2.2 pgoyette #define CR4_TLB_FLUSH \
488 1.6.2.2 pgoyette (CR4_PGE|CR4_PAE|CR4_PSE)
489 1.6.2.2 pgoyette
490 1.6.2.2 pgoyette /* -------------------------------------------------------------------------- */
491 1.6.2.2 pgoyette
492 1.6.2.2 pgoyette struct svm_machdata {
493 1.6.2.2 pgoyette bool cpuidpresent[SVM_NCPUIDS];
494 1.6.2.2 pgoyette struct nvmm_x86_conf_cpuid cpuid[SVM_NCPUIDS];
495 1.6.2.2 pgoyette };
496 1.6.2.2 pgoyette
497 1.6.2.2 pgoyette static const size_t svm_conf_sizes[NVMM_X86_NCONF] = {
498 1.6.2.2 pgoyette [NVMM_X86_CONF_CPUID] = sizeof(struct nvmm_x86_conf_cpuid)
499 1.6.2.2 pgoyette };
500 1.6.2.2 pgoyette
501 1.6.2.2 pgoyette struct svm_cpudata {
502 1.6.2.2 pgoyette /* General */
503 1.6.2.2 pgoyette bool shared_asid;
504 1.6.2.2 pgoyette bool tlb_want_flush;
505 1.6.2.2 pgoyette
506 1.6.2.2 pgoyette /* VMCB */
507 1.6.2.2 pgoyette struct vmcb *vmcb;
508 1.6.2.2 pgoyette paddr_t vmcb_pa;
509 1.6.2.2 pgoyette
510 1.6.2.2 pgoyette /* I/O bitmap */
511 1.6.2.2 pgoyette uint8_t *iobm;
512 1.6.2.2 pgoyette paddr_t iobm_pa;
513 1.6.2.2 pgoyette
514 1.6.2.2 pgoyette /* MSR bitmap */
515 1.6.2.2 pgoyette uint8_t *msrbm;
516 1.6.2.2 pgoyette paddr_t msrbm_pa;
517 1.6.2.2 pgoyette
518 1.6.2.2 pgoyette /* Host state */
519 1.6.2.4 pgoyette uint64_t hxcr0;
520 1.6.2.2 pgoyette uint64_t star;
521 1.6.2.2 pgoyette uint64_t lstar;
522 1.6.2.2 pgoyette uint64_t cstar;
523 1.6.2.2 pgoyette uint64_t sfmask;
524 1.6.2.4 pgoyette uint64_t fsbase;
525 1.6.2.4 pgoyette uint64_t kernelgsbase;
526 1.6.2.2 pgoyette bool ts_set;
527 1.6.2.2 pgoyette struct xsave_header hfpu __aligned(16);
528 1.6.2.2 pgoyette
529 1.6.2.4 pgoyette /* Event state */
530 1.6.2.4 pgoyette bool int_window_exit;
531 1.6.2.4 pgoyette bool nmi_window_exit;
532 1.6.2.4 pgoyette
533 1.6.2.2 pgoyette /* Guest state */
534 1.6.2.4 pgoyette uint64_t gxcr0;
535 1.6.2.4 pgoyette uint64_t gprs[NVMM_X64_NGPR];
536 1.6.2.4 pgoyette uint64_t drs[NVMM_X64_NDR];
537 1.6.2.2 pgoyette uint64_t tsc_offset;
538 1.6.2.2 pgoyette struct xsave_header gfpu __aligned(16);
539 1.6.2.2 pgoyette };
540 1.6.2.2 pgoyette
541 1.6.2.4 pgoyette static void
542 1.6.2.4 pgoyette svm_vmcb_cache_default(struct vmcb *vmcb)
543 1.6.2.4 pgoyette {
544 1.6.2.4 pgoyette vmcb->ctrl.vmcb_clean =
545 1.6.2.4 pgoyette VMCB_CTRL_VMCB_CLEAN_I |
546 1.6.2.4 pgoyette VMCB_CTRL_VMCB_CLEAN_IOPM |
547 1.6.2.4 pgoyette VMCB_CTRL_VMCB_CLEAN_ASID |
548 1.6.2.4 pgoyette VMCB_CTRL_VMCB_CLEAN_TPR |
549 1.6.2.4 pgoyette VMCB_CTRL_VMCB_CLEAN_NP |
550 1.6.2.4 pgoyette VMCB_CTRL_VMCB_CLEAN_CR |
551 1.6.2.4 pgoyette VMCB_CTRL_VMCB_CLEAN_DR |
552 1.6.2.4 pgoyette VMCB_CTRL_VMCB_CLEAN_DT |
553 1.6.2.4 pgoyette VMCB_CTRL_VMCB_CLEAN_SEG |
554 1.6.2.4 pgoyette VMCB_CTRL_VMCB_CLEAN_CR2 |
555 1.6.2.4 pgoyette VMCB_CTRL_VMCB_CLEAN_LBR |
556 1.6.2.4 pgoyette VMCB_CTRL_VMCB_CLEAN_AVIC;
557 1.6.2.4 pgoyette }
558 1.6.2.4 pgoyette
559 1.6.2.4 pgoyette static void
560 1.6.2.4 pgoyette svm_vmcb_cache_update(struct vmcb *vmcb, uint64_t flags)
561 1.6.2.4 pgoyette {
562 1.6.2.4 pgoyette if (flags & NVMM_X64_STATE_SEGS) {
563 1.6.2.4 pgoyette vmcb->ctrl.vmcb_clean &=
564 1.6.2.4 pgoyette ~(VMCB_CTRL_VMCB_CLEAN_SEG | VMCB_CTRL_VMCB_CLEAN_DT);
565 1.6.2.4 pgoyette }
566 1.6.2.4 pgoyette if (flags & NVMM_X64_STATE_CRS) {
567 1.6.2.4 pgoyette vmcb->ctrl.vmcb_clean &=
568 1.6.2.4 pgoyette ~(VMCB_CTRL_VMCB_CLEAN_CR | VMCB_CTRL_VMCB_CLEAN_CR2 |
569 1.6.2.4 pgoyette VMCB_CTRL_VMCB_CLEAN_TPR);
570 1.6.2.4 pgoyette }
571 1.6.2.4 pgoyette if (flags & NVMM_X64_STATE_DRS) {
572 1.6.2.4 pgoyette vmcb->ctrl.vmcb_clean &= ~VMCB_CTRL_VMCB_CLEAN_DR;
573 1.6.2.4 pgoyette }
574 1.6.2.4 pgoyette if (flags & NVMM_X64_STATE_MSRS) {
575 1.6.2.4 pgoyette /* CR for EFER, NP for PAT. */
576 1.6.2.4 pgoyette vmcb->ctrl.vmcb_clean &=
577 1.6.2.4 pgoyette ~(VMCB_CTRL_VMCB_CLEAN_CR | VMCB_CTRL_VMCB_CLEAN_NP);
578 1.6.2.4 pgoyette }
579 1.6.2.4 pgoyette if (flags & NVMM_X64_STATE_MISC) {
580 1.6.2.4 pgoyette /* SEG for CPL. */
581 1.6.2.4 pgoyette vmcb->ctrl.vmcb_clean &= ~VMCB_CTRL_VMCB_CLEAN_SEG;
582 1.6.2.4 pgoyette }
583 1.6.2.4 pgoyette }
584 1.6.2.4 pgoyette
585 1.6.2.4 pgoyette static inline void
586 1.6.2.4 pgoyette svm_vmcb_cache_flush(struct vmcb *vmcb, uint64_t flags)
587 1.6.2.4 pgoyette {
588 1.6.2.4 pgoyette vmcb->ctrl.vmcb_clean &= ~flags;
589 1.6.2.4 pgoyette }
590 1.6.2.4 pgoyette
591 1.6.2.4 pgoyette static inline void
592 1.6.2.4 pgoyette svm_vmcb_cache_flush_all(struct vmcb *vmcb)
593 1.6.2.4 pgoyette {
594 1.6.2.4 pgoyette vmcb->ctrl.vmcb_clean = 0;
595 1.6.2.4 pgoyette }
596 1.6.2.4 pgoyette
597 1.6.2.2 pgoyette #define SVM_EVENT_TYPE_HW_INT 0
598 1.6.2.2 pgoyette #define SVM_EVENT_TYPE_NMI 2
599 1.6.2.2 pgoyette #define SVM_EVENT_TYPE_EXC 3
600 1.6.2.2 pgoyette #define SVM_EVENT_TYPE_SW_INT 4
601 1.6.2.2 pgoyette
602 1.6.2.2 pgoyette static void
603 1.6.2.4 pgoyette svm_event_waitexit_enable(struct nvmm_cpu *vcpu, bool nmi)
604 1.6.2.2 pgoyette {
605 1.6.2.4 pgoyette struct svm_cpudata *cpudata = vcpu->cpudata;
606 1.6.2.4 pgoyette struct vmcb *vmcb = cpudata->vmcb;
607 1.6.2.4 pgoyette
608 1.6.2.2 pgoyette if (nmi) {
609 1.6.2.2 pgoyette vmcb->ctrl.intercept_misc1 |= VMCB_CTRL_INTERCEPT_IRET;
610 1.6.2.4 pgoyette cpudata->nmi_window_exit = true;
611 1.6.2.2 pgoyette } else {
612 1.6.2.2 pgoyette vmcb->ctrl.intercept_misc1 |= VMCB_CTRL_INTERCEPT_VINTR;
613 1.6.2.4 pgoyette vmcb->ctrl.v |= (VMCB_CTRL_V_IRQ | VMCB_CTRL_V_IGN_TPR);
614 1.6.2.4 pgoyette svm_vmcb_cache_flush(vmcb, VMCB_CTRL_VMCB_CLEAN_TPR);
615 1.6.2.4 pgoyette cpudata->int_window_exit = true;
616 1.6.2.2 pgoyette }
617 1.6.2.4 pgoyette
618 1.6.2.4 pgoyette svm_vmcb_cache_flush(vmcb, VMCB_CTRL_VMCB_CLEAN_I);
619 1.6.2.2 pgoyette }
620 1.6.2.2 pgoyette
621 1.6.2.2 pgoyette static void
622 1.6.2.4 pgoyette svm_event_waitexit_disable(struct nvmm_cpu *vcpu, bool nmi)
623 1.6.2.2 pgoyette {
624 1.6.2.4 pgoyette struct svm_cpudata *cpudata = vcpu->cpudata;
625 1.6.2.4 pgoyette struct vmcb *vmcb = cpudata->vmcb;
626 1.6.2.4 pgoyette
627 1.6.2.2 pgoyette if (nmi) {
628 1.6.2.2 pgoyette vmcb->ctrl.intercept_misc1 &= ~VMCB_CTRL_INTERCEPT_IRET;
629 1.6.2.4 pgoyette cpudata->nmi_window_exit = false;
630 1.6.2.2 pgoyette } else {
631 1.6.2.2 pgoyette vmcb->ctrl.intercept_misc1 &= ~VMCB_CTRL_INTERCEPT_VINTR;
632 1.6.2.4 pgoyette vmcb->ctrl.v &= ~(VMCB_CTRL_V_IRQ | VMCB_CTRL_V_IGN_TPR);
633 1.6.2.4 pgoyette svm_vmcb_cache_flush(vmcb, VMCB_CTRL_VMCB_CLEAN_TPR);
634 1.6.2.4 pgoyette cpudata->int_window_exit = false;
635 1.6.2.2 pgoyette }
636 1.6.2.4 pgoyette
637 1.6.2.4 pgoyette svm_vmcb_cache_flush(vmcb, VMCB_CTRL_VMCB_CLEAN_I);
638 1.6.2.2 pgoyette }
639 1.6.2.2 pgoyette
640 1.6.2.2 pgoyette static inline int
641 1.6.2.2 pgoyette svm_event_has_error(uint64_t vector)
642 1.6.2.2 pgoyette {
643 1.6.2.2 pgoyette switch (vector) {
644 1.6.2.2 pgoyette case 8: /* #DF */
645 1.6.2.2 pgoyette case 10: /* #TS */
646 1.6.2.2 pgoyette case 11: /* #NP */
647 1.6.2.2 pgoyette case 12: /* #SS */
648 1.6.2.2 pgoyette case 13: /* #GP */
649 1.6.2.2 pgoyette case 14: /* #PF */
650 1.6.2.2 pgoyette case 17: /* #AC */
651 1.6.2.2 pgoyette case 30: /* #SX */
652 1.6.2.2 pgoyette return 1;
653 1.6.2.2 pgoyette default:
654 1.6.2.2 pgoyette return 0;
655 1.6.2.2 pgoyette }
656 1.6.2.2 pgoyette }
657 1.6.2.2 pgoyette
658 1.6.2.2 pgoyette static int
659 1.6.2.2 pgoyette svm_vcpu_inject(struct nvmm_machine *mach, struct nvmm_cpu *vcpu,
660 1.6.2.2 pgoyette struct nvmm_event *event)
661 1.6.2.2 pgoyette {
662 1.6.2.2 pgoyette struct svm_cpudata *cpudata = vcpu->cpudata;
663 1.6.2.2 pgoyette struct vmcb *vmcb = cpudata->vmcb;
664 1.6.2.2 pgoyette int type = 0, err = 0;
665 1.6.2.2 pgoyette
666 1.6.2.2 pgoyette if (event->vector >= 256) {
667 1.6.2.2 pgoyette return EINVAL;
668 1.6.2.2 pgoyette }
669 1.6.2.2 pgoyette
670 1.6.2.2 pgoyette switch (event->type) {
671 1.6.2.2 pgoyette case NVMM_EVENT_INTERRUPT_HW:
672 1.6.2.2 pgoyette type = SVM_EVENT_TYPE_HW_INT;
673 1.6.2.2 pgoyette if (event->vector == 2) {
674 1.6.2.2 pgoyette type = SVM_EVENT_TYPE_NMI;
675 1.6.2.2 pgoyette }
676 1.6.2.2 pgoyette if (type == SVM_EVENT_TYPE_NMI) {
677 1.6.2.4 pgoyette if (cpudata->nmi_window_exit) {
678 1.6.2.2 pgoyette return EAGAIN;
679 1.6.2.2 pgoyette }
680 1.6.2.4 pgoyette svm_event_waitexit_enable(vcpu, true);
681 1.6.2.2 pgoyette } else {
682 1.6.2.4 pgoyette if (((vmcb->state.rflags & PSL_I) == 0) ||
683 1.6.2.4 pgoyette ((vmcb->ctrl.intr & VMCB_CTRL_INTR_SHADOW) != 0)) {
684 1.6.2.4 pgoyette svm_event_waitexit_enable(vcpu, false);
685 1.6.2.2 pgoyette return EAGAIN;
686 1.6.2.2 pgoyette }
687 1.6.2.2 pgoyette }
688 1.6.2.2 pgoyette err = 0;
689 1.6.2.2 pgoyette break;
690 1.6.2.2 pgoyette case NVMM_EVENT_INTERRUPT_SW:
691 1.6.2.2 pgoyette type = SVM_EVENT_TYPE_SW_INT;
692 1.6.2.2 pgoyette err = 0;
693 1.6.2.2 pgoyette break;
694 1.6.2.2 pgoyette case NVMM_EVENT_EXCEPTION:
695 1.6.2.2 pgoyette type = SVM_EVENT_TYPE_EXC;
696 1.6.2.2 pgoyette if (event->vector == 2 || event->vector >= 32)
697 1.6.2.2 pgoyette return EINVAL;
698 1.6.2.2 pgoyette err = svm_event_has_error(event->vector);
699 1.6.2.2 pgoyette break;
700 1.6.2.2 pgoyette default:
701 1.6.2.2 pgoyette return EINVAL;
702 1.6.2.2 pgoyette }
703 1.6.2.2 pgoyette
704 1.6.2.2 pgoyette vmcb->ctrl.eventinj =
705 1.6.2.2 pgoyette __SHIFTIN(event->vector, VMCB_CTRL_EVENTINJ_VECTOR) |
706 1.6.2.2 pgoyette __SHIFTIN(type, VMCB_CTRL_EVENTINJ_TYPE) |
707 1.6.2.2 pgoyette __SHIFTIN(err, VMCB_CTRL_EVENTINJ_EV) |
708 1.6.2.2 pgoyette __SHIFTIN(1, VMCB_CTRL_EVENTINJ_V) |
709 1.6.2.2 pgoyette __SHIFTIN(event->u.error, VMCB_CTRL_EVENTINJ_ERRORCODE);
710 1.6.2.2 pgoyette
711 1.6.2.2 pgoyette return 0;
712 1.6.2.2 pgoyette }
713 1.6.2.2 pgoyette
714 1.6.2.2 pgoyette static void
715 1.6.2.2 pgoyette svm_inject_ud(struct nvmm_machine *mach, struct nvmm_cpu *vcpu)
716 1.6.2.2 pgoyette {
717 1.6.2.2 pgoyette struct nvmm_event event;
718 1.6.2.2 pgoyette int ret __diagused;
719 1.6.2.2 pgoyette
720 1.6.2.2 pgoyette event.type = NVMM_EVENT_EXCEPTION;
721 1.6.2.2 pgoyette event.vector = 6;
722 1.6.2.2 pgoyette event.u.error = 0;
723 1.6.2.2 pgoyette
724 1.6.2.2 pgoyette ret = svm_vcpu_inject(mach, vcpu, &event);
725 1.6.2.2 pgoyette KASSERT(ret == 0);
726 1.6.2.2 pgoyette }
727 1.6.2.2 pgoyette
728 1.6.2.2 pgoyette static void
729 1.6.2.2 pgoyette svm_inject_db(struct nvmm_machine *mach, struct nvmm_cpu *vcpu)
730 1.6.2.2 pgoyette {
731 1.6.2.2 pgoyette struct nvmm_event event;
732 1.6.2.2 pgoyette int ret __diagused;
733 1.6.2.2 pgoyette
734 1.6.2.2 pgoyette event.type = NVMM_EVENT_EXCEPTION;
735 1.6.2.2 pgoyette event.vector = 1;
736 1.6.2.2 pgoyette event.u.error = 0;
737 1.6.2.2 pgoyette
738 1.6.2.2 pgoyette ret = svm_vcpu_inject(mach, vcpu, &event);
739 1.6.2.2 pgoyette KASSERT(ret == 0);
740 1.6.2.2 pgoyette }
741 1.6.2.2 pgoyette
742 1.6.2.2 pgoyette static void
743 1.6.2.2 pgoyette svm_inject_gp(struct nvmm_machine *mach, struct nvmm_cpu *vcpu)
744 1.6.2.2 pgoyette {
745 1.6.2.2 pgoyette struct nvmm_event event;
746 1.6.2.2 pgoyette int ret __diagused;
747 1.6.2.2 pgoyette
748 1.6.2.2 pgoyette event.type = NVMM_EVENT_EXCEPTION;
749 1.6.2.2 pgoyette event.vector = 13;
750 1.6.2.2 pgoyette event.u.error = 0;
751 1.6.2.2 pgoyette
752 1.6.2.2 pgoyette ret = svm_vcpu_inject(mach, vcpu, &event);
753 1.6.2.2 pgoyette KASSERT(ret == 0);
754 1.6.2.2 pgoyette }
755 1.6.2.2 pgoyette
756 1.6.2.2 pgoyette static void
757 1.6.2.2 pgoyette svm_inkernel_handle_cpuid(struct nvmm_cpu *vcpu, uint64_t eax, uint64_t ecx)
758 1.6.2.2 pgoyette {
759 1.6.2.2 pgoyette struct svm_cpudata *cpudata = vcpu->cpudata;
760 1.6.2.2 pgoyette
761 1.6.2.2 pgoyette switch (eax) {
762 1.6.2.2 pgoyette case 0x00000001: /* APIC number in RBX. The rest is tunable. */
763 1.6.2.4 pgoyette cpudata->gprs[NVMM_X64_GPR_RBX] &= ~CPUID_LOCAL_APIC_ID;
764 1.6.2.4 pgoyette cpudata->gprs[NVMM_X64_GPR_RBX] |= __SHIFTIN(vcpu->cpuid,
765 1.6.2.2 pgoyette CPUID_LOCAL_APIC_ID);
766 1.6.2.2 pgoyette break;
767 1.6.2.2 pgoyette case 0x0000000D: /* FPU description. Not tunable. */
768 1.6.2.2 pgoyette if (ecx != 0 || svm_xcr0_mask == 0) {
769 1.6.2.2 pgoyette break;
770 1.6.2.2 pgoyette }
771 1.6.2.2 pgoyette cpudata->vmcb->state.rax = svm_xcr0_mask & 0xFFFFFFFF;
772 1.6.2.4 pgoyette if (cpudata->gxcr0 & XCR0_SSE) {
773 1.6.2.4 pgoyette cpudata->gprs[NVMM_X64_GPR_RBX] = sizeof(struct fxsave);
774 1.6.2.2 pgoyette } else {
775 1.6.2.4 pgoyette cpudata->gprs[NVMM_X64_GPR_RBX] = sizeof(struct save87);
776 1.6.2.2 pgoyette }
777 1.6.2.4 pgoyette cpudata->gprs[NVMM_X64_GPR_RBX] += 64; /* XSAVE header */
778 1.6.2.4 pgoyette cpudata->gprs[NVMM_X64_GPR_RCX] = sizeof(struct fxsave);
779 1.6.2.4 pgoyette cpudata->gprs[NVMM_X64_GPR_RDX] = svm_xcr0_mask >> 32;
780 1.6.2.4 pgoyette break;
781 1.6.2.4 pgoyette case 0x40000000:
782 1.6.2.4 pgoyette memcpy(&cpudata->gprs[NVMM_X64_GPR_RBX], "___ ", 4);
783 1.6.2.4 pgoyette memcpy(&cpudata->gprs[NVMM_X64_GPR_RCX], "NVMM", 4);
784 1.6.2.4 pgoyette memcpy(&cpudata->gprs[NVMM_X64_GPR_RDX], " ___", 4);
785 1.6.2.4 pgoyette break;
786 1.6.2.4 pgoyette case 0x80000001: /* No SVM in ECX. The rest is tunable. */
787 1.6.2.4 pgoyette cpudata->gprs[NVMM_X64_GPR_RCX] &= ~CPUID_SVM;
788 1.6.2.2 pgoyette break;
789 1.6.2.2 pgoyette default:
790 1.6.2.2 pgoyette break;
791 1.6.2.2 pgoyette }
792 1.6.2.2 pgoyette }
793 1.6.2.2 pgoyette
794 1.6.2.2 pgoyette static void
795 1.6.2.2 pgoyette svm_exit_cpuid(struct nvmm_machine *mach, struct nvmm_cpu *vcpu,
796 1.6.2.2 pgoyette struct nvmm_exit *exit)
797 1.6.2.2 pgoyette {
798 1.6.2.2 pgoyette struct svm_machdata *machdata = mach->machdata;
799 1.6.2.2 pgoyette struct svm_cpudata *cpudata = vcpu->cpudata;
800 1.6.2.2 pgoyette struct nvmm_x86_conf_cpuid *cpuid;
801 1.6.2.2 pgoyette uint64_t eax, ecx;
802 1.6.2.2 pgoyette u_int descs[4];
803 1.6.2.2 pgoyette size_t i;
804 1.6.2.2 pgoyette
805 1.6.2.2 pgoyette eax = cpudata->vmcb->state.rax;
806 1.6.2.4 pgoyette ecx = cpudata->gprs[NVMM_X64_GPR_RCX];
807 1.6.2.2 pgoyette x86_cpuid2(eax, ecx, descs);
808 1.6.2.2 pgoyette
809 1.6.2.2 pgoyette cpudata->vmcb->state.rax = descs[0];
810 1.6.2.4 pgoyette cpudata->gprs[NVMM_X64_GPR_RBX] = descs[1];
811 1.6.2.4 pgoyette cpudata->gprs[NVMM_X64_GPR_RCX] = descs[2];
812 1.6.2.4 pgoyette cpudata->gprs[NVMM_X64_GPR_RDX] = descs[3];
813 1.6.2.2 pgoyette
814 1.6.2.2 pgoyette for (i = 0; i < SVM_NCPUIDS; i++) {
815 1.6.2.2 pgoyette cpuid = &machdata->cpuid[i];
816 1.6.2.2 pgoyette if (!machdata->cpuidpresent[i]) {
817 1.6.2.2 pgoyette continue;
818 1.6.2.2 pgoyette }
819 1.6.2.2 pgoyette if (cpuid->leaf != eax) {
820 1.6.2.2 pgoyette continue;
821 1.6.2.2 pgoyette }
822 1.6.2.2 pgoyette
823 1.6.2.2 pgoyette /* del */
824 1.6.2.2 pgoyette cpudata->vmcb->state.rax &= ~cpuid->del.eax;
825 1.6.2.4 pgoyette cpudata->gprs[NVMM_X64_GPR_RBX] &= ~cpuid->del.ebx;
826 1.6.2.4 pgoyette cpudata->gprs[NVMM_X64_GPR_RCX] &= ~cpuid->del.ecx;
827 1.6.2.4 pgoyette cpudata->gprs[NVMM_X64_GPR_RDX] &= ~cpuid->del.edx;
828 1.6.2.2 pgoyette
829 1.6.2.2 pgoyette /* set */
830 1.6.2.2 pgoyette cpudata->vmcb->state.rax |= cpuid->set.eax;
831 1.6.2.4 pgoyette cpudata->gprs[NVMM_X64_GPR_RBX] |= cpuid->set.ebx;
832 1.6.2.4 pgoyette cpudata->gprs[NVMM_X64_GPR_RCX] |= cpuid->set.ecx;
833 1.6.2.4 pgoyette cpudata->gprs[NVMM_X64_GPR_RDX] |= cpuid->set.edx;
834 1.6.2.2 pgoyette
835 1.6.2.2 pgoyette break;
836 1.6.2.2 pgoyette }
837 1.6.2.2 pgoyette
838 1.6.2.2 pgoyette /* Overwrite non-tunable leaves. */
839 1.6.2.2 pgoyette svm_inkernel_handle_cpuid(vcpu, eax, ecx);
840 1.6.2.2 pgoyette
841 1.6.2.2 pgoyette /* For now we omit DBREGS. */
842 1.6.2.2 pgoyette if (__predict_false(cpudata->vmcb->state.rflags & PSL_T)) {
843 1.6.2.2 pgoyette svm_inject_db(mach, vcpu);
844 1.6.2.2 pgoyette }
845 1.6.2.2 pgoyette
846 1.6.2.2 pgoyette cpudata->vmcb->state.rip = cpudata->vmcb->ctrl.nrip;
847 1.6.2.2 pgoyette exit->reason = NVMM_EXIT_NONE;
848 1.6.2.2 pgoyette }
849 1.6.2.2 pgoyette
850 1.6.2.4 pgoyette static void
851 1.6.2.4 pgoyette svm_exit_hlt(struct nvmm_machine *mach, struct nvmm_cpu *vcpu,
852 1.6.2.4 pgoyette struct nvmm_exit *exit)
853 1.6.2.4 pgoyette {
854 1.6.2.4 pgoyette struct svm_cpudata *cpudata = vcpu->cpudata;
855 1.6.2.4 pgoyette
856 1.6.2.4 pgoyette exit->reason = NVMM_EXIT_HLT;
857 1.6.2.4 pgoyette exit->u.hlt.npc = cpudata->vmcb->ctrl.nrip;
858 1.6.2.4 pgoyette }
859 1.6.2.4 pgoyette
860 1.6.2.2 pgoyette #define SVM_EXIT_IO_PORT __BITS(31,16)
861 1.6.2.2 pgoyette #define SVM_EXIT_IO_SEG __BITS(12,10)
862 1.6.2.2 pgoyette #define SVM_EXIT_IO_A64 __BIT(9)
863 1.6.2.2 pgoyette #define SVM_EXIT_IO_A32 __BIT(8)
864 1.6.2.2 pgoyette #define SVM_EXIT_IO_A16 __BIT(7)
865 1.6.2.2 pgoyette #define SVM_EXIT_IO_SZ32 __BIT(6)
866 1.6.2.2 pgoyette #define SVM_EXIT_IO_SZ16 __BIT(5)
867 1.6.2.2 pgoyette #define SVM_EXIT_IO_SZ8 __BIT(4)
868 1.6.2.2 pgoyette #define SVM_EXIT_IO_REP __BIT(3)
869 1.6.2.2 pgoyette #define SVM_EXIT_IO_STR __BIT(2)
870 1.6.2.2 pgoyette #define SVM_EXIT_IO_IN __BIT(0)
871 1.6.2.2 pgoyette
872 1.6.2.2 pgoyette static const int seg_to_nvmm[] = {
873 1.6.2.2 pgoyette [0] = NVMM_X64_SEG_ES,
874 1.6.2.2 pgoyette [1] = NVMM_X64_SEG_CS,
875 1.6.2.2 pgoyette [2] = NVMM_X64_SEG_SS,
876 1.6.2.2 pgoyette [3] = NVMM_X64_SEG_DS,
877 1.6.2.2 pgoyette [4] = NVMM_X64_SEG_FS,
878 1.6.2.2 pgoyette [5] = NVMM_X64_SEG_GS
879 1.6.2.2 pgoyette };
880 1.6.2.2 pgoyette
881 1.6.2.2 pgoyette static void
882 1.6.2.2 pgoyette svm_exit_io(struct nvmm_machine *mach, struct nvmm_cpu *vcpu,
883 1.6.2.2 pgoyette struct nvmm_exit *exit)
884 1.6.2.2 pgoyette {
885 1.6.2.2 pgoyette struct svm_cpudata *cpudata = vcpu->cpudata;
886 1.6.2.2 pgoyette uint64_t info = cpudata->vmcb->ctrl.exitinfo1;
887 1.6.2.2 pgoyette uint64_t nextpc = cpudata->vmcb->ctrl.exitinfo2;
888 1.6.2.2 pgoyette
889 1.6.2.2 pgoyette exit->reason = NVMM_EXIT_IO;
890 1.6.2.2 pgoyette
891 1.6.2.2 pgoyette if (info & SVM_EXIT_IO_IN) {
892 1.6.2.2 pgoyette exit->u.io.type = NVMM_EXIT_IO_IN;
893 1.6.2.2 pgoyette } else {
894 1.6.2.2 pgoyette exit->u.io.type = NVMM_EXIT_IO_OUT;
895 1.6.2.2 pgoyette }
896 1.6.2.2 pgoyette
897 1.6.2.2 pgoyette exit->u.io.port = __SHIFTOUT(info, SVM_EXIT_IO_PORT);
898 1.6.2.2 pgoyette
899 1.6.2.2 pgoyette if (svm_decode_assist) {
900 1.6.2.2 pgoyette KASSERT(__SHIFTOUT(info, SVM_EXIT_IO_SEG) < 6);
901 1.6.2.2 pgoyette exit->u.io.seg = seg_to_nvmm[__SHIFTOUT(info, SVM_EXIT_IO_SEG)];
902 1.6.2.2 pgoyette } else {
903 1.6.2.4 pgoyette exit->u.io.seg = -1;
904 1.6.2.2 pgoyette }
905 1.6.2.2 pgoyette
906 1.6.2.2 pgoyette if (info & SVM_EXIT_IO_A64) {
907 1.6.2.2 pgoyette exit->u.io.address_size = 8;
908 1.6.2.2 pgoyette } else if (info & SVM_EXIT_IO_A32) {
909 1.6.2.2 pgoyette exit->u.io.address_size = 4;
910 1.6.2.2 pgoyette } else if (info & SVM_EXIT_IO_A16) {
911 1.6.2.2 pgoyette exit->u.io.address_size = 2;
912 1.6.2.2 pgoyette }
913 1.6.2.2 pgoyette
914 1.6.2.2 pgoyette if (info & SVM_EXIT_IO_SZ32) {
915 1.6.2.2 pgoyette exit->u.io.operand_size = 4;
916 1.6.2.2 pgoyette } else if (info & SVM_EXIT_IO_SZ16) {
917 1.6.2.2 pgoyette exit->u.io.operand_size = 2;
918 1.6.2.2 pgoyette } else if (info & SVM_EXIT_IO_SZ8) {
919 1.6.2.2 pgoyette exit->u.io.operand_size = 1;
920 1.6.2.2 pgoyette }
921 1.6.2.2 pgoyette
922 1.6.2.2 pgoyette exit->u.io.rep = (info & SVM_EXIT_IO_REP) != 0;
923 1.6.2.2 pgoyette exit->u.io.str = (info & SVM_EXIT_IO_STR) != 0;
924 1.6.2.2 pgoyette exit->u.io.npc = nextpc;
925 1.6.2.2 pgoyette }
926 1.6.2.2 pgoyette
927 1.6.2.4 pgoyette static const uint64_t msr_ignore_list[] = {
928 1.6.2.4 pgoyette 0xc0010055, /* MSR_CMPHALT */
929 1.6.2.4 pgoyette MSR_DE_CFG,
930 1.6.2.4 pgoyette MSR_IC_CFG,
931 1.6.2.4 pgoyette MSR_UCODE_AMD_PATCHLEVEL
932 1.6.2.4 pgoyette };
933 1.6.2.4 pgoyette
934 1.6.2.2 pgoyette static bool
935 1.6.2.2 pgoyette svm_inkernel_handle_msr(struct nvmm_machine *mach, struct nvmm_cpu *vcpu,
936 1.6.2.2 pgoyette struct nvmm_exit *exit)
937 1.6.2.2 pgoyette {
938 1.6.2.2 pgoyette struct svm_cpudata *cpudata = vcpu->cpudata;
939 1.6.2.4 pgoyette uint64_t val;
940 1.6.2.4 pgoyette size_t i;
941 1.6.2.2 pgoyette
942 1.6.2.2 pgoyette switch (exit->u.msr.type) {
943 1.6.2.2 pgoyette case NVMM_EXIT_MSR_RDMSR:
944 1.6.2.2 pgoyette if (exit->u.msr.msr == MSR_CR_PAT) {
945 1.6.2.4 pgoyette val = cpudata->vmcb->state.g_pat;
946 1.6.2.4 pgoyette cpudata->vmcb->state.rax = (val & 0xFFFFFFFF);
947 1.6.2.4 pgoyette cpudata->gprs[NVMM_X64_GPR_RDX] = (val >> 32);
948 1.6.2.4 pgoyette goto handled;
949 1.6.2.4 pgoyette }
950 1.6.2.4 pgoyette if (exit->u.msr.msr == MSR_NB_CFG) {
951 1.6.2.4 pgoyette val = NB_CFG_INITAPICCPUIDLO;
952 1.6.2.4 pgoyette cpudata->vmcb->state.rax = (val & 0xFFFFFFFF);
953 1.6.2.4 pgoyette cpudata->gprs[NVMM_X64_GPR_RDX] = (val >> 32);
954 1.6.2.4 pgoyette goto handled;
955 1.6.2.4 pgoyette }
956 1.6.2.4 pgoyette for (i = 0; i < __arraycount(msr_ignore_list); i++) {
957 1.6.2.4 pgoyette if (msr_ignore_list[i] != exit->u.msr.msr)
958 1.6.2.4 pgoyette continue;
959 1.6.2.4 pgoyette val = 0;
960 1.6.2.4 pgoyette cpudata->vmcb->state.rax = (val & 0xFFFFFFFF);
961 1.6.2.4 pgoyette cpudata->gprs[NVMM_X64_GPR_RDX] = (val >> 32);
962 1.6.2.2 pgoyette goto handled;
963 1.6.2.2 pgoyette }
964 1.6.2.2 pgoyette break;
965 1.6.2.2 pgoyette case NVMM_EXIT_MSR_WRMSR:
966 1.6.2.2 pgoyette if (exit->u.msr.msr == MSR_EFER) {
967 1.6.2.2 pgoyette if (__predict_false(exit->u.msr.val & ~EFER_VALID)) {
968 1.6.2.2 pgoyette svm_inject_gp(mach, vcpu);
969 1.6.2.2 pgoyette goto handled;
970 1.6.2.2 pgoyette }
971 1.6.2.2 pgoyette if ((cpudata->vmcb->state.efer ^ exit->u.msr.val) &
972 1.6.2.2 pgoyette EFER_TLB_FLUSH) {
973 1.6.2.2 pgoyette cpudata->tlb_want_flush = true;
974 1.6.2.2 pgoyette }
975 1.6.2.2 pgoyette cpudata->vmcb->state.efer = exit->u.msr.val | EFER_SVME;
976 1.6.2.2 pgoyette goto handled;
977 1.6.2.2 pgoyette }
978 1.6.2.2 pgoyette if (exit->u.msr.msr == MSR_CR_PAT) {
979 1.6.2.2 pgoyette cpudata->vmcb->state.g_pat = exit->u.msr.val;
980 1.6.2.2 pgoyette goto handled;
981 1.6.2.2 pgoyette }
982 1.6.2.4 pgoyette for (i = 0; i < __arraycount(msr_ignore_list); i++) {
983 1.6.2.4 pgoyette if (msr_ignore_list[i] != exit->u.msr.msr)
984 1.6.2.4 pgoyette continue;
985 1.6.2.4 pgoyette goto handled;
986 1.6.2.4 pgoyette }
987 1.6.2.2 pgoyette break;
988 1.6.2.2 pgoyette }
989 1.6.2.2 pgoyette
990 1.6.2.2 pgoyette return false;
991 1.6.2.2 pgoyette
992 1.6.2.2 pgoyette handled:
993 1.6.2.2 pgoyette cpudata->vmcb->state.rip = cpudata->vmcb->ctrl.nrip;
994 1.6.2.2 pgoyette return true;
995 1.6.2.2 pgoyette }
996 1.6.2.2 pgoyette
997 1.6.2.2 pgoyette static void
998 1.6.2.2 pgoyette svm_exit_msr(struct nvmm_machine *mach, struct nvmm_cpu *vcpu,
999 1.6.2.2 pgoyette struct nvmm_exit *exit)
1000 1.6.2.2 pgoyette {
1001 1.6.2.2 pgoyette struct svm_cpudata *cpudata = vcpu->cpudata;
1002 1.6.2.2 pgoyette uint64_t info = cpudata->vmcb->ctrl.exitinfo1;
1003 1.6.2.2 pgoyette
1004 1.6.2.2 pgoyette if (info == 0) {
1005 1.6.2.2 pgoyette exit->u.msr.type = NVMM_EXIT_MSR_RDMSR;
1006 1.6.2.2 pgoyette } else {
1007 1.6.2.2 pgoyette exit->u.msr.type = NVMM_EXIT_MSR_WRMSR;
1008 1.6.2.2 pgoyette }
1009 1.6.2.2 pgoyette
1010 1.6.2.4 pgoyette exit->u.msr.msr = cpudata->gprs[NVMM_X64_GPR_RCX];
1011 1.6.2.2 pgoyette
1012 1.6.2.2 pgoyette if (info == 1) {
1013 1.6.2.2 pgoyette uint64_t rdx, rax;
1014 1.6.2.4 pgoyette rdx = cpudata->gprs[NVMM_X64_GPR_RDX];
1015 1.6.2.2 pgoyette rax = cpudata->vmcb->state.rax;
1016 1.6.2.2 pgoyette exit->u.msr.val = (rdx << 32) | (rax & 0xFFFFFFFF);
1017 1.6.2.2 pgoyette } else {
1018 1.6.2.2 pgoyette exit->u.msr.val = 0;
1019 1.6.2.2 pgoyette }
1020 1.6.2.2 pgoyette
1021 1.6.2.2 pgoyette if (svm_inkernel_handle_msr(mach, vcpu, exit)) {
1022 1.6.2.2 pgoyette exit->reason = NVMM_EXIT_NONE;
1023 1.6.2.2 pgoyette return;
1024 1.6.2.2 pgoyette }
1025 1.6.2.2 pgoyette
1026 1.6.2.2 pgoyette exit->reason = NVMM_EXIT_MSR;
1027 1.6.2.2 pgoyette exit->u.msr.npc = cpudata->vmcb->ctrl.nrip;
1028 1.6.2.2 pgoyette }
1029 1.6.2.2 pgoyette
1030 1.6.2.2 pgoyette static void
1031 1.6.2.2 pgoyette svm_exit_npf(struct nvmm_machine *mach, struct nvmm_cpu *vcpu,
1032 1.6.2.2 pgoyette struct nvmm_exit *exit)
1033 1.6.2.2 pgoyette {
1034 1.6.2.2 pgoyette struct svm_cpudata *cpudata = vcpu->cpudata;
1035 1.6.2.2 pgoyette gpaddr_t gpa = cpudata->vmcb->ctrl.exitinfo2;
1036 1.6.2.2 pgoyette int error;
1037 1.6.2.2 pgoyette
1038 1.6.2.2 pgoyette error = uvm_fault(&mach->vm->vm_map, gpa, VM_PROT_ALL);
1039 1.6.2.2 pgoyette
1040 1.6.2.2 pgoyette if (error) {
1041 1.6.2.2 pgoyette exit->reason = NVMM_EXIT_MEMORY;
1042 1.6.2.2 pgoyette if (cpudata->vmcb->ctrl.exitinfo1 & PGEX_W)
1043 1.6.2.2 pgoyette exit->u.mem.perm = NVMM_EXIT_MEMORY_WRITE;
1044 1.6.2.2 pgoyette else if (cpudata->vmcb->ctrl.exitinfo1 & PGEX_X)
1045 1.6.2.2 pgoyette exit->u.mem.perm = NVMM_EXIT_MEMORY_EXEC;
1046 1.6.2.2 pgoyette else
1047 1.6.2.2 pgoyette exit->u.mem.perm = NVMM_EXIT_MEMORY_READ;
1048 1.6.2.2 pgoyette exit->u.mem.gpa = gpa;
1049 1.6.2.2 pgoyette exit->u.mem.inst_len = cpudata->vmcb->ctrl.inst_len;
1050 1.6.2.2 pgoyette memcpy(exit->u.mem.inst_bytes, cpudata->vmcb->ctrl.inst_bytes,
1051 1.6.2.2 pgoyette sizeof(exit->u.mem.inst_bytes));
1052 1.6.2.2 pgoyette exit->u.mem.npc = cpudata->vmcb->ctrl.nrip;
1053 1.6.2.2 pgoyette } else {
1054 1.6.2.2 pgoyette exit->reason = NVMM_EXIT_NONE;
1055 1.6.2.2 pgoyette }
1056 1.6.2.2 pgoyette }
1057 1.6.2.2 pgoyette
1058 1.6.2.2 pgoyette static void
1059 1.6.2.2 pgoyette svm_exit_xsetbv(struct nvmm_machine *mach, struct nvmm_cpu *vcpu,
1060 1.6.2.2 pgoyette struct nvmm_exit *exit)
1061 1.6.2.2 pgoyette {
1062 1.6.2.2 pgoyette struct svm_cpudata *cpudata = vcpu->cpudata;
1063 1.6.2.2 pgoyette struct vmcb *vmcb = cpudata->vmcb;
1064 1.6.2.2 pgoyette uint64_t val;
1065 1.6.2.2 pgoyette
1066 1.6.2.2 pgoyette exit->reason = NVMM_EXIT_NONE;
1067 1.6.2.2 pgoyette
1068 1.6.2.4 pgoyette val = (cpudata->gprs[NVMM_X64_GPR_RDX] << 32) |
1069 1.6.2.2 pgoyette (vmcb->state.rax & 0xFFFFFFFF);
1070 1.6.2.2 pgoyette
1071 1.6.2.4 pgoyette if (__predict_false(cpudata->gprs[NVMM_X64_GPR_RCX] != 0)) {
1072 1.6.2.2 pgoyette goto error;
1073 1.6.2.2 pgoyette } else if (__predict_false(vmcb->state.cpl != 0)) {
1074 1.6.2.2 pgoyette goto error;
1075 1.6.2.2 pgoyette } else if (__predict_false((val & ~svm_xcr0_mask) != 0)) {
1076 1.6.2.2 pgoyette goto error;
1077 1.6.2.2 pgoyette } else if (__predict_false((val & XCR0_X87) == 0)) {
1078 1.6.2.2 pgoyette goto error;
1079 1.6.2.2 pgoyette }
1080 1.6.2.2 pgoyette
1081 1.6.2.4 pgoyette cpudata->gxcr0 = val;
1082 1.6.2.2 pgoyette
1083 1.6.2.3 pgoyette cpudata->vmcb->state.rip = cpudata->vmcb->ctrl.nrip;
1084 1.6.2.2 pgoyette return;
1085 1.6.2.2 pgoyette
1086 1.6.2.2 pgoyette error:
1087 1.6.2.2 pgoyette svm_inject_gp(mach, vcpu);
1088 1.6.2.2 pgoyette }
1089 1.6.2.2 pgoyette
1090 1.6.2.2 pgoyette static void
1091 1.6.2.2 pgoyette svm_vcpu_guest_fpu_enter(struct nvmm_cpu *vcpu)
1092 1.6.2.2 pgoyette {
1093 1.6.2.2 pgoyette struct svm_cpudata *cpudata = vcpu->cpudata;
1094 1.6.2.2 pgoyette
1095 1.6.2.2 pgoyette if (x86_xsave_features != 0) {
1096 1.6.2.4 pgoyette cpudata->hxcr0 = rdxcr(0);
1097 1.6.2.4 pgoyette wrxcr(0, cpudata->gxcr0);
1098 1.6.2.2 pgoyette }
1099 1.6.2.2 pgoyette
1100 1.6.2.2 pgoyette cpudata->ts_set = (rcr0() & CR0_TS) != 0;
1101 1.6.2.2 pgoyette
1102 1.6.2.2 pgoyette fpu_area_save(&cpudata->hfpu);
1103 1.6.2.2 pgoyette fpu_area_restore(&cpudata->gfpu);
1104 1.6.2.2 pgoyette }
1105 1.6.2.2 pgoyette
1106 1.6.2.2 pgoyette static void
1107 1.6.2.2 pgoyette svm_vcpu_guest_fpu_leave(struct nvmm_cpu *vcpu)
1108 1.6.2.2 pgoyette {
1109 1.6.2.2 pgoyette struct svm_cpudata *cpudata = vcpu->cpudata;
1110 1.6.2.2 pgoyette
1111 1.6.2.2 pgoyette fpu_area_save(&cpudata->gfpu);
1112 1.6.2.2 pgoyette fpu_area_restore(&cpudata->hfpu);
1113 1.6.2.2 pgoyette
1114 1.6.2.2 pgoyette if (cpudata->ts_set) {
1115 1.6.2.2 pgoyette stts();
1116 1.6.2.2 pgoyette }
1117 1.6.2.2 pgoyette
1118 1.6.2.2 pgoyette if (x86_xsave_features != 0) {
1119 1.6.2.4 pgoyette cpudata->gxcr0 = rdxcr(0);
1120 1.6.2.4 pgoyette wrxcr(0, cpudata->hxcr0);
1121 1.6.2.2 pgoyette }
1122 1.6.2.2 pgoyette }
1123 1.6.2.2 pgoyette
1124 1.6.2.2 pgoyette static void
1125 1.6.2.2 pgoyette svm_vcpu_guest_dbregs_enter(struct nvmm_cpu *vcpu)
1126 1.6.2.2 pgoyette {
1127 1.6.2.2 pgoyette struct svm_cpudata *cpudata = vcpu->cpudata;
1128 1.6.2.2 pgoyette
1129 1.6.2.2 pgoyette x86_dbregs_save(curlwp);
1130 1.6.2.2 pgoyette
1131 1.6.2.4 pgoyette ldr7(0);
1132 1.6.2.4 pgoyette
1133 1.6.2.4 pgoyette ldr0(cpudata->drs[NVMM_X64_DR_DR0]);
1134 1.6.2.4 pgoyette ldr1(cpudata->drs[NVMM_X64_DR_DR1]);
1135 1.6.2.4 pgoyette ldr2(cpudata->drs[NVMM_X64_DR_DR2]);
1136 1.6.2.4 pgoyette ldr3(cpudata->drs[NVMM_X64_DR_DR3]);
1137 1.6.2.2 pgoyette }
1138 1.6.2.2 pgoyette
1139 1.6.2.2 pgoyette static void
1140 1.6.2.2 pgoyette svm_vcpu_guest_dbregs_leave(struct nvmm_cpu *vcpu)
1141 1.6.2.2 pgoyette {
1142 1.6.2.2 pgoyette struct svm_cpudata *cpudata = vcpu->cpudata;
1143 1.6.2.2 pgoyette
1144 1.6.2.4 pgoyette cpudata->drs[NVMM_X64_DR_DR0] = rdr0();
1145 1.6.2.4 pgoyette cpudata->drs[NVMM_X64_DR_DR1] = rdr1();
1146 1.6.2.4 pgoyette cpudata->drs[NVMM_X64_DR_DR2] = rdr2();
1147 1.6.2.4 pgoyette cpudata->drs[NVMM_X64_DR_DR3] = rdr3();
1148 1.6.2.2 pgoyette
1149 1.6.2.2 pgoyette x86_dbregs_restore(curlwp);
1150 1.6.2.2 pgoyette }
1151 1.6.2.2 pgoyette
1152 1.6.2.2 pgoyette static void
1153 1.6.2.2 pgoyette svm_vcpu_guest_misc_enter(struct nvmm_cpu *vcpu)
1154 1.6.2.2 pgoyette {
1155 1.6.2.2 pgoyette struct svm_cpudata *cpudata = vcpu->cpudata;
1156 1.6.2.2 pgoyette
1157 1.6.2.2 pgoyette cpudata->star = rdmsr(MSR_STAR);
1158 1.6.2.2 pgoyette cpudata->lstar = rdmsr(MSR_LSTAR);
1159 1.6.2.2 pgoyette cpudata->cstar = rdmsr(MSR_CSTAR);
1160 1.6.2.2 pgoyette cpudata->sfmask = rdmsr(MSR_SFMASK);
1161 1.6.2.4 pgoyette cpudata->fsbase = rdmsr(MSR_FSBASE);
1162 1.6.2.4 pgoyette cpudata->kernelgsbase = rdmsr(MSR_KERNELGSBASE);
1163 1.6.2.2 pgoyette }
1164 1.6.2.2 pgoyette
1165 1.6.2.2 pgoyette static void
1166 1.6.2.2 pgoyette svm_vcpu_guest_misc_leave(struct nvmm_cpu *vcpu)
1167 1.6.2.2 pgoyette {
1168 1.6.2.2 pgoyette struct svm_cpudata *cpudata = vcpu->cpudata;
1169 1.6.2.2 pgoyette
1170 1.6.2.2 pgoyette wrmsr(MSR_STAR, cpudata->star);
1171 1.6.2.2 pgoyette wrmsr(MSR_LSTAR, cpudata->lstar);
1172 1.6.2.2 pgoyette wrmsr(MSR_CSTAR, cpudata->cstar);
1173 1.6.2.2 pgoyette wrmsr(MSR_SFMASK, cpudata->sfmask);
1174 1.6.2.4 pgoyette wrmsr(MSR_FSBASE, cpudata->fsbase);
1175 1.6.2.4 pgoyette wrmsr(MSR_KERNELGSBASE, cpudata->kernelgsbase);
1176 1.6.2.2 pgoyette }
1177 1.6.2.2 pgoyette
1178 1.6.2.2 pgoyette static int
1179 1.6.2.2 pgoyette svm_vcpu_run(struct nvmm_machine *mach, struct nvmm_cpu *vcpu,
1180 1.6.2.2 pgoyette struct nvmm_exit *exit)
1181 1.6.2.2 pgoyette {
1182 1.6.2.2 pgoyette struct svm_cpudata *cpudata = vcpu->cpudata;
1183 1.6.2.2 pgoyette struct vmcb *vmcb = cpudata->vmcb;
1184 1.6.2.2 pgoyette bool tlb_need_flush = false;
1185 1.6.2.2 pgoyette int hcpu, s;
1186 1.6.2.2 pgoyette
1187 1.6.2.2 pgoyette kpreempt_disable();
1188 1.6.2.2 pgoyette hcpu = cpu_number();
1189 1.6.2.2 pgoyette
1190 1.6.2.2 pgoyette if (vcpu->hcpu_last != hcpu || cpudata->shared_asid) {
1191 1.6.2.2 pgoyette tlb_need_flush = true;
1192 1.6.2.2 pgoyette }
1193 1.6.2.2 pgoyette
1194 1.6.2.2 pgoyette if (cpudata->tlb_want_flush || tlb_need_flush) {
1195 1.6.2.2 pgoyette vmcb->ctrl.tlb_ctrl = svm_ctrl_tlb_flush;
1196 1.6.2.2 pgoyette } else {
1197 1.6.2.2 pgoyette vmcb->ctrl.tlb_ctrl = 0;
1198 1.6.2.2 pgoyette }
1199 1.6.2.2 pgoyette
1200 1.6.2.2 pgoyette if (vcpu->hcpu_last != hcpu) {
1201 1.6.2.2 pgoyette vmcb->ctrl.tsc_offset = cpudata->tsc_offset +
1202 1.6.2.2 pgoyette curcpu()->ci_data.cpu_cc_skew;
1203 1.6.2.4 pgoyette svm_vmcb_cache_flush_all(vmcb);
1204 1.6.2.2 pgoyette }
1205 1.6.2.2 pgoyette
1206 1.6.2.2 pgoyette svm_vcpu_guest_dbregs_enter(vcpu);
1207 1.6.2.2 pgoyette svm_vcpu_guest_misc_enter(vcpu);
1208 1.6.2.2 pgoyette
1209 1.6.2.2 pgoyette while (1) {
1210 1.6.2.2 pgoyette s = splhigh();
1211 1.6.2.2 pgoyette svm_vcpu_guest_fpu_enter(vcpu);
1212 1.6.2.4 pgoyette svm_vmrun(cpudata->vmcb_pa, cpudata->gprs);
1213 1.6.2.2 pgoyette svm_vcpu_guest_fpu_leave(vcpu);
1214 1.6.2.2 pgoyette splx(s);
1215 1.6.2.2 pgoyette
1216 1.6.2.2 pgoyette svm_vmcb_cache_default(vmcb);
1217 1.6.2.2 pgoyette
1218 1.6.2.2 pgoyette if (vmcb->ctrl.exitcode != VMCB_EXITCODE_INVALID) {
1219 1.6.2.2 pgoyette if (cpudata->tlb_want_flush) {
1220 1.6.2.2 pgoyette cpudata->tlb_want_flush = false;
1221 1.6.2.2 pgoyette }
1222 1.6.2.2 pgoyette vcpu->hcpu_last = hcpu;
1223 1.6.2.2 pgoyette }
1224 1.6.2.2 pgoyette
1225 1.6.2.2 pgoyette switch (vmcb->ctrl.exitcode) {
1226 1.6.2.2 pgoyette case VMCB_EXITCODE_INTR:
1227 1.6.2.2 pgoyette case VMCB_EXITCODE_NMI:
1228 1.6.2.2 pgoyette exit->reason = NVMM_EXIT_NONE;
1229 1.6.2.2 pgoyette break;
1230 1.6.2.2 pgoyette case VMCB_EXITCODE_VINTR:
1231 1.6.2.4 pgoyette svm_event_waitexit_disable(vcpu, false);
1232 1.6.2.2 pgoyette exit->reason = NVMM_EXIT_INT_READY;
1233 1.6.2.2 pgoyette break;
1234 1.6.2.2 pgoyette case VMCB_EXITCODE_IRET:
1235 1.6.2.4 pgoyette svm_event_waitexit_disable(vcpu, true);
1236 1.6.2.2 pgoyette exit->reason = NVMM_EXIT_NMI_READY;
1237 1.6.2.2 pgoyette break;
1238 1.6.2.2 pgoyette case VMCB_EXITCODE_CPUID:
1239 1.6.2.2 pgoyette svm_exit_cpuid(mach, vcpu, exit);
1240 1.6.2.2 pgoyette break;
1241 1.6.2.2 pgoyette case VMCB_EXITCODE_HLT:
1242 1.6.2.4 pgoyette svm_exit_hlt(mach, vcpu, exit);
1243 1.6.2.2 pgoyette break;
1244 1.6.2.2 pgoyette case VMCB_EXITCODE_IOIO:
1245 1.6.2.2 pgoyette svm_exit_io(mach, vcpu, exit);
1246 1.6.2.2 pgoyette break;
1247 1.6.2.2 pgoyette case VMCB_EXITCODE_MSR:
1248 1.6.2.2 pgoyette svm_exit_msr(mach, vcpu, exit);
1249 1.6.2.2 pgoyette break;
1250 1.6.2.2 pgoyette case VMCB_EXITCODE_SHUTDOWN:
1251 1.6.2.2 pgoyette exit->reason = NVMM_EXIT_SHUTDOWN;
1252 1.6.2.2 pgoyette break;
1253 1.6.2.2 pgoyette case VMCB_EXITCODE_RDPMC:
1254 1.6.2.2 pgoyette case VMCB_EXITCODE_RSM:
1255 1.6.2.2 pgoyette case VMCB_EXITCODE_INVLPGA:
1256 1.6.2.2 pgoyette case VMCB_EXITCODE_VMRUN:
1257 1.6.2.2 pgoyette case VMCB_EXITCODE_VMMCALL:
1258 1.6.2.2 pgoyette case VMCB_EXITCODE_VMLOAD:
1259 1.6.2.2 pgoyette case VMCB_EXITCODE_VMSAVE:
1260 1.6.2.2 pgoyette case VMCB_EXITCODE_STGI:
1261 1.6.2.2 pgoyette case VMCB_EXITCODE_CLGI:
1262 1.6.2.2 pgoyette case VMCB_EXITCODE_SKINIT:
1263 1.6.2.2 pgoyette case VMCB_EXITCODE_RDTSCP:
1264 1.6.2.2 pgoyette svm_inject_ud(mach, vcpu);
1265 1.6.2.2 pgoyette exit->reason = NVMM_EXIT_NONE;
1266 1.6.2.2 pgoyette break;
1267 1.6.2.2 pgoyette case VMCB_EXITCODE_MONITOR:
1268 1.6.2.2 pgoyette exit->reason = NVMM_EXIT_MONITOR;
1269 1.6.2.2 pgoyette break;
1270 1.6.2.2 pgoyette case VMCB_EXITCODE_MWAIT:
1271 1.6.2.2 pgoyette exit->reason = NVMM_EXIT_MWAIT;
1272 1.6.2.2 pgoyette break;
1273 1.6.2.2 pgoyette case VMCB_EXITCODE_MWAIT_CONDITIONAL:
1274 1.6.2.2 pgoyette exit->reason = NVMM_EXIT_MWAIT_COND;
1275 1.6.2.2 pgoyette break;
1276 1.6.2.2 pgoyette case VMCB_EXITCODE_XSETBV:
1277 1.6.2.2 pgoyette svm_exit_xsetbv(mach, vcpu, exit);
1278 1.6.2.2 pgoyette break;
1279 1.6.2.2 pgoyette case VMCB_EXITCODE_NPF:
1280 1.6.2.2 pgoyette svm_exit_npf(mach, vcpu, exit);
1281 1.6.2.2 pgoyette break;
1282 1.6.2.2 pgoyette case VMCB_EXITCODE_FERR_FREEZE: /* ? */
1283 1.6.2.2 pgoyette default:
1284 1.6.2.2 pgoyette exit->reason = NVMM_EXIT_INVALID;
1285 1.6.2.2 pgoyette break;
1286 1.6.2.2 pgoyette }
1287 1.6.2.2 pgoyette
1288 1.6.2.2 pgoyette /* If no reason to return to userland, keep rolling. */
1289 1.6.2.2 pgoyette if (curcpu()->ci_schedstate.spc_flags & SPCF_SHOULDYIELD) {
1290 1.6.2.2 pgoyette break;
1291 1.6.2.2 pgoyette }
1292 1.6.2.4 pgoyette if (curcpu()->ci_data.cpu_softints != 0) {
1293 1.6.2.4 pgoyette break;
1294 1.6.2.4 pgoyette }
1295 1.6.2.4 pgoyette if (curlwp->l_flag & LW_USERRET) {
1296 1.6.2.4 pgoyette break;
1297 1.6.2.4 pgoyette }
1298 1.6.2.2 pgoyette if (exit->reason != NVMM_EXIT_NONE) {
1299 1.6.2.2 pgoyette break;
1300 1.6.2.2 pgoyette }
1301 1.6.2.2 pgoyette }
1302 1.6.2.2 pgoyette
1303 1.6.2.2 pgoyette svm_vcpu_guest_misc_leave(vcpu);
1304 1.6.2.2 pgoyette svm_vcpu_guest_dbregs_leave(vcpu);
1305 1.6.2.2 pgoyette
1306 1.6.2.2 pgoyette kpreempt_enable();
1307 1.6.2.2 pgoyette
1308 1.6.2.2 pgoyette exit->exitstate[NVMM_X64_EXITSTATE_CR8] = __SHIFTOUT(vmcb->ctrl.v,
1309 1.6.2.2 pgoyette VMCB_CTRL_V_TPR);
1310 1.6.2.2 pgoyette exit->exitstate[NVMM_X64_EXITSTATE_RFLAGS] = vmcb->state.rflags;
1311 1.6.2.2 pgoyette
1312 1.6.2.4 pgoyette exit->exitstate[NVMM_X64_EXITSTATE_INT_SHADOW] =
1313 1.6.2.4 pgoyette ((vmcb->ctrl.intr & VMCB_CTRL_INTR_SHADOW) != 0);
1314 1.6.2.4 pgoyette exit->exitstate[NVMM_X64_EXITSTATE_INT_WINDOW_EXIT] =
1315 1.6.2.4 pgoyette cpudata->int_window_exit;
1316 1.6.2.4 pgoyette exit->exitstate[NVMM_X64_EXITSTATE_NMI_WINDOW_EXIT] =
1317 1.6.2.4 pgoyette cpudata->nmi_window_exit;
1318 1.6.2.4 pgoyette
1319 1.6.2.2 pgoyette return 0;
1320 1.6.2.2 pgoyette }
1321 1.6.2.2 pgoyette
1322 1.6.2.2 pgoyette /* -------------------------------------------------------------------------- */
1323 1.6.2.2 pgoyette
1324 1.6.2.2 pgoyette static int
1325 1.6.2.2 pgoyette svm_memalloc(paddr_t *pa, vaddr_t *va, size_t npages)
1326 1.6.2.2 pgoyette {
1327 1.6.2.2 pgoyette struct pglist pglist;
1328 1.6.2.2 pgoyette paddr_t _pa;
1329 1.6.2.2 pgoyette vaddr_t _va;
1330 1.6.2.2 pgoyette size_t i;
1331 1.6.2.2 pgoyette int ret;
1332 1.6.2.2 pgoyette
1333 1.6.2.2 pgoyette ret = uvm_pglistalloc(npages * PAGE_SIZE, 0, ~0UL, PAGE_SIZE, 0,
1334 1.6.2.2 pgoyette &pglist, 1, 0);
1335 1.6.2.2 pgoyette if (ret != 0)
1336 1.6.2.2 pgoyette return ENOMEM;
1337 1.6.2.2 pgoyette _pa = TAILQ_FIRST(&pglist)->phys_addr;
1338 1.6.2.2 pgoyette _va = uvm_km_alloc(kernel_map, npages * PAGE_SIZE, 0,
1339 1.6.2.2 pgoyette UVM_KMF_VAONLY | UVM_KMF_NOWAIT);
1340 1.6.2.2 pgoyette if (_va == 0)
1341 1.6.2.2 pgoyette goto error;
1342 1.6.2.2 pgoyette
1343 1.6.2.2 pgoyette for (i = 0; i < npages; i++) {
1344 1.6.2.2 pgoyette pmap_kenter_pa(_va + i * PAGE_SIZE, _pa + i * PAGE_SIZE,
1345 1.6.2.2 pgoyette VM_PROT_READ | VM_PROT_WRITE, PMAP_WRITE_BACK);
1346 1.6.2.2 pgoyette }
1347 1.6.2.2 pgoyette pmap_update(pmap_kernel());
1348 1.6.2.2 pgoyette
1349 1.6.2.2 pgoyette memset((void *)_va, 0, npages * PAGE_SIZE);
1350 1.6.2.2 pgoyette
1351 1.6.2.2 pgoyette *pa = _pa;
1352 1.6.2.2 pgoyette *va = _va;
1353 1.6.2.2 pgoyette return 0;
1354 1.6.2.2 pgoyette
1355 1.6.2.2 pgoyette error:
1356 1.6.2.2 pgoyette for (i = 0; i < npages; i++) {
1357 1.6.2.2 pgoyette uvm_pagefree(PHYS_TO_VM_PAGE(_pa + i * PAGE_SIZE));
1358 1.6.2.2 pgoyette }
1359 1.6.2.2 pgoyette return ENOMEM;
1360 1.6.2.2 pgoyette }
1361 1.6.2.2 pgoyette
1362 1.6.2.2 pgoyette static void
1363 1.6.2.2 pgoyette svm_memfree(paddr_t pa, vaddr_t va, size_t npages)
1364 1.6.2.2 pgoyette {
1365 1.6.2.2 pgoyette size_t i;
1366 1.6.2.2 pgoyette
1367 1.6.2.2 pgoyette pmap_kremove(va, npages * PAGE_SIZE);
1368 1.6.2.2 pgoyette pmap_update(pmap_kernel());
1369 1.6.2.2 pgoyette uvm_km_free(kernel_map, va, npages * PAGE_SIZE, UVM_KMF_VAONLY);
1370 1.6.2.2 pgoyette for (i = 0; i < npages; i++) {
1371 1.6.2.2 pgoyette uvm_pagefree(PHYS_TO_VM_PAGE(pa + i * PAGE_SIZE));
1372 1.6.2.2 pgoyette }
1373 1.6.2.2 pgoyette }
1374 1.6.2.2 pgoyette
1375 1.6.2.2 pgoyette /* -------------------------------------------------------------------------- */
1376 1.6.2.2 pgoyette
1377 1.6.2.2 pgoyette #define SVM_MSRBM_READ __BIT(0)
1378 1.6.2.2 pgoyette #define SVM_MSRBM_WRITE __BIT(1)
1379 1.6.2.2 pgoyette
1380 1.6.2.2 pgoyette static void
1381 1.6.2.2 pgoyette svm_vcpu_msr_allow(uint8_t *bitmap, uint64_t msr, bool read, bool write)
1382 1.6.2.2 pgoyette {
1383 1.6.2.2 pgoyette uint64_t byte;
1384 1.6.2.2 pgoyette uint8_t bitoff;
1385 1.6.2.2 pgoyette
1386 1.6.2.2 pgoyette if (msr < 0x00002000) {
1387 1.6.2.2 pgoyette /* Range 1 */
1388 1.6.2.2 pgoyette byte = ((msr - 0x00000000) >> 2UL) + 0x0000;
1389 1.6.2.2 pgoyette } else if (msr >= 0xC0000000 && msr < 0xC0002000) {
1390 1.6.2.2 pgoyette /* Range 2 */
1391 1.6.2.2 pgoyette byte = ((msr - 0xC0000000) >> 2UL) + 0x0800;
1392 1.6.2.2 pgoyette } else if (msr >= 0xC0010000 && msr < 0xC0012000) {
1393 1.6.2.2 pgoyette /* Range 3 */
1394 1.6.2.2 pgoyette byte = ((msr - 0xC0010000) >> 2UL) + 0x1000;
1395 1.6.2.2 pgoyette } else {
1396 1.6.2.2 pgoyette panic("%s: wrong range", __func__);
1397 1.6.2.2 pgoyette }
1398 1.6.2.2 pgoyette
1399 1.6.2.2 pgoyette bitoff = (msr & 0x3) << 1;
1400 1.6.2.2 pgoyette
1401 1.6.2.2 pgoyette if (read) {
1402 1.6.2.2 pgoyette bitmap[byte] &= ~(SVM_MSRBM_READ << bitoff);
1403 1.6.2.2 pgoyette }
1404 1.6.2.2 pgoyette if (write) {
1405 1.6.2.2 pgoyette bitmap[byte] &= ~(SVM_MSRBM_WRITE << bitoff);
1406 1.6.2.2 pgoyette }
1407 1.6.2.2 pgoyette }
1408 1.6.2.2 pgoyette
1409 1.6.2.2 pgoyette static void
1410 1.6.2.2 pgoyette svm_asid_alloc(struct nvmm_cpu *vcpu)
1411 1.6.2.2 pgoyette {
1412 1.6.2.2 pgoyette struct svm_cpudata *cpudata = vcpu->cpudata;
1413 1.6.2.2 pgoyette struct vmcb *vmcb = cpudata->vmcb;
1414 1.6.2.2 pgoyette size_t i, oct, bit;
1415 1.6.2.2 pgoyette
1416 1.6.2.2 pgoyette mutex_enter(&svm_asidlock);
1417 1.6.2.2 pgoyette
1418 1.6.2.2 pgoyette for (i = 0; i < svm_maxasid; i++) {
1419 1.6.2.2 pgoyette oct = i / 8;
1420 1.6.2.2 pgoyette bit = i % 8;
1421 1.6.2.2 pgoyette
1422 1.6.2.2 pgoyette if (svm_asidmap[oct] & __BIT(bit)) {
1423 1.6.2.2 pgoyette continue;
1424 1.6.2.2 pgoyette }
1425 1.6.2.2 pgoyette
1426 1.6.2.2 pgoyette svm_asidmap[oct] |= __BIT(bit);
1427 1.6.2.2 pgoyette vmcb->ctrl.guest_asid = i;
1428 1.6.2.2 pgoyette mutex_exit(&svm_asidlock);
1429 1.6.2.2 pgoyette return;
1430 1.6.2.2 pgoyette }
1431 1.6.2.2 pgoyette
1432 1.6.2.2 pgoyette /*
1433 1.6.2.2 pgoyette * No free ASID. Use the last one, which is shared and requires
1434 1.6.2.2 pgoyette * special TLB handling.
1435 1.6.2.2 pgoyette */
1436 1.6.2.2 pgoyette cpudata->shared_asid = true;
1437 1.6.2.2 pgoyette vmcb->ctrl.guest_asid = svm_maxasid - 1;
1438 1.6.2.2 pgoyette mutex_exit(&svm_asidlock);
1439 1.6.2.2 pgoyette }
1440 1.6.2.2 pgoyette
1441 1.6.2.2 pgoyette static void
1442 1.6.2.2 pgoyette svm_asid_free(struct nvmm_cpu *vcpu)
1443 1.6.2.2 pgoyette {
1444 1.6.2.2 pgoyette struct svm_cpudata *cpudata = vcpu->cpudata;
1445 1.6.2.2 pgoyette struct vmcb *vmcb = cpudata->vmcb;
1446 1.6.2.2 pgoyette size_t oct, bit;
1447 1.6.2.2 pgoyette
1448 1.6.2.2 pgoyette if (cpudata->shared_asid) {
1449 1.6.2.2 pgoyette return;
1450 1.6.2.2 pgoyette }
1451 1.6.2.2 pgoyette
1452 1.6.2.2 pgoyette oct = vmcb->ctrl.guest_asid / 8;
1453 1.6.2.2 pgoyette bit = vmcb->ctrl.guest_asid % 8;
1454 1.6.2.2 pgoyette
1455 1.6.2.2 pgoyette mutex_enter(&svm_asidlock);
1456 1.6.2.2 pgoyette svm_asidmap[oct] &= ~__BIT(bit);
1457 1.6.2.2 pgoyette mutex_exit(&svm_asidlock);
1458 1.6.2.2 pgoyette }
1459 1.6.2.2 pgoyette
1460 1.6.2.2 pgoyette static void
1461 1.6.2.2 pgoyette svm_vcpu_init(struct nvmm_machine *mach, struct nvmm_cpu *vcpu)
1462 1.6.2.2 pgoyette {
1463 1.6.2.2 pgoyette struct svm_cpudata *cpudata = vcpu->cpudata;
1464 1.6.2.2 pgoyette struct vmcb *vmcb = cpudata->vmcb;
1465 1.6.2.2 pgoyette
1466 1.6.2.2 pgoyette /* Allow reads/writes of Control Registers. */
1467 1.6.2.2 pgoyette vmcb->ctrl.intercept_cr = 0;
1468 1.6.2.2 pgoyette
1469 1.6.2.2 pgoyette /* Allow reads/writes of Debug Registers. */
1470 1.6.2.2 pgoyette vmcb->ctrl.intercept_dr = 0;
1471 1.6.2.2 pgoyette
1472 1.6.2.2 pgoyette /* Allow exceptions 0 to 31. */
1473 1.6.2.2 pgoyette vmcb->ctrl.intercept_vec = 0;
1474 1.6.2.2 pgoyette
1475 1.6.2.2 pgoyette /*
1476 1.6.2.2 pgoyette * Allow:
1477 1.6.2.2 pgoyette * - SMI [smm interrupts]
1478 1.6.2.2 pgoyette * - VINTR [virtual interrupts]
1479 1.6.2.2 pgoyette * - CR0_SPEC [CR0 writes changing other fields than CR0.TS or CR0.MP]
1480 1.6.2.2 pgoyette * - RIDTR [reads of IDTR]
1481 1.6.2.2 pgoyette * - RGDTR [reads of GDTR]
1482 1.6.2.2 pgoyette * - RLDTR [reads of LDTR]
1483 1.6.2.2 pgoyette * - RTR [reads of TR]
1484 1.6.2.2 pgoyette * - WIDTR [writes of IDTR]
1485 1.6.2.2 pgoyette * - WGDTR [writes of GDTR]
1486 1.6.2.2 pgoyette * - WLDTR [writes of LDTR]
1487 1.6.2.2 pgoyette * - WTR [writes of TR]
1488 1.6.2.2 pgoyette * - RDTSC [rdtsc instruction]
1489 1.6.2.2 pgoyette * - PUSHF [pushf instruction]
1490 1.6.2.2 pgoyette * - POPF [popf instruction]
1491 1.6.2.2 pgoyette * - IRET [iret instruction]
1492 1.6.2.2 pgoyette * - INTN [int $n instructions]
1493 1.6.2.2 pgoyette * - INVD [invd instruction]
1494 1.6.2.2 pgoyette * - PAUSE [pause instruction]
1495 1.6.2.2 pgoyette * - INVLPG [invplg instruction]
1496 1.6.2.2 pgoyette * - TASKSW [task switches]
1497 1.6.2.2 pgoyette *
1498 1.6.2.2 pgoyette * Intercept the rest below.
1499 1.6.2.2 pgoyette */
1500 1.6.2.2 pgoyette vmcb->ctrl.intercept_misc1 =
1501 1.6.2.2 pgoyette VMCB_CTRL_INTERCEPT_INTR |
1502 1.6.2.2 pgoyette VMCB_CTRL_INTERCEPT_NMI |
1503 1.6.2.2 pgoyette VMCB_CTRL_INTERCEPT_INIT |
1504 1.6.2.2 pgoyette VMCB_CTRL_INTERCEPT_RDPMC |
1505 1.6.2.2 pgoyette VMCB_CTRL_INTERCEPT_CPUID |
1506 1.6.2.2 pgoyette VMCB_CTRL_INTERCEPT_RSM |
1507 1.6.2.2 pgoyette VMCB_CTRL_INTERCEPT_HLT |
1508 1.6.2.2 pgoyette VMCB_CTRL_INTERCEPT_INVLPGA |
1509 1.6.2.2 pgoyette VMCB_CTRL_INTERCEPT_IOIO_PROT |
1510 1.6.2.2 pgoyette VMCB_CTRL_INTERCEPT_MSR_PROT |
1511 1.6.2.2 pgoyette VMCB_CTRL_INTERCEPT_FERR_FREEZE |
1512 1.6.2.2 pgoyette VMCB_CTRL_INTERCEPT_SHUTDOWN;
1513 1.6.2.2 pgoyette
1514 1.6.2.2 pgoyette /*
1515 1.6.2.2 pgoyette * Allow:
1516 1.6.2.2 pgoyette * - ICEBP [icebp instruction]
1517 1.6.2.2 pgoyette * - WBINVD [wbinvd instruction]
1518 1.6.2.2 pgoyette * - WCR_SPEC(0..15) [writes of CR0-15, received after instruction]
1519 1.6.2.2 pgoyette *
1520 1.6.2.2 pgoyette * Intercept the rest below.
1521 1.6.2.2 pgoyette */
1522 1.6.2.2 pgoyette vmcb->ctrl.intercept_misc2 =
1523 1.6.2.2 pgoyette VMCB_CTRL_INTERCEPT_VMRUN |
1524 1.6.2.2 pgoyette VMCB_CTRL_INTERCEPT_VMMCALL |
1525 1.6.2.2 pgoyette VMCB_CTRL_INTERCEPT_VMLOAD |
1526 1.6.2.2 pgoyette VMCB_CTRL_INTERCEPT_VMSAVE |
1527 1.6.2.2 pgoyette VMCB_CTRL_INTERCEPT_STGI |
1528 1.6.2.2 pgoyette VMCB_CTRL_INTERCEPT_CLGI |
1529 1.6.2.2 pgoyette VMCB_CTRL_INTERCEPT_SKINIT |
1530 1.6.2.2 pgoyette VMCB_CTRL_INTERCEPT_RDTSCP |
1531 1.6.2.2 pgoyette VMCB_CTRL_INTERCEPT_MONITOR |
1532 1.6.2.2 pgoyette VMCB_CTRL_INTERCEPT_MWAIT |
1533 1.6.2.2 pgoyette VMCB_CTRL_INTERCEPT_XSETBV;
1534 1.6.2.2 pgoyette
1535 1.6.2.2 pgoyette /* Intercept all I/O accesses. */
1536 1.6.2.2 pgoyette memset(cpudata->iobm, 0xFF, IOBM_SIZE);
1537 1.6.2.2 pgoyette vmcb->ctrl.iopm_base_pa = cpudata->iobm_pa;
1538 1.6.2.2 pgoyette
1539 1.6.2.2 pgoyette /*
1540 1.6.2.2 pgoyette * Allow:
1541 1.6.2.2 pgoyette * - EFER [read]
1542 1.6.2.2 pgoyette * - STAR [read, write]
1543 1.6.2.2 pgoyette * - LSTAR [read, write]
1544 1.6.2.2 pgoyette * - CSTAR [read, write]
1545 1.6.2.2 pgoyette * - SFMASK [read, write]
1546 1.6.2.2 pgoyette * - KERNELGSBASE [read, write]
1547 1.6.2.2 pgoyette * - SYSENTER_CS [read, write]
1548 1.6.2.2 pgoyette * - SYSENTER_ESP [read, write]
1549 1.6.2.2 pgoyette * - SYSENTER_EIP [read, write]
1550 1.6.2.2 pgoyette * - FSBASE [read, write]
1551 1.6.2.2 pgoyette * - GSBASE [read, write]
1552 1.6.2.4 pgoyette * - TSC [read]
1553 1.6.2.2 pgoyette *
1554 1.6.2.2 pgoyette * Intercept the rest.
1555 1.6.2.2 pgoyette */
1556 1.6.2.2 pgoyette memset(cpudata->msrbm, 0xFF, MSRBM_SIZE);
1557 1.6.2.2 pgoyette svm_vcpu_msr_allow(cpudata->msrbm, MSR_EFER, true, false);
1558 1.6.2.2 pgoyette svm_vcpu_msr_allow(cpudata->msrbm, MSR_STAR, true, true);
1559 1.6.2.2 pgoyette svm_vcpu_msr_allow(cpudata->msrbm, MSR_LSTAR, true, true);
1560 1.6.2.2 pgoyette svm_vcpu_msr_allow(cpudata->msrbm, MSR_CSTAR, true, true);
1561 1.6.2.2 pgoyette svm_vcpu_msr_allow(cpudata->msrbm, MSR_SFMASK, true, true);
1562 1.6.2.2 pgoyette svm_vcpu_msr_allow(cpudata->msrbm, MSR_KERNELGSBASE, true, true);
1563 1.6.2.2 pgoyette svm_vcpu_msr_allow(cpudata->msrbm, MSR_SYSENTER_CS, true, true);
1564 1.6.2.2 pgoyette svm_vcpu_msr_allow(cpudata->msrbm, MSR_SYSENTER_ESP, true, true);
1565 1.6.2.2 pgoyette svm_vcpu_msr_allow(cpudata->msrbm, MSR_SYSENTER_EIP, true, true);
1566 1.6.2.2 pgoyette svm_vcpu_msr_allow(cpudata->msrbm, MSR_FSBASE, true, true);
1567 1.6.2.2 pgoyette svm_vcpu_msr_allow(cpudata->msrbm, MSR_GSBASE, true, true);
1568 1.6.2.4 pgoyette svm_vcpu_msr_allow(cpudata->msrbm, MSR_TSC, true, false);
1569 1.6.2.2 pgoyette vmcb->ctrl.msrpm_base_pa = cpudata->msrbm_pa;
1570 1.6.2.2 pgoyette
1571 1.6.2.2 pgoyette /* Generate ASID. */
1572 1.6.2.2 pgoyette svm_asid_alloc(vcpu);
1573 1.6.2.2 pgoyette
1574 1.6.2.2 pgoyette /* Virtual TPR. */
1575 1.6.2.2 pgoyette vmcb->ctrl.v = VMCB_CTRL_V_INTR_MASKING;
1576 1.6.2.2 pgoyette
1577 1.6.2.2 pgoyette /* Enable Nested Paging. */
1578 1.6.2.2 pgoyette vmcb->ctrl.enable1 = VMCB_CTRL_ENABLE_NP;
1579 1.6.2.2 pgoyette vmcb->ctrl.n_cr3 = mach->vm->vm_map.pmap->pm_pdirpa[0];
1580 1.6.2.2 pgoyette
1581 1.6.2.2 pgoyette /* Must always be set. */
1582 1.6.2.2 pgoyette vmcb->state.efer = EFER_SVME;
1583 1.6.2.2 pgoyette
1584 1.6.2.2 pgoyette /* Init XSAVE header. */
1585 1.6.2.2 pgoyette cpudata->gfpu.xsh_xstate_bv = svm_xcr0_mask;
1586 1.6.2.2 pgoyette cpudata->gfpu.xsh_xcomp_bv = 0;
1587 1.6.2.2 pgoyette
1588 1.6.2.2 pgoyette /* Bluntly hide the host TSC. */
1589 1.6.2.2 pgoyette cpudata->tsc_offset = rdtsc();
1590 1.6.2.2 pgoyette }
1591 1.6.2.2 pgoyette
1592 1.6.2.2 pgoyette static int
1593 1.6.2.2 pgoyette svm_vcpu_create(struct nvmm_machine *mach, struct nvmm_cpu *vcpu)
1594 1.6.2.2 pgoyette {
1595 1.6.2.2 pgoyette struct svm_cpudata *cpudata;
1596 1.6.2.2 pgoyette int error;
1597 1.6.2.2 pgoyette
1598 1.6.2.2 pgoyette /* Allocate the SVM cpudata. */
1599 1.6.2.2 pgoyette cpudata = (struct svm_cpudata *)uvm_km_alloc(kernel_map,
1600 1.6.2.2 pgoyette roundup(sizeof(*cpudata), PAGE_SIZE), 0,
1601 1.6.2.2 pgoyette UVM_KMF_WIRED|UVM_KMF_ZERO);
1602 1.6.2.2 pgoyette vcpu->cpudata = cpudata;
1603 1.6.2.2 pgoyette
1604 1.6.2.2 pgoyette /* VMCB */
1605 1.6.2.2 pgoyette error = svm_memalloc(&cpudata->vmcb_pa, (vaddr_t *)&cpudata->vmcb,
1606 1.6.2.2 pgoyette VMCB_NPAGES);
1607 1.6.2.2 pgoyette if (error)
1608 1.6.2.2 pgoyette goto error;
1609 1.6.2.2 pgoyette
1610 1.6.2.2 pgoyette /* I/O Bitmap */
1611 1.6.2.2 pgoyette error = svm_memalloc(&cpudata->iobm_pa, (vaddr_t *)&cpudata->iobm,
1612 1.6.2.2 pgoyette IOBM_NPAGES);
1613 1.6.2.2 pgoyette if (error)
1614 1.6.2.2 pgoyette goto error;
1615 1.6.2.2 pgoyette
1616 1.6.2.2 pgoyette /* MSR Bitmap */
1617 1.6.2.2 pgoyette error = svm_memalloc(&cpudata->msrbm_pa, (vaddr_t *)&cpudata->msrbm,
1618 1.6.2.2 pgoyette MSRBM_NPAGES);
1619 1.6.2.2 pgoyette if (error)
1620 1.6.2.2 pgoyette goto error;
1621 1.6.2.2 pgoyette
1622 1.6.2.2 pgoyette /* Init the VCPU info. */
1623 1.6.2.2 pgoyette svm_vcpu_init(mach, vcpu);
1624 1.6.2.2 pgoyette
1625 1.6.2.2 pgoyette return 0;
1626 1.6.2.2 pgoyette
1627 1.6.2.2 pgoyette error:
1628 1.6.2.2 pgoyette if (cpudata->vmcb_pa) {
1629 1.6.2.2 pgoyette svm_memfree(cpudata->vmcb_pa, (vaddr_t)cpudata->vmcb,
1630 1.6.2.2 pgoyette VMCB_NPAGES);
1631 1.6.2.2 pgoyette }
1632 1.6.2.2 pgoyette if (cpudata->iobm_pa) {
1633 1.6.2.2 pgoyette svm_memfree(cpudata->iobm_pa, (vaddr_t)cpudata->iobm,
1634 1.6.2.2 pgoyette IOBM_NPAGES);
1635 1.6.2.2 pgoyette }
1636 1.6.2.2 pgoyette if (cpudata->msrbm_pa) {
1637 1.6.2.2 pgoyette svm_memfree(cpudata->msrbm_pa, (vaddr_t)cpudata->msrbm,
1638 1.6.2.2 pgoyette MSRBM_NPAGES);
1639 1.6.2.2 pgoyette }
1640 1.6.2.2 pgoyette uvm_km_free(kernel_map, (vaddr_t)cpudata,
1641 1.6.2.2 pgoyette roundup(sizeof(*cpudata), PAGE_SIZE), UVM_KMF_WIRED);
1642 1.6.2.2 pgoyette return error;
1643 1.6.2.2 pgoyette }
1644 1.6.2.2 pgoyette
1645 1.6.2.2 pgoyette static void
1646 1.6.2.2 pgoyette svm_vcpu_destroy(struct nvmm_machine *mach, struct nvmm_cpu *vcpu)
1647 1.6.2.2 pgoyette {
1648 1.6.2.2 pgoyette struct svm_cpudata *cpudata = vcpu->cpudata;
1649 1.6.2.2 pgoyette
1650 1.6.2.2 pgoyette svm_asid_free(vcpu);
1651 1.6.2.2 pgoyette
1652 1.6.2.2 pgoyette svm_memfree(cpudata->vmcb_pa, (vaddr_t)cpudata->vmcb, VMCB_NPAGES);
1653 1.6.2.2 pgoyette svm_memfree(cpudata->iobm_pa, (vaddr_t)cpudata->iobm, IOBM_NPAGES);
1654 1.6.2.2 pgoyette svm_memfree(cpudata->msrbm_pa, (vaddr_t)cpudata->msrbm, MSRBM_NPAGES);
1655 1.6.2.2 pgoyette
1656 1.6.2.2 pgoyette uvm_km_free(kernel_map, (vaddr_t)cpudata,
1657 1.6.2.2 pgoyette roundup(sizeof(*cpudata), PAGE_SIZE), UVM_KMF_WIRED);
1658 1.6.2.2 pgoyette }
1659 1.6.2.2 pgoyette
1660 1.6.2.2 pgoyette #define SVM_SEG_ATTRIB_TYPE __BITS(4,0)
1661 1.6.2.2 pgoyette #define SVM_SEG_ATTRIB_DPL __BITS(6,5)
1662 1.6.2.2 pgoyette #define SVM_SEG_ATTRIB_P __BIT(7)
1663 1.6.2.2 pgoyette #define SVM_SEG_ATTRIB_AVL __BIT(8)
1664 1.6.2.2 pgoyette #define SVM_SEG_ATTRIB_LONG __BIT(9)
1665 1.6.2.2 pgoyette #define SVM_SEG_ATTRIB_DEF32 __BIT(10)
1666 1.6.2.2 pgoyette #define SVM_SEG_ATTRIB_GRAN __BIT(11)
1667 1.6.2.2 pgoyette
1668 1.6.2.2 pgoyette static void
1669 1.6.2.2 pgoyette svm_vcpu_setstate_seg(struct nvmm_x64_state_seg *seg, struct vmcb_segment *vseg)
1670 1.6.2.2 pgoyette {
1671 1.6.2.2 pgoyette vseg->selector = seg->selector;
1672 1.6.2.2 pgoyette vseg->attrib =
1673 1.6.2.2 pgoyette __SHIFTIN(seg->attrib.type, SVM_SEG_ATTRIB_TYPE) |
1674 1.6.2.2 pgoyette __SHIFTIN(seg->attrib.dpl, SVM_SEG_ATTRIB_DPL) |
1675 1.6.2.2 pgoyette __SHIFTIN(seg->attrib.p, SVM_SEG_ATTRIB_P) |
1676 1.6.2.2 pgoyette __SHIFTIN(seg->attrib.avl, SVM_SEG_ATTRIB_AVL) |
1677 1.6.2.2 pgoyette __SHIFTIN(seg->attrib.lng, SVM_SEG_ATTRIB_LONG) |
1678 1.6.2.2 pgoyette __SHIFTIN(seg->attrib.def32, SVM_SEG_ATTRIB_DEF32) |
1679 1.6.2.2 pgoyette __SHIFTIN(seg->attrib.gran, SVM_SEG_ATTRIB_GRAN);
1680 1.6.2.2 pgoyette vseg->limit = seg->limit;
1681 1.6.2.2 pgoyette vseg->base = seg->base;
1682 1.6.2.2 pgoyette }
1683 1.6.2.2 pgoyette
1684 1.6.2.2 pgoyette static void
1685 1.6.2.2 pgoyette svm_vcpu_getstate_seg(struct nvmm_x64_state_seg *seg, struct vmcb_segment *vseg)
1686 1.6.2.2 pgoyette {
1687 1.6.2.2 pgoyette seg->selector = vseg->selector;
1688 1.6.2.2 pgoyette seg->attrib.type = __SHIFTOUT(vseg->attrib, SVM_SEG_ATTRIB_TYPE);
1689 1.6.2.2 pgoyette seg->attrib.dpl = __SHIFTOUT(vseg->attrib, SVM_SEG_ATTRIB_DPL);
1690 1.6.2.2 pgoyette seg->attrib.p = __SHIFTOUT(vseg->attrib, SVM_SEG_ATTRIB_P);
1691 1.6.2.2 pgoyette seg->attrib.avl = __SHIFTOUT(vseg->attrib, SVM_SEG_ATTRIB_AVL);
1692 1.6.2.2 pgoyette seg->attrib.lng = __SHIFTOUT(vseg->attrib, SVM_SEG_ATTRIB_LONG);
1693 1.6.2.2 pgoyette seg->attrib.def32 = __SHIFTOUT(vseg->attrib, SVM_SEG_ATTRIB_DEF32);
1694 1.6.2.2 pgoyette seg->attrib.gran = __SHIFTOUT(vseg->attrib, SVM_SEG_ATTRIB_GRAN);
1695 1.6.2.2 pgoyette seg->limit = vseg->limit;
1696 1.6.2.2 pgoyette seg->base = vseg->base;
1697 1.6.2.2 pgoyette }
1698 1.6.2.2 pgoyette
1699 1.6.2.4 pgoyette static inline bool
1700 1.6.2.4 pgoyette svm_state_tlb_flush(struct vmcb *vmcb, struct nvmm_x64_state *state,
1701 1.6.2.4 pgoyette uint64_t flags)
1702 1.6.2.2 pgoyette {
1703 1.6.2.2 pgoyette if (flags & NVMM_X64_STATE_CRS) {
1704 1.6.2.4 pgoyette if ((vmcb->state.cr0 ^
1705 1.6.2.4 pgoyette state->crs[NVMM_X64_CR_CR0]) & CR0_TLB_FLUSH) {
1706 1.6.2.2 pgoyette return true;
1707 1.6.2.2 pgoyette }
1708 1.6.2.4 pgoyette if (vmcb->state.cr3 != state->crs[NVMM_X64_CR_CR3]) {
1709 1.6.2.2 pgoyette return true;
1710 1.6.2.2 pgoyette }
1711 1.6.2.4 pgoyette if ((vmcb->state.cr4 ^
1712 1.6.2.4 pgoyette state->crs[NVMM_X64_CR_CR4]) & CR4_TLB_FLUSH) {
1713 1.6.2.2 pgoyette return true;
1714 1.6.2.2 pgoyette }
1715 1.6.2.2 pgoyette }
1716 1.6.2.2 pgoyette
1717 1.6.2.2 pgoyette if (flags & NVMM_X64_STATE_MSRS) {
1718 1.6.2.4 pgoyette if ((vmcb->state.efer ^
1719 1.6.2.4 pgoyette state->msrs[NVMM_X64_MSR_EFER]) & EFER_TLB_FLUSH) {
1720 1.6.2.2 pgoyette return true;
1721 1.6.2.2 pgoyette }
1722 1.6.2.2 pgoyette }
1723 1.6.2.2 pgoyette
1724 1.6.2.2 pgoyette return false;
1725 1.6.2.2 pgoyette }
1726 1.6.2.2 pgoyette
1727 1.6.2.2 pgoyette static void
1728 1.6.2.2 pgoyette svm_vcpu_setstate(struct nvmm_cpu *vcpu, void *data, uint64_t flags)
1729 1.6.2.2 pgoyette {
1730 1.6.2.4 pgoyette struct nvmm_x64_state *state = (struct nvmm_x64_state *)data;
1731 1.6.2.2 pgoyette struct svm_cpudata *cpudata = vcpu->cpudata;
1732 1.6.2.2 pgoyette struct vmcb *vmcb = cpudata->vmcb;
1733 1.6.2.2 pgoyette struct fxsave *fpustate;
1734 1.6.2.2 pgoyette
1735 1.6.2.4 pgoyette if (svm_state_tlb_flush(vmcb, state, flags)) {
1736 1.6.2.2 pgoyette cpudata->tlb_want_flush = true;
1737 1.6.2.2 pgoyette }
1738 1.6.2.2 pgoyette
1739 1.6.2.2 pgoyette if (flags & NVMM_X64_STATE_SEGS) {
1740 1.6.2.4 pgoyette svm_vcpu_setstate_seg(&state->segs[NVMM_X64_SEG_CS],
1741 1.6.2.2 pgoyette &vmcb->state.cs);
1742 1.6.2.4 pgoyette svm_vcpu_setstate_seg(&state->segs[NVMM_X64_SEG_DS],
1743 1.6.2.2 pgoyette &vmcb->state.ds);
1744 1.6.2.4 pgoyette svm_vcpu_setstate_seg(&state->segs[NVMM_X64_SEG_ES],
1745 1.6.2.2 pgoyette &vmcb->state.es);
1746 1.6.2.4 pgoyette svm_vcpu_setstate_seg(&state->segs[NVMM_X64_SEG_FS],
1747 1.6.2.2 pgoyette &vmcb->state.fs);
1748 1.6.2.4 pgoyette svm_vcpu_setstate_seg(&state->segs[NVMM_X64_SEG_GS],
1749 1.6.2.2 pgoyette &vmcb->state.gs);
1750 1.6.2.4 pgoyette svm_vcpu_setstate_seg(&state->segs[NVMM_X64_SEG_SS],
1751 1.6.2.2 pgoyette &vmcb->state.ss);
1752 1.6.2.4 pgoyette svm_vcpu_setstate_seg(&state->segs[NVMM_X64_SEG_GDT],
1753 1.6.2.2 pgoyette &vmcb->state.gdt);
1754 1.6.2.4 pgoyette svm_vcpu_setstate_seg(&state->segs[NVMM_X64_SEG_IDT],
1755 1.6.2.2 pgoyette &vmcb->state.idt);
1756 1.6.2.4 pgoyette svm_vcpu_setstate_seg(&state->segs[NVMM_X64_SEG_LDT],
1757 1.6.2.2 pgoyette &vmcb->state.ldt);
1758 1.6.2.4 pgoyette svm_vcpu_setstate_seg(&state->segs[NVMM_X64_SEG_TR],
1759 1.6.2.2 pgoyette &vmcb->state.tr);
1760 1.6.2.2 pgoyette }
1761 1.6.2.2 pgoyette
1762 1.6.2.4 pgoyette CTASSERT(sizeof(cpudata->gprs) == sizeof(state->gprs));
1763 1.6.2.2 pgoyette if (flags & NVMM_X64_STATE_GPRS) {
1764 1.6.2.4 pgoyette memcpy(cpudata->gprs, state->gprs, sizeof(state->gprs));
1765 1.6.2.2 pgoyette
1766 1.6.2.4 pgoyette vmcb->state.rip = state->gprs[NVMM_X64_GPR_RIP];
1767 1.6.2.4 pgoyette vmcb->state.rsp = state->gprs[NVMM_X64_GPR_RSP];
1768 1.6.2.4 pgoyette vmcb->state.rax = state->gprs[NVMM_X64_GPR_RAX];
1769 1.6.2.4 pgoyette vmcb->state.rflags = state->gprs[NVMM_X64_GPR_RFLAGS];
1770 1.6.2.2 pgoyette }
1771 1.6.2.2 pgoyette
1772 1.6.2.2 pgoyette if (flags & NVMM_X64_STATE_CRS) {
1773 1.6.2.4 pgoyette vmcb->state.cr0 = state->crs[NVMM_X64_CR_CR0];
1774 1.6.2.4 pgoyette vmcb->state.cr2 = state->crs[NVMM_X64_CR_CR2];
1775 1.6.2.4 pgoyette vmcb->state.cr3 = state->crs[NVMM_X64_CR_CR3];
1776 1.6.2.4 pgoyette vmcb->state.cr4 = state->crs[NVMM_X64_CR_CR4];
1777 1.6.2.2 pgoyette
1778 1.6.2.2 pgoyette vmcb->ctrl.v &= ~VMCB_CTRL_V_TPR;
1779 1.6.2.4 pgoyette vmcb->ctrl.v |= __SHIFTIN(state->crs[NVMM_X64_CR_CR8],
1780 1.6.2.2 pgoyette VMCB_CTRL_V_TPR);
1781 1.6.2.2 pgoyette
1782 1.6.2.2 pgoyette /* Clear unsupported XCR0 bits, set mandatory X87 bit. */
1783 1.6.2.2 pgoyette if (svm_xcr0_mask != 0) {
1784 1.6.2.4 pgoyette cpudata->gxcr0 = state->crs[NVMM_X64_CR_XCR0];
1785 1.6.2.4 pgoyette cpudata->gxcr0 &= svm_xcr0_mask;
1786 1.6.2.4 pgoyette cpudata->gxcr0 |= XCR0_X87;
1787 1.6.2.2 pgoyette } else {
1788 1.6.2.4 pgoyette cpudata->gxcr0 = 0;
1789 1.6.2.2 pgoyette }
1790 1.6.2.2 pgoyette }
1791 1.6.2.2 pgoyette
1792 1.6.2.4 pgoyette CTASSERT(sizeof(cpudata->drs) == sizeof(state->drs));
1793 1.6.2.2 pgoyette if (flags & NVMM_X64_STATE_DRS) {
1794 1.6.2.4 pgoyette memcpy(cpudata->drs, state->drs, sizeof(state->drs));
1795 1.6.2.2 pgoyette
1796 1.6.2.4 pgoyette vmcb->state.dr6 = state->drs[NVMM_X64_DR_DR6];
1797 1.6.2.4 pgoyette vmcb->state.dr7 = state->drs[NVMM_X64_DR_DR7];
1798 1.6.2.2 pgoyette }
1799 1.6.2.2 pgoyette
1800 1.6.2.2 pgoyette if (flags & NVMM_X64_STATE_MSRS) {
1801 1.6.2.2 pgoyette /* Bit EFER_SVME is mandatory. */
1802 1.6.2.4 pgoyette vmcb->state.efer = state->msrs[NVMM_X64_MSR_EFER] | EFER_SVME;
1803 1.6.2.2 pgoyette
1804 1.6.2.4 pgoyette vmcb->state.star = state->msrs[NVMM_X64_MSR_STAR];
1805 1.6.2.4 pgoyette vmcb->state.lstar = state->msrs[NVMM_X64_MSR_LSTAR];
1806 1.6.2.4 pgoyette vmcb->state.cstar = state->msrs[NVMM_X64_MSR_CSTAR];
1807 1.6.2.4 pgoyette vmcb->state.sfmask = state->msrs[NVMM_X64_MSR_SFMASK];
1808 1.6.2.2 pgoyette vmcb->state.kernelgsbase =
1809 1.6.2.4 pgoyette state->msrs[NVMM_X64_MSR_KERNELGSBASE];
1810 1.6.2.2 pgoyette vmcb->state.sysenter_cs =
1811 1.6.2.4 pgoyette state->msrs[NVMM_X64_MSR_SYSENTER_CS];
1812 1.6.2.2 pgoyette vmcb->state.sysenter_esp =
1813 1.6.2.4 pgoyette state->msrs[NVMM_X64_MSR_SYSENTER_ESP];
1814 1.6.2.2 pgoyette vmcb->state.sysenter_eip =
1815 1.6.2.4 pgoyette state->msrs[NVMM_X64_MSR_SYSENTER_EIP];
1816 1.6.2.4 pgoyette vmcb->state.g_pat = state->msrs[NVMM_X64_MSR_PAT];
1817 1.6.2.2 pgoyette }
1818 1.6.2.2 pgoyette
1819 1.6.2.2 pgoyette if (flags & NVMM_X64_STATE_MISC) {
1820 1.6.2.4 pgoyette vmcb->state.cpl = state->misc[NVMM_X64_MISC_CPL];
1821 1.6.2.2 pgoyette
1822 1.6.2.4 pgoyette if (state->misc[NVMM_X64_MISC_INT_SHADOW]) {
1823 1.6.2.4 pgoyette vmcb->ctrl.intr |= VMCB_CTRL_INTR_SHADOW;
1824 1.6.2.4 pgoyette } else {
1825 1.6.2.4 pgoyette vmcb->ctrl.intr &= ~VMCB_CTRL_INTR_SHADOW;
1826 1.6.2.4 pgoyette }
1827 1.6.2.4 pgoyette
1828 1.6.2.4 pgoyette if (state->misc[NVMM_X64_MISC_INT_WINDOW_EXIT]) {
1829 1.6.2.4 pgoyette svm_event_waitexit_enable(vcpu, false);
1830 1.6.2.4 pgoyette } else {
1831 1.6.2.4 pgoyette svm_event_waitexit_disable(vcpu, false);
1832 1.6.2.4 pgoyette }
1833 1.6.2.4 pgoyette
1834 1.6.2.4 pgoyette if (state->misc[NVMM_X64_MISC_NMI_WINDOW_EXIT]) {
1835 1.6.2.4 pgoyette svm_event_waitexit_enable(vcpu, true);
1836 1.6.2.4 pgoyette } else {
1837 1.6.2.4 pgoyette svm_event_waitexit_disable(vcpu, true);
1838 1.6.2.4 pgoyette }
1839 1.6.2.2 pgoyette }
1840 1.6.2.2 pgoyette
1841 1.6.2.4 pgoyette CTASSERT(sizeof(cpudata->gfpu.xsh_fxsave) == sizeof(state->fpu));
1842 1.6.2.2 pgoyette if (flags & NVMM_X64_STATE_FPU) {
1843 1.6.2.4 pgoyette memcpy(cpudata->gfpu.xsh_fxsave, &state->fpu,
1844 1.6.2.4 pgoyette sizeof(state->fpu));
1845 1.6.2.2 pgoyette
1846 1.6.2.2 pgoyette fpustate = (struct fxsave *)cpudata->gfpu.xsh_fxsave;
1847 1.6.2.2 pgoyette fpustate->fx_mxcsr_mask &= x86_fpu_mxcsr_mask;
1848 1.6.2.2 pgoyette fpustate->fx_mxcsr &= fpustate->fx_mxcsr_mask;
1849 1.6.2.2 pgoyette }
1850 1.6.2.4 pgoyette
1851 1.6.2.4 pgoyette svm_vmcb_cache_update(vmcb, flags);
1852 1.6.2.2 pgoyette }
1853 1.6.2.2 pgoyette
1854 1.6.2.2 pgoyette static void
1855 1.6.2.2 pgoyette svm_vcpu_getstate(struct nvmm_cpu *vcpu, void *data, uint64_t flags)
1856 1.6.2.2 pgoyette {
1857 1.6.2.4 pgoyette struct nvmm_x64_state *state = (struct nvmm_x64_state *)data;
1858 1.6.2.2 pgoyette struct svm_cpudata *cpudata = vcpu->cpudata;
1859 1.6.2.2 pgoyette struct vmcb *vmcb = cpudata->vmcb;
1860 1.6.2.2 pgoyette
1861 1.6.2.2 pgoyette if (flags & NVMM_X64_STATE_SEGS) {
1862 1.6.2.4 pgoyette svm_vcpu_getstate_seg(&state->segs[NVMM_X64_SEG_CS],
1863 1.6.2.2 pgoyette &vmcb->state.cs);
1864 1.6.2.4 pgoyette svm_vcpu_getstate_seg(&state->segs[NVMM_X64_SEG_DS],
1865 1.6.2.2 pgoyette &vmcb->state.ds);
1866 1.6.2.4 pgoyette svm_vcpu_getstate_seg(&state->segs[NVMM_X64_SEG_ES],
1867 1.6.2.2 pgoyette &vmcb->state.es);
1868 1.6.2.4 pgoyette svm_vcpu_getstate_seg(&state->segs[NVMM_X64_SEG_FS],
1869 1.6.2.2 pgoyette &vmcb->state.fs);
1870 1.6.2.4 pgoyette svm_vcpu_getstate_seg(&state->segs[NVMM_X64_SEG_GS],
1871 1.6.2.2 pgoyette &vmcb->state.gs);
1872 1.6.2.4 pgoyette svm_vcpu_getstate_seg(&state->segs[NVMM_X64_SEG_SS],
1873 1.6.2.2 pgoyette &vmcb->state.ss);
1874 1.6.2.4 pgoyette svm_vcpu_getstate_seg(&state->segs[NVMM_X64_SEG_GDT],
1875 1.6.2.2 pgoyette &vmcb->state.gdt);
1876 1.6.2.4 pgoyette svm_vcpu_getstate_seg(&state->segs[NVMM_X64_SEG_IDT],
1877 1.6.2.2 pgoyette &vmcb->state.idt);
1878 1.6.2.4 pgoyette svm_vcpu_getstate_seg(&state->segs[NVMM_X64_SEG_LDT],
1879 1.6.2.2 pgoyette &vmcb->state.ldt);
1880 1.6.2.4 pgoyette svm_vcpu_getstate_seg(&state->segs[NVMM_X64_SEG_TR],
1881 1.6.2.2 pgoyette &vmcb->state.tr);
1882 1.6.2.2 pgoyette }
1883 1.6.2.2 pgoyette
1884 1.6.2.4 pgoyette CTASSERT(sizeof(cpudata->gprs) == sizeof(state->gprs));
1885 1.6.2.2 pgoyette if (flags & NVMM_X64_STATE_GPRS) {
1886 1.6.2.4 pgoyette memcpy(state->gprs, cpudata->gprs, sizeof(state->gprs));
1887 1.6.2.2 pgoyette
1888 1.6.2.4 pgoyette state->gprs[NVMM_X64_GPR_RIP] = vmcb->state.rip;
1889 1.6.2.4 pgoyette state->gprs[NVMM_X64_GPR_RSP] = vmcb->state.rsp;
1890 1.6.2.4 pgoyette state->gprs[NVMM_X64_GPR_RAX] = vmcb->state.rax;
1891 1.6.2.4 pgoyette state->gprs[NVMM_X64_GPR_RFLAGS] = vmcb->state.rflags;
1892 1.6.2.2 pgoyette }
1893 1.6.2.2 pgoyette
1894 1.6.2.2 pgoyette if (flags & NVMM_X64_STATE_CRS) {
1895 1.6.2.4 pgoyette state->crs[NVMM_X64_CR_CR0] = vmcb->state.cr0;
1896 1.6.2.4 pgoyette state->crs[NVMM_X64_CR_CR2] = vmcb->state.cr2;
1897 1.6.2.4 pgoyette state->crs[NVMM_X64_CR_CR3] = vmcb->state.cr3;
1898 1.6.2.4 pgoyette state->crs[NVMM_X64_CR_CR4] = vmcb->state.cr4;
1899 1.6.2.4 pgoyette state->crs[NVMM_X64_CR_CR8] = __SHIFTOUT(vmcb->ctrl.v,
1900 1.6.2.2 pgoyette VMCB_CTRL_V_TPR);
1901 1.6.2.4 pgoyette state->crs[NVMM_X64_CR_XCR0] = cpudata->gxcr0;
1902 1.6.2.2 pgoyette }
1903 1.6.2.2 pgoyette
1904 1.6.2.4 pgoyette CTASSERT(sizeof(cpudata->drs) == sizeof(state->drs));
1905 1.6.2.2 pgoyette if (flags & NVMM_X64_STATE_DRS) {
1906 1.6.2.4 pgoyette memcpy(state->drs, cpudata->drs, sizeof(state->drs));
1907 1.6.2.2 pgoyette
1908 1.6.2.4 pgoyette state->drs[NVMM_X64_DR_DR6] = vmcb->state.dr6;
1909 1.6.2.4 pgoyette state->drs[NVMM_X64_DR_DR7] = vmcb->state.dr7;
1910 1.6.2.2 pgoyette }
1911 1.6.2.2 pgoyette
1912 1.6.2.2 pgoyette if (flags & NVMM_X64_STATE_MSRS) {
1913 1.6.2.4 pgoyette state->msrs[NVMM_X64_MSR_EFER] = vmcb->state.efer;
1914 1.6.2.4 pgoyette state->msrs[NVMM_X64_MSR_STAR] = vmcb->state.star;
1915 1.6.2.4 pgoyette state->msrs[NVMM_X64_MSR_LSTAR] = vmcb->state.lstar;
1916 1.6.2.4 pgoyette state->msrs[NVMM_X64_MSR_CSTAR] = vmcb->state.cstar;
1917 1.6.2.4 pgoyette state->msrs[NVMM_X64_MSR_SFMASK] = vmcb->state.sfmask;
1918 1.6.2.4 pgoyette state->msrs[NVMM_X64_MSR_KERNELGSBASE] =
1919 1.6.2.2 pgoyette vmcb->state.kernelgsbase;
1920 1.6.2.4 pgoyette state->msrs[NVMM_X64_MSR_SYSENTER_CS] =
1921 1.6.2.2 pgoyette vmcb->state.sysenter_cs;
1922 1.6.2.4 pgoyette state->msrs[NVMM_X64_MSR_SYSENTER_ESP] =
1923 1.6.2.2 pgoyette vmcb->state.sysenter_esp;
1924 1.6.2.4 pgoyette state->msrs[NVMM_X64_MSR_SYSENTER_EIP] =
1925 1.6.2.2 pgoyette vmcb->state.sysenter_eip;
1926 1.6.2.4 pgoyette state->msrs[NVMM_X64_MSR_PAT] = vmcb->state.g_pat;
1927 1.6.2.2 pgoyette
1928 1.6.2.2 pgoyette /* Hide SVME. */
1929 1.6.2.4 pgoyette state->msrs[NVMM_X64_MSR_EFER] &= ~EFER_SVME;
1930 1.6.2.2 pgoyette }
1931 1.6.2.2 pgoyette
1932 1.6.2.2 pgoyette if (flags & NVMM_X64_STATE_MISC) {
1933 1.6.2.4 pgoyette state->misc[NVMM_X64_MISC_CPL] = vmcb->state.cpl;
1934 1.6.2.2 pgoyette
1935 1.6.2.4 pgoyette state->misc[NVMM_X64_MISC_INT_SHADOW] =
1936 1.6.2.4 pgoyette (vmcb->ctrl.intr & VMCB_CTRL_INTR_SHADOW) != 0;
1937 1.6.2.4 pgoyette state->misc[NVMM_X64_MISC_INT_WINDOW_EXIT] =
1938 1.6.2.4 pgoyette cpudata->int_window_exit;
1939 1.6.2.4 pgoyette state->misc[NVMM_X64_MISC_NMI_WINDOW_EXIT] =
1940 1.6.2.4 pgoyette cpudata->nmi_window_exit;
1941 1.6.2.2 pgoyette }
1942 1.6.2.2 pgoyette
1943 1.6.2.4 pgoyette CTASSERT(sizeof(cpudata->gfpu.xsh_fxsave) == sizeof(state->fpu));
1944 1.6.2.2 pgoyette if (flags & NVMM_X64_STATE_FPU) {
1945 1.6.2.4 pgoyette memcpy(&state->fpu, cpudata->gfpu.xsh_fxsave,
1946 1.6.2.4 pgoyette sizeof(state->fpu));
1947 1.6.2.2 pgoyette }
1948 1.6.2.2 pgoyette }
1949 1.6.2.2 pgoyette
1950 1.6.2.2 pgoyette /* -------------------------------------------------------------------------- */
1951 1.6.2.2 pgoyette
1952 1.6.2.2 pgoyette static void
1953 1.6.2.2 pgoyette svm_tlb_flush(struct pmap *pm)
1954 1.6.2.2 pgoyette {
1955 1.6.2.2 pgoyette struct nvmm_machine *mach = pm->pm_data;
1956 1.6.2.2 pgoyette struct svm_cpudata *cpudata;
1957 1.6.2.2 pgoyette struct nvmm_cpu *vcpu;
1958 1.6.2.2 pgoyette int error;
1959 1.6.2.2 pgoyette size_t i;
1960 1.6.2.2 pgoyette
1961 1.6.2.2 pgoyette /* Request TLB flushes. */
1962 1.6.2.2 pgoyette for (i = 0; i < NVMM_MAX_VCPUS; i++) {
1963 1.6.2.2 pgoyette error = nvmm_vcpu_get(mach, i, &vcpu);
1964 1.6.2.2 pgoyette if (error)
1965 1.6.2.2 pgoyette continue;
1966 1.6.2.2 pgoyette cpudata = vcpu->cpudata;
1967 1.6.2.2 pgoyette cpudata->tlb_want_flush = true;
1968 1.6.2.2 pgoyette nvmm_vcpu_put(vcpu);
1969 1.6.2.2 pgoyette }
1970 1.6.2.2 pgoyette }
1971 1.6.2.2 pgoyette
1972 1.6.2.2 pgoyette static void
1973 1.6.2.2 pgoyette svm_machine_create(struct nvmm_machine *mach)
1974 1.6.2.2 pgoyette {
1975 1.6.2.2 pgoyette /* Fill in pmap info. */
1976 1.6.2.2 pgoyette mach->vm->vm_map.pmap->pm_data = (void *)mach;
1977 1.6.2.2 pgoyette mach->vm->vm_map.pmap->pm_tlb_flush = svm_tlb_flush;
1978 1.6.2.2 pgoyette
1979 1.6.2.2 pgoyette mach->machdata = kmem_zalloc(sizeof(struct svm_machdata), KM_SLEEP);
1980 1.6.2.2 pgoyette }
1981 1.6.2.2 pgoyette
1982 1.6.2.2 pgoyette static void
1983 1.6.2.2 pgoyette svm_machine_destroy(struct nvmm_machine *mach)
1984 1.6.2.2 pgoyette {
1985 1.6.2.2 pgoyette kmem_free(mach->machdata, sizeof(struct svm_machdata));
1986 1.6.2.2 pgoyette }
1987 1.6.2.2 pgoyette
1988 1.6.2.2 pgoyette static int
1989 1.6.2.2 pgoyette svm_machine_configure(struct nvmm_machine *mach, uint64_t op, void *data)
1990 1.6.2.2 pgoyette {
1991 1.6.2.2 pgoyette struct nvmm_x86_conf_cpuid *cpuid = data;
1992 1.6.2.2 pgoyette struct svm_machdata *machdata = (struct svm_machdata *)mach->machdata;
1993 1.6.2.2 pgoyette size_t i;
1994 1.6.2.2 pgoyette
1995 1.6.2.2 pgoyette if (__predict_false(op != NVMM_X86_CONF_CPUID)) {
1996 1.6.2.2 pgoyette return EINVAL;
1997 1.6.2.2 pgoyette }
1998 1.6.2.2 pgoyette
1999 1.6.2.2 pgoyette if (__predict_false((cpuid->set.eax & cpuid->del.eax) ||
2000 1.6.2.2 pgoyette (cpuid->set.ebx & cpuid->del.ebx) ||
2001 1.6.2.2 pgoyette (cpuid->set.ecx & cpuid->del.ecx) ||
2002 1.6.2.2 pgoyette (cpuid->set.edx & cpuid->del.edx))) {
2003 1.6.2.2 pgoyette return EINVAL;
2004 1.6.2.2 pgoyette }
2005 1.6.2.2 pgoyette
2006 1.6.2.2 pgoyette /* If already here, replace. */
2007 1.6.2.2 pgoyette for (i = 0; i < SVM_NCPUIDS; i++) {
2008 1.6.2.2 pgoyette if (!machdata->cpuidpresent[i]) {
2009 1.6.2.2 pgoyette continue;
2010 1.6.2.2 pgoyette }
2011 1.6.2.2 pgoyette if (machdata->cpuid[i].leaf == cpuid->leaf) {
2012 1.6.2.2 pgoyette memcpy(&machdata->cpuid[i], cpuid,
2013 1.6.2.2 pgoyette sizeof(struct nvmm_x86_conf_cpuid));
2014 1.6.2.2 pgoyette return 0;
2015 1.6.2.2 pgoyette }
2016 1.6.2.2 pgoyette }
2017 1.6.2.2 pgoyette
2018 1.6.2.2 pgoyette /* Not here, insert. */
2019 1.6.2.2 pgoyette for (i = 0; i < SVM_NCPUIDS; i++) {
2020 1.6.2.2 pgoyette if (!machdata->cpuidpresent[i]) {
2021 1.6.2.2 pgoyette machdata->cpuidpresent[i] = true;
2022 1.6.2.2 pgoyette memcpy(&machdata->cpuid[i], cpuid,
2023 1.6.2.2 pgoyette sizeof(struct nvmm_x86_conf_cpuid));
2024 1.6.2.2 pgoyette return 0;
2025 1.6.2.2 pgoyette }
2026 1.6.2.2 pgoyette }
2027 1.6.2.2 pgoyette
2028 1.6.2.2 pgoyette return ENOBUFS;
2029 1.6.2.2 pgoyette }
2030 1.6.2.2 pgoyette
2031 1.6.2.2 pgoyette /* -------------------------------------------------------------------------- */
2032 1.6.2.2 pgoyette
2033 1.6.2.2 pgoyette static bool
2034 1.6.2.2 pgoyette svm_ident(void)
2035 1.6.2.2 pgoyette {
2036 1.6.2.2 pgoyette u_int descs[4];
2037 1.6.2.2 pgoyette uint64_t msr;
2038 1.6.2.2 pgoyette
2039 1.6.2.2 pgoyette if (cpu_vendor != CPUVENDOR_AMD) {
2040 1.6.2.2 pgoyette return false;
2041 1.6.2.2 pgoyette }
2042 1.6.2.2 pgoyette if (!(cpu_feature[3] & CPUID_SVM)) {
2043 1.6.2.2 pgoyette return false;
2044 1.6.2.2 pgoyette }
2045 1.6.2.2 pgoyette
2046 1.6.2.2 pgoyette if (curcpu()->ci_max_ext_cpuid < 0x8000000a) {
2047 1.6.2.2 pgoyette return false;
2048 1.6.2.2 pgoyette }
2049 1.6.2.2 pgoyette x86_cpuid(0x8000000a, descs);
2050 1.6.2.2 pgoyette
2051 1.6.2.2 pgoyette /* Want Nested Paging. */
2052 1.6.2.2 pgoyette if (!(descs[3] & CPUID_AMD_SVM_NP)) {
2053 1.6.2.2 pgoyette return false;
2054 1.6.2.2 pgoyette }
2055 1.6.2.2 pgoyette
2056 1.6.2.2 pgoyette /* Want nRIP. */
2057 1.6.2.2 pgoyette if (!(descs[3] & CPUID_AMD_SVM_NRIPS)) {
2058 1.6.2.2 pgoyette return false;
2059 1.6.2.2 pgoyette }
2060 1.6.2.2 pgoyette
2061 1.6.2.2 pgoyette svm_decode_assist = (descs[3] & CPUID_AMD_SVM_DecodeAssist) != 0;
2062 1.6.2.2 pgoyette
2063 1.6.2.2 pgoyette msr = rdmsr(MSR_VMCR);
2064 1.6.2.2 pgoyette if ((msr & VMCR_SVMED) && (msr & VMCR_LOCK)) {
2065 1.6.2.2 pgoyette return false;
2066 1.6.2.2 pgoyette }
2067 1.6.2.2 pgoyette
2068 1.6.2.2 pgoyette return true;
2069 1.6.2.2 pgoyette }
2070 1.6.2.2 pgoyette
2071 1.6.2.2 pgoyette static void
2072 1.6.2.2 pgoyette svm_init_asid(uint32_t maxasid)
2073 1.6.2.2 pgoyette {
2074 1.6.2.2 pgoyette size_t i, j, allocsz;
2075 1.6.2.2 pgoyette
2076 1.6.2.2 pgoyette mutex_init(&svm_asidlock, MUTEX_DEFAULT, IPL_NONE);
2077 1.6.2.2 pgoyette
2078 1.6.2.2 pgoyette /* Arbitrarily limit. */
2079 1.6.2.2 pgoyette maxasid = uimin(maxasid, 8192);
2080 1.6.2.2 pgoyette
2081 1.6.2.2 pgoyette svm_maxasid = maxasid;
2082 1.6.2.2 pgoyette allocsz = roundup(maxasid, 8) / 8;
2083 1.6.2.2 pgoyette svm_asidmap = kmem_zalloc(allocsz, KM_SLEEP);
2084 1.6.2.2 pgoyette
2085 1.6.2.2 pgoyette /* ASID 0 is reserved for the host. */
2086 1.6.2.2 pgoyette svm_asidmap[0] |= __BIT(0);
2087 1.6.2.2 pgoyette
2088 1.6.2.2 pgoyette /* ASID n-1 is special, we share it. */
2089 1.6.2.2 pgoyette i = (maxasid - 1) / 8;
2090 1.6.2.2 pgoyette j = (maxasid - 1) % 8;
2091 1.6.2.2 pgoyette svm_asidmap[i] |= __BIT(j);
2092 1.6.2.2 pgoyette }
2093 1.6.2.2 pgoyette
2094 1.6.2.2 pgoyette static void
2095 1.6.2.2 pgoyette svm_change_cpu(void *arg1, void *arg2)
2096 1.6.2.2 pgoyette {
2097 1.6.2.2 pgoyette bool enable = (bool)arg1;
2098 1.6.2.2 pgoyette uint64_t msr;
2099 1.6.2.2 pgoyette
2100 1.6.2.2 pgoyette msr = rdmsr(MSR_VMCR);
2101 1.6.2.2 pgoyette if (msr & VMCR_SVMED) {
2102 1.6.2.2 pgoyette wrmsr(MSR_VMCR, msr & ~VMCR_SVMED);
2103 1.6.2.2 pgoyette }
2104 1.6.2.2 pgoyette
2105 1.6.2.2 pgoyette if (!enable) {
2106 1.6.2.2 pgoyette wrmsr(MSR_VM_HSAVE_PA, 0);
2107 1.6.2.2 pgoyette }
2108 1.6.2.2 pgoyette
2109 1.6.2.2 pgoyette msr = rdmsr(MSR_EFER);
2110 1.6.2.2 pgoyette if (enable) {
2111 1.6.2.2 pgoyette msr |= EFER_SVME;
2112 1.6.2.2 pgoyette } else {
2113 1.6.2.2 pgoyette msr &= ~EFER_SVME;
2114 1.6.2.2 pgoyette }
2115 1.6.2.2 pgoyette wrmsr(MSR_EFER, msr);
2116 1.6.2.2 pgoyette
2117 1.6.2.2 pgoyette if (enable) {
2118 1.6.2.2 pgoyette wrmsr(MSR_VM_HSAVE_PA, hsave[cpu_index(curcpu())].pa);
2119 1.6.2.2 pgoyette }
2120 1.6.2.2 pgoyette }
2121 1.6.2.2 pgoyette
2122 1.6.2.2 pgoyette static void
2123 1.6.2.2 pgoyette svm_init(void)
2124 1.6.2.2 pgoyette {
2125 1.6.2.2 pgoyette CPU_INFO_ITERATOR cii;
2126 1.6.2.2 pgoyette struct cpu_info *ci;
2127 1.6.2.2 pgoyette struct vm_page *pg;
2128 1.6.2.2 pgoyette u_int descs[4];
2129 1.6.2.2 pgoyette uint64_t xc;
2130 1.6.2.2 pgoyette
2131 1.6.2.2 pgoyette x86_cpuid(0x8000000a, descs);
2132 1.6.2.2 pgoyette
2133 1.6.2.2 pgoyette /* The guest TLB flush command. */
2134 1.6.2.2 pgoyette if (descs[3] & CPUID_AMD_SVM_FlushByASID) {
2135 1.6.2.2 pgoyette svm_ctrl_tlb_flush = VMCB_CTRL_TLB_CTRL_FLUSH_GUEST;
2136 1.6.2.2 pgoyette } else {
2137 1.6.2.2 pgoyette svm_ctrl_tlb_flush = VMCB_CTRL_TLB_CTRL_FLUSH_ALL;
2138 1.6.2.2 pgoyette }
2139 1.6.2.2 pgoyette
2140 1.6.2.2 pgoyette /* Init the ASID. */
2141 1.6.2.2 pgoyette svm_init_asid(descs[1]);
2142 1.6.2.2 pgoyette
2143 1.6.2.2 pgoyette /* Init the XCR0 mask. */
2144 1.6.2.2 pgoyette svm_xcr0_mask = SVM_XCR0_MASK_DEFAULT & x86_xsave_features;
2145 1.6.2.2 pgoyette
2146 1.6.2.2 pgoyette memset(hsave, 0, sizeof(hsave));
2147 1.6.2.2 pgoyette for (CPU_INFO_FOREACH(cii, ci)) {
2148 1.6.2.2 pgoyette pg = uvm_pagealloc(NULL, 0, NULL, UVM_PGA_ZERO);
2149 1.6.2.2 pgoyette hsave[cpu_index(ci)].pa = VM_PAGE_TO_PHYS(pg);
2150 1.6.2.2 pgoyette }
2151 1.6.2.2 pgoyette
2152 1.6.2.2 pgoyette xc = xc_broadcast(0, svm_change_cpu, (void *)true, NULL);
2153 1.6.2.2 pgoyette xc_wait(xc);
2154 1.6.2.2 pgoyette }
2155 1.6.2.2 pgoyette
2156 1.6.2.2 pgoyette static void
2157 1.6.2.2 pgoyette svm_fini_asid(void)
2158 1.6.2.2 pgoyette {
2159 1.6.2.2 pgoyette size_t allocsz;
2160 1.6.2.2 pgoyette
2161 1.6.2.2 pgoyette allocsz = roundup(svm_maxasid, 8) / 8;
2162 1.6.2.2 pgoyette kmem_free(svm_asidmap, allocsz);
2163 1.6.2.2 pgoyette
2164 1.6.2.2 pgoyette mutex_destroy(&svm_asidlock);
2165 1.6.2.2 pgoyette }
2166 1.6.2.2 pgoyette
2167 1.6.2.2 pgoyette static void
2168 1.6.2.2 pgoyette svm_fini(void)
2169 1.6.2.2 pgoyette {
2170 1.6.2.2 pgoyette uint64_t xc;
2171 1.6.2.2 pgoyette size_t i;
2172 1.6.2.2 pgoyette
2173 1.6.2.2 pgoyette xc = xc_broadcast(0, svm_change_cpu, (void *)false, NULL);
2174 1.6.2.2 pgoyette xc_wait(xc);
2175 1.6.2.2 pgoyette
2176 1.6.2.2 pgoyette for (i = 0; i < MAXCPUS; i++) {
2177 1.6.2.2 pgoyette if (hsave[i].pa != 0)
2178 1.6.2.2 pgoyette uvm_pagefree(PHYS_TO_VM_PAGE(hsave[i].pa));
2179 1.6.2.2 pgoyette }
2180 1.6.2.2 pgoyette
2181 1.6.2.2 pgoyette svm_fini_asid();
2182 1.6.2.2 pgoyette }
2183 1.6.2.2 pgoyette
2184 1.6.2.2 pgoyette static void
2185 1.6.2.2 pgoyette svm_capability(struct nvmm_capability *cap)
2186 1.6.2.2 pgoyette {
2187 1.6.2.2 pgoyette cap->u.x86.xcr0_mask = svm_xcr0_mask;
2188 1.6.2.2 pgoyette cap->u.x86.mxcsr_mask = x86_fpu_mxcsr_mask;
2189 1.6.2.2 pgoyette cap->u.x86.conf_cpuid_maxops = SVM_NCPUIDS;
2190 1.6.2.2 pgoyette }
2191 1.6.2.2 pgoyette
2192 1.6.2.2 pgoyette const struct nvmm_impl nvmm_x86_svm = {
2193 1.6.2.2 pgoyette .ident = svm_ident,
2194 1.6.2.2 pgoyette .init = svm_init,
2195 1.6.2.2 pgoyette .fini = svm_fini,
2196 1.6.2.2 pgoyette .capability = svm_capability,
2197 1.6.2.2 pgoyette .conf_max = NVMM_X86_NCONF,
2198 1.6.2.2 pgoyette .conf_sizes = svm_conf_sizes,
2199 1.6.2.2 pgoyette .state_size = sizeof(struct nvmm_x64_state),
2200 1.6.2.2 pgoyette .machine_create = svm_machine_create,
2201 1.6.2.2 pgoyette .machine_destroy = svm_machine_destroy,
2202 1.6.2.2 pgoyette .machine_configure = svm_machine_configure,
2203 1.6.2.2 pgoyette .vcpu_create = svm_vcpu_create,
2204 1.6.2.2 pgoyette .vcpu_destroy = svm_vcpu_destroy,
2205 1.6.2.2 pgoyette .vcpu_setstate = svm_vcpu_setstate,
2206 1.6.2.2 pgoyette .vcpu_getstate = svm_vcpu_getstate,
2207 1.6.2.2 pgoyette .vcpu_inject = svm_vcpu_inject,
2208 1.6.2.2 pgoyette .vcpu_run = svm_vcpu_run
2209 1.6.2.2 pgoyette };
2210