blob: 1c2a0414a88d12495cb889f028725c4020c62480 [file] [log] [blame]
Joerg Roedel883b0a92020-03-24 10:41:52 +01001// 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>
Paolo Bonzini5679b802020-05-04 11:28:25 -040022#include <asm/debugreg.h>
Joerg Roedel883b0a92020-03-24 10:41:52 +010023
24#include "kvm_emulate.h"
25#include "trace.h"
26#include "mmu.h"
27#include "x86.h"
Paolo Bonzinicc440cd2020-05-13 13:36:32 -040028#include "cpuid.h"
Paolo Bonzini5b6724082020-05-16 08:50:35 -040029#include "lapic.h"
Joerg Roedel883b0a92020-03-24 10:41:52 +010030#include "svm.h"
31
Sean Christopherson11f0cbf2021-02-03 16:01:17 -080032#define CC KVM_NESTED_VMENTER_CONSISTENCY_CHECK
33
Joerg Roedel883b0a92020-03-24 10:41:52 +010034static void nested_svm_inject_npf_exit(struct kvm_vcpu *vcpu,
35 struct x86_exception *fault)
36{
37 struct vcpu_svm *svm = to_svm(vcpu);
38
39 if (svm->vmcb->control.exit_code != SVM_EXIT_NPF) {
40 /*
41 * TODO: track the cause of the nested page fault, and
42 * correctly fill in the high bits of exit_info_1.
43 */
44 svm->vmcb->control.exit_code = SVM_EXIT_NPF;
45 svm->vmcb->control.exit_code_hi = 0;
46 svm->vmcb->control.exit_info_1 = (1ULL << 32);
47 svm->vmcb->control.exit_info_2 = fault->address;
48 }
49
50 svm->vmcb->control.exit_info_1 &= ~0xffffffffULL;
51 svm->vmcb->control.exit_info_1 |= fault->error_code;
52
Joerg Roedel883b0a92020-03-24 10:41:52 +010053 nested_svm_vmexit(svm);
54}
55
Paolo Bonzinia04aead2021-02-18 07:16:59 -050056static void svm_inject_page_fault_nested(struct kvm_vcpu *vcpu, struct x86_exception *fault)
57{
58 struct vcpu_svm *svm = to_svm(vcpu);
59 WARN_ON(!is_guest_mode(vcpu));
60
61 if (vmcb_is_intercept(&svm->nested.ctl, INTERCEPT_EXCEPTION_OFFSET + PF_VECTOR) &&
62 !svm->nested.nested_run_pending) {
63 svm->vmcb->control.exit_code = SVM_EXIT_EXCP_BASE + PF_VECTOR;
64 svm->vmcb->control.exit_code_hi = 0;
65 svm->vmcb->control.exit_info_1 = fault->error_code;
66 svm->vmcb->control.exit_info_2 = fault->address;
67 nested_svm_vmexit(svm);
68 } else {
69 kvm_inject_page_fault(vcpu, fault);
70 }
71}
72
Joerg Roedel883b0a92020-03-24 10:41:52 +010073static u64 nested_svm_get_tdp_pdptr(struct kvm_vcpu *vcpu, int index)
74{
75 struct vcpu_svm *svm = to_svm(vcpu);
Paolo Bonzinie670bf62020-05-13 13:16:12 -040076 u64 cr3 = svm->nested.ctl.nested_cr3;
Joerg Roedel883b0a92020-03-24 10:41:52 +010077 u64 pdpte;
78 int ret;
79
Sean Christopherson2732be92021-02-03 16:01:07 -080080 ret = kvm_vcpu_read_guest_page(vcpu, gpa_to_gfn(cr3), &pdpte,
Joerg Roedel883b0a92020-03-24 10:41:52 +010081 offset_in_page(cr3) + index * 8, 8);
82 if (ret)
83 return 0;
84 return pdpte;
85}
86
87static unsigned long nested_svm_get_tdp_cr3(struct kvm_vcpu *vcpu)
88{
89 struct vcpu_svm *svm = to_svm(vcpu);
90
Paolo Bonzinie670bf62020-05-13 13:16:12 -040091 return svm->nested.ctl.nested_cr3;
Joerg Roedel883b0a92020-03-24 10:41:52 +010092}
93
94static void nested_svm_init_mmu_context(struct kvm_vcpu *vcpu)
95{
Paolo Bonzini929d1cf2020-05-19 06:18:31 -040096 struct vcpu_svm *svm = to_svm(vcpu);
Paolo Bonzini929d1cf2020-05-19 06:18:31 -040097
Joerg Roedel883b0a92020-03-24 10:41:52 +010098 WARN_ON(mmu_is_nested(vcpu));
99
100 vcpu->arch.mmu = &vcpu->arch.guest_mmu;
Sean Christopherson31e96bc2021-06-22 10:57:00 -0700101
102 /*
103 * The NPT format depends on L1's CR4 and EFER, which is in vmcb01. Note,
104 * when called via KVM_SET_NESTED_STATE, that state may _not_ match current
105 * vCPU state. CR0.WP is explicitly ignored, while CR0.PG is required.
106 */
Cathy Avery4995a362021-01-13 07:07:52 -0500107 kvm_init_shadow_npt_mmu(vcpu, X86_CR0_PG, svm->vmcb01.ptr->save.cr4,
108 svm->vmcb01.ptr->save.efer,
Vitaly Kuznetsov0f04a2a2020-07-10 16:11:49 +0200109 svm->nested.ctl.nested_cr3);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100110 vcpu->arch.mmu->get_guest_pgd = nested_svm_get_tdp_cr3;
111 vcpu->arch.mmu->get_pdptr = nested_svm_get_tdp_pdptr;
112 vcpu->arch.mmu->inject_page_fault = nested_svm_inject_npf_exit;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100113 vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu;
114}
115
116static void nested_svm_uninit_mmu_context(struct kvm_vcpu *vcpu)
117{
118 vcpu->arch.mmu = &vcpu->arch.root_mmu;
119 vcpu->arch.walk_mmu = &vcpu->arch.root_mmu;
120}
121
122void recalc_intercepts(struct vcpu_svm *svm)
123{
Paolo Bonzinie670bf62020-05-13 13:16:12 -0400124 struct vmcb_control_area *c, *h, *g;
Babu Mogerc45ad722020-09-11 14:27:58 -0500125 unsigned int i;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100126
Joerg Roedel06e78522020-06-25 10:03:23 +0200127 vmcb_mark_dirty(svm->vmcb, VMCB_INTERCEPTS);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100128
129 if (!is_guest_mode(&svm->vcpu))
130 return;
131
132 c = &svm->vmcb->control;
Cathy Avery4995a362021-01-13 07:07:52 -0500133 h = &svm->vmcb01.ptr->control;
Paolo Bonzinie670bf62020-05-13 13:16:12 -0400134 g = &svm->nested.ctl;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100135
Babu Mogerc45ad722020-09-11 14:27:58 -0500136 for (i = 0; i < MAX_INTERCEPT; i++)
137 c->intercepts[i] = h->intercepts[i];
138
Paolo Bonzinie9fd7612020-05-13 13:28:23 -0400139 if (g->int_ctl & V_INTR_MASKING_MASK) {
Joerg Roedel883b0a92020-03-24 10:41:52 +0100140 /* We only want the cr8 intercept bits of L1 */
Babu Moger03bfeeb2020-09-11 14:28:05 -0500141 vmcb_clr_intercept(c, INTERCEPT_CR8_READ);
142 vmcb_clr_intercept(c, INTERCEPT_CR8_WRITE);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100143
144 /*
145 * Once running L2 with HF_VINTR_MASK, EFLAGS.IF does not
146 * affect any interrupt we may want to inject; therefore,
147 * interrupt window vmexits are irrelevant to L0.
148 */
Babu Mogerc62e2e92020-09-11 14:28:28 -0500149 vmcb_clr_intercept(c, INTERCEPT_VINTR);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100150 }
151
152 /* We don't want to see VMMCALLs from a nested guest */
Babu Mogerc62e2e92020-09-11 14:28:28 -0500153 vmcb_clr_intercept(c, INTERCEPT_VMMCALL);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100154
Babu Mogerc45ad722020-09-11 14:27:58 -0500155 for (i = 0; i < MAX_INTERCEPT; i++)
156 c->intercepts[i] |= g->intercepts[i];
Maxim Levitsky4b639a92021-07-07 15:51:00 +0300157
158 /* If SMI is not intercepted, ignore guest SMI intercept as well */
159 if (!intercept_smi)
160 vmcb_clr_intercept(c, INTERCEPT_SMI);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100161}
162
Paolo Bonzini2f675912020-05-18 15:21:22 -0400163static void copy_vmcb_control_area(struct vmcb_control_area *dst,
164 struct vmcb_control_area *from)
Joerg Roedel883b0a92020-03-24 10:41:52 +0100165{
Babu Mogerc45ad722020-09-11 14:27:58 -0500166 unsigned int i;
167
168 for (i = 0; i < MAX_INTERCEPT; i++)
169 dst->intercepts[i] = from->intercepts[i];
170
Joerg Roedel883b0a92020-03-24 10:41:52 +0100171 dst->iopm_base_pa = from->iopm_base_pa;
172 dst->msrpm_base_pa = from->msrpm_base_pa;
173 dst->tsc_offset = from->tsc_offset;
Paolo Bonzini6c0238c2020-05-20 08:02:17 -0400174 /* asid not copied, it is handled manually for svm->vmcb. */
Joerg Roedel883b0a92020-03-24 10:41:52 +0100175 dst->tlb_ctl = from->tlb_ctl;
176 dst->int_ctl = from->int_ctl;
177 dst->int_vector = from->int_vector;
178 dst->int_state = from->int_state;
179 dst->exit_code = from->exit_code;
180 dst->exit_code_hi = from->exit_code_hi;
181 dst->exit_info_1 = from->exit_info_1;
182 dst->exit_info_2 = from->exit_info_2;
183 dst->exit_int_info = from->exit_int_info;
184 dst->exit_int_info_err = from->exit_int_info_err;
185 dst->nested_ctl = from->nested_ctl;
186 dst->event_inj = from->event_inj;
187 dst->event_inj_err = from->event_inj_err;
188 dst->nested_cr3 = from->nested_cr3;
189 dst->virt_ext = from->virt_ext;
190 dst->pause_filter_count = from->pause_filter_count;
191 dst->pause_filter_thresh = from->pause_filter_thresh;
192}
193
194static bool nested_svm_vmrun_msrpm(struct vcpu_svm *svm)
195{
196 /*
197 * This function merges the msr permission bitmaps of kvm and the
198 * nested vmcb. It is optimized in that it only merges the parts where
199 * the kvm msr permission bitmap may contain zero bits
200 */
201 int i;
202
Babu Mogerc62e2e92020-09-11 14:28:28 -0500203 if (!(vmcb_is_intercept(&svm->nested.ctl, INTERCEPT_MSR_PROT)))
Joerg Roedel883b0a92020-03-24 10:41:52 +0100204 return true;
205
206 for (i = 0; i < MSRPM_OFFSETS; i++) {
207 u32 value, p;
208 u64 offset;
209
210 if (msrpm_offsets[i] == 0xffffffff)
211 break;
212
213 p = msrpm_offsets[i];
Paolo Bonzinie670bf62020-05-13 13:16:12 -0400214 offset = svm->nested.ctl.msrpm_base_pa + (p * 4);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100215
216 if (kvm_vcpu_read_guest(&svm->vcpu, offset, &value, 4))
217 return false;
218
219 svm->nested.msrpm[p] = svm->msrpm[p] | value;
220 }
221
222 svm->vmcb->control.msrpm_base_pa = __sme_set(__pa(svm->nested.msrpm));
223
224 return true;
225}
226
Krish Sadhukhanee695f22021-04-12 17:56:08 -0400227/*
228 * Bits 11:0 of bitmap address are ignored by hardware
229 */
230static bool nested_svm_check_bitmap_pa(struct kvm_vcpu *vcpu, u64 pa, u32 size)
231{
232 u64 addr = PAGE_ALIGN(pa);
233
234 return kvm_vcpu_is_legal_gpa(vcpu, addr) &&
235 kvm_vcpu_is_legal_gpa(vcpu, addr + size - 1);
236}
237
238static bool nested_vmcb_check_controls(struct kvm_vcpu *vcpu,
239 struct vmcb_control_area *control)
Paolo Bonzinica46d732020-05-18 13:02:15 -0400240{
Sean Christopherson11f0cbf2021-02-03 16:01:17 -0800241 if (CC(!vmcb_is_intercept(control, INTERCEPT_VMRUN)))
Paolo Bonzinica46d732020-05-18 13:02:15 -0400242 return false;
243
Sean Christopherson11f0cbf2021-02-03 16:01:17 -0800244 if (CC(control->asid == 0))
Paolo Bonzinica46d732020-05-18 13:02:15 -0400245 return false;
246
Sean Christopherson11f0cbf2021-02-03 16:01:17 -0800247 if (CC((control->nested_ctl & SVM_NESTED_CTL_NP_ENABLE) && !npt_enabled))
Paolo Bonzinica46d732020-05-18 13:02:15 -0400248 return false;
249
Krish Sadhukhanee695f22021-04-12 17:56:08 -0400250 if (CC(!nested_svm_check_bitmap_pa(vcpu, control->msrpm_base_pa,
251 MSRPM_SIZE)))
252 return false;
253 if (CC(!nested_svm_check_bitmap_pa(vcpu, control->iopm_base_pa,
254 IOPM_SIZE)))
255 return false;
256
Paolo Bonzinica46d732020-05-18 13:02:15 -0400257 return true;
258}
259
Paolo Bonzini63129752021-03-02 14:40:39 -0500260static bool nested_vmcb_check_cr3_cr4(struct kvm_vcpu *vcpu,
Krish Sadhukhan6906e062020-10-06 19:06:52 +0000261 struct vmcb_save_area *save)
Joerg Roedel883b0a92020-03-24 10:41:52 +0100262{
Krish Sadhukhan6906e062020-10-06 19:06:52 +0000263 /*
264 * These checks are also performed by KVM_SET_SREGS,
265 * except that EFER.LMA is not checked by SVM against
266 * CR0.PG && EFER.LME.
267 */
268 if ((save->efer & EFER_LME) && (save->cr0 & X86_CR0_PG)) {
Sean Christopherson11f0cbf2021-02-03 16:01:17 -0800269 if (CC(!(save->cr4 & X86_CR4_PAE)) ||
270 CC(!(save->cr0 & X86_CR0_PE)) ||
271 CC(kvm_vcpu_is_illegal_gpa(vcpu, save->cr3)))
Krish Sadhukhan761e4162020-07-08 00:39:56 +0000272 return false;
273 }
Krish Sadhukhan6906e062020-10-06 19:06:52 +0000274
Sean Christopherson11f0cbf2021-02-03 16:01:17 -0800275 if (CC(!kvm_is_valid_cr4(vcpu, save->cr4)))
276 return false;
277
278 return true;
Krish Sadhukhan6906e062020-10-06 19:06:52 +0000279}
280
281/* Common checks that apply to both L1 and L2 state. */
Paolo Bonzini63129752021-03-02 14:40:39 -0500282static bool nested_vmcb_valid_sregs(struct kvm_vcpu *vcpu,
Krish Sadhukhan6906e062020-10-06 19:06:52 +0000283 struct vmcb_save_area *save)
284{
Paolo Bonzini3c346c02021-03-31 06:28:01 -0400285 /*
286 * FIXME: these should be done after copying the fields,
287 * to avoid TOC/TOU races. For these save area checks
288 * the possible damage is limited since kvm_set_cr0 and
289 * kvm_set_cr4 handle failure; EFER_SVME is an exception
290 * so it is force-set later in nested_prepare_vmcb_save.
291 */
Sean Christopherson11f0cbf2021-02-03 16:01:17 -0800292 if (CC(!(save->efer & EFER_SVME)))
Krish Sadhukhan6906e062020-10-06 19:06:52 +0000293 return false;
294
Sean Christopherson11f0cbf2021-02-03 16:01:17 -0800295 if (CC((save->cr0 & X86_CR0_CD) == 0 && (save->cr0 & X86_CR0_NW)) ||
296 CC(save->cr0 & ~0xffffffffULL))
Krish Sadhukhan6906e062020-10-06 19:06:52 +0000297 return false;
298
Sean Christopherson11f0cbf2021-02-03 16:01:17 -0800299 if (CC(!kvm_dr6_valid(save->dr6)) || CC(!kvm_dr7_valid(save->dr7)))
Krish Sadhukhan6906e062020-10-06 19:06:52 +0000300 return false;
301
Paolo Bonzini63129752021-03-02 14:40:39 -0500302 if (!nested_vmcb_check_cr3_cr4(vcpu, save))
Krish Sadhukhan6906e062020-10-06 19:06:52 +0000303 return false;
304
Paolo Bonzini63129752021-03-02 14:40:39 -0500305 if (CC(!kvm_valid_efer(vcpu, save->efer)))
Krish Sadhukhan6906e062020-10-06 19:06:52 +0000306 return false;
307
308 return true;
309}
310
Vitaly Kuznetsovbb00bd92021-06-28 12:44:24 +0200311void nested_load_control_from_vmcb12(struct vcpu_svm *svm,
312 struct vmcb_control_area *control)
Paolo Bonzini3e06f012020-05-13 13:07:26 -0400313{
Paolo Bonzinie670bf62020-05-13 13:16:12 -0400314 copy_vmcb_control_area(&svm->nested.ctl, control);
Paolo Bonzini3e06f012020-05-13 13:07:26 -0400315
Paolo Bonzinicc440cd2020-05-13 13:36:32 -0400316 /* Copy it here because nested_svm_check_controls will check it. */
317 svm->nested.ctl.asid = control->asid;
Paolo Bonzinie670bf62020-05-13 13:16:12 -0400318 svm->nested.ctl.msrpm_base_pa &= ~0x0fffULL;
319 svm->nested.ctl.iopm_base_pa &= ~0x0fffULL;
Paolo Bonzini3e06f012020-05-13 13:07:26 -0400320}
321
Paolo Bonzini2d8a42b2020-05-22 03:50:14 -0400322/*
323 * Synchronize fields that are written by the processor, so that
Paolo Bonzini9e8f0fb2020-11-17 05:15:41 -0500324 * they can be copied back into the vmcb12.
Paolo Bonzini2d8a42b2020-05-22 03:50:14 -0400325 */
Paolo Bonzini9e8f0fb2020-11-17 05:15:41 -0500326void nested_sync_control_from_vmcb02(struct vcpu_svm *svm)
Paolo Bonzini2d8a42b2020-05-22 03:50:14 -0400327{
328 u32 mask;
329 svm->nested.ctl.event_inj = svm->vmcb->control.event_inj;
330 svm->nested.ctl.event_inj_err = svm->vmcb->control.event_inj_err;
331
332 /* Only a few fields of int_ctl are written by the processor. */
333 mask = V_IRQ_MASK | V_TPR_MASK;
334 if (!(svm->nested.ctl.int_ctl & V_INTR_MASKING_MASK) &&
Joerg Roedela284ba52020-06-25 10:03:24 +0200335 svm_is_intercept(svm, INTERCEPT_VINTR)) {
Paolo Bonzini2d8a42b2020-05-22 03:50:14 -0400336 /*
337 * In order to request an interrupt window, L0 is usurping
338 * svm->vmcb->control.int_ctl and possibly setting V_IRQ
339 * even if it was clear in L1's VMCB. Restoring it would be
340 * wrong. However, in this case V_IRQ will remain true until
341 * interrupt_window_interception calls svm_clear_vintr and
342 * restores int_ctl. We can just leave it aside.
343 */
344 mask &= ~V_IRQ_MASK;
345 }
346 svm->nested.ctl.int_ctl &= ~mask;
347 svm->nested.ctl.int_ctl |= svm->vmcb->control.int_ctl & mask;
348}
349
Paolo Bonzini36e2e982020-05-22 06:04:57 -0400350/*
351 * Transfer any event that L0 or L1 wanted to inject into L2 to
352 * EXIT_INT_INFO.
353 */
Paolo Bonzini9e8f0fb2020-11-17 05:15:41 -0500354static void nested_save_pending_event_to_vmcb12(struct vcpu_svm *svm,
355 struct vmcb *vmcb12)
Paolo Bonzini36e2e982020-05-22 06:04:57 -0400356{
357 struct kvm_vcpu *vcpu = &svm->vcpu;
358 u32 exit_int_info = 0;
359 unsigned int nr;
360
361 if (vcpu->arch.exception.injected) {
362 nr = vcpu->arch.exception.nr;
363 exit_int_info = nr | SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_EXEPT;
364
365 if (vcpu->arch.exception.has_error_code) {
366 exit_int_info |= SVM_EVTINJ_VALID_ERR;
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300367 vmcb12->control.exit_int_info_err =
Paolo Bonzini36e2e982020-05-22 06:04:57 -0400368 vcpu->arch.exception.error_code;
369 }
370
371 } else if (vcpu->arch.nmi_injected) {
372 exit_int_info = SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_NMI;
373
374 } else if (vcpu->arch.interrupt.injected) {
375 nr = vcpu->arch.interrupt.nr;
376 exit_int_info = nr | SVM_EVTINJ_VALID;
377
378 if (vcpu->arch.interrupt.soft)
379 exit_int_info |= SVM_EVTINJ_TYPE_SOFT;
380 else
381 exit_int_info |= SVM_EVTINJ_TYPE_INTR;
382 }
383
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300384 vmcb12->control.exit_int_info = exit_int_info;
Paolo Bonzini36e2e982020-05-22 06:04:57 -0400385}
386
Vitaly Kuznetsov62156f62020-07-10 16:11:53 +0200387static inline bool nested_npt_enabled(struct vcpu_svm *svm)
388{
389 return svm->nested.ctl.nested_ctl & SVM_NESTED_CTL_NP_ENABLE;
390}
391
Sean Christophersond2e56012021-06-09 16:42:26 -0700392static void nested_svm_transition_tlb_flush(struct kvm_vcpu *vcpu)
393{
394 /*
395 * TODO: optimize unconditional TLB flush/MMU sync. A partial list of
396 * things to fix before this can be conditional:
397 *
398 * - Flush TLBs for both L1 and L2 remote TLB flush
399 * - Honor L1's request to flush an ASID on nested VMRUN
400 * - Sync nested NPT MMU on VMRUN that flushes L2's ASID[*]
401 * - Don't crush a pending TLB flush in vmcb02 on nested VMRUN
402 * - Flush L1's ASID on KVM_REQ_TLB_FLUSH_GUEST
403 *
404 * [*] Unlike nested EPT, SVM's ASID management can invalidate nested
405 * NPT guest-physical mappings on VMRUN.
406 */
407 kvm_make_request(KVM_REQ_MMU_SYNC, vcpu);
408 kvm_make_request(KVM_REQ_TLB_FLUSH_CURRENT, vcpu);
409}
410
Vitaly Kuznetsov62156f62020-07-10 16:11:53 +0200411/*
Vitaly Kuznetsovd82aaef2020-07-10 16:11:56 +0200412 * Load guest's/host's cr3 on nested vmentry or vmexit. @nested_npt is true
413 * if we are emulating VM-Entry into a guest with NPT enabled.
Vitaly Kuznetsov62156f62020-07-10 16:11:53 +0200414 */
415static int nested_svm_load_cr3(struct kvm_vcpu *vcpu, unsigned long cr3,
Maxim Levitskyb222b0b2021-06-07 12:01:59 +0300416 bool nested_npt, bool reload_pdptrs)
Vitaly Kuznetsov62156f62020-07-10 16:11:53 +0200417{
Sean Christopherson11f0cbf2021-02-03 16:01:17 -0800418 if (CC(kvm_vcpu_is_illegal_gpa(vcpu, cr3)))
Vitaly Kuznetsova506fdd2020-07-10 16:11:55 +0200419 return -EINVAL;
420
Maxim Levitskyb222b0b2021-06-07 12:01:59 +0300421 if (reload_pdptrs && !nested_npt && is_pae_paging(vcpu) &&
Sean Christophersona36dbec62021-06-07 12:01:57 +0300422 CC(!load_pdptrs(vcpu, vcpu->arch.walk_mmu, cr3)))
423 return -EINVAL;
Vitaly Kuznetsova506fdd2020-07-10 16:11:55 +0200424
Vitaly Kuznetsova506fdd2020-07-10 16:11:55 +0200425 if (!nested_npt)
Sean Christophersonb5129102021-06-09 16:42:27 -0700426 kvm_mmu_new_pgd(vcpu, cr3);
Vitaly Kuznetsova506fdd2020-07-10 16:11:55 +0200427
428 vcpu->arch.cr3 = cr3;
429 kvm_register_mark_available(vcpu, VCPU_EXREG_CR3);
430
Sean Christopherson616007c2021-06-22 10:57:34 -0700431 /* Re-initialize the MMU, e.g. to pick up CR4 MMU role changes. */
Sean Christophersonc9060662021-06-09 16:42:33 -0700432 kvm_init_mmu(vcpu);
Vitaly Kuznetsova506fdd2020-07-10 16:11:55 +0200433
434 return 0;
Vitaly Kuznetsov62156f62020-07-10 16:11:53 +0200435}
436
Cathy Avery4995a362021-01-13 07:07:52 -0500437void nested_vmcb02_compute_g_pat(struct vcpu_svm *svm)
438{
439 if (!svm->nested.vmcb02.ptr)
440 return;
441
442 /* FIXME: merge g_pat from vmcb01 and vmcb12. */
443 svm->nested.vmcb02.ptr->save.g_pat = svm->vmcb01.ptr->save.g_pat;
444}
445
Paolo Bonzini9e8f0fb2020-11-17 05:15:41 -0500446static void nested_vmcb02_prepare_save(struct vcpu_svm *svm, struct vmcb *vmcb12)
Joerg Roedel883b0a92020-03-24 10:41:52 +0100447{
Cathy Avery81733962021-03-01 15:08:44 -0500448 bool new_vmcb12 = false;
449
Cathy Avery4995a362021-01-13 07:07:52 -0500450 nested_vmcb02_compute_g_pat(svm);
451
Joerg Roedel883b0a92020-03-24 10:41:52 +0100452 /* Load the nested guest state */
Cathy Avery81733962021-03-01 15:08:44 -0500453 if (svm->nested.vmcb12_gpa != svm->nested.last_vmcb12_gpa) {
454 new_vmcb12 = true;
455 svm->nested.last_vmcb12_gpa = svm->nested.vmcb12_gpa;
456 }
457
458 if (unlikely(new_vmcb12 || vmcb_is_dirty(vmcb12, VMCB_SEG))) {
459 svm->vmcb->save.es = vmcb12->save.es;
460 svm->vmcb->save.cs = vmcb12->save.cs;
461 svm->vmcb->save.ss = vmcb12->save.ss;
462 svm->vmcb->save.ds = vmcb12->save.ds;
463 svm->vmcb->save.cpl = vmcb12->save.cpl;
464 vmcb_mark_dirty(svm->vmcb, VMCB_SEG);
465 }
466
467 if (unlikely(new_vmcb12 || vmcb_is_dirty(vmcb12, VMCB_DT))) {
468 svm->vmcb->save.gdtr = vmcb12->save.gdtr;
469 svm->vmcb->save.idtr = vmcb12->save.idtr;
470 vmcb_mark_dirty(svm->vmcb, VMCB_DT);
471 }
Paolo Bonzini4bb170a2020-11-16 06:38:19 -0500472
Paolo Bonzini8cce12b2020-11-27 12:46:36 -0500473 kvm_set_rflags(&svm->vcpu, vmcb12->save.rflags | X86_EFLAGS_FIXED);
Paolo Bonzini3c346c02021-03-31 06:28:01 -0400474
475 /*
476 * Force-set EFER_SVME even though it is checked earlier on the
477 * VMCB12, because the guest can flip the bit between the check
478 * and now. Clearing EFER_SVME would call svm_free_nested.
479 */
480 svm_set_efer(&svm->vcpu, vmcb12->save.efer | EFER_SVME);
481
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300482 svm_set_cr0(&svm->vcpu, vmcb12->save.cr0);
483 svm_set_cr4(&svm->vcpu, vmcb12->save.cr4);
Paolo Bonzini4bb170a2020-11-16 06:38:19 -0500484
485 svm->vcpu.arch.cr2 = vmcb12->save.cr2;
Cathy Avery81733962021-03-01 15:08:44 -0500486
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300487 kvm_rax_write(&svm->vcpu, vmcb12->save.rax);
488 kvm_rsp_write(&svm->vcpu, vmcb12->save.rsp);
489 kvm_rip_write(&svm->vcpu, vmcb12->save.rip);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100490
491 /* In case we don't even reach vcpu_run, the fields are not updated */
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300492 svm->vmcb->save.rax = vmcb12->save.rax;
493 svm->vmcb->save.rsp = vmcb12->save.rsp;
494 svm->vmcb->save.rip = vmcb12->save.rip;
Paolo Bonzini4bb170a2020-11-16 06:38:19 -0500495
Cathy Avery81733962021-03-01 15:08:44 -0500496 /* These bits will be set properly on the first execution when new_vmc12 is true */
497 if (unlikely(new_vmcb12 || vmcb_is_dirty(vmcb12, VMCB_DR))) {
498 svm->vmcb->save.dr7 = vmcb12->save.dr7 | DR7_FIXED_1;
499 svm->vcpu.arch.dr6 = vmcb12->save.dr6 | DR6_ACTIVE_LOW;
500 vmcb_mark_dirty(svm->vmcb, VMCB_DR);
501 }
Paolo Bonzinif241d712020-05-18 10:56:43 -0400502}
Joerg Roedel883b0a92020-03-24 10:41:52 +0100503
Paolo Bonzini9e8f0fb2020-11-17 05:15:41 -0500504static void nested_vmcb02_prepare_control(struct vcpu_svm *svm)
Paolo Bonzinif241d712020-05-18 10:56:43 -0400505{
Paolo Bonzini91b71302020-05-22 12:28:52 -0400506 const u32 mask = V_INTR_MASKING_MASK | V_GIF_ENABLE_MASK | V_GIF_MASK;
Sean Christophersond2e56012021-06-09 16:42:26 -0700507 struct kvm_vcpu *vcpu = &svm->vcpu;
Vitaly Kuznetsov62156f62020-07-10 16:11:53 +0200508
Paolo Bonzini7c3ecfc2020-11-16 06:13:15 -0500509 /*
510 * Filled at exit: exit_code, exit_code_hi, exit_info_1, exit_info_2,
511 * exit_int_info, exit_int_info_err, next_rip, insn_len, insn_bytes.
512 */
Cathy Avery4995a362021-01-13 07:07:52 -0500513
Paolo Bonzini7c3ecfc2020-11-16 06:13:15 -0500514 /*
515 * Also covers avic_vapic_bar, avic_backing_page, avic_logical_id,
516 * avic_physical_id.
517 */
518 WARN_ON(svm->vmcb01.ptr->control.int_ctl & AVIC_ENABLE_MASK);
519
520 /* Copied from vmcb01. msrpm_base can be overwritten later. */
521 svm->vmcb->control.nested_ctl = svm->vmcb01.ptr->control.nested_ctl;
522 svm->vmcb->control.iopm_base_pa = svm->vmcb01.ptr->control.iopm_base_pa;
523 svm->vmcb->control.msrpm_base_pa = svm->vmcb01.ptr->control.msrpm_base_pa;
524
525 /* Done at vmrun: asid. */
526
527 /* Also overwritten later if necessary. */
528 svm->vmcb->control.tlb_ctl = TLB_CONTROL_DO_NOTHING;
529
530 /* nested_cr3. */
Vitaly Kuznetsov62156f62020-07-10 16:11:53 +0200531 if (nested_npt_enabled(svm))
Sean Christophersond2e56012021-06-09 16:42:26 -0700532 nested_svm_init_mmu_context(vcpu);
Paolo Bonzini69cb8772020-05-22 05:27:46 -0400533
Sean Christophersond2e56012021-06-09 16:42:26 -0700534 svm->vmcb->control.tsc_offset = vcpu->arch.tsc_offset =
535 vcpu->arch.l1_tsc_offset + svm->nested.ctl.tsc_offset;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100536
Paolo Bonzini91b71302020-05-22 12:28:52 -0400537 svm->vmcb->control.int_ctl =
538 (svm->nested.ctl.int_ctl & ~mask) |
Cathy Avery4995a362021-01-13 07:07:52 -0500539 (svm->vmcb01.ptr->control.int_ctl & mask);
Paolo Bonzini91b71302020-05-22 12:28:52 -0400540
Paolo Bonzinie670bf62020-05-13 13:16:12 -0400541 svm->vmcb->control.virt_ext = svm->nested.ctl.virt_ext;
542 svm->vmcb->control.int_vector = svm->nested.ctl.int_vector;
543 svm->vmcb->control.int_state = svm->nested.ctl.int_state;
544 svm->vmcb->control.event_inj = svm->nested.ctl.event_inj;
545 svm->vmcb->control.event_inj_err = svm->nested.ctl.event_inj_err;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100546
Paolo Bonzinie670bf62020-05-13 13:16:12 -0400547 svm->vmcb->control.pause_filter_count = svm->nested.ctl.pause_filter_count;
548 svm->vmcb->control.pause_filter_thresh = svm->nested.ctl.pause_filter_thresh;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100549
Sean Christophersond2e56012021-06-09 16:42:26 -0700550 nested_svm_transition_tlb_flush(vcpu);
551
Joerg Roedel883b0a92020-03-24 10:41:52 +0100552 /* Enter Guest-Mode */
Sean Christophersond2e56012021-06-09 16:42:26 -0700553 enter_guest_mode(vcpu);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100554
555 /*
Paolo Bonzini4bb170a2020-11-16 06:38:19 -0500556 * Merge guest and host intercepts - must be called with vcpu in
557 * guest-mode to take effect.
Joerg Roedel883b0a92020-03-24 10:41:52 +0100558 */
559 recalc_intercepts(svm);
Paolo Bonzinif241d712020-05-18 10:56:43 -0400560}
561
Babu Mogerd00b99c2021-02-17 10:56:04 -0500562static void nested_svm_copy_common_state(struct vmcb *from_vmcb, struct vmcb *to_vmcb)
563{
564 /*
565 * Some VMCB state is shared between L1 and L2 and thus has to be
566 * moved at the time of nested vmrun and vmexit.
567 *
568 * VMLOAD/VMSAVE state would also belong in this category, but KVM
569 * always performs VMLOAD and VMSAVE from the VMCB01.
570 */
571 to_vmcb->save.spec_ctrl = from_vmcb->save.spec_ctrl;
572}
573
Paolo Bonzini63129752021-03-02 14:40:39 -0500574int enter_svm_guest_mode(struct kvm_vcpu *vcpu, u64 vmcb12_gpa,
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300575 struct vmcb *vmcb12)
Paolo Bonzinif241d712020-05-18 10:56:43 -0400576{
Paolo Bonzini63129752021-03-02 14:40:39 -0500577 struct vcpu_svm *svm = to_svm(vcpu);
Vitaly Kuznetsova506fdd2020-07-10 16:11:55 +0200578 int ret;
579
Maxim Levitsky954f4192021-02-17 16:57:13 +0200580 trace_kvm_nested_vmrun(svm->vmcb->save.rip, vmcb12_gpa,
581 vmcb12->save.rip,
582 vmcb12->control.int_ctl,
583 vmcb12->control.event_inj,
584 vmcb12->control.nested_ctl);
585
586 trace_kvm_nested_intercepts(vmcb12->control.intercepts[INTERCEPT_CR] & 0xffff,
587 vmcb12->control.intercepts[INTERCEPT_CR] >> 16,
588 vmcb12->control.intercepts[INTERCEPT_EXCEPTION],
589 vmcb12->control.intercepts[INTERCEPT_WORD3],
590 vmcb12->control.intercepts[INTERCEPT_WORD4],
591 vmcb12->control.intercepts[INTERCEPT_WORD5]);
592
593
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300594 svm->nested.vmcb12_gpa = vmcb12_gpa;
Cathy Avery4995a362021-01-13 07:07:52 -0500595
596 WARN_ON(svm->vmcb == svm->nested.vmcb02.ptr);
597
Babu Mogerd00b99c2021-02-17 10:56:04 -0500598 nested_svm_copy_common_state(svm->vmcb01.ptr, svm->nested.vmcb02.ptr);
Cathy Avery4995a362021-01-13 07:07:52 -0500599
600 svm_switch_vmcb(svm, &svm->nested.vmcb02);
Paolo Bonzini9e8f0fb2020-11-17 05:15:41 -0500601 nested_vmcb02_prepare_control(svm);
602 nested_vmcb02_prepare_save(svm, vmcb12);
Paolo Bonzinif241d712020-05-18 10:56:43 -0400603
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300604 ret = nested_svm_load_cr3(&svm->vcpu, vmcb12->save.cr3,
Maxim Levitskyb222b0b2021-06-07 12:01:59 +0300605 nested_npt_enabled(svm), true);
Vitaly Kuznetsova506fdd2020-07-10 16:11:55 +0200606 if (ret)
607 return ret;
608
Paolo Bonzinia04aead2021-02-18 07:16:59 -0500609 if (!npt_enabled)
Paolo Bonzini63129752021-03-02 14:40:39 -0500610 vcpu->arch.mmu->inject_page_fault = svm_inject_page_fault_nested;
Paolo Bonzinia04aead2021-02-18 07:16:59 -0500611
Paolo Bonziniffdf7f92020-05-22 12:18:27 -0400612 svm_set_gif(svm, true);
Vitaly Kuznetsov59cd9bc2020-07-10 16:11:52 +0200613
614 return 0;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100615}
616
Paolo Bonzini63129752021-03-02 14:40:39 -0500617int nested_svm_vmrun(struct kvm_vcpu *vcpu)
Joerg Roedel883b0a92020-03-24 10:41:52 +0100618{
Paolo Bonzini63129752021-03-02 14:40:39 -0500619 struct vcpu_svm *svm = to_svm(vcpu);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100620 int ret;
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300621 struct vmcb *vmcb12;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100622 struct kvm_host_map map;
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300623 u64 vmcb12_gpa;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100624
Vitaly Kuznetsovfb79f562021-06-28 12:44:21 +0200625 if (!svm->nested.hsave_msr) {
626 kvm_inject_gp(vcpu, 0);
627 return 1;
628 }
629
Paolo Bonzini63129752021-03-02 14:40:39 -0500630 if (is_smm(vcpu)) {
631 kvm_queue_exception(vcpu, UD_VECTOR);
Paolo Bonzini7c67f5462020-04-23 10:52:48 -0400632 return 1;
633 }
Joerg Roedel883b0a92020-03-24 10:41:52 +0100634
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300635 vmcb12_gpa = svm->vmcb->save.rax;
Paolo Bonzini63129752021-03-02 14:40:39 -0500636 ret = kvm_vcpu_map(vcpu, gpa_to_gfn(vmcb12_gpa), &map);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100637 if (ret == -EINVAL) {
Paolo Bonzini63129752021-03-02 14:40:39 -0500638 kvm_inject_gp(vcpu, 0);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100639 return 1;
640 } else if (ret) {
Paolo Bonzini63129752021-03-02 14:40:39 -0500641 return kvm_skip_emulated_instruction(vcpu);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100642 }
643
Paolo Bonzini63129752021-03-02 14:40:39 -0500644 ret = kvm_skip_emulated_instruction(vcpu);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100645
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300646 vmcb12 = map.hva;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100647
Maxim Levitsky2fcf4872020-10-01 14:29:54 +0300648 if (WARN_ON_ONCE(!svm->nested.initialized))
649 return -EINVAL;
650
Paolo Bonzinicb9b6a12021-03-31 07:35:52 -0400651 nested_load_control_from_vmcb12(svm, &vmcb12->control);
652
653 if (!nested_vmcb_valid_sregs(vcpu, &vmcb12->save) ||
Krish Sadhukhanee695f22021-04-12 17:56:08 -0400654 !nested_vmcb_check_controls(vcpu, &svm->nested.ctl)) {
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300655 vmcb12->control.exit_code = SVM_EXIT_ERR;
656 vmcb12->control.exit_code_hi = 0;
657 vmcb12->control.exit_info_1 = 0;
658 vmcb12->control.exit_info_2 = 0;
Paolo Bonzini69c9dfa2020-05-13 12:57:26 -0400659 goto out;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100660 }
661
Joerg Roedel883b0a92020-03-24 10:41:52 +0100662
663 /* Clear internal status */
Paolo Bonzini63129752021-03-02 14:40:39 -0500664 kvm_clear_exception_queue(vcpu);
665 kvm_clear_interrupt_queue(vcpu);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100666
667 /*
Cathy Avery4995a362021-01-13 07:07:52 -0500668 * Since vmcb01 is not in use, we can use it to store some of the L1
669 * state.
Joerg Roedel883b0a92020-03-24 10:41:52 +0100670 */
Paolo Bonzini63129752021-03-02 14:40:39 -0500671 svm->vmcb01.ptr->save.efer = vcpu->arch.efer;
672 svm->vmcb01.ptr->save.cr0 = kvm_read_cr0(vcpu);
673 svm->vmcb01.ptr->save.cr4 = vcpu->arch.cr4;
674 svm->vmcb01.ptr->save.rflags = kvm_get_rflags(vcpu);
675 svm->vmcb01.ptr->save.rip = kvm_rip_read(vcpu);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100676
Cathy Avery4995a362021-01-13 07:07:52 -0500677 if (!npt_enabled)
Paolo Bonzini63129752021-03-02 14:40:39 -0500678 svm->vmcb01.ptr->save.cr3 = kvm_read_cr3(vcpu);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100679
Paolo Bonzinif74f9412020-04-23 13:22:27 -0400680 svm->nested.nested_run_pending = 1;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100681
Paolo Bonzini63129752021-03-02 14:40:39 -0500682 if (enter_svm_guest_mode(vcpu, vmcb12_gpa, vmcb12))
Vitaly Kuznetsov59cd9bc2020-07-10 16:11:52 +0200683 goto out_exit_err;
Vitaly Kuznetsovebdb3db2020-07-10 16:11:51 +0200684
Vitaly Kuznetsov59cd9bc2020-07-10 16:11:52 +0200685 if (nested_svm_vmrun_msrpm(svm))
686 goto out;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100687
Vitaly Kuznetsov59cd9bc2020-07-10 16:11:52 +0200688out_exit_err:
689 svm->nested.nested_run_pending = 0;
690
691 svm->vmcb->control.exit_code = SVM_EXIT_ERR;
692 svm->vmcb->control.exit_code_hi = 0;
693 svm->vmcb->control.exit_info_1 = 0;
694 svm->vmcb->control.exit_info_2 = 0;
695
696 nested_svm_vmexit(svm);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100697
Paolo Bonzini69c9dfa2020-05-13 12:57:26 -0400698out:
Paolo Bonzini63129752021-03-02 14:40:39 -0500699 kvm_vcpu_unmap(vcpu, &map, true);
Paolo Bonzini69c9dfa2020-05-13 12:57:26 -0400700
Joerg Roedel883b0a92020-03-24 10:41:52 +0100701 return ret;
702}
703
Vitaly Kuznetsov0a758292021-06-28 12:44:22 +0200704/* Copy state save area fields which are handled by VMRUN */
Vitaly Kuznetsov2bb16be2021-07-19 11:03:22 +0200705void svm_copy_vmrun_state(struct vmcb_save_area *to_save,
706 struct vmcb_save_area *from_save)
Vitaly Kuznetsov0a758292021-06-28 12:44:22 +0200707{
708 to_save->es = from_save->es;
709 to_save->cs = from_save->cs;
710 to_save->ss = from_save->ss;
711 to_save->ds = from_save->ds;
712 to_save->gdtr = from_save->gdtr;
713 to_save->idtr = from_save->idtr;
714 to_save->rflags = from_save->rflags | X86_EFLAGS_FIXED;
715 to_save->efer = from_save->efer;
716 to_save->cr0 = from_save->cr0;
717 to_save->cr3 = from_save->cr3;
718 to_save->cr4 = from_save->cr4;
719 to_save->rax = from_save->rax;
720 to_save->rsp = from_save->rsp;
721 to_save->rip = from_save->rip;
722 to_save->cpl = 0;
723}
724
Vitaly Kuznetsov2bb16be2021-07-19 11:03:22 +0200725void svm_copy_vmloadsave_state(struct vmcb *to_vmcb, struct vmcb *from_vmcb)
Joerg Roedel883b0a92020-03-24 10:41:52 +0100726{
727 to_vmcb->save.fs = from_vmcb->save.fs;
728 to_vmcb->save.gs = from_vmcb->save.gs;
729 to_vmcb->save.tr = from_vmcb->save.tr;
730 to_vmcb->save.ldtr = from_vmcb->save.ldtr;
731 to_vmcb->save.kernel_gs_base = from_vmcb->save.kernel_gs_base;
732 to_vmcb->save.star = from_vmcb->save.star;
733 to_vmcb->save.lstar = from_vmcb->save.lstar;
734 to_vmcb->save.cstar = from_vmcb->save.cstar;
735 to_vmcb->save.sfmask = from_vmcb->save.sfmask;
736 to_vmcb->save.sysenter_cs = from_vmcb->save.sysenter_cs;
737 to_vmcb->save.sysenter_esp = from_vmcb->save.sysenter_esp;
738 to_vmcb->save.sysenter_eip = from_vmcb->save.sysenter_eip;
739}
740
741int nested_svm_vmexit(struct vcpu_svm *svm)
742{
Paolo Bonzini63129752021-03-02 14:40:39 -0500743 struct kvm_vcpu *vcpu = &svm->vcpu;
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300744 struct vmcb *vmcb12;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100745 struct vmcb *vmcb = svm->vmcb;
746 struct kvm_host_map map;
Paolo Bonzini63129752021-03-02 14:40:39 -0500747 int rc;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100748
Sean Christophersoncb6a32c2021-03-02 09:45:14 -0800749 /* Triple faults in L2 should never escape. */
750 WARN_ON_ONCE(kvm_check_request(KVM_REQ_TRIPLE_FAULT, vcpu));
751
Paolo Bonzini63129752021-03-02 14:40:39 -0500752 rc = kvm_vcpu_map(vcpu, gpa_to_gfn(svm->nested.vmcb12_gpa), &map);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100753 if (rc) {
754 if (rc == -EINVAL)
Paolo Bonzini63129752021-03-02 14:40:39 -0500755 kvm_inject_gp(vcpu, 0);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100756 return 1;
757 }
758
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300759 vmcb12 = map.hva;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100760
761 /* Exit Guest-Mode */
Paolo Bonzini63129752021-03-02 14:40:39 -0500762 leave_guest_mode(vcpu);
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300763 svm->nested.vmcb12_gpa = 0;
Paolo Bonzini2d8a42b2020-05-22 03:50:14 -0400764 WARN_ON_ONCE(svm->nested.nested_run_pending);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100765
Paolo Bonzini63129752021-03-02 14:40:39 -0500766 kvm_clear_request(KVM_REQ_GET_NESTED_STATE_PAGES, vcpu);
Maxim Levitskyf2c7ef32021-01-07 11:38:51 +0200767
Paolo Bonzini38c0b192020-04-23 13:13:09 -0400768 /* in case we halted in L2 */
769 svm->vcpu.arch.mp_state = KVM_MP_STATE_RUNNABLE;
770
Joerg Roedel883b0a92020-03-24 10:41:52 +0100771 /* Give the current vmcb to the guest */
Joerg Roedel883b0a92020-03-24 10:41:52 +0100772
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300773 vmcb12->save.es = vmcb->save.es;
774 vmcb12->save.cs = vmcb->save.cs;
775 vmcb12->save.ss = vmcb->save.ss;
776 vmcb12->save.ds = vmcb->save.ds;
777 vmcb12->save.gdtr = vmcb->save.gdtr;
778 vmcb12->save.idtr = vmcb->save.idtr;
779 vmcb12->save.efer = svm->vcpu.arch.efer;
Paolo Bonzini63129752021-03-02 14:40:39 -0500780 vmcb12->save.cr0 = kvm_read_cr0(vcpu);
781 vmcb12->save.cr3 = kvm_read_cr3(vcpu);
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300782 vmcb12->save.cr2 = vmcb->save.cr2;
783 vmcb12->save.cr4 = svm->vcpu.arch.cr4;
Paolo Bonzini63129752021-03-02 14:40:39 -0500784 vmcb12->save.rflags = kvm_get_rflags(vcpu);
785 vmcb12->save.rip = kvm_rip_read(vcpu);
786 vmcb12->save.rsp = kvm_rsp_read(vcpu);
787 vmcb12->save.rax = kvm_rax_read(vcpu);
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300788 vmcb12->save.dr7 = vmcb->save.dr7;
789 vmcb12->save.dr6 = svm->vcpu.arch.dr6;
790 vmcb12->save.cpl = vmcb->save.cpl;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100791
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300792 vmcb12->control.int_state = vmcb->control.int_state;
793 vmcb12->control.exit_code = vmcb->control.exit_code;
794 vmcb12->control.exit_code_hi = vmcb->control.exit_code_hi;
795 vmcb12->control.exit_info_1 = vmcb->control.exit_info_1;
796 vmcb12->control.exit_info_2 = vmcb->control.exit_info_2;
Paolo Bonzini36e2e982020-05-22 06:04:57 -0400797
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300798 if (vmcb12->control.exit_code != SVM_EXIT_ERR)
Paolo Bonzini9e8f0fb2020-11-17 05:15:41 -0500799 nested_save_pending_event_to_vmcb12(svm, vmcb12);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100800
801 if (svm->nrips_enabled)
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300802 vmcb12->control.next_rip = vmcb->control.next_rip;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100803
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300804 vmcb12->control.int_ctl = svm->nested.ctl.int_ctl;
805 vmcb12->control.tlb_ctl = svm->nested.ctl.tlb_ctl;
806 vmcb12->control.event_inj = svm->nested.ctl.event_inj;
807 vmcb12->control.event_inj_err = svm->nested.ctl.event_inj_err;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100808
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300809 vmcb12->control.pause_filter_count =
Joerg Roedel883b0a92020-03-24 10:41:52 +0100810 svm->vmcb->control.pause_filter_count;
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300811 vmcb12->control.pause_filter_thresh =
Joerg Roedel883b0a92020-03-24 10:41:52 +0100812 svm->vmcb->control.pause_filter_thresh;
813
Babu Mogerd00b99c2021-02-17 10:56:04 -0500814 nested_svm_copy_common_state(svm->nested.vmcb02.ptr, svm->vmcb01.ptr);
815
Cathy Avery4995a362021-01-13 07:07:52 -0500816 svm_switch_vmcb(svm, &svm->vmcb01);
817
818 /*
819 * On vmexit the GIF is set to false and
820 * no event can be injected in L1.
821 */
Maxim Levitsky98837642020-08-27 19:27:18 +0300822 svm_set_gif(svm, false);
Cathy Avery4995a362021-01-13 07:07:52 -0500823 svm->vmcb->control.exit_int_info = 0;
Maxim Levitsky98837642020-08-27 19:27:18 +0300824
Paolo Bonzini7ca62d12020-11-16 06:38:19 -0500825 svm->vcpu.arch.tsc_offset = svm->vcpu.arch.l1_tsc_offset;
826 if (svm->vmcb->control.tsc_offset != svm->vcpu.arch.tsc_offset) {
827 svm->vmcb->control.tsc_offset = svm->vcpu.arch.tsc_offset;
828 vmcb_mark_dirty(svm->vmcb, VMCB_INTERCEPTS);
829 }
Paolo Bonzini18fc6c52020-05-18 11:07:08 -0400830
Paolo Bonzinie670bf62020-05-13 13:16:12 -0400831 svm->nested.ctl.nested_cr3 = 0;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100832
Cathy Avery4995a362021-01-13 07:07:52 -0500833 /*
834 * Restore processor state that had been saved in vmcb01
835 */
Paolo Bonzini63129752021-03-02 14:40:39 -0500836 kvm_set_rflags(vcpu, svm->vmcb->save.rflags);
837 svm_set_efer(vcpu, svm->vmcb->save.efer);
838 svm_set_cr0(vcpu, svm->vmcb->save.cr0 | X86_CR0_PE);
839 svm_set_cr4(vcpu, svm->vmcb->save.cr4);
840 kvm_rax_write(vcpu, svm->vmcb->save.rax);
841 kvm_rsp_write(vcpu, svm->vmcb->save.rsp);
842 kvm_rip_write(vcpu, svm->vmcb->save.rip);
Cathy Avery4995a362021-01-13 07:07:52 -0500843
844 svm->vcpu.arch.dr7 = DR7_FIXED_1;
845 kvm_update_dr7(&svm->vcpu);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100846
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300847 trace_kvm_nested_vmexit_inject(vmcb12->control.exit_code,
848 vmcb12->control.exit_info_1,
849 vmcb12->control.exit_info_2,
850 vmcb12->control.exit_int_info,
851 vmcb12->control.exit_int_info_err,
Paolo Bonzini36e2e982020-05-22 06:04:57 -0400852 KVM_ISA_SVM);
853
Paolo Bonzini63129752021-03-02 14:40:39 -0500854 kvm_vcpu_unmap(vcpu, &map, true);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100855
Sean Christophersond2e56012021-06-09 16:42:26 -0700856 nested_svm_transition_tlb_flush(vcpu);
857
Paolo Bonzini63129752021-03-02 14:40:39 -0500858 nested_svm_uninit_mmu_context(vcpu);
Vitaly Kuznetsovbf7dea42020-07-10 16:11:54 +0200859
Maxim Levitskyb222b0b2021-06-07 12:01:59 +0300860 rc = nested_svm_load_cr3(vcpu, svm->vmcb->save.cr3, false, true);
Vitaly Kuznetsovd82aaef2020-07-10 16:11:56 +0200861 if (rc)
862 return 1;
Vitaly Kuznetsovbf7dea42020-07-10 16:11:54 +0200863
Joerg Roedel883b0a92020-03-24 10:41:52 +0100864 /*
865 * Drop what we picked up for L2 via svm_complete_interrupts() so it
866 * doesn't end up in L1.
867 */
868 svm->vcpu.arch.nmi_injected = false;
Paolo Bonzini63129752021-03-02 14:40:39 -0500869 kvm_clear_exception_queue(vcpu);
870 kvm_clear_interrupt_queue(vcpu);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100871
Krish Sadhukhan9a7de6e2021-03-23 13:50:03 -0400872 /*
873 * If we are here following the completion of a VMRUN that
874 * is being single-stepped, queue the pending #DB intercept
875 * right now so that it an be accounted for before we execute
876 * L1's next instruction.
877 */
878 if (unlikely(svm->vmcb->save.rflags & X86_EFLAGS_TF))
879 kvm_queue_exception(&(svm->vcpu), DB_VECTOR);
880
Joerg Roedel883b0a92020-03-24 10:41:52 +0100881 return 0;
882}
883
Sean Christophersoncb6a32c2021-03-02 09:45:14 -0800884static void nested_svm_triple_fault(struct kvm_vcpu *vcpu)
885{
Sean Christopherson3a87c7e2021-03-02 09:45:15 -0800886 nested_svm_simple_vmexit(to_svm(vcpu), SVM_EXIT_SHUTDOWN);
Sean Christophersoncb6a32c2021-03-02 09:45:14 -0800887}
888
Maxim Levitsky2fcf4872020-10-01 14:29:54 +0300889int svm_allocate_nested(struct vcpu_svm *svm)
890{
Cathy Avery4995a362021-01-13 07:07:52 -0500891 struct page *vmcb02_page;
Maxim Levitsky2fcf4872020-10-01 14:29:54 +0300892
893 if (svm->nested.initialized)
894 return 0;
895
Cathy Avery4995a362021-01-13 07:07:52 -0500896 vmcb02_page = alloc_page(GFP_KERNEL_ACCOUNT | __GFP_ZERO);
897 if (!vmcb02_page)
Maxim Levitsky2fcf4872020-10-01 14:29:54 +0300898 return -ENOMEM;
Cathy Avery4995a362021-01-13 07:07:52 -0500899 svm->nested.vmcb02.ptr = page_address(vmcb02_page);
900 svm->nested.vmcb02.pa = __sme_set(page_to_pfn(vmcb02_page) << PAGE_SHIFT);
Maxim Levitsky2fcf4872020-10-01 14:29:54 +0300901
902 svm->nested.msrpm = svm_vcpu_alloc_msrpm();
903 if (!svm->nested.msrpm)
Cathy Avery4995a362021-01-13 07:07:52 -0500904 goto err_free_vmcb02;
Maxim Levitsky2fcf4872020-10-01 14:29:54 +0300905 svm_vcpu_init_msrpm(&svm->vcpu, svm->nested.msrpm);
906
907 svm->nested.initialized = true;
908 return 0;
909
Cathy Avery4995a362021-01-13 07:07:52 -0500910err_free_vmcb02:
911 __free_page(vmcb02_page);
Maxim Levitsky2fcf4872020-10-01 14:29:54 +0300912 return -ENOMEM;
913}
914
915void svm_free_nested(struct vcpu_svm *svm)
916{
917 if (!svm->nested.initialized)
918 return;
919
920 svm_vcpu_free_msrpm(svm->nested.msrpm);
921 svm->nested.msrpm = NULL;
922
Cathy Avery4995a362021-01-13 07:07:52 -0500923 __free_page(virt_to_page(svm->nested.vmcb02.ptr));
924 svm->nested.vmcb02.ptr = NULL;
Maxim Levitsky2fcf4872020-10-01 14:29:54 +0300925
Maxim Levitskyc74ad082021-05-03 15:54:43 +0300926 /*
927 * When last_vmcb12_gpa matches the current vmcb12 gpa,
928 * some vmcb12 fields are not loaded if they are marked clean
929 * in the vmcb12, since in this case they are up to date already.
930 *
931 * When the vmcb02 is freed, this optimization becomes invalid.
932 */
933 svm->nested.last_vmcb12_gpa = INVALID_GPA;
934
Maxim Levitsky2fcf4872020-10-01 14:29:54 +0300935 svm->nested.initialized = false;
936}
937
Paolo Bonzinic513f482020-05-18 13:08:37 -0400938/*
939 * Forcibly leave nested mode in order to be able to reset the VCPU later on.
940 */
941void svm_leave_nested(struct vcpu_svm *svm)
942{
Paolo Bonzini63129752021-03-02 14:40:39 -0500943 struct kvm_vcpu *vcpu = &svm->vcpu;
944
945 if (is_guest_mode(vcpu)) {
Paolo Bonzinic513f482020-05-18 13:08:37 -0400946 svm->nested.nested_run_pending = 0;
Maxim Levitskyc74ad082021-05-03 15:54:43 +0300947 svm->nested.vmcb12_gpa = INVALID_GPA;
948
Paolo Bonzini63129752021-03-02 14:40:39 -0500949 leave_guest_mode(vcpu);
Cathy Avery4995a362021-01-13 07:07:52 -0500950
Maxim Levitskydeee59b2021-05-03 15:54:42 +0300951 svm_switch_vmcb(svm, &svm->vmcb01);
Cathy Avery4995a362021-01-13 07:07:52 -0500952
Paolo Bonzini63129752021-03-02 14:40:39 -0500953 nested_svm_uninit_mmu_context(vcpu);
Maxim Levitsky56fe28d2021-01-07 11:38:54 +0200954 vmcb_mark_all_dirty(svm->vmcb);
Paolo Bonzinic513f482020-05-18 13:08:37 -0400955 }
Paolo Bonzinia7d5c7c2020-09-22 07:43:14 -0400956
Paolo Bonzini63129752021-03-02 14:40:39 -0500957 kvm_clear_request(KVM_REQ_GET_NESTED_STATE_PAGES, vcpu);
Paolo Bonzinic513f482020-05-18 13:08:37 -0400958}
959
Joerg Roedel883b0a92020-03-24 10:41:52 +0100960static int nested_svm_exit_handled_msr(struct vcpu_svm *svm)
961{
962 u32 offset, msr, value;
963 int write, mask;
964
Babu Mogerc62e2e92020-09-11 14:28:28 -0500965 if (!(vmcb_is_intercept(&svm->nested.ctl, INTERCEPT_MSR_PROT)))
Joerg Roedel883b0a92020-03-24 10:41:52 +0100966 return NESTED_EXIT_HOST;
967
968 msr = svm->vcpu.arch.regs[VCPU_REGS_RCX];
969 offset = svm_msrpm_offset(msr);
970 write = svm->vmcb->control.exit_info_1 & 1;
971 mask = 1 << ((2 * (msr & 0xf)) + write);
972
973 if (offset == MSR_INVALID)
974 return NESTED_EXIT_DONE;
975
976 /* Offset is in 32 bit units but need in 8 bit units */
977 offset *= 4;
978
Paolo Bonzinie670bf62020-05-13 13:16:12 -0400979 if (kvm_vcpu_read_guest(&svm->vcpu, svm->nested.ctl.msrpm_base_pa + offset, &value, 4))
Joerg Roedel883b0a92020-03-24 10:41:52 +0100980 return NESTED_EXIT_DONE;
981
982 return (value & mask) ? NESTED_EXIT_DONE : NESTED_EXIT_HOST;
983}
984
Joerg Roedel883b0a92020-03-24 10:41:52 +0100985static int nested_svm_intercept_ioio(struct vcpu_svm *svm)
986{
987 unsigned port, size, iopm_len;
988 u16 val, mask;
989 u8 start_bit;
990 u64 gpa;
991
Babu Mogerc62e2e92020-09-11 14:28:28 -0500992 if (!(vmcb_is_intercept(&svm->nested.ctl, INTERCEPT_IOIO_PROT)))
Joerg Roedel883b0a92020-03-24 10:41:52 +0100993 return NESTED_EXIT_HOST;
994
995 port = svm->vmcb->control.exit_info_1 >> 16;
996 size = (svm->vmcb->control.exit_info_1 & SVM_IOIO_SIZE_MASK) >>
997 SVM_IOIO_SIZE_SHIFT;
Paolo Bonzinie670bf62020-05-13 13:16:12 -0400998 gpa = svm->nested.ctl.iopm_base_pa + (port / 8);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100999 start_bit = port % 8;
1000 iopm_len = (start_bit + size > 8) ? 2 : 1;
1001 mask = (0xf >> (4 - size)) << start_bit;
1002 val = 0;
1003
1004 if (kvm_vcpu_read_guest(&svm->vcpu, gpa, &val, iopm_len))
1005 return NESTED_EXIT_DONE;
1006
1007 return (val & mask) ? NESTED_EXIT_DONE : NESTED_EXIT_HOST;
1008}
1009
1010static int nested_svm_intercept(struct vcpu_svm *svm)
1011{
1012 u32 exit_code = svm->vmcb->control.exit_code;
1013 int vmexit = NESTED_EXIT_HOST;
1014
1015 switch (exit_code) {
1016 case SVM_EXIT_MSR:
1017 vmexit = nested_svm_exit_handled_msr(svm);
1018 break;
1019 case SVM_EXIT_IOIO:
1020 vmexit = nested_svm_intercept_ioio(svm);
1021 break;
1022 case SVM_EXIT_READ_CR0 ... SVM_EXIT_WRITE_CR8: {
Babu Moger03bfeeb2020-09-11 14:28:05 -05001023 if (vmcb_is_intercept(&svm->nested.ctl, exit_code))
Joerg Roedel883b0a92020-03-24 10:41:52 +01001024 vmexit = NESTED_EXIT_DONE;
1025 break;
1026 }
1027 case SVM_EXIT_READ_DR0 ... SVM_EXIT_WRITE_DR7: {
Babu Moger30abaa882020-09-11 14:28:12 -05001028 if (vmcb_is_intercept(&svm->nested.ctl, exit_code))
Joerg Roedel883b0a92020-03-24 10:41:52 +01001029 vmexit = NESTED_EXIT_DONE;
1030 break;
1031 }
1032 case SVM_EXIT_EXCP_BASE ... SVM_EXIT_EXCP_BASE + 0x1f: {
Paolo Bonzini7c866632020-05-16 08:42:28 -04001033 /*
1034 * Host-intercepted exceptions have been checked already in
1035 * nested_svm_exit_special. There is nothing to do here,
1036 * the vmexit is injected by svm_check_nested_events.
1037 */
1038 vmexit = NESTED_EXIT_DONE;
Joerg Roedel883b0a92020-03-24 10:41:52 +01001039 break;
1040 }
1041 case SVM_EXIT_ERR: {
1042 vmexit = NESTED_EXIT_DONE;
1043 break;
1044 }
1045 default: {
Babu Mogerc62e2e92020-09-11 14:28:28 -05001046 if (vmcb_is_intercept(&svm->nested.ctl, exit_code))
Joerg Roedel883b0a92020-03-24 10:41:52 +01001047 vmexit = NESTED_EXIT_DONE;
1048 }
1049 }
1050
1051 return vmexit;
1052}
1053
1054int nested_svm_exit_handled(struct vcpu_svm *svm)
1055{
1056 int vmexit;
1057
1058 vmexit = nested_svm_intercept(svm);
1059
1060 if (vmexit == NESTED_EXIT_DONE)
1061 nested_svm_vmexit(svm);
1062
1063 return vmexit;
1064}
1065
Paolo Bonzini63129752021-03-02 14:40:39 -05001066int nested_svm_check_permissions(struct kvm_vcpu *vcpu)
Joerg Roedel883b0a92020-03-24 10:41:52 +01001067{
Paolo Bonzini63129752021-03-02 14:40:39 -05001068 if (!(vcpu->arch.efer & EFER_SVME) || !is_paging(vcpu)) {
1069 kvm_queue_exception(vcpu, UD_VECTOR);
Joerg Roedel883b0a92020-03-24 10:41:52 +01001070 return 1;
1071 }
1072
Paolo Bonzini63129752021-03-02 14:40:39 -05001073 if (to_svm(vcpu)->vmcb->save.cpl) {
1074 kvm_inject_gp(vcpu, 0);
Joerg Roedel883b0a92020-03-24 10:41:52 +01001075 return 1;
1076 }
1077
1078 return 0;
1079}
1080
Paolo Bonzini7c866632020-05-16 08:42:28 -04001081static bool nested_exit_on_exception(struct vcpu_svm *svm)
Joerg Roedel883b0a92020-03-24 10:41:52 +01001082{
Paolo Bonzini7c866632020-05-16 08:42:28 -04001083 unsigned int nr = svm->vcpu.arch.exception.nr;
Joerg Roedel883b0a92020-03-24 10:41:52 +01001084
Babu Moger9780d512020-09-11 14:28:20 -05001085 return (svm->nested.ctl.intercepts[INTERCEPT_EXCEPTION] & BIT(nr));
Paolo Bonzini7c866632020-05-16 08:42:28 -04001086}
Joerg Roedel883b0a92020-03-24 10:41:52 +01001087
Paolo Bonzini7c866632020-05-16 08:42:28 -04001088static void nested_svm_inject_exception_vmexit(struct vcpu_svm *svm)
1089{
1090 unsigned int nr = svm->vcpu.arch.exception.nr;
Joerg Roedel883b0a92020-03-24 10:41:52 +01001091
1092 svm->vmcb->control.exit_code = SVM_EXIT_EXCP_BASE + nr;
1093 svm->vmcb->control.exit_code_hi = 0;
Paolo Bonzini7c866632020-05-16 08:42:28 -04001094
1095 if (svm->vcpu.arch.exception.has_error_code)
1096 svm->vmcb->control.exit_info_1 = svm->vcpu.arch.exception.error_code;
Joerg Roedel883b0a92020-03-24 10:41:52 +01001097
1098 /*
1099 * EXITINFO2 is undefined for all exception intercepts other
1100 * than #PF.
1101 */
Paolo Bonzini7c866632020-05-16 08:42:28 -04001102 if (nr == PF_VECTOR) {
1103 if (svm->vcpu.arch.exception.nested_apf)
1104 svm->vmcb->control.exit_info_2 = svm->vcpu.arch.apf.nested_apf_token;
1105 else if (svm->vcpu.arch.exception.has_payload)
1106 svm->vmcb->control.exit_info_2 = svm->vcpu.arch.exception.payload;
1107 else
1108 svm->vmcb->control.exit_info_2 = svm->vcpu.arch.cr2;
1109 } else if (nr == DB_VECTOR) {
1110 /* See inject_pending_event. */
1111 kvm_deliver_exception_payload(&svm->vcpu);
1112 if (svm->vcpu.arch.dr7 & DR7_GD) {
1113 svm->vcpu.arch.dr7 &= ~DR7_GD;
1114 kvm_update_dr7(&svm->vcpu);
1115 }
1116 } else
1117 WARN_ON(svm->vcpu.arch.exception.has_payload);
Joerg Roedel883b0a92020-03-24 10:41:52 +01001118
Paolo Bonzini7c866632020-05-16 08:42:28 -04001119 nested_svm_vmexit(svm);
Joerg Roedel883b0a92020-03-24 10:41:52 +01001120}
1121
Paolo Bonzini5b6724082020-05-16 08:50:35 -04001122static inline bool nested_exit_on_init(struct vcpu_svm *svm)
1123{
Babu Mogerc62e2e92020-09-11 14:28:28 -05001124 return vmcb_is_intercept(&svm->nested.ctl, INTERCEPT_INIT);
Paolo Bonzini5b6724082020-05-16 08:50:35 -04001125}
1126
Paolo Bonzini33b22172020-04-17 10:24:18 -04001127static int svm_check_nested_events(struct kvm_vcpu *vcpu)
Joerg Roedel883b0a92020-03-24 10:41:52 +01001128{
1129 struct vcpu_svm *svm = to_svm(vcpu);
1130 bool block_nested_events =
Paolo Bonzinibd279622020-05-16 08:46:00 -04001131 kvm_event_needs_reinjection(vcpu) || svm->nested.nested_run_pending;
Paolo Bonzini5b6724082020-05-16 08:50:35 -04001132 struct kvm_lapic *apic = vcpu->arch.apic;
1133
1134 if (lapic_in_kernel(vcpu) &&
1135 test_bit(KVM_APIC_INIT, &apic->pending_events)) {
1136 if (block_nested_events)
1137 return -EBUSY;
1138 if (!nested_exit_on_init(svm))
1139 return 0;
Sean Christopherson3a87c7e2021-03-02 09:45:15 -08001140 nested_svm_simple_vmexit(svm, SVM_EXIT_INIT);
Paolo Bonzini5b6724082020-05-16 08:50:35 -04001141 return 0;
1142 }
Joerg Roedel883b0a92020-03-24 10:41:52 +01001143
Paolo Bonzini7c866632020-05-16 08:42:28 -04001144 if (vcpu->arch.exception.pending) {
Maxim Levitsky4020da32021-04-01 17:38:14 +03001145 /*
1146 * Only a pending nested run can block a pending exception.
1147 * Otherwise an injected NMI/interrupt should either be
1148 * lost or delivered to the nested hypervisor in the EXITINTINFO
1149 * vmcb field, while delivering the pending exception.
1150 */
1151 if (svm->nested.nested_run_pending)
Paolo Bonzini7c866632020-05-16 08:42:28 -04001152 return -EBUSY;
1153 if (!nested_exit_on_exception(svm))
1154 return 0;
1155 nested_svm_inject_exception_vmexit(svm);
1156 return 0;
1157 }
1158
Paolo Bonzini221e7612020-04-23 08:13:10 -04001159 if (vcpu->arch.smi_pending && !svm_smi_blocked(vcpu)) {
Paolo Bonzini55714cd2020-04-23 08:17:28 -04001160 if (block_nested_events)
1161 return -EBUSY;
Paolo Bonzini221e7612020-04-23 08:13:10 -04001162 if (!nested_exit_on_smi(svm))
1163 return 0;
Sean Christopherson3a87c7e2021-03-02 09:45:15 -08001164 nested_svm_simple_vmexit(svm, SVM_EXIT_SMI);
Paolo Bonzini55714cd2020-04-23 08:17:28 -04001165 return 0;
1166 }
1167
Paolo Bonzini221e7612020-04-23 08:13:10 -04001168 if (vcpu->arch.nmi_pending && !svm_nmi_blocked(vcpu)) {
Cathy Avery9c3d3702020-04-14 16:11:06 -04001169 if (block_nested_events)
1170 return -EBUSY;
Paolo Bonzini221e7612020-04-23 08:13:10 -04001171 if (!nested_exit_on_nmi(svm))
1172 return 0;
Sean Christopherson3a87c7e2021-03-02 09:45:15 -08001173 nested_svm_simple_vmexit(svm, SVM_EXIT_NMI);
Cathy Avery9c3d3702020-04-14 16:11:06 -04001174 return 0;
1175 }
1176
Paolo Bonzini221e7612020-04-23 08:13:10 -04001177 if (kvm_cpu_has_interrupt(vcpu) && !svm_interrupt_blocked(vcpu)) {
Joerg Roedel883b0a92020-03-24 10:41:52 +01001178 if (block_nested_events)
1179 return -EBUSY;
Paolo Bonzini221e7612020-04-23 08:13:10 -04001180 if (!nested_exit_on_intr(svm))
1181 return 0;
Sean Christopherson3a87c7e2021-03-02 09:45:15 -08001182 trace_kvm_nested_intr_vmexit(svm->vmcb->save.rip);
1183 nested_svm_simple_vmexit(svm, SVM_EXIT_INTR);
Joerg Roedel883b0a92020-03-24 10:41:52 +01001184 return 0;
1185 }
1186
1187 return 0;
1188}
1189
1190int nested_svm_exit_special(struct vcpu_svm *svm)
1191{
1192 u32 exit_code = svm->vmcb->control.exit_code;
1193
1194 switch (exit_code) {
1195 case SVM_EXIT_INTR:
1196 case SVM_EXIT_NMI:
Joerg Roedel883b0a92020-03-24 10:41:52 +01001197 case SVM_EXIT_NPF:
Paolo Bonzini7c866632020-05-16 08:42:28 -04001198 return NESTED_EXIT_HOST;
1199 case SVM_EXIT_EXCP_BASE ... SVM_EXIT_EXCP_BASE + 0x1f: {
1200 u32 excp_bits = 1 << (exit_code - SVM_EXIT_EXCP_BASE);
1201
Cathy Avery4995a362021-01-13 07:07:52 -05001202 if (svm->vmcb01.ptr->control.intercepts[INTERCEPT_EXCEPTION] &
1203 excp_bits)
Paolo Bonzini7c866632020-05-16 08:42:28 -04001204 return NESTED_EXIT_HOST;
1205 else if (exit_code == SVM_EXIT_EXCP_BASE + PF_VECTOR &&
Vitaly Kuznetsov68fd66f2020-05-25 16:41:17 +02001206 svm->vcpu.arch.apf.host_apf_flags)
Paolo Bonzini7c866632020-05-16 08:42:28 -04001207 /* Trap async PF even if not shadowing */
Joerg Roedel883b0a92020-03-24 10:41:52 +01001208 return NESTED_EXIT_HOST;
1209 break;
Paolo Bonzini7c866632020-05-16 08:42:28 -04001210 }
Joerg Roedel883b0a92020-03-24 10:41:52 +01001211 default:
1212 break;
1213 }
1214
1215 return NESTED_EXIT_CONTINUE;
1216}
Paolo Bonzini33b22172020-04-17 10:24:18 -04001217
Paolo Bonzinicc440cd2020-05-13 13:36:32 -04001218static int svm_get_nested_state(struct kvm_vcpu *vcpu,
1219 struct kvm_nested_state __user *user_kvm_nested_state,
1220 u32 user_data_size)
1221{
1222 struct vcpu_svm *svm;
1223 struct kvm_nested_state kvm_state = {
1224 .flags = 0,
1225 .format = KVM_STATE_NESTED_FORMAT_SVM,
1226 .size = sizeof(kvm_state),
1227 };
1228 struct vmcb __user *user_vmcb = (struct vmcb __user *)
1229 &user_kvm_nested_state->data.svm[0];
1230
1231 if (!vcpu)
1232 return kvm_state.size + KVM_STATE_NESTED_SVM_VMCB_SIZE;
1233
1234 svm = to_svm(vcpu);
1235
1236 if (user_data_size < kvm_state.size)
1237 goto out;
1238
1239 /* First fill in the header and copy it out. */
1240 if (is_guest_mode(vcpu)) {
Maxim Levitsky0dd16b52020-08-27 20:11:39 +03001241 kvm_state.hdr.svm.vmcb_pa = svm->nested.vmcb12_gpa;
Paolo Bonzinicc440cd2020-05-13 13:36:32 -04001242 kvm_state.size += KVM_STATE_NESTED_SVM_VMCB_SIZE;
1243 kvm_state.flags |= KVM_STATE_NESTED_GUEST_MODE;
1244
1245 if (svm->nested.nested_run_pending)
1246 kvm_state.flags |= KVM_STATE_NESTED_RUN_PENDING;
1247 }
1248
1249 if (gif_set(svm))
1250 kvm_state.flags |= KVM_STATE_NESTED_GIF_SET;
1251
1252 if (copy_to_user(user_kvm_nested_state, &kvm_state, sizeof(kvm_state)))
1253 return -EFAULT;
1254
1255 if (!is_guest_mode(vcpu))
1256 goto out;
1257
1258 /*
1259 * Copy over the full size of the VMCB rather than just the size
1260 * of the structs.
1261 */
1262 if (clear_user(user_vmcb, KVM_STATE_NESTED_SVM_VMCB_SIZE))
1263 return -EFAULT;
1264 if (copy_to_user(&user_vmcb->control, &svm->nested.ctl,
1265 sizeof(user_vmcb->control)))
1266 return -EFAULT;
Cathy Avery4995a362021-01-13 07:07:52 -05001267 if (copy_to_user(&user_vmcb->save, &svm->vmcb01.ptr->save,
Paolo Bonzinicc440cd2020-05-13 13:36:32 -04001268 sizeof(user_vmcb->save)))
1269 return -EFAULT;
Paolo Bonzinicc440cd2020-05-13 13:36:32 -04001270out:
1271 return kvm_state.size;
1272}
1273
1274static int svm_set_nested_state(struct kvm_vcpu *vcpu,
1275 struct kvm_nested_state __user *user_kvm_nested_state,
1276 struct kvm_nested_state *kvm_state)
1277{
1278 struct vcpu_svm *svm = to_svm(vcpu);
Paolo Bonzinicc440cd2020-05-13 13:36:32 -04001279 struct vmcb __user *user_vmcb = (struct vmcb __user *)
1280 &user_kvm_nested_state->data.svm[0];
Joerg Roedel6ccbd292020-09-07 15:15:02 +02001281 struct vmcb_control_area *ctl;
1282 struct vmcb_save_area *save;
Sean Christophersondbc47392021-06-22 10:56:59 -07001283 unsigned long cr0;
Joerg Roedel6ccbd292020-09-07 15:15:02 +02001284 int ret;
Paolo Bonzinicc440cd2020-05-13 13:36:32 -04001285
Joerg Roedel6ccbd292020-09-07 15:15:02 +02001286 BUILD_BUG_ON(sizeof(struct vmcb_control_area) + sizeof(struct vmcb_save_area) >
1287 KVM_STATE_NESTED_SVM_VMCB_SIZE);
1288
Paolo Bonzinicc440cd2020-05-13 13:36:32 -04001289 if (kvm_state->format != KVM_STATE_NESTED_FORMAT_SVM)
1290 return -EINVAL;
1291
1292 if (kvm_state->flags & ~(KVM_STATE_NESTED_GUEST_MODE |
1293 KVM_STATE_NESTED_RUN_PENDING |
1294 KVM_STATE_NESTED_GIF_SET))
1295 return -EINVAL;
1296
1297 /*
1298 * If in guest mode, vcpu->arch.efer actually refers to the L2 guest's
1299 * EFER.SVME, but EFER.SVME still has to be 1 for VMRUN to succeed.
1300 */
1301 if (!(vcpu->arch.efer & EFER_SVME)) {
1302 /* GIF=1 and no guest mode are required if SVME=0. */
1303 if (kvm_state->flags != KVM_STATE_NESTED_GIF_SET)
1304 return -EINVAL;
1305 }
1306
1307 /* SMM temporarily disables SVM, so we cannot be in guest mode. */
1308 if (is_smm(vcpu) && (kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE))
1309 return -EINVAL;
1310
1311 if (!(kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE)) {
1312 svm_leave_nested(svm);
Vitaly Kuznetsovd5cd6f32020-09-14 15:37:25 +02001313 svm_set_gif(svm, !!(kvm_state->flags & KVM_STATE_NESTED_GIF_SET));
1314 return 0;
Paolo Bonzinicc440cd2020-05-13 13:36:32 -04001315 }
1316
1317 if (!page_address_valid(vcpu, kvm_state->hdr.svm.vmcb_pa))
1318 return -EINVAL;
1319 if (kvm_state->size < sizeof(*kvm_state) + KVM_STATE_NESTED_SVM_VMCB_SIZE)
1320 return -EINVAL;
Paolo Bonzinicc440cd2020-05-13 13:36:32 -04001321
Joerg Roedel6ccbd292020-09-07 15:15:02 +02001322 ret = -ENOMEM;
Sean Christophersoneba04b22021-03-30 19:30:25 -07001323 ctl = kzalloc(sizeof(*ctl), GFP_KERNEL_ACCOUNT);
1324 save = kzalloc(sizeof(*save), GFP_KERNEL_ACCOUNT);
Joerg Roedel6ccbd292020-09-07 15:15:02 +02001325 if (!ctl || !save)
1326 goto out_free;
1327
1328 ret = -EFAULT;
1329 if (copy_from_user(ctl, &user_vmcb->control, sizeof(*ctl)))
1330 goto out_free;
1331 if (copy_from_user(save, &user_vmcb->save, sizeof(*save)))
1332 goto out_free;
1333
1334 ret = -EINVAL;
Krish Sadhukhanee695f22021-04-12 17:56:08 -04001335 if (!nested_vmcb_check_controls(vcpu, ctl))
Joerg Roedel6ccbd292020-09-07 15:15:02 +02001336 goto out_free;
Paolo Bonzinicc440cd2020-05-13 13:36:32 -04001337
1338 /*
1339 * Processor state contains L2 state. Check that it is
Paolo Bonzinicb9b6a12021-03-31 07:35:52 -04001340 * valid for guest mode (see nested_vmcb_check_save).
Paolo Bonzinicc440cd2020-05-13 13:36:32 -04001341 */
1342 cr0 = kvm_read_cr0(vcpu);
1343 if (((cr0 & X86_CR0_CD) == 0) && (cr0 & X86_CR0_NW))
Joerg Roedel6ccbd292020-09-07 15:15:02 +02001344 goto out_free;
Paolo Bonzinicc440cd2020-05-13 13:36:32 -04001345
1346 /*
1347 * Validate host state saved from before VMRUN (see
1348 * nested_svm_check_permissions).
Paolo Bonzinicc440cd2020-05-13 13:36:32 -04001349 */
Krish Sadhukhan6906e062020-10-06 19:06:52 +00001350 if (!(save->cr0 & X86_CR0_PG) ||
1351 !(save->cr0 & X86_CR0_PE) ||
1352 (save->rflags & X86_EFLAGS_VM) ||
Paolo Bonzini63129752021-03-02 14:40:39 -05001353 !nested_vmcb_valid_sregs(vcpu, save))
Joerg Roedel6ccbd292020-09-07 15:15:02 +02001354 goto out_free;
Paolo Bonzinicc440cd2020-05-13 13:36:32 -04001355
1356 /*
Maxim Levitskyb222b0b2021-06-07 12:01:59 +03001357 * While the nested guest CR3 is already checked and set by
1358 * KVM_SET_SREGS, it was set when nested state was yet loaded,
1359 * thus MMU might not be initialized correctly.
1360 * Set it again to fix this.
1361 */
1362
1363 ret = nested_svm_load_cr3(&svm->vcpu, vcpu->arch.cr3,
1364 nested_npt_enabled(svm), false);
1365 if (WARN_ON_ONCE(ret))
1366 goto out_free;
1367
1368
1369 /*
Cathy Avery4995a362021-01-13 07:07:52 -05001370 * All checks done, we can enter guest mode. Userspace provides
1371 * vmcb12.control, which will be combined with L1 and stored into
1372 * vmcb02, and the L1 save state which we store in vmcb01.
1373 * L2 registers if needed are moved from the current VMCB to VMCB02.
Paolo Bonzinicc440cd2020-05-13 13:36:32 -04001374 */
Maxim Levitsky81f76ad2021-01-07 11:38:52 +02001375
Maxim Levitsky9d290e12021-05-03 15:54:44 +03001376 if (is_guest_mode(vcpu))
1377 svm_leave_nested(svm);
1378 else
1379 svm->nested.vmcb02.ptr->save = svm->vmcb01.ptr->save;
1380
Maxim Levitsky063ab162021-05-04 17:39:35 +03001381 svm_set_gif(svm, !!(kvm_state->flags & KVM_STATE_NESTED_GIF_SET));
1382
Maxim Levitsky81f76ad2021-01-07 11:38:52 +02001383 svm->nested.nested_run_pending =
1384 !!(kvm_state->flags & KVM_STATE_NESTED_RUN_PENDING);
1385
Maxim Levitsky0dd16b52020-08-27 20:11:39 +03001386 svm->nested.vmcb12_gpa = kvm_state->hdr.svm.vmcb_pa;
Paolo Bonzinic08f3902020-11-17 02:51:35 -05001387
Vitaly Kuznetsov2bb16be2021-07-19 11:03:22 +02001388 svm_copy_vmrun_state(&svm->vmcb01.ptr->save, save);
Paolo Bonzini9e8f0fb2020-11-17 05:15:41 -05001389 nested_load_control_from_vmcb12(svm, ctl);
Cathy Avery4995a362021-01-13 07:07:52 -05001390
1391 svm_switch_vmcb(svm, &svm->nested.vmcb02);
Paolo Bonzini9e8f0fb2020-11-17 05:15:41 -05001392 nested_vmcb02_prepare_control(svm);
Paolo Bonzinia7d5c7c2020-09-22 07:43:14 -04001393 kvm_make_request(KVM_REQ_GET_NESTED_STATE_PAGES, vcpu);
Joerg Roedel6ccbd292020-09-07 15:15:02 +02001394 ret = 0;
1395out_free:
1396 kfree(save);
1397 kfree(ctl);
1398
1399 return ret;
Paolo Bonzinicc440cd2020-05-13 13:36:32 -04001400}
1401
Maxim Levitsky232f75d2021-04-01 17:18:10 +03001402static bool svm_get_nested_state_pages(struct kvm_vcpu *vcpu)
1403{
1404 struct vcpu_svm *svm = to_svm(vcpu);
1405
1406 if (WARN_ON(!is_guest_mode(vcpu)))
1407 return true;
1408
Maxim Levitsky158a48e2021-06-07 12:02:03 +03001409 if (!vcpu->arch.pdptrs_from_userspace &&
1410 !nested_npt_enabled(svm) && is_pae_paging(vcpu))
Maxim Levitskyb222b0b2021-06-07 12:01:59 +03001411 /*
1412 * Reload the guest's PDPTRs since after a migration
1413 * the guest CR3 might be restored prior to setting the nested
1414 * state which can lead to a load of wrong PDPTRs.
1415 */
1416 if (CC(!load_pdptrs(vcpu, vcpu->arch.walk_mmu, vcpu->arch.cr3)))
1417 return false;
Maxim Levitsky232f75d2021-04-01 17:18:10 +03001418
1419 if (!nested_svm_vmrun_msrpm(svm)) {
1420 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
1421 vcpu->run->internal.suberror =
1422 KVM_INTERNAL_ERROR_EMULATION;
1423 vcpu->run->internal.ndata = 0;
1424 return false;
1425 }
1426
1427 return true;
1428}
1429
Paolo Bonzini33b22172020-04-17 10:24:18 -04001430struct kvm_x86_nested_ops svm_nested_ops = {
1431 .check_events = svm_check_nested_events,
Sean Christophersoncb6a32c2021-03-02 09:45:14 -08001432 .triple_fault = nested_svm_triple_fault,
Paolo Bonzinia7d5c7c2020-09-22 07:43:14 -04001433 .get_nested_state_pages = svm_get_nested_state_pages,
Paolo Bonzinicc440cd2020-05-13 13:36:32 -04001434 .get_state = svm_get_nested_state,
1435 .set_state = svm_set_nested_state,
Paolo Bonzini33b22172020-04-17 10:24:18 -04001436};