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