1 // SPDX-License-Identifier: GPL-2.0-only
3 * Kernel-based Virtual Machine driver for Linux
7 * Copyright 2010 Red Hat, Inc. and/or its affiliates.
9 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
11 #include <linux/kvm_types.h>
12 #include <linux/kvm_host.h>
13 #include <linux/kernel.h>
14 #include <linux/highmem.h>
15 #include <linux/psp.h>
16 #include <linux/psp-sev.h>
17 #include <linux/pagemap.h>
18 #include <linux/swap.h>
19 #include <linux/misc_cgroup.h>
20 #include <linux/processor.h>
21 #include <linux/trace_events.h>
24 #include <asm/trapnr.h>
25 #include <asm/fpu/xcr.h>
26 #include <asm/debugreg.h>
35 #ifndef CONFIG_KVM_AMD_SEV
37 * When this config is not defined, SEV feature is not supported and APIs in
38 * this file are not used but this file still gets compiled into the KVM AMD
41 * We will not have MISC_CG_RES_SEV and MISC_CG_RES_SEV_ES entries in the enum
42 * misc_res_type {} defined in linux/misc_cgroup.h.
44 * Below macros allow compilation to succeed.
46 #define MISC_CG_RES_SEV MISC_CG_RES_TYPES
47 #define MISC_CG_RES_SEV_ES MISC_CG_RES_TYPES
50 #ifdef CONFIG_KVM_AMD_SEV
51 /* enable/disable SEV support */
52 static bool sev_enabled = true;
53 module_param_named(sev, sev_enabled, bool, 0444);
55 /* enable/disable SEV-ES support */
56 static bool sev_es_enabled = true;
57 module_param_named(sev_es, sev_es_enabled, bool, 0444);
59 /* enable/disable SEV-ES DebugSwap support */
60 static bool sev_es_debug_swap_enabled = true;
61 module_param_named(debug_swap, sev_es_debug_swap_enabled, bool, 0444);
63 #define sev_enabled false
64 #define sev_es_enabled false
65 #define sev_es_debug_swap_enabled false
66 #endif /* CONFIG_KVM_AMD_SEV */
68 static u8 sev_enc_bit;
69 static DECLARE_RWSEM(sev_deactivate_lock);
70 static DEFINE_MUTEX(sev_bitmap_lock);
71 unsigned int max_sev_asid;
72 static unsigned int min_sev_asid;
73 static unsigned long sev_me_mask;
74 static unsigned int nr_asids;
75 static unsigned long *sev_asid_bitmap;
76 static unsigned long *sev_reclaim_asid_bitmap;
79 struct list_head list;
86 /* Called with the sev_bitmap_lock held, or on shutdown */
87 static int sev_flush_asids(int min_asid, int max_asid)
89 int ret, asid, error = 0;
91 /* Check if there are any ASIDs to reclaim before performing a flush */
92 asid = find_next_bit(sev_reclaim_asid_bitmap, nr_asids, min_asid);
97 * DEACTIVATE will clear the WBINVD indicator causing DF_FLUSH to fail,
98 * so it must be guarded.
100 down_write(&sev_deactivate_lock);
102 wbinvd_on_all_cpus();
103 ret = sev_guest_df_flush(&error);
105 up_write(&sev_deactivate_lock);
108 pr_err("SEV: DF_FLUSH failed, ret=%d, error=%#x\n", ret, error);
113 static inline bool is_mirroring_enc_context(struct kvm *kvm)
115 return !!to_kvm_svm(kvm)->sev_info.enc_context_owner;
118 /* Must be called with the sev_bitmap_lock held */
119 static bool __sev_recycle_asids(int min_asid, int max_asid)
121 if (sev_flush_asids(min_asid, max_asid))
124 /* The flush process will flush all reclaimable SEV and SEV-ES ASIDs */
125 bitmap_xor(sev_asid_bitmap, sev_asid_bitmap, sev_reclaim_asid_bitmap,
127 bitmap_zero(sev_reclaim_asid_bitmap, nr_asids);
132 static int sev_misc_cg_try_charge(struct kvm_sev_info *sev)
134 enum misc_res_type type = sev->es_active ? MISC_CG_RES_SEV_ES : MISC_CG_RES_SEV;
135 return misc_cg_try_charge(type, sev->misc_cg, 1);
138 static void sev_misc_cg_uncharge(struct kvm_sev_info *sev)
140 enum misc_res_type type = sev->es_active ? MISC_CG_RES_SEV_ES : MISC_CG_RES_SEV;
141 misc_cg_uncharge(type, sev->misc_cg, 1);
144 static int sev_asid_new(struct kvm_sev_info *sev)
146 int asid, min_asid, max_asid, ret;
149 WARN_ON(sev->misc_cg);
150 sev->misc_cg = get_current_misc_cg();
151 ret = sev_misc_cg_try_charge(sev);
153 put_misc_cg(sev->misc_cg);
158 mutex_lock(&sev_bitmap_lock);
161 * SEV-enabled guests must use asid from min_sev_asid to max_sev_asid.
162 * SEV-ES-enabled guest can use from 1 to min_sev_asid - 1.
164 min_asid = sev->es_active ? 1 : min_sev_asid;
165 max_asid = sev->es_active ? min_sev_asid - 1 : max_sev_asid;
167 asid = find_next_zero_bit(sev_asid_bitmap, max_asid + 1, min_asid);
168 if (asid > max_asid) {
169 if (retry && __sev_recycle_asids(min_asid, max_asid)) {
173 mutex_unlock(&sev_bitmap_lock);
178 __set_bit(asid, sev_asid_bitmap);
180 mutex_unlock(&sev_bitmap_lock);
184 sev_misc_cg_uncharge(sev);
185 put_misc_cg(sev->misc_cg);
190 static int sev_get_asid(struct kvm *kvm)
192 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
197 static void sev_asid_free(struct kvm_sev_info *sev)
199 struct svm_cpu_data *sd;
202 mutex_lock(&sev_bitmap_lock);
204 __set_bit(sev->asid, sev_reclaim_asid_bitmap);
206 for_each_possible_cpu(cpu) {
207 sd = per_cpu_ptr(&svm_data, cpu);
208 sd->sev_vmcbs[sev->asid] = NULL;
211 mutex_unlock(&sev_bitmap_lock);
213 sev_misc_cg_uncharge(sev);
214 put_misc_cg(sev->misc_cg);
218 static void sev_decommission(unsigned int handle)
220 struct sev_data_decommission decommission;
225 decommission.handle = handle;
226 sev_guest_decommission(&decommission, NULL);
229 static void sev_unbind_asid(struct kvm *kvm, unsigned int handle)
231 struct sev_data_deactivate deactivate;
236 deactivate.handle = handle;
238 /* Guard DEACTIVATE against WBINVD/DF_FLUSH used in ASID recycling */
239 down_read(&sev_deactivate_lock);
240 sev_guest_deactivate(&deactivate, NULL);
241 up_read(&sev_deactivate_lock);
243 sev_decommission(handle);
246 static int sev_guest_init(struct kvm *kvm, struct kvm_sev_cmd *argp)
248 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
251 if (kvm->created_vcpus)
255 if (unlikely(sev->active))
259 sev->es_active = argp->id == KVM_SEV_ES_INIT;
260 asid = sev_asid_new(sev);
265 ret = sev_platform_init(&argp->error);
269 INIT_LIST_HEAD(&sev->regions_list);
270 INIT_LIST_HEAD(&sev->mirror_vms);
272 kvm_set_apicv_inhibit(kvm, APICV_INHIBIT_REASON_SEV);
280 sev->es_active = false;
285 static int sev_bind_asid(struct kvm *kvm, unsigned int handle, int *error)
287 struct sev_data_activate activate;
288 int asid = sev_get_asid(kvm);
291 /* activate ASID on the given handle */
292 activate.handle = handle;
293 activate.asid = asid;
294 ret = sev_guest_activate(&activate, error);
299 static int __sev_issue_cmd(int fd, int id, void *data, int *error)
308 ret = sev_issue_cmd_external_user(f.file, id, data, error);
314 static int sev_issue_cmd(struct kvm *kvm, int id, void *data, int *error)
316 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
318 return __sev_issue_cmd(sev->fd, id, data, error);
321 static int sev_launch_start(struct kvm *kvm, struct kvm_sev_cmd *argp)
323 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
324 struct sev_data_launch_start start;
325 struct kvm_sev_launch_start params;
326 void *dh_blob, *session_blob;
327 int *error = &argp->error;
333 if (copy_from_user(¶ms, (void __user *)(uintptr_t)argp->data, sizeof(params)))
336 memset(&start, 0, sizeof(start));
339 if (params.dh_uaddr) {
340 dh_blob = psp_copy_user_blob(params.dh_uaddr, params.dh_len);
342 return PTR_ERR(dh_blob);
344 start.dh_cert_address = __sme_set(__pa(dh_blob));
345 start.dh_cert_len = params.dh_len;
349 if (params.session_uaddr) {
350 session_blob = psp_copy_user_blob(params.session_uaddr, params.session_len);
351 if (IS_ERR(session_blob)) {
352 ret = PTR_ERR(session_blob);
356 start.session_address = __sme_set(__pa(session_blob));
357 start.session_len = params.session_len;
360 start.handle = params.handle;
361 start.policy = params.policy;
363 /* create memory encryption context */
364 ret = __sev_issue_cmd(argp->sev_fd, SEV_CMD_LAUNCH_START, &start, error);
368 /* Bind ASID to this guest */
369 ret = sev_bind_asid(kvm, start.handle, error);
371 sev_decommission(start.handle);
375 /* return handle to userspace */
376 params.handle = start.handle;
377 if (copy_to_user((void __user *)(uintptr_t)argp->data, ¶ms, sizeof(params))) {
378 sev_unbind_asid(kvm, start.handle);
383 sev->handle = start.handle;
384 sev->fd = argp->sev_fd;
393 static struct page **sev_pin_memory(struct kvm *kvm, unsigned long uaddr,
394 unsigned long ulen, unsigned long *n,
397 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
398 unsigned long npages, size;
400 unsigned long locked, lock_limit;
402 unsigned long first, last;
405 lockdep_assert_held(&kvm->lock);
407 if (ulen == 0 || uaddr + ulen < uaddr)
408 return ERR_PTR(-EINVAL);
410 /* Calculate number of pages. */
411 first = (uaddr & PAGE_MASK) >> PAGE_SHIFT;
412 last = ((uaddr + ulen - 1) & PAGE_MASK) >> PAGE_SHIFT;
413 npages = (last - first + 1);
415 locked = sev->pages_locked + npages;
416 lock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
417 if (locked > lock_limit && !capable(CAP_IPC_LOCK)) {
418 pr_err("SEV: %lu locked pages exceed the lock limit of %lu.\n", locked, lock_limit);
419 return ERR_PTR(-ENOMEM);
422 if (WARN_ON_ONCE(npages > INT_MAX))
423 return ERR_PTR(-EINVAL);
425 /* Avoid using vmalloc for smaller buffers. */
426 size = npages * sizeof(struct page *);
427 if (size > PAGE_SIZE)
428 pages = __vmalloc(size, GFP_KERNEL_ACCOUNT | __GFP_ZERO);
430 pages = kmalloc(size, GFP_KERNEL_ACCOUNT);
433 return ERR_PTR(-ENOMEM);
435 /* Pin the user virtual address. */
436 npinned = pin_user_pages_fast(uaddr, npages, write ? FOLL_WRITE : 0, pages);
437 if (npinned != npages) {
438 pr_err("SEV: Failure locking %lu pages.\n", npages);
444 sev->pages_locked = locked;
450 unpin_user_pages(pages, npinned);
456 static void sev_unpin_memory(struct kvm *kvm, struct page **pages,
457 unsigned long npages)
459 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
461 unpin_user_pages(pages, npages);
463 sev->pages_locked -= npages;
466 static void sev_clflush_pages(struct page *pages[], unsigned long npages)
468 uint8_t *page_virtual;
471 if (this_cpu_has(X86_FEATURE_SME_COHERENT) || npages == 0 ||
475 for (i = 0; i < npages; i++) {
476 page_virtual = kmap_local_page(pages[i]);
477 clflush_cache_range(page_virtual, PAGE_SIZE);
478 kunmap_local(page_virtual);
483 static unsigned long get_num_contig_pages(unsigned long idx,
484 struct page **inpages, unsigned long npages)
486 unsigned long paddr, next_paddr;
487 unsigned long i = idx + 1, pages = 1;
489 /* find the number of contiguous pages starting from idx */
490 paddr = __sme_page_pa(inpages[idx]);
492 next_paddr = __sme_page_pa(inpages[i++]);
493 if ((paddr + PAGE_SIZE) == next_paddr) {
504 static int sev_launch_update_data(struct kvm *kvm, struct kvm_sev_cmd *argp)
506 unsigned long vaddr, vaddr_end, next_vaddr, npages, pages, size, i;
507 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
508 struct kvm_sev_launch_update_data params;
509 struct sev_data_launch_update_data data;
510 struct page **inpages;
516 if (copy_from_user(¶ms, (void __user *)(uintptr_t)argp->data, sizeof(params)))
519 vaddr = params.uaddr;
521 vaddr_end = vaddr + size;
523 /* Lock the user memory. */
524 inpages = sev_pin_memory(kvm, vaddr, size, &npages, 1);
526 return PTR_ERR(inpages);
529 * Flush (on non-coherent CPUs) before LAUNCH_UPDATE encrypts pages in
530 * place; the cache may contain the data that was written unencrypted.
532 sev_clflush_pages(inpages, npages);
535 data.handle = sev->handle;
537 for (i = 0; vaddr < vaddr_end; vaddr = next_vaddr, i += pages) {
541 * If the user buffer is not page-aligned, calculate the offset
544 offset = vaddr & (PAGE_SIZE - 1);
546 /* Calculate the number of pages that can be encrypted in one go. */
547 pages = get_num_contig_pages(i, inpages, npages);
549 len = min_t(size_t, ((pages * PAGE_SIZE) - offset), size);
552 data.address = __sme_page_pa(inpages[i]) + offset;
553 ret = sev_issue_cmd(kvm, SEV_CMD_LAUNCH_UPDATE_DATA, &data, &argp->error);
558 next_vaddr = vaddr + len;
562 /* content of memory is updated, mark pages dirty */
563 for (i = 0; i < npages; i++) {
564 set_page_dirty_lock(inpages[i]);
565 mark_page_accessed(inpages[i]);
567 /* unlock the user pages */
568 sev_unpin_memory(kvm, inpages, npages);
572 static int sev_es_sync_vmsa(struct vcpu_svm *svm)
574 struct sev_es_save_area *save = svm->sev_es.vmsa;
576 /* Check some debug related fields before encrypting the VMSA */
577 if (svm->vcpu.guest_debug || (svm->vmcb->save.dr7 & ~DR7_FIXED_1))
581 * SEV-ES will use a VMSA that is pointed to by the VMCB, not
582 * the traditional VMSA that is part of the VMCB. Copy the
583 * traditional VMSA as it has been built so far (in prep
584 * for LAUNCH_UPDATE_VMSA) to be the initial SEV-ES state.
586 memcpy(save, &svm->vmcb->save, sizeof(svm->vmcb->save));
588 /* Sync registgers */
589 save->rax = svm->vcpu.arch.regs[VCPU_REGS_RAX];
590 save->rbx = svm->vcpu.arch.regs[VCPU_REGS_RBX];
591 save->rcx = svm->vcpu.arch.regs[VCPU_REGS_RCX];
592 save->rdx = svm->vcpu.arch.regs[VCPU_REGS_RDX];
593 save->rsp = svm->vcpu.arch.regs[VCPU_REGS_RSP];
594 save->rbp = svm->vcpu.arch.regs[VCPU_REGS_RBP];
595 save->rsi = svm->vcpu.arch.regs[VCPU_REGS_RSI];
596 save->rdi = svm->vcpu.arch.regs[VCPU_REGS_RDI];
598 save->r8 = svm->vcpu.arch.regs[VCPU_REGS_R8];
599 save->r9 = svm->vcpu.arch.regs[VCPU_REGS_R9];
600 save->r10 = svm->vcpu.arch.regs[VCPU_REGS_R10];
601 save->r11 = svm->vcpu.arch.regs[VCPU_REGS_R11];
602 save->r12 = svm->vcpu.arch.regs[VCPU_REGS_R12];
603 save->r13 = svm->vcpu.arch.regs[VCPU_REGS_R13];
604 save->r14 = svm->vcpu.arch.regs[VCPU_REGS_R14];
605 save->r15 = svm->vcpu.arch.regs[VCPU_REGS_R15];
607 save->rip = svm->vcpu.arch.regs[VCPU_REGS_RIP];
609 /* Sync some non-GPR registers before encrypting */
610 save->xcr0 = svm->vcpu.arch.xcr0;
611 save->pkru = svm->vcpu.arch.pkru;
612 save->xss = svm->vcpu.arch.ia32_xss;
613 save->dr6 = svm->vcpu.arch.dr6;
615 if (sev_es_debug_swap_enabled)
616 save->sev_features |= SVM_SEV_FEAT_DEBUG_SWAP;
618 pr_debug("Virtual Machine Save Area (VMSA):\n");
619 print_hex_dump_debug("", DUMP_PREFIX_NONE, 16, 1, save, sizeof(*save), false);
624 static int __sev_launch_update_vmsa(struct kvm *kvm, struct kvm_vcpu *vcpu,
627 struct sev_data_launch_update_vmsa vmsa;
628 struct vcpu_svm *svm = to_svm(vcpu);
631 if (vcpu->guest_debug) {
632 pr_warn_once("KVM_SET_GUEST_DEBUG for SEV-ES guest is not supported");
636 /* Perform some pre-encryption checks against the VMSA */
637 ret = sev_es_sync_vmsa(svm);
642 * The LAUNCH_UPDATE_VMSA command will perform in-place encryption of
643 * the VMSA memory content (i.e it will write the same memory region
644 * with the guest's key), so invalidate it first.
646 clflush_cache_range(svm->sev_es.vmsa, PAGE_SIZE);
649 vmsa.handle = to_kvm_svm(kvm)->sev_info.handle;
650 vmsa.address = __sme_pa(svm->sev_es.vmsa);
651 vmsa.len = PAGE_SIZE;
652 ret = sev_issue_cmd(kvm, SEV_CMD_LAUNCH_UPDATE_VMSA, &vmsa, error);
656 vcpu->arch.guest_state_protected = true;
660 static int sev_launch_update_vmsa(struct kvm *kvm, struct kvm_sev_cmd *argp)
662 struct kvm_vcpu *vcpu;
666 if (!sev_es_guest(kvm))
669 kvm_for_each_vcpu(i, vcpu, kvm) {
670 ret = mutex_lock_killable(&vcpu->mutex);
674 ret = __sev_launch_update_vmsa(kvm, vcpu, &argp->error);
676 mutex_unlock(&vcpu->mutex);
684 static int sev_launch_measure(struct kvm *kvm, struct kvm_sev_cmd *argp)
686 void __user *measure = (void __user *)(uintptr_t)argp->data;
687 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
688 struct sev_data_launch_measure data;
689 struct kvm_sev_launch_measure params;
690 void __user *p = NULL;
697 if (copy_from_user(¶ms, measure, sizeof(params)))
700 memset(&data, 0, sizeof(data));
702 /* User wants to query the blob length */
706 p = (void __user *)(uintptr_t)params.uaddr;
708 if (params.len > SEV_FW_BLOB_MAX_SIZE)
711 blob = kzalloc(params.len, GFP_KERNEL_ACCOUNT);
715 data.address = __psp_pa(blob);
716 data.len = params.len;
720 data.handle = sev->handle;
721 ret = sev_issue_cmd(kvm, SEV_CMD_LAUNCH_MEASURE, &data, &argp->error);
724 * If we query the session length, FW responded with expected data.
733 if (copy_to_user(p, blob, params.len))
738 params.len = data.len;
739 if (copy_to_user(measure, ¶ms, sizeof(params)))
746 static int sev_launch_finish(struct kvm *kvm, struct kvm_sev_cmd *argp)
748 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
749 struct sev_data_launch_finish data;
754 data.handle = sev->handle;
755 return sev_issue_cmd(kvm, SEV_CMD_LAUNCH_FINISH, &data, &argp->error);
758 static int sev_guest_status(struct kvm *kvm, struct kvm_sev_cmd *argp)
760 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
761 struct kvm_sev_guest_status params;
762 struct sev_data_guest_status data;
768 memset(&data, 0, sizeof(data));
770 data.handle = sev->handle;
771 ret = sev_issue_cmd(kvm, SEV_CMD_GUEST_STATUS, &data, &argp->error);
775 params.policy = data.policy;
776 params.state = data.state;
777 params.handle = data.handle;
779 if (copy_to_user((void __user *)(uintptr_t)argp->data, ¶ms, sizeof(params)))
785 static int __sev_issue_dbg_cmd(struct kvm *kvm, unsigned long src,
786 unsigned long dst, int size,
787 int *error, bool enc)
789 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
790 struct sev_data_dbg data;
793 data.handle = sev->handle;
798 return sev_issue_cmd(kvm,
799 enc ? SEV_CMD_DBG_ENCRYPT : SEV_CMD_DBG_DECRYPT,
803 static int __sev_dbg_decrypt(struct kvm *kvm, unsigned long src_paddr,
804 unsigned long dst_paddr, int sz, int *err)
809 * Its safe to read more than we are asked, caller should ensure that
810 * destination has enough space.
812 offset = src_paddr & 15;
813 src_paddr = round_down(src_paddr, 16);
814 sz = round_up(sz + offset, 16);
816 return __sev_issue_dbg_cmd(kvm, src_paddr, dst_paddr, sz, err, false);
819 static int __sev_dbg_decrypt_user(struct kvm *kvm, unsigned long paddr,
820 void __user *dst_uaddr,
821 unsigned long dst_paddr,
824 struct page *tpage = NULL;
827 /* if inputs are not 16-byte then use intermediate buffer */
828 if (!IS_ALIGNED(dst_paddr, 16) ||
829 !IS_ALIGNED(paddr, 16) ||
830 !IS_ALIGNED(size, 16)) {
831 tpage = (void *)alloc_page(GFP_KERNEL_ACCOUNT | __GFP_ZERO);
835 dst_paddr = __sme_page_pa(tpage);
838 ret = __sev_dbg_decrypt(kvm, paddr, dst_paddr, size, err);
844 if (copy_to_user(dst_uaddr, page_address(tpage) + offset, size))
855 static int __sev_dbg_encrypt_user(struct kvm *kvm, unsigned long paddr,
857 unsigned long dst_paddr,
858 void __user *dst_vaddr,
859 int size, int *error)
861 struct page *src_tpage = NULL;
862 struct page *dst_tpage = NULL;
865 /* If source buffer is not aligned then use an intermediate buffer */
866 if (!IS_ALIGNED((unsigned long)vaddr, 16)) {
867 src_tpage = alloc_page(GFP_KERNEL_ACCOUNT);
871 if (copy_from_user(page_address(src_tpage), vaddr, size)) {
872 __free_page(src_tpage);
876 paddr = __sme_page_pa(src_tpage);
880 * If destination buffer or length is not aligned then do read-modify-write:
881 * - decrypt destination in an intermediate buffer
882 * - copy the source buffer in an intermediate buffer
883 * - use the intermediate buffer as source buffer
885 if (!IS_ALIGNED((unsigned long)dst_vaddr, 16) || !IS_ALIGNED(size, 16)) {
888 dst_tpage = alloc_page(GFP_KERNEL_ACCOUNT);
894 ret = __sev_dbg_decrypt(kvm, dst_paddr,
895 __sme_page_pa(dst_tpage), size, error);
900 * If source is kernel buffer then use memcpy() otherwise
903 dst_offset = dst_paddr & 15;
906 memcpy(page_address(dst_tpage) + dst_offset,
907 page_address(src_tpage), size);
909 if (copy_from_user(page_address(dst_tpage) + dst_offset,
916 paddr = __sme_page_pa(dst_tpage);
917 dst_paddr = round_down(dst_paddr, 16);
918 len = round_up(size, 16);
921 ret = __sev_issue_dbg_cmd(kvm, paddr, dst_paddr, len, error, true);
925 __free_page(src_tpage);
927 __free_page(dst_tpage);
931 static int sev_dbg_crypt(struct kvm *kvm, struct kvm_sev_cmd *argp, bool dec)
933 unsigned long vaddr, vaddr_end, next_vaddr;
934 unsigned long dst_vaddr;
935 struct page **src_p, **dst_p;
936 struct kvm_sev_dbg debug;
944 if (copy_from_user(&debug, (void __user *)(uintptr_t)argp->data, sizeof(debug)))
947 if (!debug.len || debug.src_uaddr + debug.len < debug.src_uaddr)
949 if (!debug.dst_uaddr)
952 vaddr = debug.src_uaddr;
954 vaddr_end = vaddr + size;
955 dst_vaddr = debug.dst_uaddr;
957 for (; vaddr < vaddr_end; vaddr = next_vaddr) {
958 int len, s_off, d_off;
960 /* lock userspace source and destination page */
961 src_p = sev_pin_memory(kvm, vaddr & PAGE_MASK, PAGE_SIZE, &n, 0);
963 return PTR_ERR(src_p);
965 dst_p = sev_pin_memory(kvm, dst_vaddr & PAGE_MASK, PAGE_SIZE, &n, 1);
967 sev_unpin_memory(kvm, src_p, n);
968 return PTR_ERR(dst_p);
972 * Flush (on non-coherent CPUs) before DBG_{DE,EN}CRYPT read or modify
973 * the pages; flush the destination too so that future accesses do not
976 sev_clflush_pages(src_p, 1);
977 sev_clflush_pages(dst_p, 1);
980 * Since user buffer may not be page aligned, calculate the
981 * offset within the page.
983 s_off = vaddr & ~PAGE_MASK;
984 d_off = dst_vaddr & ~PAGE_MASK;
985 len = min_t(size_t, (PAGE_SIZE - s_off), size);
988 ret = __sev_dbg_decrypt_user(kvm,
989 __sme_page_pa(src_p[0]) + s_off,
990 (void __user *)dst_vaddr,
991 __sme_page_pa(dst_p[0]) + d_off,
994 ret = __sev_dbg_encrypt_user(kvm,
995 __sme_page_pa(src_p[0]) + s_off,
996 (void __user *)vaddr,
997 __sme_page_pa(dst_p[0]) + d_off,
998 (void __user *)dst_vaddr,
1001 sev_unpin_memory(kvm, src_p, n);
1002 sev_unpin_memory(kvm, dst_p, n);
1007 next_vaddr = vaddr + len;
1008 dst_vaddr = dst_vaddr + len;
1015 static int sev_launch_secret(struct kvm *kvm, struct kvm_sev_cmd *argp)
1017 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
1018 struct sev_data_launch_secret data;
1019 struct kvm_sev_launch_secret params;
1020 struct page **pages;
1025 if (!sev_guest(kvm))
1028 if (copy_from_user(¶ms, (void __user *)(uintptr_t)argp->data, sizeof(params)))
1031 pages = sev_pin_memory(kvm, params.guest_uaddr, params.guest_len, &n, 1);
1033 return PTR_ERR(pages);
1036 * Flush (on non-coherent CPUs) before LAUNCH_SECRET encrypts pages in
1037 * place; the cache may contain the data that was written unencrypted.
1039 sev_clflush_pages(pages, n);
1042 * The secret must be copied into contiguous memory region, lets verify
1043 * that userspace memory pages are contiguous before we issue command.
1045 if (get_num_contig_pages(0, pages, n) != n) {
1047 goto e_unpin_memory;
1050 memset(&data, 0, sizeof(data));
1052 offset = params.guest_uaddr & (PAGE_SIZE - 1);
1053 data.guest_address = __sme_page_pa(pages[0]) + offset;
1054 data.guest_len = params.guest_len;
1056 blob = psp_copy_user_blob(params.trans_uaddr, params.trans_len);
1058 ret = PTR_ERR(blob);
1059 goto e_unpin_memory;
1062 data.trans_address = __psp_pa(blob);
1063 data.trans_len = params.trans_len;
1065 hdr = psp_copy_user_blob(params.hdr_uaddr, params.hdr_len);
1070 data.hdr_address = __psp_pa(hdr);
1071 data.hdr_len = params.hdr_len;
1073 data.handle = sev->handle;
1074 ret = sev_issue_cmd(kvm, SEV_CMD_LAUNCH_UPDATE_SECRET, &data, &argp->error);
1081 /* content of memory is updated, mark pages dirty */
1082 for (i = 0; i < n; i++) {
1083 set_page_dirty_lock(pages[i]);
1084 mark_page_accessed(pages[i]);
1086 sev_unpin_memory(kvm, pages, n);
1090 static int sev_get_attestation_report(struct kvm *kvm, struct kvm_sev_cmd *argp)
1092 void __user *report = (void __user *)(uintptr_t)argp->data;
1093 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
1094 struct sev_data_attestation_report data;
1095 struct kvm_sev_attestation_report params;
1100 if (!sev_guest(kvm))
1103 if (copy_from_user(¶ms, (void __user *)(uintptr_t)argp->data, sizeof(params)))
1106 memset(&data, 0, sizeof(data));
1108 /* User wants to query the blob length */
1112 p = (void __user *)(uintptr_t)params.uaddr;
1114 if (params.len > SEV_FW_BLOB_MAX_SIZE)
1117 blob = kzalloc(params.len, GFP_KERNEL_ACCOUNT);
1121 data.address = __psp_pa(blob);
1122 data.len = params.len;
1123 memcpy(data.mnonce, params.mnonce, sizeof(params.mnonce));
1126 data.handle = sev->handle;
1127 ret = sev_issue_cmd(kvm, SEV_CMD_ATTESTATION_REPORT, &data, &argp->error);
1129 * If we query the session length, FW responded with expected data.
1138 if (copy_to_user(p, blob, params.len))
1143 params.len = data.len;
1144 if (copy_to_user(report, ¶ms, sizeof(params)))
1151 /* Userspace wants to query session length. */
1153 __sev_send_start_query_session_length(struct kvm *kvm, struct kvm_sev_cmd *argp,
1154 struct kvm_sev_send_start *params)
1156 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
1157 struct sev_data_send_start data;
1160 memset(&data, 0, sizeof(data));
1161 data.handle = sev->handle;
1162 ret = sev_issue_cmd(kvm, SEV_CMD_SEND_START, &data, &argp->error);
1164 params->session_len = data.session_len;
1165 if (copy_to_user((void __user *)(uintptr_t)argp->data, params,
1166 sizeof(struct kvm_sev_send_start)))
1172 static int sev_send_start(struct kvm *kvm, struct kvm_sev_cmd *argp)
1174 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
1175 struct sev_data_send_start data;
1176 struct kvm_sev_send_start params;
1177 void *amd_certs, *session_data;
1178 void *pdh_cert, *plat_certs;
1181 if (!sev_guest(kvm))
1184 if (copy_from_user(¶ms, (void __user *)(uintptr_t)argp->data,
1185 sizeof(struct kvm_sev_send_start)))
1188 /* if session_len is zero, userspace wants to query the session length */
1189 if (!params.session_len)
1190 return __sev_send_start_query_session_length(kvm, argp,
1193 /* some sanity checks */
1194 if (!params.pdh_cert_uaddr || !params.pdh_cert_len ||
1195 !params.session_uaddr || params.session_len > SEV_FW_BLOB_MAX_SIZE)
1198 /* allocate the memory to hold the session data blob */
1199 session_data = kzalloc(params.session_len, GFP_KERNEL_ACCOUNT);
1203 /* copy the certificate blobs from userspace */
1204 pdh_cert = psp_copy_user_blob(params.pdh_cert_uaddr,
1205 params.pdh_cert_len);
1206 if (IS_ERR(pdh_cert)) {
1207 ret = PTR_ERR(pdh_cert);
1208 goto e_free_session;
1211 plat_certs = psp_copy_user_blob(params.plat_certs_uaddr,
1212 params.plat_certs_len);
1213 if (IS_ERR(plat_certs)) {
1214 ret = PTR_ERR(plat_certs);
1218 amd_certs = psp_copy_user_blob(params.amd_certs_uaddr,
1219 params.amd_certs_len);
1220 if (IS_ERR(amd_certs)) {
1221 ret = PTR_ERR(amd_certs);
1222 goto e_free_plat_cert;
1225 /* populate the FW SEND_START field with system physical address */
1226 memset(&data, 0, sizeof(data));
1227 data.pdh_cert_address = __psp_pa(pdh_cert);
1228 data.pdh_cert_len = params.pdh_cert_len;
1229 data.plat_certs_address = __psp_pa(plat_certs);
1230 data.plat_certs_len = params.plat_certs_len;
1231 data.amd_certs_address = __psp_pa(amd_certs);
1232 data.amd_certs_len = params.amd_certs_len;
1233 data.session_address = __psp_pa(session_data);
1234 data.session_len = params.session_len;
1235 data.handle = sev->handle;
1237 ret = sev_issue_cmd(kvm, SEV_CMD_SEND_START, &data, &argp->error);
1239 if (!ret && copy_to_user((void __user *)(uintptr_t)params.session_uaddr,
1240 session_data, params.session_len)) {
1242 goto e_free_amd_cert;
1245 params.policy = data.policy;
1246 params.session_len = data.session_len;
1247 if (copy_to_user((void __user *)(uintptr_t)argp->data, ¶ms,
1248 sizeof(struct kvm_sev_send_start)))
1258 kfree(session_data);
1262 /* Userspace wants to query either header or trans length. */
1264 __sev_send_update_data_query_lengths(struct kvm *kvm, struct kvm_sev_cmd *argp,
1265 struct kvm_sev_send_update_data *params)
1267 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
1268 struct sev_data_send_update_data data;
1271 memset(&data, 0, sizeof(data));
1272 data.handle = sev->handle;
1273 ret = sev_issue_cmd(kvm, SEV_CMD_SEND_UPDATE_DATA, &data, &argp->error);
1275 params->hdr_len = data.hdr_len;
1276 params->trans_len = data.trans_len;
1278 if (copy_to_user((void __user *)(uintptr_t)argp->data, params,
1279 sizeof(struct kvm_sev_send_update_data)))
1285 static int sev_send_update_data(struct kvm *kvm, struct kvm_sev_cmd *argp)
1287 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
1288 struct sev_data_send_update_data data;
1289 struct kvm_sev_send_update_data params;
1290 void *hdr, *trans_data;
1291 struct page **guest_page;
1295 if (!sev_guest(kvm))
1298 if (copy_from_user(¶ms, (void __user *)(uintptr_t)argp->data,
1299 sizeof(struct kvm_sev_send_update_data)))
1302 /* userspace wants to query either header or trans length */
1303 if (!params.trans_len || !params.hdr_len)
1304 return __sev_send_update_data_query_lengths(kvm, argp, ¶ms);
1306 if (!params.trans_uaddr || !params.guest_uaddr ||
1307 !params.guest_len || !params.hdr_uaddr)
1310 /* Check if we are crossing the page boundary */
1311 offset = params.guest_uaddr & (PAGE_SIZE - 1);
1312 if (params.guest_len > PAGE_SIZE || (params.guest_len + offset) > PAGE_SIZE)
1315 /* Pin guest memory */
1316 guest_page = sev_pin_memory(kvm, params.guest_uaddr & PAGE_MASK,
1318 if (IS_ERR(guest_page))
1319 return PTR_ERR(guest_page);
1321 /* allocate memory for header and transport buffer */
1323 hdr = kzalloc(params.hdr_len, GFP_KERNEL_ACCOUNT);
1327 trans_data = kzalloc(params.trans_len, GFP_KERNEL_ACCOUNT);
1331 memset(&data, 0, sizeof(data));
1332 data.hdr_address = __psp_pa(hdr);
1333 data.hdr_len = params.hdr_len;
1334 data.trans_address = __psp_pa(trans_data);
1335 data.trans_len = params.trans_len;
1337 /* The SEND_UPDATE_DATA command requires C-bit to be always set. */
1338 data.guest_address = (page_to_pfn(guest_page[0]) << PAGE_SHIFT) + offset;
1339 data.guest_address |= sev_me_mask;
1340 data.guest_len = params.guest_len;
1341 data.handle = sev->handle;
1343 ret = sev_issue_cmd(kvm, SEV_CMD_SEND_UPDATE_DATA, &data, &argp->error);
1346 goto e_free_trans_data;
1348 /* copy transport buffer to user space */
1349 if (copy_to_user((void __user *)(uintptr_t)params.trans_uaddr,
1350 trans_data, params.trans_len)) {
1352 goto e_free_trans_data;
1355 /* Copy packet header to userspace. */
1356 if (copy_to_user((void __user *)(uintptr_t)params.hdr_uaddr, hdr,
1365 sev_unpin_memory(kvm, guest_page, n);
1370 static int sev_send_finish(struct kvm *kvm, struct kvm_sev_cmd *argp)
1372 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
1373 struct sev_data_send_finish data;
1375 if (!sev_guest(kvm))
1378 data.handle = sev->handle;
1379 return sev_issue_cmd(kvm, SEV_CMD_SEND_FINISH, &data, &argp->error);
1382 static int sev_send_cancel(struct kvm *kvm, struct kvm_sev_cmd *argp)
1384 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
1385 struct sev_data_send_cancel data;
1387 if (!sev_guest(kvm))
1390 data.handle = sev->handle;
1391 return sev_issue_cmd(kvm, SEV_CMD_SEND_CANCEL, &data, &argp->error);
1394 static int sev_receive_start(struct kvm *kvm, struct kvm_sev_cmd *argp)
1396 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
1397 struct sev_data_receive_start start;
1398 struct kvm_sev_receive_start params;
1399 int *error = &argp->error;
1404 if (!sev_guest(kvm))
1407 /* Get parameter from the userspace */
1408 if (copy_from_user(¶ms, (void __user *)(uintptr_t)argp->data,
1409 sizeof(struct kvm_sev_receive_start)))
1412 /* some sanity checks */
1413 if (!params.pdh_uaddr || !params.pdh_len ||
1414 !params.session_uaddr || !params.session_len)
1417 pdh_data = psp_copy_user_blob(params.pdh_uaddr, params.pdh_len);
1418 if (IS_ERR(pdh_data))
1419 return PTR_ERR(pdh_data);
1421 session_data = psp_copy_user_blob(params.session_uaddr,
1422 params.session_len);
1423 if (IS_ERR(session_data)) {
1424 ret = PTR_ERR(session_data);
1428 memset(&start, 0, sizeof(start));
1429 start.handle = params.handle;
1430 start.policy = params.policy;
1431 start.pdh_cert_address = __psp_pa(pdh_data);
1432 start.pdh_cert_len = params.pdh_len;
1433 start.session_address = __psp_pa(session_data);
1434 start.session_len = params.session_len;
1436 /* create memory encryption context */
1437 ret = __sev_issue_cmd(argp->sev_fd, SEV_CMD_RECEIVE_START, &start,
1440 goto e_free_session;
1442 /* Bind ASID to this guest */
1443 ret = sev_bind_asid(kvm, start.handle, error);
1445 sev_decommission(start.handle);
1446 goto e_free_session;
1449 params.handle = start.handle;
1450 if (copy_to_user((void __user *)(uintptr_t)argp->data,
1451 ¶ms, sizeof(struct kvm_sev_receive_start))) {
1453 sev_unbind_asid(kvm, start.handle);
1454 goto e_free_session;
1457 sev->handle = start.handle;
1458 sev->fd = argp->sev_fd;
1461 kfree(session_data);
1468 static int sev_receive_update_data(struct kvm *kvm, struct kvm_sev_cmd *argp)
1470 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
1471 struct kvm_sev_receive_update_data params;
1472 struct sev_data_receive_update_data data;
1473 void *hdr = NULL, *trans = NULL;
1474 struct page **guest_page;
1478 if (!sev_guest(kvm))
1481 if (copy_from_user(¶ms, (void __user *)(uintptr_t)argp->data,
1482 sizeof(struct kvm_sev_receive_update_data)))
1485 if (!params.hdr_uaddr || !params.hdr_len ||
1486 !params.guest_uaddr || !params.guest_len ||
1487 !params.trans_uaddr || !params.trans_len)
1490 /* Check if we are crossing the page boundary */
1491 offset = params.guest_uaddr & (PAGE_SIZE - 1);
1492 if (params.guest_len > PAGE_SIZE || (params.guest_len + offset) > PAGE_SIZE)
1495 hdr = psp_copy_user_blob(params.hdr_uaddr, params.hdr_len);
1497 return PTR_ERR(hdr);
1499 trans = psp_copy_user_blob(params.trans_uaddr, params.trans_len);
1500 if (IS_ERR(trans)) {
1501 ret = PTR_ERR(trans);
1505 memset(&data, 0, sizeof(data));
1506 data.hdr_address = __psp_pa(hdr);
1507 data.hdr_len = params.hdr_len;
1508 data.trans_address = __psp_pa(trans);
1509 data.trans_len = params.trans_len;
1511 /* Pin guest memory */
1512 guest_page = sev_pin_memory(kvm, params.guest_uaddr & PAGE_MASK,
1514 if (IS_ERR(guest_page)) {
1515 ret = PTR_ERR(guest_page);
1520 * Flush (on non-coherent CPUs) before RECEIVE_UPDATE_DATA, the PSP
1521 * encrypts the written data with the guest's key, and the cache may
1522 * contain dirty, unencrypted data.
1524 sev_clflush_pages(guest_page, n);
1526 /* The RECEIVE_UPDATE_DATA command requires C-bit to be always set. */
1527 data.guest_address = (page_to_pfn(guest_page[0]) << PAGE_SHIFT) + offset;
1528 data.guest_address |= sev_me_mask;
1529 data.guest_len = params.guest_len;
1530 data.handle = sev->handle;
1532 ret = sev_issue_cmd(kvm, SEV_CMD_RECEIVE_UPDATE_DATA, &data,
1535 sev_unpin_memory(kvm, guest_page, n);
1545 static int sev_receive_finish(struct kvm *kvm, struct kvm_sev_cmd *argp)
1547 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
1548 struct sev_data_receive_finish data;
1550 if (!sev_guest(kvm))
1553 data.handle = sev->handle;
1554 return sev_issue_cmd(kvm, SEV_CMD_RECEIVE_FINISH, &data, &argp->error);
1557 static bool is_cmd_allowed_from_mirror(u32 cmd_id)
1560 * Allow mirrors VM to call KVM_SEV_LAUNCH_UPDATE_VMSA to enable SEV-ES
1561 * active mirror VMs. Also allow the debugging and status commands.
1563 if (cmd_id == KVM_SEV_LAUNCH_UPDATE_VMSA ||
1564 cmd_id == KVM_SEV_GUEST_STATUS || cmd_id == KVM_SEV_DBG_DECRYPT ||
1565 cmd_id == KVM_SEV_DBG_ENCRYPT)
1571 static int sev_lock_two_vms(struct kvm *dst_kvm, struct kvm *src_kvm)
1573 struct kvm_sev_info *dst_sev = &to_kvm_svm(dst_kvm)->sev_info;
1574 struct kvm_sev_info *src_sev = &to_kvm_svm(src_kvm)->sev_info;
1577 if (dst_kvm == src_kvm)
1581 * Bail if these VMs are already involved in a migration to avoid
1582 * deadlock between two VMs trying to migrate to/from each other.
1584 if (atomic_cmpxchg_acquire(&dst_sev->migration_in_progress, 0, 1))
1587 if (atomic_cmpxchg_acquire(&src_sev->migration_in_progress, 0, 1))
1591 if (mutex_lock_killable(&dst_kvm->lock))
1593 if (mutex_lock_killable_nested(&src_kvm->lock, SINGLE_DEPTH_NESTING))
1598 mutex_unlock(&dst_kvm->lock);
1600 atomic_set_release(&src_sev->migration_in_progress, 0);
1602 atomic_set_release(&dst_sev->migration_in_progress, 0);
1606 static void sev_unlock_two_vms(struct kvm *dst_kvm, struct kvm *src_kvm)
1608 struct kvm_sev_info *dst_sev = &to_kvm_svm(dst_kvm)->sev_info;
1609 struct kvm_sev_info *src_sev = &to_kvm_svm(src_kvm)->sev_info;
1611 mutex_unlock(&dst_kvm->lock);
1612 mutex_unlock(&src_kvm->lock);
1613 atomic_set_release(&dst_sev->migration_in_progress, 0);
1614 atomic_set_release(&src_sev->migration_in_progress, 0);
1617 /* vCPU mutex subclasses. */
1618 enum sev_migration_role {
1619 SEV_MIGRATION_SOURCE = 0,
1620 SEV_MIGRATION_TARGET,
1621 SEV_NR_MIGRATION_ROLES,
1624 static int sev_lock_vcpus_for_migration(struct kvm *kvm,
1625 enum sev_migration_role role)
1627 struct kvm_vcpu *vcpu;
1630 kvm_for_each_vcpu(i, vcpu, kvm) {
1631 if (mutex_lock_killable_nested(&vcpu->mutex, role))
1634 #ifdef CONFIG_PROVE_LOCKING
1637 * Reset the role to one that avoids colliding with
1638 * the role used for the first vcpu mutex.
1640 role = SEV_NR_MIGRATION_ROLES;
1642 mutex_release(&vcpu->mutex.dep_map, _THIS_IP_);
1650 kvm_for_each_vcpu(j, vcpu, kvm) {
1654 #ifdef CONFIG_PROVE_LOCKING
1656 mutex_acquire(&vcpu->mutex.dep_map, role, 0, _THIS_IP_);
1659 mutex_unlock(&vcpu->mutex);
1664 static void sev_unlock_vcpus_for_migration(struct kvm *kvm)
1666 struct kvm_vcpu *vcpu;
1670 kvm_for_each_vcpu(i, vcpu, kvm) {
1674 mutex_acquire(&vcpu->mutex.dep_map,
1675 SEV_NR_MIGRATION_ROLES, 0, _THIS_IP_);
1677 mutex_unlock(&vcpu->mutex);
1681 static void sev_migrate_from(struct kvm *dst_kvm, struct kvm *src_kvm)
1683 struct kvm_sev_info *dst = &to_kvm_svm(dst_kvm)->sev_info;
1684 struct kvm_sev_info *src = &to_kvm_svm(src_kvm)->sev_info;
1685 struct kvm_vcpu *dst_vcpu, *src_vcpu;
1686 struct vcpu_svm *dst_svm, *src_svm;
1687 struct kvm_sev_info *mirror;
1691 dst->asid = src->asid;
1692 dst->handle = src->handle;
1693 dst->pages_locked = src->pages_locked;
1694 dst->enc_context_owner = src->enc_context_owner;
1695 dst->es_active = src->es_active;
1698 src->active = false;
1700 src->pages_locked = 0;
1701 src->enc_context_owner = NULL;
1702 src->es_active = false;
1704 list_cut_before(&dst->regions_list, &src->regions_list, &src->regions_list);
1707 * If this VM has mirrors, "transfer" each mirror's refcount of the
1708 * source to the destination (this KVM). The caller holds a reference
1709 * to the source, so there's no danger of use-after-free.
1711 list_cut_before(&dst->mirror_vms, &src->mirror_vms, &src->mirror_vms);
1712 list_for_each_entry(mirror, &dst->mirror_vms, mirror_entry) {
1713 kvm_get_kvm(dst_kvm);
1714 kvm_put_kvm(src_kvm);
1715 mirror->enc_context_owner = dst_kvm;
1719 * If this VM is a mirror, remove the old mirror from the owners list
1720 * and add the new mirror to the list.
1722 if (is_mirroring_enc_context(dst_kvm)) {
1723 struct kvm_sev_info *owner_sev_info =
1724 &to_kvm_svm(dst->enc_context_owner)->sev_info;
1726 list_del(&src->mirror_entry);
1727 list_add_tail(&dst->mirror_entry, &owner_sev_info->mirror_vms);
1730 kvm_for_each_vcpu(i, dst_vcpu, dst_kvm) {
1731 dst_svm = to_svm(dst_vcpu);
1733 sev_init_vmcb(dst_svm);
1735 if (!dst->es_active)
1739 * Note, the source is not required to have the same number of
1740 * vCPUs as the destination when migrating a vanilla SEV VM.
1742 src_vcpu = kvm_get_vcpu(src_kvm, i);
1743 src_svm = to_svm(src_vcpu);
1746 * Transfer VMSA and GHCB state to the destination. Nullify and
1747 * clear source fields as appropriate, the state now belongs to
1750 memcpy(&dst_svm->sev_es, &src_svm->sev_es, sizeof(src_svm->sev_es));
1751 dst_svm->vmcb->control.ghcb_gpa = src_svm->vmcb->control.ghcb_gpa;
1752 dst_svm->vmcb->control.vmsa_pa = src_svm->vmcb->control.vmsa_pa;
1753 dst_vcpu->arch.guest_state_protected = true;
1755 memset(&src_svm->sev_es, 0, sizeof(src_svm->sev_es));
1756 src_svm->vmcb->control.ghcb_gpa = INVALID_PAGE;
1757 src_svm->vmcb->control.vmsa_pa = INVALID_PAGE;
1758 src_vcpu->arch.guest_state_protected = false;
1762 static int sev_check_source_vcpus(struct kvm *dst, struct kvm *src)
1764 struct kvm_vcpu *src_vcpu;
1767 if (!sev_es_guest(src))
1770 if (atomic_read(&src->online_vcpus) != atomic_read(&dst->online_vcpus))
1773 kvm_for_each_vcpu(i, src_vcpu, src) {
1774 if (!src_vcpu->arch.guest_state_protected)
1781 int sev_vm_move_enc_context_from(struct kvm *kvm, unsigned int source_fd)
1783 struct kvm_sev_info *dst_sev = &to_kvm_svm(kvm)->sev_info;
1784 struct kvm_sev_info *src_sev, *cg_cleanup_sev;
1785 struct fd f = fdget(source_fd);
1786 struct kvm *source_kvm;
1787 bool charged = false;
1793 if (!file_is_kvm(f.file)) {
1798 source_kvm = f.file->private_data;
1799 ret = sev_lock_two_vms(kvm, source_kvm);
1803 if (sev_guest(kvm) || !sev_guest(source_kvm)) {
1808 src_sev = &to_kvm_svm(source_kvm)->sev_info;
1810 dst_sev->misc_cg = get_current_misc_cg();
1811 cg_cleanup_sev = dst_sev;
1812 if (dst_sev->misc_cg != src_sev->misc_cg) {
1813 ret = sev_misc_cg_try_charge(dst_sev);
1815 goto out_dst_cgroup;
1819 ret = sev_lock_vcpus_for_migration(kvm, SEV_MIGRATION_SOURCE);
1821 goto out_dst_cgroup;
1822 ret = sev_lock_vcpus_for_migration(source_kvm, SEV_MIGRATION_TARGET);
1826 ret = sev_check_source_vcpus(kvm, source_kvm);
1828 goto out_source_vcpu;
1830 sev_migrate_from(kvm, source_kvm);
1831 kvm_vm_dead(source_kvm);
1832 cg_cleanup_sev = src_sev;
1836 sev_unlock_vcpus_for_migration(source_kvm);
1838 sev_unlock_vcpus_for_migration(kvm);
1840 /* Operates on the source on success, on the destination on failure. */
1842 sev_misc_cg_uncharge(cg_cleanup_sev);
1843 put_misc_cg(cg_cleanup_sev->misc_cg);
1844 cg_cleanup_sev->misc_cg = NULL;
1846 sev_unlock_two_vms(kvm, source_kvm);
1852 int sev_mem_enc_ioctl(struct kvm *kvm, void __user *argp)
1854 struct kvm_sev_cmd sev_cmd;
1863 if (copy_from_user(&sev_cmd, argp, sizeof(struct kvm_sev_cmd)))
1866 mutex_lock(&kvm->lock);
1868 /* Only the enc_context_owner handles some memory enc operations. */
1869 if (is_mirroring_enc_context(kvm) &&
1870 !is_cmd_allowed_from_mirror(sev_cmd.id)) {
1875 switch (sev_cmd.id) {
1876 case KVM_SEV_ES_INIT:
1877 if (!sev_es_enabled) {
1883 r = sev_guest_init(kvm, &sev_cmd);
1885 case KVM_SEV_LAUNCH_START:
1886 r = sev_launch_start(kvm, &sev_cmd);
1888 case KVM_SEV_LAUNCH_UPDATE_DATA:
1889 r = sev_launch_update_data(kvm, &sev_cmd);
1891 case KVM_SEV_LAUNCH_UPDATE_VMSA:
1892 r = sev_launch_update_vmsa(kvm, &sev_cmd);
1894 case KVM_SEV_LAUNCH_MEASURE:
1895 r = sev_launch_measure(kvm, &sev_cmd);
1897 case KVM_SEV_LAUNCH_FINISH:
1898 r = sev_launch_finish(kvm, &sev_cmd);
1900 case KVM_SEV_GUEST_STATUS:
1901 r = sev_guest_status(kvm, &sev_cmd);
1903 case KVM_SEV_DBG_DECRYPT:
1904 r = sev_dbg_crypt(kvm, &sev_cmd, true);
1906 case KVM_SEV_DBG_ENCRYPT:
1907 r = sev_dbg_crypt(kvm, &sev_cmd, false);
1909 case KVM_SEV_LAUNCH_SECRET:
1910 r = sev_launch_secret(kvm, &sev_cmd);
1912 case KVM_SEV_GET_ATTESTATION_REPORT:
1913 r = sev_get_attestation_report(kvm, &sev_cmd);
1915 case KVM_SEV_SEND_START:
1916 r = sev_send_start(kvm, &sev_cmd);
1918 case KVM_SEV_SEND_UPDATE_DATA:
1919 r = sev_send_update_data(kvm, &sev_cmd);
1921 case KVM_SEV_SEND_FINISH:
1922 r = sev_send_finish(kvm, &sev_cmd);
1924 case KVM_SEV_SEND_CANCEL:
1925 r = sev_send_cancel(kvm, &sev_cmd);
1927 case KVM_SEV_RECEIVE_START:
1928 r = sev_receive_start(kvm, &sev_cmd);
1930 case KVM_SEV_RECEIVE_UPDATE_DATA:
1931 r = sev_receive_update_data(kvm, &sev_cmd);
1933 case KVM_SEV_RECEIVE_FINISH:
1934 r = sev_receive_finish(kvm, &sev_cmd);
1941 if (copy_to_user(argp, &sev_cmd, sizeof(struct kvm_sev_cmd)))
1945 mutex_unlock(&kvm->lock);
1949 int sev_mem_enc_register_region(struct kvm *kvm,
1950 struct kvm_enc_region *range)
1952 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
1953 struct enc_region *region;
1956 if (!sev_guest(kvm))
1959 /* If kvm is mirroring encryption context it isn't responsible for it */
1960 if (is_mirroring_enc_context(kvm))
1963 if (range->addr > ULONG_MAX || range->size > ULONG_MAX)
1966 region = kzalloc(sizeof(*region), GFP_KERNEL_ACCOUNT);
1970 mutex_lock(&kvm->lock);
1971 region->pages = sev_pin_memory(kvm, range->addr, range->size, ®ion->npages, 1);
1972 if (IS_ERR(region->pages)) {
1973 ret = PTR_ERR(region->pages);
1974 mutex_unlock(&kvm->lock);
1978 region->uaddr = range->addr;
1979 region->size = range->size;
1981 list_add_tail(®ion->list, &sev->regions_list);
1982 mutex_unlock(&kvm->lock);
1985 * The guest may change the memory encryption attribute from C=0 -> C=1
1986 * or vice versa for this memory range. Lets make sure caches are
1987 * flushed to ensure that guest data gets written into memory with
1990 sev_clflush_pages(region->pages, region->npages);
1999 static struct enc_region *
2000 find_enc_region(struct kvm *kvm, struct kvm_enc_region *range)
2002 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
2003 struct list_head *head = &sev->regions_list;
2004 struct enc_region *i;
2006 list_for_each_entry(i, head, list) {
2007 if (i->uaddr == range->addr &&
2008 i->size == range->size)
2015 static void __unregister_enc_region_locked(struct kvm *kvm,
2016 struct enc_region *region)
2018 sev_unpin_memory(kvm, region->pages, region->npages);
2019 list_del(®ion->list);
2023 int sev_mem_enc_unregister_region(struct kvm *kvm,
2024 struct kvm_enc_region *range)
2026 struct enc_region *region;
2029 /* If kvm is mirroring encryption context it isn't responsible for it */
2030 if (is_mirroring_enc_context(kvm))
2033 mutex_lock(&kvm->lock);
2035 if (!sev_guest(kvm)) {
2040 region = find_enc_region(kvm, range);
2047 * Ensure that all guest tagged cache entries are flushed before
2048 * releasing the pages back to the system for use. CLFLUSH will
2049 * not do this, so issue a WBINVD.
2051 wbinvd_on_all_cpus();
2053 __unregister_enc_region_locked(kvm, region);
2055 mutex_unlock(&kvm->lock);
2059 mutex_unlock(&kvm->lock);
2063 int sev_vm_copy_enc_context_from(struct kvm *kvm, unsigned int source_fd)
2065 struct fd f = fdget(source_fd);
2066 struct kvm *source_kvm;
2067 struct kvm_sev_info *source_sev, *mirror_sev;
2073 if (!file_is_kvm(f.file)) {
2078 source_kvm = f.file->private_data;
2079 ret = sev_lock_two_vms(kvm, source_kvm);
2084 * Mirrors of mirrors should work, but let's not get silly. Also
2085 * disallow out-of-band SEV/SEV-ES init if the target is already an
2086 * SEV guest, or if vCPUs have been created. KVM relies on vCPUs being
2087 * created after SEV/SEV-ES initialization, e.g. to init intercepts.
2089 if (sev_guest(kvm) || !sev_guest(source_kvm) ||
2090 is_mirroring_enc_context(source_kvm) || kvm->created_vcpus) {
2096 * The mirror kvm holds an enc_context_owner ref so its asid can't
2097 * disappear until we're done with it
2099 source_sev = &to_kvm_svm(source_kvm)->sev_info;
2100 kvm_get_kvm(source_kvm);
2101 mirror_sev = &to_kvm_svm(kvm)->sev_info;
2102 list_add_tail(&mirror_sev->mirror_entry, &source_sev->mirror_vms);
2104 /* Set enc_context_owner and copy its encryption context over */
2105 mirror_sev->enc_context_owner = source_kvm;
2106 mirror_sev->active = true;
2107 mirror_sev->asid = source_sev->asid;
2108 mirror_sev->fd = source_sev->fd;
2109 mirror_sev->es_active = source_sev->es_active;
2110 mirror_sev->handle = source_sev->handle;
2111 INIT_LIST_HEAD(&mirror_sev->regions_list);
2112 INIT_LIST_HEAD(&mirror_sev->mirror_vms);
2116 * Do not copy ap_jump_table. Since the mirror does not share the same
2117 * KVM contexts as the original, and they may have different
2122 sev_unlock_two_vms(kvm, source_kvm);
2128 void sev_vm_destroy(struct kvm *kvm)
2130 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
2131 struct list_head *head = &sev->regions_list;
2132 struct list_head *pos, *q;
2134 if (!sev_guest(kvm))
2137 WARN_ON(!list_empty(&sev->mirror_vms));
2139 /* If this is a mirror_kvm release the enc_context_owner and skip sev cleanup */
2140 if (is_mirroring_enc_context(kvm)) {
2141 struct kvm *owner_kvm = sev->enc_context_owner;
2143 mutex_lock(&owner_kvm->lock);
2144 list_del(&sev->mirror_entry);
2145 mutex_unlock(&owner_kvm->lock);
2146 kvm_put_kvm(owner_kvm);
2151 * Ensure that all guest tagged cache entries are flushed before
2152 * releasing the pages back to the system for use. CLFLUSH will
2153 * not do this, so issue a WBINVD.
2155 wbinvd_on_all_cpus();
2158 * if userspace was terminated before unregistering the memory regions
2159 * then lets unpin all the registered memory.
2161 if (!list_empty(head)) {
2162 list_for_each_safe(pos, q, head) {
2163 __unregister_enc_region_locked(kvm,
2164 list_entry(pos, struct enc_region, list));
2169 sev_unbind_asid(kvm, sev->handle);
2173 void __init sev_set_cpu_caps(void)
2176 kvm_cpu_cap_clear(X86_FEATURE_SEV);
2177 if (!sev_es_enabled)
2178 kvm_cpu_cap_clear(X86_FEATURE_SEV_ES);
2181 void __init sev_hardware_setup(void)
2183 #ifdef CONFIG_KVM_AMD_SEV
2184 unsigned int eax, ebx, ecx, edx, sev_asid_count, sev_es_asid_count;
2185 bool sev_es_supported = false;
2186 bool sev_supported = false;
2188 if (!sev_enabled || !npt_enabled || !nrips)
2192 * SEV must obviously be supported in hardware. Sanity check that the
2193 * CPU supports decode assists, which is mandatory for SEV guests to
2194 * support instruction emulation.
2196 if (!boot_cpu_has(X86_FEATURE_SEV) ||
2197 WARN_ON_ONCE(!boot_cpu_has(X86_FEATURE_DECODEASSISTS)))
2200 /* Retrieve SEV CPUID information */
2201 cpuid(0x8000001f, &eax, &ebx, &ecx, &edx);
2203 /* Set encryption bit location for SEV-ES guests */
2204 sev_enc_bit = ebx & 0x3f;
2206 /* Maximum number of encrypted guests supported simultaneously */
2211 /* Minimum ASID value that should be used for SEV guest */
2213 sev_me_mask = 1UL << (ebx & 0x3f);
2216 * Initialize SEV ASID bitmaps. Allocate space for ASID 0 in the bitmap,
2217 * even though it's never used, so that the bitmap is indexed by the
2220 nr_asids = max_sev_asid + 1;
2221 sev_asid_bitmap = bitmap_zalloc(nr_asids, GFP_KERNEL);
2222 if (!sev_asid_bitmap)
2225 sev_reclaim_asid_bitmap = bitmap_zalloc(nr_asids, GFP_KERNEL);
2226 if (!sev_reclaim_asid_bitmap) {
2227 bitmap_free(sev_asid_bitmap);
2228 sev_asid_bitmap = NULL;
2232 sev_asid_count = max_sev_asid - min_sev_asid + 1;
2233 WARN_ON_ONCE(misc_cg_set_capacity(MISC_CG_RES_SEV, sev_asid_count));
2234 sev_supported = true;
2236 /* SEV-ES support requested? */
2237 if (!sev_es_enabled)
2241 * SEV-ES requires MMIO caching as KVM doesn't have access to the guest
2242 * instruction stream, i.e. can't emulate in response to a #NPF and
2243 * instead relies on #NPF(RSVD) being reflected into the guest as #VC
2244 * (the guest can then do a #VMGEXIT to request MMIO emulation).
2246 if (!enable_mmio_caching)
2249 /* Does the CPU support SEV-ES? */
2250 if (!boot_cpu_has(X86_FEATURE_SEV_ES))
2253 /* Has the system been allocated ASIDs for SEV-ES? */
2254 if (min_sev_asid == 1)
2257 sev_es_asid_count = min_sev_asid - 1;
2258 WARN_ON_ONCE(misc_cg_set_capacity(MISC_CG_RES_SEV_ES, sev_es_asid_count));
2259 sev_es_supported = true;
2262 if (boot_cpu_has(X86_FEATURE_SEV))
2263 pr_info("SEV %s (ASIDs %u - %u)\n",
2264 sev_supported ? "enabled" : "disabled",
2265 min_sev_asid, max_sev_asid);
2266 if (boot_cpu_has(X86_FEATURE_SEV_ES))
2267 pr_info("SEV-ES %s (ASIDs %u - %u)\n",
2268 sev_es_supported ? "enabled" : "disabled",
2269 min_sev_asid > 1 ? 1 : 0, min_sev_asid - 1);
2271 sev_enabled = sev_supported;
2272 sev_es_enabled = sev_es_supported;
2273 if (!sev_es_enabled || !cpu_feature_enabled(X86_FEATURE_DEBUG_SWAP) ||
2274 !cpu_feature_enabled(X86_FEATURE_NO_NESTED_DATA_BP))
2275 sev_es_debug_swap_enabled = false;
2279 void sev_hardware_unsetup(void)
2284 /* No need to take sev_bitmap_lock, all VMs have been destroyed. */
2285 sev_flush_asids(1, max_sev_asid);
2287 bitmap_free(sev_asid_bitmap);
2288 bitmap_free(sev_reclaim_asid_bitmap);
2290 misc_cg_set_capacity(MISC_CG_RES_SEV, 0);
2291 misc_cg_set_capacity(MISC_CG_RES_SEV_ES, 0);
2294 int sev_cpu_init(struct svm_cpu_data *sd)
2299 sd->sev_vmcbs = kcalloc(nr_asids, sizeof(void *), GFP_KERNEL);
2307 * Pages used by hardware to hold guest encrypted state must be flushed before
2308 * returning them to the system.
2310 static void sev_flush_encrypted_page(struct kvm_vcpu *vcpu, void *va)
2312 int asid = to_kvm_svm(vcpu->kvm)->sev_info.asid;
2315 * Note! The address must be a kernel address, as regular page walk
2316 * checks are performed by VM_PAGE_FLUSH, i.e. operating on a user
2317 * address is non-deterministic and unsafe. This function deliberately
2318 * takes a pointer to deter passing in a user address.
2320 unsigned long addr = (unsigned long)va;
2323 * If CPU enforced cache coherency for encrypted mappings of the
2324 * same physical page is supported, use CLFLUSHOPT instead. NOTE: cache
2325 * flush is still needed in order to work properly with DMA devices.
2327 if (boot_cpu_has(X86_FEATURE_SME_COHERENT)) {
2328 clflush_cache_range(va, PAGE_SIZE);
2333 * VM Page Flush takes a host virtual address and a guest ASID. Fall
2334 * back to WBINVD if this faults so as not to make any problems worse
2335 * by leaving stale encrypted data in the cache.
2337 if (WARN_ON_ONCE(wrmsrl_safe(MSR_AMD64_VM_PAGE_FLUSH, addr | asid)))
2343 wbinvd_on_all_cpus();
2346 void sev_guest_memory_reclaimed(struct kvm *kvm)
2348 if (!sev_guest(kvm))
2351 wbinvd_on_all_cpus();
2354 void sev_free_vcpu(struct kvm_vcpu *vcpu)
2356 struct vcpu_svm *svm;
2358 if (!sev_es_guest(vcpu->kvm))
2363 if (vcpu->arch.guest_state_protected)
2364 sev_flush_encrypted_page(vcpu, svm->sev_es.vmsa);
2366 __free_page(virt_to_page(svm->sev_es.vmsa));
2368 if (svm->sev_es.ghcb_sa_free)
2369 kvfree(svm->sev_es.ghcb_sa);
2372 static void dump_ghcb(struct vcpu_svm *svm)
2374 struct ghcb *ghcb = svm->sev_es.ghcb;
2377 /* Re-use the dump_invalid_vmcb module parameter */
2378 if (!dump_invalid_vmcb) {
2379 pr_warn_ratelimited("set kvm_amd.dump_invalid_vmcb=1 to dump internal KVM state.\n");
2383 nbits = sizeof(ghcb->save.valid_bitmap) * 8;
2385 pr_err("GHCB (GPA=%016llx):\n", svm->vmcb->control.ghcb_gpa);
2386 pr_err("%-20s%016llx is_valid: %u\n", "sw_exit_code",
2387 ghcb->save.sw_exit_code, ghcb_sw_exit_code_is_valid(ghcb));
2388 pr_err("%-20s%016llx is_valid: %u\n", "sw_exit_info_1",
2389 ghcb->save.sw_exit_info_1, ghcb_sw_exit_info_1_is_valid(ghcb));
2390 pr_err("%-20s%016llx is_valid: %u\n", "sw_exit_info_2",
2391 ghcb->save.sw_exit_info_2, ghcb_sw_exit_info_2_is_valid(ghcb));
2392 pr_err("%-20s%016llx is_valid: %u\n", "sw_scratch",
2393 ghcb->save.sw_scratch, ghcb_sw_scratch_is_valid(ghcb));
2394 pr_err("%-20s%*pb\n", "valid_bitmap", nbits, ghcb->save.valid_bitmap);
2397 static void sev_es_sync_to_ghcb(struct vcpu_svm *svm)
2399 struct kvm_vcpu *vcpu = &svm->vcpu;
2400 struct ghcb *ghcb = svm->sev_es.ghcb;
2403 * The GHCB protocol so far allows for the following data
2405 * GPRs RAX, RBX, RCX, RDX
2407 * Copy their values, even if they may not have been written during the
2408 * VM-Exit. It's the guest's responsibility to not consume random data.
2410 ghcb_set_rax(ghcb, vcpu->arch.regs[VCPU_REGS_RAX]);
2411 ghcb_set_rbx(ghcb, vcpu->arch.regs[VCPU_REGS_RBX]);
2412 ghcb_set_rcx(ghcb, vcpu->arch.regs[VCPU_REGS_RCX]);
2413 ghcb_set_rdx(ghcb, vcpu->arch.regs[VCPU_REGS_RDX]);
2416 static void sev_es_sync_from_ghcb(struct vcpu_svm *svm)
2418 struct vmcb_control_area *control = &svm->vmcb->control;
2419 struct kvm_vcpu *vcpu = &svm->vcpu;
2420 struct ghcb *ghcb = svm->sev_es.ghcb;
2424 * The GHCB protocol so far allows for the following data
2426 * GPRs RAX, RBX, RCX, RDX
2430 * VMMCALL allows the guest to provide extra registers. KVM also
2431 * expects RSI for hypercalls, so include that, too.
2433 * Copy their values to the appropriate location if supplied.
2435 memset(vcpu->arch.regs, 0, sizeof(vcpu->arch.regs));
2437 BUILD_BUG_ON(sizeof(svm->sev_es.valid_bitmap) != sizeof(ghcb->save.valid_bitmap));
2438 memcpy(&svm->sev_es.valid_bitmap, &ghcb->save.valid_bitmap, sizeof(ghcb->save.valid_bitmap));
2440 vcpu->arch.regs[VCPU_REGS_RAX] = kvm_ghcb_get_rax_if_valid(svm, ghcb);
2441 vcpu->arch.regs[VCPU_REGS_RBX] = kvm_ghcb_get_rbx_if_valid(svm, ghcb);
2442 vcpu->arch.regs[VCPU_REGS_RCX] = kvm_ghcb_get_rcx_if_valid(svm, ghcb);
2443 vcpu->arch.regs[VCPU_REGS_RDX] = kvm_ghcb_get_rdx_if_valid(svm, ghcb);
2444 vcpu->arch.regs[VCPU_REGS_RSI] = kvm_ghcb_get_rsi_if_valid(svm, ghcb);
2446 svm->vmcb->save.cpl = kvm_ghcb_get_cpl_if_valid(svm, ghcb);
2448 if (kvm_ghcb_xcr0_is_valid(svm)) {
2449 vcpu->arch.xcr0 = ghcb_get_xcr0(ghcb);
2450 kvm_update_cpuid_runtime(vcpu);
2453 /* Copy the GHCB exit information into the VMCB fields */
2454 exit_code = ghcb_get_sw_exit_code(ghcb);
2455 control->exit_code = lower_32_bits(exit_code);
2456 control->exit_code_hi = upper_32_bits(exit_code);
2457 control->exit_info_1 = ghcb_get_sw_exit_info_1(ghcb);
2458 control->exit_info_2 = ghcb_get_sw_exit_info_2(ghcb);
2459 svm->sev_es.sw_scratch = kvm_ghcb_get_sw_scratch_if_valid(svm, ghcb);
2461 /* Clear the valid entries fields */
2462 memset(ghcb->save.valid_bitmap, 0, sizeof(ghcb->save.valid_bitmap));
2465 static u64 kvm_ghcb_get_sw_exit_code(struct vmcb_control_area *control)
2467 return (((u64)control->exit_code_hi) << 32) | control->exit_code;
2470 static int sev_es_validate_vmgexit(struct vcpu_svm *svm)
2472 struct vmcb_control_area *control = &svm->vmcb->control;
2473 struct kvm_vcpu *vcpu = &svm->vcpu;
2478 * Retrieve the exit code now even though it may not be marked valid
2479 * as it could help with debugging.
2481 exit_code = kvm_ghcb_get_sw_exit_code(control);
2483 /* Only GHCB Usage code 0 is supported */
2484 if (svm->sev_es.ghcb->ghcb_usage) {
2485 reason = GHCB_ERR_INVALID_USAGE;
2489 reason = GHCB_ERR_MISSING_INPUT;
2491 if (!kvm_ghcb_sw_exit_code_is_valid(svm) ||
2492 !kvm_ghcb_sw_exit_info_1_is_valid(svm) ||
2493 !kvm_ghcb_sw_exit_info_2_is_valid(svm))
2496 switch (exit_code) {
2497 case SVM_EXIT_READ_DR7:
2499 case SVM_EXIT_WRITE_DR7:
2500 if (!kvm_ghcb_rax_is_valid(svm))
2503 case SVM_EXIT_RDTSC:
2505 case SVM_EXIT_RDPMC:
2506 if (!kvm_ghcb_rcx_is_valid(svm))
2509 case SVM_EXIT_CPUID:
2510 if (!kvm_ghcb_rax_is_valid(svm) ||
2511 !kvm_ghcb_rcx_is_valid(svm))
2513 if (vcpu->arch.regs[VCPU_REGS_RAX] == 0xd)
2514 if (!kvm_ghcb_xcr0_is_valid(svm))
2520 if (control->exit_info_1 & SVM_IOIO_STR_MASK) {
2521 if (!kvm_ghcb_sw_scratch_is_valid(svm))
2524 if (!(control->exit_info_1 & SVM_IOIO_TYPE_MASK))
2525 if (!kvm_ghcb_rax_is_valid(svm))
2530 if (!kvm_ghcb_rcx_is_valid(svm))
2532 if (control->exit_info_1) {
2533 if (!kvm_ghcb_rax_is_valid(svm) ||
2534 !kvm_ghcb_rdx_is_valid(svm))
2538 case SVM_EXIT_VMMCALL:
2539 if (!kvm_ghcb_rax_is_valid(svm) ||
2540 !kvm_ghcb_cpl_is_valid(svm))
2543 case SVM_EXIT_RDTSCP:
2545 case SVM_EXIT_WBINVD:
2547 case SVM_EXIT_MONITOR:
2548 if (!kvm_ghcb_rax_is_valid(svm) ||
2549 !kvm_ghcb_rcx_is_valid(svm) ||
2550 !kvm_ghcb_rdx_is_valid(svm))
2553 case SVM_EXIT_MWAIT:
2554 if (!kvm_ghcb_rax_is_valid(svm) ||
2555 !kvm_ghcb_rcx_is_valid(svm))
2558 case SVM_VMGEXIT_MMIO_READ:
2559 case SVM_VMGEXIT_MMIO_WRITE:
2560 if (!kvm_ghcb_sw_scratch_is_valid(svm))
2563 case SVM_VMGEXIT_NMI_COMPLETE:
2564 case SVM_VMGEXIT_AP_HLT_LOOP:
2565 case SVM_VMGEXIT_AP_JUMP_TABLE:
2566 case SVM_VMGEXIT_UNSUPPORTED_EVENT:
2569 reason = GHCB_ERR_INVALID_EVENT;
2576 if (reason == GHCB_ERR_INVALID_USAGE) {
2577 vcpu_unimpl(vcpu, "vmgexit: ghcb usage %#x is not valid\n",
2578 svm->sev_es.ghcb->ghcb_usage);
2579 } else if (reason == GHCB_ERR_INVALID_EVENT) {
2580 vcpu_unimpl(vcpu, "vmgexit: exit code %#llx is not valid\n",
2583 vcpu_unimpl(vcpu, "vmgexit: exit code %#llx input is not valid\n",
2588 ghcb_set_sw_exit_info_1(svm->sev_es.ghcb, 2);
2589 ghcb_set_sw_exit_info_2(svm->sev_es.ghcb, reason);
2591 /* Resume the guest to "return" the error code. */
2595 void sev_es_unmap_ghcb(struct vcpu_svm *svm)
2597 if (!svm->sev_es.ghcb)
2600 if (svm->sev_es.ghcb_sa_free) {
2602 * The scratch area lives outside the GHCB, so there is a
2603 * buffer that, depending on the operation performed, may
2604 * need to be synced, then freed.
2606 if (svm->sev_es.ghcb_sa_sync) {
2607 kvm_write_guest(svm->vcpu.kvm,
2608 svm->sev_es.sw_scratch,
2609 svm->sev_es.ghcb_sa,
2610 svm->sev_es.ghcb_sa_len);
2611 svm->sev_es.ghcb_sa_sync = false;
2614 kvfree(svm->sev_es.ghcb_sa);
2615 svm->sev_es.ghcb_sa = NULL;
2616 svm->sev_es.ghcb_sa_free = false;
2619 trace_kvm_vmgexit_exit(svm->vcpu.vcpu_id, svm->sev_es.ghcb);
2621 sev_es_sync_to_ghcb(svm);
2623 kvm_vcpu_unmap(&svm->vcpu, &svm->sev_es.ghcb_map, true);
2624 svm->sev_es.ghcb = NULL;
2627 void pre_sev_run(struct vcpu_svm *svm, int cpu)
2629 struct svm_cpu_data *sd = per_cpu_ptr(&svm_data, cpu);
2630 int asid = sev_get_asid(svm->vcpu.kvm);
2632 /* Assign the asid allocated with this SEV guest */
2638 * 1) when different VMCB for the same ASID is to be run on the same host CPU.
2639 * 2) or this VMCB was executed on different host CPU in previous VMRUNs.
2641 if (sd->sev_vmcbs[asid] == svm->vmcb &&
2642 svm->vcpu.arch.last_vmentry_cpu == cpu)
2645 sd->sev_vmcbs[asid] = svm->vmcb;
2646 svm->vmcb->control.tlb_ctl = TLB_CONTROL_FLUSH_ASID;
2647 vmcb_mark_dirty(svm->vmcb, VMCB_ASID);
2650 #define GHCB_SCRATCH_AREA_LIMIT (16ULL * PAGE_SIZE)
2651 static int setup_vmgexit_scratch(struct vcpu_svm *svm, bool sync, u64 len)
2653 struct vmcb_control_area *control = &svm->vmcb->control;
2654 u64 ghcb_scratch_beg, ghcb_scratch_end;
2655 u64 scratch_gpa_beg, scratch_gpa_end;
2658 scratch_gpa_beg = svm->sev_es.sw_scratch;
2659 if (!scratch_gpa_beg) {
2660 pr_err("vmgexit: scratch gpa not provided\n");
2664 scratch_gpa_end = scratch_gpa_beg + len;
2665 if (scratch_gpa_end < scratch_gpa_beg) {
2666 pr_err("vmgexit: scratch length (%#llx) not valid for scratch address (%#llx)\n",
2667 len, scratch_gpa_beg);
2671 if ((scratch_gpa_beg & PAGE_MASK) == control->ghcb_gpa) {
2672 /* Scratch area begins within GHCB */
2673 ghcb_scratch_beg = control->ghcb_gpa +
2674 offsetof(struct ghcb, shared_buffer);
2675 ghcb_scratch_end = control->ghcb_gpa +
2676 offsetof(struct ghcb, reserved_0xff0);
2679 * If the scratch area begins within the GHCB, it must be
2680 * completely contained in the GHCB shared buffer area.
2682 if (scratch_gpa_beg < ghcb_scratch_beg ||
2683 scratch_gpa_end > ghcb_scratch_end) {
2684 pr_err("vmgexit: scratch area is outside of GHCB shared buffer area (%#llx - %#llx)\n",
2685 scratch_gpa_beg, scratch_gpa_end);
2689 scratch_va = (void *)svm->sev_es.ghcb;
2690 scratch_va += (scratch_gpa_beg - control->ghcb_gpa);
2693 * The guest memory must be read into a kernel buffer, so
2696 if (len > GHCB_SCRATCH_AREA_LIMIT) {
2697 pr_err("vmgexit: scratch area exceeds KVM limits (%#llx requested, %#llx limit)\n",
2698 len, GHCB_SCRATCH_AREA_LIMIT);
2701 scratch_va = kvzalloc(len, GFP_KERNEL_ACCOUNT);
2705 if (kvm_read_guest(svm->vcpu.kvm, scratch_gpa_beg, scratch_va, len)) {
2706 /* Unable to copy scratch area from guest */
2707 pr_err("vmgexit: kvm_read_guest for scratch area failed\n");
2714 * The scratch area is outside the GHCB. The operation will
2715 * dictate whether the buffer needs to be synced before running
2716 * the vCPU next time (i.e. a read was requested so the data
2717 * must be written back to the guest memory).
2719 svm->sev_es.ghcb_sa_sync = sync;
2720 svm->sev_es.ghcb_sa_free = true;
2723 svm->sev_es.ghcb_sa = scratch_va;
2724 svm->sev_es.ghcb_sa_len = len;
2729 ghcb_set_sw_exit_info_1(svm->sev_es.ghcb, 2);
2730 ghcb_set_sw_exit_info_2(svm->sev_es.ghcb, GHCB_ERR_INVALID_SCRATCH_AREA);
2735 static void set_ghcb_msr_bits(struct vcpu_svm *svm, u64 value, u64 mask,
2738 svm->vmcb->control.ghcb_gpa &= ~(mask << pos);
2739 svm->vmcb->control.ghcb_gpa |= (value & mask) << pos;
2742 static u64 get_ghcb_msr_bits(struct vcpu_svm *svm, u64 mask, unsigned int pos)
2744 return (svm->vmcb->control.ghcb_gpa >> pos) & mask;
2747 static void set_ghcb_msr(struct vcpu_svm *svm, u64 value)
2749 svm->vmcb->control.ghcb_gpa = value;
2752 static int sev_handle_vmgexit_msr_protocol(struct vcpu_svm *svm)
2754 struct vmcb_control_area *control = &svm->vmcb->control;
2755 struct kvm_vcpu *vcpu = &svm->vcpu;
2759 ghcb_info = control->ghcb_gpa & GHCB_MSR_INFO_MASK;
2761 trace_kvm_vmgexit_msr_protocol_enter(svm->vcpu.vcpu_id,
2764 switch (ghcb_info) {
2765 case GHCB_MSR_SEV_INFO_REQ:
2766 set_ghcb_msr(svm, GHCB_MSR_SEV_INFO(GHCB_VERSION_MAX,
2770 case GHCB_MSR_CPUID_REQ: {
2771 u64 cpuid_fn, cpuid_reg, cpuid_value;
2773 cpuid_fn = get_ghcb_msr_bits(svm,
2774 GHCB_MSR_CPUID_FUNC_MASK,
2775 GHCB_MSR_CPUID_FUNC_POS);
2777 /* Initialize the registers needed by the CPUID intercept */
2778 vcpu->arch.regs[VCPU_REGS_RAX] = cpuid_fn;
2779 vcpu->arch.regs[VCPU_REGS_RCX] = 0;
2781 ret = svm_invoke_exit_handler(vcpu, SVM_EXIT_CPUID);
2783 /* Error, keep GHCB MSR value as-is */
2787 cpuid_reg = get_ghcb_msr_bits(svm,
2788 GHCB_MSR_CPUID_REG_MASK,
2789 GHCB_MSR_CPUID_REG_POS);
2791 cpuid_value = vcpu->arch.regs[VCPU_REGS_RAX];
2792 else if (cpuid_reg == 1)
2793 cpuid_value = vcpu->arch.regs[VCPU_REGS_RBX];
2794 else if (cpuid_reg == 2)
2795 cpuid_value = vcpu->arch.regs[VCPU_REGS_RCX];
2797 cpuid_value = vcpu->arch.regs[VCPU_REGS_RDX];
2799 set_ghcb_msr_bits(svm, cpuid_value,
2800 GHCB_MSR_CPUID_VALUE_MASK,
2801 GHCB_MSR_CPUID_VALUE_POS);
2803 set_ghcb_msr_bits(svm, GHCB_MSR_CPUID_RESP,
2808 case GHCB_MSR_TERM_REQ: {
2809 u64 reason_set, reason_code;
2811 reason_set = get_ghcb_msr_bits(svm,
2812 GHCB_MSR_TERM_REASON_SET_MASK,
2813 GHCB_MSR_TERM_REASON_SET_POS);
2814 reason_code = get_ghcb_msr_bits(svm,
2815 GHCB_MSR_TERM_REASON_MASK,
2816 GHCB_MSR_TERM_REASON_POS);
2817 pr_info("SEV-ES guest requested termination: %#llx:%#llx\n",
2818 reason_set, reason_code);
2820 vcpu->run->exit_reason = KVM_EXIT_SYSTEM_EVENT;
2821 vcpu->run->system_event.type = KVM_SYSTEM_EVENT_SEV_TERM;
2822 vcpu->run->system_event.ndata = 1;
2823 vcpu->run->system_event.data[0] = control->ghcb_gpa;
2828 /* Error, keep GHCB MSR value as-is */
2832 trace_kvm_vmgexit_msr_protocol_exit(svm->vcpu.vcpu_id,
2833 control->ghcb_gpa, ret);
2838 int sev_handle_vmgexit(struct kvm_vcpu *vcpu)
2840 struct vcpu_svm *svm = to_svm(vcpu);
2841 struct vmcb_control_area *control = &svm->vmcb->control;
2842 u64 ghcb_gpa, exit_code;
2845 /* Validate the GHCB */
2846 ghcb_gpa = control->ghcb_gpa;
2847 if (ghcb_gpa & GHCB_MSR_INFO_MASK)
2848 return sev_handle_vmgexit_msr_protocol(svm);
2851 vcpu_unimpl(vcpu, "vmgexit: GHCB gpa is not set\n");
2853 /* Without a GHCB, just return right back to the guest */
2857 if (kvm_vcpu_map(vcpu, ghcb_gpa >> PAGE_SHIFT, &svm->sev_es.ghcb_map)) {
2858 /* Unable to map GHCB from guest */
2859 vcpu_unimpl(vcpu, "vmgexit: error mapping GHCB [%#llx] from guest\n",
2862 /* Without a GHCB, just return right back to the guest */
2866 svm->sev_es.ghcb = svm->sev_es.ghcb_map.hva;
2868 trace_kvm_vmgexit_enter(vcpu->vcpu_id, svm->sev_es.ghcb);
2870 sev_es_sync_from_ghcb(svm);
2871 ret = sev_es_validate_vmgexit(svm);
2875 ghcb_set_sw_exit_info_1(svm->sev_es.ghcb, 0);
2876 ghcb_set_sw_exit_info_2(svm->sev_es.ghcb, 0);
2878 exit_code = kvm_ghcb_get_sw_exit_code(control);
2879 switch (exit_code) {
2880 case SVM_VMGEXIT_MMIO_READ:
2881 ret = setup_vmgexit_scratch(svm, true, control->exit_info_2);
2885 ret = kvm_sev_es_mmio_read(vcpu,
2886 control->exit_info_1,
2887 control->exit_info_2,
2888 svm->sev_es.ghcb_sa);
2890 case SVM_VMGEXIT_MMIO_WRITE:
2891 ret = setup_vmgexit_scratch(svm, false, control->exit_info_2);
2895 ret = kvm_sev_es_mmio_write(vcpu,
2896 control->exit_info_1,
2897 control->exit_info_2,
2898 svm->sev_es.ghcb_sa);
2900 case SVM_VMGEXIT_NMI_COMPLETE:
2901 ++vcpu->stat.nmi_window_exits;
2902 svm->nmi_masked = false;
2903 kvm_make_request(KVM_REQ_EVENT, vcpu);
2906 case SVM_VMGEXIT_AP_HLT_LOOP:
2907 ret = kvm_emulate_ap_reset_hold(vcpu);
2909 case SVM_VMGEXIT_AP_JUMP_TABLE: {
2910 struct kvm_sev_info *sev = &to_kvm_svm(vcpu->kvm)->sev_info;
2912 switch (control->exit_info_1) {
2914 /* Set AP jump table address */
2915 sev->ap_jump_table = control->exit_info_2;
2918 /* Get AP jump table address */
2919 ghcb_set_sw_exit_info_2(svm->sev_es.ghcb, sev->ap_jump_table);
2922 pr_err("svm: vmgexit: unsupported AP jump table request - exit_info_1=%#llx\n",
2923 control->exit_info_1);
2924 ghcb_set_sw_exit_info_1(svm->sev_es.ghcb, 2);
2925 ghcb_set_sw_exit_info_2(svm->sev_es.ghcb, GHCB_ERR_INVALID_INPUT);
2931 case SVM_VMGEXIT_UNSUPPORTED_EVENT:
2933 "vmgexit: unsupported event - exit_info_1=%#llx, exit_info_2=%#llx\n",
2934 control->exit_info_1, control->exit_info_2);
2938 ret = svm_invoke_exit_handler(vcpu, exit_code);
2944 int sev_es_string_io(struct vcpu_svm *svm, int size, unsigned int port, int in)
2950 if (svm->vmcb->control.exit_info_2 > INT_MAX)
2953 count = svm->vmcb->control.exit_info_2;
2954 if (unlikely(check_mul_overflow(count, size, &bytes)))
2957 r = setup_vmgexit_scratch(svm, in, bytes);
2961 return kvm_sev_es_string_io(&svm->vcpu, size, port, svm->sev_es.ghcb_sa,
2965 static void sev_es_vcpu_after_set_cpuid(struct vcpu_svm *svm)
2967 struct kvm_vcpu *vcpu = &svm->vcpu;
2969 if (boot_cpu_has(X86_FEATURE_V_TSC_AUX)) {
2970 bool v_tsc_aux = guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP) ||
2971 guest_cpuid_has(vcpu, X86_FEATURE_RDPID);
2973 set_msr_interception(vcpu, svm->msrpm, MSR_TSC_AUX, v_tsc_aux, v_tsc_aux);
2977 void sev_vcpu_after_set_cpuid(struct vcpu_svm *svm)
2979 struct kvm_vcpu *vcpu = &svm->vcpu;
2980 struct kvm_cpuid_entry2 *best;
2982 /* For sev guests, the memory encryption bit is not reserved in CR3. */
2983 best = kvm_find_cpuid_entry(vcpu, 0x8000001F);
2985 vcpu->arch.reserved_gpa_bits &= ~(1UL << (best->ebx & 0x3f));
2987 if (sev_es_guest(svm->vcpu.kvm))
2988 sev_es_vcpu_after_set_cpuid(svm);
2991 static void sev_es_init_vmcb(struct vcpu_svm *svm)
2993 struct vmcb *vmcb = svm->vmcb01.ptr;
2994 struct kvm_vcpu *vcpu = &svm->vcpu;
2996 svm->vmcb->control.nested_ctl |= SVM_NESTED_CTL_SEV_ES_ENABLE;
2997 svm->vmcb->control.virt_ext |= LBR_CTL_ENABLE_MASK;
3000 * An SEV-ES guest requires a VMSA area that is a separate from the
3001 * VMCB page. Do not include the encryption mask on the VMSA physical
3002 * address since hardware will access it using the guest key. Note,
3003 * the VMSA will be NULL if this vCPU is the destination for intrahost
3004 * migration, and will be copied later.
3006 if (svm->sev_es.vmsa)
3007 svm->vmcb->control.vmsa_pa = __pa(svm->sev_es.vmsa);
3009 /* Can't intercept CR register access, HV can't modify CR registers */
3010 svm_clr_intercept(svm, INTERCEPT_CR0_READ);
3011 svm_clr_intercept(svm, INTERCEPT_CR4_READ);
3012 svm_clr_intercept(svm, INTERCEPT_CR8_READ);
3013 svm_clr_intercept(svm, INTERCEPT_CR0_WRITE);
3014 svm_clr_intercept(svm, INTERCEPT_CR4_WRITE);
3015 svm_clr_intercept(svm, INTERCEPT_CR8_WRITE);
3017 svm_clr_intercept(svm, INTERCEPT_SELECTIVE_CR0);
3019 /* Track EFER/CR register changes */
3020 svm_set_intercept(svm, TRAP_EFER_WRITE);
3021 svm_set_intercept(svm, TRAP_CR0_WRITE);
3022 svm_set_intercept(svm, TRAP_CR4_WRITE);
3023 svm_set_intercept(svm, TRAP_CR8_WRITE);
3025 vmcb->control.intercepts[INTERCEPT_DR] = 0;
3026 if (!sev_es_debug_swap_enabled) {
3027 vmcb_set_intercept(&vmcb->control, INTERCEPT_DR7_READ);
3028 vmcb_set_intercept(&vmcb->control, INTERCEPT_DR7_WRITE);
3029 recalc_intercepts(svm);
3032 * Disable #DB intercept iff DebugSwap is enabled. KVM doesn't
3033 * allow debugging SEV-ES guests, and enables DebugSwap iff
3034 * NO_NESTED_DATA_BP is supported, so there's no reason to
3035 * intercept #DB when DebugSwap is enabled. For simplicity
3036 * with respect to guest debug, intercept #DB for other VMs
3037 * even if NO_NESTED_DATA_BP is supported, i.e. even if the
3038 * guest can't DoS the CPU with infinite #DB vectoring.
3040 clr_exception_intercept(svm, DB_VECTOR);
3043 /* Can't intercept XSETBV, HV can't modify XCR0 directly */
3044 svm_clr_intercept(svm, INTERCEPT_XSETBV);
3046 /* Clear intercepts on selected MSRs */
3047 set_msr_interception(vcpu, svm->msrpm, MSR_EFER, 1, 1);
3048 set_msr_interception(vcpu, svm->msrpm, MSR_IA32_CR_PAT, 1, 1);
3049 set_msr_interception(vcpu, svm->msrpm, MSR_IA32_LASTBRANCHFROMIP, 1, 1);
3050 set_msr_interception(vcpu, svm->msrpm, MSR_IA32_LASTBRANCHTOIP, 1, 1);
3051 set_msr_interception(vcpu, svm->msrpm, MSR_IA32_LASTINTFROMIP, 1, 1);
3052 set_msr_interception(vcpu, svm->msrpm, MSR_IA32_LASTINTTOIP, 1, 1);
3055 void sev_init_vmcb(struct vcpu_svm *svm)
3057 svm->vmcb->control.nested_ctl |= SVM_NESTED_CTL_SEV_ENABLE;
3058 clr_exception_intercept(svm, UD_VECTOR);
3061 * Don't intercept #GP for SEV guests, e.g. for the VMware backdoor, as
3062 * KVM can't decrypt guest memory to decode the faulting instruction.
3064 clr_exception_intercept(svm, GP_VECTOR);
3066 if (sev_es_guest(svm->vcpu.kvm))
3067 sev_es_init_vmcb(svm);
3070 void sev_es_vcpu_reset(struct vcpu_svm *svm)
3073 * Set the GHCB MSR value as per the GHCB specification when emulating
3074 * vCPU RESET for an SEV-ES guest.
3076 set_ghcb_msr(svm, GHCB_MSR_SEV_INFO(GHCB_VERSION_MAX,
3081 void sev_es_prepare_switch_to_guest(struct sev_es_save_area *hostsa)
3084 * All host state for SEV-ES guests is categorized into three swap types
3085 * based on how it is handled by hardware during a world switch:
3087 * A: VMRUN: Host state saved in host save area
3088 * VMEXIT: Host state loaded from host save area
3090 * B: VMRUN: Host state _NOT_ saved in host save area
3091 * VMEXIT: Host state loaded from host save area
3093 * C: VMRUN: Host state _NOT_ saved in host save area
3094 * VMEXIT: Host state initialized to default(reset) values
3096 * Manually save type-B state, i.e. state that is loaded by VMEXIT but
3097 * isn't saved by VMRUN, that isn't already saved by VMSAVE (performed
3098 * by common SVM code).
3100 hostsa->xcr0 = xgetbv(XCR_XFEATURE_ENABLED_MASK);
3101 hostsa->pkru = read_pkru();
3102 hostsa->xss = host_xss;
3105 * If DebugSwap is enabled, debug registers are loaded but NOT saved by
3106 * the CPU (Type-B). If DebugSwap is disabled/unsupported, the CPU both
3107 * saves and loads debug registers (Type-A).
3109 if (sev_es_debug_swap_enabled) {
3110 hostsa->dr0 = native_get_debugreg(0);
3111 hostsa->dr1 = native_get_debugreg(1);
3112 hostsa->dr2 = native_get_debugreg(2);
3113 hostsa->dr3 = native_get_debugreg(3);
3114 hostsa->dr0_addr_mask = amd_get_dr_addr_mask(0);
3115 hostsa->dr1_addr_mask = amd_get_dr_addr_mask(1);
3116 hostsa->dr2_addr_mask = amd_get_dr_addr_mask(2);
3117 hostsa->dr3_addr_mask = amd_get_dr_addr_mask(3);
3121 void sev_vcpu_deliver_sipi_vector(struct kvm_vcpu *vcpu, u8 vector)
3123 struct vcpu_svm *svm = to_svm(vcpu);
3125 /* First SIPI: Use the values as initially set by the VMM */
3126 if (!svm->sev_es.received_first_sipi) {
3127 svm->sev_es.received_first_sipi = true;
3132 * Subsequent SIPI: Return from an AP Reset Hold VMGEXIT, where
3133 * the guest will set the CS and RIP. Set SW_EXIT_INFO_2 to a
3136 if (!svm->sev_es.ghcb)
3139 ghcb_set_sw_exit_info_2(svm->sev_es.ghcb, 1);