1 #define pr_fmt(fmt) "SVM: " fmt
2
3 #include <linux/kvm_host.h>
4
5 #include "irq.h"
6 #include "mmu.h"
7 #include "kvm_cache_regs.h"
8 #include "x86.h"
9 #include "cpuid.h"
10 #include "pmu.h"
11
12 #include <linux/module.h>
13 #include <linux/mod_devicetable.h>
14 #include <linux/kernel.h>
15 #include <linux/vmalloc.h>
16 #include <linux/highmem.h>
17 #include <linux/amd-iommu.h>
18 #include <linux/sched.h>
19 #include <linux/trace_events.h>
20 #include <linux/slab.h>
21 #include <linux/hashtable.h>
22 #include <linux/objtool.h>
23 #include <linux/psp-sev.h>
24 #include <linux/file.h>
25 #include <linux/pagemap.h>
26 #include <linux/swap.h>
27 #include <linux/rwsem.h>
28
29 #include <asm/apic.h>
30 #include <asm/perf_event.h>
31 #include <asm/tlbflush.h>
32 #include <asm/desc.h>
33 #include <asm/debugreg.h>
34 #include <asm/kvm_para.h>
35 #include <asm/irq_remapping.h>
36 #include <asm/mce.h>
37 #include <asm/spec-ctrl.h>
38 #include <asm/cpu_device_id.h>
39
40 #include <asm/virtext.h>
41 #include "trace.h"
42
43 #include "svm.h"
44
45 #define __ex(x) __kvm_handle_fault_on_reboot(x)
46
47 MODULE_AUTHOR("Qumranet");
48 MODULE_LICENSE("GPL");
49
50 #ifdef MODULE
51 static const struct x86_cpu_id svm_cpu_id[] = {
52 X86_MATCH_FEATURE(X86_FEATURE_SVM, NULL),
53 {}
54 };
55 MODULE_DEVICE_TABLE(x86cpu, svm_cpu_id);
56 #endif
57
58 #define IOPM_ALLOC_ORDER 2
59 #define MSRPM_ALLOC_ORDER 1
60
61 #define SEG_TYPE_LDT 2
62 #define SEG_TYPE_BUSY_TSS16 3
63
64 #define SVM_FEATURE_LBRV (1 << 1)
65 #define SVM_FEATURE_SVML (1 << 2)
66 #define SVM_FEATURE_TSC_RATE (1 << 4)
67 #define SVM_FEATURE_VMCB_CLEAN (1 << 5)
68 #define SVM_FEATURE_FLUSH_ASID (1 << 6)
69 #define SVM_FEATURE_DECODE_ASSIST (1 << 7)
70 #define SVM_FEATURE_PAUSE_FILTER (1 << 10)
71
72 #define DEBUGCTL_RESERVED_BITS (~(0x3fULL))
73
74 #define TSC_RATIO_RSVD 0xffffff0000000000ULL
75 #define TSC_RATIO_MIN 0x0000000000000001ULL
76 #define TSC_RATIO_MAX 0x000000ffffffffffULL
77
78 static bool erratum_383_found __read_mostly;
79
80 u32 msrpm_offsets[MSRPM_OFFSETS] __read_mostly;
81
82 /*
83 * Set osvw_len to higher value when updated Revision Guides
84 * are published and we know what the new status bits are
85 */
86 static uint64_t osvw_len = 4, osvw_status;
87
88 static DEFINE_PER_CPU(u64, current_tsc_ratio);
89 #define TSC_RATIO_DEFAULT 0x0100000000ULL
90
91 static const struct svm_direct_access_msrs {
92 u32 index; /* Index of the MSR */
93 bool always; /* True if intercept is always on */
94 } direct_access_msrs[MAX_DIRECT_ACCESS_MSRS] = {
95 { .index = MSR_STAR, .always = true },
96 { .index = MSR_IA32_SYSENTER_CS, .always = true },
97 #ifdef CONFIG_X86_64
98 { .index = MSR_GS_BASE, .always = true },
99 { .index = MSR_FS_BASE, .always = true },
100 { .index = MSR_KERNEL_GS_BASE, .always = true },
101 { .index = MSR_LSTAR, .always = true },
102 { .index = MSR_CSTAR, .always = true },
103 { .index = MSR_SYSCALL_MASK, .always = true },
104 #endif
105 { .index = MSR_IA32_SPEC_CTRL, .always = false },
106 { .index = MSR_IA32_PRED_CMD, .always = false },
107 { .index = MSR_IA32_LASTBRANCHFROMIP, .always = false },
108 { .index = MSR_IA32_LASTBRANCHTOIP, .always = false },
109 { .index = MSR_IA32_LASTINTFROMIP, .always = false },
110 { .index = MSR_IA32_LASTINTTOIP, .always = false },
111 { .index = MSR_INVALID, .always = false },
112 };
113
114 /* enable NPT for AMD64 and X86 with PAE */
115 #if defined(CONFIG_X86_64) || defined(CONFIG_X86_PAE)
116 bool npt_enabled = true;
117 #else
118 bool npt_enabled;
119 #endif
120
121 /*
122 * These 2 parameters are used to config the controls for Pause-Loop Exiting:
123 * pause_filter_count: On processors that support Pause filtering(indicated
124 * by CPUID Fn8000_000A_EDX), the VMCB provides a 16 bit pause filter
125 * count value. On VMRUN this value is loaded into an internal counter.
126 * Each time a pause instruction is executed, this counter is decremented
127 * until it reaches zero at which time a #VMEXIT is generated if pause
128 * intercept is enabled. Refer to AMD APM Vol 2 Section 15.14.4 Pause
129 * Intercept Filtering for more details.
130 * This also indicate if ple logic enabled.
131 *
132 * pause_filter_thresh: In addition, some processor families support advanced
133 * pause filtering (indicated by CPUID Fn8000_000A_EDX) upper bound on
134 * the amount of time a guest is allowed to execute in a pause loop.
135 * In this mode, a 16-bit pause filter threshold field is added in the
136 * VMCB. The threshold value is a cycle count that is used to reset the
137 * pause counter. As with simple pause filtering, VMRUN loads the pause
138 * count value from VMCB into an internal counter. Then, on each pause
139 * instruction the hardware checks the elapsed number of cycles since
140 * the most recent pause instruction against the pause filter threshold.
141 * If the elapsed cycle count is greater than the pause filter threshold,
142 * then the internal pause count is reloaded from the VMCB and execution
143 * continues. If the elapsed cycle count is less than the pause filter
144 * threshold, then the internal pause count is decremented. If the count
145 * value is less than zero and PAUSE intercept is enabled, a #VMEXIT is
146 * triggered. If advanced pause filtering is supported and pause filter
147 * threshold field is set to zero, the filter will operate in the simpler,
148 * count only mode.
149 */
150
151 static unsigned short pause_filter_thresh = KVM_DEFAULT_PLE_GAP;
152 module_param(pause_filter_thresh, ushort, 0444);
153
154 static unsigned short pause_filter_count = KVM_SVM_DEFAULT_PLE_WINDOW;
155 module_param(pause_filter_count, ushort, 0444);
156
157 /* Default doubles per-vcpu window every exit. */
158 static unsigned short pause_filter_count_grow = KVM_DEFAULT_PLE_WINDOW_GROW;
159 module_param(pause_filter_count_grow, ushort, 0444);
160
161 /* Default resets per-vcpu window every exit to pause_filter_count. */
162 static unsigned short pause_filter_count_shrink = KVM_DEFAULT_PLE_WINDOW_SHRINK;
163 module_param(pause_filter_count_shrink, ushort, 0444);
164
165 /* Default is to compute the maximum so we can never overflow. */
166 static unsigned short pause_filter_count_max = KVM_SVM_DEFAULT_PLE_WINDOW_MAX;
167 module_param(pause_filter_count_max, ushort, 0444);
168
169 /* allow nested paging (virtualized MMU) for all guests */
170 static int npt = true;
171 module_param(npt, int, S_IRUGO);
172
173 /* allow nested virtualization in KVM/SVM */
174 static int nested = true;
175 module_param(nested, int, S_IRUGO);
176
177 /* enable/disable Next RIP Save */
178 static int nrips = true;
179 module_param(nrips, int, 0444);
180
181 /* enable/disable Virtual VMLOAD VMSAVE */
182 static int vls = true;
183 module_param(vls, int, 0444);
184
185 /* enable/disable Virtual GIF */
186 static int vgif = true;
187 module_param(vgif, int, 0444);
188
189 /* enable/disable SEV support */
190 static int sev = IS_ENABLED(CONFIG_AMD_MEM_ENCRYPT_ACTIVE_BY_DEFAULT);
191 module_param(sev, int, 0444);
192
193 static bool __read_mostly dump_invalid_vmcb = 0;
194 module_param(dump_invalid_vmcb, bool, 0644);
195
196 static u8 rsm_ins_bytes[] = "\x0f\xaa";
197
198 static void svm_complete_interrupts(struct vcpu_svm *svm);
199
200 static unsigned long iopm_base;
201
202 struct kvm_ldttss_desc {
203 u16 limit0;
204 u16 base0;
205 unsigned base1:8, type:5, dpl:2, p:1;
206 unsigned limit1:4, zero0:3, g:1, base2:8;
207 u32 base3;
208 u32 zero1;
209 } __attribute__((packed));
210
211 DEFINE_PER_CPU(struct svm_cpu_data *, svm_data);
212
213 static const u32 msrpm_ranges[] = {0, 0xc0000000, 0xc0010000};
214
215 #define NUM_MSR_MAPS ARRAY_SIZE(msrpm_ranges)
216 #define MSRS_RANGE_SIZE 2048
217 #define MSRS_IN_RANGE (MSRS_RANGE_SIZE * 8 / 2)
218
svm_msrpm_offset(u32 msr)219 u32 svm_msrpm_offset(u32 msr)
220 {
221 u32 offset;
222 int i;
223
224 for (i = 0; i < NUM_MSR_MAPS; i++) {
225 if (msr < msrpm_ranges[i] ||
226 msr >= msrpm_ranges[i] + MSRS_IN_RANGE)
227 continue;
228
229 offset = (msr - msrpm_ranges[i]) / 4; /* 4 msrs per u8 */
230 offset += (i * MSRS_RANGE_SIZE); /* add range offset */
231
232 /* Now we have the u8 offset - but need the u32 offset */
233 return offset / 4;
234 }
235
236 /* MSR not in any range */
237 return MSR_INVALID;
238 }
239
240 #define MAX_INST_SIZE 15
241
clgi(void)242 static inline void clgi(void)
243 {
244 asm volatile (__ex("clgi"));
245 }
246
stgi(void)247 static inline void stgi(void)
248 {
249 asm volatile (__ex("stgi"));
250 }
251
invlpga(unsigned long addr,u32 asid)252 static inline void invlpga(unsigned long addr, u32 asid)
253 {
254 asm volatile (__ex("invlpga %1, %0") : : "c"(asid), "a"(addr));
255 }
256
get_max_npt_level(void)257 static int get_max_npt_level(void)
258 {
259 #ifdef CONFIG_X86_64
260 return PT64_ROOT_4LEVEL;
261 #else
262 return PT32E_ROOT_LEVEL;
263 #endif
264 }
265
svm_set_efer(struct kvm_vcpu * vcpu,u64 efer)266 int svm_set_efer(struct kvm_vcpu *vcpu, u64 efer)
267 {
268 struct vcpu_svm *svm = to_svm(vcpu);
269 u64 old_efer = vcpu->arch.efer;
270 vcpu->arch.efer = efer;
271
272 if (!npt_enabled) {
273 /* Shadow paging assumes NX to be available. */
274 efer |= EFER_NX;
275
276 if (!(efer & EFER_LMA))
277 efer &= ~EFER_LME;
278 }
279
280 if ((old_efer & EFER_SVME) != (efer & EFER_SVME)) {
281 if (!(efer & EFER_SVME)) {
282 svm_leave_nested(vcpu);
283 svm_set_gif(svm, true);
284
285 /*
286 * Free the nested guest state, unless we are in SMM.
287 * In this case we will return to the nested guest
288 * as soon as we leave SMM.
289 */
290 if (!is_smm(&svm->vcpu))
291 svm_free_nested(svm);
292
293 } else {
294 int ret = svm_allocate_nested(svm);
295
296 if (ret) {
297 vcpu->arch.efer = old_efer;
298 return ret;
299 }
300 }
301 }
302
303 svm->vmcb->save.efer = efer | EFER_SVME;
304 vmcb_mark_dirty(svm->vmcb, VMCB_CR);
305 return 0;
306 }
307
svm_get_interrupt_shadow(struct kvm_vcpu * vcpu)308 static u32 svm_get_interrupt_shadow(struct kvm_vcpu *vcpu)
309 {
310 struct vcpu_svm *svm = to_svm(vcpu);
311 u32 ret = 0;
312
313 if (svm->vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK)
314 ret = KVM_X86_SHADOW_INT_STI | KVM_X86_SHADOW_INT_MOV_SS;
315 return ret;
316 }
317
svm_set_interrupt_shadow(struct kvm_vcpu * vcpu,int mask)318 static void svm_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
319 {
320 struct vcpu_svm *svm = to_svm(vcpu);
321
322 if (mask == 0)
323 svm->vmcb->control.int_state &= ~SVM_INTERRUPT_SHADOW_MASK;
324 else
325 svm->vmcb->control.int_state |= SVM_INTERRUPT_SHADOW_MASK;
326
327 }
328
skip_emulated_instruction(struct kvm_vcpu * vcpu)329 static int skip_emulated_instruction(struct kvm_vcpu *vcpu)
330 {
331 struct vcpu_svm *svm = to_svm(vcpu);
332
333 if (nrips && svm->vmcb->control.next_rip != 0) {
334 WARN_ON_ONCE(!static_cpu_has(X86_FEATURE_NRIPS));
335 svm->next_rip = svm->vmcb->control.next_rip;
336 }
337
338 if (!svm->next_rip) {
339 if (!kvm_emulate_instruction(vcpu, EMULTYPE_SKIP))
340 return 0;
341 } else {
342 kvm_rip_write(vcpu, svm->next_rip);
343 }
344 svm_set_interrupt_shadow(vcpu, 0);
345
346 return 1;
347 }
348
svm_queue_exception(struct kvm_vcpu * vcpu)349 static void svm_queue_exception(struct kvm_vcpu *vcpu)
350 {
351 struct vcpu_svm *svm = to_svm(vcpu);
352 unsigned nr = vcpu->arch.exception.nr;
353 bool has_error_code = vcpu->arch.exception.has_error_code;
354 u32 error_code = vcpu->arch.exception.error_code;
355
356 kvm_deliver_exception_payload(&svm->vcpu);
357
358 if (nr == BP_VECTOR && !nrips) {
359 unsigned long rip, old_rip = kvm_rip_read(&svm->vcpu);
360
361 /*
362 * For guest debugging where we have to reinject #BP if some
363 * INT3 is guest-owned:
364 * Emulate nRIP by moving RIP forward. Will fail if injection
365 * raises a fault that is not intercepted. Still better than
366 * failing in all cases.
367 */
368 (void)skip_emulated_instruction(&svm->vcpu);
369 rip = kvm_rip_read(&svm->vcpu);
370 svm->int3_rip = rip + svm->vmcb->save.cs.base;
371 svm->int3_injected = rip - old_rip;
372 }
373
374 svm->vmcb->control.event_inj = nr
375 | SVM_EVTINJ_VALID
376 | (has_error_code ? SVM_EVTINJ_VALID_ERR : 0)
377 | SVM_EVTINJ_TYPE_EXEPT;
378 svm->vmcb->control.event_inj_err = error_code;
379 }
380
svm_init_erratum_383(void)381 static void svm_init_erratum_383(void)
382 {
383 u32 low, high;
384 int err;
385 u64 val;
386
387 if (!static_cpu_has_bug(X86_BUG_AMD_TLB_MMATCH))
388 return;
389
390 /* Use _safe variants to not break nested virtualization */
391 val = native_read_msr_safe(MSR_AMD64_DC_CFG, &err);
392 if (err)
393 return;
394
395 val |= (1ULL << 47);
396
397 low = lower_32_bits(val);
398 high = upper_32_bits(val);
399
400 native_write_msr_safe(MSR_AMD64_DC_CFG, low, high);
401
402 erratum_383_found = true;
403 }
404
svm_init_osvw(struct kvm_vcpu * vcpu)405 static void svm_init_osvw(struct kvm_vcpu *vcpu)
406 {
407 /*
408 * Guests should see errata 400 and 415 as fixed (assuming that
409 * HLT and IO instructions are intercepted).
410 */
411 vcpu->arch.osvw.length = (osvw_len >= 3) ? (osvw_len) : 3;
412 vcpu->arch.osvw.status = osvw_status & ~(6ULL);
413
414 /*
415 * By increasing VCPU's osvw.length to 3 we are telling the guest that
416 * all osvw.status bits inside that length, including bit 0 (which is
417 * reserved for erratum 298), are valid. However, if host processor's
418 * osvw_len is 0 then osvw_status[0] carries no information. We need to
419 * be conservative here and therefore we tell the guest that erratum 298
420 * is present (because we really don't know).
421 */
422 if (osvw_len == 0 && boot_cpu_data.x86 == 0x10)
423 vcpu->arch.osvw.status |= 1;
424 }
425
has_svm(void)426 static int has_svm(void)
427 {
428 const char *msg;
429
430 if (!cpu_has_svm(&msg)) {
431 printk(KERN_INFO "has_svm: %s\n", msg);
432 return 0;
433 }
434
435 if (sev_active()) {
436 pr_info("KVM is unsupported when running as an SEV guest\n");
437 return 0;
438 }
439
440 return 1;
441 }
442
svm_hardware_disable(void)443 static void svm_hardware_disable(void)
444 {
445 /* Make sure we clean up behind us */
446 if (static_cpu_has(X86_FEATURE_TSCRATEMSR))
447 wrmsrl(MSR_AMD64_TSC_RATIO, TSC_RATIO_DEFAULT);
448
449 cpu_svm_disable();
450
451 amd_pmu_disable_virt();
452 }
453
svm_hardware_enable(void)454 static int svm_hardware_enable(void)
455 {
456
457 struct svm_cpu_data *sd;
458 uint64_t efer;
459 struct desc_struct *gdt;
460 int me = raw_smp_processor_id();
461
462 rdmsrl(MSR_EFER, efer);
463 if (efer & EFER_SVME)
464 return -EBUSY;
465
466 if (!has_svm()) {
467 pr_err("%s: err EOPNOTSUPP on %d\n", __func__, me);
468 return -EINVAL;
469 }
470 sd = per_cpu(svm_data, me);
471 if (!sd) {
472 pr_err("%s: svm_data is NULL on %d\n", __func__, me);
473 return -EINVAL;
474 }
475
476 sd->asid_generation = 1;
477 sd->max_asid = cpuid_ebx(SVM_CPUID_FUNC) - 1;
478 sd->next_asid = sd->max_asid + 1;
479 sd->min_asid = max_sev_asid + 1;
480
481 gdt = get_current_gdt_rw();
482 sd->tss_desc = (struct kvm_ldttss_desc *)(gdt + GDT_ENTRY_TSS);
483
484 wrmsrl(MSR_EFER, efer | EFER_SVME);
485
486 wrmsrl(MSR_VM_HSAVE_PA, page_to_pfn(sd->save_area) << PAGE_SHIFT);
487
488 if (static_cpu_has(X86_FEATURE_TSCRATEMSR)) {
489 wrmsrl(MSR_AMD64_TSC_RATIO, TSC_RATIO_DEFAULT);
490 __this_cpu_write(current_tsc_ratio, TSC_RATIO_DEFAULT);
491 }
492
493
494 /*
495 * Get OSVW bits.
496 *
497 * Note that it is possible to have a system with mixed processor
498 * revisions and therefore different OSVW bits. If bits are not the same
499 * on different processors then choose the worst case (i.e. if erratum
500 * is present on one processor and not on another then assume that the
501 * erratum is present everywhere).
502 */
503 if (cpu_has(&boot_cpu_data, X86_FEATURE_OSVW)) {
504 uint64_t len, status = 0;
505 int err;
506
507 len = native_read_msr_safe(MSR_AMD64_OSVW_ID_LENGTH, &err);
508 if (!err)
509 status = native_read_msr_safe(MSR_AMD64_OSVW_STATUS,
510 &err);
511
512 if (err)
513 osvw_status = osvw_len = 0;
514 else {
515 if (len < osvw_len)
516 osvw_len = len;
517 osvw_status |= status;
518 osvw_status &= (1ULL << osvw_len) - 1;
519 }
520 } else
521 osvw_status = osvw_len = 0;
522
523 svm_init_erratum_383();
524
525 amd_pmu_enable_virt();
526
527 return 0;
528 }
529
svm_cpu_uninit(int cpu)530 static void svm_cpu_uninit(int cpu)
531 {
532 struct svm_cpu_data *sd = per_cpu(svm_data, cpu);
533
534 if (!sd)
535 return;
536
537 per_cpu(svm_data, cpu) = NULL;
538 kfree(sd->sev_vmcbs);
539 __free_page(sd->save_area);
540 kfree(sd);
541 }
542
svm_cpu_init(int cpu)543 static int svm_cpu_init(int cpu)
544 {
545 struct svm_cpu_data *sd;
546
547 sd = kzalloc(sizeof(struct svm_cpu_data), GFP_KERNEL);
548 if (!sd)
549 return -ENOMEM;
550 sd->cpu = cpu;
551 sd->save_area = alloc_page(GFP_KERNEL);
552 if (!sd->save_area)
553 goto free_cpu_data;
554
555 if (svm_sev_enabled()) {
556 sd->sev_vmcbs = kmalloc_array(max_sev_asid + 1,
557 sizeof(void *),
558 GFP_KERNEL);
559 if (!sd->sev_vmcbs)
560 goto free_save_area;
561 }
562
563 per_cpu(svm_data, cpu) = sd;
564
565 return 0;
566
567 free_save_area:
568 __free_page(sd->save_area);
569 free_cpu_data:
570 kfree(sd);
571 return -ENOMEM;
572
573 }
574
direct_access_msr_slot(u32 msr)575 static int direct_access_msr_slot(u32 msr)
576 {
577 u32 i;
578
579 for (i = 0; direct_access_msrs[i].index != MSR_INVALID; i++)
580 if (direct_access_msrs[i].index == msr)
581 return i;
582
583 return -ENOENT;
584 }
585
set_shadow_msr_intercept(struct kvm_vcpu * vcpu,u32 msr,int read,int write)586 static void set_shadow_msr_intercept(struct kvm_vcpu *vcpu, u32 msr, int read,
587 int write)
588 {
589 struct vcpu_svm *svm = to_svm(vcpu);
590 int slot = direct_access_msr_slot(msr);
591
592 if (slot == -ENOENT)
593 return;
594
595 /* Set the shadow bitmaps to the desired intercept states */
596 if (read)
597 set_bit(slot, svm->shadow_msr_intercept.read);
598 else
599 clear_bit(slot, svm->shadow_msr_intercept.read);
600
601 if (write)
602 set_bit(slot, svm->shadow_msr_intercept.write);
603 else
604 clear_bit(slot, svm->shadow_msr_intercept.write);
605 }
606
valid_msr_intercept(u32 index)607 static bool valid_msr_intercept(u32 index)
608 {
609 return direct_access_msr_slot(index) != -ENOENT;
610 }
611
msr_write_intercepted(struct kvm_vcpu * vcpu,u32 msr)612 static bool msr_write_intercepted(struct kvm_vcpu *vcpu, u32 msr)
613 {
614 u8 bit_write;
615 unsigned long tmp;
616 u32 offset;
617 u32 *msrpm;
618
619 msrpm = is_guest_mode(vcpu) ? to_svm(vcpu)->nested.msrpm:
620 to_svm(vcpu)->msrpm;
621
622 offset = svm_msrpm_offset(msr);
623 bit_write = 2 * (msr & 0x0f) + 1;
624 tmp = msrpm[offset];
625
626 BUG_ON(offset == MSR_INVALID);
627
628 return !!test_bit(bit_write, &tmp);
629 }
630
set_msr_interception_bitmap(struct kvm_vcpu * vcpu,u32 * msrpm,u32 msr,int read,int write)631 static void set_msr_interception_bitmap(struct kvm_vcpu *vcpu, u32 *msrpm,
632 u32 msr, int read, int write)
633 {
634 u8 bit_read, bit_write;
635 unsigned long tmp;
636 u32 offset;
637
638 /*
639 * If this warning triggers extend the direct_access_msrs list at the
640 * beginning of the file
641 */
642 WARN_ON(!valid_msr_intercept(msr));
643
644 /* Enforce non allowed MSRs to trap */
645 if (read && !kvm_msr_allowed(vcpu, msr, KVM_MSR_FILTER_READ))
646 read = 0;
647
648 if (write && !kvm_msr_allowed(vcpu, msr, KVM_MSR_FILTER_WRITE))
649 write = 0;
650
651 offset = svm_msrpm_offset(msr);
652 bit_read = 2 * (msr & 0x0f);
653 bit_write = 2 * (msr & 0x0f) + 1;
654 tmp = msrpm[offset];
655
656 BUG_ON(offset == MSR_INVALID);
657
658 read ? clear_bit(bit_read, &tmp) : set_bit(bit_read, &tmp);
659 write ? clear_bit(bit_write, &tmp) : set_bit(bit_write, &tmp);
660
661 msrpm[offset] = tmp;
662 }
663
set_msr_interception(struct kvm_vcpu * vcpu,u32 * msrpm,u32 msr,int read,int write)664 static void set_msr_interception(struct kvm_vcpu *vcpu, u32 *msrpm, u32 msr,
665 int read, int write)
666 {
667 set_shadow_msr_intercept(vcpu, msr, read, write);
668 set_msr_interception_bitmap(vcpu, msrpm, msr, read, write);
669 }
670
svm_vcpu_alloc_msrpm(void)671 u32 *svm_vcpu_alloc_msrpm(void)
672 {
673 struct page *pages = alloc_pages(GFP_KERNEL_ACCOUNT, MSRPM_ALLOC_ORDER);
674 u32 *msrpm;
675
676 if (!pages)
677 return NULL;
678
679 msrpm = page_address(pages);
680 memset(msrpm, 0xff, PAGE_SIZE * (1 << MSRPM_ALLOC_ORDER));
681
682 return msrpm;
683 }
684
svm_vcpu_init_msrpm(struct kvm_vcpu * vcpu,u32 * msrpm)685 void svm_vcpu_init_msrpm(struct kvm_vcpu *vcpu, u32 *msrpm)
686 {
687 int i;
688
689 for (i = 0; direct_access_msrs[i].index != MSR_INVALID; i++) {
690 if (!direct_access_msrs[i].always)
691 continue;
692 set_msr_interception(vcpu, msrpm, direct_access_msrs[i].index, 1, 1);
693 }
694 }
695
696
svm_vcpu_free_msrpm(u32 * msrpm)697 void svm_vcpu_free_msrpm(u32 *msrpm)
698 {
699 __free_pages(virt_to_page(msrpm), MSRPM_ALLOC_ORDER);
700 }
701
svm_msr_filter_changed(struct kvm_vcpu * vcpu)702 static void svm_msr_filter_changed(struct kvm_vcpu *vcpu)
703 {
704 struct vcpu_svm *svm = to_svm(vcpu);
705 u32 i;
706
707 /*
708 * Set intercept permissions for all direct access MSRs again. They
709 * will automatically get filtered through the MSR filter, so we are
710 * back in sync after this.
711 */
712 for (i = 0; direct_access_msrs[i].index != MSR_INVALID; i++) {
713 u32 msr = direct_access_msrs[i].index;
714 u32 read = test_bit(i, svm->shadow_msr_intercept.read);
715 u32 write = test_bit(i, svm->shadow_msr_intercept.write);
716
717 set_msr_interception_bitmap(vcpu, svm->msrpm, msr, read, write);
718 }
719 }
720
add_msr_offset(u32 offset)721 static void add_msr_offset(u32 offset)
722 {
723 int i;
724
725 for (i = 0; i < MSRPM_OFFSETS; ++i) {
726
727 /* Offset already in list? */
728 if (msrpm_offsets[i] == offset)
729 return;
730
731 /* Slot used by another offset? */
732 if (msrpm_offsets[i] != MSR_INVALID)
733 continue;
734
735 /* Add offset to list */
736 msrpm_offsets[i] = offset;
737
738 return;
739 }
740
741 /*
742 * If this BUG triggers the msrpm_offsets table has an overflow. Just
743 * increase MSRPM_OFFSETS in this case.
744 */
745 BUG();
746 }
747
init_msrpm_offsets(void)748 static void init_msrpm_offsets(void)
749 {
750 int i;
751
752 memset(msrpm_offsets, 0xff, sizeof(msrpm_offsets));
753
754 for (i = 0; direct_access_msrs[i].index != MSR_INVALID; i++) {
755 u32 offset;
756
757 offset = svm_msrpm_offset(direct_access_msrs[i].index);
758 BUG_ON(offset == MSR_INVALID);
759
760 add_msr_offset(offset);
761 }
762 }
763
svm_enable_lbrv(struct kvm_vcpu * vcpu)764 static void svm_enable_lbrv(struct kvm_vcpu *vcpu)
765 {
766 struct vcpu_svm *svm = to_svm(vcpu);
767
768 svm->vmcb->control.virt_ext |= LBR_CTL_ENABLE_MASK;
769 set_msr_interception(vcpu, svm->msrpm, MSR_IA32_LASTBRANCHFROMIP, 1, 1);
770 set_msr_interception(vcpu, svm->msrpm, MSR_IA32_LASTBRANCHTOIP, 1, 1);
771 set_msr_interception(vcpu, svm->msrpm, MSR_IA32_LASTINTFROMIP, 1, 1);
772 set_msr_interception(vcpu, svm->msrpm, MSR_IA32_LASTINTTOIP, 1, 1);
773 }
774
svm_disable_lbrv(struct kvm_vcpu * vcpu)775 static void svm_disable_lbrv(struct kvm_vcpu *vcpu)
776 {
777 struct vcpu_svm *svm = to_svm(vcpu);
778
779 svm->vmcb->control.virt_ext &= ~LBR_CTL_ENABLE_MASK;
780 set_msr_interception(vcpu, svm->msrpm, MSR_IA32_LASTBRANCHFROMIP, 0, 0);
781 set_msr_interception(vcpu, svm->msrpm, MSR_IA32_LASTBRANCHTOIP, 0, 0);
782 set_msr_interception(vcpu, svm->msrpm, MSR_IA32_LASTINTFROMIP, 0, 0);
783 set_msr_interception(vcpu, svm->msrpm, MSR_IA32_LASTINTTOIP, 0, 0);
784 }
785
disable_nmi_singlestep(struct vcpu_svm * svm)786 void disable_nmi_singlestep(struct vcpu_svm *svm)
787 {
788 svm->nmi_singlestep = false;
789
790 if (!(svm->vcpu.guest_debug & KVM_GUESTDBG_SINGLESTEP)) {
791 /* Clear our flags if they were not set by the guest */
792 if (!(svm->nmi_singlestep_guest_rflags & X86_EFLAGS_TF))
793 svm->vmcb->save.rflags &= ~X86_EFLAGS_TF;
794 if (!(svm->nmi_singlestep_guest_rflags & X86_EFLAGS_RF))
795 svm->vmcb->save.rflags &= ~X86_EFLAGS_RF;
796 }
797 }
798
grow_ple_window(struct kvm_vcpu * vcpu)799 static void grow_ple_window(struct kvm_vcpu *vcpu)
800 {
801 struct vcpu_svm *svm = to_svm(vcpu);
802 struct vmcb_control_area *control = &svm->vmcb->control;
803 int old = control->pause_filter_count;
804
805 control->pause_filter_count = __grow_ple_window(old,
806 pause_filter_count,
807 pause_filter_count_grow,
808 pause_filter_count_max);
809
810 if (control->pause_filter_count != old) {
811 vmcb_mark_dirty(svm->vmcb, VMCB_INTERCEPTS);
812 trace_kvm_ple_window_update(vcpu->vcpu_id,
813 control->pause_filter_count, old);
814 }
815 }
816
shrink_ple_window(struct kvm_vcpu * vcpu)817 static void shrink_ple_window(struct kvm_vcpu *vcpu)
818 {
819 struct vcpu_svm *svm = to_svm(vcpu);
820 struct vmcb_control_area *control = &svm->vmcb->control;
821 int old = control->pause_filter_count;
822
823 control->pause_filter_count =
824 __shrink_ple_window(old,
825 pause_filter_count,
826 pause_filter_count_shrink,
827 pause_filter_count);
828 if (control->pause_filter_count != old) {
829 vmcb_mark_dirty(svm->vmcb, VMCB_INTERCEPTS);
830 trace_kvm_ple_window_update(vcpu->vcpu_id,
831 control->pause_filter_count, old);
832 }
833 }
834
835 /*
836 * The default MMIO mask is a single bit (excluding the present bit),
837 * which could conflict with the memory encryption bit. Check for
838 * memory encryption support and override the default MMIO mask if
839 * memory encryption is enabled.
840 */
svm_adjust_mmio_mask(void)841 static __init void svm_adjust_mmio_mask(void)
842 {
843 unsigned int enc_bit, mask_bit;
844 u64 msr, mask;
845
846 /* If there is no memory encryption support, use existing mask */
847 if (cpuid_eax(0x80000000) < 0x8000001f)
848 return;
849
850 /* If memory encryption is not enabled, use existing mask */
851 rdmsrl(MSR_K8_SYSCFG, msr);
852 if (!(msr & MSR_K8_SYSCFG_MEM_ENCRYPT))
853 return;
854
855 enc_bit = cpuid_ebx(0x8000001f) & 0x3f;
856 mask_bit = boot_cpu_data.x86_phys_bits;
857
858 /* Increment the mask bit if it is the same as the encryption bit */
859 if (enc_bit == mask_bit)
860 mask_bit++;
861
862 /*
863 * If the mask bit location is below 52, then some bits above the
864 * physical addressing limit will always be reserved, so use the
865 * rsvd_bits() function to generate the mask. This mask, along with
866 * the present bit, will be used to generate a page fault with
867 * PFER.RSV = 1.
868 *
869 * If the mask bit location is 52 (or above), then clear the mask.
870 */
871 mask = (mask_bit < 52) ? rsvd_bits(mask_bit, 51) | PT_PRESENT_MASK : 0;
872
873 kvm_mmu_set_mmio_spte_mask(mask, PT_WRITABLE_MASK | PT_USER_MASK);
874 }
875
svm_hardware_teardown(void)876 static void svm_hardware_teardown(void)
877 {
878 int cpu;
879
880 if (svm_sev_enabled())
881 sev_hardware_teardown();
882
883 for_each_possible_cpu(cpu)
884 svm_cpu_uninit(cpu);
885
886 __free_pages(pfn_to_page(iopm_base >> PAGE_SHIFT), IOPM_ALLOC_ORDER);
887 iopm_base = 0;
888 }
889
svm_set_cpu_caps(void)890 static __init void svm_set_cpu_caps(void)
891 {
892 kvm_set_cpu_caps();
893
894 supported_xss = 0;
895
896 /* CPUID 0x80000001 and 0x8000000A (SVM features) */
897 if (nested) {
898 kvm_cpu_cap_set(X86_FEATURE_SVM);
899
900 if (nrips)
901 kvm_cpu_cap_set(X86_FEATURE_NRIPS);
902
903 if (npt_enabled)
904 kvm_cpu_cap_set(X86_FEATURE_NPT);
905 }
906
907 /* CPUID 0x80000008 */
908 if (boot_cpu_has(X86_FEATURE_LS_CFG_SSBD) ||
909 boot_cpu_has(X86_FEATURE_AMD_SSBD))
910 kvm_cpu_cap_set(X86_FEATURE_VIRT_SSBD);
911
912 /* Enable INVPCID feature */
913 kvm_cpu_cap_check_and_set(X86_FEATURE_INVPCID);
914 }
915
svm_hardware_setup(void)916 static __init int svm_hardware_setup(void)
917 {
918 int cpu;
919 struct page *iopm_pages;
920 void *iopm_va;
921 int r;
922
923 iopm_pages = alloc_pages(GFP_KERNEL, IOPM_ALLOC_ORDER);
924
925 if (!iopm_pages)
926 return -ENOMEM;
927
928 iopm_va = page_address(iopm_pages);
929 memset(iopm_va, 0xff, PAGE_SIZE * (1 << IOPM_ALLOC_ORDER));
930 iopm_base = page_to_pfn(iopm_pages) << PAGE_SHIFT;
931
932 init_msrpm_offsets();
933
934 supported_xcr0 &= ~(XFEATURE_MASK_BNDREGS | XFEATURE_MASK_BNDCSR);
935
936 if (boot_cpu_has(X86_FEATURE_NX))
937 kvm_enable_efer_bits(EFER_NX);
938
939 if (boot_cpu_has(X86_FEATURE_FXSR_OPT))
940 kvm_enable_efer_bits(EFER_FFXSR);
941
942 if (boot_cpu_has(X86_FEATURE_TSCRATEMSR)) {
943 kvm_has_tsc_control = true;
944 kvm_max_tsc_scaling_ratio = TSC_RATIO_MAX;
945 kvm_tsc_scaling_ratio_frac_bits = 32;
946 }
947
948 /* Check for pause filtering support */
949 if (!boot_cpu_has(X86_FEATURE_PAUSEFILTER)) {
950 pause_filter_count = 0;
951 pause_filter_thresh = 0;
952 } else if (!boot_cpu_has(X86_FEATURE_PFTHRESHOLD)) {
953 pause_filter_thresh = 0;
954 }
955
956 if (nested) {
957 printk(KERN_INFO "kvm: Nested Virtualization enabled\n");
958 kvm_enable_efer_bits(EFER_SVME | EFER_LMSLE);
959 }
960
961 if (sev) {
962 if (boot_cpu_has(X86_FEATURE_SEV) &&
963 IS_ENABLED(CONFIG_KVM_AMD_SEV)) {
964 r = sev_hardware_setup();
965 if (r)
966 sev = false;
967 } else {
968 sev = false;
969 }
970 }
971
972 svm_adjust_mmio_mask();
973
974 for_each_possible_cpu(cpu) {
975 r = svm_cpu_init(cpu);
976 if (r)
977 goto err;
978 }
979
980 if (!boot_cpu_has(X86_FEATURE_NPT))
981 npt_enabled = false;
982
983 if (npt_enabled && !npt)
984 npt_enabled = false;
985
986 kvm_configure_mmu(npt_enabled, get_max_npt_level(), PG_LEVEL_1G);
987 pr_info("kvm: Nested Paging %sabled\n", npt_enabled ? "en" : "dis");
988
989 if (nrips) {
990 if (!boot_cpu_has(X86_FEATURE_NRIPS))
991 nrips = false;
992 }
993
994 if (avic) {
995 if (!npt_enabled ||
996 !boot_cpu_has(X86_FEATURE_AVIC) ||
997 !IS_ENABLED(CONFIG_X86_LOCAL_APIC)) {
998 avic = false;
999 } else {
1000 pr_info("AVIC enabled\n");
1001
1002 amd_iommu_register_ga_log_notifier(&avic_ga_log_notifier);
1003 }
1004 }
1005
1006 if (vls) {
1007 if (!npt_enabled ||
1008 !boot_cpu_has(X86_FEATURE_V_VMSAVE_VMLOAD) ||
1009 !IS_ENABLED(CONFIG_X86_64)) {
1010 vls = false;
1011 } else {
1012 pr_info("Virtual VMLOAD VMSAVE supported\n");
1013 }
1014 }
1015
1016 if (vgif) {
1017 if (!boot_cpu_has(X86_FEATURE_VGIF))
1018 vgif = false;
1019 else
1020 pr_info("Virtual GIF supported\n");
1021 }
1022
1023 svm_set_cpu_caps();
1024
1025 /*
1026 * It seems that on AMD processors PTE's accessed bit is
1027 * being set by the CPU hardware before the NPF vmexit.
1028 * This is not expected behaviour and our tests fail because
1029 * of it.
1030 * A workaround here is to disable support for
1031 * GUEST_MAXPHYADDR < HOST_MAXPHYADDR if NPT is enabled.
1032 * In this case userspace can know if there is support using
1033 * KVM_CAP_SMALLER_MAXPHYADDR extension and decide how to handle
1034 * it
1035 * If future AMD CPU models change the behaviour described above,
1036 * this variable can be changed accordingly
1037 */
1038 allow_smaller_maxphyaddr = !npt_enabled;
1039
1040 return 0;
1041
1042 err:
1043 svm_hardware_teardown();
1044 return r;
1045 }
1046
init_seg(struct vmcb_seg * seg)1047 static void init_seg(struct vmcb_seg *seg)
1048 {
1049 seg->selector = 0;
1050 seg->attrib = SVM_SELECTOR_P_MASK | SVM_SELECTOR_S_MASK |
1051 SVM_SELECTOR_WRITE_MASK; /* Read/Write Data Segment */
1052 seg->limit = 0xffff;
1053 seg->base = 0;
1054 }
1055
init_sys_seg(struct vmcb_seg * seg,uint32_t type)1056 static void init_sys_seg(struct vmcb_seg *seg, uint32_t type)
1057 {
1058 seg->selector = 0;
1059 seg->attrib = SVM_SELECTOR_P_MASK | type;
1060 seg->limit = 0xffff;
1061 seg->base = 0;
1062 }
1063
svm_write_l1_tsc_offset(struct kvm_vcpu * vcpu,u64 offset)1064 static u64 svm_write_l1_tsc_offset(struct kvm_vcpu *vcpu, u64 offset)
1065 {
1066 struct vcpu_svm *svm = to_svm(vcpu);
1067 u64 g_tsc_offset = 0;
1068
1069 if (is_guest_mode(vcpu)) {
1070 /* Write L1's TSC offset. */
1071 g_tsc_offset = svm->vmcb->control.tsc_offset -
1072 svm->nested.hsave->control.tsc_offset;
1073 svm->nested.hsave->control.tsc_offset = offset;
1074 }
1075
1076 trace_kvm_write_tsc_offset(vcpu->vcpu_id,
1077 svm->vmcb->control.tsc_offset - g_tsc_offset,
1078 offset);
1079
1080 svm->vmcb->control.tsc_offset = offset + g_tsc_offset;
1081
1082 vmcb_mark_dirty(svm->vmcb, VMCB_INTERCEPTS);
1083 return svm->vmcb->control.tsc_offset;
1084 }
1085
svm_check_invpcid(struct vcpu_svm * svm)1086 static void svm_check_invpcid(struct vcpu_svm *svm)
1087 {
1088 /*
1089 * Intercept INVPCID if shadow paging is enabled to sync/free shadow
1090 * roots, or if INVPCID is disabled in the guest to inject #UD.
1091 */
1092 if (kvm_cpu_cap_has(X86_FEATURE_INVPCID)) {
1093 if (!npt_enabled ||
1094 !guest_cpuid_has(&svm->vcpu, X86_FEATURE_INVPCID))
1095 svm_set_intercept(svm, INTERCEPT_INVPCID);
1096 else
1097 svm_clr_intercept(svm, INTERCEPT_INVPCID);
1098 }
1099 }
1100
init_vmcb(struct vcpu_svm * svm)1101 static void init_vmcb(struct vcpu_svm *svm)
1102 {
1103 struct vmcb_control_area *control = &svm->vmcb->control;
1104 struct vmcb_save_area *save = &svm->vmcb->save;
1105
1106 svm->vcpu.arch.hflags = 0;
1107
1108 svm_set_intercept(svm, INTERCEPT_CR0_READ);
1109 svm_set_intercept(svm, INTERCEPT_CR3_READ);
1110 svm_set_intercept(svm, INTERCEPT_CR4_READ);
1111 svm_set_intercept(svm, INTERCEPT_CR0_WRITE);
1112 svm_set_intercept(svm, INTERCEPT_CR3_WRITE);
1113 svm_set_intercept(svm, INTERCEPT_CR4_WRITE);
1114 if (!kvm_vcpu_apicv_active(&svm->vcpu))
1115 svm_set_intercept(svm, INTERCEPT_CR8_WRITE);
1116
1117 set_dr_intercepts(svm);
1118
1119 set_exception_intercept(svm, PF_VECTOR);
1120 set_exception_intercept(svm, UD_VECTOR);
1121 set_exception_intercept(svm, MC_VECTOR);
1122 set_exception_intercept(svm, AC_VECTOR);
1123 set_exception_intercept(svm, DB_VECTOR);
1124 /*
1125 * Guest access to VMware backdoor ports could legitimately
1126 * trigger #GP because of TSS I/O permission bitmap.
1127 * We intercept those #GP and allow access to them anyway
1128 * as VMware does.
1129 */
1130 if (enable_vmware_backdoor)
1131 set_exception_intercept(svm, GP_VECTOR);
1132
1133 svm_set_intercept(svm, INTERCEPT_INTR);
1134 svm_set_intercept(svm, INTERCEPT_NMI);
1135 svm_set_intercept(svm, INTERCEPT_SMI);
1136 svm_set_intercept(svm, INTERCEPT_SELECTIVE_CR0);
1137 svm_set_intercept(svm, INTERCEPT_RDPMC);
1138 svm_set_intercept(svm, INTERCEPT_CPUID);
1139 svm_set_intercept(svm, INTERCEPT_INVD);
1140 svm_set_intercept(svm, INTERCEPT_INVLPG);
1141 svm_set_intercept(svm, INTERCEPT_INVLPGA);
1142 svm_set_intercept(svm, INTERCEPT_IOIO_PROT);
1143 svm_set_intercept(svm, INTERCEPT_MSR_PROT);
1144 svm_set_intercept(svm, INTERCEPT_TASK_SWITCH);
1145 svm_set_intercept(svm, INTERCEPT_SHUTDOWN);
1146 svm_set_intercept(svm, INTERCEPT_VMRUN);
1147 svm_set_intercept(svm, INTERCEPT_VMMCALL);
1148 svm_set_intercept(svm, INTERCEPT_VMLOAD);
1149 svm_set_intercept(svm, INTERCEPT_VMSAVE);
1150 svm_set_intercept(svm, INTERCEPT_STGI);
1151 svm_set_intercept(svm, INTERCEPT_CLGI);
1152 svm_set_intercept(svm, INTERCEPT_SKINIT);
1153 svm_set_intercept(svm, INTERCEPT_WBINVD);
1154 svm_set_intercept(svm, INTERCEPT_XSETBV);
1155 svm_set_intercept(svm, INTERCEPT_RDPRU);
1156 svm_set_intercept(svm, INTERCEPT_RSM);
1157
1158 if (!kvm_mwait_in_guest(svm->vcpu.kvm)) {
1159 svm_set_intercept(svm, INTERCEPT_MONITOR);
1160 svm_set_intercept(svm, INTERCEPT_MWAIT);
1161 }
1162
1163 if (!kvm_hlt_in_guest(svm->vcpu.kvm))
1164 svm_set_intercept(svm, INTERCEPT_HLT);
1165
1166 control->iopm_base_pa = __sme_set(iopm_base);
1167 control->msrpm_base_pa = __sme_set(__pa(svm->msrpm));
1168 control->int_ctl = V_INTR_MASKING_MASK;
1169
1170 init_seg(&save->es);
1171 init_seg(&save->ss);
1172 init_seg(&save->ds);
1173 init_seg(&save->fs);
1174 init_seg(&save->gs);
1175
1176 save->cs.selector = 0xf000;
1177 save->cs.base = 0xffff0000;
1178 /* Executable/Readable Code Segment */
1179 save->cs.attrib = SVM_SELECTOR_READ_MASK | SVM_SELECTOR_P_MASK |
1180 SVM_SELECTOR_S_MASK | SVM_SELECTOR_CODE_MASK;
1181 save->cs.limit = 0xffff;
1182
1183 save->gdtr.limit = 0xffff;
1184 save->idtr.limit = 0xffff;
1185
1186 init_sys_seg(&save->ldtr, SEG_TYPE_LDT);
1187 init_sys_seg(&save->tr, SEG_TYPE_BUSY_TSS16);
1188
1189 svm_set_cr4(&svm->vcpu, 0);
1190 svm_set_efer(&svm->vcpu, 0);
1191 save->dr6 = 0xffff0ff0;
1192 kvm_set_rflags(&svm->vcpu, 2);
1193 save->rip = 0x0000fff0;
1194 svm->vcpu.arch.regs[VCPU_REGS_RIP] = save->rip;
1195
1196 /*
1197 * svm_set_cr0() sets PG and WP and clears NW and CD on save->cr0.
1198 * It also updates the guest-visible cr0 value.
1199 */
1200 svm_set_cr0(&svm->vcpu, X86_CR0_NW | X86_CR0_CD | X86_CR0_ET);
1201 kvm_mmu_reset_context(&svm->vcpu);
1202
1203 save->cr4 = X86_CR4_PAE;
1204 /* rdx = ?? */
1205
1206 if (npt_enabled) {
1207 /* Setup VMCB for Nested Paging */
1208 control->nested_ctl |= SVM_NESTED_CTL_NP_ENABLE;
1209 svm_clr_intercept(svm, INTERCEPT_INVLPG);
1210 clr_exception_intercept(svm, PF_VECTOR);
1211 svm_clr_intercept(svm, INTERCEPT_CR3_READ);
1212 svm_clr_intercept(svm, INTERCEPT_CR3_WRITE);
1213 save->g_pat = svm->vcpu.arch.pat;
1214 save->cr3 = 0;
1215 save->cr4 = 0;
1216 }
1217 svm->asid_generation = 0;
1218
1219 svm->nested.vmcb12_gpa = 0;
1220 svm->vcpu.arch.hflags = 0;
1221
1222 if (!kvm_pause_in_guest(svm->vcpu.kvm)) {
1223 control->pause_filter_count = pause_filter_count;
1224 if (pause_filter_thresh)
1225 control->pause_filter_thresh = pause_filter_thresh;
1226 svm_set_intercept(svm, INTERCEPT_PAUSE);
1227 } else {
1228 svm_clr_intercept(svm, INTERCEPT_PAUSE);
1229 }
1230
1231 svm_check_invpcid(svm);
1232
1233 if (kvm_vcpu_apicv_active(&svm->vcpu))
1234 avic_init_vmcb(svm);
1235
1236 /*
1237 * If hardware supports Virtual VMLOAD VMSAVE then enable it
1238 * in VMCB and clear intercepts to avoid #VMEXIT.
1239 */
1240 if (vls) {
1241 svm_clr_intercept(svm, INTERCEPT_VMLOAD);
1242 svm_clr_intercept(svm, INTERCEPT_VMSAVE);
1243 svm->vmcb->control.virt_ext |= VIRTUAL_VMLOAD_VMSAVE_ENABLE_MASK;
1244 }
1245
1246 if (vgif) {
1247 svm_clr_intercept(svm, INTERCEPT_STGI);
1248 svm_clr_intercept(svm, INTERCEPT_CLGI);
1249 svm->vmcb->control.int_ctl |= V_GIF_ENABLE_MASK;
1250 }
1251
1252 if (sev_guest(svm->vcpu.kvm)) {
1253 svm->vmcb->control.nested_ctl |= SVM_NESTED_CTL_SEV_ENABLE;
1254 clr_exception_intercept(svm, UD_VECTOR);
1255 }
1256
1257 vmcb_mark_all_dirty(svm->vmcb);
1258
1259 enable_gif(svm);
1260
1261 }
1262
svm_vcpu_reset(struct kvm_vcpu * vcpu,bool init_event)1263 static void svm_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)
1264 {
1265 struct vcpu_svm *svm = to_svm(vcpu);
1266 u32 dummy;
1267 u32 eax = 1;
1268
1269 svm->spec_ctrl = 0;
1270 svm->virt_spec_ctrl = 0;
1271
1272 if (!init_event) {
1273 svm->vcpu.arch.apic_base = APIC_DEFAULT_PHYS_BASE |
1274 MSR_IA32_APICBASE_ENABLE;
1275 if (kvm_vcpu_is_reset_bsp(&svm->vcpu))
1276 svm->vcpu.arch.apic_base |= MSR_IA32_APICBASE_BSP;
1277 }
1278 init_vmcb(svm);
1279
1280 kvm_cpuid(vcpu, &eax, &dummy, &dummy, &dummy, false);
1281 kvm_rdx_write(vcpu, eax);
1282
1283 if (kvm_vcpu_apicv_active(vcpu) && !init_event)
1284 avic_update_vapic_bar(svm, APIC_DEFAULT_PHYS_BASE);
1285 }
1286
svm_create_vcpu(struct kvm_vcpu * vcpu)1287 static int svm_create_vcpu(struct kvm_vcpu *vcpu)
1288 {
1289 struct vcpu_svm *svm;
1290 struct page *vmcb_page;
1291 int err;
1292
1293 BUILD_BUG_ON(offsetof(struct vcpu_svm, vcpu) != 0);
1294 svm = to_svm(vcpu);
1295
1296 err = -ENOMEM;
1297 vmcb_page = alloc_page(GFP_KERNEL_ACCOUNT | __GFP_ZERO);
1298 if (!vmcb_page)
1299 goto out;
1300
1301 err = avic_init_vcpu(svm);
1302 if (err)
1303 goto error_free_vmcb_page;
1304
1305 /* We initialize this flag to true to make sure that the is_running
1306 * bit would be set the first time the vcpu is loaded.
1307 */
1308 if (irqchip_in_kernel(vcpu->kvm) && kvm_apicv_activated(vcpu->kvm))
1309 svm->avic_is_running = true;
1310
1311 svm->msrpm = svm_vcpu_alloc_msrpm();
1312 if (!svm->msrpm) {
1313 err = -ENOMEM;
1314 goto error_free_vmcb_page;
1315 }
1316
1317 svm_vcpu_init_msrpm(vcpu, svm->msrpm);
1318
1319 svm->vmcb = page_address(vmcb_page);
1320 svm->vmcb_pa = __sme_set(page_to_pfn(vmcb_page) << PAGE_SHIFT);
1321 svm->asid_generation = 0;
1322 init_vmcb(svm);
1323
1324 svm_init_osvw(vcpu);
1325 vcpu->arch.microcode_version = 0x01000065;
1326
1327 return 0;
1328
1329 error_free_vmcb_page:
1330 __free_page(vmcb_page);
1331 out:
1332 return err;
1333 }
1334
svm_clear_current_vmcb(struct vmcb * vmcb)1335 static void svm_clear_current_vmcb(struct vmcb *vmcb)
1336 {
1337 int i;
1338
1339 for_each_online_cpu(i)
1340 cmpxchg(&per_cpu(svm_data, i)->current_vmcb, vmcb, NULL);
1341 }
1342
svm_free_vcpu(struct kvm_vcpu * vcpu)1343 static void svm_free_vcpu(struct kvm_vcpu *vcpu)
1344 {
1345 struct vcpu_svm *svm = to_svm(vcpu);
1346
1347 /*
1348 * The vmcb page can be recycled, causing a false negative in
1349 * svm_vcpu_load(). So, ensure that no logical CPU has this
1350 * vmcb page recorded as its current vmcb.
1351 */
1352 svm_clear_current_vmcb(svm->vmcb);
1353
1354 svm_leave_nested(vcpu);
1355 svm_free_nested(svm);
1356
1357 __free_page(pfn_to_page(__sme_clr(svm->vmcb_pa) >> PAGE_SHIFT));
1358 __free_pages(virt_to_page(svm->msrpm), MSRPM_ALLOC_ORDER);
1359 }
1360
svm_vcpu_load(struct kvm_vcpu * vcpu,int cpu)1361 static void svm_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
1362 {
1363 struct vcpu_svm *svm = to_svm(vcpu);
1364 struct svm_cpu_data *sd = per_cpu(svm_data, cpu);
1365 int i;
1366
1367 if (unlikely(cpu != vcpu->cpu)) {
1368 svm->asid_generation = 0;
1369 vmcb_mark_all_dirty(svm->vmcb);
1370 }
1371
1372 #ifdef CONFIG_X86_64
1373 rdmsrl(MSR_GS_BASE, to_svm(vcpu)->host.gs_base);
1374 #endif
1375 savesegment(fs, svm->host.fs);
1376 savesegment(gs, svm->host.gs);
1377 svm->host.ldt = kvm_read_ldt();
1378
1379 for (i = 0; i < NR_HOST_SAVE_USER_MSRS; i++)
1380 rdmsrl(host_save_user_msrs[i], svm->host_user_msrs[i]);
1381
1382 if (static_cpu_has(X86_FEATURE_TSCRATEMSR)) {
1383 u64 tsc_ratio = vcpu->arch.tsc_scaling_ratio;
1384 if (tsc_ratio != __this_cpu_read(current_tsc_ratio)) {
1385 __this_cpu_write(current_tsc_ratio, tsc_ratio);
1386 wrmsrl(MSR_AMD64_TSC_RATIO, tsc_ratio);
1387 }
1388 }
1389 /* This assumes that the kernel never uses MSR_TSC_AUX */
1390 if (static_cpu_has(X86_FEATURE_RDTSCP))
1391 wrmsrl(MSR_TSC_AUX, svm->tsc_aux);
1392
1393 if (sd->current_vmcb != svm->vmcb) {
1394 sd->current_vmcb = svm->vmcb;
1395 indirect_branch_prediction_barrier();
1396 }
1397 avic_vcpu_load(vcpu, cpu);
1398 }
1399
svm_vcpu_put(struct kvm_vcpu * vcpu)1400 static void svm_vcpu_put(struct kvm_vcpu *vcpu)
1401 {
1402 struct vcpu_svm *svm = to_svm(vcpu);
1403 int i;
1404
1405 avic_vcpu_put(vcpu);
1406
1407 ++vcpu->stat.host_state_reload;
1408 kvm_load_ldt(svm->host.ldt);
1409 #ifdef CONFIG_X86_64
1410 loadsegment(fs, svm->host.fs);
1411 wrmsrl(MSR_KERNEL_GS_BASE, current->thread.gsbase);
1412 load_gs_index(svm->host.gs);
1413 #else
1414 #ifdef CONFIG_X86_32_LAZY_GS
1415 loadsegment(gs, svm->host.gs);
1416 #endif
1417 #endif
1418 for (i = 0; i < NR_HOST_SAVE_USER_MSRS; i++)
1419 wrmsrl(host_save_user_msrs[i], svm->host_user_msrs[i]);
1420 }
1421
svm_get_rflags(struct kvm_vcpu * vcpu)1422 static unsigned long svm_get_rflags(struct kvm_vcpu *vcpu)
1423 {
1424 struct vcpu_svm *svm = to_svm(vcpu);
1425 unsigned long rflags = svm->vmcb->save.rflags;
1426
1427 if (svm->nmi_singlestep) {
1428 /* Hide our flags if they were not set by the guest */
1429 if (!(svm->nmi_singlestep_guest_rflags & X86_EFLAGS_TF))
1430 rflags &= ~X86_EFLAGS_TF;
1431 if (!(svm->nmi_singlestep_guest_rflags & X86_EFLAGS_RF))
1432 rflags &= ~X86_EFLAGS_RF;
1433 }
1434 return rflags;
1435 }
1436
svm_set_rflags(struct kvm_vcpu * vcpu,unsigned long rflags)1437 static void svm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
1438 {
1439 if (to_svm(vcpu)->nmi_singlestep)
1440 rflags |= (X86_EFLAGS_TF | X86_EFLAGS_RF);
1441
1442 /*
1443 * Any change of EFLAGS.VM is accompanied by a reload of SS
1444 * (caused by either a task switch or an inter-privilege IRET),
1445 * so we do not need to update the CPL here.
1446 */
1447 to_svm(vcpu)->vmcb->save.rflags = rflags;
1448 }
1449
svm_cache_reg(struct kvm_vcpu * vcpu,enum kvm_reg reg)1450 static void svm_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
1451 {
1452 switch (reg) {
1453 case VCPU_EXREG_PDPTR:
1454 BUG_ON(!npt_enabled);
1455 load_pdptrs(vcpu, vcpu->arch.walk_mmu, kvm_read_cr3(vcpu));
1456 break;
1457 default:
1458 WARN_ON_ONCE(1);
1459 }
1460 }
1461
svm_set_vintr(struct vcpu_svm * svm)1462 static void svm_set_vintr(struct vcpu_svm *svm)
1463 {
1464 struct vmcb_control_area *control;
1465
1466 /* The following fields are ignored when AVIC is enabled */
1467 WARN_ON(kvm_vcpu_apicv_active(&svm->vcpu));
1468 svm_set_intercept(svm, INTERCEPT_VINTR);
1469
1470 /*
1471 * This is just a dummy VINTR to actually cause a vmexit to happen.
1472 * Actual injection of virtual interrupts happens through EVENTINJ.
1473 */
1474 control = &svm->vmcb->control;
1475 control->int_vector = 0x0;
1476 control->int_ctl &= ~V_INTR_PRIO_MASK;
1477 control->int_ctl |= V_IRQ_MASK |
1478 ((/*control->int_vector >> 4*/ 0xf) << V_INTR_PRIO_SHIFT);
1479 vmcb_mark_dirty(svm->vmcb, VMCB_INTR);
1480 }
1481
svm_clear_vintr(struct vcpu_svm * svm)1482 static void svm_clear_vintr(struct vcpu_svm *svm)
1483 {
1484 svm_clr_intercept(svm, INTERCEPT_VINTR);
1485
1486 /* Drop int_ctl fields related to VINTR injection. */
1487 svm->vmcb->control.int_ctl &= ~V_IRQ_INJECTION_BITS_MASK;
1488 if (is_guest_mode(&svm->vcpu)) {
1489 svm->nested.hsave->control.int_ctl &= ~V_IRQ_INJECTION_BITS_MASK;
1490
1491 WARN_ON((svm->vmcb->control.int_ctl & V_TPR_MASK) !=
1492 (svm->nested.ctl.int_ctl & V_TPR_MASK));
1493 svm->vmcb->control.int_ctl |= svm->nested.ctl.int_ctl &
1494 V_IRQ_INJECTION_BITS_MASK;
1495
1496 svm->vmcb->control.int_vector = svm->nested.ctl.int_vector;
1497 }
1498
1499 vmcb_mark_dirty(svm->vmcb, VMCB_INTR);
1500 }
1501
svm_seg(struct kvm_vcpu * vcpu,int seg)1502 static struct vmcb_seg *svm_seg(struct kvm_vcpu *vcpu, int seg)
1503 {
1504 struct vmcb_save_area *save = &to_svm(vcpu)->vmcb->save;
1505
1506 switch (seg) {
1507 case VCPU_SREG_CS: return &save->cs;
1508 case VCPU_SREG_DS: return &save->ds;
1509 case VCPU_SREG_ES: return &save->es;
1510 case VCPU_SREG_FS: return &save->fs;
1511 case VCPU_SREG_GS: return &save->gs;
1512 case VCPU_SREG_SS: return &save->ss;
1513 case VCPU_SREG_TR: return &save->tr;
1514 case VCPU_SREG_LDTR: return &save->ldtr;
1515 }
1516 BUG();
1517 return NULL;
1518 }
1519
svm_get_segment_base(struct kvm_vcpu * vcpu,int seg)1520 static u64 svm_get_segment_base(struct kvm_vcpu *vcpu, int seg)
1521 {
1522 struct vmcb_seg *s = svm_seg(vcpu, seg);
1523
1524 return s->base;
1525 }
1526
svm_get_segment(struct kvm_vcpu * vcpu,struct kvm_segment * var,int seg)1527 static void svm_get_segment(struct kvm_vcpu *vcpu,
1528 struct kvm_segment *var, int seg)
1529 {
1530 struct vmcb_seg *s = svm_seg(vcpu, seg);
1531
1532 var->base = s->base;
1533 var->limit = s->limit;
1534 var->selector = s->selector;
1535 var->type = s->attrib & SVM_SELECTOR_TYPE_MASK;
1536 var->s = (s->attrib >> SVM_SELECTOR_S_SHIFT) & 1;
1537 var->dpl = (s->attrib >> SVM_SELECTOR_DPL_SHIFT) & 3;
1538 var->present = (s->attrib >> SVM_SELECTOR_P_SHIFT) & 1;
1539 var->avl = (s->attrib >> SVM_SELECTOR_AVL_SHIFT) & 1;
1540 var->l = (s->attrib >> SVM_SELECTOR_L_SHIFT) & 1;
1541 var->db = (s->attrib >> SVM_SELECTOR_DB_SHIFT) & 1;
1542
1543 /*
1544 * AMD CPUs circa 2014 track the G bit for all segments except CS.
1545 * However, the SVM spec states that the G bit is not observed by the
1546 * CPU, and some VMware virtual CPUs drop the G bit for all segments.
1547 * So let's synthesize a legal G bit for all segments, this helps
1548 * running KVM nested. It also helps cross-vendor migration, because
1549 * Intel's vmentry has a check on the 'G' bit.
1550 */
1551 var->g = s->limit > 0xfffff;
1552
1553 /*
1554 * AMD's VMCB does not have an explicit unusable field, so emulate it
1555 * for cross vendor migration purposes by "not present"
1556 */
1557 var->unusable = !var->present;
1558
1559 switch (seg) {
1560 case VCPU_SREG_TR:
1561 /*
1562 * Work around a bug where the busy flag in the tr selector
1563 * isn't exposed
1564 */
1565 var->type |= 0x2;
1566 break;
1567 case VCPU_SREG_DS:
1568 case VCPU_SREG_ES:
1569 case VCPU_SREG_FS:
1570 case VCPU_SREG_GS:
1571 /*
1572 * The accessed bit must always be set in the segment
1573 * descriptor cache, although it can be cleared in the
1574 * descriptor, the cached bit always remains at 1. Since
1575 * Intel has a check on this, set it here to support
1576 * cross-vendor migration.
1577 */
1578 if (!var->unusable)
1579 var->type |= 0x1;
1580 break;
1581 case VCPU_SREG_SS:
1582 /*
1583 * On AMD CPUs sometimes the DB bit in the segment
1584 * descriptor is left as 1, although the whole segment has
1585 * been made unusable. Clear it here to pass an Intel VMX
1586 * entry check when cross vendor migrating.
1587 */
1588 if (var->unusable)
1589 var->db = 0;
1590 /* This is symmetric with svm_set_segment() */
1591 var->dpl = to_svm(vcpu)->vmcb->save.cpl;
1592 break;
1593 }
1594 }
1595
svm_get_cpl(struct kvm_vcpu * vcpu)1596 static int svm_get_cpl(struct kvm_vcpu *vcpu)
1597 {
1598 struct vmcb_save_area *save = &to_svm(vcpu)->vmcb->save;
1599
1600 return save->cpl;
1601 }
1602
svm_get_idt(struct kvm_vcpu * vcpu,struct desc_ptr * dt)1603 static void svm_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
1604 {
1605 struct vcpu_svm *svm = to_svm(vcpu);
1606
1607 dt->size = svm->vmcb->save.idtr.limit;
1608 dt->address = svm->vmcb->save.idtr.base;
1609 }
1610
svm_set_idt(struct kvm_vcpu * vcpu,struct desc_ptr * dt)1611 static void svm_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
1612 {
1613 struct vcpu_svm *svm = to_svm(vcpu);
1614
1615 svm->vmcb->save.idtr.limit = dt->size;
1616 svm->vmcb->save.idtr.base = dt->address ;
1617 vmcb_mark_dirty(svm->vmcb, VMCB_DT);
1618 }
1619
svm_get_gdt(struct kvm_vcpu * vcpu,struct desc_ptr * dt)1620 static void svm_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
1621 {
1622 struct vcpu_svm *svm = to_svm(vcpu);
1623
1624 dt->size = svm->vmcb->save.gdtr.limit;
1625 dt->address = svm->vmcb->save.gdtr.base;
1626 }
1627
svm_set_gdt(struct kvm_vcpu * vcpu,struct desc_ptr * dt)1628 static void svm_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
1629 {
1630 struct vcpu_svm *svm = to_svm(vcpu);
1631
1632 svm->vmcb->save.gdtr.limit = dt->size;
1633 svm->vmcb->save.gdtr.base = dt->address ;
1634 vmcb_mark_dirty(svm->vmcb, VMCB_DT);
1635 }
1636
update_cr0_intercept(struct vcpu_svm * svm)1637 static void update_cr0_intercept(struct vcpu_svm *svm)
1638 {
1639 ulong gcr0 = svm->vcpu.arch.cr0;
1640 u64 *hcr0 = &svm->vmcb->save.cr0;
1641
1642 *hcr0 = (*hcr0 & ~SVM_CR0_SELECTIVE_MASK)
1643 | (gcr0 & SVM_CR0_SELECTIVE_MASK);
1644
1645 vmcb_mark_dirty(svm->vmcb, VMCB_CR);
1646
1647 if (gcr0 == *hcr0) {
1648 svm_clr_intercept(svm, INTERCEPT_CR0_READ);
1649 svm_clr_intercept(svm, INTERCEPT_CR0_WRITE);
1650 } else {
1651 svm_set_intercept(svm, INTERCEPT_CR0_READ);
1652 svm_set_intercept(svm, INTERCEPT_CR0_WRITE);
1653 }
1654 }
1655
svm_set_cr0(struct kvm_vcpu * vcpu,unsigned long cr0)1656 void svm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
1657 {
1658 struct vcpu_svm *svm = to_svm(vcpu);
1659
1660 #ifdef CONFIG_X86_64
1661 if (vcpu->arch.efer & EFER_LME) {
1662 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) {
1663 vcpu->arch.efer |= EFER_LMA;
1664 svm->vmcb->save.efer |= EFER_LMA | EFER_LME;
1665 }
1666
1667 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG)) {
1668 vcpu->arch.efer &= ~EFER_LMA;
1669 svm->vmcb->save.efer &= ~(EFER_LMA | EFER_LME);
1670 }
1671 }
1672 #endif
1673 vcpu->arch.cr0 = cr0;
1674
1675 if (!npt_enabled)
1676 cr0 |= X86_CR0_PG | X86_CR0_WP;
1677
1678 /*
1679 * re-enable caching here because the QEMU bios
1680 * does not do it - this results in some delay at
1681 * reboot
1682 */
1683 if (kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_CD_NW_CLEARED))
1684 cr0 &= ~(X86_CR0_CD | X86_CR0_NW);
1685 svm->vmcb->save.cr0 = cr0;
1686 vmcb_mark_dirty(svm->vmcb, VMCB_CR);
1687 update_cr0_intercept(svm);
1688 }
1689
svm_is_valid_cr4(struct kvm_vcpu * vcpu,unsigned long cr4)1690 static bool svm_is_valid_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
1691 {
1692 return true;
1693 }
1694
svm_set_cr4(struct kvm_vcpu * vcpu,unsigned long cr4)1695 void svm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
1696 {
1697 unsigned long host_cr4_mce = cr4_read_shadow() & X86_CR4_MCE;
1698 unsigned long old_cr4 = to_svm(vcpu)->vmcb->save.cr4;
1699
1700 if (npt_enabled && ((old_cr4 ^ cr4) & X86_CR4_PGE))
1701 svm_flush_tlb(vcpu);
1702
1703 vcpu->arch.cr4 = cr4;
1704 if (!npt_enabled)
1705 cr4 |= X86_CR4_PAE;
1706 cr4 |= host_cr4_mce;
1707 to_svm(vcpu)->vmcb->save.cr4 = cr4;
1708 vmcb_mark_dirty(to_svm(vcpu)->vmcb, VMCB_CR);
1709 }
1710
svm_set_segment(struct kvm_vcpu * vcpu,struct kvm_segment * var,int seg)1711 static void svm_set_segment(struct kvm_vcpu *vcpu,
1712 struct kvm_segment *var, int seg)
1713 {
1714 struct vcpu_svm *svm = to_svm(vcpu);
1715 struct vmcb_seg *s = svm_seg(vcpu, seg);
1716
1717 s->base = var->base;
1718 s->limit = var->limit;
1719 s->selector = var->selector;
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) && !var->unusable) << 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
1729 /*
1730 * This is always accurate, except if SYSRET returned to a segment
1731 * with SS.DPL != 3. Intel does not have this quirk, and always
1732 * forces SS.DPL to 3 on sysret, so we ignore that case; fixing it
1733 * would entail passing the CPL to userspace and back.
1734 */
1735 if (seg == VCPU_SREG_SS)
1736 /* This is symmetric with svm_get_segment() */
1737 svm->vmcb->save.cpl = (var->dpl & 3);
1738
1739 vmcb_mark_dirty(svm->vmcb, VMCB_SEG);
1740 }
1741
update_exception_bitmap(struct kvm_vcpu * vcpu)1742 static void update_exception_bitmap(struct kvm_vcpu *vcpu)
1743 {
1744 struct vcpu_svm *svm = to_svm(vcpu);
1745
1746 clr_exception_intercept(svm, BP_VECTOR);
1747
1748 if (vcpu->guest_debug & KVM_GUESTDBG_ENABLE) {
1749 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
1750 set_exception_intercept(svm, BP_VECTOR);
1751 }
1752 }
1753
new_asid(struct vcpu_svm * svm,struct svm_cpu_data * sd)1754 static void new_asid(struct vcpu_svm *svm, struct svm_cpu_data *sd)
1755 {
1756 if (sd->next_asid > sd->max_asid) {
1757 ++sd->asid_generation;
1758 sd->next_asid = sd->min_asid;
1759 svm->vmcb->control.tlb_ctl = TLB_CONTROL_FLUSH_ALL_ASID;
1760 }
1761
1762 svm->asid_generation = sd->asid_generation;
1763 svm->vmcb->control.asid = sd->next_asid++;
1764
1765 vmcb_mark_dirty(svm->vmcb, VMCB_ASID);
1766 }
1767
svm_set_dr6(struct vcpu_svm * svm,unsigned long value)1768 static void svm_set_dr6(struct vcpu_svm *svm, unsigned long value)
1769 {
1770 struct vmcb *vmcb = svm->vmcb;
1771
1772 if (unlikely(value != vmcb->save.dr6)) {
1773 vmcb->save.dr6 = value;
1774 vmcb_mark_dirty(vmcb, VMCB_DR);
1775 }
1776 }
1777
svm_sync_dirty_debug_regs(struct kvm_vcpu * vcpu)1778 static void svm_sync_dirty_debug_regs(struct kvm_vcpu *vcpu)
1779 {
1780 struct vcpu_svm *svm = to_svm(vcpu);
1781
1782 get_debugreg(vcpu->arch.db[0], 0);
1783 get_debugreg(vcpu->arch.db[1], 1);
1784 get_debugreg(vcpu->arch.db[2], 2);
1785 get_debugreg(vcpu->arch.db[3], 3);
1786 /*
1787 * We cannot reset svm->vmcb->save.dr6 to DR6_FIXED_1|DR6_RTM here,
1788 * because db_interception might need it. We can do it before vmentry.
1789 */
1790 vcpu->arch.dr6 = svm->vmcb->save.dr6;
1791 vcpu->arch.dr7 = svm->vmcb->save.dr7;
1792 vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_WONT_EXIT;
1793 set_dr_intercepts(svm);
1794 }
1795
svm_set_dr7(struct kvm_vcpu * vcpu,unsigned long value)1796 static void svm_set_dr7(struct kvm_vcpu *vcpu, unsigned long value)
1797 {
1798 struct vcpu_svm *svm = to_svm(vcpu);
1799
1800 svm->vmcb->save.dr7 = value;
1801 vmcb_mark_dirty(svm->vmcb, VMCB_DR);
1802 }
1803
pf_interception(struct vcpu_svm * svm)1804 static int pf_interception(struct vcpu_svm *svm)
1805 {
1806 u64 fault_address = svm->vmcb->control.exit_info_2;
1807 u64 error_code = svm->vmcb->control.exit_info_1;
1808
1809 return kvm_handle_page_fault(&svm->vcpu, error_code, fault_address,
1810 static_cpu_has(X86_FEATURE_DECODEASSISTS) ?
1811 svm->vmcb->control.insn_bytes : NULL,
1812 svm->vmcb->control.insn_len);
1813 }
1814
npf_interception(struct vcpu_svm * svm)1815 static int npf_interception(struct vcpu_svm *svm)
1816 {
1817 u64 fault_address = __sme_clr(svm->vmcb->control.exit_info_2);
1818 u64 error_code = svm->vmcb->control.exit_info_1;
1819
1820 trace_kvm_page_fault(fault_address, error_code);
1821 return kvm_mmu_page_fault(&svm->vcpu, fault_address, error_code,
1822 static_cpu_has(X86_FEATURE_DECODEASSISTS) ?
1823 svm->vmcb->control.insn_bytes : NULL,
1824 svm->vmcb->control.insn_len);
1825 }
1826
db_interception(struct vcpu_svm * svm)1827 static int db_interception(struct vcpu_svm *svm)
1828 {
1829 struct kvm_run *kvm_run = svm->vcpu.run;
1830 struct kvm_vcpu *vcpu = &svm->vcpu;
1831
1832 if (!(svm->vcpu.guest_debug &
1833 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)) &&
1834 !svm->nmi_singlestep) {
1835 u32 payload = (svm->vmcb->save.dr6 ^ DR6_RTM) & ~DR6_FIXED_1;
1836 kvm_queue_exception_p(&svm->vcpu, DB_VECTOR, payload);
1837 return 1;
1838 }
1839
1840 if (svm->nmi_singlestep) {
1841 disable_nmi_singlestep(svm);
1842 /* Make sure we check for pending NMIs upon entry */
1843 kvm_make_request(KVM_REQ_EVENT, vcpu);
1844 }
1845
1846 if (svm->vcpu.guest_debug &
1847 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)) {
1848 kvm_run->exit_reason = KVM_EXIT_DEBUG;
1849 kvm_run->debug.arch.dr6 = svm->vmcb->save.dr6;
1850 kvm_run->debug.arch.dr7 = svm->vmcb->save.dr7;
1851 kvm_run->debug.arch.pc =
1852 svm->vmcb->save.cs.base + svm->vmcb->save.rip;
1853 kvm_run->debug.arch.exception = DB_VECTOR;
1854 return 0;
1855 }
1856
1857 return 1;
1858 }
1859
bp_interception(struct vcpu_svm * svm)1860 static int bp_interception(struct vcpu_svm *svm)
1861 {
1862 struct kvm_run *kvm_run = svm->vcpu.run;
1863
1864 kvm_run->exit_reason = KVM_EXIT_DEBUG;
1865 kvm_run->debug.arch.pc = svm->vmcb->save.cs.base + svm->vmcb->save.rip;
1866 kvm_run->debug.arch.exception = BP_VECTOR;
1867 return 0;
1868 }
1869
ud_interception(struct vcpu_svm * svm)1870 static int ud_interception(struct vcpu_svm *svm)
1871 {
1872 return handle_ud(&svm->vcpu);
1873 }
1874
ac_interception(struct vcpu_svm * svm)1875 static int ac_interception(struct vcpu_svm *svm)
1876 {
1877 kvm_queue_exception_e(&svm->vcpu, AC_VECTOR, 0);
1878 return 1;
1879 }
1880
gp_interception(struct vcpu_svm * svm)1881 static int gp_interception(struct vcpu_svm *svm)
1882 {
1883 struct kvm_vcpu *vcpu = &svm->vcpu;
1884 u32 error_code = svm->vmcb->control.exit_info_1;
1885
1886 WARN_ON_ONCE(!enable_vmware_backdoor);
1887
1888 /*
1889 * VMware backdoor emulation on #GP interception only handles IN{S},
1890 * OUT{S}, and RDPMC, none of which generate a non-zero error code.
1891 */
1892 if (error_code) {
1893 kvm_queue_exception_e(vcpu, GP_VECTOR, error_code);
1894 return 1;
1895 }
1896 return kvm_emulate_instruction(vcpu, EMULTYPE_VMWARE_GP);
1897 }
1898
is_erratum_383(void)1899 static bool is_erratum_383(void)
1900 {
1901 int err, i;
1902 u64 value;
1903
1904 if (!erratum_383_found)
1905 return false;
1906
1907 value = native_read_msr_safe(MSR_IA32_MC0_STATUS, &err);
1908 if (err)
1909 return false;
1910
1911 /* Bit 62 may or may not be set for this mce */
1912 value &= ~(1ULL << 62);
1913
1914 if (value != 0xb600000000010015ULL)
1915 return false;
1916
1917 /* Clear MCi_STATUS registers */
1918 for (i = 0; i < 6; ++i)
1919 native_write_msr_safe(MSR_IA32_MCx_STATUS(i), 0, 0);
1920
1921 value = native_read_msr_safe(MSR_IA32_MCG_STATUS, &err);
1922 if (!err) {
1923 u32 low, high;
1924
1925 value &= ~(1ULL << 2);
1926 low = lower_32_bits(value);
1927 high = upper_32_bits(value);
1928
1929 native_write_msr_safe(MSR_IA32_MCG_STATUS, low, high);
1930 }
1931
1932 /* Flush tlb to evict multi-match entries */
1933 __flush_tlb_all();
1934
1935 return true;
1936 }
1937
1938 /*
1939 * Trigger machine check on the host. We assume all the MSRs are already set up
1940 * by the CPU and that we still run on the same CPU as the MCE occurred on.
1941 * We pass a fake environment to the machine check handler because we want
1942 * the guest to be always treated like user space, no matter what context
1943 * it used internally.
1944 */
kvm_machine_check(void)1945 static void kvm_machine_check(void)
1946 {
1947 #if defined(CONFIG_X86_MCE)
1948 struct pt_regs regs = {
1949 .cs = 3, /* Fake ring 3 no matter what the guest ran on */
1950 .flags = X86_EFLAGS_IF,
1951 };
1952
1953 do_machine_check(®s);
1954 #endif
1955 }
1956
svm_handle_mce(struct vcpu_svm * svm)1957 static void svm_handle_mce(struct vcpu_svm *svm)
1958 {
1959 if (is_erratum_383()) {
1960 /*
1961 * Erratum 383 triggered. Guest state is corrupt so kill the
1962 * guest.
1963 */
1964 pr_err("KVM: Guest triggered AMD Erratum 383\n");
1965
1966 kvm_make_request(KVM_REQ_TRIPLE_FAULT, &svm->vcpu);
1967
1968 return;
1969 }
1970
1971 /*
1972 * On an #MC intercept the MCE handler is not called automatically in
1973 * the host. So do it by hand here.
1974 */
1975 kvm_machine_check();
1976 }
1977
mc_interception(struct vcpu_svm * svm)1978 static int mc_interception(struct vcpu_svm *svm)
1979 {
1980 return 1;
1981 }
1982
shutdown_interception(struct vcpu_svm * svm)1983 static int shutdown_interception(struct vcpu_svm *svm)
1984 {
1985 struct kvm_run *kvm_run = svm->vcpu.run;
1986
1987 /*
1988 * VMCB is undefined after a SHUTDOWN intercept
1989 * so reinitialize it.
1990 */
1991 clear_page(svm->vmcb);
1992 init_vmcb(svm);
1993
1994 kvm_run->exit_reason = KVM_EXIT_SHUTDOWN;
1995 return 0;
1996 }
1997
io_interception(struct vcpu_svm * svm)1998 static int io_interception(struct vcpu_svm *svm)
1999 {
2000 struct kvm_vcpu *vcpu = &svm->vcpu;
2001 u32 io_info = svm->vmcb->control.exit_info_1; /* address size bug? */
2002 int size, in, string;
2003 unsigned port;
2004
2005 ++svm->vcpu.stat.io_exits;
2006 string = (io_info & SVM_IOIO_STR_MASK) != 0;
2007 in = (io_info & SVM_IOIO_TYPE_MASK) != 0;
2008 if (string)
2009 return kvm_emulate_instruction(vcpu, 0);
2010
2011 port = io_info >> 16;
2012 size = (io_info & SVM_IOIO_SIZE_MASK) >> SVM_IOIO_SIZE_SHIFT;
2013 svm->next_rip = svm->vmcb->control.exit_info_2;
2014
2015 return kvm_fast_pio(&svm->vcpu, size, port, in);
2016 }
2017
nmi_interception(struct vcpu_svm * svm)2018 static int nmi_interception(struct vcpu_svm *svm)
2019 {
2020 return 1;
2021 }
2022
intr_interception(struct vcpu_svm * svm)2023 static int intr_interception(struct vcpu_svm *svm)
2024 {
2025 ++svm->vcpu.stat.irq_exits;
2026 return 1;
2027 }
2028
nop_on_interception(struct vcpu_svm * svm)2029 static int nop_on_interception(struct vcpu_svm *svm)
2030 {
2031 return 1;
2032 }
2033
halt_interception(struct vcpu_svm * svm)2034 static int halt_interception(struct vcpu_svm *svm)
2035 {
2036 return kvm_emulate_halt(&svm->vcpu);
2037 }
2038
vmmcall_interception(struct vcpu_svm * svm)2039 static int vmmcall_interception(struct vcpu_svm *svm)
2040 {
2041 return kvm_emulate_hypercall(&svm->vcpu);
2042 }
2043
vmload_interception(struct vcpu_svm * svm)2044 static int vmload_interception(struct vcpu_svm *svm)
2045 {
2046 struct vmcb *nested_vmcb;
2047 struct kvm_host_map map;
2048 int ret;
2049
2050 if (nested_svm_check_permissions(svm))
2051 return 1;
2052
2053 ret = kvm_vcpu_map(&svm->vcpu, gpa_to_gfn(svm->vmcb->save.rax), &map);
2054 if (ret) {
2055 if (ret == -EINVAL)
2056 kvm_inject_gp(&svm->vcpu, 0);
2057 return 1;
2058 }
2059
2060 nested_vmcb = map.hva;
2061
2062 ret = kvm_skip_emulated_instruction(&svm->vcpu);
2063
2064 nested_svm_vmloadsave(nested_vmcb, svm->vmcb);
2065 kvm_vcpu_unmap(&svm->vcpu, &map, true);
2066
2067 return ret;
2068 }
2069
vmsave_interception(struct vcpu_svm * svm)2070 static int vmsave_interception(struct vcpu_svm *svm)
2071 {
2072 struct vmcb *nested_vmcb;
2073 struct kvm_host_map map;
2074 int ret;
2075
2076 if (nested_svm_check_permissions(svm))
2077 return 1;
2078
2079 ret = kvm_vcpu_map(&svm->vcpu, gpa_to_gfn(svm->vmcb->save.rax), &map);
2080 if (ret) {
2081 if (ret == -EINVAL)
2082 kvm_inject_gp(&svm->vcpu, 0);
2083 return 1;
2084 }
2085
2086 nested_vmcb = map.hva;
2087
2088 ret = kvm_skip_emulated_instruction(&svm->vcpu);
2089
2090 nested_svm_vmloadsave(svm->vmcb, nested_vmcb);
2091 kvm_vcpu_unmap(&svm->vcpu, &map, true);
2092
2093 return ret;
2094 }
2095
vmrun_interception(struct vcpu_svm * svm)2096 static int vmrun_interception(struct vcpu_svm *svm)
2097 {
2098 if (nested_svm_check_permissions(svm))
2099 return 1;
2100
2101 return nested_svm_vmrun(svm);
2102 }
2103
svm_set_gif(struct vcpu_svm * svm,bool value)2104 void svm_set_gif(struct vcpu_svm *svm, bool value)
2105 {
2106 if (value) {
2107 /*
2108 * If VGIF is enabled, the STGI intercept is only added to
2109 * detect the opening of the SMI/NMI window; remove it now.
2110 * Likewise, clear the VINTR intercept, we will set it
2111 * again while processing KVM_REQ_EVENT if needed.
2112 */
2113 if (vgif_enabled(svm))
2114 svm_clr_intercept(svm, INTERCEPT_STGI);
2115 if (svm_is_intercept(svm, INTERCEPT_VINTR))
2116 svm_clear_vintr(svm);
2117
2118 enable_gif(svm);
2119 if (svm->vcpu.arch.smi_pending ||
2120 svm->vcpu.arch.nmi_pending ||
2121 kvm_cpu_has_injectable_intr(&svm->vcpu))
2122 kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
2123 } else {
2124 disable_gif(svm);
2125
2126 /*
2127 * After a CLGI no interrupts should come. But if vGIF is
2128 * in use, we still rely on the VINTR intercept (rather than
2129 * STGI) to detect an open interrupt window.
2130 */
2131 if (!vgif_enabled(svm))
2132 svm_clear_vintr(svm);
2133 }
2134 }
2135
stgi_interception(struct vcpu_svm * svm)2136 static int stgi_interception(struct vcpu_svm *svm)
2137 {
2138 int ret;
2139
2140 if (nested_svm_check_permissions(svm))
2141 return 1;
2142
2143 ret = kvm_skip_emulated_instruction(&svm->vcpu);
2144 svm_set_gif(svm, true);
2145 return ret;
2146 }
2147
clgi_interception(struct vcpu_svm * svm)2148 static int clgi_interception(struct vcpu_svm *svm)
2149 {
2150 int ret;
2151
2152 if (nested_svm_check_permissions(svm))
2153 return 1;
2154
2155 ret = kvm_skip_emulated_instruction(&svm->vcpu);
2156 svm_set_gif(svm, false);
2157 return ret;
2158 }
2159
invlpga_interception(struct vcpu_svm * svm)2160 static int invlpga_interception(struct vcpu_svm *svm)
2161 {
2162 struct kvm_vcpu *vcpu = &svm->vcpu;
2163
2164 trace_kvm_invlpga(svm->vmcb->save.rip, kvm_rcx_read(&svm->vcpu),
2165 kvm_rax_read(&svm->vcpu));
2166
2167 /* Let's treat INVLPGA the same as INVLPG (can be optimized!) */
2168 kvm_mmu_invlpg(vcpu, kvm_rax_read(&svm->vcpu));
2169
2170 return kvm_skip_emulated_instruction(&svm->vcpu);
2171 }
2172
skinit_interception(struct vcpu_svm * svm)2173 static int skinit_interception(struct vcpu_svm *svm)
2174 {
2175 trace_kvm_skinit(svm->vmcb->save.rip, kvm_rax_read(&svm->vcpu));
2176
2177 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
2178 return 1;
2179 }
2180
wbinvd_interception(struct vcpu_svm * svm)2181 static int wbinvd_interception(struct vcpu_svm *svm)
2182 {
2183 return kvm_emulate_wbinvd(&svm->vcpu);
2184 }
2185
xsetbv_interception(struct vcpu_svm * svm)2186 static int xsetbv_interception(struct vcpu_svm *svm)
2187 {
2188 u64 new_bv = kvm_read_edx_eax(&svm->vcpu);
2189 u32 index = kvm_rcx_read(&svm->vcpu);
2190
2191 if (kvm_set_xcr(&svm->vcpu, index, new_bv) == 0) {
2192 return kvm_skip_emulated_instruction(&svm->vcpu);
2193 }
2194
2195 return 1;
2196 }
2197
rdpru_interception(struct vcpu_svm * svm)2198 static int rdpru_interception(struct vcpu_svm *svm)
2199 {
2200 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
2201 return 1;
2202 }
2203
task_switch_interception(struct vcpu_svm * svm)2204 static int task_switch_interception(struct vcpu_svm *svm)
2205 {
2206 u16 tss_selector;
2207 int reason;
2208 int int_type = svm->vmcb->control.exit_int_info &
2209 SVM_EXITINTINFO_TYPE_MASK;
2210 int int_vec = svm->vmcb->control.exit_int_info & SVM_EVTINJ_VEC_MASK;
2211 uint32_t type =
2212 svm->vmcb->control.exit_int_info & SVM_EXITINTINFO_TYPE_MASK;
2213 uint32_t idt_v =
2214 svm->vmcb->control.exit_int_info & SVM_EXITINTINFO_VALID;
2215 bool has_error_code = false;
2216 u32 error_code = 0;
2217
2218 tss_selector = (u16)svm->vmcb->control.exit_info_1;
2219
2220 if (svm->vmcb->control.exit_info_2 &
2221 (1ULL << SVM_EXITINFOSHIFT_TS_REASON_IRET))
2222 reason = TASK_SWITCH_IRET;
2223 else if (svm->vmcb->control.exit_info_2 &
2224 (1ULL << SVM_EXITINFOSHIFT_TS_REASON_JMP))
2225 reason = TASK_SWITCH_JMP;
2226 else if (idt_v)
2227 reason = TASK_SWITCH_GATE;
2228 else
2229 reason = TASK_SWITCH_CALL;
2230
2231 if (reason == TASK_SWITCH_GATE) {
2232 switch (type) {
2233 case SVM_EXITINTINFO_TYPE_NMI:
2234 svm->vcpu.arch.nmi_injected = false;
2235 break;
2236 case SVM_EXITINTINFO_TYPE_EXEPT:
2237 if (svm->vmcb->control.exit_info_2 &
2238 (1ULL << SVM_EXITINFOSHIFT_TS_HAS_ERROR_CODE)) {
2239 has_error_code = true;
2240 error_code =
2241 (u32)svm->vmcb->control.exit_info_2;
2242 }
2243 kvm_clear_exception_queue(&svm->vcpu);
2244 break;
2245 case SVM_EXITINTINFO_TYPE_INTR:
2246 kvm_clear_interrupt_queue(&svm->vcpu);
2247 break;
2248 default:
2249 break;
2250 }
2251 }
2252
2253 if (reason != TASK_SWITCH_GATE ||
2254 int_type == SVM_EXITINTINFO_TYPE_SOFT ||
2255 (int_type == SVM_EXITINTINFO_TYPE_EXEPT &&
2256 (int_vec == OF_VECTOR || int_vec == BP_VECTOR))) {
2257 if (!skip_emulated_instruction(&svm->vcpu))
2258 return 0;
2259 }
2260
2261 if (int_type != SVM_EXITINTINFO_TYPE_SOFT)
2262 int_vec = -1;
2263
2264 return kvm_task_switch(&svm->vcpu, tss_selector, int_vec, reason,
2265 has_error_code, error_code);
2266 }
2267
cpuid_interception(struct vcpu_svm * svm)2268 static int cpuid_interception(struct vcpu_svm *svm)
2269 {
2270 return kvm_emulate_cpuid(&svm->vcpu);
2271 }
2272
iret_interception(struct vcpu_svm * svm)2273 static int iret_interception(struct vcpu_svm *svm)
2274 {
2275 ++svm->vcpu.stat.nmi_window_exits;
2276 svm_clr_intercept(svm, INTERCEPT_IRET);
2277 svm->vcpu.arch.hflags |= HF_IRET_MASK;
2278 svm->nmi_iret_rip = kvm_rip_read(&svm->vcpu);
2279 kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
2280 return 1;
2281 }
2282
invd_interception(struct vcpu_svm * svm)2283 static int invd_interception(struct vcpu_svm *svm)
2284 {
2285 /* Treat an INVD instruction as a NOP and just skip it. */
2286 return kvm_skip_emulated_instruction(&svm->vcpu);
2287 }
2288
invlpg_interception(struct vcpu_svm * svm)2289 static int invlpg_interception(struct vcpu_svm *svm)
2290 {
2291 if (!static_cpu_has(X86_FEATURE_DECODEASSISTS))
2292 return kvm_emulate_instruction(&svm->vcpu, 0);
2293
2294 kvm_mmu_invlpg(&svm->vcpu, svm->vmcb->control.exit_info_1);
2295 return kvm_skip_emulated_instruction(&svm->vcpu);
2296 }
2297
emulate_on_interception(struct vcpu_svm * svm)2298 static int emulate_on_interception(struct vcpu_svm *svm)
2299 {
2300 return kvm_emulate_instruction(&svm->vcpu, 0);
2301 }
2302
rsm_interception(struct vcpu_svm * svm)2303 static int rsm_interception(struct vcpu_svm *svm)
2304 {
2305 return kvm_emulate_instruction_from_buffer(&svm->vcpu, rsm_ins_bytes, 2);
2306 }
2307
rdpmc_interception(struct vcpu_svm * svm)2308 static int rdpmc_interception(struct vcpu_svm *svm)
2309 {
2310 int err;
2311
2312 if (!nrips)
2313 return emulate_on_interception(svm);
2314
2315 err = kvm_rdpmc(&svm->vcpu);
2316 return kvm_complete_insn_gp(&svm->vcpu, err);
2317 }
2318
check_selective_cr0_intercepted(struct vcpu_svm * svm,unsigned long val)2319 static bool check_selective_cr0_intercepted(struct vcpu_svm *svm,
2320 unsigned long val)
2321 {
2322 unsigned long cr0 = svm->vcpu.arch.cr0;
2323 bool ret = false;
2324
2325 if (!is_guest_mode(&svm->vcpu) ||
2326 (!(vmcb_is_intercept(&svm->nested.ctl, INTERCEPT_SELECTIVE_CR0))))
2327 return false;
2328
2329 cr0 &= ~SVM_CR0_SELECTIVE_MASK;
2330 val &= ~SVM_CR0_SELECTIVE_MASK;
2331
2332 if (cr0 ^ val) {
2333 svm->vmcb->control.exit_code = SVM_EXIT_CR0_SEL_WRITE;
2334 ret = (nested_svm_exit_handled(svm) == NESTED_EXIT_DONE);
2335 }
2336
2337 return ret;
2338 }
2339
2340 #define CR_VALID (1ULL << 63)
2341
cr_interception(struct vcpu_svm * svm)2342 static int cr_interception(struct vcpu_svm *svm)
2343 {
2344 int reg, cr;
2345 unsigned long val;
2346 int err;
2347
2348 if (!static_cpu_has(X86_FEATURE_DECODEASSISTS))
2349 return emulate_on_interception(svm);
2350
2351 if (unlikely((svm->vmcb->control.exit_info_1 & CR_VALID) == 0))
2352 return emulate_on_interception(svm);
2353
2354 reg = svm->vmcb->control.exit_info_1 & SVM_EXITINFO_REG_MASK;
2355 if (svm->vmcb->control.exit_code == SVM_EXIT_CR0_SEL_WRITE)
2356 cr = SVM_EXIT_WRITE_CR0 - SVM_EXIT_READ_CR0;
2357 else
2358 cr = svm->vmcb->control.exit_code - SVM_EXIT_READ_CR0;
2359
2360 err = 0;
2361 if (cr >= 16) { /* mov to cr */
2362 cr -= 16;
2363 val = kvm_register_readl(&svm->vcpu, reg);
2364 trace_kvm_cr_write(cr, val);
2365 switch (cr) {
2366 case 0:
2367 if (!check_selective_cr0_intercepted(svm, val))
2368 err = kvm_set_cr0(&svm->vcpu, val);
2369 else
2370 return 1;
2371
2372 break;
2373 case 3:
2374 err = kvm_set_cr3(&svm->vcpu, val);
2375 break;
2376 case 4:
2377 err = kvm_set_cr4(&svm->vcpu, val);
2378 break;
2379 case 8:
2380 err = kvm_set_cr8(&svm->vcpu, val);
2381 break;
2382 default:
2383 WARN(1, "unhandled write to CR%d", cr);
2384 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
2385 return 1;
2386 }
2387 } else { /* mov from cr */
2388 switch (cr) {
2389 case 0:
2390 val = kvm_read_cr0(&svm->vcpu);
2391 break;
2392 case 2:
2393 val = svm->vcpu.arch.cr2;
2394 break;
2395 case 3:
2396 val = kvm_read_cr3(&svm->vcpu);
2397 break;
2398 case 4:
2399 val = kvm_read_cr4(&svm->vcpu);
2400 break;
2401 case 8:
2402 val = kvm_get_cr8(&svm->vcpu);
2403 break;
2404 default:
2405 WARN(1, "unhandled read from CR%d", cr);
2406 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
2407 return 1;
2408 }
2409 kvm_register_writel(&svm->vcpu, reg, val);
2410 trace_kvm_cr_read(cr, val);
2411 }
2412 return kvm_complete_insn_gp(&svm->vcpu, err);
2413 }
2414
dr_interception(struct vcpu_svm * svm)2415 static int dr_interception(struct vcpu_svm *svm)
2416 {
2417 int reg, dr;
2418 unsigned long val;
2419
2420 if (svm->vcpu.guest_debug == 0) {
2421 /*
2422 * No more DR vmexits; force a reload of the debug registers
2423 * and reenter on this instruction. The next vmexit will
2424 * retrieve the full state of the debug registers.
2425 */
2426 clr_dr_intercepts(svm);
2427 svm->vcpu.arch.switch_db_regs |= KVM_DEBUGREG_WONT_EXIT;
2428 return 1;
2429 }
2430
2431 if (!boot_cpu_has(X86_FEATURE_DECODEASSISTS))
2432 return emulate_on_interception(svm);
2433
2434 reg = svm->vmcb->control.exit_info_1 & SVM_EXITINFO_REG_MASK;
2435 dr = svm->vmcb->control.exit_code - SVM_EXIT_READ_DR0;
2436
2437 if (dr >= 16) { /* mov to DRn */
2438 if (!kvm_require_dr(&svm->vcpu, dr - 16))
2439 return 1;
2440 val = kvm_register_readl(&svm->vcpu, reg);
2441 kvm_set_dr(&svm->vcpu, dr - 16, val);
2442 } else {
2443 if (!kvm_require_dr(&svm->vcpu, dr))
2444 return 1;
2445 kvm_get_dr(&svm->vcpu, dr, &val);
2446 kvm_register_writel(&svm->vcpu, reg, val);
2447 }
2448
2449 return kvm_skip_emulated_instruction(&svm->vcpu);
2450 }
2451
cr8_write_interception(struct vcpu_svm * svm)2452 static int cr8_write_interception(struct vcpu_svm *svm)
2453 {
2454 struct kvm_run *kvm_run = svm->vcpu.run;
2455 int r;
2456
2457 u8 cr8_prev = kvm_get_cr8(&svm->vcpu);
2458 /* instruction emulation calls kvm_set_cr8() */
2459 r = cr_interception(svm);
2460 if (lapic_in_kernel(&svm->vcpu))
2461 return r;
2462 if (cr8_prev <= kvm_get_cr8(&svm->vcpu))
2463 return r;
2464 kvm_run->exit_reason = KVM_EXIT_SET_TPR;
2465 return 0;
2466 }
2467
svm_get_msr_feature(struct kvm_msr_entry * msr)2468 static int svm_get_msr_feature(struct kvm_msr_entry *msr)
2469 {
2470 msr->data = 0;
2471
2472 switch (msr->index) {
2473 case MSR_AMD64_DE_CFG:
2474 if (cpu_feature_enabled(X86_FEATURE_LFENCE_RDTSC))
2475 msr->data |= MSR_AMD64_DE_CFG_LFENCE_SERIALIZE;
2476 break;
2477 case MSR_IA32_PERF_CAPABILITIES:
2478 return 0;
2479 default:
2480 return KVM_MSR_RET_INVALID;
2481 }
2482
2483 return 0;
2484 }
2485
svm_get_msr(struct kvm_vcpu * vcpu,struct msr_data * msr_info)2486 static int svm_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
2487 {
2488 struct vcpu_svm *svm = to_svm(vcpu);
2489
2490 switch (msr_info->index) {
2491 case MSR_STAR:
2492 msr_info->data = svm->vmcb->save.star;
2493 break;
2494 #ifdef CONFIG_X86_64
2495 case MSR_LSTAR:
2496 msr_info->data = svm->vmcb->save.lstar;
2497 break;
2498 case MSR_CSTAR:
2499 msr_info->data = svm->vmcb->save.cstar;
2500 break;
2501 case MSR_KERNEL_GS_BASE:
2502 msr_info->data = svm->vmcb->save.kernel_gs_base;
2503 break;
2504 case MSR_SYSCALL_MASK:
2505 msr_info->data = svm->vmcb->save.sfmask;
2506 break;
2507 #endif
2508 case MSR_IA32_SYSENTER_CS:
2509 msr_info->data = svm->vmcb->save.sysenter_cs;
2510 break;
2511 case MSR_IA32_SYSENTER_EIP:
2512 msr_info->data = svm->sysenter_eip;
2513 break;
2514 case MSR_IA32_SYSENTER_ESP:
2515 msr_info->data = svm->sysenter_esp;
2516 break;
2517 case MSR_TSC_AUX:
2518 if (!boot_cpu_has(X86_FEATURE_RDTSCP))
2519 return 1;
2520 if (!msr_info->host_initiated &&
2521 !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP))
2522 return 1;
2523 msr_info->data = svm->tsc_aux;
2524 break;
2525 /*
2526 * Nobody will change the following 5 values in the VMCB so we can
2527 * safely return them on rdmsr. They will always be 0 until LBRV is
2528 * implemented.
2529 */
2530 case MSR_IA32_DEBUGCTLMSR:
2531 msr_info->data = svm->vmcb->save.dbgctl;
2532 break;
2533 case MSR_IA32_LASTBRANCHFROMIP:
2534 msr_info->data = svm->vmcb->save.br_from;
2535 break;
2536 case MSR_IA32_LASTBRANCHTOIP:
2537 msr_info->data = svm->vmcb->save.br_to;
2538 break;
2539 case MSR_IA32_LASTINTFROMIP:
2540 msr_info->data = svm->vmcb->save.last_excp_from;
2541 break;
2542 case MSR_IA32_LASTINTTOIP:
2543 msr_info->data = svm->vmcb->save.last_excp_to;
2544 break;
2545 case MSR_VM_HSAVE_PA:
2546 msr_info->data = svm->nested.hsave_msr;
2547 break;
2548 case MSR_VM_CR:
2549 msr_info->data = svm->nested.vm_cr_msr;
2550 break;
2551 case MSR_IA32_SPEC_CTRL:
2552 if (!msr_info->host_initiated &&
2553 !guest_has_spec_ctrl_msr(vcpu))
2554 return 1;
2555
2556 msr_info->data = svm->spec_ctrl;
2557 break;
2558 case MSR_AMD64_VIRT_SPEC_CTRL:
2559 if (!msr_info->host_initiated &&
2560 !guest_cpuid_has(vcpu, X86_FEATURE_VIRT_SSBD))
2561 return 1;
2562
2563 msr_info->data = svm->virt_spec_ctrl;
2564 break;
2565 case MSR_F15H_IC_CFG: {
2566
2567 int family, model;
2568
2569 family = guest_cpuid_family(vcpu);
2570 model = guest_cpuid_model(vcpu);
2571
2572 if (family < 0 || model < 0)
2573 return kvm_get_msr_common(vcpu, msr_info);
2574
2575 msr_info->data = 0;
2576
2577 if (family == 0x15 &&
2578 (model >= 0x2 && model < 0x20))
2579 msr_info->data = 0x1E;
2580 }
2581 break;
2582 case MSR_AMD64_DE_CFG:
2583 msr_info->data = svm->msr_decfg;
2584 break;
2585 default:
2586 return kvm_get_msr_common(vcpu, msr_info);
2587 }
2588 return 0;
2589 }
2590
rdmsr_interception(struct vcpu_svm * svm)2591 static int rdmsr_interception(struct vcpu_svm *svm)
2592 {
2593 return kvm_emulate_rdmsr(&svm->vcpu);
2594 }
2595
svm_set_vm_cr(struct kvm_vcpu * vcpu,u64 data)2596 static int svm_set_vm_cr(struct kvm_vcpu *vcpu, u64 data)
2597 {
2598 struct vcpu_svm *svm = to_svm(vcpu);
2599 int svm_dis, chg_mask;
2600
2601 if (data & ~SVM_VM_CR_VALID_MASK)
2602 return 1;
2603
2604 chg_mask = SVM_VM_CR_VALID_MASK;
2605
2606 if (svm->nested.vm_cr_msr & SVM_VM_CR_SVM_DIS_MASK)
2607 chg_mask &= ~(SVM_VM_CR_SVM_LOCK_MASK | SVM_VM_CR_SVM_DIS_MASK);
2608
2609 svm->nested.vm_cr_msr &= ~chg_mask;
2610 svm->nested.vm_cr_msr |= (data & chg_mask);
2611
2612 svm_dis = svm->nested.vm_cr_msr & SVM_VM_CR_SVM_DIS_MASK;
2613
2614 /* check for svm_disable while efer.svme is set */
2615 if (svm_dis && (vcpu->arch.efer & EFER_SVME))
2616 return 1;
2617
2618 return 0;
2619 }
2620
svm_set_msr(struct kvm_vcpu * vcpu,struct msr_data * msr)2621 static int svm_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr)
2622 {
2623 struct vcpu_svm *svm = to_svm(vcpu);
2624
2625 u32 ecx = msr->index;
2626 u64 data = msr->data;
2627 switch (ecx) {
2628 case MSR_IA32_CR_PAT:
2629 if (!kvm_mtrr_valid(vcpu, MSR_IA32_CR_PAT, data))
2630 return 1;
2631 vcpu->arch.pat = data;
2632 svm->vmcb->save.g_pat = data;
2633 vmcb_mark_dirty(svm->vmcb, VMCB_NPT);
2634 break;
2635 case MSR_IA32_SPEC_CTRL:
2636 if (!msr->host_initiated &&
2637 !guest_has_spec_ctrl_msr(vcpu))
2638 return 1;
2639
2640 if (kvm_spec_ctrl_test_value(data))
2641 return 1;
2642
2643 svm->spec_ctrl = data;
2644 if (!data)
2645 break;
2646
2647 /*
2648 * For non-nested:
2649 * When it's written (to non-zero) for the first time, pass
2650 * it through.
2651 *
2652 * For nested:
2653 * The handling of the MSR bitmap for L2 guests is done in
2654 * nested_svm_vmrun_msrpm.
2655 * We update the L1 MSR bit as well since it will end up
2656 * touching the MSR anyway now.
2657 */
2658 set_msr_interception(vcpu, svm->msrpm, MSR_IA32_SPEC_CTRL, 1, 1);
2659 break;
2660 case MSR_IA32_PRED_CMD:
2661 if (!msr->host_initiated &&
2662 !guest_has_pred_cmd_msr(vcpu))
2663 return 1;
2664
2665 if (data & ~PRED_CMD_IBPB)
2666 return 1;
2667 if (!boot_cpu_has(X86_FEATURE_IBPB))
2668 return 1;
2669 if (!data)
2670 break;
2671
2672 wrmsrl(MSR_IA32_PRED_CMD, PRED_CMD_IBPB);
2673 set_msr_interception(vcpu, svm->msrpm, MSR_IA32_PRED_CMD, 0, 1);
2674 break;
2675 case MSR_AMD64_VIRT_SPEC_CTRL:
2676 if (!msr->host_initiated &&
2677 !guest_cpuid_has(vcpu, X86_FEATURE_VIRT_SSBD))
2678 return 1;
2679
2680 if (data & ~SPEC_CTRL_SSBD)
2681 return 1;
2682
2683 svm->virt_spec_ctrl = data;
2684 break;
2685 case MSR_STAR:
2686 svm->vmcb->save.star = data;
2687 break;
2688 #ifdef CONFIG_X86_64
2689 case MSR_LSTAR:
2690 svm->vmcb->save.lstar = data;
2691 break;
2692 case MSR_CSTAR:
2693 svm->vmcb->save.cstar = data;
2694 break;
2695 case MSR_KERNEL_GS_BASE:
2696 svm->vmcb->save.kernel_gs_base = data;
2697 break;
2698 case MSR_SYSCALL_MASK:
2699 svm->vmcb->save.sfmask = data;
2700 break;
2701 #endif
2702 case MSR_IA32_SYSENTER_CS:
2703 svm->vmcb->save.sysenter_cs = data;
2704 break;
2705 case MSR_IA32_SYSENTER_EIP:
2706 svm->sysenter_eip = data;
2707 svm->vmcb->save.sysenter_eip = data;
2708 break;
2709 case MSR_IA32_SYSENTER_ESP:
2710 svm->sysenter_esp = data;
2711 svm->vmcb->save.sysenter_esp = data;
2712 break;
2713 case MSR_TSC_AUX:
2714 if (!boot_cpu_has(X86_FEATURE_RDTSCP))
2715 return 1;
2716
2717 if (!msr->host_initiated &&
2718 !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP))
2719 return 1;
2720
2721 /*
2722 * This is rare, so we update the MSR here instead of using
2723 * direct_access_msrs. Doing that would require a rdmsr in
2724 * svm_vcpu_put.
2725 */
2726 svm->tsc_aux = data;
2727 wrmsrl(MSR_TSC_AUX, svm->tsc_aux);
2728 break;
2729 case MSR_IA32_DEBUGCTLMSR:
2730 if (!boot_cpu_has(X86_FEATURE_LBRV)) {
2731 vcpu_unimpl(vcpu, "%s: MSR_IA32_DEBUGCTL 0x%llx, nop\n",
2732 __func__, data);
2733 break;
2734 }
2735 if (data & DEBUGCTL_RESERVED_BITS)
2736 return 1;
2737
2738 svm->vmcb->save.dbgctl = data;
2739 vmcb_mark_dirty(svm->vmcb, VMCB_LBR);
2740 if (data & (1ULL<<0))
2741 svm_enable_lbrv(vcpu);
2742 else
2743 svm_disable_lbrv(vcpu);
2744 break;
2745 case MSR_VM_HSAVE_PA:
2746 /*
2747 * Old kernels did not validate the value written to
2748 * MSR_VM_HSAVE_PA. Allow KVM_SET_MSR to set an invalid
2749 * value to allow live migrating buggy or malicious guests
2750 * originating from those kernels.
2751 */
2752 if (!msr->host_initiated && !page_address_valid(vcpu, data))
2753 return 1;
2754
2755 svm->nested.hsave_msr = data & PAGE_MASK;
2756 break;
2757 case MSR_VM_CR:
2758 return svm_set_vm_cr(vcpu, data);
2759 case MSR_VM_IGNNE:
2760 vcpu_unimpl(vcpu, "unimplemented wrmsr: 0x%x data 0x%llx\n", ecx, data);
2761 break;
2762 case MSR_AMD64_DE_CFG: {
2763 struct kvm_msr_entry msr_entry;
2764
2765 msr_entry.index = msr->index;
2766 if (svm_get_msr_feature(&msr_entry))
2767 return 1;
2768
2769 /* Check the supported bits */
2770 if (data & ~msr_entry.data)
2771 return 1;
2772
2773 /* Don't allow the guest to change a bit, #GP */
2774 if (!msr->host_initiated && (data ^ msr_entry.data))
2775 return 1;
2776
2777 svm->msr_decfg = data;
2778 break;
2779 }
2780 case MSR_IA32_APICBASE:
2781 if (kvm_vcpu_apicv_active(vcpu))
2782 avic_update_vapic_bar(to_svm(vcpu), data);
2783 fallthrough;
2784 default:
2785 return kvm_set_msr_common(vcpu, msr);
2786 }
2787 return 0;
2788 }
2789
wrmsr_interception(struct vcpu_svm * svm)2790 static int wrmsr_interception(struct vcpu_svm *svm)
2791 {
2792 return kvm_emulate_wrmsr(&svm->vcpu);
2793 }
2794
msr_interception(struct vcpu_svm * svm)2795 static int msr_interception(struct vcpu_svm *svm)
2796 {
2797 if (svm->vmcb->control.exit_info_1)
2798 return wrmsr_interception(svm);
2799 else
2800 return rdmsr_interception(svm);
2801 }
2802
interrupt_window_interception(struct vcpu_svm * svm)2803 static int interrupt_window_interception(struct vcpu_svm *svm)
2804 {
2805 kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
2806 svm_clear_vintr(svm);
2807
2808 /*
2809 * For AVIC, the only reason to end up here is ExtINTs.
2810 * In this case AVIC was temporarily disabled for
2811 * requesting the IRQ window and we have to re-enable it.
2812 */
2813 svm_toggle_avic_for_irq_window(&svm->vcpu, true);
2814
2815 ++svm->vcpu.stat.irq_window_exits;
2816 return 1;
2817 }
2818
pause_interception(struct vcpu_svm * svm)2819 static int pause_interception(struct vcpu_svm *svm)
2820 {
2821 struct kvm_vcpu *vcpu = &svm->vcpu;
2822 bool in_kernel = (svm_get_cpl(vcpu) == 0);
2823
2824 if (!kvm_pause_in_guest(vcpu->kvm))
2825 grow_ple_window(vcpu);
2826
2827 kvm_vcpu_on_spin(vcpu, in_kernel);
2828 return 1;
2829 }
2830
nop_interception(struct vcpu_svm * svm)2831 static int nop_interception(struct vcpu_svm *svm)
2832 {
2833 return kvm_skip_emulated_instruction(&(svm->vcpu));
2834 }
2835
monitor_interception(struct vcpu_svm * svm)2836 static int monitor_interception(struct vcpu_svm *svm)
2837 {
2838 printk_once(KERN_WARNING "kvm: MONITOR instruction emulated as NOP!\n");
2839 return nop_interception(svm);
2840 }
2841
mwait_interception(struct vcpu_svm * svm)2842 static int mwait_interception(struct vcpu_svm *svm)
2843 {
2844 printk_once(KERN_WARNING "kvm: MWAIT instruction emulated as NOP!\n");
2845 return nop_interception(svm);
2846 }
2847
invpcid_interception(struct vcpu_svm * svm)2848 static int invpcid_interception(struct vcpu_svm *svm)
2849 {
2850 struct kvm_vcpu *vcpu = &svm->vcpu;
2851 unsigned long type;
2852 gva_t gva;
2853
2854 if (!guest_cpuid_has(vcpu, X86_FEATURE_INVPCID)) {
2855 kvm_queue_exception(vcpu, UD_VECTOR);
2856 return 1;
2857 }
2858
2859 /*
2860 * For an INVPCID intercept:
2861 * EXITINFO1 provides the linear address of the memory operand.
2862 * EXITINFO2 provides the contents of the register operand.
2863 */
2864 type = svm->vmcb->control.exit_info_2;
2865 gva = svm->vmcb->control.exit_info_1;
2866
2867 if (type > 3) {
2868 kvm_inject_gp(vcpu, 0);
2869 return 1;
2870 }
2871
2872 return kvm_handle_invpcid(vcpu, type, gva);
2873 }
2874
2875 static int (*const svm_exit_handlers[])(struct vcpu_svm *svm) = {
2876 [SVM_EXIT_READ_CR0] = cr_interception,
2877 [SVM_EXIT_READ_CR3] = cr_interception,
2878 [SVM_EXIT_READ_CR4] = cr_interception,
2879 [SVM_EXIT_READ_CR8] = cr_interception,
2880 [SVM_EXIT_CR0_SEL_WRITE] = cr_interception,
2881 [SVM_EXIT_WRITE_CR0] = cr_interception,
2882 [SVM_EXIT_WRITE_CR3] = cr_interception,
2883 [SVM_EXIT_WRITE_CR4] = cr_interception,
2884 [SVM_EXIT_WRITE_CR8] = cr8_write_interception,
2885 [SVM_EXIT_READ_DR0] = dr_interception,
2886 [SVM_EXIT_READ_DR1] = dr_interception,
2887 [SVM_EXIT_READ_DR2] = dr_interception,
2888 [SVM_EXIT_READ_DR3] = dr_interception,
2889 [SVM_EXIT_READ_DR4] = dr_interception,
2890 [SVM_EXIT_READ_DR5] = dr_interception,
2891 [SVM_EXIT_READ_DR6] = dr_interception,
2892 [SVM_EXIT_READ_DR7] = dr_interception,
2893 [SVM_EXIT_WRITE_DR0] = dr_interception,
2894 [SVM_EXIT_WRITE_DR1] = dr_interception,
2895 [SVM_EXIT_WRITE_DR2] = dr_interception,
2896 [SVM_EXIT_WRITE_DR3] = dr_interception,
2897 [SVM_EXIT_WRITE_DR4] = dr_interception,
2898 [SVM_EXIT_WRITE_DR5] = dr_interception,
2899 [SVM_EXIT_WRITE_DR6] = dr_interception,
2900 [SVM_EXIT_WRITE_DR7] = dr_interception,
2901 [SVM_EXIT_EXCP_BASE + DB_VECTOR] = db_interception,
2902 [SVM_EXIT_EXCP_BASE + BP_VECTOR] = bp_interception,
2903 [SVM_EXIT_EXCP_BASE + UD_VECTOR] = ud_interception,
2904 [SVM_EXIT_EXCP_BASE + PF_VECTOR] = pf_interception,
2905 [SVM_EXIT_EXCP_BASE + MC_VECTOR] = mc_interception,
2906 [SVM_EXIT_EXCP_BASE + AC_VECTOR] = ac_interception,
2907 [SVM_EXIT_EXCP_BASE + GP_VECTOR] = gp_interception,
2908 [SVM_EXIT_INTR] = intr_interception,
2909 [SVM_EXIT_NMI] = nmi_interception,
2910 [SVM_EXIT_SMI] = nop_on_interception,
2911 [SVM_EXIT_INIT] = nop_on_interception,
2912 [SVM_EXIT_VINTR] = interrupt_window_interception,
2913 [SVM_EXIT_RDPMC] = rdpmc_interception,
2914 [SVM_EXIT_CPUID] = cpuid_interception,
2915 [SVM_EXIT_IRET] = iret_interception,
2916 [SVM_EXIT_INVD] = invd_interception,
2917 [SVM_EXIT_PAUSE] = pause_interception,
2918 [SVM_EXIT_HLT] = halt_interception,
2919 [SVM_EXIT_INVLPG] = invlpg_interception,
2920 [SVM_EXIT_INVLPGA] = invlpga_interception,
2921 [SVM_EXIT_IOIO] = io_interception,
2922 [SVM_EXIT_MSR] = msr_interception,
2923 [SVM_EXIT_TASK_SWITCH] = task_switch_interception,
2924 [SVM_EXIT_SHUTDOWN] = shutdown_interception,
2925 [SVM_EXIT_VMRUN] = vmrun_interception,
2926 [SVM_EXIT_VMMCALL] = vmmcall_interception,
2927 [SVM_EXIT_VMLOAD] = vmload_interception,
2928 [SVM_EXIT_VMSAVE] = vmsave_interception,
2929 [SVM_EXIT_STGI] = stgi_interception,
2930 [SVM_EXIT_CLGI] = clgi_interception,
2931 [SVM_EXIT_SKINIT] = skinit_interception,
2932 [SVM_EXIT_WBINVD] = wbinvd_interception,
2933 [SVM_EXIT_MONITOR] = monitor_interception,
2934 [SVM_EXIT_MWAIT] = mwait_interception,
2935 [SVM_EXIT_XSETBV] = xsetbv_interception,
2936 [SVM_EXIT_RDPRU] = rdpru_interception,
2937 [SVM_EXIT_INVPCID] = invpcid_interception,
2938 [SVM_EXIT_NPF] = npf_interception,
2939 [SVM_EXIT_RSM] = rsm_interception,
2940 [SVM_EXIT_AVIC_INCOMPLETE_IPI] = avic_incomplete_ipi_interception,
2941 [SVM_EXIT_AVIC_UNACCELERATED_ACCESS] = avic_unaccelerated_access_interception,
2942 };
2943
dump_vmcb(struct kvm_vcpu * vcpu)2944 static void dump_vmcb(struct kvm_vcpu *vcpu)
2945 {
2946 struct vcpu_svm *svm = to_svm(vcpu);
2947 struct vmcb_control_area *control = &svm->vmcb->control;
2948 struct vmcb_save_area *save = &svm->vmcb->save;
2949
2950 if (!dump_invalid_vmcb) {
2951 pr_warn_ratelimited("set kvm_amd.dump_invalid_vmcb=1 to dump internal KVM state.\n");
2952 return;
2953 }
2954
2955 pr_err("VMCB Control Area:\n");
2956 pr_err("%-20s%04x\n", "cr_read:", control->intercepts[INTERCEPT_CR] & 0xffff);
2957 pr_err("%-20s%04x\n", "cr_write:", control->intercepts[INTERCEPT_CR] >> 16);
2958 pr_err("%-20s%04x\n", "dr_read:", control->intercepts[INTERCEPT_DR] & 0xffff);
2959 pr_err("%-20s%04x\n", "dr_write:", control->intercepts[INTERCEPT_DR] >> 16);
2960 pr_err("%-20s%08x\n", "exceptions:", control->intercepts[INTERCEPT_EXCEPTION]);
2961 pr_err("%-20s%08x %08x\n", "intercepts:",
2962 control->intercepts[INTERCEPT_WORD3],
2963 control->intercepts[INTERCEPT_WORD4]);
2964 pr_err("%-20s%d\n", "pause filter count:", control->pause_filter_count);
2965 pr_err("%-20s%d\n", "pause filter threshold:",
2966 control->pause_filter_thresh);
2967 pr_err("%-20s%016llx\n", "iopm_base_pa:", control->iopm_base_pa);
2968 pr_err("%-20s%016llx\n", "msrpm_base_pa:", control->msrpm_base_pa);
2969 pr_err("%-20s%016llx\n", "tsc_offset:", control->tsc_offset);
2970 pr_err("%-20s%d\n", "asid:", control->asid);
2971 pr_err("%-20s%d\n", "tlb_ctl:", control->tlb_ctl);
2972 pr_err("%-20s%08x\n", "int_ctl:", control->int_ctl);
2973 pr_err("%-20s%08x\n", "int_vector:", control->int_vector);
2974 pr_err("%-20s%08x\n", "int_state:", control->int_state);
2975 pr_err("%-20s%08x\n", "exit_code:", control->exit_code);
2976 pr_err("%-20s%016llx\n", "exit_info1:", control->exit_info_1);
2977 pr_err("%-20s%016llx\n", "exit_info2:", control->exit_info_2);
2978 pr_err("%-20s%08x\n", "exit_int_info:", control->exit_int_info);
2979 pr_err("%-20s%08x\n", "exit_int_info_err:", control->exit_int_info_err);
2980 pr_err("%-20s%lld\n", "nested_ctl:", control->nested_ctl);
2981 pr_err("%-20s%016llx\n", "nested_cr3:", control->nested_cr3);
2982 pr_err("%-20s%016llx\n", "avic_vapic_bar:", control->avic_vapic_bar);
2983 pr_err("%-20s%08x\n", "event_inj:", control->event_inj);
2984 pr_err("%-20s%08x\n", "event_inj_err:", control->event_inj_err);
2985 pr_err("%-20s%lld\n", "virt_ext:", control->virt_ext);
2986 pr_err("%-20s%016llx\n", "next_rip:", control->next_rip);
2987 pr_err("%-20s%016llx\n", "avic_backing_page:", control->avic_backing_page);
2988 pr_err("%-20s%016llx\n", "avic_logical_id:", control->avic_logical_id);
2989 pr_err("%-20s%016llx\n", "avic_physical_id:", control->avic_physical_id);
2990 pr_err("VMCB State Save Area:\n");
2991 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
2992 "es:",
2993 save->es.selector, save->es.attrib,
2994 save->es.limit, save->es.base);
2995 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
2996 "cs:",
2997 save->cs.selector, save->cs.attrib,
2998 save->cs.limit, save->cs.base);
2999 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
3000 "ss:",
3001 save->ss.selector, save->ss.attrib,
3002 save->ss.limit, save->ss.base);
3003 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
3004 "ds:",
3005 save->ds.selector, save->ds.attrib,
3006 save->ds.limit, save->ds.base);
3007 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
3008 "fs:",
3009 save->fs.selector, save->fs.attrib,
3010 save->fs.limit, save->fs.base);
3011 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
3012 "gs:",
3013 save->gs.selector, save->gs.attrib,
3014 save->gs.limit, save->gs.base);
3015 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
3016 "gdtr:",
3017 save->gdtr.selector, save->gdtr.attrib,
3018 save->gdtr.limit, save->gdtr.base);
3019 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
3020 "ldtr:",
3021 save->ldtr.selector, save->ldtr.attrib,
3022 save->ldtr.limit, save->ldtr.base);
3023 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
3024 "idtr:",
3025 save->idtr.selector, save->idtr.attrib,
3026 save->idtr.limit, save->idtr.base);
3027 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
3028 "tr:",
3029 save->tr.selector, save->tr.attrib,
3030 save->tr.limit, save->tr.base);
3031 pr_err("cpl: %d efer: %016llx\n",
3032 save->cpl, save->efer);
3033 pr_err("%-15s %016llx %-13s %016llx\n",
3034 "cr0:", save->cr0, "cr2:", save->cr2);
3035 pr_err("%-15s %016llx %-13s %016llx\n",
3036 "cr3:", save->cr3, "cr4:", save->cr4);
3037 pr_err("%-15s %016llx %-13s %016llx\n",
3038 "dr6:", save->dr6, "dr7:", save->dr7);
3039 pr_err("%-15s %016llx %-13s %016llx\n",
3040 "rip:", save->rip, "rflags:", save->rflags);
3041 pr_err("%-15s %016llx %-13s %016llx\n",
3042 "rsp:", save->rsp, "rax:", save->rax);
3043 pr_err("%-15s %016llx %-13s %016llx\n",
3044 "star:", save->star, "lstar:", save->lstar);
3045 pr_err("%-15s %016llx %-13s %016llx\n",
3046 "cstar:", save->cstar, "sfmask:", save->sfmask);
3047 pr_err("%-15s %016llx %-13s %016llx\n",
3048 "kernel_gs_base:", save->kernel_gs_base,
3049 "sysenter_cs:", save->sysenter_cs);
3050 pr_err("%-15s %016llx %-13s %016llx\n",
3051 "sysenter_esp:", save->sysenter_esp,
3052 "sysenter_eip:", save->sysenter_eip);
3053 pr_err("%-15s %016llx %-13s %016llx\n",
3054 "gpat:", save->g_pat, "dbgctl:", save->dbgctl);
3055 pr_err("%-15s %016llx %-13s %016llx\n",
3056 "br_from:", save->br_from, "br_to:", save->br_to);
3057 pr_err("%-15s %016llx %-13s %016llx\n",
3058 "excp_from:", save->last_excp_from,
3059 "excp_to:", save->last_excp_to);
3060 }
3061
svm_get_exit_info(struct kvm_vcpu * vcpu,u64 * info1,u64 * info2,u32 * intr_info,u32 * error_code)3062 static void svm_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2,
3063 u32 *intr_info, u32 *error_code)
3064 {
3065 struct vmcb_control_area *control = &to_svm(vcpu)->vmcb->control;
3066
3067 *info1 = control->exit_info_1;
3068 *info2 = control->exit_info_2;
3069 *intr_info = control->exit_int_info;
3070 if ((*intr_info & SVM_EXITINTINFO_VALID) &&
3071 (*intr_info & SVM_EXITINTINFO_VALID_ERR))
3072 *error_code = control->exit_int_info_err;
3073 else
3074 *error_code = 0;
3075 }
3076
handle_exit(struct kvm_vcpu * vcpu,fastpath_t exit_fastpath)3077 static int handle_exit(struct kvm_vcpu *vcpu, fastpath_t exit_fastpath)
3078 {
3079 struct vcpu_svm *svm = to_svm(vcpu);
3080 struct kvm_run *kvm_run = vcpu->run;
3081 u32 exit_code = svm->vmcb->control.exit_code;
3082
3083 trace_kvm_exit(exit_code, vcpu, KVM_ISA_SVM);
3084
3085 if (!svm_is_intercept(svm, INTERCEPT_CR0_WRITE))
3086 vcpu->arch.cr0 = svm->vmcb->save.cr0;
3087 if (npt_enabled)
3088 vcpu->arch.cr3 = svm->vmcb->save.cr3;
3089
3090 if (is_guest_mode(vcpu)) {
3091 int vmexit;
3092
3093 trace_kvm_nested_vmexit(exit_code, vcpu, KVM_ISA_SVM);
3094
3095 vmexit = nested_svm_exit_special(svm);
3096
3097 if (vmexit == NESTED_EXIT_CONTINUE)
3098 vmexit = nested_svm_exit_handled(svm);
3099
3100 if (vmexit == NESTED_EXIT_DONE)
3101 return 1;
3102 }
3103
3104 if (svm->vmcb->control.exit_code == SVM_EXIT_ERR) {
3105 kvm_run->exit_reason = KVM_EXIT_FAIL_ENTRY;
3106 kvm_run->fail_entry.hardware_entry_failure_reason
3107 = svm->vmcb->control.exit_code;
3108 kvm_run->fail_entry.cpu = vcpu->arch.last_vmentry_cpu;
3109 dump_vmcb(vcpu);
3110 return 0;
3111 }
3112
3113 if (exit_fastpath != EXIT_FASTPATH_NONE)
3114 return 1;
3115
3116 if (exit_code >= ARRAY_SIZE(svm_exit_handlers)
3117 || !svm_exit_handlers[exit_code]) {
3118 vcpu_unimpl(vcpu, "svm: unexpected exit reason 0x%x\n", exit_code);
3119 dump_vmcb(vcpu);
3120 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
3121 vcpu->run->internal.suberror =
3122 KVM_INTERNAL_ERROR_UNEXPECTED_EXIT_REASON;
3123 vcpu->run->internal.ndata = 2;
3124 vcpu->run->internal.data[0] = exit_code;
3125 vcpu->run->internal.data[1] = vcpu->arch.last_vmentry_cpu;
3126 return 0;
3127 }
3128
3129 #ifdef CONFIG_RETPOLINE
3130 if (exit_code == SVM_EXIT_MSR)
3131 return msr_interception(svm);
3132 else if (exit_code == SVM_EXIT_VINTR)
3133 return interrupt_window_interception(svm);
3134 else if (exit_code == SVM_EXIT_INTR)
3135 return intr_interception(svm);
3136 else if (exit_code == SVM_EXIT_HLT)
3137 return halt_interception(svm);
3138 else if (exit_code == SVM_EXIT_NPF)
3139 return npf_interception(svm);
3140 #endif
3141 return svm_exit_handlers[exit_code](svm);
3142 }
3143
reload_tss(struct kvm_vcpu * vcpu)3144 static void reload_tss(struct kvm_vcpu *vcpu)
3145 {
3146 struct svm_cpu_data *sd = per_cpu(svm_data, vcpu->cpu);
3147
3148 sd->tss_desc->type = 9; /* available 32/64-bit TSS */
3149 load_TR_desc();
3150 }
3151
pre_svm_run(struct vcpu_svm * svm)3152 static void pre_svm_run(struct vcpu_svm *svm)
3153 {
3154 struct svm_cpu_data *sd = per_cpu(svm_data, svm->vcpu.cpu);
3155
3156 if (sev_guest(svm->vcpu.kvm))
3157 return pre_sev_run(svm, svm->vcpu.cpu);
3158
3159 /* FIXME: handle wraparound of asid_generation */
3160 if (svm->asid_generation != sd->asid_generation)
3161 new_asid(svm, sd);
3162 }
3163
svm_inject_nmi(struct kvm_vcpu * vcpu)3164 static void svm_inject_nmi(struct kvm_vcpu *vcpu)
3165 {
3166 struct vcpu_svm *svm = to_svm(vcpu);
3167
3168 svm->vmcb->control.event_inj = SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_NMI;
3169 vcpu->arch.hflags |= HF_NMI_MASK;
3170 svm_set_intercept(svm, INTERCEPT_IRET);
3171 ++vcpu->stat.nmi_injections;
3172 }
3173
svm_set_irq(struct kvm_vcpu * vcpu)3174 static void svm_set_irq(struct kvm_vcpu *vcpu)
3175 {
3176 struct vcpu_svm *svm = to_svm(vcpu);
3177
3178 trace_kvm_inj_virq(vcpu->arch.interrupt.nr);
3179 ++vcpu->stat.irq_injections;
3180
3181 svm->vmcb->control.event_inj = vcpu->arch.interrupt.nr |
3182 SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_INTR;
3183 }
3184
update_cr8_intercept(struct kvm_vcpu * vcpu,int tpr,int irr)3185 static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
3186 {
3187 struct vcpu_svm *svm = to_svm(vcpu);
3188
3189 if (nested_svm_virtualize_tpr(vcpu))
3190 return;
3191
3192 svm_clr_intercept(svm, INTERCEPT_CR8_WRITE);
3193
3194 if (irr == -1)
3195 return;
3196
3197 if (tpr >= irr)
3198 svm_set_intercept(svm, INTERCEPT_CR8_WRITE);
3199 }
3200
svm_nmi_blocked(struct kvm_vcpu * vcpu)3201 bool svm_nmi_blocked(struct kvm_vcpu *vcpu)
3202 {
3203 struct vcpu_svm *svm = to_svm(vcpu);
3204 struct vmcb *vmcb = svm->vmcb;
3205 bool ret;
3206
3207 if (!gif_set(svm))
3208 return true;
3209
3210 if (is_guest_mode(vcpu) && nested_exit_on_nmi(svm))
3211 return false;
3212
3213 ret = (vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK) ||
3214 (svm->vcpu.arch.hflags & HF_NMI_MASK);
3215
3216 return ret;
3217 }
3218
svm_nmi_allowed(struct kvm_vcpu * vcpu,bool for_injection)3219 static int svm_nmi_allowed(struct kvm_vcpu *vcpu, bool for_injection)
3220 {
3221 struct vcpu_svm *svm = to_svm(vcpu);
3222 if (svm->nested.nested_run_pending)
3223 return -EBUSY;
3224
3225 /* An NMI must not be injected into L2 if it's supposed to VM-Exit. */
3226 if (for_injection && is_guest_mode(vcpu) && nested_exit_on_nmi(svm))
3227 return -EBUSY;
3228
3229 return !svm_nmi_blocked(vcpu);
3230 }
3231
svm_get_nmi_mask(struct kvm_vcpu * vcpu)3232 static bool svm_get_nmi_mask(struct kvm_vcpu *vcpu)
3233 {
3234 struct vcpu_svm *svm = to_svm(vcpu);
3235
3236 return !!(svm->vcpu.arch.hflags & HF_NMI_MASK);
3237 }
3238
svm_set_nmi_mask(struct kvm_vcpu * vcpu,bool masked)3239 static void svm_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
3240 {
3241 struct vcpu_svm *svm = to_svm(vcpu);
3242
3243 if (masked) {
3244 svm->vcpu.arch.hflags |= HF_NMI_MASK;
3245 svm_set_intercept(svm, INTERCEPT_IRET);
3246 } else {
3247 svm->vcpu.arch.hflags &= ~HF_NMI_MASK;
3248 svm_clr_intercept(svm, INTERCEPT_IRET);
3249 }
3250 }
3251
svm_interrupt_blocked(struct kvm_vcpu * vcpu)3252 bool svm_interrupt_blocked(struct kvm_vcpu *vcpu)
3253 {
3254 struct vcpu_svm *svm = to_svm(vcpu);
3255 struct vmcb *vmcb = svm->vmcb;
3256
3257 if (!gif_set(svm))
3258 return true;
3259
3260 if (is_guest_mode(vcpu)) {
3261 /* As long as interrupts are being delivered... */
3262 if ((svm->nested.ctl.int_ctl & V_INTR_MASKING_MASK)
3263 ? !(svm->nested.hsave->save.rflags & X86_EFLAGS_IF)
3264 : !(kvm_get_rflags(vcpu) & X86_EFLAGS_IF))
3265 return true;
3266
3267 /* ... vmexits aren't blocked by the interrupt shadow */
3268 if (nested_exit_on_intr(svm))
3269 return false;
3270 } else {
3271 if (!(kvm_get_rflags(vcpu) & X86_EFLAGS_IF))
3272 return true;
3273 }
3274
3275 return (vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK);
3276 }
3277
svm_interrupt_allowed(struct kvm_vcpu * vcpu,bool for_injection)3278 static int svm_interrupt_allowed(struct kvm_vcpu *vcpu, bool for_injection)
3279 {
3280 struct vcpu_svm *svm = to_svm(vcpu);
3281 if (svm->nested.nested_run_pending)
3282 return -EBUSY;
3283
3284 /*
3285 * An IRQ must not be injected into L2 if it's supposed to VM-Exit,
3286 * e.g. if the IRQ arrived asynchronously after checking nested events.
3287 */
3288 if (for_injection && is_guest_mode(vcpu) && nested_exit_on_intr(svm))
3289 return -EBUSY;
3290
3291 return !svm_interrupt_blocked(vcpu);
3292 }
3293
enable_irq_window(struct kvm_vcpu * vcpu)3294 static void enable_irq_window(struct kvm_vcpu *vcpu)
3295 {
3296 struct vcpu_svm *svm = to_svm(vcpu);
3297
3298 /*
3299 * In case GIF=0 we can't rely on the CPU to tell us when GIF becomes
3300 * 1, because that's a separate STGI/VMRUN intercept. The next time we
3301 * get that intercept, this function will be called again though and
3302 * we'll get the vintr intercept. However, if the vGIF feature is
3303 * enabled, the STGI interception will not occur. Enable the irq
3304 * window under the assumption that the hardware will set the GIF.
3305 */
3306 if (vgif_enabled(svm) || gif_set(svm)) {
3307 /*
3308 * IRQ window is not needed when AVIC is enabled,
3309 * unless we have pending ExtINT since it cannot be injected
3310 * via AVIC. In such case, we need to temporarily disable AVIC,
3311 * and fallback to injecting IRQ via V_IRQ.
3312 */
3313 svm_toggle_avic_for_irq_window(vcpu, false);
3314 svm_set_vintr(svm);
3315 }
3316 }
3317
enable_nmi_window(struct kvm_vcpu * vcpu)3318 static void enable_nmi_window(struct kvm_vcpu *vcpu)
3319 {
3320 struct vcpu_svm *svm = to_svm(vcpu);
3321
3322 if ((svm->vcpu.arch.hflags & (HF_NMI_MASK | HF_IRET_MASK))
3323 == HF_NMI_MASK)
3324 return; /* IRET will cause a vm exit */
3325
3326 if (!gif_set(svm)) {
3327 if (vgif_enabled(svm))
3328 svm_set_intercept(svm, INTERCEPT_STGI);
3329 return; /* STGI will cause a vm exit */
3330 }
3331
3332 /*
3333 * Something prevents NMI from been injected. Single step over possible
3334 * problem (IRET or exception injection or interrupt shadow)
3335 */
3336 svm->nmi_singlestep_guest_rflags = svm_get_rflags(vcpu);
3337 svm->nmi_singlestep = true;
3338 svm->vmcb->save.rflags |= (X86_EFLAGS_TF | X86_EFLAGS_RF);
3339 }
3340
svm_set_tss_addr(struct kvm * kvm,unsigned int addr)3341 static int svm_set_tss_addr(struct kvm *kvm, unsigned int addr)
3342 {
3343 return 0;
3344 }
3345
svm_set_identity_map_addr(struct kvm * kvm,u64 ident_addr)3346 static int svm_set_identity_map_addr(struct kvm *kvm, u64 ident_addr)
3347 {
3348 return 0;
3349 }
3350
svm_flush_tlb(struct kvm_vcpu * vcpu)3351 void svm_flush_tlb(struct kvm_vcpu *vcpu)
3352 {
3353 struct vcpu_svm *svm = to_svm(vcpu);
3354
3355 /*
3356 * Flush only the current ASID even if the TLB flush was invoked via
3357 * kvm_flush_remote_tlbs(). Although flushing remote TLBs requires all
3358 * ASIDs to be flushed, KVM uses a single ASID for L1 and L2, and
3359 * unconditionally does a TLB flush on both nested VM-Enter and nested
3360 * VM-Exit (via kvm_mmu_reset_context()).
3361 */
3362 if (static_cpu_has(X86_FEATURE_FLUSHBYASID))
3363 svm->vmcb->control.tlb_ctl = TLB_CONTROL_FLUSH_ASID;
3364 else
3365 svm->asid_generation--;
3366 }
3367
svm_flush_tlb_gva(struct kvm_vcpu * vcpu,gva_t gva)3368 static void svm_flush_tlb_gva(struct kvm_vcpu *vcpu, gva_t gva)
3369 {
3370 struct vcpu_svm *svm = to_svm(vcpu);
3371
3372 invlpga(gva, svm->vmcb->control.asid);
3373 }
3374
svm_prepare_guest_switch(struct kvm_vcpu * vcpu)3375 static void svm_prepare_guest_switch(struct kvm_vcpu *vcpu)
3376 {
3377 }
3378
sync_cr8_to_lapic(struct kvm_vcpu * vcpu)3379 static inline void sync_cr8_to_lapic(struct kvm_vcpu *vcpu)
3380 {
3381 struct vcpu_svm *svm = to_svm(vcpu);
3382
3383 if (nested_svm_virtualize_tpr(vcpu))
3384 return;
3385
3386 if (!svm_is_intercept(svm, INTERCEPT_CR8_WRITE)) {
3387 int cr8 = svm->vmcb->control.int_ctl & V_TPR_MASK;
3388 kvm_set_cr8(vcpu, cr8);
3389 }
3390 }
3391
sync_lapic_to_cr8(struct kvm_vcpu * vcpu)3392 static inline void sync_lapic_to_cr8(struct kvm_vcpu *vcpu)
3393 {
3394 struct vcpu_svm *svm = to_svm(vcpu);
3395 u64 cr8;
3396
3397 if (nested_svm_virtualize_tpr(vcpu) ||
3398 kvm_vcpu_apicv_active(vcpu))
3399 return;
3400
3401 cr8 = kvm_get_cr8(vcpu);
3402 svm->vmcb->control.int_ctl &= ~V_TPR_MASK;
3403 svm->vmcb->control.int_ctl |= cr8 & V_TPR_MASK;
3404 }
3405
svm_complete_interrupts(struct vcpu_svm * svm)3406 static void svm_complete_interrupts(struct vcpu_svm *svm)
3407 {
3408 u8 vector;
3409 int type;
3410 u32 exitintinfo = svm->vmcb->control.exit_int_info;
3411 unsigned int3_injected = svm->int3_injected;
3412
3413 svm->int3_injected = 0;
3414
3415 /*
3416 * If we've made progress since setting HF_IRET_MASK, we've
3417 * executed an IRET and can allow NMI injection.
3418 */
3419 if ((svm->vcpu.arch.hflags & HF_IRET_MASK)
3420 && kvm_rip_read(&svm->vcpu) != svm->nmi_iret_rip) {
3421 svm->vcpu.arch.hflags &= ~(HF_NMI_MASK | HF_IRET_MASK);
3422 kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
3423 }
3424
3425 svm->vcpu.arch.nmi_injected = false;
3426 kvm_clear_exception_queue(&svm->vcpu);
3427 kvm_clear_interrupt_queue(&svm->vcpu);
3428
3429 if (!(exitintinfo & SVM_EXITINTINFO_VALID))
3430 return;
3431
3432 kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
3433
3434 vector = exitintinfo & SVM_EXITINTINFO_VEC_MASK;
3435 type = exitintinfo & SVM_EXITINTINFO_TYPE_MASK;
3436
3437 switch (type) {
3438 case SVM_EXITINTINFO_TYPE_NMI:
3439 svm->vcpu.arch.nmi_injected = true;
3440 break;
3441 case SVM_EXITINTINFO_TYPE_EXEPT:
3442 /*
3443 * In case of software exceptions, do not reinject the vector,
3444 * but re-execute the instruction instead. Rewind RIP first
3445 * if we emulated INT3 before.
3446 */
3447 if (kvm_exception_is_soft(vector)) {
3448 if (vector == BP_VECTOR && int3_injected &&
3449 kvm_is_linear_rip(&svm->vcpu, svm->int3_rip))
3450 kvm_rip_write(&svm->vcpu,
3451 kvm_rip_read(&svm->vcpu) -
3452 int3_injected);
3453 break;
3454 }
3455 if (exitintinfo & SVM_EXITINTINFO_VALID_ERR) {
3456 u32 err = svm->vmcb->control.exit_int_info_err;
3457 kvm_requeue_exception_e(&svm->vcpu, vector, err);
3458
3459 } else
3460 kvm_requeue_exception(&svm->vcpu, vector);
3461 break;
3462 case SVM_EXITINTINFO_TYPE_INTR:
3463 kvm_queue_interrupt(&svm->vcpu, vector, false);
3464 break;
3465 default:
3466 break;
3467 }
3468 }
3469
svm_cancel_injection(struct kvm_vcpu * vcpu)3470 static void svm_cancel_injection(struct kvm_vcpu *vcpu)
3471 {
3472 struct vcpu_svm *svm = to_svm(vcpu);
3473 struct vmcb_control_area *control = &svm->vmcb->control;
3474
3475 control->exit_int_info = control->event_inj;
3476 control->exit_int_info_err = control->event_inj_err;
3477 control->event_inj = 0;
3478 svm_complete_interrupts(svm);
3479 }
3480
svm_exit_handlers_fastpath(struct kvm_vcpu * vcpu)3481 static fastpath_t svm_exit_handlers_fastpath(struct kvm_vcpu *vcpu)
3482 {
3483 if (to_svm(vcpu)->vmcb->control.exit_code == SVM_EXIT_MSR &&
3484 to_svm(vcpu)->vmcb->control.exit_info_1)
3485 return handle_fastpath_set_msr_irqoff(vcpu);
3486
3487 return EXIT_FASTPATH_NONE;
3488 }
3489
3490 void __svm_vcpu_run(unsigned long vmcb_pa, unsigned long *regs);
3491
svm_vcpu_enter_exit(struct kvm_vcpu * vcpu,struct vcpu_svm * svm)3492 static noinstr void svm_vcpu_enter_exit(struct kvm_vcpu *vcpu,
3493 struct vcpu_svm *svm)
3494 {
3495 /*
3496 * VMENTER enables interrupts (host state), but the kernel state is
3497 * interrupts disabled when this is invoked. Also tell RCU about
3498 * it. This is the same logic as for exit_to_user_mode().
3499 *
3500 * This ensures that e.g. latency analysis on the host observes
3501 * guest mode as interrupt enabled.
3502 *
3503 * guest_enter_irqoff() informs context tracking about the
3504 * transition to guest mode and if enabled adjusts RCU state
3505 * accordingly.
3506 */
3507 instrumentation_begin();
3508 trace_hardirqs_on_prepare();
3509 lockdep_hardirqs_on_prepare(CALLER_ADDR0);
3510 instrumentation_end();
3511
3512 guest_enter_irqoff();
3513 lockdep_hardirqs_on(CALLER_ADDR0);
3514
3515 __svm_vcpu_run(svm->vmcb_pa, (unsigned long *)&svm->vcpu.arch.regs);
3516
3517 #ifdef CONFIG_X86_64
3518 native_wrmsrl(MSR_GS_BASE, svm->host.gs_base);
3519 #else
3520 loadsegment(fs, svm->host.fs);
3521 #ifndef CONFIG_X86_32_LAZY_GS
3522 loadsegment(gs, svm->host.gs);
3523 #endif
3524 #endif
3525
3526 /*
3527 * VMEXIT disables interrupts (host state), but tracing and lockdep
3528 * have them in state 'on' as recorded before entering guest mode.
3529 * Same as enter_from_user_mode().
3530 *
3531 * context_tracking_guest_exit() restores host context and reinstates
3532 * RCU if enabled and required.
3533 *
3534 * This needs to be done before the below as native_read_msr()
3535 * contains a tracepoint and x86_spec_ctrl_restore_host() calls
3536 * into world and some more.
3537 */
3538 lockdep_hardirqs_off(CALLER_ADDR0);
3539 context_tracking_guest_exit();
3540
3541 instrumentation_begin();
3542 trace_hardirqs_off_finish();
3543 instrumentation_end();
3544 }
3545
svm_vcpu_run(struct kvm_vcpu * vcpu)3546 static __no_kcsan fastpath_t svm_vcpu_run(struct kvm_vcpu *vcpu)
3547 {
3548 struct vcpu_svm *svm = to_svm(vcpu);
3549
3550 svm->vmcb->save.rax = vcpu->arch.regs[VCPU_REGS_RAX];
3551 svm->vmcb->save.rsp = vcpu->arch.regs[VCPU_REGS_RSP];
3552 svm->vmcb->save.rip = vcpu->arch.regs[VCPU_REGS_RIP];
3553
3554 /*
3555 * Disable singlestep if we're injecting an interrupt/exception.
3556 * We don't want our modified rflags to be pushed on the stack where
3557 * we might not be able to easily reset them if we disabled NMI
3558 * singlestep later.
3559 */
3560 if (svm->nmi_singlestep && svm->vmcb->control.event_inj) {
3561 /*
3562 * Event injection happens before external interrupts cause a
3563 * vmexit and interrupts are disabled here, so smp_send_reschedule
3564 * is enough to force an immediate vmexit.
3565 */
3566 disable_nmi_singlestep(svm);
3567 smp_send_reschedule(vcpu->cpu);
3568 }
3569
3570 pre_svm_run(svm);
3571
3572 sync_lapic_to_cr8(vcpu);
3573
3574 svm->vmcb->save.cr2 = vcpu->arch.cr2;
3575
3576 /*
3577 * Run with all-zero DR6 unless needed, so that we can get the exact cause
3578 * of a #DB.
3579 */
3580 if (unlikely(svm->vcpu.arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT))
3581 svm_set_dr6(svm, vcpu->arch.dr6);
3582 else
3583 svm_set_dr6(svm, DR6_FIXED_1 | DR6_RTM);
3584
3585 clgi();
3586 kvm_load_guest_xsave_state(vcpu);
3587
3588 kvm_wait_lapic_expire(vcpu);
3589
3590 /*
3591 * If this vCPU has touched SPEC_CTRL, restore the guest's value if
3592 * it's non-zero. Since vmentry is serialising on affected CPUs, there
3593 * is no need to worry about the conditional branch over the wrmsr
3594 * being speculatively taken.
3595 */
3596 x86_spec_ctrl_set_guest(svm->spec_ctrl, svm->virt_spec_ctrl);
3597
3598 svm_vcpu_enter_exit(vcpu, svm);
3599
3600 /*
3601 * We do not use IBRS in the kernel. If this vCPU has used the
3602 * SPEC_CTRL MSR it may have left it on; save the value and
3603 * turn it off. This is much more efficient than blindly adding
3604 * it to the atomic save/restore list. Especially as the former
3605 * (Saving guest MSRs on vmexit) doesn't even exist in KVM.
3606 *
3607 * For non-nested case:
3608 * If the L01 MSR bitmap does not intercept the MSR, then we need to
3609 * save it.
3610 *
3611 * For nested case:
3612 * If the L02 MSR bitmap does not intercept the MSR, then we need to
3613 * save it.
3614 */
3615 if (unlikely(!msr_write_intercepted(vcpu, MSR_IA32_SPEC_CTRL)))
3616 svm->spec_ctrl = native_read_msr(MSR_IA32_SPEC_CTRL);
3617
3618 reload_tss(vcpu);
3619
3620 x86_spec_ctrl_restore_host(svm->spec_ctrl, svm->virt_spec_ctrl);
3621
3622 vcpu->arch.cr2 = svm->vmcb->save.cr2;
3623 vcpu->arch.regs[VCPU_REGS_RAX] = svm->vmcb->save.rax;
3624 vcpu->arch.regs[VCPU_REGS_RSP] = svm->vmcb->save.rsp;
3625 vcpu->arch.regs[VCPU_REGS_RIP] = svm->vmcb->save.rip;
3626
3627 if (unlikely(svm->vmcb->control.exit_code == SVM_EXIT_NMI))
3628 kvm_before_interrupt(&svm->vcpu);
3629
3630 kvm_load_host_xsave_state(vcpu);
3631 stgi();
3632
3633 /* Any pending NMI will happen here */
3634
3635 if (unlikely(svm->vmcb->control.exit_code == SVM_EXIT_NMI))
3636 kvm_after_interrupt(&svm->vcpu);
3637
3638 sync_cr8_to_lapic(vcpu);
3639
3640 svm->next_rip = 0;
3641 if (is_guest_mode(&svm->vcpu)) {
3642 sync_nested_vmcb_control(svm);
3643 svm->nested.nested_run_pending = 0;
3644 }
3645
3646 svm->vmcb->control.tlb_ctl = TLB_CONTROL_DO_NOTHING;
3647 vmcb_mark_all_clean(svm->vmcb);
3648
3649 /* if exit due to PF check for async PF */
3650 if (svm->vmcb->control.exit_code == SVM_EXIT_EXCP_BASE + PF_VECTOR)
3651 svm->vcpu.arch.apf.host_apf_flags =
3652 kvm_read_and_reset_apf_flags();
3653
3654 if (npt_enabled) {
3655 vcpu->arch.regs_avail &= ~(1 << VCPU_EXREG_PDPTR);
3656 vcpu->arch.regs_dirty &= ~(1 << VCPU_EXREG_PDPTR);
3657 }
3658
3659 /*
3660 * We need to handle MC intercepts here before the vcpu has a chance to
3661 * change the physical cpu
3662 */
3663 if (unlikely(svm->vmcb->control.exit_code ==
3664 SVM_EXIT_EXCP_BASE + MC_VECTOR))
3665 svm_handle_mce(svm);
3666
3667 svm_complete_interrupts(svm);
3668
3669 if (is_guest_mode(vcpu))
3670 return EXIT_FASTPATH_NONE;
3671
3672 return svm_exit_handlers_fastpath(vcpu);
3673 }
3674
svm_load_mmu_pgd(struct kvm_vcpu * vcpu,unsigned long root,int root_level)3675 static void svm_load_mmu_pgd(struct kvm_vcpu *vcpu, unsigned long root,
3676 int root_level)
3677 {
3678 struct vcpu_svm *svm = to_svm(vcpu);
3679 unsigned long cr3;
3680
3681 cr3 = __sme_set(root);
3682 if (npt_enabled) {
3683 svm->vmcb->control.nested_cr3 = cr3;
3684 vmcb_mark_dirty(svm->vmcb, VMCB_NPT);
3685
3686 /* Loading L2's CR3 is handled by enter_svm_guest_mode. */
3687 if (!test_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail))
3688 return;
3689 cr3 = vcpu->arch.cr3;
3690 }
3691
3692 svm->vmcb->save.cr3 = cr3;
3693 vmcb_mark_dirty(svm->vmcb, VMCB_CR);
3694 }
3695
is_disabled(void)3696 static int is_disabled(void)
3697 {
3698 u64 vm_cr;
3699
3700 rdmsrl(MSR_VM_CR, vm_cr);
3701 if (vm_cr & (1 << SVM_VM_CR_SVM_DISABLE))
3702 return 1;
3703
3704 return 0;
3705 }
3706
3707 static void
svm_patch_hypercall(struct kvm_vcpu * vcpu,unsigned char * hypercall)3708 svm_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
3709 {
3710 /*
3711 * Patch in the VMMCALL instruction:
3712 */
3713 hypercall[0] = 0x0f;
3714 hypercall[1] = 0x01;
3715 hypercall[2] = 0xd9;
3716 }
3717
svm_check_processor_compat(void)3718 static int __init svm_check_processor_compat(void)
3719 {
3720 return 0;
3721 }
3722
svm_cpu_has_accelerated_tpr(void)3723 static bool svm_cpu_has_accelerated_tpr(void)
3724 {
3725 return false;
3726 }
3727
svm_has_emulated_msr(u32 index)3728 static bool svm_has_emulated_msr(u32 index)
3729 {
3730 switch (index) {
3731 case MSR_IA32_MCG_EXT_CTL:
3732 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
3733 return false;
3734 default:
3735 break;
3736 }
3737
3738 return true;
3739 }
3740
svm_get_mt_mask(struct kvm_vcpu * vcpu,gfn_t gfn,bool is_mmio)3741 static u64 svm_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
3742 {
3743 return 0;
3744 }
3745
svm_vcpu_after_set_cpuid(struct kvm_vcpu * vcpu)3746 static void svm_vcpu_after_set_cpuid(struct kvm_vcpu *vcpu)
3747 {
3748 struct vcpu_svm *svm = to_svm(vcpu);
3749 struct kvm_cpuid_entry2 *best;
3750
3751 vcpu->arch.xsaves_enabled = guest_cpuid_has(vcpu, X86_FEATURE_XSAVE) &&
3752 boot_cpu_has(X86_FEATURE_XSAVE) &&
3753 boot_cpu_has(X86_FEATURE_XSAVES);
3754
3755 /* Update nrips enabled cache */
3756 svm->nrips_enabled = kvm_cpu_cap_has(X86_FEATURE_NRIPS) &&
3757 guest_cpuid_has(&svm->vcpu, X86_FEATURE_NRIPS);
3758
3759 /* Check again if INVPCID interception if required */
3760 svm_check_invpcid(svm);
3761
3762 /* For sev guests, the memory encryption bit is not reserved in CR3. */
3763 if (sev_guest(vcpu->kvm)) {
3764 best = kvm_find_cpuid_entry(vcpu, 0x8000001F, 0);
3765 if (best)
3766 vcpu->arch.cr3_lm_rsvd_bits &= ~(1UL << (best->ebx & 0x3f));
3767 }
3768
3769 if (!kvm_vcpu_apicv_active(vcpu))
3770 return;
3771
3772 /*
3773 * AVIC does not work with an x2APIC mode guest. If the X2APIC feature
3774 * is exposed to the guest, disable AVIC.
3775 */
3776 if (guest_cpuid_has(vcpu, X86_FEATURE_X2APIC))
3777 kvm_request_apicv_update(vcpu->kvm, false,
3778 APICV_INHIBIT_REASON_X2APIC);
3779
3780 /*
3781 * Currently, AVIC does not work with nested virtualization.
3782 * So, we disable AVIC when cpuid for SVM is set in the L1 guest.
3783 */
3784 if (nested && guest_cpuid_has(vcpu, X86_FEATURE_SVM))
3785 kvm_request_apicv_update(vcpu->kvm, false,
3786 APICV_INHIBIT_REASON_NESTED);
3787 }
3788
svm_has_wbinvd_exit(void)3789 static bool svm_has_wbinvd_exit(void)
3790 {
3791 return true;
3792 }
3793
3794 #define PRE_EX(exit) { .exit_code = (exit), \
3795 .stage = X86_ICPT_PRE_EXCEPT, }
3796 #define POST_EX(exit) { .exit_code = (exit), \
3797 .stage = X86_ICPT_POST_EXCEPT, }
3798 #define POST_MEM(exit) { .exit_code = (exit), \
3799 .stage = X86_ICPT_POST_MEMACCESS, }
3800
3801 static const struct __x86_intercept {
3802 u32 exit_code;
3803 enum x86_intercept_stage stage;
3804 } x86_intercept_map[] = {
3805 [x86_intercept_cr_read] = POST_EX(SVM_EXIT_READ_CR0),
3806 [x86_intercept_cr_write] = POST_EX(SVM_EXIT_WRITE_CR0),
3807 [x86_intercept_clts] = POST_EX(SVM_EXIT_WRITE_CR0),
3808 [x86_intercept_lmsw] = POST_EX(SVM_EXIT_WRITE_CR0),
3809 [x86_intercept_smsw] = POST_EX(SVM_EXIT_READ_CR0),
3810 [x86_intercept_dr_read] = POST_EX(SVM_EXIT_READ_DR0),
3811 [x86_intercept_dr_write] = POST_EX(SVM_EXIT_WRITE_DR0),
3812 [x86_intercept_sldt] = POST_EX(SVM_EXIT_LDTR_READ),
3813 [x86_intercept_str] = POST_EX(SVM_EXIT_TR_READ),
3814 [x86_intercept_lldt] = POST_EX(SVM_EXIT_LDTR_WRITE),
3815 [x86_intercept_ltr] = POST_EX(SVM_EXIT_TR_WRITE),
3816 [x86_intercept_sgdt] = POST_EX(SVM_EXIT_GDTR_READ),
3817 [x86_intercept_sidt] = POST_EX(SVM_EXIT_IDTR_READ),
3818 [x86_intercept_lgdt] = POST_EX(SVM_EXIT_GDTR_WRITE),
3819 [x86_intercept_lidt] = POST_EX(SVM_EXIT_IDTR_WRITE),
3820 [x86_intercept_vmrun] = POST_EX(SVM_EXIT_VMRUN),
3821 [x86_intercept_vmmcall] = POST_EX(SVM_EXIT_VMMCALL),
3822 [x86_intercept_vmload] = POST_EX(SVM_EXIT_VMLOAD),
3823 [x86_intercept_vmsave] = POST_EX(SVM_EXIT_VMSAVE),
3824 [x86_intercept_stgi] = POST_EX(SVM_EXIT_STGI),
3825 [x86_intercept_clgi] = POST_EX(SVM_EXIT_CLGI),
3826 [x86_intercept_skinit] = POST_EX(SVM_EXIT_SKINIT),
3827 [x86_intercept_invlpga] = POST_EX(SVM_EXIT_INVLPGA),
3828 [x86_intercept_rdtscp] = POST_EX(SVM_EXIT_RDTSCP),
3829 [x86_intercept_monitor] = POST_MEM(SVM_EXIT_MONITOR),
3830 [x86_intercept_mwait] = POST_EX(SVM_EXIT_MWAIT),
3831 [x86_intercept_invlpg] = POST_EX(SVM_EXIT_INVLPG),
3832 [x86_intercept_invd] = POST_EX(SVM_EXIT_INVD),
3833 [x86_intercept_wbinvd] = POST_EX(SVM_EXIT_WBINVD),
3834 [x86_intercept_wrmsr] = POST_EX(SVM_EXIT_MSR),
3835 [x86_intercept_rdtsc] = POST_EX(SVM_EXIT_RDTSC),
3836 [x86_intercept_rdmsr] = POST_EX(SVM_EXIT_MSR),
3837 [x86_intercept_rdpmc] = POST_EX(SVM_EXIT_RDPMC),
3838 [x86_intercept_cpuid] = PRE_EX(SVM_EXIT_CPUID),
3839 [x86_intercept_rsm] = PRE_EX(SVM_EXIT_RSM),
3840 [x86_intercept_pause] = PRE_EX(SVM_EXIT_PAUSE),
3841 [x86_intercept_pushf] = PRE_EX(SVM_EXIT_PUSHF),
3842 [x86_intercept_popf] = PRE_EX(SVM_EXIT_POPF),
3843 [x86_intercept_intn] = PRE_EX(SVM_EXIT_SWINT),
3844 [x86_intercept_iret] = PRE_EX(SVM_EXIT_IRET),
3845 [x86_intercept_icebp] = PRE_EX(SVM_EXIT_ICEBP),
3846 [x86_intercept_hlt] = POST_EX(SVM_EXIT_HLT),
3847 [x86_intercept_in] = POST_EX(SVM_EXIT_IOIO),
3848 [x86_intercept_ins] = POST_EX(SVM_EXIT_IOIO),
3849 [x86_intercept_out] = POST_EX(SVM_EXIT_IOIO),
3850 [x86_intercept_outs] = POST_EX(SVM_EXIT_IOIO),
3851 [x86_intercept_xsetbv] = PRE_EX(SVM_EXIT_XSETBV),
3852 };
3853
3854 #undef PRE_EX
3855 #undef POST_EX
3856 #undef POST_MEM
3857
svm_check_intercept(struct kvm_vcpu * vcpu,struct x86_instruction_info * info,enum x86_intercept_stage stage,struct x86_exception * exception)3858 static int svm_check_intercept(struct kvm_vcpu *vcpu,
3859 struct x86_instruction_info *info,
3860 enum x86_intercept_stage stage,
3861 struct x86_exception *exception)
3862 {
3863 struct vcpu_svm *svm = to_svm(vcpu);
3864 int vmexit, ret = X86EMUL_CONTINUE;
3865 struct __x86_intercept icpt_info;
3866 struct vmcb *vmcb = svm->vmcb;
3867
3868 if (info->intercept >= ARRAY_SIZE(x86_intercept_map))
3869 goto out;
3870
3871 icpt_info = x86_intercept_map[info->intercept];
3872
3873 if (stage != icpt_info.stage)
3874 goto out;
3875
3876 switch (icpt_info.exit_code) {
3877 case SVM_EXIT_READ_CR0:
3878 if (info->intercept == x86_intercept_cr_read)
3879 icpt_info.exit_code += info->modrm_reg;
3880 break;
3881 case SVM_EXIT_WRITE_CR0: {
3882 unsigned long cr0, val;
3883
3884 if (info->intercept == x86_intercept_cr_write)
3885 icpt_info.exit_code += info->modrm_reg;
3886
3887 if (icpt_info.exit_code != SVM_EXIT_WRITE_CR0 ||
3888 info->intercept == x86_intercept_clts)
3889 break;
3890
3891 if (!(vmcb_is_intercept(&svm->nested.ctl,
3892 INTERCEPT_SELECTIVE_CR0)))
3893 break;
3894
3895 cr0 = vcpu->arch.cr0 & ~SVM_CR0_SELECTIVE_MASK;
3896 val = info->src_val & ~SVM_CR0_SELECTIVE_MASK;
3897
3898 if (info->intercept == x86_intercept_lmsw) {
3899 cr0 &= 0xfUL;
3900 val &= 0xfUL;
3901 /* lmsw can't clear PE - catch this here */
3902 if (cr0 & X86_CR0_PE)
3903 val |= X86_CR0_PE;
3904 }
3905
3906 if (cr0 ^ val)
3907 icpt_info.exit_code = SVM_EXIT_CR0_SEL_WRITE;
3908
3909 break;
3910 }
3911 case SVM_EXIT_READ_DR0:
3912 case SVM_EXIT_WRITE_DR0:
3913 icpt_info.exit_code += info->modrm_reg;
3914 break;
3915 case SVM_EXIT_MSR:
3916 if (info->intercept == x86_intercept_wrmsr)
3917 vmcb->control.exit_info_1 = 1;
3918 else
3919 vmcb->control.exit_info_1 = 0;
3920 break;
3921 case SVM_EXIT_PAUSE:
3922 /*
3923 * We get this for NOP only, but pause
3924 * is rep not, check this here
3925 */
3926 if (info->rep_prefix != REPE_PREFIX)
3927 goto out;
3928 break;
3929 case SVM_EXIT_IOIO: {
3930 u64 exit_info;
3931 u32 bytes;
3932
3933 if (info->intercept == x86_intercept_in ||
3934 info->intercept == x86_intercept_ins) {
3935 exit_info = ((info->src_val & 0xffff) << 16) |
3936 SVM_IOIO_TYPE_MASK;
3937 bytes = info->dst_bytes;
3938 } else {
3939 exit_info = (info->dst_val & 0xffff) << 16;
3940 bytes = info->src_bytes;
3941 }
3942
3943 if (info->intercept == x86_intercept_outs ||
3944 info->intercept == x86_intercept_ins)
3945 exit_info |= SVM_IOIO_STR_MASK;
3946
3947 if (info->rep_prefix)
3948 exit_info |= SVM_IOIO_REP_MASK;
3949
3950 bytes = min(bytes, 4u);
3951
3952 exit_info |= bytes << SVM_IOIO_SIZE_SHIFT;
3953
3954 exit_info |= (u32)info->ad_bytes << (SVM_IOIO_ASIZE_SHIFT - 1);
3955
3956 vmcb->control.exit_info_1 = exit_info;
3957 vmcb->control.exit_info_2 = info->next_rip;
3958
3959 break;
3960 }
3961 default:
3962 break;
3963 }
3964
3965 /* TODO: Advertise NRIPS to guest hypervisor unconditionally */
3966 if (static_cpu_has(X86_FEATURE_NRIPS))
3967 vmcb->control.next_rip = info->next_rip;
3968 vmcb->control.exit_code = icpt_info.exit_code;
3969 vmexit = nested_svm_exit_handled(svm);
3970
3971 ret = (vmexit == NESTED_EXIT_DONE) ? X86EMUL_INTERCEPTED
3972 : X86EMUL_CONTINUE;
3973
3974 out:
3975 return ret;
3976 }
3977
svm_handle_exit_irqoff(struct kvm_vcpu * vcpu)3978 static void svm_handle_exit_irqoff(struct kvm_vcpu *vcpu)
3979 {
3980 if (to_svm(vcpu)->vmcb->control.exit_code == SVM_EXIT_INTR)
3981 vcpu->arch.at_instruction_boundary = true;
3982 }
3983
svm_sched_in(struct kvm_vcpu * vcpu,int cpu)3984 static void svm_sched_in(struct kvm_vcpu *vcpu, int cpu)
3985 {
3986 if (!kvm_pause_in_guest(vcpu->kvm))
3987 shrink_ple_window(vcpu);
3988 }
3989
svm_setup_mce(struct kvm_vcpu * vcpu)3990 static void svm_setup_mce(struct kvm_vcpu *vcpu)
3991 {
3992 /* [63:9] are reserved. */
3993 vcpu->arch.mcg_cap &= 0x1ff;
3994 }
3995
svm_smi_blocked(struct kvm_vcpu * vcpu)3996 bool svm_smi_blocked(struct kvm_vcpu *vcpu)
3997 {
3998 struct vcpu_svm *svm = to_svm(vcpu);
3999
4000 /* Per APM Vol.2 15.22.2 "Response to SMI" */
4001 if (!gif_set(svm))
4002 return true;
4003
4004 return is_smm(vcpu);
4005 }
4006
svm_smi_allowed(struct kvm_vcpu * vcpu,bool for_injection)4007 static int svm_smi_allowed(struct kvm_vcpu *vcpu, bool for_injection)
4008 {
4009 struct vcpu_svm *svm = to_svm(vcpu);
4010 if (svm->nested.nested_run_pending)
4011 return -EBUSY;
4012
4013 /* An SMI must not be injected into L2 if it's supposed to VM-Exit. */
4014 if (for_injection && is_guest_mode(vcpu) && nested_exit_on_smi(svm))
4015 return -EBUSY;
4016
4017 return !svm_smi_blocked(vcpu);
4018 }
4019
svm_pre_enter_smm(struct kvm_vcpu * vcpu,char * smstate)4020 static int svm_pre_enter_smm(struct kvm_vcpu *vcpu, char *smstate)
4021 {
4022 struct vcpu_svm *svm = to_svm(vcpu);
4023 int ret;
4024
4025 if (is_guest_mode(vcpu)) {
4026 /* FED8h - SVM Guest */
4027 put_smstate(u64, smstate, 0x7ed8, 1);
4028 /* FEE0h - SVM Guest VMCB Physical Address */
4029 put_smstate(u64, smstate, 0x7ee0, svm->nested.vmcb12_gpa);
4030
4031 svm->vmcb->save.rax = vcpu->arch.regs[VCPU_REGS_RAX];
4032 svm->vmcb->save.rsp = vcpu->arch.regs[VCPU_REGS_RSP];
4033 svm->vmcb->save.rip = vcpu->arch.regs[VCPU_REGS_RIP];
4034
4035 ret = nested_svm_vmexit(svm);
4036 if (ret)
4037 return ret;
4038 }
4039 return 0;
4040 }
4041
svm_pre_leave_smm(struct kvm_vcpu * vcpu,const char * smstate)4042 static int svm_pre_leave_smm(struct kvm_vcpu *vcpu, const char *smstate)
4043 {
4044 struct vcpu_svm *svm = to_svm(vcpu);
4045 struct kvm_host_map map;
4046 int ret = 0;
4047
4048 if (guest_cpuid_has(vcpu, X86_FEATURE_LM)) {
4049 u64 saved_efer = GET_SMSTATE(u64, smstate, 0x7ed0);
4050 u64 guest = GET_SMSTATE(u64, smstate, 0x7ed8);
4051 u64 vmcb12_gpa = GET_SMSTATE(u64, smstate, 0x7ee0);
4052
4053 if (guest) {
4054 if (!guest_cpuid_has(vcpu, X86_FEATURE_SVM))
4055 return 1;
4056
4057 if (!(saved_efer & EFER_SVME))
4058 return 1;
4059
4060 if (kvm_vcpu_map(&svm->vcpu,
4061 gpa_to_gfn(vmcb12_gpa), &map) == -EINVAL)
4062 return 1;
4063
4064 if (svm_allocate_nested(svm))
4065 return 1;
4066
4067 ret = enter_svm_guest_mode(svm, vmcb12_gpa, map.hva);
4068 kvm_vcpu_unmap(&svm->vcpu, &map, true);
4069 }
4070 }
4071
4072 return ret;
4073 }
4074
enable_smi_window(struct kvm_vcpu * vcpu)4075 static void enable_smi_window(struct kvm_vcpu *vcpu)
4076 {
4077 struct vcpu_svm *svm = to_svm(vcpu);
4078
4079 if (!gif_set(svm)) {
4080 if (vgif_enabled(svm))
4081 svm_set_intercept(svm, INTERCEPT_STGI);
4082 /* STGI will cause a vm exit */
4083 } else {
4084 /* We must be in SMM; RSM will cause a vmexit anyway. */
4085 }
4086 }
4087
svm_can_emulate_instruction(struct kvm_vcpu * vcpu,void * insn,int insn_len)4088 static bool svm_can_emulate_instruction(struct kvm_vcpu *vcpu, void *insn, int insn_len)
4089 {
4090 bool smep, smap, is_user;
4091 unsigned long cr4;
4092
4093 /* Emulation is always possible when KVM has access to all guest state. */
4094 if (!sev_guest(vcpu->kvm))
4095 return true;
4096
4097 /*
4098 * Detect and workaround Errata 1096 Fam_17h_00_0Fh.
4099 *
4100 * Errata:
4101 * When CPU raise #NPF on guest data access and vCPU CR4.SMAP=1, it is
4102 * possible that CPU microcode implementing DecodeAssist will fail
4103 * to read bytes of instruction which caused #NPF. In this case,
4104 * GuestIntrBytes field of the VMCB on a VMEXIT will incorrectly
4105 * return 0 instead of the correct guest instruction bytes.
4106 *
4107 * This happens because CPU microcode reading instruction bytes
4108 * uses a special opcode which attempts to read data using CPL=0
4109 * priviledges. The microcode reads CS:RIP and if it hits a SMAP
4110 * fault, it gives up and returns no instruction bytes.
4111 *
4112 * Detection:
4113 * We reach here in case CPU supports DecodeAssist, raised #NPF and
4114 * returned 0 in GuestIntrBytes field of the VMCB.
4115 * First, errata can only be triggered in case vCPU CR4.SMAP=1.
4116 * Second, if vCPU CR4.SMEP=1, errata could only be triggered
4117 * in case vCPU CPL==3 (Because otherwise guest would have triggered
4118 * a SMEP fault instead of #NPF).
4119 * Otherwise, vCPU CR4.SMEP=0, errata could be triggered by any vCPU CPL.
4120 * As most guests enable SMAP if they have also enabled SMEP, use above
4121 * logic in order to attempt minimize false-positive of detecting errata
4122 * while still preserving all cases semantic correctness.
4123 *
4124 * Workaround:
4125 * To determine what instruction the guest was executing, the hypervisor
4126 * will have to decode the instruction at the instruction pointer.
4127 *
4128 * In non SEV guest, hypervisor will be able to read the guest
4129 * memory to decode the instruction pointer when insn_len is zero
4130 * so we return true to indicate that decoding is possible.
4131 *
4132 * But in the SEV guest, the guest memory is encrypted with the
4133 * guest specific key and hypervisor will not be able to decode the
4134 * instruction pointer so we will not able to workaround it. Lets
4135 * print the error and request to kill the guest.
4136 */
4137 if (likely(!insn || insn_len))
4138 return true;
4139
4140 cr4 = kvm_read_cr4(vcpu);
4141 smep = cr4 & X86_CR4_SMEP;
4142 smap = cr4 & X86_CR4_SMAP;
4143 is_user = svm_get_cpl(vcpu) == 3;
4144 if (smap && (!smep || is_user)) {
4145 pr_err_ratelimited("KVM: SEV Guest triggered AMD Erratum 1096\n");
4146
4147 /*
4148 * If the fault occurred in userspace, arbitrarily inject #GP
4149 * to avoid killing the guest and to hopefully avoid confusing
4150 * the guest kernel too much, e.g. injecting #PF would not be
4151 * coherent with respect to the guest's page tables. Request
4152 * triple fault if the fault occurred in the kernel as there's
4153 * no fault that KVM can inject without confusing the guest.
4154 * In practice, the triple fault is moot as no sane SEV kernel
4155 * will execute from user memory while also running with SMAP=1.
4156 */
4157 if (is_user)
4158 kvm_inject_gp(vcpu, 0);
4159 else
4160 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
4161 }
4162
4163 return false;
4164 }
4165
svm_apic_init_signal_blocked(struct kvm_vcpu * vcpu)4166 static bool svm_apic_init_signal_blocked(struct kvm_vcpu *vcpu)
4167 {
4168 struct vcpu_svm *svm = to_svm(vcpu);
4169
4170 /*
4171 * TODO: Last condition latch INIT signals on vCPU when
4172 * vCPU is in guest-mode and vmcb12 defines intercept on INIT.
4173 * To properly emulate the INIT intercept,
4174 * svm_check_nested_events() should call nested_svm_vmexit()
4175 * if an INIT signal is pending.
4176 */
4177 return !gif_set(svm) ||
4178 (vmcb_is_intercept(&svm->vmcb->control, INTERCEPT_INIT));
4179 }
4180
svm_vm_destroy(struct kvm * kvm)4181 static void svm_vm_destroy(struct kvm *kvm)
4182 {
4183 avic_vm_destroy(kvm);
4184 sev_vm_destroy(kvm);
4185 }
4186
svm_vm_init(struct kvm * kvm)4187 static int svm_vm_init(struct kvm *kvm)
4188 {
4189 if (!pause_filter_count || !pause_filter_thresh)
4190 kvm->arch.pause_in_guest = true;
4191
4192 if (avic) {
4193 int ret = avic_vm_init(kvm);
4194 if (ret)
4195 return ret;
4196 }
4197
4198 kvm_apicv_init(kvm, avic);
4199 return 0;
4200 }
4201
4202 static struct kvm_x86_ops svm_x86_ops __initdata = {
4203 .hardware_unsetup = svm_hardware_teardown,
4204 .hardware_enable = svm_hardware_enable,
4205 .hardware_disable = svm_hardware_disable,
4206 .cpu_has_accelerated_tpr = svm_cpu_has_accelerated_tpr,
4207 .has_emulated_msr = svm_has_emulated_msr,
4208
4209 .vcpu_create = svm_create_vcpu,
4210 .vcpu_free = svm_free_vcpu,
4211 .vcpu_reset = svm_vcpu_reset,
4212
4213 .vm_size = sizeof(struct kvm_svm),
4214 .vm_init = svm_vm_init,
4215 .vm_destroy = svm_vm_destroy,
4216
4217 .prepare_guest_switch = svm_prepare_guest_switch,
4218 .vcpu_load = svm_vcpu_load,
4219 .vcpu_put = svm_vcpu_put,
4220 .vcpu_blocking = svm_vcpu_blocking,
4221 .vcpu_unblocking = svm_vcpu_unblocking,
4222
4223 .update_exception_bitmap = update_exception_bitmap,
4224 .get_msr_feature = svm_get_msr_feature,
4225 .get_msr = svm_get_msr,
4226 .set_msr = svm_set_msr,
4227 .get_segment_base = svm_get_segment_base,
4228 .get_segment = svm_get_segment,
4229 .set_segment = svm_set_segment,
4230 .get_cpl = svm_get_cpl,
4231 .get_cs_db_l_bits = kvm_get_cs_db_l_bits,
4232 .set_cr0 = svm_set_cr0,
4233 .is_valid_cr4 = svm_is_valid_cr4,
4234 .set_cr4 = svm_set_cr4,
4235 .set_efer = svm_set_efer,
4236 .get_idt = svm_get_idt,
4237 .set_idt = svm_set_idt,
4238 .get_gdt = svm_get_gdt,
4239 .set_gdt = svm_set_gdt,
4240 .set_dr7 = svm_set_dr7,
4241 .sync_dirty_debug_regs = svm_sync_dirty_debug_regs,
4242 .cache_reg = svm_cache_reg,
4243 .get_rflags = svm_get_rflags,
4244 .set_rflags = svm_set_rflags,
4245
4246 .tlb_flush_all = svm_flush_tlb,
4247 .tlb_flush_current = svm_flush_tlb,
4248 .tlb_flush_gva = svm_flush_tlb_gva,
4249 .tlb_flush_guest = svm_flush_tlb,
4250
4251 .run = svm_vcpu_run,
4252 .handle_exit = handle_exit,
4253 .skip_emulated_instruction = skip_emulated_instruction,
4254 .update_emulated_instruction = NULL,
4255 .set_interrupt_shadow = svm_set_interrupt_shadow,
4256 .get_interrupt_shadow = svm_get_interrupt_shadow,
4257 .patch_hypercall = svm_patch_hypercall,
4258 .set_irq = svm_set_irq,
4259 .set_nmi = svm_inject_nmi,
4260 .queue_exception = svm_queue_exception,
4261 .cancel_injection = svm_cancel_injection,
4262 .interrupt_allowed = svm_interrupt_allowed,
4263 .nmi_allowed = svm_nmi_allowed,
4264 .get_nmi_mask = svm_get_nmi_mask,
4265 .set_nmi_mask = svm_set_nmi_mask,
4266 .enable_nmi_window = enable_nmi_window,
4267 .enable_irq_window = enable_irq_window,
4268 .update_cr8_intercept = update_cr8_intercept,
4269 .set_virtual_apic_mode = svm_set_virtual_apic_mode,
4270 .refresh_apicv_exec_ctrl = svm_refresh_apicv_exec_ctrl,
4271 .check_apicv_inhibit_reasons = svm_check_apicv_inhibit_reasons,
4272 .pre_update_apicv_exec_ctrl = svm_pre_update_apicv_exec_ctrl,
4273 .load_eoi_exitmap = svm_load_eoi_exitmap,
4274 .hwapic_irr_update = svm_hwapic_irr_update,
4275 .hwapic_isr_update = svm_hwapic_isr_update,
4276 .sync_pir_to_irr = kvm_lapic_find_highest_irr,
4277 .apicv_post_state_restore = avic_post_state_restore,
4278
4279 .set_tss_addr = svm_set_tss_addr,
4280 .set_identity_map_addr = svm_set_identity_map_addr,
4281 .get_mt_mask = svm_get_mt_mask,
4282
4283 .get_exit_info = svm_get_exit_info,
4284
4285 .vcpu_after_set_cpuid = svm_vcpu_after_set_cpuid,
4286
4287 .has_wbinvd_exit = svm_has_wbinvd_exit,
4288
4289 .write_l1_tsc_offset = svm_write_l1_tsc_offset,
4290
4291 .load_mmu_pgd = svm_load_mmu_pgd,
4292
4293 .check_intercept = svm_check_intercept,
4294 .handle_exit_irqoff = svm_handle_exit_irqoff,
4295
4296 .request_immediate_exit = __kvm_request_immediate_exit,
4297
4298 .sched_in = svm_sched_in,
4299
4300 .pmu_ops = &amd_pmu_ops,
4301 .nested_ops = &svm_nested_ops,
4302
4303 .deliver_posted_interrupt = svm_deliver_avic_intr,
4304 .dy_apicv_has_pending_interrupt = svm_dy_apicv_has_pending_interrupt,
4305 .update_pi_irte = svm_update_pi_irte,
4306 .setup_mce = svm_setup_mce,
4307
4308 .smi_allowed = svm_smi_allowed,
4309 .pre_enter_smm = svm_pre_enter_smm,
4310 .pre_leave_smm = svm_pre_leave_smm,
4311 .enable_smi_window = enable_smi_window,
4312
4313 .mem_enc_op = svm_mem_enc_op,
4314 .mem_enc_reg_region = svm_register_enc_region,
4315 .mem_enc_unreg_region = svm_unregister_enc_region,
4316 .guest_memory_reclaimed = sev_guest_memory_reclaimed,
4317
4318 .can_emulate_instruction = svm_can_emulate_instruction,
4319
4320 .apic_init_signal_blocked = svm_apic_init_signal_blocked,
4321
4322 .msr_filter_changed = svm_msr_filter_changed,
4323 };
4324
4325 static struct kvm_x86_init_ops svm_init_ops __initdata = {
4326 .cpu_has_kvm_support = has_svm,
4327 .disabled_by_bios = is_disabled,
4328 .hardware_setup = svm_hardware_setup,
4329 .check_processor_compatibility = svm_check_processor_compat,
4330
4331 .runtime_ops = &svm_x86_ops,
4332 };
4333
svm_init(void)4334 static int __init svm_init(void)
4335 {
4336 __unused_size_checks();
4337
4338 return kvm_init(&svm_init_ops, sizeof(struct vcpu_svm),
4339 __alignof__(struct vcpu_svm), THIS_MODULE);
4340 }
4341
svm_exit(void)4342 static void __exit svm_exit(void)
4343 {
4344 kvm_exit();
4345 }
4346
4347 module_init(svm_init)
4348 module_exit(svm_exit)
4349