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