blob: e5515477c30a6152046e74e5b0e9e9a4f574f7a2 [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);
Maxim Levitskyc7dfa402021-07-19 16:05:00 +0300161
162 vmcb_set_intercept(c, INTERCEPT_VMLOAD);
163 vmcb_set_intercept(c, INTERCEPT_VMSAVE);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100164}
165
Paolo Bonzini2f675912020-05-18 15:21:22 -0400166static void copy_vmcb_control_area(struct vmcb_control_area *dst,
167 struct vmcb_control_area *from)
Joerg Roedel883b0a92020-03-24 10:41:52 +0100168{
Babu Mogerc45ad722020-09-11 14:27:58 -0500169 unsigned int i;
170
171 for (i = 0; i < MAX_INTERCEPT; i++)
172 dst->intercepts[i] = from->intercepts[i];
173
Joerg Roedel883b0a92020-03-24 10:41:52 +0100174 dst->iopm_base_pa = from->iopm_base_pa;
175 dst->msrpm_base_pa = from->msrpm_base_pa;
176 dst->tsc_offset = from->tsc_offset;
Paolo Bonzini6c0238c2020-05-20 08:02:17 -0400177 /* asid not copied, it is handled manually for svm->vmcb. */
Joerg Roedel883b0a92020-03-24 10:41:52 +0100178 dst->tlb_ctl = from->tlb_ctl;
179 dst->int_ctl = from->int_ctl;
180 dst->int_vector = from->int_vector;
181 dst->int_state = from->int_state;
182 dst->exit_code = from->exit_code;
183 dst->exit_code_hi = from->exit_code_hi;
184 dst->exit_info_1 = from->exit_info_1;
185 dst->exit_info_2 = from->exit_info_2;
186 dst->exit_int_info = from->exit_int_info;
187 dst->exit_int_info_err = from->exit_int_info_err;
188 dst->nested_ctl = from->nested_ctl;
189 dst->event_inj = from->event_inj;
190 dst->event_inj_err = from->event_inj_err;
191 dst->nested_cr3 = from->nested_cr3;
192 dst->virt_ext = from->virt_ext;
193 dst->pause_filter_count = from->pause_filter_count;
194 dst->pause_filter_thresh = from->pause_filter_thresh;
195}
196
197static bool nested_svm_vmrun_msrpm(struct vcpu_svm *svm)
198{
199 /*
200 * This function merges the msr permission bitmaps of kvm and the
201 * nested vmcb. It is optimized in that it only merges the parts where
202 * the kvm msr permission bitmap may contain zero bits
203 */
204 int i;
205
Babu Mogerc62e2e92020-09-11 14:28:28 -0500206 if (!(vmcb_is_intercept(&svm->nested.ctl, INTERCEPT_MSR_PROT)))
Joerg Roedel883b0a92020-03-24 10:41:52 +0100207 return true;
208
209 for (i = 0; i < MSRPM_OFFSETS; i++) {
210 u32 value, p;
211 u64 offset;
212
213 if (msrpm_offsets[i] == 0xffffffff)
214 break;
215
216 p = msrpm_offsets[i];
Paolo Bonzinie670bf62020-05-13 13:16:12 -0400217 offset = svm->nested.ctl.msrpm_base_pa + (p * 4);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100218
219 if (kvm_vcpu_read_guest(&svm->vcpu, offset, &value, 4))
220 return false;
221
222 svm->nested.msrpm[p] = svm->msrpm[p] | value;
223 }
224
225 svm->vmcb->control.msrpm_base_pa = __sme_set(__pa(svm->nested.msrpm));
226
227 return true;
228}
229
Krish Sadhukhanee695f22021-04-12 17:56:08 -0400230/*
231 * Bits 11:0 of bitmap address are ignored by hardware
232 */
233static bool nested_svm_check_bitmap_pa(struct kvm_vcpu *vcpu, u64 pa, u32 size)
234{
235 u64 addr = PAGE_ALIGN(pa);
236
237 return kvm_vcpu_is_legal_gpa(vcpu, addr) &&
238 kvm_vcpu_is_legal_gpa(vcpu, addr + size - 1);
239}
240
241static bool nested_vmcb_check_controls(struct kvm_vcpu *vcpu,
242 struct vmcb_control_area *control)
Paolo Bonzinica46d732020-05-18 13:02:15 -0400243{
Sean Christopherson11f0cbf2021-02-03 16:01:17 -0800244 if (CC(!vmcb_is_intercept(control, INTERCEPT_VMRUN)))
Paolo Bonzinica46d732020-05-18 13:02:15 -0400245 return false;
246
Sean Christopherson11f0cbf2021-02-03 16:01:17 -0800247 if (CC(control->asid == 0))
Paolo Bonzinica46d732020-05-18 13:02:15 -0400248 return false;
249
Sean Christopherson11f0cbf2021-02-03 16:01:17 -0800250 if (CC((control->nested_ctl & SVM_NESTED_CTL_NP_ENABLE) && !npt_enabled))
Paolo Bonzinica46d732020-05-18 13:02:15 -0400251 return false;
252
Krish Sadhukhanee695f22021-04-12 17:56:08 -0400253 if (CC(!nested_svm_check_bitmap_pa(vcpu, control->msrpm_base_pa,
254 MSRPM_SIZE)))
255 return false;
256 if (CC(!nested_svm_check_bitmap_pa(vcpu, control->iopm_base_pa,
257 IOPM_SIZE)))
258 return false;
259
Paolo Bonzinica46d732020-05-18 13:02:15 -0400260 return true;
261}
262
Paolo Bonzini63129752021-03-02 14:40:39 -0500263static bool nested_vmcb_check_cr3_cr4(struct kvm_vcpu *vcpu,
Krish Sadhukhan6906e062020-10-06 19:06:52 +0000264 struct vmcb_save_area *save)
Joerg Roedel883b0a92020-03-24 10:41:52 +0100265{
Krish Sadhukhan6906e062020-10-06 19:06:52 +0000266 /*
267 * These checks are also performed by KVM_SET_SREGS,
268 * except that EFER.LMA is not checked by SVM against
269 * CR0.PG && EFER.LME.
270 */
271 if ((save->efer & EFER_LME) && (save->cr0 & X86_CR0_PG)) {
Sean Christopherson11f0cbf2021-02-03 16:01:17 -0800272 if (CC(!(save->cr4 & X86_CR4_PAE)) ||
273 CC(!(save->cr0 & X86_CR0_PE)) ||
274 CC(kvm_vcpu_is_illegal_gpa(vcpu, save->cr3)))
Krish Sadhukhan761e4162020-07-08 00:39:56 +0000275 return false;
276 }
Krish Sadhukhan6906e062020-10-06 19:06:52 +0000277
Sean Christopherson11f0cbf2021-02-03 16:01:17 -0800278 if (CC(!kvm_is_valid_cr4(vcpu, save->cr4)))
279 return false;
280
281 return true;
Krish Sadhukhan6906e062020-10-06 19:06:52 +0000282}
283
284/* Common checks that apply to both L1 and L2 state. */
Paolo Bonzini63129752021-03-02 14:40:39 -0500285static bool nested_vmcb_valid_sregs(struct kvm_vcpu *vcpu,
Krish Sadhukhan6906e062020-10-06 19:06:52 +0000286 struct vmcb_save_area *save)
287{
Paolo Bonzini3c346c02021-03-31 06:28:01 -0400288 /*
289 * FIXME: these should be done after copying the fields,
290 * to avoid TOC/TOU races. For these save area checks
291 * the possible damage is limited since kvm_set_cr0 and
292 * kvm_set_cr4 handle failure; EFER_SVME is an exception
293 * so it is force-set later in nested_prepare_vmcb_save.
294 */
Sean Christopherson11f0cbf2021-02-03 16:01:17 -0800295 if (CC(!(save->efer & EFER_SVME)))
Krish Sadhukhan6906e062020-10-06 19:06:52 +0000296 return false;
297
Sean Christopherson11f0cbf2021-02-03 16:01:17 -0800298 if (CC((save->cr0 & X86_CR0_CD) == 0 && (save->cr0 & X86_CR0_NW)) ||
299 CC(save->cr0 & ~0xffffffffULL))
Krish Sadhukhan6906e062020-10-06 19:06:52 +0000300 return false;
301
Sean Christopherson11f0cbf2021-02-03 16:01:17 -0800302 if (CC(!kvm_dr6_valid(save->dr6)) || CC(!kvm_dr7_valid(save->dr7)))
Krish Sadhukhan6906e062020-10-06 19:06:52 +0000303 return false;
304
Paolo Bonzini63129752021-03-02 14:40:39 -0500305 if (!nested_vmcb_check_cr3_cr4(vcpu, save))
Krish Sadhukhan6906e062020-10-06 19:06:52 +0000306 return false;
307
Paolo Bonzini63129752021-03-02 14:40:39 -0500308 if (CC(!kvm_valid_efer(vcpu, save->efer)))
Krish Sadhukhan6906e062020-10-06 19:06:52 +0000309 return false;
310
311 return true;
312}
313
Vitaly Kuznetsovbb00bd92021-06-28 12:44:24 +0200314void nested_load_control_from_vmcb12(struct vcpu_svm *svm,
315 struct vmcb_control_area *control)
Paolo Bonzini3e06f012020-05-13 13:07:26 -0400316{
Paolo Bonzinie670bf62020-05-13 13:16:12 -0400317 copy_vmcb_control_area(&svm->nested.ctl, control);
Paolo Bonzini3e06f012020-05-13 13:07:26 -0400318
Paolo Bonzinicc440cd2020-05-13 13:36:32 -0400319 /* Copy it here because nested_svm_check_controls will check it. */
320 svm->nested.ctl.asid = control->asid;
Paolo Bonzinie670bf62020-05-13 13:16:12 -0400321 svm->nested.ctl.msrpm_base_pa &= ~0x0fffULL;
322 svm->nested.ctl.iopm_base_pa &= ~0x0fffULL;
Paolo Bonzini3e06f012020-05-13 13:07:26 -0400323}
324
Paolo Bonzini2d8a42b2020-05-22 03:50:14 -0400325/*
326 * Synchronize fields that are written by the processor, so that
Paolo Bonzini9e8f0fb2020-11-17 05:15:41 -0500327 * they can be copied back into the vmcb12.
Paolo Bonzini2d8a42b2020-05-22 03:50:14 -0400328 */
Paolo Bonzini9e8f0fb2020-11-17 05:15:41 -0500329void nested_sync_control_from_vmcb02(struct vcpu_svm *svm)
Paolo Bonzini2d8a42b2020-05-22 03:50:14 -0400330{
331 u32 mask;
332 svm->nested.ctl.event_inj = svm->vmcb->control.event_inj;
333 svm->nested.ctl.event_inj_err = svm->vmcb->control.event_inj_err;
334
335 /* Only a few fields of int_ctl are written by the processor. */
336 mask = V_IRQ_MASK | V_TPR_MASK;
337 if (!(svm->nested.ctl.int_ctl & V_INTR_MASKING_MASK) &&
Joerg Roedela284ba52020-06-25 10:03:24 +0200338 svm_is_intercept(svm, INTERCEPT_VINTR)) {
Paolo Bonzini2d8a42b2020-05-22 03:50:14 -0400339 /*
340 * In order to request an interrupt window, L0 is usurping
341 * svm->vmcb->control.int_ctl and possibly setting V_IRQ
342 * even if it was clear in L1's VMCB. Restoring it would be
343 * wrong. However, in this case V_IRQ will remain true until
344 * interrupt_window_interception calls svm_clear_vintr and
345 * restores int_ctl. We can just leave it aside.
346 */
347 mask &= ~V_IRQ_MASK;
348 }
349 svm->nested.ctl.int_ctl &= ~mask;
350 svm->nested.ctl.int_ctl |= svm->vmcb->control.int_ctl & mask;
351}
352
Paolo Bonzini36e2e982020-05-22 06:04:57 -0400353/*
354 * Transfer any event that L0 or L1 wanted to inject into L2 to
355 * EXIT_INT_INFO.
356 */
Paolo Bonzini9e8f0fb2020-11-17 05:15:41 -0500357static void nested_save_pending_event_to_vmcb12(struct vcpu_svm *svm,
358 struct vmcb *vmcb12)
Paolo Bonzini36e2e982020-05-22 06:04:57 -0400359{
360 struct kvm_vcpu *vcpu = &svm->vcpu;
361 u32 exit_int_info = 0;
362 unsigned int nr;
363
364 if (vcpu->arch.exception.injected) {
365 nr = vcpu->arch.exception.nr;
366 exit_int_info = nr | SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_EXEPT;
367
368 if (vcpu->arch.exception.has_error_code) {
369 exit_int_info |= SVM_EVTINJ_VALID_ERR;
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300370 vmcb12->control.exit_int_info_err =
Paolo Bonzini36e2e982020-05-22 06:04:57 -0400371 vcpu->arch.exception.error_code;
372 }
373
374 } else if (vcpu->arch.nmi_injected) {
375 exit_int_info = SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_NMI;
376
377 } else if (vcpu->arch.interrupt.injected) {
378 nr = vcpu->arch.interrupt.nr;
379 exit_int_info = nr | SVM_EVTINJ_VALID;
380
381 if (vcpu->arch.interrupt.soft)
382 exit_int_info |= SVM_EVTINJ_TYPE_SOFT;
383 else
384 exit_int_info |= SVM_EVTINJ_TYPE_INTR;
385 }
386
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300387 vmcb12->control.exit_int_info = exit_int_info;
Paolo Bonzini36e2e982020-05-22 06:04:57 -0400388}
389
Vitaly Kuznetsov62156f62020-07-10 16:11:53 +0200390static inline bool nested_npt_enabled(struct vcpu_svm *svm)
391{
392 return svm->nested.ctl.nested_ctl & SVM_NESTED_CTL_NP_ENABLE;
393}
394
Sean Christophersond2e56012021-06-09 16:42:26 -0700395static void nested_svm_transition_tlb_flush(struct kvm_vcpu *vcpu)
396{
397 /*
398 * TODO: optimize unconditional TLB flush/MMU sync. A partial list of
399 * things to fix before this can be conditional:
400 *
401 * - Flush TLBs for both L1 and L2 remote TLB flush
402 * - Honor L1's request to flush an ASID on nested VMRUN
403 * - Sync nested NPT MMU on VMRUN that flushes L2's ASID[*]
404 * - Don't crush a pending TLB flush in vmcb02 on nested VMRUN
405 * - Flush L1's ASID on KVM_REQ_TLB_FLUSH_GUEST
406 *
407 * [*] Unlike nested EPT, SVM's ASID management can invalidate nested
408 * NPT guest-physical mappings on VMRUN.
409 */
410 kvm_make_request(KVM_REQ_MMU_SYNC, vcpu);
411 kvm_make_request(KVM_REQ_TLB_FLUSH_CURRENT, vcpu);
412}
413
Vitaly Kuznetsov62156f62020-07-10 16:11:53 +0200414/*
Vitaly Kuznetsovd82aaef2020-07-10 16:11:56 +0200415 * Load guest's/host's cr3 on nested vmentry or vmexit. @nested_npt is true
416 * if we are emulating VM-Entry into a guest with NPT enabled.
Vitaly Kuznetsov62156f62020-07-10 16:11:53 +0200417 */
418static int nested_svm_load_cr3(struct kvm_vcpu *vcpu, unsigned long cr3,
Maxim Levitskyb222b0b2021-06-07 12:01:59 +0300419 bool nested_npt, bool reload_pdptrs)
Vitaly Kuznetsov62156f62020-07-10 16:11:53 +0200420{
Sean Christopherson11f0cbf2021-02-03 16:01:17 -0800421 if (CC(kvm_vcpu_is_illegal_gpa(vcpu, cr3)))
Vitaly Kuznetsova506fdd2020-07-10 16:11:55 +0200422 return -EINVAL;
423
Maxim Levitskyb222b0b2021-06-07 12:01:59 +0300424 if (reload_pdptrs && !nested_npt && is_pae_paging(vcpu) &&
Sean Christophersona36dbec62021-06-07 12:01:57 +0300425 CC(!load_pdptrs(vcpu, vcpu->arch.walk_mmu, cr3)))
426 return -EINVAL;
Vitaly Kuznetsova506fdd2020-07-10 16:11:55 +0200427
Vitaly Kuznetsova506fdd2020-07-10 16:11:55 +0200428 if (!nested_npt)
Sean Christophersonb5129102021-06-09 16:42:27 -0700429 kvm_mmu_new_pgd(vcpu, cr3);
Vitaly Kuznetsova506fdd2020-07-10 16:11:55 +0200430
431 vcpu->arch.cr3 = cr3;
432 kvm_register_mark_available(vcpu, VCPU_EXREG_CR3);
433
Sean Christopherson616007c2021-06-22 10:57:34 -0700434 /* Re-initialize the MMU, e.g. to pick up CR4 MMU role changes. */
Sean Christophersonc9060662021-06-09 16:42:33 -0700435 kvm_init_mmu(vcpu);
Vitaly Kuznetsova506fdd2020-07-10 16:11:55 +0200436
437 return 0;
Vitaly Kuznetsov62156f62020-07-10 16:11:53 +0200438}
439
Cathy Avery4995a362021-01-13 07:07:52 -0500440void nested_vmcb02_compute_g_pat(struct vcpu_svm *svm)
441{
442 if (!svm->nested.vmcb02.ptr)
443 return;
444
445 /* FIXME: merge g_pat from vmcb01 and vmcb12. */
446 svm->nested.vmcb02.ptr->save.g_pat = svm->vmcb01.ptr->save.g_pat;
447}
448
Paolo Bonzini9e8f0fb2020-11-17 05:15:41 -0500449static void nested_vmcb02_prepare_save(struct vcpu_svm *svm, struct vmcb *vmcb12)
Joerg Roedel883b0a92020-03-24 10:41:52 +0100450{
Cathy Avery81733962021-03-01 15:08:44 -0500451 bool new_vmcb12 = false;
452
Cathy Avery4995a362021-01-13 07:07:52 -0500453 nested_vmcb02_compute_g_pat(svm);
454
Joerg Roedel883b0a92020-03-24 10:41:52 +0100455 /* Load the nested guest state */
Cathy Avery81733962021-03-01 15:08:44 -0500456 if (svm->nested.vmcb12_gpa != svm->nested.last_vmcb12_gpa) {
457 new_vmcb12 = true;
458 svm->nested.last_vmcb12_gpa = svm->nested.vmcb12_gpa;
459 }
460
461 if (unlikely(new_vmcb12 || vmcb_is_dirty(vmcb12, VMCB_SEG))) {
462 svm->vmcb->save.es = vmcb12->save.es;
463 svm->vmcb->save.cs = vmcb12->save.cs;
464 svm->vmcb->save.ss = vmcb12->save.ss;
465 svm->vmcb->save.ds = vmcb12->save.ds;
466 svm->vmcb->save.cpl = vmcb12->save.cpl;
467 vmcb_mark_dirty(svm->vmcb, VMCB_SEG);
468 }
469
470 if (unlikely(new_vmcb12 || vmcb_is_dirty(vmcb12, VMCB_DT))) {
471 svm->vmcb->save.gdtr = vmcb12->save.gdtr;
472 svm->vmcb->save.idtr = vmcb12->save.idtr;
473 vmcb_mark_dirty(svm->vmcb, VMCB_DT);
474 }
Paolo Bonzini4bb170a2020-11-16 06:38:19 -0500475
Paolo Bonzini8cce12b2020-11-27 12:46:36 -0500476 kvm_set_rflags(&svm->vcpu, vmcb12->save.rflags | X86_EFLAGS_FIXED);
Paolo Bonzini3c346c02021-03-31 06:28:01 -0400477
478 /*
479 * Force-set EFER_SVME even though it is checked earlier on the
480 * VMCB12, because the guest can flip the bit between the check
481 * and now. Clearing EFER_SVME would call svm_free_nested.
482 */
483 svm_set_efer(&svm->vcpu, vmcb12->save.efer | EFER_SVME);
484
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300485 svm_set_cr0(&svm->vcpu, vmcb12->save.cr0);
486 svm_set_cr4(&svm->vcpu, vmcb12->save.cr4);
Paolo Bonzini4bb170a2020-11-16 06:38:19 -0500487
488 svm->vcpu.arch.cr2 = vmcb12->save.cr2;
Cathy Avery81733962021-03-01 15:08:44 -0500489
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300490 kvm_rax_write(&svm->vcpu, vmcb12->save.rax);
491 kvm_rsp_write(&svm->vcpu, vmcb12->save.rsp);
492 kvm_rip_write(&svm->vcpu, vmcb12->save.rip);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100493
494 /* In case we don't even reach vcpu_run, the fields are not updated */
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300495 svm->vmcb->save.rax = vmcb12->save.rax;
496 svm->vmcb->save.rsp = vmcb12->save.rsp;
497 svm->vmcb->save.rip = vmcb12->save.rip;
Paolo Bonzini4bb170a2020-11-16 06:38:19 -0500498
Cathy Avery81733962021-03-01 15:08:44 -0500499 /* These bits will be set properly on the first execution when new_vmc12 is true */
500 if (unlikely(new_vmcb12 || vmcb_is_dirty(vmcb12, VMCB_DR))) {
501 svm->vmcb->save.dr7 = vmcb12->save.dr7 | DR7_FIXED_1;
502 svm->vcpu.arch.dr6 = vmcb12->save.dr6 | DR6_ACTIVE_LOW;
503 vmcb_mark_dirty(svm->vmcb, VMCB_DR);
504 }
Paolo Bonzinif241d712020-05-18 10:56:43 -0400505}
Joerg Roedel883b0a92020-03-24 10:41:52 +0100506
Paolo Bonzini9e8f0fb2020-11-17 05:15:41 -0500507static void nested_vmcb02_prepare_control(struct vcpu_svm *svm)
Paolo Bonzinif241d712020-05-18 10:56:43 -0400508{
Maxim Levitsky0f923e02021-07-15 01:56:24 +0300509 const u32 int_ctl_vmcb01_bits =
510 V_INTR_MASKING_MASK | V_GIF_MASK | V_GIF_ENABLE_MASK;
511
512 const u32 int_ctl_vmcb12_bits = V_TPR_MASK | V_IRQ_INJECTION_BITS_MASK;
513
Sean Christophersond2e56012021-06-09 16:42:26 -0700514 struct kvm_vcpu *vcpu = &svm->vcpu;
Vitaly Kuznetsov62156f62020-07-10 16:11:53 +0200515
Paolo Bonzini7c3ecfc2020-11-16 06:13:15 -0500516 /*
517 * Filled at exit: exit_code, exit_code_hi, exit_info_1, exit_info_2,
518 * exit_int_info, exit_int_info_err, next_rip, insn_len, insn_bytes.
519 */
Cathy Avery4995a362021-01-13 07:07:52 -0500520
Paolo Bonzini7c3ecfc2020-11-16 06:13:15 -0500521 /*
522 * Also covers avic_vapic_bar, avic_backing_page, avic_logical_id,
523 * avic_physical_id.
524 */
Maxim Levitskyfeea0132021-07-13 17:20:17 +0300525 WARN_ON(kvm_apicv_activated(svm->vcpu.kvm));
Paolo Bonzini7c3ecfc2020-11-16 06:13:15 -0500526
527 /* Copied from vmcb01. msrpm_base can be overwritten later. */
528 svm->vmcb->control.nested_ctl = svm->vmcb01.ptr->control.nested_ctl;
529 svm->vmcb->control.iopm_base_pa = svm->vmcb01.ptr->control.iopm_base_pa;
530 svm->vmcb->control.msrpm_base_pa = svm->vmcb01.ptr->control.msrpm_base_pa;
531
532 /* Done at vmrun: asid. */
533
534 /* Also overwritten later if necessary. */
535 svm->vmcb->control.tlb_ctl = TLB_CONTROL_DO_NOTHING;
536
537 /* nested_cr3. */
Vitaly Kuznetsov62156f62020-07-10 16:11:53 +0200538 if (nested_npt_enabled(svm))
Sean Christophersond2e56012021-06-09 16:42:26 -0700539 nested_svm_init_mmu_context(vcpu);
Paolo Bonzini69cb8772020-05-22 05:27:46 -0400540
Sean Christophersond2e56012021-06-09 16:42:26 -0700541 svm->vmcb->control.tsc_offset = vcpu->arch.tsc_offset =
542 vcpu->arch.l1_tsc_offset + svm->nested.ctl.tsc_offset;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100543
Paolo Bonzini91b71302020-05-22 12:28:52 -0400544 svm->vmcb->control.int_ctl =
Maxim Levitsky0f923e02021-07-15 01:56:24 +0300545 (svm->nested.ctl.int_ctl & int_ctl_vmcb12_bits) |
546 (svm->vmcb01.ptr->control.int_ctl & int_ctl_vmcb01_bits);
Paolo Bonzini91b71302020-05-22 12:28:52 -0400547
Paolo Bonzinie670bf62020-05-13 13:16:12 -0400548 svm->vmcb->control.virt_ext = svm->nested.ctl.virt_ext;
549 svm->vmcb->control.int_vector = svm->nested.ctl.int_vector;
550 svm->vmcb->control.int_state = svm->nested.ctl.int_state;
551 svm->vmcb->control.event_inj = svm->nested.ctl.event_inj;
552 svm->vmcb->control.event_inj_err = svm->nested.ctl.event_inj_err;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100553
Paolo Bonzinie670bf62020-05-13 13:16:12 -0400554 svm->vmcb->control.pause_filter_count = svm->nested.ctl.pause_filter_count;
555 svm->vmcb->control.pause_filter_thresh = svm->nested.ctl.pause_filter_thresh;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100556
Sean Christophersond2e56012021-06-09 16:42:26 -0700557 nested_svm_transition_tlb_flush(vcpu);
558
Joerg Roedel883b0a92020-03-24 10:41:52 +0100559 /* Enter Guest-Mode */
Sean Christophersond2e56012021-06-09 16:42:26 -0700560 enter_guest_mode(vcpu);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100561
562 /*
Paolo Bonzini4bb170a2020-11-16 06:38:19 -0500563 * Merge guest and host intercepts - must be called with vcpu in
564 * guest-mode to take effect.
Joerg Roedel883b0a92020-03-24 10:41:52 +0100565 */
566 recalc_intercepts(svm);
Paolo Bonzinif241d712020-05-18 10:56:43 -0400567}
568
Babu Mogerd00b99c2021-02-17 10:56:04 -0500569static void nested_svm_copy_common_state(struct vmcb *from_vmcb, struct vmcb *to_vmcb)
570{
571 /*
572 * Some VMCB state is shared between L1 and L2 and thus has to be
573 * moved at the time of nested vmrun and vmexit.
574 *
575 * VMLOAD/VMSAVE state would also belong in this category, but KVM
576 * always performs VMLOAD and VMSAVE from the VMCB01.
577 */
578 to_vmcb->save.spec_ctrl = from_vmcb->save.spec_ctrl;
579}
580
Paolo Bonzini63129752021-03-02 14:40:39 -0500581int enter_svm_guest_mode(struct kvm_vcpu *vcpu, u64 vmcb12_gpa,
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300582 struct vmcb *vmcb12)
Paolo Bonzinif241d712020-05-18 10:56:43 -0400583{
Paolo Bonzini63129752021-03-02 14:40:39 -0500584 struct vcpu_svm *svm = to_svm(vcpu);
Vitaly Kuznetsova506fdd2020-07-10 16:11:55 +0200585 int ret;
586
Maxim Levitsky954f4192021-02-17 16:57:13 +0200587 trace_kvm_nested_vmrun(svm->vmcb->save.rip, vmcb12_gpa,
588 vmcb12->save.rip,
589 vmcb12->control.int_ctl,
590 vmcb12->control.event_inj,
591 vmcb12->control.nested_ctl);
592
593 trace_kvm_nested_intercepts(vmcb12->control.intercepts[INTERCEPT_CR] & 0xffff,
594 vmcb12->control.intercepts[INTERCEPT_CR] >> 16,
595 vmcb12->control.intercepts[INTERCEPT_EXCEPTION],
596 vmcb12->control.intercepts[INTERCEPT_WORD3],
597 vmcb12->control.intercepts[INTERCEPT_WORD4],
598 vmcb12->control.intercepts[INTERCEPT_WORD5]);
599
600
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300601 svm->nested.vmcb12_gpa = vmcb12_gpa;
Cathy Avery4995a362021-01-13 07:07:52 -0500602
603 WARN_ON(svm->vmcb == svm->nested.vmcb02.ptr);
604
Babu Mogerd00b99c2021-02-17 10:56:04 -0500605 nested_svm_copy_common_state(svm->vmcb01.ptr, svm->nested.vmcb02.ptr);
Cathy Avery4995a362021-01-13 07:07:52 -0500606
607 svm_switch_vmcb(svm, &svm->nested.vmcb02);
Paolo Bonzini9e8f0fb2020-11-17 05:15:41 -0500608 nested_vmcb02_prepare_control(svm);
609 nested_vmcb02_prepare_save(svm, vmcb12);
Paolo Bonzinif241d712020-05-18 10:56:43 -0400610
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300611 ret = nested_svm_load_cr3(&svm->vcpu, vmcb12->save.cr3,
Maxim Levitskyb222b0b2021-06-07 12:01:59 +0300612 nested_npt_enabled(svm), true);
Vitaly Kuznetsova506fdd2020-07-10 16:11:55 +0200613 if (ret)
614 return ret;
615
Paolo Bonzinia04aead2021-02-18 07:16:59 -0500616 if (!npt_enabled)
Paolo Bonzini63129752021-03-02 14:40:39 -0500617 vcpu->arch.mmu->inject_page_fault = svm_inject_page_fault_nested;
Paolo Bonzinia04aead2021-02-18 07:16:59 -0500618
Paolo Bonziniffdf7f92020-05-22 12:18:27 -0400619 svm_set_gif(svm, true);
Vitaly Kuznetsov59cd9bc2020-07-10 16:11:52 +0200620
621 return 0;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100622}
623
Paolo Bonzini63129752021-03-02 14:40:39 -0500624int nested_svm_vmrun(struct kvm_vcpu *vcpu)
Joerg Roedel883b0a92020-03-24 10:41:52 +0100625{
Paolo Bonzini63129752021-03-02 14:40:39 -0500626 struct vcpu_svm *svm = to_svm(vcpu);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100627 int ret;
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300628 struct vmcb *vmcb12;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100629 struct kvm_host_map map;
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300630 u64 vmcb12_gpa;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100631
Vitaly Kuznetsovfb79f562021-06-28 12:44:21 +0200632 if (!svm->nested.hsave_msr) {
633 kvm_inject_gp(vcpu, 0);
634 return 1;
635 }
636
Paolo Bonzini63129752021-03-02 14:40:39 -0500637 if (is_smm(vcpu)) {
638 kvm_queue_exception(vcpu, UD_VECTOR);
Paolo Bonzini7c67f5462020-04-23 10:52:48 -0400639 return 1;
640 }
Joerg Roedel883b0a92020-03-24 10:41:52 +0100641
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300642 vmcb12_gpa = svm->vmcb->save.rax;
Paolo Bonzini63129752021-03-02 14:40:39 -0500643 ret = kvm_vcpu_map(vcpu, gpa_to_gfn(vmcb12_gpa), &map);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100644 if (ret == -EINVAL) {
Paolo Bonzini63129752021-03-02 14:40:39 -0500645 kvm_inject_gp(vcpu, 0);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100646 return 1;
647 } else if (ret) {
Paolo Bonzini63129752021-03-02 14:40:39 -0500648 return kvm_skip_emulated_instruction(vcpu);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100649 }
650
Paolo Bonzini63129752021-03-02 14:40:39 -0500651 ret = kvm_skip_emulated_instruction(vcpu);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100652
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300653 vmcb12 = map.hva;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100654
Maxim Levitsky2fcf4872020-10-01 14:29:54 +0300655 if (WARN_ON_ONCE(!svm->nested.initialized))
656 return -EINVAL;
657
Paolo Bonzinicb9b6a12021-03-31 07:35:52 -0400658 nested_load_control_from_vmcb12(svm, &vmcb12->control);
659
660 if (!nested_vmcb_valid_sregs(vcpu, &vmcb12->save) ||
Krish Sadhukhanee695f22021-04-12 17:56:08 -0400661 !nested_vmcb_check_controls(vcpu, &svm->nested.ctl)) {
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300662 vmcb12->control.exit_code = SVM_EXIT_ERR;
663 vmcb12->control.exit_code_hi = 0;
664 vmcb12->control.exit_info_1 = 0;
665 vmcb12->control.exit_info_2 = 0;
Paolo Bonzini69c9dfa2020-05-13 12:57:26 -0400666 goto out;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100667 }
668
Joerg Roedel883b0a92020-03-24 10:41:52 +0100669
670 /* Clear internal status */
Paolo Bonzini63129752021-03-02 14:40:39 -0500671 kvm_clear_exception_queue(vcpu);
672 kvm_clear_interrupt_queue(vcpu);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100673
674 /*
Cathy Avery4995a362021-01-13 07:07:52 -0500675 * Since vmcb01 is not in use, we can use it to store some of the L1
676 * state.
Joerg Roedel883b0a92020-03-24 10:41:52 +0100677 */
Paolo Bonzini63129752021-03-02 14:40:39 -0500678 svm->vmcb01.ptr->save.efer = vcpu->arch.efer;
679 svm->vmcb01.ptr->save.cr0 = kvm_read_cr0(vcpu);
680 svm->vmcb01.ptr->save.cr4 = vcpu->arch.cr4;
681 svm->vmcb01.ptr->save.rflags = kvm_get_rflags(vcpu);
682 svm->vmcb01.ptr->save.rip = kvm_rip_read(vcpu);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100683
Cathy Avery4995a362021-01-13 07:07:52 -0500684 if (!npt_enabled)
Paolo Bonzini63129752021-03-02 14:40:39 -0500685 svm->vmcb01.ptr->save.cr3 = kvm_read_cr3(vcpu);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100686
Paolo Bonzinif74f9412020-04-23 13:22:27 -0400687 svm->nested.nested_run_pending = 1;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100688
Paolo Bonzini63129752021-03-02 14:40:39 -0500689 if (enter_svm_guest_mode(vcpu, vmcb12_gpa, vmcb12))
Vitaly Kuznetsov59cd9bc2020-07-10 16:11:52 +0200690 goto out_exit_err;
Vitaly Kuznetsovebdb3db2020-07-10 16:11:51 +0200691
Vitaly Kuznetsov59cd9bc2020-07-10 16:11:52 +0200692 if (nested_svm_vmrun_msrpm(svm))
693 goto out;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100694
Vitaly Kuznetsov59cd9bc2020-07-10 16:11:52 +0200695out_exit_err:
696 svm->nested.nested_run_pending = 0;
697
698 svm->vmcb->control.exit_code = SVM_EXIT_ERR;
699 svm->vmcb->control.exit_code_hi = 0;
700 svm->vmcb->control.exit_info_1 = 0;
701 svm->vmcb->control.exit_info_2 = 0;
702
703 nested_svm_vmexit(svm);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100704
Paolo Bonzini69c9dfa2020-05-13 12:57:26 -0400705out:
Paolo Bonzini63129752021-03-02 14:40:39 -0500706 kvm_vcpu_unmap(vcpu, &map, true);
Paolo Bonzini69c9dfa2020-05-13 12:57:26 -0400707
Joerg Roedel883b0a92020-03-24 10:41:52 +0100708 return ret;
709}
710
Vitaly Kuznetsov0a758292021-06-28 12:44:22 +0200711/* Copy state save area fields which are handled by VMRUN */
Vitaly Kuznetsov2bb16be2021-07-19 11:03:22 +0200712void svm_copy_vmrun_state(struct vmcb_save_area *to_save,
713 struct vmcb_save_area *from_save)
Vitaly Kuznetsov0a758292021-06-28 12:44:22 +0200714{
715 to_save->es = from_save->es;
716 to_save->cs = from_save->cs;
717 to_save->ss = from_save->ss;
718 to_save->ds = from_save->ds;
719 to_save->gdtr = from_save->gdtr;
720 to_save->idtr = from_save->idtr;
721 to_save->rflags = from_save->rflags | X86_EFLAGS_FIXED;
722 to_save->efer = from_save->efer;
723 to_save->cr0 = from_save->cr0;
724 to_save->cr3 = from_save->cr3;
725 to_save->cr4 = from_save->cr4;
726 to_save->rax = from_save->rax;
727 to_save->rsp = from_save->rsp;
728 to_save->rip = from_save->rip;
729 to_save->cpl = 0;
730}
731
Vitaly Kuznetsov2bb16be2021-07-19 11:03:22 +0200732void svm_copy_vmloadsave_state(struct vmcb *to_vmcb, struct vmcb *from_vmcb)
Joerg Roedel883b0a92020-03-24 10:41:52 +0100733{
734 to_vmcb->save.fs = from_vmcb->save.fs;
735 to_vmcb->save.gs = from_vmcb->save.gs;
736 to_vmcb->save.tr = from_vmcb->save.tr;
737 to_vmcb->save.ldtr = from_vmcb->save.ldtr;
738 to_vmcb->save.kernel_gs_base = from_vmcb->save.kernel_gs_base;
739 to_vmcb->save.star = from_vmcb->save.star;
740 to_vmcb->save.lstar = from_vmcb->save.lstar;
741 to_vmcb->save.cstar = from_vmcb->save.cstar;
742 to_vmcb->save.sfmask = from_vmcb->save.sfmask;
743 to_vmcb->save.sysenter_cs = from_vmcb->save.sysenter_cs;
744 to_vmcb->save.sysenter_esp = from_vmcb->save.sysenter_esp;
745 to_vmcb->save.sysenter_eip = from_vmcb->save.sysenter_eip;
746}
747
748int nested_svm_vmexit(struct vcpu_svm *svm)
749{
Paolo Bonzini63129752021-03-02 14:40:39 -0500750 struct kvm_vcpu *vcpu = &svm->vcpu;
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300751 struct vmcb *vmcb12;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100752 struct vmcb *vmcb = svm->vmcb;
753 struct kvm_host_map map;
Paolo Bonzini63129752021-03-02 14:40:39 -0500754 int rc;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100755
Sean Christophersoncb6a32c2021-03-02 09:45:14 -0800756 /* Triple faults in L2 should never escape. */
757 WARN_ON_ONCE(kvm_check_request(KVM_REQ_TRIPLE_FAULT, vcpu));
758
Paolo Bonzini63129752021-03-02 14:40:39 -0500759 rc = kvm_vcpu_map(vcpu, gpa_to_gfn(svm->nested.vmcb12_gpa), &map);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100760 if (rc) {
761 if (rc == -EINVAL)
Paolo Bonzini63129752021-03-02 14:40:39 -0500762 kvm_inject_gp(vcpu, 0);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100763 return 1;
764 }
765
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300766 vmcb12 = map.hva;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100767
768 /* Exit Guest-Mode */
Paolo Bonzini63129752021-03-02 14:40:39 -0500769 leave_guest_mode(vcpu);
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300770 svm->nested.vmcb12_gpa = 0;
Paolo Bonzini2d8a42b2020-05-22 03:50:14 -0400771 WARN_ON_ONCE(svm->nested.nested_run_pending);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100772
Paolo Bonzini63129752021-03-02 14:40:39 -0500773 kvm_clear_request(KVM_REQ_GET_NESTED_STATE_PAGES, vcpu);
Maxim Levitskyf2c7ef32021-01-07 11:38:51 +0200774
Paolo Bonzini38c0b192020-04-23 13:13:09 -0400775 /* in case we halted in L2 */
776 svm->vcpu.arch.mp_state = KVM_MP_STATE_RUNNABLE;
777
Joerg Roedel883b0a92020-03-24 10:41:52 +0100778 /* Give the current vmcb to the guest */
Joerg Roedel883b0a92020-03-24 10:41:52 +0100779
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300780 vmcb12->save.es = vmcb->save.es;
781 vmcb12->save.cs = vmcb->save.cs;
782 vmcb12->save.ss = vmcb->save.ss;
783 vmcb12->save.ds = vmcb->save.ds;
784 vmcb12->save.gdtr = vmcb->save.gdtr;
785 vmcb12->save.idtr = vmcb->save.idtr;
786 vmcb12->save.efer = svm->vcpu.arch.efer;
Paolo Bonzini63129752021-03-02 14:40:39 -0500787 vmcb12->save.cr0 = kvm_read_cr0(vcpu);
788 vmcb12->save.cr3 = kvm_read_cr3(vcpu);
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300789 vmcb12->save.cr2 = vmcb->save.cr2;
790 vmcb12->save.cr4 = svm->vcpu.arch.cr4;
Paolo Bonzini63129752021-03-02 14:40:39 -0500791 vmcb12->save.rflags = kvm_get_rflags(vcpu);
792 vmcb12->save.rip = kvm_rip_read(vcpu);
793 vmcb12->save.rsp = kvm_rsp_read(vcpu);
794 vmcb12->save.rax = kvm_rax_read(vcpu);
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300795 vmcb12->save.dr7 = vmcb->save.dr7;
796 vmcb12->save.dr6 = svm->vcpu.arch.dr6;
797 vmcb12->save.cpl = vmcb->save.cpl;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100798
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300799 vmcb12->control.int_state = vmcb->control.int_state;
800 vmcb12->control.exit_code = vmcb->control.exit_code;
801 vmcb12->control.exit_code_hi = vmcb->control.exit_code_hi;
802 vmcb12->control.exit_info_1 = vmcb->control.exit_info_1;
803 vmcb12->control.exit_info_2 = vmcb->control.exit_info_2;
Paolo Bonzini36e2e982020-05-22 06:04:57 -0400804
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300805 if (vmcb12->control.exit_code != SVM_EXIT_ERR)
Paolo Bonzini9e8f0fb2020-11-17 05:15:41 -0500806 nested_save_pending_event_to_vmcb12(svm, vmcb12);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100807
808 if (svm->nrips_enabled)
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300809 vmcb12->control.next_rip = vmcb->control.next_rip;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100810
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300811 vmcb12->control.int_ctl = svm->nested.ctl.int_ctl;
812 vmcb12->control.tlb_ctl = svm->nested.ctl.tlb_ctl;
813 vmcb12->control.event_inj = svm->nested.ctl.event_inj;
814 vmcb12->control.event_inj_err = svm->nested.ctl.event_inj_err;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100815
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300816 vmcb12->control.pause_filter_count =
Joerg Roedel883b0a92020-03-24 10:41:52 +0100817 svm->vmcb->control.pause_filter_count;
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300818 vmcb12->control.pause_filter_thresh =
Joerg Roedel883b0a92020-03-24 10:41:52 +0100819 svm->vmcb->control.pause_filter_thresh;
820
Babu Mogerd00b99c2021-02-17 10:56:04 -0500821 nested_svm_copy_common_state(svm->nested.vmcb02.ptr, svm->vmcb01.ptr);
822
Cathy Avery4995a362021-01-13 07:07:52 -0500823 svm_switch_vmcb(svm, &svm->vmcb01);
824
825 /*
826 * On vmexit the GIF is set to false and
827 * no event can be injected in L1.
828 */
Maxim Levitsky98837642020-08-27 19:27:18 +0300829 svm_set_gif(svm, false);
Cathy Avery4995a362021-01-13 07:07:52 -0500830 svm->vmcb->control.exit_int_info = 0;
Maxim Levitsky98837642020-08-27 19:27:18 +0300831
Paolo Bonzini7ca62d12020-11-16 06:38:19 -0500832 svm->vcpu.arch.tsc_offset = svm->vcpu.arch.l1_tsc_offset;
833 if (svm->vmcb->control.tsc_offset != svm->vcpu.arch.tsc_offset) {
834 svm->vmcb->control.tsc_offset = svm->vcpu.arch.tsc_offset;
835 vmcb_mark_dirty(svm->vmcb, VMCB_INTERCEPTS);
836 }
Paolo Bonzini18fc6c52020-05-18 11:07:08 -0400837
Paolo Bonzinie670bf62020-05-13 13:16:12 -0400838 svm->nested.ctl.nested_cr3 = 0;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100839
Cathy Avery4995a362021-01-13 07:07:52 -0500840 /*
841 * Restore processor state that had been saved in vmcb01
842 */
Paolo Bonzini63129752021-03-02 14:40:39 -0500843 kvm_set_rflags(vcpu, svm->vmcb->save.rflags);
844 svm_set_efer(vcpu, svm->vmcb->save.efer);
845 svm_set_cr0(vcpu, svm->vmcb->save.cr0 | X86_CR0_PE);
846 svm_set_cr4(vcpu, svm->vmcb->save.cr4);
847 kvm_rax_write(vcpu, svm->vmcb->save.rax);
848 kvm_rsp_write(vcpu, svm->vmcb->save.rsp);
849 kvm_rip_write(vcpu, svm->vmcb->save.rip);
Cathy Avery4995a362021-01-13 07:07:52 -0500850
851 svm->vcpu.arch.dr7 = DR7_FIXED_1;
852 kvm_update_dr7(&svm->vcpu);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100853
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300854 trace_kvm_nested_vmexit_inject(vmcb12->control.exit_code,
855 vmcb12->control.exit_info_1,
856 vmcb12->control.exit_info_2,
857 vmcb12->control.exit_int_info,
858 vmcb12->control.exit_int_info_err,
Paolo Bonzini36e2e982020-05-22 06:04:57 -0400859 KVM_ISA_SVM);
860
Paolo Bonzini63129752021-03-02 14:40:39 -0500861 kvm_vcpu_unmap(vcpu, &map, true);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100862
Sean Christophersond2e56012021-06-09 16:42:26 -0700863 nested_svm_transition_tlb_flush(vcpu);
864
Paolo Bonzini63129752021-03-02 14:40:39 -0500865 nested_svm_uninit_mmu_context(vcpu);
Vitaly Kuznetsovbf7dea42020-07-10 16:11:54 +0200866
Maxim Levitskyb222b0b2021-06-07 12:01:59 +0300867 rc = nested_svm_load_cr3(vcpu, svm->vmcb->save.cr3, false, true);
Vitaly Kuznetsovd82aaef2020-07-10 16:11:56 +0200868 if (rc)
869 return 1;
Vitaly Kuznetsovbf7dea42020-07-10 16:11:54 +0200870
Joerg Roedel883b0a92020-03-24 10:41:52 +0100871 /*
872 * Drop what we picked up for L2 via svm_complete_interrupts() so it
873 * doesn't end up in L1.
874 */
875 svm->vcpu.arch.nmi_injected = false;
Paolo Bonzini63129752021-03-02 14:40:39 -0500876 kvm_clear_exception_queue(vcpu);
877 kvm_clear_interrupt_queue(vcpu);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100878
Krish Sadhukhan9a7de6e2021-03-23 13:50:03 -0400879 /*
880 * If we are here following the completion of a VMRUN that
881 * is being single-stepped, queue the pending #DB intercept
882 * right now so that it an be accounted for before we execute
883 * L1's next instruction.
884 */
885 if (unlikely(svm->vmcb->save.rflags & X86_EFLAGS_TF))
886 kvm_queue_exception(&(svm->vcpu), DB_VECTOR);
887
Joerg Roedel883b0a92020-03-24 10:41:52 +0100888 return 0;
889}
890
Sean Christophersoncb6a32c2021-03-02 09:45:14 -0800891static void nested_svm_triple_fault(struct kvm_vcpu *vcpu)
892{
Sean Christopherson3a87c7e2021-03-02 09:45:15 -0800893 nested_svm_simple_vmexit(to_svm(vcpu), SVM_EXIT_SHUTDOWN);
Sean Christophersoncb6a32c2021-03-02 09:45:14 -0800894}
895
Maxim Levitsky2fcf4872020-10-01 14:29:54 +0300896int svm_allocate_nested(struct vcpu_svm *svm)
897{
Cathy Avery4995a362021-01-13 07:07:52 -0500898 struct page *vmcb02_page;
Maxim Levitsky2fcf4872020-10-01 14:29:54 +0300899
900 if (svm->nested.initialized)
901 return 0;
902
Cathy Avery4995a362021-01-13 07:07:52 -0500903 vmcb02_page = alloc_page(GFP_KERNEL_ACCOUNT | __GFP_ZERO);
904 if (!vmcb02_page)
Maxim Levitsky2fcf4872020-10-01 14:29:54 +0300905 return -ENOMEM;
Cathy Avery4995a362021-01-13 07:07:52 -0500906 svm->nested.vmcb02.ptr = page_address(vmcb02_page);
907 svm->nested.vmcb02.pa = __sme_set(page_to_pfn(vmcb02_page) << PAGE_SHIFT);
Maxim Levitsky2fcf4872020-10-01 14:29:54 +0300908
909 svm->nested.msrpm = svm_vcpu_alloc_msrpm();
910 if (!svm->nested.msrpm)
Cathy Avery4995a362021-01-13 07:07:52 -0500911 goto err_free_vmcb02;
Maxim Levitsky2fcf4872020-10-01 14:29:54 +0300912 svm_vcpu_init_msrpm(&svm->vcpu, svm->nested.msrpm);
913
914 svm->nested.initialized = true;
915 return 0;
916
Cathy Avery4995a362021-01-13 07:07:52 -0500917err_free_vmcb02:
918 __free_page(vmcb02_page);
Maxim Levitsky2fcf4872020-10-01 14:29:54 +0300919 return -ENOMEM;
920}
921
922void svm_free_nested(struct vcpu_svm *svm)
923{
924 if (!svm->nested.initialized)
925 return;
926
927 svm_vcpu_free_msrpm(svm->nested.msrpm);
928 svm->nested.msrpm = NULL;
929
Cathy Avery4995a362021-01-13 07:07:52 -0500930 __free_page(virt_to_page(svm->nested.vmcb02.ptr));
931 svm->nested.vmcb02.ptr = NULL;
Maxim Levitsky2fcf4872020-10-01 14:29:54 +0300932
Maxim Levitskyc74ad082021-05-03 15:54:43 +0300933 /*
934 * When last_vmcb12_gpa matches the current vmcb12 gpa,
935 * some vmcb12 fields are not loaded if they are marked clean
936 * in the vmcb12, since in this case they are up to date already.
937 *
938 * When the vmcb02 is freed, this optimization becomes invalid.
939 */
940 svm->nested.last_vmcb12_gpa = INVALID_GPA;
941
Maxim Levitsky2fcf4872020-10-01 14:29:54 +0300942 svm->nested.initialized = false;
943}
944
Paolo Bonzinic513f482020-05-18 13:08:37 -0400945/*
946 * Forcibly leave nested mode in order to be able to reset the VCPU later on.
947 */
948void svm_leave_nested(struct vcpu_svm *svm)
949{
Paolo Bonzini63129752021-03-02 14:40:39 -0500950 struct kvm_vcpu *vcpu = &svm->vcpu;
951
952 if (is_guest_mode(vcpu)) {
Paolo Bonzinic513f482020-05-18 13:08:37 -0400953 svm->nested.nested_run_pending = 0;
Maxim Levitskyc74ad082021-05-03 15:54:43 +0300954 svm->nested.vmcb12_gpa = INVALID_GPA;
955
Paolo Bonzini63129752021-03-02 14:40:39 -0500956 leave_guest_mode(vcpu);
Cathy Avery4995a362021-01-13 07:07:52 -0500957
Maxim Levitskydeee59b2021-05-03 15:54:42 +0300958 svm_switch_vmcb(svm, &svm->vmcb01);
Cathy Avery4995a362021-01-13 07:07:52 -0500959
Paolo Bonzini63129752021-03-02 14:40:39 -0500960 nested_svm_uninit_mmu_context(vcpu);
Maxim Levitsky56fe28d2021-01-07 11:38:54 +0200961 vmcb_mark_all_dirty(svm->vmcb);
Paolo Bonzinic513f482020-05-18 13:08:37 -0400962 }
Paolo Bonzinia7d5c7c2020-09-22 07:43:14 -0400963
Paolo Bonzini63129752021-03-02 14:40:39 -0500964 kvm_clear_request(KVM_REQ_GET_NESTED_STATE_PAGES, vcpu);
Paolo Bonzinic513f482020-05-18 13:08:37 -0400965}
966
Joerg Roedel883b0a92020-03-24 10:41:52 +0100967static int nested_svm_exit_handled_msr(struct vcpu_svm *svm)
968{
969 u32 offset, msr, value;
970 int write, mask;
971
Babu Mogerc62e2e92020-09-11 14:28:28 -0500972 if (!(vmcb_is_intercept(&svm->nested.ctl, INTERCEPT_MSR_PROT)))
Joerg Roedel883b0a92020-03-24 10:41:52 +0100973 return NESTED_EXIT_HOST;
974
975 msr = svm->vcpu.arch.regs[VCPU_REGS_RCX];
976 offset = svm_msrpm_offset(msr);
977 write = svm->vmcb->control.exit_info_1 & 1;
978 mask = 1 << ((2 * (msr & 0xf)) + write);
979
980 if (offset == MSR_INVALID)
981 return NESTED_EXIT_DONE;
982
983 /* Offset is in 32 bit units but need in 8 bit units */
984 offset *= 4;
985
Paolo Bonzinie670bf62020-05-13 13:16:12 -0400986 if (kvm_vcpu_read_guest(&svm->vcpu, svm->nested.ctl.msrpm_base_pa + offset, &value, 4))
Joerg Roedel883b0a92020-03-24 10:41:52 +0100987 return NESTED_EXIT_DONE;
988
989 return (value & mask) ? NESTED_EXIT_DONE : NESTED_EXIT_HOST;
990}
991
Joerg Roedel883b0a92020-03-24 10:41:52 +0100992static int nested_svm_intercept_ioio(struct vcpu_svm *svm)
993{
994 unsigned port, size, iopm_len;
995 u16 val, mask;
996 u8 start_bit;
997 u64 gpa;
998
Babu Mogerc62e2e92020-09-11 14:28:28 -0500999 if (!(vmcb_is_intercept(&svm->nested.ctl, INTERCEPT_IOIO_PROT)))
Joerg Roedel883b0a92020-03-24 10:41:52 +01001000 return NESTED_EXIT_HOST;
1001
1002 port = svm->vmcb->control.exit_info_1 >> 16;
1003 size = (svm->vmcb->control.exit_info_1 & SVM_IOIO_SIZE_MASK) >>
1004 SVM_IOIO_SIZE_SHIFT;
Paolo Bonzinie670bf62020-05-13 13:16:12 -04001005 gpa = svm->nested.ctl.iopm_base_pa + (port / 8);
Joerg Roedel883b0a92020-03-24 10:41:52 +01001006 start_bit = port % 8;
1007 iopm_len = (start_bit + size > 8) ? 2 : 1;
1008 mask = (0xf >> (4 - size)) << start_bit;
1009 val = 0;
1010
1011 if (kvm_vcpu_read_guest(&svm->vcpu, gpa, &val, iopm_len))
1012 return NESTED_EXIT_DONE;
1013
1014 return (val & mask) ? NESTED_EXIT_DONE : NESTED_EXIT_HOST;
1015}
1016
1017static int nested_svm_intercept(struct vcpu_svm *svm)
1018{
1019 u32 exit_code = svm->vmcb->control.exit_code;
1020 int vmexit = NESTED_EXIT_HOST;
1021
1022 switch (exit_code) {
1023 case SVM_EXIT_MSR:
1024 vmexit = nested_svm_exit_handled_msr(svm);
1025 break;
1026 case SVM_EXIT_IOIO:
1027 vmexit = nested_svm_intercept_ioio(svm);
1028 break;
1029 case SVM_EXIT_READ_CR0 ... SVM_EXIT_WRITE_CR8: {
Babu Moger03bfeeb2020-09-11 14:28:05 -05001030 if (vmcb_is_intercept(&svm->nested.ctl, exit_code))
Joerg Roedel883b0a92020-03-24 10:41:52 +01001031 vmexit = NESTED_EXIT_DONE;
1032 break;
1033 }
1034 case SVM_EXIT_READ_DR0 ... SVM_EXIT_WRITE_DR7: {
Babu Moger30abaa882020-09-11 14:28:12 -05001035 if (vmcb_is_intercept(&svm->nested.ctl, exit_code))
Joerg Roedel883b0a92020-03-24 10:41:52 +01001036 vmexit = NESTED_EXIT_DONE;
1037 break;
1038 }
1039 case SVM_EXIT_EXCP_BASE ... SVM_EXIT_EXCP_BASE + 0x1f: {
Paolo Bonzini7c866632020-05-16 08:42:28 -04001040 /*
1041 * Host-intercepted exceptions have been checked already in
1042 * nested_svm_exit_special. There is nothing to do here,
1043 * the vmexit is injected by svm_check_nested_events.
1044 */
1045 vmexit = NESTED_EXIT_DONE;
Joerg Roedel883b0a92020-03-24 10:41:52 +01001046 break;
1047 }
1048 case SVM_EXIT_ERR: {
1049 vmexit = NESTED_EXIT_DONE;
1050 break;
1051 }
1052 default: {
Babu Mogerc62e2e92020-09-11 14:28:28 -05001053 if (vmcb_is_intercept(&svm->nested.ctl, exit_code))
Joerg Roedel883b0a92020-03-24 10:41:52 +01001054 vmexit = NESTED_EXIT_DONE;
1055 }
1056 }
1057
1058 return vmexit;
1059}
1060
1061int nested_svm_exit_handled(struct vcpu_svm *svm)
1062{
1063 int vmexit;
1064
1065 vmexit = nested_svm_intercept(svm);
1066
1067 if (vmexit == NESTED_EXIT_DONE)
1068 nested_svm_vmexit(svm);
1069
1070 return vmexit;
1071}
1072
Paolo Bonzini63129752021-03-02 14:40:39 -05001073int nested_svm_check_permissions(struct kvm_vcpu *vcpu)
Joerg Roedel883b0a92020-03-24 10:41:52 +01001074{
Paolo Bonzini63129752021-03-02 14:40:39 -05001075 if (!(vcpu->arch.efer & EFER_SVME) || !is_paging(vcpu)) {
1076 kvm_queue_exception(vcpu, UD_VECTOR);
Joerg Roedel883b0a92020-03-24 10:41:52 +01001077 return 1;
1078 }
1079
Paolo Bonzini63129752021-03-02 14:40:39 -05001080 if (to_svm(vcpu)->vmcb->save.cpl) {
1081 kvm_inject_gp(vcpu, 0);
Joerg Roedel883b0a92020-03-24 10:41:52 +01001082 return 1;
1083 }
1084
1085 return 0;
1086}
1087
Paolo Bonzini7c866632020-05-16 08:42:28 -04001088static bool nested_exit_on_exception(struct vcpu_svm *svm)
Joerg Roedel883b0a92020-03-24 10:41:52 +01001089{
Paolo Bonzini7c866632020-05-16 08:42:28 -04001090 unsigned int nr = svm->vcpu.arch.exception.nr;
Joerg Roedel883b0a92020-03-24 10:41:52 +01001091
Babu Moger9780d512020-09-11 14:28:20 -05001092 return (svm->nested.ctl.intercepts[INTERCEPT_EXCEPTION] & BIT(nr));
Paolo Bonzini7c866632020-05-16 08:42:28 -04001093}
Joerg Roedel883b0a92020-03-24 10:41:52 +01001094
Paolo Bonzini7c866632020-05-16 08:42:28 -04001095static void nested_svm_inject_exception_vmexit(struct vcpu_svm *svm)
1096{
1097 unsigned int nr = svm->vcpu.arch.exception.nr;
Joerg Roedel883b0a92020-03-24 10:41:52 +01001098
1099 svm->vmcb->control.exit_code = SVM_EXIT_EXCP_BASE + nr;
1100 svm->vmcb->control.exit_code_hi = 0;
Paolo Bonzini7c866632020-05-16 08:42:28 -04001101
1102 if (svm->vcpu.arch.exception.has_error_code)
1103 svm->vmcb->control.exit_info_1 = svm->vcpu.arch.exception.error_code;
Joerg Roedel883b0a92020-03-24 10:41:52 +01001104
1105 /*
1106 * EXITINFO2 is undefined for all exception intercepts other
1107 * than #PF.
1108 */
Paolo Bonzini7c866632020-05-16 08:42:28 -04001109 if (nr == PF_VECTOR) {
1110 if (svm->vcpu.arch.exception.nested_apf)
1111 svm->vmcb->control.exit_info_2 = svm->vcpu.arch.apf.nested_apf_token;
1112 else if (svm->vcpu.arch.exception.has_payload)
1113 svm->vmcb->control.exit_info_2 = svm->vcpu.arch.exception.payload;
1114 else
1115 svm->vmcb->control.exit_info_2 = svm->vcpu.arch.cr2;
1116 } else if (nr == DB_VECTOR) {
1117 /* See inject_pending_event. */
1118 kvm_deliver_exception_payload(&svm->vcpu);
1119 if (svm->vcpu.arch.dr7 & DR7_GD) {
1120 svm->vcpu.arch.dr7 &= ~DR7_GD;
1121 kvm_update_dr7(&svm->vcpu);
1122 }
1123 } else
1124 WARN_ON(svm->vcpu.arch.exception.has_payload);
Joerg Roedel883b0a92020-03-24 10:41:52 +01001125
Paolo Bonzini7c866632020-05-16 08:42:28 -04001126 nested_svm_vmexit(svm);
Joerg Roedel883b0a92020-03-24 10:41:52 +01001127}
1128
Paolo Bonzini5b6724082020-05-16 08:50:35 -04001129static inline bool nested_exit_on_init(struct vcpu_svm *svm)
1130{
Babu Mogerc62e2e92020-09-11 14:28:28 -05001131 return vmcb_is_intercept(&svm->nested.ctl, INTERCEPT_INIT);
Paolo Bonzini5b6724082020-05-16 08:50:35 -04001132}
1133
Paolo Bonzini33b22172020-04-17 10:24:18 -04001134static int svm_check_nested_events(struct kvm_vcpu *vcpu)
Joerg Roedel883b0a92020-03-24 10:41:52 +01001135{
1136 struct vcpu_svm *svm = to_svm(vcpu);
1137 bool block_nested_events =
Paolo Bonzinibd279622020-05-16 08:46:00 -04001138 kvm_event_needs_reinjection(vcpu) || svm->nested.nested_run_pending;
Paolo Bonzini5b6724082020-05-16 08:50:35 -04001139 struct kvm_lapic *apic = vcpu->arch.apic;
1140
1141 if (lapic_in_kernel(vcpu) &&
1142 test_bit(KVM_APIC_INIT, &apic->pending_events)) {
1143 if (block_nested_events)
1144 return -EBUSY;
1145 if (!nested_exit_on_init(svm))
1146 return 0;
Sean Christopherson3a87c7e2021-03-02 09:45:15 -08001147 nested_svm_simple_vmexit(svm, SVM_EXIT_INIT);
Paolo Bonzini5b6724082020-05-16 08:50:35 -04001148 return 0;
1149 }
Joerg Roedel883b0a92020-03-24 10:41:52 +01001150
Paolo Bonzini7c866632020-05-16 08:42:28 -04001151 if (vcpu->arch.exception.pending) {
Maxim Levitsky4020da32021-04-01 17:38:14 +03001152 /*
1153 * Only a pending nested run can block a pending exception.
1154 * Otherwise an injected NMI/interrupt should either be
1155 * lost or delivered to the nested hypervisor in the EXITINTINFO
1156 * vmcb field, while delivering the pending exception.
1157 */
1158 if (svm->nested.nested_run_pending)
Paolo Bonzini7c866632020-05-16 08:42:28 -04001159 return -EBUSY;
1160 if (!nested_exit_on_exception(svm))
1161 return 0;
1162 nested_svm_inject_exception_vmexit(svm);
1163 return 0;
1164 }
1165
Paolo Bonzini221e7612020-04-23 08:13:10 -04001166 if (vcpu->arch.smi_pending && !svm_smi_blocked(vcpu)) {
Paolo Bonzini55714cd2020-04-23 08:17:28 -04001167 if (block_nested_events)
1168 return -EBUSY;
Paolo Bonzini221e7612020-04-23 08:13:10 -04001169 if (!nested_exit_on_smi(svm))
1170 return 0;
Sean Christopherson3a87c7e2021-03-02 09:45:15 -08001171 nested_svm_simple_vmexit(svm, SVM_EXIT_SMI);
Paolo Bonzini55714cd2020-04-23 08:17:28 -04001172 return 0;
1173 }
1174
Paolo Bonzini221e7612020-04-23 08:13:10 -04001175 if (vcpu->arch.nmi_pending && !svm_nmi_blocked(vcpu)) {
Cathy Avery9c3d3702020-04-14 16:11:06 -04001176 if (block_nested_events)
1177 return -EBUSY;
Paolo Bonzini221e7612020-04-23 08:13:10 -04001178 if (!nested_exit_on_nmi(svm))
1179 return 0;
Sean Christopherson3a87c7e2021-03-02 09:45:15 -08001180 nested_svm_simple_vmexit(svm, SVM_EXIT_NMI);
Cathy Avery9c3d3702020-04-14 16:11:06 -04001181 return 0;
1182 }
1183
Paolo Bonzini221e7612020-04-23 08:13:10 -04001184 if (kvm_cpu_has_interrupt(vcpu) && !svm_interrupt_blocked(vcpu)) {
Joerg Roedel883b0a92020-03-24 10:41:52 +01001185 if (block_nested_events)
1186 return -EBUSY;
Paolo Bonzini221e7612020-04-23 08:13:10 -04001187 if (!nested_exit_on_intr(svm))
1188 return 0;
Sean Christopherson3a87c7e2021-03-02 09:45:15 -08001189 trace_kvm_nested_intr_vmexit(svm->vmcb->save.rip);
1190 nested_svm_simple_vmexit(svm, SVM_EXIT_INTR);
Joerg Roedel883b0a92020-03-24 10:41:52 +01001191 return 0;
1192 }
1193
1194 return 0;
1195}
1196
1197int nested_svm_exit_special(struct vcpu_svm *svm)
1198{
1199 u32 exit_code = svm->vmcb->control.exit_code;
1200
1201 switch (exit_code) {
1202 case SVM_EXIT_INTR:
1203 case SVM_EXIT_NMI:
Joerg Roedel883b0a92020-03-24 10:41:52 +01001204 case SVM_EXIT_NPF:
Paolo Bonzini7c866632020-05-16 08:42:28 -04001205 return NESTED_EXIT_HOST;
1206 case SVM_EXIT_EXCP_BASE ... SVM_EXIT_EXCP_BASE + 0x1f: {
1207 u32 excp_bits = 1 << (exit_code - SVM_EXIT_EXCP_BASE);
1208
Cathy Avery4995a362021-01-13 07:07:52 -05001209 if (svm->vmcb01.ptr->control.intercepts[INTERCEPT_EXCEPTION] &
1210 excp_bits)
Paolo Bonzini7c866632020-05-16 08:42:28 -04001211 return NESTED_EXIT_HOST;
1212 else if (exit_code == SVM_EXIT_EXCP_BASE + PF_VECTOR &&
Vitaly Kuznetsov68fd66f2020-05-25 16:41:17 +02001213 svm->vcpu.arch.apf.host_apf_flags)
Paolo Bonzini7c866632020-05-16 08:42:28 -04001214 /* Trap async PF even if not shadowing */
Joerg Roedel883b0a92020-03-24 10:41:52 +01001215 return NESTED_EXIT_HOST;
1216 break;
Paolo Bonzini7c866632020-05-16 08:42:28 -04001217 }
Joerg Roedel883b0a92020-03-24 10:41:52 +01001218 default:
1219 break;
1220 }
1221
1222 return NESTED_EXIT_CONTINUE;
1223}
Paolo Bonzini33b22172020-04-17 10:24:18 -04001224
Paolo Bonzinicc440cd2020-05-13 13:36:32 -04001225static int svm_get_nested_state(struct kvm_vcpu *vcpu,
1226 struct kvm_nested_state __user *user_kvm_nested_state,
1227 u32 user_data_size)
1228{
1229 struct vcpu_svm *svm;
1230 struct kvm_nested_state kvm_state = {
1231 .flags = 0,
1232 .format = KVM_STATE_NESTED_FORMAT_SVM,
1233 .size = sizeof(kvm_state),
1234 };
1235 struct vmcb __user *user_vmcb = (struct vmcb __user *)
1236 &user_kvm_nested_state->data.svm[0];
1237
1238 if (!vcpu)
1239 return kvm_state.size + KVM_STATE_NESTED_SVM_VMCB_SIZE;
1240
1241 svm = to_svm(vcpu);
1242
1243 if (user_data_size < kvm_state.size)
1244 goto out;
1245
1246 /* First fill in the header and copy it out. */
1247 if (is_guest_mode(vcpu)) {
Maxim Levitsky0dd16b52020-08-27 20:11:39 +03001248 kvm_state.hdr.svm.vmcb_pa = svm->nested.vmcb12_gpa;
Paolo Bonzinicc440cd2020-05-13 13:36:32 -04001249 kvm_state.size += KVM_STATE_NESTED_SVM_VMCB_SIZE;
1250 kvm_state.flags |= KVM_STATE_NESTED_GUEST_MODE;
1251
1252 if (svm->nested.nested_run_pending)
1253 kvm_state.flags |= KVM_STATE_NESTED_RUN_PENDING;
1254 }
1255
1256 if (gif_set(svm))
1257 kvm_state.flags |= KVM_STATE_NESTED_GIF_SET;
1258
1259 if (copy_to_user(user_kvm_nested_state, &kvm_state, sizeof(kvm_state)))
1260 return -EFAULT;
1261
1262 if (!is_guest_mode(vcpu))
1263 goto out;
1264
1265 /*
1266 * Copy over the full size of the VMCB rather than just the size
1267 * of the structs.
1268 */
1269 if (clear_user(user_vmcb, KVM_STATE_NESTED_SVM_VMCB_SIZE))
1270 return -EFAULT;
1271 if (copy_to_user(&user_vmcb->control, &svm->nested.ctl,
1272 sizeof(user_vmcb->control)))
1273 return -EFAULT;
Cathy Avery4995a362021-01-13 07:07:52 -05001274 if (copy_to_user(&user_vmcb->save, &svm->vmcb01.ptr->save,
Paolo Bonzinicc440cd2020-05-13 13:36:32 -04001275 sizeof(user_vmcb->save)))
1276 return -EFAULT;
Paolo Bonzinicc440cd2020-05-13 13:36:32 -04001277out:
1278 return kvm_state.size;
1279}
1280
1281static int svm_set_nested_state(struct kvm_vcpu *vcpu,
1282 struct kvm_nested_state __user *user_kvm_nested_state,
1283 struct kvm_nested_state *kvm_state)
1284{
1285 struct vcpu_svm *svm = to_svm(vcpu);
Paolo Bonzinicc440cd2020-05-13 13:36:32 -04001286 struct vmcb __user *user_vmcb = (struct vmcb __user *)
1287 &user_kvm_nested_state->data.svm[0];
Joerg Roedel6ccbd292020-09-07 15:15:02 +02001288 struct vmcb_control_area *ctl;
1289 struct vmcb_save_area *save;
Sean Christophersondbc47392021-06-22 10:56:59 -07001290 unsigned long cr0;
Joerg Roedel6ccbd292020-09-07 15:15:02 +02001291 int ret;
Paolo Bonzinicc440cd2020-05-13 13:36:32 -04001292
Joerg Roedel6ccbd292020-09-07 15:15:02 +02001293 BUILD_BUG_ON(sizeof(struct vmcb_control_area) + sizeof(struct vmcb_save_area) >
1294 KVM_STATE_NESTED_SVM_VMCB_SIZE);
1295
Paolo Bonzinicc440cd2020-05-13 13:36:32 -04001296 if (kvm_state->format != KVM_STATE_NESTED_FORMAT_SVM)
1297 return -EINVAL;
1298
1299 if (kvm_state->flags & ~(KVM_STATE_NESTED_GUEST_MODE |
1300 KVM_STATE_NESTED_RUN_PENDING |
1301 KVM_STATE_NESTED_GIF_SET))
1302 return -EINVAL;
1303
1304 /*
1305 * If in guest mode, vcpu->arch.efer actually refers to the L2 guest's
1306 * EFER.SVME, but EFER.SVME still has to be 1 for VMRUN to succeed.
1307 */
1308 if (!(vcpu->arch.efer & EFER_SVME)) {
1309 /* GIF=1 and no guest mode are required if SVME=0. */
1310 if (kvm_state->flags != KVM_STATE_NESTED_GIF_SET)
1311 return -EINVAL;
1312 }
1313
1314 /* SMM temporarily disables SVM, so we cannot be in guest mode. */
1315 if (is_smm(vcpu) && (kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE))
1316 return -EINVAL;
1317
1318 if (!(kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE)) {
1319 svm_leave_nested(svm);
Vitaly Kuznetsovd5cd6f32020-09-14 15:37:25 +02001320 svm_set_gif(svm, !!(kvm_state->flags & KVM_STATE_NESTED_GIF_SET));
1321 return 0;
Paolo Bonzinicc440cd2020-05-13 13:36:32 -04001322 }
1323
1324 if (!page_address_valid(vcpu, kvm_state->hdr.svm.vmcb_pa))
1325 return -EINVAL;
1326 if (kvm_state->size < sizeof(*kvm_state) + KVM_STATE_NESTED_SVM_VMCB_SIZE)
1327 return -EINVAL;
Paolo Bonzinicc440cd2020-05-13 13:36:32 -04001328
Joerg Roedel6ccbd292020-09-07 15:15:02 +02001329 ret = -ENOMEM;
Sean Christophersoneba04b22021-03-30 19:30:25 -07001330 ctl = kzalloc(sizeof(*ctl), GFP_KERNEL_ACCOUNT);
1331 save = kzalloc(sizeof(*save), GFP_KERNEL_ACCOUNT);
Joerg Roedel6ccbd292020-09-07 15:15:02 +02001332 if (!ctl || !save)
1333 goto out_free;
1334
1335 ret = -EFAULT;
1336 if (copy_from_user(ctl, &user_vmcb->control, sizeof(*ctl)))
1337 goto out_free;
1338 if (copy_from_user(save, &user_vmcb->save, sizeof(*save)))
1339 goto out_free;
1340
1341 ret = -EINVAL;
Krish Sadhukhanee695f22021-04-12 17:56:08 -04001342 if (!nested_vmcb_check_controls(vcpu, ctl))
Joerg Roedel6ccbd292020-09-07 15:15:02 +02001343 goto out_free;
Paolo Bonzinicc440cd2020-05-13 13:36:32 -04001344
1345 /*
1346 * Processor state contains L2 state. Check that it is
Paolo Bonzinicb9b6a12021-03-31 07:35:52 -04001347 * valid for guest mode (see nested_vmcb_check_save).
Paolo Bonzinicc440cd2020-05-13 13:36:32 -04001348 */
1349 cr0 = kvm_read_cr0(vcpu);
1350 if (((cr0 & X86_CR0_CD) == 0) && (cr0 & X86_CR0_NW))
Joerg Roedel6ccbd292020-09-07 15:15:02 +02001351 goto out_free;
Paolo Bonzinicc440cd2020-05-13 13:36:32 -04001352
1353 /*
1354 * Validate host state saved from before VMRUN (see
1355 * nested_svm_check_permissions).
Paolo Bonzinicc440cd2020-05-13 13:36:32 -04001356 */
Krish Sadhukhan6906e062020-10-06 19:06:52 +00001357 if (!(save->cr0 & X86_CR0_PG) ||
1358 !(save->cr0 & X86_CR0_PE) ||
1359 (save->rflags & X86_EFLAGS_VM) ||
Paolo Bonzini63129752021-03-02 14:40:39 -05001360 !nested_vmcb_valid_sregs(vcpu, save))
Joerg Roedel6ccbd292020-09-07 15:15:02 +02001361 goto out_free;
Paolo Bonzinicc440cd2020-05-13 13:36:32 -04001362
1363 /*
Maxim Levitskyb222b0b2021-06-07 12:01:59 +03001364 * While the nested guest CR3 is already checked and set by
1365 * KVM_SET_SREGS, it was set when nested state was yet loaded,
1366 * thus MMU might not be initialized correctly.
1367 * Set it again to fix this.
1368 */
1369
1370 ret = nested_svm_load_cr3(&svm->vcpu, vcpu->arch.cr3,
1371 nested_npt_enabled(svm), false);
1372 if (WARN_ON_ONCE(ret))
1373 goto out_free;
1374
1375
1376 /*
Cathy Avery4995a362021-01-13 07:07:52 -05001377 * All checks done, we can enter guest mode. Userspace provides
1378 * vmcb12.control, which will be combined with L1 and stored into
1379 * vmcb02, and the L1 save state which we store in vmcb01.
1380 * L2 registers if needed are moved from the current VMCB to VMCB02.
Paolo Bonzinicc440cd2020-05-13 13:36:32 -04001381 */
Maxim Levitsky81f76ad2021-01-07 11:38:52 +02001382
Maxim Levitsky9d290e12021-05-03 15:54:44 +03001383 if (is_guest_mode(vcpu))
1384 svm_leave_nested(svm);
1385 else
1386 svm->nested.vmcb02.ptr->save = svm->vmcb01.ptr->save;
1387
Maxim Levitsky063ab162021-05-04 17:39:35 +03001388 svm_set_gif(svm, !!(kvm_state->flags & KVM_STATE_NESTED_GIF_SET));
1389
Maxim Levitsky81f76ad2021-01-07 11:38:52 +02001390 svm->nested.nested_run_pending =
1391 !!(kvm_state->flags & KVM_STATE_NESTED_RUN_PENDING);
1392
Maxim Levitsky0dd16b52020-08-27 20:11:39 +03001393 svm->nested.vmcb12_gpa = kvm_state->hdr.svm.vmcb_pa;
Paolo Bonzinic08f3902020-11-17 02:51:35 -05001394
Vitaly Kuznetsov2bb16be2021-07-19 11:03:22 +02001395 svm_copy_vmrun_state(&svm->vmcb01.ptr->save, save);
Paolo Bonzini9e8f0fb2020-11-17 05:15:41 -05001396 nested_load_control_from_vmcb12(svm, ctl);
Cathy Avery4995a362021-01-13 07:07:52 -05001397
1398 svm_switch_vmcb(svm, &svm->nested.vmcb02);
Paolo Bonzini9e8f0fb2020-11-17 05:15:41 -05001399 nested_vmcb02_prepare_control(svm);
Paolo Bonzinia7d5c7c2020-09-22 07:43:14 -04001400 kvm_make_request(KVM_REQ_GET_NESTED_STATE_PAGES, vcpu);
Joerg Roedel6ccbd292020-09-07 15:15:02 +02001401 ret = 0;
1402out_free:
1403 kfree(save);
1404 kfree(ctl);
1405
1406 return ret;
Paolo Bonzinicc440cd2020-05-13 13:36:32 -04001407}
1408
Maxim Levitsky232f75d2021-04-01 17:18:10 +03001409static bool svm_get_nested_state_pages(struct kvm_vcpu *vcpu)
1410{
1411 struct vcpu_svm *svm = to_svm(vcpu);
1412
1413 if (WARN_ON(!is_guest_mode(vcpu)))
1414 return true;
1415
Maxim Levitsky158a48e2021-06-07 12:02:03 +03001416 if (!vcpu->arch.pdptrs_from_userspace &&
1417 !nested_npt_enabled(svm) && is_pae_paging(vcpu))
Maxim Levitskyb222b0b2021-06-07 12:01:59 +03001418 /*
1419 * Reload the guest's PDPTRs since after a migration
1420 * the guest CR3 might be restored prior to setting the nested
1421 * state which can lead to a load of wrong PDPTRs.
1422 */
1423 if (CC(!load_pdptrs(vcpu, vcpu->arch.walk_mmu, vcpu->arch.cr3)))
1424 return false;
Maxim Levitsky232f75d2021-04-01 17:18:10 +03001425
1426 if (!nested_svm_vmrun_msrpm(svm)) {
1427 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
1428 vcpu->run->internal.suberror =
1429 KVM_INTERNAL_ERROR_EMULATION;
1430 vcpu->run->internal.ndata = 0;
1431 return false;
1432 }
1433
1434 return true;
1435}
1436
Paolo Bonzini33b22172020-04-17 10:24:18 -04001437struct kvm_x86_nested_ops svm_nested_ops = {
1438 .check_events = svm_check_nested_events,
Sean Christophersoncb6a32c2021-03-02 09:45:14 -08001439 .triple_fault = nested_svm_triple_fault,
Paolo Bonzinia7d5c7c2020-09-22 07:43:14 -04001440 .get_nested_state_pages = svm_get_nested_state_pages,
Paolo Bonzinicc440cd2020-05-13 13:36:32 -04001441 .get_state = svm_get_nested_state,
1442 .set_state = svm_set_nested_state,
Paolo Bonzini33b22172020-04-17 10:24:18 -04001443};