blob: 56e6198d8f33dcb526c87c25ecac5a6e6d2f6e2b [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
32static void nested_svm_inject_npf_exit(struct kvm_vcpu *vcpu,
33 struct x86_exception *fault)
34{
35 struct vcpu_svm *svm = to_svm(vcpu);
36
37 if (svm->vmcb->control.exit_code != SVM_EXIT_NPF) {
38 /*
39 * TODO: track the cause of the nested page fault, and
40 * correctly fill in the high bits of exit_info_1.
41 */
42 svm->vmcb->control.exit_code = SVM_EXIT_NPF;
43 svm->vmcb->control.exit_code_hi = 0;
44 svm->vmcb->control.exit_info_1 = (1ULL << 32);
45 svm->vmcb->control.exit_info_2 = fault->address;
46 }
47
48 svm->vmcb->control.exit_info_1 &= ~0xffffffffULL;
49 svm->vmcb->control.exit_info_1 |= fault->error_code;
50
Joerg Roedel883b0a92020-03-24 10:41:52 +010051 nested_svm_vmexit(svm);
52}
53
Paolo Bonzinia04aead2021-02-18 07:16:59 -050054static void svm_inject_page_fault_nested(struct kvm_vcpu *vcpu, struct x86_exception *fault)
55{
56 struct vcpu_svm *svm = to_svm(vcpu);
57 WARN_ON(!is_guest_mode(vcpu));
58
59 if (vmcb_is_intercept(&svm->nested.ctl, INTERCEPT_EXCEPTION_OFFSET + PF_VECTOR) &&
60 !svm->nested.nested_run_pending) {
61 svm->vmcb->control.exit_code = SVM_EXIT_EXCP_BASE + PF_VECTOR;
62 svm->vmcb->control.exit_code_hi = 0;
63 svm->vmcb->control.exit_info_1 = fault->error_code;
64 svm->vmcb->control.exit_info_2 = fault->address;
65 nested_svm_vmexit(svm);
66 } else {
67 kvm_inject_page_fault(vcpu, fault);
68 }
69}
70
Joerg Roedel883b0a92020-03-24 10:41:52 +010071static u64 nested_svm_get_tdp_pdptr(struct kvm_vcpu *vcpu, int index)
72{
73 struct vcpu_svm *svm = to_svm(vcpu);
Paolo Bonzinie670bf62020-05-13 13:16:12 -040074 u64 cr3 = svm->nested.ctl.nested_cr3;
Joerg Roedel883b0a92020-03-24 10:41:52 +010075 u64 pdpte;
76 int ret;
77
Sean Christopherson2732be92021-02-03 16:01:07 -080078 ret = kvm_vcpu_read_guest_page(vcpu, gpa_to_gfn(cr3), &pdpte,
Joerg Roedel883b0a92020-03-24 10:41:52 +010079 offset_in_page(cr3) + index * 8, 8);
80 if (ret)
81 return 0;
82 return pdpte;
83}
84
85static unsigned long nested_svm_get_tdp_cr3(struct kvm_vcpu *vcpu)
86{
87 struct vcpu_svm *svm = to_svm(vcpu);
88
Paolo Bonzinie670bf62020-05-13 13:16:12 -040089 return svm->nested.ctl.nested_cr3;
Joerg Roedel883b0a92020-03-24 10:41:52 +010090}
91
92static void nested_svm_init_mmu_context(struct kvm_vcpu *vcpu)
93{
Paolo Bonzini929d1cf2020-05-19 06:18:31 -040094 struct vcpu_svm *svm = to_svm(vcpu);
Paolo Bonzini929d1cf2020-05-19 06:18:31 -040095
Joerg Roedel883b0a92020-03-24 10:41:52 +010096 WARN_ON(mmu_is_nested(vcpu));
97
98 vcpu->arch.mmu = &vcpu->arch.guest_mmu;
Cathy Avery4995a362021-01-13 07:07:52 -050099 kvm_init_shadow_npt_mmu(vcpu, X86_CR0_PG, svm->vmcb01.ptr->save.cr4,
100 svm->vmcb01.ptr->save.efer,
Vitaly Kuznetsov0f04a2a2020-07-10 16:11:49 +0200101 svm->nested.ctl.nested_cr3);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100102 vcpu->arch.mmu->get_guest_pgd = nested_svm_get_tdp_cr3;
103 vcpu->arch.mmu->get_pdptr = nested_svm_get_tdp_pdptr;
104 vcpu->arch.mmu->inject_page_fault = nested_svm_inject_npf_exit;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100105 reset_shadow_zero_bits_mask(vcpu, vcpu->arch.mmu);
106 vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu;
107}
108
109static void nested_svm_uninit_mmu_context(struct kvm_vcpu *vcpu)
110{
111 vcpu->arch.mmu = &vcpu->arch.root_mmu;
112 vcpu->arch.walk_mmu = &vcpu->arch.root_mmu;
113}
114
115void recalc_intercepts(struct vcpu_svm *svm)
116{
Paolo Bonzinie670bf62020-05-13 13:16:12 -0400117 struct vmcb_control_area *c, *h, *g;
Babu Mogerc45ad722020-09-11 14:27:58 -0500118 unsigned int i;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100119
Joerg Roedel06e78522020-06-25 10:03:23 +0200120 vmcb_mark_dirty(svm->vmcb, VMCB_INTERCEPTS);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100121
122 if (!is_guest_mode(&svm->vcpu))
123 return;
124
125 c = &svm->vmcb->control;
Cathy Avery4995a362021-01-13 07:07:52 -0500126 h = &svm->vmcb01.ptr->control;
Paolo Bonzinie670bf62020-05-13 13:16:12 -0400127 g = &svm->nested.ctl;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100128
Babu Mogerc45ad722020-09-11 14:27:58 -0500129 for (i = 0; i < MAX_INTERCEPT; i++)
130 c->intercepts[i] = h->intercepts[i];
131
Paolo Bonzinie9fd7612020-05-13 13:28:23 -0400132 if (g->int_ctl & V_INTR_MASKING_MASK) {
Joerg Roedel883b0a92020-03-24 10:41:52 +0100133 /* We only want the cr8 intercept bits of L1 */
Babu Moger03bfeeb2020-09-11 14:28:05 -0500134 vmcb_clr_intercept(c, INTERCEPT_CR8_READ);
135 vmcb_clr_intercept(c, INTERCEPT_CR8_WRITE);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100136
137 /*
138 * Once running L2 with HF_VINTR_MASK, EFLAGS.IF does not
139 * affect any interrupt we may want to inject; therefore,
140 * interrupt window vmexits are irrelevant to L0.
141 */
Babu Mogerc62e2e92020-09-11 14:28:28 -0500142 vmcb_clr_intercept(c, INTERCEPT_VINTR);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100143 }
144
145 /* We don't want to see VMMCALLs from a nested guest */
Babu Mogerc62e2e92020-09-11 14:28:28 -0500146 vmcb_clr_intercept(c, INTERCEPT_VMMCALL);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100147
Babu Mogerc45ad722020-09-11 14:27:58 -0500148 for (i = 0; i < MAX_INTERCEPT; i++)
149 c->intercepts[i] |= g->intercepts[i];
Joerg Roedel883b0a92020-03-24 10:41:52 +0100150}
151
Paolo Bonzini2f675912020-05-18 15:21:22 -0400152static void copy_vmcb_control_area(struct vmcb_control_area *dst,
153 struct vmcb_control_area *from)
Joerg Roedel883b0a92020-03-24 10:41:52 +0100154{
Babu Mogerc45ad722020-09-11 14:27:58 -0500155 unsigned int i;
156
157 for (i = 0; i < MAX_INTERCEPT; i++)
158 dst->intercepts[i] = from->intercepts[i];
159
Joerg Roedel883b0a92020-03-24 10:41:52 +0100160 dst->iopm_base_pa = from->iopm_base_pa;
161 dst->msrpm_base_pa = from->msrpm_base_pa;
162 dst->tsc_offset = from->tsc_offset;
Paolo Bonzini6c0238c2020-05-20 08:02:17 -0400163 /* asid not copied, it is handled manually for svm->vmcb. */
Joerg Roedel883b0a92020-03-24 10:41:52 +0100164 dst->tlb_ctl = from->tlb_ctl;
165 dst->int_ctl = from->int_ctl;
166 dst->int_vector = from->int_vector;
167 dst->int_state = from->int_state;
168 dst->exit_code = from->exit_code;
169 dst->exit_code_hi = from->exit_code_hi;
170 dst->exit_info_1 = from->exit_info_1;
171 dst->exit_info_2 = from->exit_info_2;
172 dst->exit_int_info = from->exit_int_info;
173 dst->exit_int_info_err = from->exit_int_info_err;
174 dst->nested_ctl = from->nested_ctl;
175 dst->event_inj = from->event_inj;
176 dst->event_inj_err = from->event_inj_err;
177 dst->nested_cr3 = from->nested_cr3;
178 dst->virt_ext = from->virt_ext;
179 dst->pause_filter_count = from->pause_filter_count;
180 dst->pause_filter_thresh = from->pause_filter_thresh;
181}
182
183static bool nested_svm_vmrun_msrpm(struct vcpu_svm *svm)
184{
185 /*
186 * This function merges the msr permission bitmaps of kvm and the
187 * nested vmcb. It is optimized in that it only merges the parts where
188 * the kvm msr permission bitmap may contain zero bits
189 */
190 int i;
191
Babu Mogerc62e2e92020-09-11 14:28:28 -0500192 if (!(vmcb_is_intercept(&svm->nested.ctl, INTERCEPT_MSR_PROT)))
Joerg Roedel883b0a92020-03-24 10:41:52 +0100193 return true;
194
195 for (i = 0; i < MSRPM_OFFSETS; i++) {
196 u32 value, p;
197 u64 offset;
198
199 if (msrpm_offsets[i] == 0xffffffff)
200 break;
201
202 p = msrpm_offsets[i];
Paolo Bonzinie670bf62020-05-13 13:16:12 -0400203 offset = svm->nested.ctl.msrpm_base_pa + (p * 4);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100204
205 if (kvm_vcpu_read_guest(&svm->vcpu, offset, &value, 4))
206 return false;
207
208 svm->nested.msrpm[p] = svm->msrpm[p] | value;
209 }
210
211 svm->vmcb->control.msrpm_base_pa = __sme_set(__pa(svm->nested.msrpm));
212
213 return true;
214}
215
Paolo Bonzinia7d5c7c2020-09-22 07:43:14 -0400216static bool svm_get_nested_state_pages(struct kvm_vcpu *vcpu)
217{
218 struct vcpu_svm *svm = to_svm(vcpu);
Maxim Levitskyf2c7ef32021-01-07 11:38:51 +0200219
Paolo Bonzini9a78e152021-01-08 11:43:08 -0500220 if (WARN_ON(!is_guest_mode(vcpu)))
221 return true;
222
Paolo Bonzinia7d5c7c2020-09-22 07:43:14 -0400223 if (!nested_svm_vmrun_msrpm(svm)) {
224 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
225 vcpu->run->internal.suberror =
226 KVM_INTERNAL_ERROR_EMULATION;
227 vcpu->run->internal.ndata = 0;
228 return false;
229 }
230
231 return true;
232}
233
Paolo Bonzinica46d732020-05-18 13:02:15 -0400234static bool nested_vmcb_check_controls(struct vmcb_control_area *control)
235{
Babu Mogerc62e2e92020-09-11 14:28:28 -0500236 if ((vmcb_is_intercept(control, INTERCEPT_VMRUN)) == 0)
Paolo Bonzinica46d732020-05-18 13:02:15 -0400237 return false;
238
239 if (control->asid == 0)
240 return false;
241
242 if ((control->nested_ctl & SVM_NESTED_CTL_NP_ENABLE) &&
243 !npt_enabled)
244 return false;
245
246 return true;
247}
248
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300249static bool nested_vmcb_checks(struct vcpu_svm *svm, struct vmcb *vmcb12)
Joerg Roedel883b0a92020-03-24 10:41:52 +0100250{
Paolo Bonzinic1c35cf2020-11-13 08:30:38 -0500251 struct kvm_vcpu *vcpu = &svm->vcpu;
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300252 bool vmcb12_lma;
253
254 if ((vmcb12->save.efer & EFER_SVME) == 0)
Joerg Roedel883b0a92020-03-24 10:41:52 +0100255 return false;
256
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300257 if (((vmcb12->save.cr0 & X86_CR0_CD) == 0) && (vmcb12->save.cr0 & X86_CR0_NW))
Krish Sadhukhan4f233372020-04-09 16:50:33 -0400258 return false;
259
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300260 if (!kvm_dr6_valid(vmcb12->save.dr6) || !kvm_dr7_valid(vmcb12->save.dr7))
Krish Sadhukhan1aef8162020-05-22 18:19:52 -0400261 return false;
262
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300263 vmcb12_lma = (vmcb12->save.efer & EFER_LME) && (vmcb12->save.cr0 & X86_CR0_PG);
Krish Sadhukhan761e4162020-07-08 00:39:56 +0000264
Paolo Bonzinic1c35cf2020-11-13 08:30:38 -0500265 if (vmcb12_lma) {
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300266 if (!(vmcb12->save.cr4 & X86_CR4_PAE) ||
267 !(vmcb12->save.cr0 & X86_CR0_PE) ||
Sean Christophersonca29e142021-02-03 16:01:12 -0800268 kvm_vcpu_is_illegal_gpa(vcpu, vmcb12->save.cr3))
Krish Sadhukhan761e4162020-07-08 00:39:56 +0000269 return false;
270 }
Sean Christophersonee69c922020-10-06 18:44:16 -0700271 if (!kvm_is_valid_cr4(&svm->vcpu, vmcb12->save.cr4))
Krish Sadhukhan761e4162020-07-08 00:39:56 +0000272 return false;
273
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300274 return nested_vmcb_check_controls(&vmcb12->control);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100275}
276
Paolo Bonzini9e8f0fb2020-11-17 05:15:41 -0500277static void nested_load_control_from_vmcb12(struct vcpu_svm *svm,
278 struct vmcb_control_area *control)
Paolo Bonzini3e06f012020-05-13 13:07:26 -0400279{
Paolo Bonzinie670bf62020-05-13 13:16:12 -0400280 copy_vmcb_control_area(&svm->nested.ctl, control);
Paolo Bonzini3e06f012020-05-13 13:07:26 -0400281
Paolo Bonzinicc440cd2020-05-13 13:36:32 -0400282 /* Copy it here because nested_svm_check_controls will check it. */
283 svm->nested.ctl.asid = control->asid;
Paolo Bonzinie670bf62020-05-13 13:16:12 -0400284 svm->nested.ctl.msrpm_base_pa &= ~0x0fffULL;
285 svm->nested.ctl.iopm_base_pa &= ~0x0fffULL;
Paolo Bonzini3e06f012020-05-13 13:07:26 -0400286}
287
Paolo Bonzini2d8a42b2020-05-22 03:50:14 -0400288/*
289 * Synchronize fields that are written by the processor, so that
Paolo Bonzini9e8f0fb2020-11-17 05:15:41 -0500290 * they can be copied back into the vmcb12.
Paolo Bonzini2d8a42b2020-05-22 03:50:14 -0400291 */
Paolo Bonzini9e8f0fb2020-11-17 05:15:41 -0500292void nested_sync_control_from_vmcb02(struct vcpu_svm *svm)
Paolo Bonzini2d8a42b2020-05-22 03:50:14 -0400293{
294 u32 mask;
295 svm->nested.ctl.event_inj = svm->vmcb->control.event_inj;
296 svm->nested.ctl.event_inj_err = svm->vmcb->control.event_inj_err;
297
298 /* Only a few fields of int_ctl are written by the processor. */
299 mask = V_IRQ_MASK | V_TPR_MASK;
300 if (!(svm->nested.ctl.int_ctl & V_INTR_MASKING_MASK) &&
Joerg Roedela284ba52020-06-25 10:03:24 +0200301 svm_is_intercept(svm, INTERCEPT_VINTR)) {
Paolo Bonzini2d8a42b2020-05-22 03:50:14 -0400302 /*
303 * In order to request an interrupt window, L0 is usurping
304 * svm->vmcb->control.int_ctl and possibly setting V_IRQ
305 * even if it was clear in L1's VMCB. Restoring it would be
306 * wrong. However, in this case V_IRQ will remain true until
307 * interrupt_window_interception calls svm_clear_vintr and
308 * restores int_ctl. We can just leave it aside.
309 */
310 mask &= ~V_IRQ_MASK;
311 }
312 svm->nested.ctl.int_ctl &= ~mask;
313 svm->nested.ctl.int_ctl |= svm->vmcb->control.int_ctl & mask;
314}
315
Paolo Bonzini36e2e982020-05-22 06:04:57 -0400316/*
317 * Transfer any event that L0 or L1 wanted to inject into L2 to
318 * EXIT_INT_INFO.
319 */
Paolo Bonzini9e8f0fb2020-11-17 05:15:41 -0500320static void nested_save_pending_event_to_vmcb12(struct vcpu_svm *svm,
321 struct vmcb *vmcb12)
Paolo Bonzini36e2e982020-05-22 06:04:57 -0400322{
323 struct kvm_vcpu *vcpu = &svm->vcpu;
324 u32 exit_int_info = 0;
325 unsigned int nr;
326
327 if (vcpu->arch.exception.injected) {
328 nr = vcpu->arch.exception.nr;
329 exit_int_info = nr | SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_EXEPT;
330
331 if (vcpu->arch.exception.has_error_code) {
332 exit_int_info |= SVM_EVTINJ_VALID_ERR;
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300333 vmcb12->control.exit_int_info_err =
Paolo Bonzini36e2e982020-05-22 06:04:57 -0400334 vcpu->arch.exception.error_code;
335 }
336
337 } else if (vcpu->arch.nmi_injected) {
338 exit_int_info = SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_NMI;
339
340 } else if (vcpu->arch.interrupt.injected) {
341 nr = vcpu->arch.interrupt.nr;
342 exit_int_info = nr | SVM_EVTINJ_VALID;
343
344 if (vcpu->arch.interrupt.soft)
345 exit_int_info |= SVM_EVTINJ_TYPE_SOFT;
346 else
347 exit_int_info |= SVM_EVTINJ_TYPE_INTR;
348 }
349
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300350 vmcb12->control.exit_int_info = exit_int_info;
Paolo Bonzini36e2e982020-05-22 06:04:57 -0400351}
352
Vitaly Kuznetsov62156f62020-07-10 16:11:53 +0200353static inline bool nested_npt_enabled(struct vcpu_svm *svm)
354{
355 return svm->nested.ctl.nested_ctl & SVM_NESTED_CTL_NP_ENABLE;
356}
357
358/*
Vitaly Kuznetsovd82aaef2020-07-10 16:11:56 +0200359 * Load guest's/host's cr3 on nested vmentry or vmexit. @nested_npt is true
360 * if we are emulating VM-Entry into a guest with NPT enabled.
Vitaly Kuznetsov62156f62020-07-10 16:11:53 +0200361 */
362static int nested_svm_load_cr3(struct kvm_vcpu *vcpu, unsigned long cr3,
363 bool nested_npt)
364{
Sean Christophersonbbc2c632021-02-03 16:01:11 -0800365 if (kvm_vcpu_is_illegal_gpa(vcpu, cr3))
Vitaly Kuznetsova506fdd2020-07-10 16:11:55 +0200366 return -EINVAL;
367
368 if (!nested_npt && is_pae_paging(vcpu) &&
369 (cr3 != kvm_read_cr3(vcpu) || pdptrs_changed(vcpu))) {
370 if (!load_pdptrs(vcpu, vcpu->arch.walk_mmu, cr3))
371 return -EINVAL;
372 }
373
374 /*
375 * TODO: optimize unconditional TLB flush/MMU sync here and in
376 * kvm_init_shadow_npt_mmu().
377 */
378 if (!nested_npt)
379 kvm_mmu_new_pgd(vcpu, cr3, false, false);
380
381 vcpu->arch.cr3 = cr3;
382 kvm_register_mark_available(vcpu, VCPU_EXREG_CR3);
383
384 kvm_init_mmu(vcpu, false);
385
386 return 0;
Vitaly Kuznetsov62156f62020-07-10 16:11:53 +0200387}
388
Cathy Avery4995a362021-01-13 07:07:52 -0500389void nested_vmcb02_compute_g_pat(struct vcpu_svm *svm)
390{
391 if (!svm->nested.vmcb02.ptr)
392 return;
393
394 /* FIXME: merge g_pat from vmcb01 and vmcb12. */
395 svm->nested.vmcb02.ptr->save.g_pat = svm->vmcb01.ptr->save.g_pat;
396}
397
Paolo Bonzini9e8f0fb2020-11-17 05:15:41 -0500398static void nested_vmcb02_prepare_save(struct vcpu_svm *svm, struct vmcb *vmcb12)
Joerg Roedel883b0a92020-03-24 10:41:52 +0100399{
Cathy Avery4995a362021-01-13 07:07:52 -0500400 nested_vmcb02_compute_g_pat(svm);
401
Joerg Roedel883b0a92020-03-24 10:41:52 +0100402 /* Load the nested guest state */
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300403 svm->vmcb->save.es = vmcb12->save.es;
404 svm->vmcb->save.cs = vmcb12->save.cs;
405 svm->vmcb->save.ss = vmcb12->save.ss;
406 svm->vmcb->save.ds = vmcb12->save.ds;
Paolo Bonzini4bb170a2020-11-16 06:38:19 -0500407 svm->vmcb->save.cpl = vmcb12->save.cpl;
408 vmcb_mark_dirty(svm->vmcb, VMCB_SEG);
409
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300410 svm->vmcb->save.gdtr = vmcb12->save.gdtr;
411 svm->vmcb->save.idtr = vmcb12->save.idtr;
Paolo Bonzini4bb170a2020-11-16 06:38:19 -0500412 vmcb_mark_dirty(svm->vmcb, VMCB_DT);
413
Paolo Bonzini8cce12b2020-11-27 12:46:36 -0500414 kvm_set_rflags(&svm->vcpu, vmcb12->save.rflags | X86_EFLAGS_FIXED);
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300415 svm_set_efer(&svm->vcpu, vmcb12->save.efer);
416 svm_set_cr0(&svm->vcpu, vmcb12->save.cr0);
417 svm_set_cr4(&svm->vcpu, vmcb12->save.cr4);
Paolo Bonzini4bb170a2020-11-16 06:38:19 -0500418
419 svm->vcpu.arch.cr2 = vmcb12->save.cr2;
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300420 kvm_rax_write(&svm->vcpu, vmcb12->save.rax);
421 kvm_rsp_write(&svm->vcpu, vmcb12->save.rsp);
422 kvm_rip_write(&svm->vcpu, vmcb12->save.rip);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100423
424 /* In case we don't even reach vcpu_run, the fields are not updated */
Paolo Bonzini4bb170a2020-11-16 06:38:19 -0500425 svm->vmcb->save.cr2 = svm->vcpu.arch.cr2;
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300426 svm->vmcb->save.rax = vmcb12->save.rax;
427 svm->vmcb->save.rsp = vmcb12->save.rsp;
428 svm->vmcb->save.rip = vmcb12->save.rip;
Paolo Bonzini4bb170a2020-11-16 06:38:19 -0500429
Paolo Bonzini8cce12b2020-11-27 12:46:36 -0500430 svm->vmcb->save.dr7 = vmcb12->save.dr7 | DR7_FIXED_1;
Chenyi Qiang9a3ecd52021-02-02 17:04:31 +0800431 svm->vcpu.arch.dr6 = vmcb12->save.dr6 | DR6_ACTIVE_LOW;
Paolo Bonzini4bb170a2020-11-16 06:38:19 -0500432 vmcb_mark_dirty(svm->vmcb, VMCB_DR);
Paolo Bonzinif241d712020-05-18 10:56:43 -0400433}
Joerg Roedel883b0a92020-03-24 10:41:52 +0100434
Paolo Bonzini9e8f0fb2020-11-17 05:15:41 -0500435static void nested_vmcb02_prepare_control(struct vcpu_svm *svm)
Paolo Bonzinif241d712020-05-18 10:56:43 -0400436{
Paolo Bonzini91b71302020-05-22 12:28:52 -0400437 const u32 mask = V_INTR_MASKING_MASK | V_GIF_ENABLE_MASK | V_GIF_MASK;
Vitaly Kuznetsov62156f62020-07-10 16:11:53 +0200438
Paolo Bonzini7c3ecfc2020-11-16 06:13:15 -0500439 /*
440 * Filled at exit: exit_code, exit_code_hi, exit_info_1, exit_info_2,
441 * exit_int_info, exit_int_info_err, next_rip, insn_len, insn_bytes.
442 */
Cathy Avery4995a362021-01-13 07:07:52 -0500443
Paolo Bonzini7c3ecfc2020-11-16 06:13:15 -0500444 /*
445 * Also covers avic_vapic_bar, avic_backing_page, avic_logical_id,
446 * avic_physical_id.
447 */
448 WARN_ON(svm->vmcb01.ptr->control.int_ctl & AVIC_ENABLE_MASK);
449
450 /* Copied from vmcb01. msrpm_base can be overwritten later. */
451 svm->vmcb->control.nested_ctl = svm->vmcb01.ptr->control.nested_ctl;
452 svm->vmcb->control.iopm_base_pa = svm->vmcb01.ptr->control.iopm_base_pa;
453 svm->vmcb->control.msrpm_base_pa = svm->vmcb01.ptr->control.msrpm_base_pa;
454
455 /* Done at vmrun: asid. */
456
457 /* Also overwritten later if necessary. */
458 svm->vmcb->control.tlb_ctl = TLB_CONTROL_DO_NOTHING;
459
460 /* nested_cr3. */
Vitaly Kuznetsov62156f62020-07-10 16:11:53 +0200461 if (nested_npt_enabled(svm))
Paolo Bonzini69cb8772020-05-22 05:27:46 -0400462 nested_svm_init_mmu_context(&svm->vcpu);
463
Paolo Bonzini18fc6c52020-05-18 11:07:08 -0400464 svm->vmcb->control.tsc_offset = svm->vcpu.arch.tsc_offset =
Paolo Bonzinie670bf62020-05-13 13:16:12 -0400465 svm->vcpu.arch.l1_tsc_offset + svm->nested.ctl.tsc_offset;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100466
Paolo Bonzini91b71302020-05-22 12:28:52 -0400467 svm->vmcb->control.int_ctl =
468 (svm->nested.ctl.int_ctl & ~mask) |
Cathy Avery4995a362021-01-13 07:07:52 -0500469 (svm->vmcb01.ptr->control.int_ctl & mask);
Paolo Bonzini91b71302020-05-22 12:28:52 -0400470
Paolo Bonzinie670bf62020-05-13 13:16:12 -0400471 svm->vmcb->control.virt_ext = svm->nested.ctl.virt_ext;
472 svm->vmcb->control.int_vector = svm->nested.ctl.int_vector;
473 svm->vmcb->control.int_state = svm->nested.ctl.int_state;
474 svm->vmcb->control.event_inj = svm->nested.ctl.event_inj;
475 svm->vmcb->control.event_inj_err = svm->nested.ctl.event_inj_err;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100476
Paolo Bonzinie670bf62020-05-13 13:16:12 -0400477 svm->vmcb->control.pause_filter_count = svm->nested.ctl.pause_filter_count;
478 svm->vmcb->control.pause_filter_thresh = svm->nested.ctl.pause_filter_thresh;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100479
Joerg Roedel883b0a92020-03-24 10:41:52 +0100480 /* Enter Guest-Mode */
481 enter_guest_mode(&svm->vcpu);
482
483 /*
Paolo Bonzini4bb170a2020-11-16 06:38:19 -0500484 * Merge guest and host intercepts - must be called with vcpu in
485 * guest-mode to take effect.
Joerg Roedel883b0a92020-03-24 10:41:52 +0100486 */
487 recalc_intercepts(svm);
Paolo Bonzinif241d712020-05-18 10:56:43 -0400488}
489
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300490int enter_svm_guest_mode(struct vcpu_svm *svm, u64 vmcb12_gpa,
491 struct vmcb *vmcb12)
Paolo Bonzinif241d712020-05-18 10:56:43 -0400492{
Vitaly Kuznetsova506fdd2020-07-10 16:11:55 +0200493 int ret;
494
Maxim Levitsky954f4192021-02-17 16:57:13 +0200495 trace_kvm_nested_vmrun(svm->vmcb->save.rip, vmcb12_gpa,
496 vmcb12->save.rip,
497 vmcb12->control.int_ctl,
498 vmcb12->control.event_inj,
499 vmcb12->control.nested_ctl);
500
501 trace_kvm_nested_intercepts(vmcb12->control.intercepts[INTERCEPT_CR] & 0xffff,
502 vmcb12->control.intercepts[INTERCEPT_CR] >> 16,
503 vmcb12->control.intercepts[INTERCEPT_EXCEPTION],
504 vmcb12->control.intercepts[INTERCEPT_WORD3],
505 vmcb12->control.intercepts[INTERCEPT_WORD4],
506 vmcb12->control.intercepts[INTERCEPT_WORD5]);
507
508
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300509 svm->nested.vmcb12_gpa = vmcb12_gpa;
Cathy Avery4995a362021-01-13 07:07:52 -0500510
511 WARN_ON(svm->vmcb == svm->nested.vmcb02.ptr);
512
513 nested_svm_vmloadsave(svm->vmcb01.ptr, svm->nested.vmcb02.ptr);
Paolo Bonzini9e8f0fb2020-11-17 05:15:41 -0500514 nested_load_control_from_vmcb12(svm, &vmcb12->control);
Cathy Avery4995a362021-01-13 07:07:52 -0500515
516 svm_switch_vmcb(svm, &svm->nested.vmcb02);
Paolo Bonzini9e8f0fb2020-11-17 05:15:41 -0500517 nested_vmcb02_prepare_control(svm);
518 nested_vmcb02_prepare_save(svm, vmcb12);
Paolo Bonzinif241d712020-05-18 10:56:43 -0400519
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300520 ret = nested_svm_load_cr3(&svm->vcpu, vmcb12->save.cr3,
Vitaly Kuznetsova506fdd2020-07-10 16:11:55 +0200521 nested_npt_enabled(svm));
522 if (ret)
523 return ret;
524
Paolo Bonzinia04aead2021-02-18 07:16:59 -0500525 if (!npt_enabled)
526 svm->vcpu.arch.mmu->inject_page_fault = svm_inject_page_fault_nested;
527
Paolo Bonziniffdf7f92020-05-22 12:18:27 -0400528 svm_set_gif(svm, true);
Vitaly Kuznetsov59cd9bc2020-07-10 16:11:52 +0200529
530 return 0;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100531}
532
533int nested_svm_vmrun(struct vcpu_svm *svm)
534{
535 int ret;
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300536 struct vmcb *vmcb12;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100537 struct kvm_host_map map;
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300538 u64 vmcb12_gpa;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100539
Dongli Zhang43c11d92021-03-05 14:57:47 -0800540 ++svm->vcpu.stat.nested_run;
541
Paolo Bonzini7c67f5462020-04-23 10:52:48 -0400542 if (is_smm(&svm->vcpu)) {
543 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
544 return 1;
545 }
Joerg Roedel883b0a92020-03-24 10:41:52 +0100546
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300547 vmcb12_gpa = svm->vmcb->save.rax;
548 ret = kvm_vcpu_map(&svm->vcpu, gpa_to_gfn(vmcb12_gpa), &map);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100549 if (ret == -EINVAL) {
550 kvm_inject_gp(&svm->vcpu, 0);
551 return 1;
552 } else if (ret) {
553 return kvm_skip_emulated_instruction(&svm->vcpu);
554 }
555
556 ret = kvm_skip_emulated_instruction(&svm->vcpu);
557
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300558 vmcb12 = map.hva;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100559
Maxim Levitsky2fcf4872020-10-01 14:29:54 +0300560 if (WARN_ON_ONCE(!svm->nested.initialized))
561 return -EINVAL;
562
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300563 if (!nested_vmcb_checks(svm, vmcb12)) {
564 vmcb12->control.exit_code = SVM_EXIT_ERR;
565 vmcb12->control.exit_code_hi = 0;
566 vmcb12->control.exit_info_1 = 0;
567 vmcb12->control.exit_info_2 = 0;
Paolo Bonzini69c9dfa2020-05-13 12:57:26 -0400568 goto out;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100569 }
570
Joerg Roedel883b0a92020-03-24 10:41:52 +0100571
572 /* Clear internal status */
573 kvm_clear_exception_queue(&svm->vcpu);
574 kvm_clear_interrupt_queue(&svm->vcpu);
575
576 /*
Cathy Avery4995a362021-01-13 07:07:52 -0500577 * Since vmcb01 is not in use, we can use it to store some of the L1
578 * state.
Joerg Roedel883b0a92020-03-24 10:41:52 +0100579 */
Cathy Avery4995a362021-01-13 07:07:52 -0500580 svm->vmcb01.ptr->save.efer = svm->vcpu.arch.efer;
581 svm->vmcb01.ptr->save.cr0 = kvm_read_cr0(&svm->vcpu);
582 svm->vmcb01.ptr->save.cr4 = svm->vcpu.arch.cr4;
583 svm->vmcb01.ptr->save.rflags = kvm_get_rflags(&svm->vcpu);
584 svm->vmcb01.ptr->save.rip = kvm_rip_read(&svm->vcpu);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100585
Cathy Avery4995a362021-01-13 07:07:52 -0500586 if (!npt_enabled)
587 svm->vmcb01.ptr->save.cr3 = kvm_read_cr3(&svm->vcpu);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100588
Paolo Bonzinif74f9412020-04-23 13:22:27 -0400589 svm->nested.nested_run_pending = 1;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100590
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300591 if (enter_svm_guest_mode(svm, vmcb12_gpa, vmcb12))
Vitaly Kuznetsov59cd9bc2020-07-10 16:11:52 +0200592 goto out_exit_err;
Vitaly Kuznetsovebdb3db2020-07-10 16:11:51 +0200593
Vitaly Kuznetsov59cd9bc2020-07-10 16:11:52 +0200594 if (nested_svm_vmrun_msrpm(svm))
595 goto out;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100596
Vitaly Kuznetsov59cd9bc2020-07-10 16:11:52 +0200597out_exit_err:
598 svm->nested.nested_run_pending = 0;
599
600 svm->vmcb->control.exit_code = SVM_EXIT_ERR;
601 svm->vmcb->control.exit_code_hi = 0;
602 svm->vmcb->control.exit_info_1 = 0;
603 svm->vmcb->control.exit_info_2 = 0;
604
605 nested_svm_vmexit(svm);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100606
Paolo Bonzini69c9dfa2020-05-13 12:57:26 -0400607out:
608 kvm_vcpu_unmap(&svm->vcpu, &map, true);
609
Joerg Roedel883b0a92020-03-24 10:41:52 +0100610 return ret;
611}
612
613void nested_svm_vmloadsave(struct vmcb *from_vmcb, struct vmcb *to_vmcb)
614{
615 to_vmcb->save.fs = from_vmcb->save.fs;
616 to_vmcb->save.gs = from_vmcb->save.gs;
617 to_vmcb->save.tr = from_vmcb->save.tr;
618 to_vmcb->save.ldtr = from_vmcb->save.ldtr;
619 to_vmcb->save.kernel_gs_base = from_vmcb->save.kernel_gs_base;
620 to_vmcb->save.star = from_vmcb->save.star;
621 to_vmcb->save.lstar = from_vmcb->save.lstar;
622 to_vmcb->save.cstar = from_vmcb->save.cstar;
623 to_vmcb->save.sfmask = from_vmcb->save.sfmask;
624 to_vmcb->save.sysenter_cs = from_vmcb->save.sysenter_cs;
625 to_vmcb->save.sysenter_esp = from_vmcb->save.sysenter_esp;
626 to_vmcb->save.sysenter_eip = from_vmcb->save.sysenter_eip;
627}
628
629int nested_svm_vmexit(struct vcpu_svm *svm)
630{
631 int rc;
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300632 struct vmcb *vmcb12;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100633 struct vmcb *vmcb = svm->vmcb;
634 struct kvm_host_map map;
635
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300636 rc = kvm_vcpu_map(&svm->vcpu, gpa_to_gfn(svm->nested.vmcb12_gpa), &map);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100637 if (rc) {
638 if (rc == -EINVAL)
639 kvm_inject_gp(&svm->vcpu, 0);
640 return 1;
641 }
642
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300643 vmcb12 = map.hva;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100644
645 /* Exit Guest-Mode */
646 leave_guest_mode(&svm->vcpu);
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300647 svm->nested.vmcb12_gpa = 0;
Paolo Bonzini2d8a42b2020-05-22 03:50:14 -0400648 WARN_ON_ONCE(svm->nested.nested_run_pending);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100649
Maxim Levitskyf2c7ef32021-01-07 11:38:51 +0200650 kvm_clear_request(KVM_REQ_GET_NESTED_STATE_PAGES, &svm->vcpu);
651
Paolo Bonzini38c0b192020-04-23 13:13:09 -0400652 /* in case we halted in L2 */
653 svm->vcpu.arch.mp_state = KVM_MP_STATE_RUNNABLE;
654
Joerg Roedel883b0a92020-03-24 10:41:52 +0100655 /* Give the current vmcb to the guest */
Joerg Roedel883b0a92020-03-24 10:41:52 +0100656
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300657 vmcb12->save.es = vmcb->save.es;
658 vmcb12->save.cs = vmcb->save.cs;
659 vmcb12->save.ss = vmcb->save.ss;
660 vmcb12->save.ds = vmcb->save.ds;
661 vmcb12->save.gdtr = vmcb->save.gdtr;
662 vmcb12->save.idtr = vmcb->save.idtr;
663 vmcb12->save.efer = svm->vcpu.arch.efer;
664 vmcb12->save.cr0 = kvm_read_cr0(&svm->vcpu);
665 vmcb12->save.cr3 = kvm_read_cr3(&svm->vcpu);
666 vmcb12->save.cr2 = vmcb->save.cr2;
667 vmcb12->save.cr4 = svm->vcpu.arch.cr4;
668 vmcb12->save.rflags = kvm_get_rflags(&svm->vcpu);
669 vmcb12->save.rip = kvm_rip_read(&svm->vcpu);
670 vmcb12->save.rsp = kvm_rsp_read(&svm->vcpu);
671 vmcb12->save.rax = kvm_rax_read(&svm->vcpu);
672 vmcb12->save.dr7 = vmcb->save.dr7;
673 vmcb12->save.dr6 = svm->vcpu.arch.dr6;
674 vmcb12->save.cpl = vmcb->save.cpl;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100675
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300676 vmcb12->control.int_state = vmcb->control.int_state;
677 vmcb12->control.exit_code = vmcb->control.exit_code;
678 vmcb12->control.exit_code_hi = vmcb->control.exit_code_hi;
679 vmcb12->control.exit_info_1 = vmcb->control.exit_info_1;
680 vmcb12->control.exit_info_2 = vmcb->control.exit_info_2;
Paolo Bonzini36e2e982020-05-22 06:04:57 -0400681
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300682 if (vmcb12->control.exit_code != SVM_EXIT_ERR)
Paolo Bonzini9e8f0fb2020-11-17 05:15:41 -0500683 nested_save_pending_event_to_vmcb12(svm, vmcb12);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100684
685 if (svm->nrips_enabled)
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300686 vmcb12->control.next_rip = vmcb->control.next_rip;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100687
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300688 vmcb12->control.int_ctl = svm->nested.ctl.int_ctl;
689 vmcb12->control.tlb_ctl = svm->nested.ctl.tlb_ctl;
690 vmcb12->control.event_inj = svm->nested.ctl.event_inj;
691 vmcb12->control.event_inj_err = svm->nested.ctl.event_inj_err;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100692
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300693 vmcb12->control.pause_filter_count =
Joerg Roedel883b0a92020-03-24 10:41:52 +0100694 svm->vmcb->control.pause_filter_count;
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300695 vmcb12->control.pause_filter_thresh =
Joerg Roedel883b0a92020-03-24 10:41:52 +0100696 svm->vmcb->control.pause_filter_thresh;
697
Cathy Avery4995a362021-01-13 07:07:52 -0500698 nested_svm_vmloadsave(svm->nested.vmcb02.ptr, svm->vmcb01.ptr);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100699
Cathy Avery4995a362021-01-13 07:07:52 -0500700 svm_switch_vmcb(svm, &svm->vmcb01);
701
702 /*
703 * On vmexit the GIF is set to false and
704 * no event can be injected in L1.
705 */
Maxim Levitsky98837642020-08-27 19:27:18 +0300706 svm_set_gif(svm, false);
Cathy Avery4995a362021-01-13 07:07:52 -0500707 svm->vmcb->control.exit_int_info = 0;
Maxim Levitsky98837642020-08-27 19:27:18 +0300708
Paolo Bonzini7ca62d12020-11-16 06:38:19 -0500709 svm->vcpu.arch.tsc_offset = svm->vcpu.arch.l1_tsc_offset;
710 if (svm->vmcb->control.tsc_offset != svm->vcpu.arch.tsc_offset) {
711 svm->vmcb->control.tsc_offset = svm->vcpu.arch.tsc_offset;
712 vmcb_mark_dirty(svm->vmcb, VMCB_INTERCEPTS);
713 }
Paolo Bonzini18fc6c52020-05-18 11:07:08 -0400714
Paolo Bonzinie670bf62020-05-13 13:16:12 -0400715 svm->nested.ctl.nested_cr3 = 0;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100716
Cathy Avery4995a362021-01-13 07:07:52 -0500717 /*
718 * Restore processor state that had been saved in vmcb01
719 */
Paolo Bonzinic08f3902020-11-17 02:51:35 -0500720 kvm_set_rflags(&svm->vcpu, svm->vmcb->save.rflags);
Cathy Avery4995a362021-01-13 07:07:52 -0500721 svm_set_efer(&svm->vcpu, svm->vmcb->save.efer);
722 svm_set_cr0(&svm->vcpu, svm->vmcb->save.cr0 | X86_CR0_PE);
723 svm_set_cr4(&svm->vcpu, svm->vmcb->save.cr4);
724 kvm_rax_write(&svm->vcpu, svm->vmcb->save.rax);
725 kvm_rsp_write(&svm->vcpu, svm->vmcb->save.rsp);
726 kvm_rip_write(&svm->vcpu, svm->vmcb->save.rip);
727
728 svm->vcpu.arch.dr7 = DR7_FIXED_1;
729 kvm_update_dr7(&svm->vcpu);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100730
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300731 trace_kvm_nested_vmexit_inject(vmcb12->control.exit_code,
732 vmcb12->control.exit_info_1,
733 vmcb12->control.exit_info_2,
734 vmcb12->control.exit_int_info,
735 vmcb12->control.exit_int_info_err,
Paolo Bonzini36e2e982020-05-22 06:04:57 -0400736 KVM_ISA_SVM);
737
Joerg Roedel883b0a92020-03-24 10:41:52 +0100738 kvm_vcpu_unmap(&svm->vcpu, &map, true);
739
740 nested_svm_uninit_mmu_context(&svm->vcpu);
Vitaly Kuznetsovbf7dea42020-07-10 16:11:54 +0200741
Cathy Avery4995a362021-01-13 07:07:52 -0500742 rc = nested_svm_load_cr3(&svm->vcpu, svm->vmcb->save.cr3, false);
Vitaly Kuznetsovd82aaef2020-07-10 16:11:56 +0200743 if (rc)
744 return 1;
Vitaly Kuznetsovbf7dea42020-07-10 16:11:54 +0200745
Joerg Roedel883b0a92020-03-24 10:41:52 +0100746 /*
747 * Drop what we picked up for L2 via svm_complete_interrupts() so it
748 * doesn't end up in L1.
749 */
750 svm->vcpu.arch.nmi_injected = false;
751 kvm_clear_exception_queue(&svm->vcpu);
752 kvm_clear_interrupt_queue(&svm->vcpu);
753
754 return 0;
755}
756
Maxim Levitsky2fcf4872020-10-01 14:29:54 +0300757int svm_allocate_nested(struct vcpu_svm *svm)
758{
Cathy Avery4995a362021-01-13 07:07:52 -0500759 struct page *vmcb02_page;
Maxim Levitsky2fcf4872020-10-01 14:29:54 +0300760
761 if (svm->nested.initialized)
762 return 0;
763
Cathy Avery4995a362021-01-13 07:07:52 -0500764 vmcb02_page = alloc_page(GFP_KERNEL_ACCOUNT | __GFP_ZERO);
765 if (!vmcb02_page)
Maxim Levitsky2fcf4872020-10-01 14:29:54 +0300766 return -ENOMEM;
Cathy Avery4995a362021-01-13 07:07:52 -0500767 svm->nested.vmcb02.ptr = page_address(vmcb02_page);
768 svm->nested.vmcb02.pa = __sme_set(page_to_pfn(vmcb02_page) << PAGE_SHIFT);
Maxim Levitsky2fcf4872020-10-01 14:29:54 +0300769
770 svm->nested.msrpm = svm_vcpu_alloc_msrpm();
771 if (!svm->nested.msrpm)
Cathy Avery4995a362021-01-13 07:07:52 -0500772 goto err_free_vmcb02;
Maxim Levitsky2fcf4872020-10-01 14:29:54 +0300773 svm_vcpu_init_msrpm(&svm->vcpu, svm->nested.msrpm);
774
775 svm->nested.initialized = true;
776 return 0;
777
Cathy Avery4995a362021-01-13 07:07:52 -0500778err_free_vmcb02:
779 __free_page(vmcb02_page);
Maxim Levitsky2fcf4872020-10-01 14:29:54 +0300780 return -ENOMEM;
781}
782
783void svm_free_nested(struct vcpu_svm *svm)
784{
785 if (!svm->nested.initialized)
786 return;
787
788 svm_vcpu_free_msrpm(svm->nested.msrpm);
789 svm->nested.msrpm = NULL;
790
Cathy Avery4995a362021-01-13 07:07:52 -0500791 __free_page(virt_to_page(svm->nested.vmcb02.ptr));
792 svm->nested.vmcb02.ptr = NULL;
Maxim Levitsky2fcf4872020-10-01 14:29:54 +0300793
794 svm->nested.initialized = false;
795}
796
Paolo Bonzinic513f482020-05-18 13:08:37 -0400797/*
798 * Forcibly leave nested mode in order to be able to reset the VCPU later on.
799 */
800void svm_leave_nested(struct vcpu_svm *svm)
801{
802 if (is_guest_mode(&svm->vcpu)) {
Paolo Bonzinic513f482020-05-18 13:08:37 -0400803 svm->nested.nested_run_pending = 0;
804 leave_guest_mode(&svm->vcpu);
Cathy Avery4995a362021-01-13 07:07:52 -0500805
806 svm_switch_vmcb(svm, &svm->nested.vmcb02);
807
Paolo Bonzinic513f482020-05-18 13:08:37 -0400808 nested_svm_uninit_mmu_context(&svm->vcpu);
Maxim Levitsky56fe28d2021-01-07 11:38:54 +0200809 vmcb_mark_all_dirty(svm->vmcb);
Paolo Bonzinic513f482020-05-18 13:08:37 -0400810 }
Paolo Bonzinia7d5c7c2020-09-22 07:43:14 -0400811
812 kvm_clear_request(KVM_REQ_GET_NESTED_STATE_PAGES, &svm->vcpu);
Paolo Bonzinic513f482020-05-18 13:08:37 -0400813}
814
Joerg Roedel883b0a92020-03-24 10:41:52 +0100815static int nested_svm_exit_handled_msr(struct vcpu_svm *svm)
816{
817 u32 offset, msr, value;
818 int write, mask;
819
Babu Mogerc62e2e92020-09-11 14:28:28 -0500820 if (!(vmcb_is_intercept(&svm->nested.ctl, INTERCEPT_MSR_PROT)))
Joerg Roedel883b0a92020-03-24 10:41:52 +0100821 return NESTED_EXIT_HOST;
822
823 msr = svm->vcpu.arch.regs[VCPU_REGS_RCX];
824 offset = svm_msrpm_offset(msr);
825 write = svm->vmcb->control.exit_info_1 & 1;
826 mask = 1 << ((2 * (msr & 0xf)) + write);
827
828 if (offset == MSR_INVALID)
829 return NESTED_EXIT_DONE;
830
831 /* Offset is in 32 bit units but need in 8 bit units */
832 offset *= 4;
833
Paolo Bonzinie670bf62020-05-13 13:16:12 -0400834 if (kvm_vcpu_read_guest(&svm->vcpu, svm->nested.ctl.msrpm_base_pa + offset, &value, 4))
Joerg Roedel883b0a92020-03-24 10:41:52 +0100835 return NESTED_EXIT_DONE;
836
837 return (value & mask) ? NESTED_EXIT_DONE : NESTED_EXIT_HOST;
838}
839
Joerg Roedel883b0a92020-03-24 10:41:52 +0100840static int nested_svm_intercept_ioio(struct vcpu_svm *svm)
841{
842 unsigned port, size, iopm_len;
843 u16 val, mask;
844 u8 start_bit;
845 u64 gpa;
846
Babu Mogerc62e2e92020-09-11 14:28:28 -0500847 if (!(vmcb_is_intercept(&svm->nested.ctl, INTERCEPT_IOIO_PROT)))
Joerg Roedel883b0a92020-03-24 10:41:52 +0100848 return NESTED_EXIT_HOST;
849
850 port = svm->vmcb->control.exit_info_1 >> 16;
851 size = (svm->vmcb->control.exit_info_1 & SVM_IOIO_SIZE_MASK) >>
852 SVM_IOIO_SIZE_SHIFT;
Paolo Bonzinie670bf62020-05-13 13:16:12 -0400853 gpa = svm->nested.ctl.iopm_base_pa + (port / 8);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100854 start_bit = port % 8;
855 iopm_len = (start_bit + size > 8) ? 2 : 1;
856 mask = (0xf >> (4 - size)) << start_bit;
857 val = 0;
858
859 if (kvm_vcpu_read_guest(&svm->vcpu, gpa, &val, iopm_len))
860 return NESTED_EXIT_DONE;
861
862 return (val & mask) ? NESTED_EXIT_DONE : NESTED_EXIT_HOST;
863}
864
865static int nested_svm_intercept(struct vcpu_svm *svm)
866{
867 u32 exit_code = svm->vmcb->control.exit_code;
868 int vmexit = NESTED_EXIT_HOST;
869
870 switch (exit_code) {
871 case SVM_EXIT_MSR:
872 vmexit = nested_svm_exit_handled_msr(svm);
873 break;
874 case SVM_EXIT_IOIO:
875 vmexit = nested_svm_intercept_ioio(svm);
876 break;
877 case SVM_EXIT_READ_CR0 ... SVM_EXIT_WRITE_CR8: {
Babu Moger03bfeeb2020-09-11 14:28:05 -0500878 if (vmcb_is_intercept(&svm->nested.ctl, exit_code))
Joerg Roedel883b0a92020-03-24 10:41:52 +0100879 vmexit = NESTED_EXIT_DONE;
880 break;
881 }
882 case SVM_EXIT_READ_DR0 ... SVM_EXIT_WRITE_DR7: {
Babu Moger30abaa882020-09-11 14:28:12 -0500883 if (vmcb_is_intercept(&svm->nested.ctl, exit_code))
Joerg Roedel883b0a92020-03-24 10:41:52 +0100884 vmexit = NESTED_EXIT_DONE;
885 break;
886 }
887 case SVM_EXIT_EXCP_BASE ... SVM_EXIT_EXCP_BASE + 0x1f: {
Paolo Bonzini7c866632020-05-16 08:42:28 -0400888 /*
889 * Host-intercepted exceptions have been checked already in
890 * nested_svm_exit_special. There is nothing to do here,
891 * the vmexit is injected by svm_check_nested_events.
892 */
893 vmexit = NESTED_EXIT_DONE;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100894 break;
895 }
896 case SVM_EXIT_ERR: {
897 vmexit = NESTED_EXIT_DONE;
898 break;
899 }
900 default: {
Babu Mogerc62e2e92020-09-11 14:28:28 -0500901 if (vmcb_is_intercept(&svm->nested.ctl, exit_code))
Joerg Roedel883b0a92020-03-24 10:41:52 +0100902 vmexit = NESTED_EXIT_DONE;
903 }
904 }
905
906 return vmexit;
907}
908
909int nested_svm_exit_handled(struct vcpu_svm *svm)
910{
911 int vmexit;
912
913 vmexit = nested_svm_intercept(svm);
914
915 if (vmexit == NESTED_EXIT_DONE)
916 nested_svm_vmexit(svm);
917
918 return vmexit;
919}
920
921int nested_svm_check_permissions(struct vcpu_svm *svm)
922{
923 if (!(svm->vcpu.arch.efer & EFER_SVME) ||
924 !is_paging(&svm->vcpu)) {
925 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
926 return 1;
927 }
928
929 if (svm->vmcb->save.cpl) {
930 kvm_inject_gp(&svm->vcpu, 0);
931 return 1;
932 }
933
934 return 0;
935}
936
Paolo Bonzini7c866632020-05-16 08:42:28 -0400937static bool nested_exit_on_exception(struct vcpu_svm *svm)
Joerg Roedel883b0a92020-03-24 10:41:52 +0100938{
Paolo Bonzini7c866632020-05-16 08:42:28 -0400939 unsigned int nr = svm->vcpu.arch.exception.nr;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100940
Babu Moger9780d512020-09-11 14:28:20 -0500941 return (svm->nested.ctl.intercepts[INTERCEPT_EXCEPTION] & BIT(nr));
Paolo Bonzini7c866632020-05-16 08:42:28 -0400942}
Joerg Roedel883b0a92020-03-24 10:41:52 +0100943
Paolo Bonzini7c866632020-05-16 08:42:28 -0400944static void nested_svm_inject_exception_vmexit(struct vcpu_svm *svm)
945{
946 unsigned int nr = svm->vcpu.arch.exception.nr;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100947
948 svm->vmcb->control.exit_code = SVM_EXIT_EXCP_BASE + nr;
949 svm->vmcb->control.exit_code_hi = 0;
Paolo Bonzini7c866632020-05-16 08:42:28 -0400950
951 if (svm->vcpu.arch.exception.has_error_code)
952 svm->vmcb->control.exit_info_1 = svm->vcpu.arch.exception.error_code;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100953
954 /*
955 * EXITINFO2 is undefined for all exception intercepts other
956 * than #PF.
957 */
Paolo Bonzini7c866632020-05-16 08:42:28 -0400958 if (nr == PF_VECTOR) {
959 if (svm->vcpu.arch.exception.nested_apf)
960 svm->vmcb->control.exit_info_2 = svm->vcpu.arch.apf.nested_apf_token;
961 else if (svm->vcpu.arch.exception.has_payload)
962 svm->vmcb->control.exit_info_2 = svm->vcpu.arch.exception.payload;
963 else
964 svm->vmcb->control.exit_info_2 = svm->vcpu.arch.cr2;
965 } else if (nr == DB_VECTOR) {
966 /* See inject_pending_event. */
967 kvm_deliver_exception_payload(&svm->vcpu);
968 if (svm->vcpu.arch.dr7 & DR7_GD) {
969 svm->vcpu.arch.dr7 &= ~DR7_GD;
970 kvm_update_dr7(&svm->vcpu);
971 }
972 } else
973 WARN_ON(svm->vcpu.arch.exception.has_payload);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100974
Paolo Bonzini7c866632020-05-16 08:42:28 -0400975 nested_svm_vmexit(svm);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100976}
977
Paolo Bonzini55714cd2020-04-23 08:17:28 -0400978static void nested_svm_smi(struct vcpu_svm *svm)
979{
980 svm->vmcb->control.exit_code = SVM_EXIT_SMI;
981 svm->vmcb->control.exit_info_1 = 0;
982 svm->vmcb->control.exit_info_2 = 0;
983
984 nested_svm_vmexit(svm);
985}
986
Cathy Avery9c3d3702020-04-14 16:11:06 -0400987static void nested_svm_nmi(struct vcpu_svm *svm)
988{
989 svm->vmcb->control.exit_code = SVM_EXIT_NMI;
990 svm->vmcb->control.exit_info_1 = 0;
991 svm->vmcb->control.exit_info_2 = 0;
992
993 nested_svm_vmexit(svm);
994}
995
Joerg Roedel883b0a92020-03-24 10:41:52 +0100996static void nested_svm_intr(struct vcpu_svm *svm)
997{
Paolo Bonzini6e085cb2020-04-23 13:15:33 -0400998 trace_kvm_nested_intr_vmexit(svm->vmcb->save.rip);
999
Joerg Roedel883b0a92020-03-24 10:41:52 +01001000 svm->vmcb->control.exit_code = SVM_EXIT_INTR;
1001 svm->vmcb->control.exit_info_1 = 0;
1002 svm->vmcb->control.exit_info_2 = 0;
1003
Paolo Bonzini6e085cb2020-04-23 13:15:33 -04001004 nested_svm_vmexit(svm);
Joerg Roedel883b0a92020-03-24 10:41:52 +01001005}
1006
Paolo Bonzini5b6724082020-05-16 08:50:35 -04001007static inline bool nested_exit_on_init(struct vcpu_svm *svm)
1008{
Babu Mogerc62e2e92020-09-11 14:28:28 -05001009 return vmcb_is_intercept(&svm->nested.ctl, INTERCEPT_INIT);
Paolo Bonzini5b6724082020-05-16 08:50:35 -04001010}
1011
1012static void nested_svm_init(struct vcpu_svm *svm)
1013{
1014 svm->vmcb->control.exit_code = SVM_EXIT_INIT;
1015 svm->vmcb->control.exit_info_1 = 0;
1016 svm->vmcb->control.exit_info_2 = 0;
1017
1018 nested_svm_vmexit(svm);
1019}
1020
1021
Paolo Bonzini33b22172020-04-17 10:24:18 -04001022static int svm_check_nested_events(struct kvm_vcpu *vcpu)
Joerg Roedel883b0a92020-03-24 10:41:52 +01001023{
1024 struct vcpu_svm *svm = to_svm(vcpu);
1025 bool block_nested_events =
Paolo Bonzinibd279622020-05-16 08:46:00 -04001026 kvm_event_needs_reinjection(vcpu) || svm->nested.nested_run_pending;
Paolo Bonzini5b6724082020-05-16 08:50:35 -04001027 struct kvm_lapic *apic = vcpu->arch.apic;
1028
1029 if (lapic_in_kernel(vcpu) &&
1030 test_bit(KVM_APIC_INIT, &apic->pending_events)) {
1031 if (block_nested_events)
1032 return -EBUSY;
1033 if (!nested_exit_on_init(svm))
1034 return 0;
1035 nested_svm_init(svm);
1036 return 0;
1037 }
Joerg Roedel883b0a92020-03-24 10:41:52 +01001038
Paolo Bonzini7c866632020-05-16 08:42:28 -04001039 if (vcpu->arch.exception.pending) {
1040 if (block_nested_events)
1041 return -EBUSY;
1042 if (!nested_exit_on_exception(svm))
1043 return 0;
1044 nested_svm_inject_exception_vmexit(svm);
1045 return 0;
1046 }
1047
Paolo Bonzini221e7612020-04-23 08:13:10 -04001048 if (vcpu->arch.smi_pending && !svm_smi_blocked(vcpu)) {
Paolo Bonzini55714cd2020-04-23 08:17:28 -04001049 if (block_nested_events)
1050 return -EBUSY;
Paolo Bonzini221e7612020-04-23 08:13:10 -04001051 if (!nested_exit_on_smi(svm))
1052 return 0;
Paolo Bonzini55714cd2020-04-23 08:17:28 -04001053 nested_svm_smi(svm);
1054 return 0;
1055 }
1056
Paolo Bonzini221e7612020-04-23 08:13:10 -04001057 if (vcpu->arch.nmi_pending && !svm_nmi_blocked(vcpu)) {
Cathy Avery9c3d3702020-04-14 16:11:06 -04001058 if (block_nested_events)
1059 return -EBUSY;
Paolo Bonzini221e7612020-04-23 08:13:10 -04001060 if (!nested_exit_on_nmi(svm))
1061 return 0;
Cathy Avery9c3d3702020-04-14 16:11:06 -04001062 nested_svm_nmi(svm);
1063 return 0;
1064 }
1065
Paolo Bonzini221e7612020-04-23 08:13:10 -04001066 if (kvm_cpu_has_interrupt(vcpu) && !svm_interrupt_blocked(vcpu)) {
Joerg Roedel883b0a92020-03-24 10:41:52 +01001067 if (block_nested_events)
1068 return -EBUSY;
Paolo Bonzini221e7612020-04-23 08:13:10 -04001069 if (!nested_exit_on_intr(svm))
1070 return 0;
Joerg Roedel883b0a92020-03-24 10:41:52 +01001071 nested_svm_intr(svm);
1072 return 0;
1073 }
1074
1075 return 0;
1076}
1077
1078int nested_svm_exit_special(struct vcpu_svm *svm)
1079{
1080 u32 exit_code = svm->vmcb->control.exit_code;
1081
1082 switch (exit_code) {
1083 case SVM_EXIT_INTR:
1084 case SVM_EXIT_NMI:
Joerg Roedel883b0a92020-03-24 10:41:52 +01001085 case SVM_EXIT_NPF:
Paolo Bonzini7c866632020-05-16 08:42:28 -04001086 return NESTED_EXIT_HOST;
1087 case SVM_EXIT_EXCP_BASE ... SVM_EXIT_EXCP_BASE + 0x1f: {
1088 u32 excp_bits = 1 << (exit_code - SVM_EXIT_EXCP_BASE);
1089
Cathy Avery4995a362021-01-13 07:07:52 -05001090 if (svm->vmcb01.ptr->control.intercepts[INTERCEPT_EXCEPTION] &
1091 excp_bits)
Paolo Bonzini7c866632020-05-16 08:42:28 -04001092 return NESTED_EXIT_HOST;
1093 else if (exit_code == SVM_EXIT_EXCP_BASE + PF_VECTOR &&
Vitaly Kuznetsov68fd66f2020-05-25 16:41:17 +02001094 svm->vcpu.arch.apf.host_apf_flags)
Paolo Bonzini7c866632020-05-16 08:42:28 -04001095 /* Trap async PF even if not shadowing */
Joerg Roedel883b0a92020-03-24 10:41:52 +01001096 return NESTED_EXIT_HOST;
1097 break;
Paolo Bonzini7c866632020-05-16 08:42:28 -04001098 }
Joerg Roedel883b0a92020-03-24 10:41:52 +01001099 default:
1100 break;
1101 }
1102
1103 return NESTED_EXIT_CONTINUE;
1104}
Paolo Bonzini33b22172020-04-17 10:24:18 -04001105
Paolo Bonzinicc440cd2020-05-13 13:36:32 -04001106static int svm_get_nested_state(struct kvm_vcpu *vcpu,
1107 struct kvm_nested_state __user *user_kvm_nested_state,
1108 u32 user_data_size)
1109{
1110 struct vcpu_svm *svm;
1111 struct kvm_nested_state kvm_state = {
1112 .flags = 0,
1113 .format = KVM_STATE_NESTED_FORMAT_SVM,
1114 .size = sizeof(kvm_state),
1115 };
1116 struct vmcb __user *user_vmcb = (struct vmcb __user *)
1117 &user_kvm_nested_state->data.svm[0];
1118
1119 if (!vcpu)
1120 return kvm_state.size + KVM_STATE_NESTED_SVM_VMCB_SIZE;
1121
1122 svm = to_svm(vcpu);
1123
1124 if (user_data_size < kvm_state.size)
1125 goto out;
1126
1127 /* First fill in the header and copy it out. */
1128 if (is_guest_mode(vcpu)) {
Maxim Levitsky0dd16b52020-08-27 20:11:39 +03001129 kvm_state.hdr.svm.vmcb_pa = svm->nested.vmcb12_gpa;
Paolo Bonzinicc440cd2020-05-13 13:36:32 -04001130 kvm_state.size += KVM_STATE_NESTED_SVM_VMCB_SIZE;
1131 kvm_state.flags |= KVM_STATE_NESTED_GUEST_MODE;
1132
1133 if (svm->nested.nested_run_pending)
1134 kvm_state.flags |= KVM_STATE_NESTED_RUN_PENDING;
1135 }
1136
1137 if (gif_set(svm))
1138 kvm_state.flags |= KVM_STATE_NESTED_GIF_SET;
1139
1140 if (copy_to_user(user_kvm_nested_state, &kvm_state, sizeof(kvm_state)))
1141 return -EFAULT;
1142
1143 if (!is_guest_mode(vcpu))
1144 goto out;
1145
1146 /*
1147 * Copy over the full size of the VMCB rather than just the size
1148 * of the structs.
1149 */
1150 if (clear_user(user_vmcb, KVM_STATE_NESTED_SVM_VMCB_SIZE))
1151 return -EFAULT;
1152 if (copy_to_user(&user_vmcb->control, &svm->nested.ctl,
1153 sizeof(user_vmcb->control)))
1154 return -EFAULT;
Cathy Avery4995a362021-01-13 07:07:52 -05001155 if (copy_to_user(&user_vmcb->save, &svm->vmcb01.ptr->save,
Paolo Bonzinicc440cd2020-05-13 13:36:32 -04001156 sizeof(user_vmcb->save)))
1157 return -EFAULT;
Paolo Bonzinicc440cd2020-05-13 13:36:32 -04001158out:
1159 return kvm_state.size;
1160}
1161
1162static int svm_set_nested_state(struct kvm_vcpu *vcpu,
1163 struct kvm_nested_state __user *user_kvm_nested_state,
1164 struct kvm_nested_state *kvm_state)
1165{
1166 struct vcpu_svm *svm = to_svm(vcpu);
Paolo Bonzinicc440cd2020-05-13 13:36:32 -04001167 struct vmcb __user *user_vmcb = (struct vmcb __user *)
1168 &user_kvm_nested_state->data.svm[0];
Joerg Roedel6ccbd292020-09-07 15:15:02 +02001169 struct vmcb_control_area *ctl;
1170 struct vmcb_save_area *save;
1171 int ret;
Paolo Bonzinicc440cd2020-05-13 13:36:32 -04001172 u32 cr0;
1173
Joerg Roedel6ccbd292020-09-07 15:15:02 +02001174 BUILD_BUG_ON(sizeof(struct vmcb_control_area) + sizeof(struct vmcb_save_area) >
1175 KVM_STATE_NESTED_SVM_VMCB_SIZE);
1176
Paolo Bonzinicc440cd2020-05-13 13:36:32 -04001177 if (kvm_state->format != KVM_STATE_NESTED_FORMAT_SVM)
1178 return -EINVAL;
1179
1180 if (kvm_state->flags & ~(KVM_STATE_NESTED_GUEST_MODE |
1181 KVM_STATE_NESTED_RUN_PENDING |
1182 KVM_STATE_NESTED_GIF_SET))
1183 return -EINVAL;
1184
1185 /*
1186 * If in guest mode, vcpu->arch.efer actually refers to the L2 guest's
1187 * EFER.SVME, but EFER.SVME still has to be 1 for VMRUN to succeed.
1188 */
1189 if (!(vcpu->arch.efer & EFER_SVME)) {
1190 /* GIF=1 and no guest mode are required if SVME=0. */
1191 if (kvm_state->flags != KVM_STATE_NESTED_GIF_SET)
1192 return -EINVAL;
1193 }
1194
1195 /* SMM temporarily disables SVM, so we cannot be in guest mode. */
1196 if (is_smm(vcpu) && (kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE))
1197 return -EINVAL;
1198
1199 if (!(kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE)) {
1200 svm_leave_nested(svm);
Vitaly Kuznetsovd5cd6f32020-09-14 15:37:25 +02001201 svm_set_gif(svm, !!(kvm_state->flags & KVM_STATE_NESTED_GIF_SET));
1202 return 0;
Paolo Bonzinicc440cd2020-05-13 13:36:32 -04001203 }
1204
1205 if (!page_address_valid(vcpu, kvm_state->hdr.svm.vmcb_pa))
1206 return -EINVAL;
1207 if (kvm_state->size < sizeof(*kvm_state) + KVM_STATE_NESTED_SVM_VMCB_SIZE)
1208 return -EINVAL;
Paolo Bonzinicc440cd2020-05-13 13:36:32 -04001209
Joerg Roedel6ccbd292020-09-07 15:15:02 +02001210 ret = -ENOMEM;
1211 ctl = kzalloc(sizeof(*ctl), GFP_KERNEL);
1212 save = kzalloc(sizeof(*save), GFP_KERNEL);
1213 if (!ctl || !save)
1214 goto out_free;
1215
1216 ret = -EFAULT;
1217 if (copy_from_user(ctl, &user_vmcb->control, sizeof(*ctl)))
1218 goto out_free;
1219 if (copy_from_user(save, &user_vmcb->save, sizeof(*save)))
1220 goto out_free;
1221
1222 ret = -EINVAL;
1223 if (!nested_vmcb_check_controls(ctl))
1224 goto out_free;
Paolo Bonzinicc440cd2020-05-13 13:36:32 -04001225
1226 /*
1227 * Processor state contains L2 state. Check that it is
1228 * valid for guest mode (see nested_vmcb_checks).
1229 */
1230 cr0 = kvm_read_cr0(vcpu);
1231 if (((cr0 & X86_CR0_CD) == 0) && (cr0 & X86_CR0_NW))
Joerg Roedel6ccbd292020-09-07 15:15:02 +02001232 goto out_free;
Paolo Bonzinicc440cd2020-05-13 13:36:32 -04001233
1234 /*
1235 * Validate host state saved from before VMRUN (see
1236 * nested_svm_check_permissions).
1237 * TODO: validate reserved bits for all saved state.
1238 */
Joerg Roedel6ccbd292020-09-07 15:15:02 +02001239 if (!(save->cr0 & X86_CR0_PG))
1240 goto out_free;
Paolo Bonzinicc440cd2020-05-13 13:36:32 -04001241
1242 /*
Cathy Avery4995a362021-01-13 07:07:52 -05001243 * All checks done, we can enter guest mode. Userspace provides
1244 * vmcb12.control, which will be combined with L1 and stored into
1245 * vmcb02, and the L1 save state which we store in vmcb01.
1246 * L2 registers if needed are moved from the current VMCB to VMCB02.
Paolo Bonzinicc440cd2020-05-13 13:36:32 -04001247 */
Maxim Levitsky81f76ad2021-01-07 11:38:52 +02001248
1249 svm->nested.nested_run_pending =
1250 !!(kvm_state->flags & KVM_STATE_NESTED_RUN_PENDING);
1251
Maxim Levitsky0dd16b52020-08-27 20:11:39 +03001252 svm->nested.vmcb12_gpa = kvm_state->hdr.svm.vmcb_pa;
Cathy Avery4995a362021-01-13 07:07:52 -05001253 if (svm->current_vmcb == &svm->vmcb01)
1254 svm->nested.vmcb02.ptr->save = svm->vmcb01.ptr->save;
Paolo Bonzinic08f3902020-11-17 02:51:35 -05001255
1256 svm->vmcb01.ptr->save.es = save->es;
1257 svm->vmcb01.ptr->save.cs = save->cs;
1258 svm->vmcb01.ptr->save.ss = save->ss;
1259 svm->vmcb01.ptr->save.ds = save->ds;
1260 svm->vmcb01.ptr->save.gdtr = save->gdtr;
1261 svm->vmcb01.ptr->save.idtr = save->idtr;
1262 svm->vmcb01.ptr->save.rflags = save->rflags | X86_EFLAGS_FIXED;
1263 svm->vmcb01.ptr->save.efer = save->efer;
1264 svm->vmcb01.ptr->save.cr0 = save->cr0;
1265 svm->vmcb01.ptr->save.cr3 = save->cr3;
1266 svm->vmcb01.ptr->save.cr4 = save->cr4;
1267 svm->vmcb01.ptr->save.rax = save->rax;
1268 svm->vmcb01.ptr->save.rsp = save->rsp;
1269 svm->vmcb01.ptr->save.rip = save->rip;
1270 svm->vmcb01.ptr->save.cpl = 0;
1271
Paolo Bonzini9e8f0fb2020-11-17 05:15:41 -05001272 nested_load_control_from_vmcb12(svm, ctl);
Cathy Avery4995a362021-01-13 07:07:52 -05001273
1274 svm_switch_vmcb(svm, &svm->nested.vmcb02);
1275
Paolo Bonzini9e8f0fb2020-11-17 05:15:41 -05001276 nested_vmcb02_prepare_control(svm);
Paolo Bonzinicc440cd2020-05-13 13:36:32 -04001277
Paolo Bonzinia7d5c7c2020-09-22 07:43:14 -04001278 kvm_make_request(KVM_REQ_GET_NESTED_STATE_PAGES, vcpu);
Joerg Roedel6ccbd292020-09-07 15:15:02 +02001279 ret = 0;
1280out_free:
1281 kfree(save);
1282 kfree(ctl);
1283
1284 return ret;
Paolo Bonzinicc440cd2020-05-13 13:36:32 -04001285}
1286
Paolo Bonzini33b22172020-04-17 10:24:18 -04001287struct kvm_x86_nested_ops svm_nested_ops = {
1288 .check_events = svm_check_nested_events,
Paolo Bonzinia7d5c7c2020-09-22 07:43:14 -04001289 .get_nested_state_pages = svm_get_nested_state_pages,
Paolo Bonzinicc440cd2020-05-13 13:36:32 -04001290 .get_state = svm_get_nested_state,
1291 .set_state = svm_set_nested_state,
Paolo Bonzini33b22172020-04-17 10:24:18 -04001292};