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