nvmm_x86_svm.c revision 1.6.2.5 1 1.6.2.5 pgoyette /* $NetBSD: nvmm_x86_svm.c,v 1.6.2.5 2019/01/26 22:00:07 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.5 pgoyette __KERNEL_RCSID(0, "$NetBSD: nvmm_x86_svm.c,v 1.6.2.5 2019/01/26 22:00:07 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.5 pgoyette struct xsave_header hfpu __aligned(64);
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.5 pgoyette struct xsave_header gfpu __aligned(64);
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.5 pgoyette svm_inject_gp(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.5 pgoyette event.vector = 13;
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.5 pgoyette static inline void
743 1.6.2.5 pgoyette svm_inkernel_advance(struct vmcb *vmcb)
744 1.6.2.2 pgoyette {
745 1.6.2.5 pgoyette /*
746 1.6.2.5 pgoyette * Maybe we should also apply single-stepping and debug exceptions.
747 1.6.2.5 pgoyette * Matters for guest-ring3, because it can execute 'cpuid' under a
748 1.6.2.5 pgoyette * debugger.
749 1.6.2.5 pgoyette */
750 1.6.2.5 pgoyette vmcb->state.rip = vmcb->ctrl.nrip;
751 1.6.2.5 pgoyette vmcb->ctrl.intr &= ~VMCB_CTRL_INTR_SHADOW;
752 1.6.2.2 pgoyette }
753 1.6.2.2 pgoyette
754 1.6.2.2 pgoyette static void
755 1.6.2.2 pgoyette svm_inkernel_handle_cpuid(struct nvmm_cpu *vcpu, uint64_t eax, uint64_t ecx)
756 1.6.2.2 pgoyette {
757 1.6.2.2 pgoyette struct svm_cpudata *cpudata = vcpu->cpudata;
758 1.6.2.2 pgoyette
759 1.6.2.2 pgoyette switch (eax) {
760 1.6.2.2 pgoyette case 0x00000001: /* APIC number in RBX. The rest is tunable. */
761 1.6.2.4 pgoyette cpudata->gprs[NVMM_X64_GPR_RBX] &= ~CPUID_LOCAL_APIC_ID;
762 1.6.2.4 pgoyette cpudata->gprs[NVMM_X64_GPR_RBX] |= __SHIFTIN(vcpu->cpuid,
763 1.6.2.2 pgoyette CPUID_LOCAL_APIC_ID);
764 1.6.2.2 pgoyette break;
765 1.6.2.2 pgoyette case 0x0000000D: /* FPU description. Not tunable. */
766 1.6.2.2 pgoyette if (ecx != 0 || svm_xcr0_mask == 0) {
767 1.6.2.2 pgoyette break;
768 1.6.2.2 pgoyette }
769 1.6.2.2 pgoyette cpudata->vmcb->state.rax = svm_xcr0_mask & 0xFFFFFFFF;
770 1.6.2.4 pgoyette if (cpudata->gxcr0 & XCR0_SSE) {
771 1.6.2.4 pgoyette cpudata->gprs[NVMM_X64_GPR_RBX] = sizeof(struct fxsave);
772 1.6.2.2 pgoyette } else {
773 1.6.2.4 pgoyette cpudata->gprs[NVMM_X64_GPR_RBX] = sizeof(struct save87);
774 1.6.2.2 pgoyette }
775 1.6.2.4 pgoyette cpudata->gprs[NVMM_X64_GPR_RBX] += 64; /* XSAVE header */
776 1.6.2.4 pgoyette cpudata->gprs[NVMM_X64_GPR_RCX] = sizeof(struct fxsave);
777 1.6.2.4 pgoyette cpudata->gprs[NVMM_X64_GPR_RDX] = svm_xcr0_mask >> 32;
778 1.6.2.4 pgoyette break;
779 1.6.2.4 pgoyette case 0x40000000:
780 1.6.2.5 pgoyette cpudata->gprs[NVMM_X64_GPR_RBX] = 0;
781 1.6.2.5 pgoyette cpudata->gprs[NVMM_X64_GPR_RCX] = 0;
782 1.6.2.5 pgoyette cpudata->gprs[NVMM_X64_GPR_RDX] = 0;
783 1.6.2.4 pgoyette memcpy(&cpudata->gprs[NVMM_X64_GPR_RBX], "___ ", 4);
784 1.6.2.4 pgoyette memcpy(&cpudata->gprs[NVMM_X64_GPR_RCX], "NVMM", 4);
785 1.6.2.4 pgoyette memcpy(&cpudata->gprs[NVMM_X64_GPR_RDX], " ___", 4);
786 1.6.2.4 pgoyette break;
787 1.6.2.5 pgoyette case 0x80000001: /* No SVM, no RDTSCP. The rest is tunable. */
788 1.6.2.4 pgoyette cpudata->gprs[NVMM_X64_GPR_RCX] &= ~CPUID_SVM;
789 1.6.2.5 pgoyette cpudata->gprs[NVMM_X64_GPR_RDX] &= ~CPUID_RDTSCP;
790 1.6.2.2 pgoyette break;
791 1.6.2.2 pgoyette default:
792 1.6.2.2 pgoyette break;
793 1.6.2.2 pgoyette }
794 1.6.2.2 pgoyette }
795 1.6.2.2 pgoyette
796 1.6.2.2 pgoyette static void
797 1.6.2.2 pgoyette svm_exit_cpuid(struct nvmm_machine *mach, struct nvmm_cpu *vcpu,
798 1.6.2.2 pgoyette struct nvmm_exit *exit)
799 1.6.2.2 pgoyette {
800 1.6.2.2 pgoyette struct svm_machdata *machdata = mach->machdata;
801 1.6.2.2 pgoyette struct svm_cpudata *cpudata = vcpu->cpudata;
802 1.6.2.2 pgoyette struct nvmm_x86_conf_cpuid *cpuid;
803 1.6.2.2 pgoyette uint64_t eax, ecx;
804 1.6.2.2 pgoyette u_int descs[4];
805 1.6.2.2 pgoyette size_t i;
806 1.6.2.2 pgoyette
807 1.6.2.2 pgoyette eax = cpudata->vmcb->state.rax;
808 1.6.2.4 pgoyette ecx = cpudata->gprs[NVMM_X64_GPR_RCX];
809 1.6.2.2 pgoyette x86_cpuid2(eax, ecx, descs);
810 1.6.2.2 pgoyette
811 1.6.2.2 pgoyette cpudata->vmcb->state.rax = descs[0];
812 1.6.2.4 pgoyette cpudata->gprs[NVMM_X64_GPR_RBX] = descs[1];
813 1.6.2.4 pgoyette cpudata->gprs[NVMM_X64_GPR_RCX] = descs[2];
814 1.6.2.4 pgoyette cpudata->gprs[NVMM_X64_GPR_RDX] = descs[3];
815 1.6.2.2 pgoyette
816 1.6.2.2 pgoyette for (i = 0; i < SVM_NCPUIDS; i++) {
817 1.6.2.2 pgoyette cpuid = &machdata->cpuid[i];
818 1.6.2.2 pgoyette if (!machdata->cpuidpresent[i]) {
819 1.6.2.2 pgoyette continue;
820 1.6.2.2 pgoyette }
821 1.6.2.2 pgoyette if (cpuid->leaf != eax) {
822 1.6.2.2 pgoyette continue;
823 1.6.2.2 pgoyette }
824 1.6.2.2 pgoyette
825 1.6.2.2 pgoyette /* del */
826 1.6.2.2 pgoyette cpudata->vmcb->state.rax &= ~cpuid->del.eax;
827 1.6.2.4 pgoyette cpudata->gprs[NVMM_X64_GPR_RBX] &= ~cpuid->del.ebx;
828 1.6.2.4 pgoyette cpudata->gprs[NVMM_X64_GPR_RCX] &= ~cpuid->del.ecx;
829 1.6.2.4 pgoyette cpudata->gprs[NVMM_X64_GPR_RDX] &= ~cpuid->del.edx;
830 1.6.2.2 pgoyette
831 1.6.2.2 pgoyette /* set */
832 1.6.2.2 pgoyette cpudata->vmcb->state.rax |= cpuid->set.eax;
833 1.6.2.4 pgoyette cpudata->gprs[NVMM_X64_GPR_RBX] |= cpuid->set.ebx;
834 1.6.2.4 pgoyette cpudata->gprs[NVMM_X64_GPR_RCX] |= cpuid->set.ecx;
835 1.6.2.4 pgoyette cpudata->gprs[NVMM_X64_GPR_RDX] |= cpuid->set.edx;
836 1.6.2.2 pgoyette
837 1.6.2.2 pgoyette break;
838 1.6.2.2 pgoyette }
839 1.6.2.2 pgoyette
840 1.6.2.2 pgoyette /* Overwrite non-tunable leaves. */
841 1.6.2.2 pgoyette svm_inkernel_handle_cpuid(vcpu, eax, ecx);
842 1.6.2.2 pgoyette
843 1.6.2.5 pgoyette svm_inkernel_advance(cpudata->vmcb);
844 1.6.2.2 pgoyette exit->reason = NVMM_EXIT_NONE;
845 1.6.2.2 pgoyette }
846 1.6.2.2 pgoyette
847 1.6.2.4 pgoyette static void
848 1.6.2.4 pgoyette svm_exit_hlt(struct nvmm_machine *mach, struct nvmm_cpu *vcpu,
849 1.6.2.4 pgoyette struct nvmm_exit *exit)
850 1.6.2.4 pgoyette {
851 1.6.2.4 pgoyette struct svm_cpudata *cpudata = vcpu->cpudata;
852 1.6.2.5 pgoyette struct vmcb *vmcb = cpudata->vmcb;
853 1.6.2.4 pgoyette
854 1.6.2.5 pgoyette if (cpudata->int_window_exit && (vmcb->state.rflags & PSL_I)) {
855 1.6.2.5 pgoyette svm_event_waitexit_disable(vcpu, false);
856 1.6.2.5 pgoyette }
857 1.6.2.5 pgoyette
858 1.6.2.5 pgoyette svm_inkernel_advance(cpudata->vmcb);
859 1.6.2.5 pgoyette exit->reason = NVMM_EXIT_HALTED;
860 1.6.2.4 pgoyette }
861 1.6.2.4 pgoyette
862 1.6.2.2 pgoyette #define SVM_EXIT_IO_PORT __BITS(31,16)
863 1.6.2.2 pgoyette #define SVM_EXIT_IO_SEG __BITS(12,10)
864 1.6.2.2 pgoyette #define SVM_EXIT_IO_A64 __BIT(9)
865 1.6.2.2 pgoyette #define SVM_EXIT_IO_A32 __BIT(8)
866 1.6.2.2 pgoyette #define SVM_EXIT_IO_A16 __BIT(7)
867 1.6.2.2 pgoyette #define SVM_EXIT_IO_SZ32 __BIT(6)
868 1.6.2.2 pgoyette #define SVM_EXIT_IO_SZ16 __BIT(5)
869 1.6.2.2 pgoyette #define SVM_EXIT_IO_SZ8 __BIT(4)
870 1.6.2.2 pgoyette #define SVM_EXIT_IO_REP __BIT(3)
871 1.6.2.2 pgoyette #define SVM_EXIT_IO_STR __BIT(2)
872 1.6.2.2 pgoyette #define SVM_EXIT_IO_IN __BIT(0)
873 1.6.2.2 pgoyette
874 1.6.2.2 pgoyette static const int seg_to_nvmm[] = {
875 1.6.2.2 pgoyette [0] = NVMM_X64_SEG_ES,
876 1.6.2.2 pgoyette [1] = NVMM_X64_SEG_CS,
877 1.6.2.2 pgoyette [2] = NVMM_X64_SEG_SS,
878 1.6.2.2 pgoyette [3] = NVMM_X64_SEG_DS,
879 1.6.2.2 pgoyette [4] = NVMM_X64_SEG_FS,
880 1.6.2.2 pgoyette [5] = NVMM_X64_SEG_GS
881 1.6.2.2 pgoyette };
882 1.6.2.2 pgoyette
883 1.6.2.2 pgoyette static void
884 1.6.2.2 pgoyette svm_exit_io(struct nvmm_machine *mach, struct nvmm_cpu *vcpu,
885 1.6.2.2 pgoyette struct nvmm_exit *exit)
886 1.6.2.2 pgoyette {
887 1.6.2.2 pgoyette struct svm_cpudata *cpudata = vcpu->cpudata;
888 1.6.2.2 pgoyette uint64_t info = cpudata->vmcb->ctrl.exitinfo1;
889 1.6.2.2 pgoyette uint64_t nextpc = cpudata->vmcb->ctrl.exitinfo2;
890 1.6.2.2 pgoyette
891 1.6.2.2 pgoyette exit->reason = NVMM_EXIT_IO;
892 1.6.2.2 pgoyette
893 1.6.2.2 pgoyette if (info & SVM_EXIT_IO_IN) {
894 1.6.2.2 pgoyette exit->u.io.type = NVMM_EXIT_IO_IN;
895 1.6.2.2 pgoyette } else {
896 1.6.2.2 pgoyette exit->u.io.type = NVMM_EXIT_IO_OUT;
897 1.6.2.2 pgoyette }
898 1.6.2.2 pgoyette
899 1.6.2.2 pgoyette exit->u.io.port = __SHIFTOUT(info, SVM_EXIT_IO_PORT);
900 1.6.2.2 pgoyette
901 1.6.2.2 pgoyette if (svm_decode_assist) {
902 1.6.2.2 pgoyette KASSERT(__SHIFTOUT(info, SVM_EXIT_IO_SEG) < 6);
903 1.6.2.2 pgoyette exit->u.io.seg = seg_to_nvmm[__SHIFTOUT(info, SVM_EXIT_IO_SEG)];
904 1.6.2.2 pgoyette } else {
905 1.6.2.4 pgoyette exit->u.io.seg = -1;
906 1.6.2.2 pgoyette }
907 1.6.2.2 pgoyette
908 1.6.2.2 pgoyette if (info & SVM_EXIT_IO_A64) {
909 1.6.2.2 pgoyette exit->u.io.address_size = 8;
910 1.6.2.2 pgoyette } else if (info & SVM_EXIT_IO_A32) {
911 1.6.2.2 pgoyette exit->u.io.address_size = 4;
912 1.6.2.2 pgoyette } else if (info & SVM_EXIT_IO_A16) {
913 1.6.2.2 pgoyette exit->u.io.address_size = 2;
914 1.6.2.2 pgoyette }
915 1.6.2.2 pgoyette
916 1.6.2.2 pgoyette if (info & SVM_EXIT_IO_SZ32) {
917 1.6.2.2 pgoyette exit->u.io.operand_size = 4;
918 1.6.2.2 pgoyette } else if (info & SVM_EXIT_IO_SZ16) {
919 1.6.2.2 pgoyette exit->u.io.operand_size = 2;
920 1.6.2.2 pgoyette } else if (info & SVM_EXIT_IO_SZ8) {
921 1.6.2.2 pgoyette exit->u.io.operand_size = 1;
922 1.6.2.2 pgoyette }
923 1.6.2.2 pgoyette
924 1.6.2.2 pgoyette exit->u.io.rep = (info & SVM_EXIT_IO_REP) != 0;
925 1.6.2.2 pgoyette exit->u.io.str = (info & SVM_EXIT_IO_STR) != 0;
926 1.6.2.2 pgoyette exit->u.io.npc = nextpc;
927 1.6.2.2 pgoyette }
928 1.6.2.2 pgoyette
929 1.6.2.4 pgoyette static const uint64_t msr_ignore_list[] = {
930 1.6.2.4 pgoyette 0xc0010055, /* MSR_CMPHALT */
931 1.6.2.4 pgoyette MSR_DE_CFG,
932 1.6.2.4 pgoyette MSR_IC_CFG,
933 1.6.2.4 pgoyette MSR_UCODE_AMD_PATCHLEVEL
934 1.6.2.4 pgoyette };
935 1.6.2.4 pgoyette
936 1.6.2.2 pgoyette static bool
937 1.6.2.2 pgoyette svm_inkernel_handle_msr(struct nvmm_machine *mach, struct nvmm_cpu *vcpu,
938 1.6.2.2 pgoyette struct nvmm_exit *exit)
939 1.6.2.2 pgoyette {
940 1.6.2.2 pgoyette struct svm_cpudata *cpudata = vcpu->cpudata;
941 1.6.2.4 pgoyette uint64_t val;
942 1.6.2.4 pgoyette size_t i;
943 1.6.2.2 pgoyette
944 1.6.2.2 pgoyette switch (exit->u.msr.type) {
945 1.6.2.2 pgoyette case NVMM_EXIT_MSR_RDMSR:
946 1.6.2.2 pgoyette if (exit->u.msr.msr == MSR_CR_PAT) {
947 1.6.2.4 pgoyette val = cpudata->vmcb->state.g_pat;
948 1.6.2.4 pgoyette cpudata->vmcb->state.rax = (val & 0xFFFFFFFF);
949 1.6.2.4 pgoyette cpudata->gprs[NVMM_X64_GPR_RDX] = (val >> 32);
950 1.6.2.4 pgoyette goto handled;
951 1.6.2.4 pgoyette }
952 1.6.2.4 pgoyette if (exit->u.msr.msr == MSR_NB_CFG) {
953 1.6.2.4 pgoyette val = NB_CFG_INITAPICCPUIDLO;
954 1.6.2.4 pgoyette cpudata->vmcb->state.rax = (val & 0xFFFFFFFF);
955 1.6.2.4 pgoyette cpudata->gprs[NVMM_X64_GPR_RDX] = (val >> 32);
956 1.6.2.4 pgoyette goto handled;
957 1.6.2.4 pgoyette }
958 1.6.2.4 pgoyette for (i = 0; i < __arraycount(msr_ignore_list); i++) {
959 1.6.2.4 pgoyette if (msr_ignore_list[i] != exit->u.msr.msr)
960 1.6.2.4 pgoyette continue;
961 1.6.2.4 pgoyette val = 0;
962 1.6.2.4 pgoyette cpudata->vmcb->state.rax = (val & 0xFFFFFFFF);
963 1.6.2.4 pgoyette cpudata->gprs[NVMM_X64_GPR_RDX] = (val >> 32);
964 1.6.2.2 pgoyette goto handled;
965 1.6.2.2 pgoyette }
966 1.6.2.2 pgoyette break;
967 1.6.2.2 pgoyette case NVMM_EXIT_MSR_WRMSR:
968 1.6.2.2 pgoyette if (exit->u.msr.msr == MSR_EFER) {
969 1.6.2.2 pgoyette if (__predict_false(exit->u.msr.val & ~EFER_VALID)) {
970 1.6.2.2 pgoyette svm_inject_gp(mach, vcpu);
971 1.6.2.2 pgoyette goto handled;
972 1.6.2.2 pgoyette }
973 1.6.2.2 pgoyette if ((cpudata->vmcb->state.efer ^ exit->u.msr.val) &
974 1.6.2.2 pgoyette EFER_TLB_FLUSH) {
975 1.6.2.2 pgoyette cpudata->tlb_want_flush = true;
976 1.6.2.2 pgoyette }
977 1.6.2.2 pgoyette cpudata->vmcb->state.efer = exit->u.msr.val | EFER_SVME;
978 1.6.2.2 pgoyette goto handled;
979 1.6.2.2 pgoyette }
980 1.6.2.2 pgoyette if (exit->u.msr.msr == MSR_CR_PAT) {
981 1.6.2.2 pgoyette cpudata->vmcb->state.g_pat = exit->u.msr.val;
982 1.6.2.2 pgoyette goto handled;
983 1.6.2.2 pgoyette }
984 1.6.2.4 pgoyette for (i = 0; i < __arraycount(msr_ignore_list); i++) {
985 1.6.2.4 pgoyette if (msr_ignore_list[i] != exit->u.msr.msr)
986 1.6.2.4 pgoyette continue;
987 1.6.2.4 pgoyette goto handled;
988 1.6.2.4 pgoyette }
989 1.6.2.2 pgoyette break;
990 1.6.2.2 pgoyette }
991 1.6.2.2 pgoyette
992 1.6.2.2 pgoyette return false;
993 1.6.2.2 pgoyette
994 1.6.2.2 pgoyette handled:
995 1.6.2.5 pgoyette svm_inkernel_advance(cpudata->vmcb);
996 1.6.2.2 pgoyette return true;
997 1.6.2.2 pgoyette }
998 1.6.2.2 pgoyette
999 1.6.2.2 pgoyette static void
1000 1.6.2.2 pgoyette svm_exit_msr(struct nvmm_machine *mach, struct nvmm_cpu *vcpu,
1001 1.6.2.2 pgoyette struct nvmm_exit *exit)
1002 1.6.2.2 pgoyette {
1003 1.6.2.2 pgoyette struct svm_cpudata *cpudata = vcpu->cpudata;
1004 1.6.2.2 pgoyette uint64_t info = cpudata->vmcb->ctrl.exitinfo1;
1005 1.6.2.2 pgoyette
1006 1.6.2.2 pgoyette if (info == 0) {
1007 1.6.2.2 pgoyette exit->u.msr.type = NVMM_EXIT_MSR_RDMSR;
1008 1.6.2.2 pgoyette } else {
1009 1.6.2.2 pgoyette exit->u.msr.type = NVMM_EXIT_MSR_WRMSR;
1010 1.6.2.2 pgoyette }
1011 1.6.2.2 pgoyette
1012 1.6.2.5 pgoyette exit->u.msr.msr = (cpudata->gprs[NVMM_X64_GPR_RCX] & 0xFFFFFFFF);
1013 1.6.2.2 pgoyette
1014 1.6.2.2 pgoyette if (info == 1) {
1015 1.6.2.2 pgoyette uint64_t rdx, rax;
1016 1.6.2.4 pgoyette rdx = cpudata->gprs[NVMM_X64_GPR_RDX];
1017 1.6.2.2 pgoyette rax = cpudata->vmcb->state.rax;
1018 1.6.2.2 pgoyette exit->u.msr.val = (rdx << 32) | (rax & 0xFFFFFFFF);
1019 1.6.2.2 pgoyette } else {
1020 1.6.2.2 pgoyette exit->u.msr.val = 0;
1021 1.6.2.2 pgoyette }
1022 1.6.2.2 pgoyette
1023 1.6.2.2 pgoyette if (svm_inkernel_handle_msr(mach, vcpu, exit)) {
1024 1.6.2.2 pgoyette exit->reason = NVMM_EXIT_NONE;
1025 1.6.2.2 pgoyette return;
1026 1.6.2.2 pgoyette }
1027 1.6.2.2 pgoyette
1028 1.6.2.2 pgoyette exit->reason = NVMM_EXIT_MSR;
1029 1.6.2.2 pgoyette exit->u.msr.npc = cpudata->vmcb->ctrl.nrip;
1030 1.6.2.2 pgoyette }
1031 1.6.2.2 pgoyette
1032 1.6.2.2 pgoyette static void
1033 1.6.2.2 pgoyette svm_exit_npf(struct nvmm_machine *mach, struct nvmm_cpu *vcpu,
1034 1.6.2.2 pgoyette struct nvmm_exit *exit)
1035 1.6.2.2 pgoyette {
1036 1.6.2.2 pgoyette struct svm_cpudata *cpudata = vcpu->cpudata;
1037 1.6.2.2 pgoyette gpaddr_t gpa = cpudata->vmcb->ctrl.exitinfo2;
1038 1.6.2.2 pgoyette int error;
1039 1.6.2.2 pgoyette
1040 1.6.2.2 pgoyette error = uvm_fault(&mach->vm->vm_map, gpa, VM_PROT_ALL);
1041 1.6.2.2 pgoyette
1042 1.6.2.2 pgoyette if (error) {
1043 1.6.2.2 pgoyette exit->reason = NVMM_EXIT_MEMORY;
1044 1.6.2.2 pgoyette if (cpudata->vmcb->ctrl.exitinfo1 & PGEX_W)
1045 1.6.2.2 pgoyette exit->u.mem.perm = NVMM_EXIT_MEMORY_WRITE;
1046 1.6.2.2 pgoyette else if (cpudata->vmcb->ctrl.exitinfo1 & PGEX_X)
1047 1.6.2.2 pgoyette exit->u.mem.perm = NVMM_EXIT_MEMORY_EXEC;
1048 1.6.2.2 pgoyette else
1049 1.6.2.2 pgoyette exit->u.mem.perm = NVMM_EXIT_MEMORY_READ;
1050 1.6.2.2 pgoyette exit->u.mem.gpa = gpa;
1051 1.6.2.2 pgoyette exit->u.mem.inst_len = cpudata->vmcb->ctrl.inst_len;
1052 1.6.2.2 pgoyette memcpy(exit->u.mem.inst_bytes, cpudata->vmcb->ctrl.inst_bytes,
1053 1.6.2.2 pgoyette sizeof(exit->u.mem.inst_bytes));
1054 1.6.2.2 pgoyette } else {
1055 1.6.2.2 pgoyette exit->reason = NVMM_EXIT_NONE;
1056 1.6.2.2 pgoyette }
1057 1.6.2.2 pgoyette }
1058 1.6.2.2 pgoyette
1059 1.6.2.2 pgoyette static void
1060 1.6.2.5 pgoyette svm_exit_insn(struct vmcb *vmcb, struct nvmm_exit *exit, uint64_t reason)
1061 1.6.2.5 pgoyette {
1062 1.6.2.5 pgoyette exit->u.insn.npc = vmcb->ctrl.nrip;
1063 1.6.2.5 pgoyette exit->reason = reason;
1064 1.6.2.5 pgoyette }
1065 1.6.2.5 pgoyette
1066 1.6.2.5 pgoyette static void
1067 1.6.2.2 pgoyette svm_exit_xsetbv(struct nvmm_machine *mach, struct nvmm_cpu *vcpu,
1068 1.6.2.2 pgoyette struct nvmm_exit *exit)
1069 1.6.2.2 pgoyette {
1070 1.6.2.2 pgoyette struct svm_cpudata *cpudata = vcpu->cpudata;
1071 1.6.2.2 pgoyette struct vmcb *vmcb = cpudata->vmcb;
1072 1.6.2.2 pgoyette uint64_t val;
1073 1.6.2.2 pgoyette
1074 1.6.2.2 pgoyette exit->reason = NVMM_EXIT_NONE;
1075 1.6.2.2 pgoyette
1076 1.6.2.4 pgoyette val = (cpudata->gprs[NVMM_X64_GPR_RDX] << 32) |
1077 1.6.2.2 pgoyette (vmcb->state.rax & 0xFFFFFFFF);
1078 1.6.2.2 pgoyette
1079 1.6.2.4 pgoyette if (__predict_false(cpudata->gprs[NVMM_X64_GPR_RCX] != 0)) {
1080 1.6.2.2 pgoyette goto error;
1081 1.6.2.2 pgoyette } else if (__predict_false(vmcb->state.cpl != 0)) {
1082 1.6.2.2 pgoyette goto error;
1083 1.6.2.2 pgoyette } else if (__predict_false((val & ~svm_xcr0_mask) != 0)) {
1084 1.6.2.2 pgoyette goto error;
1085 1.6.2.2 pgoyette } else if (__predict_false((val & XCR0_X87) == 0)) {
1086 1.6.2.2 pgoyette goto error;
1087 1.6.2.2 pgoyette }
1088 1.6.2.2 pgoyette
1089 1.6.2.4 pgoyette cpudata->gxcr0 = val;
1090 1.6.2.2 pgoyette
1091 1.6.2.5 pgoyette svm_inkernel_advance(cpudata->vmcb);
1092 1.6.2.2 pgoyette return;
1093 1.6.2.2 pgoyette
1094 1.6.2.2 pgoyette error:
1095 1.6.2.2 pgoyette svm_inject_gp(mach, vcpu);
1096 1.6.2.2 pgoyette }
1097 1.6.2.2 pgoyette
1098 1.6.2.2 pgoyette static void
1099 1.6.2.2 pgoyette svm_vcpu_guest_fpu_enter(struct nvmm_cpu *vcpu)
1100 1.6.2.2 pgoyette {
1101 1.6.2.2 pgoyette struct svm_cpudata *cpudata = vcpu->cpudata;
1102 1.6.2.2 pgoyette
1103 1.6.2.5 pgoyette cpudata->ts_set = (rcr0() & CR0_TS) != 0;
1104 1.6.2.5 pgoyette
1105 1.6.2.5 pgoyette fpu_area_save(&cpudata->hfpu, svm_xcr0_mask);
1106 1.6.2.5 pgoyette fpu_area_restore(&cpudata->gfpu, svm_xcr0_mask);
1107 1.6.2.5 pgoyette
1108 1.6.2.5 pgoyette if (svm_xcr0_mask != 0) {
1109 1.6.2.4 pgoyette cpudata->hxcr0 = rdxcr(0);
1110 1.6.2.4 pgoyette wrxcr(0, cpudata->gxcr0);
1111 1.6.2.2 pgoyette }
1112 1.6.2.2 pgoyette }
1113 1.6.2.2 pgoyette
1114 1.6.2.2 pgoyette static void
1115 1.6.2.2 pgoyette svm_vcpu_guest_fpu_leave(struct nvmm_cpu *vcpu)
1116 1.6.2.2 pgoyette {
1117 1.6.2.2 pgoyette struct svm_cpudata *cpudata = vcpu->cpudata;
1118 1.6.2.2 pgoyette
1119 1.6.2.5 pgoyette if (svm_xcr0_mask != 0) {
1120 1.6.2.5 pgoyette cpudata->gxcr0 = rdxcr(0);
1121 1.6.2.5 pgoyette wrxcr(0, cpudata->hxcr0);
1122 1.6.2.5 pgoyette }
1123 1.6.2.5 pgoyette
1124 1.6.2.5 pgoyette fpu_area_save(&cpudata->gfpu, svm_xcr0_mask);
1125 1.6.2.5 pgoyette fpu_area_restore(&cpudata->hfpu, svm_xcr0_mask);
1126 1.6.2.2 pgoyette
1127 1.6.2.2 pgoyette if (cpudata->ts_set) {
1128 1.6.2.2 pgoyette stts();
1129 1.6.2.2 pgoyette }
1130 1.6.2.2 pgoyette }
1131 1.6.2.2 pgoyette
1132 1.6.2.2 pgoyette static void
1133 1.6.2.2 pgoyette svm_vcpu_guest_dbregs_enter(struct nvmm_cpu *vcpu)
1134 1.6.2.2 pgoyette {
1135 1.6.2.2 pgoyette struct svm_cpudata *cpudata = vcpu->cpudata;
1136 1.6.2.2 pgoyette
1137 1.6.2.2 pgoyette x86_dbregs_save(curlwp);
1138 1.6.2.2 pgoyette
1139 1.6.2.4 pgoyette ldr7(0);
1140 1.6.2.4 pgoyette
1141 1.6.2.4 pgoyette ldr0(cpudata->drs[NVMM_X64_DR_DR0]);
1142 1.6.2.4 pgoyette ldr1(cpudata->drs[NVMM_X64_DR_DR1]);
1143 1.6.2.4 pgoyette ldr2(cpudata->drs[NVMM_X64_DR_DR2]);
1144 1.6.2.4 pgoyette ldr3(cpudata->drs[NVMM_X64_DR_DR3]);
1145 1.6.2.2 pgoyette }
1146 1.6.2.2 pgoyette
1147 1.6.2.2 pgoyette static void
1148 1.6.2.2 pgoyette svm_vcpu_guest_dbregs_leave(struct nvmm_cpu *vcpu)
1149 1.6.2.2 pgoyette {
1150 1.6.2.2 pgoyette struct svm_cpudata *cpudata = vcpu->cpudata;
1151 1.6.2.2 pgoyette
1152 1.6.2.4 pgoyette cpudata->drs[NVMM_X64_DR_DR0] = rdr0();
1153 1.6.2.4 pgoyette cpudata->drs[NVMM_X64_DR_DR1] = rdr1();
1154 1.6.2.4 pgoyette cpudata->drs[NVMM_X64_DR_DR2] = rdr2();
1155 1.6.2.4 pgoyette cpudata->drs[NVMM_X64_DR_DR3] = rdr3();
1156 1.6.2.2 pgoyette
1157 1.6.2.2 pgoyette x86_dbregs_restore(curlwp);
1158 1.6.2.2 pgoyette }
1159 1.6.2.2 pgoyette
1160 1.6.2.2 pgoyette static void
1161 1.6.2.2 pgoyette svm_vcpu_guest_misc_enter(struct nvmm_cpu *vcpu)
1162 1.6.2.2 pgoyette {
1163 1.6.2.2 pgoyette struct svm_cpudata *cpudata = vcpu->cpudata;
1164 1.6.2.2 pgoyette
1165 1.6.2.2 pgoyette cpudata->star = rdmsr(MSR_STAR);
1166 1.6.2.2 pgoyette cpudata->lstar = rdmsr(MSR_LSTAR);
1167 1.6.2.2 pgoyette cpudata->cstar = rdmsr(MSR_CSTAR);
1168 1.6.2.2 pgoyette cpudata->sfmask = rdmsr(MSR_SFMASK);
1169 1.6.2.4 pgoyette cpudata->fsbase = rdmsr(MSR_FSBASE);
1170 1.6.2.4 pgoyette cpudata->kernelgsbase = rdmsr(MSR_KERNELGSBASE);
1171 1.6.2.2 pgoyette }
1172 1.6.2.2 pgoyette
1173 1.6.2.2 pgoyette static void
1174 1.6.2.2 pgoyette svm_vcpu_guest_misc_leave(struct nvmm_cpu *vcpu)
1175 1.6.2.2 pgoyette {
1176 1.6.2.2 pgoyette struct svm_cpudata *cpudata = vcpu->cpudata;
1177 1.6.2.2 pgoyette
1178 1.6.2.2 pgoyette wrmsr(MSR_STAR, cpudata->star);
1179 1.6.2.2 pgoyette wrmsr(MSR_LSTAR, cpudata->lstar);
1180 1.6.2.2 pgoyette wrmsr(MSR_CSTAR, cpudata->cstar);
1181 1.6.2.2 pgoyette wrmsr(MSR_SFMASK, cpudata->sfmask);
1182 1.6.2.4 pgoyette wrmsr(MSR_FSBASE, cpudata->fsbase);
1183 1.6.2.4 pgoyette wrmsr(MSR_KERNELGSBASE, cpudata->kernelgsbase);
1184 1.6.2.2 pgoyette }
1185 1.6.2.2 pgoyette
1186 1.6.2.2 pgoyette static int
1187 1.6.2.2 pgoyette svm_vcpu_run(struct nvmm_machine *mach, struct nvmm_cpu *vcpu,
1188 1.6.2.2 pgoyette struct nvmm_exit *exit)
1189 1.6.2.2 pgoyette {
1190 1.6.2.2 pgoyette struct svm_cpudata *cpudata = vcpu->cpudata;
1191 1.6.2.2 pgoyette struct vmcb *vmcb = cpudata->vmcb;
1192 1.6.2.2 pgoyette bool tlb_need_flush = false;
1193 1.6.2.2 pgoyette int hcpu, s;
1194 1.6.2.2 pgoyette
1195 1.6.2.2 pgoyette kpreempt_disable();
1196 1.6.2.2 pgoyette hcpu = cpu_number();
1197 1.6.2.2 pgoyette
1198 1.6.2.2 pgoyette if (vcpu->hcpu_last != hcpu || cpudata->shared_asid) {
1199 1.6.2.2 pgoyette tlb_need_flush = true;
1200 1.6.2.2 pgoyette }
1201 1.6.2.2 pgoyette
1202 1.6.2.2 pgoyette if (cpudata->tlb_want_flush || tlb_need_flush) {
1203 1.6.2.2 pgoyette vmcb->ctrl.tlb_ctrl = svm_ctrl_tlb_flush;
1204 1.6.2.2 pgoyette } else {
1205 1.6.2.2 pgoyette vmcb->ctrl.tlb_ctrl = 0;
1206 1.6.2.2 pgoyette }
1207 1.6.2.2 pgoyette
1208 1.6.2.2 pgoyette if (vcpu->hcpu_last != hcpu) {
1209 1.6.2.2 pgoyette vmcb->ctrl.tsc_offset = cpudata->tsc_offset +
1210 1.6.2.2 pgoyette curcpu()->ci_data.cpu_cc_skew;
1211 1.6.2.4 pgoyette svm_vmcb_cache_flush_all(vmcb);
1212 1.6.2.2 pgoyette }
1213 1.6.2.2 pgoyette
1214 1.6.2.2 pgoyette svm_vcpu_guest_dbregs_enter(vcpu);
1215 1.6.2.2 pgoyette svm_vcpu_guest_misc_enter(vcpu);
1216 1.6.2.2 pgoyette
1217 1.6.2.2 pgoyette while (1) {
1218 1.6.2.2 pgoyette s = splhigh();
1219 1.6.2.2 pgoyette svm_vcpu_guest_fpu_enter(vcpu);
1220 1.6.2.4 pgoyette svm_vmrun(cpudata->vmcb_pa, cpudata->gprs);
1221 1.6.2.2 pgoyette svm_vcpu_guest_fpu_leave(vcpu);
1222 1.6.2.2 pgoyette splx(s);
1223 1.6.2.2 pgoyette
1224 1.6.2.2 pgoyette svm_vmcb_cache_default(vmcb);
1225 1.6.2.2 pgoyette
1226 1.6.2.2 pgoyette if (vmcb->ctrl.exitcode != VMCB_EXITCODE_INVALID) {
1227 1.6.2.2 pgoyette if (cpudata->tlb_want_flush) {
1228 1.6.2.2 pgoyette cpudata->tlb_want_flush = false;
1229 1.6.2.2 pgoyette }
1230 1.6.2.2 pgoyette vcpu->hcpu_last = hcpu;
1231 1.6.2.2 pgoyette }
1232 1.6.2.2 pgoyette
1233 1.6.2.2 pgoyette switch (vmcb->ctrl.exitcode) {
1234 1.6.2.2 pgoyette case VMCB_EXITCODE_INTR:
1235 1.6.2.2 pgoyette case VMCB_EXITCODE_NMI:
1236 1.6.2.2 pgoyette exit->reason = NVMM_EXIT_NONE;
1237 1.6.2.2 pgoyette break;
1238 1.6.2.2 pgoyette case VMCB_EXITCODE_VINTR:
1239 1.6.2.4 pgoyette svm_event_waitexit_disable(vcpu, false);
1240 1.6.2.2 pgoyette exit->reason = NVMM_EXIT_INT_READY;
1241 1.6.2.2 pgoyette break;
1242 1.6.2.2 pgoyette case VMCB_EXITCODE_IRET:
1243 1.6.2.4 pgoyette svm_event_waitexit_disable(vcpu, true);
1244 1.6.2.2 pgoyette exit->reason = NVMM_EXIT_NMI_READY;
1245 1.6.2.2 pgoyette break;
1246 1.6.2.2 pgoyette case VMCB_EXITCODE_CPUID:
1247 1.6.2.2 pgoyette svm_exit_cpuid(mach, vcpu, exit);
1248 1.6.2.2 pgoyette break;
1249 1.6.2.2 pgoyette case VMCB_EXITCODE_HLT:
1250 1.6.2.4 pgoyette svm_exit_hlt(mach, vcpu, exit);
1251 1.6.2.2 pgoyette break;
1252 1.6.2.2 pgoyette case VMCB_EXITCODE_IOIO:
1253 1.6.2.2 pgoyette svm_exit_io(mach, vcpu, exit);
1254 1.6.2.2 pgoyette break;
1255 1.6.2.2 pgoyette case VMCB_EXITCODE_MSR:
1256 1.6.2.2 pgoyette svm_exit_msr(mach, vcpu, exit);
1257 1.6.2.2 pgoyette break;
1258 1.6.2.2 pgoyette case VMCB_EXITCODE_SHUTDOWN:
1259 1.6.2.2 pgoyette exit->reason = NVMM_EXIT_SHUTDOWN;
1260 1.6.2.2 pgoyette break;
1261 1.6.2.2 pgoyette case VMCB_EXITCODE_RDPMC:
1262 1.6.2.2 pgoyette case VMCB_EXITCODE_RSM:
1263 1.6.2.2 pgoyette case VMCB_EXITCODE_INVLPGA:
1264 1.6.2.2 pgoyette case VMCB_EXITCODE_VMRUN:
1265 1.6.2.2 pgoyette case VMCB_EXITCODE_VMMCALL:
1266 1.6.2.2 pgoyette case VMCB_EXITCODE_VMLOAD:
1267 1.6.2.2 pgoyette case VMCB_EXITCODE_VMSAVE:
1268 1.6.2.2 pgoyette case VMCB_EXITCODE_STGI:
1269 1.6.2.2 pgoyette case VMCB_EXITCODE_CLGI:
1270 1.6.2.2 pgoyette case VMCB_EXITCODE_SKINIT:
1271 1.6.2.2 pgoyette case VMCB_EXITCODE_RDTSCP:
1272 1.6.2.2 pgoyette svm_inject_ud(mach, vcpu);
1273 1.6.2.2 pgoyette exit->reason = NVMM_EXIT_NONE;
1274 1.6.2.2 pgoyette break;
1275 1.6.2.2 pgoyette case VMCB_EXITCODE_MONITOR:
1276 1.6.2.5 pgoyette svm_exit_insn(vmcb, exit, NVMM_EXIT_MONITOR);
1277 1.6.2.2 pgoyette break;
1278 1.6.2.2 pgoyette case VMCB_EXITCODE_MWAIT:
1279 1.6.2.5 pgoyette svm_exit_insn(vmcb, exit, NVMM_EXIT_MWAIT);
1280 1.6.2.2 pgoyette break;
1281 1.6.2.2 pgoyette case VMCB_EXITCODE_MWAIT_CONDITIONAL:
1282 1.6.2.5 pgoyette svm_exit_insn(vmcb, exit, NVMM_EXIT_MWAIT_COND);
1283 1.6.2.2 pgoyette break;
1284 1.6.2.2 pgoyette case VMCB_EXITCODE_XSETBV:
1285 1.6.2.2 pgoyette svm_exit_xsetbv(mach, vcpu, exit);
1286 1.6.2.2 pgoyette break;
1287 1.6.2.2 pgoyette case VMCB_EXITCODE_NPF:
1288 1.6.2.2 pgoyette svm_exit_npf(mach, vcpu, exit);
1289 1.6.2.2 pgoyette break;
1290 1.6.2.2 pgoyette case VMCB_EXITCODE_FERR_FREEZE: /* ? */
1291 1.6.2.2 pgoyette default:
1292 1.6.2.2 pgoyette exit->reason = NVMM_EXIT_INVALID;
1293 1.6.2.2 pgoyette break;
1294 1.6.2.2 pgoyette }
1295 1.6.2.2 pgoyette
1296 1.6.2.2 pgoyette /* If no reason to return to userland, keep rolling. */
1297 1.6.2.2 pgoyette if (curcpu()->ci_schedstate.spc_flags & SPCF_SHOULDYIELD) {
1298 1.6.2.2 pgoyette break;
1299 1.6.2.2 pgoyette }
1300 1.6.2.4 pgoyette if (curcpu()->ci_data.cpu_softints != 0) {
1301 1.6.2.4 pgoyette break;
1302 1.6.2.4 pgoyette }
1303 1.6.2.4 pgoyette if (curlwp->l_flag & LW_USERRET) {
1304 1.6.2.4 pgoyette break;
1305 1.6.2.4 pgoyette }
1306 1.6.2.2 pgoyette if (exit->reason != NVMM_EXIT_NONE) {
1307 1.6.2.2 pgoyette break;
1308 1.6.2.2 pgoyette }
1309 1.6.2.2 pgoyette }
1310 1.6.2.2 pgoyette
1311 1.6.2.2 pgoyette svm_vcpu_guest_misc_leave(vcpu);
1312 1.6.2.2 pgoyette svm_vcpu_guest_dbregs_leave(vcpu);
1313 1.6.2.2 pgoyette
1314 1.6.2.2 pgoyette kpreempt_enable();
1315 1.6.2.2 pgoyette
1316 1.6.2.2 pgoyette exit->exitstate[NVMM_X64_EXITSTATE_CR8] = __SHIFTOUT(vmcb->ctrl.v,
1317 1.6.2.2 pgoyette VMCB_CTRL_V_TPR);
1318 1.6.2.2 pgoyette exit->exitstate[NVMM_X64_EXITSTATE_RFLAGS] = vmcb->state.rflags;
1319 1.6.2.2 pgoyette
1320 1.6.2.4 pgoyette exit->exitstate[NVMM_X64_EXITSTATE_INT_SHADOW] =
1321 1.6.2.4 pgoyette ((vmcb->ctrl.intr & VMCB_CTRL_INTR_SHADOW) != 0);
1322 1.6.2.4 pgoyette exit->exitstate[NVMM_X64_EXITSTATE_INT_WINDOW_EXIT] =
1323 1.6.2.4 pgoyette cpudata->int_window_exit;
1324 1.6.2.4 pgoyette exit->exitstate[NVMM_X64_EXITSTATE_NMI_WINDOW_EXIT] =
1325 1.6.2.4 pgoyette cpudata->nmi_window_exit;
1326 1.6.2.4 pgoyette
1327 1.6.2.2 pgoyette return 0;
1328 1.6.2.2 pgoyette }
1329 1.6.2.2 pgoyette
1330 1.6.2.2 pgoyette /* -------------------------------------------------------------------------- */
1331 1.6.2.2 pgoyette
1332 1.6.2.2 pgoyette static int
1333 1.6.2.2 pgoyette svm_memalloc(paddr_t *pa, vaddr_t *va, size_t npages)
1334 1.6.2.2 pgoyette {
1335 1.6.2.2 pgoyette struct pglist pglist;
1336 1.6.2.2 pgoyette paddr_t _pa;
1337 1.6.2.2 pgoyette vaddr_t _va;
1338 1.6.2.2 pgoyette size_t i;
1339 1.6.2.2 pgoyette int ret;
1340 1.6.2.2 pgoyette
1341 1.6.2.2 pgoyette ret = uvm_pglistalloc(npages * PAGE_SIZE, 0, ~0UL, PAGE_SIZE, 0,
1342 1.6.2.2 pgoyette &pglist, 1, 0);
1343 1.6.2.2 pgoyette if (ret != 0)
1344 1.6.2.2 pgoyette return ENOMEM;
1345 1.6.2.2 pgoyette _pa = TAILQ_FIRST(&pglist)->phys_addr;
1346 1.6.2.2 pgoyette _va = uvm_km_alloc(kernel_map, npages * PAGE_SIZE, 0,
1347 1.6.2.2 pgoyette UVM_KMF_VAONLY | UVM_KMF_NOWAIT);
1348 1.6.2.2 pgoyette if (_va == 0)
1349 1.6.2.2 pgoyette goto error;
1350 1.6.2.2 pgoyette
1351 1.6.2.2 pgoyette for (i = 0; i < npages; i++) {
1352 1.6.2.2 pgoyette pmap_kenter_pa(_va + i * PAGE_SIZE, _pa + i * PAGE_SIZE,
1353 1.6.2.2 pgoyette VM_PROT_READ | VM_PROT_WRITE, PMAP_WRITE_BACK);
1354 1.6.2.2 pgoyette }
1355 1.6.2.2 pgoyette pmap_update(pmap_kernel());
1356 1.6.2.2 pgoyette
1357 1.6.2.2 pgoyette memset((void *)_va, 0, npages * PAGE_SIZE);
1358 1.6.2.2 pgoyette
1359 1.6.2.2 pgoyette *pa = _pa;
1360 1.6.2.2 pgoyette *va = _va;
1361 1.6.2.2 pgoyette return 0;
1362 1.6.2.2 pgoyette
1363 1.6.2.2 pgoyette error:
1364 1.6.2.2 pgoyette for (i = 0; i < npages; i++) {
1365 1.6.2.2 pgoyette uvm_pagefree(PHYS_TO_VM_PAGE(_pa + i * PAGE_SIZE));
1366 1.6.2.2 pgoyette }
1367 1.6.2.2 pgoyette return ENOMEM;
1368 1.6.2.2 pgoyette }
1369 1.6.2.2 pgoyette
1370 1.6.2.2 pgoyette static void
1371 1.6.2.2 pgoyette svm_memfree(paddr_t pa, vaddr_t va, size_t npages)
1372 1.6.2.2 pgoyette {
1373 1.6.2.2 pgoyette size_t i;
1374 1.6.2.2 pgoyette
1375 1.6.2.2 pgoyette pmap_kremove(va, npages * PAGE_SIZE);
1376 1.6.2.2 pgoyette pmap_update(pmap_kernel());
1377 1.6.2.2 pgoyette uvm_km_free(kernel_map, va, npages * PAGE_SIZE, UVM_KMF_VAONLY);
1378 1.6.2.2 pgoyette for (i = 0; i < npages; i++) {
1379 1.6.2.2 pgoyette uvm_pagefree(PHYS_TO_VM_PAGE(pa + i * PAGE_SIZE));
1380 1.6.2.2 pgoyette }
1381 1.6.2.2 pgoyette }
1382 1.6.2.2 pgoyette
1383 1.6.2.2 pgoyette /* -------------------------------------------------------------------------- */
1384 1.6.2.2 pgoyette
1385 1.6.2.2 pgoyette #define SVM_MSRBM_READ __BIT(0)
1386 1.6.2.2 pgoyette #define SVM_MSRBM_WRITE __BIT(1)
1387 1.6.2.2 pgoyette
1388 1.6.2.2 pgoyette static void
1389 1.6.2.2 pgoyette svm_vcpu_msr_allow(uint8_t *bitmap, uint64_t msr, bool read, bool write)
1390 1.6.2.2 pgoyette {
1391 1.6.2.2 pgoyette uint64_t byte;
1392 1.6.2.2 pgoyette uint8_t bitoff;
1393 1.6.2.2 pgoyette
1394 1.6.2.2 pgoyette if (msr < 0x00002000) {
1395 1.6.2.2 pgoyette /* Range 1 */
1396 1.6.2.2 pgoyette byte = ((msr - 0x00000000) >> 2UL) + 0x0000;
1397 1.6.2.2 pgoyette } else if (msr >= 0xC0000000 && msr < 0xC0002000) {
1398 1.6.2.2 pgoyette /* Range 2 */
1399 1.6.2.2 pgoyette byte = ((msr - 0xC0000000) >> 2UL) + 0x0800;
1400 1.6.2.2 pgoyette } else if (msr >= 0xC0010000 && msr < 0xC0012000) {
1401 1.6.2.2 pgoyette /* Range 3 */
1402 1.6.2.2 pgoyette byte = ((msr - 0xC0010000) >> 2UL) + 0x1000;
1403 1.6.2.2 pgoyette } else {
1404 1.6.2.2 pgoyette panic("%s: wrong range", __func__);
1405 1.6.2.2 pgoyette }
1406 1.6.2.2 pgoyette
1407 1.6.2.2 pgoyette bitoff = (msr & 0x3) << 1;
1408 1.6.2.2 pgoyette
1409 1.6.2.2 pgoyette if (read) {
1410 1.6.2.2 pgoyette bitmap[byte] &= ~(SVM_MSRBM_READ << bitoff);
1411 1.6.2.2 pgoyette }
1412 1.6.2.2 pgoyette if (write) {
1413 1.6.2.2 pgoyette bitmap[byte] &= ~(SVM_MSRBM_WRITE << bitoff);
1414 1.6.2.2 pgoyette }
1415 1.6.2.2 pgoyette }
1416 1.6.2.2 pgoyette
1417 1.6.2.2 pgoyette static void
1418 1.6.2.2 pgoyette svm_asid_alloc(struct nvmm_cpu *vcpu)
1419 1.6.2.2 pgoyette {
1420 1.6.2.2 pgoyette struct svm_cpudata *cpudata = vcpu->cpudata;
1421 1.6.2.2 pgoyette struct vmcb *vmcb = cpudata->vmcb;
1422 1.6.2.2 pgoyette size_t i, oct, bit;
1423 1.6.2.2 pgoyette
1424 1.6.2.2 pgoyette mutex_enter(&svm_asidlock);
1425 1.6.2.2 pgoyette
1426 1.6.2.2 pgoyette for (i = 0; i < svm_maxasid; i++) {
1427 1.6.2.2 pgoyette oct = i / 8;
1428 1.6.2.2 pgoyette bit = i % 8;
1429 1.6.2.2 pgoyette
1430 1.6.2.2 pgoyette if (svm_asidmap[oct] & __BIT(bit)) {
1431 1.6.2.2 pgoyette continue;
1432 1.6.2.2 pgoyette }
1433 1.6.2.2 pgoyette
1434 1.6.2.2 pgoyette svm_asidmap[oct] |= __BIT(bit);
1435 1.6.2.2 pgoyette vmcb->ctrl.guest_asid = i;
1436 1.6.2.2 pgoyette mutex_exit(&svm_asidlock);
1437 1.6.2.2 pgoyette return;
1438 1.6.2.2 pgoyette }
1439 1.6.2.2 pgoyette
1440 1.6.2.2 pgoyette /*
1441 1.6.2.2 pgoyette * No free ASID. Use the last one, which is shared and requires
1442 1.6.2.2 pgoyette * special TLB handling.
1443 1.6.2.2 pgoyette */
1444 1.6.2.2 pgoyette cpudata->shared_asid = true;
1445 1.6.2.2 pgoyette vmcb->ctrl.guest_asid = svm_maxasid - 1;
1446 1.6.2.2 pgoyette mutex_exit(&svm_asidlock);
1447 1.6.2.2 pgoyette }
1448 1.6.2.2 pgoyette
1449 1.6.2.2 pgoyette static void
1450 1.6.2.2 pgoyette svm_asid_free(struct nvmm_cpu *vcpu)
1451 1.6.2.2 pgoyette {
1452 1.6.2.2 pgoyette struct svm_cpudata *cpudata = vcpu->cpudata;
1453 1.6.2.2 pgoyette struct vmcb *vmcb = cpudata->vmcb;
1454 1.6.2.2 pgoyette size_t oct, bit;
1455 1.6.2.2 pgoyette
1456 1.6.2.2 pgoyette if (cpudata->shared_asid) {
1457 1.6.2.2 pgoyette return;
1458 1.6.2.2 pgoyette }
1459 1.6.2.2 pgoyette
1460 1.6.2.2 pgoyette oct = vmcb->ctrl.guest_asid / 8;
1461 1.6.2.2 pgoyette bit = vmcb->ctrl.guest_asid % 8;
1462 1.6.2.2 pgoyette
1463 1.6.2.2 pgoyette mutex_enter(&svm_asidlock);
1464 1.6.2.2 pgoyette svm_asidmap[oct] &= ~__BIT(bit);
1465 1.6.2.2 pgoyette mutex_exit(&svm_asidlock);
1466 1.6.2.2 pgoyette }
1467 1.6.2.2 pgoyette
1468 1.6.2.2 pgoyette static void
1469 1.6.2.2 pgoyette svm_vcpu_init(struct nvmm_machine *mach, struct nvmm_cpu *vcpu)
1470 1.6.2.2 pgoyette {
1471 1.6.2.2 pgoyette struct svm_cpudata *cpudata = vcpu->cpudata;
1472 1.6.2.2 pgoyette struct vmcb *vmcb = cpudata->vmcb;
1473 1.6.2.2 pgoyette
1474 1.6.2.2 pgoyette /* Allow reads/writes of Control Registers. */
1475 1.6.2.2 pgoyette vmcb->ctrl.intercept_cr = 0;
1476 1.6.2.2 pgoyette
1477 1.6.2.2 pgoyette /* Allow reads/writes of Debug Registers. */
1478 1.6.2.2 pgoyette vmcb->ctrl.intercept_dr = 0;
1479 1.6.2.2 pgoyette
1480 1.6.2.2 pgoyette /* Allow exceptions 0 to 31. */
1481 1.6.2.2 pgoyette vmcb->ctrl.intercept_vec = 0;
1482 1.6.2.2 pgoyette
1483 1.6.2.2 pgoyette /*
1484 1.6.2.2 pgoyette * Allow:
1485 1.6.2.2 pgoyette * - SMI [smm interrupts]
1486 1.6.2.2 pgoyette * - VINTR [virtual interrupts]
1487 1.6.2.2 pgoyette * - CR0_SPEC [CR0 writes changing other fields than CR0.TS or CR0.MP]
1488 1.6.2.2 pgoyette * - RIDTR [reads of IDTR]
1489 1.6.2.2 pgoyette * - RGDTR [reads of GDTR]
1490 1.6.2.2 pgoyette * - RLDTR [reads of LDTR]
1491 1.6.2.2 pgoyette * - RTR [reads of TR]
1492 1.6.2.2 pgoyette * - WIDTR [writes of IDTR]
1493 1.6.2.2 pgoyette * - WGDTR [writes of GDTR]
1494 1.6.2.2 pgoyette * - WLDTR [writes of LDTR]
1495 1.6.2.2 pgoyette * - WTR [writes of TR]
1496 1.6.2.2 pgoyette * - RDTSC [rdtsc instruction]
1497 1.6.2.2 pgoyette * - PUSHF [pushf instruction]
1498 1.6.2.2 pgoyette * - POPF [popf instruction]
1499 1.6.2.2 pgoyette * - IRET [iret instruction]
1500 1.6.2.2 pgoyette * - INTN [int $n instructions]
1501 1.6.2.2 pgoyette * - INVD [invd instruction]
1502 1.6.2.2 pgoyette * - PAUSE [pause instruction]
1503 1.6.2.2 pgoyette * - INVLPG [invplg instruction]
1504 1.6.2.2 pgoyette * - TASKSW [task switches]
1505 1.6.2.2 pgoyette *
1506 1.6.2.2 pgoyette * Intercept the rest below.
1507 1.6.2.2 pgoyette */
1508 1.6.2.2 pgoyette vmcb->ctrl.intercept_misc1 =
1509 1.6.2.2 pgoyette VMCB_CTRL_INTERCEPT_INTR |
1510 1.6.2.2 pgoyette VMCB_CTRL_INTERCEPT_NMI |
1511 1.6.2.2 pgoyette VMCB_CTRL_INTERCEPT_INIT |
1512 1.6.2.2 pgoyette VMCB_CTRL_INTERCEPT_RDPMC |
1513 1.6.2.2 pgoyette VMCB_CTRL_INTERCEPT_CPUID |
1514 1.6.2.2 pgoyette VMCB_CTRL_INTERCEPT_RSM |
1515 1.6.2.2 pgoyette VMCB_CTRL_INTERCEPT_HLT |
1516 1.6.2.2 pgoyette VMCB_CTRL_INTERCEPT_INVLPGA |
1517 1.6.2.2 pgoyette VMCB_CTRL_INTERCEPT_IOIO_PROT |
1518 1.6.2.2 pgoyette VMCB_CTRL_INTERCEPT_MSR_PROT |
1519 1.6.2.2 pgoyette VMCB_CTRL_INTERCEPT_FERR_FREEZE |
1520 1.6.2.2 pgoyette VMCB_CTRL_INTERCEPT_SHUTDOWN;
1521 1.6.2.2 pgoyette
1522 1.6.2.2 pgoyette /*
1523 1.6.2.2 pgoyette * Allow:
1524 1.6.2.2 pgoyette * - ICEBP [icebp instruction]
1525 1.6.2.2 pgoyette * - WBINVD [wbinvd instruction]
1526 1.6.2.2 pgoyette * - WCR_SPEC(0..15) [writes of CR0-15, received after instruction]
1527 1.6.2.2 pgoyette *
1528 1.6.2.2 pgoyette * Intercept the rest below.
1529 1.6.2.2 pgoyette */
1530 1.6.2.2 pgoyette vmcb->ctrl.intercept_misc2 =
1531 1.6.2.2 pgoyette VMCB_CTRL_INTERCEPT_VMRUN |
1532 1.6.2.2 pgoyette VMCB_CTRL_INTERCEPT_VMMCALL |
1533 1.6.2.2 pgoyette VMCB_CTRL_INTERCEPT_VMLOAD |
1534 1.6.2.2 pgoyette VMCB_CTRL_INTERCEPT_VMSAVE |
1535 1.6.2.2 pgoyette VMCB_CTRL_INTERCEPT_STGI |
1536 1.6.2.2 pgoyette VMCB_CTRL_INTERCEPT_CLGI |
1537 1.6.2.2 pgoyette VMCB_CTRL_INTERCEPT_SKINIT |
1538 1.6.2.2 pgoyette VMCB_CTRL_INTERCEPT_RDTSCP |
1539 1.6.2.2 pgoyette VMCB_CTRL_INTERCEPT_MONITOR |
1540 1.6.2.2 pgoyette VMCB_CTRL_INTERCEPT_MWAIT |
1541 1.6.2.2 pgoyette VMCB_CTRL_INTERCEPT_XSETBV;
1542 1.6.2.2 pgoyette
1543 1.6.2.2 pgoyette /* Intercept all I/O accesses. */
1544 1.6.2.2 pgoyette memset(cpudata->iobm, 0xFF, IOBM_SIZE);
1545 1.6.2.2 pgoyette vmcb->ctrl.iopm_base_pa = cpudata->iobm_pa;
1546 1.6.2.2 pgoyette
1547 1.6.2.2 pgoyette /*
1548 1.6.2.2 pgoyette * Allow:
1549 1.6.2.2 pgoyette * - EFER [read]
1550 1.6.2.2 pgoyette * - STAR [read, write]
1551 1.6.2.2 pgoyette * - LSTAR [read, write]
1552 1.6.2.2 pgoyette * - CSTAR [read, write]
1553 1.6.2.2 pgoyette * - SFMASK [read, write]
1554 1.6.2.2 pgoyette * - KERNELGSBASE [read, write]
1555 1.6.2.2 pgoyette * - SYSENTER_CS [read, write]
1556 1.6.2.2 pgoyette * - SYSENTER_ESP [read, write]
1557 1.6.2.2 pgoyette * - SYSENTER_EIP [read, write]
1558 1.6.2.2 pgoyette * - FSBASE [read, write]
1559 1.6.2.2 pgoyette * - GSBASE [read, write]
1560 1.6.2.4 pgoyette * - TSC [read]
1561 1.6.2.2 pgoyette *
1562 1.6.2.2 pgoyette * Intercept the rest.
1563 1.6.2.2 pgoyette */
1564 1.6.2.2 pgoyette memset(cpudata->msrbm, 0xFF, MSRBM_SIZE);
1565 1.6.2.2 pgoyette svm_vcpu_msr_allow(cpudata->msrbm, MSR_EFER, true, false);
1566 1.6.2.2 pgoyette svm_vcpu_msr_allow(cpudata->msrbm, MSR_STAR, true, true);
1567 1.6.2.2 pgoyette svm_vcpu_msr_allow(cpudata->msrbm, MSR_LSTAR, true, true);
1568 1.6.2.2 pgoyette svm_vcpu_msr_allow(cpudata->msrbm, MSR_CSTAR, true, true);
1569 1.6.2.2 pgoyette svm_vcpu_msr_allow(cpudata->msrbm, MSR_SFMASK, true, true);
1570 1.6.2.2 pgoyette svm_vcpu_msr_allow(cpudata->msrbm, MSR_KERNELGSBASE, true, true);
1571 1.6.2.2 pgoyette svm_vcpu_msr_allow(cpudata->msrbm, MSR_SYSENTER_CS, true, true);
1572 1.6.2.2 pgoyette svm_vcpu_msr_allow(cpudata->msrbm, MSR_SYSENTER_ESP, true, true);
1573 1.6.2.2 pgoyette svm_vcpu_msr_allow(cpudata->msrbm, MSR_SYSENTER_EIP, true, true);
1574 1.6.2.2 pgoyette svm_vcpu_msr_allow(cpudata->msrbm, MSR_FSBASE, true, true);
1575 1.6.2.2 pgoyette svm_vcpu_msr_allow(cpudata->msrbm, MSR_GSBASE, true, true);
1576 1.6.2.4 pgoyette svm_vcpu_msr_allow(cpudata->msrbm, MSR_TSC, true, false);
1577 1.6.2.2 pgoyette vmcb->ctrl.msrpm_base_pa = cpudata->msrbm_pa;
1578 1.6.2.2 pgoyette
1579 1.6.2.2 pgoyette /* Generate ASID. */
1580 1.6.2.2 pgoyette svm_asid_alloc(vcpu);
1581 1.6.2.2 pgoyette
1582 1.6.2.2 pgoyette /* Virtual TPR. */
1583 1.6.2.2 pgoyette vmcb->ctrl.v = VMCB_CTRL_V_INTR_MASKING;
1584 1.6.2.2 pgoyette
1585 1.6.2.2 pgoyette /* Enable Nested Paging. */
1586 1.6.2.2 pgoyette vmcb->ctrl.enable1 = VMCB_CTRL_ENABLE_NP;
1587 1.6.2.2 pgoyette vmcb->ctrl.n_cr3 = mach->vm->vm_map.pmap->pm_pdirpa[0];
1588 1.6.2.2 pgoyette
1589 1.6.2.2 pgoyette /* Must always be set. */
1590 1.6.2.2 pgoyette vmcb->state.efer = EFER_SVME;
1591 1.6.2.5 pgoyette cpudata->gxcr0 = XCR0_X87;
1592 1.6.2.2 pgoyette
1593 1.6.2.2 pgoyette /* Init XSAVE header. */
1594 1.6.2.2 pgoyette cpudata->gfpu.xsh_xstate_bv = svm_xcr0_mask;
1595 1.6.2.2 pgoyette cpudata->gfpu.xsh_xcomp_bv = 0;
1596 1.6.2.2 pgoyette
1597 1.6.2.2 pgoyette /* Bluntly hide the host TSC. */
1598 1.6.2.2 pgoyette cpudata->tsc_offset = rdtsc();
1599 1.6.2.2 pgoyette }
1600 1.6.2.2 pgoyette
1601 1.6.2.2 pgoyette static int
1602 1.6.2.2 pgoyette svm_vcpu_create(struct nvmm_machine *mach, struct nvmm_cpu *vcpu)
1603 1.6.2.2 pgoyette {
1604 1.6.2.2 pgoyette struct svm_cpudata *cpudata;
1605 1.6.2.2 pgoyette int error;
1606 1.6.2.2 pgoyette
1607 1.6.2.2 pgoyette /* Allocate the SVM cpudata. */
1608 1.6.2.2 pgoyette cpudata = (struct svm_cpudata *)uvm_km_alloc(kernel_map,
1609 1.6.2.2 pgoyette roundup(sizeof(*cpudata), PAGE_SIZE), 0,
1610 1.6.2.2 pgoyette UVM_KMF_WIRED|UVM_KMF_ZERO);
1611 1.6.2.2 pgoyette vcpu->cpudata = cpudata;
1612 1.6.2.2 pgoyette
1613 1.6.2.2 pgoyette /* VMCB */
1614 1.6.2.2 pgoyette error = svm_memalloc(&cpudata->vmcb_pa, (vaddr_t *)&cpudata->vmcb,
1615 1.6.2.2 pgoyette VMCB_NPAGES);
1616 1.6.2.2 pgoyette if (error)
1617 1.6.2.2 pgoyette goto error;
1618 1.6.2.2 pgoyette
1619 1.6.2.2 pgoyette /* I/O Bitmap */
1620 1.6.2.2 pgoyette error = svm_memalloc(&cpudata->iobm_pa, (vaddr_t *)&cpudata->iobm,
1621 1.6.2.2 pgoyette IOBM_NPAGES);
1622 1.6.2.2 pgoyette if (error)
1623 1.6.2.2 pgoyette goto error;
1624 1.6.2.2 pgoyette
1625 1.6.2.2 pgoyette /* MSR Bitmap */
1626 1.6.2.2 pgoyette error = svm_memalloc(&cpudata->msrbm_pa, (vaddr_t *)&cpudata->msrbm,
1627 1.6.2.2 pgoyette MSRBM_NPAGES);
1628 1.6.2.2 pgoyette if (error)
1629 1.6.2.2 pgoyette goto error;
1630 1.6.2.2 pgoyette
1631 1.6.2.2 pgoyette /* Init the VCPU info. */
1632 1.6.2.2 pgoyette svm_vcpu_init(mach, vcpu);
1633 1.6.2.2 pgoyette
1634 1.6.2.2 pgoyette return 0;
1635 1.6.2.2 pgoyette
1636 1.6.2.2 pgoyette error:
1637 1.6.2.2 pgoyette if (cpudata->vmcb_pa) {
1638 1.6.2.2 pgoyette svm_memfree(cpudata->vmcb_pa, (vaddr_t)cpudata->vmcb,
1639 1.6.2.2 pgoyette VMCB_NPAGES);
1640 1.6.2.2 pgoyette }
1641 1.6.2.2 pgoyette if (cpudata->iobm_pa) {
1642 1.6.2.2 pgoyette svm_memfree(cpudata->iobm_pa, (vaddr_t)cpudata->iobm,
1643 1.6.2.2 pgoyette IOBM_NPAGES);
1644 1.6.2.2 pgoyette }
1645 1.6.2.2 pgoyette if (cpudata->msrbm_pa) {
1646 1.6.2.2 pgoyette svm_memfree(cpudata->msrbm_pa, (vaddr_t)cpudata->msrbm,
1647 1.6.2.2 pgoyette MSRBM_NPAGES);
1648 1.6.2.2 pgoyette }
1649 1.6.2.2 pgoyette uvm_km_free(kernel_map, (vaddr_t)cpudata,
1650 1.6.2.2 pgoyette roundup(sizeof(*cpudata), PAGE_SIZE), UVM_KMF_WIRED);
1651 1.6.2.2 pgoyette return error;
1652 1.6.2.2 pgoyette }
1653 1.6.2.2 pgoyette
1654 1.6.2.2 pgoyette static void
1655 1.6.2.2 pgoyette svm_vcpu_destroy(struct nvmm_machine *mach, struct nvmm_cpu *vcpu)
1656 1.6.2.2 pgoyette {
1657 1.6.2.2 pgoyette struct svm_cpudata *cpudata = vcpu->cpudata;
1658 1.6.2.2 pgoyette
1659 1.6.2.2 pgoyette svm_asid_free(vcpu);
1660 1.6.2.2 pgoyette
1661 1.6.2.2 pgoyette svm_memfree(cpudata->vmcb_pa, (vaddr_t)cpudata->vmcb, VMCB_NPAGES);
1662 1.6.2.2 pgoyette svm_memfree(cpudata->iobm_pa, (vaddr_t)cpudata->iobm, IOBM_NPAGES);
1663 1.6.2.2 pgoyette svm_memfree(cpudata->msrbm_pa, (vaddr_t)cpudata->msrbm, MSRBM_NPAGES);
1664 1.6.2.2 pgoyette
1665 1.6.2.2 pgoyette uvm_km_free(kernel_map, (vaddr_t)cpudata,
1666 1.6.2.2 pgoyette roundup(sizeof(*cpudata), PAGE_SIZE), UVM_KMF_WIRED);
1667 1.6.2.2 pgoyette }
1668 1.6.2.2 pgoyette
1669 1.6.2.2 pgoyette #define SVM_SEG_ATTRIB_TYPE __BITS(4,0)
1670 1.6.2.2 pgoyette #define SVM_SEG_ATTRIB_DPL __BITS(6,5)
1671 1.6.2.2 pgoyette #define SVM_SEG_ATTRIB_P __BIT(7)
1672 1.6.2.2 pgoyette #define SVM_SEG_ATTRIB_AVL __BIT(8)
1673 1.6.2.2 pgoyette #define SVM_SEG_ATTRIB_LONG __BIT(9)
1674 1.6.2.2 pgoyette #define SVM_SEG_ATTRIB_DEF32 __BIT(10)
1675 1.6.2.2 pgoyette #define SVM_SEG_ATTRIB_GRAN __BIT(11)
1676 1.6.2.2 pgoyette
1677 1.6.2.2 pgoyette static void
1678 1.6.2.2 pgoyette svm_vcpu_setstate_seg(struct nvmm_x64_state_seg *seg, struct vmcb_segment *vseg)
1679 1.6.2.2 pgoyette {
1680 1.6.2.2 pgoyette vseg->selector = seg->selector;
1681 1.6.2.2 pgoyette vseg->attrib =
1682 1.6.2.2 pgoyette __SHIFTIN(seg->attrib.type, SVM_SEG_ATTRIB_TYPE) |
1683 1.6.2.2 pgoyette __SHIFTIN(seg->attrib.dpl, SVM_SEG_ATTRIB_DPL) |
1684 1.6.2.2 pgoyette __SHIFTIN(seg->attrib.p, SVM_SEG_ATTRIB_P) |
1685 1.6.2.2 pgoyette __SHIFTIN(seg->attrib.avl, SVM_SEG_ATTRIB_AVL) |
1686 1.6.2.2 pgoyette __SHIFTIN(seg->attrib.lng, SVM_SEG_ATTRIB_LONG) |
1687 1.6.2.2 pgoyette __SHIFTIN(seg->attrib.def32, SVM_SEG_ATTRIB_DEF32) |
1688 1.6.2.2 pgoyette __SHIFTIN(seg->attrib.gran, SVM_SEG_ATTRIB_GRAN);
1689 1.6.2.2 pgoyette vseg->limit = seg->limit;
1690 1.6.2.2 pgoyette vseg->base = seg->base;
1691 1.6.2.2 pgoyette }
1692 1.6.2.2 pgoyette
1693 1.6.2.2 pgoyette static void
1694 1.6.2.2 pgoyette svm_vcpu_getstate_seg(struct nvmm_x64_state_seg *seg, struct vmcb_segment *vseg)
1695 1.6.2.2 pgoyette {
1696 1.6.2.2 pgoyette seg->selector = vseg->selector;
1697 1.6.2.2 pgoyette seg->attrib.type = __SHIFTOUT(vseg->attrib, SVM_SEG_ATTRIB_TYPE);
1698 1.6.2.2 pgoyette seg->attrib.dpl = __SHIFTOUT(vseg->attrib, SVM_SEG_ATTRIB_DPL);
1699 1.6.2.2 pgoyette seg->attrib.p = __SHIFTOUT(vseg->attrib, SVM_SEG_ATTRIB_P);
1700 1.6.2.2 pgoyette seg->attrib.avl = __SHIFTOUT(vseg->attrib, SVM_SEG_ATTRIB_AVL);
1701 1.6.2.2 pgoyette seg->attrib.lng = __SHIFTOUT(vseg->attrib, SVM_SEG_ATTRIB_LONG);
1702 1.6.2.2 pgoyette seg->attrib.def32 = __SHIFTOUT(vseg->attrib, SVM_SEG_ATTRIB_DEF32);
1703 1.6.2.2 pgoyette seg->attrib.gran = __SHIFTOUT(vseg->attrib, SVM_SEG_ATTRIB_GRAN);
1704 1.6.2.2 pgoyette seg->limit = vseg->limit;
1705 1.6.2.2 pgoyette seg->base = vseg->base;
1706 1.6.2.2 pgoyette }
1707 1.6.2.2 pgoyette
1708 1.6.2.4 pgoyette static inline bool
1709 1.6.2.4 pgoyette svm_state_tlb_flush(struct vmcb *vmcb, struct nvmm_x64_state *state,
1710 1.6.2.4 pgoyette uint64_t flags)
1711 1.6.2.2 pgoyette {
1712 1.6.2.2 pgoyette if (flags & NVMM_X64_STATE_CRS) {
1713 1.6.2.4 pgoyette if ((vmcb->state.cr0 ^
1714 1.6.2.4 pgoyette state->crs[NVMM_X64_CR_CR0]) & CR0_TLB_FLUSH) {
1715 1.6.2.2 pgoyette return true;
1716 1.6.2.2 pgoyette }
1717 1.6.2.4 pgoyette if (vmcb->state.cr3 != state->crs[NVMM_X64_CR_CR3]) {
1718 1.6.2.2 pgoyette return true;
1719 1.6.2.2 pgoyette }
1720 1.6.2.4 pgoyette if ((vmcb->state.cr4 ^
1721 1.6.2.4 pgoyette state->crs[NVMM_X64_CR_CR4]) & CR4_TLB_FLUSH) {
1722 1.6.2.2 pgoyette return true;
1723 1.6.2.2 pgoyette }
1724 1.6.2.2 pgoyette }
1725 1.6.2.2 pgoyette
1726 1.6.2.2 pgoyette if (flags & NVMM_X64_STATE_MSRS) {
1727 1.6.2.4 pgoyette if ((vmcb->state.efer ^
1728 1.6.2.4 pgoyette state->msrs[NVMM_X64_MSR_EFER]) & EFER_TLB_FLUSH) {
1729 1.6.2.2 pgoyette return true;
1730 1.6.2.2 pgoyette }
1731 1.6.2.2 pgoyette }
1732 1.6.2.2 pgoyette
1733 1.6.2.2 pgoyette return false;
1734 1.6.2.2 pgoyette }
1735 1.6.2.2 pgoyette
1736 1.6.2.2 pgoyette static void
1737 1.6.2.2 pgoyette svm_vcpu_setstate(struct nvmm_cpu *vcpu, void *data, uint64_t flags)
1738 1.6.2.2 pgoyette {
1739 1.6.2.4 pgoyette struct nvmm_x64_state *state = (struct nvmm_x64_state *)data;
1740 1.6.2.2 pgoyette struct svm_cpudata *cpudata = vcpu->cpudata;
1741 1.6.2.2 pgoyette struct vmcb *vmcb = cpudata->vmcb;
1742 1.6.2.2 pgoyette struct fxsave *fpustate;
1743 1.6.2.2 pgoyette
1744 1.6.2.4 pgoyette if (svm_state_tlb_flush(vmcb, state, flags)) {
1745 1.6.2.2 pgoyette cpudata->tlb_want_flush = true;
1746 1.6.2.2 pgoyette }
1747 1.6.2.2 pgoyette
1748 1.6.2.2 pgoyette if (flags & NVMM_X64_STATE_SEGS) {
1749 1.6.2.4 pgoyette svm_vcpu_setstate_seg(&state->segs[NVMM_X64_SEG_CS],
1750 1.6.2.2 pgoyette &vmcb->state.cs);
1751 1.6.2.4 pgoyette svm_vcpu_setstate_seg(&state->segs[NVMM_X64_SEG_DS],
1752 1.6.2.2 pgoyette &vmcb->state.ds);
1753 1.6.2.4 pgoyette svm_vcpu_setstate_seg(&state->segs[NVMM_X64_SEG_ES],
1754 1.6.2.2 pgoyette &vmcb->state.es);
1755 1.6.2.4 pgoyette svm_vcpu_setstate_seg(&state->segs[NVMM_X64_SEG_FS],
1756 1.6.2.2 pgoyette &vmcb->state.fs);
1757 1.6.2.4 pgoyette svm_vcpu_setstate_seg(&state->segs[NVMM_X64_SEG_GS],
1758 1.6.2.2 pgoyette &vmcb->state.gs);
1759 1.6.2.4 pgoyette svm_vcpu_setstate_seg(&state->segs[NVMM_X64_SEG_SS],
1760 1.6.2.2 pgoyette &vmcb->state.ss);
1761 1.6.2.4 pgoyette svm_vcpu_setstate_seg(&state->segs[NVMM_X64_SEG_GDT],
1762 1.6.2.2 pgoyette &vmcb->state.gdt);
1763 1.6.2.4 pgoyette svm_vcpu_setstate_seg(&state->segs[NVMM_X64_SEG_IDT],
1764 1.6.2.2 pgoyette &vmcb->state.idt);
1765 1.6.2.4 pgoyette svm_vcpu_setstate_seg(&state->segs[NVMM_X64_SEG_LDT],
1766 1.6.2.2 pgoyette &vmcb->state.ldt);
1767 1.6.2.4 pgoyette svm_vcpu_setstate_seg(&state->segs[NVMM_X64_SEG_TR],
1768 1.6.2.2 pgoyette &vmcb->state.tr);
1769 1.6.2.2 pgoyette }
1770 1.6.2.2 pgoyette
1771 1.6.2.4 pgoyette CTASSERT(sizeof(cpudata->gprs) == sizeof(state->gprs));
1772 1.6.2.2 pgoyette if (flags & NVMM_X64_STATE_GPRS) {
1773 1.6.2.4 pgoyette memcpy(cpudata->gprs, state->gprs, sizeof(state->gprs));
1774 1.6.2.2 pgoyette
1775 1.6.2.4 pgoyette vmcb->state.rip = state->gprs[NVMM_X64_GPR_RIP];
1776 1.6.2.4 pgoyette vmcb->state.rsp = state->gprs[NVMM_X64_GPR_RSP];
1777 1.6.2.4 pgoyette vmcb->state.rax = state->gprs[NVMM_X64_GPR_RAX];
1778 1.6.2.4 pgoyette vmcb->state.rflags = state->gprs[NVMM_X64_GPR_RFLAGS];
1779 1.6.2.2 pgoyette }
1780 1.6.2.2 pgoyette
1781 1.6.2.2 pgoyette if (flags & NVMM_X64_STATE_CRS) {
1782 1.6.2.4 pgoyette vmcb->state.cr0 = state->crs[NVMM_X64_CR_CR0];
1783 1.6.2.4 pgoyette vmcb->state.cr2 = state->crs[NVMM_X64_CR_CR2];
1784 1.6.2.4 pgoyette vmcb->state.cr3 = state->crs[NVMM_X64_CR_CR3];
1785 1.6.2.4 pgoyette vmcb->state.cr4 = state->crs[NVMM_X64_CR_CR4];
1786 1.6.2.2 pgoyette
1787 1.6.2.2 pgoyette vmcb->ctrl.v &= ~VMCB_CTRL_V_TPR;
1788 1.6.2.4 pgoyette vmcb->ctrl.v |= __SHIFTIN(state->crs[NVMM_X64_CR_CR8],
1789 1.6.2.2 pgoyette VMCB_CTRL_V_TPR);
1790 1.6.2.2 pgoyette
1791 1.6.2.2 pgoyette if (svm_xcr0_mask != 0) {
1792 1.6.2.5 pgoyette /* Clear illegal XCR0 bits, set mandatory X87 bit. */
1793 1.6.2.4 pgoyette cpudata->gxcr0 = state->crs[NVMM_X64_CR_XCR0];
1794 1.6.2.4 pgoyette cpudata->gxcr0 &= svm_xcr0_mask;
1795 1.6.2.4 pgoyette cpudata->gxcr0 |= XCR0_X87;
1796 1.6.2.2 pgoyette }
1797 1.6.2.2 pgoyette }
1798 1.6.2.2 pgoyette
1799 1.6.2.4 pgoyette CTASSERT(sizeof(cpudata->drs) == sizeof(state->drs));
1800 1.6.2.2 pgoyette if (flags & NVMM_X64_STATE_DRS) {
1801 1.6.2.4 pgoyette memcpy(cpudata->drs, state->drs, sizeof(state->drs));
1802 1.6.2.2 pgoyette
1803 1.6.2.4 pgoyette vmcb->state.dr6 = state->drs[NVMM_X64_DR_DR6];
1804 1.6.2.4 pgoyette vmcb->state.dr7 = state->drs[NVMM_X64_DR_DR7];
1805 1.6.2.2 pgoyette }
1806 1.6.2.2 pgoyette
1807 1.6.2.2 pgoyette if (flags & NVMM_X64_STATE_MSRS) {
1808 1.6.2.2 pgoyette /* Bit EFER_SVME is mandatory. */
1809 1.6.2.4 pgoyette vmcb->state.efer = state->msrs[NVMM_X64_MSR_EFER] | EFER_SVME;
1810 1.6.2.2 pgoyette
1811 1.6.2.4 pgoyette vmcb->state.star = state->msrs[NVMM_X64_MSR_STAR];
1812 1.6.2.4 pgoyette vmcb->state.lstar = state->msrs[NVMM_X64_MSR_LSTAR];
1813 1.6.2.4 pgoyette vmcb->state.cstar = state->msrs[NVMM_X64_MSR_CSTAR];
1814 1.6.2.4 pgoyette vmcb->state.sfmask = state->msrs[NVMM_X64_MSR_SFMASK];
1815 1.6.2.2 pgoyette vmcb->state.kernelgsbase =
1816 1.6.2.4 pgoyette state->msrs[NVMM_X64_MSR_KERNELGSBASE];
1817 1.6.2.2 pgoyette vmcb->state.sysenter_cs =
1818 1.6.2.4 pgoyette state->msrs[NVMM_X64_MSR_SYSENTER_CS];
1819 1.6.2.2 pgoyette vmcb->state.sysenter_esp =
1820 1.6.2.4 pgoyette state->msrs[NVMM_X64_MSR_SYSENTER_ESP];
1821 1.6.2.2 pgoyette vmcb->state.sysenter_eip =
1822 1.6.2.4 pgoyette state->msrs[NVMM_X64_MSR_SYSENTER_EIP];
1823 1.6.2.4 pgoyette vmcb->state.g_pat = state->msrs[NVMM_X64_MSR_PAT];
1824 1.6.2.2 pgoyette }
1825 1.6.2.2 pgoyette
1826 1.6.2.2 pgoyette if (flags & NVMM_X64_STATE_MISC) {
1827 1.6.2.4 pgoyette vmcb->state.cpl = state->misc[NVMM_X64_MISC_CPL];
1828 1.6.2.2 pgoyette
1829 1.6.2.4 pgoyette if (state->misc[NVMM_X64_MISC_INT_SHADOW]) {
1830 1.6.2.4 pgoyette vmcb->ctrl.intr |= VMCB_CTRL_INTR_SHADOW;
1831 1.6.2.4 pgoyette } else {
1832 1.6.2.4 pgoyette vmcb->ctrl.intr &= ~VMCB_CTRL_INTR_SHADOW;
1833 1.6.2.4 pgoyette }
1834 1.6.2.4 pgoyette
1835 1.6.2.4 pgoyette if (state->misc[NVMM_X64_MISC_INT_WINDOW_EXIT]) {
1836 1.6.2.4 pgoyette svm_event_waitexit_enable(vcpu, false);
1837 1.6.2.4 pgoyette } else {
1838 1.6.2.4 pgoyette svm_event_waitexit_disable(vcpu, false);
1839 1.6.2.4 pgoyette }
1840 1.6.2.4 pgoyette
1841 1.6.2.4 pgoyette if (state->misc[NVMM_X64_MISC_NMI_WINDOW_EXIT]) {
1842 1.6.2.4 pgoyette svm_event_waitexit_enable(vcpu, true);
1843 1.6.2.4 pgoyette } else {
1844 1.6.2.4 pgoyette svm_event_waitexit_disable(vcpu, true);
1845 1.6.2.4 pgoyette }
1846 1.6.2.2 pgoyette }
1847 1.6.2.2 pgoyette
1848 1.6.2.4 pgoyette CTASSERT(sizeof(cpudata->gfpu.xsh_fxsave) == sizeof(state->fpu));
1849 1.6.2.2 pgoyette if (flags & NVMM_X64_STATE_FPU) {
1850 1.6.2.4 pgoyette memcpy(cpudata->gfpu.xsh_fxsave, &state->fpu,
1851 1.6.2.4 pgoyette sizeof(state->fpu));
1852 1.6.2.2 pgoyette
1853 1.6.2.2 pgoyette fpustate = (struct fxsave *)cpudata->gfpu.xsh_fxsave;
1854 1.6.2.2 pgoyette fpustate->fx_mxcsr_mask &= x86_fpu_mxcsr_mask;
1855 1.6.2.2 pgoyette fpustate->fx_mxcsr &= fpustate->fx_mxcsr_mask;
1856 1.6.2.5 pgoyette
1857 1.6.2.5 pgoyette if (svm_xcr0_mask != 0) {
1858 1.6.2.5 pgoyette /* Reset XSTATE_BV, to force a reload. */
1859 1.6.2.5 pgoyette cpudata->gfpu.xsh_xstate_bv = svm_xcr0_mask;
1860 1.6.2.5 pgoyette }
1861 1.6.2.2 pgoyette }
1862 1.6.2.4 pgoyette
1863 1.6.2.4 pgoyette svm_vmcb_cache_update(vmcb, flags);
1864 1.6.2.2 pgoyette }
1865 1.6.2.2 pgoyette
1866 1.6.2.2 pgoyette static void
1867 1.6.2.2 pgoyette svm_vcpu_getstate(struct nvmm_cpu *vcpu, void *data, uint64_t flags)
1868 1.6.2.2 pgoyette {
1869 1.6.2.4 pgoyette struct nvmm_x64_state *state = (struct nvmm_x64_state *)data;
1870 1.6.2.2 pgoyette struct svm_cpudata *cpudata = vcpu->cpudata;
1871 1.6.2.2 pgoyette struct vmcb *vmcb = cpudata->vmcb;
1872 1.6.2.2 pgoyette
1873 1.6.2.2 pgoyette if (flags & NVMM_X64_STATE_SEGS) {
1874 1.6.2.4 pgoyette svm_vcpu_getstate_seg(&state->segs[NVMM_X64_SEG_CS],
1875 1.6.2.2 pgoyette &vmcb->state.cs);
1876 1.6.2.4 pgoyette svm_vcpu_getstate_seg(&state->segs[NVMM_X64_SEG_DS],
1877 1.6.2.2 pgoyette &vmcb->state.ds);
1878 1.6.2.4 pgoyette svm_vcpu_getstate_seg(&state->segs[NVMM_X64_SEG_ES],
1879 1.6.2.2 pgoyette &vmcb->state.es);
1880 1.6.2.4 pgoyette svm_vcpu_getstate_seg(&state->segs[NVMM_X64_SEG_FS],
1881 1.6.2.2 pgoyette &vmcb->state.fs);
1882 1.6.2.4 pgoyette svm_vcpu_getstate_seg(&state->segs[NVMM_X64_SEG_GS],
1883 1.6.2.2 pgoyette &vmcb->state.gs);
1884 1.6.2.4 pgoyette svm_vcpu_getstate_seg(&state->segs[NVMM_X64_SEG_SS],
1885 1.6.2.2 pgoyette &vmcb->state.ss);
1886 1.6.2.4 pgoyette svm_vcpu_getstate_seg(&state->segs[NVMM_X64_SEG_GDT],
1887 1.6.2.2 pgoyette &vmcb->state.gdt);
1888 1.6.2.4 pgoyette svm_vcpu_getstate_seg(&state->segs[NVMM_X64_SEG_IDT],
1889 1.6.2.2 pgoyette &vmcb->state.idt);
1890 1.6.2.4 pgoyette svm_vcpu_getstate_seg(&state->segs[NVMM_X64_SEG_LDT],
1891 1.6.2.2 pgoyette &vmcb->state.ldt);
1892 1.6.2.4 pgoyette svm_vcpu_getstate_seg(&state->segs[NVMM_X64_SEG_TR],
1893 1.6.2.2 pgoyette &vmcb->state.tr);
1894 1.6.2.2 pgoyette }
1895 1.6.2.2 pgoyette
1896 1.6.2.4 pgoyette CTASSERT(sizeof(cpudata->gprs) == sizeof(state->gprs));
1897 1.6.2.2 pgoyette if (flags & NVMM_X64_STATE_GPRS) {
1898 1.6.2.4 pgoyette memcpy(state->gprs, cpudata->gprs, sizeof(state->gprs));
1899 1.6.2.2 pgoyette
1900 1.6.2.4 pgoyette state->gprs[NVMM_X64_GPR_RIP] = vmcb->state.rip;
1901 1.6.2.4 pgoyette state->gprs[NVMM_X64_GPR_RSP] = vmcb->state.rsp;
1902 1.6.2.4 pgoyette state->gprs[NVMM_X64_GPR_RAX] = vmcb->state.rax;
1903 1.6.2.4 pgoyette state->gprs[NVMM_X64_GPR_RFLAGS] = vmcb->state.rflags;
1904 1.6.2.2 pgoyette }
1905 1.6.2.2 pgoyette
1906 1.6.2.2 pgoyette if (flags & NVMM_X64_STATE_CRS) {
1907 1.6.2.4 pgoyette state->crs[NVMM_X64_CR_CR0] = vmcb->state.cr0;
1908 1.6.2.4 pgoyette state->crs[NVMM_X64_CR_CR2] = vmcb->state.cr2;
1909 1.6.2.4 pgoyette state->crs[NVMM_X64_CR_CR3] = vmcb->state.cr3;
1910 1.6.2.4 pgoyette state->crs[NVMM_X64_CR_CR4] = vmcb->state.cr4;
1911 1.6.2.4 pgoyette state->crs[NVMM_X64_CR_CR8] = __SHIFTOUT(vmcb->ctrl.v,
1912 1.6.2.2 pgoyette VMCB_CTRL_V_TPR);
1913 1.6.2.4 pgoyette state->crs[NVMM_X64_CR_XCR0] = cpudata->gxcr0;
1914 1.6.2.2 pgoyette }
1915 1.6.2.2 pgoyette
1916 1.6.2.4 pgoyette CTASSERT(sizeof(cpudata->drs) == sizeof(state->drs));
1917 1.6.2.2 pgoyette if (flags & NVMM_X64_STATE_DRS) {
1918 1.6.2.4 pgoyette memcpy(state->drs, cpudata->drs, sizeof(state->drs));
1919 1.6.2.2 pgoyette
1920 1.6.2.4 pgoyette state->drs[NVMM_X64_DR_DR6] = vmcb->state.dr6;
1921 1.6.2.4 pgoyette state->drs[NVMM_X64_DR_DR7] = vmcb->state.dr7;
1922 1.6.2.2 pgoyette }
1923 1.6.2.2 pgoyette
1924 1.6.2.2 pgoyette if (flags & NVMM_X64_STATE_MSRS) {
1925 1.6.2.4 pgoyette state->msrs[NVMM_X64_MSR_EFER] = vmcb->state.efer;
1926 1.6.2.4 pgoyette state->msrs[NVMM_X64_MSR_STAR] = vmcb->state.star;
1927 1.6.2.4 pgoyette state->msrs[NVMM_X64_MSR_LSTAR] = vmcb->state.lstar;
1928 1.6.2.4 pgoyette state->msrs[NVMM_X64_MSR_CSTAR] = vmcb->state.cstar;
1929 1.6.2.4 pgoyette state->msrs[NVMM_X64_MSR_SFMASK] = vmcb->state.sfmask;
1930 1.6.2.4 pgoyette state->msrs[NVMM_X64_MSR_KERNELGSBASE] =
1931 1.6.2.2 pgoyette vmcb->state.kernelgsbase;
1932 1.6.2.4 pgoyette state->msrs[NVMM_X64_MSR_SYSENTER_CS] =
1933 1.6.2.2 pgoyette vmcb->state.sysenter_cs;
1934 1.6.2.4 pgoyette state->msrs[NVMM_X64_MSR_SYSENTER_ESP] =
1935 1.6.2.2 pgoyette vmcb->state.sysenter_esp;
1936 1.6.2.4 pgoyette state->msrs[NVMM_X64_MSR_SYSENTER_EIP] =
1937 1.6.2.2 pgoyette vmcb->state.sysenter_eip;
1938 1.6.2.4 pgoyette state->msrs[NVMM_X64_MSR_PAT] = vmcb->state.g_pat;
1939 1.6.2.2 pgoyette
1940 1.6.2.2 pgoyette /* Hide SVME. */
1941 1.6.2.4 pgoyette state->msrs[NVMM_X64_MSR_EFER] &= ~EFER_SVME;
1942 1.6.2.2 pgoyette }
1943 1.6.2.2 pgoyette
1944 1.6.2.2 pgoyette if (flags & NVMM_X64_STATE_MISC) {
1945 1.6.2.4 pgoyette state->misc[NVMM_X64_MISC_CPL] = vmcb->state.cpl;
1946 1.6.2.2 pgoyette
1947 1.6.2.4 pgoyette state->misc[NVMM_X64_MISC_INT_SHADOW] =
1948 1.6.2.4 pgoyette (vmcb->ctrl.intr & VMCB_CTRL_INTR_SHADOW) != 0;
1949 1.6.2.4 pgoyette state->misc[NVMM_X64_MISC_INT_WINDOW_EXIT] =
1950 1.6.2.4 pgoyette cpudata->int_window_exit;
1951 1.6.2.4 pgoyette state->misc[NVMM_X64_MISC_NMI_WINDOW_EXIT] =
1952 1.6.2.4 pgoyette cpudata->nmi_window_exit;
1953 1.6.2.2 pgoyette }
1954 1.6.2.2 pgoyette
1955 1.6.2.4 pgoyette CTASSERT(sizeof(cpudata->gfpu.xsh_fxsave) == sizeof(state->fpu));
1956 1.6.2.2 pgoyette if (flags & NVMM_X64_STATE_FPU) {
1957 1.6.2.4 pgoyette memcpy(&state->fpu, cpudata->gfpu.xsh_fxsave,
1958 1.6.2.4 pgoyette sizeof(state->fpu));
1959 1.6.2.2 pgoyette }
1960 1.6.2.2 pgoyette }
1961 1.6.2.2 pgoyette
1962 1.6.2.2 pgoyette /* -------------------------------------------------------------------------- */
1963 1.6.2.2 pgoyette
1964 1.6.2.2 pgoyette static void
1965 1.6.2.2 pgoyette svm_tlb_flush(struct pmap *pm)
1966 1.6.2.2 pgoyette {
1967 1.6.2.2 pgoyette struct nvmm_machine *mach = pm->pm_data;
1968 1.6.2.2 pgoyette struct svm_cpudata *cpudata;
1969 1.6.2.2 pgoyette struct nvmm_cpu *vcpu;
1970 1.6.2.2 pgoyette int error;
1971 1.6.2.2 pgoyette size_t i;
1972 1.6.2.2 pgoyette
1973 1.6.2.2 pgoyette /* Request TLB flushes. */
1974 1.6.2.2 pgoyette for (i = 0; i < NVMM_MAX_VCPUS; i++) {
1975 1.6.2.2 pgoyette error = nvmm_vcpu_get(mach, i, &vcpu);
1976 1.6.2.2 pgoyette if (error)
1977 1.6.2.2 pgoyette continue;
1978 1.6.2.2 pgoyette cpudata = vcpu->cpudata;
1979 1.6.2.2 pgoyette cpudata->tlb_want_flush = true;
1980 1.6.2.2 pgoyette nvmm_vcpu_put(vcpu);
1981 1.6.2.2 pgoyette }
1982 1.6.2.2 pgoyette }
1983 1.6.2.2 pgoyette
1984 1.6.2.2 pgoyette static void
1985 1.6.2.2 pgoyette svm_machine_create(struct nvmm_machine *mach)
1986 1.6.2.2 pgoyette {
1987 1.6.2.2 pgoyette /* Fill in pmap info. */
1988 1.6.2.2 pgoyette mach->vm->vm_map.pmap->pm_data = (void *)mach;
1989 1.6.2.2 pgoyette mach->vm->vm_map.pmap->pm_tlb_flush = svm_tlb_flush;
1990 1.6.2.2 pgoyette
1991 1.6.2.2 pgoyette mach->machdata = kmem_zalloc(sizeof(struct svm_machdata), KM_SLEEP);
1992 1.6.2.2 pgoyette }
1993 1.6.2.2 pgoyette
1994 1.6.2.2 pgoyette static void
1995 1.6.2.2 pgoyette svm_machine_destroy(struct nvmm_machine *mach)
1996 1.6.2.2 pgoyette {
1997 1.6.2.2 pgoyette kmem_free(mach->machdata, sizeof(struct svm_machdata));
1998 1.6.2.2 pgoyette }
1999 1.6.2.2 pgoyette
2000 1.6.2.2 pgoyette static int
2001 1.6.2.2 pgoyette svm_machine_configure(struct nvmm_machine *mach, uint64_t op, void *data)
2002 1.6.2.2 pgoyette {
2003 1.6.2.2 pgoyette struct nvmm_x86_conf_cpuid *cpuid = data;
2004 1.6.2.2 pgoyette struct svm_machdata *machdata = (struct svm_machdata *)mach->machdata;
2005 1.6.2.2 pgoyette size_t i;
2006 1.6.2.2 pgoyette
2007 1.6.2.2 pgoyette if (__predict_false(op != NVMM_X86_CONF_CPUID)) {
2008 1.6.2.2 pgoyette return EINVAL;
2009 1.6.2.2 pgoyette }
2010 1.6.2.2 pgoyette
2011 1.6.2.2 pgoyette if (__predict_false((cpuid->set.eax & cpuid->del.eax) ||
2012 1.6.2.2 pgoyette (cpuid->set.ebx & cpuid->del.ebx) ||
2013 1.6.2.2 pgoyette (cpuid->set.ecx & cpuid->del.ecx) ||
2014 1.6.2.2 pgoyette (cpuid->set.edx & cpuid->del.edx))) {
2015 1.6.2.2 pgoyette return EINVAL;
2016 1.6.2.2 pgoyette }
2017 1.6.2.2 pgoyette
2018 1.6.2.2 pgoyette /* If already here, replace. */
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 continue;
2022 1.6.2.2 pgoyette }
2023 1.6.2.2 pgoyette if (machdata->cpuid[i].leaf == cpuid->leaf) {
2024 1.6.2.2 pgoyette memcpy(&machdata->cpuid[i], cpuid,
2025 1.6.2.2 pgoyette sizeof(struct nvmm_x86_conf_cpuid));
2026 1.6.2.2 pgoyette return 0;
2027 1.6.2.2 pgoyette }
2028 1.6.2.2 pgoyette }
2029 1.6.2.2 pgoyette
2030 1.6.2.2 pgoyette /* Not here, insert. */
2031 1.6.2.2 pgoyette for (i = 0; i < SVM_NCPUIDS; i++) {
2032 1.6.2.2 pgoyette if (!machdata->cpuidpresent[i]) {
2033 1.6.2.2 pgoyette machdata->cpuidpresent[i] = true;
2034 1.6.2.2 pgoyette memcpy(&machdata->cpuid[i], cpuid,
2035 1.6.2.2 pgoyette sizeof(struct nvmm_x86_conf_cpuid));
2036 1.6.2.2 pgoyette return 0;
2037 1.6.2.2 pgoyette }
2038 1.6.2.2 pgoyette }
2039 1.6.2.2 pgoyette
2040 1.6.2.2 pgoyette return ENOBUFS;
2041 1.6.2.2 pgoyette }
2042 1.6.2.2 pgoyette
2043 1.6.2.2 pgoyette /* -------------------------------------------------------------------------- */
2044 1.6.2.2 pgoyette
2045 1.6.2.2 pgoyette static bool
2046 1.6.2.2 pgoyette svm_ident(void)
2047 1.6.2.2 pgoyette {
2048 1.6.2.2 pgoyette u_int descs[4];
2049 1.6.2.2 pgoyette uint64_t msr;
2050 1.6.2.2 pgoyette
2051 1.6.2.2 pgoyette if (cpu_vendor != CPUVENDOR_AMD) {
2052 1.6.2.2 pgoyette return false;
2053 1.6.2.2 pgoyette }
2054 1.6.2.2 pgoyette if (!(cpu_feature[3] & CPUID_SVM)) {
2055 1.6.2.2 pgoyette return false;
2056 1.6.2.2 pgoyette }
2057 1.6.2.2 pgoyette
2058 1.6.2.2 pgoyette if (curcpu()->ci_max_ext_cpuid < 0x8000000a) {
2059 1.6.2.2 pgoyette return false;
2060 1.6.2.2 pgoyette }
2061 1.6.2.2 pgoyette x86_cpuid(0x8000000a, descs);
2062 1.6.2.2 pgoyette
2063 1.6.2.2 pgoyette /* Want Nested Paging. */
2064 1.6.2.2 pgoyette if (!(descs[3] & CPUID_AMD_SVM_NP)) {
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 /* Want nRIP. */
2069 1.6.2.2 pgoyette if (!(descs[3] & CPUID_AMD_SVM_NRIPS)) {
2070 1.6.2.2 pgoyette return false;
2071 1.6.2.2 pgoyette }
2072 1.6.2.2 pgoyette
2073 1.6.2.2 pgoyette svm_decode_assist = (descs[3] & CPUID_AMD_SVM_DecodeAssist) != 0;
2074 1.6.2.2 pgoyette
2075 1.6.2.2 pgoyette msr = rdmsr(MSR_VMCR);
2076 1.6.2.2 pgoyette if ((msr & VMCR_SVMED) && (msr & VMCR_LOCK)) {
2077 1.6.2.2 pgoyette return false;
2078 1.6.2.2 pgoyette }
2079 1.6.2.2 pgoyette
2080 1.6.2.2 pgoyette return true;
2081 1.6.2.2 pgoyette }
2082 1.6.2.2 pgoyette
2083 1.6.2.2 pgoyette static void
2084 1.6.2.2 pgoyette svm_init_asid(uint32_t maxasid)
2085 1.6.2.2 pgoyette {
2086 1.6.2.2 pgoyette size_t i, j, allocsz;
2087 1.6.2.2 pgoyette
2088 1.6.2.2 pgoyette mutex_init(&svm_asidlock, MUTEX_DEFAULT, IPL_NONE);
2089 1.6.2.2 pgoyette
2090 1.6.2.2 pgoyette /* Arbitrarily limit. */
2091 1.6.2.2 pgoyette maxasid = uimin(maxasid, 8192);
2092 1.6.2.2 pgoyette
2093 1.6.2.2 pgoyette svm_maxasid = maxasid;
2094 1.6.2.2 pgoyette allocsz = roundup(maxasid, 8) / 8;
2095 1.6.2.2 pgoyette svm_asidmap = kmem_zalloc(allocsz, KM_SLEEP);
2096 1.6.2.2 pgoyette
2097 1.6.2.2 pgoyette /* ASID 0 is reserved for the host. */
2098 1.6.2.2 pgoyette svm_asidmap[0] |= __BIT(0);
2099 1.6.2.2 pgoyette
2100 1.6.2.2 pgoyette /* ASID n-1 is special, we share it. */
2101 1.6.2.2 pgoyette i = (maxasid - 1) / 8;
2102 1.6.2.2 pgoyette j = (maxasid - 1) % 8;
2103 1.6.2.2 pgoyette svm_asidmap[i] |= __BIT(j);
2104 1.6.2.2 pgoyette }
2105 1.6.2.2 pgoyette
2106 1.6.2.2 pgoyette static void
2107 1.6.2.2 pgoyette svm_change_cpu(void *arg1, void *arg2)
2108 1.6.2.2 pgoyette {
2109 1.6.2.2 pgoyette bool enable = (bool)arg1;
2110 1.6.2.2 pgoyette uint64_t msr;
2111 1.6.2.2 pgoyette
2112 1.6.2.2 pgoyette msr = rdmsr(MSR_VMCR);
2113 1.6.2.2 pgoyette if (msr & VMCR_SVMED) {
2114 1.6.2.2 pgoyette wrmsr(MSR_VMCR, msr & ~VMCR_SVMED);
2115 1.6.2.2 pgoyette }
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, 0);
2119 1.6.2.2 pgoyette }
2120 1.6.2.2 pgoyette
2121 1.6.2.2 pgoyette msr = rdmsr(MSR_EFER);
2122 1.6.2.2 pgoyette if (enable) {
2123 1.6.2.2 pgoyette msr |= EFER_SVME;
2124 1.6.2.2 pgoyette } else {
2125 1.6.2.2 pgoyette msr &= ~EFER_SVME;
2126 1.6.2.2 pgoyette }
2127 1.6.2.2 pgoyette wrmsr(MSR_EFER, msr);
2128 1.6.2.2 pgoyette
2129 1.6.2.2 pgoyette if (enable) {
2130 1.6.2.2 pgoyette wrmsr(MSR_VM_HSAVE_PA, hsave[cpu_index(curcpu())].pa);
2131 1.6.2.2 pgoyette }
2132 1.6.2.2 pgoyette }
2133 1.6.2.2 pgoyette
2134 1.6.2.2 pgoyette static void
2135 1.6.2.2 pgoyette svm_init(void)
2136 1.6.2.2 pgoyette {
2137 1.6.2.2 pgoyette CPU_INFO_ITERATOR cii;
2138 1.6.2.2 pgoyette struct cpu_info *ci;
2139 1.6.2.2 pgoyette struct vm_page *pg;
2140 1.6.2.2 pgoyette u_int descs[4];
2141 1.6.2.2 pgoyette uint64_t xc;
2142 1.6.2.2 pgoyette
2143 1.6.2.2 pgoyette x86_cpuid(0x8000000a, descs);
2144 1.6.2.2 pgoyette
2145 1.6.2.2 pgoyette /* The guest TLB flush command. */
2146 1.6.2.2 pgoyette if (descs[3] & CPUID_AMD_SVM_FlushByASID) {
2147 1.6.2.2 pgoyette svm_ctrl_tlb_flush = VMCB_CTRL_TLB_CTRL_FLUSH_GUEST;
2148 1.6.2.2 pgoyette } else {
2149 1.6.2.2 pgoyette svm_ctrl_tlb_flush = VMCB_CTRL_TLB_CTRL_FLUSH_ALL;
2150 1.6.2.2 pgoyette }
2151 1.6.2.2 pgoyette
2152 1.6.2.2 pgoyette /* Init the ASID. */
2153 1.6.2.2 pgoyette svm_init_asid(descs[1]);
2154 1.6.2.2 pgoyette
2155 1.6.2.2 pgoyette /* Init the XCR0 mask. */
2156 1.6.2.2 pgoyette svm_xcr0_mask = SVM_XCR0_MASK_DEFAULT & x86_xsave_features;
2157 1.6.2.2 pgoyette
2158 1.6.2.2 pgoyette memset(hsave, 0, sizeof(hsave));
2159 1.6.2.2 pgoyette for (CPU_INFO_FOREACH(cii, ci)) {
2160 1.6.2.2 pgoyette pg = uvm_pagealloc(NULL, 0, NULL, UVM_PGA_ZERO);
2161 1.6.2.2 pgoyette hsave[cpu_index(ci)].pa = VM_PAGE_TO_PHYS(pg);
2162 1.6.2.2 pgoyette }
2163 1.6.2.2 pgoyette
2164 1.6.2.2 pgoyette xc = xc_broadcast(0, svm_change_cpu, (void *)true, NULL);
2165 1.6.2.2 pgoyette xc_wait(xc);
2166 1.6.2.2 pgoyette }
2167 1.6.2.2 pgoyette
2168 1.6.2.2 pgoyette static void
2169 1.6.2.2 pgoyette svm_fini_asid(void)
2170 1.6.2.2 pgoyette {
2171 1.6.2.2 pgoyette size_t allocsz;
2172 1.6.2.2 pgoyette
2173 1.6.2.2 pgoyette allocsz = roundup(svm_maxasid, 8) / 8;
2174 1.6.2.2 pgoyette kmem_free(svm_asidmap, allocsz);
2175 1.6.2.2 pgoyette
2176 1.6.2.2 pgoyette mutex_destroy(&svm_asidlock);
2177 1.6.2.2 pgoyette }
2178 1.6.2.2 pgoyette
2179 1.6.2.2 pgoyette static void
2180 1.6.2.2 pgoyette svm_fini(void)
2181 1.6.2.2 pgoyette {
2182 1.6.2.2 pgoyette uint64_t xc;
2183 1.6.2.2 pgoyette size_t i;
2184 1.6.2.2 pgoyette
2185 1.6.2.2 pgoyette xc = xc_broadcast(0, svm_change_cpu, (void *)false, NULL);
2186 1.6.2.2 pgoyette xc_wait(xc);
2187 1.6.2.2 pgoyette
2188 1.6.2.2 pgoyette for (i = 0; i < MAXCPUS; i++) {
2189 1.6.2.2 pgoyette if (hsave[i].pa != 0)
2190 1.6.2.2 pgoyette uvm_pagefree(PHYS_TO_VM_PAGE(hsave[i].pa));
2191 1.6.2.2 pgoyette }
2192 1.6.2.2 pgoyette
2193 1.6.2.2 pgoyette svm_fini_asid();
2194 1.6.2.2 pgoyette }
2195 1.6.2.2 pgoyette
2196 1.6.2.2 pgoyette static void
2197 1.6.2.2 pgoyette svm_capability(struct nvmm_capability *cap)
2198 1.6.2.2 pgoyette {
2199 1.6.2.2 pgoyette cap->u.x86.xcr0_mask = svm_xcr0_mask;
2200 1.6.2.2 pgoyette cap->u.x86.mxcsr_mask = x86_fpu_mxcsr_mask;
2201 1.6.2.2 pgoyette cap->u.x86.conf_cpuid_maxops = SVM_NCPUIDS;
2202 1.6.2.2 pgoyette }
2203 1.6.2.2 pgoyette
2204 1.6.2.2 pgoyette const struct nvmm_impl nvmm_x86_svm = {
2205 1.6.2.2 pgoyette .ident = svm_ident,
2206 1.6.2.2 pgoyette .init = svm_init,
2207 1.6.2.2 pgoyette .fini = svm_fini,
2208 1.6.2.2 pgoyette .capability = svm_capability,
2209 1.6.2.2 pgoyette .conf_max = NVMM_X86_NCONF,
2210 1.6.2.2 pgoyette .conf_sizes = svm_conf_sizes,
2211 1.6.2.2 pgoyette .state_size = sizeof(struct nvmm_x64_state),
2212 1.6.2.2 pgoyette .machine_create = svm_machine_create,
2213 1.6.2.2 pgoyette .machine_destroy = svm_machine_destroy,
2214 1.6.2.2 pgoyette .machine_configure = svm_machine_configure,
2215 1.6.2.2 pgoyette .vcpu_create = svm_vcpu_create,
2216 1.6.2.2 pgoyette .vcpu_destroy = svm_vcpu_destroy,
2217 1.6.2.2 pgoyette .vcpu_setstate = svm_vcpu_setstate,
2218 1.6.2.2 pgoyette .vcpu_getstate = svm_vcpu_getstate,
2219 1.6.2.2 pgoyette .vcpu_inject = svm_vcpu_inject,
2220 1.6.2.2 pgoyette .vcpu_run = svm_vcpu_run
2221 1.6.2.2 pgoyette };
2222