• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
10 
11 #include <linux/kvm_types.h>
12 #include <linux/kvm_host.h>
13 #include <linux/kernel.h>
14 #include <linux/highmem.h>
15 #include <linux/psp.h>
16 #include <linux/psp-sev.h>
17 #include <linux/pagemap.h>
18 #include <linux/swap.h>
19 #include <linux/misc_cgroup.h>
20 #include <linux/processor.h>
21 #include <linux/trace_events.h>
22 #include <uapi/linux/sev-guest.h>
23 
24 #include <asm/pkru.h>
25 #include <asm/trapnr.h>
26 #include <asm/fpu/xcr.h>
27 #include <asm/fpu/xstate.h>
28 #include <asm/debugreg.h>
29 #include <asm/sev.h>
30 
31 #include "mmu.h"
32 #include "x86.h"
33 #include "svm.h"
34 #include "svm_ops.h"
35 #include "cpuid.h"
36 #include "trace.h"
37 
38 #define GHCB_VERSION_MAX	2ULL
39 #define GHCB_VERSION_DEFAULT	2ULL
40 #define GHCB_VERSION_MIN	1ULL
41 
42 #define GHCB_HV_FT_SUPPORTED	(GHCB_HV_FT_SNP | GHCB_HV_FT_SNP_AP_CREATION)
43 
44 /* enable/disable SEV support */
45 static bool sev_enabled = true;
46 module_param_named(sev, sev_enabled, bool, 0444);
47 
48 /* enable/disable SEV-ES support */
49 static bool sev_es_enabled = true;
50 module_param_named(sev_es, sev_es_enabled, bool, 0444);
51 
52 /* enable/disable SEV-SNP support */
53 static bool sev_snp_enabled = true;
54 module_param_named(sev_snp, sev_snp_enabled, bool, 0444);
55 
56 /* enable/disable SEV-ES DebugSwap support */
57 static bool sev_es_debug_swap_enabled = true;
58 module_param_named(debug_swap, sev_es_debug_swap_enabled, bool, 0444);
59 static u64 sev_supported_vmsa_features;
60 
61 #define AP_RESET_HOLD_NONE		0
62 #define AP_RESET_HOLD_NAE_EVENT		1
63 #define AP_RESET_HOLD_MSR_PROTO		2
64 
65 /* As defined by SEV-SNP Firmware ABI, under "Guest Policy". */
66 #define SNP_POLICY_MASK_API_MINOR	GENMASK_ULL(7, 0)
67 #define SNP_POLICY_MASK_API_MAJOR	GENMASK_ULL(15, 8)
68 #define SNP_POLICY_MASK_SMT		BIT_ULL(16)
69 #define SNP_POLICY_MASK_RSVD_MBO	BIT_ULL(17)
70 #define SNP_POLICY_MASK_DEBUG		BIT_ULL(19)
71 #define SNP_POLICY_MASK_SINGLE_SOCKET	BIT_ULL(20)
72 
73 #define SNP_POLICY_MASK_VALID		(SNP_POLICY_MASK_API_MINOR	| \
74 					 SNP_POLICY_MASK_API_MAJOR	| \
75 					 SNP_POLICY_MASK_SMT		| \
76 					 SNP_POLICY_MASK_RSVD_MBO	| \
77 					 SNP_POLICY_MASK_DEBUG		| \
78 					 SNP_POLICY_MASK_SINGLE_SOCKET)
79 
80 #define INITIAL_VMSA_GPA 0xFFFFFFFFF000
81 
82 static u8 sev_enc_bit;
83 static DECLARE_RWSEM(sev_deactivate_lock);
84 static DEFINE_MUTEX(sev_bitmap_lock);
85 unsigned int max_sev_asid;
86 static unsigned int min_sev_asid;
87 static unsigned long sev_me_mask;
88 static unsigned int nr_asids;
89 static unsigned long *sev_asid_bitmap;
90 static unsigned long *sev_reclaim_asid_bitmap;
91 
92 static int snp_decommission_context(struct kvm *kvm);
93 
94 struct enc_region {
95 	struct list_head list;
96 	unsigned long npages;
97 	struct page **pages;
98 	unsigned long uaddr;
99 	unsigned long size;
100 };
101 
102 /* Called with the sev_bitmap_lock held, or on shutdown  */
sev_flush_asids(unsigned int min_asid,unsigned int max_asid)103 static int sev_flush_asids(unsigned int min_asid, unsigned int max_asid)
104 {
105 	int ret, error = 0;
106 	unsigned int asid;
107 
108 	/* Check if there are any ASIDs to reclaim before performing a flush */
109 	asid = find_next_bit(sev_reclaim_asid_bitmap, nr_asids, min_asid);
110 	if (asid > max_asid)
111 		return -EBUSY;
112 
113 	/*
114 	 * DEACTIVATE will clear the WBINVD indicator causing DF_FLUSH to fail,
115 	 * so it must be guarded.
116 	 */
117 	down_write(&sev_deactivate_lock);
118 
119 	wbinvd_on_all_cpus();
120 
121 	if (sev_snp_enabled)
122 		ret = sev_do_cmd(SEV_CMD_SNP_DF_FLUSH, NULL, &error);
123 	else
124 		ret = sev_guest_df_flush(&error);
125 
126 	up_write(&sev_deactivate_lock);
127 
128 	if (ret)
129 		pr_err("SEV%s: DF_FLUSH failed, ret=%d, error=%#x\n",
130 		       sev_snp_enabled ? "-SNP" : "", ret, error);
131 
132 	return ret;
133 }
134 
is_mirroring_enc_context(struct kvm * kvm)135 static inline bool is_mirroring_enc_context(struct kvm *kvm)
136 {
137 	return !!to_kvm_sev_info(kvm)->enc_context_owner;
138 }
139 
sev_vcpu_has_debug_swap(struct vcpu_svm * svm)140 static bool sev_vcpu_has_debug_swap(struct vcpu_svm *svm)
141 {
142 	struct kvm_vcpu *vcpu = &svm->vcpu;
143 	struct kvm_sev_info *sev = &to_kvm_svm(vcpu->kvm)->sev_info;
144 
145 	return sev->vmsa_features & SVM_SEV_FEAT_DEBUG_SWAP;
146 }
147 
148 /* Must be called with the sev_bitmap_lock held */
__sev_recycle_asids(unsigned int min_asid,unsigned int max_asid)149 static bool __sev_recycle_asids(unsigned int min_asid, unsigned int max_asid)
150 {
151 	if (sev_flush_asids(min_asid, max_asid))
152 		return false;
153 
154 	/* The flush process will flush all reclaimable SEV and SEV-ES ASIDs */
155 	bitmap_xor(sev_asid_bitmap, sev_asid_bitmap, sev_reclaim_asid_bitmap,
156 		   nr_asids);
157 	bitmap_zero(sev_reclaim_asid_bitmap, nr_asids);
158 
159 	return true;
160 }
161 
sev_misc_cg_try_charge(struct kvm_sev_info * sev)162 static int sev_misc_cg_try_charge(struct kvm_sev_info *sev)
163 {
164 	enum misc_res_type type = sev->es_active ? MISC_CG_RES_SEV_ES : MISC_CG_RES_SEV;
165 	return misc_cg_try_charge(type, sev->misc_cg, 1);
166 }
167 
sev_misc_cg_uncharge(struct kvm_sev_info * sev)168 static void sev_misc_cg_uncharge(struct kvm_sev_info *sev)
169 {
170 	enum misc_res_type type = sev->es_active ? MISC_CG_RES_SEV_ES : MISC_CG_RES_SEV;
171 	misc_cg_uncharge(type, sev->misc_cg, 1);
172 }
173 
sev_asid_new(struct kvm_sev_info * sev)174 static int sev_asid_new(struct kvm_sev_info *sev)
175 {
176 	/*
177 	 * SEV-enabled guests must use asid from min_sev_asid to max_sev_asid.
178 	 * SEV-ES-enabled guest can use from 1 to min_sev_asid - 1.
179 	 * Note: min ASID can end up larger than the max if basic SEV support is
180 	 * effectively disabled by disallowing use of ASIDs for SEV guests.
181 	 */
182 	unsigned int min_asid = sev->es_active ? 1 : min_sev_asid;
183 	unsigned int max_asid = sev->es_active ? min_sev_asid - 1 : max_sev_asid;
184 	unsigned int asid;
185 	bool retry = true;
186 	int ret;
187 
188 	if (min_asid > max_asid)
189 		return -ENOTTY;
190 
191 	WARN_ON(sev->misc_cg);
192 	sev->misc_cg = get_current_misc_cg();
193 	ret = sev_misc_cg_try_charge(sev);
194 	if (ret) {
195 		put_misc_cg(sev->misc_cg);
196 		sev->misc_cg = NULL;
197 		return ret;
198 	}
199 
200 	mutex_lock(&sev_bitmap_lock);
201 
202 again:
203 	asid = find_next_zero_bit(sev_asid_bitmap, max_asid + 1, min_asid);
204 	if (asid > max_asid) {
205 		if (retry && __sev_recycle_asids(min_asid, max_asid)) {
206 			retry = false;
207 			goto again;
208 		}
209 		mutex_unlock(&sev_bitmap_lock);
210 		ret = -EBUSY;
211 		goto e_uncharge;
212 	}
213 
214 	__set_bit(asid, sev_asid_bitmap);
215 
216 	mutex_unlock(&sev_bitmap_lock);
217 
218 	sev->asid = asid;
219 	return 0;
220 e_uncharge:
221 	sev_misc_cg_uncharge(sev);
222 	put_misc_cg(sev->misc_cg);
223 	sev->misc_cg = NULL;
224 	return ret;
225 }
226 
sev_get_asid(struct kvm * kvm)227 static unsigned int sev_get_asid(struct kvm *kvm)
228 {
229 	struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
230 
231 	return sev->asid;
232 }
233 
sev_asid_free(struct kvm_sev_info * sev)234 static void sev_asid_free(struct kvm_sev_info *sev)
235 {
236 	struct svm_cpu_data *sd;
237 	int cpu;
238 
239 	mutex_lock(&sev_bitmap_lock);
240 
241 	__set_bit(sev->asid, sev_reclaim_asid_bitmap);
242 
243 	for_each_possible_cpu(cpu) {
244 		sd = per_cpu_ptr(&svm_data, cpu);
245 		sd->sev_vmcbs[sev->asid] = NULL;
246 	}
247 
248 	mutex_unlock(&sev_bitmap_lock);
249 
250 	sev_misc_cg_uncharge(sev);
251 	put_misc_cg(sev->misc_cg);
252 	sev->misc_cg = NULL;
253 }
254 
sev_decommission(unsigned int handle)255 static void sev_decommission(unsigned int handle)
256 {
257 	struct sev_data_decommission decommission;
258 
259 	if (!handle)
260 		return;
261 
262 	decommission.handle = handle;
263 	sev_guest_decommission(&decommission, NULL);
264 }
265 
266 /*
267  * Transition a page to hypervisor-owned/shared state in the RMP table. This
268  * should not fail under normal conditions, but leak the page should that
269  * happen since it will no longer be usable by the host due to RMP protections.
270  */
kvm_rmp_make_shared(struct kvm * kvm,u64 pfn,enum pg_level level)271 static int kvm_rmp_make_shared(struct kvm *kvm, u64 pfn, enum pg_level level)
272 {
273 	if (KVM_BUG_ON(rmp_make_shared(pfn, level), kvm)) {
274 		snp_leak_pages(pfn, page_level_size(level) >> PAGE_SHIFT);
275 		return -EIO;
276 	}
277 
278 	return 0;
279 }
280 
281 /*
282  * Certain page-states, such as Pre-Guest and Firmware pages (as documented
283  * in Chapter 5 of the SEV-SNP Firmware ABI under "Page States") cannot be
284  * directly transitioned back to normal/hypervisor-owned state via RMPUPDATE
285  * unless they are reclaimed first.
286  *
287  * Until they are reclaimed and subsequently transitioned via RMPUPDATE, they
288  * might not be usable by the host due to being set as immutable or still
289  * being associated with a guest ASID.
290  *
291  * Bug the VM and leak the page if reclaim fails, or if the RMP entry can't be
292  * converted back to shared, as the page is no longer usable due to RMP
293  * protections, and it's infeasible for the guest to continue on.
294  */
snp_page_reclaim(struct kvm * kvm,u64 pfn)295 static int snp_page_reclaim(struct kvm *kvm, u64 pfn)
296 {
297 	struct sev_data_snp_page_reclaim data = {0};
298 	int fw_err, rc;
299 
300 	data.paddr = __sme_set(pfn << PAGE_SHIFT);
301 	rc = sev_do_cmd(SEV_CMD_SNP_PAGE_RECLAIM, &data, &fw_err);
302 	if (KVM_BUG(rc, kvm, "Failed to reclaim PFN %llx, rc %d fw_err %d", pfn, rc, fw_err)) {
303 		snp_leak_pages(pfn, 1);
304 		return -EIO;
305 	}
306 
307 	if (kvm_rmp_make_shared(kvm, pfn, PG_LEVEL_4K))
308 		return -EIO;
309 
310 	return rc;
311 }
312 
sev_unbind_asid(struct kvm * kvm,unsigned int handle)313 static void sev_unbind_asid(struct kvm *kvm, unsigned int handle)
314 {
315 	struct sev_data_deactivate deactivate;
316 
317 	if (!handle)
318 		return;
319 
320 	deactivate.handle = handle;
321 
322 	/* Guard DEACTIVATE against WBINVD/DF_FLUSH used in ASID recycling */
323 	down_read(&sev_deactivate_lock);
324 	sev_guest_deactivate(&deactivate, NULL);
325 	up_read(&sev_deactivate_lock);
326 
327 	sev_decommission(handle);
328 }
329 
330 /*
331  * This sets up bounce buffers/firmware pages to handle SNP Guest Request
332  * messages (e.g. attestation requests). See "SNP Guest Request" in the GHCB
333  * 2.0 specification for more details.
334  *
335  * Technically, when an SNP Guest Request is issued, the guest will provide its
336  * own request/response pages, which could in theory be passed along directly
337  * to firmware rather than using bounce pages. However, these pages would need
338  * special care:
339  *
340  *   - Both pages are from shared guest memory, so they need to be protected
341  *     from migration/etc. occurring while firmware reads/writes to them. At a
342  *     minimum, this requires elevating the ref counts and potentially needing
343  *     an explicit pinning of the memory. This places additional restrictions
344  *     on what type of memory backends userspace can use for shared guest
345  *     memory since there is some reliance on using refcounted pages.
346  *
347  *   - The response page needs to be switched to Firmware-owned[1] state
348  *     before the firmware can write to it, which can lead to potential
349  *     host RMP #PFs if the guest is misbehaved and hands the host a
350  *     guest page that KVM might write to for other reasons (e.g. virtio
351  *     buffers/etc.).
352  *
353  * Both of these issues can be avoided completely by using separately-allocated
354  * bounce pages for both the request/response pages and passing those to
355  * firmware instead. So that's what is being set up here.
356  *
357  * Guest requests rely on message sequence numbers to ensure requests are
358  * issued to firmware in the order the guest issues them, so concurrent guest
359  * requests generally shouldn't happen. But a misbehaved guest could issue
360  * concurrent guest requests in theory, so a mutex is used to serialize
361  * access to the bounce buffers.
362  *
363  * [1] See the "Page States" section of the SEV-SNP Firmware ABI for more
364  *     details on Firmware-owned pages, along with "RMP and VMPL Access Checks"
365  *     in the APM for details on the related RMP restrictions.
366  */
snp_guest_req_init(struct kvm * kvm)367 static int snp_guest_req_init(struct kvm *kvm)
368 {
369 	struct kvm_sev_info *sev = to_kvm_sev_info(kvm);
370 	struct page *req_page;
371 
372 	req_page = alloc_page(GFP_KERNEL_ACCOUNT | __GFP_ZERO);
373 	if (!req_page)
374 		return -ENOMEM;
375 
376 	sev->guest_resp_buf = snp_alloc_firmware_page(GFP_KERNEL_ACCOUNT | __GFP_ZERO);
377 	if (!sev->guest_resp_buf) {
378 		__free_page(req_page);
379 		return -EIO;
380 	}
381 
382 	sev->guest_req_buf = page_address(req_page);
383 	mutex_init(&sev->guest_req_mutex);
384 
385 	return 0;
386 }
387 
snp_guest_req_cleanup(struct kvm * kvm)388 static void snp_guest_req_cleanup(struct kvm *kvm)
389 {
390 	struct kvm_sev_info *sev = to_kvm_sev_info(kvm);
391 
392 	if (sev->guest_resp_buf)
393 		snp_free_firmware_page(sev->guest_resp_buf);
394 
395 	if (sev->guest_req_buf)
396 		__free_page(virt_to_page(sev->guest_req_buf));
397 
398 	sev->guest_req_buf = NULL;
399 	sev->guest_resp_buf = NULL;
400 }
401 
__sev_guest_init(struct kvm * kvm,struct kvm_sev_cmd * argp,struct kvm_sev_init * data,unsigned long vm_type)402 static int __sev_guest_init(struct kvm *kvm, struct kvm_sev_cmd *argp,
403 			    struct kvm_sev_init *data,
404 			    unsigned long vm_type)
405 {
406 	struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
407 	struct sev_platform_init_args init_args = {0};
408 	bool es_active = vm_type != KVM_X86_SEV_VM;
409 	u64 valid_vmsa_features = es_active ? sev_supported_vmsa_features : 0;
410 	int ret;
411 
412 	if (kvm->created_vcpus)
413 		return -EINVAL;
414 
415 	if (data->flags)
416 		return -EINVAL;
417 
418 	if (data->vmsa_features & ~valid_vmsa_features)
419 		return -EINVAL;
420 
421 	if (data->ghcb_version > GHCB_VERSION_MAX || (!es_active && data->ghcb_version))
422 		return -EINVAL;
423 
424 	if (unlikely(sev->active))
425 		return -EINVAL;
426 
427 	sev->active = true;
428 	sev->es_active = es_active;
429 	sev->vmsa_features = data->vmsa_features;
430 	sev->ghcb_version = data->ghcb_version;
431 
432 	/*
433 	 * Currently KVM supports the full range of mandatory features defined
434 	 * by version 2 of the GHCB protocol, so default to that for SEV-ES
435 	 * guests created via KVM_SEV_INIT2.
436 	 */
437 	if (sev->es_active && !sev->ghcb_version)
438 		sev->ghcb_version = GHCB_VERSION_DEFAULT;
439 
440 	if (vm_type == KVM_X86_SNP_VM)
441 		sev->vmsa_features |= SVM_SEV_FEAT_SNP_ACTIVE;
442 
443 	ret = sev_asid_new(sev);
444 	if (ret)
445 		goto e_no_asid;
446 
447 	init_args.probe = false;
448 	ret = sev_platform_init(&init_args);
449 	if (ret)
450 		goto e_free;
451 
452 	/* This needs to happen after SEV/SNP firmware initialization. */
453 	if (vm_type == KVM_X86_SNP_VM) {
454 		ret = snp_guest_req_init(kvm);
455 		if (ret)
456 			goto e_free;
457 	}
458 
459 	INIT_LIST_HEAD(&sev->regions_list);
460 	INIT_LIST_HEAD(&sev->mirror_vms);
461 	sev->need_init = false;
462 
463 	kvm_set_apicv_inhibit(kvm, APICV_INHIBIT_REASON_SEV);
464 
465 	return 0;
466 
467 e_free:
468 	argp->error = init_args.error;
469 	sev_asid_free(sev);
470 	sev->asid = 0;
471 e_no_asid:
472 	sev->vmsa_features = 0;
473 	sev->es_active = false;
474 	sev->active = false;
475 	return ret;
476 }
477 
sev_guest_init(struct kvm * kvm,struct kvm_sev_cmd * argp)478 static int sev_guest_init(struct kvm *kvm, struct kvm_sev_cmd *argp)
479 {
480 	struct kvm_sev_init data = {
481 		.vmsa_features = 0,
482 		.ghcb_version = 0,
483 	};
484 	unsigned long vm_type;
485 
486 	if (kvm->arch.vm_type != KVM_X86_DEFAULT_VM)
487 		return -EINVAL;
488 
489 	vm_type = (argp->id == KVM_SEV_INIT ? KVM_X86_SEV_VM : KVM_X86_SEV_ES_VM);
490 
491 	/*
492 	 * KVM_SEV_ES_INIT has been deprecated by KVM_SEV_INIT2, so it will
493 	 * continue to only ever support the minimal GHCB protocol version.
494 	 */
495 	if (vm_type == KVM_X86_SEV_ES_VM)
496 		data.ghcb_version = GHCB_VERSION_MIN;
497 
498 	return __sev_guest_init(kvm, argp, &data, vm_type);
499 }
500 
sev_guest_init2(struct kvm * kvm,struct kvm_sev_cmd * argp)501 static int sev_guest_init2(struct kvm *kvm, struct kvm_sev_cmd *argp)
502 {
503 	struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
504 	struct kvm_sev_init data;
505 
506 	if (!sev->need_init)
507 		return -EINVAL;
508 
509 	if (kvm->arch.vm_type != KVM_X86_SEV_VM &&
510 	    kvm->arch.vm_type != KVM_X86_SEV_ES_VM &&
511 	    kvm->arch.vm_type != KVM_X86_SNP_VM)
512 		return -EINVAL;
513 
514 	if (copy_from_user(&data, u64_to_user_ptr(argp->data), sizeof(data)))
515 		return -EFAULT;
516 
517 	return __sev_guest_init(kvm, argp, &data, kvm->arch.vm_type);
518 }
519 
sev_bind_asid(struct kvm * kvm,unsigned int handle,int * error)520 static int sev_bind_asid(struct kvm *kvm, unsigned int handle, int *error)
521 {
522 	unsigned int asid = sev_get_asid(kvm);
523 	struct sev_data_activate activate;
524 	int ret;
525 
526 	/* activate ASID on the given handle */
527 	activate.handle = handle;
528 	activate.asid   = asid;
529 	ret = sev_guest_activate(&activate, error);
530 
531 	return ret;
532 }
533 
__sev_issue_cmd(int fd,int id,void * data,int * error)534 static int __sev_issue_cmd(int fd, int id, void *data, int *error)
535 {
536 	struct fd f;
537 	int ret;
538 
539 	f = fdget(fd);
540 	if (!fd_file(f))
541 		return -EBADF;
542 
543 	ret = sev_issue_cmd_external_user(fd_file(f), id, data, error);
544 
545 	fdput(f);
546 	return ret;
547 }
548 
sev_issue_cmd(struct kvm * kvm,int id,void * data,int * error)549 static int sev_issue_cmd(struct kvm *kvm, int id, void *data, int *error)
550 {
551 	struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
552 
553 	return __sev_issue_cmd(sev->fd, id, data, error);
554 }
555 
sev_launch_start(struct kvm * kvm,struct kvm_sev_cmd * argp)556 static int sev_launch_start(struct kvm *kvm, struct kvm_sev_cmd *argp)
557 {
558 	struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
559 	struct sev_data_launch_start start;
560 	struct kvm_sev_launch_start params;
561 	void *dh_blob, *session_blob;
562 	int *error = &argp->error;
563 	int ret;
564 
565 	if (!sev_guest(kvm))
566 		return -ENOTTY;
567 
568 	if (copy_from_user(&params, u64_to_user_ptr(argp->data), sizeof(params)))
569 		return -EFAULT;
570 
571 	memset(&start, 0, sizeof(start));
572 
573 	dh_blob = NULL;
574 	if (params.dh_uaddr) {
575 		dh_blob = psp_copy_user_blob(params.dh_uaddr, params.dh_len);
576 		if (IS_ERR(dh_blob))
577 			return PTR_ERR(dh_blob);
578 
579 		start.dh_cert_address = __sme_set(__pa(dh_blob));
580 		start.dh_cert_len = params.dh_len;
581 	}
582 
583 	session_blob = NULL;
584 	if (params.session_uaddr) {
585 		session_blob = psp_copy_user_blob(params.session_uaddr, params.session_len);
586 		if (IS_ERR(session_blob)) {
587 			ret = PTR_ERR(session_blob);
588 			goto e_free_dh;
589 		}
590 
591 		start.session_address = __sme_set(__pa(session_blob));
592 		start.session_len = params.session_len;
593 	}
594 
595 	start.handle = params.handle;
596 	start.policy = params.policy;
597 
598 	/* create memory encryption context */
599 	ret = __sev_issue_cmd(argp->sev_fd, SEV_CMD_LAUNCH_START, &start, error);
600 	if (ret)
601 		goto e_free_session;
602 
603 	/* Bind ASID to this guest */
604 	ret = sev_bind_asid(kvm, start.handle, error);
605 	if (ret) {
606 		sev_decommission(start.handle);
607 		goto e_free_session;
608 	}
609 
610 	/* return handle to userspace */
611 	params.handle = start.handle;
612 	if (copy_to_user(u64_to_user_ptr(argp->data), &params, sizeof(params))) {
613 		sev_unbind_asid(kvm, start.handle);
614 		ret = -EFAULT;
615 		goto e_free_session;
616 	}
617 
618 	sev->handle = start.handle;
619 	sev->fd = argp->sev_fd;
620 
621 e_free_session:
622 	kfree(session_blob);
623 e_free_dh:
624 	kfree(dh_blob);
625 	return ret;
626 }
627 
sev_pin_memory(struct kvm * kvm,unsigned long uaddr,unsigned long ulen,unsigned long * n,int write)628 static struct page **sev_pin_memory(struct kvm *kvm, unsigned long uaddr,
629 				    unsigned long ulen, unsigned long *n,
630 				    int write)
631 {
632 	struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
633 	unsigned long npages, size;
634 	int npinned;
635 	unsigned long locked, lock_limit;
636 	struct page **pages;
637 	unsigned long first, last;
638 	int ret;
639 
640 	lockdep_assert_held(&kvm->lock);
641 
642 	if (ulen == 0 || uaddr + ulen < uaddr)
643 		return ERR_PTR(-EINVAL);
644 
645 	/* Calculate number of pages. */
646 	first = (uaddr & PAGE_MASK) >> PAGE_SHIFT;
647 	last = ((uaddr + ulen - 1) & PAGE_MASK) >> PAGE_SHIFT;
648 	npages = (last - first + 1);
649 
650 	locked = sev->pages_locked + npages;
651 	lock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
652 	if (locked > lock_limit && !capable(CAP_IPC_LOCK)) {
653 		pr_err("SEV: %lu locked pages exceed the lock limit of %lu.\n", locked, lock_limit);
654 		return ERR_PTR(-ENOMEM);
655 	}
656 
657 	if (WARN_ON_ONCE(npages > INT_MAX))
658 		return ERR_PTR(-EINVAL);
659 
660 	/* Avoid using vmalloc for smaller buffers. */
661 	size = npages * sizeof(struct page *);
662 	if (size > PAGE_SIZE)
663 		pages = __vmalloc(size, GFP_KERNEL_ACCOUNT);
664 	else
665 		pages = kmalloc(size, GFP_KERNEL_ACCOUNT);
666 
667 	if (!pages)
668 		return ERR_PTR(-ENOMEM);
669 
670 	/* Pin the user virtual address. */
671 	npinned = pin_user_pages_fast(uaddr, npages, write ? FOLL_WRITE : 0, pages);
672 	if (npinned != npages) {
673 		pr_err("SEV: Failure locking %lu pages.\n", npages);
674 		ret = -ENOMEM;
675 		goto err;
676 	}
677 
678 	*n = npages;
679 	sev->pages_locked = locked;
680 
681 	return pages;
682 
683 err:
684 	if (npinned > 0)
685 		unpin_user_pages(pages, npinned);
686 
687 	kvfree(pages);
688 	return ERR_PTR(ret);
689 }
690 
sev_unpin_memory(struct kvm * kvm,struct page ** pages,unsigned long npages)691 static void sev_unpin_memory(struct kvm *kvm, struct page **pages,
692 			     unsigned long npages)
693 {
694 	struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
695 
696 	unpin_user_pages(pages, npages);
697 	kvfree(pages);
698 	sev->pages_locked -= npages;
699 }
700 
sev_clflush_pages(struct page * pages[],unsigned long npages)701 static void sev_clflush_pages(struct page *pages[], unsigned long npages)
702 {
703 	uint8_t *page_virtual;
704 	unsigned long i;
705 
706 	if (this_cpu_has(X86_FEATURE_SME_COHERENT) || npages == 0 ||
707 	    pages == NULL)
708 		return;
709 
710 	for (i = 0; i < npages; i++) {
711 		page_virtual = kmap_local_page(pages[i]);
712 		clflush_cache_range(page_virtual, PAGE_SIZE);
713 		kunmap_local(page_virtual);
714 		cond_resched();
715 	}
716 }
717 
get_num_contig_pages(unsigned long idx,struct page ** inpages,unsigned long npages)718 static unsigned long get_num_contig_pages(unsigned long idx,
719 				struct page **inpages, unsigned long npages)
720 {
721 	unsigned long paddr, next_paddr;
722 	unsigned long i = idx + 1, pages = 1;
723 
724 	/* find the number of contiguous pages starting from idx */
725 	paddr = __sme_page_pa(inpages[idx]);
726 	while (i < npages) {
727 		next_paddr = __sme_page_pa(inpages[i++]);
728 		if ((paddr + PAGE_SIZE) == next_paddr) {
729 			pages++;
730 			paddr = next_paddr;
731 			continue;
732 		}
733 		break;
734 	}
735 
736 	return pages;
737 }
738 
sev_launch_update_data(struct kvm * kvm,struct kvm_sev_cmd * argp)739 static int sev_launch_update_data(struct kvm *kvm, struct kvm_sev_cmd *argp)
740 {
741 	unsigned long vaddr, vaddr_end, next_vaddr, npages, pages, size, i;
742 	struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
743 	struct kvm_sev_launch_update_data params;
744 	struct sev_data_launch_update_data data;
745 	struct page **inpages;
746 	int ret;
747 
748 	if (!sev_guest(kvm))
749 		return -ENOTTY;
750 
751 	if (copy_from_user(&params, u64_to_user_ptr(argp->data), sizeof(params)))
752 		return -EFAULT;
753 
754 	vaddr = params.uaddr;
755 	size = params.len;
756 	vaddr_end = vaddr + size;
757 
758 	/* Lock the user memory. */
759 	inpages = sev_pin_memory(kvm, vaddr, size, &npages, 1);
760 	if (IS_ERR(inpages))
761 		return PTR_ERR(inpages);
762 
763 	/*
764 	 * Flush (on non-coherent CPUs) before LAUNCH_UPDATE encrypts pages in
765 	 * place; the cache may contain the data that was written unencrypted.
766 	 */
767 	sev_clflush_pages(inpages, npages);
768 
769 	data.reserved = 0;
770 	data.handle = sev->handle;
771 
772 	for (i = 0; vaddr < vaddr_end; vaddr = next_vaddr, i += pages) {
773 		int offset, len;
774 
775 		/*
776 		 * If the user buffer is not page-aligned, calculate the offset
777 		 * within the page.
778 		 */
779 		offset = vaddr & (PAGE_SIZE - 1);
780 
781 		/* Calculate the number of pages that can be encrypted in one go. */
782 		pages = get_num_contig_pages(i, inpages, npages);
783 
784 		len = min_t(size_t, ((pages * PAGE_SIZE) - offset), size);
785 
786 		data.len = len;
787 		data.address = __sme_page_pa(inpages[i]) + offset;
788 		ret = sev_issue_cmd(kvm, SEV_CMD_LAUNCH_UPDATE_DATA, &data, &argp->error);
789 		if (ret)
790 			goto e_unpin;
791 
792 		size -= len;
793 		next_vaddr = vaddr + len;
794 	}
795 
796 e_unpin:
797 	/* content of memory is updated, mark pages dirty */
798 	for (i = 0; i < npages; i++) {
799 		set_page_dirty_lock(inpages[i]);
800 		mark_page_accessed(inpages[i]);
801 	}
802 	/* unlock the user pages */
803 	sev_unpin_memory(kvm, inpages, npages);
804 	return ret;
805 }
806 
sev_es_sync_vmsa(struct vcpu_svm * svm)807 static int sev_es_sync_vmsa(struct vcpu_svm *svm)
808 {
809 	struct kvm_vcpu *vcpu = &svm->vcpu;
810 	struct kvm_sev_info *sev = &to_kvm_svm(vcpu->kvm)->sev_info;
811 	struct sev_es_save_area *save = svm->sev_es.vmsa;
812 	struct xregs_state *xsave;
813 	const u8 *s;
814 	u8 *d;
815 	int i;
816 
817 	/* Check some debug related fields before encrypting the VMSA */
818 	if (svm->vcpu.guest_debug || (svm->vmcb->save.dr7 & ~DR7_FIXED_1))
819 		return -EINVAL;
820 
821 	/*
822 	 * SEV-ES will use a VMSA that is pointed to by the VMCB, not
823 	 * the traditional VMSA that is part of the VMCB. Copy the
824 	 * traditional VMSA as it has been built so far (in prep
825 	 * for LAUNCH_UPDATE_VMSA) to be the initial SEV-ES state.
826 	 */
827 	memcpy(save, &svm->vmcb->save, sizeof(svm->vmcb->save));
828 
829 	/* Sync registgers */
830 	save->rax = svm->vcpu.arch.regs[VCPU_REGS_RAX];
831 	save->rbx = svm->vcpu.arch.regs[VCPU_REGS_RBX];
832 	save->rcx = svm->vcpu.arch.regs[VCPU_REGS_RCX];
833 	save->rdx = svm->vcpu.arch.regs[VCPU_REGS_RDX];
834 	save->rsp = svm->vcpu.arch.regs[VCPU_REGS_RSP];
835 	save->rbp = svm->vcpu.arch.regs[VCPU_REGS_RBP];
836 	save->rsi = svm->vcpu.arch.regs[VCPU_REGS_RSI];
837 	save->rdi = svm->vcpu.arch.regs[VCPU_REGS_RDI];
838 #ifdef CONFIG_X86_64
839 	save->r8  = svm->vcpu.arch.regs[VCPU_REGS_R8];
840 	save->r9  = svm->vcpu.arch.regs[VCPU_REGS_R9];
841 	save->r10 = svm->vcpu.arch.regs[VCPU_REGS_R10];
842 	save->r11 = svm->vcpu.arch.regs[VCPU_REGS_R11];
843 	save->r12 = svm->vcpu.arch.regs[VCPU_REGS_R12];
844 	save->r13 = svm->vcpu.arch.regs[VCPU_REGS_R13];
845 	save->r14 = svm->vcpu.arch.regs[VCPU_REGS_R14];
846 	save->r15 = svm->vcpu.arch.regs[VCPU_REGS_R15];
847 #endif
848 	save->rip = svm->vcpu.arch.regs[VCPU_REGS_RIP];
849 
850 	/* Sync some non-GPR registers before encrypting */
851 	save->xcr0 = svm->vcpu.arch.xcr0;
852 	save->pkru = svm->vcpu.arch.pkru;
853 	save->xss  = svm->vcpu.arch.ia32_xss;
854 	save->dr6  = svm->vcpu.arch.dr6;
855 
856 	save->sev_features = sev->vmsa_features;
857 
858 	/*
859 	 * Skip FPU and AVX setup with KVM_SEV_ES_INIT to avoid
860 	 * breaking older measurements.
861 	 */
862 	if (vcpu->kvm->arch.vm_type != KVM_X86_DEFAULT_VM) {
863 		xsave = &vcpu->arch.guest_fpu.fpstate->regs.xsave;
864 		save->x87_dp = xsave->i387.rdp;
865 		save->mxcsr = xsave->i387.mxcsr;
866 		save->x87_ftw = xsave->i387.twd;
867 		save->x87_fsw = xsave->i387.swd;
868 		save->x87_fcw = xsave->i387.cwd;
869 		save->x87_fop = xsave->i387.fop;
870 		save->x87_ds = 0;
871 		save->x87_cs = 0;
872 		save->x87_rip = xsave->i387.rip;
873 
874 		for (i = 0; i < 8; i++) {
875 			/*
876 			 * The format of the x87 save area is undocumented and
877 			 * definitely not what you would expect.  It consists of
878 			 * an 8*8 bytes area with bytes 0-7, and an 8*2 bytes
879 			 * area with bytes 8-9 of each register.
880 			 */
881 			d = save->fpreg_x87 + i * 8;
882 			s = ((u8 *)xsave->i387.st_space) + i * 16;
883 			memcpy(d, s, 8);
884 			save->fpreg_x87[64 + i * 2] = s[8];
885 			save->fpreg_x87[64 + i * 2 + 1] = s[9];
886 		}
887 		memcpy(save->fpreg_xmm, xsave->i387.xmm_space, 256);
888 
889 		s = get_xsave_addr(xsave, XFEATURE_YMM);
890 		if (s)
891 			memcpy(save->fpreg_ymm, s, 256);
892 		else
893 			memset(save->fpreg_ymm, 0, 256);
894 	}
895 
896 	pr_debug("Virtual Machine Save Area (VMSA):\n");
897 	print_hex_dump_debug("", DUMP_PREFIX_NONE, 16, 1, save, sizeof(*save), false);
898 
899 	return 0;
900 }
901 
__sev_launch_update_vmsa(struct kvm * kvm,struct kvm_vcpu * vcpu,int * error)902 static int __sev_launch_update_vmsa(struct kvm *kvm, struct kvm_vcpu *vcpu,
903 				    int *error)
904 {
905 	struct sev_data_launch_update_vmsa vmsa;
906 	struct vcpu_svm *svm = to_svm(vcpu);
907 	int ret;
908 
909 	if (vcpu->guest_debug) {
910 		pr_warn_once("KVM_SET_GUEST_DEBUG for SEV-ES guest is not supported");
911 		return -EINVAL;
912 	}
913 
914 	/* Perform some pre-encryption checks against the VMSA */
915 	ret = sev_es_sync_vmsa(svm);
916 	if (ret)
917 		return ret;
918 
919 	/*
920 	 * The LAUNCH_UPDATE_VMSA command will perform in-place encryption of
921 	 * the VMSA memory content (i.e it will write the same memory region
922 	 * with the guest's key), so invalidate it first.
923 	 */
924 	clflush_cache_range(svm->sev_es.vmsa, PAGE_SIZE);
925 
926 	vmsa.reserved = 0;
927 	vmsa.handle = to_kvm_sev_info(kvm)->handle;
928 	vmsa.address = __sme_pa(svm->sev_es.vmsa);
929 	vmsa.len = PAGE_SIZE;
930 	ret = sev_issue_cmd(kvm, SEV_CMD_LAUNCH_UPDATE_VMSA, &vmsa, error);
931 	if (ret)
932 	  return ret;
933 
934 	/*
935 	 * SEV-ES guests maintain an encrypted version of their FPU
936 	 * state which is restored and saved on VMRUN and VMEXIT.
937 	 * Mark vcpu->arch.guest_fpu->fpstate as scratch so it won't
938 	 * do xsave/xrstor on it.
939 	 */
940 	fpstate_set_confidential(&vcpu->arch.guest_fpu);
941 	vcpu->arch.guest_state_protected = true;
942 
943 	/*
944 	 * SEV-ES guest mandates LBR Virtualization to be _always_ ON. Enable it
945 	 * only after setting guest_state_protected because KVM_SET_MSRS allows
946 	 * dynamic toggling of LBRV (for performance reason) on write access to
947 	 * MSR_IA32_DEBUGCTLMSR when guest_state_protected is not set.
948 	 */
949 	svm_enable_lbrv(vcpu);
950 	return 0;
951 }
952 
sev_launch_update_vmsa(struct kvm * kvm,struct kvm_sev_cmd * argp)953 static int sev_launch_update_vmsa(struct kvm *kvm, struct kvm_sev_cmd *argp)
954 {
955 	struct kvm_vcpu *vcpu;
956 	unsigned long i;
957 	int ret;
958 
959 	if (!sev_es_guest(kvm))
960 		return -ENOTTY;
961 
962 	kvm_for_each_vcpu(i, vcpu, kvm) {
963 		ret = mutex_lock_killable(&vcpu->mutex);
964 		if (ret)
965 			return ret;
966 
967 		ret = __sev_launch_update_vmsa(kvm, vcpu, &argp->error);
968 
969 		mutex_unlock(&vcpu->mutex);
970 		if (ret)
971 			return ret;
972 	}
973 
974 	return 0;
975 }
976 
sev_launch_measure(struct kvm * kvm,struct kvm_sev_cmd * argp)977 static int sev_launch_measure(struct kvm *kvm, struct kvm_sev_cmd *argp)
978 {
979 	void __user *measure = u64_to_user_ptr(argp->data);
980 	struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
981 	struct sev_data_launch_measure data;
982 	struct kvm_sev_launch_measure params;
983 	void __user *p = NULL;
984 	void *blob = NULL;
985 	int ret;
986 
987 	if (!sev_guest(kvm))
988 		return -ENOTTY;
989 
990 	if (copy_from_user(&params, measure, sizeof(params)))
991 		return -EFAULT;
992 
993 	memset(&data, 0, sizeof(data));
994 
995 	/* User wants to query the blob length */
996 	if (!params.len)
997 		goto cmd;
998 
999 	p = u64_to_user_ptr(params.uaddr);
1000 	if (p) {
1001 		if (params.len > SEV_FW_BLOB_MAX_SIZE)
1002 			return -EINVAL;
1003 
1004 		blob = kzalloc(params.len, GFP_KERNEL_ACCOUNT);
1005 		if (!blob)
1006 			return -ENOMEM;
1007 
1008 		data.address = __psp_pa(blob);
1009 		data.len = params.len;
1010 	}
1011 
1012 cmd:
1013 	data.handle = sev->handle;
1014 	ret = sev_issue_cmd(kvm, SEV_CMD_LAUNCH_MEASURE, &data, &argp->error);
1015 
1016 	/*
1017 	 * If we query the session length, FW responded with expected data.
1018 	 */
1019 	if (!params.len)
1020 		goto done;
1021 
1022 	if (ret)
1023 		goto e_free_blob;
1024 
1025 	if (blob) {
1026 		if (copy_to_user(p, blob, params.len))
1027 			ret = -EFAULT;
1028 	}
1029 
1030 done:
1031 	params.len = data.len;
1032 	if (copy_to_user(measure, &params, sizeof(params)))
1033 		ret = -EFAULT;
1034 e_free_blob:
1035 	kfree(blob);
1036 	return ret;
1037 }
1038 
sev_launch_finish(struct kvm * kvm,struct kvm_sev_cmd * argp)1039 static int sev_launch_finish(struct kvm *kvm, struct kvm_sev_cmd *argp)
1040 {
1041 	struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
1042 	struct sev_data_launch_finish data;
1043 
1044 	if (!sev_guest(kvm))
1045 		return -ENOTTY;
1046 
1047 	data.handle = sev->handle;
1048 	return sev_issue_cmd(kvm, SEV_CMD_LAUNCH_FINISH, &data, &argp->error);
1049 }
1050 
sev_guest_status(struct kvm * kvm,struct kvm_sev_cmd * argp)1051 static int sev_guest_status(struct kvm *kvm, struct kvm_sev_cmd *argp)
1052 {
1053 	struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
1054 	struct kvm_sev_guest_status params;
1055 	struct sev_data_guest_status data;
1056 	int ret;
1057 
1058 	if (!sev_guest(kvm))
1059 		return -ENOTTY;
1060 
1061 	memset(&data, 0, sizeof(data));
1062 
1063 	data.handle = sev->handle;
1064 	ret = sev_issue_cmd(kvm, SEV_CMD_GUEST_STATUS, &data, &argp->error);
1065 	if (ret)
1066 		return ret;
1067 
1068 	params.policy = data.policy;
1069 	params.state = data.state;
1070 	params.handle = data.handle;
1071 
1072 	if (copy_to_user(u64_to_user_ptr(argp->data), &params, sizeof(params)))
1073 		ret = -EFAULT;
1074 
1075 	return ret;
1076 }
1077 
__sev_issue_dbg_cmd(struct kvm * kvm,unsigned long src,unsigned long dst,int size,int * error,bool enc)1078 static int __sev_issue_dbg_cmd(struct kvm *kvm, unsigned long src,
1079 			       unsigned long dst, int size,
1080 			       int *error, bool enc)
1081 {
1082 	struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
1083 	struct sev_data_dbg data;
1084 
1085 	data.reserved = 0;
1086 	data.handle = sev->handle;
1087 	data.dst_addr = dst;
1088 	data.src_addr = src;
1089 	data.len = size;
1090 
1091 	return sev_issue_cmd(kvm,
1092 			     enc ? SEV_CMD_DBG_ENCRYPT : SEV_CMD_DBG_DECRYPT,
1093 			     &data, error);
1094 }
1095 
__sev_dbg_decrypt(struct kvm * kvm,unsigned long src_paddr,unsigned long dst_paddr,int sz,int * err)1096 static int __sev_dbg_decrypt(struct kvm *kvm, unsigned long src_paddr,
1097 			     unsigned long dst_paddr, int sz, int *err)
1098 {
1099 	int offset;
1100 
1101 	/*
1102 	 * Its safe to read more than we are asked, caller should ensure that
1103 	 * destination has enough space.
1104 	 */
1105 	offset = src_paddr & 15;
1106 	src_paddr = round_down(src_paddr, 16);
1107 	sz = round_up(sz + offset, 16);
1108 
1109 	return __sev_issue_dbg_cmd(kvm, src_paddr, dst_paddr, sz, err, false);
1110 }
1111 
__sev_dbg_decrypt_user(struct kvm * kvm,unsigned long paddr,void __user * dst_uaddr,unsigned long dst_paddr,int size,int * err)1112 static int __sev_dbg_decrypt_user(struct kvm *kvm, unsigned long paddr,
1113 				  void __user *dst_uaddr,
1114 				  unsigned long dst_paddr,
1115 				  int size, int *err)
1116 {
1117 	struct page *tpage = NULL;
1118 	int ret, offset;
1119 
1120 	/* if inputs are not 16-byte then use intermediate buffer */
1121 	if (!IS_ALIGNED(dst_paddr, 16) ||
1122 	    !IS_ALIGNED(paddr,     16) ||
1123 	    !IS_ALIGNED(size,      16)) {
1124 		tpage = (void *)alloc_page(GFP_KERNEL_ACCOUNT | __GFP_ZERO);
1125 		if (!tpage)
1126 			return -ENOMEM;
1127 
1128 		dst_paddr = __sme_page_pa(tpage);
1129 	}
1130 
1131 	ret = __sev_dbg_decrypt(kvm, paddr, dst_paddr, size, err);
1132 	if (ret)
1133 		goto e_free;
1134 
1135 	if (tpage) {
1136 		offset = paddr & 15;
1137 		if (copy_to_user(dst_uaddr, page_address(tpage) + offset, size))
1138 			ret = -EFAULT;
1139 	}
1140 
1141 e_free:
1142 	if (tpage)
1143 		__free_page(tpage);
1144 
1145 	return ret;
1146 }
1147 
__sev_dbg_encrypt_user(struct kvm * kvm,unsigned long paddr,void __user * vaddr,unsigned long dst_paddr,void __user * dst_vaddr,int size,int * error)1148 static int __sev_dbg_encrypt_user(struct kvm *kvm, unsigned long paddr,
1149 				  void __user *vaddr,
1150 				  unsigned long dst_paddr,
1151 				  void __user *dst_vaddr,
1152 				  int size, int *error)
1153 {
1154 	struct page *src_tpage = NULL;
1155 	struct page *dst_tpage = NULL;
1156 	int ret, len = size;
1157 
1158 	/* If source buffer is not aligned then use an intermediate buffer */
1159 	if (!IS_ALIGNED((unsigned long)vaddr, 16)) {
1160 		src_tpage = alloc_page(GFP_KERNEL_ACCOUNT);
1161 		if (!src_tpage)
1162 			return -ENOMEM;
1163 
1164 		if (copy_from_user(page_address(src_tpage), vaddr, size)) {
1165 			__free_page(src_tpage);
1166 			return -EFAULT;
1167 		}
1168 
1169 		paddr = __sme_page_pa(src_tpage);
1170 	}
1171 
1172 	/*
1173 	 *  If destination buffer or length is not aligned then do read-modify-write:
1174 	 *   - decrypt destination in an intermediate buffer
1175 	 *   - copy the source buffer in an intermediate buffer
1176 	 *   - use the intermediate buffer as source buffer
1177 	 */
1178 	if (!IS_ALIGNED((unsigned long)dst_vaddr, 16) || !IS_ALIGNED(size, 16)) {
1179 		int dst_offset;
1180 
1181 		dst_tpage = alloc_page(GFP_KERNEL_ACCOUNT);
1182 		if (!dst_tpage) {
1183 			ret = -ENOMEM;
1184 			goto e_free;
1185 		}
1186 
1187 		ret = __sev_dbg_decrypt(kvm, dst_paddr,
1188 					__sme_page_pa(dst_tpage), size, error);
1189 		if (ret)
1190 			goto e_free;
1191 
1192 		/*
1193 		 *  If source is kernel buffer then use memcpy() otherwise
1194 		 *  copy_from_user().
1195 		 */
1196 		dst_offset = dst_paddr & 15;
1197 
1198 		if (src_tpage)
1199 			memcpy(page_address(dst_tpage) + dst_offset,
1200 			       page_address(src_tpage), size);
1201 		else {
1202 			if (copy_from_user(page_address(dst_tpage) + dst_offset,
1203 					   vaddr, size)) {
1204 				ret = -EFAULT;
1205 				goto e_free;
1206 			}
1207 		}
1208 
1209 		paddr = __sme_page_pa(dst_tpage);
1210 		dst_paddr = round_down(dst_paddr, 16);
1211 		len = round_up(size, 16);
1212 	}
1213 
1214 	ret = __sev_issue_dbg_cmd(kvm, paddr, dst_paddr, len, error, true);
1215 
1216 e_free:
1217 	if (src_tpage)
1218 		__free_page(src_tpage);
1219 	if (dst_tpage)
1220 		__free_page(dst_tpage);
1221 	return ret;
1222 }
1223 
sev_dbg_crypt(struct kvm * kvm,struct kvm_sev_cmd * argp,bool dec)1224 static int sev_dbg_crypt(struct kvm *kvm, struct kvm_sev_cmd *argp, bool dec)
1225 {
1226 	unsigned long vaddr, vaddr_end, next_vaddr;
1227 	unsigned long dst_vaddr;
1228 	struct page **src_p, **dst_p;
1229 	struct kvm_sev_dbg debug;
1230 	unsigned long n;
1231 	unsigned int size;
1232 	int ret;
1233 
1234 	if (!sev_guest(kvm))
1235 		return -ENOTTY;
1236 
1237 	if (copy_from_user(&debug, u64_to_user_ptr(argp->data), sizeof(debug)))
1238 		return -EFAULT;
1239 
1240 	if (!debug.len || debug.src_uaddr + debug.len < debug.src_uaddr)
1241 		return -EINVAL;
1242 	if (!debug.dst_uaddr)
1243 		return -EINVAL;
1244 
1245 	vaddr = debug.src_uaddr;
1246 	size = debug.len;
1247 	vaddr_end = vaddr + size;
1248 	dst_vaddr = debug.dst_uaddr;
1249 
1250 	for (; vaddr < vaddr_end; vaddr = next_vaddr) {
1251 		int len, s_off, d_off;
1252 
1253 		/* lock userspace source and destination page */
1254 		src_p = sev_pin_memory(kvm, vaddr & PAGE_MASK, PAGE_SIZE, &n, 0);
1255 		if (IS_ERR(src_p))
1256 			return PTR_ERR(src_p);
1257 
1258 		dst_p = sev_pin_memory(kvm, dst_vaddr & PAGE_MASK, PAGE_SIZE, &n, 1);
1259 		if (IS_ERR(dst_p)) {
1260 			sev_unpin_memory(kvm, src_p, n);
1261 			return PTR_ERR(dst_p);
1262 		}
1263 
1264 		/*
1265 		 * Flush (on non-coherent CPUs) before DBG_{DE,EN}CRYPT read or modify
1266 		 * the pages; flush the destination too so that future accesses do not
1267 		 * see stale data.
1268 		 */
1269 		sev_clflush_pages(src_p, 1);
1270 		sev_clflush_pages(dst_p, 1);
1271 
1272 		/*
1273 		 * Since user buffer may not be page aligned, calculate the
1274 		 * offset within the page.
1275 		 */
1276 		s_off = vaddr & ~PAGE_MASK;
1277 		d_off = dst_vaddr & ~PAGE_MASK;
1278 		len = min_t(size_t, (PAGE_SIZE - s_off), size);
1279 
1280 		if (dec)
1281 			ret = __sev_dbg_decrypt_user(kvm,
1282 						     __sme_page_pa(src_p[0]) + s_off,
1283 						     (void __user *)dst_vaddr,
1284 						     __sme_page_pa(dst_p[0]) + d_off,
1285 						     len, &argp->error);
1286 		else
1287 			ret = __sev_dbg_encrypt_user(kvm,
1288 						     __sme_page_pa(src_p[0]) + s_off,
1289 						     (void __user *)vaddr,
1290 						     __sme_page_pa(dst_p[0]) + d_off,
1291 						     (void __user *)dst_vaddr,
1292 						     len, &argp->error);
1293 
1294 		sev_unpin_memory(kvm, src_p, n);
1295 		sev_unpin_memory(kvm, dst_p, n);
1296 
1297 		if (ret)
1298 			goto err;
1299 
1300 		next_vaddr = vaddr + len;
1301 		dst_vaddr = dst_vaddr + len;
1302 		size -= len;
1303 	}
1304 err:
1305 	return ret;
1306 }
1307 
sev_launch_secret(struct kvm * kvm,struct kvm_sev_cmd * argp)1308 static int sev_launch_secret(struct kvm *kvm, struct kvm_sev_cmd *argp)
1309 {
1310 	struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
1311 	struct sev_data_launch_secret data;
1312 	struct kvm_sev_launch_secret params;
1313 	struct page **pages;
1314 	void *blob, *hdr;
1315 	unsigned long n, i;
1316 	int ret, offset;
1317 
1318 	if (!sev_guest(kvm))
1319 		return -ENOTTY;
1320 
1321 	if (copy_from_user(&params, u64_to_user_ptr(argp->data), sizeof(params)))
1322 		return -EFAULT;
1323 
1324 	pages = sev_pin_memory(kvm, params.guest_uaddr, params.guest_len, &n, 1);
1325 	if (IS_ERR(pages))
1326 		return PTR_ERR(pages);
1327 
1328 	/*
1329 	 * Flush (on non-coherent CPUs) before LAUNCH_SECRET encrypts pages in
1330 	 * place; the cache may contain the data that was written unencrypted.
1331 	 */
1332 	sev_clflush_pages(pages, n);
1333 
1334 	/*
1335 	 * The secret must be copied into contiguous memory region, lets verify
1336 	 * that userspace memory pages are contiguous before we issue command.
1337 	 */
1338 	if (get_num_contig_pages(0, pages, n) != n) {
1339 		ret = -EINVAL;
1340 		goto e_unpin_memory;
1341 	}
1342 
1343 	memset(&data, 0, sizeof(data));
1344 
1345 	offset = params.guest_uaddr & (PAGE_SIZE - 1);
1346 	data.guest_address = __sme_page_pa(pages[0]) + offset;
1347 	data.guest_len = params.guest_len;
1348 
1349 	blob = psp_copy_user_blob(params.trans_uaddr, params.trans_len);
1350 	if (IS_ERR(blob)) {
1351 		ret = PTR_ERR(blob);
1352 		goto e_unpin_memory;
1353 	}
1354 
1355 	data.trans_address = __psp_pa(blob);
1356 	data.trans_len = params.trans_len;
1357 
1358 	hdr = psp_copy_user_blob(params.hdr_uaddr, params.hdr_len);
1359 	if (IS_ERR(hdr)) {
1360 		ret = PTR_ERR(hdr);
1361 		goto e_free_blob;
1362 	}
1363 	data.hdr_address = __psp_pa(hdr);
1364 	data.hdr_len = params.hdr_len;
1365 
1366 	data.handle = sev->handle;
1367 	ret = sev_issue_cmd(kvm, SEV_CMD_LAUNCH_UPDATE_SECRET, &data, &argp->error);
1368 
1369 	kfree(hdr);
1370 
1371 e_free_blob:
1372 	kfree(blob);
1373 e_unpin_memory:
1374 	/* content of memory is updated, mark pages dirty */
1375 	for (i = 0; i < n; i++) {
1376 		set_page_dirty_lock(pages[i]);
1377 		mark_page_accessed(pages[i]);
1378 	}
1379 	sev_unpin_memory(kvm, pages, n);
1380 	return ret;
1381 }
1382 
sev_get_attestation_report(struct kvm * kvm,struct kvm_sev_cmd * argp)1383 static int sev_get_attestation_report(struct kvm *kvm, struct kvm_sev_cmd *argp)
1384 {
1385 	void __user *report = u64_to_user_ptr(argp->data);
1386 	struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
1387 	struct sev_data_attestation_report data;
1388 	struct kvm_sev_attestation_report params;
1389 	void __user *p;
1390 	void *blob = NULL;
1391 	int ret;
1392 
1393 	if (!sev_guest(kvm))
1394 		return -ENOTTY;
1395 
1396 	if (copy_from_user(&params, u64_to_user_ptr(argp->data), sizeof(params)))
1397 		return -EFAULT;
1398 
1399 	memset(&data, 0, sizeof(data));
1400 
1401 	/* User wants to query the blob length */
1402 	if (!params.len)
1403 		goto cmd;
1404 
1405 	p = u64_to_user_ptr(params.uaddr);
1406 	if (p) {
1407 		if (params.len > SEV_FW_BLOB_MAX_SIZE)
1408 			return -EINVAL;
1409 
1410 		blob = kzalloc(params.len, GFP_KERNEL_ACCOUNT);
1411 		if (!blob)
1412 			return -ENOMEM;
1413 
1414 		data.address = __psp_pa(blob);
1415 		data.len = params.len;
1416 		memcpy(data.mnonce, params.mnonce, sizeof(params.mnonce));
1417 	}
1418 cmd:
1419 	data.handle = sev->handle;
1420 	ret = sev_issue_cmd(kvm, SEV_CMD_ATTESTATION_REPORT, &data, &argp->error);
1421 	/*
1422 	 * If we query the session length, FW responded with expected data.
1423 	 */
1424 	if (!params.len)
1425 		goto done;
1426 
1427 	if (ret)
1428 		goto e_free_blob;
1429 
1430 	if (blob) {
1431 		if (copy_to_user(p, blob, params.len))
1432 			ret = -EFAULT;
1433 	}
1434 
1435 done:
1436 	params.len = data.len;
1437 	if (copy_to_user(report, &params, sizeof(params)))
1438 		ret = -EFAULT;
1439 e_free_blob:
1440 	kfree(blob);
1441 	return ret;
1442 }
1443 
1444 /* Userspace wants to query session length. */
1445 static int
__sev_send_start_query_session_length(struct kvm * kvm,struct kvm_sev_cmd * argp,struct kvm_sev_send_start * params)1446 __sev_send_start_query_session_length(struct kvm *kvm, struct kvm_sev_cmd *argp,
1447 				      struct kvm_sev_send_start *params)
1448 {
1449 	struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
1450 	struct sev_data_send_start data;
1451 	int ret;
1452 
1453 	memset(&data, 0, sizeof(data));
1454 	data.handle = sev->handle;
1455 	ret = sev_issue_cmd(kvm, SEV_CMD_SEND_START, &data, &argp->error);
1456 
1457 	params->session_len = data.session_len;
1458 	if (copy_to_user(u64_to_user_ptr(argp->data), params,
1459 				sizeof(struct kvm_sev_send_start)))
1460 		ret = -EFAULT;
1461 
1462 	return ret;
1463 }
1464 
sev_send_start(struct kvm * kvm,struct kvm_sev_cmd * argp)1465 static int sev_send_start(struct kvm *kvm, struct kvm_sev_cmd *argp)
1466 {
1467 	struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
1468 	struct sev_data_send_start data;
1469 	struct kvm_sev_send_start params;
1470 	void *amd_certs, *session_data;
1471 	void *pdh_cert, *plat_certs;
1472 	int ret;
1473 
1474 	if (!sev_guest(kvm))
1475 		return -ENOTTY;
1476 
1477 	if (copy_from_user(&params, u64_to_user_ptr(argp->data),
1478 				sizeof(struct kvm_sev_send_start)))
1479 		return -EFAULT;
1480 
1481 	/* if session_len is zero, userspace wants to query the session length */
1482 	if (!params.session_len)
1483 		return __sev_send_start_query_session_length(kvm, argp,
1484 				&params);
1485 
1486 	/* some sanity checks */
1487 	if (!params.pdh_cert_uaddr || !params.pdh_cert_len ||
1488 	    !params.session_uaddr || params.session_len > SEV_FW_BLOB_MAX_SIZE)
1489 		return -EINVAL;
1490 
1491 	/* allocate the memory to hold the session data blob */
1492 	session_data = kzalloc(params.session_len, GFP_KERNEL_ACCOUNT);
1493 	if (!session_data)
1494 		return -ENOMEM;
1495 
1496 	/* copy the certificate blobs from userspace */
1497 	pdh_cert = psp_copy_user_blob(params.pdh_cert_uaddr,
1498 				params.pdh_cert_len);
1499 	if (IS_ERR(pdh_cert)) {
1500 		ret = PTR_ERR(pdh_cert);
1501 		goto e_free_session;
1502 	}
1503 
1504 	plat_certs = psp_copy_user_blob(params.plat_certs_uaddr,
1505 				params.plat_certs_len);
1506 	if (IS_ERR(plat_certs)) {
1507 		ret = PTR_ERR(plat_certs);
1508 		goto e_free_pdh;
1509 	}
1510 
1511 	amd_certs = psp_copy_user_blob(params.amd_certs_uaddr,
1512 				params.amd_certs_len);
1513 	if (IS_ERR(amd_certs)) {
1514 		ret = PTR_ERR(amd_certs);
1515 		goto e_free_plat_cert;
1516 	}
1517 
1518 	/* populate the FW SEND_START field with system physical address */
1519 	memset(&data, 0, sizeof(data));
1520 	data.pdh_cert_address = __psp_pa(pdh_cert);
1521 	data.pdh_cert_len = params.pdh_cert_len;
1522 	data.plat_certs_address = __psp_pa(plat_certs);
1523 	data.plat_certs_len = params.plat_certs_len;
1524 	data.amd_certs_address = __psp_pa(amd_certs);
1525 	data.amd_certs_len = params.amd_certs_len;
1526 	data.session_address = __psp_pa(session_data);
1527 	data.session_len = params.session_len;
1528 	data.handle = sev->handle;
1529 
1530 	ret = sev_issue_cmd(kvm, SEV_CMD_SEND_START, &data, &argp->error);
1531 
1532 	if (!ret && copy_to_user(u64_to_user_ptr(params.session_uaddr),
1533 			session_data, params.session_len)) {
1534 		ret = -EFAULT;
1535 		goto e_free_amd_cert;
1536 	}
1537 
1538 	params.policy = data.policy;
1539 	params.session_len = data.session_len;
1540 	if (copy_to_user(u64_to_user_ptr(argp->data), &params,
1541 				sizeof(struct kvm_sev_send_start)))
1542 		ret = -EFAULT;
1543 
1544 e_free_amd_cert:
1545 	kfree(amd_certs);
1546 e_free_plat_cert:
1547 	kfree(plat_certs);
1548 e_free_pdh:
1549 	kfree(pdh_cert);
1550 e_free_session:
1551 	kfree(session_data);
1552 	return ret;
1553 }
1554 
1555 /* Userspace wants to query either header or trans length. */
1556 static int
__sev_send_update_data_query_lengths(struct kvm * kvm,struct kvm_sev_cmd * argp,struct kvm_sev_send_update_data * params)1557 __sev_send_update_data_query_lengths(struct kvm *kvm, struct kvm_sev_cmd *argp,
1558 				     struct kvm_sev_send_update_data *params)
1559 {
1560 	struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
1561 	struct sev_data_send_update_data data;
1562 	int ret;
1563 
1564 	memset(&data, 0, sizeof(data));
1565 	data.handle = sev->handle;
1566 	ret = sev_issue_cmd(kvm, SEV_CMD_SEND_UPDATE_DATA, &data, &argp->error);
1567 
1568 	params->hdr_len = data.hdr_len;
1569 	params->trans_len = data.trans_len;
1570 
1571 	if (copy_to_user(u64_to_user_ptr(argp->data), params,
1572 			 sizeof(struct kvm_sev_send_update_data)))
1573 		ret = -EFAULT;
1574 
1575 	return ret;
1576 }
1577 
sev_send_update_data(struct kvm * kvm,struct kvm_sev_cmd * argp)1578 static int sev_send_update_data(struct kvm *kvm, struct kvm_sev_cmd *argp)
1579 {
1580 	struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
1581 	struct sev_data_send_update_data data;
1582 	struct kvm_sev_send_update_data params;
1583 	void *hdr, *trans_data;
1584 	struct page **guest_page;
1585 	unsigned long n;
1586 	int ret, offset;
1587 
1588 	if (!sev_guest(kvm))
1589 		return -ENOTTY;
1590 
1591 	if (copy_from_user(&params, u64_to_user_ptr(argp->data),
1592 			sizeof(struct kvm_sev_send_update_data)))
1593 		return -EFAULT;
1594 
1595 	/* userspace wants to query either header or trans length */
1596 	if (!params.trans_len || !params.hdr_len)
1597 		return __sev_send_update_data_query_lengths(kvm, argp, &params);
1598 
1599 	if (!params.trans_uaddr || !params.guest_uaddr ||
1600 	    !params.guest_len || !params.hdr_uaddr)
1601 		return -EINVAL;
1602 
1603 	/* Check if we are crossing the page boundary */
1604 	offset = params.guest_uaddr & (PAGE_SIZE - 1);
1605 	if (params.guest_len > PAGE_SIZE || (params.guest_len + offset) > PAGE_SIZE)
1606 		return -EINVAL;
1607 
1608 	/* Pin guest memory */
1609 	guest_page = sev_pin_memory(kvm, params.guest_uaddr & PAGE_MASK,
1610 				    PAGE_SIZE, &n, 0);
1611 	if (IS_ERR(guest_page))
1612 		return PTR_ERR(guest_page);
1613 
1614 	/* allocate memory for header and transport buffer */
1615 	ret = -ENOMEM;
1616 	hdr = kzalloc(params.hdr_len, GFP_KERNEL_ACCOUNT);
1617 	if (!hdr)
1618 		goto e_unpin;
1619 
1620 	trans_data = kzalloc(params.trans_len, GFP_KERNEL_ACCOUNT);
1621 	if (!trans_data)
1622 		goto e_free_hdr;
1623 
1624 	memset(&data, 0, sizeof(data));
1625 	data.hdr_address = __psp_pa(hdr);
1626 	data.hdr_len = params.hdr_len;
1627 	data.trans_address = __psp_pa(trans_data);
1628 	data.trans_len = params.trans_len;
1629 
1630 	/* The SEND_UPDATE_DATA command requires C-bit to be always set. */
1631 	data.guest_address = (page_to_pfn(guest_page[0]) << PAGE_SHIFT) + offset;
1632 	data.guest_address |= sev_me_mask;
1633 	data.guest_len = params.guest_len;
1634 	data.handle = sev->handle;
1635 
1636 	ret = sev_issue_cmd(kvm, SEV_CMD_SEND_UPDATE_DATA, &data, &argp->error);
1637 
1638 	if (ret)
1639 		goto e_free_trans_data;
1640 
1641 	/* copy transport buffer to user space */
1642 	if (copy_to_user(u64_to_user_ptr(params.trans_uaddr),
1643 			 trans_data, params.trans_len)) {
1644 		ret = -EFAULT;
1645 		goto e_free_trans_data;
1646 	}
1647 
1648 	/* Copy packet header to userspace. */
1649 	if (copy_to_user(u64_to_user_ptr(params.hdr_uaddr), hdr,
1650 			 params.hdr_len))
1651 		ret = -EFAULT;
1652 
1653 e_free_trans_data:
1654 	kfree(trans_data);
1655 e_free_hdr:
1656 	kfree(hdr);
1657 e_unpin:
1658 	sev_unpin_memory(kvm, guest_page, n);
1659 
1660 	return ret;
1661 }
1662 
sev_send_finish(struct kvm * kvm,struct kvm_sev_cmd * argp)1663 static int sev_send_finish(struct kvm *kvm, struct kvm_sev_cmd *argp)
1664 {
1665 	struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
1666 	struct sev_data_send_finish data;
1667 
1668 	if (!sev_guest(kvm))
1669 		return -ENOTTY;
1670 
1671 	data.handle = sev->handle;
1672 	return sev_issue_cmd(kvm, SEV_CMD_SEND_FINISH, &data, &argp->error);
1673 }
1674 
sev_send_cancel(struct kvm * kvm,struct kvm_sev_cmd * argp)1675 static int sev_send_cancel(struct kvm *kvm, struct kvm_sev_cmd *argp)
1676 {
1677 	struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
1678 	struct sev_data_send_cancel data;
1679 
1680 	if (!sev_guest(kvm))
1681 		return -ENOTTY;
1682 
1683 	data.handle = sev->handle;
1684 	return sev_issue_cmd(kvm, SEV_CMD_SEND_CANCEL, &data, &argp->error);
1685 }
1686 
sev_receive_start(struct kvm * kvm,struct kvm_sev_cmd * argp)1687 static int sev_receive_start(struct kvm *kvm, struct kvm_sev_cmd *argp)
1688 {
1689 	struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
1690 	struct sev_data_receive_start start;
1691 	struct kvm_sev_receive_start params;
1692 	int *error = &argp->error;
1693 	void *session_data;
1694 	void *pdh_data;
1695 	int ret;
1696 
1697 	if (!sev_guest(kvm))
1698 		return -ENOTTY;
1699 
1700 	/* Get parameter from the userspace */
1701 	if (copy_from_user(&params, u64_to_user_ptr(argp->data),
1702 			sizeof(struct kvm_sev_receive_start)))
1703 		return -EFAULT;
1704 
1705 	/* some sanity checks */
1706 	if (!params.pdh_uaddr || !params.pdh_len ||
1707 	    !params.session_uaddr || !params.session_len)
1708 		return -EINVAL;
1709 
1710 	pdh_data = psp_copy_user_blob(params.pdh_uaddr, params.pdh_len);
1711 	if (IS_ERR(pdh_data))
1712 		return PTR_ERR(pdh_data);
1713 
1714 	session_data = psp_copy_user_blob(params.session_uaddr,
1715 			params.session_len);
1716 	if (IS_ERR(session_data)) {
1717 		ret = PTR_ERR(session_data);
1718 		goto e_free_pdh;
1719 	}
1720 
1721 	memset(&start, 0, sizeof(start));
1722 	start.handle = params.handle;
1723 	start.policy = params.policy;
1724 	start.pdh_cert_address = __psp_pa(pdh_data);
1725 	start.pdh_cert_len = params.pdh_len;
1726 	start.session_address = __psp_pa(session_data);
1727 	start.session_len = params.session_len;
1728 
1729 	/* create memory encryption context */
1730 	ret = __sev_issue_cmd(argp->sev_fd, SEV_CMD_RECEIVE_START, &start,
1731 				error);
1732 	if (ret)
1733 		goto e_free_session;
1734 
1735 	/* Bind ASID to this guest */
1736 	ret = sev_bind_asid(kvm, start.handle, error);
1737 	if (ret) {
1738 		sev_decommission(start.handle);
1739 		goto e_free_session;
1740 	}
1741 
1742 	params.handle = start.handle;
1743 	if (copy_to_user(u64_to_user_ptr(argp->data),
1744 			 &params, sizeof(struct kvm_sev_receive_start))) {
1745 		ret = -EFAULT;
1746 		sev_unbind_asid(kvm, start.handle);
1747 		goto e_free_session;
1748 	}
1749 
1750     	sev->handle = start.handle;
1751 	sev->fd = argp->sev_fd;
1752 
1753 e_free_session:
1754 	kfree(session_data);
1755 e_free_pdh:
1756 	kfree(pdh_data);
1757 
1758 	return ret;
1759 }
1760 
sev_receive_update_data(struct kvm * kvm,struct kvm_sev_cmd * argp)1761 static int sev_receive_update_data(struct kvm *kvm, struct kvm_sev_cmd *argp)
1762 {
1763 	struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
1764 	struct kvm_sev_receive_update_data params;
1765 	struct sev_data_receive_update_data data;
1766 	void *hdr = NULL, *trans = NULL;
1767 	struct page **guest_page;
1768 	unsigned long n;
1769 	int ret, offset;
1770 
1771 	if (!sev_guest(kvm))
1772 		return -EINVAL;
1773 
1774 	if (copy_from_user(&params, u64_to_user_ptr(argp->data),
1775 			sizeof(struct kvm_sev_receive_update_data)))
1776 		return -EFAULT;
1777 
1778 	if (!params.hdr_uaddr || !params.hdr_len ||
1779 	    !params.guest_uaddr || !params.guest_len ||
1780 	    !params.trans_uaddr || !params.trans_len)
1781 		return -EINVAL;
1782 
1783 	/* Check if we are crossing the page boundary */
1784 	offset = params.guest_uaddr & (PAGE_SIZE - 1);
1785 	if (params.guest_len > PAGE_SIZE || (params.guest_len + offset) > PAGE_SIZE)
1786 		return -EINVAL;
1787 
1788 	hdr = psp_copy_user_blob(params.hdr_uaddr, params.hdr_len);
1789 	if (IS_ERR(hdr))
1790 		return PTR_ERR(hdr);
1791 
1792 	trans = psp_copy_user_blob(params.trans_uaddr, params.trans_len);
1793 	if (IS_ERR(trans)) {
1794 		ret = PTR_ERR(trans);
1795 		goto e_free_hdr;
1796 	}
1797 
1798 	memset(&data, 0, sizeof(data));
1799 	data.hdr_address = __psp_pa(hdr);
1800 	data.hdr_len = params.hdr_len;
1801 	data.trans_address = __psp_pa(trans);
1802 	data.trans_len = params.trans_len;
1803 
1804 	/* Pin guest memory */
1805 	guest_page = sev_pin_memory(kvm, params.guest_uaddr & PAGE_MASK,
1806 				    PAGE_SIZE, &n, 1);
1807 	if (IS_ERR(guest_page)) {
1808 		ret = PTR_ERR(guest_page);
1809 		goto e_free_trans;
1810 	}
1811 
1812 	/*
1813 	 * Flush (on non-coherent CPUs) before RECEIVE_UPDATE_DATA, the PSP
1814 	 * encrypts the written data with the guest's key, and the cache may
1815 	 * contain dirty, unencrypted data.
1816 	 */
1817 	sev_clflush_pages(guest_page, n);
1818 
1819 	/* The RECEIVE_UPDATE_DATA command requires C-bit to be always set. */
1820 	data.guest_address = (page_to_pfn(guest_page[0]) << PAGE_SHIFT) + offset;
1821 	data.guest_address |= sev_me_mask;
1822 	data.guest_len = params.guest_len;
1823 	data.handle = sev->handle;
1824 
1825 	ret = sev_issue_cmd(kvm, SEV_CMD_RECEIVE_UPDATE_DATA, &data,
1826 				&argp->error);
1827 
1828 	sev_unpin_memory(kvm, guest_page, n);
1829 
1830 e_free_trans:
1831 	kfree(trans);
1832 e_free_hdr:
1833 	kfree(hdr);
1834 
1835 	return ret;
1836 }
1837 
sev_receive_finish(struct kvm * kvm,struct kvm_sev_cmd * argp)1838 static int sev_receive_finish(struct kvm *kvm, struct kvm_sev_cmd *argp)
1839 {
1840 	struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
1841 	struct sev_data_receive_finish data;
1842 
1843 	if (!sev_guest(kvm))
1844 		return -ENOTTY;
1845 
1846 	data.handle = sev->handle;
1847 	return sev_issue_cmd(kvm, SEV_CMD_RECEIVE_FINISH, &data, &argp->error);
1848 }
1849 
is_cmd_allowed_from_mirror(u32 cmd_id)1850 static bool is_cmd_allowed_from_mirror(u32 cmd_id)
1851 {
1852 	/*
1853 	 * Allow mirrors VM to call KVM_SEV_LAUNCH_UPDATE_VMSA to enable SEV-ES
1854 	 * active mirror VMs. Also allow the debugging and status commands.
1855 	 */
1856 	if (cmd_id == KVM_SEV_LAUNCH_UPDATE_VMSA ||
1857 	    cmd_id == KVM_SEV_GUEST_STATUS || cmd_id == KVM_SEV_DBG_DECRYPT ||
1858 	    cmd_id == KVM_SEV_DBG_ENCRYPT)
1859 		return true;
1860 
1861 	return false;
1862 }
1863 
sev_lock_two_vms(struct kvm * dst_kvm,struct kvm * src_kvm)1864 static int sev_lock_two_vms(struct kvm *dst_kvm, struct kvm *src_kvm)
1865 {
1866 	struct kvm_sev_info *dst_sev = &to_kvm_svm(dst_kvm)->sev_info;
1867 	struct kvm_sev_info *src_sev = &to_kvm_svm(src_kvm)->sev_info;
1868 	int r = -EBUSY;
1869 
1870 	if (dst_kvm == src_kvm)
1871 		return -EINVAL;
1872 
1873 	/*
1874 	 * Bail if these VMs are already involved in a migration to avoid
1875 	 * deadlock between two VMs trying to migrate to/from each other.
1876 	 */
1877 	if (atomic_cmpxchg_acquire(&dst_sev->migration_in_progress, 0, 1))
1878 		return -EBUSY;
1879 
1880 	if (atomic_cmpxchg_acquire(&src_sev->migration_in_progress, 0, 1))
1881 		goto release_dst;
1882 
1883 	r = -EINTR;
1884 	if (mutex_lock_killable(&dst_kvm->lock))
1885 		goto release_src;
1886 	if (mutex_lock_killable_nested(&src_kvm->lock, SINGLE_DEPTH_NESTING))
1887 		goto unlock_dst;
1888 	return 0;
1889 
1890 unlock_dst:
1891 	mutex_unlock(&dst_kvm->lock);
1892 release_src:
1893 	atomic_set_release(&src_sev->migration_in_progress, 0);
1894 release_dst:
1895 	atomic_set_release(&dst_sev->migration_in_progress, 0);
1896 	return r;
1897 }
1898 
sev_unlock_two_vms(struct kvm * dst_kvm,struct kvm * src_kvm)1899 static void sev_unlock_two_vms(struct kvm *dst_kvm, struct kvm *src_kvm)
1900 {
1901 	struct kvm_sev_info *dst_sev = &to_kvm_svm(dst_kvm)->sev_info;
1902 	struct kvm_sev_info *src_sev = &to_kvm_svm(src_kvm)->sev_info;
1903 
1904 	mutex_unlock(&dst_kvm->lock);
1905 	mutex_unlock(&src_kvm->lock);
1906 	atomic_set_release(&dst_sev->migration_in_progress, 0);
1907 	atomic_set_release(&src_sev->migration_in_progress, 0);
1908 }
1909 
1910 /* vCPU mutex subclasses.  */
1911 enum sev_migration_role {
1912 	SEV_MIGRATION_SOURCE = 0,
1913 	SEV_MIGRATION_TARGET,
1914 	SEV_NR_MIGRATION_ROLES,
1915 };
1916 
sev_lock_vcpus_for_migration(struct kvm * kvm,enum sev_migration_role role)1917 static int sev_lock_vcpus_for_migration(struct kvm *kvm,
1918 					enum sev_migration_role role)
1919 {
1920 	struct kvm_vcpu *vcpu;
1921 	unsigned long i, j;
1922 
1923 	kvm_for_each_vcpu(i, vcpu, kvm) {
1924 		if (mutex_lock_killable_nested(&vcpu->mutex, role))
1925 			goto out_unlock;
1926 
1927 #ifdef CONFIG_PROVE_LOCKING
1928 		if (!i)
1929 			/*
1930 			 * Reset the role to one that avoids colliding with
1931 			 * the role used for the first vcpu mutex.
1932 			 */
1933 			role = SEV_NR_MIGRATION_ROLES;
1934 		else
1935 			mutex_release(&vcpu->mutex.dep_map, _THIS_IP_);
1936 #endif
1937 	}
1938 
1939 	return 0;
1940 
1941 out_unlock:
1942 
1943 	kvm_for_each_vcpu(j, vcpu, kvm) {
1944 		if (i == j)
1945 			break;
1946 
1947 #ifdef CONFIG_PROVE_LOCKING
1948 		if (j)
1949 			mutex_acquire(&vcpu->mutex.dep_map, role, 0, _THIS_IP_);
1950 #endif
1951 
1952 		mutex_unlock(&vcpu->mutex);
1953 	}
1954 	return -EINTR;
1955 }
1956 
sev_unlock_vcpus_for_migration(struct kvm * kvm)1957 static void sev_unlock_vcpus_for_migration(struct kvm *kvm)
1958 {
1959 	struct kvm_vcpu *vcpu;
1960 	unsigned long i;
1961 	bool first = true;
1962 
1963 	kvm_for_each_vcpu(i, vcpu, kvm) {
1964 		if (first)
1965 			first = false;
1966 		else
1967 			mutex_acquire(&vcpu->mutex.dep_map,
1968 				      SEV_NR_MIGRATION_ROLES, 0, _THIS_IP_);
1969 
1970 		mutex_unlock(&vcpu->mutex);
1971 	}
1972 }
1973 
sev_migrate_from(struct kvm * dst_kvm,struct kvm * src_kvm)1974 static void sev_migrate_from(struct kvm *dst_kvm, struct kvm *src_kvm)
1975 {
1976 	struct kvm_sev_info *dst = &to_kvm_svm(dst_kvm)->sev_info;
1977 	struct kvm_sev_info *src = &to_kvm_svm(src_kvm)->sev_info;
1978 	struct kvm_vcpu *dst_vcpu, *src_vcpu;
1979 	struct vcpu_svm *dst_svm, *src_svm;
1980 	struct kvm_sev_info *mirror;
1981 	unsigned long i;
1982 
1983 	dst->active = true;
1984 	dst->asid = src->asid;
1985 	dst->handle = src->handle;
1986 	dst->pages_locked = src->pages_locked;
1987 	dst->enc_context_owner = src->enc_context_owner;
1988 	dst->es_active = src->es_active;
1989 	dst->vmsa_features = src->vmsa_features;
1990 
1991 	src->asid = 0;
1992 	src->active = false;
1993 	src->handle = 0;
1994 	src->pages_locked = 0;
1995 	src->enc_context_owner = NULL;
1996 	src->es_active = false;
1997 
1998 	list_cut_before(&dst->regions_list, &src->regions_list, &src->regions_list);
1999 
2000 	/*
2001 	 * If this VM has mirrors, "transfer" each mirror's refcount of the
2002 	 * source to the destination (this KVM).  The caller holds a reference
2003 	 * to the source, so there's no danger of use-after-free.
2004 	 */
2005 	list_cut_before(&dst->mirror_vms, &src->mirror_vms, &src->mirror_vms);
2006 	list_for_each_entry(mirror, &dst->mirror_vms, mirror_entry) {
2007 		kvm_get_kvm(dst_kvm);
2008 		kvm_put_kvm(src_kvm);
2009 		mirror->enc_context_owner = dst_kvm;
2010 	}
2011 
2012 	/*
2013 	 * If this VM is a mirror, remove the old mirror from the owners list
2014 	 * and add the new mirror to the list.
2015 	 */
2016 	if (is_mirroring_enc_context(dst_kvm)) {
2017 		struct kvm_sev_info *owner_sev_info =
2018 			&to_kvm_svm(dst->enc_context_owner)->sev_info;
2019 
2020 		list_del(&src->mirror_entry);
2021 		list_add_tail(&dst->mirror_entry, &owner_sev_info->mirror_vms);
2022 	}
2023 
2024 	kvm_for_each_vcpu(i, dst_vcpu, dst_kvm) {
2025 		dst_svm = to_svm(dst_vcpu);
2026 
2027 		sev_init_vmcb(dst_svm);
2028 
2029 		if (!dst->es_active)
2030 			continue;
2031 
2032 		/*
2033 		 * Note, the source is not required to have the same number of
2034 		 * vCPUs as the destination when migrating a vanilla SEV VM.
2035 		 */
2036 		src_vcpu = kvm_get_vcpu(src_kvm, i);
2037 		src_svm = to_svm(src_vcpu);
2038 
2039 		/*
2040 		 * Transfer VMSA and GHCB state to the destination.  Nullify and
2041 		 * clear source fields as appropriate, the state now belongs to
2042 		 * the destination.
2043 		 */
2044 		memcpy(&dst_svm->sev_es, &src_svm->sev_es, sizeof(src_svm->sev_es));
2045 		dst_svm->vmcb->control.ghcb_gpa = src_svm->vmcb->control.ghcb_gpa;
2046 		dst_svm->vmcb->control.vmsa_pa = src_svm->vmcb->control.vmsa_pa;
2047 		dst_vcpu->arch.guest_state_protected = true;
2048 
2049 		memset(&src_svm->sev_es, 0, sizeof(src_svm->sev_es));
2050 		src_svm->vmcb->control.ghcb_gpa = INVALID_PAGE;
2051 		src_svm->vmcb->control.vmsa_pa = INVALID_PAGE;
2052 		src_vcpu->arch.guest_state_protected = false;
2053 	}
2054 }
2055 
sev_check_source_vcpus(struct kvm * dst,struct kvm * src)2056 static int sev_check_source_vcpus(struct kvm *dst, struct kvm *src)
2057 {
2058 	struct kvm_vcpu *src_vcpu;
2059 	unsigned long i;
2060 
2061 	if (src->created_vcpus != atomic_read(&src->online_vcpus) ||
2062 	    dst->created_vcpus != atomic_read(&dst->online_vcpus))
2063 		return -EBUSY;
2064 
2065 	if (!sev_es_guest(src))
2066 		return 0;
2067 
2068 	if (atomic_read(&src->online_vcpus) != atomic_read(&dst->online_vcpus))
2069 		return -EINVAL;
2070 
2071 	kvm_for_each_vcpu(i, src_vcpu, src) {
2072 		if (!src_vcpu->arch.guest_state_protected)
2073 			return -EINVAL;
2074 	}
2075 
2076 	return 0;
2077 }
2078 
sev_vm_move_enc_context_from(struct kvm * kvm,unsigned int source_fd)2079 int sev_vm_move_enc_context_from(struct kvm *kvm, unsigned int source_fd)
2080 {
2081 	struct kvm_sev_info *dst_sev = &to_kvm_svm(kvm)->sev_info;
2082 	struct kvm_sev_info *src_sev, *cg_cleanup_sev;
2083 	struct fd f = fdget(source_fd);
2084 	struct kvm *source_kvm;
2085 	bool charged = false;
2086 	int ret;
2087 
2088 	if (!fd_file(f))
2089 		return -EBADF;
2090 
2091 	if (!file_is_kvm(fd_file(f))) {
2092 		ret = -EBADF;
2093 		goto out_fput;
2094 	}
2095 
2096 	source_kvm = fd_file(f)->private_data;
2097 	ret = sev_lock_two_vms(kvm, source_kvm);
2098 	if (ret)
2099 		goto out_fput;
2100 
2101 	if (kvm->arch.vm_type != source_kvm->arch.vm_type ||
2102 	    sev_guest(kvm) || !sev_guest(source_kvm)) {
2103 		ret = -EINVAL;
2104 		goto out_unlock;
2105 	}
2106 
2107 	src_sev = &to_kvm_svm(source_kvm)->sev_info;
2108 
2109 	dst_sev->misc_cg = get_current_misc_cg();
2110 	cg_cleanup_sev = dst_sev;
2111 	if (dst_sev->misc_cg != src_sev->misc_cg) {
2112 		ret = sev_misc_cg_try_charge(dst_sev);
2113 		if (ret)
2114 			goto out_dst_cgroup;
2115 		charged = true;
2116 	}
2117 
2118 	ret = sev_lock_vcpus_for_migration(kvm, SEV_MIGRATION_SOURCE);
2119 	if (ret)
2120 		goto out_dst_cgroup;
2121 	ret = sev_lock_vcpus_for_migration(source_kvm, SEV_MIGRATION_TARGET);
2122 	if (ret)
2123 		goto out_dst_vcpu;
2124 
2125 	ret = sev_check_source_vcpus(kvm, source_kvm);
2126 	if (ret)
2127 		goto out_source_vcpu;
2128 
2129 	sev_migrate_from(kvm, source_kvm);
2130 	kvm_vm_dead(source_kvm);
2131 	cg_cleanup_sev = src_sev;
2132 	ret = 0;
2133 
2134 out_source_vcpu:
2135 	sev_unlock_vcpus_for_migration(source_kvm);
2136 out_dst_vcpu:
2137 	sev_unlock_vcpus_for_migration(kvm);
2138 out_dst_cgroup:
2139 	/* Operates on the source on success, on the destination on failure.  */
2140 	if (charged)
2141 		sev_misc_cg_uncharge(cg_cleanup_sev);
2142 	put_misc_cg(cg_cleanup_sev->misc_cg);
2143 	cg_cleanup_sev->misc_cg = NULL;
2144 out_unlock:
2145 	sev_unlock_two_vms(kvm, source_kvm);
2146 out_fput:
2147 	fdput(f);
2148 	return ret;
2149 }
2150 
sev_dev_get_attr(u32 group,u64 attr,u64 * val)2151 int sev_dev_get_attr(u32 group, u64 attr, u64 *val)
2152 {
2153 	if (group != KVM_X86_GRP_SEV)
2154 		return -ENXIO;
2155 
2156 	switch (attr) {
2157 	case KVM_X86_SEV_VMSA_FEATURES:
2158 		*val = sev_supported_vmsa_features;
2159 		return 0;
2160 
2161 	default:
2162 		return -ENXIO;
2163 	}
2164 }
2165 
2166 /*
2167  * The guest context contains all the information, keys and metadata
2168  * associated with the guest that the firmware tracks to implement SEV
2169  * and SNP features. The firmware stores the guest context in hypervisor
2170  * provide page via the SNP_GCTX_CREATE command.
2171  */
snp_context_create(struct kvm * kvm,struct kvm_sev_cmd * argp)2172 static void *snp_context_create(struct kvm *kvm, struct kvm_sev_cmd *argp)
2173 {
2174 	struct sev_data_snp_addr data = {};
2175 	void *context;
2176 	int rc;
2177 
2178 	/* Allocate memory for context page */
2179 	context = snp_alloc_firmware_page(GFP_KERNEL_ACCOUNT);
2180 	if (!context)
2181 		return NULL;
2182 
2183 	data.address = __psp_pa(context);
2184 	rc = __sev_issue_cmd(argp->sev_fd, SEV_CMD_SNP_GCTX_CREATE, &data, &argp->error);
2185 	if (rc) {
2186 		pr_warn("Failed to create SEV-SNP context, rc %d fw_error %d",
2187 			rc, argp->error);
2188 		snp_free_firmware_page(context);
2189 		return NULL;
2190 	}
2191 
2192 	return context;
2193 }
2194 
snp_bind_asid(struct kvm * kvm,int * error)2195 static int snp_bind_asid(struct kvm *kvm, int *error)
2196 {
2197 	struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
2198 	struct sev_data_snp_activate data = {0};
2199 
2200 	data.gctx_paddr = __psp_pa(sev->snp_context);
2201 	data.asid = sev_get_asid(kvm);
2202 	return sev_issue_cmd(kvm, SEV_CMD_SNP_ACTIVATE, &data, error);
2203 }
2204 
snp_launch_start(struct kvm * kvm,struct kvm_sev_cmd * argp)2205 static int snp_launch_start(struct kvm *kvm, struct kvm_sev_cmd *argp)
2206 {
2207 	struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
2208 	struct sev_data_snp_launch_start start = {0};
2209 	struct kvm_sev_snp_launch_start params;
2210 	int rc;
2211 
2212 	if (!sev_snp_guest(kvm))
2213 		return -ENOTTY;
2214 
2215 	if (copy_from_user(&params, u64_to_user_ptr(argp->data), sizeof(params)))
2216 		return -EFAULT;
2217 
2218 	/* Don't allow userspace to allocate memory for more than 1 SNP context. */
2219 	if (sev->snp_context)
2220 		return -EINVAL;
2221 
2222 	if (params.flags)
2223 		return -EINVAL;
2224 
2225 	if (params.policy & ~SNP_POLICY_MASK_VALID)
2226 		return -EINVAL;
2227 
2228 	/* Check for policy bits that must be set */
2229 	if (!(params.policy & SNP_POLICY_MASK_RSVD_MBO) ||
2230 	    !(params.policy & SNP_POLICY_MASK_SMT))
2231 		return -EINVAL;
2232 
2233 	if (params.policy & SNP_POLICY_MASK_SINGLE_SOCKET)
2234 		return -EINVAL;
2235 
2236 	sev->snp_context = snp_context_create(kvm, argp);
2237 	if (!sev->snp_context)
2238 		return -ENOTTY;
2239 
2240 	start.gctx_paddr = __psp_pa(sev->snp_context);
2241 	start.policy = params.policy;
2242 	memcpy(start.gosvw, params.gosvw, sizeof(params.gosvw));
2243 	rc = __sev_issue_cmd(argp->sev_fd, SEV_CMD_SNP_LAUNCH_START, &start, &argp->error);
2244 	if (rc) {
2245 		pr_debug("%s: SEV_CMD_SNP_LAUNCH_START firmware command failed, rc %d\n",
2246 			 __func__, rc);
2247 		goto e_free_context;
2248 	}
2249 
2250 	sev->fd = argp->sev_fd;
2251 	rc = snp_bind_asid(kvm, &argp->error);
2252 	if (rc) {
2253 		pr_debug("%s: Failed to bind ASID to SEV-SNP context, rc %d\n",
2254 			 __func__, rc);
2255 		goto e_free_context;
2256 	}
2257 
2258 	return 0;
2259 
2260 e_free_context:
2261 	snp_decommission_context(kvm);
2262 
2263 	return rc;
2264 }
2265 
2266 struct sev_gmem_populate_args {
2267 	__u8 type;
2268 	int sev_fd;
2269 	int fw_error;
2270 };
2271 
sev_gmem_post_populate(struct kvm * kvm,gfn_t gfn_start,kvm_pfn_t pfn,void __user * src,int order,void * opaque)2272 static int sev_gmem_post_populate(struct kvm *kvm, gfn_t gfn_start, kvm_pfn_t pfn,
2273 				  void __user *src, int order, void *opaque)
2274 {
2275 	struct sev_gmem_populate_args *sev_populate_args = opaque;
2276 	struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
2277 	int n_private = 0, ret, i;
2278 	int npages = (1 << order);
2279 	gfn_t gfn;
2280 
2281 	if (WARN_ON_ONCE(sev_populate_args->type != KVM_SEV_SNP_PAGE_TYPE_ZERO && !src))
2282 		return -EINVAL;
2283 
2284 	for (gfn = gfn_start, i = 0; gfn < gfn_start + npages; gfn++, i++) {
2285 		struct sev_data_snp_launch_update fw_args = {0};
2286 		bool assigned = false;
2287 		int level;
2288 
2289 		ret = snp_lookup_rmpentry((u64)pfn + i, &assigned, &level);
2290 		if (ret || assigned) {
2291 			pr_debug("%s: Failed to ensure GFN 0x%llx RMP entry is initial shared state, ret: %d assigned: %d\n",
2292 				 __func__, gfn, ret, assigned);
2293 			ret = ret ? -EINVAL : -EEXIST;
2294 			goto err;
2295 		}
2296 
2297 		if (src) {
2298 			void *vaddr = kmap_local_pfn(pfn + i);
2299 
2300 			if (copy_from_user(vaddr, src + i * PAGE_SIZE, PAGE_SIZE)) {
2301 				ret = -EFAULT;
2302 				goto err;
2303 			}
2304 			kunmap_local(vaddr);
2305 		}
2306 
2307 		ret = rmp_make_private(pfn + i, gfn << PAGE_SHIFT, PG_LEVEL_4K,
2308 				       sev_get_asid(kvm), true);
2309 		if (ret)
2310 			goto err;
2311 
2312 		n_private++;
2313 
2314 		fw_args.gctx_paddr = __psp_pa(sev->snp_context);
2315 		fw_args.address = __sme_set(pfn_to_hpa(pfn + i));
2316 		fw_args.page_size = PG_LEVEL_TO_RMP(PG_LEVEL_4K);
2317 		fw_args.page_type = sev_populate_args->type;
2318 
2319 		ret = __sev_issue_cmd(sev_populate_args->sev_fd, SEV_CMD_SNP_LAUNCH_UPDATE,
2320 				      &fw_args, &sev_populate_args->fw_error);
2321 		if (ret)
2322 			goto fw_err;
2323 	}
2324 
2325 	return 0;
2326 
2327 fw_err:
2328 	/*
2329 	 * If the firmware command failed handle the reclaim and cleanup of that
2330 	 * PFN specially vs. prior pages which can be cleaned up below without
2331 	 * needing to reclaim in advance.
2332 	 *
2333 	 * Additionally, when invalid CPUID function entries are detected,
2334 	 * firmware writes the expected values into the page and leaves it
2335 	 * unencrypted so it can be used for debugging and error-reporting.
2336 	 *
2337 	 * Copy this page back into the source buffer so userspace can use this
2338 	 * information to provide information on which CPUID leaves/fields
2339 	 * failed CPUID validation.
2340 	 */
2341 	if (!snp_page_reclaim(kvm, pfn + i) &&
2342 	    sev_populate_args->type == KVM_SEV_SNP_PAGE_TYPE_CPUID &&
2343 	    sev_populate_args->fw_error == SEV_RET_INVALID_PARAM) {
2344 		void *vaddr = kmap_local_pfn(pfn + i);
2345 
2346 		if (copy_to_user(src + i * PAGE_SIZE, vaddr, PAGE_SIZE))
2347 			pr_debug("Failed to write CPUID page back to userspace\n");
2348 
2349 		kunmap_local(vaddr);
2350 	}
2351 
2352 	/* pfn + i is hypervisor-owned now, so skip below cleanup for it. */
2353 	n_private--;
2354 
2355 err:
2356 	pr_debug("%s: exiting with error ret %d (fw_error %d), restoring %d gmem PFNs to shared.\n",
2357 		 __func__, ret, sev_populate_args->fw_error, n_private);
2358 	for (i = 0; i < n_private; i++)
2359 		kvm_rmp_make_shared(kvm, pfn + i, PG_LEVEL_4K);
2360 
2361 	return ret;
2362 }
2363 
snp_launch_update(struct kvm * kvm,struct kvm_sev_cmd * argp)2364 static int snp_launch_update(struct kvm *kvm, struct kvm_sev_cmd *argp)
2365 {
2366 	struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
2367 	struct sev_gmem_populate_args sev_populate_args = {0};
2368 	struct kvm_sev_snp_launch_update params;
2369 	struct kvm_memory_slot *memslot;
2370 	long npages, count;
2371 	void __user *src;
2372 	int ret = 0;
2373 
2374 	if (!sev_snp_guest(kvm) || !sev->snp_context)
2375 		return -EINVAL;
2376 
2377 	if (copy_from_user(&params, u64_to_user_ptr(argp->data), sizeof(params)))
2378 		return -EFAULT;
2379 
2380 	pr_debug("%s: GFN start 0x%llx length 0x%llx type %d flags %d\n", __func__,
2381 		 params.gfn_start, params.len, params.type, params.flags);
2382 
2383 	if (!PAGE_ALIGNED(params.len) || params.flags ||
2384 	    (params.type != KVM_SEV_SNP_PAGE_TYPE_NORMAL &&
2385 	     params.type != KVM_SEV_SNP_PAGE_TYPE_ZERO &&
2386 	     params.type != KVM_SEV_SNP_PAGE_TYPE_UNMEASURED &&
2387 	     params.type != KVM_SEV_SNP_PAGE_TYPE_SECRETS &&
2388 	     params.type != KVM_SEV_SNP_PAGE_TYPE_CPUID))
2389 		return -EINVAL;
2390 
2391 	npages = params.len / PAGE_SIZE;
2392 
2393 	/*
2394 	 * For each GFN that's being prepared as part of the initial guest
2395 	 * state, the following pre-conditions are verified:
2396 	 *
2397 	 *   1) The backing memslot is a valid private memslot.
2398 	 *   2) The GFN has been set to private via KVM_SET_MEMORY_ATTRIBUTES
2399 	 *      beforehand.
2400 	 *   3) The PFN of the guest_memfd has not already been set to private
2401 	 *      in the RMP table.
2402 	 *
2403 	 * The KVM MMU relies on kvm->mmu_invalidate_seq to retry nested page
2404 	 * faults if there's a race between a fault and an attribute update via
2405 	 * KVM_SET_MEMORY_ATTRIBUTES, and a similar approach could be utilized
2406 	 * here. However, kvm->slots_lock guards against both this as well as
2407 	 * concurrent memslot updates occurring while these checks are being
2408 	 * performed, so use that here to make it easier to reason about the
2409 	 * initial expected state and better guard against unexpected
2410 	 * situations.
2411 	 */
2412 	mutex_lock(&kvm->slots_lock);
2413 
2414 	memslot = gfn_to_memslot(kvm, params.gfn_start);
2415 	if (!kvm_slot_can_be_private(memslot)) {
2416 		ret = -EINVAL;
2417 		goto out;
2418 	}
2419 
2420 	sev_populate_args.sev_fd = argp->sev_fd;
2421 	sev_populate_args.type = params.type;
2422 	src = params.type == KVM_SEV_SNP_PAGE_TYPE_ZERO ? NULL : u64_to_user_ptr(params.uaddr);
2423 
2424 	count = kvm_gmem_populate(kvm, params.gfn_start, src, npages,
2425 				  sev_gmem_post_populate, &sev_populate_args);
2426 	if (count < 0) {
2427 		argp->error = sev_populate_args.fw_error;
2428 		pr_debug("%s: kvm_gmem_populate failed, ret %ld (fw_error %d)\n",
2429 			 __func__, count, argp->error);
2430 		ret = -EIO;
2431 	} else {
2432 		params.gfn_start += count;
2433 		params.len -= count * PAGE_SIZE;
2434 		if (params.type != KVM_SEV_SNP_PAGE_TYPE_ZERO)
2435 			params.uaddr += count * PAGE_SIZE;
2436 
2437 		ret = 0;
2438 		if (copy_to_user(u64_to_user_ptr(argp->data), &params, sizeof(params)))
2439 			ret = -EFAULT;
2440 	}
2441 
2442 out:
2443 	mutex_unlock(&kvm->slots_lock);
2444 
2445 	return ret;
2446 }
2447 
snp_launch_update_vmsa(struct kvm * kvm,struct kvm_sev_cmd * argp)2448 static int snp_launch_update_vmsa(struct kvm *kvm, struct kvm_sev_cmd *argp)
2449 {
2450 	struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
2451 	struct sev_data_snp_launch_update data = {};
2452 	struct kvm_vcpu *vcpu;
2453 	unsigned long i;
2454 	int ret;
2455 
2456 	data.gctx_paddr = __psp_pa(sev->snp_context);
2457 	data.page_type = SNP_PAGE_TYPE_VMSA;
2458 
2459 	kvm_for_each_vcpu(i, vcpu, kvm) {
2460 		struct vcpu_svm *svm = to_svm(vcpu);
2461 		u64 pfn = __pa(svm->sev_es.vmsa) >> PAGE_SHIFT;
2462 
2463 		ret = sev_es_sync_vmsa(svm);
2464 		if (ret)
2465 			return ret;
2466 
2467 		/* Transition the VMSA page to a firmware state. */
2468 		ret = rmp_make_private(pfn, INITIAL_VMSA_GPA, PG_LEVEL_4K, sev->asid, true);
2469 		if (ret)
2470 			return ret;
2471 
2472 		/* Issue the SNP command to encrypt the VMSA */
2473 		data.address = __sme_pa(svm->sev_es.vmsa);
2474 		ret = __sev_issue_cmd(argp->sev_fd, SEV_CMD_SNP_LAUNCH_UPDATE,
2475 				      &data, &argp->error);
2476 		if (ret) {
2477 			snp_page_reclaim(kvm, pfn);
2478 
2479 			return ret;
2480 		}
2481 
2482 		svm->vcpu.arch.guest_state_protected = true;
2483 		/*
2484 		 * SEV-ES (and thus SNP) guest mandates LBR Virtualization to
2485 		 * be _always_ ON. Enable it only after setting
2486 		 * guest_state_protected because KVM_SET_MSRS allows dynamic
2487 		 * toggling of LBRV (for performance reason) on write access to
2488 		 * MSR_IA32_DEBUGCTLMSR when guest_state_protected is not set.
2489 		 */
2490 		svm_enable_lbrv(vcpu);
2491 	}
2492 
2493 	return 0;
2494 }
2495 
snp_launch_finish(struct kvm * kvm,struct kvm_sev_cmd * argp)2496 static int snp_launch_finish(struct kvm *kvm, struct kvm_sev_cmd *argp)
2497 {
2498 	struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
2499 	struct kvm_sev_snp_launch_finish params;
2500 	struct sev_data_snp_launch_finish *data;
2501 	void *id_block = NULL, *id_auth = NULL;
2502 	int ret;
2503 
2504 	if (!sev_snp_guest(kvm))
2505 		return -ENOTTY;
2506 
2507 	if (!sev->snp_context)
2508 		return -EINVAL;
2509 
2510 	if (copy_from_user(&params, u64_to_user_ptr(argp->data), sizeof(params)))
2511 		return -EFAULT;
2512 
2513 	if (params.flags)
2514 		return -EINVAL;
2515 
2516 	/* Measure all vCPUs using LAUNCH_UPDATE before finalizing the launch flow. */
2517 	ret = snp_launch_update_vmsa(kvm, argp);
2518 	if (ret)
2519 		return ret;
2520 
2521 	data = kzalloc(sizeof(*data), GFP_KERNEL_ACCOUNT);
2522 	if (!data)
2523 		return -ENOMEM;
2524 
2525 	if (params.id_block_en) {
2526 		id_block = psp_copy_user_blob(params.id_block_uaddr, KVM_SEV_SNP_ID_BLOCK_SIZE);
2527 		if (IS_ERR(id_block)) {
2528 			ret = PTR_ERR(id_block);
2529 			goto e_free;
2530 		}
2531 
2532 		data->id_block_en = 1;
2533 		data->id_block_paddr = __sme_pa(id_block);
2534 
2535 		id_auth = psp_copy_user_blob(params.id_auth_uaddr, KVM_SEV_SNP_ID_AUTH_SIZE);
2536 		if (IS_ERR(id_auth)) {
2537 			ret = PTR_ERR(id_auth);
2538 			goto e_free_id_block;
2539 		}
2540 
2541 		data->id_auth_paddr = __sme_pa(id_auth);
2542 
2543 		if (params.auth_key_en)
2544 			data->auth_key_en = 1;
2545 	}
2546 
2547 	data->vcek_disabled = params.vcek_disabled;
2548 
2549 	memcpy(data->host_data, params.host_data, KVM_SEV_SNP_FINISH_DATA_SIZE);
2550 	data->gctx_paddr = __psp_pa(sev->snp_context);
2551 	ret = sev_issue_cmd(kvm, SEV_CMD_SNP_LAUNCH_FINISH, data, &argp->error);
2552 
2553 	/*
2554 	 * Now that there will be no more SNP_LAUNCH_UPDATE ioctls, private pages
2555 	 * can be given to the guest simply by marking the RMP entry as private.
2556 	 * This can happen on first access and also with KVM_PRE_FAULT_MEMORY.
2557 	 */
2558 	if (!ret)
2559 		kvm->arch.pre_fault_allowed = true;
2560 
2561 	kfree(id_auth);
2562 
2563 e_free_id_block:
2564 	kfree(id_block);
2565 
2566 e_free:
2567 	kfree(data);
2568 
2569 	return ret;
2570 }
2571 
sev_mem_enc_ioctl(struct kvm * kvm,void __user * argp)2572 int sev_mem_enc_ioctl(struct kvm *kvm, void __user *argp)
2573 {
2574 	struct kvm_sev_cmd sev_cmd;
2575 	int r;
2576 
2577 	if (!sev_enabled)
2578 		return -ENOTTY;
2579 
2580 	if (!argp)
2581 		return 0;
2582 
2583 	if (copy_from_user(&sev_cmd, argp, sizeof(struct kvm_sev_cmd)))
2584 		return -EFAULT;
2585 
2586 	mutex_lock(&kvm->lock);
2587 
2588 	/* Only the enc_context_owner handles some memory enc operations. */
2589 	if (is_mirroring_enc_context(kvm) &&
2590 	    !is_cmd_allowed_from_mirror(sev_cmd.id)) {
2591 		r = -EINVAL;
2592 		goto out;
2593 	}
2594 
2595 	/*
2596 	 * Once KVM_SEV_INIT2 initializes a KVM instance as an SNP guest, only
2597 	 * allow the use of SNP-specific commands.
2598 	 */
2599 	if (sev_snp_guest(kvm) && sev_cmd.id < KVM_SEV_SNP_LAUNCH_START) {
2600 		r = -EPERM;
2601 		goto out;
2602 	}
2603 
2604 	switch (sev_cmd.id) {
2605 	case KVM_SEV_ES_INIT:
2606 		if (!sev_es_enabled) {
2607 			r = -ENOTTY;
2608 			goto out;
2609 		}
2610 		fallthrough;
2611 	case KVM_SEV_INIT:
2612 		r = sev_guest_init(kvm, &sev_cmd);
2613 		break;
2614 	case KVM_SEV_INIT2:
2615 		r = sev_guest_init2(kvm, &sev_cmd);
2616 		break;
2617 	case KVM_SEV_LAUNCH_START:
2618 		r = sev_launch_start(kvm, &sev_cmd);
2619 		break;
2620 	case KVM_SEV_LAUNCH_UPDATE_DATA:
2621 		r = sev_launch_update_data(kvm, &sev_cmd);
2622 		break;
2623 	case KVM_SEV_LAUNCH_UPDATE_VMSA:
2624 		r = sev_launch_update_vmsa(kvm, &sev_cmd);
2625 		break;
2626 	case KVM_SEV_LAUNCH_MEASURE:
2627 		r = sev_launch_measure(kvm, &sev_cmd);
2628 		break;
2629 	case KVM_SEV_LAUNCH_FINISH:
2630 		r = sev_launch_finish(kvm, &sev_cmd);
2631 		break;
2632 	case KVM_SEV_GUEST_STATUS:
2633 		r = sev_guest_status(kvm, &sev_cmd);
2634 		break;
2635 	case KVM_SEV_DBG_DECRYPT:
2636 		r = sev_dbg_crypt(kvm, &sev_cmd, true);
2637 		break;
2638 	case KVM_SEV_DBG_ENCRYPT:
2639 		r = sev_dbg_crypt(kvm, &sev_cmd, false);
2640 		break;
2641 	case KVM_SEV_LAUNCH_SECRET:
2642 		r = sev_launch_secret(kvm, &sev_cmd);
2643 		break;
2644 	case KVM_SEV_GET_ATTESTATION_REPORT:
2645 		r = sev_get_attestation_report(kvm, &sev_cmd);
2646 		break;
2647 	case KVM_SEV_SEND_START:
2648 		r = sev_send_start(kvm, &sev_cmd);
2649 		break;
2650 	case KVM_SEV_SEND_UPDATE_DATA:
2651 		r = sev_send_update_data(kvm, &sev_cmd);
2652 		break;
2653 	case KVM_SEV_SEND_FINISH:
2654 		r = sev_send_finish(kvm, &sev_cmd);
2655 		break;
2656 	case KVM_SEV_SEND_CANCEL:
2657 		r = sev_send_cancel(kvm, &sev_cmd);
2658 		break;
2659 	case KVM_SEV_RECEIVE_START:
2660 		r = sev_receive_start(kvm, &sev_cmd);
2661 		break;
2662 	case KVM_SEV_RECEIVE_UPDATE_DATA:
2663 		r = sev_receive_update_data(kvm, &sev_cmd);
2664 		break;
2665 	case KVM_SEV_RECEIVE_FINISH:
2666 		r = sev_receive_finish(kvm, &sev_cmd);
2667 		break;
2668 	case KVM_SEV_SNP_LAUNCH_START:
2669 		r = snp_launch_start(kvm, &sev_cmd);
2670 		break;
2671 	case KVM_SEV_SNP_LAUNCH_UPDATE:
2672 		r = snp_launch_update(kvm, &sev_cmd);
2673 		break;
2674 	case KVM_SEV_SNP_LAUNCH_FINISH:
2675 		r = snp_launch_finish(kvm, &sev_cmd);
2676 		break;
2677 	default:
2678 		r = -EINVAL;
2679 		goto out;
2680 	}
2681 
2682 	if (copy_to_user(argp, &sev_cmd, sizeof(struct kvm_sev_cmd)))
2683 		r = -EFAULT;
2684 
2685 out:
2686 	mutex_unlock(&kvm->lock);
2687 	return r;
2688 }
2689 
sev_mem_enc_register_region(struct kvm * kvm,struct kvm_enc_region * range)2690 int sev_mem_enc_register_region(struct kvm *kvm,
2691 				struct kvm_enc_region *range)
2692 {
2693 	struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
2694 	struct enc_region *region;
2695 	int ret = 0;
2696 
2697 	if (!sev_guest(kvm))
2698 		return -ENOTTY;
2699 
2700 	/* If kvm is mirroring encryption context it isn't responsible for it */
2701 	if (is_mirroring_enc_context(kvm))
2702 		return -EINVAL;
2703 
2704 	if (range->addr > ULONG_MAX || range->size > ULONG_MAX)
2705 		return -EINVAL;
2706 
2707 	region = kzalloc(sizeof(*region), GFP_KERNEL_ACCOUNT);
2708 	if (!region)
2709 		return -ENOMEM;
2710 
2711 	mutex_lock(&kvm->lock);
2712 	region->pages = sev_pin_memory(kvm, range->addr, range->size, &region->npages, 1);
2713 	if (IS_ERR(region->pages)) {
2714 		ret = PTR_ERR(region->pages);
2715 		mutex_unlock(&kvm->lock);
2716 		goto e_free;
2717 	}
2718 
2719 	/*
2720 	 * The guest may change the memory encryption attribute from C=0 -> C=1
2721 	 * or vice versa for this memory range. Lets make sure caches are
2722 	 * flushed to ensure that guest data gets written into memory with
2723 	 * correct C-bit.  Note, this must be done before dropping kvm->lock,
2724 	 * as region and its array of pages can be freed by a different task
2725 	 * once kvm->lock is released.
2726 	 */
2727 	sev_clflush_pages(region->pages, region->npages);
2728 
2729 	region->uaddr = range->addr;
2730 	region->size = range->size;
2731 
2732 	list_add_tail(&region->list, &sev->regions_list);
2733 	mutex_unlock(&kvm->lock);
2734 
2735 	return ret;
2736 
2737 e_free:
2738 	kfree(region);
2739 	return ret;
2740 }
2741 
2742 static struct enc_region *
find_enc_region(struct kvm * kvm,struct kvm_enc_region * range)2743 find_enc_region(struct kvm *kvm, struct kvm_enc_region *range)
2744 {
2745 	struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
2746 	struct list_head *head = &sev->regions_list;
2747 	struct enc_region *i;
2748 
2749 	list_for_each_entry(i, head, list) {
2750 		if (i->uaddr == range->addr &&
2751 		    i->size == range->size)
2752 			return i;
2753 	}
2754 
2755 	return NULL;
2756 }
2757 
__unregister_enc_region_locked(struct kvm * kvm,struct enc_region * region)2758 static void __unregister_enc_region_locked(struct kvm *kvm,
2759 					   struct enc_region *region)
2760 {
2761 	sev_unpin_memory(kvm, region->pages, region->npages);
2762 	list_del(&region->list);
2763 	kfree(region);
2764 }
2765 
sev_mem_enc_unregister_region(struct kvm * kvm,struct kvm_enc_region * range)2766 int sev_mem_enc_unregister_region(struct kvm *kvm,
2767 				  struct kvm_enc_region *range)
2768 {
2769 	struct enc_region *region;
2770 	int ret;
2771 
2772 	/* If kvm is mirroring encryption context it isn't responsible for it */
2773 	if (is_mirroring_enc_context(kvm))
2774 		return -EINVAL;
2775 
2776 	mutex_lock(&kvm->lock);
2777 
2778 	if (!sev_guest(kvm)) {
2779 		ret = -ENOTTY;
2780 		goto failed;
2781 	}
2782 
2783 	region = find_enc_region(kvm, range);
2784 	if (!region) {
2785 		ret = -EINVAL;
2786 		goto failed;
2787 	}
2788 
2789 	/*
2790 	 * Ensure that all guest tagged cache entries are flushed before
2791 	 * releasing the pages back to the system for use. CLFLUSH will
2792 	 * not do this, so issue a WBINVD.
2793 	 */
2794 	wbinvd_on_all_cpus();
2795 
2796 	__unregister_enc_region_locked(kvm, region);
2797 
2798 	mutex_unlock(&kvm->lock);
2799 	return 0;
2800 
2801 failed:
2802 	mutex_unlock(&kvm->lock);
2803 	return ret;
2804 }
2805 
sev_vm_copy_enc_context_from(struct kvm * kvm,unsigned int source_fd)2806 int sev_vm_copy_enc_context_from(struct kvm *kvm, unsigned int source_fd)
2807 {
2808 	struct fd f = fdget(source_fd);
2809 	struct kvm *source_kvm;
2810 	struct kvm_sev_info *source_sev, *mirror_sev;
2811 	int ret;
2812 
2813 	if (!fd_file(f))
2814 		return -EBADF;
2815 
2816 	if (!file_is_kvm(fd_file(f))) {
2817 		ret = -EBADF;
2818 		goto e_source_fput;
2819 	}
2820 
2821 	source_kvm = fd_file(f)->private_data;
2822 	ret = sev_lock_two_vms(kvm, source_kvm);
2823 	if (ret)
2824 		goto e_source_fput;
2825 
2826 	/*
2827 	 * Mirrors of mirrors should work, but let's not get silly.  Also
2828 	 * disallow out-of-band SEV/SEV-ES init if the target is already an
2829 	 * SEV guest, or if vCPUs have been created.  KVM relies on vCPUs being
2830 	 * created after SEV/SEV-ES initialization, e.g. to init intercepts.
2831 	 */
2832 	if (sev_guest(kvm) || !sev_guest(source_kvm) ||
2833 	    is_mirroring_enc_context(source_kvm) || kvm->created_vcpus) {
2834 		ret = -EINVAL;
2835 		goto e_unlock;
2836 	}
2837 
2838 	/*
2839 	 * The mirror kvm holds an enc_context_owner ref so its asid can't
2840 	 * disappear until we're done with it
2841 	 */
2842 	source_sev = &to_kvm_svm(source_kvm)->sev_info;
2843 	kvm_get_kvm(source_kvm);
2844 	mirror_sev = &to_kvm_svm(kvm)->sev_info;
2845 	list_add_tail(&mirror_sev->mirror_entry, &source_sev->mirror_vms);
2846 
2847 	/* Set enc_context_owner and copy its encryption context over */
2848 	mirror_sev->enc_context_owner = source_kvm;
2849 	mirror_sev->active = true;
2850 	mirror_sev->asid = source_sev->asid;
2851 	mirror_sev->fd = source_sev->fd;
2852 	mirror_sev->es_active = source_sev->es_active;
2853 	mirror_sev->need_init = false;
2854 	mirror_sev->handle = source_sev->handle;
2855 	INIT_LIST_HEAD(&mirror_sev->regions_list);
2856 	INIT_LIST_HEAD(&mirror_sev->mirror_vms);
2857 	ret = 0;
2858 
2859 	/*
2860 	 * Do not copy ap_jump_table. Since the mirror does not share the same
2861 	 * KVM contexts as the original, and they may have different
2862 	 * memory-views.
2863 	 */
2864 
2865 e_unlock:
2866 	sev_unlock_two_vms(kvm, source_kvm);
2867 e_source_fput:
2868 	fdput(f);
2869 	return ret;
2870 }
2871 
snp_decommission_context(struct kvm * kvm)2872 static int snp_decommission_context(struct kvm *kvm)
2873 {
2874 	struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
2875 	struct sev_data_snp_addr data = {};
2876 	int ret;
2877 
2878 	/* If context is not created then do nothing */
2879 	if (!sev->snp_context)
2880 		return 0;
2881 
2882 	/* Do the decommision, which will unbind the ASID from the SNP context */
2883 	data.address = __sme_pa(sev->snp_context);
2884 	down_write(&sev_deactivate_lock);
2885 	ret = sev_do_cmd(SEV_CMD_SNP_DECOMMISSION, &data, NULL);
2886 	up_write(&sev_deactivate_lock);
2887 
2888 	if (WARN_ONCE(ret, "Failed to release guest context, ret %d", ret))
2889 		return ret;
2890 
2891 	snp_free_firmware_page(sev->snp_context);
2892 	sev->snp_context = NULL;
2893 
2894 	return 0;
2895 }
2896 
sev_vm_destroy(struct kvm * kvm)2897 void sev_vm_destroy(struct kvm *kvm)
2898 {
2899 	struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
2900 	struct list_head *head = &sev->regions_list;
2901 	struct list_head *pos, *q;
2902 
2903 	if (!sev_guest(kvm))
2904 		return;
2905 
2906 	WARN_ON(!list_empty(&sev->mirror_vms));
2907 
2908 	/* If this is a mirror_kvm release the enc_context_owner and skip sev cleanup */
2909 	if (is_mirroring_enc_context(kvm)) {
2910 		struct kvm *owner_kvm = sev->enc_context_owner;
2911 
2912 		mutex_lock(&owner_kvm->lock);
2913 		list_del(&sev->mirror_entry);
2914 		mutex_unlock(&owner_kvm->lock);
2915 		kvm_put_kvm(owner_kvm);
2916 		return;
2917 	}
2918 
2919 	/*
2920 	 * Ensure that all guest tagged cache entries are flushed before
2921 	 * releasing the pages back to the system for use. CLFLUSH will
2922 	 * not do this, so issue a WBINVD.
2923 	 */
2924 	wbinvd_on_all_cpus();
2925 
2926 	/*
2927 	 * if userspace was terminated before unregistering the memory regions
2928 	 * then lets unpin all the registered memory.
2929 	 */
2930 	if (!list_empty(head)) {
2931 		list_for_each_safe(pos, q, head) {
2932 			__unregister_enc_region_locked(kvm,
2933 				list_entry(pos, struct enc_region, list));
2934 			cond_resched();
2935 		}
2936 	}
2937 
2938 	if (sev_snp_guest(kvm)) {
2939 		snp_guest_req_cleanup(kvm);
2940 
2941 		/*
2942 		 * Decomission handles unbinding of the ASID. If it fails for
2943 		 * some unexpected reason, just leak the ASID.
2944 		 */
2945 		if (snp_decommission_context(kvm))
2946 			return;
2947 	} else {
2948 		sev_unbind_asid(kvm, sev->handle);
2949 	}
2950 
2951 	sev_asid_free(sev);
2952 }
2953 
sev_set_cpu_caps(void)2954 void __init sev_set_cpu_caps(void)
2955 {
2956 	if (sev_enabled) {
2957 		kvm_cpu_cap_set(X86_FEATURE_SEV);
2958 		kvm_caps.supported_vm_types |= BIT(KVM_X86_SEV_VM);
2959 	}
2960 	if (sev_es_enabled) {
2961 		kvm_cpu_cap_set(X86_FEATURE_SEV_ES);
2962 		kvm_caps.supported_vm_types |= BIT(KVM_X86_SEV_ES_VM);
2963 	}
2964 	if (sev_snp_enabled) {
2965 		kvm_cpu_cap_set(X86_FEATURE_SEV_SNP);
2966 		kvm_caps.supported_vm_types |= BIT(KVM_X86_SNP_VM);
2967 	}
2968 }
2969 
sev_hardware_setup(void)2970 void __init sev_hardware_setup(void)
2971 {
2972 	unsigned int eax, ebx, ecx, edx, sev_asid_count, sev_es_asid_count;
2973 	bool sev_snp_supported = false;
2974 	bool sev_es_supported = false;
2975 	bool sev_supported = false;
2976 
2977 	if (!sev_enabled || !npt_enabled || !nrips)
2978 		goto out;
2979 
2980 	/*
2981 	 * SEV must obviously be supported in hardware.  Sanity check that the
2982 	 * CPU supports decode assists, which is mandatory for SEV guests to
2983 	 * support instruction emulation.  Ditto for flushing by ASID, as SEV
2984 	 * guests are bound to a single ASID, i.e. KVM can't rotate to a new
2985 	 * ASID to effect a TLB flush.
2986 	 */
2987 	if (!boot_cpu_has(X86_FEATURE_SEV) ||
2988 	    WARN_ON_ONCE(!boot_cpu_has(X86_FEATURE_DECODEASSISTS)) ||
2989 	    WARN_ON_ONCE(!boot_cpu_has(X86_FEATURE_FLUSHBYASID)))
2990 		goto out;
2991 
2992 	/* Retrieve SEV CPUID information */
2993 	cpuid(0x8000001f, &eax, &ebx, &ecx, &edx);
2994 
2995 	/* Set encryption bit location for SEV-ES guests */
2996 	sev_enc_bit = ebx & 0x3f;
2997 
2998 	/* Maximum number of encrypted guests supported simultaneously */
2999 	max_sev_asid = ecx;
3000 	if (!max_sev_asid)
3001 		goto out;
3002 
3003 	/* Minimum ASID value that should be used for SEV guest */
3004 	min_sev_asid = edx;
3005 	sev_me_mask = 1UL << (ebx & 0x3f);
3006 
3007 	/*
3008 	 * Initialize SEV ASID bitmaps. Allocate space for ASID 0 in the bitmap,
3009 	 * even though it's never used, so that the bitmap is indexed by the
3010 	 * actual ASID.
3011 	 */
3012 	nr_asids = max_sev_asid + 1;
3013 	sev_asid_bitmap = bitmap_zalloc(nr_asids, GFP_KERNEL);
3014 	if (!sev_asid_bitmap)
3015 		goto out;
3016 
3017 	sev_reclaim_asid_bitmap = bitmap_zalloc(nr_asids, GFP_KERNEL);
3018 	if (!sev_reclaim_asid_bitmap) {
3019 		bitmap_free(sev_asid_bitmap);
3020 		sev_asid_bitmap = NULL;
3021 		goto out;
3022 	}
3023 
3024 	if (min_sev_asid <= max_sev_asid) {
3025 		sev_asid_count = max_sev_asid - min_sev_asid + 1;
3026 		WARN_ON_ONCE(misc_cg_set_capacity(MISC_CG_RES_SEV, sev_asid_count));
3027 	}
3028 	sev_supported = true;
3029 
3030 	/* SEV-ES support requested? */
3031 	if (!sev_es_enabled)
3032 		goto out;
3033 
3034 	/*
3035 	 * SEV-ES requires MMIO caching as KVM doesn't have access to the guest
3036 	 * instruction stream, i.e. can't emulate in response to a #NPF and
3037 	 * instead relies on #NPF(RSVD) being reflected into the guest as #VC
3038 	 * (the guest can then do a #VMGEXIT to request MMIO emulation).
3039 	 */
3040 	if (!enable_mmio_caching)
3041 		goto out;
3042 
3043 	/* Does the CPU support SEV-ES? */
3044 	if (!boot_cpu_has(X86_FEATURE_SEV_ES))
3045 		goto out;
3046 
3047 	if (!lbrv) {
3048 		WARN_ONCE(!boot_cpu_has(X86_FEATURE_LBRV),
3049 			  "LBRV must be present for SEV-ES support");
3050 		goto out;
3051 	}
3052 
3053 	/* Has the system been allocated ASIDs for SEV-ES? */
3054 	if (min_sev_asid == 1)
3055 		goto out;
3056 
3057 	sev_es_asid_count = min_sev_asid - 1;
3058 	WARN_ON_ONCE(misc_cg_set_capacity(MISC_CG_RES_SEV_ES, sev_es_asid_count));
3059 	sev_es_supported = true;
3060 	sev_snp_supported = sev_snp_enabled && cc_platform_has(CC_ATTR_HOST_SEV_SNP);
3061 
3062 out:
3063 	if (boot_cpu_has(X86_FEATURE_SEV))
3064 		pr_info("SEV %s (ASIDs %u - %u)\n",
3065 			sev_supported ? min_sev_asid <= max_sev_asid ? "enabled" :
3066 								       "unusable" :
3067 								       "disabled",
3068 			min_sev_asid, max_sev_asid);
3069 	if (boot_cpu_has(X86_FEATURE_SEV_ES))
3070 		pr_info("SEV-ES %s (ASIDs %u - %u)\n",
3071 			sev_es_supported ? "enabled" : "disabled",
3072 			min_sev_asid > 1 ? 1 : 0, min_sev_asid - 1);
3073 	if (boot_cpu_has(X86_FEATURE_SEV_SNP))
3074 		pr_info("SEV-SNP %s (ASIDs %u - %u)\n",
3075 			sev_snp_supported ? "enabled" : "disabled",
3076 			min_sev_asid > 1 ? 1 : 0, min_sev_asid - 1);
3077 
3078 	sev_enabled = sev_supported;
3079 	sev_es_enabled = sev_es_supported;
3080 	sev_snp_enabled = sev_snp_supported;
3081 
3082 	if (!sev_es_enabled || !cpu_feature_enabled(X86_FEATURE_DEBUG_SWAP) ||
3083 	    !cpu_feature_enabled(X86_FEATURE_NO_NESTED_DATA_BP))
3084 		sev_es_debug_swap_enabled = false;
3085 
3086 	sev_supported_vmsa_features = 0;
3087 	if (sev_es_debug_swap_enabled)
3088 		sev_supported_vmsa_features |= SVM_SEV_FEAT_DEBUG_SWAP;
3089 }
3090 
sev_hardware_unsetup(void)3091 void sev_hardware_unsetup(void)
3092 {
3093 	if (!sev_enabled)
3094 		return;
3095 
3096 	/* No need to take sev_bitmap_lock, all VMs have been destroyed. */
3097 	sev_flush_asids(1, max_sev_asid);
3098 
3099 	bitmap_free(sev_asid_bitmap);
3100 	bitmap_free(sev_reclaim_asid_bitmap);
3101 
3102 	misc_cg_set_capacity(MISC_CG_RES_SEV, 0);
3103 	misc_cg_set_capacity(MISC_CG_RES_SEV_ES, 0);
3104 }
3105 
sev_cpu_init(struct svm_cpu_data * sd)3106 int sev_cpu_init(struct svm_cpu_data *sd)
3107 {
3108 	if (!sev_enabled)
3109 		return 0;
3110 
3111 	sd->sev_vmcbs = kcalloc(nr_asids, sizeof(void *), GFP_KERNEL);
3112 	if (!sd->sev_vmcbs)
3113 		return -ENOMEM;
3114 
3115 	return 0;
3116 }
3117 
3118 /*
3119  * Pages used by hardware to hold guest encrypted state must be flushed before
3120  * returning them to the system.
3121  */
sev_flush_encrypted_page(struct kvm_vcpu * vcpu,void * va)3122 static void sev_flush_encrypted_page(struct kvm_vcpu *vcpu, void *va)
3123 {
3124 	unsigned int asid = sev_get_asid(vcpu->kvm);
3125 
3126 	/*
3127 	 * Note!  The address must be a kernel address, as regular page walk
3128 	 * checks are performed by VM_PAGE_FLUSH, i.e. operating on a user
3129 	 * address is non-deterministic and unsafe.  This function deliberately
3130 	 * takes a pointer to deter passing in a user address.
3131 	 */
3132 	unsigned long addr = (unsigned long)va;
3133 
3134 	/*
3135 	 * If CPU enforced cache coherency for encrypted mappings of the
3136 	 * same physical page is supported, use CLFLUSHOPT instead. NOTE: cache
3137 	 * flush is still needed in order to work properly with DMA devices.
3138 	 */
3139 	if (boot_cpu_has(X86_FEATURE_SME_COHERENT)) {
3140 		clflush_cache_range(va, PAGE_SIZE);
3141 		return;
3142 	}
3143 
3144 	/*
3145 	 * VM Page Flush takes a host virtual address and a guest ASID.  Fall
3146 	 * back to WBINVD if this faults so as not to make any problems worse
3147 	 * by leaving stale encrypted data in the cache.
3148 	 */
3149 	if (WARN_ON_ONCE(wrmsrl_safe(MSR_AMD64_VM_PAGE_FLUSH, addr | asid)))
3150 		goto do_wbinvd;
3151 
3152 	return;
3153 
3154 do_wbinvd:
3155 	wbinvd_on_all_cpus();
3156 }
3157 
sev_guest_memory_reclaimed(struct kvm * kvm)3158 void sev_guest_memory_reclaimed(struct kvm *kvm)
3159 {
3160 	/*
3161 	 * With SNP+gmem, private/encrypted memory is unreachable via the
3162 	 * hva-based mmu notifiers, so these events are only actually
3163 	 * pertaining to shared pages where there is no need to perform
3164 	 * the WBINVD to flush associated caches.
3165 	 */
3166 	if (!sev_guest(kvm) || sev_snp_guest(kvm))
3167 		return;
3168 
3169 	wbinvd_on_all_cpus();
3170 }
3171 
sev_free_vcpu(struct kvm_vcpu * vcpu)3172 void sev_free_vcpu(struct kvm_vcpu *vcpu)
3173 {
3174 	struct vcpu_svm *svm;
3175 
3176 	if (!sev_es_guest(vcpu->kvm))
3177 		return;
3178 
3179 	svm = to_svm(vcpu);
3180 
3181 	/*
3182 	 * If it's an SNP guest, then the VMSA was marked in the RMP table as
3183 	 * a guest-owned page. Transition the page to hypervisor state before
3184 	 * releasing it back to the system.
3185 	 */
3186 	if (sev_snp_guest(vcpu->kvm)) {
3187 		u64 pfn = __pa(svm->sev_es.vmsa) >> PAGE_SHIFT;
3188 
3189 		if (kvm_rmp_make_shared(vcpu->kvm, pfn, PG_LEVEL_4K))
3190 			goto skip_vmsa_free;
3191 	}
3192 
3193 	if (vcpu->arch.guest_state_protected)
3194 		sev_flush_encrypted_page(vcpu, svm->sev_es.vmsa);
3195 
3196 	__free_page(virt_to_page(svm->sev_es.vmsa));
3197 
3198 skip_vmsa_free:
3199 	if (svm->sev_es.ghcb_sa_free)
3200 		kvfree(svm->sev_es.ghcb_sa);
3201 }
3202 
dump_ghcb(struct vcpu_svm * svm)3203 static void dump_ghcb(struct vcpu_svm *svm)
3204 {
3205 	struct ghcb *ghcb = svm->sev_es.ghcb;
3206 	unsigned int nbits;
3207 
3208 	/* Re-use the dump_invalid_vmcb module parameter */
3209 	if (!dump_invalid_vmcb) {
3210 		pr_warn_ratelimited("set kvm_amd.dump_invalid_vmcb=1 to dump internal KVM state.\n");
3211 		return;
3212 	}
3213 
3214 	nbits = sizeof(ghcb->save.valid_bitmap) * 8;
3215 
3216 	pr_err("GHCB (GPA=%016llx):\n", svm->vmcb->control.ghcb_gpa);
3217 	pr_err("%-20s%016llx is_valid: %u\n", "sw_exit_code",
3218 	       ghcb->save.sw_exit_code, ghcb_sw_exit_code_is_valid(ghcb));
3219 	pr_err("%-20s%016llx is_valid: %u\n", "sw_exit_info_1",
3220 	       ghcb->save.sw_exit_info_1, ghcb_sw_exit_info_1_is_valid(ghcb));
3221 	pr_err("%-20s%016llx is_valid: %u\n", "sw_exit_info_2",
3222 	       ghcb->save.sw_exit_info_2, ghcb_sw_exit_info_2_is_valid(ghcb));
3223 	pr_err("%-20s%016llx is_valid: %u\n", "sw_scratch",
3224 	       ghcb->save.sw_scratch, ghcb_sw_scratch_is_valid(ghcb));
3225 	pr_err("%-20s%*pb\n", "valid_bitmap", nbits, ghcb->save.valid_bitmap);
3226 }
3227 
sev_es_sync_to_ghcb(struct vcpu_svm * svm)3228 static void sev_es_sync_to_ghcb(struct vcpu_svm *svm)
3229 {
3230 	struct kvm_vcpu *vcpu = &svm->vcpu;
3231 	struct ghcb *ghcb = svm->sev_es.ghcb;
3232 
3233 	/*
3234 	 * The GHCB protocol so far allows for the following data
3235 	 * to be returned:
3236 	 *   GPRs RAX, RBX, RCX, RDX
3237 	 *
3238 	 * Copy their values, even if they may not have been written during the
3239 	 * VM-Exit.  It's the guest's responsibility to not consume random data.
3240 	 */
3241 	ghcb_set_rax(ghcb, vcpu->arch.regs[VCPU_REGS_RAX]);
3242 	ghcb_set_rbx(ghcb, vcpu->arch.regs[VCPU_REGS_RBX]);
3243 	ghcb_set_rcx(ghcb, vcpu->arch.regs[VCPU_REGS_RCX]);
3244 	ghcb_set_rdx(ghcb, vcpu->arch.regs[VCPU_REGS_RDX]);
3245 }
3246 
sev_es_sync_from_ghcb(struct vcpu_svm * svm)3247 static void sev_es_sync_from_ghcb(struct vcpu_svm *svm)
3248 {
3249 	struct vmcb_control_area *control = &svm->vmcb->control;
3250 	struct kvm_vcpu *vcpu = &svm->vcpu;
3251 	struct ghcb *ghcb = svm->sev_es.ghcb;
3252 	u64 exit_code;
3253 
3254 	/*
3255 	 * The GHCB protocol so far allows for the following data
3256 	 * to be supplied:
3257 	 *   GPRs RAX, RBX, RCX, RDX
3258 	 *   XCR0
3259 	 *   CPL
3260 	 *
3261 	 * VMMCALL allows the guest to provide extra registers. KVM also
3262 	 * expects RSI for hypercalls, so include that, too.
3263 	 *
3264 	 * Copy their values to the appropriate location if supplied.
3265 	 */
3266 	memset(vcpu->arch.regs, 0, sizeof(vcpu->arch.regs));
3267 
3268 	BUILD_BUG_ON(sizeof(svm->sev_es.valid_bitmap) != sizeof(ghcb->save.valid_bitmap));
3269 	memcpy(&svm->sev_es.valid_bitmap, &ghcb->save.valid_bitmap, sizeof(ghcb->save.valid_bitmap));
3270 
3271 	vcpu->arch.regs[VCPU_REGS_RAX] = kvm_ghcb_get_rax_if_valid(svm, ghcb);
3272 	vcpu->arch.regs[VCPU_REGS_RBX] = kvm_ghcb_get_rbx_if_valid(svm, ghcb);
3273 	vcpu->arch.regs[VCPU_REGS_RCX] = kvm_ghcb_get_rcx_if_valid(svm, ghcb);
3274 	vcpu->arch.regs[VCPU_REGS_RDX] = kvm_ghcb_get_rdx_if_valid(svm, ghcb);
3275 	vcpu->arch.regs[VCPU_REGS_RSI] = kvm_ghcb_get_rsi_if_valid(svm, ghcb);
3276 
3277 	svm->vmcb->save.cpl = kvm_ghcb_get_cpl_if_valid(svm, ghcb);
3278 
3279 	if (kvm_ghcb_xcr0_is_valid(svm)) {
3280 		vcpu->arch.xcr0 = ghcb_get_xcr0(ghcb);
3281 		kvm_update_cpuid_runtime(vcpu);
3282 	}
3283 
3284 	/* Copy the GHCB exit information into the VMCB fields */
3285 	exit_code = ghcb_get_sw_exit_code(ghcb);
3286 	control->exit_code = lower_32_bits(exit_code);
3287 	control->exit_code_hi = upper_32_bits(exit_code);
3288 	control->exit_info_1 = ghcb_get_sw_exit_info_1(ghcb);
3289 	control->exit_info_2 = ghcb_get_sw_exit_info_2(ghcb);
3290 	svm->sev_es.sw_scratch = kvm_ghcb_get_sw_scratch_if_valid(svm, ghcb);
3291 
3292 	/* Clear the valid entries fields */
3293 	memset(ghcb->save.valid_bitmap, 0, sizeof(ghcb->save.valid_bitmap));
3294 }
3295 
kvm_ghcb_get_sw_exit_code(struct vmcb_control_area * control)3296 static u64 kvm_ghcb_get_sw_exit_code(struct vmcb_control_area *control)
3297 {
3298 	return (((u64)control->exit_code_hi) << 32) | control->exit_code;
3299 }
3300 
sev_es_validate_vmgexit(struct vcpu_svm * svm)3301 static int sev_es_validate_vmgexit(struct vcpu_svm *svm)
3302 {
3303 	struct vmcb_control_area *control = &svm->vmcb->control;
3304 	struct kvm_vcpu *vcpu = &svm->vcpu;
3305 	u64 exit_code;
3306 	u64 reason;
3307 
3308 	/*
3309 	 * Retrieve the exit code now even though it may not be marked valid
3310 	 * as it could help with debugging.
3311 	 */
3312 	exit_code = kvm_ghcb_get_sw_exit_code(control);
3313 
3314 	/* Only GHCB Usage code 0 is supported */
3315 	if (svm->sev_es.ghcb->ghcb_usage) {
3316 		reason = GHCB_ERR_INVALID_USAGE;
3317 		goto vmgexit_err;
3318 	}
3319 
3320 	reason = GHCB_ERR_MISSING_INPUT;
3321 
3322 	if (!kvm_ghcb_sw_exit_code_is_valid(svm) ||
3323 	    !kvm_ghcb_sw_exit_info_1_is_valid(svm) ||
3324 	    !kvm_ghcb_sw_exit_info_2_is_valid(svm))
3325 		goto vmgexit_err;
3326 
3327 	switch (exit_code) {
3328 	case SVM_EXIT_READ_DR7:
3329 		break;
3330 	case SVM_EXIT_WRITE_DR7:
3331 		if (!kvm_ghcb_rax_is_valid(svm))
3332 			goto vmgexit_err;
3333 		break;
3334 	case SVM_EXIT_RDTSC:
3335 		break;
3336 	case SVM_EXIT_RDPMC:
3337 		if (!kvm_ghcb_rcx_is_valid(svm))
3338 			goto vmgexit_err;
3339 		break;
3340 	case SVM_EXIT_CPUID:
3341 		if (!kvm_ghcb_rax_is_valid(svm) ||
3342 		    !kvm_ghcb_rcx_is_valid(svm))
3343 			goto vmgexit_err;
3344 		if (vcpu->arch.regs[VCPU_REGS_RAX] == 0xd)
3345 			if (!kvm_ghcb_xcr0_is_valid(svm))
3346 				goto vmgexit_err;
3347 		break;
3348 	case SVM_EXIT_INVD:
3349 		break;
3350 	case SVM_EXIT_IOIO:
3351 		if (control->exit_info_1 & SVM_IOIO_STR_MASK) {
3352 			if (!kvm_ghcb_sw_scratch_is_valid(svm))
3353 				goto vmgexit_err;
3354 		} else {
3355 			if (!(control->exit_info_1 & SVM_IOIO_TYPE_MASK))
3356 				if (!kvm_ghcb_rax_is_valid(svm))
3357 					goto vmgexit_err;
3358 		}
3359 		break;
3360 	case SVM_EXIT_MSR:
3361 		if (!kvm_ghcb_rcx_is_valid(svm))
3362 			goto vmgexit_err;
3363 		if (control->exit_info_1) {
3364 			if (!kvm_ghcb_rax_is_valid(svm) ||
3365 			    !kvm_ghcb_rdx_is_valid(svm))
3366 				goto vmgexit_err;
3367 		}
3368 		break;
3369 	case SVM_EXIT_VMMCALL:
3370 		if (!kvm_ghcb_rax_is_valid(svm) ||
3371 		    !kvm_ghcb_cpl_is_valid(svm))
3372 			goto vmgexit_err;
3373 		break;
3374 	case SVM_EXIT_RDTSCP:
3375 		break;
3376 	case SVM_EXIT_WBINVD:
3377 		break;
3378 	case SVM_EXIT_MONITOR:
3379 		if (!kvm_ghcb_rax_is_valid(svm) ||
3380 		    !kvm_ghcb_rcx_is_valid(svm) ||
3381 		    !kvm_ghcb_rdx_is_valid(svm))
3382 			goto vmgexit_err;
3383 		break;
3384 	case SVM_EXIT_MWAIT:
3385 		if (!kvm_ghcb_rax_is_valid(svm) ||
3386 		    !kvm_ghcb_rcx_is_valid(svm))
3387 			goto vmgexit_err;
3388 		break;
3389 	case SVM_VMGEXIT_MMIO_READ:
3390 	case SVM_VMGEXIT_MMIO_WRITE:
3391 		if (!kvm_ghcb_sw_scratch_is_valid(svm))
3392 			goto vmgexit_err;
3393 		break;
3394 	case SVM_VMGEXIT_AP_CREATION:
3395 		if (!sev_snp_guest(vcpu->kvm))
3396 			goto vmgexit_err;
3397 		if (lower_32_bits(control->exit_info_1) != SVM_VMGEXIT_AP_DESTROY)
3398 			if (!kvm_ghcb_rax_is_valid(svm))
3399 				goto vmgexit_err;
3400 		break;
3401 	case SVM_VMGEXIT_NMI_COMPLETE:
3402 	case SVM_VMGEXIT_AP_HLT_LOOP:
3403 	case SVM_VMGEXIT_AP_JUMP_TABLE:
3404 	case SVM_VMGEXIT_UNSUPPORTED_EVENT:
3405 	case SVM_VMGEXIT_HV_FEATURES:
3406 	case SVM_VMGEXIT_TERM_REQUEST:
3407 		break;
3408 	case SVM_VMGEXIT_PSC:
3409 		if (!sev_snp_guest(vcpu->kvm) || !kvm_ghcb_sw_scratch_is_valid(svm))
3410 			goto vmgexit_err;
3411 		break;
3412 	case SVM_VMGEXIT_GUEST_REQUEST:
3413 	case SVM_VMGEXIT_EXT_GUEST_REQUEST:
3414 		if (!sev_snp_guest(vcpu->kvm) ||
3415 		    !PAGE_ALIGNED(control->exit_info_1) ||
3416 		    !PAGE_ALIGNED(control->exit_info_2) ||
3417 		    control->exit_info_1 == control->exit_info_2)
3418 			goto vmgexit_err;
3419 		break;
3420 	default:
3421 		reason = GHCB_ERR_INVALID_EVENT;
3422 		goto vmgexit_err;
3423 	}
3424 
3425 	return 0;
3426 
3427 vmgexit_err:
3428 	if (reason == GHCB_ERR_INVALID_USAGE) {
3429 		vcpu_unimpl(vcpu, "vmgexit: ghcb usage %#x is not valid\n",
3430 			    svm->sev_es.ghcb->ghcb_usage);
3431 	} else if (reason == GHCB_ERR_INVALID_EVENT) {
3432 		vcpu_unimpl(vcpu, "vmgexit: exit code %#llx is not valid\n",
3433 			    exit_code);
3434 	} else {
3435 		vcpu_unimpl(vcpu, "vmgexit: exit code %#llx input is not valid\n",
3436 			    exit_code);
3437 		dump_ghcb(svm);
3438 	}
3439 
3440 	ghcb_set_sw_exit_info_1(svm->sev_es.ghcb, 2);
3441 	ghcb_set_sw_exit_info_2(svm->sev_es.ghcb, reason);
3442 
3443 	/* Resume the guest to "return" the error code. */
3444 	return 1;
3445 }
3446 
sev_es_unmap_ghcb(struct vcpu_svm * svm)3447 void sev_es_unmap_ghcb(struct vcpu_svm *svm)
3448 {
3449 	/* Clear any indication that the vCPU is in a type of AP Reset Hold */
3450 	svm->sev_es.ap_reset_hold_type = AP_RESET_HOLD_NONE;
3451 
3452 	if (!svm->sev_es.ghcb)
3453 		return;
3454 
3455 	if (svm->sev_es.ghcb_sa_free) {
3456 		/*
3457 		 * The scratch area lives outside the GHCB, so there is a
3458 		 * buffer that, depending on the operation performed, may
3459 		 * need to be synced, then freed.
3460 		 */
3461 		if (svm->sev_es.ghcb_sa_sync) {
3462 			kvm_write_guest(svm->vcpu.kvm,
3463 					svm->sev_es.sw_scratch,
3464 					svm->sev_es.ghcb_sa,
3465 					svm->sev_es.ghcb_sa_len);
3466 			svm->sev_es.ghcb_sa_sync = false;
3467 		}
3468 
3469 		kvfree(svm->sev_es.ghcb_sa);
3470 		svm->sev_es.ghcb_sa = NULL;
3471 		svm->sev_es.ghcb_sa_free = false;
3472 	}
3473 
3474 	trace_kvm_vmgexit_exit(svm->vcpu.vcpu_id, svm->sev_es.ghcb);
3475 
3476 	sev_es_sync_to_ghcb(svm);
3477 
3478 	kvm_vcpu_unmap(&svm->vcpu, &svm->sev_es.ghcb_map, true);
3479 	svm->sev_es.ghcb = NULL;
3480 }
3481 
pre_sev_run(struct vcpu_svm * svm,int cpu)3482 void pre_sev_run(struct vcpu_svm *svm, int cpu)
3483 {
3484 	struct svm_cpu_data *sd = per_cpu_ptr(&svm_data, cpu);
3485 	unsigned int asid = sev_get_asid(svm->vcpu.kvm);
3486 
3487 	/* Assign the asid allocated with this SEV guest */
3488 	svm->asid = asid;
3489 
3490 	/*
3491 	 * Flush guest TLB:
3492 	 *
3493 	 * 1) when different VMCB for the same ASID is to be run on the same host CPU.
3494 	 * 2) or this VMCB was executed on different host CPU in previous VMRUNs.
3495 	 */
3496 	if (sd->sev_vmcbs[asid] == svm->vmcb &&
3497 	    svm->vcpu.arch.last_vmentry_cpu == cpu)
3498 		return;
3499 
3500 	sd->sev_vmcbs[asid] = svm->vmcb;
3501 	svm->vmcb->control.tlb_ctl = TLB_CONTROL_FLUSH_ASID;
3502 	vmcb_mark_dirty(svm->vmcb, VMCB_ASID);
3503 }
3504 
3505 #define GHCB_SCRATCH_AREA_LIMIT		(16ULL * PAGE_SIZE)
setup_vmgexit_scratch(struct vcpu_svm * svm,bool sync,u64 len)3506 static int setup_vmgexit_scratch(struct vcpu_svm *svm, bool sync, u64 len)
3507 {
3508 	struct vmcb_control_area *control = &svm->vmcb->control;
3509 	u64 ghcb_scratch_beg, ghcb_scratch_end;
3510 	u64 scratch_gpa_beg, scratch_gpa_end;
3511 	void *scratch_va;
3512 
3513 	scratch_gpa_beg = svm->sev_es.sw_scratch;
3514 	if (!scratch_gpa_beg) {
3515 		pr_err("vmgexit: scratch gpa not provided\n");
3516 		goto e_scratch;
3517 	}
3518 
3519 	scratch_gpa_end = scratch_gpa_beg + len;
3520 	if (scratch_gpa_end < scratch_gpa_beg) {
3521 		pr_err("vmgexit: scratch length (%#llx) not valid for scratch address (%#llx)\n",
3522 		       len, scratch_gpa_beg);
3523 		goto e_scratch;
3524 	}
3525 
3526 	if ((scratch_gpa_beg & PAGE_MASK) == control->ghcb_gpa) {
3527 		/* Scratch area begins within GHCB */
3528 		ghcb_scratch_beg = control->ghcb_gpa +
3529 				   offsetof(struct ghcb, shared_buffer);
3530 		ghcb_scratch_end = control->ghcb_gpa +
3531 				   offsetof(struct ghcb, reserved_0xff0);
3532 
3533 		/*
3534 		 * If the scratch area begins within the GHCB, it must be
3535 		 * completely contained in the GHCB shared buffer area.
3536 		 */
3537 		if (scratch_gpa_beg < ghcb_scratch_beg ||
3538 		    scratch_gpa_end > ghcb_scratch_end) {
3539 			pr_err("vmgexit: scratch area is outside of GHCB shared buffer area (%#llx - %#llx)\n",
3540 			       scratch_gpa_beg, scratch_gpa_end);
3541 			goto e_scratch;
3542 		}
3543 
3544 		scratch_va = (void *)svm->sev_es.ghcb;
3545 		scratch_va += (scratch_gpa_beg - control->ghcb_gpa);
3546 	} else {
3547 		/*
3548 		 * The guest memory must be read into a kernel buffer, so
3549 		 * limit the size
3550 		 */
3551 		if (len > GHCB_SCRATCH_AREA_LIMIT) {
3552 			pr_err("vmgexit: scratch area exceeds KVM limits (%#llx requested, %#llx limit)\n",
3553 			       len, GHCB_SCRATCH_AREA_LIMIT);
3554 			goto e_scratch;
3555 		}
3556 		scratch_va = kvzalloc(len, GFP_KERNEL_ACCOUNT);
3557 		if (!scratch_va)
3558 			return -ENOMEM;
3559 
3560 		if (kvm_read_guest(svm->vcpu.kvm, scratch_gpa_beg, scratch_va, len)) {
3561 			/* Unable to copy scratch area from guest */
3562 			pr_err("vmgexit: kvm_read_guest for scratch area failed\n");
3563 
3564 			kvfree(scratch_va);
3565 			return -EFAULT;
3566 		}
3567 
3568 		/*
3569 		 * The scratch area is outside the GHCB. The operation will
3570 		 * dictate whether the buffer needs to be synced before running
3571 		 * the vCPU next time (i.e. a read was requested so the data
3572 		 * must be written back to the guest memory).
3573 		 */
3574 		svm->sev_es.ghcb_sa_sync = sync;
3575 		svm->sev_es.ghcb_sa_free = true;
3576 	}
3577 
3578 	svm->sev_es.ghcb_sa = scratch_va;
3579 	svm->sev_es.ghcb_sa_len = len;
3580 
3581 	return 0;
3582 
3583 e_scratch:
3584 	ghcb_set_sw_exit_info_1(svm->sev_es.ghcb, 2);
3585 	ghcb_set_sw_exit_info_2(svm->sev_es.ghcb, GHCB_ERR_INVALID_SCRATCH_AREA);
3586 
3587 	return 1;
3588 }
3589 
set_ghcb_msr_bits(struct vcpu_svm * svm,u64 value,u64 mask,unsigned int pos)3590 static void set_ghcb_msr_bits(struct vcpu_svm *svm, u64 value, u64 mask,
3591 			      unsigned int pos)
3592 {
3593 	svm->vmcb->control.ghcb_gpa &= ~(mask << pos);
3594 	svm->vmcb->control.ghcb_gpa |= (value & mask) << pos;
3595 }
3596 
get_ghcb_msr_bits(struct vcpu_svm * svm,u64 mask,unsigned int pos)3597 static u64 get_ghcb_msr_bits(struct vcpu_svm *svm, u64 mask, unsigned int pos)
3598 {
3599 	return (svm->vmcb->control.ghcb_gpa >> pos) & mask;
3600 }
3601 
set_ghcb_msr(struct vcpu_svm * svm,u64 value)3602 static void set_ghcb_msr(struct vcpu_svm *svm, u64 value)
3603 {
3604 	svm->vmcb->control.ghcb_gpa = value;
3605 }
3606 
snp_rmptable_psmash(kvm_pfn_t pfn)3607 static int snp_rmptable_psmash(kvm_pfn_t pfn)
3608 {
3609 	int ret;
3610 
3611 	pfn = pfn & ~(KVM_PAGES_PER_HPAGE(PG_LEVEL_2M) - 1);
3612 
3613 	/*
3614 	 * PSMASH_FAIL_INUSE indicates another processor is modifying the
3615 	 * entry, so retry until that's no longer the case.
3616 	 */
3617 	do {
3618 		ret = psmash(pfn);
3619 	} while (ret == PSMASH_FAIL_INUSE);
3620 
3621 	return ret;
3622 }
3623 
snp_complete_psc_msr(struct kvm_vcpu * vcpu)3624 static int snp_complete_psc_msr(struct kvm_vcpu *vcpu)
3625 {
3626 	struct vcpu_svm *svm = to_svm(vcpu);
3627 
3628 	if (vcpu->run->hypercall.ret)
3629 		set_ghcb_msr(svm, GHCB_MSR_PSC_RESP_ERROR);
3630 	else
3631 		set_ghcb_msr(svm, GHCB_MSR_PSC_RESP);
3632 
3633 	return 1; /* resume guest */
3634 }
3635 
snp_begin_psc_msr(struct vcpu_svm * svm,u64 ghcb_msr)3636 static int snp_begin_psc_msr(struct vcpu_svm *svm, u64 ghcb_msr)
3637 {
3638 	u64 gpa = gfn_to_gpa(GHCB_MSR_PSC_REQ_TO_GFN(ghcb_msr));
3639 	u8 op = GHCB_MSR_PSC_REQ_TO_OP(ghcb_msr);
3640 	struct kvm_vcpu *vcpu = &svm->vcpu;
3641 
3642 	if (op != SNP_PAGE_STATE_PRIVATE && op != SNP_PAGE_STATE_SHARED) {
3643 		set_ghcb_msr(svm, GHCB_MSR_PSC_RESP_ERROR);
3644 		return 1; /* resume guest */
3645 	}
3646 
3647 	if (!(vcpu->kvm->arch.hypercall_exit_enabled & (1 << KVM_HC_MAP_GPA_RANGE))) {
3648 		set_ghcb_msr(svm, GHCB_MSR_PSC_RESP_ERROR);
3649 		return 1; /* resume guest */
3650 	}
3651 
3652 	vcpu->run->exit_reason = KVM_EXIT_HYPERCALL;
3653 	vcpu->run->hypercall.nr = KVM_HC_MAP_GPA_RANGE;
3654 	vcpu->run->hypercall.args[0] = gpa;
3655 	vcpu->run->hypercall.args[1] = 1;
3656 	vcpu->run->hypercall.args[2] = (op == SNP_PAGE_STATE_PRIVATE)
3657 				       ? KVM_MAP_GPA_RANGE_ENCRYPTED
3658 				       : KVM_MAP_GPA_RANGE_DECRYPTED;
3659 	vcpu->run->hypercall.args[2] |= KVM_MAP_GPA_RANGE_PAGE_SZ_4K;
3660 
3661 	vcpu->arch.complete_userspace_io = snp_complete_psc_msr;
3662 
3663 	return 0; /* forward request to userspace */
3664 }
3665 
3666 struct psc_buffer {
3667 	struct psc_hdr hdr;
3668 	struct psc_entry entries[];
3669 } __packed;
3670 
3671 static int snp_begin_psc(struct vcpu_svm *svm, struct psc_buffer *psc);
3672 
snp_complete_psc(struct vcpu_svm * svm,u64 psc_ret)3673 static void snp_complete_psc(struct vcpu_svm *svm, u64 psc_ret)
3674 {
3675 	svm->sev_es.psc_inflight = 0;
3676 	svm->sev_es.psc_idx = 0;
3677 	svm->sev_es.psc_2m = false;
3678 	ghcb_set_sw_exit_info_2(svm->sev_es.ghcb, psc_ret);
3679 }
3680 
__snp_complete_one_psc(struct vcpu_svm * svm)3681 static void __snp_complete_one_psc(struct vcpu_svm *svm)
3682 {
3683 	struct psc_buffer *psc = svm->sev_es.ghcb_sa;
3684 	struct psc_entry *entries = psc->entries;
3685 	struct psc_hdr *hdr = &psc->hdr;
3686 	__u16 idx;
3687 
3688 	/*
3689 	 * Everything in-flight has been processed successfully. Update the
3690 	 * corresponding entries in the guest's PSC buffer and zero out the
3691 	 * count of in-flight PSC entries.
3692 	 */
3693 	for (idx = svm->sev_es.psc_idx; svm->sev_es.psc_inflight;
3694 	     svm->sev_es.psc_inflight--, idx++) {
3695 		struct psc_entry *entry = &entries[idx];
3696 
3697 		entry->cur_page = entry->pagesize ? 512 : 1;
3698 	}
3699 
3700 	hdr->cur_entry = idx;
3701 }
3702 
snp_complete_one_psc(struct kvm_vcpu * vcpu)3703 static int snp_complete_one_psc(struct kvm_vcpu *vcpu)
3704 {
3705 	struct vcpu_svm *svm = to_svm(vcpu);
3706 	struct psc_buffer *psc = svm->sev_es.ghcb_sa;
3707 
3708 	if (vcpu->run->hypercall.ret) {
3709 		snp_complete_psc(svm, VMGEXIT_PSC_ERROR_GENERIC);
3710 		return 1; /* resume guest */
3711 	}
3712 
3713 	__snp_complete_one_psc(svm);
3714 
3715 	/* Handle the next range (if any). */
3716 	return snp_begin_psc(svm, psc);
3717 }
3718 
snp_begin_psc(struct vcpu_svm * svm,struct psc_buffer * psc)3719 static int snp_begin_psc(struct vcpu_svm *svm, struct psc_buffer *psc)
3720 {
3721 	struct psc_entry *entries = psc->entries;
3722 	struct kvm_vcpu *vcpu = &svm->vcpu;
3723 	struct psc_hdr *hdr = &psc->hdr;
3724 	struct psc_entry entry_start;
3725 	u16 idx, idx_start, idx_end;
3726 	int npages;
3727 	bool huge;
3728 	u64 gfn;
3729 
3730 	if (!(vcpu->kvm->arch.hypercall_exit_enabled & (1 << KVM_HC_MAP_GPA_RANGE))) {
3731 		snp_complete_psc(svm, VMGEXIT_PSC_ERROR_GENERIC);
3732 		return 1;
3733 	}
3734 
3735 next_range:
3736 	/* There should be no other PSCs in-flight at this point. */
3737 	if (WARN_ON_ONCE(svm->sev_es.psc_inflight)) {
3738 		snp_complete_psc(svm, VMGEXIT_PSC_ERROR_GENERIC);
3739 		return 1;
3740 	}
3741 
3742 	/*
3743 	 * The PSC descriptor buffer can be modified by a misbehaved guest after
3744 	 * validation, so take care to only use validated copies of values used
3745 	 * for things like array indexing.
3746 	 */
3747 	idx_start = hdr->cur_entry;
3748 	idx_end = hdr->end_entry;
3749 
3750 	if (idx_end >= VMGEXIT_PSC_MAX_COUNT) {
3751 		snp_complete_psc(svm, VMGEXIT_PSC_ERROR_INVALID_HDR);
3752 		return 1;
3753 	}
3754 
3755 	/* Find the start of the next range which needs processing. */
3756 	for (idx = idx_start; idx <= idx_end; idx++, hdr->cur_entry++) {
3757 		entry_start = entries[idx];
3758 
3759 		gfn = entry_start.gfn;
3760 		huge = entry_start.pagesize;
3761 		npages = huge ? 512 : 1;
3762 
3763 		if (entry_start.cur_page > npages || !IS_ALIGNED(gfn, npages)) {
3764 			snp_complete_psc(svm, VMGEXIT_PSC_ERROR_INVALID_ENTRY);
3765 			return 1;
3766 		}
3767 
3768 		if (entry_start.cur_page) {
3769 			/*
3770 			 * If this is a partially-completed 2M range, force 4K handling
3771 			 * for the remaining pages since they're effectively split at
3772 			 * this point. Subsequent code should ensure this doesn't get
3773 			 * combined with adjacent PSC entries where 2M handling is still
3774 			 * possible.
3775 			 */
3776 			npages -= entry_start.cur_page;
3777 			gfn += entry_start.cur_page;
3778 			huge = false;
3779 		}
3780 
3781 		if (npages)
3782 			break;
3783 	}
3784 
3785 	if (idx > idx_end) {
3786 		/* Nothing more to process. */
3787 		snp_complete_psc(svm, 0);
3788 		return 1;
3789 	}
3790 
3791 	svm->sev_es.psc_2m = huge;
3792 	svm->sev_es.psc_idx = idx;
3793 	svm->sev_es.psc_inflight = 1;
3794 
3795 	/*
3796 	 * Find all subsequent PSC entries that contain adjacent GPA
3797 	 * ranges/operations and can be combined into a single
3798 	 * KVM_HC_MAP_GPA_RANGE exit.
3799 	 */
3800 	while (++idx <= idx_end) {
3801 		struct psc_entry entry = entries[idx];
3802 
3803 		if (entry.operation != entry_start.operation ||
3804 		    entry.gfn != entry_start.gfn + npages ||
3805 		    entry.cur_page || !!entry.pagesize != huge)
3806 			break;
3807 
3808 		svm->sev_es.psc_inflight++;
3809 		npages += huge ? 512 : 1;
3810 	}
3811 
3812 	switch (entry_start.operation) {
3813 	case VMGEXIT_PSC_OP_PRIVATE:
3814 	case VMGEXIT_PSC_OP_SHARED:
3815 		vcpu->run->exit_reason = KVM_EXIT_HYPERCALL;
3816 		vcpu->run->hypercall.nr = KVM_HC_MAP_GPA_RANGE;
3817 		vcpu->run->hypercall.args[0] = gfn_to_gpa(gfn);
3818 		vcpu->run->hypercall.args[1] = npages;
3819 		vcpu->run->hypercall.args[2] = entry_start.operation == VMGEXIT_PSC_OP_PRIVATE
3820 					       ? KVM_MAP_GPA_RANGE_ENCRYPTED
3821 					       : KVM_MAP_GPA_RANGE_DECRYPTED;
3822 		vcpu->run->hypercall.args[2] |= entry_start.pagesize
3823 						? KVM_MAP_GPA_RANGE_PAGE_SZ_2M
3824 						: KVM_MAP_GPA_RANGE_PAGE_SZ_4K;
3825 		vcpu->arch.complete_userspace_io = snp_complete_one_psc;
3826 		return 0; /* forward request to userspace */
3827 	default:
3828 		/*
3829 		 * Only shared/private PSC operations are currently supported, so if the
3830 		 * entire range consists of unsupported operations (e.g. SMASH/UNSMASH),
3831 		 * then consider the entire range completed and avoid exiting to
3832 		 * userspace. In theory snp_complete_psc() can always be called directly
3833 		 * at this point to complete the current range and start the next one,
3834 		 * but that could lead to unexpected levels of recursion.
3835 		 */
3836 		__snp_complete_one_psc(svm);
3837 		goto next_range;
3838 	}
3839 
3840 	BUG();
3841 }
3842 
__sev_snp_update_protected_guest_state(struct kvm_vcpu * vcpu)3843 static int __sev_snp_update_protected_guest_state(struct kvm_vcpu *vcpu)
3844 {
3845 	struct vcpu_svm *svm = to_svm(vcpu);
3846 
3847 	WARN_ON(!mutex_is_locked(&svm->sev_es.snp_vmsa_mutex));
3848 
3849 	/* Mark the vCPU as offline and not runnable */
3850 	vcpu->arch.pv.pv_unhalted = false;
3851 	vcpu->arch.mp_state = KVM_MP_STATE_HALTED;
3852 
3853 	/* Clear use of the VMSA */
3854 	svm->vmcb->control.vmsa_pa = INVALID_PAGE;
3855 
3856 	if (VALID_PAGE(svm->sev_es.snp_vmsa_gpa)) {
3857 		gfn_t gfn = gpa_to_gfn(svm->sev_es.snp_vmsa_gpa);
3858 		struct kvm_memory_slot *slot;
3859 		kvm_pfn_t pfn;
3860 
3861 		slot = gfn_to_memslot(vcpu->kvm, gfn);
3862 		if (!slot)
3863 			return -EINVAL;
3864 
3865 		/*
3866 		 * The new VMSA will be private memory guest memory, so
3867 		 * retrieve the PFN from the gmem backend.
3868 		 */
3869 		if (kvm_gmem_get_pfn(vcpu->kvm, slot, gfn, &pfn, NULL))
3870 			return -EINVAL;
3871 
3872 		/*
3873 		 * From this point forward, the VMSA will always be a
3874 		 * guest-mapped page rather than the initial one allocated
3875 		 * by KVM in svm->sev_es.vmsa. In theory, svm->sev_es.vmsa
3876 		 * could be free'd and cleaned up here, but that involves
3877 		 * cleanups like wbinvd_on_all_cpus() which would ideally
3878 		 * be handled during teardown rather than guest boot.
3879 		 * Deferring that also allows the existing logic for SEV-ES
3880 		 * VMSAs to be re-used with minimal SNP-specific changes.
3881 		 */
3882 		svm->sev_es.snp_has_guest_vmsa = true;
3883 
3884 		/* Use the new VMSA */
3885 		svm->vmcb->control.vmsa_pa = pfn_to_hpa(pfn);
3886 
3887 		/* Mark the vCPU as runnable */
3888 		vcpu->arch.pv.pv_unhalted = false;
3889 		vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
3890 
3891 		svm->sev_es.snp_vmsa_gpa = INVALID_PAGE;
3892 
3893 		/*
3894 		 * gmem pages aren't currently migratable, but if this ever
3895 		 * changes then care should be taken to ensure
3896 		 * svm->sev_es.vmsa is pinned through some other means.
3897 		 */
3898 		kvm_release_pfn_clean(pfn);
3899 	}
3900 
3901 	/*
3902 	 * When replacing the VMSA during SEV-SNP AP creation,
3903 	 * mark the VMCB dirty so that full state is always reloaded.
3904 	 */
3905 	vmcb_mark_all_dirty(svm->vmcb);
3906 
3907 	return 0;
3908 }
3909 
3910 /*
3911  * Invoked as part of svm_vcpu_reset() processing of an init event.
3912  */
sev_snp_init_protected_guest_state(struct kvm_vcpu * vcpu)3913 void sev_snp_init_protected_guest_state(struct kvm_vcpu *vcpu)
3914 {
3915 	struct vcpu_svm *svm = to_svm(vcpu);
3916 	int ret;
3917 
3918 	if (!sev_snp_guest(vcpu->kvm))
3919 		return;
3920 
3921 	mutex_lock(&svm->sev_es.snp_vmsa_mutex);
3922 
3923 	if (!svm->sev_es.snp_ap_waiting_for_reset)
3924 		goto unlock;
3925 
3926 	svm->sev_es.snp_ap_waiting_for_reset = false;
3927 
3928 	ret = __sev_snp_update_protected_guest_state(vcpu);
3929 	if (ret)
3930 		vcpu_unimpl(vcpu, "snp: AP state update on init failed\n");
3931 
3932 unlock:
3933 	mutex_unlock(&svm->sev_es.snp_vmsa_mutex);
3934 }
3935 
sev_snp_ap_creation(struct vcpu_svm * svm)3936 static int sev_snp_ap_creation(struct vcpu_svm *svm)
3937 {
3938 	struct kvm_sev_info *sev = &to_kvm_svm(svm->vcpu.kvm)->sev_info;
3939 	struct kvm_vcpu *vcpu = &svm->vcpu;
3940 	struct kvm_vcpu *target_vcpu;
3941 	struct vcpu_svm *target_svm;
3942 	unsigned int request;
3943 	unsigned int apic_id;
3944 	bool kick;
3945 	int ret;
3946 
3947 	request = lower_32_bits(svm->vmcb->control.exit_info_1);
3948 	apic_id = upper_32_bits(svm->vmcb->control.exit_info_1);
3949 
3950 	/* Validate the APIC ID */
3951 	target_vcpu = kvm_get_vcpu_by_id(vcpu->kvm, apic_id);
3952 	if (!target_vcpu) {
3953 		vcpu_unimpl(vcpu, "vmgexit: invalid AP APIC ID [%#x] from guest\n",
3954 			    apic_id);
3955 		return -EINVAL;
3956 	}
3957 
3958 	ret = 0;
3959 
3960 	target_svm = to_svm(target_vcpu);
3961 
3962 	/*
3963 	 * The target vCPU is valid, so the vCPU will be kicked unless the
3964 	 * request is for CREATE_ON_INIT.
3965 	 */
3966 	kick = true;
3967 
3968 	mutex_lock(&target_svm->sev_es.snp_vmsa_mutex);
3969 
3970 	/* Interrupt injection mode shouldn't change for AP creation */
3971 	if (request < SVM_VMGEXIT_AP_DESTROY) {
3972 		u64 sev_features;
3973 
3974 		sev_features = vcpu->arch.regs[VCPU_REGS_RAX];
3975 		sev_features ^= sev->vmsa_features;
3976 
3977 		if (sev_features & SVM_SEV_FEAT_INT_INJ_MODES) {
3978 			vcpu_unimpl(vcpu, "vmgexit: invalid AP injection mode [%#lx] from guest\n",
3979 				    vcpu->arch.regs[VCPU_REGS_RAX]);
3980 			ret = -EINVAL;
3981 			goto out;
3982 		}
3983 	}
3984 
3985 	switch (request) {
3986 	case SVM_VMGEXIT_AP_CREATE_ON_INIT:
3987 		kick = false;
3988 		fallthrough;
3989 	case SVM_VMGEXIT_AP_CREATE:
3990 		if (!page_address_valid(vcpu, svm->vmcb->control.exit_info_2)) {
3991 			vcpu_unimpl(vcpu, "vmgexit: invalid AP VMSA address [%#llx] from guest\n",
3992 				    svm->vmcb->control.exit_info_2);
3993 			ret = -EINVAL;
3994 			goto out;
3995 		}
3996 
3997 		/*
3998 		 * Malicious guest can RMPADJUST a large page into VMSA which
3999 		 * will hit the SNP erratum where the CPU will incorrectly signal
4000 		 * an RMP violation #PF if a hugepage collides with the RMP entry
4001 		 * of VMSA page, reject the AP CREATE request if VMSA address from
4002 		 * guest is 2M aligned.
4003 		 */
4004 		if (IS_ALIGNED(svm->vmcb->control.exit_info_2, PMD_SIZE)) {
4005 			vcpu_unimpl(vcpu,
4006 				    "vmgexit: AP VMSA address [%llx] from guest is unsafe as it is 2M aligned\n",
4007 				    svm->vmcb->control.exit_info_2);
4008 			ret = -EINVAL;
4009 			goto out;
4010 		}
4011 
4012 		target_svm->sev_es.snp_vmsa_gpa = svm->vmcb->control.exit_info_2;
4013 		break;
4014 	case SVM_VMGEXIT_AP_DESTROY:
4015 		target_svm->sev_es.snp_vmsa_gpa = INVALID_PAGE;
4016 		break;
4017 	default:
4018 		vcpu_unimpl(vcpu, "vmgexit: invalid AP creation request [%#x] from guest\n",
4019 			    request);
4020 		ret = -EINVAL;
4021 		goto out;
4022 	}
4023 
4024 	target_svm->sev_es.snp_ap_waiting_for_reset = true;
4025 
4026 	if (kick) {
4027 		kvm_make_request(KVM_REQ_UPDATE_PROTECTED_GUEST_STATE, target_vcpu);
4028 		kvm_vcpu_kick(target_vcpu);
4029 	}
4030 
4031 out:
4032 	mutex_unlock(&target_svm->sev_es.snp_vmsa_mutex);
4033 
4034 	return ret;
4035 }
4036 
snp_handle_guest_req(struct vcpu_svm * svm,gpa_t req_gpa,gpa_t resp_gpa)4037 static int snp_handle_guest_req(struct vcpu_svm *svm, gpa_t req_gpa, gpa_t resp_gpa)
4038 {
4039 	struct sev_data_snp_guest_request data = {0};
4040 	struct kvm *kvm = svm->vcpu.kvm;
4041 	struct kvm_sev_info *sev = to_kvm_sev_info(kvm);
4042 	sev_ret_code fw_err = 0;
4043 	int ret;
4044 
4045 	if (!sev_snp_guest(kvm))
4046 		return -EINVAL;
4047 
4048 	mutex_lock(&sev->guest_req_mutex);
4049 
4050 	if (kvm_read_guest(kvm, req_gpa, sev->guest_req_buf, PAGE_SIZE)) {
4051 		ret = -EIO;
4052 		goto out_unlock;
4053 	}
4054 
4055 	data.gctx_paddr = __psp_pa(sev->snp_context);
4056 	data.req_paddr = __psp_pa(sev->guest_req_buf);
4057 	data.res_paddr = __psp_pa(sev->guest_resp_buf);
4058 
4059 	/*
4060 	 * Firmware failures are propagated on to guest, but any other failure
4061 	 * condition along the way should be reported to userspace. E.g. if
4062 	 * the PSP is dead and commands are timing out.
4063 	 */
4064 	ret = sev_issue_cmd(kvm, SEV_CMD_SNP_GUEST_REQUEST, &data, &fw_err);
4065 	if (ret && !fw_err)
4066 		goto out_unlock;
4067 
4068 	if (kvm_write_guest(kvm, resp_gpa, sev->guest_resp_buf, PAGE_SIZE)) {
4069 		ret = -EIO;
4070 		goto out_unlock;
4071 	}
4072 
4073 	ghcb_set_sw_exit_info_2(svm->sev_es.ghcb, SNP_GUEST_ERR(0, fw_err));
4074 
4075 	ret = 1; /* resume guest */
4076 
4077 out_unlock:
4078 	mutex_unlock(&sev->guest_req_mutex);
4079 	return ret;
4080 }
4081 
snp_handle_ext_guest_req(struct vcpu_svm * svm,gpa_t req_gpa,gpa_t resp_gpa)4082 static int snp_handle_ext_guest_req(struct vcpu_svm *svm, gpa_t req_gpa, gpa_t resp_gpa)
4083 {
4084 	struct kvm *kvm = svm->vcpu.kvm;
4085 	u8 msg_type;
4086 
4087 	if (!sev_snp_guest(kvm))
4088 		return -EINVAL;
4089 
4090 	if (kvm_read_guest(kvm, req_gpa + offsetof(struct snp_guest_msg_hdr, msg_type),
4091 			   &msg_type, 1))
4092 		return -EIO;
4093 
4094 	/*
4095 	 * As per GHCB spec, requests of type MSG_REPORT_REQ also allow for
4096 	 * additional certificate data to be provided alongside the attestation
4097 	 * report via the guest-provided data pages indicated by RAX/RBX. The
4098 	 * certificate data is optional and requires additional KVM enablement
4099 	 * to provide an interface for userspace to provide it, but KVM still
4100 	 * needs to be able to handle extended guest requests either way. So
4101 	 * provide a stub implementation that will always return an empty
4102 	 * certificate table in the guest-provided data pages.
4103 	 */
4104 	if (msg_type == SNP_MSG_REPORT_REQ) {
4105 		struct kvm_vcpu *vcpu = &svm->vcpu;
4106 		u64 data_npages;
4107 		gpa_t data_gpa;
4108 
4109 		if (!kvm_ghcb_rax_is_valid(svm) || !kvm_ghcb_rbx_is_valid(svm))
4110 			goto request_invalid;
4111 
4112 		data_gpa = vcpu->arch.regs[VCPU_REGS_RAX];
4113 		data_npages = vcpu->arch.regs[VCPU_REGS_RBX];
4114 
4115 		if (!PAGE_ALIGNED(data_gpa))
4116 			goto request_invalid;
4117 
4118 		/*
4119 		 * As per GHCB spec (see "SNP Extended Guest Request"), the
4120 		 * certificate table is terminated by 24-bytes of zeroes.
4121 		 */
4122 		if (data_npages && kvm_clear_guest(kvm, data_gpa, 24))
4123 			return -EIO;
4124 	}
4125 
4126 	return snp_handle_guest_req(svm, req_gpa, resp_gpa);
4127 
4128 request_invalid:
4129 	ghcb_set_sw_exit_info_1(svm->sev_es.ghcb, 2);
4130 	ghcb_set_sw_exit_info_2(svm->sev_es.ghcb, GHCB_ERR_INVALID_INPUT);
4131 	return 1; /* resume guest */
4132 }
4133 
sev_handle_vmgexit_msr_protocol(struct vcpu_svm * svm)4134 static int sev_handle_vmgexit_msr_protocol(struct vcpu_svm *svm)
4135 {
4136 	struct vmcb_control_area *control = &svm->vmcb->control;
4137 	struct kvm_vcpu *vcpu = &svm->vcpu;
4138 	struct kvm_sev_info *sev = &to_kvm_svm(vcpu->kvm)->sev_info;
4139 	u64 ghcb_info;
4140 	int ret = 1;
4141 
4142 	ghcb_info = control->ghcb_gpa & GHCB_MSR_INFO_MASK;
4143 
4144 	trace_kvm_vmgexit_msr_protocol_enter(svm->vcpu.vcpu_id,
4145 					     control->ghcb_gpa);
4146 
4147 	switch (ghcb_info) {
4148 	case GHCB_MSR_SEV_INFO_REQ:
4149 		set_ghcb_msr(svm, GHCB_MSR_SEV_INFO((__u64)sev->ghcb_version,
4150 						    GHCB_VERSION_MIN,
4151 						    sev_enc_bit));
4152 		break;
4153 	case GHCB_MSR_CPUID_REQ: {
4154 		u64 cpuid_fn, cpuid_reg, cpuid_value;
4155 
4156 		cpuid_fn = get_ghcb_msr_bits(svm,
4157 					     GHCB_MSR_CPUID_FUNC_MASK,
4158 					     GHCB_MSR_CPUID_FUNC_POS);
4159 
4160 		/* Initialize the registers needed by the CPUID intercept */
4161 		vcpu->arch.regs[VCPU_REGS_RAX] = cpuid_fn;
4162 		vcpu->arch.regs[VCPU_REGS_RCX] = 0;
4163 
4164 		ret = svm_invoke_exit_handler(vcpu, SVM_EXIT_CPUID);
4165 		if (!ret) {
4166 			/* Error, keep GHCB MSR value as-is */
4167 			break;
4168 		}
4169 
4170 		cpuid_reg = get_ghcb_msr_bits(svm,
4171 					      GHCB_MSR_CPUID_REG_MASK,
4172 					      GHCB_MSR_CPUID_REG_POS);
4173 		if (cpuid_reg == 0)
4174 			cpuid_value = vcpu->arch.regs[VCPU_REGS_RAX];
4175 		else if (cpuid_reg == 1)
4176 			cpuid_value = vcpu->arch.regs[VCPU_REGS_RBX];
4177 		else if (cpuid_reg == 2)
4178 			cpuid_value = vcpu->arch.regs[VCPU_REGS_RCX];
4179 		else
4180 			cpuid_value = vcpu->arch.regs[VCPU_REGS_RDX];
4181 
4182 		set_ghcb_msr_bits(svm, cpuid_value,
4183 				  GHCB_MSR_CPUID_VALUE_MASK,
4184 				  GHCB_MSR_CPUID_VALUE_POS);
4185 
4186 		set_ghcb_msr_bits(svm, GHCB_MSR_CPUID_RESP,
4187 				  GHCB_MSR_INFO_MASK,
4188 				  GHCB_MSR_INFO_POS);
4189 		break;
4190 	}
4191 	case GHCB_MSR_AP_RESET_HOLD_REQ:
4192 		svm->sev_es.ap_reset_hold_type = AP_RESET_HOLD_MSR_PROTO;
4193 		ret = kvm_emulate_ap_reset_hold(&svm->vcpu);
4194 
4195 		/*
4196 		 * Preset the result to a non-SIPI return and then only set
4197 		 * the result to non-zero when delivering a SIPI.
4198 		 */
4199 		set_ghcb_msr_bits(svm, 0,
4200 				  GHCB_MSR_AP_RESET_HOLD_RESULT_MASK,
4201 				  GHCB_MSR_AP_RESET_HOLD_RESULT_POS);
4202 
4203 		set_ghcb_msr_bits(svm, GHCB_MSR_AP_RESET_HOLD_RESP,
4204 				  GHCB_MSR_INFO_MASK,
4205 				  GHCB_MSR_INFO_POS);
4206 		break;
4207 	case GHCB_MSR_HV_FT_REQ:
4208 		set_ghcb_msr_bits(svm, GHCB_HV_FT_SUPPORTED,
4209 				  GHCB_MSR_HV_FT_MASK, GHCB_MSR_HV_FT_POS);
4210 		set_ghcb_msr_bits(svm, GHCB_MSR_HV_FT_RESP,
4211 				  GHCB_MSR_INFO_MASK, GHCB_MSR_INFO_POS);
4212 		break;
4213 	case GHCB_MSR_PREF_GPA_REQ:
4214 		if (!sev_snp_guest(vcpu->kvm))
4215 			goto out_terminate;
4216 
4217 		set_ghcb_msr_bits(svm, GHCB_MSR_PREF_GPA_NONE, GHCB_MSR_GPA_VALUE_MASK,
4218 				  GHCB_MSR_GPA_VALUE_POS);
4219 		set_ghcb_msr_bits(svm, GHCB_MSR_PREF_GPA_RESP, GHCB_MSR_INFO_MASK,
4220 				  GHCB_MSR_INFO_POS);
4221 		break;
4222 	case GHCB_MSR_REG_GPA_REQ: {
4223 		u64 gfn;
4224 
4225 		if (!sev_snp_guest(vcpu->kvm))
4226 			goto out_terminate;
4227 
4228 		gfn = get_ghcb_msr_bits(svm, GHCB_MSR_GPA_VALUE_MASK,
4229 					GHCB_MSR_GPA_VALUE_POS);
4230 
4231 		svm->sev_es.ghcb_registered_gpa = gfn_to_gpa(gfn);
4232 
4233 		set_ghcb_msr_bits(svm, gfn, GHCB_MSR_GPA_VALUE_MASK,
4234 				  GHCB_MSR_GPA_VALUE_POS);
4235 		set_ghcb_msr_bits(svm, GHCB_MSR_REG_GPA_RESP, GHCB_MSR_INFO_MASK,
4236 				  GHCB_MSR_INFO_POS);
4237 		break;
4238 	}
4239 	case GHCB_MSR_PSC_REQ:
4240 		if (!sev_snp_guest(vcpu->kvm))
4241 			goto out_terminate;
4242 
4243 		ret = snp_begin_psc_msr(svm, control->ghcb_gpa);
4244 		break;
4245 	case GHCB_MSR_TERM_REQ: {
4246 		u64 reason_set, reason_code;
4247 
4248 		reason_set = get_ghcb_msr_bits(svm,
4249 					       GHCB_MSR_TERM_REASON_SET_MASK,
4250 					       GHCB_MSR_TERM_REASON_SET_POS);
4251 		reason_code = get_ghcb_msr_bits(svm,
4252 						GHCB_MSR_TERM_REASON_MASK,
4253 						GHCB_MSR_TERM_REASON_POS);
4254 		pr_info("SEV-ES guest requested termination: %#llx:%#llx\n",
4255 			reason_set, reason_code);
4256 
4257 		goto out_terminate;
4258 	}
4259 	default:
4260 		/* Error, keep GHCB MSR value as-is */
4261 		break;
4262 	}
4263 
4264 	trace_kvm_vmgexit_msr_protocol_exit(svm->vcpu.vcpu_id,
4265 					    control->ghcb_gpa, ret);
4266 
4267 	return ret;
4268 
4269 out_terminate:
4270 	vcpu->run->exit_reason = KVM_EXIT_SYSTEM_EVENT;
4271 	vcpu->run->system_event.type = KVM_SYSTEM_EVENT_SEV_TERM;
4272 	vcpu->run->system_event.ndata = 1;
4273 	vcpu->run->system_event.data[0] = control->ghcb_gpa;
4274 
4275 	return 0;
4276 }
4277 
sev_handle_vmgexit(struct kvm_vcpu * vcpu)4278 int sev_handle_vmgexit(struct kvm_vcpu *vcpu)
4279 {
4280 	struct vcpu_svm *svm = to_svm(vcpu);
4281 	struct vmcb_control_area *control = &svm->vmcb->control;
4282 	u64 ghcb_gpa, exit_code;
4283 	int ret;
4284 
4285 	/* Validate the GHCB */
4286 	ghcb_gpa = control->ghcb_gpa;
4287 	if (ghcb_gpa & GHCB_MSR_INFO_MASK)
4288 		return sev_handle_vmgexit_msr_protocol(svm);
4289 
4290 	if (!ghcb_gpa) {
4291 		vcpu_unimpl(vcpu, "vmgexit: GHCB gpa is not set\n");
4292 
4293 		/* Without a GHCB, just return right back to the guest */
4294 		return 1;
4295 	}
4296 
4297 	if (kvm_vcpu_map(vcpu, ghcb_gpa >> PAGE_SHIFT, &svm->sev_es.ghcb_map)) {
4298 		/* Unable to map GHCB from guest */
4299 		vcpu_unimpl(vcpu, "vmgexit: error mapping GHCB [%#llx] from guest\n",
4300 			    ghcb_gpa);
4301 
4302 		/* Without a GHCB, just return right back to the guest */
4303 		return 1;
4304 	}
4305 
4306 	svm->sev_es.ghcb = svm->sev_es.ghcb_map.hva;
4307 
4308 	trace_kvm_vmgexit_enter(vcpu->vcpu_id, svm->sev_es.ghcb);
4309 
4310 	sev_es_sync_from_ghcb(svm);
4311 
4312 	/* SEV-SNP guest requires that the GHCB GPA must be registered */
4313 	if (sev_snp_guest(svm->vcpu.kvm) && !ghcb_gpa_is_registered(svm, ghcb_gpa)) {
4314 		vcpu_unimpl(&svm->vcpu, "vmgexit: GHCB GPA [%#llx] is not registered.\n", ghcb_gpa);
4315 		return -EINVAL;
4316 	}
4317 
4318 	ret = sev_es_validate_vmgexit(svm);
4319 	if (ret)
4320 		return ret;
4321 
4322 	ghcb_set_sw_exit_info_1(svm->sev_es.ghcb, 0);
4323 	ghcb_set_sw_exit_info_2(svm->sev_es.ghcb, 0);
4324 
4325 	exit_code = kvm_ghcb_get_sw_exit_code(control);
4326 	switch (exit_code) {
4327 	case SVM_VMGEXIT_MMIO_READ:
4328 		ret = setup_vmgexit_scratch(svm, true, control->exit_info_2);
4329 		if (ret)
4330 			break;
4331 
4332 		ret = kvm_sev_es_mmio_read(vcpu,
4333 					   control->exit_info_1,
4334 					   control->exit_info_2,
4335 					   svm->sev_es.ghcb_sa);
4336 		break;
4337 	case SVM_VMGEXIT_MMIO_WRITE:
4338 		ret = setup_vmgexit_scratch(svm, false, control->exit_info_2);
4339 		if (ret)
4340 			break;
4341 
4342 		ret = kvm_sev_es_mmio_write(vcpu,
4343 					    control->exit_info_1,
4344 					    control->exit_info_2,
4345 					    svm->sev_es.ghcb_sa);
4346 		break;
4347 	case SVM_VMGEXIT_NMI_COMPLETE:
4348 		++vcpu->stat.nmi_window_exits;
4349 		svm->nmi_masked = false;
4350 		kvm_make_request(KVM_REQ_EVENT, vcpu);
4351 		ret = 1;
4352 		break;
4353 	case SVM_VMGEXIT_AP_HLT_LOOP:
4354 		svm->sev_es.ap_reset_hold_type = AP_RESET_HOLD_NAE_EVENT;
4355 		ret = kvm_emulate_ap_reset_hold(vcpu);
4356 		break;
4357 	case SVM_VMGEXIT_AP_JUMP_TABLE: {
4358 		struct kvm_sev_info *sev = &to_kvm_svm(vcpu->kvm)->sev_info;
4359 
4360 		switch (control->exit_info_1) {
4361 		case 0:
4362 			/* Set AP jump table address */
4363 			sev->ap_jump_table = control->exit_info_2;
4364 			break;
4365 		case 1:
4366 			/* Get AP jump table address */
4367 			ghcb_set_sw_exit_info_2(svm->sev_es.ghcb, sev->ap_jump_table);
4368 			break;
4369 		default:
4370 			pr_err("svm: vmgexit: unsupported AP jump table request - exit_info_1=%#llx\n",
4371 			       control->exit_info_1);
4372 			ghcb_set_sw_exit_info_1(svm->sev_es.ghcb, 2);
4373 			ghcb_set_sw_exit_info_2(svm->sev_es.ghcb, GHCB_ERR_INVALID_INPUT);
4374 		}
4375 
4376 		ret = 1;
4377 		break;
4378 	}
4379 	case SVM_VMGEXIT_HV_FEATURES:
4380 		ghcb_set_sw_exit_info_2(svm->sev_es.ghcb, GHCB_HV_FT_SUPPORTED);
4381 
4382 		ret = 1;
4383 		break;
4384 	case SVM_VMGEXIT_TERM_REQUEST:
4385 		pr_info("SEV-ES guest requested termination: reason %#llx info %#llx\n",
4386 			control->exit_info_1, control->exit_info_2);
4387 		vcpu->run->exit_reason = KVM_EXIT_SYSTEM_EVENT;
4388 		vcpu->run->system_event.type = KVM_SYSTEM_EVENT_SEV_TERM;
4389 		vcpu->run->system_event.ndata = 1;
4390 		vcpu->run->system_event.data[0] = control->ghcb_gpa;
4391 		break;
4392 	case SVM_VMGEXIT_PSC:
4393 		ret = setup_vmgexit_scratch(svm, true, control->exit_info_2);
4394 		if (ret)
4395 			break;
4396 
4397 		ret = snp_begin_psc(svm, svm->sev_es.ghcb_sa);
4398 		break;
4399 	case SVM_VMGEXIT_AP_CREATION:
4400 		ret = sev_snp_ap_creation(svm);
4401 		if (ret) {
4402 			ghcb_set_sw_exit_info_1(svm->sev_es.ghcb, 2);
4403 			ghcb_set_sw_exit_info_2(svm->sev_es.ghcb, GHCB_ERR_INVALID_INPUT);
4404 		}
4405 
4406 		ret = 1;
4407 		break;
4408 	case SVM_VMGEXIT_GUEST_REQUEST:
4409 		ret = snp_handle_guest_req(svm, control->exit_info_1, control->exit_info_2);
4410 		break;
4411 	case SVM_VMGEXIT_EXT_GUEST_REQUEST:
4412 		ret = snp_handle_ext_guest_req(svm, control->exit_info_1, control->exit_info_2);
4413 		break;
4414 	case SVM_VMGEXIT_UNSUPPORTED_EVENT:
4415 		vcpu_unimpl(vcpu,
4416 			    "vmgexit: unsupported event - exit_info_1=%#llx, exit_info_2=%#llx\n",
4417 			    control->exit_info_1, control->exit_info_2);
4418 		ret = -EINVAL;
4419 		break;
4420 	default:
4421 		ret = svm_invoke_exit_handler(vcpu, exit_code);
4422 	}
4423 
4424 	return ret;
4425 }
4426 
sev_es_string_io(struct vcpu_svm * svm,int size,unsigned int port,int in)4427 int sev_es_string_io(struct vcpu_svm *svm, int size, unsigned int port, int in)
4428 {
4429 	int count;
4430 	int bytes;
4431 	int r;
4432 
4433 	if (svm->vmcb->control.exit_info_2 > INT_MAX)
4434 		return -EINVAL;
4435 
4436 	count = svm->vmcb->control.exit_info_2;
4437 	if (unlikely(check_mul_overflow(count, size, &bytes)))
4438 		return -EINVAL;
4439 
4440 	r = setup_vmgexit_scratch(svm, in, bytes);
4441 	if (r)
4442 		return r;
4443 
4444 	return kvm_sev_es_string_io(&svm->vcpu, size, port, svm->sev_es.ghcb_sa,
4445 				    count, in);
4446 }
4447 
sev_es_vcpu_after_set_cpuid(struct vcpu_svm * svm)4448 static void sev_es_vcpu_after_set_cpuid(struct vcpu_svm *svm)
4449 {
4450 	struct kvm_vcpu *vcpu = &svm->vcpu;
4451 
4452 	if (boot_cpu_has(X86_FEATURE_V_TSC_AUX)) {
4453 		bool v_tsc_aux = guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP) ||
4454 				 guest_cpuid_has(vcpu, X86_FEATURE_RDPID);
4455 
4456 		set_msr_interception(vcpu, svm->msrpm, MSR_TSC_AUX, v_tsc_aux, v_tsc_aux);
4457 	}
4458 
4459 	/*
4460 	 * For SEV-ES, accesses to MSR_IA32_XSS should not be intercepted if
4461 	 * the host/guest supports its use.
4462 	 *
4463 	 * guest_can_use() checks a number of requirements on the host/guest to
4464 	 * ensure that MSR_IA32_XSS is available, but it might report true even
4465 	 * if X86_FEATURE_XSAVES isn't configured in the guest to ensure host
4466 	 * MSR_IA32_XSS is always properly restored. For SEV-ES, it is better
4467 	 * to further check that the guest CPUID actually supports
4468 	 * X86_FEATURE_XSAVES so that accesses to MSR_IA32_XSS by misbehaved
4469 	 * guests will still get intercepted and caught in the normal
4470 	 * kvm_emulate_rdmsr()/kvm_emulated_wrmsr() paths.
4471 	 */
4472 	if (guest_can_use(vcpu, X86_FEATURE_XSAVES) &&
4473 	    guest_cpuid_has(vcpu, X86_FEATURE_XSAVES))
4474 		set_msr_interception(vcpu, svm->msrpm, MSR_IA32_XSS, 1, 1);
4475 	else
4476 		set_msr_interception(vcpu, svm->msrpm, MSR_IA32_XSS, 0, 0);
4477 }
4478 
sev_vcpu_after_set_cpuid(struct vcpu_svm * svm)4479 void sev_vcpu_after_set_cpuid(struct vcpu_svm *svm)
4480 {
4481 	struct kvm_vcpu *vcpu = &svm->vcpu;
4482 	struct kvm_cpuid_entry2 *best;
4483 
4484 	/* For sev guests, the memory encryption bit is not reserved in CR3.  */
4485 	best = kvm_find_cpuid_entry(vcpu, 0x8000001F);
4486 	if (best)
4487 		vcpu->arch.reserved_gpa_bits &= ~(1UL << (best->ebx & 0x3f));
4488 
4489 	if (sev_es_guest(svm->vcpu.kvm))
4490 		sev_es_vcpu_after_set_cpuid(svm);
4491 }
4492 
sev_es_init_vmcb(struct vcpu_svm * svm)4493 static void sev_es_init_vmcb(struct vcpu_svm *svm)
4494 {
4495 	struct vmcb *vmcb = svm->vmcb01.ptr;
4496 	struct kvm_vcpu *vcpu = &svm->vcpu;
4497 
4498 	svm->vmcb->control.nested_ctl |= SVM_NESTED_CTL_SEV_ES_ENABLE;
4499 
4500 	/*
4501 	 * An SEV-ES guest requires a VMSA area that is a separate from the
4502 	 * VMCB page. Do not include the encryption mask on the VMSA physical
4503 	 * address since hardware will access it using the guest key.  Note,
4504 	 * the VMSA will be NULL if this vCPU is the destination for intrahost
4505 	 * migration, and will be copied later.
4506 	 */
4507 	if (svm->sev_es.vmsa && !svm->sev_es.snp_has_guest_vmsa)
4508 		svm->vmcb->control.vmsa_pa = __pa(svm->sev_es.vmsa);
4509 
4510 	/* Can't intercept CR register access, HV can't modify CR registers */
4511 	svm_clr_intercept(svm, INTERCEPT_CR0_READ);
4512 	svm_clr_intercept(svm, INTERCEPT_CR4_READ);
4513 	svm_clr_intercept(svm, INTERCEPT_CR8_READ);
4514 	svm_clr_intercept(svm, INTERCEPT_CR0_WRITE);
4515 	svm_clr_intercept(svm, INTERCEPT_CR4_WRITE);
4516 	svm_clr_intercept(svm, INTERCEPT_CR8_WRITE);
4517 
4518 	svm_clr_intercept(svm, INTERCEPT_SELECTIVE_CR0);
4519 
4520 	/* Track EFER/CR register changes */
4521 	svm_set_intercept(svm, TRAP_EFER_WRITE);
4522 	svm_set_intercept(svm, TRAP_CR0_WRITE);
4523 	svm_set_intercept(svm, TRAP_CR4_WRITE);
4524 	svm_set_intercept(svm, TRAP_CR8_WRITE);
4525 
4526 	vmcb->control.intercepts[INTERCEPT_DR] = 0;
4527 	if (!sev_vcpu_has_debug_swap(svm)) {
4528 		vmcb_set_intercept(&vmcb->control, INTERCEPT_DR7_READ);
4529 		vmcb_set_intercept(&vmcb->control, INTERCEPT_DR7_WRITE);
4530 		recalc_intercepts(svm);
4531 	} else {
4532 		/*
4533 		 * Disable #DB intercept iff DebugSwap is enabled.  KVM doesn't
4534 		 * allow debugging SEV-ES guests, and enables DebugSwap iff
4535 		 * NO_NESTED_DATA_BP is supported, so there's no reason to
4536 		 * intercept #DB when DebugSwap is enabled.  For simplicity
4537 		 * with respect to guest debug, intercept #DB for other VMs
4538 		 * even if NO_NESTED_DATA_BP is supported, i.e. even if the
4539 		 * guest can't DoS the CPU with infinite #DB vectoring.
4540 		 */
4541 		clr_exception_intercept(svm, DB_VECTOR);
4542 	}
4543 
4544 	/* Can't intercept XSETBV, HV can't modify XCR0 directly */
4545 	svm_clr_intercept(svm, INTERCEPT_XSETBV);
4546 
4547 	/* Clear intercepts on selected MSRs */
4548 	set_msr_interception(vcpu, svm->msrpm, MSR_EFER, 1, 1);
4549 	set_msr_interception(vcpu, svm->msrpm, MSR_IA32_CR_PAT, 1, 1);
4550 }
4551 
sev_init_vmcb(struct vcpu_svm * svm)4552 void sev_init_vmcb(struct vcpu_svm *svm)
4553 {
4554 	svm->vmcb->control.nested_ctl |= SVM_NESTED_CTL_SEV_ENABLE;
4555 	clr_exception_intercept(svm, UD_VECTOR);
4556 
4557 	/*
4558 	 * Don't intercept #GP for SEV guests, e.g. for the VMware backdoor, as
4559 	 * KVM can't decrypt guest memory to decode the faulting instruction.
4560 	 */
4561 	clr_exception_intercept(svm, GP_VECTOR);
4562 
4563 	if (sev_es_guest(svm->vcpu.kvm))
4564 		sev_es_init_vmcb(svm);
4565 }
4566 
sev_es_vcpu_reset(struct vcpu_svm * svm)4567 void sev_es_vcpu_reset(struct vcpu_svm *svm)
4568 {
4569 	struct kvm_vcpu *vcpu = &svm->vcpu;
4570 	struct kvm_sev_info *sev = &to_kvm_svm(vcpu->kvm)->sev_info;
4571 
4572 	/*
4573 	 * Set the GHCB MSR value as per the GHCB specification when emulating
4574 	 * vCPU RESET for an SEV-ES guest.
4575 	 */
4576 	set_ghcb_msr(svm, GHCB_MSR_SEV_INFO((__u64)sev->ghcb_version,
4577 					    GHCB_VERSION_MIN,
4578 					    sev_enc_bit));
4579 
4580 	mutex_init(&svm->sev_es.snp_vmsa_mutex);
4581 }
4582 
sev_es_prepare_switch_to_guest(struct vcpu_svm * svm,struct sev_es_save_area * hostsa)4583 void sev_es_prepare_switch_to_guest(struct vcpu_svm *svm, struct sev_es_save_area *hostsa)
4584 {
4585 	struct kvm *kvm = svm->vcpu.kvm;
4586 
4587 	/*
4588 	 * All host state for SEV-ES guests is categorized into three swap types
4589 	 * based on how it is handled by hardware during a world switch:
4590 	 *
4591 	 * A: VMRUN:   Host state saved in host save area
4592 	 *    VMEXIT:  Host state loaded from host save area
4593 	 *
4594 	 * B: VMRUN:   Host state _NOT_ saved in host save area
4595 	 *    VMEXIT:  Host state loaded from host save area
4596 	 *
4597 	 * C: VMRUN:   Host state _NOT_ saved in host save area
4598 	 *    VMEXIT:  Host state initialized to default(reset) values
4599 	 *
4600 	 * Manually save type-B state, i.e. state that is loaded by VMEXIT but
4601 	 * isn't saved by VMRUN, that isn't already saved by VMSAVE (performed
4602 	 * by common SVM code).
4603 	 */
4604 	hostsa->xcr0 = kvm_host.xcr0;
4605 	hostsa->pkru = read_pkru();
4606 	hostsa->xss = kvm_host.xss;
4607 
4608 	/*
4609 	 * If DebugSwap is enabled, debug registers are loaded but NOT saved by
4610 	 * the CPU (Type-B). If DebugSwap is disabled/unsupported, the CPU does
4611 	 * not save or load debug registers.  Sadly, on CPUs without
4612 	 * ALLOWED_SEV_FEATURES, KVM can't prevent SNP guests from enabling
4613 	 * DebugSwap on secondary vCPUs without KVM's knowledge via "AP Create".
4614 	 * Save all registers if DebugSwap is supported to prevent host state
4615 	 * from being clobbered by a misbehaving guest.
4616 	 */
4617 	if (sev_vcpu_has_debug_swap(svm) ||
4618 	    (sev_snp_guest(kvm) && cpu_feature_enabled(X86_FEATURE_DEBUG_SWAP))) {
4619 		hostsa->dr0 = native_get_debugreg(0);
4620 		hostsa->dr1 = native_get_debugreg(1);
4621 		hostsa->dr2 = native_get_debugreg(2);
4622 		hostsa->dr3 = native_get_debugreg(3);
4623 		hostsa->dr0_addr_mask = amd_get_dr_addr_mask(0);
4624 		hostsa->dr1_addr_mask = amd_get_dr_addr_mask(1);
4625 		hostsa->dr2_addr_mask = amd_get_dr_addr_mask(2);
4626 		hostsa->dr3_addr_mask = amd_get_dr_addr_mask(3);
4627 	}
4628 }
4629 
sev_vcpu_deliver_sipi_vector(struct kvm_vcpu * vcpu,u8 vector)4630 void sev_vcpu_deliver_sipi_vector(struct kvm_vcpu *vcpu, u8 vector)
4631 {
4632 	struct vcpu_svm *svm = to_svm(vcpu);
4633 
4634 	/* First SIPI: Use the values as initially set by the VMM */
4635 	if (!svm->sev_es.received_first_sipi) {
4636 		svm->sev_es.received_first_sipi = true;
4637 		return;
4638 	}
4639 
4640 	/* Subsequent SIPI */
4641 	switch (svm->sev_es.ap_reset_hold_type) {
4642 	case AP_RESET_HOLD_NAE_EVENT:
4643 		/*
4644 		 * Return from an AP Reset Hold VMGEXIT, where the guest will
4645 		 * set the CS and RIP. Set SW_EXIT_INFO_2 to a non-zero value.
4646 		 */
4647 		ghcb_set_sw_exit_info_2(svm->sev_es.ghcb, 1);
4648 		break;
4649 	case AP_RESET_HOLD_MSR_PROTO:
4650 		/*
4651 		 * Return from an AP Reset Hold VMGEXIT, where the guest will
4652 		 * set the CS and RIP. Set GHCB data field to a non-zero value.
4653 		 */
4654 		set_ghcb_msr_bits(svm, 1,
4655 				  GHCB_MSR_AP_RESET_HOLD_RESULT_MASK,
4656 				  GHCB_MSR_AP_RESET_HOLD_RESULT_POS);
4657 
4658 		set_ghcb_msr_bits(svm, GHCB_MSR_AP_RESET_HOLD_RESP,
4659 				  GHCB_MSR_INFO_MASK,
4660 				  GHCB_MSR_INFO_POS);
4661 		break;
4662 	default:
4663 		break;
4664 	}
4665 }
4666 
snp_safe_alloc_page_node(int node,gfp_t gfp)4667 struct page *snp_safe_alloc_page_node(int node, gfp_t gfp)
4668 {
4669 	unsigned long pfn;
4670 	struct page *p;
4671 
4672 	if (!cc_platform_has(CC_ATTR_HOST_SEV_SNP))
4673 		return alloc_pages_node(node, gfp | __GFP_ZERO, 0);
4674 
4675 	/*
4676 	 * Allocate an SNP-safe page to workaround the SNP erratum where
4677 	 * the CPU will incorrectly signal an RMP violation #PF if a
4678 	 * hugepage (2MB or 1GB) collides with the RMP entry of a
4679 	 * 2MB-aligned VMCB, VMSA, or AVIC backing page.
4680 	 *
4681 	 * Allocate one extra page, choose a page which is not
4682 	 * 2MB-aligned, and free the other.
4683 	 */
4684 	p = alloc_pages_node(node, gfp | __GFP_ZERO, 1);
4685 	if (!p)
4686 		return NULL;
4687 
4688 	split_page(p, 1);
4689 
4690 	pfn = page_to_pfn(p);
4691 	if (IS_ALIGNED(pfn, PTRS_PER_PMD))
4692 		__free_page(p++);
4693 	else
4694 		__free_page(p + 1);
4695 
4696 	return p;
4697 }
4698 
sev_handle_rmp_fault(struct kvm_vcpu * vcpu,gpa_t gpa,u64 error_code)4699 void sev_handle_rmp_fault(struct kvm_vcpu *vcpu, gpa_t gpa, u64 error_code)
4700 {
4701 	struct kvm_memory_slot *slot;
4702 	struct kvm *kvm = vcpu->kvm;
4703 	int order, rmp_level, ret;
4704 	bool assigned;
4705 	kvm_pfn_t pfn;
4706 	gfn_t gfn;
4707 
4708 	gfn = gpa >> PAGE_SHIFT;
4709 
4710 	/*
4711 	 * The only time RMP faults occur for shared pages is when the guest is
4712 	 * triggering an RMP fault for an implicit page-state change from
4713 	 * shared->private. Implicit page-state changes are forwarded to
4714 	 * userspace via KVM_EXIT_MEMORY_FAULT events, however, so RMP faults
4715 	 * for shared pages should not end up here.
4716 	 */
4717 	if (!kvm_mem_is_private(kvm, gfn)) {
4718 		pr_warn_ratelimited("SEV: Unexpected RMP fault for non-private GPA 0x%llx\n",
4719 				    gpa);
4720 		return;
4721 	}
4722 
4723 	slot = gfn_to_memslot(kvm, gfn);
4724 	if (!kvm_slot_can_be_private(slot)) {
4725 		pr_warn_ratelimited("SEV: Unexpected RMP fault, non-private slot for GPA 0x%llx\n",
4726 				    gpa);
4727 		return;
4728 	}
4729 
4730 	ret = kvm_gmem_get_pfn(kvm, slot, gfn, &pfn, &order);
4731 	if (ret) {
4732 		pr_warn_ratelimited("SEV: Unexpected RMP fault, no backing page for private GPA 0x%llx\n",
4733 				    gpa);
4734 		return;
4735 	}
4736 
4737 	ret = snp_lookup_rmpentry(pfn, &assigned, &rmp_level);
4738 	if (ret || !assigned) {
4739 		pr_warn_ratelimited("SEV: Unexpected RMP fault, no assigned RMP entry found for GPA 0x%llx PFN 0x%llx error %d\n",
4740 				    gpa, pfn, ret);
4741 		goto out_no_trace;
4742 	}
4743 
4744 	/*
4745 	 * There are 2 cases where a PSMASH may be needed to resolve an #NPF
4746 	 * with PFERR_GUEST_RMP_BIT set:
4747 	 *
4748 	 * 1) RMPADJUST/PVALIDATE can trigger an #NPF with PFERR_GUEST_SIZEM
4749 	 *    bit set if the guest issues them with a smaller granularity than
4750 	 *    what is indicated by the page-size bit in the 2MB RMP entry for
4751 	 *    the PFN that backs the GPA.
4752 	 *
4753 	 * 2) Guest access via NPT can trigger an #NPF if the NPT mapping is
4754 	 *    smaller than what is indicated by the 2MB RMP entry for the PFN
4755 	 *    that backs the GPA.
4756 	 *
4757 	 * In both these cases, the corresponding 2M RMP entry needs to
4758 	 * be PSMASH'd to 512 4K RMP entries.  If the RMP entry is already
4759 	 * split into 4K RMP entries, then this is likely a spurious case which
4760 	 * can occur when there are concurrent accesses by the guest to a 2MB
4761 	 * GPA range that is backed by a 2MB-aligned PFN who's RMP entry is in
4762 	 * the process of being PMASH'd into 4K entries. These cases should
4763 	 * resolve automatically on subsequent accesses, so just ignore them
4764 	 * here.
4765 	 */
4766 	if (rmp_level == PG_LEVEL_4K)
4767 		goto out;
4768 
4769 	ret = snp_rmptable_psmash(pfn);
4770 	if (ret) {
4771 		/*
4772 		 * Look it up again. If it's 4K now then the PSMASH may have
4773 		 * raced with another process and the issue has already resolved
4774 		 * itself.
4775 		 */
4776 		if (!snp_lookup_rmpentry(pfn, &assigned, &rmp_level) &&
4777 		    assigned && rmp_level == PG_LEVEL_4K)
4778 			goto out;
4779 
4780 		pr_warn_ratelimited("SEV: Unable to split RMP entry for GPA 0x%llx PFN 0x%llx ret %d\n",
4781 				    gpa, pfn, ret);
4782 	}
4783 
4784 	kvm_zap_gfn_range(kvm, gfn, gfn + PTRS_PER_PMD);
4785 out:
4786 	trace_kvm_rmp_fault(vcpu, gpa, pfn, error_code, rmp_level, ret);
4787 out_no_trace:
4788 	put_page(pfn_to_page(pfn));
4789 }
4790 
is_pfn_range_shared(kvm_pfn_t start,kvm_pfn_t end)4791 static bool is_pfn_range_shared(kvm_pfn_t start, kvm_pfn_t end)
4792 {
4793 	kvm_pfn_t pfn = start;
4794 
4795 	while (pfn < end) {
4796 		int ret, rmp_level;
4797 		bool assigned;
4798 
4799 		ret = snp_lookup_rmpentry(pfn, &assigned, &rmp_level);
4800 		if (ret) {
4801 			pr_warn_ratelimited("SEV: Failed to retrieve RMP entry: PFN 0x%llx GFN start 0x%llx GFN end 0x%llx RMP level %d error %d\n",
4802 					    pfn, start, end, rmp_level, ret);
4803 			return false;
4804 		}
4805 
4806 		if (assigned) {
4807 			pr_debug("%s: overlap detected, PFN 0x%llx start 0x%llx end 0x%llx RMP level %d\n",
4808 				 __func__, pfn, start, end, rmp_level);
4809 			return false;
4810 		}
4811 
4812 		pfn++;
4813 	}
4814 
4815 	return true;
4816 }
4817 
max_level_for_order(int order)4818 static u8 max_level_for_order(int order)
4819 {
4820 	if (order >= KVM_HPAGE_GFN_SHIFT(PG_LEVEL_2M))
4821 		return PG_LEVEL_2M;
4822 
4823 	return PG_LEVEL_4K;
4824 }
4825 
is_large_rmp_possible(struct kvm * kvm,kvm_pfn_t pfn,int order)4826 static bool is_large_rmp_possible(struct kvm *kvm, kvm_pfn_t pfn, int order)
4827 {
4828 	kvm_pfn_t pfn_aligned = ALIGN_DOWN(pfn, PTRS_PER_PMD);
4829 
4830 	/*
4831 	 * If this is a large folio, and the entire 2M range containing the
4832 	 * PFN is currently shared, then the entire 2M-aligned range can be
4833 	 * set to private via a single 2M RMP entry.
4834 	 */
4835 	if (max_level_for_order(order) > PG_LEVEL_4K &&
4836 	    is_pfn_range_shared(pfn_aligned, pfn_aligned + PTRS_PER_PMD))
4837 		return true;
4838 
4839 	return false;
4840 }
4841 
sev_gmem_prepare(struct kvm * kvm,kvm_pfn_t pfn,gfn_t gfn,int max_order)4842 int sev_gmem_prepare(struct kvm *kvm, kvm_pfn_t pfn, gfn_t gfn, int max_order)
4843 {
4844 	struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
4845 	kvm_pfn_t pfn_aligned;
4846 	gfn_t gfn_aligned;
4847 	int level, rc;
4848 	bool assigned;
4849 
4850 	if (!sev_snp_guest(kvm))
4851 		return 0;
4852 
4853 	rc = snp_lookup_rmpentry(pfn, &assigned, &level);
4854 	if (rc) {
4855 		pr_err_ratelimited("SEV: Failed to look up RMP entry: GFN %llx PFN %llx error %d\n",
4856 				   gfn, pfn, rc);
4857 		return -ENOENT;
4858 	}
4859 
4860 	if (assigned) {
4861 		pr_debug("%s: already assigned: gfn %llx pfn %llx max_order %d level %d\n",
4862 			 __func__, gfn, pfn, max_order, level);
4863 		return 0;
4864 	}
4865 
4866 	if (is_large_rmp_possible(kvm, pfn, max_order)) {
4867 		level = PG_LEVEL_2M;
4868 		pfn_aligned = ALIGN_DOWN(pfn, PTRS_PER_PMD);
4869 		gfn_aligned = ALIGN_DOWN(gfn, PTRS_PER_PMD);
4870 	} else {
4871 		level = PG_LEVEL_4K;
4872 		pfn_aligned = pfn;
4873 		gfn_aligned = gfn;
4874 	}
4875 
4876 	rc = rmp_make_private(pfn_aligned, gfn_to_gpa(gfn_aligned), level, sev->asid, false);
4877 	if (rc) {
4878 		pr_err_ratelimited("SEV: Failed to update RMP entry: GFN %llx PFN %llx level %d error %d\n",
4879 				   gfn, pfn, level, rc);
4880 		return -EINVAL;
4881 	}
4882 
4883 	pr_debug("%s: updated: gfn %llx pfn %llx pfn_aligned %llx max_order %d level %d\n",
4884 		 __func__, gfn, pfn, pfn_aligned, max_order, level);
4885 
4886 	return 0;
4887 }
4888 
sev_gmem_invalidate(kvm_pfn_t start,kvm_pfn_t end)4889 void sev_gmem_invalidate(kvm_pfn_t start, kvm_pfn_t end)
4890 {
4891 	kvm_pfn_t pfn;
4892 
4893 	if (!cc_platform_has(CC_ATTR_HOST_SEV_SNP))
4894 		return;
4895 
4896 	pr_debug("%s: PFN start 0x%llx PFN end 0x%llx\n", __func__, start, end);
4897 
4898 	for (pfn = start; pfn < end;) {
4899 		bool use_2m_update = false;
4900 		int rc, rmp_level;
4901 		bool assigned;
4902 
4903 		rc = snp_lookup_rmpentry(pfn, &assigned, &rmp_level);
4904 		if (rc || !assigned)
4905 			goto next_pfn;
4906 
4907 		use_2m_update = IS_ALIGNED(pfn, PTRS_PER_PMD) &&
4908 				end >= (pfn + PTRS_PER_PMD) &&
4909 				rmp_level > PG_LEVEL_4K;
4910 
4911 		/*
4912 		 * If an unaligned PFN corresponds to a 2M region assigned as a
4913 		 * large page in the RMP table, PSMASH the region into individual
4914 		 * 4K RMP entries before attempting to convert a 4K sub-page.
4915 		 */
4916 		if (!use_2m_update && rmp_level > PG_LEVEL_4K) {
4917 			/*
4918 			 * This shouldn't fail, but if it does, report it, but
4919 			 * still try to update RMP entry to shared and pray this
4920 			 * was a spurious error that can be addressed later.
4921 			 */
4922 			rc = snp_rmptable_psmash(pfn);
4923 			WARN_ONCE(rc, "SEV: Failed to PSMASH RMP entry for PFN 0x%llx error %d\n",
4924 				  pfn, rc);
4925 		}
4926 
4927 		rc = rmp_make_shared(pfn, use_2m_update ? PG_LEVEL_2M : PG_LEVEL_4K);
4928 		if (WARN_ONCE(rc, "SEV: Failed to update RMP entry for PFN 0x%llx error %d\n",
4929 			      pfn, rc))
4930 			goto next_pfn;
4931 
4932 		/*
4933 		 * SEV-ES avoids host/guest cache coherency issues through
4934 		 * WBINVD hooks issued via MMU notifiers during run-time, and
4935 		 * KVM's VM destroy path at shutdown. Those MMU notifier events
4936 		 * don't cover gmem since there is no requirement to map pages
4937 		 * to a HVA in order to use them for a running guest. While the
4938 		 * shutdown path would still likely cover things for SNP guests,
4939 		 * userspace may also free gmem pages during run-time via
4940 		 * hole-punching operations on the guest_memfd, so flush the
4941 		 * cache entries for these pages before free'ing them back to
4942 		 * the host.
4943 		 */
4944 		clflush_cache_range(__va(pfn_to_hpa(pfn)),
4945 				    use_2m_update ? PMD_SIZE : PAGE_SIZE);
4946 next_pfn:
4947 		pfn += use_2m_update ? PTRS_PER_PMD : 1;
4948 		cond_resched();
4949 	}
4950 }
4951 
sev_private_max_mapping_level(struct kvm * kvm,kvm_pfn_t pfn)4952 int sev_private_max_mapping_level(struct kvm *kvm, kvm_pfn_t pfn)
4953 {
4954 	int level, rc;
4955 	bool assigned;
4956 
4957 	if (!sev_snp_guest(kvm))
4958 		return 0;
4959 
4960 	rc = snp_lookup_rmpentry(pfn, &assigned, &level);
4961 	if (rc || !assigned)
4962 		return PG_LEVEL_4K;
4963 
4964 	return level;
4965 }
4966