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> 15 #include <linux/pagemap.h> 16 #include <linux/swap.h> 17 #include <linux/processor.h> 18 #include <linux/trace_events.h> 19 #include <asm/fpu/internal.h> 20 21 #include <asm/trapnr.h> 22 23 #include "x86.h" 24 #include "svm.h" 25 #include "cpuid.h" 26 #include "trace.h" 27 28 #define __ex(x) __kvm_handle_fault_on_reboot(x) 29 30 static u8 sev_enc_bit; 31 static int sev_flush_asids(void); 32 static DECLARE_RWSEM(sev_deactivate_lock); 33 static DEFINE_MUTEX(sev_bitmap_lock); 34 unsigned int max_sev_asid; 35 static unsigned int min_sev_asid; 36 static unsigned long *sev_asid_bitmap; 37 static unsigned long *sev_reclaim_asid_bitmap; 38 39 struct enc_region { 40 struct list_head list; 41 unsigned long npages; 42 struct page **pages; 43 unsigned long uaddr; 44 unsigned long size; 45 }; 46 47 static int sev_flush_asids(void) 48 { 49 int ret, error = 0; 50 51 /* 52 * DEACTIVATE will clear the WBINVD indicator causing DF_FLUSH to fail, 53 * so it must be guarded. 54 */ 55 down_write(&sev_deactivate_lock); 56 57 wbinvd_on_all_cpus(); 58 ret = sev_guest_df_flush(&error); 59 60 up_write(&sev_deactivate_lock); 61 62 if (ret) 63 pr_err("SEV: DF_FLUSH failed, ret=%d, error=%#x\n", ret, error); 64 65 return ret; 66 } 67 68 /* Must be called with the sev_bitmap_lock held */ 69 static bool __sev_recycle_asids(int min_asid, int max_asid) 70 { 71 int pos; 72 73 /* Check if there are any ASIDs to reclaim before performing a flush */ 74 pos = find_next_bit(sev_reclaim_asid_bitmap, max_sev_asid, min_asid); 75 if (pos >= max_asid) 76 return false; 77 78 if (sev_flush_asids()) 79 return false; 80 81 /* The flush process will flush all reclaimable SEV and SEV-ES ASIDs */ 82 bitmap_xor(sev_asid_bitmap, sev_asid_bitmap, sev_reclaim_asid_bitmap, 83 max_sev_asid); 84 bitmap_zero(sev_reclaim_asid_bitmap, max_sev_asid); 85 86 return true; 87 } 88 89 static int sev_asid_new(struct kvm_sev_info *sev) 90 { 91 int pos, min_asid, max_asid; 92 bool retry = true; 93 94 mutex_lock(&sev_bitmap_lock); 95 96 /* 97 * SEV-enabled guests must use asid from min_sev_asid to max_sev_asid. 98 * SEV-ES-enabled guest can use from 1 to min_sev_asid - 1. 99 */ 100 min_asid = sev->es_active ? 0 : min_sev_asid - 1; 101 max_asid = sev->es_active ? min_sev_asid - 1 : max_sev_asid; 102 again: 103 pos = find_next_zero_bit(sev_asid_bitmap, max_sev_asid, min_asid); 104 if (pos >= max_asid) { 105 if (retry && __sev_recycle_asids(min_asid, max_asid)) { 106 retry = false; 107 goto again; 108 } 109 mutex_unlock(&sev_bitmap_lock); 110 return -EBUSY; 111 } 112 113 __set_bit(pos, sev_asid_bitmap); 114 115 mutex_unlock(&sev_bitmap_lock); 116 117 return pos + 1; 118 } 119 120 static int sev_get_asid(struct kvm *kvm) 121 { 122 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info; 123 124 return sev->asid; 125 } 126 127 static void sev_asid_free(int asid) 128 { 129 struct svm_cpu_data *sd; 130 int cpu, pos; 131 132 mutex_lock(&sev_bitmap_lock); 133 134 pos = asid - 1; 135 __set_bit(pos, sev_reclaim_asid_bitmap); 136 137 for_each_possible_cpu(cpu) { 138 sd = per_cpu(svm_data, cpu); 139 sd->sev_vmcbs[pos] = NULL; 140 } 141 142 mutex_unlock(&sev_bitmap_lock); 143 } 144 145 static void sev_unbind_asid(struct kvm *kvm, unsigned int handle) 146 { 147 struct sev_data_decommission *decommission; 148 struct sev_data_deactivate *data; 149 150 if (!handle) 151 return; 152 153 data = kzalloc(sizeof(*data), GFP_KERNEL); 154 if (!data) 155 return; 156 157 /* deactivate handle */ 158 data->handle = handle; 159 160 /* Guard DEACTIVATE against WBINVD/DF_FLUSH used in ASID recycling */ 161 down_read(&sev_deactivate_lock); 162 sev_guest_deactivate(data, NULL); 163 up_read(&sev_deactivate_lock); 164 165 kfree(data); 166 167 decommission = kzalloc(sizeof(*decommission), GFP_KERNEL); 168 if (!decommission) 169 return; 170 171 /* decommission handle */ 172 decommission->handle = handle; 173 sev_guest_decommission(decommission, NULL); 174 175 kfree(decommission); 176 } 177 178 static int sev_guest_init(struct kvm *kvm, struct kvm_sev_cmd *argp) 179 { 180 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info; 181 int asid, ret; 182 183 ret = -EBUSY; 184 if (unlikely(sev->active)) 185 return ret; 186 187 asid = sev_asid_new(sev); 188 if (asid < 0) 189 return ret; 190 191 ret = sev_platform_init(&argp->error); 192 if (ret) 193 goto e_free; 194 195 sev->active = true; 196 sev->asid = asid; 197 INIT_LIST_HEAD(&sev->regions_list); 198 199 return 0; 200 201 e_free: 202 sev_asid_free(asid); 203 return ret; 204 } 205 206 static int sev_es_guest_init(struct kvm *kvm, struct kvm_sev_cmd *argp) 207 { 208 if (!sev_es) 209 return -ENOTTY; 210 211 to_kvm_svm(kvm)->sev_info.es_active = true; 212 213 return sev_guest_init(kvm, argp); 214 } 215 216 static int sev_bind_asid(struct kvm *kvm, unsigned int handle, int *error) 217 { 218 struct sev_data_activate *data; 219 int asid = sev_get_asid(kvm); 220 int ret; 221 222 data = kzalloc(sizeof(*data), GFP_KERNEL_ACCOUNT); 223 if (!data) 224 return -ENOMEM; 225 226 /* activate ASID on the given handle */ 227 data->handle = handle; 228 data->asid = asid; 229 ret = sev_guest_activate(data, error); 230 kfree(data); 231 232 return ret; 233 } 234 235 static int __sev_issue_cmd(int fd, int id, void *data, int *error) 236 { 237 struct fd f; 238 int ret; 239 240 f = fdget(fd); 241 if (!f.file) 242 return -EBADF; 243 244 ret = sev_issue_cmd_external_user(f.file, id, data, error); 245 246 fdput(f); 247 return ret; 248 } 249 250 static int sev_issue_cmd(struct kvm *kvm, int id, void *data, int *error) 251 { 252 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info; 253 254 return __sev_issue_cmd(sev->fd, id, data, error); 255 } 256 257 static int sev_launch_start(struct kvm *kvm, struct kvm_sev_cmd *argp) 258 { 259 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info; 260 struct sev_data_launch_start *start; 261 struct kvm_sev_launch_start params; 262 void *dh_blob, *session_blob; 263 int *error = &argp->error; 264 int ret; 265 266 if (!sev_guest(kvm)) 267 return -ENOTTY; 268 269 if (copy_from_user(¶ms, (void __user *)(uintptr_t)argp->data, sizeof(params))) 270 return -EFAULT; 271 272 start = kzalloc(sizeof(*start), GFP_KERNEL_ACCOUNT); 273 if (!start) 274 return -ENOMEM; 275 276 dh_blob = NULL; 277 if (params.dh_uaddr) { 278 dh_blob = psp_copy_user_blob(params.dh_uaddr, params.dh_len); 279 if (IS_ERR(dh_blob)) { 280 ret = PTR_ERR(dh_blob); 281 goto e_free; 282 } 283 284 start->dh_cert_address = __sme_set(__pa(dh_blob)); 285 start->dh_cert_len = params.dh_len; 286 } 287 288 session_blob = NULL; 289 if (params.session_uaddr) { 290 session_blob = psp_copy_user_blob(params.session_uaddr, params.session_len); 291 if (IS_ERR(session_blob)) { 292 ret = PTR_ERR(session_blob); 293 goto e_free_dh; 294 } 295 296 start->session_address = __sme_set(__pa(session_blob)); 297 start->session_len = params.session_len; 298 } 299 300 start->handle = params.handle; 301 start->policy = params.policy; 302 303 /* create memory encryption context */ 304 ret = __sev_issue_cmd(argp->sev_fd, SEV_CMD_LAUNCH_START, start, error); 305 if (ret) 306 goto e_free_session; 307 308 /* Bind ASID to this guest */ 309 ret = sev_bind_asid(kvm, start->handle, error); 310 if (ret) 311 goto e_free_session; 312 313 /* return handle to userspace */ 314 params.handle = start->handle; 315 if (copy_to_user((void __user *)(uintptr_t)argp->data, ¶ms, sizeof(params))) { 316 sev_unbind_asid(kvm, start->handle); 317 ret = -EFAULT; 318 goto e_free_session; 319 } 320 321 sev->handle = start->handle; 322 sev->fd = argp->sev_fd; 323 324 e_free_session: 325 kfree(session_blob); 326 e_free_dh: 327 kfree(dh_blob); 328 e_free: 329 kfree(start); 330 return ret; 331 } 332 333 static struct page **sev_pin_memory(struct kvm *kvm, unsigned long uaddr, 334 unsigned long ulen, unsigned long *n, 335 int write) 336 { 337 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info; 338 unsigned long npages, size; 339 int npinned; 340 unsigned long locked, lock_limit; 341 struct page **pages; 342 unsigned long first, last; 343 int ret; 344 345 if (ulen == 0 || uaddr + ulen < uaddr) 346 return ERR_PTR(-EINVAL); 347 348 /* Calculate number of pages. */ 349 first = (uaddr & PAGE_MASK) >> PAGE_SHIFT; 350 last = ((uaddr + ulen - 1) & PAGE_MASK) >> PAGE_SHIFT; 351 npages = (last - first + 1); 352 353 locked = sev->pages_locked + npages; 354 lock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT; 355 if (locked > lock_limit && !capable(CAP_IPC_LOCK)) { 356 pr_err("SEV: %lu locked pages exceed the lock limit of %lu.\n", locked, lock_limit); 357 return ERR_PTR(-ENOMEM); 358 } 359 360 if (WARN_ON_ONCE(npages > INT_MAX)) 361 return ERR_PTR(-EINVAL); 362 363 /* Avoid using vmalloc for smaller buffers. */ 364 size = npages * sizeof(struct page *); 365 if (size > PAGE_SIZE) 366 pages = __vmalloc(size, GFP_KERNEL_ACCOUNT | __GFP_ZERO); 367 else 368 pages = kmalloc(size, GFP_KERNEL_ACCOUNT); 369 370 if (!pages) 371 return ERR_PTR(-ENOMEM); 372 373 /* Pin the user virtual address. */ 374 npinned = pin_user_pages_fast(uaddr, npages, write ? FOLL_WRITE : 0, pages); 375 if (npinned != npages) { 376 pr_err("SEV: Failure locking %lu pages.\n", npages); 377 ret = -ENOMEM; 378 goto err; 379 } 380 381 *n = npages; 382 sev->pages_locked = locked; 383 384 return pages; 385 386 err: 387 if (npinned > 0) 388 unpin_user_pages(pages, npinned); 389 390 kvfree(pages); 391 return ERR_PTR(ret); 392 } 393 394 static void sev_unpin_memory(struct kvm *kvm, struct page **pages, 395 unsigned long npages) 396 { 397 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info; 398 399 unpin_user_pages(pages, npages); 400 kvfree(pages); 401 sev->pages_locked -= npages; 402 } 403 404 static void sev_clflush_pages(struct page *pages[], unsigned long npages) 405 { 406 uint8_t *page_virtual; 407 unsigned long i; 408 409 if (this_cpu_has(X86_FEATURE_SME_COHERENT) || npages == 0 || 410 pages == NULL) 411 return; 412 413 for (i = 0; i < npages; i++) { 414 page_virtual = kmap_atomic(pages[i]); 415 clflush_cache_range(page_virtual, PAGE_SIZE); 416 kunmap_atomic(page_virtual); 417 } 418 } 419 420 static unsigned long get_num_contig_pages(unsigned long idx, 421 struct page **inpages, unsigned long npages) 422 { 423 unsigned long paddr, next_paddr; 424 unsigned long i = idx + 1, pages = 1; 425 426 /* find the number of contiguous pages starting from idx */ 427 paddr = __sme_page_pa(inpages[idx]); 428 while (i < npages) { 429 next_paddr = __sme_page_pa(inpages[i++]); 430 if ((paddr + PAGE_SIZE) == next_paddr) { 431 pages++; 432 paddr = next_paddr; 433 continue; 434 } 435 break; 436 } 437 438 return pages; 439 } 440 441 static int sev_launch_update_data(struct kvm *kvm, struct kvm_sev_cmd *argp) 442 { 443 unsigned long vaddr, vaddr_end, next_vaddr, npages, pages, size, i; 444 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info; 445 struct kvm_sev_launch_update_data params; 446 struct sev_data_launch_update_data *data; 447 struct page **inpages; 448 int ret; 449 450 if (!sev_guest(kvm)) 451 return -ENOTTY; 452 453 if (copy_from_user(¶ms, (void __user *)(uintptr_t)argp->data, sizeof(params))) 454 return -EFAULT; 455 456 data = kzalloc(sizeof(*data), GFP_KERNEL_ACCOUNT); 457 if (!data) 458 return -ENOMEM; 459 460 vaddr = params.uaddr; 461 size = params.len; 462 vaddr_end = vaddr + size; 463 464 /* Lock the user memory. */ 465 inpages = sev_pin_memory(kvm, vaddr, size, &npages, 1); 466 if (IS_ERR(inpages)) { 467 ret = PTR_ERR(inpages); 468 goto e_free; 469 } 470 471 /* 472 * Flush (on non-coherent CPUs) before LAUNCH_UPDATE encrypts pages in 473 * place; the cache may contain the data that was written unencrypted. 474 */ 475 sev_clflush_pages(inpages, npages); 476 477 for (i = 0; vaddr < vaddr_end; vaddr = next_vaddr, i += pages) { 478 int offset, len; 479 480 /* 481 * If the user buffer is not page-aligned, calculate the offset 482 * within the page. 483 */ 484 offset = vaddr & (PAGE_SIZE - 1); 485 486 /* Calculate the number of pages that can be encrypted in one go. */ 487 pages = get_num_contig_pages(i, inpages, npages); 488 489 len = min_t(size_t, ((pages * PAGE_SIZE) - offset), size); 490 491 data->handle = sev->handle; 492 data->len = len; 493 data->address = __sme_page_pa(inpages[i]) + offset; 494 ret = sev_issue_cmd(kvm, SEV_CMD_LAUNCH_UPDATE_DATA, data, &argp->error); 495 if (ret) 496 goto e_unpin; 497 498 size -= len; 499 next_vaddr = vaddr + len; 500 } 501 502 e_unpin: 503 /* content of memory is updated, mark pages dirty */ 504 for (i = 0; i < npages; i++) { 505 set_page_dirty_lock(inpages[i]); 506 mark_page_accessed(inpages[i]); 507 } 508 /* unlock the user pages */ 509 sev_unpin_memory(kvm, inpages, npages); 510 e_free: 511 kfree(data); 512 return ret; 513 } 514 515 static int sev_es_sync_vmsa(struct vcpu_svm *svm) 516 { 517 struct vmcb_save_area *save = &svm->vmcb->save; 518 519 /* Check some debug related fields before encrypting the VMSA */ 520 if (svm->vcpu.guest_debug || (save->dr7 & ~DR7_FIXED_1)) 521 return -EINVAL; 522 523 /* Sync registgers */ 524 save->rax = svm->vcpu.arch.regs[VCPU_REGS_RAX]; 525 save->rbx = svm->vcpu.arch.regs[VCPU_REGS_RBX]; 526 save->rcx = svm->vcpu.arch.regs[VCPU_REGS_RCX]; 527 save->rdx = svm->vcpu.arch.regs[VCPU_REGS_RDX]; 528 save->rsp = svm->vcpu.arch.regs[VCPU_REGS_RSP]; 529 save->rbp = svm->vcpu.arch.regs[VCPU_REGS_RBP]; 530 save->rsi = svm->vcpu.arch.regs[VCPU_REGS_RSI]; 531 save->rdi = svm->vcpu.arch.regs[VCPU_REGS_RDI]; 532 #ifdef CONFIG_X86_64 533 save->r8 = svm->vcpu.arch.regs[VCPU_REGS_R8]; 534 save->r9 = svm->vcpu.arch.regs[VCPU_REGS_R9]; 535 save->r10 = svm->vcpu.arch.regs[VCPU_REGS_R10]; 536 save->r11 = svm->vcpu.arch.regs[VCPU_REGS_R11]; 537 save->r12 = svm->vcpu.arch.regs[VCPU_REGS_R12]; 538 save->r13 = svm->vcpu.arch.regs[VCPU_REGS_R13]; 539 save->r14 = svm->vcpu.arch.regs[VCPU_REGS_R14]; 540 save->r15 = svm->vcpu.arch.regs[VCPU_REGS_R15]; 541 #endif 542 save->rip = svm->vcpu.arch.regs[VCPU_REGS_RIP]; 543 544 /* Sync some non-GPR registers before encrypting */ 545 save->xcr0 = svm->vcpu.arch.xcr0; 546 save->pkru = svm->vcpu.arch.pkru; 547 save->xss = svm->vcpu.arch.ia32_xss; 548 549 /* 550 * SEV-ES will use a VMSA that is pointed to by the VMCB, not 551 * the traditional VMSA that is part of the VMCB. Copy the 552 * traditional VMSA as it has been built so far (in prep 553 * for LAUNCH_UPDATE_VMSA) to be the initial SEV-ES state. 554 */ 555 memcpy(svm->vmsa, save, sizeof(*save)); 556 557 return 0; 558 } 559 560 static int sev_launch_update_vmsa(struct kvm *kvm, struct kvm_sev_cmd *argp) 561 { 562 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info; 563 struct sev_data_launch_update_vmsa *vmsa; 564 int i, ret; 565 566 if (!sev_es_guest(kvm)) 567 return -ENOTTY; 568 569 vmsa = kzalloc(sizeof(*vmsa), GFP_KERNEL); 570 if (!vmsa) 571 return -ENOMEM; 572 573 for (i = 0; i < kvm->created_vcpus; i++) { 574 struct vcpu_svm *svm = to_svm(kvm->vcpus[i]); 575 576 /* Perform some pre-encryption checks against the VMSA */ 577 ret = sev_es_sync_vmsa(svm); 578 if (ret) 579 goto e_free; 580 581 /* 582 * The LAUNCH_UPDATE_VMSA command will perform in-place 583 * encryption of the VMSA memory content (i.e it will write 584 * the same memory region with the guest's key), so invalidate 585 * it first. 586 */ 587 clflush_cache_range(svm->vmsa, PAGE_SIZE); 588 589 vmsa->handle = sev->handle; 590 vmsa->address = __sme_pa(svm->vmsa); 591 vmsa->len = PAGE_SIZE; 592 ret = sev_issue_cmd(kvm, SEV_CMD_LAUNCH_UPDATE_VMSA, vmsa, 593 &argp->error); 594 if (ret) 595 goto e_free; 596 597 svm->vcpu.arch.guest_state_protected = true; 598 } 599 600 e_free: 601 kfree(vmsa); 602 return ret; 603 } 604 605 static int sev_launch_measure(struct kvm *kvm, struct kvm_sev_cmd *argp) 606 { 607 void __user *measure = (void __user *)(uintptr_t)argp->data; 608 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info; 609 struct sev_data_launch_measure *data; 610 struct kvm_sev_launch_measure params; 611 void __user *p = NULL; 612 void *blob = NULL; 613 int ret; 614 615 if (!sev_guest(kvm)) 616 return -ENOTTY; 617 618 if (copy_from_user(¶ms, measure, sizeof(params))) 619 return -EFAULT; 620 621 data = kzalloc(sizeof(*data), GFP_KERNEL_ACCOUNT); 622 if (!data) 623 return -ENOMEM; 624 625 /* User wants to query the blob length */ 626 if (!params.len) 627 goto cmd; 628 629 p = (void __user *)(uintptr_t)params.uaddr; 630 if (p) { 631 if (params.len > SEV_FW_BLOB_MAX_SIZE) { 632 ret = -EINVAL; 633 goto e_free; 634 } 635 636 ret = -ENOMEM; 637 blob = kmalloc(params.len, GFP_KERNEL); 638 if (!blob) 639 goto e_free; 640 641 data->address = __psp_pa(blob); 642 data->len = params.len; 643 } 644 645 cmd: 646 data->handle = sev->handle; 647 ret = sev_issue_cmd(kvm, SEV_CMD_LAUNCH_MEASURE, data, &argp->error); 648 649 /* 650 * If we query the session length, FW responded with expected data. 651 */ 652 if (!params.len) 653 goto done; 654 655 if (ret) 656 goto e_free_blob; 657 658 if (blob) { 659 if (copy_to_user(p, blob, params.len)) 660 ret = -EFAULT; 661 } 662 663 done: 664 params.len = data->len; 665 if (copy_to_user(measure, ¶ms, sizeof(params))) 666 ret = -EFAULT; 667 e_free_blob: 668 kfree(blob); 669 e_free: 670 kfree(data); 671 return ret; 672 } 673 674 static int sev_launch_finish(struct kvm *kvm, struct kvm_sev_cmd *argp) 675 { 676 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info; 677 struct sev_data_launch_finish *data; 678 int ret; 679 680 if (!sev_guest(kvm)) 681 return -ENOTTY; 682 683 data = kzalloc(sizeof(*data), GFP_KERNEL_ACCOUNT); 684 if (!data) 685 return -ENOMEM; 686 687 data->handle = sev->handle; 688 ret = sev_issue_cmd(kvm, SEV_CMD_LAUNCH_FINISH, data, &argp->error); 689 690 kfree(data); 691 return ret; 692 } 693 694 static int sev_guest_status(struct kvm *kvm, struct kvm_sev_cmd *argp) 695 { 696 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info; 697 struct kvm_sev_guest_status params; 698 struct sev_data_guest_status *data; 699 int ret; 700 701 if (!sev_guest(kvm)) 702 return -ENOTTY; 703 704 data = kzalloc(sizeof(*data), GFP_KERNEL_ACCOUNT); 705 if (!data) 706 return -ENOMEM; 707 708 data->handle = sev->handle; 709 ret = sev_issue_cmd(kvm, SEV_CMD_GUEST_STATUS, data, &argp->error); 710 if (ret) 711 goto e_free; 712 713 params.policy = data->policy; 714 params.state = data->state; 715 params.handle = data->handle; 716 717 if (copy_to_user((void __user *)(uintptr_t)argp->data, ¶ms, sizeof(params))) 718 ret = -EFAULT; 719 e_free: 720 kfree(data); 721 return ret; 722 } 723 724 static int __sev_issue_dbg_cmd(struct kvm *kvm, unsigned long src, 725 unsigned long dst, int size, 726 int *error, bool enc) 727 { 728 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info; 729 struct sev_data_dbg *data; 730 int ret; 731 732 data = kzalloc(sizeof(*data), GFP_KERNEL_ACCOUNT); 733 if (!data) 734 return -ENOMEM; 735 736 data->handle = sev->handle; 737 data->dst_addr = dst; 738 data->src_addr = src; 739 data->len = size; 740 741 ret = sev_issue_cmd(kvm, 742 enc ? SEV_CMD_DBG_ENCRYPT : SEV_CMD_DBG_DECRYPT, 743 data, error); 744 kfree(data); 745 return ret; 746 } 747 748 static int __sev_dbg_decrypt(struct kvm *kvm, unsigned long src_paddr, 749 unsigned long dst_paddr, int sz, int *err) 750 { 751 int offset; 752 753 /* 754 * Its safe to read more than we are asked, caller should ensure that 755 * destination has enough space. 756 */ 757 offset = src_paddr & 15; 758 src_paddr = round_down(src_paddr, 16); 759 sz = round_up(sz + offset, 16); 760 761 return __sev_issue_dbg_cmd(kvm, src_paddr, dst_paddr, sz, err, false); 762 } 763 764 static int __sev_dbg_decrypt_user(struct kvm *kvm, unsigned long paddr, 765 unsigned long __user dst_uaddr, 766 unsigned long dst_paddr, 767 int size, int *err) 768 { 769 struct page *tpage = NULL; 770 int ret, offset; 771 772 /* if inputs are not 16-byte then use intermediate buffer */ 773 if (!IS_ALIGNED(dst_paddr, 16) || 774 !IS_ALIGNED(paddr, 16) || 775 !IS_ALIGNED(size, 16)) { 776 tpage = (void *)alloc_page(GFP_KERNEL); 777 if (!tpage) 778 return -ENOMEM; 779 780 dst_paddr = __sme_page_pa(tpage); 781 } 782 783 ret = __sev_dbg_decrypt(kvm, paddr, dst_paddr, size, err); 784 if (ret) 785 goto e_free; 786 787 if (tpage) { 788 offset = paddr & 15; 789 if (copy_to_user((void __user *)(uintptr_t)dst_uaddr, 790 page_address(tpage) + offset, size)) 791 ret = -EFAULT; 792 } 793 794 e_free: 795 if (tpage) 796 __free_page(tpage); 797 798 return ret; 799 } 800 801 static int __sev_dbg_encrypt_user(struct kvm *kvm, unsigned long paddr, 802 unsigned long __user vaddr, 803 unsigned long dst_paddr, 804 unsigned long __user dst_vaddr, 805 int size, int *error) 806 { 807 struct page *src_tpage = NULL; 808 struct page *dst_tpage = NULL; 809 int ret, len = size; 810 811 /* If source buffer is not aligned then use an intermediate buffer */ 812 if (!IS_ALIGNED(vaddr, 16)) { 813 src_tpage = alloc_page(GFP_KERNEL); 814 if (!src_tpage) 815 return -ENOMEM; 816 817 if (copy_from_user(page_address(src_tpage), 818 (void __user *)(uintptr_t)vaddr, size)) { 819 __free_page(src_tpage); 820 return -EFAULT; 821 } 822 823 paddr = __sme_page_pa(src_tpage); 824 } 825 826 /* 827 * If destination buffer or length is not aligned then do read-modify-write: 828 * - decrypt destination in an intermediate buffer 829 * - copy the source buffer in an intermediate buffer 830 * - use the intermediate buffer as source buffer 831 */ 832 if (!IS_ALIGNED(dst_vaddr, 16) || !IS_ALIGNED(size, 16)) { 833 int dst_offset; 834 835 dst_tpage = alloc_page(GFP_KERNEL); 836 if (!dst_tpage) { 837 ret = -ENOMEM; 838 goto e_free; 839 } 840 841 ret = __sev_dbg_decrypt(kvm, dst_paddr, 842 __sme_page_pa(dst_tpage), size, error); 843 if (ret) 844 goto e_free; 845 846 /* 847 * If source is kernel buffer then use memcpy() otherwise 848 * copy_from_user(). 849 */ 850 dst_offset = dst_paddr & 15; 851 852 if (src_tpage) 853 memcpy(page_address(dst_tpage) + dst_offset, 854 page_address(src_tpage), size); 855 else { 856 if (copy_from_user(page_address(dst_tpage) + dst_offset, 857 (void __user *)(uintptr_t)vaddr, size)) { 858 ret = -EFAULT; 859 goto e_free; 860 } 861 } 862 863 paddr = __sme_page_pa(dst_tpage); 864 dst_paddr = round_down(dst_paddr, 16); 865 len = round_up(size, 16); 866 } 867 868 ret = __sev_issue_dbg_cmd(kvm, paddr, dst_paddr, len, error, true); 869 870 e_free: 871 if (src_tpage) 872 __free_page(src_tpage); 873 if (dst_tpage) 874 __free_page(dst_tpage); 875 return ret; 876 } 877 878 static int sev_dbg_crypt(struct kvm *kvm, struct kvm_sev_cmd *argp, bool dec) 879 { 880 unsigned long vaddr, vaddr_end, next_vaddr; 881 unsigned long dst_vaddr; 882 struct page **src_p, **dst_p; 883 struct kvm_sev_dbg debug; 884 unsigned long n; 885 unsigned int size; 886 int ret; 887 888 if (!sev_guest(kvm)) 889 return -ENOTTY; 890 891 if (copy_from_user(&debug, (void __user *)(uintptr_t)argp->data, sizeof(debug))) 892 return -EFAULT; 893 894 if (!debug.len || debug.src_uaddr + debug.len < debug.src_uaddr) 895 return -EINVAL; 896 if (!debug.dst_uaddr) 897 return -EINVAL; 898 899 vaddr = debug.src_uaddr; 900 size = debug.len; 901 vaddr_end = vaddr + size; 902 dst_vaddr = debug.dst_uaddr; 903 904 for (; vaddr < vaddr_end; vaddr = next_vaddr) { 905 int len, s_off, d_off; 906 907 /* lock userspace source and destination page */ 908 src_p = sev_pin_memory(kvm, vaddr & PAGE_MASK, PAGE_SIZE, &n, 0); 909 if (IS_ERR(src_p)) 910 return PTR_ERR(src_p); 911 912 dst_p = sev_pin_memory(kvm, dst_vaddr & PAGE_MASK, PAGE_SIZE, &n, 1); 913 if (IS_ERR(dst_p)) { 914 sev_unpin_memory(kvm, src_p, n); 915 return PTR_ERR(dst_p); 916 } 917 918 /* 919 * Flush (on non-coherent CPUs) before DBG_{DE,EN}CRYPT read or modify 920 * the pages; flush the destination too so that future accesses do not 921 * see stale data. 922 */ 923 sev_clflush_pages(src_p, 1); 924 sev_clflush_pages(dst_p, 1); 925 926 /* 927 * Since user buffer may not be page aligned, calculate the 928 * offset within the page. 929 */ 930 s_off = vaddr & ~PAGE_MASK; 931 d_off = dst_vaddr & ~PAGE_MASK; 932 len = min_t(size_t, (PAGE_SIZE - s_off), size); 933 934 if (dec) 935 ret = __sev_dbg_decrypt_user(kvm, 936 __sme_page_pa(src_p[0]) + s_off, 937 dst_vaddr, 938 __sme_page_pa(dst_p[0]) + d_off, 939 len, &argp->error); 940 else 941 ret = __sev_dbg_encrypt_user(kvm, 942 __sme_page_pa(src_p[0]) + s_off, 943 vaddr, 944 __sme_page_pa(dst_p[0]) + d_off, 945 dst_vaddr, 946 len, &argp->error); 947 948 sev_unpin_memory(kvm, src_p, n); 949 sev_unpin_memory(kvm, dst_p, n); 950 951 if (ret) 952 goto err; 953 954 next_vaddr = vaddr + len; 955 dst_vaddr = dst_vaddr + len; 956 size -= len; 957 } 958 err: 959 return ret; 960 } 961 962 static int sev_launch_secret(struct kvm *kvm, struct kvm_sev_cmd *argp) 963 { 964 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info; 965 struct sev_data_launch_secret *data; 966 struct kvm_sev_launch_secret params; 967 struct page **pages; 968 void *blob, *hdr; 969 unsigned long n, i; 970 int ret, offset; 971 972 if (!sev_guest(kvm)) 973 return -ENOTTY; 974 975 if (copy_from_user(¶ms, (void __user *)(uintptr_t)argp->data, sizeof(params))) 976 return -EFAULT; 977 978 pages = sev_pin_memory(kvm, params.guest_uaddr, params.guest_len, &n, 1); 979 if (IS_ERR(pages)) 980 return PTR_ERR(pages); 981 982 /* 983 * Flush (on non-coherent CPUs) before LAUNCH_SECRET encrypts pages in 984 * place; the cache may contain the data that was written unencrypted. 985 */ 986 sev_clflush_pages(pages, n); 987 988 /* 989 * The secret must be copied into contiguous memory region, lets verify 990 * that userspace memory pages are contiguous before we issue command. 991 */ 992 if (get_num_contig_pages(0, pages, n) != n) { 993 ret = -EINVAL; 994 goto e_unpin_memory; 995 } 996 997 ret = -ENOMEM; 998 data = kzalloc(sizeof(*data), GFP_KERNEL_ACCOUNT); 999 if (!data) 1000 goto e_unpin_memory; 1001 1002 offset = params.guest_uaddr & (PAGE_SIZE - 1); 1003 data->guest_address = __sme_page_pa(pages[0]) + offset; 1004 data->guest_len = params.guest_len; 1005 1006 blob = psp_copy_user_blob(params.trans_uaddr, params.trans_len); 1007 if (IS_ERR(blob)) { 1008 ret = PTR_ERR(blob); 1009 goto e_free; 1010 } 1011 1012 data->trans_address = __psp_pa(blob); 1013 data->trans_len = params.trans_len; 1014 1015 hdr = psp_copy_user_blob(params.hdr_uaddr, params.hdr_len); 1016 if (IS_ERR(hdr)) { 1017 ret = PTR_ERR(hdr); 1018 goto e_free_blob; 1019 } 1020 data->hdr_address = __psp_pa(hdr); 1021 data->hdr_len = params.hdr_len; 1022 1023 data->handle = sev->handle; 1024 ret = sev_issue_cmd(kvm, SEV_CMD_LAUNCH_UPDATE_SECRET, data, &argp->error); 1025 1026 kfree(hdr); 1027 1028 e_free_blob: 1029 kfree(blob); 1030 e_free: 1031 kfree(data); 1032 e_unpin_memory: 1033 /* content of memory is updated, mark pages dirty */ 1034 for (i = 0; i < n; i++) { 1035 set_page_dirty_lock(pages[i]); 1036 mark_page_accessed(pages[i]); 1037 } 1038 sev_unpin_memory(kvm, pages, n); 1039 return ret; 1040 } 1041 1042 int svm_mem_enc_op(struct kvm *kvm, void __user *argp) 1043 { 1044 struct kvm_sev_cmd sev_cmd; 1045 int r; 1046 1047 if (!svm_sev_enabled() || !sev) 1048 return -ENOTTY; 1049 1050 if (!argp) 1051 return 0; 1052 1053 if (copy_from_user(&sev_cmd, argp, sizeof(struct kvm_sev_cmd))) 1054 return -EFAULT; 1055 1056 mutex_lock(&kvm->lock); 1057 1058 switch (sev_cmd.id) { 1059 case KVM_SEV_INIT: 1060 r = sev_guest_init(kvm, &sev_cmd); 1061 break; 1062 case KVM_SEV_ES_INIT: 1063 r = sev_es_guest_init(kvm, &sev_cmd); 1064 break; 1065 case KVM_SEV_LAUNCH_START: 1066 r = sev_launch_start(kvm, &sev_cmd); 1067 break; 1068 case KVM_SEV_LAUNCH_UPDATE_DATA: 1069 r = sev_launch_update_data(kvm, &sev_cmd); 1070 break; 1071 case KVM_SEV_LAUNCH_UPDATE_VMSA: 1072 r = sev_launch_update_vmsa(kvm, &sev_cmd); 1073 break; 1074 case KVM_SEV_LAUNCH_MEASURE: 1075 r = sev_launch_measure(kvm, &sev_cmd); 1076 break; 1077 case KVM_SEV_LAUNCH_FINISH: 1078 r = sev_launch_finish(kvm, &sev_cmd); 1079 break; 1080 case KVM_SEV_GUEST_STATUS: 1081 r = sev_guest_status(kvm, &sev_cmd); 1082 break; 1083 case KVM_SEV_DBG_DECRYPT: 1084 r = sev_dbg_crypt(kvm, &sev_cmd, true); 1085 break; 1086 case KVM_SEV_DBG_ENCRYPT: 1087 r = sev_dbg_crypt(kvm, &sev_cmd, false); 1088 break; 1089 case KVM_SEV_LAUNCH_SECRET: 1090 r = sev_launch_secret(kvm, &sev_cmd); 1091 break; 1092 default: 1093 r = -EINVAL; 1094 goto out; 1095 } 1096 1097 if (copy_to_user(argp, &sev_cmd, sizeof(struct kvm_sev_cmd))) 1098 r = -EFAULT; 1099 1100 out: 1101 mutex_unlock(&kvm->lock); 1102 return r; 1103 } 1104 1105 int svm_register_enc_region(struct kvm *kvm, 1106 struct kvm_enc_region *range) 1107 { 1108 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info; 1109 struct enc_region *region; 1110 int ret = 0; 1111 1112 if (!sev_guest(kvm)) 1113 return -ENOTTY; 1114 1115 if (range->addr > ULONG_MAX || range->size > ULONG_MAX) 1116 return -EINVAL; 1117 1118 region = kzalloc(sizeof(*region), GFP_KERNEL_ACCOUNT); 1119 if (!region) 1120 return -ENOMEM; 1121 1122 region->pages = sev_pin_memory(kvm, range->addr, range->size, ®ion->npages, 1); 1123 if (IS_ERR(region->pages)) { 1124 ret = PTR_ERR(region->pages); 1125 goto e_free; 1126 } 1127 1128 /* 1129 * The guest may change the memory encryption attribute from C=0 -> C=1 1130 * or vice versa for this memory range. Lets make sure caches are 1131 * flushed to ensure that guest data gets written into memory with 1132 * correct C-bit. 1133 */ 1134 sev_clflush_pages(region->pages, region->npages); 1135 1136 region->uaddr = range->addr; 1137 region->size = range->size; 1138 1139 mutex_lock(&kvm->lock); 1140 list_add_tail(®ion->list, &sev->regions_list); 1141 mutex_unlock(&kvm->lock); 1142 1143 return ret; 1144 1145 e_free: 1146 kfree(region); 1147 return ret; 1148 } 1149 1150 static struct enc_region * 1151 find_enc_region(struct kvm *kvm, struct kvm_enc_region *range) 1152 { 1153 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info; 1154 struct list_head *head = &sev->regions_list; 1155 struct enc_region *i; 1156 1157 list_for_each_entry(i, head, list) { 1158 if (i->uaddr == range->addr && 1159 i->size == range->size) 1160 return i; 1161 } 1162 1163 return NULL; 1164 } 1165 1166 static void __unregister_enc_region_locked(struct kvm *kvm, 1167 struct enc_region *region) 1168 { 1169 sev_unpin_memory(kvm, region->pages, region->npages); 1170 list_del(®ion->list); 1171 kfree(region); 1172 } 1173 1174 int svm_unregister_enc_region(struct kvm *kvm, 1175 struct kvm_enc_region *range) 1176 { 1177 struct enc_region *region; 1178 int ret; 1179 1180 mutex_lock(&kvm->lock); 1181 1182 if (!sev_guest(kvm)) { 1183 ret = -ENOTTY; 1184 goto failed; 1185 } 1186 1187 region = find_enc_region(kvm, range); 1188 if (!region) { 1189 ret = -EINVAL; 1190 goto failed; 1191 } 1192 1193 /* 1194 * Ensure that all guest tagged cache entries are flushed before 1195 * releasing the pages back to the system for use. CLFLUSH will 1196 * not do this, so issue a WBINVD. 1197 */ 1198 wbinvd_on_all_cpus(); 1199 1200 __unregister_enc_region_locked(kvm, region); 1201 1202 mutex_unlock(&kvm->lock); 1203 return 0; 1204 1205 failed: 1206 mutex_unlock(&kvm->lock); 1207 return ret; 1208 } 1209 1210 void sev_vm_destroy(struct kvm *kvm) 1211 { 1212 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info; 1213 struct list_head *head = &sev->regions_list; 1214 struct list_head *pos, *q; 1215 1216 if (!sev_guest(kvm)) 1217 return; 1218 1219 mutex_lock(&kvm->lock); 1220 1221 /* 1222 * Ensure that all guest tagged cache entries are flushed before 1223 * releasing the pages back to the system for use. CLFLUSH will 1224 * not do this, so issue a WBINVD. 1225 */ 1226 wbinvd_on_all_cpus(); 1227 1228 /* 1229 * if userspace was terminated before unregistering the memory regions 1230 * then lets unpin all the registered memory. 1231 */ 1232 if (!list_empty(head)) { 1233 list_for_each_safe(pos, q, head) { 1234 __unregister_enc_region_locked(kvm, 1235 list_entry(pos, struct enc_region, list)); 1236 cond_resched(); 1237 } 1238 } 1239 1240 mutex_unlock(&kvm->lock); 1241 1242 sev_unbind_asid(kvm, sev->handle); 1243 sev_asid_free(sev->asid); 1244 } 1245 1246 void __init sev_hardware_setup(void) 1247 { 1248 unsigned int eax, ebx, ecx, edx; 1249 bool sev_es_supported = false; 1250 bool sev_supported = false; 1251 1252 /* Does the CPU support SEV? */ 1253 if (!boot_cpu_has(X86_FEATURE_SEV)) 1254 goto out; 1255 1256 /* Retrieve SEV CPUID information */ 1257 cpuid(0x8000001f, &eax, &ebx, &ecx, &edx); 1258 1259 /* Set encryption bit location for SEV-ES guests */ 1260 sev_enc_bit = ebx & 0x3f; 1261 1262 /* Maximum number of encrypted guests supported simultaneously */ 1263 max_sev_asid = ecx; 1264 1265 if (!svm_sev_enabled()) 1266 goto out; 1267 1268 /* Minimum ASID value that should be used for SEV guest */ 1269 min_sev_asid = edx; 1270 1271 /* Initialize SEV ASID bitmaps */ 1272 sev_asid_bitmap = bitmap_zalloc(max_sev_asid, GFP_KERNEL); 1273 if (!sev_asid_bitmap) 1274 goto out; 1275 1276 sev_reclaim_asid_bitmap = bitmap_zalloc(max_sev_asid, GFP_KERNEL); 1277 if (!sev_reclaim_asid_bitmap) 1278 goto out; 1279 1280 pr_info("SEV supported: %u ASIDs\n", max_sev_asid - min_sev_asid + 1); 1281 sev_supported = true; 1282 1283 /* SEV-ES support requested? */ 1284 if (!sev_es) 1285 goto out; 1286 1287 /* Does the CPU support SEV-ES? */ 1288 if (!boot_cpu_has(X86_FEATURE_SEV_ES)) 1289 goto out; 1290 1291 /* Has the system been allocated ASIDs for SEV-ES? */ 1292 if (min_sev_asid == 1) 1293 goto out; 1294 1295 pr_info("SEV-ES supported: %u ASIDs\n", min_sev_asid - 1); 1296 sev_es_supported = true; 1297 1298 out: 1299 sev = sev_supported; 1300 sev_es = sev_es_supported; 1301 } 1302 1303 void sev_hardware_teardown(void) 1304 { 1305 if (!svm_sev_enabled()) 1306 return; 1307 1308 bitmap_free(sev_asid_bitmap); 1309 bitmap_free(sev_reclaim_asid_bitmap); 1310 1311 sev_flush_asids(); 1312 } 1313 1314 /* 1315 * Pages used by hardware to hold guest encrypted state must be flushed before 1316 * returning them to the system. 1317 */ 1318 static void sev_flush_guest_memory(struct vcpu_svm *svm, void *va, 1319 unsigned long len) 1320 { 1321 /* 1322 * If hardware enforced cache coherency for encrypted mappings of the 1323 * same physical page is supported, nothing to do. 1324 */ 1325 if (boot_cpu_has(X86_FEATURE_SME_COHERENT)) 1326 return; 1327 1328 /* 1329 * If the VM Page Flush MSR is supported, use it to flush the page 1330 * (using the page virtual address and the guest ASID). 1331 */ 1332 if (boot_cpu_has(X86_FEATURE_VM_PAGE_FLUSH)) { 1333 struct kvm_sev_info *sev; 1334 unsigned long va_start; 1335 u64 start, stop; 1336 1337 /* Align start and stop to page boundaries. */ 1338 va_start = (unsigned long)va; 1339 start = (u64)va_start & PAGE_MASK; 1340 stop = PAGE_ALIGN((u64)va_start + len); 1341 1342 if (start < stop) { 1343 sev = &to_kvm_svm(svm->vcpu.kvm)->sev_info; 1344 1345 while (start < stop) { 1346 wrmsrl(MSR_AMD64_VM_PAGE_FLUSH, 1347 start | sev->asid); 1348 1349 start += PAGE_SIZE; 1350 } 1351 1352 return; 1353 } 1354 1355 WARN(1, "Address overflow, using WBINVD\n"); 1356 } 1357 1358 /* 1359 * Hardware should always have one of the above features, 1360 * but if not, use WBINVD and issue a warning. 1361 */ 1362 WARN_ONCE(1, "Using WBINVD to flush guest memory\n"); 1363 wbinvd_on_all_cpus(); 1364 } 1365 1366 void sev_free_vcpu(struct kvm_vcpu *vcpu) 1367 { 1368 struct vcpu_svm *svm; 1369 1370 if (!sev_es_guest(vcpu->kvm)) 1371 return; 1372 1373 svm = to_svm(vcpu); 1374 1375 if (vcpu->arch.guest_state_protected) 1376 sev_flush_guest_memory(svm, svm->vmsa, PAGE_SIZE); 1377 __free_page(virt_to_page(svm->vmsa)); 1378 1379 if (svm->ghcb_sa_free) 1380 kfree(svm->ghcb_sa); 1381 } 1382 1383 static void dump_ghcb(struct vcpu_svm *svm) 1384 { 1385 struct ghcb *ghcb = svm->ghcb; 1386 unsigned int nbits; 1387 1388 /* Re-use the dump_invalid_vmcb module parameter */ 1389 if (!dump_invalid_vmcb) { 1390 pr_warn_ratelimited("set kvm_amd.dump_invalid_vmcb=1 to dump internal KVM state.\n"); 1391 return; 1392 } 1393 1394 nbits = sizeof(ghcb->save.valid_bitmap) * 8; 1395 1396 pr_err("GHCB (GPA=%016llx):\n", svm->vmcb->control.ghcb_gpa); 1397 pr_err("%-20s%016llx is_valid: %u\n", "sw_exit_code", 1398 ghcb->save.sw_exit_code, ghcb_sw_exit_code_is_valid(ghcb)); 1399 pr_err("%-20s%016llx is_valid: %u\n", "sw_exit_info_1", 1400 ghcb->save.sw_exit_info_1, ghcb_sw_exit_info_1_is_valid(ghcb)); 1401 pr_err("%-20s%016llx is_valid: %u\n", "sw_exit_info_2", 1402 ghcb->save.sw_exit_info_2, ghcb_sw_exit_info_2_is_valid(ghcb)); 1403 pr_err("%-20s%016llx is_valid: %u\n", "sw_scratch", 1404 ghcb->save.sw_scratch, ghcb_sw_scratch_is_valid(ghcb)); 1405 pr_err("%-20s%*pb\n", "valid_bitmap", nbits, ghcb->save.valid_bitmap); 1406 } 1407 1408 static void sev_es_sync_to_ghcb(struct vcpu_svm *svm) 1409 { 1410 struct kvm_vcpu *vcpu = &svm->vcpu; 1411 struct ghcb *ghcb = svm->ghcb; 1412 1413 /* 1414 * The GHCB protocol so far allows for the following data 1415 * to be returned: 1416 * GPRs RAX, RBX, RCX, RDX 1417 * 1418 * Copy their values to the GHCB if they are dirty. 1419 */ 1420 if (kvm_register_is_dirty(vcpu, VCPU_REGS_RAX)) 1421 ghcb_set_rax(ghcb, vcpu->arch.regs[VCPU_REGS_RAX]); 1422 if (kvm_register_is_dirty(vcpu, VCPU_REGS_RBX)) 1423 ghcb_set_rbx(ghcb, vcpu->arch.regs[VCPU_REGS_RBX]); 1424 if (kvm_register_is_dirty(vcpu, VCPU_REGS_RCX)) 1425 ghcb_set_rcx(ghcb, vcpu->arch.regs[VCPU_REGS_RCX]); 1426 if (kvm_register_is_dirty(vcpu, VCPU_REGS_RDX)) 1427 ghcb_set_rdx(ghcb, vcpu->arch.regs[VCPU_REGS_RDX]); 1428 } 1429 1430 static void sev_es_sync_from_ghcb(struct vcpu_svm *svm) 1431 { 1432 struct vmcb_control_area *control = &svm->vmcb->control; 1433 struct kvm_vcpu *vcpu = &svm->vcpu; 1434 struct ghcb *ghcb = svm->ghcb; 1435 u64 exit_code; 1436 1437 /* 1438 * The GHCB protocol so far allows for the following data 1439 * to be supplied: 1440 * GPRs RAX, RBX, RCX, RDX 1441 * XCR0 1442 * CPL 1443 * 1444 * VMMCALL allows the guest to provide extra registers. KVM also 1445 * expects RSI for hypercalls, so include that, too. 1446 * 1447 * Copy their values to the appropriate location if supplied. 1448 */ 1449 memset(vcpu->arch.regs, 0, sizeof(vcpu->arch.regs)); 1450 1451 vcpu->arch.regs[VCPU_REGS_RAX] = ghcb_get_rax_if_valid(ghcb); 1452 vcpu->arch.regs[VCPU_REGS_RBX] = ghcb_get_rbx_if_valid(ghcb); 1453 vcpu->arch.regs[VCPU_REGS_RCX] = ghcb_get_rcx_if_valid(ghcb); 1454 vcpu->arch.regs[VCPU_REGS_RDX] = ghcb_get_rdx_if_valid(ghcb); 1455 vcpu->arch.regs[VCPU_REGS_RSI] = ghcb_get_rsi_if_valid(ghcb); 1456 1457 svm->vmcb->save.cpl = ghcb_get_cpl_if_valid(ghcb); 1458 1459 if (ghcb_xcr0_is_valid(ghcb)) { 1460 vcpu->arch.xcr0 = ghcb_get_xcr0(ghcb); 1461 kvm_update_cpuid_runtime(vcpu); 1462 } 1463 1464 /* Copy the GHCB exit information into the VMCB fields */ 1465 exit_code = ghcb_get_sw_exit_code(ghcb); 1466 control->exit_code = lower_32_bits(exit_code); 1467 control->exit_code_hi = upper_32_bits(exit_code); 1468 control->exit_info_1 = ghcb_get_sw_exit_info_1(ghcb); 1469 control->exit_info_2 = ghcb_get_sw_exit_info_2(ghcb); 1470 1471 /* Clear the valid entries fields */ 1472 memset(ghcb->save.valid_bitmap, 0, sizeof(ghcb->save.valid_bitmap)); 1473 } 1474 1475 static int sev_es_validate_vmgexit(struct vcpu_svm *svm) 1476 { 1477 struct kvm_vcpu *vcpu; 1478 struct ghcb *ghcb; 1479 u64 exit_code = 0; 1480 1481 ghcb = svm->ghcb; 1482 1483 /* Only GHCB Usage code 0 is supported */ 1484 if (ghcb->ghcb_usage) 1485 goto vmgexit_err; 1486 1487 /* 1488 * Retrieve the exit code now even though is may not be marked valid 1489 * as it could help with debugging. 1490 */ 1491 exit_code = ghcb_get_sw_exit_code(ghcb); 1492 1493 if (!ghcb_sw_exit_code_is_valid(ghcb) || 1494 !ghcb_sw_exit_info_1_is_valid(ghcb) || 1495 !ghcb_sw_exit_info_2_is_valid(ghcb)) 1496 goto vmgexit_err; 1497 1498 switch (ghcb_get_sw_exit_code(ghcb)) { 1499 case SVM_EXIT_READ_DR7: 1500 break; 1501 case SVM_EXIT_WRITE_DR7: 1502 if (!ghcb_rax_is_valid(ghcb)) 1503 goto vmgexit_err; 1504 break; 1505 case SVM_EXIT_RDTSC: 1506 break; 1507 case SVM_EXIT_RDPMC: 1508 if (!ghcb_rcx_is_valid(ghcb)) 1509 goto vmgexit_err; 1510 break; 1511 case SVM_EXIT_CPUID: 1512 if (!ghcb_rax_is_valid(ghcb) || 1513 !ghcb_rcx_is_valid(ghcb)) 1514 goto vmgexit_err; 1515 if (ghcb_get_rax(ghcb) == 0xd) 1516 if (!ghcb_xcr0_is_valid(ghcb)) 1517 goto vmgexit_err; 1518 break; 1519 case SVM_EXIT_INVD: 1520 break; 1521 case SVM_EXIT_IOIO: 1522 if (ghcb_get_sw_exit_info_1(ghcb) & SVM_IOIO_STR_MASK) { 1523 if (!ghcb_sw_scratch_is_valid(ghcb)) 1524 goto vmgexit_err; 1525 } else { 1526 if (!(ghcb_get_sw_exit_info_1(ghcb) & SVM_IOIO_TYPE_MASK)) 1527 if (!ghcb_rax_is_valid(ghcb)) 1528 goto vmgexit_err; 1529 } 1530 break; 1531 case SVM_EXIT_MSR: 1532 if (!ghcb_rcx_is_valid(ghcb)) 1533 goto vmgexit_err; 1534 if (ghcb_get_sw_exit_info_1(ghcb)) { 1535 if (!ghcb_rax_is_valid(ghcb) || 1536 !ghcb_rdx_is_valid(ghcb)) 1537 goto vmgexit_err; 1538 } 1539 break; 1540 case SVM_EXIT_VMMCALL: 1541 if (!ghcb_rax_is_valid(ghcb) || 1542 !ghcb_cpl_is_valid(ghcb)) 1543 goto vmgexit_err; 1544 break; 1545 case SVM_EXIT_RDTSCP: 1546 break; 1547 case SVM_EXIT_WBINVD: 1548 break; 1549 case SVM_EXIT_MONITOR: 1550 if (!ghcb_rax_is_valid(ghcb) || 1551 !ghcb_rcx_is_valid(ghcb) || 1552 !ghcb_rdx_is_valid(ghcb)) 1553 goto vmgexit_err; 1554 break; 1555 case SVM_EXIT_MWAIT: 1556 if (!ghcb_rax_is_valid(ghcb) || 1557 !ghcb_rcx_is_valid(ghcb)) 1558 goto vmgexit_err; 1559 break; 1560 case SVM_VMGEXIT_MMIO_READ: 1561 case SVM_VMGEXIT_MMIO_WRITE: 1562 if (!ghcb_sw_scratch_is_valid(ghcb)) 1563 goto vmgexit_err; 1564 break; 1565 case SVM_VMGEXIT_NMI_COMPLETE: 1566 case SVM_VMGEXIT_AP_JUMP_TABLE: 1567 case SVM_VMGEXIT_UNSUPPORTED_EVENT: 1568 break; 1569 default: 1570 goto vmgexit_err; 1571 } 1572 1573 return 0; 1574 1575 vmgexit_err: 1576 vcpu = &svm->vcpu; 1577 1578 if (ghcb->ghcb_usage) { 1579 vcpu_unimpl(vcpu, "vmgexit: ghcb usage %#x is not valid\n", 1580 ghcb->ghcb_usage); 1581 } else { 1582 vcpu_unimpl(vcpu, "vmgexit: exit reason %#llx is not valid\n", 1583 exit_code); 1584 dump_ghcb(svm); 1585 } 1586 1587 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR; 1588 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_UNEXPECTED_EXIT_REASON; 1589 vcpu->run->internal.ndata = 2; 1590 vcpu->run->internal.data[0] = exit_code; 1591 vcpu->run->internal.data[1] = vcpu->arch.last_vmentry_cpu; 1592 1593 return -EINVAL; 1594 } 1595 1596 static void pre_sev_es_run(struct vcpu_svm *svm) 1597 { 1598 if (!svm->ghcb) 1599 return; 1600 1601 if (svm->ghcb_sa_free) { 1602 /* 1603 * The scratch area lives outside the GHCB, so there is a 1604 * buffer that, depending on the operation performed, may 1605 * need to be synced, then freed. 1606 */ 1607 if (svm->ghcb_sa_sync) { 1608 kvm_write_guest(svm->vcpu.kvm, 1609 ghcb_get_sw_scratch(svm->ghcb), 1610 svm->ghcb_sa, svm->ghcb_sa_len); 1611 svm->ghcb_sa_sync = false; 1612 } 1613 1614 kfree(svm->ghcb_sa); 1615 svm->ghcb_sa = NULL; 1616 svm->ghcb_sa_free = false; 1617 } 1618 1619 trace_kvm_vmgexit_exit(svm->vcpu.vcpu_id, svm->ghcb); 1620 1621 sev_es_sync_to_ghcb(svm); 1622 1623 kvm_vcpu_unmap(&svm->vcpu, &svm->ghcb_map, true); 1624 svm->ghcb = NULL; 1625 } 1626 1627 void pre_sev_run(struct vcpu_svm *svm, int cpu) 1628 { 1629 struct svm_cpu_data *sd = per_cpu(svm_data, cpu); 1630 int asid = sev_get_asid(svm->vcpu.kvm); 1631 1632 /* Perform any SEV-ES pre-run actions */ 1633 pre_sev_es_run(svm); 1634 1635 /* Assign the asid allocated with this SEV guest */ 1636 svm->asid = asid; 1637 1638 /* 1639 * Flush guest TLB: 1640 * 1641 * 1) when different VMCB for the same ASID is to be run on the same host CPU. 1642 * 2) or this VMCB was executed on different host CPU in previous VMRUNs. 1643 */ 1644 if (sd->sev_vmcbs[asid] == svm->vmcb && 1645 svm->vcpu.arch.last_vmentry_cpu == cpu) 1646 return; 1647 1648 sd->sev_vmcbs[asid] = svm->vmcb; 1649 svm->vmcb->control.tlb_ctl = TLB_CONTROL_FLUSH_ASID; 1650 vmcb_mark_dirty(svm->vmcb, VMCB_ASID); 1651 } 1652 1653 #define GHCB_SCRATCH_AREA_LIMIT (16ULL * PAGE_SIZE) 1654 static bool setup_vmgexit_scratch(struct vcpu_svm *svm, bool sync, u64 len) 1655 { 1656 struct vmcb_control_area *control = &svm->vmcb->control; 1657 struct ghcb *ghcb = svm->ghcb; 1658 u64 ghcb_scratch_beg, ghcb_scratch_end; 1659 u64 scratch_gpa_beg, scratch_gpa_end; 1660 void *scratch_va; 1661 1662 scratch_gpa_beg = ghcb_get_sw_scratch(ghcb); 1663 if (!scratch_gpa_beg) { 1664 pr_err("vmgexit: scratch gpa not provided\n"); 1665 return false; 1666 } 1667 1668 scratch_gpa_end = scratch_gpa_beg + len; 1669 if (scratch_gpa_end < scratch_gpa_beg) { 1670 pr_err("vmgexit: scratch length (%#llx) not valid for scratch address (%#llx)\n", 1671 len, scratch_gpa_beg); 1672 return false; 1673 } 1674 1675 if ((scratch_gpa_beg & PAGE_MASK) == control->ghcb_gpa) { 1676 /* Scratch area begins within GHCB */ 1677 ghcb_scratch_beg = control->ghcb_gpa + 1678 offsetof(struct ghcb, shared_buffer); 1679 ghcb_scratch_end = control->ghcb_gpa + 1680 offsetof(struct ghcb, reserved_1); 1681 1682 /* 1683 * If the scratch area begins within the GHCB, it must be 1684 * completely contained in the GHCB shared buffer area. 1685 */ 1686 if (scratch_gpa_beg < ghcb_scratch_beg || 1687 scratch_gpa_end > ghcb_scratch_end) { 1688 pr_err("vmgexit: scratch area is outside of GHCB shared buffer area (%#llx - %#llx)\n", 1689 scratch_gpa_beg, scratch_gpa_end); 1690 return false; 1691 } 1692 1693 scratch_va = (void *)svm->ghcb; 1694 scratch_va += (scratch_gpa_beg - control->ghcb_gpa); 1695 } else { 1696 /* 1697 * The guest memory must be read into a kernel buffer, so 1698 * limit the size 1699 */ 1700 if (len > GHCB_SCRATCH_AREA_LIMIT) { 1701 pr_err("vmgexit: scratch area exceeds KVM limits (%#llx requested, %#llx limit)\n", 1702 len, GHCB_SCRATCH_AREA_LIMIT); 1703 return false; 1704 } 1705 scratch_va = kzalloc(len, GFP_KERNEL); 1706 if (!scratch_va) 1707 return false; 1708 1709 if (kvm_read_guest(svm->vcpu.kvm, scratch_gpa_beg, scratch_va, len)) { 1710 /* Unable to copy scratch area from guest */ 1711 pr_err("vmgexit: kvm_read_guest for scratch area failed\n"); 1712 1713 kfree(scratch_va); 1714 return false; 1715 } 1716 1717 /* 1718 * The scratch area is outside the GHCB. The operation will 1719 * dictate whether the buffer needs to be synced before running 1720 * the vCPU next time (i.e. a read was requested so the data 1721 * must be written back to the guest memory). 1722 */ 1723 svm->ghcb_sa_sync = sync; 1724 svm->ghcb_sa_free = true; 1725 } 1726 1727 svm->ghcb_sa = scratch_va; 1728 svm->ghcb_sa_len = len; 1729 1730 return true; 1731 } 1732 1733 static void set_ghcb_msr_bits(struct vcpu_svm *svm, u64 value, u64 mask, 1734 unsigned int pos) 1735 { 1736 svm->vmcb->control.ghcb_gpa &= ~(mask << pos); 1737 svm->vmcb->control.ghcb_gpa |= (value & mask) << pos; 1738 } 1739 1740 static u64 get_ghcb_msr_bits(struct vcpu_svm *svm, u64 mask, unsigned int pos) 1741 { 1742 return (svm->vmcb->control.ghcb_gpa >> pos) & mask; 1743 } 1744 1745 static void set_ghcb_msr(struct vcpu_svm *svm, u64 value) 1746 { 1747 svm->vmcb->control.ghcb_gpa = value; 1748 } 1749 1750 static int sev_handle_vmgexit_msr_protocol(struct vcpu_svm *svm) 1751 { 1752 struct vmcb_control_area *control = &svm->vmcb->control; 1753 struct kvm_vcpu *vcpu = &svm->vcpu; 1754 u64 ghcb_info; 1755 int ret = 1; 1756 1757 ghcb_info = control->ghcb_gpa & GHCB_MSR_INFO_MASK; 1758 1759 trace_kvm_vmgexit_msr_protocol_enter(svm->vcpu.vcpu_id, 1760 control->ghcb_gpa); 1761 1762 switch (ghcb_info) { 1763 case GHCB_MSR_SEV_INFO_REQ: 1764 set_ghcb_msr(svm, GHCB_MSR_SEV_INFO(GHCB_VERSION_MAX, 1765 GHCB_VERSION_MIN, 1766 sev_enc_bit)); 1767 break; 1768 case GHCB_MSR_CPUID_REQ: { 1769 u64 cpuid_fn, cpuid_reg, cpuid_value; 1770 1771 cpuid_fn = get_ghcb_msr_bits(svm, 1772 GHCB_MSR_CPUID_FUNC_MASK, 1773 GHCB_MSR_CPUID_FUNC_POS); 1774 1775 /* Initialize the registers needed by the CPUID intercept */ 1776 vcpu->arch.regs[VCPU_REGS_RAX] = cpuid_fn; 1777 vcpu->arch.regs[VCPU_REGS_RCX] = 0; 1778 1779 ret = svm_invoke_exit_handler(svm, SVM_EXIT_CPUID); 1780 if (!ret) { 1781 ret = -EINVAL; 1782 break; 1783 } 1784 1785 cpuid_reg = get_ghcb_msr_bits(svm, 1786 GHCB_MSR_CPUID_REG_MASK, 1787 GHCB_MSR_CPUID_REG_POS); 1788 if (cpuid_reg == 0) 1789 cpuid_value = vcpu->arch.regs[VCPU_REGS_RAX]; 1790 else if (cpuid_reg == 1) 1791 cpuid_value = vcpu->arch.regs[VCPU_REGS_RBX]; 1792 else if (cpuid_reg == 2) 1793 cpuid_value = vcpu->arch.regs[VCPU_REGS_RCX]; 1794 else 1795 cpuid_value = vcpu->arch.regs[VCPU_REGS_RDX]; 1796 1797 set_ghcb_msr_bits(svm, cpuid_value, 1798 GHCB_MSR_CPUID_VALUE_MASK, 1799 GHCB_MSR_CPUID_VALUE_POS); 1800 1801 set_ghcb_msr_bits(svm, GHCB_MSR_CPUID_RESP, 1802 GHCB_MSR_INFO_MASK, 1803 GHCB_MSR_INFO_POS); 1804 break; 1805 } 1806 case GHCB_MSR_TERM_REQ: { 1807 u64 reason_set, reason_code; 1808 1809 reason_set = get_ghcb_msr_bits(svm, 1810 GHCB_MSR_TERM_REASON_SET_MASK, 1811 GHCB_MSR_TERM_REASON_SET_POS); 1812 reason_code = get_ghcb_msr_bits(svm, 1813 GHCB_MSR_TERM_REASON_MASK, 1814 GHCB_MSR_TERM_REASON_POS); 1815 pr_info("SEV-ES guest requested termination: %#llx:%#llx\n", 1816 reason_set, reason_code); 1817 fallthrough; 1818 } 1819 default: 1820 ret = -EINVAL; 1821 } 1822 1823 trace_kvm_vmgexit_msr_protocol_exit(svm->vcpu.vcpu_id, 1824 control->ghcb_gpa, ret); 1825 1826 return ret; 1827 } 1828 1829 int sev_handle_vmgexit(struct vcpu_svm *svm) 1830 { 1831 struct vmcb_control_area *control = &svm->vmcb->control; 1832 u64 ghcb_gpa, exit_code; 1833 struct ghcb *ghcb; 1834 int ret; 1835 1836 /* Validate the GHCB */ 1837 ghcb_gpa = control->ghcb_gpa; 1838 if (ghcb_gpa & GHCB_MSR_INFO_MASK) 1839 return sev_handle_vmgexit_msr_protocol(svm); 1840 1841 if (!ghcb_gpa) { 1842 vcpu_unimpl(&svm->vcpu, "vmgexit: GHCB gpa is not set\n"); 1843 return -EINVAL; 1844 } 1845 1846 if (kvm_vcpu_map(&svm->vcpu, ghcb_gpa >> PAGE_SHIFT, &svm->ghcb_map)) { 1847 /* Unable to map GHCB from guest */ 1848 vcpu_unimpl(&svm->vcpu, "vmgexit: error mapping GHCB [%#llx] from guest\n", 1849 ghcb_gpa); 1850 return -EINVAL; 1851 } 1852 1853 svm->ghcb = svm->ghcb_map.hva; 1854 ghcb = svm->ghcb_map.hva; 1855 1856 trace_kvm_vmgexit_enter(svm->vcpu.vcpu_id, ghcb); 1857 1858 exit_code = ghcb_get_sw_exit_code(ghcb); 1859 1860 ret = sev_es_validate_vmgexit(svm); 1861 if (ret) 1862 return ret; 1863 1864 sev_es_sync_from_ghcb(svm); 1865 ghcb_set_sw_exit_info_1(ghcb, 0); 1866 ghcb_set_sw_exit_info_2(ghcb, 0); 1867 1868 ret = -EINVAL; 1869 switch (exit_code) { 1870 case SVM_VMGEXIT_MMIO_READ: 1871 if (!setup_vmgexit_scratch(svm, true, control->exit_info_2)) 1872 break; 1873 1874 ret = kvm_sev_es_mmio_read(&svm->vcpu, 1875 control->exit_info_1, 1876 control->exit_info_2, 1877 svm->ghcb_sa); 1878 break; 1879 case SVM_VMGEXIT_MMIO_WRITE: 1880 if (!setup_vmgexit_scratch(svm, false, control->exit_info_2)) 1881 break; 1882 1883 ret = kvm_sev_es_mmio_write(&svm->vcpu, 1884 control->exit_info_1, 1885 control->exit_info_2, 1886 svm->ghcb_sa); 1887 break; 1888 case SVM_VMGEXIT_NMI_COMPLETE: 1889 ret = svm_invoke_exit_handler(svm, SVM_EXIT_IRET); 1890 break; 1891 case SVM_VMGEXIT_AP_JUMP_TABLE: { 1892 struct kvm_sev_info *sev = &to_kvm_svm(svm->vcpu.kvm)->sev_info; 1893 1894 switch (control->exit_info_1) { 1895 case 0: 1896 /* Set AP jump table address */ 1897 sev->ap_jump_table = control->exit_info_2; 1898 break; 1899 case 1: 1900 /* Get AP jump table address */ 1901 ghcb_set_sw_exit_info_2(ghcb, sev->ap_jump_table); 1902 break; 1903 default: 1904 pr_err("svm: vmgexit: unsupported AP jump table request - exit_info_1=%#llx\n", 1905 control->exit_info_1); 1906 ghcb_set_sw_exit_info_1(ghcb, 1); 1907 ghcb_set_sw_exit_info_2(ghcb, 1908 X86_TRAP_UD | 1909 SVM_EVTINJ_TYPE_EXEPT | 1910 SVM_EVTINJ_VALID); 1911 } 1912 1913 ret = 1; 1914 break; 1915 } 1916 case SVM_VMGEXIT_UNSUPPORTED_EVENT: 1917 vcpu_unimpl(&svm->vcpu, 1918 "vmgexit: unsupported event - exit_info_1=%#llx, exit_info_2=%#llx\n", 1919 control->exit_info_1, control->exit_info_2); 1920 break; 1921 default: 1922 ret = svm_invoke_exit_handler(svm, exit_code); 1923 } 1924 1925 return ret; 1926 } 1927 1928 int sev_es_string_io(struct vcpu_svm *svm, int size, unsigned int port, int in) 1929 { 1930 if (!setup_vmgexit_scratch(svm, in, svm->vmcb->control.exit_info_2)) 1931 return -EINVAL; 1932 1933 return kvm_sev_es_string_io(&svm->vcpu, size, port, 1934 svm->ghcb_sa, svm->ghcb_sa_len, in); 1935 } 1936 1937 void sev_es_init_vmcb(struct vcpu_svm *svm) 1938 { 1939 struct kvm_vcpu *vcpu = &svm->vcpu; 1940 1941 svm->vmcb->control.nested_ctl |= SVM_NESTED_CTL_SEV_ES_ENABLE; 1942 svm->vmcb->control.virt_ext |= LBR_CTL_ENABLE_MASK; 1943 1944 /* 1945 * An SEV-ES guest requires a VMSA area that is a separate from the 1946 * VMCB page. Do not include the encryption mask on the VMSA physical 1947 * address since hardware will access it using the guest key. 1948 */ 1949 svm->vmcb->control.vmsa_pa = __pa(svm->vmsa); 1950 1951 /* Can't intercept CR register access, HV can't modify CR registers */ 1952 svm_clr_intercept(svm, INTERCEPT_CR0_READ); 1953 svm_clr_intercept(svm, INTERCEPT_CR4_READ); 1954 svm_clr_intercept(svm, INTERCEPT_CR8_READ); 1955 svm_clr_intercept(svm, INTERCEPT_CR0_WRITE); 1956 svm_clr_intercept(svm, INTERCEPT_CR4_WRITE); 1957 svm_clr_intercept(svm, INTERCEPT_CR8_WRITE); 1958 1959 svm_clr_intercept(svm, INTERCEPT_SELECTIVE_CR0); 1960 1961 /* Track EFER/CR register changes */ 1962 svm_set_intercept(svm, TRAP_EFER_WRITE); 1963 svm_set_intercept(svm, TRAP_CR0_WRITE); 1964 svm_set_intercept(svm, TRAP_CR4_WRITE); 1965 svm_set_intercept(svm, TRAP_CR8_WRITE); 1966 1967 /* No support for enable_vmware_backdoor */ 1968 clr_exception_intercept(svm, GP_VECTOR); 1969 1970 /* Can't intercept XSETBV, HV can't modify XCR0 directly */ 1971 svm_clr_intercept(svm, INTERCEPT_XSETBV); 1972 1973 /* Clear intercepts on selected MSRs */ 1974 set_msr_interception(vcpu, svm->msrpm, MSR_EFER, 1, 1); 1975 set_msr_interception(vcpu, svm->msrpm, MSR_IA32_CR_PAT, 1, 1); 1976 set_msr_interception(vcpu, svm->msrpm, MSR_IA32_LASTBRANCHFROMIP, 1, 1); 1977 set_msr_interception(vcpu, svm->msrpm, MSR_IA32_LASTBRANCHTOIP, 1, 1); 1978 set_msr_interception(vcpu, svm->msrpm, MSR_IA32_LASTINTFROMIP, 1, 1); 1979 set_msr_interception(vcpu, svm->msrpm, MSR_IA32_LASTINTTOIP, 1, 1); 1980 } 1981 1982 void sev_es_create_vcpu(struct vcpu_svm *svm) 1983 { 1984 /* 1985 * Set the GHCB MSR value as per the GHCB specification when creating 1986 * a vCPU for an SEV-ES guest. 1987 */ 1988 set_ghcb_msr(svm, GHCB_MSR_SEV_INFO(GHCB_VERSION_MAX, 1989 GHCB_VERSION_MIN, 1990 sev_enc_bit)); 1991 } 1992 1993 void sev_es_vcpu_load(struct vcpu_svm *svm, int cpu) 1994 { 1995 struct svm_cpu_data *sd = per_cpu(svm_data, cpu); 1996 struct vmcb_save_area *hostsa; 1997 unsigned int i; 1998 1999 /* 2000 * As an SEV-ES guest, hardware will restore the host state on VMEXIT, 2001 * of which one step is to perform a VMLOAD. Since hardware does not 2002 * perform a VMSAVE on VMRUN, the host savearea must be updated. 2003 */ 2004 asm volatile(__ex("vmsave") : : "a" (__sme_page_pa(sd->save_area)) : "memory"); 2005 2006 /* 2007 * Certain MSRs are restored on VMEXIT, only save ones that aren't 2008 * restored. 2009 */ 2010 for (i = 0; i < NR_HOST_SAVE_USER_MSRS; i++) { 2011 if (host_save_user_msrs[i].sev_es_restored) 2012 continue; 2013 2014 rdmsrl(host_save_user_msrs[i].index, svm->host_user_msrs[i]); 2015 } 2016 2017 /* XCR0 is restored on VMEXIT, save the current host value */ 2018 hostsa = (struct vmcb_save_area *)(page_address(sd->save_area) + 0x400); 2019 hostsa->xcr0 = xgetbv(XCR_XFEATURE_ENABLED_MASK); 2020 2021 /* PKRU is restored on VMEXIT, save the curent host value */ 2022 hostsa->pkru = read_pkru(); 2023 2024 /* MSR_IA32_XSS is restored on VMEXIT, save the currnet host value */ 2025 hostsa->xss = host_xss; 2026 } 2027 2028 void sev_es_vcpu_put(struct vcpu_svm *svm) 2029 { 2030 unsigned int i; 2031 2032 /* 2033 * Certain MSRs are restored on VMEXIT and were saved with vmsave in 2034 * sev_es_vcpu_load() above. Only restore ones that weren't. 2035 */ 2036 for (i = 0; i < NR_HOST_SAVE_USER_MSRS; i++) { 2037 if (host_save_user_msrs[i].sev_es_restored) 2038 continue; 2039 2040 wrmsrl(host_save_user_msrs[i].index, svm->host_user_msrs[i]); 2041 } 2042 } 2043