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