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 #define __sme_page_pa(x) __sme_set(page_to_pfn(x) << PAGE_SHIFT)
27 #define IOPM_SIZE PAGE_SIZE * 3
28 #define MSRPM_SIZE PAGE_SIZE * 2
30 #define MAX_DIRECT_ACCESS_MSRS 20
31 #define MSRPM_OFFSETS 16
32 extern u32 msrpm_offsets[MSRPM_OFFSETS] __read_mostly;
33 extern bool npt_enabled;
34 extern bool intercept_smi;
38 * VMCB_ALL_CLEAN_MASK might also need to
39 * be updated if this enum is modified.
42 VMCB_INTERCEPTS, /* Intercept vectors, TSC offset,
44 VMCB_PERM_MAP, /* IOPM Base and MSRPM Base */
46 VMCB_INTR, /* int_ctl, int_vector */
47 VMCB_NPT, /* npt_en, nCR3, gPAT */
48 VMCB_CR, /* CR0, CR3, CR4, EFER */
49 VMCB_DR, /* DR6, DR7 */
50 VMCB_DT, /* GDT, IDT */
51 VMCB_SEG, /* CS, DS, SS, ES, CPL */
52 VMCB_CR2, /* CR2 only */
53 VMCB_LBR, /* DBGCTL, BR_FROM, BR_TO, LAST_EX_FROM, LAST_EX_TO */
54 VMCB_AVIC, /* AVIC APIC_BAR, AVIC APIC_BACKING_PAGE,
55 * AVIC PHYSICAL_TABLE pointer,
56 * AVIC LOGICAL_TABLE pointer
58 VMCB_SW = 31, /* Reserved for hypervisor/software use */
61 #define VMCB_ALL_CLEAN_MASK ( \
62 (1U << VMCB_INTERCEPTS) | (1U << VMCB_PERM_MAP) | \
63 (1U << VMCB_ASID) | (1U << VMCB_INTR) | \
64 (1U << VMCB_NPT) | (1U << VMCB_CR) | (1U << VMCB_DR) | \
65 (1U << VMCB_DT) | (1U << VMCB_SEG) | (1U << VMCB_CR2) | \
66 (1U << VMCB_LBR) | (1U << VMCB_AVIC) | \
69 /* TPR and CR2 are always written before VMRUN */
70 #define VMCB_ALWAYS_DIRTY_MASK ((1U << VMCB_INTR) | (1U << VMCB_CR2))
73 bool active; /* SEV enabled guest */
74 bool es_active; /* SEV-ES enabled guest */
75 unsigned int asid; /* ASID used for this guest */
76 unsigned int handle; /* SEV firmware handle */
77 int fd; /* SEV device fd */
78 unsigned long pages_locked; /* Number of pages locked */
79 struct list_head regions_list; /* List of registered regions */
80 u64 ap_jump_table; /* SEV-ES AP Jump Table address */
81 struct kvm *enc_context_owner; /* Owner of copied encryption context */
82 unsigned long num_mirrored_vms; /* Number of VMs sharing this ASID */
83 struct misc_cg *misc_cg; /* For misc cgroup accounting */
84 atomic_t migration_in_progress;
90 /* Struct members for AVIC */
92 struct page *avic_logical_id_table_page;
93 struct page *avic_physical_id_table_page;
94 struct hlist_node hnode;
96 struct kvm_sev_info sev_info;
101 struct kvm_vmcb_info {
105 uint64_t asid_generation;
108 struct vmcb_save_area_cached {
117 struct vmcb_ctrl_area_cached {
118 u32 intercepts[MAX_INTERCEPT];
119 u16 pause_filter_thresh;
120 u16 pause_filter_count;
134 u32 exit_int_info_err;
142 struct svm_nested_state {
143 struct kvm_vmcb_info vmcb02;
149 /* These are the merged vectors */
152 /* A VMRUN has started but has not yet been performed, so
153 * we cannot inject a nested vmexit yet. */
154 bool nested_run_pending;
156 /* cache for control fields of the guest */
157 struct vmcb_ctrl_area_cached ctl;
160 * Note: this struct is not kept up-to-date while L2 runs; it is only
161 * valid within nested_svm_vmrun.
163 struct vmcb_save_area_cached save;
168 struct vcpu_sev_es_state {
170 struct vmcb_save_area *vmsa;
172 struct kvm_host_map ghcb_map;
173 bool received_first_sipi;
175 /* SEV-ES scratch area support */
183 struct kvm_vcpu vcpu;
184 /* vmcb always points at current_vmcb->ptr, it's purely a shorthand. */
186 struct kvm_vmcb_info vmcb01;
187 struct kvm_vmcb_info *current_vmcb;
188 struct svm_cpu_data *svm_data;
202 * Contains guest-controlled bits of VIRT_SPEC_CTRL, which will be
203 * translated into the appropriate L2_CFG bits on the host to
204 * perform speculative control.
212 struct svm_nested_state nested;
215 u64 nmi_singlestep_guest_rflags;
217 unsigned int3_injected;
218 unsigned long int3_rip;
220 /* cached guest cpuid flags for faster access */
221 bool nrips_enabled : 1;
222 bool tsc_scaling_enabled : 1;
226 struct page *avic_backing_page;
227 u64 *avic_physical_id_cache;
230 * Per-vcpu list of struct amd_svm_iommu_ir:
231 * This is used mainly to store interrupt remapping information used
232 * when update the vcpu affinity. This avoids the need to scan for
233 * IRTE and try to match ga_tag in the IOMMU driver.
235 struct list_head ir_list;
236 spinlock_t ir_list_lock;
238 /* Save desired MSR intercept (read: pass-through) state */
240 DECLARE_BITMAP(read, MAX_DIRECT_ACCESS_MSRS);
241 DECLARE_BITMAP(write, MAX_DIRECT_ACCESS_MSRS);
242 } shadow_msr_intercept;
244 struct vcpu_sev_es_state sev_es;
246 bool guest_state_loaded;
249 struct svm_cpu_data {
256 struct kvm_ldttss_desc *tss_desc;
258 struct page *save_area;
259 struct vmcb *current_vmcb;
261 /* index = sev_asid, value = vmcb pointer */
262 struct vmcb **sev_vmcbs;
265 DECLARE_PER_CPU(struct svm_cpu_data *, svm_data);
267 void recalc_intercepts(struct vcpu_svm *svm);
269 static __always_inline struct kvm_svm *to_kvm_svm(struct kvm *kvm)
271 return container_of(kvm, struct kvm_svm, kvm);
274 static __always_inline bool sev_guest(struct kvm *kvm)
276 #ifdef CONFIG_KVM_AMD_SEV
277 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
285 static __always_inline bool sev_es_guest(struct kvm *kvm)
287 #ifdef CONFIG_KVM_AMD_SEV
288 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
290 return sev->es_active && !WARN_ON_ONCE(!sev->active);
296 static inline void vmcb_mark_all_dirty(struct vmcb *vmcb)
298 vmcb->control.clean = 0;
301 static inline void vmcb_mark_all_clean(struct vmcb *vmcb)
303 vmcb->control.clean = VMCB_ALL_CLEAN_MASK
304 & ~VMCB_ALWAYS_DIRTY_MASK;
307 static inline void vmcb_mark_dirty(struct vmcb *vmcb, int bit)
309 vmcb->control.clean &= ~(1 << bit);
312 static inline bool vmcb_is_dirty(struct vmcb *vmcb, int bit)
314 return !test_bit(bit, (unsigned long *)&vmcb->control.clean);
317 static __always_inline struct vcpu_svm *to_svm(struct kvm_vcpu *vcpu)
319 return container_of(vcpu, struct vcpu_svm, vcpu);
323 * Only the PDPTRs are loaded on demand into the shadow MMU. All other
324 * fields are synchronized in handle_exit, because accessing the VMCB is cheap.
326 * CR3 might be out of date in the VMCB but it is not marked dirty; instead,
327 * KVM_REQ_LOAD_MMU_PGD is always requested when the cached vcpu->arch.cr3
328 * is changed. svm_load_mmu_pgd() then syncs the new CR3 value into the VMCB.
330 #define SVM_REGS_LAZY_LOAD_SET (1 << VCPU_EXREG_PDPTR)
332 static inline void vmcb_set_intercept(struct vmcb_control_area *control, u32 bit)
334 WARN_ON_ONCE(bit >= 32 * MAX_INTERCEPT);
335 __set_bit(bit, (unsigned long *)&control->intercepts);
338 static inline void vmcb_clr_intercept(struct vmcb_control_area *control, u32 bit)
340 WARN_ON_ONCE(bit >= 32 * MAX_INTERCEPT);
341 __clear_bit(bit, (unsigned long *)&control->intercepts);
344 static inline bool vmcb_is_intercept(struct vmcb_control_area *control, u32 bit)
346 WARN_ON_ONCE(bit >= 32 * MAX_INTERCEPT);
347 return test_bit(bit, (unsigned long *)&control->intercepts);
350 static inline bool vmcb12_is_intercept(struct vmcb_ctrl_area_cached *control, u32 bit)
352 WARN_ON_ONCE(bit >= 32 * MAX_INTERCEPT);
353 return test_bit(bit, (unsigned long *)&control->intercepts);
356 static inline void set_dr_intercepts(struct vcpu_svm *svm)
358 struct vmcb *vmcb = svm->vmcb01.ptr;
360 if (!sev_es_guest(svm->vcpu.kvm)) {
361 vmcb_set_intercept(&vmcb->control, INTERCEPT_DR0_READ);
362 vmcb_set_intercept(&vmcb->control, INTERCEPT_DR1_READ);
363 vmcb_set_intercept(&vmcb->control, INTERCEPT_DR2_READ);
364 vmcb_set_intercept(&vmcb->control, INTERCEPT_DR3_READ);
365 vmcb_set_intercept(&vmcb->control, INTERCEPT_DR4_READ);
366 vmcb_set_intercept(&vmcb->control, INTERCEPT_DR5_READ);
367 vmcb_set_intercept(&vmcb->control, INTERCEPT_DR6_READ);
368 vmcb_set_intercept(&vmcb->control, INTERCEPT_DR0_WRITE);
369 vmcb_set_intercept(&vmcb->control, INTERCEPT_DR1_WRITE);
370 vmcb_set_intercept(&vmcb->control, INTERCEPT_DR2_WRITE);
371 vmcb_set_intercept(&vmcb->control, INTERCEPT_DR3_WRITE);
372 vmcb_set_intercept(&vmcb->control, INTERCEPT_DR4_WRITE);
373 vmcb_set_intercept(&vmcb->control, INTERCEPT_DR5_WRITE);
374 vmcb_set_intercept(&vmcb->control, INTERCEPT_DR6_WRITE);
377 vmcb_set_intercept(&vmcb->control, INTERCEPT_DR7_READ);
378 vmcb_set_intercept(&vmcb->control, INTERCEPT_DR7_WRITE);
380 recalc_intercepts(svm);
383 static inline void clr_dr_intercepts(struct vcpu_svm *svm)
385 struct vmcb *vmcb = svm->vmcb01.ptr;
387 vmcb->control.intercepts[INTERCEPT_DR] = 0;
389 /* DR7 access must remain intercepted for an SEV-ES guest */
390 if (sev_es_guest(svm->vcpu.kvm)) {
391 vmcb_set_intercept(&vmcb->control, INTERCEPT_DR7_READ);
392 vmcb_set_intercept(&vmcb->control, INTERCEPT_DR7_WRITE);
395 recalc_intercepts(svm);
398 static inline void set_exception_intercept(struct vcpu_svm *svm, u32 bit)
400 struct vmcb *vmcb = svm->vmcb01.ptr;
402 WARN_ON_ONCE(bit >= 32);
403 vmcb_set_intercept(&vmcb->control, INTERCEPT_EXCEPTION_OFFSET + bit);
405 recalc_intercepts(svm);
408 static inline void clr_exception_intercept(struct vcpu_svm *svm, u32 bit)
410 struct vmcb *vmcb = svm->vmcb01.ptr;
412 WARN_ON_ONCE(bit >= 32);
413 vmcb_clr_intercept(&vmcb->control, INTERCEPT_EXCEPTION_OFFSET + bit);
415 recalc_intercepts(svm);
418 static inline void svm_set_intercept(struct vcpu_svm *svm, int bit)
420 struct vmcb *vmcb = svm->vmcb01.ptr;
422 vmcb_set_intercept(&vmcb->control, bit);
424 recalc_intercepts(svm);
427 static inline void svm_clr_intercept(struct vcpu_svm *svm, int bit)
429 struct vmcb *vmcb = svm->vmcb01.ptr;
431 vmcb_clr_intercept(&vmcb->control, bit);
433 recalc_intercepts(svm);
436 static inline bool svm_is_intercept(struct vcpu_svm *svm, int bit)
438 return vmcb_is_intercept(&svm->vmcb->control, bit);
441 static inline bool vgif_enabled(struct vcpu_svm *svm)
443 return !!(svm->vmcb->control.int_ctl & V_GIF_ENABLE_MASK);
446 static inline void enable_gif(struct vcpu_svm *svm)
448 if (vgif_enabled(svm))
449 svm->vmcb->control.int_ctl |= V_GIF_MASK;
451 svm->vcpu.arch.hflags |= HF_GIF_MASK;
454 static inline void disable_gif(struct vcpu_svm *svm)
456 if (vgif_enabled(svm))
457 svm->vmcb->control.int_ctl &= ~V_GIF_MASK;
459 svm->vcpu.arch.hflags &= ~HF_GIF_MASK;
462 static inline bool gif_set(struct vcpu_svm *svm)
464 if (vgif_enabled(svm))
465 return !!(svm->vmcb->control.int_ctl & V_GIF_MASK);
467 return !!(svm->vcpu.arch.hflags & HF_GIF_MASK);
471 #define MSR_INVALID 0xffffffffU
473 extern bool dump_invalid_vmcb;
475 u32 svm_msrpm_offset(u32 msr);
476 u32 *svm_vcpu_alloc_msrpm(void);
477 void svm_vcpu_init_msrpm(struct kvm_vcpu *vcpu, u32 *msrpm);
478 void svm_vcpu_free_msrpm(u32 *msrpm);
480 int svm_set_efer(struct kvm_vcpu *vcpu, u64 efer);
481 void svm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0);
482 void svm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4);
483 void svm_flush_tlb(struct kvm_vcpu *vcpu);
484 void disable_nmi_singlestep(struct vcpu_svm *svm);
485 bool svm_smi_blocked(struct kvm_vcpu *vcpu);
486 bool svm_nmi_blocked(struct kvm_vcpu *vcpu);
487 bool svm_interrupt_blocked(struct kvm_vcpu *vcpu);
488 void svm_set_gif(struct vcpu_svm *svm, bool value);
489 int svm_invoke_exit_handler(struct kvm_vcpu *vcpu, u64 exit_code);
490 void set_msr_interception(struct kvm_vcpu *vcpu, u32 *msrpm, u32 msr,
491 int read, int write);
492 void svm_complete_interrupt_delivery(struct kvm_vcpu *vcpu, int delivery_mode,
493 int trig_mode, int vec);
497 #define NESTED_EXIT_HOST 0 /* Exit handled on host level */
498 #define NESTED_EXIT_DONE 1 /* Exit caused nested vmexit */
499 #define NESTED_EXIT_CONTINUE 2 /* Further checks needed */
501 static inline bool nested_svm_virtualize_tpr(struct kvm_vcpu *vcpu)
503 struct vcpu_svm *svm = to_svm(vcpu);
505 return is_guest_mode(vcpu) && (svm->nested.ctl.int_ctl & V_INTR_MASKING_MASK);
508 static inline bool nested_exit_on_smi(struct vcpu_svm *svm)
510 return vmcb12_is_intercept(&svm->nested.ctl, INTERCEPT_SMI);
513 static inline bool nested_exit_on_intr(struct vcpu_svm *svm)
515 return vmcb12_is_intercept(&svm->nested.ctl, INTERCEPT_INTR);
518 static inline bool nested_exit_on_nmi(struct vcpu_svm *svm)
520 return vmcb12_is_intercept(&svm->nested.ctl, INTERCEPT_NMI);
523 int enter_svm_guest_mode(struct kvm_vcpu *vcpu,
524 u64 vmcb_gpa, struct vmcb *vmcb12, bool from_vmrun);
525 void svm_leave_nested(struct kvm_vcpu *vcpu);
526 void svm_free_nested(struct vcpu_svm *svm);
527 int svm_allocate_nested(struct vcpu_svm *svm);
528 int nested_svm_vmrun(struct kvm_vcpu *vcpu);
529 void svm_copy_vmrun_state(struct vmcb_save_area *to_save,
530 struct vmcb_save_area *from_save);
531 void svm_copy_vmloadsave_state(struct vmcb *to_vmcb, struct vmcb *from_vmcb);
532 int nested_svm_vmexit(struct vcpu_svm *svm);
534 static inline int nested_svm_simple_vmexit(struct vcpu_svm *svm, u32 exit_code)
536 svm->vmcb->control.exit_code = exit_code;
537 svm->vmcb->control.exit_info_1 = 0;
538 svm->vmcb->control.exit_info_2 = 0;
539 return nested_svm_vmexit(svm);
542 int nested_svm_exit_handled(struct vcpu_svm *svm);
543 int nested_svm_check_permissions(struct kvm_vcpu *vcpu);
544 int nested_svm_check_exception(struct vcpu_svm *svm, unsigned nr,
545 bool has_error_code, u32 error_code);
546 int nested_svm_exit_special(struct vcpu_svm *svm);
547 void nested_svm_update_tsc_ratio_msr(struct kvm_vcpu *vcpu);
548 void svm_write_tsc_multiplier(struct kvm_vcpu *vcpu, u64 multiplier);
549 void nested_copy_vmcb_control_to_cache(struct vcpu_svm *svm,
550 struct vmcb_control_area *control);
551 void nested_copy_vmcb_save_to_cache(struct vcpu_svm *svm,
552 struct vmcb_save_area *save);
553 void nested_sync_control_from_vmcb02(struct vcpu_svm *svm);
554 void nested_vmcb02_compute_g_pat(struct vcpu_svm *svm);
555 void svm_switch_vmcb(struct vcpu_svm *svm, struct kvm_vmcb_info *target_vmcb);
557 extern struct kvm_x86_nested_ops svm_nested_ops;
561 int avic_ga_log_notifier(u32 ga_tag);
562 void avic_vm_destroy(struct kvm *kvm);
563 int avic_vm_init(struct kvm *kvm);
564 void avic_init_vmcb(struct vcpu_svm *svm);
565 int avic_incomplete_ipi_interception(struct kvm_vcpu *vcpu);
566 int avic_unaccelerated_access_interception(struct kvm_vcpu *vcpu);
567 int avic_init_vcpu(struct vcpu_svm *svm);
568 void avic_vcpu_load(struct kvm_vcpu *vcpu, int cpu);
569 void avic_vcpu_put(struct kvm_vcpu *vcpu);
570 void avic_post_state_restore(struct kvm_vcpu *vcpu);
571 void svm_set_virtual_apic_mode(struct kvm_vcpu *vcpu);
572 void svm_refresh_apicv_exec_ctrl(struct kvm_vcpu *vcpu);
573 bool svm_check_apicv_inhibit_reasons(ulong bit);
574 void svm_load_eoi_exitmap(struct kvm_vcpu *vcpu, u64 *eoi_exit_bitmap);
575 void svm_hwapic_irr_update(struct kvm_vcpu *vcpu, int max_irr);
576 void svm_hwapic_isr_update(struct kvm_vcpu *vcpu, int max_isr);
577 bool svm_dy_apicv_has_pending_interrupt(struct kvm_vcpu *vcpu);
578 int svm_update_pi_irte(struct kvm *kvm, unsigned int host_irq,
579 uint32_t guest_irq, bool set);
580 void avic_vcpu_blocking(struct kvm_vcpu *vcpu);
581 void avic_vcpu_unblocking(struct kvm_vcpu *vcpu);
582 void avic_ring_doorbell(struct kvm_vcpu *vcpu);
586 #define GHCB_VERSION_MAX 1ULL
587 #define GHCB_VERSION_MIN 1ULL
590 extern unsigned int max_sev_asid;
592 void sev_vm_destroy(struct kvm *kvm);
593 int svm_mem_enc_op(struct kvm *kvm, void __user *argp);
594 int svm_register_enc_region(struct kvm *kvm,
595 struct kvm_enc_region *range);
596 int svm_unregister_enc_region(struct kvm *kvm,
597 struct kvm_enc_region *range);
598 int svm_vm_copy_asid_from(struct kvm *kvm, unsigned int source_fd);
599 int svm_vm_migrate_from(struct kvm *kvm, unsigned int source_fd);
600 void pre_sev_run(struct vcpu_svm *svm, int cpu);
601 void __init sev_set_cpu_caps(void);
602 void __init sev_hardware_setup(void);
603 void sev_hardware_teardown(void);
604 int sev_cpu_init(struct svm_cpu_data *sd);
605 void sev_free_vcpu(struct kvm_vcpu *vcpu);
606 int sev_handle_vmgexit(struct kvm_vcpu *vcpu);
607 int sev_es_string_io(struct vcpu_svm *svm, int size, unsigned int port, int in);
608 void sev_es_init_vmcb(struct vcpu_svm *svm);
609 void sev_es_vcpu_reset(struct vcpu_svm *svm);
610 void sev_vcpu_deliver_sipi_vector(struct kvm_vcpu *vcpu, u8 vector);
611 void sev_es_prepare_guest_switch(struct vcpu_svm *svm, unsigned int cpu);
612 void sev_es_unmap_ghcb(struct vcpu_svm *svm);
616 void __svm_sev_es_vcpu_run(unsigned long vmcb_pa);
617 void __svm_vcpu_run(unsigned long vmcb_pa, unsigned long *regs);