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