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.
10 #include <linux/kvm_types.h>
11 #include <linux/kvm_host.h>
12 #include <linux/kernel.h>
13 #include <linux/highmem.h>
14 #include <linux/psp-sev.h>
15 #include <linux/pagemap.h>
16 #include <linux/swap.h>
17 #include <linux/misc_cgroup.h>
18 #include <linux/processor.h>
19 #include <linux/trace_events.h>
20 #include <asm/fpu/internal.h>
23 #include <asm/trapnr.h>
31 #ifndef CONFIG_KVM_AMD_SEV
33 * When this config is not defined, SEV feature is not supported and APIs in
34 * this file are not used but this file still gets compiled into the KVM AMD
37 * We will not have MISC_CG_RES_SEV and MISC_CG_RES_SEV_ES entries in the enum
38 * misc_res_type {} defined in linux/misc_cgroup.h.
40 * Below macros allow compilation to succeed.
42 #define MISC_CG_RES_SEV MISC_CG_RES_TYPES
43 #define MISC_CG_RES_SEV_ES MISC_CG_RES_TYPES
46 #ifdef CONFIG_KVM_AMD_SEV
47 /* enable/disable SEV support */
48 static bool sev_enabled = true;
49 module_param_named(sev, sev_enabled, bool, 0444);
51 /* enable/disable SEV-ES support */
52 static bool sev_es_enabled = true;
53 module_param_named(sev_es, sev_es_enabled, bool, 0444);
55 #define sev_enabled false
56 #define sev_es_enabled false
57 #endif /* CONFIG_KVM_AMD_SEV */
59 static u8 sev_enc_bit;
60 static DECLARE_RWSEM(sev_deactivate_lock);
61 static DEFINE_MUTEX(sev_bitmap_lock);
62 unsigned int max_sev_asid;
63 static unsigned int min_sev_asid;
64 static unsigned long sev_me_mask;
65 static unsigned int nr_asids;
66 static unsigned long *sev_asid_bitmap;
67 static unsigned long *sev_reclaim_asid_bitmap;
70 struct list_head list;
77 /* Called with the sev_bitmap_lock held, or on shutdown */
78 static int sev_flush_asids(int min_asid, int max_asid)
80 int ret, asid, error = 0;
82 /* Check if there are any ASIDs to reclaim before performing a flush */
83 asid = find_next_bit(sev_reclaim_asid_bitmap, nr_asids, min_asid);
88 * DEACTIVATE will clear the WBINVD indicator causing DF_FLUSH to fail,
89 * so it must be guarded.
91 down_write(&sev_deactivate_lock);
94 ret = sev_guest_df_flush(&error);
96 up_write(&sev_deactivate_lock);
99 pr_err("SEV: DF_FLUSH failed, ret=%d, error=%#x\n", ret, error);
104 static inline bool is_mirroring_enc_context(struct kvm *kvm)
106 return !!to_kvm_svm(kvm)->sev_info.enc_context_owner;
109 /* Must be called with the sev_bitmap_lock held */
110 static bool __sev_recycle_asids(int min_asid, int max_asid)
112 if (sev_flush_asids(min_asid, max_asid))
115 /* The flush process will flush all reclaimable SEV and SEV-ES ASIDs */
116 bitmap_xor(sev_asid_bitmap, sev_asid_bitmap, sev_reclaim_asid_bitmap,
118 bitmap_zero(sev_reclaim_asid_bitmap, nr_asids);
123 static int sev_asid_new(struct kvm_sev_info *sev)
125 int asid, min_asid, max_asid, ret;
127 enum misc_res_type type;
129 type = sev->es_active ? MISC_CG_RES_SEV_ES : MISC_CG_RES_SEV;
130 WARN_ON(sev->misc_cg);
131 sev->misc_cg = get_current_misc_cg();
132 ret = misc_cg_try_charge(type, sev->misc_cg, 1);
134 put_misc_cg(sev->misc_cg);
139 mutex_lock(&sev_bitmap_lock);
142 * SEV-enabled guests must use asid from min_sev_asid to max_sev_asid.
143 * SEV-ES-enabled guest can use from 1 to min_sev_asid - 1.
145 min_asid = sev->es_active ? 1 : min_sev_asid;
146 max_asid = sev->es_active ? min_sev_asid - 1 : max_sev_asid;
148 asid = find_next_zero_bit(sev_asid_bitmap, max_asid + 1, min_asid);
149 if (asid > max_asid) {
150 if (retry && __sev_recycle_asids(min_asid, max_asid)) {
154 mutex_unlock(&sev_bitmap_lock);
159 __set_bit(asid, sev_asid_bitmap);
161 mutex_unlock(&sev_bitmap_lock);
165 misc_cg_uncharge(type, sev->misc_cg, 1);
166 put_misc_cg(sev->misc_cg);
171 static int sev_get_asid(struct kvm *kvm)
173 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
178 static void sev_asid_free(struct kvm_sev_info *sev)
180 struct svm_cpu_data *sd;
182 enum misc_res_type type;
184 mutex_lock(&sev_bitmap_lock);
186 __set_bit(sev->asid, sev_reclaim_asid_bitmap);
188 for_each_possible_cpu(cpu) {
189 sd = per_cpu(svm_data, cpu);
190 sd->sev_vmcbs[sev->asid] = NULL;
193 mutex_unlock(&sev_bitmap_lock);
195 type = sev->es_active ? MISC_CG_RES_SEV_ES : MISC_CG_RES_SEV;
196 misc_cg_uncharge(type, sev->misc_cg, 1);
197 put_misc_cg(sev->misc_cg);
201 static void sev_decommission(unsigned int handle)
203 struct sev_data_decommission decommission;
208 decommission.handle = handle;
209 sev_guest_decommission(&decommission, NULL);
212 static void sev_unbind_asid(struct kvm *kvm, unsigned int handle)
214 struct sev_data_deactivate deactivate;
219 deactivate.handle = handle;
221 /* Guard DEACTIVATE against WBINVD/DF_FLUSH used in ASID recycling */
222 down_read(&sev_deactivate_lock);
223 sev_guest_deactivate(&deactivate, NULL);
224 up_read(&sev_deactivate_lock);
226 sev_decommission(handle);
229 static int sev_guest_init(struct kvm *kvm, struct kvm_sev_cmd *argp)
231 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
232 bool es_active = argp->id == KVM_SEV_ES_INIT;
235 if (kvm->created_vcpus)
239 if (unlikely(sev->active))
242 sev->es_active = es_active;
243 asid = sev_asid_new(sev);
248 ret = sev_platform_init(&argp->error);
254 INIT_LIST_HEAD(&sev->regions_list);
262 sev->es_active = false;
266 static int sev_bind_asid(struct kvm *kvm, unsigned int handle, int *error)
268 struct sev_data_activate activate;
269 int asid = sev_get_asid(kvm);
272 /* activate ASID on the given handle */
273 activate.handle = handle;
274 activate.asid = asid;
275 ret = sev_guest_activate(&activate, error);
280 static int __sev_issue_cmd(int fd, int id, void *data, int *error)
289 ret = sev_issue_cmd_external_user(f.file, id, data, error);
295 static int sev_issue_cmd(struct kvm *kvm, int id, void *data, int *error)
297 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
299 return __sev_issue_cmd(sev->fd, id, data, error);
302 static int sev_launch_start(struct kvm *kvm, struct kvm_sev_cmd *argp)
304 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
305 struct sev_data_launch_start start;
306 struct kvm_sev_launch_start params;
307 void *dh_blob, *session_blob;
308 int *error = &argp->error;
314 if (copy_from_user(¶ms, (void __user *)(uintptr_t)argp->data, sizeof(params)))
317 memset(&start, 0, sizeof(start));
320 if (params.dh_uaddr) {
321 dh_blob = psp_copy_user_blob(params.dh_uaddr, params.dh_len);
323 return PTR_ERR(dh_blob);
325 start.dh_cert_address = __sme_set(__pa(dh_blob));
326 start.dh_cert_len = params.dh_len;
330 if (params.session_uaddr) {
331 session_blob = psp_copy_user_blob(params.session_uaddr, params.session_len);
332 if (IS_ERR(session_blob)) {
333 ret = PTR_ERR(session_blob);
337 start.session_address = __sme_set(__pa(session_blob));
338 start.session_len = params.session_len;
341 start.handle = params.handle;
342 start.policy = params.policy;
344 /* create memory encryption context */
345 ret = __sev_issue_cmd(argp->sev_fd, SEV_CMD_LAUNCH_START, &start, error);
349 /* Bind ASID to this guest */
350 ret = sev_bind_asid(kvm, start.handle, error);
352 sev_decommission(start.handle);
356 /* return handle to userspace */
357 params.handle = start.handle;
358 if (copy_to_user((void __user *)(uintptr_t)argp->data, ¶ms, sizeof(params))) {
359 sev_unbind_asid(kvm, start.handle);
364 sev->handle = start.handle;
365 sev->fd = argp->sev_fd;
374 static struct page **sev_pin_memory(struct kvm *kvm, unsigned long uaddr,
375 unsigned long ulen, unsigned long *n,
378 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
379 unsigned long npages, size;
381 unsigned long locked, lock_limit;
383 unsigned long first, last;
386 lockdep_assert_held(&kvm->lock);
388 if (ulen == 0 || uaddr + ulen < uaddr)
389 return ERR_PTR(-EINVAL);
391 /* Calculate number of pages. */
392 first = (uaddr & PAGE_MASK) >> PAGE_SHIFT;
393 last = ((uaddr + ulen - 1) & PAGE_MASK) >> PAGE_SHIFT;
394 npages = (last - first + 1);
396 locked = sev->pages_locked + npages;
397 lock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
398 if (locked > lock_limit && !capable(CAP_IPC_LOCK)) {
399 pr_err("SEV: %lu locked pages exceed the lock limit of %lu.\n", locked, lock_limit);
400 return ERR_PTR(-ENOMEM);
403 if (WARN_ON_ONCE(npages > INT_MAX))
404 return ERR_PTR(-EINVAL);
406 /* Avoid using vmalloc for smaller buffers. */
407 size = npages * sizeof(struct page *);
408 if (size > PAGE_SIZE)
409 pages = __vmalloc(size, GFP_KERNEL_ACCOUNT | __GFP_ZERO);
411 pages = kmalloc(size, GFP_KERNEL_ACCOUNT);
414 return ERR_PTR(-ENOMEM);
416 /* Pin the user virtual address. */
417 npinned = pin_user_pages_fast(uaddr, npages, write ? FOLL_WRITE : 0, pages);
418 if (npinned != npages) {
419 pr_err("SEV: Failure locking %lu pages.\n", npages);
425 sev->pages_locked = locked;
431 unpin_user_pages(pages, npinned);
437 static void sev_unpin_memory(struct kvm *kvm, struct page **pages,
438 unsigned long npages)
440 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
442 unpin_user_pages(pages, npages);
444 sev->pages_locked -= npages;
447 static void sev_clflush_pages(struct page *pages[], unsigned long npages)
449 uint8_t *page_virtual;
452 if (this_cpu_has(X86_FEATURE_SME_COHERENT) || npages == 0 ||
456 for (i = 0; i < npages; i++) {
457 page_virtual = kmap_atomic(pages[i]);
458 clflush_cache_range(page_virtual, PAGE_SIZE);
459 kunmap_atomic(page_virtual);
463 static unsigned long get_num_contig_pages(unsigned long idx,
464 struct page **inpages, unsigned long npages)
466 unsigned long paddr, next_paddr;
467 unsigned long i = idx + 1, pages = 1;
469 /* find the number of contiguous pages starting from idx */
470 paddr = __sme_page_pa(inpages[idx]);
472 next_paddr = __sme_page_pa(inpages[i++]);
473 if ((paddr + PAGE_SIZE) == next_paddr) {
484 static int sev_launch_update_data(struct kvm *kvm, struct kvm_sev_cmd *argp)
486 unsigned long vaddr, vaddr_end, next_vaddr, npages, pages, size, i;
487 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
488 struct kvm_sev_launch_update_data params;
489 struct sev_data_launch_update_data data;
490 struct page **inpages;
496 if (copy_from_user(¶ms, (void __user *)(uintptr_t)argp->data, sizeof(params)))
499 vaddr = params.uaddr;
501 vaddr_end = vaddr + size;
503 /* Lock the user memory. */
504 inpages = sev_pin_memory(kvm, vaddr, size, &npages, 1);
506 return PTR_ERR(inpages);
509 * Flush (on non-coherent CPUs) before LAUNCH_UPDATE encrypts pages in
510 * place; the cache may contain the data that was written unencrypted.
512 sev_clflush_pages(inpages, npages);
515 data.handle = sev->handle;
517 for (i = 0; vaddr < vaddr_end; vaddr = next_vaddr, i += pages) {
521 * If the user buffer is not page-aligned, calculate the offset
524 offset = vaddr & (PAGE_SIZE - 1);
526 /* Calculate the number of pages that can be encrypted in one go. */
527 pages = get_num_contig_pages(i, inpages, npages);
529 len = min_t(size_t, ((pages * PAGE_SIZE) - offset), size);
532 data.address = __sme_page_pa(inpages[i]) + offset;
533 ret = sev_issue_cmd(kvm, SEV_CMD_LAUNCH_UPDATE_DATA, &data, &argp->error);
538 next_vaddr = vaddr + len;
542 /* content of memory is updated, mark pages dirty */
543 for (i = 0; i < npages; i++) {
544 set_page_dirty_lock(inpages[i]);
545 mark_page_accessed(inpages[i]);
547 /* unlock the user pages */
548 sev_unpin_memory(kvm, inpages, npages);
552 static int sev_es_sync_vmsa(struct vcpu_svm *svm)
554 struct vmcb_save_area *save = &svm->vmcb->save;
556 /* Check some debug related fields before encrypting the VMSA */
557 if (svm->vcpu.guest_debug || (save->dr7 & ~DR7_FIXED_1))
560 /* Sync registgers */
561 save->rax = svm->vcpu.arch.regs[VCPU_REGS_RAX];
562 save->rbx = svm->vcpu.arch.regs[VCPU_REGS_RBX];
563 save->rcx = svm->vcpu.arch.regs[VCPU_REGS_RCX];
564 save->rdx = svm->vcpu.arch.regs[VCPU_REGS_RDX];
565 save->rsp = svm->vcpu.arch.regs[VCPU_REGS_RSP];
566 save->rbp = svm->vcpu.arch.regs[VCPU_REGS_RBP];
567 save->rsi = svm->vcpu.arch.regs[VCPU_REGS_RSI];
568 save->rdi = svm->vcpu.arch.regs[VCPU_REGS_RDI];
570 save->r8 = svm->vcpu.arch.regs[VCPU_REGS_R8];
571 save->r9 = svm->vcpu.arch.regs[VCPU_REGS_R9];
572 save->r10 = svm->vcpu.arch.regs[VCPU_REGS_R10];
573 save->r11 = svm->vcpu.arch.regs[VCPU_REGS_R11];
574 save->r12 = svm->vcpu.arch.regs[VCPU_REGS_R12];
575 save->r13 = svm->vcpu.arch.regs[VCPU_REGS_R13];
576 save->r14 = svm->vcpu.arch.regs[VCPU_REGS_R14];
577 save->r15 = svm->vcpu.arch.regs[VCPU_REGS_R15];
579 save->rip = svm->vcpu.arch.regs[VCPU_REGS_RIP];
581 /* Sync some non-GPR registers before encrypting */
582 save->xcr0 = svm->vcpu.arch.xcr0;
583 save->pkru = svm->vcpu.arch.pkru;
584 save->xss = svm->vcpu.arch.ia32_xss;
585 save->dr6 = svm->vcpu.arch.dr6;
588 * SEV-ES will use a VMSA that is pointed to by the VMCB, not
589 * the traditional VMSA that is part of the VMCB. Copy the
590 * traditional VMSA as it has been built so far (in prep
591 * for LAUNCH_UPDATE_VMSA) to be the initial SEV-ES state.
593 memcpy(svm->vmsa, save, sizeof(*save));
598 static int __sev_launch_update_vmsa(struct kvm *kvm, struct kvm_vcpu *vcpu,
601 struct sev_data_launch_update_vmsa vmsa;
602 struct vcpu_svm *svm = to_svm(vcpu);
605 /* Perform some pre-encryption checks against the VMSA */
606 ret = sev_es_sync_vmsa(svm);
611 * The LAUNCH_UPDATE_VMSA command will perform in-place encryption of
612 * the VMSA memory content (i.e it will write the same memory region
613 * with the guest's key), so invalidate it first.
615 clflush_cache_range(svm->vmsa, PAGE_SIZE);
618 vmsa.handle = to_kvm_svm(kvm)->sev_info.handle;
619 vmsa.address = __sme_pa(svm->vmsa);
620 vmsa.len = PAGE_SIZE;
621 ret = sev_issue_cmd(kvm, SEV_CMD_LAUNCH_UPDATE_VMSA, &vmsa, error);
625 vcpu->arch.guest_state_protected = true;
629 static int sev_launch_update_vmsa(struct kvm *kvm, struct kvm_sev_cmd *argp)
631 struct kvm_vcpu *vcpu;
634 if (!sev_es_guest(kvm))
637 kvm_for_each_vcpu(i, vcpu, kvm) {
638 ret = mutex_lock_killable(&vcpu->mutex);
642 ret = __sev_launch_update_vmsa(kvm, vcpu, &argp->error);
644 mutex_unlock(&vcpu->mutex);
652 static int sev_launch_measure(struct kvm *kvm, struct kvm_sev_cmd *argp)
654 void __user *measure = (void __user *)(uintptr_t)argp->data;
655 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
656 struct sev_data_launch_measure data;
657 struct kvm_sev_launch_measure params;
658 void __user *p = NULL;
665 if (copy_from_user(¶ms, measure, sizeof(params)))
668 memset(&data, 0, sizeof(data));
670 /* User wants to query the blob length */
674 p = (void __user *)(uintptr_t)params.uaddr;
676 if (params.len > SEV_FW_BLOB_MAX_SIZE)
679 blob = kmalloc(params.len, GFP_KERNEL_ACCOUNT);
683 data.address = __psp_pa(blob);
684 data.len = params.len;
688 data.handle = sev->handle;
689 ret = sev_issue_cmd(kvm, SEV_CMD_LAUNCH_MEASURE, &data, &argp->error);
692 * If we query the session length, FW responded with expected data.
701 if (copy_to_user(p, blob, params.len))
706 params.len = data.len;
707 if (copy_to_user(measure, ¶ms, sizeof(params)))
714 static int sev_launch_finish(struct kvm *kvm, struct kvm_sev_cmd *argp)
716 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
717 struct sev_data_launch_finish data;
722 data.handle = sev->handle;
723 return sev_issue_cmd(kvm, SEV_CMD_LAUNCH_FINISH, &data, &argp->error);
726 static int sev_guest_status(struct kvm *kvm, struct kvm_sev_cmd *argp)
728 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
729 struct kvm_sev_guest_status params;
730 struct sev_data_guest_status data;
736 memset(&data, 0, sizeof(data));
738 data.handle = sev->handle;
739 ret = sev_issue_cmd(kvm, SEV_CMD_GUEST_STATUS, &data, &argp->error);
743 params.policy = data.policy;
744 params.state = data.state;
745 params.handle = data.handle;
747 if (copy_to_user((void __user *)(uintptr_t)argp->data, ¶ms, sizeof(params)))
753 static int __sev_issue_dbg_cmd(struct kvm *kvm, unsigned long src,
754 unsigned long dst, int size,
755 int *error, bool enc)
757 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
758 struct sev_data_dbg data;
761 data.handle = sev->handle;
766 return sev_issue_cmd(kvm,
767 enc ? SEV_CMD_DBG_ENCRYPT : SEV_CMD_DBG_DECRYPT,
771 static int __sev_dbg_decrypt(struct kvm *kvm, unsigned long src_paddr,
772 unsigned long dst_paddr, int sz, int *err)
777 * Its safe to read more than we are asked, caller should ensure that
778 * destination has enough space.
780 offset = src_paddr & 15;
781 src_paddr = round_down(src_paddr, 16);
782 sz = round_up(sz + offset, 16);
784 return __sev_issue_dbg_cmd(kvm, src_paddr, dst_paddr, sz, err, false);
787 static int __sev_dbg_decrypt_user(struct kvm *kvm, unsigned long paddr,
788 void __user *dst_uaddr,
789 unsigned long dst_paddr,
792 struct page *tpage = NULL;
795 /* if inputs are not 16-byte then use intermediate buffer */
796 if (!IS_ALIGNED(dst_paddr, 16) ||
797 !IS_ALIGNED(paddr, 16) ||
798 !IS_ALIGNED(size, 16)) {
799 tpage = (void *)alloc_page(GFP_KERNEL);
803 dst_paddr = __sme_page_pa(tpage);
806 ret = __sev_dbg_decrypt(kvm, paddr, dst_paddr, size, err);
812 if (copy_to_user(dst_uaddr, page_address(tpage) + offset, size))
823 static int __sev_dbg_encrypt_user(struct kvm *kvm, unsigned long paddr,
825 unsigned long dst_paddr,
826 void __user *dst_vaddr,
827 int size, int *error)
829 struct page *src_tpage = NULL;
830 struct page *dst_tpage = NULL;
833 /* If source buffer is not aligned then use an intermediate buffer */
834 if (!IS_ALIGNED((unsigned long)vaddr, 16)) {
835 src_tpage = alloc_page(GFP_KERNEL);
839 if (copy_from_user(page_address(src_tpage), vaddr, size)) {
840 __free_page(src_tpage);
844 paddr = __sme_page_pa(src_tpage);
848 * If destination buffer or length is not aligned then do read-modify-write:
849 * - decrypt destination in an intermediate buffer
850 * - copy the source buffer in an intermediate buffer
851 * - use the intermediate buffer as source buffer
853 if (!IS_ALIGNED((unsigned long)dst_vaddr, 16) || !IS_ALIGNED(size, 16)) {
856 dst_tpage = alloc_page(GFP_KERNEL);
862 ret = __sev_dbg_decrypt(kvm, dst_paddr,
863 __sme_page_pa(dst_tpage), size, error);
868 * If source is kernel buffer then use memcpy() otherwise
871 dst_offset = dst_paddr & 15;
874 memcpy(page_address(dst_tpage) + dst_offset,
875 page_address(src_tpage), size);
877 if (copy_from_user(page_address(dst_tpage) + dst_offset,
884 paddr = __sme_page_pa(dst_tpage);
885 dst_paddr = round_down(dst_paddr, 16);
886 len = round_up(size, 16);
889 ret = __sev_issue_dbg_cmd(kvm, paddr, dst_paddr, len, error, true);
893 __free_page(src_tpage);
895 __free_page(dst_tpage);
899 static int sev_dbg_crypt(struct kvm *kvm, struct kvm_sev_cmd *argp, bool dec)
901 unsigned long vaddr, vaddr_end, next_vaddr;
902 unsigned long dst_vaddr;
903 struct page **src_p, **dst_p;
904 struct kvm_sev_dbg debug;
912 if (copy_from_user(&debug, (void __user *)(uintptr_t)argp->data, sizeof(debug)))
915 if (!debug.len || debug.src_uaddr + debug.len < debug.src_uaddr)
917 if (!debug.dst_uaddr)
920 vaddr = debug.src_uaddr;
922 vaddr_end = vaddr + size;
923 dst_vaddr = debug.dst_uaddr;
925 for (; vaddr < vaddr_end; vaddr = next_vaddr) {
926 int len, s_off, d_off;
928 /* lock userspace source and destination page */
929 src_p = sev_pin_memory(kvm, vaddr & PAGE_MASK, PAGE_SIZE, &n, 0);
931 return PTR_ERR(src_p);
933 dst_p = sev_pin_memory(kvm, dst_vaddr & PAGE_MASK, PAGE_SIZE, &n, 1);
935 sev_unpin_memory(kvm, src_p, n);
936 return PTR_ERR(dst_p);
940 * Flush (on non-coherent CPUs) before DBG_{DE,EN}CRYPT read or modify
941 * the pages; flush the destination too so that future accesses do not
944 sev_clflush_pages(src_p, 1);
945 sev_clflush_pages(dst_p, 1);
948 * Since user buffer may not be page aligned, calculate the
949 * offset within the page.
951 s_off = vaddr & ~PAGE_MASK;
952 d_off = dst_vaddr & ~PAGE_MASK;
953 len = min_t(size_t, (PAGE_SIZE - s_off), size);
956 ret = __sev_dbg_decrypt_user(kvm,
957 __sme_page_pa(src_p[0]) + s_off,
958 (void __user *)dst_vaddr,
959 __sme_page_pa(dst_p[0]) + d_off,
962 ret = __sev_dbg_encrypt_user(kvm,
963 __sme_page_pa(src_p[0]) + s_off,
964 (void __user *)vaddr,
965 __sme_page_pa(dst_p[0]) + d_off,
966 (void __user *)dst_vaddr,
969 sev_unpin_memory(kvm, src_p, n);
970 sev_unpin_memory(kvm, dst_p, n);
975 next_vaddr = vaddr + len;
976 dst_vaddr = dst_vaddr + len;
983 static int sev_launch_secret(struct kvm *kvm, struct kvm_sev_cmd *argp)
985 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
986 struct sev_data_launch_secret data;
987 struct kvm_sev_launch_secret params;
996 if (copy_from_user(¶ms, (void __user *)(uintptr_t)argp->data, sizeof(params)))
999 pages = sev_pin_memory(kvm, params.guest_uaddr, params.guest_len, &n, 1);
1001 return PTR_ERR(pages);
1004 * Flush (on non-coherent CPUs) before LAUNCH_SECRET encrypts pages in
1005 * place; the cache may contain the data that was written unencrypted.
1007 sev_clflush_pages(pages, n);
1010 * The secret must be copied into contiguous memory region, lets verify
1011 * that userspace memory pages are contiguous before we issue command.
1013 if (get_num_contig_pages(0, pages, n) != n) {
1015 goto e_unpin_memory;
1018 memset(&data, 0, sizeof(data));
1020 offset = params.guest_uaddr & (PAGE_SIZE - 1);
1021 data.guest_address = __sme_page_pa(pages[0]) + offset;
1022 data.guest_len = params.guest_len;
1024 blob = psp_copy_user_blob(params.trans_uaddr, params.trans_len);
1026 ret = PTR_ERR(blob);
1027 goto e_unpin_memory;
1030 data.trans_address = __psp_pa(blob);
1031 data.trans_len = params.trans_len;
1033 hdr = psp_copy_user_blob(params.hdr_uaddr, params.hdr_len);
1038 data.hdr_address = __psp_pa(hdr);
1039 data.hdr_len = params.hdr_len;
1041 data.handle = sev->handle;
1042 ret = sev_issue_cmd(kvm, SEV_CMD_LAUNCH_UPDATE_SECRET, &data, &argp->error);
1049 /* content of memory is updated, mark pages dirty */
1050 for (i = 0; i < n; i++) {
1051 set_page_dirty_lock(pages[i]);
1052 mark_page_accessed(pages[i]);
1054 sev_unpin_memory(kvm, pages, n);
1058 static int sev_get_attestation_report(struct kvm *kvm, struct kvm_sev_cmd *argp)
1060 void __user *report = (void __user *)(uintptr_t)argp->data;
1061 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
1062 struct sev_data_attestation_report data;
1063 struct kvm_sev_attestation_report params;
1068 if (!sev_guest(kvm))
1071 if (copy_from_user(¶ms, (void __user *)(uintptr_t)argp->data, sizeof(params)))
1074 memset(&data, 0, sizeof(data));
1076 /* User wants to query the blob length */
1080 p = (void __user *)(uintptr_t)params.uaddr;
1082 if (params.len > SEV_FW_BLOB_MAX_SIZE)
1085 blob = kmalloc(params.len, GFP_KERNEL_ACCOUNT);
1089 data.address = __psp_pa(blob);
1090 data.len = params.len;
1091 memcpy(data.mnonce, params.mnonce, sizeof(params.mnonce));
1094 data.handle = sev->handle;
1095 ret = sev_issue_cmd(kvm, SEV_CMD_ATTESTATION_REPORT, &data, &argp->error);
1097 * If we query the session length, FW responded with expected data.
1106 if (copy_to_user(p, blob, params.len))
1111 params.len = data.len;
1112 if (copy_to_user(report, ¶ms, sizeof(params)))
1119 /* Userspace wants to query session length. */
1121 __sev_send_start_query_session_length(struct kvm *kvm, struct kvm_sev_cmd *argp,
1122 struct kvm_sev_send_start *params)
1124 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
1125 struct sev_data_send_start data;
1128 memset(&data, 0, sizeof(data));
1129 data.handle = sev->handle;
1130 ret = sev_issue_cmd(kvm, SEV_CMD_SEND_START, &data, &argp->error);
1132 params->session_len = data.session_len;
1133 if (copy_to_user((void __user *)(uintptr_t)argp->data, params,
1134 sizeof(struct kvm_sev_send_start)))
1140 static int sev_send_start(struct kvm *kvm, struct kvm_sev_cmd *argp)
1142 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
1143 struct sev_data_send_start data;
1144 struct kvm_sev_send_start params;
1145 void *amd_certs, *session_data;
1146 void *pdh_cert, *plat_certs;
1149 if (!sev_guest(kvm))
1152 if (copy_from_user(¶ms, (void __user *)(uintptr_t)argp->data,
1153 sizeof(struct kvm_sev_send_start)))
1156 /* if session_len is zero, userspace wants to query the session length */
1157 if (!params.session_len)
1158 return __sev_send_start_query_session_length(kvm, argp,
1161 /* some sanity checks */
1162 if (!params.pdh_cert_uaddr || !params.pdh_cert_len ||
1163 !params.session_uaddr || params.session_len > SEV_FW_BLOB_MAX_SIZE)
1166 /* allocate the memory to hold the session data blob */
1167 session_data = kmalloc(params.session_len, GFP_KERNEL_ACCOUNT);
1171 /* copy the certificate blobs from userspace */
1172 pdh_cert = psp_copy_user_blob(params.pdh_cert_uaddr,
1173 params.pdh_cert_len);
1174 if (IS_ERR(pdh_cert)) {
1175 ret = PTR_ERR(pdh_cert);
1176 goto e_free_session;
1179 plat_certs = psp_copy_user_blob(params.plat_certs_uaddr,
1180 params.plat_certs_len);
1181 if (IS_ERR(plat_certs)) {
1182 ret = PTR_ERR(plat_certs);
1186 amd_certs = psp_copy_user_blob(params.amd_certs_uaddr,
1187 params.amd_certs_len);
1188 if (IS_ERR(amd_certs)) {
1189 ret = PTR_ERR(amd_certs);
1190 goto e_free_plat_cert;
1193 /* populate the FW SEND_START field with system physical address */
1194 memset(&data, 0, sizeof(data));
1195 data.pdh_cert_address = __psp_pa(pdh_cert);
1196 data.pdh_cert_len = params.pdh_cert_len;
1197 data.plat_certs_address = __psp_pa(plat_certs);
1198 data.plat_certs_len = params.plat_certs_len;
1199 data.amd_certs_address = __psp_pa(amd_certs);
1200 data.amd_certs_len = params.amd_certs_len;
1201 data.session_address = __psp_pa(session_data);
1202 data.session_len = params.session_len;
1203 data.handle = sev->handle;
1205 ret = sev_issue_cmd(kvm, SEV_CMD_SEND_START, &data, &argp->error);
1207 if (!ret && copy_to_user((void __user *)(uintptr_t)params.session_uaddr,
1208 session_data, params.session_len)) {
1210 goto e_free_amd_cert;
1213 params.policy = data.policy;
1214 params.session_len = data.session_len;
1215 if (copy_to_user((void __user *)(uintptr_t)argp->data, ¶ms,
1216 sizeof(struct kvm_sev_send_start)))
1226 kfree(session_data);
1230 /* Userspace wants to query either header or trans length. */
1232 __sev_send_update_data_query_lengths(struct kvm *kvm, struct kvm_sev_cmd *argp,
1233 struct kvm_sev_send_update_data *params)
1235 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
1236 struct sev_data_send_update_data data;
1239 memset(&data, 0, sizeof(data));
1240 data.handle = sev->handle;
1241 ret = sev_issue_cmd(kvm, SEV_CMD_SEND_UPDATE_DATA, &data, &argp->error);
1243 params->hdr_len = data.hdr_len;
1244 params->trans_len = data.trans_len;
1246 if (copy_to_user((void __user *)(uintptr_t)argp->data, params,
1247 sizeof(struct kvm_sev_send_update_data)))
1253 static int sev_send_update_data(struct kvm *kvm, struct kvm_sev_cmd *argp)
1255 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
1256 struct sev_data_send_update_data data;
1257 struct kvm_sev_send_update_data params;
1258 void *hdr, *trans_data;
1259 struct page **guest_page;
1263 if (!sev_guest(kvm))
1266 if (copy_from_user(¶ms, (void __user *)(uintptr_t)argp->data,
1267 sizeof(struct kvm_sev_send_update_data)))
1270 /* userspace wants to query either header or trans length */
1271 if (!params.trans_len || !params.hdr_len)
1272 return __sev_send_update_data_query_lengths(kvm, argp, ¶ms);
1274 if (!params.trans_uaddr || !params.guest_uaddr ||
1275 !params.guest_len || !params.hdr_uaddr)
1278 /* Check if we are crossing the page boundary */
1279 offset = params.guest_uaddr & (PAGE_SIZE - 1);
1280 if ((params.guest_len + offset > PAGE_SIZE))
1283 /* Pin guest memory */
1284 guest_page = sev_pin_memory(kvm, params.guest_uaddr & PAGE_MASK,
1286 if (IS_ERR(guest_page))
1287 return PTR_ERR(guest_page);
1289 /* allocate memory for header and transport buffer */
1291 hdr = kmalloc(params.hdr_len, GFP_KERNEL_ACCOUNT);
1295 trans_data = kmalloc(params.trans_len, GFP_KERNEL_ACCOUNT);
1299 memset(&data, 0, sizeof(data));
1300 data.hdr_address = __psp_pa(hdr);
1301 data.hdr_len = params.hdr_len;
1302 data.trans_address = __psp_pa(trans_data);
1303 data.trans_len = params.trans_len;
1305 /* The SEND_UPDATE_DATA command requires C-bit to be always set. */
1306 data.guest_address = (page_to_pfn(guest_page[0]) << PAGE_SHIFT) + offset;
1307 data.guest_address |= sev_me_mask;
1308 data.guest_len = params.guest_len;
1309 data.handle = sev->handle;
1311 ret = sev_issue_cmd(kvm, SEV_CMD_SEND_UPDATE_DATA, &data, &argp->error);
1314 goto e_free_trans_data;
1316 /* copy transport buffer to user space */
1317 if (copy_to_user((void __user *)(uintptr_t)params.trans_uaddr,
1318 trans_data, params.trans_len)) {
1320 goto e_free_trans_data;
1323 /* Copy packet header to userspace. */
1324 if (copy_to_user((void __user *)(uintptr_t)params.hdr_uaddr, hdr,
1333 sev_unpin_memory(kvm, guest_page, n);
1338 static int sev_send_finish(struct kvm *kvm, struct kvm_sev_cmd *argp)
1340 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
1341 struct sev_data_send_finish data;
1343 if (!sev_guest(kvm))
1346 data.handle = sev->handle;
1347 return sev_issue_cmd(kvm, SEV_CMD_SEND_FINISH, &data, &argp->error);
1350 static int sev_send_cancel(struct kvm *kvm, struct kvm_sev_cmd *argp)
1352 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
1353 struct sev_data_send_cancel data;
1355 if (!sev_guest(kvm))
1358 data.handle = sev->handle;
1359 return sev_issue_cmd(kvm, SEV_CMD_SEND_CANCEL, &data, &argp->error);
1362 static int sev_receive_start(struct kvm *kvm, struct kvm_sev_cmd *argp)
1364 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
1365 struct sev_data_receive_start start;
1366 struct kvm_sev_receive_start params;
1367 int *error = &argp->error;
1372 if (!sev_guest(kvm))
1375 /* Get parameter from the userspace */
1376 if (copy_from_user(¶ms, (void __user *)(uintptr_t)argp->data,
1377 sizeof(struct kvm_sev_receive_start)))
1380 /* some sanity checks */
1381 if (!params.pdh_uaddr || !params.pdh_len ||
1382 !params.session_uaddr || !params.session_len)
1385 pdh_data = psp_copy_user_blob(params.pdh_uaddr, params.pdh_len);
1386 if (IS_ERR(pdh_data))
1387 return PTR_ERR(pdh_data);
1389 session_data = psp_copy_user_blob(params.session_uaddr,
1390 params.session_len);
1391 if (IS_ERR(session_data)) {
1392 ret = PTR_ERR(session_data);
1396 memset(&start, 0, sizeof(start));
1397 start.handle = params.handle;
1398 start.policy = params.policy;
1399 start.pdh_cert_address = __psp_pa(pdh_data);
1400 start.pdh_cert_len = params.pdh_len;
1401 start.session_address = __psp_pa(session_data);
1402 start.session_len = params.session_len;
1404 /* create memory encryption context */
1405 ret = __sev_issue_cmd(argp->sev_fd, SEV_CMD_RECEIVE_START, &start,
1408 goto e_free_session;
1410 /* Bind ASID to this guest */
1411 ret = sev_bind_asid(kvm, start.handle, error);
1413 sev_decommission(start.handle);
1414 goto e_free_session;
1417 params.handle = start.handle;
1418 if (copy_to_user((void __user *)(uintptr_t)argp->data,
1419 ¶ms, sizeof(struct kvm_sev_receive_start))) {
1421 sev_unbind_asid(kvm, start.handle);
1422 goto e_free_session;
1425 sev->handle = start.handle;
1426 sev->fd = argp->sev_fd;
1429 kfree(session_data);
1436 static int sev_receive_update_data(struct kvm *kvm, struct kvm_sev_cmd *argp)
1438 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
1439 struct kvm_sev_receive_update_data params;
1440 struct sev_data_receive_update_data data;
1441 void *hdr = NULL, *trans = NULL;
1442 struct page **guest_page;
1446 if (!sev_guest(kvm))
1449 if (copy_from_user(¶ms, (void __user *)(uintptr_t)argp->data,
1450 sizeof(struct kvm_sev_receive_update_data)))
1453 if (!params.hdr_uaddr || !params.hdr_len ||
1454 !params.guest_uaddr || !params.guest_len ||
1455 !params.trans_uaddr || !params.trans_len)
1458 /* Check if we are crossing the page boundary */
1459 offset = params.guest_uaddr & (PAGE_SIZE - 1);
1460 if ((params.guest_len + offset > PAGE_SIZE))
1463 hdr = psp_copy_user_blob(params.hdr_uaddr, params.hdr_len);
1465 return PTR_ERR(hdr);
1467 trans = psp_copy_user_blob(params.trans_uaddr, params.trans_len);
1468 if (IS_ERR(trans)) {
1469 ret = PTR_ERR(trans);
1473 memset(&data, 0, sizeof(data));
1474 data.hdr_address = __psp_pa(hdr);
1475 data.hdr_len = params.hdr_len;
1476 data.trans_address = __psp_pa(trans);
1477 data.trans_len = params.trans_len;
1479 /* Pin guest memory */
1480 guest_page = sev_pin_memory(kvm, params.guest_uaddr & PAGE_MASK,
1482 if (IS_ERR(guest_page)) {
1483 ret = PTR_ERR(guest_page);
1488 * Flush (on non-coherent CPUs) before RECEIVE_UPDATE_DATA, the PSP
1489 * encrypts the written data with the guest's key, and the cache may
1490 * contain dirty, unencrypted data.
1492 sev_clflush_pages(guest_page, n);
1494 /* The RECEIVE_UPDATE_DATA command requires C-bit to be always set. */
1495 data.guest_address = (page_to_pfn(guest_page[0]) << PAGE_SHIFT) + offset;
1496 data.guest_address |= sev_me_mask;
1497 data.guest_len = params.guest_len;
1498 data.handle = sev->handle;
1500 ret = sev_issue_cmd(kvm, SEV_CMD_RECEIVE_UPDATE_DATA, &data,
1503 sev_unpin_memory(kvm, guest_page, n);
1513 static int sev_receive_finish(struct kvm *kvm, struct kvm_sev_cmd *argp)
1515 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
1516 struct sev_data_receive_finish data;
1518 if (!sev_guest(kvm))
1521 data.handle = sev->handle;
1522 return sev_issue_cmd(kvm, SEV_CMD_RECEIVE_FINISH, &data, &argp->error);
1525 static bool cmd_allowed_from_miror(u32 cmd_id)
1528 * Allow mirrors VM to call KVM_SEV_LAUNCH_UPDATE_VMSA to enable SEV-ES
1529 * active mirror VMs. Also allow the debugging and status commands.
1531 if (cmd_id == KVM_SEV_LAUNCH_UPDATE_VMSA ||
1532 cmd_id == KVM_SEV_GUEST_STATUS || cmd_id == KVM_SEV_DBG_DECRYPT ||
1533 cmd_id == KVM_SEV_DBG_ENCRYPT)
1539 int svm_mem_enc_op(struct kvm *kvm, void __user *argp)
1541 struct kvm_sev_cmd sev_cmd;
1550 if (copy_from_user(&sev_cmd, argp, sizeof(struct kvm_sev_cmd)))
1553 mutex_lock(&kvm->lock);
1555 /* Only the enc_context_owner handles some memory enc operations. */
1556 if (is_mirroring_enc_context(kvm) &&
1557 !cmd_allowed_from_miror(sev_cmd.id)) {
1562 switch (sev_cmd.id) {
1563 case KVM_SEV_ES_INIT:
1564 if (!sev_es_enabled) {
1570 r = sev_guest_init(kvm, &sev_cmd);
1572 case KVM_SEV_LAUNCH_START:
1573 r = sev_launch_start(kvm, &sev_cmd);
1575 case KVM_SEV_LAUNCH_UPDATE_DATA:
1576 r = sev_launch_update_data(kvm, &sev_cmd);
1578 case KVM_SEV_LAUNCH_UPDATE_VMSA:
1579 r = sev_launch_update_vmsa(kvm, &sev_cmd);
1581 case KVM_SEV_LAUNCH_MEASURE:
1582 r = sev_launch_measure(kvm, &sev_cmd);
1584 case KVM_SEV_LAUNCH_FINISH:
1585 r = sev_launch_finish(kvm, &sev_cmd);
1587 case KVM_SEV_GUEST_STATUS:
1588 r = sev_guest_status(kvm, &sev_cmd);
1590 case KVM_SEV_DBG_DECRYPT:
1591 r = sev_dbg_crypt(kvm, &sev_cmd, true);
1593 case KVM_SEV_DBG_ENCRYPT:
1594 r = sev_dbg_crypt(kvm, &sev_cmd, false);
1596 case KVM_SEV_LAUNCH_SECRET:
1597 r = sev_launch_secret(kvm, &sev_cmd);
1599 case KVM_SEV_GET_ATTESTATION_REPORT:
1600 r = sev_get_attestation_report(kvm, &sev_cmd);
1602 case KVM_SEV_SEND_START:
1603 r = sev_send_start(kvm, &sev_cmd);
1605 case KVM_SEV_SEND_UPDATE_DATA:
1606 r = sev_send_update_data(kvm, &sev_cmd);
1608 case KVM_SEV_SEND_FINISH:
1609 r = sev_send_finish(kvm, &sev_cmd);
1611 case KVM_SEV_SEND_CANCEL:
1612 r = sev_send_cancel(kvm, &sev_cmd);
1614 case KVM_SEV_RECEIVE_START:
1615 r = sev_receive_start(kvm, &sev_cmd);
1617 case KVM_SEV_RECEIVE_UPDATE_DATA:
1618 r = sev_receive_update_data(kvm, &sev_cmd);
1620 case KVM_SEV_RECEIVE_FINISH:
1621 r = sev_receive_finish(kvm, &sev_cmd);
1628 if (copy_to_user(argp, &sev_cmd, sizeof(struct kvm_sev_cmd)))
1632 mutex_unlock(&kvm->lock);
1636 int svm_register_enc_region(struct kvm *kvm,
1637 struct kvm_enc_region *range)
1639 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
1640 struct enc_region *region;
1643 if (!sev_guest(kvm))
1646 /* If kvm is mirroring encryption context it isn't responsible for it */
1647 if (is_mirroring_enc_context(kvm))
1650 if (range->addr > ULONG_MAX || range->size > ULONG_MAX)
1653 region = kzalloc(sizeof(*region), GFP_KERNEL_ACCOUNT);
1657 mutex_lock(&kvm->lock);
1658 region->pages = sev_pin_memory(kvm, range->addr, range->size, ®ion->npages, 1);
1659 if (IS_ERR(region->pages)) {
1660 ret = PTR_ERR(region->pages);
1661 mutex_unlock(&kvm->lock);
1665 region->uaddr = range->addr;
1666 region->size = range->size;
1668 list_add_tail(®ion->list, &sev->regions_list);
1669 mutex_unlock(&kvm->lock);
1672 * The guest may change the memory encryption attribute from C=0 -> C=1
1673 * or vice versa for this memory range. Lets make sure caches are
1674 * flushed to ensure that guest data gets written into memory with
1677 sev_clflush_pages(region->pages, region->npages);
1686 static struct enc_region *
1687 find_enc_region(struct kvm *kvm, struct kvm_enc_region *range)
1689 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
1690 struct list_head *head = &sev->regions_list;
1691 struct enc_region *i;
1693 list_for_each_entry(i, head, list) {
1694 if (i->uaddr == range->addr &&
1695 i->size == range->size)
1702 static void __unregister_enc_region_locked(struct kvm *kvm,
1703 struct enc_region *region)
1705 sev_unpin_memory(kvm, region->pages, region->npages);
1706 list_del(®ion->list);
1710 int svm_unregister_enc_region(struct kvm *kvm,
1711 struct kvm_enc_region *range)
1713 struct enc_region *region;
1716 /* If kvm is mirroring encryption context it isn't responsible for it */
1717 if (is_mirroring_enc_context(kvm))
1720 mutex_lock(&kvm->lock);
1722 if (!sev_guest(kvm)) {
1727 region = find_enc_region(kvm, range);
1734 * Ensure that all guest tagged cache entries are flushed before
1735 * releasing the pages back to the system for use. CLFLUSH will
1736 * not do this, so issue a WBINVD.
1738 wbinvd_on_all_cpus();
1740 __unregister_enc_region_locked(kvm, region);
1742 mutex_unlock(&kvm->lock);
1746 mutex_unlock(&kvm->lock);
1750 int svm_vm_copy_asid_from(struct kvm *kvm, unsigned int source_fd)
1752 struct file *source_kvm_file;
1753 struct kvm *source_kvm;
1754 struct kvm_sev_info source_sev, *mirror_sev;
1757 source_kvm_file = fget(source_fd);
1758 if (!file_is_kvm(source_kvm_file)) {
1763 source_kvm = source_kvm_file->private_data;
1764 mutex_lock(&source_kvm->lock);
1766 if (!sev_guest(source_kvm)) {
1768 goto e_source_unlock;
1771 /* Mirrors of mirrors should work, but let's not get silly */
1772 if (is_mirroring_enc_context(source_kvm) || source_kvm == kvm) {
1774 goto e_source_unlock;
1777 memcpy(&source_sev, &to_kvm_svm(source_kvm)->sev_info,
1778 sizeof(source_sev));
1781 * The mirror kvm holds an enc_context_owner ref so its asid can't
1782 * disappear until we're done with it
1784 kvm_get_kvm(source_kvm);
1786 fput(source_kvm_file);
1787 mutex_unlock(&source_kvm->lock);
1788 mutex_lock(&kvm->lock);
1790 if (sev_guest(kvm)) {
1792 goto e_mirror_unlock;
1795 /* Set enc_context_owner and copy its encryption context over */
1796 mirror_sev = &to_kvm_svm(kvm)->sev_info;
1797 mirror_sev->enc_context_owner = source_kvm;
1798 mirror_sev->active = true;
1799 mirror_sev->asid = source_sev.asid;
1800 mirror_sev->fd = source_sev.fd;
1801 mirror_sev->es_active = source_sev.es_active;
1802 mirror_sev->handle = source_sev.handle;
1804 * Do not copy ap_jump_table. Since the mirror does not share the same
1805 * KVM contexts as the original, and they may have different
1809 mutex_unlock(&kvm->lock);
1813 mutex_unlock(&kvm->lock);
1814 kvm_put_kvm(source_kvm);
1817 mutex_unlock(&source_kvm->lock);
1819 if (source_kvm_file)
1820 fput(source_kvm_file);
1824 void sev_vm_destroy(struct kvm *kvm)
1826 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
1827 struct list_head *head = &sev->regions_list;
1828 struct list_head *pos, *q;
1830 if (!sev_guest(kvm))
1833 /* If this is a mirror_kvm release the enc_context_owner and skip sev cleanup */
1834 if (is_mirroring_enc_context(kvm)) {
1835 kvm_put_kvm(sev->enc_context_owner);
1839 mutex_lock(&kvm->lock);
1842 * Ensure that all guest tagged cache entries are flushed before
1843 * releasing the pages back to the system for use. CLFLUSH will
1844 * not do this, so issue a WBINVD.
1846 wbinvd_on_all_cpus();
1849 * if userspace was terminated before unregistering the memory regions
1850 * then lets unpin all the registered memory.
1852 if (!list_empty(head)) {
1853 list_for_each_safe(pos, q, head) {
1854 __unregister_enc_region_locked(kvm,
1855 list_entry(pos, struct enc_region, list));
1860 mutex_unlock(&kvm->lock);
1862 sev_unbind_asid(kvm, sev->handle);
1866 void __init sev_set_cpu_caps(void)
1869 kvm_cpu_cap_clear(X86_FEATURE_SEV);
1870 if (!sev_es_enabled)
1871 kvm_cpu_cap_clear(X86_FEATURE_SEV_ES);
1874 void __init sev_hardware_setup(void)
1876 #ifdef CONFIG_KVM_AMD_SEV
1877 unsigned int eax, ebx, ecx, edx, sev_asid_count, sev_es_asid_count;
1878 bool sev_es_supported = false;
1879 bool sev_supported = false;
1881 if (!sev_enabled || !npt_enabled)
1884 /* Does the CPU support SEV? */
1885 if (!boot_cpu_has(X86_FEATURE_SEV))
1888 /* Retrieve SEV CPUID information */
1889 cpuid(0x8000001f, &eax, &ebx, &ecx, &edx);
1891 /* Set encryption bit location for SEV-ES guests */
1892 sev_enc_bit = ebx & 0x3f;
1894 /* Maximum number of encrypted guests supported simultaneously */
1899 /* Minimum ASID value that should be used for SEV guest */
1901 sev_me_mask = 1UL << (ebx & 0x3f);
1904 * Initialize SEV ASID bitmaps. Allocate space for ASID 0 in the bitmap,
1905 * even though it's never used, so that the bitmap is indexed by the
1908 nr_asids = max_sev_asid + 1;
1909 sev_asid_bitmap = bitmap_zalloc(nr_asids, GFP_KERNEL);
1910 if (!sev_asid_bitmap)
1913 sev_reclaim_asid_bitmap = bitmap_zalloc(nr_asids, GFP_KERNEL);
1914 if (!sev_reclaim_asid_bitmap) {
1915 bitmap_free(sev_asid_bitmap);
1916 sev_asid_bitmap = NULL;
1920 sev_asid_count = max_sev_asid - min_sev_asid + 1;
1921 if (misc_cg_set_capacity(MISC_CG_RES_SEV, sev_asid_count))
1924 pr_info("SEV supported: %u ASIDs\n", sev_asid_count);
1925 sev_supported = true;
1927 /* SEV-ES support requested? */
1928 if (!sev_es_enabled)
1931 /* Does the CPU support SEV-ES? */
1932 if (!boot_cpu_has(X86_FEATURE_SEV_ES))
1935 /* Has the system been allocated ASIDs for SEV-ES? */
1936 if (min_sev_asid == 1)
1939 sev_es_asid_count = min_sev_asid - 1;
1940 if (misc_cg_set_capacity(MISC_CG_RES_SEV_ES, sev_es_asid_count))
1943 pr_info("SEV-ES supported: %u ASIDs\n", sev_es_asid_count);
1944 sev_es_supported = true;
1947 sev_enabled = sev_supported;
1948 sev_es_enabled = sev_es_supported;
1952 void sev_hardware_teardown(void)
1957 /* No need to take sev_bitmap_lock, all VMs have been destroyed. */
1958 sev_flush_asids(1, max_sev_asid);
1960 bitmap_free(sev_asid_bitmap);
1961 bitmap_free(sev_reclaim_asid_bitmap);
1963 misc_cg_set_capacity(MISC_CG_RES_SEV, 0);
1964 misc_cg_set_capacity(MISC_CG_RES_SEV_ES, 0);
1967 int sev_cpu_init(struct svm_cpu_data *sd)
1972 sd->sev_vmcbs = kcalloc(nr_asids, sizeof(void *), GFP_KERNEL);
1980 * Pages used by hardware to hold guest encrypted state must be flushed before
1981 * returning them to the system.
1983 static void sev_flush_guest_memory(struct vcpu_svm *svm, void *va,
1987 * If hardware enforced cache coherency for encrypted mappings of the
1988 * same physical page is supported, nothing to do.
1990 if (boot_cpu_has(X86_FEATURE_SME_COHERENT))
1994 * If the VM Page Flush MSR is supported, use it to flush the page
1995 * (using the page virtual address and the guest ASID).
1997 if (boot_cpu_has(X86_FEATURE_VM_PAGE_FLUSH)) {
1998 struct kvm_sev_info *sev;
1999 unsigned long va_start;
2002 /* Align start and stop to page boundaries. */
2003 va_start = (unsigned long)va;
2004 start = (u64)va_start & PAGE_MASK;
2005 stop = PAGE_ALIGN((u64)va_start + len);
2008 sev = &to_kvm_svm(svm->vcpu.kvm)->sev_info;
2010 while (start < stop) {
2011 wrmsrl(MSR_AMD64_VM_PAGE_FLUSH,
2020 WARN(1, "Address overflow, using WBINVD\n");
2024 * Hardware should always have one of the above features,
2025 * but if not, use WBINVD and issue a warning.
2027 WARN_ONCE(1, "Using WBINVD to flush guest memory\n");
2028 wbinvd_on_all_cpus();
2031 void sev_free_vcpu(struct kvm_vcpu *vcpu)
2033 struct vcpu_svm *svm;
2035 if (!sev_es_guest(vcpu->kvm))
2040 if (vcpu->arch.guest_state_protected)
2041 sev_flush_guest_memory(svm, svm->vmsa, PAGE_SIZE);
2042 __free_page(virt_to_page(svm->vmsa));
2044 if (svm->ghcb_sa_free)
2045 kfree(svm->ghcb_sa);
2048 static void dump_ghcb(struct vcpu_svm *svm)
2050 struct ghcb *ghcb = svm->ghcb;
2053 /* Re-use the dump_invalid_vmcb module parameter */
2054 if (!dump_invalid_vmcb) {
2055 pr_warn_ratelimited("set kvm_amd.dump_invalid_vmcb=1 to dump internal KVM state.\n");
2059 nbits = sizeof(ghcb->save.valid_bitmap) * 8;
2061 pr_err("GHCB (GPA=%016llx):\n", svm->vmcb->control.ghcb_gpa);
2062 pr_err("%-20s%016llx is_valid: %u\n", "sw_exit_code",
2063 ghcb->save.sw_exit_code, ghcb_sw_exit_code_is_valid(ghcb));
2064 pr_err("%-20s%016llx is_valid: %u\n", "sw_exit_info_1",
2065 ghcb->save.sw_exit_info_1, ghcb_sw_exit_info_1_is_valid(ghcb));
2066 pr_err("%-20s%016llx is_valid: %u\n", "sw_exit_info_2",
2067 ghcb->save.sw_exit_info_2, ghcb_sw_exit_info_2_is_valid(ghcb));
2068 pr_err("%-20s%016llx is_valid: %u\n", "sw_scratch",
2069 ghcb->save.sw_scratch, ghcb_sw_scratch_is_valid(ghcb));
2070 pr_err("%-20s%*pb\n", "valid_bitmap", nbits, ghcb->save.valid_bitmap);
2073 static void sev_es_sync_to_ghcb(struct vcpu_svm *svm)
2075 struct kvm_vcpu *vcpu = &svm->vcpu;
2076 struct ghcb *ghcb = svm->ghcb;
2079 * The GHCB protocol so far allows for the following data
2081 * GPRs RAX, RBX, RCX, RDX
2083 * Copy their values, even if they may not have been written during the
2084 * VM-Exit. It's the guest's responsibility to not consume random data.
2086 ghcb_set_rax(ghcb, vcpu->arch.regs[VCPU_REGS_RAX]);
2087 ghcb_set_rbx(ghcb, vcpu->arch.regs[VCPU_REGS_RBX]);
2088 ghcb_set_rcx(ghcb, vcpu->arch.regs[VCPU_REGS_RCX]);
2089 ghcb_set_rdx(ghcb, vcpu->arch.regs[VCPU_REGS_RDX]);
2092 static void sev_es_sync_from_ghcb(struct vcpu_svm *svm)
2094 struct vmcb_control_area *control = &svm->vmcb->control;
2095 struct kvm_vcpu *vcpu = &svm->vcpu;
2096 struct ghcb *ghcb = svm->ghcb;
2100 * The GHCB protocol so far allows for the following data
2102 * GPRs RAX, RBX, RCX, RDX
2106 * VMMCALL allows the guest to provide extra registers. KVM also
2107 * expects RSI for hypercalls, so include that, too.
2109 * Copy their values to the appropriate location if supplied.
2111 memset(vcpu->arch.regs, 0, sizeof(vcpu->arch.regs));
2113 vcpu->arch.regs[VCPU_REGS_RAX] = ghcb_get_rax_if_valid(ghcb);
2114 vcpu->arch.regs[VCPU_REGS_RBX] = ghcb_get_rbx_if_valid(ghcb);
2115 vcpu->arch.regs[VCPU_REGS_RCX] = ghcb_get_rcx_if_valid(ghcb);
2116 vcpu->arch.regs[VCPU_REGS_RDX] = ghcb_get_rdx_if_valid(ghcb);
2117 vcpu->arch.regs[VCPU_REGS_RSI] = ghcb_get_rsi_if_valid(ghcb);
2119 svm->vmcb->save.cpl = ghcb_get_cpl_if_valid(ghcb);
2121 if (ghcb_xcr0_is_valid(ghcb)) {
2122 vcpu->arch.xcr0 = ghcb_get_xcr0(ghcb);
2123 kvm_update_cpuid_runtime(vcpu);
2126 /* Copy the GHCB exit information into the VMCB fields */
2127 exit_code = ghcb_get_sw_exit_code(ghcb);
2128 control->exit_code = lower_32_bits(exit_code);
2129 control->exit_code_hi = upper_32_bits(exit_code);
2130 control->exit_info_1 = ghcb_get_sw_exit_info_1(ghcb);
2131 control->exit_info_2 = ghcb_get_sw_exit_info_2(ghcb);
2133 /* Clear the valid entries fields */
2134 memset(ghcb->save.valid_bitmap, 0, sizeof(ghcb->save.valid_bitmap));
2137 static int sev_es_validate_vmgexit(struct vcpu_svm *svm)
2139 struct kvm_vcpu *vcpu;
2145 /* Only GHCB Usage code 0 is supported */
2146 if (ghcb->ghcb_usage)
2150 * Retrieve the exit code now even though is may not be marked valid
2151 * as it could help with debugging.
2153 exit_code = ghcb_get_sw_exit_code(ghcb);
2155 if (!ghcb_sw_exit_code_is_valid(ghcb) ||
2156 !ghcb_sw_exit_info_1_is_valid(ghcb) ||
2157 !ghcb_sw_exit_info_2_is_valid(ghcb))
2160 switch (ghcb_get_sw_exit_code(ghcb)) {
2161 case SVM_EXIT_READ_DR7:
2163 case SVM_EXIT_WRITE_DR7:
2164 if (!ghcb_rax_is_valid(ghcb))
2167 case SVM_EXIT_RDTSC:
2169 case SVM_EXIT_RDPMC:
2170 if (!ghcb_rcx_is_valid(ghcb))
2173 case SVM_EXIT_CPUID:
2174 if (!ghcb_rax_is_valid(ghcb) ||
2175 !ghcb_rcx_is_valid(ghcb))
2177 if (ghcb_get_rax(ghcb) == 0xd)
2178 if (!ghcb_xcr0_is_valid(ghcb))
2184 if (ghcb_get_sw_exit_info_1(ghcb) & SVM_IOIO_STR_MASK) {
2185 if (!ghcb_sw_scratch_is_valid(ghcb))
2188 if (!(ghcb_get_sw_exit_info_1(ghcb) & SVM_IOIO_TYPE_MASK))
2189 if (!ghcb_rax_is_valid(ghcb))
2194 if (!ghcb_rcx_is_valid(ghcb))
2196 if (ghcb_get_sw_exit_info_1(ghcb)) {
2197 if (!ghcb_rax_is_valid(ghcb) ||
2198 !ghcb_rdx_is_valid(ghcb))
2202 case SVM_EXIT_VMMCALL:
2203 if (!ghcb_rax_is_valid(ghcb) ||
2204 !ghcb_cpl_is_valid(ghcb))
2207 case SVM_EXIT_RDTSCP:
2209 case SVM_EXIT_WBINVD:
2211 case SVM_EXIT_MONITOR:
2212 if (!ghcb_rax_is_valid(ghcb) ||
2213 !ghcb_rcx_is_valid(ghcb) ||
2214 !ghcb_rdx_is_valid(ghcb))
2217 case SVM_EXIT_MWAIT:
2218 if (!ghcb_rax_is_valid(ghcb) ||
2219 !ghcb_rcx_is_valid(ghcb))
2222 case SVM_VMGEXIT_MMIO_READ:
2223 case SVM_VMGEXIT_MMIO_WRITE:
2224 if (!ghcb_sw_scratch_is_valid(ghcb))
2227 case SVM_VMGEXIT_NMI_COMPLETE:
2228 case SVM_VMGEXIT_AP_HLT_LOOP:
2229 case SVM_VMGEXIT_AP_JUMP_TABLE:
2230 case SVM_VMGEXIT_UNSUPPORTED_EVENT:
2241 if (ghcb->ghcb_usage) {
2242 vcpu_unimpl(vcpu, "vmgexit: ghcb usage %#x is not valid\n",
2245 vcpu_unimpl(vcpu, "vmgexit: exit reason %#llx is not valid\n",
2250 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
2251 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_UNEXPECTED_EXIT_REASON;
2252 vcpu->run->internal.ndata = 2;
2253 vcpu->run->internal.data[0] = exit_code;
2254 vcpu->run->internal.data[1] = vcpu->arch.last_vmentry_cpu;
2259 void sev_es_unmap_ghcb(struct vcpu_svm *svm)
2264 if (svm->ghcb_sa_free) {
2266 * The scratch area lives outside the GHCB, so there is a
2267 * buffer that, depending on the operation performed, may
2268 * need to be synced, then freed.
2270 if (svm->ghcb_sa_sync) {
2271 kvm_write_guest(svm->vcpu.kvm,
2272 ghcb_get_sw_scratch(svm->ghcb),
2273 svm->ghcb_sa, svm->ghcb_sa_len);
2274 svm->ghcb_sa_sync = false;
2277 kfree(svm->ghcb_sa);
2278 svm->ghcb_sa = NULL;
2279 svm->ghcb_sa_free = false;
2282 trace_kvm_vmgexit_exit(svm->vcpu.vcpu_id, svm->ghcb);
2284 sev_es_sync_to_ghcb(svm);
2286 kvm_vcpu_unmap(&svm->vcpu, &svm->ghcb_map, true);
2290 void pre_sev_run(struct vcpu_svm *svm, int cpu)
2292 struct svm_cpu_data *sd = per_cpu(svm_data, cpu);
2293 int asid = sev_get_asid(svm->vcpu.kvm);
2295 /* Assign the asid allocated with this SEV guest */
2301 * 1) when different VMCB for the same ASID is to be run on the same host CPU.
2302 * 2) or this VMCB was executed on different host CPU in previous VMRUNs.
2304 if (sd->sev_vmcbs[asid] == svm->vmcb &&
2305 svm->vcpu.arch.last_vmentry_cpu == cpu)
2308 sd->sev_vmcbs[asid] = svm->vmcb;
2309 svm->vmcb->control.tlb_ctl = TLB_CONTROL_FLUSH_ASID;
2310 vmcb_mark_dirty(svm->vmcb, VMCB_ASID);
2313 #define GHCB_SCRATCH_AREA_LIMIT (16ULL * PAGE_SIZE)
2314 static bool setup_vmgexit_scratch(struct vcpu_svm *svm, bool sync, u64 len)
2316 struct vmcb_control_area *control = &svm->vmcb->control;
2317 struct ghcb *ghcb = svm->ghcb;
2318 u64 ghcb_scratch_beg, ghcb_scratch_end;
2319 u64 scratch_gpa_beg, scratch_gpa_end;
2322 scratch_gpa_beg = ghcb_get_sw_scratch(ghcb);
2323 if (!scratch_gpa_beg) {
2324 pr_err("vmgexit: scratch gpa not provided\n");
2328 scratch_gpa_end = scratch_gpa_beg + len;
2329 if (scratch_gpa_end < scratch_gpa_beg) {
2330 pr_err("vmgexit: scratch length (%#llx) not valid for scratch address (%#llx)\n",
2331 len, scratch_gpa_beg);
2335 if ((scratch_gpa_beg & PAGE_MASK) == control->ghcb_gpa) {
2336 /* Scratch area begins within GHCB */
2337 ghcb_scratch_beg = control->ghcb_gpa +
2338 offsetof(struct ghcb, shared_buffer);
2339 ghcb_scratch_end = control->ghcb_gpa +
2340 offsetof(struct ghcb, reserved_1);
2343 * If the scratch area begins within the GHCB, it must be
2344 * completely contained in the GHCB shared buffer area.
2346 if (scratch_gpa_beg < ghcb_scratch_beg ||
2347 scratch_gpa_end > ghcb_scratch_end) {
2348 pr_err("vmgexit: scratch area is outside of GHCB shared buffer area (%#llx - %#llx)\n",
2349 scratch_gpa_beg, scratch_gpa_end);
2353 scratch_va = (void *)svm->ghcb;
2354 scratch_va += (scratch_gpa_beg - control->ghcb_gpa);
2357 * The guest memory must be read into a kernel buffer, so
2360 if (len > GHCB_SCRATCH_AREA_LIMIT) {
2361 pr_err("vmgexit: scratch area exceeds KVM limits (%#llx requested, %#llx limit)\n",
2362 len, GHCB_SCRATCH_AREA_LIMIT);
2365 scratch_va = kzalloc(len, GFP_KERNEL_ACCOUNT);
2369 if (kvm_read_guest(svm->vcpu.kvm, scratch_gpa_beg, scratch_va, len)) {
2370 /* Unable to copy scratch area from guest */
2371 pr_err("vmgexit: kvm_read_guest for scratch area failed\n");
2378 * The scratch area is outside the GHCB. The operation will
2379 * dictate whether the buffer needs to be synced before running
2380 * the vCPU next time (i.e. a read was requested so the data
2381 * must be written back to the guest memory).
2383 svm->ghcb_sa_sync = sync;
2384 svm->ghcb_sa_free = true;
2387 svm->ghcb_sa = scratch_va;
2388 svm->ghcb_sa_len = len;
2393 static void set_ghcb_msr_bits(struct vcpu_svm *svm, u64 value, u64 mask,
2396 svm->vmcb->control.ghcb_gpa &= ~(mask << pos);
2397 svm->vmcb->control.ghcb_gpa |= (value & mask) << pos;
2400 static u64 get_ghcb_msr_bits(struct vcpu_svm *svm, u64 mask, unsigned int pos)
2402 return (svm->vmcb->control.ghcb_gpa >> pos) & mask;
2405 static void set_ghcb_msr(struct vcpu_svm *svm, u64 value)
2407 svm->vmcb->control.ghcb_gpa = value;
2410 static int sev_handle_vmgexit_msr_protocol(struct vcpu_svm *svm)
2412 struct vmcb_control_area *control = &svm->vmcb->control;
2413 struct kvm_vcpu *vcpu = &svm->vcpu;
2417 ghcb_info = control->ghcb_gpa & GHCB_MSR_INFO_MASK;
2419 trace_kvm_vmgexit_msr_protocol_enter(svm->vcpu.vcpu_id,
2422 switch (ghcb_info) {
2423 case GHCB_MSR_SEV_INFO_REQ:
2424 set_ghcb_msr(svm, GHCB_MSR_SEV_INFO(GHCB_VERSION_MAX,
2428 case GHCB_MSR_CPUID_REQ: {
2429 u64 cpuid_fn, cpuid_reg, cpuid_value;
2431 cpuid_fn = get_ghcb_msr_bits(svm,
2432 GHCB_MSR_CPUID_FUNC_MASK,
2433 GHCB_MSR_CPUID_FUNC_POS);
2435 /* Initialize the registers needed by the CPUID intercept */
2436 vcpu->arch.regs[VCPU_REGS_RAX] = cpuid_fn;
2437 vcpu->arch.regs[VCPU_REGS_RCX] = 0;
2439 ret = svm_invoke_exit_handler(vcpu, SVM_EXIT_CPUID);
2445 cpuid_reg = get_ghcb_msr_bits(svm,
2446 GHCB_MSR_CPUID_REG_MASK,
2447 GHCB_MSR_CPUID_REG_POS);
2449 cpuid_value = vcpu->arch.regs[VCPU_REGS_RAX];
2450 else if (cpuid_reg == 1)
2451 cpuid_value = vcpu->arch.regs[VCPU_REGS_RBX];
2452 else if (cpuid_reg == 2)
2453 cpuid_value = vcpu->arch.regs[VCPU_REGS_RCX];
2455 cpuid_value = vcpu->arch.regs[VCPU_REGS_RDX];
2457 set_ghcb_msr_bits(svm, cpuid_value,
2458 GHCB_MSR_CPUID_VALUE_MASK,
2459 GHCB_MSR_CPUID_VALUE_POS);
2461 set_ghcb_msr_bits(svm, GHCB_MSR_CPUID_RESP,
2466 case GHCB_MSR_TERM_REQ: {
2467 u64 reason_set, reason_code;
2469 reason_set = get_ghcb_msr_bits(svm,
2470 GHCB_MSR_TERM_REASON_SET_MASK,
2471 GHCB_MSR_TERM_REASON_SET_POS);
2472 reason_code = get_ghcb_msr_bits(svm,
2473 GHCB_MSR_TERM_REASON_MASK,
2474 GHCB_MSR_TERM_REASON_POS);
2475 pr_info("SEV-ES guest requested termination: %#llx:%#llx\n",
2476 reason_set, reason_code);
2483 trace_kvm_vmgexit_msr_protocol_exit(svm->vcpu.vcpu_id,
2484 control->ghcb_gpa, ret);
2489 int sev_handle_vmgexit(struct kvm_vcpu *vcpu)
2491 struct vcpu_svm *svm = to_svm(vcpu);
2492 struct vmcb_control_area *control = &svm->vmcb->control;
2493 u64 ghcb_gpa, exit_code;
2497 /* Validate the GHCB */
2498 ghcb_gpa = control->ghcb_gpa;
2499 if (ghcb_gpa & GHCB_MSR_INFO_MASK)
2500 return sev_handle_vmgexit_msr_protocol(svm);
2503 vcpu_unimpl(vcpu, "vmgexit: GHCB gpa is not set\n");
2507 if (kvm_vcpu_map(vcpu, ghcb_gpa >> PAGE_SHIFT, &svm->ghcb_map)) {
2508 /* Unable to map GHCB from guest */
2509 vcpu_unimpl(vcpu, "vmgexit: error mapping GHCB [%#llx] from guest\n",
2514 svm->ghcb = svm->ghcb_map.hva;
2515 ghcb = svm->ghcb_map.hva;
2517 trace_kvm_vmgexit_enter(vcpu->vcpu_id, ghcb);
2519 exit_code = ghcb_get_sw_exit_code(ghcb);
2521 ret = sev_es_validate_vmgexit(svm);
2525 sev_es_sync_from_ghcb(svm);
2526 ghcb_set_sw_exit_info_1(ghcb, 0);
2527 ghcb_set_sw_exit_info_2(ghcb, 0);
2530 switch (exit_code) {
2531 case SVM_VMGEXIT_MMIO_READ:
2532 if (!setup_vmgexit_scratch(svm, true, control->exit_info_2))
2535 ret = kvm_sev_es_mmio_read(vcpu,
2536 control->exit_info_1,
2537 control->exit_info_2,
2540 case SVM_VMGEXIT_MMIO_WRITE:
2541 if (!setup_vmgexit_scratch(svm, false, control->exit_info_2))
2544 ret = kvm_sev_es_mmio_write(vcpu,
2545 control->exit_info_1,
2546 control->exit_info_2,
2549 case SVM_VMGEXIT_NMI_COMPLETE:
2550 ret = svm_invoke_exit_handler(vcpu, SVM_EXIT_IRET);
2552 case SVM_VMGEXIT_AP_HLT_LOOP:
2553 ret = kvm_emulate_ap_reset_hold(vcpu);
2555 case SVM_VMGEXIT_AP_JUMP_TABLE: {
2556 struct kvm_sev_info *sev = &to_kvm_svm(vcpu->kvm)->sev_info;
2558 switch (control->exit_info_1) {
2560 /* Set AP jump table address */
2561 sev->ap_jump_table = control->exit_info_2;
2564 /* Get AP jump table address */
2565 ghcb_set_sw_exit_info_2(ghcb, sev->ap_jump_table);
2568 pr_err("svm: vmgexit: unsupported AP jump table request - exit_info_1=%#llx\n",
2569 control->exit_info_1);
2570 ghcb_set_sw_exit_info_1(ghcb, 1);
2571 ghcb_set_sw_exit_info_2(ghcb,
2573 SVM_EVTINJ_TYPE_EXEPT |
2580 case SVM_VMGEXIT_UNSUPPORTED_EVENT:
2582 "vmgexit: unsupported event - exit_info_1=%#llx, exit_info_2=%#llx\n",
2583 control->exit_info_1, control->exit_info_2);
2586 ret = svm_invoke_exit_handler(vcpu, exit_code);
2592 int sev_es_string_io(struct vcpu_svm *svm, int size, unsigned int port, int in)
2594 if (!setup_vmgexit_scratch(svm, in, svm->vmcb->control.exit_info_2))
2597 return kvm_sev_es_string_io(&svm->vcpu, size, port,
2598 svm->ghcb_sa, svm->ghcb_sa_len / size, in);
2601 void sev_es_init_vmcb(struct vcpu_svm *svm)
2603 struct kvm_vcpu *vcpu = &svm->vcpu;
2605 svm->vmcb->control.nested_ctl |= SVM_NESTED_CTL_SEV_ES_ENABLE;
2606 svm->vmcb->control.virt_ext |= LBR_CTL_ENABLE_MASK;
2609 * An SEV-ES guest requires a VMSA area that is a separate from the
2610 * VMCB page. Do not include the encryption mask on the VMSA physical
2611 * address since hardware will access it using the guest key.
2613 svm->vmcb->control.vmsa_pa = __pa(svm->vmsa);
2615 /* Can't intercept CR register access, HV can't modify CR registers */
2616 svm_clr_intercept(svm, INTERCEPT_CR0_READ);
2617 svm_clr_intercept(svm, INTERCEPT_CR4_READ);
2618 svm_clr_intercept(svm, INTERCEPT_CR8_READ);
2619 svm_clr_intercept(svm, INTERCEPT_CR0_WRITE);
2620 svm_clr_intercept(svm, INTERCEPT_CR4_WRITE);
2621 svm_clr_intercept(svm, INTERCEPT_CR8_WRITE);
2623 svm_clr_intercept(svm, INTERCEPT_SELECTIVE_CR0);
2625 /* Track EFER/CR register changes */
2626 svm_set_intercept(svm, TRAP_EFER_WRITE);
2627 svm_set_intercept(svm, TRAP_CR0_WRITE);
2628 svm_set_intercept(svm, TRAP_CR4_WRITE);
2629 svm_set_intercept(svm, TRAP_CR8_WRITE);
2631 /* No support for enable_vmware_backdoor */
2632 clr_exception_intercept(svm, GP_VECTOR);
2634 /* Can't intercept XSETBV, HV can't modify XCR0 directly */
2635 svm_clr_intercept(svm, INTERCEPT_XSETBV);
2637 /* Clear intercepts on selected MSRs */
2638 set_msr_interception(vcpu, svm->msrpm, MSR_EFER, 1, 1);
2639 set_msr_interception(vcpu, svm->msrpm, MSR_IA32_CR_PAT, 1, 1);
2640 set_msr_interception(vcpu, svm->msrpm, MSR_IA32_LASTBRANCHFROMIP, 1, 1);
2641 set_msr_interception(vcpu, svm->msrpm, MSR_IA32_LASTBRANCHTOIP, 1, 1);
2642 set_msr_interception(vcpu, svm->msrpm, MSR_IA32_LASTINTFROMIP, 1, 1);
2643 set_msr_interception(vcpu, svm->msrpm, MSR_IA32_LASTINTTOIP, 1, 1);
2646 void sev_es_create_vcpu(struct vcpu_svm *svm)
2649 * Set the GHCB MSR value as per the GHCB specification when creating
2650 * a vCPU for an SEV-ES guest.
2652 set_ghcb_msr(svm, GHCB_MSR_SEV_INFO(GHCB_VERSION_MAX,
2657 void sev_es_prepare_guest_switch(struct vcpu_svm *svm, unsigned int cpu)
2659 struct svm_cpu_data *sd = per_cpu(svm_data, cpu);
2660 struct vmcb_save_area *hostsa;
2663 * As an SEV-ES guest, hardware will restore the host state on VMEXIT,
2664 * of which one step is to perform a VMLOAD. Since hardware does not
2665 * perform a VMSAVE on VMRUN, the host savearea must be updated.
2667 vmsave(__sme_page_pa(sd->save_area));
2669 /* XCR0 is restored on VMEXIT, save the current host value */
2670 hostsa = (struct vmcb_save_area *)(page_address(sd->save_area) + 0x400);
2671 hostsa->xcr0 = xgetbv(XCR_XFEATURE_ENABLED_MASK);
2673 /* PKRU is restored on VMEXIT, save the current host value */
2674 hostsa->pkru = read_pkru();
2676 /* MSR_IA32_XSS is restored on VMEXIT, save the currnet host value */
2677 hostsa->xss = host_xss;
2680 void sev_vcpu_deliver_sipi_vector(struct kvm_vcpu *vcpu, u8 vector)
2682 struct vcpu_svm *svm = to_svm(vcpu);
2684 /* First SIPI: Use the values as initially set by the VMM */
2685 if (!svm->received_first_sipi) {
2686 svm->received_first_sipi = true;
2691 * Subsequent SIPI: Return from an AP Reset Hold VMGEXIT, where
2692 * the guest will set the CS and RIP. Set SW_EXIT_INFO_2 to a
2698 ghcb_set_sw_exit_info_2(svm->ghcb, 1);