blob: 35947464ee2a39cd43fcf66ee58bce52893c914f [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;
Cathy Avery4995a362021-01-13 07:07:52 -0500101 kvm_init_shadow_npt_mmu(vcpu, X86_CR0_PG, svm->vmcb01.ptr->save.cr4,
102 svm->vmcb01.ptr->save.efer,
Vitaly Kuznetsov0f04a2a2020-07-10 16:11:49 +0200103 svm->nested.ctl.nested_cr3);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100104 vcpu->arch.mmu->get_guest_pgd = nested_svm_get_tdp_cr3;
105 vcpu->arch.mmu->get_pdptr = nested_svm_get_tdp_pdptr;
106 vcpu->arch.mmu->inject_page_fault = nested_svm_inject_npf_exit;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100107 reset_shadow_zero_bits_mask(vcpu, vcpu->arch.mmu);
108 vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu;
109}
110
111static void nested_svm_uninit_mmu_context(struct kvm_vcpu *vcpu)
112{
113 vcpu->arch.mmu = &vcpu->arch.root_mmu;
114 vcpu->arch.walk_mmu = &vcpu->arch.root_mmu;
115}
116
117void recalc_intercepts(struct vcpu_svm *svm)
118{
Paolo Bonzinie670bf62020-05-13 13:16:12 -0400119 struct vmcb_control_area *c, *h, *g;
Babu Mogerc45ad722020-09-11 14:27:58 -0500120 unsigned int i;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100121
Joerg Roedel06e78522020-06-25 10:03:23 +0200122 vmcb_mark_dirty(svm->vmcb, VMCB_INTERCEPTS);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100123
124 if (!is_guest_mode(&svm->vcpu))
125 return;
126
127 c = &svm->vmcb->control;
Cathy Avery4995a362021-01-13 07:07:52 -0500128 h = &svm->vmcb01.ptr->control;
Paolo Bonzinie670bf62020-05-13 13:16:12 -0400129 g = &svm->nested.ctl;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100130
Babu Mogerc45ad722020-09-11 14:27:58 -0500131 for (i = 0; i < MAX_INTERCEPT; i++)
132 c->intercepts[i] = h->intercepts[i];
133
Paolo Bonzinie9fd7612020-05-13 13:28:23 -0400134 if (g->int_ctl & V_INTR_MASKING_MASK) {
Joerg Roedel883b0a92020-03-24 10:41:52 +0100135 /* We only want the cr8 intercept bits of L1 */
Babu Moger03bfeeb2020-09-11 14:28:05 -0500136 vmcb_clr_intercept(c, INTERCEPT_CR8_READ);
137 vmcb_clr_intercept(c, INTERCEPT_CR8_WRITE);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100138
139 /*
140 * Once running L2 with HF_VINTR_MASK, EFLAGS.IF does not
141 * affect any interrupt we may want to inject; therefore,
142 * interrupt window vmexits are irrelevant to L0.
143 */
Babu Mogerc62e2e92020-09-11 14:28:28 -0500144 vmcb_clr_intercept(c, INTERCEPT_VINTR);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100145 }
146
147 /* We don't want to see VMMCALLs from a nested guest */
Babu Mogerc62e2e92020-09-11 14:28:28 -0500148 vmcb_clr_intercept(c, INTERCEPT_VMMCALL);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100149
Babu Mogerc45ad722020-09-11 14:27:58 -0500150 for (i = 0; i < MAX_INTERCEPT; i++)
151 c->intercepts[i] |= g->intercepts[i];
Joerg Roedel883b0a92020-03-24 10:41:52 +0100152}
153
Paolo Bonzini2f675912020-05-18 15:21:22 -0400154static void copy_vmcb_control_area(struct vmcb_control_area *dst,
155 struct vmcb_control_area *from)
Joerg Roedel883b0a92020-03-24 10:41:52 +0100156{
Babu Mogerc45ad722020-09-11 14:27:58 -0500157 unsigned int i;
158
159 for (i = 0; i < MAX_INTERCEPT; i++)
160 dst->intercepts[i] = from->intercepts[i];
161
Joerg Roedel883b0a92020-03-24 10:41:52 +0100162 dst->iopm_base_pa = from->iopm_base_pa;
163 dst->msrpm_base_pa = from->msrpm_base_pa;
164 dst->tsc_offset = from->tsc_offset;
Paolo Bonzini6c0238c2020-05-20 08:02:17 -0400165 /* asid not copied, it is handled manually for svm->vmcb. */
Joerg Roedel883b0a92020-03-24 10:41:52 +0100166 dst->tlb_ctl = from->tlb_ctl;
167 dst->int_ctl = from->int_ctl;
168 dst->int_vector = from->int_vector;
169 dst->int_state = from->int_state;
170 dst->exit_code = from->exit_code;
171 dst->exit_code_hi = from->exit_code_hi;
172 dst->exit_info_1 = from->exit_info_1;
173 dst->exit_info_2 = from->exit_info_2;
174 dst->exit_int_info = from->exit_int_info;
175 dst->exit_int_info_err = from->exit_int_info_err;
176 dst->nested_ctl = from->nested_ctl;
177 dst->event_inj = from->event_inj;
178 dst->event_inj_err = from->event_inj_err;
179 dst->nested_cr3 = from->nested_cr3;
180 dst->virt_ext = from->virt_ext;
181 dst->pause_filter_count = from->pause_filter_count;
182 dst->pause_filter_thresh = from->pause_filter_thresh;
183}
184
185static bool nested_svm_vmrun_msrpm(struct vcpu_svm *svm)
186{
187 /*
188 * This function merges the msr permission bitmaps of kvm and the
189 * nested vmcb. It is optimized in that it only merges the parts where
190 * the kvm msr permission bitmap may contain zero bits
191 */
192 int i;
193
Babu Mogerc62e2e92020-09-11 14:28:28 -0500194 if (!(vmcb_is_intercept(&svm->nested.ctl, INTERCEPT_MSR_PROT)))
Joerg Roedel883b0a92020-03-24 10:41:52 +0100195 return true;
196
197 for (i = 0; i < MSRPM_OFFSETS; i++) {
198 u32 value, p;
199 u64 offset;
200
201 if (msrpm_offsets[i] == 0xffffffff)
202 break;
203
204 p = msrpm_offsets[i];
Paolo Bonzinie670bf62020-05-13 13:16:12 -0400205 offset = svm->nested.ctl.msrpm_base_pa + (p * 4);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100206
207 if (kvm_vcpu_read_guest(&svm->vcpu, offset, &value, 4))
208 return false;
209
210 svm->nested.msrpm[p] = svm->msrpm[p] | value;
211 }
212
213 svm->vmcb->control.msrpm_base_pa = __sme_set(__pa(svm->nested.msrpm));
214
215 return true;
216}
217
Paolo Bonzinia7d5c7c2020-09-22 07:43:14 -0400218static bool svm_get_nested_state_pages(struct kvm_vcpu *vcpu)
219{
220 struct vcpu_svm *svm = to_svm(vcpu);
Maxim Levitskyf2c7ef32021-01-07 11:38:51 +0200221
Paolo Bonzini9a78e152021-01-08 11:43:08 -0500222 if (WARN_ON(!is_guest_mode(vcpu)))
223 return true;
224
Paolo Bonzinia7d5c7c2020-09-22 07:43:14 -0400225 if (!nested_svm_vmrun_msrpm(svm)) {
226 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
227 vcpu->run->internal.suberror =
228 KVM_INTERNAL_ERROR_EMULATION;
229 vcpu->run->internal.ndata = 0;
230 return false;
231 }
232
233 return true;
234}
235
Paolo Bonzinica46d732020-05-18 13:02:15 -0400236static bool nested_vmcb_check_controls(struct vmcb_control_area *control)
237{
Sean Christopherson11f0cbf2021-02-03 16:01:17 -0800238 if (CC(!vmcb_is_intercept(control, INTERCEPT_VMRUN)))
Paolo Bonzinica46d732020-05-18 13:02:15 -0400239 return false;
240
Sean Christopherson11f0cbf2021-02-03 16:01:17 -0800241 if (CC(control->asid == 0))
Paolo Bonzinica46d732020-05-18 13:02:15 -0400242 return false;
243
Sean Christopherson11f0cbf2021-02-03 16:01:17 -0800244 if (CC((control->nested_ctl & SVM_NESTED_CTL_NP_ENABLE) && !npt_enabled))
Paolo Bonzinica46d732020-05-18 13:02:15 -0400245 return false;
246
247 return true;
248}
249
Paolo Bonzini63129752021-03-02 14:40:39 -0500250static bool nested_vmcb_check_cr3_cr4(struct kvm_vcpu *vcpu,
Krish Sadhukhan6906e062020-10-06 19:06:52 +0000251 struct vmcb_save_area *save)
Joerg Roedel883b0a92020-03-24 10:41:52 +0100252{
Krish Sadhukhan6906e062020-10-06 19:06:52 +0000253 /*
254 * These checks are also performed by KVM_SET_SREGS,
255 * except that EFER.LMA is not checked by SVM against
256 * CR0.PG && EFER.LME.
257 */
258 if ((save->efer & EFER_LME) && (save->cr0 & X86_CR0_PG)) {
Sean Christopherson11f0cbf2021-02-03 16:01:17 -0800259 if (CC(!(save->cr4 & X86_CR4_PAE)) ||
260 CC(!(save->cr0 & X86_CR0_PE)) ||
261 CC(kvm_vcpu_is_illegal_gpa(vcpu, save->cr3)))
Krish Sadhukhan761e4162020-07-08 00:39:56 +0000262 return false;
263 }
Krish Sadhukhan6906e062020-10-06 19:06:52 +0000264
Sean Christopherson11f0cbf2021-02-03 16:01:17 -0800265 if (CC(!kvm_is_valid_cr4(vcpu, save->cr4)))
266 return false;
267
268 return true;
Krish Sadhukhan6906e062020-10-06 19:06:52 +0000269}
270
271/* Common checks that apply to both L1 and L2 state. */
Paolo Bonzini63129752021-03-02 14:40:39 -0500272static bool nested_vmcb_valid_sregs(struct kvm_vcpu *vcpu,
Krish Sadhukhan6906e062020-10-06 19:06:52 +0000273 struct vmcb_save_area *save)
274{
Sean Christopherson11f0cbf2021-02-03 16:01:17 -0800275 if (CC(!(save->efer & EFER_SVME)))
Krish Sadhukhan6906e062020-10-06 19:06:52 +0000276 return false;
277
Sean Christopherson11f0cbf2021-02-03 16:01:17 -0800278 if (CC((save->cr0 & X86_CR0_CD) == 0 && (save->cr0 & X86_CR0_NW)) ||
279 CC(save->cr0 & ~0xffffffffULL))
Krish Sadhukhan6906e062020-10-06 19:06:52 +0000280 return false;
281
Sean Christopherson11f0cbf2021-02-03 16:01:17 -0800282 if (CC(!kvm_dr6_valid(save->dr6)) || CC(!kvm_dr7_valid(save->dr7)))
Krish Sadhukhan6906e062020-10-06 19:06:52 +0000283 return false;
284
Paolo Bonzini63129752021-03-02 14:40:39 -0500285 if (!nested_vmcb_check_cr3_cr4(vcpu, save))
Krish Sadhukhan6906e062020-10-06 19:06:52 +0000286 return false;
287
Paolo Bonzini63129752021-03-02 14:40:39 -0500288 if (CC(!kvm_valid_efer(vcpu, save->efer)))
Krish Sadhukhan6906e062020-10-06 19:06:52 +0000289 return false;
290
291 return true;
292}
293
Paolo Bonzini63129752021-03-02 14:40:39 -0500294static bool nested_vmcb_checks(struct kvm_vcpu *vcpu, struct vmcb *vmcb12)
Krish Sadhukhan6906e062020-10-06 19:06:52 +0000295{
Paolo Bonzini63129752021-03-02 14:40:39 -0500296 if (!nested_vmcb_valid_sregs(vcpu, &vmcb12->save))
Krish Sadhukhan761e4162020-07-08 00:39:56 +0000297 return false;
298
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300299 return nested_vmcb_check_controls(&vmcb12->control);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100300}
301
Paolo Bonzini9e8f0fb2020-11-17 05:15:41 -0500302static void nested_load_control_from_vmcb12(struct vcpu_svm *svm,
303 struct vmcb_control_area *control)
Paolo Bonzini3e06f012020-05-13 13:07:26 -0400304{
Paolo Bonzinie670bf62020-05-13 13:16:12 -0400305 copy_vmcb_control_area(&svm->nested.ctl, control);
Paolo Bonzini3e06f012020-05-13 13:07:26 -0400306
Paolo Bonzinicc440cd2020-05-13 13:36:32 -0400307 /* Copy it here because nested_svm_check_controls will check it. */
308 svm->nested.ctl.asid = control->asid;
Paolo Bonzinie670bf62020-05-13 13:16:12 -0400309 svm->nested.ctl.msrpm_base_pa &= ~0x0fffULL;
310 svm->nested.ctl.iopm_base_pa &= ~0x0fffULL;
Paolo Bonzini3e06f012020-05-13 13:07:26 -0400311}
312
Paolo Bonzini2d8a42b2020-05-22 03:50:14 -0400313/*
314 * Synchronize fields that are written by the processor, so that
Paolo Bonzini9e8f0fb2020-11-17 05:15:41 -0500315 * they can be copied back into the vmcb12.
Paolo Bonzini2d8a42b2020-05-22 03:50:14 -0400316 */
Paolo Bonzini9e8f0fb2020-11-17 05:15:41 -0500317void nested_sync_control_from_vmcb02(struct vcpu_svm *svm)
Paolo Bonzini2d8a42b2020-05-22 03:50:14 -0400318{
319 u32 mask;
320 svm->nested.ctl.event_inj = svm->vmcb->control.event_inj;
321 svm->nested.ctl.event_inj_err = svm->vmcb->control.event_inj_err;
322
323 /* Only a few fields of int_ctl are written by the processor. */
324 mask = V_IRQ_MASK | V_TPR_MASK;
325 if (!(svm->nested.ctl.int_ctl & V_INTR_MASKING_MASK) &&
Joerg Roedela284ba52020-06-25 10:03:24 +0200326 svm_is_intercept(svm, INTERCEPT_VINTR)) {
Paolo Bonzini2d8a42b2020-05-22 03:50:14 -0400327 /*
328 * In order to request an interrupt window, L0 is usurping
329 * svm->vmcb->control.int_ctl and possibly setting V_IRQ
330 * even if it was clear in L1's VMCB. Restoring it would be
331 * wrong. However, in this case V_IRQ will remain true until
332 * interrupt_window_interception calls svm_clear_vintr and
333 * restores int_ctl. We can just leave it aside.
334 */
335 mask &= ~V_IRQ_MASK;
336 }
337 svm->nested.ctl.int_ctl &= ~mask;
338 svm->nested.ctl.int_ctl |= svm->vmcb->control.int_ctl & mask;
339}
340
Paolo Bonzini36e2e982020-05-22 06:04:57 -0400341/*
342 * Transfer any event that L0 or L1 wanted to inject into L2 to
343 * EXIT_INT_INFO.
344 */
Paolo Bonzini9e8f0fb2020-11-17 05:15:41 -0500345static void nested_save_pending_event_to_vmcb12(struct vcpu_svm *svm,
346 struct vmcb *vmcb12)
Paolo Bonzini36e2e982020-05-22 06:04:57 -0400347{
348 struct kvm_vcpu *vcpu = &svm->vcpu;
349 u32 exit_int_info = 0;
350 unsigned int nr;
351
352 if (vcpu->arch.exception.injected) {
353 nr = vcpu->arch.exception.nr;
354 exit_int_info = nr | SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_EXEPT;
355
356 if (vcpu->arch.exception.has_error_code) {
357 exit_int_info |= SVM_EVTINJ_VALID_ERR;
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300358 vmcb12->control.exit_int_info_err =
Paolo Bonzini36e2e982020-05-22 06:04:57 -0400359 vcpu->arch.exception.error_code;
360 }
361
362 } else if (vcpu->arch.nmi_injected) {
363 exit_int_info = SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_NMI;
364
365 } else if (vcpu->arch.interrupt.injected) {
366 nr = vcpu->arch.interrupt.nr;
367 exit_int_info = nr | SVM_EVTINJ_VALID;
368
369 if (vcpu->arch.interrupt.soft)
370 exit_int_info |= SVM_EVTINJ_TYPE_SOFT;
371 else
372 exit_int_info |= SVM_EVTINJ_TYPE_INTR;
373 }
374
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300375 vmcb12->control.exit_int_info = exit_int_info;
Paolo Bonzini36e2e982020-05-22 06:04:57 -0400376}
377
Vitaly Kuznetsov62156f62020-07-10 16:11:53 +0200378static inline bool nested_npt_enabled(struct vcpu_svm *svm)
379{
380 return svm->nested.ctl.nested_ctl & SVM_NESTED_CTL_NP_ENABLE;
381}
382
383/*
Vitaly Kuznetsovd82aaef2020-07-10 16:11:56 +0200384 * Load guest's/host's cr3 on nested vmentry or vmexit. @nested_npt is true
385 * if we are emulating VM-Entry into a guest with NPT enabled.
Vitaly Kuznetsov62156f62020-07-10 16:11:53 +0200386 */
387static int nested_svm_load_cr3(struct kvm_vcpu *vcpu, unsigned long cr3,
388 bool nested_npt)
389{
Sean Christopherson11f0cbf2021-02-03 16:01:17 -0800390 if (CC(kvm_vcpu_is_illegal_gpa(vcpu, cr3)))
Vitaly Kuznetsova506fdd2020-07-10 16:11:55 +0200391 return -EINVAL;
392
393 if (!nested_npt && is_pae_paging(vcpu) &&
394 (cr3 != kvm_read_cr3(vcpu) || pdptrs_changed(vcpu))) {
Sean Christopherson11f0cbf2021-02-03 16:01:17 -0800395 if (CC(!load_pdptrs(vcpu, vcpu->arch.walk_mmu, cr3)))
Vitaly Kuznetsova506fdd2020-07-10 16:11:55 +0200396 return -EINVAL;
397 }
398
399 /*
400 * TODO: optimize unconditional TLB flush/MMU sync here and in
401 * kvm_init_shadow_npt_mmu().
402 */
403 if (!nested_npt)
404 kvm_mmu_new_pgd(vcpu, cr3, false, false);
405
406 vcpu->arch.cr3 = cr3;
407 kvm_register_mark_available(vcpu, VCPU_EXREG_CR3);
408
409 kvm_init_mmu(vcpu, false);
410
411 return 0;
Vitaly Kuznetsov62156f62020-07-10 16:11:53 +0200412}
413
Cathy Avery4995a362021-01-13 07:07:52 -0500414void nested_vmcb02_compute_g_pat(struct vcpu_svm *svm)
415{
416 if (!svm->nested.vmcb02.ptr)
417 return;
418
419 /* FIXME: merge g_pat from vmcb01 and vmcb12. */
420 svm->nested.vmcb02.ptr->save.g_pat = svm->vmcb01.ptr->save.g_pat;
421}
422
Paolo Bonzini9e8f0fb2020-11-17 05:15:41 -0500423static void nested_vmcb02_prepare_save(struct vcpu_svm *svm, struct vmcb *vmcb12)
Joerg Roedel883b0a92020-03-24 10:41:52 +0100424{
Cathy Avery4995a362021-01-13 07:07:52 -0500425 nested_vmcb02_compute_g_pat(svm);
426
Joerg Roedel883b0a92020-03-24 10:41:52 +0100427 /* Load the nested guest state */
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300428 svm->vmcb->save.es = vmcb12->save.es;
429 svm->vmcb->save.cs = vmcb12->save.cs;
430 svm->vmcb->save.ss = vmcb12->save.ss;
431 svm->vmcb->save.ds = vmcb12->save.ds;
Paolo Bonzini4bb170a2020-11-16 06:38:19 -0500432 svm->vmcb->save.cpl = vmcb12->save.cpl;
433 vmcb_mark_dirty(svm->vmcb, VMCB_SEG);
434
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300435 svm->vmcb->save.gdtr = vmcb12->save.gdtr;
436 svm->vmcb->save.idtr = vmcb12->save.idtr;
Paolo Bonzini4bb170a2020-11-16 06:38:19 -0500437 vmcb_mark_dirty(svm->vmcb, VMCB_DT);
438
Paolo Bonzini8cce12b2020-11-27 12:46:36 -0500439 kvm_set_rflags(&svm->vcpu, vmcb12->save.rflags | X86_EFLAGS_FIXED);
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300440 svm_set_efer(&svm->vcpu, vmcb12->save.efer);
441 svm_set_cr0(&svm->vcpu, vmcb12->save.cr0);
442 svm_set_cr4(&svm->vcpu, vmcb12->save.cr4);
Paolo Bonzini4bb170a2020-11-16 06:38:19 -0500443
444 svm->vcpu.arch.cr2 = vmcb12->save.cr2;
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300445 kvm_rax_write(&svm->vcpu, vmcb12->save.rax);
446 kvm_rsp_write(&svm->vcpu, vmcb12->save.rsp);
447 kvm_rip_write(&svm->vcpu, vmcb12->save.rip);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100448
449 /* In case we don't even reach vcpu_run, the fields are not updated */
Paolo Bonzini4bb170a2020-11-16 06:38:19 -0500450 svm->vmcb->save.cr2 = svm->vcpu.arch.cr2;
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300451 svm->vmcb->save.rax = vmcb12->save.rax;
452 svm->vmcb->save.rsp = vmcb12->save.rsp;
453 svm->vmcb->save.rip = vmcb12->save.rip;
Paolo Bonzini4bb170a2020-11-16 06:38:19 -0500454
Paolo Bonzini8cce12b2020-11-27 12:46:36 -0500455 svm->vmcb->save.dr7 = vmcb12->save.dr7 | DR7_FIXED_1;
Chenyi Qiang9a3ecd52021-02-02 17:04:31 +0800456 svm->vcpu.arch.dr6 = vmcb12->save.dr6 | DR6_ACTIVE_LOW;
Paolo Bonzini4bb170a2020-11-16 06:38:19 -0500457 vmcb_mark_dirty(svm->vmcb, VMCB_DR);
Paolo Bonzinif241d712020-05-18 10:56:43 -0400458}
Joerg Roedel883b0a92020-03-24 10:41:52 +0100459
Paolo Bonzini9e8f0fb2020-11-17 05:15:41 -0500460static void nested_vmcb02_prepare_control(struct vcpu_svm *svm)
Paolo Bonzinif241d712020-05-18 10:56:43 -0400461{
Paolo Bonzini91b71302020-05-22 12:28:52 -0400462 const u32 mask = V_INTR_MASKING_MASK | V_GIF_ENABLE_MASK | V_GIF_MASK;
Vitaly Kuznetsov62156f62020-07-10 16:11:53 +0200463
Paolo Bonzini7c3ecfc2020-11-16 06:13:15 -0500464 /*
465 * Filled at exit: exit_code, exit_code_hi, exit_info_1, exit_info_2,
466 * exit_int_info, exit_int_info_err, next_rip, insn_len, insn_bytes.
467 */
Cathy Avery4995a362021-01-13 07:07:52 -0500468
Paolo Bonzini7c3ecfc2020-11-16 06:13:15 -0500469 /*
470 * Also covers avic_vapic_bar, avic_backing_page, avic_logical_id,
471 * avic_physical_id.
472 */
473 WARN_ON(svm->vmcb01.ptr->control.int_ctl & AVIC_ENABLE_MASK);
474
475 /* Copied from vmcb01. msrpm_base can be overwritten later. */
476 svm->vmcb->control.nested_ctl = svm->vmcb01.ptr->control.nested_ctl;
477 svm->vmcb->control.iopm_base_pa = svm->vmcb01.ptr->control.iopm_base_pa;
478 svm->vmcb->control.msrpm_base_pa = svm->vmcb01.ptr->control.msrpm_base_pa;
479
480 /* Done at vmrun: asid. */
481
482 /* Also overwritten later if necessary. */
483 svm->vmcb->control.tlb_ctl = TLB_CONTROL_DO_NOTHING;
484
485 /* nested_cr3. */
Vitaly Kuznetsov62156f62020-07-10 16:11:53 +0200486 if (nested_npt_enabled(svm))
Paolo Bonzini69cb8772020-05-22 05:27:46 -0400487 nested_svm_init_mmu_context(&svm->vcpu);
488
Paolo Bonzini18fc6c52020-05-18 11:07:08 -0400489 svm->vmcb->control.tsc_offset = svm->vcpu.arch.tsc_offset =
Paolo Bonzinie670bf62020-05-13 13:16:12 -0400490 svm->vcpu.arch.l1_tsc_offset + svm->nested.ctl.tsc_offset;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100491
Paolo Bonzini91b71302020-05-22 12:28:52 -0400492 svm->vmcb->control.int_ctl =
493 (svm->nested.ctl.int_ctl & ~mask) |
Cathy Avery4995a362021-01-13 07:07:52 -0500494 (svm->vmcb01.ptr->control.int_ctl & mask);
Paolo Bonzini91b71302020-05-22 12:28:52 -0400495
Paolo Bonzinie670bf62020-05-13 13:16:12 -0400496 svm->vmcb->control.virt_ext = svm->nested.ctl.virt_ext;
497 svm->vmcb->control.int_vector = svm->nested.ctl.int_vector;
498 svm->vmcb->control.int_state = svm->nested.ctl.int_state;
499 svm->vmcb->control.event_inj = svm->nested.ctl.event_inj;
500 svm->vmcb->control.event_inj_err = svm->nested.ctl.event_inj_err;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100501
Paolo Bonzinie670bf62020-05-13 13:16:12 -0400502 svm->vmcb->control.pause_filter_count = svm->nested.ctl.pause_filter_count;
503 svm->vmcb->control.pause_filter_thresh = svm->nested.ctl.pause_filter_thresh;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100504
Joerg Roedel883b0a92020-03-24 10:41:52 +0100505 /* Enter Guest-Mode */
506 enter_guest_mode(&svm->vcpu);
507
508 /*
Paolo Bonzini4bb170a2020-11-16 06:38:19 -0500509 * Merge guest and host intercepts - must be called with vcpu in
510 * guest-mode to take effect.
Joerg Roedel883b0a92020-03-24 10:41:52 +0100511 */
512 recalc_intercepts(svm);
Paolo Bonzinif241d712020-05-18 10:56:43 -0400513}
514
Paolo Bonzini63129752021-03-02 14:40:39 -0500515int enter_svm_guest_mode(struct kvm_vcpu *vcpu, u64 vmcb12_gpa,
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300516 struct vmcb *vmcb12)
Paolo Bonzinif241d712020-05-18 10:56:43 -0400517{
Paolo Bonzini63129752021-03-02 14:40:39 -0500518 struct vcpu_svm *svm = to_svm(vcpu);
Vitaly Kuznetsova506fdd2020-07-10 16:11:55 +0200519 int ret;
520
Maxim Levitsky954f4192021-02-17 16:57:13 +0200521 trace_kvm_nested_vmrun(svm->vmcb->save.rip, vmcb12_gpa,
522 vmcb12->save.rip,
523 vmcb12->control.int_ctl,
524 vmcb12->control.event_inj,
525 vmcb12->control.nested_ctl);
526
527 trace_kvm_nested_intercepts(vmcb12->control.intercepts[INTERCEPT_CR] & 0xffff,
528 vmcb12->control.intercepts[INTERCEPT_CR] >> 16,
529 vmcb12->control.intercepts[INTERCEPT_EXCEPTION],
530 vmcb12->control.intercepts[INTERCEPT_WORD3],
531 vmcb12->control.intercepts[INTERCEPT_WORD4],
532 vmcb12->control.intercepts[INTERCEPT_WORD5]);
533
534
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300535 svm->nested.vmcb12_gpa = vmcb12_gpa;
Cathy Avery4995a362021-01-13 07:07:52 -0500536
537 WARN_ON(svm->vmcb == svm->nested.vmcb02.ptr);
538
Paolo Bonzini9e8f0fb2020-11-17 05:15:41 -0500539 nested_load_control_from_vmcb12(svm, &vmcb12->control);
Cathy Avery4995a362021-01-13 07:07:52 -0500540
541 svm_switch_vmcb(svm, &svm->nested.vmcb02);
Paolo Bonzini9e8f0fb2020-11-17 05:15:41 -0500542 nested_vmcb02_prepare_control(svm);
543 nested_vmcb02_prepare_save(svm, vmcb12);
Paolo Bonzinif241d712020-05-18 10:56:43 -0400544
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300545 ret = nested_svm_load_cr3(&svm->vcpu, vmcb12->save.cr3,
Vitaly Kuznetsova506fdd2020-07-10 16:11:55 +0200546 nested_npt_enabled(svm));
547 if (ret)
548 return ret;
549
Paolo Bonzinia04aead2021-02-18 07:16:59 -0500550 if (!npt_enabled)
Paolo Bonzini63129752021-03-02 14:40:39 -0500551 vcpu->arch.mmu->inject_page_fault = svm_inject_page_fault_nested;
Paolo Bonzinia04aead2021-02-18 07:16:59 -0500552
Paolo Bonziniffdf7f92020-05-22 12:18:27 -0400553 svm_set_gif(svm, true);
Vitaly Kuznetsov59cd9bc2020-07-10 16:11:52 +0200554
555 return 0;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100556}
557
Paolo Bonzini63129752021-03-02 14:40:39 -0500558int nested_svm_vmrun(struct kvm_vcpu *vcpu)
Joerg Roedel883b0a92020-03-24 10:41:52 +0100559{
Paolo Bonzini63129752021-03-02 14:40:39 -0500560 struct vcpu_svm *svm = to_svm(vcpu);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100561 int ret;
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300562 struct vmcb *vmcb12;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100563 struct kvm_host_map map;
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300564 u64 vmcb12_gpa;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100565
Paolo Bonzini63129752021-03-02 14:40:39 -0500566 ++vcpu->stat.nested_run;
Dongli Zhang43c11d92021-03-05 14:57:47 -0800567
Paolo Bonzini63129752021-03-02 14:40:39 -0500568 if (is_smm(vcpu)) {
569 kvm_queue_exception(vcpu, UD_VECTOR);
Paolo Bonzini7c67f5462020-04-23 10:52:48 -0400570 return 1;
571 }
Joerg Roedel883b0a92020-03-24 10:41:52 +0100572
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300573 vmcb12_gpa = svm->vmcb->save.rax;
Paolo Bonzini63129752021-03-02 14:40:39 -0500574 ret = kvm_vcpu_map(vcpu, gpa_to_gfn(vmcb12_gpa), &map);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100575 if (ret == -EINVAL) {
Paolo Bonzini63129752021-03-02 14:40:39 -0500576 kvm_inject_gp(vcpu, 0);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100577 return 1;
578 } else if (ret) {
Paolo Bonzini63129752021-03-02 14:40:39 -0500579 return kvm_skip_emulated_instruction(vcpu);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100580 }
581
Paolo Bonzini63129752021-03-02 14:40:39 -0500582 ret = kvm_skip_emulated_instruction(vcpu);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100583
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300584 vmcb12 = map.hva;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100585
Maxim Levitsky2fcf4872020-10-01 14:29:54 +0300586 if (WARN_ON_ONCE(!svm->nested.initialized))
587 return -EINVAL;
588
Paolo Bonzini63129752021-03-02 14:40:39 -0500589 if (!nested_vmcb_checks(vcpu, vmcb12)) {
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300590 vmcb12->control.exit_code = SVM_EXIT_ERR;
591 vmcb12->control.exit_code_hi = 0;
592 vmcb12->control.exit_info_1 = 0;
593 vmcb12->control.exit_info_2 = 0;
Paolo Bonzini69c9dfa2020-05-13 12:57:26 -0400594 goto out;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100595 }
596
Joerg Roedel883b0a92020-03-24 10:41:52 +0100597
598 /* Clear internal status */
Paolo Bonzini63129752021-03-02 14:40:39 -0500599 kvm_clear_exception_queue(vcpu);
600 kvm_clear_interrupt_queue(vcpu);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100601
602 /*
Cathy Avery4995a362021-01-13 07:07:52 -0500603 * Since vmcb01 is not in use, we can use it to store some of the L1
604 * state.
Joerg Roedel883b0a92020-03-24 10:41:52 +0100605 */
Paolo Bonzini63129752021-03-02 14:40:39 -0500606 svm->vmcb01.ptr->save.efer = vcpu->arch.efer;
607 svm->vmcb01.ptr->save.cr0 = kvm_read_cr0(vcpu);
608 svm->vmcb01.ptr->save.cr4 = vcpu->arch.cr4;
609 svm->vmcb01.ptr->save.rflags = kvm_get_rflags(vcpu);
610 svm->vmcb01.ptr->save.rip = kvm_rip_read(vcpu);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100611
Cathy Avery4995a362021-01-13 07:07:52 -0500612 if (!npt_enabled)
Paolo Bonzini63129752021-03-02 14:40:39 -0500613 svm->vmcb01.ptr->save.cr3 = kvm_read_cr3(vcpu);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100614
Paolo Bonzinif74f9412020-04-23 13:22:27 -0400615 svm->nested.nested_run_pending = 1;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100616
Paolo Bonzini63129752021-03-02 14:40:39 -0500617 if (enter_svm_guest_mode(vcpu, vmcb12_gpa, vmcb12))
Vitaly Kuznetsov59cd9bc2020-07-10 16:11:52 +0200618 goto out_exit_err;
Vitaly Kuznetsovebdb3db2020-07-10 16:11:51 +0200619
Vitaly Kuznetsov59cd9bc2020-07-10 16:11:52 +0200620 if (nested_svm_vmrun_msrpm(svm))
621 goto out;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100622
Vitaly Kuznetsov59cd9bc2020-07-10 16:11:52 +0200623out_exit_err:
624 svm->nested.nested_run_pending = 0;
625
626 svm->vmcb->control.exit_code = SVM_EXIT_ERR;
627 svm->vmcb->control.exit_code_hi = 0;
628 svm->vmcb->control.exit_info_1 = 0;
629 svm->vmcb->control.exit_info_2 = 0;
630
631 nested_svm_vmexit(svm);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100632
Paolo Bonzini69c9dfa2020-05-13 12:57:26 -0400633out:
Paolo Bonzini63129752021-03-02 14:40:39 -0500634 kvm_vcpu_unmap(vcpu, &map, true);
Paolo Bonzini69c9dfa2020-05-13 12:57:26 -0400635
Joerg Roedel883b0a92020-03-24 10:41:52 +0100636 return ret;
637}
638
639void nested_svm_vmloadsave(struct vmcb *from_vmcb, struct vmcb *to_vmcb)
640{
641 to_vmcb->save.fs = from_vmcb->save.fs;
642 to_vmcb->save.gs = from_vmcb->save.gs;
643 to_vmcb->save.tr = from_vmcb->save.tr;
644 to_vmcb->save.ldtr = from_vmcb->save.ldtr;
645 to_vmcb->save.kernel_gs_base = from_vmcb->save.kernel_gs_base;
646 to_vmcb->save.star = from_vmcb->save.star;
647 to_vmcb->save.lstar = from_vmcb->save.lstar;
648 to_vmcb->save.cstar = from_vmcb->save.cstar;
649 to_vmcb->save.sfmask = from_vmcb->save.sfmask;
650 to_vmcb->save.sysenter_cs = from_vmcb->save.sysenter_cs;
651 to_vmcb->save.sysenter_esp = from_vmcb->save.sysenter_esp;
652 to_vmcb->save.sysenter_eip = from_vmcb->save.sysenter_eip;
653}
654
655int nested_svm_vmexit(struct vcpu_svm *svm)
656{
Paolo Bonzini63129752021-03-02 14:40:39 -0500657 struct kvm_vcpu *vcpu = &svm->vcpu;
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300658 struct vmcb *vmcb12;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100659 struct vmcb *vmcb = svm->vmcb;
660 struct kvm_host_map map;
Paolo Bonzini63129752021-03-02 14:40:39 -0500661 int rc;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100662
Sean Christophersoncb6a32c2021-03-02 09:45:14 -0800663 /* Triple faults in L2 should never escape. */
664 WARN_ON_ONCE(kvm_check_request(KVM_REQ_TRIPLE_FAULT, vcpu));
665
Paolo Bonzini63129752021-03-02 14:40:39 -0500666 rc = kvm_vcpu_map(vcpu, gpa_to_gfn(svm->nested.vmcb12_gpa), &map);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100667 if (rc) {
668 if (rc == -EINVAL)
Paolo Bonzini63129752021-03-02 14:40:39 -0500669 kvm_inject_gp(vcpu, 0);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100670 return 1;
671 }
672
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300673 vmcb12 = map.hva;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100674
675 /* Exit Guest-Mode */
Paolo Bonzini63129752021-03-02 14:40:39 -0500676 leave_guest_mode(vcpu);
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300677 svm->nested.vmcb12_gpa = 0;
Paolo Bonzini2d8a42b2020-05-22 03:50:14 -0400678 WARN_ON_ONCE(svm->nested.nested_run_pending);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100679
Paolo Bonzini63129752021-03-02 14:40:39 -0500680 kvm_clear_request(KVM_REQ_GET_NESTED_STATE_PAGES, vcpu);
Maxim Levitskyf2c7ef32021-01-07 11:38:51 +0200681
Paolo Bonzini38c0b192020-04-23 13:13:09 -0400682 /* in case we halted in L2 */
683 svm->vcpu.arch.mp_state = KVM_MP_STATE_RUNNABLE;
684
Joerg Roedel883b0a92020-03-24 10:41:52 +0100685 /* Give the current vmcb to the guest */
Joerg Roedel883b0a92020-03-24 10:41:52 +0100686
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300687 vmcb12->save.es = vmcb->save.es;
688 vmcb12->save.cs = vmcb->save.cs;
689 vmcb12->save.ss = vmcb->save.ss;
690 vmcb12->save.ds = vmcb->save.ds;
691 vmcb12->save.gdtr = vmcb->save.gdtr;
692 vmcb12->save.idtr = vmcb->save.idtr;
693 vmcb12->save.efer = svm->vcpu.arch.efer;
Paolo Bonzini63129752021-03-02 14:40:39 -0500694 vmcb12->save.cr0 = kvm_read_cr0(vcpu);
695 vmcb12->save.cr3 = kvm_read_cr3(vcpu);
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300696 vmcb12->save.cr2 = vmcb->save.cr2;
697 vmcb12->save.cr4 = svm->vcpu.arch.cr4;
Paolo Bonzini63129752021-03-02 14:40:39 -0500698 vmcb12->save.rflags = kvm_get_rflags(vcpu);
699 vmcb12->save.rip = kvm_rip_read(vcpu);
700 vmcb12->save.rsp = kvm_rsp_read(vcpu);
701 vmcb12->save.rax = kvm_rax_read(vcpu);
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300702 vmcb12->save.dr7 = vmcb->save.dr7;
703 vmcb12->save.dr6 = svm->vcpu.arch.dr6;
704 vmcb12->save.cpl = vmcb->save.cpl;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100705
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300706 vmcb12->control.int_state = vmcb->control.int_state;
707 vmcb12->control.exit_code = vmcb->control.exit_code;
708 vmcb12->control.exit_code_hi = vmcb->control.exit_code_hi;
709 vmcb12->control.exit_info_1 = vmcb->control.exit_info_1;
710 vmcb12->control.exit_info_2 = vmcb->control.exit_info_2;
Paolo Bonzini36e2e982020-05-22 06:04:57 -0400711
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300712 if (vmcb12->control.exit_code != SVM_EXIT_ERR)
Paolo Bonzini9e8f0fb2020-11-17 05:15:41 -0500713 nested_save_pending_event_to_vmcb12(svm, vmcb12);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100714
715 if (svm->nrips_enabled)
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300716 vmcb12->control.next_rip = vmcb->control.next_rip;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100717
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300718 vmcb12->control.int_ctl = svm->nested.ctl.int_ctl;
719 vmcb12->control.tlb_ctl = svm->nested.ctl.tlb_ctl;
720 vmcb12->control.event_inj = svm->nested.ctl.event_inj;
721 vmcb12->control.event_inj_err = svm->nested.ctl.event_inj_err;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100722
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300723 vmcb12->control.pause_filter_count =
Joerg Roedel883b0a92020-03-24 10:41:52 +0100724 svm->vmcb->control.pause_filter_count;
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300725 vmcb12->control.pause_filter_thresh =
Joerg Roedel883b0a92020-03-24 10:41:52 +0100726 svm->vmcb->control.pause_filter_thresh;
727
Cathy Avery4995a362021-01-13 07:07:52 -0500728 svm_switch_vmcb(svm, &svm->vmcb01);
729
730 /*
731 * On vmexit the GIF is set to false and
732 * no event can be injected in L1.
733 */
Maxim Levitsky98837642020-08-27 19:27:18 +0300734 svm_set_gif(svm, false);
Cathy Avery4995a362021-01-13 07:07:52 -0500735 svm->vmcb->control.exit_int_info = 0;
Maxim Levitsky98837642020-08-27 19:27:18 +0300736
Paolo Bonzini7ca62d12020-11-16 06:38:19 -0500737 svm->vcpu.arch.tsc_offset = svm->vcpu.arch.l1_tsc_offset;
738 if (svm->vmcb->control.tsc_offset != svm->vcpu.arch.tsc_offset) {
739 svm->vmcb->control.tsc_offset = svm->vcpu.arch.tsc_offset;
740 vmcb_mark_dirty(svm->vmcb, VMCB_INTERCEPTS);
741 }
Paolo Bonzini18fc6c52020-05-18 11:07:08 -0400742
Paolo Bonzinie670bf62020-05-13 13:16:12 -0400743 svm->nested.ctl.nested_cr3 = 0;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100744
Cathy Avery4995a362021-01-13 07:07:52 -0500745 /*
746 * Restore processor state that had been saved in vmcb01
747 */
Paolo Bonzini63129752021-03-02 14:40:39 -0500748 kvm_set_rflags(vcpu, svm->vmcb->save.rflags);
749 svm_set_efer(vcpu, svm->vmcb->save.efer);
750 svm_set_cr0(vcpu, svm->vmcb->save.cr0 | X86_CR0_PE);
751 svm_set_cr4(vcpu, svm->vmcb->save.cr4);
752 kvm_rax_write(vcpu, svm->vmcb->save.rax);
753 kvm_rsp_write(vcpu, svm->vmcb->save.rsp);
754 kvm_rip_write(vcpu, svm->vmcb->save.rip);
Cathy Avery4995a362021-01-13 07:07:52 -0500755
756 svm->vcpu.arch.dr7 = DR7_FIXED_1;
757 kvm_update_dr7(&svm->vcpu);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100758
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300759 trace_kvm_nested_vmexit_inject(vmcb12->control.exit_code,
760 vmcb12->control.exit_info_1,
761 vmcb12->control.exit_info_2,
762 vmcb12->control.exit_int_info,
763 vmcb12->control.exit_int_info_err,
Paolo Bonzini36e2e982020-05-22 06:04:57 -0400764 KVM_ISA_SVM);
765
Paolo Bonzini63129752021-03-02 14:40:39 -0500766 kvm_vcpu_unmap(vcpu, &map, true);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100767
Paolo Bonzini63129752021-03-02 14:40:39 -0500768 nested_svm_uninit_mmu_context(vcpu);
Vitaly Kuznetsovbf7dea42020-07-10 16:11:54 +0200769
Paolo Bonzini63129752021-03-02 14:40:39 -0500770 rc = nested_svm_load_cr3(vcpu, svm->vmcb->save.cr3, false);
Vitaly Kuznetsovd82aaef2020-07-10 16:11:56 +0200771 if (rc)
772 return 1;
Vitaly Kuznetsovbf7dea42020-07-10 16:11:54 +0200773
Joerg Roedel883b0a92020-03-24 10:41:52 +0100774 /*
775 * Drop what we picked up for L2 via svm_complete_interrupts() so it
776 * doesn't end up in L1.
777 */
778 svm->vcpu.arch.nmi_injected = false;
Paolo Bonzini63129752021-03-02 14:40:39 -0500779 kvm_clear_exception_queue(vcpu);
780 kvm_clear_interrupt_queue(vcpu);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100781
782 return 0;
783}
784
Sean Christophersoncb6a32c2021-03-02 09:45:14 -0800785static void nested_svm_triple_fault(struct kvm_vcpu *vcpu)
786{
Sean Christopherson3a87c7e2021-03-02 09:45:15 -0800787 nested_svm_simple_vmexit(to_svm(vcpu), SVM_EXIT_SHUTDOWN);
Sean Christophersoncb6a32c2021-03-02 09:45:14 -0800788}
789
Maxim Levitsky2fcf4872020-10-01 14:29:54 +0300790int svm_allocate_nested(struct vcpu_svm *svm)
791{
Cathy Avery4995a362021-01-13 07:07:52 -0500792 struct page *vmcb02_page;
Maxim Levitsky2fcf4872020-10-01 14:29:54 +0300793
794 if (svm->nested.initialized)
795 return 0;
796
Cathy Avery4995a362021-01-13 07:07:52 -0500797 vmcb02_page = alloc_page(GFP_KERNEL_ACCOUNT | __GFP_ZERO);
798 if (!vmcb02_page)
Maxim Levitsky2fcf4872020-10-01 14:29:54 +0300799 return -ENOMEM;
Cathy Avery4995a362021-01-13 07:07:52 -0500800 svm->nested.vmcb02.ptr = page_address(vmcb02_page);
801 svm->nested.vmcb02.pa = __sme_set(page_to_pfn(vmcb02_page) << PAGE_SHIFT);
Maxim Levitsky2fcf4872020-10-01 14:29:54 +0300802
803 svm->nested.msrpm = svm_vcpu_alloc_msrpm();
804 if (!svm->nested.msrpm)
Cathy Avery4995a362021-01-13 07:07:52 -0500805 goto err_free_vmcb02;
Maxim Levitsky2fcf4872020-10-01 14:29:54 +0300806 svm_vcpu_init_msrpm(&svm->vcpu, svm->nested.msrpm);
807
808 svm->nested.initialized = true;
809 return 0;
810
Cathy Avery4995a362021-01-13 07:07:52 -0500811err_free_vmcb02:
812 __free_page(vmcb02_page);
Maxim Levitsky2fcf4872020-10-01 14:29:54 +0300813 return -ENOMEM;
814}
815
816void svm_free_nested(struct vcpu_svm *svm)
817{
818 if (!svm->nested.initialized)
819 return;
820
821 svm_vcpu_free_msrpm(svm->nested.msrpm);
822 svm->nested.msrpm = NULL;
823
Cathy Avery4995a362021-01-13 07:07:52 -0500824 __free_page(virt_to_page(svm->nested.vmcb02.ptr));
825 svm->nested.vmcb02.ptr = NULL;
Maxim Levitsky2fcf4872020-10-01 14:29:54 +0300826
827 svm->nested.initialized = false;
828}
829
Paolo Bonzinic513f482020-05-18 13:08:37 -0400830/*
831 * Forcibly leave nested mode in order to be able to reset the VCPU later on.
832 */
833void svm_leave_nested(struct vcpu_svm *svm)
834{
Paolo Bonzini63129752021-03-02 14:40:39 -0500835 struct kvm_vcpu *vcpu = &svm->vcpu;
836
837 if (is_guest_mode(vcpu)) {
Paolo Bonzinic513f482020-05-18 13:08:37 -0400838 svm->nested.nested_run_pending = 0;
Paolo Bonzini63129752021-03-02 14:40:39 -0500839 leave_guest_mode(vcpu);
Cathy Avery4995a362021-01-13 07:07:52 -0500840
841 svm_switch_vmcb(svm, &svm->nested.vmcb02);
842
Paolo Bonzini63129752021-03-02 14:40:39 -0500843 nested_svm_uninit_mmu_context(vcpu);
Maxim Levitsky56fe28d2021-01-07 11:38:54 +0200844 vmcb_mark_all_dirty(svm->vmcb);
Paolo Bonzinic513f482020-05-18 13:08:37 -0400845 }
Paolo Bonzinia7d5c7c2020-09-22 07:43:14 -0400846
Paolo Bonzini63129752021-03-02 14:40:39 -0500847 kvm_clear_request(KVM_REQ_GET_NESTED_STATE_PAGES, vcpu);
Paolo Bonzinic513f482020-05-18 13:08:37 -0400848}
849
Joerg Roedel883b0a92020-03-24 10:41:52 +0100850static int nested_svm_exit_handled_msr(struct vcpu_svm *svm)
851{
852 u32 offset, msr, value;
853 int write, mask;
854
Babu Mogerc62e2e92020-09-11 14:28:28 -0500855 if (!(vmcb_is_intercept(&svm->nested.ctl, INTERCEPT_MSR_PROT)))
Joerg Roedel883b0a92020-03-24 10:41:52 +0100856 return NESTED_EXIT_HOST;
857
858 msr = svm->vcpu.arch.regs[VCPU_REGS_RCX];
859 offset = svm_msrpm_offset(msr);
860 write = svm->vmcb->control.exit_info_1 & 1;
861 mask = 1 << ((2 * (msr & 0xf)) + write);
862
863 if (offset == MSR_INVALID)
864 return NESTED_EXIT_DONE;
865
866 /* Offset is in 32 bit units but need in 8 bit units */
867 offset *= 4;
868
Paolo Bonzinie670bf62020-05-13 13:16:12 -0400869 if (kvm_vcpu_read_guest(&svm->vcpu, svm->nested.ctl.msrpm_base_pa + offset, &value, 4))
Joerg Roedel883b0a92020-03-24 10:41:52 +0100870 return NESTED_EXIT_DONE;
871
872 return (value & mask) ? NESTED_EXIT_DONE : NESTED_EXIT_HOST;
873}
874
Joerg Roedel883b0a92020-03-24 10:41:52 +0100875static int nested_svm_intercept_ioio(struct vcpu_svm *svm)
876{
877 unsigned port, size, iopm_len;
878 u16 val, mask;
879 u8 start_bit;
880 u64 gpa;
881
Babu Mogerc62e2e92020-09-11 14:28:28 -0500882 if (!(vmcb_is_intercept(&svm->nested.ctl, INTERCEPT_IOIO_PROT)))
Joerg Roedel883b0a92020-03-24 10:41:52 +0100883 return NESTED_EXIT_HOST;
884
885 port = svm->vmcb->control.exit_info_1 >> 16;
886 size = (svm->vmcb->control.exit_info_1 & SVM_IOIO_SIZE_MASK) >>
887 SVM_IOIO_SIZE_SHIFT;
Paolo Bonzinie670bf62020-05-13 13:16:12 -0400888 gpa = svm->nested.ctl.iopm_base_pa + (port / 8);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100889 start_bit = port % 8;
890 iopm_len = (start_bit + size > 8) ? 2 : 1;
891 mask = (0xf >> (4 - size)) << start_bit;
892 val = 0;
893
894 if (kvm_vcpu_read_guest(&svm->vcpu, gpa, &val, iopm_len))
895 return NESTED_EXIT_DONE;
896
897 return (val & mask) ? NESTED_EXIT_DONE : NESTED_EXIT_HOST;
898}
899
900static int nested_svm_intercept(struct vcpu_svm *svm)
901{
902 u32 exit_code = svm->vmcb->control.exit_code;
903 int vmexit = NESTED_EXIT_HOST;
904
905 switch (exit_code) {
906 case SVM_EXIT_MSR:
907 vmexit = nested_svm_exit_handled_msr(svm);
908 break;
909 case SVM_EXIT_IOIO:
910 vmexit = nested_svm_intercept_ioio(svm);
911 break;
912 case SVM_EXIT_READ_CR0 ... SVM_EXIT_WRITE_CR8: {
Babu Moger03bfeeb2020-09-11 14:28:05 -0500913 if (vmcb_is_intercept(&svm->nested.ctl, exit_code))
Joerg Roedel883b0a92020-03-24 10:41:52 +0100914 vmexit = NESTED_EXIT_DONE;
915 break;
916 }
917 case SVM_EXIT_READ_DR0 ... SVM_EXIT_WRITE_DR7: {
Babu Moger30abaa882020-09-11 14:28:12 -0500918 if (vmcb_is_intercept(&svm->nested.ctl, exit_code))
Joerg Roedel883b0a92020-03-24 10:41:52 +0100919 vmexit = NESTED_EXIT_DONE;
920 break;
921 }
922 case SVM_EXIT_EXCP_BASE ... SVM_EXIT_EXCP_BASE + 0x1f: {
Paolo Bonzini7c866632020-05-16 08:42:28 -0400923 /*
924 * Host-intercepted exceptions have been checked already in
925 * nested_svm_exit_special. There is nothing to do here,
926 * the vmexit is injected by svm_check_nested_events.
927 */
928 vmexit = NESTED_EXIT_DONE;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100929 break;
930 }
931 case SVM_EXIT_ERR: {
932 vmexit = NESTED_EXIT_DONE;
933 break;
934 }
935 default: {
Babu Mogerc62e2e92020-09-11 14:28:28 -0500936 if (vmcb_is_intercept(&svm->nested.ctl, exit_code))
Joerg Roedel883b0a92020-03-24 10:41:52 +0100937 vmexit = NESTED_EXIT_DONE;
938 }
939 }
940
941 return vmexit;
942}
943
944int nested_svm_exit_handled(struct vcpu_svm *svm)
945{
946 int vmexit;
947
948 vmexit = nested_svm_intercept(svm);
949
950 if (vmexit == NESTED_EXIT_DONE)
951 nested_svm_vmexit(svm);
952
953 return vmexit;
954}
955
Paolo Bonzini63129752021-03-02 14:40:39 -0500956int nested_svm_check_permissions(struct kvm_vcpu *vcpu)
Joerg Roedel883b0a92020-03-24 10:41:52 +0100957{
Paolo Bonzini63129752021-03-02 14:40:39 -0500958 if (!(vcpu->arch.efer & EFER_SVME) || !is_paging(vcpu)) {
959 kvm_queue_exception(vcpu, UD_VECTOR);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100960 return 1;
961 }
962
Paolo Bonzini63129752021-03-02 14:40:39 -0500963 if (to_svm(vcpu)->vmcb->save.cpl) {
964 kvm_inject_gp(vcpu, 0);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100965 return 1;
966 }
967
968 return 0;
969}
970
Paolo Bonzini7c866632020-05-16 08:42:28 -0400971static bool nested_exit_on_exception(struct vcpu_svm *svm)
Joerg Roedel883b0a92020-03-24 10:41:52 +0100972{
Paolo Bonzini7c866632020-05-16 08:42:28 -0400973 unsigned int nr = svm->vcpu.arch.exception.nr;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100974
Babu Moger9780d512020-09-11 14:28:20 -0500975 return (svm->nested.ctl.intercepts[INTERCEPT_EXCEPTION] & BIT(nr));
Paolo Bonzini7c866632020-05-16 08:42:28 -0400976}
Joerg Roedel883b0a92020-03-24 10:41:52 +0100977
Paolo Bonzini7c866632020-05-16 08:42:28 -0400978static void nested_svm_inject_exception_vmexit(struct vcpu_svm *svm)
979{
980 unsigned int nr = svm->vcpu.arch.exception.nr;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100981
982 svm->vmcb->control.exit_code = SVM_EXIT_EXCP_BASE + nr;
983 svm->vmcb->control.exit_code_hi = 0;
Paolo Bonzini7c866632020-05-16 08:42:28 -0400984
985 if (svm->vcpu.arch.exception.has_error_code)
986 svm->vmcb->control.exit_info_1 = svm->vcpu.arch.exception.error_code;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100987
988 /*
989 * EXITINFO2 is undefined for all exception intercepts other
990 * than #PF.
991 */
Paolo Bonzini7c866632020-05-16 08:42:28 -0400992 if (nr == PF_VECTOR) {
993 if (svm->vcpu.arch.exception.nested_apf)
994 svm->vmcb->control.exit_info_2 = svm->vcpu.arch.apf.nested_apf_token;
995 else if (svm->vcpu.arch.exception.has_payload)
996 svm->vmcb->control.exit_info_2 = svm->vcpu.arch.exception.payload;
997 else
998 svm->vmcb->control.exit_info_2 = svm->vcpu.arch.cr2;
999 } else if (nr == DB_VECTOR) {
1000 /* See inject_pending_event. */
1001 kvm_deliver_exception_payload(&svm->vcpu);
1002 if (svm->vcpu.arch.dr7 & DR7_GD) {
1003 svm->vcpu.arch.dr7 &= ~DR7_GD;
1004 kvm_update_dr7(&svm->vcpu);
1005 }
1006 } else
1007 WARN_ON(svm->vcpu.arch.exception.has_payload);
Joerg Roedel883b0a92020-03-24 10:41:52 +01001008
Paolo Bonzini7c866632020-05-16 08:42:28 -04001009 nested_svm_vmexit(svm);
Joerg Roedel883b0a92020-03-24 10:41:52 +01001010}
1011
Paolo Bonzini5b6724082020-05-16 08:50:35 -04001012static inline bool nested_exit_on_init(struct vcpu_svm *svm)
1013{
Babu Mogerc62e2e92020-09-11 14:28:28 -05001014 return vmcb_is_intercept(&svm->nested.ctl, INTERCEPT_INIT);
Paolo Bonzini5b6724082020-05-16 08:50:35 -04001015}
1016
Paolo Bonzini33b22172020-04-17 10:24:18 -04001017static int svm_check_nested_events(struct kvm_vcpu *vcpu)
Joerg Roedel883b0a92020-03-24 10:41:52 +01001018{
1019 struct vcpu_svm *svm = to_svm(vcpu);
1020 bool block_nested_events =
Paolo Bonzinibd279622020-05-16 08:46:00 -04001021 kvm_event_needs_reinjection(vcpu) || svm->nested.nested_run_pending;
Paolo Bonzini5b6724082020-05-16 08:50:35 -04001022 struct kvm_lapic *apic = vcpu->arch.apic;
1023
1024 if (lapic_in_kernel(vcpu) &&
1025 test_bit(KVM_APIC_INIT, &apic->pending_events)) {
1026 if (block_nested_events)
1027 return -EBUSY;
1028 if (!nested_exit_on_init(svm))
1029 return 0;
Sean Christopherson3a87c7e2021-03-02 09:45:15 -08001030 nested_svm_simple_vmexit(svm, SVM_EXIT_INIT);
Paolo Bonzini5b6724082020-05-16 08:50:35 -04001031 return 0;
1032 }
Joerg Roedel883b0a92020-03-24 10:41:52 +01001033
Paolo Bonzini7c866632020-05-16 08:42:28 -04001034 if (vcpu->arch.exception.pending) {
1035 if (block_nested_events)
1036 return -EBUSY;
1037 if (!nested_exit_on_exception(svm))
1038 return 0;
1039 nested_svm_inject_exception_vmexit(svm);
1040 return 0;
1041 }
1042
Paolo Bonzini221e7612020-04-23 08:13:10 -04001043 if (vcpu->arch.smi_pending && !svm_smi_blocked(vcpu)) {
Paolo Bonzini55714cd2020-04-23 08:17:28 -04001044 if (block_nested_events)
1045 return -EBUSY;
Paolo Bonzini221e7612020-04-23 08:13:10 -04001046 if (!nested_exit_on_smi(svm))
1047 return 0;
Sean Christopherson3a87c7e2021-03-02 09:45:15 -08001048 nested_svm_simple_vmexit(svm, SVM_EXIT_SMI);
Paolo Bonzini55714cd2020-04-23 08:17:28 -04001049 return 0;
1050 }
1051
Paolo Bonzini221e7612020-04-23 08:13:10 -04001052 if (vcpu->arch.nmi_pending && !svm_nmi_blocked(vcpu)) {
Cathy Avery9c3d3702020-04-14 16:11:06 -04001053 if (block_nested_events)
1054 return -EBUSY;
Paolo Bonzini221e7612020-04-23 08:13:10 -04001055 if (!nested_exit_on_nmi(svm))
1056 return 0;
Sean Christopherson3a87c7e2021-03-02 09:45:15 -08001057 nested_svm_simple_vmexit(svm, SVM_EXIT_NMI);
Cathy Avery9c3d3702020-04-14 16:11:06 -04001058 return 0;
1059 }
1060
Paolo Bonzini221e7612020-04-23 08:13:10 -04001061 if (kvm_cpu_has_interrupt(vcpu) && !svm_interrupt_blocked(vcpu)) {
Joerg Roedel883b0a92020-03-24 10:41:52 +01001062 if (block_nested_events)
1063 return -EBUSY;
Paolo Bonzini221e7612020-04-23 08:13:10 -04001064 if (!nested_exit_on_intr(svm))
1065 return 0;
Sean Christopherson3a87c7e2021-03-02 09:45:15 -08001066 trace_kvm_nested_intr_vmexit(svm->vmcb->save.rip);
1067 nested_svm_simple_vmexit(svm, SVM_EXIT_INTR);
Joerg Roedel883b0a92020-03-24 10:41:52 +01001068 return 0;
1069 }
1070
1071 return 0;
1072}
1073
1074int nested_svm_exit_special(struct vcpu_svm *svm)
1075{
1076 u32 exit_code = svm->vmcb->control.exit_code;
1077
1078 switch (exit_code) {
1079 case SVM_EXIT_INTR:
1080 case SVM_EXIT_NMI:
Joerg Roedel883b0a92020-03-24 10:41:52 +01001081 case SVM_EXIT_NPF:
Paolo Bonzini7c866632020-05-16 08:42:28 -04001082 return NESTED_EXIT_HOST;
1083 case SVM_EXIT_EXCP_BASE ... SVM_EXIT_EXCP_BASE + 0x1f: {
1084 u32 excp_bits = 1 << (exit_code - SVM_EXIT_EXCP_BASE);
1085
Cathy Avery4995a362021-01-13 07:07:52 -05001086 if (svm->vmcb01.ptr->control.intercepts[INTERCEPT_EXCEPTION] &
1087 excp_bits)
Paolo Bonzini7c866632020-05-16 08:42:28 -04001088 return NESTED_EXIT_HOST;
1089 else if (exit_code == SVM_EXIT_EXCP_BASE + PF_VECTOR &&
Vitaly Kuznetsov68fd66f2020-05-25 16:41:17 +02001090 svm->vcpu.arch.apf.host_apf_flags)
Paolo Bonzini7c866632020-05-16 08:42:28 -04001091 /* Trap async PF even if not shadowing */
Joerg Roedel883b0a92020-03-24 10:41:52 +01001092 return NESTED_EXIT_HOST;
1093 break;
Paolo Bonzini7c866632020-05-16 08:42:28 -04001094 }
Joerg Roedel883b0a92020-03-24 10:41:52 +01001095 default:
1096 break;
1097 }
1098
1099 return NESTED_EXIT_CONTINUE;
1100}
Paolo Bonzini33b22172020-04-17 10:24:18 -04001101
Paolo Bonzinicc440cd2020-05-13 13:36:32 -04001102static int svm_get_nested_state(struct kvm_vcpu *vcpu,
1103 struct kvm_nested_state __user *user_kvm_nested_state,
1104 u32 user_data_size)
1105{
1106 struct vcpu_svm *svm;
1107 struct kvm_nested_state kvm_state = {
1108 .flags = 0,
1109 .format = KVM_STATE_NESTED_FORMAT_SVM,
1110 .size = sizeof(kvm_state),
1111 };
1112 struct vmcb __user *user_vmcb = (struct vmcb __user *)
1113 &user_kvm_nested_state->data.svm[0];
1114
1115 if (!vcpu)
1116 return kvm_state.size + KVM_STATE_NESTED_SVM_VMCB_SIZE;
1117
1118 svm = to_svm(vcpu);
1119
1120 if (user_data_size < kvm_state.size)
1121 goto out;
1122
1123 /* First fill in the header and copy it out. */
1124 if (is_guest_mode(vcpu)) {
Maxim Levitsky0dd16b52020-08-27 20:11:39 +03001125 kvm_state.hdr.svm.vmcb_pa = svm->nested.vmcb12_gpa;
Paolo Bonzinicc440cd2020-05-13 13:36:32 -04001126 kvm_state.size += KVM_STATE_NESTED_SVM_VMCB_SIZE;
1127 kvm_state.flags |= KVM_STATE_NESTED_GUEST_MODE;
1128
1129 if (svm->nested.nested_run_pending)
1130 kvm_state.flags |= KVM_STATE_NESTED_RUN_PENDING;
1131 }
1132
1133 if (gif_set(svm))
1134 kvm_state.flags |= KVM_STATE_NESTED_GIF_SET;
1135
1136 if (copy_to_user(user_kvm_nested_state, &kvm_state, sizeof(kvm_state)))
1137 return -EFAULT;
1138
1139 if (!is_guest_mode(vcpu))
1140 goto out;
1141
1142 /*
1143 * Copy over the full size of the VMCB rather than just the size
1144 * of the structs.
1145 */
1146 if (clear_user(user_vmcb, KVM_STATE_NESTED_SVM_VMCB_SIZE))
1147 return -EFAULT;
1148 if (copy_to_user(&user_vmcb->control, &svm->nested.ctl,
1149 sizeof(user_vmcb->control)))
1150 return -EFAULT;
Cathy Avery4995a362021-01-13 07:07:52 -05001151 if (copy_to_user(&user_vmcb->save, &svm->vmcb01.ptr->save,
Paolo Bonzinicc440cd2020-05-13 13:36:32 -04001152 sizeof(user_vmcb->save)))
1153 return -EFAULT;
Paolo Bonzinicc440cd2020-05-13 13:36:32 -04001154out:
1155 return kvm_state.size;
1156}
1157
1158static int svm_set_nested_state(struct kvm_vcpu *vcpu,
1159 struct kvm_nested_state __user *user_kvm_nested_state,
1160 struct kvm_nested_state *kvm_state)
1161{
1162 struct vcpu_svm *svm = to_svm(vcpu);
Paolo Bonzinicc440cd2020-05-13 13:36:32 -04001163 struct vmcb __user *user_vmcb = (struct vmcb __user *)
1164 &user_kvm_nested_state->data.svm[0];
Joerg Roedel6ccbd292020-09-07 15:15:02 +02001165 struct vmcb_control_area *ctl;
1166 struct vmcb_save_area *save;
1167 int ret;
Paolo Bonzinicc440cd2020-05-13 13:36:32 -04001168 u32 cr0;
1169
Joerg Roedel6ccbd292020-09-07 15:15:02 +02001170 BUILD_BUG_ON(sizeof(struct vmcb_control_area) + sizeof(struct vmcb_save_area) >
1171 KVM_STATE_NESTED_SVM_VMCB_SIZE);
1172
Paolo Bonzinicc440cd2020-05-13 13:36:32 -04001173 if (kvm_state->format != KVM_STATE_NESTED_FORMAT_SVM)
1174 return -EINVAL;
1175
1176 if (kvm_state->flags & ~(KVM_STATE_NESTED_GUEST_MODE |
1177 KVM_STATE_NESTED_RUN_PENDING |
1178 KVM_STATE_NESTED_GIF_SET))
1179 return -EINVAL;
1180
1181 /*
1182 * If in guest mode, vcpu->arch.efer actually refers to the L2 guest's
1183 * EFER.SVME, but EFER.SVME still has to be 1 for VMRUN to succeed.
1184 */
1185 if (!(vcpu->arch.efer & EFER_SVME)) {
1186 /* GIF=1 and no guest mode are required if SVME=0. */
1187 if (kvm_state->flags != KVM_STATE_NESTED_GIF_SET)
1188 return -EINVAL;
1189 }
1190
1191 /* SMM temporarily disables SVM, so we cannot be in guest mode. */
1192 if (is_smm(vcpu) && (kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE))
1193 return -EINVAL;
1194
1195 if (!(kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE)) {
1196 svm_leave_nested(svm);
Vitaly Kuznetsovd5cd6f32020-09-14 15:37:25 +02001197 svm_set_gif(svm, !!(kvm_state->flags & KVM_STATE_NESTED_GIF_SET));
1198 return 0;
Paolo Bonzinicc440cd2020-05-13 13:36:32 -04001199 }
1200
1201 if (!page_address_valid(vcpu, kvm_state->hdr.svm.vmcb_pa))
1202 return -EINVAL;
1203 if (kvm_state->size < sizeof(*kvm_state) + KVM_STATE_NESTED_SVM_VMCB_SIZE)
1204 return -EINVAL;
Paolo Bonzinicc440cd2020-05-13 13:36:32 -04001205
Joerg Roedel6ccbd292020-09-07 15:15:02 +02001206 ret = -ENOMEM;
1207 ctl = kzalloc(sizeof(*ctl), GFP_KERNEL);
1208 save = kzalloc(sizeof(*save), GFP_KERNEL);
1209 if (!ctl || !save)
1210 goto out_free;
1211
1212 ret = -EFAULT;
1213 if (copy_from_user(ctl, &user_vmcb->control, sizeof(*ctl)))
1214 goto out_free;
1215 if (copy_from_user(save, &user_vmcb->save, sizeof(*save)))
1216 goto out_free;
1217
1218 ret = -EINVAL;
1219 if (!nested_vmcb_check_controls(ctl))
1220 goto out_free;
Paolo Bonzinicc440cd2020-05-13 13:36:32 -04001221
1222 /*
1223 * Processor state contains L2 state. Check that it is
1224 * valid for guest mode (see nested_vmcb_checks).
1225 */
1226 cr0 = kvm_read_cr0(vcpu);
1227 if (((cr0 & X86_CR0_CD) == 0) && (cr0 & X86_CR0_NW))
Joerg Roedel6ccbd292020-09-07 15:15:02 +02001228 goto out_free;
Paolo Bonzinicc440cd2020-05-13 13:36:32 -04001229
1230 /*
1231 * Validate host state saved from before VMRUN (see
1232 * nested_svm_check_permissions).
Paolo Bonzinicc440cd2020-05-13 13:36:32 -04001233 */
Krish Sadhukhan6906e062020-10-06 19:06:52 +00001234 if (!(save->cr0 & X86_CR0_PG) ||
1235 !(save->cr0 & X86_CR0_PE) ||
1236 (save->rflags & X86_EFLAGS_VM) ||
Paolo Bonzini63129752021-03-02 14:40:39 -05001237 !nested_vmcb_valid_sregs(vcpu, save))
Joerg Roedel6ccbd292020-09-07 15:15:02 +02001238 goto out_free;
Paolo Bonzinicc440cd2020-05-13 13:36:32 -04001239
1240 /*
Cathy Avery4995a362021-01-13 07:07:52 -05001241 * All checks done, we can enter guest mode. Userspace provides
1242 * vmcb12.control, which will be combined with L1 and stored into
1243 * vmcb02, and the L1 save state which we store in vmcb01.
1244 * L2 registers if needed are moved from the current VMCB to VMCB02.
Paolo Bonzinicc440cd2020-05-13 13:36:32 -04001245 */
Maxim Levitsky81f76ad2021-01-07 11:38:52 +02001246
1247 svm->nested.nested_run_pending =
1248 !!(kvm_state->flags & KVM_STATE_NESTED_RUN_PENDING);
1249
Maxim Levitsky0dd16b52020-08-27 20:11:39 +03001250 svm->nested.vmcb12_gpa = kvm_state->hdr.svm.vmcb_pa;
Cathy Avery4995a362021-01-13 07:07:52 -05001251 if (svm->current_vmcb == &svm->vmcb01)
1252 svm->nested.vmcb02.ptr->save = svm->vmcb01.ptr->save;
Paolo Bonzinic08f3902020-11-17 02:51:35 -05001253
1254 svm->vmcb01.ptr->save.es = save->es;
1255 svm->vmcb01.ptr->save.cs = save->cs;
1256 svm->vmcb01.ptr->save.ss = save->ss;
1257 svm->vmcb01.ptr->save.ds = save->ds;
1258 svm->vmcb01.ptr->save.gdtr = save->gdtr;
1259 svm->vmcb01.ptr->save.idtr = save->idtr;
1260 svm->vmcb01.ptr->save.rflags = save->rflags | X86_EFLAGS_FIXED;
1261 svm->vmcb01.ptr->save.efer = save->efer;
1262 svm->vmcb01.ptr->save.cr0 = save->cr0;
1263 svm->vmcb01.ptr->save.cr3 = save->cr3;
1264 svm->vmcb01.ptr->save.cr4 = save->cr4;
1265 svm->vmcb01.ptr->save.rax = save->rax;
1266 svm->vmcb01.ptr->save.rsp = save->rsp;
1267 svm->vmcb01.ptr->save.rip = save->rip;
1268 svm->vmcb01.ptr->save.cpl = 0;
1269
Paolo Bonzini9e8f0fb2020-11-17 05:15:41 -05001270 nested_load_control_from_vmcb12(svm, ctl);
Cathy Avery4995a362021-01-13 07:07:52 -05001271
1272 svm_switch_vmcb(svm, &svm->nested.vmcb02);
1273
Paolo Bonzini9e8f0fb2020-11-17 05:15:41 -05001274 nested_vmcb02_prepare_control(svm);
Paolo Bonzinicc440cd2020-05-13 13:36:32 -04001275
Paolo Bonzinia7d5c7c2020-09-22 07:43:14 -04001276 kvm_make_request(KVM_REQ_GET_NESTED_STATE_PAGES, vcpu);
Joerg Roedel6ccbd292020-09-07 15:15:02 +02001277 ret = 0;
1278out_free:
1279 kfree(save);
1280 kfree(ctl);
1281
1282 return ret;
Paolo Bonzinicc440cd2020-05-13 13:36:32 -04001283}
1284
Paolo Bonzini33b22172020-04-17 10:24:18 -04001285struct kvm_x86_nested_ops svm_nested_ops = {
1286 .check_events = svm_check_nested_events,
Sean Christophersoncb6a32c2021-03-02 09:45:14 -08001287 .triple_fault = nested_svm_triple_fault,
Paolo Bonzinia7d5c7c2020-09-22 07:43:14 -04001288 .get_nested_state_pages = svm_get_nested_state_pages,
Paolo Bonzinicc440cd2020-05-13 13:36:32 -04001289 .get_state = svm_get_nested_state,
1290 .set_state = svm_set_nested_state,
Paolo Bonzini33b22172020-04-17 10:24:18 -04001291};