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