xref: /openbmc/linux/arch/x86/kvm/svm/sev.c (revision c4a11bf4)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Kernel-based Virtual Machine driver for Linux
4  *
5  * AMD SVM-SEV support
6  *
7  * Copyright 2010 Red Hat, Inc. and/or its affiliates.
8  */
9 
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 
21 #include <asm/pkru.h>
22 #include <asm/trapnr.h>
23 #include <asm/fpu/xcr.h>
24 
25 #include "x86.h"
26 #include "svm.h"
27 #include "svm_ops.h"
28 #include "cpuid.h"
29 #include "trace.h"
30 
31 #ifndef CONFIG_KVM_AMD_SEV
32 /*
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
35  * module.
36  *
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.
39  *
40  * Below macros allow compilation to succeed.
41  */
42 #define MISC_CG_RES_SEV MISC_CG_RES_TYPES
43 #define MISC_CG_RES_SEV_ES MISC_CG_RES_TYPES
44 #endif
45 
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);
50 
51 /* enable/disable SEV-ES support */
52 static bool sev_es_enabled = true;
53 module_param_named(sev_es, sev_es_enabled, bool, 0444);
54 #else
55 #define sev_enabled false
56 #define sev_es_enabled false
57 #endif /* CONFIG_KVM_AMD_SEV */
58 
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;
68 
69 struct enc_region {
70 	struct list_head list;
71 	unsigned long npages;
72 	struct page **pages;
73 	unsigned long uaddr;
74 	unsigned long size;
75 };
76 
77 /* Called with the sev_bitmap_lock held, or on shutdown  */
78 static int sev_flush_asids(int min_asid, int max_asid)
79 {
80 	int ret, asid, error = 0;
81 
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);
84 	if (asid > max_asid)
85 		return -EBUSY;
86 
87 	/*
88 	 * DEACTIVATE will clear the WBINVD indicator causing DF_FLUSH to fail,
89 	 * so it must be guarded.
90 	 */
91 	down_write(&sev_deactivate_lock);
92 
93 	wbinvd_on_all_cpus();
94 	ret = sev_guest_df_flush(&error);
95 
96 	up_write(&sev_deactivate_lock);
97 
98 	if (ret)
99 		pr_err("SEV: DF_FLUSH failed, ret=%d, error=%#x\n", ret, error);
100 
101 	return ret;
102 }
103 
104 static inline bool is_mirroring_enc_context(struct kvm *kvm)
105 {
106 	return !!to_kvm_svm(kvm)->sev_info.enc_context_owner;
107 }
108 
109 /* Must be called with the sev_bitmap_lock held */
110 static bool __sev_recycle_asids(int min_asid, int max_asid)
111 {
112 	if (sev_flush_asids(min_asid, max_asid))
113 		return false;
114 
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,
117 		   nr_asids);
118 	bitmap_zero(sev_reclaim_asid_bitmap, nr_asids);
119 
120 	return true;
121 }
122 
123 static int sev_asid_new(struct kvm_sev_info *sev)
124 {
125 	int asid, min_asid, max_asid, ret;
126 	bool retry = true;
127 	enum misc_res_type type;
128 
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);
133 	if (ret) {
134 		put_misc_cg(sev->misc_cg);
135 		sev->misc_cg = NULL;
136 		return ret;
137 	}
138 
139 	mutex_lock(&sev_bitmap_lock);
140 
141 	/*
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.
144 	 */
145 	min_asid = sev->es_active ? 1 : min_sev_asid;
146 	max_asid = sev->es_active ? min_sev_asid - 1 : max_sev_asid;
147 again:
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)) {
151 			retry = false;
152 			goto again;
153 		}
154 		mutex_unlock(&sev_bitmap_lock);
155 		ret = -EBUSY;
156 		goto e_uncharge;
157 	}
158 
159 	__set_bit(asid, sev_asid_bitmap);
160 
161 	mutex_unlock(&sev_bitmap_lock);
162 
163 	return asid;
164 e_uncharge:
165 	misc_cg_uncharge(type, sev->misc_cg, 1);
166 	put_misc_cg(sev->misc_cg);
167 	sev->misc_cg = NULL;
168 	return ret;
169 }
170 
171 static int sev_get_asid(struct kvm *kvm)
172 {
173 	struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
174 
175 	return sev->asid;
176 }
177 
178 static void sev_asid_free(struct kvm_sev_info *sev)
179 {
180 	struct svm_cpu_data *sd;
181 	int cpu;
182 	enum misc_res_type type;
183 
184 	mutex_lock(&sev_bitmap_lock);
185 
186 	__set_bit(sev->asid, sev_reclaim_asid_bitmap);
187 
188 	for_each_possible_cpu(cpu) {
189 		sd = per_cpu(svm_data, cpu);
190 		sd->sev_vmcbs[sev->asid] = NULL;
191 	}
192 
193 	mutex_unlock(&sev_bitmap_lock);
194 
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);
198 	sev->misc_cg = NULL;
199 }
200 
201 static void sev_decommission(unsigned int handle)
202 {
203 	struct sev_data_decommission decommission;
204 
205 	if (!handle)
206 		return;
207 
208 	decommission.handle = handle;
209 	sev_guest_decommission(&decommission, NULL);
210 }
211 
212 static void sev_unbind_asid(struct kvm *kvm, unsigned int handle)
213 {
214 	struct sev_data_deactivate deactivate;
215 
216 	if (!handle)
217 		return;
218 
219 	deactivate.handle = handle;
220 
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);
225 
226 	sev_decommission(handle);
227 }
228 
229 static int sev_guest_init(struct kvm *kvm, struct kvm_sev_cmd *argp)
230 {
231 	struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
232 	bool es_active = argp->id == KVM_SEV_ES_INIT;
233 	int asid, ret;
234 
235 	if (kvm->created_vcpus)
236 		return -EINVAL;
237 
238 	ret = -EBUSY;
239 	if (unlikely(sev->active))
240 		return ret;
241 
242 	sev->es_active = es_active;
243 	asid = sev_asid_new(sev);
244 	if (asid < 0)
245 		goto e_no_asid;
246 	sev->asid = asid;
247 
248 	ret = sev_platform_init(&argp->error);
249 	if (ret)
250 		goto e_free;
251 
252 	sev->active = true;
253 	sev->asid = asid;
254 	INIT_LIST_HEAD(&sev->regions_list);
255 
256 	return 0;
257 
258 e_free:
259 	sev_asid_free(sev);
260 	sev->asid = 0;
261 e_no_asid:
262 	sev->es_active = false;
263 	return ret;
264 }
265 
266 static int sev_bind_asid(struct kvm *kvm, unsigned int handle, int *error)
267 {
268 	struct sev_data_activate activate;
269 	int asid = sev_get_asid(kvm);
270 	int ret;
271 
272 	/* activate ASID on the given handle */
273 	activate.handle = handle;
274 	activate.asid   = asid;
275 	ret = sev_guest_activate(&activate, error);
276 
277 	return ret;
278 }
279 
280 static int __sev_issue_cmd(int fd, int id, void *data, int *error)
281 {
282 	struct fd f;
283 	int ret;
284 
285 	f = fdget(fd);
286 	if (!f.file)
287 		return -EBADF;
288 
289 	ret = sev_issue_cmd_external_user(f.file, id, data, error);
290 
291 	fdput(f);
292 	return ret;
293 }
294 
295 static int sev_issue_cmd(struct kvm *kvm, int id, void *data, int *error)
296 {
297 	struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
298 
299 	return __sev_issue_cmd(sev->fd, id, data, error);
300 }
301 
302 static int sev_launch_start(struct kvm *kvm, struct kvm_sev_cmd *argp)
303 {
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;
309 	int ret;
310 
311 	if (!sev_guest(kvm))
312 		return -ENOTTY;
313 
314 	if (copy_from_user(&params, (void __user *)(uintptr_t)argp->data, sizeof(params)))
315 		return -EFAULT;
316 
317 	memset(&start, 0, sizeof(start));
318 
319 	dh_blob = NULL;
320 	if (params.dh_uaddr) {
321 		dh_blob = psp_copy_user_blob(params.dh_uaddr, params.dh_len);
322 		if (IS_ERR(dh_blob))
323 			return PTR_ERR(dh_blob);
324 
325 		start.dh_cert_address = __sme_set(__pa(dh_blob));
326 		start.dh_cert_len = params.dh_len;
327 	}
328 
329 	session_blob = NULL;
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);
334 			goto e_free_dh;
335 		}
336 
337 		start.session_address = __sme_set(__pa(session_blob));
338 		start.session_len = params.session_len;
339 	}
340 
341 	start.handle = params.handle;
342 	start.policy = params.policy;
343 
344 	/* create memory encryption context */
345 	ret = __sev_issue_cmd(argp->sev_fd, SEV_CMD_LAUNCH_START, &start, error);
346 	if (ret)
347 		goto e_free_session;
348 
349 	/* Bind ASID to this guest */
350 	ret = sev_bind_asid(kvm, start.handle, error);
351 	if (ret) {
352 		sev_decommission(start.handle);
353 		goto e_free_session;
354 	}
355 
356 	/* return handle to userspace */
357 	params.handle = start.handle;
358 	if (copy_to_user((void __user *)(uintptr_t)argp->data, &params, sizeof(params))) {
359 		sev_unbind_asid(kvm, start.handle);
360 		ret = -EFAULT;
361 		goto e_free_session;
362 	}
363 
364 	sev->handle = start.handle;
365 	sev->fd = argp->sev_fd;
366 
367 e_free_session:
368 	kfree(session_blob);
369 e_free_dh:
370 	kfree(dh_blob);
371 	return ret;
372 }
373 
374 static struct page **sev_pin_memory(struct kvm *kvm, unsigned long uaddr,
375 				    unsigned long ulen, unsigned long *n,
376 				    int write)
377 {
378 	struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
379 	unsigned long npages, size;
380 	int npinned;
381 	unsigned long locked, lock_limit;
382 	struct page **pages;
383 	unsigned long first, last;
384 	int ret;
385 
386 	lockdep_assert_held(&kvm->lock);
387 
388 	if (ulen == 0 || uaddr + ulen < uaddr)
389 		return ERR_PTR(-EINVAL);
390 
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);
395 
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);
401 	}
402 
403 	if (WARN_ON_ONCE(npages > INT_MAX))
404 		return ERR_PTR(-EINVAL);
405 
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);
410 	else
411 		pages = kmalloc(size, GFP_KERNEL_ACCOUNT);
412 
413 	if (!pages)
414 		return ERR_PTR(-ENOMEM);
415 
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);
420 		ret = -ENOMEM;
421 		goto err;
422 	}
423 
424 	*n = npages;
425 	sev->pages_locked = locked;
426 
427 	return pages;
428 
429 err:
430 	if (npinned > 0)
431 		unpin_user_pages(pages, npinned);
432 
433 	kvfree(pages);
434 	return ERR_PTR(ret);
435 }
436 
437 static void sev_unpin_memory(struct kvm *kvm, struct page **pages,
438 			     unsigned long npages)
439 {
440 	struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
441 
442 	unpin_user_pages(pages, npages);
443 	kvfree(pages);
444 	sev->pages_locked -= npages;
445 }
446 
447 static void sev_clflush_pages(struct page *pages[], unsigned long npages)
448 {
449 	uint8_t *page_virtual;
450 	unsigned long i;
451 
452 	if (this_cpu_has(X86_FEATURE_SME_COHERENT) || npages == 0 ||
453 	    pages == NULL)
454 		return;
455 
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);
460 	}
461 }
462 
463 static unsigned long get_num_contig_pages(unsigned long idx,
464 				struct page **inpages, unsigned long npages)
465 {
466 	unsigned long paddr, next_paddr;
467 	unsigned long i = idx + 1, pages = 1;
468 
469 	/* find the number of contiguous pages starting from idx */
470 	paddr = __sme_page_pa(inpages[idx]);
471 	while (i < npages) {
472 		next_paddr = __sme_page_pa(inpages[i++]);
473 		if ((paddr + PAGE_SIZE) == next_paddr) {
474 			pages++;
475 			paddr = next_paddr;
476 			continue;
477 		}
478 		break;
479 	}
480 
481 	return pages;
482 }
483 
484 static int sev_launch_update_data(struct kvm *kvm, struct kvm_sev_cmd *argp)
485 {
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;
491 	int ret;
492 
493 	if (!sev_guest(kvm))
494 		return -ENOTTY;
495 
496 	if (copy_from_user(&params, (void __user *)(uintptr_t)argp->data, sizeof(params)))
497 		return -EFAULT;
498 
499 	vaddr = params.uaddr;
500 	size = params.len;
501 	vaddr_end = vaddr + size;
502 
503 	/* Lock the user memory. */
504 	inpages = sev_pin_memory(kvm, vaddr, size, &npages, 1);
505 	if (IS_ERR(inpages))
506 		return PTR_ERR(inpages);
507 
508 	/*
509 	 * Flush (on non-coherent CPUs) before LAUNCH_UPDATE encrypts pages in
510 	 * place; the cache may contain the data that was written unencrypted.
511 	 */
512 	sev_clflush_pages(inpages, npages);
513 
514 	data.reserved = 0;
515 	data.handle = sev->handle;
516 
517 	for (i = 0; vaddr < vaddr_end; vaddr = next_vaddr, i += pages) {
518 		int offset, len;
519 
520 		/*
521 		 * If the user buffer is not page-aligned, calculate the offset
522 		 * within the page.
523 		 */
524 		offset = vaddr & (PAGE_SIZE - 1);
525 
526 		/* Calculate the number of pages that can be encrypted in one go. */
527 		pages = get_num_contig_pages(i, inpages, npages);
528 
529 		len = min_t(size_t, ((pages * PAGE_SIZE) - offset), size);
530 
531 		data.len = len;
532 		data.address = __sme_page_pa(inpages[i]) + offset;
533 		ret = sev_issue_cmd(kvm, SEV_CMD_LAUNCH_UPDATE_DATA, &data, &argp->error);
534 		if (ret)
535 			goto e_unpin;
536 
537 		size -= len;
538 		next_vaddr = vaddr + len;
539 	}
540 
541 e_unpin:
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]);
546 	}
547 	/* unlock the user pages */
548 	sev_unpin_memory(kvm, inpages, npages);
549 	return ret;
550 }
551 
552 static int sev_es_sync_vmsa(struct vcpu_svm *svm)
553 {
554 	struct vmcb_save_area *save = &svm->vmcb->save;
555 
556 	/* Check some debug related fields before encrypting the VMSA */
557 	if (svm->vcpu.guest_debug || (save->dr7 & ~DR7_FIXED_1))
558 		return -EINVAL;
559 
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];
569 #ifdef CONFIG_X86_64
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];
578 #endif
579 	save->rip = svm->vcpu.arch.regs[VCPU_REGS_RIP];
580 
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;
586 
587 	/*
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.
592 	 */
593 	memcpy(svm->vmsa, save, sizeof(*save));
594 
595 	return 0;
596 }
597 
598 static int __sev_launch_update_vmsa(struct kvm *kvm, struct kvm_vcpu *vcpu,
599 				    int *error)
600 {
601 	struct sev_data_launch_update_vmsa vmsa;
602 	struct vcpu_svm *svm = to_svm(vcpu);
603 	int ret;
604 
605 	/* Perform some pre-encryption checks against the VMSA */
606 	ret = sev_es_sync_vmsa(svm);
607 	if (ret)
608 		return ret;
609 
610 	/*
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.
614 	 */
615 	clflush_cache_range(svm->vmsa, PAGE_SIZE);
616 
617 	vmsa.reserved = 0;
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);
622 	if (ret)
623 	  return ret;
624 
625 	vcpu->arch.guest_state_protected = true;
626 	return 0;
627 }
628 
629 static int sev_launch_update_vmsa(struct kvm *kvm, struct kvm_sev_cmd *argp)
630 {
631 	struct kvm_vcpu *vcpu;
632 	int i, ret;
633 
634 	if (!sev_es_guest(kvm))
635 		return -ENOTTY;
636 
637 	kvm_for_each_vcpu(i, vcpu, kvm) {
638 		ret = mutex_lock_killable(&vcpu->mutex);
639 		if (ret)
640 			return ret;
641 
642 		ret = __sev_launch_update_vmsa(kvm, vcpu, &argp->error);
643 
644 		mutex_unlock(&vcpu->mutex);
645 		if (ret)
646 			return ret;
647 	}
648 
649 	return 0;
650 }
651 
652 static int sev_launch_measure(struct kvm *kvm, struct kvm_sev_cmd *argp)
653 {
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;
659 	void *blob = NULL;
660 	int ret;
661 
662 	if (!sev_guest(kvm))
663 		return -ENOTTY;
664 
665 	if (copy_from_user(&params, measure, sizeof(params)))
666 		return -EFAULT;
667 
668 	memset(&data, 0, sizeof(data));
669 
670 	/* User wants to query the blob length */
671 	if (!params.len)
672 		goto cmd;
673 
674 	p = (void __user *)(uintptr_t)params.uaddr;
675 	if (p) {
676 		if (params.len > SEV_FW_BLOB_MAX_SIZE)
677 			return -EINVAL;
678 
679 		blob = kmalloc(params.len, GFP_KERNEL_ACCOUNT);
680 		if (!blob)
681 			return -ENOMEM;
682 
683 		data.address = __psp_pa(blob);
684 		data.len = params.len;
685 	}
686 
687 cmd:
688 	data.handle = sev->handle;
689 	ret = sev_issue_cmd(kvm, SEV_CMD_LAUNCH_MEASURE, &data, &argp->error);
690 
691 	/*
692 	 * If we query the session length, FW responded with expected data.
693 	 */
694 	if (!params.len)
695 		goto done;
696 
697 	if (ret)
698 		goto e_free_blob;
699 
700 	if (blob) {
701 		if (copy_to_user(p, blob, params.len))
702 			ret = -EFAULT;
703 	}
704 
705 done:
706 	params.len = data.len;
707 	if (copy_to_user(measure, &params, sizeof(params)))
708 		ret = -EFAULT;
709 e_free_blob:
710 	kfree(blob);
711 	return ret;
712 }
713 
714 static int sev_launch_finish(struct kvm *kvm, struct kvm_sev_cmd *argp)
715 {
716 	struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
717 	struct sev_data_launch_finish data;
718 
719 	if (!sev_guest(kvm))
720 		return -ENOTTY;
721 
722 	data.handle = sev->handle;
723 	return sev_issue_cmd(kvm, SEV_CMD_LAUNCH_FINISH, &data, &argp->error);
724 }
725 
726 static int sev_guest_status(struct kvm *kvm, struct kvm_sev_cmd *argp)
727 {
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;
731 	int ret;
732 
733 	if (!sev_guest(kvm))
734 		return -ENOTTY;
735 
736 	memset(&data, 0, sizeof(data));
737 
738 	data.handle = sev->handle;
739 	ret = sev_issue_cmd(kvm, SEV_CMD_GUEST_STATUS, &data, &argp->error);
740 	if (ret)
741 		return ret;
742 
743 	params.policy = data.policy;
744 	params.state = data.state;
745 	params.handle = data.handle;
746 
747 	if (copy_to_user((void __user *)(uintptr_t)argp->data, &params, sizeof(params)))
748 		ret = -EFAULT;
749 
750 	return ret;
751 }
752 
753 static int __sev_issue_dbg_cmd(struct kvm *kvm, unsigned long src,
754 			       unsigned long dst, int size,
755 			       int *error, bool enc)
756 {
757 	struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
758 	struct sev_data_dbg data;
759 
760 	data.reserved = 0;
761 	data.handle = sev->handle;
762 	data.dst_addr = dst;
763 	data.src_addr = src;
764 	data.len = size;
765 
766 	return sev_issue_cmd(kvm,
767 			     enc ? SEV_CMD_DBG_ENCRYPT : SEV_CMD_DBG_DECRYPT,
768 			     &data, error);
769 }
770 
771 static int __sev_dbg_decrypt(struct kvm *kvm, unsigned long src_paddr,
772 			     unsigned long dst_paddr, int sz, int *err)
773 {
774 	int offset;
775 
776 	/*
777 	 * Its safe to read more than we are asked, caller should ensure that
778 	 * destination has enough space.
779 	 */
780 	offset = src_paddr & 15;
781 	src_paddr = round_down(src_paddr, 16);
782 	sz = round_up(sz + offset, 16);
783 
784 	return __sev_issue_dbg_cmd(kvm, src_paddr, dst_paddr, sz, err, false);
785 }
786 
787 static int __sev_dbg_decrypt_user(struct kvm *kvm, unsigned long paddr,
788 				  void __user *dst_uaddr,
789 				  unsigned long dst_paddr,
790 				  int size, int *err)
791 {
792 	struct page *tpage = NULL;
793 	int ret, offset;
794 
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);
800 		if (!tpage)
801 			return -ENOMEM;
802 
803 		dst_paddr = __sme_page_pa(tpage);
804 	}
805 
806 	ret = __sev_dbg_decrypt(kvm, paddr, dst_paddr, size, err);
807 	if (ret)
808 		goto e_free;
809 
810 	if (tpage) {
811 		offset = paddr & 15;
812 		if (copy_to_user(dst_uaddr, page_address(tpage) + offset, size))
813 			ret = -EFAULT;
814 	}
815 
816 e_free:
817 	if (tpage)
818 		__free_page(tpage);
819 
820 	return ret;
821 }
822 
823 static int __sev_dbg_encrypt_user(struct kvm *kvm, unsigned long paddr,
824 				  void __user *vaddr,
825 				  unsigned long dst_paddr,
826 				  void __user *dst_vaddr,
827 				  int size, int *error)
828 {
829 	struct page *src_tpage = NULL;
830 	struct page *dst_tpage = NULL;
831 	int ret, len = size;
832 
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);
836 		if (!src_tpage)
837 			return -ENOMEM;
838 
839 		if (copy_from_user(page_address(src_tpage), vaddr, size)) {
840 			__free_page(src_tpage);
841 			return -EFAULT;
842 		}
843 
844 		paddr = __sme_page_pa(src_tpage);
845 	}
846 
847 	/*
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
852 	 */
853 	if (!IS_ALIGNED((unsigned long)dst_vaddr, 16) || !IS_ALIGNED(size, 16)) {
854 		int dst_offset;
855 
856 		dst_tpage = alloc_page(GFP_KERNEL);
857 		if (!dst_tpage) {
858 			ret = -ENOMEM;
859 			goto e_free;
860 		}
861 
862 		ret = __sev_dbg_decrypt(kvm, dst_paddr,
863 					__sme_page_pa(dst_tpage), size, error);
864 		if (ret)
865 			goto e_free;
866 
867 		/*
868 		 *  If source is kernel buffer then use memcpy() otherwise
869 		 *  copy_from_user().
870 		 */
871 		dst_offset = dst_paddr & 15;
872 
873 		if (src_tpage)
874 			memcpy(page_address(dst_tpage) + dst_offset,
875 			       page_address(src_tpage), size);
876 		else {
877 			if (copy_from_user(page_address(dst_tpage) + dst_offset,
878 					   vaddr, size)) {
879 				ret = -EFAULT;
880 				goto e_free;
881 			}
882 		}
883 
884 		paddr = __sme_page_pa(dst_tpage);
885 		dst_paddr = round_down(dst_paddr, 16);
886 		len = round_up(size, 16);
887 	}
888 
889 	ret = __sev_issue_dbg_cmd(kvm, paddr, dst_paddr, len, error, true);
890 
891 e_free:
892 	if (src_tpage)
893 		__free_page(src_tpage);
894 	if (dst_tpage)
895 		__free_page(dst_tpage);
896 	return ret;
897 }
898 
899 static int sev_dbg_crypt(struct kvm *kvm, struct kvm_sev_cmd *argp, bool dec)
900 {
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;
905 	unsigned long n;
906 	unsigned int size;
907 	int ret;
908 
909 	if (!sev_guest(kvm))
910 		return -ENOTTY;
911 
912 	if (copy_from_user(&debug, (void __user *)(uintptr_t)argp->data, sizeof(debug)))
913 		return -EFAULT;
914 
915 	if (!debug.len || debug.src_uaddr + debug.len < debug.src_uaddr)
916 		return -EINVAL;
917 	if (!debug.dst_uaddr)
918 		return -EINVAL;
919 
920 	vaddr = debug.src_uaddr;
921 	size = debug.len;
922 	vaddr_end = vaddr + size;
923 	dst_vaddr = debug.dst_uaddr;
924 
925 	for (; vaddr < vaddr_end; vaddr = next_vaddr) {
926 		int len, s_off, d_off;
927 
928 		/* lock userspace source and destination page */
929 		src_p = sev_pin_memory(kvm, vaddr & PAGE_MASK, PAGE_SIZE, &n, 0);
930 		if (IS_ERR(src_p))
931 			return PTR_ERR(src_p);
932 
933 		dst_p = sev_pin_memory(kvm, dst_vaddr & PAGE_MASK, PAGE_SIZE, &n, 1);
934 		if (IS_ERR(dst_p)) {
935 			sev_unpin_memory(kvm, src_p, n);
936 			return PTR_ERR(dst_p);
937 		}
938 
939 		/*
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
942 		 * see stale data.
943 		 */
944 		sev_clflush_pages(src_p, 1);
945 		sev_clflush_pages(dst_p, 1);
946 
947 		/*
948 		 * Since user buffer may not be page aligned, calculate the
949 		 * offset within the page.
950 		 */
951 		s_off = vaddr & ~PAGE_MASK;
952 		d_off = dst_vaddr & ~PAGE_MASK;
953 		len = min_t(size_t, (PAGE_SIZE - s_off), size);
954 
955 		if (dec)
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,
960 						     len, &argp->error);
961 		else
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,
967 						     len, &argp->error);
968 
969 		sev_unpin_memory(kvm, src_p, n);
970 		sev_unpin_memory(kvm, dst_p, n);
971 
972 		if (ret)
973 			goto err;
974 
975 		next_vaddr = vaddr + len;
976 		dst_vaddr = dst_vaddr + len;
977 		size -= len;
978 	}
979 err:
980 	return ret;
981 }
982 
983 static int sev_launch_secret(struct kvm *kvm, struct kvm_sev_cmd *argp)
984 {
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;
988 	struct page **pages;
989 	void *blob, *hdr;
990 	unsigned long n, i;
991 	int ret, offset;
992 
993 	if (!sev_guest(kvm))
994 		return -ENOTTY;
995 
996 	if (copy_from_user(&params, (void __user *)(uintptr_t)argp->data, sizeof(params)))
997 		return -EFAULT;
998 
999 	pages = sev_pin_memory(kvm, params.guest_uaddr, params.guest_len, &n, 1);
1000 	if (IS_ERR(pages))
1001 		return PTR_ERR(pages);
1002 
1003 	/*
1004 	 * Flush (on non-coherent CPUs) before LAUNCH_SECRET encrypts pages in
1005 	 * place; the cache may contain the data that was written unencrypted.
1006 	 */
1007 	sev_clflush_pages(pages, n);
1008 
1009 	/*
1010 	 * The secret must be copied into contiguous memory region, lets verify
1011 	 * that userspace memory pages are contiguous before we issue command.
1012 	 */
1013 	if (get_num_contig_pages(0, pages, n) != n) {
1014 		ret = -EINVAL;
1015 		goto e_unpin_memory;
1016 	}
1017 
1018 	memset(&data, 0, sizeof(data));
1019 
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;
1023 
1024 	blob = psp_copy_user_blob(params.trans_uaddr, params.trans_len);
1025 	if (IS_ERR(blob)) {
1026 		ret = PTR_ERR(blob);
1027 		goto e_unpin_memory;
1028 	}
1029 
1030 	data.trans_address = __psp_pa(blob);
1031 	data.trans_len = params.trans_len;
1032 
1033 	hdr = psp_copy_user_blob(params.hdr_uaddr, params.hdr_len);
1034 	if (IS_ERR(hdr)) {
1035 		ret = PTR_ERR(hdr);
1036 		goto e_free_blob;
1037 	}
1038 	data.hdr_address = __psp_pa(hdr);
1039 	data.hdr_len = params.hdr_len;
1040 
1041 	data.handle = sev->handle;
1042 	ret = sev_issue_cmd(kvm, SEV_CMD_LAUNCH_UPDATE_SECRET, &data, &argp->error);
1043 
1044 	kfree(hdr);
1045 
1046 e_free_blob:
1047 	kfree(blob);
1048 e_unpin_memory:
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]);
1053 	}
1054 	sev_unpin_memory(kvm, pages, n);
1055 	return ret;
1056 }
1057 
1058 static int sev_get_attestation_report(struct kvm *kvm, struct kvm_sev_cmd *argp)
1059 {
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;
1064 	void __user *p;
1065 	void *blob = NULL;
1066 	int ret;
1067 
1068 	if (!sev_guest(kvm))
1069 		return -ENOTTY;
1070 
1071 	if (copy_from_user(&params, (void __user *)(uintptr_t)argp->data, sizeof(params)))
1072 		return -EFAULT;
1073 
1074 	memset(&data, 0, sizeof(data));
1075 
1076 	/* User wants to query the blob length */
1077 	if (!params.len)
1078 		goto cmd;
1079 
1080 	p = (void __user *)(uintptr_t)params.uaddr;
1081 	if (p) {
1082 		if (params.len > SEV_FW_BLOB_MAX_SIZE)
1083 			return -EINVAL;
1084 
1085 		blob = kmalloc(params.len, GFP_KERNEL_ACCOUNT);
1086 		if (!blob)
1087 			return -ENOMEM;
1088 
1089 		data.address = __psp_pa(blob);
1090 		data.len = params.len;
1091 		memcpy(data.mnonce, params.mnonce, sizeof(params.mnonce));
1092 	}
1093 cmd:
1094 	data.handle = sev->handle;
1095 	ret = sev_issue_cmd(kvm, SEV_CMD_ATTESTATION_REPORT, &data, &argp->error);
1096 	/*
1097 	 * If we query the session length, FW responded with expected data.
1098 	 */
1099 	if (!params.len)
1100 		goto done;
1101 
1102 	if (ret)
1103 		goto e_free_blob;
1104 
1105 	if (blob) {
1106 		if (copy_to_user(p, blob, params.len))
1107 			ret = -EFAULT;
1108 	}
1109 
1110 done:
1111 	params.len = data.len;
1112 	if (copy_to_user(report, &params, sizeof(params)))
1113 		ret = -EFAULT;
1114 e_free_blob:
1115 	kfree(blob);
1116 	return ret;
1117 }
1118 
1119 /* Userspace wants to query session length. */
1120 static int
1121 __sev_send_start_query_session_length(struct kvm *kvm, struct kvm_sev_cmd *argp,
1122 				      struct kvm_sev_send_start *params)
1123 {
1124 	struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
1125 	struct sev_data_send_start data;
1126 	int ret;
1127 
1128 	memset(&data, 0, sizeof(data));
1129 	data.handle = sev->handle;
1130 	ret = sev_issue_cmd(kvm, SEV_CMD_SEND_START, &data, &argp->error);
1131 
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)))
1135 		ret = -EFAULT;
1136 
1137 	return ret;
1138 }
1139 
1140 static int sev_send_start(struct kvm *kvm, struct kvm_sev_cmd *argp)
1141 {
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;
1147 	int ret;
1148 
1149 	if (!sev_guest(kvm))
1150 		return -ENOTTY;
1151 
1152 	if (copy_from_user(&params, (void __user *)(uintptr_t)argp->data,
1153 				sizeof(struct kvm_sev_send_start)))
1154 		return -EFAULT;
1155 
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,
1159 				&params);
1160 
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)
1164 		return -EINVAL;
1165 
1166 	/* allocate the memory to hold the session data blob */
1167 	session_data = kmalloc(params.session_len, GFP_KERNEL_ACCOUNT);
1168 	if (!session_data)
1169 		return -ENOMEM;
1170 
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;
1177 	}
1178 
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);
1183 		goto e_free_pdh;
1184 	}
1185 
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;
1191 	}
1192 
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;
1204 
1205 	ret = sev_issue_cmd(kvm, SEV_CMD_SEND_START, &data, &argp->error);
1206 
1207 	if (!ret && copy_to_user((void __user *)(uintptr_t)params.session_uaddr,
1208 			session_data, params.session_len)) {
1209 		ret = -EFAULT;
1210 		goto e_free_amd_cert;
1211 	}
1212 
1213 	params.policy = data.policy;
1214 	params.session_len = data.session_len;
1215 	if (copy_to_user((void __user *)(uintptr_t)argp->data, &params,
1216 				sizeof(struct kvm_sev_send_start)))
1217 		ret = -EFAULT;
1218 
1219 e_free_amd_cert:
1220 	kfree(amd_certs);
1221 e_free_plat_cert:
1222 	kfree(plat_certs);
1223 e_free_pdh:
1224 	kfree(pdh_cert);
1225 e_free_session:
1226 	kfree(session_data);
1227 	return ret;
1228 }
1229 
1230 /* Userspace wants to query either header or trans length. */
1231 static int
1232 __sev_send_update_data_query_lengths(struct kvm *kvm, struct kvm_sev_cmd *argp,
1233 				     struct kvm_sev_send_update_data *params)
1234 {
1235 	struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
1236 	struct sev_data_send_update_data data;
1237 	int ret;
1238 
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);
1242 
1243 	params->hdr_len = data.hdr_len;
1244 	params->trans_len = data.trans_len;
1245 
1246 	if (copy_to_user((void __user *)(uintptr_t)argp->data, params,
1247 			 sizeof(struct kvm_sev_send_update_data)))
1248 		ret = -EFAULT;
1249 
1250 	return ret;
1251 }
1252 
1253 static int sev_send_update_data(struct kvm *kvm, struct kvm_sev_cmd *argp)
1254 {
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;
1260 	unsigned long n;
1261 	int ret, offset;
1262 
1263 	if (!sev_guest(kvm))
1264 		return -ENOTTY;
1265 
1266 	if (copy_from_user(&params, (void __user *)(uintptr_t)argp->data,
1267 			sizeof(struct kvm_sev_send_update_data)))
1268 		return -EFAULT;
1269 
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, &params);
1273 
1274 	if (!params.trans_uaddr || !params.guest_uaddr ||
1275 	    !params.guest_len || !params.hdr_uaddr)
1276 		return -EINVAL;
1277 
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))
1281 		return -EINVAL;
1282 
1283 	/* Pin guest memory */
1284 	guest_page = sev_pin_memory(kvm, params.guest_uaddr & PAGE_MASK,
1285 				    PAGE_SIZE, &n, 0);
1286 	if (IS_ERR(guest_page))
1287 		return PTR_ERR(guest_page);
1288 
1289 	/* allocate memory for header and transport buffer */
1290 	ret = -ENOMEM;
1291 	hdr = kmalloc(params.hdr_len, GFP_KERNEL_ACCOUNT);
1292 	if (!hdr)
1293 		goto e_unpin;
1294 
1295 	trans_data = kmalloc(params.trans_len, GFP_KERNEL_ACCOUNT);
1296 	if (!trans_data)
1297 		goto e_free_hdr;
1298 
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;
1304 
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;
1310 
1311 	ret = sev_issue_cmd(kvm, SEV_CMD_SEND_UPDATE_DATA, &data, &argp->error);
1312 
1313 	if (ret)
1314 		goto e_free_trans_data;
1315 
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)) {
1319 		ret = -EFAULT;
1320 		goto e_free_trans_data;
1321 	}
1322 
1323 	/* Copy packet header to userspace. */
1324 	if (copy_to_user((void __user *)(uintptr_t)params.hdr_uaddr, hdr,
1325 			 params.hdr_len))
1326 		ret = -EFAULT;
1327 
1328 e_free_trans_data:
1329 	kfree(trans_data);
1330 e_free_hdr:
1331 	kfree(hdr);
1332 e_unpin:
1333 	sev_unpin_memory(kvm, guest_page, n);
1334 
1335 	return ret;
1336 }
1337 
1338 static int sev_send_finish(struct kvm *kvm, struct kvm_sev_cmd *argp)
1339 {
1340 	struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
1341 	struct sev_data_send_finish data;
1342 
1343 	if (!sev_guest(kvm))
1344 		return -ENOTTY;
1345 
1346 	data.handle = sev->handle;
1347 	return sev_issue_cmd(kvm, SEV_CMD_SEND_FINISH, &data, &argp->error);
1348 }
1349 
1350 static int sev_send_cancel(struct kvm *kvm, struct kvm_sev_cmd *argp)
1351 {
1352 	struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
1353 	struct sev_data_send_cancel data;
1354 
1355 	if (!sev_guest(kvm))
1356 		return -ENOTTY;
1357 
1358 	data.handle = sev->handle;
1359 	return sev_issue_cmd(kvm, SEV_CMD_SEND_CANCEL, &data, &argp->error);
1360 }
1361 
1362 static int sev_receive_start(struct kvm *kvm, struct kvm_sev_cmd *argp)
1363 {
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;
1368 	void *session_data;
1369 	void *pdh_data;
1370 	int ret;
1371 
1372 	if (!sev_guest(kvm))
1373 		return -ENOTTY;
1374 
1375 	/* Get parameter from the userspace */
1376 	if (copy_from_user(&params, (void __user *)(uintptr_t)argp->data,
1377 			sizeof(struct kvm_sev_receive_start)))
1378 		return -EFAULT;
1379 
1380 	/* some sanity checks */
1381 	if (!params.pdh_uaddr || !params.pdh_len ||
1382 	    !params.session_uaddr || !params.session_len)
1383 		return -EINVAL;
1384 
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);
1388 
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);
1393 		goto e_free_pdh;
1394 	}
1395 
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;
1403 
1404 	/* create memory encryption context */
1405 	ret = __sev_issue_cmd(argp->sev_fd, SEV_CMD_RECEIVE_START, &start,
1406 				error);
1407 	if (ret)
1408 		goto e_free_session;
1409 
1410 	/* Bind ASID to this guest */
1411 	ret = sev_bind_asid(kvm, start.handle, error);
1412 	if (ret) {
1413 		sev_decommission(start.handle);
1414 		goto e_free_session;
1415 	}
1416 
1417 	params.handle = start.handle;
1418 	if (copy_to_user((void __user *)(uintptr_t)argp->data,
1419 			 &params, sizeof(struct kvm_sev_receive_start))) {
1420 		ret = -EFAULT;
1421 		sev_unbind_asid(kvm, start.handle);
1422 		goto e_free_session;
1423 	}
1424 
1425     	sev->handle = start.handle;
1426 	sev->fd = argp->sev_fd;
1427 
1428 e_free_session:
1429 	kfree(session_data);
1430 e_free_pdh:
1431 	kfree(pdh_data);
1432 
1433 	return ret;
1434 }
1435 
1436 static int sev_receive_update_data(struct kvm *kvm, struct kvm_sev_cmd *argp)
1437 {
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;
1443 	unsigned long n;
1444 	int ret, offset;
1445 
1446 	if (!sev_guest(kvm))
1447 		return -EINVAL;
1448 
1449 	if (copy_from_user(&params, (void __user *)(uintptr_t)argp->data,
1450 			sizeof(struct kvm_sev_receive_update_data)))
1451 		return -EFAULT;
1452 
1453 	if (!params.hdr_uaddr || !params.hdr_len ||
1454 	    !params.guest_uaddr || !params.guest_len ||
1455 	    !params.trans_uaddr || !params.trans_len)
1456 		return -EINVAL;
1457 
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))
1461 		return -EINVAL;
1462 
1463 	hdr = psp_copy_user_blob(params.hdr_uaddr, params.hdr_len);
1464 	if (IS_ERR(hdr))
1465 		return PTR_ERR(hdr);
1466 
1467 	trans = psp_copy_user_blob(params.trans_uaddr, params.trans_len);
1468 	if (IS_ERR(trans)) {
1469 		ret = PTR_ERR(trans);
1470 		goto e_free_hdr;
1471 	}
1472 
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;
1478 
1479 	/* Pin guest memory */
1480 	guest_page = sev_pin_memory(kvm, params.guest_uaddr & PAGE_MASK,
1481 				    PAGE_SIZE, &n, 1);
1482 	if (IS_ERR(guest_page)) {
1483 		ret = PTR_ERR(guest_page);
1484 		goto e_free_trans;
1485 	}
1486 
1487 	/*
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.
1491 	 */
1492 	sev_clflush_pages(guest_page, n);
1493 
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;
1499 
1500 	ret = sev_issue_cmd(kvm, SEV_CMD_RECEIVE_UPDATE_DATA, &data,
1501 				&argp->error);
1502 
1503 	sev_unpin_memory(kvm, guest_page, n);
1504 
1505 e_free_trans:
1506 	kfree(trans);
1507 e_free_hdr:
1508 	kfree(hdr);
1509 
1510 	return ret;
1511 }
1512 
1513 static int sev_receive_finish(struct kvm *kvm, struct kvm_sev_cmd *argp)
1514 {
1515 	struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
1516 	struct sev_data_receive_finish data;
1517 
1518 	if (!sev_guest(kvm))
1519 		return -ENOTTY;
1520 
1521 	data.handle = sev->handle;
1522 	return sev_issue_cmd(kvm, SEV_CMD_RECEIVE_FINISH, &data, &argp->error);
1523 }
1524 
1525 static bool cmd_allowed_from_miror(u32 cmd_id)
1526 {
1527 	/*
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.
1530 	 */
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)
1534 		return true;
1535 
1536 	return false;
1537 }
1538 
1539 int svm_mem_enc_op(struct kvm *kvm, void __user *argp)
1540 {
1541 	struct kvm_sev_cmd sev_cmd;
1542 	int r;
1543 
1544 	if (!sev_enabled)
1545 		return -ENOTTY;
1546 
1547 	if (!argp)
1548 		return 0;
1549 
1550 	if (copy_from_user(&sev_cmd, argp, sizeof(struct kvm_sev_cmd)))
1551 		return -EFAULT;
1552 
1553 	mutex_lock(&kvm->lock);
1554 
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)) {
1558 		r = -EINVAL;
1559 		goto out;
1560 	}
1561 
1562 	switch (sev_cmd.id) {
1563 	case KVM_SEV_ES_INIT:
1564 		if (!sev_es_enabled) {
1565 			r = -ENOTTY;
1566 			goto out;
1567 		}
1568 		fallthrough;
1569 	case KVM_SEV_INIT:
1570 		r = sev_guest_init(kvm, &sev_cmd);
1571 		break;
1572 	case KVM_SEV_LAUNCH_START:
1573 		r = sev_launch_start(kvm, &sev_cmd);
1574 		break;
1575 	case KVM_SEV_LAUNCH_UPDATE_DATA:
1576 		r = sev_launch_update_data(kvm, &sev_cmd);
1577 		break;
1578 	case KVM_SEV_LAUNCH_UPDATE_VMSA:
1579 		r = sev_launch_update_vmsa(kvm, &sev_cmd);
1580 		break;
1581 	case KVM_SEV_LAUNCH_MEASURE:
1582 		r = sev_launch_measure(kvm, &sev_cmd);
1583 		break;
1584 	case KVM_SEV_LAUNCH_FINISH:
1585 		r = sev_launch_finish(kvm, &sev_cmd);
1586 		break;
1587 	case KVM_SEV_GUEST_STATUS:
1588 		r = sev_guest_status(kvm, &sev_cmd);
1589 		break;
1590 	case KVM_SEV_DBG_DECRYPT:
1591 		r = sev_dbg_crypt(kvm, &sev_cmd, true);
1592 		break;
1593 	case KVM_SEV_DBG_ENCRYPT:
1594 		r = sev_dbg_crypt(kvm, &sev_cmd, false);
1595 		break;
1596 	case KVM_SEV_LAUNCH_SECRET:
1597 		r = sev_launch_secret(kvm, &sev_cmd);
1598 		break;
1599 	case KVM_SEV_GET_ATTESTATION_REPORT:
1600 		r = sev_get_attestation_report(kvm, &sev_cmd);
1601 		break;
1602 	case KVM_SEV_SEND_START:
1603 		r = sev_send_start(kvm, &sev_cmd);
1604 		break;
1605 	case KVM_SEV_SEND_UPDATE_DATA:
1606 		r = sev_send_update_data(kvm, &sev_cmd);
1607 		break;
1608 	case KVM_SEV_SEND_FINISH:
1609 		r = sev_send_finish(kvm, &sev_cmd);
1610 		break;
1611 	case KVM_SEV_SEND_CANCEL:
1612 		r = sev_send_cancel(kvm, &sev_cmd);
1613 		break;
1614 	case KVM_SEV_RECEIVE_START:
1615 		r = sev_receive_start(kvm, &sev_cmd);
1616 		break;
1617 	case KVM_SEV_RECEIVE_UPDATE_DATA:
1618 		r = sev_receive_update_data(kvm, &sev_cmd);
1619 		break;
1620 	case KVM_SEV_RECEIVE_FINISH:
1621 		r = sev_receive_finish(kvm, &sev_cmd);
1622 		break;
1623 	default:
1624 		r = -EINVAL;
1625 		goto out;
1626 	}
1627 
1628 	if (copy_to_user(argp, &sev_cmd, sizeof(struct kvm_sev_cmd)))
1629 		r = -EFAULT;
1630 
1631 out:
1632 	mutex_unlock(&kvm->lock);
1633 	return r;
1634 }
1635 
1636 int svm_register_enc_region(struct kvm *kvm,
1637 			    struct kvm_enc_region *range)
1638 {
1639 	struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
1640 	struct enc_region *region;
1641 	int ret = 0;
1642 
1643 	if (!sev_guest(kvm))
1644 		return -ENOTTY;
1645 
1646 	/* If kvm is mirroring encryption context it isn't responsible for it */
1647 	if (is_mirroring_enc_context(kvm))
1648 		return -EINVAL;
1649 
1650 	if (range->addr > ULONG_MAX || range->size > ULONG_MAX)
1651 		return -EINVAL;
1652 
1653 	region = kzalloc(sizeof(*region), GFP_KERNEL_ACCOUNT);
1654 	if (!region)
1655 		return -ENOMEM;
1656 
1657 	mutex_lock(&kvm->lock);
1658 	region->pages = sev_pin_memory(kvm, range->addr, range->size, &region->npages, 1);
1659 	if (IS_ERR(region->pages)) {
1660 		ret = PTR_ERR(region->pages);
1661 		mutex_unlock(&kvm->lock);
1662 		goto e_free;
1663 	}
1664 
1665 	region->uaddr = range->addr;
1666 	region->size = range->size;
1667 
1668 	list_add_tail(&region->list, &sev->regions_list);
1669 	mutex_unlock(&kvm->lock);
1670 
1671 	/*
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
1675 	 * correct C-bit.
1676 	 */
1677 	sev_clflush_pages(region->pages, region->npages);
1678 
1679 	return ret;
1680 
1681 e_free:
1682 	kfree(region);
1683 	return ret;
1684 }
1685 
1686 static struct enc_region *
1687 find_enc_region(struct kvm *kvm, struct kvm_enc_region *range)
1688 {
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;
1692 
1693 	list_for_each_entry(i, head, list) {
1694 		if (i->uaddr == range->addr &&
1695 		    i->size == range->size)
1696 			return i;
1697 	}
1698 
1699 	return NULL;
1700 }
1701 
1702 static void __unregister_enc_region_locked(struct kvm *kvm,
1703 					   struct enc_region *region)
1704 {
1705 	sev_unpin_memory(kvm, region->pages, region->npages);
1706 	list_del(&region->list);
1707 	kfree(region);
1708 }
1709 
1710 int svm_unregister_enc_region(struct kvm *kvm,
1711 			      struct kvm_enc_region *range)
1712 {
1713 	struct enc_region *region;
1714 	int ret;
1715 
1716 	/* If kvm is mirroring encryption context it isn't responsible for it */
1717 	if (is_mirroring_enc_context(kvm))
1718 		return -EINVAL;
1719 
1720 	mutex_lock(&kvm->lock);
1721 
1722 	if (!sev_guest(kvm)) {
1723 		ret = -ENOTTY;
1724 		goto failed;
1725 	}
1726 
1727 	region = find_enc_region(kvm, range);
1728 	if (!region) {
1729 		ret = -EINVAL;
1730 		goto failed;
1731 	}
1732 
1733 	/*
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.
1737 	 */
1738 	wbinvd_on_all_cpus();
1739 
1740 	__unregister_enc_region_locked(kvm, region);
1741 
1742 	mutex_unlock(&kvm->lock);
1743 	return 0;
1744 
1745 failed:
1746 	mutex_unlock(&kvm->lock);
1747 	return ret;
1748 }
1749 
1750 int svm_vm_copy_asid_from(struct kvm *kvm, unsigned int source_fd)
1751 {
1752 	struct file *source_kvm_file;
1753 	struct kvm *source_kvm;
1754 	struct kvm_sev_info source_sev, *mirror_sev;
1755 	int ret;
1756 
1757 	source_kvm_file = fget(source_fd);
1758 	if (!file_is_kvm(source_kvm_file)) {
1759 		ret = -EBADF;
1760 		goto e_source_put;
1761 	}
1762 
1763 	source_kvm = source_kvm_file->private_data;
1764 	mutex_lock(&source_kvm->lock);
1765 
1766 	if (!sev_guest(source_kvm)) {
1767 		ret = -EINVAL;
1768 		goto e_source_unlock;
1769 	}
1770 
1771 	/* Mirrors of mirrors should work, but let's not get silly */
1772 	if (is_mirroring_enc_context(source_kvm) || source_kvm == kvm) {
1773 		ret = -EINVAL;
1774 		goto e_source_unlock;
1775 	}
1776 
1777 	memcpy(&source_sev, &to_kvm_svm(source_kvm)->sev_info,
1778 	       sizeof(source_sev));
1779 
1780 	/*
1781 	 * The mirror kvm holds an enc_context_owner ref so its asid can't
1782 	 * disappear until we're done with it
1783 	 */
1784 	kvm_get_kvm(source_kvm);
1785 
1786 	fput(source_kvm_file);
1787 	mutex_unlock(&source_kvm->lock);
1788 	mutex_lock(&kvm->lock);
1789 
1790 	if (sev_guest(kvm)) {
1791 		ret = -EINVAL;
1792 		goto e_mirror_unlock;
1793 	}
1794 
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;
1803 	/*
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
1806 	 * memory-views.
1807 	 */
1808 
1809 	mutex_unlock(&kvm->lock);
1810 	return 0;
1811 
1812 e_mirror_unlock:
1813 	mutex_unlock(&kvm->lock);
1814 	kvm_put_kvm(source_kvm);
1815 	return ret;
1816 e_source_unlock:
1817 	mutex_unlock(&source_kvm->lock);
1818 e_source_put:
1819 	if (source_kvm_file)
1820 		fput(source_kvm_file);
1821 	return ret;
1822 }
1823 
1824 void sev_vm_destroy(struct kvm *kvm)
1825 {
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;
1829 
1830 	if (!sev_guest(kvm))
1831 		return;
1832 
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);
1836 		return;
1837 	}
1838 
1839 	mutex_lock(&kvm->lock);
1840 
1841 	/*
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.
1845 	 */
1846 	wbinvd_on_all_cpus();
1847 
1848 	/*
1849 	 * if userspace was terminated before unregistering the memory regions
1850 	 * then lets unpin all the registered memory.
1851 	 */
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));
1856 			cond_resched();
1857 		}
1858 	}
1859 
1860 	mutex_unlock(&kvm->lock);
1861 
1862 	sev_unbind_asid(kvm, sev->handle);
1863 	sev_asid_free(sev);
1864 }
1865 
1866 void __init sev_set_cpu_caps(void)
1867 {
1868 	if (!sev_enabled)
1869 		kvm_cpu_cap_clear(X86_FEATURE_SEV);
1870 	if (!sev_es_enabled)
1871 		kvm_cpu_cap_clear(X86_FEATURE_SEV_ES);
1872 }
1873 
1874 void __init sev_hardware_setup(void)
1875 {
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;
1880 
1881 	if (!sev_enabled || !npt_enabled)
1882 		goto out;
1883 
1884 	/* Does the CPU support SEV? */
1885 	if (!boot_cpu_has(X86_FEATURE_SEV))
1886 		goto out;
1887 
1888 	/* Retrieve SEV CPUID information */
1889 	cpuid(0x8000001f, &eax, &ebx, &ecx, &edx);
1890 
1891 	/* Set encryption bit location for SEV-ES guests */
1892 	sev_enc_bit = ebx & 0x3f;
1893 
1894 	/* Maximum number of encrypted guests supported simultaneously */
1895 	max_sev_asid = ecx;
1896 	if (!max_sev_asid)
1897 		goto out;
1898 
1899 	/* Minimum ASID value that should be used for SEV guest */
1900 	min_sev_asid = edx;
1901 	sev_me_mask = 1UL << (ebx & 0x3f);
1902 
1903 	/*
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
1906 	 * actual ASID.
1907 	 */
1908 	nr_asids = max_sev_asid + 1;
1909 	sev_asid_bitmap = bitmap_zalloc(nr_asids, GFP_KERNEL);
1910 	if (!sev_asid_bitmap)
1911 		goto out;
1912 
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;
1917 		goto out;
1918 	}
1919 
1920 	sev_asid_count = max_sev_asid - min_sev_asid + 1;
1921 	if (misc_cg_set_capacity(MISC_CG_RES_SEV, sev_asid_count))
1922 		goto out;
1923 
1924 	pr_info("SEV supported: %u ASIDs\n", sev_asid_count);
1925 	sev_supported = true;
1926 
1927 	/* SEV-ES support requested? */
1928 	if (!sev_es_enabled)
1929 		goto out;
1930 
1931 	/* Does the CPU support SEV-ES? */
1932 	if (!boot_cpu_has(X86_FEATURE_SEV_ES))
1933 		goto out;
1934 
1935 	/* Has the system been allocated ASIDs for SEV-ES? */
1936 	if (min_sev_asid == 1)
1937 		goto out;
1938 
1939 	sev_es_asid_count = min_sev_asid - 1;
1940 	if (misc_cg_set_capacity(MISC_CG_RES_SEV_ES, sev_es_asid_count))
1941 		goto out;
1942 
1943 	pr_info("SEV-ES supported: %u ASIDs\n", sev_es_asid_count);
1944 	sev_es_supported = true;
1945 
1946 out:
1947 	sev_enabled = sev_supported;
1948 	sev_es_enabled = sev_es_supported;
1949 #endif
1950 }
1951 
1952 void sev_hardware_teardown(void)
1953 {
1954 	if (!sev_enabled)
1955 		return;
1956 
1957 	/* No need to take sev_bitmap_lock, all VMs have been destroyed. */
1958 	sev_flush_asids(1, max_sev_asid);
1959 
1960 	bitmap_free(sev_asid_bitmap);
1961 	bitmap_free(sev_reclaim_asid_bitmap);
1962 
1963 	misc_cg_set_capacity(MISC_CG_RES_SEV, 0);
1964 	misc_cg_set_capacity(MISC_CG_RES_SEV_ES, 0);
1965 }
1966 
1967 int sev_cpu_init(struct svm_cpu_data *sd)
1968 {
1969 	if (!sev_enabled)
1970 		return 0;
1971 
1972 	sd->sev_vmcbs = kcalloc(nr_asids, sizeof(void *), GFP_KERNEL);
1973 	if (!sd->sev_vmcbs)
1974 		return -ENOMEM;
1975 
1976 	return 0;
1977 }
1978 
1979 /*
1980  * Pages used by hardware to hold guest encrypted state must be flushed before
1981  * returning them to the system.
1982  */
1983 static void sev_flush_guest_memory(struct vcpu_svm *svm, void *va,
1984 				   unsigned long len)
1985 {
1986 	/*
1987 	 * If hardware enforced cache coherency for encrypted mappings of the
1988 	 * same physical page is supported, nothing to do.
1989 	 */
1990 	if (boot_cpu_has(X86_FEATURE_SME_COHERENT))
1991 		return;
1992 
1993 	/*
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).
1996 	 */
1997 	if (boot_cpu_has(X86_FEATURE_VM_PAGE_FLUSH)) {
1998 		struct kvm_sev_info *sev;
1999 		unsigned long va_start;
2000 		u64 start, stop;
2001 
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);
2006 
2007 		if (start < stop) {
2008 			sev = &to_kvm_svm(svm->vcpu.kvm)->sev_info;
2009 
2010 			while (start < stop) {
2011 				wrmsrl(MSR_AMD64_VM_PAGE_FLUSH,
2012 				       start | sev->asid);
2013 
2014 				start += PAGE_SIZE;
2015 			}
2016 
2017 			return;
2018 		}
2019 
2020 		WARN(1, "Address overflow, using WBINVD\n");
2021 	}
2022 
2023 	/*
2024 	 * Hardware should always have one of the above features,
2025 	 * but if not, use WBINVD and issue a warning.
2026 	 */
2027 	WARN_ONCE(1, "Using WBINVD to flush guest memory\n");
2028 	wbinvd_on_all_cpus();
2029 }
2030 
2031 void sev_free_vcpu(struct kvm_vcpu *vcpu)
2032 {
2033 	struct vcpu_svm *svm;
2034 
2035 	if (!sev_es_guest(vcpu->kvm))
2036 		return;
2037 
2038 	svm = to_svm(vcpu);
2039 
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));
2043 
2044 	if (svm->ghcb_sa_free)
2045 		kfree(svm->ghcb_sa);
2046 }
2047 
2048 static void dump_ghcb(struct vcpu_svm *svm)
2049 {
2050 	struct ghcb *ghcb = svm->ghcb;
2051 	unsigned int nbits;
2052 
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");
2056 		return;
2057 	}
2058 
2059 	nbits = sizeof(ghcb->save.valid_bitmap) * 8;
2060 
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);
2071 }
2072 
2073 static void sev_es_sync_to_ghcb(struct vcpu_svm *svm)
2074 {
2075 	struct kvm_vcpu *vcpu = &svm->vcpu;
2076 	struct ghcb *ghcb = svm->ghcb;
2077 
2078 	/*
2079 	 * The GHCB protocol so far allows for the following data
2080 	 * to be returned:
2081 	 *   GPRs RAX, RBX, RCX, RDX
2082 	 *
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.
2085 	 */
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]);
2090 }
2091 
2092 static void sev_es_sync_from_ghcb(struct vcpu_svm *svm)
2093 {
2094 	struct vmcb_control_area *control = &svm->vmcb->control;
2095 	struct kvm_vcpu *vcpu = &svm->vcpu;
2096 	struct ghcb *ghcb = svm->ghcb;
2097 	u64 exit_code;
2098 
2099 	/*
2100 	 * The GHCB protocol so far allows for the following data
2101 	 * to be supplied:
2102 	 *   GPRs RAX, RBX, RCX, RDX
2103 	 *   XCR0
2104 	 *   CPL
2105 	 *
2106 	 * VMMCALL allows the guest to provide extra registers. KVM also
2107 	 * expects RSI for hypercalls, so include that, too.
2108 	 *
2109 	 * Copy their values to the appropriate location if supplied.
2110 	 */
2111 	memset(vcpu->arch.regs, 0, sizeof(vcpu->arch.regs));
2112 
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);
2118 
2119 	svm->vmcb->save.cpl = ghcb_get_cpl_if_valid(ghcb);
2120 
2121 	if (ghcb_xcr0_is_valid(ghcb)) {
2122 		vcpu->arch.xcr0 = ghcb_get_xcr0(ghcb);
2123 		kvm_update_cpuid_runtime(vcpu);
2124 	}
2125 
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);
2132 
2133 	/* Clear the valid entries fields */
2134 	memset(ghcb->save.valid_bitmap, 0, sizeof(ghcb->save.valid_bitmap));
2135 }
2136 
2137 static int sev_es_validate_vmgexit(struct vcpu_svm *svm)
2138 {
2139 	struct kvm_vcpu *vcpu;
2140 	struct ghcb *ghcb;
2141 	u64 exit_code = 0;
2142 
2143 	ghcb = svm->ghcb;
2144 
2145 	/* Only GHCB Usage code 0 is supported */
2146 	if (ghcb->ghcb_usage)
2147 		goto vmgexit_err;
2148 
2149 	/*
2150 	 * Retrieve the exit code now even though is may not be marked valid
2151 	 * as it could help with debugging.
2152 	 */
2153 	exit_code = ghcb_get_sw_exit_code(ghcb);
2154 
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))
2158 		goto vmgexit_err;
2159 
2160 	switch (ghcb_get_sw_exit_code(ghcb)) {
2161 	case SVM_EXIT_READ_DR7:
2162 		break;
2163 	case SVM_EXIT_WRITE_DR7:
2164 		if (!ghcb_rax_is_valid(ghcb))
2165 			goto vmgexit_err;
2166 		break;
2167 	case SVM_EXIT_RDTSC:
2168 		break;
2169 	case SVM_EXIT_RDPMC:
2170 		if (!ghcb_rcx_is_valid(ghcb))
2171 			goto vmgexit_err;
2172 		break;
2173 	case SVM_EXIT_CPUID:
2174 		if (!ghcb_rax_is_valid(ghcb) ||
2175 		    !ghcb_rcx_is_valid(ghcb))
2176 			goto vmgexit_err;
2177 		if (ghcb_get_rax(ghcb) == 0xd)
2178 			if (!ghcb_xcr0_is_valid(ghcb))
2179 				goto vmgexit_err;
2180 		break;
2181 	case SVM_EXIT_INVD:
2182 		break;
2183 	case SVM_EXIT_IOIO:
2184 		if (ghcb_get_sw_exit_info_1(ghcb) & SVM_IOIO_STR_MASK) {
2185 			if (!ghcb_sw_scratch_is_valid(ghcb))
2186 				goto vmgexit_err;
2187 		} else {
2188 			if (!(ghcb_get_sw_exit_info_1(ghcb) & SVM_IOIO_TYPE_MASK))
2189 				if (!ghcb_rax_is_valid(ghcb))
2190 					goto vmgexit_err;
2191 		}
2192 		break;
2193 	case SVM_EXIT_MSR:
2194 		if (!ghcb_rcx_is_valid(ghcb))
2195 			goto vmgexit_err;
2196 		if (ghcb_get_sw_exit_info_1(ghcb)) {
2197 			if (!ghcb_rax_is_valid(ghcb) ||
2198 			    !ghcb_rdx_is_valid(ghcb))
2199 				goto vmgexit_err;
2200 		}
2201 		break;
2202 	case SVM_EXIT_VMMCALL:
2203 		if (!ghcb_rax_is_valid(ghcb) ||
2204 		    !ghcb_cpl_is_valid(ghcb))
2205 			goto vmgexit_err;
2206 		break;
2207 	case SVM_EXIT_RDTSCP:
2208 		break;
2209 	case SVM_EXIT_WBINVD:
2210 		break;
2211 	case SVM_EXIT_MONITOR:
2212 		if (!ghcb_rax_is_valid(ghcb) ||
2213 		    !ghcb_rcx_is_valid(ghcb) ||
2214 		    !ghcb_rdx_is_valid(ghcb))
2215 			goto vmgexit_err;
2216 		break;
2217 	case SVM_EXIT_MWAIT:
2218 		if (!ghcb_rax_is_valid(ghcb) ||
2219 		    !ghcb_rcx_is_valid(ghcb))
2220 			goto vmgexit_err;
2221 		break;
2222 	case SVM_VMGEXIT_MMIO_READ:
2223 	case SVM_VMGEXIT_MMIO_WRITE:
2224 		if (!ghcb_sw_scratch_is_valid(ghcb))
2225 			goto vmgexit_err;
2226 		break;
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:
2231 		break;
2232 	default:
2233 		goto vmgexit_err;
2234 	}
2235 
2236 	return 0;
2237 
2238 vmgexit_err:
2239 	vcpu = &svm->vcpu;
2240 
2241 	if (ghcb->ghcb_usage) {
2242 		vcpu_unimpl(vcpu, "vmgexit: ghcb usage %#x is not valid\n",
2243 			    ghcb->ghcb_usage);
2244 	} else {
2245 		vcpu_unimpl(vcpu, "vmgexit: exit reason %#llx is not valid\n",
2246 			    exit_code);
2247 		dump_ghcb(svm);
2248 	}
2249 
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;
2255 
2256 	return -EINVAL;
2257 }
2258 
2259 void sev_es_unmap_ghcb(struct vcpu_svm *svm)
2260 {
2261 	if (!svm->ghcb)
2262 		return;
2263 
2264 	if (svm->ghcb_sa_free) {
2265 		/*
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.
2269 		 */
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;
2275 		}
2276 
2277 		kfree(svm->ghcb_sa);
2278 		svm->ghcb_sa = NULL;
2279 		svm->ghcb_sa_free = false;
2280 	}
2281 
2282 	trace_kvm_vmgexit_exit(svm->vcpu.vcpu_id, svm->ghcb);
2283 
2284 	sev_es_sync_to_ghcb(svm);
2285 
2286 	kvm_vcpu_unmap(&svm->vcpu, &svm->ghcb_map, true);
2287 	svm->ghcb = NULL;
2288 }
2289 
2290 void pre_sev_run(struct vcpu_svm *svm, int cpu)
2291 {
2292 	struct svm_cpu_data *sd = per_cpu(svm_data, cpu);
2293 	int asid = sev_get_asid(svm->vcpu.kvm);
2294 
2295 	/* Assign the asid allocated with this SEV guest */
2296 	svm->asid = asid;
2297 
2298 	/*
2299 	 * Flush guest TLB:
2300 	 *
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.
2303 	 */
2304 	if (sd->sev_vmcbs[asid] == svm->vmcb &&
2305 	    svm->vcpu.arch.last_vmentry_cpu == cpu)
2306 		return;
2307 
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);
2311 }
2312 
2313 #define GHCB_SCRATCH_AREA_LIMIT		(16ULL * PAGE_SIZE)
2314 static bool setup_vmgexit_scratch(struct vcpu_svm *svm, bool sync, u64 len)
2315 {
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;
2320 	void *scratch_va;
2321 
2322 	scratch_gpa_beg = ghcb_get_sw_scratch(ghcb);
2323 	if (!scratch_gpa_beg) {
2324 		pr_err("vmgexit: scratch gpa not provided\n");
2325 		return false;
2326 	}
2327 
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);
2332 		return false;
2333 	}
2334 
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);
2341 
2342 		/*
2343 		 * If the scratch area begins within the GHCB, it must be
2344 		 * completely contained in the GHCB shared buffer area.
2345 		 */
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);
2350 			return false;
2351 		}
2352 
2353 		scratch_va = (void *)svm->ghcb;
2354 		scratch_va += (scratch_gpa_beg - control->ghcb_gpa);
2355 	} else {
2356 		/*
2357 		 * The guest memory must be read into a kernel buffer, so
2358 		 * limit the size
2359 		 */
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);
2363 			return false;
2364 		}
2365 		scratch_va = kzalloc(len, GFP_KERNEL_ACCOUNT);
2366 		if (!scratch_va)
2367 			return false;
2368 
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");
2372 
2373 			kfree(scratch_va);
2374 			return false;
2375 		}
2376 
2377 		/*
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).
2382 		 */
2383 		svm->ghcb_sa_sync = sync;
2384 		svm->ghcb_sa_free = true;
2385 	}
2386 
2387 	svm->ghcb_sa = scratch_va;
2388 	svm->ghcb_sa_len = len;
2389 
2390 	return true;
2391 }
2392 
2393 static void set_ghcb_msr_bits(struct vcpu_svm *svm, u64 value, u64 mask,
2394 			      unsigned int pos)
2395 {
2396 	svm->vmcb->control.ghcb_gpa &= ~(mask << pos);
2397 	svm->vmcb->control.ghcb_gpa |= (value & mask) << pos;
2398 }
2399 
2400 static u64 get_ghcb_msr_bits(struct vcpu_svm *svm, u64 mask, unsigned int pos)
2401 {
2402 	return (svm->vmcb->control.ghcb_gpa >> pos) & mask;
2403 }
2404 
2405 static void set_ghcb_msr(struct vcpu_svm *svm, u64 value)
2406 {
2407 	svm->vmcb->control.ghcb_gpa = value;
2408 }
2409 
2410 static int sev_handle_vmgexit_msr_protocol(struct vcpu_svm *svm)
2411 {
2412 	struct vmcb_control_area *control = &svm->vmcb->control;
2413 	struct kvm_vcpu *vcpu = &svm->vcpu;
2414 	u64 ghcb_info;
2415 	int ret = 1;
2416 
2417 	ghcb_info = control->ghcb_gpa & GHCB_MSR_INFO_MASK;
2418 
2419 	trace_kvm_vmgexit_msr_protocol_enter(svm->vcpu.vcpu_id,
2420 					     control->ghcb_gpa);
2421 
2422 	switch (ghcb_info) {
2423 	case GHCB_MSR_SEV_INFO_REQ:
2424 		set_ghcb_msr(svm, GHCB_MSR_SEV_INFO(GHCB_VERSION_MAX,
2425 						    GHCB_VERSION_MIN,
2426 						    sev_enc_bit));
2427 		break;
2428 	case GHCB_MSR_CPUID_REQ: {
2429 		u64 cpuid_fn, cpuid_reg, cpuid_value;
2430 
2431 		cpuid_fn = get_ghcb_msr_bits(svm,
2432 					     GHCB_MSR_CPUID_FUNC_MASK,
2433 					     GHCB_MSR_CPUID_FUNC_POS);
2434 
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;
2438 
2439 		ret = svm_invoke_exit_handler(vcpu, SVM_EXIT_CPUID);
2440 		if (!ret) {
2441 			ret = -EINVAL;
2442 			break;
2443 		}
2444 
2445 		cpuid_reg = get_ghcb_msr_bits(svm,
2446 					      GHCB_MSR_CPUID_REG_MASK,
2447 					      GHCB_MSR_CPUID_REG_POS);
2448 		if (cpuid_reg == 0)
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];
2454 		else
2455 			cpuid_value = vcpu->arch.regs[VCPU_REGS_RDX];
2456 
2457 		set_ghcb_msr_bits(svm, cpuid_value,
2458 				  GHCB_MSR_CPUID_VALUE_MASK,
2459 				  GHCB_MSR_CPUID_VALUE_POS);
2460 
2461 		set_ghcb_msr_bits(svm, GHCB_MSR_CPUID_RESP,
2462 				  GHCB_MSR_INFO_MASK,
2463 				  GHCB_MSR_INFO_POS);
2464 		break;
2465 	}
2466 	case GHCB_MSR_TERM_REQ: {
2467 		u64 reason_set, reason_code;
2468 
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);
2477 		fallthrough;
2478 	}
2479 	default:
2480 		ret = -EINVAL;
2481 	}
2482 
2483 	trace_kvm_vmgexit_msr_protocol_exit(svm->vcpu.vcpu_id,
2484 					    control->ghcb_gpa, ret);
2485 
2486 	return ret;
2487 }
2488 
2489 int sev_handle_vmgexit(struct kvm_vcpu *vcpu)
2490 {
2491 	struct vcpu_svm *svm = to_svm(vcpu);
2492 	struct vmcb_control_area *control = &svm->vmcb->control;
2493 	u64 ghcb_gpa, exit_code;
2494 	struct ghcb *ghcb;
2495 	int ret;
2496 
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);
2501 
2502 	if (!ghcb_gpa) {
2503 		vcpu_unimpl(vcpu, "vmgexit: GHCB gpa is not set\n");
2504 		return -EINVAL;
2505 	}
2506 
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",
2510 			    ghcb_gpa);
2511 		return -EINVAL;
2512 	}
2513 
2514 	svm->ghcb = svm->ghcb_map.hva;
2515 	ghcb = svm->ghcb_map.hva;
2516 
2517 	trace_kvm_vmgexit_enter(vcpu->vcpu_id, ghcb);
2518 
2519 	exit_code = ghcb_get_sw_exit_code(ghcb);
2520 
2521 	ret = sev_es_validate_vmgexit(svm);
2522 	if (ret)
2523 		return ret;
2524 
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);
2528 
2529 	ret = -EINVAL;
2530 	switch (exit_code) {
2531 	case SVM_VMGEXIT_MMIO_READ:
2532 		if (!setup_vmgexit_scratch(svm, true, control->exit_info_2))
2533 			break;
2534 
2535 		ret = kvm_sev_es_mmio_read(vcpu,
2536 					   control->exit_info_1,
2537 					   control->exit_info_2,
2538 					   svm->ghcb_sa);
2539 		break;
2540 	case SVM_VMGEXIT_MMIO_WRITE:
2541 		if (!setup_vmgexit_scratch(svm, false, control->exit_info_2))
2542 			break;
2543 
2544 		ret = kvm_sev_es_mmio_write(vcpu,
2545 					    control->exit_info_1,
2546 					    control->exit_info_2,
2547 					    svm->ghcb_sa);
2548 		break;
2549 	case SVM_VMGEXIT_NMI_COMPLETE:
2550 		ret = svm_invoke_exit_handler(vcpu, SVM_EXIT_IRET);
2551 		break;
2552 	case SVM_VMGEXIT_AP_HLT_LOOP:
2553 		ret = kvm_emulate_ap_reset_hold(vcpu);
2554 		break;
2555 	case SVM_VMGEXIT_AP_JUMP_TABLE: {
2556 		struct kvm_sev_info *sev = &to_kvm_svm(vcpu->kvm)->sev_info;
2557 
2558 		switch (control->exit_info_1) {
2559 		case 0:
2560 			/* Set AP jump table address */
2561 			sev->ap_jump_table = control->exit_info_2;
2562 			break;
2563 		case 1:
2564 			/* Get AP jump table address */
2565 			ghcb_set_sw_exit_info_2(ghcb, sev->ap_jump_table);
2566 			break;
2567 		default:
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,
2572 						X86_TRAP_UD |
2573 						SVM_EVTINJ_TYPE_EXEPT |
2574 						SVM_EVTINJ_VALID);
2575 		}
2576 
2577 		ret = 1;
2578 		break;
2579 	}
2580 	case SVM_VMGEXIT_UNSUPPORTED_EVENT:
2581 		vcpu_unimpl(vcpu,
2582 			    "vmgexit: unsupported event - exit_info_1=%#llx, exit_info_2=%#llx\n",
2583 			    control->exit_info_1, control->exit_info_2);
2584 		break;
2585 	default:
2586 		ret = svm_invoke_exit_handler(vcpu, exit_code);
2587 	}
2588 
2589 	return ret;
2590 }
2591 
2592 int sev_es_string_io(struct vcpu_svm *svm, int size, unsigned int port, int in)
2593 {
2594 	int count;
2595 	int bytes;
2596 
2597 	if (svm->vmcb->control.exit_info_2 > INT_MAX)
2598 		return -EINVAL;
2599 
2600 	count = svm->vmcb->control.exit_info_2;
2601 	if (unlikely(check_mul_overflow(count, size, &bytes)))
2602 		return -EINVAL;
2603 
2604 	if (!setup_vmgexit_scratch(svm, in, bytes))
2605 		return -EINVAL;
2606 
2607 	return kvm_sev_es_string_io(&svm->vcpu, size, port, svm->ghcb_sa, count, in);
2608 }
2609 
2610 void sev_es_init_vmcb(struct vcpu_svm *svm)
2611 {
2612 	struct kvm_vcpu *vcpu = &svm->vcpu;
2613 
2614 	svm->vmcb->control.nested_ctl |= SVM_NESTED_CTL_SEV_ES_ENABLE;
2615 	svm->vmcb->control.virt_ext |= LBR_CTL_ENABLE_MASK;
2616 
2617 	/*
2618 	 * An SEV-ES guest requires a VMSA area that is a separate from the
2619 	 * VMCB page. Do not include the encryption mask on the VMSA physical
2620 	 * address since hardware will access it using the guest key.
2621 	 */
2622 	svm->vmcb->control.vmsa_pa = __pa(svm->vmsa);
2623 
2624 	/* Can't intercept CR register access, HV can't modify CR registers */
2625 	svm_clr_intercept(svm, INTERCEPT_CR0_READ);
2626 	svm_clr_intercept(svm, INTERCEPT_CR4_READ);
2627 	svm_clr_intercept(svm, INTERCEPT_CR8_READ);
2628 	svm_clr_intercept(svm, INTERCEPT_CR0_WRITE);
2629 	svm_clr_intercept(svm, INTERCEPT_CR4_WRITE);
2630 	svm_clr_intercept(svm, INTERCEPT_CR8_WRITE);
2631 
2632 	svm_clr_intercept(svm, INTERCEPT_SELECTIVE_CR0);
2633 
2634 	/* Track EFER/CR register changes */
2635 	svm_set_intercept(svm, TRAP_EFER_WRITE);
2636 	svm_set_intercept(svm, TRAP_CR0_WRITE);
2637 	svm_set_intercept(svm, TRAP_CR4_WRITE);
2638 	svm_set_intercept(svm, TRAP_CR8_WRITE);
2639 
2640 	/* No support for enable_vmware_backdoor */
2641 	clr_exception_intercept(svm, GP_VECTOR);
2642 
2643 	/* Can't intercept XSETBV, HV can't modify XCR0 directly */
2644 	svm_clr_intercept(svm, INTERCEPT_XSETBV);
2645 
2646 	/* Clear intercepts on selected MSRs */
2647 	set_msr_interception(vcpu, svm->msrpm, MSR_EFER, 1, 1);
2648 	set_msr_interception(vcpu, svm->msrpm, MSR_IA32_CR_PAT, 1, 1);
2649 	set_msr_interception(vcpu, svm->msrpm, MSR_IA32_LASTBRANCHFROMIP, 1, 1);
2650 	set_msr_interception(vcpu, svm->msrpm, MSR_IA32_LASTBRANCHTOIP, 1, 1);
2651 	set_msr_interception(vcpu, svm->msrpm, MSR_IA32_LASTINTFROMIP, 1, 1);
2652 	set_msr_interception(vcpu, svm->msrpm, MSR_IA32_LASTINTTOIP, 1, 1);
2653 }
2654 
2655 void sev_es_vcpu_reset(struct vcpu_svm *svm)
2656 {
2657 	/*
2658 	 * Set the GHCB MSR value as per the GHCB specification when emulating
2659 	 * vCPU RESET for an SEV-ES guest.
2660 	 */
2661 	set_ghcb_msr(svm, GHCB_MSR_SEV_INFO(GHCB_VERSION_MAX,
2662 					    GHCB_VERSION_MIN,
2663 					    sev_enc_bit));
2664 }
2665 
2666 void sev_es_prepare_guest_switch(struct vcpu_svm *svm, unsigned int cpu)
2667 {
2668 	struct svm_cpu_data *sd = per_cpu(svm_data, cpu);
2669 	struct vmcb_save_area *hostsa;
2670 
2671 	/*
2672 	 * As an SEV-ES guest, hardware will restore the host state on VMEXIT,
2673 	 * of which one step is to perform a VMLOAD. Since hardware does not
2674 	 * perform a VMSAVE on VMRUN, the host savearea must be updated.
2675 	 */
2676 	vmsave(__sme_page_pa(sd->save_area));
2677 
2678 	/* XCR0 is restored on VMEXIT, save the current host value */
2679 	hostsa = (struct vmcb_save_area *)(page_address(sd->save_area) + 0x400);
2680 	hostsa->xcr0 = xgetbv(XCR_XFEATURE_ENABLED_MASK);
2681 
2682 	/* PKRU is restored on VMEXIT, save the current host value */
2683 	hostsa->pkru = read_pkru();
2684 
2685 	/* MSR_IA32_XSS is restored on VMEXIT, save the currnet host value */
2686 	hostsa->xss = host_xss;
2687 }
2688 
2689 void sev_vcpu_deliver_sipi_vector(struct kvm_vcpu *vcpu, u8 vector)
2690 {
2691 	struct vcpu_svm *svm = to_svm(vcpu);
2692 
2693 	/* First SIPI: Use the values as initially set by the VMM */
2694 	if (!svm->received_first_sipi) {
2695 		svm->received_first_sipi = true;
2696 		return;
2697 	}
2698 
2699 	/*
2700 	 * Subsequent SIPI: Return from an AP Reset Hold VMGEXIT, where
2701 	 * the guest will set the CS and RIP. Set SW_EXIT_INFO_2 to a
2702 	 * non-zero value.
2703 	 */
2704 	if (!svm->ghcb)
2705 		return;
2706 
2707 	ghcb_set_sw_exit_info_2(svm->ghcb, 1);
2708 }
2709