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