blob: 1e8b26b93b4fb1db5d416ed108e4cb30f5bab84b [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
Dave Hansen784a46612021-06-23 14:02:05 +020022#include <asm/pkru.h>
Tom Lendacky8640ca52020-12-15 12:44:07 -050023#include <asm/trapnr.h>
24
Joerg Roedeleaf78262020-03-24 10:41:54 +010025#include "x86.h"
26#include "svm.h"
Sean Christopherson35a78312020-12-30 16:27:00 -080027#include "svm_ops.h"
Tom Lendacky291bd202020-12-10 11:09:47 -060028#include "cpuid.h"
Tom Lendackyd523ab6b2020-12-10 11:09:48 -060029#include "trace.h"
Joerg Roedeleaf78262020-03-24 10:41:54 +010030
Vipin Sharma7aef27f2021-03-29 21:42:06 -070031#ifndef CONFIG_KVM_AMD_SEV
32/*
33 * When this config is not defined, SEV feature is not supported and APIs in
34 * this file are not used but this file still gets compiled into the KVM AMD
35 * module.
36 *
37 * We will not have MISC_CG_RES_SEV and MISC_CG_RES_SEV_ES entries in the enum
38 * misc_res_type {} defined in linux/misc_cgroup.h.
39 *
40 * Below macros allow compilation to succeed.
41 */
42#define MISC_CG_RES_SEV MISC_CG_RES_TYPES
43#define MISC_CG_RES_SEV_ES MISC_CG_RES_TYPES
44#endif
45
Sean Christophersona479c332021-04-21 19:11:18 -070046#ifdef CONFIG_KVM_AMD_SEV
Sean Christophersone8126bd2021-04-21 19:11:14 -070047/* enable/disable SEV support */
Sean Christopherson6c2c7bf2021-04-21 19:11:19 -070048static bool sev_enabled = true;
Sean Christopherson8d364a02021-04-21 19:11:17 -070049module_param_named(sev, sev_enabled, bool, 0444);
Sean Christophersone8126bd2021-04-21 19:11:14 -070050
51/* enable/disable SEV-ES support */
Sean Christopherson6c2c7bf2021-04-21 19:11:19 -070052static bool sev_es_enabled = true;
Sean Christopherson8d364a02021-04-21 19:11:17 -070053module_param_named(sev_es, sev_es_enabled, bool, 0444);
Sean Christophersona479c332021-04-21 19:11:18 -070054#else
55#define sev_enabled false
56#define sev_es_enabled false
57#endif /* CONFIG_KVM_AMD_SEV */
Sean Christophersone8126bd2021-04-21 19:11:14 -070058
Tom Lendacky1edc1452020-12-10 11:09:49 -060059static u8 sev_enc_bit;
Joerg Roedeleaf78262020-03-24 10:41:54 +010060static DECLARE_RWSEM(sev_deactivate_lock);
61static DEFINE_MUTEX(sev_bitmap_lock);
62unsigned int max_sev_asid;
63static unsigned int min_sev_asid;
Brijesh Singhd3d1af82021-04-15 15:53:55 +000064static unsigned long sev_me_mask;
Mingwei Zhangbb2baeb2021-08-02 11:09:03 -070065static unsigned int nr_asids;
Joerg Roedeleaf78262020-03-24 10:41:54 +010066static unsigned long *sev_asid_bitmap;
67static unsigned long *sev_reclaim_asid_bitmap;
Joerg Roedeleaf78262020-03-24 10:41:54 +010068
69struct enc_region {
70 struct list_head list;
71 unsigned long npages;
72 struct page **pages;
73 unsigned long uaddr;
74 unsigned long size;
75};
76
Sean Christopherson469bb322021-04-21 19:11:25 -070077/* Called with the sev_bitmap_lock held, or on shutdown */
78static int sev_flush_asids(int min_asid, int max_asid)
Joerg Roedeleaf78262020-03-24 10:41:54 +010079{
Mingwei Zhangbb2baeb2021-08-02 11:09:03 -070080 int ret, asid, error = 0;
Sean Christopherson469bb322021-04-21 19:11:25 -070081
82 /* Check if there are any ASIDs to reclaim before performing a flush */
Mingwei Zhangbb2baeb2021-08-02 11:09:03 -070083 asid = find_next_bit(sev_reclaim_asid_bitmap, nr_asids, min_asid);
84 if (asid > max_asid)
Sean Christopherson469bb322021-04-21 19:11:25 -070085 return -EBUSY;
Joerg Roedeleaf78262020-03-24 10:41:54 +010086
87 /*
88 * DEACTIVATE will clear the WBINVD indicator causing DF_FLUSH to fail,
89 * so it must be guarded.
90 */
91 down_write(&sev_deactivate_lock);
92
93 wbinvd_on_all_cpus();
94 ret = sev_guest_df_flush(&error);
95
96 up_write(&sev_deactivate_lock);
97
98 if (ret)
99 pr_err("SEV: DF_FLUSH failed, ret=%d, error=%#x\n", ret, error);
100
101 return ret;
102}
103
Nathan Tempelman54526d12021-04-08 22:32:14 +0000104static inline bool is_mirroring_enc_context(struct kvm *kvm)
105{
106 return !!to_kvm_svm(kvm)->sev_info.enc_context_owner;
107}
108
Joerg Roedeleaf78262020-03-24 10:41:54 +0100109/* Must be called with the sev_bitmap_lock held */
Tom Lendacky80675b32020-12-10 11:10:05 -0600110static bool __sev_recycle_asids(int min_asid, int max_asid)
Joerg Roedeleaf78262020-03-24 10:41:54 +0100111{
Sean Christopherson469bb322021-04-21 19:11:25 -0700112 if (sev_flush_asids(min_asid, max_asid))
Joerg Roedeleaf78262020-03-24 10:41:54 +0100113 return false;
114
Tom Lendacky80675b32020-12-10 11:10:05 -0600115 /* The flush process will flush all reclaimable SEV and SEV-ES ASIDs */
Joerg Roedeleaf78262020-03-24 10:41:54 +0100116 bitmap_xor(sev_asid_bitmap, sev_asid_bitmap, sev_reclaim_asid_bitmap,
Mingwei Zhangbb2baeb2021-08-02 11:09:03 -0700117 nr_asids);
118 bitmap_zero(sev_reclaim_asid_bitmap, nr_asids);
Joerg Roedeleaf78262020-03-24 10:41:54 +0100119
120 return true;
121}
122
Tom Lendacky80675b32020-12-10 11:10:05 -0600123static int sev_asid_new(struct kvm_sev_info *sev)
Joerg Roedeleaf78262020-03-24 10:41:54 +0100124{
Mingwei Zhangbb2baeb2021-08-02 11:09:03 -0700125 int asid, min_asid, max_asid, ret;
Joerg Roedeleaf78262020-03-24 10:41:54 +0100126 bool retry = true;
Vipin Sharma7aef27f2021-03-29 21:42:06 -0700127 enum misc_res_type type;
128
129 type = sev->es_active ? MISC_CG_RES_SEV_ES : MISC_CG_RES_SEV;
130 WARN_ON(sev->misc_cg);
131 sev->misc_cg = get_current_misc_cg();
132 ret = misc_cg_try_charge(type, sev->misc_cg, 1);
133 if (ret) {
134 put_misc_cg(sev->misc_cg);
135 sev->misc_cg = NULL;
136 return ret;
137 }
Joerg Roedeleaf78262020-03-24 10:41:54 +0100138
139 mutex_lock(&sev_bitmap_lock);
140
141 /*
Tom Lendacky80675b32020-12-10 11:10:05 -0600142 * SEV-enabled guests must use asid from min_sev_asid to max_sev_asid.
143 * SEV-ES-enabled guest can use from 1 to min_sev_asid - 1.
Joerg Roedeleaf78262020-03-24 10:41:54 +0100144 */
Mingwei Zhangbb2baeb2021-08-02 11:09:03 -0700145 min_asid = sev->es_active ? 1 : min_sev_asid;
Tom Lendacky80675b32020-12-10 11:10:05 -0600146 max_asid = sev->es_active ? min_sev_asid - 1 : max_sev_asid;
Joerg Roedeleaf78262020-03-24 10:41:54 +0100147again:
Mingwei Zhangbb2baeb2021-08-02 11:09:03 -0700148 asid = find_next_zero_bit(sev_asid_bitmap, max_asid + 1, min_asid);
149 if (asid > max_asid) {
Tom Lendacky80675b32020-12-10 11:10:05 -0600150 if (retry && __sev_recycle_asids(min_asid, max_asid)) {
Joerg Roedeleaf78262020-03-24 10:41:54 +0100151 retry = false;
152 goto again;
153 }
154 mutex_unlock(&sev_bitmap_lock);
Vipin Sharma7aef27f2021-03-29 21:42:06 -0700155 ret = -EBUSY;
156 goto e_uncharge;
Joerg Roedeleaf78262020-03-24 10:41:54 +0100157 }
158
Mingwei Zhangbb2baeb2021-08-02 11:09:03 -0700159 __set_bit(asid, sev_asid_bitmap);
Joerg Roedeleaf78262020-03-24 10:41:54 +0100160
161 mutex_unlock(&sev_bitmap_lock);
162
Mingwei Zhangbb2baeb2021-08-02 11:09:03 -0700163 return asid;
Vipin Sharma7aef27f2021-03-29 21:42:06 -0700164e_uncharge:
165 misc_cg_uncharge(type, sev->misc_cg, 1);
166 put_misc_cg(sev->misc_cg);
167 sev->misc_cg = NULL;
168 return ret;
Joerg Roedeleaf78262020-03-24 10:41:54 +0100169}
170
171static int sev_get_asid(struct kvm *kvm)
172{
173 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
174
175 return sev->asid;
176}
177
Vipin Sharma7aef27f2021-03-29 21:42:06 -0700178static void sev_asid_free(struct kvm_sev_info *sev)
Joerg Roedeleaf78262020-03-24 10:41:54 +0100179{
180 struct svm_cpu_data *sd;
Mingwei Zhangbb2baeb2021-08-02 11:09:03 -0700181 int cpu;
Vipin Sharma7aef27f2021-03-29 21:42:06 -0700182 enum misc_res_type type;
Joerg Roedeleaf78262020-03-24 10:41:54 +0100183
184 mutex_lock(&sev_bitmap_lock);
185
Mingwei Zhangbb2baeb2021-08-02 11:09:03 -0700186 __set_bit(sev->asid, sev_reclaim_asid_bitmap);
Joerg Roedeleaf78262020-03-24 10:41:54 +0100187
188 for_each_possible_cpu(cpu) {
189 sd = per_cpu(svm_data, cpu);
Sean Christopherson179c6c22021-08-03 09:27:46 -0700190 sd->sev_vmcbs[sev->asid] = NULL;
Joerg Roedeleaf78262020-03-24 10:41:54 +0100191 }
192
193 mutex_unlock(&sev_bitmap_lock);
Vipin Sharma7aef27f2021-03-29 21:42:06 -0700194
195 type = sev->es_active ? MISC_CG_RES_SEV_ES : MISC_CG_RES_SEV;
196 misc_cg_uncharge(type, sev->misc_cg, 1);
197 put_misc_cg(sev->misc_cg);
198 sev->misc_cg = NULL;
Joerg Roedeleaf78262020-03-24 10:41:54 +0100199}
200
Alper Gun934002c2021-06-10 17:46:04 +0000201static void sev_decommission(unsigned int handle)
Joerg Roedeleaf78262020-03-24 10:41:54 +0100202{
Sean Christopherson238eca82021-04-06 15:49:52 -0700203 struct sev_data_decommission decommission;
Alper Gun934002c2021-06-10 17:46:04 +0000204
205 if (!handle)
206 return;
207
208 decommission.handle = handle;
209 sev_guest_decommission(&decommission, NULL);
210}
211
212static void sev_unbind_asid(struct kvm *kvm, unsigned int handle)
213{
Sean Christopherson238eca82021-04-06 15:49:52 -0700214 struct sev_data_deactivate deactivate;
Joerg Roedeleaf78262020-03-24 10:41:54 +0100215
216 if (!handle)
217 return;
218
Sean Christopherson238eca82021-04-06 15:49:52 -0700219 deactivate.handle = handle;
Joerg Roedeleaf78262020-03-24 10:41:54 +0100220
221 /* Guard DEACTIVATE against WBINVD/DF_FLUSH used in ASID recycling */
222 down_read(&sev_deactivate_lock);
Sean Christopherson238eca82021-04-06 15:49:52 -0700223 sev_guest_deactivate(&deactivate, NULL);
Joerg Roedeleaf78262020-03-24 10:41:54 +0100224 up_read(&sev_deactivate_lock);
225
Alper Gun934002c2021-06-10 17:46:04 +0000226 sev_decommission(handle);
Joerg Roedeleaf78262020-03-24 10:41:54 +0100227}
228
229static int sev_guest_init(struct kvm *kvm, struct kvm_sev_cmd *argp)
230{
231 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
Sean Christopherson9fa15212021-03-30 20:19:35 -0700232 bool es_active = argp->id == KVM_SEV_ES_INIT;
Joerg Roedeleaf78262020-03-24 10:41:54 +0100233 int asid, ret;
234
Sean Christopherson87279062021-03-30 20:19:36 -0700235 if (kvm->created_vcpus)
236 return -EINVAL;
237
Joerg Roedeleaf78262020-03-24 10:41:54 +0100238 ret = -EBUSY;
239 if (unlikely(sev->active))
240 return ret;
241
Paolo Bonzinifd49e8e2021-04-22 02:39:48 -0400242 sev->es_active = es_active;
Tom Lendacky80675b32020-12-10 11:10:05 -0600243 asid = sev_asid_new(sev);
Joerg Roedeleaf78262020-03-24 10:41:54 +0100244 if (asid < 0)
Paolo Bonzinifd49e8e2021-04-22 02:39:48 -0400245 goto e_no_asid;
Vipin Sharma7aef27f2021-03-29 21:42:06 -0700246 sev->asid = asid;
Joerg Roedeleaf78262020-03-24 10:41:54 +0100247
248 ret = sev_platform_init(&argp->error);
249 if (ret)
250 goto e_free;
251
252 sev->active = true;
253 sev->asid = asid;
254 INIT_LIST_HEAD(&sev->regions_list);
255
256 return 0;
257
258e_free:
Vipin Sharma7aef27f2021-03-29 21:42:06 -0700259 sev_asid_free(sev);
260 sev->asid = 0;
Paolo Bonzinifd49e8e2021-04-22 02:39:48 -0400261e_no_asid:
262 sev->es_active = false;
Joerg Roedeleaf78262020-03-24 10:41:54 +0100263 return ret;
264}
265
266static int sev_bind_asid(struct kvm *kvm, unsigned int handle, int *error)
267{
Sean Christopherson238eca82021-04-06 15:49:52 -0700268 struct sev_data_activate activate;
Joerg Roedeleaf78262020-03-24 10:41:54 +0100269 int asid = sev_get_asid(kvm);
270 int ret;
271
Joerg Roedeleaf78262020-03-24 10:41:54 +0100272 /* activate ASID on the given handle */
Sean Christopherson238eca82021-04-06 15:49:52 -0700273 activate.handle = handle;
274 activate.asid = asid;
275 ret = sev_guest_activate(&activate, error);
Joerg Roedeleaf78262020-03-24 10:41:54 +0100276
277 return ret;
278}
279
280static int __sev_issue_cmd(int fd, int id, void *data, int *error)
281{
282 struct fd f;
283 int ret;
284
285 f = fdget(fd);
286 if (!f.file)
287 return -EBADF;
288
289 ret = sev_issue_cmd_external_user(f.file, id, data, error);
290
291 fdput(f);
292 return ret;
293}
294
295static int sev_issue_cmd(struct kvm *kvm, int id, void *data, int *error)
296{
297 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
298
299 return __sev_issue_cmd(sev->fd, id, data, error);
300}
301
302static int sev_launch_start(struct kvm *kvm, struct kvm_sev_cmd *argp)
303{
304 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
Sean Christopherson238eca82021-04-06 15:49:52 -0700305 struct sev_data_launch_start start;
Joerg Roedeleaf78262020-03-24 10:41:54 +0100306 struct kvm_sev_launch_start params;
307 void *dh_blob, *session_blob;
308 int *error = &argp->error;
309 int ret;
310
311 if (!sev_guest(kvm))
312 return -ENOTTY;
313
314 if (copy_from_user(&params, (void __user *)(uintptr_t)argp->data, sizeof(params)))
315 return -EFAULT;
316
Sean Christopherson238eca82021-04-06 15:49:52 -0700317 memset(&start, 0, sizeof(start));
Joerg Roedeleaf78262020-03-24 10:41:54 +0100318
319 dh_blob = NULL;
320 if (params.dh_uaddr) {
321 dh_blob = psp_copy_user_blob(params.dh_uaddr, params.dh_len);
Sean Christopherson238eca82021-04-06 15:49:52 -0700322 if (IS_ERR(dh_blob))
323 return PTR_ERR(dh_blob);
Joerg Roedeleaf78262020-03-24 10:41:54 +0100324
Sean Christopherson238eca82021-04-06 15:49:52 -0700325 start.dh_cert_address = __sme_set(__pa(dh_blob));
326 start.dh_cert_len = params.dh_len;
Joerg Roedeleaf78262020-03-24 10:41:54 +0100327 }
328
329 session_blob = NULL;
330 if (params.session_uaddr) {
331 session_blob = psp_copy_user_blob(params.session_uaddr, params.session_len);
332 if (IS_ERR(session_blob)) {
333 ret = PTR_ERR(session_blob);
334 goto e_free_dh;
335 }
336
Sean Christopherson238eca82021-04-06 15:49:52 -0700337 start.session_address = __sme_set(__pa(session_blob));
338 start.session_len = params.session_len;
Joerg Roedeleaf78262020-03-24 10:41:54 +0100339 }
340
Sean Christopherson238eca82021-04-06 15:49:52 -0700341 start.handle = params.handle;
342 start.policy = params.policy;
Joerg Roedeleaf78262020-03-24 10:41:54 +0100343
344 /* create memory encryption context */
Sean Christopherson238eca82021-04-06 15:49:52 -0700345 ret = __sev_issue_cmd(argp->sev_fd, SEV_CMD_LAUNCH_START, &start, error);
Joerg Roedeleaf78262020-03-24 10:41:54 +0100346 if (ret)
347 goto e_free_session;
348
349 /* Bind ASID to this guest */
Sean Christopherson238eca82021-04-06 15:49:52 -0700350 ret = sev_bind_asid(kvm, start.handle, error);
Alper Gun934002c2021-06-10 17:46:04 +0000351 if (ret) {
352 sev_decommission(start.handle);
Joerg Roedeleaf78262020-03-24 10:41:54 +0100353 goto e_free_session;
Alper Gun934002c2021-06-10 17:46:04 +0000354 }
Joerg Roedeleaf78262020-03-24 10:41:54 +0100355
356 /* return handle to userspace */
Sean Christopherson238eca82021-04-06 15:49:52 -0700357 params.handle = start.handle;
Joerg Roedeleaf78262020-03-24 10:41:54 +0100358 if (copy_to_user((void __user *)(uintptr_t)argp->data, &params, sizeof(params))) {
Sean Christopherson238eca82021-04-06 15:49:52 -0700359 sev_unbind_asid(kvm, start.handle);
Joerg Roedeleaf78262020-03-24 10:41:54 +0100360 ret = -EFAULT;
361 goto e_free_session;
362 }
363
Sean Christopherson238eca82021-04-06 15:49:52 -0700364 sev->handle = start.handle;
Joerg Roedeleaf78262020-03-24 10:41:54 +0100365 sev->fd = argp->sev_fd;
366
367e_free_session:
368 kfree(session_blob);
369e_free_dh:
370 kfree(dh_blob);
Joerg Roedeleaf78262020-03-24 10:41:54 +0100371 return ret;
372}
373
374static struct page **sev_pin_memory(struct kvm *kvm, unsigned long uaddr,
375 unsigned long ulen, unsigned long *n,
376 int write)
377{
378 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
John Hubbard78824fa2020-05-25 23:22:06 -0700379 unsigned long npages, size;
380 int npinned;
Joerg Roedeleaf78262020-03-24 10:41:54 +0100381 unsigned long locked, lock_limit;
382 struct page **pages;
383 unsigned long first, last;
Dan Carpenterff2bd9f2020-07-14 17:23:51 +0300384 int ret;
Joerg Roedeleaf78262020-03-24 10:41:54 +0100385
Peter Gonda19a23da2021-01-27 08:15:24 -0800386 lockdep_assert_held(&kvm->lock);
387
Joerg Roedeleaf78262020-03-24 10:41:54 +0100388 if (ulen == 0 || uaddr + ulen < uaddr)
Paolo Bonzinia8d908b2020-06-23 05:12:24 -0400389 return ERR_PTR(-EINVAL);
Joerg Roedeleaf78262020-03-24 10:41:54 +0100390
391 /* Calculate number of pages. */
392 first = (uaddr & PAGE_MASK) >> PAGE_SHIFT;
393 last = ((uaddr + ulen - 1) & PAGE_MASK) >> PAGE_SHIFT;
394 npages = (last - first + 1);
395
396 locked = sev->pages_locked + npages;
397 lock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
398 if (locked > lock_limit && !capable(CAP_IPC_LOCK)) {
399 pr_err("SEV: %lu locked pages exceed the lock limit of %lu.\n", locked, lock_limit);
Paolo Bonzinia8d908b2020-06-23 05:12:24 -0400400 return ERR_PTR(-ENOMEM);
Joerg Roedeleaf78262020-03-24 10:41:54 +0100401 }
402
John Hubbard78824fa2020-05-25 23:22:06 -0700403 if (WARN_ON_ONCE(npages > INT_MAX))
Paolo Bonzinia8d908b2020-06-23 05:12:24 -0400404 return ERR_PTR(-EINVAL);
John Hubbard78824fa2020-05-25 23:22:06 -0700405
Joerg Roedeleaf78262020-03-24 10:41:54 +0100406 /* Avoid using vmalloc for smaller buffers. */
407 size = npages * sizeof(struct page *);
408 if (size > PAGE_SIZE)
Christoph Hellwig88dca4c2020-06-01 21:51:40 -0700409 pages = __vmalloc(size, GFP_KERNEL_ACCOUNT | __GFP_ZERO);
Joerg Roedeleaf78262020-03-24 10:41:54 +0100410 else
411 pages = kmalloc(size, GFP_KERNEL_ACCOUNT);
412
413 if (!pages)
Paolo Bonzinia8d908b2020-06-23 05:12:24 -0400414 return ERR_PTR(-ENOMEM);
Joerg Roedeleaf78262020-03-24 10:41:54 +0100415
416 /* Pin the user virtual address. */
John Hubbarddc42c8a2020-05-25 23:22:07 -0700417 npinned = pin_user_pages_fast(uaddr, npages, write ? FOLL_WRITE : 0, pages);
Joerg Roedeleaf78262020-03-24 10:41:54 +0100418 if (npinned != npages) {
419 pr_err("SEV: Failure locking %lu pages.\n", npages);
Dan Carpenterff2bd9f2020-07-14 17:23:51 +0300420 ret = -ENOMEM;
Joerg Roedeleaf78262020-03-24 10:41:54 +0100421 goto err;
422 }
423
424 *n = npages;
425 sev->pages_locked = locked;
426
427 return pages;
428
429err:
Dan Carpenterff2bd9f2020-07-14 17:23:51 +0300430 if (npinned > 0)
John Hubbarddc42c8a2020-05-25 23:22:07 -0700431 unpin_user_pages(pages, npinned);
Joerg Roedeleaf78262020-03-24 10:41:54 +0100432
433 kvfree(pages);
Dan Carpenterff2bd9f2020-07-14 17:23:51 +0300434 return ERR_PTR(ret);
Joerg Roedeleaf78262020-03-24 10:41:54 +0100435}
436
437static void sev_unpin_memory(struct kvm *kvm, struct page **pages,
438 unsigned long npages)
439{
440 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
441
John Hubbarddc42c8a2020-05-25 23:22:07 -0700442 unpin_user_pages(pages, npages);
Joerg Roedeleaf78262020-03-24 10:41:54 +0100443 kvfree(pages);
444 sev->pages_locked -= npages;
445}
446
447static void sev_clflush_pages(struct page *pages[], unsigned long npages)
448{
449 uint8_t *page_virtual;
450 unsigned long i;
451
Krish Sadhukhane1ebb2b2020-09-17 21:20:38 +0000452 if (this_cpu_has(X86_FEATURE_SME_COHERENT) || npages == 0 ||
453 pages == NULL)
Joerg Roedeleaf78262020-03-24 10:41:54 +0100454 return;
455
456 for (i = 0; i < npages; i++) {
457 page_virtual = kmap_atomic(pages[i]);
458 clflush_cache_range(page_virtual, PAGE_SIZE);
459 kunmap_atomic(page_virtual);
460 }
461}
462
463static unsigned long get_num_contig_pages(unsigned long idx,
464 struct page **inpages, unsigned long npages)
465{
466 unsigned long paddr, next_paddr;
467 unsigned long i = idx + 1, pages = 1;
468
469 /* find the number of contiguous pages starting from idx */
470 paddr = __sme_page_pa(inpages[idx]);
471 while (i < npages) {
472 next_paddr = __sme_page_pa(inpages[i++]);
473 if ((paddr + PAGE_SIZE) == next_paddr) {
474 pages++;
475 paddr = next_paddr;
476 continue;
477 }
478 break;
479 }
480
481 return pages;
482}
483
484static int sev_launch_update_data(struct kvm *kvm, struct kvm_sev_cmd *argp)
485{
486 unsigned long vaddr, vaddr_end, next_vaddr, npages, pages, size, i;
487 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
488 struct kvm_sev_launch_update_data params;
Sean Christopherson238eca82021-04-06 15:49:52 -0700489 struct sev_data_launch_update_data data;
Joerg Roedeleaf78262020-03-24 10:41:54 +0100490 struct page **inpages;
491 int ret;
492
493 if (!sev_guest(kvm))
494 return -ENOTTY;
495
496 if (copy_from_user(&params, (void __user *)(uintptr_t)argp->data, sizeof(params)))
497 return -EFAULT;
498
Joerg Roedeleaf78262020-03-24 10:41:54 +0100499 vaddr = params.uaddr;
500 size = params.len;
501 vaddr_end = vaddr + size;
502
503 /* Lock the user memory. */
504 inpages = sev_pin_memory(kvm, vaddr, size, &npages, 1);
Sean Christopherson238eca82021-04-06 15:49:52 -0700505 if (IS_ERR(inpages))
506 return PTR_ERR(inpages);
Joerg Roedeleaf78262020-03-24 10:41:54 +0100507
508 /*
Paolo Bonzini14e3dd82020-09-23 13:01:33 -0400509 * Flush (on non-coherent CPUs) before LAUNCH_UPDATE encrypts pages in
510 * place; the cache may contain the data that was written unencrypted.
Joerg Roedeleaf78262020-03-24 10:41:54 +0100511 */
512 sev_clflush_pages(inpages, npages);
513
Sean Christopherson238eca82021-04-06 15:49:52 -0700514 data.reserved = 0;
515 data.handle = sev->handle;
516
Joerg Roedeleaf78262020-03-24 10:41:54 +0100517 for (i = 0; vaddr < vaddr_end; vaddr = next_vaddr, i += pages) {
518 int offset, len;
519
520 /*
521 * If the user buffer is not page-aligned, calculate the offset
522 * within the page.
523 */
524 offset = vaddr & (PAGE_SIZE - 1);
525
526 /* Calculate the number of pages that can be encrypted in one go. */
527 pages = get_num_contig_pages(i, inpages, npages);
528
529 len = min_t(size_t, ((pages * PAGE_SIZE) - offset), size);
530
Sean Christopherson238eca82021-04-06 15:49:52 -0700531 data.len = len;
532 data.address = __sme_page_pa(inpages[i]) + offset;
533 ret = sev_issue_cmd(kvm, SEV_CMD_LAUNCH_UPDATE_DATA, &data, &argp->error);
Joerg Roedeleaf78262020-03-24 10:41:54 +0100534 if (ret)
535 goto e_unpin;
536
537 size -= len;
538 next_vaddr = vaddr + len;
539 }
540
541e_unpin:
542 /* content of memory is updated, mark pages dirty */
543 for (i = 0; i < npages; i++) {
544 set_page_dirty_lock(inpages[i]);
545 mark_page_accessed(inpages[i]);
546 }
547 /* unlock the user pages */
548 sev_unpin_memory(kvm, inpages, npages);
Joerg Roedeleaf78262020-03-24 10:41:54 +0100549 return ret;
550}
551
Tom Lendackyad731092020-12-10 11:10:09 -0600552static int sev_es_sync_vmsa(struct vcpu_svm *svm)
553{
554 struct vmcb_save_area *save = &svm->vmcb->save;
555
556 /* Check some debug related fields before encrypting the VMSA */
557 if (svm->vcpu.guest_debug || (save->dr7 & ~DR7_FIXED_1))
558 return -EINVAL;
559
560 /* Sync registgers */
561 save->rax = svm->vcpu.arch.regs[VCPU_REGS_RAX];
562 save->rbx = svm->vcpu.arch.regs[VCPU_REGS_RBX];
563 save->rcx = svm->vcpu.arch.regs[VCPU_REGS_RCX];
564 save->rdx = svm->vcpu.arch.regs[VCPU_REGS_RDX];
565 save->rsp = svm->vcpu.arch.regs[VCPU_REGS_RSP];
566 save->rbp = svm->vcpu.arch.regs[VCPU_REGS_RBP];
567 save->rsi = svm->vcpu.arch.regs[VCPU_REGS_RSI];
568 save->rdi = svm->vcpu.arch.regs[VCPU_REGS_RDI];
Paolo Bonzinid45f89f2020-12-16 13:08:21 -0500569#ifdef CONFIG_X86_64
Tom Lendackyad731092020-12-10 11:10:09 -0600570 save->r8 = svm->vcpu.arch.regs[VCPU_REGS_R8];
571 save->r9 = svm->vcpu.arch.regs[VCPU_REGS_R9];
572 save->r10 = svm->vcpu.arch.regs[VCPU_REGS_R10];
573 save->r11 = svm->vcpu.arch.regs[VCPU_REGS_R11];
574 save->r12 = svm->vcpu.arch.regs[VCPU_REGS_R12];
575 save->r13 = svm->vcpu.arch.regs[VCPU_REGS_R13];
576 save->r14 = svm->vcpu.arch.regs[VCPU_REGS_R14];
577 save->r15 = svm->vcpu.arch.regs[VCPU_REGS_R15];
Paolo Bonzinid45f89f2020-12-16 13:08:21 -0500578#endif
Tom Lendackyad731092020-12-10 11:10:09 -0600579 save->rip = svm->vcpu.arch.regs[VCPU_REGS_RIP];
580
581 /* Sync some non-GPR registers before encrypting */
582 save->xcr0 = svm->vcpu.arch.xcr0;
583 save->pkru = svm->vcpu.arch.pkru;
584 save->xss = svm->vcpu.arch.ia32_xss;
Sean Christophersond0f9f822021-07-13 09:33:10 -0700585 save->dr6 = svm->vcpu.arch.dr6;
Tom Lendackyad731092020-12-10 11:10:09 -0600586
587 /*
588 * SEV-ES will use a VMSA that is pointed to by the VMCB, not
589 * the traditional VMSA that is part of the VMCB. Copy the
590 * traditional VMSA as it has been built so far (in prep
591 * for LAUNCH_UPDATE_VMSA) to be the initial SEV-ES state.
592 */
593 memcpy(svm->vmsa, save, sizeof(*save));
594
595 return 0;
596}
597
Peter Gondabb18a672021-09-15 10:17:55 -0700598static int __sev_launch_update_vmsa(struct kvm *kvm, struct kvm_vcpu *vcpu,
599 int *error)
600{
601 struct sev_data_launch_update_vmsa vmsa;
602 struct vcpu_svm *svm = to_svm(vcpu);
603 int ret;
604
605 /* Perform some pre-encryption checks against the VMSA */
606 ret = sev_es_sync_vmsa(svm);
607 if (ret)
608 return ret;
609
610 /*
611 * The LAUNCH_UPDATE_VMSA command will perform in-place encryption of
612 * the VMSA memory content (i.e it will write the same memory region
613 * with the guest's key), so invalidate it first.
614 */
615 clflush_cache_range(svm->vmsa, PAGE_SIZE);
616
617 vmsa.reserved = 0;
618 vmsa.handle = to_kvm_svm(kvm)->sev_info.handle;
619 vmsa.address = __sme_pa(svm->vmsa);
620 vmsa.len = PAGE_SIZE;
621 return sev_issue_cmd(kvm, SEV_CMD_LAUNCH_UPDATE_VMSA, &vmsa, error);
622}
623
Tom Lendackyad731092020-12-10 11:10:09 -0600624static int sev_launch_update_vmsa(struct kvm *kvm, struct kvm_sev_cmd *argp)
625{
Sean Christophersonc36b16d2021-03-30 20:19:34 -0700626 struct kvm_vcpu *vcpu;
Tom Lendackyad731092020-12-10 11:10:09 -0600627 int i, ret;
628
629 if (!sev_es_guest(kvm))
630 return -ENOTTY;
631
Sean Christophersonc36b16d2021-03-30 20:19:34 -0700632 kvm_for_each_vcpu(i, vcpu, kvm) {
Peter Gondabb18a672021-09-15 10:17:55 -0700633 ret = mutex_lock_killable(&vcpu->mutex);
Tom Lendackyad731092020-12-10 11:10:09 -0600634 if (ret)
Sean Christopherson238eca82021-04-06 15:49:52 -0700635 return ret;
Tom Lendackyad731092020-12-10 11:10:09 -0600636
Peter Gondabb18a672021-09-15 10:17:55 -0700637 ret = __sev_launch_update_vmsa(kvm, vcpu, &argp->error);
Tom Lendackyad731092020-12-10 11:10:09 -0600638
Peter Gondabb18a672021-09-15 10:17:55 -0700639 mutex_unlock(&vcpu->mutex);
Tom Lendackyad731092020-12-10 11:10:09 -0600640 if (ret)
Sean Christopherson238eca82021-04-06 15:49:52 -0700641 return ret;
Tom Lendackyad731092020-12-10 11:10:09 -0600642 }
643
Sean Christopherson238eca82021-04-06 15:49:52 -0700644 return 0;
Tom Lendackyad731092020-12-10 11:10:09 -0600645}
646
Joerg Roedeleaf78262020-03-24 10:41:54 +0100647static int sev_launch_measure(struct kvm *kvm, struct kvm_sev_cmd *argp)
648{
649 void __user *measure = (void __user *)(uintptr_t)argp->data;
650 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
Sean Christopherson238eca82021-04-06 15:49:52 -0700651 struct sev_data_launch_measure data;
Joerg Roedeleaf78262020-03-24 10:41:54 +0100652 struct kvm_sev_launch_measure params;
653 void __user *p = NULL;
654 void *blob = NULL;
655 int ret;
656
657 if (!sev_guest(kvm))
658 return -ENOTTY;
659
660 if (copy_from_user(&params, measure, sizeof(params)))
661 return -EFAULT;
662
Sean Christopherson238eca82021-04-06 15:49:52 -0700663 memset(&data, 0, sizeof(data));
Joerg Roedeleaf78262020-03-24 10:41:54 +0100664
665 /* User wants to query the blob length */
666 if (!params.len)
667 goto cmd;
668
669 p = (void __user *)(uintptr_t)params.uaddr;
670 if (p) {
Sean Christopherson238eca82021-04-06 15:49:52 -0700671 if (params.len > SEV_FW_BLOB_MAX_SIZE)
672 return -EINVAL;
Joerg Roedeleaf78262020-03-24 10:41:54 +0100673
Sean Christophersoneba04b22021-03-30 19:30:25 -0700674 blob = kmalloc(params.len, GFP_KERNEL_ACCOUNT);
Joerg Roedeleaf78262020-03-24 10:41:54 +0100675 if (!blob)
Sean Christopherson238eca82021-04-06 15:49:52 -0700676 return -ENOMEM;
Joerg Roedeleaf78262020-03-24 10:41:54 +0100677
Sean Christopherson238eca82021-04-06 15:49:52 -0700678 data.address = __psp_pa(blob);
679 data.len = params.len;
Joerg Roedeleaf78262020-03-24 10:41:54 +0100680 }
681
682cmd:
Sean Christopherson238eca82021-04-06 15:49:52 -0700683 data.handle = sev->handle;
684 ret = sev_issue_cmd(kvm, SEV_CMD_LAUNCH_MEASURE, &data, &argp->error);
Joerg Roedeleaf78262020-03-24 10:41:54 +0100685
686 /*
687 * If we query the session length, FW responded with expected data.
688 */
689 if (!params.len)
690 goto done;
691
692 if (ret)
693 goto e_free_blob;
694
695 if (blob) {
696 if (copy_to_user(p, blob, params.len))
697 ret = -EFAULT;
698 }
699
700done:
Sean Christopherson238eca82021-04-06 15:49:52 -0700701 params.len = data.len;
Joerg Roedeleaf78262020-03-24 10:41:54 +0100702 if (copy_to_user(measure, &params, sizeof(params)))
703 ret = -EFAULT;
704e_free_blob:
705 kfree(blob);
Joerg Roedeleaf78262020-03-24 10:41:54 +0100706 return ret;
707}
708
709static int sev_launch_finish(struct kvm *kvm, struct kvm_sev_cmd *argp)
710{
711 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
Sean Christopherson238eca82021-04-06 15:49:52 -0700712 struct sev_data_launch_finish data;
Joerg Roedeleaf78262020-03-24 10:41:54 +0100713
714 if (!sev_guest(kvm))
715 return -ENOTTY;
716
Sean Christopherson238eca82021-04-06 15:49:52 -0700717 data.handle = sev->handle;
718 return sev_issue_cmd(kvm, SEV_CMD_LAUNCH_FINISH, &data, &argp->error);
Joerg Roedeleaf78262020-03-24 10:41:54 +0100719}
720
721static int sev_guest_status(struct kvm *kvm, struct kvm_sev_cmd *argp)
722{
723 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
724 struct kvm_sev_guest_status params;
Sean Christopherson238eca82021-04-06 15:49:52 -0700725 struct sev_data_guest_status data;
Joerg Roedeleaf78262020-03-24 10:41:54 +0100726 int ret;
727
728 if (!sev_guest(kvm))
729 return -ENOTTY;
730
Sean Christopherson238eca82021-04-06 15:49:52 -0700731 memset(&data, 0, sizeof(data));
Joerg Roedeleaf78262020-03-24 10:41:54 +0100732
Sean Christopherson238eca82021-04-06 15:49:52 -0700733 data.handle = sev->handle;
734 ret = sev_issue_cmd(kvm, SEV_CMD_GUEST_STATUS, &data, &argp->error);
Joerg Roedeleaf78262020-03-24 10:41:54 +0100735 if (ret)
Sean Christopherson238eca82021-04-06 15:49:52 -0700736 return ret;
Joerg Roedeleaf78262020-03-24 10:41:54 +0100737
Sean Christopherson238eca82021-04-06 15:49:52 -0700738 params.policy = data.policy;
739 params.state = data.state;
740 params.handle = data.handle;
Joerg Roedeleaf78262020-03-24 10:41:54 +0100741
742 if (copy_to_user((void __user *)(uintptr_t)argp->data, &params, sizeof(params)))
743 ret = -EFAULT;
Sean Christopherson238eca82021-04-06 15:49:52 -0700744
Joerg Roedeleaf78262020-03-24 10:41:54 +0100745 return ret;
746}
747
748static int __sev_issue_dbg_cmd(struct kvm *kvm, unsigned long src,
749 unsigned long dst, int size,
750 int *error, bool enc)
751{
752 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
Sean Christopherson238eca82021-04-06 15:49:52 -0700753 struct sev_data_dbg data;
Joerg Roedeleaf78262020-03-24 10:41:54 +0100754
Sean Christopherson238eca82021-04-06 15:49:52 -0700755 data.reserved = 0;
756 data.handle = sev->handle;
757 data.dst_addr = dst;
758 data.src_addr = src;
759 data.len = size;
Joerg Roedeleaf78262020-03-24 10:41:54 +0100760
Sean Christopherson238eca82021-04-06 15:49:52 -0700761 return sev_issue_cmd(kvm,
762 enc ? SEV_CMD_DBG_ENCRYPT : SEV_CMD_DBG_DECRYPT,
763 &data, error);
Joerg Roedeleaf78262020-03-24 10:41:54 +0100764}
765
766static int __sev_dbg_decrypt(struct kvm *kvm, unsigned long src_paddr,
767 unsigned long dst_paddr, int sz, int *err)
768{
769 int offset;
770
771 /*
772 * Its safe to read more than we are asked, caller should ensure that
773 * destination has enough space.
774 */
Joerg Roedeleaf78262020-03-24 10:41:54 +0100775 offset = src_paddr & 15;
Ashish Kalra854c57f2020-11-10 22:42:05 +0000776 src_paddr = round_down(src_paddr, 16);
Joerg Roedeleaf78262020-03-24 10:41:54 +0100777 sz = round_up(sz + offset, 16);
778
779 return __sev_issue_dbg_cmd(kvm, src_paddr, dst_paddr, sz, err, false);
780}
781
782static int __sev_dbg_decrypt_user(struct kvm *kvm, unsigned long paddr,
Sean Christopherson368340a2021-05-06 16:15:42 -0700783 void __user *dst_uaddr,
Joerg Roedeleaf78262020-03-24 10:41:54 +0100784 unsigned long dst_paddr,
785 int size, int *err)
786{
787 struct page *tpage = NULL;
788 int ret, offset;
789
790 /* if inputs are not 16-byte then use intermediate buffer */
791 if (!IS_ALIGNED(dst_paddr, 16) ||
792 !IS_ALIGNED(paddr, 16) ||
793 !IS_ALIGNED(size, 16)) {
794 tpage = (void *)alloc_page(GFP_KERNEL);
795 if (!tpage)
796 return -ENOMEM;
797
798 dst_paddr = __sme_page_pa(tpage);
799 }
800
801 ret = __sev_dbg_decrypt(kvm, paddr, dst_paddr, size, err);
802 if (ret)
803 goto e_free;
804
805 if (tpage) {
806 offset = paddr & 15;
Sean Christopherson368340a2021-05-06 16:15:42 -0700807 if (copy_to_user(dst_uaddr, page_address(tpage) + offset, size))
Joerg Roedeleaf78262020-03-24 10:41:54 +0100808 ret = -EFAULT;
809 }
810
811e_free:
812 if (tpage)
813 __free_page(tpage);
814
815 return ret;
816}
817
818static int __sev_dbg_encrypt_user(struct kvm *kvm, unsigned long paddr,
Sean Christopherson368340a2021-05-06 16:15:42 -0700819 void __user *vaddr,
Joerg Roedeleaf78262020-03-24 10:41:54 +0100820 unsigned long dst_paddr,
Sean Christopherson368340a2021-05-06 16:15:42 -0700821 void __user *dst_vaddr,
Joerg Roedeleaf78262020-03-24 10:41:54 +0100822 int size, int *error)
823{
824 struct page *src_tpage = NULL;
825 struct page *dst_tpage = NULL;
826 int ret, len = size;
827
828 /* If source buffer is not aligned then use an intermediate buffer */
Sean Christopherson368340a2021-05-06 16:15:42 -0700829 if (!IS_ALIGNED((unsigned long)vaddr, 16)) {
Joerg Roedeleaf78262020-03-24 10:41:54 +0100830 src_tpage = alloc_page(GFP_KERNEL);
831 if (!src_tpage)
832 return -ENOMEM;
833
Sean Christopherson368340a2021-05-06 16:15:42 -0700834 if (copy_from_user(page_address(src_tpage), vaddr, size)) {
Joerg Roedeleaf78262020-03-24 10:41:54 +0100835 __free_page(src_tpage);
836 return -EFAULT;
837 }
838
839 paddr = __sme_page_pa(src_tpage);
840 }
841
842 /*
843 * If destination buffer or length is not aligned then do read-modify-write:
844 * - decrypt destination in an intermediate buffer
845 * - copy the source buffer in an intermediate buffer
846 * - use the intermediate buffer as source buffer
847 */
Sean Christopherson368340a2021-05-06 16:15:42 -0700848 if (!IS_ALIGNED((unsigned long)dst_vaddr, 16) || !IS_ALIGNED(size, 16)) {
Joerg Roedeleaf78262020-03-24 10:41:54 +0100849 int dst_offset;
850
851 dst_tpage = alloc_page(GFP_KERNEL);
852 if (!dst_tpage) {
853 ret = -ENOMEM;
854 goto e_free;
855 }
856
857 ret = __sev_dbg_decrypt(kvm, dst_paddr,
858 __sme_page_pa(dst_tpage), size, error);
859 if (ret)
860 goto e_free;
861
862 /*
863 * If source is kernel buffer then use memcpy() otherwise
864 * copy_from_user().
865 */
866 dst_offset = dst_paddr & 15;
867
868 if (src_tpage)
869 memcpy(page_address(dst_tpage) + dst_offset,
870 page_address(src_tpage), size);
871 else {
872 if (copy_from_user(page_address(dst_tpage) + dst_offset,
Sean Christopherson368340a2021-05-06 16:15:42 -0700873 vaddr, size)) {
Joerg Roedeleaf78262020-03-24 10:41:54 +0100874 ret = -EFAULT;
875 goto e_free;
876 }
877 }
878
879 paddr = __sme_page_pa(dst_tpage);
880 dst_paddr = round_down(dst_paddr, 16);
881 len = round_up(size, 16);
882 }
883
884 ret = __sev_issue_dbg_cmd(kvm, paddr, dst_paddr, len, error, true);
885
886e_free:
887 if (src_tpage)
888 __free_page(src_tpage);
889 if (dst_tpage)
890 __free_page(dst_tpage);
891 return ret;
892}
893
894static int sev_dbg_crypt(struct kvm *kvm, struct kvm_sev_cmd *argp, bool dec)
895{
896 unsigned long vaddr, vaddr_end, next_vaddr;
897 unsigned long dst_vaddr;
898 struct page **src_p, **dst_p;
899 struct kvm_sev_dbg debug;
900 unsigned long n;
901 unsigned int size;
902 int ret;
903
904 if (!sev_guest(kvm))
905 return -ENOTTY;
906
907 if (copy_from_user(&debug, (void __user *)(uintptr_t)argp->data, sizeof(debug)))
908 return -EFAULT;
909
910 if (!debug.len || debug.src_uaddr + debug.len < debug.src_uaddr)
911 return -EINVAL;
912 if (!debug.dst_uaddr)
913 return -EINVAL;
914
915 vaddr = debug.src_uaddr;
916 size = debug.len;
917 vaddr_end = vaddr + size;
918 dst_vaddr = debug.dst_uaddr;
919
920 for (; vaddr < vaddr_end; vaddr = next_vaddr) {
921 int len, s_off, d_off;
922
923 /* lock userspace source and destination page */
924 src_p = sev_pin_memory(kvm, vaddr & PAGE_MASK, PAGE_SIZE, &n, 0);
Dan Carpenterff2bd9f2020-07-14 17:23:51 +0300925 if (IS_ERR(src_p))
926 return PTR_ERR(src_p);
Joerg Roedeleaf78262020-03-24 10:41:54 +0100927
928 dst_p = sev_pin_memory(kvm, dst_vaddr & PAGE_MASK, PAGE_SIZE, &n, 1);
Dan Carpenterff2bd9f2020-07-14 17:23:51 +0300929 if (IS_ERR(dst_p)) {
Joerg Roedeleaf78262020-03-24 10:41:54 +0100930 sev_unpin_memory(kvm, src_p, n);
Dan Carpenterff2bd9f2020-07-14 17:23:51 +0300931 return PTR_ERR(dst_p);
Joerg Roedeleaf78262020-03-24 10:41:54 +0100932 }
933
934 /*
Paolo Bonzini14e3dd82020-09-23 13:01:33 -0400935 * Flush (on non-coherent CPUs) before DBG_{DE,EN}CRYPT read or modify
936 * the pages; flush the destination too so that future accesses do not
937 * see stale data.
Joerg Roedeleaf78262020-03-24 10:41:54 +0100938 */
939 sev_clflush_pages(src_p, 1);
940 sev_clflush_pages(dst_p, 1);
941
942 /*
943 * Since user buffer may not be page aligned, calculate the
944 * offset within the page.
945 */
946 s_off = vaddr & ~PAGE_MASK;
947 d_off = dst_vaddr & ~PAGE_MASK;
948 len = min_t(size_t, (PAGE_SIZE - s_off), size);
949
950 if (dec)
951 ret = __sev_dbg_decrypt_user(kvm,
952 __sme_page_pa(src_p[0]) + s_off,
Sean Christopherson368340a2021-05-06 16:15:42 -0700953 (void __user *)dst_vaddr,
Joerg Roedeleaf78262020-03-24 10:41:54 +0100954 __sme_page_pa(dst_p[0]) + d_off,
955 len, &argp->error);
956 else
957 ret = __sev_dbg_encrypt_user(kvm,
958 __sme_page_pa(src_p[0]) + s_off,
Sean Christopherson368340a2021-05-06 16:15:42 -0700959 (void __user *)vaddr,
Joerg Roedeleaf78262020-03-24 10:41:54 +0100960 __sme_page_pa(dst_p[0]) + d_off,
Sean Christopherson368340a2021-05-06 16:15:42 -0700961 (void __user *)dst_vaddr,
Joerg Roedeleaf78262020-03-24 10:41:54 +0100962 len, &argp->error);
963
964 sev_unpin_memory(kvm, src_p, n);
965 sev_unpin_memory(kvm, dst_p, n);
966
967 if (ret)
968 goto err;
969
970 next_vaddr = vaddr + len;
971 dst_vaddr = dst_vaddr + len;
972 size -= len;
973 }
974err:
975 return ret;
976}
977
978static int sev_launch_secret(struct kvm *kvm, struct kvm_sev_cmd *argp)
979{
980 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
Sean Christopherson238eca82021-04-06 15:49:52 -0700981 struct sev_data_launch_secret data;
Joerg Roedeleaf78262020-03-24 10:41:54 +0100982 struct kvm_sev_launch_secret params;
983 struct page **pages;
984 void *blob, *hdr;
Cfir Cohen50085be2020-08-07 17:37:46 -0700985 unsigned long n, i;
Joerg Roedeleaf78262020-03-24 10:41:54 +0100986 int ret, offset;
987
988 if (!sev_guest(kvm))
989 return -ENOTTY;
990
991 if (copy_from_user(&params, (void __user *)(uintptr_t)argp->data, sizeof(params)))
992 return -EFAULT;
993
994 pages = sev_pin_memory(kvm, params.guest_uaddr, params.guest_len, &n, 1);
Paolo Bonzinia8d908b2020-06-23 05:12:24 -0400995 if (IS_ERR(pages))
996 return PTR_ERR(pages);
Joerg Roedeleaf78262020-03-24 10:41:54 +0100997
998 /*
Paolo Bonzini14e3dd82020-09-23 13:01:33 -0400999 * Flush (on non-coherent CPUs) before LAUNCH_SECRET encrypts pages in
1000 * place; the cache may contain the data that was written unencrypted.
Cfir Cohen50085be2020-08-07 17:37:46 -07001001 */
1002 sev_clflush_pages(pages, n);
1003
1004 /*
Joerg Roedeleaf78262020-03-24 10:41:54 +01001005 * The secret must be copied into contiguous memory region, lets verify
1006 * that userspace memory pages are contiguous before we issue command.
1007 */
1008 if (get_num_contig_pages(0, pages, n) != n) {
1009 ret = -EINVAL;
1010 goto e_unpin_memory;
1011 }
1012
Sean Christopherson238eca82021-04-06 15:49:52 -07001013 memset(&data, 0, sizeof(data));
Joerg Roedeleaf78262020-03-24 10:41:54 +01001014
1015 offset = params.guest_uaddr & (PAGE_SIZE - 1);
Sean Christopherson238eca82021-04-06 15:49:52 -07001016 data.guest_address = __sme_page_pa(pages[0]) + offset;
1017 data.guest_len = params.guest_len;
Joerg Roedeleaf78262020-03-24 10:41:54 +01001018
1019 blob = psp_copy_user_blob(params.trans_uaddr, params.trans_len);
1020 if (IS_ERR(blob)) {
1021 ret = PTR_ERR(blob);
Sean Christopherson238eca82021-04-06 15:49:52 -07001022 goto e_unpin_memory;
Joerg Roedeleaf78262020-03-24 10:41:54 +01001023 }
1024
Sean Christopherson238eca82021-04-06 15:49:52 -07001025 data.trans_address = __psp_pa(blob);
1026 data.trans_len = params.trans_len;
Joerg Roedeleaf78262020-03-24 10:41:54 +01001027
1028 hdr = psp_copy_user_blob(params.hdr_uaddr, params.hdr_len);
1029 if (IS_ERR(hdr)) {
1030 ret = PTR_ERR(hdr);
1031 goto e_free_blob;
1032 }
Sean Christopherson238eca82021-04-06 15:49:52 -07001033 data.hdr_address = __psp_pa(hdr);
1034 data.hdr_len = params.hdr_len;
Joerg Roedeleaf78262020-03-24 10:41:54 +01001035
Sean Christopherson238eca82021-04-06 15:49:52 -07001036 data.handle = sev->handle;
1037 ret = sev_issue_cmd(kvm, SEV_CMD_LAUNCH_UPDATE_SECRET, &data, &argp->error);
Joerg Roedeleaf78262020-03-24 10:41:54 +01001038
1039 kfree(hdr);
1040
1041e_free_blob:
1042 kfree(blob);
Joerg Roedeleaf78262020-03-24 10:41:54 +01001043e_unpin_memory:
Cfir Cohen50085be2020-08-07 17:37:46 -07001044 /* content of memory is updated, mark pages dirty */
1045 for (i = 0; i < n; i++) {
1046 set_page_dirty_lock(pages[i]);
1047 mark_page_accessed(pages[i]);
1048 }
Joerg Roedeleaf78262020-03-24 10:41:54 +01001049 sev_unpin_memory(kvm, pages, n);
1050 return ret;
1051}
1052
Brijesh Singh2c07ded2021-01-04 09:17:49 -06001053static int sev_get_attestation_report(struct kvm *kvm, struct kvm_sev_cmd *argp)
1054{
1055 void __user *report = (void __user *)(uintptr_t)argp->data;
1056 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
Sean Christopherson238eca82021-04-06 15:49:52 -07001057 struct sev_data_attestation_report data;
Brijesh Singh2c07ded2021-01-04 09:17:49 -06001058 struct kvm_sev_attestation_report params;
1059 void __user *p;
1060 void *blob = NULL;
1061 int ret;
1062
1063 if (!sev_guest(kvm))
1064 return -ENOTTY;
1065
1066 if (copy_from_user(&params, (void __user *)(uintptr_t)argp->data, sizeof(params)))
1067 return -EFAULT;
1068
Sean Christopherson238eca82021-04-06 15:49:52 -07001069 memset(&data, 0, sizeof(data));
Brijesh Singh2c07ded2021-01-04 09:17:49 -06001070
1071 /* User wants to query the blob length */
1072 if (!params.len)
1073 goto cmd;
1074
1075 p = (void __user *)(uintptr_t)params.uaddr;
1076 if (p) {
Sean Christopherson238eca82021-04-06 15:49:52 -07001077 if (params.len > SEV_FW_BLOB_MAX_SIZE)
1078 return -EINVAL;
Brijesh Singh2c07ded2021-01-04 09:17:49 -06001079
Sean Christophersoneba04b22021-03-30 19:30:25 -07001080 blob = kmalloc(params.len, GFP_KERNEL_ACCOUNT);
Brijesh Singh2c07ded2021-01-04 09:17:49 -06001081 if (!blob)
Sean Christopherson238eca82021-04-06 15:49:52 -07001082 return -ENOMEM;
Brijesh Singh2c07ded2021-01-04 09:17:49 -06001083
Sean Christopherson238eca82021-04-06 15:49:52 -07001084 data.address = __psp_pa(blob);
1085 data.len = params.len;
1086 memcpy(data.mnonce, params.mnonce, sizeof(params.mnonce));
Brijesh Singh2c07ded2021-01-04 09:17:49 -06001087 }
1088cmd:
Sean Christopherson238eca82021-04-06 15:49:52 -07001089 data.handle = sev->handle;
1090 ret = sev_issue_cmd(kvm, SEV_CMD_ATTESTATION_REPORT, &data, &argp->error);
Brijesh Singh2c07ded2021-01-04 09:17:49 -06001091 /*
1092 * If we query the session length, FW responded with expected data.
1093 */
1094 if (!params.len)
1095 goto done;
1096
1097 if (ret)
1098 goto e_free_blob;
1099
1100 if (blob) {
1101 if (copy_to_user(p, blob, params.len))
1102 ret = -EFAULT;
1103 }
1104
1105done:
Sean Christopherson238eca82021-04-06 15:49:52 -07001106 params.len = data.len;
Brijesh Singh2c07ded2021-01-04 09:17:49 -06001107 if (copy_to_user(report, &params, sizeof(params)))
1108 ret = -EFAULT;
1109e_free_blob:
1110 kfree(blob);
Brijesh Singh2c07ded2021-01-04 09:17:49 -06001111 return ret;
1112}
1113
Brijesh Singh4cfdd472021-04-15 15:53:14 +00001114/* Userspace wants to query session length. */
1115static int
1116__sev_send_start_query_session_length(struct kvm *kvm, struct kvm_sev_cmd *argp,
1117 struct kvm_sev_send_start *params)
1118{
1119 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
Sean Christopherson238eca82021-04-06 15:49:52 -07001120 struct sev_data_send_start data;
Brijesh Singh4cfdd472021-04-15 15:53:14 +00001121 int ret;
1122
Ashish Kalra4f13d472021-06-07 06:15:32 +00001123 memset(&data, 0, sizeof(data));
Sean Christopherson238eca82021-04-06 15:49:52 -07001124 data.handle = sev->handle;
1125 ret = sev_issue_cmd(kvm, SEV_CMD_SEND_START, &data, &argp->error);
Brijesh Singh4cfdd472021-04-15 15:53:14 +00001126
Sean Christopherson238eca82021-04-06 15:49:52 -07001127 params->session_len = data.session_len;
Brijesh Singh4cfdd472021-04-15 15:53:14 +00001128 if (copy_to_user((void __user *)(uintptr_t)argp->data, params,
1129 sizeof(struct kvm_sev_send_start)))
1130 ret = -EFAULT;
1131
Brijesh Singh4cfdd472021-04-15 15:53:14 +00001132 return ret;
1133}
1134
1135static int sev_send_start(struct kvm *kvm, struct kvm_sev_cmd *argp)
1136{
1137 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
Sean Christopherson238eca82021-04-06 15:49:52 -07001138 struct sev_data_send_start data;
Brijesh Singh4cfdd472021-04-15 15:53:14 +00001139 struct kvm_sev_send_start params;
1140 void *amd_certs, *session_data;
1141 void *pdh_cert, *plat_certs;
1142 int ret;
1143
1144 if (!sev_guest(kvm))
1145 return -ENOTTY;
1146
1147 if (copy_from_user(&params, (void __user *)(uintptr_t)argp->data,
1148 sizeof(struct kvm_sev_send_start)))
1149 return -EFAULT;
1150
1151 /* if session_len is zero, userspace wants to query the session length */
1152 if (!params.session_len)
1153 return __sev_send_start_query_session_length(kvm, argp,
1154 &params);
1155
1156 /* some sanity checks */
1157 if (!params.pdh_cert_uaddr || !params.pdh_cert_len ||
1158 !params.session_uaddr || params.session_len > SEV_FW_BLOB_MAX_SIZE)
1159 return -EINVAL;
1160
1161 /* allocate the memory to hold the session data blob */
1162 session_data = kmalloc(params.session_len, GFP_KERNEL_ACCOUNT);
1163 if (!session_data)
1164 return -ENOMEM;
1165
1166 /* copy the certificate blobs from userspace */
1167 pdh_cert = psp_copy_user_blob(params.pdh_cert_uaddr,
1168 params.pdh_cert_len);
1169 if (IS_ERR(pdh_cert)) {
1170 ret = PTR_ERR(pdh_cert);
1171 goto e_free_session;
1172 }
1173
1174 plat_certs = psp_copy_user_blob(params.plat_certs_uaddr,
1175 params.plat_certs_len);
1176 if (IS_ERR(plat_certs)) {
1177 ret = PTR_ERR(plat_certs);
1178 goto e_free_pdh;
1179 }
1180
1181 amd_certs = psp_copy_user_blob(params.amd_certs_uaddr,
1182 params.amd_certs_len);
1183 if (IS_ERR(amd_certs)) {
1184 ret = PTR_ERR(amd_certs);
1185 goto e_free_plat_cert;
1186 }
1187
Brijesh Singh4cfdd472021-04-15 15:53:14 +00001188 /* populate the FW SEND_START field with system physical address */
Sean Christopherson238eca82021-04-06 15:49:52 -07001189 memset(&data, 0, sizeof(data));
1190 data.pdh_cert_address = __psp_pa(pdh_cert);
1191 data.pdh_cert_len = params.pdh_cert_len;
1192 data.plat_certs_address = __psp_pa(plat_certs);
1193 data.plat_certs_len = params.plat_certs_len;
1194 data.amd_certs_address = __psp_pa(amd_certs);
1195 data.amd_certs_len = params.amd_certs_len;
1196 data.session_address = __psp_pa(session_data);
1197 data.session_len = params.session_len;
1198 data.handle = sev->handle;
Brijesh Singh4cfdd472021-04-15 15:53:14 +00001199
Sean Christopherson238eca82021-04-06 15:49:52 -07001200 ret = sev_issue_cmd(kvm, SEV_CMD_SEND_START, &data, &argp->error);
Brijesh Singh4cfdd472021-04-15 15:53:14 +00001201
1202 if (!ret && copy_to_user((void __user *)(uintptr_t)params.session_uaddr,
1203 session_data, params.session_len)) {
1204 ret = -EFAULT;
Sean Christopherson238eca82021-04-06 15:49:52 -07001205 goto e_free_amd_cert;
Brijesh Singh4cfdd472021-04-15 15:53:14 +00001206 }
1207
Sean Christopherson238eca82021-04-06 15:49:52 -07001208 params.policy = data.policy;
1209 params.session_len = data.session_len;
Brijesh Singh4cfdd472021-04-15 15:53:14 +00001210 if (copy_to_user((void __user *)(uintptr_t)argp->data, &params,
1211 sizeof(struct kvm_sev_send_start)))
1212 ret = -EFAULT;
1213
Brijesh Singh4cfdd472021-04-15 15:53:14 +00001214e_free_amd_cert:
1215 kfree(amd_certs);
1216e_free_plat_cert:
1217 kfree(plat_certs);
1218e_free_pdh:
1219 kfree(pdh_cert);
1220e_free_session:
1221 kfree(session_data);
1222 return ret;
1223}
1224
Brijesh Singhd3d1af82021-04-15 15:53:55 +00001225/* Userspace wants to query either header or trans length. */
1226static int
1227__sev_send_update_data_query_lengths(struct kvm *kvm, struct kvm_sev_cmd *argp,
1228 struct kvm_sev_send_update_data *params)
1229{
1230 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
Sean Christopherson238eca82021-04-06 15:49:52 -07001231 struct sev_data_send_update_data data;
Brijesh Singhd3d1af82021-04-15 15:53:55 +00001232 int ret;
1233
Ashish Kalra4f13d472021-06-07 06:15:32 +00001234 memset(&data, 0, sizeof(data));
Sean Christopherson238eca82021-04-06 15:49:52 -07001235 data.handle = sev->handle;
1236 ret = sev_issue_cmd(kvm, SEV_CMD_SEND_UPDATE_DATA, &data, &argp->error);
Brijesh Singhd3d1af82021-04-15 15:53:55 +00001237
Sean Christopherson238eca82021-04-06 15:49:52 -07001238 params->hdr_len = data.hdr_len;
1239 params->trans_len = data.trans_len;
Brijesh Singhd3d1af82021-04-15 15:53:55 +00001240
1241 if (copy_to_user((void __user *)(uintptr_t)argp->data, params,
1242 sizeof(struct kvm_sev_send_update_data)))
1243 ret = -EFAULT;
1244
Brijesh Singhd3d1af82021-04-15 15:53:55 +00001245 return ret;
1246}
1247
1248static int sev_send_update_data(struct kvm *kvm, struct kvm_sev_cmd *argp)
1249{
1250 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
Sean Christopherson238eca82021-04-06 15:49:52 -07001251 struct sev_data_send_update_data data;
Brijesh Singhd3d1af82021-04-15 15:53:55 +00001252 struct kvm_sev_send_update_data params;
1253 void *hdr, *trans_data;
1254 struct page **guest_page;
1255 unsigned long n;
1256 int ret, offset;
1257
1258 if (!sev_guest(kvm))
1259 return -ENOTTY;
1260
1261 if (copy_from_user(&params, (void __user *)(uintptr_t)argp->data,
1262 sizeof(struct kvm_sev_send_update_data)))
1263 return -EFAULT;
1264
1265 /* userspace wants to query either header or trans length */
1266 if (!params.trans_len || !params.hdr_len)
1267 return __sev_send_update_data_query_lengths(kvm, argp, &params);
1268
1269 if (!params.trans_uaddr || !params.guest_uaddr ||
1270 !params.guest_len || !params.hdr_uaddr)
1271 return -EINVAL;
1272
1273 /* Check if we are crossing the page boundary */
1274 offset = params.guest_uaddr & (PAGE_SIZE - 1);
1275 if ((params.guest_len + offset > PAGE_SIZE))
1276 return -EINVAL;
1277
1278 /* Pin guest memory */
1279 guest_page = sev_pin_memory(kvm, params.guest_uaddr & PAGE_MASK,
1280 PAGE_SIZE, &n, 0);
Sean Christophersonc7a1b2b2021-05-06 10:58:26 -07001281 if (IS_ERR(guest_page))
1282 return PTR_ERR(guest_page);
Brijesh Singhd3d1af82021-04-15 15:53:55 +00001283
1284 /* allocate memory for header and transport buffer */
1285 ret = -ENOMEM;
1286 hdr = kmalloc(params.hdr_len, GFP_KERNEL_ACCOUNT);
1287 if (!hdr)
1288 goto e_unpin;
1289
1290 trans_data = kmalloc(params.trans_len, GFP_KERNEL_ACCOUNT);
1291 if (!trans_data)
1292 goto e_free_hdr;
1293
Sean Christopherson238eca82021-04-06 15:49:52 -07001294 memset(&data, 0, sizeof(data));
1295 data.hdr_address = __psp_pa(hdr);
1296 data.hdr_len = params.hdr_len;
1297 data.trans_address = __psp_pa(trans_data);
1298 data.trans_len = params.trans_len;
Brijesh Singhd3d1af82021-04-15 15:53:55 +00001299
1300 /* The SEND_UPDATE_DATA command requires C-bit to be always set. */
Sean Christopherson238eca82021-04-06 15:49:52 -07001301 data.guest_address = (page_to_pfn(guest_page[0]) << PAGE_SHIFT) + offset;
1302 data.guest_address |= sev_me_mask;
1303 data.guest_len = params.guest_len;
1304 data.handle = sev->handle;
Brijesh Singhd3d1af82021-04-15 15:53:55 +00001305
Sean Christopherson238eca82021-04-06 15:49:52 -07001306 ret = sev_issue_cmd(kvm, SEV_CMD_SEND_UPDATE_DATA, &data, &argp->error);
Brijesh Singhd3d1af82021-04-15 15:53:55 +00001307
1308 if (ret)
Sean Christopherson238eca82021-04-06 15:49:52 -07001309 goto e_free_trans_data;
Brijesh Singhd3d1af82021-04-15 15:53:55 +00001310
1311 /* copy transport buffer to user space */
1312 if (copy_to_user((void __user *)(uintptr_t)params.trans_uaddr,
1313 trans_data, params.trans_len)) {
1314 ret = -EFAULT;
Sean Christopherson238eca82021-04-06 15:49:52 -07001315 goto e_free_trans_data;
Brijesh Singhd3d1af82021-04-15 15:53:55 +00001316 }
1317
1318 /* Copy packet header to userspace. */
Sean Christophersonb4a69392021-05-06 10:58:25 -07001319 if (copy_to_user((void __user *)(uintptr_t)params.hdr_uaddr, hdr,
1320 params.hdr_len))
1321 ret = -EFAULT;
Brijesh Singhd3d1af82021-04-15 15:53:55 +00001322
Brijesh Singhd3d1af82021-04-15 15:53:55 +00001323e_free_trans_data:
1324 kfree(trans_data);
1325e_free_hdr:
1326 kfree(hdr);
1327e_unpin:
1328 sev_unpin_memory(kvm, guest_page, n);
1329
1330 return ret;
1331}
1332
Brijesh Singhfddecf62021-04-15 15:54:15 +00001333static int sev_send_finish(struct kvm *kvm, struct kvm_sev_cmd *argp)
1334{
1335 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
Sean Christopherson238eca82021-04-06 15:49:52 -07001336 struct sev_data_send_finish data;
Brijesh Singhfddecf62021-04-15 15:54:15 +00001337
1338 if (!sev_guest(kvm))
1339 return -ENOTTY;
1340
Sean Christopherson238eca82021-04-06 15:49:52 -07001341 data.handle = sev->handle;
1342 return sev_issue_cmd(kvm, SEV_CMD_SEND_FINISH, &data, &argp->error);
Brijesh Singhfddecf62021-04-15 15:54:15 +00001343}
1344
Steve Rutherford5569e2e2021-04-20 05:01:20 -04001345static int sev_send_cancel(struct kvm *kvm, struct kvm_sev_cmd *argp)
1346{
1347 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
Sean Christopherson238eca82021-04-06 15:49:52 -07001348 struct sev_data_send_cancel data;
Steve Rutherford5569e2e2021-04-20 05:01:20 -04001349
1350 if (!sev_guest(kvm))
1351 return -ENOTTY;
1352
Sean Christopherson238eca82021-04-06 15:49:52 -07001353 data.handle = sev->handle;
1354 return sev_issue_cmd(kvm, SEV_CMD_SEND_CANCEL, &data, &argp->error);
Steve Rutherford5569e2e2021-04-20 05:01:20 -04001355}
1356
Brijesh Singhaf43cbb2021-04-15 15:54:50 +00001357static int sev_receive_start(struct kvm *kvm, struct kvm_sev_cmd *argp)
1358{
1359 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
Sean Christopherson238eca82021-04-06 15:49:52 -07001360 struct sev_data_receive_start start;
Brijesh Singhaf43cbb2021-04-15 15:54:50 +00001361 struct kvm_sev_receive_start params;
1362 int *error = &argp->error;
1363 void *session_data;
1364 void *pdh_data;
1365 int ret;
1366
1367 if (!sev_guest(kvm))
1368 return -ENOTTY;
1369
1370 /* Get parameter from the userspace */
1371 if (copy_from_user(&params, (void __user *)(uintptr_t)argp->data,
1372 sizeof(struct kvm_sev_receive_start)))
1373 return -EFAULT;
1374
1375 /* some sanity checks */
1376 if (!params.pdh_uaddr || !params.pdh_len ||
1377 !params.session_uaddr || !params.session_len)
1378 return -EINVAL;
1379
1380 pdh_data = psp_copy_user_blob(params.pdh_uaddr, params.pdh_len);
1381 if (IS_ERR(pdh_data))
1382 return PTR_ERR(pdh_data);
1383
1384 session_data = psp_copy_user_blob(params.session_uaddr,
1385 params.session_len);
1386 if (IS_ERR(session_data)) {
1387 ret = PTR_ERR(session_data);
1388 goto e_free_pdh;
1389 }
1390
Sean Christopherson238eca82021-04-06 15:49:52 -07001391 memset(&start, 0, sizeof(start));
1392 start.handle = params.handle;
1393 start.policy = params.policy;
1394 start.pdh_cert_address = __psp_pa(pdh_data);
1395 start.pdh_cert_len = params.pdh_len;
1396 start.session_address = __psp_pa(session_data);
1397 start.session_len = params.session_len;
Brijesh Singhaf43cbb2021-04-15 15:54:50 +00001398
1399 /* create memory encryption context */
Sean Christopherson238eca82021-04-06 15:49:52 -07001400 ret = __sev_issue_cmd(argp->sev_fd, SEV_CMD_RECEIVE_START, &start,
Brijesh Singhaf43cbb2021-04-15 15:54:50 +00001401 error);
1402 if (ret)
Sean Christopherson238eca82021-04-06 15:49:52 -07001403 goto e_free_session;
Brijesh Singhaf43cbb2021-04-15 15:54:50 +00001404
1405 /* Bind ASID to this guest */
Sean Christopherson238eca82021-04-06 15:49:52 -07001406 ret = sev_bind_asid(kvm, start.handle, error);
Mingwei Zhangf1815e0a2021-09-12 18:18:15 +00001407 if (ret) {
1408 sev_decommission(start.handle);
Sean Christopherson238eca82021-04-06 15:49:52 -07001409 goto e_free_session;
Mingwei Zhangf1815e0a2021-09-12 18:18:15 +00001410 }
Brijesh Singhaf43cbb2021-04-15 15:54:50 +00001411
Sean Christopherson238eca82021-04-06 15:49:52 -07001412 params.handle = start.handle;
Brijesh Singhaf43cbb2021-04-15 15:54:50 +00001413 if (copy_to_user((void __user *)(uintptr_t)argp->data,
1414 &params, sizeof(struct kvm_sev_receive_start))) {
1415 ret = -EFAULT;
Sean Christopherson238eca82021-04-06 15:49:52 -07001416 sev_unbind_asid(kvm, start.handle);
1417 goto e_free_session;
Brijesh Singhaf43cbb2021-04-15 15:54:50 +00001418 }
1419
Sean Christopherson238eca82021-04-06 15:49:52 -07001420 sev->handle = start.handle;
Brijesh Singhaf43cbb2021-04-15 15:54:50 +00001421 sev->fd = argp->sev_fd;
1422
Brijesh Singhaf43cbb2021-04-15 15:54:50 +00001423e_free_session:
1424 kfree(session_data);
1425e_free_pdh:
1426 kfree(pdh_data);
1427
1428 return ret;
1429}
1430
Brijesh Singh15fb7de2021-04-15 15:55:17 +00001431static int sev_receive_update_data(struct kvm *kvm, struct kvm_sev_cmd *argp)
1432{
1433 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
1434 struct kvm_sev_receive_update_data params;
Sean Christopherson238eca82021-04-06 15:49:52 -07001435 struct sev_data_receive_update_data data;
Brijesh Singh15fb7de2021-04-15 15:55:17 +00001436 void *hdr = NULL, *trans = NULL;
1437 struct page **guest_page;
1438 unsigned long n;
1439 int ret, offset;
1440
1441 if (!sev_guest(kvm))
1442 return -EINVAL;
1443
1444 if (copy_from_user(&params, (void __user *)(uintptr_t)argp->data,
1445 sizeof(struct kvm_sev_receive_update_data)))
1446 return -EFAULT;
1447
1448 if (!params.hdr_uaddr || !params.hdr_len ||
1449 !params.guest_uaddr || !params.guest_len ||
1450 !params.trans_uaddr || !params.trans_len)
1451 return -EINVAL;
1452
1453 /* Check if we are crossing the page boundary */
1454 offset = params.guest_uaddr & (PAGE_SIZE - 1);
1455 if ((params.guest_len + offset > PAGE_SIZE))
1456 return -EINVAL;
1457
1458 hdr = psp_copy_user_blob(params.hdr_uaddr, params.hdr_len);
1459 if (IS_ERR(hdr))
1460 return PTR_ERR(hdr);
1461
1462 trans = psp_copy_user_blob(params.trans_uaddr, params.trans_len);
1463 if (IS_ERR(trans)) {
1464 ret = PTR_ERR(trans);
1465 goto e_free_hdr;
1466 }
1467
Sean Christopherson238eca82021-04-06 15:49:52 -07001468 memset(&data, 0, sizeof(data));
1469 data.hdr_address = __psp_pa(hdr);
1470 data.hdr_len = params.hdr_len;
1471 data.trans_address = __psp_pa(trans);
1472 data.trans_len = params.trans_len;
Brijesh Singh15fb7de2021-04-15 15:55:17 +00001473
1474 /* Pin guest memory */
Brijesh Singh15fb7de2021-04-15 15:55:17 +00001475 guest_page = sev_pin_memory(kvm, params.guest_uaddr & PAGE_MASK,
Sean Christopherson50c03802021-09-14 14:09:50 -07001476 PAGE_SIZE, &n, 1);
Sean Christophersonc7a1b2b2021-05-06 10:58:26 -07001477 if (IS_ERR(guest_page)) {
1478 ret = PTR_ERR(guest_page);
Sean Christopherson238eca82021-04-06 15:49:52 -07001479 goto e_free_trans;
Sean Christophersonc7a1b2b2021-05-06 10:58:26 -07001480 }
Brijesh Singh15fb7de2021-04-15 15:55:17 +00001481
1482 /* The RECEIVE_UPDATE_DATA command requires C-bit to be always set. */
Sean Christopherson238eca82021-04-06 15:49:52 -07001483 data.guest_address = (page_to_pfn(guest_page[0]) << PAGE_SHIFT) + offset;
1484 data.guest_address |= sev_me_mask;
1485 data.guest_len = params.guest_len;
1486 data.handle = sev->handle;
Brijesh Singh15fb7de2021-04-15 15:55:17 +00001487
Sean Christopherson238eca82021-04-06 15:49:52 -07001488 ret = sev_issue_cmd(kvm, SEV_CMD_RECEIVE_UPDATE_DATA, &data,
Brijesh Singh15fb7de2021-04-15 15:55:17 +00001489 &argp->error);
1490
1491 sev_unpin_memory(kvm, guest_page, n);
1492
Brijesh Singh15fb7de2021-04-15 15:55:17 +00001493e_free_trans:
1494 kfree(trans);
1495e_free_hdr:
1496 kfree(hdr);
1497
1498 return ret;
1499}
1500
Brijesh Singh6a443de2021-04-15 15:55:40 +00001501static int sev_receive_finish(struct kvm *kvm, struct kvm_sev_cmd *argp)
1502{
1503 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
Sean Christopherson238eca82021-04-06 15:49:52 -07001504 struct sev_data_receive_finish data;
Brijesh Singh6a443de2021-04-15 15:55:40 +00001505
1506 if (!sev_guest(kvm))
1507 return -ENOTTY;
1508
Sean Christopherson238eca82021-04-06 15:49:52 -07001509 data.handle = sev->handle;
1510 return sev_issue_cmd(kvm, SEV_CMD_RECEIVE_FINISH, &data, &argp->error);
Brijesh Singh6a443de2021-04-15 15:55:40 +00001511}
1512
Peter Gonda5b92b6c2021-09-21 08:03:45 -07001513static bool cmd_allowed_from_miror(u32 cmd_id)
1514{
1515 /*
1516 * Allow mirrors VM to call KVM_SEV_LAUNCH_UPDATE_VMSA to enable SEV-ES
1517 * active mirror VMs. Also allow the debugging and status commands.
1518 */
1519 if (cmd_id == KVM_SEV_LAUNCH_UPDATE_VMSA ||
1520 cmd_id == KVM_SEV_GUEST_STATUS || cmd_id == KVM_SEV_DBG_DECRYPT ||
1521 cmd_id == KVM_SEV_DBG_ENCRYPT)
1522 return true;
1523
1524 return false;
1525}
1526
Joerg Roedeleaf78262020-03-24 10:41:54 +01001527int svm_mem_enc_op(struct kvm *kvm, void __user *argp)
1528{
1529 struct kvm_sev_cmd sev_cmd;
1530 int r;
1531
Sean Christophersona5c1c5a2021-04-21 19:11:23 -07001532 if (!sev_enabled)
Joerg Roedeleaf78262020-03-24 10:41:54 +01001533 return -ENOTTY;
1534
1535 if (!argp)
1536 return 0;
1537
1538 if (copy_from_user(&sev_cmd, argp, sizeof(struct kvm_sev_cmd)))
1539 return -EFAULT;
1540
1541 mutex_lock(&kvm->lock);
1542
Peter Gonda5b92b6c2021-09-21 08:03:45 -07001543 /* Only the enc_context_owner handles some memory enc operations. */
1544 if (is_mirroring_enc_context(kvm) &&
1545 !cmd_allowed_from_miror(sev_cmd.id)) {
Nathan Tempelman54526d12021-04-08 22:32:14 +00001546 r = -EINVAL;
1547 goto out;
1548 }
1549
Joerg Roedeleaf78262020-03-24 10:41:54 +01001550 switch (sev_cmd.id) {
Sean Christopherson9fa15212021-03-30 20:19:35 -07001551 case KVM_SEV_ES_INIT:
Sean Christopherson8d364a02021-04-21 19:11:17 -07001552 if (!sev_es_enabled) {
Sean Christopherson9fa15212021-03-30 20:19:35 -07001553 r = -ENOTTY;
1554 goto out;
1555 }
1556 fallthrough;
Joerg Roedeleaf78262020-03-24 10:41:54 +01001557 case KVM_SEV_INIT:
1558 r = sev_guest_init(kvm, &sev_cmd);
1559 break;
1560 case KVM_SEV_LAUNCH_START:
1561 r = sev_launch_start(kvm, &sev_cmd);
1562 break;
1563 case KVM_SEV_LAUNCH_UPDATE_DATA:
1564 r = sev_launch_update_data(kvm, &sev_cmd);
1565 break;
Tom Lendackyad731092020-12-10 11:10:09 -06001566 case KVM_SEV_LAUNCH_UPDATE_VMSA:
1567 r = sev_launch_update_vmsa(kvm, &sev_cmd);
1568 break;
Joerg Roedeleaf78262020-03-24 10:41:54 +01001569 case KVM_SEV_LAUNCH_MEASURE:
1570 r = sev_launch_measure(kvm, &sev_cmd);
1571 break;
1572 case KVM_SEV_LAUNCH_FINISH:
1573 r = sev_launch_finish(kvm, &sev_cmd);
1574 break;
1575 case KVM_SEV_GUEST_STATUS:
1576 r = sev_guest_status(kvm, &sev_cmd);
1577 break;
1578 case KVM_SEV_DBG_DECRYPT:
1579 r = sev_dbg_crypt(kvm, &sev_cmd, true);
1580 break;
1581 case KVM_SEV_DBG_ENCRYPT:
1582 r = sev_dbg_crypt(kvm, &sev_cmd, false);
1583 break;
1584 case KVM_SEV_LAUNCH_SECRET:
1585 r = sev_launch_secret(kvm, &sev_cmd);
1586 break;
Brijesh Singh2c07ded2021-01-04 09:17:49 -06001587 case KVM_SEV_GET_ATTESTATION_REPORT:
1588 r = sev_get_attestation_report(kvm, &sev_cmd);
1589 break;
Brijesh Singh4cfdd472021-04-15 15:53:14 +00001590 case KVM_SEV_SEND_START:
1591 r = sev_send_start(kvm, &sev_cmd);
1592 break;
Brijesh Singhd3d1af82021-04-15 15:53:55 +00001593 case KVM_SEV_SEND_UPDATE_DATA:
1594 r = sev_send_update_data(kvm, &sev_cmd);
1595 break;
Brijesh Singhfddecf62021-04-15 15:54:15 +00001596 case KVM_SEV_SEND_FINISH:
1597 r = sev_send_finish(kvm, &sev_cmd);
1598 break;
Steve Rutherford5569e2e2021-04-20 05:01:20 -04001599 case KVM_SEV_SEND_CANCEL:
1600 r = sev_send_cancel(kvm, &sev_cmd);
1601 break;
Brijesh Singhaf43cbb2021-04-15 15:54:50 +00001602 case KVM_SEV_RECEIVE_START:
1603 r = sev_receive_start(kvm, &sev_cmd);
1604 break;
Brijesh Singh15fb7de2021-04-15 15:55:17 +00001605 case KVM_SEV_RECEIVE_UPDATE_DATA:
1606 r = sev_receive_update_data(kvm, &sev_cmd);
1607 break;
Brijesh Singh6a443de2021-04-15 15:55:40 +00001608 case KVM_SEV_RECEIVE_FINISH:
1609 r = sev_receive_finish(kvm, &sev_cmd);
1610 break;
Joerg Roedeleaf78262020-03-24 10:41:54 +01001611 default:
1612 r = -EINVAL;
1613 goto out;
1614 }
1615
1616 if (copy_to_user(argp, &sev_cmd, sizeof(struct kvm_sev_cmd)))
1617 r = -EFAULT;
1618
1619out:
1620 mutex_unlock(&kvm->lock);
1621 return r;
1622}
1623
1624int svm_register_enc_region(struct kvm *kvm,
1625 struct kvm_enc_region *range)
1626{
1627 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
1628 struct enc_region *region;
1629 int ret = 0;
1630
1631 if (!sev_guest(kvm))
1632 return -ENOTTY;
1633
Nathan Tempelman54526d12021-04-08 22:32:14 +00001634 /* If kvm is mirroring encryption context it isn't responsible for it */
1635 if (is_mirroring_enc_context(kvm))
1636 return -EINVAL;
1637
Joerg Roedeleaf78262020-03-24 10:41:54 +01001638 if (range->addr > ULONG_MAX || range->size > ULONG_MAX)
1639 return -EINVAL;
1640
1641 region = kzalloc(sizeof(*region), GFP_KERNEL_ACCOUNT);
1642 if (!region)
1643 return -ENOMEM;
1644
Peter Gonda19a23da2021-01-27 08:15:24 -08001645 mutex_lock(&kvm->lock);
Joerg Roedeleaf78262020-03-24 10:41:54 +01001646 region->pages = sev_pin_memory(kvm, range->addr, range->size, &region->npages, 1);
Paolo Bonzinia8d908b2020-06-23 05:12:24 -04001647 if (IS_ERR(region->pages)) {
1648 ret = PTR_ERR(region->pages);
Peter Gonda19a23da2021-01-27 08:15:24 -08001649 mutex_unlock(&kvm->lock);
Joerg Roedeleaf78262020-03-24 10:41:54 +01001650 goto e_free;
1651 }
1652
Peter Gonda19a23da2021-01-27 08:15:24 -08001653 region->uaddr = range->addr;
1654 region->size = range->size;
1655
1656 list_add_tail(&region->list, &sev->regions_list);
1657 mutex_unlock(&kvm->lock);
1658
Joerg Roedeleaf78262020-03-24 10:41:54 +01001659 /*
1660 * The guest may change the memory encryption attribute from C=0 -> C=1
1661 * or vice versa for this memory range. Lets make sure caches are
1662 * flushed to ensure that guest data gets written into memory with
1663 * correct C-bit.
1664 */
1665 sev_clflush_pages(region->pages, region->npages);
1666
Joerg Roedeleaf78262020-03-24 10:41:54 +01001667 return ret;
1668
1669e_free:
1670 kfree(region);
1671 return ret;
1672}
1673
1674static struct enc_region *
1675find_enc_region(struct kvm *kvm, struct kvm_enc_region *range)
1676{
1677 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
1678 struct list_head *head = &sev->regions_list;
1679 struct enc_region *i;
1680
1681 list_for_each_entry(i, head, list) {
1682 if (i->uaddr == range->addr &&
1683 i->size == range->size)
1684 return i;
1685 }
1686
1687 return NULL;
1688}
1689
1690static void __unregister_enc_region_locked(struct kvm *kvm,
1691 struct enc_region *region)
1692{
1693 sev_unpin_memory(kvm, region->pages, region->npages);
1694 list_del(&region->list);
1695 kfree(region);
1696}
1697
1698int svm_unregister_enc_region(struct kvm *kvm,
1699 struct kvm_enc_region *range)
1700{
1701 struct enc_region *region;
1702 int ret;
1703
Nathan Tempelman54526d12021-04-08 22:32:14 +00001704 /* If kvm is mirroring encryption context it isn't responsible for it */
1705 if (is_mirroring_enc_context(kvm))
1706 return -EINVAL;
1707
Joerg Roedeleaf78262020-03-24 10:41:54 +01001708 mutex_lock(&kvm->lock);
1709
1710 if (!sev_guest(kvm)) {
1711 ret = -ENOTTY;
1712 goto failed;
1713 }
1714
1715 region = find_enc_region(kvm, range);
1716 if (!region) {
1717 ret = -EINVAL;
1718 goto failed;
1719 }
1720
1721 /*
1722 * Ensure that all guest tagged cache entries are flushed before
1723 * releasing the pages back to the system for use. CLFLUSH will
1724 * not do this, so issue a WBINVD.
1725 */
1726 wbinvd_on_all_cpus();
1727
1728 __unregister_enc_region_locked(kvm, region);
1729
1730 mutex_unlock(&kvm->lock);
1731 return 0;
1732
1733failed:
1734 mutex_unlock(&kvm->lock);
1735 return ret;
1736}
1737
Nathan Tempelman54526d12021-04-08 22:32:14 +00001738int svm_vm_copy_asid_from(struct kvm *kvm, unsigned int source_fd)
1739{
1740 struct file *source_kvm_file;
1741 struct kvm *source_kvm;
Peter Gondaf43c8872021-09-21 08:03:44 -07001742 struct kvm_sev_info source_sev, *mirror_sev;
Nathan Tempelman54526d12021-04-08 22:32:14 +00001743 int ret;
1744
1745 source_kvm_file = fget(source_fd);
1746 if (!file_is_kvm(source_kvm_file)) {
1747 ret = -EBADF;
1748 goto e_source_put;
1749 }
1750
1751 source_kvm = source_kvm_file->private_data;
1752 mutex_lock(&source_kvm->lock);
1753
1754 if (!sev_guest(source_kvm)) {
1755 ret = -EINVAL;
1756 goto e_source_unlock;
1757 }
1758
1759 /* Mirrors of mirrors should work, but let's not get silly */
1760 if (is_mirroring_enc_context(source_kvm) || source_kvm == kvm) {
1761 ret = -EINVAL;
1762 goto e_source_unlock;
1763 }
1764
Peter Gondaf43c8872021-09-21 08:03:44 -07001765 memcpy(&source_sev, &to_kvm_svm(source_kvm)->sev_info,
1766 sizeof(source_sev));
Nathan Tempelman54526d12021-04-08 22:32:14 +00001767
1768 /*
1769 * The mirror kvm holds an enc_context_owner ref so its asid can't
1770 * disappear until we're done with it
1771 */
1772 kvm_get_kvm(source_kvm);
1773
1774 fput(source_kvm_file);
1775 mutex_unlock(&source_kvm->lock);
1776 mutex_lock(&kvm->lock);
1777
1778 if (sev_guest(kvm)) {
1779 ret = -EINVAL;
1780 goto e_mirror_unlock;
1781 }
1782
1783 /* Set enc_context_owner and copy its encryption context over */
1784 mirror_sev = &to_kvm_svm(kvm)->sev_info;
1785 mirror_sev->enc_context_owner = source_kvm;
Nathan Tempelman54526d12021-04-08 22:32:14 +00001786 mirror_sev->active = true;
Peter Gondaf43c8872021-09-21 08:03:44 -07001787 mirror_sev->asid = source_sev.asid;
1788 mirror_sev->fd = source_sev.fd;
1789 mirror_sev->es_active = source_sev.es_active;
1790 mirror_sev->handle = source_sev.handle;
1791 /*
1792 * Do not copy ap_jump_table. Since the mirror does not share the same
1793 * KVM contexts as the original, and they may have different
1794 * memory-views.
1795 */
Nathan Tempelman54526d12021-04-08 22:32:14 +00001796
1797 mutex_unlock(&kvm->lock);
1798 return 0;
1799
1800e_mirror_unlock:
1801 mutex_unlock(&kvm->lock);
1802 kvm_put_kvm(source_kvm);
1803 return ret;
1804e_source_unlock:
1805 mutex_unlock(&source_kvm->lock);
1806e_source_put:
Colin Ian King8899a5f2021-04-30 18:03:03 +01001807 if (source_kvm_file)
1808 fput(source_kvm_file);
Nathan Tempelman54526d12021-04-08 22:32:14 +00001809 return ret;
1810}
1811
Joerg Roedeleaf78262020-03-24 10:41:54 +01001812void sev_vm_destroy(struct kvm *kvm)
1813{
1814 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
1815 struct list_head *head = &sev->regions_list;
1816 struct list_head *pos, *q;
1817
1818 if (!sev_guest(kvm))
1819 return;
1820
Nathan Tempelman54526d12021-04-08 22:32:14 +00001821 /* If this is a mirror_kvm release the enc_context_owner and skip sev cleanup */
1822 if (is_mirroring_enc_context(kvm)) {
1823 kvm_put_kvm(sev->enc_context_owner);
1824 return;
1825 }
1826
Joerg Roedeleaf78262020-03-24 10:41:54 +01001827 mutex_lock(&kvm->lock);
1828
1829 /*
1830 * Ensure that all guest tagged cache entries are flushed before
1831 * releasing the pages back to the system for use. CLFLUSH will
1832 * not do this, so issue a WBINVD.
1833 */
1834 wbinvd_on_all_cpus();
1835
1836 /*
1837 * if userspace was terminated before unregistering the memory regions
1838 * then lets unpin all the registered memory.
1839 */
1840 if (!list_empty(head)) {
1841 list_for_each_safe(pos, q, head) {
1842 __unregister_enc_region_locked(kvm,
1843 list_entry(pos, struct enc_region, list));
David Rientjes7be74942020-08-25 12:56:28 -07001844 cond_resched();
Joerg Roedeleaf78262020-03-24 10:41:54 +01001845 }
1846 }
1847
1848 mutex_unlock(&kvm->lock);
1849
1850 sev_unbind_asid(kvm, sev->handle);
Vipin Sharma7aef27f2021-03-29 21:42:06 -07001851 sev_asid_free(sev);
Joerg Roedeleaf78262020-03-24 10:41:54 +01001852}
1853
Paolo Bonzinid9db0fd2021-04-21 19:11:15 -07001854void __init sev_set_cpu_caps(void)
1855{
Sean Christopherson8d364a02021-04-21 19:11:17 -07001856 if (!sev_enabled)
Paolo Bonzinid9db0fd2021-04-21 19:11:15 -07001857 kvm_cpu_cap_clear(X86_FEATURE_SEV);
Sean Christopherson8d364a02021-04-21 19:11:17 -07001858 if (!sev_es_enabled)
Paolo Bonzinid9db0fd2021-04-21 19:11:15 -07001859 kvm_cpu_cap_clear(X86_FEATURE_SEV_ES);
1860}
1861
Tom Lendacky916391a2020-12-10 11:09:38 -06001862void __init sev_hardware_setup(void)
Joerg Roedeleaf78262020-03-24 10:41:54 +01001863{
Sean Christophersona479c332021-04-21 19:11:18 -07001864#ifdef CONFIG_KVM_AMD_SEV
Vipin Sharma7aef27f2021-03-29 21:42:06 -07001865 unsigned int eax, ebx, ecx, edx, sev_asid_count, sev_es_asid_count;
Tom Lendacky916391a2020-12-10 11:09:38 -06001866 bool sev_es_supported = false;
1867 bool sev_supported = false;
1868
Sean Christophersona479c332021-04-21 19:11:18 -07001869 if (!sev_enabled || !npt_enabled)
Sean Christophersone8126bd2021-04-21 19:11:14 -07001870 goto out;
1871
Tom Lendacky916391a2020-12-10 11:09:38 -06001872 /* Does the CPU support SEV? */
1873 if (!boot_cpu_has(X86_FEATURE_SEV))
1874 goto out;
1875
1876 /* Retrieve SEV CPUID information */
1877 cpuid(0x8000001f, &eax, &ebx, &ecx, &edx);
1878
Tom Lendacky1edc1452020-12-10 11:09:49 -06001879 /* Set encryption bit location for SEV-ES guests */
1880 sev_enc_bit = ebx & 0x3f;
1881
Joerg Roedeleaf78262020-03-24 10:41:54 +01001882 /* Maximum number of encrypted guests supported simultaneously */
Tom Lendacky916391a2020-12-10 11:09:38 -06001883 max_sev_asid = ecx;
Sean Christopherson8cb756b2021-04-21 19:11:21 -07001884 if (!max_sev_asid)
Tom Lendacky916391a2020-12-10 11:09:38 -06001885 goto out;
Joerg Roedeleaf78262020-03-24 10:41:54 +01001886
1887 /* Minimum ASID value that should be used for SEV guest */
Tom Lendacky916391a2020-12-10 11:09:38 -06001888 min_sev_asid = edx;
Brijesh Singhd3d1af82021-04-15 15:53:55 +00001889 sev_me_mask = 1UL << (ebx & 0x3f);
Joerg Roedeleaf78262020-03-24 10:41:54 +01001890
Mingwei Zhangbb2baeb2021-08-02 11:09:03 -07001891 /*
1892 * Initialize SEV ASID bitmaps. Allocate space for ASID 0 in the bitmap,
1893 * even though it's never used, so that the bitmap is indexed by the
1894 * actual ASID.
1895 */
1896 nr_asids = max_sev_asid + 1;
1897 sev_asid_bitmap = bitmap_zalloc(nr_asids, GFP_KERNEL);
Joerg Roedeleaf78262020-03-24 10:41:54 +01001898 if (!sev_asid_bitmap)
Tom Lendacky916391a2020-12-10 11:09:38 -06001899 goto out;
Joerg Roedeleaf78262020-03-24 10:41:54 +01001900
Mingwei Zhangbb2baeb2021-08-02 11:09:03 -07001901 sev_reclaim_asid_bitmap = bitmap_zalloc(nr_asids, GFP_KERNEL);
Sean Christophersonf31b88b2021-04-21 19:11:12 -07001902 if (!sev_reclaim_asid_bitmap) {
1903 bitmap_free(sev_asid_bitmap);
1904 sev_asid_bitmap = NULL;
Tom Lendacky916391a2020-12-10 11:09:38 -06001905 goto out;
Sean Christophersonf31b88b2021-04-21 19:11:12 -07001906 }
Joerg Roedeleaf78262020-03-24 10:41:54 +01001907
Vipin Sharma7aef27f2021-03-29 21:42:06 -07001908 sev_asid_count = max_sev_asid - min_sev_asid + 1;
1909 if (misc_cg_set_capacity(MISC_CG_RES_SEV, sev_asid_count))
1910 goto out;
1911
1912 pr_info("SEV supported: %u ASIDs\n", sev_asid_count);
Tom Lendacky916391a2020-12-10 11:09:38 -06001913 sev_supported = true;
Joerg Roedeleaf78262020-03-24 10:41:54 +01001914
Tom Lendacky916391a2020-12-10 11:09:38 -06001915 /* SEV-ES support requested? */
Sean Christopherson8d364a02021-04-21 19:11:17 -07001916 if (!sev_es_enabled)
Tom Lendacky916391a2020-12-10 11:09:38 -06001917 goto out;
1918
1919 /* Does the CPU support SEV-ES? */
1920 if (!boot_cpu_has(X86_FEATURE_SEV_ES))
1921 goto out;
1922
1923 /* Has the system been allocated ASIDs for SEV-ES? */
1924 if (min_sev_asid == 1)
1925 goto out;
1926
Vipin Sharma7aef27f2021-03-29 21:42:06 -07001927 sev_es_asid_count = min_sev_asid - 1;
1928 if (misc_cg_set_capacity(MISC_CG_RES_SEV_ES, sev_es_asid_count))
1929 goto out;
1930
1931 pr_info("SEV-ES supported: %u ASIDs\n", sev_es_asid_count);
Tom Lendacky916391a2020-12-10 11:09:38 -06001932 sev_es_supported = true;
1933
1934out:
Sean Christopherson8d364a02021-04-21 19:11:17 -07001935 sev_enabled = sev_supported;
1936 sev_es_enabled = sev_es_supported;
Sean Christophersona479c332021-04-21 19:11:18 -07001937#endif
Joerg Roedeleaf78262020-03-24 10:41:54 +01001938}
1939
1940void sev_hardware_teardown(void)
1941{
Sean Christophersona5c1c5a2021-04-21 19:11:23 -07001942 if (!sev_enabled)
Paolo Bonzini9ef15302020-04-13 03:20:06 -04001943 return;
1944
Sean Christopherson469bb322021-04-21 19:11:25 -07001945 /* No need to take sev_bitmap_lock, all VMs have been destroyed. */
Mingwei Zhangbb2baeb2021-08-02 11:09:03 -07001946 sev_flush_asids(1, max_sev_asid);
Sean Christopherson469bb322021-04-21 19:11:25 -07001947
Joerg Roedeleaf78262020-03-24 10:41:54 +01001948 bitmap_free(sev_asid_bitmap);
1949 bitmap_free(sev_reclaim_asid_bitmap);
Sean Christopherson469bb322021-04-21 19:11:25 -07001950
Vipin Sharma7aef27f2021-03-29 21:42:06 -07001951 misc_cg_set_capacity(MISC_CG_RES_SEV, 0);
1952 misc_cg_set_capacity(MISC_CG_RES_SEV_ES, 0);
Joerg Roedeleaf78262020-03-24 10:41:54 +01001953}
Joerg Roedeleaf78262020-03-24 10:41:54 +01001954
Sean Christophersonb95c2212021-04-21 19:11:22 -07001955int sev_cpu_init(struct svm_cpu_data *sd)
1956{
Sean Christophersona5c1c5a2021-04-21 19:11:23 -07001957 if (!sev_enabled)
Sean Christophersonb95c2212021-04-21 19:11:22 -07001958 return 0;
1959
Mingwei Zhangbb2baeb2021-08-02 11:09:03 -07001960 sd->sev_vmcbs = kcalloc(nr_asids, sizeof(void *), GFP_KERNEL);
Sean Christophersonb95c2212021-04-21 19:11:22 -07001961 if (!sd->sev_vmcbs)
1962 return -ENOMEM;
1963
1964 return 0;
Joerg Roedeleaf78262020-03-24 10:41:54 +01001965}
1966
Tom Lendackyadd5e2f2020-12-10 11:09:40 -06001967/*
1968 * Pages used by hardware to hold guest encrypted state must be flushed before
1969 * returning them to the system.
1970 */
1971static void sev_flush_guest_memory(struct vcpu_svm *svm, void *va,
1972 unsigned long len)
1973{
1974 /*
1975 * If hardware enforced cache coherency for encrypted mappings of the
1976 * same physical page is supported, nothing to do.
1977 */
1978 if (boot_cpu_has(X86_FEATURE_SME_COHERENT))
1979 return;
1980
1981 /*
1982 * If the VM Page Flush MSR is supported, use it to flush the page
1983 * (using the page virtual address and the guest ASID).
1984 */
1985 if (boot_cpu_has(X86_FEATURE_VM_PAGE_FLUSH)) {
1986 struct kvm_sev_info *sev;
1987 unsigned long va_start;
1988 u64 start, stop;
1989
1990 /* Align start and stop to page boundaries. */
1991 va_start = (unsigned long)va;
1992 start = (u64)va_start & PAGE_MASK;
1993 stop = PAGE_ALIGN((u64)va_start + len);
1994
1995 if (start < stop) {
1996 sev = &to_kvm_svm(svm->vcpu.kvm)->sev_info;
1997
1998 while (start < stop) {
1999 wrmsrl(MSR_AMD64_VM_PAGE_FLUSH,
2000 start | sev->asid);
2001
2002 start += PAGE_SIZE;
2003 }
2004
2005 return;
2006 }
2007
2008 WARN(1, "Address overflow, using WBINVD\n");
2009 }
2010
2011 /*
2012 * Hardware should always have one of the above features,
2013 * but if not, use WBINVD and issue a warning.
2014 */
2015 WARN_ONCE(1, "Using WBINVD to flush guest memory\n");
2016 wbinvd_on_all_cpus();
2017}
2018
2019void sev_free_vcpu(struct kvm_vcpu *vcpu)
2020{
2021 struct vcpu_svm *svm;
2022
2023 if (!sev_es_guest(vcpu->kvm))
2024 return;
2025
2026 svm = to_svm(vcpu);
2027
2028 if (vcpu->arch.guest_state_protected)
2029 sev_flush_guest_memory(svm, svm->vmsa, PAGE_SIZE);
2030 __free_page(virt_to_page(svm->vmsa));
Tom Lendacky8f423a82020-12-10 11:09:53 -06002031
2032 if (svm->ghcb_sa_free)
2033 kfree(svm->ghcb_sa);
Tom Lendackyadd5e2f2020-12-10 11:09:40 -06002034}
2035
Tom Lendacky291bd202020-12-10 11:09:47 -06002036static void dump_ghcb(struct vcpu_svm *svm)
2037{
2038 struct ghcb *ghcb = svm->ghcb;
2039 unsigned int nbits;
2040
2041 /* Re-use the dump_invalid_vmcb module parameter */
2042 if (!dump_invalid_vmcb) {
2043 pr_warn_ratelimited("set kvm_amd.dump_invalid_vmcb=1 to dump internal KVM state.\n");
2044 return;
2045 }
2046
2047 nbits = sizeof(ghcb->save.valid_bitmap) * 8;
2048
2049 pr_err("GHCB (GPA=%016llx):\n", svm->vmcb->control.ghcb_gpa);
2050 pr_err("%-20s%016llx is_valid: %u\n", "sw_exit_code",
2051 ghcb->save.sw_exit_code, ghcb_sw_exit_code_is_valid(ghcb));
2052 pr_err("%-20s%016llx is_valid: %u\n", "sw_exit_info_1",
2053 ghcb->save.sw_exit_info_1, ghcb_sw_exit_info_1_is_valid(ghcb));
2054 pr_err("%-20s%016llx is_valid: %u\n", "sw_exit_info_2",
2055 ghcb->save.sw_exit_info_2, ghcb_sw_exit_info_2_is_valid(ghcb));
2056 pr_err("%-20s%016llx is_valid: %u\n", "sw_scratch",
2057 ghcb->save.sw_scratch, ghcb_sw_scratch_is_valid(ghcb));
2058 pr_err("%-20s%*pb\n", "valid_bitmap", nbits, ghcb->save.valid_bitmap);
2059}
2060
2061static void sev_es_sync_to_ghcb(struct vcpu_svm *svm)
2062{
2063 struct kvm_vcpu *vcpu = &svm->vcpu;
2064 struct ghcb *ghcb = svm->ghcb;
2065
2066 /*
2067 * The GHCB protocol so far allows for the following data
2068 * to be returned:
2069 * GPRs RAX, RBX, RCX, RDX
2070 *
Sean Christopherson25009142021-01-22 15:50:47 -08002071 * Copy their values, even if they may not have been written during the
2072 * VM-Exit. It's the guest's responsibility to not consume random data.
Tom Lendacky291bd202020-12-10 11:09:47 -06002073 */
Sean Christopherson25009142021-01-22 15:50:47 -08002074 ghcb_set_rax(ghcb, vcpu->arch.regs[VCPU_REGS_RAX]);
2075 ghcb_set_rbx(ghcb, vcpu->arch.regs[VCPU_REGS_RBX]);
2076 ghcb_set_rcx(ghcb, vcpu->arch.regs[VCPU_REGS_RCX]);
2077 ghcb_set_rdx(ghcb, vcpu->arch.regs[VCPU_REGS_RDX]);
Tom Lendacky291bd202020-12-10 11:09:47 -06002078}
2079
2080static void sev_es_sync_from_ghcb(struct vcpu_svm *svm)
2081{
2082 struct vmcb_control_area *control = &svm->vmcb->control;
2083 struct kvm_vcpu *vcpu = &svm->vcpu;
2084 struct ghcb *ghcb = svm->ghcb;
2085 u64 exit_code;
2086
2087 /*
2088 * The GHCB protocol so far allows for the following data
2089 * to be supplied:
2090 * GPRs RAX, RBX, RCX, RDX
2091 * XCR0
2092 * CPL
2093 *
2094 * VMMCALL allows the guest to provide extra registers. KVM also
2095 * expects RSI for hypercalls, so include that, too.
2096 *
2097 * Copy their values to the appropriate location if supplied.
2098 */
2099 memset(vcpu->arch.regs, 0, sizeof(vcpu->arch.regs));
2100
2101 vcpu->arch.regs[VCPU_REGS_RAX] = ghcb_get_rax_if_valid(ghcb);
2102 vcpu->arch.regs[VCPU_REGS_RBX] = ghcb_get_rbx_if_valid(ghcb);
2103 vcpu->arch.regs[VCPU_REGS_RCX] = ghcb_get_rcx_if_valid(ghcb);
2104 vcpu->arch.regs[VCPU_REGS_RDX] = ghcb_get_rdx_if_valid(ghcb);
2105 vcpu->arch.regs[VCPU_REGS_RSI] = ghcb_get_rsi_if_valid(ghcb);
2106
2107 svm->vmcb->save.cpl = ghcb_get_cpl_if_valid(ghcb);
2108
2109 if (ghcb_xcr0_is_valid(ghcb)) {
2110 vcpu->arch.xcr0 = ghcb_get_xcr0(ghcb);
2111 kvm_update_cpuid_runtime(vcpu);
2112 }
2113
2114 /* Copy the GHCB exit information into the VMCB fields */
2115 exit_code = ghcb_get_sw_exit_code(ghcb);
2116 control->exit_code = lower_32_bits(exit_code);
2117 control->exit_code_hi = upper_32_bits(exit_code);
2118 control->exit_info_1 = ghcb_get_sw_exit_info_1(ghcb);
2119 control->exit_info_2 = ghcb_get_sw_exit_info_2(ghcb);
2120
2121 /* Clear the valid entries fields */
2122 memset(ghcb->save.valid_bitmap, 0, sizeof(ghcb->save.valid_bitmap));
2123}
2124
2125static int sev_es_validate_vmgexit(struct vcpu_svm *svm)
2126{
2127 struct kvm_vcpu *vcpu;
2128 struct ghcb *ghcb;
2129 u64 exit_code = 0;
2130
2131 ghcb = svm->ghcb;
2132
2133 /* Only GHCB Usage code 0 is supported */
2134 if (ghcb->ghcb_usage)
2135 goto vmgexit_err;
2136
2137 /*
2138 * Retrieve the exit code now even though is may not be marked valid
2139 * as it could help with debugging.
2140 */
2141 exit_code = ghcb_get_sw_exit_code(ghcb);
2142
2143 if (!ghcb_sw_exit_code_is_valid(ghcb) ||
2144 !ghcb_sw_exit_info_1_is_valid(ghcb) ||
2145 !ghcb_sw_exit_info_2_is_valid(ghcb))
2146 goto vmgexit_err;
2147
2148 switch (ghcb_get_sw_exit_code(ghcb)) {
2149 case SVM_EXIT_READ_DR7:
2150 break;
2151 case SVM_EXIT_WRITE_DR7:
2152 if (!ghcb_rax_is_valid(ghcb))
2153 goto vmgexit_err;
2154 break;
2155 case SVM_EXIT_RDTSC:
2156 break;
2157 case SVM_EXIT_RDPMC:
2158 if (!ghcb_rcx_is_valid(ghcb))
2159 goto vmgexit_err;
2160 break;
2161 case SVM_EXIT_CPUID:
2162 if (!ghcb_rax_is_valid(ghcb) ||
2163 !ghcb_rcx_is_valid(ghcb))
2164 goto vmgexit_err;
2165 if (ghcb_get_rax(ghcb) == 0xd)
2166 if (!ghcb_xcr0_is_valid(ghcb))
2167 goto vmgexit_err;
2168 break;
2169 case SVM_EXIT_INVD:
2170 break;
2171 case SVM_EXIT_IOIO:
Tom Lendacky7ed9abf2020-12-10 11:09:54 -06002172 if (ghcb_get_sw_exit_info_1(ghcb) & SVM_IOIO_STR_MASK) {
2173 if (!ghcb_sw_scratch_is_valid(ghcb))
Tom Lendacky291bd202020-12-10 11:09:47 -06002174 goto vmgexit_err;
Tom Lendacky7ed9abf2020-12-10 11:09:54 -06002175 } else {
2176 if (!(ghcb_get_sw_exit_info_1(ghcb) & SVM_IOIO_TYPE_MASK))
2177 if (!ghcb_rax_is_valid(ghcb))
2178 goto vmgexit_err;
2179 }
Tom Lendacky291bd202020-12-10 11:09:47 -06002180 break;
2181 case SVM_EXIT_MSR:
2182 if (!ghcb_rcx_is_valid(ghcb))
2183 goto vmgexit_err;
2184 if (ghcb_get_sw_exit_info_1(ghcb)) {
2185 if (!ghcb_rax_is_valid(ghcb) ||
2186 !ghcb_rdx_is_valid(ghcb))
2187 goto vmgexit_err;
2188 }
2189 break;
2190 case SVM_EXIT_VMMCALL:
2191 if (!ghcb_rax_is_valid(ghcb) ||
2192 !ghcb_cpl_is_valid(ghcb))
2193 goto vmgexit_err;
2194 break;
2195 case SVM_EXIT_RDTSCP:
2196 break;
2197 case SVM_EXIT_WBINVD:
2198 break;
2199 case SVM_EXIT_MONITOR:
2200 if (!ghcb_rax_is_valid(ghcb) ||
2201 !ghcb_rcx_is_valid(ghcb) ||
2202 !ghcb_rdx_is_valid(ghcb))
2203 goto vmgexit_err;
2204 break;
2205 case SVM_EXIT_MWAIT:
2206 if (!ghcb_rax_is_valid(ghcb) ||
2207 !ghcb_rcx_is_valid(ghcb))
2208 goto vmgexit_err;
2209 break;
Tom Lendacky8f423a82020-12-10 11:09:53 -06002210 case SVM_VMGEXIT_MMIO_READ:
2211 case SVM_VMGEXIT_MMIO_WRITE:
2212 if (!ghcb_sw_scratch_is_valid(ghcb))
2213 goto vmgexit_err;
2214 break;
Tom Lendacky4444dfe2020-12-14 11:16:03 -05002215 case SVM_VMGEXIT_NMI_COMPLETE:
Tom Lendacky647daca2021-01-04 14:20:01 -06002216 case SVM_VMGEXIT_AP_HLT_LOOP:
Tom Lendacky8640ca52020-12-15 12:44:07 -05002217 case SVM_VMGEXIT_AP_JUMP_TABLE:
Tom Lendacky291bd202020-12-10 11:09:47 -06002218 case SVM_VMGEXIT_UNSUPPORTED_EVENT:
2219 break;
2220 default:
2221 goto vmgexit_err;
2222 }
2223
2224 return 0;
2225
2226vmgexit_err:
2227 vcpu = &svm->vcpu;
2228
2229 if (ghcb->ghcb_usage) {
2230 vcpu_unimpl(vcpu, "vmgexit: ghcb usage %#x is not valid\n",
2231 ghcb->ghcb_usage);
2232 } else {
2233 vcpu_unimpl(vcpu, "vmgexit: exit reason %#llx is not valid\n",
2234 exit_code);
2235 dump_ghcb(svm);
2236 }
2237
2238 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
2239 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_UNEXPECTED_EXIT_REASON;
2240 vcpu->run->internal.ndata = 2;
2241 vcpu->run->internal.data[0] = exit_code;
2242 vcpu->run->internal.data[1] = vcpu->arch.last_vmentry_cpu;
2243
2244 return -EINVAL;
2245}
2246
Tom Lendackyce7ea0c2021-05-06 15:14:41 -05002247void sev_es_unmap_ghcb(struct vcpu_svm *svm)
Tom Lendacky291bd202020-12-10 11:09:47 -06002248{
2249 if (!svm->ghcb)
2250 return;
2251
Tom Lendacky8f423a82020-12-10 11:09:53 -06002252 if (svm->ghcb_sa_free) {
2253 /*
2254 * The scratch area lives outside the GHCB, so there is a
2255 * buffer that, depending on the operation performed, may
2256 * need to be synced, then freed.
2257 */
2258 if (svm->ghcb_sa_sync) {
2259 kvm_write_guest(svm->vcpu.kvm,
2260 ghcb_get_sw_scratch(svm->ghcb),
2261 svm->ghcb_sa, svm->ghcb_sa_len);
2262 svm->ghcb_sa_sync = false;
2263 }
2264
2265 kfree(svm->ghcb_sa);
2266 svm->ghcb_sa = NULL;
2267 svm->ghcb_sa_free = false;
2268 }
2269
Tom Lendackyd523ab6b2020-12-10 11:09:48 -06002270 trace_kvm_vmgexit_exit(svm->vcpu.vcpu_id, svm->ghcb);
2271
Tom Lendacky291bd202020-12-10 11:09:47 -06002272 sev_es_sync_to_ghcb(svm);
2273
2274 kvm_vcpu_unmap(&svm->vcpu, &svm->ghcb_map, true);
2275 svm->ghcb = NULL;
2276}
2277
Joerg Roedeleaf78262020-03-24 10:41:54 +01002278void pre_sev_run(struct vcpu_svm *svm, int cpu)
2279{
2280 struct svm_cpu_data *sd = per_cpu(svm_data, cpu);
2281 int asid = sev_get_asid(svm->vcpu.kvm);
2282
2283 /* Assign the asid allocated with this SEV guest */
Paolo Bonzinidee734a2020-11-30 09:39:59 -05002284 svm->asid = asid;
Joerg Roedeleaf78262020-03-24 10:41:54 +01002285
2286 /*
2287 * Flush guest TLB:
2288 *
2289 * 1) when different VMCB for the same ASID is to be run on the same host CPU.
2290 * 2) or this VMCB was executed on different host CPU in previous VMRUNs.
2291 */
2292 if (sd->sev_vmcbs[asid] == svm->vmcb &&
Jim Mattson8a14fe42020-06-03 16:56:22 -07002293 svm->vcpu.arch.last_vmentry_cpu == cpu)
Joerg Roedeleaf78262020-03-24 10:41:54 +01002294 return;
2295
Joerg Roedeleaf78262020-03-24 10:41:54 +01002296 sd->sev_vmcbs[asid] = svm->vmcb;
2297 svm->vmcb->control.tlb_ctl = TLB_CONTROL_FLUSH_ASID;
Joerg Roedel06e78522020-06-25 10:03:23 +02002298 vmcb_mark_dirty(svm->vmcb, VMCB_ASID);
Joerg Roedeleaf78262020-03-24 10:41:54 +01002299}
Tom Lendacky291bd202020-12-10 11:09:47 -06002300
Tom Lendacky8f423a82020-12-10 11:09:53 -06002301#define GHCB_SCRATCH_AREA_LIMIT (16ULL * PAGE_SIZE)
2302static bool setup_vmgexit_scratch(struct vcpu_svm *svm, bool sync, u64 len)
2303{
2304 struct vmcb_control_area *control = &svm->vmcb->control;
2305 struct ghcb *ghcb = svm->ghcb;
2306 u64 ghcb_scratch_beg, ghcb_scratch_end;
2307 u64 scratch_gpa_beg, scratch_gpa_end;
2308 void *scratch_va;
2309
2310 scratch_gpa_beg = ghcb_get_sw_scratch(ghcb);
2311 if (!scratch_gpa_beg) {
2312 pr_err("vmgexit: scratch gpa not provided\n");
2313 return false;
2314 }
2315
2316 scratch_gpa_end = scratch_gpa_beg + len;
2317 if (scratch_gpa_end < scratch_gpa_beg) {
2318 pr_err("vmgexit: scratch length (%#llx) not valid for scratch address (%#llx)\n",
2319 len, scratch_gpa_beg);
2320 return false;
2321 }
2322
2323 if ((scratch_gpa_beg & PAGE_MASK) == control->ghcb_gpa) {
2324 /* Scratch area begins within GHCB */
2325 ghcb_scratch_beg = control->ghcb_gpa +
2326 offsetof(struct ghcb, shared_buffer);
2327 ghcb_scratch_end = control->ghcb_gpa +
2328 offsetof(struct ghcb, reserved_1);
2329
2330 /*
2331 * If the scratch area begins within the GHCB, it must be
2332 * completely contained in the GHCB shared buffer area.
2333 */
2334 if (scratch_gpa_beg < ghcb_scratch_beg ||
2335 scratch_gpa_end > ghcb_scratch_end) {
2336 pr_err("vmgexit: scratch area is outside of GHCB shared buffer area (%#llx - %#llx)\n",
2337 scratch_gpa_beg, scratch_gpa_end);
2338 return false;
2339 }
2340
2341 scratch_va = (void *)svm->ghcb;
2342 scratch_va += (scratch_gpa_beg - control->ghcb_gpa);
2343 } else {
2344 /*
2345 * The guest memory must be read into a kernel buffer, so
2346 * limit the size
2347 */
2348 if (len > GHCB_SCRATCH_AREA_LIMIT) {
2349 pr_err("vmgexit: scratch area exceeds KVM limits (%#llx requested, %#llx limit)\n",
2350 len, GHCB_SCRATCH_AREA_LIMIT);
2351 return false;
2352 }
Sean Christophersoneba04b22021-03-30 19:30:25 -07002353 scratch_va = kzalloc(len, GFP_KERNEL_ACCOUNT);
Tom Lendacky8f423a82020-12-10 11:09:53 -06002354 if (!scratch_va)
2355 return false;
2356
2357 if (kvm_read_guest(svm->vcpu.kvm, scratch_gpa_beg, scratch_va, len)) {
2358 /* Unable to copy scratch area from guest */
2359 pr_err("vmgexit: kvm_read_guest for scratch area failed\n");
2360
2361 kfree(scratch_va);
2362 return false;
2363 }
2364
2365 /*
2366 * The scratch area is outside the GHCB. The operation will
2367 * dictate whether the buffer needs to be synced before running
2368 * the vCPU next time (i.e. a read was requested so the data
2369 * must be written back to the guest memory).
2370 */
2371 svm->ghcb_sa_sync = sync;
2372 svm->ghcb_sa_free = true;
2373 }
2374
2375 svm->ghcb_sa = scratch_va;
2376 svm->ghcb_sa_len = len;
2377
2378 return true;
2379}
2380
Tom Lendackyd3694662020-12-10 11:09:50 -06002381static void set_ghcb_msr_bits(struct vcpu_svm *svm, u64 value, u64 mask,
2382 unsigned int pos)
2383{
2384 svm->vmcb->control.ghcb_gpa &= ~(mask << pos);
2385 svm->vmcb->control.ghcb_gpa |= (value & mask) << pos;
2386}
2387
2388static u64 get_ghcb_msr_bits(struct vcpu_svm *svm, u64 mask, unsigned int pos)
2389{
2390 return (svm->vmcb->control.ghcb_gpa >> pos) & mask;
2391}
2392
Tom Lendacky1edc1452020-12-10 11:09:49 -06002393static void set_ghcb_msr(struct vcpu_svm *svm, u64 value)
2394{
2395 svm->vmcb->control.ghcb_gpa = value;
2396}
2397
Tom Lendacky291bd202020-12-10 11:09:47 -06002398static int sev_handle_vmgexit_msr_protocol(struct vcpu_svm *svm)
2399{
Tom Lendacky1edc1452020-12-10 11:09:49 -06002400 struct vmcb_control_area *control = &svm->vmcb->control;
Tom Lendackyd3694662020-12-10 11:09:50 -06002401 struct kvm_vcpu *vcpu = &svm->vcpu;
Tom Lendacky1edc1452020-12-10 11:09:49 -06002402 u64 ghcb_info;
Tom Lendackyd3694662020-12-10 11:09:50 -06002403 int ret = 1;
Tom Lendacky1edc1452020-12-10 11:09:49 -06002404
2405 ghcb_info = control->ghcb_gpa & GHCB_MSR_INFO_MASK;
2406
Tom Lendacky59e38b52020-12-10 11:09:52 -06002407 trace_kvm_vmgexit_msr_protocol_enter(svm->vcpu.vcpu_id,
2408 control->ghcb_gpa);
2409
Tom Lendacky1edc1452020-12-10 11:09:49 -06002410 switch (ghcb_info) {
2411 case GHCB_MSR_SEV_INFO_REQ:
2412 set_ghcb_msr(svm, GHCB_MSR_SEV_INFO(GHCB_VERSION_MAX,
2413 GHCB_VERSION_MIN,
2414 sev_enc_bit));
2415 break;
Tom Lendackyd3694662020-12-10 11:09:50 -06002416 case GHCB_MSR_CPUID_REQ: {
2417 u64 cpuid_fn, cpuid_reg, cpuid_value;
2418
2419 cpuid_fn = get_ghcb_msr_bits(svm,
2420 GHCB_MSR_CPUID_FUNC_MASK,
2421 GHCB_MSR_CPUID_FUNC_POS);
2422
2423 /* Initialize the registers needed by the CPUID intercept */
2424 vcpu->arch.regs[VCPU_REGS_RAX] = cpuid_fn;
2425 vcpu->arch.regs[VCPU_REGS_RCX] = 0;
2426
Paolo Bonzini63129752021-03-02 14:40:39 -05002427 ret = svm_invoke_exit_handler(vcpu, SVM_EXIT_CPUID);
Tom Lendackyd3694662020-12-10 11:09:50 -06002428 if (!ret) {
2429 ret = -EINVAL;
2430 break;
2431 }
2432
2433 cpuid_reg = get_ghcb_msr_bits(svm,
2434 GHCB_MSR_CPUID_REG_MASK,
2435 GHCB_MSR_CPUID_REG_POS);
2436 if (cpuid_reg == 0)
2437 cpuid_value = vcpu->arch.regs[VCPU_REGS_RAX];
2438 else if (cpuid_reg == 1)
2439 cpuid_value = vcpu->arch.regs[VCPU_REGS_RBX];
2440 else if (cpuid_reg == 2)
2441 cpuid_value = vcpu->arch.regs[VCPU_REGS_RCX];
2442 else
2443 cpuid_value = vcpu->arch.regs[VCPU_REGS_RDX];
2444
2445 set_ghcb_msr_bits(svm, cpuid_value,
2446 GHCB_MSR_CPUID_VALUE_MASK,
2447 GHCB_MSR_CPUID_VALUE_POS);
2448
2449 set_ghcb_msr_bits(svm, GHCB_MSR_CPUID_RESP,
2450 GHCB_MSR_INFO_MASK,
2451 GHCB_MSR_INFO_POS);
2452 break;
2453 }
Tom Lendackye1d71112020-12-10 11:09:51 -06002454 case GHCB_MSR_TERM_REQ: {
2455 u64 reason_set, reason_code;
2456
2457 reason_set = get_ghcb_msr_bits(svm,
2458 GHCB_MSR_TERM_REASON_SET_MASK,
2459 GHCB_MSR_TERM_REASON_SET_POS);
2460 reason_code = get_ghcb_msr_bits(svm,
2461 GHCB_MSR_TERM_REASON_MASK,
2462 GHCB_MSR_TERM_REASON_POS);
2463 pr_info("SEV-ES guest requested termination: %#llx:%#llx\n",
2464 reason_set, reason_code);
2465 fallthrough;
2466 }
Tom Lendacky1edc1452020-12-10 11:09:49 -06002467 default:
Tom Lendackyd3694662020-12-10 11:09:50 -06002468 ret = -EINVAL;
Tom Lendacky1edc1452020-12-10 11:09:49 -06002469 }
2470
Tom Lendacky59e38b52020-12-10 11:09:52 -06002471 trace_kvm_vmgexit_msr_protocol_exit(svm->vcpu.vcpu_id,
2472 control->ghcb_gpa, ret);
2473
Tom Lendackyd3694662020-12-10 11:09:50 -06002474 return ret;
Tom Lendacky291bd202020-12-10 11:09:47 -06002475}
2476
Paolo Bonzini63129752021-03-02 14:40:39 -05002477int sev_handle_vmgexit(struct kvm_vcpu *vcpu)
Tom Lendacky291bd202020-12-10 11:09:47 -06002478{
Paolo Bonzini63129752021-03-02 14:40:39 -05002479 struct vcpu_svm *svm = to_svm(vcpu);
Tom Lendacky291bd202020-12-10 11:09:47 -06002480 struct vmcb_control_area *control = &svm->vmcb->control;
2481 u64 ghcb_gpa, exit_code;
2482 struct ghcb *ghcb;
2483 int ret;
2484
2485 /* Validate the GHCB */
2486 ghcb_gpa = control->ghcb_gpa;
2487 if (ghcb_gpa & GHCB_MSR_INFO_MASK)
2488 return sev_handle_vmgexit_msr_protocol(svm);
2489
2490 if (!ghcb_gpa) {
Paolo Bonzini63129752021-03-02 14:40:39 -05002491 vcpu_unimpl(vcpu, "vmgexit: GHCB gpa is not set\n");
Tom Lendacky291bd202020-12-10 11:09:47 -06002492 return -EINVAL;
2493 }
2494
Paolo Bonzini63129752021-03-02 14:40:39 -05002495 if (kvm_vcpu_map(vcpu, ghcb_gpa >> PAGE_SHIFT, &svm->ghcb_map)) {
Tom Lendacky291bd202020-12-10 11:09:47 -06002496 /* Unable to map GHCB from guest */
Paolo Bonzini63129752021-03-02 14:40:39 -05002497 vcpu_unimpl(vcpu, "vmgexit: error mapping GHCB [%#llx] from guest\n",
Tom Lendacky291bd202020-12-10 11:09:47 -06002498 ghcb_gpa);
2499 return -EINVAL;
2500 }
2501
2502 svm->ghcb = svm->ghcb_map.hva;
2503 ghcb = svm->ghcb_map.hva;
2504
Paolo Bonzini63129752021-03-02 14:40:39 -05002505 trace_kvm_vmgexit_enter(vcpu->vcpu_id, ghcb);
Tom Lendackyd523ab6b2020-12-10 11:09:48 -06002506
Tom Lendacky291bd202020-12-10 11:09:47 -06002507 exit_code = ghcb_get_sw_exit_code(ghcb);
2508
2509 ret = sev_es_validate_vmgexit(svm);
2510 if (ret)
2511 return ret;
2512
2513 sev_es_sync_from_ghcb(svm);
2514 ghcb_set_sw_exit_info_1(ghcb, 0);
2515 ghcb_set_sw_exit_info_2(ghcb, 0);
2516
2517 ret = -EINVAL;
2518 switch (exit_code) {
Tom Lendacky8f423a82020-12-10 11:09:53 -06002519 case SVM_VMGEXIT_MMIO_READ:
2520 if (!setup_vmgexit_scratch(svm, true, control->exit_info_2))
2521 break;
2522
Paolo Bonzini63129752021-03-02 14:40:39 -05002523 ret = kvm_sev_es_mmio_read(vcpu,
Tom Lendacky8f423a82020-12-10 11:09:53 -06002524 control->exit_info_1,
2525 control->exit_info_2,
2526 svm->ghcb_sa);
2527 break;
2528 case SVM_VMGEXIT_MMIO_WRITE:
2529 if (!setup_vmgexit_scratch(svm, false, control->exit_info_2))
2530 break;
2531
Paolo Bonzini63129752021-03-02 14:40:39 -05002532 ret = kvm_sev_es_mmio_write(vcpu,
Tom Lendacky8f423a82020-12-10 11:09:53 -06002533 control->exit_info_1,
2534 control->exit_info_2,
2535 svm->ghcb_sa);
2536 break;
Tom Lendacky4444dfe2020-12-14 11:16:03 -05002537 case SVM_VMGEXIT_NMI_COMPLETE:
Paolo Bonzini63129752021-03-02 14:40:39 -05002538 ret = svm_invoke_exit_handler(vcpu, SVM_EXIT_IRET);
Tom Lendacky4444dfe2020-12-14 11:16:03 -05002539 break;
Tom Lendacky647daca2021-01-04 14:20:01 -06002540 case SVM_VMGEXIT_AP_HLT_LOOP:
Paolo Bonzini63129752021-03-02 14:40:39 -05002541 ret = kvm_emulate_ap_reset_hold(vcpu);
Tom Lendacky647daca2021-01-04 14:20:01 -06002542 break;
Tom Lendacky8640ca52020-12-15 12:44:07 -05002543 case SVM_VMGEXIT_AP_JUMP_TABLE: {
Paolo Bonzini63129752021-03-02 14:40:39 -05002544 struct kvm_sev_info *sev = &to_kvm_svm(vcpu->kvm)->sev_info;
Tom Lendacky8640ca52020-12-15 12:44:07 -05002545
2546 switch (control->exit_info_1) {
2547 case 0:
2548 /* Set AP jump table address */
2549 sev->ap_jump_table = control->exit_info_2;
2550 break;
2551 case 1:
2552 /* Get AP jump table address */
2553 ghcb_set_sw_exit_info_2(ghcb, sev->ap_jump_table);
2554 break;
2555 default:
2556 pr_err("svm: vmgexit: unsupported AP jump table request - exit_info_1=%#llx\n",
2557 control->exit_info_1);
2558 ghcb_set_sw_exit_info_1(ghcb, 1);
2559 ghcb_set_sw_exit_info_2(ghcb,
2560 X86_TRAP_UD |
2561 SVM_EVTINJ_TYPE_EXEPT |
2562 SVM_EVTINJ_VALID);
2563 }
2564
2565 ret = 1;
2566 break;
2567 }
Tom Lendacky291bd202020-12-10 11:09:47 -06002568 case SVM_VMGEXIT_UNSUPPORTED_EVENT:
Paolo Bonzini63129752021-03-02 14:40:39 -05002569 vcpu_unimpl(vcpu,
Tom Lendacky291bd202020-12-10 11:09:47 -06002570 "vmgexit: unsupported event - exit_info_1=%#llx, exit_info_2=%#llx\n",
2571 control->exit_info_1, control->exit_info_2);
2572 break;
2573 default:
Paolo Bonzini63129752021-03-02 14:40:39 -05002574 ret = svm_invoke_exit_handler(vcpu, exit_code);
Tom Lendacky291bd202020-12-10 11:09:47 -06002575 }
2576
2577 return ret;
2578}
Tom Lendacky7ed9abf2020-12-10 11:09:54 -06002579
2580int sev_es_string_io(struct vcpu_svm *svm, int size, unsigned int port, int in)
2581{
2582 if (!setup_vmgexit_scratch(svm, in, svm->vmcb->control.exit_info_2))
2583 return -EINVAL;
2584
2585 return kvm_sev_es_string_io(&svm->vcpu, size, port,
2586 svm->ghcb_sa, svm->ghcb_sa_len, in);
2587}
Tom Lendacky376c6d22020-12-10 11:10:06 -06002588
2589void sev_es_init_vmcb(struct vcpu_svm *svm)
2590{
2591 struct kvm_vcpu *vcpu = &svm->vcpu;
2592
2593 svm->vmcb->control.nested_ctl |= SVM_NESTED_CTL_SEV_ES_ENABLE;
2594 svm->vmcb->control.virt_ext |= LBR_CTL_ENABLE_MASK;
2595
2596 /*
2597 * An SEV-ES guest requires a VMSA area that is a separate from the
2598 * VMCB page. Do not include the encryption mask on the VMSA physical
2599 * address since hardware will access it using the guest key.
2600 */
2601 svm->vmcb->control.vmsa_pa = __pa(svm->vmsa);
2602
2603 /* Can't intercept CR register access, HV can't modify CR registers */
2604 svm_clr_intercept(svm, INTERCEPT_CR0_READ);
2605 svm_clr_intercept(svm, INTERCEPT_CR4_READ);
2606 svm_clr_intercept(svm, INTERCEPT_CR8_READ);
2607 svm_clr_intercept(svm, INTERCEPT_CR0_WRITE);
2608 svm_clr_intercept(svm, INTERCEPT_CR4_WRITE);
2609 svm_clr_intercept(svm, INTERCEPT_CR8_WRITE);
2610
2611 svm_clr_intercept(svm, INTERCEPT_SELECTIVE_CR0);
2612
2613 /* Track EFER/CR register changes */
2614 svm_set_intercept(svm, TRAP_EFER_WRITE);
2615 svm_set_intercept(svm, TRAP_CR0_WRITE);
2616 svm_set_intercept(svm, TRAP_CR4_WRITE);
2617 svm_set_intercept(svm, TRAP_CR8_WRITE);
2618
2619 /* No support for enable_vmware_backdoor */
2620 clr_exception_intercept(svm, GP_VECTOR);
2621
2622 /* Can't intercept XSETBV, HV can't modify XCR0 directly */
2623 svm_clr_intercept(svm, INTERCEPT_XSETBV);
2624
2625 /* Clear intercepts on selected MSRs */
2626 set_msr_interception(vcpu, svm->msrpm, MSR_EFER, 1, 1);
2627 set_msr_interception(vcpu, svm->msrpm, MSR_IA32_CR_PAT, 1, 1);
2628 set_msr_interception(vcpu, svm->msrpm, MSR_IA32_LASTBRANCHFROMIP, 1, 1);
2629 set_msr_interception(vcpu, svm->msrpm, MSR_IA32_LASTBRANCHTOIP, 1, 1);
2630 set_msr_interception(vcpu, svm->msrpm, MSR_IA32_LASTINTFROMIP, 1, 1);
2631 set_msr_interception(vcpu, svm->msrpm, MSR_IA32_LASTINTTOIP, 1, 1);
2632}
2633
Sean Christopherson9ebe5302021-09-20 17:03:02 -07002634void sev_es_vcpu_reset(struct vcpu_svm *svm)
Tom Lendacky376c6d22020-12-10 11:10:06 -06002635{
2636 /*
Sean Christopherson9ebe5302021-09-20 17:03:02 -07002637 * Set the GHCB MSR value as per the GHCB specification when emulating
2638 * vCPU RESET for an SEV-ES guest.
Tom Lendacky376c6d22020-12-10 11:10:06 -06002639 */
2640 set_ghcb_msr(svm, GHCB_MSR_SEV_INFO(GHCB_VERSION_MAX,
2641 GHCB_VERSION_MIN,
2642 sev_enc_bit));
2643}
Tom Lendacky86137772020-12-10 11:10:07 -06002644
Michael Rotha7fc06d2021-02-02 13:01:26 -06002645void sev_es_prepare_guest_switch(struct vcpu_svm *svm, unsigned int cpu)
Tom Lendacky86137772020-12-10 11:10:07 -06002646{
2647 struct svm_cpu_data *sd = per_cpu(svm_data, cpu);
2648 struct vmcb_save_area *hostsa;
Tom Lendacky86137772020-12-10 11:10:07 -06002649
2650 /*
2651 * As an SEV-ES guest, hardware will restore the host state on VMEXIT,
2652 * of which one step is to perform a VMLOAD. Since hardware does not
2653 * perform a VMSAVE on VMRUN, the host savearea must be updated.
2654 */
Sean Christopherson35a78312020-12-30 16:27:00 -08002655 vmsave(__sme_page_pa(sd->save_area));
Tom Lendacky86137772020-12-10 11:10:07 -06002656
Tom Lendacky86137772020-12-10 11:10:07 -06002657 /* XCR0 is restored on VMEXIT, save the current host value */
2658 hostsa = (struct vmcb_save_area *)(page_address(sd->save_area) + 0x400);
2659 hostsa->xcr0 = xgetbv(XCR_XFEATURE_ENABLED_MASK);
2660
Ingo Molnard9f6e122021-03-18 15:28:01 +01002661 /* PKRU is restored on VMEXIT, save the current host value */
Tom Lendacky86137772020-12-10 11:10:07 -06002662 hostsa->pkru = read_pkru();
2663
2664 /* MSR_IA32_XSS is restored on VMEXIT, save the currnet host value */
2665 hostsa->xss = host_xss;
2666}
2667
Tom Lendacky647daca2021-01-04 14:20:01 -06002668void sev_vcpu_deliver_sipi_vector(struct kvm_vcpu *vcpu, u8 vector)
2669{
2670 struct vcpu_svm *svm = to_svm(vcpu);
2671
2672 /* First SIPI: Use the values as initially set by the VMM */
2673 if (!svm->received_first_sipi) {
2674 svm->received_first_sipi = true;
2675 return;
2676 }
2677
2678 /*
2679 * Subsequent SIPI: Return from an AP Reset Hold VMGEXIT, where
2680 * the guest will set the CS and RIP. Set SW_EXIT_INFO_2 to a
2681 * non-zero value.
2682 */
Tom Lendackya3ba26e2021-04-09 09:38:42 -05002683 if (!svm->ghcb)
2684 return;
2685
Tom Lendacky647daca2021-01-04 14:20:01 -06002686 ghcb_set_sw_exit_info_2(svm->ghcb, 1);
2687}