Mention branches and keyring.
[releases.git] / x86 / kvm / svm / nested.c
1 // 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>
22 #include <asm/debugreg.h>
23
24 #include "kvm_emulate.h"
25 #include "trace.h"
26 #include "mmu.h"
27 #include "x86.h"
28 #include "cpuid.h"
29 #include "lapic.h"
30 #include "svm.h"
31 #include "hyperv.h"
32
33 #define CC KVM_NESTED_VMENTER_CONSISTENCY_CHECK
34
35 static void nested_svm_inject_npf_exit(struct kvm_vcpu *vcpu,
36                                        struct x86_exception *fault)
37 {
38         struct vcpu_svm *svm = to_svm(vcpu);
39         struct vmcb *vmcb = svm->vmcb;
40
41         if (vmcb->control.exit_code != SVM_EXIT_NPF) {
42                 /*
43                  * TODO: track the cause of the nested page fault, and
44                  * correctly fill in the high bits of exit_info_1.
45                  */
46                 vmcb->control.exit_code = SVM_EXIT_NPF;
47                 vmcb->control.exit_code_hi = 0;
48                 vmcb->control.exit_info_1 = (1ULL << 32);
49                 vmcb->control.exit_info_2 = fault->address;
50         }
51
52         vmcb->control.exit_info_1 &= ~0xffffffffULL;
53         vmcb->control.exit_info_1 |= fault->error_code;
54
55         nested_svm_vmexit(svm);
56 }
57
58 static u64 nested_svm_get_tdp_pdptr(struct kvm_vcpu *vcpu, int index)
59 {
60         struct vcpu_svm *svm = to_svm(vcpu);
61         u64 cr3 = svm->nested.ctl.nested_cr3;
62         u64 pdpte;
63         int ret;
64
65         ret = kvm_vcpu_read_guest_page(vcpu, gpa_to_gfn(cr3), &pdpte,
66                                        offset_in_page(cr3) + index * 8, 8);
67         if (ret)
68                 return 0;
69         return pdpte;
70 }
71
72 static unsigned long nested_svm_get_tdp_cr3(struct kvm_vcpu *vcpu)
73 {
74         struct vcpu_svm *svm = to_svm(vcpu);
75
76         return svm->nested.ctl.nested_cr3;
77 }
78
79 static void nested_svm_init_mmu_context(struct kvm_vcpu *vcpu)
80 {
81         struct vcpu_svm *svm = to_svm(vcpu);
82
83         WARN_ON(mmu_is_nested(vcpu));
84
85         vcpu->arch.mmu = &vcpu->arch.guest_mmu;
86
87         /*
88          * The NPT format depends on L1's CR4 and EFER, which is in vmcb01.  Note,
89          * when called via KVM_SET_NESTED_STATE, that state may _not_ match current
90          * vCPU state.  CR0.WP is explicitly ignored, while CR0.PG is required.
91          */
92         kvm_init_shadow_npt_mmu(vcpu, X86_CR0_PG, svm->vmcb01.ptr->save.cr4,
93                                 svm->vmcb01.ptr->save.efer,
94                                 svm->nested.ctl.nested_cr3);
95         vcpu->arch.mmu->get_guest_pgd     = nested_svm_get_tdp_cr3;
96         vcpu->arch.mmu->get_pdptr         = nested_svm_get_tdp_pdptr;
97         vcpu->arch.mmu->inject_page_fault = nested_svm_inject_npf_exit;
98         vcpu->arch.walk_mmu              = &vcpu->arch.nested_mmu;
99 }
100
101 static void nested_svm_uninit_mmu_context(struct kvm_vcpu *vcpu)
102 {
103         vcpu->arch.mmu = &vcpu->arch.root_mmu;
104         vcpu->arch.walk_mmu = &vcpu->arch.root_mmu;
105 }
106
107 static bool nested_vmcb_needs_vls_intercept(struct vcpu_svm *svm)
108 {
109         if (!svm->v_vmload_vmsave_enabled)
110                 return true;
111
112         if (!nested_npt_enabled(svm))
113                 return true;
114
115         if (!(svm->nested.ctl.virt_ext & VIRTUAL_VMLOAD_VMSAVE_ENABLE_MASK))
116                 return true;
117
118         return false;
119 }
120
121 void recalc_intercepts(struct vcpu_svm *svm)
122 {
123         struct vmcb_control_area *c, *h;
124         struct vmcb_ctrl_area_cached *g;
125         unsigned int i;
126
127         vmcb_mark_dirty(svm->vmcb, VMCB_INTERCEPTS);
128
129         if (!is_guest_mode(&svm->vcpu))
130                 return;
131
132         c = &svm->vmcb->control;
133         h = &svm->vmcb01.ptr->control;
134         g = &svm->nested.ctl;
135
136         for (i = 0; i < MAX_INTERCEPT; i++)
137                 c->intercepts[i] = h->intercepts[i];
138
139         if (g->int_ctl & V_INTR_MASKING_MASK) {
140                 /* We only want the cr8 intercept bits of L1 */
141                 vmcb_clr_intercept(c, INTERCEPT_CR8_READ);
142                 vmcb_clr_intercept(c, INTERCEPT_CR8_WRITE);
143
144                 /*
145                  * Once running L2 with HF_VINTR_MASK, EFLAGS.IF does not
146                  * affect any interrupt we may want to inject; therefore,
147                  * interrupt window vmexits are irrelevant to L0.
148                  */
149                 vmcb_clr_intercept(c, INTERCEPT_VINTR);
150         }
151
152         /* We don't want to see VMMCALLs from a nested guest */
153         vmcb_clr_intercept(c, INTERCEPT_VMMCALL);
154
155         for (i = 0; i < MAX_INTERCEPT; i++)
156                 c->intercepts[i] |= g->intercepts[i];
157
158         /* If SMI is not intercepted, ignore guest SMI intercept as well  */
159         if (!intercept_smi)
160                 vmcb_clr_intercept(c, INTERCEPT_SMI);
161
162         if (nested_vmcb_needs_vls_intercept(svm)) {
163                 /*
164                  * If the virtual VMLOAD/VMSAVE is not enabled for the L2,
165                  * we must intercept these instructions to correctly
166                  * emulate them in case L1 doesn't intercept them.
167                  */
168                 vmcb_set_intercept(c, INTERCEPT_VMLOAD);
169                 vmcb_set_intercept(c, INTERCEPT_VMSAVE);
170         } else {
171                 WARN_ON(!(c->virt_ext & VIRTUAL_VMLOAD_VMSAVE_ENABLE_MASK));
172         }
173 }
174
175 /*
176  * Merge L0's (KVM) and L1's (Nested VMCB) MSR permission bitmaps. The function
177  * is optimized in that it only merges the parts where KVM MSR permission bitmap
178  * may contain zero bits.
179  */
180 static bool nested_svm_vmrun_msrpm(struct vcpu_svm *svm)
181 {
182         struct hv_vmcb_enlightenments *hve = &svm->nested.ctl.hv_enlightenments;
183         int i;
184
185         /*
186          * MSR bitmap update can be skipped when:
187          * - MSR bitmap for L1 hasn't changed.
188          * - Nested hypervisor (L1) is attempting to launch the same L2 as
189          *   before.
190          * - Nested hypervisor (L1) is using Hyper-V emulation interface and
191          * tells KVM (L0) there were no changes in MSR bitmap for L2.
192          */
193         if (!svm->nested.force_msr_bitmap_recalc &&
194             kvm_hv_hypercall_enabled(&svm->vcpu) &&
195             hve->hv_enlightenments_control.msr_bitmap &&
196             (svm->nested.ctl.clean & BIT(HV_VMCB_NESTED_ENLIGHTENMENTS)))
197                 goto set_msrpm_base_pa;
198
199         if (!(vmcb12_is_intercept(&svm->nested.ctl, INTERCEPT_MSR_PROT)))
200                 return true;
201
202         for (i = 0; i < MSRPM_OFFSETS; i++) {
203                 u32 value, p;
204                 u64 offset;
205
206                 if (msrpm_offsets[i] == 0xffffffff)
207                         break;
208
209                 p      = msrpm_offsets[i];
210
211                 /* x2apic msrs are intercepted always for the nested guest */
212                 if (is_x2apic_msrpm_offset(p))
213                         continue;
214
215                 offset = svm->nested.ctl.msrpm_base_pa + (p * 4);
216
217                 if (kvm_vcpu_read_guest(&svm->vcpu, offset, &value, 4))
218                         return false;
219
220                 svm->nested.msrpm[p] = svm->msrpm[p] | value;
221         }
222
223         svm->nested.force_msr_bitmap_recalc = false;
224
225 set_msrpm_base_pa:
226         svm->vmcb->control.msrpm_base_pa = __sme_set(__pa(svm->nested.msrpm));
227
228         return true;
229 }
230
231 /*
232  * Bits 11:0 of bitmap address are ignored by hardware
233  */
234 static bool nested_svm_check_bitmap_pa(struct kvm_vcpu *vcpu, u64 pa, u32 size)
235 {
236         u64 addr = PAGE_ALIGN(pa);
237
238         return kvm_vcpu_is_legal_gpa(vcpu, addr) &&
239             kvm_vcpu_is_legal_gpa(vcpu, addr + size - 1);
240 }
241
242 static bool __nested_vmcb_check_controls(struct kvm_vcpu *vcpu,
243                                          struct vmcb_ctrl_area_cached *control)
244 {
245         if (CC(!vmcb12_is_intercept(control, INTERCEPT_VMRUN)))
246                 return false;
247
248         if (CC(control->asid == 0))
249                 return false;
250
251         if (CC((control->nested_ctl & SVM_NESTED_CTL_NP_ENABLE) && !npt_enabled))
252                 return false;
253
254         if (CC(!nested_svm_check_bitmap_pa(vcpu, control->msrpm_base_pa,
255                                            MSRPM_SIZE)))
256                 return false;
257         if (CC(!nested_svm_check_bitmap_pa(vcpu, control->iopm_base_pa,
258                                            IOPM_SIZE)))
259                 return false;
260
261
262         return true;
263 }
264
265 /* Common checks that apply to both L1 and L2 state.  */
266 static bool __nested_vmcb_check_save(struct kvm_vcpu *vcpu,
267                                      struct vmcb_save_area_cached *save)
268 {
269         if (CC(!(save->efer & EFER_SVME)))
270                 return false;
271
272         if (CC((save->cr0 & X86_CR0_CD) == 0 && (save->cr0 & X86_CR0_NW)) ||
273             CC(save->cr0 & ~0xffffffffULL))
274                 return false;
275
276         if (CC(!kvm_dr6_valid(save->dr6)) || CC(!kvm_dr7_valid(save->dr7)))
277                 return false;
278
279         /*
280          * These checks are also performed by KVM_SET_SREGS,
281          * except that EFER.LMA is not checked by SVM against
282          * CR0.PG && EFER.LME.
283          */
284         if ((save->efer & EFER_LME) && (save->cr0 & X86_CR0_PG)) {
285                 if (CC(!(save->cr4 & X86_CR4_PAE)) ||
286                     CC(!(save->cr0 & X86_CR0_PE)) ||
287                     CC(kvm_vcpu_is_illegal_gpa(vcpu, save->cr3)))
288                         return false;
289         }
290
291         /* Note, SVM doesn't have any additional restrictions on CR4. */
292         if (CC(!__kvm_is_valid_cr4(vcpu, save->cr4)))
293                 return false;
294
295         if (CC(!kvm_valid_efer(vcpu, save->efer)))
296                 return false;
297
298         return true;
299 }
300
301 static bool nested_vmcb_check_save(struct kvm_vcpu *vcpu)
302 {
303         struct vcpu_svm *svm = to_svm(vcpu);
304         struct vmcb_save_area_cached *save = &svm->nested.save;
305
306         return __nested_vmcb_check_save(vcpu, save);
307 }
308
309 static bool nested_vmcb_check_controls(struct kvm_vcpu *vcpu)
310 {
311         struct vcpu_svm *svm = to_svm(vcpu);
312         struct vmcb_ctrl_area_cached *ctl = &svm->nested.ctl;
313
314         return __nested_vmcb_check_controls(vcpu, ctl);
315 }
316
317 static
318 void __nested_copy_vmcb_control_to_cache(struct kvm_vcpu *vcpu,
319                                          struct vmcb_ctrl_area_cached *to,
320                                          struct vmcb_control_area *from)
321 {
322         unsigned int i;
323
324         for (i = 0; i < MAX_INTERCEPT; i++)
325                 to->intercepts[i] = from->intercepts[i];
326
327         to->iopm_base_pa        = from->iopm_base_pa;
328         to->msrpm_base_pa       = from->msrpm_base_pa;
329         to->tsc_offset          = from->tsc_offset;
330         to->tlb_ctl             = from->tlb_ctl;
331         to->int_ctl             = from->int_ctl;
332         to->int_vector          = from->int_vector;
333         to->int_state           = from->int_state;
334         to->exit_code           = from->exit_code;
335         to->exit_code_hi        = from->exit_code_hi;
336         to->exit_info_1         = from->exit_info_1;
337         to->exit_info_2         = from->exit_info_2;
338         to->exit_int_info       = from->exit_int_info;
339         to->exit_int_info_err   = from->exit_int_info_err;
340         to->nested_ctl          = from->nested_ctl;
341         to->event_inj           = from->event_inj;
342         to->event_inj_err       = from->event_inj_err;
343         to->next_rip            = from->next_rip;
344         to->nested_cr3          = from->nested_cr3;
345         to->virt_ext            = from->virt_ext;
346         to->pause_filter_count  = from->pause_filter_count;
347         to->pause_filter_thresh = from->pause_filter_thresh;
348
349         /* Copy asid here because nested_vmcb_check_controls will check it.  */
350         to->asid           = from->asid;
351         to->msrpm_base_pa &= ~0x0fffULL;
352         to->iopm_base_pa  &= ~0x0fffULL;
353
354         /* Hyper-V extensions (Enlightened VMCB) */
355         if (kvm_hv_hypercall_enabled(vcpu)) {
356                 to->clean = from->clean;
357                 memcpy(&to->hv_enlightenments, &from->hv_enlightenments,
358                        sizeof(to->hv_enlightenments));
359         }
360 }
361
362 void nested_copy_vmcb_control_to_cache(struct vcpu_svm *svm,
363                                        struct vmcb_control_area *control)
364 {
365         __nested_copy_vmcb_control_to_cache(&svm->vcpu, &svm->nested.ctl, control);
366 }
367
368 static void __nested_copy_vmcb_save_to_cache(struct vmcb_save_area_cached *to,
369                                              struct vmcb_save_area *from)
370 {
371         /*
372          * Copy only fields that are validated, as we need them
373          * to avoid TOC/TOU races.
374          */
375         to->efer = from->efer;
376         to->cr0 = from->cr0;
377         to->cr3 = from->cr3;
378         to->cr4 = from->cr4;
379
380         to->dr6 = from->dr6;
381         to->dr7 = from->dr7;
382 }
383
384 void nested_copy_vmcb_save_to_cache(struct vcpu_svm *svm,
385                                     struct vmcb_save_area *save)
386 {
387         __nested_copy_vmcb_save_to_cache(&svm->nested.save, save);
388 }
389
390 /*
391  * Synchronize fields that are written by the processor, so that
392  * they can be copied back into the vmcb12.
393  */
394 void nested_sync_control_from_vmcb02(struct vcpu_svm *svm)
395 {
396         u32 mask;
397         svm->nested.ctl.event_inj      = svm->vmcb->control.event_inj;
398         svm->nested.ctl.event_inj_err  = svm->vmcb->control.event_inj_err;
399
400         /* Only a few fields of int_ctl are written by the processor.  */
401         mask = V_IRQ_MASK | V_TPR_MASK;
402         if (!(svm->nested.ctl.int_ctl & V_INTR_MASKING_MASK) &&
403             svm_is_intercept(svm, INTERCEPT_VINTR)) {
404                 /*
405                  * In order to request an interrupt window, L0 is usurping
406                  * svm->vmcb->control.int_ctl and possibly setting V_IRQ
407                  * even if it was clear in L1's VMCB.  Restoring it would be
408                  * wrong.  However, in this case V_IRQ will remain true until
409                  * interrupt_window_interception calls svm_clear_vintr and
410                  * restores int_ctl.  We can just leave it aside.
411                  */
412                 mask &= ~V_IRQ_MASK;
413         }
414
415         if (nested_vgif_enabled(svm))
416                 mask |= V_GIF_MASK;
417
418         svm->nested.ctl.int_ctl        &= ~mask;
419         svm->nested.ctl.int_ctl        |= svm->vmcb->control.int_ctl & mask;
420 }
421
422 /*
423  * Transfer any event that L0 or L1 wanted to inject into L2 to
424  * EXIT_INT_INFO.
425  */
426 static void nested_save_pending_event_to_vmcb12(struct vcpu_svm *svm,
427                                                 struct vmcb *vmcb12)
428 {
429         struct kvm_vcpu *vcpu = &svm->vcpu;
430         u32 exit_int_info = 0;
431         unsigned int nr;
432
433         if (vcpu->arch.exception.injected) {
434                 nr = vcpu->arch.exception.vector;
435                 exit_int_info = nr | SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_EXEPT;
436
437                 if (vcpu->arch.exception.has_error_code) {
438                         exit_int_info |= SVM_EVTINJ_VALID_ERR;
439                         vmcb12->control.exit_int_info_err =
440                                 vcpu->arch.exception.error_code;
441                 }
442
443         } else if (vcpu->arch.nmi_injected) {
444                 exit_int_info = SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_NMI;
445
446         } else if (vcpu->arch.interrupt.injected) {
447                 nr = vcpu->arch.interrupt.nr;
448                 exit_int_info = nr | SVM_EVTINJ_VALID;
449
450                 if (vcpu->arch.interrupt.soft)
451                         exit_int_info |= SVM_EVTINJ_TYPE_SOFT;
452                 else
453                         exit_int_info |= SVM_EVTINJ_TYPE_INTR;
454         }
455
456         vmcb12->control.exit_int_info = exit_int_info;
457 }
458
459 static void nested_svm_transition_tlb_flush(struct kvm_vcpu *vcpu)
460 {
461         /*
462          * TODO: optimize unconditional TLB flush/MMU sync.  A partial list of
463          * things to fix before this can be conditional:
464          *
465          *  - Flush TLBs for both L1 and L2 remote TLB flush
466          *  - Honor L1's request to flush an ASID on nested VMRUN
467          *  - Sync nested NPT MMU on VMRUN that flushes L2's ASID[*]
468          *  - Don't crush a pending TLB flush in vmcb02 on nested VMRUN
469          *  - Flush L1's ASID on KVM_REQ_TLB_FLUSH_GUEST
470          *
471          * [*] Unlike nested EPT, SVM's ASID management can invalidate nested
472          *     NPT guest-physical mappings on VMRUN.
473          */
474         kvm_make_request(KVM_REQ_MMU_SYNC, vcpu);
475         kvm_make_request(KVM_REQ_TLB_FLUSH_CURRENT, vcpu);
476 }
477
478 /*
479  * Load guest's/host's cr3 on nested vmentry or vmexit. @nested_npt is true
480  * if we are emulating VM-Entry into a guest with NPT enabled.
481  */
482 static int nested_svm_load_cr3(struct kvm_vcpu *vcpu, unsigned long cr3,
483                                bool nested_npt, bool reload_pdptrs)
484 {
485         if (CC(kvm_vcpu_is_illegal_gpa(vcpu, cr3)))
486                 return -EINVAL;
487
488         if (reload_pdptrs && !nested_npt && is_pae_paging(vcpu) &&
489             CC(!load_pdptrs(vcpu, cr3)))
490                 return -EINVAL;
491
492         vcpu->arch.cr3 = cr3;
493
494         /* Re-initialize the MMU, e.g. to pick up CR4 MMU role changes. */
495         kvm_init_mmu(vcpu);
496
497         if (!nested_npt)
498                 kvm_mmu_new_pgd(vcpu, cr3);
499
500         return 0;
501 }
502
503 void nested_vmcb02_compute_g_pat(struct vcpu_svm *svm)
504 {
505         if (!svm->nested.vmcb02.ptr)
506                 return;
507
508         /* FIXME: merge g_pat from vmcb01 and vmcb12.  */
509         svm->nested.vmcb02.ptr->save.g_pat = svm->vmcb01.ptr->save.g_pat;
510 }
511
512 static void nested_vmcb02_prepare_save(struct vcpu_svm *svm, struct vmcb *vmcb12)
513 {
514         bool new_vmcb12 = false;
515         struct vmcb *vmcb01 = svm->vmcb01.ptr;
516         struct vmcb *vmcb02 = svm->nested.vmcb02.ptr;
517
518         nested_vmcb02_compute_g_pat(svm);
519
520         /* Load the nested guest state */
521         if (svm->nested.vmcb12_gpa != svm->nested.last_vmcb12_gpa) {
522                 new_vmcb12 = true;
523                 svm->nested.last_vmcb12_gpa = svm->nested.vmcb12_gpa;
524                 svm->nested.force_msr_bitmap_recalc = true;
525         }
526
527         if (unlikely(new_vmcb12 || vmcb_is_dirty(vmcb12, VMCB_SEG))) {
528                 vmcb02->save.es = vmcb12->save.es;
529                 vmcb02->save.cs = vmcb12->save.cs;
530                 vmcb02->save.ss = vmcb12->save.ss;
531                 vmcb02->save.ds = vmcb12->save.ds;
532                 vmcb02->save.cpl = vmcb12->save.cpl;
533                 vmcb_mark_dirty(vmcb02, VMCB_SEG);
534         }
535
536         if (unlikely(new_vmcb12 || vmcb_is_dirty(vmcb12, VMCB_DT))) {
537                 vmcb02->save.gdtr = vmcb12->save.gdtr;
538                 vmcb02->save.idtr = vmcb12->save.idtr;
539                 vmcb_mark_dirty(vmcb02, VMCB_DT);
540         }
541
542         kvm_set_rflags(&svm->vcpu, vmcb12->save.rflags | X86_EFLAGS_FIXED);
543
544         svm_set_efer(&svm->vcpu, svm->nested.save.efer);
545
546         svm_set_cr0(&svm->vcpu, svm->nested.save.cr0);
547         svm_set_cr4(&svm->vcpu, svm->nested.save.cr4);
548
549         svm->vcpu.arch.cr2 = vmcb12->save.cr2;
550
551         kvm_rax_write(&svm->vcpu, vmcb12->save.rax);
552         kvm_rsp_write(&svm->vcpu, vmcb12->save.rsp);
553         kvm_rip_write(&svm->vcpu, vmcb12->save.rip);
554
555         /* In case we don't even reach vcpu_run, the fields are not updated */
556         vmcb02->save.rax = vmcb12->save.rax;
557         vmcb02->save.rsp = vmcb12->save.rsp;
558         vmcb02->save.rip = vmcb12->save.rip;
559
560         /* These bits will be set properly on the first execution when new_vmc12 is true */
561         if (unlikely(new_vmcb12 || vmcb_is_dirty(vmcb12, VMCB_DR))) {
562                 vmcb02->save.dr7 = svm->nested.save.dr7 | DR7_FIXED_1;
563                 svm->vcpu.arch.dr6  = svm->nested.save.dr6 | DR6_ACTIVE_LOW;
564                 vmcb_mark_dirty(vmcb02, VMCB_DR);
565         }
566
567         if (unlikely(svm->lbrv_enabled && (svm->nested.ctl.virt_ext & LBR_CTL_ENABLE_MASK))) {
568                 /*
569                  * Reserved bits of DEBUGCTL are ignored.  Be consistent with
570                  * svm_set_msr's definition of reserved bits.
571                  */
572                 svm_copy_lbrs(vmcb02, vmcb12);
573                 vmcb02->save.dbgctl &= ~DEBUGCTL_RESERVED_BITS;
574                 svm_update_lbrv(&svm->vcpu);
575
576         } else if (unlikely(vmcb01->control.virt_ext & LBR_CTL_ENABLE_MASK)) {
577                 svm_copy_lbrs(vmcb02, vmcb01);
578         }
579 }
580
581 static inline bool is_evtinj_soft(u32 evtinj)
582 {
583         u32 type = evtinj & SVM_EVTINJ_TYPE_MASK;
584         u8 vector = evtinj & SVM_EVTINJ_VEC_MASK;
585
586         if (!(evtinj & SVM_EVTINJ_VALID))
587                 return false;
588
589         if (type == SVM_EVTINJ_TYPE_SOFT)
590                 return true;
591
592         return type == SVM_EVTINJ_TYPE_EXEPT && kvm_exception_is_soft(vector);
593 }
594
595 static bool is_evtinj_nmi(u32 evtinj)
596 {
597         u32 type = evtinj & SVM_EVTINJ_TYPE_MASK;
598
599         if (!(evtinj & SVM_EVTINJ_VALID))
600                 return false;
601
602         return type == SVM_EVTINJ_TYPE_NMI;
603 }
604
605 static void nested_vmcb02_prepare_control(struct vcpu_svm *svm,
606                                           unsigned long vmcb12_rip,
607                                           unsigned long vmcb12_csbase)
608 {
609         u32 int_ctl_vmcb01_bits = V_INTR_MASKING_MASK;
610         u32 int_ctl_vmcb12_bits = V_TPR_MASK | V_IRQ_INJECTION_BITS_MASK;
611
612         struct kvm_vcpu *vcpu = &svm->vcpu;
613         struct vmcb *vmcb01 = svm->vmcb01.ptr;
614         struct vmcb *vmcb02 = svm->nested.vmcb02.ptr;
615         u32 pause_count12;
616         u32 pause_thresh12;
617
618         /*
619          * Filled at exit: exit_code, exit_code_hi, exit_info_1, exit_info_2,
620          * exit_int_info, exit_int_info_err, next_rip, insn_len, insn_bytes.
621          */
622
623         if (svm->vgif_enabled && (svm->nested.ctl.int_ctl & V_GIF_ENABLE_MASK))
624                 int_ctl_vmcb12_bits |= (V_GIF_MASK | V_GIF_ENABLE_MASK);
625         else
626                 int_ctl_vmcb01_bits |= (V_GIF_MASK | V_GIF_ENABLE_MASK);
627
628         /* Copied from vmcb01.  msrpm_base can be overwritten later.  */
629         vmcb02->control.nested_ctl = vmcb01->control.nested_ctl;
630         vmcb02->control.iopm_base_pa = vmcb01->control.iopm_base_pa;
631         vmcb02->control.msrpm_base_pa = vmcb01->control.msrpm_base_pa;
632
633         /* Done at vmrun: asid.  */
634
635         /* Also overwritten later if necessary.  */
636         vmcb02->control.tlb_ctl = TLB_CONTROL_DO_NOTHING;
637
638         /* nested_cr3.  */
639         if (nested_npt_enabled(svm))
640                 nested_svm_init_mmu_context(vcpu);
641
642         vcpu->arch.tsc_offset = kvm_calc_nested_tsc_offset(
643                         vcpu->arch.l1_tsc_offset,
644                         svm->nested.ctl.tsc_offset,
645                         svm->tsc_ratio_msr);
646
647         vmcb02->control.tsc_offset = vcpu->arch.tsc_offset;
648
649         if (svm->tsc_scaling_enabled &&
650             svm->tsc_ratio_msr != kvm_caps.default_tsc_scaling_ratio)
651                 nested_svm_update_tsc_ratio_msr(vcpu);
652
653         vmcb02->control.int_ctl             =
654                 (svm->nested.ctl.int_ctl & int_ctl_vmcb12_bits) |
655                 (vmcb01->control.int_ctl & int_ctl_vmcb01_bits);
656
657         vmcb02->control.int_vector          = svm->nested.ctl.int_vector;
658         vmcb02->control.int_state           = svm->nested.ctl.int_state;
659         vmcb02->control.event_inj           = svm->nested.ctl.event_inj;
660         vmcb02->control.event_inj_err       = svm->nested.ctl.event_inj_err;
661
662         /*
663          * next_rip is consumed on VMRUN as the return address pushed on the
664          * stack for injected soft exceptions/interrupts.  If nrips is exposed
665          * to L1, take it verbatim from vmcb12.  If nrips is supported in
666          * hardware but not exposed to L1, stuff the actual L2 RIP to emulate
667          * what a nrips=0 CPU would do (L1 is responsible for advancing RIP
668          * prior to injecting the event).
669          */
670         if (svm->nrips_enabled)
671                 vmcb02->control.next_rip    = svm->nested.ctl.next_rip;
672         else if (boot_cpu_has(X86_FEATURE_NRIPS))
673                 vmcb02->control.next_rip    = vmcb12_rip;
674
675         svm->nmi_l1_to_l2 = is_evtinj_nmi(vmcb02->control.event_inj);
676         if (is_evtinj_soft(vmcb02->control.event_inj)) {
677                 svm->soft_int_injected = true;
678                 svm->soft_int_csbase = vmcb12_csbase;
679                 svm->soft_int_old_rip = vmcb12_rip;
680                 if (svm->nrips_enabled)
681                         svm->soft_int_next_rip = svm->nested.ctl.next_rip;
682                 else
683                         svm->soft_int_next_rip = vmcb12_rip;
684         }
685
686         vmcb02->control.virt_ext            = vmcb01->control.virt_ext &
687                                               LBR_CTL_ENABLE_MASK;
688         if (svm->lbrv_enabled)
689                 vmcb02->control.virt_ext  |=
690                         (svm->nested.ctl.virt_ext & LBR_CTL_ENABLE_MASK);
691
692         if (!nested_vmcb_needs_vls_intercept(svm))
693                 vmcb02->control.virt_ext |= VIRTUAL_VMLOAD_VMSAVE_ENABLE_MASK;
694
695         pause_count12 = svm->pause_filter_enabled ? svm->nested.ctl.pause_filter_count : 0;
696         pause_thresh12 = svm->pause_threshold_enabled ? svm->nested.ctl.pause_filter_thresh : 0;
697         if (kvm_pause_in_guest(svm->vcpu.kvm)) {
698                 /* use guest values since host doesn't intercept PAUSE */
699                 vmcb02->control.pause_filter_count = pause_count12;
700                 vmcb02->control.pause_filter_thresh = pause_thresh12;
701
702         } else {
703                 /* start from host values otherwise */
704                 vmcb02->control.pause_filter_count = vmcb01->control.pause_filter_count;
705                 vmcb02->control.pause_filter_thresh = vmcb01->control.pause_filter_thresh;
706
707                 /* ... but ensure filtering is disabled if so requested.  */
708                 if (vmcb12_is_intercept(&svm->nested.ctl, INTERCEPT_PAUSE)) {
709                         if (!pause_count12)
710                                 vmcb02->control.pause_filter_count = 0;
711                         if (!pause_thresh12)
712                                 vmcb02->control.pause_filter_thresh = 0;
713                 }
714         }
715
716         nested_svm_transition_tlb_flush(vcpu);
717
718         /* Enter Guest-Mode */
719         enter_guest_mode(vcpu);
720
721         /*
722          * Merge guest and host intercepts - must be called with vcpu in
723          * guest-mode to take effect.
724          */
725         recalc_intercepts(svm);
726 }
727
728 static void nested_svm_copy_common_state(struct vmcb *from_vmcb, struct vmcb *to_vmcb)
729 {
730         /*
731          * Some VMCB state is shared between L1 and L2 and thus has to be
732          * moved at the time of nested vmrun and vmexit.
733          *
734          * VMLOAD/VMSAVE state would also belong in this category, but KVM
735          * always performs VMLOAD and VMSAVE from the VMCB01.
736          */
737         to_vmcb->save.spec_ctrl = from_vmcb->save.spec_ctrl;
738 }
739
740 int enter_svm_guest_mode(struct kvm_vcpu *vcpu, u64 vmcb12_gpa,
741                          struct vmcb *vmcb12, bool from_vmrun)
742 {
743         struct vcpu_svm *svm = to_svm(vcpu);
744         int ret;
745
746         trace_kvm_nested_vmenter(svm->vmcb->save.rip,
747                                  vmcb12_gpa,
748                                  vmcb12->save.rip,
749                                  vmcb12->control.int_ctl,
750                                  vmcb12->control.event_inj,
751                                  vmcb12->control.nested_ctl,
752                                  vmcb12->control.nested_cr3,
753                                  vmcb12->save.cr3,
754                                  KVM_ISA_SVM);
755
756         trace_kvm_nested_intercepts(vmcb12->control.intercepts[INTERCEPT_CR] & 0xffff,
757                                     vmcb12->control.intercepts[INTERCEPT_CR] >> 16,
758                                     vmcb12->control.intercepts[INTERCEPT_EXCEPTION],
759                                     vmcb12->control.intercepts[INTERCEPT_WORD3],
760                                     vmcb12->control.intercepts[INTERCEPT_WORD4],
761                                     vmcb12->control.intercepts[INTERCEPT_WORD5]);
762
763
764         svm->nested.vmcb12_gpa = vmcb12_gpa;
765
766         WARN_ON(svm->vmcb == svm->nested.vmcb02.ptr);
767
768         nested_svm_copy_common_state(svm->vmcb01.ptr, svm->nested.vmcb02.ptr);
769
770         svm_switch_vmcb(svm, &svm->nested.vmcb02);
771         nested_vmcb02_prepare_control(svm, vmcb12->save.rip, vmcb12->save.cs.base);
772         nested_vmcb02_prepare_save(svm, vmcb12);
773
774         ret = nested_svm_load_cr3(&svm->vcpu, svm->nested.save.cr3,
775                                   nested_npt_enabled(svm), from_vmrun);
776         if (ret)
777                 return ret;
778
779         if (!from_vmrun)
780                 kvm_make_request(KVM_REQ_GET_NESTED_STATE_PAGES, vcpu);
781
782         svm_set_gif(svm, true);
783
784         if (kvm_vcpu_apicv_active(vcpu))
785                 kvm_make_request(KVM_REQ_APICV_UPDATE, vcpu);
786
787         return 0;
788 }
789
790 int nested_svm_vmrun(struct kvm_vcpu *vcpu)
791 {
792         struct vcpu_svm *svm = to_svm(vcpu);
793         int ret;
794         struct vmcb *vmcb12;
795         struct kvm_host_map map;
796         u64 vmcb12_gpa;
797         struct vmcb *vmcb01 = svm->vmcb01.ptr;
798
799         if (!svm->nested.hsave_msr) {
800                 kvm_inject_gp(vcpu, 0);
801                 return 1;
802         }
803
804         if (is_smm(vcpu)) {
805                 kvm_queue_exception(vcpu, UD_VECTOR);
806                 return 1;
807         }
808
809         vmcb12_gpa = svm->vmcb->save.rax;
810         ret = kvm_vcpu_map(vcpu, gpa_to_gfn(vmcb12_gpa), &map);
811         if (ret == -EINVAL) {
812                 kvm_inject_gp(vcpu, 0);
813                 return 1;
814         } else if (ret) {
815                 return kvm_skip_emulated_instruction(vcpu);
816         }
817
818         ret = kvm_skip_emulated_instruction(vcpu);
819
820         vmcb12 = map.hva;
821
822         if (WARN_ON_ONCE(!svm->nested.initialized))
823                 return -EINVAL;
824
825         nested_copy_vmcb_control_to_cache(svm, &vmcb12->control);
826         nested_copy_vmcb_save_to_cache(svm, &vmcb12->save);
827
828         if (!nested_vmcb_check_save(vcpu) ||
829             !nested_vmcb_check_controls(vcpu)) {
830                 vmcb12->control.exit_code    = SVM_EXIT_ERR;
831                 vmcb12->control.exit_code_hi = 0;
832                 vmcb12->control.exit_info_1  = 0;
833                 vmcb12->control.exit_info_2  = 0;
834                 goto out;
835         }
836
837         /*
838          * Since vmcb01 is not in use, we can use it to store some of the L1
839          * state.
840          */
841         vmcb01->save.efer   = vcpu->arch.efer;
842         vmcb01->save.cr0    = kvm_read_cr0(vcpu);
843         vmcb01->save.cr4    = vcpu->arch.cr4;
844         vmcb01->save.rflags = kvm_get_rflags(vcpu);
845         vmcb01->save.rip    = kvm_rip_read(vcpu);
846
847         if (!npt_enabled)
848                 vmcb01->save.cr3 = kvm_read_cr3(vcpu);
849
850         svm->nested.nested_run_pending = 1;
851
852         if (enter_svm_guest_mode(vcpu, vmcb12_gpa, vmcb12, true))
853                 goto out_exit_err;
854
855         if (nested_svm_vmrun_msrpm(svm))
856                 goto out;
857
858 out_exit_err:
859         svm->nested.nested_run_pending = 0;
860         svm->nmi_l1_to_l2 = false;
861         svm->soft_int_injected = false;
862
863         svm->vmcb->control.exit_code    = SVM_EXIT_ERR;
864         svm->vmcb->control.exit_code_hi = 0;
865         svm->vmcb->control.exit_info_1  = 0;
866         svm->vmcb->control.exit_info_2  = 0;
867
868         nested_svm_vmexit(svm);
869
870 out:
871         kvm_vcpu_unmap(vcpu, &map, true);
872
873         return ret;
874 }
875
876 /* Copy state save area fields which are handled by VMRUN */
877 void svm_copy_vmrun_state(struct vmcb_save_area *to_save,
878                           struct vmcb_save_area *from_save)
879 {
880         to_save->es = from_save->es;
881         to_save->cs = from_save->cs;
882         to_save->ss = from_save->ss;
883         to_save->ds = from_save->ds;
884         to_save->gdtr = from_save->gdtr;
885         to_save->idtr = from_save->idtr;
886         to_save->rflags = from_save->rflags | X86_EFLAGS_FIXED;
887         to_save->efer = from_save->efer;
888         to_save->cr0 = from_save->cr0;
889         to_save->cr3 = from_save->cr3;
890         to_save->cr4 = from_save->cr4;
891         to_save->rax = from_save->rax;
892         to_save->rsp = from_save->rsp;
893         to_save->rip = from_save->rip;
894         to_save->cpl = 0;
895 }
896
897 void svm_copy_vmloadsave_state(struct vmcb *to_vmcb, struct vmcb *from_vmcb)
898 {
899         to_vmcb->save.fs = from_vmcb->save.fs;
900         to_vmcb->save.gs = from_vmcb->save.gs;
901         to_vmcb->save.tr = from_vmcb->save.tr;
902         to_vmcb->save.ldtr = from_vmcb->save.ldtr;
903         to_vmcb->save.kernel_gs_base = from_vmcb->save.kernel_gs_base;
904         to_vmcb->save.star = from_vmcb->save.star;
905         to_vmcb->save.lstar = from_vmcb->save.lstar;
906         to_vmcb->save.cstar = from_vmcb->save.cstar;
907         to_vmcb->save.sfmask = from_vmcb->save.sfmask;
908         to_vmcb->save.sysenter_cs = from_vmcb->save.sysenter_cs;
909         to_vmcb->save.sysenter_esp = from_vmcb->save.sysenter_esp;
910         to_vmcb->save.sysenter_eip = from_vmcb->save.sysenter_eip;
911 }
912
913 int nested_svm_vmexit(struct vcpu_svm *svm)
914 {
915         struct kvm_vcpu *vcpu = &svm->vcpu;
916         struct vmcb *vmcb01 = svm->vmcb01.ptr;
917         struct vmcb *vmcb02 = svm->nested.vmcb02.ptr;
918         struct vmcb *vmcb12;
919         struct kvm_host_map map;
920         int rc;
921
922         rc = kvm_vcpu_map(vcpu, gpa_to_gfn(svm->nested.vmcb12_gpa), &map);
923         if (rc) {
924                 if (rc == -EINVAL)
925                         kvm_inject_gp(vcpu, 0);
926                 return 1;
927         }
928
929         vmcb12 = map.hva;
930
931         /* Exit Guest-Mode */
932         leave_guest_mode(vcpu);
933         svm->nested.vmcb12_gpa = 0;
934         WARN_ON_ONCE(svm->nested.nested_run_pending);
935
936         kvm_clear_request(KVM_REQ_GET_NESTED_STATE_PAGES, vcpu);
937
938         /* in case we halted in L2 */
939         svm->vcpu.arch.mp_state = KVM_MP_STATE_RUNNABLE;
940
941         /* Give the current vmcb to the guest */
942
943         vmcb12->save.es     = vmcb02->save.es;
944         vmcb12->save.cs     = vmcb02->save.cs;
945         vmcb12->save.ss     = vmcb02->save.ss;
946         vmcb12->save.ds     = vmcb02->save.ds;
947         vmcb12->save.gdtr   = vmcb02->save.gdtr;
948         vmcb12->save.idtr   = vmcb02->save.idtr;
949         vmcb12->save.efer   = svm->vcpu.arch.efer;
950         vmcb12->save.cr0    = kvm_read_cr0(vcpu);
951         vmcb12->save.cr3    = kvm_read_cr3(vcpu);
952         vmcb12->save.cr2    = vmcb02->save.cr2;
953         vmcb12->save.cr4    = svm->vcpu.arch.cr4;
954         vmcb12->save.rflags = kvm_get_rflags(vcpu);
955         vmcb12->save.rip    = kvm_rip_read(vcpu);
956         vmcb12->save.rsp    = kvm_rsp_read(vcpu);
957         vmcb12->save.rax    = kvm_rax_read(vcpu);
958         vmcb12->save.dr7    = vmcb02->save.dr7;
959         vmcb12->save.dr6    = svm->vcpu.arch.dr6;
960         vmcb12->save.cpl    = vmcb02->save.cpl;
961
962         vmcb12->control.int_state         = vmcb02->control.int_state;
963         vmcb12->control.exit_code         = vmcb02->control.exit_code;
964         vmcb12->control.exit_code_hi      = vmcb02->control.exit_code_hi;
965         vmcb12->control.exit_info_1       = vmcb02->control.exit_info_1;
966         vmcb12->control.exit_info_2       = vmcb02->control.exit_info_2;
967
968         if (vmcb12->control.exit_code != SVM_EXIT_ERR)
969                 nested_save_pending_event_to_vmcb12(svm, vmcb12);
970
971         if (svm->nrips_enabled)
972                 vmcb12->control.next_rip  = vmcb02->control.next_rip;
973
974         vmcb12->control.int_ctl           = svm->nested.ctl.int_ctl;
975         vmcb12->control.tlb_ctl           = svm->nested.ctl.tlb_ctl;
976         vmcb12->control.event_inj         = svm->nested.ctl.event_inj;
977         vmcb12->control.event_inj_err     = svm->nested.ctl.event_inj_err;
978
979         if (!kvm_pause_in_guest(vcpu->kvm)) {
980                 vmcb01->control.pause_filter_count = vmcb02->control.pause_filter_count;
981                 vmcb_mark_dirty(vmcb01, VMCB_INTERCEPTS);
982
983         }
984
985         nested_svm_copy_common_state(svm->nested.vmcb02.ptr, svm->vmcb01.ptr);
986
987         svm_switch_vmcb(svm, &svm->vmcb01);
988
989         if (unlikely(svm->lbrv_enabled && (svm->nested.ctl.virt_ext & LBR_CTL_ENABLE_MASK))) {
990                 svm_copy_lbrs(vmcb12, vmcb02);
991                 svm_update_lbrv(vcpu);
992         } else if (unlikely(vmcb01->control.virt_ext & LBR_CTL_ENABLE_MASK)) {
993                 svm_copy_lbrs(vmcb01, vmcb02);
994                 svm_update_lbrv(vcpu);
995         }
996
997         /*
998          * On vmexit the  GIF is set to false and
999          * no event can be injected in L1.
1000          */
1001         svm_set_gif(svm, false);
1002         vmcb01->control.exit_int_info = 0;
1003
1004         svm->vcpu.arch.tsc_offset = svm->vcpu.arch.l1_tsc_offset;
1005         if (vmcb01->control.tsc_offset != svm->vcpu.arch.tsc_offset) {
1006                 vmcb01->control.tsc_offset = svm->vcpu.arch.tsc_offset;
1007                 vmcb_mark_dirty(vmcb01, VMCB_INTERCEPTS);
1008         }
1009
1010         if (kvm_caps.has_tsc_control &&
1011             vcpu->arch.tsc_scaling_ratio != vcpu->arch.l1_tsc_scaling_ratio) {
1012                 vcpu->arch.tsc_scaling_ratio = vcpu->arch.l1_tsc_scaling_ratio;
1013                 __svm_write_tsc_multiplier(vcpu->arch.tsc_scaling_ratio);
1014         }
1015
1016         svm->nested.ctl.nested_cr3 = 0;
1017
1018         /*
1019          * Restore processor state that had been saved in vmcb01
1020          */
1021         kvm_set_rflags(vcpu, vmcb01->save.rflags);
1022         svm_set_efer(vcpu, vmcb01->save.efer);
1023         svm_set_cr0(vcpu, vmcb01->save.cr0 | X86_CR0_PE);
1024         svm_set_cr4(vcpu, vmcb01->save.cr4);
1025         kvm_rax_write(vcpu, vmcb01->save.rax);
1026         kvm_rsp_write(vcpu, vmcb01->save.rsp);
1027         kvm_rip_write(vcpu, vmcb01->save.rip);
1028
1029         svm->vcpu.arch.dr7 = DR7_FIXED_1;
1030         kvm_update_dr7(&svm->vcpu);
1031
1032         trace_kvm_nested_vmexit_inject(vmcb12->control.exit_code,
1033                                        vmcb12->control.exit_info_1,
1034                                        vmcb12->control.exit_info_2,
1035                                        vmcb12->control.exit_int_info,
1036                                        vmcb12->control.exit_int_info_err,
1037                                        KVM_ISA_SVM);
1038
1039         kvm_vcpu_unmap(vcpu, &map, true);
1040
1041         nested_svm_transition_tlb_flush(vcpu);
1042
1043         nested_svm_uninit_mmu_context(vcpu);
1044
1045         rc = nested_svm_load_cr3(vcpu, vmcb01->save.cr3, false, true);
1046         if (rc)
1047                 return 1;
1048
1049         /*
1050          * Drop what we picked up for L2 via svm_complete_interrupts() so it
1051          * doesn't end up in L1.
1052          */
1053         svm->vcpu.arch.nmi_injected = false;
1054         kvm_clear_exception_queue(vcpu);
1055         kvm_clear_interrupt_queue(vcpu);
1056
1057         /*
1058          * If we are here following the completion of a VMRUN that
1059          * is being single-stepped, queue the pending #DB intercept
1060          * right now so that it an be accounted for before we execute
1061          * L1's next instruction.
1062          */
1063         if (unlikely(vmcb01->save.rflags & X86_EFLAGS_TF))
1064                 kvm_queue_exception(&(svm->vcpu), DB_VECTOR);
1065
1066         /*
1067          * Un-inhibit the AVIC right away, so that other vCPUs can start
1068          * to benefit from it right away.
1069          */
1070         if (kvm_apicv_activated(vcpu->kvm))
1071                 kvm_vcpu_update_apicv(vcpu);
1072
1073         return 0;
1074 }
1075
1076 static void nested_svm_triple_fault(struct kvm_vcpu *vcpu)
1077 {
1078         struct vcpu_svm *svm = to_svm(vcpu);
1079
1080         if (!vmcb12_is_intercept(&svm->nested.ctl, INTERCEPT_SHUTDOWN))
1081                 return;
1082
1083         kvm_clear_request(KVM_REQ_TRIPLE_FAULT, vcpu);
1084         nested_svm_simple_vmexit(to_svm(vcpu), SVM_EXIT_SHUTDOWN);
1085 }
1086
1087 int svm_allocate_nested(struct vcpu_svm *svm)
1088 {
1089         struct page *vmcb02_page;
1090
1091         if (svm->nested.initialized)
1092                 return 0;
1093
1094         vmcb02_page = alloc_page(GFP_KERNEL_ACCOUNT | __GFP_ZERO);
1095         if (!vmcb02_page)
1096                 return -ENOMEM;
1097         svm->nested.vmcb02.ptr = page_address(vmcb02_page);
1098         svm->nested.vmcb02.pa = __sme_set(page_to_pfn(vmcb02_page) << PAGE_SHIFT);
1099
1100         svm->nested.msrpm = svm_vcpu_alloc_msrpm();
1101         if (!svm->nested.msrpm)
1102                 goto err_free_vmcb02;
1103         svm_vcpu_init_msrpm(&svm->vcpu, svm->nested.msrpm);
1104
1105         svm->nested.initialized = true;
1106         return 0;
1107
1108 err_free_vmcb02:
1109         __free_page(vmcb02_page);
1110         return -ENOMEM;
1111 }
1112
1113 void svm_free_nested(struct vcpu_svm *svm)
1114 {
1115         if (!svm->nested.initialized)
1116                 return;
1117
1118         if (WARN_ON_ONCE(svm->vmcb != svm->vmcb01.ptr))
1119                 svm_switch_vmcb(svm, &svm->vmcb01);
1120
1121         svm_vcpu_free_msrpm(svm->nested.msrpm);
1122         svm->nested.msrpm = NULL;
1123
1124         __free_page(virt_to_page(svm->nested.vmcb02.ptr));
1125         svm->nested.vmcb02.ptr = NULL;
1126
1127         /*
1128          * When last_vmcb12_gpa matches the current vmcb12 gpa,
1129          * some vmcb12 fields are not loaded if they are marked clean
1130          * in the vmcb12, since in this case they are up to date already.
1131          *
1132          * When the vmcb02 is freed, this optimization becomes invalid.
1133          */
1134         svm->nested.last_vmcb12_gpa = INVALID_GPA;
1135
1136         svm->nested.initialized = false;
1137 }
1138
1139 void svm_leave_nested(struct kvm_vcpu *vcpu)
1140 {
1141         struct vcpu_svm *svm = to_svm(vcpu);
1142
1143         if (is_guest_mode(vcpu)) {
1144                 svm->nested.nested_run_pending = 0;
1145                 svm->nested.vmcb12_gpa = INVALID_GPA;
1146
1147                 leave_guest_mode(vcpu);
1148
1149                 svm_switch_vmcb(svm, &svm->vmcb01);
1150
1151                 nested_svm_uninit_mmu_context(vcpu);
1152                 vmcb_mark_all_dirty(svm->vmcb);
1153
1154                 if (kvm_apicv_activated(vcpu->kvm))
1155                         kvm_make_request(KVM_REQ_APICV_UPDATE, vcpu);
1156         }
1157
1158         kvm_clear_request(KVM_REQ_GET_NESTED_STATE_PAGES, vcpu);
1159 }
1160
1161 static int nested_svm_exit_handled_msr(struct vcpu_svm *svm)
1162 {
1163         u32 offset, msr, value;
1164         int write, mask;
1165
1166         if (!(vmcb12_is_intercept(&svm->nested.ctl, INTERCEPT_MSR_PROT)))
1167                 return NESTED_EXIT_HOST;
1168
1169         msr    = svm->vcpu.arch.regs[VCPU_REGS_RCX];
1170         offset = svm_msrpm_offset(msr);
1171         write  = svm->vmcb->control.exit_info_1 & 1;
1172         mask   = 1 << ((2 * (msr & 0xf)) + write);
1173
1174         if (offset == MSR_INVALID)
1175                 return NESTED_EXIT_DONE;
1176
1177         /* Offset is in 32 bit units but need in 8 bit units */
1178         offset *= 4;
1179
1180         if (kvm_vcpu_read_guest(&svm->vcpu, svm->nested.ctl.msrpm_base_pa + offset, &value, 4))
1181                 return NESTED_EXIT_DONE;
1182
1183         return (value & mask) ? NESTED_EXIT_DONE : NESTED_EXIT_HOST;
1184 }
1185
1186 static int nested_svm_intercept_ioio(struct vcpu_svm *svm)
1187 {
1188         unsigned port, size, iopm_len;
1189         u16 val, mask;
1190         u8 start_bit;
1191         u64 gpa;
1192
1193         if (!(vmcb12_is_intercept(&svm->nested.ctl, INTERCEPT_IOIO_PROT)))
1194                 return NESTED_EXIT_HOST;
1195
1196         port = svm->vmcb->control.exit_info_1 >> 16;
1197         size = (svm->vmcb->control.exit_info_1 & SVM_IOIO_SIZE_MASK) >>
1198                 SVM_IOIO_SIZE_SHIFT;
1199         gpa  = svm->nested.ctl.iopm_base_pa + (port / 8);
1200         start_bit = port % 8;
1201         iopm_len = (start_bit + size > 8) ? 2 : 1;
1202         mask = (0xf >> (4 - size)) << start_bit;
1203         val = 0;
1204
1205         if (kvm_vcpu_read_guest(&svm->vcpu, gpa, &val, iopm_len))
1206                 return NESTED_EXIT_DONE;
1207
1208         return (val & mask) ? NESTED_EXIT_DONE : NESTED_EXIT_HOST;
1209 }
1210
1211 static int nested_svm_intercept(struct vcpu_svm *svm)
1212 {
1213         u32 exit_code = svm->vmcb->control.exit_code;
1214         int vmexit = NESTED_EXIT_HOST;
1215
1216         switch (exit_code) {
1217         case SVM_EXIT_MSR:
1218                 vmexit = nested_svm_exit_handled_msr(svm);
1219                 break;
1220         case SVM_EXIT_IOIO:
1221                 vmexit = nested_svm_intercept_ioio(svm);
1222                 break;
1223         case SVM_EXIT_READ_CR0 ... SVM_EXIT_WRITE_CR8: {
1224                 if (vmcb12_is_intercept(&svm->nested.ctl, exit_code))
1225                         vmexit = NESTED_EXIT_DONE;
1226                 break;
1227         }
1228         case SVM_EXIT_READ_DR0 ... SVM_EXIT_WRITE_DR7: {
1229                 if (vmcb12_is_intercept(&svm->nested.ctl, exit_code))
1230                         vmexit = NESTED_EXIT_DONE;
1231                 break;
1232         }
1233         case SVM_EXIT_EXCP_BASE ... SVM_EXIT_EXCP_BASE + 0x1f: {
1234                 /*
1235                  * Host-intercepted exceptions have been checked already in
1236                  * nested_svm_exit_special.  There is nothing to do here,
1237                  * the vmexit is injected by svm_check_nested_events.
1238                  */
1239                 vmexit = NESTED_EXIT_DONE;
1240                 break;
1241         }
1242         case SVM_EXIT_ERR: {
1243                 vmexit = NESTED_EXIT_DONE;
1244                 break;
1245         }
1246         default: {
1247                 if (vmcb12_is_intercept(&svm->nested.ctl, exit_code))
1248                         vmexit = NESTED_EXIT_DONE;
1249         }
1250         }
1251
1252         return vmexit;
1253 }
1254
1255 int nested_svm_exit_handled(struct vcpu_svm *svm)
1256 {
1257         int vmexit;
1258
1259         vmexit = nested_svm_intercept(svm);
1260
1261         if (vmexit == NESTED_EXIT_DONE)
1262                 nested_svm_vmexit(svm);
1263
1264         return vmexit;
1265 }
1266
1267 int nested_svm_check_permissions(struct kvm_vcpu *vcpu)
1268 {
1269         if (!(vcpu->arch.efer & EFER_SVME) || !is_paging(vcpu)) {
1270                 kvm_queue_exception(vcpu, UD_VECTOR);
1271                 return 1;
1272         }
1273
1274         if (to_svm(vcpu)->vmcb->save.cpl) {
1275                 kvm_inject_gp(vcpu, 0);
1276                 return 1;
1277         }
1278
1279         return 0;
1280 }
1281
1282 static bool nested_svm_is_exception_vmexit(struct kvm_vcpu *vcpu, u8 vector,
1283                                            u32 error_code)
1284 {
1285         struct vcpu_svm *svm = to_svm(vcpu);
1286
1287         return (svm->nested.ctl.intercepts[INTERCEPT_EXCEPTION] & BIT(vector));
1288 }
1289
1290 static void nested_svm_inject_exception_vmexit(struct kvm_vcpu *vcpu)
1291 {
1292         struct kvm_queued_exception *ex = &vcpu->arch.exception_vmexit;
1293         struct vcpu_svm *svm = to_svm(vcpu);
1294         struct vmcb *vmcb = svm->vmcb;
1295
1296         vmcb->control.exit_code = SVM_EXIT_EXCP_BASE + ex->vector;
1297         vmcb->control.exit_code_hi = 0;
1298
1299         if (ex->has_error_code)
1300                 vmcb->control.exit_info_1 = ex->error_code;
1301
1302         /*
1303          * EXITINFO2 is undefined for all exception intercepts other
1304          * than #PF.
1305          */
1306         if (ex->vector == PF_VECTOR) {
1307                 if (ex->has_payload)
1308                         vmcb->control.exit_info_2 = ex->payload;
1309                 else
1310                         vmcb->control.exit_info_2 = vcpu->arch.cr2;
1311         } else if (ex->vector == DB_VECTOR) {
1312                 /* See kvm_check_and_inject_events().  */
1313                 kvm_deliver_exception_payload(vcpu, ex);
1314
1315                 if (vcpu->arch.dr7 & DR7_GD) {
1316                         vcpu->arch.dr7 &= ~DR7_GD;
1317                         kvm_update_dr7(vcpu);
1318                 }
1319         } else {
1320                 WARN_ON(ex->has_payload);
1321         }
1322
1323         nested_svm_vmexit(svm);
1324 }
1325
1326 static inline bool nested_exit_on_init(struct vcpu_svm *svm)
1327 {
1328         return vmcb12_is_intercept(&svm->nested.ctl, INTERCEPT_INIT);
1329 }
1330
1331 static int svm_check_nested_events(struct kvm_vcpu *vcpu)
1332 {
1333         struct kvm_lapic *apic = vcpu->arch.apic;
1334         struct vcpu_svm *svm = to_svm(vcpu);
1335         /*
1336          * Only a pending nested run blocks a pending exception.  If there is a
1337          * previously injected event, the pending exception occurred while said
1338          * event was being delivered and thus needs to be handled.
1339          */
1340         bool block_nested_exceptions = svm->nested.nested_run_pending;
1341         /*
1342          * New events (not exceptions) are only recognized at instruction
1343          * boundaries.  If an event needs reinjection, then KVM is handling a
1344          * VM-Exit that occurred _during_ instruction execution; new events are
1345          * blocked until the instruction completes.
1346          */
1347         bool block_nested_events = block_nested_exceptions ||
1348                                    kvm_event_needs_reinjection(vcpu);
1349
1350         if (lapic_in_kernel(vcpu) &&
1351             test_bit(KVM_APIC_INIT, &apic->pending_events)) {
1352                 if (block_nested_events)
1353                         return -EBUSY;
1354                 if (!nested_exit_on_init(svm))
1355                         return 0;
1356                 nested_svm_simple_vmexit(svm, SVM_EXIT_INIT);
1357                 return 0;
1358         }
1359
1360         if (vcpu->arch.exception_vmexit.pending) {
1361                 if (block_nested_exceptions)
1362                         return -EBUSY;
1363                 nested_svm_inject_exception_vmexit(vcpu);
1364                 return 0;
1365         }
1366
1367         if (vcpu->arch.exception.pending) {
1368                 if (block_nested_exceptions)
1369                         return -EBUSY;
1370                 return 0;
1371         }
1372
1373         if (vcpu->arch.smi_pending && !svm_smi_blocked(vcpu)) {
1374                 if (block_nested_events)
1375                         return -EBUSY;
1376                 if (!nested_exit_on_smi(svm))
1377                         return 0;
1378                 nested_svm_simple_vmexit(svm, SVM_EXIT_SMI);
1379                 return 0;
1380         }
1381
1382         if (vcpu->arch.nmi_pending && !svm_nmi_blocked(vcpu)) {
1383                 if (block_nested_events)
1384                         return -EBUSY;
1385                 if (!nested_exit_on_nmi(svm))
1386                         return 0;
1387                 nested_svm_simple_vmexit(svm, SVM_EXIT_NMI);
1388                 return 0;
1389         }
1390
1391         if (kvm_cpu_has_interrupt(vcpu) && !svm_interrupt_blocked(vcpu)) {
1392                 if (block_nested_events)
1393                         return -EBUSY;
1394                 if (!nested_exit_on_intr(svm))
1395                         return 0;
1396                 trace_kvm_nested_intr_vmexit(svm->vmcb->save.rip);
1397                 nested_svm_simple_vmexit(svm, SVM_EXIT_INTR);
1398                 return 0;
1399         }
1400
1401         return 0;
1402 }
1403
1404 int nested_svm_exit_special(struct vcpu_svm *svm)
1405 {
1406         u32 exit_code = svm->vmcb->control.exit_code;
1407
1408         switch (exit_code) {
1409         case SVM_EXIT_INTR:
1410         case SVM_EXIT_NMI:
1411         case SVM_EXIT_NPF:
1412                 return NESTED_EXIT_HOST;
1413         case SVM_EXIT_EXCP_BASE ... SVM_EXIT_EXCP_BASE + 0x1f: {
1414                 u32 excp_bits = 1 << (exit_code - SVM_EXIT_EXCP_BASE);
1415
1416                 if (svm->vmcb01.ptr->control.intercepts[INTERCEPT_EXCEPTION] &
1417                     excp_bits)
1418                         return NESTED_EXIT_HOST;
1419                 else if (exit_code == SVM_EXIT_EXCP_BASE + PF_VECTOR &&
1420                          svm->vcpu.arch.apf.host_apf_flags)
1421                         /* Trap async PF even if not shadowing */
1422                         return NESTED_EXIT_HOST;
1423                 break;
1424         }
1425         default:
1426                 break;
1427         }
1428
1429         return NESTED_EXIT_CONTINUE;
1430 }
1431
1432 void nested_svm_update_tsc_ratio_msr(struct kvm_vcpu *vcpu)
1433 {
1434         struct vcpu_svm *svm = to_svm(vcpu);
1435
1436         vcpu->arch.tsc_scaling_ratio =
1437                 kvm_calc_nested_tsc_multiplier(vcpu->arch.l1_tsc_scaling_ratio,
1438                                                svm->tsc_ratio_msr);
1439         __svm_write_tsc_multiplier(vcpu->arch.tsc_scaling_ratio);
1440 }
1441
1442 /* Inverse operation of nested_copy_vmcb_control_to_cache(). asid is copied too. */
1443 static void nested_copy_vmcb_cache_to_control(struct vmcb_control_area *dst,
1444                                               struct vmcb_ctrl_area_cached *from)
1445 {
1446         unsigned int i;
1447
1448         memset(dst, 0, sizeof(struct vmcb_control_area));
1449
1450         for (i = 0; i < MAX_INTERCEPT; i++)
1451                 dst->intercepts[i] = from->intercepts[i];
1452
1453         dst->iopm_base_pa         = from->iopm_base_pa;
1454         dst->msrpm_base_pa        = from->msrpm_base_pa;
1455         dst->tsc_offset           = from->tsc_offset;
1456         dst->asid                 = from->asid;
1457         dst->tlb_ctl              = from->tlb_ctl;
1458         dst->int_ctl              = from->int_ctl;
1459         dst->int_vector           = from->int_vector;
1460         dst->int_state            = from->int_state;
1461         dst->exit_code            = from->exit_code;
1462         dst->exit_code_hi         = from->exit_code_hi;
1463         dst->exit_info_1          = from->exit_info_1;
1464         dst->exit_info_2          = from->exit_info_2;
1465         dst->exit_int_info        = from->exit_int_info;
1466         dst->exit_int_info_err    = from->exit_int_info_err;
1467         dst->nested_ctl           = from->nested_ctl;
1468         dst->event_inj            = from->event_inj;
1469         dst->event_inj_err        = from->event_inj_err;
1470         dst->next_rip             = from->next_rip;
1471         dst->nested_cr3           = from->nested_cr3;
1472         dst->virt_ext              = from->virt_ext;
1473         dst->pause_filter_count   = from->pause_filter_count;
1474         dst->pause_filter_thresh  = from->pause_filter_thresh;
1475         /* 'clean' and 'hv_enlightenments' are not changed by KVM */
1476 }
1477
1478 static int svm_get_nested_state(struct kvm_vcpu *vcpu,
1479                                 struct kvm_nested_state __user *user_kvm_nested_state,
1480                                 u32 user_data_size)
1481 {
1482         struct vcpu_svm *svm;
1483         struct vmcb_control_area *ctl;
1484         unsigned long r;
1485         struct kvm_nested_state kvm_state = {
1486                 .flags = 0,
1487                 .format = KVM_STATE_NESTED_FORMAT_SVM,
1488                 .size = sizeof(kvm_state),
1489         };
1490         struct vmcb __user *user_vmcb = (struct vmcb __user *)
1491                 &user_kvm_nested_state->data.svm[0];
1492
1493         if (!vcpu)
1494                 return kvm_state.size + KVM_STATE_NESTED_SVM_VMCB_SIZE;
1495
1496         svm = to_svm(vcpu);
1497
1498         if (user_data_size < kvm_state.size)
1499                 goto out;
1500
1501         /* First fill in the header and copy it out.  */
1502         if (is_guest_mode(vcpu)) {
1503                 kvm_state.hdr.svm.vmcb_pa = svm->nested.vmcb12_gpa;
1504                 kvm_state.size += KVM_STATE_NESTED_SVM_VMCB_SIZE;
1505                 kvm_state.flags |= KVM_STATE_NESTED_GUEST_MODE;
1506
1507                 if (svm->nested.nested_run_pending)
1508                         kvm_state.flags |= KVM_STATE_NESTED_RUN_PENDING;
1509         }
1510
1511         if (gif_set(svm))
1512                 kvm_state.flags |= KVM_STATE_NESTED_GIF_SET;
1513
1514         if (copy_to_user(user_kvm_nested_state, &kvm_state, sizeof(kvm_state)))
1515                 return -EFAULT;
1516
1517         if (!is_guest_mode(vcpu))
1518                 goto out;
1519
1520         /*
1521          * Copy over the full size of the VMCB rather than just the size
1522          * of the structs.
1523          */
1524         if (clear_user(user_vmcb, KVM_STATE_NESTED_SVM_VMCB_SIZE))
1525                 return -EFAULT;
1526
1527         ctl = kzalloc(sizeof(*ctl), GFP_KERNEL);
1528         if (!ctl)
1529                 return -ENOMEM;
1530
1531         nested_copy_vmcb_cache_to_control(ctl, &svm->nested.ctl);
1532         r = copy_to_user(&user_vmcb->control, ctl,
1533                          sizeof(user_vmcb->control));
1534         kfree(ctl);
1535         if (r)
1536                 return -EFAULT;
1537
1538         if (copy_to_user(&user_vmcb->save, &svm->vmcb01.ptr->save,
1539                          sizeof(user_vmcb->save)))
1540                 return -EFAULT;
1541 out:
1542         return kvm_state.size;
1543 }
1544
1545 static int svm_set_nested_state(struct kvm_vcpu *vcpu,
1546                                 struct kvm_nested_state __user *user_kvm_nested_state,
1547                                 struct kvm_nested_state *kvm_state)
1548 {
1549         struct vcpu_svm *svm = to_svm(vcpu);
1550         struct vmcb __user *user_vmcb = (struct vmcb __user *)
1551                 &user_kvm_nested_state->data.svm[0];
1552         struct vmcb_control_area *ctl;
1553         struct vmcb_save_area *save;
1554         struct vmcb_save_area_cached save_cached;
1555         struct vmcb_ctrl_area_cached ctl_cached;
1556         unsigned long cr0;
1557         int ret;
1558
1559         BUILD_BUG_ON(sizeof(struct vmcb_control_area) + sizeof(struct vmcb_save_area) >
1560                      KVM_STATE_NESTED_SVM_VMCB_SIZE);
1561
1562         if (kvm_state->format != KVM_STATE_NESTED_FORMAT_SVM)
1563                 return -EINVAL;
1564
1565         if (kvm_state->flags & ~(KVM_STATE_NESTED_GUEST_MODE |
1566                                  KVM_STATE_NESTED_RUN_PENDING |
1567                                  KVM_STATE_NESTED_GIF_SET))
1568                 return -EINVAL;
1569
1570         /*
1571          * If in guest mode, vcpu->arch.efer actually refers to the L2 guest's
1572          * EFER.SVME, but EFER.SVME still has to be 1 for VMRUN to succeed.
1573          */
1574         if (!(vcpu->arch.efer & EFER_SVME)) {
1575                 /* GIF=1 and no guest mode are required if SVME=0.  */
1576                 if (kvm_state->flags != KVM_STATE_NESTED_GIF_SET)
1577                         return -EINVAL;
1578         }
1579
1580         /* SMM temporarily disables SVM, so we cannot be in guest mode.  */
1581         if (is_smm(vcpu) && (kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE))
1582                 return -EINVAL;
1583
1584         if (!(kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE)) {
1585                 svm_leave_nested(vcpu);
1586                 svm_set_gif(svm, !!(kvm_state->flags & KVM_STATE_NESTED_GIF_SET));
1587                 return 0;
1588         }
1589
1590         if (!page_address_valid(vcpu, kvm_state->hdr.svm.vmcb_pa))
1591                 return -EINVAL;
1592         if (kvm_state->size < sizeof(*kvm_state) + KVM_STATE_NESTED_SVM_VMCB_SIZE)
1593                 return -EINVAL;
1594
1595         ret  = -ENOMEM;
1596         ctl  = kzalloc(sizeof(*ctl),  GFP_KERNEL_ACCOUNT);
1597         save = kzalloc(sizeof(*save), GFP_KERNEL_ACCOUNT);
1598         if (!ctl || !save)
1599                 goto out_free;
1600
1601         ret = -EFAULT;
1602         if (copy_from_user(ctl, &user_vmcb->control, sizeof(*ctl)))
1603                 goto out_free;
1604         if (copy_from_user(save, &user_vmcb->save, sizeof(*save)))
1605                 goto out_free;
1606
1607         ret = -EINVAL;
1608         __nested_copy_vmcb_control_to_cache(vcpu, &ctl_cached, ctl);
1609         if (!__nested_vmcb_check_controls(vcpu, &ctl_cached))
1610                 goto out_free;
1611
1612         /*
1613          * Processor state contains L2 state.  Check that it is
1614          * valid for guest mode (see nested_vmcb_check_save).
1615          */
1616         cr0 = kvm_read_cr0(vcpu);
1617         if (((cr0 & X86_CR0_CD) == 0) && (cr0 & X86_CR0_NW))
1618                 goto out_free;
1619
1620         /*
1621          * Validate host state saved from before VMRUN (see
1622          * nested_svm_check_permissions).
1623          */
1624         __nested_copy_vmcb_save_to_cache(&save_cached, save);
1625         if (!(save->cr0 & X86_CR0_PG) ||
1626             !(save->cr0 & X86_CR0_PE) ||
1627             (save->rflags & X86_EFLAGS_VM) ||
1628             !__nested_vmcb_check_save(vcpu, &save_cached))
1629                 goto out_free;
1630
1631
1632         /*
1633          * All checks done, we can enter guest mode. Userspace provides
1634          * vmcb12.control, which will be combined with L1 and stored into
1635          * vmcb02, and the L1 save state which we store in vmcb01.
1636          * L2 registers if needed are moved from the current VMCB to VMCB02.
1637          */
1638
1639         if (is_guest_mode(vcpu))
1640                 svm_leave_nested(vcpu);
1641         else
1642                 svm->nested.vmcb02.ptr->save = svm->vmcb01.ptr->save;
1643
1644         svm_set_gif(svm, !!(kvm_state->flags & KVM_STATE_NESTED_GIF_SET));
1645
1646         svm->nested.nested_run_pending =
1647                 !!(kvm_state->flags & KVM_STATE_NESTED_RUN_PENDING);
1648
1649         svm->nested.vmcb12_gpa = kvm_state->hdr.svm.vmcb_pa;
1650
1651         svm_copy_vmrun_state(&svm->vmcb01.ptr->save, save);
1652         nested_copy_vmcb_control_to_cache(svm, ctl);
1653
1654         svm_switch_vmcb(svm, &svm->nested.vmcb02);
1655         nested_vmcb02_prepare_control(svm, svm->vmcb->save.rip, svm->vmcb->save.cs.base);
1656
1657         /*
1658          * While the nested guest CR3 is already checked and set by
1659          * KVM_SET_SREGS, it was set when nested state was yet loaded,
1660          * thus MMU might not be initialized correctly.
1661          * Set it again to fix this.
1662          */
1663
1664         ret = nested_svm_load_cr3(&svm->vcpu, vcpu->arch.cr3,
1665                                   nested_npt_enabled(svm), false);
1666         if (WARN_ON_ONCE(ret))
1667                 goto out_free;
1668
1669         svm->nested.force_msr_bitmap_recalc = true;
1670
1671         kvm_make_request(KVM_REQ_GET_NESTED_STATE_PAGES, vcpu);
1672         ret = 0;
1673 out_free:
1674         kfree(save);
1675         kfree(ctl);
1676
1677         return ret;
1678 }
1679
1680 static bool svm_get_nested_state_pages(struct kvm_vcpu *vcpu)
1681 {
1682         struct vcpu_svm *svm = to_svm(vcpu);
1683
1684         if (WARN_ON(!is_guest_mode(vcpu)))
1685                 return true;
1686
1687         if (!vcpu->arch.pdptrs_from_userspace &&
1688             !nested_npt_enabled(svm) && is_pae_paging(vcpu))
1689                 /*
1690                  * Reload the guest's PDPTRs since after a migration
1691                  * the guest CR3 might be restored prior to setting the nested
1692                  * state which can lead to a load of wrong PDPTRs.
1693                  */
1694                 if (CC(!load_pdptrs(vcpu, vcpu->arch.cr3)))
1695                         return false;
1696
1697         if (!nested_svm_vmrun_msrpm(svm)) {
1698                 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
1699                 vcpu->run->internal.suberror =
1700                         KVM_INTERNAL_ERROR_EMULATION;
1701                 vcpu->run->internal.ndata = 0;
1702                 return false;
1703         }
1704
1705         return true;
1706 }
1707
1708 struct kvm_x86_nested_ops svm_nested_ops = {
1709         .leave_nested = svm_leave_nested,
1710         .is_exception_vmexit = nested_svm_is_exception_vmexit,
1711         .check_events = svm_check_nested_events,
1712         .triple_fault = nested_svm_triple_fault,
1713         .get_nested_state_pages = svm_get_nested_state_pages,
1714         .get_state = svm_get_nested_state,
1715         .set_state = svm_set_nested_state,
1716 };