blob: a466336aab43adfd879e86178b0d148e55352eb9 [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
54static u64 nested_svm_get_tdp_pdptr(struct kvm_vcpu *vcpu, int index)
55{
56 struct vcpu_svm *svm = to_svm(vcpu);
Paolo Bonzinie670bf62020-05-13 13:16:12 -040057 u64 cr3 = svm->nested.ctl.nested_cr3;
Joerg Roedel883b0a92020-03-24 10:41:52 +010058 u64 pdpte;
59 int ret;
60
61 ret = kvm_vcpu_read_guest_page(vcpu, gpa_to_gfn(__sme_clr(cr3)), &pdpte,
62 offset_in_page(cr3) + index * 8, 8);
63 if (ret)
64 return 0;
65 return pdpte;
66}
67
68static unsigned long nested_svm_get_tdp_cr3(struct kvm_vcpu *vcpu)
69{
70 struct vcpu_svm *svm = to_svm(vcpu);
71
Paolo Bonzinie670bf62020-05-13 13:16:12 -040072 return svm->nested.ctl.nested_cr3;
Joerg Roedel883b0a92020-03-24 10:41:52 +010073}
74
75static void nested_svm_init_mmu_context(struct kvm_vcpu *vcpu)
76{
Paolo Bonzini929d1cf2020-05-19 06:18:31 -040077 struct vcpu_svm *svm = to_svm(vcpu);
78 struct vmcb *hsave = svm->nested.hsave;
79
Joerg Roedel883b0a92020-03-24 10:41:52 +010080 WARN_ON(mmu_is_nested(vcpu));
81
82 vcpu->arch.mmu = &vcpu->arch.guest_mmu;
Vitaly Kuznetsov0f04a2a2020-07-10 16:11:49 +020083 kvm_init_shadow_npt_mmu(vcpu, X86_CR0_PG, hsave->save.cr4, hsave->save.efer,
84 svm->nested.ctl.nested_cr3);
Joerg Roedel883b0a92020-03-24 10:41:52 +010085 vcpu->arch.mmu->get_guest_pgd = nested_svm_get_tdp_cr3;
86 vcpu->arch.mmu->get_pdptr = nested_svm_get_tdp_pdptr;
87 vcpu->arch.mmu->inject_page_fault = nested_svm_inject_npf_exit;
Joerg Roedel883b0a92020-03-24 10:41:52 +010088 reset_shadow_zero_bits_mask(vcpu, vcpu->arch.mmu);
89 vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu;
90}
91
92static void nested_svm_uninit_mmu_context(struct kvm_vcpu *vcpu)
93{
94 vcpu->arch.mmu = &vcpu->arch.root_mmu;
95 vcpu->arch.walk_mmu = &vcpu->arch.root_mmu;
96}
97
98void recalc_intercepts(struct vcpu_svm *svm)
99{
Paolo Bonzinie670bf62020-05-13 13:16:12 -0400100 struct vmcb_control_area *c, *h, *g;
Babu Mogerc45ad722020-09-11 14:27:58 -0500101 unsigned int i;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100102
Joerg Roedel06e78522020-06-25 10:03:23 +0200103 vmcb_mark_dirty(svm->vmcb, VMCB_INTERCEPTS);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100104
105 if (!is_guest_mode(&svm->vcpu))
106 return;
107
108 c = &svm->vmcb->control;
109 h = &svm->nested.hsave->control;
Paolo Bonzinie670bf62020-05-13 13:16:12 -0400110 g = &svm->nested.ctl;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100111
Babu Mogerc45ad722020-09-11 14:27:58 -0500112 for (i = 0; i < MAX_INTERCEPT; i++)
113 c->intercepts[i] = h->intercepts[i];
114
Paolo Bonzinie9fd7612020-05-13 13:28:23 -0400115 if (g->int_ctl & V_INTR_MASKING_MASK) {
Joerg Roedel883b0a92020-03-24 10:41:52 +0100116 /* We only want the cr8 intercept bits of L1 */
Babu Moger03bfeeb2020-09-11 14:28:05 -0500117 vmcb_clr_intercept(c, INTERCEPT_CR8_READ);
118 vmcb_clr_intercept(c, INTERCEPT_CR8_WRITE);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100119
120 /*
121 * Once running L2 with HF_VINTR_MASK, EFLAGS.IF does not
122 * affect any interrupt we may want to inject; therefore,
123 * interrupt window vmexits are irrelevant to L0.
124 */
Babu Mogerc62e2e92020-09-11 14:28:28 -0500125 vmcb_clr_intercept(c, INTERCEPT_VINTR);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100126 }
127
128 /* We don't want to see VMMCALLs from a nested guest */
Babu Mogerc62e2e92020-09-11 14:28:28 -0500129 vmcb_clr_intercept(c, INTERCEPT_VMMCALL);
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] |= g->intercepts[i];
Joerg Roedel883b0a92020-03-24 10:41:52 +0100133}
134
Paolo Bonzini2f675912020-05-18 15:21:22 -0400135static void copy_vmcb_control_area(struct vmcb_control_area *dst,
136 struct vmcb_control_area *from)
Joerg Roedel883b0a92020-03-24 10:41:52 +0100137{
Babu Mogerc45ad722020-09-11 14:27:58 -0500138 unsigned int i;
139
140 for (i = 0; i < MAX_INTERCEPT; i++)
141 dst->intercepts[i] = from->intercepts[i];
142
Joerg Roedel883b0a92020-03-24 10:41:52 +0100143 dst->iopm_base_pa = from->iopm_base_pa;
144 dst->msrpm_base_pa = from->msrpm_base_pa;
145 dst->tsc_offset = from->tsc_offset;
Paolo Bonzini6c0238c2020-05-20 08:02:17 -0400146 /* asid not copied, it is handled manually for svm->vmcb. */
Joerg Roedel883b0a92020-03-24 10:41:52 +0100147 dst->tlb_ctl = from->tlb_ctl;
148 dst->int_ctl = from->int_ctl;
149 dst->int_vector = from->int_vector;
150 dst->int_state = from->int_state;
151 dst->exit_code = from->exit_code;
152 dst->exit_code_hi = from->exit_code_hi;
153 dst->exit_info_1 = from->exit_info_1;
154 dst->exit_info_2 = from->exit_info_2;
155 dst->exit_int_info = from->exit_int_info;
156 dst->exit_int_info_err = from->exit_int_info_err;
157 dst->nested_ctl = from->nested_ctl;
158 dst->event_inj = from->event_inj;
159 dst->event_inj_err = from->event_inj_err;
160 dst->nested_cr3 = from->nested_cr3;
161 dst->virt_ext = from->virt_ext;
162 dst->pause_filter_count = from->pause_filter_count;
163 dst->pause_filter_thresh = from->pause_filter_thresh;
164}
165
166static bool nested_svm_vmrun_msrpm(struct vcpu_svm *svm)
167{
168 /*
169 * This function merges the msr permission bitmaps of kvm and the
170 * nested vmcb. It is optimized in that it only merges the parts where
171 * the kvm msr permission bitmap may contain zero bits
172 */
173 int i;
174
Babu Mogerc62e2e92020-09-11 14:28:28 -0500175 if (!(vmcb_is_intercept(&svm->nested.ctl, INTERCEPT_MSR_PROT)))
Joerg Roedel883b0a92020-03-24 10:41:52 +0100176 return true;
177
178 for (i = 0; i < MSRPM_OFFSETS; i++) {
179 u32 value, p;
180 u64 offset;
181
182 if (msrpm_offsets[i] == 0xffffffff)
183 break;
184
185 p = msrpm_offsets[i];
Paolo Bonzinie670bf62020-05-13 13:16:12 -0400186 offset = svm->nested.ctl.msrpm_base_pa + (p * 4);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100187
188 if (kvm_vcpu_read_guest(&svm->vcpu, offset, &value, 4))
189 return false;
190
191 svm->nested.msrpm[p] = svm->msrpm[p] | value;
192 }
193
194 svm->vmcb->control.msrpm_base_pa = __sme_set(__pa(svm->nested.msrpm));
195
196 return true;
197}
198
Paolo Bonzinia7d5c7c2020-09-22 07:43:14 -0400199static bool svm_get_nested_state_pages(struct kvm_vcpu *vcpu)
200{
201 struct vcpu_svm *svm = to_svm(vcpu);
202 if (!nested_svm_vmrun_msrpm(svm)) {
203 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
204 vcpu->run->internal.suberror =
205 KVM_INTERNAL_ERROR_EMULATION;
206 vcpu->run->internal.ndata = 0;
207 return false;
208 }
209
210 return true;
211}
212
Paolo Bonzinica46d732020-05-18 13:02:15 -0400213static bool nested_vmcb_check_controls(struct vmcb_control_area *control)
214{
Babu Mogerc62e2e92020-09-11 14:28:28 -0500215 if ((vmcb_is_intercept(control, INTERCEPT_VMRUN)) == 0)
Paolo Bonzinica46d732020-05-18 13:02:15 -0400216 return false;
217
218 if (control->asid == 0)
219 return false;
220
221 if ((control->nested_ctl & SVM_NESTED_CTL_NP_ENABLE) &&
222 !npt_enabled)
223 return false;
224
225 return true;
226}
227
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300228static bool nested_vmcb_checks(struct vcpu_svm *svm, struct vmcb *vmcb12)
Joerg Roedel883b0a92020-03-24 10:41:52 +0100229{
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300230 bool vmcb12_lma;
231
232 if ((vmcb12->save.efer & EFER_SVME) == 0)
Joerg Roedel883b0a92020-03-24 10:41:52 +0100233 return false;
234
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300235 if (((vmcb12->save.cr0 & X86_CR0_CD) == 0) && (vmcb12->save.cr0 & X86_CR0_NW))
Krish Sadhukhan4f233372020-04-09 16:50:33 -0400236 return false;
237
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300238 if (!kvm_dr6_valid(vmcb12->save.dr6) || !kvm_dr7_valid(vmcb12->save.dr7))
Krish Sadhukhan1aef8162020-05-22 18:19:52 -0400239 return false;
240
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300241 vmcb12_lma = (vmcb12->save.efer & EFER_LME) && (vmcb12->save.cr0 & X86_CR0_PG);
Krish Sadhukhan761e4162020-07-08 00:39:56 +0000242
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300243 if (!vmcb12_lma) {
244 if (vmcb12->save.cr4 & X86_CR4_PAE) {
245 if (vmcb12->save.cr3 & MSR_CR3_LEGACY_PAE_RESERVED_MASK)
Krish Sadhukhan761e4162020-07-08 00:39:56 +0000246 return false;
247 } else {
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300248 if (vmcb12->save.cr3 & MSR_CR3_LEGACY_RESERVED_MASK)
Krish Sadhukhan761e4162020-07-08 00:39:56 +0000249 return false;
250 }
251 } else {
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300252 if (!(vmcb12->save.cr4 & X86_CR4_PAE) ||
253 !(vmcb12->save.cr0 & X86_CR0_PE) ||
Krish Sadhukhanfb0f33f2020-08-29 00:48:22 +0000254 (vmcb12->save.cr3 & MSR_CR3_LONG_MBZ_MASK))
Krish Sadhukhan761e4162020-07-08 00:39:56 +0000255 return false;
256 }
Sean Christophersonee69c922020-10-06 18:44:16 -0700257 if (!kvm_is_valid_cr4(&svm->vcpu, vmcb12->save.cr4))
Krish Sadhukhan761e4162020-07-08 00:39:56 +0000258 return false;
259
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300260 return nested_vmcb_check_controls(&vmcb12->control);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100261}
262
Paolo Bonzini3e06f012020-05-13 13:07:26 -0400263static void load_nested_vmcb_control(struct vcpu_svm *svm,
264 struct vmcb_control_area *control)
265{
Paolo Bonzinie670bf62020-05-13 13:16:12 -0400266 copy_vmcb_control_area(&svm->nested.ctl, control);
Paolo Bonzini3e06f012020-05-13 13:07:26 -0400267
Paolo Bonzinicc440cd2020-05-13 13:36:32 -0400268 /* Copy it here because nested_svm_check_controls will check it. */
269 svm->nested.ctl.asid = control->asid;
Paolo Bonzinie670bf62020-05-13 13:16:12 -0400270 svm->nested.ctl.msrpm_base_pa &= ~0x0fffULL;
271 svm->nested.ctl.iopm_base_pa &= ~0x0fffULL;
Paolo Bonzini3e06f012020-05-13 13:07:26 -0400272}
273
Paolo Bonzini2d8a42b2020-05-22 03:50:14 -0400274/*
275 * Synchronize fields that are written by the processor, so that
276 * they can be copied back into the nested_vmcb.
277 */
278void sync_nested_vmcb_control(struct vcpu_svm *svm)
279{
280 u32 mask;
281 svm->nested.ctl.event_inj = svm->vmcb->control.event_inj;
282 svm->nested.ctl.event_inj_err = svm->vmcb->control.event_inj_err;
283
284 /* Only a few fields of int_ctl are written by the processor. */
285 mask = V_IRQ_MASK | V_TPR_MASK;
286 if (!(svm->nested.ctl.int_ctl & V_INTR_MASKING_MASK) &&
Joerg Roedela284ba52020-06-25 10:03:24 +0200287 svm_is_intercept(svm, INTERCEPT_VINTR)) {
Paolo Bonzini2d8a42b2020-05-22 03:50:14 -0400288 /*
289 * In order to request an interrupt window, L0 is usurping
290 * svm->vmcb->control.int_ctl and possibly setting V_IRQ
291 * even if it was clear in L1's VMCB. Restoring it would be
292 * wrong. However, in this case V_IRQ will remain true until
293 * interrupt_window_interception calls svm_clear_vintr and
294 * restores int_ctl. We can just leave it aside.
295 */
296 mask &= ~V_IRQ_MASK;
297 }
298 svm->nested.ctl.int_ctl &= ~mask;
299 svm->nested.ctl.int_ctl |= svm->vmcb->control.int_ctl & mask;
300}
301
Paolo Bonzini36e2e982020-05-22 06:04:57 -0400302/*
303 * Transfer any event that L0 or L1 wanted to inject into L2 to
304 * EXIT_INT_INFO.
305 */
306static void nested_vmcb_save_pending_event(struct vcpu_svm *svm,
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300307 struct vmcb *vmcb12)
Paolo Bonzini36e2e982020-05-22 06:04:57 -0400308{
309 struct kvm_vcpu *vcpu = &svm->vcpu;
310 u32 exit_int_info = 0;
311 unsigned int nr;
312
313 if (vcpu->arch.exception.injected) {
314 nr = vcpu->arch.exception.nr;
315 exit_int_info = nr | SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_EXEPT;
316
317 if (vcpu->arch.exception.has_error_code) {
318 exit_int_info |= SVM_EVTINJ_VALID_ERR;
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300319 vmcb12->control.exit_int_info_err =
Paolo Bonzini36e2e982020-05-22 06:04:57 -0400320 vcpu->arch.exception.error_code;
321 }
322
323 } else if (vcpu->arch.nmi_injected) {
324 exit_int_info = SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_NMI;
325
326 } else if (vcpu->arch.interrupt.injected) {
327 nr = vcpu->arch.interrupt.nr;
328 exit_int_info = nr | SVM_EVTINJ_VALID;
329
330 if (vcpu->arch.interrupt.soft)
331 exit_int_info |= SVM_EVTINJ_TYPE_SOFT;
332 else
333 exit_int_info |= SVM_EVTINJ_TYPE_INTR;
334 }
335
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300336 vmcb12->control.exit_int_info = exit_int_info;
Paolo Bonzini36e2e982020-05-22 06:04:57 -0400337}
338
Vitaly Kuznetsov62156f62020-07-10 16:11:53 +0200339static inline bool nested_npt_enabled(struct vcpu_svm *svm)
340{
341 return svm->nested.ctl.nested_ctl & SVM_NESTED_CTL_NP_ENABLE;
342}
343
344/*
Vitaly Kuznetsovd82aaef2020-07-10 16:11:56 +0200345 * Load guest's/host's cr3 on nested vmentry or vmexit. @nested_npt is true
346 * if we are emulating VM-Entry into a guest with NPT enabled.
Vitaly Kuznetsov62156f62020-07-10 16:11:53 +0200347 */
348static int nested_svm_load_cr3(struct kvm_vcpu *vcpu, unsigned long cr3,
349 bool nested_npt)
350{
Vitaly Kuznetsova506fdd2020-07-10 16:11:55 +0200351 if (cr3 & rsvd_bits(cpuid_maxphyaddr(vcpu), 63))
352 return -EINVAL;
353
354 if (!nested_npt && is_pae_paging(vcpu) &&
355 (cr3 != kvm_read_cr3(vcpu) || pdptrs_changed(vcpu))) {
356 if (!load_pdptrs(vcpu, vcpu->arch.walk_mmu, cr3))
357 return -EINVAL;
358 }
359
360 /*
361 * TODO: optimize unconditional TLB flush/MMU sync here and in
362 * kvm_init_shadow_npt_mmu().
363 */
364 if (!nested_npt)
365 kvm_mmu_new_pgd(vcpu, cr3, false, false);
366
367 vcpu->arch.cr3 = cr3;
368 kvm_register_mark_available(vcpu, VCPU_EXREG_CR3);
369
370 kvm_init_mmu(vcpu, false);
371
372 return 0;
Vitaly Kuznetsov62156f62020-07-10 16:11:53 +0200373}
374
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300375static void nested_prepare_vmcb_save(struct vcpu_svm *svm, struct vmcb *vmcb12)
Joerg Roedel883b0a92020-03-24 10:41:52 +0100376{
Joerg Roedel883b0a92020-03-24 10:41:52 +0100377 /* Load the nested guest state */
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300378 svm->vmcb->save.es = vmcb12->save.es;
379 svm->vmcb->save.cs = vmcb12->save.cs;
380 svm->vmcb->save.ss = vmcb12->save.ss;
381 svm->vmcb->save.ds = vmcb12->save.ds;
382 svm->vmcb->save.gdtr = vmcb12->save.gdtr;
383 svm->vmcb->save.idtr = vmcb12->save.idtr;
Paolo Bonzini8cce12b2020-11-27 12:46:36 -0500384 kvm_set_rflags(&svm->vcpu, vmcb12->save.rflags | X86_EFLAGS_FIXED);
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300385 svm_set_efer(&svm->vcpu, vmcb12->save.efer);
386 svm_set_cr0(&svm->vcpu, vmcb12->save.cr0);
387 svm_set_cr4(&svm->vcpu, vmcb12->save.cr4);
388 svm->vmcb->save.cr2 = svm->vcpu.arch.cr2 = vmcb12->save.cr2;
389 kvm_rax_write(&svm->vcpu, vmcb12->save.rax);
390 kvm_rsp_write(&svm->vcpu, vmcb12->save.rsp);
391 kvm_rip_write(&svm->vcpu, vmcb12->save.rip);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100392
393 /* In case we don't even reach vcpu_run, the fields are not updated */
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300394 svm->vmcb->save.rax = vmcb12->save.rax;
395 svm->vmcb->save.rsp = vmcb12->save.rsp;
396 svm->vmcb->save.rip = vmcb12->save.rip;
Paolo Bonzini8cce12b2020-11-27 12:46:36 -0500397 svm->vmcb->save.dr7 = vmcb12->save.dr7 | DR7_FIXED_1;
398 svm->vcpu.arch.dr6 = vmcb12->save.dr6 | DR6_FIXED_1 | DR6_RTM;
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300399 svm->vmcb->save.cpl = vmcb12->save.cpl;
Paolo Bonzinif241d712020-05-18 10:56:43 -0400400}
Joerg Roedel883b0a92020-03-24 10:41:52 +0100401
Paolo Bonzinie670bf62020-05-13 13:16:12 -0400402static void nested_prepare_vmcb_control(struct vcpu_svm *svm)
Paolo Bonzinif241d712020-05-18 10:56:43 -0400403{
Paolo Bonzini91b71302020-05-22 12:28:52 -0400404 const u32 mask = V_INTR_MASKING_MASK | V_GIF_ENABLE_MASK | V_GIF_MASK;
Vitaly Kuznetsov62156f62020-07-10 16:11:53 +0200405
406 if (nested_npt_enabled(svm))
Paolo Bonzini69cb8772020-05-22 05:27:46 -0400407 nested_svm_init_mmu_context(&svm->vcpu);
408
Paolo Bonzini18fc6c52020-05-18 11:07:08 -0400409 svm->vmcb->control.tsc_offset = svm->vcpu.arch.tsc_offset =
Paolo Bonzinie670bf62020-05-13 13:16:12 -0400410 svm->vcpu.arch.l1_tsc_offset + svm->nested.ctl.tsc_offset;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100411
Paolo Bonzini91b71302020-05-22 12:28:52 -0400412 svm->vmcb->control.int_ctl =
413 (svm->nested.ctl.int_ctl & ~mask) |
414 (svm->nested.hsave->control.int_ctl & mask);
415
Paolo Bonzinie670bf62020-05-13 13:16:12 -0400416 svm->vmcb->control.virt_ext = svm->nested.ctl.virt_ext;
417 svm->vmcb->control.int_vector = svm->nested.ctl.int_vector;
418 svm->vmcb->control.int_state = svm->nested.ctl.int_state;
419 svm->vmcb->control.event_inj = svm->nested.ctl.event_inj;
420 svm->vmcb->control.event_inj_err = svm->nested.ctl.event_inj_err;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100421
Paolo Bonzinie670bf62020-05-13 13:16:12 -0400422 svm->vmcb->control.pause_filter_count = svm->nested.ctl.pause_filter_count;
423 svm->vmcb->control.pause_filter_thresh = svm->nested.ctl.pause_filter_thresh;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100424
Joerg Roedel883b0a92020-03-24 10:41:52 +0100425 /* Enter Guest-Mode */
426 enter_guest_mode(&svm->vcpu);
427
428 /*
429 * Merge guest and host intercepts - must be called with vcpu in
430 * guest-mode to take affect here
431 */
432 recalc_intercepts(svm);
433
Joerg Roedel06e78522020-06-25 10:03:23 +0200434 vmcb_mark_all_dirty(svm->vmcb);
Paolo Bonzinif241d712020-05-18 10:56:43 -0400435}
436
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300437int enter_svm_guest_mode(struct vcpu_svm *svm, u64 vmcb12_gpa,
438 struct vmcb *vmcb12)
Paolo Bonzinif241d712020-05-18 10:56:43 -0400439{
Vitaly Kuznetsova506fdd2020-07-10 16:11:55 +0200440 int ret;
441
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300442 svm->nested.vmcb12_gpa = vmcb12_gpa;
443 load_nested_vmcb_control(svm, &vmcb12->control);
444 nested_prepare_vmcb_save(svm, vmcb12);
Paolo Bonzinie670bf62020-05-13 13:16:12 -0400445 nested_prepare_vmcb_control(svm);
Paolo Bonzinif241d712020-05-18 10:56:43 -0400446
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300447 ret = nested_svm_load_cr3(&svm->vcpu, vmcb12->save.cr3,
Vitaly Kuznetsova506fdd2020-07-10 16:11:55 +0200448 nested_npt_enabled(svm));
449 if (ret)
450 return ret;
451
Paolo Bonziniffdf7f92020-05-22 12:18:27 -0400452 svm_set_gif(svm, true);
Vitaly Kuznetsov59cd9bc2020-07-10 16:11:52 +0200453
454 return 0;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100455}
456
457int nested_svm_vmrun(struct vcpu_svm *svm)
458{
459 int ret;
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300460 struct vmcb *vmcb12;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100461 struct vmcb *hsave = svm->nested.hsave;
462 struct vmcb *vmcb = svm->vmcb;
463 struct kvm_host_map map;
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300464 u64 vmcb12_gpa;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100465
Paolo Bonzini7c67f5462020-04-23 10:52:48 -0400466 if (is_smm(&svm->vcpu)) {
467 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
468 return 1;
469 }
Joerg Roedel883b0a92020-03-24 10:41:52 +0100470
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300471 vmcb12_gpa = svm->vmcb->save.rax;
472 ret = kvm_vcpu_map(&svm->vcpu, gpa_to_gfn(vmcb12_gpa), &map);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100473 if (ret == -EINVAL) {
474 kvm_inject_gp(&svm->vcpu, 0);
475 return 1;
476 } else if (ret) {
477 return kvm_skip_emulated_instruction(&svm->vcpu);
478 }
479
480 ret = kvm_skip_emulated_instruction(&svm->vcpu);
481
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300482 vmcb12 = map.hva;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100483
Maxim Levitsky2fcf4872020-10-01 14:29:54 +0300484 if (WARN_ON_ONCE(!svm->nested.initialized))
485 return -EINVAL;
486
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300487 if (!nested_vmcb_checks(svm, vmcb12)) {
488 vmcb12->control.exit_code = SVM_EXIT_ERR;
489 vmcb12->control.exit_code_hi = 0;
490 vmcb12->control.exit_info_1 = 0;
491 vmcb12->control.exit_info_2 = 0;
Paolo Bonzini69c9dfa2020-05-13 12:57:26 -0400492 goto out;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100493 }
494
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300495 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);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100500
Babu Moger03bfeeb2020-09-11 14:28:05 -0500501 trace_kvm_nested_intercepts(vmcb12->control.intercepts[INTERCEPT_CR] & 0xffff,
502 vmcb12->control.intercepts[INTERCEPT_CR] >> 16,
Babu Moger9780d512020-09-11 14:28:20 -0500503 vmcb12->control.intercepts[INTERCEPT_EXCEPTION],
Babu Mogerc62e2e92020-09-11 14:28:28 -0500504 vmcb12->control.intercepts[INTERCEPT_WORD3],
Babu Moger4c44e8d2020-09-11 14:28:35 -0500505 vmcb12->control.intercepts[INTERCEPT_WORD4],
506 vmcb12->control.intercepts[INTERCEPT_WORD5]);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100507
508 /* Clear internal status */
509 kvm_clear_exception_queue(&svm->vcpu);
510 kvm_clear_interrupt_queue(&svm->vcpu);
511
512 /*
513 * Save the old vmcb, so we don't need to pick what we save, but can
514 * restore everything when a VMEXIT occurs
515 */
516 hsave->save.es = vmcb->save.es;
517 hsave->save.cs = vmcb->save.cs;
518 hsave->save.ss = vmcb->save.ss;
519 hsave->save.ds = vmcb->save.ds;
520 hsave->save.gdtr = vmcb->save.gdtr;
521 hsave->save.idtr = vmcb->save.idtr;
522 hsave->save.efer = svm->vcpu.arch.efer;
523 hsave->save.cr0 = kvm_read_cr0(&svm->vcpu);
524 hsave->save.cr4 = svm->vcpu.arch.cr4;
525 hsave->save.rflags = kvm_get_rflags(&svm->vcpu);
526 hsave->save.rip = kvm_rip_read(&svm->vcpu);
527 hsave->save.rsp = vmcb->save.rsp;
528 hsave->save.rax = vmcb->save.rax;
529 if (npt_enabled)
530 hsave->save.cr3 = vmcb->save.cr3;
531 else
532 hsave->save.cr3 = kvm_read_cr3(&svm->vcpu);
533
Paolo Bonzini2f675912020-05-18 15:21:22 -0400534 copy_vmcb_control_area(&hsave->control, &vmcb->control);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100535
Paolo Bonzinif74f9412020-04-23 13:22:27 -0400536 svm->nested.nested_run_pending = 1;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100537
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300538 if (enter_svm_guest_mode(svm, vmcb12_gpa, vmcb12))
Vitaly Kuznetsov59cd9bc2020-07-10 16:11:52 +0200539 goto out_exit_err;
Vitaly Kuznetsovebdb3db2020-07-10 16:11:51 +0200540
Vitaly Kuznetsov59cd9bc2020-07-10 16:11:52 +0200541 if (nested_svm_vmrun_msrpm(svm))
542 goto out;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100543
Vitaly Kuznetsov59cd9bc2020-07-10 16:11:52 +0200544out_exit_err:
545 svm->nested.nested_run_pending = 0;
546
547 svm->vmcb->control.exit_code = SVM_EXIT_ERR;
548 svm->vmcb->control.exit_code_hi = 0;
549 svm->vmcb->control.exit_info_1 = 0;
550 svm->vmcb->control.exit_info_2 = 0;
551
552 nested_svm_vmexit(svm);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100553
Paolo Bonzini69c9dfa2020-05-13 12:57:26 -0400554out:
555 kvm_vcpu_unmap(&svm->vcpu, &map, true);
556
Joerg Roedel883b0a92020-03-24 10:41:52 +0100557 return ret;
558}
559
560void nested_svm_vmloadsave(struct vmcb *from_vmcb, struct vmcb *to_vmcb)
561{
562 to_vmcb->save.fs = from_vmcb->save.fs;
563 to_vmcb->save.gs = from_vmcb->save.gs;
564 to_vmcb->save.tr = from_vmcb->save.tr;
565 to_vmcb->save.ldtr = from_vmcb->save.ldtr;
566 to_vmcb->save.kernel_gs_base = from_vmcb->save.kernel_gs_base;
567 to_vmcb->save.star = from_vmcb->save.star;
568 to_vmcb->save.lstar = from_vmcb->save.lstar;
569 to_vmcb->save.cstar = from_vmcb->save.cstar;
570 to_vmcb->save.sfmask = from_vmcb->save.sfmask;
571 to_vmcb->save.sysenter_cs = from_vmcb->save.sysenter_cs;
572 to_vmcb->save.sysenter_esp = from_vmcb->save.sysenter_esp;
573 to_vmcb->save.sysenter_eip = from_vmcb->save.sysenter_eip;
574}
575
576int nested_svm_vmexit(struct vcpu_svm *svm)
577{
578 int rc;
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300579 struct vmcb *vmcb12;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100580 struct vmcb *hsave = svm->nested.hsave;
581 struct vmcb *vmcb = svm->vmcb;
582 struct kvm_host_map map;
583
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300584 rc = kvm_vcpu_map(&svm->vcpu, gpa_to_gfn(svm->nested.vmcb12_gpa), &map);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100585 if (rc) {
586 if (rc == -EINVAL)
587 kvm_inject_gp(&svm->vcpu, 0);
588 return 1;
589 }
590
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300591 vmcb12 = map.hva;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100592
593 /* Exit Guest-Mode */
594 leave_guest_mode(&svm->vcpu);
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300595 svm->nested.vmcb12_gpa = 0;
Paolo Bonzini2d8a42b2020-05-22 03:50:14 -0400596 WARN_ON_ONCE(svm->nested.nested_run_pending);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100597
Paolo Bonzini38c0b192020-04-23 13:13:09 -0400598 /* in case we halted in L2 */
599 svm->vcpu.arch.mp_state = KVM_MP_STATE_RUNNABLE;
600
Joerg Roedel883b0a92020-03-24 10:41:52 +0100601 /* Give the current vmcb to the guest */
Joerg Roedel883b0a92020-03-24 10:41:52 +0100602
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300603 vmcb12->save.es = vmcb->save.es;
604 vmcb12->save.cs = vmcb->save.cs;
605 vmcb12->save.ss = vmcb->save.ss;
606 vmcb12->save.ds = vmcb->save.ds;
607 vmcb12->save.gdtr = vmcb->save.gdtr;
608 vmcb12->save.idtr = vmcb->save.idtr;
609 vmcb12->save.efer = svm->vcpu.arch.efer;
610 vmcb12->save.cr0 = kvm_read_cr0(&svm->vcpu);
611 vmcb12->save.cr3 = kvm_read_cr3(&svm->vcpu);
612 vmcb12->save.cr2 = vmcb->save.cr2;
613 vmcb12->save.cr4 = svm->vcpu.arch.cr4;
614 vmcb12->save.rflags = kvm_get_rflags(&svm->vcpu);
615 vmcb12->save.rip = kvm_rip_read(&svm->vcpu);
616 vmcb12->save.rsp = kvm_rsp_read(&svm->vcpu);
617 vmcb12->save.rax = kvm_rax_read(&svm->vcpu);
618 vmcb12->save.dr7 = vmcb->save.dr7;
619 vmcb12->save.dr6 = svm->vcpu.arch.dr6;
620 vmcb12->save.cpl = vmcb->save.cpl;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100621
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300622 vmcb12->control.int_state = vmcb->control.int_state;
623 vmcb12->control.exit_code = vmcb->control.exit_code;
624 vmcb12->control.exit_code_hi = vmcb->control.exit_code_hi;
625 vmcb12->control.exit_info_1 = vmcb->control.exit_info_1;
626 vmcb12->control.exit_info_2 = vmcb->control.exit_info_2;
Paolo Bonzini36e2e982020-05-22 06:04:57 -0400627
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300628 if (vmcb12->control.exit_code != SVM_EXIT_ERR)
629 nested_vmcb_save_pending_event(svm, vmcb12);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100630
631 if (svm->nrips_enabled)
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300632 vmcb12->control.next_rip = vmcb->control.next_rip;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100633
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300634 vmcb12->control.int_ctl = svm->nested.ctl.int_ctl;
635 vmcb12->control.tlb_ctl = svm->nested.ctl.tlb_ctl;
636 vmcb12->control.event_inj = svm->nested.ctl.event_inj;
637 vmcb12->control.event_inj_err = svm->nested.ctl.event_inj_err;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100638
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300639 vmcb12->control.pause_filter_count =
Joerg Roedel883b0a92020-03-24 10:41:52 +0100640 svm->vmcb->control.pause_filter_count;
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300641 vmcb12->control.pause_filter_thresh =
Joerg Roedel883b0a92020-03-24 10:41:52 +0100642 svm->vmcb->control.pause_filter_thresh;
643
Joerg Roedel883b0a92020-03-24 10:41:52 +0100644 /* Restore the original control entries */
Paolo Bonzini2f675912020-05-18 15:21:22 -0400645 copy_vmcb_control_area(&vmcb->control, &hsave->control);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100646
Maxim Levitsky98837642020-08-27 19:27:18 +0300647 /* On vmexit the GIF is set to false */
648 svm_set_gif(svm, false);
649
Paolo Bonzini18fc6c52020-05-18 11:07:08 -0400650 svm->vmcb->control.tsc_offset = svm->vcpu.arch.tsc_offset =
651 svm->vcpu.arch.l1_tsc_offset;
652
Paolo Bonzinie670bf62020-05-13 13:16:12 -0400653 svm->nested.ctl.nested_cr3 = 0;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100654
655 /* Restore selected save entries */
656 svm->vmcb->save.es = hsave->save.es;
657 svm->vmcb->save.cs = hsave->save.cs;
658 svm->vmcb->save.ss = hsave->save.ss;
659 svm->vmcb->save.ds = hsave->save.ds;
660 svm->vmcb->save.gdtr = hsave->save.gdtr;
661 svm->vmcb->save.idtr = hsave->save.idtr;
662 kvm_set_rflags(&svm->vcpu, hsave->save.rflags);
Paolo Bonzini8cce12b2020-11-27 12:46:36 -0500663 kvm_set_rflags(&svm->vcpu, hsave->save.rflags | X86_EFLAGS_FIXED);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100664 svm_set_efer(&svm->vcpu, hsave->save.efer);
665 svm_set_cr0(&svm->vcpu, hsave->save.cr0 | X86_CR0_PE);
666 svm_set_cr4(&svm->vcpu, hsave->save.cr4);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100667 kvm_rax_write(&svm->vcpu, hsave->save.rax);
668 kvm_rsp_write(&svm->vcpu, hsave->save.rsp);
669 kvm_rip_write(&svm->vcpu, hsave->save.rip);
Paolo Bonzini8cce12b2020-11-27 12:46:36 -0500670 svm->vmcb->save.dr7 = DR7_FIXED_1;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100671 svm->vmcb->save.cpl = 0;
672 svm->vmcb->control.exit_int_info = 0;
673
Joerg Roedel06e78522020-06-25 10:03:23 +0200674 vmcb_mark_all_dirty(svm->vmcb);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100675
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300676 trace_kvm_nested_vmexit_inject(vmcb12->control.exit_code,
677 vmcb12->control.exit_info_1,
678 vmcb12->control.exit_info_2,
679 vmcb12->control.exit_int_info,
680 vmcb12->control.exit_int_info_err,
Paolo Bonzini36e2e982020-05-22 06:04:57 -0400681 KVM_ISA_SVM);
682
Joerg Roedel883b0a92020-03-24 10:41:52 +0100683 kvm_vcpu_unmap(&svm->vcpu, &map, true);
684
685 nested_svm_uninit_mmu_context(&svm->vcpu);
Vitaly Kuznetsovbf7dea42020-07-10 16:11:54 +0200686
Vitaly Kuznetsovd82aaef2020-07-10 16:11:56 +0200687 rc = nested_svm_load_cr3(&svm->vcpu, hsave->save.cr3, false);
688 if (rc)
689 return 1;
Vitaly Kuznetsovbf7dea42020-07-10 16:11:54 +0200690
Vitaly Kuznetsovd82aaef2020-07-10 16:11:56 +0200691 if (npt_enabled)
692 svm->vmcb->save.cr3 = hsave->save.cr3;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100693
694 /*
695 * Drop what we picked up for L2 via svm_complete_interrupts() so it
696 * doesn't end up in L1.
697 */
698 svm->vcpu.arch.nmi_injected = false;
699 kvm_clear_exception_queue(&svm->vcpu);
700 kvm_clear_interrupt_queue(&svm->vcpu);
701
702 return 0;
703}
704
Maxim Levitsky2fcf4872020-10-01 14:29:54 +0300705int svm_allocate_nested(struct vcpu_svm *svm)
706{
707 struct page *hsave_page;
708
709 if (svm->nested.initialized)
710 return 0;
711
712 hsave_page = alloc_page(GFP_KERNEL_ACCOUNT | __GFP_ZERO);
713 if (!hsave_page)
714 return -ENOMEM;
715 svm->nested.hsave = page_address(hsave_page);
716
717 svm->nested.msrpm = svm_vcpu_alloc_msrpm();
718 if (!svm->nested.msrpm)
719 goto err_free_hsave;
720 svm_vcpu_init_msrpm(&svm->vcpu, svm->nested.msrpm);
721
722 svm->nested.initialized = true;
723 return 0;
724
725err_free_hsave:
726 __free_page(hsave_page);
727 return -ENOMEM;
728}
729
730void svm_free_nested(struct vcpu_svm *svm)
731{
732 if (!svm->nested.initialized)
733 return;
734
735 svm_vcpu_free_msrpm(svm->nested.msrpm);
736 svm->nested.msrpm = NULL;
737
738 __free_page(virt_to_page(svm->nested.hsave));
739 svm->nested.hsave = NULL;
740
741 svm->nested.initialized = false;
742}
743
Paolo Bonzinic513f482020-05-18 13:08:37 -0400744/*
745 * Forcibly leave nested mode in order to be able to reset the VCPU later on.
746 */
747void svm_leave_nested(struct vcpu_svm *svm)
748{
749 if (is_guest_mode(&svm->vcpu)) {
750 struct vmcb *hsave = svm->nested.hsave;
751 struct vmcb *vmcb = svm->vmcb;
752
753 svm->nested.nested_run_pending = 0;
754 leave_guest_mode(&svm->vcpu);
755 copy_vmcb_control_area(&vmcb->control, &hsave->control);
756 nested_svm_uninit_mmu_context(&svm->vcpu);
757 }
Paolo Bonzinia7d5c7c2020-09-22 07:43:14 -0400758
759 kvm_clear_request(KVM_REQ_GET_NESTED_STATE_PAGES, &svm->vcpu);
Paolo Bonzinic513f482020-05-18 13:08:37 -0400760}
761
Joerg Roedel883b0a92020-03-24 10:41:52 +0100762static int nested_svm_exit_handled_msr(struct vcpu_svm *svm)
763{
764 u32 offset, msr, value;
765 int write, mask;
766
Babu Mogerc62e2e92020-09-11 14:28:28 -0500767 if (!(vmcb_is_intercept(&svm->nested.ctl, INTERCEPT_MSR_PROT)))
Joerg Roedel883b0a92020-03-24 10:41:52 +0100768 return NESTED_EXIT_HOST;
769
770 msr = svm->vcpu.arch.regs[VCPU_REGS_RCX];
771 offset = svm_msrpm_offset(msr);
772 write = svm->vmcb->control.exit_info_1 & 1;
773 mask = 1 << ((2 * (msr & 0xf)) + write);
774
775 if (offset == MSR_INVALID)
776 return NESTED_EXIT_DONE;
777
778 /* Offset is in 32 bit units but need in 8 bit units */
779 offset *= 4;
780
Paolo Bonzinie670bf62020-05-13 13:16:12 -0400781 if (kvm_vcpu_read_guest(&svm->vcpu, svm->nested.ctl.msrpm_base_pa + offset, &value, 4))
Joerg Roedel883b0a92020-03-24 10:41:52 +0100782 return NESTED_EXIT_DONE;
783
784 return (value & mask) ? NESTED_EXIT_DONE : NESTED_EXIT_HOST;
785}
786
Joerg Roedel883b0a92020-03-24 10:41:52 +0100787static int nested_svm_intercept_ioio(struct vcpu_svm *svm)
788{
789 unsigned port, size, iopm_len;
790 u16 val, mask;
791 u8 start_bit;
792 u64 gpa;
793
Babu Mogerc62e2e92020-09-11 14:28:28 -0500794 if (!(vmcb_is_intercept(&svm->nested.ctl, INTERCEPT_IOIO_PROT)))
Joerg Roedel883b0a92020-03-24 10:41:52 +0100795 return NESTED_EXIT_HOST;
796
797 port = svm->vmcb->control.exit_info_1 >> 16;
798 size = (svm->vmcb->control.exit_info_1 & SVM_IOIO_SIZE_MASK) >>
799 SVM_IOIO_SIZE_SHIFT;
Paolo Bonzinie670bf62020-05-13 13:16:12 -0400800 gpa = svm->nested.ctl.iopm_base_pa + (port / 8);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100801 start_bit = port % 8;
802 iopm_len = (start_bit + size > 8) ? 2 : 1;
803 mask = (0xf >> (4 - size)) << start_bit;
804 val = 0;
805
806 if (kvm_vcpu_read_guest(&svm->vcpu, gpa, &val, iopm_len))
807 return NESTED_EXIT_DONE;
808
809 return (val & mask) ? NESTED_EXIT_DONE : NESTED_EXIT_HOST;
810}
811
812static int nested_svm_intercept(struct vcpu_svm *svm)
813{
814 u32 exit_code = svm->vmcb->control.exit_code;
815 int vmexit = NESTED_EXIT_HOST;
816
817 switch (exit_code) {
818 case SVM_EXIT_MSR:
819 vmexit = nested_svm_exit_handled_msr(svm);
820 break;
821 case SVM_EXIT_IOIO:
822 vmexit = nested_svm_intercept_ioio(svm);
823 break;
824 case SVM_EXIT_READ_CR0 ... SVM_EXIT_WRITE_CR8: {
Babu Moger03bfeeb2020-09-11 14:28:05 -0500825 if (vmcb_is_intercept(&svm->nested.ctl, exit_code))
Joerg Roedel883b0a92020-03-24 10:41:52 +0100826 vmexit = NESTED_EXIT_DONE;
827 break;
828 }
829 case SVM_EXIT_READ_DR0 ... SVM_EXIT_WRITE_DR7: {
Babu Moger30abaa882020-09-11 14:28:12 -0500830 if (vmcb_is_intercept(&svm->nested.ctl, exit_code))
Joerg Roedel883b0a92020-03-24 10:41:52 +0100831 vmexit = NESTED_EXIT_DONE;
832 break;
833 }
834 case SVM_EXIT_EXCP_BASE ... SVM_EXIT_EXCP_BASE + 0x1f: {
Paolo Bonzini7c866632020-05-16 08:42:28 -0400835 /*
836 * Host-intercepted exceptions have been checked already in
837 * nested_svm_exit_special. There is nothing to do here,
838 * the vmexit is injected by svm_check_nested_events.
839 */
840 vmexit = NESTED_EXIT_DONE;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100841 break;
842 }
843 case SVM_EXIT_ERR: {
844 vmexit = NESTED_EXIT_DONE;
845 break;
846 }
847 default: {
Babu Mogerc62e2e92020-09-11 14:28:28 -0500848 if (vmcb_is_intercept(&svm->nested.ctl, exit_code))
Joerg Roedel883b0a92020-03-24 10:41:52 +0100849 vmexit = NESTED_EXIT_DONE;
850 }
851 }
852
853 return vmexit;
854}
855
856int nested_svm_exit_handled(struct vcpu_svm *svm)
857{
858 int vmexit;
859
860 vmexit = nested_svm_intercept(svm);
861
862 if (vmexit == NESTED_EXIT_DONE)
863 nested_svm_vmexit(svm);
864
865 return vmexit;
866}
867
868int nested_svm_check_permissions(struct vcpu_svm *svm)
869{
870 if (!(svm->vcpu.arch.efer & EFER_SVME) ||
871 !is_paging(&svm->vcpu)) {
872 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
873 return 1;
874 }
875
876 if (svm->vmcb->save.cpl) {
877 kvm_inject_gp(&svm->vcpu, 0);
878 return 1;
879 }
880
881 return 0;
882}
883
Paolo Bonzini7c866632020-05-16 08:42:28 -0400884static bool nested_exit_on_exception(struct vcpu_svm *svm)
Joerg Roedel883b0a92020-03-24 10:41:52 +0100885{
Paolo Bonzini7c866632020-05-16 08:42:28 -0400886 unsigned int nr = svm->vcpu.arch.exception.nr;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100887
Babu Moger9780d512020-09-11 14:28:20 -0500888 return (svm->nested.ctl.intercepts[INTERCEPT_EXCEPTION] & BIT(nr));
Paolo Bonzini7c866632020-05-16 08:42:28 -0400889}
Joerg Roedel883b0a92020-03-24 10:41:52 +0100890
Paolo Bonzini7c866632020-05-16 08:42:28 -0400891static void nested_svm_inject_exception_vmexit(struct vcpu_svm *svm)
892{
893 unsigned int nr = svm->vcpu.arch.exception.nr;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100894
895 svm->vmcb->control.exit_code = SVM_EXIT_EXCP_BASE + nr;
896 svm->vmcb->control.exit_code_hi = 0;
Paolo Bonzini7c866632020-05-16 08:42:28 -0400897
898 if (svm->vcpu.arch.exception.has_error_code)
899 svm->vmcb->control.exit_info_1 = svm->vcpu.arch.exception.error_code;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100900
901 /*
902 * EXITINFO2 is undefined for all exception intercepts other
903 * than #PF.
904 */
Paolo Bonzini7c866632020-05-16 08:42:28 -0400905 if (nr == PF_VECTOR) {
906 if (svm->vcpu.arch.exception.nested_apf)
907 svm->vmcb->control.exit_info_2 = svm->vcpu.arch.apf.nested_apf_token;
908 else if (svm->vcpu.arch.exception.has_payload)
909 svm->vmcb->control.exit_info_2 = svm->vcpu.arch.exception.payload;
910 else
911 svm->vmcb->control.exit_info_2 = svm->vcpu.arch.cr2;
912 } else if (nr == DB_VECTOR) {
913 /* See inject_pending_event. */
914 kvm_deliver_exception_payload(&svm->vcpu);
915 if (svm->vcpu.arch.dr7 & DR7_GD) {
916 svm->vcpu.arch.dr7 &= ~DR7_GD;
917 kvm_update_dr7(&svm->vcpu);
918 }
919 } else
920 WARN_ON(svm->vcpu.arch.exception.has_payload);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100921
Paolo Bonzini7c866632020-05-16 08:42:28 -0400922 nested_svm_vmexit(svm);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100923}
924
Paolo Bonzini55714cd2020-04-23 08:17:28 -0400925static void nested_svm_smi(struct vcpu_svm *svm)
926{
927 svm->vmcb->control.exit_code = SVM_EXIT_SMI;
928 svm->vmcb->control.exit_info_1 = 0;
929 svm->vmcb->control.exit_info_2 = 0;
930
931 nested_svm_vmexit(svm);
932}
933
Cathy Avery9c3d3702020-04-14 16:11:06 -0400934static void nested_svm_nmi(struct vcpu_svm *svm)
935{
936 svm->vmcb->control.exit_code = SVM_EXIT_NMI;
937 svm->vmcb->control.exit_info_1 = 0;
938 svm->vmcb->control.exit_info_2 = 0;
939
940 nested_svm_vmexit(svm);
941}
942
Joerg Roedel883b0a92020-03-24 10:41:52 +0100943static void nested_svm_intr(struct vcpu_svm *svm)
944{
Paolo Bonzini6e085cb2020-04-23 13:15:33 -0400945 trace_kvm_nested_intr_vmexit(svm->vmcb->save.rip);
946
Joerg Roedel883b0a92020-03-24 10:41:52 +0100947 svm->vmcb->control.exit_code = SVM_EXIT_INTR;
948 svm->vmcb->control.exit_info_1 = 0;
949 svm->vmcb->control.exit_info_2 = 0;
950
Paolo Bonzini6e085cb2020-04-23 13:15:33 -0400951 nested_svm_vmexit(svm);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100952}
953
Paolo Bonzini5b6724082020-05-16 08:50:35 -0400954static inline bool nested_exit_on_init(struct vcpu_svm *svm)
955{
Babu Mogerc62e2e92020-09-11 14:28:28 -0500956 return vmcb_is_intercept(&svm->nested.ctl, INTERCEPT_INIT);
Paolo Bonzini5b6724082020-05-16 08:50:35 -0400957}
958
959static void nested_svm_init(struct vcpu_svm *svm)
960{
961 svm->vmcb->control.exit_code = SVM_EXIT_INIT;
962 svm->vmcb->control.exit_info_1 = 0;
963 svm->vmcb->control.exit_info_2 = 0;
964
965 nested_svm_vmexit(svm);
966}
967
968
Paolo Bonzini33b22172020-04-17 10:24:18 -0400969static int svm_check_nested_events(struct kvm_vcpu *vcpu)
Joerg Roedel883b0a92020-03-24 10:41:52 +0100970{
971 struct vcpu_svm *svm = to_svm(vcpu);
972 bool block_nested_events =
Paolo Bonzinibd279622020-05-16 08:46:00 -0400973 kvm_event_needs_reinjection(vcpu) || svm->nested.nested_run_pending;
Paolo Bonzini5b6724082020-05-16 08:50:35 -0400974 struct kvm_lapic *apic = vcpu->arch.apic;
975
976 if (lapic_in_kernel(vcpu) &&
977 test_bit(KVM_APIC_INIT, &apic->pending_events)) {
978 if (block_nested_events)
979 return -EBUSY;
980 if (!nested_exit_on_init(svm))
981 return 0;
982 nested_svm_init(svm);
983 return 0;
984 }
Joerg Roedel883b0a92020-03-24 10:41:52 +0100985
Paolo Bonzini7c866632020-05-16 08:42:28 -0400986 if (vcpu->arch.exception.pending) {
987 if (block_nested_events)
988 return -EBUSY;
989 if (!nested_exit_on_exception(svm))
990 return 0;
991 nested_svm_inject_exception_vmexit(svm);
992 return 0;
993 }
994
Paolo Bonzini221e7612020-04-23 08:13:10 -0400995 if (vcpu->arch.smi_pending && !svm_smi_blocked(vcpu)) {
Paolo Bonzini55714cd2020-04-23 08:17:28 -0400996 if (block_nested_events)
997 return -EBUSY;
Paolo Bonzini221e7612020-04-23 08:13:10 -0400998 if (!nested_exit_on_smi(svm))
999 return 0;
Paolo Bonzini55714cd2020-04-23 08:17:28 -04001000 nested_svm_smi(svm);
1001 return 0;
1002 }
1003
Paolo Bonzini221e7612020-04-23 08:13:10 -04001004 if (vcpu->arch.nmi_pending && !svm_nmi_blocked(vcpu)) {
Cathy Avery9c3d3702020-04-14 16:11:06 -04001005 if (block_nested_events)
1006 return -EBUSY;
Paolo Bonzini221e7612020-04-23 08:13:10 -04001007 if (!nested_exit_on_nmi(svm))
1008 return 0;
Cathy Avery9c3d3702020-04-14 16:11:06 -04001009 nested_svm_nmi(svm);
1010 return 0;
1011 }
1012
Paolo Bonzini221e7612020-04-23 08:13:10 -04001013 if (kvm_cpu_has_interrupt(vcpu) && !svm_interrupt_blocked(vcpu)) {
Joerg Roedel883b0a92020-03-24 10:41:52 +01001014 if (block_nested_events)
1015 return -EBUSY;
Paolo Bonzini221e7612020-04-23 08:13:10 -04001016 if (!nested_exit_on_intr(svm))
1017 return 0;
Joerg Roedel883b0a92020-03-24 10:41:52 +01001018 nested_svm_intr(svm);
1019 return 0;
1020 }
1021
1022 return 0;
1023}
1024
1025int nested_svm_exit_special(struct vcpu_svm *svm)
1026{
1027 u32 exit_code = svm->vmcb->control.exit_code;
1028
1029 switch (exit_code) {
1030 case SVM_EXIT_INTR:
1031 case SVM_EXIT_NMI:
Joerg Roedel883b0a92020-03-24 10:41:52 +01001032 case SVM_EXIT_NPF:
Paolo Bonzini7c866632020-05-16 08:42:28 -04001033 return NESTED_EXIT_HOST;
1034 case SVM_EXIT_EXCP_BASE ... SVM_EXIT_EXCP_BASE + 0x1f: {
1035 u32 excp_bits = 1 << (exit_code - SVM_EXIT_EXCP_BASE);
1036
Babu Moger9780d512020-09-11 14:28:20 -05001037 if (get_host_vmcb(svm)->control.intercepts[INTERCEPT_EXCEPTION] &
1038 excp_bits)
Paolo Bonzini7c866632020-05-16 08:42:28 -04001039 return NESTED_EXIT_HOST;
1040 else if (exit_code == SVM_EXIT_EXCP_BASE + PF_VECTOR &&
Vitaly Kuznetsov68fd66f2020-05-25 16:41:17 +02001041 svm->vcpu.arch.apf.host_apf_flags)
Paolo Bonzini7c866632020-05-16 08:42:28 -04001042 /* Trap async PF even if not shadowing */
Joerg Roedel883b0a92020-03-24 10:41:52 +01001043 return NESTED_EXIT_HOST;
1044 break;
Paolo Bonzini7c866632020-05-16 08:42:28 -04001045 }
Joerg Roedel883b0a92020-03-24 10:41:52 +01001046 default:
1047 break;
1048 }
1049
1050 return NESTED_EXIT_CONTINUE;
1051}
Paolo Bonzini33b22172020-04-17 10:24:18 -04001052
Paolo Bonzinicc440cd2020-05-13 13:36:32 -04001053static int svm_get_nested_state(struct kvm_vcpu *vcpu,
1054 struct kvm_nested_state __user *user_kvm_nested_state,
1055 u32 user_data_size)
1056{
1057 struct vcpu_svm *svm;
1058 struct kvm_nested_state kvm_state = {
1059 .flags = 0,
1060 .format = KVM_STATE_NESTED_FORMAT_SVM,
1061 .size = sizeof(kvm_state),
1062 };
1063 struct vmcb __user *user_vmcb = (struct vmcb __user *)
1064 &user_kvm_nested_state->data.svm[0];
1065
1066 if (!vcpu)
1067 return kvm_state.size + KVM_STATE_NESTED_SVM_VMCB_SIZE;
1068
1069 svm = to_svm(vcpu);
1070
1071 if (user_data_size < kvm_state.size)
1072 goto out;
1073
1074 /* First fill in the header and copy it out. */
1075 if (is_guest_mode(vcpu)) {
Maxim Levitsky0dd16b52020-08-27 20:11:39 +03001076 kvm_state.hdr.svm.vmcb_pa = svm->nested.vmcb12_gpa;
Paolo Bonzinicc440cd2020-05-13 13:36:32 -04001077 kvm_state.size += KVM_STATE_NESTED_SVM_VMCB_SIZE;
1078 kvm_state.flags |= KVM_STATE_NESTED_GUEST_MODE;
1079
1080 if (svm->nested.nested_run_pending)
1081 kvm_state.flags |= KVM_STATE_NESTED_RUN_PENDING;
1082 }
1083
1084 if (gif_set(svm))
1085 kvm_state.flags |= KVM_STATE_NESTED_GIF_SET;
1086
1087 if (copy_to_user(user_kvm_nested_state, &kvm_state, sizeof(kvm_state)))
1088 return -EFAULT;
1089
1090 if (!is_guest_mode(vcpu))
1091 goto out;
1092
1093 /*
1094 * Copy over the full size of the VMCB rather than just the size
1095 * of the structs.
1096 */
1097 if (clear_user(user_vmcb, KVM_STATE_NESTED_SVM_VMCB_SIZE))
1098 return -EFAULT;
1099 if (copy_to_user(&user_vmcb->control, &svm->nested.ctl,
1100 sizeof(user_vmcb->control)))
1101 return -EFAULT;
1102 if (copy_to_user(&user_vmcb->save, &svm->nested.hsave->save,
1103 sizeof(user_vmcb->save)))
1104 return -EFAULT;
1105
1106out:
1107 return kvm_state.size;
1108}
1109
1110static int svm_set_nested_state(struct kvm_vcpu *vcpu,
1111 struct kvm_nested_state __user *user_kvm_nested_state,
1112 struct kvm_nested_state *kvm_state)
1113{
1114 struct vcpu_svm *svm = to_svm(vcpu);
1115 struct vmcb *hsave = svm->nested.hsave;
1116 struct vmcb __user *user_vmcb = (struct vmcb __user *)
1117 &user_kvm_nested_state->data.svm[0];
Joerg Roedel6ccbd292020-09-07 15:15:02 +02001118 struct vmcb_control_area *ctl;
1119 struct vmcb_save_area *save;
1120 int ret;
Paolo Bonzinicc440cd2020-05-13 13:36:32 -04001121 u32 cr0;
1122
Joerg Roedel6ccbd292020-09-07 15:15:02 +02001123 BUILD_BUG_ON(sizeof(struct vmcb_control_area) + sizeof(struct vmcb_save_area) >
1124 KVM_STATE_NESTED_SVM_VMCB_SIZE);
1125
Paolo Bonzinicc440cd2020-05-13 13:36:32 -04001126 if (kvm_state->format != KVM_STATE_NESTED_FORMAT_SVM)
1127 return -EINVAL;
1128
1129 if (kvm_state->flags & ~(KVM_STATE_NESTED_GUEST_MODE |
1130 KVM_STATE_NESTED_RUN_PENDING |
1131 KVM_STATE_NESTED_GIF_SET))
1132 return -EINVAL;
1133
1134 /*
1135 * If in guest mode, vcpu->arch.efer actually refers to the L2 guest's
1136 * EFER.SVME, but EFER.SVME still has to be 1 for VMRUN to succeed.
1137 */
1138 if (!(vcpu->arch.efer & EFER_SVME)) {
1139 /* GIF=1 and no guest mode are required if SVME=0. */
1140 if (kvm_state->flags != KVM_STATE_NESTED_GIF_SET)
1141 return -EINVAL;
1142 }
1143
1144 /* SMM temporarily disables SVM, so we cannot be in guest mode. */
1145 if (is_smm(vcpu) && (kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE))
1146 return -EINVAL;
1147
1148 if (!(kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE)) {
1149 svm_leave_nested(svm);
Vitaly Kuznetsovd5cd6f32020-09-14 15:37:25 +02001150 svm_set_gif(svm, !!(kvm_state->flags & KVM_STATE_NESTED_GIF_SET));
1151 return 0;
Paolo Bonzinicc440cd2020-05-13 13:36:32 -04001152 }
1153
1154 if (!page_address_valid(vcpu, kvm_state->hdr.svm.vmcb_pa))
1155 return -EINVAL;
1156 if (kvm_state->size < sizeof(*kvm_state) + KVM_STATE_NESTED_SVM_VMCB_SIZE)
1157 return -EINVAL;
Paolo Bonzinicc440cd2020-05-13 13:36:32 -04001158
Joerg Roedel6ccbd292020-09-07 15:15:02 +02001159 ret = -ENOMEM;
1160 ctl = kzalloc(sizeof(*ctl), GFP_KERNEL);
1161 save = kzalloc(sizeof(*save), GFP_KERNEL);
1162 if (!ctl || !save)
1163 goto out_free;
1164
1165 ret = -EFAULT;
1166 if (copy_from_user(ctl, &user_vmcb->control, sizeof(*ctl)))
1167 goto out_free;
1168 if (copy_from_user(save, &user_vmcb->save, sizeof(*save)))
1169 goto out_free;
1170
1171 ret = -EINVAL;
1172 if (!nested_vmcb_check_controls(ctl))
1173 goto out_free;
Paolo Bonzinicc440cd2020-05-13 13:36:32 -04001174
1175 /*
1176 * Processor state contains L2 state. Check that it is
1177 * valid for guest mode (see nested_vmcb_checks).
1178 */
1179 cr0 = kvm_read_cr0(vcpu);
1180 if (((cr0 & X86_CR0_CD) == 0) && (cr0 & X86_CR0_NW))
Joerg Roedel6ccbd292020-09-07 15:15:02 +02001181 goto out_free;
Paolo Bonzinicc440cd2020-05-13 13:36:32 -04001182
1183 /*
1184 * Validate host state saved from before VMRUN (see
1185 * nested_svm_check_permissions).
1186 * TODO: validate reserved bits for all saved state.
1187 */
Joerg Roedel6ccbd292020-09-07 15:15:02 +02001188 if (!(save->cr0 & X86_CR0_PG))
1189 goto out_free;
Paolo Bonzinicc440cd2020-05-13 13:36:32 -04001190
1191 /*
1192 * All checks done, we can enter guest mode. L1 control fields
1193 * come from the nested save state. Guest state is already
1194 * in the registers, the save area of the nested state instead
1195 * contains saved L1 state.
1196 */
Maxim Levitsky81f76ad2021-01-07 11:38:52 +02001197
1198 svm->nested.nested_run_pending =
1199 !!(kvm_state->flags & KVM_STATE_NESTED_RUN_PENDING);
1200
Paolo Bonzinicc440cd2020-05-13 13:36:32 -04001201 copy_vmcb_control_area(&hsave->control, &svm->vmcb->control);
Joerg Roedel6ccbd292020-09-07 15:15:02 +02001202 hsave->save = *save;
Paolo Bonzinicc440cd2020-05-13 13:36:32 -04001203
Maxim Levitsky0dd16b52020-08-27 20:11:39 +03001204 svm->nested.vmcb12_gpa = kvm_state->hdr.svm.vmcb_pa;
Joerg Roedel6ccbd292020-09-07 15:15:02 +02001205 load_nested_vmcb_control(svm, ctl);
Paolo Bonzinicc440cd2020-05-13 13:36:32 -04001206 nested_prepare_vmcb_control(svm);
1207
Paolo Bonzinia7d5c7c2020-09-22 07:43:14 -04001208 kvm_make_request(KVM_REQ_GET_NESTED_STATE_PAGES, vcpu);
Joerg Roedel6ccbd292020-09-07 15:15:02 +02001209 ret = 0;
1210out_free:
1211 kfree(save);
1212 kfree(ctl);
1213
1214 return ret;
Paolo Bonzinicc440cd2020-05-13 13:36:32 -04001215}
1216
Paolo Bonzini33b22172020-04-17 10:24:18 -04001217struct kvm_x86_nested_ops svm_nested_ops = {
1218 .check_events = svm_check_nested_events,
Paolo Bonzinia7d5c7c2020-09-22 07:43:14 -04001219 .get_nested_state_pages = svm_get_nested_state_pages,
Paolo Bonzinicc440cd2020-05-13 13:36:32 -04001220 .get_state = svm_get_nested_state,
1221 .set_state = svm_set_nested_state,
Paolo Bonzini33b22172020-04-17 10:24:18 -04001222};