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