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