blob: 6a22798eaaee9d50ed2f49137d3a56fe67316fda [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>
Joerg Roedeleaf78262020-03-24 10:41:54 +010020
Dave Hansen784a46612021-06-23 14:02:05 +020021#include <asm/pkru.h>
Tom Lendacky8640ca52020-12-15 12:44:07 -050022#include <asm/trapnr.h>
Thomas Gleixnerd9d005f2021-10-15 03:16:31 +020023#include <asm/fpu/xcr.h>
Tom Lendacky8640ca52020-12-15 12:44:07 -050024
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
Paolo Bonzini91b692a2021-11-11 10:02:26 -0500123static int sev_misc_cg_try_charge(struct kvm_sev_info *sev)
124{
125 enum misc_res_type type = sev->es_active ? MISC_CG_RES_SEV_ES : MISC_CG_RES_SEV;
126 return misc_cg_try_charge(type, sev->misc_cg, 1);
127}
128
129static void sev_misc_cg_uncharge(struct kvm_sev_info *sev)
130{
131 enum misc_res_type type = sev->es_active ? MISC_CG_RES_SEV_ES : MISC_CG_RES_SEV;
132 misc_cg_uncharge(type, sev->misc_cg, 1);
133}
134
Tom Lendacky80675b32020-12-10 11:10:05 -0600135static int sev_asid_new(struct kvm_sev_info *sev)
Joerg Roedeleaf78262020-03-24 10:41:54 +0100136{
Mingwei Zhangbb2baeb2021-08-02 11:09:03 -0700137 int asid, min_asid, max_asid, ret;
Joerg Roedeleaf78262020-03-24 10:41:54 +0100138 bool retry = true;
Vipin Sharma7aef27f2021-03-29 21:42:06 -0700139
Vipin Sharma7aef27f2021-03-29 21:42:06 -0700140 WARN_ON(sev->misc_cg);
141 sev->misc_cg = get_current_misc_cg();
Paolo Bonzini91b692a2021-11-11 10:02:26 -0500142 ret = sev_misc_cg_try_charge(sev);
Vipin Sharma7aef27f2021-03-29 21:42:06 -0700143 if (ret) {
144 put_misc_cg(sev->misc_cg);
145 sev->misc_cg = NULL;
146 return ret;
147 }
Joerg Roedeleaf78262020-03-24 10:41:54 +0100148
149 mutex_lock(&sev_bitmap_lock);
150
151 /*
Tom Lendacky80675b32020-12-10 11:10:05 -0600152 * SEV-enabled guests must use asid from min_sev_asid to max_sev_asid.
153 * SEV-ES-enabled guest can use from 1 to min_sev_asid - 1.
Joerg Roedeleaf78262020-03-24 10:41:54 +0100154 */
Mingwei Zhangbb2baeb2021-08-02 11:09:03 -0700155 min_asid = sev->es_active ? 1 : min_sev_asid;
Tom Lendacky80675b32020-12-10 11:10:05 -0600156 max_asid = sev->es_active ? min_sev_asid - 1 : max_sev_asid;
Joerg Roedeleaf78262020-03-24 10:41:54 +0100157again:
Mingwei Zhangbb2baeb2021-08-02 11:09:03 -0700158 asid = find_next_zero_bit(sev_asid_bitmap, max_asid + 1, min_asid);
159 if (asid > max_asid) {
Tom Lendacky80675b32020-12-10 11:10:05 -0600160 if (retry && __sev_recycle_asids(min_asid, max_asid)) {
Joerg Roedeleaf78262020-03-24 10:41:54 +0100161 retry = false;
162 goto again;
163 }
164 mutex_unlock(&sev_bitmap_lock);
Vipin Sharma7aef27f2021-03-29 21:42:06 -0700165 ret = -EBUSY;
166 goto e_uncharge;
Joerg Roedeleaf78262020-03-24 10:41:54 +0100167 }
168
Mingwei Zhangbb2baeb2021-08-02 11:09:03 -0700169 __set_bit(asid, sev_asid_bitmap);
Joerg Roedeleaf78262020-03-24 10:41:54 +0100170
171 mutex_unlock(&sev_bitmap_lock);
172
Mingwei Zhangbb2baeb2021-08-02 11:09:03 -0700173 return asid;
Vipin Sharma7aef27f2021-03-29 21:42:06 -0700174e_uncharge:
Paolo Bonzini91b692a2021-11-11 10:02:26 -0500175 sev_misc_cg_uncharge(sev);
Vipin Sharma7aef27f2021-03-29 21:42:06 -0700176 put_misc_cg(sev->misc_cg);
177 sev->misc_cg = NULL;
178 return ret;
Joerg Roedeleaf78262020-03-24 10:41:54 +0100179}
180
181static int sev_get_asid(struct kvm *kvm)
182{
183 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
184
185 return sev->asid;
186}
187
Vipin Sharma7aef27f2021-03-29 21:42:06 -0700188static void sev_asid_free(struct kvm_sev_info *sev)
Joerg Roedeleaf78262020-03-24 10:41:54 +0100189{
190 struct svm_cpu_data *sd;
Mingwei Zhangbb2baeb2021-08-02 11:09:03 -0700191 int cpu;
Joerg Roedeleaf78262020-03-24 10:41:54 +0100192
193 mutex_lock(&sev_bitmap_lock);
194
Mingwei Zhangbb2baeb2021-08-02 11:09:03 -0700195 __set_bit(sev->asid, sev_reclaim_asid_bitmap);
Joerg Roedeleaf78262020-03-24 10:41:54 +0100196
197 for_each_possible_cpu(cpu) {
198 sd = per_cpu(svm_data, cpu);
Sean Christopherson179c6c22021-08-03 09:27:46 -0700199 sd->sev_vmcbs[sev->asid] = NULL;
Joerg Roedeleaf78262020-03-24 10:41:54 +0100200 }
201
202 mutex_unlock(&sev_bitmap_lock);
Vipin Sharma7aef27f2021-03-29 21:42:06 -0700203
Paolo Bonzini91b692a2021-11-11 10:02:26 -0500204 sev_misc_cg_uncharge(sev);
Vipin Sharma7aef27f2021-03-29 21:42:06 -0700205 put_misc_cg(sev->misc_cg);
206 sev->misc_cg = NULL;
Joerg Roedeleaf78262020-03-24 10:41:54 +0100207}
208
Alper Gun934002c2021-06-10 17:46:04 +0000209static void sev_decommission(unsigned int handle)
Joerg Roedeleaf78262020-03-24 10:41:54 +0100210{
Sean Christopherson238eca82021-04-06 15:49:52 -0700211 struct sev_data_decommission decommission;
Alper Gun934002c2021-06-10 17:46:04 +0000212
213 if (!handle)
214 return;
215
216 decommission.handle = handle;
217 sev_guest_decommission(&decommission, NULL);
218}
219
220static void sev_unbind_asid(struct kvm *kvm, unsigned int handle)
221{
Sean Christopherson238eca82021-04-06 15:49:52 -0700222 struct sev_data_deactivate deactivate;
Joerg Roedeleaf78262020-03-24 10:41:54 +0100223
224 if (!handle)
225 return;
226
Sean Christopherson238eca82021-04-06 15:49:52 -0700227 deactivate.handle = handle;
Joerg Roedeleaf78262020-03-24 10:41:54 +0100228
229 /* Guard DEACTIVATE against WBINVD/DF_FLUSH used in ASID recycling */
230 down_read(&sev_deactivate_lock);
Sean Christopherson238eca82021-04-06 15:49:52 -0700231 sev_guest_deactivate(&deactivate, NULL);
Joerg Roedeleaf78262020-03-24 10:41:54 +0100232 up_read(&sev_deactivate_lock);
233
Alper Gun934002c2021-06-10 17:46:04 +0000234 sev_decommission(handle);
Joerg Roedeleaf78262020-03-24 10:41:54 +0100235}
236
237static int sev_guest_init(struct kvm *kvm, struct kvm_sev_cmd *argp)
238{
239 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
240 int asid, ret;
241
Sean Christopherson87279062021-03-30 20:19:36 -0700242 if (kvm->created_vcpus)
243 return -EINVAL;
244
Joerg Roedeleaf78262020-03-24 10:41:54 +0100245 ret = -EBUSY;
246 if (unlikely(sev->active))
247 return ret;
248
Sean Christophersona41fb262021-11-09 21:50:58 +0000249 sev->active = true;
250 sev->es_active = argp->id == KVM_SEV_ES_INIT;
Tom Lendacky80675b32020-12-10 11:10:05 -0600251 asid = sev_asid_new(sev);
Joerg Roedeleaf78262020-03-24 10:41:54 +0100252 if (asid < 0)
Paolo Bonzinifd49e8e2021-04-22 02:39:48 -0400253 goto e_no_asid;
Vipin Sharma7aef27f2021-03-29 21:42:06 -0700254 sev->asid = asid;
Joerg Roedeleaf78262020-03-24 10:41:54 +0100255
256 ret = sev_platform_init(&argp->error);
257 if (ret)
258 goto e_free;
259
Joerg Roedeleaf78262020-03-24 10:41:54 +0100260 INIT_LIST_HEAD(&sev->regions_list);
261
262 return 0;
263
264e_free:
Vipin Sharma7aef27f2021-03-29 21:42:06 -0700265 sev_asid_free(sev);
266 sev->asid = 0;
Paolo Bonzinifd49e8e2021-04-22 02:39:48 -0400267e_no_asid:
268 sev->es_active = false;
Sean Christophersona41fb262021-11-09 21:50:58 +0000269 sev->active = false;
Joerg Roedeleaf78262020-03-24 10:41:54 +0100270 return ret;
271}
272
273static int sev_bind_asid(struct kvm *kvm, unsigned int handle, int *error)
274{
Sean Christopherson238eca82021-04-06 15:49:52 -0700275 struct sev_data_activate activate;
Joerg Roedeleaf78262020-03-24 10:41:54 +0100276 int asid = sev_get_asid(kvm);
277 int ret;
278
Joerg Roedeleaf78262020-03-24 10:41:54 +0100279 /* activate ASID on the given handle */
Sean Christopherson238eca82021-04-06 15:49:52 -0700280 activate.handle = handle;
281 activate.asid = asid;
282 ret = sev_guest_activate(&activate, error);
Joerg Roedeleaf78262020-03-24 10:41:54 +0100283
284 return ret;
285}
286
287static int __sev_issue_cmd(int fd, int id, void *data, int *error)
288{
289 struct fd f;
290 int ret;
291
292 f = fdget(fd);
293 if (!f.file)
294 return -EBADF;
295
296 ret = sev_issue_cmd_external_user(f.file, id, data, error);
297
298 fdput(f);
299 return ret;
300}
301
302static int sev_issue_cmd(struct kvm *kvm, int id, void *data, int *error)
303{
304 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
305
306 return __sev_issue_cmd(sev->fd, id, data, error);
307}
308
309static int sev_launch_start(struct kvm *kvm, struct kvm_sev_cmd *argp)
310{
311 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
Sean Christopherson238eca82021-04-06 15:49:52 -0700312 struct sev_data_launch_start start;
Joerg Roedeleaf78262020-03-24 10:41:54 +0100313 struct kvm_sev_launch_start params;
314 void *dh_blob, *session_blob;
315 int *error = &argp->error;
316 int ret;
317
318 if (!sev_guest(kvm))
319 return -ENOTTY;
320
321 if (copy_from_user(&params, (void __user *)(uintptr_t)argp->data, sizeof(params)))
322 return -EFAULT;
323
Sean Christopherson238eca82021-04-06 15:49:52 -0700324 memset(&start, 0, sizeof(start));
Joerg Roedeleaf78262020-03-24 10:41:54 +0100325
326 dh_blob = NULL;
327 if (params.dh_uaddr) {
328 dh_blob = psp_copy_user_blob(params.dh_uaddr, params.dh_len);
Sean Christopherson238eca82021-04-06 15:49:52 -0700329 if (IS_ERR(dh_blob))
330 return PTR_ERR(dh_blob);
Joerg Roedeleaf78262020-03-24 10:41:54 +0100331
Sean Christopherson238eca82021-04-06 15:49:52 -0700332 start.dh_cert_address = __sme_set(__pa(dh_blob));
333 start.dh_cert_len = params.dh_len;
Joerg Roedeleaf78262020-03-24 10:41:54 +0100334 }
335
336 session_blob = NULL;
337 if (params.session_uaddr) {
338 session_blob = psp_copy_user_blob(params.session_uaddr, params.session_len);
339 if (IS_ERR(session_blob)) {
340 ret = PTR_ERR(session_blob);
341 goto e_free_dh;
342 }
343
Sean Christopherson238eca82021-04-06 15:49:52 -0700344 start.session_address = __sme_set(__pa(session_blob));
345 start.session_len = params.session_len;
Joerg Roedeleaf78262020-03-24 10:41:54 +0100346 }
347
Sean Christopherson238eca82021-04-06 15:49:52 -0700348 start.handle = params.handle;
349 start.policy = params.policy;
Joerg Roedeleaf78262020-03-24 10:41:54 +0100350
351 /* create memory encryption context */
Sean Christopherson238eca82021-04-06 15:49:52 -0700352 ret = __sev_issue_cmd(argp->sev_fd, SEV_CMD_LAUNCH_START, &start, error);
Joerg Roedeleaf78262020-03-24 10:41:54 +0100353 if (ret)
354 goto e_free_session;
355
356 /* Bind ASID to this guest */
Sean Christopherson238eca82021-04-06 15:49:52 -0700357 ret = sev_bind_asid(kvm, start.handle, error);
Alper Gun934002c2021-06-10 17:46:04 +0000358 if (ret) {
359 sev_decommission(start.handle);
Joerg Roedeleaf78262020-03-24 10:41:54 +0100360 goto e_free_session;
Alper Gun934002c2021-06-10 17:46:04 +0000361 }
Joerg Roedeleaf78262020-03-24 10:41:54 +0100362
363 /* return handle to userspace */
Sean Christopherson238eca82021-04-06 15:49:52 -0700364 params.handle = start.handle;
Joerg Roedeleaf78262020-03-24 10:41:54 +0100365 if (copy_to_user((void __user *)(uintptr_t)argp->data, &params, sizeof(params))) {
Sean Christopherson238eca82021-04-06 15:49:52 -0700366 sev_unbind_asid(kvm, start.handle);
Joerg Roedeleaf78262020-03-24 10:41:54 +0100367 ret = -EFAULT;
368 goto e_free_session;
369 }
370
Sean Christopherson238eca82021-04-06 15:49:52 -0700371 sev->handle = start.handle;
Joerg Roedeleaf78262020-03-24 10:41:54 +0100372 sev->fd = argp->sev_fd;
373
374e_free_session:
375 kfree(session_blob);
376e_free_dh:
377 kfree(dh_blob);
Joerg Roedeleaf78262020-03-24 10:41:54 +0100378 return ret;
379}
380
381static struct page **sev_pin_memory(struct kvm *kvm, unsigned long uaddr,
382 unsigned long ulen, unsigned long *n,
383 int write)
384{
385 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
John Hubbard78824fa2020-05-25 23:22:06 -0700386 unsigned long npages, size;
387 int npinned;
Joerg Roedeleaf78262020-03-24 10:41:54 +0100388 unsigned long locked, lock_limit;
389 struct page **pages;
390 unsigned long first, last;
Dan Carpenterff2bd9f2020-07-14 17:23:51 +0300391 int ret;
Joerg Roedeleaf78262020-03-24 10:41:54 +0100392
Peter Gonda19a23da2021-01-27 08:15:24 -0800393 lockdep_assert_held(&kvm->lock);
394
Joerg Roedeleaf78262020-03-24 10:41:54 +0100395 if (ulen == 0 || uaddr + ulen < uaddr)
Paolo Bonzinia8d908b2020-06-23 05:12:24 -0400396 return ERR_PTR(-EINVAL);
Joerg Roedeleaf78262020-03-24 10:41:54 +0100397
398 /* Calculate number of pages. */
399 first = (uaddr & PAGE_MASK) >> PAGE_SHIFT;
400 last = ((uaddr + ulen - 1) & PAGE_MASK) >> PAGE_SHIFT;
401 npages = (last - first + 1);
402
403 locked = sev->pages_locked + npages;
404 lock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
405 if (locked > lock_limit && !capable(CAP_IPC_LOCK)) {
406 pr_err("SEV: %lu locked pages exceed the lock limit of %lu.\n", locked, lock_limit);
Paolo Bonzinia8d908b2020-06-23 05:12:24 -0400407 return ERR_PTR(-ENOMEM);
Joerg Roedeleaf78262020-03-24 10:41:54 +0100408 }
409
John Hubbard78824fa2020-05-25 23:22:06 -0700410 if (WARN_ON_ONCE(npages > INT_MAX))
Paolo Bonzinia8d908b2020-06-23 05:12:24 -0400411 return ERR_PTR(-EINVAL);
John Hubbard78824fa2020-05-25 23:22:06 -0700412
Joerg Roedeleaf78262020-03-24 10:41:54 +0100413 /* Avoid using vmalloc for smaller buffers. */
414 size = npages * sizeof(struct page *);
415 if (size > PAGE_SIZE)
Christoph Hellwig88dca4c2020-06-01 21:51:40 -0700416 pages = __vmalloc(size, GFP_KERNEL_ACCOUNT | __GFP_ZERO);
Joerg Roedeleaf78262020-03-24 10:41:54 +0100417 else
418 pages = kmalloc(size, GFP_KERNEL_ACCOUNT);
419
420 if (!pages)
Paolo Bonzinia8d908b2020-06-23 05:12:24 -0400421 return ERR_PTR(-ENOMEM);
Joerg Roedeleaf78262020-03-24 10:41:54 +0100422
423 /* Pin the user virtual address. */
John Hubbarddc42c8a2020-05-25 23:22:07 -0700424 npinned = pin_user_pages_fast(uaddr, npages, write ? FOLL_WRITE : 0, pages);
Joerg Roedeleaf78262020-03-24 10:41:54 +0100425 if (npinned != npages) {
426 pr_err("SEV: Failure locking %lu pages.\n", npages);
Dan Carpenterff2bd9f2020-07-14 17:23:51 +0300427 ret = -ENOMEM;
Joerg Roedeleaf78262020-03-24 10:41:54 +0100428 goto err;
429 }
430
431 *n = npages;
432 sev->pages_locked = locked;
433
434 return pages;
435
436err:
Dan Carpenterff2bd9f2020-07-14 17:23:51 +0300437 if (npinned > 0)
John Hubbarddc42c8a2020-05-25 23:22:07 -0700438 unpin_user_pages(pages, npinned);
Joerg Roedeleaf78262020-03-24 10:41:54 +0100439
440 kvfree(pages);
Dan Carpenterff2bd9f2020-07-14 17:23:51 +0300441 return ERR_PTR(ret);
Joerg Roedeleaf78262020-03-24 10:41:54 +0100442}
443
444static void sev_unpin_memory(struct kvm *kvm, struct page **pages,
445 unsigned long npages)
446{
447 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
448
John Hubbarddc42c8a2020-05-25 23:22:07 -0700449 unpin_user_pages(pages, npages);
Joerg Roedeleaf78262020-03-24 10:41:54 +0100450 kvfree(pages);
451 sev->pages_locked -= npages;
452}
453
454static void sev_clflush_pages(struct page *pages[], unsigned long npages)
455{
456 uint8_t *page_virtual;
457 unsigned long i;
458
Krish Sadhukhane1ebb2b2020-09-17 21:20:38 +0000459 if (this_cpu_has(X86_FEATURE_SME_COHERENT) || npages == 0 ||
460 pages == NULL)
Joerg Roedeleaf78262020-03-24 10:41:54 +0100461 return;
462
463 for (i = 0; i < npages; i++) {
464 page_virtual = kmap_atomic(pages[i]);
465 clflush_cache_range(page_virtual, PAGE_SIZE);
466 kunmap_atomic(page_virtual);
467 }
468}
469
470static unsigned long get_num_contig_pages(unsigned long idx,
471 struct page **inpages, unsigned long npages)
472{
473 unsigned long paddr, next_paddr;
474 unsigned long i = idx + 1, pages = 1;
475
476 /* find the number of contiguous pages starting from idx */
477 paddr = __sme_page_pa(inpages[idx]);
478 while (i < npages) {
479 next_paddr = __sme_page_pa(inpages[i++]);
480 if ((paddr + PAGE_SIZE) == next_paddr) {
481 pages++;
482 paddr = next_paddr;
483 continue;
484 }
485 break;
486 }
487
488 return pages;
489}
490
491static int sev_launch_update_data(struct kvm *kvm, struct kvm_sev_cmd *argp)
492{
493 unsigned long vaddr, vaddr_end, next_vaddr, npages, pages, size, i;
494 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
495 struct kvm_sev_launch_update_data params;
Sean Christopherson238eca82021-04-06 15:49:52 -0700496 struct sev_data_launch_update_data data;
Joerg Roedeleaf78262020-03-24 10:41:54 +0100497 struct page **inpages;
498 int ret;
499
500 if (!sev_guest(kvm))
501 return -ENOTTY;
502
503 if (copy_from_user(&params, (void __user *)(uintptr_t)argp->data, sizeof(params)))
504 return -EFAULT;
505
Joerg Roedeleaf78262020-03-24 10:41:54 +0100506 vaddr = params.uaddr;
507 size = params.len;
508 vaddr_end = vaddr + size;
509
510 /* Lock the user memory. */
511 inpages = sev_pin_memory(kvm, vaddr, size, &npages, 1);
Sean Christopherson238eca82021-04-06 15:49:52 -0700512 if (IS_ERR(inpages))
513 return PTR_ERR(inpages);
Joerg Roedeleaf78262020-03-24 10:41:54 +0100514
515 /*
Paolo Bonzini14e3dd82020-09-23 13:01:33 -0400516 * Flush (on non-coherent CPUs) before LAUNCH_UPDATE encrypts pages in
517 * place; the cache may contain the data that was written unencrypted.
Joerg Roedeleaf78262020-03-24 10:41:54 +0100518 */
519 sev_clflush_pages(inpages, npages);
520
Sean Christopherson238eca82021-04-06 15:49:52 -0700521 data.reserved = 0;
522 data.handle = sev->handle;
523
Joerg Roedeleaf78262020-03-24 10:41:54 +0100524 for (i = 0; vaddr < vaddr_end; vaddr = next_vaddr, i += pages) {
525 int offset, len;
526
527 /*
528 * If the user buffer is not page-aligned, calculate the offset
529 * within the page.
530 */
531 offset = vaddr & (PAGE_SIZE - 1);
532
533 /* Calculate the number of pages that can be encrypted in one go. */
534 pages = get_num_contig_pages(i, inpages, npages);
535
536 len = min_t(size_t, ((pages * PAGE_SIZE) - offset), size);
537
Sean Christopherson238eca82021-04-06 15:49:52 -0700538 data.len = len;
539 data.address = __sme_page_pa(inpages[i]) + offset;
540 ret = sev_issue_cmd(kvm, SEV_CMD_LAUNCH_UPDATE_DATA, &data, &argp->error);
Joerg Roedeleaf78262020-03-24 10:41:54 +0100541 if (ret)
542 goto e_unpin;
543
544 size -= len;
545 next_vaddr = vaddr + len;
546 }
547
548e_unpin:
549 /* content of memory is updated, mark pages dirty */
550 for (i = 0; i < npages; i++) {
551 set_page_dirty_lock(inpages[i]);
552 mark_page_accessed(inpages[i]);
553 }
554 /* unlock the user pages */
555 sev_unpin_memory(kvm, inpages, npages);
Joerg Roedeleaf78262020-03-24 10:41:54 +0100556 return ret;
557}
558
Tom Lendackyad731092020-12-10 11:10:09 -0600559static int sev_es_sync_vmsa(struct vcpu_svm *svm)
560{
561 struct vmcb_save_area *save = &svm->vmcb->save;
562
563 /* Check some debug related fields before encrypting the VMSA */
564 if (svm->vcpu.guest_debug || (save->dr7 & ~DR7_FIXED_1))
565 return -EINVAL;
566
567 /* Sync registgers */
568 save->rax = svm->vcpu.arch.regs[VCPU_REGS_RAX];
569 save->rbx = svm->vcpu.arch.regs[VCPU_REGS_RBX];
570 save->rcx = svm->vcpu.arch.regs[VCPU_REGS_RCX];
571 save->rdx = svm->vcpu.arch.regs[VCPU_REGS_RDX];
572 save->rsp = svm->vcpu.arch.regs[VCPU_REGS_RSP];
573 save->rbp = svm->vcpu.arch.regs[VCPU_REGS_RBP];
574 save->rsi = svm->vcpu.arch.regs[VCPU_REGS_RSI];
575 save->rdi = svm->vcpu.arch.regs[VCPU_REGS_RDI];
Paolo Bonzinid45f89f2020-12-16 13:08:21 -0500576#ifdef CONFIG_X86_64
Tom Lendackyad731092020-12-10 11:10:09 -0600577 save->r8 = svm->vcpu.arch.regs[VCPU_REGS_R8];
578 save->r9 = svm->vcpu.arch.regs[VCPU_REGS_R9];
579 save->r10 = svm->vcpu.arch.regs[VCPU_REGS_R10];
580 save->r11 = svm->vcpu.arch.regs[VCPU_REGS_R11];
581 save->r12 = svm->vcpu.arch.regs[VCPU_REGS_R12];
582 save->r13 = svm->vcpu.arch.regs[VCPU_REGS_R13];
583 save->r14 = svm->vcpu.arch.regs[VCPU_REGS_R14];
584 save->r15 = svm->vcpu.arch.regs[VCPU_REGS_R15];
Paolo Bonzinid45f89f2020-12-16 13:08:21 -0500585#endif
Tom Lendackyad731092020-12-10 11:10:09 -0600586 save->rip = svm->vcpu.arch.regs[VCPU_REGS_RIP];
587
588 /* Sync some non-GPR registers before encrypting */
589 save->xcr0 = svm->vcpu.arch.xcr0;
590 save->pkru = svm->vcpu.arch.pkru;
591 save->xss = svm->vcpu.arch.ia32_xss;
Sean Christophersond0f9f822021-07-13 09:33:10 -0700592 save->dr6 = svm->vcpu.arch.dr6;
Tom Lendackyad731092020-12-10 11:10:09 -0600593
594 /*
595 * SEV-ES will use a VMSA that is pointed to by the VMCB, not
596 * the traditional VMSA that is part of the VMCB. Copy the
597 * traditional VMSA as it has been built so far (in prep
598 * for LAUNCH_UPDATE_VMSA) to be the initial SEV-ES state.
599 */
Peter Gondab67a4cc2021-10-21 10:42:59 -0700600 memcpy(svm->sev_es.vmsa, save, sizeof(*save));
Tom Lendackyad731092020-12-10 11:10:09 -0600601
602 return 0;
603}
604
Peter Gondabb18a672021-09-15 10:17:55 -0700605static int __sev_launch_update_vmsa(struct kvm *kvm, struct kvm_vcpu *vcpu,
606 int *error)
607{
608 struct sev_data_launch_update_vmsa vmsa;
609 struct vcpu_svm *svm = to_svm(vcpu);
610 int ret;
611
612 /* Perform some pre-encryption checks against the VMSA */
613 ret = sev_es_sync_vmsa(svm);
614 if (ret)
615 return ret;
616
617 /*
618 * The LAUNCH_UPDATE_VMSA command will perform in-place encryption of
619 * the VMSA memory content (i.e it will write the same memory region
620 * with the guest's key), so invalidate it first.
621 */
Peter Gondab67a4cc2021-10-21 10:42:59 -0700622 clflush_cache_range(svm->sev_es.vmsa, PAGE_SIZE);
Peter Gondabb18a672021-09-15 10:17:55 -0700623
624 vmsa.reserved = 0;
625 vmsa.handle = to_kvm_svm(kvm)->sev_info.handle;
Peter Gondab67a4cc2021-10-21 10:42:59 -0700626 vmsa.address = __sme_pa(svm->sev_es.vmsa);
Peter Gondabb18a672021-09-15 10:17:55 -0700627 vmsa.len = PAGE_SIZE;
Peter Gondabaa1e5c2021-10-15 13:32:22 -0400628 ret = sev_issue_cmd(kvm, SEV_CMD_LAUNCH_UPDATE_VMSA, &vmsa, error);
629 if (ret)
630 return ret;
631
632 vcpu->arch.guest_state_protected = true;
633 return 0;
Peter Gondabb18a672021-09-15 10:17:55 -0700634}
635
Tom Lendackyad731092020-12-10 11:10:09 -0600636static int sev_launch_update_vmsa(struct kvm *kvm, struct kvm_sev_cmd *argp)
637{
Sean Christophersonc36b16d2021-03-30 20:19:34 -0700638 struct kvm_vcpu *vcpu;
Marc Zyngier46808a42021-11-16 16:04:02 +0000639 unsigned long i;
640 int ret;
Tom Lendackyad731092020-12-10 11:10:09 -0600641
642 if (!sev_es_guest(kvm))
643 return -ENOTTY;
644
Sean Christophersonc36b16d2021-03-30 20:19:34 -0700645 kvm_for_each_vcpu(i, vcpu, kvm) {
Peter Gondabb18a672021-09-15 10:17:55 -0700646 ret = mutex_lock_killable(&vcpu->mutex);
Tom Lendackyad731092020-12-10 11:10:09 -0600647 if (ret)
Sean Christopherson238eca82021-04-06 15:49:52 -0700648 return ret;
Tom Lendackyad731092020-12-10 11:10:09 -0600649
Peter Gondabb18a672021-09-15 10:17:55 -0700650 ret = __sev_launch_update_vmsa(kvm, vcpu, &argp->error);
Tom Lendackyad731092020-12-10 11:10:09 -0600651
Peter Gondabb18a672021-09-15 10:17:55 -0700652 mutex_unlock(&vcpu->mutex);
Tom Lendackyad731092020-12-10 11:10:09 -0600653 if (ret)
Sean Christopherson238eca82021-04-06 15:49:52 -0700654 return ret;
Tom Lendackyad731092020-12-10 11:10:09 -0600655 }
656
Sean Christopherson238eca82021-04-06 15:49:52 -0700657 return 0;
Tom Lendackyad731092020-12-10 11:10:09 -0600658}
659
Joerg Roedeleaf78262020-03-24 10:41:54 +0100660static int sev_launch_measure(struct kvm *kvm, struct kvm_sev_cmd *argp)
661{
662 void __user *measure = (void __user *)(uintptr_t)argp->data;
663 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
Sean Christopherson238eca82021-04-06 15:49:52 -0700664 struct sev_data_launch_measure data;
Joerg Roedeleaf78262020-03-24 10:41:54 +0100665 struct kvm_sev_launch_measure params;
666 void __user *p = NULL;
667 void *blob = NULL;
668 int ret;
669
670 if (!sev_guest(kvm))
671 return -ENOTTY;
672
673 if (copy_from_user(&params, measure, sizeof(params)))
674 return -EFAULT;
675
Sean Christopherson238eca82021-04-06 15:49:52 -0700676 memset(&data, 0, sizeof(data));
Joerg Roedeleaf78262020-03-24 10:41:54 +0100677
678 /* User wants to query the blob length */
679 if (!params.len)
680 goto cmd;
681
682 p = (void __user *)(uintptr_t)params.uaddr;
683 if (p) {
Sean Christopherson238eca82021-04-06 15:49:52 -0700684 if (params.len > SEV_FW_BLOB_MAX_SIZE)
685 return -EINVAL;
Joerg Roedeleaf78262020-03-24 10:41:54 +0100686
Sean Christophersoneba04b22021-03-30 19:30:25 -0700687 blob = kmalloc(params.len, GFP_KERNEL_ACCOUNT);
Joerg Roedeleaf78262020-03-24 10:41:54 +0100688 if (!blob)
Sean Christopherson238eca82021-04-06 15:49:52 -0700689 return -ENOMEM;
Joerg Roedeleaf78262020-03-24 10:41:54 +0100690
Sean Christopherson238eca82021-04-06 15:49:52 -0700691 data.address = __psp_pa(blob);
692 data.len = params.len;
Joerg Roedeleaf78262020-03-24 10:41:54 +0100693 }
694
695cmd:
Sean Christopherson238eca82021-04-06 15:49:52 -0700696 data.handle = sev->handle;
697 ret = sev_issue_cmd(kvm, SEV_CMD_LAUNCH_MEASURE, &data, &argp->error);
Joerg Roedeleaf78262020-03-24 10:41:54 +0100698
699 /*
700 * If we query the session length, FW responded with expected data.
701 */
702 if (!params.len)
703 goto done;
704
705 if (ret)
706 goto e_free_blob;
707
708 if (blob) {
709 if (copy_to_user(p, blob, params.len))
710 ret = -EFAULT;
711 }
712
713done:
Sean Christopherson238eca82021-04-06 15:49:52 -0700714 params.len = data.len;
Joerg Roedeleaf78262020-03-24 10:41:54 +0100715 if (copy_to_user(measure, &params, sizeof(params)))
716 ret = -EFAULT;
717e_free_blob:
718 kfree(blob);
Joerg Roedeleaf78262020-03-24 10:41:54 +0100719 return ret;
720}
721
722static int sev_launch_finish(struct kvm *kvm, struct kvm_sev_cmd *argp)
723{
724 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
Sean Christopherson238eca82021-04-06 15:49:52 -0700725 struct sev_data_launch_finish data;
Joerg Roedeleaf78262020-03-24 10:41:54 +0100726
727 if (!sev_guest(kvm))
728 return -ENOTTY;
729
Sean Christopherson238eca82021-04-06 15:49:52 -0700730 data.handle = sev->handle;
731 return sev_issue_cmd(kvm, SEV_CMD_LAUNCH_FINISH, &data, &argp->error);
Joerg Roedeleaf78262020-03-24 10:41:54 +0100732}
733
734static int sev_guest_status(struct kvm *kvm, struct kvm_sev_cmd *argp)
735{
736 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
737 struct kvm_sev_guest_status params;
Sean Christopherson238eca82021-04-06 15:49:52 -0700738 struct sev_data_guest_status data;
Joerg Roedeleaf78262020-03-24 10:41:54 +0100739 int ret;
740
741 if (!sev_guest(kvm))
742 return -ENOTTY;
743
Sean Christopherson238eca82021-04-06 15:49:52 -0700744 memset(&data, 0, sizeof(data));
Joerg Roedeleaf78262020-03-24 10:41:54 +0100745
Sean Christopherson238eca82021-04-06 15:49:52 -0700746 data.handle = sev->handle;
747 ret = sev_issue_cmd(kvm, SEV_CMD_GUEST_STATUS, &data, &argp->error);
Joerg Roedeleaf78262020-03-24 10:41:54 +0100748 if (ret)
Sean Christopherson238eca82021-04-06 15:49:52 -0700749 return ret;
Joerg Roedeleaf78262020-03-24 10:41:54 +0100750
Sean Christopherson238eca82021-04-06 15:49:52 -0700751 params.policy = data.policy;
752 params.state = data.state;
753 params.handle = data.handle;
Joerg Roedeleaf78262020-03-24 10:41:54 +0100754
755 if (copy_to_user((void __user *)(uintptr_t)argp->data, &params, sizeof(params)))
756 ret = -EFAULT;
Sean Christopherson238eca82021-04-06 15:49:52 -0700757
Joerg Roedeleaf78262020-03-24 10:41:54 +0100758 return ret;
759}
760
761static int __sev_issue_dbg_cmd(struct kvm *kvm, unsigned long src,
762 unsigned long dst, int size,
763 int *error, bool enc)
764{
765 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
Sean Christopherson238eca82021-04-06 15:49:52 -0700766 struct sev_data_dbg data;
Joerg Roedeleaf78262020-03-24 10:41:54 +0100767
Sean Christopherson238eca82021-04-06 15:49:52 -0700768 data.reserved = 0;
769 data.handle = sev->handle;
770 data.dst_addr = dst;
771 data.src_addr = src;
772 data.len = size;
Joerg Roedeleaf78262020-03-24 10:41:54 +0100773
Sean Christopherson238eca82021-04-06 15:49:52 -0700774 return sev_issue_cmd(kvm,
775 enc ? SEV_CMD_DBG_ENCRYPT : SEV_CMD_DBG_DECRYPT,
776 &data, error);
Joerg Roedeleaf78262020-03-24 10:41:54 +0100777}
778
779static int __sev_dbg_decrypt(struct kvm *kvm, unsigned long src_paddr,
780 unsigned long dst_paddr, int sz, int *err)
781{
782 int offset;
783
784 /*
785 * Its safe to read more than we are asked, caller should ensure that
786 * destination has enough space.
787 */
Joerg Roedeleaf78262020-03-24 10:41:54 +0100788 offset = src_paddr & 15;
Ashish Kalra854c57f2020-11-10 22:42:05 +0000789 src_paddr = round_down(src_paddr, 16);
Joerg Roedeleaf78262020-03-24 10:41:54 +0100790 sz = round_up(sz + offset, 16);
791
792 return __sev_issue_dbg_cmd(kvm, src_paddr, dst_paddr, sz, err, false);
793}
794
795static int __sev_dbg_decrypt_user(struct kvm *kvm, unsigned long paddr,
Sean Christopherson368340a2021-05-06 16:15:42 -0700796 void __user *dst_uaddr,
Joerg Roedeleaf78262020-03-24 10:41:54 +0100797 unsigned long dst_paddr,
798 int size, int *err)
799{
800 struct page *tpage = NULL;
801 int ret, offset;
802
803 /* if inputs are not 16-byte then use intermediate buffer */
804 if (!IS_ALIGNED(dst_paddr, 16) ||
805 !IS_ALIGNED(paddr, 16) ||
806 !IS_ALIGNED(size, 16)) {
807 tpage = (void *)alloc_page(GFP_KERNEL);
808 if (!tpage)
809 return -ENOMEM;
810
811 dst_paddr = __sme_page_pa(tpage);
812 }
813
814 ret = __sev_dbg_decrypt(kvm, paddr, dst_paddr, size, err);
815 if (ret)
816 goto e_free;
817
818 if (tpage) {
819 offset = paddr & 15;
Sean Christopherson368340a2021-05-06 16:15:42 -0700820 if (copy_to_user(dst_uaddr, page_address(tpage) + offset, size))
Joerg Roedeleaf78262020-03-24 10:41:54 +0100821 ret = -EFAULT;
822 }
823
824e_free:
825 if (tpage)
826 __free_page(tpage);
827
828 return ret;
829}
830
831static int __sev_dbg_encrypt_user(struct kvm *kvm, unsigned long paddr,
Sean Christopherson368340a2021-05-06 16:15:42 -0700832 void __user *vaddr,
Joerg Roedeleaf78262020-03-24 10:41:54 +0100833 unsigned long dst_paddr,
Sean Christopherson368340a2021-05-06 16:15:42 -0700834 void __user *dst_vaddr,
Joerg Roedeleaf78262020-03-24 10:41:54 +0100835 int size, int *error)
836{
837 struct page *src_tpage = NULL;
838 struct page *dst_tpage = NULL;
839 int ret, len = size;
840
841 /* If source buffer is not aligned then use an intermediate buffer */
Sean Christopherson368340a2021-05-06 16:15:42 -0700842 if (!IS_ALIGNED((unsigned long)vaddr, 16)) {
Joerg Roedeleaf78262020-03-24 10:41:54 +0100843 src_tpage = alloc_page(GFP_KERNEL);
844 if (!src_tpage)
845 return -ENOMEM;
846
Sean Christopherson368340a2021-05-06 16:15:42 -0700847 if (copy_from_user(page_address(src_tpage), vaddr, size)) {
Joerg Roedeleaf78262020-03-24 10:41:54 +0100848 __free_page(src_tpage);
849 return -EFAULT;
850 }
851
852 paddr = __sme_page_pa(src_tpage);
853 }
854
855 /*
856 * If destination buffer or length is not aligned then do read-modify-write:
857 * - decrypt destination in an intermediate buffer
858 * - copy the source buffer in an intermediate buffer
859 * - use the intermediate buffer as source buffer
860 */
Sean Christopherson368340a2021-05-06 16:15:42 -0700861 if (!IS_ALIGNED((unsigned long)dst_vaddr, 16) || !IS_ALIGNED(size, 16)) {
Joerg Roedeleaf78262020-03-24 10:41:54 +0100862 int dst_offset;
863
864 dst_tpage = alloc_page(GFP_KERNEL);
865 if (!dst_tpage) {
866 ret = -ENOMEM;
867 goto e_free;
868 }
869
870 ret = __sev_dbg_decrypt(kvm, dst_paddr,
871 __sme_page_pa(dst_tpage), size, error);
872 if (ret)
873 goto e_free;
874
875 /*
876 * If source is kernel buffer then use memcpy() otherwise
877 * copy_from_user().
878 */
879 dst_offset = dst_paddr & 15;
880
881 if (src_tpage)
882 memcpy(page_address(dst_tpage) + dst_offset,
883 page_address(src_tpage), size);
884 else {
885 if (copy_from_user(page_address(dst_tpage) + dst_offset,
Sean Christopherson368340a2021-05-06 16:15:42 -0700886 vaddr, size)) {
Joerg Roedeleaf78262020-03-24 10:41:54 +0100887 ret = -EFAULT;
888 goto e_free;
889 }
890 }
891
892 paddr = __sme_page_pa(dst_tpage);
893 dst_paddr = round_down(dst_paddr, 16);
894 len = round_up(size, 16);
895 }
896
897 ret = __sev_issue_dbg_cmd(kvm, paddr, dst_paddr, len, error, true);
898
899e_free:
900 if (src_tpage)
901 __free_page(src_tpage);
902 if (dst_tpage)
903 __free_page(dst_tpage);
904 return ret;
905}
906
907static int sev_dbg_crypt(struct kvm *kvm, struct kvm_sev_cmd *argp, bool dec)
908{
909 unsigned long vaddr, vaddr_end, next_vaddr;
910 unsigned long dst_vaddr;
911 struct page **src_p, **dst_p;
912 struct kvm_sev_dbg debug;
913 unsigned long n;
914 unsigned int size;
915 int ret;
916
917 if (!sev_guest(kvm))
918 return -ENOTTY;
919
920 if (copy_from_user(&debug, (void __user *)(uintptr_t)argp->data, sizeof(debug)))
921 return -EFAULT;
922
923 if (!debug.len || debug.src_uaddr + debug.len < debug.src_uaddr)
924 return -EINVAL;
925 if (!debug.dst_uaddr)
926 return -EINVAL;
927
928 vaddr = debug.src_uaddr;
929 size = debug.len;
930 vaddr_end = vaddr + size;
931 dst_vaddr = debug.dst_uaddr;
932
933 for (; vaddr < vaddr_end; vaddr = next_vaddr) {
934 int len, s_off, d_off;
935
936 /* lock userspace source and destination page */
937 src_p = sev_pin_memory(kvm, vaddr & PAGE_MASK, PAGE_SIZE, &n, 0);
Dan Carpenterff2bd9f2020-07-14 17:23:51 +0300938 if (IS_ERR(src_p))
939 return PTR_ERR(src_p);
Joerg Roedeleaf78262020-03-24 10:41:54 +0100940
941 dst_p = sev_pin_memory(kvm, dst_vaddr & PAGE_MASK, PAGE_SIZE, &n, 1);
Dan Carpenterff2bd9f2020-07-14 17:23:51 +0300942 if (IS_ERR(dst_p)) {
Joerg Roedeleaf78262020-03-24 10:41:54 +0100943 sev_unpin_memory(kvm, src_p, n);
Dan Carpenterff2bd9f2020-07-14 17:23:51 +0300944 return PTR_ERR(dst_p);
Joerg Roedeleaf78262020-03-24 10:41:54 +0100945 }
946
947 /*
Paolo Bonzini14e3dd82020-09-23 13:01:33 -0400948 * Flush (on non-coherent CPUs) before DBG_{DE,EN}CRYPT read or modify
949 * the pages; flush the destination too so that future accesses do not
950 * see stale data.
Joerg Roedeleaf78262020-03-24 10:41:54 +0100951 */
952 sev_clflush_pages(src_p, 1);
953 sev_clflush_pages(dst_p, 1);
954
955 /*
956 * Since user buffer may not be page aligned, calculate the
957 * offset within the page.
958 */
959 s_off = vaddr & ~PAGE_MASK;
960 d_off = dst_vaddr & ~PAGE_MASK;
961 len = min_t(size_t, (PAGE_SIZE - s_off), size);
962
963 if (dec)
964 ret = __sev_dbg_decrypt_user(kvm,
965 __sme_page_pa(src_p[0]) + s_off,
Sean Christopherson368340a2021-05-06 16:15:42 -0700966 (void __user *)dst_vaddr,
Joerg Roedeleaf78262020-03-24 10:41:54 +0100967 __sme_page_pa(dst_p[0]) + d_off,
968 len, &argp->error);
969 else
970 ret = __sev_dbg_encrypt_user(kvm,
971 __sme_page_pa(src_p[0]) + s_off,
Sean Christopherson368340a2021-05-06 16:15:42 -0700972 (void __user *)vaddr,
Joerg Roedeleaf78262020-03-24 10:41:54 +0100973 __sme_page_pa(dst_p[0]) + d_off,
Sean Christopherson368340a2021-05-06 16:15:42 -0700974 (void __user *)dst_vaddr,
Joerg Roedeleaf78262020-03-24 10:41:54 +0100975 len, &argp->error);
976
977 sev_unpin_memory(kvm, src_p, n);
978 sev_unpin_memory(kvm, dst_p, n);
979
980 if (ret)
981 goto err;
982
983 next_vaddr = vaddr + len;
984 dst_vaddr = dst_vaddr + len;
985 size -= len;
986 }
987err:
988 return ret;
989}
990
991static int sev_launch_secret(struct kvm *kvm, struct kvm_sev_cmd *argp)
992{
993 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
Sean Christopherson238eca82021-04-06 15:49:52 -0700994 struct sev_data_launch_secret data;
Joerg Roedeleaf78262020-03-24 10:41:54 +0100995 struct kvm_sev_launch_secret params;
996 struct page **pages;
997 void *blob, *hdr;
Cfir Cohen50085be2020-08-07 17:37:46 -0700998 unsigned long n, i;
Joerg Roedeleaf78262020-03-24 10:41:54 +0100999 int ret, offset;
1000
1001 if (!sev_guest(kvm))
1002 return -ENOTTY;
1003
1004 if (copy_from_user(&params, (void __user *)(uintptr_t)argp->data, sizeof(params)))
1005 return -EFAULT;
1006
1007 pages = sev_pin_memory(kvm, params.guest_uaddr, params.guest_len, &n, 1);
Paolo Bonzinia8d908b2020-06-23 05:12:24 -04001008 if (IS_ERR(pages))
1009 return PTR_ERR(pages);
Joerg Roedeleaf78262020-03-24 10:41:54 +01001010
1011 /*
Paolo Bonzini14e3dd82020-09-23 13:01:33 -04001012 * Flush (on non-coherent CPUs) before LAUNCH_SECRET encrypts pages in
1013 * place; the cache may contain the data that was written unencrypted.
Cfir Cohen50085be2020-08-07 17:37:46 -07001014 */
1015 sev_clflush_pages(pages, n);
1016
1017 /*
Joerg Roedeleaf78262020-03-24 10:41:54 +01001018 * The secret must be copied into contiguous memory region, lets verify
1019 * that userspace memory pages are contiguous before we issue command.
1020 */
1021 if (get_num_contig_pages(0, pages, n) != n) {
1022 ret = -EINVAL;
1023 goto e_unpin_memory;
1024 }
1025
Sean Christopherson238eca82021-04-06 15:49:52 -07001026 memset(&data, 0, sizeof(data));
Joerg Roedeleaf78262020-03-24 10:41:54 +01001027
1028 offset = params.guest_uaddr & (PAGE_SIZE - 1);
Sean Christopherson238eca82021-04-06 15:49:52 -07001029 data.guest_address = __sme_page_pa(pages[0]) + offset;
1030 data.guest_len = params.guest_len;
Joerg Roedeleaf78262020-03-24 10:41:54 +01001031
1032 blob = psp_copy_user_blob(params.trans_uaddr, params.trans_len);
1033 if (IS_ERR(blob)) {
1034 ret = PTR_ERR(blob);
Sean Christopherson238eca82021-04-06 15:49:52 -07001035 goto e_unpin_memory;
Joerg Roedeleaf78262020-03-24 10:41:54 +01001036 }
1037
Sean Christopherson238eca82021-04-06 15:49:52 -07001038 data.trans_address = __psp_pa(blob);
1039 data.trans_len = params.trans_len;
Joerg Roedeleaf78262020-03-24 10:41:54 +01001040
1041 hdr = psp_copy_user_blob(params.hdr_uaddr, params.hdr_len);
1042 if (IS_ERR(hdr)) {
1043 ret = PTR_ERR(hdr);
1044 goto e_free_blob;
1045 }
Sean Christopherson238eca82021-04-06 15:49:52 -07001046 data.hdr_address = __psp_pa(hdr);
1047 data.hdr_len = params.hdr_len;
Joerg Roedeleaf78262020-03-24 10:41:54 +01001048
Sean Christopherson238eca82021-04-06 15:49:52 -07001049 data.handle = sev->handle;
1050 ret = sev_issue_cmd(kvm, SEV_CMD_LAUNCH_UPDATE_SECRET, &data, &argp->error);
Joerg Roedeleaf78262020-03-24 10:41:54 +01001051
1052 kfree(hdr);
1053
1054e_free_blob:
1055 kfree(blob);
Joerg Roedeleaf78262020-03-24 10:41:54 +01001056e_unpin_memory:
Cfir Cohen50085be2020-08-07 17:37:46 -07001057 /* content of memory is updated, mark pages dirty */
1058 for (i = 0; i < n; i++) {
1059 set_page_dirty_lock(pages[i]);
1060 mark_page_accessed(pages[i]);
1061 }
Joerg Roedeleaf78262020-03-24 10:41:54 +01001062 sev_unpin_memory(kvm, pages, n);
1063 return ret;
1064}
1065
Brijesh Singh2c07ded2021-01-04 09:17:49 -06001066static int sev_get_attestation_report(struct kvm *kvm, struct kvm_sev_cmd *argp)
1067{
1068 void __user *report = (void __user *)(uintptr_t)argp->data;
1069 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
Sean Christopherson238eca82021-04-06 15:49:52 -07001070 struct sev_data_attestation_report data;
Brijesh Singh2c07ded2021-01-04 09:17:49 -06001071 struct kvm_sev_attestation_report params;
1072 void __user *p;
1073 void *blob = NULL;
1074 int ret;
1075
1076 if (!sev_guest(kvm))
1077 return -ENOTTY;
1078
1079 if (copy_from_user(&params, (void __user *)(uintptr_t)argp->data, sizeof(params)))
1080 return -EFAULT;
1081
Sean Christopherson238eca82021-04-06 15:49:52 -07001082 memset(&data, 0, sizeof(data));
Brijesh Singh2c07ded2021-01-04 09:17:49 -06001083
1084 /* User wants to query the blob length */
1085 if (!params.len)
1086 goto cmd;
1087
1088 p = (void __user *)(uintptr_t)params.uaddr;
1089 if (p) {
Sean Christopherson238eca82021-04-06 15:49:52 -07001090 if (params.len > SEV_FW_BLOB_MAX_SIZE)
1091 return -EINVAL;
Brijesh Singh2c07ded2021-01-04 09:17:49 -06001092
Sean Christophersoneba04b22021-03-30 19:30:25 -07001093 blob = kmalloc(params.len, GFP_KERNEL_ACCOUNT);
Brijesh Singh2c07ded2021-01-04 09:17:49 -06001094 if (!blob)
Sean Christopherson238eca82021-04-06 15:49:52 -07001095 return -ENOMEM;
Brijesh Singh2c07ded2021-01-04 09:17:49 -06001096
Sean Christopherson238eca82021-04-06 15:49:52 -07001097 data.address = __psp_pa(blob);
1098 data.len = params.len;
1099 memcpy(data.mnonce, params.mnonce, sizeof(params.mnonce));
Brijesh Singh2c07ded2021-01-04 09:17:49 -06001100 }
1101cmd:
Sean Christopherson238eca82021-04-06 15:49:52 -07001102 data.handle = sev->handle;
1103 ret = sev_issue_cmd(kvm, SEV_CMD_ATTESTATION_REPORT, &data, &argp->error);
Brijesh Singh2c07ded2021-01-04 09:17:49 -06001104 /*
1105 * If we query the session length, FW responded with expected data.
1106 */
1107 if (!params.len)
1108 goto done;
1109
1110 if (ret)
1111 goto e_free_blob;
1112
1113 if (blob) {
1114 if (copy_to_user(p, blob, params.len))
1115 ret = -EFAULT;
1116 }
1117
1118done:
Sean Christopherson238eca82021-04-06 15:49:52 -07001119 params.len = data.len;
Brijesh Singh2c07ded2021-01-04 09:17:49 -06001120 if (copy_to_user(report, &params, sizeof(params)))
1121 ret = -EFAULT;
1122e_free_blob:
1123 kfree(blob);
Brijesh Singh2c07ded2021-01-04 09:17:49 -06001124 return ret;
1125}
1126
Brijesh Singh4cfdd472021-04-15 15:53:14 +00001127/* Userspace wants to query session length. */
1128static int
1129__sev_send_start_query_session_length(struct kvm *kvm, struct kvm_sev_cmd *argp,
1130 struct kvm_sev_send_start *params)
1131{
1132 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
Sean Christopherson238eca82021-04-06 15:49:52 -07001133 struct sev_data_send_start data;
Brijesh Singh4cfdd472021-04-15 15:53:14 +00001134 int ret;
1135
Ashish Kalra4f13d472021-06-07 06:15:32 +00001136 memset(&data, 0, sizeof(data));
Sean Christopherson238eca82021-04-06 15:49:52 -07001137 data.handle = sev->handle;
1138 ret = sev_issue_cmd(kvm, SEV_CMD_SEND_START, &data, &argp->error);
Brijesh Singh4cfdd472021-04-15 15:53:14 +00001139
Sean Christopherson238eca82021-04-06 15:49:52 -07001140 params->session_len = data.session_len;
Brijesh Singh4cfdd472021-04-15 15:53:14 +00001141 if (copy_to_user((void __user *)(uintptr_t)argp->data, params,
1142 sizeof(struct kvm_sev_send_start)))
1143 ret = -EFAULT;
1144
Brijesh Singh4cfdd472021-04-15 15:53:14 +00001145 return ret;
1146}
1147
1148static int sev_send_start(struct kvm *kvm, struct kvm_sev_cmd *argp)
1149{
1150 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
Sean Christopherson238eca82021-04-06 15:49:52 -07001151 struct sev_data_send_start data;
Brijesh Singh4cfdd472021-04-15 15:53:14 +00001152 struct kvm_sev_send_start params;
1153 void *amd_certs, *session_data;
1154 void *pdh_cert, *plat_certs;
1155 int ret;
1156
1157 if (!sev_guest(kvm))
1158 return -ENOTTY;
1159
1160 if (copy_from_user(&params, (void __user *)(uintptr_t)argp->data,
1161 sizeof(struct kvm_sev_send_start)))
1162 return -EFAULT;
1163
1164 /* if session_len is zero, userspace wants to query the session length */
1165 if (!params.session_len)
1166 return __sev_send_start_query_session_length(kvm, argp,
1167 &params);
1168
1169 /* some sanity checks */
1170 if (!params.pdh_cert_uaddr || !params.pdh_cert_len ||
1171 !params.session_uaddr || params.session_len > SEV_FW_BLOB_MAX_SIZE)
1172 return -EINVAL;
1173
1174 /* allocate the memory to hold the session data blob */
1175 session_data = kmalloc(params.session_len, GFP_KERNEL_ACCOUNT);
1176 if (!session_data)
1177 return -ENOMEM;
1178
1179 /* copy the certificate blobs from userspace */
1180 pdh_cert = psp_copy_user_blob(params.pdh_cert_uaddr,
1181 params.pdh_cert_len);
1182 if (IS_ERR(pdh_cert)) {
1183 ret = PTR_ERR(pdh_cert);
1184 goto e_free_session;
1185 }
1186
1187 plat_certs = psp_copy_user_blob(params.plat_certs_uaddr,
1188 params.plat_certs_len);
1189 if (IS_ERR(plat_certs)) {
1190 ret = PTR_ERR(plat_certs);
1191 goto e_free_pdh;
1192 }
1193
1194 amd_certs = psp_copy_user_blob(params.amd_certs_uaddr,
1195 params.amd_certs_len);
1196 if (IS_ERR(amd_certs)) {
1197 ret = PTR_ERR(amd_certs);
1198 goto e_free_plat_cert;
1199 }
1200
Brijesh Singh4cfdd472021-04-15 15:53:14 +00001201 /* populate the FW SEND_START field with system physical address */
Sean Christopherson238eca82021-04-06 15:49:52 -07001202 memset(&data, 0, sizeof(data));
1203 data.pdh_cert_address = __psp_pa(pdh_cert);
1204 data.pdh_cert_len = params.pdh_cert_len;
1205 data.plat_certs_address = __psp_pa(plat_certs);
1206 data.plat_certs_len = params.plat_certs_len;
1207 data.amd_certs_address = __psp_pa(amd_certs);
1208 data.amd_certs_len = params.amd_certs_len;
1209 data.session_address = __psp_pa(session_data);
1210 data.session_len = params.session_len;
1211 data.handle = sev->handle;
Brijesh Singh4cfdd472021-04-15 15:53:14 +00001212
Sean Christopherson238eca82021-04-06 15:49:52 -07001213 ret = sev_issue_cmd(kvm, SEV_CMD_SEND_START, &data, &argp->error);
Brijesh Singh4cfdd472021-04-15 15:53:14 +00001214
1215 if (!ret && copy_to_user((void __user *)(uintptr_t)params.session_uaddr,
1216 session_data, params.session_len)) {
1217 ret = -EFAULT;
Sean Christopherson238eca82021-04-06 15:49:52 -07001218 goto e_free_amd_cert;
Brijesh Singh4cfdd472021-04-15 15:53:14 +00001219 }
1220
Sean Christopherson238eca82021-04-06 15:49:52 -07001221 params.policy = data.policy;
1222 params.session_len = data.session_len;
Brijesh Singh4cfdd472021-04-15 15:53:14 +00001223 if (copy_to_user((void __user *)(uintptr_t)argp->data, &params,
1224 sizeof(struct kvm_sev_send_start)))
1225 ret = -EFAULT;
1226
Brijesh Singh4cfdd472021-04-15 15:53:14 +00001227e_free_amd_cert:
1228 kfree(amd_certs);
1229e_free_plat_cert:
1230 kfree(plat_certs);
1231e_free_pdh:
1232 kfree(pdh_cert);
1233e_free_session:
1234 kfree(session_data);
1235 return ret;
1236}
1237
Brijesh Singhd3d1af82021-04-15 15:53:55 +00001238/* Userspace wants to query either header or trans length. */
1239static int
1240__sev_send_update_data_query_lengths(struct kvm *kvm, struct kvm_sev_cmd *argp,
1241 struct kvm_sev_send_update_data *params)
1242{
1243 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
Sean Christopherson238eca82021-04-06 15:49:52 -07001244 struct sev_data_send_update_data data;
Brijesh Singhd3d1af82021-04-15 15:53:55 +00001245 int ret;
1246
Ashish Kalra4f13d472021-06-07 06:15:32 +00001247 memset(&data, 0, sizeof(data));
Sean Christopherson238eca82021-04-06 15:49:52 -07001248 data.handle = sev->handle;
1249 ret = sev_issue_cmd(kvm, SEV_CMD_SEND_UPDATE_DATA, &data, &argp->error);
Brijesh Singhd3d1af82021-04-15 15:53:55 +00001250
Sean Christopherson238eca82021-04-06 15:49:52 -07001251 params->hdr_len = data.hdr_len;
1252 params->trans_len = data.trans_len;
Brijesh Singhd3d1af82021-04-15 15:53:55 +00001253
1254 if (copy_to_user((void __user *)(uintptr_t)argp->data, params,
1255 sizeof(struct kvm_sev_send_update_data)))
1256 ret = -EFAULT;
1257
Brijesh Singhd3d1af82021-04-15 15:53:55 +00001258 return ret;
1259}
1260
1261static int sev_send_update_data(struct kvm *kvm, struct kvm_sev_cmd *argp)
1262{
1263 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
Sean Christopherson238eca82021-04-06 15:49:52 -07001264 struct sev_data_send_update_data data;
Brijesh Singhd3d1af82021-04-15 15:53:55 +00001265 struct kvm_sev_send_update_data params;
1266 void *hdr, *trans_data;
1267 struct page **guest_page;
1268 unsigned long n;
1269 int ret, offset;
1270
1271 if (!sev_guest(kvm))
1272 return -ENOTTY;
1273
1274 if (copy_from_user(&params, (void __user *)(uintptr_t)argp->data,
1275 sizeof(struct kvm_sev_send_update_data)))
1276 return -EFAULT;
1277
1278 /* userspace wants to query either header or trans length */
1279 if (!params.trans_len || !params.hdr_len)
1280 return __sev_send_update_data_query_lengths(kvm, argp, &params);
1281
1282 if (!params.trans_uaddr || !params.guest_uaddr ||
1283 !params.guest_len || !params.hdr_uaddr)
1284 return -EINVAL;
1285
1286 /* Check if we are crossing the page boundary */
1287 offset = params.guest_uaddr & (PAGE_SIZE - 1);
1288 if ((params.guest_len + offset > PAGE_SIZE))
1289 return -EINVAL;
1290
1291 /* Pin guest memory */
1292 guest_page = sev_pin_memory(kvm, params.guest_uaddr & PAGE_MASK,
1293 PAGE_SIZE, &n, 0);
Sean Christophersonc7a1b2b2021-05-06 10:58:26 -07001294 if (IS_ERR(guest_page))
1295 return PTR_ERR(guest_page);
Brijesh Singhd3d1af82021-04-15 15:53:55 +00001296
1297 /* allocate memory for header and transport buffer */
1298 ret = -ENOMEM;
1299 hdr = kmalloc(params.hdr_len, GFP_KERNEL_ACCOUNT);
1300 if (!hdr)
1301 goto e_unpin;
1302
1303 trans_data = kmalloc(params.trans_len, GFP_KERNEL_ACCOUNT);
1304 if (!trans_data)
1305 goto e_free_hdr;
1306
Sean Christopherson238eca82021-04-06 15:49:52 -07001307 memset(&data, 0, sizeof(data));
1308 data.hdr_address = __psp_pa(hdr);
1309 data.hdr_len = params.hdr_len;
1310 data.trans_address = __psp_pa(trans_data);
1311 data.trans_len = params.trans_len;
Brijesh Singhd3d1af82021-04-15 15:53:55 +00001312
1313 /* The SEND_UPDATE_DATA command requires C-bit to be always set. */
Sean Christopherson238eca82021-04-06 15:49:52 -07001314 data.guest_address = (page_to_pfn(guest_page[0]) << PAGE_SHIFT) + offset;
1315 data.guest_address |= sev_me_mask;
1316 data.guest_len = params.guest_len;
1317 data.handle = sev->handle;
Brijesh Singhd3d1af82021-04-15 15:53:55 +00001318
Sean Christopherson238eca82021-04-06 15:49:52 -07001319 ret = sev_issue_cmd(kvm, SEV_CMD_SEND_UPDATE_DATA, &data, &argp->error);
Brijesh Singhd3d1af82021-04-15 15:53:55 +00001320
1321 if (ret)
Sean Christopherson238eca82021-04-06 15:49:52 -07001322 goto e_free_trans_data;
Brijesh Singhd3d1af82021-04-15 15:53:55 +00001323
1324 /* copy transport buffer to user space */
1325 if (copy_to_user((void __user *)(uintptr_t)params.trans_uaddr,
1326 trans_data, params.trans_len)) {
1327 ret = -EFAULT;
Sean Christopherson238eca82021-04-06 15:49:52 -07001328 goto e_free_trans_data;
Brijesh Singhd3d1af82021-04-15 15:53:55 +00001329 }
1330
1331 /* Copy packet header to userspace. */
Sean Christophersonb4a69392021-05-06 10:58:25 -07001332 if (copy_to_user((void __user *)(uintptr_t)params.hdr_uaddr, hdr,
1333 params.hdr_len))
1334 ret = -EFAULT;
Brijesh Singhd3d1af82021-04-15 15:53:55 +00001335
Brijesh Singhd3d1af82021-04-15 15:53:55 +00001336e_free_trans_data:
1337 kfree(trans_data);
1338e_free_hdr:
1339 kfree(hdr);
1340e_unpin:
1341 sev_unpin_memory(kvm, guest_page, n);
1342
1343 return ret;
1344}
1345
Brijesh Singhfddecf62021-04-15 15:54:15 +00001346static int sev_send_finish(struct kvm *kvm, struct kvm_sev_cmd *argp)
1347{
1348 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
Sean Christopherson238eca82021-04-06 15:49:52 -07001349 struct sev_data_send_finish data;
Brijesh Singhfddecf62021-04-15 15:54:15 +00001350
1351 if (!sev_guest(kvm))
1352 return -ENOTTY;
1353
Sean Christopherson238eca82021-04-06 15:49:52 -07001354 data.handle = sev->handle;
1355 return sev_issue_cmd(kvm, SEV_CMD_SEND_FINISH, &data, &argp->error);
Brijesh Singhfddecf62021-04-15 15:54:15 +00001356}
1357
Steve Rutherford5569e2e2021-04-20 05:01:20 -04001358static int sev_send_cancel(struct kvm *kvm, struct kvm_sev_cmd *argp)
1359{
1360 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
Sean Christopherson238eca82021-04-06 15:49:52 -07001361 struct sev_data_send_cancel data;
Steve Rutherford5569e2e2021-04-20 05:01:20 -04001362
1363 if (!sev_guest(kvm))
1364 return -ENOTTY;
1365
Sean Christopherson238eca82021-04-06 15:49:52 -07001366 data.handle = sev->handle;
1367 return sev_issue_cmd(kvm, SEV_CMD_SEND_CANCEL, &data, &argp->error);
Steve Rutherford5569e2e2021-04-20 05:01:20 -04001368}
1369
Brijesh Singhaf43cbb2021-04-15 15:54:50 +00001370static int sev_receive_start(struct kvm *kvm, struct kvm_sev_cmd *argp)
1371{
1372 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
Sean Christopherson238eca82021-04-06 15:49:52 -07001373 struct sev_data_receive_start start;
Brijesh Singhaf43cbb2021-04-15 15:54:50 +00001374 struct kvm_sev_receive_start params;
1375 int *error = &argp->error;
1376 void *session_data;
1377 void *pdh_data;
1378 int ret;
1379
1380 if (!sev_guest(kvm))
1381 return -ENOTTY;
1382
1383 /* Get parameter from the userspace */
1384 if (copy_from_user(&params, (void __user *)(uintptr_t)argp->data,
1385 sizeof(struct kvm_sev_receive_start)))
1386 return -EFAULT;
1387
1388 /* some sanity checks */
1389 if (!params.pdh_uaddr || !params.pdh_len ||
1390 !params.session_uaddr || !params.session_len)
1391 return -EINVAL;
1392
1393 pdh_data = psp_copy_user_blob(params.pdh_uaddr, params.pdh_len);
1394 if (IS_ERR(pdh_data))
1395 return PTR_ERR(pdh_data);
1396
1397 session_data = psp_copy_user_blob(params.session_uaddr,
1398 params.session_len);
1399 if (IS_ERR(session_data)) {
1400 ret = PTR_ERR(session_data);
1401 goto e_free_pdh;
1402 }
1403
Sean Christopherson238eca82021-04-06 15:49:52 -07001404 memset(&start, 0, sizeof(start));
1405 start.handle = params.handle;
1406 start.policy = params.policy;
1407 start.pdh_cert_address = __psp_pa(pdh_data);
1408 start.pdh_cert_len = params.pdh_len;
1409 start.session_address = __psp_pa(session_data);
1410 start.session_len = params.session_len;
Brijesh Singhaf43cbb2021-04-15 15:54:50 +00001411
1412 /* create memory encryption context */
Sean Christopherson238eca82021-04-06 15:49:52 -07001413 ret = __sev_issue_cmd(argp->sev_fd, SEV_CMD_RECEIVE_START, &start,
Brijesh Singhaf43cbb2021-04-15 15:54:50 +00001414 error);
1415 if (ret)
Sean Christopherson238eca82021-04-06 15:49:52 -07001416 goto e_free_session;
Brijesh Singhaf43cbb2021-04-15 15:54:50 +00001417
1418 /* Bind ASID to this guest */
Sean Christopherson238eca82021-04-06 15:49:52 -07001419 ret = sev_bind_asid(kvm, start.handle, error);
Mingwei Zhangf1815e0a2021-09-12 18:18:15 +00001420 if (ret) {
1421 sev_decommission(start.handle);
Sean Christopherson238eca82021-04-06 15:49:52 -07001422 goto e_free_session;
Mingwei Zhangf1815e0a2021-09-12 18:18:15 +00001423 }
Brijesh Singhaf43cbb2021-04-15 15:54:50 +00001424
Sean Christopherson238eca82021-04-06 15:49:52 -07001425 params.handle = start.handle;
Brijesh Singhaf43cbb2021-04-15 15:54:50 +00001426 if (copy_to_user((void __user *)(uintptr_t)argp->data,
1427 &params, sizeof(struct kvm_sev_receive_start))) {
1428 ret = -EFAULT;
Sean Christopherson238eca82021-04-06 15:49:52 -07001429 sev_unbind_asid(kvm, start.handle);
1430 goto e_free_session;
Brijesh Singhaf43cbb2021-04-15 15:54:50 +00001431 }
1432
Sean Christopherson238eca82021-04-06 15:49:52 -07001433 sev->handle = start.handle;
Brijesh Singhaf43cbb2021-04-15 15:54:50 +00001434 sev->fd = argp->sev_fd;
1435
Brijesh Singhaf43cbb2021-04-15 15:54:50 +00001436e_free_session:
1437 kfree(session_data);
1438e_free_pdh:
1439 kfree(pdh_data);
1440
1441 return ret;
1442}
1443
Brijesh Singh15fb7de2021-04-15 15:55:17 +00001444static int sev_receive_update_data(struct kvm *kvm, struct kvm_sev_cmd *argp)
1445{
1446 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
1447 struct kvm_sev_receive_update_data params;
Sean Christopherson238eca82021-04-06 15:49:52 -07001448 struct sev_data_receive_update_data data;
Brijesh Singh15fb7de2021-04-15 15:55:17 +00001449 void *hdr = NULL, *trans = NULL;
1450 struct page **guest_page;
1451 unsigned long n;
1452 int ret, offset;
1453
1454 if (!sev_guest(kvm))
1455 return -EINVAL;
1456
1457 if (copy_from_user(&params, (void __user *)(uintptr_t)argp->data,
1458 sizeof(struct kvm_sev_receive_update_data)))
1459 return -EFAULT;
1460
1461 if (!params.hdr_uaddr || !params.hdr_len ||
1462 !params.guest_uaddr || !params.guest_len ||
1463 !params.trans_uaddr || !params.trans_len)
1464 return -EINVAL;
1465
1466 /* Check if we are crossing the page boundary */
1467 offset = params.guest_uaddr & (PAGE_SIZE - 1);
1468 if ((params.guest_len + offset > PAGE_SIZE))
1469 return -EINVAL;
1470
1471 hdr = psp_copy_user_blob(params.hdr_uaddr, params.hdr_len);
1472 if (IS_ERR(hdr))
1473 return PTR_ERR(hdr);
1474
1475 trans = psp_copy_user_blob(params.trans_uaddr, params.trans_len);
1476 if (IS_ERR(trans)) {
1477 ret = PTR_ERR(trans);
1478 goto e_free_hdr;
1479 }
1480
Sean Christopherson238eca82021-04-06 15:49:52 -07001481 memset(&data, 0, sizeof(data));
1482 data.hdr_address = __psp_pa(hdr);
1483 data.hdr_len = params.hdr_len;
1484 data.trans_address = __psp_pa(trans);
1485 data.trans_len = params.trans_len;
Brijesh Singh15fb7de2021-04-15 15:55:17 +00001486
1487 /* Pin guest memory */
Brijesh Singh15fb7de2021-04-15 15:55:17 +00001488 guest_page = sev_pin_memory(kvm, params.guest_uaddr & PAGE_MASK,
Sean Christopherson50c03802021-09-14 14:09:50 -07001489 PAGE_SIZE, &n, 1);
Sean Christophersonc7a1b2b2021-05-06 10:58:26 -07001490 if (IS_ERR(guest_page)) {
1491 ret = PTR_ERR(guest_page);
Sean Christopherson238eca82021-04-06 15:49:52 -07001492 goto e_free_trans;
Sean Christophersonc7a1b2b2021-05-06 10:58:26 -07001493 }
Brijesh Singh15fb7de2021-04-15 15:55:17 +00001494
Masahiro Kozukac8c340a2021-09-14 14:09:51 -07001495 /*
1496 * Flush (on non-coherent CPUs) before RECEIVE_UPDATE_DATA, the PSP
1497 * encrypts the written data with the guest's key, and the cache may
1498 * contain dirty, unencrypted data.
1499 */
1500 sev_clflush_pages(guest_page, n);
1501
Brijesh Singh15fb7de2021-04-15 15:55:17 +00001502 /* The RECEIVE_UPDATE_DATA command requires C-bit to be always set. */
Sean Christopherson238eca82021-04-06 15:49:52 -07001503 data.guest_address = (page_to_pfn(guest_page[0]) << PAGE_SHIFT) + offset;
1504 data.guest_address |= sev_me_mask;
1505 data.guest_len = params.guest_len;
1506 data.handle = sev->handle;
Brijesh Singh15fb7de2021-04-15 15:55:17 +00001507
Sean Christopherson238eca82021-04-06 15:49:52 -07001508 ret = sev_issue_cmd(kvm, SEV_CMD_RECEIVE_UPDATE_DATA, &data,
Brijesh Singh15fb7de2021-04-15 15:55:17 +00001509 &argp->error);
1510
1511 sev_unpin_memory(kvm, guest_page, n);
1512
Brijesh Singh15fb7de2021-04-15 15:55:17 +00001513e_free_trans:
1514 kfree(trans);
1515e_free_hdr:
1516 kfree(hdr);
1517
1518 return ret;
1519}
1520
Brijesh Singh6a443de2021-04-15 15:55:40 +00001521static int sev_receive_finish(struct kvm *kvm, struct kvm_sev_cmd *argp)
1522{
1523 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
Sean Christopherson238eca82021-04-06 15:49:52 -07001524 struct sev_data_receive_finish data;
Brijesh Singh6a443de2021-04-15 15:55:40 +00001525
1526 if (!sev_guest(kvm))
1527 return -ENOTTY;
1528
Sean Christopherson238eca82021-04-06 15:49:52 -07001529 data.handle = sev->handle;
1530 return sev_issue_cmd(kvm, SEV_CMD_RECEIVE_FINISH, &data, &argp->error);
Brijesh Singh6a443de2021-04-15 15:55:40 +00001531}
1532
Sean Christopherson8e38e962021-11-09 21:51:01 +00001533static bool is_cmd_allowed_from_mirror(u32 cmd_id)
Peter Gonda5b92b6c2021-09-21 08:03:45 -07001534{
1535 /*
1536 * Allow mirrors VM to call KVM_SEV_LAUNCH_UPDATE_VMSA to enable SEV-ES
1537 * active mirror VMs. Also allow the debugging and status commands.
1538 */
1539 if (cmd_id == KVM_SEV_LAUNCH_UPDATE_VMSA ||
1540 cmd_id == KVM_SEV_GUEST_STATUS || cmd_id == KVM_SEV_DBG_DECRYPT ||
1541 cmd_id == KVM_SEV_DBG_ENCRYPT)
1542 return true;
1543
1544 return false;
1545}
1546
Paolo Bonzini501b5802021-11-22 19:50:29 -05001547static int sev_lock_two_vms(struct kvm *dst_kvm, struct kvm *src_kvm)
Peter Gondab5663932021-10-21 10:43:00 -07001548{
Paolo Bonzini501b5802021-11-22 19:50:29 -05001549 struct kvm_sev_info *dst_sev = &to_kvm_svm(dst_kvm)->sev_info;
1550 struct kvm_sev_info *src_sev = &to_kvm_svm(src_kvm)->sev_info;
Paolo Bonzinic9d61dc2021-11-22 19:50:36 -05001551 int r = -EBUSY;
Paolo Bonzini501b5802021-11-22 19:50:29 -05001552
1553 if (dst_kvm == src_kvm)
1554 return -EINVAL;
Peter Gondab5663932021-10-21 10:43:00 -07001555
1556 /*
Paolo Bonzini501b5802021-11-22 19:50:29 -05001557 * Bail if these VMs are already involved in a migration to avoid
1558 * deadlock between two VMs trying to migrate to/from each other.
Peter Gondab5663932021-10-21 10:43:00 -07001559 */
Paolo Bonzini501b5802021-11-22 19:50:29 -05001560 if (atomic_cmpxchg_acquire(&dst_sev->migration_in_progress, 0, 1))
Peter Gondab5663932021-10-21 10:43:00 -07001561 return -EBUSY;
1562
Paolo Bonzinic9d61dc2021-11-22 19:50:36 -05001563 if (atomic_cmpxchg_acquire(&src_sev->migration_in_progress, 0, 1))
1564 goto release_dst;
Peter Gondab5663932021-10-21 10:43:00 -07001565
Paolo Bonzinic9d61dc2021-11-22 19:50:36 -05001566 r = -EINTR;
1567 if (mutex_lock_killable(&dst_kvm->lock))
1568 goto release_src;
Wanpeng Li597cb792022-01-04 22:41:03 -08001569 if (mutex_lock_killable_nested(&src_kvm->lock, SINGLE_DEPTH_NESTING))
Paolo Bonzinic9d61dc2021-11-22 19:50:36 -05001570 goto unlock_dst;
Peter Gondab5663932021-10-21 10:43:00 -07001571 return 0;
Paolo Bonzinic9d61dc2021-11-22 19:50:36 -05001572
1573unlock_dst:
1574 mutex_unlock(&dst_kvm->lock);
1575release_src:
1576 atomic_set_release(&src_sev->migration_in_progress, 0);
1577release_dst:
1578 atomic_set_release(&dst_sev->migration_in_progress, 0);
1579 return r;
Peter Gondab5663932021-10-21 10:43:00 -07001580}
1581
Paolo Bonzini501b5802021-11-22 19:50:29 -05001582static void sev_unlock_two_vms(struct kvm *dst_kvm, struct kvm *src_kvm)
Peter Gondab5663932021-10-21 10:43:00 -07001583{
Paolo Bonzini501b5802021-11-22 19:50:29 -05001584 struct kvm_sev_info *dst_sev = &to_kvm_svm(dst_kvm)->sev_info;
1585 struct kvm_sev_info *src_sev = &to_kvm_svm(src_kvm)->sev_info;
Peter Gondab5663932021-10-21 10:43:00 -07001586
Paolo Bonzini501b5802021-11-22 19:50:29 -05001587 mutex_unlock(&dst_kvm->lock);
1588 mutex_unlock(&src_kvm->lock);
1589 atomic_set_release(&dst_sev->migration_in_progress, 0);
1590 atomic_set_release(&src_sev->migration_in_progress, 0);
Peter Gondab5663932021-10-21 10:43:00 -07001591}
1592
1593
1594static int sev_lock_vcpus_for_migration(struct kvm *kvm)
1595{
1596 struct kvm_vcpu *vcpu;
Marc Zyngier46808a42021-11-16 16:04:02 +00001597 unsigned long i, j;
Peter Gondab5663932021-10-21 10:43:00 -07001598
1599 kvm_for_each_vcpu(i, vcpu, kvm) {
1600 if (mutex_lock_killable(&vcpu->mutex))
1601 goto out_unlock;
1602 }
1603
1604 return 0;
1605
1606out_unlock:
1607 kvm_for_each_vcpu(j, vcpu, kvm) {
1608 if (i == j)
1609 break;
1610
1611 mutex_unlock(&vcpu->mutex);
1612 }
1613 return -EINTR;
1614}
1615
1616static void sev_unlock_vcpus_for_migration(struct kvm *kvm)
1617{
1618 struct kvm_vcpu *vcpu;
Marc Zyngier46808a42021-11-16 16:04:02 +00001619 unsigned long i;
Peter Gondab5663932021-10-21 10:43:00 -07001620
1621 kvm_for_each_vcpu(i, vcpu, kvm) {
1622 mutex_unlock(&vcpu->mutex);
1623 }
1624}
1625
1626static void sev_migrate_from(struct kvm_sev_info *dst,
1627 struct kvm_sev_info *src)
1628{
1629 dst->active = true;
1630 dst->asid = src->asid;
1631 dst->handle = src->handle;
1632 dst->pages_locked = src->pages_locked;
Paolo Bonzini642525e2021-11-22 19:50:31 -05001633 dst->enc_context_owner = src->enc_context_owner;
Peter Gondab5663932021-10-21 10:43:00 -07001634
1635 src->asid = 0;
1636 src->active = false;
1637 src->handle = 0;
1638 src->pages_locked = 0;
Paolo Bonzini642525e2021-11-22 19:50:31 -05001639 src->enc_context_owner = NULL;
Peter Gondab5663932021-10-21 10:43:00 -07001640
Paolo Bonzini46741642021-11-22 19:50:28 -05001641 list_cut_before(&dst->regions_list, &src->regions_list, &src->regions_list);
Peter Gondab5663932021-10-21 10:43:00 -07001642}
1643
Peter Gonda0b020f52021-10-21 10:43:01 -07001644static int sev_es_migrate_from(struct kvm *dst, struct kvm *src)
1645{
Marc Zyngier46808a42021-11-16 16:04:02 +00001646 unsigned long i;
Peter Gonda0b020f52021-10-21 10:43:01 -07001647 struct kvm_vcpu *dst_vcpu, *src_vcpu;
1648 struct vcpu_svm *dst_svm, *src_svm;
1649
1650 if (atomic_read(&src->online_vcpus) != atomic_read(&dst->online_vcpus))
1651 return -EINVAL;
1652
1653 kvm_for_each_vcpu(i, src_vcpu, src) {
1654 if (!src_vcpu->arch.guest_state_protected)
1655 return -EINVAL;
1656 }
1657
1658 kvm_for_each_vcpu(i, src_vcpu, src) {
1659 src_svm = to_svm(src_vcpu);
1660 dst_vcpu = kvm_get_vcpu(dst, i);
1661 dst_svm = to_svm(dst_vcpu);
1662
1663 /*
1664 * Transfer VMSA and GHCB state to the destination. Nullify and
1665 * clear source fields as appropriate, the state now belongs to
1666 * the destination.
1667 */
1668 memcpy(&dst_svm->sev_es, &src_svm->sev_es, sizeof(src_svm->sev_es));
1669 dst_svm->vmcb->control.ghcb_gpa = src_svm->vmcb->control.ghcb_gpa;
1670 dst_svm->vmcb->control.vmsa_pa = src_svm->vmcb->control.vmsa_pa;
1671 dst_vcpu->arch.guest_state_protected = true;
1672
1673 memset(&src_svm->sev_es, 0, sizeof(src_svm->sev_es));
1674 src_svm->vmcb->control.ghcb_gpa = INVALID_PAGE;
1675 src_svm->vmcb->control.vmsa_pa = INVALID_PAGE;
1676 src_vcpu->arch.guest_state_protected = false;
1677 }
1678 to_kvm_svm(src)->sev_info.es_active = false;
1679 to_kvm_svm(dst)->sev_info.es_active = true;
1680
1681 return 0;
1682}
1683
Peter Gondab5663932021-10-21 10:43:00 -07001684int svm_vm_migrate_from(struct kvm *kvm, unsigned int source_fd)
1685{
1686 struct kvm_sev_info *dst_sev = &to_kvm_svm(kvm)->sev_info;
Paolo Bonzini501cfe02021-11-12 04:02:24 -05001687 struct kvm_sev_info *src_sev, *cg_cleanup_sev;
Peter Gondab5663932021-10-21 10:43:00 -07001688 struct file *source_kvm_file;
1689 struct kvm *source_kvm;
Paolo Bonzini501cfe02021-11-12 04:02:24 -05001690 bool charged = false;
Peter Gondab5663932021-10-21 10:43:00 -07001691 int ret;
1692
Peter Gondab5663932021-10-21 10:43:00 -07001693 source_kvm_file = fget(source_fd);
1694 if (!file_is_kvm(source_kvm_file)) {
1695 ret = -EBADF;
1696 goto out_fput;
1697 }
1698
1699 source_kvm = source_kvm_file->private_data;
Paolo Bonzini501b5802021-11-22 19:50:29 -05001700 ret = sev_lock_two_vms(kvm, source_kvm);
Peter Gondab5663932021-10-21 10:43:00 -07001701 if (ret)
1702 goto out_fput;
1703
Paolo Bonzini501b5802021-11-22 19:50:29 -05001704 if (sev_guest(kvm) || !sev_guest(source_kvm)) {
Peter Gondab5663932021-10-21 10:43:00 -07001705 ret = -EINVAL;
Paolo Bonzini501b5802021-11-22 19:50:29 -05001706 goto out_unlock;
Peter Gondab5663932021-10-21 10:43:00 -07001707 }
1708
1709 src_sev = &to_kvm_svm(source_kvm)->sev_info;
Paolo Bonzini17d44a92021-11-22 19:50:34 -05001710
1711 /*
1712 * VMs mirroring src's encryption context rely on it to keep the
1713 * ASID allocated, but below we are clearing src_sev->asid.
1714 */
1715 if (src_sev->num_mirrored_vms) {
1716 ret = -EBUSY;
1717 goto out_unlock;
1718 }
1719
Peter Gondab5663932021-10-21 10:43:00 -07001720 dst_sev->misc_cg = get_current_misc_cg();
Paolo Bonzini501cfe02021-11-12 04:02:24 -05001721 cg_cleanup_sev = dst_sev;
Peter Gondab5663932021-10-21 10:43:00 -07001722 if (dst_sev->misc_cg != src_sev->misc_cg) {
1723 ret = sev_misc_cg_try_charge(dst_sev);
1724 if (ret)
Paolo Bonzini501cfe02021-11-12 04:02:24 -05001725 goto out_dst_cgroup;
1726 charged = true;
Peter Gondab5663932021-10-21 10:43:00 -07001727 }
1728
1729 ret = sev_lock_vcpus_for_migration(kvm);
1730 if (ret)
1731 goto out_dst_cgroup;
1732 ret = sev_lock_vcpus_for_migration(source_kvm);
1733 if (ret)
1734 goto out_dst_vcpu;
1735
Peter Gonda0b020f52021-10-21 10:43:01 -07001736 if (sev_es_guest(source_kvm)) {
1737 ret = sev_es_migrate_from(kvm, source_kvm);
1738 if (ret)
1739 goto out_source_vcpu;
1740 }
Peter Gondab5663932021-10-21 10:43:00 -07001741 sev_migrate_from(dst_sev, src_sev);
1742 kvm_vm_dead(source_kvm);
Paolo Bonzini501cfe02021-11-12 04:02:24 -05001743 cg_cleanup_sev = src_sev;
Peter Gondab5663932021-10-21 10:43:00 -07001744 ret = 0;
1745
Peter Gonda0b020f52021-10-21 10:43:01 -07001746out_source_vcpu:
Peter Gondab5663932021-10-21 10:43:00 -07001747 sev_unlock_vcpus_for_migration(source_kvm);
1748out_dst_vcpu:
1749 sev_unlock_vcpus_for_migration(kvm);
1750out_dst_cgroup:
Paolo Bonzini501cfe02021-11-12 04:02:24 -05001751 /* Operates on the source on success, on the destination on failure. */
1752 if (charged)
1753 sev_misc_cg_uncharge(cg_cleanup_sev);
1754 put_misc_cg(cg_cleanup_sev->misc_cg);
1755 cg_cleanup_sev->misc_cg = NULL;
Paolo Bonzini501b5802021-11-22 19:50:29 -05001756out_unlock:
1757 sev_unlock_two_vms(kvm, source_kvm);
Peter Gondab5663932021-10-21 10:43:00 -07001758out_fput:
1759 if (source_kvm_file)
1760 fput(source_kvm_file);
Peter Gondab5663932021-10-21 10:43:00 -07001761 return ret;
1762}
1763
Joerg Roedeleaf78262020-03-24 10:41:54 +01001764int svm_mem_enc_op(struct kvm *kvm, void __user *argp)
1765{
1766 struct kvm_sev_cmd sev_cmd;
1767 int r;
1768
Sean Christophersona5c1c5a2021-04-21 19:11:23 -07001769 if (!sev_enabled)
Joerg Roedeleaf78262020-03-24 10:41:54 +01001770 return -ENOTTY;
1771
1772 if (!argp)
1773 return 0;
1774
1775 if (copy_from_user(&sev_cmd, argp, sizeof(struct kvm_sev_cmd)))
1776 return -EFAULT;
1777
1778 mutex_lock(&kvm->lock);
1779
Peter Gonda5b92b6c2021-09-21 08:03:45 -07001780 /* Only the enc_context_owner handles some memory enc operations. */
1781 if (is_mirroring_enc_context(kvm) &&
Sean Christopherson8e38e962021-11-09 21:51:01 +00001782 !is_cmd_allowed_from_mirror(sev_cmd.id)) {
Nathan Tempelman54526d12021-04-08 22:32:14 +00001783 r = -EINVAL;
1784 goto out;
1785 }
1786
Joerg Roedeleaf78262020-03-24 10:41:54 +01001787 switch (sev_cmd.id) {
Sean Christopherson9fa15212021-03-30 20:19:35 -07001788 case KVM_SEV_ES_INIT:
Sean Christopherson8d364a02021-04-21 19:11:17 -07001789 if (!sev_es_enabled) {
Sean Christopherson9fa15212021-03-30 20:19:35 -07001790 r = -ENOTTY;
1791 goto out;
1792 }
1793 fallthrough;
Joerg Roedeleaf78262020-03-24 10:41:54 +01001794 case KVM_SEV_INIT:
1795 r = sev_guest_init(kvm, &sev_cmd);
1796 break;
1797 case KVM_SEV_LAUNCH_START:
1798 r = sev_launch_start(kvm, &sev_cmd);
1799 break;
1800 case KVM_SEV_LAUNCH_UPDATE_DATA:
1801 r = sev_launch_update_data(kvm, &sev_cmd);
1802 break;
Tom Lendackyad731092020-12-10 11:10:09 -06001803 case KVM_SEV_LAUNCH_UPDATE_VMSA:
1804 r = sev_launch_update_vmsa(kvm, &sev_cmd);
1805 break;
Joerg Roedeleaf78262020-03-24 10:41:54 +01001806 case KVM_SEV_LAUNCH_MEASURE:
1807 r = sev_launch_measure(kvm, &sev_cmd);
1808 break;
1809 case KVM_SEV_LAUNCH_FINISH:
1810 r = sev_launch_finish(kvm, &sev_cmd);
1811 break;
1812 case KVM_SEV_GUEST_STATUS:
1813 r = sev_guest_status(kvm, &sev_cmd);
1814 break;
1815 case KVM_SEV_DBG_DECRYPT:
1816 r = sev_dbg_crypt(kvm, &sev_cmd, true);
1817 break;
1818 case KVM_SEV_DBG_ENCRYPT:
1819 r = sev_dbg_crypt(kvm, &sev_cmd, false);
1820 break;
1821 case KVM_SEV_LAUNCH_SECRET:
1822 r = sev_launch_secret(kvm, &sev_cmd);
1823 break;
Brijesh Singh2c07ded2021-01-04 09:17:49 -06001824 case KVM_SEV_GET_ATTESTATION_REPORT:
1825 r = sev_get_attestation_report(kvm, &sev_cmd);
1826 break;
Brijesh Singh4cfdd472021-04-15 15:53:14 +00001827 case KVM_SEV_SEND_START:
1828 r = sev_send_start(kvm, &sev_cmd);
1829 break;
Brijesh Singhd3d1af82021-04-15 15:53:55 +00001830 case KVM_SEV_SEND_UPDATE_DATA:
1831 r = sev_send_update_data(kvm, &sev_cmd);
1832 break;
Brijesh Singhfddecf62021-04-15 15:54:15 +00001833 case KVM_SEV_SEND_FINISH:
1834 r = sev_send_finish(kvm, &sev_cmd);
1835 break;
Steve Rutherford5569e2e2021-04-20 05:01:20 -04001836 case KVM_SEV_SEND_CANCEL:
1837 r = sev_send_cancel(kvm, &sev_cmd);
1838 break;
Brijesh Singhaf43cbb2021-04-15 15:54:50 +00001839 case KVM_SEV_RECEIVE_START:
1840 r = sev_receive_start(kvm, &sev_cmd);
1841 break;
Brijesh Singh15fb7de2021-04-15 15:55:17 +00001842 case KVM_SEV_RECEIVE_UPDATE_DATA:
1843 r = sev_receive_update_data(kvm, &sev_cmd);
1844 break;
Brijesh Singh6a443de2021-04-15 15:55:40 +00001845 case KVM_SEV_RECEIVE_FINISH:
1846 r = sev_receive_finish(kvm, &sev_cmd);
1847 break;
Joerg Roedeleaf78262020-03-24 10:41:54 +01001848 default:
1849 r = -EINVAL;
1850 goto out;
1851 }
1852
1853 if (copy_to_user(argp, &sev_cmd, sizeof(struct kvm_sev_cmd)))
1854 r = -EFAULT;
1855
1856out:
1857 mutex_unlock(&kvm->lock);
1858 return r;
1859}
1860
1861int svm_register_enc_region(struct kvm *kvm,
1862 struct kvm_enc_region *range)
1863{
1864 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
1865 struct enc_region *region;
1866 int ret = 0;
1867
1868 if (!sev_guest(kvm))
1869 return -ENOTTY;
1870
Nathan Tempelman54526d12021-04-08 22:32:14 +00001871 /* If kvm is mirroring encryption context it isn't responsible for it */
1872 if (is_mirroring_enc_context(kvm))
1873 return -EINVAL;
1874
Joerg Roedeleaf78262020-03-24 10:41:54 +01001875 if (range->addr > ULONG_MAX || range->size > ULONG_MAX)
1876 return -EINVAL;
1877
1878 region = kzalloc(sizeof(*region), GFP_KERNEL_ACCOUNT);
1879 if (!region)
1880 return -ENOMEM;
1881
Peter Gonda19a23da2021-01-27 08:15:24 -08001882 mutex_lock(&kvm->lock);
Joerg Roedeleaf78262020-03-24 10:41:54 +01001883 region->pages = sev_pin_memory(kvm, range->addr, range->size, &region->npages, 1);
Paolo Bonzinia8d908b2020-06-23 05:12:24 -04001884 if (IS_ERR(region->pages)) {
1885 ret = PTR_ERR(region->pages);
Peter Gonda19a23da2021-01-27 08:15:24 -08001886 mutex_unlock(&kvm->lock);
Joerg Roedeleaf78262020-03-24 10:41:54 +01001887 goto e_free;
1888 }
1889
Peter Gonda19a23da2021-01-27 08:15:24 -08001890 region->uaddr = range->addr;
1891 region->size = range->size;
1892
1893 list_add_tail(&region->list, &sev->regions_list);
1894 mutex_unlock(&kvm->lock);
1895
Joerg Roedeleaf78262020-03-24 10:41:54 +01001896 /*
1897 * The guest may change the memory encryption attribute from C=0 -> C=1
1898 * or vice versa for this memory range. Lets make sure caches are
1899 * flushed to ensure that guest data gets written into memory with
1900 * correct C-bit.
1901 */
1902 sev_clflush_pages(region->pages, region->npages);
1903
Joerg Roedeleaf78262020-03-24 10:41:54 +01001904 return ret;
1905
1906e_free:
1907 kfree(region);
1908 return ret;
1909}
1910
1911static struct enc_region *
1912find_enc_region(struct kvm *kvm, struct kvm_enc_region *range)
1913{
1914 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
1915 struct list_head *head = &sev->regions_list;
1916 struct enc_region *i;
1917
1918 list_for_each_entry(i, head, list) {
1919 if (i->uaddr == range->addr &&
1920 i->size == range->size)
1921 return i;
1922 }
1923
1924 return NULL;
1925}
1926
1927static void __unregister_enc_region_locked(struct kvm *kvm,
1928 struct enc_region *region)
1929{
1930 sev_unpin_memory(kvm, region->pages, region->npages);
1931 list_del(&region->list);
1932 kfree(region);
1933}
1934
1935int svm_unregister_enc_region(struct kvm *kvm,
1936 struct kvm_enc_region *range)
1937{
1938 struct enc_region *region;
1939 int ret;
1940
Nathan Tempelman54526d12021-04-08 22:32:14 +00001941 /* If kvm is mirroring encryption context it isn't responsible for it */
1942 if (is_mirroring_enc_context(kvm))
1943 return -EINVAL;
1944
Joerg Roedeleaf78262020-03-24 10:41:54 +01001945 mutex_lock(&kvm->lock);
1946
1947 if (!sev_guest(kvm)) {
1948 ret = -ENOTTY;
1949 goto failed;
1950 }
1951
1952 region = find_enc_region(kvm, range);
1953 if (!region) {
1954 ret = -EINVAL;
1955 goto failed;
1956 }
1957
1958 /*
1959 * Ensure that all guest tagged cache entries are flushed before
1960 * releasing the pages back to the system for use. CLFLUSH will
1961 * not do this, so issue a WBINVD.
1962 */
1963 wbinvd_on_all_cpus();
1964
1965 __unregister_enc_region_locked(kvm, region);
1966
1967 mutex_unlock(&kvm->lock);
1968 return 0;
1969
1970failed:
1971 mutex_unlock(&kvm->lock);
1972 return ret;
1973}
1974
Nathan Tempelman54526d12021-04-08 22:32:14 +00001975int svm_vm_copy_asid_from(struct kvm *kvm, unsigned int source_fd)
1976{
1977 struct file *source_kvm_file;
1978 struct kvm *source_kvm;
Paolo Bonzinibf42b022021-11-22 19:50:33 -05001979 struct kvm_sev_info *source_sev, *mirror_sev;
Nathan Tempelman54526d12021-04-08 22:32:14 +00001980 int ret;
1981
1982 source_kvm_file = fget(source_fd);
1983 if (!file_is_kvm(source_kvm_file)) {
1984 ret = -EBADF;
Paolo Bonzinibf42b022021-11-22 19:50:33 -05001985 goto e_source_fput;
Nathan Tempelman54526d12021-04-08 22:32:14 +00001986 }
1987
1988 source_kvm = source_kvm_file->private_data;
Paolo Bonzinibf42b022021-11-22 19:50:33 -05001989 ret = sev_lock_two_vms(kvm, source_kvm);
1990 if (ret)
1991 goto e_source_fput;
Nathan Tempelman54526d12021-04-08 22:32:14 +00001992
Paolo Bonzinibf42b022021-11-22 19:50:33 -05001993 /*
1994 * Mirrors of mirrors should work, but let's not get silly. Also
1995 * disallow out-of-band SEV/SEV-ES init if the target is already an
1996 * SEV guest, or if vCPUs have been created. KVM relies on vCPUs being
1997 * created after SEV/SEV-ES initialization, e.g. to init intercepts.
1998 */
1999 if (sev_guest(kvm) || !sev_guest(source_kvm) ||
2000 is_mirroring_enc_context(source_kvm) || kvm->created_vcpus) {
Nathan Tempelman54526d12021-04-08 22:32:14 +00002001 ret = -EINVAL;
Paolo Bonzinibf42b022021-11-22 19:50:33 -05002002 goto e_unlock;
Nathan Tempelman54526d12021-04-08 22:32:14 +00002003 }
2004
Nathan Tempelman54526d12021-04-08 22:32:14 +00002005 /*
2006 * The mirror kvm holds an enc_context_owner ref so its asid can't
2007 * disappear until we're done with it
2008 */
Paolo Bonzinibf42b022021-11-22 19:50:33 -05002009 source_sev = &to_kvm_svm(source_kvm)->sev_info;
Nathan Tempelman54526d12021-04-08 22:32:14 +00002010 kvm_get_kvm(source_kvm);
Paolo Bonzini17d44a92021-11-22 19:50:34 -05002011 source_sev->num_mirrored_vms++;
Nathan Tempelman54526d12021-04-08 22:32:14 +00002012
Nathan Tempelman54526d12021-04-08 22:32:14 +00002013 /* Set enc_context_owner and copy its encryption context over */
2014 mirror_sev = &to_kvm_svm(kvm)->sev_info;
2015 mirror_sev->enc_context_owner = source_kvm;
Nathan Tempelman54526d12021-04-08 22:32:14 +00002016 mirror_sev->active = true;
Paolo Bonzinibf42b022021-11-22 19:50:33 -05002017 mirror_sev->asid = source_sev->asid;
2018 mirror_sev->fd = source_sev->fd;
2019 mirror_sev->es_active = source_sev->es_active;
2020 mirror_sev->handle = source_sev->handle;
Paolo Bonzini2b347a32021-11-22 19:50:30 -05002021 INIT_LIST_HEAD(&mirror_sev->regions_list);
Paolo Bonzinibf42b022021-11-22 19:50:33 -05002022 ret = 0;
2023
Peter Gondaf43c8872021-09-21 08:03:44 -07002024 /*
2025 * Do not copy ap_jump_table. Since the mirror does not share the same
2026 * KVM contexts as the original, and they may have different
2027 * memory-views.
2028 */
Nathan Tempelman54526d12021-04-08 22:32:14 +00002029
Paolo Bonzinibf42b022021-11-22 19:50:33 -05002030e_unlock:
2031 sev_unlock_two_vms(kvm, source_kvm);
2032e_source_fput:
Colin Ian King8899a5f2021-04-30 18:03:03 +01002033 if (source_kvm_file)
2034 fput(source_kvm_file);
Nathan Tempelman54526d12021-04-08 22:32:14 +00002035 return ret;
2036}
2037
Joerg Roedeleaf78262020-03-24 10:41:54 +01002038void sev_vm_destroy(struct kvm *kvm)
2039{
2040 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
2041 struct list_head *head = &sev->regions_list;
2042 struct list_head *pos, *q;
2043
Paolo Bonzini17d44a92021-11-22 19:50:34 -05002044 WARN_ON(sev->num_mirrored_vms);
2045
Joerg Roedeleaf78262020-03-24 10:41:54 +01002046 if (!sev_guest(kvm))
2047 return;
2048
Nathan Tempelman54526d12021-04-08 22:32:14 +00002049 /* If this is a mirror_kvm release the enc_context_owner and skip sev cleanup */
2050 if (is_mirroring_enc_context(kvm)) {
Paolo Bonzini17d44a92021-11-22 19:50:34 -05002051 struct kvm *owner_kvm = sev->enc_context_owner;
2052 struct kvm_sev_info *owner_sev = &to_kvm_svm(owner_kvm)->sev_info;
2053
2054 mutex_lock(&owner_kvm->lock);
2055 if (!WARN_ON(!owner_sev->num_mirrored_vms))
2056 owner_sev->num_mirrored_vms--;
2057 mutex_unlock(&owner_kvm->lock);
2058 kvm_put_kvm(owner_kvm);
Nathan Tempelman54526d12021-04-08 22:32:14 +00002059 return;
2060 }
2061
Joerg Roedeleaf78262020-03-24 10:41:54 +01002062 /*
2063 * Ensure that all guest tagged cache entries are flushed before
2064 * releasing the pages back to the system for use. CLFLUSH will
2065 * not do this, so issue a WBINVD.
2066 */
2067 wbinvd_on_all_cpus();
2068
2069 /*
2070 * if userspace was terminated before unregistering the memory regions
2071 * then lets unpin all the registered memory.
2072 */
2073 if (!list_empty(head)) {
2074 list_for_each_safe(pos, q, head) {
2075 __unregister_enc_region_locked(kvm,
2076 list_entry(pos, struct enc_region, list));
David Rientjes7be74942020-08-25 12:56:28 -07002077 cond_resched();
Joerg Roedeleaf78262020-03-24 10:41:54 +01002078 }
2079 }
2080
Joerg Roedeleaf78262020-03-24 10:41:54 +01002081 sev_unbind_asid(kvm, sev->handle);
Vipin Sharma7aef27f2021-03-29 21:42:06 -07002082 sev_asid_free(sev);
Joerg Roedeleaf78262020-03-24 10:41:54 +01002083}
2084
Paolo Bonzinid9db0fd2021-04-21 19:11:15 -07002085void __init sev_set_cpu_caps(void)
2086{
Sean Christopherson8d364a02021-04-21 19:11:17 -07002087 if (!sev_enabled)
Paolo Bonzinid9db0fd2021-04-21 19:11:15 -07002088 kvm_cpu_cap_clear(X86_FEATURE_SEV);
Sean Christopherson8d364a02021-04-21 19:11:17 -07002089 if (!sev_es_enabled)
Paolo Bonzinid9db0fd2021-04-21 19:11:15 -07002090 kvm_cpu_cap_clear(X86_FEATURE_SEV_ES);
2091}
2092
Tom Lendacky916391a2020-12-10 11:09:38 -06002093void __init sev_hardware_setup(void)
Joerg Roedeleaf78262020-03-24 10:41:54 +01002094{
Sean Christophersona479c332021-04-21 19:11:18 -07002095#ifdef CONFIG_KVM_AMD_SEV
Vipin Sharma7aef27f2021-03-29 21:42:06 -07002096 unsigned int eax, ebx, ecx, edx, sev_asid_count, sev_es_asid_count;
Tom Lendacky916391a2020-12-10 11:09:38 -06002097 bool sev_es_supported = false;
2098 bool sev_supported = false;
2099
Sean Christophersona479c332021-04-21 19:11:18 -07002100 if (!sev_enabled || !npt_enabled)
Sean Christophersone8126bd2021-04-21 19:11:14 -07002101 goto out;
2102
Tom Lendacky916391a2020-12-10 11:09:38 -06002103 /* Does the CPU support SEV? */
2104 if (!boot_cpu_has(X86_FEATURE_SEV))
2105 goto out;
2106
2107 /* Retrieve SEV CPUID information */
2108 cpuid(0x8000001f, &eax, &ebx, &ecx, &edx);
2109
Tom Lendacky1edc1452020-12-10 11:09:49 -06002110 /* Set encryption bit location for SEV-ES guests */
2111 sev_enc_bit = ebx & 0x3f;
2112
Joerg Roedeleaf78262020-03-24 10:41:54 +01002113 /* Maximum number of encrypted guests supported simultaneously */
Tom Lendacky916391a2020-12-10 11:09:38 -06002114 max_sev_asid = ecx;
Sean Christopherson8cb756b2021-04-21 19:11:21 -07002115 if (!max_sev_asid)
Tom Lendacky916391a2020-12-10 11:09:38 -06002116 goto out;
Joerg Roedeleaf78262020-03-24 10:41:54 +01002117
2118 /* Minimum ASID value that should be used for SEV guest */
Tom Lendacky916391a2020-12-10 11:09:38 -06002119 min_sev_asid = edx;
Brijesh Singhd3d1af82021-04-15 15:53:55 +00002120 sev_me_mask = 1UL << (ebx & 0x3f);
Joerg Roedeleaf78262020-03-24 10:41:54 +01002121
Mingwei Zhangbb2baeb2021-08-02 11:09:03 -07002122 /*
2123 * Initialize SEV ASID bitmaps. Allocate space for ASID 0 in the bitmap,
2124 * even though it's never used, so that the bitmap is indexed by the
2125 * actual ASID.
2126 */
2127 nr_asids = max_sev_asid + 1;
2128 sev_asid_bitmap = bitmap_zalloc(nr_asids, GFP_KERNEL);
Joerg Roedeleaf78262020-03-24 10:41:54 +01002129 if (!sev_asid_bitmap)
Tom Lendacky916391a2020-12-10 11:09:38 -06002130 goto out;
Joerg Roedeleaf78262020-03-24 10:41:54 +01002131
Mingwei Zhangbb2baeb2021-08-02 11:09:03 -07002132 sev_reclaim_asid_bitmap = bitmap_zalloc(nr_asids, GFP_KERNEL);
Sean Christophersonf31b88b2021-04-21 19:11:12 -07002133 if (!sev_reclaim_asid_bitmap) {
2134 bitmap_free(sev_asid_bitmap);
2135 sev_asid_bitmap = NULL;
Tom Lendacky916391a2020-12-10 11:09:38 -06002136 goto out;
Sean Christophersonf31b88b2021-04-21 19:11:12 -07002137 }
Joerg Roedeleaf78262020-03-24 10:41:54 +01002138
Vipin Sharma7aef27f2021-03-29 21:42:06 -07002139 sev_asid_count = max_sev_asid - min_sev_asid + 1;
2140 if (misc_cg_set_capacity(MISC_CG_RES_SEV, sev_asid_count))
2141 goto out;
2142
2143 pr_info("SEV supported: %u ASIDs\n", sev_asid_count);
Tom Lendacky916391a2020-12-10 11:09:38 -06002144 sev_supported = true;
Joerg Roedeleaf78262020-03-24 10:41:54 +01002145
Tom Lendacky916391a2020-12-10 11:09:38 -06002146 /* SEV-ES support requested? */
Sean Christopherson8d364a02021-04-21 19:11:17 -07002147 if (!sev_es_enabled)
Tom Lendacky916391a2020-12-10 11:09:38 -06002148 goto out;
2149
2150 /* Does the CPU support SEV-ES? */
2151 if (!boot_cpu_has(X86_FEATURE_SEV_ES))
2152 goto out;
2153
2154 /* Has the system been allocated ASIDs for SEV-ES? */
2155 if (min_sev_asid == 1)
2156 goto out;
2157
Vipin Sharma7aef27f2021-03-29 21:42:06 -07002158 sev_es_asid_count = min_sev_asid - 1;
2159 if (misc_cg_set_capacity(MISC_CG_RES_SEV_ES, sev_es_asid_count))
2160 goto out;
2161
2162 pr_info("SEV-ES supported: %u ASIDs\n", sev_es_asid_count);
Tom Lendacky916391a2020-12-10 11:09:38 -06002163 sev_es_supported = true;
2164
2165out:
Sean Christopherson8d364a02021-04-21 19:11:17 -07002166 sev_enabled = sev_supported;
2167 sev_es_enabled = sev_es_supported;
Sean Christophersona479c332021-04-21 19:11:18 -07002168#endif
Joerg Roedeleaf78262020-03-24 10:41:54 +01002169}
2170
2171void sev_hardware_teardown(void)
2172{
Sean Christophersona5c1c5a2021-04-21 19:11:23 -07002173 if (!sev_enabled)
Paolo Bonzini9ef15302020-04-13 03:20:06 -04002174 return;
2175
Sean Christopherson469bb322021-04-21 19:11:25 -07002176 /* No need to take sev_bitmap_lock, all VMs have been destroyed. */
Mingwei Zhangbb2baeb2021-08-02 11:09:03 -07002177 sev_flush_asids(1, max_sev_asid);
Sean Christopherson469bb322021-04-21 19:11:25 -07002178
Joerg Roedeleaf78262020-03-24 10:41:54 +01002179 bitmap_free(sev_asid_bitmap);
2180 bitmap_free(sev_reclaim_asid_bitmap);
Sean Christopherson469bb322021-04-21 19:11:25 -07002181
Vipin Sharma7aef27f2021-03-29 21:42:06 -07002182 misc_cg_set_capacity(MISC_CG_RES_SEV, 0);
2183 misc_cg_set_capacity(MISC_CG_RES_SEV_ES, 0);
Joerg Roedeleaf78262020-03-24 10:41:54 +01002184}
Joerg Roedeleaf78262020-03-24 10:41:54 +01002185
Sean Christophersonb95c2212021-04-21 19:11:22 -07002186int sev_cpu_init(struct svm_cpu_data *sd)
2187{
Sean Christophersona5c1c5a2021-04-21 19:11:23 -07002188 if (!sev_enabled)
Sean Christophersonb95c2212021-04-21 19:11:22 -07002189 return 0;
2190
Mingwei Zhangbb2baeb2021-08-02 11:09:03 -07002191 sd->sev_vmcbs = kcalloc(nr_asids, sizeof(void *), GFP_KERNEL);
Sean Christophersonb95c2212021-04-21 19:11:22 -07002192 if (!sd->sev_vmcbs)
2193 return -ENOMEM;
2194
2195 return 0;
Joerg Roedeleaf78262020-03-24 10:41:54 +01002196}
2197
Tom Lendackyadd5e2f2020-12-10 11:09:40 -06002198/*
2199 * Pages used by hardware to hold guest encrypted state must be flushed before
2200 * returning them to the system.
2201 */
2202static void sev_flush_guest_memory(struct vcpu_svm *svm, void *va,
2203 unsigned long len)
2204{
2205 /*
2206 * If hardware enforced cache coherency for encrypted mappings of the
2207 * same physical page is supported, nothing to do.
2208 */
2209 if (boot_cpu_has(X86_FEATURE_SME_COHERENT))
2210 return;
2211
2212 /*
2213 * If the VM Page Flush MSR is supported, use it to flush the page
2214 * (using the page virtual address and the guest ASID).
2215 */
2216 if (boot_cpu_has(X86_FEATURE_VM_PAGE_FLUSH)) {
2217 struct kvm_sev_info *sev;
2218 unsigned long va_start;
2219 u64 start, stop;
2220
2221 /* Align start and stop to page boundaries. */
2222 va_start = (unsigned long)va;
2223 start = (u64)va_start & PAGE_MASK;
2224 stop = PAGE_ALIGN((u64)va_start + len);
2225
2226 if (start < stop) {
2227 sev = &to_kvm_svm(svm->vcpu.kvm)->sev_info;
2228
2229 while (start < stop) {
2230 wrmsrl(MSR_AMD64_VM_PAGE_FLUSH,
2231 start | sev->asid);
2232
2233 start += PAGE_SIZE;
2234 }
2235
2236 return;
2237 }
2238
2239 WARN(1, "Address overflow, using WBINVD\n");
2240 }
2241
2242 /*
2243 * Hardware should always have one of the above features,
2244 * but if not, use WBINVD and issue a warning.
2245 */
2246 WARN_ONCE(1, "Using WBINVD to flush guest memory\n");
2247 wbinvd_on_all_cpus();
2248}
2249
2250void sev_free_vcpu(struct kvm_vcpu *vcpu)
2251{
2252 struct vcpu_svm *svm;
2253
2254 if (!sev_es_guest(vcpu->kvm))
2255 return;
2256
2257 svm = to_svm(vcpu);
2258
2259 if (vcpu->arch.guest_state_protected)
Peter Gondab67a4cc2021-10-21 10:42:59 -07002260 sev_flush_guest_memory(svm, svm->sev_es.vmsa, PAGE_SIZE);
2261 __free_page(virt_to_page(svm->sev_es.vmsa));
Tom Lendacky8f423a82020-12-10 11:09:53 -06002262
Peter Gondab67a4cc2021-10-21 10:42:59 -07002263 if (svm->sev_es.ghcb_sa_free)
Sean Christophersona6552762021-11-09 22:23:50 +00002264 kvfree(svm->sev_es.ghcb_sa);
Tom Lendackyadd5e2f2020-12-10 11:09:40 -06002265}
2266
Tom Lendacky291bd202020-12-10 11:09:47 -06002267static void dump_ghcb(struct vcpu_svm *svm)
2268{
Peter Gondab67a4cc2021-10-21 10:42:59 -07002269 struct ghcb *ghcb = svm->sev_es.ghcb;
Tom Lendacky291bd202020-12-10 11:09:47 -06002270 unsigned int nbits;
2271
2272 /* Re-use the dump_invalid_vmcb module parameter */
2273 if (!dump_invalid_vmcb) {
2274 pr_warn_ratelimited("set kvm_amd.dump_invalid_vmcb=1 to dump internal KVM state.\n");
2275 return;
2276 }
2277
2278 nbits = sizeof(ghcb->save.valid_bitmap) * 8;
2279
2280 pr_err("GHCB (GPA=%016llx):\n", svm->vmcb->control.ghcb_gpa);
2281 pr_err("%-20s%016llx is_valid: %u\n", "sw_exit_code",
2282 ghcb->save.sw_exit_code, ghcb_sw_exit_code_is_valid(ghcb));
2283 pr_err("%-20s%016llx is_valid: %u\n", "sw_exit_info_1",
2284 ghcb->save.sw_exit_info_1, ghcb_sw_exit_info_1_is_valid(ghcb));
2285 pr_err("%-20s%016llx is_valid: %u\n", "sw_exit_info_2",
2286 ghcb->save.sw_exit_info_2, ghcb_sw_exit_info_2_is_valid(ghcb));
2287 pr_err("%-20s%016llx is_valid: %u\n", "sw_scratch",
2288 ghcb->save.sw_scratch, ghcb_sw_scratch_is_valid(ghcb));
2289 pr_err("%-20s%*pb\n", "valid_bitmap", nbits, ghcb->save.valid_bitmap);
2290}
2291
2292static void sev_es_sync_to_ghcb(struct vcpu_svm *svm)
2293{
2294 struct kvm_vcpu *vcpu = &svm->vcpu;
Peter Gondab67a4cc2021-10-21 10:42:59 -07002295 struct ghcb *ghcb = svm->sev_es.ghcb;
Tom Lendacky291bd202020-12-10 11:09:47 -06002296
2297 /*
2298 * The GHCB protocol so far allows for the following data
2299 * to be returned:
2300 * GPRs RAX, RBX, RCX, RDX
2301 *
Sean Christopherson25009142021-01-22 15:50:47 -08002302 * Copy their values, even if they may not have been written during the
2303 * VM-Exit. It's the guest's responsibility to not consume random data.
Tom Lendacky291bd202020-12-10 11:09:47 -06002304 */
Sean Christopherson25009142021-01-22 15:50:47 -08002305 ghcb_set_rax(ghcb, vcpu->arch.regs[VCPU_REGS_RAX]);
2306 ghcb_set_rbx(ghcb, vcpu->arch.regs[VCPU_REGS_RBX]);
2307 ghcb_set_rcx(ghcb, vcpu->arch.regs[VCPU_REGS_RCX]);
2308 ghcb_set_rdx(ghcb, vcpu->arch.regs[VCPU_REGS_RDX]);
Tom Lendacky291bd202020-12-10 11:09:47 -06002309}
2310
2311static void sev_es_sync_from_ghcb(struct vcpu_svm *svm)
2312{
2313 struct vmcb_control_area *control = &svm->vmcb->control;
2314 struct kvm_vcpu *vcpu = &svm->vcpu;
Peter Gondab67a4cc2021-10-21 10:42:59 -07002315 struct ghcb *ghcb = svm->sev_es.ghcb;
Tom Lendacky291bd202020-12-10 11:09:47 -06002316 u64 exit_code;
2317
2318 /*
2319 * The GHCB protocol so far allows for the following data
2320 * to be supplied:
2321 * GPRs RAX, RBX, RCX, RDX
2322 * XCR0
2323 * CPL
2324 *
2325 * VMMCALL allows the guest to provide extra registers. KVM also
2326 * expects RSI for hypercalls, so include that, too.
2327 *
2328 * Copy their values to the appropriate location if supplied.
2329 */
2330 memset(vcpu->arch.regs, 0, sizeof(vcpu->arch.regs));
2331
2332 vcpu->arch.regs[VCPU_REGS_RAX] = ghcb_get_rax_if_valid(ghcb);
2333 vcpu->arch.regs[VCPU_REGS_RBX] = ghcb_get_rbx_if_valid(ghcb);
2334 vcpu->arch.regs[VCPU_REGS_RCX] = ghcb_get_rcx_if_valid(ghcb);
2335 vcpu->arch.regs[VCPU_REGS_RDX] = ghcb_get_rdx_if_valid(ghcb);
2336 vcpu->arch.regs[VCPU_REGS_RSI] = ghcb_get_rsi_if_valid(ghcb);
2337
2338 svm->vmcb->save.cpl = ghcb_get_cpl_if_valid(ghcb);
2339
2340 if (ghcb_xcr0_is_valid(ghcb)) {
2341 vcpu->arch.xcr0 = ghcb_get_xcr0(ghcb);
2342 kvm_update_cpuid_runtime(vcpu);
2343 }
2344
2345 /* Copy the GHCB exit information into the VMCB fields */
2346 exit_code = ghcb_get_sw_exit_code(ghcb);
2347 control->exit_code = lower_32_bits(exit_code);
2348 control->exit_code_hi = upper_32_bits(exit_code);
2349 control->exit_info_1 = ghcb_get_sw_exit_info_1(ghcb);
2350 control->exit_info_2 = ghcb_get_sw_exit_info_2(ghcb);
2351
2352 /* Clear the valid entries fields */
2353 memset(ghcb->save.valid_bitmap, 0, sizeof(ghcb->save.valid_bitmap));
2354}
2355
Tom Lendackyad5b3532021-12-02 12:52:05 -06002356static bool sev_es_validate_vmgexit(struct vcpu_svm *svm)
Tom Lendacky291bd202020-12-10 11:09:47 -06002357{
2358 struct kvm_vcpu *vcpu;
2359 struct ghcb *ghcb;
Tom Lendackyad5b3532021-12-02 12:52:05 -06002360 u64 exit_code;
2361 u64 reason;
Tom Lendacky291bd202020-12-10 11:09:47 -06002362
Peter Gondab67a4cc2021-10-21 10:42:59 -07002363 ghcb = svm->sev_es.ghcb;
Tom Lendacky291bd202020-12-10 11:09:47 -06002364
Tom Lendacky291bd202020-12-10 11:09:47 -06002365 /*
Tom Lendackyad5b3532021-12-02 12:52:05 -06002366 * Retrieve the exit code now even though it may not be marked valid
Tom Lendacky291bd202020-12-10 11:09:47 -06002367 * as it could help with debugging.
2368 */
2369 exit_code = ghcb_get_sw_exit_code(ghcb);
2370
Tom Lendackyad5b3532021-12-02 12:52:05 -06002371 /* Only GHCB Usage code 0 is supported */
2372 if (ghcb->ghcb_usage) {
2373 reason = GHCB_ERR_INVALID_USAGE;
2374 goto vmgexit_err;
2375 }
2376
2377 reason = GHCB_ERR_MISSING_INPUT;
2378
Tom Lendacky291bd202020-12-10 11:09:47 -06002379 if (!ghcb_sw_exit_code_is_valid(ghcb) ||
2380 !ghcb_sw_exit_info_1_is_valid(ghcb) ||
2381 !ghcb_sw_exit_info_2_is_valid(ghcb))
2382 goto vmgexit_err;
2383
2384 switch (ghcb_get_sw_exit_code(ghcb)) {
2385 case SVM_EXIT_READ_DR7:
2386 break;
2387 case SVM_EXIT_WRITE_DR7:
2388 if (!ghcb_rax_is_valid(ghcb))
2389 goto vmgexit_err;
2390 break;
2391 case SVM_EXIT_RDTSC:
2392 break;
2393 case SVM_EXIT_RDPMC:
2394 if (!ghcb_rcx_is_valid(ghcb))
2395 goto vmgexit_err;
2396 break;
2397 case SVM_EXIT_CPUID:
2398 if (!ghcb_rax_is_valid(ghcb) ||
2399 !ghcb_rcx_is_valid(ghcb))
2400 goto vmgexit_err;
2401 if (ghcb_get_rax(ghcb) == 0xd)
2402 if (!ghcb_xcr0_is_valid(ghcb))
2403 goto vmgexit_err;
2404 break;
2405 case SVM_EXIT_INVD:
2406 break;
2407 case SVM_EXIT_IOIO:
Tom Lendacky7ed9abf2020-12-10 11:09:54 -06002408 if (ghcb_get_sw_exit_info_1(ghcb) & SVM_IOIO_STR_MASK) {
2409 if (!ghcb_sw_scratch_is_valid(ghcb))
Tom Lendacky291bd202020-12-10 11:09:47 -06002410 goto vmgexit_err;
Tom Lendacky7ed9abf2020-12-10 11:09:54 -06002411 } else {
2412 if (!(ghcb_get_sw_exit_info_1(ghcb) & SVM_IOIO_TYPE_MASK))
2413 if (!ghcb_rax_is_valid(ghcb))
2414 goto vmgexit_err;
2415 }
Tom Lendacky291bd202020-12-10 11:09:47 -06002416 break;
2417 case SVM_EXIT_MSR:
2418 if (!ghcb_rcx_is_valid(ghcb))
2419 goto vmgexit_err;
2420 if (ghcb_get_sw_exit_info_1(ghcb)) {
2421 if (!ghcb_rax_is_valid(ghcb) ||
2422 !ghcb_rdx_is_valid(ghcb))
2423 goto vmgexit_err;
2424 }
2425 break;
2426 case SVM_EXIT_VMMCALL:
2427 if (!ghcb_rax_is_valid(ghcb) ||
2428 !ghcb_cpl_is_valid(ghcb))
2429 goto vmgexit_err;
2430 break;
2431 case SVM_EXIT_RDTSCP:
2432 break;
2433 case SVM_EXIT_WBINVD:
2434 break;
2435 case SVM_EXIT_MONITOR:
2436 if (!ghcb_rax_is_valid(ghcb) ||
2437 !ghcb_rcx_is_valid(ghcb) ||
2438 !ghcb_rdx_is_valid(ghcb))
2439 goto vmgexit_err;
2440 break;
2441 case SVM_EXIT_MWAIT:
2442 if (!ghcb_rax_is_valid(ghcb) ||
2443 !ghcb_rcx_is_valid(ghcb))
2444 goto vmgexit_err;
2445 break;
Tom Lendacky8f423a82020-12-10 11:09:53 -06002446 case SVM_VMGEXIT_MMIO_READ:
2447 case SVM_VMGEXIT_MMIO_WRITE:
2448 if (!ghcb_sw_scratch_is_valid(ghcb))
2449 goto vmgexit_err;
2450 break;
Tom Lendacky4444dfe2020-12-14 11:16:03 -05002451 case SVM_VMGEXIT_NMI_COMPLETE:
Tom Lendacky647daca2021-01-04 14:20:01 -06002452 case SVM_VMGEXIT_AP_HLT_LOOP:
Tom Lendacky8640ca52020-12-15 12:44:07 -05002453 case SVM_VMGEXIT_AP_JUMP_TABLE:
Tom Lendacky291bd202020-12-10 11:09:47 -06002454 case SVM_VMGEXIT_UNSUPPORTED_EVENT:
2455 break;
2456 default:
Tom Lendackyad5b3532021-12-02 12:52:05 -06002457 reason = GHCB_ERR_INVALID_EVENT;
Tom Lendacky291bd202020-12-10 11:09:47 -06002458 goto vmgexit_err;
2459 }
2460
Tom Lendackyad5b3532021-12-02 12:52:05 -06002461 return true;
Tom Lendacky291bd202020-12-10 11:09:47 -06002462
2463vmgexit_err:
2464 vcpu = &svm->vcpu;
2465
Tom Lendackyad5b3532021-12-02 12:52:05 -06002466 if (reason == GHCB_ERR_INVALID_USAGE) {
Tom Lendacky291bd202020-12-10 11:09:47 -06002467 vcpu_unimpl(vcpu, "vmgexit: ghcb usage %#x is not valid\n",
2468 ghcb->ghcb_usage);
Tom Lendackyad5b3532021-12-02 12:52:05 -06002469 } else if (reason == GHCB_ERR_INVALID_EVENT) {
2470 vcpu_unimpl(vcpu, "vmgexit: exit code %#llx is not valid\n",
2471 exit_code);
Tom Lendacky291bd202020-12-10 11:09:47 -06002472 } else {
Tom Lendackyad5b3532021-12-02 12:52:05 -06002473 vcpu_unimpl(vcpu, "vmgexit: exit code %#llx input is not valid\n",
Tom Lendacky291bd202020-12-10 11:09:47 -06002474 exit_code);
2475 dump_ghcb(svm);
2476 }
2477
Tom Lendackyad5b3532021-12-02 12:52:05 -06002478 /* Clear the valid entries fields */
2479 memset(ghcb->save.valid_bitmap, 0, sizeof(ghcb->save.valid_bitmap));
Tom Lendacky291bd202020-12-10 11:09:47 -06002480
Tom Lendackyad5b3532021-12-02 12:52:05 -06002481 ghcb_set_sw_exit_info_1(ghcb, 2);
2482 ghcb_set_sw_exit_info_2(ghcb, reason);
2483
2484 return false;
Tom Lendacky291bd202020-12-10 11:09:47 -06002485}
2486
Tom Lendackyce7ea0c2021-05-06 15:14:41 -05002487void sev_es_unmap_ghcb(struct vcpu_svm *svm)
Tom Lendacky291bd202020-12-10 11:09:47 -06002488{
Peter Gondab67a4cc2021-10-21 10:42:59 -07002489 if (!svm->sev_es.ghcb)
Tom Lendacky291bd202020-12-10 11:09:47 -06002490 return;
2491
Peter Gondab67a4cc2021-10-21 10:42:59 -07002492 if (svm->sev_es.ghcb_sa_free) {
Tom Lendacky8f423a82020-12-10 11:09:53 -06002493 /*
2494 * The scratch area lives outside the GHCB, so there is a
2495 * buffer that, depending on the operation performed, may
2496 * need to be synced, then freed.
2497 */
Peter Gondab67a4cc2021-10-21 10:42:59 -07002498 if (svm->sev_es.ghcb_sa_sync) {
Tom Lendacky8f423a82020-12-10 11:09:53 -06002499 kvm_write_guest(svm->vcpu.kvm,
Peter Gondab67a4cc2021-10-21 10:42:59 -07002500 ghcb_get_sw_scratch(svm->sev_es.ghcb),
2501 svm->sev_es.ghcb_sa,
2502 svm->sev_es.ghcb_sa_len);
2503 svm->sev_es.ghcb_sa_sync = false;
Tom Lendacky8f423a82020-12-10 11:09:53 -06002504 }
2505
Sean Christophersona6552762021-11-09 22:23:50 +00002506 kvfree(svm->sev_es.ghcb_sa);
Peter Gondab67a4cc2021-10-21 10:42:59 -07002507 svm->sev_es.ghcb_sa = NULL;
2508 svm->sev_es.ghcb_sa_free = false;
Tom Lendacky8f423a82020-12-10 11:09:53 -06002509 }
2510
Peter Gondab67a4cc2021-10-21 10:42:59 -07002511 trace_kvm_vmgexit_exit(svm->vcpu.vcpu_id, svm->sev_es.ghcb);
Tom Lendackyd523ab6b2020-12-10 11:09:48 -06002512
Tom Lendacky291bd202020-12-10 11:09:47 -06002513 sev_es_sync_to_ghcb(svm);
2514
Peter Gondab67a4cc2021-10-21 10:42:59 -07002515 kvm_vcpu_unmap(&svm->vcpu, &svm->sev_es.ghcb_map, true);
2516 svm->sev_es.ghcb = NULL;
Tom Lendacky291bd202020-12-10 11:09:47 -06002517}
2518
Joerg Roedeleaf78262020-03-24 10:41:54 +01002519void pre_sev_run(struct vcpu_svm *svm, int cpu)
2520{
2521 struct svm_cpu_data *sd = per_cpu(svm_data, cpu);
2522 int asid = sev_get_asid(svm->vcpu.kvm);
2523
2524 /* Assign the asid allocated with this SEV guest */
Paolo Bonzinidee734a2020-11-30 09:39:59 -05002525 svm->asid = asid;
Joerg Roedeleaf78262020-03-24 10:41:54 +01002526
2527 /*
2528 * Flush guest TLB:
2529 *
2530 * 1) when different VMCB for the same ASID is to be run on the same host CPU.
2531 * 2) or this VMCB was executed on different host CPU in previous VMRUNs.
2532 */
2533 if (sd->sev_vmcbs[asid] == svm->vmcb &&
Jim Mattson8a14fe42020-06-03 16:56:22 -07002534 svm->vcpu.arch.last_vmentry_cpu == cpu)
Joerg Roedeleaf78262020-03-24 10:41:54 +01002535 return;
2536
Joerg Roedeleaf78262020-03-24 10:41:54 +01002537 sd->sev_vmcbs[asid] = svm->vmcb;
2538 svm->vmcb->control.tlb_ctl = TLB_CONTROL_FLUSH_ASID;
Joerg Roedel06e78522020-06-25 10:03:23 +02002539 vmcb_mark_dirty(svm->vmcb, VMCB_ASID);
Joerg Roedeleaf78262020-03-24 10:41:54 +01002540}
Tom Lendacky291bd202020-12-10 11:09:47 -06002541
Tom Lendacky8f423a82020-12-10 11:09:53 -06002542#define GHCB_SCRATCH_AREA_LIMIT (16ULL * PAGE_SIZE)
Tom Lendackyad5b3532021-12-02 12:52:05 -06002543static bool setup_vmgexit_scratch(struct vcpu_svm *svm, bool sync, u64 len)
Tom Lendacky8f423a82020-12-10 11:09:53 -06002544{
2545 struct vmcb_control_area *control = &svm->vmcb->control;
Peter Gondab67a4cc2021-10-21 10:42:59 -07002546 struct ghcb *ghcb = svm->sev_es.ghcb;
Tom Lendacky8f423a82020-12-10 11:09:53 -06002547 u64 ghcb_scratch_beg, ghcb_scratch_end;
2548 u64 scratch_gpa_beg, scratch_gpa_end;
2549 void *scratch_va;
2550
2551 scratch_gpa_beg = ghcb_get_sw_scratch(ghcb);
2552 if (!scratch_gpa_beg) {
2553 pr_err("vmgexit: scratch gpa not provided\n");
Tom Lendackyad5b3532021-12-02 12:52:05 -06002554 goto e_scratch;
Tom Lendacky8f423a82020-12-10 11:09:53 -06002555 }
2556
2557 scratch_gpa_end = scratch_gpa_beg + len;
2558 if (scratch_gpa_end < scratch_gpa_beg) {
2559 pr_err("vmgexit: scratch length (%#llx) not valid for scratch address (%#llx)\n",
2560 len, scratch_gpa_beg);
Tom Lendackyad5b3532021-12-02 12:52:05 -06002561 goto e_scratch;
Tom Lendacky8f423a82020-12-10 11:09:53 -06002562 }
2563
2564 if ((scratch_gpa_beg & PAGE_MASK) == control->ghcb_gpa) {
2565 /* Scratch area begins within GHCB */
2566 ghcb_scratch_beg = control->ghcb_gpa +
2567 offsetof(struct ghcb, shared_buffer);
2568 ghcb_scratch_end = control->ghcb_gpa +
2569 offsetof(struct ghcb, reserved_1);
2570
2571 /*
2572 * If the scratch area begins within the GHCB, it must be
2573 * completely contained in the GHCB shared buffer area.
2574 */
2575 if (scratch_gpa_beg < ghcb_scratch_beg ||
2576 scratch_gpa_end > ghcb_scratch_end) {
2577 pr_err("vmgexit: scratch area is outside of GHCB shared buffer area (%#llx - %#llx)\n",
2578 scratch_gpa_beg, scratch_gpa_end);
Tom Lendackyad5b3532021-12-02 12:52:05 -06002579 goto e_scratch;
Tom Lendacky8f423a82020-12-10 11:09:53 -06002580 }
2581
Peter Gondab67a4cc2021-10-21 10:42:59 -07002582 scratch_va = (void *)svm->sev_es.ghcb;
Tom Lendacky8f423a82020-12-10 11:09:53 -06002583 scratch_va += (scratch_gpa_beg - control->ghcb_gpa);
2584 } else {
2585 /*
2586 * The guest memory must be read into a kernel buffer, so
2587 * limit the size
2588 */
2589 if (len > GHCB_SCRATCH_AREA_LIMIT) {
2590 pr_err("vmgexit: scratch area exceeds KVM limits (%#llx requested, %#llx limit)\n",
2591 len, GHCB_SCRATCH_AREA_LIMIT);
Tom Lendackyad5b3532021-12-02 12:52:05 -06002592 goto e_scratch;
Tom Lendacky8f423a82020-12-10 11:09:53 -06002593 }
Sean Christophersona6552762021-11-09 22:23:50 +00002594 scratch_va = kvzalloc(len, GFP_KERNEL_ACCOUNT);
Tom Lendacky8f423a82020-12-10 11:09:53 -06002595 if (!scratch_va)
Tom Lendackyad5b3532021-12-02 12:52:05 -06002596 goto e_scratch;
Tom Lendacky8f423a82020-12-10 11:09:53 -06002597
2598 if (kvm_read_guest(svm->vcpu.kvm, scratch_gpa_beg, scratch_va, len)) {
2599 /* Unable to copy scratch area from guest */
2600 pr_err("vmgexit: kvm_read_guest for scratch area failed\n");
2601
Sean Christophersona6552762021-11-09 22:23:50 +00002602 kvfree(scratch_va);
Tom Lendackyad5b3532021-12-02 12:52:05 -06002603 goto e_scratch;
Tom Lendacky8f423a82020-12-10 11:09:53 -06002604 }
2605
2606 /*
2607 * The scratch area is outside the GHCB. The operation will
2608 * dictate whether the buffer needs to be synced before running
2609 * the vCPU next time (i.e. a read was requested so the data
2610 * must be written back to the guest memory).
2611 */
Peter Gondab67a4cc2021-10-21 10:42:59 -07002612 svm->sev_es.ghcb_sa_sync = sync;
2613 svm->sev_es.ghcb_sa_free = true;
Tom Lendacky8f423a82020-12-10 11:09:53 -06002614 }
2615
Peter Gondab67a4cc2021-10-21 10:42:59 -07002616 svm->sev_es.ghcb_sa = scratch_va;
2617 svm->sev_es.ghcb_sa_len = len;
Tom Lendacky8f423a82020-12-10 11:09:53 -06002618
Tom Lendackyad5b3532021-12-02 12:52:05 -06002619 return true;
2620
2621e_scratch:
2622 ghcb_set_sw_exit_info_1(ghcb, 2);
2623 ghcb_set_sw_exit_info_2(ghcb, GHCB_ERR_INVALID_SCRATCH_AREA);
2624
2625 return false;
Tom Lendacky8f423a82020-12-10 11:09:53 -06002626}
2627
Tom Lendackyd3694662020-12-10 11:09:50 -06002628static void set_ghcb_msr_bits(struct vcpu_svm *svm, u64 value, u64 mask,
2629 unsigned int pos)
2630{
2631 svm->vmcb->control.ghcb_gpa &= ~(mask << pos);
2632 svm->vmcb->control.ghcb_gpa |= (value & mask) << pos;
2633}
2634
2635static u64 get_ghcb_msr_bits(struct vcpu_svm *svm, u64 mask, unsigned int pos)
2636{
2637 return (svm->vmcb->control.ghcb_gpa >> pos) & mask;
2638}
2639
Tom Lendacky1edc1452020-12-10 11:09:49 -06002640static void set_ghcb_msr(struct vcpu_svm *svm, u64 value)
2641{
2642 svm->vmcb->control.ghcb_gpa = value;
2643}
2644
Tom Lendacky291bd202020-12-10 11:09:47 -06002645static int sev_handle_vmgexit_msr_protocol(struct vcpu_svm *svm)
2646{
Tom Lendacky1edc1452020-12-10 11:09:49 -06002647 struct vmcb_control_area *control = &svm->vmcb->control;
Tom Lendackyd3694662020-12-10 11:09:50 -06002648 struct kvm_vcpu *vcpu = &svm->vcpu;
Tom Lendacky1edc1452020-12-10 11:09:49 -06002649 u64 ghcb_info;
Tom Lendackyd3694662020-12-10 11:09:50 -06002650 int ret = 1;
Tom Lendacky1edc1452020-12-10 11:09:49 -06002651
2652 ghcb_info = control->ghcb_gpa & GHCB_MSR_INFO_MASK;
2653
Tom Lendacky59e38b52020-12-10 11:09:52 -06002654 trace_kvm_vmgexit_msr_protocol_enter(svm->vcpu.vcpu_id,
2655 control->ghcb_gpa);
2656
Tom Lendacky1edc1452020-12-10 11:09:49 -06002657 switch (ghcb_info) {
2658 case GHCB_MSR_SEV_INFO_REQ:
2659 set_ghcb_msr(svm, GHCB_MSR_SEV_INFO(GHCB_VERSION_MAX,
2660 GHCB_VERSION_MIN,
2661 sev_enc_bit));
2662 break;
Tom Lendackyd3694662020-12-10 11:09:50 -06002663 case GHCB_MSR_CPUID_REQ: {
2664 u64 cpuid_fn, cpuid_reg, cpuid_value;
2665
2666 cpuid_fn = get_ghcb_msr_bits(svm,
2667 GHCB_MSR_CPUID_FUNC_MASK,
2668 GHCB_MSR_CPUID_FUNC_POS);
2669
2670 /* Initialize the registers needed by the CPUID intercept */
2671 vcpu->arch.regs[VCPU_REGS_RAX] = cpuid_fn;
2672 vcpu->arch.regs[VCPU_REGS_RCX] = 0;
2673
Paolo Bonzini63129752021-03-02 14:40:39 -05002674 ret = svm_invoke_exit_handler(vcpu, SVM_EXIT_CPUID);
Tom Lendackyd3694662020-12-10 11:09:50 -06002675 if (!ret) {
Tom Lendackyad5b3532021-12-02 12:52:05 -06002676 /* Error, keep GHCB MSR value as-is */
Tom Lendackyd3694662020-12-10 11:09:50 -06002677 break;
2678 }
2679
2680 cpuid_reg = get_ghcb_msr_bits(svm,
2681 GHCB_MSR_CPUID_REG_MASK,
2682 GHCB_MSR_CPUID_REG_POS);
2683 if (cpuid_reg == 0)
2684 cpuid_value = vcpu->arch.regs[VCPU_REGS_RAX];
2685 else if (cpuid_reg == 1)
2686 cpuid_value = vcpu->arch.regs[VCPU_REGS_RBX];
2687 else if (cpuid_reg == 2)
2688 cpuid_value = vcpu->arch.regs[VCPU_REGS_RCX];
2689 else
2690 cpuid_value = vcpu->arch.regs[VCPU_REGS_RDX];
2691
2692 set_ghcb_msr_bits(svm, cpuid_value,
2693 GHCB_MSR_CPUID_VALUE_MASK,
2694 GHCB_MSR_CPUID_VALUE_POS);
2695
2696 set_ghcb_msr_bits(svm, GHCB_MSR_CPUID_RESP,
2697 GHCB_MSR_INFO_MASK,
2698 GHCB_MSR_INFO_POS);
2699 break;
2700 }
Tom Lendackye1d71112020-12-10 11:09:51 -06002701 case GHCB_MSR_TERM_REQ: {
2702 u64 reason_set, reason_code;
2703
2704 reason_set = get_ghcb_msr_bits(svm,
2705 GHCB_MSR_TERM_REASON_SET_MASK,
2706 GHCB_MSR_TERM_REASON_SET_POS);
2707 reason_code = get_ghcb_msr_bits(svm,
2708 GHCB_MSR_TERM_REASON_MASK,
2709 GHCB_MSR_TERM_REASON_POS);
2710 pr_info("SEV-ES guest requested termination: %#llx:%#llx\n",
2711 reason_set, reason_code);
Tom Lendackyad5b3532021-12-02 12:52:05 -06002712
2713 ret = -EINVAL;
2714 break;
Tom Lendackye1d71112020-12-10 11:09:51 -06002715 }
Tom Lendacky1edc1452020-12-10 11:09:49 -06002716 default:
Tom Lendackyad5b3532021-12-02 12:52:05 -06002717 /* Error, keep GHCB MSR value as-is */
2718 break;
Tom Lendacky1edc1452020-12-10 11:09:49 -06002719 }
2720
Tom Lendacky59e38b52020-12-10 11:09:52 -06002721 trace_kvm_vmgexit_msr_protocol_exit(svm->vcpu.vcpu_id,
2722 control->ghcb_gpa, ret);
2723
Tom Lendackyd3694662020-12-10 11:09:50 -06002724 return ret;
Tom Lendacky291bd202020-12-10 11:09:47 -06002725}
2726
Paolo Bonzini63129752021-03-02 14:40:39 -05002727int sev_handle_vmgexit(struct kvm_vcpu *vcpu)
Tom Lendacky291bd202020-12-10 11:09:47 -06002728{
Paolo Bonzini63129752021-03-02 14:40:39 -05002729 struct vcpu_svm *svm = to_svm(vcpu);
Tom Lendacky291bd202020-12-10 11:09:47 -06002730 struct vmcb_control_area *control = &svm->vmcb->control;
2731 u64 ghcb_gpa, exit_code;
2732 struct ghcb *ghcb;
2733 int ret;
2734
2735 /* Validate the GHCB */
2736 ghcb_gpa = control->ghcb_gpa;
2737 if (ghcb_gpa & GHCB_MSR_INFO_MASK)
2738 return sev_handle_vmgexit_msr_protocol(svm);
2739
2740 if (!ghcb_gpa) {
Paolo Bonzini63129752021-03-02 14:40:39 -05002741 vcpu_unimpl(vcpu, "vmgexit: GHCB gpa is not set\n");
Tom Lendackyad5b3532021-12-02 12:52:05 -06002742
2743 /* Without a GHCB, just return right back to the guest */
2744 return 1;
Tom Lendacky291bd202020-12-10 11:09:47 -06002745 }
2746
Peter Gondab67a4cc2021-10-21 10:42:59 -07002747 if (kvm_vcpu_map(vcpu, ghcb_gpa >> PAGE_SHIFT, &svm->sev_es.ghcb_map)) {
Tom Lendacky291bd202020-12-10 11:09:47 -06002748 /* Unable to map GHCB from guest */
Paolo Bonzini63129752021-03-02 14:40:39 -05002749 vcpu_unimpl(vcpu, "vmgexit: error mapping GHCB [%#llx] from guest\n",
Tom Lendacky291bd202020-12-10 11:09:47 -06002750 ghcb_gpa);
Tom Lendackyad5b3532021-12-02 12:52:05 -06002751
2752 /* Without a GHCB, just return right back to the guest */
2753 return 1;
Tom Lendacky291bd202020-12-10 11:09:47 -06002754 }
2755
Peter Gondab67a4cc2021-10-21 10:42:59 -07002756 svm->sev_es.ghcb = svm->sev_es.ghcb_map.hva;
2757 ghcb = svm->sev_es.ghcb_map.hva;
Tom Lendacky291bd202020-12-10 11:09:47 -06002758
Paolo Bonzini63129752021-03-02 14:40:39 -05002759 trace_kvm_vmgexit_enter(vcpu->vcpu_id, ghcb);
Tom Lendackyd523ab6b2020-12-10 11:09:48 -06002760
Tom Lendacky291bd202020-12-10 11:09:47 -06002761 exit_code = ghcb_get_sw_exit_code(ghcb);
2762
Tom Lendackyad5b3532021-12-02 12:52:05 -06002763 if (!sev_es_validate_vmgexit(svm))
2764 return 1;
Tom Lendacky291bd202020-12-10 11:09:47 -06002765
2766 sev_es_sync_from_ghcb(svm);
2767 ghcb_set_sw_exit_info_1(ghcb, 0);
2768 ghcb_set_sw_exit_info_2(ghcb, 0);
2769
Tom Lendackyad5b3532021-12-02 12:52:05 -06002770 ret = 1;
Tom Lendacky291bd202020-12-10 11:09:47 -06002771 switch (exit_code) {
Tom Lendacky8f423a82020-12-10 11:09:53 -06002772 case SVM_VMGEXIT_MMIO_READ:
Tom Lendackyad5b3532021-12-02 12:52:05 -06002773 if (!setup_vmgexit_scratch(svm, true, control->exit_info_2))
Tom Lendacky8f423a82020-12-10 11:09:53 -06002774 break;
2775
Paolo Bonzini63129752021-03-02 14:40:39 -05002776 ret = kvm_sev_es_mmio_read(vcpu,
Tom Lendacky8f423a82020-12-10 11:09:53 -06002777 control->exit_info_1,
2778 control->exit_info_2,
Peter Gondab67a4cc2021-10-21 10:42:59 -07002779 svm->sev_es.ghcb_sa);
Tom Lendacky8f423a82020-12-10 11:09:53 -06002780 break;
2781 case SVM_VMGEXIT_MMIO_WRITE:
Tom Lendackyad5b3532021-12-02 12:52:05 -06002782 if (!setup_vmgexit_scratch(svm, false, control->exit_info_2))
Tom Lendacky8f423a82020-12-10 11:09:53 -06002783 break;
2784
Paolo Bonzini63129752021-03-02 14:40:39 -05002785 ret = kvm_sev_es_mmio_write(vcpu,
Tom Lendacky8f423a82020-12-10 11:09:53 -06002786 control->exit_info_1,
2787 control->exit_info_2,
Peter Gondab67a4cc2021-10-21 10:42:59 -07002788 svm->sev_es.ghcb_sa);
Tom Lendacky8f423a82020-12-10 11:09:53 -06002789 break;
Tom Lendacky4444dfe2020-12-14 11:16:03 -05002790 case SVM_VMGEXIT_NMI_COMPLETE:
Paolo Bonzini63129752021-03-02 14:40:39 -05002791 ret = svm_invoke_exit_handler(vcpu, SVM_EXIT_IRET);
Tom Lendacky4444dfe2020-12-14 11:16:03 -05002792 break;
Tom Lendacky647daca2021-01-04 14:20:01 -06002793 case SVM_VMGEXIT_AP_HLT_LOOP:
Paolo Bonzini63129752021-03-02 14:40:39 -05002794 ret = kvm_emulate_ap_reset_hold(vcpu);
Tom Lendacky647daca2021-01-04 14:20:01 -06002795 break;
Tom Lendacky8640ca52020-12-15 12:44:07 -05002796 case SVM_VMGEXIT_AP_JUMP_TABLE: {
Paolo Bonzini63129752021-03-02 14:40:39 -05002797 struct kvm_sev_info *sev = &to_kvm_svm(vcpu->kvm)->sev_info;
Tom Lendacky8640ca52020-12-15 12:44:07 -05002798
2799 switch (control->exit_info_1) {
2800 case 0:
2801 /* Set AP jump table address */
2802 sev->ap_jump_table = control->exit_info_2;
2803 break;
2804 case 1:
2805 /* Get AP jump table address */
2806 ghcb_set_sw_exit_info_2(ghcb, sev->ap_jump_table);
2807 break;
2808 default:
2809 pr_err("svm: vmgexit: unsupported AP jump table request - exit_info_1=%#llx\n",
2810 control->exit_info_1);
Tom Lendackyad5b3532021-12-02 12:52:05 -06002811 ghcb_set_sw_exit_info_1(ghcb, 2);
2812 ghcb_set_sw_exit_info_2(ghcb, GHCB_ERR_INVALID_INPUT);
Tom Lendacky8640ca52020-12-15 12:44:07 -05002813 }
2814
Tom Lendacky8640ca52020-12-15 12:44:07 -05002815 break;
2816 }
Tom Lendacky291bd202020-12-10 11:09:47 -06002817 case SVM_VMGEXIT_UNSUPPORTED_EVENT:
Paolo Bonzini63129752021-03-02 14:40:39 -05002818 vcpu_unimpl(vcpu,
Tom Lendacky291bd202020-12-10 11:09:47 -06002819 "vmgexit: unsupported event - exit_info_1=%#llx, exit_info_2=%#llx\n",
2820 control->exit_info_1, control->exit_info_2);
Sean Christopherson75236f52021-11-09 22:23:49 +00002821 ret = -EINVAL;
Tom Lendacky291bd202020-12-10 11:09:47 -06002822 break;
2823 default:
Paolo Bonzini63129752021-03-02 14:40:39 -05002824 ret = svm_invoke_exit_handler(vcpu, exit_code);
Tom Lendacky291bd202020-12-10 11:09:47 -06002825 }
2826
2827 return ret;
2828}
Tom Lendacky7ed9abf2020-12-10 11:09:54 -06002829
2830int sev_es_string_io(struct vcpu_svm *svm, int size, unsigned int port, int in)
2831{
Paolo Bonzini9b0971c2021-10-25 12:14:31 -04002832 int count;
2833 int bytes;
2834
2835 if (svm->vmcb->control.exit_info_2 > INT_MAX)
Tom Lendacky7ed9abf2020-12-10 11:09:54 -06002836 return -EINVAL;
2837
Paolo Bonzini9b0971c2021-10-25 12:14:31 -04002838 count = svm->vmcb->control.exit_info_2;
2839 if (unlikely(check_mul_overflow(count, size, &bytes)))
2840 return -EINVAL;
2841
Tom Lendackyad5b3532021-12-02 12:52:05 -06002842 if (!setup_vmgexit_scratch(svm, in, bytes))
2843 return 1;
Paolo Bonzini9b0971c2021-10-25 12:14:31 -04002844
Peter Gondab67a4cc2021-10-21 10:42:59 -07002845 return kvm_sev_es_string_io(&svm->vcpu, size, port, svm->sev_es.ghcb_sa,
Paolo Bonzini1f058332021-11-11 10:52:26 -05002846 count, in);
Tom Lendacky7ed9abf2020-12-10 11:09:54 -06002847}
Tom Lendacky376c6d22020-12-10 11:10:06 -06002848
2849void sev_es_init_vmcb(struct vcpu_svm *svm)
2850{
2851 struct kvm_vcpu *vcpu = &svm->vcpu;
2852
2853 svm->vmcb->control.nested_ctl |= SVM_NESTED_CTL_SEV_ES_ENABLE;
2854 svm->vmcb->control.virt_ext |= LBR_CTL_ENABLE_MASK;
2855
2856 /*
2857 * An SEV-ES guest requires a VMSA area that is a separate from the
2858 * VMCB page. Do not include the encryption mask on the VMSA physical
2859 * address since hardware will access it using the guest key.
2860 */
Peter Gondab67a4cc2021-10-21 10:42:59 -07002861 svm->vmcb->control.vmsa_pa = __pa(svm->sev_es.vmsa);
Tom Lendacky376c6d22020-12-10 11:10:06 -06002862
2863 /* Can't intercept CR register access, HV can't modify CR registers */
2864 svm_clr_intercept(svm, INTERCEPT_CR0_READ);
2865 svm_clr_intercept(svm, INTERCEPT_CR4_READ);
2866 svm_clr_intercept(svm, INTERCEPT_CR8_READ);
2867 svm_clr_intercept(svm, INTERCEPT_CR0_WRITE);
2868 svm_clr_intercept(svm, INTERCEPT_CR4_WRITE);
2869 svm_clr_intercept(svm, INTERCEPT_CR8_WRITE);
2870
2871 svm_clr_intercept(svm, INTERCEPT_SELECTIVE_CR0);
2872
2873 /* Track EFER/CR register changes */
2874 svm_set_intercept(svm, TRAP_EFER_WRITE);
2875 svm_set_intercept(svm, TRAP_CR0_WRITE);
2876 svm_set_intercept(svm, TRAP_CR4_WRITE);
2877 svm_set_intercept(svm, TRAP_CR8_WRITE);
2878
2879 /* No support for enable_vmware_backdoor */
2880 clr_exception_intercept(svm, GP_VECTOR);
2881
2882 /* Can't intercept XSETBV, HV can't modify XCR0 directly */
2883 svm_clr_intercept(svm, INTERCEPT_XSETBV);
2884
2885 /* Clear intercepts on selected MSRs */
2886 set_msr_interception(vcpu, svm->msrpm, MSR_EFER, 1, 1);
2887 set_msr_interception(vcpu, svm->msrpm, MSR_IA32_CR_PAT, 1, 1);
2888 set_msr_interception(vcpu, svm->msrpm, MSR_IA32_LASTBRANCHFROMIP, 1, 1);
2889 set_msr_interception(vcpu, svm->msrpm, MSR_IA32_LASTBRANCHTOIP, 1, 1);
2890 set_msr_interception(vcpu, svm->msrpm, MSR_IA32_LASTINTFROMIP, 1, 1);
2891 set_msr_interception(vcpu, svm->msrpm, MSR_IA32_LASTINTTOIP, 1, 1);
2892}
2893
Sean Christopherson9ebe5302021-09-20 17:03:02 -07002894void sev_es_vcpu_reset(struct vcpu_svm *svm)
Tom Lendacky376c6d22020-12-10 11:10:06 -06002895{
2896 /*
Sean Christopherson9ebe5302021-09-20 17:03:02 -07002897 * Set the GHCB MSR value as per the GHCB specification when emulating
2898 * vCPU RESET for an SEV-ES guest.
Tom Lendacky376c6d22020-12-10 11:10:06 -06002899 */
2900 set_ghcb_msr(svm, GHCB_MSR_SEV_INFO(GHCB_VERSION_MAX,
2901 GHCB_VERSION_MIN,
2902 sev_enc_bit));
2903}
Tom Lendacky86137772020-12-10 11:10:07 -06002904
Michael Rotha7fc06d2021-02-02 13:01:26 -06002905void sev_es_prepare_guest_switch(struct vcpu_svm *svm, unsigned int cpu)
Tom Lendacky86137772020-12-10 11:10:07 -06002906{
2907 struct svm_cpu_data *sd = per_cpu(svm_data, cpu);
2908 struct vmcb_save_area *hostsa;
Tom Lendacky86137772020-12-10 11:10:07 -06002909
2910 /*
2911 * As an SEV-ES guest, hardware will restore the host state on VMEXIT,
2912 * of which one step is to perform a VMLOAD. Since hardware does not
2913 * perform a VMSAVE on VMRUN, the host savearea must be updated.
2914 */
Sean Christopherson35a78312020-12-30 16:27:00 -08002915 vmsave(__sme_page_pa(sd->save_area));
Tom Lendacky86137772020-12-10 11:10:07 -06002916
Tom Lendacky86137772020-12-10 11:10:07 -06002917 /* XCR0 is restored on VMEXIT, save the current host value */
2918 hostsa = (struct vmcb_save_area *)(page_address(sd->save_area) + 0x400);
2919 hostsa->xcr0 = xgetbv(XCR_XFEATURE_ENABLED_MASK);
2920
Ingo Molnard9f6e122021-03-18 15:28:01 +01002921 /* PKRU is restored on VMEXIT, save the current host value */
Tom Lendacky86137772020-12-10 11:10:07 -06002922 hostsa->pkru = read_pkru();
2923
2924 /* MSR_IA32_XSS is restored on VMEXIT, save the currnet host value */
2925 hostsa->xss = host_xss;
2926}
2927
Tom Lendacky647daca2021-01-04 14:20:01 -06002928void sev_vcpu_deliver_sipi_vector(struct kvm_vcpu *vcpu, u8 vector)
2929{
2930 struct vcpu_svm *svm = to_svm(vcpu);
2931
2932 /* First SIPI: Use the values as initially set by the VMM */
Peter Gondab67a4cc2021-10-21 10:42:59 -07002933 if (!svm->sev_es.received_first_sipi) {
2934 svm->sev_es.received_first_sipi = true;
Tom Lendacky647daca2021-01-04 14:20:01 -06002935 return;
2936 }
2937
2938 /*
2939 * Subsequent SIPI: Return from an AP Reset Hold VMGEXIT, where
2940 * the guest will set the CS and RIP. Set SW_EXIT_INFO_2 to a
2941 * non-zero value.
2942 */
Peter Gondab67a4cc2021-10-21 10:42:59 -07002943 if (!svm->sev_es.ghcb)
Tom Lendackya3ba26e2021-04-09 09:38:42 -05002944 return;
2945
Peter Gondab67a4cc2021-10-21 10:42:59 -07002946 ghcb_set_sw_exit_info_2(svm->sev_es.ghcb, 1);
Tom Lendacky647daca2021-01-04 14:20:01 -06002947}