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