1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * AMD Secure Encrypted Virtualization (SEV) interface 4 * 5 * Copyright (C) 2016,2019 Advanced Micro Devices, Inc. 6 * 7 * Author: Brijesh Singh <brijesh.singh@amd.com> 8 */ 9 10 #include <linux/module.h> 11 #include <linux/kernel.h> 12 #include <linux/kthread.h> 13 #include <linux/sched.h> 14 #include <linux/interrupt.h> 15 #include <linux/spinlock.h> 16 #include <linux/spinlock_types.h> 17 #include <linux/types.h> 18 #include <linux/mutex.h> 19 #include <linux/delay.h> 20 #include <linux/hw_random.h> 21 #include <linux/ccp.h> 22 #include <linux/firmware.h> 23 #include <linux/gfp.h> 24 25 #include <asm/smp.h> 26 27 #include "psp-dev.h" 28 #include "sev-dev.h" 29 30 #define DEVICE_NAME "sev" 31 #define SEV_FW_FILE "amd/sev.fw" 32 #define SEV_FW_NAME_SIZE 64 33 34 static DEFINE_MUTEX(sev_cmd_mutex); 35 static struct sev_misc_dev *misc_dev; 36 37 static int psp_cmd_timeout = 100; 38 module_param(psp_cmd_timeout, int, 0644); 39 MODULE_PARM_DESC(psp_cmd_timeout, " default timeout value, in seconds, for PSP commands"); 40 41 static int psp_probe_timeout = 5; 42 module_param(psp_probe_timeout, int, 0644); 43 MODULE_PARM_DESC(psp_probe_timeout, " default timeout value, in seconds, during PSP device probe"); 44 45 static bool psp_dead; 46 static int psp_timeout; 47 48 /* Trusted Memory Region (TMR): 49 * The TMR is a 1MB area that must be 1MB aligned. Use the page allocator 50 * to allocate the memory, which will return aligned memory for the specified 51 * allocation order. 52 */ 53 #define SEV_ES_TMR_SIZE (1024 * 1024) 54 static void *sev_es_tmr; 55 56 static inline bool sev_version_greater_or_equal(u8 maj, u8 min) 57 { 58 struct sev_device *sev = psp_master->sev_data; 59 60 if (sev->api_major > maj) 61 return true; 62 63 if (sev->api_major == maj && sev->api_minor >= min) 64 return true; 65 66 return false; 67 } 68 69 static void sev_irq_handler(int irq, void *data, unsigned int status) 70 { 71 struct sev_device *sev = data; 72 int reg; 73 74 /* Check if it is command completion: */ 75 if (!(status & SEV_CMD_COMPLETE)) 76 return; 77 78 /* Check if it is SEV command completion: */ 79 reg = ioread32(sev->io_regs + sev->vdata->cmdresp_reg); 80 if (reg & PSP_CMDRESP_RESP) { 81 sev->int_rcvd = 1; 82 wake_up(&sev->int_queue); 83 } 84 } 85 86 static int sev_wait_cmd_ioc(struct sev_device *sev, 87 unsigned int *reg, unsigned int timeout) 88 { 89 int ret; 90 91 ret = wait_event_timeout(sev->int_queue, 92 sev->int_rcvd, timeout * HZ); 93 if (!ret) 94 return -ETIMEDOUT; 95 96 *reg = ioread32(sev->io_regs + sev->vdata->cmdresp_reg); 97 98 return 0; 99 } 100 101 static int sev_cmd_buffer_len(int cmd) 102 { 103 switch (cmd) { 104 case SEV_CMD_INIT: return sizeof(struct sev_data_init); 105 case SEV_CMD_PLATFORM_STATUS: return sizeof(struct sev_user_data_status); 106 case SEV_CMD_PEK_CSR: return sizeof(struct sev_data_pek_csr); 107 case SEV_CMD_PEK_CERT_IMPORT: return sizeof(struct sev_data_pek_cert_import); 108 case SEV_CMD_PDH_CERT_EXPORT: return sizeof(struct sev_data_pdh_cert_export); 109 case SEV_CMD_LAUNCH_START: return sizeof(struct sev_data_launch_start); 110 case SEV_CMD_LAUNCH_UPDATE_DATA: return sizeof(struct sev_data_launch_update_data); 111 case SEV_CMD_LAUNCH_UPDATE_VMSA: return sizeof(struct sev_data_launch_update_vmsa); 112 case SEV_CMD_LAUNCH_FINISH: return sizeof(struct sev_data_launch_finish); 113 case SEV_CMD_LAUNCH_MEASURE: return sizeof(struct sev_data_launch_measure); 114 case SEV_CMD_ACTIVATE: return sizeof(struct sev_data_activate); 115 case SEV_CMD_DEACTIVATE: return sizeof(struct sev_data_deactivate); 116 case SEV_CMD_DECOMMISSION: return sizeof(struct sev_data_decommission); 117 case SEV_CMD_GUEST_STATUS: return sizeof(struct sev_data_guest_status); 118 case SEV_CMD_DBG_DECRYPT: return sizeof(struct sev_data_dbg); 119 case SEV_CMD_DBG_ENCRYPT: return sizeof(struct sev_data_dbg); 120 case SEV_CMD_SEND_START: return sizeof(struct sev_data_send_start); 121 case SEV_CMD_SEND_UPDATE_DATA: return sizeof(struct sev_data_send_update_data); 122 case SEV_CMD_SEND_UPDATE_VMSA: return sizeof(struct sev_data_send_update_vmsa); 123 case SEV_CMD_SEND_FINISH: return sizeof(struct sev_data_send_finish); 124 case SEV_CMD_RECEIVE_START: return sizeof(struct sev_data_receive_start); 125 case SEV_CMD_RECEIVE_FINISH: return sizeof(struct sev_data_receive_finish); 126 case SEV_CMD_RECEIVE_UPDATE_DATA: return sizeof(struct sev_data_receive_update_data); 127 case SEV_CMD_RECEIVE_UPDATE_VMSA: return sizeof(struct sev_data_receive_update_vmsa); 128 case SEV_CMD_LAUNCH_UPDATE_SECRET: return sizeof(struct sev_data_launch_secret); 129 case SEV_CMD_DOWNLOAD_FIRMWARE: return sizeof(struct sev_data_download_firmware); 130 case SEV_CMD_GET_ID: return sizeof(struct sev_data_get_id); 131 default: return 0; 132 } 133 134 return 0; 135 } 136 137 static int __sev_do_cmd_locked(int cmd, void *data, int *psp_ret) 138 { 139 struct psp_device *psp = psp_master; 140 struct sev_device *sev; 141 unsigned int phys_lsb, phys_msb; 142 unsigned int reg, ret = 0; 143 144 if (!psp || !psp->sev_data) 145 return -ENODEV; 146 147 if (psp_dead) 148 return -EBUSY; 149 150 sev = psp->sev_data; 151 152 /* Get the physical address of the command buffer */ 153 phys_lsb = data ? lower_32_bits(__psp_pa(data)) : 0; 154 phys_msb = data ? upper_32_bits(__psp_pa(data)) : 0; 155 156 dev_dbg(sev->dev, "sev command id %#x buffer 0x%08x%08x timeout %us\n", 157 cmd, phys_msb, phys_lsb, psp_timeout); 158 159 print_hex_dump_debug("(in): ", DUMP_PREFIX_OFFSET, 16, 2, data, 160 sev_cmd_buffer_len(cmd), false); 161 162 iowrite32(phys_lsb, sev->io_regs + sev->vdata->cmdbuff_addr_lo_reg); 163 iowrite32(phys_msb, sev->io_regs + sev->vdata->cmdbuff_addr_hi_reg); 164 165 sev->int_rcvd = 0; 166 167 reg = cmd; 168 reg <<= SEV_CMDRESP_CMD_SHIFT; 169 reg |= SEV_CMDRESP_IOC; 170 iowrite32(reg, sev->io_regs + sev->vdata->cmdresp_reg); 171 172 /* wait for command completion */ 173 ret = sev_wait_cmd_ioc(sev, ®, psp_timeout); 174 if (ret) { 175 if (psp_ret) 176 *psp_ret = 0; 177 178 dev_err(sev->dev, "sev command %#x timed out, disabling PSP\n", cmd); 179 psp_dead = true; 180 181 return ret; 182 } 183 184 psp_timeout = psp_cmd_timeout; 185 186 if (psp_ret) 187 *psp_ret = reg & PSP_CMDRESP_ERR_MASK; 188 189 if (reg & PSP_CMDRESP_ERR_MASK) { 190 dev_dbg(sev->dev, "sev command %#x failed (%#010x)\n", 191 cmd, reg & PSP_CMDRESP_ERR_MASK); 192 ret = -EIO; 193 } 194 195 print_hex_dump_debug("(out): ", DUMP_PREFIX_OFFSET, 16, 2, data, 196 sev_cmd_buffer_len(cmd), false); 197 198 return ret; 199 } 200 201 static int sev_do_cmd(int cmd, void *data, int *psp_ret) 202 { 203 int rc; 204 205 mutex_lock(&sev_cmd_mutex); 206 rc = __sev_do_cmd_locked(cmd, data, psp_ret); 207 mutex_unlock(&sev_cmd_mutex); 208 209 return rc; 210 } 211 212 static int __sev_platform_init_locked(int *error) 213 { 214 struct psp_device *psp = psp_master; 215 struct sev_device *sev; 216 int rc = 0; 217 218 if (!psp || !psp->sev_data) 219 return -ENODEV; 220 221 sev = psp->sev_data; 222 223 if (sev->state == SEV_STATE_INIT) 224 return 0; 225 226 if (sev_es_tmr) { 227 u64 tmr_pa; 228 229 /* 230 * Do not include the encryption mask on the physical 231 * address of the TMR (firmware should clear it anyway). 232 */ 233 tmr_pa = __pa(sev_es_tmr); 234 235 sev->init_cmd_buf.flags |= SEV_INIT_FLAGS_SEV_ES; 236 sev->init_cmd_buf.tmr_address = tmr_pa; 237 sev->init_cmd_buf.tmr_len = SEV_ES_TMR_SIZE; 238 } 239 240 rc = __sev_do_cmd_locked(SEV_CMD_INIT, &sev->init_cmd_buf, error); 241 if (rc) 242 return rc; 243 244 sev->state = SEV_STATE_INIT; 245 246 /* Prepare for first SEV guest launch after INIT */ 247 wbinvd_on_all_cpus(); 248 rc = __sev_do_cmd_locked(SEV_CMD_DF_FLUSH, NULL, error); 249 if (rc) 250 return rc; 251 252 dev_dbg(sev->dev, "SEV firmware initialized\n"); 253 254 return rc; 255 } 256 257 int sev_platform_init(int *error) 258 { 259 int rc; 260 261 mutex_lock(&sev_cmd_mutex); 262 rc = __sev_platform_init_locked(error); 263 mutex_unlock(&sev_cmd_mutex); 264 265 return rc; 266 } 267 EXPORT_SYMBOL_GPL(sev_platform_init); 268 269 static int __sev_platform_shutdown_locked(int *error) 270 { 271 struct sev_device *sev = psp_master->sev_data; 272 int ret; 273 274 ret = __sev_do_cmd_locked(SEV_CMD_SHUTDOWN, NULL, error); 275 if (ret) 276 return ret; 277 278 sev->state = SEV_STATE_UNINIT; 279 dev_dbg(sev->dev, "SEV firmware shutdown\n"); 280 281 return ret; 282 } 283 284 static int sev_platform_shutdown(int *error) 285 { 286 int rc; 287 288 mutex_lock(&sev_cmd_mutex); 289 rc = __sev_platform_shutdown_locked(NULL); 290 mutex_unlock(&sev_cmd_mutex); 291 292 return rc; 293 } 294 295 static int sev_get_platform_state(int *state, int *error) 296 { 297 struct sev_device *sev = psp_master->sev_data; 298 int rc; 299 300 rc = __sev_do_cmd_locked(SEV_CMD_PLATFORM_STATUS, 301 &sev->status_cmd_buf, error); 302 if (rc) 303 return rc; 304 305 *state = sev->status_cmd_buf.state; 306 return rc; 307 } 308 309 static int sev_ioctl_do_reset(struct sev_issue_cmd *argp, bool writable) 310 { 311 int state, rc; 312 313 if (!writable) 314 return -EPERM; 315 316 /* 317 * The SEV spec requires that FACTORY_RESET must be issued in 318 * UNINIT state. Before we go further lets check if any guest is 319 * active. 320 * 321 * If FW is in WORKING state then deny the request otherwise issue 322 * SHUTDOWN command do INIT -> UNINIT before issuing the FACTORY_RESET. 323 * 324 */ 325 rc = sev_get_platform_state(&state, &argp->error); 326 if (rc) 327 return rc; 328 329 if (state == SEV_STATE_WORKING) 330 return -EBUSY; 331 332 if (state == SEV_STATE_INIT) { 333 rc = __sev_platform_shutdown_locked(&argp->error); 334 if (rc) 335 return rc; 336 } 337 338 return __sev_do_cmd_locked(SEV_CMD_FACTORY_RESET, NULL, &argp->error); 339 } 340 341 static int sev_ioctl_do_platform_status(struct sev_issue_cmd *argp) 342 { 343 struct sev_device *sev = psp_master->sev_data; 344 struct sev_user_data_status *data = &sev->status_cmd_buf; 345 int ret; 346 347 ret = __sev_do_cmd_locked(SEV_CMD_PLATFORM_STATUS, data, &argp->error); 348 if (ret) 349 return ret; 350 351 if (copy_to_user((void __user *)argp->data, data, sizeof(*data))) 352 ret = -EFAULT; 353 354 return ret; 355 } 356 357 static int sev_ioctl_do_pek_pdh_gen(int cmd, struct sev_issue_cmd *argp, bool writable) 358 { 359 struct sev_device *sev = psp_master->sev_data; 360 int rc; 361 362 if (!writable) 363 return -EPERM; 364 365 if (sev->state == SEV_STATE_UNINIT) { 366 rc = __sev_platform_init_locked(&argp->error); 367 if (rc) 368 return rc; 369 } 370 371 return __sev_do_cmd_locked(cmd, NULL, &argp->error); 372 } 373 374 static int sev_ioctl_do_pek_csr(struct sev_issue_cmd *argp, bool writable) 375 { 376 struct sev_device *sev = psp_master->sev_data; 377 struct sev_user_data_pek_csr input; 378 struct sev_data_pek_csr *data; 379 void *blob = NULL; 380 int ret; 381 382 if (!writable) 383 return -EPERM; 384 385 if (copy_from_user(&input, (void __user *)argp->data, sizeof(input))) 386 return -EFAULT; 387 388 data = kzalloc(sizeof(*data), GFP_KERNEL); 389 if (!data) 390 return -ENOMEM; 391 392 /* userspace wants to query CSR length */ 393 if (!input.address || !input.length) 394 goto cmd; 395 396 /* allocate a physically contiguous buffer to store the CSR blob */ 397 if (input.length > SEV_FW_BLOB_MAX_SIZE) { 398 ret = -EFAULT; 399 goto e_free; 400 } 401 402 blob = kmalloc(input.length, GFP_KERNEL); 403 if (!blob) { 404 ret = -ENOMEM; 405 goto e_free; 406 } 407 408 data->address = __psp_pa(blob); 409 data->len = input.length; 410 411 cmd: 412 if (sev->state == SEV_STATE_UNINIT) { 413 ret = __sev_platform_init_locked(&argp->error); 414 if (ret) 415 goto e_free_blob; 416 } 417 418 ret = __sev_do_cmd_locked(SEV_CMD_PEK_CSR, data, &argp->error); 419 420 /* If we query the CSR length, FW responded with expected data. */ 421 input.length = data->len; 422 423 if (copy_to_user((void __user *)argp->data, &input, sizeof(input))) { 424 ret = -EFAULT; 425 goto e_free_blob; 426 } 427 428 if (blob) { 429 if (copy_to_user((void __user *)input.address, blob, input.length)) 430 ret = -EFAULT; 431 } 432 433 e_free_blob: 434 kfree(blob); 435 e_free: 436 kfree(data); 437 return ret; 438 } 439 440 void *psp_copy_user_blob(u64 __user uaddr, u32 len) 441 { 442 if (!uaddr || !len) 443 return ERR_PTR(-EINVAL); 444 445 /* verify that blob length does not exceed our limit */ 446 if (len > SEV_FW_BLOB_MAX_SIZE) 447 return ERR_PTR(-EINVAL); 448 449 return memdup_user((void __user *)(uintptr_t)uaddr, len); 450 } 451 EXPORT_SYMBOL_GPL(psp_copy_user_blob); 452 453 static int sev_get_api_version(void) 454 { 455 struct sev_device *sev = psp_master->sev_data; 456 struct sev_user_data_status *status; 457 int error = 0, ret; 458 459 status = &sev->status_cmd_buf; 460 ret = sev_platform_status(status, &error); 461 if (ret) { 462 dev_err(sev->dev, 463 "SEV: failed to get status. Error: %#x\n", error); 464 return 1; 465 } 466 467 sev->api_major = status->api_major; 468 sev->api_minor = status->api_minor; 469 sev->build = status->build; 470 sev->state = status->state; 471 472 return 0; 473 } 474 475 static int sev_get_firmware(struct device *dev, 476 const struct firmware **firmware) 477 { 478 char fw_name_specific[SEV_FW_NAME_SIZE]; 479 char fw_name_subset[SEV_FW_NAME_SIZE]; 480 481 snprintf(fw_name_specific, sizeof(fw_name_specific), 482 "amd/amd_sev_fam%.2xh_model%.2xh.sbin", 483 boot_cpu_data.x86, boot_cpu_data.x86_model); 484 485 snprintf(fw_name_subset, sizeof(fw_name_subset), 486 "amd/amd_sev_fam%.2xh_model%.1xxh.sbin", 487 boot_cpu_data.x86, (boot_cpu_data.x86_model & 0xf0) >> 4); 488 489 /* Check for SEV FW for a particular model. 490 * Ex. amd_sev_fam17h_model00h.sbin for Family 17h Model 00h 491 * 492 * or 493 * 494 * Check for SEV FW common to a subset of models. 495 * Ex. amd_sev_fam17h_model0xh.sbin for 496 * Family 17h Model 00h -- Family 17h Model 0Fh 497 * 498 * or 499 * 500 * Fall-back to using generic name: sev.fw 501 */ 502 if ((firmware_request_nowarn(firmware, fw_name_specific, dev) >= 0) || 503 (firmware_request_nowarn(firmware, fw_name_subset, dev) >= 0) || 504 (firmware_request_nowarn(firmware, SEV_FW_FILE, dev) >= 0)) 505 return 0; 506 507 return -ENOENT; 508 } 509 510 /* Don't fail if SEV FW couldn't be updated. Continue with existing SEV FW */ 511 static int sev_update_firmware(struct device *dev) 512 { 513 struct sev_data_download_firmware *data; 514 const struct firmware *firmware; 515 int ret, error, order; 516 struct page *p; 517 u64 data_size; 518 519 if (sev_get_firmware(dev, &firmware) == -ENOENT) { 520 dev_dbg(dev, "No SEV firmware file present\n"); 521 return -1; 522 } 523 524 /* 525 * SEV FW expects the physical address given to it to be 32 526 * byte aligned. Memory allocated has structure placed at the 527 * beginning followed by the firmware being passed to the SEV 528 * FW. Allocate enough memory for data structure + alignment 529 * padding + SEV FW. 530 */ 531 data_size = ALIGN(sizeof(struct sev_data_download_firmware), 32); 532 533 order = get_order(firmware->size + data_size); 534 p = alloc_pages(GFP_KERNEL, order); 535 if (!p) { 536 ret = -1; 537 goto fw_err; 538 } 539 540 /* 541 * Copy firmware data to a kernel allocated contiguous 542 * memory region. 543 */ 544 data = page_address(p); 545 memcpy(page_address(p) + data_size, firmware->data, firmware->size); 546 547 data->address = __psp_pa(page_address(p) + data_size); 548 data->len = firmware->size; 549 550 ret = sev_do_cmd(SEV_CMD_DOWNLOAD_FIRMWARE, data, &error); 551 if (ret) 552 dev_dbg(dev, "Failed to update SEV firmware: %#x\n", error); 553 else 554 dev_info(dev, "SEV firmware update successful\n"); 555 556 __free_pages(p, order); 557 558 fw_err: 559 release_firmware(firmware); 560 561 return ret; 562 } 563 564 static int sev_ioctl_do_pek_import(struct sev_issue_cmd *argp, bool writable) 565 { 566 struct sev_device *sev = psp_master->sev_data; 567 struct sev_user_data_pek_cert_import input; 568 struct sev_data_pek_cert_import *data; 569 void *pek_blob, *oca_blob; 570 int ret; 571 572 if (!writable) 573 return -EPERM; 574 575 if (copy_from_user(&input, (void __user *)argp->data, sizeof(input))) 576 return -EFAULT; 577 578 data = kzalloc(sizeof(*data), GFP_KERNEL); 579 if (!data) 580 return -ENOMEM; 581 582 /* copy PEK certificate blobs from userspace */ 583 pek_blob = psp_copy_user_blob(input.pek_cert_address, input.pek_cert_len); 584 if (IS_ERR(pek_blob)) { 585 ret = PTR_ERR(pek_blob); 586 goto e_free; 587 } 588 589 data->pek_cert_address = __psp_pa(pek_blob); 590 data->pek_cert_len = input.pek_cert_len; 591 592 /* copy PEK certificate blobs from userspace */ 593 oca_blob = psp_copy_user_blob(input.oca_cert_address, input.oca_cert_len); 594 if (IS_ERR(oca_blob)) { 595 ret = PTR_ERR(oca_blob); 596 goto e_free_pek; 597 } 598 599 data->oca_cert_address = __psp_pa(oca_blob); 600 data->oca_cert_len = input.oca_cert_len; 601 602 /* If platform is not in INIT state then transition it to INIT */ 603 if (sev->state != SEV_STATE_INIT) { 604 ret = __sev_platform_init_locked(&argp->error); 605 if (ret) 606 goto e_free_oca; 607 } 608 609 ret = __sev_do_cmd_locked(SEV_CMD_PEK_CERT_IMPORT, data, &argp->error); 610 611 e_free_oca: 612 kfree(oca_blob); 613 e_free_pek: 614 kfree(pek_blob); 615 e_free: 616 kfree(data); 617 return ret; 618 } 619 620 static int sev_ioctl_do_get_id2(struct sev_issue_cmd *argp) 621 { 622 struct sev_user_data_get_id2 input; 623 struct sev_data_get_id *data; 624 void *id_blob = NULL; 625 int ret; 626 627 /* SEV GET_ID is available from SEV API v0.16 and up */ 628 if (!sev_version_greater_or_equal(0, 16)) 629 return -ENOTSUPP; 630 631 if (copy_from_user(&input, (void __user *)argp->data, sizeof(input))) 632 return -EFAULT; 633 634 data = kzalloc(sizeof(*data), GFP_KERNEL); 635 if (!data) 636 return -ENOMEM; 637 638 if (input.address && input.length) { 639 id_blob = kmalloc(input.length, GFP_KERNEL); 640 if (!id_blob) { 641 kfree(data); 642 return -ENOMEM; 643 } 644 645 data->address = __psp_pa(id_blob); 646 data->len = input.length; 647 } 648 649 ret = __sev_do_cmd_locked(SEV_CMD_GET_ID, data, &argp->error); 650 651 /* 652 * Firmware will return the length of the ID value (either the minimum 653 * required length or the actual length written), return it to the user. 654 */ 655 input.length = data->len; 656 657 if (copy_to_user((void __user *)argp->data, &input, sizeof(input))) { 658 ret = -EFAULT; 659 goto e_free; 660 } 661 662 if (id_blob) { 663 if (copy_to_user((void __user *)input.address, 664 id_blob, data->len)) { 665 ret = -EFAULT; 666 goto e_free; 667 } 668 } 669 670 e_free: 671 kfree(id_blob); 672 kfree(data); 673 674 return ret; 675 } 676 677 static int sev_ioctl_do_get_id(struct sev_issue_cmd *argp) 678 { 679 struct sev_data_get_id *data; 680 u64 data_size, user_size; 681 void *id_blob, *mem; 682 int ret; 683 684 /* SEV GET_ID available from SEV API v0.16 and up */ 685 if (!sev_version_greater_or_equal(0, 16)) 686 return -ENOTSUPP; 687 688 /* SEV FW expects the buffer it fills with the ID to be 689 * 8-byte aligned. Memory allocated should be enough to 690 * hold data structure + alignment padding + memory 691 * where SEV FW writes the ID. 692 */ 693 data_size = ALIGN(sizeof(struct sev_data_get_id), 8); 694 user_size = sizeof(struct sev_user_data_get_id); 695 696 mem = kzalloc(data_size + user_size, GFP_KERNEL); 697 if (!mem) 698 return -ENOMEM; 699 700 data = mem; 701 id_blob = mem + data_size; 702 703 data->address = __psp_pa(id_blob); 704 data->len = user_size; 705 706 ret = __sev_do_cmd_locked(SEV_CMD_GET_ID, data, &argp->error); 707 if (!ret) { 708 if (copy_to_user((void __user *)argp->data, id_blob, data->len)) 709 ret = -EFAULT; 710 } 711 712 kfree(mem); 713 714 return ret; 715 } 716 717 static int sev_ioctl_do_pdh_export(struct sev_issue_cmd *argp, bool writable) 718 { 719 struct sev_device *sev = psp_master->sev_data; 720 struct sev_user_data_pdh_cert_export input; 721 void *pdh_blob = NULL, *cert_blob = NULL; 722 struct sev_data_pdh_cert_export *data; 723 int ret; 724 725 /* If platform is not in INIT state then transition it to INIT. */ 726 if (sev->state != SEV_STATE_INIT) { 727 if (!writable) 728 return -EPERM; 729 730 ret = __sev_platform_init_locked(&argp->error); 731 if (ret) 732 return ret; 733 } 734 735 if (copy_from_user(&input, (void __user *)argp->data, sizeof(input))) 736 return -EFAULT; 737 738 data = kzalloc(sizeof(*data), GFP_KERNEL); 739 if (!data) 740 return -ENOMEM; 741 742 /* Userspace wants to query the certificate length. */ 743 if (!input.pdh_cert_address || 744 !input.pdh_cert_len || 745 !input.cert_chain_address) 746 goto cmd; 747 748 /* Allocate a physically contiguous buffer to store the PDH blob. */ 749 if (input.pdh_cert_len > SEV_FW_BLOB_MAX_SIZE) { 750 ret = -EFAULT; 751 goto e_free; 752 } 753 754 /* Allocate a physically contiguous buffer to store the cert chain blob. */ 755 if (input.cert_chain_len > SEV_FW_BLOB_MAX_SIZE) { 756 ret = -EFAULT; 757 goto e_free; 758 } 759 760 pdh_blob = kmalloc(input.pdh_cert_len, GFP_KERNEL); 761 if (!pdh_blob) { 762 ret = -ENOMEM; 763 goto e_free; 764 } 765 766 data->pdh_cert_address = __psp_pa(pdh_blob); 767 data->pdh_cert_len = input.pdh_cert_len; 768 769 cert_blob = kmalloc(input.cert_chain_len, GFP_KERNEL); 770 if (!cert_blob) { 771 ret = -ENOMEM; 772 goto e_free_pdh; 773 } 774 775 data->cert_chain_address = __psp_pa(cert_blob); 776 data->cert_chain_len = input.cert_chain_len; 777 778 cmd: 779 ret = __sev_do_cmd_locked(SEV_CMD_PDH_CERT_EXPORT, data, &argp->error); 780 781 /* If we query the length, FW responded with expected data. */ 782 input.cert_chain_len = data->cert_chain_len; 783 input.pdh_cert_len = data->pdh_cert_len; 784 785 if (copy_to_user((void __user *)argp->data, &input, sizeof(input))) { 786 ret = -EFAULT; 787 goto e_free_cert; 788 } 789 790 if (pdh_blob) { 791 if (copy_to_user((void __user *)input.pdh_cert_address, 792 pdh_blob, input.pdh_cert_len)) { 793 ret = -EFAULT; 794 goto e_free_cert; 795 } 796 } 797 798 if (cert_blob) { 799 if (copy_to_user((void __user *)input.cert_chain_address, 800 cert_blob, input.cert_chain_len)) 801 ret = -EFAULT; 802 } 803 804 e_free_cert: 805 kfree(cert_blob); 806 e_free_pdh: 807 kfree(pdh_blob); 808 e_free: 809 kfree(data); 810 return ret; 811 } 812 813 static long sev_ioctl(struct file *file, unsigned int ioctl, unsigned long arg) 814 { 815 void __user *argp = (void __user *)arg; 816 struct sev_issue_cmd input; 817 int ret = -EFAULT; 818 bool writable = file->f_mode & FMODE_WRITE; 819 820 if (!psp_master || !psp_master->sev_data) 821 return -ENODEV; 822 823 if (ioctl != SEV_ISSUE_CMD) 824 return -EINVAL; 825 826 if (copy_from_user(&input, argp, sizeof(struct sev_issue_cmd))) 827 return -EFAULT; 828 829 if (input.cmd > SEV_MAX) 830 return -EINVAL; 831 832 mutex_lock(&sev_cmd_mutex); 833 834 switch (input.cmd) { 835 836 case SEV_FACTORY_RESET: 837 ret = sev_ioctl_do_reset(&input, writable); 838 break; 839 case SEV_PLATFORM_STATUS: 840 ret = sev_ioctl_do_platform_status(&input); 841 break; 842 case SEV_PEK_GEN: 843 ret = sev_ioctl_do_pek_pdh_gen(SEV_CMD_PEK_GEN, &input, writable); 844 break; 845 case SEV_PDH_GEN: 846 ret = sev_ioctl_do_pek_pdh_gen(SEV_CMD_PDH_GEN, &input, writable); 847 break; 848 case SEV_PEK_CSR: 849 ret = sev_ioctl_do_pek_csr(&input, writable); 850 break; 851 case SEV_PEK_CERT_IMPORT: 852 ret = sev_ioctl_do_pek_import(&input, writable); 853 break; 854 case SEV_PDH_CERT_EXPORT: 855 ret = sev_ioctl_do_pdh_export(&input, writable); 856 break; 857 case SEV_GET_ID: 858 pr_warn_once("SEV_GET_ID command is deprecated, use SEV_GET_ID2\n"); 859 ret = sev_ioctl_do_get_id(&input); 860 break; 861 case SEV_GET_ID2: 862 ret = sev_ioctl_do_get_id2(&input); 863 break; 864 default: 865 ret = -EINVAL; 866 goto out; 867 } 868 869 if (copy_to_user(argp, &input, sizeof(struct sev_issue_cmd))) 870 ret = -EFAULT; 871 out: 872 mutex_unlock(&sev_cmd_mutex); 873 874 return ret; 875 } 876 877 static const struct file_operations sev_fops = { 878 .owner = THIS_MODULE, 879 .unlocked_ioctl = sev_ioctl, 880 }; 881 882 int sev_platform_status(struct sev_user_data_status *data, int *error) 883 { 884 return sev_do_cmd(SEV_CMD_PLATFORM_STATUS, data, error); 885 } 886 EXPORT_SYMBOL_GPL(sev_platform_status); 887 888 int sev_guest_deactivate(struct sev_data_deactivate *data, int *error) 889 { 890 return sev_do_cmd(SEV_CMD_DEACTIVATE, data, error); 891 } 892 EXPORT_SYMBOL_GPL(sev_guest_deactivate); 893 894 int sev_guest_activate(struct sev_data_activate *data, int *error) 895 { 896 return sev_do_cmd(SEV_CMD_ACTIVATE, data, error); 897 } 898 EXPORT_SYMBOL_GPL(sev_guest_activate); 899 900 int sev_guest_decommission(struct sev_data_decommission *data, int *error) 901 { 902 return sev_do_cmd(SEV_CMD_DECOMMISSION, data, error); 903 } 904 EXPORT_SYMBOL_GPL(sev_guest_decommission); 905 906 int sev_guest_df_flush(int *error) 907 { 908 return sev_do_cmd(SEV_CMD_DF_FLUSH, NULL, error); 909 } 910 EXPORT_SYMBOL_GPL(sev_guest_df_flush); 911 912 static void sev_exit(struct kref *ref) 913 { 914 misc_deregister(&misc_dev->misc); 915 kfree(misc_dev); 916 misc_dev = NULL; 917 } 918 919 static int sev_misc_init(struct sev_device *sev) 920 { 921 struct device *dev = sev->dev; 922 int ret; 923 924 /* 925 * SEV feature support can be detected on multiple devices but the SEV 926 * FW commands must be issued on the master. During probe, we do not 927 * know the master hence we create /dev/sev on the first device probe. 928 * sev_do_cmd() finds the right master device to which to issue the 929 * command to the firmware. 930 */ 931 if (!misc_dev) { 932 struct miscdevice *misc; 933 934 misc_dev = kzalloc(sizeof(*misc_dev), GFP_KERNEL); 935 if (!misc_dev) 936 return -ENOMEM; 937 938 misc = &misc_dev->misc; 939 misc->minor = MISC_DYNAMIC_MINOR; 940 misc->name = DEVICE_NAME; 941 misc->fops = &sev_fops; 942 943 ret = misc_register(misc); 944 if (ret) 945 return ret; 946 947 kref_init(&misc_dev->refcount); 948 } else { 949 kref_get(&misc_dev->refcount); 950 } 951 952 init_waitqueue_head(&sev->int_queue); 953 sev->misc = misc_dev; 954 dev_dbg(dev, "registered SEV device\n"); 955 956 return 0; 957 } 958 959 int sev_dev_init(struct psp_device *psp) 960 { 961 struct device *dev = psp->dev; 962 struct sev_device *sev; 963 int ret = -ENOMEM; 964 965 sev = devm_kzalloc(dev, sizeof(*sev), GFP_KERNEL); 966 if (!sev) 967 goto e_err; 968 969 psp->sev_data = sev; 970 971 sev->dev = dev; 972 sev->psp = psp; 973 974 sev->io_regs = psp->io_regs; 975 976 sev->vdata = (struct sev_vdata *)psp->vdata->sev; 977 if (!sev->vdata) { 978 ret = -ENODEV; 979 dev_err(dev, "sev: missing driver data\n"); 980 goto e_err; 981 } 982 983 psp_set_sev_irq_handler(psp, sev_irq_handler, sev); 984 985 ret = sev_misc_init(sev); 986 if (ret) 987 goto e_irq; 988 989 dev_notice(dev, "sev enabled\n"); 990 991 return 0; 992 993 e_irq: 994 psp_clear_sev_irq_handler(psp); 995 e_err: 996 psp->sev_data = NULL; 997 998 dev_notice(dev, "sev initialization failed\n"); 999 1000 return ret; 1001 } 1002 1003 void sev_dev_destroy(struct psp_device *psp) 1004 { 1005 struct sev_device *sev = psp->sev_data; 1006 1007 if (!sev) 1008 return; 1009 1010 if (sev->misc) 1011 kref_put(&misc_dev->refcount, sev_exit); 1012 1013 psp_clear_sev_irq_handler(psp); 1014 } 1015 1016 int sev_issue_cmd_external_user(struct file *filep, unsigned int cmd, 1017 void *data, int *error) 1018 { 1019 if (!filep || filep->f_op != &sev_fops) 1020 return -EBADF; 1021 1022 return sev_do_cmd(cmd, data, error); 1023 } 1024 EXPORT_SYMBOL_GPL(sev_issue_cmd_external_user); 1025 1026 void sev_pci_init(void) 1027 { 1028 struct sev_device *sev = psp_master->sev_data; 1029 struct page *tmr_page; 1030 int error, rc; 1031 1032 if (!sev) 1033 return; 1034 1035 psp_timeout = psp_probe_timeout; 1036 1037 if (sev_get_api_version()) 1038 goto err; 1039 1040 /* 1041 * If platform is not in UNINIT state then firmware upgrade and/or 1042 * platform INIT command will fail. These command require UNINIT state. 1043 * 1044 * In a normal boot we should never run into case where the firmware 1045 * is not in UNINIT state on boot. But in case of kexec boot, a reboot 1046 * may not go through a typical shutdown sequence and may leave the 1047 * firmware in INIT or WORKING state. 1048 */ 1049 1050 if (sev->state != SEV_STATE_UNINIT) { 1051 sev_platform_shutdown(NULL); 1052 sev->state = SEV_STATE_UNINIT; 1053 } 1054 1055 if (sev_version_greater_or_equal(0, 15) && 1056 sev_update_firmware(sev->dev) == 0) 1057 sev_get_api_version(); 1058 1059 /* Obtain the TMR memory area for SEV-ES use */ 1060 tmr_page = alloc_pages(GFP_KERNEL, get_order(SEV_ES_TMR_SIZE)); 1061 if (tmr_page) { 1062 sev_es_tmr = page_address(tmr_page); 1063 } else { 1064 sev_es_tmr = NULL; 1065 dev_warn(sev->dev, 1066 "SEV: TMR allocation failed, SEV-ES support unavailable\n"); 1067 } 1068 1069 /* Initialize the platform */ 1070 rc = sev_platform_init(&error); 1071 if (rc && (error == SEV_RET_SECURE_DATA_INVALID)) { 1072 /* 1073 * INIT command returned an integrity check failure 1074 * status code, meaning that firmware load and 1075 * validation of SEV related persistent data has 1076 * failed and persistent state has been erased. 1077 * Retrying INIT command here should succeed. 1078 */ 1079 dev_dbg(sev->dev, "SEV: retrying INIT command"); 1080 rc = sev_platform_init(&error); 1081 } 1082 1083 if (rc) { 1084 dev_err(sev->dev, "SEV: failed to INIT error %#x\n", error); 1085 return; 1086 } 1087 1088 dev_info(sev->dev, "SEV API:%d.%d build:%d\n", sev->api_major, 1089 sev->api_minor, sev->build); 1090 1091 return; 1092 1093 err: 1094 psp_master->sev_data = NULL; 1095 } 1096 1097 void sev_pci_exit(void) 1098 { 1099 if (!psp_master->sev_data) 1100 return; 1101 1102 sev_platform_shutdown(NULL); 1103 1104 if (sev_es_tmr) { 1105 /* The TMR area was encrypted, flush it from the cache */ 1106 wbinvd_on_all_cpus(); 1107 1108 free_pages((unsigned long)sev_es_tmr, 1109 get_order(SEV_ES_TMR_SIZE)); 1110 sev_es_tmr = NULL; 1111 } 1112 } 1113