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