]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blame - arch/x86/kvm/svm/svm.c
KVM: SVM: Clear the CR4 register on reset
[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{
0ede79e1 1891 u64 fault_address = __sme_clr(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;
2654 msr_info->data = svm->tsc_aux;
2655 break;
e0231715
JR
2656 /*
2657 * Nobody will change the following 5 values in the VMCB so we can
2658 * safely return them on rdmsr. They will always be 0 until LBRV is
2659 * implemented.
2660 */
a2938c80 2661 case MSR_IA32_DEBUGCTLMSR:
609e36d3 2662 msr_info->data = svm->vmcb->save.dbgctl;
a2938c80
JR
2663 break;
2664 case MSR_IA32_LASTBRANCHFROMIP:
609e36d3 2665 msr_info->data = svm->vmcb->save.br_from;
a2938c80
JR
2666 break;
2667 case MSR_IA32_LASTBRANCHTOIP:
609e36d3 2668 msr_info->data = svm->vmcb->save.br_to;
a2938c80
JR
2669 break;
2670 case MSR_IA32_LASTINTFROMIP:
609e36d3 2671 msr_info->data = svm->vmcb->save.last_excp_from;
a2938c80
JR
2672 break;
2673 case MSR_IA32_LASTINTTOIP:
609e36d3 2674 msr_info->data = svm->vmcb->save.last_excp_to;
a2938c80 2675 break;
b286d5d8 2676 case MSR_VM_HSAVE_PA:
609e36d3 2677 msr_info->data = svm->nested.hsave_msr;
b286d5d8 2678 break;
eb6f302e 2679 case MSR_VM_CR:
609e36d3 2680 msr_info->data = svm->nested.vm_cr_msr;
eb6f302e 2681 break;
b2ac58f9
KA
2682 case MSR_IA32_SPEC_CTRL:
2683 if (!msr_info->host_initiated &&
39485ed9 2684 !guest_has_spec_ctrl_msr(vcpu))
b2ac58f9
KA
2685 return 1;
2686
2687 msr_info->data = svm->spec_ctrl;
2688 break;
bc226f07
TL
2689 case MSR_AMD64_VIRT_SPEC_CTRL:
2690 if (!msr_info->host_initiated &&
2691 !guest_cpuid_has(vcpu, X86_FEATURE_VIRT_SSBD))
2692 return 1;
2693
2694 msr_info->data = svm->virt_spec_ctrl;
2695 break;
ae8b7875
BP
2696 case MSR_F15H_IC_CFG: {
2697
2698 int family, model;
2699
2700 family = guest_cpuid_family(vcpu);
2701 model = guest_cpuid_model(vcpu);
2702
2703 if (family < 0 || model < 0)
2704 return kvm_get_msr_common(vcpu, msr_info);
2705
2706 msr_info->data = 0;
2707
2708 if (family == 0x15 &&
2709 (model >= 0x2 && model < 0x20))
2710 msr_info->data = 0x1E;
2711 }
2712 break;
d1d93fa9
TL
2713 case MSR_F10H_DECFG:
2714 msr_info->data = svm->msr_decfg;
2715 break;
6aa8b732 2716 default:
609e36d3 2717 return kvm_get_msr_common(vcpu, msr_info);
6aa8b732
AK
2718 }
2719 return 0;
2720}
2721
f1c6366e
TL
2722static int svm_complete_emulated_msr(struct kvm_vcpu *vcpu, int err)
2723{
2724 struct vcpu_svm *svm = to_svm(vcpu);
2725 if (!sev_es_guest(svm->vcpu.kvm) || !err)
2726 return kvm_complete_insn_gp(&svm->vcpu, err);
2727
2728 ghcb_set_sw_exit_info_1(svm->ghcb, 1);
2729 ghcb_set_sw_exit_info_2(svm->ghcb,
2730 X86_TRAP_GP |
2731 SVM_EVTINJ_TYPE_EXEPT |
2732 SVM_EVTINJ_VALID);
2733 return 1;
2734}
2735
851ba692 2736static int rdmsr_interception(struct vcpu_svm *svm)
6aa8b732 2737{
1edce0a9 2738 return kvm_emulate_rdmsr(&svm->vcpu);
6aa8b732
AK
2739}
2740
4a810181
JR
2741static int svm_set_vm_cr(struct kvm_vcpu *vcpu, u64 data)
2742{
2743 struct vcpu_svm *svm = to_svm(vcpu);
2744 int svm_dis, chg_mask;
2745
2746 if (data & ~SVM_VM_CR_VALID_MASK)
2747 return 1;
2748
2749 chg_mask = SVM_VM_CR_VALID_MASK;
2750
2751 if (svm->nested.vm_cr_msr & SVM_VM_CR_SVM_DIS_MASK)
2752 chg_mask &= ~(SVM_VM_CR_SVM_LOCK_MASK | SVM_VM_CR_SVM_DIS_MASK);
2753
2754 svm->nested.vm_cr_msr &= ~chg_mask;
2755 svm->nested.vm_cr_msr |= (data & chg_mask);
2756
2757 svm_dis = svm->nested.vm_cr_msr & SVM_VM_CR_SVM_DIS_MASK;
2758
2759 /* check for svm_disable while efer.svme is set */
2760 if (svm_dis && (vcpu->arch.efer & EFER_SVME))
2761 return 1;
2762
2763 return 0;
2764}
2765
8fe8ab46 2766static int svm_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr)
6aa8b732 2767{
a2fa3e9f
GH
2768 struct vcpu_svm *svm = to_svm(vcpu);
2769
8fe8ab46
WA
2770 u32 ecx = msr->index;
2771 u64 data = msr->data;
6aa8b732 2772 switch (ecx) {
15038e14
PB
2773 case MSR_IA32_CR_PAT:
2774 if (!kvm_mtrr_valid(vcpu, MSR_IA32_CR_PAT, data))
2775 return 1;
2776 vcpu->arch.pat = data;
2777 svm->vmcb->save.g_pat = data;
06e7852c 2778 vmcb_mark_dirty(svm->vmcb, VMCB_NPT);
15038e14 2779 break;
b2ac58f9
KA
2780 case MSR_IA32_SPEC_CTRL:
2781 if (!msr->host_initiated &&
39485ed9 2782 !guest_has_spec_ctrl_msr(vcpu))
b2ac58f9
KA
2783 return 1;
2784
841c2be0 2785 if (kvm_spec_ctrl_test_value(data))
b2ac58f9
KA
2786 return 1;
2787
2788 svm->spec_ctrl = data;
b2ac58f9
KA
2789 if (!data)
2790 break;
2791
2792 /*
2793 * For non-nested:
2794 * When it's written (to non-zero) for the first time, pass
2795 * it through.
2796 *
2797 * For nested:
2798 * The handling of the MSR bitmap for L2 guests is done in
2799 * nested_svm_vmrun_msrpm.
2800 * We update the L1 MSR bit as well since it will end up
2801 * touching the MSR anyway now.
2802 */
476c9bd8 2803 set_msr_interception(vcpu, svm->msrpm, MSR_IA32_SPEC_CTRL, 1, 1);
b2ac58f9 2804 break;
15d45071
AR
2805 case MSR_IA32_PRED_CMD:
2806 if (!msr->host_initiated &&
39485ed9 2807 !guest_has_pred_cmd_msr(vcpu))
15d45071
AR
2808 return 1;
2809
2810 if (data & ~PRED_CMD_IBPB)
2811 return 1;
39485ed9 2812 if (!boot_cpu_has(X86_FEATURE_IBPB))
6441fa61 2813 return 1;
15d45071
AR
2814 if (!data)
2815 break;
2816
2817 wrmsrl(MSR_IA32_PRED_CMD, PRED_CMD_IBPB);
476c9bd8 2818 set_msr_interception(vcpu, svm->msrpm, MSR_IA32_PRED_CMD, 0, 1);
15d45071 2819 break;
bc226f07
TL
2820 case MSR_AMD64_VIRT_SPEC_CTRL:
2821 if (!msr->host_initiated &&
2822 !guest_cpuid_has(vcpu, X86_FEATURE_VIRT_SSBD))
2823 return 1;
2824
2825 if (data & ~SPEC_CTRL_SSBD)
2826 return 1;
2827
2828 svm->virt_spec_ctrl = data;
2829 break;
8c06585d 2830 case MSR_STAR:
a2fa3e9f 2831 svm->vmcb->save.star = data;
6aa8b732 2832 break;
49b14f24 2833#ifdef CONFIG_X86_64
6aa8b732 2834 case MSR_LSTAR:
a2fa3e9f 2835 svm->vmcb->save.lstar = data;
6aa8b732
AK
2836 break;
2837 case MSR_CSTAR:
a2fa3e9f 2838 svm->vmcb->save.cstar = data;
6aa8b732
AK
2839 break;
2840 case MSR_KERNEL_GS_BASE:
a2fa3e9f 2841 svm->vmcb->save.kernel_gs_base = data;
6aa8b732
AK
2842 break;
2843 case MSR_SYSCALL_MASK:
a2fa3e9f 2844 svm->vmcb->save.sfmask = data;
6aa8b732
AK
2845 break;
2846#endif
2847 case MSR_IA32_SYSENTER_CS:
a2fa3e9f 2848 svm->vmcb->save.sysenter_cs = data;
6aa8b732
AK
2849 break;
2850 case MSR_IA32_SYSENTER_EIP:
017cb99e 2851 svm->sysenter_eip = data;
a2fa3e9f 2852 svm->vmcb->save.sysenter_eip = data;
6aa8b732
AK
2853 break;
2854 case MSR_IA32_SYSENTER_ESP:
017cb99e 2855 svm->sysenter_esp = data;
a2fa3e9f 2856 svm->vmcb->save.sysenter_esp = data;
6aa8b732 2857 break;
46896c73
PB
2858 case MSR_TSC_AUX:
2859 if (!boot_cpu_has(X86_FEATURE_RDTSCP))
2860 return 1;
2861
2862 /*
2863 * This is rare, so we update the MSR here instead of using
2864 * direct_access_msrs. Doing that would require a rdmsr in
2865 * svm_vcpu_put.
2866 */
2867 svm->tsc_aux = data;
2868 wrmsrl(MSR_TSC_AUX, svm->tsc_aux);
2869 break;
a2938c80 2870 case MSR_IA32_DEBUGCTLMSR:
2a6b20b8 2871 if (!boot_cpu_has(X86_FEATURE_LBRV)) {
a737f256
CD
2872 vcpu_unimpl(vcpu, "%s: MSR_IA32_DEBUGCTL 0x%llx, nop\n",
2873 __func__, data);
24e09cbf
JR
2874 break;
2875 }
2876 if (data & DEBUGCTL_RESERVED_BITS)
2877 return 1;
2878
2879 svm->vmcb->save.dbgctl = data;
06e7852c 2880 vmcb_mark_dirty(svm->vmcb, VMCB_LBR);
24e09cbf 2881 if (data & (1ULL<<0))
476c9bd8 2882 svm_enable_lbrv(vcpu);
24e09cbf 2883 else
476c9bd8 2884 svm_disable_lbrv(vcpu);
a2938c80 2885 break;
b286d5d8 2886 case MSR_VM_HSAVE_PA:
e6aa9abd 2887 svm->nested.hsave_msr = data;
62b9abaa 2888 break;
3c5d0a44 2889 case MSR_VM_CR:
4a810181 2890 return svm_set_vm_cr(vcpu, data);
3c5d0a44 2891 case MSR_VM_IGNNE:
a737f256 2892 vcpu_unimpl(vcpu, "unimplemented wrmsr: 0x%x data 0x%llx\n", ecx, data);
3c5d0a44 2893 break;
d1d93fa9
TL
2894 case MSR_F10H_DECFG: {
2895 struct kvm_msr_entry msr_entry;
2896
2897 msr_entry.index = msr->index;
2898 if (svm_get_msr_feature(&msr_entry))
2899 return 1;
2900
2901 /* Check the supported bits */
2902 if (data & ~msr_entry.data)
2903 return 1;
2904
2905 /* Don't allow the guest to change a bit, #GP */
2906 if (!msr->host_initiated && (data ^ msr_entry.data))
2907 return 1;
2908
2909 svm->msr_decfg = data;
2910 break;
2911 }
44a95dae
SS
2912 case MSR_IA32_APICBASE:
2913 if (kvm_vcpu_apicv_active(vcpu))
2914 avic_update_vapic_bar(to_svm(vcpu), data);
df561f66 2915 fallthrough;
6aa8b732 2916 default:
8fe8ab46 2917 return kvm_set_msr_common(vcpu, msr);
6aa8b732
AK
2918 }
2919 return 0;
2920}
2921
851ba692 2922static int wrmsr_interception(struct vcpu_svm *svm)
6aa8b732 2923{
1edce0a9 2924 return kvm_emulate_wrmsr(&svm->vcpu);
6aa8b732
AK
2925}
2926
851ba692 2927static int msr_interception(struct vcpu_svm *svm)
6aa8b732 2928{
e756fc62 2929 if (svm->vmcb->control.exit_info_1)
851ba692 2930 return wrmsr_interception(svm);
6aa8b732 2931 else
851ba692 2932 return rdmsr_interception(svm);
6aa8b732
AK
2933}
2934
851ba692 2935static int interrupt_window_interception(struct vcpu_svm *svm)
c1150d8c 2936{
3842d135 2937 kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
f0b85051 2938 svm_clear_vintr(svm);
f3515dc3
SS
2939
2940 /*
2941 * For AVIC, the only reason to end up here is ExtINTs.
2942 * In this case AVIC was temporarily disabled for
2943 * requesting the IRQ window and we have to re-enable it.
2944 */
2945 svm_toggle_avic_for_irq_window(&svm->vcpu, true);
2946
675acb75 2947 ++svm->vcpu.stat.irq_window_exits;
c1150d8c
DL
2948 return 1;
2949}
2950
565d0998
ML
2951static int pause_interception(struct vcpu_svm *svm)
2952{
de63ad4c 2953 struct kvm_vcpu *vcpu = &svm->vcpu;
f1c6366e
TL
2954 bool in_kernel;
2955
2956 /*
2957 * CPL is not made available for an SEV-ES guest, therefore
2958 * vcpu->arch.preempted_in_kernel can never be true. Just
2959 * set in_kernel to false as well.
2960 */
2961 in_kernel = !sev_es_guest(svm->vcpu.kvm) && svm_get_cpl(vcpu) == 0;
de63ad4c 2962
830f01b0 2963 if (!kvm_pause_in_guest(vcpu->kvm))
8566ac8b
BM
2964 grow_ple_window(vcpu);
2965
de63ad4c 2966 kvm_vcpu_on_spin(vcpu, in_kernel);
565d0998
ML
2967 return 1;
2968}
2969
87c00572
GS
2970static int nop_interception(struct vcpu_svm *svm)
2971{
b742c1e6 2972 return kvm_skip_emulated_instruction(&(svm->vcpu));
87c00572
GS
2973}
2974
2975static int monitor_interception(struct vcpu_svm *svm)
2976{
2977 printk_once(KERN_WARNING "kvm: MONITOR instruction emulated as NOP!\n");
2978 return nop_interception(svm);
2979}
2980
2981static int mwait_interception(struct vcpu_svm *svm)
2982{
2983 printk_once(KERN_WARNING "kvm: MWAIT instruction emulated as NOP!\n");
2984 return nop_interception(svm);
2985}
2986
4407a797
BM
2987static int invpcid_interception(struct vcpu_svm *svm)
2988{
2989 struct kvm_vcpu *vcpu = &svm->vcpu;
2990 unsigned long type;
2991 gva_t gva;
2992
2993 if (!guest_cpuid_has(vcpu, X86_FEATURE_INVPCID)) {
2994 kvm_queue_exception(vcpu, UD_VECTOR);
2995 return 1;
2996 }
2997
2998 /*
2999 * For an INVPCID intercept:
3000 * EXITINFO1 provides the linear address of the memory operand.
3001 * EXITINFO2 provides the contents of the register operand.
3002 */
3003 type = svm->vmcb->control.exit_info_2;
3004 gva = svm->vmcb->control.exit_info_1;
3005
3006 if (type > 3) {
3007 kvm_inject_gp(vcpu, 0);
3008 return 1;
3009 }
3010
3011 return kvm_handle_invpcid(vcpu, type, gva);
3012}
3013
09941fbb 3014static int (*const svm_exit_handlers[])(struct vcpu_svm *svm) = {
7ff76d58
AP
3015 [SVM_EXIT_READ_CR0] = cr_interception,
3016 [SVM_EXIT_READ_CR3] = cr_interception,
3017 [SVM_EXIT_READ_CR4] = cr_interception,
3018 [SVM_EXIT_READ_CR8] = cr_interception,
5e57518d 3019 [SVM_EXIT_CR0_SEL_WRITE] = cr_interception,
628afd2a 3020 [SVM_EXIT_WRITE_CR0] = cr_interception,
7ff76d58
AP
3021 [SVM_EXIT_WRITE_CR3] = cr_interception,
3022 [SVM_EXIT_WRITE_CR4] = cr_interception,
e0231715 3023 [SVM_EXIT_WRITE_CR8] = cr8_write_interception,
cae3797a
AP
3024 [SVM_EXIT_READ_DR0] = dr_interception,
3025 [SVM_EXIT_READ_DR1] = dr_interception,
3026 [SVM_EXIT_READ_DR2] = dr_interception,
3027 [SVM_EXIT_READ_DR3] = dr_interception,
3028 [SVM_EXIT_READ_DR4] = dr_interception,
3029 [SVM_EXIT_READ_DR5] = dr_interception,
3030 [SVM_EXIT_READ_DR6] = dr_interception,
3031 [SVM_EXIT_READ_DR7] = dr_interception,
3032 [SVM_EXIT_WRITE_DR0] = dr_interception,
3033 [SVM_EXIT_WRITE_DR1] = dr_interception,
3034 [SVM_EXIT_WRITE_DR2] = dr_interception,
3035 [SVM_EXIT_WRITE_DR3] = dr_interception,
3036 [SVM_EXIT_WRITE_DR4] = dr_interception,
3037 [SVM_EXIT_WRITE_DR5] = dr_interception,
3038 [SVM_EXIT_WRITE_DR6] = dr_interception,
3039 [SVM_EXIT_WRITE_DR7] = dr_interception,
d0bfb940
JK
3040 [SVM_EXIT_EXCP_BASE + DB_VECTOR] = db_interception,
3041 [SVM_EXIT_EXCP_BASE + BP_VECTOR] = bp_interception,
7aa81cc0 3042 [SVM_EXIT_EXCP_BASE + UD_VECTOR] = ud_interception,
e0231715 3043 [SVM_EXIT_EXCP_BASE + PF_VECTOR] = pf_interception,
e0231715 3044 [SVM_EXIT_EXCP_BASE + MC_VECTOR] = mc_interception,
54a20552 3045 [SVM_EXIT_EXCP_BASE + AC_VECTOR] = ac_interception,
9718420e 3046 [SVM_EXIT_EXCP_BASE + GP_VECTOR] = gp_interception,
e0231715 3047 [SVM_EXIT_INTR] = intr_interception,
c47f098d 3048 [SVM_EXIT_NMI] = nmi_interception,
6aa8b732
AK
3049 [SVM_EXIT_SMI] = nop_on_interception,
3050 [SVM_EXIT_INIT] = nop_on_interception,
c1150d8c 3051 [SVM_EXIT_VINTR] = interrupt_window_interception,
332b56e4 3052 [SVM_EXIT_RDPMC] = rdpmc_interception,
6aa8b732 3053 [SVM_EXIT_CPUID] = cpuid_interception,
95ba8273 3054 [SVM_EXIT_IRET] = iret_interception,
4bb05f30 3055 [SVM_EXIT_INVD] = invd_interception,
565d0998 3056 [SVM_EXIT_PAUSE] = pause_interception,
6aa8b732 3057 [SVM_EXIT_HLT] = halt_interception,
a7052897 3058 [SVM_EXIT_INVLPG] = invlpg_interception,
ff092385 3059 [SVM_EXIT_INVLPGA] = invlpga_interception,
e0231715 3060 [SVM_EXIT_IOIO] = io_interception,
6aa8b732
AK
3061 [SVM_EXIT_MSR] = msr_interception,
3062 [SVM_EXIT_TASK_SWITCH] = task_switch_interception,
46fe4ddd 3063 [SVM_EXIT_SHUTDOWN] = shutdown_interception,
3d6368ef 3064 [SVM_EXIT_VMRUN] = vmrun_interception,
02e235bc 3065 [SVM_EXIT_VMMCALL] = vmmcall_interception,
5542675b
AG
3066 [SVM_EXIT_VMLOAD] = vmload_interception,
3067 [SVM_EXIT_VMSAVE] = vmsave_interception,
1371d904
AG
3068 [SVM_EXIT_STGI] = stgi_interception,
3069 [SVM_EXIT_CLGI] = clgi_interception,
532a46b9 3070 [SVM_EXIT_SKINIT] = skinit_interception,
dab429a7 3071 [SVM_EXIT_WBINVD] = wbinvd_interception,
87c00572
GS
3072 [SVM_EXIT_MONITOR] = monitor_interception,
3073 [SVM_EXIT_MWAIT] = mwait_interception,
81dd35d4 3074 [SVM_EXIT_XSETBV] = xsetbv_interception,
0cb8410b 3075 [SVM_EXIT_RDPRU] = rdpru_interception,
2985afbc 3076 [SVM_EXIT_EFER_WRITE_TRAP] = efer_trap,
f27ad38a 3077 [SVM_EXIT_CR0_WRITE_TRAP] = cr_trap,
5b51cb13 3078 [SVM_EXIT_CR4_WRITE_TRAP] = cr_trap,
d1949b93 3079 [SVM_EXIT_CR8_WRITE_TRAP] = cr_trap,
4407a797 3080 [SVM_EXIT_INVPCID] = invpcid_interception,
d0006530 3081 [SVM_EXIT_NPF] = npf_interception,
7607b717 3082 [SVM_EXIT_RSM] = rsm_interception,
18f40c53
SS
3083 [SVM_EXIT_AVIC_INCOMPLETE_IPI] = avic_incomplete_ipi_interception,
3084 [SVM_EXIT_AVIC_UNACCELERATED_ACCESS] = avic_unaccelerated_access_interception,
291bd20d 3085 [SVM_EXIT_VMGEXIT] = sev_handle_vmgexit,
6aa8b732
AK
3086};
3087
ae8cc059 3088static void dump_vmcb(struct kvm_vcpu *vcpu)
3f10c846
JR
3089{
3090 struct vcpu_svm *svm = to_svm(vcpu);
3091 struct vmcb_control_area *control = &svm->vmcb->control;
3092 struct vmcb_save_area *save = &svm->vmcb->save;
3093
6f2f8453
PB
3094 if (!dump_invalid_vmcb) {
3095 pr_warn_ratelimited("set kvm_amd.dump_invalid_vmcb=1 to dump internal KVM state.\n");
3096 return;
3097 }
3098
3f10c846 3099 pr_err("VMCB Control Area:\n");
03bfeeb9
BM
3100 pr_err("%-20s%04x\n", "cr_read:", control->intercepts[INTERCEPT_CR] & 0xffff);
3101 pr_err("%-20s%04x\n", "cr_write:", control->intercepts[INTERCEPT_CR] >> 16);
30abaa88
BM
3102 pr_err("%-20s%04x\n", "dr_read:", control->intercepts[INTERCEPT_DR] & 0xffff);
3103 pr_err("%-20s%04x\n", "dr_write:", control->intercepts[INTERCEPT_DR] >> 16);
9780d51d 3104 pr_err("%-20s%08x\n", "exceptions:", control->intercepts[INTERCEPT_EXCEPTION]);
c62e2e94
BM
3105 pr_err("%-20s%08x %08x\n", "intercepts:",
3106 control->intercepts[INTERCEPT_WORD3],
3107 control->intercepts[INTERCEPT_WORD4]);
ae8cc059 3108 pr_err("%-20s%d\n", "pause filter count:", control->pause_filter_count);
1d8fb44a
BM
3109 pr_err("%-20s%d\n", "pause filter threshold:",
3110 control->pause_filter_thresh);
ae8cc059
JP
3111 pr_err("%-20s%016llx\n", "iopm_base_pa:", control->iopm_base_pa);
3112 pr_err("%-20s%016llx\n", "msrpm_base_pa:", control->msrpm_base_pa);
3113 pr_err("%-20s%016llx\n", "tsc_offset:", control->tsc_offset);
3114 pr_err("%-20s%d\n", "asid:", control->asid);
3115 pr_err("%-20s%d\n", "tlb_ctl:", control->tlb_ctl);
3116 pr_err("%-20s%08x\n", "int_ctl:", control->int_ctl);
3117 pr_err("%-20s%08x\n", "int_vector:", control->int_vector);
3118 pr_err("%-20s%08x\n", "int_state:", control->int_state);
3119 pr_err("%-20s%08x\n", "exit_code:", control->exit_code);
3120 pr_err("%-20s%016llx\n", "exit_info1:", control->exit_info_1);
3121 pr_err("%-20s%016llx\n", "exit_info2:", control->exit_info_2);
3122 pr_err("%-20s%08x\n", "exit_int_info:", control->exit_int_info);
3123 pr_err("%-20s%08x\n", "exit_int_info_err:", control->exit_int_info_err);
3124 pr_err("%-20s%lld\n", "nested_ctl:", control->nested_ctl);
3125 pr_err("%-20s%016llx\n", "nested_cr3:", control->nested_cr3);
44a95dae 3126 pr_err("%-20s%016llx\n", "avic_vapic_bar:", control->avic_vapic_bar);
291bd20d 3127 pr_err("%-20s%016llx\n", "ghcb:", control->ghcb_gpa);
ae8cc059
JP
3128 pr_err("%-20s%08x\n", "event_inj:", control->event_inj);
3129 pr_err("%-20s%08x\n", "event_inj_err:", control->event_inj_err);
0dc92119 3130 pr_err("%-20s%lld\n", "virt_ext:", control->virt_ext);
ae8cc059 3131 pr_err("%-20s%016llx\n", "next_rip:", control->next_rip);
44a95dae
SS
3132 pr_err("%-20s%016llx\n", "avic_backing_page:", control->avic_backing_page);
3133 pr_err("%-20s%016llx\n", "avic_logical_id:", control->avic_logical_id);
3134 pr_err("%-20s%016llx\n", "avic_physical_id:", control->avic_physical_id);
376c6d28 3135 pr_err("%-20s%016llx\n", "vmsa_pa:", control->vmsa_pa);
3f10c846 3136 pr_err("VMCB State Save Area:\n");
ae8cc059
JP
3137 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
3138 "es:",
3139 save->es.selector, save->es.attrib,
3140 save->es.limit, save->es.base);
3141 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
3142 "cs:",
3143 save->cs.selector, save->cs.attrib,
3144 save->cs.limit, save->cs.base);
3145 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
3146 "ss:",
3147 save->ss.selector, save->ss.attrib,
3148 save->ss.limit, save->ss.base);
3149 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
3150 "ds:",
3151 save->ds.selector, save->ds.attrib,
3152 save->ds.limit, save->ds.base);
3153 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
3154 "fs:",
3155 save->fs.selector, save->fs.attrib,
3156 save->fs.limit, save->fs.base);
3157 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
3158 "gs:",
3159 save->gs.selector, save->gs.attrib,
3160 save->gs.limit, save->gs.base);
3161 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
3162 "gdtr:",
3163 save->gdtr.selector, save->gdtr.attrib,
3164 save->gdtr.limit, save->gdtr.base);
3165 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
3166 "ldtr:",
3167 save->ldtr.selector, save->ldtr.attrib,
3168 save->ldtr.limit, save->ldtr.base);
3169 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
3170 "idtr:",
3171 save->idtr.selector, save->idtr.attrib,
3172 save->idtr.limit, save->idtr.base);
3173 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
3174 "tr:",
3175 save->tr.selector, save->tr.attrib,
3176 save->tr.limit, save->tr.base);
3f10c846
JR
3177 pr_err("cpl: %d efer: %016llx\n",
3178 save->cpl, save->efer);
ae8cc059
JP
3179 pr_err("%-15s %016llx %-13s %016llx\n",
3180 "cr0:", save->cr0, "cr2:", save->cr2);
3181 pr_err("%-15s %016llx %-13s %016llx\n",
3182 "cr3:", save->cr3, "cr4:", save->cr4);
3183 pr_err("%-15s %016llx %-13s %016llx\n",
3184 "dr6:", save->dr6, "dr7:", save->dr7);
3185 pr_err("%-15s %016llx %-13s %016llx\n",
3186 "rip:", save->rip, "rflags:", save->rflags);
3187 pr_err("%-15s %016llx %-13s %016llx\n",
3188 "rsp:", save->rsp, "rax:", save->rax);
3189 pr_err("%-15s %016llx %-13s %016llx\n",
3190 "star:", save->star, "lstar:", save->lstar);
3191 pr_err("%-15s %016llx %-13s %016llx\n",
3192 "cstar:", save->cstar, "sfmask:", save->sfmask);
3193 pr_err("%-15s %016llx %-13s %016llx\n",
3194 "kernel_gs_base:", save->kernel_gs_base,
3195 "sysenter_cs:", save->sysenter_cs);
3196 pr_err("%-15s %016llx %-13s %016llx\n",
3197 "sysenter_esp:", save->sysenter_esp,
3198 "sysenter_eip:", save->sysenter_eip);
3199 pr_err("%-15s %016llx %-13s %016llx\n",
3200 "gpat:", save->g_pat, "dbgctl:", save->dbgctl);
3201 pr_err("%-15s %016llx %-13s %016llx\n",
3202 "br_from:", save->br_from, "br_to:", save->br_to);
3203 pr_err("%-15s %016llx %-13s %016llx\n",
3204 "excp_from:", save->last_excp_from,
3205 "excp_to:", save->last_excp_to);
3f10c846
JR
3206}
3207
e9093fd4
TL
3208static int svm_handle_invalid_exit(struct kvm_vcpu *vcpu, u64 exit_code)
3209{
3210 if (exit_code < ARRAY_SIZE(svm_exit_handlers) &&
3211 svm_exit_handlers[exit_code])
3212 return 0;
3213
3214 vcpu_unimpl(vcpu, "svm: unexpected exit reason 0x%llx\n", exit_code);
3215 dump_vmcb(vcpu);
3216 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
3217 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_UNEXPECTED_EXIT_REASON;
3218 vcpu->run->internal.ndata = 2;
3219 vcpu->run->internal.data[0] = exit_code;
3220 vcpu->run->internal.data[1] = vcpu->arch.last_vmentry_cpu;
3221
3222 return -EINVAL;
3223}
3224
291bd20d 3225int svm_invoke_exit_handler(struct vcpu_svm *svm, u64 exit_code)
e9093fd4
TL
3226{
3227 if (svm_handle_invalid_exit(&svm->vcpu, exit_code))
3228 return 0;
3229
3230#ifdef CONFIG_RETPOLINE
3231 if (exit_code == SVM_EXIT_MSR)
3232 return msr_interception(svm);
3233 else if (exit_code == SVM_EXIT_VINTR)
3234 return interrupt_window_interception(svm);
3235 else if (exit_code == SVM_EXIT_INTR)
3236 return intr_interception(svm);
3237 else if (exit_code == SVM_EXIT_HLT)
3238 return halt_interception(svm);
3239 else if (exit_code == SVM_EXIT_NPF)
3240 return npf_interception(svm);
3241#endif
3242 return svm_exit_handlers[exit_code](svm);
3243}
3244
235ba74f
SC
3245static void svm_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2,
3246 u32 *intr_info, u32 *error_code)
586f9607
AK
3247{
3248 struct vmcb_control_area *control = &to_svm(vcpu)->vmcb->control;
3249
3250 *info1 = control->exit_info_1;
3251 *info2 = control->exit_info_2;
235ba74f
SC
3252 *intr_info = control->exit_int_info;
3253 if ((*intr_info & SVM_EXITINTINFO_VALID) &&
3254 (*intr_info & SVM_EXITINTINFO_VALID_ERR))
3255 *error_code = control->exit_int_info_err;
3256 else
3257 *error_code = 0;
586f9607
AK
3258}
3259
404d5d7b 3260static int handle_exit(struct kvm_vcpu *vcpu, fastpath_t exit_fastpath)
6aa8b732 3261{
04d2cc77 3262 struct vcpu_svm *svm = to_svm(vcpu);
851ba692 3263 struct kvm_run *kvm_run = vcpu->run;
a2fa3e9f 3264 u32 exit_code = svm->vmcb->control.exit_code;
6aa8b732 3265
8b89fe1f
PB
3266 trace_kvm_exit(exit_code, vcpu, KVM_ISA_SVM);
3267
f1c6366e
TL
3268 /* SEV-ES guests must use the CR write traps to track CR registers. */
3269 if (!sev_es_guest(vcpu->kvm)) {
3270 if (!svm_is_intercept(svm, INTERCEPT_CR0_WRITE))
3271 vcpu->arch.cr0 = svm->vmcb->save.cr0;
3272 if (npt_enabled)
3273 vcpu->arch.cr3 = svm->vmcb->save.cr3;
3274 }
af9ca2d7 3275
2030753d 3276 if (is_guest_mode(vcpu)) {
410e4d57
JR
3277 int vmexit;
3278
cc167bd7 3279 trace_kvm_nested_vmexit(exit_code, vcpu, KVM_ISA_SVM);
d8cabddf 3280
410e4d57
JR
3281 vmexit = nested_svm_exit_special(svm);
3282
3283 if (vmexit == NESTED_EXIT_CONTINUE)
3284 vmexit = nested_svm_exit_handled(svm);
3285
3286 if (vmexit == NESTED_EXIT_DONE)
cf74a78b 3287 return 1;
cf74a78b
AG
3288 }
3289
04d2cc77
AK
3290 if (svm->vmcb->control.exit_code == SVM_EXIT_ERR) {
3291 kvm_run->exit_reason = KVM_EXIT_FAIL_ENTRY;
3292 kvm_run->fail_entry.hardware_entry_failure_reason
3293 = svm->vmcb->control.exit_code;
8a14fe4f 3294 kvm_run->fail_entry.cpu = vcpu->arch.last_vmentry_cpu;
3f10c846 3295 dump_vmcb(vcpu);
04d2cc77
AK
3296 return 0;
3297 }
3298
a2fa3e9f 3299 if (is_external_interrupt(svm->vmcb->control.exit_int_info) &&
709ddebf 3300 exit_code != SVM_EXIT_EXCP_BASE + PF_VECTOR &&
55c5e464
JR
3301 exit_code != SVM_EXIT_NPF && exit_code != SVM_EXIT_TASK_SWITCH &&
3302 exit_code != SVM_EXIT_INTR && exit_code != SVM_EXIT_NMI)
6614c7d0 3303 printk(KERN_ERR "%s: unexpected exit_int_info 0x%x "
6aa8b732 3304 "exit_code 0x%x\n",
b8688d51 3305 __func__, svm->vmcb->control.exit_int_info,
6aa8b732
AK
3306 exit_code);
3307
404d5d7b 3308 if (exit_fastpath != EXIT_FASTPATH_NONE)
1e9e2622 3309 return 1;
404d5d7b 3310
e9093fd4 3311 return svm_invoke_exit_handler(svm, exit_code);
6aa8b732
AK
3312}
3313
3314static void reload_tss(struct kvm_vcpu *vcpu)
3315{
73cd6e5f 3316 struct svm_cpu_data *sd = per_cpu(svm_data, vcpu->cpu);
6aa8b732 3317
0fe1e009 3318 sd->tss_desc->type = 9; /* available 32/64-bit TSS */
6aa8b732
AK
3319 load_TR_desc();
3320}
3321
e756fc62 3322static void pre_svm_run(struct vcpu_svm *svm)
6aa8b732 3323{
73cd6e5f 3324 struct svm_cpu_data *sd = per_cpu(svm_data, svm->vcpu.cpu);
6aa8b732 3325
70cd94e6 3326 if (sev_guest(svm->vcpu.kvm))
73cd6e5f 3327 return pre_sev_run(svm, svm->vcpu.cpu);
70cd94e6 3328
4b656b12 3329 /* FIXME: handle wraparound of asid_generation */
0fe1e009
TH
3330 if (svm->asid_generation != sd->asid_generation)
3331 new_asid(svm, sd);
6aa8b732
AK
3332}
3333
95ba8273
GN
3334static void svm_inject_nmi(struct kvm_vcpu *vcpu)
3335{
3336 struct vcpu_svm *svm = to_svm(vcpu);
3337
3338 svm->vmcb->control.event_inj = SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_NMI;
3339 vcpu->arch.hflags |= HF_NMI_MASK;
4444dfe4
TL
3340 if (!sev_es_guest(svm->vcpu.kvm))
3341 svm_set_intercept(svm, INTERCEPT_IRET);
95ba8273
GN
3342 ++vcpu->stat.nmi_injections;
3343}
6aa8b732 3344
66fd3f7f 3345static void svm_set_irq(struct kvm_vcpu *vcpu)
2a8067f1
ED
3346{
3347 struct vcpu_svm *svm = to_svm(vcpu);
3348
2af9194d 3349 BUG_ON(!(gif_set(svm)));
cf74a78b 3350
9fb2d2b4
GN
3351 trace_kvm_inj_virq(vcpu->arch.interrupt.nr);
3352 ++vcpu->stat.irq_injections;
3353
219b65dc
AG
3354 svm->vmcb->control.event_inj = vcpu->arch.interrupt.nr |
3355 SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_INTR;
2a8067f1
ED
3356}
3357
95ba8273 3358static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
aaacfc9a
JR
3359{
3360 struct vcpu_svm *svm = to_svm(vcpu);
aaacfc9a 3361
f1c6366e
TL
3362 /*
3363 * SEV-ES guests must always keep the CR intercepts cleared. CR
3364 * tracking is done using the CR write traps.
3365 */
3366 if (sev_es_guest(vcpu->kvm))
3367 return;
3368
01c3b2b5 3369 if (nested_svm_virtualize_tpr(vcpu))
88ab24ad
JR
3370 return;
3371
830bd71f 3372 svm_clr_intercept(svm, INTERCEPT_CR8_WRITE);
596f3142 3373
95ba8273 3374 if (irr == -1)
aaacfc9a
JR
3375 return;
3376
95ba8273 3377 if (tpr >= irr)
830bd71f 3378 svm_set_intercept(svm, INTERCEPT_CR8_WRITE);
95ba8273 3379}
aaacfc9a 3380
cae96af1 3381bool svm_nmi_blocked(struct kvm_vcpu *vcpu)
95ba8273
GN
3382{
3383 struct vcpu_svm *svm = to_svm(vcpu);
3384 struct vmcb *vmcb = svm->vmcb;
88c604b6 3385 bool ret;
9c3d370a 3386
cae96af1 3387 if (!gif_set(svm))
bbdad0b5
PB
3388 return true;
3389
cae96af1
PB
3390 if (is_guest_mode(vcpu) && nested_exit_on_nmi(svm))
3391 return false;
3392
3393 ret = (vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK) ||
3394 (svm->vcpu.arch.hflags & HF_NMI_MASK);
924584cc
JR
3395
3396 return ret;
aaacfc9a
JR
3397}
3398
c9d40913 3399static int svm_nmi_allowed(struct kvm_vcpu *vcpu, bool for_injection)
cae96af1
PB
3400{
3401 struct vcpu_svm *svm = to_svm(vcpu);
3402 if (svm->nested.nested_run_pending)
c9d40913 3403 return -EBUSY;
cae96af1 3404
c300ab9f
PB
3405 /* An NMI must not be injected into L2 if it's supposed to VM-Exit. */
3406 if (for_injection && is_guest_mode(vcpu) && nested_exit_on_nmi(svm))
c9d40913 3407 return -EBUSY;
c300ab9f
PB
3408
3409 return !svm_nmi_blocked(vcpu);
cae96af1
PB
3410}
3411
3cfc3092
JK
3412static bool svm_get_nmi_mask(struct kvm_vcpu *vcpu)
3413{
3414 struct vcpu_svm *svm = to_svm(vcpu);
3415
3416 return !!(svm->vcpu.arch.hflags & HF_NMI_MASK);
3417}
3418
3419static void svm_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
3420{
3421 struct vcpu_svm *svm = to_svm(vcpu);
3422
3423 if (masked) {
3424 svm->vcpu.arch.hflags |= HF_NMI_MASK;
4444dfe4
TL
3425 if (!sev_es_guest(svm->vcpu.kvm))
3426 svm_set_intercept(svm, INTERCEPT_IRET);
3cfc3092
JK
3427 } else {
3428 svm->vcpu.arch.hflags &= ~HF_NMI_MASK;
4444dfe4
TL
3429 if (!sev_es_guest(svm->vcpu.kvm))
3430 svm_clr_intercept(svm, INTERCEPT_IRET);
3cfc3092
JK
3431 }
3432}
3433
cae96af1 3434bool svm_interrupt_blocked(struct kvm_vcpu *vcpu)
78646121
GN
3435{
3436 struct vcpu_svm *svm = to_svm(vcpu);
3437 struct vmcb *vmcb = svm->vmcb;
7fcdb510 3438
fc6f7c03 3439 if (!gif_set(svm))
cae96af1 3440 return true;
7fcdb510 3441
f1c6366e
TL
3442 if (sev_es_guest(svm->vcpu.kvm)) {
3443 /*
3444 * SEV-ES guests to not expose RFLAGS. Use the VMCB interrupt mask
3445 * bit to determine the state of the IF flag.
3446 */
3447 if (!(vmcb->control.int_state & SVM_GUEST_INTERRUPT_MASK))
3448 return true;
3449 } else if (is_guest_mode(vcpu)) {
fc6f7c03 3450 /* As long as interrupts are being delivered... */
e9fd761a 3451 if ((svm->nested.ctl.int_ctl & V_INTR_MASKING_MASK)
08245e6d 3452 ? !(svm->nested.hsave->save.rflags & X86_EFLAGS_IF)
fc6f7c03
PB
3453 : !(kvm_get_rflags(vcpu) & X86_EFLAGS_IF))
3454 return true;
3455
3456 /* ... vmexits aren't blocked by the interrupt shadow */
3457 if (nested_exit_on_intr(svm))
3458 return false;
3459 } else {
3460 if (!(kvm_get_rflags(vcpu) & X86_EFLAGS_IF))
3461 return true;
3462 }
3463
3464 return (vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK);
cae96af1
PB
3465}
3466
c9d40913 3467static int svm_interrupt_allowed(struct kvm_vcpu *vcpu, bool for_injection)
cae96af1
PB
3468{
3469 struct vcpu_svm *svm = to_svm(vcpu);
3470 if (svm->nested.nested_run_pending)
c9d40913 3471 return -EBUSY;
cae96af1 3472
c300ab9f
PB
3473 /*
3474 * An IRQ must not be injected into L2 if it's supposed to VM-Exit,
3475 * e.g. if the IRQ arrived asynchronously after checking nested events.
3476 */
3477 if (for_injection && is_guest_mode(vcpu) && nested_exit_on_intr(svm))
c9d40913 3478 return -EBUSY;
c300ab9f
PB
3479
3480 return !svm_interrupt_blocked(vcpu);
78646121
GN
3481}
3482
c9a7953f 3483static void enable_irq_window(struct kvm_vcpu *vcpu)
6aa8b732 3484{
219b65dc 3485 struct vcpu_svm *svm = to_svm(vcpu);
219b65dc 3486
e0231715
JR
3487 /*
3488 * In case GIF=0 we can't rely on the CPU to tell us when GIF becomes
3489 * 1, because that's a separate STGI/VMRUN intercept. The next time we
3490 * get that intercept, this function will be called again though and
640bd6e5
JN
3491 * we'll get the vintr intercept. However, if the vGIF feature is
3492 * enabled, the STGI interception will not occur. Enable the irq
3493 * window under the assumption that the hardware will set the GIF.
e0231715 3494 */
b518ba9f 3495 if (vgif_enabled(svm) || gif_set(svm)) {
f3515dc3
SS
3496 /*
3497 * IRQ window is not needed when AVIC is enabled,
3498 * unless we have pending ExtINT since it cannot be injected
3499 * via AVIC. In such case, we need to temporarily disable AVIC,
3500 * and fallback to injecting IRQ via V_IRQ.
3501 */
3502 svm_toggle_avic_for_irq_window(vcpu, false);
219b65dc 3503 svm_set_vintr(svm);
219b65dc 3504 }
85f455f7
ED
3505}
3506
c9a7953f 3507static void enable_nmi_window(struct kvm_vcpu *vcpu)
c1150d8c 3508{
04d2cc77 3509 struct vcpu_svm *svm = to_svm(vcpu);
c1150d8c 3510
44c11430
GN
3511 if ((svm->vcpu.arch.hflags & (HF_NMI_MASK | HF_IRET_MASK))
3512 == HF_NMI_MASK)
c9a7953f 3513 return; /* IRET will cause a vm exit */
44c11430 3514
640bd6e5
JN
3515 if (!gif_set(svm)) {
3516 if (vgif_enabled(svm))
a284ba56 3517 svm_set_intercept(svm, INTERCEPT_STGI);
1a5e1852 3518 return; /* STGI will cause a vm exit */
640bd6e5 3519 }
1a5e1852 3520
e0231715
JR
3521 /*
3522 * Something prevents NMI from been injected. Single step over possible
3523 * problem (IRET or exception injection or interrupt shadow)
3524 */
ab2f4d73 3525 svm->nmi_singlestep_guest_rflags = svm_get_rflags(vcpu);
6be7d306 3526 svm->nmi_singlestep = true;
44c11430 3527 svm->vmcb->save.rflags |= (X86_EFLAGS_TF | X86_EFLAGS_RF);
c1150d8c
DL
3528}
3529
cbc94022
IE
3530static int svm_set_tss_addr(struct kvm *kvm, unsigned int addr)
3531{
3532 return 0;
3533}
3534
2ac52ab8
SC
3535static int svm_set_identity_map_addr(struct kvm *kvm, u64 ident_addr)
3536{
3537 return 0;
3538}
3539
f55ac304 3540void svm_flush_tlb(struct kvm_vcpu *vcpu)
d9e368d6 3541{
38e5e92f
JR
3542 struct vcpu_svm *svm = to_svm(vcpu);
3543
4a41e43c
SC
3544 /*
3545 * Flush only the current ASID even if the TLB flush was invoked via
3546 * kvm_flush_remote_tlbs(). Although flushing remote TLBs requires all
3547 * ASIDs to be flushed, KVM uses a single ASID for L1 and L2, and
3548 * unconditionally does a TLB flush on both nested VM-Enter and nested
3549 * VM-Exit (via kvm_mmu_reset_context()).
3550 */
38e5e92f
JR
3551 if (static_cpu_has(X86_FEATURE_FLUSHBYASID))
3552 svm->vmcb->control.tlb_ctl = TLB_CONTROL_FLUSH_ASID;
3553 else
3554 svm->asid_generation--;
d9e368d6
AK
3555}
3556
faff8758
JS
3557static void svm_flush_tlb_gva(struct kvm_vcpu *vcpu, gva_t gva)
3558{
3559 struct vcpu_svm *svm = to_svm(vcpu);
3560
3561 invlpga(gva, svm->vmcb->control.asid);
3562}
3563
04d2cc77
AK
3564static void svm_prepare_guest_switch(struct kvm_vcpu *vcpu)
3565{
3566}
3567
d7bf8221
JR
3568static inline void sync_cr8_to_lapic(struct kvm_vcpu *vcpu)
3569{
3570 struct vcpu_svm *svm = to_svm(vcpu);
3571
01c3b2b5 3572 if (nested_svm_virtualize_tpr(vcpu))
88ab24ad
JR
3573 return;
3574
830bd71f 3575 if (!svm_is_intercept(svm, INTERCEPT_CR8_WRITE)) {
d7bf8221 3576 int cr8 = svm->vmcb->control.int_ctl & V_TPR_MASK;
615d5193 3577 kvm_set_cr8(vcpu, cr8);
d7bf8221
JR
3578 }
3579}
3580
649d6864
JR
3581static inline void sync_lapic_to_cr8(struct kvm_vcpu *vcpu)
3582{
3583 struct vcpu_svm *svm = to_svm(vcpu);
3584 u64 cr8;
3585
01c3b2b5 3586 if (nested_svm_virtualize_tpr(vcpu) ||
3bbf3565 3587 kvm_vcpu_apicv_active(vcpu))
88ab24ad
JR
3588 return;
3589
649d6864
JR
3590 cr8 = kvm_get_cr8(vcpu);
3591 svm->vmcb->control.int_ctl &= ~V_TPR_MASK;
3592 svm->vmcb->control.int_ctl |= cr8 & V_TPR_MASK;
3593}
3594
9222be18
GN
3595static void svm_complete_interrupts(struct vcpu_svm *svm)
3596{
3597 u8 vector;
3598 int type;
3599 u32 exitintinfo = svm->vmcb->control.exit_int_info;
66b7138f
JK
3600 unsigned int3_injected = svm->int3_injected;
3601
3602 svm->int3_injected = 0;
9222be18 3603
bd3d1ec3
AK
3604 /*
3605 * If we've made progress since setting HF_IRET_MASK, we've
3606 * executed an IRET and can allow NMI injection.
3607 */
4444dfe4
TL
3608 if ((svm->vcpu.arch.hflags & HF_IRET_MASK) &&
3609 (sev_es_guest(svm->vcpu.kvm) ||
3610 kvm_rip_read(&svm->vcpu) != svm->nmi_iret_rip)) {
44c11430 3611 svm->vcpu.arch.hflags &= ~(HF_NMI_MASK | HF_IRET_MASK);
3842d135
AK
3612 kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
3613 }
44c11430 3614
9222be18
GN
3615 svm->vcpu.arch.nmi_injected = false;
3616 kvm_clear_exception_queue(&svm->vcpu);
3617 kvm_clear_interrupt_queue(&svm->vcpu);
3618
3619 if (!(exitintinfo & SVM_EXITINTINFO_VALID))
3620 return;
3621
3842d135
AK
3622 kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
3623
9222be18
GN
3624 vector = exitintinfo & SVM_EXITINTINFO_VEC_MASK;
3625 type = exitintinfo & SVM_EXITINTINFO_TYPE_MASK;
3626
3627 switch (type) {
3628 case SVM_EXITINTINFO_TYPE_NMI:
3629 svm->vcpu.arch.nmi_injected = true;
3630 break;
3631 case SVM_EXITINTINFO_TYPE_EXEPT:
f1c6366e
TL
3632 /*
3633 * Never re-inject a #VC exception.
3634 */
3635 if (vector == X86_TRAP_VC)
3636 break;
3637
66b7138f
JK
3638 /*
3639 * In case of software exceptions, do not reinject the vector,
3640 * but re-execute the instruction instead. Rewind RIP first
3641 * if we emulated INT3 before.
3642 */
3643 if (kvm_exception_is_soft(vector)) {
3644 if (vector == BP_VECTOR && int3_injected &&
3645 kvm_is_linear_rip(&svm->vcpu, svm->int3_rip))
3646 kvm_rip_write(&svm->vcpu,
3647 kvm_rip_read(&svm->vcpu) -
3648 int3_injected);
9222be18 3649 break;
66b7138f 3650 }
9222be18
GN
3651 if (exitintinfo & SVM_EXITINTINFO_VALID_ERR) {
3652 u32 err = svm->vmcb->control.exit_int_info_err;
ce7ddec4 3653 kvm_requeue_exception_e(&svm->vcpu, vector, err);
9222be18
GN
3654
3655 } else
ce7ddec4 3656 kvm_requeue_exception(&svm->vcpu, vector);
9222be18
GN
3657 break;
3658 case SVM_EXITINTINFO_TYPE_INTR:
66fd3f7f 3659 kvm_queue_interrupt(&svm->vcpu, vector, false);
9222be18
GN
3660 break;
3661 default:
3662 break;
3663 }
3664}
3665
b463a6f7
AK
3666static void svm_cancel_injection(struct kvm_vcpu *vcpu)
3667{
3668 struct vcpu_svm *svm = to_svm(vcpu);
3669 struct vmcb_control_area *control = &svm->vmcb->control;
3670
3671 control->exit_int_info = control->event_inj;
3672 control->exit_int_info_err = control->event_inj_err;
3673 control->event_inj = 0;
3674 svm_complete_interrupts(svm);
3675}
3676
404d5d7b 3677static fastpath_t svm_exit_handlers_fastpath(struct kvm_vcpu *vcpu)
a9ab13ff 3678{
4e810adb 3679 if (to_svm(vcpu)->vmcb->control.exit_code == SVM_EXIT_MSR &&
a9ab13ff
WL
3680 to_svm(vcpu)->vmcb->control.exit_info_1)
3681 return handle_fastpath_set_msr_irqoff(vcpu);
3682
3683 return EXIT_FASTPATH_NONE;
3684}
3685
135961e0
TG
3686static noinstr void svm_vcpu_enter_exit(struct kvm_vcpu *vcpu,
3687 struct vcpu_svm *svm)
3688{
3689 /*
3690 * VMENTER enables interrupts (host state), but the kernel state is
3691 * interrupts disabled when this is invoked. Also tell RCU about
3692 * it. This is the same logic as for exit_to_user_mode().
3693 *
3694 * This ensures that e.g. latency analysis on the host observes
3695 * guest mode as interrupt enabled.
3696 *
3697 * guest_enter_irqoff() informs context tracking about the
3698 * transition to guest mode and if enabled adjusts RCU state
3699 * accordingly.
3700 */
3701 instrumentation_begin();
3702 trace_hardirqs_on_prepare();
3703 lockdep_hardirqs_on_prepare(CALLER_ADDR0);
3704 instrumentation_end();
3705
3706 guest_enter_irqoff();
3707 lockdep_hardirqs_on(CALLER_ADDR0);
3708
16809ecd
TL
3709 if (sev_es_guest(svm->vcpu.kvm)) {
3710 __svm_sev_es_vcpu_run(svm->vmcb_pa);
3711 } else {
3712 __svm_vcpu_run(svm->vmcb_pa, (unsigned long *)&svm->vcpu.arch.regs);
135961e0
TG
3713
3714#ifdef CONFIG_X86_64
16809ecd 3715 native_wrmsrl(MSR_GS_BASE, svm->host.gs_base);
135961e0 3716#else
16809ecd 3717 loadsegment(fs, svm->host.fs);
135961e0 3718#ifndef CONFIG_X86_32_LAZY_GS
16809ecd 3719 loadsegment(gs, svm->host.gs);
135961e0
TG
3720#endif
3721#endif
16809ecd 3722 }
135961e0
TG
3723
3724 /*
3725 * VMEXIT disables interrupts (host state), but tracing and lockdep
3726 * have them in state 'on' as recorded before entering guest mode.
3727 * Same as enter_from_user_mode().
3728 *
3729 * guest_exit_irqoff() restores host context and reinstates RCU if
3730 * enabled and required.
3731 *
3732 * This needs to be done before the below as native_read_msr()
3733 * contains a tracepoint and x86_spec_ctrl_restore_host() calls
3734 * into world and some more.
3735 */
3736 lockdep_hardirqs_off(CALLER_ADDR0);
3737 guest_exit_irqoff();
3738
3739 instrumentation_begin();
3740 trace_hardirqs_off_finish();
3741 instrumentation_end();
3742}
3743
b95273f1 3744static __no_kcsan fastpath_t svm_vcpu_run(struct kvm_vcpu *vcpu)
6aa8b732 3745{
a2fa3e9f 3746 struct vcpu_svm *svm = to_svm(vcpu);
d9e368d6 3747
d95df951
LB
3748 trace_kvm_entry(vcpu);
3749
2041a06a
JR
3750 svm->vmcb->save.rax = vcpu->arch.regs[VCPU_REGS_RAX];
3751 svm->vmcb->save.rsp = vcpu->arch.regs[VCPU_REGS_RSP];
3752 svm->vmcb->save.rip = vcpu->arch.regs[VCPU_REGS_RIP];
3753
a12713c2
LP
3754 /*
3755 * Disable singlestep if we're injecting an interrupt/exception.
3756 * We don't want our modified rflags to be pushed on the stack where
3757 * we might not be able to easily reset them if we disabled NMI
3758 * singlestep later.
3759 */
3760 if (svm->nmi_singlestep && svm->vmcb->control.event_inj) {
3761 /*
3762 * Event injection happens before external interrupts cause a
3763 * vmexit and interrupts are disabled here, so smp_send_reschedule
3764 * is enough to force an immediate vmexit.
3765 */
3766 disable_nmi_singlestep(svm);
3767 smp_send_reschedule(vcpu->cpu);
3768 }
3769
e756fc62 3770 pre_svm_run(svm);
6aa8b732 3771
649d6864
JR
3772 sync_lapic_to_cr8(vcpu);
3773
7e8e6eed
CA
3774 if (unlikely(svm->asid != svm->vmcb->control.asid)) {
3775 svm->vmcb->control.asid = svm->asid;
3776 vmcb_mark_dirty(svm->vmcb, VMCB_ASID);
3777 }
cda0ffdd 3778 svm->vmcb->save.cr2 = vcpu->arch.cr2;
6aa8b732 3779
d67668e9
PB
3780 /*
3781 * Run with all-zero DR6 unless needed, so that we can get the exact cause
3782 * of a #DB.
3783 */
3784 if (unlikely(svm->vcpu.arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT))
3785 svm_set_dr6(svm, vcpu->arch.dr6);
3786 else
3787 svm_set_dr6(svm, DR6_FIXED_1 | DR6_RTM);
3788
04d2cc77 3789 clgi();
139a12cf 3790 kvm_load_guest_xsave_state(vcpu);
04d2cc77 3791
010fd37f 3792 kvm_wait_lapic_expire(vcpu);
b6c4bc65 3793
b2ac58f9
KA
3794 /*
3795 * If this vCPU has touched SPEC_CTRL, restore the guest's value if
3796 * it's non-zero. Since vmentry is serialising on affected CPUs, there
3797 * is no need to worry about the conditional branch over the wrmsr
3798 * being speculatively taken.
3799 */
ccbcd267 3800 x86_spec_ctrl_set_guest(svm->spec_ctrl, svm->virt_spec_ctrl);
b2ac58f9 3801
135961e0 3802 svm_vcpu_enter_exit(vcpu, svm);
15e6c22f 3803
b2ac58f9
KA
3804 /*
3805 * We do not use IBRS in the kernel. If this vCPU has used the
3806 * SPEC_CTRL MSR it may have left it on; save the value and
3807 * turn it off. This is much more efficient than blindly adding
3808 * it to the atomic save/restore list. Especially as the former
3809 * (Saving guest MSRs on vmexit) doesn't even exist in KVM.
3810 *
3811 * For non-nested case:
3812 * If the L01 MSR bitmap does not intercept the MSR, then we need to
3813 * save it.
3814 *
3815 * For nested case:
3816 * If the L02 MSR bitmap does not intercept the MSR, then we need to
3817 * save it.
3818 */
946fbbc1 3819 if (unlikely(!msr_write_intercepted(vcpu, MSR_IA32_SPEC_CTRL)))
ecb586bd 3820 svm->spec_ctrl = native_read_msr(MSR_IA32_SPEC_CTRL);
b2ac58f9 3821
16809ecd
TL
3822 if (!sev_es_guest(svm->vcpu.kvm))
3823 reload_tss(vcpu);
6aa8b732 3824
024d83ca
TG
3825 x86_spec_ctrl_restore_host(svm->spec_ctrl, svm->virt_spec_ctrl);
3826
16809ecd
TL
3827 if (!sev_es_guest(svm->vcpu.kvm)) {
3828 vcpu->arch.cr2 = svm->vmcb->save.cr2;
3829 vcpu->arch.regs[VCPU_REGS_RAX] = svm->vmcb->save.rax;
3830 vcpu->arch.regs[VCPU_REGS_RSP] = svm->vmcb->save.rsp;
3831 vcpu->arch.regs[VCPU_REGS_RIP] = svm->vmcb->save.rip;
3832 }
13c34e07 3833
3781c01c 3834 if (unlikely(svm->vmcb->control.exit_code == SVM_EXIT_NMI))
dd60d217 3835 kvm_before_interrupt(&svm->vcpu);
3781c01c 3836
139a12cf 3837 kvm_load_host_xsave_state(vcpu);
3781c01c
JR
3838 stgi();
3839
3840 /* Any pending NMI will happen here */
3841
3842 if (unlikely(svm->vmcb->control.exit_code == SVM_EXIT_NMI))
dd60d217 3843 kvm_after_interrupt(&svm->vcpu);
3781c01c 3844
d7bf8221
JR
3845 sync_cr8_to_lapic(vcpu);
3846
a2fa3e9f 3847 svm->next_rip = 0;
2d8a42be
PB
3848 if (is_guest_mode(&svm->vcpu)) {
3849 sync_nested_vmcb_control(svm);
3850 svm->nested.nested_run_pending = 0;
3851 }
9222be18 3852
38e5e92f 3853 svm->vmcb->control.tlb_ctl = TLB_CONTROL_DO_NOTHING;
e42c6828 3854 vmcb_mark_all_clean(svm->vmcb);
38e5e92f 3855
631bc487
GN
3856 /* if exit due to PF check for async PF */
3857 if (svm->vmcb->control.exit_code == SVM_EXIT_EXCP_BASE + PF_VECTOR)
68fd66f1
VK
3858 svm->vcpu.arch.apf.host_apf_flags =
3859 kvm_read_and_reset_apf_flags();
631bc487 3860
6de4f3ad
AK
3861 if (npt_enabled) {
3862 vcpu->arch.regs_avail &= ~(1 << VCPU_EXREG_PDPTR);
3863 vcpu->arch.regs_dirty &= ~(1 << VCPU_EXREG_PDPTR);
3864 }
fe5913e4
JR
3865
3866 /*
3867 * We need to handle MC intercepts here before the vcpu has a chance to
3868 * change the physical cpu
3869 */
3870 if (unlikely(svm->vmcb->control.exit_code ==
3871 SVM_EXIT_EXCP_BASE + MC_VECTOR))
3872 svm_handle_mce(svm);
8d28fec4 3873
e42c6828 3874 svm_complete_interrupts(svm);
4e810adb
WL
3875
3876 if (is_guest_mode(vcpu))
3877 return EXIT_FASTPATH_NONE;
3878
3879 return svm_exit_handlers_fastpath(vcpu);
6aa8b732
AK
3880}
3881
2a40b900
SC
3882static void svm_load_mmu_pgd(struct kvm_vcpu *vcpu, unsigned long root,
3883 int root_level)
6aa8b732 3884{
a2fa3e9f 3885 struct vcpu_svm *svm = to_svm(vcpu);
689f3bf2 3886 unsigned long cr3;
a2fa3e9f 3887
689f3bf2
PB
3888 cr3 = __sme_set(root);
3889 if (npt_enabled) {
3890 svm->vmcb->control.nested_cr3 = cr3;
06e7852c 3891 vmcb_mark_dirty(svm->vmcb, VMCB_NPT);
1c97f0a0 3892
689f3bf2 3893 /* Loading L2's CR3 is handled by enter_svm_guest_mode. */
978ce583
PB
3894 if (!test_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail))
3895 return;
3896 cr3 = vcpu->arch.cr3;
689f3bf2 3897 }
1c97f0a0 3898
978ce583 3899 svm->vmcb->save.cr3 = cr3;
06e7852c 3900 vmcb_mark_dirty(svm->vmcb, VMCB_CR);
1c97f0a0
JR
3901}
3902
6aa8b732
AK
3903static int is_disabled(void)
3904{
6031a61c
JR
3905 u64 vm_cr;
3906
3907 rdmsrl(MSR_VM_CR, vm_cr);
3908 if (vm_cr & (1 << SVM_VM_CR_SVM_DISABLE))
3909 return 1;
3910
6aa8b732
AK
3911 return 0;
3912}
3913
102d8325
IM
3914static void
3915svm_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
3916{
3917 /*
3918 * Patch in the VMMCALL instruction:
3919 */
3920 hypercall[0] = 0x0f;
3921 hypercall[1] = 0x01;
3922 hypercall[2] = 0xd9;
102d8325
IM
3923}
3924
f257d6dc 3925static int __init svm_check_processor_compat(void)
002c7f7c 3926{
f257d6dc 3927 return 0;
002c7f7c
YS
3928}
3929
774ead3a
AK
3930static bool svm_cpu_has_accelerated_tpr(void)
3931{
3932 return false;
3933}
3934
5719455f
TL
3935/*
3936 * The kvm parameter can be NULL (module initialization, or invocation before
3937 * VM creation). Be sure to check the kvm parameter before using it.
3938 */
3939static bool svm_has_emulated_msr(struct kvm *kvm, u32 index)
6d396b55 3940{
e87555e5
VK
3941 switch (index) {
3942 case MSR_IA32_MCG_EXT_CTL:
95c5c7c7 3943 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
e87555e5 3944 return false;
5719455f
TL
3945 case MSR_IA32_SMBASE:
3946 /* SEV-ES guests do not support SMM, so report false */
3947 if (kvm && sev_es_guest(kvm))
3948 return false;
3949 break;
e87555e5
VK
3950 default:
3951 break;
3952 }
3953
6d396b55
PB
3954 return true;
3955}
3956
fc07e76a
PB
3957static u64 svm_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
3958{
3959 return 0;
3960}
3961
7c1b761b 3962static void svm_vcpu_after_set_cpuid(struct kvm_vcpu *vcpu)
0e851880 3963{
6092d3d3 3964 struct vcpu_svm *svm = to_svm(vcpu);
96308b06 3965 struct kvm_cpuid_entry2 *best;
6092d3d3 3966
7204160e 3967 vcpu->arch.xsaves_enabled = guest_cpuid_has(vcpu, X86_FEATURE_XSAVE) &&
96be4e06 3968 boot_cpu_has(X86_FEATURE_XSAVE) &&
7204160e
AL
3969 boot_cpu_has(X86_FEATURE_XSAVES);
3970
6092d3d3 3971 /* Update nrips enabled cache */
4eb87460
SC
3972 svm->nrips_enabled = kvm_cpu_cap_has(X86_FEATURE_NRIPS) &&
3973 guest_cpuid_has(&svm->vcpu, X86_FEATURE_NRIPS);
46781eae 3974
4407a797
BM
3975 /* Check again if INVPCID interception if required */
3976 svm_check_invpcid(svm);
3977
96308b06
BM
3978 /* For sev guests, the memory encryption bit is not reserved in CR3. */
3979 if (sev_guest(vcpu->kvm)) {
3980 best = kvm_find_cpuid_entry(vcpu, 0x8000001F, 0);
3981 if (best)
3982 vcpu->arch.cr3_lm_rsvd_bits &= ~(1UL << (best->ebx & 0x3f));
3983 }
3984
46781eae
SS
3985 if (!kvm_vcpu_apicv_active(vcpu))
3986 return;
3987
cc7f5577
OU
3988 /*
3989 * AVIC does not work with an x2APIC mode guest. If the X2APIC feature
3990 * is exposed to the guest, disable AVIC.
3991 */
3992 if (guest_cpuid_has(vcpu, X86_FEATURE_X2APIC))
3993 kvm_request_apicv_update(vcpu->kvm, false,
3994 APICV_INHIBIT_REASON_X2APIC);
9a0bf054
SS
3995
3996 /*
3997 * Currently, AVIC does not work with nested virtualization.
3998 * So, we disable AVIC when cpuid for SVM is set in the L1 guest.
3999 */
4000 if (nested && guest_cpuid_has(vcpu, X86_FEATURE_SVM))
4001 kvm_request_apicv_update(vcpu->kvm, false,
4002 APICV_INHIBIT_REASON_NESTED);
0e851880
SY
4003}
4004
f5f48ee1
SY
4005static bool svm_has_wbinvd_exit(void)
4006{
4007 return true;
4008}
4009
8061252e 4010#define PRE_EX(exit) { .exit_code = (exit), \
40e19b51 4011 .stage = X86_ICPT_PRE_EXCEPT, }
cfec82cb 4012#define POST_EX(exit) { .exit_code = (exit), \
40e19b51 4013 .stage = X86_ICPT_POST_EXCEPT, }
d7eb8203 4014#define POST_MEM(exit) { .exit_code = (exit), \
40e19b51 4015 .stage = X86_ICPT_POST_MEMACCESS, }
cfec82cb 4016
09941fbb 4017static const struct __x86_intercept {
cfec82cb
JR
4018 u32 exit_code;
4019 enum x86_intercept_stage stage;
cfec82cb
JR
4020} x86_intercept_map[] = {
4021 [x86_intercept_cr_read] = POST_EX(SVM_EXIT_READ_CR0),
4022 [x86_intercept_cr_write] = POST_EX(SVM_EXIT_WRITE_CR0),
4023 [x86_intercept_clts] = POST_EX(SVM_EXIT_WRITE_CR0),
4024 [x86_intercept_lmsw] = POST_EX(SVM_EXIT_WRITE_CR0),
4025 [x86_intercept_smsw] = POST_EX(SVM_EXIT_READ_CR0),
3b88e41a
JR
4026 [x86_intercept_dr_read] = POST_EX(SVM_EXIT_READ_DR0),
4027 [x86_intercept_dr_write] = POST_EX(SVM_EXIT_WRITE_DR0),
dee6bb70
JR
4028 [x86_intercept_sldt] = POST_EX(SVM_EXIT_LDTR_READ),
4029 [x86_intercept_str] = POST_EX(SVM_EXIT_TR_READ),
4030 [x86_intercept_lldt] = POST_EX(SVM_EXIT_LDTR_WRITE),
4031 [x86_intercept_ltr] = POST_EX(SVM_EXIT_TR_WRITE),
4032 [x86_intercept_sgdt] = POST_EX(SVM_EXIT_GDTR_READ),
4033 [x86_intercept_sidt] = POST_EX(SVM_EXIT_IDTR_READ),
4034 [x86_intercept_lgdt] = POST_EX(SVM_EXIT_GDTR_WRITE),
4035 [x86_intercept_lidt] = POST_EX(SVM_EXIT_IDTR_WRITE),
01de8b09
JR
4036 [x86_intercept_vmrun] = POST_EX(SVM_EXIT_VMRUN),
4037 [x86_intercept_vmmcall] = POST_EX(SVM_EXIT_VMMCALL),
4038 [x86_intercept_vmload] = POST_EX(SVM_EXIT_VMLOAD),
4039 [x86_intercept_vmsave] = POST_EX(SVM_EXIT_VMSAVE),
4040 [x86_intercept_stgi] = POST_EX(SVM_EXIT_STGI),
4041 [x86_intercept_clgi] = POST_EX(SVM_EXIT_CLGI),
4042 [x86_intercept_skinit] = POST_EX(SVM_EXIT_SKINIT),
4043 [x86_intercept_invlpga] = POST_EX(SVM_EXIT_INVLPGA),
d7eb8203
JR
4044 [x86_intercept_rdtscp] = POST_EX(SVM_EXIT_RDTSCP),
4045 [x86_intercept_monitor] = POST_MEM(SVM_EXIT_MONITOR),
4046 [x86_intercept_mwait] = POST_EX(SVM_EXIT_MWAIT),
8061252e
JR
4047 [x86_intercept_invlpg] = POST_EX(SVM_EXIT_INVLPG),
4048 [x86_intercept_invd] = POST_EX(SVM_EXIT_INVD),
4049 [x86_intercept_wbinvd] = POST_EX(SVM_EXIT_WBINVD),
4050 [x86_intercept_wrmsr] = POST_EX(SVM_EXIT_MSR),
4051 [x86_intercept_rdtsc] = POST_EX(SVM_EXIT_RDTSC),
4052 [x86_intercept_rdmsr] = POST_EX(SVM_EXIT_MSR),
4053 [x86_intercept_rdpmc] = POST_EX(SVM_EXIT_RDPMC),
4054 [x86_intercept_cpuid] = PRE_EX(SVM_EXIT_CPUID),
4055 [x86_intercept_rsm] = PRE_EX(SVM_EXIT_RSM),
bf608f88
JR
4056 [x86_intercept_pause] = PRE_EX(SVM_EXIT_PAUSE),
4057 [x86_intercept_pushf] = PRE_EX(SVM_EXIT_PUSHF),
4058 [x86_intercept_popf] = PRE_EX(SVM_EXIT_POPF),
4059 [x86_intercept_intn] = PRE_EX(SVM_EXIT_SWINT),
4060 [x86_intercept_iret] = PRE_EX(SVM_EXIT_IRET),
4061 [x86_intercept_icebp] = PRE_EX(SVM_EXIT_ICEBP),
4062 [x86_intercept_hlt] = POST_EX(SVM_EXIT_HLT),
f6511935
JR
4063 [x86_intercept_in] = POST_EX(SVM_EXIT_IOIO),
4064 [x86_intercept_ins] = POST_EX(SVM_EXIT_IOIO),
4065 [x86_intercept_out] = POST_EX(SVM_EXIT_IOIO),
4066 [x86_intercept_outs] = POST_EX(SVM_EXIT_IOIO),
02d4160f 4067 [x86_intercept_xsetbv] = PRE_EX(SVM_EXIT_XSETBV),
cfec82cb
JR
4068};
4069
8061252e 4070#undef PRE_EX
cfec82cb 4071#undef POST_EX
d7eb8203 4072#undef POST_MEM
cfec82cb 4073
8a76d7f2
JR
4074static int svm_check_intercept(struct kvm_vcpu *vcpu,
4075 struct x86_instruction_info *info,
21f1b8f2
SC
4076 enum x86_intercept_stage stage,
4077 struct x86_exception *exception)
8a76d7f2 4078{
cfec82cb
JR
4079 struct vcpu_svm *svm = to_svm(vcpu);
4080 int vmexit, ret = X86EMUL_CONTINUE;
4081 struct __x86_intercept icpt_info;
4082 struct vmcb *vmcb = svm->vmcb;
4083
4084 if (info->intercept >= ARRAY_SIZE(x86_intercept_map))
4085 goto out;
4086
4087 icpt_info = x86_intercept_map[info->intercept];
4088
40e19b51 4089 if (stage != icpt_info.stage)
cfec82cb
JR
4090 goto out;
4091
4092 switch (icpt_info.exit_code) {
4093 case SVM_EXIT_READ_CR0:
4094 if (info->intercept == x86_intercept_cr_read)
4095 icpt_info.exit_code += info->modrm_reg;
4096 break;
4097 case SVM_EXIT_WRITE_CR0: {
4098 unsigned long cr0, val;
cfec82cb
JR
4099
4100 if (info->intercept == x86_intercept_cr_write)
4101 icpt_info.exit_code += info->modrm_reg;
4102
62baf44c
JK
4103 if (icpt_info.exit_code != SVM_EXIT_WRITE_CR0 ||
4104 info->intercept == x86_intercept_clts)
cfec82cb
JR
4105 break;
4106
c62e2e94
BM
4107 if (!(vmcb_is_intercept(&svm->nested.ctl,
4108 INTERCEPT_SELECTIVE_CR0)))
cfec82cb
JR
4109 break;
4110
4111 cr0 = vcpu->arch.cr0 & ~SVM_CR0_SELECTIVE_MASK;
4112 val = info->src_val & ~SVM_CR0_SELECTIVE_MASK;
4113
4114 if (info->intercept == x86_intercept_lmsw) {
4115 cr0 &= 0xfUL;
4116 val &= 0xfUL;
4117 /* lmsw can't clear PE - catch this here */
4118 if (cr0 & X86_CR0_PE)
4119 val |= X86_CR0_PE;
4120 }
4121
4122 if (cr0 ^ val)
4123 icpt_info.exit_code = SVM_EXIT_CR0_SEL_WRITE;
4124
4125 break;
4126 }
3b88e41a
JR
4127 case SVM_EXIT_READ_DR0:
4128 case SVM_EXIT_WRITE_DR0:
4129 icpt_info.exit_code += info->modrm_reg;
4130 break;
8061252e
JR
4131 case SVM_EXIT_MSR:
4132 if (info->intercept == x86_intercept_wrmsr)
4133 vmcb->control.exit_info_1 = 1;
4134 else
4135 vmcb->control.exit_info_1 = 0;
4136 break;
bf608f88
JR
4137 case SVM_EXIT_PAUSE:
4138 /*
4139 * We get this for NOP only, but pause
4140 * is rep not, check this here
4141 */
4142 if (info->rep_prefix != REPE_PREFIX)
4143 goto out;
49a8afca 4144 break;
f6511935
JR
4145 case SVM_EXIT_IOIO: {
4146 u64 exit_info;
4147 u32 bytes;
4148
f6511935
JR
4149 if (info->intercept == x86_intercept_in ||
4150 info->intercept == x86_intercept_ins) {
6cbc5f5a
JK
4151 exit_info = ((info->src_val & 0xffff) << 16) |
4152 SVM_IOIO_TYPE_MASK;
f6511935 4153 bytes = info->dst_bytes;
6493f157 4154 } else {
6cbc5f5a 4155 exit_info = (info->dst_val & 0xffff) << 16;
6493f157 4156 bytes = info->src_bytes;
f6511935
JR
4157 }
4158
4159 if (info->intercept == x86_intercept_outs ||
4160 info->intercept == x86_intercept_ins)
4161 exit_info |= SVM_IOIO_STR_MASK;
4162
4163 if (info->rep_prefix)
4164 exit_info |= SVM_IOIO_REP_MASK;
4165
4166 bytes = min(bytes, 4u);
4167
4168 exit_info |= bytes << SVM_IOIO_SIZE_SHIFT;
4169
4170 exit_info |= (u32)info->ad_bytes << (SVM_IOIO_ASIZE_SHIFT - 1);
4171
4172 vmcb->control.exit_info_1 = exit_info;
4173 vmcb->control.exit_info_2 = info->next_rip;
4174
4175 break;
4176 }
cfec82cb
JR
4177 default:
4178 break;
4179 }
4180
f104765b
BD
4181 /* TODO: Advertise NRIPS to guest hypervisor unconditionally */
4182 if (static_cpu_has(X86_FEATURE_NRIPS))
4183 vmcb->control.next_rip = info->next_rip;
cfec82cb
JR
4184 vmcb->control.exit_code = icpt_info.exit_code;
4185 vmexit = nested_svm_exit_handled(svm);
4186
4187 ret = (vmexit == NESTED_EXIT_DONE) ? X86EMUL_INTERCEPTED
4188 : X86EMUL_CONTINUE;
4189
4190out:
4191 return ret;
8a76d7f2
JR
4192}
4193
a9ab13ff 4194static void svm_handle_exit_irqoff(struct kvm_vcpu *vcpu)
a547c6db 4195{
a547c6db
YZ
4196}
4197
ae97a3b8
RK
4198static void svm_sched_in(struct kvm_vcpu *vcpu, int cpu)
4199{
830f01b0 4200 if (!kvm_pause_in_guest(vcpu->kvm))
8566ac8b 4201 shrink_ple_window(vcpu);
ae97a3b8
RK
4202}
4203
74f16909
BP
4204static void svm_setup_mce(struct kvm_vcpu *vcpu)
4205{
4206 /* [63:9] are reserved. */
4207 vcpu->arch.mcg_cap &= 0x1ff;
4208}
4209
cae96af1 4210bool svm_smi_blocked(struct kvm_vcpu *vcpu)
72d7b374 4211{
05cade71
LP
4212 struct vcpu_svm *svm = to_svm(vcpu);
4213
4214 /* Per APM Vol.2 15.22.2 "Response to SMI" */
4215 if (!gif_set(svm))
cae96af1
PB
4216 return true;
4217
4218 return is_smm(vcpu);
4219}
4220
c9d40913 4221static int svm_smi_allowed(struct kvm_vcpu *vcpu, bool for_injection)
cae96af1
PB
4222{
4223 struct vcpu_svm *svm = to_svm(vcpu);
4224 if (svm->nested.nested_run_pending)
c9d40913 4225 return -EBUSY;
05cade71 4226
c300ab9f
PB
4227 /* An SMI must not be injected into L2 if it's supposed to VM-Exit. */
4228 if (for_injection && is_guest_mode(vcpu) && nested_exit_on_smi(svm))
c9d40913 4229 return -EBUSY;
c300ab9f 4230
cae96af1 4231 return !svm_smi_blocked(vcpu);
72d7b374
LP
4232}
4233
0234bf88
LP
4234static int svm_pre_enter_smm(struct kvm_vcpu *vcpu, char *smstate)
4235{
05cade71
LP
4236 struct vcpu_svm *svm = to_svm(vcpu);
4237 int ret;
4238
4239 if (is_guest_mode(vcpu)) {
4240 /* FED8h - SVM Guest */
4241 put_smstate(u64, smstate, 0x7ed8, 1);
4242 /* FEE0h - SVM Guest VMCB Physical Address */
0dd16b5b 4243 put_smstate(u64, smstate, 0x7ee0, svm->nested.vmcb12_gpa);
05cade71
LP
4244
4245 svm->vmcb->save.rax = vcpu->arch.regs[VCPU_REGS_RAX];
4246 svm->vmcb->save.rsp = vcpu->arch.regs[VCPU_REGS_RSP];
4247 svm->vmcb->save.rip = vcpu->arch.regs[VCPU_REGS_RIP];
4248
4249 ret = nested_svm_vmexit(svm);
4250 if (ret)
4251 return ret;
4252 }
0234bf88
LP
4253 return 0;
4254}
4255
ed19321f 4256static int svm_pre_leave_smm(struct kvm_vcpu *vcpu, const char *smstate)
0234bf88 4257{
05cade71 4258 struct vcpu_svm *svm = to_svm(vcpu);
8c5fbf1a 4259 struct kvm_host_map map;
59cd9bc5 4260 int ret = 0;
05cade71 4261
3ebb5d26
ML
4262 if (guest_cpuid_has(vcpu, X86_FEATURE_LM)) {
4263 u64 saved_efer = GET_SMSTATE(u64, smstate, 0x7ed0);
4264 u64 guest = GET_SMSTATE(u64, smstate, 0x7ed8);
0dd16b5b 4265 u64 vmcb12_gpa = GET_SMSTATE(u64, smstate, 0x7ee0);
05cade71 4266
3ebb5d26
ML
4267 if (guest) {
4268 if (!guest_cpuid_has(vcpu, X86_FEATURE_SVM))
4269 return 1;
4270
4271 if (!(saved_efer & EFER_SVME))
4272 return 1;
4273
4274 if (kvm_vcpu_map(&svm->vcpu,
0dd16b5b 4275 gpa_to_gfn(vmcb12_gpa), &map) == -EINVAL)
3ebb5d26
ML
4276 return 1;
4277
2fcf4876 4278 if (svm_allocate_nested(svm))
3ebb5d26
ML
4279 return 1;
4280
0dd16b5b 4281 ret = enter_svm_guest_mode(svm, vmcb12_gpa, map.hva);
3ebb5d26
ML
4282 kvm_vcpu_unmap(&svm->vcpu, &map, true);
4283 }
05cade71 4284 }
59cd9bc5
VK
4285
4286 return ret;
0234bf88
LP
4287}
4288
c9d40913 4289static void enable_smi_window(struct kvm_vcpu *vcpu)
cc3d967f
LP
4290{
4291 struct vcpu_svm *svm = to_svm(vcpu);
4292
4293 if (!gif_set(svm)) {
4294 if (vgif_enabled(svm))
a284ba56 4295 svm_set_intercept(svm, INTERCEPT_STGI);
cc3d967f 4296 /* STGI will cause a vm exit */
c9d40913
PB
4297 } else {
4298 /* We must be in SMM; RSM will cause a vmexit anyway. */
cc3d967f 4299 }
cc3d967f
LP
4300}
4301
09e3e2a1 4302static bool svm_can_emulate_instruction(struct kvm_vcpu *vcpu, void *insn, int insn_len)
05d5a486 4303{
09e3e2a1
SC
4304 bool smep, smap, is_user;
4305 unsigned long cr4;
e72436bc 4306
bc624d9f
TL
4307 /*
4308 * When the guest is an SEV-ES guest, emulation is not possible.
4309 */
4310 if (sev_es_guest(vcpu->kvm))
4311 return false;
4312
05d5a486 4313 /*
118154bd
LA
4314 * Detect and workaround Errata 1096 Fam_17h_00_0Fh.
4315 *
4316 * Errata:
4317 * When CPU raise #NPF on guest data access and vCPU CR4.SMAP=1, it is
4318 * possible that CPU microcode implementing DecodeAssist will fail
4319 * to read bytes of instruction which caused #NPF. In this case,
4320 * GuestIntrBytes field of the VMCB on a VMEXIT will incorrectly
4321 * return 0 instead of the correct guest instruction bytes.
4322 *
4323 * This happens because CPU microcode reading instruction bytes
4324 * uses a special opcode which attempts to read data using CPL=0
4325 * priviledges. The microcode reads CS:RIP and if it hits a SMAP
4326 * fault, it gives up and returns no instruction bytes.
4327 *
4328 * Detection:
4329 * We reach here in case CPU supports DecodeAssist, raised #NPF and
4330 * returned 0 in GuestIntrBytes field of the VMCB.
4331 * First, errata can only be triggered in case vCPU CR4.SMAP=1.
4332 * Second, if vCPU CR4.SMEP=1, errata could only be triggered
4333 * in case vCPU CPL==3 (Because otherwise guest would have triggered
4334 * a SMEP fault instead of #NPF).
4335 * Otherwise, vCPU CR4.SMEP=0, errata could be triggered by any vCPU CPL.
4336 * As most guests enable SMAP if they have also enabled SMEP, use above
4337 * logic in order to attempt minimize false-positive of detecting errata
4338 * while still preserving all cases semantic correctness.
4339 *
4340 * Workaround:
4341 * To determine what instruction the guest was executing, the hypervisor
4342 * will have to decode the instruction at the instruction pointer.
05d5a486
SB
4343 *
4344 * In non SEV guest, hypervisor will be able to read the guest
4345 * memory to decode the instruction pointer when insn_len is zero
4346 * so we return true to indicate that decoding is possible.
4347 *
4348 * But in the SEV guest, the guest memory is encrypted with the
4349 * guest specific key and hypervisor will not be able to decode the
4350 * instruction pointer so we will not able to workaround it. Lets
4351 * print the error and request to kill the guest.
4352 */
09e3e2a1
SC
4353 if (likely(!insn || insn_len))
4354 return true;
4355
4356 /*
4357 * If RIP is invalid, go ahead with emulation which will cause an
4358 * internal error exit.
4359 */
4360 if (!kvm_vcpu_gfn_to_memslot(vcpu, kvm_rip_read(vcpu) >> PAGE_SHIFT))
4361 return true;
4362
4363 cr4 = kvm_read_cr4(vcpu);
4364 smep = cr4 & X86_CR4_SMEP;
4365 smap = cr4 & X86_CR4_SMAP;
4366 is_user = svm_get_cpl(vcpu) == 3;
118154bd 4367 if (smap && (!smep || is_user)) {
05d5a486
SB
4368 if (!sev_guest(vcpu->kvm))
4369 return true;
4370
118154bd 4371 pr_err_ratelimited("KVM: SEV Guest triggered AMD Erratum 1096\n");
05d5a486
SB
4372 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
4373 }
4374
4375 return false;
4376}
4377
4b9852f4
LA
4378static bool svm_apic_init_signal_blocked(struct kvm_vcpu *vcpu)
4379{
4380 struct vcpu_svm *svm = to_svm(vcpu);
4381
4382 /*
4383 * TODO: Last condition latch INIT signals on vCPU when
4384 * vCPU is in guest-mode and vmcb12 defines intercept on INIT.
33b22172
PB
4385 * To properly emulate the INIT intercept,
4386 * svm_check_nested_events() should call nested_svm_vmexit()
4387 * if an INIT signal is pending.
4b9852f4
LA
4388 */
4389 return !gif_set(svm) ||
c62e2e94 4390 (vmcb_is_intercept(&svm->vmcb->control, INTERCEPT_INIT));
4b9852f4
LA
4391}
4392
647daca2
TL
4393static void svm_vcpu_deliver_sipi_vector(struct kvm_vcpu *vcpu, u8 vector)
4394{
4395 if (!sev_es_guest(vcpu->kvm))
4396 return kvm_vcpu_deliver_sipi_vector(vcpu, vector);
4397
4398 sev_vcpu_deliver_sipi_vector(vcpu, vector);
4399}
4400
eaf78265
JR
4401static void svm_vm_destroy(struct kvm *kvm)
4402{
4403 avic_vm_destroy(kvm);
4404 sev_vm_destroy(kvm);
4405}
4406
4407static int svm_vm_init(struct kvm *kvm)
4408{
830f01b0
WL
4409 if (!pause_filter_count || !pause_filter_thresh)
4410 kvm->arch.pause_in_guest = true;
4411
eaf78265
JR
4412 if (avic) {
4413 int ret = avic_vm_init(kvm);
4414 if (ret)
4415 return ret;
4416 }
4417
4418 kvm_apicv_init(kvm, avic);
4419 return 0;
4420}
4421
9c14ee21 4422static struct kvm_x86_ops svm_x86_ops __initdata = {
dd58f3c9 4423 .hardware_unsetup = svm_hardware_teardown,
6aa8b732
AK
4424 .hardware_enable = svm_hardware_enable,
4425 .hardware_disable = svm_hardware_disable,
774ead3a 4426 .cpu_has_accelerated_tpr = svm_cpu_has_accelerated_tpr,
bc226f07 4427 .has_emulated_msr = svm_has_emulated_msr,
6aa8b732
AK
4428
4429 .vcpu_create = svm_create_vcpu,
4430 .vcpu_free = svm_free_vcpu,
04d2cc77 4431 .vcpu_reset = svm_vcpu_reset,
6aa8b732 4432
562b6b08 4433 .vm_size = sizeof(struct kvm_svm),
4e19c36f 4434 .vm_init = svm_vm_init,
1654efcb 4435 .vm_destroy = svm_vm_destroy,
44a95dae 4436
04d2cc77 4437 .prepare_guest_switch = svm_prepare_guest_switch,
6aa8b732
AK
4438 .vcpu_load = svm_vcpu_load,
4439 .vcpu_put = svm_vcpu_put,
8221c137
SS
4440 .vcpu_blocking = svm_vcpu_blocking,
4441 .vcpu_unblocking = svm_vcpu_unblocking,
6aa8b732 4442
6986982f 4443 .update_exception_bitmap = update_exception_bitmap,
801e459a 4444 .get_msr_feature = svm_get_msr_feature,
6aa8b732
AK
4445 .get_msr = svm_get_msr,
4446 .set_msr = svm_set_msr,
4447 .get_segment_base = svm_get_segment_base,
4448 .get_segment = svm_get_segment,
4449 .set_segment = svm_set_segment,
2e4d2653 4450 .get_cpl = svm_get_cpl,
1747fb71 4451 .get_cs_db_l_bits = kvm_get_cs_db_l_bits,
6aa8b732 4452 .set_cr0 = svm_set_cr0,
c2fe3cd4 4453 .is_valid_cr4 = svm_is_valid_cr4,
6aa8b732
AK
4454 .set_cr4 = svm_set_cr4,
4455 .set_efer = svm_set_efer,
4456 .get_idt = svm_get_idt,
4457 .set_idt = svm_set_idt,
4458 .get_gdt = svm_get_gdt,
4459 .set_gdt = svm_set_gdt,
020df079 4460 .set_dr7 = svm_set_dr7,
facb0139 4461 .sync_dirty_debug_regs = svm_sync_dirty_debug_regs,
6de4f3ad 4462 .cache_reg = svm_cache_reg,
6aa8b732
AK
4463 .get_rflags = svm_get_rflags,
4464 .set_rflags = svm_set_rflags,
be94f6b7 4465
7780938c 4466 .tlb_flush_all = svm_flush_tlb,
eeeb4f67 4467 .tlb_flush_current = svm_flush_tlb,
faff8758 4468 .tlb_flush_gva = svm_flush_tlb_gva,
72b38320 4469 .tlb_flush_guest = svm_flush_tlb,
6aa8b732 4470
6aa8b732 4471 .run = svm_vcpu_run,
04d2cc77 4472 .handle_exit = handle_exit,
6aa8b732 4473 .skip_emulated_instruction = skip_emulated_instruction,
5ef8acbd 4474 .update_emulated_instruction = NULL,
2809f5d2
GC
4475 .set_interrupt_shadow = svm_set_interrupt_shadow,
4476 .get_interrupt_shadow = svm_get_interrupt_shadow,
102d8325 4477 .patch_hypercall = svm_patch_hypercall,
2a8067f1 4478 .set_irq = svm_set_irq,
95ba8273 4479 .set_nmi = svm_inject_nmi,
298101da 4480 .queue_exception = svm_queue_exception,
b463a6f7 4481 .cancel_injection = svm_cancel_injection,
78646121 4482 .interrupt_allowed = svm_interrupt_allowed,
95ba8273 4483 .nmi_allowed = svm_nmi_allowed,
3cfc3092
JK
4484 .get_nmi_mask = svm_get_nmi_mask,
4485 .set_nmi_mask = svm_set_nmi_mask,
95ba8273
GN
4486 .enable_nmi_window = enable_nmi_window,
4487 .enable_irq_window = enable_irq_window,
4488 .update_cr8_intercept = update_cr8_intercept,
8d860bbe 4489 .set_virtual_apic_mode = svm_set_virtual_apic_mode,
d62caabb 4490 .refresh_apicv_exec_ctrl = svm_refresh_apicv_exec_ctrl,
ef8efd7a 4491 .check_apicv_inhibit_reasons = svm_check_apicv_inhibit_reasons,
2de9d0cc 4492 .pre_update_apicv_exec_ctrl = svm_pre_update_apicv_exec_ctrl,
c7c9c56c 4493 .load_eoi_exitmap = svm_load_eoi_exitmap,
44a95dae
SS
4494 .hwapic_irr_update = svm_hwapic_irr_update,
4495 .hwapic_isr_update = svm_hwapic_isr_update,
fa59cc00 4496 .sync_pir_to_irr = kvm_lapic_find_highest_irr,
be8ca170 4497 .apicv_post_state_restore = avic_post_state_restore,
cbc94022
IE
4498
4499 .set_tss_addr = svm_set_tss_addr,
2ac52ab8 4500 .set_identity_map_addr = svm_set_identity_map_addr,
4b12f0de 4501 .get_mt_mask = svm_get_mt_mask,
229456fc 4502
586f9607 4503 .get_exit_info = svm_get_exit_info,
586f9607 4504
7c1b761b 4505 .vcpu_after_set_cpuid = svm_vcpu_after_set_cpuid,
4e47c7a6 4506
f5f48ee1 4507 .has_wbinvd_exit = svm_has_wbinvd_exit,
99e3e30a 4508
326e7425 4509 .write_l1_tsc_offset = svm_write_l1_tsc_offset,
1c97f0a0 4510
727a7e27 4511 .load_mmu_pgd = svm_load_mmu_pgd,
8a76d7f2
JR
4512
4513 .check_intercept = svm_check_intercept,
95b5a48c 4514 .handle_exit_irqoff = svm_handle_exit_irqoff,
ae97a3b8 4515
d264ee0c
SC
4516 .request_immediate_exit = __kvm_request_immediate_exit,
4517
ae97a3b8 4518 .sched_in = svm_sched_in,
25462f7f
WH
4519
4520 .pmu_ops = &amd_pmu_ops,
33b22172
PB
4521 .nested_ops = &svm_nested_ops,
4522
340d3bc3 4523 .deliver_posted_interrupt = svm_deliver_avic_intr,
17e433b5 4524 .dy_apicv_has_pending_interrupt = svm_dy_apicv_has_pending_interrupt,
411b44ba 4525 .update_pi_irte = svm_update_pi_irte,
74f16909 4526 .setup_mce = svm_setup_mce,
0234bf88 4527
72d7b374 4528 .smi_allowed = svm_smi_allowed,
0234bf88
LP
4529 .pre_enter_smm = svm_pre_enter_smm,
4530 .pre_leave_smm = svm_pre_leave_smm,
cc3d967f 4531 .enable_smi_window = enable_smi_window,
1654efcb
BS
4532
4533 .mem_enc_op = svm_mem_enc_op,
1e80fdc0
BS
4534 .mem_enc_reg_region = svm_register_enc_region,
4535 .mem_enc_unreg_region = svm_unregister_enc_region,
57b119da 4536
09e3e2a1 4537 .can_emulate_instruction = svm_can_emulate_instruction,
4b9852f4
LA
4538
4539 .apic_init_signal_blocked = svm_apic_init_signal_blocked,
fd6fa73d
AG
4540
4541 .msr_filter_changed = svm_msr_filter_changed,
f1c6366e 4542 .complete_emulated_msr = svm_complete_emulated_msr,
647daca2
TL
4543
4544 .vcpu_deliver_sipi_vector = svm_vcpu_deliver_sipi_vector,
6aa8b732
AK
4545};
4546
d008dfdb
SC
4547static struct kvm_x86_init_ops svm_init_ops __initdata = {
4548 .cpu_has_kvm_support = has_svm,
4549 .disabled_by_bios = is_disabled,
4550 .hardware_setup = svm_hardware_setup,
4551 .check_processor_compatibility = svm_check_processor_compat,
4552
4553 .runtime_ops = &svm_x86_ops,
6aa8b732
AK
4554};
4555
4556static int __init svm_init(void)
4557{
d07f46f9
TL
4558 __unused_size_checks();
4559
d008dfdb 4560 return kvm_init(&svm_init_ops, sizeof(struct vcpu_svm),
0ee75bea 4561 __alignof__(struct vcpu_svm), THIS_MODULE);
6aa8b732
AK
4562}
4563
4564static void __exit svm_exit(void)
4565{
cb498ea2 4566 kvm_exit();
6aa8b732
AK
4567}
4568
4569module_init(svm_init)
4570module_exit(svm_exit)