KVM: nSVM: Check that VM_HSAVE_PA MSR was set before VMRUN
[linux-2.6-microblaze.git] / arch / 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
32 #define CC KVM_NESTED_VMENTER_CONSISTENCY_CHECK
33
34 static void nested_svm_inject_npf_exit(struct kvm_vcpu *vcpu,
35                                        struct x86_exception *fault)
36 {
37         struct vcpu_svm *svm = to_svm(vcpu);
38
39         if (svm->vmcb->control.exit_code != SVM_EXIT_NPF) {
40                 /*
41                  * TODO: track the cause of the nested page fault, and
42                  * correctly fill in the high bits of exit_info_1.
43                  */
44                 svm->vmcb->control.exit_code = SVM_EXIT_NPF;
45                 svm->vmcb->control.exit_code_hi = 0;
46                 svm->vmcb->control.exit_info_1 = (1ULL << 32);
47                 svm->vmcb->control.exit_info_2 = fault->address;
48         }
49
50         svm->vmcb->control.exit_info_1 &= ~0xffffffffULL;
51         svm->vmcb->control.exit_info_1 |= fault->error_code;
52
53         nested_svm_vmexit(svm);
54 }
55
56 static void svm_inject_page_fault_nested(struct kvm_vcpu *vcpu, struct x86_exception *fault)
57 {
58        struct vcpu_svm *svm = to_svm(vcpu);
59        WARN_ON(!is_guest_mode(vcpu));
60
61        if (vmcb_is_intercept(&svm->nested.ctl, INTERCEPT_EXCEPTION_OFFSET + PF_VECTOR) &&
62            !svm->nested.nested_run_pending) {
63                svm->vmcb->control.exit_code = SVM_EXIT_EXCP_BASE + PF_VECTOR;
64                svm->vmcb->control.exit_code_hi = 0;
65                svm->vmcb->control.exit_info_1 = fault->error_code;
66                svm->vmcb->control.exit_info_2 = fault->address;
67                nested_svm_vmexit(svm);
68        } else {
69                kvm_inject_page_fault(vcpu, fault);
70        }
71 }
72
73 static u64 nested_svm_get_tdp_pdptr(struct kvm_vcpu *vcpu, int index)
74 {
75         struct vcpu_svm *svm = to_svm(vcpu);
76         u64 cr3 = svm->nested.ctl.nested_cr3;
77         u64 pdpte;
78         int ret;
79
80         ret = kvm_vcpu_read_guest_page(vcpu, gpa_to_gfn(cr3), &pdpte,
81                                        offset_in_page(cr3) + index * 8, 8);
82         if (ret)
83                 return 0;
84         return pdpte;
85 }
86
87 static unsigned long nested_svm_get_tdp_cr3(struct kvm_vcpu *vcpu)
88 {
89         struct vcpu_svm *svm = to_svm(vcpu);
90
91         return svm->nested.ctl.nested_cr3;
92 }
93
94 static void nested_svm_init_mmu_context(struct kvm_vcpu *vcpu)
95 {
96         struct vcpu_svm *svm = to_svm(vcpu);
97
98         WARN_ON(mmu_is_nested(vcpu));
99
100         vcpu->arch.mmu = &vcpu->arch.guest_mmu;
101
102         /*
103          * The NPT format depends on L1's CR4 and EFER, which is in vmcb01.  Note,
104          * when called via KVM_SET_NESTED_STATE, that state may _not_ match current
105          * vCPU state.  CR0.WP is explicitly ignored, while CR0.PG is required.
106          */
107         kvm_init_shadow_npt_mmu(vcpu, X86_CR0_PG, svm->vmcb01.ptr->save.cr4,
108                                 svm->vmcb01.ptr->save.efer,
109                                 svm->nested.ctl.nested_cr3);
110         vcpu->arch.mmu->get_guest_pgd     = nested_svm_get_tdp_cr3;
111         vcpu->arch.mmu->get_pdptr         = nested_svm_get_tdp_pdptr;
112         vcpu->arch.mmu->inject_page_fault = nested_svm_inject_npf_exit;
113         vcpu->arch.walk_mmu              = &vcpu->arch.nested_mmu;
114 }
115
116 static void nested_svm_uninit_mmu_context(struct kvm_vcpu *vcpu)
117 {
118         vcpu->arch.mmu = &vcpu->arch.root_mmu;
119         vcpu->arch.walk_mmu = &vcpu->arch.root_mmu;
120 }
121
122 void recalc_intercepts(struct vcpu_svm *svm)
123 {
124         struct vmcb_control_area *c, *h, *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
163 static void copy_vmcb_control_area(struct vmcb_control_area *dst,
164                                    struct vmcb_control_area *from)
165 {
166         unsigned int i;
167
168         for (i = 0; i < MAX_INTERCEPT; i++)
169                 dst->intercepts[i] = from->intercepts[i];
170
171         dst->iopm_base_pa         = from->iopm_base_pa;
172         dst->msrpm_base_pa        = from->msrpm_base_pa;
173         dst->tsc_offset           = from->tsc_offset;
174         /* asid not copied, it is handled manually for svm->vmcb.  */
175         dst->tlb_ctl              = from->tlb_ctl;
176         dst->int_ctl              = from->int_ctl;
177         dst->int_vector           = from->int_vector;
178         dst->int_state            = from->int_state;
179         dst->exit_code            = from->exit_code;
180         dst->exit_code_hi         = from->exit_code_hi;
181         dst->exit_info_1          = from->exit_info_1;
182         dst->exit_info_2          = from->exit_info_2;
183         dst->exit_int_info        = from->exit_int_info;
184         dst->exit_int_info_err    = from->exit_int_info_err;
185         dst->nested_ctl           = from->nested_ctl;
186         dst->event_inj            = from->event_inj;
187         dst->event_inj_err        = from->event_inj_err;
188         dst->nested_cr3           = from->nested_cr3;
189         dst->virt_ext              = from->virt_ext;
190         dst->pause_filter_count   = from->pause_filter_count;
191         dst->pause_filter_thresh  = from->pause_filter_thresh;
192 }
193
194 static bool nested_svm_vmrun_msrpm(struct vcpu_svm *svm)
195 {
196         /*
197          * This function merges the msr permission bitmaps of kvm and the
198          * nested vmcb. It is optimized in that it only merges the parts where
199          * the kvm msr permission bitmap may contain zero bits
200          */
201         int i;
202
203         if (!(vmcb_is_intercept(&svm->nested.ctl, INTERCEPT_MSR_PROT)))
204                 return true;
205
206         for (i = 0; i < MSRPM_OFFSETS; i++) {
207                 u32 value, p;
208                 u64 offset;
209
210                 if (msrpm_offsets[i] == 0xffffffff)
211                         break;
212
213                 p      = msrpm_offsets[i];
214                 offset = svm->nested.ctl.msrpm_base_pa + (p * 4);
215
216                 if (kvm_vcpu_read_guest(&svm->vcpu, offset, &value, 4))
217                         return false;
218
219                 svm->nested.msrpm[p] = svm->msrpm[p] | value;
220         }
221
222         svm->vmcb->control.msrpm_base_pa = __sme_set(__pa(svm->nested.msrpm));
223
224         return true;
225 }
226
227 /*
228  * Bits 11:0 of bitmap address are ignored by hardware
229  */
230 static bool nested_svm_check_bitmap_pa(struct kvm_vcpu *vcpu, u64 pa, u32 size)
231 {
232         u64 addr = PAGE_ALIGN(pa);
233
234         return kvm_vcpu_is_legal_gpa(vcpu, addr) &&
235             kvm_vcpu_is_legal_gpa(vcpu, addr + size - 1);
236 }
237
238 static bool nested_vmcb_check_controls(struct kvm_vcpu *vcpu,
239                                        struct vmcb_control_area *control)
240 {
241         if (CC(!vmcb_is_intercept(control, INTERCEPT_VMRUN)))
242                 return false;
243
244         if (CC(control->asid == 0))
245                 return false;
246
247         if (CC((control->nested_ctl & SVM_NESTED_CTL_NP_ENABLE) && !npt_enabled))
248                 return false;
249
250         if (CC(!nested_svm_check_bitmap_pa(vcpu, control->msrpm_base_pa,
251                                            MSRPM_SIZE)))
252                 return false;
253         if (CC(!nested_svm_check_bitmap_pa(vcpu, control->iopm_base_pa,
254                                            IOPM_SIZE)))
255                 return false;
256
257         return true;
258 }
259
260 static bool nested_vmcb_check_cr3_cr4(struct kvm_vcpu *vcpu,
261                                       struct vmcb_save_area *save)
262 {
263         /*
264          * These checks are also performed by KVM_SET_SREGS,
265          * except that EFER.LMA is not checked by SVM against
266          * CR0.PG && EFER.LME.
267          */
268         if ((save->efer & EFER_LME) && (save->cr0 & X86_CR0_PG)) {
269                 if (CC(!(save->cr4 & X86_CR4_PAE)) ||
270                     CC(!(save->cr0 & X86_CR0_PE)) ||
271                     CC(kvm_vcpu_is_illegal_gpa(vcpu, save->cr3)))
272                         return false;
273         }
274
275         if (CC(!kvm_is_valid_cr4(vcpu, save->cr4)))
276                 return false;
277
278         return true;
279 }
280
281 /* Common checks that apply to both L1 and L2 state.  */
282 static bool nested_vmcb_valid_sregs(struct kvm_vcpu *vcpu,
283                                     struct vmcb_save_area *save)
284 {
285         /*
286          * FIXME: these should be done after copying the fields,
287          * to avoid TOC/TOU races.  For these save area checks
288          * the possible damage is limited since kvm_set_cr0 and
289          * kvm_set_cr4 handle failure; EFER_SVME is an exception
290          * so it is force-set later in nested_prepare_vmcb_save.
291          */
292         if (CC(!(save->efer & EFER_SVME)))
293                 return false;
294
295         if (CC((save->cr0 & X86_CR0_CD) == 0 && (save->cr0 & X86_CR0_NW)) ||
296             CC(save->cr0 & ~0xffffffffULL))
297                 return false;
298
299         if (CC(!kvm_dr6_valid(save->dr6)) || CC(!kvm_dr7_valid(save->dr7)))
300                 return false;
301
302         if (!nested_vmcb_check_cr3_cr4(vcpu, save))
303                 return false;
304
305         if (CC(!kvm_valid_efer(vcpu, save->efer)))
306                 return false;
307
308         return true;
309 }
310
311 static void nested_load_control_from_vmcb12(struct vcpu_svm *svm,
312                                             struct vmcb_control_area *control)
313 {
314         copy_vmcb_control_area(&svm->nested.ctl, control);
315
316         /* Copy it here because nested_svm_check_controls will check it.  */
317         svm->nested.ctl.asid           = control->asid;
318         svm->nested.ctl.msrpm_base_pa &= ~0x0fffULL;
319         svm->nested.ctl.iopm_base_pa  &= ~0x0fffULL;
320 }
321
322 /*
323  * Synchronize fields that are written by the processor, so that
324  * they can be copied back into the vmcb12.
325  */
326 void nested_sync_control_from_vmcb02(struct vcpu_svm *svm)
327 {
328         u32 mask;
329         svm->nested.ctl.event_inj      = svm->vmcb->control.event_inj;
330         svm->nested.ctl.event_inj_err  = svm->vmcb->control.event_inj_err;
331
332         /* Only a few fields of int_ctl are written by the processor.  */
333         mask = V_IRQ_MASK | V_TPR_MASK;
334         if (!(svm->nested.ctl.int_ctl & V_INTR_MASKING_MASK) &&
335             svm_is_intercept(svm, INTERCEPT_VINTR)) {
336                 /*
337                  * In order to request an interrupt window, L0 is usurping
338                  * svm->vmcb->control.int_ctl and possibly setting V_IRQ
339                  * even if it was clear in L1's VMCB.  Restoring it would be
340                  * wrong.  However, in this case V_IRQ will remain true until
341                  * interrupt_window_interception calls svm_clear_vintr and
342                  * restores int_ctl.  We can just leave it aside.
343                  */
344                 mask &= ~V_IRQ_MASK;
345         }
346         svm->nested.ctl.int_ctl        &= ~mask;
347         svm->nested.ctl.int_ctl        |= svm->vmcb->control.int_ctl & mask;
348 }
349
350 /*
351  * Transfer any event that L0 or L1 wanted to inject into L2 to
352  * EXIT_INT_INFO.
353  */
354 static void nested_save_pending_event_to_vmcb12(struct vcpu_svm *svm,
355                                                 struct vmcb *vmcb12)
356 {
357         struct kvm_vcpu *vcpu = &svm->vcpu;
358         u32 exit_int_info = 0;
359         unsigned int nr;
360
361         if (vcpu->arch.exception.injected) {
362                 nr = vcpu->arch.exception.nr;
363                 exit_int_info = nr | SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_EXEPT;
364
365                 if (vcpu->arch.exception.has_error_code) {
366                         exit_int_info |= SVM_EVTINJ_VALID_ERR;
367                         vmcb12->control.exit_int_info_err =
368                                 vcpu->arch.exception.error_code;
369                 }
370
371         } else if (vcpu->arch.nmi_injected) {
372                 exit_int_info = SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_NMI;
373
374         } else if (vcpu->arch.interrupt.injected) {
375                 nr = vcpu->arch.interrupt.nr;
376                 exit_int_info = nr | SVM_EVTINJ_VALID;
377
378                 if (vcpu->arch.interrupt.soft)
379                         exit_int_info |= SVM_EVTINJ_TYPE_SOFT;
380                 else
381                         exit_int_info |= SVM_EVTINJ_TYPE_INTR;
382         }
383
384         vmcb12->control.exit_int_info = exit_int_info;
385 }
386
387 static inline bool nested_npt_enabled(struct vcpu_svm *svm)
388 {
389         return svm->nested.ctl.nested_ctl & SVM_NESTED_CTL_NP_ENABLE;
390 }
391
392 static void nested_svm_transition_tlb_flush(struct kvm_vcpu *vcpu)
393 {
394         /*
395          * TODO: optimize unconditional TLB flush/MMU sync.  A partial list of
396          * things to fix before this can be conditional:
397          *
398          *  - Flush TLBs for both L1 and L2 remote TLB flush
399          *  - Honor L1's request to flush an ASID on nested VMRUN
400          *  - Sync nested NPT MMU on VMRUN that flushes L2's ASID[*]
401          *  - Don't crush a pending TLB flush in vmcb02 on nested VMRUN
402          *  - Flush L1's ASID on KVM_REQ_TLB_FLUSH_GUEST
403          *
404          * [*] Unlike nested EPT, SVM's ASID management can invalidate nested
405          *     NPT guest-physical mappings on VMRUN.
406          */
407         kvm_make_request(KVM_REQ_MMU_SYNC, vcpu);
408         kvm_make_request(KVM_REQ_TLB_FLUSH_CURRENT, vcpu);
409 }
410
411 /*
412  * Load guest's/host's cr3 on nested vmentry or vmexit. @nested_npt is true
413  * if we are emulating VM-Entry into a guest with NPT enabled.
414  */
415 static int nested_svm_load_cr3(struct kvm_vcpu *vcpu, unsigned long cr3,
416                                bool nested_npt, bool reload_pdptrs)
417 {
418         if (CC(kvm_vcpu_is_illegal_gpa(vcpu, cr3)))
419                 return -EINVAL;
420
421         if (reload_pdptrs && !nested_npt && is_pae_paging(vcpu) &&
422             CC(!load_pdptrs(vcpu, vcpu->arch.walk_mmu, cr3)))
423                 return -EINVAL;
424
425         if (!nested_npt)
426                 kvm_mmu_new_pgd(vcpu, cr3);
427
428         vcpu->arch.cr3 = cr3;
429         kvm_register_mark_available(vcpu, VCPU_EXREG_CR3);
430
431         /* Re-initialize the MMU, e.g. to pick up CR4 MMU role changes. */
432         kvm_init_mmu(vcpu);
433
434         return 0;
435 }
436
437 void nested_vmcb02_compute_g_pat(struct vcpu_svm *svm)
438 {
439         if (!svm->nested.vmcb02.ptr)
440                 return;
441
442         /* FIXME: merge g_pat from vmcb01 and vmcb12.  */
443         svm->nested.vmcb02.ptr->save.g_pat = svm->vmcb01.ptr->save.g_pat;
444 }
445
446 static void nested_vmcb02_prepare_save(struct vcpu_svm *svm, struct vmcb *vmcb12)
447 {
448         bool new_vmcb12 = false;
449
450         nested_vmcb02_compute_g_pat(svm);
451
452         /* Load the nested guest state */
453         if (svm->nested.vmcb12_gpa != svm->nested.last_vmcb12_gpa) {
454                 new_vmcb12 = true;
455                 svm->nested.last_vmcb12_gpa = svm->nested.vmcb12_gpa;
456         }
457
458         if (unlikely(new_vmcb12 || vmcb_is_dirty(vmcb12, VMCB_SEG))) {
459                 svm->vmcb->save.es = vmcb12->save.es;
460                 svm->vmcb->save.cs = vmcb12->save.cs;
461                 svm->vmcb->save.ss = vmcb12->save.ss;
462                 svm->vmcb->save.ds = vmcb12->save.ds;
463                 svm->vmcb->save.cpl = vmcb12->save.cpl;
464                 vmcb_mark_dirty(svm->vmcb, VMCB_SEG);
465         }
466
467         if (unlikely(new_vmcb12 || vmcb_is_dirty(vmcb12, VMCB_DT))) {
468                 svm->vmcb->save.gdtr = vmcb12->save.gdtr;
469                 svm->vmcb->save.idtr = vmcb12->save.idtr;
470                 vmcb_mark_dirty(svm->vmcb, VMCB_DT);
471         }
472
473         kvm_set_rflags(&svm->vcpu, vmcb12->save.rflags | X86_EFLAGS_FIXED);
474
475         /*
476          * Force-set EFER_SVME even though it is checked earlier on the
477          * VMCB12, because the guest can flip the bit between the check
478          * and now.  Clearing EFER_SVME would call svm_free_nested.
479          */
480         svm_set_efer(&svm->vcpu, vmcb12->save.efer | EFER_SVME);
481
482         svm_set_cr0(&svm->vcpu, vmcb12->save.cr0);
483         svm_set_cr4(&svm->vcpu, vmcb12->save.cr4);
484
485         svm->vcpu.arch.cr2 = vmcb12->save.cr2;
486
487         kvm_rax_write(&svm->vcpu, vmcb12->save.rax);
488         kvm_rsp_write(&svm->vcpu, vmcb12->save.rsp);
489         kvm_rip_write(&svm->vcpu, vmcb12->save.rip);
490
491         /* In case we don't even reach vcpu_run, the fields are not updated */
492         svm->vmcb->save.rax = vmcb12->save.rax;
493         svm->vmcb->save.rsp = vmcb12->save.rsp;
494         svm->vmcb->save.rip = vmcb12->save.rip;
495
496         /* These bits will be set properly on the first execution when new_vmc12 is true */
497         if (unlikely(new_vmcb12 || vmcb_is_dirty(vmcb12, VMCB_DR))) {
498                 svm->vmcb->save.dr7 = vmcb12->save.dr7 | DR7_FIXED_1;
499                 svm->vcpu.arch.dr6  = vmcb12->save.dr6 | DR6_ACTIVE_LOW;
500                 vmcb_mark_dirty(svm->vmcb, VMCB_DR);
501         }
502 }
503
504 static void nested_vmcb02_prepare_control(struct vcpu_svm *svm)
505 {
506         const u32 mask = V_INTR_MASKING_MASK | V_GIF_ENABLE_MASK | V_GIF_MASK;
507         struct kvm_vcpu *vcpu = &svm->vcpu;
508
509         /*
510          * Filled at exit: exit_code, exit_code_hi, exit_info_1, exit_info_2,
511          * exit_int_info, exit_int_info_err, next_rip, insn_len, insn_bytes.
512          */
513
514         /*
515          * Also covers avic_vapic_bar, avic_backing_page, avic_logical_id,
516          * avic_physical_id.
517          */
518         WARN_ON(svm->vmcb01.ptr->control.int_ctl & AVIC_ENABLE_MASK);
519
520         /* Copied from vmcb01.  msrpm_base can be overwritten later.  */
521         svm->vmcb->control.nested_ctl = svm->vmcb01.ptr->control.nested_ctl;
522         svm->vmcb->control.iopm_base_pa = svm->vmcb01.ptr->control.iopm_base_pa;
523         svm->vmcb->control.msrpm_base_pa = svm->vmcb01.ptr->control.msrpm_base_pa;
524
525         /* Done at vmrun: asid.  */
526
527         /* Also overwritten later if necessary.  */
528         svm->vmcb->control.tlb_ctl = TLB_CONTROL_DO_NOTHING;
529
530         /* nested_cr3.  */
531         if (nested_npt_enabled(svm))
532                 nested_svm_init_mmu_context(vcpu);
533
534         svm->vmcb->control.tsc_offset = vcpu->arch.tsc_offset =
535                 vcpu->arch.l1_tsc_offset + svm->nested.ctl.tsc_offset;
536
537         svm->vmcb->control.int_ctl             =
538                 (svm->nested.ctl.int_ctl & ~mask) |
539                 (svm->vmcb01.ptr->control.int_ctl & mask);
540
541         svm->vmcb->control.virt_ext            = svm->nested.ctl.virt_ext;
542         svm->vmcb->control.int_vector          = svm->nested.ctl.int_vector;
543         svm->vmcb->control.int_state           = svm->nested.ctl.int_state;
544         svm->vmcb->control.event_inj           = svm->nested.ctl.event_inj;
545         svm->vmcb->control.event_inj_err       = svm->nested.ctl.event_inj_err;
546
547         svm->vmcb->control.pause_filter_count  = svm->nested.ctl.pause_filter_count;
548         svm->vmcb->control.pause_filter_thresh = svm->nested.ctl.pause_filter_thresh;
549
550         nested_svm_transition_tlb_flush(vcpu);
551
552         /* Enter Guest-Mode */
553         enter_guest_mode(vcpu);
554
555         /*
556          * Merge guest and host intercepts - must be called with vcpu in
557          * guest-mode to take effect.
558          */
559         recalc_intercepts(svm);
560 }
561
562 static void nested_svm_copy_common_state(struct vmcb *from_vmcb, struct vmcb *to_vmcb)
563 {
564         /*
565          * Some VMCB state is shared between L1 and L2 and thus has to be
566          * moved at the time of nested vmrun and vmexit.
567          *
568          * VMLOAD/VMSAVE state would also belong in this category, but KVM
569          * always performs VMLOAD and VMSAVE from the VMCB01.
570          */
571         to_vmcb->save.spec_ctrl = from_vmcb->save.spec_ctrl;
572 }
573
574 int enter_svm_guest_mode(struct kvm_vcpu *vcpu, u64 vmcb12_gpa,
575                          struct vmcb *vmcb12)
576 {
577         struct vcpu_svm *svm = to_svm(vcpu);
578         int ret;
579
580         trace_kvm_nested_vmrun(svm->vmcb->save.rip, vmcb12_gpa,
581                                vmcb12->save.rip,
582                                vmcb12->control.int_ctl,
583                                vmcb12->control.event_inj,
584                                vmcb12->control.nested_ctl);
585
586         trace_kvm_nested_intercepts(vmcb12->control.intercepts[INTERCEPT_CR] & 0xffff,
587                                     vmcb12->control.intercepts[INTERCEPT_CR] >> 16,
588                                     vmcb12->control.intercepts[INTERCEPT_EXCEPTION],
589                                     vmcb12->control.intercepts[INTERCEPT_WORD3],
590                                     vmcb12->control.intercepts[INTERCEPT_WORD4],
591                                     vmcb12->control.intercepts[INTERCEPT_WORD5]);
592
593
594         svm->nested.vmcb12_gpa = vmcb12_gpa;
595
596         WARN_ON(svm->vmcb == svm->nested.vmcb02.ptr);
597
598         nested_svm_copy_common_state(svm->vmcb01.ptr, svm->nested.vmcb02.ptr);
599
600         svm_switch_vmcb(svm, &svm->nested.vmcb02);
601         nested_vmcb02_prepare_control(svm);
602         nested_vmcb02_prepare_save(svm, vmcb12);
603
604         ret = nested_svm_load_cr3(&svm->vcpu, vmcb12->save.cr3,
605                                   nested_npt_enabled(svm), true);
606         if (ret)
607                 return ret;
608
609         if (!npt_enabled)
610                 vcpu->arch.mmu->inject_page_fault = svm_inject_page_fault_nested;
611
612         svm_set_gif(svm, true);
613
614         return 0;
615 }
616
617 int nested_svm_vmrun(struct kvm_vcpu *vcpu)
618 {
619         struct vcpu_svm *svm = to_svm(vcpu);
620         int ret;
621         struct vmcb *vmcb12;
622         struct kvm_host_map map;
623         u64 vmcb12_gpa;
624
625         if (!svm->nested.hsave_msr) {
626                 kvm_inject_gp(vcpu, 0);
627                 return 1;
628         }
629
630         if (is_smm(vcpu)) {
631                 kvm_queue_exception(vcpu, UD_VECTOR);
632                 return 1;
633         }
634
635         vmcb12_gpa = svm->vmcb->save.rax;
636         ret = kvm_vcpu_map(vcpu, gpa_to_gfn(vmcb12_gpa), &map);
637         if (ret == -EINVAL) {
638                 kvm_inject_gp(vcpu, 0);
639                 return 1;
640         } else if (ret) {
641                 return kvm_skip_emulated_instruction(vcpu);
642         }
643
644         ret = kvm_skip_emulated_instruction(vcpu);
645
646         vmcb12 = map.hva;
647
648         if (WARN_ON_ONCE(!svm->nested.initialized))
649                 return -EINVAL;
650
651         nested_load_control_from_vmcb12(svm, &vmcb12->control);
652
653         if (!nested_vmcb_valid_sregs(vcpu, &vmcb12->save) ||
654             !nested_vmcb_check_controls(vcpu, &svm->nested.ctl)) {
655                 vmcb12->control.exit_code    = SVM_EXIT_ERR;
656                 vmcb12->control.exit_code_hi = 0;
657                 vmcb12->control.exit_info_1  = 0;
658                 vmcb12->control.exit_info_2  = 0;
659                 goto out;
660         }
661
662
663         /* Clear internal status */
664         kvm_clear_exception_queue(vcpu);
665         kvm_clear_interrupt_queue(vcpu);
666
667         /*
668          * Since vmcb01 is not in use, we can use it to store some of the L1
669          * state.
670          */
671         svm->vmcb01.ptr->save.efer   = vcpu->arch.efer;
672         svm->vmcb01.ptr->save.cr0    = kvm_read_cr0(vcpu);
673         svm->vmcb01.ptr->save.cr4    = vcpu->arch.cr4;
674         svm->vmcb01.ptr->save.rflags = kvm_get_rflags(vcpu);
675         svm->vmcb01.ptr->save.rip    = kvm_rip_read(vcpu);
676
677         if (!npt_enabled)
678                 svm->vmcb01.ptr->save.cr3 = kvm_read_cr3(vcpu);
679
680         svm->nested.nested_run_pending = 1;
681
682         if (enter_svm_guest_mode(vcpu, vmcb12_gpa, vmcb12))
683                 goto out_exit_err;
684
685         if (nested_svm_vmrun_msrpm(svm))
686                 goto out;
687
688 out_exit_err:
689         svm->nested.nested_run_pending = 0;
690
691         svm->vmcb->control.exit_code    = SVM_EXIT_ERR;
692         svm->vmcb->control.exit_code_hi = 0;
693         svm->vmcb->control.exit_info_1  = 0;
694         svm->vmcb->control.exit_info_2  = 0;
695
696         nested_svm_vmexit(svm);
697
698 out:
699         kvm_vcpu_unmap(vcpu, &map, true);
700
701         return ret;
702 }
703
704 void nested_svm_vmloadsave(struct vmcb *from_vmcb, struct vmcb *to_vmcb)
705 {
706         to_vmcb->save.fs = from_vmcb->save.fs;
707         to_vmcb->save.gs = from_vmcb->save.gs;
708         to_vmcb->save.tr = from_vmcb->save.tr;
709         to_vmcb->save.ldtr = from_vmcb->save.ldtr;
710         to_vmcb->save.kernel_gs_base = from_vmcb->save.kernel_gs_base;
711         to_vmcb->save.star = from_vmcb->save.star;
712         to_vmcb->save.lstar = from_vmcb->save.lstar;
713         to_vmcb->save.cstar = from_vmcb->save.cstar;
714         to_vmcb->save.sfmask = from_vmcb->save.sfmask;
715         to_vmcb->save.sysenter_cs = from_vmcb->save.sysenter_cs;
716         to_vmcb->save.sysenter_esp = from_vmcb->save.sysenter_esp;
717         to_vmcb->save.sysenter_eip = from_vmcb->save.sysenter_eip;
718 }
719
720 int nested_svm_vmexit(struct vcpu_svm *svm)
721 {
722         struct kvm_vcpu *vcpu = &svm->vcpu;
723         struct vmcb *vmcb12;
724         struct vmcb *vmcb = svm->vmcb;
725         struct kvm_host_map map;
726         int rc;
727
728         /* Triple faults in L2 should never escape. */
729         WARN_ON_ONCE(kvm_check_request(KVM_REQ_TRIPLE_FAULT, vcpu));
730
731         rc = kvm_vcpu_map(vcpu, gpa_to_gfn(svm->nested.vmcb12_gpa), &map);
732         if (rc) {
733                 if (rc == -EINVAL)
734                         kvm_inject_gp(vcpu, 0);
735                 return 1;
736         }
737
738         vmcb12 = map.hva;
739
740         /* Exit Guest-Mode */
741         leave_guest_mode(vcpu);
742         svm->nested.vmcb12_gpa = 0;
743         WARN_ON_ONCE(svm->nested.nested_run_pending);
744
745         kvm_clear_request(KVM_REQ_GET_NESTED_STATE_PAGES, vcpu);
746
747         /* in case we halted in L2 */
748         svm->vcpu.arch.mp_state = KVM_MP_STATE_RUNNABLE;
749
750         /* Give the current vmcb to the guest */
751
752         vmcb12->save.es     = vmcb->save.es;
753         vmcb12->save.cs     = vmcb->save.cs;
754         vmcb12->save.ss     = vmcb->save.ss;
755         vmcb12->save.ds     = vmcb->save.ds;
756         vmcb12->save.gdtr   = vmcb->save.gdtr;
757         vmcb12->save.idtr   = vmcb->save.idtr;
758         vmcb12->save.efer   = svm->vcpu.arch.efer;
759         vmcb12->save.cr0    = kvm_read_cr0(vcpu);
760         vmcb12->save.cr3    = kvm_read_cr3(vcpu);
761         vmcb12->save.cr2    = vmcb->save.cr2;
762         vmcb12->save.cr4    = svm->vcpu.arch.cr4;
763         vmcb12->save.rflags = kvm_get_rflags(vcpu);
764         vmcb12->save.rip    = kvm_rip_read(vcpu);
765         vmcb12->save.rsp    = kvm_rsp_read(vcpu);
766         vmcb12->save.rax    = kvm_rax_read(vcpu);
767         vmcb12->save.dr7    = vmcb->save.dr7;
768         vmcb12->save.dr6    = svm->vcpu.arch.dr6;
769         vmcb12->save.cpl    = vmcb->save.cpl;
770
771         vmcb12->control.int_state         = vmcb->control.int_state;
772         vmcb12->control.exit_code         = vmcb->control.exit_code;
773         vmcb12->control.exit_code_hi      = vmcb->control.exit_code_hi;
774         vmcb12->control.exit_info_1       = vmcb->control.exit_info_1;
775         vmcb12->control.exit_info_2       = vmcb->control.exit_info_2;
776
777         if (vmcb12->control.exit_code != SVM_EXIT_ERR)
778                 nested_save_pending_event_to_vmcb12(svm, vmcb12);
779
780         if (svm->nrips_enabled)
781                 vmcb12->control.next_rip  = vmcb->control.next_rip;
782
783         vmcb12->control.int_ctl           = svm->nested.ctl.int_ctl;
784         vmcb12->control.tlb_ctl           = svm->nested.ctl.tlb_ctl;
785         vmcb12->control.event_inj         = svm->nested.ctl.event_inj;
786         vmcb12->control.event_inj_err     = svm->nested.ctl.event_inj_err;
787
788         vmcb12->control.pause_filter_count =
789                 svm->vmcb->control.pause_filter_count;
790         vmcb12->control.pause_filter_thresh =
791                 svm->vmcb->control.pause_filter_thresh;
792
793         nested_svm_copy_common_state(svm->nested.vmcb02.ptr, svm->vmcb01.ptr);
794
795         svm_switch_vmcb(svm, &svm->vmcb01);
796
797         /*
798          * On vmexit the  GIF is set to false and
799          * no event can be injected in L1.
800          */
801         svm_set_gif(svm, false);
802         svm->vmcb->control.exit_int_info = 0;
803
804         svm->vcpu.arch.tsc_offset = svm->vcpu.arch.l1_tsc_offset;
805         if (svm->vmcb->control.tsc_offset != svm->vcpu.arch.tsc_offset) {
806                 svm->vmcb->control.tsc_offset = svm->vcpu.arch.tsc_offset;
807                 vmcb_mark_dirty(svm->vmcb, VMCB_INTERCEPTS);
808         }
809
810         svm->nested.ctl.nested_cr3 = 0;
811
812         /*
813          * Restore processor state that had been saved in vmcb01
814          */
815         kvm_set_rflags(vcpu, svm->vmcb->save.rflags);
816         svm_set_efer(vcpu, svm->vmcb->save.efer);
817         svm_set_cr0(vcpu, svm->vmcb->save.cr0 | X86_CR0_PE);
818         svm_set_cr4(vcpu, svm->vmcb->save.cr4);
819         kvm_rax_write(vcpu, svm->vmcb->save.rax);
820         kvm_rsp_write(vcpu, svm->vmcb->save.rsp);
821         kvm_rip_write(vcpu, svm->vmcb->save.rip);
822
823         svm->vcpu.arch.dr7 = DR7_FIXED_1;
824         kvm_update_dr7(&svm->vcpu);
825
826         trace_kvm_nested_vmexit_inject(vmcb12->control.exit_code,
827                                        vmcb12->control.exit_info_1,
828                                        vmcb12->control.exit_info_2,
829                                        vmcb12->control.exit_int_info,
830                                        vmcb12->control.exit_int_info_err,
831                                        KVM_ISA_SVM);
832
833         kvm_vcpu_unmap(vcpu, &map, true);
834
835         nested_svm_transition_tlb_flush(vcpu);
836
837         nested_svm_uninit_mmu_context(vcpu);
838
839         rc = nested_svm_load_cr3(vcpu, svm->vmcb->save.cr3, false, true);
840         if (rc)
841                 return 1;
842
843         /*
844          * Drop what we picked up for L2 via svm_complete_interrupts() so it
845          * doesn't end up in L1.
846          */
847         svm->vcpu.arch.nmi_injected = false;
848         kvm_clear_exception_queue(vcpu);
849         kvm_clear_interrupt_queue(vcpu);
850
851         /*
852          * If we are here following the completion of a VMRUN that
853          * is being single-stepped, queue the pending #DB intercept
854          * right now so that it an be accounted for before we execute
855          * L1's next instruction.
856          */
857         if (unlikely(svm->vmcb->save.rflags & X86_EFLAGS_TF))
858                 kvm_queue_exception(&(svm->vcpu), DB_VECTOR);
859
860         return 0;
861 }
862
863 static void nested_svm_triple_fault(struct kvm_vcpu *vcpu)
864 {
865         nested_svm_simple_vmexit(to_svm(vcpu), SVM_EXIT_SHUTDOWN);
866 }
867
868 int svm_allocate_nested(struct vcpu_svm *svm)
869 {
870         struct page *vmcb02_page;
871
872         if (svm->nested.initialized)
873                 return 0;
874
875         vmcb02_page = alloc_page(GFP_KERNEL_ACCOUNT | __GFP_ZERO);
876         if (!vmcb02_page)
877                 return -ENOMEM;
878         svm->nested.vmcb02.ptr = page_address(vmcb02_page);
879         svm->nested.vmcb02.pa = __sme_set(page_to_pfn(vmcb02_page) << PAGE_SHIFT);
880
881         svm->nested.msrpm = svm_vcpu_alloc_msrpm();
882         if (!svm->nested.msrpm)
883                 goto err_free_vmcb02;
884         svm_vcpu_init_msrpm(&svm->vcpu, svm->nested.msrpm);
885
886         svm->nested.initialized = true;
887         return 0;
888
889 err_free_vmcb02:
890         __free_page(vmcb02_page);
891         return -ENOMEM;
892 }
893
894 void svm_free_nested(struct vcpu_svm *svm)
895 {
896         if (!svm->nested.initialized)
897                 return;
898
899         svm_vcpu_free_msrpm(svm->nested.msrpm);
900         svm->nested.msrpm = NULL;
901
902         __free_page(virt_to_page(svm->nested.vmcb02.ptr));
903         svm->nested.vmcb02.ptr = NULL;
904
905         /*
906          * When last_vmcb12_gpa matches the current vmcb12 gpa,
907          * some vmcb12 fields are not loaded if they are marked clean
908          * in the vmcb12, since in this case they are up to date already.
909          *
910          * When the vmcb02 is freed, this optimization becomes invalid.
911          */
912         svm->nested.last_vmcb12_gpa = INVALID_GPA;
913
914         svm->nested.initialized = false;
915 }
916
917 /*
918  * Forcibly leave nested mode in order to be able to reset the VCPU later on.
919  */
920 void svm_leave_nested(struct vcpu_svm *svm)
921 {
922         struct kvm_vcpu *vcpu = &svm->vcpu;
923
924         if (is_guest_mode(vcpu)) {
925                 svm->nested.nested_run_pending = 0;
926                 svm->nested.vmcb12_gpa = INVALID_GPA;
927
928                 leave_guest_mode(vcpu);
929
930                 svm_switch_vmcb(svm, &svm->vmcb01);
931
932                 nested_svm_uninit_mmu_context(vcpu);
933                 vmcb_mark_all_dirty(svm->vmcb);
934         }
935
936         kvm_clear_request(KVM_REQ_GET_NESTED_STATE_PAGES, vcpu);
937 }
938
939 static int nested_svm_exit_handled_msr(struct vcpu_svm *svm)
940 {
941         u32 offset, msr, value;
942         int write, mask;
943
944         if (!(vmcb_is_intercept(&svm->nested.ctl, INTERCEPT_MSR_PROT)))
945                 return NESTED_EXIT_HOST;
946
947         msr    = svm->vcpu.arch.regs[VCPU_REGS_RCX];
948         offset = svm_msrpm_offset(msr);
949         write  = svm->vmcb->control.exit_info_1 & 1;
950         mask   = 1 << ((2 * (msr & 0xf)) + write);
951
952         if (offset == MSR_INVALID)
953                 return NESTED_EXIT_DONE;
954
955         /* Offset is in 32 bit units but need in 8 bit units */
956         offset *= 4;
957
958         if (kvm_vcpu_read_guest(&svm->vcpu, svm->nested.ctl.msrpm_base_pa + offset, &value, 4))
959                 return NESTED_EXIT_DONE;
960
961         return (value & mask) ? NESTED_EXIT_DONE : NESTED_EXIT_HOST;
962 }
963
964 static int nested_svm_intercept_ioio(struct vcpu_svm *svm)
965 {
966         unsigned port, size, iopm_len;
967         u16 val, mask;
968         u8 start_bit;
969         u64 gpa;
970
971         if (!(vmcb_is_intercept(&svm->nested.ctl, INTERCEPT_IOIO_PROT)))
972                 return NESTED_EXIT_HOST;
973
974         port = svm->vmcb->control.exit_info_1 >> 16;
975         size = (svm->vmcb->control.exit_info_1 & SVM_IOIO_SIZE_MASK) >>
976                 SVM_IOIO_SIZE_SHIFT;
977         gpa  = svm->nested.ctl.iopm_base_pa + (port / 8);
978         start_bit = port % 8;
979         iopm_len = (start_bit + size > 8) ? 2 : 1;
980         mask = (0xf >> (4 - size)) << start_bit;
981         val = 0;
982
983         if (kvm_vcpu_read_guest(&svm->vcpu, gpa, &val, iopm_len))
984                 return NESTED_EXIT_DONE;
985
986         return (val & mask) ? NESTED_EXIT_DONE : NESTED_EXIT_HOST;
987 }
988
989 static int nested_svm_intercept(struct vcpu_svm *svm)
990 {
991         u32 exit_code = svm->vmcb->control.exit_code;
992         int vmexit = NESTED_EXIT_HOST;
993
994         switch (exit_code) {
995         case SVM_EXIT_MSR:
996                 vmexit = nested_svm_exit_handled_msr(svm);
997                 break;
998         case SVM_EXIT_IOIO:
999                 vmexit = nested_svm_intercept_ioio(svm);
1000                 break;
1001         case SVM_EXIT_READ_CR0 ... SVM_EXIT_WRITE_CR8: {
1002                 if (vmcb_is_intercept(&svm->nested.ctl, exit_code))
1003                         vmexit = NESTED_EXIT_DONE;
1004                 break;
1005         }
1006         case SVM_EXIT_READ_DR0 ... SVM_EXIT_WRITE_DR7: {
1007                 if (vmcb_is_intercept(&svm->nested.ctl, exit_code))
1008                         vmexit = NESTED_EXIT_DONE;
1009                 break;
1010         }
1011         case SVM_EXIT_EXCP_BASE ... SVM_EXIT_EXCP_BASE + 0x1f: {
1012                 /*
1013                  * Host-intercepted exceptions have been checked already in
1014                  * nested_svm_exit_special.  There is nothing to do here,
1015                  * the vmexit is injected by svm_check_nested_events.
1016                  */
1017                 vmexit = NESTED_EXIT_DONE;
1018                 break;
1019         }
1020         case SVM_EXIT_ERR: {
1021                 vmexit = NESTED_EXIT_DONE;
1022                 break;
1023         }
1024         default: {
1025                 if (vmcb_is_intercept(&svm->nested.ctl, exit_code))
1026                         vmexit = NESTED_EXIT_DONE;
1027         }
1028         }
1029
1030         return vmexit;
1031 }
1032
1033 int nested_svm_exit_handled(struct vcpu_svm *svm)
1034 {
1035         int vmexit;
1036
1037         vmexit = nested_svm_intercept(svm);
1038
1039         if (vmexit == NESTED_EXIT_DONE)
1040                 nested_svm_vmexit(svm);
1041
1042         return vmexit;
1043 }
1044
1045 int nested_svm_check_permissions(struct kvm_vcpu *vcpu)
1046 {
1047         if (!(vcpu->arch.efer & EFER_SVME) || !is_paging(vcpu)) {
1048                 kvm_queue_exception(vcpu, UD_VECTOR);
1049                 return 1;
1050         }
1051
1052         if (to_svm(vcpu)->vmcb->save.cpl) {
1053                 kvm_inject_gp(vcpu, 0);
1054                 return 1;
1055         }
1056
1057         return 0;
1058 }
1059
1060 static bool nested_exit_on_exception(struct vcpu_svm *svm)
1061 {
1062         unsigned int nr = svm->vcpu.arch.exception.nr;
1063
1064         return (svm->nested.ctl.intercepts[INTERCEPT_EXCEPTION] & BIT(nr));
1065 }
1066
1067 static void nested_svm_inject_exception_vmexit(struct vcpu_svm *svm)
1068 {
1069         unsigned int nr = svm->vcpu.arch.exception.nr;
1070
1071         svm->vmcb->control.exit_code = SVM_EXIT_EXCP_BASE + nr;
1072         svm->vmcb->control.exit_code_hi = 0;
1073
1074         if (svm->vcpu.arch.exception.has_error_code)
1075                 svm->vmcb->control.exit_info_1 = svm->vcpu.arch.exception.error_code;
1076
1077         /*
1078          * EXITINFO2 is undefined for all exception intercepts other
1079          * than #PF.
1080          */
1081         if (nr == PF_VECTOR) {
1082                 if (svm->vcpu.arch.exception.nested_apf)
1083                         svm->vmcb->control.exit_info_2 = svm->vcpu.arch.apf.nested_apf_token;
1084                 else if (svm->vcpu.arch.exception.has_payload)
1085                         svm->vmcb->control.exit_info_2 = svm->vcpu.arch.exception.payload;
1086                 else
1087                         svm->vmcb->control.exit_info_2 = svm->vcpu.arch.cr2;
1088         } else if (nr == DB_VECTOR) {
1089                 /* See inject_pending_event.  */
1090                 kvm_deliver_exception_payload(&svm->vcpu);
1091                 if (svm->vcpu.arch.dr7 & DR7_GD) {
1092                         svm->vcpu.arch.dr7 &= ~DR7_GD;
1093                         kvm_update_dr7(&svm->vcpu);
1094                 }
1095         } else
1096                 WARN_ON(svm->vcpu.arch.exception.has_payload);
1097
1098         nested_svm_vmexit(svm);
1099 }
1100
1101 static inline bool nested_exit_on_init(struct vcpu_svm *svm)
1102 {
1103         return vmcb_is_intercept(&svm->nested.ctl, INTERCEPT_INIT);
1104 }
1105
1106 static int svm_check_nested_events(struct kvm_vcpu *vcpu)
1107 {
1108         struct vcpu_svm *svm = to_svm(vcpu);
1109         bool block_nested_events =
1110                 kvm_event_needs_reinjection(vcpu) || svm->nested.nested_run_pending;
1111         struct kvm_lapic *apic = vcpu->arch.apic;
1112
1113         if (lapic_in_kernel(vcpu) &&
1114             test_bit(KVM_APIC_INIT, &apic->pending_events)) {
1115                 if (block_nested_events)
1116                         return -EBUSY;
1117                 if (!nested_exit_on_init(svm))
1118                         return 0;
1119                 nested_svm_simple_vmexit(svm, SVM_EXIT_INIT);
1120                 return 0;
1121         }
1122
1123         if (vcpu->arch.exception.pending) {
1124                 /*
1125                  * Only a pending nested run can block a pending exception.
1126                  * Otherwise an injected NMI/interrupt should either be
1127                  * lost or delivered to the nested hypervisor in the EXITINTINFO
1128                  * vmcb field, while delivering the pending exception.
1129                  */
1130                 if (svm->nested.nested_run_pending)
1131                         return -EBUSY;
1132                 if (!nested_exit_on_exception(svm))
1133                         return 0;
1134                 nested_svm_inject_exception_vmexit(svm);
1135                 return 0;
1136         }
1137
1138         if (vcpu->arch.smi_pending && !svm_smi_blocked(vcpu)) {
1139                 if (block_nested_events)
1140                         return -EBUSY;
1141                 if (!nested_exit_on_smi(svm))
1142                         return 0;
1143                 nested_svm_simple_vmexit(svm, SVM_EXIT_SMI);
1144                 return 0;
1145         }
1146
1147         if (vcpu->arch.nmi_pending && !svm_nmi_blocked(vcpu)) {
1148                 if (block_nested_events)
1149                         return -EBUSY;
1150                 if (!nested_exit_on_nmi(svm))
1151                         return 0;
1152                 nested_svm_simple_vmexit(svm, SVM_EXIT_NMI);
1153                 return 0;
1154         }
1155
1156         if (kvm_cpu_has_interrupt(vcpu) && !svm_interrupt_blocked(vcpu)) {
1157                 if (block_nested_events)
1158                         return -EBUSY;
1159                 if (!nested_exit_on_intr(svm))
1160                         return 0;
1161                 trace_kvm_nested_intr_vmexit(svm->vmcb->save.rip);
1162                 nested_svm_simple_vmexit(svm, SVM_EXIT_INTR);
1163                 return 0;
1164         }
1165
1166         return 0;
1167 }
1168
1169 int nested_svm_exit_special(struct vcpu_svm *svm)
1170 {
1171         u32 exit_code = svm->vmcb->control.exit_code;
1172
1173         switch (exit_code) {
1174         case SVM_EXIT_INTR:
1175         case SVM_EXIT_NMI:
1176         case SVM_EXIT_NPF:
1177                 return NESTED_EXIT_HOST;
1178         case SVM_EXIT_EXCP_BASE ... SVM_EXIT_EXCP_BASE + 0x1f: {
1179                 u32 excp_bits = 1 << (exit_code - SVM_EXIT_EXCP_BASE);
1180
1181                 if (svm->vmcb01.ptr->control.intercepts[INTERCEPT_EXCEPTION] &
1182                     excp_bits)
1183                         return NESTED_EXIT_HOST;
1184                 else if (exit_code == SVM_EXIT_EXCP_BASE + PF_VECTOR &&
1185                          svm->vcpu.arch.apf.host_apf_flags)
1186                         /* Trap async PF even if not shadowing */
1187                         return NESTED_EXIT_HOST;
1188                 break;
1189         }
1190         default:
1191                 break;
1192         }
1193
1194         return NESTED_EXIT_CONTINUE;
1195 }
1196
1197 static int svm_get_nested_state(struct kvm_vcpu *vcpu,
1198                                 struct kvm_nested_state __user *user_kvm_nested_state,
1199                                 u32 user_data_size)
1200 {
1201         struct vcpu_svm *svm;
1202         struct kvm_nested_state kvm_state = {
1203                 .flags = 0,
1204                 .format = KVM_STATE_NESTED_FORMAT_SVM,
1205                 .size = sizeof(kvm_state),
1206         };
1207         struct vmcb __user *user_vmcb = (struct vmcb __user *)
1208                 &user_kvm_nested_state->data.svm[0];
1209
1210         if (!vcpu)
1211                 return kvm_state.size + KVM_STATE_NESTED_SVM_VMCB_SIZE;
1212
1213         svm = to_svm(vcpu);
1214
1215         if (user_data_size < kvm_state.size)
1216                 goto out;
1217
1218         /* First fill in the header and copy it out.  */
1219         if (is_guest_mode(vcpu)) {
1220                 kvm_state.hdr.svm.vmcb_pa = svm->nested.vmcb12_gpa;
1221                 kvm_state.size += KVM_STATE_NESTED_SVM_VMCB_SIZE;
1222                 kvm_state.flags |= KVM_STATE_NESTED_GUEST_MODE;
1223
1224                 if (svm->nested.nested_run_pending)
1225                         kvm_state.flags |= KVM_STATE_NESTED_RUN_PENDING;
1226         }
1227
1228         if (gif_set(svm))
1229                 kvm_state.flags |= KVM_STATE_NESTED_GIF_SET;
1230
1231         if (copy_to_user(user_kvm_nested_state, &kvm_state, sizeof(kvm_state)))
1232                 return -EFAULT;
1233
1234         if (!is_guest_mode(vcpu))
1235                 goto out;
1236
1237         /*
1238          * Copy over the full size of the VMCB rather than just the size
1239          * of the structs.
1240          */
1241         if (clear_user(user_vmcb, KVM_STATE_NESTED_SVM_VMCB_SIZE))
1242                 return -EFAULT;
1243         if (copy_to_user(&user_vmcb->control, &svm->nested.ctl,
1244                          sizeof(user_vmcb->control)))
1245                 return -EFAULT;
1246         if (copy_to_user(&user_vmcb->save, &svm->vmcb01.ptr->save,
1247                          sizeof(user_vmcb->save)))
1248                 return -EFAULT;
1249 out:
1250         return kvm_state.size;
1251 }
1252
1253 static int svm_set_nested_state(struct kvm_vcpu *vcpu,
1254                                 struct kvm_nested_state __user *user_kvm_nested_state,
1255                                 struct kvm_nested_state *kvm_state)
1256 {
1257         struct vcpu_svm *svm = to_svm(vcpu);
1258         struct vmcb __user *user_vmcb = (struct vmcb __user *)
1259                 &user_kvm_nested_state->data.svm[0];
1260         struct vmcb_control_area *ctl;
1261         struct vmcb_save_area *save;
1262         unsigned long cr0;
1263         int ret;
1264
1265         BUILD_BUG_ON(sizeof(struct vmcb_control_area) + sizeof(struct vmcb_save_area) >
1266                      KVM_STATE_NESTED_SVM_VMCB_SIZE);
1267
1268         if (kvm_state->format != KVM_STATE_NESTED_FORMAT_SVM)
1269                 return -EINVAL;
1270
1271         if (kvm_state->flags & ~(KVM_STATE_NESTED_GUEST_MODE |
1272                                  KVM_STATE_NESTED_RUN_PENDING |
1273                                  KVM_STATE_NESTED_GIF_SET))
1274                 return -EINVAL;
1275
1276         /*
1277          * If in guest mode, vcpu->arch.efer actually refers to the L2 guest's
1278          * EFER.SVME, but EFER.SVME still has to be 1 for VMRUN to succeed.
1279          */
1280         if (!(vcpu->arch.efer & EFER_SVME)) {
1281                 /* GIF=1 and no guest mode are required if SVME=0.  */
1282                 if (kvm_state->flags != KVM_STATE_NESTED_GIF_SET)
1283                         return -EINVAL;
1284         }
1285
1286         /* SMM temporarily disables SVM, so we cannot be in guest mode.  */
1287         if (is_smm(vcpu) && (kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE))
1288                 return -EINVAL;
1289
1290         if (!(kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE)) {
1291                 svm_leave_nested(svm);
1292                 svm_set_gif(svm, !!(kvm_state->flags & KVM_STATE_NESTED_GIF_SET));
1293                 return 0;
1294         }
1295
1296         if (!page_address_valid(vcpu, kvm_state->hdr.svm.vmcb_pa))
1297                 return -EINVAL;
1298         if (kvm_state->size < sizeof(*kvm_state) + KVM_STATE_NESTED_SVM_VMCB_SIZE)
1299                 return -EINVAL;
1300
1301         ret  = -ENOMEM;
1302         ctl  = kzalloc(sizeof(*ctl),  GFP_KERNEL_ACCOUNT);
1303         save = kzalloc(sizeof(*save), GFP_KERNEL_ACCOUNT);
1304         if (!ctl || !save)
1305                 goto out_free;
1306
1307         ret = -EFAULT;
1308         if (copy_from_user(ctl, &user_vmcb->control, sizeof(*ctl)))
1309                 goto out_free;
1310         if (copy_from_user(save, &user_vmcb->save, sizeof(*save)))
1311                 goto out_free;
1312
1313         ret = -EINVAL;
1314         if (!nested_vmcb_check_controls(vcpu, ctl))
1315                 goto out_free;
1316
1317         /*
1318          * Processor state contains L2 state.  Check that it is
1319          * valid for guest mode (see nested_vmcb_check_save).
1320          */
1321         cr0 = kvm_read_cr0(vcpu);
1322         if (((cr0 & X86_CR0_CD) == 0) && (cr0 & X86_CR0_NW))
1323                 goto out_free;
1324
1325         /*
1326          * Validate host state saved from before VMRUN (see
1327          * nested_svm_check_permissions).
1328          */
1329         if (!(save->cr0 & X86_CR0_PG) ||
1330             !(save->cr0 & X86_CR0_PE) ||
1331             (save->rflags & X86_EFLAGS_VM) ||
1332             !nested_vmcb_valid_sregs(vcpu, save))
1333                 goto out_free;
1334
1335         /*
1336          * While the nested guest CR3 is already checked and set by
1337          * KVM_SET_SREGS, it was set when nested state was yet loaded,
1338          * thus MMU might not be initialized correctly.
1339          * Set it again to fix this.
1340          */
1341
1342         ret = nested_svm_load_cr3(&svm->vcpu, vcpu->arch.cr3,
1343                                   nested_npt_enabled(svm), false);
1344         if (WARN_ON_ONCE(ret))
1345                 goto out_free;
1346
1347
1348         /*
1349          * All checks done, we can enter guest mode. Userspace provides
1350          * vmcb12.control, which will be combined with L1 and stored into
1351          * vmcb02, and the L1 save state which we store in vmcb01.
1352          * L2 registers if needed are moved from the current VMCB to VMCB02.
1353          */
1354
1355         if (is_guest_mode(vcpu))
1356                 svm_leave_nested(svm);
1357         else
1358                 svm->nested.vmcb02.ptr->save = svm->vmcb01.ptr->save;
1359
1360         svm_set_gif(svm, !!(kvm_state->flags & KVM_STATE_NESTED_GIF_SET));
1361
1362         svm->nested.nested_run_pending =
1363                 !!(kvm_state->flags & KVM_STATE_NESTED_RUN_PENDING);
1364
1365         svm->nested.vmcb12_gpa = kvm_state->hdr.svm.vmcb_pa;
1366
1367         svm->vmcb01.ptr->save.es = save->es;
1368         svm->vmcb01.ptr->save.cs = save->cs;
1369         svm->vmcb01.ptr->save.ss = save->ss;
1370         svm->vmcb01.ptr->save.ds = save->ds;
1371         svm->vmcb01.ptr->save.gdtr = save->gdtr;
1372         svm->vmcb01.ptr->save.idtr = save->idtr;
1373         svm->vmcb01.ptr->save.rflags = save->rflags | X86_EFLAGS_FIXED;
1374         svm->vmcb01.ptr->save.efer = save->efer;
1375         svm->vmcb01.ptr->save.cr0 = save->cr0;
1376         svm->vmcb01.ptr->save.cr3 = save->cr3;
1377         svm->vmcb01.ptr->save.cr4 = save->cr4;
1378         svm->vmcb01.ptr->save.rax = save->rax;
1379         svm->vmcb01.ptr->save.rsp = save->rsp;
1380         svm->vmcb01.ptr->save.rip = save->rip;
1381         svm->vmcb01.ptr->save.cpl = 0;
1382
1383         nested_load_control_from_vmcb12(svm, ctl);
1384
1385         svm_switch_vmcb(svm, &svm->nested.vmcb02);
1386
1387         nested_vmcb02_prepare_control(svm);
1388
1389         kvm_make_request(KVM_REQ_GET_NESTED_STATE_PAGES, vcpu);
1390         ret = 0;
1391 out_free:
1392         kfree(save);
1393         kfree(ctl);
1394
1395         return ret;
1396 }
1397
1398 static bool svm_get_nested_state_pages(struct kvm_vcpu *vcpu)
1399 {
1400         struct vcpu_svm *svm = to_svm(vcpu);
1401
1402         if (WARN_ON(!is_guest_mode(vcpu)))
1403                 return true;
1404
1405         if (!vcpu->arch.pdptrs_from_userspace &&
1406             !nested_npt_enabled(svm) && is_pae_paging(vcpu))
1407                 /*
1408                  * Reload the guest's PDPTRs since after a migration
1409                  * the guest CR3 might be restored prior to setting the nested
1410                  * state which can lead to a load of wrong PDPTRs.
1411                  */
1412                 if (CC(!load_pdptrs(vcpu, vcpu->arch.walk_mmu, vcpu->arch.cr3)))
1413                         return false;
1414
1415         if (!nested_svm_vmrun_msrpm(svm)) {
1416                 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
1417                 vcpu->run->internal.suberror =
1418                         KVM_INTERNAL_ERROR_EMULATION;
1419                 vcpu->run->internal.ndata = 0;
1420                 return false;
1421         }
1422
1423         return true;
1424 }
1425
1426 struct kvm_x86_nested_ops svm_nested_ops = {
1427         .check_events = svm_check_nested_events,
1428         .triple_fault = nested_svm_triple_fault,
1429         .get_nested_state_pages = svm_get_nested_state_pages,
1430         .get_state = svm_get_nested_state,
1431         .set_state = svm_set_nested_state,
1432 };