nvmm_x86_svm.c revision 1.6.2.2 1 1.6.2.2 pgoyette /* $NetBSD: nvmm_x86_svm.c,v 1.6.2.2 2018/11/26 01:52:32 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.2 pgoyette __KERNEL_RCSID(0, "$NetBSD: nvmm_x86_svm.c,v 1.6.2.2 2018/11/26 01:52:32 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 #define VMCB_CTRL_GUEST_INTR_MASK __BIT(1)
318 1.6.2.2 pgoyette
319 1.6.2.2 pgoyette uint64_t exitcode;
320 1.6.2.2 pgoyette uint64_t exitinfo1;
321 1.6.2.2 pgoyette uint64_t exitinfo2;
322 1.6.2.2 pgoyette
323 1.6.2.2 pgoyette uint64_t exitintinfo;
324 1.6.2.2 pgoyette #define VMCB_CTRL_EXITINTINFO_VECTOR __BITS(7,0)
325 1.6.2.2 pgoyette #define VMCB_CTRL_EXITINTINFO_TYPE __BITS(10,8)
326 1.6.2.2 pgoyette #define VMCB_CTRL_EXITINTINFO_EV __BIT(11)
327 1.6.2.2 pgoyette #define VMCB_CTRL_EXITINTINFO_V __BIT(31)
328 1.6.2.2 pgoyette #define VMCB_CTRL_EXITINTINFO_ERRORCODE __BITS(63,32)
329 1.6.2.2 pgoyette
330 1.6.2.2 pgoyette uint64_t enable1;
331 1.6.2.2 pgoyette #define VMCB_CTRL_ENABLE_NP __BIT(0)
332 1.6.2.2 pgoyette #define VMCB_CTRL_ENABLE_SEV __BIT(1)
333 1.6.2.2 pgoyette #define VMCB_CTRL_ENABLE_ES_SEV __BIT(2)
334 1.6.2.2 pgoyette
335 1.6.2.2 pgoyette uint64_t avic;
336 1.6.2.2 pgoyette #define VMCB_CTRL_AVIC_APIC_BAR __BITS(51,0)
337 1.6.2.2 pgoyette
338 1.6.2.2 pgoyette uint64_t ghcb;
339 1.6.2.2 pgoyette
340 1.6.2.2 pgoyette uint64_t eventinj;
341 1.6.2.2 pgoyette #define VMCB_CTRL_EVENTINJ_VECTOR __BITS(7,0)
342 1.6.2.2 pgoyette #define VMCB_CTRL_EVENTINJ_TYPE __BITS(10,8)
343 1.6.2.2 pgoyette #define VMCB_CTRL_EVENTINJ_EV __BIT(11)
344 1.6.2.2 pgoyette #define VMCB_CTRL_EVENTINJ_V __BIT(31)
345 1.6.2.2 pgoyette #define VMCB_CTRL_EVENTINJ_ERRORCODE __BITS(63,32)
346 1.6.2.2 pgoyette
347 1.6.2.2 pgoyette uint64_t n_cr3;
348 1.6.2.2 pgoyette
349 1.6.2.2 pgoyette uint64_t enable2;
350 1.6.2.2 pgoyette #define VMCB_CTRL_ENABLE_LBR __BIT(0)
351 1.6.2.2 pgoyette #define VMCB_CTRL_ENABLE_VVMSAVE __BIT(1)
352 1.6.2.2 pgoyette
353 1.6.2.2 pgoyette uint32_t vmcb_clean;
354 1.6.2.2 pgoyette #define VMCB_CTRL_VMCB_CLEAN_I __BIT(0)
355 1.6.2.2 pgoyette #define VMCB_CTRL_VMCB_CLEAN_IOPM __BIT(1)
356 1.6.2.2 pgoyette #define VMCB_CTRL_VMCB_CLEAN_ASID __BIT(2)
357 1.6.2.2 pgoyette #define VMCB_CTRL_VMCB_CLEAN_TPR __BIT(3)
358 1.6.2.2 pgoyette #define VMCB_CTRL_VMCB_CLEAN_NP __BIT(4)
359 1.6.2.2 pgoyette #define VMCB_CTRL_VMCB_CLEAN_CR __BIT(5)
360 1.6.2.2 pgoyette #define VMCB_CTRL_VMCB_CLEAN_DR __BIT(6)
361 1.6.2.2 pgoyette #define VMCB_CTRL_VMCB_CLEAN_DT __BIT(7)
362 1.6.2.2 pgoyette #define VMCB_CTRL_VMCB_CLEAN_SEG __BIT(8)
363 1.6.2.2 pgoyette #define VMCB_CTRL_VMCB_CLEAN_CR2 __BIT(9)
364 1.6.2.2 pgoyette #define VMCB_CTRL_VMCB_CLEAN_LBR __BIT(10)
365 1.6.2.2 pgoyette #define VMCB_CTRL_VMCB_CLEAN_AVIC __BIT(11)
366 1.6.2.2 pgoyette
367 1.6.2.2 pgoyette uint32_t rsvd2;
368 1.6.2.2 pgoyette uint64_t nrip;
369 1.6.2.2 pgoyette uint8_t inst_len;
370 1.6.2.2 pgoyette uint8_t inst_bytes[15];
371 1.6.2.2 pgoyette uint8_t pad[800];
372 1.6.2.2 pgoyette } __packed;
373 1.6.2.2 pgoyette
374 1.6.2.2 pgoyette CTASSERT(sizeof(struct vmcb_ctrl) == 1024);
375 1.6.2.2 pgoyette
376 1.6.2.2 pgoyette struct vmcb_segment {
377 1.6.2.2 pgoyette uint16_t selector;
378 1.6.2.2 pgoyette uint16_t attrib; /* hidden */
379 1.6.2.2 pgoyette uint32_t limit; /* hidden */
380 1.6.2.2 pgoyette uint64_t base; /* hidden */
381 1.6.2.2 pgoyette } __packed;
382 1.6.2.2 pgoyette
383 1.6.2.2 pgoyette CTASSERT(sizeof(struct vmcb_segment) == 16);
384 1.6.2.2 pgoyette
385 1.6.2.2 pgoyette struct vmcb_state {
386 1.6.2.2 pgoyette struct vmcb_segment es;
387 1.6.2.2 pgoyette struct vmcb_segment cs;
388 1.6.2.2 pgoyette struct vmcb_segment ss;
389 1.6.2.2 pgoyette struct vmcb_segment ds;
390 1.6.2.2 pgoyette struct vmcb_segment fs;
391 1.6.2.2 pgoyette struct vmcb_segment gs;
392 1.6.2.2 pgoyette struct vmcb_segment gdt;
393 1.6.2.2 pgoyette struct vmcb_segment ldt;
394 1.6.2.2 pgoyette struct vmcb_segment idt;
395 1.6.2.2 pgoyette struct vmcb_segment tr;
396 1.6.2.2 pgoyette uint8_t rsvd1[43];
397 1.6.2.2 pgoyette uint8_t cpl;
398 1.6.2.2 pgoyette uint8_t rsvd2[4];
399 1.6.2.2 pgoyette uint64_t efer;
400 1.6.2.2 pgoyette uint8_t rsvd3[112];
401 1.6.2.2 pgoyette uint64_t cr4;
402 1.6.2.2 pgoyette uint64_t cr3;
403 1.6.2.2 pgoyette uint64_t cr0;
404 1.6.2.2 pgoyette uint64_t dr7;
405 1.6.2.2 pgoyette uint64_t dr6;
406 1.6.2.2 pgoyette uint64_t rflags;
407 1.6.2.2 pgoyette uint64_t rip;
408 1.6.2.2 pgoyette uint8_t rsvd4[88];
409 1.6.2.2 pgoyette uint64_t rsp;
410 1.6.2.2 pgoyette uint8_t rsvd5[24];
411 1.6.2.2 pgoyette uint64_t rax;
412 1.6.2.2 pgoyette uint64_t star;
413 1.6.2.2 pgoyette uint64_t lstar;
414 1.6.2.2 pgoyette uint64_t cstar;
415 1.6.2.2 pgoyette uint64_t sfmask;
416 1.6.2.2 pgoyette uint64_t kernelgsbase;
417 1.6.2.2 pgoyette uint64_t sysenter_cs;
418 1.6.2.2 pgoyette uint64_t sysenter_esp;
419 1.6.2.2 pgoyette uint64_t sysenter_eip;
420 1.6.2.2 pgoyette uint64_t cr2;
421 1.6.2.2 pgoyette uint8_t rsvd6[32];
422 1.6.2.2 pgoyette uint64_t g_pat;
423 1.6.2.2 pgoyette uint64_t dbgctl;
424 1.6.2.2 pgoyette uint64_t br_from;
425 1.6.2.2 pgoyette uint64_t br_to;
426 1.6.2.2 pgoyette uint64_t int_from;
427 1.6.2.2 pgoyette uint64_t int_to;
428 1.6.2.2 pgoyette uint8_t pad[2408];
429 1.6.2.2 pgoyette } __packed;
430 1.6.2.2 pgoyette
431 1.6.2.2 pgoyette CTASSERT(sizeof(struct vmcb_state) == 0xC00);
432 1.6.2.2 pgoyette
433 1.6.2.2 pgoyette struct vmcb {
434 1.6.2.2 pgoyette struct vmcb_ctrl ctrl;
435 1.6.2.2 pgoyette struct vmcb_state state;
436 1.6.2.2 pgoyette } __packed;
437 1.6.2.2 pgoyette
438 1.6.2.2 pgoyette CTASSERT(sizeof(struct vmcb) == PAGE_SIZE);
439 1.6.2.2 pgoyette CTASSERT(offsetof(struct vmcb, state) == 0x400);
440 1.6.2.2 pgoyette
441 1.6.2.2 pgoyette /* -------------------------------------------------------------------------- */
442 1.6.2.2 pgoyette
443 1.6.2.2 pgoyette struct svm_hsave {
444 1.6.2.2 pgoyette paddr_t pa;
445 1.6.2.2 pgoyette };
446 1.6.2.2 pgoyette
447 1.6.2.2 pgoyette static struct svm_hsave hsave[MAXCPUS];
448 1.6.2.2 pgoyette
449 1.6.2.2 pgoyette static uint8_t *svm_asidmap __read_mostly;
450 1.6.2.2 pgoyette static uint32_t svm_maxasid __read_mostly;
451 1.6.2.2 pgoyette static kmutex_t svm_asidlock __cacheline_aligned;
452 1.6.2.2 pgoyette
453 1.6.2.2 pgoyette static bool svm_decode_assist __read_mostly;
454 1.6.2.2 pgoyette static uint32_t svm_ctrl_tlb_flush __read_mostly;
455 1.6.2.2 pgoyette
456 1.6.2.2 pgoyette #define SVM_XCR0_MASK_DEFAULT (XCR0_X87|XCR0_SSE)
457 1.6.2.2 pgoyette static uint64_t svm_xcr0_mask __read_mostly;
458 1.6.2.2 pgoyette
459 1.6.2.2 pgoyette #define SVM_NCPUIDS 32
460 1.6.2.2 pgoyette
461 1.6.2.2 pgoyette #define VMCB_NPAGES 1
462 1.6.2.2 pgoyette
463 1.6.2.2 pgoyette #define MSRBM_NPAGES 2
464 1.6.2.2 pgoyette #define MSRBM_SIZE (MSRBM_NPAGES * PAGE_SIZE)
465 1.6.2.2 pgoyette
466 1.6.2.2 pgoyette #define IOBM_NPAGES 3
467 1.6.2.2 pgoyette #define IOBM_SIZE (IOBM_NPAGES * PAGE_SIZE)
468 1.6.2.2 pgoyette
469 1.6.2.2 pgoyette /* Does not include EFER_LMSLE. */
470 1.6.2.2 pgoyette #define EFER_VALID \
471 1.6.2.2 pgoyette (EFER_SCE|EFER_LME|EFER_LMA|EFER_NXE|EFER_SVME|EFER_FFXSR|EFER_TCE)
472 1.6.2.2 pgoyette
473 1.6.2.2 pgoyette #define EFER_TLB_FLUSH \
474 1.6.2.2 pgoyette (EFER_NXE|EFER_LMA|EFER_LME)
475 1.6.2.2 pgoyette #define CR0_TLB_FLUSH \
476 1.6.2.2 pgoyette (CR0_PG|CR0_WP|CR0_CD|CR0_NW)
477 1.6.2.2 pgoyette #define CR4_TLB_FLUSH \
478 1.6.2.2 pgoyette (CR4_PGE|CR4_PAE|CR4_PSE)
479 1.6.2.2 pgoyette
480 1.6.2.2 pgoyette /* -------------------------------------------------------------------------- */
481 1.6.2.2 pgoyette
482 1.6.2.2 pgoyette struct svm_machdata {
483 1.6.2.2 pgoyette bool cpuidpresent[SVM_NCPUIDS];
484 1.6.2.2 pgoyette struct nvmm_x86_conf_cpuid cpuid[SVM_NCPUIDS];
485 1.6.2.2 pgoyette };
486 1.6.2.2 pgoyette
487 1.6.2.2 pgoyette static const size_t svm_conf_sizes[NVMM_X86_NCONF] = {
488 1.6.2.2 pgoyette [NVMM_X86_CONF_CPUID] = sizeof(struct nvmm_x86_conf_cpuid)
489 1.6.2.2 pgoyette };
490 1.6.2.2 pgoyette
491 1.6.2.2 pgoyette struct svm_cpudata {
492 1.6.2.2 pgoyette /* x64-specific */
493 1.6.2.2 pgoyette struct nvmm_x64_state state;
494 1.6.2.2 pgoyette
495 1.6.2.2 pgoyette /* General */
496 1.6.2.2 pgoyette bool shared_asid;
497 1.6.2.2 pgoyette bool tlb_want_flush;
498 1.6.2.2 pgoyette
499 1.6.2.2 pgoyette /* VMCB */
500 1.6.2.2 pgoyette struct vmcb *vmcb;
501 1.6.2.2 pgoyette paddr_t vmcb_pa;
502 1.6.2.2 pgoyette
503 1.6.2.2 pgoyette /* I/O bitmap */
504 1.6.2.2 pgoyette uint8_t *iobm;
505 1.6.2.2 pgoyette paddr_t iobm_pa;
506 1.6.2.2 pgoyette
507 1.6.2.2 pgoyette /* MSR bitmap */
508 1.6.2.2 pgoyette uint8_t *msrbm;
509 1.6.2.2 pgoyette paddr_t msrbm_pa;
510 1.6.2.2 pgoyette
511 1.6.2.2 pgoyette /* Host state */
512 1.6.2.2 pgoyette uint64_t xcr0;
513 1.6.2.2 pgoyette uint64_t star;
514 1.6.2.2 pgoyette uint64_t lstar;
515 1.6.2.2 pgoyette uint64_t cstar;
516 1.6.2.2 pgoyette uint64_t sfmask;
517 1.6.2.2 pgoyette uint64_t cr2;
518 1.6.2.2 pgoyette bool ts_set;
519 1.6.2.2 pgoyette struct xsave_header hfpu __aligned(16);
520 1.6.2.2 pgoyette
521 1.6.2.2 pgoyette /* Guest state */
522 1.6.2.2 pgoyette bool in_nmi;
523 1.6.2.2 pgoyette uint64_t tsc_offset;
524 1.6.2.2 pgoyette struct xsave_header gfpu __aligned(16);
525 1.6.2.2 pgoyette };
526 1.6.2.2 pgoyette
527 1.6.2.2 pgoyette #define SVM_EVENT_TYPE_HW_INT 0
528 1.6.2.2 pgoyette #define SVM_EVENT_TYPE_NMI 2
529 1.6.2.2 pgoyette #define SVM_EVENT_TYPE_EXC 3
530 1.6.2.2 pgoyette #define SVM_EVENT_TYPE_SW_INT 4
531 1.6.2.2 pgoyette
532 1.6.2.2 pgoyette static void
533 1.6.2.2 pgoyette svm_event_waitexit_enable(struct vmcb *vmcb, bool nmi)
534 1.6.2.2 pgoyette {
535 1.6.2.2 pgoyette if (nmi) {
536 1.6.2.2 pgoyette vmcb->ctrl.intercept_misc1 |= VMCB_CTRL_INTERCEPT_IRET;
537 1.6.2.2 pgoyette } else {
538 1.6.2.2 pgoyette vmcb->ctrl.intercept_misc1 |= VMCB_CTRL_INTERCEPT_VINTR;
539 1.6.2.2 pgoyette vmcb->ctrl.v |= (VMCB_CTRL_V_IRQ |
540 1.6.2.2 pgoyette __SHIFTIN(0, VMCB_CTRL_V_INTR_VECTOR));
541 1.6.2.2 pgoyette }
542 1.6.2.2 pgoyette }
543 1.6.2.2 pgoyette
544 1.6.2.2 pgoyette static void
545 1.6.2.2 pgoyette svm_event_waitexit_disable(struct vmcb *vmcb, bool nmi)
546 1.6.2.2 pgoyette {
547 1.6.2.2 pgoyette if (nmi) {
548 1.6.2.2 pgoyette vmcb->ctrl.intercept_misc1 &= ~VMCB_CTRL_INTERCEPT_IRET;
549 1.6.2.2 pgoyette } else {
550 1.6.2.2 pgoyette vmcb->ctrl.intercept_misc1 &= ~VMCB_CTRL_INTERCEPT_VINTR;
551 1.6.2.2 pgoyette vmcb->ctrl.v &= ~(VMCB_CTRL_V_IRQ |
552 1.6.2.2 pgoyette __SHIFTIN(0, VMCB_CTRL_V_INTR_VECTOR));
553 1.6.2.2 pgoyette }
554 1.6.2.2 pgoyette }
555 1.6.2.2 pgoyette
556 1.6.2.2 pgoyette static inline int
557 1.6.2.2 pgoyette svm_event_has_error(uint64_t vector)
558 1.6.2.2 pgoyette {
559 1.6.2.2 pgoyette switch (vector) {
560 1.6.2.2 pgoyette case 8: /* #DF */
561 1.6.2.2 pgoyette case 10: /* #TS */
562 1.6.2.2 pgoyette case 11: /* #NP */
563 1.6.2.2 pgoyette case 12: /* #SS */
564 1.6.2.2 pgoyette case 13: /* #GP */
565 1.6.2.2 pgoyette case 14: /* #PF */
566 1.6.2.2 pgoyette case 17: /* #AC */
567 1.6.2.2 pgoyette case 30: /* #SX */
568 1.6.2.2 pgoyette return 1;
569 1.6.2.2 pgoyette default:
570 1.6.2.2 pgoyette return 0;
571 1.6.2.2 pgoyette }
572 1.6.2.2 pgoyette }
573 1.6.2.2 pgoyette
574 1.6.2.2 pgoyette static int
575 1.6.2.2 pgoyette svm_vcpu_inject(struct nvmm_machine *mach, struct nvmm_cpu *vcpu,
576 1.6.2.2 pgoyette struct nvmm_event *event)
577 1.6.2.2 pgoyette {
578 1.6.2.2 pgoyette struct svm_cpudata *cpudata = vcpu->cpudata;
579 1.6.2.2 pgoyette struct vmcb *vmcb = cpudata->vmcb;
580 1.6.2.2 pgoyette uint64_t rflags = vmcb->state.rflags;
581 1.6.2.2 pgoyette int type = 0, err = 0;
582 1.6.2.2 pgoyette uint64_t tpr;
583 1.6.2.2 pgoyette
584 1.6.2.2 pgoyette if (event->vector >= 256) {
585 1.6.2.2 pgoyette return EINVAL;
586 1.6.2.2 pgoyette }
587 1.6.2.2 pgoyette
588 1.6.2.2 pgoyette switch (event->type) {
589 1.6.2.2 pgoyette case NVMM_EVENT_INTERRUPT_HW:
590 1.6.2.2 pgoyette type = SVM_EVENT_TYPE_HW_INT;
591 1.6.2.2 pgoyette if (event->vector == 2) {
592 1.6.2.2 pgoyette type = SVM_EVENT_TYPE_NMI;
593 1.6.2.2 pgoyette }
594 1.6.2.2 pgoyette if (type == SVM_EVENT_TYPE_NMI) {
595 1.6.2.2 pgoyette if (cpudata->in_nmi) {
596 1.6.2.2 pgoyette svm_event_waitexit_enable(vmcb, true);
597 1.6.2.2 pgoyette return EAGAIN;
598 1.6.2.2 pgoyette }
599 1.6.2.2 pgoyette cpudata->in_nmi = true;
600 1.6.2.2 pgoyette } else {
601 1.6.2.2 pgoyette tpr = __SHIFTOUT(vmcb->ctrl.v, VMCB_CTRL_V_TPR);
602 1.6.2.2 pgoyette if ((rflags & PSL_I) == 0 || event->u.prio <= tpr) {
603 1.6.2.2 pgoyette svm_event_waitexit_enable(vmcb, false);
604 1.6.2.2 pgoyette return EAGAIN;
605 1.6.2.2 pgoyette }
606 1.6.2.2 pgoyette }
607 1.6.2.2 pgoyette err = 0;
608 1.6.2.2 pgoyette break;
609 1.6.2.2 pgoyette case NVMM_EVENT_INTERRUPT_SW:
610 1.6.2.2 pgoyette type = SVM_EVENT_TYPE_SW_INT;
611 1.6.2.2 pgoyette err = 0;
612 1.6.2.2 pgoyette break;
613 1.6.2.2 pgoyette case NVMM_EVENT_EXCEPTION:
614 1.6.2.2 pgoyette type = SVM_EVENT_TYPE_EXC;
615 1.6.2.2 pgoyette if (event->vector == 2 || event->vector >= 32)
616 1.6.2.2 pgoyette return EINVAL;
617 1.6.2.2 pgoyette err = svm_event_has_error(event->vector);
618 1.6.2.2 pgoyette break;
619 1.6.2.2 pgoyette default:
620 1.6.2.2 pgoyette return EINVAL;
621 1.6.2.2 pgoyette }
622 1.6.2.2 pgoyette
623 1.6.2.2 pgoyette vmcb->ctrl.eventinj =
624 1.6.2.2 pgoyette __SHIFTIN(event->vector, VMCB_CTRL_EVENTINJ_VECTOR) |
625 1.6.2.2 pgoyette __SHIFTIN(type, VMCB_CTRL_EVENTINJ_TYPE) |
626 1.6.2.2 pgoyette __SHIFTIN(err, VMCB_CTRL_EVENTINJ_EV) |
627 1.6.2.2 pgoyette __SHIFTIN(1, VMCB_CTRL_EVENTINJ_V) |
628 1.6.2.2 pgoyette __SHIFTIN(event->u.error, VMCB_CTRL_EVENTINJ_ERRORCODE);
629 1.6.2.2 pgoyette
630 1.6.2.2 pgoyette return 0;
631 1.6.2.2 pgoyette }
632 1.6.2.2 pgoyette
633 1.6.2.2 pgoyette static void
634 1.6.2.2 pgoyette svm_inject_ud(struct nvmm_machine *mach, struct nvmm_cpu *vcpu)
635 1.6.2.2 pgoyette {
636 1.6.2.2 pgoyette struct nvmm_event event;
637 1.6.2.2 pgoyette int ret __diagused;
638 1.6.2.2 pgoyette
639 1.6.2.2 pgoyette event.type = NVMM_EVENT_EXCEPTION;
640 1.6.2.2 pgoyette event.vector = 6;
641 1.6.2.2 pgoyette event.u.error = 0;
642 1.6.2.2 pgoyette
643 1.6.2.2 pgoyette ret = svm_vcpu_inject(mach, vcpu, &event);
644 1.6.2.2 pgoyette KASSERT(ret == 0);
645 1.6.2.2 pgoyette }
646 1.6.2.2 pgoyette
647 1.6.2.2 pgoyette static void
648 1.6.2.2 pgoyette svm_inject_db(struct nvmm_machine *mach, struct nvmm_cpu *vcpu)
649 1.6.2.2 pgoyette {
650 1.6.2.2 pgoyette struct nvmm_event event;
651 1.6.2.2 pgoyette int ret __diagused;
652 1.6.2.2 pgoyette
653 1.6.2.2 pgoyette event.type = NVMM_EVENT_EXCEPTION;
654 1.6.2.2 pgoyette event.vector = 1;
655 1.6.2.2 pgoyette event.u.error = 0;
656 1.6.2.2 pgoyette
657 1.6.2.2 pgoyette ret = svm_vcpu_inject(mach, vcpu, &event);
658 1.6.2.2 pgoyette KASSERT(ret == 0);
659 1.6.2.2 pgoyette }
660 1.6.2.2 pgoyette
661 1.6.2.2 pgoyette static void
662 1.6.2.2 pgoyette svm_inject_gp(struct nvmm_machine *mach, struct nvmm_cpu *vcpu)
663 1.6.2.2 pgoyette {
664 1.6.2.2 pgoyette struct nvmm_event event;
665 1.6.2.2 pgoyette int ret __diagused;
666 1.6.2.2 pgoyette
667 1.6.2.2 pgoyette event.type = NVMM_EVENT_EXCEPTION;
668 1.6.2.2 pgoyette event.vector = 13;
669 1.6.2.2 pgoyette event.u.error = 0;
670 1.6.2.2 pgoyette
671 1.6.2.2 pgoyette ret = svm_vcpu_inject(mach, vcpu, &event);
672 1.6.2.2 pgoyette KASSERT(ret == 0);
673 1.6.2.2 pgoyette }
674 1.6.2.2 pgoyette
675 1.6.2.2 pgoyette static void
676 1.6.2.2 pgoyette svm_inkernel_handle_cpuid(struct nvmm_cpu *vcpu, uint64_t eax, uint64_t ecx)
677 1.6.2.2 pgoyette {
678 1.6.2.2 pgoyette struct svm_cpudata *cpudata = vcpu->cpudata;
679 1.6.2.2 pgoyette struct nvmm_x64_state *state = &cpudata->state;
680 1.6.2.2 pgoyette
681 1.6.2.2 pgoyette switch (eax) {
682 1.6.2.2 pgoyette case 0x00000001: /* APIC number in RBX. The rest is tunable. */
683 1.6.2.2 pgoyette state->gprs[NVMM_X64_GPR_RBX] &= ~CPUID_LOCAL_APIC_ID;
684 1.6.2.2 pgoyette state->gprs[NVMM_X64_GPR_RBX] |= __SHIFTIN(vcpu->cpuid,
685 1.6.2.2 pgoyette CPUID_LOCAL_APIC_ID);
686 1.6.2.2 pgoyette break;
687 1.6.2.2 pgoyette case 0x0000000D: /* FPU description. Not tunable. */
688 1.6.2.2 pgoyette if (ecx != 0 || svm_xcr0_mask == 0) {
689 1.6.2.2 pgoyette break;
690 1.6.2.2 pgoyette }
691 1.6.2.2 pgoyette cpudata->vmcb->state.rax = svm_xcr0_mask & 0xFFFFFFFF;
692 1.6.2.2 pgoyette if (state->crs[NVMM_X64_CR_XCR0] & XCR0_SSE) {
693 1.6.2.2 pgoyette state->gprs[NVMM_X64_GPR_RBX] = sizeof(struct fxsave);
694 1.6.2.2 pgoyette } else {
695 1.6.2.2 pgoyette state->gprs[NVMM_X64_GPR_RBX] = sizeof(struct save87);
696 1.6.2.2 pgoyette }
697 1.6.2.2 pgoyette state->gprs[NVMM_X64_GPR_RBX] += 64; /* XSAVE header */
698 1.6.2.2 pgoyette state->gprs[NVMM_X64_GPR_RCX] = sizeof(struct fxsave);
699 1.6.2.2 pgoyette state->gprs[NVMM_X64_GPR_RDX] = svm_xcr0_mask >> 32;
700 1.6.2.2 pgoyette break;
701 1.6.2.2 pgoyette default:
702 1.6.2.2 pgoyette break;
703 1.6.2.2 pgoyette }
704 1.6.2.2 pgoyette }
705 1.6.2.2 pgoyette
706 1.6.2.2 pgoyette static void
707 1.6.2.2 pgoyette svm_exit_cpuid(struct nvmm_machine *mach, struct nvmm_cpu *vcpu,
708 1.6.2.2 pgoyette struct nvmm_exit *exit)
709 1.6.2.2 pgoyette {
710 1.6.2.2 pgoyette struct svm_machdata *machdata = mach->machdata;
711 1.6.2.2 pgoyette struct svm_cpudata *cpudata = vcpu->cpudata;
712 1.6.2.2 pgoyette struct nvmm_x64_state *state = &cpudata->state;
713 1.6.2.2 pgoyette struct nvmm_x86_conf_cpuid *cpuid;
714 1.6.2.2 pgoyette uint64_t eax, ecx;
715 1.6.2.2 pgoyette u_int descs[4];
716 1.6.2.2 pgoyette size_t i;
717 1.6.2.2 pgoyette
718 1.6.2.2 pgoyette eax = cpudata->vmcb->state.rax;
719 1.6.2.2 pgoyette ecx = state->gprs[NVMM_X64_GPR_RCX];
720 1.6.2.2 pgoyette x86_cpuid2(eax, ecx, descs);
721 1.6.2.2 pgoyette
722 1.6.2.2 pgoyette cpudata->vmcb->state.rax = descs[0];
723 1.6.2.2 pgoyette state->gprs[NVMM_X64_GPR_RBX] = descs[1];
724 1.6.2.2 pgoyette state->gprs[NVMM_X64_GPR_RCX] = descs[2];
725 1.6.2.2 pgoyette state->gprs[NVMM_X64_GPR_RDX] = descs[3];
726 1.6.2.2 pgoyette
727 1.6.2.2 pgoyette for (i = 0; i < SVM_NCPUIDS; i++) {
728 1.6.2.2 pgoyette cpuid = &machdata->cpuid[i];
729 1.6.2.2 pgoyette if (!machdata->cpuidpresent[i]) {
730 1.6.2.2 pgoyette continue;
731 1.6.2.2 pgoyette }
732 1.6.2.2 pgoyette if (cpuid->leaf != eax) {
733 1.6.2.2 pgoyette continue;
734 1.6.2.2 pgoyette }
735 1.6.2.2 pgoyette
736 1.6.2.2 pgoyette /* del */
737 1.6.2.2 pgoyette cpudata->vmcb->state.rax &= ~cpuid->del.eax;
738 1.6.2.2 pgoyette state->gprs[NVMM_X64_GPR_RBX] &= ~cpuid->del.ebx;
739 1.6.2.2 pgoyette state->gprs[NVMM_X64_GPR_RCX] &= ~cpuid->del.ecx;
740 1.6.2.2 pgoyette state->gprs[NVMM_X64_GPR_RDX] &= ~cpuid->del.edx;
741 1.6.2.2 pgoyette
742 1.6.2.2 pgoyette /* set */
743 1.6.2.2 pgoyette cpudata->vmcb->state.rax |= cpuid->set.eax;
744 1.6.2.2 pgoyette state->gprs[NVMM_X64_GPR_RBX] |= cpuid->set.ebx;
745 1.6.2.2 pgoyette state->gprs[NVMM_X64_GPR_RCX] |= cpuid->set.ecx;
746 1.6.2.2 pgoyette state->gprs[NVMM_X64_GPR_RDX] |= cpuid->set.edx;
747 1.6.2.2 pgoyette
748 1.6.2.2 pgoyette break;
749 1.6.2.2 pgoyette }
750 1.6.2.2 pgoyette
751 1.6.2.2 pgoyette /* Overwrite non-tunable leaves. */
752 1.6.2.2 pgoyette svm_inkernel_handle_cpuid(vcpu, eax, ecx);
753 1.6.2.2 pgoyette
754 1.6.2.2 pgoyette /* For now we omit DBREGS. */
755 1.6.2.2 pgoyette if (__predict_false(cpudata->vmcb->state.rflags & PSL_T)) {
756 1.6.2.2 pgoyette svm_inject_db(mach, vcpu);
757 1.6.2.2 pgoyette }
758 1.6.2.2 pgoyette
759 1.6.2.2 pgoyette cpudata->vmcb->state.rip = cpudata->vmcb->ctrl.nrip;
760 1.6.2.2 pgoyette exit->reason = NVMM_EXIT_NONE;
761 1.6.2.2 pgoyette }
762 1.6.2.2 pgoyette
763 1.6.2.2 pgoyette #define SVM_EXIT_IO_PORT __BITS(31,16)
764 1.6.2.2 pgoyette #define SVM_EXIT_IO_SEG __BITS(12,10)
765 1.6.2.2 pgoyette #define SVM_EXIT_IO_A64 __BIT(9)
766 1.6.2.2 pgoyette #define SVM_EXIT_IO_A32 __BIT(8)
767 1.6.2.2 pgoyette #define SVM_EXIT_IO_A16 __BIT(7)
768 1.6.2.2 pgoyette #define SVM_EXIT_IO_SZ32 __BIT(6)
769 1.6.2.2 pgoyette #define SVM_EXIT_IO_SZ16 __BIT(5)
770 1.6.2.2 pgoyette #define SVM_EXIT_IO_SZ8 __BIT(4)
771 1.6.2.2 pgoyette #define SVM_EXIT_IO_REP __BIT(3)
772 1.6.2.2 pgoyette #define SVM_EXIT_IO_STR __BIT(2)
773 1.6.2.2 pgoyette #define SVM_EXIT_IO_IN __BIT(0)
774 1.6.2.2 pgoyette
775 1.6.2.2 pgoyette static const int seg_to_nvmm[] = {
776 1.6.2.2 pgoyette [0] = NVMM_X64_SEG_ES,
777 1.6.2.2 pgoyette [1] = NVMM_X64_SEG_CS,
778 1.6.2.2 pgoyette [2] = NVMM_X64_SEG_SS,
779 1.6.2.2 pgoyette [3] = NVMM_X64_SEG_DS,
780 1.6.2.2 pgoyette [4] = NVMM_X64_SEG_FS,
781 1.6.2.2 pgoyette [5] = NVMM_X64_SEG_GS
782 1.6.2.2 pgoyette };
783 1.6.2.2 pgoyette
784 1.6.2.2 pgoyette static void
785 1.6.2.2 pgoyette svm_exit_io(struct nvmm_machine *mach, struct nvmm_cpu *vcpu,
786 1.6.2.2 pgoyette struct nvmm_exit *exit)
787 1.6.2.2 pgoyette {
788 1.6.2.2 pgoyette struct svm_cpudata *cpudata = vcpu->cpudata;
789 1.6.2.2 pgoyette uint64_t info = cpudata->vmcb->ctrl.exitinfo1;
790 1.6.2.2 pgoyette uint64_t nextpc = cpudata->vmcb->ctrl.exitinfo2;
791 1.6.2.2 pgoyette
792 1.6.2.2 pgoyette exit->reason = NVMM_EXIT_IO;
793 1.6.2.2 pgoyette
794 1.6.2.2 pgoyette if (info & SVM_EXIT_IO_IN) {
795 1.6.2.2 pgoyette exit->u.io.type = NVMM_EXIT_IO_IN;
796 1.6.2.2 pgoyette } else {
797 1.6.2.2 pgoyette exit->u.io.type = NVMM_EXIT_IO_OUT;
798 1.6.2.2 pgoyette }
799 1.6.2.2 pgoyette
800 1.6.2.2 pgoyette exit->u.io.port = __SHIFTOUT(info, SVM_EXIT_IO_PORT);
801 1.6.2.2 pgoyette
802 1.6.2.2 pgoyette if (svm_decode_assist) {
803 1.6.2.2 pgoyette KASSERT(__SHIFTOUT(info, SVM_EXIT_IO_SEG) < 6);
804 1.6.2.2 pgoyette exit->u.io.seg = seg_to_nvmm[__SHIFTOUT(info, SVM_EXIT_IO_SEG)];
805 1.6.2.2 pgoyette } else {
806 1.6.2.2 pgoyette if (exit->u.io.type == NVMM_EXIT_IO_IN) {
807 1.6.2.2 pgoyette exit->u.io.seg = NVMM_X64_SEG_ES;
808 1.6.2.2 pgoyette } else {
809 1.6.2.2 pgoyette exit->u.io.seg = NVMM_X64_SEG_DS;
810 1.6.2.2 pgoyette }
811 1.6.2.2 pgoyette }
812 1.6.2.2 pgoyette
813 1.6.2.2 pgoyette if (info & SVM_EXIT_IO_A64) {
814 1.6.2.2 pgoyette exit->u.io.address_size = 8;
815 1.6.2.2 pgoyette } else if (info & SVM_EXIT_IO_A32) {
816 1.6.2.2 pgoyette exit->u.io.address_size = 4;
817 1.6.2.2 pgoyette } else if (info & SVM_EXIT_IO_A16) {
818 1.6.2.2 pgoyette exit->u.io.address_size = 2;
819 1.6.2.2 pgoyette }
820 1.6.2.2 pgoyette
821 1.6.2.2 pgoyette if (info & SVM_EXIT_IO_SZ32) {
822 1.6.2.2 pgoyette exit->u.io.operand_size = 4;
823 1.6.2.2 pgoyette } else if (info & SVM_EXIT_IO_SZ16) {
824 1.6.2.2 pgoyette exit->u.io.operand_size = 2;
825 1.6.2.2 pgoyette } else if (info & SVM_EXIT_IO_SZ8) {
826 1.6.2.2 pgoyette exit->u.io.operand_size = 1;
827 1.6.2.2 pgoyette }
828 1.6.2.2 pgoyette
829 1.6.2.2 pgoyette exit->u.io.rep = (info & SVM_EXIT_IO_REP) != 0;
830 1.6.2.2 pgoyette exit->u.io.str = (info & SVM_EXIT_IO_STR) != 0;
831 1.6.2.2 pgoyette exit->u.io.npc = nextpc;
832 1.6.2.2 pgoyette }
833 1.6.2.2 pgoyette
834 1.6.2.2 pgoyette static bool
835 1.6.2.2 pgoyette svm_inkernel_handle_msr(struct nvmm_machine *mach, struct nvmm_cpu *vcpu,
836 1.6.2.2 pgoyette struct nvmm_exit *exit)
837 1.6.2.2 pgoyette {
838 1.6.2.2 pgoyette struct svm_cpudata *cpudata = vcpu->cpudata;
839 1.6.2.2 pgoyette struct nvmm_x64_state *state = &cpudata->state;
840 1.6.2.2 pgoyette uint64_t pat;
841 1.6.2.2 pgoyette
842 1.6.2.2 pgoyette switch (exit->u.msr.type) {
843 1.6.2.2 pgoyette case NVMM_EXIT_MSR_RDMSR:
844 1.6.2.2 pgoyette if (exit->u.msr.msr == MSR_CR_PAT) {
845 1.6.2.2 pgoyette pat = cpudata->vmcb->state.g_pat;
846 1.6.2.2 pgoyette cpudata->vmcb->state.rax = (pat & 0xFFFFFFFF);
847 1.6.2.2 pgoyette state->gprs[NVMM_X64_GPR_RDX] = (pat >> 32);
848 1.6.2.2 pgoyette goto handled;
849 1.6.2.2 pgoyette }
850 1.6.2.2 pgoyette break;
851 1.6.2.2 pgoyette case NVMM_EXIT_MSR_WRMSR:
852 1.6.2.2 pgoyette if (exit->u.msr.msr == MSR_EFER) {
853 1.6.2.2 pgoyette if (__predict_false(exit->u.msr.val & ~EFER_VALID)) {
854 1.6.2.2 pgoyette svm_inject_gp(mach, vcpu);
855 1.6.2.2 pgoyette goto handled;
856 1.6.2.2 pgoyette }
857 1.6.2.2 pgoyette if ((cpudata->vmcb->state.efer ^ exit->u.msr.val) &
858 1.6.2.2 pgoyette EFER_TLB_FLUSH) {
859 1.6.2.2 pgoyette cpudata->tlb_want_flush = true;
860 1.6.2.2 pgoyette }
861 1.6.2.2 pgoyette cpudata->vmcb->state.efer = exit->u.msr.val | EFER_SVME;
862 1.6.2.2 pgoyette goto handled;
863 1.6.2.2 pgoyette }
864 1.6.2.2 pgoyette if (exit->u.msr.msr == MSR_CR_PAT) {
865 1.6.2.2 pgoyette cpudata->vmcb->state.g_pat = exit->u.msr.val;
866 1.6.2.2 pgoyette goto handled;
867 1.6.2.2 pgoyette }
868 1.6.2.2 pgoyette break;
869 1.6.2.2 pgoyette }
870 1.6.2.2 pgoyette
871 1.6.2.2 pgoyette return false;
872 1.6.2.2 pgoyette
873 1.6.2.2 pgoyette handled:
874 1.6.2.2 pgoyette cpudata->vmcb->state.rip = cpudata->vmcb->ctrl.nrip;
875 1.6.2.2 pgoyette return true;
876 1.6.2.2 pgoyette }
877 1.6.2.2 pgoyette
878 1.6.2.2 pgoyette static void
879 1.6.2.2 pgoyette svm_exit_msr(struct nvmm_machine *mach, struct nvmm_cpu *vcpu,
880 1.6.2.2 pgoyette struct nvmm_exit *exit)
881 1.6.2.2 pgoyette {
882 1.6.2.2 pgoyette struct svm_cpudata *cpudata = vcpu->cpudata;
883 1.6.2.2 pgoyette struct nvmm_x64_state *state = &cpudata->state;
884 1.6.2.2 pgoyette uint64_t info = cpudata->vmcb->ctrl.exitinfo1;
885 1.6.2.2 pgoyette
886 1.6.2.2 pgoyette if (info == 0) {
887 1.6.2.2 pgoyette exit->u.msr.type = NVMM_EXIT_MSR_RDMSR;
888 1.6.2.2 pgoyette } else {
889 1.6.2.2 pgoyette exit->u.msr.type = NVMM_EXIT_MSR_WRMSR;
890 1.6.2.2 pgoyette }
891 1.6.2.2 pgoyette
892 1.6.2.2 pgoyette exit->u.msr.msr = state->gprs[NVMM_X64_GPR_RCX];
893 1.6.2.2 pgoyette
894 1.6.2.2 pgoyette if (info == 1) {
895 1.6.2.2 pgoyette uint64_t rdx, rax;
896 1.6.2.2 pgoyette rdx = state->gprs[NVMM_X64_GPR_RDX];
897 1.6.2.2 pgoyette rax = cpudata->vmcb->state.rax;
898 1.6.2.2 pgoyette exit->u.msr.val = (rdx << 32) | (rax & 0xFFFFFFFF);
899 1.6.2.2 pgoyette } else {
900 1.6.2.2 pgoyette exit->u.msr.val = 0;
901 1.6.2.2 pgoyette }
902 1.6.2.2 pgoyette
903 1.6.2.2 pgoyette if (svm_inkernel_handle_msr(mach, vcpu, exit)) {
904 1.6.2.2 pgoyette exit->reason = NVMM_EXIT_NONE;
905 1.6.2.2 pgoyette return;
906 1.6.2.2 pgoyette }
907 1.6.2.2 pgoyette
908 1.6.2.2 pgoyette exit->reason = NVMM_EXIT_MSR;
909 1.6.2.2 pgoyette exit->u.msr.npc = cpudata->vmcb->ctrl.nrip;
910 1.6.2.2 pgoyette }
911 1.6.2.2 pgoyette
912 1.6.2.2 pgoyette static void
913 1.6.2.2 pgoyette svm_exit_npf(struct nvmm_machine *mach, struct nvmm_cpu *vcpu,
914 1.6.2.2 pgoyette struct nvmm_exit *exit)
915 1.6.2.2 pgoyette {
916 1.6.2.2 pgoyette struct svm_cpudata *cpudata = vcpu->cpudata;
917 1.6.2.2 pgoyette gpaddr_t gpa = cpudata->vmcb->ctrl.exitinfo2;
918 1.6.2.2 pgoyette int error;
919 1.6.2.2 pgoyette
920 1.6.2.2 pgoyette error = uvm_fault(&mach->vm->vm_map, gpa, VM_PROT_ALL);
921 1.6.2.2 pgoyette
922 1.6.2.2 pgoyette if (error) {
923 1.6.2.2 pgoyette exit->reason = NVMM_EXIT_MEMORY;
924 1.6.2.2 pgoyette if (cpudata->vmcb->ctrl.exitinfo1 & PGEX_W)
925 1.6.2.2 pgoyette exit->u.mem.perm = NVMM_EXIT_MEMORY_WRITE;
926 1.6.2.2 pgoyette else if (cpudata->vmcb->ctrl.exitinfo1 & PGEX_X)
927 1.6.2.2 pgoyette exit->u.mem.perm = NVMM_EXIT_MEMORY_EXEC;
928 1.6.2.2 pgoyette else
929 1.6.2.2 pgoyette exit->u.mem.perm = NVMM_EXIT_MEMORY_READ;
930 1.6.2.2 pgoyette exit->u.mem.gpa = gpa;
931 1.6.2.2 pgoyette exit->u.mem.inst_len = cpudata->vmcb->ctrl.inst_len;
932 1.6.2.2 pgoyette memcpy(exit->u.mem.inst_bytes, cpudata->vmcb->ctrl.inst_bytes,
933 1.6.2.2 pgoyette sizeof(exit->u.mem.inst_bytes));
934 1.6.2.2 pgoyette exit->u.mem.npc = cpudata->vmcb->ctrl.nrip;
935 1.6.2.2 pgoyette } else {
936 1.6.2.2 pgoyette exit->reason = NVMM_EXIT_NONE;
937 1.6.2.2 pgoyette }
938 1.6.2.2 pgoyette }
939 1.6.2.2 pgoyette
940 1.6.2.2 pgoyette static void
941 1.6.2.2 pgoyette svm_exit_xsetbv(struct nvmm_machine *mach, struct nvmm_cpu *vcpu,
942 1.6.2.2 pgoyette struct nvmm_exit *exit)
943 1.6.2.2 pgoyette {
944 1.6.2.2 pgoyette struct svm_cpudata *cpudata = vcpu->cpudata;
945 1.6.2.2 pgoyette struct nvmm_x64_state *state = &cpudata->state;
946 1.6.2.2 pgoyette struct vmcb *vmcb = cpudata->vmcb;
947 1.6.2.2 pgoyette uint64_t val;
948 1.6.2.2 pgoyette
949 1.6.2.2 pgoyette exit->reason = NVMM_EXIT_NONE;
950 1.6.2.2 pgoyette
951 1.6.2.2 pgoyette val = (state->gprs[NVMM_X64_GPR_RDX] << 32) |
952 1.6.2.2 pgoyette (vmcb->state.rax & 0xFFFFFFFF);
953 1.6.2.2 pgoyette
954 1.6.2.2 pgoyette if (__predict_false(state->gprs[NVMM_X64_GPR_RCX] != 0)) {
955 1.6.2.2 pgoyette goto error;
956 1.6.2.2 pgoyette } else if (__predict_false(vmcb->state.cpl != 0)) {
957 1.6.2.2 pgoyette goto error;
958 1.6.2.2 pgoyette } else if (__predict_false((val & ~svm_xcr0_mask) != 0)) {
959 1.6.2.2 pgoyette goto error;
960 1.6.2.2 pgoyette } else if (__predict_false((val & XCR0_X87) == 0)) {
961 1.6.2.2 pgoyette goto error;
962 1.6.2.2 pgoyette }
963 1.6.2.2 pgoyette
964 1.6.2.2 pgoyette state->crs[NVMM_X64_CR_XCR0] = val;
965 1.6.2.2 pgoyette
966 1.6.2.2 pgoyette return;
967 1.6.2.2 pgoyette
968 1.6.2.2 pgoyette error:
969 1.6.2.2 pgoyette svm_inject_gp(mach, vcpu);
970 1.6.2.2 pgoyette }
971 1.6.2.2 pgoyette
972 1.6.2.2 pgoyette static void
973 1.6.2.2 pgoyette svm_vmcb_cache_default(struct vmcb *vmcb)
974 1.6.2.2 pgoyette {
975 1.6.2.2 pgoyette vmcb->ctrl.vmcb_clean =
976 1.6.2.2 pgoyette VMCB_CTRL_VMCB_CLEAN_I |
977 1.6.2.2 pgoyette VMCB_CTRL_VMCB_CLEAN_IOPM |
978 1.6.2.2 pgoyette VMCB_CTRL_VMCB_CLEAN_ASID |
979 1.6.2.2 pgoyette VMCB_CTRL_VMCB_CLEAN_LBR |
980 1.6.2.2 pgoyette VMCB_CTRL_VMCB_CLEAN_AVIC;
981 1.6.2.2 pgoyette }
982 1.6.2.2 pgoyette
983 1.6.2.2 pgoyette static void
984 1.6.2.2 pgoyette svm_vmcb_cache_flush(struct vmcb *vmcb)
985 1.6.2.2 pgoyette {
986 1.6.2.2 pgoyette vmcb->ctrl.vmcb_clean = 0;
987 1.6.2.2 pgoyette }
988 1.6.2.2 pgoyette
989 1.6.2.2 pgoyette static void
990 1.6.2.2 pgoyette svm_vcpu_guest_fpu_enter(struct nvmm_cpu *vcpu)
991 1.6.2.2 pgoyette {
992 1.6.2.2 pgoyette struct svm_cpudata *cpudata = vcpu->cpudata;
993 1.6.2.2 pgoyette
994 1.6.2.2 pgoyette if (x86_xsave_features != 0) {
995 1.6.2.2 pgoyette cpudata->xcr0 = rdxcr(0);
996 1.6.2.2 pgoyette wrxcr(0, cpudata->state.crs[NVMM_X64_CR_XCR0]);
997 1.6.2.2 pgoyette }
998 1.6.2.2 pgoyette
999 1.6.2.2 pgoyette cpudata->ts_set = (rcr0() & CR0_TS) != 0;
1000 1.6.2.2 pgoyette
1001 1.6.2.2 pgoyette fpu_area_save(&cpudata->hfpu);
1002 1.6.2.2 pgoyette fpu_area_restore(&cpudata->gfpu);
1003 1.6.2.2 pgoyette }
1004 1.6.2.2 pgoyette
1005 1.6.2.2 pgoyette static void
1006 1.6.2.2 pgoyette svm_vcpu_guest_fpu_leave(struct nvmm_cpu *vcpu)
1007 1.6.2.2 pgoyette {
1008 1.6.2.2 pgoyette struct svm_cpudata *cpudata = vcpu->cpudata;
1009 1.6.2.2 pgoyette
1010 1.6.2.2 pgoyette fpu_area_save(&cpudata->gfpu);
1011 1.6.2.2 pgoyette fpu_area_restore(&cpudata->hfpu);
1012 1.6.2.2 pgoyette
1013 1.6.2.2 pgoyette if (cpudata->ts_set) {
1014 1.6.2.2 pgoyette stts();
1015 1.6.2.2 pgoyette }
1016 1.6.2.2 pgoyette
1017 1.6.2.2 pgoyette if (x86_xsave_features != 0) {
1018 1.6.2.2 pgoyette cpudata->state.crs[NVMM_X64_CR_XCR0] = rdxcr(0);
1019 1.6.2.2 pgoyette wrxcr(0, cpudata->xcr0);
1020 1.6.2.2 pgoyette }
1021 1.6.2.2 pgoyette }
1022 1.6.2.2 pgoyette
1023 1.6.2.2 pgoyette static void
1024 1.6.2.2 pgoyette svm_vcpu_guest_dbregs_enter(struct nvmm_cpu *vcpu)
1025 1.6.2.2 pgoyette {
1026 1.6.2.2 pgoyette struct svm_cpudata *cpudata = vcpu->cpudata;
1027 1.6.2.2 pgoyette struct nvmm_x64_state *state = &cpudata->state;
1028 1.6.2.2 pgoyette
1029 1.6.2.2 pgoyette x86_dbregs_save(curlwp);
1030 1.6.2.2 pgoyette
1031 1.6.2.2 pgoyette ldr0(state->drs[NVMM_X64_DR_DR0]);
1032 1.6.2.2 pgoyette ldr1(state->drs[NVMM_X64_DR_DR1]);
1033 1.6.2.2 pgoyette ldr2(state->drs[NVMM_X64_DR_DR2]);
1034 1.6.2.2 pgoyette ldr3(state->drs[NVMM_X64_DR_DR3]);
1035 1.6.2.2 pgoyette }
1036 1.6.2.2 pgoyette
1037 1.6.2.2 pgoyette static void
1038 1.6.2.2 pgoyette svm_vcpu_guest_dbregs_leave(struct nvmm_cpu *vcpu)
1039 1.6.2.2 pgoyette {
1040 1.6.2.2 pgoyette struct svm_cpudata *cpudata = vcpu->cpudata;
1041 1.6.2.2 pgoyette struct nvmm_x64_state *state = &cpudata->state;
1042 1.6.2.2 pgoyette
1043 1.6.2.2 pgoyette state->drs[NVMM_X64_DR_DR0] = rdr0();
1044 1.6.2.2 pgoyette state->drs[NVMM_X64_DR_DR1] = rdr1();
1045 1.6.2.2 pgoyette state->drs[NVMM_X64_DR_DR2] = rdr2();
1046 1.6.2.2 pgoyette state->drs[NVMM_X64_DR_DR3] = rdr3();
1047 1.6.2.2 pgoyette
1048 1.6.2.2 pgoyette x86_dbregs_restore(curlwp);
1049 1.6.2.2 pgoyette }
1050 1.6.2.2 pgoyette
1051 1.6.2.2 pgoyette static void
1052 1.6.2.2 pgoyette svm_vcpu_guest_misc_enter(struct nvmm_cpu *vcpu)
1053 1.6.2.2 pgoyette {
1054 1.6.2.2 pgoyette struct svm_cpudata *cpudata = vcpu->cpudata;
1055 1.6.2.2 pgoyette
1056 1.6.2.2 pgoyette /* Save the fixed Host MSRs. */
1057 1.6.2.2 pgoyette cpudata->star = rdmsr(MSR_STAR);
1058 1.6.2.2 pgoyette cpudata->lstar = rdmsr(MSR_LSTAR);
1059 1.6.2.2 pgoyette cpudata->cstar = rdmsr(MSR_CSTAR);
1060 1.6.2.2 pgoyette cpudata->sfmask = rdmsr(MSR_SFMASK);
1061 1.6.2.2 pgoyette
1062 1.6.2.2 pgoyette /* Save the Host CR2. */
1063 1.6.2.2 pgoyette cpudata->cr2 = rcr2();
1064 1.6.2.2 pgoyette }
1065 1.6.2.2 pgoyette
1066 1.6.2.2 pgoyette static void
1067 1.6.2.2 pgoyette svm_vcpu_guest_misc_leave(struct nvmm_cpu *vcpu)
1068 1.6.2.2 pgoyette {
1069 1.6.2.2 pgoyette struct svm_cpudata *cpudata = vcpu->cpudata;
1070 1.6.2.2 pgoyette
1071 1.6.2.2 pgoyette /* Restore the fixed Host MSRs. */
1072 1.6.2.2 pgoyette wrmsr(MSR_STAR, cpudata->star);
1073 1.6.2.2 pgoyette wrmsr(MSR_LSTAR, cpudata->lstar);
1074 1.6.2.2 pgoyette wrmsr(MSR_CSTAR, cpudata->cstar);
1075 1.6.2.2 pgoyette wrmsr(MSR_SFMASK, cpudata->sfmask);
1076 1.6.2.2 pgoyette
1077 1.6.2.2 pgoyette /* Restore the Host CR2. */
1078 1.6.2.2 pgoyette lcr2(cpudata->cr2);
1079 1.6.2.2 pgoyette }
1080 1.6.2.2 pgoyette
1081 1.6.2.2 pgoyette static int
1082 1.6.2.2 pgoyette svm_vcpu_run(struct nvmm_machine *mach, struct nvmm_cpu *vcpu,
1083 1.6.2.2 pgoyette struct nvmm_exit *exit)
1084 1.6.2.2 pgoyette {
1085 1.6.2.2 pgoyette struct svm_cpudata *cpudata = vcpu->cpudata;
1086 1.6.2.2 pgoyette struct vmcb *vmcb = cpudata->vmcb;
1087 1.6.2.2 pgoyette bool tlb_need_flush = false;
1088 1.6.2.2 pgoyette int hcpu, s;
1089 1.6.2.2 pgoyette
1090 1.6.2.2 pgoyette kpreempt_disable();
1091 1.6.2.2 pgoyette hcpu = cpu_number();
1092 1.6.2.2 pgoyette
1093 1.6.2.2 pgoyette if (vcpu->hcpu_last != hcpu || cpudata->shared_asid) {
1094 1.6.2.2 pgoyette tlb_need_flush = true;
1095 1.6.2.2 pgoyette }
1096 1.6.2.2 pgoyette
1097 1.6.2.2 pgoyette if (cpudata->tlb_want_flush || tlb_need_flush) {
1098 1.6.2.2 pgoyette vmcb->ctrl.tlb_ctrl = svm_ctrl_tlb_flush;
1099 1.6.2.2 pgoyette } else {
1100 1.6.2.2 pgoyette vmcb->ctrl.tlb_ctrl = 0;
1101 1.6.2.2 pgoyette }
1102 1.6.2.2 pgoyette
1103 1.6.2.2 pgoyette if (vcpu->hcpu_last != hcpu) {
1104 1.6.2.2 pgoyette vmcb->ctrl.tsc_offset = cpudata->tsc_offset +
1105 1.6.2.2 pgoyette curcpu()->ci_data.cpu_cc_skew;
1106 1.6.2.2 pgoyette svm_vmcb_cache_flush(vmcb);
1107 1.6.2.2 pgoyette }
1108 1.6.2.2 pgoyette
1109 1.6.2.2 pgoyette svm_vcpu_guest_dbregs_enter(vcpu);
1110 1.6.2.2 pgoyette svm_vcpu_guest_misc_enter(vcpu);
1111 1.6.2.2 pgoyette
1112 1.6.2.2 pgoyette while (1) {
1113 1.6.2.2 pgoyette s = splhigh();
1114 1.6.2.2 pgoyette svm_vcpu_guest_fpu_enter(vcpu);
1115 1.6.2.2 pgoyette svm_vmrun(cpudata->vmcb_pa, cpudata->state.gprs);
1116 1.6.2.2 pgoyette svm_vcpu_guest_fpu_leave(vcpu);
1117 1.6.2.2 pgoyette splx(s);
1118 1.6.2.2 pgoyette
1119 1.6.2.2 pgoyette svm_vmcb_cache_default(vmcb);
1120 1.6.2.2 pgoyette
1121 1.6.2.2 pgoyette if (vmcb->ctrl.exitcode != VMCB_EXITCODE_INVALID) {
1122 1.6.2.2 pgoyette if (cpudata->tlb_want_flush) {
1123 1.6.2.2 pgoyette cpudata->tlb_want_flush = false;
1124 1.6.2.2 pgoyette }
1125 1.6.2.2 pgoyette vcpu->hcpu_last = hcpu;
1126 1.6.2.2 pgoyette }
1127 1.6.2.2 pgoyette
1128 1.6.2.2 pgoyette switch (vmcb->ctrl.exitcode) {
1129 1.6.2.2 pgoyette case VMCB_EXITCODE_INTR:
1130 1.6.2.2 pgoyette case VMCB_EXITCODE_NMI:
1131 1.6.2.2 pgoyette exit->reason = NVMM_EXIT_NONE;
1132 1.6.2.2 pgoyette break;
1133 1.6.2.2 pgoyette case VMCB_EXITCODE_VINTR:
1134 1.6.2.2 pgoyette svm_event_waitexit_disable(vmcb, false);
1135 1.6.2.2 pgoyette exit->reason = NVMM_EXIT_INT_READY;
1136 1.6.2.2 pgoyette break;
1137 1.6.2.2 pgoyette case VMCB_EXITCODE_IRET:
1138 1.6.2.2 pgoyette svm_event_waitexit_disable(vmcb, true);
1139 1.6.2.2 pgoyette cpudata->in_nmi = false;
1140 1.6.2.2 pgoyette exit->reason = NVMM_EXIT_NMI_READY;
1141 1.6.2.2 pgoyette break;
1142 1.6.2.2 pgoyette case VMCB_EXITCODE_CPUID:
1143 1.6.2.2 pgoyette svm_exit_cpuid(mach, vcpu, exit);
1144 1.6.2.2 pgoyette break;
1145 1.6.2.2 pgoyette case VMCB_EXITCODE_HLT:
1146 1.6.2.2 pgoyette exit->reason = NVMM_EXIT_HLT;
1147 1.6.2.2 pgoyette break;
1148 1.6.2.2 pgoyette case VMCB_EXITCODE_IOIO:
1149 1.6.2.2 pgoyette svm_exit_io(mach, vcpu, exit);
1150 1.6.2.2 pgoyette break;
1151 1.6.2.2 pgoyette case VMCB_EXITCODE_MSR:
1152 1.6.2.2 pgoyette svm_exit_msr(mach, vcpu, exit);
1153 1.6.2.2 pgoyette break;
1154 1.6.2.2 pgoyette case VMCB_EXITCODE_SHUTDOWN:
1155 1.6.2.2 pgoyette exit->reason = NVMM_EXIT_SHUTDOWN;
1156 1.6.2.2 pgoyette break;
1157 1.6.2.2 pgoyette case VMCB_EXITCODE_RDPMC:
1158 1.6.2.2 pgoyette case VMCB_EXITCODE_RSM:
1159 1.6.2.2 pgoyette case VMCB_EXITCODE_INVLPGA:
1160 1.6.2.2 pgoyette case VMCB_EXITCODE_VMRUN:
1161 1.6.2.2 pgoyette case VMCB_EXITCODE_VMMCALL:
1162 1.6.2.2 pgoyette case VMCB_EXITCODE_VMLOAD:
1163 1.6.2.2 pgoyette case VMCB_EXITCODE_VMSAVE:
1164 1.6.2.2 pgoyette case VMCB_EXITCODE_STGI:
1165 1.6.2.2 pgoyette case VMCB_EXITCODE_CLGI:
1166 1.6.2.2 pgoyette case VMCB_EXITCODE_SKINIT:
1167 1.6.2.2 pgoyette case VMCB_EXITCODE_RDTSCP:
1168 1.6.2.2 pgoyette svm_inject_ud(mach, vcpu);
1169 1.6.2.2 pgoyette exit->reason = NVMM_EXIT_NONE;
1170 1.6.2.2 pgoyette break;
1171 1.6.2.2 pgoyette case VMCB_EXITCODE_MONITOR:
1172 1.6.2.2 pgoyette exit->reason = NVMM_EXIT_MONITOR;
1173 1.6.2.2 pgoyette break;
1174 1.6.2.2 pgoyette case VMCB_EXITCODE_MWAIT:
1175 1.6.2.2 pgoyette exit->reason = NVMM_EXIT_MWAIT;
1176 1.6.2.2 pgoyette break;
1177 1.6.2.2 pgoyette case VMCB_EXITCODE_MWAIT_CONDITIONAL:
1178 1.6.2.2 pgoyette exit->reason = NVMM_EXIT_MWAIT_COND;
1179 1.6.2.2 pgoyette break;
1180 1.6.2.2 pgoyette case VMCB_EXITCODE_XSETBV:
1181 1.6.2.2 pgoyette svm_exit_xsetbv(mach, vcpu, exit);
1182 1.6.2.2 pgoyette break;
1183 1.6.2.2 pgoyette case VMCB_EXITCODE_NPF:
1184 1.6.2.2 pgoyette svm_exit_npf(mach, vcpu, exit);
1185 1.6.2.2 pgoyette break;
1186 1.6.2.2 pgoyette case VMCB_EXITCODE_FERR_FREEZE: /* ? */
1187 1.6.2.2 pgoyette default:
1188 1.6.2.2 pgoyette exit->reason = NVMM_EXIT_INVALID;
1189 1.6.2.2 pgoyette break;
1190 1.6.2.2 pgoyette }
1191 1.6.2.2 pgoyette
1192 1.6.2.2 pgoyette /* If no reason to return to userland, keep rolling. */
1193 1.6.2.2 pgoyette if (curcpu()->ci_schedstate.spc_flags & SPCF_SHOULDYIELD) {
1194 1.6.2.2 pgoyette break;
1195 1.6.2.2 pgoyette }
1196 1.6.2.2 pgoyette if (exit->reason != NVMM_EXIT_NONE) {
1197 1.6.2.2 pgoyette break;
1198 1.6.2.2 pgoyette }
1199 1.6.2.2 pgoyette }
1200 1.6.2.2 pgoyette
1201 1.6.2.2 pgoyette svm_vcpu_guest_misc_leave(vcpu);
1202 1.6.2.2 pgoyette svm_vcpu_guest_dbregs_leave(vcpu);
1203 1.6.2.2 pgoyette
1204 1.6.2.2 pgoyette kpreempt_enable();
1205 1.6.2.2 pgoyette
1206 1.6.2.2 pgoyette exit->exitstate[NVMM_X64_EXITSTATE_CR8] = __SHIFTOUT(vmcb->ctrl.v,
1207 1.6.2.2 pgoyette VMCB_CTRL_V_TPR);
1208 1.6.2.2 pgoyette exit->exitstate[NVMM_X64_EXITSTATE_RFLAGS] = vmcb->state.rflags;
1209 1.6.2.2 pgoyette
1210 1.6.2.2 pgoyette return 0;
1211 1.6.2.2 pgoyette }
1212 1.6.2.2 pgoyette
1213 1.6.2.2 pgoyette /* -------------------------------------------------------------------------- */
1214 1.6.2.2 pgoyette
1215 1.6.2.2 pgoyette static int
1216 1.6.2.2 pgoyette svm_memalloc(paddr_t *pa, vaddr_t *va, size_t npages)
1217 1.6.2.2 pgoyette {
1218 1.6.2.2 pgoyette struct pglist pglist;
1219 1.6.2.2 pgoyette paddr_t _pa;
1220 1.6.2.2 pgoyette vaddr_t _va;
1221 1.6.2.2 pgoyette size_t i;
1222 1.6.2.2 pgoyette int ret;
1223 1.6.2.2 pgoyette
1224 1.6.2.2 pgoyette ret = uvm_pglistalloc(npages * PAGE_SIZE, 0, ~0UL, PAGE_SIZE, 0,
1225 1.6.2.2 pgoyette &pglist, 1, 0);
1226 1.6.2.2 pgoyette if (ret != 0)
1227 1.6.2.2 pgoyette return ENOMEM;
1228 1.6.2.2 pgoyette _pa = TAILQ_FIRST(&pglist)->phys_addr;
1229 1.6.2.2 pgoyette _va = uvm_km_alloc(kernel_map, npages * PAGE_SIZE, 0,
1230 1.6.2.2 pgoyette UVM_KMF_VAONLY | UVM_KMF_NOWAIT);
1231 1.6.2.2 pgoyette if (_va == 0)
1232 1.6.2.2 pgoyette goto error;
1233 1.6.2.2 pgoyette
1234 1.6.2.2 pgoyette for (i = 0; i < npages; i++) {
1235 1.6.2.2 pgoyette pmap_kenter_pa(_va + i * PAGE_SIZE, _pa + i * PAGE_SIZE,
1236 1.6.2.2 pgoyette VM_PROT_READ | VM_PROT_WRITE, PMAP_WRITE_BACK);
1237 1.6.2.2 pgoyette }
1238 1.6.2.2 pgoyette pmap_update(pmap_kernel());
1239 1.6.2.2 pgoyette
1240 1.6.2.2 pgoyette memset((void *)_va, 0, npages * PAGE_SIZE);
1241 1.6.2.2 pgoyette
1242 1.6.2.2 pgoyette *pa = _pa;
1243 1.6.2.2 pgoyette *va = _va;
1244 1.6.2.2 pgoyette return 0;
1245 1.6.2.2 pgoyette
1246 1.6.2.2 pgoyette error:
1247 1.6.2.2 pgoyette for (i = 0; i < npages; i++) {
1248 1.6.2.2 pgoyette uvm_pagefree(PHYS_TO_VM_PAGE(_pa + i * PAGE_SIZE));
1249 1.6.2.2 pgoyette }
1250 1.6.2.2 pgoyette return ENOMEM;
1251 1.6.2.2 pgoyette }
1252 1.6.2.2 pgoyette
1253 1.6.2.2 pgoyette static void
1254 1.6.2.2 pgoyette svm_memfree(paddr_t pa, vaddr_t va, size_t npages)
1255 1.6.2.2 pgoyette {
1256 1.6.2.2 pgoyette size_t i;
1257 1.6.2.2 pgoyette
1258 1.6.2.2 pgoyette pmap_kremove(va, npages * PAGE_SIZE);
1259 1.6.2.2 pgoyette pmap_update(pmap_kernel());
1260 1.6.2.2 pgoyette uvm_km_free(kernel_map, va, npages * PAGE_SIZE, UVM_KMF_VAONLY);
1261 1.6.2.2 pgoyette for (i = 0; i < npages; i++) {
1262 1.6.2.2 pgoyette uvm_pagefree(PHYS_TO_VM_PAGE(pa + i * PAGE_SIZE));
1263 1.6.2.2 pgoyette }
1264 1.6.2.2 pgoyette }
1265 1.6.2.2 pgoyette
1266 1.6.2.2 pgoyette /* -------------------------------------------------------------------------- */
1267 1.6.2.2 pgoyette
1268 1.6.2.2 pgoyette #define SVM_MSRBM_READ __BIT(0)
1269 1.6.2.2 pgoyette #define SVM_MSRBM_WRITE __BIT(1)
1270 1.6.2.2 pgoyette
1271 1.6.2.2 pgoyette static void
1272 1.6.2.2 pgoyette svm_vcpu_msr_allow(uint8_t *bitmap, uint64_t msr, bool read, bool write)
1273 1.6.2.2 pgoyette {
1274 1.6.2.2 pgoyette uint64_t byte;
1275 1.6.2.2 pgoyette uint8_t bitoff;
1276 1.6.2.2 pgoyette
1277 1.6.2.2 pgoyette if (msr < 0x00002000) {
1278 1.6.2.2 pgoyette /* Range 1 */
1279 1.6.2.2 pgoyette byte = ((msr - 0x00000000) >> 2UL) + 0x0000;
1280 1.6.2.2 pgoyette } else if (msr >= 0xC0000000 && msr < 0xC0002000) {
1281 1.6.2.2 pgoyette /* Range 2 */
1282 1.6.2.2 pgoyette byte = ((msr - 0xC0000000) >> 2UL) + 0x0800;
1283 1.6.2.2 pgoyette } else if (msr >= 0xC0010000 && msr < 0xC0012000) {
1284 1.6.2.2 pgoyette /* Range 3 */
1285 1.6.2.2 pgoyette byte = ((msr - 0xC0010000) >> 2UL) + 0x1000;
1286 1.6.2.2 pgoyette } else {
1287 1.6.2.2 pgoyette panic("%s: wrong range", __func__);
1288 1.6.2.2 pgoyette }
1289 1.6.2.2 pgoyette
1290 1.6.2.2 pgoyette bitoff = (msr & 0x3) << 1;
1291 1.6.2.2 pgoyette
1292 1.6.2.2 pgoyette if (read) {
1293 1.6.2.2 pgoyette bitmap[byte] &= ~(SVM_MSRBM_READ << bitoff);
1294 1.6.2.2 pgoyette }
1295 1.6.2.2 pgoyette if (write) {
1296 1.6.2.2 pgoyette bitmap[byte] &= ~(SVM_MSRBM_WRITE << bitoff);
1297 1.6.2.2 pgoyette }
1298 1.6.2.2 pgoyette }
1299 1.6.2.2 pgoyette
1300 1.6.2.2 pgoyette static void
1301 1.6.2.2 pgoyette svm_asid_alloc(struct nvmm_cpu *vcpu)
1302 1.6.2.2 pgoyette {
1303 1.6.2.2 pgoyette struct svm_cpudata *cpudata = vcpu->cpudata;
1304 1.6.2.2 pgoyette struct vmcb *vmcb = cpudata->vmcb;
1305 1.6.2.2 pgoyette size_t i, oct, bit;
1306 1.6.2.2 pgoyette
1307 1.6.2.2 pgoyette mutex_enter(&svm_asidlock);
1308 1.6.2.2 pgoyette
1309 1.6.2.2 pgoyette for (i = 0; i < svm_maxasid; i++) {
1310 1.6.2.2 pgoyette oct = i / 8;
1311 1.6.2.2 pgoyette bit = i % 8;
1312 1.6.2.2 pgoyette
1313 1.6.2.2 pgoyette if (svm_asidmap[oct] & __BIT(bit)) {
1314 1.6.2.2 pgoyette continue;
1315 1.6.2.2 pgoyette }
1316 1.6.2.2 pgoyette
1317 1.6.2.2 pgoyette svm_asidmap[oct] |= __BIT(bit);
1318 1.6.2.2 pgoyette vmcb->ctrl.guest_asid = i;
1319 1.6.2.2 pgoyette mutex_exit(&svm_asidlock);
1320 1.6.2.2 pgoyette return;
1321 1.6.2.2 pgoyette }
1322 1.6.2.2 pgoyette
1323 1.6.2.2 pgoyette /*
1324 1.6.2.2 pgoyette * No free ASID. Use the last one, which is shared and requires
1325 1.6.2.2 pgoyette * special TLB handling.
1326 1.6.2.2 pgoyette */
1327 1.6.2.2 pgoyette cpudata->shared_asid = true;
1328 1.6.2.2 pgoyette vmcb->ctrl.guest_asid = svm_maxasid - 1;
1329 1.6.2.2 pgoyette mutex_exit(&svm_asidlock);
1330 1.6.2.2 pgoyette }
1331 1.6.2.2 pgoyette
1332 1.6.2.2 pgoyette static void
1333 1.6.2.2 pgoyette svm_asid_free(struct nvmm_cpu *vcpu)
1334 1.6.2.2 pgoyette {
1335 1.6.2.2 pgoyette struct svm_cpudata *cpudata = vcpu->cpudata;
1336 1.6.2.2 pgoyette struct vmcb *vmcb = cpudata->vmcb;
1337 1.6.2.2 pgoyette size_t oct, bit;
1338 1.6.2.2 pgoyette
1339 1.6.2.2 pgoyette if (cpudata->shared_asid) {
1340 1.6.2.2 pgoyette return;
1341 1.6.2.2 pgoyette }
1342 1.6.2.2 pgoyette
1343 1.6.2.2 pgoyette oct = vmcb->ctrl.guest_asid / 8;
1344 1.6.2.2 pgoyette bit = vmcb->ctrl.guest_asid % 8;
1345 1.6.2.2 pgoyette
1346 1.6.2.2 pgoyette mutex_enter(&svm_asidlock);
1347 1.6.2.2 pgoyette svm_asidmap[oct] &= ~__BIT(bit);
1348 1.6.2.2 pgoyette mutex_exit(&svm_asidlock);
1349 1.6.2.2 pgoyette }
1350 1.6.2.2 pgoyette
1351 1.6.2.2 pgoyette static void
1352 1.6.2.2 pgoyette svm_vcpu_init(struct nvmm_machine *mach, struct nvmm_cpu *vcpu)
1353 1.6.2.2 pgoyette {
1354 1.6.2.2 pgoyette struct svm_cpudata *cpudata = vcpu->cpudata;
1355 1.6.2.2 pgoyette struct vmcb *vmcb = cpudata->vmcb;
1356 1.6.2.2 pgoyette
1357 1.6.2.2 pgoyette /* Allow reads/writes of Control Registers. */
1358 1.6.2.2 pgoyette vmcb->ctrl.intercept_cr = 0;
1359 1.6.2.2 pgoyette
1360 1.6.2.2 pgoyette /* Allow reads/writes of Debug Registers. */
1361 1.6.2.2 pgoyette vmcb->ctrl.intercept_dr = 0;
1362 1.6.2.2 pgoyette
1363 1.6.2.2 pgoyette /* Allow exceptions 0 to 31. */
1364 1.6.2.2 pgoyette vmcb->ctrl.intercept_vec = 0;
1365 1.6.2.2 pgoyette
1366 1.6.2.2 pgoyette /*
1367 1.6.2.2 pgoyette * Allow:
1368 1.6.2.2 pgoyette * - SMI [smm interrupts]
1369 1.6.2.2 pgoyette * - VINTR [virtual interrupts]
1370 1.6.2.2 pgoyette * - CR0_SPEC [CR0 writes changing other fields than CR0.TS or CR0.MP]
1371 1.6.2.2 pgoyette * - RIDTR [reads of IDTR]
1372 1.6.2.2 pgoyette * - RGDTR [reads of GDTR]
1373 1.6.2.2 pgoyette * - RLDTR [reads of LDTR]
1374 1.6.2.2 pgoyette * - RTR [reads of TR]
1375 1.6.2.2 pgoyette * - WIDTR [writes of IDTR]
1376 1.6.2.2 pgoyette * - WGDTR [writes of GDTR]
1377 1.6.2.2 pgoyette * - WLDTR [writes of LDTR]
1378 1.6.2.2 pgoyette * - WTR [writes of TR]
1379 1.6.2.2 pgoyette * - RDTSC [rdtsc instruction]
1380 1.6.2.2 pgoyette * - PUSHF [pushf instruction]
1381 1.6.2.2 pgoyette * - POPF [popf instruction]
1382 1.6.2.2 pgoyette * - IRET [iret instruction]
1383 1.6.2.2 pgoyette * - INTN [int $n instructions]
1384 1.6.2.2 pgoyette * - INVD [invd instruction]
1385 1.6.2.2 pgoyette * - PAUSE [pause instruction]
1386 1.6.2.2 pgoyette * - INVLPG [invplg instruction]
1387 1.6.2.2 pgoyette * - TASKSW [task switches]
1388 1.6.2.2 pgoyette *
1389 1.6.2.2 pgoyette * Intercept the rest below.
1390 1.6.2.2 pgoyette */
1391 1.6.2.2 pgoyette vmcb->ctrl.intercept_misc1 =
1392 1.6.2.2 pgoyette VMCB_CTRL_INTERCEPT_INTR |
1393 1.6.2.2 pgoyette VMCB_CTRL_INTERCEPT_NMI |
1394 1.6.2.2 pgoyette VMCB_CTRL_INTERCEPT_INIT |
1395 1.6.2.2 pgoyette VMCB_CTRL_INTERCEPT_RDPMC |
1396 1.6.2.2 pgoyette VMCB_CTRL_INTERCEPT_CPUID |
1397 1.6.2.2 pgoyette VMCB_CTRL_INTERCEPT_RSM |
1398 1.6.2.2 pgoyette VMCB_CTRL_INTERCEPT_HLT |
1399 1.6.2.2 pgoyette VMCB_CTRL_INTERCEPT_INVLPGA |
1400 1.6.2.2 pgoyette VMCB_CTRL_INTERCEPT_IOIO_PROT |
1401 1.6.2.2 pgoyette VMCB_CTRL_INTERCEPT_MSR_PROT |
1402 1.6.2.2 pgoyette VMCB_CTRL_INTERCEPT_FERR_FREEZE |
1403 1.6.2.2 pgoyette VMCB_CTRL_INTERCEPT_SHUTDOWN;
1404 1.6.2.2 pgoyette
1405 1.6.2.2 pgoyette /*
1406 1.6.2.2 pgoyette * Allow:
1407 1.6.2.2 pgoyette * - ICEBP [icebp instruction]
1408 1.6.2.2 pgoyette * - WBINVD [wbinvd instruction]
1409 1.6.2.2 pgoyette * - WCR_SPEC(0..15) [writes of CR0-15, received after instruction]
1410 1.6.2.2 pgoyette *
1411 1.6.2.2 pgoyette * Intercept the rest below.
1412 1.6.2.2 pgoyette */
1413 1.6.2.2 pgoyette vmcb->ctrl.intercept_misc2 =
1414 1.6.2.2 pgoyette VMCB_CTRL_INTERCEPT_VMRUN |
1415 1.6.2.2 pgoyette VMCB_CTRL_INTERCEPT_VMMCALL |
1416 1.6.2.2 pgoyette VMCB_CTRL_INTERCEPT_VMLOAD |
1417 1.6.2.2 pgoyette VMCB_CTRL_INTERCEPT_VMSAVE |
1418 1.6.2.2 pgoyette VMCB_CTRL_INTERCEPT_STGI |
1419 1.6.2.2 pgoyette VMCB_CTRL_INTERCEPT_CLGI |
1420 1.6.2.2 pgoyette VMCB_CTRL_INTERCEPT_SKINIT |
1421 1.6.2.2 pgoyette VMCB_CTRL_INTERCEPT_RDTSCP |
1422 1.6.2.2 pgoyette VMCB_CTRL_INTERCEPT_MONITOR |
1423 1.6.2.2 pgoyette VMCB_CTRL_INTERCEPT_MWAIT |
1424 1.6.2.2 pgoyette VMCB_CTRL_INTERCEPT_XSETBV;
1425 1.6.2.2 pgoyette
1426 1.6.2.2 pgoyette /* Intercept all I/O accesses. */
1427 1.6.2.2 pgoyette memset(cpudata->iobm, 0xFF, IOBM_SIZE);
1428 1.6.2.2 pgoyette vmcb->ctrl.iopm_base_pa = cpudata->iobm_pa;
1429 1.6.2.2 pgoyette
1430 1.6.2.2 pgoyette /*
1431 1.6.2.2 pgoyette * Allow:
1432 1.6.2.2 pgoyette * - EFER [read]
1433 1.6.2.2 pgoyette * - STAR [read, write]
1434 1.6.2.2 pgoyette * - LSTAR [read, write]
1435 1.6.2.2 pgoyette * - CSTAR [read, write]
1436 1.6.2.2 pgoyette * - SFMASK [read, write]
1437 1.6.2.2 pgoyette * - KERNELGSBASE [read, write]
1438 1.6.2.2 pgoyette * - SYSENTER_CS [read, write]
1439 1.6.2.2 pgoyette * - SYSENTER_ESP [read, write]
1440 1.6.2.2 pgoyette * - SYSENTER_EIP [read, write]
1441 1.6.2.2 pgoyette * - FSBASE [read, write]
1442 1.6.2.2 pgoyette * - GSBASE [read, write]
1443 1.6.2.2 pgoyette *
1444 1.6.2.2 pgoyette * Intercept the rest.
1445 1.6.2.2 pgoyette */
1446 1.6.2.2 pgoyette memset(cpudata->msrbm, 0xFF, MSRBM_SIZE);
1447 1.6.2.2 pgoyette svm_vcpu_msr_allow(cpudata->msrbm, MSR_EFER, true, false);
1448 1.6.2.2 pgoyette svm_vcpu_msr_allow(cpudata->msrbm, MSR_STAR, true, true);
1449 1.6.2.2 pgoyette svm_vcpu_msr_allow(cpudata->msrbm, MSR_LSTAR, true, true);
1450 1.6.2.2 pgoyette svm_vcpu_msr_allow(cpudata->msrbm, MSR_CSTAR, true, true);
1451 1.6.2.2 pgoyette svm_vcpu_msr_allow(cpudata->msrbm, MSR_SFMASK, true, true);
1452 1.6.2.2 pgoyette svm_vcpu_msr_allow(cpudata->msrbm, MSR_KERNELGSBASE, true, true);
1453 1.6.2.2 pgoyette svm_vcpu_msr_allow(cpudata->msrbm, MSR_SYSENTER_CS, true, true);
1454 1.6.2.2 pgoyette svm_vcpu_msr_allow(cpudata->msrbm, MSR_SYSENTER_ESP, true, true);
1455 1.6.2.2 pgoyette svm_vcpu_msr_allow(cpudata->msrbm, MSR_SYSENTER_EIP, true, true);
1456 1.6.2.2 pgoyette svm_vcpu_msr_allow(cpudata->msrbm, MSR_FSBASE, true, true);
1457 1.6.2.2 pgoyette svm_vcpu_msr_allow(cpudata->msrbm, MSR_GSBASE, true, true);
1458 1.6.2.2 pgoyette vmcb->ctrl.msrpm_base_pa = cpudata->msrbm_pa;
1459 1.6.2.2 pgoyette
1460 1.6.2.2 pgoyette /* Generate ASID. */
1461 1.6.2.2 pgoyette svm_asid_alloc(vcpu);
1462 1.6.2.2 pgoyette
1463 1.6.2.2 pgoyette /* Virtual TPR. */
1464 1.6.2.2 pgoyette vmcb->ctrl.v = VMCB_CTRL_V_INTR_MASKING;
1465 1.6.2.2 pgoyette
1466 1.6.2.2 pgoyette /* Enable Nested Paging. */
1467 1.6.2.2 pgoyette vmcb->ctrl.enable1 = VMCB_CTRL_ENABLE_NP;
1468 1.6.2.2 pgoyette vmcb->ctrl.n_cr3 = mach->vm->vm_map.pmap->pm_pdirpa[0];
1469 1.6.2.2 pgoyette
1470 1.6.2.2 pgoyette /* Must always be set. */
1471 1.6.2.2 pgoyette vmcb->state.efer = EFER_SVME;
1472 1.6.2.2 pgoyette
1473 1.6.2.2 pgoyette /* Init XSAVE header. */
1474 1.6.2.2 pgoyette cpudata->gfpu.xsh_xstate_bv = svm_xcr0_mask;
1475 1.6.2.2 pgoyette cpudata->gfpu.xsh_xcomp_bv = 0;
1476 1.6.2.2 pgoyette
1477 1.6.2.2 pgoyette /* Bluntly hide the host TSC. */
1478 1.6.2.2 pgoyette cpudata->tsc_offset = rdtsc();
1479 1.6.2.2 pgoyette }
1480 1.6.2.2 pgoyette
1481 1.6.2.2 pgoyette static int
1482 1.6.2.2 pgoyette svm_vcpu_create(struct nvmm_machine *mach, struct nvmm_cpu *vcpu)
1483 1.6.2.2 pgoyette {
1484 1.6.2.2 pgoyette struct svm_cpudata *cpudata;
1485 1.6.2.2 pgoyette int error;
1486 1.6.2.2 pgoyette
1487 1.6.2.2 pgoyette /* Allocate the SVM cpudata. */
1488 1.6.2.2 pgoyette cpudata = (struct svm_cpudata *)uvm_km_alloc(kernel_map,
1489 1.6.2.2 pgoyette roundup(sizeof(*cpudata), PAGE_SIZE), 0,
1490 1.6.2.2 pgoyette UVM_KMF_WIRED|UVM_KMF_ZERO);
1491 1.6.2.2 pgoyette vcpu->cpudata = cpudata;
1492 1.6.2.2 pgoyette
1493 1.6.2.2 pgoyette /* VMCB */
1494 1.6.2.2 pgoyette error = svm_memalloc(&cpudata->vmcb_pa, (vaddr_t *)&cpudata->vmcb,
1495 1.6.2.2 pgoyette VMCB_NPAGES);
1496 1.6.2.2 pgoyette if (error)
1497 1.6.2.2 pgoyette goto error;
1498 1.6.2.2 pgoyette
1499 1.6.2.2 pgoyette /* I/O Bitmap */
1500 1.6.2.2 pgoyette error = svm_memalloc(&cpudata->iobm_pa, (vaddr_t *)&cpudata->iobm,
1501 1.6.2.2 pgoyette IOBM_NPAGES);
1502 1.6.2.2 pgoyette if (error)
1503 1.6.2.2 pgoyette goto error;
1504 1.6.2.2 pgoyette
1505 1.6.2.2 pgoyette /* MSR Bitmap */
1506 1.6.2.2 pgoyette error = svm_memalloc(&cpudata->msrbm_pa, (vaddr_t *)&cpudata->msrbm,
1507 1.6.2.2 pgoyette MSRBM_NPAGES);
1508 1.6.2.2 pgoyette if (error)
1509 1.6.2.2 pgoyette goto error;
1510 1.6.2.2 pgoyette
1511 1.6.2.2 pgoyette /* Init the VCPU info. */
1512 1.6.2.2 pgoyette svm_vcpu_init(mach, vcpu);
1513 1.6.2.2 pgoyette
1514 1.6.2.2 pgoyette return 0;
1515 1.6.2.2 pgoyette
1516 1.6.2.2 pgoyette error:
1517 1.6.2.2 pgoyette if (cpudata->vmcb_pa) {
1518 1.6.2.2 pgoyette svm_memfree(cpudata->vmcb_pa, (vaddr_t)cpudata->vmcb,
1519 1.6.2.2 pgoyette VMCB_NPAGES);
1520 1.6.2.2 pgoyette }
1521 1.6.2.2 pgoyette if (cpudata->iobm_pa) {
1522 1.6.2.2 pgoyette svm_memfree(cpudata->iobm_pa, (vaddr_t)cpudata->iobm,
1523 1.6.2.2 pgoyette IOBM_NPAGES);
1524 1.6.2.2 pgoyette }
1525 1.6.2.2 pgoyette if (cpudata->msrbm_pa) {
1526 1.6.2.2 pgoyette svm_memfree(cpudata->msrbm_pa, (vaddr_t)cpudata->msrbm,
1527 1.6.2.2 pgoyette MSRBM_NPAGES);
1528 1.6.2.2 pgoyette }
1529 1.6.2.2 pgoyette uvm_km_free(kernel_map, (vaddr_t)cpudata,
1530 1.6.2.2 pgoyette roundup(sizeof(*cpudata), PAGE_SIZE), UVM_KMF_WIRED);
1531 1.6.2.2 pgoyette return error;
1532 1.6.2.2 pgoyette }
1533 1.6.2.2 pgoyette
1534 1.6.2.2 pgoyette static void
1535 1.6.2.2 pgoyette svm_vcpu_destroy(struct nvmm_machine *mach, struct nvmm_cpu *vcpu)
1536 1.6.2.2 pgoyette {
1537 1.6.2.2 pgoyette struct svm_cpudata *cpudata = vcpu->cpudata;
1538 1.6.2.2 pgoyette
1539 1.6.2.2 pgoyette svm_asid_free(vcpu);
1540 1.6.2.2 pgoyette
1541 1.6.2.2 pgoyette svm_memfree(cpudata->vmcb_pa, (vaddr_t)cpudata->vmcb, VMCB_NPAGES);
1542 1.6.2.2 pgoyette svm_memfree(cpudata->iobm_pa, (vaddr_t)cpudata->iobm, IOBM_NPAGES);
1543 1.6.2.2 pgoyette svm_memfree(cpudata->msrbm_pa, (vaddr_t)cpudata->msrbm, MSRBM_NPAGES);
1544 1.6.2.2 pgoyette
1545 1.6.2.2 pgoyette uvm_km_free(kernel_map, (vaddr_t)cpudata,
1546 1.6.2.2 pgoyette roundup(sizeof(*cpudata), PAGE_SIZE), UVM_KMF_WIRED);
1547 1.6.2.2 pgoyette }
1548 1.6.2.2 pgoyette
1549 1.6.2.2 pgoyette #define SVM_SEG_ATTRIB_TYPE __BITS(4,0)
1550 1.6.2.2 pgoyette #define SVM_SEG_ATTRIB_DPL __BITS(6,5)
1551 1.6.2.2 pgoyette #define SVM_SEG_ATTRIB_P __BIT(7)
1552 1.6.2.2 pgoyette #define SVM_SEG_ATTRIB_AVL __BIT(8)
1553 1.6.2.2 pgoyette #define SVM_SEG_ATTRIB_LONG __BIT(9)
1554 1.6.2.2 pgoyette #define SVM_SEG_ATTRIB_DEF32 __BIT(10)
1555 1.6.2.2 pgoyette #define SVM_SEG_ATTRIB_GRAN __BIT(11)
1556 1.6.2.2 pgoyette
1557 1.6.2.2 pgoyette static void
1558 1.6.2.2 pgoyette svm_vcpu_setstate_seg(struct nvmm_x64_state_seg *seg, struct vmcb_segment *vseg)
1559 1.6.2.2 pgoyette {
1560 1.6.2.2 pgoyette vseg->selector = seg->selector;
1561 1.6.2.2 pgoyette vseg->attrib =
1562 1.6.2.2 pgoyette __SHIFTIN(seg->attrib.type, SVM_SEG_ATTRIB_TYPE) |
1563 1.6.2.2 pgoyette __SHIFTIN(seg->attrib.dpl, SVM_SEG_ATTRIB_DPL) |
1564 1.6.2.2 pgoyette __SHIFTIN(seg->attrib.p, SVM_SEG_ATTRIB_P) |
1565 1.6.2.2 pgoyette __SHIFTIN(seg->attrib.avl, SVM_SEG_ATTRIB_AVL) |
1566 1.6.2.2 pgoyette __SHIFTIN(seg->attrib.lng, SVM_SEG_ATTRIB_LONG) |
1567 1.6.2.2 pgoyette __SHIFTIN(seg->attrib.def32, SVM_SEG_ATTRIB_DEF32) |
1568 1.6.2.2 pgoyette __SHIFTIN(seg->attrib.gran, SVM_SEG_ATTRIB_GRAN);
1569 1.6.2.2 pgoyette vseg->limit = seg->limit;
1570 1.6.2.2 pgoyette vseg->base = seg->base;
1571 1.6.2.2 pgoyette }
1572 1.6.2.2 pgoyette
1573 1.6.2.2 pgoyette static void
1574 1.6.2.2 pgoyette svm_vcpu_getstate_seg(struct nvmm_x64_state_seg *seg, struct vmcb_segment *vseg)
1575 1.6.2.2 pgoyette {
1576 1.6.2.2 pgoyette seg->selector = vseg->selector;
1577 1.6.2.2 pgoyette seg->attrib.type = __SHIFTOUT(vseg->attrib, SVM_SEG_ATTRIB_TYPE);
1578 1.6.2.2 pgoyette seg->attrib.dpl = __SHIFTOUT(vseg->attrib, SVM_SEG_ATTRIB_DPL);
1579 1.6.2.2 pgoyette seg->attrib.p = __SHIFTOUT(vseg->attrib, SVM_SEG_ATTRIB_P);
1580 1.6.2.2 pgoyette seg->attrib.avl = __SHIFTOUT(vseg->attrib, SVM_SEG_ATTRIB_AVL);
1581 1.6.2.2 pgoyette seg->attrib.lng = __SHIFTOUT(vseg->attrib, SVM_SEG_ATTRIB_LONG);
1582 1.6.2.2 pgoyette seg->attrib.def32 = __SHIFTOUT(vseg->attrib, SVM_SEG_ATTRIB_DEF32);
1583 1.6.2.2 pgoyette seg->attrib.gran = __SHIFTOUT(vseg->attrib, SVM_SEG_ATTRIB_GRAN);
1584 1.6.2.2 pgoyette seg->limit = vseg->limit;
1585 1.6.2.2 pgoyette seg->base = vseg->base;
1586 1.6.2.2 pgoyette }
1587 1.6.2.2 pgoyette
1588 1.6.2.2 pgoyette static bool
1589 1.6.2.2 pgoyette svm_state_tlb_flush(struct nvmm_x64_state *cstate,
1590 1.6.2.2 pgoyette struct nvmm_x64_state *nstate, uint64_t flags)
1591 1.6.2.2 pgoyette {
1592 1.6.2.2 pgoyette if (flags & NVMM_X64_STATE_CRS) {
1593 1.6.2.2 pgoyette if ((cstate->crs[NVMM_X64_CR_CR0] ^
1594 1.6.2.2 pgoyette nstate->crs[NVMM_X64_CR_CR0]) & CR0_TLB_FLUSH) {
1595 1.6.2.2 pgoyette return true;
1596 1.6.2.2 pgoyette }
1597 1.6.2.2 pgoyette if (cstate->crs[NVMM_X64_CR_CR3] !=
1598 1.6.2.2 pgoyette nstate->crs[NVMM_X64_CR_CR3]) {
1599 1.6.2.2 pgoyette return true;
1600 1.6.2.2 pgoyette }
1601 1.6.2.2 pgoyette if ((cstate->crs[NVMM_X64_CR_CR4] ^
1602 1.6.2.2 pgoyette nstate->crs[NVMM_X64_CR_CR4]) & CR4_TLB_FLUSH) {
1603 1.6.2.2 pgoyette return true;
1604 1.6.2.2 pgoyette }
1605 1.6.2.2 pgoyette }
1606 1.6.2.2 pgoyette
1607 1.6.2.2 pgoyette if (flags & NVMM_X64_STATE_MSRS) {
1608 1.6.2.2 pgoyette if ((cstate->msrs[NVMM_X64_MSR_EFER] ^
1609 1.6.2.2 pgoyette nstate->msrs[NVMM_X64_MSR_EFER]) & EFER_TLB_FLUSH) {
1610 1.6.2.2 pgoyette return true;
1611 1.6.2.2 pgoyette }
1612 1.6.2.2 pgoyette }
1613 1.6.2.2 pgoyette
1614 1.6.2.2 pgoyette return false;
1615 1.6.2.2 pgoyette }
1616 1.6.2.2 pgoyette
1617 1.6.2.2 pgoyette static void
1618 1.6.2.2 pgoyette svm_vcpu_setstate(struct nvmm_cpu *vcpu, void *data, uint64_t flags)
1619 1.6.2.2 pgoyette {
1620 1.6.2.2 pgoyette struct svm_cpudata *cpudata = vcpu->cpudata;
1621 1.6.2.2 pgoyette struct nvmm_x64_state *cstate = &cpudata->state;
1622 1.6.2.2 pgoyette struct nvmm_x64_state *nstate = (struct nvmm_x64_state *)data;
1623 1.6.2.2 pgoyette struct vmcb *vmcb = cpudata->vmcb;
1624 1.6.2.2 pgoyette struct fxsave *fpustate;
1625 1.6.2.2 pgoyette
1626 1.6.2.2 pgoyette if (svm_state_tlb_flush(cstate, nstate, flags)) {
1627 1.6.2.2 pgoyette cpudata->tlb_want_flush = true;
1628 1.6.2.2 pgoyette }
1629 1.6.2.2 pgoyette
1630 1.6.2.2 pgoyette if (flags & NVMM_X64_STATE_SEGS) {
1631 1.6.2.2 pgoyette memcpy(cstate->segs, nstate->segs, sizeof(nstate->segs));
1632 1.6.2.2 pgoyette
1633 1.6.2.2 pgoyette svm_vcpu_setstate_seg(&cstate->segs[NVMM_X64_SEG_CS],
1634 1.6.2.2 pgoyette &vmcb->state.cs);
1635 1.6.2.2 pgoyette svm_vcpu_setstate_seg(&cstate->segs[NVMM_X64_SEG_DS],
1636 1.6.2.2 pgoyette &vmcb->state.ds);
1637 1.6.2.2 pgoyette svm_vcpu_setstate_seg(&cstate->segs[NVMM_X64_SEG_ES],
1638 1.6.2.2 pgoyette &vmcb->state.es);
1639 1.6.2.2 pgoyette svm_vcpu_setstate_seg(&cstate->segs[NVMM_X64_SEG_FS],
1640 1.6.2.2 pgoyette &vmcb->state.fs);
1641 1.6.2.2 pgoyette svm_vcpu_setstate_seg(&cstate->segs[NVMM_X64_SEG_GS],
1642 1.6.2.2 pgoyette &vmcb->state.gs);
1643 1.6.2.2 pgoyette svm_vcpu_setstate_seg(&cstate->segs[NVMM_X64_SEG_SS],
1644 1.6.2.2 pgoyette &vmcb->state.ss);
1645 1.6.2.2 pgoyette svm_vcpu_setstate_seg(&cstate->segs[NVMM_X64_SEG_GDT],
1646 1.6.2.2 pgoyette &vmcb->state.gdt);
1647 1.6.2.2 pgoyette svm_vcpu_setstate_seg(&cstate->segs[NVMM_X64_SEG_IDT],
1648 1.6.2.2 pgoyette &vmcb->state.idt);
1649 1.6.2.2 pgoyette svm_vcpu_setstate_seg(&cstate->segs[NVMM_X64_SEG_LDT],
1650 1.6.2.2 pgoyette &vmcb->state.ldt);
1651 1.6.2.2 pgoyette svm_vcpu_setstate_seg(&cstate->segs[NVMM_X64_SEG_TR],
1652 1.6.2.2 pgoyette &vmcb->state.tr);
1653 1.6.2.2 pgoyette }
1654 1.6.2.2 pgoyette
1655 1.6.2.2 pgoyette if (flags & NVMM_X64_STATE_GPRS) {
1656 1.6.2.2 pgoyette memcpy(cstate->gprs, nstate->gprs, sizeof(nstate->gprs));
1657 1.6.2.2 pgoyette
1658 1.6.2.2 pgoyette vmcb->state.rip = cstate->gprs[NVMM_X64_GPR_RIP];
1659 1.6.2.2 pgoyette vmcb->state.rsp = cstate->gprs[NVMM_X64_GPR_RSP];
1660 1.6.2.2 pgoyette vmcb->state.rax = cstate->gprs[NVMM_X64_GPR_RAX];
1661 1.6.2.2 pgoyette vmcb->state.rflags = cstate->gprs[NVMM_X64_GPR_RFLAGS];
1662 1.6.2.2 pgoyette }
1663 1.6.2.2 pgoyette
1664 1.6.2.2 pgoyette if (flags & NVMM_X64_STATE_CRS) {
1665 1.6.2.2 pgoyette memcpy(cstate->crs, nstate->crs, sizeof(nstate->crs));
1666 1.6.2.2 pgoyette
1667 1.6.2.2 pgoyette vmcb->state.cr0 = cstate->crs[NVMM_X64_CR_CR0];
1668 1.6.2.2 pgoyette vmcb->state.cr2 = cstate->crs[NVMM_X64_CR_CR2];
1669 1.6.2.2 pgoyette vmcb->state.cr3 = cstate->crs[NVMM_X64_CR_CR3];
1670 1.6.2.2 pgoyette vmcb->state.cr4 = cstate->crs[NVMM_X64_CR_CR4];
1671 1.6.2.2 pgoyette
1672 1.6.2.2 pgoyette vmcb->ctrl.v &= ~VMCB_CTRL_V_TPR;
1673 1.6.2.2 pgoyette vmcb->ctrl.v |= __SHIFTIN(cstate->crs[NVMM_X64_CR_CR8],
1674 1.6.2.2 pgoyette VMCB_CTRL_V_TPR);
1675 1.6.2.2 pgoyette
1676 1.6.2.2 pgoyette /* Clear unsupported XCR0 bits, set mandatory X87 bit. */
1677 1.6.2.2 pgoyette if (svm_xcr0_mask != 0) {
1678 1.6.2.2 pgoyette cstate->crs[NVMM_X64_CR_XCR0] &= svm_xcr0_mask;
1679 1.6.2.2 pgoyette cstate->crs[NVMM_X64_CR_XCR0] |= XCR0_X87;
1680 1.6.2.2 pgoyette } else {
1681 1.6.2.2 pgoyette cstate->crs[NVMM_X64_CR_XCR0] = 0;
1682 1.6.2.2 pgoyette }
1683 1.6.2.2 pgoyette }
1684 1.6.2.2 pgoyette
1685 1.6.2.2 pgoyette if (flags & NVMM_X64_STATE_DRS) {
1686 1.6.2.2 pgoyette memcpy(cstate->drs, nstate->drs, sizeof(nstate->drs));
1687 1.6.2.2 pgoyette
1688 1.6.2.2 pgoyette vmcb->state.dr6 = cstate->drs[NVMM_X64_DR_DR6];
1689 1.6.2.2 pgoyette vmcb->state.dr7 = cstate->drs[NVMM_X64_DR_DR7];
1690 1.6.2.2 pgoyette }
1691 1.6.2.2 pgoyette
1692 1.6.2.2 pgoyette if (flags & NVMM_X64_STATE_MSRS) {
1693 1.6.2.2 pgoyette memcpy(cstate->msrs, nstate->msrs, sizeof(nstate->msrs));
1694 1.6.2.2 pgoyette
1695 1.6.2.2 pgoyette /* Bit EFER_SVME is mandatory. */
1696 1.6.2.2 pgoyette cstate->msrs[NVMM_X64_MSR_EFER] |= EFER_SVME;
1697 1.6.2.2 pgoyette
1698 1.6.2.2 pgoyette vmcb->state.efer = cstate->msrs[NVMM_X64_MSR_EFER];
1699 1.6.2.2 pgoyette vmcb->state.star = cstate->msrs[NVMM_X64_MSR_STAR];
1700 1.6.2.2 pgoyette vmcb->state.lstar = cstate->msrs[NVMM_X64_MSR_LSTAR];
1701 1.6.2.2 pgoyette vmcb->state.cstar = cstate->msrs[NVMM_X64_MSR_CSTAR];
1702 1.6.2.2 pgoyette vmcb->state.sfmask = cstate->msrs[NVMM_X64_MSR_SFMASK];
1703 1.6.2.2 pgoyette vmcb->state.kernelgsbase =
1704 1.6.2.2 pgoyette cstate->msrs[NVMM_X64_MSR_KERNELGSBASE];
1705 1.6.2.2 pgoyette vmcb->state.sysenter_cs =
1706 1.6.2.2 pgoyette cstate->msrs[NVMM_X64_MSR_SYSENTER_CS];
1707 1.6.2.2 pgoyette vmcb->state.sysenter_esp =
1708 1.6.2.2 pgoyette cstate->msrs[NVMM_X64_MSR_SYSENTER_ESP];
1709 1.6.2.2 pgoyette vmcb->state.sysenter_eip =
1710 1.6.2.2 pgoyette cstate->msrs[NVMM_X64_MSR_SYSENTER_EIP];
1711 1.6.2.2 pgoyette vmcb->state.g_pat = cstate->msrs[NVMM_X64_MSR_PAT];
1712 1.6.2.2 pgoyette }
1713 1.6.2.2 pgoyette
1714 1.6.2.2 pgoyette if (flags & NVMM_X64_STATE_MISC) {
1715 1.6.2.2 pgoyette memcpy(cstate->misc, nstate->misc, sizeof(nstate->misc));
1716 1.6.2.2 pgoyette
1717 1.6.2.2 pgoyette vmcb->state.cpl = cstate->misc[NVMM_X64_MISC_CPL];
1718 1.6.2.2 pgoyette }
1719 1.6.2.2 pgoyette
1720 1.6.2.2 pgoyette CTASSERT(sizeof(cpudata->gfpu.xsh_fxsave) == sizeof(cstate->fpu));
1721 1.6.2.2 pgoyette if (flags & NVMM_X64_STATE_FPU) {
1722 1.6.2.2 pgoyette memcpy(&cstate->fpu, &nstate->fpu, sizeof(nstate->fpu));
1723 1.6.2.2 pgoyette
1724 1.6.2.2 pgoyette memcpy(cpudata->gfpu.xsh_fxsave, &cstate->fpu,
1725 1.6.2.2 pgoyette sizeof(cstate->fpu));
1726 1.6.2.2 pgoyette
1727 1.6.2.2 pgoyette fpustate = (struct fxsave *)cpudata->gfpu.xsh_fxsave;
1728 1.6.2.2 pgoyette fpustate->fx_mxcsr_mask &= x86_fpu_mxcsr_mask;
1729 1.6.2.2 pgoyette fpustate->fx_mxcsr &= fpustate->fx_mxcsr_mask;
1730 1.6.2.2 pgoyette }
1731 1.6.2.2 pgoyette }
1732 1.6.2.2 pgoyette
1733 1.6.2.2 pgoyette static void
1734 1.6.2.2 pgoyette svm_vcpu_getstate(struct nvmm_cpu *vcpu, void *data, uint64_t flags)
1735 1.6.2.2 pgoyette {
1736 1.6.2.2 pgoyette struct svm_cpudata *cpudata = vcpu->cpudata;
1737 1.6.2.2 pgoyette struct nvmm_x64_state *cstate = &cpudata->state;
1738 1.6.2.2 pgoyette struct nvmm_x64_state *nstate = (struct nvmm_x64_state *)data;
1739 1.6.2.2 pgoyette struct vmcb *vmcb = cpudata->vmcb;
1740 1.6.2.2 pgoyette
1741 1.6.2.2 pgoyette if (flags & NVMM_X64_STATE_SEGS) {
1742 1.6.2.2 pgoyette svm_vcpu_getstate_seg(&cstate->segs[NVMM_X64_SEG_CS],
1743 1.6.2.2 pgoyette &vmcb->state.cs);
1744 1.6.2.2 pgoyette svm_vcpu_getstate_seg(&cstate->segs[NVMM_X64_SEG_DS],
1745 1.6.2.2 pgoyette &vmcb->state.ds);
1746 1.6.2.2 pgoyette svm_vcpu_getstate_seg(&cstate->segs[NVMM_X64_SEG_ES],
1747 1.6.2.2 pgoyette &vmcb->state.es);
1748 1.6.2.2 pgoyette svm_vcpu_getstate_seg(&cstate->segs[NVMM_X64_SEG_FS],
1749 1.6.2.2 pgoyette &vmcb->state.fs);
1750 1.6.2.2 pgoyette svm_vcpu_getstate_seg(&cstate->segs[NVMM_X64_SEG_GS],
1751 1.6.2.2 pgoyette &vmcb->state.gs);
1752 1.6.2.2 pgoyette svm_vcpu_getstate_seg(&cstate->segs[NVMM_X64_SEG_SS],
1753 1.6.2.2 pgoyette &vmcb->state.ss);
1754 1.6.2.2 pgoyette svm_vcpu_getstate_seg(&cstate->segs[NVMM_X64_SEG_GDT],
1755 1.6.2.2 pgoyette &vmcb->state.gdt);
1756 1.6.2.2 pgoyette svm_vcpu_getstate_seg(&cstate->segs[NVMM_X64_SEG_IDT],
1757 1.6.2.2 pgoyette &vmcb->state.idt);
1758 1.6.2.2 pgoyette svm_vcpu_getstate_seg(&cstate->segs[NVMM_X64_SEG_LDT],
1759 1.6.2.2 pgoyette &vmcb->state.ldt);
1760 1.6.2.2 pgoyette svm_vcpu_getstate_seg(&cstate->segs[NVMM_X64_SEG_TR],
1761 1.6.2.2 pgoyette &vmcb->state.tr);
1762 1.6.2.2 pgoyette
1763 1.6.2.2 pgoyette memcpy(nstate->segs, cstate->segs, sizeof(cstate->segs));
1764 1.6.2.2 pgoyette }
1765 1.6.2.2 pgoyette
1766 1.6.2.2 pgoyette if (flags & NVMM_X64_STATE_GPRS) {
1767 1.6.2.2 pgoyette cstate->gprs[NVMM_X64_GPR_RIP] = vmcb->state.rip;
1768 1.6.2.2 pgoyette cstate->gprs[NVMM_X64_GPR_RSP] = vmcb->state.rsp;
1769 1.6.2.2 pgoyette cstate->gprs[NVMM_X64_GPR_RAX] = vmcb->state.rax;
1770 1.6.2.2 pgoyette cstate->gprs[NVMM_X64_GPR_RFLAGS] = vmcb->state.rflags;
1771 1.6.2.2 pgoyette
1772 1.6.2.2 pgoyette memcpy(nstate->gprs, cstate->gprs, sizeof(cstate->gprs));
1773 1.6.2.2 pgoyette }
1774 1.6.2.2 pgoyette
1775 1.6.2.2 pgoyette if (flags & NVMM_X64_STATE_CRS) {
1776 1.6.2.2 pgoyette cstate->crs[NVMM_X64_CR_CR0] = vmcb->state.cr0;
1777 1.6.2.2 pgoyette cstate->crs[NVMM_X64_CR_CR2] = vmcb->state.cr2;
1778 1.6.2.2 pgoyette cstate->crs[NVMM_X64_CR_CR3] = vmcb->state.cr3;
1779 1.6.2.2 pgoyette cstate->crs[NVMM_X64_CR_CR4] = vmcb->state.cr4;
1780 1.6.2.2 pgoyette cstate->crs[NVMM_X64_CR_CR8] = __SHIFTOUT(vmcb->ctrl.v,
1781 1.6.2.2 pgoyette VMCB_CTRL_V_TPR);
1782 1.6.2.2 pgoyette
1783 1.6.2.2 pgoyette memcpy(nstate->crs, cstate->crs, sizeof(cstate->crs));
1784 1.6.2.2 pgoyette }
1785 1.6.2.2 pgoyette
1786 1.6.2.2 pgoyette if (flags & NVMM_X64_STATE_DRS) {
1787 1.6.2.2 pgoyette cstate->drs[NVMM_X64_DR_DR6] = vmcb->state.dr6;
1788 1.6.2.2 pgoyette cstate->drs[NVMM_X64_DR_DR7] = vmcb->state.dr7;
1789 1.6.2.2 pgoyette
1790 1.6.2.2 pgoyette memcpy(nstate->drs, cstate->drs, sizeof(cstate->drs));
1791 1.6.2.2 pgoyette }
1792 1.6.2.2 pgoyette
1793 1.6.2.2 pgoyette if (flags & NVMM_X64_STATE_MSRS) {
1794 1.6.2.2 pgoyette cstate->msrs[NVMM_X64_MSR_EFER] = vmcb->state.efer;
1795 1.6.2.2 pgoyette cstate->msrs[NVMM_X64_MSR_STAR] = vmcb->state.star;
1796 1.6.2.2 pgoyette cstate->msrs[NVMM_X64_MSR_LSTAR] = vmcb->state.lstar;
1797 1.6.2.2 pgoyette cstate->msrs[NVMM_X64_MSR_CSTAR] = vmcb->state.cstar;
1798 1.6.2.2 pgoyette cstate->msrs[NVMM_X64_MSR_SFMASK] = vmcb->state.sfmask;
1799 1.6.2.2 pgoyette cstate->msrs[NVMM_X64_MSR_KERNELGSBASE] =
1800 1.6.2.2 pgoyette vmcb->state.kernelgsbase;
1801 1.6.2.2 pgoyette cstate->msrs[NVMM_X64_MSR_SYSENTER_CS] =
1802 1.6.2.2 pgoyette vmcb->state.sysenter_cs;
1803 1.6.2.2 pgoyette cstate->msrs[NVMM_X64_MSR_SYSENTER_ESP] =
1804 1.6.2.2 pgoyette vmcb->state.sysenter_esp;
1805 1.6.2.2 pgoyette cstate->msrs[NVMM_X64_MSR_SYSENTER_EIP] =
1806 1.6.2.2 pgoyette vmcb->state.sysenter_eip;
1807 1.6.2.2 pgoyette cstate->msrs[NVMM_X64_MSR_PAT] = vmcb->state.g_pat;
1808 1.6.2.2 pgoyette
1809 1.6.2.2 pgoyette memcpy(nstate->msrs, cstate->msrs, sizeof(cstate->msrs));
1810 1.6.2.2 pgoyette
1811 1.6.2.2 pgoyette /* Hide SVME. */
1812 1.6.2.2 pgoyette nstate->msrs[NVMM_X64_MSR_EFER] &= ~EFER_SVME;
1813 1.6.2.2 pgoyette }
1814 1.6.2.2 pgoyette
1815 1.6.2.2 pgoyette if (flags & NVMM_X64_STATE_MISC) {
1816 1.6.2.2 pgoyette cstate->misc[NVMM_X64_MISC_CPL] = vmcb->state.cpl;
1817 1.6.2.2 pgoyette
1818 1.6.2.2 pgoyette memcpy(nstate->misc, cstate->misc, sizeof(cstate->misc));
1819 1.6.2.2 pgoyette }
1820 1.6.2.2 pgoyette
1821 1.6.2.2 pgoyette CTASSERT(sizeof(cpudata->gfpu.xsh_fxsave) == sizeof(cstate->fpu));
1822 1.6.2.2 pgoyette if (flags & NVMM_X64_STATE_FPU) {
1823 1.6.2.2 pgoyette memcpy(&cstate->fpu, cpudata->gfpu.xsh_fxsave,
1824 1.6.2.2 pgoyette sizeof(cstate->fpu));
1825 1.6.2.2 pgoyette
1826 1.6.2.2 pgoyette memcpy(&cstate->fpu, &nstate->fpu, sizeof(cstate->fpu));
1827 1.6.2.2 pgoyette }
1828 1.6.2.2 pgoyette }
1829 1.6.2.2 pgoyette
1830 1.6.2.2 pgoyette /* -------------------------------------------------------------------------- */
1831 1.6.2.2 pgoyette
1832 1.6.2.2 pgoyette static void
1833 1.6.2.2 pgoyette svm_tlb_flush(struct pmap *pm)
1834 1.6.2.2 pgoyette {
1835 1.6.2.2 pgoyette struct nvmm_machine *mach = pm->pm_data;
1836 1.6.2.2 pgoyette struct svm_cpudata *cpudata;
1837 1.6.2.2 pgoyette struct nvmm_cpu *vcpu;
1838 1.6.2.2 pgoyette int error;
1839 1.6.2.2 pgoyette size_t i;
1840 1.6.2.2 pgoyette
1841 1.6.2.2 pgoyette /* Request TLB flushes. */
1842 1.6.2.2 pgoyette for (i = 0; i < NVMM_MAX_VCPUS; i++) {
1843 1.6.2.2 pgoyette error = nvmm_vcpu_get(mach, i, &vcpu);
1844 1.6.2.2 pgoyette if (error)
1845 1.6.2.2 pgoyette continue;
1846 1.6.2.2 pgoyette cpudata = vcpu->cpudata;
1847 1.6.2.2 pgoyette cpudata->tlb_want_flush = true;
1848 1.6.2.2 pgoyette nvmm_vcpu_put(vcpu);
1849 1.6.2.2 pgoyette }
1850 1.6.2.2 pgoyette }
1851 1.6.2.2 pgoyette
1852 1.6.2.2 pgoyette static void
1853 1.6.2.2 pgoyette svm_machine_create(struct nvmm_machine *mach)
1854 1.6.2.2 pgoyette {
1855 1.6.2.2 pgoyette /* Fill in pmap info. */
1856 1.6.2.2 pgoyette mach->vm->vm_map.pmap->pm_data = (void *)mach;
1857 1.6.2.2 pgoyette mach->vm->vm_map.pmap->pm_tlb_flush = svm_tlb_flush;
1858 1.6.2.2 pgoyette
1859 1.6.2.2 pgoyette mach->machdata = kmem_zalloc(sizeof(struct svm_machdata), KM_SLEEP);
1860 1.6.2.2 pgoyette }
1861 1.6.2.2 pgoyette
1862 1.6.2.2 pgoyette static void
1863 1.6.2.2 pgoyette svm_machine_destroy(struct nvmm_machine *mach)
1864 1.6.2.2 pgoyette {
1865 1.6.2.2 pgoyette kmem_free(mach->machdata, sizeof(struct svm_machdata));
1866 1.6.2.2 pgoyette }
1867 1.6.2.2 pgoyette
1868 1.6.2.2 pgoyette static int
1869 1.6.2.2 pgoyette svm_machine_configure(struct nvmm_machine *mach, uint64_t op, void *data)
1870 1.6.2.2 pgoyette {
1871 1.6.2.2 pgoyette struct nvmm_x86_conf_cpuid *cpuid = data;
1872 1.6.2.2 pgoyette struct svm_machdata *machdata = (struct svm_machdata *)mach->machdata;
1873 1.6.2.2 pgoyette size_t i;
1874 1.6.2.2 pgoyette
1875 1.6.2.2 pgoyette if (__predict_false(op != NVMM_X86_CONF_CPUID)) {
1876 1.6.2.2 pgoyette return EINVAL;
1877 1.6.2.2 pgoyette }
1878 1.6.2.2 pgoyette
1879 1.6.2.2 pgoyette if (__predict_false((cpuid->set.eax & cpuid->del.eax) ||
1880 1.6.2.2 pgoyette (cpuid->set.ebx & cpuid->del.ebx) ||
1881 1.6.2.2 pgoyette (cpuid->set.ecx & cpuid->del.ecx) ||
1882 1.6.2.2 pgoyette (cpuid->set.edx & cpuid->del.edx))) {
1883 1.6.2.2 pgoyette return EINVAL;
1884 1.6.2.2 pgoyette }
1885 1.6.2.2 pgoyette
1886 1.6.2.2 pgoyette /* If already here, replace. */
1887 1.6.2.2 pgoyette for (i = 0; i < SVM_NCPUIDS; i++) {
1888 1.6.2.2 pgoyette if (!machdata->cpuidpresent[i]) {
1889 1.6.2.2 pgoyette continue;
1890 1.6.2.2 pgoyette }
1891 1.6.2.2 pgoyette if (machdata->cpuid[i].leaf == cpuid->leaf) {
1892 1.6.2.2 pgoyette memcpy(&machdata->cpuid[i], cpuid,
1893 1.6.2.2 pgoyette sizeof(struct nvmm_x86_conf_cpuid));
1894 1.6.2.2 pgoyette return 0;
1895 1.6.2.2 pgoyette }
1896 1.6.2.2 pgoyette }
1897 1.6.2.2 pgoyette
1898 1.6.2.2 pgoyette /* Not here, insert. */
1899 1.6.2.2 pgoyette for (i = 0; i < SVM_NCPUIDS; i++) {
1900 1.6.2.2 pgoyette if (!machdata->cpuidpresent[i]) {
1901 1.6.2.2 pgoyette machdata->cpuidpresent[i] = true;
1902 1.6.2.2 pgoyette memcpy(&machdata->cpuid[i], cpuid,
1903 1.6.2.2 pgoyette sizeof(struct nvmm_x86_conf_cpuid));
1904 1.6.2.2 pgoyette return 0;
1905 1.6.2.2 pgoyette }
1906 1.6.2.2 pgoyette }
1907 1.6.2.2 pgoyette
1908 1.6.2.2 pgoyette return ENOBUFS;
1909 1.6.2.2 pgoyette }
1910 1.6.2.2 pgoyette
1911 1.6.2.2 pgoyette /* -------------------------------------------------------------------------- */
1912 1.6.2.2 pgoyette
1913 1.6.2.2 pgoyette static bool
1914 1.6.2.2 pgoyette svm_ident(void)
1915 1.6.2.2 pgoyette {
1916 1.6.2.2 pgoyette u_int descs[4];
1917 1.6.2.2 pgoyette uint64_t msr;
1918 1.6.2.2 pgoyette
1919 1.6.2.2 pgoyette if (cpu_vendor != CPUVENDOR_AMD) {
1920 1.6.2.2 pgoyette return false;
1921 1.6.2.2 pgoyette }
1922 1.6.2.2 pgoyette if (!(cpu_feature[3] & CPUID_SVM)) {
1923 1.6.2.2 pgoyette return false;
1924 1.6.2.2 pgoyette }
1925 1.6.2.2 pgoyette
1926 1.6.2.2 pgoyette if (curcpu()->ci_max_ext_cpuid < 0x8000000a) {
1927 1.6.2.2 pgoyette return false;
1928 1.6.2.2 pgoyette }
1929 1.6.2.2 pgoyette x86_cpuid(0x8000000a, descs);
1930 1.6.2.2 pgoyette
1931 1.6.2.2 pgoyette /* Want Nested Paging. */
1932 1.6.2.2 pgoyette if (!(descs[3] & CPUID_AMD_SVM_NP)) {
1933 1.6.2.2 pgoyette return false;
1934 1.6.2.2 pgoyette }
1935 1.6.2.2 pgoyette
1936 1.6.2.2 pgoyette /* Want nRIP. */
1937 1.6.2.2 pgoyette if (!(descs[3] & CPUID_AMD_SVM_NRIPS)) {
1938 1.6.2.2 pgoyette return false;
1939 1.6.2.2 pgoyette }
1940 1.6.2.2 pgoyette
1941 1.6.2.2 pgoyette svm_decode_assist = (descs[3] & CPUID_AMD_SVM_DecodeAssist) != 0;
1942 1.6.2.2 pgoyette
1943 1.6.2.2 pgoyette msr = rdmsr(MSR_VMCR);
1944 1.6.2.2 pgoyette if ((msr & VMCR_SVMED) && (msr & VMCR_LOCK)) {
1945 1.6.2.2 pgoyette return false;
1946 1.6.2.2 pgoyette }
1947 1.6.2.2 pgoyette
1948 1.6.2.2 pgoyette return true;
1949 1.6.2.2 pgoyette }
1950 1.6.2.2 pgoyette
1951 1.6.2.2 pgoyette static void
1952 1.6.2.2 pgoyette svm_init_asid(uint32_t maxasid)
1953 1.6.2.2 pgoyette {
1954 1.6.2.2 pgoyette size_t i, j, allocsz;
1955 1.6.2.2 pgoyette
1956 1.6.2.2 pgoyette mutex_init(&svm_asidlock, MUTEX_DEFAULT, IPL_NONE);
1957 1.6.2.2 pgoyette
1958 1.6.2.2 pgoyette /* Arbitrarily limit. */
1959 1.6.2.2 pgoyette maxasid = uimin(maxasid, 8192);
1960 1.6.2.2 pgoyette
1961 1.6.2.2 pgoyette svm_maxasid = maxasid;
1962 1.6.2.2 pgoyette allocsz = roundup(maxasid, 8) / 8;
1963 1.6.2.2 pgoyette svm_asidmap = kmem_zalloc(allocsz, KM_SLEEP);
1964 1.6.2.2 pgoyette
1965 1.6.2.2 pgoyette /* ASID 0 is reserved for the host. */
1966 1.6.2.2 pgoyette svm_asidmap[0] |= __BIT(0);
1967 1.6.2.2 pgoyette
1968 1.6.2.2 pgoyette /* ASID n-1 is special, we share it. */
1969 1.6.2.2 pgoyette i = (maxasid - 1) / 8;
1970 1.6.2.2 pgoyette j = (maxasid - 1) % 8;
1971 1.6.2.2 pgoyette svm_asidmap[i] |= __BIT(j);
1972 1.6.2.2 pgoyette }
1973 1.6.2.2 pgoyette
1974 1.6.2.2 pgoyette static void
1975 1.6.2.2 pgoyette svm_change_cpu(void *arg1, void *arg2)
1976 1.6.2.2 pgoyette {
1977 1.6.2.2 pgoyette bool enable = (bool)arg1;
1978 1.6.2.2 pgoyette uint64_t msr;
1979 1.6.2.2 pgoyette
1980 1.6.2.2 pgoyette msr = rdmsr(MSR_VMCR);
1981 1.6.2.2 pgoyette if (msr & VMCR_SVMED) {
1982 1.6.2.2 pgoyette wrmsr(MSR_VMCR, msr & ~VMCR_SVMED);
1983 1.6.2.2 pgoyette }
1984 1.6.2.2 pgoyette
1985 1.6.2.2 pgoyette if (!enable) {
1986 1.6.2.2 pgoyette wrmsr(MSR_VM_HSAVE_PA, 0);
1987 1.6.2.2 pgoyette }
1988 1.6.2.2 pgoyette
1989 1.6.2.2 pgoyette msr = rdmsr(MSR_EFER);
1990 1.6.2.2 pgoyette if (enable) {
1991 1.6.2.2 pgoyette msr |= EFER_SVME;
1992 1.6.2.2 pgoyette } else {
1993 1.6.2.2 pgoyette msr &= ~EFER_SVME;
1994 1.6.2.2 pgoyette }
1995 1.6.2.2 pgoyette wrmsr(MSR_EFER, msr);
1996 1.6.2.2 pgoyette
1997 1.6.2.2 pgoyette if (enable) {
1998 1.6.2.2 pgoyette wrmsr(MSR_VM_HSAVE_PA, hsave[cpu_index(curcpu())].pa);
1999 1.6.2.2 pgoyette }
2000 1.6.2.2 pgoyette }
2001 1.6.2.2 pgoyette
2002 1.6.2.2 pgoyette static void
2003 1.6.2.2 pgoyette svm_init(void)
2004 1.6.2.2 pgoyette {
2005 1.6.2.2 pgoyette CPU_INFO_ITERATOR cii;
2006 1.6.2.2 pgoyette struct cpu_info *ci;
2007 1.6.2.2 pgoyette struct vm_page *pg;
2008 1.6.2.2 pgoyette u_int descs[4];
2009 1.6.2.2 pgoyette uint64_t xc;
2010 1.6.2.2 pgoyette
2011 1.6.2.2 pgoyette x86_cpuid(0x8000000a, descs);
2012 1.6.2.2 pgoyette
2013 1.6.2.2 pgoyette /* The guest TLB flush command. */
2014 1.6.2.2 pgoyette if (descs[3] & CPUID_AMD_SVM_FlushByASID) {
2015 1.6.2.2 pgoyette svm_ctrl_tlb_flush = VMCB_CTRL_TLB_CTRL_FLUSH_GUEST;
2016 1.6.2.2 pgoyette } else {
2017 1.6.2.2 pgoyette svm_ctrl_tlb_flush = VMCB_CTRL_TLB_CTRL_FLUSH_ALL;
2018 1.6.2.2 pgoyette }
2019 1.6.2.2 pgoyette
2020 1.6.2.2 pgoyette /* Init the ASID. */
2021 1.6.2.2 pgoyette svm_init_asid(descs[1]);
2022 1.6.2.2 pgoyette
2023 1.6.2.2 pgoyette /* Init the XCR0 mask. */
2024 1.6.2.2 pgoyette svm_xcr0_mask = SVM_XCR0_MASK_DEFAULT & x86_xsave_features;
2025 1.6.2.2 pgoyette
2026 1.6.2.2 pgoyette memset(hsave, 0, sizeof(hsave));
2027 1.6.2.2 pgoyette for (CPU_INFO_FOREACH(cii, ci)) {
2028 1.6.2.2 pgoyette pg = uvm_pagealloc(NULL, 0, NULL, UVM_PGA_ZERO);
2029 1.6.2.2 pgoyette hsave[cpu_index(ci)].pa = VM_PAGE_TO_PHYS(pg);
2030 1.6.2.2 pgoyette }
2031 1.6.2.2 pgoyette
2032 1.6.2.2 pgoyette xc = xc_broadcast(0, svm_change_cpu, (void *)true, NULL);
2033 1.6.2.2 pgoyette xc_wait(xc);
2034 1.6.2.2 pgoyette }
2035 1.6.2.2 pgoyette
2036 1.6.2.2 pgoyette static void
2037 1.6.2.2 pgoyette svm_fini_asid(void)
2038 1.6.2.2 pgoyette {
2039 1.6.2.2 pgoyette size_t allocsz;
2040 1.6.2.2 pgoyette
2041 1.6.2.2 pgoyette allocsz = roundup(svm_maxasid, 8) / 8;
2042 1.6.2.2 pgoyette kmem_free(svm_asidmap, allocsz);
2043 1.6.2.2 pgoyette
2044 1.6.2.2 pgoyette mutex_destroy(&svm_asidlock);
2045 1.6.2.2 pgoyette }
2046 1.6.2.2 pgoyette
2047 1.6.2.2 pgoyette static void
2048 1.6.2.2 pgoyette svm_fini(void)
2049 1.6.2.2 pgoyette {
2050 1.6.2.2 pgoyette uint64_t xc;
2051 1.6.2.2 pgoyette size_t i;
2052 1.6.2.2 pgoyette
2053 1.6.2.2 pgoyette xc = xc_broadcast(0, svm_change_cpu, (void *)false, NULL);
2054 1.6.2.2 pgoyette xc_wait(xc);
2055 1.6.2.2 pgoyette
2056 1.6.2.2 pgoyette for (i = 0; i < MAXCPUS; i++) {
2057 1.6.2.2 pgoyette if (hsave[i].pa != 0)
2058 1.6.2.2 pgoyette uvm_pagefree(PHYS_TO_VM_PAGE(hsave[i].pa));
2059 1.6.2.2 pgoyette }
2060 1.6.2.2 pgoyette
2061 1.6.2.2 pgoyette svm_fini_asid();
2062 1.6.2.2 pgoyette }
2063 1.6.2.2 pgoyette
2064 1.6.2.2 pgoyette static void
2065 1.6.2.2 pgoyette svm_capability(struct nvmm_capability *cap)
2066 1.6.2.2 pgoyette {
2067 1.6.2.2 pgoyette cap->u.x86.xcr0_mask = svm_xcr0_mask;
2068 1.6.2.2 pgoyette cap->u.x86.mxcsr_mask = x86_fpu_mxcsr_mask;
2069 1.6.2.2 pgoyette cap->u.x86.conf_cpuid_maxops = SVM_NCPUIDS;
2070 1.6.2.2 pgoyette }
2071 1.6.2.2 pgoyette
2072 1.6.2.2 pgoyette const struct nvmm_impl nvmm_x86_svm = {
2073 1.6.2.2 pgoyette .ident = svm_ident,
2074 1.6.2.2 pgoyette .init = svm_init,
2075 1.6.2.2 pgoyette .fini = svm_fini,
2076 1.6.2.2 pgoyette .capability = svm_capability,
2077 1.6.2.2 pgoyette .conf_max = NVMM_X86_NCONF,
2078 1.6.2.2 pgoyette .conf_sizes = svm_conf_sizes,
2079 1.6.2.2 pgoyette .state_size = sizeof(struct nvmm_x64_state),
2080 1.6.2.2 pgoyette .machine_create = svm_machine_create,
2081 1.6.2.2 pgoyette .machine_destroy = svm_machine_destroy,
2082 1.6.2.2 pgoyette .machine_configure = svm_machine_configure,
2083 1.6.2.2 pgoyette .vcpu_create = svm_vcpu_create,
2084 1.6.2.2 pgoyette .vcpu_destroy = svm_vcpu_destroy,
2085 1.6.2.2 pgoyette .vcpu_setstate = svm_vcpu_setstate,
2086 1.6.2.2 pgoyette .vcpu_getstate = svm_vcpu_getstate,
2087 1.6.2.2 pgoyette .vcpu_inject = svm_vcpu_inject,
2088 1.6.2.2 pgoyette .vcpu_run = svm_vcpu_run
2089 1.6.2.2 pgoyette };
2090