]>
Commit | Line | Data |
---|---|---|
6aa8b732 AK |
1 | /* |
2 | * Kernel-based Virtual Machine driver for Linux | |
3 | * | |
4 | * AMD SVM support | |
5 | * | |
6 | * Copyright (C) 2006 Qumranet, Inc. | |
9611c187 | 7 | * Copyright 2010 Red Hat, Inc. and/or its affiliates. |
6aa8b732 AK |
8 | * |
9 | * Authors: | |
10 | * Yaniv Kamay <yaniv@qumranet.com> | |
11 | * Avi Kivity <avi@qumranet.com> | |
12 | * | |
13 | * This work is licensed under the terms of the GNU GPL, version 2. See | |
14 | * the COPYING file in the top-level directory. | |
15 | * | |
16 | */ | |
edf88417 AK |
17 | #include <linux/kvm_host.h> |
18 | ||
85f455f7 | 19 | #include "irq.h" |
1d737c8a | 20 | #include "mmu.h" |
5fdbf976 | 21 | #include "kvm_cache_regs.h" |
fe4c7b19 | 22 | #include "x86.h" |
66f7b72e | 23 | #include "cpuid.h" |
25462f7f | 24 | #include "pmu.h" |
e495606d | 25 | |
6aa8b732 | 26 | #include <linux/module.h> |
ae759544 | 27 | #include <linux/mod_devicetable.h> |
9d8f549d | 28 | #include <linux/kernel.h> |
6aa8b732 AK |
29 | #include <linux/vmalloc.h> |
30 | #include <linux/highmem.h> | |
e8edc6e0 | 31 | #include <linux/sched.h> |
af658dca | 32 | #include <linux/trace_events.h> |
5a0e3ad6 | 33 | #include <linux/slab.h> |
6aa8b732 | 34 | |
1018faa6 | 35 | #include <asm/perf_event.h> |
67ec6607 | 36 | #include <asm/tlbflush.h> |
e495606d | 37 | #include <asm/desc.h> |
facb0139 | 38 | #include <asm/debugreg.h> |
631bc487 | 39 | #include <asm/kvm_para.h> |
6aa8b732 | 40 | |
63d1142f | 41 | #include <asm/virtext.h> |
229456fc | 42 | #include "trace.h" |
63d1142f | 43 | |
4ecac3fd AK |
44 | #define __ex(x) __kvm_handle_fault_on_reboot(x) |
45 | ||
6aa8b732 AK |
46 | MODULE_AUTHOR("Qumranet"); |
47 | MODULE_LICENSE("GPL"); | |
48 | ||
ae759544 JT |
49 | static const struct x86_cpu_id svm_cpu_id[] = { |
50 | X86_FEATURE_MATCH(X86_FEATURE_SVM), | |
51 | {} | |
52 | }; | |
53 | MODULE_DEVICE_TABLE(x86cpu, svm_cpu_id); | |
54 | ||
6aa8b732 AK |
55 | #define IOPM_ALLOC_ORDER 2 |
56 | #define MSRPM_ALLOC_ORDER 1 | |
57 | ||
6aa8b732 AK |
58 | #define SEG_TYPE_LDT 2 |
59 | #define SEG_TYPE_BUSY_TSS16 3 | |
60 | ||
6bc31bdc AP |
61 | #define SVM_FEATURE_NPT (1 << 0) |
62 | #define SVM_FEATURE_LBRV (1 << 1) | |
63 | #define SVM_FEATURE_SVML (1 << 2) | |
64 | #define SVM_FEATURE_NRIP (1 << 3) | |
ddce97aa AP |
65 | #define SVM_FEATURE_TSC_RATE (1 << 4) |
66 | #define SVM_FEATURE_VMCB_CLEAN (1 << 5) | |
67 | #define SVM_FEATURE_FLUSH_ASID (1 << 6) | |
68 | #define SVM_FEATURE_DECODE_ASSIST (1 << 7) | |
6bc31bdc | 69 | #define SVM_FEATURE_PAUSE_FILTER (1 << 10) |
80b7706e | 70 | |
410e4d57 JR |
71 | #define NESTED_EXIT_HOST 0 /* Exit handled on host level */ |
72 | #define NESTED_EXIT_DONE 1 /* Exit caused nested vmexit */ | |
73 | #define NESTED_EXIT_CONTINUE 2 /* Further checks needed */ | |
74 | ||
24e09cbf JR |
75 | #define DEBUGCTL_RESERVED_BITS (~(0x3fULL)) |
76 | ||
fbc0db76 | 77 | #define TSC_RATIO_RSVD 0xffffff0000000000ULL |
92a1f12d JR |
78 | #define TSC_RATIO_MIN 0x0000000000000001ULL |
79 | #define TSC_RATIO_MAX 0x000000ffffffffffULL | |
fbc0db76 | 80 | |
67ec6607 JR |
81 | static bool erratum_383_found __read_mostly; |
82 | ||
6c8166a7 AK |
83 | static const u32 host_save_user_msrs[] = { |
84 | #ifdef CONFIG_X86_64 | |
85 | MSR_STAR, MSR_LSTAR, MSR_CSTAR, MSR_SYSCALL_MASK, MSR_KERNEL_GS_BASE, | |
86 | MSR_FS_BASE, | |
87 | #endif | |
88 | MSR_IA32_SYSENTER_CS, MSR_IA32_SYSENTER_ESP, MSR_IA32_SYSENTER_EIP, | |
89 | }; | |
90 | ||
91 | #define NR_HOST_SAVE_USER_MSRS ARRAY_SIZE(host_save_user_msrs) | |
92 | ||
93 | struct kvm_vcpu; | |
94 | ||
e6aa9abd JR |
95 | struct nested_state { |
96 | struct vmcb *hsave; | |
97 | u64 hsave_msr; | |
4a810181 | 98 | u64 vm_cr_msr; |
e6aa9abd JR |
99 | u64 vmcb; |
100 | ||
101 | /* These are the merged vectors */ | |
102 | u32 *msrpm; | |
103 | ||
104 | /* gpa pointers to the real vectors */ | |
105 | u64 vmcb_msrpm; | |
ce2ac085 | 106 | u64 vmcb_iopm; |
aad42c64 | 107 | |
cd3ff653 JR |
108 | /* A VMEXIT is required but not yet emulated */ |
109 | bool exit_required; | |
110 | ||
aad42c64 | 111 | /* cache for intercepts of the guest */ |
4ee546b4 | 112 | u32 intercept_cr; |
3aed041a | 113 | u32 intercept_dr; |
aad42c64 JR |
114 | u32 intercept_exceptions; |
115 | u64 intercept; | |
116 | ||
5bd2edc3 JR |
117 | /* Nested Paging related state */ |
118 | u64 nested_cr3; | |
e6aa9abd JR |
119 | }; |
120 | ||
323c3d80 JR |
121 | #define MSRPM_OFFSETS 16 |
122 | static u32 msrpm_offsets[MSRPM_OFFSETS] __read_mostly; | |
123 | ||
2b036c6b BO |
124 | /* |
125 | * Set osvw_len to higher value when updated Revision Guides | |
126 | * are published and we know what the new status bits are | |
127 | */ | |
128 | static uint64_t osvw_len = 4, osvw_status; | |
129 | ||
6c8166a7 AK |
130 | struct vcpu_svm { |
131 | struct kvm_vcpu vcpu; | |
132 | struct vmcb *vmcb; | |
133 | unsigned long vmcb_pa; | |
134 | struct svm_cpu_data *svm_data; | |
135 | uint64_t asid_generation; | |
136 | uint64_t sysenter_esp; | |
137 | uint64_t sysenter_eip; | |
138 | ||
139 | u64 next_rip; | |
140 | ||
141 | u64 host_user_msrs[NR_HOST_SAVE_USER_MSRS]; | |
afe9e66f | 142 | struct { |
dacccfdd AK |
143 | u16 fs; |
144 | u16 gs; | |
145 | u16 ldt; | |
afe9e66f AK |
146 | u64 gs_base; |
147 | } host; | |
6c8166a7 AK |
148 | |
149 | u32 *msrpm; | |
6c8166a7 | 150 | |
bd3d1ec3 AK |
151 | ulong nmi_iret_rip; |
152 | ||
e6aa9abd | 153 | struct nested_state nested; |
6be7d306 JK |
154 | |
155 | bool nmi_singlestep; | |
66b7138f JK |
156 | |
157 | unsigned int3_injected; | |
158 | unsigned long int3_rip; | |
631bc487 | 159 | u32 apf_reason; |
fbc0db76 JR |
160 | |
161 | u64 tsc_ratio; | |
6c8166a7 AK |
162 | }; |
163 | ||
fbc0db76 JR |
164 | static DEFINE_PER_CPU(u64, current_tsc_ratio); |
165 | #define TSC_RATIO_DEFAULT 0x0100000000ULL | |
166 | ||
455716fa JR |
167 | #define MSR_INVALID 0xffffffffU |
168 | ||
09941fbb | 169 | static const struct svm_direct_access_msrs { |
ac72a9b7 JR |
170 | u32 index; /* Index of the MSR */ |
171 | bool always; /* True if intercept is always on */ | |
172 | } direct_access_msrs[] = { | |
8c06585d | 173 | { .index = MSR_STAR, .always = true }, |
ac72a9b7 JR |
174 | { .index = MSR_IA32_SYSENTER_CS, .always = true }, |
175 | #ifdef CONFIG_X86_64 | |
176 | { .index = MSR_GS_BASE, .always = true }, | |
177 | { .index = MSR_FS_BASE, .always = true }, | |
178 | { .index = MSR_KERNEL_GS_BASE, .always = true }, | |
179 | { .index = MSR_LSTAR, .always = true }, | |
180 | { .index = MSR_CSTAR, .always = true }, | |
181 | { .index = MSR_SYSCALL_MASK, .always = true }, | |
182 | #endif | |
183 | { .index = MSR_IA32_LASTBRANCHFROMIP, .always = false }, | |
184 | { .index = MSR_IA32_LASTBRANCHTOIP, .always = false }, | |
185 | { .index = MSR_IA32_LASTINTFROMIP, .always = false }, | |
186 | { .index = MSR_IA32_LASTINTTOIP, .always = false }, | |
187 | { .index = MSR_INVALID, .always = false }, | |
6c8166a7 AK |
188 | }; |
189 | ||
709ddebf JR |
190 | /* enable NPT for AMD64 and X86 with PAE */ |
191 | #if defined(CONFIG_X86_64) || defined(CONFIG_X86_PAE) | |
192 | static bool npt_enabled = true; | |
193 | #else | |
e0231715 | 194 | static bool npt_enabled; |
709ddebf | 195 | #endif |
6c7dac72 | 196 | |
e2358851 DB |
197 | /* allow nested paging (virtualized MMU) for all guests */ |
198 | static int npt = true; | |
6c7dac72 | 199 | module_param(npt, int, S_IRUGO); |
e3da3acd | 200 | |
e2358851 DB |
201 | /* allow nested virtualization in KVM/SVM */ |
202 | static int nested = true; | |
236de055 AG |
203 | module_param(nested, int, S_IRUGO); |
204 | ||
79a8059d | 205 | static void svm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0); |
44874f84 | 206 | static void svm_flush_tlb(struct kvm_vcpu *vcpu); |
a5c3832d | 207 | static void svm_complete_interrupts(struct vcpu_svm *svm); |
04d2cc77 | 208 | |
410e4d57 | 209 | static int nested_svm_exit_handled(struct vcpu_svm *svm); |
b8e88bc8 | 210 | static int nested_svm_intercept(struct vcpu_svm *svm); |
cf74a78b | 211 | static int nested_svm_vmexit(struct vcpu_svm *svm); |
cf74a78b AG |
212 | static int nested_svm_check_exception(struct vcpu_svm *svm, unsigned nr, |
213 | bool has_error_code, u32 error_code); | |
92a1f12d | 214 | static u64 __scale_tsc(u64 ratio, u64 tsc); |
cf74a78b | 215 | |
8d28fec4 | 216 | enum { |
116a0a23 JR |
217 | VMCB_INTERCEPTS, /* Intercept vectors, TSC offset, |
218 | pause filter count */ | |
f56838e4 | 219 | VMCB_PERM_MAP, /* IOPM Base and MSRPM Base */ |
d48086d1 | 220 | VMCB_ASID, /* ASID */ |
decdbf6a | 221 | VMCB_INTR, /* int_ctl, int_vector */ |
b2747166 | 222 | VMCB_NPT, /* npt_en, nCR3, gPAT */ |
dcca1a65 | 223 | VMCB_CR, /* CR0, CR3, CR4, EFER */ |
72214b96 | 224 | VMCB_DR, /* DR6, DR7 */ |
17a703cb | 225 | VMCB_DT, /* GDT, IDT */ |
060d0c9a | 226 | VMCB_SEG, /* CS, DS, SS, ES, CPL */ |
0574dec0 | 227 | VMCB_CR2, /* CR2 only */ |
b53ba3f9 | 228 | VMCB_LBR, /* DBGCTL, BR_FROM, BR_TO, LAST_EX_FROM, LAST_EX_TO */ |
8d28fec4 RJ |
229 | VMCB_DIRTY_MAX, |
230 | }; | |
231 | ||
0574dec0 JR |
232 | /* TPR and CR2 are always written before VMRUN */ |
233 | #define VMCB_ALWAYS_DIRTY_MASK ((1U << VMCB_INTR) | (1U << VMCB_CR2)) | |
8d28fec4 RJ |
234 | |
235 | static inline void mark_all_dirty(struct vmcb *vmcb) | |
236 | { | |
237 | vmcb->control.clean = 0; | |
238 | } | |
239 | ||
240 | static inline void mark_all_clean(struct vmcb *vmcb) | |
241 | { | |
242 | vmcb->control.clean = ((1 << VMCB_DIRTY_MAX) - 1) | |
243 | & ~VMCB_ALWAYS_DIRTY_MASK; | |
244 | } | |
245 | ||
246 | static inline void mark_dirty(struct vmcb *vmcb, int bit) | |
247 | { | |
248 | vmcb->control.clean &= ~(1 << bit); | |
249 | } | |
250 | ||
a2fa3e9f GH |
251 | static inline struct vcpu_svm *to_svm(struct kvm_vcpu *vcpu) |
252 | { | |
fb3f0f51 | 253 | return container_of(vcpu, struct vcpu_svm, vcpu); |
a2fa3e9f GH |
254 | } |
255 | ||
384c6368 JR |
256 | static void recalc_intercepts(struct vcpu_svm *svm) |
257 | { | |
258 | struct vmcb_control_area *c, *h; | |
259 | struct nested_state *g; | |
260 | ||
116a0a23 JR |
261 | mark_dirty(svm->vmcb, VMCB_INTERCEPTS); |
262 | ||
384c6368 JR |
263 | if (!is_guest_mode(&svm->vcpu)) |
264 | return; | |
265 | ||
266 | c = &svm->vmcb->control; | |
267 | h = &svm->nested.hsave->control; | |
268 | g = &svm->nested; | |
269 | ||
4ee546b4 | 270 | c->intercept_cr = h->intercept_cr | g->intercept_cr; |
3aed041a | 271 | c->intercept_dr = h->intercept_dr | g->intercept_dr; |
384c6368 JR |
272 | c->intercept_exceptions = h->intercept_exceptions | g->intercept_exceptions; |
273 | c->intercept = h->intercept | g->intercept; | |
274 | } | |
275 | ||
4ee546b4 RJ |
276 | static inline struct vmcb *get_host_vmcb(struct vcpu_svm *svm) |
277 | { | |
278 | if (is_guest_mode(&svm->vcpu)) | |
279 | return svm->nested.hsave; | |
280 | else | |
281 | return svm->vmcb; | |
282 | } | |
283 | ||
284 | static inline void set_cr_intercept(struct vcpu_svm *svm, int bit) | |
285 | { | |
286 | struct vmcb *vmcb = get_host_vmcb(svm); | |
287 | ||
288 | vmcb->control.intercept_cr |= (1U << bit); | |
289 | ||
290 | recalc_intercepts(svm); | |
291 | } | |
292 | ||
293 | static inline void clr_cr_intercept(struct vcpu_svm *svm, int bit) | |
294 | { | |
295 | struct vmcb *vmcb = get_host_vmcb(svm); | |
296 | ||
297 | vmcb->control.intercept_cr &= ~(1U << bit); | |
298 | ||
299 | recalc_intercepts(svm); | |
300 | } | |
301 | ||
302 | static inline bool is_cr_intercept(struct vcpu_svm *svm, int bit) | |
303 | { | |
304 | struct vmcb *vmcb = get_host_vmcb(svm); | |
305 | ||
306 | return vmcb->control.intercept_cr & (1U << bit); | |
307 | } | |
308 | ||
5315c716 | 309 | static inline void set_dr_intercepts(struct vcpu_svm *svm) |
3aed041a JR |
310 | { |
311 | struct vmcb *vmcb = get_host_vmcb(svm); | |
312 | ||
5315c716 PB |
313 | vmcb->control.intercept_dr = (1 << INTERCEPT_DR0_READ) |
314 | | (1 << INTERCEPT_DR1_READ) | |
315 | | (1 << INTERCEPT_DR2_READ) | |
316 | | (1 << INTERCEPT_DR3_READ) | |
317 | | (1 << INTERCEPT_DR4_READ) | |
318 | | (1 << INTERCEPT_DR5_READ) | |
319 | | (1 << INTERCEPT_DR6_READ) | |
320 | | (1 << INTERCEPT_DR7_READ) | |
321 | | (1 << INTERCEPT_DR0_WRITE) | |
322 | | (1 << INTERCEPT_DR1_WRITE) | |
323 | | (1 << INTERCEPT_DR2_WRITE) | |
324 | | (1 << INTERCEPT_DR3_WRITE) | |
325 | | (1 << INTERCEPT_DR4_WRITE) | |
326 | | (1 << INTERCEPT_DR5_WRITE) | |
327 | | (1 << INTERCEPT_DR6_WRITE) | |
328 | | (1 << INTERCEPT_DR7_WRITE); | |
3aed041a JR |
329 | |
330 | recalc_intercepts(svm); | |
331 | } | |
332 | ||
5315c716 | 333 | static inline void clr_dr_intercepts(struct vcpu_svm *svm) |
3aed041a JR |
334 | { |
335 | struct vmcb *vmcb = get_host_vmcb(svm); | |
336 | ||
5315c716 | 337 | vmcb->control.intercept_dr = 0; |
3aed041a JR |
338 | |
339 | recalc_intercepts(svm); | |
340 | } | |
341 | ||
18c918c5 JR |
342 | static inline void set_exception_intercept(struct vcpu_svm *svm, int bit) |
343 | { | |
344 | struct vmcb *vmcb = get_host_vmcb(svm); | |
345 | ||
346 | vmcb->control.intercept_exceptions |= (1U << bit); | |
347 | ||
348 | recalc_intercepts(svm); | |
349 | } | |
350 | ||
351 | static inline void clr_exception_intercept(struct vcpu_svm *svm, int bit) | |
352 | { | |
353 | struct vmcb *vmcb = get_host_vmcb(svm); | |
354 | ||
355 | vmcb->control.intercept_exceptions &= ~(1U << bit); | |
356 | ||
357 | recalc_intercepts(svm); | |
358 | } | |
359 | ||
8a05a1b8 JR |
360 | static inline void set_intercept(struct vcpu_svm *svm, int bit) |
361 | { | |
362 | struct vmcb *vmcb = get_host_vmcb(svm); | |
363 | ||
364 | vmcb->control.intercept |= (1ULL << bit); | |
365 | ||
366 | recalc_intercepts(svm); | |
367 | } | |
368 | ||
369 | static inline void clr_intercept(struct vcpu_svm *svm, int bit) | |
370 | { | |
371 | struct vmcb *vmcb = get_host_vmcb(svm); | |
372 | ||
373 | vmcb->control.intercept &= ~(1ULL << bit); | |
374 | ||
375 | recalc_intercepts(svm); | |
376 | } | |
377 | ||
2af9194d JR |
378 | static inline void enable_gif(struct vcpu_svm *svm) |
379 | { | |
380 | svm->vcpu.arch.hflags |= HF_GIF_MASK; | |
381 | } | |
382 | ||
383 | static inline void disable_gif(struct vcpu_svm *svm) | |
384 | { | |
385 | svm->vcpu.arch.hflags &= ~HF_GIF_MASK; | |
386 | } | |
387 | ||
388 | static inline bool gif_set(struct vcpu_svm *svm) | |
389 | { | |
390 | return !!(svm->vcpu.arch.hflags & HF_GIF_MASK); | |
391 | } | |
392 | ||
4866d5e3 | 393 | static unsigned long iopm_base; |
6aa8b732 AK |
394 | |
395 | struct kvm_ldttss_desc { | |
396 | u16 limit0; | |
397 | u16 base0; | |
e0231715 JR |
398 | unsigned base1:8, type:5, dpl:2, p:1; |
399 | unsigned limit1:4, zero0:3, g:1, base2:8; | |
6aa8b732 AK |
400 | u32 base3; |
401 | u32 zero1; | |
402 | } __attribute__((packed)); | |
403 | ||
404 | struct svm_cpu_data { | |
405 | int cpu; | |
406 | ||
5008fdf5 AK |
407 | u64 asid_generation; |
408 | u32 max_asid; | |
409 | u32 next_asid; | |
6aa8b732 AK |
410 | struct kvm_ldttss_desc *tss_desc; |
411 | ||
412 | struct page *save_area; | |
413 | }; | |
414 | ||
415 | static DEFINE_PER_CPU(struct svm_cpu_data *, svm_data); | |
416 | ||
417 | struct svm_init_data { | |
418 | int cpu; | |
419 | int r; | |
420 | }; | |
421 | ||
09941fbb | 422 | static const u32 msrpm_ranges[] = {0, 0xc0000000, 0xc0010000}; |
6aa8b732 | 423 | |
9d8f549d | 424 | #define NUM_MSR_MAPS ARRAY_SIZE(msrpm_ranges) |
6aa8b732 AK |
425 | #define MSRS_RANGE_SIZE 2048 |
426 | #define MSRS_IN_RANGE (MSRS_RANGE_SIZE * 8 / 2) | |
427 | ||
455716fa JR |
428 | static u32 svm_msrpm_offset(u32 msr) |
429 | { | |
430 | u32 offset; | |
431 | int i; | |
432 | ||
433 | for (i = 0; i < NUM_MSR_MAPS; i++) { | |
434 | if (msr < msrpm_ranges[i] || | |
435 | msr >= msrpm_ranges[i] + MSRS_IN_RANGE) | |
436 | continue; | |
437 | ||
438 | offset = (msr - msrpm_ranges[i]) / 4; /* 4 msrs per u8 */ | |
439 | offset += (i * MSRS_RANGE_SIZE); /* add range offset */ | |
440 | ||
441 | /* Now we have the u8 offset - but need the u32 offset */ | |
442 | return offset / 4; | |
443 | } | |
444 | ||
445 | /* MSR not in any range */ | |
446 | return MSR_INVALID; | |
447 | } | |
448 | ||
6aa8b732 AK |
449 | #define MAX_INST_SIZE 15 |
450 | ||
6aa8b732 AK |
451 | static inline void clgi(void) |
452 | { | |
4ecac3fd | 453 | asm volatile (__ex(SVM_CLGI)); |
6aa8b732 AK |
454 | } |
455 | ||
456 | static inline void stgi(void) | |
457 | { | |
4ecac3fd | 458 | asm volatile (__ex(SVM_STGI)); |
6aa8b732 AK |
459 | } |
460 | ||
461 | static inline void invlpga(unsigned long addr, u32 asid) | |
462 | { | |
e0231715 | 463 | asm volatile (__ex(SVM_INVLPGA) : : "a"(addr), "c"(asid)); |
6aa8b732 AK |
464 | } |
465 | ||
4b16184c JR |
466 | static int get_npt_level(void) |
467 | { | |
468 | #ifdef CONFIG_X86_64 | |
469 | return PT64_ROOT_LEVEL; | |
470 | #else | |
471 | return PT32E_ROOT_LEVEL; | |
472 | #endif | |
473 | } | |
474 | ||
6aa8b732 AK |
475 | static void svm_set_efer(struct kvm_vcpu *vcpu, u64 efer) |
476 | { | |
6dc696d4 | 477 | vcpu->arch.efer = efer; |
709ddebf | 478 | if (!npt_enabled && !(efer & EFER_LMA)) |
2b5203ee | 479 | efer &= ~EFER_LME; |
6aa8b732 | 480 | |
9962d032 | 481 | to_svm(vcpu)->vmcb->save.efer = efer | EFER_SVME; |
dcca1a65 | 482 | mark_dirty(to_svm(vcpu)->vmcb, VMCB_CR); |
6aa8b732 AK |
483 | } |
484 | ||
6aa8b732 AK |
485 | static int is_external_interrupt(u32 info) |
486 | { | |
487 | info &= SVM_EVTINJ_TYPE_MASK | SVM_EVTINJ_VALID; | |
488 | return info == (SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_INTR); | |
489 | } | |
490 | ||
37ccdcbe | 491 | static u32 svm_get_interrupt_shadow(struct kvm_vcpu *vcpu) |
2809f5d2 GC |
492 | { |
493 | struct vcpu_svm *svm = to_svm(vcpu); | |
494 | u32 ret = 0; | |
495 | ||
496 | if (svm->vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK) | |
37ccdcbe PB |
497 | ret = KVM_X86_SHADOW_INT_STI | KVM_X86_SHADOW_INT_MOV_SS; |
498 | return ret; | |
2809f5d2 GC |
499 | } |
500 | ||
501 | static void svm_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask) | |
502 | { | |
503 | struct vcpu_svm *svm = to_svm(vcpu); | |
504 | ||
505 | if (mask == 0) | |
506 | svm->vmcb->control.int_state &= ~SVM_INTERRUPT_SHADOW_MASK; | |
507 | else | |
508 | svm->vmcb->control.int_state |= SVM_INTERRUPT_SHADOW_MASK; | |
509 | ||
510 | } | |
511 | ||
6aa8b732 AK |
512 | static void skip_emulated_instruction(struct kvm_vcpu *vcpu) |
513 | { | |
a2fa3e9f GH |
514 | struct vcpu_svm *svm = to_svm(vcpu); |
515 | ||
f104765b BD |
516 | if (svm->vmcb->control.next_rip != 0) { |
517 | WARN_ON(!static_cpu_has(X86_FEATURE_NRIPS)); | |
6bc31bdc | 518 | svm->next_rip = svm->vmcb->control.next_rip; |
f104765b | 519 | } |
6bc31bdc | 520 | |
a2fa3e9f | 521 | if (!svm->next_rip) { |
51d8b661 | 522 | if (emulate_instruction(vcpu, EMULTYPE_SKIP) != |
f629cf84 GN |
523 | EMULATE_DONE) |
524 | printk(KERN_DEBUG "%s: NOP\n", __func__); | |
6aa8b732 AK |
525 | return; |
526 | } | |
5fdbf976 MT |
527 | if (svm->next_rip - kvm_rip_read(vcpu) > MAX_INST_SIZE) |
528 | printk(KERN_ERR "%s: ip 0x%lx next 0x%llx\n", | |
529 | __func__, kvm_rip_read(vcpu), svm->next_rip); | |
6aa8b732 | 530 | |
5fdbf976 | 531 | kvm_rip_write(vcpu, svm->next_rip); |
2809f5d2 | 532 | svm_set_interrupt_shadow(vcpu, 0); |
6aa8b732 AK |
533 | } |
534 | ||
116a4752 | 535 | static void svm_queue_exception(struct kvm_vcpu *vcpu, unsigned nr, |
ce7ddec4 JR |
536 | bool has_error_code, u32 error_code, |
537 | bool reinject) | |
116a4752 JK |
538 | { |
539 | struct vcpu_svm *svm = to_svm(vcpu); | |
540 | ||
e0231715 JR |
541 | /* |
542 | * If we are within a nested VM we'd better #VMEXIT and let the guest | |
543 | * handle the exception | |
544 | */ | |
ce7ddec4 JR |
545 | if (!reinject && |
546 | nested_svm_check_exception(svm, nr, has_error_code, error_code)) | |
116a4752 JK |
547 | return; |
548 | ||
2a6b20b8 | 549 | if (nr == BP_VECTOR && !static_cpu_has(X86_FEATURE_NRIPS)) { |
66b7138f JK |
550 | unsigned long rip, old_rip = kvm_rip_read(&svm->vcpu); |
551 | ||
552 | /* | |
553 | * For guest debugging where we have to reinject #BP if some | |
554 | * INT3 is guest-owned: | |
555 | * Emulate nRIP by moving RIP forward. Will fail if injection | |
556 | * raises a fault that is not intercepted. Still better than | |
557 | * failing in all cases. | |
558 | */ | |
559 | skip_emulated_instruction(&svm->vcpu); | |
560 | rip = kvm_rip_read(&svm->vcpu); | |
561 | svm->int3_rip = rip + svm->vmcb->save.cs.base; | |
562 | svm->int3_injected = rip - old_rip; | |
563 | } | |
564 | ||
116a4752 JK |
565 | svm->vmcb->control.event_inj = nr |
566 | | SVM_EVTINJ_VALID | |
567 | | (has_error_code ? SVM_EVTINJ_VALID_ERR : 0) | |
568 | | SVM_EVTINJ_TYPE_EXEPT; | |
569 | svm->vmcb->control.event_inj_err = error_code; | |
570 | } | |
571 | ||
67ec6607 JR |
572 | static void svm_init_erratum_383(void) |
573 | { | |
574 | u32 low, high; | |
575 | int err; | |
576 | u64 val; | |
577 | ||
e6ee94d5 | 578 | if (!static_cpu_has_bug(X86_BUG_AMD_TLB_MMATCH)) |
67ec6607 JR |
579 | return; |
580 | ||
581 | /* Use _safe variants to not break nested virtualization */ | |
582 | val = native_read_msr_safe(MSR_AMD64_DC_CFG, &err); | |
583 | if (err) | |
584 | return; | |
585 | ||
586 | val |= (1ULL << 47); | |
587 | ||
588 | low = lower_32_bits(val); | |
589 | high = upper_32_bits(val); | |
590 | ||
591 | native_write_msr_safe(MSR_AMD64_DC_CFG, low, high); | |
592 | ||
593 | erratum_383_found = true; | |
594 | } | |
595 | ||
2b036c6b BO |
596 | static void svm_init_osvw(struct kvm_vcpu *vcpu) |
597 | { | |
598 | /* | |
599 | * Guests should see errata 400 and 415 as fixed (assuming that | |
600 | * HLT and IO instructions are intercepted). | |
601 | */ | |
602 | vcpu->arch.osvw.length = (osvw_len >= 3) ? (osvw_len) : 3; | |
603 | vcpu->arch.osvw.status = osvw_status & ~(6ULL); | |
604 | ||
605 | /* | |
606 | * By increasing VCPU's osvw.length to 3 we are telling the guest that | |
607 | * all osvw.status bits inside that length, including bit 0 (which is | |
608 | * reserved for erratum 298), are valid. However, if host processor's | |
609 | * osvw_len is 0 then osvw_status[0] carries no information. We need to | |
610 | * be conservative here and therefore we tell the guest that erratum 298 | |
611 | * is present (because we really don't know). | |
612 | */ | |
613 | if (osvw_len == 0 && boot_cpu_data.x86 == 0x10) | |
614 | vcpu->arch.osvw.status |= 1; | |
615 | } | |
616 | ||
6aa8b732 AK |
617 | static int has_svm(void) |
618 | { | |
63d1142f | 619 | const char *msg; |
6aa8b732 | 620 | |
63d1142f | 621 | if (!cpu_has_svm(&msg)) { |
ff81ff10 | 622 | printk(KERN_INFO "has_svm: %s\n", msg); |
6aa8b732 AK |
623 | return 0; |
624 | } | |
625 | ||
6aa8b732 AK |
626 | return 1; |
627 | } | |
628 | ||
13a34e06 | 629 | static void svm_hardware_disable(void) |
6aa8b732 | 630 | { |
fbc0db76 JR |
631 | /* Make sure we clean up behind us */ |
632 | if (static_cpu_has(X86_FEATURE_TSCRATEMSR)) | |
633 | wrmsrl(MSR_AMD64_TSC_RATIO, TSC_RATIO_DEFAULT); | |
634 | ||
2c8dceeb | 635 | cpu_svm_disable(); |
1018faa6 JR |
636 | |
637 | amd_pmu_disable_virt(); | |
6aa8b732 AK |
638 | } |
639 | ||
13a34e06 | 640 | static int svm_hardware_enable(void) |
6aa8b732 AK |
641 | { |
642 | ||
0fe1e009 | 643 | struct svm_cpu_data *sd; |
6aa8b732 | 644 | uint64_t efer; |
89a27f4d | 645 | struct desc_ptr gdt_descr; |
6aa8b732 AK |
646 | struct desc_struct *gdt; |
647 | int me = raw_smp_processor_id(); | |
648 | ||
10474ae8 AG |
649 | rdmsrl(MSR_EFER, efer); |
650 | if (efer & EFER_SVME) | |
651 | return -EBUSY; | |
652 | ||
6aa8b732 | 653 | if (!has_svm()) { |
1f5b77f5 | 654 | pr_err("%s: err EOPNOTSUPP on %d\n", __func__, me); |
10474ae8 | 655 | return -EINVAL; |
6aa8b732 | 656 | } |
0fe1e009 | 657 | sd = per_cpu(svm_data, me); |
0fe1e009 | 658 | if (!sd) { |
1f5b77f5 | 659 | pr_err("%s: svm_data is NULL on %d\n", __func__, me); |
10474ae8 | 660 | return -EINVAL; |
6aa8b732 AK |
661 | } |
662 | ||
0fe1e009 TH |
663 | sd->asid_generation = 1; |
664 | sd->max_asid = cpuid_ebx(SVM_CPUID_FUNC) - 1; | |
665 | sd->next_asid = sd->max_asid + 1; | |
6aa8b732 | 666 | |
d6ab1ed4 | 667 | native_store_gdt(&gdt_descr); |
89a27f4d | 668 | gdt = (struct desc_struct *)gdt_descr.address; |
0fe1e009 | 669 | sd->tss_desc = (struct kvm_ldttss_desc *)(gdt + GDT_ENTRY_TSS); |
6aa8b732 | 670 | |
9962d032 | 671 | wrmsrl(MSR_EFER, efer | EFER_SVME); |
6aa8b732 | 672 | |
d0316554 | 673 | wrmsrl(MSR_VM_HSAVE_PA, page_to_pfn(sd->save_area) << PAGE_SHIFT); |
10474ae8 | 674 | |
fbc0db76 JR |
675 | if (static_cpu_has(X86_FEATURE_TSCRATEMSR)) { |
676 | wrmsrl(MSR_AMD64_TSC_RATIO, TSC_RATIO_DEFAULT); | |
89cbc767 | 677 | __this_cpu_write(current_tsc_ratio, TSC_RATIO_DEFAULT); |
fbc0db76 JR |
678 | } |
679 | ||
2b036c6b BO |
680 | |
681 | /* | |
682 | * Get OSVW bits. | |
683 | * | |
684 | * Note that it is possible to have a system with mixed processor | |
685 | * revisions and therefore different OSVW bits. If bits are not the same | |
686 | * on different processors then choose the worst case (i.e. if erratum | |
687 | * is present on one processor and not on another then assume that the | |
688 | * erratum is present everywhere). | |
689 | */ | |
690 | if (cpu_has(&boot_cpu_data, X86_FEATURE_OSVW)) { | |
691 | uint64_t len, status = 0; | |
692 | int err; | |
693 | ||
694 | len = native_read_msr_safe(MSR_AMD64_OSVW_ID_LENGTH, &err); | |
695 | if (!err) | |
696 | status = native_read_msr_safe(MSR_AMD64_OSVW_STATUS, | |
697 | &err); | |
698 | ||
699 | if (err) | |
700 | osvw_status = osvw_len = 0; | |
701 | else { | |
702 | if (len < osvw_len) | |
703 | osvw_len = len; | |
704 | osvw_status |= status; | |
705 | osvw_status &= (1ULL << osvw_len) - 1; | |
706 | } | |
707 | } else | |
708 | osvw_status = osvw_len = 0; | |
709 | ||
67ec6607 JR |
710 | svm_init_erratum_383(); |
711 | ||
1018faa6 JR |
712 | amd_pmu_enable_virt(); |
713 | ||
10474ae8 | 714 | return 0; |
6aa8b732 AK |
715 | } |
716 | ||
0da1db75 JR |
717 | static void svm_cpu_uninit(int cpu) |
718 | { | |
0fe1e009 | 719 | struct svm_cpu_data *sd = per_cpu(svm_data, raw_smp_processor_id()); |
0da1db75 | 720 | |
0fe1e009 | 721 | if (!sd) |
0da1db75 JR |
722 | return; |
723 | ||
724 | per_cpu(svm_data, raw_smp_processor_id()) = NULL; | |
0fe1e009 TH |
725 | __free_page(sd->save_area); |
726 | kfree(sd); | |
0da1db75 JR |
727 | } |
728 | ||
6aa8b732 AK |
729 | static int svm_cpu_init(int cpu) |
730 | { | |
0fe1e009 | 731 | struct svm_cpu_data *sd; |
6aa8b732 AK |
732 | int r; |
733 | ||
0fe1e009 TH |
734 | sd = kzalloc(sizeof(struct svm_cpu_data), GFP_KERNEL); |
735 | if (!sd) | |
6aa8b732 | 736 | return -ENOMEM; |
0fe1e009 TH |
737 | sd->cpu = cpu; |
738 | sd->save_area = alloc_page(GFP_KERNEL); | |
6aa8b732 | 739 | r = -ENOMEM; |
0fe1e009 | 740 | if (!sd->save_area) |
6aa8b732 AK |
741 | goto err_1; |
742 | ||
0fe1e009 | 743 | per_cpu(svm_data, cpu) = sd; |
6aa8b732 AK |
744 | |
745 | return 0; | |
746 | ||
747 | err_1: | |
0fe1e009 | 748 | kfree(sd); |
6aa8b732 AK |
749 | return r; |
750 | ||
751 | } | |
752 | ||
ac72a9b7 JR |
753 | static bool valid_msr_intercept(u32 index) |
754 | { | |
755 | int i; | |
756 | ||
757 | for (i = 0; direct_access_msrs[i].index != MSR_INVALID; i++) | |
758 | if (direct_access_msrs[i].index == index) | |
759 | return true; | |
760 | ||
761 | return false; | |
762 | } | |
763 | ||
bfc733a7 RR |
764 | static void set_msr_interception(u32 *msrpm, unsigned msr, |
765 | int read, int write) | |
6aa8b732 | 766 | { |
455716fa JR |
767 | u8 bit_read, bit_write; |
768 | unsigned long tmp; | |
769 | u32 offset; | |
6aa8b732 | 770 | |
ac72a9b7 JR |
771 | /* |
772 | * If this warning triggers extend the direct_access_msrs list at the | |
773 | * beginning of the file | |
774 | */ | |
775 | WARN_ON(!valid_msr_intercept(msr)); | |
776 | ||
455716fa JR |
777 | offset = svm_msrpm_offset(msr); |
778 | bit_read = 2 * (msr & 0x0f); | |
779 | bit_write = 2 * (msr & 0x0f) + 1; | |
780 | tmp = msrpm[offset]; | |
781 | ||
782 | BUG_ON(offset == MSR_INVALID); | |
783 | ||
784 | read ? clear_bit(bit_read, &tmp) : set_bit(bit_read, &tmp); | |
785 | write ? clear_bit(bit_write, &tmp) : set_bit(bit_write, &tmp); | |
786 | ||
787 | msrpm[offset] = tmp; | |
6aa8b732 AK |
788 | } |
789 | ||
f65c229c | 790 | static void svm_vcpu_init_msrpm(u32 *msrpm) |
6aa8b732 AK |
791 | { |
792 | int i; | |
793 | ||
f65c229c JR |
794 | memset(msrpm, 0xff, PAGE_SIZE * (1 << MSRPM_ALLOC_ORDER)); |
795 | ||
ac72a9b7 JR |
796 | for (i = 0; direct_access_msrs[i].index != MSR_INVALID; i++) { |
797 | if (!direct_access_msrs[i].always) | |
798 | continue; | |
799 | ||
800 | set_msr_interception(msrpm, direct_access_msrs[i].index, 1, 1); | |
801 | } | |
f65c229c JR |
802 | } |
803 | ||
323c3d80 JR |
804 | static void add_msr_offset(u32 offset) |
805 | { | |
806 | int i; | |
807 | ||
808 | for (i = 0; i < MSRPM_OFFSETS; ++i) { | |
809 | ||
810 | /* Offset already in list? */ | |
811 | if (msrpm_offsets[i] == offset) | |
bfc733a7 | 812 | return; |
323c3d80 JR |
813 | |
814 | /* Slot used by another offset? */ | |
815 | if (msrpm_offsets[i] != MSR_INVALID) | |
816 | continue; | |
817 | ||
818 | /* Add offset to list */ | |
819 | msrpm_offsets[i] = offset; | |
820 | ||
821 | return; | |
6aa8b732 | 822 | } |
323c3d80 JR |
823 | |
824 | /* | |
825 | * If this BUG triggers the msrpm_offsets table has an overflow. Just | |
826 | * increase MSRPM_OFFSETS in this case. | |
827 | */ | |
bfc733a7 | 828 | BUG(); |
6aa8b732 AK |
829 | } |
830 | ||
323c3d80 | 831 | static void init_msrpm_offsets(void) |
f65c229c | 832 | { |
323c3d80 | 833 | int i; |
f65c229c | 834 | |
323c3d80 JR |
835 | memset(msrpm_offsets, 0xff, sizeof(msrpm_offsets)); |
836 | ||
837 | for (i = 0; direct_access_msrs[i].index != MSR_INVALID; i++) { | |
838 | u32 offset; | |
839 | ||
840 | offset = svm_msrpm_offset(direct_access_msrs[i].index); | |
841 | BUG_ON(offset == MSR_INVALID); | |
842 | ||
843 | add_msr_offset(offset); | |
844 | } | |
f65c229c JR |
845 | } |
846 | ||
24e09cbf JR |
847 | static void svm_enable_lbrv(struct vcpu_svm *svm) |
848 | { | |
849 | u32 *msrpm = svm->msrpm; | |
850 | ||
851 | svm->vmcb->control.lbr_ctl = 1; | |
852 | set_msr_interception(msrpm, MSR_IA32_LASTBRANCHFROMIP, 1, 1); | |
853 | set_msr_interception(msrpm, MSR_IA32_LASTBRANCHTOIP, 1, 1); | |
854 | set_msr_interception(msrpm, MSR_IA32_LASTINTFROMIP, 1, 1); | |
855 | set_msr_interception(msrpm, MSR_IA32_LASTINTTOIP, 1, 1); | |
856 | } | |
857 | ||
858 | static void svm_disable_lbrv(struct vcpu_svm *svm) | |
859 | { | |
860 | u32 *msrpm = svm->msrpm; | |
861 | ||
862 | svm->vmcb->control.lbr_ctl = 0; | |
863 | set_msr_interception(msrpm, MSR_IA32_LASTBRANCHFROMIP, 0, 0); | |
864 | set_msr_interception(msrpm, MSR_IA32_LASTBRANCHTOIP, 0, 0); | |
865 | set_msr_interception(msrpm, MSR_IA32_LASTINTFROMIP, 0, 0); | |
866 | set_msr_interception(msrpm, MSR_IA32_LASTINTTOIP, 0, 0); | |
867 | } | |
868 | ||
3c2e7f7d PB |
869 | #define MTRR_TYPE_UC_MINUS 7 |
870 | #define MTRR2PROTVAL_INVALID 0xff | |
871 | ||
872 | static u8 mtrr2protval[8]; | |
873 | ||
874 | static u8 fallback_mtrr_type(int mtrr) | |
875 | { | |
876 | /* | |
877 | * WT and WP aren't always available in the host PAT. Treat | |
878 | * them as UC and UC- respectively. Everything else should be | |
879 | * there. | |
880 | */ | |
881 | switch (mtrr) | |
882 | { | |
883 | case MTRR_TYPE_WRTHROUGH: | |
884 | return MTRR_TYPE_UNCACHABLE; | |
885 | case MTRR_TYPE_WRPROT: | |
886 | return MTRR_TYPE_UC_MINUS; | |
887 | default: | |
888 | BUG(); | |
889 | } | |
890 | } | |
891 | ||
892 | static void build_mtrr2protval(void) | |
893 | { | |
894 | int i; | |
895 | u64 pat; | |
896 | ||
897 | for (i = 0; i < 8; i++) | |
898 | mtrr2protval[i] = MTRR2PROTVAL_INVALID; | |
899 | ||
900 | /* Ignore the invalid MTRR types. */ | |
901 | mtrr2protval[2] = 0; | |
902 | mtrr2protval[3] = 0; | |
903 | ||
904 | /* | |
905 | * Use host PAT value to figure out the mapping from guest MTRR | |
906 | * values to nested page table PAT/PCD/PWT values. We do not | |
907 | * want to change the host PAT value every time we enter the | |
908 | * guest. | |
909 | */ | |
910 | rdmsrl(MSR_IA32_CR_PAT, pat); | |
911 | for (i = 0; i < 8; i++) { | |
912 | u8 mtrr = pat >> (8 * i); | |
913 | ||
914 | if (mtrr2protval[mtrr] == MTRR2PROTVAL_INVALID) | |
915 | mtrr2protval[mtrr] = __cm_idx2pte(i); | |
916 | } | |
917 | ||
918 | for (i = 0; i < 8; i++) { | |
919 | if (mtrr2protval[i] == MTRR2PROTVAL_INVALID) { | |
920 | u8 fallback = fallback_mtrr_type(i); | |
921 | mtrr2protval[i] = mtrr2protval[fallback]; | |
922 | BUG_ON(mtrr2protval[i] == MTRR2PROTVAL_INVALID); | |
923 | } | |
924 | } | |
925 | } | |
926 | ||
6aa8b732 AK |
927 | static __init int svm_hardware_setup(void) |
928 | { | |
929 | int cpu; | |
930 | struct page *iopm_pages; | |
f65c229c | 931 | void *iopm_va; |
6aa8b732 AK |
932 | int r; |
933 | ||
6aa8b732 AK |
934 | iopm_pages = alloc_pages(GFP_KERNEL, IOPM_ALLOC_ORDER); |
935 | ||
936 | if (!iopm_pages) | |
937 | return -ENOMEM; | |
c8681339 AL |
938 | |
939 | iopm_va = page_address(iopm_pages); | |
940 | memset(iopm_va, 0xff, PAGE_SIZE * (1 << IOPM_ALLOC_ORDER)); | |
6aa8b732 AK |
941 | iopm_base = page_to_pfn(iopm_pages) << PAGE_SHIFT; |
942 | ||
323c3d80 JR |
943 | init_msrpm_offsets(); |
944 | ||
50a37eb4 JR |
945 | if (boot_cpu_has(X86_FEATURE_NX)) |
946 | kvm_enable_efer_bits(EFER_NX); | |
947 | ||
1b2fd70c AG |
948 | if (boot_cpu_has(X86_FEATURE_FXSR_OPT)) |
949 | kvm_enable_efer_bits(EFER_FFXSR); | |
950 | ||
92a1f12d JR |
951 | if (boot_cpu_has(X86_FEATURE_TSCRATEMSR)) { |
952 | u64 max; | |
953 | ||
954 | kvm_has_tsc_control = true; | |
955 | ||
956 | /* | |
957 | * Make sure the user can only configure tsc_khz values that | |
958 | * fit into a signed integer. | |
959 | * A min value is not calculated needed because it will always | |
960 | * be 1 on all machines and a value of 0 is used to disable | |
961 | * tsc-scaling for the vcpu. | |
962 | */ | |
963 | max = min(0x7fffffffULL, __scale_tsc(tsc_khz, TSC_RATIO_MAX)); | |
964 | ||
965 | kvm_max_guest_tsc_khz = max; | |
966 | } | |
967 | ||
236de055 AG |
968 | if (nested) { |
969 | printk(KERN_INFO "kvm: Nested Virtualization enabled\n"); | |
eec4b140 | 970 | kvm_enable_efer_bits(EFER_SVME | EFER_LMSLE); |
236de055 AG |
971 | } |
972 | ||
3230bb47 | 973 | for_each_possible_cpu(cpu) { |
6aa8b732 AK |
974 | r = svm_cpu_init(cpu); |
975 | if (r) | |
f65c229c | 976 | goto err; |
6aa8b732 | 977 | } |
33bd6a0b | 978 | |
2a6b20b8 | 979 | if (!boot_cpu_has(X86_FEATURE_NPT)) |
e3da3acd JR |
980 | npt_enabled = false; |
981 | ||
6c7dac72 JR |
982 | if (npt_enabled && !npt) { |
983 | printk(KERN_INFO "kvm: Nested Paging disabled\n"); | |
984 | npt_enabled = false; | |
985 | } | |
986 | ||
18552672 | 987 | if (npt_enabled) { |
e3da3acd | 988 | printk(KERN_INFO "kvm: Nested Paging enabled\n"); |
18552672 | 989 | kvm_enable_tdp(); |
5f4cb662 JR |
990 | } else |
991 | kvm_disable_tdp(); | |
e3da3acd | 992 | |
3c2e7f7d | 993 | build_mtrr2protval(); |
6aa8b732 AK |
994 | return 0; |
995 | ||
f65c229c | 996 | err: |
6aa8b732 AK |
997 | __free_pages(iopm_pages, IOPM_ALLOC_ORDER); |
998 | iopm_base = 0; | |
999 | return r; | |
1000 | } | |
1001 | ||
1002 | static __exit void svm_hardware_unsetup(void) | |
1003 | { | |
0da1db75 JR |
1004 | int cpu; |
1005 | ||
3230bb47 | 1006 | for_each_possible_cpu(cpu) |
0da1db75 JR |
1007 | svm_cpu_uninit(cpu); |
1008 | ||
6aa8b732 | 1009 | __free_pages(pfn_to_page(iopm_base >> PAGE_SHIFT), IOPM_ALLOC_ORDER); |
f65c229c | 1010 | iopm_base = 0; |
6aa8b732 AK |
1011 | } |
1012 | ||
1013 | static void init_seg(struct vmcb_seg *seg) | |
1014 | { | |
1015 | seg->selector = 0; | |
1016 | seg->attrib = SVM_SELECTOR_P_MASK | SVM_SELECTOR_S_MASK | | |
e0231715 | 1017 | SVM_SELECTOR_WRITE_MASK; /* Read/Write Data Segment */ |
6aa8b732 AK |
1018 | seg->limit = 0xffff; |
1019 | seg->base = 0; | |
1020 | } | |
1021 | ||
1022 | static void init_sys_seg(struct vmcb_seg *seg, uint32_t type) | |
1023 | { | |
1024 | seg->selector = 0; | |
1025 | seg->attrib = SVM_SELECTOR_P_MASK | type; | |
1026 | seg->limit = 0xffff; | |
1027 | seg->base = 0; | |
1028 | } | |
1029 | ||
fbc0db76 JR |
1030 | static u64 __scale_tsc(u64 ratio, u64 tsc) |
1031 | { | |
1032 | u64 mult, frac, _tsc; | |
1033 | ||
1034 | mult = ratio >> 32; | |
1035 | frac = ratio & ((1ULL << 32) - 1); | |
1036 | ||
1037 | _tsc = tsc; | |
1038 | _tsc *= mult; | |
1039 | _tsc += (tsc >> 32) * frac; | |
1040 | _tsc += ((tsc & ((1ULL << 32) - 1)) * frac) >> 32; | |
1041 | ||
1042 | return _tsc; | |
1043 | } | |
1044 | ||
1045 | static u64 svm_scale_tsc(struct kvm_vcpu *vcpu, u64 tsc) | |
1046 | { | |
1047 | struct vcpu_svm *svm = to_svm(vcpu); | |
1048 | u64 _tsc = tsc; | |
1049 | ||
1050 | if (svm->tsc_ratio != TSC_RATIO_DEFAULT) | |
1051 | _tsc = __scale_tsc(svm->tsc_ratio, tsc); | |
1052 | ||
1053 | return _tsc; | |
1054 | } | |
1055 | ||
cc578287 | 1056 | static void svm_set_tsc_khz(struct kvm_vcpu *vcpu, u32 user_tsc_khz, bool scale) |
4051b188 JR |
1057 | { |
1058 | struct vcpu_svm *svm = to_svm(vcpu); | |
1059 | u64 ratio; | |
1060 | u64 khz; | |
1061 | ||
cc578287 ZA |
1062 | /* Guest TSC same frequency as host TSC? */ |
1063 | if (!scale) { | |
1064 | svm->tsc_ratio = TSC_RATIO_DEFAULT; | |
4051b188 | 1065 | return; |
cc578287 | 1066 | } |
4051b188 | 1067 | |
cc578287 ZA |
1068 | /* TSC scaling supported? */ |
1069 | if (!boot_cpu_has(X86_FEATURE_TSCRATEMSR)) { | |
1070 | if (user_tsc_khz > tsc_khz) { | |
1071 | vcpu->arch.tsc_catchup = 1; | |
1072 | vcpu->arch.tsc_always_catchup = 1; | |
1073 | } else | |
1074 | WARN(1, "user requested TSC rate below hardware speed\n"); | |
4051b188 JR |
1075 | return; |
1076 | } | |
1077 | ||
1078 | khz = user_tsc_khz; | |
1079 | ||
1080 | /* TSC scaling required - calculate ratio */ | |
1081 | ratio = khz << 32; | |
1082 | do_div(ratio, tsc_khz); | |
1083 | ||
1084 | if (ratio == 0 || ratio & TSC_RATIO_RSVD) { | |
1085 | WARN_ONCE(1, "Invalid TSC ratio - virtual-tsc-khz=%u\n", | |
1086 | user_tsc_khz); | |
1087 | return; | |
1088 | } | |
4051b188 JR |
1089 | svm->tsc_ratio = ratio; |
1090 | } | |
1091 | ||
ba904635 WA |
1092 | static u64 svm_read_tsc_offset(struct kvm_vcpu *vcpu) |
1093 | { | |
1094 | struct vcpu_svm *svm = to_svm(vcpu); | |
1095 | ||
1096 | return svm->vmcb->control.tsc_offset; | |
1097 | } | |
1098 | ||
f4e1b3c8 ZA |
1099 | static void svm_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset) |
1100 | { | |
1101 | struct vcpu_svm *svm = to_svm(vcpu); | |
1102 | u64 g_tsc_offset = 0; | |
1103 | ||
2030753d | 1104 | if (is_guest_mode(vcpu)) { |
f4e1b3c8 ZA |
1105 | g_tsc_offset = svm->vmcb->control.tsc_offset - |
1106 | svm->nested.hsave->control.tsc_offset; | |
1107 | svm->nested.hsave->control.tsc_offset = offset; | |
489223ed YY |
1108 | } else |
1109 | trace_kvm_write_tsc_offset(vcpu->vcpu_id, | |
1110 | svm->vmcb->control.tsc_offset, | |
1111 | offset); | |
f4e1b3c8 ZA |
1112 | |
1113 | svm->vmcb->control.tsc_offset = offset + g_tsc_offset; | |
116a0a23 JR |
1114 | |
1115 | mark_dirty(svm->vmcb, VMCB_INTERCEPTS); | |
f4e1b3c8 ZA |
1116 | } |
1117 | ||
f1e2b260 | 1118 | static void svm_adjust_tsc_offset(struct kvm_vcpu *vcpu, s64 adjustment, bool host) |
e48672fa ZA |
1119 | { |
1120 | struct vcpu_svm *svm = to_svm(vcpu); | |
1121 | ||
d913b904 CA |
1122 | if (host) { |
1123 | if (svm->tsc_ratio != TSC_RATIO_DEFAULT) | |
1124 | WARN_ON(adjustment < 0); | |
1125 | adjustment = svm_scale_tsc(vcpu, (u64)adjustment); | |
1126 | } | |
f1e2b260 | 1127 | |
e48672fa | 1128 | svm->vmcb->control.tsc_offset += adjustment; |
2030753d | 1129 | if (is_guest_mode(vcpu)) |
e48672fa | 1130 | svm->nested.hsave->control.tsc_offset += adjustment; |
489223ed YY |
1131 | else |
1132 | trace_kvm_write_tsc_offset(vcpu->vcpu_id, | |
1133 | svm->vmcb->control.tsc_offset - adjustment, | |
1134 | svm->vmcb->control.tsc_offset); | |
1135 | ||
116a0a23 | 1136 | mark_dirty(svm->vmcb, VMCB_INTERCEPTS); |
e48672fa ZA |
1137 | } |
1138 | ||
857e4099 JR |
1139 | static u64 svm_compute_tsc_offset(struct kvm_vcpu *vcpu, u64 target_tsc) |
1140 | { | |
1141 | u64 tsc; | |
1142 | ||
4ea1636b | 1143 | tsc = svm_scale_tsc(vcpu, rdtsc()); |
857e4099 JR |
1144 | |
1145 | return target_tsc - tsc; | |
1146 | } | |
1147 | ||
3c2e7f7d PB |
1148 | static void svm_set_guest_pat(struct vcpu_svm *svm, u64 *g_pat) |
1149 | { | |
1150 | struct kvm_vcpu *vcpu = &svm->vcpu; | |
1151 | ||
1152 | /* Unlike Intel, AMD takes the guest's CR0.CD into account. | |
1153 | * | |
1154 | * AMD does not have IPAT. To emulate it for the case of guests | |
1155 | * with no assigned devices, just set everything to WB. If guests | |
1156 | * have assigned devices, however, we cannot force WB for RAM | |
1157 | * pages only, so use the guest PAT directly. | |
1158 | */ | |
1159 | if (!kvm_arch_has_assigned_device(vcpu->kvm)) | |
1160 | *g_pat = 0x0606060606060606; | |
1161 | else | |
1162 | *g_pat = vcpu->arch.pat; | |
1163 | } | |
1164 | ||
1165 | static u64 svm_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio) | |
1166 | { | |
1167 | u8 mtrr; | |
1168 | ||
1169 | /* | |
606decd6 | 1170 | * 1. MMIO: always map as UC |
3c2e7f7d PB |
1171 | * 2. No passthrough: always map as WB, and force guest PAT to WB as well |
1172 | * 3. Passthrough: can't guarantee the result, try to trust guest. | |
1173 | */ | |
606decd6 PB |
1174 | if (is_mmio) |
1175 | return _PAGE_NOCACHE; | |
1176 | ||
1177 | if (!kvm_arch_has_assigned_device(vcpu->kvm)) | |
3c2e7f7d PB |
1178 | return 0; |
1179 | ||
54928303 PB |
1180 | if (!kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_CD_NW_CLEARED) && |
1181 | kvm_read_cr0(vcpu) & X86_CR0_CD) | |
1182 | return _PAGE_NOCACHE; | |
1183 | ||
3c2e7f7d PB |
1184 | mtrr = kvm_mtrr_get_guest_memory_type(vcpu, gfn); |
1185 | return mtrr2protval[mtrr]; | |
1186 | } | |
1187 | ||
d28bc9dd | 1188 | static void init_vmcb(struct vcpu_svm *svm, bool init_event) |
6aa8b732 | 1189 | { |
e6101a96 JR |
1190 | struct vmcb_control_area *control = &svm->vmcb->control; |
1191 | struct vmcb_save_area *save = &svm->vmcb->save; | |
6aa8b732 | 1192 | |
bff78274 | 1193 | svm->vcpu.fpu_active = 1; |
4ee546b4 | 1194 | svm->vcpu.arch.hflags = 0; |
bff78274 | 1195 | |
4ee546b4 RJ |
1196 | set_cr_intercept(svm, INTERCEPT_CR0_READ); |
1197 | set_cr_intercept(svm, INTERCEPT_CR3_READ); | |
1198 | set_cr_intercept(svm, INTERCEPT_CR4_READ); | |
1199 | set_cr_intercept(svm, INTERCEPT_CR0_WRITE); | |
1200 | set_cr_intercept(svm, INTERCEPT_CR3_WRITE); | |
1201 | set_cr_intercept(svm, INTERCEPT_CR4_WRITE); | |
1202 | set_cr_intercept(svm, INTERCEPT_CR8_WRITE); | |
6aa8b732 | 1203 | |
5315c716 | 1204 | set_dr_intercepts(svm); |
6aa8b732 | 1205 | |
18c918c5 JR |
1206 | set_exception_intercept(svm, PF_VECTOR); |
1207 | set_exception_intercept(svm, UD_VECTOR); | |
1208 | set_exception_intercept(svm, MC_VECTOR); | |
6aa8b732 | 1209 | |
8a05a1b8 JR |
1210 | set_intercept(svm, INTERCEPT_INTR); |
1211 | set_intercept(svm, INTERCEPT_NMI); | |
1212 | set_intercept(svm, INTERCEPT_SMI); | |
1213 | set_intercept(svm, INTERCEPT_SELECTIVE_CR0); | |
332b56e4 | 1214 | set_intercept(svm, INTERCEPT_RDPMC); |
8a05a1b8 JR |
1215 | set_intercept(svm, INTERCEPT_CPUID); |
1216 | set_intercept(svm, INTERCEPT_INVD); | |
1217 | set_intercept(svm, INTERCEPT_HLT); | |
1218 | set_intercept(svm, INTERCEPT_INVLPG); | |
1219 | set_intercept(svm, INTERCEPT_INVLPGA); | |
1220 | set_intercept(svm, INTERCEPT_IOIO_PROT); | |
1221 | set_intercept(svm, INTERCEPT_MSR_PROT); | |
1222 | set_intercept(svm, INTERCEPT_TASK_SWITCH); | |
1223 | set_intercept(svm, INTERCEPT_SHUTDOWN); | |
1224 | set_intercept(svm, INTERCEPT_VMRUN); | |
1225 | set_intercept(svm, INTERCEPT_VMMCALL); | |
1226 | set_intercept(svm, INTERCEPT_VMLOAD); | |
1227 | set_intercept(svm, INTERCEPT_VMSAVE); | |
1228 | set_intercept(svm, INTERCEPT_STGI); | |
1229 | set_intercept(svm, INTERCEPT_CLGI); | |
1230 | set_intercept(svm, INTERCEPT_SKINIT); | |
1231 | set_intercept(svm, INTERCEPT_WBINVD); | |
1232 | set_intercept(svm, INTERCEPT_MONITOR); | |
1233 | set_intercept(svm, INTERCEPT_MWAIT); | |
81dd35d4 | 1234 | set_intercept(svm, INTERCEPT_XSETBV); |
6aa8b732 AK |
1235 | |
1236 | control->iopm_base_pa = iopm_base; | |
f65c229c | 1237 | control->msrpm_base_pa = __pa(svm->msrpm); |
6aa8b732 AK |
1238 | control->int_ctl = V_INTR_MASKING_MASK; |
1239 | ||
1240 | init_seg(&save->es); | |
1241 | init_seg(&save->ss); | |
1242 | init_seg(&save->ds); | |
1243 | init_seg(&save->fs); | |
1244 | init_seg(&save->gs); | |
1245 | ||
1246 | save->cs.selector = 0xf000; | |
04b66839 | 1247 | save->cs.base = 0xffff0000; |
6aa8b732 AK |
1248 | /* Executable/Readable Code Segment */ |
1249 | save->cs.attrib = SVM_SELECTOR_READ_MASK | SVM_SELECTOR_P_MASK | | |
1250 | SVM_SELECTOR_S_MASK | SVM_SELECTOR_CODE_MASK; | |
1251 | save->cs.limit = 0xffff; | |
6aa8b732 AK |
1252 | |
1253 | save->gdtr.limit = 0xffff; | |
1254 | save->idtr.limit = 0xffff; | |
1255 | ||
1256 | init_sys_seg(&save->ldtr, SEG_TYPE_LDT); | |
1257 | init_sys_seg(&save->tr, SEG_TYPE_BUSY_TSS16); | |
1258 | ||
d28bc9dd NA |
1259 | if (!init_event) |
1260 | svm_set_efer(&svm->vcpu, 0); | |
d77c26fc | 1261 | save->dr6 = 0xffff0ff0; |
f6e78475 | 1262 | kvm_set_rflags(&svm->vcpu, 2); |
6aa8b732 | 1263 | save->rip = 0x0000fff0; |
5fdbf976 | 1264 | svm->vcpu.arch.regs[VCPU_REGS_RIP] = save->rip; |
6aa8b732 | 1265 | |
e0231715 | 1266 | /* |
18fa000a | 1267 | * svm_set_cr0() sets PG and WP and clears NW and CD on save->cr0. |
d28bc9dd | 1268 | * It also updates the guest-visible cr0 value. |
6aa8b732 | 1269 | */ |
79a8059d | 1270 | svm_set_cr0(&svm->vcpu, X86_CR0_NW | X86_CR0_CD | X86_CR0_ET); |
ebae871a | 1271 | kvm_mmu_reset_context(&svm->vcpu); |
18fa000a | 1272 | |
66aee91a | 1273 | save->cr4 = X86_CR4_PAE; |
6aa8b732 | 1274 | /* rdx = ?? */ |
709ddebf JR |
1275 | |
1276 | if (npt_enabled) { | |
1277 | /* Setup VMCB for Nested Paging */ | |
1278 | control->nested_ctl = 1; | |
8a05a1b8 | 1279 | clr_intercept(svm, INTERCEPT_INVLPG); |
18c918c5 | 1280 | clr_exception_intercept(svm, PF_VECTOR); |
4ee546b4 RJ |
1281 | clr_cr_intercept(svm, INTERCEPT_CR3_READ); |
1282 | clr_cr_intercept(svm, INTERCEPT_CR3_WRITE); | |
74545705 | 1283 | save->g_pat = svm->vcpu.arch.pat; |
3c2e7f7d | 1284 | svm_set_guest_pat(svm, &save->g_pat); |
709ddebf JR |
1285 | save->cr3 = 0; |
1286 | save->cr4 = 0; | |
1287 | } | |
f40f6a45 | 1288 | svm->asid_generation = 0; |
1371d904 | 1289 | |
e6aa9abd | 1290 | svm->nested.vmcb = 0; |
2af9194d JR |
1291 | svm->vcpu.arch.hflags = 0; |
1292 | ||
2a6b20b8 | 1293 | if (boot_cpu_has(X86_FEATURE_PAUSEFILTER)) { |
565d0998 | 1294 | control->pause_filter_count = 3000; |
8a05a1b8 | 1295 | set_intercept(svm, INTERCEPT_PAUSE); |
565d0998 ML |
1296 | } |
1297 | ||
8d28fec4 RJ |
1298 | mark_all_dirty(svm->vmcb); |
1299 | ||
2af9194d | 1300 | enable_gif(svm); |
6aa8b732 AK |
1301 | } |
1302 | ||
d28bc9dd | 1303 | static void svm_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event) |
04d2cc77 AK |
1304 | { |
1305 | struct vcpu_svm *svm = to_svm(vcpu); | |
66f7b72e JS |
1306 | u32 dummy; |
1307 | u32 eax = 1; | |
04d2cc77 | 1308 | |
d28bc9dd NA |
1309 | if (!init_event) { |
1310 | svm->vcpu.arch.apic_base = APIC_DEFAULT_PHYS_BASE | | |
1311 | MSR_IA32_APICBASE_ENABLE; | |
1312 | if (kvm_vcpu_is_reset_bsp(&svm->vcpu)) | |
1313 | svm->vcpu.arch.apic_base |= MSR_IA32_APICBASE_BSP; | |
1314 | } | |
1315 | init_vmcb(svm, init_event); | |
70433389 | 1316 | |
66f7b72e JS |
1317 | kvm_cpuid(vcpu, &eax, &dummy, &dummy, &dummy); |
1318 | kvm_register_write(vcpu, VCPU_REGS_RDX, eax); | |
04d2cc77 AK |
1319 | } |
1320 | ||
fb3f0f51 | 1321 | static struct kvm_vcpu *svm_create_vcpu(struct kvm *kvm, unsigned int id) |
6aa8b732 | 1322 | { |
a2fa3e9f | 1323 | struct vcpu_svm *svm; |
6aa8b732 | 1324 | struct page *page; |
f65c229c | 1325 | struct page *msrpm_pages; |
b286d5d8 | 1326 | struct page *hsave_page; |
3d6368ef | 1327 | struct page *nested_msrpm_pages; |
fb3f0f51 | 1328 | int err; |
6aa8b732 | 1329 | |
c16f862d | 1330 | svm = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL); |
fb3f0f51 RR |
1331 | if (!svm) { |
1332 | err = -ENOMEM; | |
1333 | goto out; | |
1334 | } | |
1335 | ||
fbc0db76 JR |
1336 | svm->tsc_ratio = TSC_RATIO_DEFAULT; |
1337 | ||
fb3f0f51 RR |
1338 | err = kvm_vcpu_init(&svm->vcpu, kvm, id); |
1339 | if (err) | |
1340 | goto free_svm; | |
1341 | ||
b7af4043 | 1342 | err = -ENOMEM; |
6aa8b732 | 1343 | page = alloc_page(GFP_KERNEL); |
b7af4043 | 1344 | if (!page) |
fb3f0f51 | 1345 | goto uninit; |
6aa8b732 | 1346 | |
f65c229c JR |
1347 | msrpm_pages = alloc_pages(GFP_KERNEL, MSRPM_ALLOC_ORDER); |
1348 | if (!msrpm_pages) | |
b7af4043 | 1349 | goto free_page1; |
3d6368ef AG |
1350 | |
1351 | nested_msrpm_pages = alloc_pages(GFP_KERNEL, MSRPM_ALLOC_ORDER); | |
1352 | if (!nested_msrpm_pages) | |
b7af4043 | 1353 | goto free_page2; |
f65c229c | 1354 | |
b286d5d8 AG |
1355 | hsave_page = alloc_page(GFP_KERNEL); |
1356 | if (!hsave_page) | |
b7af4043 TY |
1357 | goto free_page3; |
1358 | ||
e6aa9abd | 1359 | svm->nested.hsave = page_address(hsave_page); |
b286d5d8 | 1360 | |
b7af4043 TY |
1361 | svm->msrpm = page_address(msrpm_pages); |
1362 | svm_vcpu_init_msrpm(svm->msrpm); | |
1363 | ||
e6aa9abd | 1364 | svm->nested.msrpm = page_address(nested_msrpm_pages); |
323c3d80 | 1365 | svm_vcpu_init_msrpm(svm->nested.msrpm); |
3d6368ef | 1366 | |
a2fa3e9f GH |
1367 | svm->vmcb = page_address(page); |
1368 | clear_page(svm->vmcb); | |
1369 | svm->vmcb_pa = page_to_pfn(page) << PAGE_SHIFT; | |
1370 | svm->asid_generation = 0; | |
d28bc9dd | 1371 | init_vmcb(svm, false); |
6aa8b732 | 1372 | |
2b036c6b BO |
1373 | svm_init_osvw(&svm->vcpu); |
1374 | ||
fb3f0f51 | 1375 | return &svm->vcpu; |
36241b8c | 1376 | |
b7af4043 TY |
1377 | free_page3: |
1378 | __free_pages(nested_msrpm_pages, MSRPM_ALLOC_ORDER); | |
1379 | free_page2: | |
1380 | __free_pages(msrpm_pages, MSRPM_ALLOC_ORDER); | |
1381 | free_page1: | |
1382 | __free_page(page); | |
fb3f0f51 RR |
1383 | uninit: |
1384 | kvm_vcpu_uninit(&svm->vcpu); | |
1385 | free_svm: | |
a4770347 | 1386 | kmem_cache_free(kvm_vcpu_cache, svm); |
fb3f0f51 RR |
1387 | out: |
1388 | return ERR_PTR(err); | |
6aa8b732 AK |
1389 | } |
1390 | ||
1391 | static void svm_free_vcpu(struct kvm_vcpu *vcpu) | |
1392 | { | |
a2fa3e9f GH |
1393 | struct vcpu_svm *svm = to_svm(vcpu); |
1394 | ||
fb3f0f51 | 1395 | __free_page(pfn_to_page(svm->vmcb_pa >> PAGE_SHIFT)); |
f65c229c | 1396 | __free_pages(virt_to_page(svm->msrpm), MSRPM_ALLOC_ORDER); |
e6aa9abd JR |
1397 | __free_page(virt_to_page(svm->nested.hsave)); |
1398 | __free_pages(virt_to_page(svm->nested.msrpm), MSRPM_ALLOC_ORDER); | |
fb3f0f51 | 1399 | kvm_vcpu_uninit(vcpu); |
a4770347 | 1400 | kmem_cache_free(kvm_vcpu_cache, svm); |
6aa8b732 AK |
1401 | } |
1402 | ||
15ad7146 | 1403 | static void svm_vcpu_load(struct kvm_vcpu *vcpu, int cpu) |
6aa8b732 | 1404 | { |
a2fa3e9f | 1405 | struct vcpu_svm *svm = to_svm(vcpu); |
15ad7146 | 1406 | int i; |
0cc5064d | 1407 | |
0cc5064d | 1408 | if (unlikely(cpu != vcpu->cpu)) { |
4b656b12 | 1409 | svm->asid_generation = 0; |
8d28fec4 | 1410 | mark_all_dirty(svm->vmcb); |
0cc5064d | 1411 | } |
94dfbdb3 | 1412 | |
82ca2d10 AK |
1413 | #ifdef CONFIG_X86_64 |
1414 | rdmsrl(MSR_GS_BASE, to_svm(vcpu)->host.gs_base); | |
1415 | #endif | |
dacccfdd AK |
1416 | savesegment(fs, svm->host.fs); |
1417 | savesegment(gs, svm->host.gs); | |
1418 | svm->host.ldt = kvm_read_ldt(); | |
1419 | ||
94dfbdb3 | 1420 | for (i = 0; i < NR_HOST_SAVE_USER_MSRS; i++) |
a2fa3e9f | 1421 | rdmsrl(host_save_user_msrs[i], svm->host_user_msrs[i]); |
fbc0db76 JR |
1422 | |
1423 | if (static_cpu_has(X86_FEATURE_TSCRATEMSR) && | |
89cbc767 CL |
1424 | svm->tsc_ratio != __this_cpu_read(current_tsc_ratio)) { |
1425 | __this_cpu_write(current_tsc_ratio, svm->tsc_ratio); | |
fbc0db76 JR |
1426 | wrmsrl(MSR_AMD64_TSC_RATIO, svm->tsc_ratio); |
1427 | } | |
6aa8b732 AK |
1428 | } |
1429 | ||
1430 | static void svm_vcpu_put(struct kvm_vcpu *vcpu) | |
1431 | { | |
a2fa3e9f | 1432 | struct vcpu_svm *svm = to_svm(vcpu); |
94dfbdb3 AL |
1433 | int i; |
1434 | ||
e1beb1d3 | 1435 | ++vcpu->stat.host_state_reload; |
dacccfdd AK |
1436 | kvm_load_ldt(svm->host.ldt); |
1437 | #ifdef CONFIG_X86_64 | |
1438 | loadsegment(fs, svm->host.fs); | |
dacccfdd | 1439 | wrmsrl(MSR_KERNEL_GS_BASE, current->thread.gs); |
893a5ab6 | 1440 | load_gs_index(svm->host.gs); |
dacccfdd | 1441 | #else |
831ca609 | 1442 | #ifdef CONFIG_X86_32_LAZY_GS |
dacccfdd | 1443 | loadsegment(gs, svm->host.gs); |
831ca609 | 1444 | #endif |
dacccfdd | 1445 | #endif |
94dfbdb3 | 1446 | for (i = 0; i < NR_HOST_SAVE_USER_MSRS; i++) |
a2fa3e9f | 1447 | wrmsrl(host_save_user_msrs[i], svm->host_user_msrs[i]); |
6aa8b732 AK |
1448 | } |
1449 | ||
6aa8b732 AK |
1450 | static unsigned long svm_get_rflags(struct kvm_vcpu *vcpu) |
1451 | { | |
a2fa3e9f | 1452 | return to_svm(vcpu)->vmcb->save.rflags; |
6aa8b732 AK |
1453 | } |
1454 | ||
1455 | static void svm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags) | |
1456 | { | |
ae9fedc7 PB |
1457 | /* |
1458 | * Any change of EFLAGS.VM is accompained by a reload of SS | |
1459 | * (caused by either a task switch or an inter-privilege IRET), | |
1460 | * so we do not need to update the CPL here. | |
1461 | */ | |
a2fa3e9f | 1462 | to_svm(vcpu)->vmcb->save.rflags = rflags; |
6aa8b732 AK |
1463 | } |
1464 | ||
6de4f3ad AK |
1465 | static void svm_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg) |
1466 | { | |
1467 | switch (reg) { | |
1468 | case VCPU_EXREG_PDPTR: | |
1469 | BUG_ON(!npt_enabled); | |
9f8fe504 | 1470 | load_pdptrs(vcpu, vcpu->arch.walk_mmu, kvm_read_cr3(vcpu)); |
6de4f3ad AK |
1471 | break; |
1472 | default: | |
1473 | BUG(); | |
1474 | } | |
1475 | } | |
1476 | ||
f0b85051 AG |
1477 | static void svm_set_vintr(struct vcpu_svm *svm) |
1478 | { | |
8a05a1b8 | 1479 | set_intercept(svm, INTERCEPT_VINTR); |
f0b85051 AG |
1480 | } |
1481 | ||
1482 | static void svm_clear_vintr(struct vcpu_svm *svm) | |
1483 | { | |
8a05a1b8 | 1484 | clr_intercept(svm, INTERCEPT_VINTR); |
f0b85051 AG |
1485 | } |
1486 | ||
6aa8b732 AK |
1487 | static struct vmcb_seg *svm_seg(struct kvm_vcpu *vcpu, int seg) |
1488 | { | |
a2fa3e9f | 1489 | struct vmcb_save_area *save = &to_svm(vcpu)->vmcb->save; |
6aa8b732 AK |
1490 | |
1491 | switch (seg) { | |
1492 | case VCPU_SREG_CS: return &save->cs; | |
1493 | case VCPU_SREG_DS: return &save->ds; | |
1494 | case VCPU_SREG_ES: return &save->es; | |
1495 | case VCPU_SREG_FS: return &save->fs; | |
1496 | case VCPU_SREG_GS: return &save->gs; | |
1497 | case VCPU_SREG_SS: return &save->ss; | |
1498 | case VCPU_SREG_TR: return &save->tr; | |
1499 | case VCPU_SREG_LDTR: return &save->ldtr; | |
1500 | } | |
1501 | BUG(); | |
8b6d44c7 | 1502 | return NULL; |
6aa8b732 AK |
1503 | } |
1504 | ||
1505 | static u64 svm_get_segment_base(struct kvm_vcpu *vcpu, int seg) | |
1506 | { | |
1507 | struct vmcb_seg *s = svm_seg(vcpu, seg); | |
1508 | ||
1509 | return s->base; | |
1510 | } | |
1511 | ||
1512 | static void svm_get_segment(struct kvm_vcpu *vcpu, | |
1513 | struct kvm_segment *var, int seg) | |
1514 | { | |
1515 | struct vmcb_seg *s = svm_seg(vcpu, seg); | |
1516 | ||
1517 | var->base = s->base; | |
1518 | var->limit = s->limit; | |
1519 | var->selector = s->selector; | |
1520 | var->type = s->attrib & SVM_SELECTOR_TYPE_MASK; | |
1521 | var->s = (s->attrib >> SVM_SELECTOR_S_SHIFT) & 1; | |
1522 | var->dpl = (s->attrib >> SVM_SELECTOR_DPL_SHIFT) & 3; | |
1523 | var->present = (s->attrib >> SVM_SELECTOR_P_SHIFT) & 1; | |
1524 | var->avl = (s->attrib >> SVM_SELECTOR_AVL_SHIFT) & 1; | |
1525 | var->l = (s->attrib >> SVM_SELECTOR_L_SHIFT) & 1; | |
1526 | var->db = (s->attrib >> SVM_SELECTOR_DB_SHIFT) & 1; | |
80112c89 JM |
1527 | |
1528 | /* | |
1529 | * AMD CPUs circa 2014 track the G bit for all segments except CS. | |
1530 | * However, the SVM spec states that the G bit is not observed by the | |
1531 | * CPU, and some VMware virtual CPUs drop the G bit for all segments. | |
1532 | * So let's synthesize a legal G bit for all segments, this helps | |
1533 | * running KVM nested. It also helps cross-vendor migration, because | |
1534 | * Intel's vmentry has a check on the 'G' bit. | |
1535 | */ | |
1536 | var->g = s->limit > 0xfffff; | |
25022acc | 1537 | |
e0231715 JR |
1538 | /* |
1539 | * AMD's VMCB does not have an explicit unusable field, so emulate it | |
19bca6ab AP |
1540 | * for cross vendor migration purposes by "not present" |
1541 | */ | |
1542 | var->unusable = !var->present || (var->type == 0); | |
1543 | ||
1fbdc7a5 | 1544 | switch (seg) { |
1fbdc7a5 AP |
1545 | case VCPU_SREG_TR: |
1546 | /* | |
1547 | * Work around a bug where the busy flag in the tr selector | |
1548 | * isn't exposed | |
1549 | */ | |
c0d09828 | 1550 | var->type |= 0x2; |
1fbdc7a5 AP |
1551 | break; |
1552 | case VCPU_SREG_DS: | |
1553 | case VCPU_SREG_ES: | |
1554 | case VCPU_SREG_FS: | |
1555 | case VCPU_SREG_GS: | |
1556 | /* | |
1557 | * The accessed bit must always be set in the segment | |
1558 | * descriptor cache, although it can be cleared in the | |
1559 | * descriptor, the cached bit always remains at 1. Since | |
1560 | * Intel has a check on this, set it here to support | |
1561 | * cross-vendor migration. | |
1562 | */ | |
1563 | if (!var->unusable) | |
1564 | var->type |= 0x1; | |
1565 | break; | |
b586eb02 | 1566 | case VCPU_SREG_SS: |
e0231715 JR |
1567 | /* |
1568 | * On AMD CPUs sometimes the DB bit in the segment | |
b586eb02 AP |
1569 | * descriptor is left as 1, although the whole segment has |
1570 | * been made unusable. Clear it here to pass an Intel VMX | |
1571 | * entry check when cross vendor migrating. | |
1572 | */ | |
1573 | if (var->unusable) | |
1574 | var->db = 0; | |
33b458d2 | 1575 | var->dpl = to_svm(vcpu)->vmcb->save.cpl; |
b586eb02 | 1576 | break; |
1fbdc7a5 | 1577 | } |
6aa8b732 AK |
1578 | } |
1579 | ||
2e4d2653 IE |
1580 | static int svm_get_cpl(struct kvm_vcpu *vcpu) |
1581 | { | |
1582 | struct vmcb_save_area *save = &to_svm(vcpu)->vmcb->save; | |
1583 | ||
1584 | return save->cpl; | |
1585 | } | |
1586 | ||
89a27f4d | 1587 | static void svm_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt) |
6aa8b732 | 1588 | { |
a2fa3e9f GH |
1589 | struct vcpu_svm *svm = to_svm(vcpu); |
1590 | ||
89a27f4d GN |
1591 | dt->size = svm->vmcb->save.idtr.limit; |
1592 | dt->address = svm->vmcb->save.idtr.base; | |
6aa8b732 AK |
1593 | } |
1594 | ||
89a27f4d | 1595 | static void svm_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt) |
6aa8b732 | 1596 | { |
a2fa3e9f GH |
1597 | struct vcpu_svm *svm = to_svm(vcpu); |
1598 | ||
89a27f4d GN |
1599 | svm->vmcb->save.idtr.limit = dt->size; |
1600 | svm->vmcb->save.idtr.base = dt->address ; | |
17a703cb | 1601 | mark_dirty(svm->vmcb, VMCB_DT); |
6aa8b732 AK |
1602 | } |
1603 | ||
89a27f4d | 1604 | static void svm_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt) |
6aa8b732 | 1605 | { |
a2fa3e9f GH |
1606 | struct vcpu_svm *svm = to_svm(vcpu); |
1607 | ||
89a27f4d GN |
1608 | dt->size = svm->vmcb->save.gdtr.limit; |
1609 | dt->address = svm->vmcb->save.gdtr.base; | |
6aa8b732 AK |
1610 | } |
1611 | ||
89a27f4d | 1612 | static void svm_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt) |
6aa8b732 | 1613 | { |
a2fa3e9f GH |
1614 | struct vcpu_svm *svm = to_svm(vcpu); |
1615 | ||
89a27f4d GN |
1616 | svm->vmcb->save.gdtr.limit = dt->size; |
1617 | svm->vmcb->save.gdtr.base = dt->address ; | |
17a703cb | 1618 | mark_dirty(svm->vmcb, VMCB_DT); |
6aa8b732 AK |
1619 | } |
1620 | ||
e8467fda AK |
1621 | static void svm_decache_cr0_guest_bits(struct kvm_vcpu *vcpu) |
1622 | { | |
1623 | } | |
1624 | ||
aff48baa AK |
1625 | static void svm_decache_cr3(struct kvm_vcpu *vcpu) |
1626 | { | |
1627 | } | |
1628 | ||
25c4c276 | 1629 | static void svm_decache_cr4_guest_bits(struct kvm_vcpu *vcpu) |
399badf3 AK |
1630 | { |
1631 | } | |
1632 | ||
d225157b AK |
1633 | static void update_cr0_intercept(struct vcpu_svm *svm) |
1634 | { | |
1635 | ulong gcr0 = svm->vcpu.arch.cr0; | |
1636 | u64 *hcr0 = &svm->vmcb->save.cr0; | |
1637 | ||
1638 | if (!svm->vcpu.fpu_active) | |
1639 | *hcr0 |= SVM_CR0_SELECTIVE_MASK; | |
1640 | else | |
1641 | *hcr0 = (*hcr0 & ~SVM_CR0_SELECTIVE_MASK) | |
1642 | | (gcr0 & SVM_CR0_SELECTIVE_MASK); | |
1643 | ||
dcca1a65 | 1644 | mark_dirty(svm->vmcb, VMCB_CR); |
d225157b AK |
1645 | |
1646 | if (gcr0 == *hcr0 && svm->vcpu.fpu_active) { | |
4ee546b4 RJ |
1647 | clr_cr_intercept(svm, INTERCEPT_CR0_READ); |
1648 | clr_cr_intercept(svm, INTERCEPT_CR0_WRITE); | |
d225157b | 1649 | } else { |
4ee546b4 RJ |
1650 | set_cr_intercept(svm, INTERCEPT_CR0_READ); |
1651 | set_cr_intercept(svm, INTERCEPT_CR0_WRITE); | |
d225157b AK |
1652 | } |
1653 | } | |
1654 | ||
6aa8b732 AK |
1655 | static void svm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0) |
1656 | { | |
a2fa3e9f GH |
1657 | struct vcpu_svm *svm = to_svm(vcpu); |
1658 | ||
05b3e0c2 | 1659 | #ifdef CONFIG_X86_64 |
f6801dff | 1660 | if (vcpu->arch.efer & EFER_LME) { |
707d92fa | 1661 | if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) { |
f6801dff | 1662 | vcpu->arch.efer |= EFER_LMA; |
2b5203ee | 1663 | svm->vmcb->save.efer |= EFER_LMA | EFER_LME; |
6aa8b732 AK |
1664 | } |
1665 | ||
d77c26fc | 1666 | if (is_paging(vcpu) && !(cr0 & X86_CR0_PG)) { |
f6801dff | 1667 | vcpu->arch.efer &= ~EFER_LMA; |
2b5203ee | 1668 | svm->vmcb->save.efer &= ~(EFER_LMA | EFER_LME); |
6aa8b732 AK |
1669 | } |
1670 | } | |
1671 | #endif | |
ad312c7c | 1672 | vcpu->arch.cr0 = cr0; |
888f9f3e AK |
1673 | |
1674 | if (!npt_enabled) | |
1675 | cr0 |= X86_CR0_PG | X86_CR0_WP; | |
02daab21 AK |
1676 | |
1677 | if (!vcpu->fpu_active) | |
334df50a | 1678 | cr0 |= X86_CR0_TS; |
54928303 PB |
1679 | |
1680 | /* These are emulated via page tables. */ | |
1681 | cr0 &= ~(X86_CR0_CD | X86_CR0_NW); | |
1682 | ||
a2fa3e9f | 1683 | svm->vmcb->save.cr0 = cr0; |
dcca1a65 | 1684 | mark_dirty(svm->vmcb, VMCB_CR); |
d225157b | 1685 | update_cr0_intercept(svm); |
6aa8b732 AK |
1686 | } |
1687 | ||
5e1746d6 | 1688 | static int svm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4) |
6aa8b732 | 1689 | { |
1e02ce4c | 1690 | unsigned long host_cr4_mce = cr4_read_shadow() & X86_CR4_MCE; |
e5eab0ce JR |
1691 | unsigned long old_cr4 = to_svm(vcpu)->vmcb->save.cr4; |
1692 | ||
5e1746d6 NHE |
1693 | if (cr4 & X86_CR4_VMXE) |
1694 | return 1; | |
1695 | ||
e5eab0ce | 1696 | if (npt_enabled && ((old_cr4 ^ cr4) & X86_CR4_PGE)) |
f40f6a45 | 1697 | svm_flush_tlb(vcpu); |
6394b649 | 1698 | |
ec077263 JR |
1699 | vcpu->arch.cr4 = cr4; |
1700 | if (!npt_enabled) | |
1701 | cr4 |= X86_CR4_PAE; | |
6394b649 | 1702 | cr4 |= host_cr4_mce; |
ec077263 | 1703 | to_svm(vcpu)->vmcb->save.cr4 = cr4; |
dcca1a65 | 1704 | mark_dirty(to_svm(vcpu)->vmcb, VMCB_CR); |
5e1746d6 | 1705 | return 0; |
6aa8b732 AK |
1706 | } |
1707 | ||
1708 | static void svm_set_segment(struct kvm_vcpu *vcpu, | |
1709 | struct kvm_segment *var, int seg) | |
1710 | { | |
a2fa3e9f | 1711 | struct vcpu_svm *svm = to_svm(vcpu); |
6aa8b732 AK |
1712 | struct vmcb_seg *s = svm_seg(vcpu, seg); |
1713 | ||
1714 | s->base = var->base; | |
1715 | s->limit = var->limit; | |
1716 | s->selector = var->selector; | |
1717 | if (var->unusable) | |
1718 | s->attrib = 0; | |
1719 | else { | |
1720 | s->attrib = (var->type & SVM_SELECTOR_TYPE_MASK); | |
1721 | s->attrib |= (var->s & 1) << SVM_SELECTOR_S_SHIFT; | |
1722 | s->attrib |= (var->dpl & 3) << SVM_SELECTOR_DPL_SHIFT; | |
1723 | s->attrib |= (var->present & 1) << SVM_SELECTOR_P_SHIFT; | |
1724 | s->attrib |= (var->avl & 1) << SVM_SELECTOR_AVL_SHIFT; | |
1725 | s->attrib |= (var->l & 1) << SVM_SELECTOR_L_SHIFT; | |
1726 | s->attrib |= (var->db & 1) << SVM_SELECTOR_DB_SHIFT; | |
1727 | s->attrib |= (var->g & 1) << SVM_SELECTOR_G_SHIFT; | |
1728 | } | |
ae9fedc7 PB |
1729 | |
1730 | /* | |
1731 | * This is always accurate, except if SYSRET returned to a segment | |
1732 | * with SS.DPL != 3. Intel does not have this quirk, and always | |
1733 | * forces SS.DPL to 3 on sysret, so we ignore that case; fixing it | |
1734 | * would entail passing the CPL to userspace and back. | |
1735 | */ | |
1736 | if (seg == VCPU_SREG_SS) | |
1737 | svm->vmcb->save.cpl = (s->attrib >> SVM_SELECTOR_DPL_SHIFT) & 3; | |
6aa8b732 | 1738 | |
060d0c9a | 1739 | mark_dirty(svm->vmcb, VMCB_SEG); |
6aa8b732 AK |
1740 | } |
1741 | ||
c8639010 | 1742 | static void update_db_bp_intercept(struct kvm_vcpu *vcpu) |
6aa8b732 | 1743 | { |
d0bfb940 JK |
1744 | struct vcpu_svm *svm = to_svm(vcpu); |
1745 | ||
18c918c5 JR |
1746 | clr_exception_intercept(svm, DB_VECTOR); |
1747 | clr_exception_intercept(svm, BP_VECTOR); | |
44c11430 | 1748 | |
6be7d306 | 1749 | if (svm->nmi_singlestep) |
18c918c5 | 1750 | set_exception_intercept(svm, DB_VECTOR); |
44c11430 | 1751 | |
d0bfb940 JK |
1752 | if (vcpu->guest_debug & KVM_GUESTDBG_ENABLE) { |
1753 | if (vcpu->guest_debug & | |
1754 | (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)) | |
18c918c5 | 1755 | set_exception_intercept(svm, DB_VECTOR); |
d0bfb940 | 1756 | if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP) |
18c918c5 | 1757 | set_exception_intercept(svm, BP_VECTOR); |
d0bfb940 JK |
1758 | } else |
1759 | vcpu->guest_debug = 0; | |
44c11430 GN |
1760 | } |
1761 | ||
0fe1e009 | 1762 | static void new_asid(struct vcpu_svm *svm, struct svm_cpu_data *sd) |
6aa8b732 | 1763 | { |
0fe1e009 TH |
1764 | if (sd->next_asid > sd->max_asid) { |
1765 | ++sd->asid_generation; | |
1766 | sd->next_asid = 1; | |
a2fa3e9f | 1767 | svm->vmcb->control.tlb_ctl = TLB_CONTROL_FLUSH_ALL_ASID; |
6aa8b732 AK |
1768 | } |
1769 | ||
0fe1e009 TH |
1770 | svm->asid_generation = sd->asid_generation; |
1771 | svm->vmcb->control.asid = sd->next_asid++; | |
d48086d1 JR |
1772 | |
1773 | mark_dirty(svm->vmcb, VMCB_ASID); | |
6aa8b732 AK |
1774 | } |
1775 | ||
73aaf249 JK |
1776 | static u64 svm_get_dr6(struct kvm_vcpu *vcpu) |
1777 | { | |
1778 | return to_svm(vcpu)->vmcb->save.dr6; | |
1779 | } | |
1780 | ||
1781 | static void svm_set_dr6(struct kvm_vcpu *vcpu, unsigned long value) | |
1782 | { | |
1783 | struct vcpu_svm *svm = to_svm(vcpu); | |
1784 | ||
1785 | svm->vmcb->save.dr6 = value; | |
1786 | mark_dirty(svm->vmcb, VMCB_DR); | |
1787 | } | |
1788 | ||
facb0139 PB |
1789 | static void svm_sync_dirty_debug_regs(struct kvm_vcpu *vcpu) |
1790 | { | |
1791 | struct vcpu_svm *svm = to_svm(vcpu); | |
1792 | ||
1793 | get_debugreg(vcpu->arch.db[0], 0); | |
1794 | get_debugreg(vcpu->arch.db[1], 1); | |
1795 | get_debugreg(vcpu->arch.db[2], 2); | |
1796 | get_debugreg(vcpu->arch.db[3], 3); | |
1797 | vcpu->arch.dr6 = svm_get_dr6(vcpu); | |
1798 | vcpu->arch.dr7 = svm->vmcb->save.dr7; | |
1799 | ||
1800 | vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_WONT_EXIT; | |
1801 | set_dr_intercepts(svm); | |
1802 | } | |
1803 | ||
020df079 | 1804 | static void svm_set_dr7(struct kvm_vcpu *vcpu, unsigned long value) |
6aa8b732 | 1805 | { |
42dbaa5a | 1806 | struct vcpu_svm *svm = to_svm(vcpu); |
42dbaa5a | 1807 | |
020df079 | 1808 | svm->vmcb->save.dr7 = value; |
72214b96 | 1809 | mark_dirty(svm->vmcb, VMCB_DR); |
6aa8b732 AK |
1810 | } |
1811 | ||
851ba692 | 1812 | static int pf_interception(struct vcpu_svm *svm) |
6aa8b732 | 1813 | { |
631bc487 | 1814 | u64 fault_address = svm->vmcb->control.exit_info_2; |
6aa8b732 | 1815 | u32 error_code; |
631bc487 | 1816 | int r = 1; |
6aa8b732 | 1817 | |
631bc487 GN |
1818 | switch (svm->apf_reason) { |
1819 | default: | |
1820 | error_code = svm->vmcb->control.exit_info_1; | |
af9ca2d7 | 1821 | |
631bc487 GN |
1822 | trace_kvm_page_fault(fault_address, error_code); |
1823 | if (!npt_enabled && kvm_event_needs_reinjection(&svm->vcpu)) | |
1824 | kvm_mmu_unprotect_page_virt(&svm->vcpu, fault_address); | |
dc25e89e AP |
1825 | r = kvm_mmu_page_fault(&svm->vcpu, fault_address, error_code, |
1826 | svm->vmcb->control.insn_bytes, | |
1827 | svm->vmcb->control.insn_len); | |
631bc487 GN |
1828 | break; |
1829 | case KVM_PV_REASON_PAGE_NOT_PRESENT: | |
1830 | svm->apf_reason = 0; | |
1831 | local_irq_disable(); | |
1832 | kvm_async_pf_task_wait(fault_address); | |
1833 | local_irq_enable(); | |
1834 | break; | |
1835 | case KVM_PV_REASON_PAGE_READY: | |
1836 | svm->apf_reason = 0; | |
1837 | local_irq_disable(); | |
1838 | kvm_async_pf_task_wake(fault_address); | |
1839 | local_irq_enable(); | |
1840 | break; | |
1841 | } | |
1842 | return r; | |
6aa8b732 AK |
1843 | } |
1844 | ||
851ba692 | 1845 | static int db_interception(struct vcpu_svm *svm) |
d0bfb940 | 1846 | { |
851ba692 AK |
1847 | struct kvm_run *kvm_run = svm->vcpu.run; |
1848 | ||
d0bfb940 | 1849 | if (!(svm->vcpu.guest_debug & |
44c11430 | 1850 | (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)) && |
6be7d306 | 1851 | !svm->nmi_singlestep) { |
d0bfb940 JK |
1852 | kvm_queue_exception(&svm->vcpu, DB_VECTOR); |
1853 | return 1; | |
1854 | } | |
44c11430 | 1855 | |
6be7d306 JK |
1856 | if (svm->nmi_singlestep) { |
1857 | svm->nmi_singlestep = false; | |
44c11430 GN |
1858 | if (!(svm->vcpu.guest_debug & KVM_GUESTDBG_SINGLESTEP)) |
1859 | svm->vmcb->save.rflags &= | |
1860 | ~(X86_EFLAGS_TF | X86_EFLAGS_RF); | |
c8639010 | 1861 | update_db_bp_intercept(&svm->vcpu); |
44c11430 GN |
1862 | } |
1863 | ||
1864 | if (svm->vcpu.guest_debug & | |
e0231715 | 1865 | (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)) { |
44c11430 GN |
1866 | kvm_run->exit_reason = KVM_EXIT_DEBUG; |
1867 | kvm_run->debug.arch.pc = | |
1868 | svm->vmcb->save.cs.base + svm->vmcb->save.rip; | |
1869 | kvm_run->debug.arch.exception = DB_VECTOR; | |
1870 | return 0; | |
1871 | } | |
1872 | ||
1873 | return 1; | |
d0bfb940 JK |
1874 | } |
1875 | ||
851ba692 | 1876 | static int bp_interception(struct vcpu_svm *svm) |
d0bfb940 | 1877 | { |
851ba692 AK |
1878 | struct kvm_run *kvm_run = svm->vcpu.run; |
1879 | ||
d0bfb940 JK |
1880 | kvm_run->exit_reason = KVM_EXIT_DEBUG; |
1881 | kvm_run->debug.arch.pc = svm->vmcb->save.cs.base + svm->vmcb->save.rip; | |
1882 | kvm_run->debug.arch.exception = BP_VECTOR; | |
1883 | return 0; | |
1884 | } | |
1885 | ||
851ba692 | 1886 | static int ud_interception(struct vcpu_svm *svm) |
7aa81cc0 AL |
1887 | { |
1888 | int er; | |
1889 | ||
51d8b661 | 1890 | er = emulate_instruction(&svm->vcpu, EMULTYPE_TRAP_UD); |
7aa81cc0 | 1891 | if (er != EMULATE_DONE) |
7ee5d940 | 1892 | kvm_queue_exception(&svm->vcpu, UD_VECTOR); |
7aa81cc0 AL |
1893 | return 1; |
1894 | } | |
1895 | ||
6b52d186 | 1896 | static void svm_fpu_activate(struct kvm_vcpu *vcpu) |
7807fa6c | 1897 | { |
6b52d186 | 1898 | struct vcpu_svm *svm = to_svm(vcpu); |
66a562f7 | 1899 | |
18c918c5 | 1900 | clr_exception_intercept(svm, NM_VECTOR); |
66a562f7 | 1901 | |
e756fc62 | 1902 | svm->vcpu.fpu_active = 1; |
d225157b | 1903 | update_cr0_intercept(svm); |
6b52d186 | 1904 | } |
a2fa3e9f | 1905 | |
6b52d186 AK |
1906 | static int nm_interception(struct vcpu_svm *svm) |
1907 | { | |
1908 | svm_fpu_activate(&svm->vcpu); | |
a2fa3e9f | 1909 | return 1; |
7807fa6c AL |
1910 | } |
1911 | ||
67ec6607 JR |
1912 | static bool is_erratum_383(void) |
1913 | { | |
1914 | int err, i; | |
1915 | u64 value; | |
1916 | ||
1917 | if (!erratum_383_found) | |
1918 | return false; | |
1919 | ||
1920 | value = native_read_msr_safe(MSR_IA32_MC0_STATUS, &err); | |
1921 | if (err) | |
1922 | return false; | |
1923 | ||
1924 | /* Bit 62 may or may not be set for this mce */ | |
1925 | value &= ~(1ULL << 62); | |
1926 | ||
1927 | if (value != 0xb600000000010015ULL) | |
1928 | return false; | |
1929 | ||
1930 | /* Clear MCi_STATUS registers */ | |
1931 | for (i = 0; i < 6; ++i) | |
1932 | native_write_msr_safe(MSR_IA32_MCx_STATUS(i), 0, 0); | |
1933 | ||
1934 | value = native_read_msr_safe(MSR_IA32_MCG_STATUS, &err); | |
1935 | if (!err) { | |
1936 | u32 low, high; | |
1937 | ||
1938 | value &= ~(1ULL << 2); | |
1939 | low = lower_32_bits(value); | |
1940 | high = upper_32_bits(value); | |
1941 | ||
1942 | native_write_msr_safe(MSR_IA32_MCG_STATUS, low, high); | |
1943 | } | |
1944 | ||
1945 | /* Flush tlb to evict multi-match entries */ | |
1946 | __flush_tlb_all(); | |
1947 | ||
1948 | return true; | |
1949 | } | |
1950 | ||
fe5913e4 | 1951 | static void svm_handle_mce(struct vcpu_svm *svm) |
53371b50 | 1952 | { |
67ec6607 JR |
1953 | if (is_erratum_383()) { |
1954 | /* | |
1955 | * Erratum 383 triggered. Guest state is corrupt so kill the | |
1956 | * guest. | |
1957 | */ | |
1958 | pr_err("KVM: Guest triggered AMD Erratum 383\n"); | |
1959 | ||
a8eeb04a | 1960 | kvm_make_request(KVM_REQ_TRIPLE_FAULT, &svm->vcpu); |
67ec6607 JR |
1961 | |
1962 | return; | |
1963 | } | |
1964 | ||
53371b50 JR |
1965 | /* |
1966 | * On an #MC intercept the MCE handler is not called automatically in | |
1967 | * the host. So do it by hand here. | |
1968 | */ | |
1969 | asm volatile ( | |
1970 | "int $0x12\n"); | |
1971 | /* not sure if we ever come back to this point */ | |
1972 | ||
fe5913e4 JR |
1973 | return; |
1974 | } | |
1975 | ||
1976 | static int mc_interception(struct vcpu_svm *svm) | |
1977 | { | |
53371b50 JR |
1978 | return 1; |
1979 | } | |
1980 | ||
851ba692 | 1981 | static int shutdown_interception(struct vcpu_svm *svm) |
46fe4ddd | 1982 | { |
851ba692 AK |
1983 | struct kvm_run *kvm_run = svm->vcpu.run; |
1984 | ||
46fe4ddd JR |
1985 | /* |
1986 | * VMCB is undefined after a SHUTDOWN intercept | |
1987 | * so reinitialize it. | |
1988 | */ | |
a2fa3e9f | 1989 | clear_page(svm->vmcb); |
d28bc9dd | 1990 | init_vmcb(svm, false); |
46fe4ddd JR |
1991 | |
1992 | kvm_run->exit_reason = KVM_EXIT_SHUTDOWN; | |
1993 | return 0; | |
1994 | } | |
1995 | ||
851ba692 | 1996 | static int io_interception(struct vcpu_svm *svm) |
6aa8b732 | 1997 | { |
cf8f70bf | 1998 | struct kvm_vcpu *vcpu = &svm->vcpu; |
d77c26fc | 1999 | u32 io_info = svm->vmcb->control.exit_info_1; /* address size bug? */ |
34c33d16 | 2000 | int size, in, string; |
039576c0 | 2001 | unsigned port; |
6aa8b732 | 2002 | |
e756fc62 | 2003 | ++svm->vcpu.stat.io_exits; |
e70669ab | 2004 | string = (io_info & SVM_IOIO_STR_MASK) != 0; |
039576c0 | 2005 | in = (io_info & SVM_IOIO_TYPE_MASK) != 0; |
cf8f70bf | 2006 | if (string || in) |
51d8b661 | 2007 | return emulate_instruction(vcpu, 0) == EMULATE_DONE; |
cf8f70bf | 2008 | |
039576c0 AK |
2009 | port = io_info >> 16; |
2010 | size = (io_info & SVM_IOIO_SIZE_MASK) >> SVM_IOIO_SIZE_SHIFT; | |
cf8f70bf | 2011 | svm->next_rip = svm->vmcb->control.exit_info_2; |
e93f36bc | 2012 | skip_emulated_instruction(&svm->vcpu); |
cf8f70bf GN |
2013 | |
2014 | return kvm_fast_pio_out(vcpu, size, port); | |
6aa8b732 AK |
2015 | } |
2016 | ||
851ba692 | 2017 | static int nmi_interception(struct vcpu_svm *svm) |
c47f098d JR |
2018 | { |
2019 | return 1; | |
2020 | } | |
2021 | ||
851ba692 | 2022 | static int intr_interception(struct vcpu_svm *svm) |
a0698055 JR |
2023 | { |
2024 | ++svm->vcpu.stat.irq_exits; | |
2025 | return 1; | |
2026 | } | |
2027 | ||
851ba692 | 2028 | static int nop_on_interception(struct vcpu_svm *svm) |
6aa8b732 AK |
2029 | { |
2030 | return 1; | |
2031 | } | |
2032 | ||
851ba692 | 2033 | static int halt_interception(struct vcpu_svm *svm) |
6aa8b732 | 2034 | { |
5fdbf976 | 2035 | svm->next_rip = kvm_rip_read(&svm->vcpu) + 1; |
e756fc62 | 2036 | return kvm_emulate_halt(&svm->vcpu); |
6aa8b732 AK |
2037 | } |
2038 | ||
851ba692 | 2039 | static int vmmcall_interception(struct vcpu_svm *svm) |
02e235bc | 2040 | { |
5fdbf976 | 2041 | svm->next_rip = kvm_rip_read(&svm->vcpu) + 3; |
7aa81cc0 AL |
2042 | kvm_emulate_hypercall(&svm->vcpu); |
2043 | return 1; | |
02e235bc AK |
2044 | } |
2045 | ||
5bd2edc3 JR |
2046 | static unsigned long nested_svm_get_tdp_cr3(struct kvm_vcpu *vcpu) |
2047 | { | |
2048 | struct vcpu_svm *svm = to_svm(vcpu); | |
2049 | ||
2050 | return svm->nested.nested_cr3; | |
2051 | } | |
2052 | ||
e4e517b4 AK |
2053 | static u64 nested_svm_get_tdp_pdptr(struct kvm_vcpu *vcpu, int index) |
2054 | { | |
2055 | struct vcpu_svm *svm = to_svm(vcpu); | |
2056 | u64 cr3 = svm->nested.nested_cr3; | |
2057 | u64 pdpte; | |
2058 | int ret; | |
2059 | ||
54bf36aa PB |
2060 | ret = kvm_vcpu_read_guest_page(vcpu, gpa_to_gfn(cr3), &pdpte, |
2061 | offset_in_page(cr3) + index * 8, 8); | |
e4e517b4 AK |
2062 | if (ret) |
2063 | return 0; | |
2064 | return pdpte; | |
2065 | } | |
2066 | ||
5bd2edc3 JR |
2067 | static void nested_svm_set_tdp_cr3(struct kvm_vcpu *vcpu, |
2068 | unsigned long root) | |
2069 | { | |
2070 | struct vcpu_svm *svm = to_svm(vcpu); | |
2071 | ||
2072 | svm->vmcb->control.nested_cr3 = root; | |
b2747166 | 2073 | mark_dirty(svm->vmcb, VMCB_NPT); |
f40f6a45 | 2074 | svm_flush_tlb(vcpu); |
5bd2edc3 JR |
2075 | } |
2076 | ||
6389ee94 AK |
2077 | static void nested_svm_inject_npf_exit(struct kvm_vcpu *vcpu, |
2078 | struct x86_exception *fault) | |
5bd2edc3 JR |
2079 | { |
2080 | struct vcpu_svm *svm = to_svm(vcpu); | |
2081 | ||
5e352519 PB |
2082 | if (svm->vmcb->control.exit_code != SVM_EXIT_NPF) { |
2083 | /* | |
2084 | * TODO: track the cause of the nested page fault, and | |
2085 | * correctly fill in the high bits of exit_info_1. | |
2086 | */ | |
2087 | svm->vmcb->control.exit_code = SVM_EXIT_NPF; | |
2088 | svm->vmcb->control.exit_code_hi = 0; | |
2089 | svm->vmcb->control.exit_info_1 = (1ULL << 32); | |
2090 | svm->vmcb->control.exit_info_2 = fault->address; | |
2091 | } | |
2092 | ||
2093 | svm->vmcb->control.exit_info_1 &= ~0xffffffffULL; | |
2094 | svm->vmcb->control.exit_info_1 |= fault->error_code; | |
2095 | ||
2096 | /* | |
2097 | * The present bit is always zero for page structure faults on real | |
2098 | * hardware. | |
2099 | */ | |
2100 | if (svm->vmcb->control.exit_info_1 & (2ULL << 32)) | |
2101 | svm->vmcb->control.exit_info_1 &= ~1; | |
5bd2edc3 JR |
2102 | |
2103 | nested_svm_vmexit(svm); | |
2104 | } | |
2105 | ||
8a3c1a33 | 2106 | static void nested_svm_init_mmu_context(struct kvm_vcpu *vcpu) |
4b16184c | 2107 | { |
ad896af0 PB |
2108 | WARN_ON(mmu_is_nested(vcpu)); |
2109 | kvm_init_shadow_mmu(vcpu); | |
4b16184c JR |
2110 | vcpu->arch.mmu.set_cr3 = nested_svm_set_tdp_cr3; |
2111 | vcpu->arch.mmu.get_cr3 = nested_svm_get_tdp_cr3; | |
e4e517b4 | 2112 | vcpu->arch.mmu.get_pdptr = nested_svm_get_tdp_pdptr; |
4b16184c JR |
2113 | vcpu->arch.mmu.inject_page_fault = nested_svm_inject_npf_exit; |
2114 | vcpu->arch.mmu.shadow_root_level = get_npt_level(); | |
c258b62b | 2115 | reset_shadow_zero_bits_mask(vcpu, &vcpu->arch.mmu); |
4b16184c | 2116 | vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu; |
4b16184c JR |
2117 | } |
2118 | ||
2119 | static void nested_svm_uninit_mmu_context(struct kvm_vcpu *vcpu) | |
2120 | { | |
2121 | vcpu->arch.walk_mmu = &vcpu->arch.mmu; | |
2122 | } | |
2123 | ||
c0725420 AG |
2124 | static int nested_svm_check_permissions(struct vcpu_svm *svm) |
2125 | { | |
f6801dff | 2126 | if (!(svm->vcpu.arch.efer & EFER_SVME) |
c0725420 AG |
2127 | || !is_paging(&svm->vcpu)) { |
2128 | kvm_queue_exception(&svm->vcpu, UD_VECTOR); | |
2129 | return 1; | |
2130 | } | |
2131 | ||
2132 | if (svm->vmcb->save.cpl) { | |
2133 | kvm_inject_gp(&svm->vcpu, 0); | |
2134 | return 1; | |
2135 | } | |
2136 | ||
2137 | return 0; | |
2138 | } | |
2139 | ||
cf74a78b AG |
2140 | static int nested_svm_check_exception(struct vcpu_svm *svm, unsigned nr, |
2141 | bool has_error_code, u32 error_code) | |
2142 | { | |
b8e88bc8 JR |
2143 | int vmexit; |
2144 | ||
2030753d | 2145 | if (!is_guest_mode(&svm->vcpu)) |
0295ad7d | 2146 | return 0; |
cf74a78b | 2147 | |
0295ad7d JR |
2148 | svm->vmcb->control.exit_code = SVM_EXIT_EXCP_BASE + nr; |
2149 | svm->vmcb->control.exit_code_hi = 0; | |
2150 | svm->vmcb->control.exit_info_1 = error_code; | |
2151 | svm->vmcb->control.exit_info_2 = svm->vcpu.arch.cr2; | |
2152 | ||
b8e88bc8 JR |
2153 | vmexit = nested_svm_intercept(svm); |
2154 | if (vmexit == NESTED_EXIT_DONE) | |
2155 | svm->nested.exit_required = true; | |
2156 | ||
2157 | return vmexit; | |
cf74a78b AG |
2158 | } |
2159 | ||
8fe54654 JR |
2160 | /* This function returns true if it is save to enable the irq window */ |
2161 | static inline bool nested_svm_intr(struct vcpu_svm *svm) | |
cf74a78b | 2162 | { |
2030753d | 2163 | if (!is_guest_mode(&svm->vcpu)) |
8fe54654 | 2164 | return true; |
cf74a78b | 2165 | |
26666957 | 2166 | if (!(svm->vcpu.arch.hflags & HF_VINTR_MASK)) |
8fe54654 | 2167 | return true; |
cf74a78b | 2168 | |
26666957 | 2169 | if (!(svm->vcpu.arch.hflags & HF_HIF_MASK)) |
8fe54654 | 2170 | return false; |
cf74a78b | 2171 | |
a0a07cd2 GN |
2172 | /* |
2173 | * if vmexit was already requested (by intercepted exception | |
2174 | * for instance) do not overwrite it with "external interrupt" | |
2175 | * vmexit. | |
2176 | */ | |
2177 | if (svm->nested.exit_required) | |
2178 | return false; | |
2179 | ||
197717d5 JR |
2180 | svm->vmcb->control.exit_code = SVM_EXIT_INTR; |
2181 | svm->vmcb->control.exit_info_1 = 0; | |
2182 | svm->vmcb->control.exit_info_2 = 0; | |
26666957 | 2183 | |
cd3ff653 JR |
2184 | if (svm->nested.intercept & 1ULL) { |
2185 | /* | |
2186 | * The #vmexit can't be emulated here directly because this | |
c5ec2e56 | 2187 | * code path runs with irqs and preemption disabled. A |
cd3ff653 JR |
2188 | * #vmexit emulation might sleep. Only signal request for |
2189 | * the #vmexit here. | |
2190 | */ | |
2191 | svm->nested.exit_required = true; | |
236649de | 2192 | trace_kvm_nested_intr_vmexit(svm->vmcb->save.rip); |
8fe54654 | 2193 | return false; |
cf74a78b AG |
2194 | } |
2195 | ||
8fe54654 | 2196 | return true; |
cf74a78b AG |
2197 | } |
2198 | ||
887f500c JR |
2199 | /* This function returns true if it is save to enable the nmi window */ |
2200 | static inline bool nested_svm_nmi(struct vcpu_svm *svm) | |
2201 | { | |
2030753d | 2202 | if (!is_guest_mode(&svm->vcpu)) |
887f500c JR |
2203 | return true; |
2204 | ||
2205 | if (!(svm->nested.intercept & (1ULL << INTERCEPT_NMI))) | |
2206 | return true; | |
2207 | ||
2208 | svm->vmcb->control.exit_code = SVM_EXIT_NMI; | |
2209 | svm->nested.exit_required = true; | |
2210 | ||
2211 | return false; | |
cf74a78b AG |
2212 | } |
2213 | ||
7597f129 | 2214 | static void *nested_svm_map(struct vcpu_svm *svm, u64 gpa, struct page **_page) |
34f80cfa JR |
2215 | { |
2216 | struct page *page; | |
2217 | ||
6c3bd3d7 JR |
2218 | might_sleep(); |
2219 | ||
54bf36aa | 2220 | page = kvm_vcpu_gfn_to_page(&svm->vcpu, gpa >> PAGE_SHIFT); |
34f80cfa JR |
2221 | if (is_error_page(page)) |
2222 | goto error; | |
2223 | ||
7597f129 JR |
2224 | *_page = page; |
2225 | ||
2226 | return kmap(page); | |
34f80cfa JR |
2227 | |
2228 | error: | |
34f80cfa JR |
2229 | kvm_inject_gp(&svm->vcpu, 0); |
2230 | ||
2231 | return NULL; | |
2232 | } | |
2233 | ||
7597f129 | 2234 | static void nested_svm_unmap(struct page *page) |
34f80cfa | 2235 | { |
7597f129 | 2236 | kunmap(page); |
34f80cfa JR |
2237 | kvm_release_page_dirty(page); |
2238 | } | |
34f80cfa | 2239 | |
ce2ac085 JR |
2240 | static int nested_svm_intercept_ioio(struct vcpu_svm *svm) |
2241 | { | |
9bf41833 JK |
2242 | unsigned port, size, iopm_len; |
2243 | u16 val, mask; | |
2244 | u8 start_bit; | |
ce2ac085 | 2245 | u64 gpa; |
34f80cfa | 2246 | |
ce2ac085 JR |
2247 | if (!(svm->nested.intercept & (1ULL << INTERCEPT_IOIO_PROT))) |
2248 | return NESTED_EXIT_HOST; | |
34f80cfa | 2249 | |
ce2ac085 | 2250 | port = svm->vmcb->control.exit_info_1 >> 16; |
9bf41833 JK |
2251 | size = (svm->vmcb->control.exit_info_1 & SVM_IOIO_SIZE_MASK) >> |
2252 | SVM_IOIO_SIZE_SHIFT; | |
ce2ac085 | 2253 | gpa = svm->nested.vmcb_iopm + (port / 8); |
9bf41833 JK |
2254 | start_bit = port % 8; |
2255 | iopm_len = (start_bit + size > 8) ? 2 : 1; | |
2256 | mask = (0xf >> (4 - size)) << start_bit; | |
2257 | val = 0; | |
ce2ac085 | 2258 | |
54bf36aa | 2259 | if (kvm_vcpu_read_guest(&svm->vcpu, gpa, &val, iopm_len)) |
9bf41833 | 2260 | return NESTED_EXIT_DONE; |
ce2ac085 | 2261 | |
9bf41833 | 2262 | return (val & mask) ? NESTED_EXIT_DONE : NESTED_EXIT_HOST; |
34f80cfa JR |
2263 | } |
2264 | ||
d2477826 | 2265 | static int nested_svm_exit_handled_msr(struct vcpu_svm *svm) |
4c2161ae | 2266 | { |
0d6b3537 JR |
2267 | u32 offset, msr, value; |
2268 | int write, mask; | |
4c2161ae | 2269 | |
3d62d9aa | 2270 | if (!(svm->nested.intercept & (1ULL << INTERCEPT_MSR_PROT))) |
d2477826 | 2271 | return NESTED_EXIT_HOST; |
3d62d9aa | 2272 | |
0d6b3537 JR |
2273 | msr = svm->vcpu.arch.regs[VCPU_REGS_RCX]; |
2274 | offset = svm_msrpm_offset(msr); | |
2275 | write = svm->vmcb->control.exit_info_1 & 1; | |
2276 | mask = 1 << ((2 * (msr & 0xf)) + write); | |
3d62d9aa | 2277 | |
0d6b3537 JR |
2278 | if (offset == MSR_INVALID) |
2279 | return NESTED_EXIT_DONE; | |
4c2161ae | 2280 | |
0d6b3537 JR |
2281 | /* Offset is in 32 bit units but need in 8 bit units */ |
2282 | offset *= 4; | |
4c2161ae | 2283 | |
54bf36aa | 2284 | if (kvm_vcpu_read_guest(&svm->vcpu, svm->nested.vmcb_msrpm + offset, &value, 4)) |
0d6b3537 | 2285 | return NESTED_EXIT_DONE; |
3d62d9aa | 2286 | |
0d6b3537 | 2287 | return (value & mask) ? NESTED_EXIT_DONE : NESTED_EXIT_HOST; |
4c2161ae JR |
2288 | } |
2289 | ||
410e4d57 | 2290 | static int nested_svm_exit_special(struct vcpu_svm *svm) |
cf74a78b | 2291 | { |
cf74a78b | 2292 | u32 exit_code = svm->vmcb->control.exit_code; |
4c2161ae | 2293 | |
410e4d57 JR |
2294 | switch (exit_code) { |
2295 | case SVM_EXIT_INTR: | |
2296 | case SVM_EXIT_NMI: | |
ff47a49b | 2297 | case SVM_EXIT_EXCP_BASE + MC_VECTOR: |
410e4d57 | 2298 | return NESTED_EXIT_HOST; |
410e4d57 | 2299 | case SVM_EXIT_NPF: |
e0231715 | 2300 | /* For now we are always handling NPFs when using them */ |
410e4d57 JR |
2301 | if (npt_enabled) |
2302 | return NESTED_EXIT_HOST; | |
2303 | break; | |
410e4d57 | 2304 | case SVM_EXIT_EXCP_BASE + PF_VECTOR: |
631bc487 GN |
2305 | /* When we're shadowing, trap PFs, but not async PF */ |
2306 | if (!npt_enabled && svm->apf_reason == 0) | |
410e4d57 JR |
2307 | return NESTED_EXIT_HOST; |
2308 | break; | |
66a562f7 JR |
2309 | case SVM_EXIT_EXCP_BASE + NM_VECTOR: |
2310 | nm_interception(svm); | |
2311 | break; | |
410e4d57 JR |
2312 | default: |
2313 | break; | |
cf74a78b AG |
2314 | } |
2315 | ||
410e4d57 JR |
2316 | return NESTED_EXIT_CONTINUE; |
2317 | } | |
2318 | ||
2319 | /* | |
2320 | * If this function returns true, this #vmexit was already handled | |
2321 | */ | |
b8e88bc8 | 2322 | static int nested_svm_intercept(struct vcpu_svm *svm) |
410e4d57 JR |
2323 | { |
2324 | u32 exit_code = svm->vmcb->control.exit_code; | |
2325 | int vmexit = NESTED_EXIT_HOST; | |
2326 | ||
cf74a78b | 2327 | switch (exit_code) { |
9c4e40b9 | 2328 | case SVM_EXIT_MSR: |
3d62d9aa | 2329 | vmexit = nested_svm_exit_handled_msr(svm); |
9c4e40b9 | 2330 | break; |
ce2ac085 JR |
2331 | case SVM_EXIT_IOIO: |
2332 | vmexit = nested_svm_intercept_ioio(svm); | |
2333 | break; | |
4ee546b4 RJ |
2334 | case SVM_EXIT_READ_CR0 ... SVM_EXIT_WRITE_CR8: { |
2335 | u32 bit = 1U << (exit_code - SVM_EXIT_READ_CR0); | |
2336 | if (svm->nested.intercept_cr & bit) | |
410e4d57 | 2337 | vmexit = NESTED_EXIT_DONE; |
cf74a78b AG |
2338 | break; |
2339 | } | |
3aed041a JR |
2340 | case SVM_EXIT_READ_DR0 ... SVM_EXIT_WRITE_DR7: { |
2341 | u32 bit = 1U << (exit_code - SVM_EXIT_READ_DR0); | |
2342 | if (svm->nested.intercept_dr & bit) | |
410e4d57 | 2343 | vmexit = NESTED_EXIT_DONE; |
cf74a78b AG |
2344 | break; |
2345 | } | |
2346 | case SVM_EXIT_EXCP_BASE ... SVM_EXIT_EXCP_BASE + 0x1f: { | |
2347 | u32 excp_bits = 1 << (exit_code - SVM_EXIT_EXCP_BASE); | |
aad42c64 | 2348 | if (svm->nested.intercept_exceptions & excp_bits) |
410e4d57 | 2349 | vmexit = NESTED_EXIT_DONE; |
631bc487 GN |
2350 | /* async page fault always cause vmexit */ |
2351 | else if ((exit_code == SVM_EXIT_EXCP_BASE + PF_VECTOR) && | |
2352 | svm->apf_reason != 0) | |
2353 | vmexit = NESTED_EXIT_DONE; | |
cf74a78b AG |
2354 | break; |
2355 | } | |
228070b1 JR |
2356 | case SVM_EXIT_ERR: { |
2357 | vmexit = NESTED_EXIT_DONE; | |
2358 | break; | |
2359 | } | |
cf74a78b AG |
2360 | default: { |
2361 | u64 exit_bits = 1ULL << (exit_code - SVM_EXIT_INTR); | |
aad42c64 | 2362 | if (svm->nested.intercept & exit_bits) |
410e4d57 | 2363 | vmexit = NESTED_EXIT_DONE; |
cf74a78b AG |
2364 | } |
2365 | } | |
2366 | ||
b8e88bc8 JR |
2367 | return vmexit; |
2368 | } | |
2369 | ||
2370 | static int nested_svm_exit_handled(struct vcpu_svm *svm) | |
2371 | { | |
2372 | int vmexit; | |
2373 | ||
2374 | vmexit = nested_svm_intercept(svm); | |
2375 | ||
2376 | if (vmexit == NESTED_EXIT_DONE) | |
9c4e40b9 | 2377 | nested_svm_vmexit(svm); |
9c4e40b9 JR |
2378 | |
2379 | return vmexit; | |
cf74a78b AG |
2380 | } |
2381 | ||
0460a979 JR |
2382 | static inline void copy_vmcb_control_area(struct vmcb *dst_vmcb, struct vmcb *from_vmcb) |
2383 | { | |
2384 | struct vmcb_control_area *dst = &dst_vmcb->control; | |
2385 | struct vmcb_control_area *from = &from_vmcb->control; | |
2386 | ||
4ee546b4 | 2387 | dst->intercept_cr = from->intercept_cr; |
3aed041a | 2388 | dst->intercept_dr = from->intercept_dr; |
0460a979 JR |
2389 | dst->intercept_exceptions = from->intercept_exceptions; |
2390 | dst->intercept = from->intercept; | |
2391 | dst->iopm_base_pa = from->iopm_base_pa; | |
2392 | dst->msrpm_base_pa = from->msrpm_base_pa; | |
2393 | dst->tsc_offset = from->tsc_offset; | |
2394 | dst->asid = from->asid; | |
2395 | dst->tlb_ctl = from->tlb_ctl; | |
2396 | dst->int_ctl = from->int_ctl; | |
2397 | dst->int_vector = from->int_vector; | |
2398 | dst->int_state = from->int_state; | |
2399 | dst->exit_code = from->exit_code; | |
2400 | dst->exit_code_hi = from->exit_code_hi; | |
2401 | dst->exit_info_1 = from->exit_info_1; | |
2402 | dst->exit_info_2 = from->exit_info_2; | |
2403 | dst->exit_int_info = from->exit_int_info; | |
2404 | dst->exit_int_info_err = from->exit_int_info_err; | |
2405 | dst->nested_ctl = from->nested_ctl; | |
2406 | dst->event_inj = from->event_inj; | |
2407 | dst->event_inj_err = from->event_inj_err; | |
2408 | dst->nested_cr3 = from->nested_cr3; | |
2409 | dst->lbr_ctl = from->lbr_ctl; | |
2410 | } | |
2411 | ||
34f80cfa | 2412 | static int nested_svm_vmexit(struct vcpu_svm *svm) |
cf74a78b | 2413 | { |
34f80cfa | 2414 | struct vmcb *nested_vmcb; |
e6aa9abd | 2415 | struct vmcb *hsave = svm->nested.hsave; |
33740e40 | 2416 | struct vmcb *vmcb = svm->vmcb; |
7597f129 | 2417 | struct page *page; |
cf74a78b | 2418 | |
17897f36 JR |
2419 | trace_kvm_nested_vmexit_inject(vmcb->control.exit_code, |
2420 | vmcb->control.exit_info_1, | |
2421 | vmcb->control.exit_info_2, | |
2422 | vmcb->control.exit_int_info, | |
e097e5ff SH |
2423 | vmcb->control.exit_int_info_err, |
2424 | KVM_ISA_SVM); | |
17897f36 | 2425 | |
7597f129 | 2426 | nested_vmcb = nested_svm_map(svm, svm->nested.vmcb, &page); |
34f80cfa JR |
2427 | if (!nested_vmcb) |
2428 | return 1; | |
2429 | ||
2030753d JR |
2430 | /* Exit Guest-Mode */ |
2431 | leave_guest_mode(&svm->vcpu); | |
06fc7772 JR |
2432 | svm->nested.vmcb = 0; |
2433 | ||
cf74a78b | 2434 | /* Give the current vmcb to the guest */ |
33740e40 JR |
2435 | disable_gif(svm); |
2436 | ||
2437 | nested_vmcb->save.es = vmcb->save.es; | |
2438 | nested_vmcb->save.cs = vmcb->save.cs; | |
2439 | nested_vmcb->save.ss = vmcb->save.ss; | |
2440 | nested_vmcb->save.ds = vmcb->save.ds; | |
2441 | nested_vmcb->save.gdtr = vmcb->save.gdtr; | |
2442 | nested_vmcb->save.idtr = vmcb->save.idtr; | |
3f6a9d16 | 2443 | nested_vmcb->save.efer = svm->vcpu.arch.efer; |
cdbbdc12 | 2444 | nested_vmcb->save.cr0 = kvm_read_cr0(&svm->vcpu); |
9f8fe504 | 2445 | nested_vmcb->save.cr3 = kvm_read_cr3(&svm->vcpu); |
33740e40 | 2446 | nested_vmcb->save.cr2 = vmcb->save.cr2; |
cdbbdc12 | 2447 | nested_vmcb->save.cr4 = svm->vcpu.arch.cr4; |
f6e78475 | 2448 | nested_vmcb->save.rflags = kvm_get_rflags(&svm->vcpu); |
33740e40 JR |
2449 | nested_vmcb->save.rip = vmcb->save.rip; |
2450 | nested_vmcb->save.rsp = vmcb->save.rsp; | |
2451 | nested_vmcb->save.rax = vmcb->save.rax; | |
2452 | nested_vmcb->save.dr7 = vmcb->save.dr7; | |
2453 | nested_vmcb->save.dr6 = vmcb->save.dr6; | |
2454 | nested_vmcb->save.cpl = vmcb->save.cpl; | |
2455 | ||
2456 | nested_vmcb->control.int_ctl = vmcb->control.int_ctl; | |
2457 | nested_vmcb->control.int_vector = vmcb->control.int_vector; | |
2458 | nested_vmcb->control.int_state = vmcb->control.int_state; | |
2459 | nested_vmcb->control.exit_code = vmcb->control.exit_code; | |
2460 | nested_vmcb->control.exit_code_hi = vmcb->control.exit_code_hi; | |
2461 | nested_vmcb->control.exit_info_1 = vmcb->control.exit_info_1; | |
2462 | nested_vmcb->control.exit_info_2 = vmcb->control.exit_info_2; | |
2463 | nested_vmcb->control.exit_int_info = vmcb->control.exit_int_info; | |
2464 | nested_vmcb->control.exit_int_info_err = vmcb->control.exit_int_info_err; | |
7a190667 | 2465 | nested_vmcb->control.next_rip = vmcb->control.next_rip; |
8d23c466 AG |
2466 | |
2467 | /* | |
2468 | * If we emulate a VMRUN/#VMEXIT in the same host #vmexit cycle we have | |
2469 | * to make sure that we do not lose injected events. So check event_inj | |
2470 | * here and copy it to exit_int_info if it is valid. | |
2471 | * Exit_int_info and event_inj can't be both valid because the case | |
2472 | * below only happens on a VMRUN instruction intercept which has | |
2473 | * no valid exit_int_info set. | |
2474 | */ | |
2475 | if (vmcb->control.event_inj & SVM_EVTINJ_VALID) { | |
2476 | struct vmcb_control_area *nc = &nested_vmcb->control; | |
2477 | ||
2478 | nc->exit_int_info = vmcb->control.event_inj; | |
2479 | nc->exit_int_info_err = vmcb->control.event_inj_err; | |
2480 | } | |
2481 | ||
33740e40 JR |
2482 | nested_vmcb->control.tlb_ctl = 0; |
2483 | nested_vmcb->control.event_inj = 0; | |
2484 | nested_vmcb->control.event_inj_err = 0; | |
cf74a78b AG |
2485 | |
2486 | /* We always set V_INTR_MASKING and remember the old value in hflags */ | |
2487 | if (!(svm->vcpu.arch.hflags & HF_VINTR_MASK)) | |
2488 | nested_vmcb->control.int_ctl &= ~V_INTR_MASKING_MASK; | |
2489 | ||
cf74a78b | 2490 | /* Restore the original control entries */ |
0460a979 | 2491 | copy_vmcb_control_area(vmcb, hsave); |
cf74a78b | 2492 | |
219b65dc AG |
2493 | kvm_clear_exception_queue(&svm->vcpu); |
2494 | kvm_clear_interrupt_queue(&svm->vcpu); | |
cf74a78b | 2495 | |
4b16184c JR |
2496 | svm->nested.nested_cr3 = 0; |
2497 | ||
cf74a78b AG |
2498 | /* Restore selected save entries */ |
2499 | svm->vmcb->save.es = hsave->save.es; | |
2500 | svm->vmcb->save.cs = hsave->save.cs; | |
2501 | svm->vmcb->save.ss = hsave->save.ss; | |
2502 | svm->vmcb->save.ds = hsave->save.ds; | |
2503 | svm->vmcb->save.gdtr = hsave->save.gdtr; | |
2504 | svm->vmcb->save.idtr = hsave->save.idtr; | |
f6e78475 | 2505 | kvm_set_rflags(&svm->vcpu, hsave->save.rflags); |
cf74a78b AG |
2506 | svm_set_efer(&svm->vcpu, hsave->save.efer); |
2507 | svm_set_cr0(&svm->vcpu, hsave->save.cr0 | X86_CR0_PE); | |
2508 | svm_set_cr4(&svm->vcpu, hsave->save.cr4); | |
2509 | if (npt_enabled) { | |
2510 | svm->vmcb->save.cr3 = hsave->save.cr3; | |
2511 | svm->vcpu.arch.cr3 = hsave->save.cr3; | |
2512 | } else { | |
2390218b | 2513 | (void)kvm_set_cr3(&svm->vcpu, hsave->save.cr3); |
cf74a78b AG |
2514 | } |
2515 | kvm_register_write(&svm->vcpu, VCPU_REGS_RAX, hsave->save.rax); | |
2516 | kvm_register_write(&svm->vcpu, VCPU_REGS_RSP, hsave->save.rsp); | |
2517 | kvm_register_write(&svm->vcpu, VCPU_REGS_RIP, hsave->save.rip); | |
2518 | svm->vmcb->save.dr7 = 0; | |
2519 | svm->vmcb->save.cpl = 0; | |
2520 | svm->vmcb->control.exit_int_info = 0; | |
2521 | ||
8d28fec4 RJ |
2522 | mark_all_dirty(svm->vmcb); |
2523 | ||
7597f129 | 2524 | nested_svm_unmap(page); |
cf74a78b | 2525 | |
4b16184c | 2526 | nested_svm_uninit_mmu_context(&svm->vcpu); |
cf74a78b AG |
2527 | kvm_mmu_reset_context(&svm->vcpu); |
2528 | kvm_mmu_load(&svm->vcpu); | |
2529 | ||
2530 | return 0; | |
2531 | } | |
3d6368ef | 2532 | |
9738b2c9 | 2533 | static bool nested_svm_vmrun_msrpm(struct vcpu_svm *svm) |
3d6368ef | 2534 | { |
323c3d80 JR |
2535 | /* |
2536 | * This function merges the msr permission bitmaps of kvm and the | |
c5ec2e56 | 2537 | * nested vmcb. It is optimized in that it only merges the parts where |
323c3d80 JR |
2538 | * the kvm msr permission bitmap may contain zero bits |
2539 | */ | |
3d6368ef | 2540 | int i; |
9738b2c9 | 2541 | |
323c3d80 JR |
2542 | if (!(svm->nested.intercept & (1ULL << INTERCEPT_MSR_PROT))) |
2543 | return true; | |
9738b2c9 | 2544 | |
323c3d80 JR |
2545 | for (i = 0; i < MSRPM_OFFSETS; i++) { |
2546 | u32 value, p; | |
2547 | u64 offset; | |
9738b2c9 | 2548 | |
323c3d80 JR |
2549 | if (msrpm_offsets[i] == 0xffffffff) |
2550 | break; | |
3d6368ef | 2551 | |
0d6b3537 JR |
2552 | p = msrpm_offsets[i]; |
2553 | offset = svm->nested.vmcb_msrpm + (p * 4); | |
323c3d80 | 2554 | |
54bf36aa | 2555 | if (kvm_vcpu_read_guest(&svm->vcpu, offset, &value, 4)) |
323c3d80 JR |
2556 | return false; |
2557 | ||
2558 | svm->nested.msrpm[p] = svm->msrpm[p] | value; | |
2559 | } | |
3d6368ef | 2560 | |
323c3d80 | 2561 | svm->vmcb->control.msrpm_base_pa = __pa(svm->nested.msrpm); |
9738b2c9 JR |
2562 | |
2563 | return true; | |
3d6368ef AG |
2564 | } |
2565 | ||
52c65a30 JR |
2566 | static bool nested_vmcb_checks(struct vmcb *vmcb) |
2567 | { | |
2568 | if ((vmcb->control.intercept & (1ULL << INTERCEPT_VMRUN)) == 0) | |
2569 | return false; | |
2570 | ||
dbe77584 JR |
2571 | if (vmcb->control.asid == 0) |
2572 | return false; | |
2573 | ||
4b16184c JR |
2574 | if (vmcb->control.nested_ctl && !npt_enabled) |
2575 | return false; | |
2576 | ||
52c65a30 JR |
2577 | return true; |
2578 | } | |
2579 | ||
9738b2c9 | 2580 | static bool nested_svm_vmrun(struct vcpu_svm *svm) |
3d6368ef | 2581 | { |
9738b2c9 | 2582 | struct vmcb *nested_vmcb; |
e6aa9abd | 2583 | struct vmcb *hsave = svm->nested.hsave; |
defbba56 | 2584 | struct vmcb *vmcb = svm->vmcb; |
7597f129 | 2585 | struct page *page; |
06fc7772 | 2586 | u64 vmcb_gpa; |
3d6368ef | 2587 | |
06fc7772 | 2588 | vmcb_gpa = svm->vmcb->save.rax; |
3d6368ef | 2589 | |
7597f129 | 2590 | nested_vmcb = nested_svm_map(svm, svm->vmcb->save.rax, &page); |
9738b2c9 JR |
2591 | if (!nested_vmcb) |
2592 | return false; | |
2593 | ||
52c65a30 JR |
2594 | if (!nested_vmcb_checks(nested_vmcb)) { |
2595 | nested_vmcb->control.exit_code = SVM_EXIT_ERR; | |
2596 | nested_vmcb->control.exit_code_hi = 0; | |
2597 | nested_vmcb->control.exit_info_1 = 0; | |
2598 | nested_vmcb->control.exit_info_2 = 0; | |
2599 | ||
2600 | nested_svm_unmap(page); | |
2601 | ||
2602 | return false; | |
2603 | } | |
2604 | ||
b75f4eb3 | 2605 | trace_kvm_nested_vmrun(svm->vmcb->save.rip, vmcb_gpa, |
0ac406de JR |
2606 | nested_vmcb->save.rip, |
2607 | nested_vmcb->control.int_ctl, | |
2608 | nested_vmcb->control.event_inj, | |
2609 | nested_vmcb->control.nested_ctl); | |
2610 | ||
4ee546b4 RJ |
2611 | trace_kvm_nested_intercepts(nested_vmcb->control.intercept_cr & 0xffff, |
2612 | nested_vmcb->control.intercept_cr >> 16, | |
2e554e8d JR |
2613 | nested_vmcb->control.intercept_exceptions, |
2614 | nested_vmcb->control.intercept); | |
2615 | ||
3d6368ef | 2616 | /* Clear internal status */ |
219b65dc AG |
2617 | kvm_clear_exception_queue(&svm->vcpu); |
2618 | kvm_clear_interrupt_queue(&svm->vcpu); | |
3d6368ef | 2619 | |
e0231715 JR |
2620 | /* |
2621 | * Save the old vmcb, so we don't need to pick what we save, but can | |
2622 | * restore everything when a VMEXIT occurs | |
2623 | */ | |
defbba56 JR |
2624 | hsave->save.es = vmcb->save.es; |
2625 | hsave->save.cs = vmcb->save.cs; | |
2626 | hsave->save.ss = vmcb->save.ss; | |
2627 | hsave->save.ds = vmcb->save.ds; | |
2628 | hsave->save.gdtr = vmcb->save.gdtr; | |
2629 | hsave->save.idtr = vmcb->save.idtr; | |
f6801dff | 2630 | hsave->save.efer = svm->vcpu.arch.efer; |
4d4ec087 | 2631 | hsave->save.cr0 = kvm_read_cr0(&svm->vcpu); |
defbba56 | 2632 | hsave->save.cr4 = svm->vcpu.arch.cr4; |
f6e78475 | 2633 | hsave->save.rflags = kvm_get_rflags(&svm->vcpu); |
b75f4eb3 | 2634 | hsave->save.rip = kvm_rip_read(&svm->vcpu); |
defbba56 JR |
2635 | hsave->save.rsp = vmcb->save.rsp; |
2636 | hsave->save.rax = vmcb->save.rax; | |
2637 | if (npt_enabled) | |
2638 | hsave->save.cr3 = vmcb->save.cr3; | |
2639 | else | |
9f8fe504 | 2640 | hsave->save.cr3 = kvm_read_cr3(&svm->vcpu); |
defbba56 | 2641 | |
0460a979 | 2642 | copy_vmcb_control_area(hsave, vmcb); |
3d6368ef | 2643 | |
f6e78475 | 2644 | if (kvm_get_rflags(&svm->vcpu) & X86_EFLAGS_IF) |
3d6368ef AG |
2645 | svm->vcpu.arch.hflags |= HF_HIF_MASK; |
2646 | else | |
2647 | svm->vcpu.arch.hflags &= ~HF_HIF_MASK; | |
2648 | ||
4b16184c JR |
2649 | if (nested_vmcb->control.nested_ctl) { |
2650 | kvm_mmu_unload(&svm->vcpu); | |
2651 | svm->nested.nested_cr3 = nested_vmcb->control.nested_cr3; | |
2652 | nested_svm_init_mmu_context(&svm->vcpu); | |
2653 | } | |
2654 | ||
3d6368ef AG |
2655 | /* Load the nested guest state */ |
2656 | svm->vmcb->save.es = nested_vmcb->save.es; | |
2657 | svm->vmcb->save.cs = nested_vmcb->save.cs; | |
2658 | svm->vmcb->save.ss = nested_vmcb->save.ss; | |
2659 | svm->vmcb->save.ds = nested_vmcb->save.ds; | |
2660 | svm->vmcb->save.gdtr = nested_vmcb->save.gdtr; | |
2661 | svm->vmcb->save.idtr = nested_vmcb->save.idtr; | |
f6e78475 | 2662 | kvm_set_rflags(&svm->vcpu, nested_vmcb->save.rflags); |
3d6368ef AG |
2663 | svm_set_efer(&svm->vcpu, nested_vmcb->save.efer); |
2664 | svm_set_cr0(&svm->vcpu, nested_vmcb->save.cr0); | |
2665 | svm_set_cr4(&svm->vcpu, nested_vmcb->save.cr4); | |
2666 | if (npt_enabled) { | |
2667 | svm->vmcb->save.cr3 = nested_vmcb->save.cr3; | |
2668 | svm->vcpu.arch.cr3 = nested_vmcb->save.cr3; | |
0e5cbe36 | 2669 | } else |
2390218b | 2670 | (void)kvm_set_cr3(&svm->vcpu, nested_vmcb->save.cr3); |
0e5cbe36 JR |
2671 | |
2672 | /* Guest paging mode is active - reset mmu */ | |
2673 | kvm_mmu_reset_context(&svm->vcpu); | |
2674 | ||
defbba56 | 2675 | svm->vmcb->save.cr2 = svm->vcpu.arch.cr2 = nested_vmcb->save.cr2; |
3d6368ef AG |
2676 | kvm_register_write(&svm->vcpu, VCPU_REGS_RAX, nested_vmcb->save.rax); |
2677 | kvm_register_write(&svm->vcpu, VCPU_REGS_RSP, nested_vmcb->save.rsp); | |
2678 | kvm_register_write(&svm->vcpu, VCPU_REGS_RIP, nested_vmcb->save.rip); | |
e0231715 | 2679 | |
3d6368ef AG |
2680 | /* In case we don't even reach vcpu_run, the fields are not updated */ |
2681 | svm->vmcb->save.rax = nested_vmcb->save.rax; | |
2682 | svm->vmcb->save.rsp = nested_vmcb->save.rsp; | |
2683 | svm->vmcb->save.rip = nested_vmcb->save.rip; | |
2684 | svm->vmcb->save.dr7 = nested_vmcb->save.dr7; | |
2685 | svm->vmcb->save.dr6 = nested_vmcb->save.dr6; | |
2686 | svm->vmcb->save.cpl = nested_vmcb->save.cpl; | |
2687 | ||
f7138538 | 2688 | svm->nested.vmcb_msrpm = nested_vmcb->control.msrpm_base_pa & ~0x0fffULL; |
ce2ac085 | 2689 | svm->nested.vmcb_iopm = nested_vmcb->control.iopm_base_pa & ~0x0fffULL; |
3d6368ef | 2690 | |
aad42c64 | 2691 | /* cache intercepts */ |
4ee546b4 | 2692 | svm->nested.intercept_cr = nested_vmcb->control.intercept_cr; |
3aed041a | 2693 | svm->nested.intercept_dr = nested_vmcb->control.intercept_dr; |
aad42c64 JR |
2694 | svm->nested.intercept_exceptions = nested_vmcb->control.intercept_exceptions; |
2695 | svm->nested.intercept = nested_vmcb->control.intercept; | |
2696 | ||
f40f6a45 | 2697 | svm_flush_tlb(&svm->vcpu); |
3d6368ef | 2698 | svm->vmcb->control.int_ctl = nested_vmcb->control.int_ctl | V_INTR_MASKING_MASK; |
3d6368ef AG |
2699 | if (nested_vmcb->control.int_ctl & V_INTR_MASKING_MASK) |
2700 | svm->vcpu.arch.hflags |= HF_VINTR_MASK; | |
2701 | else | |
2702 | svm->vcpu.arch.hflags &= ~HF_VINTR_MASK; | |
2703 | ||
88ab24ad JR |
2704 | if (svm->vcpu.arch.hflags & HF_VINTR_MASK) { |
2705 | /* We only want the cr8 intercept bits of the guest */ | |
4ee546b4 RJ |
2706 | clr_cr_intercept(svm, INTERCEPT_CR8_READ); |
2707 | clr_cr_intercept(svm, INTERCEPT_CR8_WRITE); | |
88ab24ad JR |
2708 | } |
2709 | ||
0d945bd9 | 2710 | /* We don't want to see VMMCALLs from a nested guest */ |
8a05a1b8 | 2711 | clr_intercept(svm, INTERCEPT_VMMCALL); |
0d945bd9 | 2712 | |
88ab24ad | 2713 | svm->vmcb->control.lbr_ctl = nested_vmcb->control.lbr_ctl; |
3d6368ef AG |
2714 | svm->vmcb->control.int_vector = nested_vmcb->control.int_vector; |
2715 | svm->vmcb->control.int_state = nested_vmcb->control.int_state; | |
2716 | svm->vmcb->control.tsc_offset += nested_vmcb->control.tsc_offset; | |
3d6368ef AG |
2717 | svm->vmcb->control.event_inj = nested_vmcb->control.event_inj; |
2718 | svm->vmcb->control.event_inj_err = nested_vmcb->control.event_inj_err; | |
2719 | ||
7597f129 | 2720 | nested_svm_unmap(page); |
9738b2c9 | 2721 | |
2030753d JR |
2722 | /* Enter Guest-Mode */ |
2723 | enter_guest_mode(&svm->vcpu); | |
2724 | ||
384c6368 JR |
2725 | /* |
2726 | * Merge guest and host intercepts - must be called with vcpu in | |
2727 | * guest-mode to take affect here | |
2728 | */ | |
2729 | recalc_intercepts(svm); | |
2730 | ||
06fc7772 | 2731 | svm->nested.vmcb = vmcb_gpa; |
9738b2c9 | 2732 | |
2af9194d | 2733 | enable_gif(svm); |
3d6368ef | 2734 | |
8d28fec4 RJ |
2735 | mark_all_dirty(svm->vmcb); |
2736 | ||
9738b2c9 | 2737 | return true; |
3d6368ef AG |
2738 | } |
2739 | ||
9966bf68 | 2740 | static void nested_svm_vmloadsave(struct vmcb *from_vmcb, struct vmcb *to_vmcb) |
5542675b AG |
2741 | { |
2742 | to_vmcb->save.fs = from_vmcb->save.fs; | |
2743 | to_vmcb->save.gs = from_vmcb->save.gs; | |
2744 | to_vmcb->save.tr = from_vmcb->save.tr; | |
2745 | to_vmcb->save.ldtr = from_vmcb->save.ldtr; | |
2746 | to_vmcb->save.kernel_gs_base = from_vmcb->save.kernel_gs_base; | |
2747 | to_vmcb->save.star = from_vmcb->save.star; | |
2748 | to_vmcb->save.lstar = from_vmcb->save.lstar; | |
2749 | to_vmcb->save.cstar = from_vmcb->save.cstar; | |
2750 | to_vmcb->save.sfmask = from_vmcb->save.sfmask; | |
2751 | to_vmcb->save.sysenter_cs = from_vmcb->save.sysenter_cs; | |
2752 | to_vmcb->save.sysenter_esp = from_vmcb->save.sysenter_esp; | |
2753 | to_vmcb->save.sysenter_eip = from_vmcb->save.sysenter_eip; | |
5542675b AG |
2754 | } |
2755 | ||
851ba692 | 2756 | static int vmload_interception(struct vcpu_svm *svm) |
5542675b | 2757 | { |
9966bf68 | 2758 | struct vmcb *nested_vmcb; |
7597f129 | 2759 | struct page *page; |
9966bf68 | 2760 | |
5542675b AG |
2761 | if (nested_svm_check_permissions(svm)) |
2762 | return 1; | |
2763 | ||
7597f129 | 2764 | nested_vmcb = nested_svm_map(svm, svm->vmcb->save.rax, &page); |
9966bf68 JR |
2765 | if (!nested_vmcb) |
2766 | return 1; | |
2767 | ||
e3e9ed3d JR |
2768 | svm->next_rip = kvm_rip_read(&svm->vcpu) + 3; |
2769 | skip_emulated_instruction(&svm->vcpu); | |
2770 | ||
9966bf68 | 2771 | nested_svm_vmloadsave(nested_vmcb, svm->vmcb); |
7597f129 | 2772 | nested_svm_unmap(page); |
5542675b AG |
2773 | |
2774 | return 1; | |
2775 | } | |
2776 | ||
851ba692 | 2777 | static int vmsave_interception(struct vcpu_svm *svm) |
5542675b | 2778 | { |
9966bf68 | 2779 | struct vmcb *nested_vmcb; |
7597f129 | 2780 | struct page *page; |
9966bf68 | 2781 | |
5542675b AG |
2782 | if (nested_svm_check_permissions(svm)) |
2783 | return 1; | |
2784 | ||
7597f129 | 2785 | nested_vmcb = nested_svm_map(svm, svm->vmcb->save.rax, &page); |
9966bf68 JR |
2786 | if (!nested_vmcb) |
2787 | return 1; | |
2788 | ||
e3e9ed3d JR |
2789 | svm->next_rip = kvm_rip_read(&svm->vcpu) + 3; |
2790 | skip_emulated_instruction(&svm->vcpu); | |
2791 | ||
9966bf68 | 2792 | nested_svm_vmloadsave(svm->vmcb, nested_vmcb); |
7597f129 | 2793 | nested_svm_unmap(page); |
5542675b AG |
2794 | |
2795 | return 1; | |
2796 | } | |
2797 | ||
851ba692 | 2798 | static int vmrun_interception(struct vcpu_svm *svm) |
3d6368ef | 2799 | { |
3d6368ef AG |
2800 | if (nested_svm_check_permissions(svm)) |
2801 | return 1; | |
2802 | ||
b75f4eb3 RJ |
2803 | /* Save rip after vmrun instruction */ |
2804 | kvm_rip_write(&svm->vcpu, kvm_rip_read(&svm->vcpu) + 3); | |
3d6368ef | 2805 | |
9738b2c9 | 2806 | if (!nested_svm_vmrun(svm)) |
3d6368ef AG |
2807 | return 1; |
2808 | ||
9738b2c9 | 2809 | if (!nested_svm_vmrun_msrpm(svm)) |
1f8da478 JR |
2810 | goto failed; |
2811 | ||
2812 | return 1; | |
2813 | ||
2814 | failed: | |
2815 | ||
2816 | svm->vmcb->control.exit_code = SVM_EXIT_ERR; | |
2817 | svm->vmcb->control.exit_code_hi = 0; | |
2818 | svm->vmcb->control.exit_info_1 = 0; | |
2819 | svm->vmcb->control.exit_info_2 = 0; | |
2820 | ||
2821 | nested_svm_vmexit(svm); | |
3d6368ef AG |
2822 | |
2823 | return 1; | |
2824 | } | |
2825 | ||
851ba692 | 2826 | static int stgi_interception(struct vcpu_svm *svm) |
1371d904 AG |
2827 | { |
2828 | if (nested_svm_check_permissions(svm)) | |
2829 | return 1; | |
2830 | ||
2831 | svm->next_rip = kvm_rip_read(&svm->vcpu) + 3; | |
2832 | skip_emulated_instruction(&svm->vcpu); | |
3842d135 | 2833 | kvm_make_request(KVM_REQ_EVENT, &svm->vcpu); |
1371d904 | 2834 | |
2af9194d | 2835 | enable_gif(svm); |
1371d904 AG |
2836 | |
2837 | return 1; | |
2838 | } | |
2839 | ||
851ba692 | 2840 | static int clgi_interception(struct vcpu_svm *svm) |
1371d904 AG |
2841 | { |
2842 | if (nested_svm_check_permissions(svm)) | |
2843 | return 1; | |
2844 | ||
2845 | svm->next_rip = kvm_rip_read(&svm->vcpu) + 3; | |
2846 | skip_emulated_instruction(&svm->vcpu); | |
2847 | ||
2af9194d | 2848 | disable_gif(svm); |
1371d904 AG |
2849 | |
2850 | /* After a CLGI no interrupts should come */ | |
2851 | svm_clear_vintr(svm); | |
2852 | svm->vmcb->control.int_ctl &= ~V_IRQ_MASK; | |
2853 | ||
decdbf6a JR |
2854 | mark_dirty(svm->vmcb, VMCB_INTR); |
2855 | ||
1371d904 AG |
2856 | return 1; |
2857 | } | |
2858 | ||
851ba692 | 2859 | static int invlpga_interception(struct vcpu_svm *svm) |
ff092385 AG |
2860 | { |
2861 | struct kvm_vcpu *vcpu = &svm->vcpu; | |
ff092385 | 2862 | |
668f198f DK |
2863 | trace_kvm_invlpga(svm->vmcb->save.rip, kvm_register_read(&svm->vcpu, VCPU_REGS_RCX), |
2864 | kvm_register_read(&svm->vcpu, VCPU_REGS_RAX)); | |
ec1ff790 | 2865 | |
ff092385 | 2866 | /* Let's treat INVLPGA the same as INVLPG (can be optimized!) */ |
668f198f | 2867 | kvm_mmu_invlpg(vcpu, kvm_register_read(&svm->vcpu, VCPU_REGS_RAX)); |
ff092385 AG |
2868 | |
2869 | svm->next_rip = kvm_rip_read(&svm->vcpu) + 3; | |
2870 | skip_emulated_instruction(&svm->vcpu); | |
2871 | return 1; | |
2872 | } | |
2873 | ||
532a46b9 JR |
2874 | static int skinit_interception(struct vcpu_svm *svm) |
2875 | { | |
668f198f | 2876 | trace_kvm_skinit(svm->vmcb->save.rip, kvm_register_read(&svm->vcpu, VCPU_REGS_RAX)); |
532a46b9 JR |
2877 | |
2878 | kvm_queue_exception(&svm->vcpu, UD_VECTOR); | |
2879 | return 1; | |
2880 | } | |
2881 | ||
dab429a7 DK |
2882 | static int wbinvd_interception(struct vcpu_svm *svm) |
2883 | { | |
2884 | kvm_emulate_wbinvd(&svm->vcpu); | |
2885 | return 1; | |
2886 | } | |
2887 | ||
81dd35d4 JR |
2888 | static int xsetbv_interception(struct vcpu_svm *svm) |
2889 | { | |
2890 | u64 new_bv = kvm_read_edx_eax(&svm->vcpu); | |
2891 | u32 index = kvm_register_read(&svm->vcpu, VCPU_REGS_RCX); | |
2892 | ||
2893 | if (kvm_set_xcr(&svm->vcpu, index, new_bv) == 0) { | |
2894 | svm->next_rip = kvm_rip_read(&svm->vcpu) + 3; | |
2895 | skip_emulated_instruction(&svm->vcpu); | |
2896 | } | |
2897 | ||
2898 | return 1; | |
2899 | } | |
2900 | ||
851ba692 | 2901 | static int task_switch_interception(struct vcpu_svm *svm) |
6aa8b732 | 2902 | { |
37817f29 | 2903 | u16 tss_selector; |
64a7ec06 GN |
2904 | int reason; |
2905 | int int_type = svm->vmcb->control.exit_int_info & | |
2906 | SVM_EXITINTINFO_TYPE_MASK; | |
8317c298 | 2907 | int int_vec = svm->vmcb->control.exit_int_info & SVM_EVTINJ_VEC_MASK; |
fe8e7f83 GN |
2908 | uint32_t type = |
2909 | svm->vmcb->control.exit_int_info & SVM_EXITINTINFO_TYPE_MASK; | |
2910 | uint32_t idt_v = | |
2911 | svm->vmcb->control.exit_int_info & SVM_EXITINTINFO_VALID; | |
e269fb21 JK |
2912 | bool has_error_code = false; |
2913 | u32 error_code = 0; | |
37817f29 IE |
2914 | |
2915 | tss_selector = (u16)svm->vmcb->control.exit_info_1; | |
64a7ec06 | 2916 | |
37817f29 IE |
2917 | if (svm->vmcb->control.exit_info_2 & |
2918 | (1ULL << SVM_EXITINFOSHIFT_TS_REASON_IRET)) | |
64a7ec06 GN |
2919 | reason = TASK_SWITCH_IRET; |
2920 | else if (svm->vmcb->control.exit_info_2 & | |
2921 | (1ULL << SVM_EXITINFOSHIFT_TS_REASON_JMP)) | |
2922 | reason = TASK_SWITCH_JMP; | |
fe8e7f83 | 2923 | else if (idt_v) |
64a7ec06 GN |
2924 | reason = TASK_SWITCH_GATE; |
2925 | else | |
2926 | reason = TASK_SWITCH_CALL; | |
2927 | ||
fe8e7f83 GN |
2928 | if (reason == TASK_SWITCH_GATE) { |
2929 | switch (type) { | |
2930 | case SVM_EXITINTINFO_TYPE_NMI: | |
2931 | svm->vcpu.arch.nmi_injected = false; | |
2932 | break; | |
2933 | case SVM_EXITINTINFO_TYPE_EXEPT: | |
e269fb21 JK |
2934 | if (svm->vmcb->control.exit_info_2 & |
2935 | (1ULL << SVM_EXITINFOSHIFT_TS_HAS_ERROR_CODE)) { | |
2936 | has_error_code = true; | |
2937 | error_code = | |
2938 | (u32)svm->vmcb->control.exit_info_2; | |
2939 | } | |
fe8e7f83 GN |
2940 | kvm_clear_exception_queue(&svm->vcpu); |
2941 | break; | |
2942 | case SVM_EXITINTINFO_TYPE_INTR: | |
2943 | kvm_clear_interrupt_queue(&svm->vcpu); | |
2944 | break; | |
2945 | default: | |
2946 | break; | |
2947 | } | |
2948 | } | |
64a7ec06 | 2949 | |
8317c298 GN |
2950 | if (reason != TASK_SWITCH_GATE || |
2951 | int_type == SVM_EXITINTINFO_TYPE_SOFT || | |
2952 | (int_type == SVM_EXITINTINFO_TYPE_EXEPT && | |
f629cf84 GN |
2953 | (int_vec == OF_VECTOR || int_vec == BP_VECTOR))) |
2954 | skip_emulated_instruction(&svm->vcpu); | |
64a7ec06 | 2955 | |
7f3d35fd KW |
2956 | if (int_type != SVM_EXITINTINFO_TYPE_SOFT) |
2957 | int_vec = -1; | |
2958 | ||
2959 | if (kvm_task_switch(&svm->vcpu, tss_selector, int_vec, reason, | |
acb54517 GN |
2960 | has_error_code, error_code) == EMULATE_FAIL) { |
2961 | svm->vcpu.run->exit_reason = KVM_EXIT_INTERNAL_ERROR; | |
2962 | svm->vcpu.run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION; | |
2963 | svm->vcpu.run->internal.ndata = 0; | |
2964 | return 0; | |
2965 | } | |
2966 | return 1; | |
6aa8b732 AK |
2967 | } |
2968 | ||
851ba692 | 2969 | static int cpuid_interception(struct vcpu_svm *svm) |
6aa8b732 | 2970 | { |
5fdbf976 | 2971 | svm->next_rip = kvm_rip_read(&svm->vcpu) + 2; |
e756fc62 | 2972 | kvm_emulate_cpuid(&svm->vcpu); |
06465c5a | 2973 | return 1; |
6aa8b732 AK |
2974 | } |
2975 | ||
851ba692 | 2976 | static int iret_interception(struct vcpu_svm *svm) |
95ba8273 GN |
2977 | { |
2978 | ++svm->vcpu.stat.nmi_window_exits; | |
8a05a1b8 | 2979 | clr_intercept(svm, INTERCEPT_IRET); |
44c11430 | 2980 | svm->vcpu.arch.hflags |= HF_IRET_MASK; |
bd3d1ec3 | 2981 | svm->nmi_iret_rip = kvm_rip_read(&svm->vcpu); |
f303b4ce | 2982 | kvm_make_request(KVM_REQ_EVENT, &svm->vcpu); |
95ba8273 GN |
2983 | return 1; |
2984 | } | |
2985 | ||
851ba692 | 2986 | static int invlpg_interception(struct vcpu_svm *svm) |
a7052897 | 2987 | { |
df4f3108 AP |
2988 | if (!static_cpu_has(X86_FEATURE_DECODEASSISTS)) |
2989 | return emulate_instruction(&svm->vcpu, 0) == EMULATE_DONE; | |
2990 | ||
2991 | kvm_mmu_invlpg(&svm->vcpu, svm->vmcb->control.exit_info_1); | |
2992 | skip_emulated_instruction(&svm->vcpu); | |
2993 | return 1; | |
a7052897 MT |
2994 | } |
2995 | ||
851ba692 | 2996 | static int emulate_on_interception(struct vcpu_svm *svm) |
6aa8b732 | 2997 | { |
51d8b661 | 2998 | return emulate_instruction(&svm->vcpu, 0) == EMULATE_DONE; |
6aa8b732 AK |
2999 | } |
3000 | ||
332b56e4 AK |
3001 | static int rdpmc_interception(struct vcpu_svm *svm) |
3002 | { | |
3003 | int err; | |
3004 | ||
3005 | if (!static_cpu_has(X86_FEATURE_NRIPS)) | |
3006 | return emulate_on_interception(svm); | |
3007 | ||
3008 | err = kvm_rdpmc(&svm->vcpu); | |
3009 | kvm_complete_insn_gp(&svm->vcpu, err); | |
3010 | ||
3011 | return 1; | |
3012 | } | |
3013 | ||
52eb5a6d XL |
3014 | static bool check_selective_cr0_intercepted(struct vcpu_svm *svm, |
3015 | unsigned long val) | |
628afd2a JR |
3016 | { |
3017 | unsigned long cr0 = svm->vcpu.arch.cr0; | |
3018 | bool ret = false; | |
3019 | u64 intercept; | |
3020 | ||
3021 | intercept = svm->nested.intercept; | |
3022 | ||
3023 | if (!is_guest_mode(&svm->vcpu) || | |
3024 | (!(intercept & (1ULL << INTERCEPT_SELECTIVE_CR0)))) | |
3025 | return false; | |
3026 | ||
3027 | cr0 &= ~SVM_CR0_SELECTIVE_MASK; | |
3028 | val &= ~SVM_CR0_SELECTIVE_MASK; | |
3029 | ||
3030 | if (cr0 ^ val) { | |
3031 | svm->vmcb->control.exit_code = SVM_EXIT_CR0_SEL_WRITE; | |
3032 | ret = (nested_svm_exit_handled(svm) == NESTED_EXIT_DONE); | |
3033 | } | |
3034 | ||
3035 | return ret; | |
3036 | } | |
3037 | ||
7ff76d58 AP |
3038 | #define CR_VALID (1ULL << 63) |
3039 | ||
3040 | static int cr_interception(struct vcpu_svm *svm) | |
3041 | { | |
3042 | int reg, cr; | |
3043 | unsigned long val; | |
3044 | int err; | |
3045 | ||
3046 | if (!static_cpu_has(X86_FEATURE_DECODEASSISTS)) | |
3047 | return emulate_on_interception(svm); | |
3048 | ||
3049 | if (unlikely((svm->vmcb->control.exit_info_1 & CR_VALID) == 0)) | |
3050 | return emulate_on_interception(svm); | |
3051 | ||
3052 | reg = svm->vmcb->control.exit_info_1 & SVM_EXITINFO_REG_MASK; | |
5e57518d DK |
3053 | if (svm->vmcb->control.exit_code == SVM_EXIT_CR0_SEL_WRITE) |
3054 | cr = SVM_EXIT_WRITE_CR0 - SVM_EXIT_READ_CR0; | |
3055 | else | |
3056 | cr = svm->vmcb->control.exit_code - SVM_EXIT_READ_CR0; | |
7ff76d58 AP |
3057 | |
3058 | err = 0; | |
3059 | if (cr >= 16) { /* mov to cr */ | |
3060 | cr -= 16; | |
3061 | val = kvm_register_read(&svm->vcpu, reg); | |
3062 | switch (cr) { | |
3063 | case 0: | |
628afd2a JR |
3064 | if (!check_selective_cr0_intercepted(svm, val)) |
3065 | err = kvm_set_cr0(&svm->vcpu, val); | |
977b2d03 JR |
3066 | else |
3067 | return 1; | |
3068 | ||
7ff76d58 AP |
3069 | break; |
3070 | case 3: | |
3071 | err = kvm_set_cr3(&svm->vcpu, val); | |
3072 | break; | |
3073 | case 4: | |
3074 | err = kvm_set_cr4(&svm->vcpu, val); | |
3075 | break; | |
3076 | case 8: | |
3077 | err = kvm_set_cr8(&svm->vcpu, val); | |
3078 | break; | |
3079 | default: | |
3080 | WARN(1, "unhandled write to CR%d", cr); | |
3081 | kvm_queue_exception(&svm->vcpu, UD_VECTOR); | |
3082 | return 1; | |
3083 | } | |
3084 | } else { /* mov from cr */ | |
3085 | switch (cr) { | |
3086 | case 0: | |
3087 | val = kvm_read_cr0(&svm->vcpu); | |
3088 | break; | |
3089 | case 2: | |
3090 | val = svm->vcpu.arch.cr2; | |
3091 | break; | |
3092 | case 3: | |
9f8fe504 | 3093 | val = kvm_read_cr3(&svm->vcpu); |
7ff76d58 AP |
3094 | break; |
3095 | case 4: | |
3096 | val = kvm_read_cr4(&svm->vcpu); | |
3097 | break; | |
3098 | case 8: | |
3099 | val = kvm_get_cr8(&svm->vcpu); | |
3100 | break; | |
3101 | default: | |
3102 | WARN(1, "unhandled read from CR%d", cr); | |
3103 | kvm_queue_exception(&svm->vcpu, UD_VECTOR); | |
3104 | return 1; | |
3105 | } | |
3106 | kvm_register_write(&svm->vcpu, reg, val); | |
3107 | } | |
3108 | kvm_complete_insn_gp(&svm->vcpu, err); | |
3109 | ||
3110 | return 1; | |
3111 | } | |
3112 | ||
cae3797a AP |
3113 | static int dr_interception(struct vcpu_svm *svm) |
3114 | { | |
3115 | int reg, dr; | |
3116 | unsigned long val; | |
cae3797a | 3117 | |
facb0139 PB |
3118 | if (svm->vcpu.guest_debug == 0) { |
3119 | /* | |
3120 | * No more DR vmexits; force a reload of the debug registers | |
3121 | * and reenter on this instruction. The next vmexit will | |
3122 | * retrieve the full state of the debug registers. | |
3123 | */ | |
3124 | clr_dr_intercepts(svm); | |
3125 | svm->vcpu.arch.switch_db_regs |= KVM_DEBUGREG_WONT_EXIT; | |
3126 | return 1; | |
3127 | } | |
3128 | ||
cae3797a AP |
3129 | if (!boot_cpu_has(X86_FEATURE_DECODEASSISTS)) |
3130 | return emulate_on_interception(svm); | |
3131 | ||
3132 | reg = svm->vmcb->control.exit_info_1 & SVM_EXITINFO_REG_MASK; | |
3133 | dr = svm->vmcb->control.exit_code - SVM_EXIT_READ_DR0; | |
3134 | ||
3135 | if (dr >= 16) { /* mov to DRn */ | |
16f8a6f9 NA |
3136 | if (!kvm_require_dr(&svm->vcpu, dr - 16)) |
3137 | return 1; | |
cae3797a AP |
3138 | val = kvm_register_read(&svm->vcpu, reg); |
3139 | kvm_set_dr(&svm->vcpu, dr - 16, val); | |
3140 | } else { | |
16f8a6f9 NA |
3141 | if (!kvm_require_dr(&svm->vcpu, dr)) |
3142 | return 1; | |
3143 | kvm_get_dr(&svm->vcpu, dr, &val); | |
3144 | kvm_register_write(&svm->vcpu, reg, val); | |
cae3797a AP |
3145 | } |
3146 | ||
2c46d2ae JR |
3147 | skip_emulated_instruction(&svm->vcpu); |
3148 | ||
cae3797a AP |
3149 | return 1; |
3150 | } | |
3151 | ||
851ba692 | 3152 | static int cr8_write_interception(struct vcpu_svm *svm) |
1d075434 | 3153 | { |
851ba692 | 3154 | struct kvm_run *kvm_run = svm->vcpu.run; |
eea1cff9 | 3155 | int r; |
851ba692 | 3156 | |
0a5fff19 GN |
3157 | u8 cr8_prev = kvm_get_cr8(&svm->vcpu); |
3158 | /* instruction emulation calls kvm_set_cr8() */ | |
7ff76d58 | 3159 | r = cr_interception(svm); |
596f3142 | 3160 | if (irqchip_in_kernel(svm->vcpu.kvm)) |
7ff76d58 | 3161 | return r; |
0a5fff19 | 3162 | if (cr8_prev <= kvm_get_cr8(&svm->vcpu)) |
7ff76d58 | 3163 | return r; |
1d075434 JR |
3164 | kvm_run->exit_reason = KVM_EXIT_SET_TPR; |
3165 | return 0; | |
3166 | } | |
3167 | ||
48d89b92 | 3168 | static u64 svm_read_l1_tsc(struct kvm_vcpu *vcpu, u64 host_tsc) |
d5c1785d NHE |
3169 | { |
3170 | struct vmcb *vmcb = get_host_vmcb(to_svm(vcpu)); | |
3171 | return vmcb->control.tsc_offset + | |
886b470c | 3172 | svm_scale_tsc(vcpu, host_tsc); |
d5c1785d NHE |
3173 | } |
3174 | ||
609e36d3 | 3175 | static int svm_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info) |
6aa8b732 | 3176 | { |
a2fa3e9f GH |
3177 | struct vcpu_svm *svm = to_svm(vcpu); |
3178 | ||
609e36d3 | 3179 | switch (msr_info->index) { |
af24a4e4 | 3180 | case MSR_IA32_TSC: { |
609e36d3 | 3181 | msr_info->data = svm->vmcb->control.tsc_offset + |
4ea1636b | 3182 | svm_scale_tsc(vcpu, rdtsc()); |
fbc0db76 | 3183 | |
6aa8b732 AK |
3184 | break; |
3185 | } | |
8c06585d | 3186 | case MSR_STAR: |
609e36d3 | 3187 | msr_info->data = svm->vmcb->save.star; |
6aa8b732 | 3188 | break; |
0e859cac | 3189 | #ifdef CONFIG_X86_64 |
6aa8b732 | 3190 | case MSR_LSTAR: |
609e36d3 | 3191 | msr_info->data = svm->vmcb->save.lstar; |
6aa8b732 AK |
3192 | break; |
3193 | case MSR_CSTAR: | |
609e36d3 | 3194 | msr_info->data = svm->vmcb->save.cstar; |
6aa8b732 AK |
3195 | break; |
3196 | case MSR_KERNEL_GS_BASE: | |
609e36d3 | 3197 | msr_info->data = svm->vmcb->save.kernel_gs_base; |
6aa8b732 AK |
3198 | break; |
3199 | case MSR_SYSCALL_MASK: | |
609e36d3 | 3200 | msr_info->data = svm->vmcb->save.sfmask; |
6aa8b732 AK |
3201 | break; |
3202 | #endif | |
3203 | case MSR_IA32_SYSENTER_CS: | |
609e36d3 | 3204 | msr_info->data = svm->vmcb->save.sysenter_cs; |
6aa8b732 AK |
3205 | break; |
3206 | case MSR_IA32_SYSENTER_EIP: | |
609e36d3 | 3207 | msr_info->data = svm->sysenter_eip; |
6aa8b732 AK |
3208 | break; |
3209 | case MSR_IA32_SYSENTER_ESP: | |
609e36d3 | 3210 | msr_info->data = svm->sysenter_esp; |
6aa8b732 | 3211 | break; |
e0231715 JR |
3212 | /* |
3213 | * Nobody will change the following 5 values in the VMCB so we can | |
3214 | * safely return them on rdmsr. They will always be 0 until LBRV is | |
3215 | * implemented. | |
3216 | */ | |
a2938c80 | 3217 | case MSR_IA32_DEBUGCTLMSR: |
609e36d3 | 3218 | msr_info->data = svm->vmcb->save.dbgctl; |
a2938c80 JR |
3219 | break; |
3220 | case MSR_IA32_LASTBRANCHFROMIP: | |
609e36d3 | 3221 | msr_info->data = svm->vmcb->save.br_from; |
a2938c80 JR |
3222 | break; |
3223 | case MSR_IA32_LASTBRANCHTOIP: | |
609e36d3 | 3224 | msr_info->data = svm->vmcb->save.br_to; |
a2938c80 JR |
3225 | break; |
3226 | case MSR_IA32_LASTINTFROMIP: | |
609e36d3 | 3227 | msr_info->data = svm->vmcb->save.last_excp_from; |
a2938c80 JR |
3228 | break; |
3229 | case MSR_IA32_LASTINTTOIP: | |
609e36d3 | 3230 | msr_info->data = svm->vmcb->save.last_excp_to; |
a2938c80 | 3231 | break; |
b286d5d8 | 3232 | case MSR_VM_HSAVE_PA: |
609e36d3 | 3233 | msr_info->data = svm->nested.hsave_msr; |
b286d5d8 | 3234 | break; |
eb6f302e | 3235 | case MSR_VM_CR: |
609e36d3 | 3236 | msr_info->data = svm->nested.vm_cr_msr; |
eb6f302e | 3237 | break; |
c8a73f18 | 3238 | case MSR_IA32_UCODE_REV: |
609e36d3 | 3239 | msr_info->data = 0x01000065; |
c8a73f18 | 3240 | break; |
6aa8b732 | 3241 | default: |
609e36d3 | 3242 | return kvm_get_msr_common(vcpu, msr_info); |
6aa8b732 AK |
3243 | } |
3244 | return 0; | |
3245 | } | |
3246 | ||
851ba692 | 3247 | static int rdmsr_interception(struct vcpu_svm *svm) |
6aa8b732 | 3248 | { |
668f198f | 3249 | u32 ecx = kvm_register_read(&svm->vcpu, VCPU_REGS_RCX); |
609e36d3 | 3250 | struct msr_data msr_info; |
6aa8b732 | 3251 | |
609e36d3 PB |
3252 | msr_info.index = ecx; |
3253 | msr_info.host_initiated = false; | |
3254 | if (svm_get_msr(&svm->vcpu, &msr_info)) { | |
59200273 | 3255 | trace_kvm_msr_read_ex(ecx); |
c1a5d4f9 | 3256 | kvm_inject_gp(&svm->vcpu, 0); |
59200273 | 3257 | } else { |
609e36d3 | 3258 | trace_kvm_msr_read(ecx, msr_info.data); |
af9ca2d7 | 3259 | |
609e36d3 PB |
3260 | kvm_register_write(&svm->vcpu, VCPU_REGS_RAX, |
3261 | msr_info.data & 0xffffffff); | |
3262 | kvm_register_write(&svm->vcpu, VCPU_REGS_RDX, | |
3263 | msr_info.data >> 32); | |
5fdbf976 | 3264 | svm->next_rip = kvm_rip_read(&svm->vcpu) + 2; |
e756fc62 | 3265 | skip_emulated_instruction(&svm->vcpu); |
6aa8b732 AK |
3266 | } |
3267 | return 1; | |
3268 | } | |
3269 | ||
4a810181 JR |
3270 | static int svm_set_vm_cr(struct kvm_vcpu *vcpu, u64 data) |
3271 | { | |
3272 | struct vcpu_svm *svm = to_svm(vcpu); | |
3273 | int svm_dis, chg_mask; | |
3274 | ||
3275 | if (data & ~SVM_VM_CR_VALID_MASK) | |
3276 | return 1; | |
3277 | ||
3278 | chg_mask = SVM_VM_CR_VALID_MASK; | |
3279 | ||
3280 | if (svm->nested.vm_cr_msr & SVM_VM_CR_SVM_DIS_MASK) | |
3281 | chg_mask &= ~(SVM_VM_CR_SVM_LOCK_MASK | SVM_VM_CR_SVM_DIS_MASK); | |
3282 | ||
3283 | svm->nested.vm_cr_msr &= ~chg_mask; | |
3284 | svm->nested.vm_cr_msr |= (data & chg_mask); | |
3285 | ||
3286 | svm_dis = svm->nested.vm_cr_msr & SVM_VM_CR_SVM_DIS_MASK; | |
3287 | ||
3288 | /* check for svm_disable while efer.svme is set */ | |
3289 | if (svm_dis && (vcpu->arch.efer & EFER_SVME)) | |
3290 | return 1; | |
3291 | ||
3292 | return 0; | |
3293 | } | |
3294 | ||
8fe8ab46 | 3295 | static int svm_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr) |
6aa8b732 | 3296 | { |
a2fa3e9f GH |
3297 | struct vcpu_svm *svm = to_svm(vcpu); |
3298 | ||
8fe8ab46 WA |
3299 | u32 ecx = msr->index; |
3300 | u64 data = msr->data; | |
6aa8b732 | 3301 | switch (ecx) { |
f4e1b3c8 | 3302 | case MSR_IA32_TSC: |
8fe8ab46 | 3303 | kvm_write_tsc(vcpu, msr); |
6aa8b732 | 3304 | break; |
8c06585d | 3305 | case MSR_STAR: |
a2fa3e9f | 3306 | svm->vmcb->save.star = data; |
6aa8b732 | 3307 | break; |
49b14f24 | 3308 | #ifdef CONFIG_X86_64 |
6aa8b732 | 3309 | case MSR_LSTAR: |
a2fa3e9f | 3310 | svm->vmcb->save.lstar = data; |
6aa8b732 AK |
3311 | break; |
3312 | case MSR_CSTAR: | |
a2fa3e9f | 3313 | svm->vmcb->save.cstar = data; |
6aa8b732 AK |
3314 | break; |
3315 | case MSR_KERNEL_GS_BASE: | |
a2fa3e9f | 3316 | svm->vmcb->save.kernel_gs_base = data; |
6aa8b732 AK |
3317 | break; |
3318 | case MSR_SYSCALL_MASK: | |
a2fa3e9f | 3319 | svm->vmcb->save.sfmask = data; |
6aa8b732 AK |
3320 | break; |
3321 | #endif | |
3322 | case MSR_IA32_SYSENTER_CS: | |
a2fa3e9f | 3323 | svm->vmcb->save.sysenter_cs = data; |
6aa8b732 AK |
3324 | break; |
3325 | case MSR_IA32_SYSENTER_EIP: | |
017cb99e | 3326 | svm->sysenter_eip = data; |
a2fa3e9f | 3327 | svm->vmcb->save.sysenter_eip = data; |
6aa8b732 AK |
3328 | break; |
3329 | case MSR_IA32_SYSENTER_ESP: | |
017cb99e | 3330 | svm->sysenter_esp = data; |
a2fa3e9f | 3331 | svm->vmcb->save.sysenter_esp = data; |
6aa8b732 | 3332 | break; |
a2938c80 | 3333 | case MSR_IA32_DEBUGCTLMSR: |
2a6b20b8 | 3334 | if (!boot_cpu_has(X86_FEATURE_LBRV)) { |
a737f256 CD |
3335 | vcpu_unimpl(vcpu, "%s: MSR_IA32_DEBUGCTL 0x%llx, nop\n", |
3336 | __func__, data); | |
24e09cbf JR |
3337 | break; |
3338 | } | |
3339 | if (data & DEBUGCTL_RESERVED_BITS) | |
3340 | return 1; | |
3341 | ||
3342 | svm->vmcb->save.dbgctl = data; | |
b53ba3f9 | 3343 | mark_dirty(svm->vmcb, VMCB_LBR); |
24e09cbf JR |
3344 | if (data & (1ULL<<0)) |
3345 | svm_enable_lbrv(svm); | |
3346 | else | |
3347 | svm_disable_lbrv(svm); | |
a2938c80 | 3348 | break; |
b286d5d8 | 3349 | case MSR_VM_HSAVE_PA: |
e6aa9abd | 3350 | svm->nested.hsave_msr = data; |
62b9abaa | 3351 | break; |
3c5d0a44 | 3352 | case MSR_VM_CR: |
4a810181 | 3353 | return svm_set_vm_cr(vcpu, data); |
3c5d0a44 | 3354 | case MSR_VM_IGNNE: |
a737f256 | 3355 | vcpu_unimpl(vcpu, "unimplemented wrmsr: 0x%x data 0x%llx\n", ecx, data); |
3c5d0a44 | 3356 | break; |
6aa8b732 | 3357 | default: |
8fe8ab46 | 3358 | return kvm_set_msr_common(vcpu, msr); |
6aa8b732 AK |
3359 | } |
3360 | return 0; | |
3361 | } | |
3362 | ||
851ba692 | 3363 | static int wrmsr_interception(struct vcpu_svm *svm) |
6aa8b732 | 3364 | { |
8fe8ab46 | 3365 | struct msr_data msr; |
668f198f DK |
3366 | u32 ecx = kvm_register_read(&svm->vcpu, VCPU_REGS_RCX); |
3367 | u64 data = kvm_read_edx_eax(&svm->vcpu); | |
af9ca2d7 | 3368 | |
8fe8ab46 WA |
3369 | msr.data = data; |
3370 | msr.index = ecx; | |
3371 | msr.host_initiated = false; | |
af9ca2d7 | 3372 | |
5fdbf976 | 3373 | svm->next_rip = kvm_rip_read(&svm->vcpu) + 2; |
854e8bb1 | 3374 | if (kvm_set_msr(&svm->vcpu, &msr)) { |
59200273 | 3375 | trace_kvm_msr_write_ex(ecx, data); |
c1a5d4f9 | 3376 | kvm_inject_gp(&svm->vcpu, 0); |
59200273 AK |
3377 | } else { |
3378 | trace_kvm_msr_write(ecx, data); | |
e756fc62 | 3379 | skip_emulated_instruction(&svm->vcpu); |
59200273 | 3380 | } |
6aa8b732 AK |
3381 | return 1; |
3382 | } | |
3383 | ||
851ba692 | 3384 | static int msr_interception(struct vcpu_svm *svm) |
6aa8b732 | 3385 | { |
e756fc62 | 3386 | if (svm->vmcb->control.exit_info_1) |
851ba692 | 3387 | return wrmsr_interception(svm); |
6aa8b732 | 3388 | else |
851ba692 | 3389 | return rdmsr_interception(svm); |
6aa8b732 AK |
3390 | } |
3391 | ||
851ba692 | 3392 | static int interrupt_window_interception(struct vcpu_svm *svm) |
c1150d8c | 3393 | { |
851ba692 AK |
3394 | struct kvm_run *kvm_run = svm->vcpu.run; |
3395 | ||
3842d135 | 3396 | kvm_make_request(KVM_REQ_EVENT, &svm->vcpu); |
f0b85051 | 3397 | svm_clear_vintr(svm); |
85f455f7 | 3398 | svm->vmcb->control.int_ctl &= ~V_IRQ_MASK; |
decdbf6a | 3399 | mark_dirty(svm->vmcb, VMCB_INTR); |
675acb75 | 3400 | ++svm->vcpu.stat.irq_window_exits; |
c1150d8c DL |
3401 | /* |
3402 | * If the user space waits to inject interrupts, exit as soon as | |
3403 | * possible | |
3404 | */ | |
8061823a GN |
3405 | if (!irqchip_in_kernel(svm->vcpu.kvm) && |
3406 | kvm_run->request_interrupt_window && | |
3407 | !kvm_cpu_has_interrupt(&svm->vcpu)) { | |
c1150d8c DL |
3408 | kvm_run->exit_reason = KVM_EXIT_IRQ_WINDOW_OPEN; |
3409 | return 0; | |
3410 | } | |
3411 | ||
3412 | return 1; | |
3413 | } | |
3414 | ||
565d0998 ML |
3415 | static int pause_interception(struct vcpu_svm *svm) |
3416 | { | |
3417 | kvm_vcpu_on_spin(&(svm->vcpu)); | |
3418 | return 1; | |
3419 | } | |
3420 | ||
87c00572 GS |
3421 | static int nop_interception(struct vcpu_svm *svm) |
3422 | { | |
3423 | skip_emulated_instruction(&(svm->vcpu)); | |
3424 | return 1; | |
3425 | } | |
3426 | ||
3427 | static int monitor_interception(struct vcpu_svm *svm) | |
3428 | { | |
3429 | printk_once(KERN_WARNING "kvm: MONITOR instruction emulated as NOP!\n"); | |
3430 | return nop_interception(svm); | |
3431 | } | |
3432 | ||
3433 | static int mwait_interception(struct vcpu_svm *svm) | |
3434 | { | |
3435 | printk_once(KERN_WARNING "kvm: MWAIT instruction emulated as NOP!\n"); | |
3436 | return nop_interception(svm); | |
3437 | } | |
3438 | ||
09941fbb | 3439 | static int (*const svm_exit_handlers[])(struct vcpu_svm *svm) = { |
7ff76d58 AP |
3440 | [SVM_EXIT_READ_CR0] = cr_interception, |
3441 | [SVM_EXIT_READ_CR3] = cr_interception, | |
3442 | [SVM_EXIT_READ_CR4] = cr_interception, | |
3443 | [SVM_EXIT_READ_CR8] = cr_interception, | |
5e57518d | 3444 | [SVM_EXIT_CR0_SEL_WRITE] = cr_interception, |
628afd2a | 3445 | [SVM_EXIT_WRITE_CR0] = cr_interception, |
7ff76d58 AP |
3446 | [SVM_EXIT_WRITE_CR3] = cr_interception, |
3447 | [SVM_EXIT_WRITE_CR4] = cr_interception, | |
e0231715 | 3448 | [SVM_EXIT_WRITE_CR8] = cr8_write_interception, |
cae3797a AP |
3449 | [SVM_EXIT_READ_DR0] = dr_interception, |
3450 | [SVM_EXIT_READ_DR1] = dr_interception, | |
3451 | [SVM_EXIT_READ_DR2] = dr_interception, | |
3452 | [SVM_EXIT_READ_DR3] = dr_interception, | |
3453 | [SVM_EXIT_READ_DR4] = dr_interception, | |
3454 | [SVM_EXIT_READ_DR5] = dr_interception, | |
3455 | [SVM_EXIT_READ_DR6] = dr_interception, | |
3456 | [SVM_EXIT_READ_DR7] = dr_interception, | |
3457 | [SVM_EXIT_WRITE_DR0] = dr_interception, | |
3458 | [SVM_EXIT_WRITE_DR1] = dr_interception, | |
3459 | [SVM_EXIT_WRITE_DR2] = dr_interception, | |
3460 | [SVM_EXIT_WRITE_DR3] = dr_interception, | |
3461 | [SVM_EXIT_WRITE_DR4] = dr_interception, | |
3462 | [SVM_EXIT_WRITE_DR5] = dr_interception, | |
3463 | [SVM_EXIT_WRITE_DR6] = dr_interception, | |
3464 | [SVM_EXIT_WRITE_DR7] = dr_interception, | |
d0bfb940 JK |
3465 | [SVM_EXIT_EXCP_BASE + DB_VECTOR] = db_interception, |
3466 | [SVM_EXIT_EXCP_BASE + BP_VECTOR] = bp_interception, | |
7aa81cc0 | 3467 | [SVM_EXIT_EXCP_BASE + UD_VECTOR] = ud_interception, |
e0231715 JR |
3468 | [SVM_EXIT_EXCP_BASE + PF_VECTOR] = pf_interception, |
3469 | [SVM_EXIT_EXCP_BASE + NM_VECTOR] = nm_interception, | |
3470 | [SVM_EXIT_EXCP_BASE + MC_VECTOR] = mc_interception, | |
3471 | [SVM_EXIT_INTR] = intr_interception, | |
c47f098d | 3472 | [SVM_EXIT_NMI] = nmi_interception, |
6aa8b732 AK |
3473 | [SVM_EXIT_SMI] = nop_on_interception, |
3474 | [SVM_EXIT_INIT] = nop_on_interception, | |
c1150d8c | 3475 | [SVM_EXIT_VINTR] = interrupt_window_interception, |
332b56e4 | 3476 | [SVM_EXIT_RDPMC] = rdpmc_interception, |
6aa8b732 | 3477 | [SVM_EXIT_CPUID] = cpuid_interception, |
95ba8273 | 3478 | [SVM_EXIT_IRET] = iret_interception, |
cf5a94d1 | 3479 | [SVM_EXIT_INVD] = emulate_on_interception, |
565d0998 | 3480 | [SVM_EXIT_PAUSE] = pause_interception, |
6aa8b732 | 3481 | [SVM_EXIT_HLT] = halt_interception, |
a7052897 | 3482 | [SVM_EXIT_INVLPG] = invlpg_interception, |
ff092385 | 3483 | [SVM_EXIT_INVLPGA] = invlpga_interception, |
e0231715 | 3484 | [SVM_EXIT_IOIO] = io_interception, |
6aa8b732 AK |
3485 | [SVM_EXIT_MSR] = msr_interception, |
3486 | [SVM_EXIT_TASK_SWITCH] = task_switch_interception, | |
46fe4ddd | 3487 | [SVM_EXIT_SHUTDOWN] = shutdown_interception, |
3d6368ef | 3488 | [SVM_EXIT_VMRUN] = vmrun_interception, |
02e235bc | 3489 | [SVM_EXIT_VMMCALL] = vmmcall_interception, |
5542675b AG |
3490 | [SVM_EXIT_VMLOAD] = vmload_interception, |
3491 | [SVM_EXIT_VMSAVE] = vmsave_interception, | |
1371d904 AG |
3492 | [SVM_EXIT_STGI] = stgi_interception, |
3493 | [SVM_EXIT_CLGI] = clgi_interception, | |
532a46b9 | 3494 | [SVM_EXIT_SKINIT] = skinit_interception, |
dab429a7 | 3495 | [SVM_EXIT_WBINVD] = wbinvd_interception, |
87c00572 GS |
3496 | [SVM_EXIT_MONITOR] = monitor_interception, |
3497 | [SVM_EXIT_MWAIT] = mwait_interception, | |
81dd35d4 | 3498 | [SVM_EXIT_XSETBV] = xsetbv_interception, |
709ddebf | 3499 | [SVM_EXIT_NPF] = pf_interception, |
64d60670 | 3500 | [SVM_EXIT_RSM] = emulate_on_interception, |
6aa8b732 AK |
3501 | }; |
3502 | ||
ae8cc059 | 3503 | static void dump_vmcb(struct kvm_vcpu *vcpu) |
3f10c846 JR |
3504 | { |
3505 | struct vcpu_svm *svm = to_svm(vcpu); | |
3506 | struct vmcb_control_area *control = &svm->vmcb->control; | |
3507 | struct vmcb_save_area *save = &svm->vmcb->save; | |
3508 | ||
3509 | pr_err("VMCB Control Area:\n"); | |
ae8cc059 JP |
3510 | pr_err("%-20s%04x\n", "cr_read:", control->intercept_cr & 0xffff); |
3511 | pr_err("%-20s%04x\n", "cr_write:", control->intercept_cr >> 16); | |
3512 | pr_err("%-20s%04x\n", "dr_read:", control->intercept_dr & 0xffff); | |
3513 | pr_err("%-20s%04x\n", "dr_write:", control->intercept_dr >> 16); | |
3514 | pr_err("%-20s%08x\n", "exceptions:", control->intercept_exceptions); | |
3515 | pr_err("%-20s%016llx\n", "intercepts:", control->intercept); | |
3516 | pr_err("%-20s%d\n", "pause filter count:", control->pause_filter_count); | |
3517 | pr_err("%-20s%016llx\n", "iopm_base_pa:", control->iopm_base_pa); | |
3518 | pr_err("%-20s%016llx\n", "msrpm_base_pa:", control->msrpm_base_pa); | |
3519 | pr_err("%-20s%016llx\n", "tsc_offset:", control->tsc_offset); | |
3520 | pr_err("%-20s%d\n", "asid:", control->asid); | |
3521 | pr_err("%-20s%d\n", "tlb_ctl:", control->tlb_ctl); | |
3522 | pr_err("%-20s%08x\n", "int_ctl:", control->int_ctl); | |
3523 | pr_err("%-20s%08x\n", "int_vector:", control->int_vector); | |
3524 | pr_err("%-20s%08x\n", "int_state:", control->int_state); | |
3525 | pr_err("%-20s%08x\n", "exit_code:", control->exit_code); | |
3526 | pr_err("%-20s%016llx\n", "exit_info1:", control->exit_info_1); | |
3527 | pr_err("%-20s%016llx\n", "exit_info2:", control->exit_info_2); | |
3528 | pr_err("%-20s%08x\n", "exit_int_info:", control->exit_int_info); | |
3529 | pr_err("%-20s%08x\n", "exit_int_info_err:", control->exit_int_info_err); | |
3530 | pr_err("%-20s%lld\n", "nested_ctl:", control->nested_ctl); | |
3531 | pr_err("%-20s%016llx\n", "nested_cr3:", control->nested_cr3); | |
3532 | pr_err("%-20s%08x\n", "event_inj:", control->event_inj); | |
3533 | pr_err("%-20s%08x\n", "event_inj_err:", control->event_inj_err); | |
3534 | pr_err("%-20s%lld\n", "lbr_ctl:", control->lbr_ctl); | |
3535 | pr_err("%-20s%016llx\n", "next_rip:", control->next_rip); | |
3f10c846 | 3536 | pr_err("VMCB State Save Area:\n"); |
ae8cc059 JP |
3537 | pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n", |
3538 | "es:", | |
3539 | save->es.selector, save->es.attrib, | |
3540 | save->es.limit, save->es.base); | |
3541 | pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n", | |
3542 | "cs:", | |
3543 | save->cs.selector, save->cs.attrib, | |
3544 | save->cs.limit, save->cs.base); | |
3545 | pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n", | |
3546 | "ss:", | |
3547 | save->ss.selector, save->ss.attrib, | |
3548 | save->ss.limit, save->ss.base); | |
3549 | pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n", | |
3550 | "ds:", | |
3551 | save->ds.selector, save->ds.attrib, | |
3552 | save->ds.limit, save->ds.base); | |
3553 | pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n", | |
3554 | "fs:", | |
3555 | save->fs.selector, save->fs.attrib, | |
3556 | save->fs.limit, save->fs.base); | |
3557 | pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n", | |
3558 | "gs:", | |
3559 | save->gs.selector, save->gs.attrib, | |
3560 | save->gs.limit, save->gs.base); | |
3561 | pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n", | |
3562 | "gdtr:", | |
3563 | save->gdtr.selector, save->gdtr.attrib, | |
3564 | save->gdtr.limit, save->gdtr.base); | |
3565 | pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n", | |
3566 | "ldtr:", | |
3567 | save->ldtr.selector, save->ldtr.attrib, | |
3568 | save->ldtr.limit, save->ldtr.base); | |
3569 | pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n", | |
3570 | "idtr:", | |
3571 | save->idtr.selector, save->idtr.attrib, | |
3572 | save->idtr.limit, save->idtr.base); | |
3573 | pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n", | |
3574 | "tr:", | |
3575 | save->tr.selector, save->tr.attrib, | |
3576 | save->tr.limit, save->tr.base); | |
3f10c846 JR |
3577 | pr_err("cpl: %d efer: %016llx\n", |
3578 | save->cpl, save->efer); | |
ae8cc059 JP |
3579 | pr_err("%-15s %016llx %-13s %016llx\n", |
3580 | "cr0:", save->cr0, "cr2:", save->cr2); | |
3581 | pr_err("%-15s %016llx %-13s %016llx\n", | |
3582 | "cr3:", save->cr3, "cr4:", save->cr4); | |
3583 | pr_err("%-15s %016llx %-13s %016llx\n", | |
3584 | "dr6:", save->dr6, "dr7:", save->dr7); | |
3585 | pr_err("%-15s %016llx %-13s %016llx\n", | |
3586 | "rip:", save->rip, "rflags:", save->rflags); | |
3587 | pr_err("%-15s %016llx %-13s %016llx\n", | |
3588 | "rsp:", save->rsp, "rax:", save->rax); | |
3589 | pr_err("%-15s %016llx %-13s %016llx\n", | |
3590 | "star:", save->star, "lstar:", save->lstar); | |
3591 | pr_err("%-15s %016llx %-13s %016llx\n", | |
3592 | "cstar:", save->cstar, "sfmask:", save->sfmask); | |
3593 | pr_err("%-15s %016llx %-13s %016llx\n", | |
3594 | "kernel_gs_base:", save->kernel_gs_base, | |
3595 | "sysenter_cs:", save->sysenter_cs); | |
3596 | pr_err("%-15s %016llx %-13s %016llx\n", | |
3597 | "sysenter_esp:", save->sysenter_esp, | |
3598 | "sysenter_eip:", save->sysenter_eip); | |
3599 | pr_err("%-15s %016llx %-13s %016llx\n", | |
3600 | "gpat:", save->g_pat, "dbgctl:", save->dbgctl); | |
3601 | pr_err("%-15s %016llx %-13s %016llx\n", | |
3602 | "br_from:", save->br_from, "br_to:", save->br_to); | |
3603 | pr_err("%-15s %016llx %-13s %016llx\n", | |
3604 | "excp_from:", save->last_excp_from, | |
3605 | "excp_to:", save->last_excp_to); | |
3f10c846 JR |
3606 | } |
3607 | ||
586f9607 AK |
3608 | static void svm_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2) |
3609 | { | |
3610 | struct vmcb_control_area *control = &to_svm(vcpu)->vmcb->control; | |
3611 | ||
3612 | *info1 = control->exit_info_1; | |
3613 | *info2 = control->exit_info_2; | |
3614 | } | |
3615 | ||
851ba692 | 3616 | static int handle_exit(struct kvm_vcpu *vcpu) |
6aa8b732 | 3617 | { |
04d2cc77 | 3618 | struct vcpu_svm *svm = to_svm(vcpu); |
851ba692 | 3619 | struct kvm_run *kvm_run = vcpu->run; |
a2fa3e9f | 3620 | u32 exit_code = svm->vmcb->control.exit_code; |
6aa8b732 | 3621 | |
4ee546b4 | 3622 | if (!is_cr_intercept(svm, INTERCEPT_CR0_WRITE)) |
2be4fc7a JR |
3623 | vcpu->arch.cr0 = svm->vmcb->save.cr0; |
3624 | if (npt_enabled) | |
3625 | vcpu->arch.cr3 = svm->vmcb->save.cr3; | |
af9ca2d7 | 3626 | |
cd3ff653 JR |
3627 | if (unlikely(svm->nested.exit_required)) { |
3628 | nested_svm_vmexit(svm); | |
3629 | svm->nested.exit_required = false; | |
3630 | ||
3631 | return 1; | |
3632 | } | |
3633 | ||
2030753d | 3634 | if (is_guest_mode(vcpu)) { |
410e4d57 JR |
3635 | int vmexit; |
3636 | ||
d8cabddf JR |
3637 | trace_kvm_nested_vmexit(svm->vmcb->save.rip, exit_code, |
3638 | svm->vmcb->control.exit_info_1, | |
3639 | svm->vmcb->control.exit_info_2, | |
3640 | svm->vmcb->control.exit_int_info, | |
e097e5ff SH |
3641 | svm->vmcb->control.exit_int_info_err, |
3642 | KVM_ISA_SVM); | |
d8cabddf | 3643 | |
410e4d57 JR |
3644 | vmexit = nested_svm_exit_special(svm); |
3645 | ||
3646 | if (vmexit == NESTED_EXIT_CONTINUE) | |
3647 | vmexit = nested_svm_exit_handled(svm); | |
3648 | ||
3649 | if (vmexit == NESTED_EXIT_DONE) | |
cf74a78b | 3650 | return 1; |
cf74a78b AG |
3651 | } |
3652 | ||
a5c3832d JR |
3653 | svm_complete_interrupts(svm); |
3654 | ||
04d2cc77 AK |
3655 | if (svm->vmcb->control.exit_code == SVM_EXIT_ERR) { |
3656 | kvm_run->exit_reason = KVM_EXIT_FAIL_ENTRY; | |
3657 | kvm_run->fail_entry.hardware_entry_failure_reason | |
3658 | = svm->vmcb->control.exit_code; | |
3f10c846 JR |
3659 | pr_err("KVM: FAILED VMRUN WITH VMCB:\n"); |
3660 | dump_vmcb(vcpu); | |
04d2cc77 AK |
3661 | return 0; |
3662 | } | |
3663 | ||
a2fa3e9f | 3664 | if (is_external_interrupt(svm->vmcb->control.exit_int_info) && |
709ddebf | 3665 | exit_code != SVM_EXIT_EXCP_BASE + PF_VECTOR && |
55c5e464 JR |
3666 | exit_code != SVM_EXIT_NPF && exit_code != SVM_EXIT_TASK_SWITCH && |
3667 | exit_code != SVM_EXIT_INTR && exit_code != SVM_EXIT_NMI) | |
6614c7d0 | 3668 | printk(KERN_ERR "%s: unexpected exit_int_info 0x%x " |
6aa8b732 | 3669 | "exit_code 0x%x\n", |
b8688d51 | 3670 | __func__, svm->vmcb->control.exit_int_info, |
6aa8b732 AK |
3671 | exit_code); |
3672 | ||
9d8f549d | 3673 | if (exit_code >= ARRAY_SIZE(svm_exit_handlers) |
56919c5c | 3674 | || !svm_exit_handlers[exit_code]) { |
faac2458 | 3675 | WARN_ONCE(1, "svm: unexpected exit reason 0x%x\n", exit_code); |
2bc19dc3 MT |
3676 | kvm_queue_exception(vcpu, UD_VECTOR); |
3677 | return 1; | |
6aa8b732 AK |
3678 | } |
3679 | ||
851ba692 | 3680 | return svm_exit_handlers[exit_code](svm); |
6aa8b732 AK |
3681 | } |
3682 | ||
3683 | static void reload_tss(struct kvm_vcpu *vcpu) | |
3684 | { | |
3685 | int cpu = raw_smp_processor_id(); | |
3686 | ||
0fe1e009 TH |
3687 | struct svm_cpu_data *sd = per_cpu(svm_data, cpu); |
3688 | sd->tss_desc->type = 9; /* available 32/64-bit TSS */ | |
6aa8b732 AK |
3689 | load_TR_desc(); |
3690 | } | |
3691 | ||
e756fc62 | 3692 | static void pre_svm_run(struct vcpu_svm *svm) |
6aa8b732 AK |
3693 | { |
3694 | int cpu = raw_smp_processor_id(); | |
3695 | ||
0fe1e009 | 3696 | struct svm_cpu_data *sd = per_cpu(svm_data, cpu); |
6aa8b732 | 3697 | |
4b656b12 | 3698 | /* FIXME: handle wraparound of asid_generation */ |
0fe1e009 TH |
3699 | if (svm->asid_generation != sd->asid_generation) |
3700 | new_asid(svm, sd); | |
6aa8b732 AK |
3701 | } |
3702 | ||
95ba8273 GN |
3703 | static void svm_inject_nmi(struct kvm_vcpu *vcpu) |
3704 | { | |
3705 | struct vcpu_svm *svm = to_svm(vcpu); | |
3706 | ||
3707 | svm->vmcb->control.event_inj = SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_NMI; | |
3708 | vcpu->arch.hflags |= HF_NMI_MASK; | |
8a05a1b8 | 3709 | set_intercept(svm, INTERCEPT_IRET); |
95ba8273 GN |
3710 | ++vcpu->stat.nmi_injections; |
3711 | } | |
6aa8b732 | 3712 | |
85f455f7 | 3713 | static inline void svm_inject_irq(struct vcpu_svm *svm, int irq) |
6aa8b732 AK |
3714 | { |
3715 | struct vmcb_control_area *control; | |
3716 | ||
e756fc62 | 3717 | control = &svm->vmcb->control; |
85f455f7 | 3718 | control->int_vector = irq; |
6aa8b732 AK |
3719 | control->int_ctl &= ~V_INTR_PRIO_MASK; |
3720 | control->int_ctl |= V_IRQ_MASK | | |
3721 | ((/*control->int_vector >> 4*/ 0xf) << V_INTR_PRIO_SHIFT); | |
decdbf6a | 3722 | mark_dirty(svm->vmcb, VMCB_INTR); |
6aa8b732 AK |
3723 | } |
3724 | ||
66fd3f7f | 3725 | static void svm_set_irq(struct kvm_vcpu *vcpu) |
2a8067f1 ED |
3726 | { |
3727 | struct vcpu_svm *svm = to_svm(vcpu); | |
3728 | ||
2af9194d | 3729 | BUG_ON(!(gif_set(svm))); |
cf74a78b | 3730 | |
9fb2d2b4 GN |
3731 | trace_kvm_inj_virq(vcpu->arch.interrupt.nr); |
3732 | ++vcpu->stat.irq_injections; | |
3733 | ||
219b65dc AG |
3734 | svm->vmcb->control.event_inj = vcpu->arch.interrupt.nr | |
3735 | SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_INTR; | |
2a8067f1 ED |
3736 | } |
3737 | ||
95ba8273 | 3738 | static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr) |
aaacfc9a JR |
3739 | { |
3740 | struct vcpu_svm *svm = to_svm(vcpu); | |
aaacfc9a | 3741 | |
2030753d | 3742 | if (is_guest_mode(vcpu) && (vcpu->arch.hflags & HF_VINTR_MASK)) |
88ab24ad JR |
3743 | return; |
3744 | ||
596f3142 RK |
3745 | clr_cr_intercept(svm, INTERCEPT_CR8_WRITE); |
3746 | ||
95ba8273 | 3747 | if (irr == -1) |
aaacfc9a JR |
3748 | return; |
3749 | ||
95ba8273 | 3750 | if (tpr >= irr) |
4ee546b4 | 3751 | set_cr_intercept(svm, INTERCEPT_CR8_WRITE); |
95ba8273 | 3752 | } |
aaacfc9a | 3753 | |
8d14695f YZ |
3754 | static void svm_set_virtual_x2apic_mode(struct kvm_vcpu *vcpu, bool set) |
3755 | { | |
3756 | return; | |
3757 | } | |
3758 | ||
c7c9c56c YZ |
3759 | static int svm_vm_has_apicv(struct kvm *kvm) |
3760 | { | |
3761 | return 0; | |
3762 | } | |
3763 | ||
3764 | static void svm_load_eoi_exitmap(struct kvm_vcpu *vcpu, u64 *eoi_exit_bitmap) | |
3765 | { | |
3766 | return; | |
3767 | } | |
3768 | ||
a20ed54d YZ |
3769 | static void svm_sync_pir_to_irr(struct kvm_vcpu *vcpu) |
3770 | { | |
3771 | return; | |
3772 | } | |
3773 | ||
95ba8273 GN |
3774 | static int svm_nmi_allowed(struct kvm_vcpu *vcpu) |
3775 | { | |
3776 | struct vcpu_svm *svm = to_svm(vcpu); | |
3777 | struct vmcb *vmcb = svm->vmcb; | |
924584cc JR |
3778 | int ret; |
3779 | ret = !(vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK) && | |
3780 | !(svm->vcpu.arch.hflags & HF_NMI_MASK); | |
3781 | ret = ret && gif_set(svm) && nested_svm_nmi(svm); | |
3782 | ||
3783 | return ret; | |
aaacfc9a JR |
3784 | } |
3785 | ||
3cfc3092 JK |
3786 | static bool svm_get_nmi_mask(struct kvm_vcpu *vcpu) |
3787 | { | |
3788 | struct vcpu_svm *svm = to_svm(vcpu); | |
3789 | ||
3790 | return !!(svm->vcpu.arch.hflags & HF_NMI_MASK); | |
3791 | } | |
3792 | ||
3793 | static void svm_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked) | |
3794 | { | |
3795 | struct vcpu_svm *svm = to_svm(vcpu); | |
3796 | ||
3797 | if (masked) { | |
3798 | svm->vcpu.arch.hflags |= HF_NMI_MASK; | |
8a05a1b8 | 3799 | set_intercept(svm, INTERCEPT_IRET); |
3cfc3092 JK |
3800 | } else { |
3801 | svm->vcpu.arch.hflags &= ~HF_NMI_MASK; | |
8a05a1b8 | 3802 | clr_intercept(svm, INTERCEPT_IRET); |
3cfc3092 JK |
3803 | } |
3804 | } | |
3805 | ||
78646121 GN |
3806 | static int svm_interrupt_allowed(struct kvm_vcpu *vcpu) |
3807 | { | |
3808 | struct vcpu_svm *svm = to_svm(vcpu); | |
3809 | struct vmcb *vmcb = svm->vmcb; | |
7fcdb510 JR |
3810 | int ret; |
3811 | ||
3812 | if (!gif_set(svm) || | |
3813 | (vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK)) | |
3814 | return 0; | |
3815 | ||
f6e78475 | 3816 | ret = !!(kvm_get_rflags(vcpu) & X86_EFLAGS_IF); |
7fcdb510 | 3817 | |
2030753d | 3818 | if (is_guest_mode(vcpu)) |
7fcdb510 JR |
3819 | return ret && !(svm->vcpu.arch.hflags & HF_VINTR_MASK); |
3820 | ||
3821 | return ret; | |
78646121 GN |
3822 | } |
3823 | ||
c9a7953f | 3824 | static void enable_irq_window(struct kvm_vcpu *vcpu) |
6aa8b732 | 3825 | { |
219b65dc | 3826 | struct vcpu_svm *svm = to_svm(vcpu); |
219b65dc | 3827 | |
e0231715 JR |
3828 | /* |
3829 | * In case GIF=0 we can't rely on the CPU to tell us when GIF becomes | |
3830 | * 1, because that's a separate STGI/VMRUN intercept. The next time we | |
3831 | * get that intercept, this function will be called again though and | |
3832 | * we'll get the vintr intercept. | |
3833 | */ | |
8fe54654 | 3834 | if (gif_set(svm) && nested_svm_intr(svm)) { |
219b65dc AG |
3835 | svm_set_vintr(svm); |
3836 | svm_inject_irq(svm, 0x0); | |
3837 | } | |
85f455f7 ED |
3838 | } |
3839 | ||
c9a7953f | 3840 | static void enable_nmi_window(struct kvm_vcpu *vcpu) |
c1150d8c | 3841 | { |
04d2cc77 | 3842 | struct vcpu_svm *svm = to_svm(vcpu); |
c1150d8c | 3843 | |
44c11430 GN |
3844 | if ((svm->vcpu.arch.hflags & (HF_NMI_MASK | HF_IRET_MASK)) |
3845 | == HF_NMI_MASK) | |
c9a7953f | 3846 | return; /* IRET will cause a vm exit */ |
44c11430 | 3847 | |
e0231715 JR |
3848 | /* |
3849 | * Something prevents NMI from been injected. Single step over possible | |
3850 | * problem (IRET or exception injection or interrupt shadow) | |
3851 | */ | |
6be7d306 | 3852 | svm->nmi_singlestep = true; |
44c11430 | 3853 | svm->vmcb->save.rflags |= (X86_EFLAGS_TF | X86_EFLAGS_RF); |
c8639010 | 3854 | update_db_bp_intercept(vcpu); |
c1150d8c DL |
3855 | } |
3856 | ||
cbc94022 IE |
3857 | static int svm_set_tss_addr(struct kvm *kvm, unsigned int addr) |
3858 | { | |
3859 | return 0; | |
3860 | } | |
3861 | ||
d9e368d6 AK |
3862 | static void svm_flush_tlb(struct kvm_vcpu *vcpu) |
3863 | { | |
38e5e92f JR |
3864 | struct vcpu_svm *svm = to_svm(vcpu); |
3865 | ||
3866 | if (static_cpu_has(X86_FEATURE_FLUSHBYASID)) | |
3867 | svm->vmcb->control.tlb_ctl = TLB_CONTROL_FLUSH_ASID; | |
3868 | else | |
3869 | svm->asid_generation--; | |
d9e368d6 AK |
3870 | } |
3871 | ||
04d2cc77 AK |
3872 | static void svm_prepare_guest_switch(struct kvm_vcpu *vcpu) |
3873 | { | |
3874 | } | |
3875 | ||
d7bf8221 JR |
3876 | static inline void sync_cr8_to_lapic(struct kvm_vcpu *vcpu) |
3877 | { | |
3878 | struct vcpu_svm *svm = to_svm(vcpu); | |
3879 | ||
2030753d | 3880 | if (is_guest_mode(vcpu) && (vcpu->arch.hflags & HF_VINTR_MASK)) |
88ab24ad JR |
3881 | return; |
3882 | ||
4ee546b4 | 3883 | if (!is_cr_intercept(svm, INTERCEPT_CR8_WRITE)) { |
d7bf8221 | 3884 | int cr8 = svm->vmcb->control.int_ctl & V_TPR_MASK; |
615d5193 | 3885 | kvm_set_cr8(vcpu, cr8); |
d7bf8221 JR |
3886 | } |
3887 | } | |
3888 | ||
649d6864 JR |
3889 | static inline void sync_lapic_to_cr8(struct kvm_vcpu *vcpu) |
3890 | { | |
3891 | struct vcpu_svm *svm = to_svm(vcpu); | |
3892 | u64 cr8; | |
3893 | ||
2030753d | 3894 | if (is_guest_mode(vcpu) && (vcpu->arch.hflags & HF_VINTR_MASK)) |
88ab24ad JR |
3895 | return; |
3896 | ||
649d6864 JR |
3897 | cr8 = kvm_get_cr8(vcpu); |
3898 | svm->vmcb->control.int_ctl &= ~V_TPR_MASK; | |
3899 | svm->vmcb->control.int_ctl |= cr8 & V_TPR_MASK; | |
3900 | } | |
3901 | ||
9222be18 GN |
3902 | static void svm_complete_interrupts(struct vcpu_svm *svm) |
3903 | { | |
3904 | u8 vector; | |
3905 | int type; | |
3906 | u32 exitintinfo = svm->vmcb->control.exit_int_info; | |
66b7138f JK |
3907 | unsigned int3_injected = svm->int3_injected; |
3908 | ||
3909 | svm->int3_injected = 0; | |
9222be18 | 3910 | |
bd3d1ec3 AK |
3911 | /* |
3912 | * If we've made progress since setting HF_IRET_MASK, we've | |
3913 | * executed an IRET and can allow NMI injection. | |
3914 | */ | |
3915 | if ((svm->vcpu.arch.hflags & HF_IRET_MASK) | |
3916 | && kvm_rip_read(&svm->vcpu) != svm->nmi_iret_rip) { | |
44c11430 | 3917 | svm->vcpu.arch.hflags &= ~(HF_NMI_MASK | HF_IRET_MASK); |
3842d135 AK |
3918 | kvm_make_request(KVM_REQ_EVENT, &svm->vcpu); |
3919 | } | |
44c11430 | 3920 | |
9222be18 GN |
3921 | svm->vcpu.arch.nmi_injected = false; |
3922 | kvm_clear_exception_queue(&svm->vcpu); | |
3923 | kvm_clear_interrupt_queue(&svm->vcpu); | |
3924 | ||
3925 | if (!(exitintinfo & SVM_EXITINTINFO_VALID)) | |
3926 | return; | |
3927 | ||
3842d135 AK |
3928 | kvm_make_request(KVM_REQ_EVENT, &svm->vcpu); |
3929 | ||
9222be18 GN |
3930 | vector = exitintinfo & SVM_EXITINTINFO_VEC_MASK; |
3931 | type = exitintinfo & SVM_EXITINTINFO_TYPE_MASK; | |
3932 | ||
3933 | switch (type) { | |
3934 | case SVM_EXITINTINFO_TYPE_NMI: | |
3935 | svm->vcpu.arch.nmi_injected = true; | |
3936 | break; | |
3937 | case SVM_EXITINTINFO_TYPE_EXEPT: | |
66b7138f JK |
3938 | /* |
3939 | * In case of software exceptions, do not reinject the vector, | |
3940 | * but re-execute the instruction instead. Rewind RIP first | |
3941 | * if we emulated INT3 before. | |
3942 | */ | |
3943 | if (kvm_exception_is_soft(vector)) { | |
3944 | if (vector == BP_VECTOR && int3_injected && | |
3945 | kvm_is_linear_rip(&svm->vcpu, svm->int3_rip)) | |
3946 | kvm_rip_write(&svm->vcpu, | |
3947 | kvm_rip_read(&svm->vcpu) - | |
3948 | int3_injected); | |
9222be18 | 3949 | break; |
66b7138f | 3950 | } |
9222be18 GN |
3951 | if (exitintinfo & SVM_EXITINTINFO_VALID_ERR) { |
3952 | u32 err = svm->vmcb->control.exit_int_info_err; | |
ce7ddec4 | 3953 | kvm_requeue_exception_e(&svm->vcpu, vector, err); |
9222be18 GN |
3954 | |
3955 | } else | |
ce7ddec4 | 3956 | kvm_requeue_exception(&svm->vcpu, vector); |
9222be18 GN |
3957 | break; |
3958 | case SVM_EXITINTINFO_TYPE_INTR: | |
66fd3f7f | 3959 | kvm_queue_interrupt(&svm->vcpu, vector, false); |
9222be18 GN |
3960 | break; |
3961 | default: | |
3962 | break; | |
3963 | } | |
3964 | } | |
3965 | ||
b463a6f7 AK |
3966 | static void svm_cancel_injection(struct kvm_vcpu *vcpu) |
3967 | { | |
3968 | struct vcpu_svm *svm = to_svm(vcpu); | |
3969 | struct vmcb_control_area *control = &svm->vmcb->control; | |
3970 | ||
3971 | control->exit_int_info = control->event_inj; | |
3972 | control->exit_int_info_err = control->event_inj_err; | |
3973 | control->event_inj = 0; | |
3974 | svm_complete_interrupts(svm); | |
3975 | } | |
3976 | ||
851ba692 | 3977 | static void svm_vcpu_run(struct kvm_vcpu *vcpu) |
6aa8b732 | 3978 | { |
a2fa3e9f | 3979 | struct vcpu_svm *svm = to_svm(vcpu); |
d9e368d6 | 3980 | |
2041a06a JR |
3981 | svm->vmcb->save.rax = vcpu->arch.regs[VCPU_REGS_RAX]; |
3982 | svm->vmcb->save.rsp = vcpu->arch.regs[VCPU_REGS_RSP]; | |
3983 | svm->vmcb->save.rip = vcpu->arch.regs[VCPU_REGS_RIP]; | |
3984 | ||
cd3ff653 JR |
3985 | /* |
3986 | * A vmexit emulation is required before the vcpu can be executed | |
3987 | * again. | |
3988 | */ | |
3989 | if (unlikely(svm->nested.exit_required)) | |
3990 | return; | |
3991 | ||
e756fc62 | 3992 | pre_svm_run(svm); |
6aa8b732 | 3993 | |
649d6864 JR |
3994 | sync_lapic_to_cr8(vcpu); |
3995 | ||
cda0ffdd | 3996 | svm->vmcb->save.cr2 = vcpu->arch.cr2; |
6aa8b732 | 3997 | |
04d2cc77 AK |
3998 | clgi(); |
3999 | ||
4000 | local_irq_enable(); | |
36241b8c | 4001 | |
6aa8b732 | 4002 | asm volatile ( |
7454766f AK |
4003 | "push %%" _ASM_BP "; \n\t" |
4004 | "mov %c[rbx](%[svm]), %%" _ASM_BX " \n\t" | |
4005 | "mov %c[rcx](%[svm]), %%" _ASM_CX " \n\t" | |
4006 | "mov %c[rdx](%[svm]), %%" _ASM_DX " \n\t" | |
4007 | "mov %c[rsi](%[svm]), %%" _ASM_SI " \n\t" | |
4008 | "mov %c[rdi](%[svm]), %%" _ASM_DI " \n\t" | |
4009 | "mov %c[rbp](%[svm]), %%" _ASM_BP " \n\t" | |
05b3e0c2 | 4010 | #ifdef CONFIG_X86_64 |
fb3f0f51 RR |
4011 | "mov %c[r8](%[svm]), %%r8 \n\t" |
4012 | "mov %c[r9](%[svm]), %%r9 \n\t" | |
4013 | "mov %c[r10](%[svm]), %%r10 \n\t" | |
4014 | "mov %c[r11](%[svm]), %%r11 \n\t" | |
4015 | "mov %c[r12](%[svm]), %%r12 \n\t" | |
4016 | "mov %c[r13](%[svm]), %%r13 \n\t" | |
4017 | "mov %c[r14](%[svm]), %%r14 \n\t" | |
4018 | "mov %c[r15](%[svm]), %%r15 \n\t" | |
6aa8b732 AK |
4019 | #endif |
4020 | ||
6aa8b732 | 4021 | /* Enter guest mode */ |
7454766f AK |
4022 | "push %%" _ASM_AX " \n\t" |
4023 | "mov %c[vmcb](%[svm]), %%" _ASM_AX " \n\t" | |
4ecac3fd AK |
4024 | __ex(SVM_VMLOAD) "\n\t" |
4025 | __ex(SVM_VMRUN) "\n\t" | |
4026 | __ex(SVM_VMSAVE) "\n\t" | |
7454766f | 4027 | "pop %%" _ASM_AX " \n\t" |
6aa8b732 AK |
4028 | |
4029 | /* Save guest registers, load host registers */ | |
7454766f AK |
4030 | "mov %%" _ASM_BX ", %c[rbx](%[svm]) \n\t" |
4031 | "mov %%" _ASM_CX ", %c[rcx](%[svm]) \n\t" | |
4032 | "mov %%" _ASM_DX ", %c[rdx](%[svm]) \n\t" | |
4033 | "mov %%" _ASM_SI ", %c[rsi](%[svm]) \n\t" | |
4034 | "mov %%" _ASM_DI ", %c[rdi](%[svm]) \n\t" | |
4035 | "mov %%" _ASM_BP ", %c[rbp](%[svm]) \n\t" | |
05b3e0c2 | 4036 | #ifdef CONFIG_X86_64 |
fb3f0f51 RR |
4037 | "mov %%r8, %c[r8](%[svm]) \n\t" |
4038 | "mov %%r9, %c[r9](%[svm]) \n\t" | |
4039 | "mov %%r10, %c[r10](%[svm]) \n\t" | |
4040 | "mov %%r11, %c[r11](%[svm]) \n\t" | |
4041 | "mov %%r12, %c[r12](%[svm]) \n\t" | |
4042 | "mov %%r13, %c[r13](%[svm]) \n\t" | |
4043 | "mov %%r14, %c[r14](%[svm]) \n\t" | |
4044 | "mov %%r15, %c[r15](%[svm]) \n\t" | |
6aa8b732 | 4045 | #endif |
7454766f | 4046 | "pop %%" _ASM_BP |
6aa8b732 | 4047 | : |
fb3f0f51 | 4048 | : [svm]"a"(svm), |
6aa8b732 | 4049 | [vmcb]"i"(offsetof(struct vcpu_svm, vmcb_pa)), |
ad312c7c ZX |
4050 | [rbx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RBX])), |
4051 | [rcx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RCX])), | |
4052 | [rdx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RDX])), | |
4053 | [rsi]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RSI])), | |
4054 | [rdi]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RDI])), | |
4055 | [rbp]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RBP])) | |
05b3e0c2 | 4056 | #ifdef CONFIG_X86_64 |
ad312c7c ZX |
4057 | , [r8]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R8])), |
4058 | [r9]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R9])), | |
4059 | [r10]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R10])), | |
4060 | [r11]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R11])), | |
4061 | [r12]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R12])), | |
4062 | [r13]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R13])), | |
4063 | [r14]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R14])), | |
4064 | [r15]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R15])) | |
6aa8b732 | 4065 | #endif |
54a08c04 LV |
4066 | : "cc", "memory" |
4067 | #ifdef CONFIG_X86_64 | |
7454766f | 4068 | , "rbx", "rcx", "rdx", "rsi", "rdi" |
54a08c04 | 4069 | , "r8", "r9", "r10", "r11" , "r12", "r13", "r14", "r15" |
7454766f AK |
4070 | #else |
4071 | , "ebx", "ecx", "edx", "esi", "edi" | |
54a08c04 LV |
4072 | #endif |
4073 | ); | |
6aa8b732 | 4074 | |
82ca2d10 AK |
4075 | #ifdef CONFIG_X86_64 |
4076 | wrmsrl(MSR_GS_BASE, svm->host.gs_base); | |
4077 | #else | |
dacccfdd | 4078 | loadsegment(fs, svm->host.fs); |
831ca609 AK |
4079 | #ifndef CONFIG_X86_32_LAZY_GS |
4080 | loadsegment(gs, svm->host.gs); | |
4081 | #endif | |
9581d442 | 4082 | #endif |
6aa8b732 AK |
4083 | |
4084 | reload_tss(vcpu); | |
4085 | ||
56ba47dd AK |
4086 | local_irq_disable(); |
4087 | ||
13c34e07 AK |
4088 | vcpu->arch.cr2 = svm->vmcb->save.cr2; |
4089 | vcpu->arch.regs[VCPU_REGS_RAX] = svm->vmcb->save.rax; | |
4090 | vcpu->arch.regs[VCPU_REGS_RSP] = svm->vmcb->save.rsp; | |
4091 | vcpu->arch.regs[VCPU_REGS_RIP] = svm->vmcb->save.rip; | |
4092 | ||
1e2b1dd7 JK |
4093 | trace_kvm_exit(svm->vmcb->control.exit_code, vcpu, KVM_ISA_SVM); |
4094 | ||
3781c01c JR |
4095 | if (unlikely(svm->vmcb->control.exit_code == SVM_EXIT_NMI)) |
4096 | kvm_before_handle_nmi(&svm->vcpu); | |
4097 | ||
4098 | stgi(); | |
4099 | ||
4100 | /* Any pending NMI will happen here */ | |
4101 | ||
4102 | if (unlikely(svm->vmcb->control.exit_code == SVM_EXIT_NMI)) | |
4103 | kvm_after_handle_nmi(&svm->vcpu); | |
4104 | ||
d7bf8221 JR |
4105 | sync_cr8_to_lapic(vcpu); |
4106 | ||
a2fa3e9f | 4107 | svm->next_rip = 0; |
9222be18 | 4108 | |
38e5e92f JR |
4109 | svm->vmcb->control.tlb_ctl = TLB_CONTROL_DO_NOTHING; |
4110 | ||
631bc487 GN |
4111 | /* if exit due to PF check for async PF */ |
4112 | if (svm->vmcb->control.exit_code == SVM_EXIT_EXCP_BASE + PF_VECTOR) | |
4113 | svm->apf_reason = kvm_read_and_reset_pf_reason(); | |
4114 | ||
6de4f3ad AK |
4115 | if (npt_enabled) { |
4116 | vcpu->arch.regs_avail &= ~(1 << VCPU_EXREG_PDPTR); | |
4117 | vcpu->arch.regs_dirty &= ~(1 << VCPU_EXREG_PDPTR); | |
4118 | } | |
fe5913e4 JR |
4119 | |
4120 | /* | |
4121 | * We need to handle MC intercepts here before the vcpu has a chance to | |
4122 | * change the physical cpu | |
4123 | */ | |
4124 | if (unlikely(svm->vmcb->control.exit_code == | |
4125 | SVM_EXIT_EXCP_BASE + MC_VECTOR)) | |
4126 | svm_handle_mce(svm); | |
8d28fec4 RJ |
4127 | |
4128 | mark_all_clean(svm->vmcb); | |
6aa8b732 AK |
4129 | } |
4130 | ||
6aa8b732 AK |
4131 | static void svm_set_cr3(struct kvm_vcpu *vcpu, unsigned long root) |
4132 | { | |
a2fa3e9f GH |
4133 | struct vcpu_svm *svm = to_svm(vcpu); |
4134 | ||
4135 | svm->vmcb->save.cr3 = root; | |
dcca1a65 | 4136 | mark_dirty(svm->vmcb, VMCB_CR); |
f40f6a45 | 4137 | svm_flush_tlb(vcpu); |
6aa8b732 AK |
4138 | } |
4139 | ||
1c97f0a0 JR |
4140 | static void set_tdp_cr3(struct kvm_vcpu *vcpu, unsigned long root) |
4141 | { | |
4142 | struct vcpu_svm *svm = to_svm(vcpu); | |
4143 | ||
4144 | svm->vmcb->control.nested_cr3 = root; | |
b2747166 | 4145 | mark_dirty(svm->vmcb, VMCB_NPT); |
1c97f0a0 JR |
4146 | |
4147 | /* Also sync guest cr3 here in case we live migrate */ | |
9f8fe504 | 4148 | svm->vmcb->save.cr3 = kvm_read_cr3(vcpu); |
dcca1a65 | 4149 | mark_dirty(svm->vmcb, VMCB_CR); |
1c97f0a0 | 4150 | |
f40f6a45 | 4151 | svm_flush_tlb(vcpu); |
1c97f0a0 JR |
4152 | } |
4153 | ||
6aa8b732 AK |
4154 | static int is_disabled(void) |
4155 | { | |
6031a61c JR |
4156 | u64 vm_cr; |
4157 | ||
4158 | rdmsrl(MSR_VM_CR, vm_cr); | |
4159 | if (vm_cr & (1 << SVM_VM_CR_SVM_DISABLE)) | |
4160 | return 1; | |
4161 | ||
6aa8b732 AK |
4162 | return 0; |
4163 | } | |
4164 | ||
102d8325 IM |
4165 | static void |
4166 | svm_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall) | |
4167 | { | |
4168 | /* | |
4169 | * Patch in the VMMCALL instruction: | |
4170 | */ | |
4171 | hypercall[0] = 0x0f; | |
4172 | hypercall[1] = 0x01; | |
4173 | hypercall[2] = 0xd9; | |
102d8325 IM |
4174 | } |
4175 | ||
002c7f7c YS |
4176 | static void svm_check_processor_compat(void *rtn) |
4177 | { | |
4178 | *(int *)rtn = 0; | |
4179 | } | |
4180 | ||
774ead3a AK |
4181 | static bool svm_cpu_has_accelerated_tpr(void) |
4182 | { | |
4183 | return false; | |
4184 | } | |
4185 | ||
6d396b55 PB |
4186 | static bool svm_has_high_real_mode_segbase(void) |
4187 | { | |
4188 | return true; | |
4189 | } | |
4190 | ||
0e851880 SY |
4191 | static void svm_cpuid_update(struct kvm_vcpu *vcpu) |
4192 | { | |
4193 | } | |
4194 | ||
d4330ef2 JR |
4195 | static void svm_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry) |
4196 | { | |
c2c63a49 | 4197 | switch (func) { |
4c62a2dc JR |
4198 | case 0x80000001: |
4199 | if (nested) | |
4200 | entry->ecx |= (1 << 2); /* Set SVM bit */ | |
4201 | break; | |
c2c63a49 JR |
4202 | case 0x8000000A: |
4203 | entry->eax = 1; /* SVM revision 1 */ | |
4204 | entry->ebx = 8; /* Lets support 8 ASIDs in case we add proper | |
4205 | ASID emulation to nested SVM */ | |
4206 | entry->ecx = 0; /* Reserved */ | |
7a190667 JR |
4207 | entry->edx = 0; /* Per default do not support any |
4208 | additional features */ | |
4209 | ||
4210 | /* Support next_rip if host supports it */ | |
2a6b20b8 | 4211 | if (boot_cpu_has(X86_FEATURE_NRIPS)) |
7a190667 | 4212 | entry->edx |= SVM_FEATURE_NRIP; |
c2c63a49 | 4213 | |
3d4aeaad JR |
4214 | /* Support NPT for the guest if enabled */ |
4215 | if (npt_enabled) | |
4216 | entry->edx |= SVM_FEATURE_NPT; | |
4217 | ||
c2c63a49 JR |
4218 | break; |
4219 | } | |
d4330ef2 JR |
4220 | } |
4221 | ||
17cc3935 | 4222 | static int svm_get_lpage_level(void) |
344f414f | 4223 | { |
17cc3935 | 4224 | return PT_PDPE_LEVEL; |
344f414f JR |
4225 | } |
4226 | ||
4e47c7a6 SY |
4227 | static bool svm_rdtscp_supported(void) |
4228 | { | |
4229 | return false; | |
4230 | } | |
4231 | ||
ad756a16 MJ |
4232 | static bool svm_invpcid_supported(void) |
4233 | { | |
4234 | return false; | |
4235 | } | |
4236 | ||
93c4adc7 PB |
4237 | static bool svm_mpx_supported(void) |
4238 | { | |
4239 | return false; | |
4240 | } | |
4241 | ||
55412b2e WL |
4242 | static bool svm_xsaves_supported(void) |
4243 | { | |
4244 | return false; | |
4245 | } | |
4246 | ||
f5f48ee1 SY |
4247 | static bool svm_has_wbinvd_exit(void) |
4248 | { | |
4249 | return true; | |
4250 | } | |
4251 | ||
02daab21 AK |
4252 | static void svm_fpu_deactivate(struct kvm_vcpu *vcpu) |
4253 | { | |
4254 | struct vcpu_svm *svm = to_svm(vcpu); | |
4255 | ||
18c918c5 | 4256 | set_exception_intercept(svm, NM_VECTOR); |
66a562f7 | 4257 | update_cr0_intercept(svm); |
02daab21 AK |
4258 | } |
4259 | ||
8061252e | 4260 | #define PRE_EX(exit) { .exit_code = (exit), \ |
40e19b51 | 4261 | .stage = X86_ICPT_PRE_EXCEPT, } |
cfec82cb | 4262 | #define POST_EX(exit) { .exit_code = (exit), \ |
40e19b51 | 4263 | .stage = X86_ICPT_POST_EXCEPT, } |
d7eb8203 | 4264 | #define POST_MEM(exit) { .exit_code = (exit), \ |
40e19b51 | 4265 | .stage = X86_ICPT_POST_MEMACCESS, } |
cfec82cb | 4266 | |
09941fbb | 4267 | static const struct __x86_intercept { |
cfec82cb JR |
4268 | u32 exit_code; |
4269 | enum x86_intercept_stage stage; | |
cfec82cb JR |
4270 | } x86_intercept_map[] = { |
4271 | [x86_intercept_cr_read] = POST_EX(SVM_EXIT_READ_CR0), | |
4272 | [x86_intercept_cr_write] = POST_EX(SVM_EXIT_WRITE_CR0), | |
4273 | [x86_intercept_clts] = POST_EX(SVM_EXIT_WRITE_CR0), | |
4274 | [x86_intercept_lmsw] = POST_EX(SVM_EXIT_WRITE_CR0), | |
4275 | [x86_intercept_smsw] = POST_EX(SVM_EXIT_READ_CR0), | |
3b88e41a JR |
4276 | [x86_intercept_dr_read] = POST_EX(SVM_EXIT_READ_DR0), |
4277 | [x86_intercept_dr_write] = POST_EX(SVM_EXIT_WRITE_DR0), | |
dee6bb70 JR |
4278 | [x86_intercept_sldt] = POST_EX(SVM_EXIT_LDTR_READ), |
4279 | [x86_intercept_str] = POST_EX(SVM_EXIT_TR_READ), | |
4280 | [x86_intercept_lldt] = POST_EX(SVM_EXIT_LDTR_WRITE), | |
4281 | [x86_intercept_ltr] = POST_EX(SVM_EXIT_TR_WRITE), | |
4282 | [x86_intercept_sgdt] = POST_EX(SVM_EXIT_GDTR_READ), | |
4283 | [x86_intercept_sidt] = POST_EX(SVM_EXIT_IDTR_READ), | |
4284 | [x86_intercept_lgdt] = POST_EX(SVM_EXIT_GDTR_WRITE), | |
4285 | [x86_intercept_lidt] = POST_EX(SVM_EXIT_IDTR_WRITE), | |
01de8b09 JR |
4286 | [x86_intercept_vmrun] = POST_EX(SVM_EXIT_VMRUN), |
4287 | [x86_intercept_vmmcall] = POST_EX(SVM_EXIT_VMMCALL), | |
4288 | [x86_intercept_vmload] = POST_EX(SVM_EXIT_VMLOAD), | |
4289 | [x86_intercept_vmsave] = POST_EX(SVM_EXIT_VMSAVE), | |
4290 | [x86_intercept_stgi] = POST_EX(SVM_EXIT_STGI), | |
4291 | [x86_intercept_clgi] = POST_EX(SVM_EXIT_CLGI), | |
4292 | [x86_intercept_skinit] = POST_EX(SVM_EXIT_SKINIT), | |
4293 | [x86_intercept_invlpga] = POST_EX(SVM_EXIT_INVLPGA), | |
d7eb8203 JR |
4294 | [x86_intercept_rdtscp] = POST_EX(SVM_EXIT_RDTSCP), |
4295 | [x86_intercept_monitor] = POST_MEM(SVM_EXIT_MONITOR), | |
4296 | [x86_intercept_mwait] = POST_EX(SVM_EXIT_MWAIT), | |
8061252e JR |
4297 | [x86_intercept_invlpg] = POST_EX(SVM_EXIT_INVLPG), |
4298 | [x86_intercept_invd] = POST_EX(SVM_EXIT_INVD), | |
4299 | [x86_intercept_wbinvd] = POST_EX(SVM_EXIT_WBINVD), | |
4300 | [x86_intercept_wrmsr] = POST_EX(SVM_EXIT_MSR), | |
4301 | [x86_intercept_rdtsc] = POST_EX(SVM_EXIT_RDTSC), | |
4302 | [x86_intercept_rdmsr] = POST_EX(SVM_EXIT_MSR), | |
4303 | [x86_intercept_rdpmc] = POST_EX(SVM_EXIT_RDPMC), | |
4304 | [x86_intercept_cpuid] = PRE_EX(SVM_EXIT_CPUID), | |
4305 | [x86_intercept_rsm] = PRE_EX(SVM_EXIT_RSM), | |
bf608f88 JR |
4306 | [x86_intercept_pause] = PRE_EX(SVM_EXIT_PAUSE), |
4307 | [x86_intercept_pushf] = PRE_EX(SVM_EXIT_PUSHF), | |
4308 | [x86_intercept_popf] = PRE_EX(SVM_EXIT_POPF), | |
4309 | [x86_intercept_intn] = PRE_EX(SVM_EXIT_SWINT), | |
4310 | [x86_intercept_iret] = PRE_EX(SVM_EXIT_IRET), | |
4311 | [x86_intercept_icebp] = PRE_EX(SVM_EXIT_ICEBP), | |
4312 | [x86_intercept_hlt] = POST_EX(SVM_EXIT_HLT), | |
f6511935 JR |
4313 | [x86_intercept_in] = POST_EX(SVM_EXIT_IOIO), |
4314 | [x86_intercept_ins] = POST_EX(SVM_EXIT_IOIO), | |
4315 | [x86_intercept_out] = POST_EX(SVM_EXIT_IOIO), | |
4316 | [x86_intercept_outs] = POST_EX(SVM_EXIT_IOIO), | |
cfec82cb JR |
4317 | }; |
4318 | ||
8061252e | 4319 | #undef PRE_EX |
cfec82cb | 4320 | #undef POST_EX |
d7eb8203 | 4321 | #undef POST_MEM |
cfec82cb | 4322 | |
8a76d7f2 JR |
4323 | static int svm_check_intercept(struct kvm_vcpu *vcpu, |
4324 | struct x86_instruction_info *info, | |
4325 | enum x86_intercept_stage stage) | |
4326 | { | |
cfec82cb JR |
4327 | struct vcpu_svm *svm = to_svm(vcpu); |
4328 | int vmexit, ret = X86EMUL_CONTINUE; | |
4329 | struct __x86_intercept icpt_info; | |
4330 | struct vmcb *vmcb = svm->vmcb; | |
4331 | ||
4332 | if (info->intercept >= ARRAY_SIZE(x86_intercept_map)) | |
4333 | goto out; | |
4334 | ||
4335 | icpt_info = x86_intercept_map[info->intercept]; | |
4336 | ||
40e19b51 | 4337 | if (stage != icpt_info.stage) |
cfec82cb JR |
4338 | goto out; |
4339 | ||
4340 | switch (icpt_info.exit_code) { | |
4341 | case SVM_EXIT_READ_CR0: | |
4342 | if (info->intercept == x86_intercept_cr_read) | |
4343 | icpt_info.exit_code += info->modrm_reg; | |
4344 | break; | |
4345 | case SVM_EXIT_WRITE_CR0: { | |
4346 | unsigned long cr0, val; | |
4347 | u64 intercept; | |
4348 | ||
4349 | if (info->intercept == x86_intercept_cr_write) | |
4350 | icpt_info.exit_code += info->modrm_reg; | |
4351 | ||
62baf44c JK |
4352 | if (icpt_info.exit_code != SVM_EXIT_WRITE_CR0 || |
4353 | info->intercept == x86_intercept_clts) | |
cfec82cb JR |
4354 | break; |
4355 | ||
4356 | intercept = svm->nested.intercept; | |
4357 | ||
4358 | if (!(intercept & (1ULL << INTERCEPT_SELECTIVE_CR0))) | |
4359 | break; | |
4360 | ||
4361 | cr0 = vcpu->arch.cr0 & ~SVM_CR0_SELECTIVE_MASK; | |
4362 | val = info->src_val & ~SVM_CR0_SELECTIVE_MASK; | |
4363 | ||
4364 | if (info->intercept == x86_intercept_lmsw) { | |
4365 | cr0 &= 0xfUL; | |
4366 | val &= 0xfUL; | |
4367 | /* lmsw can't clear PE - catch this here */ | |
4368 | if (cr0 & X86_CR0_PE) | |
4369 | val |= X86_CR0_PE; | |
4370 | } | |
4371 | ||
4372 | if (cr0 ^ val) | |
4373 | icpt_info.exit_code = SVM_EXIT_CR0_SEL_WRITE; | |
4374 | ||
4375 | break; | |
4376 | } | |
3b88e41a JR |
4377 | case SVM_EXIT_READ_DR0: |
4378 | case SVM_EXIT_WRITE_DR0: | |
4379 | icpt_info.exit_code += info->modrm_reg; | |
4380 | break; | |
8061252e JR |
4381 | case SVM_EXIT_MSR: |
4382 | if (info->intercept == x86_intercept_wrmsr) | |
4383 | vmcb->control.exit_info_1 = 1; | |
4384 | else | |
4385 | vmcb->control.exit_info_1 = 0; | |
4386 | break; | |
bf608f88 JR |
4387 | case SVM_EXIT_PAUSE: |
4388 | /* | |
4389 | * We get this for NOP only, but pause | |
4390 | * is rep not, check this here | |
4391 | */ | |
4392 | if (info->rep_prefix != REPE_PREFIX) | |
4393 | goto out; | |
f6511935 JR |
4394 | case SVM_EXIT_IOIO: { |
4395 | u64 exit_info; | |
4396 | u32 bytes; | |
4397 | ||
f6511935 JR |
4398 | if (info->intercept == x86_intercept_in || |
4399 | info->intercept == x86_intercept_ins) { | |
6cbc5f5a JK |
4400 | exit_info = ((info->src_val & 0xffff) << 16) | |
4401 | SVM_IOIO_TYPE_MASK; | |
f6511935 | 4402 | bytes = info->dst_bytes; |
6493f157 | 4403 | } else { |
6cbc5f5a | 4404 | exit_info = (info->dst_val & 0xffff) << 16; |
6493f157 | 4405 | bytes = info->src_bytes; |
f6511935 JR |
4406 | } |
4407 | ||
4408 | if (info->intercept == x86_intercept_outs || | |
4409 | info->intercept == x86_intercept_ins) | |
4410 | exit_info |= SVM_IOIO_STR_MASK; | |
4411 | ||
4412 | if (info->rep_prefix) | |
4413 | exit_info |= SVM_IOIO_REP_MASK; | |
4414 | ||
4415 | bytes = min(bytes, 4u); | |
4416 | ||
4417 | exit_info |= bytes << SVM_IOIO_SIZE_SHIFT; | |
4418 | ||
4419 | exit_info |= (u32)info->ad_bytes << (SVM_IOIO_ASIZE_SHIFT - 1); | |
4420 | ||
4421 | vmcb->control.exit_info_1 = exit_info; | |
4422 | vmcb->control.exit_info_2 = info->next_rip; | |
4423 | ||
4424 | break; | |
4425 | } | |
cfec82cb JR |
4426 | default: |
4427 | break; | |
4428 | } | |
4429 | ||
f104765b BD |
4430 | /* TODO: Advertise NRIPS to guest hypervisor unconditionally */ |
4431 | if (static_cpu_has(X86_FEATURE_NRIPS)) | |
4432 | vmcb->control.next_rip = info->next_rip; | |
cfec82cb JR |
4433 | vmcb->control.exit_code = icpt_info.exit_code; |
4434 | vmexit = nested_svm_exit_handled(svm); | |
4435 | ||
4436 | ret = (vmexit == NESTED_EXIT_DONE) ? X86EMUL_INTERCEPTED | |
4437 | : X86EMUL_CONTINUE; | |
4438 | ||
4439 | out: | |
4440 | return ret; | |
8a76d7f2 JR |
4441 | } |
4442 | ||
a547c6db YZ |
4443 | static void svm_handle_external_intr(struct kvm_vcpu *vcpu) |
4444 | { | |
4445 | local_irq_enable(); | |
4446 | } | |
4447 | ||
ae97a3b8 RK |
4448 | static void svm_sched_in(struct kvm_vcpu *vcpu, int cpu) |
4449 | { | |
4450 | } | |
4451 | ||
cbdd1bea | 4452 | static struct kvm_x86_ops svm_x86_ops = { |
6aa8b732 AK |
4453 | .cpu_has_kvm_support = has_svm, |
4454 | .disabled_by_bios = is_disabled, | |
4455 | .hardware_setup = svm_hardware_setup, | |
4456 | .hardware_unsetup = svm_hardware_unsetup, | |
002c7f7c | 4457 | .check_processor_compatibility = svm_check_processor_compat, |
6aa8b732 AK |
4458 | .hardware_enable = svm_hardware_enable, |
4459 | .hardware_disable = svm_hardware_disable, | |
774ead3a | 4460 | .cpu_has_accelerated_tpr = svm_cpu_has_accelerated_tpr, |
6d396b55 | 4461 | .cpu_has_high_real_mode_segbase = svm_has_high_real_mode_segbase, |
6aa8b732 AK |
4462 | |
4463 | .vcpu_create = svm_create_vcpu, | |
4464 | .vcpu_free = svm_free_vcpu, | |
04d2cc77 | 4465 | .vcpu_reset = svm_vcpu_reset, |
6aa8b732 | 4466 | |
04d2cc77 | 4467 | .prepare_guest_switch = svm_prepare_guest_switch, |
6aa8b732 AK |
4468 | .vcpu_load = svm_vcpu_load, |
4469 | .vcpu_put = svm_vcpu_put, | |
4470 | ||
c8639010 | 4471 | .update_db_bp_intercept = update_db_bp_intercept, |
6aa8b732 AK |
4472 | .get_msr = svm_get_msr, |
4473 | .set_msr = svm_set_msr, | |
4474 | .get_segment_base = svm_get_segment_base, | |
4475 | .get_segment = svm_get_segment, | |
4476 | .set_segment = svm_set_segment, | |
2e4d2653 | 4477 | .get_cpl = svm_get_cpl, |
1747fb71 | 4478 | .get_cs_db_l_bits = kvm_get_cs_db_l_bits, |
e8467fda | 4479 | .decache_cr0_guest_bits = svm_decache_cr0_guest_bits, |
aff48baa | 4480 | .decache_cr3 = svm_decache_cr3, |
25c4c276 | 4481 | .decache_cr4_guest_bits = svm_decache_cr4_guest_bits, |
6aa8b732 | 4482 | .set_cr0 = svm_set_cr0, |
6aa8b732 AK |
4483 | .set_cr3 = svm_set_cr3, |
4484 | .set_cr4 = svm_set_cr4, | |
4485 | .set_efer = svm_set_efer, | |
4486 | .get_idt = svm_get_idt, | |
4487 | .set_idt = svm_set_idt, | |
4488 | .get_gdt = svm_get_gdt, | |
4489 | .set_gdt = svm_set_gdt, | |
73aaf249 JK |
4490 | .get_dr6 = svm_get_dr6, |
4491 | .set_dr6 = svm_set_dr6, | |
020df079 | 4492 | .set_dr7 = svm_set_dr7, |
facb0139 | 4493 | .sync_dirty_debug_regs = svm_sync_dirty_debug_regs, |
6de4f3ad | 4494 | .cache_reg = svm_cache_reg, |
6aa8b732 AK |
4495 | .get_rflags = svm_get_rflags, |
4496 | .set_rflags = svm_set_rflags, | |
0fdd74f7 | 4497 | .fpu_activate = svm_fpu_activate, |
02daab21 | 4498 | .fpu_deactivate = svm_fpu_deactivate, |
6aa8b732 | 4499 | |
6aa8b732 | 4500 | .tlb_flush = svm_flush_tlb, |
6aa8b732 | 4501 | |
6aa8b732 | 4502 | .run = svm_vcpu_run, |
04d2cc77 | 4503 | .handle_exit = handle_exit, |
6aa8b732 | 4504 | .skip_emulated_instruction = skip_emulated_instruction, |
2809f5d2 GC |
4505 | .set_interrupt_shadow = svm_set_interrupt_shadow, |
4506 | .get_interrupt_shadow = svm_get_interrupt_shadow, | |
102d8325 | 4507 | .patch_hypercall = svm_patch_hypercall, |
2a8067f1 | 4508 | .set_irq = svm_set_irq, |
95ba8273 | 4509 | .set_nmi = svm_inject_nmi, |
298101da | 4510 | .queue_exception = svm_queue_exception, |
b463a6f7 | 4511 | .cancel_injection = svm_cancel_injection, |
78646121 | 4512 | .interrupt_allowed = svm_interrupt_allowed, |
95ba8273 | 4513 | .nmi_allowed = svm_nmi_allowed, |
3cfc3092 JK |
4514 | .get_nmi_mask = svm_get_nmi_mask, |
4515 | .set_nmi_mask = svm_set_nmi_mask, | |
95ba8273 GN |
4516 | .enable_nmi_window = enable_nmi_window, |
4517 | .enable_irq_window = enable_irq_window, | |
4518 | .update_cr8_intercept = update_cr8_intercept, | |
8d14695f | 4519 | .set_virtual_x2apic_mode = svm_set_virtual_x2apic_mode, |
c7c9c56c YZ |
4520 | .vm_has_apicv = svm_vm_has_apicv, |
4521 | .load_eoi_exitmap = svm_load_eoi_exitmap, | |
a20ed54d | 4522 | .sync_pir_to_irr = svm_sync_pir_to_irr, |
cbc94022 IE |
4523 | |
4524 | .set_tss_addr = svm_set_tss_addr, | |
67253af5 | 4525 | .get_tdp_level = get_npt_level, |
4b12f0de | 4526 | .get_mt_mask = svm_get_mt_mask, |
229456fc | 4527 | |
586f9607 | 4528 | .get_exit_info = svm_get_exit_info, |
586f9607 | 4529 | |
17cc3935 | 4530 | .get_lpage_level = svm_get_lpage_level, |
0e851880 SY |
4531 | |
4532 | .cpuid_update = svm_cpuid_update, | |
4e47c7a6 SY |
4533 | |
4534 | .rdtscp_supported = svm_rdtscp_supported, | |
ad756a16 | 4535 | .invpcid_supported = svm_invpcid_supported, |
93c4adc7 | 4536 | .mpx_supported = svm_mpx_supported, |
55412b2e | 4537 | .xsaves_supported = svm_xsaves_supported, |
d4330ef2 JR |
4538 | |
4539 | .set_supported_cpuid = svm_set_supported_cpuid, | |
f5f48ee1 SY |
4540 | |
4541 | .has_wbinvd_exit = svm_has_wbinvd_exit, | |
99e3e30a | 4542 | |
4051b188 | 4543 | .set_tsc_khz = svm_set_tsc_khz, |
ba904635 | 4544 | .read_tsc_offset = svm_read_tsc_offset, |
99e3e30a | 4545 | .write_tsc_offset = svm_write_tsc_offset, |
e48672fa | 4546 | .adjust_tsc_offset = svm_adjust_tsc_offset, |
857e4099 | 4547 | .compute_tsc_offset = svm_compute_tsc_offset, |
d5c1785d | 4548 | .read_l1_tsc = svm_read_l1_tsc, |
1c97f0a0 JR |
4549 | |
4550 | .set_tdp_cr3 = set_tdp_cr3, | |
8a76d7f2 JR |
4551 | |
4552 | .check_intercept = svm_check_intercept, | |
a547c6db | 4553 | .handle_external_intr = svm_handle_external_intr, |
ae97a3b8 RK |
4554 | |
4555 | .sched_in = svm_sched_in, | |
25462f7f WH |
4556 | |
4557 | .pmu_ops = &amd_pmu_ops, | |
6aa8b732 AK |
4558 | }; |
4559 | ||
4560 | static int __init svm_init(void) | |
4561 | { | |
cb498ea2 | 4562 | return kvm_init(&svm_x86_ops, sizeof(struct vcpu_svm), |
0ee75bea | 4563 | __alignof__(struct vcpu_svm), THIS_MODULE); |
6aa8b732 AK |
4564 | } |
4565 | ||
4566 | static void __exit svm_exit(void) | |
4567 | { | |
cb498ea2 | 4568 | kvm_exit(); |
6aa8b732 AK |
4569 | } |
4570 | ||
4571 | module_init(svm_init) | |
4572 | module_exit(svm_exit) |