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