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 #include <linux/cc_platform.h>
29
30 #include <asm/apic.h>
31 #include <asm/perf_event.h>
32 #include <asm/tlbflush.h>
33 #include <asm/desc.h>
34 #include <asm/debugreg.h>
35 #include <asm/kvm_para.h>
36 #include <asm/irq_remapping.h>
37 #include <asm/spec-ctrl.h>
38 #include <asm/cpu_device_id.h>
39 #include <asm/traps.h>
40 #include <asm/fpu/api.h>
41
42 #include <asm/virtext.h>
43 #include "trace.h"
44
45 #include "svm.h"
46 #include "svm_ops.h"
47
48 #include "kvm_onhyperv.h"
49 #include "svm_onhyperv.h"
50
51 MODULE_AUTHOR("Qumranet");
52 MODULE_LICENSE("GPL");
53
54 #ifdef MODULE
55 static const struct x86_cpu_id svm_cpu_id[] = {
56 X86_MATCH_FEATURE(X86_FEATURE_SVM, NULL),
57 {}
58 };
59 MODULE_DEVICE_TABLE(x86cpu, svm_cpu_id);
60 #endif
61
62 #define SEG_TYPE_LDT 2
63 #define SEG_TYPE_BUSY_TSS16 3
64
65 static bool erratum_383_found __read_mostly;
66
67 u32 msrpm_offsets[MSRPM_OFFSETS] __read_mostly;
68
69 /*
70 * Set osvw_len to higher value when updated Revision Guides
71 * are published and we know what the new status bits are
72 */
73 static uint64_t osvw_len = 4, osvw_status;
74
75 static DEFINE_PER_CPU(u64, current_tsc_ratio);
76
77 static const struct svm_direct_access_msrs {
78 u32 index; /* Index of the MSR */
79 bool always; /* True if intercept is initially cleared */
80 } direct_access_msrs[MAX_DIRECT_ACCESS_MSRS] = {
81 { .index = MSR_STAR, .always = true },
82 { .index = MSR_IA32_SYSENTER_CS, .always = true },
83 { .index = MSR_IA32_SYSENTER_EIP, .always = false },
84 { .index = MSR_IA32_SYSENTER_ESP, .always = false },
85 #ifdef CONFIG_X86_64
86 { .index = MSR_GS_BASE, .always = true },
87 { .index = MSR_FS_BASE, .always = true },
88 { .index = MSR_KERNEL_GS_BASE, .always = true },
89 { .index = MSR_LSTAR, .always = true },
90 { .index = MSR_CSTAR, .always = true },
91 { .index = MSR_SYSCALL_MASK, .always = true },
92 #endif
93 { .index = MSR_IA32_SPEC_CTRL, .always = false },
94 { .index = MSR_IA32_PRED_CMD, .always = false },
95 { .index = MSR_IA32_LASTBRANCHFROMIP, .always = false },
96 { .index = MSR_IA32_LASTBRANCHTOIP, .always = false },
97 { .index = MSR_IA32_LASTINTFROMIP, .always = false },
98 { .index = MSR_IA32_LASTINTTOIP, .always = false },
99 { .index = MSR_EFER, .always = false },
100 { .index = MSR_IA32_CR_PAT, .always = false },
101 { .index = MSR_AMD64_SEV_ES_GHCB, .always = true },
102 { .index = MSR_TSC_AUX, .always = false },
103 { .index = MSR_INVALID, .always = false },
104 };
105
106 /*
107 * These 2 parameters are used to config the controls for Pause-Loop Exiting:
108 * pause_filter_count: On processors that support Pause filtering(indicated
109 * by CPUID Fn8000_000A_EDX), the VMCB provides a 16 bit pause filter
110 * count value. On VMRUN this value is loaded into an internal counter.
111 * Each time a pause instruction is executed, this counter is decremented
112 * until it reaches zero at which time a #VMEXIT is generated if pause
113 * intercept is enabled. Refer to AMD APM Vol 2 Section 15.14.4 Pause
114 * Intercept Filtering for more details.
115 * This also indicate if ple logic enabled.
116 *
117 * pause_filter_thresh: In addition, some processor families support advanced
118 * pause filtering (indicated by CPUID Fn8000_000A_EDX) upper bound on
119 * the amount of time a guest is allowed to execute in a pause loop.
120 * In this mode, a 16-bit pause filter threshold field is added in the
121 * VMCB. The threshold value is a cycle count that is used to reset the
122 * pause counter. As with simple pause filtering, VMRUN loads the pause
123 * count value from VMCB into an internal counter. Then, on each pause
124 * instruction the hardware checks the elapsed number of cycles since
125 * the most recent pause instruction against the pause filter threshold.
126 * If the elapsed cycle count is greater than the pause filter threshold,
127 * then the internal pause count is reloaded from the VMCB and execution
128 * continues. If the elapsed cycle count is less than the pause filter
129 * threshold, then the internal pause count is decremented. If the count
130 * value is less than zero and PAUSE intercept is enabled, a #VMEXIT is
131 * triggered. If advanced pause filtering is supported and pause filter
132 * threshold field is set to zero, the filter will operate in the simpler,
133 * count only mode.
134 */
135
136 static unsigned short pause_filter_thresh = KVM_DEFAULT_PLE_GAP;
137 module_param(pause_filter_thresh, ushort, 0444);
138
139 static unsigned short pause_filter_count = KVM_SVM_DEFAULT_PLE_WINDOW;
140 module_param(pause_filter_count, ushort, 0444);
141
142 /* Default doubles per-vcpu window every exit. */
143 static unsigned short pause_filter_count_grow = KVM_DEFAULT_PLE_WINDOW_GROW;
144 module_param(pause_filter_count_grow, ushort, 0444);
145
146 /* Default resets per-vcpu window every exit to pause_filter_count. */
147 static unsigned short pause_filter_count_shrink = KVM_DEFAULT_PLE_WINDOW_SHRINK;
148 module_param(pause_filter_count_shrink, ushort, 0444);
149
150 /* Default is to compute the maximum so we can never overflow. */
151 static unsigned short pause_filter_count_max = KVM_SVM_DEFAULT_PLE_WINDOW_MAX;
152 module_param(pause_filter_count_max, ushort, 0444);
153
154 /*
155 * Use nested page tables by default. Note, NPT may get forced off by
156 * svm_hardware_setup() if it's unsupported by hardware or the host kernel.
157 */
158 bool npt_enabled = true;
159 module_param_named(npt, npt_enabled, bool, 0444);
160
161 /* allow nested virtualization in KVM/SVM */
162 static int nested = true;
163 module_param(nested, int, S_IRUGO);
164
165 /* enable/disable Next RIP Save */
166 static int nrips = true;
167 module_param(nrips, int, 0444);
168
169 /* enable/disable Virtual VMLOAD VMSAVE */
170 static int vls = true;
171 module_param(vls, int, 0444);
172
173 /* enable/disable Virtual GIF */
174 int vgif = true;
175 module_param(vgif, int, 0444);
176
177 /* enable/disable LBR virtualization */
178 static int lbrv = true;
179 module_param(lbrv, int, 0444);
180
181 static int tsc_scaling = true;
182 module_param(tsc_scaling, int, 0444);
183
184 /*
185 * enable / disable AVIC. Because the defaults differ for APICv
186 * support between VMX and SVM we cannot use module_param_named.
187 */
188 static bool avic;
189 module_param(avic, bool, 0444);
190
191 static bool force_avic;
192 module_param_unsafe(force_avic, bool, 0444);
193
194 bool __read_mostly dump_invalid_vmcb;
195 module_param(dump_invalid_vmcb, bool, 0644);
196
197
198 bool intercept_smi = true;
199 module_param(intercept_smi, bool, 0444);
200
201
202 static bool svm_gp_erratum_intercept = true;
203
204 static u8 rsm_ins_bytes[] = "\x0f\xaa";
205
206 static unsigned long iopm_base;
207
208 struct kvm_ldttss_desc {
209 u16 limit0;
210 u16 base0;
211 unsigned base1:8, type:5, dpl:2, p:1;
212 unsigned limit1:4, zero0:3, g:1, base2:8;
213 u32 base3;
214 u32 zero1;
215 } __attribute__((packed));
216
217 DEFINE_PER_CPU(struct svm_cpu_data *, svm_data);
218
219 /*
220 * Only MSR_TSC_AUX is switched via the user return hook. EFER is switched via
221 * the VMCB, and the SYSCALL/SYSENTER MSRs are handled by VMLOAD/VMSAVE.
222 *
223 * RDTSCP and RDPID are not used in the kernel, specifically to allow KVM to
224 * defer the restoration of TSC_AUX until the CPU returns to userspace.
225 */
226 static int tsc_aux_uret_slot __read_mostly = -1;
227
228 static const u32 msrpm_ranges[] = {0, 0xc0000000, 0xc0010000};
229
230 #define NUM_MSR_MAPS ARRAY_SIZE(msrpm_ranges)
231 #define MSRS_RANGE_SIZE 2048
232 #define MSRS_IN_RANGE (MSRS_RANGE_SIZE * 8 / 2)
233
svm_msrpm_offset(u32 msr)234 u32 svm_msrpm_offset(u32 msr)
235 {
236 u32 offset;
237 int i;
238
239 for (i = 0; i < NUM_MSR_MAPS; i++) {
240 if (msr < msrpm_ranges[i] ||
241 msr >= msrpm_ranges[i] + MSRS_IN_RANGE)
242 continue;
243
244 offset = (msr - msrpm_ranges[i]) / 4; /* 4 msrs per u8 */
245 offset += (i * MSRS_RANGE_SIZE); /* add range offset */
246
247 /* Now we have the u8 offset - but need the u32 offset */
248 return offset / 4;
249 }
250
251 /* MSR not in any range */
252 return MSR_INVALID;
253 }
254
255 static void svm_flush_tlb_current(struct kvm_vcpu *vcpu);
256
get_npt_level(void)257 static int get_npt_level(void)
258 {
259 #ifdef CONFIG_X86_64
260 return pgtable_l5_enabled() ? PT64_ROOT_5LEVEL : 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 /* #GP intercept is still needed for vmware backdoor */
285 if (!enable_vmware_backdoor)
286 clr_exception_intercept(svm, GP_VECTOR);
287
288 /*
289 * Free the nested guest state, unless we are in SMM.
290 * In this case we will return to the nested guest
291 * as soon as we leave SMM.
292 */
293 if (!is_smm(vcpu))
294 svm_free_nested(svm);
295
296 } else {
297 int ret = svm_allocate_nested(svm);
298
299 if (ret) {
300 vcpu->arch.efer = old_efer;
301 return ret;
302 }
303
304 /*
305 * Never intercept #GP for SEV guests, KVM can't
306 * decrypt guest memory to workaround the erratum.
307 */
308 if (svm_gp_erratum_intercept && !sev_guest(vcpu->kvm))
309 set_exception_intercept(svm, GP_VECTOR);
310 }
311 }
312
313 svm->vmcb->save.efer = efer | EFER_SVME;
314 vmcb_mark_dirty(svm->vmcb, VMCB_CR);
315 return 0;
316 }
317
is_external_interrupt(u32 info)318 static int is_external_interrupt(u32 info)
319 {
320 info &= SVM_EVTINJ_TYPE_MASK | SVM_EVTINJ_VALID;
321 return info == (SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_INTR);
322 }
323
svm_get_interrupt_shadow(struct kvm_vcpu * vcpu)324 static u32 svm_get_interrupt_shadow(struct kvm_vcpu *vcpu)
325 {
326 struct vcpu_svm *svm = to_svm(vcpu);
327 u32 ret = 0;
328
329 if (svm->vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK)
330 ret = KVM_X86_SHADOW_INT_STI | KVM_X86_SHADOW_INT_MOV_SS;
331 return ret;
332 }
333
svm_set_interrupt_shadow(struct kvm_vcpu * vcpu,int mask)334 static void svm_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
335 {
336 struct vcpu_svm *svm = to_svm(vcpu);
337
338 if (mask == 0)
339 svm->vmcb->control.int_state &= ~SVM_INTERRUPT_SHADOW_MASK;
340 else
341 svm->vmcb->control.int_state |= SVM_INTERRUPT_SHADOW_MASK;
342
343 }
344
svm_skip_emulated_instruction(struct kvm_vcpu * vcpu)345 static int svm_skip_emulated_instruction(struct kvm_vcpu *vcpu)
346 {
347 struct vcpu_svm *svm = to_svm(vcpu);
348
349 /*
350 * SEV-ES does not expose the next RIP. The RIP update is controlled by
351 * the type of exit and the #VC handler in the guest.
352 */
353 if (sev_es_guest(vcpu->kvm))
354 goto done;
355
356 if (nrips && svm->vmcb->control.next_rip != 0) {
357 WARN_ON_ONCE(!static_cpu_has(X86_FEATURE_NRIPS));
358 svm->next_rip = svm->vmcb->control.next_rip;
359 }
360
361 if (!svm->next_rip) {
362 if (!kvm_emulate_instruction(vcpu, EMULTYPE_SKIP))
363 return 0;
364 } else {
365 kvm_rip_write(vcpu, svm->next_rip);
366 }
367
368 done:
369 svm_set_interrupt_shadow(vcpu, 0);
370
371 return 1;
372 }
373
svm_queue_exception(struct kvm_vcpu * vcpu)374 static void svm_queue_exception(struct kvm_vcpu *vcpu)
375 {
376 struct vcpu_svm *svm = to_svm(vcpu);
377 unsigned nr = vcpu->arch.exception.nr;
378 bool has_error_code = vcpu->arch.exception.has_error_code;
379 u32 error_code = vcpu->arch.exception.error_code;
380
381 kvm_deliver_exception_payload(vcpu);
382
383 if (nr == BP_VECTOR && !nrips) {
384 unsigned long rip, old_rip = kvm_rip_read(vcpu);
385
386 /*
387 * For guest debugging where we have to reinject #BP if some
388 * INT3 is guest-owned:
389 * Emulate nRIP by moving RIP forward. Will fail if injection
390 * raises a fault that is not intercepted. Still better than
391 * failing in all cases.
392 */
393 (void)svm_skip_emulated_instruction(vcpu);
394 rip = kvm_rip_read(vcpu);
395
396 if (boot_cpu_has(X86_FEATURE_NRIPS))
397 svm->vmcb->control.next_rip = rip;
398
399 svm->int3_rip = rip + svm->vmcb->save.cs.base;
400 svm->int3_injected = rip - old_rip;
401 }
402
403 svm->vmcb->control.event_inj = nr
404 | SVM_EVTINJ_VALID
405 | (has_error_code ? SVM_EVTINJ_VALID_ERR : 0)
406 | SVM_EVTINJ_TYPE_EXEPT;
407 svm->vmcb->control.event_inj_err = error_code;
408 }
409
svm_init_erratum_383(void)410 static void svm_init_erratum_383(void)
411 {
412 u32 low, high;
413 int err;
414 u64 val;
415
416 if (!static_cpu_has_bug(X86_BUG_AMD_TLB_MMATCH))
417 return;
418
419 /* Use _safe variants to not break nested virtualization */
420 val = native_read_msr_safe(MSR_AMD64_DC_CFG, &err);
421 if (err)
422 return;
423
424 val |= (1ULL << 47);
425
426 low = lower_32_bits(val);
427 high = upper_32_bits(val);
428
429 native_write_msr_safe(MSR_AMD64_DC_CFG, low, high);
430
431 erratum_383_found = true;
432 }
433
svm_init_osvw(struct kvm_vcpu * vcpu)434 static void svm_init_osvw(struct kvm_vcpu *vcpu)
435 {
436 /*
437 * Guests should see errata 400 and 415 as fixed (assuming that
438 * HLT and IO instructions are intercepted).
439 */
440 vcpu->arch.osvw.length = (osvw_len >= 3) ? (osvw_len) : 3;
441 vcpu->arch.osvw.status = osvw_status & ~(6ULL);
442
443 /*
444 * By increasing VCPU's osvw.length to 3 we are telling the guest that
445 * all osvw.status bits inside that length, including bit 0 (which is
446 * reserved for erratum 298), are valid. However, if host processor's
447 * osvw_len is 0 then osvw_status[0] carries no information. We need to
448 * be conservative here and therefore we tell the guest that erratum 298
449 * is present (because we really don't know).
450 */
451 if (osvw_len == 0 && boot_cpu_data.x86 == 0x10)
452 vcpu->arch.osvw.status |= 1;
453 }
454
has_svm(void)455 static int has_svm(void)
456 {
457 const char *msg;
458
459 if (!cpu_has_svm(&msg)) {
460 printk(KERN_INFO "has_svm: %s\n", msg);
461 return 0;
462 }
463
464 if (cc_platform_has(CC_ATTR_GUEST_MEM_ENCRYPT)) {
465 pr_info("KVM is unsupported when running as an SEV guest\n");
466 return 0;
467 }
468
469 return 1;
470 }
471
__svm_write_tsc_multiplier(u64 multiplier)472 void __svm_write_tsc_multiplier(u64 multiplier)
473 {
474 preempt_disable();
475
476 if (multiplier == __this_cpu_read(current_tsc_ratio))
477 goto out;
478
479 wrmsrl(MSR_AMD64_TSC_RATIO, multiplier);
480 __this_cpu_write(current_tsc_ratio, multiplier);
481 out:
482 preempt_enable();
483 }
484
svm_hardware_disable(void)485 static void svm_hardware_disable(void)
486 {
487 /* Make sure we clean up behind us */
488 if (tsc_scaling)
489 __svm_write_tsc_multiplier(SVM_TSC_RATIO_DEFAULT);
490
491 cpu_svm_disable();
492
493 amd_pmu_disable_virt();
494 }
495
svm_hardware_enable(void)496 static int svm_hardware_enable(void)
497 {
498
499 struct svm_cpu_data *sd;
500 uint64_t efer;
501 struct desc_struct *gdt;
502 int me = raw_smp_processor_id();
503
504 rdmsrl(MSR_EFER, efer);
505 if (efer & EFER_SVME)
506 return -EBUSY;
507
508 if (!has_svm()) {
509 pr_err("%s: err EOPNOTSUPP on %d\n", __func__, me);
510 return -EINVAL;
511 }
512 sd = per_cpu(svm_data, me);
513 if (!sd) {
514 pr_err("%s: svm_data is NULL on %d\n", __func__, me);
515 return -EINVAL;
516 }
517
518 sd->asid_generation = 1;
519 sd->max_asid = cpuid_ebx(SVM_CPUID_FUNC) - 1;
520 sd->next_asid = sd->max_asid + 1;
521 sd->min_asid = max_sev_asid + 1;
522
523 gdt = get_current_gdt_rw();
524 sd->tss_desc = (struct kvm_ldttss_desc *)(gdt + GDT_ENTRY_TSS);
525
526 wrmsrl(MSR_EFER, efer | EFER_SVME);
527
528 wrmsrl(MSR_VM_HSAVE_PA, __sme_page_pa(sd->save_area));
529
530 if (static_cpu_has(X86_FEATURE_TSCRATEMSR)) {
531 /*
532 * Set the default value, even if we don't use TSC scaling
533 * to avoid having stale value in the msr
534 */
535 __svm_write_tsc_multiplier(SVM_TSC_RATIO_DEFAULT);
536 }
537
538
539 /*
540 * Get OSVW bits.
541 *
542 * Note that it is possible to have a system with mixed processor
543 * revisions and therefore different OSVW bits. If bits are not the same
544 * on different processors then choose the worst case (i.e. if erratum
545 * is present on one processor and not on another then assume that the
546 * erratum is present everywhere).
547 */
548 if (cpu_has(&boot_cpu_data, X86_FEATURE_OSVW)) {
549 uint64_t len, status = 0;
550 int err;
551
552 len = native_read_msr_safe(MSR_AMD64_OSVW_ID_LENGTH, &err);
553 if (!err)
554 status = native_read_msr_safe(MSR_AMD64_OSVW_STATUS,
555 &err);
556
557 if (err)
558 osvw_status = osvw_len = 0;
559 else {
560 if (len < osvw_len)
561 osvw_len = len;
562 osvw_status |= status;
563 osvw_status &= (1ULL << osvw_len) - 1;
564 }
565 } else
566 osvw_status = osvw_len = 0;
567
568 svm_init_erratum_383();
569
570 amd_pmu_enable_virt();
571
572 return 0;
573 }
574
svm_cpu_uninit(int cpu)575 static void svm_cpu_uninit(int cpu)
576 {
577 struct svm_cpu_data *sd = per_cpu(svm_data, cpu);
578
579 if (!sd)
580 return;
581
582 per_cpu(svm_data, cpu) = NULL;
583 kfree(sd->sev_vmcbs);
584 __free_page(sd->save_area);
585 kfree(sd);
586 }
587
svm_cpu_init(int cpu)588 static int svm_cpu_init(int cpu)
589 {
590 struct svm_cpu_data *sd;
591 int ret = -ENOMEM;
592
593 sd = kzalloc(sizeof(struct svm_cpu_data), GFP_KERNEL);
594 if (!sd)
595 return ret;
596 sd->cpu = cpu;
597 sd->save_area = alloc_page(GFP_KERNEL | __GFP_ZERO);
598 if (!sd->save_area)
599 goto free_cpu_data;
600
601 ret = sev_cpu_init(sd);
602 if (ret)
603 goto free_save_area;
604
605 per_cpu(svm_data, cpu) = sd;
606
607 return 0;
608
609 free_save_area:
610 __free_page(sd->save_area);
611 free_cpu_data:
612 kfree(sd);
613 return ret;
614
615 }
616
direct_access_msr_slot(u32 msr)617 static int direct_access_msr_slot(u32 msr)
618 {
619 u32 i;
620
621 for (i = 0; direct_access_msrs[i].index != MSR_INVALID; i++)
622 if (direct_access_msrs[i].index == msr)
623 return i;
624
625 return -ENOENT;
626 }
627
set_shadow_msr_intercept(struct kvm_vcpu * vcpu,u32 msr,int read,int write)628 static void set_shadow_msr_intercept(struct kvm_vcpu *vcpu, u32 msr, int read,
629 int write)
630 {
631 struct vcpu_svm *svm = to_svm(vcpu);
632 int slot = direct_access_msr_slot(msr);
633
634 if (slot == -ENOENT)
635 return;
636
637 /* Set the shadow bitmaps to the desired intercept states */
638 if (read)
639 set_bit(slot, svm->shadow_msr_intercept.read);
640 else
641 clear_bit(slot, svm->shadow_msr_intercept.read);
642
643 if (write)
644 set_bit(slot, svm->shadow_msr_intercept.write);
645 else
646 clear_bit(slot, svm->shadow_msr_intercept.write);
647 }
648
valid_msr_intercept(u32 index)649 static bool valid_msr_intercept(u32 index)
650 {
651 return direct_access_msr_slot(index) != -ENOENT;
652 }
653
msr_write_intercepted(struct kvm_vcpu * vcpu,u32 msr)654 static bool msr_write_intercepted(struct kvm_vcpu *vcpu, u32 msr)
655 {
656 u8 bit_write;
657 unsigned long tmp;
658 u32 offset;
659 u32 *msrpm;
660
661 msrpm = is_guest_mode(vcpu) ? to_svm(vcpu)->nested.msrpm:
662 to_svm(vcpu)->msrpm;
663
664 offset = svm_msrpm_offset(msr);
665 bit_write = 2 * (msr & 0x0f) + 1;
666 tmp = msrpm[offset];
667
668 BUG_ON(offset == MSR_INVALID);
669
670 return !!test_bit(bit_write, &tmp);
671 }
672
set_msr_interception_bitmap(struct kvm_vcpu * vcpu,u32 * msrpm,u32 msr,int read,int write)673 static void set_msr_interception_bitmap(struct kvm_vcpu *vcpu, u32 *msrpm,
674 u32 msr, int read, int write)
675 {
676 struct vcpu_svm *svm = to_svm(vcpu);
677 u8 bit_read, bit_write;
678 unsigned long tmp;
679 u32 offset;
680
681 /*
682 * If this warning triggers extend the direct_access_msrs list at the
683 * beginning of the file
684 */
685 WARN_ON(!valid_msr_intercept(msr));
686
687 /* Enforce non allowed MSRs to trap */
688 if (read && !kvm_msr_allowed(vcpu, msr, KVM_MSR_FILTER_READ))
689 read = 0;
690
691 if (write && !kvm_msr_allowed(vcpu, msr, KVM_MSR_FILTER_WRITE))
692 write = 0;
693
694 offset = svm_msrpm_offset(msr);
695 bit_read = 2 * (msr & 0x0f);
696 bit_write = 2 * (msr & 0x0f) + 1;
697 tmp = msrpm[offset];
698
699 BUG_ON(offset == MSR_INVALID);
700
701 read ? clear_bit(bit_read, &tmp) : set_bit(bit_read, &tmp);
702 write ? clear_bit(bit_write, &tmp) : set_bit(bit_write, &tmp);
703
704 msrpm[offset] = tmp;
705
706 svm_hv_vmcb_dirty_nested_enlightenments(vcpu);
707 svm->nested.force_msr_bitmap_recalc = true;
708 }
709
set_msr_interception(struct kvm_vcpu * vcpu,u32 * msrpm,u32 msr,int read,int write)710 void set_msr_interception(struct kvm_vcpu *vcpu, u32 *msrpm, u32 msr,
711 int read, int write)
712 {
713 set_shadow_msr_intercept(vcpu, msr, read, write);
714 set_msr_interception_bitmap(vcpu, msrpm, msr, read, write);
715 }
716
svm_vcpu_alloc_msrpm(void)717 u32 *svm_vcpu_alloc_msrpm(void)
718 {
719 unsigned int order = get_order(MSRPM_SIZE);
720 struct page *pages = alloc_pages(GFP_KERNEL_ACCOUNT, order);
721 u32 *msrpm;
722
723 if (!pages)
724 return NULL;
725
726 msrpm = page_address(pages);
727 memset(msrpm, 0xff, PAGE_SIZE * (1 << order));
728
729 return msrpm;
730 }
731
svm_vcpu_init_msrpm(struct kvm_vcpu * vcpu,u32 * msrpm)732 void svm_vcpu_init_msrpm(struct kvm_vcpu *vcpu, u32 *msrpm)
733 {
734 int i;
735
736 for (i = 0; direct_access_msrs[i].index != MSR_INVALID; i++) {
737 if (!direct_access_msrs[i].always)
738 continue;
739 set_msr_interception(vcpu, msrpm, direct_access_msrs[i].index, 1, 1);
740 }
741 }
742
743
svm_vcpu_free_msrpm(u32 * msrpm)744 void svm_vcpu_free_msrpm(u32 *msrpm)
745 {
746 __free_pages(virt_to_page(msrpm), get_order(MSRPM_SIZE));
747 }
748
svm_msr_filter_changed(struct kvm_vcpu * vcpu)749 static void svm_msr_filter_changed(struct kvm_vcpu *vcpu)
750 {
751 struct vcpu_svm *svm = to_svm(vcpu);
752 u32 i;
753
754 /*
755 * Set intercept permissions for all direct access MSRs again. They
756 * will automatically get filtered through the MSR filter, so we are
757 * back in sync after this.
758 */
759 for (i = 0; direct_access_msrs[i].index != MSR_INVALID; i++) {
760 u32 msr = direct_access_msrs[i].index;
761 u32 read = test_bit(i, svm->shadow_msr_intercept.read);
762 u32 write = test_bit(i, svm->shadow_msr_intercept.write);
763
764 set_msr_interception_bitmap(vcpu, svm->msrpm, msr, read, write);
765 }
766 }
767
add_msr_offset(u32 offset)768 static void add_msr_offset(u32 offset)
769 {
770 int i;
771
772 for (i = 0; i < MSRPM_OFFSETS; ++i) {
773
774 /* Offset already in list? */
775 if (msrpm_offsets[i] == offset)
776 return;
777
778 /* Slot used by another offset? */
779 if (msrpm_offsets[i] != MSR_INVALID)
780 continue;
781
782 /* Add offset to list */
783 msrpm_offsets[i] = offset;
784
785 return;
786 }
787
788 /*
789 * If this BUG triggers the msrpm_offsets table has an overflow. Just
790 * increase MSRPM_OFFSETS in this case.
791 */
792 BUG();
793 }
794
init_msrpm_offsets(void)795 static void init_msrpm_offsets(void)
796 {
797 int i;
798
799 memset(msrpm_offsets, 0xff, sizeof(msrpm_offsets));
800
801 for (i = 0; direct_access_msrs[i].index != MSR_INVALID; i++) {
802 u32 offset;
803
804 offset = svm_msrpm_offset(direct_access_msrs[i].index);
805 BUG_ON(offset == MSR_INVALID);
806
807 add_msr_offset(offset);
808 }
809 }
810
svm_copy_lbrs(struct vmcb * to_vmcb,struct vmcb * from_vmcb)811 void svm_copy_lbrs(struct vmcb *to_vmcb, struct vmcb *from_vmcb)
812 {
813 to_vmcb->save.dbgctl = from_vmcb->save.dbgctl;
814 to_vmcb->save.br_from = from_vmcb->save.br_from;
815 to_vmcb->save.br_to = from_vmcb->save.br_to;
816 to_vmcb->save.last_excp_from = from_vmcb->save.last_excp_from;
817 to_vmcb->save.last_excp_to = from_vmcb->save.last_excp_to;
818
819 vmcb_mark_dirty(to_vmcb, VMCB_LBR);
820 }
821
svm_enable_lbrv(struct kvm_vcpu * vcpu)822 static void svm_enable_lbrv(struct kvm_vcpu *vcpu)
823 {
824 struct vcpu_svm *svm = to_svm(vcpu);
825
826 svm->vmcb->control.virt_ext |= LBR_CTL_ENABLE_MASK;
827 set_msr_interception(vcpu, svm->msrpm, MSR_IA32_LASTBRANCHFROMIP, 1, 1);
828 set_msr_interception(vcpu, svm->msrpm, MSR_IA32_LASTBRANCHTOIP, 1, 1);
829 set_msr_interception(vcpu, svm->msrpm, MSR_IA32_LASTINTFROMIP, 1, 1);
830 set_msr_interception(vcpu, svm->msrpm, MSR_IA32_LASTINTTOIP, 1, 1);
831
832 /* Move the LBR msrs to the vmcb02 so that the guest can see them. */
833 if (is_guest_mode(vcpu))
834 svm_copy_lbrs(svm->vmcb, svm->vmcb01.ptr);
835 }
836
svm_disable_lbrv(struct kvm_vcpu * vcpu)837 static void svm_disable_lbrv(struct kvm_vcpu *vcpu)
838 {
839 struct vcpu_svm *svm = to_svm(vcpu);
840
841 svm->vmcb->control.virt_ext &= ~LBR_CTL_ENABLE_MASK;
842 set_msr_interception(vcpu, svm->msrpm, MSR_IA32_LASTBRANCHFROMIP, 0, 0);
843 set_msr_interception(vcpu, svm->msrpm, MSR_IA32_LASTBRANCHTOIP, 0, 0);
844 set_msr_interception(vcpu, svm->msrpm, MSR_IA32_LASTINTFROMIP, 0, 0);
845 set_msr_interception(vcpu, svm->msrpm, MSR_IA32_LASTINTTOIP, 0, 0);
846
847 /*
848 * Move the LBR msrs back to the vmcb01 to avoid copying them
849 * on nested guest entries.
850 */
851 if (is_guest_mode(vcpu))
852 svm_copy_lbrs(svm->vmcb01.ptr, svm->vmcb);
853 }
854
svm_get_lbr_msr(struct vcpu_svm * svm,u32 index)855 static int svm_get_lbr_msr(struct vcpu_svm *svm, u32 index)
856 {
857 /*
858 * If the LBR virtualization is disabled, the LBR msrs are always
859 * kept in the vmcb01 to avoid copying them on nested guest entries.
860 *
861 * If nested, and the LBR virtualization is enabled/disabled, the msrs
862 * are moved between the vmcb01 and vmcb02 as needed.
863 */
864 struct vmcb *vmcb =
865 (svm->vmcb->control.virt_ext & LBR_CTL_ENABLE_MASK) ?
866 svm->vmcb : svm->vmcb01.ptr;
867
868 switch (index) {
869 case MSR_IA32_DEBUGCTLMSR:
870 return vmcb->save.dbgctl;
871 case MSR_IA32_LASTBRANCHFROMIP:
872 return vmcb->save.br_from;
873 case MSR_IA32_LASTBRANCHTOIP:
874 return vmcb->save.br_to;
875 case MSR_IA32_LASTINTFROMIP:
876 return vmcb->save.last_excp_from;
877 case MSR_IA32_LASTINTTOIP:
878 return vmcb->save.last_excp_to;
879 default:
880 KVM_BUG(false, svm->vcpu.kvm,
881 "%s: Unknown MSR 0x%x", __func__, index);
882 return 0;
883 }
884 }
885
svm_update_lbrv(struct kvm_vcpu * vcpu)886 void svm_update_lbrv(struct kvm_vcpu *vcpu)
887 {
888 struct vcpu_svm *svm = to_svm(vcpu);
889
890 bool enable_lbrv = svm_get_lbr_msr(svm, MSR_IA32_DEBUGCTLMSR) &
891 DEBUGCTLMSR_LBR;
892
893 bool current_enable_lbrv = !!(svm->vmcb->control.virt_ext &
894 LBR_CTL_ENABLE_MASK);
895
896 if (unlikely(is_guest_mode(vcpu) && svm->lbrv_enabled))
897 if (unlikely(svm->nested.ctl.virt_ext & LBR_CTL_ENABLE_MASK))
898 enable_lbrv = true;
899
900 if (enable_lbrv == current_enable_lbrv)
901 return;
902
903 if (enable_lbrv)
904 svm_enable_lbrv(vcpu);
905 else
906 svm_disable_lbrv(vcpu);
907 }
908
disable_nmi_singlestep(struct vcpu_svm * svm)909 void disable_nmi_singlestep(struct vcpu_svm *svm)
910 {
911 svm->nmi_singlestep = false;
912
913 if (!(svm->vcpu.guest_debug & KVM_GUESTDBG_SINGLESTEP)) {
914 /* Clear our flags if they were not set by the guest */
915 if (!(svm->nmi_singlestep_guest_rflags & X86_EFLAGS_TF))
916 svm->vmcb->save.rflags &= ~X86_EFLAGS_TF;
917 if (!(svm->nmi_singlestep_guest_rflags & X86_EFLAGS_RF))
918 svm->vmcb->save.rflags &= ~X86_EFLAGS_RF;
919 }
920 }
921
grow_ple_window(struct kvm_vcpu * vcpu)922 static void grow_ple_window(struct kvm_vcpu *vcpu)
923 {
924 struct vcpu_svm *svm = to_svm(vcpu);
925 struct vmcb_control_area *control = &svm->vmcb->control;
926 int old = control->pause_filter_count;
927
928 if (kvm_pause_in_guest(vcpu->kvm))
929 return;
930
931 control->pause_filter_count = __grow_ple_window(old,
932 pause_filter_count,
933 pause_filter_count_grow,
934 pause_filter_count_max);
935
936 if (control->pause_filter_count != old) {
937 vmcb_mark_dirty(svm->vmcb, VMCB_INTERCEPTS);
938 trace_kvm_ple_window_update(vcpu->vcpu_id,
939 control->pause_filter_count, old);
940 }
941 }
942
shrink_ple_window(struct kvm_vcpu * vcpu)943 static void shrink_ple_window(struct kvm_vcpu *vcpu)
944 {
945 struct vcpu_svm *svm = to_svm(vcpu);
946 struct vmcb_control_area *control = &svm->vmcb->control;
947 int old = control->pause_filter_count;
948
949 if (kvm_pause_in_guest(vcpu->kvm))
950 return;
951
952 control->pause_filter_count =
953 __shrink_ple_window(old,
954 pause_filter_count,
955 pause_filter_count_shrink,
956 pause_filter_count);
957 if (control->pause_filter_count != old) {
958 vmcb_mark_dirty(svm->vmcb, VMCB_INTERCEPTS);
959 trace_kvm_ple_window_update(vcpu->vcpu_id,
960 control->pause_filter_count, old);
961 }
962 }
963
svm_hardware_unsetup(void)964 static void svm_hardware_unsetup(void)
965 {
966 int cpu;
967
968 sev_hardware_unsetup();
969
970 for_each_possible_cpu(cpu)
971 svm_cpu_uninit(cpu);
972
973 __free_pages(pfn_to_page(iopm_base >> PAGE_SHIFT),
974 get_order(IOPM_SIZE));
975 iopm_base = 0;
976 }
977
init_seg(struct vmcb_seg * seg)978 static void init_seg(struct vmcb_seg *seg)
979 {
980 seg->selector = 0;
981 seg->attrib = SVM_SELECTOR_P_MASK | SVM_SELECTOR_S_MASK |
982 SVM_SELECTOR_WRITE_MASK; /* Read/Write Data Segment */
983 seg->limit = 0xffff;
984 seg->base = 0;
985 }
986
init_sys_seg(struct vmcb_seg * seg,uint32_t type)987 static void init_sys_seg(struct vmcb_seg *seg, uint32_t type)
988 {
989 seg->selector = 0;
990 seg->attrib = SVM_SELECTOR_P_MASK | type;
991 seg->limit = 0xffff;
992 seg->base = 0;
993 }
994
svm_get_l2_tsc_offset(struct kvm_vcpu * vcpu)995 static u64 svm_get_l2_tsc_offset(struct kvm_vcpu *vcpu)
996 {
997 struct vcpu_svm *svm = to_svm(vcpu);
998
999 return svm->nested.ctl.tsc_offset;
1000 }
1001
svm_get_l2_tsc_multiplier(struct kvm_vcpu * vcpu)1002 static u64 svm_get_l2_tsc_multiplier(struct kvm_vcpu *vcpu)
1003 {
1004 struct vcpu_svm *svm = to_svm(vcpu);
1005
1006 return svm->tsc_ratio_msr;
1007 }
1008
svm_write_tsc_offset(struct kvm_vcpu * vcpu,u64 offset)1009 static void svm_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset)
1010 {
1011 struct vcpu_svm *svm = to_svm(vcpu);
1012
1013 svm->vmcb01.ptr->control.tsc_offset = vcpu->arch.l1_tsc_offset;
1014 svm->vmcb->control.tsc_offset = offset;
1015 vmcb_mark_dirty(svm->vmcb, VMCB_INTERCEPTS);
1016 }
1017
svm_write_tsc_multiplier(struct kvm_vcpu * vcpu,u64 multiplier)1018 static void svm_write_tsc_multiplier(struct kvm_vcpu *vcpu, u64 multiplier)
1019 {
1020 __svm_write_tsc_multiplier(multiplier);
1021 }
1022
1023
1024 /* Evaluate instruction intercepts that depend on guest CPUID features. */
svm_recalc_instruction_intercepts(struct kvm_vcpu * vcpu,struct vcpu_svm * svm)1025 static void svm_recalc_instruction_intercepts(struct kvm_vcpu *vcpu,
1026 struct vcpu_svm *svm)
1027 {
1028 /*
1029 * Intercept INVPCID if shadow paging is enabled to sync/free shadow
1030 * roots, or if INVPCID is disabled in the guest to inject #UD.
1031 */
1032 if (kvm_cpu_cap_has(X86_FEATURE_INVPCID)) {
1033 if (!npt_enabled ||
1034 !guest_cpuid_has(&svm->vcpu, X86_FEATURE_INVPCID))
1035 svm_set_intercept(svm, INTERCEPT_INVPCID);
1036 else
1037 svm_clr_intercept(svm, INTERCEPT_INVPCID);
1038 }
1039
1040 if (kvm_cpu_cap_has(X86_FEATURE_RDTSCP)) {
1041 if (guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP))
1042 svm_clr_intercept(svm, INTERCEPT_RDTSCP);
1043 else
1044 svm_set_intercept(svm, INTERCEPT_RDTSCP);
1045 }
1046 }
1047
init_vmcb_after_set_cpuid(struct kvm_vcpu * vcpu)1048 static inline void init_vmcb_after_set_cpuid(struct kvm_vcpu *vcpu)
1049 {
1050 struct vcpu_svm *svm = to_svm(vcpu);
1051
1052 if (guest_cpuid_is_intel(vcpu)) {
1053 /*
1054 * We must intercept SYSENTER_EIP and SYSENTER_ESP
1055 * accesses because the processor only stores 32 bits.
1056 * For the same reason we cannot use virtual VMLOAD/VMSAVE.
1057 */
1058 svm_set_intercept(svm, INTERCEPT_VMLOAD);
1059 svm_set_intercept(svm, INTERCEPT_VMSAVE);
1060 svm->vmcb->control.virt_ext &= ~VIRTUAL_VMLOAD_VMSAVE_ENABLE_MASK;
1061
1062 set_msr_interception(vcpu, svm->msrpm, MSR_IA32_SYSENTER_EIP, 0, 0);
1063 set_msr_interception(vcpu, svm->msrpm, MSR_IA32_SYSENTER_ESP, 0, 0);
1064
1065 svm->v_vmload_vmsave_enabled = false;
1066 } else {
1067 /*
1068 * If hardware supports Virtual VMLOAD VMSAVE then enable it
1069 * in VMCB and clear intercepts to avoid #VMEXIT.
1070 */
1071 if (vls) {
1072 svm_clr_intercept(svm, INTERCEPT_VMLOAD);
1073 svm_clr_intercept(svm, INTERCEPT_VMSAVE);
1074 svm->vmcb->control.virt_ext |= VIRTUAL_VMLOAD_VMSAVE_ENABLE_MASK;
1075 }
1076 /* No need to intercept these MSRs */
1077 set_msr_interception(vcpu, svm->msrpm, MSR_IA32_SYSENTER_EIP, 1, 1);
1078 set_msr_interception(vcpu, svm->msrpm, MSR_IA32_SYSENTER_ESP, 1, 1);
1079 }
1080 }
1081
init_vmcb(struct kvm_vcpu * vcpu)1082 static void init_vmcb(struct kvm_vcpu *vcpu)
1083 {
1084 struct vcpu_svm *svm = to_svm(vcpu);
1085 struct vmcb *vmcb = svm->vmcb01.ptr;
1086 struct vmcb_control_area *control = &vmcb->control;
1087 struct vmcb_save_area *save = &vmcb->save;
1088
1089 svm_set_intercept(svm, INTERCEPT_CR0_READ);
1090 svm_set_intercept(svm, INTERCEPT_CR3_READ);
1091 svm_set_intercept(svm, INTERCEPT_CR4_READ);
1092 svm_set_intercept(svm, INTERCEPT_CR0_WRITE);
1093 svm_set_intercept(svm, INTERCEPT_CR3_WRITE);
1094 svm_set_intercept(svm, INTERCEPT_CR4_WRITE);
1095 if (!kvm_vcpu_apicv_active(vcpu))
1096 svm_set_intercept(svm, INTERCEPT_CR8_WRITE);
1097
1098 set_dr_intercepts(svm);
1099
1100 set_exception_intercept(svm, PF_VECTOR);
1101 set_exception_intercept(svm, UD_VECTOR);
1102 set_exception_intercept(svm, MC_VECTOR);
1103 set_exception_intercept(svm, AC_VECTOR);
1104 set_exception_intercept(svm, DB_VECTOR);
1105 /*
1106 * Guest access to VMware backdoor ports could legitimately
1107 * trigger #GP because of TSS I/O permission bitmap.
1108 * We intercept those #GP and allow access to them anyway
1109 * as VMware does. Don't intercept #GP for SEV guests as KVM can't
1110 * decrypt guest memory to decode the faulting instruction.
1111 */
1112 if (enable_vmware_backdoor && !sev_guest(vcpu->kvm))
1113 set_exception_intercept(svm, GP_VECTOR);
1114
1115 svm_set_intercept(svm, INTERCEPT_INTR);
1116 svm_set_intercept(svm, INTERCEPT_NMI);
1117
1118 if (intercept_smi)
1119 svm_set_intercept(svm, INTERCEPT_SMI);
1120
1121 svm_set_intercept(svm, INTERCEPT_SELECTIVE_CR0);
1122 svm_set_intercept(svm, INTERCEPT_RDPMC);
1123 svm_set_intercept(svm, INTERCEPT_CPUID);
1124 svm_set_intercept(svm, INTERCEPT_INVD);
1125 svm_set_intercept(svm, INTERCEPT_INVLPG);
1126 svm_set_intercept(svm, INTERCEPT_INVLPGA);
1127 svm_set_intercept(svm, INTERCEPT_IOIO_PROT);
1128 svm_set_intercept(svm, INTERCEPT_MSR_PROT);
1129 svm_set_intercept(svm, INTERCEPT_TASK_SWITCH);
1130 svm_set_intercept(svm, INTERCEPT_SHUTDOWN);
1131 svm_set_intercept(svm, INTERCEPT_VMRUN);
1132 svm_set_intercept(svm, INTERCEPT_VMMCALL);
1133 svm_set_intercept(svm, INTERCEPT_VMLOAD);
1134 svm_set_intercept(svm, INTERCEPT_VMSAVE);
1135 svm_set_intercept(svm, INTERCEPT_STGI);
1136 svm_set_intercept(svm, INTERCEPT_CLGI);
1137 svm_set_intercept(svm, INTERCEPT_SKINIT);
1138 svm_set_intercept(svm, INTERCEPT_WBINVD);
1139 svm_set_intercept(svm, INTERCEPT_XSETBV);
1140 svm_set_intercept(svm, INTERCEPT_RDPRU);
1141 svm_set_intercept(svm, INTERCEPT_RSM);
1142
1143 if (!kvm_mwait_in_guest(vcpu->kvm)) {
1144 svm_set_intercept(svm, INTERCEPT_MONITOR);
1145 svm_set_intercept(svm, INTERCEPT_MWAIT);
1146 }
1147
1148 if (!kvm_hlt_in_guest(vcpu->kvm))
1149 svm_set_intercept(svm, INTERCEPT_HLT);
1150
1151 control->iopm_base_pa = __sme_set(iopm_base);
1152 control->msrpm_base_pa = __sme_set(__pa(svm->msrpm));
1153 control->int_ctl = V_INTR_MASKING_MASK;
1154
1155 init_seg(&save->es);
1156 init_seg(&save->ss);
1157 init_seg(&save->ds);
1158 init_seg(&save->fs);
1159 init_seg(&save->gs);
1160
1161 save->cs.selector = 0xf000;
1162 save->cs.base = 0xffff0000;
1163 /* Executable/Readable Code Segment */
1164 save->cs.attrib = SVM_SELECTOR_READ_MASK | SVM_SELECTOR_P_MASK |
1165 SVM_SELECTOR_S_MASK | SVM_SELECTOR_CODE_MASK;
1166 save->cs.limit = 0xffff;
1167
1168 save->gdtr.base = 0;
1169 save->gdtr.limit = 0xffff;
1170 save->idtr.base = 0;
1171 save->idtr.limit = 0xffff;
1172
1173 init_sys_seg(&save->ldtr, SEG_TYPE_LDT);
1174 init_sys_seg(&save->tr, SEG_TYPE_BUSY_TSS16);
1175
1176 if (npt_enabled) {
1177 /* Setup VMCB for Nested Paging */
1178 control->nested_ctl |= SVM_NESTED_CTL_NP_ENABLE;
1179 svm_clr_intercept(svm, INTERCEPT_INVLPG);
1180 clr_exception_intercept(svm, PF_VECTOR);
1181 svm_clr_intercept(svm, INTERCEPT_CR3_READ);
1182 svm_clr_intercept(svm, INTERCEPT_CR3_WRITE);
1183 save->g_pat = vcpu->arch.pat;
1184 save->cr3 = 0;
1185 }
1186 svm->current_vmcb->asid_generation = 0;
1187 svm->asid = 0;
1188
1189 svm->nested.vmcb12_gpa = INVALID_GPA;
1190 svm->nested.last_vmcb12_gpa = INVALID_GPA;
1191
1192 if (!kvm_pause_in_guest(vcpu->kvm)) {
1193 control->pause_filter_count = pause_filter_count;
1194 if (pause_filter_thresh)
1195 control->pause_filter_thresh = pause_filter_thresh;
1196 svm_set_intercept(svm, INTERCEPT_PAUSE);
1197 } else {
1198 svm_clr_intercept(svm, INTERCEPT_PAUSE);
1199 }
1200
1201 svm_recalc_instruction_intercepts(vcpu, svm);
1202
1203 /*
1204 * If the host supports V_SPEC_CTRL then disable the interception
1205 * of MSR_IA32_SPEC_CTRL.
1206 */
1207 if (boot_cpu_has(X86_FEATURE_V_SPEC_CTRL))
1208 set_msr_interception(vcpu, svm->msrpm, MSR_IA32_SPEC_CTRL, 1, 1);
1209
1210 if (kvm_vcpu_apicv_active(vcpu))
1211 avic_init_vmcb(svm, vmcb);
1212
1213 if (vgif) {
1214 svm_clr_intercept(svm, INTERCEPT_STGI);
1215 svm_clr_intercept(svm, INTERCEPT_CLGI);
1216 svm->vmcb->control.int_ctl |= V_GIF_ENABLE_MASK;
1217 }
1218
1219 if (sev_guest(vcpu->kvm))
1220 sev_init_vmcb(svm);
1221
1222 svm_hv_init_vmcb(vmcb);
1223 init_vmcb_after_set_cpuid(vcpu);
1224
1225 vmcb_mark_all_dirty(vmcb);
1226
1227 enable_gif(svm);
1228 }
1229
__svm_vcpu_reset(struct kvm_vcpu * vcpu)1230 static void __svm_vcpu_reset(struct kvm_vcpu *vcpu)
1231 {
1232 struct vcpu_svm *svm = to_svm(vcpu);
1233
1234 svm_vcpu_init_msrpm(vcpu, svm->msrpm);
1235
1236 svm_init_osvw(vcpu);
1237 vcpu->arch.microcode_version = 0x01000065;
1238 svm->tsc_ratio_msr = kvm_default_tsc_scaling_ratio;
1239
1240 if (sev_es_guest(vcpu->kvm))
1241 sev_es_vcpu_reset(svm);
1242 }
1243
svm_vcpu_reset(struct kvm_vcpu * vcpu,bool init_event)1244 static void svm_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)
1245 {
1246 struct vcpu_svm *svm = to_svm(vcpu);
1247
1248 svm->spec_ctrl = 0;
1249 svm->virt_spec_ctrl = 0;
1250
1251 init_vmcb(vcpu);
1252
1253 if (!init_event)
1254 __svm_vcpu_reset(vcpu);
1255 }
1256
svm_switch_vmcb(struct vcpu_svm * svm,struct kvm_vmcb_info * target_vmcb)1257 void svm_switch_vmcb(struct vcpu_svm *svm, struct kvm_vmcb_info *target_vmcb)
1258 {
1259 svm->current_vmcb = target_vmcb;
1260 svm->vmcb = target_vmcb->ptr;
1261 }
1262
svm_vcpu_create(struct kvm_vcpu * vcpu)1263 static int svm_vcpu_create(struct kvm_vcpu *vcpu)
1264 {
1265 struct vcpu_svm *svm;
1266 struct page *vmcb01_page;
1267 struct page *vmsa_page = NULL;
1268 int err;
1269
1270 BUILD_BUG_ON(offsetof(struct vcpu_svm, vcpu) != 0);
1271 svm = to_svm(vcpu);
1272
1273 err = -ENOMEM;
1274 vmcb01_page = alloc_page(GFP_KERNEL_ACCOUNT | __GFP_ZERO);
1275 if (!vmcb01_page)
1276 goto out;
1277
1278 if (sev_es_guest(vcpu->kvm)) {
1279 /*
1280 * SEV-ES guests require a separate VMSA page used to contain
1281 * the encrypted register state of the guest.
1282 */
1283 vmsa_page = alloc_page(GFP_KERNEL_ACCOUNT | __GFP_ZERO);
1284 if (!vmsa_page)
1285 goto error_free_vmcb_page;
1286
1287 /*
1288 * SEV-ES guests maintain an encrypted version of their FPU
1289 * state which is restored and saved on VMRUN and VMEXIT.
1290 * Mark vcpu->arch.guest_fpu->fpstate as scratch so it won't
1291 * do xsave/xrstor on it.
1292 */
1293 fpstate_set_confidential(&vcpu->arch.guest_fpu);
1294 }
1295
1296 err = avic_init_vcpu(svm);
1297 if (err)
1298 goto error_free_vmsa_page;
1299
1300 svm->msrpm = svm_vcpu_alloc_msrpm();
1301 if (!svm->msrpm) {
1302 err = -ENOMEM;
1303 goto error_free_vmsa_page;
1304 }
1305
1306 svm->vmcb01.ptr = page_address(vmcb01_page);
1307 svm->vmcb01.pa = __sme_set(page_to_pfn(vmcb01_page) << PAGE_SHIFT);
1308 svm_switch_vmcb(svm, &svm->vmcb01);
1309
1310 if (vmsa_page)
1311 svm->sev_es.vmsa = page_address(vmsa_page);
1312
1313 svm->guest_state_loaded = false;
1314
1315 return 0;
1316
1317 error_free_vmsa_page:
1318 if (vmsa_page)
1319 __free_page(vmsa_page);
1320 error_free_vmcb_page:
1321 __free_page(vmcb01_page);
1322 out:
1323 return err;
1324 }
1325
svm_clear_current_vmcb(struct vmcb * vmcb)1326 static void svm_clear_current_vmcb(struct vmcb *vmcb)
1327 {
1328 int i;
1329
1330 for_each_online_cpu(i)
1331 cmpxchg(&per_cpu(svm_data, i)->current_vmcb, vmcb, NULL);
1332 }
1333
svm_vcpu_free(struct kvm_vcpu * vcpu)1334 static void svm_vcpu_free(struct kvm_vcpu *vcpu)
1335 {
1336 struct vcpu_svm *svm = to_svm(vcpu);
1337
1338 /*
1339 * The vmcb page can be recycled, causing a false negative in
1340 * svm_vcpu_load(). So, ensure that no logical CPU has this
1341 * vmcb page recorded as its current vmcb.
1342 */
1343 svm_clear_current_vmcb(svm->vmcb);
1344
1345 svm_free_nested(svm);
1346
1347 sev_free_vcpu(vcpu);
1348
1349 __free_page(pfn_to_page(__sme_clr(svm->vmcb01.pa) >> PAGE_SHIFT));
1350 __free_pages(virt_to_page(svm->msrpm), get_order(MSRPM_SIZE));
1351 }
1352
svm_prepare_switch_to_guest(struct kvm_vcpu * vcpu)1353 static void svm_prepare_switch_to_guest(struct kvm_vcpu *vcpu)
1354 {
1355 struct vcpu_svm *svm = to_svm(vcpu);
1356 struct svm_cpu_data *sd = per_cpu(svm_data, vcpu->cpu);
1357
1358 if (sev_es_guest(vcpu->kvm))
1359 sev_es_unmap_ghcb(svm);
1360
1361 if (svm->guest_state_loaded)
1362 return;
1363
1364 /*
1365 * Save additional host state that will be restored on VMEXIT (sev-es)
1366 * or subsequent vmload of host save area.
1367 */
1368 vmsave(__sme_page_pa(sd->save_area));
1369 if (sev_es_guest(vcpu->kvm)) {
1370 struct sev_es_save_area *hostsa;
1371 hostsa = (struct sev_es_save_area *)(page_address(sd->save_area) + 0x400);
1372
1373 sev_es_prepare_switch_to_guest(hostsa);
1374 }
1375
1376 if (tsc_scaling)
1377 __svm_write_tsc_multiplier(vcpu->arch.tsc_scaling_ratio);
1378
1379 if (likely(tsc_aux_uret_slot >= 0))
1380 kvm_set_user_return_msr(tsc_aux_uret_slot, svm->tsc_aux, -1ull);
1381
1382 svm->guest_state_loaded = true;
1383 }
1384
svm_prepare_host_switch(struct kvm_vcpu * vcpu)1385 static void svm_prepare_host_switch(struct kvm_vcpu *vcpu)
1386 {
1387 to_svm(vcpu)->guest_state_loaded = false;
1388 }
1389
svm_vcpu_load(struct kvm_vcpu * vcpu,int cpu)1390 static void svm_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
1391 {
1392 struct vcpu_svm *svm = to_svm(vcpu);
1393 struct svm_cpu_data *sd = per_cpu(svm_data, cpu);
1394
1395 if (sd->current_vmcb != svm->vmcb) {
1396 sd->current_vmcb = svm->vmcb;
1397 indirect_branch_prediction_barrier();
1398 }
1399 if (kvm_vcpu_apicv_active(vcpu))
1400 avic_vcpu_load(vcpu, cpu);
1401 }
1402
svm_vcpu_put(struct kvm_vcpu * vcpu)1403 static void svm_vcpu_put(struct kvm_vcpu *vcpu)
1404 {
1405 if (kvm_vcpu_apicv_active(vcpu))
1406 avic_vcpu_put(vcpu);
1407
1408 svm_prepare_host_switch(vcpu);
1409
1410 ++vcpu->stat.host_state_reload;
1411 }
1412
svm_get_rflags(struct kvm_vcpu * vcpu)1413 static unsigned long svm_get_rflags(struct kvm_vcpu *vcpu)
1414 {
1415 struct vcpu_svm *svm = to_svm(vcpu);
1416 unsigned long rflags = svm->vmcb->save.rflags;
1417
1418 if (svm->nmi_singlestep) {
1419 /* Hide our flags if they were not set by the guest */
1420 if (!(svm->nmi_singlestep_guest_rflags & X86_EFLAGS_TF))
1421 rflags &= ~X86_EFLAGS_TF;
1422 if (!(svm->nmi_singlestep_guest_rflags & X86_EFLAGS_RF))
1423 rflags &= ~X86_EFLAGS_RF;
1424 }
1425 return rflags;
1426 }
1427
svm_set_rflags(struct kvm_vcpu * vcpu,unsigned long rflags)1428 static void svm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
1429 {
1430 if (to_svm(vcpu)->nmi_singlestep)
1431 rflags |= (X86_EFLAGS_TF | X86_EFLAGS_RF);
1432
1433 /*
1434 * Any change of EFLAGS.VM is accompanied by a reload of SS
1435 * (caused by either a task switch or an inter-privilege IRET),
1436 * so we do not need to update the CPL here.
1437 */
1438 to_svm(vcpu)->vmcb->save.rflags = rflags;
1439 }
1440
svm_get_if_flag(struct kvm_vcpu * vcpu)1441 static bool svm_get_if_flag(struct kvm_vcpu *vcpu)
1442 {
1443 struct vmcb *vmcb = to_svm(vcpu)->vmcb;
1444
1445 return sev_es_guest(vcpu->kvm)
1446 ? vmcb->control.int_state & SVM_GUEST_INTERRUPT_MASK
1447 : kvm_get_rflags(vcpu) & X86_EFLAGS_IF;
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 kvm_register_mark_available(vcpu, reg);
1453
1454 switch (reg) {
1455 case VCPU_EXREG_PDPTR:
1456 /*
1457 * When !npt_enabled, mmu->pdptrs[] is already available since
1458 * it is always updated per SDM when moving to CRs.
1459 */
1460 if (npt_enabled)
1461 load_pdptrs(vcpu, kvm_read_cr3(vcpu));
1462 break;
1463 default:
1464 KVM_BUG_ON(1, vcpu->kvm);
1465 }
1466 }
1467
svm_set_vintr(struct vcpu_svm * svm)1468 static void svm_set_vintr(struct vcpu_svm *svm)
1469 {
1470 struct vmcb_control_area *control;
1471
1472 /*
1473 * The following fields are ignored when AVIC is enabled
1474 */
1475 WARN_ON(kvm_vcpu_apicv_activated(&svm->vcpu));
1476
1477 svm_set_intercept(svm, INTERCEPT_VINTR);
1478
1479 /*
1480 * This is just a dummy VINTR to actually cause a vmexit to happen.
1481 * Actual injection of virtual interrupts happens through EVENTINJ.
1482 */
1483 control = &svm->vmcb->control;
1484 control->int_vector = 0x0;
1485 control->int_ctl &= ~V_INTR_PRIO_MASK;
1486 control->int_ctl |= V_IRQ_MASK |
1487 ((/*control->int_vector >> 4*/ 0xf) << V_INTR_PRIO_SHIFT);
1488 vmcb_mark_dirty(svm->vmcb, VMCB_INTR);
1489 }
1490
svm_clear_vintr(struct vcpu_svm * svm)1491 static void svm_clear_vintr(struct vcpu_svm *svm)
1492 {
1493 svm_clr_intercept(svm, INTERCEPT_VINTR);
1494
1495 /* Drop int_ctl fields related to VINTR injection. */
1496 svm->vmcb->control.int_ctl &= ~V_IRQ_INJECTION_BITS_MASK;
1497 if (is_guest_mode(&svm->vcpu)) {
1498 svm->vmcb01.ptr->control.int_ctl &= ~V_IRQ_INJECTION_BITS_MASK;
1499
1500 WARN_ON((svm->vmcb->control.int_ctl & V_TPR_MASK) !=
1501 (svm->nested.ctl.int_ctl & V_TPR_MASK));
1502
1503 svm->vmcb->control.int_ctl |= svm->nested.ctl.int_ctl &
1504 V_IRQ_INJECTION_BITS_MASK;
1505
1506 svm->vmcb->control.int_vector = svm->nested.ctl.int_vector;
1507 }
1508
1509 vmcb_mark_dirty(svm->vmcb, VMCB_INTR);
1510 }
1511
svm_seg(struct kvm_vcpu * vcpu,int seg)1512 static struct vmcb_seg *svm_seg(struct kvm_vcpu *vcpu, int seg)
1513 {
1514 struct vmcb_save_area *save = &to_svm(vcpu)->vmcb->save;
1515 struct vmcb_save_area *save01 = &to_svm(vcpu)->vmcb01.ptr->save;
1516
1517 switch (seg) {
1518 case VCPU_SREG_CS: return &save->cs;
1519 case VCPU_SREG_DS: return &save->ds;
1520 case VCPU_SREG_ES: return &save->es;
1521 case VCPU_SREG_FS: return &save01->fs;
1522 case VCPU_SREG_GS: return &save01->gs;
1523 case VCPU_SREG_SS: return &save->ss;
1524 case VCPU_SREG_TR: return &save01->tr;
1525 case VCPU_SREG_LDTR: return &save01->ldtr;
1526 }
1527 BUG();
1528 return NULL;
1529 }
1530
svm_get_segment_base(struct kvm_vcpu * vcpu,int seg)1531 static u64 svm_get_segment_base(struct kvm_vcpu *vcpu, int seg)
1532 {
1533 struct vmcb_seg *s = svm_seg(vcpu, seg);
1534
1535 return s->base;
1536 }
1537
svm_get_segment(struct kvm_vcpu * vcpu,struct kvm_segment * var,int seg)1538 static void svm_get_segment(struct kvm_vcpu *vcpu,
1539 struct kvm_segment *var, int seg)
1540 {
1541 struct vmcb_seg *s = svm_seg(vcpu, seg);
1542
1543 var->base = s->base;
1544 var->limit = s->limit;
1545 var->selector = s->selector;
1546 var->type = s->attrib & SVM_SELECTOR_TYPE_MASK;
1547 var->s = (s->attrib >> SVM_SELECTOR_S_SHIFT) & 1;
1548 var->dpl = (s->attrib >> SVM_SELECTOR_DPL_SHIFT) & 3;
1549 var->present = (s->attrib >> SVM_SELECTOR_P_SHIFT) & 1;
1550 var->avl = (s->attrib >> SVM_SELECTOR_AVL_SHIFT) & 1;
1551 var->l = (s->attrib >> SVM_SELECTOR_L_SHIFT) & 1;
1552 var->db = (s->attrib >> SVM_SELECTOR_DB_SHIFT) & 1;
1553
1554 /*
1555 * AMD CPUs circa 2014 track the G bit for all segments except CS.
1556 * However, the SVM spec states that the G bit is not observed by the
1557 * CPU, and some VMware virtual CPUs drop the G bit for all segments.
1558 * So let's synthesize a legal G bit for all segments, this helps
1559 * running KVM nested. It also helps cross-vendor migration, because
1560 * Intel's vmentry has a check on the 'G' bit.
1561 */
1562 var->g = s->limit > 0xfffff;
1563
1564 /*
1565 * AMD's VMCB does not have an explicit unusable field, so emulate it
1566 * for cross vendor migration purposes by "not present"
1567 */
1568 var->unusable = !var->present;
1569
1570 switch (seg) {
1571 case VCPU_SREG_TR:
1572 /*
1573 * Work around a bug where the busy flag in the tr selector
1574 * isn't exposed
1575 */
1576 var->type |= 0x2;
1577 break;
1578 case VCPU_SREG_DS:
1579 case VCPU_SREG_ES:
1580 case VCPU_SREG_FS:
1581 case VCPU_SREG_GS:
1582 /*
1583 * The accessed bit must always be set in the segment
1584 * descriptor cache, although it can be cleared in the
1585 * descriptor, the cached bit always remains at 1. Since
1586 * Intel has a check on this, set it here to support
1587 * cross-vendor migration.
1588 */
1589 if (!var->unusable)
1590 var->type |= 0x1;
1591 break;
1592 case VCPU_SREG_SS:
1593 /*
1594 * On AMD CPUs sometimes the DB bit in the segment
1595 * descriptor is left as 1, although the whole segment has
1596 * been made unusable. Clear it here to pass an Intel VMX
1597 * entry check when cross vendor migrating.
1598 */
1599 if (var->unusable)
1600 var->db = 0;
1601 /* This is symmetric with svm_set_segment() */
1602 var->dpl = to_svm(vcpu)->vmcb->save.cpl;
1603 break;
1604 }
1605 }
1606
svm_get_cpl(struct kvm_vcpu * vcpu)1607 static int svm_get_cpl(struct kvm_vcpu *vcpu)
1608 {
1609 struct vmcb_save_area *save = &to_svm(vcpu)->vmcb->save;
1610
1611 return save->cpl;
1612 }
1613
svm_get_cs_db_l_bits(struct kvm_vcpu * vcpu,int * db,int * l)1614 static void svm_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
1615 {
1616 struct kvm_segment cs;
1617
1618 svm_get_segment(vcpu, &cs, VCPU_SREG_CS);
1619 *db = cs.db;
1620 *l = cs.l;
1621 }
1622
svm_get_idt(struct kvm_vcpu * vcpu,struct desc_ptr * dt)1623 static void svm_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
1624 {
1625 struct vcpu_svm *svm = to_svm(vcpu);
1626
1627 dt->size = svm->vmcb->save.idtr.limit;
1628 dt->address = svm->vmcb->save.idtr.base;
1629 }
1630
svm_set_idt(struct kvm_vcpu * vcpu,struct desc_ptr * dt)1631 static void svm_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
1632 {
1633 struct vcpu_svm *svm = to_svm(vcpu);
1634
1635 svm->vmcb->save.idtr.limit = dt->size;
1636 svm->vmcb->save.idtr.base = dt->address ;
1637 vmcb_mark_dirty(svm->vmcb, VMCB_DT);
1638 }
1639
svm_get_gdt(struct kvm_vcpu * vcpu,struct desc_ptr * dt)1640 static void svm_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
1641 {
1642 struct vcpu_svm *svm = to_svm(vcpu);
1643
1644 dt->size = svm->vmcb->save.gdtr.limit;
1645 dt->address = svm->vmcb->save.gdtr.base;
1646 }
1647
svm_set_gdt(struct kvm_vcpu * vcpu,struct desc_ptr * dt)1648 static void svm_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
1649 {
1650 struct vcpu_svm *svm = to_svm(vcpu);
1651
1652 svm->vmcb->save.gdtr.limit = dt->size;
1653 svm->vmcb->save.gdtr.base = dt->address ;
1654 vmcb_mark_dirty(svm->vmcb, VMCB_DT);
1655 }
1656
sev_post_set_cr3(struct kvm_vcpu * vcpu,unsigned long cr3)1657 static void sev_post_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
1658 {
1659 struct vcpu_svm *svm = to_svm(vcpu);
1660
1661 /*
1662 * For guests that don't set guest_state_protected, the cr3 update is
1663 * handled via kvm_mmu_load() while entering the guest. For guests
1664 * that do (SEV-ES/SEV-SNP), the cr3 update needs to be written to
1665 * VMCB save area now, since the save area will become the initial
1666 * contents of the VMSA, and future VMCB save area updates won't be
1667 * seen.
1668 */
1669 if (sev_es_guest(vcpu->kvm)) {
1670 svm->vmcb->save.cr3 = cr3;
1671 vmcb_mark_dirty(svm->vmcb, VMCB_CR);
1672 }
1673 }
1674
svm_set_cr0(struct kvm_vcpu * vcpu,unsigned long cr0)1675 void svm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
1676 {
1677 struct vcpu_svm *svm = to_svm(vcpu);
1678 u64 hcr0 = cr0;
1679 bool old_paging = is_paging(vcpu);
1680
1681 #ifdef CONFIG_X86_64
1682 if (vcpu->arch.efer & EFER_LME && !vcpu->arch.guest_state_protected) {
1683 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) {
1684 vcpu->arch.efer |= EFER_LMA;
1685 svm->vmcb->save.efer |= EFER_LMA | EFER_LME;
1686 }
1687
1688 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG)) {
1689 vcpu->arch.efer &= ~EFER_LMA;
1690 svm->vmcb->save.efer &= ~(EFER_LMA | EFER_LME);
1691 }
1692 }
1693 #endif
1694 vcpu->arch.cr0 = cr0;
1695
1696 if (!npt_enabled) {
1697 hcr0 |= X86_CR0_PG | X86_CR0_WP;
1698 if (old_paging != is_paging(vcpu))
1699 svm_set_cr4(vcpu, kvm_read_cr4(vcpu));
1700 }
1701
1702 /*
1703 * re-enable caching here because the QEMU bios
1704 * does not do it - this results in some delay at
1705 * reboot
1706 */
1707 if (kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_CD_NW_CLEARED))
1708 hcr0 &= ~(X86_CR0_CD | X86_CR0_NW);
1709
1710 svm->vmcb->save.cr0 = hcr0;
1711 vmcb_mark_dirty(svm->vmcb, VMCB_CR);
1712
1713 /*
1714 * SEV-ES guests must always keep the CR intercepts cleared. CR
1715 * tracking is done using the CR write traps.
1716 */
1717 if (sev_es_guest(vcpu->kvm))
1718 return;
1719
1720 if (hcr0 == cr0) {
1721 /* Selective CR0 write remains on. */
1722 svm_clr_intercept(svm, INTERCEPT_CR0_READ);
1723 svm_clr_intercept(svm, INTERCEPT_CR0_WRITE);
1724 } else {
1725 svm_set_intercept(svm, INTERCEPT_CR0_READ);
1726 svm_set_intercept(svm, INTERCEPT_CR0_WRITE);
1727 }
1728 }
1729
svm_is_valid_cr4(struct kvm_vcpu * vcpu,unsigned long cr4)1730 static bool svm_is_valid_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
1731 {
1732 return true;
1733 }
1734
svm_set_cr4(struct kvm_vcpu * vcpu,unsigned long cr4)1735 void svm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
1736 {
1737 unsigned long host_cr4_mce = cr4_read_shadow() & X86_CR4_MCE;
1738 unsigned long old_cr4 = vcpu->arch.cr4;
1739
1740 if (npt_enabled && ((old_cr4 ^ cr4) & X86_CR4_PGE))
1741 svm_flush_tlb_current(vcpu);
1742
1743 vcpu->arch.cr4 = cr4;
1744 if (!npt_enabled) {
1745 cr4 |= X86_CR4_PAE;
1746
1747 if (!is_paging(vcpu))
1748 cr4 &= ~(X86_CR4_SMEP | X86_CR4_SMAP | X86_CR4_PKE);
1749 }
1750 cr4 |= host_cr4_mce;
1751 to_svm(vcpu)->vmcb->save.cr4 = cr4;
1752 vmcb_mark_dirty(to_svm(vcpu)->vmcb, VMCB_CR);
1753
1754 if ((cr4 ^ old_cr4) & (X86_CR4_OSXSAVE | X86_CR4_PKE))
1755 kvm_update_cpuid_runtime(vcpu);
1756 }
1757
svm_set_segment(struct kvm_vcpu * vcpu,struct kvm_segment * var,int seg)1758 static void svm_set_segment(struct kvm_vcpu *vcpu,
1759 struct kvm_segment *var, int seg)
1760 {
1761 struct vcpu_svm *svm = to_svm(vcpu);
1762 struct vmcb_seg *s = svm_seg(vcpu, seg);
1763
1764 s->base = var->base;
1765 s->limit = var->limit;
1766 s->selector = var->selector;
1767 s->attrib = (var->type & SVM_SELECTOR_TYPE_MASK);
1768 s->attrib |= (var->s & 1) << SVM_SELECTOR_S_SHIFT;
1769 s->attrib |= (var->dpl & 3) << SVM_SELECTOR_DPL_SHIFT;
1770 s->attrib |= ((var->present & 1) && !var->unusable) << SVM_SELECTOR_P_SHIFT;
1771 s->attrib |= (var->avl & 1) << SVM_SELECTOR_AVL_SHIFT;
1772 s->attrib |= (var->l & 1) << SVM_SELECTOR_L_SHIFT;
1773 s->attrib |= (var->db & 1) << SVM_SELECTOR_DB_SHIFT;
1774 s->attrib |= (var->g & 1) << SVM_SELECTOR_G_SHIFT;
1775
1776 /*
1777 * This is always accurate, except if SYSRET returned to a segment
1778 * with SS.DPL != 3. Intel does not have this quirk, and always
1779 * forces SS.DPL to 3 on sysret, so we ignore that case; fixing it
1780 * would entail passing the CPL to userspace and back.
1781 */
1782 if (seg == VCPU_SREG_SS)
1783 /* This is symmetric with svm_get_segment() */
1784 svm->vmcb->save.cpl = (var->dpl & 3);
1785
1786 vmcb_mark_dirty(svm->vmcb, VMCB_SEG);
1787 }
1788
svm_update_exception_bitmap(struct kvm_vcpu * vcpu)1789 static void svm_update_exception_bitmap(struct kvm_vcpu *vcpu)
1790 {
1791 struct vcpu_svm *svm = to_svm(vcpu);
1792
1793 clr_exception_intercept(svm, BP_VECTOR);
1794
1795 if (vcpu->guest_debug & KVM_GUESTDBG_ENABLE) {
1796 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
1797 set_exception_intercept(svm, BP_VECTOR);
1798 }
1799 }
1800
new_asid(struct vcpu_svm * svm,struct svm_cpu_data * sd)1801 static void new_asid(struct vcpu_svm *svm, struct svm_cpu_data *sd)
1802 {
1803 if (sd->next_asid > sd->max_asid) {
1804 ++sd->asid_generation;
1805 sd->next_asid = sd->min_asid;
1806 svm->vmcb->control.tlb_ctl = TLB_CONTROL_FLUSH_ALL_ASID;
1807 vmcb_mark_dirty(svm->vmcb, VMCB_ASID);
1808 }
1809
1810 svm->current_vmcb->asid_generation = sd->asid_generation;
1811 svm->asid = sd->next_asid++;
1812 }
1813
svm_set_dr6(struct vcpu_svm * svm,unsigned long value)1814 static void svm_set_dr6(struct vcpu_svm *svm, unsigned long value)
1815 {
1816 struct vmcb *vmcb = svm->vmcb;
1817
1818 if (svm->vcpu.arch.guest_state_protected)
1819 return;
1820
1821 if (unlikely(value != vmcb->save.dr6)) {
1822 vmcb->save.dr6 = value;
1823 vmcb_mark_dirty(vmcb, VMCB_DR);
1824 }
1825 }
1826
svm_sync_dirty_debug_regs(struct kvm_vcpu * vcpu)1827 static void svm_sync_dirty_debug_regs(struct kvm_vcpu *vcpu)
1828 {
1829 struct vcpu_svm *svm = to_svm(vcpu);
1830
1831 if (vcpu->arch.guest_state_protected)
1832 return;
1833
1834 get_debugreg(vcpu->arch.db[0], 0);
1835 get_debugreg(vcpu->arch.db[1], 1);
1836 get_debugreg(vcpu->arch.db[2], 2);
1837 get_debugreg(vcpu->arch.db[3], 3);
1838 /*
1839 * We cannot reset svm->vmcb->save.dr6 to DR6_ACTIVE_LOW here,
1840 * because db_interception might need it. We can do it before vmentry.
1841 */
1842 vcpu->arch.dr6 = svm->vmcb->save.dr6;
1843 vcpu->arch.dr7 = svm->vmcb->save.dr7;
1844 vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_WONT_EXIT;
1845 set_dr_intercepts(svm);
1846 }
1847
svm_set_dr7(struct kvm_vcpu * vcpu,unsigned long value)1848 static void svm_set_dr7(struct kvm_vcpu *vcpu, unsigned long value)
1849 {
1850 struct vcpu_svm *svm = to_svm(vcpu);
1851
1852 if (vcpu->arch.guest_state_protected)
1853 return;
1854
1855 svm->vmcb->save.dr7 = value;
1856 vmcb_mark_dirty(svm->vmcb, VMCB_DR);
1857 }
1858
pf_interception(struct kvm_vcpu * vcpu)1859 static int pf_interception(struct kvm_vcpu *vcpu)
1860 {
1861 struct vcpu_svm *svm = to_svm(vcpu);
1862
1863 u64 fault_address = svm->vmcb->control.exit_info_2;
1864 u64 error_code = svm->vmcb->control.exit_info_1;
1865
1866 return kvm_handle_page_fault(vcpu, error_code, fault_address,
1867 static_cpu_has(X86_FEATURE_DECODEASSISTS) ?
1868 svm->vmcb->control.insn_bytes : NULL,
1869 svm->vmcb->control.insn_len);
1870 }
1871
npf_interception(struct kvm_vcpu * vcpu)1872 static int npf_interception(struct kvm_vcpu *vcpu)
1873 {
1874 struct vcpu_svm *svm = to_svm(vcpu);
1875
1876 u64 fault_address = svm->vmcb->control.exit_info_2;
1877 u64 error_code = svm->vmcb->control.exit_info_1;
1878
1879 trace_kvm_page_fault(fault_address, error_code);
1880 return kvm_mmu_page_fault(vcpu, fault_address, error_code,
1881 static_cpu_has(X86_FEATURE_DECODEASSISTS) ?
1882 svm->vmcb->control.insn_bytes : NULL,
1883 svm->vmcb->control.insn_len);
1884 }
1885
db_interception(struct kvm_vcpu * vcpu)1886 static int db_interception(struct kvm_vcpu *vcpu)
1887 {
1888 struct kvm_run *kvm_run = vcpu->run;
1889 struct vcpu_svm *svm = to_svm(vcpu);
1890
1891 if (!(vcpu->guest_debug &
1892 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)) &&
1893 !svm->nmi_singlestep) {
1894 u32 payload = svm->vmcb->save.dr6 ^ DR6_ACTIVE_LOW;
1895 kvm_queue_exception_p(vcpu, DB_VECTOR, payload);
1896 return 1;
1897 }
1898
1899 if (svm->nmi_singlestep) {
1900 disable_nmi_singlestep(svm);
1901 /* Make sure we check for pending NMIs upon entry */
1902 kvm_make_request(KVM_REQ_EVENT, vcpu);
1903 }
1904
1905 if (vcpu->guest_debug &
1906 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)) {
1907 kvm_run->exit_reason = KVM_EXIT_DEBUG;
1908 kvm_run->debug.arch.dr6 = svm->vmcb->save.dr6;
1909 kvm_run->debug.arch.dr7 = svm->vmcb->save.dr7;
1910 kvm_run->debug.arch.pc =
1911 svm->vmcb->save.cs.base + svm->vmcb->save.rip;
1912 kvm_run->debug.arch.exception = DB_VECTOR;
1913 return 0;
1914 }
1915
1916 return 1;
1917 }
1918
bp_interception(struct kvm_vcpu * vcpu)1919 static int bp_interception(struct kvm_vcpu *vcpu)
1920 {
1921 struct vcpu_svm *svm = to_svm(vcpu);
1922 struct kvm_run *kvm_run = vcpu->run;
1923
1924 kvm_run->exit_reason = KVM_EXIT_DEBUG;
1925 kvm_run->debug.arch.pc = svm->vmcb->save.cs.base + svm->vmcb->save.rip;
1926 kvm_run->debug.arch.exception = BP_VECTOR;
1927 return 0;
1928 }
1929
ud_interception(struct kvm_vcpu * vcpu)1930 static int ud_interception(struct kvm_vcpu *vcpu)
1931 {
1932 return handle_ud(vcpu);
1933 }
1934
ac_interception(struct kvm_vcpu * vcpu)1935 static int ac_interception(struct kvm_vcpu *vcpu)
1936 {
1937 kvm_queue_exception_e(vcpu, AC_VECTOR, 0);
1938 return 1;
1939 }
1940
is_erratum_383(void)1941 static bool is_erratum_383(void)
1942 {
1943 int err, i;
1944 u64 value;
1945
1946 if (!erratum_383_found)
1947 return false;
1948
1949 value = native_read_msr_safe(MSR_IA32_MC0_STATUS, &err);
1950 if (err)
1951 return false;
1952
1953 /* Bit 62 may or may not be set for this mce */
1954 value &= ~(1ULL << 62);
1955
1956 if (value != 0xb600000000010015ULL)
1957 return false;
1958
1959 /* Clear MCi_STATUS registers */
1960 for (i = 0; i < 6; ++i)
1961 native_write_msr_safe(MSR_IA32_MCx_STATUS(i), 0, 0);
1962
1963 value = native_read_msr_safe(MSR_IA32_MCG_STATUS, &err);
1964 if (!err) {
1965 u32 low, high;
1966
1967 value &= ~(1ULL << 2);
1968 low = lower_32_bits(value);
1969 high = upper_32_bits(value);
1970
1971 native_write_msr_safe(MSR_IA32_MCG_STATUS, low, high);
1972 }
1973
1974 /* Flush tlb to evict multi-match entries */
1975 __flush_tlb_all();
1976
1977 return true;
1978 }
1979
svm_handle_mce(struct kvm_vcpu * vcpu)1980 static void svm_handle_mce(struct kvm_vcpu *vcpu)
1981 {
1982 if (is_erratum_383()) {
1983 /*
1984 * Erratum 383 triggered. Guest state is corrupt so kill the
1985 * guest.
1986 */
1987 pr_err("KVM: Guest triggered AMD Erratum 383\n");
1988
1989 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
1990
1991 return;
1992 }
1993
1994 /*
1995 * On an #MC intercept the MCE handler is not called automatically in
1996 * the host. So do it by hand here.
1997 */
1998 kvm_machine_check();
1999 }
2000
mc_interception(struct kvm_vcpu * vcpu)2001 static int mc_interception(struct kvm_vcpu *vcpu)
2002 {
2003 return 1;
2004 }
2005
shutdown_interception(struct kvm_vcpu * vcpu)2006 static int shutdown_interception(struct kvm_vcpu *vcpu)
2007 {
2008 struct kvm_run *kvm_run = vcpu->run;
2009 struct vcpu_svm *svm = to_svm(vcpu);
2010
2011 /*
2012 * The VM save area has already been encrypted so it
2013 * cannot be reinitialized - just terminate.
2014 */
2015 if (sev_es_guest(vcpu->kvm))
2016 return -EINVAL;
2017
2018 /*
2019 * VMCB is undefined after a SHUTDOWN intercept. INIT the vCPU to put
2020 * the VMCB in a known good state. Unfortuately, KVM doesn't have
2021 * KVM_MP_STATE_SHUTDOWN and can't add it without potentially breaking
2022 * userspace. At a platform view, INIT is acceptable behavior as
2023 * there exist bare metal platforms that automatically INIT the CPU
2024 * in response to shutdown.
2025 */
2026 clear_page(svm->vmcb);
2027 kvm_vcpu_reset(vcpu, true);
2028
2029 kvm_run->exit_reason = KVM_EXIT_SHUTDOWN;
2030 return 0;
2031 }
2032
io_interception(struct kvm_vcpu * vcpu)2033 static int io_interception(struct kvm_vcpu *vcpu)
2034 {
2035 struct vcpu_svm *svm = to_svm(vcpu);
2036 u32 io_info = svm->vmcb->control.exit_info_1; /* address size bug? */
2037 int size, in, string;
2038 unsigned port;
2039
2040 ++vcpu->stat.io_exits;
2041 string = (io_info & SVM_IOIO_STR_MASK) != 0;
2042 in = (io_info & SVM_IOIO_TYPE_MASK) != 0;
2043 port = io_info >> 16;
2044 size = (io_info & SVM_IOIO_SIZE_MASK) >> SVM_IOIO_SIZE_SHIFT;
2045
2046 if (string) {
2047 if (sev_es_guest(vcpu->kvm))
2048 return sev_es_string_io(svm, size, port, in);
2049 else
2050 return kvm_emulate_instruction(vcpu, 0);
2051 }
2052
2053 svm->next_rip = svm->vmcb->control.exit_info_2;
2054
2055 return kvm_fast_pio(vcpu, size, port, in);
2056 }
2057
nmi_interception(struct kvm_vcpu * vcpu)2058 static int nmi_interception(struct kvm_vcpu *vcpu)
2059 {
2060 return 1;
2061 }
2062
smi_interception(struct kvm_vcpu * vcpu)2063 static int smi_interception(struct kvm_vcpu *vcpu)
2064 {
2065 return 1;
2066 }
2067
intr_interception(struct kvm_vcpu * vcpu)2068 static int intr_interception(struct kvm_vcpu *vcpu)
2069 {
2070 ++vcpu->stat.irq_exits;
2071 return 1;
2072 }
2073
vmload_vmsave_interception(struct kvm_vcpu * vcpu,bool vmload)2074 static int vmload_vmsave_interception(struct kvm_vcpu *vcpu, bool vmload)
2075 {
2076 struct vcpu_svm *svm = to_svm(vcpu);
2077 struct vmcb *vmcb12;
2078 struct kvm_host_map map;
2079 int ret;
2080
2081 if (nested_svm_check_permissions(vcpu))
2082 return 1;
2083
2084 ret = kvm_vcpu_map(vcpu, gpa_to_gfn(svm->vmcb->save.rax), &map);
2085 if (ret) {
2086 if (ret == -EINVAL)
2087 kvm_inject_gp(vcpu, 0);
2088 return 1;
2089 }
2090
2091 vmcb12 = map.hva;
2092
2093 ret = kvm_skip_emulated_instruction(vcpu);
2094
2095 if (vmload) {
2096 svm_copy_vmloadsave_state(svm->vmcb, vmcb12);
2097 svm->sysenter_eip_hi = 0;
2098 svm->sysenter_esp_hi = 0;
2099 } else {
2100 svm_copy_vmloadsave_state(vmcb12, svm->vmcb);
2101 }
2102
2103 kvm_vcpu_unmap(vcpu, &map, true);
2104
2105 return ret;
2106 }
2107
vmload_interception(struct kvm_vcpu * vcpu)2108 static int vmload_interception(struct kvm_vcpu *vcpu)
2109 {
2110 return vmload_vmsave_interception(vcpu, true);
2111 }
2112
vmsave_interception(struct kvm_vcpu * vcpu)2113 static int vmsave_interception(struct kvm_vcpu *vcpu)
2114 {
2115 return vmload_vmsave_interception(vcpu, false);
2116 }
2117
vmrun_interception(struct kvm_vcpu * vcpu)2118 static int vmrun_interception(struct kvm_vcpu *vcpu)
2119 {
2120 if (nested_svm_check_permissions(vcpu))
2121 return 1;
2122
2123 return nested_svm_vmrun(vcpu);
2124 }
2125
2126 enum {
2127 NONE_SVM_INSTR,
2128 SVM_INSTR_VMRUN,
2129 SVM_INSTR_VMLOAD,
2130 SVM_INSTR_VMSAVE,
2131 };
2132
2133 /* Return NONE_SVM_INSTR if not SVM instrs, otherwise return decode result */
svm_instr_opcode(struct kvm_vcpu * vcpu)2134 static int svm_instr_opcode(struct kvm_vcpu *vcpu)
2135 {
2136 struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt;
2137
2138 if (ctxt->b != 0x1 || ctxt->opcode_len != 2)
2139 return NONE_SVM_INSTR;
2140
2141 switch (ctxt->modrm) {
2142 case 0xd8: /* VMRUN */
2143 return SVM_INSTR_VMRUN;
2144 case 0xda: /* VMLOAD */
2145 return SVM_INSTR_VMLOAD;
2146 case 0xdb: /* VMSAVE */
2147 return SVM_INSTR_VMSAVE;
2148 default:
2149 break;
2150 }
2151
2152 return NONE_SVM_INSTR;
2153 }
2154
emulate_svm_instr(struct kvm_vcpu * vcpu,int opcode)2155 static int emulate_svm_instr(struct kvm_vcpu *vcpu, int opcode)
2156 {
2157 const int guest_mode_exit_codes[] = {
2158 [SVM_INSTR_VMRUN] = SVM_EXIT_VMRUN,
2159 [SVM_INSTR_VMLOAD] = SVM_EXIT_VMLOAD,
2160 [SVM_INSTR_VMSAVE] = SVM_EXIT_VMSAVE,
2161 };
2162 int (*const svm_instr_handlers[])(struct kvm_vcpu *vcpu) = {
2163 [SVM_INSTR_VMRUN] = vmrun_interception,
2164 [SVM_INSTR_VMLOAD] = vmload_interception,
2165 [SVM_INSTR_VMSAVE] = vmsave_interception,
2166 };
2167 struct vcpu_svm *svm = to_svm(vcpu);
2168 int ret;
2169
2170 if (is_guest_mode(vcpu)) {
2171 /* Returns '1' or -errno on failure, '0' on success. */
2172 ret = nested_svm_simple_vmexit(svm, guest_mode_exit_codes[opcode]);
2173 if (ret)
2174 return ret;
2175 return 1;
2176 }
2177 return svm_instr_handlers[opcode](vcpu);
2178 }
2179
2180 /*
2181 * #GP handling code. Note that #GP can be triggered under the following two
2182 * cases:
2183 * 1) SVM VM-related instructions (VMRUN/VMSAVE/VMLOAD) that trigger #GP on
2184 * some AMD CPUs when EAX of these instructions are in the reserved memory
2185 * regions (e.g. SMM memory on host).
2186 * 2) VMware backdoor
2187 */
gp_interception(struct kvm_vcpu * vcpu)2188 static int gp_interception(struct kvm_vcpu *vcpu)
2189 {
2190 struct vcpu_svm *svm = to_svm(vcpu);
2191 u32 error_code = svm->vmcb->control.exit_info_1;
2192 int opcode;
2193
2194 /* Both #GP cases have zero error_code */
2195 if (error_code)
2196 goto reinject;
2197
2198 /* Decode the instruction for usage later */
2199 if (x86_decode_emulated_instruction(vcpu, 0, NULL, 0) != EMULATION_OK)
2200 goto reinject;
2201
2202 opcode = svm_instr_opcode(vcpu);
2203
2204 if (opcode == NONE_SVM_INSTR) {
2205 if (!enable_vmware_backdoor)
2206 goto reinject;
2207
2208 /*
2209 * VMware backdoor emulation on #GP interception only handles
2210 * IN{S}, OUT{S}, and RDPMC.
2211 */
2212 if (!is_guest_mode(vcpu))
2213 return kvm_emulate_instruction(vcpu,
2214 EMULTYPE_VMWARE_GP | EMULTYPE_NO_DECODE);
2215 } else {
2216 /* All SVM instructions expect page aligned RAX */
2217 if (svm->vmcb->save.rax & ~PAGE_MASK)
2218 goto reinject;
2219
2220 return emulate_svm_instr(vcpu, opcode);
2221 }
2222
2223 reinject:
2224 kvm_queue_exception_e(vcpu, GP_VECTOR, error_code);
2225 return 1;
2226 }
2227
svm_set_gif(struct vcpu_svm * svm,bool value)2228 void svm_set_gif(struct vcpu_svm *svm, bool value)
2229 {
2230 if (value) {
2231 /*
2232 * If VGIF is enabled, the STGI intercept is only added to
2233 * detect the opening of the SMI/NMI window; remove it now.
2234 * Likewise, clear the VINTR intercept, we will set it
2235 * again while processing KVM_REQ_EVENT if needed.
2236 */
2237 if (vgif)
2238 svm_clr_intercept(svm, INTERCEPT_STGI);
2239 if (svm_is_intercept(svm, INTERCEPT_VINTR))
2240 svm_clear_vintr(svm);
2241
2242 enable_gif(svm);
2243 if (svm->vcpu.arch.smi_pending ||
2244 svm->vcpu.arch.nmi_pending ||
2245 kvm_cpu_has_injectable_intr(&svm->vcpu))
2246 kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
2247 } else {
2248 disable_gif(svm);
2249
2250 /*
2251 * After a CLGI no interrupts should come. But if vGIF is
2252 * in use, we still rely on the VINTR intercept (rather than
2253 * STGI) to detect an open interrupt window.
2254 */
2255 if (!vgif)
2256 svm_clear_vintr(svm);
2257 }
2258 }
2259
stgi_interception(struct kvm_vcpu * vcpu)2260 static int stgi_interception(struct kvm_vcpu *vcpu)
2261 {
2262 int ret;
2263
2264 if (nested_svm_check_permissions(vcpu))
2265 return 1;
2266
2267 ret = kvm_skip_emulated_instruction(vcpu);
2268 svm_set_gif(to_svm(vcpu), true);
2269 return ret;
2270 }
2271
clgi_interception(struct kvm_vcpu * vcpu)2272 static int clgi_interception(struct kvm_vcpu *vcpu)
2273 {
2274 int ret;
2275
2276 if (nested_svm_check_permissions(vcpu))
2277 return 1;
2278
2279 ret = kvm_skip_emulated_instruction(vcpu);
2280 svm_set_gif(to_svm(vcpu), false);
2281 return ret;
2282 }
2283
invlpga_interception(struct kvm_vcpu * vcpu)2284 static int invlpga_interception(struct kvm_vcpu *vcpu)
2285 {
2286 gva_t gva = kvm_rax_read(vcpu);
2287 u32 asid = kvm_rcx_read(vcpu);
2288
2289 /* FIXME: Handle an address size prefix. */
2290 if (!is_long_mode(vcpu))
2291 gva = (u32)gva;
2292
2293 trace_kvm_invlpga(to_svm(vcpu)->vmcb->save.rip, asid, gva);
2294
2295 /* Let's treat INVLPGA the same as INVLPG (can be optimized!) */
2296 kvm_mmu_invlpg(vcpu, gva);
2297
2298 return kvm_skip_emulated_instruction(vcpu);
2299 }
2300
skinit_interception(struct kvm_vcpu * vcpu)2301 static int skinit_interception(struct kvm_vcpu *vcpu)
2302 {
2303 trace_kvm_skinit(to_svm(vcpu)->vmcb->save.rip, kvm_rax_read(vcpu));
2304
2305 kvm_queue_exception(vcpu, UD_VECTOR);
2306 return 1;
2307 }
2308
task_switch_interception(struct kvm_vcpu * vcpu)2309 static int task_switch_interception(struct kvm_vcpu *vcpu)
2310 {
2311 struct vcpu_svm *svm = to_svm(vcpu);
2312 u16 tss_selector;
2313 int reason;
2314 int int_type = svm->vmcb->control.exit_int_info &
2315 SVM_EXITINTINFO_TYPE_MASK;
2316 int int_vec = svm->vmcb->control.exit_int_info & SVM_EVTINJ_VEC_MASK;
2317 uint32_t type =
2318 svm->vmcb->control.exit_int_info & SVM_EXITINTINFO_TYPE_MASK;
2319 uint32_t idt_v =
2320 svm->vmcb->control.exit_int_info & SVM_EXITINTINFO_VALID;
2321 bool has_error_code = false;
2322 u32 error_code = 0;
2323
2324 tss_selector = (u16)svm->vmcb->control.exit_info_1;
2325
2326 if (svm->vmcb->control.exit_info_2 &
2327 (1ULL << SVM_EXITINFOSHIFT_TS_REASON_IRET))
2328 reason = TASK_SWITCH_IRET;
2329 else if (svm->vmcb->control.exit_info_2 &
2330 (1ULL << SVM_EXITINFOSHIFT_TS_REASON_JMP))
2331 reason = TASK_SWITCH_JMP;
2332 else if (idt_v)
2333 reason = TASK_SWITCH_GATE;
2334 else
2335 reason = TASK_SWITCH_CALL;
2336
2337 if (reason == TASK_SWITCH_GATE) {
2338 switch (type) {
2339 case SVM_EXITINTINFO_TYPE_NMI:
2340 vcpu->arch.nmi_injected = false;
2341 break;
2342 case SVM_EXITINTINFO_TYPE_EXEPT:
2343 if (svm->vmcb->control.exit_info_2 &
2344 (1ULL << SVM_EXITINFOSHIFT_TS_HAS_ERROR_CODE)) {
2345 has_error_code = true;
2346 error_code =
2347 (u32)svm->vmcb->control.exit_info_2;
2348 }
2349 kvm_clear_exception_queue(vcpu);
2350 break;
2351 case SVM_EXITINTINFO_TYPE_INTR:
2352 kvm_clear_interrupt_queue(vcpu);
2353 break;
2354 default:
2355 break;
2356 }
2357 }
2358
2359 if (reason != TASK_SWITCH_GATE ||
2360 int_type == SVM_EXITINTINFO_TYPE_SOFT ||
2361 (int_type == SVM_EXITINTINFO_TYPE_EXEPT &&
2362 (int_vec == OF_VECTOR || int_vec == BP_VECTOR))) {
2363 if (!svm_skip_emulated_instruction(vcpu))
2364 return 0;
2365 }
2366
2367 if (int_type != SVM_EXITINTINFO_TYPE_SOFT)
2368 int_vec = -1;
2369
2370 return kvm_task_switch(vcpu, tss_selector, int_vec, reason,
2371 has_error_code, error_code);
2372 }
2373
iret_interception(struct kvm_vcpu * vcpu)2374 static int iret_interception(struct kvm_vcpu *vcpu)
2375 {
2376 struct vcpu_svm *svm = to_svm(vcpu);
2377
2378 ++vcpu->stat.nmi_window_exits;
2379 vcpu->arch.hflags |= HF_IRET_MASK;
2380 if (!sev_es_guest(vcpu->kvm)) {
2381 svm_clr_intercept(svm, INTERCEPT_IRET);
2382 svm->nmi_iret_rip = kvm_rip_read(vcpu);
2383 }
2384 kvm_make_request(KVM_REQ_EVENT, vcpu);
2385 return 1;
2386 }
2387
invlpg_interception(struct kvm_vcpu * vcpu)2388 static int invlpg_interception(struct kvm_vcpu *vcpu)
2389 {
2390 if (!static_cpu_has(X86_FEATURE_DECODEASSISTS))
2391 return kvm_emulate_instruction(vcpu, 0);
2392
2393 kvm_mmu_invlpg(vcpu, to_svm(vcpu)->vmcb->control.exit_info_1);
2394 return kvm_skip_emulated_instruction(vcpu);
2395 }
2396
emulate_on_interception(struct kvm_vcpu * vcpu)2397 static int emulate_on_interception(struct kvm_vcpu *vcpu)
2398 {
2399 return kvm_emulate_instruction(vcpu, 0);
2400 }
2401
rsm_interception(struct kvm_vcpu * vcpu)2402 static int rsm_interception(struct kvm_vcpu *vcpu)
2403 {
2404 return kvm_emulate_instruction_from_buffer(vcpu, rsm_ins_bytes, 2);
2405 }
2406
check_selective_cr0_intercepted(struct kvm_vcpu * vcpu,unsigned long val)2407 static bool check_selective_cr0_intercepted(struct kvm_vcpu *vcpu,
2408 unsigned long val)
2409 {
2410 struct vcpu_svm *svm = to_svm(vcpu);
2411 unsigned long cr0 = vcpu->arch.cr0;
2412 bool ret = false;
2413
2414 if (!is_guest_mode(vcpu) ||
2415 (!(vmcb12_is_intercept(&svm->nested.ctl, INTERCEPT_SELECTIVE_CR0))))
2416 return false;
2417
2418 cr0 &= ~SVM_CR0_SELECTIVE_MASK;
2419 val &= ~SVM_CR0_SELECTIVE_MASK;
2420
2421 if (cr0 ^ val) {
2422 svm->vmcb->control.exit_code = SVM_EXIT_CR0_SEL_WRITE;
2423 ret = (nested_svm_exit_handled(svm) == NESTED_EXIT_DONE);
2424 }
2425
2426 return ret;
2427 }
2428
2429 #define CR_VALID (1ULL << 63)
2430
cr_interception(struct kvm_vcpu * vcpu)2431 static int cr_interception(struct kvm_vcpu *vcpu)
2432 {
2433 struct vcpu_svm *svm = to_svm(vcpu);
2434 int reg, cr;
2435 unsigned long val;
2436 int err;
2437
2438 if (!static_cpu_has(X86_FEATURE_DECODEASSISTS))
2439 return emulate_on_interception(vcpu);
2440
2441 if (unlikely((svm->vmcb->control.exit_info_1 & CR_VALID) == 0))
2442 return emulate_on_interception(vcpu);
2443
2444 reg = svm->vmcb->control.exit_info_1 & SVM_EXITINFO_REG_MASK;
2445 if (svm->vmcb->control.exit_code == SVM_EXIT_CR0_SEL_WRITE)
2446 cr = SVM_EXIT_WRITE_CR0 - SVM_EXIT_READ_CR0;
2447 else
2448 cr = svm->vmcb->control.exit_code - SVM_EXIT_READ_CR0;
2449
2450 err = 0;
2451 if (cr >= 16) { /* mov to cr */
2452 cr -= 16;
2453 val = kvm_register_read(vcpu, reg);
2454 trace_kvm_cr_write(cr, val);
2455 switch (cr) {
2456 case 0:
2457 if (!check_selective_cr0_intercepted(vcpu, val))
2458 err = kvm_set_cr0(vcpu, val);
2459 else
2460 return 1;
2461
2462 break;
2463 case 3:
2464 err = kvm_set_cr3(vcpu, val);
2465 break;
2466 case 4:
2467 err = kvm_set_cr4(vcpu, val);
2468 break;
2469 case 8:
2470 err = kvm_set_cr8(vcpu, val);
2471 break;
2472 default:
2473 WARN(1, "unhandled write to CR%d", cr);
2474 kvm_queue_exception(vcpu, UD_VECTOR);
2475 return 1;
2476 }
2477 } else { /* mov from cr */
2478 switch (cr) {
2479 case 0:
2480 val = kvm_read_cr0(vcpu);
2481 break;
2482 case 2:
2483 val = vcpu->arch.cr2;
2484 break;
2485 case 3:
2486 val = kvm_read_cr3(vcpu);
2487 break;
2488 case 4:
2489 val = kvm_read_cr4(vcpu);
2490 break;
2491 case 8:
2492 val = kvm_get_cr8(vcpu);
2493 break;
2494 default:
2495 WARN(1, "unhandled read from CR%d", cr);
2496 kvm_queue_exception(vcpu, UD_VECTOR);
2497 return 1;
2498 }
2499 kvm_register_write(vcpu, reg, val);
2500 trace_kvm_cr_read(cr, val);
2501 }
2502 return kvm_complete_insn_gp(vcpu, err);
2503 }
2504
cr_trap(struct kvm_vcpu * vcpu)2505 static int cr_trap(struct kvm_vcpu *vcpu)
2506 {
2507 struct vcpu_svm *svm = to_svm(vcpu);
2508 unsigned long old_value, new_value;
2509 unsigned int cr;
2510 int ret = 0;
2511
2512 new_value = (unsigned long)svm->vmcb->control.exit_info_1;
2513
2514 cr = svm->vmcb->control.exit_code - SVM_EXIT_CR0_WRITE_TRAP;
2515 switch (cr) {
2516 case 0:
2517 old_value = kvm_read_cr0(vcpu);
2518 svm_set_cr0(vcpu, new_value);
2519
2520 kvm_post_set_cr0(vcpu, old_value, new_value);
2521 break;
2522 case 4:
2523 old_value = kvm_read_cr4(vcpu);
2524 svm_set_cr4(vcpu, new_value);
2525
2526 kvm_post_set_cr4(vcpu, old_value, new_value);
2527 break;
2528 case 8:
2529 ret = kvm_set_cr8(vcpu, new_value);
2530 break;
2531 default:
2532 WARN(1, "unhandled CR%d write trap", cr);
2533 kvm_queue_exception(vcpu, UD_VECTOR);
2534 return 1;
2535 }
2536
2537 return kvm_complete_insn_gp(vcpu, ret);
2538 }
2539
dr_interception(struct kvm_vcpu * vcpu)2540 static int dr_interception(struct kvm_vcpu *vcpu)
2541 {
2542 struct vcpu_svm *svm = to_svm(vcpu);
2543 int reg, dr;
2544 unsigned long val;
2545 int err = 0;
2546
2547 if (vcpu->guest_debug == 0) {
2548 /*
2549 * No more DR vmexits; force a reload of the debug registers
2550 * and reenter on this instruction. The next vmexit will
2551 * retrieve the full state of the debug registers.
2552 */
2553 clr_dr_intercepts(svm);
2554 vcpu->arch.switch_db_regs |= KVM_DEBUGREG_WONT_EXIT;
2555 return 1;
2556 }
2557
2558 if (!boot_cpu_has(X86_FEATURE_DECODEASSISTS))
2559 return emulate_on_interception(vcpu);
2560
2561 reg = svm->vmcb->control.exit_info_1 & SVM_EXITINFO_REG_MASK;
2562 dr = svm->vmcb->control.exit_code - SVM_EXIT_READ_DR0;
2563 if (dr >= 16) { /* mov to DRn */
2564 dr -= 16;
2565 val = kvm_register_read(vcpu, reg);
2566 err = kvm_set_dr(vcpu, dr, val);
2567 } else {
2568 kvm_get_dr(vcpu, dr, &val);
2569 kvm_register_write(vcpu, reg, val);
2570 }
2571
2572 return kvm_complete_insn_gp(vcpu, err);
2573 }
2574
cr8_write_interception(struct kvm_vcpu * vcpu)2575 static int cr8_write_interception(struct kvm_vcpu *vcpu)
2576 {
2577 int r;
2578
2579 u8 cr8_prev = kvm_get_cr8(vcpu);
2580 /* instruction emulation calls kvm_set_cr8() */
2581 r = cr_interception(vcpu);
2582 if (lapic_in_kernel(vcpu))
2583 return r;
2584 if (cr8_prev <= kvm_get_cr8(vcpu))
2585 return r;
2586 vcpu->run->exit_reason = KVM_EXIT_SET_TPR;
2587 return 0;
2588 }
2589
efer_trap(struct kvm_vcpu * vcpu)2590 static int efer_trap(struct kvm_vcpu *vcpu)
2591 {
2592 struct msr_data msr_info;
2593 int ret;
2594
2595 /*
2596 * Clear the EFER_SVME bit from EFER. The SVM code always sets this
2597 * bit in svm_set_efer(), but __kvm_valid_efer() checks it against
2598 * whether the guest has X86_FEATURE_SVM - this avoids a failure if
2599 * the guest doesn't have X86_FEATURE_SVM.
2600 */
2601 msr_info.host_initiated = false;
2602 msr_info.index = MSR_EFER;
2603 msr_info.data = to_svm(vcpu)->vmcb->control.exit_info_1 & ~EFER_SVME;
2604 ret = kvm_set_msr_common(vcpu, &msr_info);
2605
2606 return kvm_complete_insn_gp(vcpu, ret);
2607 }
2608
svm_get_msr_feature(struct kvm_msr_entry * msr)2609 static int svm_get_msr_feature(struct kvm_msr_entry *msr)
2610 {
2611 msr->data = 0;
2612
2613 switch (msr->index) {
2614 case MSR_F10H_DECFG:
2615 if (boot_cpu_has(X86_FEATURE_LFENCE_RDTSC))
2616 msr->data |= MSR_F10H_DECFG_LFENCE_SERIALIZE;
2617 break;
2618 case MSR_IA32_PERF_CAPABILITIES:
2619 return 0;
2620 default:
2621 return KVM_MSR_RET_INVALID;
2622 }
2623
2624 return 0;
2625 }
2626
svm_get_msr(struct kvm_vcpu * vcpu,struct msr_data * msr_info)2627 static int svm_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
2628 {
2629 struct vcpu_svm *svm = to_svm(vcpu);
2630
2631 switch (msr_info->index) {
2632 case MSR_AMD64_TSC_RATIO:
2633 if (!msr_info->host_initiated && !svm->tsc_scaling_enabled)
2634 return 1;
2635 msr_info->data = svm->tsc_ratio_msr;
2636 break;
2637 case MSR_STAR:
2638 msr_info->data = svm->vmcb01.ptr->save.star;
2639 break;
2640 #ifdef CONFIG_X86_64
2641 case MSR_LSTAR:
2642 msr_info->data = svm->vmcb01.ptr->save.lstar;
2643 break;
2644 case MSR_CSTAR:
2645 msr_info->data = svm->vmcb01.ptr->save.cstar;
2646 break;
2647 case MSR_KERNEL_GS_BASE:
2648 msr_info->data = svm->vmcb01.ptr->save.kernel_gs_base;
2649 break;
2650 case MSR_SYSCALL_MASK:
2651 msr_info->data = svm->vmcb01.ptr->save.sfmask;
2652 break;
2653 #endif
2654 case MSR_IA32_SYSENTER_CS:
2655 msr_info->data = svm->vmcb01.ptr->save.sysenter_cs;
2656 break;
2657 case MSR_IA32_SYSENTER_EIP:
2658 msr_info->data = (u32)svm->vmcb01.ptr->save.sysenter_eip;
2659 if (guest_cpuid_is_intel(vcpu))
2660 msr_info->data |= (u64)svm->sysenter_eip_hi << 32;
2661 break;
2662 case MSR_IA32_SYSENTER_ESP:
2663 msr_info->data = svm->vmcb01.ptr->save.sysenter_esp;
2664 if (guest_cpuid_is_intel(vcpu))
2665 msr_info->data |= (u64)svm->sysenter_esp_hi << 32;
2666 break;
2667 case MSR_TSC_AUX:
2668 msr_info->data = svm->tsc_aux;
2669 break;
2670 case MSR_IA32_DEBUGCTLMSR:
2671 case MSR_IA32_LASTBRANCHFROMIP:
2672 case MSR_IA32_LASTBRANCHTOIP:
2673 case MSR_IA32_LASTINTFROMIP:
2674 case MSR_IA32_LASTINTTOIP:
2675 msr_info->data = svm_get_lbr_msr(svm, msr_info->index);
2676 break;
2677 case MSR_VM_HSAVE_PA:
2678 msr_info->data = svm->nested.hsave_msr;
2679 break;
2680 case MSR_VM_CR:
2681 msr_info->data = svm->nested.vm_cr_msr;
2682 break;
2683 case MSR_IA32_SPEC_CTRL:
2684 if (!msr_info->host_initiated &&
2685 !guest_has_spec_ctrl_msr(vcpu))
2686 return 1;
2687
2688 if (boot_cpu_has(X86_FEATURE_V_SPEC_CTRL))
2689 msr_info->data = svm->vmcb->save.spec_ctrl;
2690 else
2691 msr_info->data = svm->spec_ctrl;
2692 break;
2693 case MSR_AMD64_VIRT_SPEC_CTRL:
2694 if (!msr_info->host_initiated &&
2695 !guest_cpuid_has(vcpu, X86_FEATURE_VIRT_SSBD))
2696 return 1;
2697
2698 msr_info->data = svm->virt_spec_ctrl;
2699 break;
2700 case MSR_F15H_IC_CFG: {
2701
2702 int family, model;
2703
2704 family = guest_cpuid_family(vcpu);
2705 model = guest_cpuid_model(vcpu);
2706
2707 if (family < 0 || model < 0)
2708 return kvm_get_msr_common(vcpu, msr_info);
2709
2710 msr_info->data = 0;
2711
2712 if (family == 0x15 &&
2713 (model >= 0x2 && model < 0x20))
2714 msr_info->data = 0x1E;
2715 }
2716 break;
2717 case MSR_F10H_DECFG:
2718 msr_info->data = svm->msr_decfg;
2719 break;
2720 default:
2721 return kvm_get_msr_common(vcpu, msr_info);
2722 }
2723 return 0;
2724 }
2725
svm_complete_emulated_msr(struct kvm_vcpu * vcpu,int err)2726 static int svm_complete_emulated_msr(struct kvm_vcpu *vcpu, int err)
2727 {
2728 struct vcpu_svm *svm = to_svm(vcpu);
2729 if (!err || !sev_es_guest(vcpu->kvm) || WARN_ON_ONCE(!svm->sev_es.ghcb))
2730 return kvm_complete_insn_gp(vcpu, err);
2731
2732 ghcb_set_sw_exit_info_1(svm->sev_es.ghcb, 1);
2733 ghcb_set_sw_exit_info_2(svm->sev_es.ghcb,
2734 X86_TRAP_GP |
2735 SVM_EVTINJ_TYPE_EXEPT |
2736 SVM_EVTINJ_VALID);
2737 return 1;
2738 }
2739
svm_set_vm_cr(struct kvm_vcpu * vcpu,u64 data)2740 static int svm_set_vm_cr(struct kvm_vcpu *vcpu, u64 data)
2741 {
2742 struct vcpu_svm *svm = to_svm(vcpu);
2743 int svm_dis, chg_mask;
2744
2745 if (data & ~SVM_VM_CR_VALID_MASK)
2746 return 1;
2747
2748 chg_mask = SVM_VM_CR_VALID_MASK;
2749
2750 if (svm->nested.vm_cr_msr & SVM_VM_CR_SVM_DIS_MASK)
2751 chg_mask &= ~(SVM_VM_CR_SVM_LOCK_MASK | SVM_VM_CR_SVM_DIS_MASK);
2752
2753 svm->nested.vm_cr_msr &= ~chg_mask;
2754 svm->nested.vm_cr_msr |= (data & chg_mask);
2755
2756 svm_dis = svm->nested.vm_cr_msr & SVM_VM_CR_SVM_DIS_MASK;
2757
2758 /* check for svm_disable while efer.svme is set */
2759 if (svm_dis && (vcpu->arch.efer & EFER_SVME))
2760 return 1;
2761
2762 return 0;
2763 }
2764
svm_set_msr(struct kvm_vcpu * vcpu,struct msr_data * msr)2765 static int svm_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr)
2766 {
2767 struct vcpu_svm *svm = to_svm(vcpu);
2768 int r;
2769
2770 u32 ecx = msr->index;
2771 u64 data = msr->data;
2772 switch (ecx) {
2773 case MSR_AMD64_TSC_RATIO:
2774
2775 if (!svm->tsc_scaling_enabled) {
2776
2777 if (!msr->host_initiated)
2778 return 1;
2779 /*
2780 * In case TSC scaling is not enabled, always
2781 * leave this MSR at the default value.
2782 *
2783 * Due to bug in qemu 6.2.0, it would try to set
2784 * this msr to 0 if tsc scaling is not enabled.
2785 * Ignore this value as well.
2786 */
2787 if (data != 0 && data != svm->tsc_ratio_msr)
2788 return 1;
2789 break;
2790 }
2791
2792 if (data & SVM_TSC_RATIO_RSVD)
2793 return 1;
2794
2795 svm->tsc_ratio_msr = data;
2796
2797 if (svm->tsc_scaling_enabled && is_guest_mode(vcpu))
2798 nested_svm_update_tsc_ratio_msr(vcpu);
2799
2800 break;
2801 case MSR_IA32_CR_PAT:
2802 if (!kvm_mtrr_valid(vcpu, MSR_IA32_CR_PAT, data))
2803 return 1;
2804 vcpu->arch.pat = data;
2805 svm->vmcb01.ptr->save.g_pat = data;
2806 if (is_guest_mode(vcpu))
2807 nested_vmcb02_compute_g_pat(svm);
2808 vmcb_mark_dirty(svm->vmcb, VMCB_NPT);
2809 break;
2810 case MSR_IA32_SPEC_CTRL:
2811 if (!msr->host_initiated &&
2812 !guest_has_spec_ctrl_msr(vcpu))
2813 return 1;
2814
2815 if (kvm_spec_ctrl_test_value(data))
2816 return 1;
2817
2818 if (boot_cpu_has(X86_FEATURE_V_SPEC_CTRL))
2819 svm->vmcb->save.spec_ctrl = data;
2820 else
2821 svm->spec_ctrl = data;
2822 if (!data)
2823 break;
2824
2825 /*
2826 * For non-nested:
2827 * When it's written (to non-zero) for the first time, pass
2828 * it through.
2829 *
2830 * For nested:
2831 * The handling of the MSR bitmap for L2 guests is done in
2832 * nested_svm_vmrun_msrpm.
2833 * We update the L1 MSR bit as well since it will end up
2834 * touching the MSR anyway now.
2835 */
2836 set_msr_interception(vcpu, svm->msrpm, MSR_IA32_SPEC_CTRL, 1, 1);
2837 break;
2838 case MSR_IA32_PRED_CMD:
2839 if (!msr->host_initiated &&
2840 !guest_has_pred_cmd_msr(vcpu))
2841 return 1;
2842
2843 if (data & ~PRED_CMD_IBPB)
2844 return 1;
2845 if (!boot_cpu_has(X86_FEATURE_IBPB))
2846 return 1;
2847 if (!data)
2848 break;
2849
2850 wrmsrl(MSR_IA32_PRED_CMD, PRED_CMD_IBPB);
2851 set_msr_interception(vcpu, svm->msrpm, MSR_IA32_PRED_CMD, 0, 1);
2852 break;
2853 case MSR_AMD64_VIRT_SPEC_CTRL:
2854 if (!msr->host_initiated &&
2855 !guest_cpuid_has(vcpu, X86_FEATURE_VIRT_SSBD))
2856 return 1;
2857
2858 if (data & ~SPEC_CTRL_SSBD)
2859 return 1;
2860
2861 svm->virt_spec_ctrl = data;
2862 break;
2863 case MSR_STAR:
2864 svm->vmcb01.ptr->save.star = data;
2865 break;
2866 #ifdef CONFIG_X86_64
2867 case MSR_LSTAR:
2868 svm->vmcb01.ptr->save.lstar = data;
2869 break;
2870 case MSR_CSTAR:
2871 svm->vmcb01.ptr->save.cstar = data;
2872 break;
2873 case MSR_KERNEL_GS_BASE:
2874 svm->vmcb01.ptr->save.kernel_gs_base = data;
2875 break;
2876 case MSR_SYSCALL_MASK:
2877 svm->vmcb01.ptr->save.sfmask = data;
2878 break;
2879 #endif
2880 case MSR_IA32_SYSENTER_CS:
2881 svm->vmcb01.ptr->save.sysenter_cs = data;
2882 break;
2883 case MSR_IA32_SYSENTER_EIP:
2884 svm->vmcb01.ptr->save.sysenter_eip = (u32)data;
2885 /*
2886 * We only intercept the MSR_IA32_SYSENTER_{EIP|ESP} msrs
2887 * when we spoof an Intel vendor ID (for cross vendor migration).
2888 * In this case we use this intercept to track the high
2889 * 32 bit part of these msrs to support Intel's
2890 * implementation of SYSENTER/SYSEXIT.
2891 */
2892 svm->sysenter_eip_hi = guest_cpuid_is_intel(vcpu) ? (data >> 32) : 0;
2893 break;
2894 case MSR_IA32_SYSENTER_ESP:
2895 svm->vmcb01.ptr->save.sysenter_esp = (u32)data;
2896 svm->sysenter_esp_hi = guest_cpuid_is_intel(vcpu) ? (data >> 32) : 0;
2897 break;
2898 case MSR_TSC_AUX:
2899 /*
2900 * TSC_AUX is usually changed only during boot and never read
2901 * directly. Intercept TSC_AUX instead of exposing it to the
2902 * guest via direct_access_msrs, and switch it via user return.
2903 */
2904 preempt_disable();
2905 r = kvm_set_user_return_msr(tsc_aux_uret_slot, data, -1ull);
2906 preempt_enable();
2907 if (r)
2908 return 1;
2909
2910 svm->tsc_aux = data;
2911 break;
2912 case MSR_IA32_DEBUGCTLMSR:
2913 if (!lbrv) {
2914 vcpu_unimpl(vcpu, "%s: MSR_IA32_DEBUGCTL 0x%llx, nop\n",
2915 __func__, data);
2916 break;
2917 }
2918 if (data & DEBUGCTL_RESERVED_BITS)
2919 return 1;
2920
2921 if (svm->vmcb->control.virt_ext & LBR_CTL_ENABLE_MASK)
2922 svm->vmcb->save.dbgctl = data;
2923 else
2924 svm->vmcb01.ptr->save.dbgctl = data;
2925
2926 svm_update_lbrv(vcpu);
2927
2928 break;
2929 case MSR_VM_HSAVE_PA:
2930 /*
2931 * Old kernels did not validate the value written to
2932 * MSR_VM_HSAVE_PA. Allow KVM_SET_MSR to set an invalid
2933 * value to allow live migrating buggy or malicious guests
2934 * originating from those kernels.
2935 */
2936 if (!msr->host_initiated && !page_address_valid(vcpu, data))
2937 return 1;
2938
2939 svm->nested.hsave_msr = data & PAGE_MASK;
2940 break;
2941 case MSR_VM_CR:
2942 return svm_set_vm_cr(vcpu, data);
2943 case MSR_VM_IGNNE:
2944 vcpu_unimpl(vcpu, "unimplemented wrmsr: 0x%x data 0x%llx\n", ecx, data);
2945 break;
2946 case MSR_F10H_DECFG: {
2947 struct kvm_msr_entry msr_entry;
2948
2949 msr_entry.index = msr->index;
2950 if (svm_get_msr_feature(&msr_entry))
2951 return 1;
2952
2953 /* Check the supported bits */
2954 if (data & ~msr_entry.data)
2955 return 1;
2956
2957 /* Don't allow the guest to change a bit, #GP */
2958 if (!msr->host_initiated && (data ^ msr_entry.data))
2959 return 1;
2960
2961 svm->msr_decfg = data;
2962 break;
2963 }
2964 default:
2965 return kvm_set_msr_common(vcpu, msr);
2966 }
2967 return 0;
2968 }
2969
msr_interception(struct kvm_vcpu * vcpu)2970 static int msr_interception(struct kvm_vcpu *vcpu)
2971 {
2972 if (to_svm(vcpu)->vmcb->control.exit_info_1)
2973 return kvm_emulate_wrmsr(vcpu);
2974 else
2975 return kvm_emulate_rdmsr(vcpu);
2976 }
2977
interrupt_window_interception(struct kvm_vcpu * vcpu)2978 static int interrupt_window_interception(struct kvm_vcpu *vcpu)
2979 {
2980 kvm_make_request(KVM_REQ_EVENT, vcpu);
2981 svm_clear_vintr(to_svm(vcpu));
2982
2983 /*
2984 * If not running nested, for AVIC, the only reason to end up here is ExtINTs.
2985 * In this case AVIC was temporarily disabled for
2986 * requesting the IRQ window and we have to re-enable it.
2987 *
2988 * If running nested, still remove the VM wide AVIC inhibit to
2989 * support case in which the interrupt window was requested when the
2990 * vCPU was not running nested.
2991
2992 * All vCPUs which run still run nested, will remain to have their
2993 * AVIC still inhibited due to per-cpu AVIC inhibition.
2994 */
2995 kvm_clear_apicv_inhibit(vcpu->kvm, APICV_INHIBIT_REASON_IRQWIN);
2996
2997 ++vcpu->stat.irq_window_exits;
2998 return 1;
2999 }
3000
pause_interception(struct kvm_vcpu * vcpu)3001 static int pause_interception(struct kvm_vcpu *vcpu)
3002 {
3003 bool in_kernel;
3004 /*
3005 * CPL is not made available for an SEV-ES guest, therefore
3006 * vcpu->arch.preempted_in_kernel can never be true. Just
3007 * set in_kernel to false as well.
3008 */
3009 in_kernel = !sev_es_guest(vcpu->kvm) && svm_get_cpl(vcpu) == 0;
3010
3011 grow_ple_window(vcpu);
3012
3013 kvm_vcpu_on_spin(vcpu, in_kernel);
3014 return kvm_skip_emulated_instruction(vcpu);
3015 }
3016
invpcid_interception(struct kvm_vcpu * vcpu)3017 static int invpcid_interception(struct kvm_vcpu *vcpu)
3018 {
3019 struct vcpu_svm *svm = to_svm(vcpu);
3020 unsigned long type;
3021 gva_t gva;
3022
3023 if (!guest_cpuid_has(vcpu, X86_FEATURE_INVPCID)) {
3024 kvm_queue_exception(vcpu, UD_VECTOR);
3025 return 1;
3026 }
3027
3028 /*
3029 * For an INVPCID intercept:
3030 * EXITINFO1 provides the linear address of the memory operand.
3031 * EXITINFO2 provides the contents of the register operand.
3032 */
3033 type = svm->vmcb->control.exit_info_2;
3034 gva = svm->vmcb->control.exit_info_1;
3035
3036 return kvm_handle_invpcid(vcpu, type, gva);
3037 }
3038
3039 static int (*const svm_exit_handlers[])(struct kvm_vcpu *vcpu) = {
3040 [SVM_EXIT_READ_CR0] = cr_interception,
3041 [SVM_EXIT_READ_CR3] = cr_interception,
3042 [SVM_EXIT_READ_CR4] = cr_interception,
3043 [SVM_EXIT_READ_CR8] = cr_interception,
3044 [SVM_EXIT_CR0_SEL_WRITE] = cr_interception,
3045 [SVM_EXIT_WRITE_CR0] = cr_interception,
3046 [SVM_EXIT_WRITE_CR3] = cr_interception,
3047 [SVM_EXIT_WRITE_CR4] = cr_interception,
3048 [SVM_EXIT_WRITE_CR8] = cr8_write_interception,
3049 [SVM_EXIT_READ_DR0] = dr_interception,
3050 [SVM_EXIT_READ_DR1] = dr_interception,
3051 [SVM_EXIT_READ_DR2] = dr_interception,
3052 [SVM_EXIT_READ_DR3] = dr_interception,
3053 [SVM_EXIT_READ_DR4] = dr_interception,
3054 [SVM_EXIT_READ_DR5] = dr_interception,
3055 [SVM_EXIT_READ_DR6] = dr_interception,
3056 [SVM_EXIT_READ_DR7] = dr_interception,
3057 [SVM_EXIT_WRITE_DR0] = dr_interception,
3058 [SVM_EXIT_WRITE_DR1] = dr_interception,
3059 [SVM_EXIT_WRITE_DR2] = dr_interception,
3060 [SVM_EXIT_WRITE_DR3] = dr_interception,
3061 [SVM_EXIT_WRITE_DR4] = dr_interception,
3062 [SVM_EXIT_WRITE_DR5] = dr_interception,
3063 [SVM_EXIT_WRITE_DR6] = dr_interception,
3064 [SVM_EXIT_WRITE_DR7] = dr_interception,
3065 [SVM_EXIT_EXCP_BASE + DB_VECTOR] = db_interception,
3066 [SVM_EXIT_EXCP_BASE + BP_VECTOR] = bp_interception,
3067 [SVM_EXIT_EXCP_BASE + UD_VECTOR] = ud_interception,
3068 [SVM_EXIT_EXCP_BASE + PF_VECTOR] = pf_interception,
3069 [SVM_EXIT_EXCP_BASE + MC_VECTOR] = mc_interception,
3070 [SVM_EXIT_EXCP_BASE + AC_VECTOR] = ac_interception,
3071 [SVM_EXIT_EXCP_BASE + GP_VECTOR] = gp_interception,
3072 [SVM_EXIT_INTR] = intr_interception,
3073 [SVM_EXIT_NMI] = nmi_interception,
3074 [SVM_EXIT_SMI] = smi_interception,
3075 [SVM_EXIT_VINTR] = interrupt_window_interception,
3076 [SVM_EXIT_RDPMC] = kvm_emulate_rdpmc,
3077 [SVM_EXIT_CPUID] = kvm_emulate_cpuid,
3078 [SVM_EXIT_IRET] = iret_interception,
3079 [SVM_EXIT_INVD] = kvm_emulate_invd,
3080 [SVM_EXIT_PAUSE] = pause_interception,
3081 [SVM_EXIT_HLT] = kvm_emulate_halt,
3082 [SVM_EXIT_INVLPG] = invlpg_interception,
3083 [SVM_EXIT_INVLPGA] = invlpga_interception,
3084 [SVM_EXIT_IOIO] = io_interception,
3085 [SVM_EXIT_MSR] = msr_interception,
3086 [SVM_EXIT_TASK_SWITCH] = task_switch_interception,
3087 [SVM_EXIT_SHUTDOWN] = shutdown_interception,
3088 [SVM_EXIT_VMRUN] = vmrun_interception,
3089 [SVM_EXIT_VMMCALL] = kvm_emulate_hypercall,
3090 [SVM_EXIT_VMLOAD] = vmload_interception,
3091 [SVM_EXIT_VMSAVE] = vmsave_interception,
3092 [SVM_EXIT_STGI] = stgi_interception,
3093 [SVM_EXIT_CLGI] = clgi_interception,
3094 [SVM_EXIT_SKINIT] = skinit_interception,
3095 [SVM_EXIT_RDTSCP] = kvm_handle_invalid_op,
3096 [SVM_EXIT_WBINVD] = kvm_emulate_wbinvd,
3097 [SVM_EXIT_MONITOR] = kvm_emulate_monitor,
3098 [SVM_EXIT_MWAIT] = kvm_emulate_mwait,
3099 [SVM_EXIT_XSETBV] = kvm_emulate_xsetbv,
3100 [SVM_EXIT_RDPRU] = kvm_handle_invalid_op,
3101 [SVM_EXIT_EFER_WRITE_TRAP] = efer_trap,
3102 [SVM_EXIT_CR0_WRITE_TRAP] = cr_trap,
3103 [SVM_EXIT_CR4_WRITE_TRAP] = cr_trap,
3104 [SVM_EXIT_CR8_WRITE_TRAP] = cr_trap,
3105 [SVM_EXIT_INVPCID] = invpcid_interception,
3106 [SVM_EXIT_NPF] = npf_interception,
3107 [SVM_EXIT_RSM] = rsm_interception,
3108 [SVM_EXIT_AVIC_INCOMPLETE_IPI] = avic_incomplete_ipi_interception,
3109 [SVM_EXIT_AVIC_UNACCELERATED_ACCESS] = avic_unaccelerated_access_interception,
3110 [SVM_EXIT_VMGEXIT] = sev_handle_vmgexit,
3111 };
3112
dump_vmcb(struct kvm_vcpu * vcpu)3113 static void dump_vmcb(struct kvm_vcpu *vcpu)
3114 {
3115 struct vcpu_svm *svm = to_svm(vcpu);
3116 struct vmcb_control_area *control = &svm->vmcb->control;
3117 struct vmcb_save_area *save = &svm->vmcb->save;
3118 struct vmcb_save_area *save01 = &svm->vmcb01.ptr->save;
3119
3120 if (!dump_invalid_vmcb) {
3121 pr_warn_ratelimited("set kvm_amd.dump_invalid_vmcb=1 to dump internal KVM state.\n");
3122 return;
3123 }
3124
3125 pr_err("VMCB %p, last attempted VMRUN on CPU %d\n",
3126 svm->current_vmcb->ptr, vcpu->arch.last_vmentry_cpu);
3127 pr_err("VMCB Control Area:\n");
3128 pr_err("%-20s%04x\n", "cr_read:", control->intercepts[INTERCEPT_CR] & 0xffff);
3129 pr_err("%-20s%04x\n", "cr_write:", control->intercepts[INTERCEPT_CR] >> 16);
3130 pr_err("%-20s%04x\n", "dr_read:", control->intercepts[INTERCEPT_DR] & 0xffff);
3131 pr_err("%-20s%04x\n", "dr_write:", control->intercepts[INTERCEPT_DR] >> 16);
3132 pr_err("%-20s%08x\n", "exceptions:", control->intercepts[INTERCEPT_EXCEPTION]);
3133 pr_err("%-20s%08x %08x\n", "intercepts:",
3134 control->intercepts[INTERCEPT_WORD3],
3135 control->intercepts[INTERCEPT_WORD4]);
3136 pr_err("%-20s%d\n", "pause filter count:", control->pause_filter_count);
3137 pr_err("%-20s%d\n", "pause filter threshold:",
3138 control->pause_filter_thresh);
3139 pr_err("%-20s%016llx\n", "iopm_base_pa:", control->iopm_base_pa);
3140 pr_err("%-20s%016llx\n", "msrpm_base_pa:", control->msrpm_base_pa);
3141 pr_err("%-20s%016llx\n", "tsc_offset:", control->tsc_offset);
3142 pr_err("%-20s%d\n", "asid:", control->asid);
3143 pr_err("%-20s%d\n", "tlb_ctl:", control->tlb_ctl);
3144 pr_err("%-20s%08x\n", "int_ctl:", control->int_ctl);
3145 pr_err("%-20s%08x\n", "int_vector:", control->int_vector);
3146 pr_err("%-20s%08x\n", "int_state:", control->int_state);
3147 pr_err("%-20s%08x\n", "exit_code:", control->exit_code);
3148 pr_err("%-20s%016llx\n", "exit_info1:", control->exit_info_1);
3149 pr_err("%-20s%016llx\n", "exit_info2:", control->exit_info_2);
3150 pr_err("%-20s%08x\n", "exit_int_info:", control->exit_int_info);
3151 pr_err("%-20s%08x\n", "exit_int_info_err:", control->exit_int_info_err);
3152 pr_err("%-20s%lld\n", "nested_ctl:", control->nested_ctl);
3153 pr_err("%-20s%016llx\n", "nested_cr3:", control->nested_cr3);
3154 pr_err("%-20s%016llx\n", "avic_vapic_bar:", control->avic_vapic_bar);
3155 pr_err("%-20s%016llx\n", "ghcb:", control->ghcb_gpa);
3156 pr_err("%-20s%08x\n", "event_inj:", control->event_inj);
3157 pr_err("%-20s%08x\n", "event_inj_err:", control->event_inj_err);
3158 pr_err("%-20s%lld\n", "virt_ext:", control->virt_ext);
3159 pr_err("%-20s%016llx\n", "next_rip:", control->next_rip);
3160 pr_err("%-20s%016llx\n", "avic_backing_page:", control->avic_backing_page);
3161 pr_err("%-20s%016llx\n", "avic_logical_id:", control->avic_logical_id);
3162 pr_err("%-20s%016llx\n", "avic_physical_id:", control->avic_physical_id);
3163 pr_err("%-20s%016llx\n", "vmsa_pa:", control->vmsa_pa);
3164 pr_err("VMCB State Save Area:\n");
3165 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
3166 "es:",
3167 save->es.selector, save->es.attrib,
3168 save->es.limit, save->es.base);
3169 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
3170 "cs:",
3171 save->cs.selector, save->cs.attrib,
3172 save->cs.limit, save->cs.base);
3173 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
3174 "ss:",
3175 save->ss.selector, save->ss.attrib,
3176 save->ss.limit, save->ss.base);
3177 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
3178 "ds:",
3179 save->ds.selector, save->ds.attrib,
3180 save->ds.limit, save->ds.base);
3181 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
3182 "fs:",
3183 save01->fs.selector, save01->fs.attrib,
3184 save01->fs.limit, save01->fs.base);
3185 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
3186 "gs:",
3187 save01->gs.selector, save01->gs.attrib,
3188 save01->gs.limit, save01->gs.base);
3189 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
3190 "gdtr:",
3191 save->gdtr.selector, save->gdtr.attrib,
3192 save->gdtr.limit, save->gdtr.base);
3193 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
3194 "ldtr:",
3195 save01->ldtr.selector, save01->ldtr.attrib,
3196 save01->ldtr.limit, save01->ldtr.base);
3197 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
3198 "idtr:",
3199 save->idtr.selector, save->idtr.attrib,
3200 save->idtr.limit, save->idtr.base);
3201 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
3202 "tr:",
3203 save01->tr.selector, save01->tr.attrib,
3204 save01->tr.limit, save01->tr.base);
3205 pr_err("vmpl: %d cpl: %d efer: %016llx\n",
3206 save->vmpl, save->cpl, save->efer);
3207 pr_err("%-15s %016llx %-13s %016llx\n",
3208 "cr0:", save->cr0, "cr2:", save->cr2);
3209 pr_err("%-15s %016llx %-13s %016llx\n",
3210 "cr3:", save->cr3, "cr4:", save->cr4);
3211 pr_err("%-15s %016llx %-13s %016llx\n",
3212 "dr6:", save->dr6, "dr7:", save->dr7);
3213 pr_err("%-15s %016llx %-13s %016llx\n",
3214 "rip:", save->rip, "rflags:", save->rflags);
3215 pr_err("%-15s %016llx %-13s %016llx\n",
3216 "rsp:", save->rsp, "rax:", save->rax);
3217 pr_err("%-15s %016llx %-13s %016llx\n",
3218 "star:", save01->star, "lstar:", save01->lstar);
3219 pr_err("%-15s %016llx %-13s %016llx\n",
3220 "cstar:", save01->cstar, "sfmask:", save01->sfmask);
3221 pr_err("%-15s %016llx %-13s %016llx\n",
3222 "kernel_gs_base:", save01->kernel_gs_base,
3223 "sysenter_cs:", save01->sysenter_cs);
3224 pr_err("%-15s %016llx %-13s %016llx\n",
3225 "sysenter_esp:", save01->sysenter_esp,
3226 "sysenter_eip:", save01->sysenter_eip);
3227 pr_err("%-15s %016llx %-13s %016llx\n",
3228 "gpat:", save->g_pat, "dbgctl:", save->dbgctl);
3229 pr_err("%-15s %016llx %-13s %016llx\n",
3230 "br_from:", save->br_from, "br_to:", save->br_to);
3231 pr_err("%-15s %016llx %-13s %016llx\n",
3232 "excp_from:", save->last_excp_from,
3233 "excp_to:", save->last_excp_to);
3234 }
3235
svm_check_exit_valid(u64 exit_code)3236 static bool svm_check_exit_valid(u64 exit_code)
3237 {
3238 return (exit_code < ARRAY_SIZE(svm_exit_handlers) &&
3239 svm_exit_handlers[exit_code]);
3240 }
3241
svm_handle_invalid_exit(struct kvm_vcpu * vcpu,u64 exit_code)3242 static int svm_handle_invalid_exit(struct kvm_vcpu *vcpu, u64 exit_code)
3243 {
3244 vcpu_unimpl(vcpu, "svm: unexpected exit reason 0x%llx\n", exit_code);
3245 dump_vmcb(vcpu);
3246 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
3247 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_UNEXPECTED_EXIT_REASON;
3248 vcpu->run->internal.ndata = 2;
3249 vcpu->run->internal.data[0] = exit_code;
3250 vcpu->run->internal.data[1] = vcpu->arch.last_vmentry_cpu;
3251 return 0;
3252 }
3253
svm_invoke_exit_handler(struct kvm_vcpu * vcpu,u64 exit_code)3254 int svm_invoke_exit_handler(struct kvm_vcpu *vcpu, u64 exit_code)
3255 {
3256 if (!svm_check_exit_valid(exit_code))
3257 return svm_handle_invalid_exit(vcpu, exit_code);
3258
3259 #ifdef CONFIG_RETPOLINE
3260 if (exit_code == SVM_EXIT_MSR)
3261 return msr_interception(vcpu);
3262 else if (exit_code == SVM_EXIT_VINTR)
3263 return interrupt_window_interception(vcpu);
3264 else if (exit_code == SVM_EXIT_INTR)
3265 return intr_interception(vcpu);
3266 else if (exit_code == SVM_EXIT_HLT)
3267 return kvm_emulate_halt(vcpu);
3268 else if (exit_code == SVM_EXIT_NPF)
3269 return npf_interception(vcpu);
3270 #endif
3271 return svm_exit_handlers[exit_code](vcpu);
3272 }
3273
svm_get_exit_info(struct kvm_vcpu * vcpu,u32 * reason,u64 * info1,u64 * info2,u32 * intr_info,u32 * error_code)3274 static void svm_get_exit_info(struct kvm_vcpu *vcpu, u32 *reason,
3275 u64 *info1, u64 *info2,
3276 u32 *intr_info, u32 *error_code)
3277 {
3278 struct vmcb_control_area *control = &to_svm(vcpu)->vmcb->control;
3279
3280 *reason = control->exit_code;
3281 *info1 = control->exit_info_1;
3282 *info2 = control->exit_info_2;
3283 *intr_info = control->exit_int_info;
3284 if ((*intr_info & SVM_EXITINTINFO_VALID) &&
3285 (*intr_info & SVM_EXITINTINFO_VALID_ERR))
3286 *error_code = control->exit_int_info_err;
3287 else
3288 *error_code = 0;
3289 }
3290
svm_handle_exit(struct kvm_vcpu * vcpu,fastpath_t exit_fastpath)3291 static int svm_handle_exit(struct kvm_vcpu *vcpu, fastpath_t exit_fastpath)
3292 {
3293 struct vcpu_svm *svm = to_svm(vcpu);
3294 struct kvm_run *kvm_run = vcpu->run;
3295 u32 exit_code = svm->vmcb->control.exit_code;
3296
3297 trace_kvm_exit(vcpu, KVM_ISA_SVM);
3298
3299 /* SEV-ES guests must use the CR write traps to track CR registers. */
3300 if (!sev_es_guest(vcpu->kvm)) {
3301 if (!svm_is_intercept(svm, INTERCEPT_CR0_WRITE))
3302 vcpu->arch.cr0 = svm->vmcb->save.cr0;
3303 if (npt_enabled)
3304 vcpu->arch.cr3 = svm->vmcb->save.cr3;
3305 }
3306
3307 if (is_guest_mode(vcpu)) {
3308 int vmexit;
3309
3310 trace_kvm_nested_vmexit(vcpu, KVM_ISA_SVM);
3311
3312 vmexit = nested_svm_exit_special(svm);
3313
3314 if (vmexit == NESTED_EXIT_CONTINUE)
3315 vmexit = nested_svm_exit_handled(svm);
3316
3317 if (vmexit == NESTED_EXIT_DONE)
3318 return 1;
3319 }
3320
3321 if (svm->vmcb->control.exit_code == SVM_EXIT_ERR) {
3322 kvm_run->exit_reason = KVM_EXIT_FAIL_ENTRY;
3323 kvm_run->fail_entry.hardware_entry_failure_reason
3324 = svm->vmcb->control.exit_code;
3325 kvm_run->fail_entry.cpu = vcpu->arch.last_vmentry_cpu;
3326 dump_vmcb(vcpu);
3327 return 0;
3328 }
3329
3330 if (is_external_interrupt(svm->vmcb->control.exit_int_info) &&
3331 exit_code != SVM_EXIT_EXCP_BASE + PF_VECTOR &&
3332 exit_code != SVM_EXIT_NPF && exit_code != SVM_EXIT_TASK_SWITCH &&
3333 exit_code != SVM_EXIT_INTR && exit_code != SVM_EXIT_NMI)
3334 printk(KERN_ERR "%s: unexpected exit_int_info 0x%x "
3335 "exit_code 0x%x\n",
3336 __func__, svm->vmcb->control.exit_int_info,
3337 exit_code);
3338
3339 if (exit_fastpath != EXIT_FASTPATH_NONE)
3340 return 1;
3341
3342 return svm_invoke_exit_handler(vcpu, exit_code);
3343 }
3344
reload_tss(struct kvm_vcpu * vcpu)3345 static void reload_tss(struct kvm_vcpu *vcpu)
3346 {
3347 struct svm_cpu_data *sd = per_cpu(svm_data, vcpu->cpu);
3348
3349 sd->tss_desc->type = 9; /* available 32/64-bit TSS */
3350 load_TR_desc();
3351 }
3352
pre_svm_run(struct kvm_vcpu * vcpu)3353 static void pre_svm_run(struct kvm_vcpu *vcpu)
3354 {
3355 struct svm_cpu_data *sd = per_cpu(svm_data, vcpu->cpu);
3356 struct vcpu_svm *svm = to_svm(vcpu);
3357
3358 /*
3359 * If the previous vmrun of the vmcb occurred on a different physical
3360 * cpu, then mark the vmcb dirty and assign a new asid. Hardware's
3361 * vmcb clean bits are per logical CPU, as are KVM's asid assignments.
3362 */
3363 if (unlikely(svm->current_vmcb->cpu != vcpu->cpu)) {
3364 svm->current_vmcb->asid_generation = 0;
3365 vmcb_mark_all_dirty(svm->vmcb);
3366 svm->current_vmcb->cpu = vcpu->cpu;
3367 }
3368
3369 if (sev_guest(vcpu->kvm))
3370 return pre_sev_run(svm, vcpu->cpu);
3371
3372 /* FIXME: handle wraparound of asid_generation */
3373 if (svm->current_vmcb->asid_generation != sd->asid_generation)
3374 new_asid(svm, sd);
3375 }
3376
svm_inject_nmi(struct kvm_vcpu * vcpu)3377 static void svm_inject_nmi(struct kvm_vcpu *vcpu)
3378 {
3379 struct vcpu_svm *svm = to_svm(vcpu);
3380
3381 svm->vmcb->control.event_inj = SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_NMI;
3382 vcpu->arch.hflags |= HF_NMI_MASK;
3383 if (!sev_es_guest(vcpu->kvm))
3384 svm_set_intercept(svm, INTERCEPT_IRET);
3385 ++vcpu->stat.nmi_injections;
3386 }
3387
svm_inject_irq(struct kvm_vcpu * vcpu)3388 static void svm_inject_irq(struct kvm_vcpu *vcpu)
3389 {
3390 struct vcpu_svm *svm = to_svm(vcpu);
3391
3392 trace_kvm_inj_virq(vcpu->arch.interrupt.nr);
3393 ++vcpu->stat.irq_injections;
3394
3395 svm->vmcb->control.event_inj = vcpu->arch.interrupt.nr |
3396 SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_INTR;
3397 }
3398
svm_complete_interrupt_delivery(struct kvm_vcpu * vcpu,int delivery_mode,int trig_mode,int vector)3399 void svm_complete_interrupt_delivery(struct kvm_vcpu *vcpu, int delivery_mode,
3400 int trig_mode, int vector)
3401 {
3402 /*
3403 * vcpu->arch.apicv_active must be read after vcpu->mode.
3404 * Pairs with smp_store_release in vcpu_enter_guest.
3405 */
3406 bool in_guest_mode = (smp_load_acquire(&vcpu->mode) == IN_GUEST_MODE);
3407
3408 if (!READ_ONCE(vcpu->arch.apicv_active)) {
3409 /* Process the interrupt via inject_pending_event */
3410 kvm_make_request(KVM_REQ_EVENT, vcpu);
3411 kvm_vcpu_kick(vcpu);
3412 return;
3413 }
3414
3415 trace_kvm_apicv_accept_irq(vcpu->vcpu_id, delivery_mode, trig_mode, vector);
3416 if (in_guest_mode) {
3417 /*
3418 * Signal the doorbell to tell hardware to inject the IRQ. If
3419 * the vCPU exits the guest before the doorbell chimes, hardware
3420 * will automatically process AVIC interrupts at the next VMRUN.
3421 */
3422 avic_ring_doorbell(vcpu);
3423 } else {
3424 /*
3425 * Wake the vCPU if it was blocking. KVM will then detect the
3426 * pending IRQ when checking if the vCPU has a wake event.
3427 */
3428 kvm_vcpu_wake_up(vcpu);
3429 }
3430 }
3431
svm_deliver_interrupt(struct kvm_lapic * apic,int delivery_mode,int trig_mode,int vector)3432 static void svm_deliver_interrupt(struct kvm_lapic *apic, int delivery_mode,
3433 int trig_mode, int vector)
3434 {
3435 kvm_lapic_set_irr(vector, apic);
3436
3437 /*
3438 * Pairs with the smp_mb_*() after setting vcpu->guest_mode in
3439 * vcpu_enter_guest() to ensure the write to the vIRR is ordered before
3440 * the read of guest_mode. This guarantees that either VMRUN will see
3441 * and process the new vIRR entry, or that svm_complete_interrupt_delivery
3442 * will signal the doorbell if the CPU has already entered the guest.
3443 */
3444 smp_mb__after_atomic();
3445 svm_complete_interrupt_delivery(apic->vcpu, delivery_mode, trig_mode, vector);
3446 }
3447
svm_update_cr8_intercept(struct kvm_vcpu * vcpu,int tpr,int irr)3448 static void svm_update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
3449 {
3450 struct vcpu_svm *svm = to_svm(vcpu);
3451
3452 /*
3453 * SEV-ES guests must always keep the CR intercepts cleared. CR
3454 * tracking is done using the CR write traps.
3455 */
3456 if (sev_es_guest(vcpu->kvm))
3457 return;
3458
3459 if (nested_svm_virtualize_tpr(vcpu))
3460 return;
3461
3462 svm_clr_intercept(svm, INTERCEPT_CR8_WRITE);
3463
3464 if (irr == -1)
3465 return;
3466
3467 if (tpr >= irr)
3468 svm_set_intercept(svm, INTERCEPT_CR8_WRITE);
3469 }
3470
svm_nmi_blocked(struct kvm_vcpu * vcpu)3471 bool svm_nmi_blocked(struct kvm_vcpu *vcpu)
3472 {
3473 struct vcpu_svm *svm = to_svm(vcpu);
3474 struct vmcb *vmcb = svm->vmcb;
3475 bool ret;
3476
3477 if (!gif_set(svm))
3478 return true;
3479
3480 if (is_guest_mode(vcpu) && nested_exit_on_nmi(svm))
3481 return false;
3482
3483 ret = (vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK) ||
3484 (vcpu->arch.hflags & HF_NMI_MASK);
3485
3486 return ret;
3487 }
3488
svm_nmi_allowed(struct kvm_vcpu * vcpu,bool for_injection)3489 static int svm_nmi_allowed(struct kvm_vcpu *vcpu, bool for_injection)
3490 {
3491 struct vcpu_svm *svm = to_svm(vcpu);
3492 if (svm->nested.nested_run_pending)
3493 return -EBUSY;
3494
3495 if (svm_nmi_blocked(vcpu))
3496 return 0;
3497
3498 /* An NMI must not be injected into L2 if it's supposed to VM-Exit. */
3499 if (for_injection && is_guest_mode(vcpu) && nested_exit_on_nmi(svm))
3500 return -EBUSY;
3501 return 1;
3502 }
3503
svm_get_nmi_mask(struct kvm_vcpu * vcpu)3504 static bool svm_get_nmi_mask(struct kvm_vcpu *vcpu)
3505 {
3506 return !!(vcpu->arch.hflags & HF_NMI_MASK);
3507 }
3508
svm_set_nmi_mask(struct kvm_vcpu * vcpu,bool masked)3509 static void svm_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
3510 {
3511 struct vcpu_svm *svm = to_svm(vcpu);
3512
3513 if (masked) {
3514 vcpu->arch.hflags |= HF_NMI_MASK;
3515 if (!sev_es_guest(vcpu->kvm))
3516 svm_set_intercept(svm, INTERCEPT_IRET);
3517 } else {
3518 vcpu->arch.hflags &= ~HF_NMI_MASK;
3519 if (!sev_es_guest(vcpu->kvm))
3520 svm_clr_intercept(svm, INTERCEPT_IRET);
3521 }
3522 }
3523
svm_interrupt_blocked(struct kvm_vcpu * vcpu)3524 bool svm_interrupt_blocked(struct kvm_vcpu *vcpu)
3525 {
3526 struct vcpu_svm *svm = to_svm(vcpu);
3527 struct vmcb *vmcb = svm->vmcb;
3528
3529 if (!gif_set(svm))
3530 return true;
3531
3532 if (is_guest_mode(vcpu)) {
3533 /* As long as interrupts are being delivered... */
3534 if ((svm->nested.ctl.int_ctl & V_INTR_MASKING_MASK)
3535 ? !(svm->vmcb01.ptr->save.rflags & X86_EFLAGS_IF)
3536 : !(kvm_get_rflags(vcpu) & X86_EFLAGS_IF))
3537 return true;
3538
3539 /* ... vmexits aren't blocked by the interrupt shadow */
3540 if (nested_exit_on_intr(svm))
3541 return false;
3542 } else {
3543 if (!svm_get_if_flag(vcpu))
3544 return true;
3545 }
3546
3547 return (vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK);
3548 }
3549
svm_interrupt_allowed(struct kvm_vcpu * vcpu,bool for_injection)3550 static int svm_interrupt_allowed(struct kvm_vcpu *vcpu, bool for_injection)
3551 {
3552 struct vcpu_svm *svm = to_svm(vcpu);
3553
3554 if (svm->nested.nested_run_pending)
3555 return -EBUSY;
3556
3557 if (svm_interrupt_blocked(vcpu))
3558 return 0;
3559
3560 /*
3561 * An IRQ must not be injected into L2 if it's supposed to VM-Exit,
3562 * e.g. if the IRQ arrived asynchronously after checking nested events.
3563 */
3564 if (for_injection && is_guest_mode(vcpu) && nested_exit_on_intr(svm))
3565 return -EBUSY;
3566
3567 return 1;
3568 }
3569
svm_enable_irq_window(struct kvm_vcpu * vcpu)3570 static void svm_enable_irq_window(struct kvm_vcpu *vcpu)
3571 {
3572 struct vcpu_svm *svm = to_svm(vcpu);
3573
3574 /*
3575 * In case GIF=0 we can't rely on the CPU to tell us when GIF becomes
3576 * 1, because that's a separate STGI/VMRUN intercept. The next time we
3577 * get that intercept, this function will be called again though and
3578 * we'll get the vintr intercept. However, if the vGIF feature is
3579 * enabled, the STGI interception will not occur. Enable the irq
3580 * window under the assumption that the hardware will set the GIF.
3581 */
3582 if (vgif || gif_set(svm)) {
3583 /*
3584 * IRQ window is not needed when AVIC is enabled,
3585 * unless we have pending ExtINT since it cannot be injected
3586 * via AVIC. In such case, KVM needs to temporarily disable AVIC,
3587 * and fallback to injecting IRQ via V_IRQ.
3588 *
3589 * If running nested, AVIC is already locally inhibited
3590 * on this vCPU, therefore there is no need to request
3591 * the VM wide AVIC inhibition.
3592 */
3593 if (!is_guest_mode(vcpu))
3594 kvm_set_apicv_inhibit(vcpu->kvm, APICV_INHIBIT_REASON_IRQWIN);
3595
3596 svm_set_vintr(svm);
3597 }
3598 }
3599
svm_enable_nmi_window(struct kvm_vcpu * vcpu)3600 static void svm_enable_nmi_window(struct kvm_vcpu *vcpu)
3601 {
3602 struct vcpu_svm *svm = to_svm(vcpu);
3603
3604 if ((vcpu->arch.hflags & (HF_NMI_MASK | HF_IRET_MASK)) == HF_NMI_MASK)
3605 return; /* IRET will cause a vm exit */
3606
3607 if (!gif_set(svm)) {
3608 if (vgif)
3609 svm_set_intercept(svm, INTERCEPT_STGI);
3610 return; /* STGI will cause a vm exit */
3611 }
3612
3613 /*
3614 * Something prevents NMI from been injected. Single step over possible
3615 * problem (IRET or exception injection or interrupt shadow)
3616 */
3617 svm->nmi_singlestep_guest_rflags = svm_get_rflags(vcpu);
3618 svm->nmi_singlestep = true;
3619 svm->vmcb->save.rflags |= (X86_EFLAGS_TF | X86_EFLAGS_RF);
3620 }
3621
svm_flush_tlb_current(struct kvm_vcpu * vcpu)3622 static void svm_flush_tlb_current(struct kvm_vcpu *vcpu)
3623 {
3624 struct vcpu_svm *svm = to_svm(vcpu);
3625
3626 /*
3627 * Flush only the current ASID even if the TLB flush was invoked via
3628 * kvm_flush_remote_tlbs(). Although flushing remote TLBs requires all
3629 * ASIDs to be flushed, KVM uses a single ASID for L1 and L2, and
3630 * unconditionally does a TLB flush on both nested VM-Enter and nested
3631 * VM-Exit (via kvm_mmu_reset_context()).
3632 */
3633 if (static_cpu_has(X86_FEATURE_FLUSHBYASID))
3634 svm->vmcb->control.tlb_ctl = TLB_CONTROL_FLUSH_ASID;
3635 else
3636 svm->current_vmcb->asid_generation--;
3637 }
3638
svm_flush_tlb_gva(struct kvm_vcpu * vcpu,gva_t gva)3639 static void svm_flush_tlb_gva(struct kvm_vcpu *vcpu, gva_t gva)
3640 {
3641 struct vcpu_svm *svm = to_svm(vcpu);
3642
3643 invlpga(gva, svm->vmcb->control.asid);
3644 }
3645
sync_cr8_to_lapic(struct kvm_vcpu * vcpu)3646 static inline void sync_cr8_to_lapic(struct kvm_vcpu *vcpu)
3647 {
3648 struct vcpu_svm *svm = to_svm(vcpu);
3649
3650 if (nested_svm_virtualize_tpr(vcpu))
3651 return;
3652
3653 if (!svm_is_intercept(svm, INTERCEPT_CR8_WRITE)) {
3654 int cr8 = svm->vmcb->control.int_ctl & V_TPR_MASK;
3655 kvm_set_cr8(vcpu, cr8);
3656 }
3657 }
3658
sync_lapic_to_cr8(struct kvm_vcpu * vcpu)3659 static inline void sync_lapic_to_cr8(struct kvm_vcpu *vcpu)
3660 {
3661 struct vcpu_svm *svm = to_svm(vcpu);
3662 u64 cr8;
3663
3664 if (nested_svm_virtualize_tpr(vcpu) ||
3665 kvm_vcpu_apicv_active(vcpu))
3666 return;
3667
3668 cr8 = kvm_get_cr8(vcpu);
3669 svm->vmcb->control.int_ctl &= ~V_TPR_MASK;
3670 svm->vmcb->control.int_ctl |= cr8 & V_TPR_MASK;
3671 }
3672
svm_complete_interrupts(struct kvm_vcpu * vcpu)3673 static void svm_complete_interrupts(struct kvm_vcpu *vcpu)
3674 {
3675 struct vcpu_svm *svm = to_svm(vcpu);
3676 u8 vector;
3677 int type;
3678 u32 exitintinfo = svm->vmcb->control.exit_int_info;
3679 unsigned int3_injected = svm->int3_injected;
3680
3681 svm->int3_injected = 0;
3682
3683 /*
3684 * If we've made progress since setting HF_IRET_MASK, we've
3685 * executed an IRET and can allow NMI injection.
3686 */
3687 if ((vcpu->arch.hflags & HF_IRET_MASK) &&
3688 (sev_es_guest(vcpu->kvm) ||
3689 kvm_rip_read(vcpu) != svm->nmi_iret_rip)) {
3690 vcpu->arch.hflags &= ~(HF_NMI_MASK | HF_IRET_MASK);
3691 kvm_make_request(KVM_REQ_EVENT, vcpu);
3692 }
3693
3694 vcpu->arch.nmi_injected = false;
3695 kvm_clear_exception_queue(vcpu);
3696 kvm_clear_interrupt_queue(vcpu);
3697
3698 if (!(exitintinfo & SVM_EXITINTINFO_VALID))
3699 return;
3700
3701 kvm_make_request(KVM_REQ_EVENT, vcpu);
3702
3703 vector = exitintinfo & SVM_EXITINTINFO_VEC_MASK;
3704 type = exitintinfo & SVM_EXITINTINFO_TYPE_MASK;
3705
3706 /*
3707 * If NextRIP isn't enabled, KVM must manually advance RIP prior to
3708 * injecting the soft exception/interrupt. That advancement needs to
3709 * be unwound if vectoring didn't complete. Note, the new event may
3710 * not be the injected event, e.g. if KVM injected an INTn, the INTn
3711 * hit a #NP in the guest, and the #NP encountered a #PF, the #NP will
3712 * be the reported vectored event, but RIP still needs to be unwound.
3713 */
3714 if (int3_injected && type == SVM_EXITINTINFO_TYPE_EXEPT &&
3715 kvm_is_linear_rip(vcpu, svm->int3_rip))
3716 kvm_rip_write(vcpu, kvm_rip_read(vcpu) - int3_injected);
3717
3718 switch (type) {
3719 case SVM_EXITINTINFO_TYPE_NMI:
3720 vcpu->arch.nmi_injected = true;
3721 break;
3722 case SVM_EXITINTINFO_TYPE_EXEPT:
3723 /*
3724 * Never re-inject a #VC exception.
3725 */
3726 if (vector == X86_TRAP_VC)
3727 break;
3728
3729 /*
3730 * In case of software exceptions, do not reinject the vector,
3731 * but re-execute the instruction instead.
3732 */
3733 if (kvm_exception_is_soft(vector))
3734 break;
3735
3736 if (exitintinfo & SVM_EXITINTINFO_VALID_ERR) {
3737 u32 err = svm->vmcb->control.exit_int_info_err;
3738 kvm_requeue_exception_e(vcpu, vector, err);
3739
3740 } else
3741 kvm_requeue_exception(vcpu, vector);
3742 break;
3743 case SVM_EXITINTINFO_TYPE_INTR:
3744 kvm_queue_interrupt(vcpu, vector, false);
3745 break;
3746 default:
3747 break;
3748 }
3749 }
3750
svm_cancel_injection(struct kvm_vcpu * vcpu)3751 static void svm_cancel_injection(struct kvm_vcpu *vcpu)
3752 {
3753 struct vcpu_svm *svm = to_svm(vcpu);
3754 struct vmcb_control_area *control = &svm->vmcb->control;
3755
3756 control->exit_int_info = control->event_inj;
3757 control->exit_int_info_err = control->event_inj_err;
3758 control->event_inj = 0;
3759 svm_complete_interrupts(vcpu);
3760 }
3761
svm_vcpu_pre_run(struct kvm_vcpu * vcpu)3762 static int svm_vcpu_pre_run(struct kvm_vcpu *vcpu)
3763 {
3764 return 1;
3765 }
3766
svm_exit_handlers_fastpath(struct kvm_vcpu * vcpu)3767 static fastpath_t svm_exit_handlers_fastpath(struct kvm_vcpu *vcpu)
3768 {
3769 if (to_svm(vcpu)->vmcb->control.exit_code == SVM_EXIT_MSR &&
3770 to_svm(vcpu)->vmcb->control.exit_info_1)
3771 return handle_fastpath_set_msr_irqoff(vcpu);
3772
3773 return EXIT_FASTPATH_NONE;
3774 }
3775
svm_vcpu_enter_exit(struct kvm_vcpu * vcpu)3776 static noinstr void svm_vcpu_enter_exit(struct kvm_vcpu *vcpu)
3777 {
3778 struct vcpu_svm *svm = to_svm(vcpu);
3779 unsigned long vmcb_pa = svm->current_vmcb->pa;
3780
3781 guest_state_enter_irqoff();
3782
3783 if (sev_es_guest(vcpu->kvm)) {
3784 __svm_sev_es_vcpu_run(vmcb_pa);
3785 } else {
3786 struct svm_cpu_data *sd = per_cpu(svm_data, vcpu->cpu);
3787
3788 /*
3789 * Use a single vmcb (vmcb01 because it's always valid) for
3790 * context switching guest state via VMLOAD/VMSAVE, that way
3791 * the state doesn't need to be copied between vmcb01 and
3792 * vmcb02 when switching vmcbs for nested virtualization.
3793 */
3794 vmload(svm->vmcb01.pa);
3795 __svm_vcpu_run(vmcb_pa, (unsigned long *)&vcpu->arch.regs);
3796 vmsave(svm->vmcb01.pa);
3797
3798 vmload(__sme_page_pa(sd->save_area));
3799 }
3800
3801 guest_state_exit_irqoff();
3802 }
3803
svm_vcpu_run(struct kvm_vcpu * vcpu)3804 static __no_kcsan fastpath_t svm_vcpu_run(struct kvm_vcpu *vcpu)
3805 {
3806 struct vcpu_svm *svm = to_svm(vcpu);
3807
3808 trace_kvm_entry(vcpu);
3809
3810 svm->vmcb->save.rax = vcpu->arch.regs[VCPU_REGS_RAX];
3811 svm->vmcb->save.rsp = vcpu->arch.regs[VCPU_REGS_RSP];
3812 svm->vmcb->save.rip = vcpu->arch.regs[VCPU_REGS_RIP];
3813
3814 /*
3815 * Disable singlestep if we're injecting an interrupt/exception.
3816 * We don't want our modified rflags to be pushed on the stack where
3817 * we might not be able to easily reset them if we disabled NMI
3818 * singlestep later.
3819 */
3820 if (svm->nmi_singlestep && svm->vmcb->control.event_inj) {
3821 /*
3822 * Event injection happens before external interrupts cause a
3823 * vmexit and interrupts are disabled here, so smp_send_reschedule
3824 * is enough to force an immediate vmexit.
3825 */
3826 disable_nmi_singlestep(svm);
3827 smp_send_reschedule(vcpu->cpu);
3828 }
3829
3830 pre_svm_run(vcpu);
3831
3832 sync_lapic_to_cr8(vcpu);
3833
3834 if (unlikely(svm->asid != svm->vmcb->control.asid)) {
3835 svm->vmcb->control.asid = svm->asid;
3836 vmcb_mark_dirty(svm->vmcb, VMCB_ASID);
3837 }
3838 svm->vmcb->save.cr2 = vcpu->arch.cr2;
3839
3840 svm_hv_update_vp_id(svm->vmcb, vcpu);
3841
3842 /*
3843 * Run with all-zero DR6 unless needed, so that we can get the exact cause
3844 * of a #DB.
3845 */
3846 if (unlikely(vcpu->arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT))
3847 svm_set_dr6(svm, vcpu->arch.dr6);
3848 else
3849 svm_set_dr6(svm, DR6_ACTIVE_LOW);
3850
3851 clgi();
3852 kvm_load_guest_xsave_state(vcpu);
3853
3854 kvm_wait_lapic_expire(vcpu);
3855
3856 /*
3857 * If this vCPU has touched SPEC_CTRL, restore the guest's value if
3858 * it's non-zero. Since vmentry is serialising on affected CPUs, there
3859 * is no need to worry about the conditional branch over the wrmsr
3860 * being speculatively taken.
3861 */
3862 if (!static_cpu_has(X86_FEATURE_V_SPEC_CTRL))
3863 x86_spec_ctrl_set_guest(svm->spec_ctrl, svm->virt_spec_ctrl);
3864
3865 svm_vcpu_enter_exit(vcpu);
3866
3867 /*
3868 * We do not use IBRS in the kernel. If this vCPU has used the
3869 * SPEC_CTRL MSR it may have left it on; save the value and
3870 * turn it off. This is much more efficient than blindly adding
3871 * it to the atomic save/restore list. Especially as the former
3872 * (Saving guest MSRs on vmexit) doesn't even exist in KVM.
3873 *
3874 * For non-nested case:
3875 * If the L01 MSR bitmap does not intercept the MSR, then we need to
3876 * save it.
3877 *
3878 * For nested case:
3879 * If the L02 MSR bitmap does not intercept the MSR, then we need to
3880 * save it.
3881 */
3882 if (!static_cpu_has(X86_FEATURE_V_SPEC_CTRL) &&
3883 unlikely(!msr_write_intercepted(vcpu, MSR_IA32_SPEC_CTRL)))
3884 svm->spec_ctrl = native_read_msr(MSR_IA32_SPEC_CTRL);
3885
3886 if (!sev_es_guest(vcpu->kvm))
3887 reload_tss(vcpu);
3888
3889 if (!static_cpu_has(X86_FEATURE_V_SPEC_CTRL))
3890 x86_spec_ctrl_restore_host(svm->spec_ctrl, svm->virt_spec_ctrl);
3891
3892 if (!sev_es_guest(vcpu->kvm)) {
3893 vcpu->arch.cr2 = svm->vmcb->save.cr2;
3894 vcpu->arch.regs[VCPU_REGS_RAX] = svm->vmcb->save.rax;
3895 vcpu->arch.regs[VCPU_REGS_RSP] = svm->vmcb->save.rsp;
3896 vcpu->arch.regs[VCPU_REGS_RIP] = svm->vmcb->save.rip;
3897 }
3898 vcpu->arch.regs_dirty = 0;
3899
3900 if (unlikely(svm->vmcb->control.exit_code == SVM_EXIT_NMI))
3901 kvm_before_interrupt(vcpu, KVM_HANDLING_NMI);
3902
3903 kvm_load_host_xsave_state(vcpu);
3904 stgi();
3905
3906 /* Any pending NMI will happen here */
3907
3908 if (unlikely(svm->vmcb->control.exit_code == SVM_EXIT_NMI))
3909 kvm_after_interrupt(vcpu);
3910
3911 sync_cr8_to_lapic(vcpu);
3912
3913 svm->next_rip = 0;
3914 if (is_guest_mode(vcpu)) {
3915 nested_sync_control_from_vmcb02(svm);
3916
3917 /* Track VMRUNs that have made past consistency checking */
3918 if (svm->nested.nested_run_pending &&
3919 svm->vmcb->control.exit_code != SVM_EXIT_ERR)
3920 ++vcpu->stat.nested_run;
3921
3922 svm->nested.nested_run_pending = 0;
3923 }
3924
3925 svm->vmcb->control.tlb_ctl = TLB_CONTROL_DO_NOTHING;
3926 vmcb_mark_all_clean(svm->vmcb);
3927
3928 /* if exit due to PF check for async PF */
3929 if (svm->vmcb->control.exit_code == SVM_EXIT_EXCP_BASE + PF_VECTOR)
3930 vcpu->arch.apf.host_apf_flags =
3931 kvm_read_and_reset_apf_flags();
3932
3933 vcpu->arch.regs_avail &= ~SVM_REGS_LAZY_LOAD_SET;
3934
3935 /*
3936 * We need to handle MC intercepts here before the vcpu has a chance to
3937 * change the physical cpu
3938 */
3939 if (unlikely(svm->vmcb->control.exit_code ==
3940 SVM_EXIT_EXCP_BASE + MC_VECTOR))
3941 svm_handle_mce(vcpu);
3942
3943 svm_complete_interrupts(vcpu);
3944
3945 if (is_guest_mode(vcpu))
3946 return EXIT_FASTPATH_NONE;
3947
3948 return svm_exit_handlers_fastpath(vcpu);
3949 }
3950
svm_load_mmu_pgd(struct kvm_vcpu * vcpu,hpa_t root_hpa,int root_level)3951 static void svm_load_mmu_pgd(struct kvm_vcpu *vcpu, hpa_t root_hpa,
3952 int root_level)
3953 {
3954 struct vcpu_svm *svm = to_svm(vcpu);
3955 unsigned long cr3;
3956
3957 if (npt_enabled) {
3958 svm->vmcb->control.nested_cr3 = __sme_set(root_hpa);
3959 vmcb_mark_dirty(svm->vmcb, VMCB_NPT);
3960
3961 hv_track_root_tdp(vcpu, root_hpa);
3962
3963 cr3 = vcpu->arch.cr3;
3964 } else if (vcpu->arch.mmu->root_role.level >= PT64_ROOT_4LEVEL) {
3965 cr3 = __sme_set(root_hpa) | kvm_get_active_pcid(vcpu);
3966 } else {
3967 /* PCID in the guest should be impossible with a 32-bit MMU. */
3968 WARN_ON_ONCE(kvm_get_active_pcid(vcpu));
3969 cr3 = root_hpa;
3970 }
3971
3972 svm->vmcb->save.cr3 = cr3;
3973 vmcb_mark_dirty(svm->vmcb, VMCB_CR);
3974 }
3975
is_disabled(void)3976 static int is_disabled(void)
3977 {
3978 u64 vm_cr;
3979
3980 rdmsrl(MSR_VM_CR, vm_cr);
3981 if (vm_cr & (1 << SVM_VM_CR_SVM_DISABLE))
3982 return 1;
3983
3984 return 0;
3985 }
3986
3987 static void
svm_patch_hypercall(struct kvm_vcpu * vcpu,unsigned char * hypercall)3988 svm_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
3989 {
3990 /*
3991 * Patch in the VMMCALL instruction:
3992 */
3993 hypercall[0] = 0x0f;
3994 hypercall[1] = 0x01;
3995 hypercall[2] = 0xd9;
3996 }
3997
svm_check_processor_compat(void)3998 static int __init svm_check_processor_compat(void)
3999 {
4000 return 0;
4001 }
4002
4003 /*
4004 * The kvm parameter can be NULL (module initialization, or invocation before
4005 * VM creation). Be sure to check the kvm parameter before using it.
4006 */
svm_has_emulated_msr(struct kvm * kvm,u32 index)4007 static bool svm_has_emulated_msr(struct kvm *kvm, u32 index)
4008 {
4009 switch (index) {
4010 case MSR_IA32_MCG_EXT_CTL:
4011 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
4012 return false;
4013 case MSR_IA32_SMBASE:
4014 /* SEV-ES guests do not support SMM, so report false */
4015 if (kvm && sev_es_guest(kvm))
4016 return false;
4017 break;
4018 default:
4019 break;
4020 }
4021
4022 return true;
4023 }
4024
svm_get_mt_mask(struct kvm_vcpu * vcpu,gfn_t gfn,bool is_mmio)4025 static u64 svm_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
4026 {
4027 return 0;
4028 }
4029
svm_vcpu_after_set_cpuid(struct kvm_vcpu * vcpu)4030 static void svm_vcpu_after_set_cpuid(struct kvm_vcpu *vcpu)
4031 {
4032 struct vcpu_svm *svm = to_svm(vcpu);
4033 struct kvm_cpuid_entry2 *best;
4034 struct kvm *kvm = vcpu->kvm;
4035
4036 vcpu->arch.xsaves_enabled = guest_cpuid_has(vcpu, X86_FEATURE_XSAVE) &&
4037 boot_cpu_has(X86_FEATURE_XSAVE) &&
4038 boot_cpu_has(X86_FEATURE_XSAVES);
4039
4040 /* Update nrips enabled cache */
4041 svm->nrips_enabled = kvm_cpu_cap_has(X86_FEATURE_NRIPS) &&
4042 guest_cpuid_has(vcpu, X86_FEATURE_NRIPS);
4043
4044 svm->tsc_scaling_enabled = tsc_scaling && guest_cpuid_has(vcpu, X86_FEATURE_TSCRATEMSR);
4045 svm->lbrv_enabled = lbrv && guest_cpuid_has(vcpu, X86_FEATURE_LBRV);
4046
4047 svm->v_vmload_vmsave_enabled = vls && guest_cpuid_has(vcpu, X86_FEATURE_V_VMSAVE_VMLOAD);
4048
4049 svm->pause_filter_enabled = kvm_cpu_cap_has(X86_FEATURE_PAUSEFILTER) &&
4050 guest_cpuid_has(vcpu, X86_FEATURE_PAUSEFILTER);
4051
4052 svm->pause_threshold_enabled = kvm_cpu_cap_has(X86_FEATURE_PFTHRESHOLD) &&
4053 guest_cpuid_has(vcpu, X86_FEATURE_PFTHRESHOLD);
4054
4055 svm->vgif_enabled = vgif && guest_cpuid_has(vcpu, X86_FEATURE_VGIF);
4056
4057 svm_recalc_instruction_intercepts(vcpu, svm);
4058
4059 /* For sev guests, the memory encryption bit is not reserved in CR3. */
4060 if (sev_guest(vcpu->kvm)) {
4061 best = kvm_find_cpuid_entry(vcpu, 0x8000001F, 0);
4062 if (best)
4063 vcpu->arch.reserved_gpa_bits &= ~(1UL << (best->ebx & 0x3f));
4064 }
4065
4066 if (kvm_vcpu_apicv_active(vcpu)) {
4067 /*
4068 * AVIC does not work with an x2APIC mode guest. If the X2APIC feature
4069 * is exposed to the guest, disable AVIC.
4070 */
4071 if (guest_cpuid_has(vcpu, X86_FEATURE_X2APIC))
4072 kvm_set_apicv_inhibit(kvm, APICV_INHIBIT_REASON_X2APIC);
4073 }
4074 init_vmcb_after_set_cpuid(vcpu);
4075 }
4076
svm_has_wbinvd_exit(void)4077 static bool svm_has_wbinvd_exit(void)
4078 {
4079 return true;
4080 }
4081
4082 #define PRE_EX(exit) { .exit_code = (exit), \
4083 .stage = X86_ICPT_PRE_EXCEPT, }
4084 #define POST_EX(exit) { .exit_code = (exit), \
4085 .stage = X86_ICPT_POST_EXCEPT, }
4086 #define POST_MEM(exit) { .exit_code = (exit), \
4087 .stage = X86_ICPT_POST_MEMACCESS, }
4088
4089 static const struct __x86_intercept {
4090 u32 exit_code;
4091 enum x86_intercept_stage stage;
4092 } x86_intercept_map[] = {
4093 [x86_intercept_cr_read] = POST_EX(SVM_EXIT_READ_CR0),
4094 [x86_intercept_cr_write] = POST_EX(SVM_EXIT_WRITE_CR0),
4095 [x86_intercept_clts] = POST_EX(SVM_EXIT_WRITE_CR0),
4096 [x86_intercept_lmsw] = POST_EX(SVM_EXIT_WRITE_CR0),
4097 [x86_intercept_smsw] = POST_EX(SVM_EXIT_READ_CR0),
4098 [x86_intercept_dr_read] = POST_EX(SVM_EXIT_READ_DR0),
4099 [x86_intercept_dr_write] = POST_EX(SVM_EXIT_WRITE_DR0),
4100 [x86_intercept_sldt] = POST_EX(SVM_EXIT_LDTR_READ),
4101 [x86_intercept_str] = POST_EX(SVM_EXIT_TR_READ),
4102 [x86_intercept_lldt] = POST_EX(SVM_EXIT_LDTR_WRITE),
4103 [x86_intercept_ltr] = POST_EX(SVM_EXIT_TR_WRITE),
4104 [x86_intercept_sgdt] = POST_EX(SVM_EXIT_GDTR_READ),
4105 [x86_intercept_sidt] = POST_EX(SVM_EXIT_IDTR_READ),
4106 [x86_intercept_lgdt] = POST_EX(SVM_EXIT_GDTR_WRITE),
4107 [x86_intercept_lidt] = POST_EX(SVM_EXIT_IDTR_WRITE),
4108 [x86_intercept_vmrun] = POST_EX(SVM_EXIT_VMRUN),
4109 [x86_intercept_vmmcall] = POST_EX(SVM_EXIT_VMMCALL),
4110 [x86_intercept_vmload] = POST_EX(SVM_EXIT_VMLOAD),
4111 [x86_intercept_vmsave] = POST_EX(SVM_EXIT_VMSAVE),
4112 [x86_intercept_stgi] = POST_EX(SVM_EXIT_STGI),
4113 [x86_intercept_clgi] = POST_EX(SVM_EXIT_CLGI),
4114 [x86_intercept_skinit] = POST_EX(SVM_EXIT_SKINIT),
4115 [x86_intercept_invlpga] = POST_EX(SVM_EXIT_INVLPGA),
4116 [x86_intercept_rdtscp] = POST_EX(SVM_EXIT_RDTSCP),
4117 [x86_intercept_monitor] = POST_MEM(SVM_EXIT_MONITOR),
4118 [x86_intercept_mwait] = POST_EX(SVM_EXIT_MWAIT),
4119 [x86_intercept_invlpg] = POST_EX(SVM_EXIT_INVLPG),
4120 [x86_intercept_invd] = POST_EX(SVM_EXIT_INVD),
4121 [x86_intercept_wbinvd] = POST_EX(SVM_EXIT_WBINVD),
4122 [x86_intercept_wrmsr] = POST_EX(SVM_EXIT_MSR),
4123 [x86_intercept_rdtsc] = POST_EX(SVM_EXIT_RDTSC),
4124 [x86_intercept_rdmsr] = POST_EX(SVM_EXIT_MSR),
4125 [x86_intercept_rdpmc] = POST_EX(SVM_EXIT_RDPMC),
4126 [x86_intercept_cpuid] = PRE_EX(SVM_EXIT_CPUID),
4127 [x86_intercept_rsm] = PRE_EX(SVM_EXIT_RSM),
4128 [x86_intercept_pause] = PRE_EX(SVM_EXIT_PAUSE),
4129 [x86_intercept_pushf] = PRE_EX(SVM_EXIT_PUSHF),
4130 [x86_intercept_popf] = PRE_EX(SVM_EXIT_POPF),
4131 [x86_intercept_intn] = PRE_EX(SVM_EXIT_SWINT),
4132 [x86_intercept_iret] = PRE_EX(SVM_EXIT_IRET),
4133 [x86_intercept_icebp] = PRE_EX(SVM_EXIT_ICEBP),
4134 [x86_intercept_hlt] = POST_EX(SVM_EXIT_HLT),
4135 [x86_intercept_in] = POST_EX(SVM_EXIT_IOIO),
4136 [x86_intercept_ins] = POST_EX(SVM_EXIT_IOIO),
4137 [x86_intercept_out] = POST_EX(SVM_EXIT_IOIO),
4138 [x86_intercept_outs] = POST_EX(SVM_EXIT_IOIO),
4139 [x86_intercept_xsetbv] = PRE_EX(SVM_EXIT_XSETBV),
4140 };
4141
4142 #undef PRE_EX
4143 #undef POST_EX
4144 #undef POST_MEM
4145
svm_check_intercept(struct kvm_vcpu * vcpu,struct x86_instruction_info * info,enum x86_intercept_stage stage,struct x86_exception * exception)4146 static int svm_check_intercept(struct kvm_vcpu *vcpu,
4147 struct x86_instruction_info *info,
4148 enum x86_intercept_stage stage,
4149 struct x86_exception *exception)
4150 {
4151 struct vcpu_svm *svm = to_svm(vcpu);
4152 int vmexit, ret = X86EMUL_CONTINUE;
4153 struct __x86_intercept icpt_info;
4154 struct vmcb *vmcb = svm->vmcb;
4155
4156 if (info->intercept >= ARRAY_SIZE(x86_intercept_map))
4157 goto out;
4158
4159 icpt_info = x86_intercept_map[info->intercept];
4160
4161 if (stage != icpt_info.stage)
4162 goto out;
4163
4164 switch (icpt_info.exit_code) {
4165 case SVM_EXIT_READ_CR0:
4166 if (info->intercept == x86_intercept_cr_read)
4167 icpt_info.exit_code += info->modrm_reg;
4168 break;
4169 case SVM_EXIT_WRITE_CR0: {
4170 unsigned long cr0, val;
4171
4172 if (info->intercept == x86_intercept_cr_write)
4173 icpt_info.exit_code += info->modrm_reg;
4174
4175 if (icpt_info.exit_code != SVM_EXIT_WRITE_CR0 ||
4176 info->intercept == x86_intercept_clts)
4177 break;
4178
4179 if (!(vmcb12_is_intercept(&svm->nested.ctl,
4180 INTERCEPT_SELECTIVE_CR0)))
4181 break;
4182
4183 cr0 = vcpu->arch.cr0 & ~SVM_CR0_SELECTIVE_MASK;
4184 val = info->src_val & ~SVM_CR0_SELECTIVE_MASK;
4185
4186 if (info->intercept == x86_intercept_lmsw) {
4187 cr0 &= 0xfUL;
4188 val &= 0xfUL;
4189 /* lmsw can't clear PE - catch this here */
4190 if (cr0 & X86_CR0_PE)
4191 val |= X86_CR0_PE;
4192 }
4193
4194 if (cr0 ^ val)
4195 icpt_info.exit_code = SVM_EXIT_CR0_SEL_WRITE;
4196
4197 break;
4198 }
4199 case SVM_EXIT_READ_DR0:
4200 case SVM_EXIT_WRITE_DR0:
4201 icpt_info.exit_code += info->modrm_reg;
4202 break;
4203 case SVM_EXIT_MSR:
4204 if (info->intercept == x86_intercept_wrmsr)
4205 vmcb->control.exit_info_1 = 1;
4206 else
4207 vmcb->control.exit_info_1 = 0;
4208 break;
4209 case SVM_EXIT_PAUSE:
4210 /*
4211 * We get this for NOP only, but pause
4212 * is rep not, check this here
4213 */
4214 if (info->rep_prefix != REPE_PREFIX)
4215 goto out;
4216 break;
4217 case SVM_EXIT_IOIO: {
4218 u64 exit_info;
4219 u32 bytes;
4220
4221 if (info->intercept == x86_intercept_in ||
4222 info->intercept == x86_intercept_ins) {
4223 exit_info = ((info->src_val & 0xffff) << 16) |
4224 SVM_IOIO_TYPE_MASK;
4225 bytes = info->dst_bytes;
4226 } else {
4227 exit_info = (info->dst_val & 0xffff) << 16;
4228 bytes = info->src_bytes;
4229 }
4230
4231 if (info->intercept == x86_intercept_outs ||
4232 info->intercept == x86_intercept_ins)
4233 exit_info |= SVM_IOIO_STR_MASK;
4234
4235 if (info->rep_prefix)
4236 exit_info |= SVM_IOIO_REP_MASK;
4237
4238 bytes = min(bytes, 4u);
4239
4240 exit_info |= bytes << SVM_IOIO_SIZE_SHIFT;
4241
4242 exit_info |= (u32)info->ad_bytes << (SVM_IOIO_ASIZE_SHIFT - 1);
4243
4244 vmcb->control.exit_info_1 = exit_info;
4245 vmcb->control.exit_info_2 = info->next_rip;
4246
4247 break;
4248 }
4249 default:
4250 break;
4251 }
4252
4253 /* TODO: Advertise NRIPS to guest hypervisor unconditionally */
4254 if (static_cpu_has(X86_FEATURE_NRIPS))
4255 vmcb->control.next_rip = info->next_rip;
4256 vmcb->control.exit_code = icpt_info.exit_code;
4257 vmexit = nested_svm_exit_handled(svm);
4258
4259 ret = (vmexit == NESTED_EXIT_DONE) ? X86EMUL_INTERCEPTED
4260 : X86EMUL_CONTINUE;
4261
4262 out:
4263 return ret;
4264 }
4265
svm_handle_exit_irqoff(struct kvm_vcpu * vcpu)4266 static void svm_handle_exit_irqoff(struct kvm_vcpu *vcpu)
4267 {
4268 if (to_svm(vcpu)->vmcb->control.exit_code == SVM_EXIT_INTR)
4269 vcpu->arch.at_instruction_boundary = true;
4270 }
4271
svm_sched_in(struct kvm_vcpu * vcpu,int cpu)4272 static void svm_sched_in(struct kvm_vcpu *vcpu, int cpu)
4273 {
4274 if (!kvm_pause_in_guest(vcpu->kvm))
4275 shrink_ple_window(vcpu);
4276 }
4277
svm_setup_mce(struct kvm_vcpu * vcpu)4278 static void svm_setup_mce(struct kvm_vcpu *vcpu)
4279 {
4280 /* [63:9] are reserved. */
4281 vcpu->arch.mcg_cap &= 0x1ff;
4282 }
4283
svm_smi_blocked(struct kvm_vcpu * vcpu)4284 bool svm_smi_blocked(struct kvm_vcpu *vcpu)
4285 {
4286 struct vcpu_svm *svm = to_svm(vcpu);
4287
4288 /* Per APM Vol.2 15.22.2 "Response to SMI" */
4289 if (!gif_set(svm))
4290 return true;
4291
4292 return is_smm(vcpu);
4293 }
4294
svm_smi_allowed(struct kvm_vcpu * vcpu,bool for_injection)4295 static int svm_smi_allowed(struct kvm_vcpu *vcpu, bool for_injection)
4296 {
4297 struct vcpu_svm *svm = to_svm(vcpu);
4298 if (svm->nested.nested_run_pending)
4299 return -EBUSY;
4300
4301 if (svm_smi_blocked(vcpu))
4302 return 0;
4303
4304 /* An SMI must not be injected into L2 if it's supposed to VM-Exit. */
4305 if (for_injection && is_guest_mode(vcpu) && nested_exit_on_smi(svm))
4306 return -EBUSY;
4307
4308 return 1;
4309 }
4310
svm_enter_smm(struct kvm_vcpu * vcpu,char * smstate)4311 static int svm_enter_smm(struct kvm_vcpu *vcpu, char *smstate)
4312 {
4313 struct vcpu_svm *svm = to_svm(vcpu);
4314 struct kvm_host_map map_save;
4315 int ret;
4316
4317 if (!is_guest_mode(vcpu))
4318 return 0;
4319
4320 /* FED8h - SVM Guest */
4321 put_smstate(u64, smstate, 0x7ed8, 1);
4322 /* FEE0h - SVM Guest VMCB Physical Address */
4323 put_smstate(u64, smstate, 0x7ee0, svm->nested.vmcb12_gpa);
4324
4325 svm->vmcb->save.rax = vcpu->arch.regs[VCPU_REGS_RAX];
4326 svm->vmcb->save.rsp = vcpu->arch.regs[VCPU_REGS_RSP];
4327 svm->vmcb->save.rip = vcpu->arch.regs[VCPU_REGS_RIP];
4328
4329 ret = nested_svm_simple_vmexit(svm, SVM_EXIT_SW);
4330 if (ret)
4331 return ret;
4332
4333 /*
4334 * KVM uses VMCB01 to store L1 host state while L2 runs but
4335 * VMCB01 is going to be used during SMM and thus the state will
4336 * be lost. Temporary save non-VMLOAD/VMSAVE state to the host save
4337 * area pointed to by MSR_VM_HSAVE_PA. APM guarantees that the
4338 * format of the area is identical to guest save area offsetted
4339 * by 0x400 (matches the offset of 'struct vmcb_save_area'
4340 * within 'struct vmcb'). Note: HSAVE area may also be used by
4341 * L1 hypervisor to save additional host context (e.g. KVM does
4342 * that, see svm_prepare_switch_to_guest()) which must be
4343 * preserved.
4344 */
4345 if (kvm_vcpu_map(vcpu, gpa_to_gfn(svm->nested.hsave_msr),
4346 &map_save) == -EINVAL)
4347 return 1;
4348
4349 BUILD_BUG_ON(offsetof(struct vmcb, save) != 0x400);
4350
4351 svm_copy_vmrun_state(map_save.hva + 0x400,
4352 &svm->vmcb01.ptr->save);
4353
4354 kvm_vcpu_unmap(vcpu, &map_save, true);
4355 return 0;
4356 }
4357
svm_leave_smm(struct kvm_vcpu * vcpu,const char * smstate)4358 static int svm_leave_smm(struct kvm_vcpu *vcpu, const char *smstate)
4359 {
4360 struct vcpu_svm *svm = to_svm(vcpu);
4361 struct kvm_host_map map, map_save;
4362 u64 saved_efer, vmcb12_gpa;
4363 struct vmcb *vmcb12;
4364 int ret;
4365
4366 if (!guest_cpuid_has(vcpu, X86_FEATURE_LM))
4367 return 0;
4368
4369 /* Non-zero if SMI arrived while vCPU was in guest mode. */
4370 if (!GET_SMSTATE(u64, smstate, 0x7ed8))
4371 return 0;
4372
4373 if (!guest_cpuid_has(vcpu, X86_FEATURE_SVM))
4374 return 1;
4375
4376 saved_efer = GET_SMSTATE(u64, smstate, 0x7ed0);
4377 if (!(saved_efer & EFER_SVME))
4378 return 1;
4379
4380 vmcb12_gpa = GET_SMSTATE(u64, smstate, 0x7ee0);
4381 if (kvm_vcpu_map(vcpu, gpa_to_gfn(vmcb12_gpa), &map) == -EINVAL)
4382 return 1;
4383
4384 ret = 1;
4385 if (kvm_vcpu_map(vcpu, gpa_to_gfn(svm->nested.hsave_msr), &map_save) == -EINVAL)
4386 goto unmap_map;
4387
4388 if (svm_allocate_nested(svm))
4389 goto unmap_save;
4390
4391 /*
4392 * Restore L1 host state from L1 HSAVE area as VMCB01 was
4393 * used during SMM (see svm_enter_smm())
4394 */
4395
4396 svm_copy_vmrun_state(&svm->vmcb01.ptr->save, map_save.hva + 0x400);
4397
4398 /*
4399 * Enter the nested guest now
4400 */
4401
4402 vmcb_mark_all_dirty(svm->vmcb01.ptr);
4403
4404 vmcb12 = map.hva;
4405 nested_copy_vmcb_control_to_cache(svm, &vmcb12->control);
4406 nested_copy_vmcb_save_to_cache(svm, &vmcb12->save);
4407 ret = enter_svm_guest_mode(vcpu, vmcb12_gpa, vmcb12, false);
4408
4409 if (ret)
4410 goto unmap_save;
4411
4412 svm->nested.nested_run_pending = 1;
4413
4414 unmap_save:
4415 kvm_vcpu_unmap(vcpu, &map_save, true);
4416 unmap_map:
4417 kvm_vcpu_unmap(vcpu, &map, true);
4418 return ret;
4419 }
4420
svm_enable_smi_window(struct kvm_vcpu * vcpu)4421 static void svm_enable_smi_window(struct kvm_vcpu *vcpu)
4422 {
4423 struct vcpu_svm *svm = to_svm(vcpu);
4424
4425 if (!gif_set(svm)) {
4426 if (vgif)
4427 svm_set_intercept(svm, INTERCEPT_STGI);
4428 /* STGI will cause a vm exit */
4429 } else {
4430 /* We must be in SMM; RSM will cause a vmexit anyway. */
4431 }
4432 }
4433
svm_can_emulate_instruction(struct kvm_vcpu * vcpu,int emul_type,void * insn,int insn_len)4434 static bool svm_can_emulate_instruction(struct kvm_vcpu *vcpu, int emul_type,
4435 void *insn, int insn_len)
4436 {
4437 bool smep, smap, is_user;
4438 unsigned long cr4;
4439 u64 error_code;
4440
4441 /* Emulation is always possible when KVM has access to all guest state. */
4442 if (!sev_guest(vcpu->kvm))
4443 return true;
4444
4445 /* #UD and #GP should never be intercepted for SEV guests. */
4446 WARN_ON_ONCE(emul_type & (EMULTYPE_TRAP_UD |
4447 EMULTYPE_TRAP_UD_FORCED |
4448 EMULTYPE_VMWARE_GP));
4449
4450 /*
4451 * Emulation is impossible for SEV-ES guests as KVM doesn't have access
4452 * to guest register state.
4453 */
4454 if (sev_es_guest(vcpu->kvm))
4455 return false;
4456
4457 /*
4458 * Emulation is possible if the instruction is already decoded, e.g.
4459 * when completing I/O after returning from userspace.
4460 */
4461 if (emul_type & EMULTYPE_NO_DECODE)
4462 return true;
4463
4464 /*
4465 * Emulation is possible for SEV guests if and only if a prefilled
4466 * buffer containing the bytes of the intercepted instruction is
4467 * available. SEV guest memory is encrypted with a guest specific key
4468 * and cannot be decrypted by KVM, i.e. KVM would read cyphertext and
4469 * decode garbage.
4470 *
4471 * Inject #UD if KVM reached this point without an instruction buffer.
4472 * In practice, this path should never be hit by a well-behaved guest,
4473 * e.g. KVM doesn't intercept #UD or #GP for SEV guests, but this path
4474 * is still theoretically reachable, e.g. via unaccelerated fault-like
4475 * AVIC access, and needs to be handled by KVM to avoid putting the
4476 * guest into an infinite loop. Injecting #UD is somewhat arbitrary,
4477 * but its the least awful option given lack of insight into the guest.
4478 */
4479 if (unlikely(!insn)) {
4480 kvm_queue_exception(vcpu, UD_VECTOR);
4481 return false;
4482 }
4483
4484 /*
4485 * Emulate for SEV guests if the insn buffer is not empty. The buffer
4486 * will be empty if the DecodeAssist microcode cannot fetch bytes for
4487 * the faulting instruction because the code fetch itself faulted, e.g.
4488 * the guest attempted to fetch from emulated MMIO or a guest page
4489 * table used to translate CS:RIP resides in emulated MMIO.
4490 */
4491 if (likely(insn_len))
4492 return true;
4493
4494 /*
4495 * Detect and workaround Errata 1096 Fam_17h_00_0Fh.
4496 *
4497 * Errata:
4498 * When CPU raises #NPF on guest data access and vCPU CR4.SMAP=1, it is
4499 * possible that CPU microcode implementing DecodeAssist will fail to
4500 * read guest memory at CS:RIP and vmcb.GuestIntrBytes will incorrectly
4501 * be '0'. This happens because microcode reads CS:RIP using a _data_
4502 * loap uop with CPL=0 privileges. If the load hits a SMAP #PF, ucode
4503 * gives up and does not fill the instruction bytes buffer.
4504 *
4505 * As above, KVM reaches this point iff the VM is an SEV guest, the CPU
4506 * supports DecodeAssist, a #NPF was raised, KVM's page fault handler
4507 * triggered emulation (e.g. for MMIO), and the CPU returned 0 in the
4508 * GuestIntrBytes field of the VMCB.
4509 *
4510 * This does _not_ mean that the erratum has been encountered, as the
4511 * DecodeAssist will also fail if the load for CS:RIP hits a legitimate
4512 * #PF, e.g. if the guest attempt to execute from emulated MMIO and
4513 * encountered a reserved/not-present #PF.
4514 *
4515 * To hit the erratum, the following conditions must be true:
4516 * 1. CR4.SMAP=1 (obviously).
4517 * 2. CR4.SMEP=0 || CPL=3. If SMEP=1 and CPL<3, the erratum cannot
4518 * have been hit as the guest would have encountered a SMEP
4519 * violation #PF, not a #NPF.
4520 * 3. The #NPF is not due to a code fetch, in which case failure to
4521 * retrieve the instruction bytes is legitimate (see abvoe).
4522 *
4523 * In addition, don't apply the erratum workaround if the #NPF occurred
4524 * while translating guest page tables (see below).
4525 */
4526 error_code = to_svm(vcpu)->vmcb->control.exit_info_1;
4527 if (error_code & (PFERR_GUEST_PAGE_MASK | PFERR_FETCH_MASK))
4528 goto resume_guest;
4529
4530 cr4 = kvm_read_cr4(vcpu);
4531 smep = cr4 & X86_CR4_SMEP;
4532 smap = cr4 & X86_CR4_SMAP;
4533 is_user = svm_get_cpl(vcpu) == 3;
4534 if (smap && (!smep || is_user)) {
4535 pr_err_ratelimited("KVM: SEV Guest triggered AMD Erratum 1096\n");
4536
4537 /*
4538 * If the fault occurred in userspace, arbitrarily inject #GP
4539 * to avoid killing the guest and to hopefully avoid confusing
4540 * the guest kernel too much, e.g. injecting #PF would not be
4541 * coherent with respect to the guest's page tables. Request
4542 * triple fault if the fault occurred in the kernel as there's
4543 * no fault that KVM can inject without confusing the guest.
4544 * In practice, the triple fault is moot as no sane SEV kernel
4545 * will execute from user memory while also running with SMAP=1.
4546 */
4547 if (is_user)
4548 kvm_inject_gp(vcpu, 0);
4549 else
4550 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
4551 }
4552
4553 resume_guest:
4554 /*
4555 * If the erratum was not hit, simply resume the guest and let it fault
4556 * again. While awful, e.g. the vCPU may get stuck in an infinite loop
4557 * if the fault is at CPL=0, it's the lesser of all evils. Exiting to
4558 * userspace will kill the guest, and letting the emulator read garbage
4559 * will yield random behavior and potentially corrupt the guest.
4560 *
4561 * Simply resuming the guest is technically not a violation of the SEV
4562 * architecture. AMD's APM states that all code fetches and page table
4563 * accesses for SEV guest are encrypted, regardless of the C-Bit. The
4564 * APM also states that encrypted accesses to MMIO are "ignored", but
4565 * doesn't explicitly define "ignored", i.e. doing nothing and letting
4566 * the guest spin is technically "ignoring" the access.
4567 */
4568 return false;
4569 }
4570
svm_apic_init_signal_blocked(struct kvm_vcpu * vcpu)4571 static bool svm_apic_init_signal_blocked(struct kvm_vcpu *vcpu)
4572 {
4573 struct vcpu_svm *svm = to_svm(vcpu);
4574
4575 /*
4576 * TODO: Last condition latch INIT signals on vCPU when
4577 * vCPU is in guest-mode and vmcb12 defines intercept on INIT.
4578 * To properly emulate the INIT intercept,
4579 * svm_check_nested_events() should call nested_svm_vmexit()
4580 * if an INIT signal is pending.
4581 */
4582 return !gif_set(svm) ||
4583 (vmcb_is_intercept(&svm->vmcb->control, INTERCEPT_INIT));
4584 }
4585
svm_vcpu_deliver_sipi_vector(struct kvm_vcpu * vcpu,u8 vector)4586 static void svm_vcpu_deliver_sipi_vector(struct kvm_vcpu *vcpu, u8 vector)
4587 {
4588 if (!sev_es_guest(vcpu->kvm))
4589 return kvm_vcpu_deliver_sipi_vector(vcpu, vector);
4590
4591 sev_vcpu_deliver_sipi_vector(vcpu, vector);
4592 }
4593
svm_vm_destroy(struct kvm * kvm)4594 static void svm_vm_destroy(struct kvm *kvm)
4595 {
4596 avic_vm_destroy(kvm);
4597 sev_vm_destroy(kvm);
4598 }
4599
svm_vm_init(struct kvm * kvm)4600 static int svm_vm_init(struct kvm *kvm)
4601 {
4602 if (!pause_filter_count || !pause_filter_thresh)
4603 kvm->arch.pause_in_guest = true;
4604
4605 if (enable_apicv) {
4606 int ret = avic_vm_init(kvm);
4607 if (ret)
4608 return ret;
4609 }
4610
4611 return 0;
4612 }
4613
4614 static struct kvm_x86_ops svm_x86_ops __initdata = {
4615 .name = "kvm_amd",
4616
4617 .hardware_unsetup = svm_hardware_unsetup,
4618 .hardware_enable = svm_hardware_enable,
4619 .hardware_disable = svm_hardware_disable,
4620 .has_emulated_msr = svm_has_emulated_msr,
4621
4622 .vcpu_create = svm_vcpu_create,
4623 .vcpu_free = svm_vcpu_free,
4624 .vcpu_reset = svm_vcpu_reset,
4625
4626 .vm_size = sizeof(struct kvm_svm),
4627 .vm_init = svm_vm_init,
4628 .vm_destroy = svm_vm_destroy,
4629
4630 .prepare_switch_to_guest = svm_prepare_switch_to_guest,
4631 .vcpu_load = svm_vcpu_load,
4632 .vcpu_put = svm_vcpu_put,
4633 .vcpu_blocking = avic_vcpu_blocking,
4634 .vcpu_unblocking = avic_vcpu_unblocking,
4635
4636 .update_exception_bitmap = svm_update_exception_bitmap,
4637 .get_msr_feature = svm_get_msr_feature,
4638 .get_msr = svm_get_msr,
4639 .set_msr = svm_set_msr,
4640 .get_segment_base = svm_get_segment_base,
4641 .get_segment = svm_get_segment,
4642 .set_segment = svm_set_segment,
4643 .get_cpl = svm_get_cpl,
4644 .get_cs_db_l_bits = svm_get_cs_db_l_bits,
4645 .set_cr0 = svm_set_cr0,
4646 .post_set_cr3 = sev_post_set_cr3,
4647 .is_valid_cr4 = svm_is_valid_cr4,
4648 .set_cr4 = svm_set_cr4,
4649 .set_efer = svm_set_efer,
4650 .get_idt = svm_get_idt,
4651 .set_idt = svm_set_idt,
4652 .get_gdt = svm_get_gdt,
4653 .set_gdt = svm_set_gdt,
4654 .set_dr7 = svm_set_dr7,
4655 .sync_dirty_debug_regs = svm_sync_dirty_debug_regs,
4656 .cache_reg = svm_cache_reg,
4657 .get_rflags = svm_get_rflags,
4658 .set_rflags = svm_set_rflags,
4659 .get_if_flag = svm_get_if_flag,
4660
4661 .flush_tlb_all = svm_flush_tlb_current,
4662 .flush_tlb_current = svm_flush_tlb_current,
4663 .flush_tlb_gva = svm_flush_tlb_gva,
4664 .flush_tlb_guest = svm_flush_tlb_current,
4665
4666 .vcpu_pre_run = svm_vcpu_pre_run,
4667 .vcpu_run = svm_vcpu_run,
4668 .handle_exit = svm_handle_exit,
4669 .skip_emulated_instruction = svm_skip_emulated_instruction,
4670 .update_emulated_instruction = NULL,
4671 .set_interrupt_shadow = svm_set_interrupt_shadow,
4672 .get_interrupt_shadow = svm_get_interrupt_shadow,
4673 .patch_hypercall = svm_patch_hypercall,
4674 .inject_irq = svm_inject_irq,
4675 .inject_nmi = svm_inject_nmi,
4676 .queue_exception = svm_queue_exception,
4677 .cancel_injection = svm_cancel_injection,
4678 .interrupt_allowed = svm_interrupt_allowed,
4679 .nmi_allowed = svm_nmi_allowed,
4680 .get_nmi_mask = svm_get_nmi_mask,
4681 .set_nmi_mask = svm_set_nmi_mask,
4682 .enable_nmi_window = svm_enable_nmi_window,
4683 .enable_irq_window = svm_enable_irq_window,
4684 .update_cr8_intercept = svm_update_cr8_intercept,
4685 .refresh_apicv_exec_ctrl = avic_refresh_apicv_exec_ctrl,
4686 .check_apicv_inhibit_reasons = avic_check_apicv_inhibit_reasons,
4687 .apicv_post_state_restore = avic_apicv_post_state_restore,
4688
4689 .get_mt_mask = svm_get_mt_mask,
4690 .get_exit_info = svm_get_exit_info,
4691
4692 .vcpu_after_set_cpuid = svm_vcpu_after_set_cpuid,
4693
4694 .has_wbinvd_exit = svm_has_wbinvd_exit,
4695
4696 .get_l2_tsc_offset = svm_get_l2_tsc_offset,
4697 .get_l2_tsc_multiplier = svm_get_l2_tsc_multiplier,
4698 .write_tsc_offset = svm_write_tsc_offset,
4699 .write_tsc_multiplier = svm_write_tsc_multiplier,
4700
4701 .load_mmu_pgd = svm_load_mmu_pgd,
4702
4703 .check_intercept = svm_check_intercept,
4704 .handle_exit_irqoff = svm_handle_exit_irqoff,
4705
4706 .request_immediate_exit = __kvm_request_immediate_exit,
4707
4708 .sched_in = svm_sched_in,
4709
4710 .nested_ops = &svm_nested_ops,
4711
4712 .deliver_interrupt = svm_deliver_interrupt,
4713 .pi_update_irte = avic_pi_update_irte,
4714 .setup_mce = svm_setup_mce,
4715
4716 .smi_allowed = svm_smi_allowed,
4717 .enter_smm = svm_enter_smm,
4718 .leave_smm = svm_leave_smm,
4719 .enable_smi_window = svm_enable_smi_window,
4720
4721 .mem_enc_ioctl = sev_mem_enc_ioctl,
4722 .mem_enc_register_region = sev_mem_enc_register_region,
4723 .mem_enc_unregister_region = sev_mem_enc_unregister_region,
4724 .guest_memory_reclaimed = sev_guest_memory_reclaimed,
4725
4726 .vm_copy_enc_context_from = sev_vm_copy_enc_context_from,
4727 .vm_move_enc_context_from = sev_vm_move_enc_context_from,
4728
4729 .can_emulate_instruction = svm_can_emulate_instruction,
4730
4731 .apic_init_signal_blocked = svm_apic_init_signal_blocked,
4732
4733 .msr_filter_changed = svm_msr_filter_changed,
4734 .complete_emulated_msr = svm_complete_emulated_msr,
4735
4736 .vcpu_deliver_sipi_vector = svm_vcpu_deliver_sipi_vector,
4737 .vcpu_get_apicv_inhibit_reasons = avic_vcpu_get_apicv_inhibit_reasons,
4738 };
4739
4740 /*
4741 * The default MMIO mask is a single bit (excluding the present bit),
4742 * which could conflict with the memory encryption bit. Check for
4743 * memory encryption support and override the default MMIO mask if
4744 * memory encryption is enabled.
4745 */
svm_adjust_mmio_mask(void)4746 static __init void svm_adjust_mmio_mask(void)
4747 {
4748 unsigned int enc_bit, mask_bit;
4749 u64 msr, mask;
4750
4751 /* If there is no memory encryption support, use existing mask */
4752 if (cpuid_eax(0x80000000) < 0x8000001f)
4753 return;
4754
4755 /* If memory encryption is not enabled, use existing mask */
4756 rdmsrl(MSR_AMD64_SYSCFG, msr);
4757 if (!(msr & MSR_AMD64_SYSCFG_MEM_ENCRYPT))
4758 return;
4759
4760 enc_bit = cpuid_ebx(0x8000001f) & 0x3f;
4761 mask_bit = boot_cpu_data.x86_phys_bits;
4762
4763 /* Increment the mask bit if it is the same as the encryption bit */
4764 if (enc_bit == mask_bit)
4765 mask_bit++;
4766
4767 /*
4768 * If the mask bit location is below 52, then some bits above the
4769 * physical addressing limit will always be reserved, so use the
4770 * rsvd_bits() function to generate the mask. This mask, along with
4771 * the present bit, will be used to generate a page fault with
4772 * PFER.RSV = 1.
4773 *
4774 * If the mask bit location is 52 (or above), then clear the mask.
4775 */
4776 mask = (mask_bit < 52) ? rsvd_bits(mask_bit, 51) | PT_PRESENT_MASK : 0;
4777
4778 kvm_mmu_set_mmio_spte_mask(mask, mask, PT_WRITABLE_MASK | PT_USER_MASK);
4779 }
4780
svm_set_cpu_caps(void)4781 static __init void svm_set_cpu_caps(void)
4782 {
4783 kvm_set_cpu_caps();
4784
4785 supported_xss = 0;
4786
4787 /* CPUID 0x80000001 and 0x8000000A (SVM features) */
4788 if (nested) {
4789 kvm_cpu_cap_set(X86_FEATURE_SVM);
4790 kvm_cpu_cap_set(X86_FEATURE_VMCBCLEAN);
4791
4792 if (nrips)
4793 kvm_cpu_cap_set(X86_FEATURE_NRIPS);
4794
4795 if (npt_enabled)
4796 kvm_cpu_cap_set(X86_FEATURE_NPT);
4797
4798 if (tsc_scaling)
4799 kvm_cpu_cap_set(X86_FEATURE_TSCRATEMSR);
4800
4801 if (vls)
4802 kvm_cpu_cap_set(X86_FEATURE_V_VMSAVE_VMLOAD);
4803 if (lbrv)
4804 kvm_cpu_cap_set(X86_FEATURE_LBRV);
4805
4806 if (boot_cpu_has(X86_FEATURE_PAUSEFILTER))
4807 kvm_cpu_cap_set(X86_FEATURE_PAUSEFILTER);
4808
4809 if (boot_cpu_has(X86_FEATURE_PFTHRESHOLD))
4810 kvm_cpu_cap_set(X86_FEATURE_PFTHRESHOLD);
4811
4812 if (vgif)
4813 kvm_cpu_cap_set(X86_FEATURE_VGIF);
4814
4815 /* Nested VM can receive #VMEXIT instead of triggering #GP */
4816 kvm_cpu_cap_set(X86_FEATURE_SVME_ADDR_CHK);
4817 }
4818
4819 /* CPUID 0x80000008 */
4820 if (boot_cpu_has(X86_FEATURE_LS_CFG_SSBD) ||
4821 boot_cpu_has(X86_FEATURE_AMD_SSBD))
4822 kvm_cpu_cap_set(X86_FEATURE_VIRT_SSBD);
4823
4824 /* AMD PMU PERFCTR_CORE CPUID */
4825 if (enable_pmu && boot_cpu_has(X86_FEATURE_PERFCTR_CORE))
4826 kvm_cpu_cap_set(X86_FEATURE_PERFCTR_CORE);
4827
4828 /* CPUID 0x8000001F (SME/SEV features) */
4829 sev_set_cpu_caps();
4830 }
4831
svm_hardware_setup(void)4832 static __init int svm_hardware_setup(void)
4833 {
4834 int cpu;
4835 struct page *iopm_pages;
4836 void *iopm_va;
4837 int r;
4838 unsigned int order = get_order(IOPM_SIZE);
4839
4840 /*
4841 * NX is required for shadow paging and for NPT if the NX huge pages
4842 * mitigation is enabled.
4843 */
4844 if (!boot_cpu_has(X86_FEATURE_NX)) {
4845 pr_err_ratelimited("NX (Execute Disable) not supported\n");
4846 return -EOPNOTSUPP;
4847 }
4848 kvm_enable_efer_bits(EFER_NX);
4849
4850 iopm_pages = alloc_pages(GFP_KERNEL, order);
4851
4852 if (!iopm_pages)
4853 return -ENOMEM;
4854
4855 iopm_va = page_address(iopm_pages);
4856 memset(iopm_va, 0xff, PAGE_SIZE * (1 << order));
4857 iopm_base = page_to_pfn(iopm_pages) << PAGE_SHIFT;
4858
4859 init_msrpm_offsets();
4860
4861 supported_xcr0 &= ~(XFEATURE_MASK_BNDREGS | XFEATURE_MASK_BNDCSR);
4862
4863 if (boot_cpu_has(X86_FEATURE_FXSR_OPT))
4864 kvm_enable_efer_bits(EFER_FFXSR);
4865
4866 if (tsc_scaling) {
4867 if (!boot_cpu_has(X86_FEATURE_TSCRATEMSR)) {
4868 tsc_scaling = false;
4869 } else {
4870 pr_info("TSC scaling supported\n");
4871 kvm_has_tsc_control = true;
4872 }
4873 }
4874 kvm_max_tsc_scaling_ratio = SVM_TSC_RATIO_MAX;
4875 kvm_tsc_scaling_ratio_frac_bits = 32;
4876
4877 tsc_aux_uret_slot = kvm_add_user_return_msr(MSR_TSC_AUX);
4878
4879 /* Check for pause filtering support */
4880 if (!boot_cpu_has(X86_FEATURE_PAUSEFILTER)) {
4881 pause_filter_count = 0;
4882 pause_filter_thresh = 0;
4883 } else if (!boot_cpu_has(X86_FEATURE_PFTHRESHOLD)) {
4884 pause_filter_thresh = 0;
4885 }
4886
4887 if (nested) {
4888 printk(KERN_INFO "kvm: Nested Virtualization enabled\n");
4889 kvm_enable_efer_bits(EFER_SVME | EFER_LMSLE);
4890 }
4891
4892 /*
4893 * KVM's MMU doesn't support using 2-level paging for itself, and thus
4894 * NPT isn't supported if the host is using 2-level paging since host
4895 * CR4 is unchanged on VMRUN.
4896 */
4897 if (!IS_ENABLED(CONFIG_X86_64) && !IS_ENABLED(CONFIG_X86_PAE))
4898 npt_enabled = false;
4899
4900 if (!boot_cpu_has(X86_FEATURE_NPT))
4901 npt_enabled = false;
4902
4903 /* Force VM NPT level equal to the host's paging level */
4904 kvm_configure_mmu(npt_enabled, get_npt_level(),
4905 get_npt_level(), PG_LEVEL_1G);
4906 pr_info("kvm: Nested Paging %sabled\n", npt_enabled ? "en" : "dis");
4907
4908 /* Setup shadow_me_value and shadow_me_mask */
4909 kvm_mmu_set_me_spte_mask(sme_me_mask, sme_me_mask);
4910
4911 svm_adjust_mmio_mask();
4912
4913 /*
4914 * Note, SEV setup consumes npt_enabled and enable_mmio_caching (which
4915 * may be modified by svm_adjust_mmio_mask()).
4916 */
4917 sev_hardware_setup();
4918
4919 svm_hv_hardware_setup();
4920
4921 for_each_possible_cpu(cpu) {
4922 r = svm_cpu_init(cpu);
4923 if (r)
4924 goto err;
4925 }
4926
4927 if (nrips) {
4928 if (!boot_cpu_has(X86_FEATURE_NRIPS))
4929 nrips = false;
4930 }
4931
4932 enable_apicv = avic = avic && npt_enabled && (boot_cpu_has(X86_FEATURE_AVIC) || force_avic);
4933
4934 if (enable_apicv) {
4935 if (!boot_cpu_has(X86_FEATURE_AVIC)) {
4936 pr_warn("AVIC is not supported in CPUID but force enabled");
4937 pr_warn("Your system might crash and burn");
4938 } else
4939 pr_info("AVIC enabled\n");
4940
4941 amd_iommu_register_ga_log_notifier(&avic_ga_log_notifier);
4942 } else {
4943 svm_x86_ops.vcpu_blocking = NULL;
4944 svm_x86_ops.vcpu_unblocking = NULL;
4945 svm_x86_ops.vcpu_get_apicv_inhibit_reasons = NULL;
4946 }
4947
4948 if (vls) {
4949 if (!npt_enabled ||
4950 !boot_cpu_has(X86_FEATURE_V_VMSAVE_VMLOAD) ||
4951 !IS_ENABLED(CONFIG_X86_64)) {
4952 vls = false;
4953 } else {
4954 pr_info("Virtual VMLOAD VMSAVE supported\n");
4955 }
4956 }
4957
4958 if (boot_cpu_has(X86_FEATURE_SVME_ADDR_CHK))
4959 svm_gp_erratum_intercept = false;
4960
4961 if (vgif) {
4962 if (!boot_cpu_has(X86_FEATURE_VGIF))
4963 vgif = false;
4964 else
4965 pr_info("Virtual GIF supported\n");
4966 }
4967
4968 if (lbrv) {
4969 if (!boot_cpu_has(X86_FEATURE_LBRV))
4970 lbrv = false;
4971 else
4972 pr_info("LBR virtualization supported\n");
4973 }
4974
4975 if (!enable_pmu)
4976 pr_info("PMU virtualization is disabled\n");
4977
4978 svm_set_cpu_caps();
4979
4980 /*
4981 * It seems that on AMD processors PTE's accessed bit is
4982 * being set by the CPU hardware before the NPF vmexit.
4983 * This is not expected behaviour and our tests fail because
4984 * of it.
4985 * A workaround here is to disable support for
4986 * GUEST_MAXPHYADDR < HOST_MAXPHYADDR if NPT is enabled.
4987 * In this case userspace can know if there is support using
4988 * KVM_CAP_SMALLER_MAXPHYADDR extension and decide how to handle
4989 * it
4990 * If future AMD CPU models change the behaviour described above,
4991 * this variable can be changed accordingly
4992 */
4993 allow_smaller_maxphyaddr = !npt_enabled;
4994
4995 return 0;
4996
4997 err:
4998 svm_hardware_unsetup();
4999 return r;
5000 }
5001
5002
5003 static struct kvm_x86_init_ops svm_init_ops __initdata = {
5004 .cpu_has_kvm_support = has_svm,
5005 .disabled_by_bios = is_disabled,
5006 .hardware_setup = svm_hardware_setup,
5007 .check_processor_compatibility = svm_check_processor_compat,
5008
5009 .runtime_ops = &svm_x86_ops,
5010 .pmu_ops = &amd_pmu_ops,
5011 };
5012
svm_init(void)5013 static int __init svm_init(void)
5014 {
5015 __unused_size_checks();
5016
5017 return kvm_init(&svm_init_ops, sizeof(struct vcpu_svm),
5018 __alignof__(struct vcpu_svm), THIS_MODULE);
5019 }
5020
svm_exit(void)5021 static void __exit svm_exit(void)
5022 {
5023 kvm_exit();
5024 }
5025
5026 module_init(svm_init)
5027 module_exit(svm_exit)
5028