]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - arch/x86/kvm/svm.c
KVM: SVM: Add clean-bit for Segements and CPL
[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 */
edf88417
AK
17#include <linux/kvm_host.h>
18
85f455f7 19#include "irq.h"
1d737c8a 20#include "mmu.h"
5fdbf976 21#include "kvm_cache_regs.h"
fe4c7b19 22#include "x86.h"
e495606d 23
6aa8b732 24#include <linux/module.h>
9d8f549d 25#include <linux/kernel.h>
6aa8b732
AK
26#include <linux/vmalloc.h>
27#include <linux/highmem.h>
e8edc6e0 28#include <linux/sched.h>
229456fc 29#include <linux/ftrace_event.h>
5a0e3ad6 30#include <linux/slab.h>
6aa8b732 31
67ec6607 32#include <asm/tlbflush.h>
e495606d 33#include <asm/desc.h>
631bc487 34#include <asm/kvm_para.h>
6aa8b732 35
63d1142f 36#include <asm/virtext.h>
229456fc 37#include "trace.h"
63d1142f 38
4ecac3fd
AK
39#define __ex(x) __kvm_handle_fault_on_reboot(x)
40
6aa8b732
AK
41MODULE_AUTHOR("Qumranet");
42MODULE_LICENSE("GPL");
43
44#define IOPM_ALLOC_ORDER 2
45#define MSRPM_ALLOC_ORDER 1
46
6aa8b732
AK
47#define SEG_TYPE_LDT 2
48#define SEG_TYPE_BUSY_TSS16 3
49
6bc31bdc
AP
50#define SVM_FEATURE_NPT (1 << 0)
51#define SVM_FEATURE_LBRV (1 << 1)
52#define SVM_FEATURE_SVML (1 << 2)
53#define SVM_FEATURE_NRIP (1 << 3)
54#define SVM_FEATURE_PAUSE_FILTER (1 << 10)
80b7706e 55
410e4d57
JR
56#define NESTED_EXIT_HOST 0 /* Exit handled on host level */
57#define NESTED_EXIT_DONE 1 /* Exit caused nested vmexit */
58#define NESTED_EXIT_CONTINUE 2 /* Further checks needed */
59
24e09cbf
JR
60#define DEBUGCTL_RESERVED_BITS (~(0x3fULL))
61
67ec6607
JR
62static bool erratum_383_found __read_mostly;
63
6c8166a7
AK
64static const u32 host_save_user_msrs[] = {
65#ifdef CONFIG_X86_64
66 MSR_STAR, MSR_LSTAR, MSR_CSTAR, MSR_SYSCALL_MASK, MSR_KERNEL_GS_BASE,
67 MSR_FS_BASE,
68#endif
69 MSR_IA32_SYSENTER_CS, MSR_IA32_SYSENTER_ESP, MSR_IA32_SYSENTER_EIP,
70};
71
72#define NR_HOST_SAVE_USER_MSRS ARRAY_SIZE(host_save_user_msrs)
73
74struct kvm_vcpu;
75
e6aa9abd
JR
76struct nested_state {
77 struct vmcb *hsave;
78 u64 hsave_msr;
4a810181 79 u64 vm_cr_msr;
e6aa9abd
JR
80 u64 vmcb;
81
82 /* These are the merged vectors */
83 u32 *msrpm;
84
85 /* gpa pointers to the real vectors */
86 u64 vmcb_msrpm;
ce2ac085 87 u64 vmcb_iopm;
aad42c64 88
cd3ff653
JR
89 /* A VMEXIT is required but not yet emulated */
90 bool exit_required;
91
cda00082
JR
92 /*
93 * If we vmexit during an instruction emulation we need this to restore
94 * the l1 guest rip after the emulation
95 */
96 unsigned long vmexit_rip;
97 unsigned long vmexit_rsp;
98 unsigned long vmexit_rax;
99
aad42c64 100 /* cache for intercepts of the guest */
4ee546b4 101 u32 intercept_cr;
3aed041a 102 u32 intercept_dr;
aad42c64
JR
103 u32 intercept_exceptions;
104 u64 intercept;
105
5bd2edc3
JR
106 /* Nested Paging related state */
107 u64 nested_cr3;
e6aa9abd
JR
108};
109
323c3d80
JR
110#define MSRPM_OFFSETS 16
111static u32 msrpm_offsets[MSRPM_OFFSETS] __read_mostly;
112
6c8166a7
AK
113struct vcpu_svm {
114 struct kvm_vcpu vcpu;
115 struct vmcb *vmcb;
116 unsigned long vmcb_pa;
117 struct svm_cpu_data *svm_data;
118 uint64_t asid_generation;
119 uint64_t sysenter_esp;
120 uint64_t sysenter_eip;
121
122 u64 next_rip;
123
124 u64 host_user_msrs[NR_HOST_SAVE_USER_MSRS];
afe9e66f 125 struct {
dacccfdd
AK
126 u16 fs;
127 u16 gs;
128 u16 ldt;
afe9e66f
AK
129 u64 gs_base;
130 } host;
6c8166a7
AK
131
132 u32 *msrpm;
6c8166a7 133
e6aa9abd 134 struct nested_state nested;
6be7d306
JK
135
136 bool nmi_singlestep;
66b7138f
JK
137
138 unsigned int3_injected;
139 unsigned long int3_rip;
631bc487 140 u32 apf_reason;
6c8166a7
AK
141};
142
455716fa
JR
143#define MSR_INVALID 0xffffffffU
144
ac72a9b7
JR
145static struct svm_direct_access_msrs {
146 u32 index; /* Index of the MSR */
147 bool always; /* True if intercept is always on */
148} direct_access_msrs[] = {
8c06585d 149 { .index = MSR_STAR, .always = true },
ac72a9b7
JR
150 { .index = MSR_IA32_SYSENTER_CS, .always = true },
151#ifdef CONFIG_X86_64
152 { .index = MSR_GS_BASE, .always = true },
153 { .index = MSR_FS_BASE, .always = true },
154 { .index = MSR_KERNEL_GS_BASE, .always = true },
155 { .index = MSR_LSTAR, .always = true },
156 { .index = MSR_CSTAR, .always = true },
157 { .index = MSR_SYSCALL_MASK, .always = true },
158#endif
159 { .index = MSR_IA32_LASTBRANCHFROMIP, .always = false },
160 { .index = MSR_IA32_LASTBRANCHTOIP, .always = false },
161 { .index = MSR_IA32_LASTINTFROMIP, .always = false },
162 { .index = MSR_IA32_LASTINTTOIP, .always = false },
163 { .index = MSR_INVALID, .always = false },
6c8166a7
AK
164};
165
709ddebf
JR
166/* enable NPT for AMD64 and X86 with PAE */
167#if defined(CONFIG_X86_64) || defined(CONFIG_X86_PAE)
168static bool npt_enabled = true;
169#else
e0231715 170static bool npt_enabled;
709ddebf 171#endif
6c7dac72
JR
172static int npt = 1;
173
174module_param(npt, int, S_IRUGO);
e3da3acd 175
4b6e4dca 176static int nested = 1;
236de055
AG
177module_param(nested, int, S_IRUGO);
178
44874f84 179static void svm_flush_tlb(struct kvm_vcpu *vcpu);
a5c3832d 180static void svm_complete_interrupts(struct vcpu_svm *svm);
04d2cc77 181
410e4d57 182static int nested_svm_exit_handled(struct vcpu_svm *svm);
b8e88bc8 183static int nested_svm_intercept(struct vcpu_svm *svm);
cf74a78b 184static int nested_svm_vmexit(struct vcpu_svm *svm);
cf74a78b
AG
185static int nested_svm_check_exception(struct vcpu_svm *svm, unsigned nr,
186 bool has_error_code, u32 error_code);
187
8d28fec4 188enum {
116a0a23
JR
189 VMCB_INTERCEPTS, /* Intercept vectors, TSC offset,
190 pause filter count */
f56838e4 191 VMCB_PERM_MAP, /* IOPM Base and MSRPM Base */
d48086d1 192 VMCB_ASID, /* ASID */
decdbf6a 193 VMCB_INTR, /* int_ctl, int_vector */
b2747166 194 VMCB_NPT, /* npt_en, nCR3, gPAT */
dcca1a65 195 VMCB_CR, /* CR0, CR3, CR4, EFER */
72214b96 196 VMCB_DR, /* DR6, DR7 */
17a703cb 197 VMCB_DT, /* GDT, IDT */
060d0c9a 198 VMCB_SEG, /* CS, DS, SS, ES, CPL */
8d28fec4
RJ
199 VMCB_DIRTY_MAX,
200};
201
decdbf6a
JR
202/* TPR is always written before VMRUN */
203#define VMCB_ALWAYS_DIRTY_MASK (1U << VMCB_INTR)
8d28fec4
RJ
204
205static inline void mark_all_dirty(struct vmcb *vmcb)
206{
207 vmcb->control.clean = 0;
208}
209
210static inline void mark_all_clean(struct vmcb *vmcb)
211{
212 vmcb->control.clean = ((1 << VMCB_DIRTY_MAX) - 1)
213 & ~VMCB_ALWAYS_DIRTY_MASK;
214}
215
216static inline void mark_dirty(struct vmcb *vmcb, int bit)
217{
218 vmcb->control.clean &= ~(1 << bit);
219}
220
a2fa3e9f
GH
221static inline struct vcpu_svm *to_svm(struct kvm_vcpu *vcpu)
222{
fb3f0f51 223 return container_of(vcpu, struct vcpu_svm, vcpu);
a2fa3e9f
GH
224}
225
384c6368
JR
226static void recalc_intercepts(struct vcpu_svm *svm)
227{
228 struct vmcb_control_area *c, *h;
229 struct nested_state *g;
230
116a0a23
JR
231 mark_dirty(svm->vmcb, VMCB_INTERCEPTS);
232
384c6368
JR
233 if (!is_guest_mode(&svm->vcpu))
234 return;
235
236 c = &svm->vmcb->control;
237 h = &svm->nested.hsave->control;
238 g = &svm->nested;
239
4ee546b4 240 c->intercept_cr = h->intercept_cr | g->intercept_cr;
3aed041a 241 c->intercept_dr = h->intercept_dr | g->intercept_dr;
384c6368
JR
242 c->intercept_exceptions = h->intercept_exceptions | g->intercept_exceptions;
243 c->intercept = h->intercept | g->intercept;
244}
245
4ee546b4
RJ
246static inline struct vmcb *get_host_vmcb(struct vcpu_svm *svm)
247{
248 if (is_guest_mode(&svm->vcpu))
249 return svm->nested.hsave;
250 else
251 return svm->vmcb;
252}
253
254static inline void set_cr_intercept(struct vcpu_svm *svm, int bit)
255{
256 struct vmcb *vmcb = get_host_vmcb(svm);
257
258 vmcb->control.intercept_cr |= (1U << bit);
259
260 recalc_intercepts(svm);
261}
262
263static inline void clr_cr_intercept(struct vcpu_svm *svm, int bit)
264{
265 struct vmcb *vmcb = get_host_vmcb(svm);
266
267 vmcb->control.intercept_cr &= ~(1U << bit);
268
269 recalc_intercepts(svm);
270}
271
272static inline bool is_cr_intercept(struct vcpu_svm *svm, int bit)
273{
274 struct vmcb *vmcb = get_host_vmcb(svm);
275
276 return vmcb->control.intercept_cr & (1U << bit);
277}
278
3aed041a
JR
279static inline void set_dr_intercept(struct vcpu_svm *svm, int bit)
280{
281 struct vmcb *vmcb = get_host_vmcb(svm);
282
283 vmcb->control.intercept_dr |= (1U << bit);
284
285 recalc_intercepts(svm);
286}
287
288static inline void clr_dr_intercept(struct vcpu_svm *svm, int bit)
289{
290 struct vmcb *vmcb = get_host_vmcb(svm);
291
292 vmcb->control.intercept_dr &= ~(1U << bit);
293
294 recalc_intercepts(svm);
295}
296
18c918c5
JR
297static inline void set_exception_intercept(struct vcpu_svm *svm, int bit)
298{
299 struct vmcb *vmcb = get_host_vmcb(svm);
300
301 vmcb->control.intercept_exceptions |= (1U << bit);
302
303 recalc_intercepts(svm);
304}
305
306static inline void clr_exception_intercept(struct vcpu_svm *svm, int bit)
307{
308 struct vmcb *vmcb = get_host_vmcb(svm);
309
310 vmcb->control.intercept_exceptions &= ~(1U << bit);
311
312 recalc_intercepts(svm);
313}
314
8a05a1b8
JR
315static inline void set_intercept(struct vcpu_svm *svm, int bit)
316{
317 struct vmcb *vmcb = get_host_vmcb(svm);
318
319 vmcb->control.intercept |= (1ULL << bit);
320
321 recalc_intercepts(svm);
322}
323
324static inline void clr_intercept(struct vcpu_svm *svm, int bit)
325{
326 struct vmcb *vmcb = get_host_vmcb(svm);
327
328 vmcb->control.intercept &= ~(1ULL << bit);
329
330 recalc_intercepts(svm);
331}
332
2af9194d
JR
333static inline void enable_gif(struct vcpu_svm *svm)
334{
335 svm->vcpu.arch.hflags |= HF_GIF_MASK;
336}
337
338static inline void disable_gif(struct vcpu_svm *svm)
339{
340 svm->vcpu.arch.hflags &= ~HF_GIF_MASK;
341}
342
343static inline bool gif_set(struct vcpu_svm *svm)
344{
345 return !!(svm->vcpu.arch.hflags & HF_GIF_MASK);
346}
347
4866d5e3 348static unsigned long iopm_base;
6aa8b732
AK
349
350struct kvm_ldttss_desc {
351 u16 limit0;
352 u16 base0;
e0231715
JR
353 unsigned base1:8, type:5, dpl:2, p:1;
354 unsigned limit1:4, zero0:3, g:1, base2:8;
6aa8b732
AK
355 u32 base3;
356 u32 zero1;
357} __attribute__((packed));
358
359struct svm_cpu_data {
360 int cpu;
361
5008fdf5
AK
362 u64 asid_generation;
363 u32 max_asid;
364 u32 next_asid;
6aa8b732
AK
365 struct kvm_ldttss_desc *tss_desc;
366
367 struct page *save_area;
368};
369
370static DEFINE_PER_CPU(struct svm_cpu_data *, svm_data);
80b7706e 371static uint32_t svm_features;
6aa8b732
AK
372
373struct svm_init_data {
374 int cpu;
375 int r;
376};
377
378static u32 msrpm_ranges[] = {0, 0xc0000000, 0xc0010000};
379
9d8f549d 380#define NUM_MSR_MAPS ARRAY_SIZE(msrpm_ranges)
6aa8b732
AK
381#define MSRS_RANGE_SIZE 2048
382#define MSRS_IN_RANGE (MSRS_RANGE_SIZE * 8 / 2)
383
455716fa
JR
384static u32 svm_msrpm_offset(u32 msr)
385{
386 u32 offset;
387 int i;
388
389 for (i = 0; i < NUM_MSR_MAPS; i++) {
390 if (msr < msrpm_ranges[i] ||
391 msr >= msrpm_ranges[i] + MSRS_IN_RANGE)
392 continue;
393
394 offset = (msr - msrpm_ranges[i]) / 4; /* 4 msrs per u8 */
395 offset += (i * MSRS_RANGE_SIZE); /* add range offset */
396
397 /* Now we have the u8 offset - but need the u32 offset */
398 return offset / 4;
399 }
400
401 /* MSR not in any range */
402 return MSR_INVALID;
403}
404
6aa8b732
AK
405#define MAX_INST_SIZE 15
406
6aa8b732
AK
407static inline void clgi(void)
408{
4ecac3fd 409 asm volatile (__ex(SVM_CLGI));
6aa8b732
AK
410}
411
412static inline void stgi(void)
413{
4ecac3fd 414 asm volatile (__ex(SVM_STGI));
6aa8b732
AK
415}
416
417static inline void invlpga(unsigned long addr, u32 asid)
418{
e0231715 419 asm volatile (__ex(SVM_INVLPGA) : : "a"(addr), "c"(asid));
6aa8b732
AK
420}
421
6aa8b732
AK
422static inline void force_new_asid(struct kvm_vcpu *vcpu)
423{
a2fa3e9f 424 to_svm(vcpu)->asid_generation--;
6aa8b732
AK
425}
426
427static inline void flush_guest_tlb(struct kvm_vcpu *vcpu)
428{
429 force_new_asid(vcpu);
430}
431
4b16184c
JR
432static int get_npt_level(void)
433{
434#ifdef CONFIG_X86_64
435 return PT64_ROOT_LEVEL;
436#else
437 return PT32E_ROOT_LEVEL;
438#endif
439}
440
6aa8b732
AK
441static void svm_set_efer(struct kvm_vcpu *vcpu, u64 efer)
442{
6dc696d4 443 vcpu->arch.efer = efer;
709ddebf 444 if (!npt_enabled && !(efer & EFER_LMA))
2b5203ee 445 efer &= ~EFER_LME;
6aa8b732 446
9962d032 447 to_svm(vcpu)->vmcb->save.efer = efer | EFER_SVME;
dcca1a65 448 mark_dirty(to_svm(vcpu)->vmcb, VMCB_CR);
6aa8b732
AK
449}
450
6aa8b732
AK
451static int is_external_interrupt(u32 info)
452{
453 info &= SVM_EVTINJ_TYPE_MASK | SVM_EVTINJ_VALID;
454 return info == (SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_INTR);
455}
456
2809f5d2
GC
457static u32 svm_get_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
458{
459 struct vcpu_svm *svm = to_svm(vcpu);
460 u32 ret = 0;
461
462 if (svm->vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK)
48005f64 463 ret |= KVM_X86_SHADOW_INT_STI | KVM_X86_SHADOW_INT_MOV_SS;
2809f5d2
GC
464 return ret & mask;
465}
466
467static void svm_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
468{
469 struct vcpu_svm *svm = to_svm(vcpu);
470
471 if (mask == 0)
472 svm->vmcb->control.int_state &= ~SVM_INTERRUPT_SHADOW_MASK;
473 else
474 svm->vmcb->control.int_state |= SVM_INTERRUPT_SHADOW_MASK;
475
476}
477
6aa8b732
AK
478static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
479{
a2fa3e9f
GH
480 struct vcpu_svm *svm = to_svm(vcpu);
481
6bc31bdc
AP
482 if (svm->vmcb->control.next_rip != 0)
483 svm->next_rip = svm->vmcb->control.next_rip;
484
a2fa3e9f 485 if (!svm->next_rip) {
851ba692 486 if (emulate_instruction(vcpu, 0, 0, EMULTYPE_SKIP) !=
f629cf84
GN
487 EMULATE_DONE)
488 printk(KERN_DEBUG "%s: NOP\n", __func__);
6aa8b732
AK
489 return;
490 }
5fdbf976
MT
491 if (svm->next_rip - kvm_rip_read(vcpu) > MAX_INST_SIZE)
492 printk(KERN_ERR "%s: ip 0x%lx next 0x%llx\n",
493 __func__, kvm_rip_read(vcpu), svm->next_rip);
6aa8b732 494
5fdbf976 495 kvm_rip_write(vcpu, svm->next_rip);
2809f5d2 496 svm_set_interrupt_shadow(vcpu, 0);
6aa8b732
AK
497}
498
116a4752 499static void svm_queue_exception(struct kvm_vcpu *vcpu, unsigned nr,
ce7ddec4
JR
500 bool has_error_code, u32 error_code,
501 bool reinject)
116a4752
JK
502{
503 struct vcpu_svm *svm = to_svm(vcpu);
504
e0231715
JR
505 /*
506 * If we are within a nested VM we'd better #VMEXIT and let the guest
507 * handle the exception
508 */
ce7ddec4
JR
509 if (!reinject &&
510 nested_svm_check_exception(svm, nr, has_error_code, error_code))
116a4752
JK
511 return;
512
2a6b20b8 513 if (nr == BP_VECTOR && !static_cpu_has(X86_FEATURE_NRIPS)) {
66b7138f
JK
514 unsigned long rip, old_rip = kvm_rip_read(&svm->vcpu);
515
516 /*
517 * For guest debugging where we have to reinject #BP if some
518 * INT3 is guest-owned:
519 * Emulate nRIP by moving RIP forward. Will fail if injection
520 * raises a fault that is not intercepted. Still better than
521 * failing in all cases.
522 */
523 skip_emulated_instruction(&svm->vcpu);
524 rip = kvm_rip_read(&svm->vcpu);
525 svm->int3_rip = rip + svm->vmcb->save.cs.base;
526 svm->int3_injected = rip - old_rip;
527 }
528
116a4752
JK
529 svm->vmcb->control.event_inj = nr
530 | SVM_EVTINJ_VALID
531 | (has_error_code ? SVM_EVTINJ_VALID_ERR : 0)
532 | SVM_EVTINJ_TYPE_EXEPT;
533 svm->vmcb->control.event_inj_err = error_code;
534}
535
67ec6607
JR
536static void svm_init_erratum_383(void)
537{
538 u32 low, high;
539 int err;
540 u64 val;
541
1be85a6d 542 if (!cpu_has_amd_erratum(amd_erratum_383))
67ec6607
JR
543 return;
544
545 /* Use _safe variants to not break nested virtualization */
546 val = native_read_msr_safe(MSR_AMD64_DC_CFG, &err);
547 if (err)
548 return;
549
550 val |= (1ULL << 47);
551
552 low = lower_32_bits(val);
553 high = upper_32_bits(val);
554
555 native_write_msr_safe(MSR_AMD64_DC_CFG, low, high);
556
557 erratum_383_found = true;
558}
559
6aa8b732
AK
560static int has_svm(void)
561{
63d1142f 562 const char *msg;
6aa8b732 563
63d1142f 564 if (!cpu_has_svm(&msg)) {
ff81ff10 565 printk(KERN_INFO "has_svm: %s\n", msg);
6aa8b732
AK
566 return 0;
567 }
568
6aa8b732
AK
569 return 1;
570}
571
572static void svm_hardware_disable(void *garbage)
573{
2c8dceeb 574 cpu_svm_disable();
6aa8b732
AK
575}
576
10474ae8 577static int svm_hardware_enable(void *garbage)
6aa8b732
AK
578{
579
0fe1e009 580 struct svm_cpu_data *sd;
6aa8b732 581 uint64_t efer;
89a27f4d 582 struct desc_ptr gdt_descr;
6aa8b732
AK
583 struct desc_struct *gdt;
584 int me = raw_smp_processor_id();
585
10474ae8
AG
586 rdmsrl(MSR_EFER, efer);
587 if (efer & EFER_SVME)
588 return -EBUSY;
589
6aa8b732 590 if (!has_svm()) {
e6732a5a
ZA
591 printk(KERN_ERR "svm_hardware_enable: err EOPNOTSUPP on %d\n",
592 me);
10474ae8 593 return -EINVAL;
6aa8b732 594 }
0fe1e009 595 sd = per_cpu(svm_data, me);
6aa8b732 596
0fe1e009 597 if (!sd) {
e6732a5a 598 printk(KERN_ERR "svm_hardware_enable: svm_data is NULL on %d\n",
6aa8b732 599 me);
10474ae8 600 return -EINVAL;
6aa8b732
AK
601 }
602
0fe1e009
TH
603 sd->asid_generation = 1;
604 sd->max_asid = cpuid_ebx(SVM_CPUID_FUNC) - 1;
605 sd->next_asid = sd->max_asid + 1;
6aa8b732 606
d6ab1ed4 607 native_store_gdt(&gdt_descr);
89a27f4d 608 gdt = (struct desc_struct *)gdt_descr.address;
0fe1e009 609 sd->tss_desc = (struct kvm_ldttss_desc *)(gdt + GDT_ENTRY_TSS);
6aa8b732 610
9962d032 611 wrmsrl(MSR_EFER, efer | EFER_SVME);
6aa8b732 612
d0316554 613 wrmsrl(MSR_VM_HSAVE_PA, page_to_pfn(sd->save_area) << PAGE_SHIFT);
10474ae8 614
67ec6607
JR
615 svm_init_erratum_383();
616
10474ae8 617 return 0;
6aa8b732
AK
618}
619
0da1db75
JR
620static void svm_cpu_uninit(int cpu)
621{
0fe1e009 622 struct svm_cpu_data *sd = per_cpu(svm_data, raw_smp_processor_id());
0da1db75 623
0fe1e009 624 if (!sd)
0da1db75
JR
625 return;
626
627 per_cpu(svm_data, raw_smp_processor_id()) = NULL;
0fe1e009
TH
628 __free_page(sd->save_area);
629 kfree(sd);
0da1db75
JR
630}
631
6aa8b732
AK
632static int svm_cpu_init(int cpu)
633{
0fe1e009 634 struct svm_cpu_data *sd;
6aa8b732
AK
635 int r;
636
0fe1e009
TH
637 sd = kzalloc(sizeof(struct svm_cpu_data), GFP_KERNEL);
638 if (!sd)
6aa8b732 639 return -ENOMEM;
0fe1e009
TH
640 sd->cpu = cpu;
641 sd->save_area = alloc_page(GFP_KERNEL);
6aa8b732 642 r = -ENOMEM;
0fe1e009 643 if (!sd->save_area)
6aa8b732
AK
644 goto err_1;
645
0fe1e009 646 per_cpu(svm_data, cpu) = sd;
6aa8b732
AK
647
648 return 0;
649
650err_1:
0fe1e009 651 kfree(sd);
6aa8b732
AK
652 return r;
653
654}
655
ac72a9b7
JR
656static bool valid_msr_intercept(u32 index)
657{
658 int i;
659
660 for (i = 0; direct_access_msrs[i].index != MSR_INVALID; i++)
661 if (direct_access_msrs[i].index == index)
662 return true;
663
664 return false;
665}
666
bfc733a7
RR
667static void set_msr_interception(u32 *msrpm, unsigned msr,
668 int read, int write)
6aa8b732 669{
455716fa
JR
670 u8 bit_read, bit_write;
671 unsigned long tmp;
672 u32 offset;
6aa8b732 673
ac72a9b7
JR
674 /*
675 * If this warning triggers extend the direct_access_msrs list at the
676 * beginning of the file
677 */
678 WARN_ON(!valid_msr_intercept(msr));
679
455716fa
JR
680 offset = svm_msrpm_offset(msr);
681 bit_read = 2 * (msr & 0x0f);
682 bit_write = 2 * (msr & 0x0f) + 1;
683 tmp = msrpm[offset];
684
685 BUG_ON(offset == MSR_INVALID);
686
687 read ? clear_bit(bit_read, &tmp) : set_bit(bit_read, &tmp);
688 write ? clear_bit(bit_write, &tmp) : set_bit(bit_write, &tmp);
689
690 msrpm[offset] = tmp;
6aa8b732
AK
691}
692
f65c229c 693static void svm_vcpu_init_msrpm(u32 *msrpm)
6aa8b732
AK
694{
695 int i;
696
f65c229c
JR
697 memset(msrpm, 0xff, PAGE_SIZE * (1 << MSRPM_ALLOC_ORDER));
698
ac72a9b7
JR
699 for (i = 0; direct_access_msrs[i].index != MSR_INVALID; i++) {
700 if (!direct_access_msrs[i].always)
701 continue;
702
703 set_msr_interception(msrpm, direct_access_msrs[i].index, 1, 1);
704 }
f65c229c
JR
705}
706
323c3d80
JR
707static void add_msr_offset(u32 offset)
708{
709 int i;
710
711 for (i = 0; i < MSRPM_OFFSETS; ++i) {
712
713 /* Offset already in list? */
714 if (msrpm_offsets[i] == offset)
bfc733a7 715 return;
323c3d80
JR
716
717 /* Slot used by another offset? */
718 if (msrpm_offsets[i] != MSR_INVALID)
719 continue;
720
721 /* Add offset to list */
722 msrpm_offsets[i] = offset;
723
724 return;
6aa8b732 725 }
323c3d80
JR
726
727 /*
728 * If this BUG triggers the msrpm_offsets table has an overflow. Just
729 * increase MSRPM_OFFSETS in this case.
730 */
bfc733a7 731 BUG();
6aa8b732
AK
732}
733
323c3d80 734static void init_msrpm_offsets(void)
f65c229c 735{
323c3d80 736 int i;
f65c229c 737
323c3d80
JR
738 memset(msrpm_offsets, 0xff, sizeof(msrpm_offsets));
739
740 for (i = 0; direct_access_msrs[i].index != MSR_INVALID; i++) {
741 u32 offset;
742
743 offset = svm_msrpm_offset(direct_access_msrs[i].index);
744 BUG_ON(offset == MSR_INVALID);
745
746 add_msr_offset(offset);
747 }
f65c229c
JR
748}
749
24e09cbf
JR
750static void svm_enable_lbrv(struct vcpu_svm *svm)
751{
752 u32 *msrpm = svm->msrpm;
753
754 svm->vmcb->control.lbr_ctl = 1;
755 set_msr_interception(msrpm, MSR_IA32_LASTBRANCHFROMIP, 1, 1);
756 set_msr_interception(msrpm, MSR_IA32_LASTBRANCHTOIP, 1, 1);
757 set_msr_interception(msrpm, MSR_IA32_LASTINTFROMIP, 1, 1);
758 set_msr_interception(msrpm, MSR_IA32_LASTINTTOIP, 1, 1);
759}
760
761static void svm_disable_lbrv(struct vcpu_svm *svm)
762{
763 u32 *msrpm = svm->msrpm;
764
765 svm->vmcb->control.lbr_ctl = 0;
766 set_msr_interception(msrpm, MSR_IA32_LASTBRANCHFROMIP, 0, 0);
767 set_msr_interception(msrpm, MSR_IA32_LASTBRANCHTOIP, 0, 0);
768 set_msr_interception(msrpm, MSR_IA32_LASTINTFROMIP, 0, 0);
769 set_msr_interception(msrpm, MSR_IA32_LASTINTTOIP, 0, 0);
770}
771
6aa8b732
AK
772static __init int svm_hardware_setup(void)
773{
774 int cpu;
775 struct page *iopm_pages;
f65c229c 776 void *iopm_va;
6aa8b732
AK
777 int r;
778
6aa8b732
AK
779 iopm_pages = alloc_pages(GFP_KERNEL, IOPM_ALLOC_ORDER);
780
781 if (!iopm_pages)
782 return -ENOMEM;
c8681339
AL
783
784 iopm_va = page_address(iopm_pages);
785 memset(iopm_va, 0xff, PAGE_SIZE * (1 << IOPM_ALLOC_ORDER));
6aa8b732
AK
786 iopm_base = page_to_pfn(iopm_pages) << PAGE_SHIFT;
787
323c3d80
JR
788 init_msrpm_offsets();
789
50a37eb4
JR
790 if (boot_cpu_has(X86_FEATURE_NX))
791 kvm_enable_efer_bits(EFER_NX);
792
1b2fd70c
AG
793 if (boot_cpu_has(X86_FEATURE_FXSR_OPT))
794 kvm_enable_efer_bits(EFER_FFXSR);
795
236de055
AG
796 if (nested) {
797 printk(KERN_INFO "kvm: Nested Virtualization enabled\n");
eec4b140 798 kvm_enable_efer_bits(EFER_SVME | EFER_LMSLE);
236de055
AG
799 }
800
3230bb47 801 for_each_possible_cpu(cpu) {
6aa8b732
AK
802 r = svm_cpu_init(cpu);
803 if (r)
f65c229c 804 goto err;
6aa8b732 805 }
33bd6a0b
JR
806
807 svm_features = cpuid_edx(SVM_CPUID_FUNC);
808
2a6b20b8 809 if (!boot_cpu_has(X86_FEATURE_NPT))
e3da3acd
JR
810 npt_enabled = false;
811
6c7dac72
JR
812 if (npt_enabled && !npt) {
813 printk(KERN_INFO "kvm: Nested Paging disabled\n");
814 npt_enabled = false;
815 }
816
18552672 817 if (npt_enabled) {
e3da3acd 818 printk(KERN_INFO "kvm: Nested Paging enabled\n");
18552672 819 kvm_enable_tdp();
5f4cb662
JR
820 } else
821 kvm_disable_tdp();
e3da3acd 822
6aa8b732
AK
823 return 0;
824
f65c229c 825err:
6aa8b732
AK
826 __free_pages(iopm_pages, IOPM_ALLOC_ORDER);
827 iopm_base = 0;
828 return r;
829}
830
831static __exit void svm_hardware_unsetup(void)
832{
0da1db75
JR
833 int cpu;
834
3230bb47 835 for_each_possible_cpu(cpu)
0da1db75
JR
836 svm_cpu_uninit(cpu);
837
6aa8b732 838 __free_pages(pfn_to_page(iopm_base >> PAGE_SHIFT), IOPM_ALLOC_ORDER);
f65c229c 839 iopm_base = 0;
6aa8b732
AK
840}
841
842static void init_seg(struct vmcb_seg *seg)
843{
844 seg->selector = 0;
845 seg->attrib = SVM_SELECTOR_P_MASK | SVM_SELECTOR_S_MASK |
e0231715 846 SVM_SELECTOR_WRITE_MASK; /* Read/Write Data Segment */
6aa8b732
AK
847 seg->limit = 0xffff;
848 seg->base = 0;
849}
850
851static void init_sys_seg(struct vmcb_seg *seg, uint32_t type)
852{
853 seg->selector = 0;
854 seg->attrib = SVM_SELECTOR_P_MASK | type;
855 seg->limit = 0xffff;
856 seg->base = 0;
857}
858
f4e1b3c8
ZA
859static void svm_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset)
860{
861 struct vcpu_svm *svm = to_svm(vcpu);
862 u64 g_tsc_offset = 0;
863
2030753d 864 if (is_guest_mode(vcpu)) {
f4e1b3c8
ZA
865 g_tsc_offset = svm->vmcb->control.tsc_offset -
866 svm->nested.hsave->control.tsc_offset;
867 svm->nested.hsave->control.tsc_offset = offset;
868 }
869
870 svm->vmcb->control.tsc_offset = offset + g_tsc_offset;
116a0a23
JR
871
872 mark_dirty(svm->vmcb, VMCB_INTERCEPTS);
f4e1b3c8
ZA
873}
874
e48672fa
ZA
875static void svm_adjust_tsc_offset(struct kvm_vcpu *vcpu, s64 adjustment)
876{
877 struct vcpu_svm *svm = to_svm(vcpu);
878
879 svm->vmcb->control.tsc_offset += adjustment;
2030753d 880 if (is_guest_mode(vcpu))
e48672fa 881 svm->nested.hsave->control.tsc_offset += adjustment;
116a0a23 882 mark_dirty(svm->vmcb, VMCB_INTERCEPTS);
e48672fa
ZA
883}
884
e6101a96 885static void init_vmcb(struct vcpu_svm *svm)
6aa8b732 886{
e6101a96
JR
887 struct vmcb_control_area *control = &svm->vmcb->control;
888 struct vmcb_save_area *save = &svm->vmcb->save;
6aa8b732 889
bff78274 890 svm->vcpu.fpu_active = 1;
4ee546b4 891 svm->vcpu.arch.hflags = 0;
bff78274 892
4ee546b4
RJ
893 set_cr_intercept(svm, INTERCEPT_CR0_READ);
894 set_cr_intercept(svm, INTERCEPT_CR3_READ);
895 set_cr_intercept(svm, INTERCEPT_CR4_READ);
896 set_cr_intercept(svm, INTERCEPT_CR0_WRITE);
897 set_cr_intercept(svm, INTERCEPT_CR3_WRITE);
898 set_cr_intercept(svm, INTERCEPT_CR4_WRITE);
899 set_cr_intercept(svm, INTERCEPT_CR8_WRITE);
6aa8b732 900
3aed041a
JR
901 set_dr_intercept(svm, INTERCEPT_DR0_READ);
902 set_dr_intercept(svm, INTERCEPT_DR1_READ);
903 set_dr_intercept(svm, INTERCEPT_DR2_READ);
904 set_dr_intercept(svm, INTERCEPT_DR3_READ);
905 set_dr_intercept(svm, INTERCEPT_DR4_READ);
906 set_dr_intercept(svm, INTERCEPT_DR5_READ);
907 set_dr_intercept(svm, INTERCEPT_DR6_READ);
908 set_dr_intercept(svm, INTERCEPT_DR7_READ);
909
910 set_dr_intercept(svm, INTERCEPT_DR0_WRITE);
911 set_dr_intercept(svm, INTERCEPT_DR1_WRITE);
912 set_dr_intercept(svm, INTERCEPT_DR2_WRITE);
913 set_dr_intercept(svm, INTERCEPT_DR3_WRITE);
914 set_dr_intercept(svm, INTERCEPT_DR4_WRITE);
915 set_dr_intercept(svm, INTERCEPT_DR5_WRITE);
916 set_dr_intercept(svm, INTERCEPT_DR6_WRITE);
917 set_dr_intercept(svm, INTERCEPT_DR7_WRITE);
6aa8b732 918
18c918c5
JR
919 set_exception_intercept(svm, PF_VECTOR);
920 set_exception_intercept(svm, UD_VECTOR);
921 set_exception_intercept(svm, MC_VECTOR);
6aa8b732 922
8a05a1b8
JR
923 set_intercept(svm, INTERCEPT_INTR);
924 set_intercept(svm, INTERCEPT_NMI);
925 set_intercept(svm, INTERCEPT_SMI);
926 set_intercept(svm, INTERCEPT_SELECTIVE_CR0);
927 set_intercept(svm, INTERCEPT_CPUID);
928 set_intercept(svm, INTERCEPT_INVD);
929 set_intercept(svm, INTERCEPT_HLT);
930 set_intercept(svm, INTERCEPT_INVLPG);
931 set_intercept(svm, INTERCEPT_INVLPGA);
932 set_intercept(svm, INTERCEPT_IOIO_PROT);
933 set_intercept(svm, INTERCEPT_MSR_PROT);
934 set_intercept(svm, INTERCEPT_TASK_SWITCH);
935 set_intercept(svm, INTERCEPT_SHUTDOWN);
936 set_intercept(svm, INTERCEPT_VMRUN);
937 set_intercept(svm, INTERCEPT_VMMCALL);
938 set_intercept(svm, INTERCEPT_VMLOAD);
939 set_intercept(svm, INTERCEPT_VMSAVE);
940 set_intercept(svm, INTERCEPT_STGI);
941 set_intercept(svm, INTERCEPT_CLGI);
942 set_intercept(svm, INTERCEPT_SKINIT);
943 set_intercept(svm, INTERCEPT_WBINVD);
944 set_intercept(svm, INTERCEPT_MONITOR);
945 set_intercept(svm, INTERCEPT_MWAIT);
6aa8b732
AK
946
947 control->iopm_base_pa = iopm_base;
f65c229c 948 control->msrpm_base_pa = __pa(svm->msrpm);
6aa8b732
AK
949 control->int_ctl = V_INTR_MASKING_MASK;
950
951 init_seg(&save->es);
952 init_seg(&save->ss);
953 init_seg(&save->ds);
954 init_seg(&save->fs);
955 init_seg(&save->gs);
956
957 save->cs.selector = 0xf000;
958 /* Executable/Readable Code Segment */
959 save->cs.attrib = SVM_SELECTOR_READ_MASK | SVM_SELECTOR_P_MASK |
960 SVM_SELECTOR_S_MASK | SVM_SELECTOR_CODE_MASK;
961 save->cs.limit = 0xffff;
d92899a0
AK
962 /*
963 * cs.base should really be 0xffff0000, but vmx can't handle that, so
964 * be consistent with it.
965 *
966 * Replace when we have real mode working for vmx.
967 */
968 save->cs.base = 0xf0000;
6aa8b732
AK
969
970 save->gdtr.limit = 0xffff;
971 save->idtr.limit = 0xffff;
972
973 init_sys_seg(&save->ldtr, SEG_TYPE_LDT);
974 init_sys_seg(&save->tr, SEG_TYPE_BUSY_TSS16);
975
eaa48512 976 svm_set_efer(&svm->vcpu, 0);
d77c26fc 977 save->dr6 = 0xffff0ff0;
6aa8b732
AK
978 save->dr7 = 0x400;
979 save->rflags = 2;
980 save->rip = 0x0000fff0;
5fdbf976 981 svm->vcpu.arch.regs[VCPU_REGS_RIP] = save->rip;
6aa8b732 982
e0231715
JR
983 /*
984 * This is the guest-visible cr0 value.
18fa000a 985 * svm_set_cr0() sets PG and WP and clears NW and CD on save->cr0.
6aa8b732 986 */
678041ad
MT
987 svm->vcpu.arch.cr0 = 0;
988 (void)kvm_set_cr0(&svm->vcpu, X86_CR0_NW | X86_CR0_CD | X86_CR0_ET);
18fa000a 989
66aee91a 990 save->cr4 = X86_CR4_PAE;
6aa8b732 991 /* rdx = ?? */
709ddebf
JR
992
993 if (npt_enabled) {
994 /* Setup VMCB for Nested Paging */
995 control->nested_ctl = 1;
8a05a1b8
JR
996 clr_intercept(svm, INTERCEPT_TASK_SWITCH);
997 clr_intercept(svm, INTERCEPT_INVLPG);
18c918c5 998 clr_exception_intercept(svm, PF_VECTOR);
4ee546b4
RJ
999 clr_cr_intercept(svm, INTERCEPT_CR3_READ);
1000 clr_cr_intercept(svm, INTERCEPT_CR3_WRITE);
709ddebf 1001 save->g_pat = 0x0007040600070406ULL;
709ddebf
JR
1002 save->cr3 = 0;
1003 save->cr4 = 0;
1004 }
a79d2f18 1005 force_new_asid(&svm->vcpu);
1371d904 1006
e6aa9abd 1007 svm->nested.vmcb = 0;
2af9194d
JR
1008 svm->vcpu.arch.hflags = 0;
1009
2a6b20b8 1010 if (boot_cpu_has(X86_FEATURE_PAUSEFILTER)) {
565d0998 1011 control->pause_filter_count = 3000;
8a05a1b8 1012 set_intercept(svm, INTERCEPT_PAUSE);
565d0998
ML
1013 }
1014
8d28fec4
RJ
1015 mark_all_dirty(svm->vmcb);
1016
2af9194d 1017 enable_gif(svm);
6aa8b732
AK
1018}
1019
e00c8cf2 1020static int svm_vcpu_reset(struct kvm_vcpu *vcpu)
04d2cc77
AK
1021{
1022 struct vcpu_svm *svm = to_svm(vcpu);
1023
e6101a96 1024 init_vmcb(svm);
70433389 1025
c5af89b6 1026 if (!kvm_vcpu_is_bsp(vcpu)) {
5fdbf976 1027 kvm_rip_write(vcpu, 0);
ad312c7c
ZX
1028 svm->vmcb->save.cs.base = svm->vcpu.arch.sipi_vector << 12;
1029 svm->vmcb->save.cs.selector = svm->vcpu.arch.sipi_vector << 8;
70433389 1030 }
5fdbf976
MT
1031 vcpu->arch.regs_avail = ~0;
1032 vcpu->arch.regs_dirty = ~0;
e00c8cf2
AK
1033
1034 return 0;
04d2cc77
AK
1035}
1036
fb3f0f51 1037static struct kvm_vcpu *svm_create_vcpu(struct kvm *kvm, unsigned int id)
6aa8b732 1038{
a2fa3e9f 1039 struct vcpu_svm *svm;
6aa8b732 1040 struct page *page;
f65c229c 1041 struct page *msrpm_pages;
b286d5d8 1042 struct page *hsave_page;
3d6368ef 1043 struct page *nested_msrpm_pages;
fb3f0f51 1044 int err;
6aa8b732 1045
c16f862d 1046 svm = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
fb3f0f51
RR
1047 if (!svm) {
1048 err = -ENOMEM;
1049 goto out;
1050 }
1051
1052 err = kvm_vcpu_init(&svm->vcpu, kvm, id);
1053 if (err)
1054 goto free_svm;
1055
b7af4043 1056 err = -ENOMEM;
6aa8b732 1057 page = alloc_page(GFP_KERNEL);
b7af4043 1058 if (!page)
fb3f0f51 1059 goto uninit;
6aa8b732 1060
f65c229c
JR
1061 msrpm_pages = alloc_pages(GFP_KERNEL, MSRPM_ALLOC_ORDER);
1062 if (!msrpm_pages)
b7af4043 1063 goto free_page1;
3d6368ef
AG
1064
1065 nested_msrpm_pages = alloc_pages(GFP_KERNEL, MSRPM_ALLOC_ORDER);
1066 if (!nested_msrpm_pages)
b7af4043 1067 goto free_page2;
f65c229c 1068
b286d5d8
AG
1069 hsave_page = alloc_page(GFP_KERNEL);
1070 if (!hsave_page)
b7af4043
TY
1071 goto free_page3;
1072
e6aa9abd 1073 svm->nested.hsave = page_address(hsave_page);
b286d5d8 1074
b7af4043
TY
1075 svm->msrpm = page_address(msrpm_pages);
1076 svm_vcpu_init_msrpm(svm->msrpm);
1077
e6aa9abd 1078 svm->nested.msrpm = page_address(nested_msrpm_pages);
323c3d80 1079 svm_vcpu_init_msrpm(svm->nested.msrpm);
3d6368ef 1080
a2fa3e9f
GH
1081 svm->vmcb = page_address(page);
1082 clear_page(svm->vmcb);
1083 svm->vmcb_pa = page_to_pfn(page) << PAGE_SHIFT;
1084 svm->asid_generation = 0;
e6101a96 1085 init_vmcb(svm);
99e3e30a 1086 kvm_write_tsc(&svm->vcpu, 0);
a2fa3e9f 1087
10ab25cd
JK
1088 err = fx_init(&svm->vcpu);
1089 if (err)
1090 goto free_page4;
1091
ad312c7c 1092 svm->vcpu.arch.apic_base = 0xfee00000 | MSR_IA32_APICBASE_ENABLE;
c5af89b6 1093 if (kvm_vcpu_is_bsp(&svm->vcpu))
ad312c7c 1094 svm->vcpu.arch.apic_base |= MSR_IA32_APICBASE_BSP;
6aa8b732 1095
fb3f0f51 1096 return &svm->vcpu;
36241b8c 1097
10ab25cd
JK
1098free_page4:
1099 __free_page(hsave_page);
b7af4043
TY
1100free_page3:
1101 __free_pages(nested_msrpm_pages, MSRPM_ALLOC_ORDER);
1102free_page2:
1103 __free_pages(msrpm_pages, MSRPM_ALLOC_ORDER);
1104free_page1:
1105 __free_page(page);
fb3f0f51
RR
1106uninit:
1107 kvm_vcpu_uninit(&svm->vcpu);
1108free_svm:
a4770347 1109 kmem_cache_free(kvm_vcpu_cache, svm);
fb3f0f51
RR
1110out:
1111 return ERR_PTR(err);
6aa8b732
AK
1112}
1113
1114static void svm_free_vcpu(struct kvm_vcpu *vcpu)
1115{
a2fa3e9f
GH
1116 struct vcpu_svm *svm = to_svm(vcpu);
1117
fb3f0f51 1118 __free_page(pfn_to_page(svm->vmcb_pa >> PAGE_SHIFT));
f65c229c 1119 __free_pages(virt_to_page(svm->msrpm), MSRPM_ALLOC_ORDER);
e6aa9abd
JR
1120 __free_page(virt_to_page(svm->nested.hsave));
1121 __free_pages(virt_to_page(svm->nested.msrpm), MSRPM_ALLOC_ORDER);
fb3f0f51 1122 kvm_vcpu_uninit(vcpu);
a4770347 1123 kmem_cache_free(kvm_vcpu_cache, svm);
6aa8b732
AK
1124}
1125
15ad7146 1126static void svm_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
6aa8b732 1127{
a2fa3e9f 1128 struct vcpu_svm *svm = to_svm(vcpu);
15ad7146 1129 int i;
0cc5064d 1130
0cc5064d 1131 if (unlikely(cpu != vcpu->cpu)) {
4b656b12 1132 svm->asid_generation = 0;
8d28fec4 1133 mark_all_dirty(svm->vmcb);
0cc5064d 1134 }
94dfbdb3 1135
82ca2d10
AK
1136#ifdef CONFIG_X86_64
1137 rdmsrl(MSR_GS_BASE, to_svm(vcpu)->host.gs_base);
1138#endif
dacccfdd
AK
1139 savesegment(fs, svm->host.fs);
1140 savesegment(gs, svm->host.gs);
1141 svm->host.ldt = kvm_read_ldt();
1142
94dfbdb3 1143 for (i = 0; i < NR_HOST_SAVE_USER_MSRS; i++)
a2fa3e9f 1144 rdmsrl(host_save_user_msrs[i], svm->host_user_msrs[i]);
6aa8b732
AK
1145}
1146
1147static void svm_vcpu_put(struct kvm_vcpu *vcpu)
1148{
a2fa3e9f 1149 struct vcpu_svm *svm = to_svm(vcpu);
94dfbdb3
AL
1150 int i;
1151
e1beb1d3 1152 ++vcpu->stat.host_state_reload;
dacccfdd
AK
1153 kvm_load_ldt(svm->host.ldt);
1154#ifdef CONFIG_X86_64
1155 loadsegment(fs, svm->host.fs);
1156 load_gs_index(svm->host.gs);
1157 wrmsrl(MSR_KERNEL_GS_BASE, current->thread.gs);
1158#else
1159 loadsegment(gs, svm->host.gs);
1160#endif
94dfbdb3 1161 for (i = 0; i < NR_HOST_SAVE_USER_MSRS; i++)
a2fa3e9f 1162 wrmsrl(host_save_user_msrs[i], svm->host_user_msrs[i]);
6aa8b732
AK
1163}
1164
6aa8b732
AK
1165static unsigned long svm_get_rflags(struct kvm_vcpu *vcpu)
1166{
a2fa3e9f 1167 return to_svm(vcpu)->vmcb->save.rflags;
6aa8b732
AK
1168}
1169
1170static void svm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
1171{
a2fa3e9f 1172 to_svm(vcpu)->vmcb->save.rflags = rflags;
6aa8b732
AK
1173}
1174
6de4f3ad
AK
1175static void svm_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
1176{
1177 switch (reg) {
1178 case VCPU_EXREG_PDPTR:
1179 BUG_ON(!npt_enabled);
ff03a073 1180 load_pdptrs(vcpu, vcpu->arch.walk_mmu, vcpu->arch.cr3);
6de4f3ad
AK
1181 break;
1182 default:
1183 BUG();
1184 }
1185}
1186
f0b85051
AG
1187static void svm_set_vintr(struct vcpu_svm *svm)
1188{
8a05a1b8 1189 set_intercept(svm, INTERCEPT_VINTR);
f0b85051
AG
1190}
1191
1192static void svm_clear_vintr(struct vcpu_svm *svm)
1193{
8a05a1b8 1194 clr_intercept(svm, INTERCEPT_VINTR);
f0b85051
AG
1195}
1196
6aa8b732
AK
1197static struct vmcb_seg *svm_seg(struct kvm_vcpu *vcpu, int seg)
1198{
a2fa3e9f 1199 struct vmcb_save_area *save = &to_svm(vcpu)->vmcb->save;
6aa8b732
AK
1200
1201 switch (seg) {
1202 case VCPU_SREG_CS: return &save->cs;
1203 case VCPU_SREG_DS: return &save->ds;
1204 case VCPU_SREG_ES: return &save->es;
1205 case VCPU_SREG_FS: return &save->fs;
1206 case VCPU_SREG_GS: return &save->gs;
1207 case VCPU_SREG_SS: return &save->ss;
1208 case VCPU_SREG_TR: return &save->tr;
1209 case VCPU_SREG_LDTR: return &save->ldtr;
1210 }
1211 BUG();
8b6d44c7 1212 return NULL;
6aa8b732
AK
1213}
1214
1215static u64 svm_get_segment_base(struct kvm_vcpu *vcpu, int seg)
1216{
1217 struct vmcb_seg *s = svm_seg(vcpu, seg);
1218
1219 return s->base;
1220}
1221
1222static void svm_get_segment(struct kvm_vcpu *vcpu,
1223 struct kvm_segment *var, int seg)
1224{
1225 struct vmcb_seg *s = svm_seg(vcpu, seg);
1226
1227 var->base = s->base;
1228 var->limit = s->limit;
1229 var->selector = s->selector;
1230 var->type = s->attrib & SVM_SELECTOR_TYPE_MASK;
1231 var->s = (s->attrib >> SVM_SELECTOR_S_SHIFT) & 1;
1232 var->dpl = (s->attrib >> SVM_SELECTOR_DPL_SHIFT) & 3;
1233 var->present = (s->attrib >> SVM_SELECTOR_P_SHIFT) & 1;
1234 var->avl = (s->attrib >> SVM_SELECTOR_AVL_SHIFT) & 1;
1235 var->l = (s->attrib >> SVM_SELECTOR_L_SHIFT) & 1;
1236 var->db = (s->attrib >> SVM_SELECTOR_DB_SHIFT) & 1;
1237 var->g = (s->attrib >> SVM_SELECTOR_G_SHIFT) & 1;
25022acc 1238
e0231715
JR
1239 /*
1240 * AMD's VMCB does not have an explicit unusable field, so emulate it
19bca6ab
AP
1241 * for cross vendor migration purposes by "not present"
1242 */
1243 var->unusable = !var->present || (var->type == 0);
1244
1fbdc7a5
AP
1245 switch (seg) {
1246 case VCPU_SREG_CS:
1247 /*
1248 * SVM always stores 0 for the 'G' bit in the CS selector in
1249 * the VMCB on a VMEXIT. This hurts cross-vendor migration:
1250 * Intel's VMENTRY has a check on the 'G' bit.
1251 */
25022acc 1252 var->g = s->limit > 0xfffff;
1fbdc7a5
AP
1253 break;
1254 case VCPU_SREG_TR:
1255 /*
1256 * Work around a bug where the busy flag in the tr selector
1257 * isn't exposed
1258 */
c0d09828 1259 var->type |= 0x2;
1fbdc7a5
AP
1260 break;
1261 case VCPU_SREG_DS:
1262 case VCPU_SREG_ES:
1263 case VCPU_SREG_FS:
1264 case VCPU_SREG_GS:
1265 /*
1266 * The accessed bit must always be set in the segment
1267 * descriptor cache, although it can be cleared in the
1268 * descriptor, the cached bit always remains at 1. Since
1269 * Intel has a check on this, set it here to support
1270 * cross-vendor migration.
1271 */
1272 if (!var->unusable)
1273 var->type |= 0x1;
1274 break;
b586eb02 1275 case VCPU_SREG_SS:
e0231715
JR
1276 /*
1277 * On AMD CPUs sometimes the DB bit in the segment
b586eb02
AP
1278 * descriptor is left as 1, although the whole segment has
1279 * been made unusable. Clear it here to pass an Intel VMX
1280 * entry check when cross vendor migrating.
1281 */
1282 if (var->unusable)
1283 var->db = 0;
1284 break;
1fbdc7a5 1285 }
6aa8b732
AK
1286}
1287
2e4d2653
IE
1288static int svm_get_cpl(struct kvm_vcpu *vcpu)
1289{
1290 struct vmcb_save_area *save = &to_svm(vcpu)->vmcb->save;
1291
1292 return save->cpl;
1293}
1294
89a27f4d 1295static void svm_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
6aa8b732 1296{
a2fa3e9f
GH
1297 struct vcpu_svm *svm = to_svm(vcpu);
1298
89a27f4d
GN
1299 dt->size = svm->vmcb->save.idtr.limit;
1300 dt->address = svm->vmcb->save.idtr.base;
6aa8b732
AK
1301}
1302
89a27f4d 1303static void svm_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
6aa8b732 1304{
a2fa3e9f
GH
1305 struct vcpu_svm *svm = to_svm(vcpu);
1306
89a27f4d
GN
1307 svm->vmcb->save.idtr.limit = dt->size;
1308 svm->vmcb->save.idtr.base = dt->address ;
17a703cb 1309 mark_dirty(svm->vmcb, VMCB_DT);
6aa8b732
AK
1310}
1311
89a27f4d 1312static void svm_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
6aa8b732 1313{
a2fa3e9f
GH
1314 struct vcpu_svm *svm = to_svm(vcpu);
1315
89a27f4d
GN
1316 dt->size = svm->vmcb->save.gdtr.limit;
1317 dt->address = svm->vmcb->save.gdtr.base;
6aa8b732
AK
1318}
1319
89a27f4d 1320static void svm_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
6aa8b732 1321{
a2fa3e9f
GH
1322 struct vcpu_svm *svm = to_svm(vcpu);
1323
89a27f4d
GN
1324 svm->vmcb->save.gdtr.limit = dt->size;
1325 svm->vmcb->save.gdtr.base = dt->address ;
17a703cb 1326 mark_dirty(svm->vmcb, VMCB_DT);
6aa8b732
AK
1327}
1328
e8467fda
AK
1329static void svm_decache_cr0_guest_bits(struct kvm_vcpu *vcpu)
1330{
1331}
1332
25c4c276 1333static void svm_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
399badf3
AK
1334{
1335}
1336
d225157b
AK
1337static void update_cr0_intercept(struct vcpu_svm *svm)
1338{
1339 ulong gcr0 = svm->vcpu.arch.cr0;
1340 u64 *hcr0 = &svm->vmcb->save.cr0;
1341
1342 if (!svm->vcpu.fpu_active)
1343 *hcr0 |= SVM_CR0_SELECTIVE_MASK;
1344 else
1345 *hcr0 = (*hcr0 & ~SVM_CR0_SELECTIVE_MASK)
1346 | (gcr0 & SVM_CR0_SELECTIVE_MASK);
1347
dcca1a65 1348 mark_dirty(svm->vmcb, VMCB_CR);
d225157b
AK
1349
1350 if (gcr0 == *hcr0 && svm->vcpu.fpu_active) {
4ee546b4
RJ
1351 clr_cr_intercept(svm, INTERCEPT_CR0_READ);
1352 clr_cr_intercept(svm, INTERCEPT_CR0_WRITE);
d225157b 1353 } else {
4ee546b4
RJ
1354 set_cr_intercept(svm, INTERCEPT_CR0_READ);
1355 set_cr_intercept(svm, INTERCEPT_CR0_WRITE);
d225157b
AK
1356 }
1357}
1358
6aa8b732
AK
1359static void svm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
1360{
a2fa3e9f
GH
1361 struct vcpu_svm *svm = to_svm(vcpu);
1362
2030753d 1363 if (is_guest_mode(vcpu)) {
7f5d8b56
JR
1364 /*
1365 * We are here because we run in nested mode, the host kvm
1366 * intercepts cr0 writes but the l1 hypervisor does not.
1367 * But the L1 hypervisor may intercept selective cr0 writes.
1368 * This needs to be checked here.
1369 */
1370 unsigned long old, new;
1371
1372 /* Remove bits that would trigger a real cr0 write intercept */
1373 old = vcpu->arch.cr0 & SVM_CR0_SELECTIVE_MASK;
1374 new = cr0 & SVM_CR0_SELECTIVE_MASK;
1375
1376 if (old == new) {
1377 /* cr0 write with ts and mp unchanged */
1378 svm->vmcb->control.exit_code = SVM_EXIT_CR0_SEL_WRITE;
cda00082
JR
1379 if (nested_svm_exit_handled(svm) == NESTED_EXIT_DONE) {
1380 svm->nested.vmexit_rip = kvm_rip_read(vcpu);
1381 svm->nested.vmexit_rsp = kvm_register_read(vcpu, VCPU_REGS_RSP);
1382 svm->nested.vmexit_rax = kvm_register_read(vcpu, VCPU_REGS_RAX);
7f5d8b56 1383 return;
cda00082 1384 }
7f5d8b56
JR
1385 }
1386 }
1387
05b3e0c2 1388#ifdef CONFIG_X86_64
f6801dff 1389 if (vcpu->arch.efer & EFER_LME) {
707d92fa 1390 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) {
f6801dff 1391 vcpu->arch.efer |= EFER_LMA;
2b5203ee 1392 svm->vmcb->save.efer |= EFER_LMA | EFER_LME;
6aa8b732
AK
1393 }
1394
d77c26fc 1395 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG)) {
f6801dff 1396 vcpu->arch.efer &= ~EFER_LMA;
2b5203ee 1397 svm->vmcb->save.efer &= ~(EFER_LMA | EFER_LME);
6aa8b732
AK
1398 }
1399 }
1400#endif
ad312c7c 1401 vcpu->arch.cr0 = cr0;
888f9f3e
AK
1402
1403 if (!npt_enabled)
1404 cr0 |= X86_CR0_PG | X86_CR0_WP;
02daab21
AK
1405
1406 if (!vcpu->fpu_active)
334df50a 1407 cr0 |= X86_CR0_TS;
709ddebf
JR
1408 /*
1409 * re-enable caching here because the QEMU bios
1410 * does not do it - this results in some delay at
1411 * reboot
1412 */
1413 cr0 &= ~(X86_CR0_CD | X86_CR0_NW);
a2fa3e9f 1414 svm->vmcb->save.cr0 = cr0;
dcca1a65 1415 mark_dirty(svm->vmcb, VMCB_CR);
d225157b 1416 update_cr0_intercept(svm);
6aa8b732
AK
1417}
1418
1419static void svm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
1420{
6394b649 1421 unsigned long host_cr4_mce = read_cr4() & X86_CR4_MCE;
e5eab0ce
JR
1422 unsigned long old_cr4 = to_svm(vcpu)->vmcb->save.cr4;
1423
1424 if (npt_enabled && ((old_cr4 ^ cr4) & X86_CR4_PGE))
1425 force_new_asid(vcpu);
6394b649 1426
ec077263
JR
1427 vcpu->arch.cr4 = cr4;
1428 if (!npt_enabled)
1429 cr4 |= X86_CR4_PAE;
6394b649 1430 cr4 |= host_cr4_mce;
ec077263 1431 to_svm(vcpu)->vmcb->save.cr4 = cr4;
dcca1a65 1432 mark_dirty(to_svm(vcpu)->vmcb, VMCB_CR);
6aa8b732
AK
1433}
1434
1435static void svm_set_segment(struct kvm_vcpu *vcpu,
1436 struct kvm_segment *var, int seg)
1437{
a2fa3e9f 1438 struct vcpu_svm *svm = to_svm(vcpu);
6aa8b732
AK
1439 struct vmcb_seg *s = svm_seg(vcpu, seg);
1440
1441 s->base = var->base;
1442 s->limit = var->limit;
1443 s->selector = var->selector;
1444 if (var->unusable)
1445 s->attrib = 0;
1446 else {
1447 s->attrib = (var->type & SVM_SELECTOR_TYPE_MASK);
1448 s->attrib |= (var->s & 1) << SVM_SELECTOR_S_SHIFT;
1449 s->attrib |= (var->dpl & 3) << SVM_SELECTOR_DPL_SHIFT;
1450 s->attrib |= (var->present & 1) << SVM_SELECTOR_P_SHIFT;
1451 s->attrib |= (var->avl & 1) << SVM_SELECTOR_AVL_SHIFT;
1452 s->attrib |= (var->l & 1) << SVM_SELECTOR_L_SHIFT;
1453 s->attrib |= (var->db & 1) << SVM_SELECTOR_DB_SHIFT;
1454 s->attrib |= (var->g & 1) << SVM_SELECTOR_G_SHIFT;
1455 }
1456 if (seg == VCPU_SREG_CS)
a2fa3e9f
GH
1457 svm->vmcb->save.cpl
1458 = (svm->vmcb->save.cs.attrib
6aa8b732
AK
1459 >> SVM_SELECTOR_DPL_SHIFT) & 3;
1460
060d0c9a 1461 mark_dirty(svm->vmcb, VMCB_SEG);
6aa8b732
AK
1462}
1463
44c11430 1464static void update_db_intercept(struct kvm_vcpu *vcpu)
6aa8b732 1465{
d0bfb940
JK
1466 struct vcpu_svm *svm = to_svm(vcpu);
1467
18c918c5
JR
1468 clr_exception_intercept(svm, DB_VECTOR);
1469 clr_exception_intercept(svm, BP_VECTOR);
44c11430 1470
6be7d306 1471 if (svm->nmi_singlestep)
18c918c5 1472 set_exception_intercept(svm, DB_VECTOR);
44c11430 1473
d0bfb940
JK
1474 if (vcpu->guest_debug & KVM_GUESTDBG_ENABLE) {
1475 if (vcpu->guest_debug &
1476 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
18c918c5 1477 set_exception_intercept(svm, DB_VECTOR);
d0bfb940 1478 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
18c918c5 1479 set_exception_intercept(svm, BP_VECTOR);
d0bfb940
JK
1480 } else
1481 vcpu->guest_debug = 0;
44c11430
GN
1482}
1483
355be0b9 1484static void svm_guest_debug(struct kvm_vcpu *vcpu, struct kvm_guest_debug *dbg)
44c11430 1485{
44c11430
GN
1486 struct vcpu_svm *svm = to_svm(vcpu);
1487
ae675ef0
JK
1488 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)
1489 svm->vmcb->save.dr7 = dbg->arch.debugreg[7];
1490 else
1491 svm->vmcb->save.dr7 = vcpu->arch.dr7;
1492
72214b96
JR
1493 mark_dirty(svm->vmcb, VMCB_DR);
1494
355be0b9 1495 update_db_intercept(vcpu);
6aa8b732
AK
1496}
1497
0fe1e009 1498static void new_asid(struct vcpu_svm *svm, struct svm_cpu_data *sd)
6aa8b732 1499{
0fe1e009
TH
1500 if (sd->next_asid > sd->max_asid) {
1501 ++sd->asid_generation;
1502 sd->next_asid = 1;
a2fa3e9f 1503 svm->vmcb->control.tlb_ctl = TLB_CONTROL_FLUSH_ALL_ASID;
6aa8b732
AK
1504 }
1505
0fe1e009
TH
1506 svm->asid_generation = sd->asid_generation;
1507 svm->vmcb->control.asid = sd->next_asid++;
d48086d1
JR
1508
1509 mark_dirty(svm->vmcb, VMCB_ASID);
6aa8b732
AK
1510}
1511
020df079 1512static void svm_set_dr7(struct kvm_vcpu *vcpu, unsigned long value)
6aa8b732 1513{
42dbaa5a 1514 struct vcpu_svm *svm = to_svm(vcpu);
42dbaa5a 1515
020df079 1516 svm->vmcb->save.dr7 = value;
72214b96 1517 mark_dirty(svm->vmcb, VMCB_DR);
6aa8b732
AK
1518}
1519
851ba692 1520static int pf_interception(struct vcpu_svm *svm)
6aa8b732 1521{
631bc487 1522 u64 fault_address = svm->vmcb->control.exit_info_2;
6aa8b732 1523 u32 error_code;
631bc487 1524 int r = 1;
6aa8b732 1525
631bc487
GN
1526 switch (svm->apf_reason) {
1527 default:
1528 error_code = svm->vmcb->control.exit_info_1;
af9ca2d7 1529
631bc487
GN
1530 trace_kvm_page_fault(fault_address, error_code);
1531 if (!npt_enabled && kvm_event_needs_reinjection(&svm->vcpu))
1532 kvm_mmu_unprotect_page_virt(&svm->vcpu, fault_address);
1533 r = kvm_mmu_page_fault(&svm->vcpu, fault_address, error_code);
1534 break;
1535 case KVM_PV_REASON_PAGE_NOT_PRESENT:
1536 svm->apf_reason = 0;
1537 local_irq_disable();
1538 kvm_async_pf_task_wait(fault_address);
1539 local_irq_enable();
1540 break;
1541 case KVM_PV_REASON_PAGE_READY:
1542 svm->apf_reason = 0;
1543 local_irq_disable();
1544 kvm_async_pf_task_wake(fault_address);
1545 local_irq_enable();
1546 break;
1547 }
1548 return r;
6aa8b732
AK
1549}
1550
851ba692 1551static int db_interception(struct vcpu_svm *svm)
d0bfb940 1552{
851ba692
AK
1553 struct kvm_run *kvm_run = svm->vcpu.run;
1554
d0bfb940 1555 if (!(svm->vcpu.guest_debug &
44c11430 1556 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)) &&
6be7d306 1557 !svm->nmi_singlestep) {
d0bfb940
JK
1558 kvm_queue_exception(&svm->vcpu, DB_VECTOR);
1559 return 1;
1560 }
44c11430 1561
6be7d306
JK
1562 if (svm->nmi_singlestep) {
1563 svm->nmi_singlestep = false;
44c11430
GN
1564 if (!(svm->vcpu.guest_debug & KVM_GUESTDBG_SINGLESTEP))
1565 svm->vmcb->save.rflags &=
1566 ~(X86_EFLAGS_TF | X86_EFLAGS_RF);
1567 update_db_intercept(&svm->vcpu);
1568 }
1569
1570 if (svm->vcpu.guest_debug &
e0231715 1571 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)) {
44c11430
GN
1572 kvm_run->exit_reason = KVM_EXIT_DEBUG;
1573 kvm_run->debug.arch.pc =
1574 svm->vmcb->save.cs.base + svm->vmcb->save.rip;
1575 kvm_run->debug.arch.exception = DB_VECTOR;
1576 return 0;
1577 }
1578
1579 return 1;
d0bfb940
JK
1580}
1581
851ba692 1582static int bp_interception(struct vcpu_svm *svm)
d0bfb940 1583{
851ba692
AK
1584 struct kvm_run *kvm_run = svm->vcpu.run;
1585
d0bfb940
JK
1586 kvm_run->exit_reason = KVM_EXIT_DEBUG;
1587 kvm_run->debug.arch.pc = svm->vmcb->save.cs.base + svm->vmcb->save.rip;
1588 kvm_run->debug.arch.exception = BP_VECTOR;
1589 return 0;
1590}
1591
851ba692 1592static int ud_interception(struct vcpu_svm *svm)
7aa81cc0
AL
1593{
1594 int er;
1595
851ba692 1596 er = emulate_instruction(&svm->vcpu, 0, 0, EMULTYPE_TRAP_UD);
7aa81cc0 1597 if (er != EMULATE_DONE)
7ee5d940 1598 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
7aa81cc0
AL
1599 return 1;
1600}
1601
6b52d186 1602static void svm_fpu_activate(struct kvm_vcpu *vcpu)
7807fa6c 1603{
6b52d186 1604 struct vcpu_svm *svm = to_svm(vcpu);
66a562f7 1605
18c918c5 1606 clr_exception_intercept(svm, NM_VECTOR);
66a562f7 1607
e756fc62 1608 svm->vcpu.fpu_active = 1;
d225157b 1609 update_cr0_intercept(svm);
6b52d186 1610}
a2fa3e9f 1611
6b52d186
AK
1612static int nm_interception(struct vcpu_svm *svm)
1613{
1614 svm_fpu_activate(&svm->vcpu);
a2fa3e9f 1615 return 1;
7807fa6c
AL
1616}
1617
67ec6607
JR
1618static bool is_erratum_383(void)
1619{
1620 int err, i;
1621 u64 value;
1622
1623 if (!erratum_383_found)
1624 return false;
1625
1626 value = native_read_msr_safe(MSR_IA32_MC0_STATUS, &err);
1627 if (err)
1628 return false;
1629
1630 /* Bit 62 may or may not be set for this mce */
1631 value &= ~(1ULL << 62);
1632
1633 if (value != 0xb600000000010015ULL)
1634 return false;
1635
1636 /* Clear MCi_STATUS registers */
1637 for (i = 0; i < 6; ++i)
1638 native_write_msr_safe(MSR_IA32_MCx_STATUS(i), 0, 0);
1639
1640 value = native_read_msr_safe(MSR_IA32_MCG_STATUS, &err);
1641 if (!err) {
1642 u32 low, high;
1643
1644 value &= ~(1ULL << 2);
1645 low = lower_32_bits(value);
1646 high = upper_32_bits(value);
1647
1648 native_write_msr_safe(MSR_IA32_MCG_STATUS, low, high);
1649 }
1650
1651 /* Flush tlb to evict multi-match entries */
1652 __flush_tlb_all();
1653
1654 return true;
1655}
1656
fe5913e4 1657static void svm_handle_mce(struct vcpu_svm *svm)
53371b50 1658{
67ec6607
JR
1659 if (is_erratum_383()) {
1660 /*
1661 * Erratum 383 triggered. Guest state is corrupt so kill the
1662 * guest.
1663 */
1664 pr_err("KVM: Guest triggered AMD Erratum 383\n");
1665
a8eeb04a 1666 kvm_make_request(KVM_REQ_TRIPLE_FAULT, &svm->vcpu);
67ec6607
JR
1667
1668 return;
1669 }
1670
53371b50
JR
1671 /*
1672 * On an #MC intercept the MCE handler is not called automatically in
1673 * the host. So do it by hand here.
1674 */
1675 asm volatile (
1676 "int $0x12\n");
1677 /* not sure if we ever come back to this point */
1678
fe5913e4
JR
1679 return;
1680}
1681
1682static int mc_interception(struct vcpu_svm *svm)
1683{
53371b50
JR
1684 return 1;
1685}
1686
851ba692 1687static int shutdown_interception(struct vcpu_svm *svm)
46fe4ddd 1688{
851ba692
AK
1689 struct kvm_run *kvm_run = svm->vcpu.run;
1690
46fe4ddd
JR
1691 /*
1692 * VMCB is undefined after a SHUTDOWN intercept
1693 * so reinitialize it.
1694 */
a2fa3e9f 1695 clear_page(svm->vmcb);
e6101a96 1696 init_vmcb(svm);
46fe4ddd
JR
1697
1698 kvm_run->exit_reason = KVM_EXIT_SHUTDOWN;
1699 return 0;
1700}
1701
851ba692 1702static int io_interception(struct vcpu_svm *svm)
6aa8b732 1703{
cf8f70bf 1704 struct kvm_vcpu *vcpu = &svm->vcpu;
d77c26fc 1705 u32 io_info = svm->vmcb->control.exit_info_1; /* address size bug? */
34c33d16 1706 int size, in, string;
039576c0 1707 unsigned port;
6aa8b732 1708
e756fc62 1709 ++svm->vcpu.stat.io_exits;
e70669ab 1710 string = (io_info & SVM_IOIO_STR_MASK) != 0;
039576c0 1711 in = (io_info & SVM_IOIO_TYPE_MASK) != 0;
cf8f70bf 1712 if (string || in)
6d77dbfc 1713 return emulate_instruction(vcpu, 0, 0, 0) == EMULATE_DONE;
cf8f70bf 1714
039576c0
AK
1715 port = io_info >> 16;
1716 size = (io_info & SVM_IOIO_SIZE_MASK) >> SVM_IOIO_SIZE_SHIFT;
cf8f70bf 1717 svm->next_rip = svm->vmcb->control.exit_info_2;
e93f36bc 1718 skip_emulated_instruction(&svm->vcpu);
cf8f70bf
GN
1719
1720 return kvm_fast_pio_out(vcpu, size, port);
6aa8b732
AK
1721}
1722
851ba692 1723static int nmi_interception(struct vcpu_svm *svm)
c47f098d
JR
1724{
1725 return 1;
1726}
1727
851ba692 1728static int intr_interception(struct vcpu_svm *svm)
a0698055
JR
1729{
1730 ++svm->vcpu.stat.irq_exits;
1731 return 1;
1732}
1733
851ba692 1734static int nop_on_interception(struct vcpu_svm *svm)
6aa8b732
AK
1735{
1736 return 1;
1737}
1738
851ba692 1739static int halt_interception(struct vcpu_svm *svm)
6aa8b732 1740{
5fdbf976 1741 svm->next_rip = kvm_rip_read(&svm->vcpu) + 1;
e756fc62
RR
1742 skip_emulated_instruction(&svm->vcpu);
1743 return kvm_emulate_halt(&svm->vcpu);
6aa8b732
AK
1744}
1745
851ba692 1746static int vmmcall_interception(struct vcpu_svm *svm)
02e235bc 1747{
5fdbf976 1748 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
e756fc62 1749 skip_emulated_instruction(&svm->vcpu);
7aa81cc0
AL
1750 kvm_emulate_hypercall(&svm->vcpu);
1751 return 1;
02e235bc
AK
1752}
1753
5bd2edc3
JR
1754static unsigned long nested_svm_get_tdp_cr3(struct kvm_vcpu *vcpu)
1755{
1756 struct vcpu_svm *svm = to_svm(vcpu);
1757
1758 return svm->nested.nested_cr3;
1759}
1760
1761static void nested_svm_set_tdp_cr3(struct kvm_vcpu *vcpu,
1762 unsigned long root)
1763{
1764 struct vcpu_svm *svm = to_svm(vcpu);
1765
1766 svm->vmcb->control.nested_cr3 = root;
b2747166 1767 mark_dirty(svm->vmcb, VMCB_NPT);
5bd2edc3
JR
1768 force_new_asid(vcpu);
1769}
1770
6389ee94
AK
1771static void nested_svm_inject_npf_exit(struct kvm_vcpu *vcpu,
1772 struct x86_exception *fault)
5bd2edc3
JR
1773{
1774 struct vcpu_svm *svm = to_svm(vcpu);
1775
1776 svm->vmcb->control.exit_code = SVM_EXIT_NPF;
1777 svm->vmcb->control.exit_code_hi = 0;
6389ee94
AK
1778 svm->vmcb->control.exit_info_1 = fault->error_code;
1779 svm->vmcb->control.exit_info_2 = fault->address;
5bd2edc3
JR
1780
1781 nested_svm_vmexit(svm);
1782}
1783
4b16184c
JR
1784static int nested_svm_init_mmu_context(struct kvm_vcpu *vcpu)
1785{
1786 int r;
1787
1788 r = kvm_init_shadow_mmu(vcpu, &vcpu->arch.mmu);
1789
1790 vcpu->arch.mmu.set_cr3 = nested_svm_set_tdp_cr3;
1791 vcpu->arch.mmu.get_cr3 = nested_svm_get_tdp_cr3;
1792 vcpu->arch.mmu.inject_page_fault = nested_svm_inject_npf_exit;
1793 vcpu->arch.mmu.shadow_root_level = get_npt_level();
1794 vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu;
1795
1796 return r;
1797}
1798
1799static void nested_svm_uninit_mmu_context(struct kvm_vcpu *vcpu)
1800{
1801 vcpu->arch.walk_mmu = &vcpu->arch.mmu;
1802}
1803
c0725420
AG
1804static int nested_svm_check_permissions(struct vcpu_svm *svm)
1805{
f6801dff 1806 if (!(svm->vcpu.arch.efer & EFER_SVME)
c0725420
AG
1807 || !is_paging(&svm->vcpu)) {
1808 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
1809 return 1;
1810 }
1811
1812 if (svm->vmcb->save.cpl) {
1813 kvm_inject_gp(&svm->vcpu, 0);
1814 return 1;
1815 }
1816
1817 return 0;
1818}
1819
cf74a78b
AG
1820static int nested_svm_check_exception(struct vcpu_svm *svm, unsigned nr,
1821 bool has_error_code, u32 error_code)
1822{
b8e88bc8
JR
1823 int vmexit;
1824
2030753d 1825 if (!is_guest_mode(&svm->vcpu))
0295ad7d 1826 return 0;
cf74a78b 1827
0295ad7d
JR
1828 svm->vmcb->control.exit_code = SVM_EXIT_EXCP_BASE + nr;
1829 svm->vmcb->control.exit_code_hi = 0;
1830 svm->vmcb->control.exit_info_1 = error_code;
1831 svm->vmcb->control.exit_info_2 = svm->vcpu.arch.cr2;
1832
b8e88bc8
JR
1833 vmexit = nested_svm_intercept(svm);
1834 if (vmexit == NESTED_EXIT_DONE)
1835 svm->nested.exit_required = true;
1836
1837 return vmexit;
cf74a78b
AG
1838}
1839
8fe54654
JR
1840/* This function returns true if it is save to enable the irq window */
1841static inline bool nested_svm_intr(struct vcpu_svm *svm)
cf74a78b 1842{
2030753d 1843 if (!is_guest_mode(&svm->vcpu))
8fe54654 1844 return true;
cf74a78b 1845
26666957 1846 if (!(svm->vcpu.arch.hflags & HF_VINTR_MASK))
8fe54654 1847 return true;
cf74a78b 1848
26666957 1849 if (!(svm->vcpu.arch.hflags & HF_HIF_MASK))
8fe54654 1850 return false;
cf74a78b 1851
a0a07cd2
GN
1852 /*
1853 * if vmexit was already requested (by intercepted exception
1854 * for instance) do not overwrite it with "external interrupt"
1855 * vmexit.
1856 */
1857 if (svm->nested.exit_required)
1858 return false;
1859
197717d5
JR
1860 svm->vmcb->control.exit_code = SVM_EXIT_INTR;
1861 svm->vmcb->control.exit_info_1 = 0;
1862 svm->vmcb->control.exit_info_2 = 0;
26666957 1863
cd3ff653
JR
1864 if (svm->nested.intercept & 1ULL) {
1865 /*
1866 * The #vmexit can't be emulated here directly because this
1867 * code path runs with irqs and preemtion disabled. A
1868 * #vmexit emulation might sleep. Only signal request for
1869 * the #vmexit here.
1870 */
1871 svm->nested.exit_required = true;
236649de 1872 trace_kvm_nested_intr_vmexit(svm->vmcb->save.rip);
8fe54654 1873 return false;
cf74a78b
AG
1874 }
1875
8fe54654 1876 return true;
cf74a78b
AG
1877}
1878
887f500c
JR
1879/* This function returns true if it is save to enable the nmi window */
1880static inline bool nested_svm_nmi(struct vcpu_svm *svm)
1881{
2030753d 1882 if (!is_guest_mode(&svm->vcpu))
887f500c
JR
1883 return true;
1884
1885 if (!(svm->nested.intercept & (1ULL << INTERCEPT_NMI)))
1886 return true;
1887
1888 svm->vmcb->control.exit_code = SVM_EXIT_NMI;
1889 svm->nested.exit_required = true;
1890
1891 return false;
cf74a78b
AG
1892}
1893
7597f129 1894static void *nested_svm_map(struct vcpu_svm *svm, u64 gpa, struct page **_page)
34f80cfa
JR
1895{
1896 struct page *page;
1897
6c3bd3d7
JR
1898 might_sleep();
1899
34f80cfa 1900 page = gfn_to_page(svm->vcpu.kvm, gpa >> PAGE_SHIFT);
34f80cfa
JR
1901 if (is_error_page(page))
1902 goto error;
1903
7597f129
JR
1904 *_page = page;
1905
1906 return kmap(page);
34f80cfa
JR
1907
1908error:
1909 kvm_release_page_clean(page);
1910 kvm_inject_gp(&svm->vcpu, 0);
1911
1912 return NULL;
1913}
1914
7597f129 1915static void nested_svm_unmap(struct page *page)
34f80cfa 1916{
7597f129 1917 kunmap(page);
34f80cfa
JR
1918 kvm_release_page_dirty(page);
1919}
34f80cfa 1920
ce2ac085
JR
1921static int nested_svm_intercept_ioio(struct vcpu_svm *svm)
1922{
1923 unsigned port;
1924 u8 val, bit;
1925 u64 gpa;
34f80cfa 1926
ce2ac085
JR
1927 if (!(svm->nested.intercept & (1ULL << INTERCEPT_IOIO_PROT)))
1928 return NESTED_EXIT_HOST;
34f80cfa 1929
ce2ac085
JR
1930 port = svm->vmcb->control.exit_info_1 >> 16;
1931 gpa = svm->nested.vmcb_iopm + (port / 8);
1932 bit = port % 8;
1933 val = 0;
1934
1935 if (kvm_read_guest(svm->vcpu.kvm, gpa, &val, 1))
1936 val &= (1 << bit);
1937
1938 return val ? NESTED_EXIT_DONE : NESTED_EXIT_HOST;
34f80cfa
JR
1939}
1940
d2477826 1941static int nested_svm_exit_handled_msr(struct vcpu_svm *svm)
4c2161ae 1942{
0d6b3537
JR
1943 u32 offset, msr, value;
1944 int write, mask;
4c2161ae 1945
3d62d9aa 1946 if (!(svm->nested.intercept & (1ULL << INTERCEPT_MSR_PROT)))
d2477826 1947 return NESTED_EXIT_HOST;
3d62d9aa 1948
0d6b3537
JR
1949 msr = svm->vcpu.arch.regs[VCPU_REGS_RCX];
1950 offset = svm_msrpm_offset(msr);
1951 write = svm->vmcb->control.exit_info_1 & 1;
1952 mask = 1 << ((2 * (msr & 0xf)) + write);
3d62d9aa 1953
0d6b3537
JR
1954 if (offset == MSR_INVALID)
1955 return NESTED_EXIT_DONE;
4c2161ae 1956
0d6b3537
JR
1957 /* Offset is in 32 bit units but need in 8 bit units */
1958 offset *= 4;
4c2161ae 1959
0d6b3537
JR
1960 if (kvm_read_guest(svm->vcpu.kvm, svm->nested.vmcb_msrpm + offset, &value, 4))
1961 return NESTED_EXIT_DONE;
3d62d9aa 1962
0d6b3537 1963 return (value & mask) ? NESTED_EXIT_DONE : NESTED_EXIT_HOST;
4c2161ae
JR
1964}
1965
410e4d57 1966static int nested_svm_exit_special(struct vcpu_svm *svm)
cf74a78b 1967{
cf74a78b 1968 u32 exit_code = svm->vmcb->control.exit_code;
4c2161ae 1969
410e4d57
JR
1970 switch (exit_code) {
1971 case SVM_EXIT_INTR:
1972 case SVM_EXIT_NMI:
ff47a49b 1973 case SVM_EXIT_EXCP_BASE + MC_VECTOR:
410e4d57 1974 return NESTED_EXIT_HOST;
410e4d57 1975 case SVM_EXIT_NPF:
e0231715 1976 /* For now we are always handling NPFs when using them */
410e4d57
JR
1977 if (npt_enabled)
1978 return NESTED_EXIT_HOST;
1979 break;
410e4d57 1980 case SVM_EXIT_EXCP_BASE + PF_VECTOR:
631bc487
GN
1981 /* When we're shadowing, trap PFs, but not async PF */
1982 if (!npt_enabled && svm->apf_reason == 0)
410e4d57
JR
1983 return NESTED_EXIT_HOST;
1984 break;
66a562f7
JR
1985 case SVM_EXIT_EXCP_BASE + NM_VECTOR:
1986 nm_interception(svm);
1987 break;
410e4d57
JR
1988 default:
1989 break;
cf74a78b
AG
1990 }
1991
410e4d57
JR
1992 return NESTED_EXIT_CONTINUE;
1993}
1994
1995/*
1996 * If this function returns true, this #vmexit was already handled
1997 */
b8e88bc8 1998static int nested_svm_intercept(struct vcpu_svm *svm)
410e4d57
JR
1999{
2000 u32 exit_code = svm->vmcb->control.exit_code;
2001 int vmexit = NESTED_EXIT_HOST;
2002
cf74a78b 2003 switch (exit_code) {
9c4e40b9 2004 case SVM_EXIT_MSR:
3d62d9aa 2005 vmexit = nested_svm_exit_handled_msr(svm);
9c4e40b9 2006 break;
ce2ac085
JR
2007 case SVM_EXIT_IOIO:
2008 vmexit = nested_svm_intercept_ioio(svm);
2009 break;
4ee546b4
RJ
2010 case SVM_EXIT_READ_CR0 ... SVM_EXIT_WRITE_CR8: {
2011 u32 bit = 1U << (exit_code - SVM_EXIT_READ_CR0);
2012 if (svm->nested.intercept_cr & bit)
410e4d57 2013 vmexit = NESTED_EXIT_DONE;
cf74a78b
AG
2014 break;
2015 }
3aed041a
JR
2016 case SVM_EXIT_READ_DR0 ... SVM_EXIT_WRITE_DR7: {
2017 u32 bit = 1U << (exit_code - SVM_EXIT_READ_DR0);
2018 if (svm->nested.intercept_dr & bit)
410e4d57 2019 vmexit = NESTED_EXIT_DONE;
cf74a78b
AG
2020 break;
2021 }
2022 case SVM_EXIT_EXCP_BASE ... SVM_EXIT_EXCP_BASE + 0x1f: {
2023 u32 excp_bits = 1 << (exit_code - SVM_EXIT_EXCP_BASE);
aad42c64 2024 if (svm->nested.intercept_exceptions & excp_bits)
410e4d57 2025 vmexit = NESTED_EXIT_DONE;
631bc487
GN
2026 /* async page fault always cause vmexit */
2027 else if ((exit_code == SVM_EXIT_EXCP_BASE + PF_VECTOR) &&
2028 svm->apf_reason != 0)
2029 vmexit = NESTED_EXIT_DONE;
cf74a78b
AG
2030 break;
2031 }
228070b1
JR
2032 case SVM_EXIT_ERR: {
2033 vmexit = NESTED_EXIT_DONE;
2034 break;
2035 }
cf74a78b
AG
2036 default: {
2037 u64 exit_bits = 1ULL << (exit_code - SVM_EXIT_INTR);
aad42c64 2038 if (svm->nested.intercept & exit_bits)
410e4d57 2039 vmexit = NESTED_EXIT_DONE;
cf74a78b
AG
2040 }
2041 }
2042
b8e88bc8
JR
2043 return vmexit;
2044}
2045
2046static int nested_svm_exit_handled(struct vcpu_svm *svm)
2047{
2048 int vmexit;
2049
2050 vmexit = nested_svm_intercept(svm);
2051
2052 if (vmexit == NESTED_EXIT_DONE)
9c4e40b9 2053 nested_svm_vmexit(svm);
9c4e40b9
JR
2054
2055 return vmexit;
cf74a78b
AG
2056}
2057
0460a979
JR
2058static inline void copy_vmcb_control_area(struct vmcb *dst_vmcb, struct vmcb *from_vmcb)
2059{
2060 struct vmcb_control_area *dst = &dst_vmcb->control;
2061 struct vmcb_control_area *from = &from_vmcb->control;
2062
4ee546b4 2063 dst->intercept_cr = from->intercept_cr;
3aed041a 2064 dst->intercept_dr = from->intercept_dr;
0460a979
JR
2065 dst->intercept_exceptions = from->intercept_exceptions;
2066 dst->intercept = from->intercept;
2067 dst->iopm_base_pa = from->iopm_base_pa;
2068 dst->msrpm_base_pa = from->msrpm_base_pa;
2069 dst->tsc_offset = from->tsc_offset;
2070 dst->asid = from->asid;
2071 dst->tlb_ctl = from->tlb_ctl;
2072 dst->int_ctl = from->int_ctl;
2073 dst->int_vector = from->int_vector;
2074 dst->int_state = from->int_state;
2075 dst->exit_code = from->exit_code;
2076 dst->exit_code_hi = from->exit_code_hi;
2077 dst->exit_info_1 = from->exit_info_1;
2078 dst->exit_info_2 = from->exit_info_2;
2079 dst->exit_int_info = from->exit_int_info;
2080 dst->exit_int_info_err = from->exit_int_info_err;
2081 dst->nested_ctl = from->nested_ctl;
2082 dst->event_inj = from->event_inj;
2083 dst->event_inj_err = from->event_inj_err;
2084 dst->nested_cr3 = from->nested_cr3;
2085 dst->lbr_ctl = from->lbr_ctl;
2086}
2087
34f80cfa 2088static int nested_svm_vmexit(struct vcpu_svm *svm)
cf74a78b 2089{
34f80cfa 2090 struct vmcb *nested_vmcb;
e6aa9abd 2091 struct vmcb *hsave = svm->nested.hsave;
33740e40 2092 struct vmcb *vmcb = svm->vmcb;
7597f129 2093 struct page *page;
cf74a78b 2094
17897f36
JR
2095 trace_kvm_nested_vmexit_inject(vmcb->control.exit_code,
2096 vmcb->control.exit_info_1,
2097 vmcb->control.exit_info_2,
2098 vmcb->control.exit_int_info,
2099 vmcb->control.exit_int_info_err);
2100
7597f129 2101 nested_vmcb = nested_svm_map(svm, svm->nested.vmcb, &page);
34f80cfa
JR
2102 if (!nested_vmcb)
2103 return 1;
2104
2030753d
JR
2105 /* Exit Guest-Mode */
2106 leave_guest_mode(&svm->vcpu);
06fc7772
JR
2107 svm->nested.vmcb = 0;
2108
cf74a78b 2109 /* Give the current vmcb to the guest */
33740e40
JR
2110 disable_gif(svm);
2111
2112 nested_vmcb->save.es = vmcb->save.es;
2113 nested_vmcb->save.cs = vmcb->save.cs;
2114 nested_vmcb->save.ss = vmcb->save.ss;
2115 nested_vmcb->save.ds = vmcb->save.ds;
2116 nested_vmcb->save.gdtr = vmcb->save.gdtr;
2117 nested_vmcb->save.idtr = vmcb->save.idtr;
3f6a9d16 2118 nested_vmcb->save.efer = svm->vcpu.arch.efer;
cdbbdc12 2119 nested_vmcb->save.cr0 = kvm_read_cr0(&svm->vcpu);
2be4fc7a 2120 nested_vmcb->save.cr3 = svm->vcpu.arch.cr3;
33740e40 2121 nested_vmcb->save.cr2 = vmcb->save.cr2;
cdbbdc12 2122 nested_vmcb->save.cr4 = svm->vcpu.arch.cr4;
33740e40
JR
2123 nested_vmcb->save.rflags = vmcb->save.rflags;
2124 nested_vmcb->save.rip = vmcb->save.rip;
2125 nested_vmcb->save.rsp = vmcb->save.rsp;
2126 nested_vmcb->save.rax = vmcb->save.rax;
2127 nested_vmcb->save.dr7 = vmcb->save.dr7;
2128 nested_vmcb->save.dr6 = vmcb->save.dr6;
2129 nested_vmcb->save.cpl = vmcb->save.cpl;
2130
2131 nested_vmcb->control.int_ctl = vmcb->control.int_ctl;
2132 nested_vmcb->control.int_vector = vmcb->control.int_vector;
2133 nested_vmcb->control.int_state = vmcb->control.int_state;
2134 nested_vmcb->control.exit_code = vmcb->control.exit_code;
2135 nested_vmcb->control.exit_code_hi = vmcb->control.exit_code_hi;
2136 nested_vmcb->control.exit_info_1 = vmcb->control.exit_info_1;
2137 nested_vmcb->control.exit_info_2 = vmcb->control.exit_info_2;
2138 nested_vmcb->control.exit_int_info = vmcb->control.exit_int_info;
2139 nested_vmcb->control.exit_int_info_err = vmcb->control.exit_int_info_err;
7a190667 2140 nested_vmcb->control.next_rip = vmcb->control.next_rip;
8d23c466
AG
2141
2142 /*
2143 * If we emulate a VMRUN/#VMEXIT in the same host #vmexit cycle we have
2144 * to make sure that we do not lose injected events. So check event_inj
2145 * here and copy it to exit_int_info if it is valid.
2146 * Exit_int_info and event_inj can't be both valid because the case
2147 * below only happens on a VMRUN instruction intercept which has
2148 * no valid exit_int_info set.
2149 */
2150 if (vmcb->control.event_inj & SVM_EVTINJ_VALID) {
2151 struct vmcb_control_area *nc = &nested_vmcb->control;
2152
2153 nc->exit_int_info = vmcb->control.event_inj;
2154 nc->exit_int_info_err = vmcb->control.event_inj_err;
2155 }
2156
33740e40
JR
2157 nested_vmcb->control.tlb_ctl = 0;
2158 nested_vmcb->control.event_inj = 0;
2159 nested_vmcb->control.event_inj_err = 0;
cf74a78b
AG
2160
2161 /* We always set V_INTR_MASKING and remember the old value in hflags */
2162 if (!(svm->vcpu.arch.hflags & HF_VINTR_MASK))
2163 nested_vmcb->control.int_ctl &= ~V_INTR_MASKING_MASK;
2164
cf74a78b 2165 /* Restore the original control entries */
0460a979 2166 copy_vmcb_control_area(vmcb, hsave);
cf74a78b 2167
219b65dc
AG
2168 kvm_clear_exception_queue(&svm->vcpu);
2169 kvm_clear_interrupt_queue(&svm->vcpu);
cf74a78b 2170
4b16184c
JR
2171 svm->nested.nested_cr3 = 0;
2172
cf74a78b
AG
2173 /* Restore selected save entries */
2174 svm->vmcb->save.es = hsave->save.es;
2175 svm->vmcb->save.cs = hsave->save.cs;
2176 svm->vmcb->save.ss = hsave->save.ss;
2177 svm->vmcb->save.ds = hsave->save.ds;
2178 svm->vmcb->save.gdtr = hsave->save.gdtr;
2179 svm->vmcb->save.idtr = hsave->save.idtr;
2180 svm->vmcb->save.rflags = hsave->save.rflags;
2181 svm_set_efer(&svm->vcpu, hsave->save.efer);
2182 svm_set_cr0(&svm->vcpu, hsave->save.cr0 | X86_CR0_PE);
2183 svm_set_cr4(&svm->vcpu, hsave->save.cr4);
2184 if (npt_enabled) {
2185 svm->vmcb->save.cr3 = hsave->save.cr3;
2186 svm->vcpu.arch.cr3 = hsave->save.cr3;
2187 } else {
2390218b 2188 (void)kvm_set_cr3(&svm->vcpu, hsave->save.cr3);
cf74a78b
AG
2189 }
2190 kvm_register_write(&svm->vcpu, VCPU_REGS_RAX, hsave->save.rax);
2191 kvm_register_write(&svm->vcpu, VCPU_REGS_RSP, hsave->save.rsp);
2192 kvm_register_write(&svm->vcpu, VCPU_REGS_RIP, hsave->save.rip);
2193 svm->vmcb->save.dr7 = 0;
2194 svm->vmcb->save.cpl = 0;
2195 svm->vmcb->control.exit_int_info = 0;
2196
8d28fec4
RJ
2197 mark_all_dirty(svm->vmcb);
2198
7597f129 2199 nested_svm_unmap(page);
cf74a78b 2200
4b16184c 2201 nested_svm_uninit_mmu_context(&svm->vcpu);
cf74a78b
AG
2202 kvm_mmu_reset_context(&svm->vcpu);
2203 kvm_mmu_load(&svm->vcpu);
2204
2205 return 0;
2206}
3d6368ef 2207
9738b2c9 2208static bool nested_svm_vmrun_msrpm(struct vcpu_svm *svm)
3d6368ef 2209{
323c3d80
JR
2210 /*
2211 * This function merges the msr permission bitmaps of kvm and the
2212 * nested vmcb. It is omptimized in that it only merges the parts where
2213 * the kvm msr permission bitmap may contain zero bits
2214 */
3d6368ef 2215 int i;
9738b2c9 2216
323c3d80
JR
2217 if (!(svm->nested.intercept & (1ULL << INTERCEPT_MSR_PROT)))
2218 return true;
9738b2c9 2219
323c3d80
JR
2220 for (i = 0; i < MSRPM_OFFSETS; i++) {
2221 u32 value, p;
2222 u64 offset;
9738b2c9 2223
323c3d80
JR
2224 if (msrpm_offsets[i] == 0xffffffff)
2225 break;
3d6368ef 2226
0d6b3537
JR
2227 p = msrpm_offsets[i];
2228 offset = svm->nested.vmcb_msrpm + (p * 4);
323c3d80
JR
2229
2230 if (kvm_read_guest(svm->vcpu.kvm, offset, &value, 4))
2231 return false;
2232
2233 svm->nested.msrpm[p] = svm->msrpm[p] | value;
2234 }
3d6368ef 2235
323c3d80 2236 svm->vmcb->control.msrpm_base_pa = __pa(svm->nested.msrpm);
9738b2c9
JR
2237
2238 return true;
3d6368ef
AG
2239}
2240
52c65a30
JR
2241static bool nested_vmcb_checks(struct vmcb *vmcb)
2242{
2243 if ((vmcb->control.intercept & (1ULL << INTERCEPT_VMRUN)) == 0)
2244 return false;
2245
dbe77584
JR
2246 if (vmcb->control.asid == 0)
2247 return false;
2248
4b16184c
JR
2249 if (vmcb->control.nested_ctl && !npt_enabled)
2250 return false;
2251
52c65a30
JR
2252 return true;
2253}
2254
9738b2c9 2255static bool nested_svm_vmrun(struct vcpu_svm *svm)
3d6368ef 2256{
9738b2c9 2257 struct vmcb *nested_vmcb;
e6aa9abd 2258 struct vmcb *hsave = svm->nested.hsave;
defbba56 2259 struct vmcb *vmcb = svm->vmcb;
7597f129 2260 struct page *page;
06fc7772 2261 u64 vmcb_gpa;
3d6368ef 2262
06fc7772 2263 vmcb_gpa = svm->vmcb->save.rax;
3d6368ef 2264
7597f129 2265 nested_vmcb = nested_svm_map(svm, svm->vmcb->save.rax, &page);
9738b2c9
JR
2266 if (!nested_vmcb)
2267 return false;
2268
52c65a30
JR
2269 if (!nested_vmcb_checks(nested_vmcb)) {
2270 nested_vmcb->control.exit_code = SVM_EXIT_ERR;
2271 nested_vmcb->control.exit_code_hi = 0;
2272 nested_vmcb->control.exit_info_1 = 0;
2273 nested_vmcb->control.exit_info_2 = 0;
2274
2275 nested_svm_unmap(page);
2276
2277 return false;
2278 }
2279
b75f4eb3 2280 trace_kvm_nested_vmrun(svm->vmcb->save.rip, vmcb_gpa,
0ac406de
JR
2281 nested_vmcb->save.rip,
2282 nested_vmcb->control.int_ctl,
2283 nested_vmcb->control.event_inj,
2284 nested_vmcb->control.nested_ctl);
2285
4ee546b4
RJ
2286 trace_kvm_nested_intercepts(nested_vmcb->control.intercept_cr & 0xffff,
2287 nested_vmcb->control.intercept_cr >> 16,
2e554e8d
JR
2288 nested_vmcb->control.intercept_exceptions,
2289 nested_vmcb->control.intercept);
2290
3d6368ef 2291 /* Clear internal status */
219b65dc
AG
2292 kvm_clear_exception_queue(&svm->vcpu);
2293 kvm_clear_interrupt_queue(&svm->vcpu);
3d6368ef 2294
e0231715
JR
2295 /*
2296 * Save the old vmcb, so we don't need to pick what we save, but can
2297 * restore everything when a VMEXIT occurs
2298 */
defbba56
JR
2299 hsave->save.es = vmcb->save.es;
2300 hsave->save.cs = vmcb->save.cs;
2301 hsave->save.ss = vmcb->save.ss;
2302 hsave->save.ds = vmcb->save.ds;
2303 hsave->save.gdtr = vmcb->save.gdtr;
2304 hsave->save.idtr = vmcb->save.idtr;
f6801dff 2305 hsave->save.efer = svm->vcpu.arch.efer;
4d4ec087 2306 hsave->save.cr0 = kvm_read_cr0(&svm->vcpu);
defbba56
JR
2307 hsave->save.cr4 = svm->vcpu.arch.cr4;
2308 hsave->save.rflags = vmcb->save.rflags;
b75f4eb3 2309 hsave->save.rip = kvm_rip_read(&svm->vcpu);
defbba56
JR
2310 hsave->save.rsp = vmcb->save.rsp;
2311 hsave->save.rax = vmcb->save.rax;
2312 if (npt_enabled)
2313 hsave->save.cr3 = vmcb->save.cr3;
2314 else
2315 hsave->save.cr3 = svm->vcpu.arch.cr3;
2316
0460a979 2317 copy_vmcb_control_area(hsave, vmcb);
3d6368ef
AG
2318
2319 if (svm->vmcb->save.rflags & X86_EFLAGS_IF)
2320 svm->vcpu.arch.hflags |= HF_HIF_MASK;
2321 else
2322 svm->vcpu.arch.hflags &= ~HF_HIF_MASK;
2323
4b16184c
JR
2324 if (nested_vmcb->control.nested_ctl) {
2325 kvm_mmu_unload(&svm->vcpu);
2326 svm->nested.nested_cr3 = nested_vmcb->control.nested_cr3;
2327 nested_svm_init_mmu_context(&svm->vcpu);
2328 }
2329
3d6368ef
AG
2330 /* Load the nested guest state */
2331 svm->vmcb->save.es = nested_vmcb->save.es;
2332 svm->vmcb->save.cs = nested_vmcb->save.cs;
2333 svm->vmcb->save.ss = nested_vmcb->save.ss;
2334 svm->vmcb->save.ds = nested_vmcb->save.ds;
2335 svm->vmcb->save.gdtr = nested_vmcb->save.gdtr;
2336 svm->vmcb->save.idtr = nested_vmcb->save.idtr;
2337 svm->vmcb->save.rflags = nested_vmcb->save.rflags;
2338 svm_set_efer(&svm->vcpu, nested_vmcb->save.efer);
2339 svm_set_cr0(&svm->vcpu, nested_vmcb->save.cr0);
2340 svm_set_cr4(&svm->vcpu, nested_vmcb->save.cr4);
2341 if (npt_enabled) {
2342 svm->vmcb->save.cr3 = nested_vmcb->save.cr3;
2343 svm->vcpu.arch.cr3 = nested_vmcb->save.cr3;
0e5cbe36 2344 } else
2390218b 2345 (void)kvm_set_cr3(&svm->vcpu, nested_vmcb->save.cr3);
0e5cbe36
JR
2346
2347 /* Guest paging mode is active - reset mmu */
2348 kvm_mmu_reset_context(&svm->vcpu);
2349
defbba56 2350 svm->vmcb->save.cr2 = svm->vcpu.arch.cr2 = nested_vmcb->save.cr2;
3d6368ef
AG
2351 kvm_register_write(&svm->vcpu, VCPU_REGS_RAX, nested_vmcb->save.rax);
2352 kvm_register_write(&svm->vcpu, VCPU_REGS_RSP, nested_vmcb->save.rsp);
2353 kvm_register_write(&svm->vcpu, VCPU_REGS_RIP, nested_vmcb->save.rip);
e0231715 2354
3d6368ef
AG
2355 /* In case we don't even reach vcpu_run, the fields are not updated */
2356 svm->vmcb->save.rax = nested_vmcb->save.rax;
2357 svm->vmcb->save.rsp = nested_vmcb->save.rsp;
2358 svm->vmcb->save.rip = nested_vmcb->save.rip;
2359 svm->vmcb->save.dr7 = nested_vmcb->save.dr7;
2360 svm->vmcb->save.dr6 = nested_vmcb->save.dr6;
2361 svm->vmcb->save.cpl = nested_vmcb->save.cpl;
2362
f7138538 2363 svm->nested.vmcb_msrpm = nested_vmcb->control.msrpm_base_pa & ~0x0fffULL;
ce2ac085 2364 svm->nested.vmcb_iopm = nested_vmcb->control.iopm_base_pa & ~0x0fffULL;
3d6368ef 2365
aad42c64 2366 /* cache intercepts */
4ee546b4 2367 svm->nested.intercept_cr = nested_vmcb->control.intercept_cr;
3aed041a 2368 svm->nested.intercept_dr = nested_vmcb->control.intercept_dr;
aad42c64
JR
2369 svm->nested.intercept_exceptions = nested_vmcb->control.intercept_exceptions;
2370 svm->nested.intercept = nested_vmcb->control.intercept;
2371
3d6368ef 2372 force_new_asid(&svm->vcpu);
3d6368ef 2373 svm->vmcb->control.int_ctl = nested_vmcb->control.int_ctl | V_INTR_MASKING_MASK;
3d6368ef
AG
2374 if (nested_vmcb->control.int_ctl & V_INTR_MASKING_MASK)
2375 svm->vcpu.arch.hflags |= HF_VINTR_MASK;
2376 else
2377 svm->vcpu.arch.hflags &= ~HF_VINTR_MASK;
2378
88ab24ad
JR
2379 if (svm->vcpu.arch.hflags & HF_VINTR_MASK) {
2380 /* We only want the cr8 intercept bits of the guest */
4ee546b4
RJ
2381 clr_cr_intercept(svm, INTERCEPT_CR8_READ);
2382 clr_cr_intercept(svm, INTERCEPT_CR8_WRITE);
88ab24ad
JR
2383 }
2384
0d945bd9 2385 /* We don't want to see VMMCALLs from a nested guest */
8a05a1b8 2386 clr_intercept(svm, INTERCEPT_VMMCALL);
0d945bd9 2387
88ab24ad 2388 svm->vmcb->control.lbr_ctl = nested_vmcb->control.lbr_ctl;
3d6368ef
AG
2389 svm->vmcb->control.int_vector = nested_vmcb->control.int_vector;
2390 svm->vmcb->control.int_state = nested_vmcb->control.int_state;
2391 svm->vmcb->control.tsc_offset += nested_vmcb->control.tsc_offset;
3d6368ef
AG
2392 svm->vmcb->control.event_inj = nested_vmcb->control.event_inj;
2393 svm->vmcb->control.event_inj_err = nested_vmcb->control.event_inj_err;
2394
7597f129 2395 nested_svm_unmap(page);
9738b2c9 2396
2030753d
JR
2397 /* Enter Guest-Mode */
2398 enter_guest_mode(&svm->vcpu);
2399
384c6368
JR
2400 /*
2401 * Merge guest and host intercepts - must be called with vcpu in
2402 * guest-mode to take affect here
2403 */
2404 recalc_intercepts(svm);
2405
06fc7772 2406 svm->nested.vmcb = vmcb_gpa;
9738b2c9 2407
2af9194d 2408 enable_gif(svm);
3d6368ef 2409
8d28fec4
RJ
2410 mark_all_dirty(svm->vmcb);
2411
9738b2c9 2412 return true;
3d6368ef
AG
2413}
2414
9966bf68 2415static void nested_svm_vmloadsave(struct vmcb *from_vmcb, struct vmcb *to_vmcb)
5542675b
AG
2416{
2417 to_vmcb->save.fs = from_vmcb->save.fs;
2418 to_vmcb->save.gs = from_vmcb->save.gs;
2419 to_vmcb->save.tr = from_vmcb->save.tr;
2420 to_vmcb->save.ldtr = from_vmcb->save.ldtr;
2421 to_vmcb->save.kernel_gs_base = from_vmcb->save.kernel_gs_base;
2422 to_vmcb->save.star = from_vmcb->save.star;
2423 to_vmcb->save.lstar = from_vmcb->save.lstar;
2424 to_vmcb->save.cstar = from_vmcb->save.cstar;
2425 to_vmcb->save.sfmask = from_vmcb->save.sfmask;
2426 to_vmcb->save.sysenter_cs = from_vmcb->save.sysenter_cs;
2427 to_vmcb->save.sysenter_esp = from_vmcb->save.sysenter_esp;
2428 to_vmcb->save.sysenter_eip = from_vmcb->save.sysenter_eip;
5542675b
AG
2429}
2430
851ba692 2431static int vmload_interception(struct vcpu_svm *svm)
5542675b 2432{
9966bf68 2433 struct vmcb *nested_vmcb;
7597f129 2434 struct page *page;
9966bf68 2435
5542675b
AG
2436 if (nested_svm_check_permissions(svm))
2437 return 1;
2438
2439 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
2440 skip_emulated_instruction(&svm->vcpu);
2441
7597f129 2442 nested_vmcb = nested_svm_map(svm, svm->vmcb->save.rax, &page);
9966bf68
JR
2443 if (!nested_vmcb)
2444 return 1;
2445
2446 nested_svm_vmloadsave(nested_vmcb, svm->vmcb);
7597f129 2447 nested_svm_unmap(page);
5542675b
AG
2448
2449 return 1;
2450}
2451
851ba692 2452static int vmsave_interception(struct vcpu_svm *svm)
5542675b 2453{
9966bf68 2454 struct vmcb *nested_vmcb;
7597f129 2455 struct page *page;
9966bf68 2456
5542675b
AG
2457 if (nested_svm_check_permissions(svm))
2458 return 1;
2459
2460 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
2461 skip_emulated_instruction(&svm->vcpu);
2462
7597f129 2463 nested_vmcb = nested_svm_map(svm, svm->vmcb->save.rax, &page);
9966bf68
JR
2464 if (!nested_vmcb)
2465 return 1;
2466
2467 nested_svm_vmloadsave(svm->vmcb, nested_vmcb);
7597f129 2468 nested_svm_unmap(page);
5542675b
AG
2469
2470 return 1;
2471}
2472
851ba692 2473static int vmrun_interception(struct vcpu_svm *svm)
3d6368ef 2474{
3d6368ef
AG
2475 if (nested_svm_check_permissions(svm))
2476 return 1;
2477
b75f4eb3
RJ
2478 /* Save rip after vmrun instruction */
2479 kvm_rip_write(&svm->vcpu, kvm_rip_read(&svm->vcpu) + 3);
3d6368ef 2480
9738b2c9 2481 if (!nested_svm_vmrun(svm))
3d6368ef
AG
2482 return 1;
2483
9738b2c9 2484 if (!nested_svm_vmrun_msrpm(svm))
1f8da478
JR
2485 goto failed;
2486
2487 return 1;
2488
2489failed:
2490
2491 svm->vmcb->control.exit_code = SVM_EXIT_ERR;
2492 svm->vmcb->control.exit_code_hi = 0;
2493 svm->vmcb->control.exit_info_1 = 0;
2494 svm->vmcb->control.exit_info_2 = 0;
2495
2496 nested_svm_vmexit(svm);
3d6368ef
AG
2497
2498 return 1;
2499}
2500
851ba692 2501static int stgi_interception(struct vcpu_svm *svm)
1371d904
AG
2502{
2503 if (nested_svm_check_permissions(svm))
2504 return 1;
2505
2506 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
2507 skip_emulated_instruction(&svm->vcpu);
3842d135 2508 kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
1371d904 2509
2af9194d 2510 enable_gif(svm);
1371d904
AG
2511
2512 return 1;
2513}
2514
851ba692 2515static int clgi_interception(struct vcpu_svm *svm)
1371d904
AG
2516{
2517 if (nested_svm_check_permissions(svm))
2518 return 1;
2519
2520 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
2521 skip_emulated_instruction(&svm->vcpu);
2522
2af9194d 2523 disable_gif(svm);
1371d904
AG
2524
2525 /* After a CLGI no interrupts should come */
2526 svm_clear_vintr(svm);
2527 svm->vmcb->control.int_ctl &= ~V_IRQ_MASK;
2528
decdbf6a
JR
2529 mark_dirty(svm->vmcb, VMCB_INTR);
2530
1371d904
AG
2531 return 1;
2532}
2533
851ba692 2534static int invlpga_interception(struct vcpu_svm *svm)
ff092385
AG
2535{
2536 struct kvm_vcpu *vcpu = &svm->vcpu;
ff092385 2537
ec1ff790
JR
2538 trace_kvm_invlpga(svm->vmcb->save.rip, vcpu->arch.regs[VCPU_REGS_RCX],
2539 vcpu->arch.regs[VCPU_REGS_RAX]);
2540
ff092385
AG
2541 /* Let's treat INVLPGA the same as INVLPG (can be optimized!) */
2542 kvm_mmu_invlpg(vcpu, vcpu->arch.regs[VCPU_REGS_RAX]);
2543
2544 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
2545 skip_emulated_instruction(&svm->vcpu);
2546 return 1;
2547}
2548
532a46b9
JR
2549static int skinit_interception(struct vcpu_svm *svm)
2550{
2551 trace_kvm_skinit(svm->vmcb->save.rip, svm->vcpu.arch.regs[VCPU_REGS_RAX]);
2552
2553 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
2554 return 1;
2555}
2556
851ba692 2557static int invalid_op_interception(struct vcpu_svm *svm)
6aa8b732 2558{
7ee5d940 2559 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
6aa8b732
AK
2560 return 1;
2561}
2562
851ba692 2563static int task_switch_interception(struct vcpu_svm *svm)
6aa8b732 2564{
37817f29 2565 u16 tss_selector;
64a7ec06
GN
2566 int reason;
2567 int int_type = svm->vmcb->control.exit_int_info &
2568 SVM_EXITINTINFO_TYPE_MASK;
8317c298 2569 int int_vec = svm->vmcb->control.exit_int_info & SVM_EVTINJ_VEC_MASK;
fe8e7f83
GN
2570 uint32_t type =
2571 svm->vmcb->control.exit_int_info & SVM_EXITINTINFO_TYPE_MASK;
2572 uint32_t idt_v =
2573 svm->vmcb->control.exit_int_info & SVM_EXITINTINFO_VALID;
e269fb21
JK
2574 bool has_error_code = false;
2575 u32 error_code = 0;
37817f29
IE
2576
2577 tss_selector = (u16)svm->vmcb->control.exit_info_1;
64a7ec06 2578
37817f29
IE
2579 if (svm->vmcb->control.exit_info_2 &
2580 (1ULL << SVM_EXITINFOSHIFT_TS_REASON_IRET))
64a7ec06
GN
2581 reason = TASK_SWITCH_IRET;
2582 else if (svm->vmcb->control.exit_info_2 &
2583 (1ULL << SVM_EXITINFOSHIFT_TS_REASON_JMP))
2584 reason = TASK_SWITCH_JMP;
fe8e7f83 2585 else if (idt_v)
64a7ec06
GN
2586 reason = TASK_SWITCH_GATE;
2587 else
2588 reason = TASK_SWITCH_CALL;
2589
fe8e7f83
GN
2590 if (reason == TASK_SWITCH_GATE) {
2591 switch (type) {
2592 case SVM_EXITINTINFO_TYPE_NMI:
2593 svm->vcpu.arch.nmi_injected = false;
2594 break;
2595 case SVM_EXITINTINFO_TYPE_EXEPT:
e269fb21
JK
2596 if (svm->vmcb->control.exit_info_2 &
2597 (1ULL << SVM_EXITINFOSHIFT_TS_HAS_ERROR_CODE)) {
2598 has_error_code = true;
2599 error_code =
2600 (u32)svm->vmcb->control.exit_info_2;
2601 }
fe8e7f83
GN
2602 kvm_clear_exception_queue(&svm->vcpu);
2603 break;
2604 case SVM_EXITINTINFO_TYPE_INTR:
2605 kvm_clear_interrupt_queue(&svm->vcpu);
2606 break;
2607 default:
2608 break;
2609 }
2610 }
64a7ec06 2611
8317c298
GN
2612 if (reason != TASK_SWITCH_GATE ||
2613 int_type == SVM_EXITINTINFO_TYPE_SOFT ||
2614 (int_type == SVM_EXITINTINFO_TYPE_EXEPT &&
f629cf84
GN
2615 (int_vec == OF_VECTOR || int_vec == BP_VECTOR)))
2616 skip_emulated_instruction(&svm->vcpu);
64a7ec06 2617
acb54517
GN
2618 if (kvm_task_switch(&svm->vcpu, tss_selector, reason,
2619 has_error_code, error_code) == EMULATE_FAIL) {
2620 svm->vcpu.run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
2621 svm->vcpu.run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
2622 svm->vcpu.run->internal.ndata = 0;
2623 return 0;
2624 }
2625 return 1;
6aa8b732
AK
2626}
2627
851ba692 2628static int cpuid_interception(struct vcpu_svm *svm)
6aa8b732 2629{
5fdbf976 2630 svm->next_rip = kvm_rip_read(&svm->vcpu) + 2;
e756fc62 2631 kvm_emulate_cpuid(&svm->vcpu);
06465c5a 2632 return 1;
6aa8b732
AK
2633}
2634
851ba692 2635static int iret_interception(struct vcpu_svm *svm)
95ba8273
GN
2636{
2637 ++svm->vcpu.stat.nmi_window_exits;
8a05a1b8 2638 clr_intercept(svm, INTERCEPT_IRET);
44c11430 2639 svm->vcpu.arch.hflags |= HF_IRET_MASK;
95ba8273
GN
2640 return 1;
2641}
2642
851ba692 2643static int invlpg_interception(struct vcpu_svm *svm)
a7052897 2644{
6d77dbfc 2645 return emulate_instruction(&svm->vcpu, 0, 0, 0) == EMULATE_DONE;
a7052897
MT
2646}
2647
851ba692 2648static int emulate_on_interception(struct vcpu_svm *svm)
6aa8b732 2649{
6d77dbfc 2650 return emulate_instruction(&svm->vcpu, 0, 0, 0) == EMULATE_DONE;
6aa8b732
AK
2651}
2652
cda00082
JR
2653static int cr0_write_interception(struct vcpu_svm *svm)
2654{
2655 struct kvm_vcpu *vcpu = &svm->vcpu;
2656 int r;
2657
2658 r = emulate_instruction(&svm->vcpu, 0, 0, 0);
2659
2660 if (svm->nested.vmexit_rip) {
2661 kvm_register_write(vcpu, VCPU_REGS_RIP, svm->nested.vmexit_rip);
2662 kvm_register_write(vcpu, VCPU_REGS_RSP, svm->nested.vmexit_rsp);
2663 kvm_register_write(vcpu, VCPU_REGS_RAX, svm->nested.vmexit_rax);
2664 svm->nested.vmexit_rip = 0;
2665 }
2666
2667 return r == EMULATE_DONE;
2668}
2669
851ba692 2670static int cr8_write_interception(struct vcpu_svm *svm)
1d075434 2671{
851ba692
AK
2672 struct kvm_run *kvm_run = svm->vcpu.run;
2673
0a5fff19
GN
2674 u8 cr8_prev = kvm_get_cr8(&svm->vcpu);
2675 /* instruction emulation calls kvm_set_cr8() */
851ba692 2676 emulate_instruction(&svm->vcpu, 0, 0, 0);
95ba8273 2677 if (irqchip_in_kernel(svm->vcpu.kvm)) {
4ee546b4 2678 clr_cr_intercept(svm, INTERCEPT_CR8_WRITE);
1d075434 2679 return 1;
95ba8273 2680 }
0a5fff19
GN
2681 if (cr8_prev <= kvm_get_cr8(&svm->vcpu))
2682 return 1;
1d075434
JR
2683 kvm_run->exit_reason = KVM_EXIT_SET_TPR;
2684 return 0;
2685}
2686
6aa8b732
AK
2687static int svm_get_msr(struct kvm_vcpu *vcpu, unsigned ecx, u64 *data)
2688{
a2fa3e9f
GH
2689 struct vcpu_svm *svm = to_svm(vcpu);
2690
6aa8b732 2691 switch (ecx) {
af24a4e4 2692 case MSR_IA32_TSC: {
4cc70310 2693 struct vmcb *vmcb = get_host_vmcb(svm);
6aa8b732 2694
4cc70310 2695 *data = vmcb->control.tsc_offset + native_read_tsc();
6aa8b732
AK
2696 break;
2697 }
8c06585d 2698 case MSR_STAR:
a2fa3e9f 2699 *data = svm->vmcb->save.star;
6aa8b732 2700 break;
0e859cac 2701#ifdef CONFIG_X86_64
6aa8b732 2702 case MSR_LSTAR:
a2fa3e9f 2703 *data = svm->vmcb->save.lstar;
6aa8b732
AK
2704 break;
2705 case MSR_CSTAR:
a2fa3e9f 2706 *data = svm->vmcb->save.cstar;
6aa8b732
AK
2707 break;
2708 case MSR_KERNEL_GS_BASE:
a2fa3e9f 2709 *data = svm->vmcb->save.kernel_gs_base;
6aa8b732
AK
2710 break;
2711 case MSR_SYSCALL_MASK:
a2fa3e9f 2712 *data = svm->vmcb->save.sfmask;
6aa8b732
AK
2713 break;
2714#endif
2715 case MSR_IA32_SYSENTER_CS:
a2fa3e9f 2716 *data = svm->vmcb->save.sysenter_cs;
6aa8b732
AK
2717 break;
2718 case MSR_IA32_SYSENTER_EIP:
017cb99e 2719 *data = svm->sysenter_eip;
6aa8b732
AK
2720 break;
2721 case MSR_IA32_SYSENTER_ESP:
017cb99e 2722 *data = svm->sysenter_esp;
6aa8b732 2723 break;
e0231715
JR
2724 /*
2725 * Nobody will change the following 5 values in the VMCB so we can
2726 * safely return them on rdmsr. They will always be 0 until LBRV is
2727 * implemented.
2728 */
a2938c80
JR
2729 case MSR_IA32_DEBUGCTLMSR:
2730 *data = svm->vmcb->save.dbgctl;
2731 break;
2732 case MSR_IA32_LASTBRANCHFROMIP:
2733 *data = svm->vmcb->save.br_from;
2734 break;
2735 case MSR_IA32_LASTBRANCHTOIP:
2736 *data = svm->vmcb->save.br_to;
2737 break;
2738 case MSR_IA32_LASTINTFROMIP:
2739 *data = svm->vmcb->save.last_excp_from;
2740 break;
2741 case MSR_IA32_LASTINTTOIP:
2742 *data = svm->vmcb->save.last_excp_to;
2743 break;
b286d5d8 2744 case MSR_VM_HSAVE_PA:
e6aa9abd 2745 *data = svm->nested.hsave_msr;
b286d5d8 2746 break;
eb6f302e 2747 case MSR_VM_CR:
4a810181 2748 *data = svm->nested.vm_cr_msr;
eb6f302e 2749 break;
c8a73f18
AG
2750 case MSR_IA32_UCODE_REV:
2751 *data = 0x01000065;
2752 break;
6aa8b732 2753 default:
3bab1f5d 2754 return kvm_get_msr_common(vcpu, ecx, data);
6aa8b732
AK
2755 }
2756 return 0;
2757}
2758
851ba692 2759static int rdmsr_interception(struct vcpu_svm *svm)
6aa8b732 2760{
ad312c7c 2761 u32 ecx = svm->vcpu.arch.regs[VCPU_REGS_RCX];
6aa8b732
AK
2762 u64 data;
2763
59200273
AK
2764 if (svm_get_msr(&svm->vcpu, ecx, &data)) {
2765 trace_kvm_msr_read_ex(ecx);
c1a5d4f9 2766 kvm_inject_gp(&svm->vcpu, 0);
59200273 2767 } else {
229456fc 2768 trace_kvm_msr_read(ecx, data);
af9ca2d7 2769
5fdbf976 2770 svm->vcpu.arch.regs[VCPU_REGS_RAX] = data & 0xffffffff;
ad312c7c 2771 svm->vcpu.arch.regs[VCPU_REGS_RDX] = data >> 32;
5fdbf976 2772 svm->next_rip = kvm_rip_read(&svm->vcpu) + 2;
e756fc62 2773 skip_emulated_instruction(&svm->vcpu);
6aa8b732
AK
2774 }
2775 return 1;
2776}
2777
4a810181
JR
2778static int svm_set_vm_cr(struct kvm_vcpu *vcpu, u64 data)
2779{
2780 struct vcpu_svm *svm = to_svm(vcpu);
2781 int svm_dis, chg_mask;
2782
2783 if (data & ~SVM_VM_CR_VALID_MASK)
2784 return 1;
2785
2786 chg_mask = SVM_VM_CR_VALID_MASK;
2787
2788 if (svm->nested.vm_cr_msr & SVM_VM_CR_SVM_DIS_MASK)
2789 chg_mask &= ~(SVM_VM_CR_SVM_LOCK_MASK | SVM_VM_CR_SVM_DIS_MASK);
2790
2791 svm->nested.vm_cr_msr &= ~chg_mask;
2792 svm->nested.vm_cr_msr |= (data & chg_mask);
2793
2794 svm_dis = svm->nested.vm_cr_msr & SVM_VM_CR_SVM_DIS_MASK;
2795
2796 /* check for svm_disable while efer.svme is set */
2797 if (svm_dis && (vcpu->arch.efer & EFER_SVME))
2798 return 1;
2799
2800 return 0;
2801}
2802
6aa8b732
AK
2803static int svm_set_msr(struct kvm_vcpu *vcpu, unsigned ecx, u64 data)
2804{
a2fa3e9f
GH
2805 struct vcpu_svm *svm = to_svm(vcpu);
2806
6aa8b732 2807 switch (ecx) {
f4e1b3c8 2808 case MSR_IA32_TSC:
99e3e30a 2809 kvm_write_tsc(vcpu, data);
6aa8b732 2810 break;
8c06585d 2811 case MSR_STAR:
a2fa3e9f 2812 svm->vmcb->save.star = data;
6aa8b732 2813 break;
49b14f24 2814#ifdef CONFIG_X86_64
6aa8b732 2815 case MSR_LSTAR:
a2fa3e9f 2816 svm->vmcb->save.lstar = data;
6aa8b732
AK
2817 break;
2818 case MSR_CSTAR:
a2fa3e9f 2819 svm->vmcb->save.cstar = data;
6aa8b732
AK
2820 break;
2821 case MSR_KERNEL_GS_BASE:
a2fa3e9f 2822 svm->vmcb->save.kernel_gs_base = data;
6aa8b732
AK
2823 break;
2824 case MSR_SYSCALL_MASK:
a2fa3e9f 2825 svm->vmcb->save.sfmask = data;
6aa8b732
AK
2826 break;
2827#endif
2828 case MSR_IA32_SYSENTER_CS:
a2fa3e9f 2829 svm->vmcb->save.sysenter_cs = data;
6aa8b732
AK
2830 break;
2831 case MSR_IA32_SYSENTER_EIP:
017cb99e 2832 svm->sysenter_eip = data;
a2fa3e9f 2833 svm->vmcb->save.sysenter_eip = data;
6aa8b732
AK
2834 break;
2835 case MSR_IA32_SYSENTER_ESP:
017cb99e 2836 svm->sysenter_esp = data;
a2fa3e9f 2837 svm->vmcb->save.sysenter_esp = data;
6aa8b732 2838 break;
a2938c80 2839 case MSR_IA32_DEBUGCTLMSR:
2a6b20b8 2840 if (!boot_cpu_has(X86_FEATURE_LBRV)) {
24e09cbf 2841 pr_unimpl(vcpu, "%s: MSR_IA32_DEBUGCTL 0x%llx, nop\n",
b8688d51 2842 __func__, data);
24e09cbf
JR
2843 break;
2844 }
2845 if (data & DEBUGCTL_RESERVED_BITS)
2846 return 1;
2847
2848 svm->vmcb->save.dbgctl = data;
2849 if (data & (1ULL<<0))
2850 svm_enable_lbrv(svm);
2851 else
2852 svm_disable_lbrv(svm);
a2938c80 2853 break;
b286d5d8 2854 case MSR_VM_HSAVE_PA:
e6aa9abd 2855 svm->nested.hsave_msr = data;
62b9abaa 2856 break;
3c5d0a44 2857 case MSR_VM_CR:
4a810181 2858 return svm_set_vm_cr(vcpu, data);
3c5d0a44 2859 case MSR_VM_IGNNE:
3c5d0a44
AG
2860 pr_unimpl(vcpu, "unimplemented wrmsr: 0x%x data 0x%llx\n", ecx, data);
2861 break;
6aa8b732 2862 default:
3bab1f5d 2863 return kvm_set_msr_common(vcpu, ecx, data);
6aa8b732
AK
2864 }
2865 return 0;
2866}
2867
851ba692 2868static int wrmsr_interception(struct vcpu_svm *svm)
6aa8b732 2869{
ad312c7c 2870 u32 ecx = svm->vcpu.arch.regs[VCPU_REGS_RCX];
5fdbf976 2871 u64 data = (svm->vcpu.arch.regs[VCPU_REGS_RAX] & -1u)
ad312c7c 2872 | ((u64)(svm->vcpu.arch.regs[VCPU_REGS_RDX] & -1u) << 32);
af9ca2d7 2873
af9ca2d7 2874
5fdbf976 2875 svm->next_rip = kvm_rip_read(&svm->vcpu) + 2;
59200273
AK
2876 if (svm_set_msr(&svm->vcpu, ecx, data)) {
2877 trace_kvm_msr_write_ex(ecx, data);
c1a5d4f9 2878 kvm_inject_gp(&svm->vcpu, 0);
59200273
AK
2879 } else {
2880 trace_kvm_msr_write(ecx, data);
e756fc62 2881 skip_emulated_instruction(&svm->vcpu);
59200273 2882 }
6aa8b732
AK
2883 return 1;
2884}
2885
851ba692 2886static int msr_interception(struct vcpu_svm *svm)
6aa8b732 2887{
e756fc62 2888 if (svm->vmcb->control.exit_info_1)
851ba692 2889 return wrmsr_interception(svm);
6aa8b732 2890 else
851ba692 2891 return rdmsr_interception(svm);
6aa8b732
AK
2892}
2893
851ba692 2894static int interrupt_window_interception(struct vcpu_svm *svm)
c1150d8c 2895{
851ba692
AK
2896 struct kvm_run *kvm_run = svm->vcpu.run;
2897
3842d135 2898 kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
f0b85051 2899 svm_clear_vintr(svm);
85f455f7 2900 svm->vmcb->control.int_ctl &= ~V_IRQ_MASK;
decdbf6a 2901 mark_dirty(svm->vmcb, VMCB_INTR);
c1150d8c
DL
2902 /*
2903 * If the user space waits to inject interrupts, exit as soon as
2904 * possible
2905 */
8061823a
GN
2906 if (!irqchip_in_kernel(svm->vcpu.kvm) &&
2907 kvm_run->request_interrupt_window &&
2908 !kvm_cpu_has_interrupt(&svm->vcpu)) {
e756fc62 2909 ++svm->vcpu.stat.irq_window_exits;
c1150d8c
DL
2910 kvm_run->exit_reason = KVM_EXIT_IRQ_WINDOW_OPEN;
2911 return 0;
2912 }
2913
2914 return 1;
2915}
2916
565d0998
ML
2917static int pause_interception(struct vcpu_svm *svm)
2918{
2919 kvm_vcpu_on_spin(&(svm->vcpu));
2920 return 1;
2921}
2922
851ba692 2923static int (*svm_exit_handlers[])(struct vcpu_svm *svm) = {
e0231715
JR
2924 [SVM_EXIT_READ_CR0] = emulate_on_interception,
2925 [SVM_EXIT_READ_CR3] = emulate_on_interception,
2926 [SVM_EXIT_READ_CR4] = emulate_on_interception,
2927 [SVM_EXIT_READ_CR8] = emulate_on_interception,
d225157b 2928 [SVM_EXIT_CR0_SEL_WRITE] = emulate_on_interception,
cda00082 2929 [SVM_EXIT_WRITE_CR0] = cr0_write_interception,
e0231715
JR
2930 [SVM_EXIT_WRITE_CR3] = emulate_on_interception,
2931 [SVM_EXIT_WRITE_CR4] = emulate_on_interception,
2932 [SVM_EXIT_WRITE_CR8] = cr8_write_interception,
2933 [SVM_EXIT_READ_DR0] = emulate_on_interception,
6aa8b732
AK
2934 [SVM_EXIT_READ_DR1] = emulate_on_interception,
2935 [SVM_EXIT_READ_DR2] = emulate_on_interception,
2936 [SVM_EXIT_READ_DR3] = emulate_on_interception,
727f5a23
JK
2937 [SVM_EXIT_READ_DR4] = emulate_on_interception,
2938 [SVM_EXIT_READ_DR5] = emulate_on_interception,
2939 [SVM_EXIT_READ_DR6] = emulate_on_interception,
2940 [SVM_EXIT_READ_DR7] = emulate_on_interception,
6aa8b732
AK
2941 [SVM_EXIT_WRITE_DR0] = emulate_on_interception,
2942 [SVM_EXIT_WRITE_DR1] = emulate_on_interception,
2943 [SVM_EXIT_WRITE_DR2] = emulate_on_interception,
2944 [SVM_EXIT_WRITE_DR3] = emulate_on_interception,
727f5a23 2945 [SVM_EXIT_WRITE_DR4] = emulate_on_interception,
6aa8b732 2946 [SVM_EXIT_WRITE_DR5] = emulate_on_interception,
727f5a23 2947 [SVM_EXIT_WRITE_DR6] = emulate_on_interception,
6aa8b732 2948 [SVM_EXIT_WRITE_DR7] = emulate_on_interception,
d0bfb940
JK
2949 [SVM_EXIT_EXCP_BASE + DB_VECTOR] = db_interception,
2950 [SVM_EXIT_EXCP_BASE + BP_VECTOR] = bp_interception,
7aa81cc0 2951 [SVM_EXIT_EXCP_BASE + UD_VECTOR] = ud_interception,
e0231715
JR
2952 [SVM_EXIT_EXCP_BASE + PF_VECTOR] = pf_interception,
2953 [SVM_EXIT_EXCP_BASE + NM_VECTOR] = nm_interception,
2954 [SVM_EXIT_EXCP_BASE + MC_VECTOR] = mc_interception,
2955 [SVM_EXIT_INTR] = intr_interception,
c47f098d 2956 [SVM_EXIT_NMI] = nmi_interception,
6aa8b732
AK
2957 [SVM_EXIT_SMI] = nop_on_interception,
2958 [SVM_EXIT_INIT] = nop_on_interception,
c1150d8c 2959 [SVM_EXIT_VINTR] = interrupt_window_interception,
6aa8b732 2960 [SVM_EXIT_CPUID] = cpuid_interception,
95ba8273 2961 [SVM_EXIT_IRET] = iret_interception,
cf5a94d1 2962 [SVM_EXIT_INVD] = emulate_on_interception,
565d0998 2963 [SVM_EXIT_PAUSE] = pause_interception,
6aa8b732 2964 [SVM_EXIT_HLT] = halt_interception,
a7052897 2965 [SVM_EXIT_INVLPG] = invlpg_interception,
ff092385 2966 [SVM_EXIT_INVLPGA] = invlpga_interception,
e0231715 2967 [SVM_EXIT_IOIO] = io_interception,
6aa8b732
AK
2968 [SVM_EXIT_MSR] = msr_interception,
2969 [SVM_EXIT_TASK_SWITCH] = task_switch_interception,
46fe4ddd 2970 [SVM_EXIT_SHUTDOWN] = shutdown_interception,
3d6368ef 2971 [SVM_EXIT_VMRUN] = vmrun_interception,
02e235bc 2972 [SVM_EXIT_VMMCALL] = vmmcall_interception,
5542675b
AG
2973 [SVM_EXIT_VMLOAD] = vmload_interception,
2974 [SVM_EXIT_VMSAVE] = vmsave_interception,
1371d904
AG
2975 [SVM_EXIT_STGI] = stgi_interception,
2976 [SVM_EXIT_CLGI] = clgi_interception,
532a46b9 2977 [SVM_EXIT_SKINIT] = skinit_interception,
cf5a94d1 2978 [SVM_EXIT_WBINVD] = emulate_on_interception,
916ce236
JR
2979 [SVM_EXIT_MONITOR] = invalid_op_interception,
2980 [SVM_EXIT_MWAIT] = invalid_op_interception,
709ddebf 2981 [SVM_EXIT_NPF] = pf_interception,
6aa8b732
AK
2982};
2983
3f10c846
JR
2984void dump_vmcb(struct kvm_vcpu *vcpu)
2985{
2986 struct vcpu_svm *svm = to_svm(vcpu);
2987 struct vmcb_control_area *control = &svm->vmcb->control;
2988 struct vmcb_save_area *save = &svm->vmcb->save;
2989
2990 pr_err("VMCB Control Area:\n");
4ee546b4
RJ
2991 pr_err("cr_read: %04x\n", control->intercept_cr & 0xffff);
2992 pr_err("cr_write: %04x\n", control->intercept_cr >> 16);
3aed041a
JR
2993 pr_err("dr_read: %04x\n", control->intercept_dr & 0xffff);
2994 pr_err("dr_write: %04x\n", control->intercept_dr >> 16);
3f10c846
JR
2995 pr_err("exceptions: %08x\n", control->intercept_exceptions);
2996 pr_err("intercepts: %016llx\n", control->intercept);
2997 pr_err("pause filter count: %d\n", control->pause_filter_count);
2998 pr_err("iopm_base_pa: %016llx\n", control->iopm_base_pa);
2999 pr_err("msrpm_base_pa: %016llx\n", control->msrpm_base_pa);
3000 pr_err("tsc_offset: %016llx\n", control->tsc_offset);
3001 pr_err("asid: %d\n", control->asid);
3002 pr_err("tlb_ctl: %d\n", control->tlb_ctl);
3003 pr_err("int_ctl: %08x\n", control->int_ctl);
3004 pr_err("int_vector: %08x\n", control->int_vector);
3005 pr_err("int_state: %08x\n", control->int_state);
3006 pr_err("exit_code: %08x\n", control->exit_code);
3007 pr_err("exit_info1: %016llx\n", control->exit_info_1);
3008 pr_err("exit_info2: %016llx\n", control->exit_info_2);
3009 pr_err("exit_int_info: %08x\n", control->exit_int_info);
3010 pr_err("exit_int_info_err: %08x\n", control->exit_int_info_err);
3011 pr_err("nested_ctl: %lld\n", control->nested_ctl);
3012 pr_err("nested_cr3: %016llx\n", control->nested_cr3);
3013 pr_err("event_inj: %08x\n", control->event_inj);
3014 pr_err("event_inj_err: %08x\n", control->event_inj_err);
3015 pr_err("lbr_ctl: %lld\n", control->lbr_ctl);
3016 pr_err("next_rip: %016llx\n", control->next_rip);
3017 pr_err("VMCB State Save Area:\n");
3018 pr_err("es: s: %04x a: %04x l: %08x b: %016llx\n",
3019 save->es.selector, save->es.attrib,
3020 save->es.limit, save->es.base);
3021 pr_err("cs: s: %04x a: %04x l: %08x b: %016llx\n",
3022 save->cs.selector, save->cs.attrib,
3023 save->cs.limit, save->cs.base);
3024 pr_err("ss: s: %04x a: %04x l: %08x b: %016llx\n",
3025 save->ss.selector, save->ss.attrib,
3026 save->ss.limit, save->ss.base);
3027 pr_err("ds: s: %04x a: %04x l: %08x b: %016llx\n",
3028 save->ds.selector, save->ds.attrib,
3029 save->ds.limit, save->ds.base);
3030 pr_err("fs: s: %04x a: %04x l: %08x b: %016llx\n",
3031 save->fs.selector, save->fs.attrib,
3032 save->fs.limit, save->fs.base);
3033 pr_err("gs: s: %04x a: %04x l: %08x b: %016llx\n",
3034 save->gs.selector, save->gs.attrib,
3035 save->gs.limit, save->gs.base);
3036 pr_err("gdtr: s: %04x a: %04x l: %08x b: %016llx\n",
3037 save->gdtr.selector, save->gdtr.attrib,
3038 save->gdtr.limit, save->gdtr.base);
3039 pr_err("ldtr: s: %04x a: %04x l: %08x b: %016llx\n",
3040 save->ldtr.selector, save->ldtr.attrib,
3041 save->ldtr.limit, save->ldtr.base);
3042 pr_err("idtr: s: %04x a: %04x l: %08x b: %016llx\n",
3043 save->idtr.selector, save->idtr.attrib,
3044 save->idtr.limit, save->idtr.base);
3045 pr_err("tr: s: %04x a: %04x l: %08x b: %016llx\n",
3046 save->tr.selector, save->tr.attrib,
3047 save->tr.limit, save->tr.base);
3048 pr_err("cpl: %d efer: %016llx\n",
3049 save->cpl, save->efer);
3050 pr_err("cr0: %016llx cr2: %016llx\n",
3051 save->cr0, save->cr2);
3052 pr_err("cr3: %016llx cr4: %016llx\n",
3053 save->cr3, save->cr4);
3054 pr_err("dr6: %016llx dr7: %016llx\n",
3055 save->dr6, save->dr7);
3056 pr_err("rip: %016llx rflags: %016llx\n",
3057 save->rip, save->rflags);
3058 pr_err("rsp: %016llx rax: %016llx\n",
3059 save->rsp, save->rax);
3060 pr_err("star: %016llx lstar: %016llx\n",
3061 save->star, save->lstar);
3062 pr_err("cstar: %016llx sfmask: %016llx\n",
3063 save->cstar, save->sfmask);
3064 pr_err("kernel_gs_base: %016llx sysenter_cs: %016llx\n",
3065 save->kernel_gs_base, save->sysenter_cs);
3066 pr_err("sysenter_esp: %016llx sysenter_eip: %016llx\n",
3067 save->sysenter_esp, save->sysenter_eip);
3068 pr_err("gpat: %016llx dbgctl: %016llx\n",
3069 save->g_pat, save->dbgctl);
3070 pr_err("br_from: %016llx br_to: %016llx\n",
3071 save->br_from, save->br_to);
3072 pr_err("excp_from: %016llx excp_to: %016llx\n",
3073 save->last_excp_from, save->last_excp_to);
3074
3075}
3076
586f9607
AK
3077static void svm_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2)
3078{
3079 struct vmcb_control_area *control = &to_svm(vcpu)->vmcb->control;
3080
3081 *info1 = control->exit_info_1;
3082 *info2 = control->exit_info_2;
3083}
3084
851ba692 3085static int handle_exit(struct kvm_vcpu *vcpu)
6aa8b732 3086{
04d2cc77 3087 struct vcpu_svm *svm = to_svm(vcpu);
851ba692 3088 struct kvm_run *kvm_run = vcpu->run;
a2fa3e9f 3089 u32 exit_code = svm->vmcb->control.exit_code;
6aa8b732 3090
aa17911e 3091 trace_kvm_exit(exit_code, vcpu, KVM_ISA_SVM);
af9ca2d7 3092
4ee546b4 3093 if (!is_cr_intercept(svm, INTERCEPT_CR0_WRITE))
2be4fc7a
JR
3094 vcpu->arch.cr0 = svm->vmcb->save.cr0;
3095 if (npt_enabled)
3096 vcpu->arch.cr3 = svm->vmcb->save.cr3;
af9ca2d7 3097
cd3ff653
JR
3098 if (unlikely(svm->nested.exit_required)) {
3099 nested_svm_vmexit(svm);
3100 svm->nested.exit_required = false;
3101
3102 return 1;
3103 }
3104
2030753d 3105 if (is_guest_mode(vcpu)) {
410e4d57
JR
3106 int vmexit;
3107
d8cabddf
JR
3108 trace_kvm_nested_vmexit(svm->vmcb->save.rip, exit_code,
3109 svm->vmcb->control.exit_info_1,
3110 svm->vmcb->control.exit_info_2,
3111 svm->vmcb->control.exit_int_info,
3112 svm->vmcb->control.exit_int_info_err);
3113
410e4d57
JR
3114 vmexit = nested_svm_exit_special(svm);
3115
3116 if (vmexit == NESTED_EXIT_CONTINUE)
3117 vmexit = nested_svm_exit_handled(svm);
3118
3119 if (vmexit == NESTED_EXIT_DONE)
cf74a78b 3120 return 1;
cf74a78b
AG
3121 }
3122
a5c3832d
JR
3123 svm_complete_interrupts(svm);
3124
04d2cc77
AK
3125 if (svm->vmcb->control.exit_code == SVM_EXIT_ERR) {
3126 kvm_run->exit_reason = KVM_EXIT_FAIL_ENTRY;
3127 kvm_run->fail_entry.hardware_entry_failure_reason
3128 = svm->vmcb->control.exit_code;
3f10c846
JR
3129 pr_err("KVM: FAILED VMRUN WITH VMCB:\n");
3130 dump_vmcb(vcpu);
04d2cc77
AK
3131 return 0;
3132 }
3133
a2fa3e9f 3134 if (is_external_interrupt(svm->vmcb->control.exit_int_info) &&
709ddebf 3135 exit_code != SVM_EXIT_EXCP_BASE + PF_VECTOR &&
55c5e464
JR
3136 exit_code != SVM_EXIT_NPF && exit_code != SVM_EXIT_TASK_SWITCH &&
3137 exit_code != SVM_EXIT_INTR && exit_code != SVM_EXIT_NMI)
6aa8b732
AK
3138 printk(KERN_ERR "%s: unexpected exit_ini_info 0x%x "
3139 "exit_code 0x%x\n",
b8688d51 3140 __func__, svm->vmcb->control.exit_int_info,
6aa8b732
AK
3141 exit_code);
3142
9d8f549d 3143 if (exit_code >= ARRAY_SIZE(svm_exit_handlers)
56919c5c 3144 || !svm_exit_handlers[exit_code]) {
6aa8b732 3145 kvm_run->exit_reason = KVM_EXIT_UNKNOWN;
364b625b 3146 kvm_run->hw.hardware_exit_reason = exit_code;
6aa8b732
AK
3147 return 0;
3148 }
3149
851ba692 3150 return svm_exit_handlers[exit_code](svm);
6aa8b732
AK
3151}
3152
3153static void reload_tss(struct kvm_vcpu *vcpu)
3154{
3155 int cpu = raw_smp_processor_id();
3156
0fe1e009
TH
3157 struct svm_cpu_data *sd = per_cpu(svm_data, cpu);
3158 sd->tss_desc->type = 9; /* available 32/64-bit TSS */
6aa8b732
AK
3159 load_TR_desc();
3160}
3161
e756fc62 3162static void pre_svm_run(struct vcpu_svm *svm)
6aa8b732
AK
3163{
3164 int cpu = raw_smp_processor_id();
3165
0fe1e009 3166 struct svm_cpu_data *sd = per_cpu(svm_data, cpu);
6aa8b732 3167
a2fa3e9f 3168 svm->vmcb->control.tlb_ctl = TLB_CONTROL_DO_NOTHING;
4b656b12 3169 /* FIXME: handle wraparound of asid_generation */
0fe1e009
TH
3170 if (svm->asid_generation != sd->asid_generation)
3171 new_asid(svm, sd);
6aa8b732
AK
3172}
3173
95ba8273
GN
3174static void svm_inject_nmi(struct kvm_vcpu *vcpu)
3175{
3176 struct vcpu_svm *svm = to_svm(vcpu);
3177
3178 svm->vmcb->control.event_inj = SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_NMI;
3179 vcpu->arch.hflags |= HF_NMI_MASK;
8a05a1b8 3180 set_intercept(svm, INTERCEPT_IRET);
95ba8273
GN
3181 ++vcpu->stat.nmi_injections;
3182}
6aa8b732 3183
85f455f7 3184static inline void svm_inject_irq(struct vcpu_svm *svm, int irq)
6aa8b732
AK
3185{
3186 struct vmcb_control_area *control;
3187
e756fc62 3188 control = &svm->vmcb->control;
85f455f7 3189 control->int_vector = irq;
6aa8b732
AK
3190 control->int_ctl &= ~V_INTR_PRIO_MASK;
3191 control->int_ctl |= V_IRQ_MASK |
3192 ((/*control->int_vector >> 4*/ 0xf) << V_INTR_PRIO_SHIFT);
decdbf6a 3193 mark_dirty(svm->vmcb, VMCB_INTR);
6aa8b732
AK
3194}
3195
66fd3f7f 3196static void svm_set_irq(struct kvm_vcpu *vcpu)
2a8067f1
ED
3197{
3198 struct vcpu_svm *svm = to_svm(vcpu);
3199
2af9194d 3200 BUG_ON(!(gif_set(svm)));
cf74a78b 3201
9fb2d2b4
GN
3202 trace_kvm_inj_virq(vcpu->arch.interrupt.nr);
3203 ++vcpu->stat.irq_injections;
3204
219b65dc
AG
3205 svm->vmcb->control.event_inj = vcpu->arch.interrupt.nr |
3206 SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_INTR;
2a8067f1
ED
3207}
3208
95ba8273 3209static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
aaacfc9a
JR
3210{
3211 struct vcpu_svm *svm = to_svm(vcpu);
aaacfc9a 3212
2030753d 3213 if (is_guest_mode(vcpu) && (vcpu->arch.hflags & HF_VINTR_MASK))
88ab24ad
JR
3214 return;
3215
95ba8273 3216 if (irr == -1)
aaacfc9a
JR
3217 return;
3218
95ba8273 3219 if (tpr >= irr)
4ee546b4 3220 set_cr_intercept(svm, INTERCEPT_CR8_WRITE);
95ba8273 3221}
aaacfc9a 3222
95ba8273
GN
3223static int svm_nmi_allowed(struct kvm_vcpu *vcpu)
3224{
3225 struct vcpu_svm *svm = to_svm(vcpu);
3226 struct vmcb *vmcb = svm->vmcb;
924584cc
JR
3227 int ret;
3228 ret = !(vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK) &&
3229 !(svm->vcpu.arch.hflags & HF_NMI_MASK);
3230 ret = ret && gif_set(svm) && nested_svm_nmi(svm);
3231
3232 return ret;
aaacfc9a
JR
3233}
3234
3cfc3092
JK
3235static bool svm_get_nmi_mask(struct kvm_vcpu *vcpu)
3236{
3237 struct vcpu_svm *svm = to_svm(vcpu);
3238
3239 return !!(svm->vcpu.arch.hflags & HF_NMI_MASK);
3240}
3241
3242static void svm_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
3243{
3244 struct vcpu_svm *svm = to_svm(vcpu);
3245
3246 if (masked) {
3247 svm->vcpu.arch.hflags |= HF_NMI_MASK;
8a05a1b8 3248 set_intercept(svm, INTERCEPT_IRET);
3cfc3092
JK
3249 } else {
3250 svm->vcpu.arch.hflags &= ~HF_NMI_MASK;
8a05a1b8 3251 clr_intercept(svm, INTERCEPT_IRET);
3cfc3092
JK
3252 }
3253}
3254
78646121
GN
3255static int svm_interrupt_allowed(struct kvm_vcpu *vcpu)
3256{
3257 struct vcpu_svm *svm = to_svm(vcpu);
3258 struct vmcb *vmcb = svm->vmcb;
7fcdb510
JR
3259 int ret;
3260
3261 if (!gif_set(svm) ||
3262 (vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK))
3263 return 0;
3264
3265 ret = !!(vmcb->save.rflags & X86_EFLAGS_IF);
3266
2030753d 3267 if (is_guest_mode(vcpu))
7fcdb510
JR
3268 return ret && !(svm->vcpu.arch.hflags & HF_VINTR_MASK);
3269
3270 return ret;
78646121
GN
3271}
3272
9222be18 3273static void enable_irq_window(struct kvm_vcpu *vcpu)
6aa8b732 3274{
219b65dc 3275 struct vcpu_svm *svm = to_svm(vcpu);
219b65dc 3276
e0231715
JR
3277 /*
3278 * In case GIF=0 we can't rely on the CPU to tell us when GIF becomes
3279 * 1, because that's a separate STGI/VMRUN intercept. The next time we
3280 * get that intercept, this function will be called again though and
3281 * we'll get the vintr intercept.
3282 */
8fe54654 3283 if (gif_set(svm) && nested_svm_intr(svm)) {
219b65dc
AG
3284 svm_set_vintr(svm);
3285 svm_inject_irq(svm, 0x0);
3286 }
85f455f7
ED
3287}
3288
95ba8273 3289static void enable_nmi_window(struct kvm_vcpu *vcpu)
c1150d8c 3290{
04d2cc77 3291 struct vcpu_svm *svm = to_svm(vcpu);
c1150d8c 3292
44c11430
GN
3293 if ((svm->vcpu.arch.hflags & (HF_NMI_MASK | HF_IRET_MASK))
3294 == HF_NMI_MASK)
3295 return; /* IRET will cause a vm exit */
3296
e0231715
JR
3297 /*
3298 * Something prevents NMI from been injected. Single step over possible
3299 * problem (IRET or exception injection or interrupt shadow)
3300 */
6be7d306 3301 svm->nmi_singlestep = true;
44c11430
GN
3302 svm->vmcb->save.rflags |= (X86_EFLAGS_TF | X86_EFLAGS_RF);
3303 update_db_intercept(vcpu);
c1150d8c
DL
3304}
3305
cbc94022
IE
3306static int svm_set_tss_addr(struct kvm *kvm, unsigned int addr)
3307{
3308 return 0;
3309}
3310
d9e368d6
AK
3311static void svm_flush_tlb(struct kvm_vcpu *vcpu)
3312{
3313 force_new_asid(vcpu);
3314}
3315
04d2cc77
AK
3316static void svm_prepare_guest_switch(struct kvm_vcpu *vcpu)
3317{
3318}
3319
d7bf8221
JR
3320static inline void sync_cr8_to_lapic(struct kvm_vcpu *vcpu)
3321{
3322 struct vcpu_svm *svm = to_svm(vcpu);
3323
2030753d 3324 if (is_guest_mode(vcpu) && (vcpu->arch.hflags & HF_VINTR_MASK))
88ab24ad
JR
3325 return;
3326
4ee546b4 3327 if (!is_cr_intercept(svm, INTERCEPT_CR8_WRITE)) {
d7bf8221 3328 int cr8 = svm->vmcb->control.int_ctl & V_TPR_MASK;
615d5193 3329 kvm_set_cr8(vcpu, cr8);
d7bf8221
JR
3330 }
3331}
3332
649d6864
JR
3333static inline void sync_lapic_to_cr8(struct kvm_vcpu *vcpu)
3334{
3335 struct vcpu_svm *svm = to_svm(vcpu);
3336 u64 cr8;
3337
2030753d 3338 if (is_guest_mode(vcpu) && (vcpu->arch.hflags & HF_VINTR_MASK))
88ab24ad
JR
3339 return;
3340
649d6864
JR
3341 cr8 = kvm_get_cr8(vcpu);
3342 svm->vmcb->control.int_ctl &= ~V_TPR_MASK;
3343 svm->vmcb->control.int_ctl |= cr8 & V_TPR_MASK;
3344}
3345
9222be18
GN
3346static void svm_complete_interrupts(struct vcpu_svm *svm)
3347{
3348 u8 vector;
3349 int type;
3350 u32 exitintinfo = svm->vmcb->control.exit_int_info;
66b7138f
JK
3351 unsigned int3_injected = svm->int3_injected;
3352
3353 svm->int3_injected = 0;
9222be18 3354
3842d135 3355 if (svm->vcpu.arch.hflags & HF_IRET_MASK) {
44c11430 3356 svm->vcpu.arch.hflags &= ~(HF_NMI_MASK | HF_IRET_MASK);
3842d135
AK
3357 kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
3358 }
44c11430 3359
9222be18
GN
3360 svm->vcpu.arch.nmi_injected = false;
3361 kvm_clear_exception_queue(&svm->vcpu);
3362 kvm_clear_interrupt_queue(&svm->vcpu);
3363
3364 if (!(exitintinfo & SVM_EXITINTINFO_VALID))
3365 return;
3366
3842d135
AK
3367 kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
3368
9222be18
GN
3369 vector = exitintinfo & SVM_EXITINTINFO_VEC_MASK;
3370 type = exitintinfo & SVM_EXITINTINFO_TYPE_MASK;
3371
3372 switch (type) {
3373 case SVM_EXITINTINFO_TYPE_NMI:
3374 svm->vcpu.arch.nmi_injected = true;
3375 break;
3376 case SVM_EXITINTINFO_TYPE_EXEPT:
66b7138f
JK
3377 /*
3378 * In case of software exceptions, do not reinject the vector,
3379 * but re-execute the instruction instead. Rewind RIP first
3380 * if we emulated INT3 before.
3381 */
3382 if (kvm_exception_is_soft(vector)) {
3383 if (vector == BP_VECTOR && int3_injected &&
3384 kvm_is_linear_rip(&svm->vcpu, svm->int3_rip))
3385 kvm_rip_write(&svm->vcpu,
3386 kvm_rip_read(&svm->vcpu) -
3387 int3_injected);
9222be18 3388 break;
66b7138f 3389 }
9222be18
GN
3390 if (exitintinfo & SVM_EXITINTINFO_VALID_ERR) {
3391 u32 err = svm->vmcb->control.exit_int_info_err;
ce7ddec4 3392 kvm_requeue_exception_e(&svm->vcpu, vector, err);
9222be18
GN
3393
3394 } else
ce7ddec4 3395 kvm_requeue_exception(&svm->vcpu, vector);
9222be18
GN
3396 break;
3397 case SVM_EXITINTINFO_TYPE_INTR:
66fd3f7f 3398 kvm_queue_interrupt(&svm->vcpu, vector, false);
9222be18
GN
3399 break;
3400 default:
3401 break;
3402 }
3403}
3404
b463a6f7
AK
3405static void svm_cancel_injection(struct kvm_vcpu *vcpu)
3406{
3407 struct vcpu_svm *svm = to_svm(vcpu);
3408 struct vmcb_control_area *control = &svm->vmcb->control;
3409
3410 control->exit_int_info = control->event_inj;
3411 control->exit_int_info_err = control->event_inj_err;
3412 control->event_inj = 0;
3413 svm_complete_interrupts(svm);
3414}
3415
80e31d4f
AK
3416#ifdef CONFIG_X86_64
3417#define R "r"
3418#else
3419#define R "e"
3420#endif
3421
851ba692 3422static void svm_vcpu_run(struct kvm_vcpu *vcpu)
6aa8b732 3423{
a2fa3e9f 3424 struct vcpu_svm *svm = to_svm(vcpu);
d9e368d6 3425
2041a06a
JR
3426 svm->vmcb->save.rax = vcpu->arch.regs[VCPU_REGS_RAX];
3427 svm->vmcb->save.rsp = vcpu->arch.regs[VCPU_REGS_RSP];
3428 svm->vmcb->save.rip = vcpu->arch.regs[VCPU_REGS_RIP];
3429
cd3ff653
JR
3430 /*
3431 * A vmexit emulation is required before the vcpu can be executed
3432 * again.
3433 */
3434 if (unlikely(svm->nested.exit_required))
3435 return;
3436
e756fc62 3437 pre_svm_run(svm);
6aa8b732 3438
649d6864
JR
3439 sync_lapic_to_cr8(vcpu);
3440
cda0ffdd 3441 svm->vmcb->save.cr2 = vcpu->arch.cr2;
6aa8b732 3442
04d2cc77
AK
3443 clgi();
3444
3445 local_irq_enable();
36241b8c 3446
6aa8b732 3447 asm volatile (
80e31d4f
AK
3448 "push %%"R"bp; \n\t"
3449 "mov %c[rbx](%[svm]), %%"R"bx \n\t"
3450 "mov %c[rcx](%[svm]), %%"R"cx \n\t"
3451 "mov %c[rdx](%[svm]), %%"R"dx \n\t"
3452 "mov %c[rsi](%[svm]), %%"R"si \n\t"
3453 "mov %c[rdi](%[svm]), %%"R"di \n\t"
3454 "mov %c[rbp](%[svm]), %%"R"bp \n\t"
05b3e0c2 3455#ifdef CONFIG_X86_64
fb3f0f51
RR
3456 "mov %c[r8](%[svm]), %%r8 \n\t"
3457 "mov %c[r9](%[svm]), %%r9 \n\t"
3458 "mov %c[r10](%[svm]), %%r10 \n\t"
3459 "mov %c[r11](%[svm]), %%r11 \n\t"
3460 "mov %c[r12](%[svm]), %%r12 \n\t"
3461 "mov %c[r13](%[svm]), %%r13 \n\t"
3462 "mov %c[r14](%[svm]), %%r14 \n\t"
3463 "mov %c[r15](%[svm]), %%r15 \n\t"
6aa8b732
AK
3464#endif
3465
6aa8b732 3466 /* Enter guest mode */
80e31d4f
AK
3467 "push %%"R"ax \n\t"
3468 "mov %c[vmcb](%[svm]), %%"R"ax \n\t"
4ecac3fd
AK
3469 __ex(SVM_VMLOAD) "\n\t"
3470 __ex(SVM_VMRUN) "\n\t"
3471 __ex(SVM_VMSAVE) "\n\t"
80e31d4f 3472 "pop %%"R"ax \n\t"
6aa8b732
AK
3473
3474 /* Save guest registers, load host registers */
80e31d4f
AK
3475 "mov %%"R"bx, %c[rbx](%[svm]) \n\t"
3476 "mov %%"R"cx, %c[rcx](%[svm]) \n\t"
3477 "mov %%"R"dx, %c[rdx](%[svm]) \n\t"
3478 "mov %%"R"si, %c[rsi](%[svm]) \n\t"
3479 "mov %%"R"di, %c[rdi](%[svm]) \n\t"
3480 "mov %%"R"bp, %c[rbp](%[svm]) \n\t"
05b3e0c2 3481#ifdef CONFIG_X86_64
fb3f0f51
RR
3482 "mov %%r8, %c[r8](%[svm]) \n\t"
3483 "mov %%r9, %c[r9](%[svm]) \n\t"
3484 "mov %%r10, %c[r10](%[svm]) \n\t"
3485 "mov %%r11, %c[r11](%[svm]) \n\t"
3486 "mov %%r12, %c[r12](%[svm]) \n\t"
3487 "mov %%r13, %c[r13](%[svm]) \n\t"
3488 "mov %%r14, %c[r14](%[svm]) \n\t"
3489 "mov %%r15, %c[r15](%[svm]) \n\t"
6aa8b732 3490#endif
80e31d4f 3491 "pop %%"R"bp"
6aa8b732 3492 :
fb3f0f51 3493 : [svm]"a"(svm),
6aa8b732 3494 [vmcb]"i"(offsetof(struct vcpu_svm, vmcb_pa)),
ad312c7c
ZX
3495 [rbx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RBX])),
3496 [rcx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RCX])),
3497 [rdx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RDX])),
3498 [rsi]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RSI])),
3499 [rdi]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RDI])),
3500 [rbp]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RBP]))
05b3e0c2 3501#ifdef CONFIG_X86_64
ad312c7c
ZX
3502 , [r8]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R8])),
3503 [r9]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R9])),
3504 [r10]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R10])),
3505 [r11]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R11])),
3506 [r12]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R12])),
3507 [r13]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R13])),
3508 [r14]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R14])),
3509 [r15]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R15]))
6aa8b732 3510#endif
54a08c04 3511 : "cc", "memory"
80e31d4f 3512 , R"bx", R"cx", R"dx", R"si", R"di"
54a08c04 3513#ifdef CONFIG_X86_64
54a08c04
LV
3514 , "r8", "r9", "r10", "r11" , "r12", "r13", "r14", "r15"
3515#endif
3516 );
6aa8b732 3517
82ca2d10
AK
3518#ifdef CONFIG_X86_64
3519 wrmsrl(MSR_GS_BASE, svm->host.gs_base);
3520#else
dacccfdd 3521 loadsegment(fs, svm->host.fs);
9581d442 3522#endif
6aa8b732
AK
3523
3524 reload_tss(vcpu);
3525
56ba47dd
AK
3526 local_irq_disable();
3527
3528 stgi();
3529
13c34e07
AK
3530 vcpu->arch.cr2 = svm->vmcb->save.cr2;
3531 vcpu->arch.regs[VCPU_REGS_RAX] = svm->vmcb->save.rax;
3532 vcpu->arch.regs[VCPU_REGS_RSP] = svm->vmcb->save.rsp;
3533 vcpu->arch.regs[VCPU_REGS_RIP] = svm->vmcb->save.rip;
3534
d7bf8221
JR
3535 sync_cr8_to_lapic(vcpu);
3536
a2fa3e9f 3537 svm->next_rip = 0;
9222be18 3538
631bc487
GN
3539 /* if exit due to PF check for async PF */
3540 if (svm->vmcb->control.exit_code == SVM_EXIT_EXCP_BASE + PF_VECTOR)
3541 svm->apf_reason = kvm_read_and_reset_pf_reason();
3542
6de4f3ad
AK
3543 if (npt_enabled) {
3544 vcpu->arch.regs_avail &= ~(1 << VCPU_EXREG_PDPTR);
3545 vcpu->arch.regs_dirty &= ~(1 << VCPU_EXREG_PDPTR);
3546 }
fe5913e4
JR
3547
3548 /*
3549 * We need to handle MC intercepts here before the vcpu has a chance to
3550 * change the physical cpu
3551 */
3552 if (unlikely(svm->vmcb->control.exit_code ==
3553 SVM_EXIT_EXCP_BASE + MC_VECTOR))
3554 svm_handle_mce(svm);
8d28fec4
RJ
3555
3556 mark_all_clean(svm->vmcb);
6aa8b732
AK
3557}
3558
80e31d4f
AK
3559#undef R
3560
6aa8b732
AK
3561static void svm_set_cr3(struct kvm_vcpu *vcpu, unsigned long root)
3562{
a2fa3e9f
GH
3563 struct vcpu_svm *svm = to_svm(vcpu);
3564
3565 svm->vmcb->save.cr3 = root;
dcca1a65 3566 mark_dirty(svm->vmcb, VMCB_CR);
6aa8b732
AK
3567 force_new_asid(vcpu);
3568}
3569
1c97f0a0
JR
3570static void set_tdp_cr3(struct kvm_vcpu *vcpu, unsigned long root)
3571{
3572 struct vcpu_svm *svm = to_svm(vcpu);
3573
3574 svm->vmcb->control.nested_cr3 = root;
b2747166 3575 mark_dirty(svm->vmcb, VMCB_NPT);
1c97f0a0
JR
3576
3577 /* Also sync guest cr3 here in case we live migrate */
3578 svm->vmcb->save.cr3 = vcpu->arch.cr3;
dcca1a65 3579 mark_dirty(svm->vmcb, VMCB_CR);
1c97f0a0
JR
3580
3581 force_new_asid(vcpu);
3582}
3583
6aa8b732
AK
3584static int is_disabled(void)
3585{
6031a61c
JR
3586 u64 vm_cr;
3587
3588 rdmsrl(MSR_VM_CR, vm_cr);
3589 if (vm_cr & (1 << SVM_VM_CR_SVM_DISABLE))
3590 return 1;
3591
6aa8b732
AK
3592 return 0;
3593}
3594
102d8325
IM
3595static void
3596svm_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
3597{
3598 /*
3599 * Patch in the VMMCALL instruction:
3600 */
3601 hypercall[0] = 0x0f;
3602 hypercall[1] = 0x01;
3603 hypercall[2] = 0xd9;
102d8325
IM
3604}
3605
002c7f7c
YS
3606static void svm_check_processor_compat(void *rtn)
3607{
3608 *(int *)rtn = 0;
3609}
3610
774ead3a
AK
3611static bool svm_cpu_has_accelerated_tpr(void)
3612{
3613 return false;
3614}
3615
4b12f0de 3616static u64 svm_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
64d4d521
SY
3617{
3618 return 0;
3619}
3620
0e851880
SY
3621static void svm_cpuid_update(struct kvm_vcpu *vcpu)
3622{
3623}
3624
d4330ef2
JR
3625static void svm_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry)
3626{
c2c63a49 3627 switch (func) {
24d1b15f
JR
3628 case 0x00000001:
3629 /* Mask out xsave bit as long as it is not supported by SVM */
3630 entry->ecx &= ~(bit(X86_FEATURE_XSAVE));
3631 break;
4c62a2dc
JR
3632 case 0x80000001:
3633 if (nested)
3634 entry->ecx |= (1 << 2); /* Set SVM bit */
3635 break;
c2c63a49
JR
3636 case 0x8000000A:
3637 entry->eax = 1; /* SVM revision 1 */
3638 entry->ebx = 8; /* Lets support 8 ASIDs in case we add proper
3639 ASID emulation to nested SVM */
3640 entry->ecx = 0; /* Reserved */
7a190667
JR
3641 entry->edx = 0; /* Per default do not support any
3642 additional features */
3643
3644 /* Support next_rip if host supports it */
2a6b20b8 3645 if (boot_cpu_has(X86_FEATURE_NRIPS))
7a190667 3646 entry->edx |= SVM_FEATURE_NRIP;
c2c63a49 3647
3d4aeaad
JR
3648 /* Support NPT for the guest if enabled */
3649 if (npt_enabled)
3650 entry->edx |= SVM_FEATURE_NPT;
3651
c2c63a49
JR
3652 break;
3653 }
d4330ef2
JR
3654}
3655
229456fc 3656static const struct trace_print_flags svm_exit_reasons_str[] = {
e0231715
JR
3657 { SVM_EXIT_READ_CR0, "read_cr0" },
3658 { SVM_EXIT_READ_CR3, "read_cr3" },
3659 { SVM_EXIT_READ_CR4, "read_cr4" },
3660 { SVM_EXIT_READ_CR8, "read_cr8" },
3661 { SVM_EXIT_WRITE_CR0, "write_cr0" },
3662 { SVM_EXIT_WRITE_CR3, "write_cr3" },
3663 { SVM_EXIT_WRITE_CR4, "write_cr4" },
3664 { SVM_EXIT_WRITE_CR8, "write_cr8" },
3665 { SVM_EXIT_READ_DR0, "read_dr0" },
3666 { SVM_EXIT_READ_DR1, "read_dr1" },
3667 { SVM_EXIT_READ_DR2, "read_dr2" },
3668 { SVM_EXIT_READ_DR3, "read_dr3" },
3669 { SVM_EXIT_WRITE_DR0, "write_dr0" },
3670 { SVM_EXIT_WRITE_DR1, "write_dr1" },
3671 { SVM_EXIT_WRITE_DR2, "write_dr2" },
3672 { SVM_EXIT_WRITE_DR3, "write_dr3" },
3673 { SVM_EXIT_WRITE_DR5, "write_dr5" },
3674 { SVM_EXIT_WRITE_DR7, "write_dr7" },
229456fc
MT
3675 { SVM_EXIT_EXCP_BASE + DB_VECTOR, "DB excp" },
3676 { SVM_EXIT_EXCP_BASE + BP_VECTOR, "BP excp" },
3677 { SVM_EXIT_EXCP_BASE + UD_VECTOR, "UD excp" },
3678 { SVM_EXIT_EXCP_BASE + PF_VECTOR, "PF excp" },
3679 { SVM_EXIT_EXCP_BASE + NM_VECTOR, "NM excp" },
3680 { SVM_EXIT_EXCP_BASE + MC_VECTOR, "MC excp" },
3681 { SVM_EXIT_INTR, "interrupt" },
3682 { SVM_EXIT_NMI, "nmi" },
3683 { SVM_EXIT_SMI, "smi" },
3684 { SVM_EXIT_INIT, "init" },
3685 { SVM_EXIT_VINTR, "vintr" },
3686 { SVM_EXIT_CPUID, "cpuid" },
3687 { SVM_EXIT_INVD, "invd" },
3688 { SVM_EXIT_HLT, "hlt" },
3689 { SVM_EXIT_INVLPG, "invlpg" },
3690 { SVM_EXIT_INVLPGA, "invlpga" },
3691 { SVM_EXIT_IOIO, "io" },
3692 { SVM_EXIT_MSR, "msr" },
3693 { SVM_EXIT_TASK_SWITCH, "task_switch" },
3694 { SVM_EXIT_SHUTDOWN, "shutdown" },
3695 { SVM_EXIT_VMRUN, "vmrun" },
3696 { SVM_EXIT_VMMCALL, "hypercall" },
3697 { SVM_EXIT_VMLOAD, "vmload" },
3698 { SVM_EXIT_VMSAVE, "vmsave" },
3699 { SVM_EXIT_STGI, "stgi" },
3700 { SVM_EXIT_CLGI, "clgi" },
3701 { SVM_EXIT_SKINIT, "skinit" },
3702 { SVM_EXIT_WBINVD, "wbinvd" },
3703 { SVM_EXIT_MONITOR, "monitor" },
3704 { SVM_EXIT_MWAIT, "mwait" },
3705 { SVM_EXIT_NPF, "npf" },
3706 { -1, NULL }
3707};
3708
17cc3935 3709static int svm_get_lpage_level(void)
344f414f 3710{
17cc3935 3711 return PT_PDPE_LEVEL;
344f414f
JR
3712}
3713
4e47c7a6
SY
3714static bool svm_rdtscp_supported(void)
3715{
3716 return false;
3717}
3718
f5f48ee1
SY
3719static bool svm_has_wbinvd_exit(void)
3720{
3721 return true;
3722}
3723
02daab21
AK
3724static void svm_fpu_deactivate(struct kvm_vcpu *vcpu)
3725{
3726 struct vcpu_svm *svm = to_svm(vcpu);
3727
18c918c5 3728 set_exception_intercept(svm, NM_VECTOR);
66a562f7 3729 update_cr0_intercept(svm);
02daab21
AK
3730}
3731
cbdd1bea 3732static struct kvm_x86_ops svm_x86_ops = {
6aa8b732
AK
3733 .cpu_has_kvm_support = has_svm,
3734 .disabled_by_bios = is_disabled,
3735 .hardware_setup = svm_hardware_setup,
3736 .hardware_unsetup = svm_hardware_unsetup,
002c7f7c 3737 .check_processor_compatibility = svm_check_processor_compat,
6aa8b732
AK
3738 .hardware_enable = svm_hardware_enable,
3739 .hardware_disable = svm_hardware_disable,
774ead3a 3740 .cpu_has_accelerated_tpr = svm_cpu_has_accelerated_tpr,
6aa8b732
AK
3741
3742 .vcpu_create = svm_create_vcpu,
3743 .vcpu_free = svm_free_vcpu,
04d2cc77 3744 .vcpu_reset = svm_vcpu_reset,
6aa8b732 3745
04d2cc77 3746 .prepare_guest_switch = svm_prepare_guest_switch,
6aa8b732
AK
3747 .vcpu_load = svm_vcpu_load,
3748 .vcpu_put = svm_vcpu_put,
3749
3750 .set_guest_debug = svm_guest_debug,
3751 .get_msr = svm_get_msr,
3752 .set_msr = svm_set_msr,
3753 .get_segment_base = svm_get_segment_base,
3754 .get_segment = svm_get_segment,
3755 .set_segment = svm_set_segment,
2e4d2653 3756 .get_cpl = svm_get_cpl,
1747fb71 3757 .get_cs_db_l_bits = kvm_get_cs_db_l_bits,
e8467fda 3758 .decache_cr0_guest_bits = svm_decache_cr0_guest_bits,
25c4c276 3759 .decache_cr4_guest_bits = svm_decache_cr4_guest_bits,
6aa8b732 3760 .set_cr0 = svm_set_cr0,
6aa8b732
AK
3761 .set_cr3 = svm_set_cr3,
3762 .set_cr4 = svm_set_cr4,
3763 .set_efer = svm_set_efer,
3764 .get_idt = svm_get_idt,
3765 .set_idt = svm_set_idt,
3766 .get_gdt = svm_get_gdt,
3767 .set_gdt = svm_set_gdt,
020df079 3768 .set_dr7 = svm_set_dr7,
6de4f3ad 3769 .cache_reg = svm_cache_reg,
6aa8b732
AK
3770 .get_rflags = svm_get_rflags,
3771 .set_rflags = svm_set_rflags,
6b52d186 3772 .fpu_activate = svm_fpu_activate,
02daab21 3773 .fpu_deactivate = svm_fpu_deactivate,
6aa8b732 3774
6aa8b732 3775 .tlb_flush = svm_flush_tlb,
6aa8b732 3776
6aa8b732 3777 .run = svm_vcpu_run,
04d2cc77 3778 .handle_exit = handle_exit,
6aa8b732 3779 .skip_emulated_instruction = skip_emulated_instruction,
2809f5d2
GC
3780 .set_interrupt_shadow = svm_set_interrupt_shadow,
3781 .get_interrupt_shadow = svm_get_interrupt_shadow,
102d8325 3782 .patch_hypercall = svm_patch_hypercall,
2a8067f1 3783 .set_irq = svm_set_irq,
95ba8273 3784 .set_nmi = svm_inject_nmi,
298101da 3785 .queue_exception = svm_queue_exception,
b463a6f7 3786 .cancel_injection = svm_cancel_injection,
78646121 3787 .interrupt_allowed = svm_interrupt_allowed,
95ba8273 3788 .nmi_allowed = svm_nmi_allowed,
3cfc3092
JK
3789 .get_nmi_mask = svm_get_nmi_mask,
3790 .set_nmi_mask = svm_set_nmi_mask,
95ba8273
GN
3791 .enable_nmi_window = enable_nmi_window,
3792 .enable_irq_window = enable_irq_window,
3793 .update_cr8_intercept = update_cr8_intercept,
cbc94022
IE
3794
3795 .set_tss_addr = svm_set_tss_addr,
67253af5 3796 .get_tdp_level = get_npt_level,
4b12f0de 3797 .get_mt_mask = svm_get_mt_mask,
229456fc 3798
586f9607 3799 .get_exit_info = svm_get_exit_info,
229456fc 3800 .exit_reasons_str = svm_exit_reasons_str,
586f9607 3801
17cc3935 3802 .get_lpage_level = svm_get_lpage_level,
0e851880
SY
3803
3804 .cpuid_update = svm_cpuid_update,
4e47c7a6
SY
3805
3806 .rdtscp_supported = svm_rdtscp_supported,
d4330ef2
JR
3807
3808 .set_supported_cpuid = svm_set_supported_cpuid,
f5f48ee1
SY
3809
3810 .has_wbinvd_exit = svm_has_wbinvd_exit,
99e3e30a
ZA
3811
3812 .write_tsc_offset = svm_write_tsc_offset,
e48672fa 3813 .adjust_tsc_offset = svm_adjust_tsc_offset,
1c97f0a0
JR
3814
3815 .set_tdp_cr3 = set_tdp_cr3,
6aa8b732
AK
3816};
3817
3818static int __init svm_init(void)
3819{
cb498ea2 3820 return kvm_init(&svm_x86_ops, sizeof(struct vcpu_svm),
0ee75bea 3821 __alignof__(struct vcpu_svm), THIS_MODULE);
6aa8b732
AK
3822}
3823
3824static void __exit svm_exit(void)
3825{
cb498ea2 3826 kvm_exit();
6aa8b732
AK
3827}
3828
3829module_init(svm_init)
3830module_exit(svm_exit)