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