1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Kernel-based Virtual Machine driver for Linux
4 *
5 * AMD SVM-SEV support
6 *
7 * Copyright 2010 Red Hat, Inc. and/or its affiliates.
8 */
9
10 #include <linux/kvm_types.h>
11 #include <linux/kvm_host.h>
12 #include <linux/kernel.h>
13 #include <linux/highmem.h>
14 #include <linux/psp-sev.h>
15 #include <linux/pagemap.h>
16 #include <linux/swap.h>
17 #include <linux/misc_cgroup.h>
18 #include <linux/processor.h>
19 #include <linux/trace_events.h>
20
21 #include <asm/pkru.h>
22 #include <asm/trapnr.h>
23 #include <asm/fpu/xcr.h>
24
25 #include "mmu.h"
26 #include "x86.h"
27 #include "svm.h"
28 #include "svm_ops.h"
29 #include "cpuid.h"
30 #include "trace.h"
31
32 #ifndef CONFIG_KVM_AMD_SEV
33 /*
34 * When this config is not defined, SEV feature is not supported and APIs in
35 * this file are not used but this file still gets compiled into the KVM AMD
36 * module.
37 *
38 * We will not have MISC_CG_RES_SEV and MISC_CG_RES_SEV_ES entries in the enum
39 * misc_res_type {} defined in linux/misc_cgroup.h.
40 *
41 * Below macros allow compilation to succeed.
42 */
43 #define MISC_CG_RES_SEV MISC_CG_RES_TYPES
44 #define MISC_CG_RES_SEV_ES MISC_CG_RES_TYPES
45 #endif
46
47 #ifdef CONFIG_KVM_AMD_SEV
48 /* enable/disable SEV support */
49 static bool sev_enabled = true;
50 module_param_named(sev, sev_enabled, bool, 0444);
51
52 /* enable/disable SEV-ES support */
53 static bool sev_es_enabled = true;
54 module_param_named(sev_es, sev_es_enabled, bool, 0444);
55 #else
56 #define sev_enabled false
57 #define sev_es_enabled false
58 #endif /* CONFIG_KVM_AMD_SEV */
59
60 static u8 sev_enc_bit;
61 static DECLARE_RWSEM(sev_deactivate_lock);
62 static DEFINE_MUTEX(sev_bitmap_lock);
63 unsigned int max_sev_asid;
64 static unsigned int min_sev_asid;
65 static unsigned long sev_me_mask;
66 static unsigned int nr_asids;
67 static unsigned long *sev_asid_bitmap;
68 static unsigned long *sev_reclaim_asid_bitmap;
69
70 struct enc_region {
71 struct list_head list;
72 unsigned long npages;
73 struct page **pages;
74 unsigned long uaddr;
75 unsigned long size;
76 };
77
78 /* Called with the sev_bitmap_lock held, or on shutdown */
sev_flush_asids(int min_asid,int max_asid)79 static int sev_flush_asids(int min_asid, int max_asid)
80 {
81 int ret, asid, error = 0;
82
83 /* Check if there are any ASIDs to reclaim before performing a flush */
84 asid = find_next_bit(sev_reclaim_asid_bitmap, nr_asids, min_asid);
85 if (asid > max_asid)
86 return -EBUSY;
87
88 /*
89 * DEACTIVATE will clear the WBINVD indicator causing DF_FLUSH to fail,
90 * so it must be guarded.
91 */
92 down_write(&sev_deactivate_lock);
93
94 wbinvd_on_all_cpus();
95 ret = sev_guest_df_flush(&error);
96
97 up_write(&sev_deactivate_lock);
98
99 if (ret)
100 pr_err("SEV: DF_FLUSH failed, ret=%d, error=%#x\n", ret, error);
101
102 return ret;
103 }
104
is_mirroring_enc_context(struct kvm * kvm)105 static inline bool is_mirroring_enc_context(struct kvm *kvm)
106 {
107 return !!to_kvm_svm(kvm)->sev_info.enc_context_owner;
108 }
109
110 /* Must be called with the sev_bitmap_lock held */
__sev_recycle_asids(int min_asid,int max_asid)111 static bool __sev_recycle_asids(int min_asid, int max_asid)
112 {
113 if (sev_flush_asids(min_asid, max_asid))
114 return false;
115
116 /* The flush process will flush all reclaimable SEV and SEV-ES ASIDs */
117 bitmap_xor(sev_asid_bitmap, sev_asid_bitmap, sev_reclaim_asid_bitmap,
118 nr_asids);
119 bitmap_zero(sev_reclaim_asid_bitmap, nr_asids);
120
121 return true;
122 }
123
sev_misc_cg_try_charge(struct kvm_sev_info * sev)124 static int sev_misc_cg_try_charge(struct kvm_sev_info *sev)
125 {
126 enum misc_res_type type = sev->es_active ? MISC_CG_RES_SEV_ES : MISC_CG_RES_SEV;
127 return misc_cg_try_charge(type, sev->misc_cg, 1);
128 }
129
sev_misc_cg_uncharge(struct kvm_sev_info * sev)130 static void sev_misc_cg_uncharge(struct kvm_sev_info *sev)
131 {
132 enum misc_res_type type = sev->es_active ? MISC_CG_RES_SEV_ES : MISC_CG_RES_SEV;
133 misc_cg_uncharge(type, sev->misc_cg, 1);
134 }
135
sev_asid_new(struct kvm_sev_info * sev)136 static int sev_asid_new(struct kvm_sev_info *sev)
137 {
138 int asid, min_asid, max_asid, ret;
139 bool retry = true;
140
141 WARN_ON(sev->misc_cg);
142 sev->misc_cg = get_current_misc_cg();
143 ret = sev_misc_cg_try_charge(sev);
144 if (ret) {
145 put_misc_cg(sev->misc_cg);
146 sev->misc_cg = NULL;
147 return ret;
148 }
149
150 mutex_lock(&sev_bitmap_lock);
151
152 /*
153 * SEV-enabled guests must use asid from min_sev_asid to max_sev_asid.
154 * SEV-ES-enabled guest can use from 1 to min_sev_asid - 1.
155 */
156 min_asid = sev->es_active ? 1 : min_sev_asid;
157 max_asid = sev->es_active ? min_sev_asid - 1 : max_sev_asid;
158 again:
159 asid = find_next_zero_bit(sev_asid_bitmap, max_asid + 1, min_asid);
160 if (asid > max_asid) {
161 if (retry && __sev_recycle_asids(min_asid, max_asid)) {
162 retry = false;
163 goto again;
164 }
165 mutex_unlock(&sev_bitmap_lock);
166 ret = -EBUSY;
167 goto e_uncharge;
168 }
169
170 __set_bit(asid, sev_asid_bitmap);
171
172 mutex_unlock(&sev_bitmap_lock);
173
174 return asid;
175 e_uncharge:
176 sev_misc_cg_uncharge(sev);
177 put_misc_cg(sev->misc_cg);
178 sev->misc_cg = NULL;
179 return ret;
180 }
181
sev_get_asid(struct kvm * kvm)182 static int sev_get_asid(struct kvm *kvm)
183 {
184 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
185
186 return sev->asid;
187 }
188
sev_asid_free(struct kvm_sev_info * sev)189 static void sev_asid_free(struct kvm_sev_info *sev)
190 {
191 struct svm_cpu_data *sd;
192 int cpu;
193
194 mutex_lock(&sev_bitmap_lock);
195
196 __set_bit(sev->asid, sev_reclaim_asid_bitmap);
197
198 for_each_possible_cpu(cpu) {
199 sd = per_cpu(svm_data, cpu);
200 sd->sev_vmcbs[sev->asid] = NULL;
201 }
202
203 mutex_unlock(&sev_bitmap_lock);
204
205 sev_misc_cg_uncharge(sev);
206 put_misc_cg(sev->misc_cg);
207 sev->misc_cg = NULL;
208 }
209
sev_decommission(unsigned int handle)210 static void sev_decommission(unsigned int handle)
211 {
212 struct sev_data_decommission decommission;
213
214 if (!handle)
215 return;
216
217 decommission.handle = handle;
218 sev_guest_decommission(&decommission, NULL);
219 }
220
sev_unbind_asid(struct kvm * kvm,unsigned int handle)221 static void sev_unbind_asid(struct kvm *kvm, unsigned int handle)
222 {
223 struct sev_data_deactivate deactivate;
224
225 if (!handle)
226 return;
227
228 deactivate.handle = handle;
229
230 /* Guard DEACTIVATE against WBINVD/DF_FLUSH used in ASID recycling */
231 down_read(&sev_deactivate_lock);
232 sev_guest_deactivate(&deactivate, NULL);
233 up_read(&sev_deactivate_lock);
234
235 sev_decommission(handle);
236 }
237
sev_guest_init(struct kvm * kvm,struct kvm_sev_cmd * argp)238 static int sev_guest_init(struct kvm *kvm, struct kvm_sev_cmd *argp)
239 {
240 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
241 int asid, ret;
242
243 if (kvm->created_vcpus)
244 return -EINVAL;
245
246 ret = -EBUSY;
247 if (unlikely(sev->active))
248 return ret;
249
250 sev->active = true;
251 sev->es_active = argp->id == KVM_SEV_ES_INIT;
252 asid = sev_asid_new(sev);
253 if (asid < 0)
254 goto e_no_asid;
255 sev->asid = asid;
256
257 ret = sev_platform_init(&argp->error);
258 if (ret)
259 goto e_free;
260
261 INIT_LIST_HEAD(&sev->regions_list);
262 INIT_LIST_HEAD(&sev->mirror_vms);
263
264 kvm_set_apicv_inhibit(kvm, APICV_INHIBIT_REASON_SEV);
265
266 return 0;
267
268 e_free:
269 sev_asid_free(sev);
270 sev->asid = 0;
271 e_no_asid:
272 sev->es_active = false;
273 sev->active = false;
274 return ret;
275 }
276
sev_bind_asid(struct kvm * kvm,unsigned int handle,int * error)277 static int sev_bind_asid(struct kvm *kvm, unsigned int handle, int *error)
278 {
279 struct sev_data_activate activate;
280 int asid = sev_get_asid(kvm);
281 int ret;
282
283 /* activate ASID on the given handle */
284 activate.handle = handle;
285 activate.asid = asid;
286 ret = sev_guest_activate(&activate, error);
287
288 return ret;
289 }
290
__sev_issue_cmd(int fd,int id,void * data,int * error)291 static int __sev_issue_cmd(int fd, int id, void *data, int *error)
292 {
293 struct fd f;
294 int ret;
295
296 f = fdget(fd);
297 if (!f.file)
298 return -EBADF;
299
300 ret = sev_issue_cmd_external_user(f.file, id, data, error);
301
302 fdput(f);
303 return ret;
304 }
305
sev_issue_cmd(struct kvm * kvm,int id,void * data,int * error)306 static int sev_issue_cmd(struct kvm *kvm, int id, void *data, int *error)
307 {
308 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
309
310 return __sev_issue_cmd(sev->fd, id, data, error);
311 }
312
sev_launch_start(struct kvm * kvm,struct kvm_sev_cmd * argp)313 static int sev_launch_start(struct kvm *kvm, struct kvm_sev_cmd *argp)
314 {
315 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
316 struct sev_data_launch_start start;
317 struct kvm_sev_launch_start params;
318 void *dh_blob, *session_blob;
319 int *error = &argp->error;
320 int ret;
321
322 if (!sev_guest(kvm))
323 return -ENOTTY;
324
325 if (copy_from_user(¶ms, (void __user *)(uintptr_t)argp->data, sizeof(params)))
326 return -EFAULT;
327
328 memset(&start, 0, sizeof(start));
329
330 dh_blob = NULL;
331 if (params.dh_uaddr) {
332 dh_blob = psp_copy_user_blob(params.dh_uaddr, params.dh_len);
333 if (IS_ERR(dh_blob))
334 return PTR_ERR(dh_blob);
335
336 start.dh_cert_address = __sme_set(__pa(dh_blob));
337 start.dh_cert_len = params.dh_len;
338 }
339
340 session_blob = NULL;
341 if (params.session_uaddr) {
342 session_blob = psp_copy_user_blob(params.session_uaddr, params.session_len);
343 if (IS_ERR(session_blob)) {
344 ret = PTR_ERR(session_blob);
345 goto e_free_dh;
346 }
347
348 start.session_address = __sme_set(__pa(session_blob));
349 start.session_len = params.session_len;
350 }
351
352 start.handle = params.handle;
353 start.policy = params.policy;
354
355 /* create memory encryption context */
356 ret = __sev_issue_cmd(argp->sev_fd, SEV_CMD_LAUNCH_START, &start, error);
357 if (ret)
358 goto e_free_session;
359
360 /* Bind ASID to this guest */
361 ret = sev_bind_asid(kvm, start.handle, error);
362 if (ret) {
363 sev_decommission(start.handle);
364 goto e_free_session;
365 }
366
367 /* return handle to userspace */
368 params.handle = start.handle;
369 if (copy_to_user((void __user *)(uintptr_t)argp->data, ¶ms, sizeof(params))) {
370 sev_unbind_asid(kvm, start.handle);
371 ret = -EFAULT;
372 goto e_free_session;
373 }
374
375 sev->handle = start.handle;
376 sev->fd = argp->sev_fd;
377
378 e_free_session:
379 kfree(session_blob);
380 e_free_dh:
381 kfree(dh_blob);
382 return ret;
383 }
384
sev_pin_memory(struct kvm * kvm,unsigned long uaddr,unsigned long ulen,unsigned long * n,int write)385 static struct page **sev_pin_memory(struct kvm *kvm, unsigned long uaddr,
386 unsigned long ulen, unsigned long *n,
387 int write)
388 {
389 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
390 unsigned long npages, size;
391 int npinned;
392 unsigned long locked, lock_limit;
393 struct page **pages;
394 unsigned long first, last;
395 int ret;
396
397 lockdep_assert_held(&kvm->lock);
398
399 if (ulen == 0 || uaddr + ulen < uaddr)
400 return ERR_PTR(-EINVAL);
401
402 /* Calculate number of pages. */
403 first = (uaddr & PAGE_MASK) >> PAGE_SHIFT;
404 last = ((uaddr + ulen - 1) & PAGE_MASK) >> PAGE_SHIFT;
405 npages = (last - first + 1);
406
407 locked = sev->pages_locked + npages;
408 lock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
409 if (locked > lock_limit && !capable(CAP_IPC_LOCK)) {
410 pr_err("SEV: %lu locked pages exceed the lock limit of %lu.\n", locked, lock_limit);
411 return ERR_PTR(-ENOMEM);
412 }
413
414 if (WARN_ON_ONCE(npages > INT_MAX))
415 return ERR_PTR(-EINVAL);
416
417 /* Avoid using vmalloc for smaller buffers. */
418 size = npages * sizeof(struct page *);
419 if (size > PAGE_SIZE)
420 pages = __vmalloc(size, GFP_KERNEL_ACCOUNT | __GFP_ZERO);
421 else
422 pages = kmalloc(size, GFP_KERNEL_ACCOUNT);
423
424 if (!pages)
425 return ERR_PTR(-ENOMEM);
426
427 /* Pin the user virtual address. */
428 npinned = pin_user_pages_fast(uaddr, npages, write ? FOLL_WRITE : 0, pages);
429 if (npinned != npages) {
430 pr_err("SEV: Failure locking %lu pages.\n", npages);
431 ret = -ENOMEM;
432 goto err;
433 }
434
435 *n = npages;
436 sev->pages_locked = locked;
437
438 return pages;
439
440 err:
441 if (npinned > 0)
442 unpin_user_pages(pages, npinned);
443
444 kvfree(pages);
445 return ERR_PTR(ret);
446 }
447
sev_unpin_memory(struct kvm * kvm,struct page ** pages,unsigned long npages)448 static void sev_unpin_memory(struct kvm *kvm, struct page **pages,
449 unsigned long npages)
450 {
451 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
452
453 unpin_user_pages(pages, npages);
454 kvfree(pages);
455 sev->pages_locked -= npages;
456 }
457
sev_clflush_pages(struct page * pages[],unsigned long npages)458 static void sev_clflush_pages(struct page *pages[], unsigned long npages)
459 {
460 uint8_t *page_virtual;
461 unsigned long i;
462
463 if (this_cpu_has(X86_FEATURE_SME_COHERENT) || npages == 0 ||
464 pages == NULL)
465 return;
466
467 for (i = 0; i < npages; i++) {
468 page_virtual = kmap_atomic(pages[i]);
469 clflush_cache_range(page_virtual, PAGE_SIZE);
470 kunmap_atomic(page_virtual);
471 cond_resched();
472 }
473 }
474
get_num_contig_pages(unsigned long idx,struct page ** inpages,unsigned long npages)475 static unsigned long get_num_contig_pages(unsigned long idx,
476 struct page **inpages, unsigned long npages)
477 {
478 unsigned long paddr, next_paddr;
479 unsigned long i = idx + 1, pages = 1;
480
481 /* find the number of contiguous pages starting from idx */
482 paddr = __sme_page_pa(inpages[idx]);
483 while (i < npages) {
484 next_paddr = __sme_page_pa(inpages[i++]);
485 if ((paddr + PAGE_SIZE) == next_paddr) {
486 pages++;
487 paddr = next_paddr;
488 continue;
489 }
490 break;
491 }
492
493 return pages;
494 }
495
sev_launch_update_data(struct kvm * kvm,struct kvm_sev_cmd * argp)496 static int sev_launch_update_data(struct kvm *kvm, struct kvm_sev_cmd *argp)
497 {
498 unsigned long vaddr, vaddr_end, next_vaddr, npages, pages, size, i;
499 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
500 struct kvm_sev_launch_update_data params;
501 struct sev_data_launch_update_data data;
502 struct page **inpages;
503 int ret;
504
505 if (!sev_guest(kvm))
506 return -ENOTTY;
507
508 if (copy_from_user(¶ms, (void __user *)(uintptr_t)argp->data, sizeof(params)))
509 return -EFAULT;
510
511 vaddr = params.uaddr;
512 size = params.len;
513 vaddr_end = vaddr + size;
514
515 /* Lock the user memory. */
516 inpages = sev_pin_memory(kvm, vaddr, size, &npages, 1);
517 if (IS_ERR(inpages))
518 return PTR_ERR(inpages);
519
520 /*
521 * Flush (on non-coherent CPUs) before LAUNCH_UPDATE encrypts pages in
522 * place; the cache may contain the data that was written unencrypted.
523 */
524 sev_clflush_pages(inpages, npages);
525
526 data.reserved = 0;
527 data.handle = sev->handle;
528
529 for (i = 0; vaddr < vaddr_end; vaddr = next_vaddr, i += pages) {
530 int offset, len;
531
532 /*
533 * If the user buffer is not page-aligned, calculate the offset
534 * within the page.
535 */
536 offset = vaddr & (PAGE_SIZE - 1);
537
538 /* Calculate the number of pages that can be encrypted in one go. */
539 pages = get_num_contig_pages(i, inpages, npages);
540
541 len = min_t(size_t, ((pages * PAGE_SIZE) - offset), size);
542
543 data.len = len;
544 data.address = __sme_page_pa(inpages[i]) + offset;
545 ret = sev_issue_cmd(kvm, SEV_CMD_LAUNCH_UPDATE_DATA, &data, &argp->error);
546 if (ret)
547 goto e_unpin;
548
549 size -= len;
550 next_vaddr = vaddr + len;
551 }
552
553 e_unpin:
554 /* content of memory is updated, mark pages dirty */
555 for (i = 0; i < npages; i++) {
556 set_page_dirty_lock(inpages[i]);
557 mark_page_accessed(inpages[i]);
558 }
559 /* unlock the user pages */
560 sev_unpin_memory(kvm, inpages, npages);
561 return ret;
562 }
563
sev_es_sync_vmsa(struct vcpu_svm * svm)564 static int sev_es_sync_vmsa(struct vcpu_svm *svm)
565 {
566 struct sev_es_save_area *save = svm->sev_es.vmsa;
567
568 /* Check some debug related fields before encrypting the VMSA */
569 if (svm->vcpu.guest_debug || (svm->vmcb->save.dr7 & ~DR7_FIXED_1))
570 return -EINVAL;
571
572 /*
573 * SEV-ES will use a VMSA that is pointed to by the VMCB, not
574 * the traditional VMSA that is part of the VMCB. Copy the
575 * traditional VMSA as it has been built so far (in prep
576 * for LAUNCH_UPDATE_VMSA) to be the initial SEV-ES state.
577 */
578 memcpy(save, &svm->vmcb->save, sizeof(svm->vmcb->save));
579
580 /* Sync registgers */
581 save->rax = svm->vcpu.arch.regs[VCPU_REGS_RAX];
582 save->rbx = svm->vcpu.arch.regs[VCPU_REGS_RBX];
583 save->rcx = svm->vcpu.arch.regs[VCPU_REGS_RCX];
584 save->rdx = svm->vcpu.arch.regs[VCPU_REGS_RDX];
585 save->rsp = svm->vcpu.arch.regs[VCPU_REGS_RSP];
586 save->rbp = svm->vcpu.arch.regs[VCPU_REGS_RBP];
587 save->rsi = svm->vcpu.arch.regs[VCPU_REGS_RSI];
588 save->rdi = svm->vcpu.arch.regs[VCPU_REGS_RDI];
589 #ifdef CONFIG_X86_64
590 save->r8 = svm->vcpu.arch.regs[VCPU_REGS_R8];
591 save->r9 = svm->vcpu.arch.regs[VCPU_REGS_R9];
592 save->r10 = svm->vcpu.arch.regs[VCPU_REGS_R10];
593 save->r11 = svm->vcpu.arch.regs[VCPU_REGS_R11];
594 save->r12 = svm->vcpu.arch.regs[VCPU_REGS_R12];
595 save->r13 = svm->vcpu.arch.regs[VCPU_REGS_R13];
596 save->r14 = svm->vcpu.arch.regs[VCPU_REGS_R14];
597 save->r15 = svm->vcpu.arch.regs[VCPU_REGS_R15];
598 #endif
599 save->rip = svm->vcpu.arch.regs[VCPU_REGS_RIP];
600
601 /* Sync some non-GPR registers before encrypting */
602 save->xcr0 = svm->vcpu.arch.xcr0;
603 save->pkru = svm->vcpu.arch.pkru;
604 save->xss = svm->vcpu.arch.ia32_xss;
605 save->dr6 = svm->vcpu.arch.dr6;
606
607 return 0;
608 }
609
__sev_launch_update_vmsa(struct kvm * kvm,struct kvm_vcpu * vcpu,int * error)610 static int __sev_launch_update_vmsa(struct kvm *kvm, struct kvm_vcpu *vcpu,
611 int *error)
612 {
613 struct sev_data_launch_update_vmsa vmsa;
614 struct vcpu_svm *svm = to_svm(vcpu);
615 int ret;
616
617 /* Perform some pre-encryption checks against the VMSA */
618 ret = sev_es_sync_vmsa(svm);
619 if (ret)
620 return ret;
621
622 /*
623 * The LAUNCH_UPDATE_VMSA command will perform in-place encryption of
624 * the VMSA memory content (i.e it will write the same memory region
625 * with the guest's key), so invalidate it first.
626 */
627 clflush_cache_range(svm->sev_es.vmsa, PAGE_SIZE);
628
629 vmsa.reserved = 0;
630 vmsa.handle = to_kvm_svm(kvm)->sev_info.handle;
631 vmsa.address = __sme_pa(svm->sev_es.vmsa);
632 vmsa.len = PAGE_SIZE;
633 ret = sev_issue_cmd(kvm, SEV_CMD_LAUNCH_UPDATE_VMSA, &vmsa, error);
634 if (ret)
635 return ret;
636
637 vcpu->arch.guest_state_protected = true;
638 return 0;
639 }
640
sev_launch_update_vmsa(struct kvm * kvm,struct kvm_sev_cmd * argp)641 static int sev_launch_update_vmsa(struct kvm *kvm, struct kvm_sev_cmd *argp)
642 {
643 struct kvm_vcpu *vcpu;
644 unsigned long i;
645 int ret;
646
647 if (!sev_es_guest(kvm))
648 return -ENOTTY;
649
650 kvm_for_each_vcpu(i, vcpu, kvm) {
651 ret = mutex_lock_killable(&vcpu->mutex);
652 if (ret)
653 return ret;
654
655 ret = __sev_launch_update_vmsa(kvm, vcpu, &argp->error);
656
657 mutex_unlock(&vcpu->mutex);
658 if (ret)
659 return ret;
660 }
661
662 return 0;
663 }
664
sev_launch_measure(struct kvm * kvm,struct kvm_sev_cmd * argp)665 static int sev_launch_measure(struct kvm *kvm, struct kvm_sev_cmd *argp)
666 {
667 void __user *measure = (void __user *)(uintptr_t)argp->data;
668 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
669 struct sev_data_launch_measure data;
670 struct kvm_sev_launch_measure params;
671 void __user *p = NULL;
672 void *blob = NULL;
673 int ret;
674
675 if (!sev_guest(kvm))
676 return -ENOTTY;
677
678 if (copy_from_user(¶ms, measure, sizeof(params)))
679 return -EFAULT;
680
681 memset(&data, 0, sizeof(data));
682
683 /* User wants to query the blob length */
684 if (!params.len)
685 goto cmd;
686
687 p = (void __user *)(uintptr_t)params.uaddr;
688 if (p) {
689 if (params.len > SEV_FW_BLOB_MAX_SIZE)
690 return -EINVAL;
691
692 blob = kzalloc(params.len, GFP_KERNEL_ACCOUNT);
693 if (!blob)
694 return -ENOMEM;
695
696 data.address = __psp_pa(blob);
697 data.len = params.len;
698 }
699
700 cmd:
701 data.handle = sev->handle;
702 ret = sev_issue_cmd(kvm, SEV_CMD_LAUNCH_MEASURE, &data, &argp->error);
703
704 /*
705 * If we query the session length, FW responded with expected data.
706 */
707 if (!params.len)
708 goto done;
709
710 if (ret)
711 goto e_free_blob;
712
713 if (blob) {
714 if (copy_to_user(p, blob, params.len))
715 ret = -EFAULT;
716 }
717
718 done:
719 params.len = data.len;
720 if (copy_to_user(measure, ¶ms, sizeof(params)))
721 ret = -EFAULT;
722 e_free_blob:
723 kfree(blob);
724 return ret;
725 }
726
sev_launch_finish(struct kvm * kvm,struct kvm_sev_cmd * argp)727 static int sev_launch_finish(struct kvm *kvm, struct kvm_sev_cmd *argp)
728 {
729 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
730 struct sev_data_launch_finish data;
731
732 if (!sev_guest(kvm))
733 return -ENOTTY;
734
735 data.handle = sev->handle;
736 return sev_issue_cmd(kvm, SEV_CMD_LAUNCH_FINISH, &data, &argp->error);
737 }
738
sev_guest_status(struct kvm * kvm,struct kvm_sev_cmd * argp)739 static int sev_guest_status(struct kvm *kvm, struct kvm_sev_cmd *argp)
740 {
741 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
742 struct kvm_sev_guest_status params;
743 struct sev_data_guest_status data;
744 int ret;
745
746 if (!sev_guest(kvm))
747 return -ENOTTY;
748
749 memset(&data, 0, sizeof(data));
750
751 data.handle = sev->handle;
752 ret = sev_issue_cmd(kvm, SEV_CMD_GUEST_STATUS, &data, &argp->error);
753 if (ret)
754 return ret;
755
756 params.policy = data.policy;
757 params.state = data.state;
758 params.handle = data.handle;
759
760 if (copy_to_user((void __user *)(uintptr_t)argp->data, ¶ms, sizeof(params)))
761 ret = -EFAULT;
762
763 return ret;
764 }
765
__sev_issue_dbg_cmd(struct kvm * kvm,unsigned long src,unsigned long dst,int size,int * error,bool enc)766 static int __sev_issue_dbg_cmd(struct kvm *kvm, unsigned long src,
767 unsigned long dst, int size,
768 int *error, bool enc)
769 {
770 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
771 struct sev_data_dbg data;
772
773 data.reserved = 0;
774 data.handle = sev->handle;
775 data.dst_addr = dst;
776 data.src_addr = src;
777 data.len = size;
778
779 return sev_issue_cmd(kvm,
780 enc ? SEV_CMD_DBG_ENCRYPT : SEV_CMD_DBG_DECRYPT,
781 &data, error);
782 }
783
__sev_dbg_decrypt(struct kvm * kvm,unsigned long src_paddr,unsigned long dst_paddr,int sz,int * err)784 static int __sev_dbg_decrypt(struct kvm *kvm, unsigned long src_paddr,
785 unsigned long dst_paddr, int sz, int *err)
786 {
787 int offset;
788
789 /*
790 * Its safe to read more than we are asked, caller should ensure that
791 * destination has enough space.
792 */
793 offset = src_paddr & 15;
794 src_paddr = round_down(src_paddr, 16);
795 sz = round_up(sz + offset, 16);
796
797 return __sev_issue_dbg_cmd(kvm, src_paddr, dst_paddr, sz, err, false);
798 }
799
__sev_dbg_decrypt_user(struct kvm * kvm,unsigned long paddr,void __user * dst_uaddr,unsigned long dst_paddr,int size,int * err)800 static int __sev_dbg_decrypt_user(struct kvm *kvm, unsigned long paddr,
801 void __user *dst_uaddr,
802 unsigned long dst_paddr,
803 int size, int *err)
804 {
805 struct page *tpage = NULL;
806 int ret, offset;
807
808 /* if inputs are not 16-byte then use intermediate buffer */
809 if (!IS_ALIGNED(dst_paddr, 16) ||
810 !IS_ALIGNED(paddr, 16) ||
811 !IS_ALIGNED(size, 16)) {
812 tpage = (void *)alloc_page(GFP_KERNEL | __GFP_ZERO);
813 if (!tpage)
814 return -ENOMEM;
815
816 dst_paddr = __sme_page_pa(tpage);
817 }
818
819 ret = __sev_dbg_decrypt(kvm, paddr, dst_paddr, size, err);
820 if (ret)
821 goto e_free;
822
823 if (tpage) {
824 offset = paddr & 15;
825 if (copy_to_user(dst_uaddr, page_address(tpage) + offset, size))
826 ret = -EFAULT;
827 }
828
829 e_free:
830 if (tpage)
831 __free_page(tpage);
832
833 return ret;
834 }
835
__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)836 static int __sev_dbg_encrypt_user(struct kvm *kvm, unsigned long paddr,
837 void __user *vaddr,
838 unsigned long dst_paddr,
839 void __user *dst_vaddr,
840 int size, int *error)
841 {
842 struct page *src_tpage = NULL;
843 struct page *dst_tpage = NULL;
844 int ret, len = size;
845
846 /* If source buffer is not aligned then use an intermediate buffer */
847 if (!IS_ALIGNED((unsigned long)vaddr, 16)) {
848 src_tpage = alloc_page(GFP_KERNEL_ACCOUNT);
849 if (!src_tpage)
850 return -ENOMEM;
851
852 if (copy_from_user(page_address(src_tpage), vaddr, size)) {
853 __free_page(src_tpage);
854 return -EFAULT;
855 }
856
857 paddr = __sme_page_pa(src_tpage);
858 }
859
860 /*
861 * If destination buffer or length is not aligned then do read-modify-write:
862 * - decrypt destination in an intermediate buffer
863 * - copy the source buffer in an intermediate buffer
864 * - use the intermediate buffer as source buffer
865 */
866 if (!IS_ALIGNED((unsigned long)dst_vaddr, 16) || !IS_ALIGNED(size, 16)) {
867 int dst_offset;
868
869 dst_tpage = alloc_page(GFP_KERNEL_ACCOUNT);
870 if (!dst_tpage) {
871 ret = -ENOMEM;
872 goto e_free;
873 }
874
875 ret = __sev_dbg_decrypt(kvm, dst_paddr,
876 __sme_page_pa(dst_tpage), size, error);
877 if (ret)
878 goto e_free;
879
880 /*
881 * If source is kernel buffer then use memcpy() otherwise
882 * copy_from_user().
883 */
884 dst_offset = dst_paddr & 15;
885
886 if (src_tpage)
887 memcpy(page_address(dst_tpage) + dst_offset,
888 page_address(src_tpage), size);
889 else {
890 if (copy_from_user(page_address(dst_tpage) + dst_offset,
891 vaddr, size)) {
892 ret = -EFAULT;
893 goto e_free;
894 }
895 }
896
897 paddr = __sme_page_pa(dst_tpage);
898 dst_paddr = round_down(dst_paddr, 16);
899 len = round_up(size, 16);
900 }
901
902 ret = __sev_issue_dbg_cmd(kvm, paddr, dst_paddr, len, error, true);
903
904 e_free:
905 if (src_tpage)
906 __free_page(src_tpage);
907 if (dst_tpage)
908 __free_page(dst_tpage);
909 return ret;
910 }
911
sev_dbg_crypt(struct kvm * kvm,struct kvm_sev_cmd * argp,bool dec)912 static int sev_dbg_crypt(struct kvm *kvm, struct kvm_sev_cmd *argp, bool dec)
913 {
914 unsigned long vaddr, vaddr_end, next_vaddr;
915 unsigned long dst_vaddr;
916 struct page **src_p, **dst_p;
917 struct kvm_sev_dbg debug;
918 unsigned long n;
919 unsigned int size;
920 int ret;
921
922 if (!sev_guest(kvm))
923 return -ENOTTY;
924
925 if (copy_from_user(&debug, (void __user *)(uintptr_t)argp->data, sizeof(debug)))
926 return -EFAULT;
927
928 if (!debug.len || debug.src_uaddr + debug.len < debug.src_uaddr)
929 return -EINVAL;
930 if (!debug.dst_uaddr)
931 return -EINVAL;
932
933 vaddr = debug.src_uaddr;
934 size = debug.len;
935 vaddr_end = vaddr + size;
936 dst_vaddr = debug.dst_uaddr;
937
938 for (; vaddr < vaddr_end; vaddr = next_vaddr) {
939 int len, s_off, d_off;
940
941 /* lock userspace source and destination page */
942 src_p = sev_pin_memory(kvm, vaddr & PAGE_MASK, PAGE_SIZE, &n, 0);
943 if (IS_ERR(src_p))
944 return PTR_ERR(src_p);
945
946 dst_p = sev_pin_memory(kvm, dst_vaddr & PAGE_MASK, PAGE_SIZE, &n, 1);
947 if (IS_ERR(dst_p)) {
948 sev_unpin_memory(kvm, src_p, n);
949 return PTR_ERR(dst_p);
950 }
951
952 /*
953 * Flush (on non-coherent CPUs) before DBG_{DE,EN}CRYPT read or modify
954 * the pages; flush the destination too so that future accesses do not
955 * see stale data.
956 */
957 sev_clflush_pages(src_p, 1);
958 sev_clflush_pages(dst_p, 1);
959
960 /*
961 * Since user buffer may not be page aligned, calculate the
962 * offset within the page.
963 */
964 s_off = vaddr & ~PAGE_MASK;
965 d_off = dst_vaddr & ~PAGE_MASK;
966 len = min_t(size_t, (PAGE_SIZE - s_off), size);
967
968 if (dec)
969 ret = __sev_dbg_decrypt_user(kvm,
970 __sme_page_pa(src_p[0]) + s_off,
971 (void __user *)dst_vaddr,
972 __sme_page_pa(dst_p[0]) + d_off,
973 len, &argp->error);
974 else
975 ret = __sev_dbg_encrypt_user(kvm,
976 __sme_page_pa(src_p[0]) + s_off,
977 (void __user *)vaddr,
978 __sme_page_pa(dst_p[0]) + d_off,
979 (void __user *)dst_vaddr,
980 len, &argp->error);
981
982 sev_unpin_memory(kvm, src_p, n);
983 sev_unpin_memory(kvm, dst_p, n);
984
985 if (ret)
986 goto err;
987
988 next_vaddr = vaddr + len;
989 dst_vaddr = dst_vaddr + len;
990 size -= len;
991 }
992 err:
993 return ret;
994 }
995
sev_launch_secret(struct kvm * kvm,struct kvm_sev_cmd * argp)996 static int sev_launch_secret(struct kvm *kvm, struct kvm_sev_cmd *argp)
997 {
998 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
999 struct sev_data_launch_secret data;
1000 struct kvm_sev_launch_secret params;
1001 struct page **pages;
1002 void *blob, *hdr;
1003 unsigned long n, i;
1004 int ret, offset;
1005
1006 if (!sev_guest(kvm))
1007 return -ENOTTY;
1008
1009 if (copy_from_user(¶ms, (void __user *)(uintptr_t)argp->data, sizeof(params)))
1010 return -EFAULT;
1011
1012 pages = sev_pin_memory(kvm, params.guest_uaddr, params.guest_len, &n, 1);
1013 if (IS_ERR(pages))
1014 return PTR_ERR(pages);
1015
1016 /*
1017 * Flush (on non-coherent CPUs) before LAUNCH_SECRET encrypts pages in
1018 * place; the cache may contain the data that was written unencrypted.
1019 */
1020 sev_clflush_pages(pages, n);
1021
1022 /*
1023 * The secret must be copied into contiguous memory region, lets verify
1024 * that userspace memory pages are contiguous before we issue command.
1025 */
1026 if (get_num_contig_pages(0, pages, n) != n) {
1027 ret = -EINVAL;
1028 goto e_unpin_memory;
1029 }
1030
1031 memset(&data, 0, sizeof(data));
1032
1033 offset = params.guest_uaddr & (PAGE_SIZE - 1);
1034 data.guest_address = __sme_page_pa(pages[0]) + offset;
1035 data.guest_len = params.guest_len;
1036
1037 blob = psp_copy_user_blob(params.trans_uaddr, params.trans_len);
1038 if (IS_ERR(blob)) {
1039 ret = PTR_ERR(blob);
1040 goto e_unpin_memory;
1041 }
1042
1043 data.trans_address = __psp_pa(blob);
1044 data.trans_len = params.trans_len;
1045
1046 hdr = psp_copy_user_blob(params.hdr_uaddr, params.hdr_len);
1047 if (IS_ERR(hdr)) {
1048 ret = PTR_ERR(hdr);
1049 goto e_free_blob;
1050 }
1051 data.hdr_address = __psp_pa(hdr);
1052 data.hdr_len = params.hdr_len;
1053
1054 data.handle = sev->handle;
1055 ret = sev_issue_cmd(kvm, SEV_CMD_LAUNCH_UPDATE_SECRET, &data, &argp->error);
1056
1057 kfree(hdr);
1058
1059 e_free_blob:
1060 kfree(blob);
1061 e_unpin_memory:
1062 /* content of memory is updated, mark pages dirty */
1063 for (i = 0; i < n; i++) {
1064 set_page_dirty_lock(pages[i]);
1065 mark_page_accessed(pages[i]);
1066 }
1067 sev_unpin_memory(kvm, pages, n);
1068 return ret;
1069 }
1070
sev_get_attestation_report(struct kvm * kvm,struct kvm_sev_cmd * argp)1071 static int sev_get_attestation_report(struct kvm *kvm, struct kvm_sev_cmd *argp)
1072 {
1073 void __user *report = (void __user *)(uintptr_t)argp->data;
1074 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
1075 struct sev_data_attestation_report data;
1076 struct kvm_sev_attestation_report params;
1077 void __user *p;
1078 void *blob = NULL;
1079 int ret;
1080
1081 if (!sev_guest(kvm))
1082 return -ENOTTY;
1083
1084 if (copy_from_user(¶ms, (void __user *)(uintptr_t)argp->data, sizeof(params)))
1085 return -EFAULT;
1086
1087 memset(&data, 0, sizeof(data));
1088
1089 /* User wants to query the blob length */
1090 if (!params.len)
1091 goto cmd;
1092
1093 p = (void __user *)(uintptr_t)params.uaddr;
1094 if (p) {
1095 if (params.len > SEV_FW_BLOB_MAX_SIZE)
1096 return -EINVAL;
1097
1098 blob = kzalloc(params.len, GFP_KERNEL_ACCOUNT);
1099 if (!blob)
1100 return -ENOMEM;
1101
1102 data.address = __psp_pa(blob);
1103 data.len = params.len;
1104 memcpy(data.mnonce, params.mnonce, sizeof(params.mnonce));
1105 }
1106 cmd:
1107 data.handle = sev->handle;
1108 ret = sev_issue_cmd(kvm, SEV_CMD_ATTESTATION_REPORT, &data, &argp->error);
1109 /*
1110 * If we query the session length, FW responded with expected data.
1111 */
1112 if (!params.len)
1113 goto done;
1114
1115 if (ret)
1116 goto e_free_blob;
1117
1118 if (blob) {
1119 if (copy_to_user(p, blob, params.len))
1120 ret = -EFAULT;
1121 }
1122
1123 done:
1124 params.len = data.len;
1125 if (copy_to_user(report, ¶ms, sizeof(params)))
1126 ret = -EFAULT;
1127 e_free_blob:
1128 kfree(blob);
1129 return ret;
1130 }
1131
1132 /* Userspace wants to query session length. */
1133 static int
__sev_send_start_query_session_length(struct kvm * kvm,struct kvm_sev_cmd * argp,struct kvm_sev_send_start * params)1134 __sev_send_start_query_session_length(struct kvm *kvm, struct kvm_sev_cmd *argp,
1135 struct kvm_sev_send_start *params)
1136 {
1137 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
1138 struct sev_data_send_start data;
1139 int ret;
1140
1141 memset(&data, 0, sizeof(data));
1142 data.handle = sev->handle;
1143 ret = sev_issue_cmd(kvm, SEV_CMD_SEND_START, &data, &argp->error);
1144
1145 params->session_len = data.session_len;
1146 if (copy_to_user((void __user *)(uintptr_t)argp->data, params,
1147 sizeof(struct kvm_sev_send_start)))
1148 ret = -EFAULT;
1149
1150 return ret;
1151 }
1152
sev_send_start(struct kvm * kvm,struct kvm_sev_cmd * argp)1153 static int sev_send_start(struct kvm *kvm, struct kvm_sev_cmd *argp)
1154 {
1155 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
1156 struct sev_data_send_start data;
1157 struct kvm_sev_send_start params;
1158 void *amd_certs, *session_data;
1159 void *pdh_cert, *plat_certs;
1160 int ret;
1161
1162 if (!sev_guest(kvm))
1163 return -ENOTTY;
1164
1165 if (copy_from_user(¶ms, (void __user *)(uintptr_t)argp->data,
1166 sizeof(struct kvm_sev_send_start)))
1167 return -EFAULT;
1168
1169 /* if session_len is zero, userspace wants to query the session length */
1170 if (!params.session_len)
1171 return __sev_send_start_query_session_length(kvm, argp,
1172 ¶ms);
1173
1174 /* some sanity checks */
1175 if (!params.pdh_cert_uaddr || !params.pdh_cert_len ||
1176 !params.session_uaddr || params.session_len > SEV_FW_BLOB_MAX_SIZE)
1177 return -EINVAL;
1178
1179 /* allocate the memory to hold the session data blob */
1180 session_data = kzalloc(params.session_len, GFP_KERNEL_ACCOUNT);
1181 if (!session_data)
1182 return -ENOMEM;
1183
1184 /* copy the certificate blobs from userspace */
1185 pdh_cert = psp_copy_user_blob(params.pdh_cert_uaddr,
1186 params.pdh_cert_len);
1187 if (IS_ERR(pdh_cert)) {
1188 ret = PTR_ERR(pdh_cert);
1189 goto e_free_session;
1190 }
1191
1192 plat_certs = psp_copy_user_blob(params.plat_certs_uaddr,
1193 params.plat_certs_len);
1194 if (IS_ERR(plat_certs)) {
1195 ret = PTR_ERR(plat_certs);
1196 goto e_free_pdh;
1197 }
1198
1199 amd_certs = psp_copy_user_blob(params.amd_certs_uaddr,
1200 params.amd_certs_len);
1201 if (IS_ERR(amd_certs)) {
1202 ret = PTR_ERR(amd_certs);
1203 goto e_free_plat_cert;
1204 }
1205
1206 /* populate the FW SEND_START field with system physical address */
1207 memset(&data, 0, sizeof(data));
1208 data.pdh_cert_address = __psp_pa(pdh_cert);
1209 data.pdh_cert_len = params.pdh_cert_len;
1210 data.plat_certs_address = __psp_pa(plat_certs);
1211 data.plat_certs_len = params.plat_certs_len;
1212 data.amd_certs_address = __psp_pa(amd_certs);
1213 data.amd_certs_len = params.amd_certs_len;
1214 data.session_address = __psp_pa(session_data);
1215 data.session_len = params.session_len;
1216 data.handle = sev->handle;
1217
1218 ret = sev_issue_cmd(kvm, SEV_CMD_SEND_START, &data, &argp->error);
1219
1220 if (!ret && copy_to_user((void __user *)(uintptr_t)params.session_uaddr,
1221 session_data, params.session_len)) {
1222 ret = -EFAULT;
1223 goto e_free_amd_cert;
1224 }
1225
1226 params.policy = data.policy;
1227 params.session_len = data.session_len;
1228 if (copy_to_user((void __user *)(uintptr_t)argp->data, ¶ms,
1229 sizeof(struct kvm_sev_send_start)))
1230 ret = -EFAULT;
1231
1232 e_free_amd_cert:
1233 kfree(amd_certs);
1234 e_free_plat_cert:
1235 kfree(plat_certs);
1236 e_free_pdh:
1237 kfree(pdh_cert);
1238 e_free_session:
1239 kfree(session_data);
1240 return ret;
1241 }
1242
1243 /* Userspace wants to query either header or trans length. */
1244 static int
__sev_send_update_data_query_lengths(struct kvm * kvm,struct kvm_sev_cmd * argp,struct kvm_sev_send_update_data * params)1245 __sev_send_update_data_query_lengths(struct kvm *kvm, struct kvm_sev_cmd *argp,
1246 struct kvm_sev_send_update_data *params)
1247 {
1248 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
1249 struct sev_data_send_update_data data;
1250 int ret;
1251
1252 memset(&data, 0, sizeof(data));
1253 data.handle = sev->handle;
1254 ret = sev_issue_cmd(kvm, SEV_CMD_SEND_UPDATE_DATA, &data, &argp->error);
1255
1256 params->hdr_len = data.hdr_len;
1257 params->trans_len = data.trans_len;
1258
1259 if (copy_to_user((void __user *)(uintptr_t)argp->data, params,
1260 sizeof(struct kvm_sev_send_update_data)))
1261 ret = -EFAULT;
1262
1263 return ret;
1264 }
1265
sev_send_update_data(struct kvm * kvm,struct kvm_sev_cmd * argp)1266 static int sev_send_update_data(struct kvm *kvm, struct kvm_sev_cmd *argp)
1267 {
1268 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
1269 struct sev_data_send_update_data data;
1270 struct kvm_sev_send_update_data params;
1271 void *hdr, *trans_data;
1272 struct page **guest_page;
1273 unsigned long n;
1274 int ret, offset;
1275
1276 if (!sev_guest(kvm))
1277 return -ENOTTY;
1278
1279 if (copy_from_user(¶ms, (void __user *)(uintptr_t)argp->data,
1280 sizeof(struct kvm_sev_send_update_data)))
1281 return -EFAULT;
1282
1283 /* userspace wants to query either header or trans length */
1284 if (!params.trans_len || !params.hdr_len)
1285 return __sev_send_update_data_query_lengths(kvm, argp, ¶ms);
1286
1287 if (!params.trans_uaddr || !params.guest_uaddr ||
1288 !params.guest_len || !params.hdr_uaddr)
1289 return -EINVAL;
1290
1291 /* Check if we are crossing the page boundary */
1292 offset = params.guest_uaddr & (PAGE_SIZE - 1);
1293 if ((params.guest_len + offset > PAGE_SIZE))
1294 return -EINVAL;
1295
1296 /* Pin guest memory */
1297 guest_page = sev_pin_memory(kvm, params.guest_uaddr & PAGE_MASK,
1298 PAGE_SIZE, &n, 0);
1299 if (IS_ERR(guest_page))
1300 return PTR_ERR(guest_page);
1301
1302 /* allocate memory for header and transport buffer */
1303 ret = -ENOMEM;
1304 hdr = kzalloc(params.hdr_len, GFP_KERNEL_ACCOUNT);
1305 if (!hdr)
1306 goto e_unpin;
1307
1308 trans_data = kzalloc(params.trans_len, GFP_KERNEL_ACCOUNT);
1309 if (!trans_data)
1310 goto e_free_hdr;
1311
1312 memset(&data, 0, sizeof(data));
1313 data.hdr_address = __psp_pa(hdr);
1314 data.hdr_len = params.hdr_len;
1315 data.trans_address = __psp_pa(trans_data);
1316 data.trans_len = params.trans_len;
1317
1318 /* The SEND_UPDATE_DATA command requires C-bit to be always set. */
1319 data.guest_address = (page_to_pfn(guest_page[0]) << PAGE_SHIFT) + offset;
1320 data.guest_address |= sev_me_mask;
1321 data.guest_len = params.guest_len;
1322 data.handle = sev->handle;
1323
1324 ret = sev_issue_cmd(kvm, SEV_CMD_SEND_UPDATE_DATA, &data, &argp->error);
1325
1326 if (ret)
1327 goto e_free_trans_data;
1328
1329 /* copy transport buffer to user space */
1330 if (copy_to_user((void __user *)(uintptr_t)params.trans_uaddr,
1331 trans_data, params.trans_len)) {
1332 ret = -EFAULT;
1333 goto e_free_trans_data;
1334 }
1335
1336 /* Copy packet header to userspace. */
1337 if (copy_to_user((void __user *)(uintptr_t)params.hdr_uaddr, hdr,
1338 params.hdr_len))
1339 ret = -EFAULT;
1340
1341 e_free_trans_data:
1342 kfree(trans_data);
1343 e_free_hdr:
1344 kfree(hdr);
1345 e_unpin:
1346 sev_unpin_memory(kvm, guest_page, n);
1347
1348 return ret;
1349 }
1350
sev_send_finish(struct kvm * kvm,struct kvm_sev_cmd * argp)1351 static int sev_send_finish(struct kvm *kvm, struct kvm_sev_cmd *argp)
1352 {
1353 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
1354 struct sev_data_send_finish data;
1355
1356 if (!sev_guest(kvm))
1357 return -ENOTTY;
1358
1359 data.handle = sev->handle;
1360 return sev_issue_cmd(kvm, SEV_CMD_SEND_FINISH, &data, &argp->error);
1361 }
1362
sev_send_cancel(struct kvm * kvm,struct kvm_sev_cmd * argp)1363 static int sev_send_cancel(struct kvm *kvm, struct kvm_sev_cmd *argp)
1364 {
1365 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
1366 struct sev_data_send_cancel data;
1367
1368 if (!sev_guest(kvm))
1369 return -ENOTTY;
1370
1371 data.handle = sev->handle;
1372 return sev_issue_cmd(kvm, SEV_CMD_SEND_CANCEL, &data, &argp->error);
1373 }
1374
sev_receive_start(struct kvm * kvm,struct kvm_sev_cmd * argp)1375 static int sev_receive_start(struct kvm *kvm, struct kvm_sev_cmd *argp)
1376 {
1377 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
1378 struct sev_data_receive_start start;
1379 struct kvm_sev_receive_start params;
1380 int *error = &argp->error;
1381 void *session_data;
1382 void *pdh_data;
1383 int ret;
1384
1385 if (!sev_guest(kvm))
1386 return -ENOTTY;
1387
1388 /* Get parameter from the userspace */
1389 if (copy_from_user(¶ms, (void __user *)(uintptr_t)argp->data,
1390 sizeof(struct kvm_sev_receive_start)))
1391 return -EFAULT;
1392
1393 /* some sanity checks */
1394 if (!params.pdh_uaddr || !params.pdh_len ||
1395 !params.session_uaddr || !params.session_len)
1396 return -EINVAL;
1397
1398 pdh_data = psp_copy_user_blob(params.pdh_uaddr, params.pdh_len);
1399 if (IS_ERR(pdh_data))
1400 return PTR_ERR(pdh_data);
1401
1402 session_data = psp_copy_user_blob(params.session_uaddr,
1403 params.session_len);
1404 if (IS_ERR(session_data)) {
1405 ret = PTR_ERR(session_data);
1406 goto e_free_pdh;
1407 }
1408
1409 memset(&start, 0, sizeof(start));
1410 start.handle = params.handle;
1411 start.policy = params.policy;
1412 start.pdh_cert_address = __psp_pa(pdh_data);
1413 start.pdh_cert_len = params.pdh_len;
1414 start.session_address = __psp_pa(session_data);
1415 start.session_len = params.session_len;
1416
1417 /* create memory encryption context */
1418 ret = __sev_issue_cmd(argp->sev_fd, SEV_CMD_RECEIVE_START, &start,
1419 error);
1420 if (ret)
1421 goto e_free_session;
1422
1423 /* Bind ASID to this guest */
1424 ret = sev_bind_asid(kvm, start.handle, error);
1425 if (ret) {
1426 sev_decommission(start.handle);
1427 goto e_free_session;
1428 }
1429
1430 params.handle = start.handle;
1431 if (copy_to_user((void __user *)(uintptr_t)argp->data,
1432 ¶ms, sizeof(struct kvm_sev_receive_start))) {
1433 ret = -EFAULT;
1434 sev_unbind_asid(kvm, start.handle);
1435 goto e_free_session;
1436 }
1437
1438 sev->handle = start.handle;
1439 sev->fd = argp->sev_fd;
1440
1441 e_free_session:
1442 kfree(session_data);
1443 e_free_pdh:
1444 kfree(pdh_data);
1445
1446 return ret;
1447 }
1448
sev_receive_update_data(struct kvm * kvm,struct kvm_sev_cmd * argp)1449 static int sev_receive_update_data(struct kvm *kvm, struct kvm_sev_cmd *argp)
1450 {
1451 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
1452 struct kvm_sev_receive_update_data params;
1453 struct sev_data_receive_update_data data;
1454 void *hdr = NULL, *trans = NULL;
1455 struct page **guest_page;
1456 unsigned long n;
1457 int ret, offset;
1458
1459 if (!sev_guest(kvm))
1460 return -EINVAL;
1461
1462 if (copy_from_user(¶ms, (void __user *)(uintptr_t)argp->data,
1463 sizeof(struct kvm_sev_receive_update_data)))
1464 return -EFAULT;
1465
1466 if (!params.hdr_uaddr || !params.hdr_len ||
1467 !params.guest_uaddr || !params.guest_len ||
1468 !params.trans_uaddr || !params.trans_len)
1469 return -EINVAL;
1470
1471 /* Check if we are crossing the page boundary */
1472 offset = params.guest_uaddr & (PAGE_SIZE - 1);
1473 if ((params.guest_len + offset > PAGE_SIZE))
1474 return -EINVAL;
1475
1476 hdr = psp_copy_user_blob(params.hdr_uaddr, params.hdr_len);
1477 if (IS_ERR(hdr))
1478 return PTR_ERR(hdr);
1479
1480 trans = psp_copy_user_blob(params.trans_uaddr, params.trans_len);
1481 if (IS_ERR(trans)) {
1482 ret = PTR_ERR(trans);
1483 goto e_free_hdr;
1484 }
1485
1486 memset(&data, 0, sizeof(data));
1487 data.hdr_address = __psp_pa(hdr);
1488 data.hdr_len = params.hdr_len;
1489 data.trans_address = __psp_pa(trans);
1490 data.trans_len = params.trans_len;
1491
1492 /* Pin guest memory */
1493 guest_page = sev_pin_memory(kvm, params.guest_uaddr & PAGE_MASK,
1494 PAGE_SIZE, &n, 1);
1495 if (IS_ERR(guest_page)) {
1496 ret = PTR_ERR(guest_page);
1497 goto e_free_trans;
1498 }
1499
1500 /*
1501 * Flush (on non-coherent CPUs) before RECEIVE_UPDATE_DATA, the PSP
1502 * encrypts the written data with the guest's key, and the cache may
1503 * contain dirty, unencrypted data.
1504 */
1505 sev_clflush_pages(guest_page, n);
1506
1507 /* The RECEIVE_UPDATE_DATA command requires C-bit to be always set. */
1508 data.guest_address = (page_to_pfn(guest_page[0]) << PAGE_SHIFT) + offset;
1509 data.guest_address |= sev_me_mask;
1510 data.guest_len = params.guest_len;
1511 data.handle = sev->handle;
1512
1513 ret = sev_issue_cmd(kvm, SEV_CMD_RECEIVE_UPDATE_DATA, &data,
1514 &argp->error);
1515
1516 sev_unpin_memory(kvm, guest_page, n);
1517
1518 e_free_trans:
1519 kfree(trans);
1520 e_free_hdr:
1521 kfree(hdr);
1522
1523 return ret;
1524 }
1525
sev_receive_finish(struct kvm * kvm,struct kvm_sev_cmd * argp)1526 static int sev_receive_finish(struct kvm *kvm, struct kvm_sev_cmd *argp)
1527 {
1528 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
1529 struct sev_data_receive_finish data;
1530
1531 if (!sev_guest(kvm))
1532 return -ENOTTY;
1533
1534 data.handle = sev->handle;
1535 return sev_issue_cmd(kvm, SEV_CMD_RECEIVE_FINISH, &data, &argp->error);
1536 }
1537
is_cmd_allowed_from_mirror(u32 cmd_id)1538 static bool is_cmd_allowed_from_mirror(u32 cmd_id)
1539 {
1540 /*
1541 * Allow mirrors VM to call KVM_SEV_LAUNCH_UPDATE_VMSA to enable SEV-ES
1542 * active mirror VMs. Also allow the debugging and status commands.
1543 */
1544 if (cmd_id == KVM_SEV_LAUNCH_UPDATE_VMSA ||
1545 cmd_id == KVM_SEV_GUEST_STATUS || cmd_id == KVM_SEV_DBG_DECRYPT ||
1546 cmd_id == KVM_SEV_DBG_ENCRYPT)
1547 return true;
1548
1549 return false;
1550 }
1551
sev_lock_two_vms(struct kvm * dst_kvm,struct kvm * src_kvm)1552 static int sev_lock_two_vms(struct kvm *dst_kvm, struct kvm *src_kvm)
1553 {
1554 struct kvm_sev_info *dst_sev = &to_kvm_svm(dst_kvm)->sev_info;
1555 struct kvm_sev_info *src_sev = &to_kvm_svm(src_kvm)->sev_info;
1556 int r = -EBUSY;
1557
1558 if (dst_kvm == src_kvm)
1559 return -EINVAL;
1560
1561 /*
1562 * Bail if these VMs are already involved in a migration to avoid
1563 * deadlock between two VMs trying to migrate to/from each other.
1564 */
1565 if (atomic_cmpxchg_acquire(&dst_sev->migration_in_progress, 0, 1))
1566 return -EBUSY;
1567
1568 if (atomic_cmpxchg_acquire(&src_sev->migration_in_progress, 0, 1))
1569 goto release_dst;
1570
1571 r = -EINTR;
1572 if (mutex_lock_killable(&dst_kvm->lock))
1573 goto release_src;
1574 if (mutex_lock_killable_nested(&src_kvm->lock, SINGLE_DEPTH_NESTING))
1575 goto unlock_dst;
1576 return 0;
1577
1578 unlock_dst:
1579 mutex_unlock(&dst_kvm->lock);
1580 release_src:
1581 atomic_set_release(&src_sev->migration_in_progress, 0);
1582 release_dst:
1583 atomic_set_release(&dst_sev->migration_in_progress, 0);
1584 return r;
1585 }
1586
sev_unlock_two_vms(struct kvm * dst_kvm,struct kvm * src_kvm)1587 static void sev_unlock_two_vms(struct kvm *dst_kvm, struct kvm *src_kvm)
1588 {
1589 struct kvm_sev_info *dst_sev = &to_kvm_svm(dst_kvm)->sev_info;
1590 struct kvm_sev_info *src_sev = &to_kvm_svm(src_kvm)->sev_info;
1591
1592 mutex_unlock(&dst_kvm->lock);
1593 mutex_unlock(&src_kvm->lock);
1594 atomic_set_release(&dst_sev->migration_in_progress, 0);
1595 atomic_set_release(&src_sev->migration_in_progress, 0);
1596 }
1597
1598 /* vCPU mutex subclasses. */
1599 enum sev_migration_role {
1600 SEV_MIGRATION_SOURCE = 0,
1601 SEV_MIGRATION_TARGET,
1602 SEV_NR_MIGRATION_ROLES,
1603 };
1604
sev_lock_vcpus_for_migration(struct kvm * kvm,enum sev_migration_role role)1605 static int sev_lock_vcpus_for_migration(struct kvm *kvm,
1606 enum sev_migration_role role)
1607 {
1608 struct kvm_vcpu *vcpu;
1609 unsigned long i, j;
1610 bool first = true;
1611
1612 kvm_for_each_vcpu(i, vcpu, kvm) {
1613 if (mutex_lock_killable_nested(&vcpu->mutex, role))
1614 goto out_unlock;
1615
1616 if (first) {
1617 /*
1618 * Reset the role to one that avoids colliding with
1619 * the role used for the first vcpu mutex.
1620 */
1621 role = SEV_NR_MIGRATION_ROLES;
1622 first = false;
1623 } else {
1624 mutex_release(&vcpu->mutex.dep_map, _THIS_IP_);
1625 }
1626 }
1627
1628 return 0;
1629
1630 out_unlock:
1631
1632 first = true;
1633 kvm_for_each_vcpu(j, vcpu, kvm) {
1634 if (i == j)
1635 break;
1636
1637 if (first)
1638 first = false;
1639 else
1640 mutex_acquire(&vcpu->mutex.dep_map, role, 0, _THIS_IP_);
1641
1642
1643 mutex_unlock(&vcpu->mutex);
1644 }
1645 return -EINTR;
1646 }
1647
sev_unlock_vcpus_for_migration(struct kvm * kvm)1648 static void sev_unlock_vcpus_for_migration(struct kvm *kvm)
1649 {
1650 struct kvm_vcpu *vcpu;
1651 unsigned long i;
1652 bool first = true;
1653
1654 kvm_for_each_vcpu(i, vcpu, kvm) {
1655 if (first)
1656 first = false;
1657 else
1658 mutex_acquire(&vcpu->mutex.dep_map,
1659 SEV_NR_MIGRATION_ROLES, 0, _THIS_IP_);
1660
1661 mutex_unlock(&vcpu->mutex);
1662 }
1663 }
1664
sev_migrate_from(struct kvm * dst_kvm,struct kvm * src_kvm)1665 static void sev_migrate_from(struct kvm *dst_kvm, struct kvm *src_kvm)
1666 {
1667 struct kvm_sev_info *dst = &to_kvm_svm(dst_kvm)->sev_info;
1668 struct kvm_sev_info *src = &to_kvm_svm(src_kvm)->sev_info;
1669 struct kvm_vcpu *dst_vcpu, *src_vcpu;
1670 struct vcpu_svm *dst_svm, *src_svm;
1671 struct kvm_sev_info *mirror;
1672 unsigned long i;
1673
1674 dst->active = true;
1675 dst->asid = src->asid;
1676 dst->handle = src->handle;
1677 dst->pages_locked = src->pages_locked;
1678 dst->enc_context_owner = src->enc_context_owner;
1679 dst->es_active = src->es_active;
1680
1681 src->asid = 0;
1682 src->active = false;
1683 src->handle = 0;
1684 src->pages_locked = 0;
1685 src->enc_context_owner = NULL;
1686 src->es_active = false;
1687
1688 list_cut_before(&dst->regions_list, &src->regions_list, &src->regions_list);
1689
1690 /*
1691 * If this VM has mirrors, "transfer" each mirror's refcount of the
1692 * source to the destination (this KVM). The caller holds a reference
1693 * to the source, so there's no danger of use-after-free.
1694 */
1695 list_cut_before(&dst->mirror_vms, &src->mirror_vms, &src->mirror_vms);
1696 list_for_each_entry(mirror, &dst->mirror_vms, mirror_entry) {
1697 kvm_get_kvm(dst_kvm);
1698 kvm_put_kvm(src_kvm);
1699 mirror->enc_context_owner = dst_kvm;
1700 }
1701
1702 /*
1703 * If this VM is a mirror, remove the old mirror from the owners list
1704 * and add the new mirror to the list.
1705 */
1706 if (is_mirroring_enc_context(dst_kvm)) {
1707 struct kvm_sev_info *owner_sev_info =
1708 &to_kvm_svm(dst->enc_context_owner)->sev_info;
1709
1710 list_del(&src->mirror_entry);
1711 list_add_tail(&dst->mirror_entry, &owner_sev_info->mirror_vms);
1712 }
1713
1714 kvm_for_each_vcpu(i, dst_vcpu, dst_kvm) {
1715 dst_svm = to_svm(dst_vcpu);
1716
1717 sev_init_vmcb(dst_svm);
1718
1719 if (!dst->es_active)
1720 continue;
1721
1722 /*
1723 * Note, the source is not required to have the same number of
1724 * vCPUs as the destination when migrating a vanilla SEV VM.
1725 */
1726 src_vcpu = kvm_get_vcpu(dst_kvm, i);
1727 src_svm = to_svm(src_vcpu);
1728
1729 /*
1730 * Transfer VMSA and GHCB state to the destination. Nullify and
1731 * clear source fields as appropriate, the state now belongs to
1732 * the destination.
1733 */
1734 memcpy(&dst_svm->sev_es, &src_svm->sev_es, sizeof(src_svm->sev_es));
1735 dst_svm->vmcb->control.ghcb_gpa = src_svm->vmcb->control.ghcb_gpa;
1736 dst_svm->vmcb->control.vmsa_pa = src_svm->vmcb->control.vmsa_pa;
1737 dst_vcpu->arch.guest_state_protected = true;
1738
1739 memset(&src_svm->sev_es, 0, sizeof(src_svm->sev_es));
1740 src_svm->vmcb->control.ghcb_gpa = INVALID_PAGE;
1741 src_svm->vmcb->control.vmsa_pa = INVALID_PAGE;
1742 src_vcpu->arch.guest_state_protected = false;
1743 }
1744 }
1745
sev_check_source_vcpus(struct kvm * dst,struct kvm * src)1746 static int sev_check_source_vcpus(struct kvm *dst, struct kvm *src)
1747 {
1748 struct kvm_vcpu *src_vcpu;
1749 unsigned long i;
1750
1751 if (!sev_es_guest(src))
1752 return 0;
1753
1754 if (atomic_read(&src->online_vcpus) != atomic_read(&dst->online_vcpus))
1755 return -EINVAL;
1756
1757 kvm_for_each_vcpu(i, src_vcpu, src) {
1758 if (!src_vcpu->arch.guest_state_protected)
1759 return -EINVAL;
1760 }
1761
1762 return 0;
1763 }
1764
sev_vm_move_enc_context_from(struct kvm * kvm,unsigned int source_fd)1765 int sev_vm_move_enc_context_from(struct kvm *kvm, unsigned int source_fd)
1766 {
1767 struct kvm_sev_info *dst_sev = &to_kvm_svm(kvm)->sev_info;
1768 struct kvm_sev_info *src_sev, *cg_cleanup_sev;
1769 struct file *source_kvm_file;
1770 struct kvm *source_kvm;
1771 bool charged = false;
1772 int ret;
1773
1774 source_kvm_file = fget(source_fd);
1775 if (!file_is_kvm(source_kvm_file)) {
1776 ret = -EBADF;
1777 goto out_fput;
1778 }
1779
1780 source_kvm = source_kvm_file->private_data;
1781 ret = sev_lock_two_vms(kvm, source_kvm);
1782 if (ret)
1783 goto out_fput;
1784
1785 if (sev_guest(kvm) || !sev_guest(source_kvm)) {
1786 ret = -EINVAL;
1787 goto out_unlock;
1788 }
1789
1790 src_sev = &to_kvm_svm(source_kvm)->sev_info;
1791
1792 dst_sev->misc_cg = get_current_misc_cg();
1793 cg_cleanup_sev = dst_sev;
1794 if (dst_sev->misc_cg != src_sev->misc_cg) {
1795 ret = sev_misc_cg_try_charge(dst_sev);
1796 if (ret)
1797 goto out_dst_cgroup;
1798 charged = true;
1799 }
1800
1801 ret = sev_lock_vcpus_for_migration(kvm, SEV_MIGRATION_SOURCE);
1802 if (ret)
1803 goto out_dst_cgroup;
1804 ret = sev_lock_vcpus_for_migration(source_kvm, SEV_MIGRATION_TARGET);
1805 if (ret)
1806 goto out_dst_vcpu;
1807
1808 ret = sev_check_source_vcpus(kvm, source_kvm);
1809 if (ret)
1810 goto out_source_vcpu;
1811
1812 sev_migrate_from(kvm, source_kvm);
1813 kvm_vm_dead(source_kvm);
1814 cg_cleanup_sev = src_sev;
1815 ret = 0;
1816
1817 out_source_vcpu:
1818 sev_unlock_vcpus_for_migration(source_kvm);
1819 out_dst_vcpu:
1820 sev_unlock_vcpus_for_migration(kvm);
1821 out_dst_cgroup:
1822 /* Operates on the source on success, on the destination on failure. */
1823 if (charged)
1824 sev_misc_cg_uncharge(cg_cleanup_sev);
1825 put_misc_cg(cg_cleanup_sev->misc_cg);
1826 cg_cleanup_sev->misc_cg = NULL;
1827 out_unlock:
1828 sev_unlock_two_vms(kvm, source_kvm);
1829 out_fput:
1830 if (source_kvm_file)
1831 fput(source_kvm_file);
1832 return ret;
1833 }
1834
sev_mem_enc_ioctl(struct kvm * kvm,void __user * argp)1835 int sev_mem_enc_ioctl(struct kvm *kvm, void __user *argp)
1836 {
1837 struct kvm_sev_cmd sev_cmd;
1838 int r;
1839
1840 if (!sev_enabled)
1841 return -ENOTTY;
1842
1843 if (!argp)
1844 return 0;
1845
1846 if (copy_from_user(&sev_cmd, argp, sizeof(struct kvm_sev_cmd)))
1847 return -EFAULT;
1848
1849 mutex_lock(&kvm->lock);
1850
1851 /* Only the enc_context_owner handles some memory enc operations. */
1852 if (is_mirroring_enc_context(kvm) &&
1853 !is_cmd_allowed_from_mirror(sev_cmd.id)) {
1854 r = -EINVAL;
1855 goto out;
1856 }
1857
1858 switch (sev_cmd.id) {
1859 case KVM_SEV_ES_INIT:
1860 if (!sev_es_enabled) {
1861 r = -ENOTTY;
1862 goto out;
1863 }
1864 fallthrough;
1865 case KVM_SEV_INIT:
1866 r = sev_guest_init(kvm, &sev_cmd);
1867 break;
1868 case KVM_SEV_LAUNCH_START:
1869 r = sev_launch_start(kvm, &sev_cmd);
1870 break;
1871 case KVM_SEV_LAUNCH_UPDATE_DATA:
1872 r = sev_launch_update_data(kvm, &sev_cmd);
1873 break;
1874 case KVM_SEV_LAUNCH_UPDATE_VMSA:
1875 r = sev_launch_update_vmsa(kvm, &sev_cmd);
1876 break;
1877 case KVM_SEV_LAUNCH_MEASURE:
1878 r = sev_launch_measure(kvm, &sev_cmd);
1879 break;
1880 case KVM_SEV_LAUNCH_FINISH:
1881 r = sev_launch_finish(kvm, &sev_cmd);
1882 break;
1883 case KVM_SEV_GUEST_STATUS:
1884 r = sev_guest_status(kvm, &sev_cmd);
1885 break;
1886 case KVM_SEV_DBG_DECRYPT:
1887 r = sev_dbg_crypt(kvm, &sev_cmd, true);
1888 break;
1889 case KVM_SEV_DBG_ENCRYPT:
1890 r = sev_dbg_crypt(kvm, &sev_cmd, false);
1891 break;
1892 case KVM_SEV_LAUNCH_SECRET:
1893 r = sev_launch_secret(kvm, &sev_cmd);
1894 break;
1895 case KVM_SEV_GET_ATTESTATION_REPORT:
1896 r = sev_get_attestation_report(kvm, &sev_cmd);
1897 break;
1898 case KVM_SEV_SEND_START:
1899 r = sev_send_start(kvm, &sev_cmd);
1900 break;
1901 case KVM_SEV_SEND_UPDATE_DATA:
1902 r = sev_send_update_data(kvm, &sev_cmd);
1903 break;
1904 case KVM_SEV_SEND_FINISH:
1905 r = sev_send_finish(kvm, &sev_cmd);
1906 break;
1907 case KVM_SEV_SEND_CANCEL:
1908 r = sev_send_cancel(kvm, &sev_cmd);
1909 break;
1910 case KVM_SEV_RECEIVE_START:
1911 r = sev_receive_start(kvm, &sev_cmd);
1912 break;
1913 case KVM_SEV_RECEIVE_UPDATE_DATA:
1914 r = sev_receive_update_data(kvm, &sev_cmd);
1915 break;
1916 case KVM_SEV_RECEIVE_FINISH:
1917 r = sev_receive_finish(kvm, &sev_cmd);
1918 break;
1919 default:
1920 r = -EINVAL;
1921 goto out;
1922 }
1923
1924 if (copy_to_user(argp, &sev_cmd, sizeof(struct kvm_sev_cmd)))
1925 r = -EFAULT;
1926
1927 out:
1928 mutex_unlock(&kvm->lock);
1929 return r;
1930 }
1931
sev_mem_enc_register_region(struct kvm * kvm,struct kvm_enc_region * range)1932 int sev_mem_enc_register_region(struct kvm *kvm,
1933 struct kvm_enc_region *range)
1934 {
1935 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
1936 struct enc_region *region;
1937 int ret = 0;
1938
1939 if (!sev_guest(kvm))
1940 return -ENOTTY;
1941
1942 /* If kvm is mirroring encryption context it isn't responsible for it */
1943 if (is_mirroring_enc_context(kvm))
1944 return -EINVAL;
1945
1946 if (range->addr > ULONG_MAX || range->size > ULONG_MAX)
1947 return -EINVAL;
1948
1949 region = kzalloc(sizeof(*region), GFP_KERNEL_ACCOUNT);
1950 if (!region)
1951 return -ENOMEM;
1952
1953 mutex_lock(&kvm->lock);
1954 region->pages = sev_pin_memory(kvm, range->addr, range->size, ®ion->npages, 1);
1955 if (IS_ERR(region->pages)) {
1956 ret = PTR_ERR(region->pages);
1957 mutex_unlock(&kvm->lock);
1958 goto e_free;
1959 }
1960
1961 region->uaddr = range->addr;
1962 region->size = range->size;
1963
1964 list_add_tail(®ion->list, &sev->regions_list);
1965 mutex_unlock(&kvm->lock);
1966
1967 /*
1968 * The guest may change the memory encryption attribute from C=0 -> C=1
1969 * or vice versa for this memory range. Lets make sure caches are
1970 * flushed to ensure that guest data gets written into memory with
1971 * correct C-bit.
1972 */
1973 sev_clflush_pages(region->pages, region->npages);
1974
1975 return ret;
1976
1977 e_free:
1978 kfree(region);
1979 return ret;
1980 }
1981
1982 static struct enc_region *
find_enc_region(struct kvm * kvm,struct kvm_enc_region * range)1983 find_enc_region(struct kvm *kvm, struct kvm_enc_region *range)
1984 {
1985 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
1986 struct list_head *head = &sev->regions_list;
1987 struct enc_region *i;
1988
1989 list_for_each_entry(i, head, list) {
1990 if (i->uaddr == range->addr &&
1991 i->size == range->size)
1992 return i;
1993 }
1994
1995 return NULL;
1996 }
1997
__unregister_enc_region_locked(struct kvm * kvm,struct enc_region * region)1998 static void __unregister_enc_region_locked(struct kvm *kvm,
1999 struct enc_region *region)
2000 {
2001 sev_unpin_memory(kvm, region->pages, region->npages);
2002 list_del(®ion->list);
2003 kfree(region);
2004 }
2005
sev_mem_enc_unregister_region(struct kvm * kvm,struct kvm_enc_region * range)2006 int sev_mem_enc_unregister_region(struct kvm *kvm,
2007 struct kvm_enc_region *range)
2008 {
2009 struct enc_region *region;
2010 int ret;
2011
2012 /* If kvm is mirroring encryption context it isn't responsible for it */
2013 if (is_mirroring_enc_context(kvm))
2014 return -EINVAL;
2015
2016 mutex_lock(&kvm->lock);
2017
2018 if (!sev_guest(kvm)) {
2019 ret = -ENOTTY;
2020 goto failed;
2021 }
2022
2023 region = find_enc_region(kvm, range);
2024 if (!region) {
2025 ret = -EINVAL;
2026 goto failed;
2027 }
2028
2029 /*
2030 * Ensure that all guest tagged cache entries are flushed before
2031 * releasing the pages back to the system for use. CLFLUSH will
2032 * not do this, so issue a WBINVD.
2033 */
2034 wbinvd_on_all_cpus();
2035
2036 __unregister_enc_region_locked(kvm, region);
2037
2038 mutex_unlock(&kvm->lock);
2039 return 0;
2040
2041 failed:
2042 mutex_unlock(&kvm->lock);
2043 return ret;
2044 }
2045
sev_vm_copy_enc_context_from(struct kvm * kvm,unsigned int source_fd)2046 int sev_vm_copy_enc_context_from(struct kvm *kvm, unsigned int source_fd)
2047 {
2048 struct file *source_kvm_file;
2049 struct kvm *source_kvm;
2050 struct kvm_sev_info *source_sev, *mirror_sev;
2051 int ret;
2052
2053 source_kvm_file = fget(source_fd);
2054 if (!file_is_kvm(source_kvm_file)) {
2055 ret = -EBADF;
2056 goto e_source_fput;
2057 }
2058
2059 source_kvm = source_kvm_file->private_data;
2060 ret = sev_lock_two_vms(kvm, source_kvm);
2061 if (ret)
2062 goto e_source_fput;
2063
2064 /*
2065 * Mirrors of mirrors should work, but let's not get silly. Also
2066 * disallow out-of-band SEV/SEV-ES init if the target is already an
2067 * SEV guest, or if vCPUs have been created. KVM relies on vCPUs being
2068 * created after SEV/SEV-ES initialization, e.g. to init intercepts.
2069 */
2070 if (sev_guest(kvm) || !sev_guest(source_kvm) ||
2071 is_mirroring_enc_context(source_kvm) || kvm->created_vcpus) {
2072 ret = -EINVAL;
2073 goto e_unlock;
2074 }
2075
2076 /*
2077 * The mirror kvm holds an enc_context_owner ref so its asid can't
2078 * disappear until we're done with it
2079 */
2080 source_sev = &to_kvm_svm(source_kvm)->sev_info;
2081 kvm_get_kvm(source_kvm);
2082 mirror_sev = &to_kvm_svm(kvm)->sev_info;
2083 list_add_tail(&mirror_sev->mirror_entry, &source_sev->mirror_vms);
2084
2085 /* Set enc_context_owner and copy its encryption context over */
2086 mirror_sev->enc_context_owner = source_kvm;
2087 mirror_sev->active = true;
2088 mirror_sev->asid = source_sev->asid;
2089 mirror_sev->fd = source_sev->fd;
2090 mirror_sev->es_active = source_sev->es_active;
2091 mirror_sev->handle = source_sev->handle;
2092 INIT_LIST_HEAD(&mirror_sev->regions_list);
2093 INIT_LIST_HEAD(&mirror_sev->mirror_vms);
2094 ret = 0;
2095
2096 /*
2097 * Do not copy ap_jump_table. Since the mirror does not share the same
2098 * KVM contexts as the original, and they may have different
2099 * memory-views.
2100 */
2101
2102 e_unlock:
2103 sev_unlock_two_vms(kvm, source_kvm);
2104 e_source_fput:
2105 if (source_kvm_file)
2106 fput(source_kvm_file);
2107 return ret;
2108 }
2109
sev_vm_destroy(struct kvm * kvm)2110 void sev_vm_destroy(struct kvm *kvm)
2111 {
2112 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
2113 struct list_head *head = &sev->regions_list;
2114 struct list_head *pos, *q;
2115
2116 if (!sev_guest(kvm))
2117 return;
2118
2119 WARN_ON(!list_empty(&sev->mirror_vms));
2120
2121 /* If this is a mirror_kvm release the enc_context_owner and skip sev cleanup */
2122 if (is_mirroring_enc_context(kvm)) {
2123 struct kvm *owner_kvm = sev->enc_context_owner;
2124
2125 mutex_lock(&owner_kvm->lock);
2126 list_del(&sev->mirror_entry);
2127 mutex_unlock(&owner_kvm->lock);
2128 kvm_put_kvm(owner_kvm);
2129 return;
2130 }
2131
2132 /*
2133 * Ensure that all guest tagged cache entries are flushed before
2134 * releasing the pages back to the system for use. CLFLUSH will
2135 * not do this, so issue a WBINVD.
2136 */
2137 wbinvd_on_all_cpus();
2138
2139 /*
2140 * if userspace was terminated before unregistering the memory regions
2141 * then lets unpin all the registered memory.
2142 */
2143 if (!list_empty(head)) {
2144 list_for_each_safe(pos, q, head) {
2145 __unregister_enc_region_locked(kvm,
2146 list_entry(pos, struct enc_region, list));
2147 cond_resched();
2148 }
2149 }
2150
2151 sev_unbind_asid(kvm, sev->handle);
2152 sev_asid_free(sev);
2153 }
2154
sev_set_cpu_caps(void)2155 void __init sev_set_cpu_caps(void)
2156 {
2157 if (!sev_enabled)
2158 kvm_cpu_cap_clear(X86_FEATURE_SEV);
2159 if (!sev_es_enabled)
2160 kvm_cpu_cap_clear(X86_FEATURE_SEV_ES);
2161 }
2162
sev_hardware_setup(void)2163 void __init sev_hardware_setup(void)
2164 {
2165 #ifdef CONFIG_KVM_AMD_SEV
2166 unsigned int eax, ebx, ecx, edx, sev_asid_count, sev_es_asid_count;
2167 bool sev_es_supported = false;
2168 bool sev_supported = false;
2169
2170 if (!sev_enabled || !npt_enabled)
2171 goto out;
2172
2173 /*
2174 * SEV must obviously be supported in hardware. Sanity check that the
2175 * CPU supports decode assists, which is mandatory for SEV guests to
2176 * support instruction emulation.
2177 */
2178 if (!boot_cpu_has(X86_FEATURE_SEV) ||
2179 WARN_ON_ONCE(!boot_cpu_has(X86_FEATURE_DECODEASSISTS)))
2180 goto out;
2181
2182 /* Retrieve SEV CPUID information */
2183 cpuid(0x8000001f, &eax, &ebx, &ecx, &edx);
2184
2185 /* Set encryption bit location for SEV-ES guests */
2186 sev_enc_bit = ebx & 0x3f;
2187
2188 /* Maximum number of encrypted guests supported simultaneously */
2189 max_sev_asid = ecx;
2190 if (!max_sev_asid)
2191 goto out;
2192
2193 /* Minimum ASID value that should be used for SEV guest */
2194 min_sev_asid = edx;
2195 sev_me_mask = 1UL << (ebx & 0x3f);
2196
2197 /*
2198 * Initialize SEV ASID bitmaps. Allocate space for ASID 0 in the bitmap,
2199 * even though it's never used, so that the bitmap is indexed by the
2200 * actual ASID.
2201 */
2202 nr_asids = max_sev_asid + 1;
2203 sev_asid_bitmap = bitmap_zalloc(nr_asids, GFP_KERNEL);
2204 if (!sev_asid_bitmap)
2205 goto out;
2206
2207 sev_reclaim_asid_bitmap = bitmap_zalloc(nr_asids, GFP_KERNEL);
2208 if (!sev_reclaim_asid_bitmap) {
2209 bitmap_free(sev_asid_bitmap);
2210 sev_asid_bitmap = NULL;
2211 goto out;
2212 }
2213
2214 sev_asid_count = max_sev_asid - min_sev_asid + 1;
2215 if (misc_cg_set_capacity(MISC_CG_RES_SEV, sev_asid_count))
2216 goto out;
2217
2218 pr_info("SEV supported: %u ASIDs\n", sev_asid_count);
2219 sev_supported = true;
2220
2221 /* SEV-ES support requested? */
2222 if (!sev_es_enabled)
2223 goto out;
2224
2225 /*
2226 * SEV-ES requires MMIO caching as KVM doesn't have access to the guest
2227 * instruction stream, i.e. can't emulate in response to a #NPF and
2228 * instead relies on #NPF(RSVD) being reflected into the guest as #VC
2229 * (the guest can then do a #VMGEXIT to request MMIO emulation).
2230 */
2231 if (!enable_mmio_caching)
2232 goto out;
2233
2234 /* Does the CPU support SEV-ES? */
2235 if (!boot_cpu_has(X86_FEATURE_SEV_ES))
2236 goto out;
2237
2238 /* Has the system been allocated ASIDs for SEV-ES? */
2239 if (min_sev_asid == 1)
2240 goto out;
2241
2242 sev_es_asid_count = min_sev_asid - 1;
2243 if (misc_cg_set_capacity(MISC_CG_RES_SEV_ES, sev_es_asid_count))
2244 goto out;
2245
2246 pr_info("SEV-ES supported: %u ASIDs\n", sev_es_asid_count);
2247 sev_es_supported = true;
2248
2249 out:
2250 sev_enabled = sev_supported;
2251 sev_es_enabled = sev_es_supported;
2252 #endif
2253 }
2254
sev_hardware_unsetup(void)2255 void sev_hardware_unsetup(void)
2256 {
2257 if (!sev_enabled)
2258 return;
2259
2260 /* No need to take sev_bitmap_lock, all VMs have been destroyed. */
2261 sev_flush_asids(1, max_sev_asid);
2262
2263 bitmap_free(sev_asid_bitmap);
2264 bitmap_free(sev_reclaim_asid_bitmap);
2265
2266 misc_cg_set_capacity(MISC_CG_RES_SEV, 0);
2267 misc_cg_set_capacity(MISC_CG_RES_SEV_ES, 0);
2268 }
2269
sev_cpu_init(struct svm_cpu_data * sd)2270 int sev_cpu_init(struct svm_cpu_data *sd)
2271 {
2272 if (!sev_enabled)
2273 return 0;
2274
2275 sd->sev_vmcbs = kcalloc(nr_asids, sizeof(void *), GFP_KERNEL);
2276 if (!sd->sev_vmcbs)
2277 return -ENOMEM;
2278
2279 return 0;
2280 }
2281
2282 /*
2283 * Pages used by hardware to hold guest encrypted state must be flushed before
2284 * returning them to the system.
2285 */
sev_flush_encrypted_page(struct kvm_vcpu * vcpu,void * va)2286 static void sev_flush_encrypted_page(struct kvm_vcpu *vcpu, void *va)
2287 {
2288 int asid = to_kvm_svm(vcpu->kvm)->sev_info.asid;
2289
2290 /*
2291 * Note! The address must be a kernel address, as regular page walk
2292 * checks are performed by VM_PAGE_FLUSH, i.e. operating on a user
2293 * address is non-deterministic and unsafe. This function deliberately
2294 * takes a pointer to deter passing in a user address.
2295 */
2296 unsigned long addr = (unsigned long)va;
2297
2298 /*
2299 * If CPU enforced cache coherency for encrypted mappings of the
2300 * same physical page is supported, use CLFLUSHOPT instead. NOTE: cache
2301 * flush is still needed in order to work properly with DMA devices.
2302 */
2303 if (boot_cpu_has(X86_FEATURE_SME_COHERENT)) {
2304 clflush_cache_range(va, PAGE_SIZE);
2305 return;
2306 }
2307
2308 /*
2309 * VM Page Flush takes a host virtual address and a guest ASID. Fall
2310 * back to WBINVD if this faults so as not to make any problems worse
2311 * by leaving stale encrypted data in the cache.
2312 */
2313 if (WARN_ON_ONCE(wrmsrl_safe(MSR_AMD64_VM_PAGE_FLUSH, addr | asid)))
2314 goto do_wbinvd;
2315
2316 return;
2317
2318 do_wbinvd:
2319 wbinvd_on_all_cpus();
2320 }
2321
sev_guest_memory_reclaimed(struct kvm * kvm)2322 void sev_guest_memory_reclaimed(struct kvm *kvm)
2323 {
2324 if (!sev_guest(kvm))
2325 return;
2326
2327 wbinvd_on_all_cpus();
2328 }
2329
sev_free_vcpu(struct kvm_vcpu * vcpu)2330 void sev_free_vcpu(struct kvm_vcpu *vcpu)
2331 {
2332 struct vcpu_svm *svm;
2333
2334 if (!sev_es_guest(vcpu->kvm))
2335 return;
2336
2337 svm = to_svm(vcpu);
2338
2339 if (vcpu->arch.guest_state_protected)
2340 sev_flush_encrypted_page(vcpu, svm->sev_es.vmsa);
2341
2342 __free_page(virt_to_page(svm->sev_es.vmsa));
2343
2344 if (svm->sev_es.ghcb_sa_free)
2345 kvfree(svm->sev_es.ghcb_sa);
2346 }
2347
dump_ghcb(struct vcpu_svm * svm)2348 static void dump_ghcb(struct vcpu_svm *svm)
2349 {
2350 struct ghcb *ghcb = svm->sev_es.ghcb;
2351 unsigned int nbits;
2352
2353 /* Re-use the dump_invalid_vmcb module parameter */
2354 if (!dump_invalid_vmcb) {
2355 pr_warn_ratelimited("set kvm_amd.dump_invalid_vmcb=1 to dump internal KVM state.\n");
2356 return;
2357 }
2358
2359 nbits = sizeof(ghcb->save.valid_bitmap) * 8;
2360
2361 pr_err("GHCB (GPA=%016llx):\n", svm->vmcb->control.ghcb_gpa);
2362 pr_err("%-20s%016llx is_valid: %u\n", "sw_exit_code",
2363 ghcb->save.sw_exit_code, ghcb_sw_exit_code_is_valid(ghcb));
2364 pr_err("%-20s%016llx is_valid: %u\n", "sw_exit_info_1",
2365 ghcb->save.sw_exit_info_1, ghcb_sw_exit_info_1_is_valid(ghcb));
2366 pr_err("%-20s%016llx is_valid: %u\n", "sw_exit_info_2",
2367 ghcb->save.sw_exit_info_2, ghcb_sw_exit_info_2_is_valid(ghcb));
2368 pr_err("%-20s%016llx is_valid: %u\n", "sw_scratch",
2369 ghcb->save.sw_scratch, ghcb_sw_scratch_is_valid(ghcb));
2370 pr_err("%-20s%*pb\n", "valid_bitmap", nbits, ghcb->save.valid_bitmap);
2371 }
2372
sev_es_sync_to_ghcb(struct vcpu_svm * svm)2373 static void sev_es_sync_to_ghcb(struct vcpu_svm *svm)
2374 {
2375 struct kvm_vcpu *vcpu = &svm->vcpu;
2376 struct ghcb *ghcb = svm->sev_es.ghcb;
2377
2378 /*
2379 * The GHCB protocol so far allows for the following data
2380 * to be returned:
2381 * GPRs RAX, RBX, RCX, RDX
2382 *
2383 * Copy their values, even if they may not have been written during the
2384 * VM-Exit. It's the guest's responsibility to not consume random data.
2385 */
2386 ghcb_set_rax(ghcb, vcpu->arch.regs[VCPU_REGS_RAX]);
2387 ghcb_set_rbx(ghcb, vcpu->arch.regs[VCPU_REGS_RBX]);
2388 ghcb_set_rcx(ghcb, vcpu->arch.regs[VCPU_REGS_RCX]);
2389 ghcb_set_rdx(ghcb, vcpu->arch.regs[VCPU_REGS_RDX]);
2390 }
2391
sev_es_sync_from_ghcb(struct vcpu_svm * svm)2392 static void sev_es_sync_from_ghcb(struct vcpu_svm *svm)
2393 {
2394 struct vmcb_control_area *control = &svm->vmcb->control;
2395 struct kvm_vcpu *vcpu = &svm->vcpu;
2396 struct ghcb *ghcb = svm->sev_es.ghcb;
2397 u64 exit_code;
2398
2399 /*
2400 * The GHCB protocol so far allows for the following data
2401 * to be supplied:
2402 * GPRs RAX, RBX, RCX, RDX
2403 * XCR0
2404 * CPL
2405 *
2406 * VMMCALL allows the guest to provide extra registers. KVM also
2407 * expects RSI for hypercalls, so include that, too.
2408 *
2409 * Copy their values to the appropriate location if supplied.
2410 */
2411 memset(vcpu->arch.regs, 0, sizeof(vcpu->arch.regs));
2412
2413 vcpu->arch.regs[VCPU_REGS_RAX] = ghcb_get_rax_if_valid(ghcb);
2414 vcpu->arch.regs[VCPU_REGS_RBX] = ghcb_get_rbx_if_valid(ghcb);
2415 vcpu->arch.regs[VCPU_REGS_RCX] = ghcb_get_rcx_if_valid(ghcb);
2416 vcpu->arch.regs[VCPU_REGS_RDX] = ghcb_get_rdx_if_valid(ghcb);
2417 vcpu->arch.regs[VCPU_REGS_RSI] = ghcb_get_rsi_if_valid(ghcb);
2418
2419 svm->vmcb->save.cpl = ghcb_get_cpl_if_valid(ghcb);
2420
2421 if (ghcb_xcr0_is_valid(ghcb)) {
2422 vcpu->arch.xcr0 = ghcb_get_xcr0(ghcb);
2423 kvm_update_cpuid_runtime(vcpu);
2424 }
2425
2426 /* Copy the GHCB exit information into the VMCB fields */
2427 exit_code = ghcb_get_sw_exit_code(ghcb);
2428 control->exit_code = lower_32_bits(exit_code);
2429 control->exit_code_hi = upper_32_bits(exit_code);
2430 control->exit_info_1 = ghcb_get_sw_exit_info_1(ghcb);
2431 control->exit_info_2 = ghcb_get_sw_exit_info_2(ghcb);
2432
2433 /* Clear the valid entries fields */
2434 memset(ghcb->save.valid_bitmap, 0, sizeof(ghcb->save.valid_bitmap));
2435 }
2436
sev_es_validate_vmgexit(struct vcpu_svm * svm)2437 static int sev_es_validate_vmgexit(struct vcpu_svm *svm)
2438 {
2439 struct kvm_vcpu *vcpu;
2440 struct ghcb *ghcb;
2441 u64 exit_code;
2442 u64 reason;
2443
2444 ghcb = svm->sev_es.ghcb;
2445
2446 /*
2447 * Retrieve the exit code now even though it may not be marked valid
2448 * as it could help with debugging.
2449 */
2450 exit_code = ghcb_get_sw_exit_code(ghcb);
2451
2452 /* Only GHCB Usage code 0 is supported */
2453 if (ghcb->ghcb_usage) {
2454 reason = GHCB_ERR_INVALID_USAGE;
2455 goto vmgexit_err;
2456 }
2457
2458 reason = GHCB_ERR_MISSING_INPUT;
2459
2460 if (!ghcb_sw_exit_code_is_valid(ghcb) ||
2461 !ghcb_sw_exit_info_1_is_valid(ghcb) ||
2462 !ghcb_sw_exit_info_2_is_valid(ghcb))
2463 goto vmgexit_err;
2464
2465 switch (ghcb_get_sw_exit_code(ghcb)) {
2466 case SVM_EXIT_READ_DR7:
2467 break;
2468 case SVM_EXIT_WRITE_DR7:
2469 if (!ghcb_rax_is_valid(ghcb))
2470 goto vmgexit_err;
2471 break;
2472 case SVM_EXIT_RDTSC:
2473 break;
2474 case SVM_EXIT_RDPMC:
2475 if (!ghcb_rcx_is_valid(ghcb))
2476 goto vmgexit_err;
2477 break;
2478 case SVM_EXIT_CPUID:
2479 if (!ghcb_rax_is_valid(ghcb) ||
2480 !ghcb_rcx_is_valid(ghcb))
2481 goto vmgexit_err;
2482 if (ghcb_get_rax(ghcb) == 0xd)
2483 if (!ghcb_xcr0_is_valid(ghcb))
2484 goto vmgexit_err;
2485 break;
2486 case SVM_EXIT_INVD:
2487 break;
2488 case SVM_EXIT_IOIO:
2489 if (ghcb_get_sw_exit_info_1(ghcb) & SVM_IOIO_STR_MASK) {
2490 if (!ghcb_sw_scratch_is_valid(ghcb))
2491 goto vmgexit_err;
2492 } else {
2493 if (!(ghcb_get_sw_exit_info_1(ghcb) & SVM_IOIO_TYPE_MASK))
2494 if (!ghcb_rax_is_valid(ghcb))
2495 goto vmgexit_err;
2496 }
2497 break;
2498 case SVM_EXIT_MSR:
2499 if (!ghcb_rcx_is_valid(ghcb))
2500 goto vmgexit_err;
2501 if (ghcb_get_sw_exit_info_1(ghcb)) {
2502 if (!ghcb_rax_is_valid(ghcb) ||
2503 !ghcb_rdx_is_valid(ghcb))
2504 goto vmgexit_err;
2505 }
2506 break;
2507 case SVM_EXIT_VMMCALL:
2508 if (!ghcb_rax_is_valid(ghcb) ||
2509 !ghcb_cpl_is_valid(ghcb))
2510 goto vmgexit_err;
2511 break;
2512 case SVM_EXIT_RDTSCP:
2513 break;
2514 case SVM_EXIT_WBINVD:
2515 break;
2516 case SVM_EXIT_MONITOR:
2517 if (!ghcb_rax_is_valid(ghcb) ||
2518 !ghcb_rcx_is_valid(ghcb) ||
2519 !ghcb_rdx_is_valid(ghcb))
2520 goto vmgexit_err;
2521 break;
2522 case SVM_EXIT_MWAIT:
2523 if (!ghcb_rax_is_valid(ghcb) ||
2524 !ghcb_rcx_is_valid(ghcb))
2525 goto vmgexit_err;
2526 break;
2527 case SVM_VMGEXIT_MMIO_READ:
2528 case SVM_VMGEXIT_MMIO_WRITE:
2529 if (!ghcb_sw_scratch_is_valid(ghcb))
2530 goto vmgexit_err;
2531 break;
2532 case SVM_VMGEXIT_NMI_COMPLETE:
2533 case SVM_VMGEXIT_AP_HLT_LOOP:
2534 case SVM_VMGEXIT_AP_JUMP_TABLE:
2535 case SVM_VMGEXIT_UNSUPPORTED_EVENT:
2536 break;
2537 default:
2538 reason = GHCB_ERR_INVALID_EVENT;
2539 goto vmgexit_err;
2540 }
2541
2542 return 0;
2543
2544 vmgexit_err:
2545 vcpu = &svm->vcpu;
2546
2547 if (reason == GHCB_ERR_INVALID_USAGE) {
2548 vcpu_unimpl(vcpu, "vmgexit: ghcb usage %#x is not valid\n",
2549 ghcb->ghcb_usage);
2550 } else if (reason == GHCB_ERR_INVALID_EVENT) {
2551 vcpu_unimpl(vcpu, "vmgexit: exit code %#llx is not valid\n",
2552 exit_code);
2553 } else {
2554 vcpu_unimpl(vcpu, "vmgexit: exit code %#llx input is not valid\n",
2555 exit_code);
2556 dump_ghcb(svm);
2557 }
2558
2559 /* Clear the valid entries fields */
2560 memset(ghcb->save.valid_bitmap, 0, sizeof(ghcb->save.valid_bitmap));
2561
2562 ghcb_set_sw_exit_info_1(ghcb, 2);
2563 ghcb_set_sw_exit_info_2(ghcb, reason);
2564
2565 /* Resume the guest to "return" the error code. */
2566 return 1;
2567 }
2568
sev_es_unmap_ghcb(struct vcpu_svm * svm)2569 void sev_es_unmap_ghcb(struct vcpu_svm *svm)
2570 {
2571 if (!svm->sev_es.ghcb)
2572 return;
2573
2574 if (svm->sev_es.ghcb_sa_free) {
2575 /*
2576 * The scratch area lives outside the GHCB, so there is a
2577 * buffer that, depending on the operation performed, may
2578 * need to be synced, then freed.
2579 */
2580 if (svm->sev_es.ghcb_sa_sync) {
2581 kvm_write_guest(svm->vcpu.kvm,
2582 ghcb_get_sw_scratch(svm->sev_es.ghcb),
2583 svm->sev_es.ghcb_sa,
2584 svm->sev_es.ghcb_sa_len);
2585 svm->sev_es.ghcb_sa_sync = false;
2586 }
2587
2588 kvfree(svm->sev_es.ghcb_sa);
2589 svm->sev_es.ghcb_sa = NULL;
2590 svm->sev_es.ghcb_sa_free = false;
2591 }
2592
2593 trace_kvm_vmgexit_exit(svm->vcpu.vcpu_id, svm->sev_es.ghcb);
2594
2595 sev_es_sync_to_ghcb(svm);
2596
2597 kvm_vcpu_unmap(&svm->vcpu, &svm->sev_es.ghcb_map, true);
2598 svm->sev_es.ghcb = NULL;
2599 }
2600
pre_sev_run(struct vcpu_svm * svm,int cpu)2601 void pre_sev_run(struct vcpu_svm *svm, int cpu)
2602 {
2603 struct svm_cpu_data *sd = per_cpu(svm_data, cpu);
2604 int asid = sev_get_asid(svm->vcpu.kvm);
2605
2606 /* Assign the asid allocated with this SEV guest */
2607 svm->asid = asid;
2608
2609 /*
2610 * Flush guest TLB:
2611 *
2612 * 1) when different VMCB for the same ASID is to be run on the same host CPU.
2613 * 2) or this VMCB was executed on different host CPU in previous VMRUNs.
2614 */
2615 if (sd->sev_vmcbs[asid] == svm->vmcb &&
2616 svm->vcpu.arch.last_vmentry_cpu == cpu)
2617 return;
2618
2619 sd->sev_vmcbs[asid] = svm->vmcb;
2620 svm->vmcb->control.tlb_ctl = TLB_CONTROL_FLUSH_ASID;
2621 vmcb_mark_dirty(svm->vmcb, VMCB_ASID);
2622 }
2623
2624 #define GHCB_SCRATCH_AREA_LIMIT (16ULL * PAGE_SIZE)
setup_vmgexit_scratch(struct vcpu_svm * svm,bool sync,u64 len)2625 static int setup_vmgexit_scratch(struct vcpu_svm *svm, bool sync, u64 len)
2626 {
2627 struct vmcb_control_area *control = &svm->vmcb->control;
2628 struct ghcb *ghcb = svm->sev_es.ghcb;
2629 u64 ghcb_scratch_beg, ghcb_scratch_end;
2630 u64 scratch_gpa_beg, scratch_gpa_end;
2631 void *scratch_va;
2632
2633 scratch_gpa_beg = ghcb_get_sw_scratch(ghcb);
2634 if (!scratch_gpa_beg) {
2635 pr_err("vmgexit: scratch gpa not provided\n");
2636 goto e_scratch;
2637 }
2638
2639 scratch_gpa_end = scratch_gpa_beg + len;
2640 if (scratch_gpa_end < scratch_gpa_beg) {
2641 pr_err("vmgexit: scratch length (%#llx) not valid for scratch address (%#llx)\n",
2642 len, scratch_gpa_beg);
2643 goto e_scratch;
2644 }
2645
2646 if ((scratch_gpa_beg & PAGE_MASK) == control->ghcb_gpa) {
2647 /* Scratch area begins within GHCB */
2648 ghcb_scratch_beg = control->ghcb_gpa +
2649 offsetof(struct ghcb, shared_buffer);
2650 ghcb_scratch_end = control->ghcb_gpa +
2651 offsetof(struct ghcb, reserved_1);
2652
2653 /*
2654 * If the scratch area begins within the GHCB, it must be
2655 * completely contained in the GHCB shared buffer area.
2656 */
2657 if (scratch_gpa_beg < ghcb_scratch_beg ||
2658 scratch_gpa_end > ghcb_scratch_end) {
2659 pr_err("vmgexit: scratch area is outside of GHCB shared buffer area (%#llx - %#llx)\n",
2660 scratch_gpa_beg, scratch_gpa_end);
2661 goto e_scratch;
2662 }
2663
2664 scratch_va = (void *)svm->sev_es.ghcb;
2665 scratch_va += (scratch_gpa_beg - control->ghcb_gpa);
2666 } else {
2667 /*
2668 * The guest memory must be read into a kernel buffer, so
2669 * limit the size
2670 */
2671 if (len > GHCB_SCRATCH_AREA_LIMIT) {
2672 pr_err("vmgexit: scratch area exceeds KVM limits (%#llx requested, %#llx limit)\n",
2673 len, GHCB_SCRATCH_AREA_LIMIT);
2674 goto e_scratch;
2675 }
2676 scratch_va = kvzalloc(len, GFP_KERNEL_ACCOUNT);
2677 if (!scratch_va)
2678 return -ENOMEM;
2679
2680 if (kvm_read_guest(svm->vcpu.kvm, scratch_gpa_beg, scratch_va, len)) {
2681 /* Unable to copy scratch area from guest */
2682 pr_err("vmgexit: kvm_read_guest for scratch area failed\n");
2683
2684 kvfree(scratch_va);
2685 return -EFAULT;
2686 }
2687
2688 /*
2689 * The scratch area is outside the GHCB. The operation will
2690 * dictate whether the buffer needs to be synced before running
2691 * the vCPU next time (i.e. a read was requested so the data
2692 * must be written back to the guest memory).
2693 */
2694 svm->sev_es.ghcb_sa_sync = sync;
2695 svm->sev_es.ghcb_sa_free = true;
2696 }
2697
2698 svm->sev_es.ghcb_sa = scratch_va;
2699 svm->sev_es.ghcb_sa_len = len;
2700
2701 return 0;
2702
2703 e_scratch:
2704 ghcb_set_sw_exit_info_1(ghcb, 2);
2705 ghcb_set_sw_exit_info_2(ghcb, GHCB_ERR_INVALID_SCRATCH_AREA);
2706
2707 return 1;
2708 }
2709
set_ghcb_msr_bits(struct vcpu_svm * svm,u64 value,u64 mask,unsigned int pos)2710 static void set_ghcb_msr_bits(struct vcpu_svm *svm, u64 value, u64 mask,
2711 unsigned int pos)
2712 {
2713 svm->vmcb->control.ghcb_gpa &= ~(mask << pos);
2714 svm->vmcb->control.ghcb_gpa |= (value & mask) << pos;
2715 }
2716
get_ghcb_msr_bits(struct vcpu_svm * svm,u64 mask,unsigned int pos)2717 static u64 get_ghcb_msr_bits(struct vcpu_svm *svm, u64 mask, unsigned int pos)
2718 {
2719 return (svm->vmcb->control.ghcb_gpa >> pos) & mask;
2720 }
2721
set_ghcb_msr(struct vcpu_svm * svm,u64 value)2722 static void set_ghcb_msr(struct vcpu_svm *svm, u64 value)
2723 {
2724 svm->vmcb->control.ghcb_gpa = value;
2725 }
2726
sev_handle_vmgexit_msr_protocol(struct vcpu_svm * svm)2727 static int sev_handle_vmgexit_msr_protocol(struct vcpu_svm *svm)
2728 {
2729 struct vmcb_control_area *control = &svm->vmcb->control;
2730 struct kvm_vcpu *vcpu = &svm->vcpu;
2731 u64 ghcb_info;
2732 int ret = 1;
2733
2734 ghcb_info = control->ghcb_gpa & GHCB_MSR_INFO_MASK;
2735
2736 trace_kvm_vmgexit_msr_protocol_enter(svm->vcpu.vcpu_id,
2737 control->ghcb_gpa);
2738
2739 switch (ghcb_info) {
2740 case GHCB_MSR_SEV_INFO_REQ:
2741 set_ghcb_msr(svm, GHCB_MSR_SEV_INFO(GHCB_VERSION_MAX,
2742 GHCB_VERSION_MIN,
2743 sev_enc_bit));
2744 break;
2745 case GHCB_MSR_CPUID_REQ: {
2746 u64 cpuid_fn, cpuid_reg, cpuid_value;
2747
2748 cpuid_fn = get_ghcb_msr_bits(svm,
2749 GHCB_MSR_CPUID_FUNC_MASK,
2750 GHCB_MSR_CPUID_FUNC_POS);
2751
2752 /* Initialize the registers needed by the CPUID intercept */
2753 vcpu->arch.regs[VCPU_REGS_RAX] = cpuid_fn;
2754 vcpu->arch.regs[VCPU_REGS_RCX] = 0;
2755
2756 ret = svm_invoke_exit_handler(vcpu, SVM_EXIT_CPUID);
2757 if (!ret) {
2758 /* Error, keep GHCB MSR value as-is */
2759 break;
2760 }
2761
2762 cpuid_reg = get_ghcb_msr_bits(svm,
2763 GHCB_MSR_CPUID_REG_MASK,
2764 GHCB_MSR_CPUID_REG_POS);
2765 if (cpuid_reg == 0)
2766 cpuid_value = vcpu->arch.regs[VCPU_REGS_RAX];
2767 else if (cpuid_reg == 1)
2768 cpuid_value = vcpu->arch.regs[VCPU_REGS_RBX];
2769 else if (cpuid_reg == 2)
2770 cpuid_value = vcpu->arch.regs[VCPU_REGS_RCX];
2771 else
2772 cpuid_value = vcpu->arch.regs[VCPU_REGS_RDX];
2773
2774 set_ghcb_msr_bits(svm, cpuid_value,
2775 GHCB_MSR_CPUID_VALUE_MASK,
2776 GHCB_MSR_CPUID_VALUE_POS);
2777
2778 set_ghcb_msr_bits(svm, GHCB_MSR_CPUID_RESP,
2779 GHCB_MSR_INFO_MASK,
2780 GHCB_MSR_INFO_POS);
2781 break;
2782 }
2783 case GHCB_MSR_TERM_REQ: {
2784 u64 reason_set, reason_code;
2785
2786 reason_set = get_ghcb_msr_bits(svm,
2787 GHCB_MSR_TERM_REASON_SET_MASK,
2788 GHCB_MSR_TERM_REASON_SET_POS);
2789 reason_code = get_ghcb_msr_bits(svm,
2790 GHCB_MSR_TERM_REASON_MASK,
2791 GHCB_MSR_TERM_REASON_POS);
2792 pr_info("SEV-ES guest requested termination: %#llx:%#llx\n",
2793 reason_set, reason_code);
2794
2795 vcpu->run->exit_reason = KVM_EXIT_SYSTEM_EVENT;
2796 vcpu->run->system_event.type = KVM_SYSTEM_EVENT_SEV_TERM;
2797 vcpu->run->system_event.ndata = 1;
2798 vcpu->run->system_event.data[0] = control->ghcb_gpa;
2799
2800 return 0;
2801 }
2802 default:
2803 /* Error, keep GHCB MSR value as-is */
2804 break;
2805 }
2806
2807 trace_kvm_vmgexit_msr_protocol_exit(svm->vcpu.vcpu_id,
2808 control->ghcb_gpa, ret);
2809
2810 return ret;
2811 }
2812
sev_handle_vmgexit(struct kvm_vcpu * vcpu)2813 int sev_handle_vmgexit(struct kvm_vcpu *vcpu)
2814 {
2815 struct vcpu_svm *svm = to_svm(vcpu);
2816 struct vmcb_control_area *control = &svm->vmcb->control;
2817 u64 ghcb_gpa, exit_code;
2818 struct ghcb *ghcb;
2819 int ret;
2820
2821 /* Validate the GHCB */
2822 ghcb_gpa = control->ghcb_gpa;
2823 if (ghcb_gpa & GHCB_MSR_INFO_MASK)
2824 return sev_handle_vmgexit_msr_protocol(svm);
2825
2826 if (!ghcb_gpa) {
2827 vcpu_unimpl(vcpu, "vmgexit: GHCB gpa is not set\n");
2828
2829 /* Without a GHCB, just return right back to the guest */
2830 return 1;
2831 }
2832
2833 if (kvm_vcpu_map(vcpu, ghcb_gpa >> PAGE_SHIFT, &svm->sev_es.ghcb_map)) {
2834 /* Unable to map GHCB from guest */
2835 vcpu_unimpl(vcpu, "vmgexit: error mapping GHCB [%#llx] from guest\n",
2836 ghcb_gpa);
2837
2838 /* Without a GHCB, just return right back to the guest */
2839 return 1;
2840 }
2841
2842 svm->sev_es.ghcb = svm->sev_es.ghcb_map.hva;
2843 ghcb = svm->sev_es.ghcb_map.hva;
2844
2845 trace_kvm_vmgexit_enter(vcpu->vcpu_id, ghcb);
2846
2847 exit_code = ghcb_get_sw_exit_code(ghcb);
2848
2849 ret = sev_es_validate_vmgexit(svm);
2850 if (ret)
2851 return ret;
2852
2853 sev_es_sync_from_ghcb(svm);
2854 ghcb_set_sw_exit_info_1(ghcb, 0);
2855 ghcb_set_sw_exit_info_2(ghcb, 0);
2856
2857 switch (exit_code) {
2858 case SVM_VMGEXIT_MMIO_READ:
2859 ret = setup_vmgexit_scratch(svm, true, control->exit_info_2);
2860 if (ret)
2861 break;
2862
2863 ret = kvm_sev_es_mmio_read(vcpu,
2864 control->exit_info_1,
2865 control->exit_info_2,
2866 svm->sev_es.ghcb_sa);
2867 break;
2868 case SVM_VMGEXIT_MMIO_WRITE:
2869 ret = setup_vmgexit_scratch(svm, false, control->exit_info_2);
2870 if (ret)
2871 break;
2872
2873 ret = kvm_sev_es_mmio_write(vcpu,
2874 control->exit_info_1,
2875 control->exit_info_2,
2876 svm->sev_es.ghcb_sa);
2877 break;
2878 case SVM_VMGEXIT_NMI_COMPLETE:
2879 ret = svm_invoke_exit_handler(vcpu, SVM_EXIT_IRET);
2880 break;
2881 case SVM_VMGEXIT_AP_HLT_LOOP:
2882 ret = kvm_emulate_ap_reset_hold(vcpu);
2883 break;
2884 case SVM_VMGEXIT_AP_JUMP_TABLE: {
2885 struct kvm_sev_info *sev = &to_kvm_svm(vcpu->kvm)->sev_info;
2886
2887 switch (control->exit_info_1) {
2888 case 0:
2889 /* Set AP jump table address */
2890 sev->ap_jump_table = control->exit_info_2;
2891 break;
2892 case 1:
2893 /* Get AP jump table address */
2894 ghcb_set_sw_exit_info_2(ghcb, sev->ap_jump_table);
2895 break;
2896 default:
2897 pr_err("svm: vmgexit: unsupported AP jump table request - exit_info_1=%#llx\n",
2898 control->exit_info_1);
2899 ghcb_set_sw_exit_info_1(ghcb, 2);
2900 ghcb_set_sw_exit_info_2(ghcb, GHCB_ERR_INVALID_INPUT);
2901 }
2902
2903 ret = 1;
2904 break;
2905 }
2906 case SVM_VMGEXIT_UNSUPPORTED_EVENT:
2907 vcpu_unimpl(vcpu,
2908 "vmgexit: unsupported event - exit_info_1=%#llx, exit_info_2=%#llx\n",
2909 control->exit_info_1, control->exit_info_2);
2910 ret = -EINVAL;
2911 break;
2912 default:
2913 ret = svm_invoke_exit_handler(vcpu, exit_code);
2914 }
2915
2916 return ret;
2917 }
2918
sev_es_string_io(struct vcpu_svm * svm,int size,unsigned int port,int in)2919 int sev_es_string_io(struct vcpu_svm *svm, int size, unsigned int port, int in)
2920 {
2921 int count;
2922 int bytes;
2923 int r;
2924
2925 if (svm->vmcb->control.exit_info_2 > INT_MAX)
2926 return -EINVAL;
2927
2928 count = svm->vmcb->control.exit_info_2;
2929 if (unlikely(check_mul_overflow(count, size, &bytes)))
2930 return -EINVAL;
2931
2932 r = setup_vmgexit_scratch(svm, in, bytes);
2933 if (r)
2934 return r;
2935
2936 return kvm_sev_es_string_io(&svm->vcpu, size, port, svm->sev_es.ghcb_sa,
2937 count, in);
2938 }
2939
sev_es_init_vmcb(struct vcpu_svm * svm)2940 static void sev_es_init_vmcb(struct vcpu_svm *svm)
2941 {
2942 struct kvm_vcpu *vcpu = &svm->vcpu;
2943
2944 svm->vmcb->control.nested_ctl |= SVM_NESTED_CTL_SEV_ES_ENABLE;
2945 svm->vmcb->control.virt_ext |= LBR_CTL_ENABLE_MASK;
2946
2947 /*
2948 * An SEV-ES guest requires a VMSA area that is a separate from the
2949 * VMCB page. Do not include the encryption mask on the VMSA physical
2950 * address since hardware will access it using the guest key.
2951 */
2952 svm->vmcb->control.vmsa_pa = __pa(svm->sev_es.vmsa);
2953
2954 /* Can't intercept CR register access, HV can't modify CR registers */
2955 svm_clr_intercept(svm, INTERCEPT_CR0_READ);
2956 svm_clr_intercept(svm, INTERCEPT_CR4_READ);
2957 svm_clr_intercept(svm, INTERCEPT_CR8_READ);
2958 svm_clr_intercept(svm, INTERCEPT_CR0_WRITE);
2959 svm_clr_intercept(svm, INTERCEPT_CR4_WRITE);
2960 svm_clr_intercept(svm, INTERCEPT_CR8_WRITE);
2961
2962 svm_clr_intercept(svm, INTERCEPT_SELECTIVE_CR0);
2963
2964 /* Track EFER/CR register changes */
2965 svm_set_intercept(svm, TRAP_EFER_WRITE);
2966 svm_set_intercept(svm, TRAP_CR0_WRITE);
2967 svm_set_intercept(svm, TRAP_CR4_WRITE);
2968 svm_set_intercept(svm, TRAP_CR8_WRITE);
2969
2970 /* No support for enable_vmware_backdoor */
2971 clr_exception_intercept(svm, GP_VECTOR);
2972
2973 /* Can't intercept XSETBV, HV can't modify XCR0 directly */
2974 svm_clr_intercept(svm, INTERCEPT_XSETBV);
2975
2976 /* Clear intercepts on selected MSRs */
2977 set_msr_interception(vcpu, svm->msrpm, MSR_EFER, 1, 1);
2978 set_msr_interception(vcpu, svm->msrpm, MSR_IA32_CR_PAT, 1, 1);
2979 set_msr_interception(vcpu, svm->msrpm, MSR_IA32_LASTBRANCHFROMIP, 1, 1);
2980 set_msr_interception(vcpu, svm->msrpm, MSR_IA32_LASTBRANCHTOIP, 1, 1);
2981 set_msr_interception(vcpu, svm->msrpm, MSR_IA32_LASTINTFROMIP, 1, 1);
2982 set_msr_interception(vcpu, svm->msrpm, MSR_IA32_LASTINTTOIP, 1, 1);
2983
2984 if (boot_cpu_has(X86_FEATURE_V_TSC_AUX) &&
2985 (guest_cpuid_has(&svm->vcpu, X86_FEATURE_RDTSCP) ||
2986 guest_cpuid_has(&svm->vcpu, X86_FEATURE_RDPID))) {
2987 set_msr_interception(vcpu, svm->msrpm, MSR_TSC_AUX, 1, 1);
2988 if (guest_cpuid_has(&svm->vcpu, X86_FEATURE_RDTSCP))
2989 svm_clr_intercept(svm, INTERCEPT_RDTSCP);
2990 }
2991 }
2992
sev_init_vmcb(struct vcpu_svm * svm)2993 void sev_init_vmcb(struct vcpu_svm *svm)
2994 {
2995 svm->vmcb->control.nested_ctl |= SVM_NESTED_CTL_SEV_ENABLE;
2996 clr_exception_intercept(svm, UD_VECTOR);
2997
2998 if (sev_es_guest(svm->vcpu.kvm))
2999 sev_es_init_vmcb(svm);
3000 }
3001
sev_es_vcpu_reset(struct vcpu_svm * svm)3002 void sev_es_vcpu_reset(struct vcpu_svm *svm)
3003 {
3004 /*
3005 * Set the GHCB MSR value as per the GHCB specification when emulating
3006 * vCPU RESET for an SEV-ES guest.
3007 */
3008 set_ghcb_msr(svm, GHCB_MSR_SEV_INFO(GHCB_VERSION_MAX,
3009 GHCB_VERSION_MIN,
3010 sev_enc_bit));
3011 }
3012
sev_es_prepare_switch_to_guest(struct sev_es_save_area * hostsa)3013 void sev_es_prepare_switch_to_guest(struct sev_es_save_area *hostsa)
3014 {
3015 /*
3016 * As an SEV-ES guest, hardware will restore the host state on VMEXIT,
3017 * of which one step is to perform a VMLOAD. KVM performs the
3018 * corresponding VMSAVE in svm_prepare_guest_switch for both
3019 * traditional and SEV-ES guests.
3020 */
3021
3022 /* XCR0 is restored on VMEXIT, save the current host value */
3023 hostsa->xcr0 = xgetbv(XCR_XFEATURE_ENABLED_MASK);
3024
3025 /* PKRU is restored on VMEXIT, save the current host value */
3026 hostsa->pkru = read_pkru();
3027
3028 /* MSR_IA32_XSS is restored on VMEXIT, save the currnet host value */
3029 hostsa->xss = host_xss;
3030 }
3031
sev_vcpu_deliver_sipi_vector(struct kvm_vcpu * vcpu,u8 vector)3032 void sev_vcpu_deliver_sipi_vector(struct kvm_vcpu *vcpu, u8 vector)
3033 {
3034 struct vcpu_svm *svm = to_svm(vcpu);
3035
3036 /* First SIPI: Use the values as initially set by the VMM */
3037 if (!svm->sev_es.received_first_sipi) {
3038 svm->sev_es.received_first_sipi = true;
3039 return;
3040 }
3041
3042 /*
3043 * Subsequent SIPI: Return from an AP Reset Hold VMGEXIT, where
3044 * the guest will set the CS and RIP. Set SW_EXIT_INFO_2 to a
3045 * non-zero value.
3046 */
3047 if (!svm->sev_es.ghcb)
3048 return;
3049
3050 ghcb_set_sw_exit_info_2(svm->sev_es.ghcb, 1);
3051 }
3052