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