xref: /openbmc/linux/arch/x86/kvm/svm/nested.c (revision 2c4adc9b)
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) KBUILD_MODNAME ": " 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 "smm.h"
29 #include "cpuid.h"
30 #include "lapic.h"
31 #include "svm.h"
32 #include "hyperv.h"
33 
34 #define CC KVM_NESTED_VMENTER_CONSISTENCY_CHECK
35 
nested_svm_inject_npf_exit(struct kvm_vcpu * vcpu,struct x86_exception * fault)36 static void nested_svm_inject_npf_exit(struct kvm_vcpu *vcpu,
37 				       struct x86_exception *fault)
38 {
39 	struct vcpu_svm *svm = to_svm(vcpu);
40 	struct vmcb *vmcb = svm->vmcb;
41 
42 	if (vmcb->control.exit_code != SVM_EXIT_NPF) {
43 		/*
44 		 * TODO: track the cause of the nested page fault, and
45 		 * correctly fill in the high bits of exit_info_1.
46 		 */
47 		vmcb->control.exit_code = SVM_EXIT_NPF;
48 		vmcb->control.exit_code_hi = 0;
49 		vmcb->control.exit_info_1 = (1ULL << 32);
50 		vmcb->control.exit_info_2 = fault->address;
51 	}
52 
53 	vmcb->control.exit_info_1 &= ~0xffffffffULL;
54 	vmcb->control.exit_info_1 |= fault->error_code;
55 
56 	nested_svm_vmexit(svm);
57 }
58 
nested_svm_get_tdp_pdptr(struct kvm_vcpu * vcpu,int index)59 static u64 nested_svm_get_tdp_pdptr(struct kvm_vcpu *vcpu, int index)
60 {
61 	struct vcpu_svm *svm = to_svm(vcpu);
62 	u64 cr3 = svm->nested.ctl.nested_cr3;
63 	u64 pdpte;
64 	int ret;
65 
66 	/*
67 	 * Note, nCR3 is "assumed" to be 32-byte aligned, i.e. the CPU ignores
68 	 * nCR3[4:0] when loading PDPTEs from memory.
69 	 */
70 	ret = kvm_vcpu_read_guest_page(vcpu, gpa_to_gfn(cr3), &pdpte,
71 				       (cr3 & GENMASK(11, 5)) + index * 8, 8);
72 	if (ret)
73 		return 0;
74 	return pdpte;
75 }
76 
nested_svm_get_tdp_cr3(struct kvm_vcpu * vcpu)77 static unsigned long nested_svm_get_tdp_cr3(struct kvm_vcpu *vcpu)
78 {
79 	struct vcpu_svm *svm = to_svm(vcpu);
80 
81 	return svm->nested.ctl.nested_cr3;
82 }
83 
nested_svm_init_mmu_context(struct kvm_vcpu * vcpu)84 static void nested_svm_init_mmu_context(struct kvm_vcpu *vcpu)
85 {
86 	struct vcpu_svm *svm = to_svm(vcpu);
87 
88 	WARN_ON(mmu_is_nested(vcpu));
89 
90 	vcpu->arch.mmu = &vcpu->arch.guest_mmu;
91 
92 	/*
93 	 * The NPT format depends on L1's CR4 and EFER, which is in vmcb01.  Note,
94 	 * when called via KVM_SET_NESTED_STATE, that state may _not_ match current
95 	 * vCPU state.  CR0.WP is explicitly ignored, while CR0.PG is required.
96 	 */
97 	kvm_init_shadow_npt_mmu(vcpu, X86_CR0_PG, svm->vmcb01.ptr->save.cr4,
98 				svm->vmcb01.ptr->save.efer,
99 				svm->nested.ctl.nested_cr3);
100 	vcpu->arch.mmu->get_guest_pgd     = nested_svm_get_tdp_cr3;
101 	vcpu->arch.mmu->get_pdptr         = nested_svm_get_tdp_pdptr;
102 	vcpu->arch.mmu->inject_page_fault = nested_svm_inject_npf_exit;
103 	vcpu->arch.walk_mmu              = &vcpu->arch.nested_mmu;
104 }
105 
nested_svm_uninit_mmu_context(struct kvm_vcpu * vcpu)106 static void nested_svm_uninit_mmu_context(struct kvm_vcpu *vcpu)
107 {
108 	vcpu->arch.mmu = &vcpu->arch.root_mmu;
109 	vcpu->arch.walk_mmu = &vcpu->arch.root_mmu;
110 }
111 
nested_vmcb_needs_vls_intercept(struct vcpu_svm * svm)112 static bool nested_vmcb_needs_vls_intercept(struct vcpu_svm *svm)
113 {
114 	if (!guest_can_use(&svm->vcpu, X86_FEATURE_V_VMSAVE_VMLOAD))
115 		return true;
116 
117 	if (!nested_npt_enabled(svm))
118 		return true;
119 
120 	if (!(svm->nested.ctl.virt_ext & VIRTUAL_VMLOAD_VMSAVE_ENABLE_MASK))
121 		return true;
122 
123 	return false;
124 }
125 
recalc_intercepts(struct vcpu_svm * svm)126 void recalc_intercepts(struct vcpu_svm *svm)
127 {
128 	struct vmcb_control_area *c, *h;
129 	struct vmcb_ctrl_area_cached *g;
130 	unsigned int i;
131 
132 	vmcb_mark_dirty(svm->vmcb, VMCB_INTERCEPTS);
133 
134 	if (!is_guest_mode(&svm->vcpu))
135 		return;
136 
137 	c = &svm->vmcb->control;
138 	h = &svm->vmcb01.ptr->control;
139 	g = &svm->nested.ctl;
140 
141 	for (i = 0; i < MAX_INTERCEPT; i++)
142 		c->intercepts[i] = h->intercepts[i];
143 
144 	if (g->int_ctl & V_INTR_MASKING_MASK) {
145 		/*
146 		 * If L2 is active and V_INTR_MASKING is enabled in vmcb12,
147 		 * disable intercept of CR8 writes as L2's CR8 does not affect
148 		 * any interrupt KVM may want to inject.
149 		 *
150 		 * Similarly, disable intercept of virtual interrupts (used to
151 		 * detect interrupt windows) if the saved RFLAGS.IF is '0', as
152 		 * the effective RFLAGS.IF for L1 interrupts will never be set
153 		 * while L2 is running (L2's RFLAGS.IF doesn't affect L1 IRQs).
154 		 */
155 		vmcb_clr_intercept(c, INTERCEPT_CR8_WRITE);
156 		if (!(svm->vmcb01.ptr->save.rflags & X86_EFLAGS_IF))
157 			vmcb_clr_intercept(c, INTERCEPT_VINTR);
158 	}
159 
160 	/*
161 	 * We want to see VMMCALLs from a nested guest only when Hyper-V L2 TLB
162 	 * flush feature is enabled.
163 	 */
164 	if (!nested_svm_l2_tlb_flush_enabled(&svm->vcpu))
165 		vmcb_clr_intercept(c, INTERCEPT_VMMCALL);
166 
167 	for (i = 0; i < MAX_INTERCEPT; i++)
168 		c->intercepts[i] |= g->intercepts[i];
169 
170 	/* If SMI is not intercepted, ignore guest SMI intercept as well  */
171 	if (!intercept_smi)
172 		vmcb_clr_intercept(c, INTERCEPT_SMI);
173 
174 	if (nested_vmcb_needs_vls_intercept(svm)) {
175 		/*
176 		 * If the virtual VMLOAD/VMSAVE is not enabled for the L2,
177 		 * we must intercept these instructions to correctly
178 		 * emulate them in case L1 doesn't intercept them.
179 		 */
180 		vmcb_set_intercept(c, INTERCEPT_VMLOAD);
181 		vmcb_set_intercept(c, INTERCEPT_VMSAVE);
182 	} else {
183 		WARN_ON(!(c->virt_ext & VIRTUAL_VMLOAD_VMSAVE_ENABLE_MASK));
184 	}
185 }
186 
187 /*
188  * Merge L0's (KVM) and L1's (Nested VMCB) MSR permission bitmaps. The function
189  * is optimized in that it only merges the parts where KVM MSR permission bitmap
190  * may contain zero bits.
191  */
nested_svm_vmrun_msrpm(struct vcpu_svm * svm)192 static bool nested_svm_vmrun_msrpm(struct vcpu_svm *svm)
193 {
194 	struct hv_vmcb_enlightenments *hve = &svm->nested.ctl.hv_enlightenments;
195 	int i;
196 
197 	/*
198 	 * MSR bitmap update can be skipped when:
199 	 * - MSR bitmap for L1 hasn't changed.
200 	 * - Nested hypervisor (L1) is attempting to launch the same L2 as
201 	 *   before.
202 	 * - Nested hypervisor (L1) is using Hyper-V emulation interface and
203 	 * tells KVM (L0) there were no changes in MSR bitmap for L2.
204 	 */
205 	if (!svm->nested.force_msr_bitmap_recalc &&
206 	    kvm_hv_hypercall_enabled(&svm->vcpu) &&
207 	    hve->hv_enlightenments_control.msr_bitmap &&
208 	    (svm->nested.ctl.clean & BIT(HV_VMCB_NESTED_ENLIGHTENMENTS)))
209 		goto set_msrpm_base_pa;
210 
211 	if (!(vmcb12_is_intercept(&svm->nested.ctl, INTERCEPT_MSR_PROT)))
212 		return true;
213 
214 	for (i = 0; i < MSRPM_OFFSETS; i++) {
215 		u32 value, p;
216 		u64 offset;
217 
218 		if (msrpm_offsets[i] == 0xffffffff)
219 			break;
220 
221 		p      = msrpm_offsets[i];
222 
223 		/* x2apic msrs are intercepted always for the nested guest */
224 		if (is_x2apic_msrpm_offset(p))
225 			continue;
226 
227 		offset = svm->nested.ctl.msrpm_base_pa + (p * 4);
228 
229 		if (kvm_vcpu_read_guest(&svm->vcpu, offset, &value, 4))
230 			return false;
231 
232 		svm->nested.msrpm[p] = svm->msrpm[p] | value;
233 	}
234 
235 	svm->nested.force_msr_bitmap_recalc = false;
236 
237 set_msrpm_base_pa:
238 	svm->vmcb->control.msrpm_base_pa = __sme_set(__pa(svm->nested.msrpm));
239 
240 	return true;
241 }
242 
243 /*
244  * Bits 11:0 of bitmap address are ignored by hardware
245  */
nested_svm_check_bitmap_pa(struct kvm_vcpu * vcpu,u64 pa,u32 size)246 static bool nested_svm_check_bitmap_pa(struct kvm_vcpu *vcpu, u64 pa, u32 size)
247 {
248 	u64 addr = PAGE_ALIGN(pa);
249 
250 	return kvm_vcpu_is_legal_gpa(vcpu, addr) &&
251 	    kvm_vcpu_is_legal_gpa(vcpu, addr + size - 1);
252 }
253 
__nested_vmcb_check_controls(struct kvm_vcpu * vcpu,struct vmcb_ctrl_area_cached * control)254 static bool __nested_vmcb_check_controls(struct kvm_vcpu *vcpu,
255 					 struct vmcb_ctrl_area_cached *control)
256 {
257 	if (CC(!vmcb12_is_intercept(control, INTERCEPT_VMRUN)))
258 		return false;
259 
260 	if (CC(control->asid == 0))
261 		return false;
262 
263 	if (CC((control->nested_ctl & SVM_NESTED_CTL_NP_ENABLE) && !npt_enabled))
264 		return false;
265 
266 	if (CC(!nested_svm_check_bitmap_pa(vcpu, control->msrpm_base_pa,
267 					   MSRPM_SIZE)))
268 		return false;
269 	if (CC(!nested_svm_check_bitmap_pa(vcpu, control->iopm_base_pa,
270 					   IOPM_SIZE)))
271 		return false;
272 
273 	if (CC((control->int_ctl & V_NMI_ENABLE_MASK) &&
274 	       !vmcb12_is_intercept(control, INTERCEPT_NMI))) {
275 		return false;
276 	}
277 
278 	return true;
279 }
280 
281 /* Common checks that apply to both L1 and L2 state.  */
__nested_vmcb_check_save(struct kvm_vcpu * vcpu,struct vmcb_save_area_cached * save)282 static bool __nested_vmcb_check_save(struct kvm_vcpu *vcpu,
283 				     struct vmcb_save_area_cached *save)
284 {
285 	if (CC(!(save->efer & EFER_SVME)))
286 		return false;
287 
288 	if (CC((save->cr0 & X86_CR0_CD) == 0 && (save->cr0 & X86_CR0_NW)) ||
289 	    CC(save->cr0 & ~0xffffffffULL))
290 		return false;
291 
292 	if (CC(!kvm_dr6_valid(save->dr6)) || CC(!kvm_dr7_valid(save->dr7)))
293 		return false;
294 
295 	/*
296 	 * These checks are also performed by KVM_SET_SREGS,
297 	 * except that EFER.LMA is not checked by SVM against
298 	 * CR0.PG && EFER.LME.
299 	 */
300 	if ((save->efer & EFER_LME) && (save->cr0 & X86_CR0_PG)) {
301 		if (CC(!(save->cr4 & X86_CR4_PAE)) ||
302 		    CC(!(save->cr0 & X86_CR0_PE)) ||
303 		    CC(kvm_vcpu_is_illegal_gpa(vcpu, save->cr3)))
304 			return false;
305 	}
306 
307 	/* Note, SVM doesn't have any additional restrictions on CR4. */
308 	if (CC(!__kvm_is_valid_cr4(vcpu, save->cr4)))
309 		return false;
310 
311 	if (CC(!kvm_valid_efer(vcpu, save->efer)))
312 		return false;
313 
314 	return true;
315 }
316 
nested_vmcb_check_save(struct kvm_vcpu * vcpu)317 static bool nested_vmcb_check_save(struct kvm_vcpu *vcpu)
318 {
319 	struct vcpu_svm *svm = to_svm(vcpu);
320 	struct vmcb_save_area_cached *save = &svm->nested.save;
321 
322 	return __nested_vmcb_check_save(vcpu, save);
323 }
324 
nested_vmcb_check_controls(struct kvm_vcpu * vcpu)325 static bool nested_vmcb_check_controls(struct kvm_vcpu *vcpu)
326 {
327 	struct vcpu_svm *svm = to_svm(vcpu);
328 	struct vmcb_ctrl_area_cached *ctl = &svm->nested.ctl;
329 
330 	return __nested_vmcb_check_controls(vcpu, ctl);
331 }
332 
333 static
__nested_copy_vmcb_control_to_cache(struct kvm_vcpu * vcpu,struct vmcb_ctrl_area_cached * to,struct vmcb_control_area * from)334 void __nested_copy_vmcb_control_to_cache(struct kvm_vcpu *vcpu,
335 					 struct vmcb_ctrl_area_cached *to,
336 					 struct vmcb_control_area *from)
337 {
338 	unsigned int i;
339 
340 	for (i = 0; i < MAX_INTERCEPT; i++)
341 		to->intercepts[i] = from->intercepts[i];
342 
343 	to->iopm_base_pa        = from->iopm_base_pa;
344 	to->msrpm_base_pa       = from->msrpm_base_pa;
345 	to->tsc_offset          = from->tsc_offset;
346 	to->tlb_ctl             = from->tlb_ctl;
347 	to->int_ctl             = from->int_ctl;
348 	to->int_vector          = from->int_vector;
349 	to->int_state           = from->int_state;
350 	to->exit_code           = from->exit_code;
351 	to->exit_code_hi        = from->exit_code_hi;
352 	to->exit_info_1         = from->exit_info_1;
353 	to->exit_info_2         = from->exit_info_2;
354 	to->exit_int_info       = from->exit_int_info;
355 	to->exit_int_info_err   = from->exit_int_info_err;
356 	to->nested_ctl          = from->nested_ctl;
357 	to->event_inj           = from->event_inj;
358 	to->event_inj_err       = from->event_inj_err;
359 	to->next_rip            = from->next_rip;
360 	to->nested_cr3          = from->nested_cr3;
361 	to->virt_ext            = from->virt_ext;
362 	to->pause_filter_count  = from->pause_filter_count;
363 	to->pause_filter_thresh = from->pause_filter_thresh;
364 
365 	/* Copy asid here because nested_vmcb_check_controls will check it.  */
366 	to->asid           = from->asid;
367 	to->msrpm_base_pa &= ~0x0fffULL;
368 	to->iopm_base_pa  &= ~0x0fffULL;
369 
370 	/* Hyper-V extensions (Enlightened VMCB) */
371 	if (kvm_hv_hypercall_enabled(vcpu)) {
372 		to->clean = from->clean;
373 		memcpy(&to->hv_enlightenments, &from->hv_enlightenments,
374 		       sizeof(to->hv_enlightenments));
375 	}
376 }
377 
nested_copy_vmcb_control_to_cache(struct vcpu_svm * svm,struct vmcb_control_area * control)378 void nested_copy_vmcb_control_to_cache(struct vcpu_svm *svm,
379 				       struct vmcb_control_area *control)
380 {
381 	__nested_copy_vmcb_control_to_cache(&svm->vcpu, &svm->nested.ctl, control);
382 }
383 
__nested_copy_vmcb_save_to_cache(struct vmcb_save_area_cached * to,struct vmcb_save_area * from)384 static void __nested_copy_vmcb_save_to_cache(struct vmcb_save_area_cached *to,
385 					     struct vmcb_save_area *from)
386 {
387 	/*
388 	 * Copy only fields that are validated, as we need them
389 	 * to avoid TOC/TOU races.
390 	 */
391 	to->efer = from->efer;
392 	to->cr0 = from->cr0;
393 	to->cr3 = from->cr3;
394 	to->cr4 = from->cr4;
395 
396 	to->dr6 = from->dr6;
397 	to->dr7 = from->dr7;
398 }
399 
nested_copy_vmcb_save_to_cache(struct vcpu_svm * svm,struct vmcb_save_area * save)400 void nested_copy_vmcb_save_to_cache(struct vcpu_svm *svm,
401 				    struct vmcb_save_area *save)
402 {
403 	__nested_copy_vmcb_save_to_cache(&svm->nested.save, save);
404 }
405 
406 /*
407  * Synchronize fields that are written by the processor, so that
408  * they can be copied back into the vmcb12.
409  */
nested_sync_control_from_vmcb02(struct vcpu_svm * svm)410 void nested_sync_control_from_vmcb02(struct vcpu_svm *svm)
411 {
412 	u32 mask;
413 	svm->nested.ctl.event_inj      = svm->vmcb->control.event_inj;
414 	svm->nested.ctl.event_inj_err  = svm->vmcb->control.event_inj_err;
415 
416 	/* Only a few fields of int_ctl are written by the processor.  */
417 	mask = V_IRQ_MASK | V_TPR_MASK;
418 	/*
419 	 * Don't sync vmcb02 V_IRQ back to vmcb12 if KVM (L0) is intercepting
420 	 * virtual interrupts in order to request an interrupt window, as KVM
421 	 * has usurped vmcb02's int_ctl.  If an interrupt window opens before
422 	 * the next VM-Exit, svm_clear_vintr() will restore vmcb12's int_ctl.
423 	 * If no window opens, V_IRQ will be correctly preserved in vmcb12's
424 	 * int_ctl (because it was never recognized while L2 was running).
425 	 */
426 	if (svm_is_intercept(svm, INTERCEPT_VINTR) &&
427 	    !test_bit(INTERCEPT_VINTR, (unsigned long *)svm->nested.ctl.intercepts))
428 		mask &= ~V_IRQ_MASK;
429 
430 	if (nested_vgif_enabled(svm))
431 		mask |= V_GIF_MASK;
432 
433 	if (nested_vnmi_enabled(svm))
434 		mask |= V_NMI_BLOCKING_MASK | V_NMI_PENDING_MASK;
435 
436 	svm->nested.ctl.int_ctl        &= ~mask;
437 	svm->nested.ctl.int_ctl        |= svm->vmcb->control.int_ctl & mask;
438 }
439 
440 /*
441  * Transfer any event that L0 or L1 wanted to inject into L2 to
442  * EXIT_INT_INFO.
443  */
nested_save_pending_event_to_vmcb12(struct vcpu_svm * svm,struct vmcb * vmcb12)444 static void nested_save_pending_event_to_vmcb12(struct vcpu_svm *svm,
445 						struct vmcb *vmcb12)
446 {
447 	struct kvm_vcpu *vcpu = &svm->vcpu;
448 	u32 exit_int_info = 0;
449 	unsigned int nr;
450 
451 	if (vcpu->arch.exception.injected) {
452 		nr = vcpu->arch.exception.vector;
453 		exit_int_info = nr | SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_EXEPT;
454 
455 		if (vcpu->arch.exception.has_error_code) {
456 			exit_int_info |= SVM_EVTINJ_VALID_ERR;
457 			vmcb12->control.exit_int_info_err =
458 				vcpu->arch.exception.error_code;
459 		}
460 
461 	} else if (vcpu->arch.nmi_injected) {
462 		exit_int_info = SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_NMI;
463 
464 	} else if (vcpu->arch.interrupt.injected) {
465 		nr = vcpu->arch.interrupt.nr;
466 		exit_int_info = nr | SVM_EVTINJ_VALID;
467 
468 		if (vcpu->arch.interrupt.soft)
469 			exit_int_info |= SVM_EVTINJ_TYPE_SOFT;
470 		else
471 			exit_int_info |= SVM_EVTINJ_TYPE_INTR;
472 	}
473 
474 	vmcb12->control.exit_int_info = exit_int_info;
475 }
476 
nested_svm_transition_tlb_flush(struct kvm_vcpu * vcpu)477 static void nested_svm_transition_tlb_flush(struct kvm_vcpu *vcpu)
478 {
479 	/*
480 	 * KVM_REQ_HV_TLB_FLUSH flushes entries from either L1's VP_ID or
481 	 * L2's VP_ID upon request from the guest. Make sure we check for
482 	 * pending entries in the right FIFO upon L1/L2 transition as these
483 	 * requests are put by other vCPUs asynchronously.
484 	 */
485 	if (to_hv_vcpu(vcpu) && npt_enabled)
486 		kvm_make_request(KVM_REQ_HV_TLB_FLUSH, vcpu);
487 
488 	/*
489 	 * TODO: optimize unconditional TLB flush/MMU sync.  A partial list of
490 	 * things to fix before this can be conditional:
491 	 *
492 	 *  - Flush TLBs for both L1 and L2 remote TLB flush
493 	 *  - Honor L1's request to flush an ASID on nested VMRUN
494 	 *  - Sync nested NPT MMU on VMRUN that flushes L2's ASID[*]
495 	 *  - Don't crush a pending TLB flush in vmcb02 on nested VMRUN
496 	 *  - Flush L1's ASID on KVM_REQ_TLB_FLUSH_GUEST
497 	 *
498 	 * [*] Unlike nested EPT, SVM's ASID management can invalidate nested
499 	 *     NPT guest-physical mappings on VMRUN.
500 	 */
501 	kvm_make_request(KVM_REQ_MMU_SYNC, vcpu);
502 	kvm_make_request(KVM_REQ_TLB_FLUSH_CURRENT, vcpu);
503 }
504 
505 /*
506  * Load guest's/host's cr3 on nested vmentry or vmexit. @nested_npt is true
507  * if we are emulating VM-Entry into a guest with NPT enabled.
508  */
nested_svm_load_cr3(struct kvm_vcpu * vcpu,unsigned long cr3,bool nested_npt,bool reload_pdptrs)509 static int nested_svm_load_cr3(struct kvm_vcpu *vcpu, unsigned long cr3,
510 			       bool nested_npt, bool reload_pdptrs)
511 {
512 	if (CC(kvm_vcpu_is_illegal_gpa(vcpu, cr3)))
513 		return -EINVAL;
514 
515 	if (reload_pdptrs && !nested_npt && is_pae_paging(vcpu) &&
516 	    CC(!load_pdptrs(vcpu, cr3)))
517 		return -EINVAL;
518 
519 	vcpu->arch.cr3 = cr3;
520 
521 	/* Re-initialize the MMU, e.g. to pick up CR4 MMU role changes. */
522 	kvm_init_mmu(vcpu);
523 
524 	if (!nested_npt)
525 		kvm_mmu_new_pgd(vcpu, cr3);
526 
527 	return 0;
528 }
529 
nested_vmcb02_compute_g_pat(struct vcpu_svm * svm)530 void nested_vmcb02_compute_g_pat(struct vcpu_svm *svm)
531 {
532 	if (!svm->nested.vmcb02.ptr)
533 		return;
534 
535 	/* FIXME: merge g_pat from vmcb01 and vmcb12.  */
536 	svm->nested.vmcb02.ptr->save.g_pat = svm->vmcb01.ptr->save.g_pat;
537 }
538 
nested_vmcb02_prepare_save(struct vcpu_svm * svm,struct vmcb * vmcb12)539 static void nested_vmcb02_prepare_save(struct vcpu_svm *svm, struct vmcb *vmcb12)
540 {
541 	bool new_vmcb12 = false;
542 	struct vmcb *vmcb01 = svm->vmcb01.ptr;
543 	struct vmcb *vmcb02 = svm->nested.vmcb02.ptr;
544 	struct kvm_vcpu *vcpu = &svm->vcpu;
545 
546 	nested_vmcb02_compute_g_pat(svm);
547 
548 	/* Load the nested guest state */
549 	if (svm->nested.vmcb12_gpa != svm->nested.last_vmcb12_gpa) {
550 		new_vmcb12 = true;
551 		svm->nested.last_vmcb12_gpa = svm->nested.vmcb12_gpa;
552 		svm->nested.force_msr_bitmap_recalc = true;
553 	}
554 
555 	if (unlikely(new_vmcb12 || vmcb_is_dirty(vmcb12, VMCB_SEG))) {
556 		vmcb02->save.es = vmcb12->save.es;
557 		vmcb02->save.cs = vmcb12->save.cs;
558 		vmcb02->save.ss = vmcb12->save.ss;
559 		vmcb02->save.ds = vmcb12->save.ds;
560 		vmcb02->save.cpl = vmcb12->save.cpl;
561 		vmcb_mark_dirty(vmcb02, VMCB_SEG);
562 	}
563 
564 	if (unlikely(new_vmcb12 || vmcb_is_dirty(vmcb12, VMCB_DT))) {
565 		vmcb02->save.gdtr = vmcb12->save.gdtr;
566 		vmcb02->save.idtr = vmcb12->save.idtr;
567 		vmcb_mark_dirty(vmcb02, VMCB_DT);
568 	}
569 
570 	kvm_set_rflags(vcpu, vmcb12->save.rflags | X86_EFLAGS_FIXED);
571 
572 	svm_set_efer(vcpu, svm->nested.save.efer);
573 
574 	svm_set_cr0(vcpu, svm->nested.save.cr0);
575 	svm_set_cr4(vcpu, svm->nested.save.cr4);
576 
577 	svm->vcpu.arch.cr2 = vmcb12->save.cr2;
578 
579 	kvm_rax_write(vcpu, vmcb12->save.rax);
580 	kvm_rsp_write(vcpu, vmcb12->save.rsp);
581 	kvm_rip_write(vcpu, vmcb12->save.rip);
582 
583 	/* In case we don't even reach vcpu_run, the fields are not updated */
584 	vmcb02->save.rax = vmcb12->save.rax;
585 	vmcb02->save.rsp = vmcb12->save.rsp;
586 	vmcb02->save.rip = vmcb12->save.rip;
587 
588 	/* These bits will be set properly on the first execution when new_vmc12 is true */
589 	if (unlikely(new_vmcb12 || vmcb_is_dirty(vmcb12, VMCB_DR))) {
590 		vmcb02->save.dr7 = svm->nested.save.dr7 | DR7_FIXED_1;
591 		svm->vcpu.arch.dr6  = svm->nested.save.dr6 | DR6_ACTIVE_LOW;
592 		vmcb_mark_dirty(vmcb02, VMCB_DR);
593 	}
594 
595 	if (unlikely(guest_can_use(vcpu, X86_FEATURE_LBRV) &&
596 		     (svm->nested.ctl.virt_ext & LBR_CTL_ENABLE_MASK))) {
597 		/*
598 		 * Reserved bits of DEBUGCTL are ignored.  Be consistent with
599 		 * svm_set_msr's definition of reserved bits.
600 		 */
601 		svm_copy_lbrs(vmcb02, vmcb12);
602 		vmcb02->save.dbgctl &= ~DEBUGCTL_RESERVED_BITS;
603 		svm_update_lbrv(&svm->vcpu);
604 
605 	} else if (unlikely(vmcb01->control.virt_ext & LBR_CTL_ENABLE_MASK)) {
606 		svm_copy_lbrs(vmcb02, vmcb01);
607 	}
608 }
609 
is_evtinj_soft(u32 evtinj)610 static inline bool is_evtinj_soft(u32 evtinj)
611 {
612 	u32 type = evtinj & SVM_EVTINJ_TYPE_MASK;
613 	u8 vector = evtinj & SVM_EVTINJ_VEC_MASK;
614 
615 	if (!(evtinj & SVM_EVTINJ_VALID))
616 		return false;
617 
618 	if (type == SVM_EVTINJ_TYPE_SOFT)
619 		return true;
620 
621 	return type == SVM_EVTINJ_TYPE_EXEPT && kvm_exception_is_soft(vector);
622 }
623 
is_evtinj_nmi(u32 evtinj)624 static bool is_evtinj_nmi(u32 evtinj)
625 {
626 	u32 type = evtinj & SVM_EVTINJ_TYPE_MASK;
627 
628 	if (!(evtinj & SVM_EVTINJ_VALID))
629 		return false;
630 
631 	return type == SVM_EVTINJ_TYPE_NMI;
632 }
633 
nested_vmcb02_prepare_control(struct vcpu_svm * svm,unsigned long vmcb12_rip,unsigned long vmcb12_csbase)634 static void nested_vmcb02_prepare_control(struct vcpu_svm *svm,
635 					  unsigned long vmcb12_rip,
636 					  unsigned long vmcb12_csbase)
637 {
638 	u32 int_ctl_vmcb01_bits = V_INTR_MASKING_MASK;
639 	u32 int_ctl_vmcb12_bits = V_TPR_MASK | V_IRQ_INJECTION_BITS_MASK;
640 
641 	struct kvm_vcpu *vcpu = &svm->vcpu;
642 	struct vmcb *vmcb01 = svm->vmcb01.ptr;
643 	struct vmcb *vmcb02 = svm->nested.vmcb02.ptr;
644 	u32 pause_count12;
645 	u32 pause_thresh12;
646 
647 	/*
648 	 * Filled at exit: exit_code, exit_code_hi, exit_info_1, exit_info_2,
649 	 * exit_int_info, exit_int_info_err, next_rip, insn_len, insn_bytes.
650 	 */
651 
652 	if (guest_can_use(vcpu, X86_FEATURE_VGIF) &&
653 	    (svm->nested.ctl.int_ctl & V_GIF_ENABLE_MASK))
654 		int_ctl_vmcb12_bits |= (V_GIF_MASK | V_GIF_ENABLE_MASK);
655 	else
656 		int_ctl_vmcb01_bits |= (V_GIF_MASK | V_GIF_ENABLE_MASK);
657 
658 	if (vnmi) {
659 		if (vmcb01->control.int_ctl & V_NMI_PENDING_MASK) {
660 			svm->vcpu.arch.nmi_pending++;
661 			kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
662 		}
663 		if (nested_vnmi_enabled(svm))
664 			int_ctl_vmcb12_bits |= (V_NMI_PENDING_MASK |
665 						V_NMI_ENABLE_MASK |
666 						V_NMI_BLOCKING_MASK);
667 	}
668 
669 	/* Copied from vmcb01.  msrpm_base can be overwritten later.  */
670 	vmcb02->control.nested_ctl = vmcb01->control.nested_ctl;
671 	vmcb02->control.iopm_base_pa = vmcb01->control.iopm_base_pa;
672 	vmcb02->control.msrpm_base_pa = vmcb01->control.msrpm_base_pa;
673 
674 	/* Done at vmrun: asid.  */
675 
676 	/* Also overwritten later if necessary.  */
677 	vmcb02->control.tlb_ctl = TLB_CONTROL_DO_NOTHING;
678 
679 	/* nested_cr3.  */
680 	if (nested_npt_enabled(svm))
681 		nested_svm_init_mmu_context(vcpu);
682 
683 	vcpu->arch.tsc_offset = kvm_calc_nested_tsc_offset(
684 			vcpu->arch.l1_tsc_offset,
685 			svm->nested.ctl.tsc_offset,
686 			svm->tsc_ratio_msr);
687 
688 	vmcb02->control.tsc_offset = vcpu->arch.tsc_offset;
689 
690 	if (guest_can_use(vcpu, X86_FEATURE_TSCRATEMSR) &&
691 	    svm->tsc_ratio_msr != kvm_caps.default_tsc_scaling_ratio)
692 		nested_svm_update_tsc_ratio_msr(vcpu);
693 
694 	vmcb02->control.int_ctl             =
695 		(svm->nested.ctl.int_ctl & int_ctl_vmcb12_bits) |
696 		(vmcb01->control.int_ctl & int_ctl_vmcb01_bits);
697 
698 	vmcb02->control.int_vector          = svm->nested.ctl.int_vector;
699 	vmcb02->control.int_state           = svm->nested.ctl.int_state;
700 	vmcb02->control.event_inj           = svm->nested.ctl.event_inj;
701 	vmcb02->control.event_inj_err       = svm->nested.ctl.event_inj_err;
702 
703 	/*
704 	 * next_rip is consumed on VMRUN as the return address pushed on the
705 	 * stack for injected soft exceptions/interrupts.  If nrips is exposed
706 	 * to L1, take it verbatim from vmcb12.  If nrips is supported in
707 	 * hardware but not exposed to L1, stuff the actual L2 RIP to emulate
708 	 * what a nrips=0 CPU would do (L1 is responsible for advancing RIP
709 	 * prior to injecting the event).
710 	 */
711 	if (guest_can_use(vcpu, X86_FEATURE_NRIPS))
712 		vmcb02->control.next_rip    = svm->nested.ctl.next_rip;
713 	else if (boot_cpu_has(X86_FEATURE_NRIPS))
714 		vmcb02->control.next_rip    = vmcb12_rip;
715 
716 	svm->nmi_l1_to_l2 = is_evtinj_nmi(vmcb02->control.event_inj);
717 	if (is_evtinj_soft(vmcb02->control.event_inj)) {
718 		svm->soft_int_injected = true;
719 		svm->soft_int_csbase = vmcb12_csbase;
720 		svm->soft_int_old_rip = vmcb12_rip;
721 		if (guest_can_use(vcpu, X86_FEATURE_NRIPS))
722 			svm->soft_int_next_rip = svm->nested.ctl.next_rip;
723 		else
724 			svm->soft_int_next_rip = vmcb12_rip;
725 	}
726 
727 	vmcb02->control.virt_ext            = vmcb01->control.virt_ext &
728 					      LBR_CTL_ENABLE_MASK;
729 	if (guest_can_use(vcpu, X86_FEATURE_LBRV))
730 		vmcb02->control.virt_ext  |=
731 			(svm->nested.ctl.virt_ext & LBR_CTL_ENABLE_MASK);
732 
733 	if (!nested_vmcb_needs_vls_intercept(svm))
734 		vmcb02->control.virt_ext |= VIRTUAL_VMLOAD_VMSAVE_ENABLE_MASK;
735 
736 	if (guest_can_use(vcpu, X86_FEATURE_PAUSEFILTER))
737 		pause_count12 = svm->nested.ctl.pause_filter_count;
738 	else
739 		pause_count12 = 0;
740 	if (guest_can_use(vcpu, X86_FEATURE_PFTHRESHOLD))
741 		pause_thresh12 = svm->nested.ctl.pause_filter_thresh;
742 	else
743 		pause_thresh12 = 0;
744 	if (kvm_pause_in_guest(svm->vcpu.kvm)) {
745 		/* use guest values since host doesn't intercept PAUSE */
746 		vmcb02->control.pause_filter_count = pause_count12;
747 		vmcb02->control.pause_filter_thresh = pause_thresh12;
748 
749 	} else {
750 		/* start from host values otherwise */
751 		vmcb02->control.pause_filter_count = vmcb01->control.pause_filter_count;
752 		vmcb02->control.pause_filter_thresh = vmcb01->control.pause_filter_thresh;
753 
754 		/* ... but ensure filtering is disabled if so requested.  */
755 		if (vmcb12_is_intercept(&svm->nested.ctl, INTERCEPT_PAUSE)) {
756 			if (!pause_count12)
757 				vmcb02->control.pause_filter_count = 0;
758 			if (!pause_thresh12)
759 				vmcb02->control.pause_filter_thresh = 0;
760 		}
761 	}
762 
763 	nested_svm_transition_tlb_flush(vcpu);
764 
765 	/* Enter Guest-Mode */
766 	enter_guest_mode(vcpu);
767 
768 	/*
769 	 * Merge guest and host intercepts - must be called with vcpu in
770 	 * guest-mode to take effect.
771 	 */
772 	recalc_intercepts(svm);
773 }
774 
nested_svm_copy_common_state(struct vmcb * from_vmcb,struct vmcb * to_vmcb)775 static void nested_svm_copy_common_state(struct vmcb *from_vmcb, struct vmcb *to_vmcb)
776 {
777 	/*
778 	 * Some VMCB state is shared between L1 and L2 and thus has to be
779 	 * moved at the time of nested vmrun and vmexit.
780 	 *
781 	 * VMLOAD/VMSAVE state would also belong in this category, but KVM
782 	 * always performs VMLOAD and VMSAVE from the VMCB01.
783 	 */
784 	to_vmcb->save.spec_ctrl = from_vmcb->save.spec_ctrl;
785 }
786 
enter_svm_guest_mode(struct kvm_vcpu * vcpu,u64 vmcb12_gpa,struct vmcb * vmcb12,bool from_vmrun)787 int enter_svm_guest_mode(struct kvm_vcpu *vcpu, u64 vmcb12_gpa,
788 			 struct vmcb *vmcb12, bool from_vmrun)
789 {
790 	struct vcpu_svm *svm = to_svm(vcpu);
791 	int ret;
792 
793 	trace_kvm_nested_vmenter(svm->vmcb->save.rip,
794 				 vmcb12_gpa,
795 				 vmcb12->save.rip,
796 				 vmcb12->control.int_ctl,
797 				 vmcb12->control.event_inj,
798 				 vmcb12->control.nested_ctl,
799 				 vmcb12->control.nested_cr3,
800 				 vmcb12->save.cr3,
801 				 KVM_ISA_SVM);
802 
803 	trace_kvm_nested_intercepts(vmcb12->control.intercepts[INTERCEPT_CR] & 0xffff,
804 				    vmcb12->control.intercepts[INTERCEPT_CR] >> 16,
805 				    vmcb12->control.intercepts[INTERCEPT_EXCEPTION],
806 				    vmcb12->control.intercepts[INTERCEPT_WORD3],
807 				    vmcb12->control.intercepts[INTERCEPT_WORD4],
808 				    vmcb12->control.intercepts[INTERCEPT_WORD5]);
809 
810 
811 	svm->nested.vmcb12_gpa = vmcb12_gpa;
812 
813 	WARN_ON(svm->vmcb == svm->nested.vmcb02.ptr);
814 
815 	nested_svm_copy_common_state(svm->vmcb01.ptr, svm->nested.vmcb02.ptr);
816 
817 	svm_switch_vmcb(svm, &svm->nested.vmcb02);
818 	nested_vmcb02_prepare_control(svm, vmcb12->save.rip, vmcb12->save.cs.base);
819 	nested_vmcb02_prepare_save(svm, vmcb12);
820 
821 	ret = nested_svm_load_cr3(&svm->vcpu, svm->nested.save.cr3,
822 				  nested_npt_enabled(svm), from_vmrun);
823 	if (ret)
824 		return ret;
825 
826 	if (!from_vmrun)
827 		kvm_make_request(KVM_REQ_GET_NESTED_STATE_PAGES, vcpu);
828 
829 	svm_set_gif(svm, true);
830 
831 	if (kvm_vcpu_apicv_active(vcpu))
832 		kvm_make_request(KVM_REQ_APICV_UPDATE, vcpu);
833 
834 	nested_svm_hv_update_vm_vp_ids(vcpu);
835 
836 	return 0;
837 }
838 
nested_svm_vmrun(struct kvm_vcpu * vcpu)839 int nested_svm_vmrun(struct kvm_vcpu *vcpu)
840 {
841 	struct vcpu_svm *svm = to_svm(vcpu);
842 	int ret;
843 	struct vmcb *vmcb12;
844 	struct kvm_host_map map;
845 	u64 vmcb12_gpa;
846 	struct vmcb *vmcb01 = svm->vmcb01.ptr;
847 
848 	if (!svm->nested.hsave_msr) {
849 		kvm_inject_gp(vcpu, 0);
850 		return 1;
851 	}
852 
853 	if (is_smm(vcpu)) {
854 		kvm_queue_exception(vcpu, UD_VECTOR);
855 		return 1;
856 	}
857 
858 	/* This fails when VP assist page is enabled but the supplied GPA is bogus */
859 	ret = kvm_hv_verify_vp_assist(vcpu);
860 	if (ret) {
861 		kvm_inject_gp(vcpu, 0);
862 		return ret;
863 	}
864 
865 	vmcb12_gpa = svm->vmcb->save.rax;
866 	ret = kvm_vcpu_map(vcpu, gpa_to_gfn(vmcb12_gpa), &map);
867 	if (ret == -EINVAL) {
868 		kvm_inject_gp(vcpu, 0);
869 		return 1;
870 	} else if (ret) {
871 		return kvm_skip_emulated_instruction(vcpu);
872 	}
873 
874 	ret = kvm_skip_emulated_instruction(vcpu);
875 
876 	vmcb12 = map.hva;
877 
878 	if (WARN_ON_ONCE(!svm->nested.initialized))
879 		return -EINVAL;
880 
881 	nested_copy_vmcb_control_to_cache(svm, &vmcb12->control);
882 	nested_copy_vmcb_save_to_cache(svm, &vmcb12->save);
883 
884 	if (!nested_vmcb_check_save(vcpu) ||
885 	    !nested_vmcb_check_controls(vcpu)) {
886 		vmcb12->control.exit_code    = SVM_EXIT_ERR;
887 		vmcb12->control.exit_code_hi = 0;
888 		vmcb12->control.exit_info_1  = 0;
889 		vmcb12->control.exit_info_2  = 0;
890 		goto out;
891 	}
892 
893 	/*
894 	 * Since vmcb01 is not in use, we can use it to store some of the L1
895 	 * state.
896 	 */
897 	vmcb01->save.efer   = vcpu->arch.efer;
898 	vmcb01->save.cr0    = kvm_read_cr0(vcpu);
899 	vmcb01->save.cr4    = vcpu->arch.cr4;
900 	vmcb01->save.rflags = kvm_get_rflags(vcpu);
901 	vmcb01->save.rip    = kvm_rip_read(vcpu);
902 
903 	if (!npt_enabled)
904 		vmcb01->save.cr3 = kvm_read_cr3(vcpu);
905 
906 	svm->nested.nested_run_pending = 1;
907 
908 	if (enter_svm_guest_mode(vcpu, vmcb12_gpa, vmcb12, true))
909 		goto out_exit_err;
910 
911 	if (nested_svm_vmrun_msrpm(svm))
912 		goto out;
913 
914 out_exit_err:
915 	svm->nested.nested_run_pending = 0;
916 	svm->nmi_l1_to_l2 = false;
917 	svm->soft_int_injected = false;
918 
919 	svm->vmcb->control.exit_code    = SVM_EXIT_ERR;
920 	svm->vmcb->control.exit_code_hi = 0;
921 	svm->vmcb->control.exit_info_1  = 0;
922 	svm->vmcb->control.exit_info_2  = 0;
923 
924 	nested_svm_vmexit(svm);
925 
926 out:
927 	kvm_vcpu_unmap(vcpu, &map, true);
928 
929 	return ret;
930 }
931 
932 /* Copy state save area fields which are handled by VMRUN */
svm_copy_vmrun_state(struct vmcb_save_area * to_save,struct vmcb_save_area * from_save)933 void svm_copy_vmrun_state(struct vmcb_save_area *to_save,
934 			  struct vmcb_save_area *from_save)
935 {
936 	to_save->es = from_save->es;
937 	to_save->cs = from_save->cs;
938 	to_save->ss = from_save->ss;
939 	to_save->ds = from_save->ds;
940 	to_save->gdtr = from_save->gdtr;
941 	to_save->idtr = from_save->idtr;
942 	to_save->rflags = from_save->rflags | X86_EFLAGS_FIXED;
943 	to_save->efer = from_save->efer;
944 	to_save->cr0 = from_save->cr0;
945 	to_save->cr3 = from_save->cr3;
946 	to_save->cr4 = from_save->cr4;
947 	to_save->rax = from_save->rax;
948 	to_save->rsp = from_save->rsp;
949 	to_save->rip = from_save->rip;
950 	to_save->cpl = 0;
951 }
952 
svm_copy_vmloadsave_state(struct vmcb * to_vmcb,struct vmcb * from_vmcb)953 void svm_copy_vmloadsave_state(struct vmcb *to_vmcb, struct vmcb *from_vmcb)
954 {
955 	to_vmcb->save.fs = from_vmcb->save.fs;
956 	to_vmcb->save.gs = from_vmcb->save.gs;
957 	to_vmcb->save.tr = from_vmcb->save.tr;
958 	to_vmcb->save.ldtr = from_vmcb->save.ldtr;
959 	to_vmcb->save.kernel_gs_base = from_vmcb->save.kernel_gs_base;
960 	to_vmcb->save.star = from_vmcb->save.star;
961 	to_vmcb->save.lstar = from_vmcb->save.lstar;
962 	to_vmcb->save.cstar = from_vmcb->save.cstar;
963 	to_vmcb->save.sfmask = from_vmcb->save.sfmask;
964 	to_vmcb->save.sysenter_cs = from_vmcb->save.sysenter_cs;
965 	to_vmcb->save.sysenter_esp = from_vmcb->save.sysenter_esp;
966 	to_vmcb->save.sysenter_eip = from_vmcb->save.sysenter_eip;
967 }
968 
nested_svm_vmexit(struct vcpu_svm * svm)969 int nested_svm_vmexit(struct vcpu_svm *svm)
970 {
971 	struct kvm_vcpu *vcpu = &svm->vcpu;
972 	struct vmcb *vmcb01 = svm->vmcb01.ptr;
973 	struct vmcb *vmcb02 = svm->nested.vmcb02.ptr;
974 	struct vmcb *vmcb12;
975 	struct kvm_host_map map;
976 	int rc;
977 
978 	rc = kvm_vcpu_map(vcpu, gpa_to_gfn(svm->nested.vmcb12_gpa), &map);
979 	if (rc) {
980 		if (rc == -EINVAL)
981 			kvm_inject_gp(vcpu, 0);
982 		return 1;
983 	}
984 
985 	vmcb12 = map.hva;
986 
987 	/* Exit Guest-Mode */
988 	leave_guest_mode(vcpu);
989 	svm->nested.vmcb12_gpa = 0;
990 	WARN_ON_ONCE(svm->nested.nested_run_pending);
991 
992 	kvm_clear_request(KVM_REQ_GET_NESTED_STATE_PAGES, vcpu);
993 
994 	/* in case we halted in L2 */
995 	svm->vcpu.arch.mp_state = KVM_MP_STATE_RUNNABLE;
996 
997 	/* Give the current vmcb to the guest */
998 
999 	vmcb12->save.es     = vmcb02->save.es;
1000 	vmcb12->save.cs     = vmcb02->save.cs;
1001 	vmcb12->save.ss     = vmcb02->save.ss;
1002 	vmcb12->save.ds     = vmcb02->save.ds;
1003 	vmcb12->save.gdtr   = vmcb02->save.gdtr;
1004 	vmcb12->save.idtr   = vmcb02->save.idtr;
1005 	vmcb12->save.efer   = svm->vcpu.arch.efer;
1006 	vmcb12->save.cr0    = kvm_read_cr0(vcpu);
1007 	vmcb12->save.cr3    = kvm_read_cr3(vcpu);
1008 	vmcb12->save.cr2    = vmcb02->save.cr2;
1009 	vmcb12->save.cr4    = svm->vcpu.arch.cr4;
1010 	vmcb12->save.rflags = kvm_get_rflags(vcpu);
1011 	vmcb12->save.rip    = kvm_rip_read(vcpu);
1012 	vmcb12->save.rsp    = kvm_rsp_read(vcpu);
1013 	vmcb12->save.rax    = kvm_rax_read(vcpu);
1014 	vmcb12->save.dr7    = vmcb02->save.dr7;
1015 	vmcb12->save.dr6    = svm->vcpu.arch.dr6;
1016 	vmcb12->save.cpl    = vmcb02->save.cpl;
1017 
1018 	vmcb12->control.int_state         = vmcb02->control.int_state;
1019 	vmcb12->control.exit_code         = vmcb02->control.exit_code;
1020 	vmcb12->control.exit_code_hi      = vmcb02->control.exit_code_hi;
1021 	vmcb12->control.exit_info_1       = vmcb02->control.exit_info_1;
1022 	vmcb12->control.exit_info_2       = vmcb02->control.exit_info_2;
1023 
1024 	if (vmcb12->control.exit_code != SVM_EXIT_ERR)
1025 		nested_save_pending_event_to_vmcb12(svm, vmcb12);
1026 
1027 	if (guest_can_use(vcpu, X86_FEATURE_NRIPS))
1028 		vmcb12->control.next_rip  = vmcb02->control.next_rip;
1029 
1030 	vmcb12->control.int_ctl           = svm->nested.ctl.int_ctl;
1031 	vmcb12->control.event_inj         = svm->nested.ctl.event_inj;
1032 	vmcb12->control.event_inj_err     = svm->nested.ctl.event_inj_err;
1033 
1034 	if (!kvm_pause_in_guest(vcpu->kvm)) {
1035 		vmcb01->control.pause_filter_count = vmcb02->control.pause_filter_count;
1036 		vmcb_mark_dirty(vmcb01, VMCB_INTERCEPTS);
1037 
1038 	}
1039 
1040 	nested_svm_copy_common_state(svm->nested.vmcb02.ptr, svm->vmcb01.ptr);
1041 
1042 	svm_switch_vmcb(svm, &svm->vmcb01);
1043 
1044 	/*
1045 	 * Rules for synchronizing int_ctl bits from vmcb02 to vmcb01:
1046 	 *
1047 	 * V_IRQ, V_IRQ_VECTOR, V_INTR_PRIO_MASK, V_IGN_TPR:  If L1 doesn't
1048 	 * intercept interrupts, then KVM will use vmcb02's V_IRQ (and related
1049 	 * flags) to detect interrupt windows for L1 IRQs (even if L1 uses
1050 	 * virtual interrupt masking).  Raise KVM_REQ_EVENT to ensure that
1051 	 * KVM re-requests an interrupt window if necessary, which implicitly
1052 	 * copies this bits from vmcb02 to vmcb01.
1053 	 *
1054 	 * V_TPR: If L1 doesn't use virtual interrupt masking, then L1's vTPR
1055 	 * is stored in vmcb02, but its value doesn't need to be copied from/to
1056 	 * vmcb01 because it is copied from/to the virtual APIC's TPR register
1057 	 * on each VM entry/exit.
1058 	 *
1059 	 * V_GIF: If nested vGIF is not used, KVM uses vmcb02's V_GIF for L1's
1060 	 * V_GIF.  However, GIF is architecturally clear on each VM exit, thus
1061 	 * there is no need to copy V_GIF from vmcb02 to vmcb01.
1062 	 */
1063 	if (!nested_exit_on_intr(svm))
1064 		kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
1065 
1066 	if (unlikely(guest_can_use(vcpu, X86_FEATURE_LBRV) &&
1067 		     (svm->nested.ctl.virt_ext & LBR_CTL_ENABLE_MASK))) {
1068 		svm_copy_lbrs(vmcb12, vmcb02);
1069 		svm_update_lbrv(vcpu);
1070 	} else if (unlikely(vmcb01->control.virt_ext & LBR_CTL_ENABLE_MASK)) {
1071 		svm_copy_lbrs(vmcb01, vmcb02);
1072 		svm_update_lbrv(vcpu);
1073 	}
1074 
1075 	if (vnmi) {
1076 		if (vmcb02->control.int_ctl & V_NMI_BLOCKING_MASK)
1077 			vmcb01->control.int_ctl |= V_NMI_BLOCKING_MASK;
1078 		else
1079 			vmcb01->control.int_ctl &= ~V_NMI_BLOCKING_MASK;
1080 
1081 		if (vcpu->arch.nmi_pending) {
1082 			vcpu->arch.nmi_pending--;
1083 			vmcb01->control.int_ctl |= V_NMI_PENDING_MASK;
1084 		} else {
1085 			vmcb01->control.int_ctl &= ~V_NMI_PENDING_MASK;
1086 		}
1087 	}
1088 
1089 	/*
1090 	 * On vmexit the  GIF is set to false and
1091 	 * no event can be injected in L1.
1092 	 */
1093 	svm_set_gif(svm, false);
1094 	vmcb01->control.exit_int_info = 0;
1095 
1096 	svm->vcpu.arch.tsc_offset = svm->vcpu.arch.l1_tsc_offset;
1097 	if (vmcb01->control.tsc_offset != svm->vcpu.arch.tsc_offset) {
1098 		vmcb01->control.tsc_offset = svm->vcpu.arch.tsc_offset;
1099 		vmcb_mark_dirty(vmcb01, VMCB_INTERCEPTS);
1100 	}
1101 
1102 	if (kvm_caps.has_tsc_control &&
1103 	    vcpu->arch.tsc_scaling_ratio != vcpu->arch.l1_tsc_scaling_ratio) {
1104 		vcpu->arch.tsc_scaling_ratio = vcpu->arch.l1_tsc_scaling_ratio;
1105 		svm_write_tsc_multiplier(vcpu);
1106 	}
1107 
1108 	svm->nested.ctl.nested_cr3 = 0;
1109 
1110 	/*
1111 	 * Restore processor state that had been saved in vmcb01
1112 	 */
1113 	kvm_set_rflags(vcpu, vmcb01->save.rflags);
1114 	svm_set_efer(vcpu, vmcb01->save.efer);
1115 	svm_set_cr0(vcpu, vmcb01->save.cr0 | X86_CR0_PE);
1116 	svm_set_cr4(vcpu, vmcb01->save.cr4);
1117 	kvm_rax_write(vcpu, vmcb01->save.rax);
1118 	kvm_rsp_write(vcpu, vmcb01->save.rsp);
1119 	kvm_rip_write(vcpu, vmcb01->save.rip);
1120 
1121 	svm->vcpu.arch.dr7 = DR7_FIXED_1;
1122 	kvm_update_dr7(&svm->vcpu);
1123 
1124 	trace_kvm_nested_vmexit_inject(vmcb12->control.exit_code,
1125 				       vmcb12->control.exit_info_1,
1126 				       vmcb12->control.exit_info_2,
1127 				       vmcb12->control.exit_int_info,
1128 				       vmcb12->control.exit_int_info_err,
1129 				       KVM_ISA_SVM);
1130 
1131 	kvm_vcpu_unmap(vcpu, &map, true);
1132 
1133 	nested_svm_transition_tlb_flush(vcpu);
1134 
1135 	nested_svm_uninit_mmu_context(vcpu);
1136 
1137 	rc = nested_svm_load_cr3(vcpu, vmcb01->save.cr3, false, true);
1138 	if (rc)
1139 		return 1;
1140 
1141 	/*
1142 	 * Drop what we picked up for L2 via svm_complete_interrupts() so it
1143 	 * doesn't end up in L1.
1144 	 */
1145 	svm->vcpu.arch.nmi_injected = false;
1146 	kvm_clear_exception_queue(vcpu);
1147 	kvm_clear_interrupt_queue(vcpu);
1148 
1149 	/*
1150 	 * If we are here following the completion of a VMRUN that
1151 	 * is being single-stepped, queue the pending #DB intercept
1152 	 * right now so that it an be accounted for before we execute
1153 	 * L1's next instruction.
1154 	 */
1155 	if (unlikely(vmcb01->save.rflags & X86_EFLAGS_TF))
1156 		kvm_queue_exception(&(svm->vcpu), DB_VECTOR);
1157 
1158 	/*
1159 	 * Un-inhibit the AVIC right away, so that other vCPUs can start
1160 	 * to benefit from it right away.
1161 	 */
1162 	if (kvm_apicv_activated(vcpu->kvm))
1163 		__kvm_vcpu_update_apicv(vcpu);
1164 
1165 	return 0;
1166 }
1167 
nested_svm_triple_fault(struct kvm_vcpu * vcpu)1168 static void nested_svm_triple_fault(struct kvm_vcpu *vcpu)
1169 {
1170 	struct vcpu_svm *svm = to_svm(vcpu);
1171 
1172 	if (!vmcb12_is_intercept(&svm->nested.ctl, INTERCEPT_SHUTDOWN))
1173 		return;
1174 
1175 	kvm_clear_request(KVM_REQ_TRIPLE_FAULT, vcpu);
1176 	nested_svm_simple_vmexit(to_svm(vcpu), SVM_EXIT_SHUTDOWN);
1177 }
1178 
svm_allocate_nested(struct vcpu_svm * svm)1179 int svm_allocate_nested(struct vcpu_svm *svm)
1180 {
1181 	struct page *vmcb02_page;
1182 
1183 	if (svm->nested.initialized)
1184 		return 0;
1185 
1186 	vmcb02_page = alloc_page(GFP_KERNEL_ACCOUNT | __GFP_ZERO);
1187 	if (!vmcb02_page)
1188 		return -ENOMEM;
1189 	svm->nested.vmcb02.ptr = page_address(vmcb02_page);
1190 	svm->nested.vmcb02.pa = __sme_set(page_to_pfn(vmcb02_page) << PAGE_SHIFT);
1191 
1192 	svm->nested.msrpm = svm_vcpu_alloc_msrpm();
1193 	if (!svm->nested.msrpm)
1194 		goto err_free_vmcb02;
1195 	svm_vcpu_init_msrpm(&svm->vcpu, svm->nested.msrpm);
1196 
1197 	svm->nested.initialized = true;
1198 	return 0;
1199 
1200 err_free_vmcb02:
1201 	__free_page(vmcb02_page);
1202 	return -ENOMEM;
1203 }
1204 
svm_free_nested(struct vcpu_svm * svm)1205 void svm_free_nested(struct vcpu_svm *svm)
1206 {
1207 	if (!svm->nested.initialized)
1208 		return;
1209 
1210 	if (WARN_ON_ONCE(svm->vmcb != svm->vmcb01.ptr))
1211 		svm_switch_vmcb(svm, &svm->vmcb01);
1212 
1213 	svm_vcpu_free_msrpm(svm->nested.msrpm);
1214 	svm->nested.msrpm = NULL;
1215 
1216 	__free_page(virt_to_page(svm->nested.vmcb02.ptr));
1217 	svm->nested.vmcb02.ptr = NULL;
1218 
1219 	/*
1220 	 * When last_vmcb12_gpa matches the current vmcb12 gpa,
1221 	 * some vmcb12 fields are not loaded if they are marked clean
1222 	 * in the vmcb12, since in this case they are up to date already.
1223 	 *
1224 	 * When the vmcb02 is freed, this optimization becomes invalid.
1225 	 */
1226 	svm->nested.last_vmcb12_gpa = INVALID_GPA;
1227 
1228 	svm->nested.initialized = false;
1229 }
1230 
svm_leave_nested(struct kvm_vcpu * vcpu)1231 void svm_leave_nested(struct kvm_vcpu *vcpu)
1232 {
1233 	struct vcpu_svm *svm = to_svm(vcpu);
1234 
1235 	if (is_guest_mode(vcpu)) {
1236 		svm->nested.nested_run_pending = 0;
1237 		svm->nested.vmcb12_gpa = INVALID_GPA;
1238 
1239 		leave_guest_mode(vcpu);
1240 
1241 		svm_switch_vmcb(svm, &svm->vmcb01);
1242 
1243 		nested_svm_uninit_mmu_context(vcpu);
1244 		vmcb_mark_all_dirty(svm->vmcb);
1245 
1246 		if (kvm_apicv_activated(vcpu->kvm))
1247 			kvm_make_request(KVM_REQ_APICV_UPDATE, vcpu);
1248 	}
1249 
1250 	kvm_clear_request(KVM_REQ_GET_NESTED_STATE_PAGES, vcpu);
1251 }
1252 
nested_svm_exit_handled_msr(struct vcpu_svm * svm)1253 static int nested_svm_exit_handled_msr(struct vcpu_svm *svm)
1254 {
1255 	u32 offset, msr, value;
1256 	int write, mask;
1257 
1258 	if (!(vmcb12_is_intercept(&svm->nested.ctl, INTERCEPT_MSR_PROT)))
1259 		return NESTED_EXIT_HOST;
1260 
1261 	msr    = svm->vcpu.arch.regs[VCPU_REGS_RCX];
1262 	offset = svm_msrpm_offset(msr);
1263 	write  = svm->vmcb->control.exit_info_1 & 1;
1264 	mask   = 1 << ((2 * (msr & 0xf)) + write);
1265 
1266 	if (offset == MSR_INVALID)
1267 		return NESTED_EXIT_DONE;
1268 
1269 	/* Offset is in 32 bit units but need in 8 bit units */
1270 	offset *= 4;
1271 
1272 	if (kvm_vcpu_read_guest(&svm->vcpu, svm->nested.ctl.msrpm_base_pa + offset, &value, 4))
1273 		return NESTED_EXIT_DONE;
1274 
1275 	return (value & mask) ? NESTED_EXIT_DONE : NESTED_EXIT_HOST;
1276 }
1277 
nested_svm_intercept_ioio(struct vcpu_svm * svm)1278 static int nested_svm_intercept_ioio(struct vcpu_svm *svm)
1279 {
1280 	unsigned port, size, iopm_len;
1281 	u16 val, mask;
1282 	u8 start_bit;
1283 	u64 gpa;
1284 
1285 	if (!(vmcb12_is_intercept(&svm->nested.ctl, INTERCEPT_IOIO_PROT)))
1286 		return NESTED_EXIT_HOST;
1287 
1288 	port = svm->vmcb->control.exit_info_1 >> 16;
1289 	size = (svm->vmcb->control.exit_info_1 & SVM_IOIO_SIZE_MASK) >>
1290 		SVM_IOIO_SIZE_SHIFT;
1291 	gpa  = svm->nested.ctl.iopm_base_pa + (port / 8);
1292 	start_bit = port % 8;
1293 	iopm_len = (start_bit + size > 8) ? 2 : 1;
1294 	mask = (0xf >> (4 - size)) << start_bit;
1295 	val = 0;
1296 
1297 	if (kvm_vcpu_read_guest(&svm->vcpu, gpa, &val, iopm_len))
1298 		return NESTED_EXIT_DONE;
1299 
1300 	return (val & mask) ? NESTED_EXIT_DONE : NESTED_EXIT_HOST;
1301 }
1302 
nested_svm_intercept(struct vcpu_svm * svm)1303 static int nested_svm_intercept(struct vcpu_svm *svm)
1304 {
1305 	u32 exit_code = svm->vmcb->control.exit_code;
1306 	int vmexit = NESTED_EXIT_HOST;
1307 
1308 	switch (exit_code) {
1309 	case SVM_EXIT_MSR:
1310 		vmexit = nested_svm_exit_handled_msr(svm);
1311 		break;
1312 	case SVM_EXIT_IOIO:
1313 		vmexit = nested_svm_intercept_ioio(svm);
1314 		break;
1315 	case SVM_EXIT_READ_CR0 ... SVM_EXIT_WRITE_CR8: {
1316 		if (vmcb12_is_intercept(&svm->nested.ctl, exit_code))
1317 			vmexit = NESTED_EXIT_DONE;
1318 		break;
1319 	}
1320 	case SVM_EXIT_READ_DR0 ... SVM_EXIT_WRITE_DR7: {
1321 		if (vmcb12_is_intercept(&svm->nested.ctl, exit_code))
1322 			vmexit = NESTED_EXIT_DONE;
1323 		break;
1324 	}
1325 	case SVM_EXIT_EXCP_BASE ... SVM_EXIT_EXCP_BASE + 0x1f: {
1326 		/*
1327 		 * Host-intercepted exceptions have been checked already in
1328 		 * nested_svm_exit_special.  There is nothing to do here,
1329 		 * the vmexit is injected by svm_check_nested_events.
1330 		 */
1331 		vmexit = NESTED_EXIT_DONE;
1332 		break;
1333 	}
1334 	case SVM_EXIT_ERR: {
1335 		vmexit = NESTED_EXIT_DONE;
1336 		break;
1337 	}
1338 	default: {
1339 		if (vmcb12_is_intercept(&svm->nested.ctl, exit_code))
1340 			vmexit = NESTED_EXIT_DONE;
1341 	}
1342 	}
1343 
1344 	return vmexit;
1345 }
1346 
nested_svm_exit_handled(struct vcpu_svm * svm)1347 int nested_svm_exit_handled(struct vcpu_svm *svm)
1348 {
1349 	int vmexit;
1350 
1351 	vmexit = nested_svm_intercept(svm);
1352 
1353 	if (vmexit == NESTED_EXIT_DONE)
1354 		nested_svm_vmexit(svm);
1355 
1356 	return vmexit;
1357 }
1358 
nested_svm_check_permissions(struct kvm_vcpu * vcpu)1359 int nested_svm_check_permissions(struct kvm_vcpu *vcpu)
1360 {
1361 	if (!(vcpu->arch.efer & EFER_SVME) || !is_paging(vcpu)) {
1362 		kvm_queue_exception(vcpu, UD_VECTOR);
1363 		return 1;
1364 	}
1365 
1366 	if (to_svm(vcpu)->vmcb->save.cpl) {
1367 		kvm_inject_gp(vcpu, 0);
1368 		return 1;
1369 	}
1370 
1371 	return 0;
1372 }
1373 
nested_svm_is_exception_vmexit(struct kvm_vcpu * vcpu,u8 vector,u32 error_code)1374 static bool nested_svm_is_exception_vmexit(struct kvm_vcpu *vcpu, u8 vector,
1375 					   u32 error_code)
1376 {
1377 	struct vcpu_svm *svm = to_svm(vcpu);
1378 
1379 	return (svm->nested.ctl.intercepts[INTERCEPT_EXCEPTION] & BIT(vector));
1380 }
1381 
nested_svm_inject_exception_vmexit(struct kvm_vcpu * vcpu)1382 static void nested_svm_inject_exception_vmexit(struct kvm_vcpu *vcpu)
1383 {
1384 	struct kvm_queued_exception *ex = &vcpu->arch.exception_vmexit;
1385 	struct vcpu_svm *svm = to_svm(vcpu);
1386 	struct vmcb *vmcb = svm->vmcb;
1387 
1388 	vmcb->control.exit_code = SVM_EXIT_EXCP_BASE + ex->vector;
1389 	vmcb->control.exit_code_hi = 0;
1390 
1391 	if (ex->has_error_code)
1392 		vmcb->control.exit_info_1 = ex->error_code;
1393 
1394 	/*
1395 	 * EXITINFO2 is undefined for all exception intercepts other
1396 	 * than #PF.
1397 	 */
1398 	if (ex->vector == PF_VECTOR) {
1399 		if (ex->has_payload)
1400 			vmcb->control.exit_info_2 = ex->payload;
1401 		else
1402 			vmcb->control.exit_info_2 = vcpu->arch.cr2;
1403 	} else if (ex->vector == DB_VECTOR) {
1404 		/* See kvm_check_and_inject_events().  */
1405 		kvm_deliver_exception_payload(vcpu, ex);
1406 
1407 		if (vcpu->arch.dr7 & DR7_GD) {
1408 			vcpu->arch.dr7 &= ~DR7_GD;
1409 			kvm_update_dr7(vcpu);
1410 		}
1411 	} else {
1412 		WARN_ON(ex->has_payload);
1413 	}
1414 
1415 	nested_svm_vmexit(svm);
1416 }
1417 
nested_exit_on_init(struct vcpu_svm * svm)1418 static inline bool nested_exit_on_init(struct vcpu_svm *svm)
1419 {
1420 	return vmcb12_is_intercept(&svm->nested.ctl, INTERCEPT_INIT);
1421 }
1422 
svm_check_nested_events(struct kvm_vcpu * vcpu)1423 static int svm_check_nested_events(struct kvm_vcpu *vcpu)
1424 {
1425 	struct kvm_lapic *apic = vcpu->arch.apic;
1426 	struct vcpu_svm *svm = to_svm(vcpu);
1427 	/*
1428 	 * Only a pending nested run blocks a pending exception.  If there is a
1429 	 * previously injected event, the pending exception occurred while said
1430 	 * event was being delivered and thus needs to be handled.
1431 	 */
1432 	bool block_nested_exceptions = svm->nested.nested_run_pending;
1433 	/*
1434 	 * New events (not exceptions) are only recognized at instruction
1435 	 * boundaries.  If an event needs reinjection, then KVM is handling a
1436 	 * VM-Exit that occurred _during_ instruction execution; new events are
1437 	 * blocked until the instruction completes.
1438 	 */
1439 	bool block_nested_events = block_nested_exceptions ||
1440 				   kvm_event_needs_reinjection(vcpu);
1441 
1442 	if (lapic_in_kernel(vcpu) &&
1443 	    test_bit(KVM_APIC_INIT, &apic->pending_events)) {
1444 		if (block_nested_events)
1445 			return -EBUSY;
1446 		if (!nested_exit_on_init(svm))
1447 			return 0;
1448 		nested_svm_simple_vmexit(svm, SVM_EXIT_INIT);
1449 		return 0;
1450 	}
1451 
1452 	if (vcpu->arch.exception_vmexit.pending) {
1453 		if (block_nested_exceptions)
1454                         return -EBUSY;
1455 		nested_svm_inject_exception_vmexit(vcpu);
1456 		return 0;
1457 	}
1458 
1459 	if (vcpu->arch.exception.pending) {
1460 		if (block_nested_exceptions)
1461 			return -EBUSY;
1462 		return 0;
1463 	}
1464 
1465 #ifdef CONFIG_KVM_SMM
1466 	if (vcpu->arch.smi_pending && !svm_smi_blocked(vcpu)) {
1467 		if (block_nested_events)
1468 			return -EBUSY;
1469 		if (!nested_exit_on_smi(svm))
1470 			return 0;
1471 		nested_svm_simple_vmexit(svm, SVM_EXIT_SMI);
1472 		return 0;
1473 	}
1474 #endif
1475 
1476 	if (vcpu->arch.nmi_pending && !svm_nmi_blocked(vcpu)) {
1477 		if (block_nested_events)
1478 			return -EBUSY;
1479 		if (!nested_exit_on_nmi(svm))
1480 			return 0;
1481 		nested_svm_simple_vmexit(svm, SVM_EXIT_NMI);
1482 		return 0;
1483 	}
1484 
1485 	if (kvm_cpu_has_interrupt(vcpu) && !svm_interrupt_blocked(vcpu)) {
1486 		if (block_nested_events)
1487 			return -EBUSY;
1488 		if (!nested_exit_on_intr(svm))
1489 			return 0;
1490 		trace_kvm_nested_intr_vmexit(svm->vmcb->save.rip);
1491 		nested_svm_simple_vmexit(svm, SVM_EXIT_INTR);
1492 		return 0;
1493 	}
1494 
1495 	return 0;
1496 }
1497 
nested_svm_exit_special(struct vcpu_svm * svm)1498 int nested_svm_exit_special(struct vcpu_svm *svm)
1499 {
1500 	u32 exit_code = svm->vmcb->control.exit_code;
1501 	struct kvm_vcpu *vcpu = &svm->vcpu;
1502 
1503 	switch (exit_code) {
1504 	case SVM_EXIT_INTR:
1505 	case SVM_EXIT_NMI:
1506 	case SVM_EXIT_NPF:
1507 		return NESTED_EXIT_HOST;
1508 	case SVM_EXIT_EXCP_BASE ... SVM_EXIT_EXCP_BASE + 0x1f: {
1509 		u32 excp_bits = 1 << (exit_code - SVM_EXIT_EXCP_BASE);
1510 
1511 		if (svm->vmcb01.ptr->control.intercepts[INTERCEPT_EXCEPTION] &
1512 		    excp_bits)
1513 			return NESTED_EXIT_HOST;
1514 		else if (exit_code == SVM_EXIT_EXCP_BASE + PF_VECTOR &&
1515 			 svm->vcpu.arch.apf.host_apf_flags)
1516 			/* Trap async PF even if not shadowing */
1517 			return NESTED_EXIT_HOST;
1518 		break;
1519 	}
1520 	case SVM_EXIT_VMMCALL:
1521 		/* Hyper-V L2 TLB flush hypercall is handled by L0 */
1522 		if (guest_hv_cpuid_has_l2_tlb_flush(vcpu) &&
1523 		    nested_svm_l2_tlb_flush_enabled(vcpu) &&
1524 		    kvm_hv_is_tlb_flush_hcall(vcpu))
1525 			return NESTED_EXIT_HOST;
1526 		break;
1527 	default:
1528 		break;
1529 	}
1530 
1531 	return NESTED_EXIT_CONTINUE;
1532 }
1533 
nested_svm_update_tsc_ratio_msr(struct kvm_vcpu * vcpu)1534 void nested_svm_update_tsc_ratio_msr(struct kvm_vcpu *vcpu)
1535 {
1536 	struct vcpu_svm *svm = to_svm(vcpu);
1537 
1538 	vcpu->arch.tsc_scaling_ratio =
1539 		kvm_calc_nested_tsc_multiplier(vcpu->arch.l1_tsc_scaling_ratio,
1540 					       svm->tsc_ratio_msr);
1541 	svm_write_tsc_multiplier(vcpu);
1542 }
1543 
1544 /* Inverse operation of nested_copy_vmcb_control_to_cache(). asid is copied too. */
nested_copy_vmcb_cache_to_control(struct vmcb_control_area * dst,struct vmcb_ctrl_area_cached * from)1545 static void nested_copy_vmcb_cache_to_control(struct vmcb_control_area *dst,
1546 					      struct vmcb_ctrl_area_cached *from)
1547 {
1548 	unsigned int i;
1549 
1550 	memset(dst, 0, sizeof(struct vmcb_control_area));
1551 
1552 	for (i = 0; i < MAX_INTERCEPT; i++)
1553 		dst->intercepts[i] = from->intercepts[i];
1554 
1555 	dst->iopm_base_pa         = from->iopm_base_pa;
1556 	dst->msrpm_base_pa        = from->msrpm_base_pa;
1557 	dst->tsc_offset           = from->tsc_offset;
1558 	dst->asid                 = from->asid;
1559 	dst->tlb_ctl              = from->tlb_ctl;
1560 	dst->int_ctl              = from->int_ctl;
1561 	dst->int_vector           = from->int_vector;
1562 	dst->int_state            = from->int_state;
1563 	dst->exit_code            = from->exit_code;
1564 	dst->exit_code_hi         = from->exit_code_hi;
1565 	dst->exit_info_1          = from->exit_info_1;
1566 	dst->exit_info_2          = from->exit_info_2;
1567 	dst->exit_int_info        = from->exit_int_info;
1568 	dst->exit_int_info_err    = from->exit_int_info_err;
1569 	dst->nested_ctl           = from->nested_ctl;
1570 	dst->event_inj            = from->event_inj;
1571 	dst->event_inj_err        = from->event_inj_err;
1572 	dst->next_rip             = from->next_rip;
1573 	dst->nested_cr3           = from->nested_cr3;
1574 	dst->virt_ext              = from->virt_ext;
1575 	dst->pause_filter_count   = from->pause_filter_count;
1576 	dst->pause_filter_thresh  = from->pause_filter_thresh;
1577 	/* 'clean' and 'hv_enlightenments' are not changed by KVM */
1578 }
1579 
svm_get_nested_state(struct kvm_vcpu * vcpu,struct kvm_nested_state __user * user_kvm_nested_state,u32 user_data_size)1580 static int svm_get_nested_state(struct kvm_vcpu *vcpu,
1581 				struct kvm_nested_state __user *user_kvm_nested_state,
1582 				u32 user_data_size)
1583 {
1584 	struct vcpu_svm *svm;
1585 	struct vmcb_control_area *ctl;
1586 	unsigned long r;
1587 	struct kvm_nested_state kvm_state = {
1588 		.flags = 0,
1589 		.format = KVM_STATE_NESTED_FORMAT_SVM,
1590 		.size = sizeof(kvm_state),
1591 	};
1592 	struct vmcb __user *user_vmcb = (struct vmcb __user *)
1593 		&user_kvm_nested_state->data.svm[0];
1594 
1595 	if (!vcpu)
1596 		return kvm_state.size + KVM_STATE_NESTED_SVM_VMCB_SIZE;
1597 
1598 	svm = to_svm(vcpu);
1599 
1600 	if (user_data_size < kvm_state.size)
1601 		goto out;
1602 
1603 	/* First fill in the header and copy it out.  */
1604 	if (is_guest_mode(vcpu)) {
1605 		kvm_state.hdr.svm.vmcb_pa = svm->nested.vmcb12_gpa;
1606 		kvm_state.size += KVM_STATE_NESTED_SVM_VMCB_SIZE;
1607 		kvm_state.flags |= KVM_STATE_NESTED_GUEST_MODE;
1608 
1609 		if (svm->nested.nested_run_pending)
1610 			kvm_state.flags |= KVM_STATE_NESTED_RUN_PENDING;
1611 	}
1612 
1613 	if (gif_set(svm))
1614 		kvm_state.flags |= KVM_STATE_NESTED_GIF_SET;
1615 
1616 	if (copy_to_user(user_kvm_nested_state, &kvm_state, sizeof(kvm_state)))
1617 		return -EFAULT;
1618 
1619 	if (!is_guest_mode(vcpu))
1620 		goto out;
1621 
1622 	/*
1623 	 * Copy over the full size of the VMCB rather than just the size
1624 	 * of the structs.
1625 	 */
1626 	if (clear_user(user_vmcb, KVM_STATE_NESTED_SVM_VMCB_SIZE))
1627 		return -EFAULT;
1628 
1629 	ctl = kzalloc(sizeof(*ctl), GFP_KERNEL);
1630 	if (!ctl)
1631 		return -ENOMEM;
1632 
1633 	nested_copy_vmcb_cache_to_control(ctl, &svm->nested.ctl);
1634 	r = copy_to_user(&user_vmcb->control, ctl,
1635 			 sizeof(user_vmcb->control));
1636 	kfree(ctl);
1637 	if (r)
1638 		return -EFAULT;
1639 
1640 	if (copy_to_user(&user_vmcb->save, &svm->vmcb01.ptr->save,
1641 			 sizeof(user_vmcb->save)))
1642 		return -EFAULT;
1643 out:
1644 	return kvm_state.size;
1645 }
1646 
svm_set_nested_state(struct kvm_vcpu * vcpu,struct kvm_nested_state __user * user_kvm_nested_state,struct kvm_nested_state * kvm_state)1647 static int svm_set_nested_state(struct kvm_vcpu *vcpu,
1648 				struct kvm_nested_state __user *user_kvm_nested_state,
1649 				struct kvm_nested_state *kvm_state)
1650 {
1651 	struct vcpu_svm *svm = to_svm(vcpu);
1652 	struct vmcb __user *user_vmcb = (struct vmcb __user *)
1653 		&user_kvm_nested_state->data.svm[0];
1654 	struct vmcb_control_area *ctl;
1655 	struct vmcb_save_area *save;
1656 	struct vmcb_save_area_cached save_cached;
1657 	struct vmcb_ctrl_area_cached ctl_cached;
1658 	unsigned long cr0;
1659 	int ret;
1660 
1661 	BUILD_BUG_ON(sizeof(struct vmcb_control_area) + sizeof(struct vmcb_save_area) >
1662 		     KVM_STATE_NESTED_SVM_VMCB_SIZE);
1663 
1664 	if (kvm_state->format != KVM_STATE_NESTED_FORMAT_SVM)
1665 		return -EINVAL;
1666 
1667 	if (kvm_state->flags & ~(KVM_STATE_NESTED_GUEST_MODE |
1668 				 KVM_STATE_NESTED_RUN_PENDING |
1669 				 KVM_STATE_NESTED_GIF_SET))
1670 		return -EINVAL;
1671 
1672 	/*
1673 	 * If in guest mode, vcpu->arch.efer actually refers to the L2 guest's
1674 	 * EFER.SVME, but EFER.SVME still has to be 1 for VMRUN to succeed.
1675 	 */
1676 	if (!(vcpu->arch.efer & EFER_SVME)) {
1677 		/* GIF=1 and no guest mode are required if SVME=0.  */
1678 		if (kvm_state->flags != KVM_STATE_NESTED_GIF_SET)
1679 			return -EINVAL;
1680 	}
1681 
1682 	/* SMM temporarily disables SVM, so we cannot be in guest mode.  */
1683 	if (is_smm(vcpu) && (kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE))
1684 		return -EINVAL;
1685 
1686 	if (!(kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE)) {
1687 		svm_leave_nested(vcpu);
1688 		svm_set_gif(svm, !!(kvm_state->flags & KVM_STATE_NESTED_GIF_SET));
1689 		return 0;
1690 	}
1691 
1692 	if (!page_address_valid(vcpu, kvm_state->hdr.svm.vmcb_pa))
1693 		return -EINVAL;
1694 	if (kvm_state->size < sizeof(*kvm_state) + KVM_STATE_NESTED_SVM_VMCB_SIZE)
1695 		return -EINVAL;
1696 
1697 	ret  = -ENOMEM;
1698 	ctl  = kzalloc(sizeof(*ctl),  GFP_KERNEL_ACCOUNT);
1699 	save = kzalloc(sizeof(*save), GFP_KERNEL_ACCOUNT);
1700 	if (!ctl || !save)
1701 		goto out_free;
1702 
1703 	ret = -EFAULT;
1704 	if (copy_from_user(ctl, &user_vmcb->control, sizeof(*ctl)))
1705 		goto out_free;
1706 	if (copy_from_user(save, &user_vmcb->save, sizeof(*save)))
1707 		goto out_free;
1708 
1709 	ret = -EINVAL;
1710 	__nested_copy_vmcb_control_to_cache(vcpu, &ctl_cached, ctl);
1711 	if (!__nested_vmcb_check_controls(vcpu, &ctl_cached))
1712 		goto out_free;
1713 
1714 	/*
1715 	 * Processor state contains L2 state.  Check that it is
1716 	 * valid for guest mode (see nested_vmcb_check_save).
1717 	 */
1718 	cr0 = kvm_read_cr0(vcpu);
1719         if (((cr0 & X86_CR0_CD) == 0) && (cr0 & X86_CR0_NW))
1720 		goto out_free;
1721 
1722 	/*
1723 	 * Validate host state saved from before VMRUN (see
1724 	 * nested_svm_check_permissions).
1725 	 */
1726 	__nested_copy_vmcb_save_to_cache(&save_cached, save);
1727 	if (!(save->cr0 & X86_CR0_PG) ||
1728 	    !(save->cr0 & X86_CR0_PE) ||
1729 	    (save->rflags & X86_EFLAGS_VM) ||
1730 	    !__nested_vmcb_check_save(vcpu, &save_cached))
1731 		goto out_free;
1732 
1733 
1734 	/*
1735 	 * All checks done, we can enter guest mode. Userspace provides
1736 	 * vmcb12.control, which will be combined with L1 and stored into
1737 	 * vmcb02, and the L1 save state which we store in vmcb01.
1738 	 * L2 registers if needed are moved from the current VMCB to VMCB02.
1739 	 */
1740 
1741 	if (is_guest_mode(vcpu))
1742 		svm_leave_nested(vcpu);
1743 	else
1744 		svm->nested.vmcb02.ptr->save = svm->vmcb01.ptr->save;
1745 
1746 	svm_set_gif(svm, !!(kvm_state->flags & KVM_STATE_NESTED_GIF_SET));
1747 
1748 	svm->nested.nested_run_pending =
1749 		!!(kvm_state->flags & KVM_STATE_NESTED_RUN_PENDING);
1750 
1751 	svm->nested.vmcb12_gpa = kvm_state->hdr.svm.vmcb_pa;
1752 
1753 	svm_copy_vmrun_state(&svm->vmcb01.ptr->save, save);
1754 	nested_copy_vmcb_control_to_cache(svm, ctl);
1755 
1756 	svm_switch_vmcb(svm, &svm->nested.vmcb02);
1757 	nested_vmcb02_prepare_control(svm, svm->vmcb->save.rip, svm->vmcb->save.cs.base);
1758 
1759 	/*
1760 	 * While the nested guest CR3 is already checked and set by
1761 	 * KVM_SET_SREGS, it was set when nested state was yet loaded,
1762 	 * thus MMU might not be initialized correctly.
1763 	 * Set it again to fix this.
1764 	 */
1765 
1766 	ret = nested_svm_load_cr3(&svm->vcpu, vcpu->arch.cr3,
1767 				  nested_npt_enabled(svm), false);
1768 	if (WARN_ON_ONCE(ret))
1769 		goto out_free;
1770 
1771 	svm->nested.force_msr_bitmap_recalc = true;
1772 
1773 	kvm_make_request(KVM_REQ_GET_NESTED_STATE_PAGES, vcpu);
1774 	ret = 0;
1775 out_free:
1776 	kfree(save);
1777 	kfree(ctl);
1778 
1779 	return ret;
1780 }
1781 
svm_get_nested_state_pages(struct kvm_vcpu * vcpu)1782 static bool svm_get_nested_state_pages(struct kvm_vcpu *vcpu)
1783 {
1784 	struct vcpu_svm *svm = to_svm(vcpu);
1785 
1786 	if (WARN_ON(!is_guest_mode(vcpu)))
1787 		return true;
1788 
1789 	if (!vcpu->arch.pdptrs_from_userspace &&
1790 	    !nested_npt_enabled(svm) && is_pae_paging(vcpu))
1791 		/*
1792 		 * Reload the guest's PDPTRs since after a migration
1793 		 * the guest CR3 might be restored prior to setting the nested
1794 		 * state which can lead to a load of wrong PDPTRs.
1795 		 */
1796 		if (CC(!load_pdptrs(vcpu, vcpu->arch.cr3)))
1797 			return false;
1798 
1799 	if (!nested_svm_vmrun_msrpm(svm)) {
1800 		vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
1801 		vcpu->run->internal.suberror =
1802 			KVM_INTERNAL_ERROR_EMULATION;
1803 		vcpu->run->internal.ndata = 0;
1804 		return false;
1805 	}
1806 
1807 	if (kvm_hv_verify_vp_assist(vcpu))
1808 		return false;
1809 
1810 	return true;
1811 }
1812 
1813 struct kvm_x86_nested_ops svm_nested_ops = {
1814 	.leave_nested = svm_leave_nested,
1815 	.is_exception_vmexit = nested_svm_is_exception_vmexit,
1816 	.check_events = svm_check_nested_events,
1817 	.triple_fault = nested_svm_triple_fault,
1818 	.get_nested_state_pages = svm_get_nested_state_pages,
1819 	.get_state = svm_get_nested_state,
1820 	.set_state = svm_set_nested_state,
1821 	.hv_inject_synthetic_vmexit_post_tlb_flush = svm_hv_inject_synthetic_vmexit_post_tlb_flush,
1822 };
1823