blob: d7088a1d4de6c9e3566696c8e323127e7e7e840f [file] [log] [blame]
Joerg Roedeleaf78262020-03-24 10:41:54 +01001// 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>
Borislav Petkovb2bce0a2020-04-11 18:09:27 +020015#include <linux/pagemap.h>
Joerg Roedeleaf78262020-03-24 10:41:54 +010016#include <linux/swap.h>
Vipin Sharma7aef27f2021-03-29 21:42:06 -070017#include <linux/misc_cgroup.h>
Tom Lendackyadd5e2f2020-12-10 11:09:40 -060018#include <linux/processor.h>
Tom Lendackyd523ab6b2020-12-10 11:09:48 -060019#include <linux/trace_events.h>
Tom Lendacky86137772020-12-10 11:10:07 -060020#include <asm/fpu/internal.h>
Joerg Roedeleaf78262020-03-24 10:41:54 +010021
Tom Lendacky8640ca52020-12-15 12:44:07 -050022#include <asm/trapnr.h>
23
Joerg Roedeleaf78262020-03-24 10:41:54 +010024#include "x86.h"
25#include "svm.h"
Sean Christopherson35a78312020-12-30 16:27:00 -080026#include "svm_ops.h"
Tom Lendacky291bd202020-12-10 11:09:47 -060027#include "cpuid.h"
Tom Lendackyd523ab6b2020-12-10 11:09:48 -060028#include "trace.h"
Joerg Roedeleaf78262020-03-24 10:41:54 +010029
Tom Lendacky86137772020-12-10 11:10:07 -060030#define __ex(x) __kvm_handle_fault_on_reboot(x)
31
Vipin Sharma7aef27f2021-03-29 21:42:06 -070032#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
Sean Christophersone8126bd2021-04-21 19:11:14 -070047/* enable/disable SEV support */
48static int sev = IS_ENABLED(CONFIG_AMD_MEM_ENCRYPT_ACTIVE_BY_DEFAULT);
49module_param(sev, int, 0444);
50
51/* enable/disable SEV-ES support */
52static int sev_es = IS_ENABLED(CONFIG_AMD_MEM_ENCRYPT_ACTIVE_BY_DEFAULT);
53module_param(sev_es, int, 0444);
54
Tom Lendacky1edc1452020-12-10 11:09:49 -060055static u8 sev_enc_bit;
Joerg Roedeleaf78262020-03-24 10:41:54 +010056static int sev_flush_asids(void);
57static DECLARE_RWSEM(sev_deactivate_lock);
58static DEFINE_MUTEX(sev_bitmap_lock);
59unsigned int max_sev_asid;
60static unsigned int min_sev_asid;
Brijesh Singhd3d1af82021-04-15 15:53:55 +000061static unsigned long sev_me_mask;
Joerg Roedeleaf78262020-03-24 10:41:54 +010062static unsigned long *sev_asid_bitmap;
63static unsigned long *sev_reclaim_asid_bitmap;
Joerg Roedeleaf78262020-03-24 10:41:54 +010064
65struct enc_region {
66 struct list_head list;
67 unsigned long npages;
68 struct page **pages;
69 unsigned long uaddr;
70 unsigned long size;
71};
72
73static int sev_flush_asids(void)
74{
75 int ret, error = 0;
76
77 /*
78 * DEACTIVATE will clear the WBINVD indicator causing DF_FLUSH to fail,
79 * so it must be guarded.
80 */
81 down_write(&sev_deactivate_lock);
82
83 wbinvd_on_all_cpus();
84 ret = sev_guest_df_flush(&error);
85
86 up_write(&sev_deactivate_lock);
87
88 if (ret)
89 pr_err("SEV: DF_FLUSH failed, ret=%d, error=%#x\n", ret, error);
90
91 return ret;
92}
93
Nathan Tempelman54526d12021-04-08 22:32:14 +000094static inline bool is_mirroring_enc_context(struct kvm *kvm)
95{
96 return !!to_kvm_svm(kvm)->sev_info.enc_context_owner;
97}
98
Joerg Roedeleaf78262020-03-24 10:41:54 +010099/* Must be called with the sev_bitmap_lock held */
Tom Lendacky80675b32020-12-10 11:10:05 -0600100static bool __sev_recycle_asids(int min_asid, int max_asid)
Joerg Roedeleaf78262020-03-24 10:41:54 +0100101{
102 int pos;
103
104 /* Check if there are any ASIDs to reclaim before performing a flush */
Tom Lendacky80675b32020-12-10 11:10:05 -0600105 pos = find_next_bit(sev_reclaim_asid_bitmap, max_sev_asid, min_asid);
106 if (pos >= max_asid)
Joerg Roedeleaf78262020-03-24 10:41:54 +0100107 return false;
108
109 if (sev_flush_asids())
110 return false;
111
Tom Lendacky80675b32020-12-10 11:10:05 -0600112 /* The flush process will flush all reclaimable SEV and SEV-ES ASIDs */
Joerg Roedeleaf78262020-03-24 10:41:54 +0100113 bitmap_xor(sev_asid_bitmap, sev_asid_bitmap, sev_reclaim_asid_bitmap,
114 max_sev_asid);
115 bitmap_zero(sev_reclaim_asid_bitmap, max_sev_asid);
116
117 return true;
118}
119
Tom Lendacky80675b32020-12-10 11:10:05 -0600120static int sev_asid_new(struct kvm_sev_info *sev)
Joerg Roedeleaf78262020-03-24 10:41:54 +0100121{
Vipin Sharma7aef27f2021-03-29 21:42:06 -0700122 int pos, min_asid, max_asid, ret;
Joerg Roedeleaf78262020-03-24 10:41:54 +0100123 bool retry = true;
Vipin Sharma7aef27f2021-03-29 21:42:06 -0700124 enum misc_res_type type;
125
126 type = sev->es_active ? MISC_CG_RES_SEV_ES : MISC_CG_RES_SEV;
127 WARN_ON(sev->misc_cg);
128 sev->misc_cg = get_current_misc_cg();
129 ret = misc_cg_try_charge(type, sev->misc_cg, 1);
130 if (ret) {
131 put_misc_cg(sev->misc_cg);
132 sev->misc_cg = NULL;
133 return ret;
134 }
Joerg Roedeleaf78262020-03-24 10:41:54 +0100135
136 mutex_lock(&sev_bitmap_lock);
137
138 /*
Tom Lendacky80675b32020-12-10 11:10:05 -0600139 * SEV-enabled guests must use asid from min_sev_asid to max_sev_asid.
140 * SEV-ES-enabled guest can use from 1 to min_sev_asid - 1.
Joerg Roedeleaf78262020-03-24 10:41:54 +0100141 */
Tom Lendacky80675b32020-12-10 11:10:05 -0600142 min_asid = sev->es_active ? 0 : min_sev_asid - 1;
143 max_asid = sev->es_active ? min_sev_asid - 1 : max_sev_asid;
Joerg Roedeleaf78262020-03-24 10:41:54 +0100144again:
Tom Lendacky80675b32020-12-10 11:10:05 -0600145 pos = find_next_zero_bit(sev_asid_bitmap, max_sev_asid, min_asid);
146 if (pos >= max_asid) {
147 if (retry && __sev_recycle_asids(min_asid, max_asid)) {
Joerg Roedeleaf78262020-03-24 10:41:54 +0100148 retry = false;
149 goto again;
150 }
151 mutex_unlock(&sev_bitmap_lock);
Vipin Sharma7aef27f2021-03-29 21:42:06 -0700152 ret = -EBUSY;
153 goto e_uncharge;
Joerg Roedeleaf78262020-03-24 10:41:54 +0100154 }
155
156 __set_bit(pos, sev_asid_bitmap);
157
158 mutex_unlock(&sev_bitmap_lock);
159
160 return pos + 1;
Vipin Sharma7aef27f2021-03-29 21:42:06 -0700161e_uncharge:
162 misc_cg_uncharge(type, sev->misc_cg, 1);
163 put_misc_cg(sev->misc_cg);
164 sev->misc_cg = NULL;
165 return ret;
Joerg Roedeleaf78262020-03-24 10:41:54 +0100166}
167
168static int sev_get_asid(struct kvm *kvm)
169{
170 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
171
172 return sev->asid;
173}
174
Vipin Sharma7aef27f2021-03-29 21:42:06 -0700175static void sev_asid_free(struct kvm_sev_info *sev)
Joerg Roedeleaf78262020-03-24 10:41:54 +0100176{
177 struct svm_cpu_data *sd;
178 int cpu, pos;
Vipin Sharma7aef27f2021-03-29 21:42:06 -0700179 enum misc_res_type type;
Joerg Roedeleaf78262020-03-24 10:41:54 +0100180
181 mutex_lock(&sev_bitmap_lock);
182
Vipin Sharma7aef27f2021-03-29 21:42:06 -0700183 pos = sev->asid - 1;
Joerg Roedeleaf78262020-03-24 10:41:54 +0100184 __set_bit(pos, sev_reclaim_asid_bitmap);
185
186 for_each_possible_cpu(cpu) {
187 sd = per_cpu(svm_data, cpu);
188 sd->sev_vmcbs[pos] = NULL;
189 }
190
191 mutex_unlock(&sev_bitmap_lock);
Vipin Sharma7aef27f2021-03-29 21:42:06 -0700192
193 type = sev->es_active ? MISC_CG_RES_SEV_ES : MISC_CG_RES_SEV;
194 misc_cg_uncharge(type, sev->misc_cg, 1);
195 put_misc_cg(sev->misc_cg);
196 sev->misc_cg = NULL;
Joerg Roedeleaf78262020-03-24 10:41:54 +0100197}
198
199static void sev_unbind_asid(struct kvm *kvm, unsigned int handle)
200{
Sean Christopherson238eca82021-04-06 15:49:52 -0700201 struct sev_data_decommission decommission;
202 struct sev_data_deactivate deactivate;
Joerg Roedeleaf78262020-03-24 10:41:54 +0100203
204 if (!handle)
205 return;
206
Sean Christopherson238eca82021-04-06 15:49:52 -0700207 deactivate.handle = handle;
Joerg Roedeleaf78262020-03-24 10:41:54 +0100208
209 /* Guard DEACTIVATE against WBINVD/DF_FLUSH used in ASID recycling */
210 down_read(&sev_deactivate_lock);
Sean Christopherson238eca82021-04-06 15:49:52 -0700211 sev_guest_deactivate(&deactivate, NULL);
Joerg Roedeleaf78262020-03-24 10:41:54 +0100212 up_read(&sev_deactivate_lock);
213
Joerg Roedeleaf78262020-03-24 10:41:54 +0100214 /* decommission handle */
Sean Christopherson238eca82021-04-06 15:49:52 -0700215 decommission.handle = handle;
216 sev_guest_decommission(&decommission, NULL);
Joerg Roedeleaf78262020-03-24 10:41:54 +0100217}
218
219static int sev_guest_init(struct kvm *kvm, struct kvm_sev_cmd *argp)
220{
221 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
Sean Christopherson9fa15212021-03-30 20:19:35 -0700222 bool es_active = argp->id == KVM_SEV_ES_INIT;
Joerg Roedeleaf78262020-03-24 10:41:54 +0100223 int asid, ret;
224
Sean Christopherson87279062021-03-30 20:19:36 -0700225 if (kvm->created_vcpus)
226 return -EINVAL;
227
Joerg Roedeleaf78262020-03-24 10:41:54 +0100228 ret = -EBUSY;
229 if (unlikely(sev->active))
230 return ret;
231
Paolo Bonzinifd49e8e2021-04-22 02:39:48 -0400232 sev->es_active = es_active;
Tom Lendacky80675b32020-12-10 11:10:05 -0600233 asid = sev_asid_new(sev);
Joerg Roedeleaf78262020-03-24 10:41:54 +0100234 if (asid < 0)
Paolo Bonzinifd49e8e2021-04-22 02:39:48 -0400235 goto e_no_asid;
Vipin Sharma7aef27f2021-03-29 21:42:06 -0700236 sev->asid = asid;
Joerg Roedeleaf78262020-03-24 10:41:54 +0100237
238 ret = sev_platform_init(&argp->error);
239 if (ret)
240 goto e_free;
241
242 sev->active = true;
243 sev->asid = asid;
244 INIT_LIST_HEAD(&sev->regions_list);
245
246 return 0;
247
248e_free:
Vipin Sharma7aef27f2021-03-29 21:42:06 -0700249 sev_asid_free(sev);
250 sev->asid = 0;
Paolo Bonzinifd49e8e2021-04-22 02:39:48 -0400251e_no_asid:
252 sev->es_active = false;
Joerg Roedeleaf78262020-03-24 10:41:54 +0100253 return ret;
254}
255
256static int sev_bind_asid(struct kvm *kvm, unsigned int handle, int *error)
257{
Sean Christopherson238eca82021-04-06 15:49:52 -0700258 struct sev_data_activate activate;
Joerg Roedeleaf78262020-03-24 10:41:54 +0100259 int asid = sev_get_asid(kvm);
260 int ret;
261
Joerg Roedeleaf78262020-03-24 10:41:54 +0100262 /* activate ASID on the given handle */
Sean Christopherson238eca82021-04-06 15:49:52 -0700263 activate.handle = handle;
264 activate.asid = asid;
265 ret = sev_guest_activate(&activate, error);
Joerg Roedeleaf78262020-03-24 10:41:54 +0100266
267 return ret;
268}
269
270static int __sev_issue_cmd(int fd, int id, void *data, int *error)
271{
272 struct fd f;
273 int ret;
274
275 f = fdget(fd);
276 if (!f.file)
277 return -EBADF;
278
279 ret = sev_issue_cmd_external_user(f.file, id, data, error);
280
281 fdput(f);
282 return ret;
283}
284
285static int sev_issue_cmd(struct kvm *kvm, int id, void *data, int *error)
286{
287 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
288
289 return __sev_issue_cmd(sev->fd, id, data, error);
290}
291
292static int sev_launch_start(struct kvm *kvm, struct kvm_sev_cmd *argp)
293{
294 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
Sean Christopherson238eca82021-04-06 15:49:52 -0700295 struct sev_data_launch_start start;
Joerg Roedeleaf78262020-03-24 10:41:54 +0100296 struct kvm_sev_launch_start params;
297 void *dh_blob, *session_blob;
298 int *error = &argp->error;
299 int ret;
300
301 if (!sev_guest(kvm))
302 return -ENOTTY;
303
304 if (copy_from_user(&params, (void __user *)(uintptr_t)argp->data, sizeof(params)))
305 return -EFAULT;
306
Sean Christopherson238eca82021-04-06 15:49:52 -0700307 memset(&start, 0, sizeof(start));
Joerg Roedeleaf78262020-03-24 10:41:54 +0100308
309 dh_blob = NULL;
310 if (params.dh_uaddr) {
311 dh_blob = psp_copy_user_blob(params.dh_uaddr, params.dh_len);
Sean Christopherson238eca82021-04-06 15:49:52 -0700312 if (IS_ERR(dh_blob))
313 return PTR_ERR(dh_blob);
Joerg Roedeleaf78262020-03-24 10:41:54 +0100314
Sean Christopherson238eca82021-04-06 15:49:52 -0700315 start.dh_cert_address = __sme_set(__pa(dh_blob));
316 start.dh_cert_len = params.dh_len;
Joerg Roedeleaf78262020-03-24 10:41:54 +0100317 }
318
319 session_blob = NULL;
320 if (params.session_uaddr) {
321 session_blob = psp_copy_user_blob(params.session_uaddr, params.session_len);
322 if (IS_ERR(session_blob)) {
323 ret = PTR_ERR(session_blob);
324 goto e_free_dh;
325 }
326
Sean Christopherson238eca82021-04-06 15:49:52 -0700327 start.session_address = __sme_set(__pa(session_blob));
328 start.session_len = params.session_len;
Joerg Roedeleaf78262020-03-24 10:41:54 +0100329 }
330
Sean Christopherson238eca82021-04-06 15:49:52 -0700331 start.handle = params.handle;
332 start.policy = params.policy;
Joerg Roedeleaf78262020-03-24 10:41:54 +0100333
334 /* create memory encryption context */
Sean Christopherson238eca82021-04-06 15:49:52 -0700335 ret = __sev_issue_cmd(argp->sev_fd, SEV_CMD_LAUNCH_START, &start, error);
Joerg Roedeleaf78262020-03-24 10:41:54 +0100336 if (ret)
337 goto e_free_session;
338
339 /* Bind ASID to this guest */
Sean Christopherson238eca82021-04-06 15:49:52 -0700340 ret = sev_bind_asid(kvm, start.handle, error);
Joerg Roedeleaf78262020-03-24 10:41:54 +0100341 if (ret)
342 goto e_free_session;
343
344 /* return handle to userspace */
Sean Christopherson238eca82021-04-06 15:49:52 -0700345 params.handle = start.handle;
Joerg Roedeleaf78262020-03-24 10:41:54 +0100346 if (copy_to_user((void __user *)(uintptr_t)argp->data, &params, sizeof(params))) {
Sean Christopherson238eca82021-04-06 15:49:52 -0700347 sev_unbind_asid(kvm, start.handle);
Joerg Roedeleaf78262020-03-24 10:41:54 +0100348 ret = -EFAULT;
349 goto e_free_session;
350 }
351
Sean Christopherson238eca82021-04-06 15:49:52 -0700352 sev->handle = start.handle;
Joerg Roedeleaf78262020-03-24 10:41:54 +0100353 sev->fd = argp->sev_fd;
354
355e_free_session:
356 kfree(session_blob);
357e_free_dh:
358 kfree(dh_blob);
Joerg Roedeleaf78262020-03-24 10:41:54 +0100359 return ret;
360}
361
362static struct page **sev_pin_memory(struct kvm *kvm, unsigned long uaddr,
363 unsigned long ulen, unsigned long *n,
364 int write)
365{
366 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
John Hubbard78824fa2020-05-25 23:22:06 -0700367 unsigned long npages, size;
368 int npinned;
Joerg Roedeleaf78262020-03-24 10:41:54 +0100369 unsigned long locked, lock_limit;
370 struct page **pages;
371 unsigned long first, last;
Dan Carpenterff2bd9f2020-07-14 17:23:51 +0300372 int ret;
Joerg Roedeleaf78262020-03-24 10:41:54 +0100373
Peter Gonda19a23da2021-01-27 08:15:24 -0800374 lockdep_assert_held(&kvm->lock);
375
Joerg Roedeleaf78262020-03-24 10:41:54 +0100376 if (ulen == 0 || uaddr + ulen < uaddr)
Paolo Bonzinia8d908b2020-06-23 05:12:24 -0400377 return ERR_PTR(-EINVAL);
Joerg Roedeleaf78262020-03-24 10:41:54 +0100378
379 /* Calculate number of pages. */
380 first = (uaddr & PAGE_MASK) >> PAGE_SHIFT;
381 last = ((uaddr + ulen - 1) & PAGE_MASK) >> PAGE_SHIFT;
382 npages = (last - first + 1);
383
384 locked = sev->pages_locked + npages;
385 lock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
386 if (locked > lock_limit && !capable(CAP_IPC_LOCK)) {
387 pr_err("SEV: %lu locked pages exceed the lock limit of %lu.\n", locked, lock_limit);
Paolo Bonzinia8d908b2020-06-23 05:12:24 -0400388 return ERR_PTR(-ENOMEM);
Joerg Roedeleaf78262020-03-24 10:41:54 +0100389 }
390
John Hubbard78824fa2020-05-25 23:22:06 -0700391 if (WARN_ON_ONCE(npages > INT_MAX))
Paolo Bonzinia8d908b2020-06-23 05:12:24 -0400392 return ERR_PTR(-EINVAL);
John Hubbard78824fa2020-05-25 23:22:06 -0700393
Joerg Roedeleaf78262020-03-24 10:41:54 +0100394 /* Avoid using vmalloc for smaller buffers. */
395 size = npages * sizeof(struct page *);
396 if (size > PAGE_SIZE)
Christoph Hellwig88dca4c2020-06-01 21:51:40 -0700397 pages = __vmalloc(size, GFP_KERNEL_ACCOUNT | __GFP_ZERO);
Joerg Roedeleaf78262020-03-24 10:41:54 +0100398 else
399 pages = kmalloc(size, GFP_KERNEL_ACCOUNT);
400
401 if (!pages)
Paolo Bonzinia8d908b2020-06-23 05:12:24 -0400402 return ERR_PTR(-ENOMEM);
Joerg Roedeleaf78262020-03-24 10:41:54 +0100403
404 /* Pin the user virtual address. */
John Hubbarddc42c8a2020-05-25 23:22:07 -0700405 npinned = pin_user_pages_fast(uaddr, npages, write ? FOLL_WRITE : 0, pages);
Joerg Roedeleaf78262020-03-24 10:41:54 +0100406 if (npinned != npages) {
407 pr_err("SEV: Failure locking %lu pages.\n", npages);
Dan Carpenterff2bd9f2020-07-14 17:23:51 +0300408 ret = -ENOMEM;
Joerg Roedeleaf78262020-03-24 10:41:54 +0100409 goto err;
410 }
411
412 *n = npages;
413 sev->pages_locked = locked;
414
415 return pages;
416
417err:
Dan Carpenterff2bd9f2020-07-14 17:23:51 +0300418 if (npinned > 0)
John Hubbarddc42c8a2020-05-25 23:22:07 -0700419 unpin_user_pages(pages, npinned);
Joerg Roedeleaf78262020-03-24 10:41:54 +0100420
421 kvfree(pages);
Dan Carpenterff2bd9f2020-07-14 17:23:51 +0300422 return ERR_PTR(ret);
Joerg Roedeleaf78262020-03-24 10:41:54 +0100423}
424
425static void sev_unpin_memory(struct kvm *kvm, struct page **pages,
426 unsigned long npages)
427{
428 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
429
John Hubbarddc42c8a2020-05-25 23:22:07 -0700430 unpin_user_pages(pages, npages);
Joerg Roedeleaf78262020-03-24 10:41:54 +0100431 kvfree(pages);
432 sev->pages_locked -= npages;
433}
434
435static void sev_clflush_pages(struct page *pages[], unsigned long npages)
436{
437 uint8_t *page_virtual;
438 unsigned long i;
439
Krish Sadhukhane1ebb2b2020-09-17 21:20:38 +0000440 if (this_cpu_has(X86_FEATURE_SME_COHERENT) || npages == 0 ||
441 pages == NULL)
Joerg Roedeleaf78262020-03-24 10:41:54 +0100442 return;
443
444 for (i = 0; i < npages; i++) {
445 page_virtual = kmap_atomic(pages[i]);
446 clflush_cache_range(page_virtual, PAGE_SIZE);
447 kunmap_atomic(page_virtual);
448 }
449}
450
451static unsigned long get_num_contig_pages(unsigned long idx,
452 struct page **inpages, unsigned long npages)
453{
454 unsigned long paddr, next_paddr;
455 unsigned long i = idx + 1, pages = 1;
456
457 /* find the number of contiguous pages starting from idx */
458 paddr = __sme_page_pa(inpages[idx]);
459 while (i < npages) {
460 next_paddr = __sme_page_pa(inpages[i++]);
461 if ((paddr + PAGE_SIZE) == next_paddr) {
462 pages++;
463 paddr = next_paddr;
464 continue;
465 }
466 break;
467 }
468
469 return pages;
470}
471
472static int sev_launch_update_data(struct kvm *kvm, struct kvm_sev_cmd *argp)
473{
474 unsigned long vaddr, vaddr_end, next_vaddr, npages, pages, size, i;
475 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
476 struct kvm_sev_launch_update_data params;
Sean Christopherson238eca82021-04-06 15:49:52 -0700477 struct sev_data_launch_update_data data;
Joerg Roedeleaf78262020-03-24 10:41:54 +0100478 struct page **inpages;
479 int ret;
480
481 if (!sev_guest(kvm))
482 return -ENOTTY;
483
484 if (copy_from_user(&params, (void __user *)(uintptr_t)argp->data, sizeof(params)))
485 return -EFAULT;
486
Joerg Roedeleaf78262020-03-24 10:41:54 +0100487 vaddr = params.uaddr;
488 size = params.len;
489 vaddr_end = vaddr + size;
490
491 /* Lock the user memory. */
492 inpages = sev_pin_memory(kvm, vaddr, size, &npages, 1);
Sean Christopherson238eca82021-04-06 15:49:52 -0700493 if (IS_ERR(inpages))
494 return PTR_ERR(inpages);
Joerg Roedeleaf78262020-03-24 10:41:54 +0100495
496 /*
Paolo Bonzini14e3dd82020-09-23 13:01:33 -0400497 * Flush (on non-coherent CPUs) before LAUNCH_UPDATE encrypts pages in
498 * place; the cache may contain the data that was written unencrypted.
Joerg Roedeleaf78262020-03-24 10:41:54 +0100499 */
500 sev_clflush_pages(inpages, npages);
501
Sean Christopherson238eca82021-04-06 15:49:52 -0700502 data.reserved = 0;
503 data.handle = sev->handle;
504
Joerg Roedeleaf78262020-03-24 10:41:54 +0100505 for (i = 0; vaddr < vaddr_end; vaddr = next_vaddr, i += pages) {
506 int offset, len;
507
508 /*
509 * If the user buffer is not page-aligned, calculate the offset
510 * within the page.
511 */
512 offset = vaddr & (PAGE_SIZE - 1);
513
514 /* Calculate the number of pages that can be encrypted in one go. */
515 pages = get_num_contig_pages(i, inpages, npages);
516
517 len = min_t(size_t, ((pages * PAGE_SIZE) - offset), size);
518
Sean Christopherson238eca82021-04-06 15:49:52 -0700519 data.len = len;
520 data.address = __sme_page_pa(inpages[i]) + offset;
521 ret = sev_issue_cmd(kvm, SEV_CMD_LAUNCH_UPDATE_DATA, &data, &argp->error);
Joerg Roedeleaf78262020-03-24 10:41:54 +0100522 if (ret)
523 goto e_unpin;
524
525 size -= len;
526 next_vaddr = vaddr + len;
527 }
528
529e_unpin:
530 /* content of memory is updated, mark pages dirty */
531 for (i = 0; i < npages; i++) {
532 set_page_dirty_lock(inpages[i]);
533 mark_page_accessed(inpages[i]);
534 }
535 /* unlock the user pages */
536 sev_unpin_memory(kvm, inpages, npages);
Joerg Roedeleaf78262020-03-24 10:41:54 +0100537 return ret;
538}
539
Tom Lendackyad731092020-12-10 11:10:09 -0600540static int sev_es_sync_vmsa(struct vcpu_svm *svm)
541{
542 struct vmcb_save_area *save = &svm->vmcb->save;
543
544 /* Check some debug related fields before encrypting the VMSA */
545 if (svm->vcpu.guest_debug || (save->dr7 & ~DR7_FIXED_1))
546 return -EINVAL;
547
548 /* Sync registgers */
549 save->rax = svm->vcpu.arch.regs[VCPU_REGS_RAX];
550 save->rbx = svm->vcpu.arch.regs[VCPU_REGS_RBX];
551 save->rcx = svm->vcpu.arch.regs[VCPU_REGS_RCX];
552 save->rdx = svm->vcpu.arch.regs[VCPU_REGS_RDX];
553 save->rsp = svm->vcpu.arch.regs[VCPU_REGS_RSP];
554 save->rbp = svm->vcpu.arch.regs[VCPU_REGS_RBP];
555 save->rsi = svm->vcpu.arch.regs[VCPU_REGS_RSI];
556 save->rdi = svm->vcpu.arch.regs[VCPU_REGS_RDI];
Paolo Bonzinid45f89f2020-12-16 13:08:21 -0500557#ifdef CONFIG_X86_64
Tom Lendackyad731092020-12-10 11:10:09 -0600558 save->r8 = svm->vcpu.arch.regs[VCPU_REGS_R8];
559 save->r9 = svm->vcpu.arch.regs[VCPU_REGS_R9];
560 save->r10 = svm->vcpu.arch.regs[VCPU_REGS_R10];
561 save->r11 = svm->vcpu.arch.regs[VCPU_REGS_R11];
562 save->r12 = svm->vcpu.arch.regs[VCPU_REGS_R12];
563 save->r13 = svm->vcpu.arch.regs[VCPU_REGS_R13];
564 save->r14 = svm->vcpu.arch.regs[VCPU_REGS_R14];
565 save->r15 = svm->vcpu.arch.regs[VCPU_REGS_R15];
Paolo Bonzinid45f89f2020-12-16 13:08:21 -0500566#endif
Tom Lendackyad731092020-12-10 11:10:09 -0600567 save->rip = svm->vcpu.arch.regs[VCPU_REGS_RIP];
568
569 /* Sync some non-GPR registers before encrypting */
570 save->xcr0 = svm->vcpu.arch.xcr0;
571 save->pkru = svm->vcpu.arch.pkru;
572 save->xss = svm->vcpu.arch.ia32_xss;
573
574 /*
575 * SEV-ES will use a VMSA that is pointed to by the VMCB, not
576 * the traditional VMSA that is part of the VMCB. Copy the
577 * traditional VMSA as it has been built so far (in prep
578 * for LAUNCH_UPDATE_VMSA) to be the initial SEV-ES state.
579 */
580 memcpy(svm->vmsa, save, sizeof(*save));
581
582 return 0;
583}
584
585static int sev_launch_update_vmsa(struct kvm *kvm, struct kvm_sev_cmd *argp)
586{
587 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
Sean Christopherson238eca82021-04-06 15:49:52 -0700588 struct sev_data_launch_update_vmsa vmsa;
Sean Christophersonc36b16d2021-03-30 20:19:34 -0700589 struct kvm_vcpu *vcpu;
Tom Lendackyad731092020-12-10 11:10:09 -0600590 int i, ret;
591
592 if (!sev_es_guest(kvm))
593 return -ENOTTY;
594
Sean Christopherson238eca82021-04-06 15:49:52 -0700595 vmsa.reserved = 0;
Tom Lendackyad731092020-12-10 11:10:09 -0600596
Sean Christophersonc36b16d2021-03-30 20:19:34 -0700597 kvm_for_each_vcpu(i, vcpu, kvm) {
598 struct vcpu_svm *svm = to_svm(vcpu);
Tom Lendackyad731092020-12-10 11:10:09 -0600599
600 /* Perform some pre-encryption checks against the VMSA */
601 ret = sev_es_sync_vmsa(svm);
602 if (ret)
Sean Christopherson238eca82021-04-06 15:49:52 -0700603 return ret;
Tom Lendackyad731092020-12-10 11:10:09 -0600604
605 /*
606 * The LAUNCH_UPDATE_VMSA command will perform in-place
607 * encryption of the VMSA memory content (i.e it will write
608 * the same memory region with the guest's key), so invalidate
609 * it first.
610 */
611 clflush_cache_range(svm->vmsa, PAGE_SIZE);
612
Sean Christopherson238eca82021-04-06 15:49:52 -0700613 vmsa.handle = sev->handle;
614 vmsa.address = __sme_pa(svm->vmsa);
615 vmsa.len = PAGE_SIZE;
616 ret = sev_issue_cmd(kvm, SEV_CMD_LAUNCH_UPDATE_VMSA, &vmsa,
Tom Lendackyad731092020-12-10 11:10:09 -0600617 &argp->error);
618 if (ret)
Sean Christopherson238eca82021-04-06 15:49:52 -0700619 return ret;
Tom Lendackyad731092020-12-10 11:10:09 -0600620
621 svm->vcpu.arch.guest_state_protected = true;
622 }
623
Sean Christopherson238eca82021-04-06 15:49:52 -0700624 return 0;
Tom Lendackyad731092020-12-10 11:10:09 -0600625}
626
Joerg Roedeleaf78262020-03-24 10:41:54 +0100627static int sev_launch_measure(struct kvm *kvm, struct kvm_sev_cmd *argp)
628{
629 void __user *measure = (void __user *)(uintptr_t)argp->data;
630 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
Sean Christopherson238eca82021-04-06 15:49:52 -0700631 struct sev_data_launch_measure data;
Joerg Roedeleaf78262020-03-24 10:41:54 +0100632 struct kvm_sev_launch_measure params;
633 void __user *p = NULL;
634 void *blob = NULL;
635 int ret;
636
637 if (!sev_guest(kvm))
638 return -ENOTTY;
639
640 if (copy_from_user(&params, measure, sizeof(params)))
641 return -EFAULT;
642
Sean Christopherson238eca82021-04-06 15:49:52 -0700643 memset(&data, 0, sizeof(data));
Joerg Roedeleaf78262020-03-24 10:41:54 +0100644
645 /* User wants to query the blob length */
646 if (!params.len)
647 goto cmd;
648
649 p = (void __user *)(uintptr_t)params.uaddr;
650 if (p) {
Sean Christopherson238eca82021-04-06 15:49:52 -0700651 if (params.len > SEV_FW_BLOB_MAX_SIZE)
652 return -EINVAL;
Joerg Roedeleaf78262020-03-24 10:41:54 +0100653
Sean Christophersoneba04b22021-03-30 19:30:25 -0700654 blob = kmalloc(params.len, GFP_KERNEL_ACCOUNT);
Joerg Roedeleaf78262020-03-24 10:41:54 +0100655 if (!blob)
Sean Christopherson238eca82021-04-06 15:49:52 -0700656 return -ENOMEM;
Joerg Roedeleaf78262020-03-24 10:41:54 +0100657
Sean Christopherson238eca82021-04-06 15:49:52 -0700658 data.address = __psp_pa(blob);
659 data.len = params.len;
Joerg Roedeleaf78262020-03-24 10:41:54 +0100660 }
661
662cmd:
Sean Christopherson238eca82021-04-06 15:49:52 -0700663 data.handle = sev->handle;
664 ret = sev_issue_cmd(kvm, SEV_CMD_LAUNCH_MEASURE, &data, &argp->error);
Joerg Roedeleaf78262020-03-24 10:41:54 +0100665
666 /*
667 * If we query the session length, FW responded with expected data.
668 */
669 if (!params.len)
670 goto done;
671
672 if (ret)
673 goto e_free_blob;
674
675 if (blob) {
676 if (copy_to_user(p, blob, params.len))
677 ret = -EFAULT;
678 }
679
680done:
Sean Christopherson238eca82021-04-06 15:49:52 -0700681 params.len = data.len;
Joerg Roedeleaf78262020-03-24 10:41:54 +0100682 if (copy_to_user(measure, &params, sizeof(params)))
683 ret = -EFAULT;
684e_free_blob:
685 kfree(blob);
Joerg Roedeleaf78262020-03-24 10:41:54 +0100686 return ret;
687}
688
689static int sev_launch_finish(struct kvm *kvm, struct kvm_sev_cmd *argp)
690{
691 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
Sean Christopherson238eca82021-04-06 15:49:52 -0700692 struct sev_data_launch_finish data;
Joerg Roedeleaf78262020-03-24 10:41:54 +0100693
694 if (!sev_guest(kvm))
695 return -ENOTTY;
696
Sean Christopherson238eca82021-04-06 15:49:52 -0700697 data.handle = sev->handle;
698 return sev_issue_cmd(kvm, SEV_CMD_LAUNCH_FINISH, &data, &argp->error);
Joerg Roedeleaf78262020-03-24 10:41:54 +0100699}
700
701static int sev_guest_status(struct kvm *kvm, struct kvm_sev_cmd *argp)
702{
703 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
704 struct kvm_sev_guest_status params;
Sean Christopherson238eca82021-04-06 15:49:52 -0700705 struct sev_data_guest_status data;
Joerg Roedeleaf78262020-03-24 10:41:54 +0100706 int ret;
707
708 if (!sev_guest(kvm))
709 return -ENOTTY;
710
Sean Christopherson238eca82021-04-06 15:49:52 -0700711 memset(&data, 0, sizeof(data));
Joerg Roedeleaf78262020-03-24 10:41:54 +0100712
Sean Christopherson238eca82021-04-06 15:49:52 -0700713 data.handle = sev->handle;
714 ret = sev_issue_cmd(kvm, SEV_CMD_GUEST_STATUS, &data, &argp->error);
Joerg Roedeleaf78262020-03-24 10:41:54 +0100715 if (ret)
Sean Christopherson238eca82021-04-06 15:49:52 -0700716 return ret;
Joerg Roedeleaf78262020-03-24 10:41:54 +0100717
Sean Christopherson238eca82021-04-06 15:49:52 -0700718 params.policy = data.policy;
719 params.state = data.state;
720 params.handle = data.handle;
Joerg Roedeleaf78262020-03-24 10:41:54 +0100721
722 if (copy_to_user((void __user *)(uintptr_t)argp->data, &params, sizeof(params)))
723 ret = -EFAULT;
Sean Christopherson238eca82021-04-06 15:49:52 -0700724
Joerg Roedeleaf78262020-03-24 10:41:54 +0100725 return ret;
726}
727
728static int __sev_issue_dbg_cmd(struct kvm *kvm, unsigned long src,
729 unsigned long dst, int size,
730 int *error, bool enc)
731{
732 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
Sean Christopherson238eca82021-04-06 15:49:52 -0700733 struct sev_data_dbg data;
Joerg Roedeleaf78262020-03-24 10:41:54 +0100734
Sean Christopherson238eca82021-04-06 15:49:52 -0700735 data.reserved = 0;
736 data.handle = sev->handle;
737 data.dst_addr = dst;
738 data.src_addr = src;
739 data.len = size;
Joerg Roedeleaf78262020-03-24 10:41:54 +0100740
Sean Christopherson238eca82021-04-06 15:49:52 -0700741 return sev_issue_cmd(kvm,
742 enc ? SEV_CMD_DBG_ENCRYPT : SEV_CMD_DBG_DECRYPT,
743 &data, error);
Joerg Roedeleaf78262020-03-24 10:41:54 +0100744}
745
746static int __sev_dbg_decrypt(struct kvm *kvm, unsigned long src_paddr,
747 unsigned long dst_paddr, int sz, int *err)
748{
749 int offset;
750
751 /*
752 * Its safe to read more than we are asked, caller should ensure that
753 * destination has enough space.
754 */
Joerg Roedeleaf78262020-03-24 10:41:54 +0100755 offset = src_paddr & 15;
Ashish Kalra854c57f2020-11-10 22:42:05 +0000756 src_paddr = round_down(src_paddr, 16);
Joerg Roedeleaf78262020-03-24 10:41:54 +0100757 sz = round_up(sz + offset, 16);
758
759 return __sev_issue_dbg_cmd(kvm, src_paddr, dst_paddr, sz, err, false);
760}
761
762static int __sev_dbg_decrypt_user(struct kvm *kvm, unsigned long paddr,
763 unsigned long __user dst_uaddr,
764 unsigned long dst_paddr,
765 int size, int *err)
766{
767 struct page *tpage = NULL;
768 int ret, offset;
769
770 /* if inputs are not 16-byte then use intermediate buffer */
771 if (!IS_ALIGNED(dst_paddr, 16) ||
772 !IS_ALIGNED(paddr, 16) ||
773 !IS_ALIGNED(size, 16)) {
774 tpage = (void *)alloc_page(GFP_KERNEL);
775 if (!tpage)
776 return -ENOMEM;
777
778 dst_paddr = __sme_page_pa(tpage);
779 }
780
781 ret = __sev_dbg_decrypt(kvm, paddr, dst_paddr, size, err);
782 if (ret)
783 goto e_free;
784
785 if (tpage) {
786 offset = paddr & 15;
787 if (copy_to_user((void __user *)(uintptr_t)dst_uaddr,
788 page_address(tpage) + offset, size))
789 ret = -EFAULT;
790 }
791
792e_free:
793 if (tpage)
794 __free_page(tpage);
795
796 return ret;
797}
798
799static int __sev_dbg_encrypt_user(struct kvm *kvm, unsigned long paddr,
800 unsigned long __user vaddr,
801 unsigned long dst_paddr,
802 unsigned long __user dst_vaddr,
803 int size, int *error)
804{
805 struct page *src_tpage = NULL;
806 struct page *dst_tpage = NULL;
807 int ret, len = size;
808
809 /* If source buffer is not aligned then use an intermediate buffer */
810 if (!IS_ALIGNED(vaddr, 16)) {
811 src_tpage = alloc_page(GFP_KERNEL);
812 if (!src_tpage)
813 return -ENOMEM;
814
815 if (copy_from_user(page_address(src_tpage),
816 (void __user *)(uintptr_t)vaddr, size)) {
817 __free_page(src_tpage);
818 return -EFAULT;
819 }
820
821 paddr = __sme_page_pa(src_tpage);
822 }
823
824 /*
825 * If destination buffer or length is not aligned then do read-modify-write:
826 * - decrypt destination in an intermediate buffer
827 * - copy the source buffer in an intermediate buffer
828 * - use the intermediate buffer as source buffer
829 */
830 if (!IS_ALIGNED(dst_vaddr, 16) || !IS_ALIGNED(size, 16)) {
831 int dst_offset;
832
833 dst_tpage = alloc_page(GFP_KERNEL);
834 if (!dst_tpage) {
835 ret = -ENOMEM;
836 goto e_free;
837 }
838
839 ret = __sev_dbg_decrypt(kvm, dst_paddr,
840 __sme_page_pa(dst_tpage), size, error);
841 if (ret)
842 goto e_free;
843
844 /*
845 * If source is kernel buffer then use memcpy() otherwise
846 * copy_from_user().
847 */
848 dst_offset = dst_paddr & 15;
849
850 if (src_tpage)
851 memcpy(page_address(dst_tpage) + dst_offset,
852 page_address(src_tpage), size);
853 else {
854 if (copy_from_user(page_address(dst_tpage) + dst_offset,
855 (void __user *)(uintptr_t)vaddr, size)) {
856 ret = -EFAULT;
857 goto e_free;
858 }
859 }
860
861 paddr = __sme_page_pa(dst_tpage);
862 dst_paddr = round_down(dst_paddr, 16);
863 len = round_up(size, 16);
864 }
865
866 ret = __sev_issue_dbg_cmd(kvm, paddr, dst_paddr, len, error, true);
867
868e_free:
869 if (src_tpage)
870 __free_page(src_tpage);
871 if (dst_tpage)
872 __free_page(dst_tpage);
873 return ret;
874}
875
876static int sev_dbg_crypt(struct kvm *kvm, struct kvm_sev_cmd *argp, bool dec)
877{
878 unsigned long vaddr, vaddr_end, next_vaddr;
879 unsigned long dst_vaddr;
880 struct page **src_p, **dst_p;
881 struct kvm_sev_dbg debug;
882 unsigned long n;
883 unsigned int size;
884 int ret;
885
886 if (!sev_guest(kvm))
887 return -ENOTTY;
888
889 if (copy_from_user(&debug, (void __user *)(uintptr_t)argp->data, sizeof(debug)))
890 return -EFAULT;
891
892 if (!debug.len || debug.src_uaddr + debug.len < debug.src_uaddr)
893 return -EINVAL;
894 if (!debug.dst_uaddr)
895 return -EINVAL;
896
897 vaddr = debug.src_uaddr;
898 size = debug.len;
899 vaddr_end = vaddr + size;
900 dst_vaddr = debug.dst_uaddr;
901
902 for (; vaddr < vaddr_end; vaddr = next_vaddr) {
903 int len, s_off, d_off;
904
905 /* lock userspace source and destination page */
906 src_p = sev_pin_memory(kvm, vaddr & PAGE_MASK, PAGE_SIZE, &n, 0);
Dan Carpenterff2bd9f2020-07-14 17:23:51 +0300907 if (IS_ERR(src_p))
908 return PTR_ERR(src_p);
Joerg Roedeleaf78262020-03-24 10:41:54 +0100909
910 dst_p = sev_pin_memory(kvm, dst_vaddr & PAGE_MASK, PAGE_SIZE, &n, 1);
Dan Carpenterff2bd9f2020-07-14 17:23:51 +0300911 if (IS_ERR(dst_p)) {
Joerg Roedeleaf78262020-03-24 10:41:54 +0100912 sev_unpin_memory(kvm, src_p, n);
Dan Carpenterff2bd9f2020-07-14 17:23:51 +0300913 return PTR_ERR(dst_p);
Joerg Roedeleaf78262020-03-24 10:41:54 +0100914 }
915
916 /*
Paolo Bonzini14e3dd82020-09-23 13:01:33 -0400917 * Flush (on non-coherent CPUs) before DBG_{DE,EN}CRYPT read or modify
918 * the pages; flush the destination too so that future accesses do not
919 * see stale data.
Joerg Roedeleaf78262020-03-24 10:41:54 +0100920 */
921 sev_clflush_pages(src_p, 1);
922 sev_clflush_pages(dst_p, 1);
923
924 /*
925 * Since user buffer may not be page aligned, calculate the
926 * offset within the page.
927 */
928 s_off = vaddr & ~PAGE_MASK;
929 d_off = dst_vaddr & ~PAGE_MASK;
930 len = min_t(size_t, (PAGE_SIZE - s_off), size);
931
932 if (dec)
933 ret = __sev_dbg_decrypt_user(kvm,
934 __sme_page_pa(src_p[0]) + s_off,
935 dst_vaddr,
936 __sme_page_pa(dst_p[0]) + d_off,
937 len, &argp->error);
938 else
939 ret = __sev_dbg_encrypt_user(kvm,
940 __sme_page_pa(src_p[0]) + s_off,
941 vaddr,
942 __sme_page_pa(dst_p[0]) + d_off,
943 dst_vaddr,
944 len, &argp->error);
945
946 sev_unpin_memory(kvm, src_p, n);
947 sev_unpin_memory(kvm, dst_p, n);
948
949 if (ret)
950 goto err;
951
952 next_vaddr = vaddr + len;
953 dst_vaddr = dst_vaddr + len;
954 size -= len;
955 }
956err:
957 return ret;
958}
959
960static int sev_launch_secret(struct kvm *kvm, struct kvm_sev_cmd *argp)
961{
962 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
Sean Christopherson238eca82021-04-06 15:49:52 -0700963 struct sev_data_launch_secret data;
Joerg Roedeleaf78262020-03-24 10:41:54 +0100964 struct kvm_sev_launch_secret params;
965 struct page **pages;
966 void *blob, *hdr;
Cfir Cohen50085be2020-08-07 17:37:46 -0700967 unsigned long n, i;
Joerg Roedeleaf78262020-03-24 10:41:54 +0100968 int ret, offset;
969
970 if (!sev_guest(kvm))
971 return -ENOTTY;
972
973 if (copy_from_user(&params, (void __user *)(uintptr_t)argp->data, sizeof(params)))
974 return -EFAULT;
975
976 pages = sev_pin_memory(kvm, params.guest_uaddr, params.guest_len, &n, 1);
Paolo Bonzinia8d908b2020-06-23 05:12:24 -0400977 if (IS_ERR(pages))
978 return PTR_ERR(pages);
Joerg Roedeleaf78262020-03-24 10:41:54 +0100979
980 /*
Paolo Bonzini14e3dd82020-09-23 13:01:33 -0400981 * Flush (on non-coherent CPUs) before LAUNCH_SECRET encrypts pages in
982 * place; the cache may contain the data that was written unencrypted.
Cfir Cohen50085be2020-08-07 17:37:46 -0700983 */
984 sev_clflush_pages(pages, n);
985
986 /*
Joerg Roedeleaf78262020-03-24 10:41:54 +0100987 * The secret must be copied into contiguous memory region, lets verify
988 * that userspace memory pages are contiguous before we issue command.
989 */
990 if (get_num_contig_pages(0, pages, n) != n) {
991 ret = -EINVAL;
992 goto e_unpin_memory;
993 }
994
Sean Christopherson238eca82021-04-06 15:49:52 -0700995 memset(&data, 0, sizeof(data));
Joerg Roedeleaf78262020-03-24 10:41:54 +0100996
997 offset = params.guest_uaddr & (PAGE_SIZE - 1);
Sean Christopherson238eca82021-04-06 15:49:52 -0700998 data.guest_address = __sme_page_pa(pages[0]) + offset;
999 data.guest_len = params.guest_len;
Joerg Roedeleaf78262020-03-24 10:41:54 +01001000
1001 blob = psp_copy_user_blob(params.trans_uaddr, params.trans_len);
1002 if (IS_ERR(blob)) {
1003 ret = PTR_ERR(blob);
Sean Christopherson238eca82021-04-06 15:49:52 -07001004 goto e_unpin_memory;
Joerg Roedeleaf78262020-03-24 10:41:54 +01001005 }
1006
Sean Christopherson238eca82021-04-06 15:49:52 -07001007 data.trans_address = __psp_pa(blob);
1008 data.trans_len = params.trans_len;
Joerg Roedeleaf78262020-03-24 10:41:54 +01001009
1010 hdr = psp_copy_user_blob(params.hdr_uaddr, params.hdr_len);
1011 if (IS_ERR(hdr)) {
1012 ret = PTR_ERR(hdr);
1013 goto e_free_blob;
1014 }
Sean Christopherson238eca82021-04-06 15:49:52 -07001015 data.hdr_address = __psp_pa(hdr);
1016 data.hdr_len = params.hdr_len;
Joerg Roedeleaf78262020-03-24 10:41:54 +01001017
Sean Christopherson238eca82021-04-06 15:49:52 -07001018 data.handle = sev->handle;
1019 ret = sev_issue_cmd(kvm, SEV_CMD_LAUNCH_UPDATE_SECRET, &data, &argp->error);
Joerg Roedeleaf78262020-03-24 10:41:54 +01001020
1021 kfree(hdr);
1022
1023e_free_blob:
1024 kfree(blob);
Joerg Roedeleaf78262020-03-24 10:41:54 +01001025e_unpin_memory:
Cfir Cohen50085be2020-08-07 17:37:46 -07001026 /* content of memory is updated, mark pages dirty */
1027 for (i = 0; i < n; i++) {
1028 set_page_dirty_lock(pages[i]);
1029 mark_page_accessed(pages[i]);
1030 }
Joerg Roedeleaf78262020-03-24 10:41:54 +01001031 sev_unpin_memory(kvm, pages, n);
1032 return ret;
1033}
1034
Brijesh Singh2c07ded2021-01-04 09:17:49 -06001035static int sev_get_attestation_report(struct kvm *kvm, struct kvm_sev_cmd *argp)
1036{
1037 void __user *report = (void __user *)(uintptr_t)argp->data;
1038 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
Sean Christopherson238eca82021-04-06 15:49:52 -07001039 struct sev_data_attestation_report data;
Brijesh Singh2c07ded2021-01-04 09:17:49 -06001040 struct kvm_sev_attestation_report params;
1041 void __user *p;
1042 void *blob = NULL;
1043 int ret;
1044
1045 if (!sev_guest(kvm))
1046 return -ENOTTY;
1047
1048 if (copy_from_user(&params, (void __user *)(uintptr_t)argp->data, sizeof(params)))
1049 return -EFAULT;
1050
Sean Christopherson238eca82021-04-06 15:49:52 -07001051 memset(&data, 0, sizeof(data));
Brijesh Singh2c07ded2021-01-04 09:17:49 -06001052
1053 /* User wants to query the blob length */
1054 if (!params.len)
1055 goto cmd;
1056
1057 p = (void __user *)(uintptr_t)params.uaddr;
1058 if (p) {
Sean Christopherson238eca82021-04-06 15:49:52 -07001059 if (params.len > SEV_FW_BLOB_MAX_SIZE)
1060 return -EINVAL;
Brijesh Singh2c07ded2021-01-04 09:17:49 -06001061
Sean Christophersoneba04b22021-03-30 19:30:25 -07001062 blob = kmalloc(params.len, GFP_KERNEL_ACCOUNT);
Brijesh Singh2c07ded2021-01-04 09:17:49 -06001063 if (!blob)
Sean Christopherson238eca82021-04-06 15:49:52 -07001064 return -ENOMEM;
Brijesh Singh2c07ded2021-01-04 09:17:49 -06001065
Sean Christopherson238eca82021-04-06 15:49:52 -07001066 data.address = __psp_pa(blob);
1067 data.len = params.len;
1068 memcpy(data.mnonce, params.mnonce, sizeof(params.mnonce));
Brijesh Singh2c07ded2021-01-04 09:17:49 -06001069 }
1070cmd:
Sean Christopherson238eca82021-04-06 15:49:52 -07001071 data.handle = sev->handle;
1072 ret = sev_issue_cmd(kvm, SEV_CMD_ATTESTATION_REPORT, &data, &argp->error);
Brijesh Singh2c07ded2021-01-04 09:17:49 -06001073 /*
1074 * If we query the session length, FW responded with expected data.
1075 */
1076 if (!params.len)
1077 goto done;
1078
1079 if (ret)
1080 goto e_free_blob;
1081
1082 if (blob) {
1083 if (copy_to_user(p, blob, params.len))
1084 ret = -EFAULT;
1085 }
1086
1087done:
Sean Christopherson238eca82021-04-06 15:49:52 -07001088 params.len = data.len;
Brijesh Singh2c07ded2021-01-04 09:17:49 -06001089 if (copy_to_user(report, &params, sizeof(params)))
1090 ret = -EFAULT;
1091e_free_blob:
1092 kfree(blob);
Brijesh Singh2c07ded2021-01-04 09:17:49 -06001093 return ret;
1094}
1095
Brijesh Singh4cfdd472021-04-15 15:53:14 +00001096/* Userspace wants to query session length. */
1097static int
1098__sev_send_start_query_session_length(struct kvm *kvm, struct kvm_sev_cmd *argp,
1099 struct kvm_sev_send_start *params)
1100{
1101 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
Sean Christopherson238eca82021-04-06 15:49:52 -07001102 struct sev_data_send_start data;
Brijesh Singh4cfdd472021-04-15 15:53:14 +00001103 int ret;
1104
Sean Christopherson238eca82021-04-06 15:49:52 -07001105 data.handle = sev->handle;
1106 ret = sev_issue_cmd(kvm, SEV_CMD_SEND_START, &data, &argp->error);
Brijesh Singh4cfdd472021-04-15 15:53:14 +00001107 if (ret < 0)
Sean Christopherson238eca82021-04-06 15:49:52 -07001108 return ret;
Brijesh Singh4cfdd472021-04-15 15:53:14 +00001109
Sean Christopherson238eca82021-04-06 15:49:52 -07001110 params->session_len = data.session_len;
Brijesh Singh4cfdd472021-04-15 15:53:14 +00001111 if (copy_to_user((void __user *)(uintptr_t)argp->data, params,
1112 sizeof(struct kvm_sev_send_start)))
1113 ret = -EFAULT;
1114
Brijesh Singh4cfdd472021-04-15 15:53:14 +00001115 return ret;
1116}
1117
1118static int sev_send_start(struct kvm *kvm, struct kvm_sev_cmd *argp)
1119{
1120 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
Sean Christopherson238eca82021-04-06 15:49:52 -07001121 struct sev_data_send_start data;
Brijesh Singh4cfdd472021-04-15 15:53:14 +00001122 struct kvm_sev_send_start params;
1123 void *amd_certs, *session_data;
1124 void *pdh_cert, *plat_certs;
1125 int ret;
1126
1127 if (!sev_guest(kvm))
1128 return -ENOTTY;
1129
1130 if (copy_from_user(&params, (void __user *)(uintptr_t)argp->data,
1131 sizeof(struct kvm_sev_send_start)))
1132 return -EFAULT;
1133
1134 /* if session_len is zero, userspace wants to query the session length */
1135 if (!params.session_len)
1136 return __sev_send_start_query_session_length(kvm, argp,
1137 &params);
1138
1139 /* some sanity checks */
1140 if (!params.pdh_cert_uaddr || !params.pdh_cert_len ||
1141 !params.session_uaddr || params.session_len > SEV_FW_BLOB_MAX_SIZE)
1142 return -EINVAL;
1143
1144 /* allocate the memory to hold the session data blob */
1145 session_data = kmalloc(params.session_len, GFP_KERNEL_ACCOUNT);
1146 if (!session_data)
1147 return -ENOMEM;
1148
1149 /* copy the certificate blobs from userspace */
1150 pdh_cert = psp_copy_user_blob(params.pdh_cert_uaddr,
1151 params.pdh_cert_len);
1152 if (IS_ERR(pdh_cert)) {
1153 ret = PTR_ERR(pdh_cert);
1154 goto e_free_session;
1155 }
1156
1157 plat_certs = psp_copy_user_blob(params.plat_certs_uaddr,
1158 params.plat_certs_len);
1159 if (IS_ERR(plat_certs)) {
1160 ret = PTR_ERR(plat_certs);
1161 goto e_free_pdh;
1162 }
1163
1164 amd_certs = psp_copy_user_blob(params.amd_certs_uaddr,
1165 params.amd_certs_len);
1166 if (IS_ERR(amd_certs)) {
1167 ret = PTR_ERR(amd_certs);
1168 goto e_free_plat_cert;
1169 }
1170
Brijesh Singh4cfdd472021-04-15 15:53:14 +00001171 /* populate the FW SEND_START field with system physical address */
Sean Christopherson238eca82021-04-06 15:49:52 -07001172 memset(&data, 0, sizeof(data));
1173 data.pdh_cert_address = __psp_pa(pdh_cert);
1174 data.pdh_cert_len = params.pdh_cert_len;
1175 data.plat_certs_address = __psp_pa(plat_certs);
1176 data.plat_certs_len = params.plat_certs_len;
1177 data.amd_certs_address = __psp_pa(amd_certs);
1178 data.amd_certs_len = params.amd_certs_len;
1179 data.session_address = __psp_pa(session_data);
1180 data.session_len = params.session_len;
1181 data.handle = sev->handle;
Brijesh Singh4cfdd472021-04-15 15:53:14 +00001182
Sean Christopherson238eca82021-04-06 15:49:52 -07001183 ret = sev_issue_cmd(kvm, SEV_CMD_SEND_START, &data, &argp->error);
Brijesh Singh4cfdd472021-04-15 15:53:14 +00001184
1185 if (!ret && copy_to_user((void __user *)(uintptr_t)params.session_uaddr,
1186 session_data, params.session_len)) {
1187 ret = -EFAULT;
Sean Christopherson238eca82021-04-06 15:49:52 -07001188 goto e_free_amd_cert;
Brijesh Singh4cfdd472021-04-15 15:53:14 +00001189 }
1190
Sean Christopherson238eca82021-04-06 15:49:52 -07001191 params.policy = data.policy;
1192 params.session_len = data.session_len;
Brijesh Singh4cfdd472021-04-15 15:53:14 +00001193 if (copy_to_user((void __user *)(uintptr_t)argp->data, &params,
1194 sizeof(struct kvm_sev_send_start)))
1195 ret = -EFAULT;
1196
Brijesh Singh4cfdd472021-04-15 15:53:14 +00001197e_free_amd_cert:
1198 kfree(amd_certs);
1199e_free_plat_cert:
1200 kfree(plat_certs);
1201e_free_pdh:
1202 kfree(pdh_cert);
1203e_free_session:
1204 kfree(session_data);
1205 return ret;
1206}
1207
Brijesh Singhd3d1af82021-04-15 15:53:55 +00001208/* Userspace wants to query either header or trans length. */
1209static int
1210__sev_send_update_data_query_lengths(struct kvm *kvm, struct kvm_sev_cmd *argp,
1211 struct kvm_sev_send_update_data *params)
1212{
1213 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
Sean Christopherson238eca82021-04-06 15:49:52 -07001214 struct sev_data_send_update_data data;
Brijesh Singhd3d1af82021-04-15 15:53:55 +00001215 int ret;
1216
Sean Christopherson238eca82021-04-06 15:49:52 -07001217 data.handle = sev->handle;
1218 ret = sev_issue_cmd(kvm, SEV_CMD_SEND_UPDATE_DATA, &data, &argp->error);
Brijesh Singhd3d1af82021-04-15 15:53:55 +00001219 if (ret < 0)
Sean Christopherson238eca82021-04-06 15:49:52 -07001220 return ret;
Brijesh Singhd3d1af82021-04-15 15:53:55 +00001221
Sean Christopherson238eca82021-04-06 15:49:52 -07001222 params->hdr_len = data.hdr_len;
1223 params->trans_len = data.trans_len;
Brijesh Singhd3d1af82021-04-15 15:53:55 +00001224
1225 if (copy_to_user((void __user *)(uintptr_t)argp->data, params,
1226 sizeof(struct kvm_sev_send_update_data)))
1227 ret = -EFAULT;
1228
Brijesh Singhd3d1af82021-04-15 15:53:55 +00001229 return ret;
1230}
1231
1232static int sev_send_update_data(struct kvm *kvm, struct kvm_sev_cmd *argp)
1233{
1234 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
Sean Christopherson238eca82021-04-06 15:49:52 -07001235 struct sev_data_send_update_data data;
Brijesh Singhd3d1af82021-04-15 15:53:55 +00001236 struct kvm_sev_send_update_data params;
1237 void *hdr, *trans_data;
1238 struct page **guest_page;
1239 unsigned long n;
1240 int ret, offset;
1241
1242 if (!sev_guest(kvm))
1243 return -ENOTTY;
1244
1245 if (copy_from_user(&params, (void __user *)(uintptr_t)argp->data,
1246 sizeof(struct kvm_sev_send_update_data)))
1247 return -EFAULT;
1248
1249 /* userspace wants to query either header or trans length */
1250 if (!params.trans_len || !params.hdr_len)
1251 return __sev_send_update_data_query_lengths(kvm, argp, &params);
1252
1253 if (!params.trans_uaddr || !params.guest_uaddr ||
1254 !params.guest_len || !params.hdr_uaddr)
1255 return -EINVAL;
1256
1257 /* Check if we are crossing the page boundary */
1258 offset = params.guest_uaddr & (PAGE_SIZE - 1);
1259 if ((params.guest_len + offset > PAGE_SIZE))
1260 return -EINVAL;
1261
1262 /* Pin guest memory */
1263 guest_page = sev_pin_memory(kvm, params.guest_uaddr & PAGE_MASK,
1264 PAGE_SIZE, &n, 0);
1265 if (!guest_page)
1266 return -EFAULT;
1267
1268 /* allocate memory for header and transport buffer */
1269 ret = -ENOMEM;
1270 hdr = kmalloc(params.hdr_len, GFP_KERNEL_ACCOUNT);
1271 if (!hdr)
1272 goto e_unpin;
1273
1274 trans_data = kmalloc(params.trans_len, GFP_KERNEL_ACCOUNT);
1275 if (!trans_data)
1276 goto e_free_hdr;
1277
Sean Christopherson238eca82021-04-06 15:49:52 -07001278 memset(&data, 0, sizeof(data));
1279 data.hdr_address = __psp_pa(hdr);
1280 data.hdr_len = params.hdr_len;
1281 data.trans_address = __psp_pa(trans_data);
1282 data.trans_len = params.trans_len;
Brijesh Singhd3d1af82021-04-15 15:53:55 +00001283
1284 /* The SEND_UPDATE_DATA command requires C-bit to be always set. */
Sean Christopherson238eca82021-04-06 15:49:52 -07001285 data.guest_address = (page_to_pfn(guest_page[0]) << PAGE_SHIFT) + offset;
1286 data.guest_address |= sev_me_mask;
1287 data.guest_len = params.guest_len;
1288 data.handle = sev->handle;
Brijesh Singhd3d1af82021-04-15 15:53:55 +00001289
Sean Christopherson238eca82021-04-06 15:49:52 -07001290 ret = sev_issue_cmd(kvm, SEV_CMD_SEND_UPDATE_DATA, &data, &argp->error);
Brijesh Singhd3d1af82021-04-15 15:53:55 +00001291
1292 if (ret)
Sean Christopherson238eca82021-04-06 15:49:52 -07001293 goto e_free_trans_data;
Brijesh Singhd3d1af82021-04-15 15:53:55 +00001294
1295 /* copy transport buffer to user space */
1296 if (copy_to_user((void __user *)(uintptr_t)params.trans_uaddr,
1297 trans_data, params.trans_len)) {
1298 ret = -EFAULT;
Sean Christopherson238eca82021-04-06 15:49:52 -07001299 goto e_free_trans_data;
Brijesh Singhd3d1af82021-04-15 15:53:55 +00001300 }
1301
1302 /* Copy packet header to userspace. */
1303 ret = copy_to_user((void __user *)(uintptr_t)params.hdr_uaddr, hdr,
1304 params.hdr_len);
1305
Brijesh Singhd3d1af82021-04-15 15:53:55 +00001306e_free_trans_data:
1307 kfree(trans_data);
1308e_free_hdr:
1309 kfree(hdr);
1310e_unpin:
1311 sev_unpin_memory(kvm, guest_page, n);
1312
1313 return ret;
1314}
1315
Brijesh Singhfddecf62021-04-15 15:54:15 +00001316static int sev_send_finish(struct kvm *kvm, struct kvm_sev_cmd *argp)
1317{
1318 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
Sean Christopherson238eca82021-04-06 15:49:52 -07001319 struct sev_data_send_finish data;
Brijesh Singhfddecf62021-04-15 15:54:15 +00001320
1321 if (!sev_guest(kvm))
1322 return -ENOTTY;
1323
Sean Christopherson238eca82021-04-06 15:49:52 -07001324 data.handle = sev->handle;
1325 return sev_issue_cmd(kvm, SEV_CMD_SEND_FINISH, &data, &argp->error);
Brijesh Singhfddecf62021-04-15 15:54:15 +00001326}
1327
Steve Rutherford5569e2e2021-04-20 05:01:20 -04001328static int sev_send_cancel(struct kvm *kvm, struct kvm_sev_cmd *argp)
1329{
1330 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
Sean Christopherson238eca82021-04-06 15:49:52 -07001331 struct sev_data_send_cancel data;
Steve Rutherford5569e2e2021-04-20 05:01:20 -04001332
1333 if (!sev_guest(kvm))
1334 return -ENOTTY;
1335
Sean Christopherson238eca82021-04-06 15:49:52 -07001336 data.handle = sev->handle;
1337 return sev_issue_cmd(kvm, SEV_CMD_SEND_CANCEL, &data, &argp->error);
Steve Rutherford5569e2e2021-04-20 05:01:20 -04001338}
1339
Brijesh Singhaf43cbb2021-04-15 15:54:50 +00001340static int sev_receive_start(struct kvm *kvm, struct kvm_sev_cmd *argp)
1341{
1342 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
Sean Christopherson238eca82021-04-06 15:49:52 -07001343 struct sev_data_receive_start start;
Brijesh Singhaf43cbb2021-04-15 15:54:50 +00001344 struct kvm_sev_receive_start params;
1345 int *error = &argp->error;
1346 void *session_data;
1347 void *pdh_data;
1348 int ret;
1349
1350 if (!sev_guest(kvm))
1351 return -ENOTTY;
1352
1353 /* Get parameter from the userspace */
1354 if (copy_from_user(&params, (void __user *)(uintptr_t)argp->data,
1355 sizeof(struct kvm_sev_receive_start)))
1356 return -EFAULT;
1357
1358 /* some sanity checks */
1359 if (!params.pdh_uaddr || !params.pdh_len ||
1360 !params.session_uaddr || !params.session_len)
1361 return -EINVAL;
1362
1363 pdh_data = psp_copy_user_blob(params.pdh_uaddr, params.pdh_len);
1364 if (IS_ERR(pdh_data))
1365 return PTR_ERR(pdh_data);
1366
1367 session_data = psp_copy_user_blob(params.session_uaddr,
1368 params.session_len);
1369 if (IS_ERR(session_data)) {
1370 ret = PTR_ERR(session_data);
1371 goto e_free_pdh;
1372 }
1373
Sean Christopherson238eca82021-04-06 15:49:52 -07001374 memset(&start, 0, sizeof(start));
1375 start.handle = params.handle;
1376 start.policy = params.policy;
1377 start.pdh_cert_address = __psp_pa(pdh_data);
1378 start.pdh_cert_len = params.pdh_len;
1379 start.session_address = __psp_pa(session_data);
1380 start.session_len = params.session_len;
Brijesh Singhaf43cbb2021-04-15 15:54:50 +00001381
1382 /* create memory encryption context */
Sean Christopherson238eca82021-04-06 15:49:52 -07001383 ret = __sev_issue_cmd(argp->sev_fd, SEV_CMD_RECEIVE_START, &start,
Brijesh Singhaf43cbb2021-04-15 15:54:50 +00001384 error);
1385 if (ret)
Sean Christopherson238eca82021-04-06 15:49:52 -07001386 goto e_free_session;
Brijesh Singhaf43cbb2021-04-15 15:54:50 +00001387
1388 /* Bind ASID to this guest */
Sean Christopherson238eca82021-04-06 15:49:52 -07001389 ret = sev_bind_asid(kvm, start.handle, error);
Brijesh Singhaf43cbb2021-04-15 15:54:50 +00001390 if (ret)
Sean Christopherson238eca82021-04-06 15:49:52 -07001391 goto e_free_session;
Brijesh Singhaf43cbb2021-04-15 15:54:50 +00001392
Sean Christopherson238eca82021-04-06 15:49:52 -07001393 params.handle = start.handle;
Brijesh Singhaf43cbb2021-04-15 15:54:50 +00001394 if (copy_to_user((void __user *)(uintptr_t)argp->data,
1395 &params, sizeof(struct kvm_sev_receive_start))) {
1396 ret = -EFAULT;
Sean Christopherson238eca82021-04-06 15:49:52 -07001397 sev_unbind_asid(kvm, start.handle);
1398 goto e_free_session;
Brijesh Singhaf43cbb2021-04-15 15:54:50 +00001399 }
1400
Sean Christopherson238eca82021-04-06 15:49:52 -07001401 sev->handle = start.handle;
Brijesh Singhaf43cbb2021-04-15 15:54:50 +00001402 sev->fd = argp->sev_fd;
1403
Brijesh Singhaf43cbb2021-04-15 15:54:50 +00001404e_free_session:
1405 kfree(session_data);
1406e_free_pdh:
1407 kfree(pdh_data);
1408
1409 return ret;
1410}
1411
Brijesh Singh15fb7de2021-04-15 15:55:17 +00001412static int sev_receive_update_data(struct kvm *kvm, struct kvm_sev_cmd *argp)
1413{
1414 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
1415 struct kvm_sev_receive_update_data params;
Sean Christopherson238eca82021-04-06 15:49:52 -07001416 struct sev_data_receive_update_data data;
Brijesh Singh15fb7de2021-04-15 15:55:17 +00001417 void *hdr = NULL, *trans = NULL;
1418 struct page **guest_page;
1419 unsigned long n;
1420 int ret, offset;
1421
1422 if (!sev_guest(kvm))
1423 return -EINVAL;
1424
1425 if (copy_from_user(&params, (void __user *)(uintptr_t)argp->data,
1426 sizeof(struct kvm_sev_receive_update_data)))
1427 return -EFAULT;
1428
1429 if (!params.hdr_uaddr || !params.hdr_len ||
1430 !params.guest_uaddr || !params.guest_len ||
1431 !params.trans_uaddr || !params.trans_len)
1432 return -EINVAL;
1433
1434 /* Check if we are crossing the page boundary */
1435 offset = params.guest_uaddr & (PAGE_SIZE - 1);
1436 if ((params.guest_len + offset > PAGE_SIZE))
1437 return -EINVAL;
1438
1439 hdr = psp_copy_user_blob(params.hdr_uaddr, params.hdr_len);
1440 if (IS_ERR(hdr))
1441 return PTR_ERR(hdr);
1442
1443 trans = psp_copy_user_blob(params.trans_uaddr, params.trans_len);
1444 if (IS_ERR(trans)) {
1445 ret = PTR_ERR(trans);
1446 goto e_free_hdr;
1447 }
1448
Sean Christopherson238eca82021-04-06 15:49:52 -07001449 memset(&data, 0, sizeof(data));
1450 data.hdr_address = __psp_pa(hdr);
1451 data.hdr_len = params.hdr_len;
1452 data.trans_address = __psp_pa(trans);
1453 data.trans_len = params.trans_len;
Brijesh Singh15fb7de2021-04-15 15:55:17 +00001454
1455 /* Pin guest memory */
1456 ret = -EFAULT;
1457 guest_page = sev_pin_memory(kvm, params.guest_uaddr & PAGE_MASK,
1458 PAGE_SIZE, &n, 0);
1459 if (!guest_page)
Sean Christopherson238eca82021-04-06 15:49:52 -07001460 goto e_free_trans;
Brijesh Singh15fb7de2021-04-15 15:55:17 +00001461
1462 /* The RECEIVE_UPDATE_DATA command requires C-bit to be always set. */
Sean Christopherson238eca82021-04-06 15:49:52 -07001463 data.guest_address = (page_to_pfn(guest_page[0]) << PAGE_SHIFT) + offset;
1464 data.guest_address |= sev_me_mask;
1465 data.guest_len = params.guest_len;
1466 data.handle = sev->handle;
Brijesh Singh15fb7de2021-04-15 15:55:17 +00001467
Sean Christopherson238eca82021-04-06 15:49:52 -07001468 ret = sev_issue_cmd(kvm, SEV_CMD_RECEIVE_UPDATE_DATA, &data,
Brijesh Singh15fb7de2021-04-15 15:55:17 +00001469 &argp->error);
1470
1471 sev_unpin_memory(kvm, guest_page, n);
1472
Brijesh Singh15fb7de2021-04-15 15:55:17 +00001473e_free_trans:
1474 kfree(trans);
1475e_free_hdr:
1476 kfree(hdr);
1477
1478 return ret;
1479}
1480
Brijesh Singh6a443de2021-04-15 15:55:40 +00001481static int sev_receive_finish(struct kvm *kvm, struct kvm_sev_cmd *argp)
1482{
1483 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
Sean Christopherson238eca82021-04-06 15:49:52 -07001484 struct sev_data_receive_finish data;
Brijesh Singh6a443de2021-04-15 15:55:40 +00001485
1486 if (!sev_guest(kvm))
1487 return -ENOTTY;
1488
Sean Christopherson238eca82021-04-06 15:49:52 -07001489 data.handle = sev->handle;
1490 return sev_issue_cmd(kvm, SEV_CMD_RECEIVE_FINISH, &data, &argp->error);
Brijesh Singh6a443de2021-04-15 15:55:40 +00001491}
1492
Joerg Roedeleaf78262020-03-24 10:41:54 +01001493int svm_mem_enc_op(struct kvm *kvm, void __user *argp)
1494{
1495 struct kvm_sev_cmd sev_cmd;
1496 int r;
1497
Tom Lendacky916391a2020-12-10 11:09:38 -06001498 if (!svm_sev_enabled() || !sev)
Joerg Roedeleaf78262020-03-24 10:41:54 +01001499 return -ENOTTY;
1500
1501 if (!argp)
1502 return 0;
1503
1504 if (copy_from_user(&sev_cmd, argp, sizeof(struct kvm_sev_cmd)))
1505 return -EFAULT;
1506
1507 mutex_lock(&kvm->lock);
1508
Nathan Tempelman54526d12021-04-08 22:32:14 +00001509 /* enc_context_owner handles all memory enc operations */
1510 if (is_mirroring_enc_context(kvm)) {
1511 r = -EINVAL;
1512 goto out;
1513 }
1514
Joerg Roedeleaf78262020-03-24 10:41:54 +01001515 switch (sev_cmd.id) {
Sean Christopherson9fa15212021-03-30 20:19:35 -07001516 case KVM_SEV_ES_INIT:
1517 if (!sev_es) {
1518 r = -ENOTTY;
1519 goto out;
1520 }
1521 fallthrough;
Joerg Roedeleaf78262020-03-24 10:41:54 +01001522 case KVM_SEV_INIT:
1523 r = sev_guest_init(kvm, &sev_cmd);
1524 break;
1525 case KVM_SEV_LAUNCH_START:
1526 r = sev_launch_start(kvm, &sev_cmd);
1527 break;
1528 case KVM_SEV_LAUNCH_UPDATE_DATA:
1529 r = sev_launch_update_data(kvm, &sev_cmd);
1530 break;
Tom Lendackyad731092020-12-10 11:10:09 -06001531 case KVM_SEV_LAUNCH_UPDATE_VMSA:
1532 r = sev_launch_update_vmsa(kvm, &sev_cmd);
1533 break;
Joerg Roedeleaf78262020-03-24 10:41:54 +01001534 case KVM_SEV_LAUNCH_MEASURE:
1535 r = sev_launch_measure(kvm, &sev_cmd);
1536 break;
1537 case KVM_SEV_LAUNCH_FINISH:
1538 r = sev_launch_finish(kvm, &sev_cmd);
1539 break;
1540 case KVM_SEV_GUEST_STATUS:
1541 r = sev_guest_status(kvm, &sev_cmd);
1542 break;
1543 case KVM_SEV_DBG_DECRYPT:
1544 r = sev_dbg_crypt(kvm, &sev_cmd, true);
1545 break;
1546 case KVM_SEV_DBG_ENCRYPT:
1547 r = sev_dbg_crypt(kvm, &sev_cmd, false);
1548 break;
1549 case KVM_SEV_LAUNCH_SECRET:
1550 r = sev_launch_secret(kvm, &sev_cmd);
1551 break;
Brijesh Singh2c07ded2021-01-04 09:17:49 -06001552 case KVM_SEV_GET_ATTESTATION_REPORT:
1553 r = sev_get_attestation_report(kvm, &sev_cmd);
1554 break;
Brijesh Singh4cfdd472021-04-15 15:53:14 +00001555 case KVM_SEV_SEND_START:
1556 r = sev_send_start(kvm, &sev_cmd);
1557 break;
Brijesh Singhd3d1af82021-04-15 15:53:55 +00001558 case KVM_SEV_SEND_UPDATE_DATA:
1559 r = sev_send_update_data(kvm, &sev_cmd);
1560 break;
Brijesh Singhfddecf62021-04-15 15:54:15 +00001561 case KVM_SEV_SEND_FINISH:
1562 r = sev_send_finish(kvm, &sev_cmd);
1563 break;
Steve Rutherford5569e2e2021-04-20 05:01:20 -04001564 case KVM_SEV_SEND_CANCEL:
1565 r = sev_send_cancel(kvm, &sev_cmd);
1566 break;
Brijesh Singhaf43cbb2021-04-15 15:54:50 +00001567 case KVM_SEV_RECEIVE_START:
1568 r = sev_receive_start(kvm, &sev_cmd);
1569 break;
Brijesh Singh15fb7de2021-04-15 15:55:17 +00001570 case KVM_SEV_RECEIVE_UPDATE_DATA:
1571 r = sev_receive_update_data(kvm, &sev_cmd);
1572 break;
Brijesh Singh6a443de2021-04-15 15:55:40 +00001573 case KVM_SEV_RECEIVE_FINISH:
1574 r = sev_receive_finish(kvm, &sev_cmd);
1575 break;
Joerg Roedeleaf78262020-03-24 10:41:54 +01001576 default:
1577 r = -EINVAL;
1578 goto out;
1579 }
1580
1581 if (copy_to_user(argp, &sev_cmd, sizeof(struct kvm_sev_cmd)))
1582 r = -EFAULT;
1583
1584out:
1585 mutex_unlock(&kvm->lock);
1586 return r;
1587}
1588
1589int svm_register_enc_region(struct kvm *kvm,
1590 struct kvm_enc_region *range)
1591{
1592 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
1593 struct enc_region *region;
1594 int ret = 0;
1595
1596 if (!sev_guest(kvm))
1597 return -ENOTTY;
1598
Nathan Tempelman54526d12021-04-08 22:32:14 +00001599 /* If kvm is mirroring encryption context it isn't responsible for it */
1600 if (is_mirroring_enc_context(kvm))
1601 return -EINVAL;
1602
Joerg Roedeleaf78262020-03-24 10:41:54 +01001603 if (range->addr > ULONG_MAX || range->size > ULONG_MAX)
1604 return -EINVAL;
1605
1606 region = kzalloc(sizeof(*region), GFP_KERNEL_ACCOUNT);
1607 if (!region)
1608 return -ENOMEM;
1609
Peter Gonda19a23da2021-01-27 08:15:24 -08001610 mutex_lock(&kvm->lock);
Joerg Roedeleaf78262020-03-24 10:41:54 +01001611 region->pages = sev_pin_memory(kvm, range->addr, range->size, &region->npages, 1);
Paolo Bonzinia8d908b2020-06-23 05:12:24 -04001612 if (IS_ERR(region->pages)) {
1613 ret = PTR_ERR(region->pages);
Peter Gonda19a23da2021-01-27 08:15:24 -08001614 mutex_unlock(&kvm->lock);
Joerg Roedeleaf78262020-03-24 10:41:54 +01001615 goto e_free;
1616 }
1617
Peter Gonda19a23da2021-01-27 08:15:24 -08001618 region->uaddr = range->addr;
1619 region->size = range->size;
1620
1621 list_add_tail(&region->list, &sev->regions_list);
1622 mutex_unlock(&kvm->lock);
1623
Joerg Roedeleaf78262020-03-24 10:41:54 +01001624 /*
1625 * The guest may change the memory encryption attribute from C=0 -> C=1
1626 * or vice versa for this memory range. Lets make sure caches are
1627 * flushed to ensure that guest data gets written into memory with
1628 * correct C-bit.
1629 */
1630 sev_clflush_pages(region->pages, region->npages);
1631
Joerg Roedeleaf78262020-03-24 10:41:54 +01001632 return ret;
1633
1634e_free:
1635 kfree(region);
1636 return ret;
1637}
1638
1639static struct enc_region *
1640find_enc_region(struct kvm *kvm, struct kvm_enc_region *range)
1641{
1642 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
1643 struct list_head *head = &sev->regions_list;
1644 struct enc_region *i;
1645
1646 list_for_each_entry(i, head, list) {
1647 if (i->uaddr == range->addr &&
1648 i->size == range->size)
1649 return i;
1650 }
1651
1652 return NULL;
1653}
1654
1655static void __unregister_enc_region_locked(struct kvm *kvm,
1656 struct enc_region *region)
1657{
1658 sev_unpin_memory(kvm, region->pages, region->npages);
1659 list_del(&region->list);
1660 kfree(region);
1661}
1662
1663int svm_unregister_enc_region(struct kvm *kvm,
1664 struct kvm_enc_region *range)
1665{
1666 struct enc_region *region;
1667 int ret;
1668
Nathan Tempelman54526d12021-04-08 22:32:14 +00001669 /* If kvm is mirroring encryption context it isn't responsible for it */
1670 if (is_mirroring_enc_context(kvm))
1671 return -EINVAL;
1672
Joerg Roedeleaf78262020-03-24 10:41:54 +01001673 mutex_lock(&kvm->lock);
1674
1675 if (!sev_guest(kvm)) {
1676 ret = -ENOTTY;
1677 goto failed;
1678 }
1679
1680 region = find_enc_region(kvm, range);
1681 if (!region) {
1682 ret = -EINVAL;
1683 goto failed;
1684 }
1685
1686 /*
1687 * Ensure that all guest tagged cache entries are flushed before
1688 * releasing the pages back to the system for use. CLFLUSH will
1689 * not do this, so issue a WBINVD.
1690 */
1691 wbinvd_on_all_cpus();
1692
1693 __unregister_enc_region_locked(kvm, region);
1694
1695 mutex_unlock(&kvm->lock);
1696 return 0;
1697
1698failed:
1699 mutex_unlock(&kvm->lock);
1700 return ret;
1701}
1702
Nathan Tempelman54526d12021-04-08 22:32:14 +00001703int svm_vm_copy_asid_from(struct kvm *kvm, unsigned int source_fd)
1704{
1705 struct file *source_kvm_file;
1706 struct kvm *source_kvm;
1707 struct kvm_sev_info *mirror_sev;
1708 unsigned int asid;
1709 int ret;
1710
1711 source_kvm_file = fget(source_fd);
1712 if (!file_is_kvm(source_kvm_file)) {
1713 ret = -EBADF;
1714 goto e_source_put;
1715 }
1716
1717 source_kvm = source_kvm_file->private_data;
1718 mutex_lock(&source_kvm->lock);
1719
1720 if (!sev_guest(source_kvm)) {
1721 ret = -EINVAL;
1722 goto e_source_unlock;
1723 }
1724
1725 /* Mirrors of mirrors should work, but let's not get silly */
1726 if (is_mirroring_enc_context(source_kvm) || source_kvm == kvm) {
1727 ret = -EINVAL;
1728 goto e_source_unlock;
1729 }
1730
1731 asid = to_kvm_svm(source_kvm)->sev_info.asid;
1732
1733 /*
1734 * The mirror kvm holds an enc_context_owner ref so its asid can't
1735 * disappear until we're done with it
1736 */
1737 kvm_get_kvm(source_kvm);
1738
1739 fput(source_kvm_file);
1740 mutex_unlock(&source_kvm->lock);
1741 mutex_lock(&kvm->lock);
1742
1743 if (sev_guest(kvm)) {
1744 ret = -EINVAL;
1745 goto e_mirror_unlock;
1746 }
1747
1748 /* Set enc_context_owner and copy its encryption context over */
1749 mirror_sev = &to_kvm_svm(kvm)->sev_info;
1750 mirror_sev->enc_context_owner = source_kvm;
1751 mirror_sev->asid = asid;
1752 mirror_sev->active = true;
1753
1754 mutex_unlock(&kvm->lock);
1755 return 0;
1756
1757e_mirror_unlock:
1758 mutex_unlock(&kvm->lock);
1759 kvm_put_kvm(source_kvm);
1760 return ret;
1761e_source_unlock:
1762 mutex_unlock(&source_kvm->lock);
1763e_source_put:
1764 fput(source_kvm_file);
1765 return ret;
1766}
1767
Joerg Roedeleaf78262020-03-24 10:41:54 +01001768void sev_vm_destroy(struct kvm *kvm)
1769{
1770 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
1771 struct list_head *head = &sev->regions_list;
1772 struct list_head *pos, *q;
1773
1774 if (!sev_guest(kvm))
1775 return;
1776
Nathan Tempelman54526d12021-04-08 22:32:14 +00001777 /* If this is a mirror_kvm release the enc_context_owner and skip sev cleanup */
1778 if (is_mirroring_enc_context(kvm)) {
1779 kvm_put_kvm(sev->enc_context_owner);
1780 return;
1781 }
1782
Joerg Roedeleaf78262020-03-24 10:41:54 +01001783 mutex_lock(&kvm->lock);
1784
1785 /*
1786 * Ensure that all guest tagged cache entries are flushed before
1787 * releasing the pages back to the system for use. CLFLUSH will
1788 * not do this, so issue a WBINVD.
1789 */
1790 wbinvd_on_all_cpus();
1791
1792 /*
1793 * if userspace was terminated before unregistering the memory regions
1794 * then lets unpin all the registered memory.
1795 */
1796 if (!list_empty(head)) {
1797 list_for_each_safe(pos, q, head) {
1798 __unregister_enc_region_locked(kvm,
1799 list_entry(pos, struct enc_region, list));
David Rientjes7be74942020-08-25 12:56:28 -07001800 cond_resched();
Joerg Roedeleaf78262020-03-24 10:41:54 +01001801 }
1802 }
1803
1804 mutex_unlock(&kvm->lock);
1805
1806 sev_unbind_asid(kvm, sev->handle);
Vipin Sharma7aef27f2021-03-29 21:42:06 -07001807 sev_asid_free(sev);
Joerg Roedeleaf78262020-03-24 10:41:54 +01001808}
1809
Tom Lendacky916391a2020-12-10 11:09:38 -06001810void __init sev_hardware_setup(void)
Joerg Roedeleaf78262020-03-24 10:41:54 +01001811{
Vipin Sharma7aef27f2021-03-29 21:42:06 -07001812 unsigned int eax, ebx, ecx, edx, sev_asid_count, sev_es_asid_count;
Tom Lendacky916391a2020-12-10 11:09:38 -06001813 bool sev_es_supported = false;
1814 bool sev_supported = false;
1815
Sean Christophersone8126bd2021-04-21 19:11:14 -07001816 if (!IS_ENABLED(CONFIG_KVM_AMD_SEV) || !sev || !npt_enabled)
1817 goto out;
1818
Tom Lendacky916391a2020-12-10 11:09:38 -06001819 /* Does the CPU support SEV? */
1820 if (!boot_cpu_has(X86_FEATURE_SEV))
1821 goto out;
1822
1823 /* Retrieve SEV CPUID information */
1824 cpuid(0x8000001f, &eax, &ebx, &ecx, &edx);
1825
Tom Lendacky1edc1452020-12-10 11:09:49 -06001826 /* Set encryption bit location for SEV-ES guests */
1827 sev_enc_bit = ebx & 0x3f;
1828
Joerg Roedeleaf78262020-03-24 10:41:54 +01001829 /* Maximum number of encrypted guests supported simultaneously */
Tom Lendacky916391a2020-12-10 11:09:38 -06001830 max_sev_asid = ecx;
Joerg Roedeleaf78262020-03-24 10:41:54 +01001831
Paolo Bonzini9ef15302020-04-13 03:20:06 -04001832 if (!svm_sev_enabled())
Tom Lendacky916391a2020-12-10 11:09:38 -06001833 goto out;
Joerg Roedeleaf78262020-03-24 10:41:54 +01001834
1835 /* Minimum ASID value that should be used for SEV guest */
Tom Lendacky916391a2020-12-10 11:09:38 -06001836 min_sev_asid = edx;
Brijesh Singhd3d1af82021-04-15 15:53:55 +00001837 sev_me_mask = 1UL << (ebx & 0x3f);
Joerg Roedeleaf78262020-03-24 10:41:54 +01001838
1839 /* Initialize SEV ASID bitmaps */
1840 sev_asid_bitmap = bitmap_zalloc(max_sev_asid, GFP_KERNEL);
1841 if (!sev_asid_bitmap)
Tom Lendacky916391a2020-12-10 11:09:38 -06001842 goto out;
Joerg Roedeleaf78262020-03-24 10:41:54 +01001843
1844 sev_reclaim_asid_bitmap = bitmap_zalloc(max_sev_asid, GFP_KERNEL);
Sean Christophersonf31b88b2021-04-21 19:11:12 -07001845 if (!sev_reclaim_asid_bitmap) {
1846 bitmap_free(sev_asid_bitmap);
1847 sev_asid_bitmap = NULL;
Tom Lendacky916391a2020-12-10 11:09:38 -06001848 goto out;
Sean Christophersonf31b88b2021-04-21 19:11:12 -07001849 }
Joerg Roedeleaf78262020-03-24 10:41:54 +01001850
Vipin Sharma7aef27f2021-03-29 21:42:06 -07001851 sev_asid_count = max_sev_asid - min_sev_asid + 1;
1852 if (misc_cg_set_capacity(MISC_CG_RES_SEV, sev_asid_count))
1853 goto out;
1854
1855 pr_info("SEV supported: %u ASIDs\n", sev_asid_count);
Tom Lendacky916391a2020-12-10 11:09:38 -06001856 sev_supported = true;
Joerg Roedeleaf78262020-03-24 10:41:54 +01001857
Tom Lendacky916391a2020-12-10 11:09:38 -06001858 /* SEV-ES support requested? */
1859 if (!sev_es)
1860 goto out;
1861
1862 /* Does the CPU support SEV-ES? */
1863 if (!boot_cpu_has(X86_FEATURE_SEV_ES))
1864 goto out;
1865
1866 /* Has the system been allocated ASIDs for SEV-ES? */
1867 if (min_sev_asid == 1)
1868 goto out;
1869
Vipin Sharma7aef27f2021-03-29 21:42:06 -07001870 sev_es_asid_count = min_sev_asid - 1;
1871 if (misc_cg_set_capacity(MISC_CG_RES_SEV_ES, sev_es_asid_count))
1872 goto out;
1873
1874 pr_info("SEV-ES supported: %u ASIDs\n", sev_es_asid_count);
Tom Lendacky916391a2020-12-10 11:09:38 -06001875 sev_es_supported = true;
1876
1877out:
1878 sev = sev_supported;
1879 sev_es = sev_es_supported;
Joerg Roedeleaf78262020-03-24 10:41:54 +01001880}
1881
1882void sev_hardware_teardown(void)
1883{
Paolo Bonzini9ef15302020-04-13 03:20:06 -04001884 if (!svm_sev_enabled())
1885 return;
1886
Joerg Roedeleaf78262020-03-24 10:41:54 +01001887 bitmap_free(sev_asid_bitmap);
1888 bitmap_free(sev_reclaim_asid_bitmap);
Vipin Sharma7aef27f2021-03-29 21:42:06 -07001889 misc_cg_set_capacity(MISC_CG_RES_SEV, 0);
1890 misc_cg_set_capacity(MISC_CG_RES_SEV_ES, 0);
Joerg Roedeleaf78262020-03-24 10:41:54 +01001891
1892 sev_flush_asids();
1893}
1894
Tom Lendackyadd5e2f2020-12-10 11:09:40 -06001895/*
1896 * Pages used by hardware to hold guest encrypted state must be flushed before
1897 * returning them to the system.
1898 */
1899static void sev_flush_guest_memory(struct vcpu_svm *svm, void *va,
1900 unsigned long len)
1901{
1902 /*
1903 * If hardware enforced cache coherency for encrypted mappings of the
1904 * same physical page is supported, nothing to do.
1905 */
1906 if (boot_cpu_has(X86_FEATURE_SME_COHERENT))
1907 return;
1908
1909 /*
1910 * If the VM Page Flush MSR is supported, use it to flush the page
1911 * (using the page virtual address and the guest ASID).
1912 */
1913 if (boot_cpu_has(X86_FEATURE_VM_PAGE_FLUSH)) {
1914 struct kvm_sev_info *sev;
1915 unsigned long va_start;
1916 u64 start, stop;
1917
1918 /* Align start and stop to page boundaries. */
1919 va_start = (unsigned long)va;
1920 start = (u64)va_start & PAGE_MASK;
1921 stop = PAGE_ALIGN((u64)va_start + len);
1922
1923 if (start < stop) {
1924 sev = &to_kvm_svm(svm->vcpu.kvm)->sev_info;
1925
1926 while (start < stop) {
1927 wrmsrl(MSR_AMD64_VM_PAGE_FLUSH,
1928 start | sev->asid);
1929
1930 start += PAGE_SIZE;
1931 }
1932
1933 return;
1934 }
1935
1936 WARN(1, "Address overflow, using WBINVD\n");
1937 }
1938
1939 /*
1940 * Hardware should always have one of the above features,
1941 * but if not, use WBINVD and issue a warning.
1942 */
1943 WARN_ONCE(1, "Using WBINVD to flush guest memory\n");
1944 wbinvd_on_all_cpus();
1945}
1946
1947void sev_free_vcpu(struct kvm_vcpu *vcpu)
1948{
1949 struct vcpu_svm *svm;
1950
1951 if (!sev_es_guest(vcpu->kvm))
1952 return;
1953
1954 svm = to_svm(vcpu);
1955
1956 if (vcpu->arch.guest_state_protected)
1957 sev_flush_guest_memory(svm, svm->vmsa, PAGE_SIZE);
1958 __free_page(virt_to_page(svm->vmsa));
Tom Lendacky8f423a82020-12-10 11:09:53 -06001959
1960 if (svm->ghcb_sa_free)
1961 kfree(svm->ghcb_sa);
Tom Lendackyadd5e2f2020-12-10 11:09:40 -06001962}
1963
Tom Lendacky291bd202020-12-10 11:09:47 -06001964static void dump_ghcb(struct vcpu_svm *svm)
1965{
1966 struct ghcb *ghcb = svm->ghcb;
1967 unsigned int nbits;
1968
1969 /* Re-use the dump_invalid_vmcb module parameter */
1970 if (!dump_invalid_vmcb) {
1971 pr_warn_ratelimited("set kvm_amd.dump_invalid_vmcb=1 to dump internal KVM state.\n");
1972 return;
1973 }
1974
1975 nbits = sizeof(ghcb->save.valid_bitmap) * 8;
1976
1977 pr_err("GHCB (GPA=%016llx):\n", svm->vmcb->control.ghcb_gpa);
1978 pr_err("%-20s%016llx is_valid: %u\n", "sw_exit_code",
1979 ghcb->save.sw_exit_code, ghcb_sw_exit_code_is_valid(ghcb));
1980 pr_err("%-20s%016llx is_valid: %u\n", "sw_exit_info_1",
1981 ghcb->save.sw_exit_info_1, ghcb_sw_exit_info_1_is_valid(ghcb));
1982 pr_err("%-20s%016llx is_valid: %u\n", "sw_exit_info_2",
1983 ghcb->save.sw_exit_info_2, ghcb_sw_exit_info_2_is_valid(ghcb));
1984 pr_err("%-20s%016llx is_valid: %u\n", "sw_scratch",
1985 ghcb->save.sw_scratch, ghcb_sw_scratch_is_valid(ghcb));
1986 pr_err("%-20s%*pb\n", "valid_bitmap", nbits, ghcb->save.valid_bitmap);
1987}
1988
1989static void sev_es_sync_to_ghcb(struct vcpu_svm *svm)
1990{
1991 struct kvm_vcpu *vcpu = &svm->vcpu;
1992 struct ghcb *ghcb = svm->ghcb;
1993
1994 /*
1995 * The GHCB protocol so far allows for the following data
1996 * to be returned:
1997 * GPRs RAX, RBX, RCX, RDX
1998 *
Sean Christopherson25009142021-01-22 15:50:47 -08001999 * Copy their values, even if they may not have been written during the
2000 * VM-Exit. It's the guest's responsibility to not consume random data.
Tom Lendacky291bd202020-12-10 11:09:47 -06002001 */
Sean Christopherson25009142021-01-22 15:50:47 -08002002 ghcb_set_rax(ghcb, vcpu->arch.regs[VCPU_REGS_RAX]);
2003 ghcb_set_rbx(ghcb, vcpu->arch.regs[VCPU_REGS_RBX]);
2004 ghcb_set_rcx(ghcb, vcpu->arch.regs[VCPU_REGS_RCX]);
2005 ghcb_set_rdx(ghcb, vcpu->arch.regs[VCPU_REGS_RDX]);
Tom Lendacky291bd202020-12-10 11:09:47 -06002006}
2007
2008static void sev_es_sync_from_ghcb(struct vcpu_svm *svm)
2009{
2010 struct vmcb_control_area *control = &svm->vmcb->control;
2011 struct kvm_vcpu *vcpu = &svm->vcpu;
2012 struct ghcb *ghcb = svm->ghcb;
2013 u64 exit_code;
2014
2015 /*
2016 * The GHCB protocol so far allows for the following data
2017 * to be supplied:
2018 * GPRs RAX, RBX, RCX, RDX
2019 * XCR0
2020 * CPL
2021 *
2022 * VMMCALL allows the guest to provide extra registers. KVM also
2023 * expects RSI for hypercalls, so include that, too.
2024 *
2025 * Copy their values to the appropriate location if supplied.
2026 */
2027 memset(vcpu->arch.regs, 0, sizeof(vcpu->arch.regs));
2028
2029 vcpu->arch.regs[VCPU_REGS_RAX] = ghcb_get_rax_if_valid(ghcb);
2030 vcpu->arch.regs[VCPU_REGS_RBX] = ghcb_get_rbx_if_valid(ghcb);
2031 vcpu->arch.regs[VCPU_REGS_RCX] = ghcb_get_rcx_if_valid(ghcb);
2032 vcpu->arch.regs[VCPU_REGS_RDX] = ghcb_get_rdx_if_valid(ghcb);
2033 vcpu->arch.regs[VCPU_REGS_RSI] = ghcb_get_rsi_if_valid(ghcb);
2034
2035 svm->vmcb->save.cpl = ghcb_get_cpl_if_valid(ghcb);
2036
2037 if (ghcb_xcr0_is_valid(ghcb)) {
2038 vcpu->arch.xcr0 = ghcb_get_xcr0(ghcb);
2039 kvm_update_cpuid_runtime(vcpu);
2040 }
2041
2042 /* Copy the GHCB exit information into the VMCB fields */
2043 exit_code = ghcb_get_sw_exit_code(ghcb);
2044 control->exit_code = lower_32_bits(exit_code);
2045 control->exit_code_hi = upper_32_bits(exit_code);
2046 control->exit_info_1 = ghcb_get_sw_exit_info_1(ghcb);
2047 control->exit_info_2 = ghcb_get_sw_exit_info_2(ghcb);
2048
2049 /* Clear the valid entries fields */
2050 memset(ghcb->save.valid_bitmap, 0, sizeof(ghcb->save.valid_bitmap));
2051}
2052
2053static int sev_es_validate_vmgexit(struct vcpu_svm *svm)
2054{
2055 struct kvm_vcpu *vcpu;
2056 struct ghcb *ghcb;
2057 u64 exit_code = 0;
2058
2059 ghcb = svm->ghcb;
2060
2061 /* Only GHCB Usage code 0 is supported */
2062 if (ghcb->ghcb_usage)
2063 goto vmgexit_err;
2064
2065 /*
2066 * Retrieve the exit code now even though is may not be marked valid
2067 * as it could help with debugging.
2068 */
2069 exit_code = ghcb_get_sw_exit_code(ghcb);
2070
2071 if (!ghcb_sw_exit_code_is_valid(ghcb) ||
2072 !ghcb_sw_exit_info_1_is_valid(ghcb) ||
2073 !ghcb_sw_exit_info_2_is_valid(ghcb))
2074 goto vmgexit_err;
2075
2076 switch (ghcb_get_sw_exit_code(ghcb)) {
2077 case SVM_EXIT_READ_DR7:
2078 break;
2079 case SVM_EXIT_WRITE_DR7:
2080 if (!ghcb_rax_is_valid(ghcb))
2081 goto vmgexit_err;
2082 break;
2083 case SVM_EXIT_RDTSC:
2084 break;
2085 case SVM_EXIT_RDPMC:
2086 if (!ghcb_rcx_is_valid(ghcb))
2087 goto vmgexit_err;
2088 break;
2089 case SVM_EXIT_CPUID:
2090 if (!ghcb_rax_is_valid(ghcb) ||
2091 !ghcb_rcx_is_valid(ghcb))
2092 goto vmgexit_err;
2093 if (ghcb_get_rax(ghcb) == 0xd)
2094 if (!ghcb_xcr0_is_valid(ghcb))
2095 goto vmgexit_err;
2096 break;
2097 case SVM_EXIT_INVD:
2098 break;
2099 case SVM_EXIT_IOIO:
Tom Lendacky7ed9abf2020-12-10 11:09:54 -06002100 if (ghcb_get_sw_exit_info_1(ghcb) & SVM_IOIO_STR_MASK) {
2101 if (!ghcb_sw_scratch_is_valid(ghcb))
Tom Lendacky291bd202020-12-10 11:09:47 -06002102 goto vmgexit_err;
Tom Lendacky7ed9abf2020-12-10 11:09:54 -06002103 } else {
2104 if (!(ghcb_get_sw_exit_info_1(ghcb) & SVM_IOIO_TYPE_MASK))
2105 if (!ghcb_rax_is_valid(ghcb))
2106 goto vmgexit_err;
2107 }
Tom Lendacky291bd202020-12-10 11:09:47 -06002108 break;
2109 case SVM_EXIT_MSR:
2110 if (!ghcb_rcx_is_valid(ghcb))
2111 goto vmgexit_err;
2112 if (ghcb_get_sw_exit_info_1(ghcb)) {
2113 if (!ghcb_rax_is_valid(ghcb) ||
2114 !ghcb_rdx_is_valid(ghcb))
2115 goto vmgexit_err;
2116 }
2117 break;
2118 case SVM_EXIT_VMMCALL:
2119 if (!ghcb_rax_is_valid(ghcb) ||
2120 !ghcb_cpl_is_valid(ghcb))
2121 goto vmgexit_err;
2122 break;
2123 case SVM_EXIT_RDTSCP:
2124 break;
2125 case SVM_EXIT_WBINVD:
2126 break;
2127 case SVM_EXIT_MONITOR:
2128 if (!ghcb_rax_is_valid(ghcb) ||
2129 !ghcb_rcx_is_valid(ghcb) ||
2130 !ghcb_rdx_is_valid(ghcb))
2131 goto vmgexit_err;
2132 break;
2133 case SVM_EXIT_MWAIT:
2134 if (!ghcb_rax_is_valid(ghcb) ||
2135 !ghcb_rcx_is_valid(ghcb))
2136 goto vmgexit_err;
2137 break;
Tom Lendacky8f423a82020-12-10 11:09:53 -06002138 case SVM_VMGEXIT_MMIO_READ:
2139 case SVM_VMGEXIT_MMIO_WRITE:
2140 if (!ghcb_sw_scratch_is_valid(ghcb))
2141 goto vmgexit_err;
2142 break;
Tom Lendacky4444dfe2020-12-14 11:16:03 -05002143 case SVM_VMGEXIT_NMI_COMPLETE:
Tom Lendacky647daca2021-01-04 14:20:01 -06002144 case SVM_VMGEXIT_AP_HLT_LOOP:
Tom Lendacky8640ca52020-12-15 12:44:07 -05002145 case SVM_VMGEXIT_AP_JUMP_TABLE:
Tom Lendacky291bd202020-12-10 11:09:47 -06002146 case SVM_VMGEXIT_UNSUPPORTED_EVENT:
2147 break;
2148 default:
2149 goto vmgexit_err;
2150 }
2151
2152 return 0;
2153
2154vmgexit_err:
2155 vcpu = &svm->vcpu;
2156
2157 if (ghcb->ghcb_usage) {
2158 vcpu_unimpl(vcpu, "vmgexit: ghcb usage %#x is not valid\n",
2159 ghcb->ghcb_usage);
2160 } else {
2161 vcpu_unimpl(vcpu, "vmgexit: exit reason %#llx is not valid\n",
2162 exit_code);
2163 dump_ghcb(svm);
2164 }
2165
2166 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
2167 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_UNEXPECTED_EXIT_REASON;
2168 vcpu->run->internal.ndata = 2;
2169 vcpu->run->internal.data[0] = exit_code;
2170 vcpu->run->internal.data[1] = vcpu->arch.last_vmentry_cpu;
2171
2172 return -EINVAL;
2173}
2174
2175static void pre_sev_es_run(struct vcpu_svm *svm)
2176{
2177 if (!svm->ghcb)
2178 return;
2179
Tom Lendacky8f423a82020-12-10 11:09:53 -06002180 if (svm->ghcb_sa_free) {
2181 /*
2182 * The scratch area lives outside the GHCB, so there is a
2183 * buffer that, depending on the operation performed, may
2184 * need to be synced, then freed.
2185 */
2186 if (svm->ghcb_sa_sync) {
2187 kvm_write_guest(svm->vcpu.kvm,
2188 ghcb_get_sw_scratch(svm->ghcb),
2189 svm->ghcb_sa, svm->ghcb_sa_len);
2190 svm->ghcb_sa_sync = false;
2191 }
2192
2193 kfree(svm->ghcb_sa);
2194 svm->ghcb_sa = NULL;
2195 svm->ghcb_sa_free = false;
2196 }
2197
Tom Lendackyd523ab6b2020-12-10 11:09:48 -06002198 trace_kvm_vmgexit_exit(svm->vcpu.vcpu_id, svm->ghcb);
2199
Tom Lendacky291bd202020-12-10 11:09:47 -06002200 sev_es_sync_to_ghcb(svm);
2201
2202 kvm_vcpu_unmap(&svm->vcpu, &svm->ghcb_map, true);
2203 svm->ghcb = NULL;
2204}
2205
Joerg Roedeleaf78262020-03-24 10:41:54 +01002206void pre_sev_run(struct vcpu_svm *svm, int cpu)
2207{
2208 struct svm_cpu_data *sd = per_cpu(svm_data, cpu);
2209 int asid = sev_get_asid(svm->vcpu.kvm);
2210
Tom Lendacky291bd202020-12-10 11:09:47 -06002211 /* Perform any SEV-ES pre-run actions */
2212 pre_sev_es_run(svm);
2213
Joerg Roedeleaf78262020-03-24 10:41:54 +01002214 /* Assign the asid allocated with this SEV guest */
Paolo Bonzinidee734a2020-11-30 09:39:59 -05002215 svm->asid = asid;
Joerg Roedeleaf78262020-03-24 10:41:54 +01002216
2217 /*
2218 * Flush guest TLB:
2219 *
2220 * 1) when different VMCB for the same ASID is to be run on the same host CPU.
2221 * 2) or this VMCB was executed on different host CPU in previous VMRUNs.
2222 */
2223 if (sd->sev_vmcbs[asid] == svm->vmcb &&
Jim Mattson8a14fe42020-06-03 16:56:22 -07002224 svm->vcpu.arch.last_vmentry_cpu == cpu)
Joerg Roedeleaf78262020-03-24 10:41:54 +01002225 return;
2226
Joerg Roedeleaf78262020-03-24 10:41:54 +01002227 sd->sev_vmcbs[asid] = svm->vmcb;
2228 svm->vmcb->control.tlb_ctl = TLB_CONTROL_FLUSH_ASID;
Joerg Roedel06e78522020-06-25 10:03:23 +02002229 vmcb_mark_dirty(svm->vmcb, VMCB_ASID);
Joerg Roedeleaf78262020-03-24 10:41:54 +01002230}
Tom Lendacky291bd202020-12-10 11:09:47 -06002231
Tom Lendacky8f423a82020-12-10 11:09:53 -06002232#define GHCB_SCRATCH_AREA_LIMIT (16ULL * PAGE_SIZE)
2233static bool setup_vmgexit_scratch(struct vcpu_svm *svm, bool sync, u64 len)
2234{
2235 struct vmcb_control_area *control = &svm->vmcb->control;
2236 struct ghcb *ghcb = svm->ghcb;
2237 u64 ghcb_scratch_beg, ghcb_scratch_end;
2238 u64 scratch_gpa_beg, scratch_gpa_end;
2239 void *scratch_va;
2240
2241 scratch_gpa_beg = ghcb_get_sw_scratch(ghcb);
2242 if (!scratch_gpa_beg) {
2243 pr_err("vmgexit: scratch gpa not provided\n");
2244 return false;
2245 }
2246
2247 scratch_gpa_end = scratch_gpa_beg + len;
2248 if (scratch_gpa_end < scratch_gpa_beg) {
2249 pr_err("vmgexit: scratch length (%#llx) not valid for scratch address (%#llx)\n",
2250 len, scratch_gpa_beg);
2251 return false;
2252 }
2253
2254 if ((scratch_gpa_beg & PAGE_MASK) == control->ghcb_gpa) {
2255 /* Scratch area begins within GHCB */
2256 ghcb_scratch_beg = control->ghcb_gpa +
2257 offsetof(struct ghcb, shared_buffer);
2258 ghcb_scratch_end = control->ghcb_gpa +
2259 offsetof(struct ghcb, reserved_1);
2260
2261 /*
2262 * If the scratch area begins within the GHCB, it must be
2263 * completely contained in the GHCB shared buffer area.
2264 */
2265 if (scratch_gpa_beg < ghcb_scratch_beg ||
2266 scratch_gpa_end > ghcb_scratch_end) {
2267 pr_err("vmgexit: scratch area is outside of GHCB shared buffer area (%#llx - %#llx)\n",
2268 scratch_gpa_beg, scratch_gpa_end);
2269 return false;
2270 }
2271
2272 scratch_va = (void *)svm->ghcb;
2273 scratch_va += (scratch_gpa_beg - control->ghcb_gpa);
2274 } else {
2275 /*
2276 * The guest memory must be read into a kernel buffer, so
2277 * limit the size
2278 */
2279 if (len > GHCB_SCRATCH_AREA_LIMIT) {
2280 pr_err("vmgexit: scratch area exceeds KVM limits (%#llx requested, %#llx limit)\n",
2281 len, GHCB_SCRATCH_AREA_LIMIT);
2282 return false;
2283 }
Sean Christophersoneba04b22021-03-30 19:30:25 -07002284 scratch_va = kzalloc(len, GFP_KERNEL_ACCOUNT);
Tom Lendacky8f423a82020-12-10 11:09:53 -06002285 if (!scratch_va)
2286 return false;
2287
2288 if (kvm_read_guest(svm->vcpu.kvm, scratch_gpa_beg, scratch_va, len)) {
2289 /* Unable to copy scratch area from guest */
2290 pr_err("vmgexit: kvm_read_guest for scratch area failed\n");
2291
2292 kfree(scratch_va);
2293 return false;
2294 }
2295
2296 /*
2297 * The scratch area is outside the GHCB. The operation will
2298 * dictate whether the buffer needs to be synced before running
2299 * the vCPU next time (i.e. a read was requested so the data
2300 * must be written back to the guest memory).
2301 */
2302 svm->ghcb_sa_sync = sync;
2303 svm->ghcb_sa_free = true;
2304 }
2305
2306 svm->ghcb_sa = scratch_va;
2307 svm->ghcb_sa_len = len;
2308
2309 return true;
2310}
2311
Tom Lendackyd3694662020-12-10 11:09:50 -06002312static void set_ghcb_msr_bits(struct vcpu_svm *svm, u64 value, u64 mask,
2313 unsigned int pos)
2314{
2315 svm->vmcb->control.ghcb_gpa &= ~(mask << pos);
2316 svm->vmcb->control.ghcb_gpa |= (value & mask) << pos;
2317}
2318
2319static u64 get_ghcb_msr_bits(struct vcpu_svm *svm, u64 mask, unsigned int pos)
2320{
2321 return (svm->vmcb->control.ghcb_gpa >> pos) & mask;
2322}
2323
Tom Lendacky1edc1452020-12-10 11:09:49 -06002324static void set_ghcb_msr(struct vcpu_svm *svm, u64 value)
2325{
2326 svm->vmcb->control.ghcb_gpa = value;
2327}
2328
Tom Lendacky291bd202020-12-10 11:09:47 -06002329static int sev_handle_vmgexit_msr_protocol(struct vcpu_svm *svm)
2330{
Tom Lendacky1edc1452020-12-10 11:09:49 -06002331 struct vmcb_control_area *control = &svm->vmcb->control;
Tom Lendackyd3694662020-12-10 11:09:50 -06002332 struct kvm_vcpu *vcpu = &svm->vcpu;
Tom Lendacky1edc1452020-12-10 11:09:49 -06002333 u64 ghcb_info;
Tom Lendackyd3694662020-12-10 11:09:50 -06002334 int ret = 1;
Tom Lendacky1edc1452020-12-10 11:09:49 -06002335
2336 ghcb_info = control->ghcb_gpa & GHCB_MSR_INFO_MASK;
2337
Tom Lendacky59e38b52020-12-10 11:09:52 -06002338 trace_kvm_vmgexit_msr_protocol_enter(svm->vcpu.vcpu_id,
2339 control->ghcb_gpa);
2340
Tom Lendacky1edc1452020-12-10 11:09:49 -06002341 switch (ghcb_info) {
2342 case GHCB_MSR_SEV_INFO_REQ:
2343 set_ghcb_msr(svm, GHCB_MSR_SEV_INFO(GHCB_VERSION_MAX,
2344 GHCB_VERSION_MIN,
2345 sev_enc_bit));
2346 break;
Tom Lendackyd3694662020-12-10 11:09:50 -06002347 case GHCB_MSR_CPUID_REQ: {
2348 u64 cpuid_fn, cpuid_reg, cpuid_value;
2349
2350 cpuid_fn = get_ghcb_msr_bits(svm,
2351 GHCB_MSR_CPUID_FUNC_MASK,
2352 GHCB_MSR_CPUID_FUNC_POS);
2353
2354 /* Initialize the registers needed by the CPUID intercept */
2355 vcpu->arch.regs[VCPU_REGS_RAX] = cpuid_fn;
2356 vcpu->arch.regs[VCPU_REGS_RCX] = 0;
2357
Paolo Bonzini63129752021-03-02 14:40:39 -05002358 ret = svm_invoke_exit_handler(vcpu, SVM_EXIT_CPUID);
Tom Lendackyd3694662020-12-10 11:09:50 -06002359 if (!ret) {
2360 ret = -EINVAL;
2361 break;
2362 }
2363
2364 cpuid_reg = get_ghcb_msr_bits(svm,
2365 GHCB_MSR_CPUID_REG_MASK,
2366 GHCB_MSR_CPUID_REG_POS);
2367 if (cpuid_reg == 0)
2368 cpuid_value = vcpu->arch.regs[VCPU_REGS_RAX];
2369 else if (cpuid_reg == 1)
2370 cpuid_value = vcpu->arch.regs[VCPU_REGS_RBX];
2371 else if (cpuid_reg == 2)
2372 cpuid_value = vcpu->arch.regs[VCPU_REGS_RCX];
2373 else
2374 cpuid_value = vcpu->arch.regs[VCPU_REGS_RDX];
2375
2376 set_ghcb_msr_bits(svm, cpuid_value,
2377 GHCB_MSR_CPUID_VALUE_MASK,
2378 GHCB_MSR_CPUID_VALUE_POS);
2379
2380 set_ghcb_msr_bits(svm, GHCB_MSR_CPUID_RESP,
2381 GHCB_MSR_INFO_MASK,
2382 GHCB_MSR_INFO_POS);
2383 break;
2384 }
Tom Lendackye1d71112020-12-10 11:09:51 -06002385 case GHCB_MSR_TERM_REQ: {
2386 u64 reason_set, reason_code;
2387
2388 reason_set = get_ghcb_msr_bits(svm,
2389 GHCB_MSR_TERM_REASON_SET_MASK,
2390 GHCB_MSR_TERM_REASON_SET_POS);
2391 reason_code = get_ghcb_msr_bits(svm,
2392 GHCB_MSR_TERM_REASON_MASK,
2393 GHCB_MSR_TERM_REASON_POS);
2394 pr_info("SEV-ES guest requested termination: %#llx:%#llx\n",
2395 reason_set, reason_code);
2396 fallthrough;
2397 }
Tom Lendacky1edc1452020-12-10 11:09:49 -06002398 default:
Tom Lendackyd3694662020-12-10 11:09:50 -06002399 ret = -EINVAL;
Tom Lendacky1edc1452020-12-10 11:09:49 -06002400 }
2401
Tom Lendacky59e38b52020-12-10 11:09:52 -06002402 trace_kvm_vmgexit_msr_protocol_exit(svm->vcpu.vcpu_id,
2403 control->ghcb_gpa, ret);
2404
Tom Lendackyd3694662020-12-10 11:09:50 -06002405 return ret;
Tom Lendacky291bd202020-12-10 11:09:47 -06002406}
2407
Paolo Bonzini63129752021-03-02 14:40:39 -05002408int sev_handle_vmgexit(struct kvm_vcpu *vcpu)
Tom Lendacky291bd202020-12-10 11:09:47 -06002409{
Paolo Bonzini63129752021-03-02 14:40:39 -05002410 struct vcpu_svm *svm = to_svm(vcpu);
Tom Lendacky291bd202020-12-10 11:09:47 -06002411 struct vmcb_control_area *control = &svm->vmcb->control;
2412 u64 ghcb_gpa, exit_code;
2413 struct ghcb *ghcb;
2414 int ret;
2415
2416 /* Validate the GHCB */
2417 ghcb_gpa = control->ghcb_gpa;
2418 if (ghcb_gpa & GHCB_MSR_INFO_MASK)
2419 return sev_handle_vmgexit_msr_protocol(svm);
2420
2421 if (!ghcb_gpa) {
Paolo Bonzini63129752021-03-02 14:40:39 -05002422 vcpu_unimpl(vcpu, "vmgexit: GHCB gpa is not set\n");
Tom Lendacky291bd202020-12-10 11:09:47 -06002423 return -EINVAL;
2424 }
2425
Paolo Bonzini63129752021-03-02 14:40:39 -05002426 if (kvm_vcpu_map(vcpu, ghcb_gpa >> PAGE_SHIFT, &svm->ghcb_map)) {
Tom Lendacky291bd202020-12-10 11:09:47 -06002427 /* Unable to map GHCB from guest */
Paolo Bonzini63129752021-03-02 14:40:39 -05002428 vcpu_unimpl(vcpu, "vmgexit: error mapping GHCB [%#llx] from guest\n",
Tom Lendacky291bd202020-12-10 11:09:47 -06002429 ghcb_gpa);
2430 return -EINVAL;
2431 }
2432
2433 svm->ghcb = svm->ghcb_map.hva;
2434 ghcb = svm->ghcb_map.hva;
2435
Paolo Bonzini63129752021-03-02 14:40:39 -05002436 trace_kvm_vmgexit_enter(vcpu->vcpu_id, ghcb);
Tom Lendackyd523ab6b2020-12-10 11:09:48 -06002437
Tom Lendacky291bd202020-12-10 11:09:47 -06002438 exit_code = ghcb_get_sw_exit_code(ghcb);
2439
2440 ret = sev_es_validate_vmgexit(svm);
2441 if (ret)
2442 return ret;
2443
2444 sev_es_sync_from_ghcb(svm);
2445 ghcb_set_sw_exit_info_1(ghcb, 0);
2446 ghcb_set_sw_exit_info_2(ghcb, 0);
2447
2448 ret = -EINVAL;
2449 switch (exit_code) {
Tom Lendacky8f423a82020-12-10 11:09:53 -06002450 case SVM_VMGEXIT_MMIO_READ:
2451 if (!setup_vmgexit_scratch(svm, true, control->exit_info_2))
2452 break;
2453
Paolo Bonzini63129752021-03-02 14:40:39 -05002454 ret = kvm_sev_es_mmio_read(vcpu,
Tom Lendacky8f423a82020-12-10 11:09:53 -06002455 control->exit_info_1,
2456 control->exit_info_2,
2457 svm->ghcb_sa);
2458 break;
2459 case SVM_VMGEXIT_MMIO_WRITE:
2460 if (!setup_vmgexit_scratch(svm, false, control->exit_info_2))
2461 break;
2462
Paolo Bonzini63129752021-03-02 14:40:39 -05002463 ret = kvm_sev_es_mmio_write(vcpu,
Tom Lendacky8f423a82020-12-10 11:09:53 -06002464 control->exit_info_1,
2465 control->exit_info_2,
2466 svm->ghcb_sa);
2467 break;
Tom Lendacky4444dfe2020-12-14 11:16:03 -05002468 case SVM_VMGEXIT_NMI_COMPLETE:
Paolo Bonzini63129752021-03-02 14:40:39 -05002469 ret = svm_invoke_exit_handler(vcpu, SVM_EXIT_IRET);
Tom Lendacky4444dfe2020-12-14 11:16:03 -05002470 break;
Tom Lendacky647daca2021-01-04 14:20:01 -06002471 case SVM_VMGEXIT_AP_HLT_LOOP:
Paolo Bonzini63129752021-03-02 14:40:39 -05002472 ret = kvm_emulate_ap_reset_hold(vcpu);
Tom Lendacky647daca2021-01-04 14:20:01 -06002473 break;
Tom Lendacky8640ca52020-12-15 12:44:07 -05002474 case SVM_VMGEXIT_AP_JUMP_TABLE: {
Paolo Bonzini63129752021-03-02 14:40:39 -05002475 struct kvm_sev_info *sev = &to_kvm_svm(vcpu->kvm)->sev_info;
Tom Lendacky8640ca52020-12-15 12:44:07 -05002476
2477 switch (control->exit_info_1) {
2478 case 0:
2479 /* Set AP jump table address */
2480 sev->ap_jump_table = control->exit_info_2;
2481 break;
2482 case 1:
2483 /* Get AP jump table address */
2484 ghcb_set_sw_exit_info_2(ghcb, sev->ap_jump_table);
2485 break;
2486 default:
2487 pr_err("svm: vmgexit: unsupported AP jump table request - exit_info_1=%#llx\n",
2488 control->exit_info_1);
2489 ghcb_set_sw_exit_info_1(ghcb, 1);
2490 ghcb_set_sw_exit_info_2(ghcb,
2491 X86_TRAP_UD |
2492 SVM_EVTINJ_TYPE_EXEPT |
2493 SVM_EVTINJ_VALID);
2494 }
2495
2496 ret = 1;
2497 break;
2498 }
Tom Lendacky291bd202020-12-10 11:09:47 -06002499 case SVM_VMGEXIT_UNSUPPORTED_EVENT:
Paolo Bonzini63129752021-03-02 14:40:39 -05002500 vcpu_unimpl(vcpu,
Tom Lendacky291bd202020-12-10 11:09:47 -06002501 "vmgexit: unsupported event - exit_info_1=%#llx, exit_info_2=%#llx\n",
2502 control->exit_info_1, control->exit_info_2);
2503 break;
2504 default:
Paolo Bonzini63129752021-03-02 14:40:39 -05002505 ret = svm_invoke_exit_handler(vcpu, exit_code);
Tom Lendacky291bd202020-12-10 11:09:47 -06002506 }
2507
2508 return ret;
2509}
Tom Lendacky7ed9abf2020-12-10 11:09:54 -06002510
2511int sev_es_string_io(struct vcpu_svm *svm, int size, unsigned int port, int in)
2512{
2513 if (!setup_vmgexit_scratch(svm, in, svm->vmcb->control.exit_info_2))
2514 return -EINVAL;
2515
2516 return kvm_sev_es_string_io(&svm->vcpu, size, port,
2517 svm->ghcb_sa, svm->ghcb_sa_len, in);
2518}
Tom Lendacky376c6d22020-12-10 11:10:06 -06002519
2520void sev_es_init_vmcb(struct vcpu_svm *svm)
2521{
2522 struct kvm_vcpu *vcpu = &svm->vcpu;
2523
2524 svm->vmcb->control.nested_ctl |= SVM_NESTED_CTL_SEV_ES_ENABLE;
2525 svm->vmcb->control.virt_ext |= LBR_CTL_ENABLE_MASK;
2526
2527 /*
2528 * An SEV-ES guest requires a VMSA area that is a separate from the
2529 * VMCB page. Do not include the encryption mask on the VMSA physical
2530 * address since hardware will access it using the guest key.
2531 */
2532 svm->vmcb->control.vmsa_pa = __pa(svm->vmsa);
2533
2534 /* Can't intercept CR register access, HV can't modify CR registers */
2535 svm_clr_intercept(svm, INTERCEPT_CR0_READ);
2536 svm_clr_intercept(svm, INTERCEPT_CR4_READ);
2537 svm_clr_intercept(svm, INTERCEPT_CR8_READ);
2538 svm_clr_intercept(svm, INTERCEPT_CR0_WRITE);
2539 svm_clr_intercept(svm, INTERCEPT_CR4_WRITE);
2540 svm_clr_intercept(svm, INTERCEPT_CR8_WRITE);
2541
2542 svm_clr_intercept(svm, INTERCEPT_SELECTIVE_CR0);
2543
2544 /* Track EFER/CR register changes */
2545 svm_set_intercept(svm, TRAP_EFER_WRITE);
2546 svm_set_intercept(svm, TRAP_CR0_WRITE);
2547 svm_set_intercept(svm, TRAP_CR4_WRITE);
2548 svm_set_intercept(svm, TRAP_CR8_WRITE);
2549
2550 /* No support for enable_vmware_backdoor */
2551 clr_exception_intercept(svm, GP_VECTOR);
2552
2553 /* Can't intercept XSETBV, HV can't modify XCR0 directly */
2554 svm_clr_intercept(svm, INTERCEPT_XSETBV);
2555
2556 /* Clear intercepts on selected MSRs */
2557 set_msr_interception(vcpu, svm->msrpm, MSR_EFER, 1, 1);
2558 set_msr_interception(vcpu, svm->msrpm, MSR_IA32_CR_PAT, 1, 1);
2559 set_msr_interception(vcpu, svm->msrpm, MSR_IA32_LASTBRANCHFROMIP, 1, 1);
2560 set_msr_interception(vcpu, svm->msrpm, MSR_IA32_LASTBRANCHTOIP, 1, 1);
2561 set_msr_interception(vcpu, svm->msrpm, MSR_IA32_LASTINTFROMIP, 1, 1);
2562 set_msr_interception(vcpu, svm->msrpm, MSR_IA32_LASTINTTOIP, 1, 1);
2563}
2564
2565void sev_es_create_vcpu(struct vcpu_svm *svm)
2566{
2567 /*
2568 * Set the GHCB MSR value as per the GHCB specification when creating
2569 * a vCPU for an SEV-ES guest.
2570 */
2571 set_ghcb_msr(svm, GHCB_MSR_SEV_INFO(GHCB_VERSION_MAX,
2572 GHCB_VERSION_MIN,
2573 sev_enc_bit));
2574}
Tom Lendacky86137772020-12-10 11:10:07 -06002575
Michael Rotha7fc06d2021-02-02 13:01:26 -06002576void sev_es_prepare_guest_switch(struct vcpu_svm *svm, unsigned int cpu)
Tom Lendacky86137772020-12-10 11:10:07 -06002577{
2578 struct svm_cpu_data *sd = per_cpu(svm_data, cpu);
2579 struct vmcb_save_area *hostsa;
Tom Lendacky86137772020-12-10 11:10:07 -06002580
2581 /*
2582 * As an SEV-ES guest, hardware will restore the host state on VMEXIT,
2583 * of which one step is to perform a VMLOAD. Since hardware does not
2584 * perform a VMSAVE on VMRUN, the host savearea must be updated.
2585 */
Sean Christopherson35a78312020-12-30 16:27:00 -08002586 vmsave(__sme_page_pa(sd->save_area));
Tom Lendacky86137772020-12-10 11:10:07 -06002587
Tom Lendacky86137772020-12-10 11:10:07 -06002588 /* XCR0 is restored on VMEXIT, save the current host value */
2589 hostsa = (struct vmcb_save_area *)(page_address(sd->save_area) + 0x400);
2590 hostsa->xcr0 = xgetbv(XCR_XFEATURE_ENABLED_MASK);
2591
2592 /* PKRU is restored on VMEXIT, save the curent host value */
2593 hostsa->pkru = read_pkru();
2594
2595 /* MSR_IA32_XSS is restored on VMEXIT, save the currnet host value */
2596 hostsa->xss = host_xss;
2597}
2598
Tom Lendacky647daca2021-01-04 14:20:01 -06002599void sev_vcpu_deliver_sipi_vector(struct kvm_vcpu *vcpu, u8 vector)
2600{
2601 struct vcpu_svm *svm = to_svm(vcpu);
2602
2603 /* First SIPI: Use the values as initially set by the VMM */
2604 if (!svm->received_first_sipi) {
2605 svm->received_first_sipi = true;
2606 return;
2607 }
2608
2609 /*
2610 * Subsequent SIPI: Return from an AP Reset Hold VMGEXIT, where
2611 * the guest will set the CS and RIP. Set SW_EXIT_INFO_2 to a
2612 * non-zero value.
2613 */
Tom Lendackya3ba26e2021-04-09 09:38:42 -05002614 if (!svm->ghcb)
2615 return;
2616
Tom Lendacky647daca2021-01-04 14:20:01 -06002617 ghcb_set_sw_exit_info_2(svm->ghcb, 1);
2618}