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