1 // SPDX-License-Identifier: GPL-2.0-only
3 * Kernel-based Virtual Machine driver for Linux
7 * Copyright (C) 2006 Qumranet, Inc.
8 * Copyright 2010 Red Hat, Inc. and/or its affiliates.
11 * Yaniv Kamay <yaniv@qumranet.com>
12 * Avi Kivity <avi@qumranet.com>
18 #include <linux/kvm_types.h>
19 #include <linux/kvm_host.h>
20 #include <linux/bits.h>
23 #include <asm/sev-common.h>
25 #include "kvm_cache_regs.h"
27 #define __sme_page_pa(x) __sme_set(page_to_pfn(x) << PAGE_SHIFT)
29 #define IOPM_SIZE PAGE_SIZE * 3
30 #define MSRPM_SIZE PAGE_SIZE * 2
32 #define MAX_DIRECT_ACCESS_MSRS 20
33 #define MSRPM_OFFSETS 16
34 extern u32 msrpm_offsets[MSRPM_OFFSETS] __read_mostly;
35 extern bool npt_enabled;
36 extern bool intercept_smi;
40 * VMCB_ALL_CLEAN_MASK might also need to
41 * be updated if this enum is modified.
44 VMCB_INTERCEPTS, /* Intercept vectors, TSC offset,
46 VMCB_PERM_MAP, /* IOPM Base and MSRPM Base */
48 VMCB_INTR, /* int_ctl, int_vector */
49 VMCB_NPT, /* npt_en, nCR3, gPAT */
50 VMCB_CR, /* CR0, CR3, CR4, EFER */
51 VMCB_DR, /* DR6, DR7 */
52 VMCB_DT, /* GDT, IDT */
53 VMCB_SEG, /* CS, DS, SS, ES, CPL */
54 VMCB_CR2, /* CR2 only */
55 VMCB_LBR, /* DBGCTL, BR_FROM, BR_TO, LAST_EX_FROM, LAST_EX_TO */
56 VMCB_AVIC, /* AVIC APIC_BAR, AVIC APIC_BACKING_PAGE,
57 * AVIC PHYSICAL_TABLE pointer,
58 * AVIC LOGICAL_TABLE pointer
60 VMCB_SW = 31, /* Reserved for hypervisor/software use */
63 #define VMCB_ALL_CLEAN_MASK ( \
64 (1U << VMCB_INTERCEPTS) | (1U << VMCB_PERM_MAP) | \
65 (1U << VMCB_ASID) | (1U << VMCB_INTR) | \
66 (1U << VMCB_NPT) | (1U << VMCB_CR) | (1U << VMCB_DR) | \
67 (1U << VMCB_DT) | (1U << VMCB_SEG) | (1U << VMCB_CR2) | \
68 (1U << VMCB_LBR) | (1U << VMCB_AVIC) | \
71 /* TPR and CR2 are always written before VMRUN */
72 #define VMCB_ALWAYS_DIRTY_MASK ((1U << VMCB_INTR) | (1U << VMCB_CR2))
75 bool active; /* SEV enabled guest */
76 bool es_active; /* SEV-ES enabled guest */
77 unsigned int asid; /* ASID used for this guest */
78 unsigned int handle; /* SEV firmware handle */
79 int fd; /* SEV device fd */
80 unsigned long pages_locked; /* Number of pages locked */
81 struct list_head regions_list; /* List of registered regions */
82 u64 ap_jump_table; /* SEV-ES AP Jump Table address */
83 struct kvm *enc_context_owner; /* Owner of copied encryption context */
84 struct list_head mirror_vms; /* List of VMs mirroring */
85 struct list_head mirror_entry; /* Use as a list entry of mirrors */
86 struct misc_cg *misc_cg; /* For misc cgroup accounting */
87 atomic_t migration_in_progress;
93 /* Struct members for AVIC */
95 struct page *avic_logical_id_table_page;
96 struct page *avic_physical_id_table_page;
97 struct hlist_node hnode;
99 struct kvm_sev_info sev_info;
104 struct kvm_vmcb_info {
108 uint64_t asid_generation;
111 struct vmcb_save_area_cached {
120 struct vmcb_ctrl_area_cached {
121 u32 intercepts[MAX_INTERCEPT];
122 u16 pause_filter_thresh;
123 u16 pause_filter_count;
137 u32 exit_int_info_err;
147 struct svm_nested_state {
148 struct kvm_vmcb_info vmcb02;
154 /* These are the merged vectors */
157 /* A VMRUN has started but has not yet been performed, so
158 * we cannot inject a nested vmexit yet. */
159 bool nested_run_pending;
161 /* cache for control fields of the guest */
162 struct vmcb_ctrl_area_cached ctl;
165 * Note: this struct is not kept up-to-date while L2 runs; it is only
166 * valid within nested_svm_vmrun.
168 struct vmcb_save_area_cached save;
173 * Indicates whether MSR bitmap for L2 needs to be rebuilt due to
174 * changes in MSR bitmap for L1 or switching to a different L2. Note,
175 * this flag can only be used reliably in conjunction with a paravirt L1
176 * which informs L0 whether any changes to MSR bitmap for L2 were done
179 bool force_msr_bitmap_recalc;
182 struct vcpu_sev_es_state {
184 struct vmcb_save_area *vmsa;
186 struct kvm_host_map ghcb_map;
187 bool received_first_sipi;
189 /* SEV-ES scratch area support */
197 struct kvm_vcpu vcpu;
198 /* vmcb always points at current_vmcb->ptr, it's purely a shorthand. */
200 struct kvm_vmcb_info vmcb01;
201 struct kvm_vmcb_info *current_vmcb;
202 struct svm_cpu_data *svm_data;
216 * Contains guest-controlled bits of VIRT_SPEC_CTRL, which will be
217 * translated into the appropriate L2_CFG bits on the host to
218 * perform speculative control.
226 struct svm_nested_state nested;
229 u64 nmi_singlestep_guest_rflags;
231 unsigned int3_injected;
232 unsigned long int3_rip;
234 /* cached guest cpuid flags for faster access */
235 bool nrips_enabled : 1;
236 bool tsc_scaling_enabled : 1;
240 struct page *avic_backing_page;
241 u64 *avic_physical_id_cache;
244 * Per-vcpu list of struct amd_svm_iommu_ir:
245 * This is used mainly to store interrupt remapping information used
246 * when update the vcpu affinity. This avoids the need to scan for
247 * IRTE and try to match ga_tag in the IOMMU driver.
249 struct list_head ir_list;
250 spinlock_t ir_list_lock;
252 /* Save desired MSR intercept (read: pass-through) state */
254 DECLARE_BITMAP(read, MAX_DIRECT_ACCESS_MSRS);
255 DECLARE_BITMAP(write, MAX_DIRECT_ACCESS_MSRS);
256 } shadow_msr_intercept;
258 struct vcpu_sev_es_state sev_es;
260 bool guest_state_loaded;
263 struct svm_cpu_data {
270 struct kvm_ldttss_desc *tss_desc;
272 struct page *save_area;
273 struct vmcb *current_vmcb;
275 /* index = sev_asid, value = vmcb pointer */
276 struct vmcb **sev_vmcbs;
279 DECLARE_PER_CPU(struct svm_cpu_data *, svm_data);
281 void recalc_intercepts(struct vcpu_svm *svm);
283 static __always_inline struct kvm_svm *to_kvm_svm(struct kvm *kvm)
285 return container_of(kvm, struct kvm_svm, kvm);
288 static __always_inline bool sev_guest(struct kvm *kvm)
290 #ifdef CONFIG_KVM_AMD_SEV
291 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
299 static __always_inline bool sev_es_guest(struct kvm *kvm)
301 #ifdef CONFIG_KVM_AMD_SEV
302 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
304 return sev->es_active && !WARN_ON_ONCE(!sev->active);
310 static inline void vmcb_mark_all_dirty(struct vmcb *vmcb)
312 vmcb->control.clean = 0;
315 static inline void vmcb_mark_all_clean(struct vmcb *vmcb)
317 vmcb->control.clean = VMCB_ALL_CLEAN_MASK
318 & ~VMCB_ALWAYS_DIRTY_MASK;
321 static inline void vmcb_mark_dirty(struct vmcb *vmcb, int bit)
323 vmcb->control.clean &= ~(1 << bit);
326 static inline bool vmcb_is_dirty(struct vmcb *vmcb, int bit)
328 return !test_bit(bit, (unsigned long *)&vmcb->control.clean);
331 static __always_inline struct vcpu_svm *to_svm(struct kvm_vcpu *vcpu)
333 return container_of(vcpu, struct vcpu_svm, vcpu);
337 * Only the PDPTRs are loaded on demand into the shadow MMU. All other
338 * fields are synchronized on VM-Exit, because accessing the VMCB is cheap.
340 * CR3 might be out of date in the VMCB but it is not marked dirty; instead,
341 * KVM_REQ_LOAD_MMU_PGD is always requested when the cached vcpu->arch.cr3
342 * is changed. svm_load_mmu_pgd() then syncs the new CR3 value into the VMCB.
344 #define SVM_REGS_LAZY_LOAD_SET (1 << VCPU_EXREG_PDPTR)
346 static inline void vmcb_set_intercept(struct vmcb_control_area *control, u32 bit)
348 WARN_ON_ONCE(bit >= 32 * MAX_INTERCEPT);
349 __set_bit(bit, (unsigned long *)&control->intercepts);
352 static inline void vmcb_clr_intercept(struct vmcb_control_area *control, u32 bit)
354 WARN_ON_ONCE(bit >= 32 * MAX_INTERCEPT);
355 __clear_bit(bit, (unsigned long *)&control->intercepts);
358 static inline bool vmcb_is_intercept(struct vmcb_control_area *control, u32 bit)
360 WARN_ON_ONCE(bit >= 32 * MAX_INTERCEPT);
361 return test_bit(bit, (unsigned long *)&control->intercepts);
364 static inline bool vmcb12_is_intercept(struct vmcb_ctrl_area_cached *control, u32 bit)
366 WARN_ON_ONCE(bit >= 32 * MAX_INTERCEPT);
367 return test_bit(bit, (unsigned long *)&control->intercepts);
370 static inline void set_dr_intercepts(struct vcpu_svm *svm)
372 struct vmcb *vmcb = svm->vmcb01.ptr;
374 if (!sev_es_guest(svm->vcpu.kvm)) {
375 vmcb_set_intercept(&vmcb->control, INTERCEPT_DR0_READ);
376 vmcb_set_intercept(&vmcb->control, INTERCEPT_DR1_READ);
377 vmcb_set_intercept(&vmcb->control, INTERCEPT_DR2_READ);
378 vmcb_set_intercept(&vmcb->control, INTERCEPT_DR3_READ);
379 vmcb_set_intercept(&vmcb->control, INTERCEPT_DR4_READ);
380 vmcb_set_intercept(&vmcb->control, INTERCEPT_DR5_READ);
381 vmcb_set_intercept(&vmcb->control, INTERCEPT_DR6_READ);
382 vmcb_set_intercept(&vmcb->control, INTERCEPT_DR0_WRITE);
383 vmcb_set_intercept(&vmcb->control, INTERCEPT_DR1_WRITE);
384 vmcb_set_intercept(&vmcb->control, INTERCEPT_DR2_WRITE);
385 vmcb_set_intercept(&vmcb->control, INTERCEPT_DR3_WRITE);
386 vmcb_set_intercept(&vmcb->control, INTERCEPT_DR4_WRITE);
387 vmcb_set_intercept(&vmcb->control, INTERCEPT_DR5_WRITE);
388 vmcb_set_intercept(&vmcb->control, INTERCEPT_DR6_WRITE);
391 vmcb_set_intercept(&vmcb->control, INTERCEPT_DR7_READ);
392 vmcb_set_intercept(&vmcb->control, INTERCEPT_DR7_WRITE);
394 recalc_intercepts(svm);
397 static inline void clr_dr_intercepts(struct vcpu_svm *svm)
399 struct vmcb *vmcb = svm->vmcb01.ptr;
401 vmcb->control.intercepts[INTERCEPT_DR] = 0;
403 /* DR7 access must remain intercepted for an SEV-ES guest */
404 if (sev_es_guest(svm->vcpu.kvm)) {
405 vmcb_set_intercept(&vmcb->control, INTERCEPT_DR7_READ);
406 vmcb_set_intercept(&vmcb->control, INTERCEPT_DR7_WRITE);
409 recalc_intercepts(svm);
412 static inline void set_exception_intercept(struct vcpu_svm *svm, u32 bit)
414 struct vmcb *vmcb = svm->vmcb01.ptr;
416 WARN_ON_ONCE(bit >= 32);
417 vmcb_set_intercept(&vmcb->control, INTERCEPT_EXCEPTION_OFFSET + bit);
419 recalc_intercepts(svm);
422 static inline void clr_exception_intercept(struct vcpu_svm *svm, u32 bit)
424 struct vmcb *vmcb = svm->vmcb01.ptr;
426 WARN_ON_ONCE(bit >= 32);
427 vmcb_clr_intercept(&vmcb->control, INTERCEPT_EXCEPTION_OFFSET + bit);
429 recalc_intercepts(svm);
432 static inline void svm_set_intercept(struct vcpu_svm *svm, int bit)
434 struct vmcb *vmcb = svm->vmcb01.ptr;
436 vmcb_set_intercept(&vmcb->control, bit);
438 recalc_intercepts(svm);
441 static inline void svm_clr_intercept(struct vcpu_svm *svm, int bit)
443 struct vmcb *vmcb = svm->vmcb01.ptr;
445 vmcb_clr_intercept(&vmcb->control, bit);
447 recalc_intercepts(svm);
450 static inline bool svm_is_intercept(struct vcpu_svm *svm, int bit)
452 return vmcb_is_intercept(&svm->vmcb->control, bit);
455 static inline bool vgif_enabled(struct vcpu_svm *svm)
457 return !!(svm->vmcb->control.int_ctl & V_GIF_ENABLE_MASK);
460 static inline void enable_gif(struct vcpu_svm *svm)
462 if (vgif_enabled(svm))
463 svm->vmcb->control.int_ctl |= V_GIF_MASK;
465 svm->vcpu.arch.hflags |= HF_GIF_MASK;
468 static inline void disable_gif(struct vcpu_svm *svm)
470 if (vgif_enabled(svm))
471 svm->vmcb->control.int_ctl &= ~V_GIF_MASK;
473 svm->vcpu.arch.hflags &= ~HF_GIF_MASK;
476 static inline bool gif_set(struct vcpu_svm *svm)
478 if (vgif_enabled(svm))
479 return !!(svm->vmcb->control.int_ctl & V_GIF_MASK);
481 return !!(svm->vcpu.arch.hflags & HF_GIF_MASK);
485 #define MSR_INVALID 0xffffffffU
487 extern bool dump_invalid_vmcb;
489 u32 svm_msrpm_offset(u32 msr);
490 u32 *svm_vcpu_alloc_msrpm(void);
491 void svm_vcpu_init_msrpm(struct kvm_vcpu *vcpu, u32 *msrpm);
492 void svm_vcpu_free_msrpm(u32 *msrpm);
494 int svm_set_efer(struct kvm_vcpu *vcpu, u64 efer);
495 void svm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0);
496 void svm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4);
497 void disable_nmi_singlestep(struct vcpu_svm *svm);
498 bool svm_smi_blocked(struct kvm_vcpu *vcpu);
499 bool svm_nmi_blocked(struct kvm_vcpu *vcpu);
500 bool svm_interrupt_blocked(struct kvm_vcpu *vcpu);
501 void svm_set_gif(struct vcpu_svm *svm, bool value);
502 int svm_invoke_exit_handler(struct kvm_vcpu *vcpu, u64 exit_code);
503 void set_msr_interception(struct kvm_vcpu *vcpu, u32 *msrpm, u32 msr,
504 int read, int write);
505 void svm_complete_interrupt_delivery(struct kvm_vcpu *vcpu, int delivery_mode,
506 int trig_mode, int vec);
510 #define NESTED_EXIT_HOST 0 /* Exit handled on host level */
511 #define NESTED_EXIT_DONE 1 /* Exit caused nested vmexit */
512 #define NESTED_EXIT_CONTINUE 2 /* Further checks needed */
514 static inline bool nested_svm_virtualize_tpr(struct kvm_vcpu *vcpu)
516 struct vcpu_svm *svm = to_svm(vcpu);
518 return is_guest_mode(vcpu) && (svm->nested.ctl.int_ctl & V_INTR_MASKING_MASK);
521 static inline bool nested_exit_on_smi(struct vcpu_svm *svm)
523 return vmcb12_is_intercept(&svm->nested.ctl, INTERCEPT_SMI);
526 static inline bool nested_exit_on_intr(struct vcpu_svm *svm)
528 return vmcb12_is_intercept(&svm->nested.ctl, INTERCEPT_INTR);
531 static inline bool nested_exit_on_nmi(struct vcpu_svm *svm)
533 return vmcb12_is_intercept(&svm->nested.ctl, INTERCEPT_NMI);
536 int enter_svm_guest_mode(struct kvm_vcpu *vcpu,
537 u64 vmcb_gpa, struct vmcb *vmcb12, bool from_vmrun);
538 void svm_leave_nested(struct kvm_vcpu *vcpu);
539 void svm_free_nested(struct vcpu_svm *svm);
540 int svm_allocate_nested(struct vcpu_svm *svm);
541 int nested_svm_vmrun(struct kvm_vcpu *vcpu);
542 void svm_copy_vmrun_state(struct vmcb_save_area *to_save,
543 struct vmcb_save_area *from_save);
544 void svm_copy_vmloadsave_state(struct vmcb *to_vmcb, struct vmcb *from_vmcb);
545 int nested_svm_vmexit(struct vcpu_svm *svm);
547 static inline int nested_svm_simple_vmexit(struct vcpu_svm *svm, u32 exit_code)
549 svm->vmcb->control.exit_code = exit_code;
550 svm->vmcb->control.exit_info_1 = 0;
551 svm->vmcb->control.exit_info_2 = 0;
552 return nested_svm_vmexit(svm);
555 int nested_svm_exit_handled(struct vcpu_svm *svm);
556 int nested_svm_check_permissions(struct kvm_vcpu *vcpu);
557 int nested_svm_check_exception(struct vcpu_svm *svm, unsigned nr,
558 bool has_error_code, u32 error_code);
559 int nested_svm_exit_special(struct vcpu_svm *svm);
560 void nested_svm_update_tsc_ratio_msr(struct kvm_vcpu *vcpu);
561 void svm_write_tsc_multiplier(struct kvm_vcpu *vcpu, u64 multiplier);
562 void nested_copy_vmcb_control_to_cache(struct vcpu_svm *svm,
563 struct vmcb_control_area *control);
564 void nested_copy_vmcb_save_to_cache(struct vcpu_svm *svm,
565 struct vmcb_save_area *save);
566 void nested_sync_control_from_vmcb02(struct vcpu_svm *svm);
567 void nested_vmcb02_compute_g_pat(struct vcpu_svm *svm);
568 void svm_switch_vmcb(struct vcpu_svm *svm, struct kvm_vmcb_info *target_vmcb);
570 extern struct kvm_x86_nested_ops svm_nested_ops;
574 int avic_ga_log_notifier(u32 ga_tag);
575 void avic_vm_destroy(struct kvm *kvm);
576 int avic_vm_init(struct kvm *kvm);
577 void avic_init_vmcb(struct vcpu_svm *svm);
578 int avic_incomplete_ipi_interception(struct kvm_vcpu *vcpu);
579 int avic_unaccelerated_access_interception(struct kvm_vcpu *vcpu);
580 int avic_init_vcpu(struct vcpu_svm *svm);
581 void __avic_vcpu_load(struct kvm_vcpu *vcpu, int cpu);
582 void __avic_vcpu_put(struct kvm_vcpu *vcpu);
583 void avic_apicv_post_state_restore(struct kvm_vcpu *vcpu);
584 void avic_set_virtual_apic_mode(struct kvm_vcpu *vcpu);
585 void avic_refresh_apicv_exec_ctrl(struct kvm_vcpu *vcpu);
586 bool avic_check_apicv_inhibit_reasons(enum kvm_apicv_inhibit reason);
587 void avic_hwapic_irr_update(struct kvm_vcpu *vcpu, int max_irr);
588 void avic_hwapic_isr_update(struct kvm_vcpu *vcpu, int max_isr);
589 bool avic_dy_apicv_has_pending_interrupt(struct kvm_vcpu *vcpu);
590 int avic_pi_update_irte(struct kvm *kvm, unsigned int host_irq,
591 uint32_t guest_irq, bool set);
592 void avic_vcpu_blocking(struct kvm_vcpu *vcpu);
593 void avic_vcpu_unblocking(struct kvm_vcpu *vcpu);
594 void avic_ring_doorbell(struct kvm_vcpu *vcpu);
598 #define GHCB_VERSION_MAX 1ULL
599 #define GHCB_VERSION_MIN 1ULL
602 extern unsigned int max_sev_asid;
604 void sev_vm_destroy(struct kvm *kvm);
605 int sev_mem_enc_ioctl(struct kvm *kvm, void __user *argp);
606 int sev_mem_enc_register_region(struct kvm *kvm,
607 struct kvm_enc_region *range);
608 int sev_mem_enc_unregister_region(struct kvm *kvm,
609 struct kvm_enc_region *range);
610 int sev_vm_copy_enc_context_from(struct kvm *kvm, unsigned int source_fd);
611 int sev_vm_move_enc_context_from(struct kvm *kvm, unsigned int source_fd);
612 void pre_sev_run(struct vcpu_svm *svm, int cpu);
613 void __init sev_set_cpu_caps(void);
614 void __init sev_hardware_setup(void);
615 void sev_hardware_unsetup(void);
616 int sev_cpu_init(struct svm_cpu_data *sd);
617 void sev_free_vcpu(struct kvm_vcpu *vcpu);
618 int sev_handle_vmgexit(struct kvm_vcpu *vcpu);
619 int sev_es_string_io(struct vcpu_svm *svm, int size, unsigned int port, int in);
620 void sev_es_init_vmcb(struct vcpu_svm *svm);
621 void sev_es_vcpu_reset(struct vcpu_svm *svm);
622 void sev_vcpu_deliver_sipi_vector(struct kvm_vcpu *vcpu, u8 vector);
623 void sev_es_prepare_switch_to_guest(struct vmcb_save_area *hostsa);
624 void sev_es_unmap_ghcb(struct vcpu_svm *svm);
628 void __svm_sev_es_vcpu_run(unsigned long vmcb_pa);
629 void __svm_vcpu_run(unsigned long vmcb_pa, unsigned long *regs);