xref: /openbmc/linux/arch/x86/kvm/svm/nested.c (revision 002dff36)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Kernel-based Virtual Machine driver for Linux
4  *
5  * AMD SVM support
6  *
7  * Copyright (C) 2006 Qumranet, Inc.
8  * Copyright 2010 Red Hat, Inc. and/or its affiliates.
9  *
10  * Authors:
11  *   Yaniv Kamay  <yaniv@qumranet.com>
12  *   Avi Kivity   <avi@qumranet.com>
13  */
14 
15 #define pr_fmt(fmt) "SVM: " fmt
16 
17 #include <linux/kvm_types.h>
18 #include <linux/kvm_host.h>
19 #include <linux/kernel.h>
20 
21 #include <asm/msr-index.h>
22 #include <asm/debugreg.h>
23 
24 #include "kvm_emulate.h"
25 #include "trace.h"
26 #include "mmu.h"
27 #include "x86.h"
28 #include "cpuid.h"
29 #include "lapic.h"
30 #include "svm.h"
31 
32 static void nested_svm_inject_npf_exit(struct kvm_vcpu *vcpu,
33 				       struct x86_exception *fault)
34 {
35 	struct vcpu_svm *svm = to_svm(vcpu);
36 
37 	if (svm->vmcb->control.exit_code != SVM_EXIT_NPF) {
38 		/*
39 		 * TODO: track the cause of the nested page fault, and
40 		 * correctly fill in the high bits of exit_info_1.
41 		 */
42 		svm->vmcb->control.exit_code = SVM_EXIT_NPF;
43 		svm->vmcb->control.exit_code_hi = 0;
44 		svm->vmcb->control.exit_info_1 = (1ULL << 32);
45 		svm->vmcb->control.exit_info_2 = fault->address;
46 	}
47 
48 	svm->vmcb->control.exit_info_1 &= ~0xffffffffULL;
49 	svm->vmcb->control.exit_info_1 |= fault->error_code;
50 
51 	/*
52 	 * The present bit is always zero for page structure faults on real
53 	 * hardware.
54 	 */
55 	if (svm->vmcb->control.exit_info_1 & (2ULL << 32))
56 		svm->vmcb->control.exit_info_1 &= ~1;
57 
58 	nested_svm_vmexit(svm);
59 }
60 
61 static u64 nested_svm_get_tdp_pdptr(struct kvm_vcpu *vcpu, int index)
62 {
63 	struct vcpu_svm *svm = to_svm(vcpu);
64 	u64 cr3 = svm->nested.ctl.nested_cr3;
65 	u64 pdpte;
66 	int ret;
67 
68 	ret = kvm_vcpu_read_guest_page(vcpu, gpa_to_gfn(__sme_clr(cr3)), &pdpte,
69 				       offset_in_page(cr3) + index * 8, 8);
70 	if (ret)
71 		return 0;
72 	return pdpte;
73 }
74 
75 static unsigned long nested_svm_get_tdp_cr3(struct kvm_vcpu *vcpu)
76 {
77 	struct vcpu_svm *svm = to_svm(vcpu);
78 
79 	return svm->nested.ctl.nested_cr3;
80 }
81 
82 static void nested_svm_init_mmu_context(struct kvm_vcpu *vcpu)
83 {
84 	struct vcpu_svm *svm = to_svm(vcpu);
85 	struct vmcb *hsave = svm->nested.hsave;
86 
87 	WARN_ON(mmu_is_nested(vcpu));
88 
89 	vcpu->arch.mmu = &vcpu->arch.guest_mmu;
90 	kvm_init_shadow_mmu(vcpu, X86_CR0_PG, hsave->save.cr4, hsave->save.efer);
91 	vcpu->arch.mmu->get_guest_pgd     = nested_svm_get_tdp_cr3;
92 	vcpu->arch.mmu->get_pdptr         = nested_svm_get_tdp_pdptr;
93 	vcpu->arch.mmu->inject_page_fault = nested_svm_inject_npf_exit;
94 	vcpu->arch.mmu->shadow_root_level = vcpu->arch.tdp_level;
95 	reset_shadow_zero_bits_mask(vcpu, vcpu->arch.mmu);
96 	vcpu->arch.walk_mmu              = &vcpu->arch.nested_mmu;
97 }
98 
99 static void nested_svm_uninit_mmu_context(struct kvm_vcpu *vcpu)
100 {
101 	vcpu->arch.mmu = &vcpu->arch.root_mmu;
102 	vcpu->arch.walk_mmu = &vcpu->arch.root_mmu;
103 }
104 
105 void recalc_intercepts(struct vcpu_svm *svm)
106 {
107 	struct vmcb_control_area *c, *h, *g;
108 
109 	mark_dirty(svm->vmcb, VMCB_INTERCEPTS);
110 
111 	if (!is_guest_mode(&svm->vcpu))
112 		return;
113 
114 	c = &svm->vmcb->control;
115 	h = &svm->nested.hsave->control;
116 	g = &svm->nested.ctl;
117 
118 	svm->nested.host_intercept_exceptions = h->intercept_exceptions;
119 
120 	c->intercept_cr = h->intercept_cr;
121 	c->intercept_dr = h->intercept_dr;
122 	c->intercept_exceptions = h->intercept_exceptions;
123 	c->intercept = h->intercept;
124 
125 	if (g->int_ctl & V_INTR_MASKING_MASK) {
126 		/* We only want the cr8 intercept bits of L1 */
127 		c->intercept_cr &= ~(1U << INTERCEPT_CR8_READ);
128 		c->intercept_cr &= ~(1U << INTERCEPT_CR8_WRITE);
129 
130 		/*
131 		 * Once running L2 with HF_VINTR_MASK, EFLAGS.IF does not
132 		 * affect any interrupt we may want to inject; therefore,
133 		 * interrupt window vmexits are irrelevant to L0.
134 		 */
135 		c->intercept &= ~(1ULL << INTERCEPT_VINTR);
136 	}
137 
138 	/* We don't want to see VMMCALLs from a nested guest */
139 	c->intercept &= ~(1ULL << INTERCEPT_VMMCALL);
140 
141 	c->intercept_cr |= g->intercept_cr;
142 	c->intercept_dr |= g->intercept_dr;
143 	c->intercept_exceptions |= g->intercept_exceptions;
144 	c->intercept |= g->intercept;
145 }
146 
147 static void copy_vmcb_control_area(struct vmcb_control_area *dst,
148 				   struct vmcb_control_area *from)
149 {
150 	dst->intercept_cr         = from->intercept_cr;
151 	dst->intercept_dr         = from->intercept_dr;
152 	dst->intercept_exceptions = from->intercept_exceptions;
153 	dst->intercept            = from->intercept;
154 	dst->iopm_base_pa         = from->iopm_base_pa;
155 	dst->msrpm_base_pa        = from->msrpm_base_pa;
156 	dst->tsc_offset           = from->tsc_offset;
157 	/* asid not copied, it is handled manually for svm->vmcb.  */
158 	dst->tlb_ctl              = from->tlb_ctl;
159 	dst->int_ctl              = from->int_ctl;
160 	dst->int_vector           = from->int_vector;
161 	dst->int_state            = from->int_state;
162 	dst->exit_code            = from->exit_code;
163 	dst->exit_code_hi         = from->exit_code_hi;
164 	dst->exit_info_1          = from->exit_info_1;
165 	dst->exit_info_2          = from->exit_info_2;
166 	dst->exit_int_info        = from->exit_int_info;
167 	dst->exit_int_info_err    = from->exit_int_info_err;
168 	dst->nested_ctl           = from->nested_ctl;
169 	dst->event_inj            = from->event_inj;
170 	dst->event_inj_err        = from->event_inj_err;
171 	dst->nested_cr3           = from->nested_cr3;
172 	dst->virt_ext              = from->virt_ext;
173 	dst->pause_filter_count   = from->pause_filter_count;
174 	dst->pause_filter_thresh  = from->pause_filter_thresh;
175 }
176 
177 static bool nested_svm_vmrun_msrpm(struct vcpu_svm *svm)
178 {
179 	/*
180 	 * This function merges the msr permission bitmaps of kvm and the
181 	 * nested vmcb. It is optimized in that it only merges the parts where
182 	 * the kvm msr permission bitmap may contain zero bits
183 	 */
184 	int i;
185 
186 	if (!(svm->nested.ctl.intercept & (1ULL << INTERCEPT_MSR_PROT)))
187 		return true;
188 
189 	for (i = 0; i < MSRPM_OFFSETS; i++) {
190 		u32 value, p;
191 		u64 offset;
192 
193 		if (msrpm_offsets[i] == 0xffffffff)
194 			break;
195 
196 		p      = msrpm_offsets[i];
197 		offset = svm->nested.ctl.msrpm_base_pa + (p * 4);
198 
199 		if (kvm_vcpu_read_guest(&svm->vcpu, offset, &value, 4))
200 			return false;
201 
202 		svm->nested.msrpm[p] = svm->msrpm[p] | value;
203 	}
204 
205 	svm->vmcb->control.msrpm_base_pa = __sme_set(__pa(svm->nested.msrpm));
206 
207 	return true;
208 }
209 
210 static bool nested_vmcb_check_controls(struct vmcb_control_area *control)
211 {
212 	if ((control->intercept & (1ULL << INTERCEPT_VMRUN)) == 0)
213 		return false;
214 
215 	if (control->asid == 0)
216 		return false;
217 
218 	if ((control->nested_ctl & SVM_NESTED_CTL_NP_ENABLE) &&
219 	    !npt_enabled)
220 		return false;
221 
222 	return true;
223 }
224 
225 static bool nested_vmcb_checks(struct vmcb *vmcb)
226 {
227 	if ((vmcb->save.efer & EFER_SVME) == 0)
228 		return false;
229 
230 	if (((vmcb->save.cr0 & X86_CR0_CD) == 0) &&
231 	    (vmcb->save.cr0 & X86_CR0_NW))
232 		return false;
233 
234 	return nested_vmcb_check_controls(&vmcb->control);
235 }
236 
237 static void load_nested_vmcb_control(struct vcpu_svm *svm,
238 				     struct vmcb_control_area *control)
239 {
240 	copy_vmcb_control_area(&svm->nested.ctl, control);
241 
242 	/* Copy it here because nested_svm_check_controls will check it.  */
243 	svm->nested.ctl.asid           = control->asid;
244 	svm->nested.ctl.msrpm_base_pa &= ~0x0fffULL;
245 	svm->nested.ctl.iopm_base_pa  &= ~0x0fffULL;
246 }
247 
248 /*
249  * Synchronize fields that are written by the processor, so that
250  * they can be copied back into the nested_vmcb.
251  */
252 void sync_nested_vmcb_control(struct vcpu_svm *svm)
253 {
254 	u32 mask;
255 	svm->nested.ctl.event_inj      = svm->vmcb->control.event_inj;
256 	svm->nested.ctl.event_inj_err  = svm->vmcb->control.event_inj_err;
257 
258 	/* Only a few fields of int_ctl are written by the processor.  */
259 	mask = V_IRQ_MASK | V_TPR_MASK;
260 	if (!(svm->nested.ctl.int_ctl & V_INTR_MASKING_MASK) &&
261 	    is_intercept(svm, INTERCEPT_VINTR)) {
262 		/*
263 		 * In order to request an interrupt window, L0 is usurping
264 		 * svm->vmcb->control.int_ctl and possibly setting V_IRQ
265 		 * even if it was clear in L1's VMCB.  Restoring it would be
266 		 * wrong.  However, in this case V_IRQ will remain true until
267 		 * interrupt_window_interception calls svm_clear_vintr and
268 		 * restores int_ctl.  We can just leave it aside.
269 		 */
270 		mask &= ~V_IRQ_MASK;
271 	}
272 	svm->nested.ctl.int_ctl        &= ~mask;
273 	svm->nested.ctl.int_ctl        |= svm->vmcb->control.int_ctl & mask;
274 }
275 
276 /*
277  * Transfer any event that L0 or L1 wanted to inject into L2 to
278  * EXIT_INT_INFO.
279  */
280 static void nested_vmcb_save_pending_event(struct vcpu_svm *svm,
281 					   struct vmcb *nested_vmcb)
282 {
283 	struct kvm_vcpu *vcpu = &svm->vcpu;
284 	u32 exit_int_info = 0;
285 	unsigned int nr;
286 
287 	if (vcpu->arch.exception.injected) {
288 		nr = vcpu->arch.exception.nr;
289 		exit_int_info = nr | SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_EXEPT;
290 
291 		if (vcpu->arch.exception.has_error_code) {
292 			exit_int_info |= SVM_EVTINJ_VALID_ERR;
293 			nested_vmcb->control.exit_int_info_err =
294 				vcpu->arch.exception.error_code;
295 		}
296 
297 	} else if (vcpu->arch.nmi_injected) {
298 		exit_int_info = SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_NMI;
299 
300 	} else if (vcpu->arch.interrupt.injected) {
301 		nr = vcpu->arch.interrupt.nr;
302 		exit_int_info = nr | SVM_EVTINJ_VALID;
303 
304 		if (vcpu->arch.interrupt.soft)
305 			exit_int_info |= SVM_EVTINJ_TYPE_SOFT;
306 		else
307 			exit_int_info |= SVM_EVTINJ_TYPE_INTR;
308 	}
309 
310 	nested_vmcb->control.exit_int_info = exit_int_info;
311 }
312 
313 static void nested_prepare_vmcb_save(struct vcpu_svm *svm, struct vmcb *nested_vmcb)
314 {
315 	/* Load the nested guest state */
316 	svm->vmcb->save.es = nested_vmcb->save.es;
317 	svm->vmcb->save.cs = nested_vmcb->save.cs;
318 	svm->vmcb->save.ss = nested_vmcb->save.ss;
319 	svm->vmcb->save.ds = nested_vmcb->save.ds;
320 	svm->vmcb->save.gdtr = nested_vmcb->save.gdtr;
321 	svm->vmcb->save.idtr = nested_vmcb->save.idtr;
322 	kvm_set_rflags(&svm->vcpu, nested_vmcb->save.rflags);
323 	svm_set_efer(&svm->vcpu, nested_vmcb->save.efer);
324 	svm_set_cr0(&svm->vcpu, nested_vmcb->save.cr0);
325 	svm_set_cr4(&svm->vcpu, nested_vmcb->save.cr4);
326 	(void)kvm_set_cr3(&svm->vcpu, nested_vmcb->save.cr3);
327 
328 	svm->vmcb->save.cr2 = svm->vcpu.arch.cr2 = nested_vmcb->save.cr2;
329 	kvm_rax_write(&svm->vcpu, nested_vmcb->save.rax);
330 	kvm_rsp_write(&svm->vcpu, nested_vmcb->save.rsp);
331 	kvm_rip_write(&svm->vcpu, nested_vmcb->save.rip);
332 
333 	/* In case we don't even reach vcpu_run, the fields are not updated */
334 	svm->vmcb->save.rax = nested_vmcb->save.rax;
335 	svm->vmcb->save.rsp = nested_vmcb->save.rsp;
336 	svm->vmcb->save.rip = nested_vmcb->save.rip;
337 	svm->vmcb->save.dr7 = nested_vmcb->save.dr7;
338 	svm->vcpu.arch.dr6  = nested_vmcb->save.dr6;
339 	svm->vmcb->save.cpl = nested_vmcb->save.cpl;
340 }
341 
342 static void nested_prepare_vmcb_control(struct vcpu_svm *svm)
343 {
344 	const u32 mask = V_INTR_MASKING_MASK | V_GIF_ENABLE_MASK | V_GIF_MASK;
345 	if (svm->nested.ctl.nested_ctl & SVM_NESTED_CTL_NP_ENABLE)
346 		nested_svm_init_mmu_context(&svm->vcpu);
347 
348 	/* Guest paging mode is active - reset mmu */
349 	kvm_mmu_reset_context(&svm->vcpu);
350 
351 	svm_flush_tlb(&svm->vcpu);
352 
353 	svm->vmcb->control.tsc_offset = svm->vcpu.arch.tsc_offset =
354 		svm->vcpu.arch.l1_tsc_offset + svm->nested.ctl.tsc_offset;
355 
356 	svm->vmcb->control.int_ctl             =
357 		(svm->nested.ctl.int_ctl & ~mask) |
358 		(svm->nested.hsave->control.int_ctl & mask);
359 
360 	svm->vmcb->control.virt_ext            = svm->nested.ctl.virt_ext;
361 	svm->vmcb->control.int_vector          = svm->nested.ctl.int_vector;
362 	svm->vmcb->control.int_state           = svm->nested.ctl.int_state;
363 	svm->vmcb->control.event_inj           = svm->nested.ctl.event_inj;
364 	svm->vmcb->control.event_inj_err       = svm->nested.ctl.event_inj_err;
365 
366 	svm->vmcb->control.pause_filter_count  = svm->nested.ctl.pause_filter_count;
367 	svm->vmcb->control.pause_filter_thresh = svm->nested.ctl.pause_filter_thresh;
368 
369 	/* Enter Guest-Mode */
370 	enter_guest_mode(&svm->vcpu);
371 
372 	/*
373 	 * Merge guest and host intercepts - must be called  with vcpu in
374 	 * guest-mode to take affect here
375 	 */
376 	recalc_intercepts(svm);
377 
378 	mark_all_dirty(svm->vmcb);
379 }
380 
381 void enter_svm_guest_mode(struct vcpu_svm *svm, u64 vmcb_gpa,
382 			  struct vmcb *nested_vmcb)
383 {
384 	svm->nested.vmcb = vmcb_gpa;
385 	load_nested_vmcb_control(svm, &nested_vmcb->control);
386 	nested_prepare_vmcb_save(svm, nested_vmcb);
387 	nested_prepare_vmcb_control(svm);
388 
389 	svm_set_gif(svm, true);
390 }
391 
392 int nested_svm_vmrun(struct vcpu_svm *svm)
393 {
394 	int ret;
395 	struct vmcb *nested_vmcb;
396 	struct vmcb *hsave = svm->nested.hsave;
397 	struct vmcb *vmcb = svm->vmcb;
398 	struct kvm_host_map map;
399 	u64 vmcb_gpa;
400 
401 	if (is_smm(&svm->vcpu)) {
402 		kvm_queue_exception(&svm->vcpu, UD_VECTOR);
403 		return 1;
404 	}
405 
406 	vmcb_gpa = svm->vmcb->save.rax;
407 	ret = kvm_vcpu_map(&svm->vcpu, gpa_to_gfn(vmcb_gpa), &map);
408 	if (ret == -EINVAL) {
409 		kvm_inject_gp(&svm->vcpu, 0);
410 		return 1;
411 	} else if (ret) {
412 		return kvm_skip_emulated_instruction(&svm->vcpu);
413 	}
414 
415 	ret = kvm_skip_emulated_instruction(&svm->vcpu);
416 
417 	nested_vmcb = map.hva;
418 
419 	if (!nested_vmcb_checks(nested_vmcb)) {
420 		nested_vmcb->control.exit_code    = SVM_EXIT_ERR;
421 		nested_vmcb->control.exit_code_hi = 0;
422 		nested_vmcb->control.exit_info_1  = 0;
423 		nested_vmcb->control.exit_info_2  = 0;
424 		goto out;
425 	}
426 
427 	trace_kvm_nested_vmrun(svm->vmcb->save.rip, vmcb_gpa,
428 			       nested_vmcb->save.rip,
429 			       nested_vmcb->control.int_ctl,
430 			       nested_vmcb->control.event_inj,
431 			       nested_vmcb->control.nested_ctl);
432 
433 	trace_kvm_nested_intercepts(nested_vmcb->control.intercept_cr & 0xffff,
434 				    nested_vmcb->control.intercept_cr >> 16,
435 				    nested_vmcb->control.intercept_exceptions,
436 				    nested_vmcb->control.intercept);
437 
438 	/* Clear internal status */
439 	kvm_clear_exception_queue(&svm->vcpu);
440 	kvm_clear_interrupt_queue(&svm->vcpu);
441 
442 	/*
443 	 * Save the old vmcb, so we don't need to pick what we save, but can
444 	 * restore everything when a VMEXIT occurs
445 	 */
446 	hsave->save.es     = vmcb->save.es;
447 	hsave->save.cs     = vmcb->save.cs;
448 	hsave->save.ss     = vmcb->save.ss;
449 	hsave->save.ds     = vmcb->save.ds;
450 	hsave->save.gdtr   = vmcb->save.gdtr;
451 	hsave->save.idtr   = vmcb->save.idtr;
452 	hsave->save.efer   = svm->vcpu.arch.efer;
453 	hsave->save.cr0    = kvm_read_cr0(&svm->vcpu);
454 	hsave->save.cr4    = svm->vcpu.arch.cr4;
455 	hsave->save.rflags = kvm_get_rflags(&svm->vcpu);
456 	hsave->save.rip    = kvm_rip_read(&svm->vcpu);
457 	hsave->save.rsp    = vmcb->save.rsp;
458 	hsave->save.rax    = vmcb->save.rax;
459 	if (npt_enabled)
460 		hsave->save.cr3    = vmcb->save.cr3;
461 	else
462 		hsave->save.cr3    = kvm_read_cr3(&svm->vcpu);
463 
464 	copy_vmcb_control_area(&hsave->control, &vmcb->control);
465 
466 	svm->nested.nested_run_pending = 1;
467 	enter_svm_guest_mode(svm, vmcb_gpa, nested_vmcb);
468 
469 	if (!nested_svm_vmrun_msrpm(svm)) {
470 		svm->vmcb->control.exit_code    = SVM_EXIT_ERR;
471 		svm->vmcb->control.exit_code_hi = 0;
472 		svm->vmcb->control.exit_info_1  = 0;
473 		svm->vmcb->control.exit_info_2  = 0;
474 
475 		nested_svm_vmexit(svm);
476 	}
477 
478 out:
479 	kvm_vcpu_unmap(&svm->vcpu, &map, true);
480 
481 	return ret;
482 }
483 
484 void nested_svm_vmloadsave(struct vmcb *from_vmcb, struct vmcb *to_vmcb)
485 {
486 	to_vmcb->save.fs = from_vmcb->save.fs;
487 	to_vmcb->save.gs = from_vmcb->save.gs;
488 	to_vmcb->save.tr = from_vmcb->save.tr;
489 	to_vmcb->save.ldtr = from_vmcb->save.ldtr;
490 	to_vmcb->save.kernel_gs_base = from_vmcb->save.kernel_gs_base;
491 	to_vmcb->save.star = from_vmcb->save.star;
492 	to_vmcb->save.lstar = from_vmcb->save.lstar;
493 	to_vmcb->save.cstar = from_vmcb->save.cstar;
494 	to_vmcb->save.sfmask = from_vmcb->save.sfmask;
495 	to_vmcb->save.sysenter_cs = from_vmcb->save.sysenter_cs;
496 	to_vmcb->save.sysenter_esp = from_vmcb->save.sysenter_esp;
497 	to_vmcb->save.sysenter_eip = from_vmcb->save.sysenter_eip;
498 }
499 
500 int nested_svm_vmexit(struct vcpu_svm *svm)
501 {
502 	int rc;
503 	struct vmcb *nested_vmcb;
504 	struct vmcb *hsave = svm->nested.hsave;
505 	struct vmcb *vmcb = svm->vmcb;
506 	struct kvm_host_map map;
507 
508 	rc = kvm_vcpu_map(&svm->vcpu, gpa_to_gfn(svm->nested.vmcb), &map);
509 	if (rc) {
510 		if (rc == -EINVAL)
511 			kvm_inject_gp(&svm->vcpu, 0);
512 		return 1;
513 	}
514 
515 	nested_vmcb = map.hva;
516 
517 	/* Exit Guest-Mode */
518 	leave_guest_mode(&svm->vcpu);
519 	svm->nested.vmcb = 0;
520 	WARN_ON_ONCE(svm->nested.nested_run_pending);
521 
522 	/* in case we halted in L2 */
523 	svm->vcpu.arch.mp_state = KVM_MP_STATE_RUNNABLE;
524 
525 	/* Give the current vmcb to the guest */
526 	svm_set_gif(svm, false);
527 
528 	nested_vmcb->save.es     = vmcb->save.es;
529 	nested_vmcb->save.cs     = vmcb->save.cs;
530 	nested_vmcb->save.ss     = vmcb->save.ss;
531 	nested_vmcb->save.ds     = vmcb->save.ds;
532 	nested_vmcb->save.gdtr   = vmcb->save.gdtr;
533 	nested_vmcb->save.idtr   = vmcb->save.idtr;
534 	nested_vmcb->save.efer   = svm->vcpu.arch.efer;
535 	nested_vmcb->save.cr0    = kvm_read_cr0(&svm->vcpu);
536 	nested_vmcb->save.cr3    = kvm_read_cr3(&svm->vcpu);
537 	nested_vmcb->save.cr2    = vmcb->save.cr2;
538 	nested_vmcb->save.cr4    = svm->vcpu.arch.cr4;
539 	nested_vmcb->save.rflags = kvm_get_rflags(&svm->vcpu);
540 	nested_vmcb->save.rip    = kvm_rip_read(&svm->vcpu);
541 	nested_vmcb->save.rsp    = kvm_rsp_read(&svm->vcpu);
542 	nested_vmcb->save.rax    = kvm_rax_read(&svm->vcpu);
543 	nested_vmcb->save.dr7    = vmcb->save.dr7;
544 	nested_vmcb->save.dr6    = svm->vcpu.arch.dr6;
545 	nested_vmcb->save.cpl    = vmcb->save.cpl;
546 
547 	nested_vmcb->control.int_state         = vmcb->control.int_state;
548 	nested_vmcb->control.exit_code         = vmcb->control.exit_code;
549 	nested_vmcb->control.exit_code_hi      = vmcb->control.exit_code_hi;
550 	nested_vmcb->control.exit_info_1       = vmcb->control.exit_info_1;
551 	nested_vmcb->control.exit_info_2       = vmcb->control.exit_info_2;
552 
553 	if (nested_vmcb->control.exit_code != SVM_EXIT_ERR)
554 		nested_vmcb_save_pending_event(svm, nested_vmcb);
555 
556 	if (svm->nrips_enabled)
557 		nested_vmcb->control.next_rip  = vmcb->control.next_rip;
558 
559 	nested_vmcb->control.int_ctl           = svm->nested.ctl.int_ctl;
560 	nested_vmcb->control.tlb_ctl           = svm->nested.ctl.tlb_ctl;
561 	nested_vmcb->control.event_inj         = svm->nested.ctl.event_inj;
562 	nested_vmcb->control.event_inj_err     = svm->nested.ctl.event_inj_err;
563 
564 	nested_vmcb->control.pause_filter_count =
565 		svm->vmcb->control.pause_filter_count;
566 	nested_vmcb->control.pause_filter_thresh =
567 		svm->vmcb->control.pause_filter_thresh;
568 
569 	/* Restore the original control entries */
570 	copy_vmcb_control_area(&vmcb->control, &hsave->control);
571 
572 	svm->vmcb->control.tsc_offset = svm->vcpu.arch.tsc_offset =
573 		svm->vcpu.arch.l1_tsc_offset;
574 
575 	svm->nested.ctl.nested_cr3 = 0;
576 
577 	/* Restore selected save entries */
578 	svm->vmcb->save.es = hsave->save.es;
579 	svm->vmcb->save.cs = hsave->save.cs;
580 	svm->vmcb->save.ss = hsave->save.ss;
581 	svm->vmcb->save.ds = hsave->save.ds;
582 	svm->vmcb->save.gdtr = hsave->save.gdtr;
583 	svm->vmcb->save.idtr = hsave->save.idtr;
584 	kvm_set_rflags(&svm->vcpu, hsave->save.rflags);
585 	svm_set_efer(&svm->vcpu, hsave->save.efer);
586 	svm_set_cr0(&svm->vcpu, hsave->save.cr0 | X86_CR0_PE);
587 	svm_set_cr4(&svm->vcpu, hsave->save.cr4);
588 	if (npt_enabled) {
589 		svm->vmcb->save.cr3 = hsave->save.cr3;
590 		svm->vcpu.arch.cr3 = hsave->save.cr3;
591 	} else {
592 		(void)kvm_set_cr3(&svm->vcpu, hsave->save.cr3);
593 	}
594 	kvm_rax_write(&svm->vcpu, hsave->save.rax);
595 	kvm_rsp_write(&svm->vcpu, hsave->save.rsp);
596 	kvm_rip_write(&svm->vcpu, hsave->save.rip);
597 	svm->vmcb->save.dr7 = 0;
598 	svm->vmcb->save.cpl = 0;
599 	svm->vmcb->control.exit_int_info = 0;
600 
601 	mark_all_dirty(svm->vmcb);
602 
603 	trace_kvm_nested_vmexit_inject(nested_vmcb->control.exit_code,
604 				       nested_vmcb->control.exit_info_1,
605 				       nested_vmcb->control.exit_info_2,
606 				       nested_vmcb->control.exit_int_info,
607 				       nested_vmcb->control.exit_int_info_err,
608 				       KVM_ISA_SVM);
609 
610 	kvm_vcpu_unmap(&svm->vcpu, &map, true);
611 
612 	nested_svm_uninit_mmu_context(&svm->vcpu);
613 	kvm_mmu_reset_context(&svm->vcpu);
614 	kvm_mmu_load(&svm->vcpu);
615 
616 	/*
617 	 * Drop what we picked up for L2 via svm_complete_interrupts() so it
618 	 * doesn't end up in L1.
619 	 */
620 	svm->vcpu.arch.nmi_injected = false;
621 	kvm_clear_exception_queue(&svm->vcpu);
622 	kvm_clear_interrupt_queue(&svm->vcpu);
623 
624 	return 0;
625 }
626 
627 /*
628  * Forcibly leave nested mode in order to be able to reset the VCPU later on.
629  */
630 void svm_leave_nested(struct vcpu_svm *svm)
631 {
632 	if (is_guest_mode(&svm->vcpu)) {
633 		struct vmcb *hsave = svm->nested.hsave;
634 		struct vmcb *vmcb = svm->vmcb;
635 
636 		svm->nested.nested_run_pending = 0;
637 		leave_guest_mode(&svm->vcpu);
638 		copy_vmcb_control_area(&vmcb->control, &hsave->control);
639 		nested_svm_uninit_mmu_context(&svm->vcpu);
640 	}
641 }
642 
643 static int nested_svm_exit_handled_msr(struct vcpu_svm *svm)
644 {
645 	u32 offset, msr, value;
646 	int write, mask;
647 
648 	if (!(svm->nested.ctl.intercept & (1ULL << INTERCEPT_MSR_PROT)))
649 		return NESTED_EXIT_HOST;
650 
651 	msr    = svm->vcpu.arch.regs[VCPU_REGS_RCX];
652 	offset = svm_msrpm_offset(msr);
653 	write  = svm->vmcb->control.exit_info_1 & 1;
654 	mask   = 1 << ((2 * (msr & 0xf)) + write);
655 
656 	if (offset == MSR_INVALID)
657 		return NESTED_EXIT_DONE;
658 
659 	/* Offset is in 32 bit units but need in 8 bit units */
660 	offset *= 4;
661 
662 	if (kvm_vcpu_read_guest(&svm->vcpu, svm->nested.ctl.msrpm_base_pa + offset, &value, 4))
663 		return NESTED_EXIT_DONE;
664 
665 	return (value & mask) ? NESTED_EXIT_DONE : NESTED_EXIT_HOST;
666 }
667 
668 static int nested_svm_intercept_ioio(struct vcpu_svm *svm)
669 {
670 	unsigned port, size, iopm_len;
671 	u16 val, mask;
672 	u8 start_bit;
673 	u64 gpa;
674 
675 	if (!(svm->nested.ctl.intercept & (1ULL << INTERCEPT_IOIO_PROT)))
676 		return NESTED_EXIT_HOST;
677 
678 	port = svm->vmcb->control.exit_info_1 >> 16;
679 	size = (svm->vmcb->control.exit_info_1 & SVM_IOIO_SIZE_MASK) >>
680 		SVM_IOIO_SIZE_SHIFT;
681 	gpa  = svm->nested.ctl.iopm_base_pa + (port / 8);
682 	start_bit = port % 8;
683 	iopm_len = (start_bit + size > 8) ? 2 : 1;
684 	mask = (0xf >> (4 - size)) << start_bit;
685 	val = 0;
686 
687 	if (kvm_vcpu_read_guest(&svm->vcpu, gpa, &val, iopm_len))
688 		return NESTED_EXIT_DONE;
689 
690 	return (val & mask) ? NESTED_EXIT_DONE : NESTED_EXIT_HOST;
691 }
692 
693 static int nested_svm_intercept(struct vcpu_svm *svm)
694 {
695 	u32 exit_code = svm->vmcb->control.exit_code;
696 	int vmexit = NESTED_EXIT_HOST;
697 
698 	switch (exit_code) {
699 	case SVM_EXIT_MSR:
700 		vmexit = nested_svm_exit_handled_msr(svm);
701 		break;
702 	case SVM_EXIT_IOIO:
703 		vmexit = nested_svm_intercept_ioio(svm);
704 		break;
705 	case SVM_EXIT_READ_CR0 ... SVM_EXIT_WRITE_CR8: {
706 		u32 bit = 1U << (exit_code - SVM_EXIT_READ_CR0);
707 		if (svm->nested.ctl.intercept_cr & bit)
708 			vmexit = NESTED_EXIT_DONE;
709 		break;
710 	}
711 	case SVM_EXIT_READ_DR0 ... SVM_EXIT_WRITE_DR7: {
712 		u32 bit = 1U << (exit_code - SVM_EXIT_READ_DR0);
713 		if (svm->nested.ctl.intercept_dr & bit)
714 			vmexit = NESTED_EXIT_DONE;
715 		break;
716 	}
717 	case SVM_EXIT_EXCP_BASE ... SVM_EXIT_EXCP_BASE + 0x1f: {
718 		/*
719 		 * Host-intercepted exceptions have been checked already in
720 		 * nested_svm_exit_special.  There is nothing to do here,
721 		 * the vmexit is injected by svm_check_nested_events.
722 		 */
723 		vmexit = NESTED_EXIT_DONE;
724 		break;
725 	}
726 	case SVM_EXIT_ERR: {
727 		vmexit = NESTED_EXIT_DONE;
728 		break;
729 	}
730 	default: {
731 		u64 exit_bits = 1ULL << (exit_code - SVM_EXIT_INTR);
732 		if (svm->nested.ctl.intercept & exit_bits)
733 			vmexit = NESTED_EXIT_DONE;
734 	}
735 	}
736 
737 	return vmexit;
738 }
739 
740 int nested_svm_exit_handled(struct vcpu_svm *svm)
741 {
742 	int vmexit;
743 
744 	vmexit = nested_svm_intercept(svm);
745 
746 	if (vmexit == NESTED_EXIT_DONE)
747 		nested_svm_vmexit(svm);
748 
749 	return vmexit;
750 }
751 
752 int nested_svm_check_permissions(struct vcpu_svm *svm)
753 {
754 	if (!(svm->vcpu.arch.efer & EFER_SVME) ||
755 	    !is_paging(&svm->vcpu)) {
756 		kvm_queue_exception(&svm->vcpu, UD_VECTOR);
757 		return 1;
758 	}
759 
760 	if (svm->vmcb->save.cpl) {
761 		kvm_inject_gp(&svm->vcpu, 0);
762 		return 1;
763 	}
764 
765 	return 0;
766 }
767 
768 static bool nested_exit_on_exception(struct vcpu_svm *svm)
769 {
770 	unsigned int nr = svm->vcpu.arch.exception.nr;
771 
772 	return (svm->nested.ctl.intercept_exceptions & (1 << nr));
773 }
774 
775 static void nested_svm_inject_exception_vmexit(struct vcpu_svm *svm)
776 {
777 	unsigned int nr = svm->vcpu.arch.exception.nr;
778 
779 	svm->vmcb->control.exit_code = SVM_EXIT_EXCP_BASE + nr;
780 	svm->vmcb->control.exit_code_hi = 0;
781 
782 	if (svm->vcpu.arch.exception.has_error_code)
783 		svm->vmcb->control.exit_info_1 = svm->vcpu.arch.exception.error_code;
784 
785 	/*
786 	 * EXITINFO2 is undefined for all exception intercepts other
787 	 * than #PF.
788 	 */
789 	if (nr == PF_VECTOR) {
790 		if (svm->vcpu.arch.exception.nested_apf)
791 			svm->vmcb->control.exit_info_2 = svm->vcpu.arch.apf.nested_apf_token;
792 		else if (svm->vcpu.arch.exception.has_payload)
793 			svm->vmcb->control.exit_info_2 = svm->vcpu.arch.exception.payload;
794 		else
795 			svm->vmcb->control.exit_info_2 = svm->vcpu.arch.cr2;
796 	} else if (nr == DB_VECTOR) {
797 		/* See inject_pending_event.  */
798 		kvm_deliver_exception_payload(&svm->vcpu);
799 		if (svm->vcpu.arch.dr7 & DR7_GD) {
800 			svm->vcpu.arch.dr7 &= ~DR7_GD;
801 			kvm_update_dr7(&svm->vcpu);
802 		}
803 	} else
804 		WARN_ON(svm->vcpu.arch.exception.has_payload);
805 
806 	nested_svm_vmexit(svm);
807 }
808 
809 static void nested_svm_smi(struct vcpu_svm *svm)
810 {
811 	svm->vmcb->control.exit_code = SVM_EXIT_SMI;
812 	svm->vmcb->control.exit_info_1 = 0;
813 	svm->vmcb->control.exit_info_2 = 0;
814 
815 	nested_svm_vmexit(svm);
816 }
817 
818 static void nested_svm_nmi(struct vcpu_svm *svm)
819 {
820 	svm->vmcb->control.exit_code = SVM_EXIT_NMI;
821 	svm->vmcb->control.exit_info_1 = 0;
822 	svm->vmcb->control.exit_info_2 = 0;
823 
824 	nested_svm_vmexit(svm);
825 }
826 
827 static void nested_svm_intr(struct vcpu_svm *svm)
828 {
829 	trace_kvm_nested_intr_vmexit(svm->vmcb->save.rip);
830 
831 	svm->vmcb->control.exit_code   = SVM_EXIT_INTR;
832 	svm->vmcb->control.exit_info_1 = 0;
833 	svm->vmcb->control.exit_info_2 = 0;
834 
835 	nested_svm_vmexit(svm);
836 }
837 
838 static inline bool nested_exit_on_init(struct vcpu_svm *svm)
839 {
840 	return (svm->nested.ctl.intercept & (1ULL << INTERCEPT_INIT));
841 }
842 
843 static void nested_svm_init(struct vcpu_svm *svm)
844 {
845 	svm->vmcb->control.exit_code   = SVM_EXIT_INIT;
846 	svm->vmcb->control.exit_info_1 = 0;
847 	svm->vmcb->control.exit_info_2 = 0;
848 
849 	nested_svm_vmexit(svm);
850 }
851 
852 
853 static int svm_check_nested_events(struct kvm_vcpu *vcpu)
854 {
855 	struct vcpu_svm *svm = to_svm(vcpu);
856 	bool block_nested_events =
857 		kvm_event_needs_reinjection(vcpu) || svm->nested.nested_run_pending;
858 	struct kvm_lapic *apic = vcpu->arch.apic;
859 
860 	if (lapic_in_kernel(vcpu) &&
861 	    test_bit(KVM_APIC_INIT, &apic->pending_events)) {
862 		if (block_nested_events)
863 			return -EBUSY;
864 		if (!nested_exit_on_init(svm))
865 			return 0;
866 		nested_svm_init(svm);
867 		return 0;
868 	}
869 
870 	if (vcpu->arch.exception.pending) {
871 		if (block_nested_events)
872                         return -EBUSY;
873 		if (!nested_exit_on_exception(svm))
874 			return 0;
875 		nested_svm_inject_exception_vmexit(svm);
876 		return 0;
877 	}
878 
879 	if (vcpu->arch.smi_pending && !svm_smi_blocked(vcpu)) {
880 		if (block_nested_events)
881 			return -EBUSY;
882 		if (!nested_exit_on_smi(svm))
883 			return 0;
884 		nested_svm_smi(svm);
885 		return 0;
886 	}
887 
888 	if (vcpu->arch.nmi_pending && !svm_nmi_blocked(vcpu)) {
889 		if (block_nested_events)
890 			return -EBUSY;
891 		if (!nested_exit_on_nmi(svm))
892 			return 0;
893 		nested_svm_nmi(svm);
894 		return 0;
895 	}
896 
897 	if (kvm_cpu_has_interrupt(vcpu) && !svm_interrupt_blocked(vcpu)) {
898 		if (block_nested_events)
899 			return -EBUSY;
900 		if (!nested_exit_on_intr(svm))
901 			return 0;
902 		nested_svm_intr(svm);
903 		return 0;
904 	}
905 
906 	return 0;
907 }
908 
909 int nested_svm_exit_special(struct vcpu_svm *svm)
910 {
911 	u32 exit_code = svm->vmcb->control.exit_code;
912 
913 	switch (exit_code) {
914 	case SVM_EXIT_INTR:
915 	case SVM_EXIT_NMI:
916 	case SVM_EXIT_NPF:
917 		return NESTED_EXIT_HOST;
918 	case SVM_EXIT_EXCP_BASE ... SVM_EXIT_EXCP_BASE + 0x1f: {
919 		u32 excp_bits = 1 << (exit_code - SVM_EXIT_EXCP_BASE);
920 
921 		if (get_host_vmcb(svm)->control.intercept_exceptions & excp_bits)
922 			return NESTED_EXIT_HOST;
923 		else if (exit_code == SVM_EXIT_EXCP_BASE + PF_VECTOR &&
924 			 svm->vcpu.arch.apf.host_apf_flags)
925 			/* Trap async PF even if not shadowing */
926 			return NESTED_EXIT_HOST;
927 		break;
928 	}
929 	default:
930 		break;
931 	}
932 
933 	return NESTED_EXIT_CONTINUE;
934 }
935 
936 static int svm_get_nested_state(struct kvm_vcpu *vcpu,
937 				struct kvm_nested_state __user *user_kvm_nested_state,
938 				u32 user_data_size)
939 {
940 	struct vcpu_svm *svm;
941 	struct kvm_nested_state kvm_state = {
942 		.flags = 0,
943 		.format = KVM_STATE_NESTED_FORMAT_SVM,
944 		.size = sizeof(kvm_state),
945 	};
946 	struct vmcb __user *user_vmcb = (struct vmcb __user *)
947 		&user_kvm_nested_state->data.svm[0];
948 
949 	if (!vcpu)
950 		return kvm_state.size + KVM_STATE_NESTED_SVM_VMCB_SIZE;
951 
952 	svm = to_svm(vcpu);
953 
954 	if (user_data_size < kvm_state.size)
955 		goto out;
956 
957 	/* First fill in the header and copy it out.  */
958 	if (is_guest_mode(vcpu)) {
959 		kvm_state.hdr.svm.vmcb_pa = svm->nested.vmcb;
960 		kvm_state.size += KVM_STATE_NESTED_SVM_VMCB_SIZE;
961 		kvm_state.flags |= KVM_STATE_NESTED_GUEST_MODE;
962 
963 		if (svm->nested.nested_run_pending)
964 			kvm_state.flags |= KVM_STATE_NESTED_RUN_PENDING;
965 	}
966 
967 	if (gif_set(svm))
968 		kvm_state.flags |= KVM_STATE_NESTED_GIF_SET;
969 
970 	if (copy_to_user(user_kvm_nested_state, &kvm_state, sizeof(kvm_state)))
971 		return -EFAULT;
972 
973 	if (!is_guest_mode(vcpu))
974 		goto out;
975 
976 	/*
977 	 * Copy over the full size of the VMCB rather than just the size
978 	 * of the structs.
979 	 */
980 	if (clear_user(user_vmcb, KVM_STATE_NESTED_SVM_VMCB_SIZE))
981 		return -EFAULT;
982 	if (copy_to_user(&user_vmcb->control, &svm->nested.ctl,
983 			 sizeof(user_vmcb->control)))
984 		return -EFAULT;
985 	if (copy_to_user(&user_vmcb->save, &svm->nested.hsave->save,
986 			 sizeof(user_vmcb->save)))
987 		return -EFAULT;
988 
989 out:
990 	return kvm_state.size;
991 }
992 
993 static int svm_set_nested_state(struct kvm_vcpu *vcpu,
994 				struct kvm_nested_state __user *user_kvm_nested_state,
995 				struct kvm_nested_state *kvm_state)
996 {
997 	struct vcpu_svm *svm = to_svm(vcpu);
998 	struct vmcb *hsave = svm->nested.hsave;
999 	struct vmcb __user *user_vmcb = (struct vmcb __user *)
1000 		&user_kvm_nested_state->data.svm[0];
1001 	struct vmcb_control_area ctl;
1002 	struct vmcb_save_area save;
1003 	u32 cr0;
1004 
1005 	if (kvm_state->format != KVM_STATE_NESTED_FORMAT_SVM)
1006 		return -EINVAL;
1007 
1008 	if (kvm_state->flags & ~(KVM_STATE_NESTED_GUEST_MODE |
1009 				 KVM_STATE_NESTED_RUN_PENDING |
1010 				 KVM_STATE_NESTED_GIF_SET))
1011 		return -EINVAL;
1012 
1013 	/*
1014 	 * If in guest mode, vcpu->arch.efer actually refers to the L2 guest's
1015 	 * EFER.SVME, but EFER.SVME still has to be 1 for VMRUN to succeed.
1016 	 */
1017 	if (!(vcpu->arch.efer & EFER_SVME)) {
1018 		/* GIF=1 and no guest mode are required if SVME=0.  */
1019 		if (kvm_state->flags != KVM_STATE_NESTED_GIF_SET)
1020 			return -EINVAL;
1021 	}
1022 
1023 	/* SMM temporarily disables SVM, so we cannot be in guest mode.  */
1024 	if (is_smm(vcpu) && (kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE))
1025 		return -EINVAL;
1026 
1027 	if (!(kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE)) {
1028 		svm_leave_nested(svm);
1029 		goto out_set_gif;
1030 	}
1031 
1032 	if (!page_address_valid(vcpu, kvm_state->hdr.svm.vmcb_pa))
1033 		return -EINVAL;
1034 	if (kvm_state->size < sizeof(*kvm_state) + KVM_STATE_NESTED_SVM_VMCB_SIZE)
1035 		return -EINVAL;
1036 	if (copy_from_user(&ctl, &user_vmcb->control, sizeof(ctl)))
1037 		return -EFAULT;
1038 	if (copy_from_user(&save, &user_vmcb->save, sizeof(save)))
1039 		return -EFAULT;
1040 
1041 	if (!nested_vmcb_check_controls(&ctl))
1042 		return -EINVAL;
1043 
1044 	/*
1045 	 * Processor state contains L2 state.  Check that it is
1046 	 * valid for guest mode (see nested_vmcb_checks).
1047 	 */
1048 	cr0 = kvm_read_cr0(vcpu);
1049         if (((cr0 & X86_CR0_CD) == 0) && (cr0 & X86_CR0_NW))
1050                 return -EINVAL;
1051 
1052 	/*
1053 	 * Validate host state saved from before VMRUN (see
1054 	 * nested_svm_check_permissions).
1055 	 * TODO: validate reserved bits for all saved state.
1056 	 */
1057 	if (!(save.cr0 & X86_CR0_PG))
1058 		return -EINVAL;
1059 
1060 	/*
1061 	 * All checks done, we can enter guest mode.  L1 control fields
1062 	 * come from the nested save state.  Guest state is already
1063 	 * in the registers, the save area of the nested state instead
1064 	 * contains saved L1 state.
1065 	 */
1066 	copy_vmcb_control_area(&hsave->control, &svm->vmcb->control);
1067 	hsave->save = save;
1068 
1069 	svm->nested.vmcb = kvm_state->hdr.svm.vmcb_pa;
1070 	load_nested_vmcb_control(svm, &ctl);
1071 	nested_prepare_vmcb_control(svm);
1072 
1073 out_set_gif:
1074 	svm_set_gif(svm, !!(kvm_state->flags & KVM_STATE_NESTED_GIF_SET));
1075 	return 0;
1076 }
1077 
1078 struct kvm_x86_nested_ops svm_nested_ops = {
1079 	.check_events = svm_check_nested_events,
1080 	.get_state = svm_get_nested_state,
1081 	.set_state = svm_set_nested_state,
1082 };
1083