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