]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - arch/x86/kvm/svm.c
x86/bugs, KVM: Support the combination of guest and host IBRS
[mirror_ubuntu-bionic-kernel.git] / arch / x86 / kvm / svm.c
CommitLineData
6aa8b732
AK
1/*
2 * Kernel-based Virtual Machine driver for Linux
3 *
4 * AMD SVM support
5 *
6 * Copyright (C) 2006 Qumranet, Inc.
9611c187 7 * Copyright 2010 Red Hat, Inc. and/or its affiliates.
6aa8b732
AK
8 *
9 * Authors:
10 * Yaniv Kamay <yaniv@qumranet.com>
11 * Avi Kivity <avi@qumranet.com>
12 *
13 * This work is licensed under the terms of the GNU GPL, version 2. See
14 * the COPYING file in the top-level directory.
15 *
16 */
44a95dae
SS
17
18#define pr_fmt(fmt) "SVM: " fmt
19
edf88417
AK
20#include <linux/kvm_host.h>
21
85f455f7 22#include "irq.h"
1d737c8a 23#include "mmu.h"
5fdbf976 24#include "kvm_cache_regs.h"
fe4c7b19 25#include "x86.h"
66f7b72e 26#include "cpuid.h"
25462f7f 27#include "pmu.h"
e495606d 28
6aa8b732 29#include <linux/module.h>
ae759544 30#include <linux/mod_devicetable.h>
9d8f549d 31#include <linux/kernel.h>
6aa8b732
AK
32#include <linux/vmalloc.h>
33#include <linux/highmem.h>
e8edc6e0 34#include <linux/sched.h>
af658dca 35#include <linux/trace_events.h>
5a0e3ad6 36#include <linux/slab.h>
5881f737
SS
37#include <linux/amd-iommu.h>
38#include <linux/hashtable.h>
c207aee4 39#include <linux/frame.h>
6aa8b732 40
8221c137 41#include <asm/apic.h>
1018faa6 42#include <asm/perf_event.h>
67ec6607 43#include <asm/tlbflush.h>
e495606d 44#include <asm/desc.h>
facb0139 45#include <asm/debugreg.h>
631bc487 46#include <asm/kvm_para.h>
411b44ba 47#include <asm/irq_remapping.h>
f471d71b 48#include <asm/microcode.h>
117cc7a9 49#include <asm/nospec-branch.h>
6aa8b732 50
63d1142f 51#include <asm/virtext.h>
229456fc 52#include "trace.h"
63d1142f 53
4ecac3fd
AK
54#define __ex(x) __kvm_handle_fault_on_reboot(x)
55
6aa8b732
AK
56MODULE_AUTHOR("Qumranet");
57MODULE_LICENSE("GPL");
58
ae759544
JT
59static const struct x86_cpu_id svm_cpu_id[] = {
60 X86_FEATURE_MATCH(X86_FEATURE_SVM),
61 {}
62};
63MODULE_DEVICE_TABLE(x86cpu, svm_cpu_id);
64
6aa8b732
AK
65#define IOPM_ALLOC_ORDER 2
66#define MSRPM_ALLOC_ORDER 1
67
6aa8b732
AK
68#define SEG_TYPE_LDT 2
69#define SEG_TYPE_BUSY_TSS16 3
70
6bc31bdc
AP
71#define SVM_FEATURE_NPT (1 << 0)
72#define SVM_FEATURE_LBRV (1 << 1)
73#define SVM_FEATURE_SVML (1 << 2)
74#define SVM_FEATURE_NRIP (1 << 3)
ddce97aa
AP
75#define SVM_FEATURE_TSC_RATE (1 << 4)
76#define SVM_FEATURE_VMCB_CLEAN (1 << 5)
77#define SVM_FEATURE_FLUSH_ASID (1 << 6)
78#define SVM_FEATURE_DECODE_ASSIST (1 << 7)
6bc31bdc 79#define SVM_FEATURE_PAUSE_FILTER (1 << 10)
80b7706e 80
340d3bc3
SS
81#define SVM_AVIC_DOORBELL 0xc001011b
82
410e4d57
JR
83#define NESTED_EXIT_HOST 0 /* Exit handled on host level */
84#define NESTED_EXIT_DONE 1 /* Exit caused nested vmexit */
85#define NESTED_EXIT_CONTINUE 2 /* Further checks needed */
86
24e09cbf
JR
87#define DEBUGCTL_RESERVED_BITS (~(0x3fULL))
88
fbc0db76 89#define TSC_RATIO_RSVD 0xffffff0000000000ULL
92a1f12d
JR
90#define TSC_RATIO_MIN 0x0000000000000001ULL
91#define TSC_RATIO_MAX 0x000000ffffffffffULL
fbc0db76 92
5446a979 93#define AVIC_HPA_MASK ~((0xFFFULL << 52) | 0xFFF)
44a95dae
SS
94
95/*
96 * 0xff is broadcast, so the max index allowed for physical APIC ID
97 * table is 0xfe. APIC IDs above 0xff are reserved.
98 */
99#define AVIC_MAX_PHYSICAL_ID_COUNT 255
100
18f40c53
SS
101#define AVIC_UNACCEL_ACCESS_WRITE_MASK 1
102#define AVIC_UNACCEL_ACCESS_OFFSET_MASK 0xFF0
103#define AVIC_UNACCEL_ACCESS_VECTOR_MASK 0xFFFFFFFF
104
5ea11f2b
SS
105/* AVIC GATAG is encoded using VM and VCPU IDs */
106#define AVIC_VCPU_ID_BITS 8
107#define AVIC_VCPU_ID_MASK ((1 << AVIC_VCPU_ID_BITS) - 1)
108
109#define AVIC_VM_ID_BITS 24
110#define AVIC_VM_ID_NR (1 << AVIC_VM_ID_BITS)
111#define AVIC_VM_ID_MASK ((1 << AVIC_VM_ID_BITS) - 1)
112
113#define AVIC_GATAG(x, y) (((x & AVIC_VM_ID_MASK) << AVIC_VCPU_ID_BITS) | \
114 (y & AVIC_VCPU_ID_MASK))
115#define AVIC_GATAG_TO_VMID(x) ((x >> AVIC_VCPU_ID_BITS) & AVIC_VM_ID_MASK)
116#define AVIC_GATAG_TO_VCPUID(x) (x & AVIC_VCPU_ID_MASK)
117
67ec6607
JR
118static bool erratum_383_found __read_mostly;
119
6c8166a7
AK
120static const u32 host_save_user_msrs[] = {
121#ifdef CONFIG_X86_64
122 MSR_STAR, MSR_LSTAR, MSR_CSTAR, MSR_SYSCALL_MASK, MSR_KERNEL_GS_BASE,
123 MSR_FS_BASE,
124#endif
125 MSR_IA32_SYSENTER_CS, MSR_IA32_SYSENTER_ESP, MSR_IA32_SYSENTER_EIP,
46896c73 126 MSR_TSC_AUX,
6c8166a7
AK
127};
128
129#define NR_HOST_SAVE_USER_MSRS ARRAY_SIZE(host_save_user_msrs)
130
131struct kvm_vcpu;
132
e6aa9abd
JR
133struct nested_state {
134 struct vmcb *hsave;
135 u64 hsave_msr;
4a810181 136 u64 vm_cr_msr;
e6aa9abd
JR
137 u64 vmcb;
138
139 /* These are the merged vectors */
140 u32 *msrpm;
141
142 /* gpa pointers to the real vectors */
143 u64 vmcb_msrpm;
ce2ac085 144 u64 vmcb_iopm;
aad42c64 145
cd3ff653
JR
146 /* A VMEXIT is required but not yet emulated */
147 bool exit_required;
148
aad42c64 149 /* cache for intercepts of the guest */
4ee546b4 150 u32 intercept_cr;
3aed041a 151 u32 intercept_dr;
aad42c64
JR
152 u32 intercept_exceptions;
153 u64 intercept;
154
5bd2edc3
JR
155 /* Nested Paging related state */
156 u64 nested_cr3;
e6aa9abd
JR
157};
158
323c3d80
JR
159#define MSRPM_OFFSETS 16
160static u32 msrpm_offsets[MSRPM_OFFSETS] __read_mostly;
161
2b036c6b
BO
162/*
163 * Set osvw_len to higher value when updated Revision Guides
164 * are published and we know what the new status bits are
165 */
166static uint64_t osvw_len = 4, osvw_status;
167
6c8166a7
AK
168struct vcpu_svm {
169 struct kvm_vcpu vcpu;
170 struct vmcb *vmcb;
171 unsigned long vmcb_pa;
172 struct svm_cpu_data *svm_data;
173 uint64_t asid_generation;
174 uint64_t sysenter_esp;
175 uint64_t sysenter_eip;
46896c73 176 uint64_t tsc_aux;
6c8166a7
AK
177
178 u64 next_rip;
179
180 u64 host_user_msrs[NR_HOST_SAVE_USER_MSRS];
afe9e66f 181 struct {
dacccfdd
AK
182 u16 fs;
183 u16 gs;
184 u16 ldt;
afe9e66f
AK
185 u64 gs_base;
186 } host;
6c8166a7 187
a57b802c
KA
188 u64 spec_ctrl;
189
6c8166a7 190 u32 *msrpm;
6c8166a7 191
bd3d1ec3
AK
192 ulong nmi_iret_rip;
193
e6aa9abd 194 struct nested_state nested;
6be7d306
JK
195
196 bool nmi_singlestep;
ab2f4d73 197 u64 nmi_singlestep_guest_rflags;
66b7138f
JK
198
199 unsigned int3_injected;
200 unsigned long int3_rip;
fbc0db76 201
6092d3d3
JR
202 /* cached guest cpuid flags for faster access */
203 bool nrips_enabled : 1;
44a95dae 204
18f40c53 205 u32 ldr_reg;
44a95dae
SS
206 struct page *avic_backing_page;
207 u64 *avic_physical_id_cache;
8221c137 208 bool avic_is_running;
411b44ba
SS
209
210 /*
211 * Per-vcpu list of struct amd_svm_iommu_ir:
212 * This is used mainly to store interrupt remapping information used
213 * when update the vcpu affinity. This avoids the need to scan for
214 * IRTE and try to match ga_tag in the IOMMU driver.
215 */
216 struct list_head ir_list;
217 spinlock_t ir_list_lock;
218};
219
220/*
221 * This is a wrapper of struct amd_iommu_ir_data.
222 */
223struct amd_svm_iommu_ir {
224 struct list_head node; /* Used by SVM for per-vcpu ir_list */
225 void *data; /* Storing pointer to struct amd_ir_data */
6c8166a7
AK
226};
227
44a95dae
SS
228#define AVIC_LOGICAL_ID_ENTRY_GUEST_PHYSICAL_ID_MASK (0xFF)
229#define AVIC_LOGICAL_ID_ENTRY_VALID_MASK (1 << 31)
230
231#define AVIC_PHYSICAL_ID_ENTRY_HOST_PHYSICAL_ID_MASK (0xFFULL)
232#define AVIC_PHYSICAL_ID_ENTRY_BACKING_PAGE_MASK (0xFFFFFFFFFFULL << 12)
233#define AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK (1ULL << 62)
234#define AVIC_PHYSICAL_ID_ENTRY_VALID_MASK (1ULL << 63)
235
fbc0db76
JR
236static DEFINE_PER_CPU(u64, current_tsc_ratio);
237#define TSC_RATIO_DEFAULT 0x0100000000ULL
238
455716fa
JR
239#define MSR_INVALID 0xffffffffU
240
09941fbb 241static const struct svm_direct_access_msrs {
ac72a9b7
JR
242 u32 index; /* Index of the MSR */
243 bool always; /* True if intercept is always on */
244} direct_access_msrs[] = {
8c06585d 245 { .index = MSR_STAR, .always = true },
ac72a9b7
JR
246 { .index = MSR_IA32_SYSENTER_CS, .always = true },
247#ifdef CONFIG_X86_64
248 { .index = MSR_GS_BASE, .always = true },
249 { .index = MSR_FS_BASE, .always = true },
250 { .index = MSR_KERNEL_GS_BASE, .always = true },
251 { .index = MSR_LSTAR, .always = true },
252 { .index = MSR_CSTAR, .always = true },
253 { .index = MSR_SYSCALL_MASK, .always = true },
254#endif
a57b802c 255 { .index = MSR_IA32_SPEC_CTRL, .always = false },
33241bfe 256 { .index = MSR_IA32_PRED_CMD, .always = false },
ac72a9b7
JR
257 { .index = MSR_IA32_LASTBRANCHFROMIP, .always = false },
258 { .index = MSR_IA32_LASTBRANCHTOIP, .always = false },
259 { .index = MSR_IA32_LASTINTFROMIP, .always = false },
260 { .index = MSR_IA32_LASTINTTOIP, .always = false },
261 { .index = MSR_INVALID, .always = false },
6c8166a7
AK
262};
263
709ddebf
JR
264/* enable NPT for AMD64 and X86 with PAE */
265#if defined(CONFIG_X86_64) || defined(CONFIG_X86_PAE)
266static bool npt_enabled = true;
267#else
e0231715 268static bool npt_enabled;
709ddebf 269#endif
6c7dac72 270
e2358851
DB
271/* allow nested paging (virtualized MMU) for all guests */
272static int npt = true;
6c7dac72 273module_param(npt, int, S_IRUGO);
e3da3acd 274
e2358851
DB
275/* allow nested virtualization in KVM/SVM */
276static int nested = true;
236de055
AG
277module_param(nested, int, S_IRUGO);
278
44a95dae
SS
279/* enable / disable AVIC */
280static int avic;
5b8abf1f 281#ifdef CONFIG_X86_LOCAL_APIC
44a95dae 282module_param(avic, int, S_IRUGO);
5b8abf1f 283#endif
44a95dae 284
89c8a498
JN
285/* enable/disable Virtual VMLOAD VMSAVE */
286static int vls = true;
287module_param(vls, int, 0444);
288
640bd6e5
JN
289/* enable/disable Virtual GIF */
290static int vgif = true;
291module_param(vgif, int, 0444);
5ea11f2b 292
79a8059d 293static void svm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0);
44874f84 294static void svm_flush_tlb(struct kvm_vcpu *vcpu);
a5c3832d 295static void svm_complete_interrupts(struct vcpu_svm *svm);
04d2cc77 296
410e4d57 297static int nested_svm_exit_handled(struct vcpu_svm *svm);
b8e88bc8 298static int nested_svm_intercept(struct vcpu_svm *svm);
cf74a78b 299static int nested_svm_vmexit(struct vcpu_svm *svm);
cf74a78b
AG
300static int nested_svm_check_exception(struct vcpu_svm *svm, unsigned nr,
301 bool has_error_code, u32 error_code);
302
8d28fec4 303enum {
116a0a23
JR
304 VMCB_INTERCEPTS, /* Intercept vectors, TSC offset,
305 pause filter count */
f56838e4 306 VMCB_PERM_MAP, /* IOPM Base and MSRPM Base */
d48086d1 307 VMCB_ASID, /* ASID */
decdbf6a 308 VMCB_INTR, /* int_ctl, int_vector */
b2747166 309 VMCB_NPT, /* npt_en, nCR3, gPAT */
dcca1a65 310 VMCB_CR, /* CR0, CR3, CR4, EFER */
72214b96 311 VMCB_DR, /* DR6, DR7 */
17a703cb 312 VMCB_DT, /* GDT, IDT */
060d0c9a 313 VMCB_SEG, /* CS, DS, SS, ES, CPL */
0574dec0 314 VMCB_CR2, /* CR2 only */
b53ba3f9 315 VMCB_LBR, /* DBGCTL, BR_FROM, BR_TO, LAST_EX_FROM, LAST_EX_TO */
44a95dae
SS
316 VMCB_AVIC, /* AVIC APIC_BAR, AVIC APIC_BACKING_PAGE,
317 * AVIC PHYSICAL_TABLE pointer,
318 * AVIC LOGICAL_TABLE pointer
319 */
8d28fec4
RJ
320 VMCB_DIRTY_MAX,
321};
322
0574dec0
JR
323/* TPR and CR2 are always written before VMRUN */
324#define VMCB_ALWAYS_DIRTY_MASK ((1U << VMCB_INTR) | (1U << VMCB_CR2))
8d28fec4 325
44a95dae
SS
326#define VMCB_AVIC_APIC_BAR_MASK 0xFFFFFFFFFF000ULL
327
8d28fec4
RJ
328static inline void mark_all_dirty(struct vmcb *vmcb)
329{
330 vmcb->control.clean = 0;
331}
332
333static inline void mark_all_clean(struct vmcb *vmcb)
334{
335 vmcb->control.clean = ((1 << VMCB_DIRTY_MAX) - 1)
336 & ~VMCB_ALWAYS_DIRTY_MASK;
337}
338
339static inline void mark_dirty(struct vmcb *vmcb, int bit)
340{
341 vmcb->control.clean &= ~(1 << bit);
342}
343
a2fa3e9f
GH
344static inline struct vcpu_svm *to_svm(struct kvm_vcpu *vcpu)
345{
fb3f0f51 346 return container_of(vcpu, struct vcpu_svm, vcpu);
a2fa3e9f
GH
347}
348
44a95dae
SS
349static inline void avic_update_vapic_bar(struct vcpu_svm *svm, u64 data)
350{
351 svm->vmcb->control.avic_vapic_bar = data & VMCB_AVIC_APIC_BAR_MASK;
352 mark_dirty(svm->vmcb, VMCB_AVIC);
353}
354
340d3bc3
SS
355static inline bool avic_vcpu_is_running(struct kvm_vcpu *vcpu)
356{
357 struct vcpu_svm *svm = to_svm(vcpu);
358 u64 *entry = svm->avic_physical_id_cache;
359
360 if (!entry)
361 return false;
362
363 return (READ_ONCE(*entry) & AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK);
364}
365
384c6368
JR
366static void recalc_intercepts(struct vcpu_svm *svm)
367{
368 struct vmcb_control_area *c, *h;
369 struct nested_state *g;
370
116a0a23
JR
371 mark_dirty(svm->vmcb, VMCB_INTERCEPTS);
372
384c6368
JR
373 if (!is_guest_mode(&svm->vcpu))
374 return;
375
376 c = &svm->vmcb->control;
377 h = &svm->nested.hsave->control;
378 g = &svm->nested;
379
4ee546b4 380 c->intercept_cr = h->intercept_cr | g->intercept_cr;
3aed041a 381 c->intercept_dr = h->intercept_dr | g->intercept_dr;
bd89525a 382 c->intercept_exceptions = h->intercept_exceptions | g->intercept_exceptions;
384c6368
JR
383 c->intercept = h->intercept | g->intercept;
384}
385
4ee546b4
RJ
386static inline struct vmcb *get_host_vmcb(struct vcpu_svm *svm)
387{
388 if (is_guest_mode(&svm->vcpu))
389 return svm->nested.hsave;
390 else
391 return svm->vmcb;
392}
393
394static inline void set_cr_intercept(struct vcpu_svm *svm, int bit)
395{
396 struct vmcb *vmcb = get_host_vmcb(svm);
397
398 vmcb->control.intercept_cr |= (1U << bit);
399
400 recalc_intercepts(svm);
401}
402
403static inline void clr_cr_intercept(struct vcpu_svm *svm, int bit)
404{
405 struct vmcb *vmcb = get_host_vmcb(svm);
406
407 vmcb->control.intercept_cr &= ~(1U << bit);
408
409 recalc_intercepts(svm);
410}
411
412static inline bool is_cr_intercept(struct vcpu_svm *svm, int bit)
413{
414 struct vmcb *vmcb = get_host_vmcb(svm);
415
416 return vmcb->control.intercept_cr & (1U << bit);
417}
418
5315c716 419static inline void set_dr_intercepts(struct vcpu_svm *svm)
3aed041a
JR
420{
421 struct vmcb *vmcb = get_host_vmcb(svm);
422
5315c716
PB
423 vmcb->control.intercept_dr = (1 << INTERCEPT_DR0_READ)
424 | (1 << INTERCEPT_DR1_READ)
425 | (1 << INTERCEPT_DR2_READ)
426 | (1 << INTERCEPT_DR3_READ)
427 | (1 << INTERCEPT_DR4_READ)
428 | (1 << INTERCEPT_DR5_READ)
429 | (1 << INTERCEPT_DR6_READ)
430 | (1 << INTERCEPT_DR7_READ)
431 | (1 << INTERCEPT_DR0_WRITE)
432 | (1 << INTERCEPT_DR1_WRITE)
433 | (1 << INTERCEPT_DR2_WRITE)
434 | (1 << INTERCEPT_DR3_WRITE)
435 | (1 << INTERCEPT_DR4_WRITE)
436 | (1 << INTERCEPT_DR5_WRITE)
437 | (1 << INTERCEPT_DR6_WRITE)
438 | (1 << INTERCEPT_DR7_WRITE);
3aed041a
JR
439
440 recalc_intercepts(svm);
441}
442
5315c716 443static inline void clr_dr_intercepts(struct vcpu_svm *svm)
3aed041a
JR
444{
445 struct vmcb *vmcb = get_host_vmcb(svm);
446
5315c716 447 vmcb->control.intercept_dr = 0;
3aed041a
JR
448
449 recalc_intercepts(svm);
450}
451
18c918c5
JR
452static inline void set_exception_intercept(struct vcpu_svm *svm, int bit)
453{
454 struct vmcb *vmcb = get_host_vmcb(svm);
455
456 vmcb->control.intercept_exceptions |= (1U << bit);
457
458 recalc_intercepts(svm);
459}
460
461static inline void clr_exception_intercept(struct vcpu_svm *svm, int bit)
462{
463 struct vmcb *vmcb = get_host_vmcb(svm);
464
465 vmcb->control.intercept_exceptions &= ~(1U << bit);
466
467 recalc_intercepts(svm);
468}
469
8a05a1b8
JR
470static inline void set_intercept(struct vcpu_svm *svm, int bit)
471{
472 struct vmcb *vmcb = get_host_vmcb(svm);
473
474 vmcb->control.intercept |= (1ULL << bit);
475
476 recalc_intercepts(svm);
477}
478
479static inline void clr_intercept(struct vcpu_svm *svm, int bit)
480{
481 struct vmcb *vmcb = get_host_vmcb(svm);
482
483 vmcb->control.intercept &= ~(1ULL << bit);
484
485 recalc_intercepts(svm);
486}
487
640bd6e5
JN
488static inline bool vgif_enabled(struct vcpu_svm *svm)
489{
490 return !!(svm->vmcb->control.int_ctl & V_GIF_ENABLE_MASK);
491}
492
2af9194d
JR
493static inline void enable_gif(struct vcpu_svm *svm)
494{
640bd6e5
JN
495 if (vgif_enabled(svm))
496 svm->vmcb->control.int_ctl |= V_GIF_MASK;
497 else
498 svm->vcpu.arch.hflags |= HF_GIF_MASK;
2af9194d
JR
499}
500
501static inline void disable_gif(struct vcpu_svm *svm)
502{
640bd6e5
JN
503 if (vgif_enabled(svm))
504 svm->vmcb->control.int_ctl &= ~V_GIF_MASK;
505 else
506 svm->vcpu.arch.hflags &= ~HF_GIF_MASK;
2af9194d
JR
507}
508
509static inline bool gif_set(struct vcpu_svm *svm)
510{
640bd6e5
JN
511 if (vgif_enabled(svm))
512 return !!(svm->vmcb->control.int_ctl & V_GIF_MASK);
513 else
514 return !!(svm->vcpu.arch.hflags & HF_GIF_MASK);
2af9194d
JR
515}
516
4866d5e3 517static unsigned long iopm_base;
6aa8b732
AK
518
519struct kvm_ldttss_desc {
520 u16 limit0;
521 u16 base0;
e0231715
JR
522 unsigned base1:8, type:5, dpl:2, p:1;
523 unsigned limit1:4, zero0:3, g:1, base2:8;
6aa8b732
AK
524 u32 base3;
525 u32 zero1;
526} __attribute__((packed));
527
528struct svm_cpu_data {
529 int cpu;
530
5008fdf5
AK
531 u64 asid_generation;
532 u32 max_asid;
533 u32 next_asid;
6aa8b732
AK
534 struct kvm_ldttss_desc *tss_desc;
535
536 struct page *save_area;
33241bfe 537 struct vmcb *current_vmcb;
6aa8b732
AK
538};
539
540static DEFINE_PER_CPU(struct svm_cpu_data *, svm_data);
541
542struct svm_init_data {
543 int cpu;
544 int r;
545};
546
09941fbb 547static const u32 msrpm_ranges[] = {0, 0xc0000000, 0xc0010000};
6aa8b732 548
9d8f549d 549#define NUM_MSR_MAPS ARRAY_SIZE(msrpm_ranges)
6aa8b732
AK
550#define MSRS_RANGE_SIZE 2048
551#define MSRS_IN_RANGE (MSRS_RANGE_SIZE * 8 / 2)
552
455716fa
JR
553static u32 svm_msrpm_offset(u32 msr)
554{
555 u32 offset;
556 int i;
557
558 for (i = 0; i < NUM_MSR_MAPS; i++) {
559 if (msr < msrpm_ranges[i] ||
560 msr >= msrpm_ranges[i] + MSRS_IN_RANGE)
561 continue;
562
563 offset = (msr - msrpm_ranges[i]) / 4; /* 4 msrs per u8 */
564 offset += (i * MSRS_RANGE_SIZE); /* add range offset */
565
566 /* Now we have the u8 offset - but need the u32 offset */
567 return offset / 4;
568 }
569
570 /* MSR not in any range */
571 return MSR_INVALID;
572}
573
6aa8b732
AK
574#define MAX_INST_SIZE 15
575
6aa8b732
AK
576static inline void clgi(void)
577{
4ecac3fd 578 asm volatile (__ex(SVM_CLGI));
6aa8b732
AK
579}
580
581static inline void stgi(void)
582{
4ecac3fd 583 asm volatile (__ex(SVM_STGI));
6aa8b732
AK
584}
585
586static inline void invlpga(unsigned long addr, u32 asid)
587{
e0231715 588 asm volatile (__ex(SVM_INVLPGA) : : "a"(addr), "c"(asid));
6aa8b732
AK
589}
590
855feb67 591static int get_npt_level(struct kvm_vcpu *vcpu)
4b16184c
JR
592{
593#ifdef CONFIG_X86_64
2a7266a8 594 return PT64_ROOT_4LEVEL;
4b16184c
JR
595#else
596 return PT32E_ROOT_LEVEL;
597#endif
598}
599
6aa8b732
AK
600static void svm_set_efer(struct kvm_vcpu *vcpu, u64 efer)
601{
6dc696d4 602 vcpu->arch.efer = efer;
709ddebf 603 if (!npt_enabled && !(efer & EFER_LMA))
2b5203ee 604 efer &= ~EFER_LME;
6aa8b732 605
9962d032 606 to_svm(vcpu)->vmcb->save.efer = efer | EFER_SVME;
dcca1a65 607 mark_dirty(to_svm(vcpu)->vmcb, VMCB_CR);
6aa8b732
AK
608}
609
6aa8b732
AK
610static int is_external_interrupt(u32 info)
611{
612 info &= SVM_EVTINJ_TYPE_MASK | SVM_EVTINJ_VALID;
613 return info == (SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_INTR);
614}
615
37ccdcbe 616static u32 svm_get_interrupt_shadow(struct kvm_vcpu *vcpu)
2809f5d2
GC
617{
618 struct vcpu_svm *svm = to_svm(vcpu);
619 u32 ret = 0;
620
621 if (svm->vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK)
37ccdcbe
PB
622 ret = KVM_X86_SHADOW_INT_STI | KVM_X86_SHADOW_INT_MOV_SS;
623 return ret;
2809f5d2
GC
624}
625
626static void svm_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
627{
628 struct vcpu_svm *svm = to_svm(vcpu);
629
630 if (mask == 0)
631 svm->vmcb->control.int_state &= ~SVM_INTERRUPT_SHADOW_MASK;
632 else
633 svm->vmcb->control.int_state |= SVM_INTERRUPT_SHADOW_MASK;
634
635}
636
6aa8b732
AK
637static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
638{
a2fa3e9f
GH
639 struct vcpu_svm *svm = to_svm(vcpu);
640
f104765b 641 if (svm->vmcb->control.next_rip != 0) {
d2922422 642 WARN_ON_ONCE(!static_cpu_has(X86_FEATURE_NRIPS));
6bc31bdc 643 svm->next_rip = svm->vmcb->control.next_rip;
f104765b 644 }
6bc31bdc 645
a2fa3e9f 646 if (!svm->next_rip) {
51d8b661 647 if (emulate_instruction(vcpu, EMULTYPE_SKIP) !=
f629cf84
GN
648 EMULATE_DONE)
649 printk(KERN_DEBUG "%s: NOP\n", __func__);
6aa8b732
AK
650 return;
651 }
5fdbf976
MT
652 if (svm->next_rip - kvm_rip_read(vcpu) > MAX_INST_SIZE)
653 printk(KERN_ERR "%s: ip 0x%lx next 0x%llx\n",
654 __func__, kvm_rip_read(vcpu), svm->next_rip);
6aa8b732 655
5fdbf976 656 kvm_rip_write(vcpu, svm->next_rip);
2809f5d2 657 svm_set_interrupt_shadow(vcpu, 0);
6aa8b732
AK
658}
659
cfcd20e5 660static void svm_queue_exception(struct kvm_vcpu *vcpu)
116a4752
JK
661{
662 struct vcpu_svm *svm = to_svm(vcpu);
cfcd20e5
WL
663 unsigned nr = vcpu->arch.exception.nr;
664 bool has_error_code = vcpu->arch.exception.has_error_code;
664f8e26 665 bool reinject = vcpu->arch.exception.injected;
cfcd20e5 666 u32 error_code = vcpu->arch.exception.error_code;
116a4752 667
e0231715
JR
668 /*
669 * If we are within a nested VM we'd better #VMEXIT and let the guest
670 * handle the exception
671 */
ce7ddec4
JR
672 if (!reinject &&
673 nested_svm_check_exception(svm, nr, has_error_code, error_code))
116a4752
JK
674 return;
675
2a6b20b8 676 if (nr == BP_VECTOR && !static_cpu_has(X86_FEATURE_NRIPS)) {
66b7138f
JK
677 unsigned long rip, old_rip = kvm_rip_read(&svm->vcpu);
678
679 /*
680 * For guest debugging where we have to reinject #BP if some
681 * INT3 is guest-owned:
682 * Emulate nRIP by moving RIP forward. Will fail if injection
683 * raises a fault that is not intercepted. Still better than
684 * failing in all cases.
685 */
686 skip_emulated_instruction(&svm->vcpu);
687 rip = kvm_rip_read(&svm->vcpu);
688 svm->int3_rip = rip + svm->vmcb->save.cs.base;
689 svm->int3_injected = rip - old_rip;
690 }
691
116a4752
JK
692 svm->vmcb->control.event_inj = nr
693 | SVM_EVTINJ_VALID
694 | (has_error_code ? SVM_EVTINJ_VALID_ERR : 0)
695 | SVM_EVTINJ_TYPE_EXEPT;
696 svm->vmcb->control.event_inj_err = error_code;
697}
698
67ec6607
JR
699static void svm_init_erratum_383(void)
700{
701 u32 low, high;
702 int err;
703 u64 val;
704
e6ee94d5 705 if (!static_cpu_has_bug(X86_BUG_AMD_TLB_MMATCH))
67ec6607
JR
706 return;
707
708 /* Use _safe variants to not break nested virtualization */
709 val = native_read_msr_safe(MSR_AMD64_DC_CFG, &err);
710 if (err)
711 return;
712
713 val |= (1ULL << 47);
714
715 low = lower_32_bits(val);
716 high = upper_32_bits(val);
717
718 native_write_msr_safe(MSR_AMD64_DC_CFG, low, high);
719
720 erratum_383_found = true;
721}
722
2b036c6b
BO
723static void svm_init_osvw(struct kvm_vcpu *vcpu)
724{
725 /*
726 * Guests should see errata 400 and 415 as fixed (assuming that
727 * HLT and IO instructions are intercepted).
728 */
729 vcpu->arch.osvw.length = (osvw_len >= 3) ? (osvw_len) : 3;
730 vcpu->arch.osvw.status = osvw_status & ~(6ULL);
731
732 /*
733 * By increasing VCPU's osvw.length to 3 we are telling the guest that
734 * all osvw.status bits inside that length, including bit 0 (which is
735 * reserved for erratum 298), are valid. However, if host processor's
736 * osvw_len is 0 then osvw_status[0] carries no information. We need to
737 * be conservative here and therefore we tell the guest that erratum 298
738 * is present (because we really don't know).
739 */
740 if (osvw_len == 0 && boot_cpu_data.x86 == 0x10)
741 vcpu->arch.osvw.status |= 1;
742}
743
6aa8b732
AK
744static int has_svm(void)
745{
63d1142f 746 const char *msg;
6aa8b732 747
63d1142f 748 if (!cpu_has_svm(&msg)) {
ff81ff10 749 printk(KERN_INFO "has_svm: %s\n", msg);
6aa8b732
AK
750 return 0;
751 }
752
6aa8b732
AK
753 return 1;
754}
755
13a34e06 756static void svm_hardware_disable(void)
6aa8b732 757{
fbc0db76
JR
758 /* Make sure we clean up behind us */
759 if (static_cpu_has(X86_FEATURE_TSCRATEMSR))
760 wrmsrl(MSR_AMD64_TSC_RATIO, TSC_RATIO_DEFAULT);
761
2c8dceeb 762 cpu_svm_disable();
1018faa6
JR
763
764 amd_pmu_disable_virt();
6aa8b732
AK
765}
766
13a34e06 767static int svm_hardware_enable(void)
6aa8b732
AK
768{
769
0fe1e009 770 struct svm_cpu_data *sd;
6aa8b732 771 uint64_t efer;
6aa8b732
AK
772 struct desc_struct *gdt;
773 int me = raw_smp_processor_id();
774
10474ae8
AG
775 rdmsrl(MSR_EFER, efer);
776 if (efer & EFER_SVME)
777 return -EBUSY;
778
6aa8b732 779 if (!has_svm()) {
1f5b77f5 780 pr_err("%s: err EOPNOTSUPP on %d\n", __func__, me);
10474ae8 781 return -EINVAL;
6aa8b732 782 }
0fe1e009 783 sd = per_cpu(svm_data, me);
0fe1e009 784 if (!sd) {
1f5b77f5 785 pr_err("%s: svm_data is NULL on %d\n", __func__, me);
10474ae8 786 return -EINVAL;
6aa8b732
AK
787 }
788
0fe1e009
TH
789 sd->asid_generation = 1;
790 sd->max_asid = cpuid_ebx(SVM_CPUID_FUNC) - 1;
791 sd->next_asid = sd->max_asid + 1;
6aa8b732 792
45fc8757 793 gdt = get_current_gdt_rw();
0fe1e009 794 sd->tss_desc = (struct kvm_ldttss_desc *)(gdt + GDT_ENTRY_TSS);
6aa8b732 795
9962d032 796 wrmsrl(MSR_EFER, efer | EFER_SVME);
6aa8b732 797
d0316554 798 wrmsrl(MSR_VM_HSAVE_PA, page_to_pfn(sd->save_area) << PAGE_SHIFT);
10474ae8 799
fbc0db76
JR
800 if (static_cpu_has(X86_FEATURE_TSCRATEMSR)) {
801 wrmsrl(MSR_AMD64_TSC_RATIO, TSC_RATIO_DEFAULT);
89cbc767 802 __this_cpu_write(current_tsc_ratio, TSC_RATIO_DEFAULT);
fbc0db76
JR
803 }
804
2b036c6b
BO
805
806 /*
807 * Get OSVW bits.
808 *
809 * Note that it is possible to have a system with mixed processor
810 * revisions and therefore different OSVW bits. If bits are not the same
811 * on different processors then choose the worst case (i.e. if erratum
812 * is present on one processor and not on another then assume that the
813 * erratum is present everywhere).
814 */
815 if (cpu_has(&boot_cpu_data, X86_FEATURE_OSVW)) {
816 uint64_t len, status = 0;
817 int err;
818
819 len = native_read_msr_safe(MSR_AMD64_OSVW_ID_LENGTH, &err);
820 if (!err)
821 status = native_read_msr_safe(MSR_AMD64_OSVW_STATUS,
822 &err);
823
824 if (err)
825 osvw_status = osvw_len = 0;
826 else {
827 if (len < osvw_len)
828 osvw_len = len;
829 osvw_status |= status;
830 osvw_status &= (1ULL << osvw_len) - 1;
831 }
832 } else
833 osvw_status = osvw_len = 0;
834
67ec6607
JR
835 svm_init_erratum_383();
836
1018faa6
JR
837 amd_pmu_enable_virt();
838
10474ae8 839 return 0;
6aa8b732
AK
840}
841
0da1db75
JR
842static void svm_cpu_uninit(int cpu)
843{
0fe1e009 844 struct svm_cpu_data *sd = per_cpu(svm_data, raw_smp_processor_id());
0da1db75 845
0fe1e009 846 if (!sd)
0da1db75
JR
847 return;
848
849 per_cpu(svm_data, raw_smp_processor_id()) = NULL;
0fe1e009
TH
850 __free_page(sd->save_area);
851 kfree(sd);
0da1db75
JR
852}
853
6aa8b732
AK
854static int svm_cpu_init(int cpu)
855{
0fe1e009 856 struct svm_cpu_data *sd;
6aa8b732
AK
857 int r;
858
0fe1e009
TH
859 sd = kzalloc(sizeof(struct svm_cpu_data), GFP_KERNEL);
860 if (!sd)
6aa8b732 861 return -ENOMEM;
0fe1e009
TH
862 sd->cpu = cpu;
863 sd->save_area = alloc_page(GFP_KERNEL);
6aa8b732 864 r = -ENOMEM;
0fe1e009 865 if (!sd->save_area)
6aa8b732
AK
866 goto err_1;
867
0fe1e009 868 per_cpu(svm_data, cpu) = sd;
6aa8b732
AK
869
870 return 0;
871
872err_1:
0fe1e009 873 kfree(sd);
6aa8b732
AK
874 return r;
875
876}
877
ac72a9b7
JR
878static bool valid_msr_intercept(u32 index)
879{
880 int i;
881
882 for (i = 0; direct_access_msrs[i].index != MSR_INVALID; i++)
883 if (direct_access_msrs[i].index == index)
884 return true;
885
886 return false;
887}
888
a57b802c
KA
889static bool msr_write_intercepted(struct kvm_vcpu *vcpu, unsigned msr)
890{
891 u8 bit_write;
892 unsigned long tmp;
893 u32 offset;
894 u32 *msrpm;
895
896 msrpm = is_guest_mode(vcpu) ? to_svm(vcpu)->nested.msrpm:
897 to_svm(vcpu)->msrpm;
898
899 offset = svm_msrpm_offset(msr);
900 bit_write = 2 * (msr & 0x0f) + 1;
901 tmp = msrpm[offset];
902
903 BUG_ON(offset == MSR_INVALID);
904
905 return !!test_bit(bit_write, &tmp);
906}
907
bfc733a7
RR
908static void set_msr_interception(u32 *msrpm, unsigned msr,
909 int read, int write)
6aa8b732 910{
455716fa
JR
911 u8 bit_read, bit_write;
912 unsigned long tmp;
913 u32 offset;
6aa8b732 914
ac72a9b7
JR
915 /*
916 * If this warning triggers extend the direct_access_msrs list at the
917 * beginning of the file
918 */
919 WARN_ON(!valid_msr_intercept(msr));
920
455716fa
JR
921 offset = svm_msrpm_offset(msr);
922 bit_read = 2 * (msr & 0x0f);
923 bit_write = 2 * (msr & 0x0f) + 1;
924 tmp = msrpm[offset];
925
926 BUG_ON(offset == MSR_INVALID);
927
928 read ? clear_bit(bit_read, &tmp) : set_bit(bit_read, &tmp);
929 write ? clear_bit(bit_write, &tmp) : set_bit(bit_write, &tmp);
930
931 msrpm[offset] = tmp;
6aa8b732
AK
932}
933
f65c229c 934static void svm_vcpu_init_msrpm(u32 *msrpm)
6aa8b732
AK
935{
936 int i;
937
f65c229c
JR
938 memset(msrpm, 0xff, PAGE_SIZE * (1 << MSRPM_ALLOC_ORDER));
939
ac72a9b7
JR
940 for (i = 0; direct_access_msrs[i].index != MSR_INVALID; i++) {
941 if (!direct_access_msrs[i].always)
942 continue;
943
944 set_msr_interception(msrpm, direct_access_msrs[i].index, 1, 1);
945 }
f65c229c
JR
946}
947
323c3d80
JR
948static void add_msr_offset(u32 offset)
949{
950 int i;
951
952 for (i = 0; i < MSRPM_OFFSETS; ++i) {
953
954 /* Offset already in list? */
955 if (msrpm_offsets[i] == offset)
bfc733a7 956 return;
323c3d80
JR
957
958 /* Slot used by another offset? */
959 if (msrpm_offsets[i] != MSR_INVALID)
960 continue;
961
962 /* Add offset to list */
963 msrpm_offsets[i] = offset;
964
965 return;
6aa8b732 966 }
323c3d80
JR
967
968 /*
969 * If this BUG triggers the msrpm_offsets table has an overflow. Just
970 * increase MSRPM_OFFSETS in this case.
971 */
bfc733a7 972 BUG();
6aa8b732
AK
973}
974
323c3d80 975static void init_msrpm_offsets(void)
f65c229c 976{
323c3d80 977 int i;
f65c229c 978
323c3d80
JR
979 memset(msrpm_offsets, 0xff, sizeof(msrpm_offsets));
980
981 for (i = 0; direct_access_msrs[i].index != MSR_INVALID; i++) {
982 u32 offset;
983
984 offset = svm_msrpm_offset(direct_access_msrs[i].index);
985 BUG_ON(offset == MSR_INVALID);
986
987 add_msr_offset(offset);
988 }
f65c229c
JR
989}
990
24e09cbf
JR
991static void svm_enable_lbrv(struct vcpu_svm *svm)
992{
993 u32 *msrpm = svm->msrpm;
994
0dc92119 995 svm->vmcb->control.virt_ext |= LBR_CTL_ENABLE_MASK;
24e09cbf
JR
996 set_msr_interception(msrpm, MSR_IA32_LASTBRANCHFROMIP, 1, 1);
997 set_msr_interception(msrpm, MSR_IA32_LASTBRANCHTOIP, 1, 1);
998 set_msr_interception(msrpm, MSR_IA32_LASTINTFROMIP, 1, 1);
999 set_msr_interception(msrpm, MSR_IA32_LASTINTTOIP, 1, 1);
1000}
1001
1002static void svm_disable_lbrv(struct vcpu_svm *svm)
1003{
1004 u32 *msrpm = svm->msrpm;
1005
0dc92119 1006 svm->vmcb->control.virt_ext &= ~LBR_CTL_ENABLE_MASK;
24e09cbf
JR
1007 set_msr_interception(msrpm, MSR_IA32_LASTBRANCHFROMIP, 0, 0);
1008 set_msr_interception(msrpm, MSR_IA32_LASTBRANCHTOIP, 0, 0);
1009 set_msr_interception(msrpm, MSR_IA32_LASTINTFROMIP, 0, 0);
1010 set_msr_interception(msrpm, MSR_IA32_LASTINTTOIP, 0, 0);
1011}
1012
4aebd0e9
LP
1013static void disable_nmi_singlestep(struct vcpu_svm *svm)
1014{
1015 svm->nmi_singlestep = false;
640bd6e5 1016
ab2f4d73
LP
1017 if (!(svm->vcpu.guest_debug & KVM_GUESTDBG_SINGLESTEP)) {
1018 /* Clear our flags if they were not set by the guest */
1019 if (!(svm->nmi_singlestep_guest_rflags & X86_EFLAGS_TF))
1020 svm->vmcb->save.rflags &= ~X86_EFLAGS_TF;
1021 if (!(svm->nmi_singlestep_guest_rflags & X86_EFLAGS_RF))
1022 svm->vmcb->save.rflags &= ~X86_EFLAGS_RF;
1023 }
4aebd0e9
LP
1024}
1025
5881f737
SS
1026/* Note:
1027 * This hash table is used to map VM_ID to a struct kvm_arch,
1028 * when handling AMD IOMMU GALOG notification to schedule in
1029 * a particular vCPU.
1030 */
1031#define SVM_VM_DATA_HASH_BITS 8
681bcea8 1032static DEFINE_HASHTABLE(svm_vm_data_hash, SVM_VM_DATA_HASH_BITS);
3f0d4db7
DV
1033static u32 next_vm_id = 0;
1034static bool next_vm_id_wrapped = 0;
681bcea8 1035static DEFINE_SPINLOCK(svm_vm_data_hash_lock);
5881f737
SS
1036
1037/* Note:
1038 * This function is called from IOMMU driver to notify
1039 * SVM to schedule in a particular vCPU of a particular VM.
1040 */
1041static int avic_ga_log_notifier(u32 ga_tag)
1042{
1043 unsigned long flags;
1044 struct kvm_arch *ka = NULL;
1045 struct kvm_vcpu *vcpu = NULL;
1046 u32 vm_id = AVIC_GATAG_TO_VMID(ga_tag);
1047 u32 vcpu_id = AVIC_GATAG_TO_VCPUID(ga_tag);
1048
1049 pr_debug("SVM: %s: vm_id=%#x, vcpu_id=%#x\n", __func__, vm_id, vcpu_id);
1050
1051 spin_lock_irqsave(&svm_vm_data_hash_lock, flags);
1052 hash_for_each_possible(svm_vm_data_hash, ka, hnode, vm_id) {
1053 struct kvm *kvm = container_of(ka, struct kvm, arch);
1054 struct kvm_arch *vm_data = &kvm->arch;
1055
1056 if (vm_data->avic_vm_id != vm_id)
1057 continue;
1058 vcpu = kvm_get_vcpu_by_id(kvm, vcpu_id);
1059 break;
1060 }
1061 spin_unlock_irqrestore(&svm_vm_data_hash_lock, flags);
1062
5881f737
SS
1063 /* Note:
1064 * At this point, the IOMMU should have already set the pending
1065 * bit in the vAPIC backing page. So, we just need to schedule
1066 * in the vcpu.
1067 */
1cf53587 1068 if (vcpu)
5881f737
SS
1069 kvm_vcpu_wake_up(vcpu);
1070
1071 return 0;
1072}
1073
6aa8b732
AK
1074static __init int svm_hardware_setup(void)
1075{
1076 int cpu;
1077 struct page *iopm_pages;
f65c229c 1078 void *iopm_va;
6aa8b732
AK
1079 int r;
1080
6aa8b732
AK
1081 iopm_pages = alloc_pages(GFP_KERNEL, IOPM_ALLOC_ORDER);
1082
1083 if (!iopm_pages)
1084 return -ENOMEM;
c8681339
AL
1085
1086 iopm_va = page_address(iopm_pages);
1087 memset(iopm_va, 0xff, PAGE_SIZE * (1 << IOPM_ALLOC_ORDER));
6aa8b732
AK
1088 iopm_base = page_to_pfn(iopm_pages) << PAGE_SHIFT;
1089
323c3d80
JR
1090 init_msrpm_offsets();
1091
50a37eb4
JR
1092 if (boot_cpu_has(X86_FEATURE_NX))
1093 kvm_enable_efer_bits(EFER_NX);
1094
1b2fd70c
AG
1095 if (boot_cpu_has(X86_FEATURE_FXSR_OPT))
1096 kvm_enable_efer_bits(EFER_FFXSR);
1097
92a1f12d 1098 if (boot_cpu_has(X86_FEATURE_TSCRATEMSR)) {
92a1f12d 1099 kvm_has_tsc_control = true;
bc9b961b
HZ
1100 kvm_max_tsc_scaling_ratio = TSC_RATIO_MAX;
1101 kvm_tsc_scaling_ratio_frac_bits = 32;
92a1f12d
JR
1102 }
1103
236de055
AG
1104 if (nested) {
1105 printk(KERN_INFO "kvm: Nested Virtualization enabled\n");
eec4b140 1106 kvm_enable_efer_bits(EFER_SVME | EFER_LMSLE);
236de055
AG
1107 }
1108
3230bb47 1109 for_each_possible_cpu(cpu) {
6aa8b732
AK
1110 r = svm_cpu_init(cpu);
1111 if (r)
f65c229c 1112 goto err;
6aa8b732 1113 }
33bd6a0b 1114
2a6b20b8 1115 if (!boot_cpu_has(X86_FEATURE_NPT))
e3da3acd
JR
1116 npt_enabled = false;
1117
6c7dac72
JR
1118 if (npt_enabled && !npt) {
1119 printk(KERN_INFO "kvm: Nested Paging disabled\n");
1120 npt_enabled = false;
1121 }
1122
18552672 1123 if (npt_enabled) {
e3da3acd 1124 printk(KERN_INFO "kvm: Nested Paging enabled\n");
18552672 1125 kvm_enable_tdp();
5f4cb662
JR
1126 } else
1127 kvm_disable_tdp();
e3da3acd 1128
5b8abf1f
SS
1129 if (avic) {
1130 if (!npt_enabled ||
1131 !boot_cpu_has(X86_FEATURE_AVIC) ||
5881f737 1132 !IS_ENABLED(CONFIG_X86_LOCAL_APIC)) {
5b8abf1f 1133 avic = false;
5881f737 1134 } else {
5b8abf1f 1135 pr_info("AVIC enabled\n");
5881f737 1136
5881f737
SS
1137 amd_iommu_register_ga_log_notifier(&avic_ga_log_notifier);
1138 }
5b8abf1f 1139 }
44a95dae 1140
89c8a498
JN
1141 if (vls) {
1142 if (!npt_enabled ||
5442c269 1143 !boot_cpu_has(X86_FEATURE_V_VMSAVE_VMLOAD) ||
89c8a498
JN
1144 !IS_ENABLED(CONFIG_X86_64)) {
1145 vls = false;
1146 } else {
1147 pr_info("Virtual VMLOAD VMSAVE supported\n");
1148 }
1149 }
1150
640bd6e5
JN
1151 if (vgif) {
1152 if (!boot_cpu_has(X86_FEATURE_VGIF))
1153 vgif = false;
1154 else
1155 pr_info("Virtual GIF supported\n");
1156 }
1157
6aa8b732
AK
1158 return 0;
1159
f65c229c 1160err:
6aa8b732
AK
1161 __free_pages(iopm_pages, IOPM_ALLOC_ORDER);
1162 iopm_base = 0;
1163 return r;
1164}
1165
1166static __exit void svm_hardware_unsetup(void)
1167{
0da1db75
JR
1168 int cpu;
1169
3230bb47 1170 for_each_possible_cpu(cpu)
0da1db75
JR
1171 svm_cpu_uninit(cpu);
1172
6aa8b732 1173 __free_pages(pfn_to_page(iopm_base >> PAGE_SHIFT), IOPM_ALLOC_ORDER);
f65c229c 1174 iopm_base = 0;
6aa8b732
AK
1175}
1176
1177static void init_seg(struct vmcb_seg *seg)
1178{
1179 seg->selector = 0;
1180 seg->attrib = SVM_SELECTOR_P_MASK | SVM_SELECTOR_S_MASK |
e0231715 1181 SVM_SELECTOR_WRITE_MASK; /* Read/Write Data Segment */
6aa8b732
AK
1182 seg->limit = 0xffff;
1183 seg->base = 0;
1184}
1185
1186static void init_sys_seg(struct vmcb_seg *seg, uint32_t type)
1187{
1188 seg->selector = 0;
1189 seg->attrib = SVM_SELECTOR_P_MASK | type;
1190 seg->limit = 0xffff;
1191 seg->base = 0;
1192}
1193
f4e1b3c8
ZA
1194static void svm_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset)
1195{
1196 struct vcpu_svm *svm = to_svm(vcpu);
1197 u64 g_tsc_offset = 0;
1198
2030753d 1199 if (is_guest_mode(vcpu)) {
f4e1b3c8
ZA
1200 g_tsc_offset = svm->vmcb->control.tsc_offset -
1201 svm->nested.hsave->control.tsc_offset;
1202 svm->nested.hsave->control.tsc_offset = offset;
489223ed
YY
1203 } else
1204 trace_kvm_write_tsc_offset(vcpu->vcpu_id,
1205 svm->vmcb->control.tsc_offset,
1206 offset);
f4e1b3c8
ZA
1207
1208 svm->vmcb->control.tsc_offset = offset + g_tsc_offset;
116a0a23
JR
1209
1210 mark_dirty(svm->vmcb, VMCB_INTERCEPTS);
f4e1b3c8
ZA
1211}
1212
44a95dae
SS
1213static void avic_init_vmcb(struct vcpu_svm *svm)
1214{
1215 struct vmcb *vmcb = svm->vmcb;
1216 struct kvm_arch *vm_data = &svm->vcpu.kvm->arch;
d0ec49d4
TL
1217 phys_addr_t bpa = __sme_set(page_to_phys(svm->avic_backing_page));
1218 phys_addr_t lpa = __sme_set(page_to_phys(vm_data->avic_logical_id_table_page));
1219 phys_addr_t ppa = __sme_set(page_to_phys(vm_data->avic_physical_id_table_page));
44a95dae
SS
1220
1221 vmcb->control.avic_backing_page = bpa & AVIC_HPA_MASK;
1222 vmcb->control.avic_logical_id = lpa & AVIC_HPA_MASK;
1223 vmcb->control.avic_physical_id = ppa & AVIC_HPA_MASK;
1224 vmcb->control.avic_physical_id |= AVIC_MAX_PHYSICAL_ID_COUNT;
1225 vmcb->control.int_ctl |= AVIC_ENABLE_MASK;
44a95dae
SS
1226}
1227
5690891b 1228static void init_vmcb(struct vcpu_svm *svm)
6aa8b732 1229{
e6101a96
JR
1230 struct vmcb_control_area *control = &svm->vmcb->control;
1231 struct vmcb_save_area *save = &svm->vmcb->save;
6aa8b732 1232
4ee546b4 1233 svm->vcpu.arch.hflags = 0;
bff78274 1234
4ee546b4
RJ
1235 set_cr_intercept(svm, INTERCEPT_CR0_READ);
1236 set_cr_intercept(svm, INTERCEPT_CR3_READ);
1237 set_cr_intercept(svm, INTERCEPT_CR4_READ);
1238 set_cr_intercept(svm, INTERCEPT_CR0_WRITE);
1239 set_cr_intercept(svm, INTERCEPT_CR3_WRITE);
1240 set_cr_intercept(svm, INTERCEPT_CR4_WRITE);
3bbf3565
SS
1241 if (!kvm_vcpu_apicv_active(&svm->vcpu))
1242 set_cr_intercept(svm, INTERCEPT_CR8_WRITE);
6aa8b732 1243
5315c716 1244 set_dr_intercepts(svm);
6aa8b732 1245
18c918c5
JR
1246 set_exception_intercept(svm, PF_VECTOR);
1247 set_exception_intercept(svm, UD_VECTOR);
1248 set_exception_intercept(svm, MC_VECTOR);
54a20552 1249 set_exception_intercept(svm, AC_VECTOR);
cbdb967a 1250 set_exception_intercept(svm, DB_VECTOR);
6aa8b732 1251
8a05a1b8
JR
1252 set_intercept(svm, INTERCEPT_INTR);
1253 set_intercept(svm, INTERCEPT_NMI);
1254 set_intercept(svm, INTERCEPT_SMI);
1255 set_intercept(svm, INTERCEPT_SELECTIVE_CR0);
332b56e4 1256 set_intercept(svm, INTERCEPT_RDPMC);
8a05a1b8
JR
1257 set_intercept(svm, INTERCEPT_CPUID);
1258 set_intercept(svm, INTERCEPT_INVD);
1259 set_intercept(svm, INTERCEPT_HLT);
1260 set_intercept(svm, INTERCEPT_INVLPG);
1261 set_intercept(svm, INTERCEPT_INVLPGA);
1262 set_intercept(svm, INTERCEPT_IOIO_PROT);
1263 set_intercept(svm, INTERCEPT_MSR_PROT);
1264 set_intercept(svm, INTERCEPT_TASK_SWITCH);
1265 set_intercept(svm, INTERCEPT_SHUTDOWN);
1266 set_intercept(svm, INTERCEPT_VMRUN);
1267 set_intercept(svm, INTERCEPT_VMMCALL);
1268 set_intercept(svm, INTERCEPT_VMLOAD);
1269 set_intercept(svm, INTERCEPT_VMSAVE);
1270 set_intercept(svm, INTERCEPT_STGI);
1271 set_intercept(svm, INTERCEPT_CLGI);
1272 set_intercept(svm, INTERCEPT_SKINIT);
1273 set_intercept(svm, INTERCEPT_WBINVD);
81dd35d4 1274 set_intercept(svm, INTERCEPT_XSETBV);
6aa8b732 1275
668fffa3
MT
1276 if (!kvm_mwait_in_guest()) {
1277 set_intercept(svm, INTERCEPT_MONITOR);
1278 set_intercept(svm, INTERCEPT_MWAIT);
1279 }
1280
d0ec49d4
TL
1281 control->iopm_base_pa = __sme_set(iopm_base);
1282 control->msrpm_base_pa = __sme_set(__pa(svm->msrpm));
6aa8b732
AK
1283 control->int_ctl = V_INTR_MASKING_MASK;
1284
1285 init_seg(&save->es);
1286 init_seg(&save->ss);
1287 init_seg(&save->ds);
1288 init_seg(&save->fs);
1289 init_seg(&save->gs);
1290
1291 save->cs.selector = 0xf000;
04b66839 1292 save->cs.base = 0xffff0000;
6aa8b732
AK
1293 /* Executable/Readable Code Segment */
1294 save->cs.attrib = SVM_SELECTOR_READ_MASK | SVM_SELECTOR_P_MASK |
1295 SVM_SELECTOR_S_MASK | SVM_SELECTOR_CODE_MASK;
1296 save->cs.limit = 0xffff;
6aa8b732
AK
1297
1298 save->gdtr.limit = 0xffff;
1299 save->idtr.limit = 0xffff;
1300
1301 init_sys_seg(&save->ldtr, SEG_TYPE_LDT);
1302 init_sys_seg(&save->tr, SEG_TYPE_BUSY_TSS16);
1303
5690891b 1304 svm_set_efer(&svm->vcpu, 0);
d77c26fc 1305 save->dr6 = 0xffff0ff0;
f6e78475 1306 kvm_set_rflags(&svm->vcpu, 2);
6aa8b732 1307 save->rip = 0x0000fff0;
5fdbf976 1308 svm->vcpu.arch.regs[VCPU_REGS_RIP] = save->rip;
6aa8b732 1309
e0231715 1310 /*
18fa000a 1311 * svm_set_cr0() sets PG and WP and clears NW and CD on save->cr0.
d28bc9dd 1312 * It also updates the guest-visible cr0 value.
6aa8b732 1313 */
79a8059d 1314 svm_set_cr0(&svm->vcpu, X86_CR0_NW | X86_CR0_CD | X86_CR0_ET);
ebae871a 1315 kvm_mmu_reset_context(&svm->vcpu);
18fa000a 1316
66aee91a 1317 save->cr4 = X86_CR4_PAE;
6aa8b732 1318 /* rdx = ?? */
709ddebf
JR
1319
1320 if (npt_enabled) {
1321 /* Setup VMCB for Nested Paging */
1322 control->nested_ctl = 1;
8a05a1b8 1323 clr_intercept(svm, INTERCEPT_INVLPG);
18c918c5 1324 clr_exception_intercept(svm, PF_VECTOR);
4ee546b4
RJ
1325 clr_cr_intercept(svm, INTERCEPT_CR3_READ);
1326 clr_cr_intercept(svm, INTERCEPT_CR3_WRITE);
74545705 1327 save->g_pat = svm->vcpu.arch.pat;
709ddebf
JR
1328 save->cr3 = 0;
1329 save->cr4 = 0;
1330 }
f40f6a45 1331 svm->asid_generation = 0;
1371d904 1332
e6aa9abd 1333 svm->nested.vmcb = 0;
2af9194d
JR
1334 svm->vcpu.arch.hflags = 0;
1335
2a6b20b8 1336 if (boot_cpu_has(X86_FEATURE_PAUSEFILTER)) {
565d0998 1337 control->pause_filter_count = 3000;
8a05a1b8 1338 set_intercept(svm, INTERCEPT_PAUSE);
565d0998
ML
1339 }
1340
67034bb9 1341 if (kvm_vcpu_apicv_active(&svm->vcpu))
44a95dae
SS
1342 avic_init_vmcb(svm);
1343
89c8a498
JN
1344 /*
1345 * If hardware supports Virtual VMLOAD VMSAVE then enable it
1346 * in VMCB and clear intercepts to avoid #VMEXIT.
1347 */
1348 if (vls) {
1349 clr_intercept(svm, INTERCEPT_VMLOAD);
1350 clr_intercept(svm, INTERCEPT_VMSAVE);
1351 svm->vmcb->control.virt_ext |= VIRTUAL_VMLOAD_VMSAVE_ENABLE_MASK;
1352 }
1353
640bd6e5
JN
1354 if (vgif) {
1355 clr_intercept(svm, INTERCEPT_STGI);
1356 clr_intercept(svm, INTERCEPT_CLGI);
1357 svm->vmcb->control.int_ctl |= V_GIF_ENABLE_MASK;
1358 }
1359
8d28fec4
RJ
1360 mark_all_dirty(svm->vmcb);
1361
2af9194d 1362 enable_gif(svm);
44a95dae
SS
1363
1364}
1365
d3e7dec0
DC
1366static u64 *avic_get_physical_id_entry(struct kvm_vcpu *vcpu,
1367 unsigned int index)
44a95dae
SS
1368{
1369 u64 *avic_physical_id_table;
1370 struct kvm_arch *vm_data = &vcpu->kvm->arch;
1371
1372 if (index >= AVIC_MAX_PHYSICAL_ID_COUNT)
1373 return NULL;
1374
1375 avic_physical_id_table = page_address(vm_data->avic_physical_id_table_page);
1376
1377 return &avic_physical_id_table[index];
1378}
1379
1380/**
1381 * Note:
1382 * AVIC hardware walks the nested page table to check permissions,
1383 * but does not use the SPA address specified in the leaf page
1384 * table entry since it uses address in the AVIC_BACKING_PAGE pointer
1385 * field of the VMCB. Therefore, we set up the
1386 * APIC_ACCESS_PAGE_PRIVATE_MEMSLOT (4KB) here.
1387 */
1388static int avic_init_access_page(struct kvm_vcpu *vcpu)
1389{
1390 struct kvm *kvm = vcpu->kvm;
1391 int ret;
1392
1393 if (kvm->arch.apic_access_page_done)
1394 return 0;
1395
1396 ret = x86_set_memory_region(kvm,
1397 APIC_ACCESS_PAGE_PRIVATE_MEMSLOT,
1398 APIC_DEFAULT_PHYS_BASE,
1399 PAGE_SIZE);
1400 if (ret)
1401 return ret;
1402
1403 kvm->arch.apic_access_page_done = true;
1404 return 0;
1405}
1406
1407static int avic_init_backing_page(struct kvm_vcpu *vcpu)
1408{
1409 int ret;
1410 u64 *entry, new_entry;
1411 int id = vcpu->vcpu_id;
1412 struct vcpu_svm *svm = to_svm(vcpu);
1413
1414 ret = avic_init_access_page(vcpu);
1415 if (ret)
1416 return ret;
1417
1418 if (id >= AVIC_MAX_PHYSICAL_ID_COUNT)
1419 return -EINVAL;
1420
1421 if (!svm->vcpu.arch.apic->regs)
1422 return -EINVAL;
1423
1424 svm->avic_backing_page = virt_to_page(svm->vcpu.arch.apic->regs);
1425
1426 /* Setting AVIC backing page address in the phy APIC ID table */
1427 entry = avic_get_physical_id_entry(vcpu, id);
1428 if (!entry)
1429 return -EINVAL;
1430
1431 new_entry = READ_ONCE(*entry);
d0ec49d4
TL
1432 new_entry = __sme_set((page_to_phys(svm->avic_backing_page) &
1433 AVIC_PHYSICAL_ID_ENTRY_BACKING_PAGE_MASK) |
1434 AVIC_PHYSICAL_ID_ENTRY_VALID_MASK);
44a95dae
SS
1435 WRITE_ONCE(*entry, new_entry);
1436
1437 svm->avic_physical_id_cache = entry;
1438
1439 return 0;
1440}
1441
1442static void avic_vm_destroy(struct kvm *kvm)
1443{
5881f737 1444 unsigned long flags;
44a95dae
SS
1445 struct kvm_arch *vm_data = &kvm->arch;
1446
3863dff0
DV
1447 if (!avic)
1448 return;
1449
44a95dae
SS
1450 if (vm_data->avic_logical_id_table_page)
1451 __free_page(vm_data->avic_logical_id_table_page);
1452 if (vm_data->avic_physical_id_table_page)
1453 __free_page(vm_data->avic_physical_id_table_page);
5881f737
SS
1454
1455 spin_lock_irqsave(&svm_vm_data_hash_lock, flags);
1456 hash_del(&vm_data->hnode);
1457 spin_unlock_irqrestore(&svm_vm_data_hash_lock, flags);
44a95dae
SS
1458}
1459
1460static int avic_vm_init(struct kvm *kvm)
1461{
5881f737 1462 unsigned long flags;
3f0d4db7 1463 int err = -ENOMEM;
44a95dae
SS
1464 struct kvm_arch *vm_data = &kvm->arch;
1465 struct page *p_page;
1466 struct page *l_page;
3f0d4db7
DV
1467 struct kvm_arch *ka;
1468 u32 vm_id;
44a95dae
SS
1469
1470 if (!avic)
1471 return 0;
1472
1473 /* Allocating physical APIC ID table (4KB) */
1474 p_page = alloc_page(GFP_KERNEL);
1475 if (!p_page)
1476 goto free_avic;
1477
1478 vm_data->avic_physical_id_table_page = p_page;
1479 clear_page(page_address(p_page));
1480
1481 /* Allocating logical APIC ID table (4KB) */
1482 l_page = alloc_page(GFP_KERNEL);
1483 if (!l_page)
1484 goto free_avic;
1485
1486 vm_data->avic_logical_id_table_page = l_page;
1487 clear_page(page_address(l_page));
1488
5881f737 1489 spin_lock_irqsave(&svm_vm_data_hash_lock, flags);
3f0d4db7
DV
1490 again:
1491 vm_id = next_vm_id = (next_vm_id + 1) & AVIC_VM_ID_MASK;
1492 if (vm_id == 0) { /* id is 1-based, zero is not okay */
1493 next_vm_id_wrapped = 1;
1494 goto again;
1495 }
1496 /* Is it still in use? Only possible if wrapped at least once */
1497 if (next_vm_id_wrapped) {
1498 hash_for_each_possible(svm_vm_data_hash, ka, hnode, vm_id) {
1499 struct kvm *k2 = container_of(ka, struct kvm, arch);
1500 struct kvm_arch *vd2 = &k2->arch;
1501 if (vd2->avic_vm_id == vm_id)
1502 goto again;
1503 }
1504 }
1505 vm_data->avic_vm_id = vm_id;
5881f737
SS
1506 hash_add(svm_vm_data_hash, &vm_data->hnode, vm_data->avic_vm_id);
1507 spin_unlock_irqrestore(&svm_vm_data_hash_lock, flags);
1508
44a95dae
SS
1509 return 0;
1510
1511free_avic:
1512 avic_vm_destroy(kvm);
1513 return err;
6aa8b732
AK
1514}
1515
411b44ba
SS
1516static inline int
1517avic_update_iommu_vcpu_affinity(struct kvm_vcpu *vcpu, int cpu, bool r)
8221c137 1518{
411b44ba
SS
1519 int ret = 0;
1520 unsigned long flags;
1521 struct amd_svm_iommu_ir *ir;
8221c137
SS
1522 struct vcpu_svm *svm = to_svm(vcpu);
1523
411b44ba
SS
1524 if (!kvm_arch_has_assigned_device(vcpu->kvm))
1525 return 0;
8221c137 1526
411b44ba
SS
1527 /*
1528 * Here, we go through the per-vcpu ir_list to update all existing
1529 * interrupt remapping table entry targeting this vcpu.
1530 */
1531 spin_lock_irqsave(&svm->ir_list_lock, flags);
8221c137 1532
411b44ba
SS
1533 if (list_empty(&svm->ir_list))
1534 goto out;
8221c137 1535
411b44ba
SS
1536 list_for_each_entry(ir, &svm->ir_list, node) {
1537 ret = amd_iommu_update_ga(cpu, r, ir->data);
1538 if (ret)
1539 break;
1540 }
1541out:
1542 spin_unlock_irqrestore(&svm->ir_list_lock, flags);
1543 return ret;
8221c137
SS
1544}
1545
1546static void avic_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
1547{
1548 u64 entry;
1549 /* ID = 0xff (broadcast), ID > 0xff (reserved) */
7d669f50 1550 int h_physical_id = kvm_cpu_get_apicid(cpu);
8221c137
SS
1551 struct vcpu_svm *svm = to_svm(vcpu);
1552
1553 if (!kvm_vcpu_apicv_active(vcpu))
1554 return;
1555
1556 if (WARN_ON(h_physical_id >= AVIC_MAX_PHYSICAL_ID_COUNT))
1557 return;
1558
1559 entry = READ_ONCE(*(svm->avic_physical_id_cache));
1560 WARN_ON(entry & AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK);
1561
1562 entry &= ~AVIC_PHYSICAL_ID_ENTRY_HOST_PHYSICAL_ID_MASK;
1563 entry |= (h_physical_id & AVIC_PHYSICAL_ID_ENTRY_HOST_PHYSICAL_ID_MASK);
1564
1565 entry &= ~AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK;
1566 if (svm->avic_is_running)
1567 entry |= AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK;
1568
1569 WRITE_ONCE(*(svm->avic_physical_id_cache), entry);
411b44ba
SS
1570 avic_update_iommu_vcpu_affinity(vcpu, h_physical_id,
1571 svm->avic_is_running);
8221c137
SS
1572}
1573
1574static void avic_vcpu_put(struct kvm_vcpu *vcpu)
1575{
1576 u64 entry;
1577 struct vcpu_svm *svm = to_svm(vcpu);
1578
1579 if (!kvm_vcpu_apicv_active(vcpu))
1580 return;
1581
1582 entry = READ_ONCE(*(svm->avic_physical_id_cache));
411b44ba
SS
1583 if (entry & AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK)
1584 avic_update_iommu_vcpu_affinity(vcpu, -1, 0);
1585
8221c137
SS
1586 entry &= ~AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK;
1587 WRITE_ONCE(*(svm->avic_physical_id_cache), entry);
6aa8b732
AK
1588}
1589
411b44ba
SS
1590/**
1591 * This function is called during VCPU halt/unhalt.
1592 */
1593static void avic_set_running(struct kvm_vcpu *vcpu, bool is_run)
1594{
1595 struct vcpu_svm *svm = to_svm(vcpu);
1596
1597 svm->avic_is_running = is_run;
1598 if (is_run)
1599 avic_vcpu_load(vcpu, vcpu->cpu);
1600 else
1601 avic_vcpu_put(vcpu);
1602}
1603
d28bc9dd 1604static void svm_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)
04d2cc77
AK
1605{
1606 struct vcpu_svm *svm = to_svm(vcpu);
66f7b72e
JS
1607 u32 dummy;
1608 u32 eax = 1;
04d2cc77 1609
a57b802c
KA
1610 svm->spec_ctrl = 0;
1611
d28bc9dd
NA
1612 if (!init_event) {
1613 svm->vcpu.arch.apic_base = APIC_DEFAULT_PHYS_BASE |
1614 MSR_IA32_APICBASE_ENABLE;
1615 if (kvm_vcpu_is_reset_bsp(&svm->vcpu))
1616 svm->vcpu.arch.apic_base |= MSR_IA32_APICBASE_BSP;
1617 }
5690891b 1618 init_vmcb(svm);
70433389 1619
e911eb3b 1620 kvm_cpuid(vcpu, &eax, &dummy, &dummy, &dummy, true);
66f7b72e 1621 kvm_register_write(vcpu, VCPU_REGS_RDX, eax);
44a95dae
SS
1622
1623 if (kvm_vcpu_apicv_active(vcpu) && !init_event)
1624 avic_update_vapic_bar(svm, APIC_DEFAULT_PHYS_BASE);
04d2cc77
AK
1625}
1626
dfa20099
SS
1627static int avic_init_vcpu(struct vcpu_svm *svm)
1628{
1629 int ret;
1630
67034bb9 1631 if (!kvm_vcpu_apicv_active(&svm->vcpu))
dfa20099
SS
1632 return 0;
1633
1634 ret = avic_init_backing_page(&svm->vcpu);
1635 if (ret)
1636 return ret;
1637
1638 INIT_LIST_HEAD(&svm->ir_list);
1639 spin_lock_init(&svm->ir_list_lock);
1640
1641 return ret;
1642}
1643
fb3f0f51 1644static struct kvm_vcpu *svm_create_vcpu(struct kvm *kvm, unsigned int id)
6aa8b732 1645{
a2fa3e9f 1646 struct vcpu_svm *svm;
6aa8b732 1647 struct page *page;
f65c229c 1648 struct page *msrpm_pages;
b286d5d8 1649 struct page *hsave_page;
3d6368ef 1650 struct page *nested_msrpm_pages;
fb3f0f51 1651 int err;
6aa8b732 1652
c16f862d 1653 svm = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
fb3f0f51
RR
1654 if (!svm) {
1655 err = -ENOMEM;
1656 goto out;
1657 }
1658
1659 err = kvm_vcpu_init(&svm->vcpu, kvm, id);
1660 if (err)
1661 goto free_svm;
1662
b7af4043 1663 err = -ENOMEM;
6aa8b732 1664 page = alloc_page(GFP_KERNEL);
b7af4043 1665 if (!page)
fb3f0f51 1666 goto uninit;
6aa8b732 1667
f65c229c
JR
1668 msrpm_pages = alloc_pages(GFP_KERNEL, MSRPM_ALLOC_ORDER);
1669 if (!msrpm_pages)
b7af4043 1670 goto free_page1;
3d6368ef
AG
1671
1672 nested_msrpm_pages = alloc_pages(GFP_KERNEL, MSRPM_ALLOC_ORDER);
1673 if (!nested_msrpm_pages)
b7af4043 1674 goto free_page2;
f65c229c 1675
b286d5d8
AG
1676 hsave_page = alloc_page(GFP_KERNEL);
1677 if (!hsave_page)
b7af4043
TY
1678 goto free_page3;
1679
dfa20099
SS
1680 err = avic_init_vcpu(svm);
1681 if (err)
1682 goto free_page4;
44a95dae 1683
8221c137
SS
1684 /* We initialize this flag to true to make sure that the is_running
1685 * bit would be set the first time the vcpu is loaded.
1686 */
1687 svm->avic_is_running = true;
1688
e6aa9abd 1689 svm->nested.hsave = page_address(hsave_page);
b286d5d8 1690
b7af4043
TY
1691 svm->msrpm = page_address(msrpm_pages);
1692 svm_vcpu_init_msrpm(svm->msrpm);
1693
e6aa9abd 1694 svm->nested.msrpm = page_address(nested_msrpm_pages);
323c3d80 1695 svm_vcpu_init_msrpm(svm->nested.msrpm);
3d6368ef 1696
a2fa3e9f
GH
1697 svm->vmcb = page_address(page);
1698 clear_page(svm->vmcb);
d0ec49d4 1699 svm->vmcb_pa = __sme_set(page_to_pfn(page) << PAGE_SHIFT);
a2fa3e9f 1700 svm->asid_generation = 0;
5690891b 1701 init_vmcb(svm);
6aa8b732 1702
2b036c6b
BO
1703 svm_init_osvw(&svm->vcpu);
1704
fb3f0f51 1705 return &svm->vcpu;
36241b8c 1706
44a95dae
SS
1707free_page4:
1708 __free_page(hsave_page);
b7af4043
TY
1709free_page3:
1710 __free_pages(nested_msrpm_pages, MSRPM_ALLOC_ORDER);
1711free_page2:
1712 __free_pages(msrpm_pages, MSRPM_ALLOC_ORDER);
1713free_page1:
1714 __free_page(page);
fb3f0f51
RR
1715uninit:
1716 kvm_vcpu_uninit(&svm->vcpu);
1717free_svm:
a4770347 1718 kmem_cache_free(kvm_vcpu_cache, svm);
fb3f0f51
RR
1719out:
1720 return ERR_PTR(err);
6aa8b732
AK
1721}
1722
1723static void svm_free_vcpu(struct kvm_vcpu *vcpu)
1724{
a2fa3e9f
GH
1725 struct vcpu_svm *svm = to_svm(vcpu);
1726
d0ec49d4 1727 __free_page(pfn_to_page(__sme_clr(svm->vmcb_pa) >> PAGE_SHIFT));
f65c229c 1728 __free_pages(virt_to_page(svm->msrpm), MSRPM_ALLOC_ORDER);
e6aa9abd
JR
1729 __free_page(virt_to_page(svm->nested.hsave));
1730 __free_pages(virt_to_page(svm->nested.msrpm), MSRPM_ALLOC_ORDER);
fb3f0f51 1731 kvm_vcpu_uninit(vcpu);
a4770347 1732 kmem_cache_free(kvm_vcpu_cache, svm);
33241bfe
AR
1733 /*
1734 * The vmcb page can be recycled, causing a false negative in
1735 * svm_vcpu_load(). So do a full IBPB now.
1736 */
1737 indirect_branch_prediction_barrier();
6aa8b732
AK
1738}
1739
15ad7146 1740static void svm_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
6aa8b732 1741{
a2fa3e9f 1742 struct vcpu_svm *svm = to_svm(vcpu);
33241bfe 1743 struct svm_cpu_data *sd = per_cpu(svm_data, cpu);
15ad7146 1744 int i;
0cc5064d 1745
0cc5064d 1746 if (unlikely(cpu != vcpu->cpu)) {
4b656b12 1747 svm->asid_generation = 0;
8d28fec4 1748 mark_all_dirty(svm->vmcb);
0cc5064d 1749 }
94dfbdb3 1750
82ca2d10
AK
1751#ifdef CONFIG_X86_64
1752 rdmsrl(MSR_GS_BASE, to_svm(vcpu)->host.gs_base);
1753#endif
dacccfdd
AK
1754 savesegment(fs, svm->host.fs);
1755 savesegment(gs, svm->host.gs);
1756 svm->host.ldt = kvm_read_ldt();
1757
94dfbdb3 1758 for (i = 0; i < NR_HOST_SAVE_USER_MSRS; i++)
a2fa3e9f 1759 rdmsrl(host_save_user_msrs[i], svm->host_user_msrs[i]);
fbc0db76 1760
ad721883
HZ
1761 if (static_cpu_has(X86_FEATURE_TSCRATEMSR)) {
1762 u64 tsc_ratio = vcpu->arch.tsc_scaling_ratio;
1763 if (tsc_ratio != __this_cpu_read(current_tsc_ratio)) {
1764 __this_cpu_write(current_tsc_ratio, tsc_ratio);
1765 wrmsrl(MSR_AMD64_TSC_RATIO, tsc_ratio);
1766 }
fbc0db76 1767 }
46896c73
PB
1768 /* This assumes that the kernel never uses MSR_TSC_AUX */
1769 if (static_cpu_has(X86_FEATURE_RDTSCP))
1770 wrmsrl(MSR_TSC_AUX, svm->tsc_aux);
8221c137 1771
33241bfe
AR
1772 if (sd->current_vmcb != svm->vmcb) {
1773 sd->current_vmcb = svm->vmcb;
1774 indirect_branch_prediction_barrier();
1775 }
8221c137 1776 avic_vcpu_load(vcpu, cpu);
6aa8b732
AK
1777}
1778
1779static void svm_vcpu_put(struct kvm_vcpu *vcpu)
1780{
a2fa3e9f 1781 struct vcpu_svm *svm = to_svm(vcpu);
94dfbdb3
AL
1782 int i;
1783
8221c137
SS
1784 avic_vcpu_put(vcpu);
1785
e1beb1d3 1786 ++vcpu->stat.host_state_reload;
dacccfdd
AK
1787 kvm_load_ldt(svm->host.ldt);
1788#ifdef CONFIG_X86_64
1789 loadsegment(fs, svm->host.fs);
296f781a 1790 wrmsrl(MSR_KERNEL_GS_BASE, current->thread.gsbase);
893a5ab6 1791 load_gs_index(svm->host.gs);
dacccfdd 1792#else
831ca609 1793#ifdef CONFIG_X86_32_LAZY_GS
dacccfdd 1794 loadsegment(gs, svm->host.gs);
831ca609 1795#endif
dacccfdd 1796#endif
94dfbdb3 1797 for (i = 0; i < NR_HOST_SAVE_USER_MSRS; i++)
a2fa3e9f 1798 wrmsrl(host_save_user_msrs[i], svm->host_user_msrs[i]);
6aa8b732
AK
1799}
1800
8221c137
SS
1801static void svm_vcpu_blocking(struct kvm_vcpu *vcpu)
1802{
1803 avic_set_running(vcpu, false);
1804}
1805
1806static void svm_vcpu_unblocking(struct kvm_vcpu *vcpu)
1807{
1808 avic_set_running(vcpu, true);
1809}
1810
6aa8b732
AK
1811static unsigned long svm_get_rflags(struct kvm_vcpu *vcpu)
1812{
9b611747
LP
1813 struct vcpu_svm *svm = to_svm(vcpu);
1814 unsigned long rflags = svm->vmcb->save.rflags;
1815
1816 if (svm->nmi_singlestep) {
1817 /* Hide our flags if they were not set by the guest */
1818 if (!(svm->nmi_singlestep_guest_rflags & X86_EFLAGS_TF))
1819 rflags &= ~X86_EFLAGS_TF;
1820 if (!(svm->nmi_singlestep_guest_rflags & X86_EFLAGS_RF))
1821 rflags &= ~X86_EFLAGS_RF;
1822 }
1823 return rflags;
6aa8b732
AK
1824}
1825
1826static void svm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
1827{
9b611747
LP
1828 if (to_svm(vcpu)->nmi_singlestep)
1829 rflags |= (X86_EFLAGS_TF | X86_EFLAGS_RF);
1830
ae9fedc7 1831 /*
bb3541f1 1832 * Any change of EFLAGS.VM is accompanied by a reload of SS
ae9fedc7
PB
1833 * (caused by either a task switch or an inter-privilege IRET),
1834 * so we do not need to update the CPL here.
1835 */
a2fa3e9f 1836 to_svm(vcpu)->vmcb->save.rflags = rflags;
6aa8b732
AK
1837}
1838
6de4f3ad
AK
1839static void svm_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
1840{
1841 switch (reg) {
1842 case VCPU_EXREG_PDPTR:
1843 BUG_ON(!npt_enabled);
9f8fe504 1844 load_pdptrs(vcpu, vcpu->arch.walk_mmu, kvm_read_cr3(vcpu));
6de4f3ad
AK
1845 break;
1846 default:
1847 BUG();
1848 }
1849}
1850
f0b85051
AG
1851static void svm_set_vintr(struct vcpu_svm *svm)
1852{
8a05a1b8 1853 set_intercept(svm, INTERCEPT_VINTR);
f0b85051
AG
1854}
1855
1856static void svm_clear_vintr(struct vcpu_svm *svm)
1857{
8a05a1b8 1858 clr_intercept(svm, INTERCEPT_VINTR);
f0b85051
AG
1859}
1860
6aa8b732
AK
1861static struct vmcb_seg *svm_seg(struct kvm_vcpu *vcpu, int seg)
1862{
a2fa3e9f 1863 struct vmcb_save_area *save = &to_svm(vcpu)->vmcb->save;
6aa8b732
AK
1864
1865 switch (seg) {
1866 case VCPU_SREG_CS: return &save->cs;
1867 case VCPU_SREG_DS: return &save->ds;
1868 case VCPU_SREG_ES: return &save->es;
1869 case VCPU_SREG_FS: return &save->fs;
1870 case VCPU_SREG_GS: return &save->gs;
1871 case VCPU_SREG_SS: return &save->ss;
1872 case VCPU_SREG_TR: return &save->tr;
1873 case VCPU_SREG_LDTR: return &save->ldtr;
1874 }
1875 BUG();
8b6d44c7 1876 return NULL;
6aa8b732
AK
1877}
1878
1879static u64 svm_get_segment_base(struct kvm_vcpu *vcpu, int seg)
1880{
1881 struct vmcb_seg *s = svm_seg(vcpu, seg);
1882
1883 return s->base;
1884}
1885
1886static void svm_get_segment(struct kvm_vcpu *vcpu,
1887 struct kvm_segment *var, int seg)
1888{
1889 struct vmcb_seg *s = svm_seg(vcpu, seg);
1890
1891 var->base = s->base;
1892 var->limit = s->limit;
1893 var->selector = s->selector;
1894 var->type = s->attrib & SVM_SELECTOR_TYPE_MASK;
1895 var->s = (s->attrib >> SVM_SELECTOR_S_SHIFT) & 1;
1896 var->dpl = (s->attrib >> SVM_SELECTOR_DPL_SHIFT) & 3;
1897 var->present = (s->attrib >> SVM_SELECTOR_P_SHIFT) & 1;
1898 var->avl = (s->attrib >> SVM_SELECTOR_AVL_SHIFT) & 1;
1899 var->l = (s->attrib >> SVM_SELECTOR_L_SHIFT) & 1;
1900 var->db = (s->attrib >> SVM_SELECTOR_DB_SHIFT) & 1;
80112c89
JM
1901
1902 /*
1903 * AMD CPUs circa 2014 track the G bit for all segments except CS.
1904 * However, the SVM spec states that the G bit is not observed by the
1905 * CPU, and some VMware virtual CPUs drop the G bit for all segments.
1906 * So let's synthesize a legal G bit for all segments, this helps
1907 * running KVM nested. It also helps cross-vendor migration, because
1908 * Intel's vmentry has a check on the 'G' bit.
1909 */
1910 var->g = s->limit > 0xfffff;
25022acc 1911
e0231715
JR
1912 /*
1913 * AMD's VMCB does not have an explicit unusable field, so emulate it
19bca6ab
AP
1914 * for cross vendor migration purposes by "not present"
1915 */
8eae9570 1916 var->unusable = !var->present;
19bca6ab 1917
1fbdc7a5 1918 switch (seg) {
1fbdc7a5
AP
1919 case VCPU_SREG_TR:
1920 /*
1921 * Work around a bug where the busy flag in the tr selector
1922 * isn't exposed
1923 */
c0d09828 1924 var->type |= 0x2;
1fbdc7a5
AP
1925 break;
1926 case VCPU_SREG_DS:
1927 case VCPU_SREG_ES:
1928 case VCPU_SREG_FS:
1929 case VCPU_SREG_GS:
1930 /*
1931 * The accessed bit must always be set in the segment
1932 * descriptor cache, although it can be cleared in the
1933 * descriptor, the cached bit always remains at 1. Since
1934 * Intel has a check on this, set it here to support
1935 * cross-vendor migration.
1936 */
1937 if (!var->unusable)
1938 var->type |= 0x1;
1939 break;
b586eb02 1940 case VCPU_SREG_SS:
e0231715
JR
1941 /*
1942 * On AMD CPUs sometimes the DB bit in the segment
b586eb02
AP
1943 * descriptor is left as 1, although the whole segment has
1944 * been made unusable. Clear it here to pass an Intel VMX
1945 * entry check when cross vendor migrating.
1946 */
1947 if (var->unusable)
1948 var->db = 0;
d9c1b543 1949 /* This is symmetric with svm_set_segment() */
33b458d2 1950 var->dpl = to_svm(vcpu)->vmcb->save.cpl;
b586eb02 1951 break;
1fbdc7a5 1952 }
6aa8b732
AK
1953}
1954
2e4d2653
IE
1955static int svm_get_cpl(struct kvm_vcpu *vcpu)
1956{
1957 struct vmcb_save_area *save = &to_svm(vcpu)->vmcb->save;
1958
1959 return save->cpl;
1960}
1961
89a27f4d 1962static void svm_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
6aa8b732 1963{
a2fa3e9f
GH
1964 struct vcpu_svm *svm = to_svm(vcpu);
1965
89a27f4d
GN
1966 dt->size = svm->vmcb->save.idtr.limit;
1967 dt->address = svm->vmcb->save.idtr.base;
6aa8b732
AK
1968}
1969
89a27f4d 1970static void svm_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
6aa8b732 1971{
a2fa3e9f
GH
1972 struct vcpu_svm *svm = to_svm(vcpu);
1973
89a27f4d
GN
1974 svm->vmcb->save.idtr.limit = dt->size;
1975 svm->vmcb->save.idtr.base = dt->address ;
17a703cb 1976 mark_dirty(svm->vmcb, VMCB_DT);
6aa8b732
AK
1977}
1978
89a27f4d 1979static void svm_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
6aa8b732 1980{
a2fa3e9f
GH
1981 struct vcpu_svm *svm = to_svm(vcpu);
1982
89a27f4d
GN
1983 dt->size = svm->vmcb->save.gdtr.limit;
1984 dt->address = svm->vmcb->save.gdtr.base;
6aa8b732
AK
1985}
1986
89a27f4d 1987static void svm_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
6aa8b732 1988{
a2fa3e9f
GH
1989 struct vcpu_svm *svm = to_svm(vcpu);
1990
89a27f4d
GN
1991 svm->vmcb->save.gdtr.limit = dt->size;
1992 svm->vmcb->save.gdtr.base = dt->address ;
17a703cb 1993 mark_dirty(svm->vmcb, VMCB_DT);
6aa8b732
AK
1994}
1995
e8467fda
AK
1996static void svm_decache_cr0_guest_bits(struct kvm_vcpu *vcpu)
1997{
1998}
1999
aff48baa
AK
2000static void svm_decache_cr3(struct kvm_vcpu *vcpu)
2001{
2002}
2003
25c4c276 2004static void svm_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
399badf3
AK
2005{
2006}
2007
d225157b
AK
2008static void update_cr0_intercept(struct vcpu_svm *svm)
2009{
2010 ulong gcr0 = svm->vcpu.arch.cr0;
2011 u64 *hcr0 = &svm->vmcb->save.cr0;
2012
bd7e5b08
PB
2013 *hcr0 = (*hcr0 & ~SVM_CR0_SELECTIVE_MASK)
2014 | (gcr0 & SVM_CR0_SELECTIVE_MASK);
d225157b 2015
dcca1a65 2016 mark_dirty(svm->vmcb, VMCB_CR);
d225157b 2017
bd7e5b08 2018 if (gcr0 == *hcr0) {
4ee546b4
RJ
2019 clr_cr_intercept(svm, INTERCEPT_CR0_READ);
2020 clr_cr_intercept(svm, INTERCEPT_CR0_WRITE);
d225157b 2021 } else {
4ee546b4
RJ
2022 set_cr_intercept(svm, INTERCEPT_CR0_READ);
2023 set_cr_intercept(svm, INTERCEPT_CR0_WRITE);
d225157b
AK
2024 }
2025}
2026
6aa8b732
AK
2027static void svm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
2028{
a2fa3e9f
GH
2029 struct vcpu_svm *svm = to_svm(vcpu);
2030
05b3e0c2 2031#ifdef CONFIG_X86_64
f6801dff 2032 if (vcpu->arch.efer & EFER_LME) {
707d92fa 2033 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) {
f6801dff 2034 vcpu->arch.efer |= EFER_LMA;
2b5203ee 2035 svm->vmcb->save.efer |= EFER_LMA | EFER_LME;
6aa8b732
AK
2036 }
2037
d77c26fc 2038 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG)) {
f6801dff 2039 vcpu->arch.efer &= ~EFER_LMA;
2b5203ee 2040 svm->vmcb->save.efer &= ~(EFER_LMA | EFER_LME);
6aa8b732
AK
2041 }
2042 }
2043#endif
ad312c7c 2044 vcpu->arch.cr0 = cr0;
888f9f3e
AK
2045
2046 if (!npt_enabled)
2047 cr0 |= X86_CR0_PG | X86_CR0_WP;
02daab21 2048
bcf166a9
PB
2049 /*
2050 * re-enable caching here because the QEMU bios
2051 * does not do it - this results in some delay at
2052 * reboot
2053 */
2054 if (kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_CD_NW_CLEARED))
2055 cr0 &= ~(X86_CR0_CD | X86_CR0_NW);
a2fa3e9f 2056 svm->vmcb->save.cr0 = cr0;
dcca1a65 2057 mark_dirty(svm->vmcb, VMCB_CR);
d225157b 2058 update_cr0_intercept(svm);
6aa8b732
AK
2059}
2060
5e1746d6 2061static int svm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
6aa8b732 2062{
1e02ce4c 2063 unsigned long host_cr4_mce = cr4_read_shadow() & X86_CR4_MCE;
e5eab0ce
JR
2064 unsigned long old_cr4 = to_svm(vcpu)->vmcb->save.cr4;
2065
5e1746d6
NHE
2066 if (cr4 & X86_CR4_VMXE)
2067 return 1;
2068
e5eab0ce 2069 if (npt_enabled && ((old_cr4 ^ cr4) & X86_CR4_PGE))
f40f6a45 2070 svm_flush_tlb(vcpu);
6394b649 2071
ec077263
JR
2072 vcpu->arch.cr4 = cr4;
2073 if (!npt_enabled)
2074 cr4 |= X86_CR4_PAE;
6394b649 2075 cr4 |= host_cr4_mce;
ec077263 2076 to_svm(vcpu)->vmcb->save.cr4 = cr4;
dcca1a65 2077 mark_dirty(to_svm(vcpu)->vmcb, VMCB_CR);
5e1746d6 2078 return 0;
6aa8b732
AK
2079}
2080
2081static void svm_set_segment(struct kvm_vcpu *vcpu,
2082 struct kvm_segment *var, int seg)
2083{
a2fa3e9f 2084 struct vcpu_svm *svm = to_svm(vcpu);
6aa8b732
AK
2085 struct vmcb_seg *s = svm_seg(vcpu, seg);
2086
2087 s->base = var->base;
2088 s->limit = var->limit;
2089 s->selector = var->selector;
d9c1b543
RP
2090 s->attrib = (var->type & SVM_SELECTOR_TYPE_MASK);
2091 s->attrib |= (var->s & 1) << SVM_SELECTOR_S_SHIFT;
2092 s->attrib |= (var->dpl & 3) << SVM_SELECTOR_DPL_SHIFT;
2093 s->attrib |= ((var->present & 1) && !var->unusable) << SVM_SELECTOR_P_SHIFT;
2094 s->attrib |= (var->avl & 1) << SVM_SELECTOR_AVL_SHIFT;
2095 s->attrib |= (var->l & 1) << SVM_SELECTOR_L_SHIFT;
2096 s->attrib |= (var->db & 1) << SVM_SELECTOR_DB_SHIFT;
2097 s->attrib |= (var->g & 1) << SVM_SELECTOR_G_SHIFT;
ae9fedc7
PB
2098
2099 /*
2100 * This is always accurate, except if SYSRET returned to a segment
2101 * with SS.DPL != 3. Intel does not have this quirk, and always
2102 * forces SS.DPL to 3 on sysret, so we ignore that case; fixing it
2103 * would entail passing the CPL to userspace and back.
2104 */
2105 if (seg == VCPU_SREG_SS)
d9c1b543
RP
2106 /* This is symmetric with svm_get_segment() */
2107 svm->vmcb->save.cpl = (var->dpl & 3);
6aa8b732 2108
060d0c9a 2109 mark_dirty(svm->vmcb, VMCB_SEG);
6aa8b732
AK
2110}
2111
cbdb967a 2112static void update_bp_intercept(struct kvm_vcpu *vcpu)
6aa8b732 2113{
d0bfb940
JK
2114 struct vcpu_svm *svm = to_svm(vcpu);
2115
18c918c5 2116 clr_exception_intercept(svm, BP_VECTOR);
44c11430 2117
d0bfb940 2118 if (vcpu->guest_debug & KVM_GUESTDBG_ENABLE) {
d0bfb940 2119 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
18c918c5 2120 set_exception_intercept(svm, BP_VECTOR);
d0bfb940
JK
2121 } else
2122 vcpu->guest_debug = 0;
44c11430
GN
2123}
2124
0fe1e009 2125static void new_asid(struct vcpu_svm *svm, struct svm_cpu_data *sd)
6aa8b732 2126{
0fe1e009
TH
2127 if (sd->next_asid > sd->max_asid) {
2128 ++sd->asid_generation;
2129 sd->next_asid = 1;
a2fa3e9f 2130 svm->vmcb->control.tlb_ctl = TLB_CONTROL_FLUSH_ALL_ASID;
6aa8b732
AK
2131 }
2132
0fe1e009
TH
2133 svm->asid_generation = sd->asid_generation;
2134 svm->vmcb->control.asid = sd->next_asid++;
d48086d1
JR
2135
2136 mark_dirty(svm->vmcb, VMCB_ASID);
6aa8b732
AK
2137}
2138
73aaf249
JK
2139static u64 svm_get_dr6(struct kvm_vcpu *vcpu)
2140{
2141 return to_svm(vcpu)->vmcb->save.dr6;
2142}
2143
2144static void svm_set_dr6(struct kvm_vcpu *vcpu, unsigned long value)
2145{
2146 struct vcpu_svm *svm = to_svm(vcpu);
2147
2148 svm->vmcb->save.dr6 = value;
2149 mark_dirty(svm->vmcb, VMCB_DR);
2150}
2151
facb0139
PB
2152static void svm_sync_dirty_debug_regs(struct kvm_vcpu *vcpu)
2153{
2154 struct vcpu_svm *svm = to_svm(vcpu);
2155
2156 get_debugreg(vcpu->arch.db[0], 0);
2157 get_debugreg(vcpu->arch.db[1], 1);
2158 get_debugreg(vcpu->arch.db[2], 2);
2159 get_debugreg(vcpu->arch.db[3], 3);
2160 vcpu->arch.dr6 = svm_get_dr6(vcpu);
2161 vcpu->arch.dr7 = svm->vmcb->save.dr7;
2162
2163 vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_WONT_EXIT;
2164 set_dr_intercepts(svm);
2165}
2166
020df079 2167static void svm_set_dr7(struct kvm_vcpu *vcpu, unsigned long value)
6aa8b732 2168{
42dbaa5a 2169 struct vcpu_svm *svm = to_svm(vcpu);
42dbaa5a 2170
020df079 2171 svm->vmcb->save.dr7 = value;
72214b96 2172 mark_dirty(svm->vmcb, VMCB_DR);
6aa8b732
AK
2173}
2174
851ba692 2175static int pf_interception(struct vcpu_svm *svm)
6aa8b732 2176{
631bc487 2177 u64 fault_address = svm->vmcb->control.exit_info_2;
1261bfa3 2178 u64 error_code = svm->vmcb->control.exit_info_1;
6aa8b732 2179
1261bfa3 2180 return kvm_handle_page_fault(&svm->vcpu, error_code, fault_address,
706db035
BS
2181 static_cpu_has(X86_FEATURE_DECODEASSISTS) ?
2182 svm->vmcb->control.insn_bytes : NULL,
d0006530
PB
2183 svm->vmcb->control.insn_len);
2184}
2185
2186static int npf_interception(struct vcpu_svm *svm)
2187{
2188 u64 fault_address = svm->vmcb->control.exit_info_2;
2189 u64 error_code = svm->vmcb->control.exit_info_1;
2190
2191 trace_kvm_page_fault(fault_address, error_code);
2192 return kvm_mmu_page_fault(&svm->vcpu, fault_address, error_code,
706db035
BS
2193 static_cpu_has(X86_FEATURE_DECODEASSISTS) ?
2194 svm->vmcb->control.insn_bytes : NULL,
d0006530 2195 svm->vmcb->control.insn_len);
6aa8b732
AK
2196}
2197
851ba692 2198static int db_interception(struct vcpu_svm *svm)
d0bfb940 2199{
851ba692
AK
2200 struct kvm_run *kvm_run = svm->vcpu.run;
2201
d0bfb940 2202 if (!(svm->vcpu.guest_debug &
44c11430 2203 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)) &&
6be7d306 2204 !svm->nmi_singlestep) {
d0bfb940
JK
2205 kvm_queue_exception(&svm->vcpu, DB_VECTOR);
2206 return 1;
2207 }
44c11430 2208
6be7d306 2209 if (svm->nmi_singlestep) {
4aebd0e9 2210 disable_nmi_singlestep(svm);
44c11430
GN
2211 }
2212
2213 if (svm->vcpu.guest_debug &
e0231715 2214 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)) {
44c11430
GN
2215 kvm_run->exit_reason = KVM_EXIT_DEBUG;
2216 kvm_run->debug.arch.pc =
2217 svm->vmcb->save.cs.base + svm->vmcb->save.rip;
2218 kvm_run->debug.arch.exception = DB_VECTOR;
2219 return 0;
2220 }
2221
2222 return 1;
d0bfb940
JK
2223}
2224
851ba692 2225static int bp_interception(struct vcpu_svm *svm)
d0bfb940 2226{
851ba692
AK
2227 struct kvm_run *kvm_run = svm->vcpu.run;
2228
d0bfb940
JK
2229 kvm_run->exit_reason = KVM_EXIT_DEBUG;
2230 kvm_run->debug.arch.pc = svm->vmcb->save.cs.base + svm->vmcb->save.rip;
2231 kvm_run->debug.arch.exception = BP_VECTOR;
2232 return 0;
2233}
2234
851ba692 2235static int ud_interception(struct vcpu_svm *svm)
7aa81cc0
AL
2236{
2237 int er;
2238
51d8b661 2239 er = emulate_instruction(&svm->vcpu, EMULTYPE_TRAP_UD);
61cb57c9
LA
2240 if (er == EMULATE_USER_EXIT)
2241 return 0;
7aa81cc0 2242 if (er != EMULATE_DONE)
7ee5d940 2243 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
7aa81cc0
AL
2244 return 1;
2245}
2246
54a20552
EN
2247static int ac_interception(struct vcpu_svm *svm)
2248{
2249 kvm_queue_exception_e(&svm->vcpu, AC_VECTOR, 0);
2250 return 1;
2251}
2252
67ec6607
JR
2253static bool is_erratum_383(void)
2254{
2255 int err, i;
2256 u64 value;
2257
2258 if (!erratum_383_found)
2259 return false;
2260
2261 value = native_read_msr_safe(MSR_IA32_MC0_STATUS, &err);
2262 if (err)
2263 return false;
2264
2265 /* Bit 62 may or may not be set for this mce */
2266 value &= ~(1ULL << 62);
2267
2268 if (value != 0xb600000000010015ULL)
2269 return false;
2270
2271 /* Clear MCi_STATUS registers */
2272 for (i = 0; i < 6; ++i)
2273 native_write_msr_safe(MSR_IA32_MCx_STATUS(i), 0, 0);
2274
2275 value = native_read_msr_safe(MSR_IA32_MCG_STATUS, &err);
2276 if (!err) {
2277 u32 low, high;
2278
2279 value &= ~(1ULL << 2);
2280 low = lower_32_bits(value);
2281 high = upper_32_bits(value);
2282
2283 native_write_msr_safe(MSR_IA32_MCG_STATUS, low, high);
2284 }
2285
2286 /* Flush tlb to evict multi-match entries */
2287 __flush_tlb_all();
2288
2289 return true;
2290}
2291
fe5913e4 2292static void svm_handle_mce(struct vcpu_svm *svm)
53371b50 2293{
67ec6607
JR
2294 if (is_erratum_383()) {
2295 /*
2296 * Erratum 383 triggered. Guest state is corrupt so kill the
2297 * guest.
2298 */
2299 pr_err("KVM: Guest triggered AMD Erratum 383\n");
2300
a8eeb04a 2301 kvm_make_request(KVM_REQ_TRIPLE_FAULT, &svm->vcpu);
67ec6607
JR
2302
2303 return;
2304 }
2305
53371b50
JR
2306 /*
2307 * On an #MC intercept the MCE handler is not called automatically in
2308 * the host. So do it by hand here.
2309 */
2310 asm volatile (
2311 "int $0x12\n");
2312 /* not sure if we ever come back to this point */
2313
fe5913e4
JR
2314 return;
2315}
2316
2317static int mc_interception(struct vcpu_svm *svm)
2318{
53371b50
JR
2319 return 1;
2320}
2321
851ba692 2322static int shutdown_interception(struct vcpu_svm *svm)
46fe4ddd 2323{
851ba692
AK
2324 struct kvm_run *kvm_run = svm->vcpu.run;
2325
46fe4ddd
JR
2326 /*
2327 * VMCB is undefined after a SHUTDOWN intercept
2328 * so reinitialize it.
2329 */
a2fa3e9f 2330 clear_page(svm->vmcb);
5690891b 2331 init_vmcb(svm);
46fe4ddd
JR
2332
2333 kvm_run->exit_reason = KVM_EXIT_SHUTDOWN;
2334 return 0;
2335}
2336
851ba692 2337static int io_interception(struct vcpu_svm *svm)
6aa8b732 2338{
cf8f70bf 2339 struct kvm_vcpu *vcpu = &svm->vcpu;
d77c26fc 2340 u32 io_info = svm->vmcb->control.exit_info_1; /* address size bug? */
b742c1e6 2341 int size, in, string, ret;
039576c0 2342 unsigned port;
6aa8b732 2343
e756fc62 2344 ++svm->vcpu.stat.io_exits;
e70669ab 2345 string = (io_info & SVM_IOIO_STR_MASK) != 0;
039576c0 2346 in = (io_info & SVM_IOIO_TYPE_MASK) != 0;
8370c3d0 2347 if (string)
51d8b661 2348 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
cf8f70bf 2349
039576c0
AK
2350 port = io_info >> 16;
2351 size = (io_info & SVM_IOIO_SIZE_MASK) >> SVM_IOIO_SIZE_SHIFT;
cf8f70bf 2352 svm->next_rip = svm->vmcb->control.exit_info_2;
b742c1e6 2353 ret = kvm_skip_emulated_instruction(&svm->vcpu);
cf8f70bf 2354
b742c1e6
LP
2355 /*
2356 * TODO: we might be squashing a KVM_GUESTDBG_SINGLESTEP-triggered
2357 * KVM_EXIT_DEBUG here.
2358 */
2359 if (in)
2360 return kvm_fast_pio_in(vcpu, size, port) && ret;
2361 else
2362 return kvm_fast_pio_out(vcpu, size, port) && ret;
6aa8b732
AK
2363}
2364
851ba692 2365static int nmi_interception(struct vcpu_svm *svm)
c47f098d
JR
2366{
2367 return 1;
2368}
2369
851ba692 2370static int intr_interception(struct vcpu_svm *svm)
a0698055
JR
2371{
2372 ++svm->vcpu.stat.irq_exits;
2373 return 1;
2374}
2375
851ba692 2376static int nop_on_interception(struct vcpu_svm *svm)
6aa8b732
AK
2377{
2378 return 1;
2379}
2380
851ba692 2381static int halt_interception(struct vcpu_svm *svm)
6aa8b732 2382{
5fdbf976 2383 svm->next_rip = kvm_rip_read(&svm->vcpu) + 1;
e756fc62 2384 return kvm_emulate_halt(&svm->vcpu);
6aa8b732
AK
2385}
2386
851ba692 2387static int vmmcall_interception(struct vcpu_svm *svm)
02e235bc 2388{
5fdbf976 2389 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
0d9c055e 2390 return kvm_emulate_hypercall(&svm->vcpu);
02e235bc
AK
2391}
2392
5bd2edc3
JR
2393static unsigned long nested_svm_get_tdp_cr3(struct kvm_vcpu *vcpu)
2394{
2395 struct vcpu_svm *svm = to_svm(vcpu);
2396
2397 return svm->nested.nested_cr3;
2398}
2399
e4e517b4
AK
2400static u64 nested_svm_get_tdp_pdptr(struct kvm_vcpu *vcpu, int index)
2401{
2402 struct vcpu_svm *svm = to_svm(vcpu);
2403 u64 cr3 = svm->nested.nested_cr3;
2404 u64 pdpte;
2405 int ret;
2406
d0ec49d4 2407 ret = kvm_vcpu_read_guest_page(vcpu, gpa_to_gfn(__sme_clr(cr3)), &pdpte,
54bf36aa 2408 offset_in_page(cr3) + index * 8, 8);
e4e517b4
AK
2409 if (ret)
2410 return 0;
2411 return pdpte;
2412}
2413
5bd2edc3
JR
2414static void nested_svm_set_tdp_cr3(struct kvm_vcpu *vcpu,
2415 unsigned long root)
2416{
2417 struct vcpu_svm *svm = to_svm(vcpu);
2418
d0ec49d4 2419 svm->vmcb->control.nested_cr3 = __sme_set(root);
b2747166 2420 mark_dirty(svm->vmcb, VMCB_NPT);
f40f6a45 2421 svm_flush_tlb(vcpu);
5bd2edc3
JR
2422}
2423
6389ee94
AK
2424static void nested_svm_inject_npf_exit(struct kvm_vcpu *vcpu,
2425 struct x86_exception *fault)
5bd2edc3
JR
2426{
2427 struct vcpu_svm *svm = to_svm(vcpu);
2428
5e352519
PB
2429 if (svm->vmcb->control.exit_code != SVM_EXIT_NPF) {
2430 /*
2431 * TODO: track the cause of the nested page fault, and
2432 * correctly fill in the high bits of exit_info_1.
2433 */
2434 svm->vmcb->control.exit_code = SVM_EXIT_NPF;
2435 svm->vmcb->control.exit_code_hi = 0;
2436 svm->vmcb->control.exit_info_1 = (1ULL << 32);
2437 svm->vmcb->control.exit_info_2 = fault->address;
2438 }
2439
2440 svm->vmcb->control.exit_info_1 &= ~0xffffffffULL;
2441 svm->vmcb->control.exit_info_1 |= fault->error_code;
2442
2443 /*
2444 * The present bit is always zero for page structure faults on real
2445 * hardware.
2446 */
2447 if (svm->vmcb->control.exit_info_1 & (2ULL << 32))
2448 svm->vmcb->control.exit_info_1 &= ~1;
5bd2edc3
JR
2449
2450 nested_svm_vmexit(svm);
2451}
2452
8a3c1a33 2453static void nested_svm_init_mmu_context(struct kvm_vcpu *vcpu)
4b16184c 2454{
ad896af0
PB
2455 WARN_ON(mmu_is_nested(vcpu));
2456 kvm_init_shadow_mmu(vcpu);
4b16184c
JR
2457 vcpu->arch.mmu.set_cr3 = nested_svm_set_tdp_cr3;
2458 vcpu->arch.mmu.get_cr3 = nested_svm_get_tdp_cr3;
e4e517b4 2459 vcpu->arch.mmu.get_pdptr = nested_svm_get_tdp_pdptr;
4b16184c 2460 vcpu->arch.mmu.inject_page_fault = nested_svm_inject_npf_exit;
855feb67 2461 vcpu->arch.mmu.shadow_root_level = get_npt_level(vcpu);
c258b62b 2462 reset_shadow_zero_bits_mask(vcpu, &vcpu->arch.mmu);
4b16184c 2463 vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu;
4b16184c
JR
2464}
2465
2466static void nested_svm_uninit_mmu_context(struct kvm_vcpu *vcpu)
2467{
2468 vcpu->arch.walk_mmu = &vcpu->arch.mmu;
2469}
2470
c0725420
AG
2471static int nested_svm_check_permissions(struct vcpu_svm *svm)
2472{
e9196ceb
DC
2473 if (!(svm->vcpu.arch.efer & EFER_SVME) ||
2474 !is_paging(&svm->vcpu)) {
c0725420
AG
2475 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
2476 return 1;
2477 }
2478
2479 if (svm->vmcb->save.cpl) {
2480 kvm_inject_gp(&svm->vcpu, 0);
2481 return 1;
2482 }
2483
e9196ceb 2484 return 0;
c0725420
AG
2485}
2486
cf74a78b
AG
2487static int nested_svm_check_exception(struct vcpu_svm *svm, unsigned nr,
2488 bool has_error_code, u32 error_code)
2489{
b8e88bc8
JR
2490 int vmexit;
2491
2030753d 2492 if (!is_guest_mode(&svm->vcpu))
0295ad7d 2493 return 0;
cf74a78b 2494
adfe20fb
WL
2495 vmexit = nested_svm_intercept(svm);
2496 if (vmexit != NESTED_EXIT_DONE)
2497 return 0;
2498
0295ad7d
JR
2499 svm->vmcb->control.exit_code = SVM_EXIT_EXCP_BASE + nr;
2500 svm->vmcb->control.exit_code_hi = 0;
2501 svm->vmcb->control.exit_info_1 = error_code;
b96fb439
PB
2502
2503 /*
2504 * FIXME: we should not write CR2 when L1 intercepts an L2 #PF exception.
2505 * The fix is to add the ancillary datum (CR2 or DR6) to structs
2506 * kvm_queued_exception and kvm_vcpu_events, so that CR2 and DR6 can be
2507 * written only when inject_pending_event runs (DR6 would written here
2508 * too). This should be conditional on a new capability---if the
2509 * capability is disabled, kvm_multiple_exception would write the
2510 * ancillary information to CR2 or DR6, for backwards ABI-compatibility.
2511 */
adfe20fb
WL
2512 if (svm->vcpu.arch.exception.nested_apf)
2513 svm->vmcb->control.exit_info_2 = svm->vcpu.arch.apf.nested_apf_token;
2514 else
2515 svm->vmcb->control.exit_info_2 = svm->vcpu.arch.cr2;
b8e88bc8 2516
adfe20fb 2517 svm->nested.exit_required = true;
b8e88bc8 2518 return vmexit;
cf74a78b
AG
2519}
2520
8fe54654
JR
2521/* This function returns true if it is save to enable the irq window */
2522static inline bool nested_svm_intr(struct vcpu_svm *svm)
cf74a78b 2523{
2030753d 2524 if (!is_guest_mode(&svm->vcpu))
8fe54654 2525 return true;
cf74a78b 2526
26666957 2527 if (!(svm->vcpu.arch.hflags & HF_VINTR_MASK))
8fe54654 2528 return true;
cf74a78b 2529
26666957 2530 if (!(svm->vcpu.arch.hflags & HF_HIF_MASK))
8fe54654 2531 return false;
cf74a78b 2532
a0a07cd2
GN
2533 /*
2534 * if vmexit was already requested (by intercepted exception
2535 * for instance) do not overwrite it with "external interrupt"
2536 * vmexit.
2537 */
2538 if (svm->nested.exit_required)
2539 return false;
2540
197717d5
JR
2541 svm->vmcb->control.exit_code = SVM_EXIT_INTR;
2542 svm->vmcb->control.exit_info_1 = 0;
2543 svm->vmcb->control.exit_info_2 = 0;
26666957 2544
cd3ff653
JR
2545 if (svm->nested.intercept & 1ULL) {
2546 /*
2547 * The #vmexit can't be emulated here directly because this
c5ec2e56 2548 * code path runs with irqs and preemption disabled. A
cd3ff653
JR
2549 * #vmexit emulation might sleep. Only signal request for
2550 * the #vmexit here.
2551 */
2552 svm->nested.exit_required = true;
236649de 2553 trace_kvm_nested_intr_vmexit(svm->vmcb->save.rip);
8fe54654 2554 return false;
cf74a78b
AG
2555 }
2556
8fe54654 2557 return true;
cf74a78b
AG
2558}
2559
887f500c
JR
2560/* This function returns true if it is save to enable the nmi window */
2561static inline bool nested_svm_nmi(struct vcpu_svm *svm)
2562{
2030753d 2563 if (!is_guest_mode(&svm->vcpu))
887f500c
JR
2564 return true;
2565
2566 if (!(svm->nested.intercept & (1ULL << INTERCEPT_NMI)))
2567 return true;
2568
2569 svm->vmcb->control.exit_code = SVM_EXIT_NMI;
2570 svm->nested.exit_required = true;
2571
2572 return false;
cf74a78b
AG
2573}
2574
7597f129 2575static void *nested_svm_map(struct vcpu_svm *svm, u64 gpa, struct page **_page)
34f80cfa
JR
2576{
2577 struct page *page;
2578
6c3bd3d7
JR
2579 might_sleep();
2580
54bf36aa 2581 page = kvm_vcpu_gfn_to_page(&svm->vcpu, gpa >> PAGE_SHIFT);
34f80cfa
JR
2582 if (is_error_page(page))
2583 goto error;
2584
7597f129
JR
2585 *_page = page;
2586
2587 return kmap(page);
34f80cfa
JR
2588
2589error:
34f80cfa
JR
2590 kvm_inject_gp(&svm->vcpu, 0);
2591
2592 return NULL;
2593}
2594
7597f129 2595static void nested_svm_unmap(struct page *page)
34f80cfa 2596{
7597f129 2597 kunmap(page);
34f80cfa
JR
2598 kvm_release_page_dirty(page);
2599}
34f80cfa 2600
ce2ac085
JR
2601static int nested_svm_intercept_ioio(struct vcpu_svm *svm)
2602{
9bf41833
JK
2603 unsigned port, size, iopm_len;
2604 u16 val, mask;
2605 u8 start_bit;
ce2ac085 2606 u64 gpa;
34f80cfa 2607
ce2ac085
JR
2608 if (!(svm->nested.intercept & (1ULL << INTERCEPT_IOIO_PROT)))
2609 return NESTED_EXIT_HOST;
34f80cfa 2610
ce2ac085 2611 port = svm->vmcb->control.exit_info_1 >> 16;
9bf41833
JK
2612 size = (svm->vmcb->control.exit_info_1 & SVM_IOIO_SIZE_MASK) >>
2613 SVM_IOIO_SIZE_SHIFT;
ce2ac085 2614 gpa = svm->nested.vmcb_iopm + (port / 8);
9bf41833
JK
2615 start_bit = port % 8;
2616 iopm_len = (start_bit + size > 8) ? 2 : 1;
2617 mask = (0xf >> (4 - size)) << start_bit;
2618 val = 0;
ce2ac085 2619
54bf36aa 2620 if (kvm_vcpu_read_guest(&svm->vcpu, gpa, &val, iopm_len))
9bf41833 2621 return NESTED_EXIT_DONE;
ce2ac085 2622
9bf41833 2623 return (val & mask) ? NESTED_EXIT_DONE : NESTED_EXIT_HOST;
34f80cfa
JR
2624}
2625
d2477826 2626static int nested_svm_exit_handled_msr(struct vcpu_svm *svm)
4c2161ae 2627{
0d6b3537
JR
2628 u32 offset, msr, value;
2629 int write, mask;
4c2161ae 2630
3d62d9aa 2631 if (!(svm->nested.intercept & (1ULL << INTERCEPT_MSR_PROT)))
d2477826 2632 return NESTED_EXIT_HOST;
3d62d9aa 2633
0d6b3537
JR
2634 msr = svm->vcpu.arch.regs[VCPU_REGS_RCX];
2635 offset = svm_msrpm_offset(msr);
2636 write = svm->vmcb->control.exit_info_1 & 1;
2637 mask = 1 << ((2 * (msr & 0xf)) + write);
3d62d9aa 2638
0d6b3537
JR
2639 if (offset == MSR_INVALID)
2640 return NESTED_EXIT_DONE;
4c2161ae 2641
0d6b3537
JR
2642 /* Offset is in 32 bit units but need in 8 bit units */
2643 offset *= 4;
4c2161ae 2644
54bf36aa 2645 if (kvm_vcpu_read_guest(&svm->vcpu, svm->nested.vmcb_msrpm + offset, &value, 4))
0d6b3537 2646 return NESTED_EXIT_DONE;
3d62d9aa 2647
0d6b3537 2648 return (value & mask) ? NESTED_EXIT_DONE : NESTED_EXIT_HOST;
4c2161ae
JR
2649}
2650
ab2f4d73
LP
2651/* DB exceptions for our internal use must not cause vmexit */
2652static int nested_svm_intercept_db(struct vcpu_svm *svm)
2653{
2654 unsigned long dr6;
2655
2656 /* if we're not singlestepping, it's not ours */
2657 if (!svm->nmi_singlestep)
2658 return NESTED_EXIT_DONE;
2659
2660 /* if it's not a singlestep exception, it's not ours */
2661 if (kvm_get_dr(&svm->vcpu, 6, &dr6))
2662 return NESTED_EXIT_DONE;
2663 if (!(dr6 & DR6_BS))
2664 return NESTED_EXIT_DONE;
2665
2666 /* if the guest is singlestepping, it should get the vmexit */
2667 if (svm->nmi_singlestep_guest_rflags & X86_EFLAGS_TF) {
2668 disable_nmi_singlestep(svm);
2669 return NESTED_EXIT_DONE;
2670 }
2671
2672 /* it's ours, the nested hypervisor must not see this one */
2673 return NESTED_EXIT_HOST;
2674}
2675
410e4d57 2676static int nested_svm_exit_special(struct vcpu_svm *svm)
cf74a78b 2677{
cf74a78b 2678 u32 exit_code = svm->vmcb->control.exit_code;
4c2161ae 2679
410e4d57
JR
2680 switch (exit_code) {
2681 case SVM_EXIT_INTR:
2682 case SVM_EXIT_NMI:
ff47a49b 2683 case SVM_EXIT_EXCP_BASE + MC_VECTOR:
410e4d57 2684 return NESTED_EXIT_HOST;
410e4d57 2685 case SVM_EXIT_NPF:
e0231715 2686 /* For now we are always handling NPFs when using them */
410e4d57
JR
2687 if (npt_enabled)
2688 return NESTED_EXIT_HOST;
2689 break;
410e4d57 2690 case SVM_EXIT_EXCP_BASE + PF_VECTOR:
631bc487 2691 /* When we're shadowing, trap PFs, but not async PF */
1261bfa3 2692 if (!npt_enabled && svm->vcpu.arch.apf.host_apf_reason == 0)
410e4d57
JR
2693 return NESTED_EXIT_HOST;
2694 break;
2695 default:
2696 break;
cf74a78b
AG
2697 }
2698
410e4d57
JR
2699 return NESTED_EXIT_CONTINUE;
2700}
2701
2702/*
2703 * If this function returns true, this #vmexit was already handled
2704 */
b8e88bc8 2705static int nested_svm_intercept(struct vcpu_svm *svm)
410e4d57
JR
2706{
2707 u32 exit_code = svm->vmcb->control.exit_code;
2708 int vmexit = NESTED_EXIT_HOST;
2709
cf74a78b 2710 switch (exit_code) {
9c4e40b9 2711 case SVM_EXIT_MSR:
3d62d9aa 2712 vmexit = nested_svm_exit_handled_msr(svm);
9c4e40b9 2713 break;
ce2ac085
JR
2714 case SVM_EXIT_IOIO:
2715 vmexit = nested_svm_intercept_ioio(svm);
2716 break;
4ee546b4
RJ
2717 case SVM_EXIT_READ_CR0 ... SVM_EXIT_WRITE_CR8: {
2718 u32 bit = 1U << (exit_code - SVM_EXIT_READ_CR0);
2719 if (svm->nested.intercept_cr & bit)
410e4d57 2720 vmexit = NESTED_EXIT_DONE;
cf74a78b
AG
2721 break;
2722 }
3aed041a
JR
2723 case SVM_EXIT_READ_DR0 ... SVM_EXIT_WRITE_DR7: {
2724 u32 bit = 1U << (exit_code - SVM_EXIT_READ_DR0);
2725 if (svm->nested.intercept_dr & bit)
410e4d57 2726 vmexit = NESTED_EXIT_DONE;
cf74a78b
AG
2727 break;
2728 }
2729 case SVM_EXIT_EXCP_BASE ... SVM_EXIT_EXCP_BASE + 0x1f: {
2730 u32 excp_bits = 1 << (exit_code - SVM_EXIT_EXCP_BASE);
ab2f4d73
LP
2731 if (svm->nested.intercept_exceptions & excp_bits) {
2732 if (exit_code == SVM_EXIT_EXCP_BASE + DB_VECTOR)
2733 vmexit = nested_svm_intercept_db(svm);
2734 else
2735 vmexit = NESTED_EXIT_DONE;
2736 }
631bc487
GN
2737 /* async page fault always cause vmexit */
2738 else if ((exit_code == SVM_EXIT_EXCP_BASE + PF_VECTOR) &&
adfe20fb 2739 svm->vcpu.arch.exception.nested_apf != 0)
631bc487 2740 vmexit = NESTED_EXIT_DONE;
cf74a78b
AG
2741 break;
2742 }
228070b1
JR
2743 case SVM_EXIT_ERR: {
2744 vmexit = NESTED_EXIT_DONE;
2745 break;
2746 }
cf74a78b
AG
2747 default: {
2748 u64 exit_bits = 1ULL << (exit_code - SVM_EXIT_INTR);
aad42c64 2749 if (svm->nested.intercept & exit_bits)
410e4d57 2750 vmexit = NESTED_EXIT_DONE;
cf74a78b
AG
2751 }
2752 }
2753
b8e88bc8
JR
2754 return vmexit;
2755}
2756
2757static int nested_svm_exit_handled(struct vcpu_svm *svm)
2758{
2759 int vmexit;
2760
2761 vmexit = nested_svm_intercept(svm);
2762
2763 if (vmexit == NESTED_EXIT_DONE)
9c4e40b9 2764 nested_svm_vmexit(svm);
9c4e40b9
JR
2765
2766 return vmexit;
cf74a78b
AG
2767}
2768
0460a979
JR
2769static inline void copy_vmcb_control_area(struct vmcb *dst_vmcb, struct vmcb *from_vmcb)
2770{
2771 struct vmcb_control_area *dst = &dst_vmcb->control;
2772 struct vmcb_control_area *from = &from_vmcb->control;
2773
4ee546b4 2774 dst->intercept_cr = from->intercept_cr;
3aed041a 2775 dst->intercept_dr = from->intercept_dr;
0460a979
JR
2776 dst->intercept_exceptions = from->intercept_exceptions;
2777 dst->intercept = from->intercept;
2778 dst->iopm_base_pa = from->iopm_base_pa;
2779 dst->msrpm_base_pa = from->msrpm_base_pa;
2780 dst->tsc_offset = from->tsc_offset;
2781 dst->asid = from->asid;
2782 dst->tlb_ctl = from->tlb_ctl;
2783 dst->int_ctl = from->int_ctl;
2784 dst->int_vector = from->int_vector;
2785 dst->int_state = from->int_state;
2786 dst->exit_code = from->exit_code;
2787 dst->exit_code_hi = from->exit_code_hi;
2788 dst->exit_info_1 = from->exit_info_1;
2789 dst->exit_info_2 = from->exit_info_2;
2790 dst->exit_int_info = from->exit_int_info;
2791 dst->exit_int_info_err = from->exit_int_info_err;
2792 dst->nested_ctl = from->nested_ctl;
2793 dst->event_inj = from->event_inj;
2794 dst->event_inj_err = from->event_inj_err;
2795 dst->nested_cr3 = from->nested_cr3;
0dc92119 2796 dst->virt_ext = from->virt_ext;
0460a979
JR
2797}
2798
34f80cfa 2799static int nested_svm_vmexit(struct vcpu_svm *svm)
cf74a78b 2800{
34f80cfa 2801 struct vmcb *nested_vmcb;
e6aa9abd 2802 struct vmcb *hsave = svm->nested.hsave;
33740e40 2803 struct vmcb *vmcb = svm->vmcb;
7597f129 2804 struct page *page;
cf74a78b 2805
17897f36
JR
2806 trace_kvm_nested_vmexit_inject(vmcb->control.exit_code,
2807 vmcb->control.exit_info_1,
2808 vmcb->control.exit_info_2,
2809 vmcb->control.exit_int_info,
e097e5ff
SH
2810 vmcb->control.exit_int_info_err,
2811 KVM_ISA_SVM);
17897f36 2812
7597f129 2813 nested_vmcb = nested_svm_map(svm, svm->nested.vmcb, &page);
34f80cfa
JR
2814 if (!nested_vmcb)
2815 return 1;
2816
2030753d
JR
2817 /* Exit Guest-Mode */
2818 leave_guest_mode(&svm->vcpu);
06fc7772
JR
2819 svm->nested.vmcb = 0;
2820
cf74a78b 2821 /* Give the current vmcb to the guest */
33740e40
JR
2822 disable_gif(svm);
2823
2824 nested_vmcb->save.es = vmcb->save.es;
2825 nested_vmcb->save.cs = vmcb->save.cs;
2826 nested_vmcb->save.ss = vmcb->save.ss;
2827 nested_vmcb->save.ds = vmcb->save.ds;
2828 nested_vmcb->save.gdtr = vmcb->save.gdtr;
2829 nested_vmcb->save.idtr = vmcb->save.idtr;
3f6a9d16 2830 nested_vmcb->save.efer = svm->vcpu.arch.efer;
cdbbdc12 2831 nested_vmcb->save.cr0 = kvm_read_cr0(&svm->vcpu);
9f8fe504 2832 nested_vmcb->save.cr3 = kvm_read_cr3(&svm->vcpu);
33740e40 2833 nested_vmcb->save.cr2 = vmcb->save.cr2;
cdbbdc12 2834 nested_vmcb->save.cr4 = svm->vcpu.arch.cr4;
f6e78475 2835 nested_vmcb->save.rflags = kvm_get_rflags(&svm->vcpu);
33740e40
JR
2836 nested_vmcb->save.rip = vmcb->save.rip;
2837 nested_vmcb->save.rsp = vmcb->save.rsp;
2838 nested_vmcb->save.rax = vmcb->save.rax;
2839 nested_vmcb->save.dr7 = vmcb->save.dr7;
2840 nested_vmcb->save.dr6 = vmcb->save.dr6;
2841 nested_vmcb->save.cpl = vmcb->save.cpl;
2842
2843 nested_vmcb->control.int_ctl = vmcb->control.int_ctl;
2844 nested_vmcb->control.int_vector = vmcb->control.int_vector;
2845 nested_vmcb->control.int_state = vmcb->control.int_state;
2846 nested_vmcb->control.exit_code = vmcb->control.exit_code;
2847 nested_vmcb->control.exit_code_hi = vmcb->control.exit_code_hi;
2848 nested_vmcb->control.exit_info_1 = vmcb->control.exit_info_1;
2849 nested_vmcb->control.exit_info_2 = vmcb->control.exit_info_2;
2850 nested_vmcb->control.exit_int_info = vmcb->control.exit_int_info;
2851 nested_vmcb->control.exit_int_info_err = vmcb->control.exit_int_info_err;
6092d3d3
JR
2852
2853 if (svm->nrips_enabled)
2854 nested_vmcb->control.next_rip = vmcb->control.next_rip;
8d23c466
AG
2855
2856 /*
2857 * If we emulate a VMRUN/#VMEXIT in the same host #vmexit cycle we have
2858 * to make sure that we do not lose injected events. So check event_inj
2859 * here and copy it to exit_int_info if it is valid.
2860 * Exit_int_info and event_inj can't be both valid because the case
2861 * below only happens on a VMRUN instruction intercept which has
2862 * no valid exit_int_info set.
2863 */
2864 if (vmcb->control.event_inj & SVM_EVTINJ_VALID) {
2865 struct vmcb_control_area *nc = &nested_vmcb->control;
2866
2867 nc->exit_int_info = vmcb->control.event_inj;
2868 nc->exit_int_info_err = vmcb->control.event_inj_err;
2869 }
2870
33740e40
JR
2871 nested_vmcb->control.tlb_ctl = 0;
2872 nested_vmcb->control.event_inj = 0;
2873 nested_vmcb->control.event_inj_err = 0;
cf74a78b
AG
2874
2875 /* We always set V_INTR_MASKING and remember the old value in hflags */
2876 if (!(svm->vcpu.arch.hflags & HF_VINTR_MASK))
2877 nested_vmcb->control.int_ctl &= ~V_INTR_MASKING_MASK;
2878
cf74a78b 2879 /* Restore the original control entries */
0460a979 2880 copy_vmcb_control_area(vmcb, hsave);
cf74a78b 2881
219b65dc
AG
2882 kvm_clear_exception_queue(&svm->vcpu);
2883 kvm_clear_interrupt_queue(&svm->vcpu);
cf74a78b 2884
4b16184c
JR
2885 svm->nested.nested_cr3 = 0;
2886
cf74a78b
AG
2887 /* Restore selected save entries */
2888 svm->vmcb->save.es = hsave->save.es;
2889 svm->vmcb->save.cs = hsave->save.cs;
2890 svm->vmcb->save.ss = hsave->save.ss;
2891 svm->vmcb->save.ds = hsave->save.ds;
2892 svm->vmcb->save.gdtr = hsave->save.gdtr;
2893 svm->vmcb->save.idtr = hsave->save.idtr;
f6e78475 2894 kvm_set_rflags(&svm->vcpu, hsave->save.rflags);
cf74a78b
AG
2895 svm_set_efer(&svm->vcpu, hsave->save.efer);
2896 svm_set_cr0(&svm->vcpu, hsave->save.cr0 | X86_CR0_PE);
2897 svm_set_cr4(&svm->vcpu, hsave->save.cr4);
2898 if (npt_enabled) {
2899 svm->vmcb->save.cr3 = hsave->save.cr3;
2900 svm->vcpu.arch.cr3 = hsave->save.cr3;
2901 } else {
2390218b 2902 (void)kvm_set_cr3(&svm->vcpu, hsave->save.cr3);
cf74a78b
AG
2903 }
2904 kvm_register_write(&svm->vcpu, VCPU_REGS_RAX, hsave->save.rax);
2905 kvm_register_write(&svm->vcpu, VCPU_REGS_RSP, hsave->save.rsp);
2906 kvm_register_write(&svm->vcpu, VCPU_REGS_RIP, hsave->save.rip);
2907 svm->vmcb->save.dr7 = 0;
2908 svm->vmcb->save.cpl = 0;
2909 svm->vmcb->control.exit_int_info = 0;
2910
8d28fec4
RJ
2911 mark_all_dirty(svm->vmcb);
2912
7597f129 2913 nested_svm_unmap(page);
cf74a78b 2914
4b16184c 2915 nested_svm_uninit_mmu_context(&svm->vcpu);
cf74a78b
AG
2916 kvm_mmu_reset_context(&svm->vcpu);
2917 kvm_mmu_load(&svm->vcpu);
2918
2919 return 0;
2920}
3d6368ef 2921
9738b2c9 2922static bool nested_svm_vmrun_msrpm(struct vcpu_svm *svm)
3d6368ef 2923{
323c3d80
JR
2924 /*
2925 * This function merges the msr permission bitmaps of kvm and the
c5ec2e56 2926 * nested vmcb. It is optimized in that it only merges the parts where
323c3d80
JR
2927 * the kvm msr permission bitmap may contain zero bits
2928 */
3d6368ef 2929 int i;
9738b2c9 2930
323c3d80
JR
2931 if (!(svm->nested.intercept & (1ULL << INTERCEPT_MSR_PROT)))
2932 return true;
9738b2c9 2933
323c3d80
JR
2934 for (i = 0; i < MSRPM_OFFSETS; i++) {
2935 u32 value, p;
2936 u64 offset;
9738b2c9 2937
323c3d80
JR
2938 if (msrpm_offsets[i] == 0xffffffff)
2939 break;
3d6368ef 2940
0d6b3537
JR
2941 p = msrpm_offsets[i];
2942 offset = svm->nested.vmcb_msrpm + (p * 4);
323c3d80 2943
54bf36aa 2944 if (kvm_vcpu_read_guest(&svm->vcpu, offset, &value, 4))
323c3d80
JR
2945 return false;
2946
2947 svm->nested.msrpm[p] = svm->msrpm[p] | value;
2948 }
3d6368ef 2949
d0ec49d4 2950 svm->vmcb->control.msrpm_base_pa = __sme_set(__pa(svm->nested.msrpm));
9738b2c9
JR
2951
2952 return true;
3d6368ef
AG
2953}
2954
52c65a30
JR
2955static bool nested_vmcb_checks(struct vmcb *vmcb)
2956{
2957 if ((vmcb->control.intercept & (1ULL << INTERCEPT_VMRUN)) == 0)
2958 return false;
2959
dbe77584
JR
2960 if (vmcb->control.asid == 0)
2961 return false;
2962
4b16184c
JR
2963 if (vmcb->control.nested_ctl && !npt_enabled)
2964 return false;
2965
52c65a30
JR
2966 return true;
2967}
2968
c2634065
LP
2969static void enter_svm_guest_mode(struct vcpu_svm *svm, u64 vmcb_gpa,
2970 struct vmcb *nested_vmcb, struct page *page)
3d6368ef 2971{
f6e78475 2972 if (kvm_get_rflags(&svm->vcpu) & X86_EFLAGS_IF)
3d6368ef
AG
2973 svm->vcpu.arch.hflags |= HF_HIF_MASK;
2974 else
2975 svm->vcpu.arch.hflags &= ~HF_HIF_MASK;
2976
4b16184c
JR
2977 if (nested_vmcb->control.nested_ctl) {
2978 kvm_mmu_unload(&svm->vcpu);
2979 svm->nested.nested_cr3 = nested_vmcb->control.nested_cr3;
2980 nested_svm_init_mmu_context(&svm->vcpu);
2981 }
2982
3d6368ef
AG
2983 /* Load the nested guest state */
2984 svm->vmcb->save.es = nested_vmcb->save.es;
2985 svm->vmcb->save.cs = nested_vmcb->save.cs;
2986 svm->vmcb->save.ss = nested_vmcb->save.ss;
2987 svm->vmcb->save.ds = nested_vmcb->save.ds;
2988 svm->vmcb->save.gdtr = nested_vmcb->save.gdtr;
2989 svm->vmcb->save.idtr = nested_vmcb->save.idtr;
f6e78475 2990 kvm_set_rflags(&svm->vcpu, nested_vmcb->save.rflags);
3d6368ef
AG
2991 svm_set_efer(&svm->vcpu, nested_vmcb->save.efer);
2992 svm_set_cr0(&svm->vcpu, nested_vmcb->save.cr0);
2993 svm_set_cr4(&svm->vcpu, nested_vmcb->save.cr4);
2994 if (npt_enabled) {
2995 svm->vmcb->save.cr3 = nested_vmcb->save.cr3;
2996 svm->vcpu.arch.cr3 = nested_vmcb->save.cr3;
0e5cbe36 2997 } else
2390218b 2998 (void)kvm_set_cr3(&svm->vcpu, nested_vmcb->save.cr3);
0e5cbe36
JR
2999
3000 /* Guest paging mode is active - reset mmu */
3001 kvm_mmu_reset_context(&svm->vcpu);
3002
defbba56 3003 svm->vmcb->save.cr2 = svm->vcpu.arch.cr2 = nested_vmcb->save.cr2;
3d6368ef
AG
3004 kvm_register_write(&svm->vcpu, VCPU_REGS_RAX, nested_vmcb->save.rax);
3005 kvm_register_write(&svm->vcpu, VCPU_REGS_RSP, nested_vmcb->save.rsp);
3006 kvm_register_write(&svm->vcpu, VCPU_REGS_RIP, nested_vmcb->save.rip);
e0231715 3007
3d6368ef
AG
3008 /* In case we don't even reach vcpu_run, the fields are not updated */
3009 svm->vmcb->save.rax = nested_vmcb->save.rax;
3010 svm->vmcb->save.rsp = nested_vmcb->save.rsp;
3011 svm->vmcb->save.rip = nested_vmcb->save.rip;
3012 svm->vmcb->save.dr7 = nested_vmcb->save.dr7;
3013 svm->vmcb->save.dr6 = nested_vmcb->save.dr6;
3014 svm->vmcb->save.cpl = nested_vmcb->save.cpl;
3015
f7138538 3016 svm->nested.vmcb_msrpm = nested_vmcb->control.msrpm_base_pa & ~0x0fffULL;
ce2ac085 3017 svm->nested.vmcb_iopm = nested_vmcb->control.iopm_base_pa & ~0x0fffULL;
3d6368ef 3018
aad42c64 3019 /* cache intercepts */
4ee546b4 3020 svm->nested.intercept_cr = nested_vmcb->control.intercept_cr;
3aed041a 3021 svm->nested.intercept_dr = nested_vmcb->control.intercept_dr;
aad42c64
JR
3022 svm->nested.intercept_exceptions = nested_vmcb->control.intercept_exceptions;
3023 svm->nested.intercept = nested_vmcb->control.intercept;
3024
f40f6a45 3025 svm_flush_tlb(&svm->vcpu);
3d6368ef 3026 svm->vmcb->control.int_ctl = nested_vmcb->control.int_ctl | V_INTR_MASKING_MASK;
3d6368ef
AG
3027 if (nested_vmcb->control.int_ctl & V_INTR_MASKING_MASK)
3028 svm->vcpu.arch.hflags |= HF_VINTR_MASK;
3029 else
3030 svm->vcpu.arch.hflags &= ~HF_VINTR_MASK;
3031
88ab24ad
JR
3032 if (svm->vcpu.arch.hflags & HF_VINTR_MASK) {
3033 /* We only want the cr8 intercept bits of the guest */
4ee546b4
RJ
3034 clr_cr_intercept(svm, INTERCEPT_CR8_READ);
3035 clr_cr_intercept(svm, INTERCEPT_CR8_WRITE);
88ab24ad
JR
3036 }
3037
0d945bd9 3038 /* We don't want to see VMMCALLs from a nested guest */
8a05a1b8 3039 clr_intercept(svm, INTERCEPT_VMMCALL);
0d945bd9 3040
0dc92119 3041 svm->vmcb->control.virt_ext = nested_vmcb->control.virt_ext;
3d6368ef
AG
3042 svm->vmcb->control.int_vector = nested_vmcb->control.int_vector;
3043 svm->vmcb->control.int_state = nested_vmcb->control.int_state;
3044 svm->vmcb->control.tsc_offset += nested_vmcb->control.tsc_offset;
3d6368ef
AG
3045 svm->vmcb->control.event_inj = nested_vmcb->control.event_inj;
3046 svm->vmcb->control.event_inj_err = nested_vmcb->control.event_inj_err;
3047
7597f129 3048 nested_svm_unmap(page);
9738b2c9 3049
2030753d
JR
3050 /* Enter Guest-Mode */
3051 enter_guest_mode(&svm->vcpu);
3052
384c6368
JR
3053 /*
3054 * Merge guest and host intercepts - must be called with vcpu in
3055 * guest-mode to take affect here
3056 */
3057 recalc_intercepts(svm);
3058
06fc7772 3059 svm->nested.vmcb = vmcb_gpa;
9738b2c9 3060
2af9194d 3061 enable_gif(svm);
3d6368ef 3062
8d28fec4 3063 mark_all_dirty(svm->vmcb);
c2634065
LP
3064}
3065
3066static bool nested_svm_vmrun(struct vcpu_svm *svm)
3067{
3068 struct vmcb *nested_vmcb;
3069 struct vmcb *hsave = svm->nested.hsave;
3070 struct vmcb *vmcb = svm->vmcb;
3071 struct page *page;
3072 u64 vmcb_gpa;
3073
3074 vmcb_gpa = svm->vmcb->save.rax;
3075
3076 nested_vmcb = nested_svm_map(svm, svm->vmcb->save.rax, &page);
3077 if (!nested_vmcb)
3078 return false;
3079
3080 if (!nested_vmcb_checks(nested_vmcb)) {
3081 nested_vmcb->control.exit_code = SVM_EXIT_ERR;
3082 nested_vmcb->control.exit_code_hi = 0;
3083 nested_vmcb->control.exit_info_1 = 0;
3084 nested_vmcb->control.exit_info_2 = 0;
3085
3086 nested_svm_unmap(page);
3087
3088 return false;
3089 }
3090
3091 trace_kvm_nested_vmrun(svm->vmcb->save.rip, vmcb_gpa,
3092 nested_vmcb->save.rip,
3093 nested_vmcb->control.int_ctl,
3094 nested_vmcb->control.event_inj,
3095 nested_vmcb->control.nested_ctl);
3096
3097 trace_kvm_nested_intercepts(nested_vmcb->control.intercept_cr & 0xffff,
3098 nested_vmcb->control.intercept_cr >> 16,
3099 nested_vmcb->control.intercept_exceptions,
3100 nested_vmcb->control.intercept);
3101
3102 /* Clear internal status */
3103 kvm_clear_exception_queue(&svm->vcpu);
3104 kvm_clear_interrupt_queue(&svm->vcpu);
3105
3106 /*
3107 * Save the old vmcb, so we don't need to pick what we save, but can
3108 * restore everything when a VMEXIT occurs
3109 */
3110 hsave->save.es = vmcb->save.es;
3111 hsave->save.cs = vmcb->save.cs;
3112 hsave->save.ss = vmcb->save.ss;
3113 hsave->save.ds = vmcb->save.ds;
3114 hsave->save.gdtr = vmcb->save.gdtr;
3115 hsave->save.idtr = vmcb->save.idtr;
3116 hsave->save.efer = svm->vcpu.arch.efer;
3117 hsave->save.cr0 = kvm_read_cr0(&svm->vcpu);
3118 hsave->save.cr4 = svm->vcpu.arch.cr4;
3119 hsave->save.rflags = kvm_get_rflags(&svm->vcpu);
3120 hsave->save.rip = kvm_rip_read(&svm->vcpu);
3121 hsave->save.rsp = vmcb->save.rsp;
3122 hsave->save.rax = vmcb->save.rax;
3123 if (npt_enabled)
3124 hsave->save.cr3 = vmcb->save.cr3;
3125 else
3126 hsave->save.cr3 = kvm_read_cr3(&svm->vcpu);
3127
3128 copy_vmcb_control_area(hsave, vmcb);
3129
3130 enter_svm_guest_mode(svm, vmcb_gpa, nested_vmcb, page);
8d28fec4 3131
9738b2c9 3132 return true;
3d6368ef
AG
3133}
3134
9966bf68 3135static void nested_svm_vmloadsave(struct vmcb *from_vmcb, struct vmcb *to_vmcb)
5542675b
AG
3136{
3137 to_vmcb->save.fs = from_vmcb->save.fs;
3138 to_vmcb->save.gs = from_vmcb->save.gs;
3139 to_vmcb->save.tr = from_vmcb->save.tr;
3140 to_vmcb->save.ldtr = from_vmcb->save.ldtr;
3141 to_vmcb->save.kernel_gs_base = from_vmcb->save.kernel_gs_base;
3142 to_vmcb->save.star = from_vmcb->save.star;
3143 to_vmcb->save.lstar = from_vmcb->save.lstar;
3144 to_vmcb->save.cstar = from_vmcb->save.cstar;
3145 to_vmcb->save.sfmask = from_vmcb->save.sfmask;
3146 to_vmcb->save.sysenter_cs = from_vmcb->save.sysenter_cs;
3147 to_vmcb->save.sysenter_esp = from_vmcb->save.sysenter_esp;
3148 to_vmcb->save.sysenter_eip = from_vmcb->save.sysenter_eip;
5542675b
AG
3149}
3150
851ba692 3151static int vmload_interception(struct vcpu_svm *svm)
5542675b 3152{
9966bf68 3153 struct vmcb *nested_vmcb;
7597f129 3154 struct page *page;
b742c1e6 3155 int ret;
9966bf68 3156
5542675b
AG
3157 if (nested_svm_check_permissions(svm))
3158 return 1;
3159
7597f129 3160 nested_vmcb = nested_svm_map(svm, svm->vmcb->save.rax, &page);
9966bf68
JR
3161 if (!nested_vmcb)
3162 return 1;
3163
e3e9ed3d 3164 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
b742c1e6 3165 ret = kvm_skip_emulated_instruction(&svm->vcpu);
e3e9ed3d 3166
9966bf68 3167 nested_svm_vmloadsave(nested_vmcb, svm->vmcb);
7597f129 3168 nested_svm_unmap(page);
5542675b 3169
b742c1e6 3170 return ret;
5542675b
AG
3171}
3172
851ba692 3173static int vmsave_interception(struct vcpu_svm *svm)
5542675b 3174{
9966bf68 3175 struct vmcb *nested_vmcb;
7597f129 3176 struct page *page;
b742c1e6 3177 int ret;
9966bf68 3178
5542675b
AG
3179 if (nested_svm_check_permissions(svm))
3180 return 1;
3181
7597f129 3182 nested_vmcb = nested_svm_map(svm, svm->vmcb->save.rax, &page);
9966bf68
JR
3183 if (!nested_vmcb)
3184 return 1;
3185
e3e9ed3d 3186 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
b742c1e6 3187 ret = kvm_skip_emulated_instruction(&svm->vcpu);
e3e9ed3d 3188
9966bf68 3189 nested_svm_vmloadsave(svm->vmcb, nested_vmcb);
7597f129 3190 nested_svm_unmap(page);
5542675b 3191
b742c1e6 3192 return ret;
5542675b
AG
3193}
3194
851ba692 3195static int vmrun_interception(struct vcpu_svm *svm)
3d6368ef 3196{
3d6368ef
AG
3197 if (nested_svm_check_permissions(svm))
3198 return 1;
3199
b75f4eb3
RJ
3200 /* Save rip after vmrun instruction */
3201 kvm_rip_write(&svm->vcpu, kvm_rip_read(&svm->vcpu) + 3);
3d6368ef 3202
9738b2c9 3203 if (!nested_svm_vmrun(svm))
3d6368ef
AG
3204 return 1;
3205
9738b2c9 3206 if (!nested_svm_vmrun_msrpm(svm))
1f8da478
JR
3207 goto failed;
3208
3209 return 1;
3210
3211failed:
3212
3213 svm->vmcb->control.exit_code = SVM_EXIT_ERR;
3214 svm->vmcb->control.exit_code_hi = 0;
3215 svm->vmcb->control.exit_info_1 = 0;
3216 svm->vmcb->control.exit_info_2 = 0;
3217
3218 nested_svm_vmexit(svm);
3d6368ef
AG
3219
3220 return 1;
3221}
3222
851ba692 3223static int stgi_interception(struct vcpu_svm *svm)
1371d904 3224{
b742c1e6
LP
3225 int ret;
3226
1371d904
AG
3227 if (nested_svm_check_permissions(svm))
3228 return 1;
3229
640bd6e5
JN
3230 /*
3231 * If VGIF is enabled, the STGI intercept is only added to
cc3d967f 3232 * detect the opening of the SMI/NMI window; remove it now.
640bd6e5
JN
3233 */
3234 if (vgif_enabled(svm))
3235 clr_intercept(svm, INTERCEPT_STGI);
3236
1371d904 3237 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
b742c1e6 3238 ret = kvm_skip_emulated_instruction(&svm->vcpu);
3842d135 3239 kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
1371d904 3240
2af9194d 3241 enable_gif(svm);
1371d904 3242
b742c1e6 3243 return ret;
1371d904
AG
3244}
3245
851ba692 3246static int clgi_interception(struct vcpu_svm *svm)
1371d904 3247{
b742c1e6
LP
3248 int ret;
3249
1371d904
AG
3250 if (nested_svm_check_permissions(svm))
3251 return 1;
3252
3253 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
b742c1e6 3254 ret = kvm_skip_emulated_instruction(&svm->vcpu);
1371d904 3255
2af9194d 3256 disable_gif(svm);
1371d904
AG
3257
3258 /* After a CLGI no interrupts should come */
340d3bc3
SS
3259 if (!kvm_vcpu_apicv_active(&svm->vcpu)) {
3260 svm_clear_vintr(svm);
3261 svm->vmcb->control.int_ctl &= ~V_IRQ_MASK;
3262 mark_dirty(svm->vmcb, VMCB_INTR);
3263 }
decdbf6a 3264
b742c1e6 3265 return ret;
1371d904
AG
3266}
3267
851ba692 3268static int invlpga_interception(struct vcpu_svm *svm)
ff092385
AG
3269{
3270 struct kvm_vcpu *vcpu = &svm->vcpu;
ff092385 3271
668f198f
DK
3272 trace_kvm_invlpga(svm->vmcb->save.rip, kvm_register_read(&svm->vcpu, VCPU_REGS_RCX),
3273 kvm_register_read(&svm->vcpu, VCPU_REGS_RAX));
ec1ff790 3274
ff092385 3275 /* Let's treat INVLPGA the same as INVLPG (can be optimized!) */
668f198f 3276 kvm_mmu_invlpg(vcpu, kvm_register_read(&svm->vcpu, VCPU_REGS_RAX));
ff092385
AG
3277
3278 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
b742c1e6 3279 return kvm_skip_emulated_instruction(&svm->vcpu);
ff092385
AG
3280}
3281
532a46b9
JR
3282static int skinit_interception(struct vcpu_svm *svm)
3283{
668f198f 3284 trace_kvm_skinit(svm->vmcb->save.rip, kvm_register_read(&svm->vcpu, VCPU_REGS_RAX));
532a46b9
JR
3285
3286 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
3287 return 1;
3288}
3289
dab429a7
DK
3290static int wbinvd_interception(struct vcpu_svm *svm)
3291{
6affcbed 3292 return kvm_emulate_wbinvd(&svm->vcpu);
dab429a7
DK
3293}
3294
81dd35d4
JR
3295static int xsetbv_interception(struct vcpu_svm *svm)
3296{
3297 u64 new_bv = kvm_read_edx_eax(&svm->vcpu);
3298 u32 index = kvm_register_read(&svm->vcpu, VCPU_REGS_RCX);
3299
3300 if (kvm_set_xcr(&svm->vcpu, index, new_bv) == 0) {
3301 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
b742c1e6 3302 return kvm_skip_emulated_instruction(&svm->vcpu);
81dd35d4
JR
3303 }
3304
3305 return 1;
3306}
3307
851ba692 3308static int task_switch_interception(struct vcpu_svm *svm)
6aa8b732 3309{
37817f29 3310 u16 tss_selector;
64a7ec06
GN
3311 int reason;
3312 int int_type = svm->vmcb->control.exit_int_info &
3313 SVM_EXITINTINFO_TYPE_MASK;
8317c298 3314 int int_vec = svm->vmcb->control.exit_int_info & SVM_EVTINJ_VEC_MASK;
fe8e7f83
GN
3315 uint32_t type =
3316 svm->vmcb->control.exit_int_info & SVM_EXITINTINFO_TYPE_MASK;
3317 uint32_t idt_v =
3318 svm->vmcb->control.exit_int_info & SVM_EXITINTINFO_VALID;
e269fb21
JK
3319 bool has_error_code = false;
3320 u32 error_code = 0;
37817f29
IE
3321
3322 tss_selector = (u16)svm->vmcb->control.exit_info_1;
64a7ec06 3323
37817f29
IE
3324 if (svm->vmcb->control.exit_info_2 &
3325 (1ULL << SVM_EXITINFOSHIFT_TS_REASON_IRET))
64a7ec06
GN
3326 reason = TASK_SWITCH_IRET;
3327 else if (svm->vmcb->control.exit_info_2 &
3328 (1ULL << SVM_EXITINFOSHIFT_TS_REASON_JMP))
3329 reason = TASK_SWITCH_JMP;
fe8e7f83 3330 else if (idt_v)
64a7ec06
GN
3331 reason = TASK_SWITCH_GATE;
3332 else
3333 reason = TASK_SWITCH_CALL;
3334
fe8e7f83
GN
3335 if (reason == TASK_SWITCH_GATE) {
3336 switch (type) {
3337 case SVM_EXITINTINFO_TYPE_NMI:
3338 svm->vcpu.arch.nmi_injected = false;
3339 break;
3340 case SVM_EXITINTINFO_TYPE_EXEPT:
e269fb21
JK
3341 if (svm->vmcb->control.exit_info_2 &
3342 (1ULL << SVM_EXITINFOSHIFT_TS_HAS_ERROR_CODE)) {
3343 has_error_code = true;
3344 error_code =
3345 (u32)svm->vmcb->control.exit_info_2;
3346 }
fe8e7f83
GN
3347 kvm_clear_exception_queue(&svm->vcpu);
3348 break;
3349 case SVM_EXITINTINFO_TYPE_INTR:
3350 kvm_clear_interrupt_queue(&svm->vcpu);
3351 break;
3352 default:
3353 break;
3354 }
3355 }
64a7ec06 3356
8317c298
GN
3357 if (reason != TASK_SWITCH_GATE ||
3358 int_type == SVM_EXITINTINFO_TYPE_SOFT ||
3359 (int_type == SVM_EXITINTINFO_TYPE_EXEPT &&
f629cf84
GN
3360 (int_vec == OF_VECTOR || int_vec == BP_VECTOR)))
3361 skip_emulated_instruction(&svm->vcpu);
64a7ec06 3362
7f3d35fd
KW
3363 if (int_type != SVM_EXITINTINFO_TYPE_SOFT)
3364 int_vec = -1;
3365
3366 if (kvm_task_switch(&svm->vcpu, tss_selector, int_vec, reason,
acb54517
GN
3367 has_error_code, error_code) == EMULATE_FAIL) {
3368 svm->vcpu.run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
3369 svm->vcpu.run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
3370 svm->vcpu.run->internal.ndata = 0;
3371 return 0;
3372 }
3373 return 1;
6aa8b732
AK
3374}
3375
851ba692 3376static int cpuid_interception(struct vcpu_svm *svm)
6aa8b732 3377{
5fdbf976 3378 svm->next_rip = kvm_rip_read(&svm->vcpu) + 2;
6a908b62 3379 return kvm_emulate_cpuid(&svm->vcpu);
6aa8b732
AK
3380}
3381
851ba692 3382static int iret_interception(struct vcpu_svm *svm)
95ba8273
GN
3383{
3384 ++svm->vcpu.stat.nmi_window_exits;
8a05a1b8 3385 clr_intercept(svm, INTERCEPT_IRET);
44c11430 3386 svm->vcpu.arch.hflags |= HF_IRET_MASK;
bd3d1ec3 3387 svm->nmi_iret_rip = kvm_rip_read(&svm->vcpu);
f303b4ce 3388 kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
95ba8273
GN
3389 return 1;
3390}
3391
851ba692 3392static int invlpg_interception(struct vcpu_svm *svm)
a7052897 3393{
df4f3108
AP
3394 if (!static_cpu_has(X86_FEATURE_DECODEASSISTS))
3395 return emulate_instruction(&svm->vcpu, 0) == EMULATE_DONE;
3396
3397 kvm_mmu_invlpg(&svm->vcpu, svm->vmcb->control.exit_info_1);
b742c1e6 3398 return kvm_skip_emulated_instruction(&svm->vcpu);
a7052897
MT
3399}
3400
851ba692 3401static int emulate_on_interception(struct vcpu_svm *svm)
6aa8b732 3402{
51d8b661 3403 return emulate_instruction(&svm->vcpu, 0) == EMULATE_DONE;
6aa8b732
AK
3404}
3405
332b56e4
AK
3406static int rdpmc_interception(struct vcpu_svm *svm)
3407{
3408 int err;
3409
3410 if (!static_cpu_has(X86_FEATURE_NRIPS))
3411 return emulate_on_interception(svm);
3412
3413 err = kvm_rdpmc(&svm->vcpu);
6affcbed 3414 return kvm_complete_insn_gp(&svm->vcpu, err);
332b56e4
AK
3415}
3416
52eb5a6d
XL
3417static bool check_selective_cr0_intercepted(struct vcpu_svm *svm,
3418 unsigned long val)
628afd2a
JR
3419{
3420 unsigned long cr0 = svm->vcpu.arch.cr0;
3421 bool ret = false;
3422 u64 intercept;
3423
3424 intercept = svm->nested.intercept;
3425
3426 if (!is_guest_mode(&svm->vcpu) ||
3427 (!(intercept & (1ULL << INTERCEPT_SELECTIVE_CR0))))
3428 return false;
3429
3430 cr0 &= ~SVM_CR0_SELECTIVE_MASK;
3431 val &= ~SVM_CR0_SELECTIVE_MASK;
3432
3433 if (cr0 ^ val) {
3434 svm->vmcb->control.exit_code = SVM_EXIT_CR0_SEL_WRITE;
3435 ret = (nested_svm_exit_handled(svm) == NESTED_EXIT_DONE);
3436 }
3437
3438 return ret;
3439}
3440
7ff76d58
AP
3441#define CR_VALID (1ULL << 63)
3442
3443static int cr_interception(struct vcpu_svm *svm)
3444{
3445 int reg, cr;
3446 unsigned long val;
3447 int err;
3448
3449 if (!static_cpu_has(X86_FEATURE_DECODEASSISTS))
3450 return emulate_on_interception(svm);
3451
3452 if (unlikely((svm->vmcb->control.exit_info_1 & CR_VALID) == 0))
3453 return emulate_on_interception(svm);
3454
3455 reg = svm->vmcb->control.exit_info_1 & SVM_EXITINFO_REG_MASK;
5e57518d
DK
3456 if (svm->vmcb->control.exit_code == SVM_EXIT_CR0_SEL_WRITE)
3457 cr = SVM_EXIT_WRITE_CR0 - SVM_EXIT_READ_CR0;
3458 else
3459 cr = svm->vmcb->control.exit_code - SVM_EXIT_READ_CR0;
7ff76d58
AP
3460
3461 err = 0;
3462 if (cr >= 16) { /* mov to cr */
3463 cr -= 16;
3464 val = kvm_register_read(&svm->vcpu, reg);
3465 switch (cr) {
3466 case 0:
628afd2a
JR
3467 if (!check_selective_cr0_intercepted(svm, val))
3468 err = kvm_set_cr0(&svm->vcpu, val);
977b2d03
JR
3469 else
3470 return 1;
3471
7ff76d58
AP
3472 break;
3473 case 3:
3474 err = kvm_set_cr3(&svm->vcpu, val);
3475 break;
3476 case 4:
3477 err = kvm_set_cr4(&svm->vcpu, val);
3478 break;
3479 case 8:
3480 err = kvm_set_cr8(&svm->vcpu, val);
3481 break;
3482 default:
3483 WARN(1, "unhandled write to CR%d", cr);
3484 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
3485 return 1;
3486 }
3487 } else { /* mov from cr */
3488 switch (cr) {
3489 case 0:
3490 val = kvm_read_cr0(&svm->vcpu);
3491 break;
3492 case 2:
3493 val = svm->vcpu.arch.cr2;
3494 break;
3495 case 3:
9f8fe504 3496 val = kvm_read_cr3(&svm->vcpu);
7ff76d58
AP
3497 break;
3498 case 4:
3499 val = kvm_read_cr4(&svm->vcpu);
3500 break;
3501 case 8:
3502 val = kvm_get_cr8(&svm->vcpu);
3503 break;
3504 default:
3505 WARN(1, "unhandled read from CR%d", cr);
3506 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
3507 return 1;
3508 }
3509 kvm_register_write(&svm->vcpu, reg, val);
3510 }
6affcbed 3511 return kvm_complete_insn_gp(&svm->vcpu, err);
7ff76d58
AP
3512}
3513
cae3797a
AP
3514static int dr_interception(struct vcpu_svm *svm)
3515{
3516 int reg, dr;
3517 unsigned long val;
cae3797a 3518
facb0139
PB
3519 if (svm->vcpu.guest_debug == 0) {
3520 /*
3521 * No more DR vmexits; force a reload of the debug registers
3522 * and reenter on this instruction. The next vmexit will
3523 * retrieve the full state of the debug registers.
3524 */
3525 clr_dr_intercepts(svm);
3526 svm->vcpu.arch.switch_db_regs |= KVM_DEBUGREG_WONT_EXIT;
3527 return 1;
3528 }
3529
cae3797a
AP
3530 if (!boot_cpu_has(X86_FEATURE_DECODEASSISTS))
3531 return emulate_on_interception(svm);
3532
3533 reg = svm->vmcb->control.exit_info_1 & SVM_EXITINFO_REG_MASK;
3534 dr = svm->vmcb->control.exit_code - SVM_EXIT_READ_DR0;
3535
3536 if (dr >= 16) { /* mov to DRn */
16f8a6f9
NA
3537 if (!kvm_require_dr(&svm->vcpu, dr - 16))
3538 return 1;
cae3797a
AP
3539 val = kvm_register_read(&svm->vcpu, reg);
3540 kvm_set_dr(&svm->vcpu, dr - 16, val);
3541 } else {
16f8a6f9
NA
3542 if (!kvm_require_dr(&svm->vcpu, dr))
3543 return 1;
3544 kvm_get_dr(&svm->vcpu, dr, &val);
3545 kvm_register_write(&svm->vcpu, reg, val);
cae3797a
AP
3546 }
3547
b742c1e6 3548 return kvm_skip_emulated_instruction(&svm->vcpu);
cae3797a
AP
3549}
3550
851ba692 3551static int cr8_write_interception(struct vcpu_svm *svm)
1d075434 3552{
851ba692 3553 struct kvm_run *kvm_run = svm->vcpu.run;
eea1cff9 3554 int r;
851ba692 3555
0a5fff19
GN
3556 u8 cr8_prev = kvm_get_cr8(&svm->vcpu);
3557 /* instruction emulation calls kvm_set_cr8() */
7ff76d58 3558 r = cr_interception(svm);
35754c98 3559 if (lapic_in_kernel(&svm->vcpu))
7ff76d58 3560 return r;
0a5fff19 3561 if (cr8_prev <= kvm_get_cr8(&svm->vcpu))
7ff76d58 3562 return r;
1d075434
JR
3563 kvm_run->exit_reason = KVM_EXIT_SET_TPR;
3564 return 0;
3565}
3566
609e36d3 3567static int svm_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
6aa8b732 3568{
a2fa3e9f
GH
3569 struct vcpu_svm *svm = to_svm(vcpu);
3570
609e36d3 3571 switch (msr_info->index) {
af24a4e4 3572 case MSR_IA32_TSC: {
609e36d3 3573 msr_info->data = svm->vmcb->control.tsc_offset +
35181e86 3574 kvm_scale_tsc(vcpu, rdtsc());
fbc0db76 3575
6aa8b732
AK
3576 break;
3577 }
8c06585d 3578 case MSR_STAR:
609e36d3 3579 msr_info->data = svm->vmcb->save.star;
6aa8b732 3580 break;
0e859cac 3581#ifdef CONFIG_X86_64
6aa8b732 3582 case MSR_LSTAR:
609e36d3 3583 msr_info->data = svm->vmcb->save.lstar;
6aa8b732
AK
3584 break;
3585 case MSR_CSTAR:
609e36d3 3586 msr_info->data = svm->vmcb->save.cstar;
6aa8b732
AK
3587 break;
3588 case MSR_KERNEL_GS_BASE:
609e36d3 3589 msr_info->data = svm->vmcb->save.kernel_gs_base;
6aa8b732
AK
3590 break;
3591 case MSR_SYSCALL_MASK:
609e36d3 3592 msr_info->data = svm->vmcb->save.sfmask;
6aa8b732
AK
3593 break;
3594#endif
3595 case MSR_IA32_SYSENTER_CS:
609e36d3 3596 msr_info->data = svm->vmcb->save.sysenter_cs;
6aa8b732
AK
3597 break;
3598 case MSR_IA32_SYSENTER_EIP:
609e36d3 3599 msr_info->data = svm->sysenter_eip;
6aa8b732
AK
3600 break;
3601 case MSR_IA32_SYSENTER_ESP:
609e36d3 3602 msr_info->data = svm->sysenter_esp;
6aa8b732 3603 break;
46896c73
PB
3604 case MSR_TSC_AUX:
3605 if (!boot_cpu_has(X86_FEATURE_RDTSCP))
3606 return 1;
3607 msr_info->data = svm->tsc_aux;
3608 break;
e0231715
JR
3609 /*
3610 * Nobody will change the following 5 values in the VMCB so we can
3611 * safely return them on rdmsr. They will always be 0 until LBRV is
3612 * implemented.
3613 */
a2938c80 3614 case MSR_IA32_DEBUGCTLMSR:
609e36d3 3615 msr_info->data = svm->vmcb->save.dbgctl;
a2938c80
JR
3616 break;
3617 case MSR_IA32_LASTBRANCHFROMIP:
609e36d3 3618 msr_info->data = svm->vmcb->save.br_from;
a2938c80
JR
3619 break;
3620 case MSR_IA32_LASTBRANCHTOIP:
609e36d3 3621 msr_info->data = svm->vmcb->save.br_to;
a2938c80
JR
3622 break;
3623 case MSR_IA32_LASTINTFROMIP:
609e36d3 3624 msr_info->data = svm->vmcb->save.last_excp_from;
a2938c80
JR
3625 break;
3626 case MSR_IA32_LASTINTTOIP:
609e36d3 3627 msr_info->data = svm->vmcb->save.last_excp_to;
a2938c80 3628 break;
b286d5d8 3629 case MSR_VM_HSAVE_PA:
609e36d3 3630 msr_info->data = svm->nested.hsave_msr;
b286d5d8 3631 break;
eb6f302e 3632 case MSR_VM_CR:
609e36d3 3633 msr_info->data = svm->nested.vm_cr_msr;
eb6f302e 3634 break;
a57b802c
KA
3635 case MSR_IA32_SPEC_CTRL:
3636 if (!msr_info->host_initiated &&
3637 !guest_cpuid_has(vcpu, X86_FEATURE_IBRS))
3638 return 1;
3639
3640 msr_info->data = svm->spec_ctrl;
3641 break;
c8a73f18 3642 case MSR_IA32_UCODE_REV:
609e36d3 3643 msr_info->data = 0x01000065;
c8a73f18 3644 break;
ae8b7875
BP
3645 case MSR_F15H_IC_CFG: {
3646
3647 int family, model;
3648
3649 family = guest_cpuid_family(vcpu);
3650 model = guest_cpuid_model(vcpu);
3651
3652 if (family < 0 || model < 0)
3653 return kvm_get_msr_common(vcpu, msr_info);
3654
3655 msr_info->data = 0;
3656
3657 if (family == 0x15 &&
3658 (model >= 0x2 && model < 0x20))
3659 msr_info->data = 0x1E;
3660 }
3661 break;
6aa8b732 3662 default:
609e36d3 3663 return kvm_get_msr_common(vcpu, msr_info);
6aa8b732
AK
3664 }
3665 return 0;
3666}
3667
851ba692 3668static int rdmsr_interception(struct vcpu_svm *svm)
6aa8b732 3669{
668f198f 3670 u32 ecx = kvm_register_read(&svm->vcpu, VCPU_REGS_RCX);
609e36d3 3671 struct msr_data msr_info;
6aa8b732 3672
609e36d3
PB
3673 msr_info.index = ecx;
3674 msr_info.host_initiated = false;
3675 if (svm_get_msr(&svm->vcpu, &msr_info)) {
59200273 3676 trace_kvm_msr_read_ex(ecx);
c1a5d4f9 3677 kvm_inject_gp(&svm->vcpu, 0);
b742c1e6 3678 return 1;
59200273 3679 } else {
609e36d3 3680 trace_kvm_msr_read(ecx, msr_info.data);
af9ca2d7 3681
609e36d3
PB
3682 kvm_register_write(&svm->vcpu, VCPU_REGS_RAX,
3683 msr_info.data & 0xffffffff);
3684 kvm_register_write(&svm->vcpu, VCPU_REGS_RDX,
3685 msr_info.data >> 32);
5fdbf976 3686 svm->next_rip = kvm_rip_read(&svm->vcpu) + 2;
b742c1e6 3687 return kvm_skip_emulated_instruction(&svm->vcpu);
6aa8b732 3688 }
6aa8b732
AK
3689}
3690
4a810181
JR
3691static int svm_set_vm_cr(struct kvm_vcpu *vcpu, u64 data)
3692{
3693 struct vcpu_svm *svm = to_svm(vcpu);
3694 int svm_dis, chg_mask;
3695
3696 if (data & ~SVM_VM_CR_VALID_MASK)
3697 return 1;
3698
3699 chg_mask = SVM_VM_CR_VALID_MASK;
3700
3701 if (svm->nested.vm_cr_msr & SVM_VM_CR_SVM_DIS_MASK)
3702 chg_mask &= ~(SVM_VM_CR_SVM_LOCK_MASK | SVM_VM_CR_SVM_DIS_MASK);
3703
3704 svm->nested.vm_cr_msr &= ~chg_mask;
3705 svm->nested.vm_cr_msr |= (data & chg_mask);
3706
3707 svm_dis = svm->nested.vm_cr_msr & SVM_VM_CR_SVM_DIS_MASK;
3708
3709 /* check for svm_disable while efer.svme is set */
3710 if (svm_dis && (vcpu->arch.efer & EFER_SVME))
3711 return 1;
3712
3713 return 0;
3714}
3715
8fe8ab46 3716static int svm_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr)
6aa8b732 3717{
a2fa3e9f
GH
3718 struct vcpu_svm *svm = to_svm(vcpu);
3719
8fe8ab46
WA
3720 u32 ecx = msr->index;
3721 u64 data = msr->data;
6aa8b732 3722 switch (ecx) {
15038e14
PB
3723 case MSR_IA32_CR_PAT:
3724 if (!kvm_mtrr_valid(vcpu, MSR_IA32_CR_PAT, data))
3725 return 1;
3726 vcpu->arch.pat = data;
3727 svm->vmcb->save.g_pat = data;
3728 mark_dirty(svm->vmcb, VMCB_NPT);
3729 break;
f4e1b3c8 3730 case MSR_IA32_TSC:
8fe8ab46 3731 kvm_write_tsc(vcpu, msr);
6aa8b732 3732 break;
a57b802c
KA
3733 case MSR_IA32_SPEC_CTRL:
3734 if (!msr->host_initiated &&
3735 !guest_cpuid_has(vcpu, X86_FEATURE_IBRS))
3736 return 1;
3737
3738 /* The STIBP bit doesn't fault even if it's not advertised */
3739 if (data & ~(SPEC_CTRL_IBRS | SPEC_CTRL_STIBP))
3740 return 1;
3741
3742 svm->spec_ctrl = data;
3743
3744 if (!data)
3745 break;
3746
3747 /*
3748 * For non-nested:
3749 * When it's written (to non-zero) for the first time, pass
3750 * it through.
3751 *
3752 * For nested:
3753 * The handling of the MSR bitmap for L2 guests is done in
3754 * nested_svm_vmrun_msrpm.
3755 * We update the L1 MSR bit as well since it will end up
3756 * touching the MSR anyway now.
3757 */
3758 set_msr_interception(svm->msrpm, MSR_IA32_SPEC_CTRL, 1, 1);
3759 break;
33241bfe
AR
3760 case MSR_IA32_PRED_CMD:
3761 if (!msr->host_initiated &&
3762 !guest_cpuid_has(vcpu, X86_FEATURE_IBPB))
3763 return 1;
3764
3765 if (data & ~PRED_CMD_IBPB)
3766 return 1;
3767
3768 if (!data)
3769 break;
3770
3771 wrmsrl(MSR_IA32_PRED_CMD, PRED_CMD_IBPB);
3772 if (is_guest_mode(vcpu))
3773 break;
3774 set_msr_interception(svm->msrpm, MSR_IA32_PRED_CMD, 0, 1);
3775 break;
8c06585d 3776 case MSR_STAR:
a2fa3e9f 3777 svm->vmcb->save.star = data;
6aa8b732 3778 break;
49b14f24 3779#ifdef CONFIG_X86_64
6aa8b732 3780 case MSR_LSTAR:
a2fa3e9f 3781 svm->vmcb->save.lstar = data;
6aa8b732
AK
3782 break;
3783 case MSR_CSTAR:
a2fa3e9f 3784 svm->vmcb->save.cstar = data;
6aa8b732
AK
3785 break;
3786 case MSR_KERNEL_GS_BASE:
a2fa3e9f 3787 svm->vmcb->save.kernel_gs_base = data;
6aa8b732
AK
3788 break;
3789 case MSR_SYSCALL_MASK:
a2fa3e9f 3790 svm->vmcb->save.sfmask = data;
6aa8b732
AK
3791 break;
3792#endif
3793 case MSR_IA32_SYSENTER_CS:
a2fa3e9f 3794 svm->vmcb->save.sysenter_cs = data;
6aa8b732
AK
3795 break;
3796 case MSR_IA32_SYSENTER_EIP:
017cb99e 3797 svm->sysenter_eip = data;
a2fa3e9f 3798 svm->vmcb->save.sysenter_eip = data;
6aa8b732
AK
3799 break;
3800 case MSR_IA32_SYSENTER_ESP:
017cb99e 3801 svm->sysenter_esp = data;
a2fa3e9f 3802 svm->vmcb->save.sysenter_esp = data;
6aa8b732 3803 break;
46896c73
PB
3804 case MSR_TSC_AUX:
3805 if (!boot_cpu_has(X86_FEATURE_RDTSCP))
3806 return 1;
3807
3808 /*
3809 * This is rare, so we update the MSR here instead of using
3810 * direct_access_msrs. Doing that would require a rdmsr in
3811 * svm_vcpu_put.
3812 */
3813 svm->tsc_aux = data;
3814 wrmsrl(MSR_TSC_AUX, svm->tsc_aux);
3815 break;
a2938c80 3816 case MSR_IA32_DEBUGCTLMSR:
2a6b20b8 3817 if (!boot_cpu_has(X86_FEATURE_LBRV)) {
a737f256
CD
3818 vcpu_unimpl(vcpu, "%s: MSR_IA32_DEBUGCTL 0x%llx, nop\n",
3819 __func__, data);
24e09cbf
JR
3820 break;
3821 }
3822 if (data & DEBUGCTL_RESERVED_BITS)
3823 return 1;
3824
3825 svm->vmcb->save.dbgctl = data;
b53ba3f9 3826 mark_dirty(svm->vmcb, VMCB_LBR);
24e09cbf
JR
3827 if (data & (1ULL<<0))
3828 svm_enable_lbrv(svm);
3829 else
3830 svm_disable_lbrv(svm);
a2938c80 3831 break;
b286d5d8 3832 case MSR_VM_HSAVE_PA:
e6aa9abd 3833 svm->nested.hsave_msr = data;
62b9abaa 3834 break;
3c5d0a44 3835 case MSR_VM_CR:
4a810181 3836 return svm_set_vm_cr(vcpu, data);
3c5d0a44 3837 case MSR_VM_IGNNE:
a737f256 3838 vcpu_unimpl(vcpu, "unimplemented wrmsr: 0x%x data 0x%llx\n", ecx, data);
3c5d0a44 3839 break;
44a95dae
SS
3840 case MSR_IA32_APICBASE:
3841 if (kvm_vcpu_apicv_active(vcpu))
3842 avic_update_vapic_bar(to_svm(vcpu), data);
3843 /* Follow through */
6aa8b732 3844 default:
8fe8ab46 3845 return kvm_set_msr_common(vcpu, msr);
6aa8b732
AK
3846 }
3847 return 0;
3848}
3849
851ba692 3850static int wrmsr_interception(struct vcpu_svm *svm)
6aa8b732 3851{
8fe8ab46 3852 struct msr_data msr;
668f198f
DK
3853 u32 ecx = kvm_register_read(&svm->vcpu, VCPU_REGS_RCX);
3854 u64 data = kvm_read_edx_eax(&svm->vcpu);
af9ca2d7 3855
8fe8ab46
WA
3856 msr.data = data;
3857 msr.index = ecx;
3858 msr.host_initiated = false;
af9ca2d7 3859
5fdbf976 3860 svm->next_rip = kvm_rip_read(&svm->vcpu) + 2;
854e8bb1 3861 if (kvm_set_msr(&svm->vcpu, &msr)) {
59200273 3862 trace_kvm_msr_write_ex(ecx, data);
c1a5d4f9 3863 kvm_inject_gp(&svm->vcpu, 0);
b742c1e6 3864 return 1;
59200273
AK
3865 } else {
3866 trace_kvm_msr_write(ecx, data);
b742c1e6 3867 return kvm_skip_emulated_instruction(&svm->vcpu);
59200273 3868 }
6aa8b732
AK
3869}
3870
851ba692 3871static int msr_interception(struct vcpu_svm *svm)
6aa8b732 3872{
e756fc62 3873 if (svm->vmcb->control.exit_info_1)
851ba692 3874 return wrmsr_interception(svm);
6aa8b732 3875 else
851ba692 3876 return rdmsr_interception(svm);
6aa8b732
AK
3877}
3878
851ba692 3879static int interrupt_window_interception(struct vcpu_svm *svm)
c1150d8c 3880{
3842d135 3881 kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
f0b85051 3882 svm_clear_vintr(svm);
85f455f7 3883 svm->vmcb->control.int_ctl &= ~V_IRQ_MASK;
decdbf6a 3884 mark_dirty(svm->vmcb, VMCB_INTR);
675acb75 3885 ++svm->vcpu.stat.irq_window_exits;
c1150d8c
DL
3886 return 1;
3887}
3888
565d0998
ML
3889static int pause_interception(struct vcpu_svm *svm)
3890{
de63ad4c
LM
3891 struct kvm_vcpu *vcpu = &svm->vcpu;
3892 bool in_kernel = (svm_get_cpl(vcpu) == 0);
3893
3894 kvm_vcpu_on_spin(vcpu, in_kernel);
565d0998
ML
3895 return 1;
3896}
3897
87c00572
GS
3898static int nop_interception(struct vcpu_svm *svm)
3899{
b742c1e6 3900 return kvm_skip_emulated_instruction(&(svm->vcpu));
87c00572
GS
3901}
3902
3903static int monitor_interception(struct vcpu_svm *svm)
3904{
3905 printk_once(KERN_WARNING "kvm: MONITOR instruction emulated as NOP!\n");
3906 return nop_interception(svm);
3907}
3908
3909static int mwait_interception(struct vcpu_svm *svm)
3910{
3911 printk_once(KERN_WARNING "kvm: MWAIT instruction emulated as NOP!\n");
3912 return nop_interception(svm);
3913}
3914
18f40c53
SS
3915enum avic_ipi_failure_cause {
3916 AVIC_IPI_FAILURE_INVALID_INT_TYPE,
3917 AVIC_IPI_FAILURE_TARGET_NOT_RUNNING,
3918 AVIC_IPI_FAILURE_INVALID_TARGET,
3919 AVIC_IPI_FAILURE_INVALID_BACKING_PAGE,
3920};
3921
3922static int avic_incomplete_ipi_interception(struct vcpu_svm *svm)
3923{
3924 u32 icrh = svm->vmcb->control.exit_info_1 >> 32;
3925 u32 icrl = svm->vmcb->control.exit_info_1;
3926 u32 id = svm->vmcb->control.exit_info_2 >> 32;
5446a979 3927 u32 index = svm->vmcb->control.exit_info_2 & 0xFF;
18f40c53
SS
3928 struct kvm_lapic *apic = svm->vcpu.arch.apic;
3929
3930 trace_kvm_avic_incomplete_ipi(svm->vcpu.vcpu_id, icrh, icrl, id, index);
3931
3932 switch (id) {
3933 case AVIC_IPI_FAILURE_INVALID_INT_TYPE:
3934 /*
3935 * AVIC hardware handles the generation of
3936 * IPIs when the specified Message Type is Fixed
3937 * (also known as fixed delivery mode) and
3938 * the Trigger Mode is edge-triggered. The hardware
3939 * also supports self and broadcast delivery modes
3940 * specified via the Destination Shorthand(DSH)
3941 * field of the ICRL. Logical and physical APIC ID
3942 * formats are supported. All other IPI types cause
3943 * a #VMEXIT, which needs to emulated.
3944 */
3945 kvm_lapic_reg_write(apic, APIC_ICR2, icrh);
3946 kvm_lapic_reg_write(apic, APIC_ICR, icrl);
3947 break;
3948 case AVIC_IPI_FAILURE_TARGET_NOT_RUNNING: {
3949 int i;
3950 struct kvm_vcpu *vcpu;
3951 struct kvm *kvm = svm->vcpu.kvm;
3952 struct kvm_lapic *apic = svm->vcpu.arch.apic;
3953
3954 /*
3955 * At this point, we expect that the AVIC HW has already
3956 * set the appropriate IRR bits on the valid target
3957 * vcpus. So, we just need to kick the appropriate vcpu.
3958 */
3959 kvm_for_each_vcpu(i, vcpu, kvm) {
3960 bool m = kvm_apic_match_dest(vcpu, apic,
3961 icrl & KVM_APIC_SHORT_MASK,
3962 GET_APIC_DEST_FIELD(icrh),
3963 icrl & KVM_APIC_DEST_MASK);
3964
3965 if (m && !avic_vcpu_is_running(vcpu))
3966 kvm_vcpu_wake_up(vcpu);
3967 }
3968 break;
3969 }
3970 case AVIC_IPI_FAILURE_INVALID_TARGET:
3971 break;
3972 case AVIC_IPI_FAILURE_INVALID_BACKING_PAGE:
3973 WARN_ONCE(1, "Invalid backing page\n");
3974 break;
3975 default:
3976 pr_err("Unknown IPI interception\n");
3977 }
3978
3979 return 1;
3980}
3981
3982static u32 *avic_get_logical_id_entry(struct kvm_vcpu *vcpu, u32 ldr, bool flat)
3983{
3984 struct kvm_arch *vm_data = &vcpu->kvm->arch;
3985 int index;
3986 u32 *logical_apic_id_table;
3987 int dlid = GET_APIC_LOGICAL_ID(ldr);
3988
3989 if (!dlid)
3990 return NULL;
3991
3992 if (flat) { /* flat */
3993 index = ffs(dlid) - 1;
3994 if (index > 7)
3995 return NULL;
3996 } else { /* cluster */
3997 int cluster = (dlid & 0xf0) >> 4;
3998 int apic = ffs(dlid & 0x0f) - 1;
3999
4000 if ((apic < 0) || (apic > 7) ||
4001 (cluster >= 0xf))
4002 return NULL;
4003 index = (cluster << 2) + apic;
4004 }
4005
4006 logical_apic_id_table = (u32 *) page_address(vm_data->avic_logical_id_table_page);
4007
4008 return &logical_apic_id_table[index];
4009}
4010
4011static int avic_ldr_write(struct kvm_vcpu *vcpu, u8 g_physical_id, u32 ldr,
4012 bool valid)
4013{
4014 bool flat;
4015 u32 *entry, new_entry;
4016
4017 flat = kvm_lapic_get_reg(vcpu->arch.apic, APIC_DFR) == APIC_DFR_FLAT;
4018 entry = avic_get_logical_id_entry(vcpu, ldr, flat);
4019 if (!entry)
4020 return -EINVAL;
4021
4022 new_entry = READ_ONCE(*entry);
4023 new_entry &= ~AVIC_LOGICAL_ID_ENTRY_GUEST_PHYSICAL_ID_MASK;
4024 new_entry |= (g_physical_id & AVIC_LOGICAL_ID_ENTRY_GUEST_PHYSICAL_ID_MASK);
4025 if (valid)
4026 new_entry |= AVIC_LOGICAL_ID_ENTRY_VALID_MASK;
4027 else
4028 new_entry &= ~AVIC_LOGICAL_ID_ENTRY_VALID_MASK;
4029 WRITE_ONCE(*entry, new_entry);
4030
4031 return 0;
4032}
4033
4034static int avic_handle_ldr_update(struct kvm_vcpu *vcpu)
4035{
4036 int ret;
4037 struct vcpu_svm *svm = to_svm(vcpu);
4038 u32 ldr = kvm_lapic_get_reg(vcpu->arch.apic, APIC_LDR);
4039
4040 if (!ldr)
4041 return 1;
4042
4043 ret = avic_ldr_write(vcpu, vcpu->vcpu_id, ldr, true);
4044 if (ret && svm->ldr_reg) {
4045 avic_ldr_write(vcpu, 0, svm->ldr_reg, false);
4046 svm->ldr_reg = 0;
4047 } else {
4048 svm->ldr_reg = ldr;
4049 }
4050 return ret;
4051}
4052
4053static int avic_handle_apic_id_update(struct kvm_vcpu *vcpu)
4054{
4055 u64 *old, *new;
4056 struct vcpu_svm *svm = to_svm(vcpu);
4057 u32 apic_id_reg = kvm_lapic_get_reg(vcpu->arch.apic, APIC_ID);
4058 u32 id = (apic_id_reg >> 24) & 0xff;
4059
4060 if (vcpu->vcpu_id == id)
4061 return 0;
4062
4063 old = avic_get_physical_id_entry(vcpu, vcpu->vcpu_id);
4064 new = avic_get_physical_id_entry(vcpu, id);
4065 if (!new || !old)
4066 return 1;
4067
4068 /* We need to move physical_id_entry to new offset */
4069 *new = *old;
4070 *old = 0ULL;
4071 to_svm(vcpu)->avic_physical_id_cache = new;
4072
4073 /*
4074 * Also update the guest physical APIC ID in the logical
4075 * APIC ID table entry if already setup the LDR.
4076 */
4077 if (svm->ldr_reg)
4078 avic_handle_ldr_update(vcpu);
4079
4080 return 0;
4081}
4082
4083static int avic_handle_dfr_update(struct kvm_vcpu *vcpu)
4084{
4085 struct vcpu_svm *svm = to_svm(vcpu);
4086 struct kvm_arch *vm_data = &vcpu->kvm->arch;
4087 u32 dfr = kvm_lapic_get_reg(vcpu->arch.apic, APIC_DFR);
4088 u32 mod = (dfr >> 28) & 0xf;
4089
4090 /*
4091 * We assume that all local APICs are using the same type.
4092 * If this changes, we need to flush the AVIC logical
4093 * APID id table.
4094 */
4095 if (vm_data->ldr_mode == mod)
4096 return 0;
4097
4098 clear_page(page_address(vm_data->avic_logical_id_table_page));
4099 vm_data->ldr_mode = mod;
4100
4101 if (svm->ldr_reg)
4102 avic_handle_ldr_update(vcpu);
4103 return 0;
4104}
4105
4106static int avic_unaccel_trap_write(struct vcpu_svm *svm)
4107{
4108 struct kvm_lapic *apic = svm->vcpu.arch.apic;
4109 u32 offset = svm->vmcb->control.exit_info_1 &
4110 AVIC_UNACCEL_ACCESS_OFFSET_MASK;
4111
4112 switch (offset) {
4113 case APIC_ID:
4114 if (avic_handle_apic_id_update(&svm->vcpu))
4115 return 0;
4116 break;
4117 case APIC_LDR:
4118 if (avic_handle_ldr_update(&svm->vcpu))
4119 return 0;
4120 break;
4121 case APIC_DFR:
4122 avic_handle_dfr_update(&svm->vcpu);
4123 break;
4124 default:
4125 break;
4126 }
4127
4128 kvm_lapic_reg_write(apic, offset, kvm_lapic_get_reg(apic, offset));
4129
4130 return 1;
4131}
4132
4133static bool is_avic_unaccelerated_access_trap(u32 offset)
4134{
4135 bool ret = false;
4136
4137 switch (offset) {
4138 case APIC_ID:
4139 case APIC_EOI:
4140 case APIC_RRR:
4141 case APIC_LDR:
4142 case APIC_DFR:
4143 case APIC_SPIV:
4144 case APIC_ESR:
4145 case APIC_ICR:
4146 case APIC_LVTT:
4147 case APIC_LVTTHMR:
4148 case APIC_LVTPC:
4149 case APIC_LVT0:
4150 case APIC_LVT1:
4151 case APIC_LVTERR:
4152 case APIC_TMICT:
4153 case APIC_TDCR:
4154 ret = true;
4155 break;
4156 default:
4157 break;
4158 }
4159 return ret;
4160}
4161
4162static int avic_unaccelerated_access_interception(struct vcpu_svm *svm)
4163{
4164 int ret = 0;
4165 u32 offset = svm->vmcb->control.exit_info_1 &
4166 AVIC_UNACCEL_ACCESS_OFFSET_MASK;
4167 u32 vector = svm->vmcb->control.exit_info_2 &
4168 AVIC_UNACCEL_ACCESS_VECTOR_MASK;
4169 bool write = (svm->vmcb->control.exit_info_1 >> 32) &
4170 AVIC_UNACCEL_ACCESS_WRITE_MASK;
4171 bool trap = is_avic_unaccelerated_access_trap(offset);
4172
4173 trace_kvm_avic_unaccelerated_access(svm->vcpu.vcpu_id, offset,
4174 trap, write, vector);
4175 if (trap) {
4176 /* Handling Trap */
4177 WARN_ONCE(!write, "svm: Handling trap read.\n");
4178 ret = avic_unaccel_trap_write(svm);
4179 } else {
4180 /* Handling Fault */
4181 ret = (emulate_instruction(&svm->vcpu, 0) == EMULATE_DONE);
4182 }
4183
4184 return ret;
4185}
4186
09941fbb 4187static int (*const svm_exit_handlers[])(struct vcpu_svm *svm) = {
7ff76d58
AP
4188 [SVM_EXIT_READ_CR0] = cr_interception,
4189 [SVM_EXIT_READ_CR3] = cr_interception,
4190 [SVM_EXIT_READ_CR4] = cr_interception,
4191 [SVM_EXIT_READ_CR8] = cr_interception,
5e57518d 4192 [SVM_EXIT_CR0_SEL_WRITE] = cr_interception,
628afd2a 4193 [SVM_EXIT_WRITE_CR0] = cr_interception,
7ff76d58
AP
4194 [SVM_EXIT_WRITE_CR3] = cr_interception,
4195 [SVM_EXIT_WRITE_CR4] = cr_interception,
e0231715 4196 [SVM_EXIT_WRITE_CR8] = cr8_write_interception,
cae3797a
AP
4197 [SVM_EXIT_READ_DR0] = dr_interception,
4198 [SVM_EXIT_READ_DR1] = dr_interception,
4199 [SVM_EXIT_READ_DR2] = dr_interception,
4200 [SVM_EXIT_READ_DR3] = dr_interception,
4201 [SVM_EXIT_READ_DR4] = dr_interception,
4202 [SVM_EXIT_READ_DR5] = dr_interception,
4203 [SVM_EXIT_READ_DR6] = dr_interception,
4204 [SVM_EXIT_READ_DR7] = dr_interception,
4205 [SVM_EXIT_WRITE_DR0] = dr_interception,
4206 [SVM_EXIT_WRITE_DR1] = dr_interception,
4207 [SVM_EXIT_WRITE_DR2] = dr_interception,
4208 [SVM_EXIT_WRITE_DR3] = dr_interception,
4209 [SVM_EXIT_WRITE_DR4] = dr_interception,
4210 [SVM_EXIT_WRITE_DR5] = dr_interception,
4211 [SVM_EXIT_WRITE_DR6] = dr_interception,
4212 [SVM_EXIT_WRITE_DR7] = dr_interception,
d0bfb940
JK
4213 [SVM_EXIT_EXCP_BASE + DB_VECTOR] = db_interception,
4214 [SVM_EXIT_EXCP_BASE + BP_VECTOR] = bp_interception,
7aa81cc0 4215 [SVM_EXIT_EXCP_BASE + UD_VECTOR] = ud_interception,
e0231715 4216 [SVM_EXIT_EXCP_BASE + PF_VECTOR] = pf_interception,
e0231715 4217 [SVM_EXIT_EXCP_BASE + MC_VECTOR] = mc_interception,
54a20552 4218 [SVM_EXIT_EXCP_BASE + AC_VECTOR] = ac_interception,
e0231715 4219 [SVM_EXIT_INTR] = intr_interception,
c47f098d 4220 [SVM_EXIT_NMI] = nmi_interception,
6aa8b732
AK
4221 [SVM_EXIT_SMI] = nop_on_interception,
4222 [SVM_EXIT_INIT] = nop_on_interception,
c1150d8c 4223 [SVM_EXIT_VINTR] = interrupt_window_interception,
332b56e4 4224 [SVM_EXIT_RDPMC] = rdpmc_interception,
6aa8b732 4225 [SVM_EXIT_CPUID] = cpuid_interception,
95ba8273 4226 [SVM_EXIT_IRET] = iret_interception,
cf5a94d1 4227 [SVM_EXIT_INVD] = emulate_on_interception,
565d0998 4228 [SVM_EXIT_PAUSE] = pause_interception,
6aa8b732 4229 [SVM_EXIT_HLT] = halt_interception,
a7052897 4230 [SVM_EXIT_INVLPG] = invlpg_interception,
ff092385 4231 [SVM_EXIT_INVLPGA] = invlpga_interception,
e0231715 4232 [SVM_EXIT_IOIO] = io_interception,
6aa8b732
AK
4233 [SVM_EXIT_MSR] = msr_interception,
4234 [SVM_EXIT_TASK_SWITCH] = task_switch_interception,
46fe4ddd 4235 [SVM_EXIT_SHUTDOWN] = shutdown_interception,
3d6368ef 4236 [SVM_EXIT_VMRUN] = vmrun_interception,
02e235bc 4237 [SVM_EXIT_VMMCALL] = vmmcall_interception,
5542675b
AG
4238 [SVM_EXIT_VMLOAD] = vmload_interception,
4239 [SVM_EXIT_VMSAVE] = vmsave_interception,
1371d904
AG
4240 [SVM_EXIT_STGI] = stgi_interception,
4241 [SVM_EXIT_CLGI] = clgi_interception,
532a46b9 4242 [SVM_EXIT_SKINIT] = skinit_interception,
dab429a7 4243 [SVM_EXIT_WBINVD] = wbinvd_interception,
87c00572
GS
4244 [SVM_EXIT_MONITOR] = monitor_interception,
4245 [SVM_EXIT_MWAIT] = mwait_interception,
81dd35d4 4246 [SVM_EXIT_XSETBV] = xsetbv_interception,
d0006530 4247 [SVM_EXIT_NPF] = npf_interception,
64d60670 4248 [SVM_EXIT_RSM] = emulate_on_interception,
18f40c53
SS
4249 [SVM_EXIT_AVIC_INCOMPLETE_IPI] = avic_incomplete_ipi_interception,
4250 [SVM_EXIT_AVIC_UNACCELERATED_ACCESS] = avic_unaccelerated_access_interception,
6aa8b732
AK
4251};
4252
ae8cc059 4253static void dump_vmcb(struct kvm_vcpu *vcpu)
3f10c846
JR
4254{
4255 struct vcpu_svm *svm = to_svm(vcpu);
4256 struct vmcb_control_area *control = &svm->vmcb->control;
4257 struct vmcb_save_area *save = &svm->vmcb->save;
4258
4259 pr_err("VMCB Control Area:\n");
ae8cc059
JP
4260 pr_err("%-20s%04x\n", "cr_read:", control->intercept_cr & 0xffff);
4261 pr_err("%-20s%04x\n", "cr_write:", control->intercept_cr >> 16);
4262 pr_err("%-20s%04x\n", "dr_read:", control->intercept_dr & 0xffff);
4263 pr_err("%-20s%04x\n", "dr_write:", control->intercept_dr >> 16);
4264 pr_err("%-20s%08x\n", "exceptions:", control->intercept_exceptions);
4265 pr_err("%-20s%016llx\n", "intercepts:", control->intercept);
4266 pr_err("%-20s%d\n", "pause filter count:", control->pause_filter_count);
4267 pr_err("%-20s%016llx\n", "iopm_base_pa:", control->iopm_base_pa);
4268 pr_err("%-20s%016llx\n", "msrpm_base_pa:", control->msrpm_base_pa);
4269 pr_err("%-20s%016llx\n", "tsc_offset:", control->tsc_offset);
4270 pr_err("%-20s%d\n", "asid:", control->asid);
4271 pr_err("%-20s%d\n", "tlb_ctl:", control->tlb_ctl);
4272 pr_err("%-20s%08x\n", "int_ctl:", control->int_ctl);
4273 pr_err("%-20s%08x\n", "int_vector:", control->int_vector);
4274 pr_err("%-20s%08x\n", "int_state:", control->int_state);
4275 pr_err("%-20s%08x\n", "exit_code:", control->exit_code);
4276 pr_err("%-20s%016llx\n", "exit_info1:", control->exit_info_1);
4277 pr_err("%-20s%016llx\n", "exit_info2:", control->exit_info_2);
4278 pr_err("%-20s%08x\n", "exit_int_info:", control->exit_int_info);
4279 pr_err("%-20s%08x\n", "exit_int_info_err:", control->exit_int_info_err);
4280 pr_err("%-20s%lld\n", "nested_ctl:", control->nested_ctl);
4281 pr_err("%-20s%016llx\n", "nested_cr3:", control->nested_cr3);
44a95dae 4282 pr_err("%-20s%016llx\n", "avic_vapic_bar:", control->avic_vapic_bar);
ae8cc059
JP
4283 pr_err("%-20s%08x\n", "event_inj:", control->event_inj);
4284 pr_err("%-20s%08x\n", "event_inj_err:", control->event_inj_err);
0dc92119 4285 pr_err("%-20s%lld\n", "virt_ext:", control->virt_ext);
ae8cc059 4286 pr_err("%-20s%016llx\n", "next_rip:", control->next_rip);
44a95dae
SS
4287 pr_err("%-20s%016llx\n", "avic_backing_page:", control->avic_backing_page);
4288 pr_err("%-20s%016llx\n", "avic_logical_id:", control->avic_logical_id);
4289 pr_err("%-20s%016llx\n", "avic_physical_id:", control->avic_physical_id);
3f10c846 4290 pr_err("VMCB State Save Area:\n");
ae8cc059
JP
4291 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
4292 "es:",
4293 save->es.selector, save->es.attrib,
4294 save->es.limit, save->es.base);
4295 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
4296 "cs:",
4297 save->cs.selector, save->cs.attrib,
4298 save->cs.limit, save->cs.base);
4299 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
4300 "ss:",
4301 save->ss.selector, save->ss.attrib,
4302 save->ss.limit, save->ss.base);
4303 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
4304 "ds:",
4305 save->ds.selector, save->ds.attrib,
4306 save->ds.limit, save->ds.base);
4307 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
4308 "fs:",
4309 save->fs.selector, save->fs.attrib,
4310 save->fs.limit, save->fs.base);
4311 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
4312 "gs:",
4313 save->gs.selector, save->gs.attrib,
4314 save->gs.limit, save->gs.base);
4315 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
4316 "gdtr:",
4317 save->gdtr.selector, save->gdtr.attrib,
4318 save->gdtr.limit, save->gdtr.base);
4319 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
4320 "ldtr:",
4321 save->ldtr.selector, save->ldtr.attrib,
4322 save->ldtr.limit, save->ldtr.base);
4323 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
4324 "idtr:",
4325 save->idtr.selector, save->idtr.attrib,
4326 save->idtr.limit, save->idtr.base);
4327 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
4328 "tr:",
4329 save->tr.selector, save->tr.attrib,
4330 save->tr.limit, save->tr.base);
3f10c846
JR
4331 pr_err("cpl: %d efer: %016llx\n",
4332 save->cpl, save->efer);
ae8cc059
JP
4333 pr_err("%-15s %016llx %-13s %016llx\n",
4334 "cr0:", save->cr0, "cr2:", save->cr2);
4335 pr_err("%-15s %016llx %-13s %016llx\n",
4336 "cr3:", save->cr3, "cr4:", save->cr4);
4337 pr_err("%-15s %016llx %-13s %016llx\n",
4338 "dr6:", save->dr6, "dr7:", save->dr7);
4339 pr_err("%-15s %016llx %-13s %016llx\n",
4340 "rip:", save->rip, "rflags:", save->rflags);
4341 pr_err("%-15s %016llx %-13s %016llx\n",
4342 "rsp:", save->rsp, "rax:", save->rax);
4343 pr_err("%-15s %016llx %-13s %016llx\n",
4344 "star:", save->star, "lstar:", save->lstar);
4345 pr_err("%-15s %016llx %-13s %016llx\n",
4346 "cstar:", save->cstar, "sfmask:", save->sfmask);
4347 pr_err("%-15s %016llx %-13s %016llx\n",
4348 "kernel_gs_base:", save->kernel_gs_base,
4349 "sysenter_cs:", save->sysenter_cs);
4350 pr_err("%-15s %016llx %-13s %016llx\n",
4351 "sysenter_esp:", save->sysenter_esp,
4352 "sysenter_eip:", save->sysenter_eip);
4353 pr_err("%-15s %016llx %-13s %016llx\n",
4354 "gpat:", save->g_pat, "dbgctl:", save->dbgctl);
4355 pr_err("%-15s %016llx %-13s %016llx\n",
4356 "br_from:", save->br_from, "br_to:", save->br_to);
4357 pr_err("%-15s %016llx %-13s %016llx\n",
4358 "excp_from:", save->last_excp_from,
4359 "excp_to:", save->last_excp_to);
3f10c846
JR
4360}
4361
586f9607
AK
4362static void svm_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2)
4363{
4364 struct vmcb_control_area *control = &to_svm(vcpu)->vmcb->control;
4365
4366 *info1 = control->exit_info_1;
4367 *info2 = control->exit_info_2;
4368}
4369
851ba692 4370static int handle_exit(struct kvm_vcpu *vcpu)
6aa8b732 4371{
04d2cc77 4372 struct vcpu_svm *svm = to_svm(vcpu);
851ba692 4373 struct kvm_run *kvm_run = vcpu->run;
a2fa3e9f 4374 u32 exit_code = svm->vmcb->control.exit_code;
6aa8b732 4375
8b89fe1f
PB
4376 trace_kvm_exit(exit_code, vcpu, KVM_ISA_SVM);
4377
4ee546b4 4378 if (!is_cr_intercept(svm, INTERCEPT_CR0_WRITE))
2be4fc7a
JR
4379 vcpu->arch.cr0 = svm->vmcb->save.cr0;
4380 if (npt_enabled)
4381 vcpu->arch.cr3 = svm->vmcb->save.cr3;
af9ca2d7 4382
cd3ff653
JR
4383 if (unlikely(svm->nested.exit_required)) {
4384 nested_svm_vmexit(svm);
4385 svm->nested.exit_required = false;
4386
4387 return 1;
4388 }
4389
2030753d 4390 if (is_guest_mode(vcpu)) {
410e4d57
JR
4391 int vmexit;
4392
d8cabddf
JR
4393 trace_kvm_nested_vmexit(svm->vmcb->save.rip, exit_code,
4394 svm->vmcb->control.exit_info_1,
4395 svm->vmcb->control.exit_info_2,
4396 svm->vmcb->control.exit_int_info,
e097e5ff
SH
4397 svm->vmcb->control.exit_int_info_err,
4398 KVM_ISA_SVM);
d8cabddf 4399
410e4d57
JR
4400 vmexit = nested_svm_exit_special(svm);
4401
4402 if (vmexit == NESTED_EXIT_CONTINUE)
4403 vmexit = nested_svm_exit_handled(svm);
4404
4405 if (vmexit == NESTED_EXIT_DONE)
cf74a78b 4406 return 1;
cf74a78b
AG
4407 }
4408
a5c3832d
JR
4409 svm_complete_interrupts(svm);
4410
04d2cc77
AK
4411 if (svm->vmcb->control.exit_code == SVM_EXIT_ERR) {
4412 kvm_run->exit_reason = KVM_EXIT_FAIL_ENTRY;
4413 kvm_run->fail_entry.hardware_entry_failure_reason
4414 = svm->vmcb->control.exit_code;
3f10c846
JR
4415 pr_err("KVM: FAILED VMRUN WITH VMCB:\n");
4416 dump_vmcb(vcpu);
04d2cc77
AK
4417 return 0;
4418 }
4419
a2fa3e9f 4420 if (is_external_interrupt(svm->vmcb->control.exit_int_info) &&
709ddebf 4421 exit_code != SVM_EXIT_EXCP_BASE + PF_VECTOR &&
55c5e464
JR
4422 exit_code != SVM_EXIT_NPF && exit_code != SVM_EXIT_TASK_SWITCH &&
4423 exit_code != SVM_EXIT_INTR && exit_code != SVM_EXIT_NMI)
6614c7d0 4424 printk(KERN_ERR "%s: unexpected exit_int_info 0x%x "
6aa8b732 4425 "exit_code 0x%x\n",
b8688d51 4426 __func__, svm->vmcb->control.exit_int_info,
6aa8b732
AK
4427 exit_code);
4428
9d8f549d 4429 if (exit_code >= ARRAY_SIZE(svm_exit_handlers)
56919c5c 4430 || !svm_exit_handlers[exit_code]) {
faac2458 4431 WARN_ONCE(1, "svm: unexpected exit reason 0x%x\n", exit_code);
2bc19dc3
MT
4432 kvm_queue_exception(vcpu, UD_VECTOR);
4433 return 1;
6aa8b732
AK
4434 }
4435
851ba692 4436 return svm_exit_handlers[exit_code](svm);
6aa8b732
AK
4437}
4438
4439static void reload_tss(struct kvm_vcpu *vcpu)
4440{
4441 int cpu = raw_smp_processor_id();
4442
0fe1e009
TH
4443 struct svm_cpu_data *sd = per_cpu(svm_data, cpu);
4444 sd->tss_desc->type = 9; /* available 32/64-bit TSS */
6aa8b732
AK
4445 load_TR_desc();
4446}
4447
e756fc62 4448static void pre_svm_run(struct vcpu_svm *svm)
6aa8b732
AK
4449{
4450 int cpu = raw_smp_processor_id();
4451
0fe1e009 4452 struct svm_cpu_data *sd = per_cpu(svm_data, cpu);
6aa8b732 4453
4b656b12 4454 /* FIXME: handle wraparound of asid_generation */
0fe1e009
TH
4455 if (svm->asid_generation != sd->asid_generation)
4456 new_asid(svm, sd);
6aa8b732
AK
4457}
4458
95ba8273
GN
4459static void svm_inject_nmi(struct kvm_vcpu *vcpu)
4460{
4461 struct vcpu_svm *svm = to_svm(vcpu);
4462
4463 svm->vmcb->control.event_inj = SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_NMI;
4464 vcpu->arch.hflags |= HF_NMI_MASK;
8a05a1b8 4465 set_intercept(svm, INTERCEPT_IRET);
95ba8273
GN
4466 ++vcpu->stat.nmi_injections;
4467}
6aa8b732 4468
85f455f7 4469static inline void svm_inject_irq(struct vcpu_svm *svm, int irq)
6aa8b732
AK
4470{
4471 struct vmcb_control_area *control;
4472
340d3bc3 4473 /* The following fields are ignored when AVIC is enabled */
e756fc62 4474 control = &svm->vmcb->control;
85f455f7 4475 control->int_vector = irq;
6aa8b732
AK
4476 control->int_ctl &= ~V_INTR_PRIO_MASK;
4477 control->int_ctl |= V_IRQ_MASK |
4478 ((/*control->int_vector >> 4*/ 0xf) << V_INTR_PRIO_SHIFT);
decdbf6a 4479 mark_dirty(svm->vmcb, VMCB_INTR);
6aa8b732
AK
4480}
4481
66fd3f7f 4482static void svm_set_irq(struct kvm_vcpu *vcpu)
2a8067f1
ED
4483{
4484 struct vcpu_svm *svm = to_svm(vcpu);
4485
2af9194d 4486 BUG_ON(!(gif_set(svm)));
cf74a78b 4487
9fb2d2b4
GN
4488 trace_kvm_inj_virq(vcpu->arch.interrupt.nr);
4489 ++vcpu->stat.irq_injections;
4490
219b65dc
AG
4491 svm->vmcb->control.event_inj = vcpu->arch.interrupt.nr |
4492 SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_INTR;
2a8067f1
ED
4493}
4494
3bbf3565
SS
4495static inline bool svm_nested_virtualize_tpr(struct kvm_vcpu *vcpu)
4496{
4497 return is_guest_mode(vcpu) && (vcpu->arch.hflags & HF_VINTR_MASK);
4498}
4499
95ba8273 4500static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
aaacfc9a
JR
4501{
4502 struct vcpu_svm *svm = to_svm(vcpu);
aaacfc9a 4503
3bbf3565
SS
4504 if (svm_nested_virtualize_tpr(vcpu) ||
4505 kvm_vcpu_apicv_active(vcpu))
88ab24ad
JR
4506 return;
4507
596f3142
RK
4508 clr_cr_intercept(svm, INTERCEPT_CR8_WRITE);
4509
95ba8273 4510 if (irr == -1)
aaacfc9a
JR
4511 return;
4512
95ba8273 4513 if (tpr >= irr)
4ee546b4 4514 set_cr_intercept(svm, INTERCEPT_CR8_WRITE);
95ba8273 4515}
aaacfc9a 4516
8d14695f
YZ
4517static void svm_set_virtual_x2apic_mode(struct kvm_vcpu *vcpu, bool set)
4518{
4519 return;
4520}
4521
b2a05fef 4522static bool svm_get_enable_apicv(struct kvm_vcpu *vcpu)
d62caabb 4523{
67034bb9 4524 return avic && irqchip_split(vcpu->kvm);
44a95dae
SS
4525}
4526
4527static void svm_hwapic_irr_update(struct kvm_vcpu *vcpu, int max_irr)
4528{
d62caabb
AS
4529}
4530
67c9dddc 4531static void svm_hwapic_isr_update(struct kvm_vcpu *vcpu, int max_isr)
44a95dae 4532{
d62caabb
AS
4533}
4534
44a95dae 4535/* Note: Currently only used by Hyper-V. */
d62caabb 4536static void svm_refresh_apicv_exec_ctrl(struct kvm_vcpu *vcpu)
c7c9c56c 4537{
44a95dae
SS
4538 struct vcpu_svm *svm = to_svm(vcpu);
4539 struct vmcb *vmcb = svm->vmcb;
4540
67034bb9 4541 if (!kvm_vcpu_apicv_active(&svm->vcpu))
44a95dae
SS
4542 return;
4543
4544 vmcb->control.int_ctl &= ~AVIC_ENABLE_MASK;
4545 mark_dirty(vmcb, VMCB_INTR);
c7c9c56c
YZ
4546}
4547
6308630b 4548static void svm_load_eoi_exitmap(struct kvm_vcpu *vcpu, u64 *eoi_exit_bitmap)
c7c9c56c
YZ
4549{
4550 return;
4551}
4552
340d3bc3
SS
4553static void svm_deliver_avic_intr(struct kvm_vcpu *vcpu, int vec)
4554{
4555 kvm_lapic_set_irr(vec, vcpu->arch.apic);
4556 smp_mb__after_atomic();
4557
4558 if (avic_vcpu_is_running(vcpu))
4559 wrmsrl(SVM_AVIC_DOORBELL,
7d669f50 4560 kvm_cpu_get_apicid(vcpu->cpu));
340d3bc3
SS
4561 else
4562 kvm_vcpu_wake_up(vcpu);
4563}
4564
411b44ba
SS
4565static void svm_ir_list_del(struct vcpu_svm *svm, struct amd_iommu_pi_data *pi)
4566{
4567 unsigned long flags;
4568 struct amd_svm_iommu_ir *cur;
4569
4570 spin_lock_irqsave(&svm->ir_list_lock, flags);
4571 list_for_each_entry(cur, &svm->ir_list, node) {
4572 if (cur->data != pi->ir_data)
4573 continue;
4574 list_del(&cur->node);
4575 kfree(cur);
4576 break;
4577 }
4578 spin_unlock_irqrestore(&svm->ir_list_lock, flags);
4579}
4580
4581static int svm_ir_list_add(struct vcpu_svm *svm, struct amd_iommu_pi_data *pi)
4582{
4583 int ret = 0;
4584 unsigned long flags;
4585 struct amd_svm_iommu_ir *ir;
4586
4587 /**
4588 * In some cases, the existing irte is updaed and re-set,
4589 * so we need to check here if it's already been * added
4590 * to the ir_list.
4591 */
4592 if (pi->ir_data && (pi->prev_ga_tag != 0)) {
4593 struct kvm *kvm = svm->vcpu.kvm;
4594 u32 vcpu_id = AVIC_GATAG_TO_VCPUID(pi->prev_ga_tag);
4595 struct kvm_vcpu *prev_vcpu = kvm_get_vcpu_by_id(kvm, vcpu_id);
4596 struct vcpu_svm *prev_svm;
4597
4598 if (!prev_vcpu) {
4599 ret = -EINVAL;
4600 goto out;
4601 }
4602
4603 prev_svm = to_svm(prev_vcpu);
4604 svm_ir_list_del(prev_svm, pi);
4605 }
4606
4607 /**
4608 * Allocating new amd_iommu_pi_data, which will get
4609 * add to the per-vcpu ir_list.
4610 */
4611 ir = kzalloc(sizeof(struct amd_svm_iommu_ir), GFP_KERNEL);
4612 if (!ir) {
4613 ret = -ENOMEM;
4614 goto out;
4615 }
4616 ir->data = pi->ir_data;
4617
4618 spin_lock_irqsave(&svm->ir_list_lock, flags);
4619 list_add(&ir->node, &svm->ir_list);
4620 spin_unlock_irqrestore(&svm->ir_list_lock, flags);
4621out:
4622 return ret;
4623}
4624
4625/**
4626 * Note:
4627 * The HW cannot support posting multicast/broadcast
4628 * interrupts to a vCPU. So, we still use legacy interrupt
4629 * remapping for these kind of interrupts.
4630 *
4631 * For lowest-priority interrupts, we only support
4632 * those with single CPU as the destination, e.g. user
4633 * configures the interrupts via /proc/irq or uses
4634 * irqbalance to make the interrupts single-CPU.
4635 */
4636static int
4637get_pi_vcpu_info(struct kvm *kvm, struct kvm_kernel_irq_routing_entry *e,
4638 struct vcpu_data *vcpu_info, struct vcpu_svm **svm)
4639{
4640 struct kvm_lapic_irq irq;
4641 struct kvm_vcpu *vcpu = NULL;
4642
4643 kvm_set_msi_irq(kvm, e, &irq);
4644
4645 if (!kvm_intr_is_single_vcpu(kvm, &irq, &vcpu)) {
4646 pr_debug("SVM: %s: use legacy intr remap mode for irq %u\n",
4647 __func__, irq.vector);
4648 return -1;
4649 }
4650
4651 pr_debug("SVM: %s: use GA mode for irq %u\n", __func__,
4652 irq.vector);
4653 *svm = to_svm(vcpu);
d0ec49d4 4654 vcpu_info->pi_desc_addr = __sme_set(page_to_phys((*svm)->avic_backing_page));
411b44ba
SS
4655 vcpu_info->vector = irq.vector;
4656
4657 return 0;
4658}
4659
4660/*
4661 * svm_update_pi_irte - set IRTE for Posted-Interrupts
4662 *
4663 * @kvm: kvm
4664 * @host_irq: host irq of the interrupt
4665 * @guest_irq: gsi of the interrupt
4666 * @set: set or unset PI
4667 * returns 0 on success, < 0 on failure
4668 */
4669static int svm_update_pi_irte(struct kvm *kvm, unsigned int host_irq,
4670 uint32_t guest_irq, bool set)
4671{
4672 struct kvm_kernel_irq_routing_entry *e;
4673 struct kvm_irq_routing_table *irq_rt;
4674 int idx, ret = -EINVAL;
4675
4676 if (!kvm_arch_has_assigned_device(kvm) ||
4677 !irq_remapping_cap(IRQ_POSTING_CAP))
4678 return 0;
4679
4680 pr_debug("SVM: %s: host_irq=%#x, guest_irq=%#x, set=%#x\n",
4681 __func__, host_irq, guest_irq, set);
4682
4683 idx = srcu_read_lock(&kvm->irq_srcu);
4684 irq_rt = srcu_dereference(kvm->irq_routing, &kvm->irq_srcu);
4685 WARN_ON(guest_irq >= irq_rt->nr_rt_entries);
4686
4687 hlist_for_each_entry(e, &irq_rt->map[guest_irq], link) {
4688 struct vcpu_data vcpu_info;
4689 struct vcpu_svm *svm = NULL;
4690
4691 if (e->type != KVM_IRQ_ROUTING_MSI)
4692 continue;
4693
4694 /**
4695 * Here, we setup with legacy mode in the following cases:
4696 * 1. When cannot target interrupt to a specific vcpu.
4697 * 2. Unsetting posted interrupt.
4698 * 3. APIC virtialization is disabled for the vcpu.
4699 */
4700 if (!get_pi_vcpu_info(kvm, e, &vcpu_info, &svm) && set &&
4701 kvm_vcpu_apicv_active(&svm->vcpu)) {
4702 struct amd_iommu_pi_data pi;
4703
4704 /* Try to enable guest_mode in IRTE */
d0ec49d4
TL
4705 pi.base = __sme_set(page_to_phys(svm->avic_backing_page) &
4706 AVIC_HPA_MASK);
411b44ba
SS
4707 pi.ga_tag = AVIC_GATAG(kvm->arch.avic_vm_id,
4708 svm->vcpu.vcpu_id);
4709 pi.is_guest_mode = true;
4710 pi.vcpu_data = &vcpu_info;
4711 ret = irq_set_vcpu_affinity(host_irq, &pi);
4712
4713 /**
4714 * Here, we successfully setting up vcpu affinity in
4715 * IOMMU guest mode. Now, we need to store the posted
4716 * interrupt information in a per-vcpu ir_list so that
4717 * we can reference to them directly when we update vcpu
4718 * scheduling information in IOMMU irte.
4719 */
4720 if (!ret && pi.is_guest_mode)
4721 svm_ir_list_add(svm, &pi);
4722 } else {
4723 /* Use legacy mode in IRTE */
4724 struct amd_iommu_pi_data pi;
4725
4726 /**
4727 * Here, pi is used to:
4728 * - Tell IOMMU to use legacy mode for this interrupt.
4729 * - Retrieve ga_tag of prior interrupt remapping data.
4730 */
4731 pi.is_guest_mode = false;
4732 ret = irq_set_vcpu_affinity(host_irq, &pi);
4733
4734 /**
4735 * Check if the posted interrupt was previously
4736 * setup with the guest_mode by checking if the ga_tag
4737 * was cached. If so, we need to clean up the per-vcpu
4738 * ir_list.
4739 */
4740 if (!ret && pi.prev_ga_tag) {
4741 int id = AVIC_GATAG_TO_VCPUID(pi.prev_ga_tag);
4742 struct kvm_vcpu *vcpu;
4743
4744 vcpu = kvm_get_vcpu_by_id(kvm, id);
4745 if (vcpu)
4746 svm_ir_list_del(to_svm(vcpu), &pi);
4747 }
4748 }
4749
4750 if (!ret && svm) {
4751 trace_kvm_pi_irte_update(svm->vcpu.vcpu_id,
4752 host_irq, e->gsi,
4753 vcpu_info.vector,
4754 vcpu_info.pi_desc_addr, set);
4755 }
4756
4757 if (ret < 0) {
4758 pr_err("%s: failed to update PI IRTE\n", __func__);
4759 goto out;
4760 }
4761 }
4762
4763 ret = 0;
4764out:
4765 srcu_read_unlock(&kvm->irq_srcu, idx);
4766 return ret;
4767}
4768
95ba8273
GN
4769static int svm_nmi_allowed(struct kvm_vcpu *vcpu)
4770{
4771 struct vcpu_svm *svm = to_svm(vcpu);
4772 struct vmcb *vmcb = svm->vmcb;
924584cc
JR
4773 int ret;
4774 ret = !(vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK) &&
4775 !(svm->vcpu.arch.hflags & HF_NMI_MASK);
4776 ret = ret && gif_set(svm) && nested_svm_nmi(svm);
4777
4778 return ret;
aaacfc9a
JR
4779}
4780
3cfc3092
JK
4781static bool svm_get_nmi_mask(struct kvm_vcpu *vcpu)
4782{
4783 struct vcpu_svm *svm = to_svm(vcpu);
4784
4785 return !!(svm->vcpu.arch.hflags & HF_NMI_MASK);
4786}
4787
4788static void svm_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
4789{
4790 struct vcpu_svm *svm = to_svm(vcpu);
4791
4792 if (masked) {
4793 svm->vcpu.arch.hflags |= HF_NMI_MASK;
8a05a1b8 4794 set_intercept(svm, INTERCEPT_IRET);
3cfc3092
JK
4795 } else {
4796 svm->vcpu.arch.hflags &= ~HF_NMI_MASK;
8a05a1b8 4797 clr_intercept(svm, INTERCEPT_IRET);
3cfc3092
JK
4798 }
4799}
4800
78646121
GN
4801static int svm_interrupt_allowed(struct kvm_vcpu *vcpu)
4802{
4803 struct vcpu_svm *svm = to_svm(vcpu);
4804 struct vmcb *vmcb = svm->vmcb;
7fcdb510
JR
4805 int ret;
4806
4807 if (!gif_set(svm) ||
4808 (vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK))
4809 return 0;
4810
f6e78475 4811 ret = !!(kvm_get_rflags(vcpu) & X86_EFLAGS_IF);
7fcdb510 4812
2030753d 4813 if (is_guest_mode(vcpu))
7fcdb510
JR
4814 return ret && !(svm->vcpu.arch.hflags & HF_VINTR_MASK);
4815
4816 return ret;
78646121
GN
4817}
4818
c9a7953f 4819static void enable_irq_window(struct kvm_vcpu *vcpu)
6aa8b732 4820{
219b65dc 4821 struct vcpu_svm *svm = to_svm(vcpu);
219b65dc 4822
340d3bc3
SS
4823 if (kvm_vcpu_apicv_active(vcpu))
4824 return;
4825
e0231715
JR
4826 /*
4827 * In case GIF=0 we can't rely on the CPU to tell us when GIF becomes
4828 * 1, because that's a separate STGI/VMRUN intercept. The next time we
4829 * get that intercept, this function will be called again though and
640bd6e5
JN
4830 * we'll get the vintr intercept. However, if the vGIF feature is
4831 * enabled, the STGI interception will not occur. Enable the irq
4832 * window under the assumption that the hardware will set the GIF.
e0231715 4833 */
640bd6e5 4834 if ((vgif_enabled(svm) || gif_set(svm)) && nested_svm_intr(svm)) {
219b65dc
AG
4835 svm_set_vintr(svm);
4836 svm_inject_irq(svm, 0x0);
4837 }
85f455f7
ED
4838}
4839
c9a7953f 4840static void enable_nmi_window(struct kvm_vcpu *vcpu)
c1150d8c 4841{
04d2cc77 4842 struct vcpu_svm *svm = to_svm(vcpu);
c1150d8c 4843
44c11430
GN
4844 if ((svm->vcpu.arch.hflags & (HF_NMI_MASK | HF_IRET_MASK))
4845 == HF_NMI_MASK)
c9a7953f 4846 return; /* IRET will cause a vm exit */
44c11430 4847
640bd6e5
JN
4848 if (!gif_set(svm)) {
4849 if (vgif_enabled(svm))
4850 set_intercept(svm, INTERCEPT_STGI);
1a5e1852 4851 return; /* STGI will cause a vm exit */
640bd6e5 4852 }
1a5e1852
LP
4853
4854 if (svm->nested.exit_required)
4855 return; /* we're not going to run the guest yet */
4856
e0231715
JR
4857 /*
4858 * Something prevents NMI from been injected. Single step over possible
4859 * problem (IRET or exception injection or interrupt shadow)
4860 */
ab2f4d73 4861 svm->nmi_singlestep_guest_rflags = svm_get_rflags(vcpu);
6be7d306 4862 svm->nmi_singlestep = true;
44c11430 4863 svm->vmcb->save.rflags |= (X86_EFLAGS_TF | X86_EFLAGS_RF);
c1150d8c
DL
4864}
4865
cbc94022
IE
4866static int svm_set_tss_addr(struct kvm *kvm, unsigned int addr)
4867{
4868 return 0;
4869}
4870
d9e368d6
AK
4871static void svm_flush_tlb(struct kvm_vcpu *vcpu)
4872{
38e5e92f
JR
4873 struct vcpu_svm *svm = to_svm(vcpu);
4874
4875 if (static_cpu_has(X86_FEATURE_FLUSHBYASID))
4876 svm->vmcb->control.tlb_ctl = TLB_CONTROL_FLUSH_ASID;
4877 else
4878 svm->asid_generation--;
d9e368d6
AK
4879}
4880
04d2cc77
AK
4881static void svm_prepare_guest_switch(struct kvm_vcpu *vcpu)
4882{
4883}
4884
d7bf8221
JR
4885static inline void sync_cr8_to_lapic(struct kvm_vcpu *vcpu)
4886{
4887 struct vcpu_svm *svm = to_svm(vcpu);
4888
3bbf3565 4889 if (svm_nested_virtualize_tpr(vcpu))
88ab24ad
JR
4890 return;
4891
4ee546b4 4892 if (!is_cr_intercept(svm, INTERCEPT_CR8_WRITE)) {
d7bf8221 4893 int cr8 = svm->vmcb->control.int_ctl & V_TPR_MASK;
615d5193 4894 kvm_set_cr8(vcpu, cr8);
d7bf8221
JR
4895 }
4896}
4897
649d6864
JR
4898static inline void sync_lapic_to_cr8(struct kvm_vcpu *vcpu)
4899{
4900 struct vcpu_svm *svm = to_svm(vcpu);
4901 u64 cr8;
4902
3bbf3565
SS
4903 if (svm_nested_virtualize_tpr(vcpu) ||
4904 kvm_vcpu_apicv_active(vcpu))
88ab24ad
JR
4905 return;
4906
649d6864
JR
4907 cr8 = kvm_get_cr8(vcpu);
4908 svm->vmcb->control.int_ctl &= ~V_TPR_MASK;
4909 svm->vmcb->control.int_ctl |= cr8 & V_TPR_MASK;
4910}
4911
9222be18
GN
4912static void svm_complete_interrupts(struct vcpu_svm *svm)
4913{
4914 u8 vector;
4915 int type;
4916 u32 exitintinfo = svm->vmcb->control.exit_int_info;
66b7138f
JK
4917 unsigned int3_injected = svm->int3_injected;
4918
4919 svm->int3_injected = 0;
9222be18 4920
bd3d1ec3
AK
4921 /*
4922 * If we've made progress since setting HF_IRET_MASK, we've
4923 * executed an IRET and can allow NMI injection.
4924 */
4925 if ((svm->vcpu.arch.hflags & HF_IRET_MASK)
4926 && kvm_rip_read(&svm->vcpu) != svm->nmi_iret_rip) {
44c11430 4927 svm->vcpu.arch.hflags &= ~(HF_NMI_MASK | HF_IRET_MASK);
3842d135
AK
4928 kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
4929 }
44c11430 4930
9222be18
GN
4931 svm->vcpu.arch.nmi_injected = false;
4932 kvm_clear_exception_queue(&svm->vcpu);
4933 kvm_clear_interrupt_queue(&svm->vcpu);
4934
4935 if (!(exitintinfo & SVM_EXITINTINFO_VALID))
4936 return;
4937
3842d135
AK
4938 kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
4939
9222be18
GN
4940 vector = exitintinfo & SVM_EXITINTINFO_VEC_MASK;
4941 type = exitintinfo & SVM_EXITINTINFO_TYPE_MASK;
4942
4943 switch (type) {
4944 case SVM_EXITINTINFO_TYPE_NMI:
4945 svm->vcpu.arch.nmi_injected = true;
4946 break;
4947 case SVM_EXITINTINFO_TYPE_EXEPT:
66b7138f
JK
4948 /*
4949 * In case of software exceptions, do not reinject the vector,
4950 * but re-execute the instruction instead. Rewind RIP first
4951 * if we emulated INT3 before.
4952 */
4953 if (kvm_exception_is_soft(vector)) {
4954 if (vector == BP_VECTOR && int3_injected &&
4955 kvm_is_linear_rip(&svm->vcpu, svm->int3_rip))
4956 kvm_rip_write(&svm->vcpu,
4957 kvm_rip_read(&svm->vcpu) -
4958 int3_injected);
9222be18 4959 break;
66b7138f 4960 }
9222be18
GN
4961 if (exitintinfo & SVM_EXITINTINFO_VALID_ERR) {
4962 u32 err = svm->vmcb->control.exit_int_info_err;
ce7ddec4 4963 kvm_requeue_exception_e(&svm->vcpu, vector, err);
9222be18
GN
4964
4965 } else
ce7ddec4 4966 kvm_requeue_exception(&svm->vcpu, vector);
9222be18
GN
4967 break;
4968 case SVM_EXITINTINFO_TYPE_INTR:
66fd3f7f 4969 kvm_queue_interrupt(&svm->vcpu, vector, false);
9222be18
GN
4970 break;
4971 default:
4972 break;
4973 }
4974}
4975
b463a6f7
AK
4976static void svm_cancel_injection(struct kvm_vcpu *vcpu)
4977{
4978 struct vcpu_svm *svm = to_svm(vcpu);
4979 struct vmcb_control_area *control = &svm->vmcb->control;
4980
4981 control->exit_int_info = control->event_inj;
4982 control->exit_int_info_err = control->event_inj_err;
4983 control->event_inj = 0;
4984 svm_complete_interrupts(svm);
4985}
4986
851ba692 4987static void svm_vcpu_run(struct kvm_vcpu *vcpu)
6aa8b732 4988{
a2fa3e9f 4989 struct vcpu_svm *svm = to_svm(vcpu);
d9e368d6 4990
2041a06a
JR
4991 svm->vmcb->save.rax = vcpu->arch.regs[VCPU_REGS_RAX];
4992 svm->vmcb->save.rsp = vcpu->arch.regs[VCPU_REGS_RSP];
4993 svm->vmcb->save.rip = vcpu->arch.regs[VCPU_REGS_RIP];
4994
cd3ff653
JR
4995 /*
4996 * A vmexit emulation is required before the vcpu can be executed
4997 * again.
4998 */
4999 if (unlikely(svm->nested.exit_required))
5000 return;
5001
a12713c2
LP
5002 /*
5003 * Disable singlestep if we're injecting an interrupt/exception.
5004 * We don't want our modified rflags to be pushed on the stack where
5005 * we might not be able to easily reset them if we disabled NMI
5006 * singlestep later.
5007 */
5008 if (svm->nmi_singlestep && svm->vmcb->control.event_inj) {
5009 /*
5010 * Event injection happens before external interrupts cause a
5011 * vmexit and interrupts are disabled here, so smp_send_reschedule
5012 * is enough to force an immediate vmexit.
5013 */
5014 disable_nmi_singlestep(svm);
5015 smp_send_reschedule(vcpu->cpu);
5016 }
5017
e756fc62 5018 pre_svm_run(svm);
6aa8b732 5019
649d6864
JR
5020 sync_lapic_to_cr8(vcpu);
5021
cda0ffdd 5022 svm->vmcb->save.cr2 = vcpu->arch.cr2;
6aa8b732 5023
04d2cc77
AK
5024 clgi();
5025
5026 local_irq_enable();
36241b8c 5027
a57b802c
KA
5028 /*
5029 * If this vCPU has touched SPEC_CTRL, restore the guest's value if
5030 * it's non-zero. Since vmentry is serialising on affected CPUs, there
5031 * is no need to worry about the conditional branch over the wrmsr
5032 * being speculatively taken.
5033 */
19fff03f 5034 x86_spec_ctrl_set_guest(svm->spec_ctrl);
a57b802c 5035
6aa8b732 5036 asm volatile (
7454766f
AK
5037 "push %%" _ASM_BP "; \n\t"
5038 "mov %c[rbx](%[svm]), %%" _ASM_BX " \n\t"
5039 "mov %c[rcx](%[svm]), %%" _ASM_CX " \n\t"
5040 "mov %c[rdx](%[svm]), %%" _ASM_DX " \n\t"
5041 "mov %c[rsi](%[svm]), %%" _ASM_SI " \n\t"
5042 "mov %c[rdi](%[svm]), %%" _ASM_DI " \n\t"
5043 "mov %c[rbp](%[svm]), %%" _ASM_BP " \n\t"
05b3e0c2 5044#ifdef CONFIG_X86_64
fb3f0f51
RR
5045 "mov %c[r8](%[svm]), %%r8 \n\t"
5046 "mov %c[r9](%[svm]), %%r9 \n\t"
5047 "mov %c[r10](%[svm]), %%r10 \n\t"
5048 "mov %c[r11](%[svm]), %%r11 \n\t"
5049 "mov %c[r12](%[svm]), %%r12 \n\t"
5050 "mov %c[r13](%[svm]), %%r13 \n\t"
5051 "mov %c[r14](%[svm]), %%r14 \n\t"
5052 "mov %c[r15](%[svm]), %%r15 \n\t"
6aa8b732
AK
5053#endif
5054
6aa8b732 5055 /* Enter guest mode */
7454766f
AK
5056 "push %%" _ASM_AX " \n\t"
5057 "mov %c[vmcb](%[svm]), %%" _ASM_AX " \n\t"
4ecac3fd
AK
5058 __ex(SVM_VMLOAD) "\n\t"
5059 __ex(SVM_VMRUN) "\n\t"
5060 __ex(SVM_VMSAVE) "\n\t"
7454766f 5061 "pop %%" _ASM_AX " \n\t"
6aa8b732
AK
5062
5063 /* Save guest registers, load host registers */
7454766f
AK
5064 "mov %%" _ASM_BX ", %c[rbx](%[svm]) \n\t"
5065 "mov %%" _ASM_CX ", %c[rcx](%[svm]) \n\t"
5066 "mov %%" _ASM_DX ", %c[rdx](%[svm]) \n\t"
5067 "mov %%" _ASM_SI ", %c[rsi](%[svm]) \n\t"
5068 "mov %%" _ASM_DI ", %c[rdi](%[svm]) \n\t"
5069 "mov %%" _ASM_BP ", %c[rbp](%[svm]) \n\t"
05b3e0c2 5070#ifdef CONFIG_X86_64
fb3f0f51
RR
5071 "mov %%r8, %c[r8](%[svm]) \n\t"
5072 "mov %%r9, %c[r9](%[svm]) \n\t"
5073 "mov %%r10, %c[r10](%[svm]) \n\t"
5074 "mov %%r11, %c[r11](%[svm]) \n\t"
5075 "mov %%r12, %c[r12](%[svm]) \n\t"
5076 "mov %%r13, %c[r13](%[svm]) \n\t"
5077 "mov %%r14, %c[r14](%[svm]) \n\t"
5078 "mov %%r15, %c[r15](%[svm]) \n\t"
0cb5b306
JM
5079#endif
5080 /*
5081 * Clear host registers marked as clobbered to prevent
5082 * speculative use.
5083 */
5084 "xor %%" _ASM_BX ", %%" _ASM_BX " \n\t"
5085 "xor %%" _ASM_CX ", %%" _ASM_CX " \n\t"
5086 "xor %%" _ASM_DX ", %%" _ASM_DX " \n\t"
5087 "xor %%" _ASM_SI ", %%" _ASM_SI " \n\t"
5088 "xor %%" _ASM_DI ", %%" _ASM_DI " \n\t"
5089#ifdef CONFIG_X86_64
5090 "xor %%r8, %%r8 \n\t"
5091 "xor %%r9, %%r9 \n\t"
5092 "xor %%r10, %%r10 \n\t"
5093 "xor %%r11, %%r11 \n\t"
5094 "xor %%r12, %%r12 \n\t"
5095 "xor %%r13, %%r13 \n\t"
5096 "xor %%r14, %%r14 \n\t"
5097 "xor %%r15, %%r15 \n\t"
6aa8b732 5098#endif
7454766f 5099 "pop %%" _ASM_BP
6aa8b732 5100 :
fb3f0f51 5101 : [svm]"a"(svm),
6aa8b732 5102 [vmcb]"i"(offsetof(struct vcpu_svm, vmcb_pa)),
ad312c7c
ZX
5103 [rbx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RBX])),
5104 [rcx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RCX])),
5105 [rdx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RDX])),
5106 [rsi]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RSI])),
5107 [rdi]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RDI])),
5108 [rbp]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RBP]))
05b3e0c2 5109#ifdef CONFIG_X86_64
ad312c7c
ZX
5110 , [r8]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R8])),
5111 [r9]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R9])),
5112 [r10]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R10])),
5113 [r11]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R11])),
5114 [r12]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R12])),
5115 [r13]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R13])),
5116 [r14]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R14])),
5117 [r15]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R15]))
6aa8b732 5118#endif
54a08c04
LV
5119 : "cc", "memory"
5120#ifdef CONFIG_X86_64
7454766f 5121 , "rbx", "rcx", "rdx", "rsi", "rdi"
54a08c04 5122 , "r8", "r9", "r10", "r11" , "r12", "r13", "r14", "r15"
7454766f
AK
5123#else
5124 , "ebx", "ecx", "edx", "esi", "edi"
54a08c04
LV
5125#endif
5126 );
6aa8b732 5127
a57b802c
KA
5128 /*
5129 * We do not use IBRS in the kernel. If this vCPU has used the
5130 * SPEC_CTRL MSR it may have left it on; save the value and
5131 * turn it off. This is much more efficient than blindly adding
5132 * it to the atomic save/restore list. Especially as the former
5133 * (Saving guest MSRs on vmexit) doesn't even exist in KVM.
5134 *
5135 * For non-nested case:
5136 * If the L01 MSR bitmap does not intercept the MSR, then we need to
5137 * save it.
5138 *
5139 * For nested case:
5140 * If the L02 MSR bitmap does not intercept the MSR, then we need to
5141 * save it.
5142 */
481ab71e 5143 if (unlikely(!msr_write_intercepted(vcpu, MSR_IA32_SPEC_CTRL)))
f471d71b 5144 svm->spec_ctrl = native_read_msr(MSR_IA32_SPEC_CTRL);
a57b802c 5145
19fff03f 5146 x86_spec_ctrl_restore_host(svm->spec_ctrl);
a57b802c 5147
117cc7a9
DW
5148 /* Eliminate branch target predictions from guest mode */
5149 vmexit_fill_RSB();
5150
82ca2d10
AK
5151#ifdef CONFIG_X86_64
5152 wrmsrl(MSR_GS_BASE, svm->host.gs_base);
5153#else
dacccfdd 5154 loadsegment(fs, svm->host.fs);
831ca609
AK
5155#ifndef CONFIG_X86_32_LAZY_GS
5156 loadsegment(gs, svm->host.gs);
5157#endif
9581d442 5158#endif
6aa8b732
AK
5159
5160 reload_tss(vcpu);
5161
56ba47dd
AK
5162 local_irq_disable();
5163
13c34e07
AK
5164 vcpu->arch.cr2 = svm->vmcb->save.cr2;
5165 vcpu->arch.regs[VCPU_REGS_RAX] = svm->vmcb->save.rax;
5166 vcpu->arch.regs[VCPU_REGS_RSP] = svm->vmcb->save.rsp;
5167 vcpu->arch.regs[VCPU_REGS_RIP] = svm->vmcb->save.rip;
5168
3781c01c
JR
5169 if (unlikely(svm->vmcb->control.exit_code == SVM_EXIT_NMI))
5170 kvm_before_handle_nmi(&svm->vcpu);
5171
5172 stgi();
5173
5174 /* Any pending NMI will happen here */
5175
5176 if (unlikely(svm->vmcb->control.exit_code == SVM_EXIT_NMI))
5177 kvm_after_handle_nmi(&svm->vcpu);
5178
d7bf8221
JR
5179 sync_cr8_to_lapic(vcpu);
5180
a2fa3e9f 5181 svm->next_rip = 0;
9222be18 5182
38e5e92f
JR
5183 svm->vmcb->control.tlb_ctl = TLB_CONTROL_DO_NOTHING;
5184
631bc487
GN
5185 /* if exit due to PF check for async PF */
5186 if (svm->vmcb->control.exit_code == SVM_EXIT_EXCP_BASE + PF_VECTOR)
1261bfa3 5187 svm->vcpu.arch.apf.host_apf_reason = kvm_read_and_reset_pf_reason();
631bc487 5188
6de4f3ad
AK
5189 if (npt_enabled) {
5190 vcpu->arch.regs_avail &= ~(1 << VCPU_EXREG_PDPTR);
5191 vcpu->arch.regs_dirty &= ~(1 << VCPU_EXREG_PDPTR);
5192 }
fe5913e4
JR
5193
5194 /*
5195 * We need to handle MC intercepts here before the vcpu has a chance to
5196 * change the physical cpu
5197 */
5198 if (unlikely(svm->vmcb->control.exit_code ==
5199 SVM_EXIT_EXCP_BASE + MC_VECTOR))
5200 svm_handle_mce(svm);
8d28fec4
RJ
5201
5202 mark_all_clean(svm->vmcb);
6aa8b732 5203}
c207aee4 5204STACK_FRAME_NON_STANDARD(svm_vcpu_run);
6aa8b732 5205
6aa8b732
AK
5206static void svm_set_cr3(struct kvm_vcpu *vcpu, unsigned long root)
5207{
a2fa3e9f
GH
5208 struct vcpu_svm *svm = to_svm(vcpu);
5209
d0ec49d4 5210 svm->vmcb->save.cr3 = __sme_set(root);
dcca1a65 5211 mark_dirty(svm->vmcb, VMCB_CR);
f40f6a45 5212 svm_flush_tlb(vcpu);
6aa8b732
AK
5213}
5214
1c97f0a0
JR
5215static void set_tdp_cr3(struct kvm_vcpu *vcpu, unsigned long root)
5216{
5217 struct vcpu_svm *svm = to_svm(vcpu);
5218
d0ec49d4 5219 svm->vmcb->control.nested_cr3 = __sme_set(root);
b2747166 5220 mark_dirty(svm->vmcb, VMCB_NPT);
1c97f0a0
JR
5221
5222 /* Also sync guest cr3 here in case we live migrate */
9f8fe504 5223 svm->vmcb->save.cr3 = kvm_read_cr3(vcpu);
dcca1a65 5224 mark_dirty(svm->vmcb, VMCB_CR);
1c97f0a0 5225
f40f6a45 5226 svm_flush_tlb(vcpu);
1c97f0a0
JR
5227}
5228
6aa8b732
AK
5229static int is_disabled(void)
5230{
6031a61c
JR
5231 u64 vm_cr;
5232
5233 rdmsrl(MSR_VM_CR, vm_cr);
5234 if (vm_cr & (1 << SVM_VM_CR_SVM_DISABLE))
5235 return 1;
5236
6aa8b732
AK
5237 return 0;
5238}
5239
102d8325
IM
5240static void
5241svm_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
5242{
5243 /*
5244 * Patch in the VMMCALL instruction:
5245 */
5246 hypercall[0] = 0x0f;
5247 hypercall[1] = 0x01;
5248 hypercall[2] = 0xd9;
102d8325
IM
5249}
5250
002c7f7c
YS
5251static void svm_check_processor_compat(void *rtn)
5252{
5253 *(int *)rtn = 0;
5254}
5255
774ead3a
AK
5256static bool svm_cpu_has_accelerated_tpr(void)
5257{
5258 return false;
5259}
5260
6d396b55
PB
5261static bool svm_has_high_real_mode_segbase(void)
5262{
5263 return true;
5264}
5265
fc07e76a
PB
5266static u64 svm_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
5267{
5268 return 0;
5269}
5270
0e851880
SY
5271static void svm_cpuid_update(struct kvm_vcpu *vcpu)
5272{
6092d3d3
JR
5273 struct vcpu_svm *svm = to_svm(vcpu);
5274
5275 /* Update nrips enabled cache */
d6321d49 5276 svm->nrips_enabled = !!guest_cpuid_has(&svm->vcpu, X86_FEATURE_NRIPS);
46781eae
SS
5277
5278 if (!kvm_vcpu_apicv_active(vcpu))
5279 return;
5280
1b4d56b8 5281 guest_cpuid_clear(vcpu, X86_FEATURE_X2APIC);
0e851880
SY
5282}
5283
d4330ef2
JR
5284static void svm_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry)
5285{
c2c63a49 5286 switch (func) {
46781eae
SS
5287 case 0x1:
5288 if (avic)
5289 entry->ecx &= ~bit(X86_FEATURE_X2APIC);
5290 break;
4c62a2dc
JR
5291 case 0x80000001:
5292 if (nested)
5293 entry->ecx |= (1 << 2); /* Set SVM bit */
5294 break;
c2c63a49
JR
5295 case 0x8000000A:
5296 entry->eax = 1; /* SVM revision 1 */
5297 entry->ebx = 8; /* Lets support 8 ASIDs in case we add proper
5298 ASID emulation to nested SVM */
5299 entry->ecx = 0; /* Reserved */
7a190667
JR
5300 entry->edx = 0; /* Per default do not support any
5301 additional features */
5302
5303 /* Support next_rip if host supports it */
2a6b20b8 5304 if (boot_cpu_has(X86_FEATURE_NRIPS))
7a190667 5305 entry->edx |= SVM_FEATURE_NRIP;
c2c63a49 5306
3d4aeaad
JR
5307 /* Support NPT for the guest if enabled */
5308 if (npt_enabled)
5309 entry->edx |= SVM_FEATURE_NPT;
5310
c2c63a49
JR
5311 break;
5312 }
d4330ef2
JR
5313}
5314
17cc3935 5315static int svm_get_lpage_level(void)
344f414f 5316{
17cc3935 5317 return PT_PDPE_LEVEL;
344f414f
JR
5318}
5319
4e47c7a6
SY
5320static bool svm_rdtscp_supported(void)
5321{
46896c73 5322 return boot_cpu_has(X86_FEATURE_RDTSCP);
4e47c7a6
SY
5323}
5324
ad756a16
MJ
5325static bool svm_invpcid_supported(void)
5326{
5327 return false;
5328}
5329
93c4adc7
PB
5330static bool svm_mpx_supported(void)
5331{
5332 return false;
5333}
5334
55412b2e
WL
5335static bool svm_xsaves_supported(void)
5336{
5337 return false;
5338}
5339
f5f48ee1
SY
5340static bool svm_has_wbinvd_exit(void)
5341{
5342 return true;
5343}
5344
8061252e 5345#define PRE_EX(exit) { .exit_code = (exit), \
40e19b51 5346 .stage = X86_ICPT_PRE_EXCEPT, }
cfec82cb 5347#define POST_EX(exit) { .exit_code = (exit), \
40e19b51 5348 .stage = X86_ICPT_POST_EXCEPT, }
d7eb8203 5349#define POST_MEM(exit) { .exit_code = (exit), \
40e19b51 5350 .stage = X86_ICPT_POST_MEMACCESS, }
cfec82cb 5351
09941fbb 5352static const struct __x86_intercept {
cfec82cb
JR
5353 u32 exit_code;
5354 enum x86_intercept_stage stage;
cfec82cb
JR
5355} x86_intercept_map[] = {
5356 [x86_intercept_cr_read] = POST_EX(SVM_EXIT_READ_CR0),
5357 [x86_intercept_cr_write] = POST_EX(SVM_EXIT_WRITE_CR0),
5358 [x86_intercept_clts] = POST_EX(SVM_EXIT_WRITE_CR0),
5359 [x86_intercept_lmsw] = POST_EX(SVM_EXIT_WRITE_CR0),
5360 [x86_intercept_smsw] = POST_EX(SVM_EXIT_READ_CR0),
3b88e41a
JR
5361 [x86_intercept_dr_read] = POST_EX(SVM_EXIT_READ_DR0),
5362 [x86_intercept_dr_write] = POST_EX(SVM_EXIT_WRITE_DR0),
dee6bb70
JR
5363 [x86_intercept_sldt] = POST_EX(SVM_EXIT_LDTR_READ),
5364 [x86_intercept_str] = POST_EX(SVM_EXIT_TR_READ),
5365 [x86_intercept_lldt] = POST_EX(SVM_EXIT_LDTR_WRITE),
5366 [x86_intercept_ltr] = POST_EX(SVM_EXIT_TR_WRITE),
5367 [x86_intercept_sgdt] = POST_EX(SVM_EXIT_GDTR_READ),
5368 [x86_intercept_sidt] = POST_EX(SVM_EXIT_IDTR_READ),
5369 [x86_intercept_lgdt] = POST_EX(SVM_EXIT_GDTR_WRITE),
5370 [x86_intercept_lidt] = POST_EX(SVM_EXIT_IDTR_WRITE),
01de8b09
JR
5371 [x86_intercept_vmrun] = POST_EX(SVM_EXIT_VMRUN),
5372 [x86_intercept_vmmcall] = POST_EX(SVM_EXIT_VMMCALL),
5373 [x86_intercept_vmload] = POST_EX(SVM_EXIT_VMLOAD),
5374 [x86_intercept_vmsave] = POST_EX(SVM_EXIT_VMSAVE),
5375 [x86_intercept_stgi] = POST_EX(SVM_EXIT_STGI),
5376 [x86_intercept_clgi] = POST_EX(SVM_EXIT_CLGI),
5377 [x86_intercept_skinit] = POST_EX(SVM_EXIT_SKINIT),
5378 [x86_intercept_invlpga] = POST_EX(SVM_EXIT_INVLPGA),
d7eb8203
JR
5379 [x86_intercept_rdtscp] = POST_EX(SVM_EXIT_RDTSCP),
5380 [x86_intercept_monitor] = POST_MEM(SVM_EXIT_MONITOR),
5381 [x86_intercept_mwait] = POST_EX(SVM_EXIT_MWAIT),
8061252e
JR
5382 [x86_intercept_invlpg] = POST_EX(SVM_EXIT_INVLPG),
5383 [x86_intercept_invd] = POST_EX(SVM_EXIT_INVD),
5384 [x86_intercept_wbinvd] = POST_EX(SVM_EXIT_WBINVD),
5385 [x86_intercept_wrmsr] = POST_EX(SVM_EXIT_MSR),
5386 [x86_intercept_rdtsc] = POST_EX(SVM_EXIT_RDTSC),
5387 [x86_intercept_rdmsr] = POST_EX(SVM_EXIT_MSR),
5388 [x86_intercept_rdpmc] = POST_EX(SVM_EXIT_RDPMC),
5389 [x86_intercept_cpuid] = PRE_EX(SVM_EXIT_CPUID),
5390 [x86_intercept_rsm] = PRE_EX(SVM_EXIT_RSM),
bf608f88
JR
5391 [x86_intercept_pause] = PRE_EX(SVM_EXIT_PAUSE),
5392 [x86_intercept_pushf] = PRE_EX(SVM_EXIT_PUSHF),
5393 [x86_intercept_popf] = PRE_EX(SVM_EXIT_POPF),
5394 [x86_intercept_intn] = PRE_EX(SVM_EXIT_SWINT),
5395 [x86_intercept_iret] = PRE_EX(SVM_EXIT_IRET),
5396 [x86_intercept_icebp] = PRE_EX(SVM_EXIT_ICEBP),
5397 [x86_intercept_hlt] = POST_EX(SVM_EXIT_HLT),
f6511935
JR
5398 [x86_intercept_in] = POST_EX(SVM_EXIT_IOIO),
5399 [x86_intercept_ins] = POST_EX(SVM_EXIT_IOIO),
5400 [x86_intercept_out] = POST_EX(SVM_EXIT_IOIO),
5401 [x86_intercept_outs] = POST_EX(SVM_EXIT_IOIO),
cfec82cb
JR
5402};
5403
8061252e 5404#undef PRE_EX
cfec82cb 5405#undef POST_EX
d7eb8203 5406#undef POST_MEM
cfec82cb 5407
8a76d7f2
JR
5408static int svm_check_intercept(struct kvm_vcpu *vcpu,
5409 struct x86_instruction_info *info,
5410 enum x86_intercept_stage stage)
5411{
cfec82cb
JR
5412 struct vcpu_svm *svm = to_svm(vcpu);
5413 int vmexit, ret = X86EMUL_CONTINUE;
5414 struct __x86_intercept icpt_info;
5415 struct vmcb *vmcb = svm->vmcb;
5416
5417 if (info->intercept >= ARRAY_SIZE(x86_intercept_map))
5418 goto out;
5419
5420 icpt_info = x86_intercept_map[info->intercept];
5421
40e19b51 5422 if (stage != icpt_info.stage)
cfec82cb
JR
5423 goto out;
5424
5425 switch (icpt_info.exit_code) {
5426 case SVM_EXIT_READ_CR0:
5427 if (info->intercept == x86_intercept_cr_read)
5428 icpt_info.exit_code += info->modrm_reg;
5429 break;
5430 case SVM_EXIT_WRITE_CR0: {
5431 unsigned long cr0, val;
5432 u64 intercept;
5433
5434 if (info->intercept == x86_intercept_cr_write)
5435 icpt_info.exit_code += info->modrm_reg;
5436
62baf44c
JK
5437 if (icpt_info.exit_code != SVM_EXIT_WRITE_CR0 ||
5438 info->intercept == x86_intercept_clts)
cfec82cb
JR
5439 break;
5440
5441 intercept = svm->nested.intercept;
5442
5443 if (!(intercept & (1ULL << INTERCEPT_SELECTIVE_CR0)))
5444 break;
5445
5446 cr0 = vcpu->arch.cr0 & ~SVM_CR0_SELECTIVE_MASK;
5447 val = info->src_val & ~SVM_CR0_SELECTIVE_MASK;
5448
5449 if (info->intercept == x86_intercept_lmsw) {
5450 cr0 &= 0xfUL;
5451 val &= 0xfUL;
5452 /* lmsw can't clear PE - catch this here */
5453 if (cr0 & X86_CR0_PE)
5454 val |= X86_CR0_PE;
5455 }
5456
5457 if (cr0 ^ val)
5458 icpt_info.exit_code = SVM_EXIT_CR0_SEL_WRITE;
5459
5460 break;
5461 }
3b88e41a
JR
5462 case SVM_EXIT_READ_DR0:
5463 case SVM_EXIT_WRITE_DR0:
5464 icpt_info.exit_code += info->modrm_reg;
5465 break;
8061252e
JR
5466 case SVM_EXIT_MSR:
5467 if (info->intercept == x86_intercept_wrmsr)
5468 vmcb->control.exit_info_1 = 1;
5469 else
5470 vmcb->control.exit_info_1 = 0;
5471 break;
bf608f88
JR
5472 case SVM_EXIT_PAUSE:
5473 /*
5474 * We get this for NOP only, but pause
5475 * is rep not, check this here
5476 */
5477 if (info->rep_prefix != REPE_PREFIX)
5478 goto out;
49a8afca 5479 break;
f6511935
JR
5480 case SVM_EXIT_IOIO: {
5481 u64 exit_info;
5482 u32 bytes;
5483
f6511935
JR
5484 if (info->intercept == x86_intercept_in ||
5485 info->intercept == x86_intercept_ins) {
6cbc5f5a
JK
5486 exit_info = ((info->src_val & 0xffff) << 16) |
5487 SVM_IOIO_TYPE_MASK;
f6511935 5488 bytes = info->dst_bytes;
6493f157 5489 } else {
6cbc5f5a 5490 exit_info = (info->dst_val & 0xffff) << 16;
6493f157 5491 bytes = info->src_bytes;
f6511935
JR
5492 }
5493
5494 if (info->intercept == x86_intercept_outs ||
5495 info->intercept == x86_intercept_ins)
5496 exit_info |= SVM_IOIO_STR_MASK;
5497
5498 if (info->rep_prefix)
5499 exit_info |= SVM_IOIO_REP_MASK;
5500
5501 bytes = min(bytes, 4u);
5502
5503 exit_info |= bytes << SVM_IOIO_SIZE_SHIFT;
5504
5505 exit_info |= (u32)info->ad_bytes << (SVM_IOIO_ASIZE_SHIFT - 1);
5506
5507 vmcb->control.exit_info_1 = exit_info;
5508 vmcb->control.exit_info_2 = info->next_rip;
5509
5510 break;
5511 }
cfec82cb
JR
5512 default:
5513 break;
5514 }
5515
f104765b
BD
5516 /* TODO: Advertise NRIPS to guest hypervisor unconditionally */
5517 if (static_cpu_has(X86_FEATURE_NRIPS))
5518 vmcb->control.next_rip = info->next_rip;
cfec82cb
JR
5519 vmcb->control.exit_code = icpt_info.exit_code;
5520 vmexit = nested_svm_exit_handled(svm);
5521
5522 ret = (vmexit == NESTED_EXIT_DONE) ? X86EMUL_INTERCEPTED
5523 : X86EMUL_CONTINUE;
5524
5525out:
5526 return ret;
8a76d7f2
JR
5527}
5528
a547c6db
YZ
5529static void svm_handle_external_intr(struct kvm_vcpu *vcpu)
5530{
5531 local_irq_enable();
f2485b3e
PB
5532 /*
5533 * We must have an instruction with interrupts enabled, so
5534 * the timer interrupt isn't delayed by the interrupt shadow.
5535 */
5536 asm("nop");
5537 local_irq_disable();
a547c6db
YZ
5538}
5539
ae97a3b8
RK
5540static void svm_sched_in(struct kvm_vcpu *vcpu, int cpu)
5541{
5542}
5543
be8ca170
SS
5544static inline void avic_post_state_restore(struct kvm_vcpu *vcpu)
5545{
5546 if (avic_handle_apic_id_update(vcpu) != 0)
5547 return;
5548 if (avic_handle_dfr_update(vcpu) != 0)
5549 return;
5550 avic_handle_ldr_update(vcpu);
5551}
5552
74f16909
BP
5553static void svm_setup_mce(struct kvm_vcpu *vcpu)
5554{
5555 /* [63:9] are reserved. */
5556 vcpu->arch.mcg_cap &= 0x1ff;
5557}
5558
72d7b374
LP
5559static int svm_smi_allowed(struct kvm_vcpu *vcpu)
5560{
05cade71
LP
5561 struct vcpu_svm *svm = to_svm(vcpu);
5562
5563 /* Per APM Vol.2 15.22.2 "Response to SMI" */
5564 if (!gif_set(svm))
5565 return 0;
5566
5567 if (is_guest_mode(&svm->vcpu) &&
5568 svm->nested.intercept & (1ULL << INTERCEPT_SMI)) {
5569 /* TODO: Might need to set exit_info_1 and exit_info_2 here */
5570 svm->vmcb->control.exit_code = SVM_EXIT_SMI;
5571 svm->nested.exit_required = true;
5572 return 0;
5573 }
5574
72d7b374
LP
5575 return 1;
5576}
5577
0234bf88
LP
5578static int svm_pre_enter_smm(struct kvm_vcpu *vcpu, char *smstate)
5579{
05cade71
LP
5580 struct vcpu_svm *svm = to_svm(vcpu);
5581 int ret;
5582
5583 if (is_guest_mode(vcpu)) {
5584 /* FED8h - SVM Guest */
5585 put_smstate(u64, smstate, 0x7ed8, 1);
5586 /* FEE0h - SVM Guest VMCB Physical Address */
5587 put_smstate(u64, smstate, 0x7ee0, svm->nested.vmcb);
5588
5589 svm->vmcb->save.rax = vcpu->arch.regs[VCPU_REGS_RAX];
5590 svm->vmcb->save.rsp = vcpu->arch.regs[VCPU_REGS_RSP];
5591 svm->vmcb->save.rip = vcpu->arch.regs[VCPU_REGS_RIP];
5592
5593 ret = nested_svm_vmexit(svm);
5594 if (ret)
5595 return ret;
5596 }
0234bf88
LP
5597 return 0;
5598}
5599
5600static int svm_pre_leave_smm(struct kvm_vcpu *vcpu, u64 smbase)
5601{
05cade71
LP
5602 struct vcpu_svm *svm = to_svm(vcpu);
5603 struct vmcb *nested_vmcb;
5604 struct page *page;
5605 struct {
5606 u64 guest;
5607 u64 vmcb;
5608 } svm_state_save;
5609 int ret;
5610
5611 ret = kvm_vcpu_read_guest(vcpu, smbase + 0xfed8, &svm_state_save,
5612 sizeof(svm_state_save));
5613 if (ret)
5614 return ret;
5615
5616 if (svm_state_save.guest) {
5617 vcpu->arch.hflags &= ~HF_SMM_MASK;
5618 nested_vmcb = nested_svm_map(svm, svm_state_save.vmcb, &page);
5619 if (nested_vmcb)
5620 enter_svm_guest_mode(svm, svm_state_save.vmcb, nested_vmcb, page);
5621 else
5622 ret = 1;
5623 vcpu->arch.hflags |= HF_SMM_MASK;
5624 }
5625 return ret;
0234bf88
LP
5626}
5627
cc3d967f
LP
5628static int enable_smi_window(struct kvm_vcpu *vcpu)
5629{
5630 struct vcpu_svm *svm = to_svm(vcpu);
5631
5632 if (!gif_set(svm)) {
5633 if (vgif_enabled(svm))
5634 set_intercept(svm, INTERCEPT_STGI);
5635 /* STGI will cause a vm exit */
5636 return 1;
5637 }
5638 return 0;
5639}
5640
404f6aac 5641static struct kvm_x86_ops svm_x86_ops __ro_after_init = {
6aa8b732
AK
5642 .cpu_has_kvm_support = has_svm,
5643 .disabled_by_bios = is_disabled,
5644 .hardware_setup = svm_hardware_setup,
5645 .hardware_unsetup = svm_hardware_unsetup,
002c7f7c 5646 .check_processor_compatibility = svm_check_processor_compat,
6aa8b732
AK
5647 .hardware_enable = svm_hardware_enable,
5648 .hardware_disable = svm_hardware_disable,
774ead3a 5649 .cpu_has_accelerated_tpr = svm_cpu_has_accelerated_tpr,
6d396b55 5650 .cpu_has_high_real_mode_segbase = svm_has_high_real_mode_segbase,
6aa8b732
AK
5651
5652 .vcpu_create = svm_create_vcpu,
5653 .vcpu_free = svm_free_vcpu,
04d2cc77 5654 .vcpu_reset = svm_vcpu_reset,
6aa8b732 5655
44a95dae
SS
5656 .vm_init = avic_vm_init,
5657 .vm_destroy = avic_vm_destroy,
5658
04d2cc77 5659 .prepare_guest_switch = svm_prepare_guest_switch,
6aa8b732
AK
5660 .vcpu_load = svm_vcpu_load,
5661 .vcpu_put = svm_vcpu_put,
8221c137
SS
5662 .vcpu_blocking = svm_vcpu_blocking,
5663 .vcpu_unblocking = svm_vcpu_unblocking,
6aa8b732 5664
a96036b8 5665 .update_bp_intercept = update_bp_intercept,
6aa8b732
AK
5666 .get_msr = svm_get_msr,
5667 .set_msr = svm_set_msr,
5668 .get_segment_base = svm_get_segment_base,
5669 .get_segment = svm_get_segment,
5670 .set_segment = svm_set_segment,
2e4d2653 5671 .get_cpl = svm_get_cpl,
1747fb71 5672 .get_cs_db_l_bits = kvm_get_cs_db_l_bits,
e8467fda 5673 .decache_cr0_guest_bits = svm_decache_cr0_guest_bits,
aff48baa 5674 .decache_cr3 = svm_decache_cr3,
25c4c276 5675 .decache_cr4_guest_bits = svm_decache_cr4_guest_bits,
6aa8b732 5676 .set_cr0 = svm_set_cr0,
6aa8b732
AK
5677 .set_cr3 = svm_set_cr3,
5678 .set_cr4 = svm_set_cr4,
5679 .set_efer = svm_set_efer,
5680 .get_idt = svm_get_idt,
5681 .set_idt = svm_set_idt,
5682 .get_gdt = svm_get_gdt,
5683 .set_gdt = svm_set_gdt,
73aaf249
JK
5684 .get_dr6 = svm_get_dr6,
5685 .set_dr6 = svm_set_dr6,
020df079 5686 .set_dr7 = svm_set_dr7,
facb0139 5687 .sync_dirty_debug_regs = svm_sync_dirty_debug_regs,
6de4f3ad 5688 .cache_reg = svm_cache_reg,
6aa8b732
AK
5689 .get_rflags = svm_get_rflags,
5690 .set_rflags = svm_set_rflags,
be94f6b7 5691
6aa8b732 5692 .tlb_flush = svm_flush_tlb,
6aa8b732 5693
6aa8b732 5694 .run = svm_vcpu_run,
04d2cc77 5695 .handle_exit = handle_exit,
6aa8b732 5696 .skip_emulated_instruction = skip_emulated_instruction,
2809f5d2
GC
5697 .set_interrupt_shadow = svm_set_interrupt_shadow,
5698 .get_interrupt_shadow = svm_get_interrupt_shadow,
102d8325 5699 .patch_hypercall = svm_patch_hypercall,
2a8067f1 5700 .set_irq = svm_set_irq,
95ba8273 5701 .set_nmi = svm_inject_nmi,
298101da 5702 .queue_exception = svm_queue_exception,
b463a6f7 5703 .cancel_injection = svm_cancel_injection,
78646121 5704 .interrupt_allowed = svm_interrupt_allowed,
95ba8273 5705 .nmi_allowed = svm_nmi_allowed,
3cfc3092
JK
5706 .get_nmi_mask = svm_get_nmi_mask,
5707 .set_nmi_mask = svm_set_nmi_mask,
95ba8273
GN
5708 .enable_nmi_window = enable_nmi_window,
5709 .enable_irq_window = enable_irq_window,
5710 .update_cr8_intercept = update_cr8_intercept,
8d14695f 5711 .set_virtual_x2apic_mode = svm_set_virtual_x2apic_mode,
d62caabb
AS
5712 .get_enable_apicv = svm_get_enable_apicv,
5713 .refresh_apicv_exec_ctrl = svm_refresh_apicv_exec_ctrl,
c7c9c56c 5714 .load_eoi_exitmap = svm_load_eoi_exitmap,
44a95dae
SS
5715 .hwapic_irr_update = svm_hwapic_irr_update,
5716 .hwapic_isr_update = svm_hwapic_isr_update,
be8ca170 5717 .apicv_post_state_restore = avic_post_state_restore,
cbc94022
IE
5718
5719 .set_tss_addr = svm_set_tss_addr,
67253af5 5720 .get_tdp_level = get_npt_level,
4b12f0de 5721 .get_mt_mask = svm_get_mt_mask,
229456fc 5722
586f9607 5723 .get_exit_info = svm_get_exit_info,
586f9607 5724
17cc3935 5725 .get_lpage_level = svm_get_lpage_level,
0e851880
SY
5726
5727 .cpuid_update = svm_cpuid_update,
4e47c7a6
SY
5728
5729 .rdtscp_supported = svm_rdtscp_supported,
ad756a16 5730 .invpcid_supported = svm_invpcid_supported,
93c4adc7 5731 .mpx_supported = svm_mpx_supported,
55412b2e 5732 .xsaves_supported = svm_xsaves_supported,
d4330ef2
JR
5733
5734 .set_supported_cpuid = svm_set_supported_cpuid,
f5f48ee1
SY
5735
5736 .has_wbinvd_exit = svm_has_wbinvd_exit,
99e3e30a
ZA
5737
5738 .write_tsc_offset = svm_write_tsc_offset,
1c97f0a0
JR
5739
5740 .set_tdp_cr3 = set_tdp_cr3,
8a76d7f2
JR
5741
5742 .check_intercept = svm_check_intercept,
a547c6db 5743 .handle_external_intr = svm_handle_external_intr,
ae97a3b8
RK
5744
5745 .sched_in = svm_sched_in,
25462f7f
WH
5746
5747 .pmu_ops = &amd_pmu_ops,
340d3bc3 5748 .deliver_posted_interrupt = svm_deliver_avic_intr,
411b44ba 5749 .update_pi_irte = svm_update_pi_irte,
74f16909 5750 .setup_mce = svm_setup_mce,
0234bf88 5751
72d7b374 5752 .smi_allowed = svm_smi_allowed,
0234bf88
LP
5753 .pre_enter_smm = svm_pre_enter_smm,
5754 .pre_leave_smm = svm_pre_leave_smm,
cc3d967f 5755 .enable_smi_window = enable_smi_window,
6aa8b732
AK
5756};
5757
5758static int __init svm_init(void)
5759{
cb498ea2 5760 return kvm_init(&svm_x86_ops, sizeof(struct vcpu_svm),
0ee75bea 5761 __alignof__(struct vcpu_svm), THIS_MODULE);
6aa8b732
AK
5762}
5763
5764static void __exit svm_exit(void)
5765{
cb498ea2 5766 kvm_exit();
6aa8b732
AK
5767}
5768
5769module_init(svm_init)
5770module_exit(svm_exit)