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