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> |
Tom Lendacky | add5e2f | 2020-12-10 11:09:40 -0600 | [diff] [blame] | 17 | #include <linux/processor.h> |
Joerg Roedel | eaf7826 | 2020-03-24 10:41:54 +0100 | [diff] [blame] | 18 | |
| 19 | #include "x86.h" |
| 20 | #include "svm.h" |
Tom Lendacky | 291bd20 | 2020-12-10 11:09:47 -0600 | [diff] [blame^] | 21 | #include "cpuid.h" |
Joerg Roedel | eaf7826 | 2020-03-24 10:41:54 +0100 | [diff] [blame] | 22 | |
| 23 | static int sev_flush_asids(void); |
| 24 | static DECLARE_RWSEM(sev_deactivate_lock); |
| 25 | static DEFINE_MUTEX(sev_bitmap_lock); |
| 26 | unsigned int max_sev_asid; |
| 27 | static unsigned int min_sev_asid; |
| 28 | static unsigned long *sev_asid_bitmap; |
| 29 | static unsigned long *sev_reclaim_asid_bitmap; |
| 30 | #define __sme_page_pa(x) __sme_set(page_to_pfn(x) << PAGE_SHIFT) |
| 31 | |
| 32 | struct enc_region { |
| 33 | struct list_head list; |
| 34 | unsigned long npages; |
| 35 | struct page **pages; |
| 36 | unsigned long uaddr; |
| 37 | unsigned long size; |
| 38 | }; |
| 39 | |
| 40 | static int sev_flush_asids(void) |
| 41 | { |
| 42 | int ret, error = 0; |
| 43 | |
| 44 | /* |
| 45 | * DEACTIVATE will clear the WBINVD indicator causing DF_FLUSH to fail, |
| 46 | * so it must be guarded. |
| 47 | */ |
| 48 | down_write(&sev_deactivate_lock); |
| 49 | |
| 50 | wbinvd_on_all_cpus(); |
| 51 | ret = sev_guest_df_flush(&error); |
| 52 | |
| 53 | up_write(&sev_deactivate_lock); |
| 54 | |
| 55 | if (ret) |
| 56 | pr_err("SEV: DF_FLUSH failed, ret=%d, error=%#x\n", ret, error); |
| 57 | |
| 58 | return ret; |
| 59 | } |
| 60 | |
| 61 | /* Must be called with the sev_bitmap_lock held */ |
| 62 | static bool __sev_recycle_asids(void) |
| 63 | { |
| 64 | int pos; |
| 65 | |
| 66 | /* Check if there are any ASIDs to reclaim before performing a flush */ |
| 67 | pos = find_next_bit(sev_reclaim_asid_bitmap, |
| 68 | max_sev_asid, min_sev_asid - 1); |
| 69 | if (pos >= max_sev_asid) |
| 70 | return false; |
| 71 | |
| 72 | if (sev_flush_asids()) |
| 73 | return false; |
| 74 | |
| 75 | bitmap_xor(sev_asid_bitmap, sev_asid_bitmap, sev_reclaim_asid_bitmap, |
| 76 | max_sev_asid); |
| 77 | bitmap_zero(sev_reclaim_asid_bitmap, max_sev_asid); |
| 78 | |
| 79 | return true; |
| 80 | } |
| 81 | |
| 82 | static int sev_asid_new(void) |
| 83 | { |
| 84 | bool retry = true; |
| 85 | int pos; |
| 86 | |
| 87 | mutex_lock(&sev_bitmap_lock); |
| 88 | |
| 89 | /* |
| 90 | * SEV-enabled guest must use asid from min_sev_asid to max_sev_asid. |
| 91 | */ |
| 92 | again: |
| 93 | pos = find_next_zero_bit(sev_asid_bitmap, max_sev_asid, min_sev_asid - 1); |
| 94 | if (pos >= max_sev_asid) { |
| 95 | if (retry && __sev_recycle_asids()) { |
| 96 | retry = false; |
| 97 | goto again; |
| 98 | } |
| 99 | mutex_unlock(&sev_bitmap_lock); |
| 100 | return -EBUSY; |
| 101 | } |
| 102 | |
| 103 | __set_bit(pos, sev_asid_bitmap); |
| 104 | |
| 105 | mutex_unlock(&sev_bitmap_lock); |
| 106 | |
| 107 | return pos + 1; |
| 108 | } |
| 109 | |
| 110 | static int sev_get_asid(struct kvm *kvm) |
| 111 | { |
| 112 | struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info; |
| 113 | |
| 114 | return sev->asid; |
| 115 | } |
| 116 | |
| 117 | static void sev_asid_free(int asid) |
| 118 | { |
| 119 | struct svm_cpu_data *sd; |
| 120 | int cpu, pos; |
| 121 | |
| 122 | mutex_lock(&sev_bitmap_lock); |
| 123 | |
| 124 | pos = asid - 1; |
| 125 | __set_bit(pos, sev_reclaim_asid_bitmap); |
| 126 | |
| 127 | for_each_possible_cpu(cpu) { |
| 128 | sd = per_cpu(svm_data, cpu); |
| 129 | sd->sev_vmcbs[pos] = NULL; |
| 130 | } |
| 131 | |
| 132 | mutex_unlock(&sev_bitmap_lock); |
| 133 | } |
| 134 | |
| 135 | static void sev_unbind_asid(struct kvm *kvm, unsigned int handle) |
| 136 | { |
| 137 | struct sev_data_decommission *decommission; |
| 138 | struct sev_data_deactivate *data; |
| 139 | |
| 140 | if (!handle) |
| 141 | return; |
| 142 | |
| 143 | data = kzalloc(sizeof(*data), GFP_KERNEL); |
| 144 | if (!data) |
| 145 | return; |
| 146 | |
| 147 | /* deactivate handle */ |
| 148 | data->handle = handle; |
| 149 | |
| 150 | /* Guard DEACTIVATE against WBINVD/DF_FLUSH used in ASID recycling */ |
| 151 | down_read(&sev_deactivate_lock); |
| 152 | sev_guest_deactivate(data, NULL); |
| 153 | up_read(&sev_deactivate_lock); |
| 154 | |
| 155 | kfree(data); |
| 156 | |
| 157 | decommission = kzalloc(sizeof(*decommission), GFP_KERNEL); |
| 158 | if (!decommission) |
| 159 | return; |
| 160 | |
| 161 | /* decommission handle */ |
| 162 | decommission->handle = handle; |
| 163 | sev_guest_decommission(decommission, NULL); |
| 164 | |
| 165 | kfree(decommission); |
| 166 | } |
| 167 | |
| 168 | static int sev_guest_init(struct kvm *kvm, struct kvm_sev_cmd *argp) |
| 169 | { |
| 170 | struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info; |
| 171 | int asid, ret; |
| 172 | |
| 173 | ret = -EBUSY; |
| 174 | if (unlikely(sev->active)) |
| 175 | return ret; |
| 176 | |
| 177 | asid = sev_asid_new(); |
| 178 | if (asid < 0) |
| 179 | return ret; |
| 180 | |
| 181 | ret = sev_platform_init(&argp->error); |
| 182 | if (ret) |
| 183 | goto e_free; |
| 184 | |
| 185 | sev->active = true; |
| 186 | sev->asid = asid; |
| 187 | INIT_LIST_HEAD(&sev->regions_list); |
| 188 | |
| 189 | return 0; |
| 190 | |
| 191 | e_free: |
| 192 | sev_asid_free(asid); |
| 193 | return ret; |
| 194 | } |
| 195 | |
| 196 | static int sev_bind_asid(struct kvm *kvm, unsigned int handle, int *error) |
| 197 | { |
| 198 | struct sev_data_activate *data; |
| 199 | int asid = sev_get_asid(kvm); |
| 200 | int ret; |
| 201 | |
| 202 | data = kzalloc(sizeof(*data), GFP_KERNEL_ACCOUNT); |
| 203 | if (!data) |
| 204 | return -ENOMEM; |
| 205 | |
| 206 | /* activate ASID on the given handle */ |
| 207 | data->handle = handle; |
| 208 | data->asid = asid; |
| 209 | ret = sev_guest_activate(data, error); |
| 210 | kfree(data); |
| 211 | |
| 212 | return ret; |
| 213 | } |
| 214 | |
| 215 | static int __sev_issue_cmd(int fd, int id, void *data, int *error) |
| 216 | { |
| 217 | struct fd f; |
| 218 | int ret; |
| 219 | |
| 220 | f = fdget(fd); |
| 221 | if (!f.file) |
| 222 | return -EBADF; |
| 223 | |
| 224 | ret = sev_issue_cmd_external_user(f.file, id, data, error); |
| 225 | |
| 226 | fdput(f); |
| 227 | return ret; |
| 228 | } |
| 229 | |
| 230 | static int sev_issue_cmd(struct kvm *kvm, int id, void *data, int *error) |
| 231 | { |
| 232 | struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info; |
| 233 | |
| 234 | return __sev_issue_cmd(sev->fd, id, data, error); |
| 235 | } |
| 236 | |
| 237 | static int sev_launch_start(struct kvm *kvm, struct kvm_sev_cmd *argp) |
| 238 | { |
| 239 | struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info; |
| 240 | struct sev_data_launch_start *start; |
| 241 | struct kvm_sev_launch_start params; |
| 242 | void *dh_blob, *session_blob; |
| 243 | int *error = &argp->error; |
| 244 | int ret; |
| 245 | |
| 246 | if (!sev_guest(kvm)) |
| 247 | return -ENOTTY; |
| 248 | |
| 249 | if (copy_from_user(¶ms, (void __user *)(uintptr_t)argp->data, sizeof(params))) |
| 250 | return -EFAULT; |
| 251 | |
| 252 | start = kzalloc(sizeof(*start), GFP_KERNEL_ACCOUNT); |
| 253 | if (!start) |
| 254 | return -ENOMEM; |
| 255 | |
| 256 | dh_blob = NULL; |
| 257 | if (params.dh_uaddr) { |
| 258 | dh_blob = psp_copy_user_blob(params.dh_uaddr, params.dh_len); |
| 259 | if (IS_ERR(dh_blob)) { |
| 260 | ret = PTR_ERR(dh_blob); |
| 261 | goto e_free; |
| 262 | } |
| 263 | |
| 264 | start->dh_cert_address = __sme_set(__pa(dh_blob)); |
| 265 | start->dh_cert_len = params.dh_len; |
| 266 | } |
| 267 | |
| 268 | session_blob = NULL; |
| 269 | if (params.session_uaddr) { |
| 270 | session_blob = psp_copy_user_blob(params.session_uaddr, params.session_len); |
| 271 | if (IS_ERR(session_blob)) { |
| 272 | ret = PTR_ERR(session_blob); |
| 273 | goto e_free_dh; |
| 274 | } |
| 275 | |
| 276 | start->session_address = __sme_set(__pa(session_blob)); |
| 277 | start->session_len = params.session_len; |
| 278 | } |
| 279 | |
| 280 | start->handle = params.handle; |
| 281 | start->policy = params.policy; |
| 282 | |
| 283 | /* create memory encryption context */ |
| 284 | ret = __sev_issue_cmd(argp->sev_fd, SEV_CMD_LAUNCH_START, start, error); |
| 285 | if (ret) |
| 286 | goto e_free_session; |
| 287 | |
| 288 | /* Bind ASID to this guest */ |
| 289 | ret = sev_bind_asid(kvm, start->handle, error); |
| 290 | if (ret) |
| 291 | goto e_free_session; |
| 292 | |
| 293 | /* return handle to userspace */ |
| 294 | params.handle = start->handle; |
| 295 | if (copy_to_user((void __user *)(uintptr_t)argp->data, ¶ms, sizeof(params))) { |
| 296 | sev_unbind_asid(kvm, start->handle); |
| 297 | ret = -EFAULT; |
| 298 | goto e_free_session; |
| 299 | } |
| 300 | |
| 301 | sev->handle = start->handle; |
| 302 | sev->fd = argp->sev_fd; |
| 303 | |
| 304 | e_free_session: |
| 305 | kfree(session_blob); |
| 306 | e_free_dh: |
| 307 | kfree(dh_blob); |
| 308 | e_free: |
| 309 | kfree(start); |
| 310 | return ret; |
| 311 | } |
| 312 | |
| 313 | static struct page **sev_pin_memory(struct kvm *kvm, unsigned long uaddr, |
| 314 | unsigned long ulen, unsigned long *n, |
| 315 | int write) |
| 316 | { |
| 317 | struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info; |
John Hubbard | 78824fa | 2020-05-25 23:22:06 -0700 | [diff] [blame] | 318 | unsigned long npages, size; |
| 319 | int npinned; |
Joerg Roedel | eaf7826 | 2020-03-24 10:41:54 +0100 | [diff] [blame] | 320 | unsigned long locked, lock_limit; |
| 321 | struct page **pages; |
| 322 | unsigned long first, last; |
Dan Carpenter | ff2bd9f | 2020-07-14 17:23:51 +0300 | [diff] [blame] | 323 | int ret; |
Joerg Roedel | eaf7826 | 2020-03-24 10:41:54 +0100 | [diff] [blame] | 324 | |
| 325 | if (ulen == 0 || uaddr + ulen < uaddr) |
Paolo Bonzini | a8d908b | 2020-06-23 05:12:24 -0400 | [diff] [blame] | 326 | return ERR_PTR(-EINVAL); |
Joerg Roedel | eaf7826 | 2020-03-24 10:41:54 +0100 | [diff] [blame] | 327 | |
| 328 | /* Calculate number of pages. */ |
| 329 | first = (uaddr & PAGE_MASK) >> PAGE_SHIFT; |
| 330 | last = ((uaddr + ulen - 1) & PAGE_MASK) >> PAGE_SHIFT; |
| 331 | npages = (last - first + 1); |
| 332 | |
| 333 | locked = sev->pages_locked + npages; |
| 334 | lock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT; |
| 335 | if (locked > lock_limit && !capable(CAP_IPC_LOCK)) { |
| 336 | 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] | 337 | return ERR_PTR(-ENOMEM); |
Joerg Roedel | eaf7826 | 2020-03-24 10:41:54 +0100 | [diff] [blame] | 338 | } |
| 339 | |
John Hubbard | 78824fa | 2020-05-25 23:22:06 -0700 | [diff] [blame] | 340 | if (WARN_ON_ONCE(npages > INT_MAX)) |
Paolo Bonzini | a8d908b | 2020-06-23 05:12:24 -0400 | [diff] [blame] | 341 | return ERR_PTR(-EINVAL); |
John Hubbard | 78824fa | 2020-05-25 23:22:06 -0700 | [diff] [blame] | 342 | |
Joerg Roedel | eaf7826 | 2020-03-24 10:41:54 +0100 | [diff] [blame] | 343 | /* Avoid using vmalloc for smaller buffers. */ |
| 344 | size = npages * sizeof(struct page *); |
| 345 | if (size > PAGE_SIZE) |
Christoph Hellwig | 88dca4c | 2020-06-01 21:51:40 -0700 | [diff] [blame] | 346 | pages = __vmalloc(size, GFP_KERNEL_ACCOUNT | __GFP_ZERO); |
Joerg Roedel | eaf7826 | 2020-03-24 10:41:54 +0100 | [diff] [blame] | 347 | else |
| 348 | pages = kmalloc(size, GFP_KERNEL_ACCOUNT); |
| 349 | |
| 350 | if (!pages) |
Paolo Bonzini | a8d908b | 2020-06-23 05:12:24 -0400 | [diff] [blame] | 351 | return ERR_PTR(-ENOMEM); |
Joerg Roedel | eaf7826 | 2020-03-24 10:41:54 +0100 | [diff] [blame] | 352 | |
| 353 | /* Pin the user virtual address. */ |
John Hubbard | dc42c8a | 2020-05-25 23:22:07 -0700 | [diff] [blame] | 354 | npinned = pin_user_pages_fast(uaddr, npages, write ? FOLL_WRITE : 0, pages); |
Joerg Roedel | eaf7826 | 2020-03-24 10:41:54 +0100 | [diff] [blame] | 355 | if (npinned != npages) { |
| 356 | pr_err("SEV: Failure locking %lu pages.\n", npages); |
Dan Carpenter | ff2bd9f | 2020-07-14 17:23:51 +0300 | [diff] [blame] | 357 | ret = -ENOMEM; |
Joerg Roedel | eaf7826 | 2020-03-24 10:41:54 +0100 | [diff] [blame] | 358 | goto err; |
| 359 | } |
| 360 | |
| 361 | *n = npages; |
| 362 | sev->pages_locked = locked; |
| 363 | |
| 364 | return pages; |
| 365 | |
| 366 | err: |
Dan Carpenter | ff2bd9f | 2020-07-14 17:23:51 +0300 | [diff] [blame] | 367 | if (npinned > 0) |
John Hubbard | dc42c8a | 2020-05-25 23:22:07 -0700 | [diff] [blame] | 368 | unpin_user_pages(pages, npinned); |
Joerg Roedel | eaf7826 | 2020-03-24 10:41:54 +0100 | [diff] [blame] | 369 | |
| 370 | kvfree(pages); |
Dan Carpenter | ff2bd9f | 2020-07-14 17:23:51 +0300 | [diff] [blame] | 371 | return ERR_PTR(ret); |
Joerg Roedel | eaf7826 | 2020-03-24 10:41:54 +0100 | [diff] [blame] | 372 | } |
| 373 | |
| 374 | static void sev_unpin_memory(struct kvm *kvm, struct page **pages, |
| 375 | unsigned long npages) |
| 376 | { |
| 377 | struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info; |
| 378 | |
John Hubbard | dc42c8a | 2020-05-25 23:22:07 -0700 | [diff] [blame] | 379 | unpin_user_pages(pages, npages); |
Joerg Roedel | eaf7826 | 2020-03-24 10:41:54 +0100 | [diff] [blame] | 380 | kvfree(pages); |
| 381 | sev->pages_locked -= npages; |
| 382 | } |
| 383 | |
| 384 | static void sev_clflush_pages(struct page *pages[], unsigned long npages) |
| 385 | { |
| 386 | uint8_t *page_virtual; |
| 387 | unsigned long i; |
| 388 | |
Krish Sadhukhan | e1ebb2b | 2020-09-17 21:20:38 +0000 | [diff] [blame] | 389 | if (this_cpu_has(X86_FEATURE_SME_COHERENT) || npages == 0 || |
| 390 | pages == NULL) |
Joerg Roedel | eaf7826 | 2020-03-24 10:41:54 +0100 | [diff] [blame] | 391 | return; |
| 392 | |
| 393 | for (i = 0; i < npages; i++) { |
| 394 | page_virtual = kmap_atomic(pages[i]); |
| 395 | clflush_cache_range(page_virtual, PAGE_SIZE); |
| 396 | kunmap_atomic(page_virtual); |
| 397 | } |
| 398 | } |
| 399 | |
| 400 | static unsigned long get_num_contig_pages(unsigned long idx, |
| 401 | struct page **inpages, unsigned long npages) |
| 402 | { |
| 403 | unsigned long paddr, next_paddr; |
| 404 | unsigned long i = idx + 1, pages = 1; |
| 405 | |
| 406 | /* find the number of contiguous pages starting from idx */ |
| 407 | paddr = __sme_page_pa(inpages[idx]); |
| 408 | while (i < npages) { |
| 409 | next_paddr = __sme_page_pa(inpages[i++]); |
| 410 | if ((paddr + PAGE_SIZE) == next_paddr) { |
| 411 | pages++; |
| 412 | paddr = next_paddr; |
| 413 | continue; |
| 414 | } |
| 415 | break; |
| 416 | } |
| 417 | |
| 418 | return pages; |
| 419 | } |
| 420 | |
| 421 | static int sev_launch_update_data(struct kvm *kvm, struct kvm_sev_cmd *argp) |
| 422 | { |
| 423 | unsigned long vaddr, vaddr_end, next_vaddr, npages, pages, size, i; |
| 424 | struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info; |
| 425 | struct kvm_sev_launch_update_data params; |
| 426 | struct sev_data_launch_update_data *data; |
| 427 | struct page **inpages; |
| 428 | int ret; |
| 429 | |
| 430 | if (!sev_guest(kvm)) |
| 431 | return -ENOTTY; |
| 432 | |
| 433 | if (copy_from_user(¶ms, (void __user *)(uintptr_t)argp->data, sizeof(params))) |
| 434 | return -EFAULT; |
| 435 | |
| 436 | data = kzalloc(sizeof(*data), GFP_KERNEL_ACCOUNT); |
| 437 | if (!data) |
| 438 | return -ENOMEM; |
| 439 | |
| 440 | vaddr = params.uaddr; |
| 441 | size = params.len; |
| 442 | vaddr_end = vaddr + size; |
| 443 | |
| 444 | /* Lock the user memory. */ |
| 445 | inpages = sev_pin_memory(kvm, vaddr, size, &npages, 1); |
Dan Carpenter | ff2bd9f | 2020-07-14 17:23:51 +0300 | [diff] [blame] | 446 | if (IS_ERR(inpages)) { |
| 447 | ret = PTR_ERR(inpages); |
Joerg Roedel | eaf7826 | 2020-03-24 10:41:54 +0100 | [diff] [blame] | 448 | goto e_free; |
| 449 | } |
| 450 | |
| 451 | /* |
Paolo Bonzini | 14e3dd8 | 2020-09-23 13:01:33 -0400 | [diff] [blame] | 452 | * Flush (on non-coherent CPUs) before LAUNCH_UPDATE encrypts pages in |
| 453 | * place; the cache may contain the data that was written unencrypted. |
Joerg Roedel | eaf7826 | 2020-03-24 10:41:54 +0100 | [diff] [blame] | 454 | */ |
| 455 | sev_clflush_pages(inpages, npages); |
| 456 | |
| 457 | for (i = 0; vaddr < vaddr_end; vaddr = next_vaddr, i += pages) { |
| 458 | int offset, len; |
| 459 | |
| 460 | /* |
| 461 | * If the user buffer is not page-aligned, calculate the offset |
| 462 | * within the page. |
| 463 | */ |
| 464 | offset = vaddr & (PAGE_SIZE - 1); |
| 465 | |
| 466 | /* Calculate the number of pages that can be encrypted in one go. */ |
| 467 | pages = get_num_contig_pages(i, inpages, npages); |
| 468 | |
| 469 | len = min_t(size_t, ((pages * PAGE_SIZE) - offset), size); |
| 470 | |
| 471 | data->handle = sev->handle; |
| 472 | data->len = len; |
| 473 | data->address = __sme_page_pa(inpages[i]) + offset; |
| 474 | ret = sev_issue_cmd(kvm, SEV_CMD_LAUNCH_UPDATE_DATA, data, &argp->error); |
| 475 | if (ret) |
| 476 | goto e_unpin; |
| 477 | |
| 478 | size -= len; |
| 479 | next_vaddr = vaddr + len; |
| 480 | } |
| 481 | |
| 482 | e_unpin: |
| 483 | /* content of memory is updated, mark pages dirty */ |
| 484 | for (i = 0; i < npages; i++) { |
| 485 | set_page_dirty_lock(inpages[i]); |
| 486 | mark_page_accessed(inpages[i]); |
| 487 | } |
| 488 | /* unlock the user pages */ |
| 489 | sev_unpin_memory(kvm, inpages, npages); |
| 490 | e_free: |
| 491 | kfree(data); |
| 492 | return ret; |
| 493 | } |
| 494 | |
| 495 | static int sev_launch_measure(struct kvm *kvm, struct kvm_sev_cmd *argp) |
| 496 | { |
| 497 | void __user *measure = (void __user *)(uintptr_t)argp->data; |
| 498 | struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info; |
| 499 | struct sev_data_launch_measure *data; |
| 500 | struct kvm_sev_launch_measure params; |
| 501 | void __user *p = NULL; |
| 502 | void *blob = NULL; |
| 503 | int ret; |
| 504 | |
| 505 | if (!sev_guest(kvm)) |
| 506 | return -ENOTTY; |
| 507 | |
| 508 | if (copy_from_user(¶ms, measure, sizeof(params))) |
| 509 | return -EFAULT; |
| 510 | |
| 511 | data = kzalloc(sizeof(*data), GFP_KERNEL_ACCOUNT); |
| 512 | if (!data) |
| 513 | return -ENOMEM; |
| 514 | |
| 515 | /* User wants to query the blob length */ |
| 516 | if (!params.len) |
| 517 | goto cmd; |
| 518 | |
| 519 | p = (void __user *)(uintptr_t)params.uaddr; |
| 520 | if (p) { |
| 521 | if (params.len > SEV_FW_BLOB_MAX_SIZE) { |
| 522 | ret = -EINVAL; |
| 523 | goto e_free; |
| 524 | } |
| 525 | |
| 526 | ret = -ENOMEM; |
| 527 | blob = kmalloc(params.len, GFP_KERNEL); |
| 528 | if (!blob) |
| 529 | goto e_free; |
| 530 | |
| 531 | data->address = __psp_pa(blob); |
| 532 | data->len = params.len; |
| 533 | } |
| 534 | |
| 535 | cmd: |
| 536 | data->handle = sev->handle; |
| 537 | ret = sev_issue_cmd(kvm, SEV_CMD_LAUNCH_MEASURE, data, &argp->error); |
| 538 | |
| 539 | /* |
| 540 | * If we query the session length, FW responded with expected data. |
| 541 | */ |
| 542 | if (!params.len) |
| 543 | goto done; |
| 544 | |
| 545 | if (ret) |
| 546 | goto e_free_blob; |
| 547 | |
| 548 | if (blob) { |
| 549 | if (copy_to_user(p, blob, params.len)) |
| 550 | ret = -EFAULT; |
| 551 | } |
| 552 | |
| 553 | done: |
| 554 | params.len = data->len; |
| 555 | if (copy_to_user(measure, ¶ms, sizeof(params))) |
| 556 | ret = -EFAULT; |
| 557 | e_free_blob: |
| 558 | kfree(blob); |
| 559 | e_free: |
| 560 | kfree(data); |
| 561 | return ret; |
| 562 | } |
| 563 | |
| 564 | static int sev_launch_finish(struct kvm *kvm, struct kvm_sev_cmd *argp) |
| 565 | { |
| 566 | struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info; |
| 567 | struct sev_data_launch_finish *data; |
| 568 | int ret; |
| 569 | |
| 570 | if (!sev_guest(kvm)) |
| 571 | return -ENOTTY; |
| 572 | |
| 573 | data = kzalloc(sizeof(*data), GFP_KERNEL_ACCOUNT); |
| 574 | if (!data) |
| 575 | return -ENOMEM; |
| 576 | |
| 577 | data->handle = sev->handle; |
| 578 | ret = sev_issue_cmd(kvm, SEV_CMD_LAUNCH_FINISH, data, &argp->error); |
| 579 | |
| 580 | kfree(data); |
| 581 | return ret; |
| 582 | } |
| 583 | |
| 584 | static int sev_guest_status(struct kvm *kvm, struct kvm_sev_cmd *argp) |
| 585 | { |
| 586 | struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info; |
| 587 | struct kvm_sev_guest_status params; |
| 588 | struct sev_data_guest_status *data; |
| 589 | int ret; |
| 590 | |
| 591 | if (!sev_guest(kvm)) |
| 592 | return -ENOTTY; |
| 593 | |
| 594 | data = kzalloc(sizeof(*data), GFP_KERNEL_ACCOUNT); |
| 595 | if (!data) |
| 596 | return -ENOMEM; |
| 597 | |
| 598 | data->handle = sev->handle; |
| 599 | ret = sev_issue_cmd(kvm, SEV_CMD_GUEST_STATUS, data, &argp->error); |
| 600 | if (ret) |
| 601 | goto e_free; |
| 602 | |
| 603 | params.policy = data->policy; |
| 604 | params.state = data->state; |
| 605 | params.handle = data->handle; |
| 606 | |
| 607 | if (copy_to_user((void __user *)(uintptr_t)argp->data, ¶ms, sizeof(params))) |
| 608 | ret = -EFAULT; |
| 609 | e_free: |
| 610 | kfree(data); |
| 611 | return ret; |
| 612 | } |
| 613 | |
| 614 | static int __sev_issue_dbg_cmd(struct kvm *kvm, unsigned long src, |
| 615 | unsigned long dst, int size, |
| 616 | int *error, bool enc) |
| 617 | { |
| 618 | struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info; |
| 619 | struct sev_data_dbg *data; |
| 620 | int ret; |
| 621 | |
| 622 | data = kzalloc(sizeof(*data), GFP_KERNEL_ACCOUNT); |
| 623 | if (!data) |
| 624 | return -ENOMEM; |
| 625 | |
| 626 | data->handle = sev->handle; |
| 627 | data->dst_addr = dst; |
| 628 | data->src_addr = src; |
| 629 | data->len = size; |
| 630 | |
| 631 | ret = sev_issue_cmd(kvm, |
| 632 | enc ? SEV_CMD_DBG_ENCRYPT : SEV_CMD_DBG_DECRYPT, |
| 633 | data, error); |
| 634 | kfree(data); |
| 635 | return ret; |
| 636 | } |
| 637 | |
| 638 | static int __sev_dbg_decrypt(struct kvm *kvm, unsigned long src_paddr, |
| 639 | unsigned long dst_paddr, int sz, int *err) |
| 640 | { |
| 641 | int offset; |
| 642 | |
| 643 | /* |
| 644 | * Its safe to read more than we are asked, caller should ensure that |
| 645 | * destination has enough space. |
| 646 | */ |
| 647 | src_paddr = round_down(src_paddr, 16); |
| 648 | offset = src_paddr & 15; |
| 649 | sz = round_up(sz + offset, 16); |
| 650 | |
| 651 | return __sev_issue_dbg_cmd(kvm, src_paddr, dst_paddr, sz, err, false); |
| 652 | } |
| 653 | |
| 654 | static int __sev_dbg_decrypt_user(struct kvm *kvm, unsigned long paddr, |
| 655 | unsigned long __user dst_uaddr, |
| 656 | unsigned long dst_paddr, |
| 657 | int size, int *err) |
| 658 | { |
| 659 | struct page *tpage = NULL; |
| 660 | int ret, offset; |
| 661 | |
| 662 | /* if inputs are not 16-byte then use intermediate buffer */ |
| 663 | if (!IS_ALIGNED(dst_paddr, 16) || |
| 664 | !IS_ALIGNED(paddr, 16) || |
| 665 | !IS_ALIGNED(size, 16)) { |
| 666 | tpage = (void *)alloc_page(GFP_KERNEL); |
| 667 | if (!tpage) |
| 668 | return -ENOMEM; |
| 669 | |
| 670 | dst_paddr = __sme_page_pa(tpage); |
| 671 | } |
| 672 | |
| 673 | ret = __sev_dbg_decrypt(kvm, paddr, dst_paddr, size, err); |
| 674 | if (ret) |
| 675 | goto e_free; |
| 676 | |
| 677 | if (tpage) { |
| 678 | offset = paddr & 15; |
| 679 | if (copy_to_user((void __user *)(uintptr_t)dst_uaddr, |
| 680 | page_address(tpage) + offset, size)) |
| 681 | ret = -EFAULT; |
| 682 | } |
| 683 | |
| 684 | e_free: |
| 685 | if (tpage) |
| 686 | __free_page(tpage); |
| 687 | |
| 688 | return ret; |
| 689 | } |
| 690 | |
| 691 | static int __sev_dbg_encrypt_user(struct kvm *kvm, unsigned long paddr, |
| 692 | unsigned long __user vaddr, |
| 693 | unsigned long dst_paddr, |
| 694 | unsigned long __user dst_vaddr, |
| 695 | int size, int *error) |
| 696 | { |
| 697 | struct page *src_tpage = NULL; |
| 698 | struct page *dst_tpage = NULL; |
| 699 | int ret, len = size; |
| 700 | |
| 701 | /* If source buffer is not aligned then use an intermediate buffer */ |
| 702 | if (!IS_ALIGNED(vaddr, 16)) { |
| 703 | src_tpage = alloc_page(GFP_KERNEL); |
| 704 | if (!src_tpage) |
| 705 | return -ENOMEM; |
| 706 | |
| 707 | if (copy_from_user(page_address(src_tpage), |
| 708 | (void __user *)(uintptr_t)vaddr, size)) { |
| 709 | __free_page(src_tpage); |
| 710 | return -EFAULT; |
| 711 | } |
| 712 | |
| 713 | paddr = __sme_page_pa(src_tpage); |
| 714 | } |
| 715 | |
| 716 | /* |
| 717 | * If destination buffer or length is not aligned then do read-modify-write: |
| 718 | * - decrypt destination in an intermediate buffer |
| 719 | * - copy the source buffer in an intermediate buffer |
| 720 | * - use the intermediate buffer as source buffer |
| 721 | */ |
| 722 | if (!IS_ALIGNED(dst_vaddr, 16) || !IS_ALIGNED(size, 16)) { |
| 723 | int dst_offset; |
| 724 | |
| 725 | dst_tpage = alloc_page(GFP_KERNEL); |
| 726 | if (!dst_tpage) { |
| 727 | ret = -ENOMEM; |
| 728 | goto e_free; |
| 729 | } |
| 730 | |
| 731 | ret = __sev_dbg_decrypt(kvm, dst_paddr, |
| 732 | __sme_page_pa(dst_tpage), size, error); |
| 733 | if (ret) |
| 734 | goto e_free; |
| 735 | |
| 736 | /* |
| 737 | * If source is kernel buffer then use memcpy() otherwise |
| 738 | * copy_from_user(). |
| 739 | */ |
| 740 | dst_offset = dst_paddr & 15; |
| 741 | |
| 742 | if (src_tpage) |
| 743 | memcpy(page_address(dst_tpage) + dst_offset, |
| 744 | page_address(src_tpage), size); |
| 745 | else { |
| 746 | if (copy_from_user(page_address(dst_tpage) + dst_offset, |
| 747 | (void __user *)(uintptr_t)vaddr, size)) { |
| 748 | ret = -EFAULT; |
| 749 | goto e_free; |
| 750 | } |
| 751 | } |
| 752 | |
| 753 | paddr = __sme_page_pa(dst_tpage); |
| 754 | dst_paddr = round_down(dst_paddr, 16); |
| 755 | len = round_up(size, 16); |
| 756 | } |
| 757 | |
| 758 | ret = __sev_issue_dbg_cmd(kvm, paddr, dst_paddr, len, error, true); |
| 759 | |
| 760 | e_free: |
| 761 | if (src_tpage) |
| 762 | __free_page(src_tpage); |
| 763 | if (dst_tpage) |
| 764 | __free_page(dst_tpage); |
| 765 | return ret; |
| 766 | } |
| 767 | |
| 768 | static int sev_dbg_crypt(struct kvm *kvm, struct kvm_sev_cmd *argp, bool dec) |
| 769 | { |
| 770 | unsigned long vaddr, vaddr_end, next_vaddr; |
| 771 | unsigned long dst_vaddr; |
| 772 | struct page **src_p, **dst_p; |
| 773 | struct kvm_sev_dbg debug; |
| 774 | unsigned long n; |
| 775 | unsigned int size; |
| 776 | int ret; |
| 777 | |
| 778 | if (!sev_guest(kvm)) |
| 779 | return -ENOTTY; |
| 780 | |
| 781 | if (copy_from_user(&debug, (void __user *)(uintptr_t)argp->data, sizeof(debug))) |
| 782 | return -EFAULT; |
| 783 | |
| 784 | if (!debug.len || debug.src_uaddr + debug.len < debug.src_uaddr) |
| 785 | return -EINVAL; |
| 786 | if (!debug.dst_uaddr) |
| 787 | return -EINVAL; |
| 788 | |
| 789 | vaddr = debug.src_uaddr; |
| 790 | size = debug.len; |
| 791 | vaddr_end = vaddr + size; |
| 792 | dst_vaddr = debug.dst_uaddr; |
| 793 | |
| 794 | for (; vaddr < vaddr_end; vaddr = next_vaddr) { |
| 795 | int len, s_off, d_off; |
| 796 | |
| 797 | /* lock userspace source and destination page */ |
| 798 | 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] | 799 | if (IS_ERR(src_p)) |
| 800 | return PTR_ERR(src_p); |
Joerg Roedel | eaf7826 | 2020-03-24 10:41:54 +0100 | [diff] [blame] | 801 | |
| 802 | 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] | 803 | if (IS_ERR(dst_p)) { |
Joerg Roedel | eaf7826 | 2020-03-24 10:41:54 +0100 | [diff] [blame] | 804 | sev_unpin_memory(kvm, src_p, n); |
Dan Carpenter | ff2bd9f | 2020-07-14 17:23:51 +0300 | [diff] [blame] | 805 | return PTR_ERR(dst_p); |
Joerg Roedel | eaf7826 | 2020-03-24 10:41:54 +0100 | [diff] [blame] | 806 | } |
| 807 | |
| 808 | /* |
Paolo Bonzini | 14e3dd8 | 2020-09-23 13:01:33 -0400 | [diff] [blame] | 809 | * Flush (on non-coherent CPUs) before DBG_{DE,EN}CRYPT read or modify |
| 810 | * the pages; flush the destination too so that future accesses do not |
| 811 | * see stale data. |
Joerg Roedel | eaf7826 | 2020-03-24 10:41:54 +0100 | [diff] [blame] | 812 | */ |
| 813 | sev_clflush_pages(src_p, 1); |
| 814 | sev_clflush_pages(dst_p, 1); |
| 815 | |
| 816 | /* |
| 817 | * Since user buffer may not be page aligned, calculate the |
| 818 | * offset within the page. |
| 819 | */ |
| 820 | s_off = vaddr & ~PAGE_MASK; |
| 821 | d_off = dst_vaddr & ~PAGE_MASK; |
| 822 | len = min_t(size_t, (PAGE_SIZE - s_off), size); |
| 823 | |
| 824 | if (dec) |
| 825 | ret = __sev_dbg_decrypt_user(kvm, |
| 826 | __sme_page_pa(src_p[0]) + s_off, |
| 827 | dst_vaddr, |
| 828 | __sme_page_pa(dst_p[0]) + d_off, |
| 829 | len, &argp->error); |
| 830 | else |
| 831 | ret = __sev_dbg_encrypt_user(kvm, |
| 832 | __sme_page_pa(src_p[0]) + s_off, |
| 833 | vaddr, |
| 834 | __sme_page_pa(dst_p[0]) + d_off, |
| 835 | dst_vaddr, |
| 836 | len, &argp->error); |
| 837 | |
| 838 | sev_unpin_memory(kvm, src_p, n); |
| 839 | sev_unpin_memory(kvm, dst_p, n); |
| 840 | |
| 841 | if (ret) |
| 842 | goto err; |
| 843 | |
| 844 | next_vaddr = vaddr + len; |
| 845 | dst_vaddr = dst_vaddr + len; |
| 846 | size -= len; |
| 847 | } |
| 848 | err: |
| 849 | return ret; |
| 850 | } |
| 851 | |
| 852 | static int sev_launch_secret(struct kvm *kvm, struct kvm_sev_cmd *argp) |
| 853 | { |
| 854 | struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info; |
| 855 | struct sev_data_launch_secret *data; |
| 856 | struct kvm_sev_launch_secret params; |
| 857 | struct page **pages; |
| 858 | void *blob, *hdr; |
Cfir Cohen | 50085be | 2020-08-07 17:37:46 -0700 | [diff] [blame] | 859 | unsigned long n, i; |
Joerg Roedel | eaf7826 | 2020-03-24 10:41:54 +0100 | [diff] [blame] | 860 | int ret, offset; |
| 861 | |
| 862 | if (!sev_guest(kvm)) |
| 863 | return -ENOTTY; |
| 864 | |
| 865 | if (copy_from_user(¶ms, (void __user *)(uintptr_t)argp->data, sizeof(params))) |
| 866 | return -EFAULT; |
| 867 | |
| 868 | 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] | 869 | if (IS_ERR(pages)) |
| 870 | return PTR_ERR(pages); |
Joerg Roedel | eaf7826 | 2020-03-24 10:41:54 +0100 | [diff] [blame] | 871 | |
| 872 | /* |
Paolo Bonzini | 14e3dd8 | 2020-09-23 13:01:33 -0400 | [diff] [blame] | 873 | * Flush (on non-coherent CPUs) before LAUNCH_SECRET encrypts pages in |
| 874 | * place; the cache may contain the data that was written unencrypted. |
Cfir Cohen | 50085be | 2020-08-07 17:37:46 -0700 | [diff] [blame] | 875 | */ |
| 876 | sev_clflush_pages(pages, n); |
| 877 | |
| 878 | /* |
Joerg Roedel | eaf7826 | 2020-03-24 10:41:54 +0100 | [diff] [blame] | 879 | * The secret must be copied into contiguous memory region, lets verify |
| 880 | * that userspace memory pages are contiguous before we issue command. |
| 881 | */ |
| 882 | if (get_num_contig_pages(0, pages, n) != n) { |
| 883 | ret = -EINVAL; |
| 884 | goto e_unpin_memory; |
| 885 | } |
| 886 | |
| 887 | ret = -ENOMEM; |
| 888 | data = kzalloc(sizeof(*data), GFP_KERNEL_ACCOUNT); |
| 889 | if (!data) |
| 890 | goto e_unpin_memory; |
| 891 | |
| 892 | offset = params.guest_uaddr & (PAGE_SIZE - 1); |
| 893 | data->guest_address = __sme_page_pa(pages[0]) + offset; |
| 894 | data->guest_len = params.guest_len; |
| 895 | |
| 896 | blob = psp_copy_user_blob(params.trans_uaddr, params.trans_len); |
| 897 | if (IS_ERR(blob)) { |
| 898 | ret = PTR_ERR(blob); |
| 899 | goto e_free; |
| 900 | } |
| 901 | |
| 902 | data->trans_address = __psp_pa(blob); |
| 903 | data->trans_len = params.trans_len; |
| 904 | |
| 905 | hdr = psp_copy_user_blob(params.hdr_uaddr, params.hdr_len); |
| 906 | if (IS_ERR(hdr)) { |
| 907 | ret = PTR_ERR(hdr); |
| 908 | goto e_free_blob; |
| 909 | } |
| 910 | data->hdr_address = __psp_pa(hdr); |
| 911 | data->hdr_len = params.hdr_len; |
| 912 | |
| 913 | data->handle = sev->handle; |
| 914 | ret = sev_issue_cmd(kvm, SEV_CMD_LAUNCH_UPDATE_SECRET, data, &argp->error); |
| 915 | |
| 916 | kfree(hdr); |
| 917 | |
| 918 | e_free_blob: |
| 919 | kfree(blob); |
| 920 | e_free: |
| 921 | kfree(data); |
| 922 | e_unpin_memory: |
Cfir Cohen | 50085be | 2020-08-07 17:37:46 -0700 | [diff] [blame] | 923 | /* content of memory is updated, mark pages dirty */ |
| 924 | for (i = 0; i < n; i++) { |
| 925 | set_page_dirty_lock(pages[i]); |
| 926 | mark_page_accessed(pages[i]); |
| 927 | } |
Joerg Roedel | eaf7826 | 2020-03-24 10:41:54 +0100 | [diff] [blame] | 928 | sev_unpin_memory(kvm, pages, n); |
| 929 | return ret; |
| 930 | } |
| 931 | |
| 932 | int svm_mem_enc_op(struct kvm *kvm, void __user *argp) |
| 933 | { |
| 934 | struct kvm_sev_cmd sev_cmd; |
| 935 | int r; |
| 936 | |
Tom Lendacky | 916391a | 2020-12-10 11:09:38 -0600 | [diff] [blame] | 937 | if (!svm_sev_enabled() || !sev) |
Joerg Roedel | eaf7826 | 2020-03-24 10:41:54 +0100 | [diff] [blame] | 938 | return -ENOTTY; |
| 939 | |
| 940 | if (!argp) |
| 941 | return 0; |
| 942 | |
| 943 | if (copy_from_user(&sev_cmd, argp, sizeof(struct kvm_sev_cmd))) |
| 944 | return -EFAULT; |
| 945 | |
| 946 | mutex_lock(&kvm->lock); |
| 947 | |
| 948 | switch (sev_cmd.id) { |
| 949 | case KVM_SEV_INIT: |
| 950 | r = sev_guest_init(kvm, &sev_cmd); |
| 951 | break; |
| 952 | case KVM_SEV_LAUNCH_START: |
| 953 | r = sev_launch_start(kvm, &sev_cmd); |
| 954 | break; |
| 955 | case KVM_SEV_LAUNCH_UPDATE_DATA: |
| 956 | r = sev_launch_update_data(kvm, &sev_cmd); |
| 957 | break; |
| 958 | case KVM_SEV_LAUNCH_MEASURE: |
| 959 | r = sev_launch_measure(kvm, &sev_cmd); |
| 960 | break; |
| 961 | case KVM_SEV_LAUNCH_FINISH: |
| 962 | r = sev_launch_finish(kvm, &sev_cmd); |
| 963 | break; |
| 964 | case KVM_SEV_GUEST_STATUS: |
| 965 | r = sev_guest_status(kvm, &sev_cmd); |
| 966 | break; |
| 967 | case KVM_SEV_DBG_DECRYPT: |
| 968 | r = sev_dbg_crypt(kvm, &sev_cmd, true); |
| 969 | break; |
| 970 | case KVM_SEV_DBG_ENCRYPT: |
| 971 | r = sev_dbg_crypt(kvm, &sev_cmd, false); |
| 972 | break; |
| 973 | case KVM_SEV_LAUNCH_SECRET: |
| 974 | r = sev_launch_secret(kvm, &sev_cmd); |
| 975 | break; |
| 976 | default: |
| 977 | r = -EINVAL; |
| 978 | goto out; |
| 979 | } |
| 980 | |
| 981 | if (copy_to_user(argp, &sev_cmd, sizeof(struct kvm_sev_cmd))) |
| 982 | r = -EFAULT; |
| 983 | |
| 984 | out: |
| 985 | mutex_unlock(&kvm->lock); |
| 986 | return r; |
| 987 | } |
| 988 | |
| 989 | int svm_register_enc_region(struct kvm *kvm, |
| 990 | struct kvm_enc_region *range) |
| 991 | { |
| 992 | struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info; |
| 993 | struct enc_region *region; |
| 994 | int ret = 0; |
| 995 | |
| 996 | if (!sev_guest(kvm)) |
| 997 | return -ENOTTY; |
| 998 | |
| 999 | if (range->addr > ULONG_MAX || range->size > ULONG_MAX) |
| 1000 | return -EINVAL; |
| 1001 | |
| 1002 | region = kzalloc(sizeof(*region), GFP_KERNEL_ACCOUNT); |
| 1003 | if (!region) |
| 1004 | return -ENOMEM; |
| 1005 | |
| 1006 | 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] | 1007 | if (IS_ERR(region->pages)) { |
| 1008 | ret = PTR_ERR(region->pages); |
Joerg Roedel | eaf7826 | 2020-03-24 10:41:54 +0100 | [diff] [blame] | 1009 | goto e_free; |
| 1010 | } |
| 1011 | |
| 1012 | /* |
| 1013 | * The guest may change the memory encryption attribute from C=0 -> C=1 |
| 1014 | * or vice versa for this memory range. Lets make sure caches are |
| 1015 | * flushed to ensure that guest data gets written into memory with |
| 1016 | * correct C-bit. |
| 1017 | */ |
| 1018 | sev_clflush_pages(region->pages, region->npages); |
| 1019 | |
| 1020 | region->uaddr = range->addr; |
| 1021 | region->size = range->size; |
| 1022 | |
| 1023 | mutex_lock(&kvm->lock); |
| 1024 | list_add_tail(®ion->list, &sev->regions_list); |
| 1025 | mutex_unlock(&kvm->lock); |
| 1026 | |
| 1027 | return ret; |
| 1028 | |
| 1029 | e_free: |
| 1030 | kfree(region); |
| 1031 | return ret; |
| 1032 | } |
| 1033 | |
| 1034 | static struct enc_region * |
| 1035 | find_enc_region(struct kvm *kvm, struct kvm_enc_region *range) |
| 1036 | { |
| 1037 | struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info; |
| 1038 | struct list_head *head = &sev->regions_list; |
| 1039 | struct enc_region *i; |
| 1040 | |
| 1041 | list_for_each_entry(i, head, list) { |
| 1042 | if (i->uaddr == range->addr && |
| 1043 | i->size == range->size) |
| 1044 | return i; |
| 1045 | } |
| 1046 | |
| 1047 | return NULL; |
| 1048 | } |
| 1049 | |
| 1050 | static void __unregister_enc_region_locked(struct kvm *kvm, |
| 1051 | struct enc_region *region) |
| 1052 | { |
| 1053 | sev_unpin_memory(kvm, region->pages, region->npages); |
| 1054 | list_del(®ion->list); |
| 1055 | kfree(region); |
| 1056 | } |
| 1057 | |
| 1058 | int svm_unregister_enc_region(struct kvm *kvm, |
| 1059 | struct kvm_enc_region *range) |
| 1060 | { |
| 1061 | struct enc_region *region; |
| 1062 | int ret; |
| 1063 | |
| 1064 | mutex_lock(&kvm->lock); |
| 1065 | |
| 1066 | if (!sev_guest(kvm)) { |
| 1067 | ret = -ENOTTY; |
| 1068 | goto failed; |
| 1069 | } |
| 1070 | |
| 1071 | region = find_enc_region(kvm, range); |
| 1072 | if (!region) { |
| 1073 | ret = -EINVAL; |
| 1074 | goto failed; |
| 1075 | } |
| 1076 | |
| 1077 | /* |
| 1078 | * Ensure that all guest tagged cache entries are flushed before |
| 1079 | * releasing the pages back to the system for use. CLFLUSH will |
| 1080 | * not do this, so issue a WBINVD. |
| 1081 | */ |
| 1082 | wbinvd_on_all_cpus(); |
| 1083 | |
| 1084 | __unregister_enc_region_locked(kvm, region); |
| 1085 | |
| 1086 | mutex_unlock(&kvm->lock); |
| 1087 | return 0; |
| 1088 | |
| 1089 | failed: |
| 1090 | mutex_unlock(&kvm->lock); |
| 1091 | return ret; |
| 1092 | } |
| 1093 | |
| 1094 | void sev_vm_destroy(struct kvm *kvm) |
| 1095 | { |
| 1096 | struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info; |
| 1097 | struct list_head *head = &sev->regions_list; |
| 1098 | struct list_head *pos, *q; |
| 1099 | |
| 1100 | if (!sev_guest(kvm)) |
| 1101 | return; |
| 1102 | |
| 1103 | mutex_lock(&kvm->lock); |
| 1104 | |
| 1105 | /* |
| 1106 | * Ensure that all guest tagged cache entries are flushed before |
| 1107 | * releasing the pages back to the system for use. CLFLUSH will |
| 1108 | * not do this, so issue a WBINVD. |
| 1109 | */ |
| 1110 | wbinvd_on_all_cpus(); |
| 1111 | |
| 1112 | /* |
| 1113 | * if userspace was terminated before unregistering the memory regions |
| 1114 | * then lets unpin all the registered memory. |
| 1115 | */ |
| 1116 | if (!list_empty(head)) { |
| 1117 | list_for_each_safe(pos, q, head) { |
| 1118 | __unregister_enc_region_locked(kvm, |
| 1119 | list_entry(pos, struct enc_region, list)); |
David Rientjes | 7be7494 | 2020-08-25 12:56:28 -0700 | [diff] [blame] | 1120 | cond_resched(); |
Joerg Roedel | eaf7826 | 2020-03-24 10:41:54 +0100 | [diff] [blame] | 1121 | } |
| 1122 | } |
| 1123 | |
| 1124 | mutex_unlock(&kvm->lock); |
| 1125 | |
| 1126 | sev_unbind_asid(kvm, sev->handle); |
| 1127 | sev_asid_free(sev->asid); |
| 1128 | } |
| 1129 | |
Tom Lendacky | 916391a | 2020-12-10 11:09:38 -0600 | [diff] [blame] | 1130 | void __init sev_hardware_setup(void) |
Joerg Roedel | eaf7826 | 2020-03-24 10:41:54 +0100 | [diff] [blame] | 1131 | { |
Tom Lendacky | 916391a | 2020-12-10 11:09:38 -0600 | [diff] [blame] | 1132 | unsigned int eax, ebx, ecx, edx; |
| 1133 | bool sev_es_supported = false; |
| 1134 | bool sev_supported = false; |
| 1135 | |
| 1136 | /* Does the CPU support SEV? */ |
| 1137 | if (!boot_cpu_has(X86_FEATURE_SEV)) |
| 1138 | goto out; |
| 1139 | |
| 1140 | /* Retrieve SEV CPUID information */ |
| 1141 | cpuid(0x8000001f, &eax, &ebx, &ecx, &edx); |
| 1142 | |
Joerg Roedel | eaf7826 | 2020-03-24 10:41:54 +0100 | [diff] [blame] | 1143 | /* Maximum number of encrypted guests supported simultaneously */ |
Tom Lendacky | 916391a | 2020-12-10 11:09:38 -0600 | [diff] [blame] | 1144 | max_sev_asid = ecx; |
Joerg Roedel | eaf7826 | 2020-03-24 10:41:54 +0100 | [diff] [blame] | 1145 | |
Paolo Bonzini | 9ef1530 | 2020-04-13 03:20:06 -0400 | [diff] [blame] | 1146 | if (!svm_sev_enabled()) |
Tom Lendacky | 916391a | 2020-12-10 11:09:38 -0600 | [diff] [blame] | 1147 | goto out; |
Joerg Roedel | eaf7826 | 2020-03-24 10:41:54 +0100 | [diff] [blame] | 1148 | |
| 1149 | /* Minimum ASID value that should be used for SEV guest */ |
Tom Lendacky | 916391a | 2020-12-10 11:09:38 -0600 | [diff] [blame] | 1150 | min_sev_asid = edx; |
Joerg Roedel | eaf7826 | 2020-03-24 10:41:54 +0100 | [diff] [blame] | 1151 | |
| 1152 | /* Initialize SEV ASID bitmaps */ |
| 1153 | sev_asid_bitmap = bitmap_zalloc(max_sev_asid, GFP_KERNEL); |
| 1154 | if (!sev_asid_bitmap) |
Tom Lendacky | 916391a | 2020-12-10 11:09:38 -0600 | [diff] [blame] | 1155 | goto out; |
Joerg Roedel | eaf7826 | 2020-03-24 10:41:54 +0100 | [diff] [blame] | 1156 | |
| 1157 | sev_reclaim_asid_bitmap = bitmap_zalloc(max_sev_asid, GFP_KERNEL); |
| 1158 | if (!sev_reclaim_asid_bitmap) |
Tom Lendacky | 916391a | 2020-12-10 11:09:38 -0600 | [diff] [blame] | 1159 | goto out; |
Joerg Roedel | eaf7826 | 2020-03-24 10:41:54 +0100 | [diff] [blame] | 1160 | |
Tom Lendacky | 916391a | 2020-12-10 11:09:38 -0600 | [diff] [blame] | 1161 | pr_info("SEV supported: %u ASIDs\n", max_sev_asid - min_sev_asid + 1); |
| 1162 | sev_supported = true; |
Joerg Roedel | eaf7826 | 2020-03-24 10:41:54 +0100 | [diff] [blame] | 1163 | |
Tom Lendacky | 916391a | 2020-12-10 11:09:38 -0600 | [diff] [blame] | 1164 | /* SEV-ES support requested? */ |
| 1165 | if (!sev_es) |
| 1166 | goto out; |
| 1167 | |
| 1168 | /* Does the CPU support SEV-ES? */ |
| 1169 | if (!boot_cpu_has(X86_FEATURE_SEV_ES)) |
| 1170 | goto out; |
| 1171 | |
| 1172 | /* Has the system been allocated ASIDs for SEV-ES? */ |
| 1173 | if (min_sev_asid == 1) |
| 1174 | goto out; |
| 1175 | |
| 1176 | pr_info("SEV-ES supported: %u ASIDs\n", min_sev_asid - 1); |
| 1177 | sev_es_supported = true; |
| 1178 | |
| 1179 | out: |
| 1180 | sev = sev_supported; |
| 1181 | sev_es = sev_es_supported; |
Joerg Roedel | eaf7826 | 2020-03-24 10:41:54 +0100 | [diff] [blame] | 1182 | } |
| 1183 | |
| 1184 | void sev_hardware_teardown(void) |
| 1185 | { |
Paolo Bonzini | 9ef1530 | 2020-04-13 03:20:06 -0400 | [diff] [blame] | 1186 | if (!svm_sev_enabled()) |
| 1187 | return; |
| 1188 | |
Joerg Roedel | eaf7826 | 2020-03-24 10:41:54 +0100 | [diff] [blame] | 1189 | bitmap_free(sev_asid_bitmap); |
| 1190 | bitmap_free(sev_reclaim_asid_bitmap); |
| 1191 | |
| 1192 | sev_flush_asids(); |
| 1193 | } |
| 1194 | |
Tom Lendacky | add5e2f | 2020-12-10 11:09:40 -0600 | [diff] [blame] | 1195 | /* |
| 1196 | * Pages used by hardware to hold guest encrypted state must be flushed before |
| 1197 | * returning them to the system. |
| 1198 | */ |
| 1199 | static void sev_flush_guest_memory(struct vcpu_svm *svm, void *va, |
| 1200 | unsigned long len) |
| 1201 | { |
| 1202 | /* |
| 1203 | * If hardware enforced cache coherency for encrypted mappings of the |
| 1204 | * same physical page is supported, nothing to do. |
| 1205 | */ |
| 1206 | if (boot_cpu_has(X86_FEATURE_SME_COHERENT)) |
| 1207 | return; |
| 1208 | |
| 1209 | /* |
| 1210 | * If the VM Page Flush MSR is supported, use it to flush the page |
| 1211 | * (using the page virtual address and the guest ASID). |
| 1212 | */ |
| 1213 | if (boot_cpu_has(X86_FEATURE_VM_PAGE_FLUSH)) { |
| 1214 | struct kvm_sev_info *sev; |
| 1215 | unsigned long va_start; |
| 1216 | u64 start, stop; |
| 1217 | |
| 1218 | /* Align start and stop to page boundaries. */ |
| 1219 | va_start = (unsigned long)va; |
| 1220 | start = (u64)va_start & PAGE_MASK; |
| 1221 | stop = PAGE_ALIGN((u64)va_start + len); |
| 1222 | |
| 1223 | if (start < stop) { |
| 1224 | sev = &to_kvm_svm(svm->vcpu.kvm)->sev_info; |
| 1225 | |
| 1226 | while (start < stop) { |
| 1227 | wrmsrl(MSR_AMD64_VM_PAGE_FLUSH, |
| 1228 | start | sev->asid); |
| 1229 | |
| 1230 | start += PAGE_SIZE; |
| 1231 | } |
| 1232 | |
| 1233 | return; |
| 1234 | } |
| 1235 | |
| 1236 | WARN(1, "Address overflow, using WBINVD\n"); |
| 1237 | } |
| 1238 | |
| 1239 | /* |
| 1240 | * Hardware should always have one of the above features, |
| 1241 | * but if not, use WBINVD and issue a warning. |
| 1242 | */ |
| 1243 | WARN_ONCE(1, "Using WBINVD to flush guest memory\n"); |
| 1244 | wbinvd_on_all_cpus(); |
| 1245 | } |
| 1246 | |
| 1247 | void sev_free_vcpu(struct kvm_vcpu *vcpu) |
| 1248 | { |
| 1249 | struct vcpu_svm *svm; |
| 1250 | |
| 1251 | if (!sev_es_guest(vcpu->kvm)) |
| 1252 | return; |
| 1253 | |
| 1254 | svm = to_svm(vcpu); |
| 1255 | |
| 1256 | if (vcpu->arch.guest_state_protected) |
| 1257 | sev_flush_guest_memory(svm, svm->vmsa, PAGE_SIZE); |
| 1258 | __free_page(virt_to_page(svm->vmsa)); |
| 1259 | } |
| 1260 | |
Tom Lendacky | 291bd20 | 2020-12-10 11:09:47 -0600 | [diff] [blame^] | 1261 | static void dump_ghcb(struct vcpu_svm *svm) |
| 1262 | { |
| 1263 | struct ghcb *ghcb = svm->ghcb; |
| 1264 | unsigned int nbits; |
| 1265 | |
| 1266 | /* Re-use the dump_invalid_vmcb module parameter */ |
| 1267 | if (!dump_invalid_vmcb) { |
| 1268 | pr_warn_ratelimited("set kvm_amd.dump_invalid_vmcb=1 to dump internal KVM state.\n"); |
| 1269 | return; |
| 1270 | } |
| 1271 | |
| 1272 | nbits = sizeof(ghcb->save.valid_bitmap) * 8; |
| 1273 | |
| 1274 | pr_err("GHCB (GPA=%016llx):\n", svm->vmcb->control.ghcb_gpa); |
| 1275 | pr_err("%-20s%016llx is_valid: %u\n", "sw_exit_code", |
| 1276 | ghcb->save.sw_exit_code, ghcb_sw_exit_code_is_valid(ghcb)); |
| 1277 | pr_err("%-20s%016llx is_valid: %u\n", "sw_exit_info_1", |
| 1278 | ghcb->save.sw_exit_info_1, ghcb_sw_exit_info_1_is_valid(ghcb)); |
| 1279 | pr_err("%-20s%016llx is_valid: %u\n", "sw_exit_info_2", |
| 1280 | ghcb->save.sw_exit_info_2, ghcb_sw_exit_info_2_is_valid(ghcb)); |
| 1281 | pr_err("%-20s%016llx is_valid: %u\n", "sw_scratch", |
| 1282 | ghcb->save.sw_scratch, ghcb_sw_scratch_is_valid(ghcb)); |
| 1283 | pr_err("%-20s%*pb\n", "valid_bitmap", nbits, ghcb->save.valid_bitmap); |
| 1284 | } |
| 1285 | |
| 1286 | static void sev_es_sync_to_ghcb(struct vcpu_svm *svm) |
| 1287 | { |
| 1288 | struct kvm_vcpu *vcpu = &svm->vcpu; |
| 1289 | struct ghcb *ghcb = svm->ghcb; |
| 1290 | |
| 1291 | /* |
| 1292 | * The GHCB protocol so far allows for the following data |
| 1293 | * to be returned: |
| 1294 | * GPRs RAX, RBX, RCX, RDX |
| 1295 | * |
| 1296 | * Copy their values to the GHCB if they are dirty. |
| 1297 | */ |
| 1298 | if (kvm_register_is_dirty(vcpu, VCPU_REGS_RAX)) |
| 1299 | ghcb_set_rax(ghcb, vcpu->arch.regs[VCPU_REGS_RAX]); |
| 1300 | if (kvm_register_is_dirty(vcpu, VCPU_REGS_RBX)) |
| 1301 | ghcb_set_rbx(ghcb, vcpu->arch.regs[VCPU_REGS_RBX]); |
| 1302 | if (kvm_register_is_dirty(vcpu, VCPU_REGS_RCX)) |
| 1303 | ghcb_set_rcx(ghcb, vcpu->arch.regs[VCPU_REGS_RCX]); |
| 1304 | if (kvm_register_is_dirty(vcpu, VCPU_REGS_RDX)) |
| 1305 | ghcb_set_rdx(ghcb, vcpu->arch.regs[VCPU_REGS_RDX]); |
| 1306 | } |
| 1307 | |
| 1308 | static void sev_es_sync_from_ghcb(struct vcpu_svm *svm) |
| 1309 | { |
| 1310 | struct vmcb_control_area *control = &svm->vmcb->control; |
| 1311 | struct kvm_vcpu *vcpu = &svm->vcpu; |
| 1312 | struct ghcb *ghcb = svm->ghcb; |
| 1313 | u64 exit_code; |
| 1314 | |
| 1315 | /* |
| 1316 | * The GHCB protocol so far allows for the following data |
| 1317 | * to be supplied: |
| 1318 | * GPRs RAX, RBX, RCX, RDX |
| 1319 | * XCR0 |
| 1320 | * CPL |
| 1321 | * |
| 1322 | * VMMCALL allows the guest to provide extra registers. KVM also |
| 1323 | * expects RSI for hypercalls, so include that, too. |
| 1324 | * |
| 1325 | * Copy their values to the appropriate location if supplied. |
| 1326 | */ |
| 1327 | memset(vcpu->arch.regs, 0, sizeof(vcpu->arch.regs)); |
| 1328 | |
| 1329 | vcpu->arch.regs[VCPU_REGS_RAX] = ghcb_get_rax_if_valid(ghcb); |
| 1330 | vcpu->arch.regs[VCPU_REGS_RBX] = ghcb_get_rbx_if_valid(ghcb); |
| 1331 | vcpu->arch.regs[VCPU_REGS_RCX] = ghcb_get_rcx_if_valid(ghcb); |
| 1332 | vcpu->arch.regs[VCPU_REGS_RDX] = ghcb_get_rdx_if_valid(ghcb); |
| 1333 | vcpu->arch.regs[VCPU_REGS_RSI] = ghcb_get_rsi_if_valid(ghcb); |
| 1334 | |
| 1335 | svm->vmcb->save.cpl = ghcb_get_cpl_if_valid(ghcb); |
| 1336 | |
| 1337 | if (ghcb_xcr0_is_valid(ghcb)) { |
| 1338 | vcpu->arch.xcr0 = ghcb_get_xcr0(ghcb); |
| 1339 | kvm_update_cpuid_runtime(vcpu); |
| 1340 | } |
| 1341 | |
| 1342 | /* Copy the GHCB exit information into the VMCB fields */ |
| 1343 | exit_code = ghcb_get_sw_exit_code(ghcb); |
| 1344 | control->exit_code = lower_32_bits(exit_code); |
| 1345 | control->exit_code_hi = upper_32_bits(exit_code); |
| 1346 | control->exit_info_1 = ghcb_get_sw_exit_info_1(ghcb); |
| 1347 | control->exit_info_2 = ghcb_get_sw_exit_info_2(ghcb); |
| 1348 | |
| 1349 | /* Clear the valid entries fields */ |
| 1350 | memset(ghcb->save.valid_bitmap, 0, sizeof(ghcb->save.valid_bitmap)); |
| 1351 | } |
| 1352 | |
| 1353 | static int sev_es_validate_vmgexit(struct vcpu_svm *svm) |
| 1354 | { |
| 1355 | struct kvm_vcpu *vcpu; |
| 1356 | struct ghcb *ghcb; |
| 1357 | u64 exit_code = 0; |
| 1358 | |
| 1359 | ghcb = svm->ghcb; |
| 1360 | |
| 1361 | /* Only GHCB Usage code 0 is supported */ |
| 1362 | if (ghcb->ghcb_usage) |
| 1363 | goto vmgexit_err; |
| 1364 | |
| 1365 | /* |
| 1366 | * Retrieve the exit code now even though is may not be marked valid |
| 1367 | * as it could help with debugging. |
| 1368 | */ |
| 1369 | exit_code = ghcb_get_sw_exit_code(ghcb); |
| 1370 | |
| 1371 | if (!ghcb_sw_exit_code_is_valid(ghcb) || |
| 1372 | !ghcb_sw_exit_info_1_is_valid(ghcb) || |
| 1373 | !ghcb_sw_exit_info_2_is_valid(ghcb)) |
| 1374 | goto vmgexit_err; |
| 1375 | |
| 1376 | switch (ghcb_get_sw_exit_code(ghcb)) { |
| 1377 | case SVM_EXIT_READ_DR7: |
| 1378 | break; |
| 1379 | case SVM_EXIT_WRITE_DR7: |
| 1380 | if (!ghcb_rax_is_valid(ghcb)) |
| 1381 | goto vmgexit_err; |
| 1382 | break; |
| 1383 | case SVM_EXIT_RDTSC: |
| 1384 | break; |
| 1385 | case SVM_EXIT_RDPMC: |
| 1386 | if (!ghcb_rcx_is_valid(ghcb)) |
| 1387 | goto vmgexit_err; |
| 1388 | break; |
| 1389 | case SVM_EXIT_CPUID: |
| 1390 | if (!ghcb_rax_is_valid(ghcb) || |
| 1391 | !ghcb_rcx_is_valid(ghcb)) |
| 1392 | goto vmgexit_err; |
| 1393 | if (ghcb_get_rax(ghcb) == 0xd) |
| 1394 | if (!ghcb_xcr0_is_valid(ghcb)) |
| 1395 | goto vmgexit_err; |
| 1396 | break; |
| 1397 | case SVM_EXIT_INVD: |
| 1398 | break; |
| 1399 | case SVM_EXIT_IOIO: |
| 1400 | if (!(ghcb_get_sw_exit_info_1(ghcb) & SVM_IOIO_TYPE_MASK)) |
| 1401 | if (!ghcb_rax_is_valid(ghcb)) |
| 1402 | goto vmgexit_err; |
| 1403 | break; |
| 1404 | case SVM_EXIT_MSR: |
| 1405 | if (!ghcb_rcx_is_valid(ghcb)) |
| 1406 | goto vmgexit_err; |
| 1407 | if (ghcb_get_sw_exit_info_1(ghcb)) { |
| 1408 | if (!ghcb_rax_is_valid(ghcb) || |
| 1409 | !ghcb_rdx_is_valid(ghcb)) |
| 1410 | goto vmgexit_err; |
| 1411 | } |
| 1412 | break; |
| 1413 | case SVM_EXIT_VMMCALL: |
| 1414 | if (!ghcb_rax_is_valid(ghcb) || |
| 1415 | !ghcb_cpl_is_valid(ghcb)) |
| 1416 | goto vmgexit_err; |
| 1417 | break; |
| 1418 | case SVM_EXIT_RDTSCP: |
| 1419 | break; |
| 1420 | case SVM_EXIT_WBINVD: |
| 1421 | break; |
| 1422 | case SVM_EXIT_MONITOR: |
| 1423 | if (!ghcb_rax_is_valid(ghcb) || |
| 1424 | !ghcb_rcx_is_valid(ghcb) || |
| 1425 | !ghcb_rdx_is_valid(ghcb)) |
| 1426 | goto vmgexit_err; |
| 1427 | break; |
| 1428 | case SVM_EXIT_MWAIT: |
| 1429 | if (!ghcb_rax_is_valid(ghcb) || |
| 1430 | !ghcb_rcx_is_valid(ghcb)) |
| 1431 | goto vmgexit_err; |
| 1432 | break; |
| 1433 | case SVM_VMGEXIT_UNSUPPORTED_EVENT: |
| 1434 | break; |
| 1435 | default: |
| 1436 | goto vmgexit_err; |
| 1437 | } |
| 1438 | |
| 1439 | return 0; |
| 1440 | |
| 1441 | vmgexit_err: |
| 1442 | vcpu = &svm->vcpu; |
| 1443 | |
| 1444 | if (ghcb->ghcb_usage) { |
| 1445 | vcpu_unimpl(vcpu, "vmgexit: ghcb usage %#x is not valid\n", |
| 1446 | ghcb->ghcb_usage); |
| 1447 | } else { |
| 1448 | vcpu_unimpl(vcpu, "vmgexit: exit reason %#llx is not valid\n", |
| 1449 | exit_code); |
| 1450 | dump_ghcb(svm); |
| 1451 | } |
| 1452 | |
| 1453 | vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR; |
| 1454 | vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_UNEXPECTED_EXIT_REASON; |
| 1455 | vcpu->run->internal.ndata = 2; |
| 1456 | vcpu->run->internal.data[0] = exit_code; |
| 1457 | vcpu->run->internal.data[1] = vcpu->arch.last_vmentry_cpu; |
| 1458 | |
| 1459 | return -EINVAL; |
| 1460 | } |
| 1461 | |
| 1462 | static void pre_sev_es_run(struct vcpu_svm *svm) |
| 1463 | { |
| 1464 | if (!svm->ghcb) |
| 1465 | return; |
| 1466 | |
| 1467 | sev_es_sync_to_ghcb(svm); |
| 1468 | |
| 1469 | kvm_vcpu_unmap(&svm->vcpu, &svm->ghcb_map, true); |
| 1470 | svm->ghcb = NULL; |
| 1471 | } |
| 1472 | |
Joerg Roedel | eaf7826 | 2020-03-24 10:41:54 +0100 | [diff] [blame] | 1473 | void pre_sev_run(struct vcpu_svm *svm, int cpu) |
| 1474 | { |
| 1475 | struct svm_cpu_data *sd = per_cpu(svm_data, cpu); |
| 1476 | int asid = sev_get_asid(svm->vcpu.kvm); |
| 1477 | |
Tom Lendacky | 291bd20 | 2020-12-10 11:09:47 -0600 | [diff] [blame^] | 1478 | /* Perform any SEV-ES pre-run actions */ |
| 1479 | pre_sev_es_run(svm); |
| 1480 | |
Joerg Roedel | eaf7826 | 2020-03-24 10:41:54 +0100 | [diff] [blame] | 1481 | /* Assign the asid allocated with this SEV guest */ |
Paolo Bonzini | dee734a | 2020-11-30 09:39:59 -0500 | [diff] [blame] | 1482 | svm->asid = asid; |
Joerg Roedel | eaf7826 | 2020-03-24 10:41:54 +0100 | [diff] [blame] | 1483 | |
| 1484 | /* |
| 1485 | * Flush guest TLB: |
| 1486 | * |
| 1487 | * 1) when different VMCB for the same ASID is to be run on the same host CPU. |
| 1488 | * 2) or this VMCB was executed on different host CPU in previous VMRUNs. |
| 1489 | */ |
| 1490 | if (sd->sev_vmcbs[asid] == svm->vmcb && |
Jim Mattson | 8a14fe4 | 2020-06-03 16:56:22 -0700 | [diff] [blame] | 1491 | svm->vcpu.arch.last_vmentry_cpu == cpu) |
Joerg Roedel | eaf7826 | 2020-03-24 10:41:54 +0100 | [diff] [blame] | 1492 | return; |
| 1493 | |
Joerg Roedel | eaf7826 | 2020-03-24 10:41:54 +0100 | [diff] [blame] | 1494 | sd->sev_vmcbs[asid] = svm->vmcb; |
| 1495 | svm->vmcb->control.tlb_ctl = TLB_CONTROL_FLUSH_ASID; |
Joerg Roedel | 06e7852 | 2020-06-25 10:03:23 +0200 | [diff] [blame] | 1496 | vmcb_mark_dirty(svm->vmcb, VMCB_ASID); |
Joerg Roedel | eaf7826 | 2020-03-24 10:41:54 +0100 | [diff] [blame] | 1497 | } |
Tom Lendacky | 291bd20 | 2020-12-10 11:09:47 -0600 | [diff] [blame^] | 1498 | |
| 1499 | static int sev_handle_vmgexit_msr_protocol(struct vcpu_svm *svm) |
| 1500 | { |
| 1501 | return -EINVAL; |
| 1502 | } |
| 1503 | |
| 1504 | int sev_handle_vmgexit(struct vcpu_svm *svm) |
| 1505 | { |
| 1506 | struct vmcb_control_area *control = &svm->vmcb->control; |
| 1507 | u64 ghcb_gpa, exit_code; |
| 1508 | struct ghcb *ghcb; |
| 1509 | int ret; |
| 1510 | |
| 1511 | /* Validate the GHCB */ |
| 1512 | ghcb_gpa = control->ghcb_gpa; |
| 1513 | if (ghcb_gpa & GHCB_MSR_INFO_MASK) |
| 1514 | return sev_handle_vmgexit_msr_protocol(svm); |
| 1515 | |
| 1516 | if (!ghcb_gpa) { |
| 1517 | vcpu_unimpl(&svm->vcpu, "vmgexit: GHCB gpa is not set\n"); |
| 1518 | return -EINVAL; |
| 1519 | } |
| 1520 | |
| 1521 | if (kvm_vcpu_map(&svm->vcpu, ghcb_gpa >> PAGE_SHIFT, &svm->ghcb_map)) { |
| 1522 | /* Unable to map GHCB from guest */ |
| 1523 | vcpu_unimpl(&svm->vcpu, "vmgexit: error mapping GHCB [%#llx] from guest\n", |
| 1524 | ghcb_gpa); |
| 1525 | return -EINVAL; |
| 1526 | } |
| 1527 | |
| 1528 | svm->ghcb = svm->ghcb_map.hva; |
| 1529 | ghcb = svm->ghcb_map.hva; |
| 1530 | |
| 1531 | exit_code = ghcb_get_sw_exit_code(ghcb); |
| 1532 | |
| 1533 | ret = sev_es_validate_vmgexit(svm); |
| 1534 | if (ret) |
| 1535 | return ret; |
| 1536 | |
| 1537 | sev_es_sync_from_ghcb(svm); |
| 1538 | ghcb_set_sw_exit_info_1(ghcb, 0); |
| 1539 | ghcb_set_sw_exit_info_2(ghcb, 0); |
| 1540 | |
| 1541 | ret = -EINVAL; |
| 1542 | switch (exit_code) { |
| 1543 | case SVM_VMGEXIT_UNSUPPORTED_EVENT: |
| 1544 | vcpu_unimpl(&svm->vcpu, |
| 1545 | "vmgexit: unsupported event - exit_info_1=%#llx, exit_info_2=%#llx\n", |
| 1546 | control->exit_info_1, control->exit_info_2); |
| 1547 | break; |
| 1548 | default: |
| 1549 | ret = svm_invoke_exit_handler(svm, exit_code); |
| 1550 | } |
| 1551 | |
| 1552 | return ret; |
| 1553 | } |