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 #include <linux/cpufeature.h> 25 #include <linux/fs.h> 26 #include <linux/fs_struct.h> 27 28 #include <asm/smp.h> 29 #include <asm/cacheflush.h> 30 31 #include "psp-dev.h" 32 #include "sev-dev.h" 33 34 #define DEVICE_NAME "sev" 35 #define SEV_FW_FILE "amd/sev.fw" 36 #define SEV_FW_NAME_SIZE 64 37 38 static DEFINE_MUTEX(sev_cmd_mutex); 39 static struct sev_misc_dev *misc_dev; 40 41 static int psp_cmd_timeout = 100; 42 module_param(psp_cmd_timeout, int, 0644); 43 MODULE_PARM_DESC(psp_cmd_timeout, " default timeout value, in seconds, for PSP commands"); 44 45 static int psp_probe_timeout = 5; 46 module_param(psp_probe_timeout, int, 0644); 47 MODULE_PARM_DESC(psp_probe_timeout, " default timeout value, in seconds, during PSP device probe"); 48 49 static char *init_ex_path; 50 module_param(init_ex_path, charp, 0444); 51 MODULE_PARM_DESC(init_ex_path, " Path for INIT_EX data; if set try INIT_EX"); 52 53 static bool psp_init_on_probe = true; 54 module_param(psp_init_on_probe, bool, 0444); 55 MODULE_PARM_DESC(psp_init_on_probe, " if true, the PSP will be initialized on module init. Else the PSP will be initialized on the first command requiring it"); 56 57 MODULE_FIRMWARE("amd/amd_sev_fam17h_model0xh.sbin"); /* 1st gen EPYC */ 58 MODULE_FIRMWARE("amd/amd_sev_fam17h_model3xh.sbin"); /* 2nd gen EPYC */ 59 MODULE_FIRMWARE("amd/amd_sev_fam19h_model0xh.sbin"); /* 3rd gen EPYC */ 60 MODULE_FIRMWARE("amd/amd_sev_fam19h_model1xh.sbin"); /* 4th gen EPYC */ 61 62 static bool psp_dead; 63 static int psp_timeout; 64 65 /* Trusted Memory Region (TMR): 66 * The TMR is a 1MB area that must be 1MB aligned. Use the page allocator 67 * to allocate the memory, which will return aligned memory for the specified 68 * allocation order. 69 */ 70 #define SEV_ES_TMR_SIZE (1024 * 1024) 71 static void *sev_es_tmr; 72 73 /* INIT_EX NV Storage: 74 * The NV Storage is a 32Kb area and must be 4Kb page aligned. Use the page 75 * allocator to allocate the memory, which will return aligned memory for the 76 * specified allocation order. 77 */ 78 #define NV_LENGTH (32 * 1024) 79 static void *sev_init_ex_buffer; 80 81 static inline bool sev_version_greater_or_equal(u8 maj, u8 min) 82 { 83 struct sev_device *sev = psp_master->sev_data; 84 85 if (sev->api_major > maj) 86 return true; 87 88 if (sev->api_major == maj && sev->api_minor >= min) 89 return true; 90 91 return false; 92 } 93 94 static void sev_irq_handler(int irq, void *data, unsigned int status) 95 { 96 struct sev_device *sev = data; 97 int reg; 98 99 /* Check if it is command completion: */ 100 if (!(status & SEV_CMD_COMPLETE)) 101 return; 102 103 /* Check if it is SEV command completion: */ 104 reg = ioread32(sev->io_regs + sev->vdata->cmdresp_reg); 105 if (reg & PSP_CMDRESP_RESP) { 106 sev->int_rcvd = 1; 107 wake_up(&sev->int_queue); 108 } 109 } 110 111 static int sev_wait_cmd_ioc(struct sev_device *sev, 112 unsigned int *reg, unsigned int timeout) 113 { 114 int ret; 115 116 ret = wait_event_timeout(sev->int_queue, 117 sev->int_rcvd, timeout * HZ); 118 if (!ret) 119 return -ETIMEDOUT; 120 121 *reg = ioread32(sev->io_regs + sev->vdata->cmdresp_reg); 122 123 return 0; 124 } 125 126 static int sev_cmd_buffer_len(int cmd) 127 { 128 switch (cmd) { 129 case SEV_CMD_INIT: return sizeof(struct sev_data_init); 130 case SEV_CMD_INIT_EX: return sizeof(struct sev_data_init_ex); 131 case SEV_CMD_PLATFORM_STATUS: return sizeof(struct sev_user_data_status); 132 case SEV_CMD_PEK_CSR: return sizeof(struct sev_data_pek_csr); 133 case SEV_CMD_PEK_CERT_IMPORT: return sizeof(struct sev_data_pek_cert_import); 134 case SEV_CMD_PDH_CERT_EXPORT: return sizeof(struct sev_data_pdh_cert_export); 135 case SEV_CMD_LAUNCH_START: return sizeof(struct sev_data_launch_start); 136 case SEV_CMD_LAUNCH_UPDATE_DATA: return sizeof(struct sev_data_launch_update_data); 137 case SEV_CMD_LAUNCH_UPDATE_VMSA: return sizeof(struct sev_data_launch_update_vmsa); 138 case SEV_CMD_LAUNCH_FINISH: return sizeof(struct sev_data_launch_finish); 139 case SEV_CMD_LAUNCH_MEASURE: return sizeof(struct sev_data_launch_measure); 140 case SEV_CMD_ACTIVATE: return sizeof(struct sev_data_activate); 141 case SEV_CMD_DEACTIVATE: return sizeof(struct sev_data_deactivate); 142 case SEV_CMD_DECOMMISSION: return sizeof(struct sev_data_decommission); 143 case SEV_CMD_GUEST_STATUS: return sizeof(struct sev_data_guest_status); 144 case SEV_CMD_DBG_DECRYPT: return sizeof(struct sev_data_dbg); 145 case SEV_CMD_DBG_ENCRYPT: return sizeof(struct sev_data_dbg); 146 case SEV_CMD_SEND_START: return sizeof(struct sev_data_send_start); 147 case SEV_CMD_SEND_UPDATE_DATA: return sizeof(struct sev_data_send_update_data); 148 case SEV_CMD_SEND_UPDATE_VMSA: return sizeof(struct sev_data_send_update_vmsa); 149 case SEV_CMD_SEND_FINISH: return sizeof(struct sev_data_send_finish); 150 case SEV_CMD_RECEIVE_START: return sizeof(struct sev_data_receive_start); 151 case SEV_CMD_RECEIVE_FINISH: return sizeof(struct sev_data_receive_finish); 152 case SEV_CMD_RECEIVE_UPDATE_DATA: return sizeof(struct sev_data_receive_update_data); 153 case SEV_CMD_RECEIVE_UPDATE_VMSA: return sizeof(struct sev_data_receive_update_vmsa); 154 case SEV_CMD_LAUNCH_UPDATE_SECRET: return sizeof(struct sev_data_launch_secret); 155 case SEV_CMD_DOWNLOAD_FIRMWARE: return sizeof(struct sev_data_download_firmware); 156 case SEV_CMD_GET_ID: return sizeof(struct sev_data_get_id); 157 case SEV_CMD_ATTESTATION_REPORT: return sizeof(struct sev_data_attestation_report); 158 case SEV_CMD_SEND_CANCEL: return sizeof(struct sev_data_send_cancel); 159 default: return 0; 160 } 161 162 return 0; 163 } 164 165 static void *sev_fw_alloc(unsigned long len) 166 { 167 struct page *page; 168 169 page = alloc_pages(GFP_KERNEL, get_order(len)); 170 if (!page) 171 return NULL; 172 173 return page_address(page); 174 } 175 176 static struct file *open_file_as_root(const char *filename, int flags, umode_t mode) 177 { 178 struct file *fp; 179 struct path root; 180 struct cred *cred; 181 const struct cred *old_cred; 182 183 task_lock(&init_task); 184 get_fs_root(init_task.fs, &root); 185 task_unlock(&init_task); 186 187 cred = prepare_creds(); 188 if (!cred) 189 return ERR_PTR(-ENOMEM); 190 cred->fsuid = GLOBAL_ROOT_UID; 191 old_cred = override_creds(cred); 192 193 fp = file_open_root(&root, filename, flags, mode); 194 path_put(&root); 195 196 revert_creds(old_cred); 197 198 return fp; 199 } 200 201 static int sev_read_init_ex_file(void) 202 { 203 struct sev_device *sev = psp_master->sev_data; 204 struct file *fp; 205 ssize_t nread; 206 207 lockdep_assert_held(&sev_cmd_mutex); 208 209 if (!sev_init_ex_buffer) 210 return -EOPNOTSUPP; 211 212 fp = open_file_as_root(init_ex_path, O_RDONLY, 0); 213 if (IS_ERR(fp)) { 214 int ret = PTR_ERR(fp); 215 216 if (ret == -ENOENT) { 217 dev_info(sev->dev, 218 "SEV: %s does not exist and will be created later.\n", 219 init_ex_path); 220 ret = 0; 221 } else { 222 dev_err(sev->dev, 223 "SEV: could not open %s for read, error %d\n", 224 init_ex_path, ret); 225 } 226 return ret; 227 } 228 229 nread = kernel_read(fp, sev_init_ex_buffer, NV_LENGTH, NULL); 230 if (nread != NV_LENGTH) { 231 dev_info(sev->dev, 232 "SEV: could not read %u bytes to non volatile memory area, ret %ld\n", 233 NV_LENGTH, nread); 234 } 235 236 dev_dbg(sev->dev, "SEV: read %ld bytes from NV file\n", nread); 237 filp_close(fp, NULL); 238 239 return 0; 240 } 241 242 static int sev_write_init_ex_file(void) 243 { 244 struct sev_device *sev = psp_master->sev_data; 245 struct file *fp; 246 loff_t offset = 0; 247 ssize_t nwrite; 248 249 lockdep_assert_held(&sev_cmd_mutex); 250 251 if (!sev_init_ex_buffer) 252 return 0; 253 254 fp = open_file_as_root(init_ex_path, O_CREAT | O_WRONLY, 0600); 255 if (IS_ERR(fp)) { 256 int ret = PTR_ERR(fp); 257 258 dev_err(sev->dev, 259 "SEV: could not open file for write, error %d\n", 260 ret); 261 return ret; 262 } 263 264 nwrite = kernel_write(fp, sev_init_ex_buffer, NV_LENGTH, &offset); 265 vfs_fsync(fp, 0); 266 filp_close(fp, NULL); 267 268 if (nwrite != NV_LENGTH) { 269 dev_err(sev->dev, 270 "SEV: failed to write %u bytes to non volatile memory area, ret %ld\n", 271 NV_LENGTH, nwrite); 272 return -EIO; 273 } 274 275 dev_dbg(sev->dev, "SEV: write successful to NV file\n"); 276 277 return 0; 278 } 279 280 static int sev_write_init_ex_file_if_required(int cmd_id) 281 { 282 lockdep_assert_held(&sev_cmd_mutex); 283 284 if (!sev_init_ex_buffer) 285 return 0; 286 287 /* 288 * Only a few platform commands modify the SPI/NV area, but none of the 289 * non-platform commands do. Only INIT(_EX), PLATFORM_RESET, PEK_GEN, 290 * PEK_CERT_IMPORT, and PDH_GEN do. 291 */ 292 switch (cmd_id) { 293 case SEV_CMD_FACTORY_RESET: 294 case SEV_CMD_INIT_EX: 295 case SEV_CMD_PDH_GEN: 296 case SEV_CMD_PEK_CERT_IMPORT: 297 case SEV_CMD_PEK_GEN: 298 break; 299 default: 300 return 0; 301 } 302 303 return sev_write_init_ex_file(); 304 } 305 306 static int __sev_do_cmd_locked(int cmd, void *data, int *psp_ret) 307 { 308 struct psp_device *psp = psp_master; 309 struct sev_device *sev; 310 unsigned int phys_lsb, phys_msb; 311 unsigned int reg, ret = 0; 312 int buf_len; 313 314 if (!psp || !psp->sev_data) 315 return -ENODEV; 316 317 if (psp_dead) 318 return -EBUSY; 319 320 sev = psp->sev_data; 321 322 buf_len = sev_cmd_buffer_len(cmd); 323 if (WARN_ON_ONCE(!data != !buf_len)) 324 return -EINVAL; 325 326 /* 327 * Copy the incoming data to driver's scratch buffer as __pa() will not 328 * work for some memory, e.g. vmalloc'd addresses, and @data may not be 329 * physically contiguous. 330 */ 331 if (data) 332 memcpy(sev->cmd_buf, data, buf_len); 333 334 /* Get the physical address of the command buffer */ 335 phys_lsb = data ? lower_32_bits(__psp_pa(sev->cmd_buf)) : 0; 336 phys_msb = data ? upper_32_bits(__psp_pa(sev->cmd_buf)) : 0; 337 338 dev_dbg(sev->dev, "sev command id %#x buffer 0x%08x%08x timeout %us\n", 339 cmd, phys_msb, phys_lsb, psp_timeout); 340 341 print_hex_dump_debug("(in): ", DUMP_PREFIX_OFFSET, 16, 2, data, 342 buf_len, false); 343 344 iowrite32(phys_lsb, sev->io_regs + sev->vdata->cmdbuff_addr_lo_reg); 345 iowrite32(phys_msb, sev->io_regs + sev->vdata->cmdbuff_addr_hi_reg); 346 347 sev->int_rcvd = 0; 348 349 reg = cmd; 350 reg <<= SEV_CMDRESP_CMD_SHIFT; 351 reg |= SEV_CMDRESP_IOC; 352 iowrite32(reg, sev->io_regs + sev->vdata->cmdresp_reg); 353 354 /* wait for command completion */ 355 ret = sev_wait_cmd_ioc(sev, ®, psp_timeout); 356 if (ret) { 357 if (psp_ret) 358 *psp_ret = 0; 359 360 dev_err(sev->dev, "sev command %#x timed out, disabling PSP\n", cmd); 361 psp_dead = true; 362 363 return ret; 364 } 365 366 psp_timeout = psp_cmd_timeout; 367 368 if (psp_ret) 369 *psp_ret = reg & PSP_CMDRESP_ERR_MASK; 370 371 if (reg & PSP_CMDRESP_ERR_MASK) { 372 dev_dbg(sev->dev, "sev command %#x failed (%#010x)\n", 373 cmd, reg & PSP_CMDRESP_ERR_MASK); 374 ret = -EIO; 375 } else { 376 ret = sev_write_init_ex_file_if_required(cmd); 377 } 378 379 print_hex_dump_debug("(out): ", DUMP_PREFIX_OFFSET, 16, 2, data, 380 buf_len, false); 381 382 /* 383 * Copy potential output from the PSP back to data. Do this even on 384 * failure in case the caller wants to glean something from the error. 385 */ 386 if (data) 387 memcpy(data, sev->cmd_buf, buf_len); 388 389 return ret; 390 } 391 392 static int sev_do_cmd(int cmd, void *data, int *psp_ret) 393 { 394 int rc; 395 396 mutex_lock(&sev_cmd_mutex); 397 rc = __sev_do_cmd_locked(cmd, data, psp_ret); 398 mutex_unlock(&sev_cmd_mutex); 399 400 return rc; 401 } 402 403 static int __sev_init_locked(int *error) 404 { 405 struct sev_data_init data; 406 407 memset(&data, 0, sizeof(data)); 408 if (sev_es_tmr) { 409 /* 410 * Do not include the encryption mask on the physical 411 * address of the TMR (firmware should clear it anyway). 412 */ 413 data.tmr_address = __pa(sev_es_tmr); 414 415 data.flags |= SEV_INIT_FLAGS_SEV_ES; 416 data.tmr_len = SEV_ES_TMR_SIZE; 417 } 418 419 return __sev_do_cmd_locked(SEV_CMD_INIT, &data, error); 420 } 421 422 static int __sev_init_ex_locked(int *error) 423 { 424 struct sev_data_init_ex data; 425 426 memset(&data, 0, sizeof(data)); 427 data.length = sizeof(data); 428 data.nv_address = __psp_pa(sev_init_ex_buffer); 429 data.nv_len = NV_LENGTH; 430 431 if (sev_es_tmr) { 432 /* 433 * Do not include the encryption mask on the physical 434 * address of the TMR (firmware should clear it anyway). 435 */ 436 data.tmr_address = __pa(sev_es_tmr); 437 438 data.flags |= SEV_INIT_FLAGS_SEV_ES; 439 data.tmr_len = SEV_ES_TMR_SIZE; 440 } 441 442 return __sev_do_cmd_locked(SEV_CMD_INIT_EX, &data, error); 443 } 444 445 static int __sev_platform_init_locked(int *error) 446 { 447 int rc = 0, psp_ret = SEV_RET_NO_FW_CALL; 448 struct psp_device *psp = psp_master; 449 int (*init_function)(int *error); 450 struct sev_device *sev; 451 452 if (!psp || !psp->sev_data) 453 return -ENODEV; 454 455 sev = psp->sev_data; 456 457 if (sev->state == SEV_STATE_INIT) 458 return 0; 459 460 if (sev_init_ex_buffer) { 461 init_function = __sev_init_ex_locked; 462 rc = sev_read_init_ex_file(); 463 if (rc) 464 return rc; 465 } else { 466 init_function = __sev_init_locked; 467 } 468 469 rc = init_function(&psp_ret); 470 if (rc && psp_ret == SEV_RET_SECURE_DATA_INVALID) { 471 /* 472 * Initialization command returned an integrity check failure 473 * status code, meaning that firmware load and validation of SEV 474 * related persistent data has failed. Retrying the 475 * initialization function should succeed by replacing the state 476 * with a reset state. 477 */ 478 dev_err(sev->dev, 479 "SEV: retrying INIT command because of SECURE_DATA_INVALID error. Retrying once to reset PSP SEV state."); 480 rc = init_function(&psp_ret); 481 } 482 483 if (error) 484 *error = psp_ret; 485 486 if (rc) 487 return rc; 488 489 sev->state = SEV_STATE_INIT; 490 491 /* Prepare for first SEV guest launch after INIT */ 492 wbinvd_on_all_cpus(); 493 rc = __sev_do_cmd_locked(SEV_CMD_DF_FLUSH, NULL, error); 494 if (rc) 495 return rc; 496 497 dev_dbg(sev->dev, "SEV firmware initialized\n"); 498 499 dev_info(sev->dev, "SEV API:%d.%d build:%d\n", sev->api_major, 500 sev->api_minor, sev->build); 501 502 return 0; 503 } 504 505 int sev_platform_init(int *error) 506 { 507 int rc; 508 509 mutex_lock(&sev_cmd_mutex); 510 rc = __sev_platform_init_locked(error); 511 mutex_unlock(&sev_cmd_mutex); 512 513 return rc; 514 } 515 EXPORT_SYMBOL_GPL(sev_platform_init); 516 517 static int __sev_platform_shutdown_locked(int *error) 518 { 519 struct sev_device *sev = psp_master->sev_data; 520 int ret; 521 522 if (!sev || sev->state == SEV_STATE_UNINIT) 523 return 0; 524 525 ret = __sev_do_cmd_locked(SEV_CMD_SHUTDOWN, NULL, error); 526 if (ret) 527 return ret; 528 529 sev->state = SEV_STATE_UNINIT; 530 dev_dbg(sev->dev, "SEV firmware shutdown\n"); 531 532 return ret; 533 } 534 535 static int sev_platform_shutdown(int *error) 536 { 537 int rc; 538 539 mutex_lock(&sev_cmd_mutex); 540 rc = __sev_platform_shutdown_locked(NULL); 541 mutex_unlock(&sev_cmd_mutex); 542 543 return rc; 544 } 545 546 static int sev_get_platform_state(int *state, int *error) 547 { 548 struct sev_user_data_status data; 549 int rc; 550 551 rc = __sev_do_cmd_locked(SEV_CMD_PLATFORM_STATUS, &data, error); 552 if (rc) 553 return rc; 554 555 *state = data.state; 556 return rc; 557 } 558 559 static int sev_ioctl_do_reset(struct sev_issue_cmd *argp, bool writable) 560 { 561 int state, rc; 562 563 if (!writable) 564 return -EPERM; 565 566 /* 567 * The SEV spec requires that FACTORY_RESET must be issued in 568 * UNINIT state. Before we go further lets check if any guest is 569 * active. 570 * 571 * If FW is in WORKING state then deny the request otherwise issue 572 * SHUTDOWN command do INIT -> UNINIT before issuing the FACTORY_RESET. 573 * 574 */ 575 rc = sev_get_platform_state(&state, &argp->error); 576 if (rc) 577 return rc; 578 579 if (state == SEV_STATE_WORKING) 580 return -EBUSY; 581 582 if (state == SEV_STATE_INIT) { 583 rc = __sev_platform_shutdown_locked(&argp->error); 584 if (rc) 585 return rc; 586 } 587 588 return __sev_do_cmd_locked(SEV_CMD_FACTORY_RESET, NULL, &argp->error); 589 } 590 591 static int sev_ioctl_do_platform_status(struct sev_issue_cmd *argp) 592 { 593 struct sev_user_data_status data; 594 int ret; 595 596 memset(&data, 0, sizeof(data)); 597 598 ret = __sev_do_cmd_locked(SEV_CMD_PLATFORM_STATUS, &data, &argp->error); 599 if (ret) 600 return ret; 601 602 if (copy_to_user((void __user *)argp->data, &data, sizeof(data))) 603 ret = -EFAULT; 604 605 return ret; 606 } 607 608 static int sev_ioctl_do_pek_pdh_gen(int cmd, struct sev_issue_cmd *argp, bool writable) 609 { 610 struct sev_device *sev = psp_master->sev_data; 611 int rc; 612 613 if (!writable) 614 return -EPERM; 615 616 if (sev->state == SEV_STATE_UNINIT) { 617 rc = __sev_platform_init_locked(&argp->error); 618 if (rc) 619 return rc; 620 } 621 622 return __sev_do_cmd_locked(cmd, NULL, &argp->error); 623 } 624 625 static int sev_ioctl_do_pek_csr(struct sev_issue_cmd *argp, bool writable) 626 { 627 struct sev_device *sev = psp_master->sev_data; 628 struct sev_user_data_pek_csr input; 629 struct sev_data_pek_csr data; 630 void __user *input_address; 631 void *blob = NULL; 632 int ret; 633 634 if (!writable) 635 return -EPERM; 636 637 if (copy_from_user(&input, (void __user *)argp->data, sizeof(input))) 638 return -EFAULT; 639 640 memset(&data, 0, sizeof(data)); 641 642 /* userspace wants to query CSR length */ 643 if (!input.address || !input.length) 644 goto cmd; 645 646 /* allocate a physically contiguous buffer to store the CSR blob */ 647 input_address = (void __user *)input.address; 648 if (input.length > SEV_FW_BLOB_MAX_SIZE) 649 return -EFAULT; 650 651 blob = kzalloc(input.length, GFP_KERNEL); 652 if (!blob) 653 return -ENOMEM; 654 655 data.address = __psp_pa(blob); 656 data.len = input.length; 657 658 cmd: 659 if (sev->state == SEV_STATE_UNINIT) { 660 ret = __sev_platform_init_locked(&argp->error); 661 if (ret) 662 goto e_free_blob; 663 } 664 665 ret = __sev_do_cmd_locked(SEV_CMD_PEK_CSR, &data, &argp->error); 666 667 /* If we query the CSR length, FW responded with expected data. */ 668 input.length = data.len; 669 670 if (copy_to_user((void __user *)argp->data, &input, sizeof(input))) { 671 ret = -EFAULT; 672 goto e_free_blob; 673 } 674 675 if (blob) { 676 if (copy_to_user(input_address, blob, input.length)) 677 ret = -EFAULT; 678 } 679 680 e_free_blob: 681 kfree(blob); 682 return ret; 683 } 684 685 void *psp_copy_user_blob(u64 uaddr, u32 len) 686 { 687 if (!uaddr || !len) 688 return ERR_PTR(-EINVAL); 689 690 /* verify that blob length does not exceed our limit */ 691 if (len > SEV_FW_BLOB_MAX_SIZE) 692 return ERR_PTR(-EINVAL); 693 694 return memdup_user((void __user *)uaddr, len); 695 } 696 EXPORT_SYMBOL_GPL(psp_copy_user_blob); 697 698 static int sev_get_api_version(void) 699 { 700 struct sev_device *sev = psp_master->sev_data; 701 struct sev_user_data_status status; 702 int error = 0, ret; 703 704 ret = sev_platform_status(&status, &error); 705 if (ret) { 706 dev_err(sev->dev, 707 "SEV: failed to get status. Error: %#x\n", error); 708 return 1; 709 } 710 711 sev->api_major = status.api_major; 712 sev->api_minor = status.api_minor; 713 sev->build = status.build; 714 sev->state = status.state; 715 716 return 0; 717 } 718 719 static int sev_get_firmware(struct device *dev, 720 const struct firmware **firmware) 721 { 722 char fw_name_specific[SEV_FW_NAME_SIZE]; 723 char fw_name_subset[SEV_FW_NAME_SIZE]; 724 725 snprintf(fw_name_specific, sizeof(fw_name_specific), 726 "amd/amd_sev_fam%.2xh_model%.2xh.sbin", 727 boot_cpu_data.x86, boot_cpu_data.x86_model); 728 729 snprintf(fw_name_subset, sizeof(fw_name_subset), 730 "amd/amd_sev_fam%.2xh_model%.1xxh.sbin", 731 boot_cpu_data.x86, (boot_cpu_data.x86_model & 0xf0) >> 4); 732 733 /* Check for SEV FW for a particular model. 734 * Ex. amd_sev_fam17h_model00h.sbin for Family 17h Model 00h 735 * 736 * or 737 * 738 * Check for SEV FW common to a subset of models. 739 * Ex. amd_sev_fam17h_model0xh.sbin for 740 * Family 17h Model 00h -- Family 17h Model 0Fh 741 * 742 * or 743 * 744 * Fall-back to using generic name: sev.fw 745 */ 746 if ((firmware_request_nowarn(firmware, fw_name_specific, dev) >= 0) || 747 (firmware_request_nowarn(firmware, fw_name_subset, dev) >= 0) || 748 (firmware_request_nowarn(firmware, SEV_FW_FILE, dev) >= 0)) 749 return 0; 750 751 return -ENOENT; 752 } 753 754 /* Don't fail if SEV FW couldn't be updated. Continue with existing SEV FW */ 755 static int sev_update_firmware(struct device *dev) 756 { 757 struct sev_data_download_firmware *data; 758 const struct firmware *firmware; 759 int ret, error, order; 760 struct page *p; 761 u64 data_size; 762 763 if (!sev_version_greater_or_equal(0, 15)) { 764 dev_dbg(dev, "DOWNLOAD_FIRMWARE not supported\n"); 765 return -1; 766 } 767 768 if (sev_get_firmware(dev, &firmware) == -ENOENT) { 769 dev_dbg(dev, "No SEV firmware file present\n"); 770 return -1; 771 } 772 773 /* 774 * SEV FW expects the physical address given to it to be 32 775 * byte aligned. Memory allocated has structure placed at the 776 * beginning followed by the firmware being passed to the SEV 777 * FW. Allocate enough memory for data structure + alignment 778 * padding + SEV FW. 779 */ 780 data_size = ALIGN(sizeof(struct sev_data_download_firmware), 32); 781 782 order = get_order(firmware->size + data_size); 783 p = alloc_pages(GFP_KERNEL, order); 784 if (!p) { 785 ret = -1; 786 goto fw_err; 787 } 788 789 /* 790 * Copy firmware data to a kernel allocated contiguous 791 * memory region. 792 */ 793 data = page_address(p); 794 memcpy(page_address(p) + data_size, firmware->data, firmware->size); 795 796 data->address = __psp_pa(page_address(p) + data_size); 797 data->len = firmware->size; 798 799 ret = sev_do_cmd(SEV_CMD_DOWNLOAD_FIRMWARE, data, &error); 800 801 /* 802 * A quirk for fixing the committed TCB version, when upgrading from 803 * earlier firmware version than 1.50. 804 */ 805 if (!ret && !sev_version_greater_or_equal(1, 50)) 806 ret = sev_do_cmd(SEV_CMD_DOWNLOAD_FIRMWARE, data, &error); 807 808 if (ret) 809 dev_dbg(dev, "Failed to update SEV firmware: %#x\n", error); 810 else 811 dev_info(dev, "SEV firmware update successful\n"); 812 813 __free_pages(p, order); 814 815 fw_err: 816 release_firmware(firmware); 817 818 return ret; 819 } 820 821 static int sev_ioctl_do_pek_import(struct sev_issue_cmd *argp, bool writable) 822 { 823 struct sev_device *sev = psp_master->sev_data; 824 struct sev_user_data_pek_cert_import input; 825 struct sev_data_pek_cert_import data; 826 void *pek_blob, *oca_blob; 827 int ret; 828 829 if (!writable) 830 return -EPERM; 831 832 if (copy_from_user(&input, (void __user *)argp->data, sizeof(input))) 833 return -EFAULT; 834 835 /* copy PEK certificate blobs from userspace */ 836 pek_blob = psp_copy_user_blob(input.pek_cert_address, input.pek_cert_len); 837 if (IS_ERR(pek_blob)) 838 return PTR_ERR(pek_blob); 839 840 data.reserved = 0; 841 data.pek_cert_address = __psp_pa(pek_blob); 842 data.pek_cert_len = input.pek_cert_len; 843 844 /* copy PEK certificate blobs from userspace */ 845 oca_blob = psp_copy_user_blob(input.oca_cert_address, input.oca_cert_len); 846 if (IS_ERR(oca_blob)) { 847 ret = PTR_ERR(oca_blob); 848 goto e_free_pek; 849 } 850 851 data.oca_cert_address = __psp_pa(oca_blob); 852 data.oca_cert_len = input.oca_cert_len; 853 854 /* If platform is not in INIT state then transition it to INIT */ 855 if (sev->state != SEV_STATE_INIT) { 856 ret = __sev_platform_init_locked(&argp->error); 857 if (ret) 858 goto e_free_oca; 859 } 860 861 ret = __sev_do_cmd_locked(SEV_CMD_PEK_CERT_IMPORT, &data, &argp->error); 862 863 e_free_oca: 864 kfree(oca_blob); 865 e_free_pek: 866 kfree(pek_blob); 867 return ret; 868 } 869 870 static int sev_ioctl_do_get_id2(struct sev_issue_cmd *argp) 871 { 872 struct sev_user_data_get_id2 input; 873 struct sev_data_get_id data; 874 void __user *input_address; 875 void *id_blob = NULL; 876 int ret; 877 878 /* SEV GET_ID is available from SEV API v0.16 and up */ 879 if (!sev_version_greater_or_equal(0, 16)) 880 return -ENOTSUPP; 881 882 if (copy_from_user(&input, (void __user *)argp->data, sizeof(input))) 883 return -EFAULT; 884 885 input_address = (void __user *)input.address; 886 887 if (input.address && input.length) { 888 /* 889 * The length of the ID shouldn't be assumed by software since 890 * it may change in the future. The allocation size is limited 891 * to 1 << (PAGE_SHIFT + MAX_ORDER - 1) by the page allocator. 892 * If the allocation fails, simply return ENOMEM rather than 893 * warning in the kernel log. 894 */ 895 id_blob = kzalloc(input.length, GFP_KERNEL | __GFP_NOWARN); 896 if (!id_blob) 897 return -ENOMEM; 898 899 data.address = __psp_pa(id_blob); 900 data.len = input.length; 901 } else { 902 data.address = 0; 903 data.len = 0; 904 } 905 906 ret = __sev_do_cmd_locked(SEV_CMD_GET_ID, &data, &argp->error); 907 908 /* 909 * Firmware will return the length of the ID value (either the minimum 910 * required length or the actual length written), return it to the user. 911 */ 912 input.length = data.len; 913 914 if (copy_to_user((void __user *)argp->data, &input, sizeof(input))) { 915 ret = -EFAULT; 916 goto e_free; 917 } 918 919 if (id_blob) { 920 if (copy_to_user(input_address, id_blob, data.len)) { 921 ret = -EFAULT; 922 goto e_free; 923 } 924 } 925 926 e_free: 927 kfree(id_blob); 928 929 return ret; 930 } 931 932 static int sev_ioctl_do_get_id(struct sev_issue_cmd *argp) 933 { 934 struct sev_data_get_id *data; 935 u64 data_size, user_size; 936 void *id_blob, *mem; 937 int ret; 938 939 /* SEV GET_ID available from SEV API v0.16 and up */ 940 if (!sev_version_greater_or_equal(0, 16)) 941 return -ENOTSUPP; 942 943 /* SEV FW expects the buffer it fills with the ID to be 944 * 8-byte aligned. Memory allocated should be enough to 945 * hold data structure + alignment padding + memory 946 * where SEV FW writes the ID. 947 */ 948 data_size = ALIGN(sizeof(struct sev_data_get_id), 8); 949 user_size = sizeof(struct sev_user_data_get_id); 950 951 mem = kzalloc(data_size + user_size, GFP_KERNEL); 952 if (!mem) 953 return -ENOMEM; 954 955 data = mem; 956 id_blob = mem + data_size; 957 958 data->address = __psp_pa(id_blob); 959 data->len = user_size; 960 961 ret = __sev_do_cmd_locked(SEV_CMD_GET_ID, data, &argp->error); 962 if (!ret) { 963 if (copy_to_user((void __user *)argp->data, id_blob, data->len)) 964 ret = -EFAULT; 965 } 966 967 kfree(mem); 968 969 return ret; 970 } 971 972 static int sev_ioctl_do_pdh_export(struct sev_issue_cmd *argp, bool writable) 973 { 974 struct sev_device *sev = psp_master->sev_data; 975 struct sev_user_data_pdh_cert_export input; 976 void *pdh_blob = NULL, *cert_blob = NULL; 977 struct sev_data_pdh_cert_export data; 978 void __user *input_cert_chain_address; 979 void __user *input_pdh_cert_address; 980 int ret; 981 982 /* If platform is not in INIT state then transition it to INIT. */ 983 if (sev->state != SEV_STATE_INIT) { 984 if (!writable) 985 return -EPERM; 986 987 ret = __sev_platform_init_locked(&argp->error); 988 if (ret) 989 return ret; 990 } 991 992 if (copy_from_user(&input, (void __user *)argp->data, sizeof(input))) 993 return -EFAULT; 994 995 memset(&data, 0, sizeof(data)); 996 997 /* Userspace wants to query the certificate length. */ 998 if (!input.pdh_cert_address || 999 !input.pdh_cert_len || 1000 !input.cert_chain_address) 1001 goto cmd; 1002 1003 input_pdh_cert_address = (void __user *)input.pdh_cert_address; 1004 input_cert_chain_address = (void __user *)input.cert_chain_address; 1005 1006 /* Allocate a physically contiguous buffer to store the PDH blob. */ 1007 if (input.pdh_cert_len > SEV_FW_BLOB_MAX_SIZE) 1008 return -EFAULT; 1009 1010 /* Allocate a physically contiguous buffer to store the cert chain blob. */ 1011 if (input.cert_chain_len > SEV_FW_BLOB_MAX_SIZE) 1012 return -EFAULT; 1013 1014 pdh_blob = kzalloc(input.pdh_cert_len, GFP_KERNEL); 1015 if (!pdh_blob) 1016 return -ENOMEM; 1017 1018 data.pdh_cert_address = __psp_pa(pdh_blob); 1019 data.pdh_cert_len = input.pdh_cert_len; 1020 1021 cert_blob = kzalloc(input.cert_chain_len, GFP_KERNEL); 1022 if (!cert_blob) { 1023 ret = -ENOMEM; 1024 goto e_free_pdh; 1025 } 1026 1027 data.cert_chain_address = __psp_pa(cert_blob); 1028 data.cert_chain_len = input.cert_chain_len; 1029 1030 cmd: 1031 ret = __sev_do_cmd_locked(SEV_CMD_PDH_CERT_EXPORT, &data, &argp->error); 1032 1033 /* If we query the length, FW responded with expected data. */ 1034 input.cert_chain_len = data.cert_chain_len; 1035 input.pdh_cert_len = data.pdh_cert_len; 1036 1037 if (copy_to_user((void __user *)argp->data, &input, sizeof(input))) { 1038 ret = -EFAULT; 1039 goto e_free_cert; 1040 } 1041 1042 if (pdh_blob) { 1043 if (copy_to_user(input_pdh_cert_address, 1044 pdh_blob, input.pdh_cert_len)) { 1045 ret = -EFAULT; 1046 goto e_free_cert; 1047 } 1048 } 1049 1050 if (cert_blob) { 1051 if (copy_to_user(input_cert_chain_address, 1052 cert_blob, input.cert_chain_len)) 1053 ret = -EFAULT; 1054 } 1055 1056 e_free_cert: 1057 kfree(cert_blob); 1058 e_free_pdh: 1059 kfree(pdh_blob); 1060 return ret; 1061 } 1062 1063 static long sev_ioctl(struct file *file, unsigned int ioctl, unsigned long arg) 1064 { 1065 void __user *argp = (void __user *)arg; 1066 struct sev_issue_cmd input; 1067 int ret = -EFAULT; 1068 bool writable = file->f_mode & FMODE_WRITE; 1069 1070 if (!psp_master || !psp_master->sev_data) 1071 return -ENODEV; 1072 1073 if (ioctl != SEV_ISSUE_CMD) 1074 return -EINVAL; 1075 1076 if (copy_from_user(&input, argp, sizeof(struct sev_issue_cmd))) 1077 return -EFAULT; 1078 1079 if (input.cmd > SEV_MAX) 1080 return -EINVAL; 1081 1082 mutex_lock(&sev_cmd_mutex); 1083 1084 switch (input.cmd) { 1085 1086 case SEV_FACTORY_RESET: 1087 ret = sev_ioctl_do_reset(&input, writable); 1088 break; 1089 case SEV_PLATFORM_STATUS: 1090 ret = sev_ioctl_do_platform_status(&input); 1091 break; 1092 case SEV_PEK_GEN: 1093 ret = sev_ioctl_do_pek_pdh_gen(SEV_CMD_PEK_GEN, &input, writable); 1094 break; 1095 case SEV_PDH_GEN: 1096 ret = sev_ioctl_do_pek_pdh_gen(SEV_CMD_PDH_GEN, &input, writable); 1097 break; 1098 case SEV_PEK_CSR: 1099 ret = sev_ioctl_do_pek_csr(&input, writable); 1100 break; 1101 case SEV_PEK_CERT_IMPORT: 1102 ret = sev_ioctl_do_pek_import(&input, writable); 1103 break; 1104 case SEV_PDH_CERT_EXPORT: 1105 ret = sev_ioctl_do_pdh_export(&input, writable); 1106 break; 1107 case SEV_GET_ID: 1108 pr_warn_once("SEV_GET_ID command is deprecated, use SEV_GET_ID2\n"); 1109 ret = sev_ioctl_do_get_id(&input); 1110 break; 1111 case SEV_GET_ID2: 1112 ret = sev_ioctl_do_get_id2(&input); 1113 break; 1114 default: 1115 ret = -EINVAL; 1116 goto out; 1117 } 1118 1119 if (copy_to_user(argp, &input, sizeof(struct sev_issue_cmd))) 1120 ret = -EFAULT; 1121 out: 1122 mutex_unlock(&sev_cmd_mutex); 1123 1124 return ret; 1125 } 1126 1127 static const struct file_operations sev_fops = { 1128 .owner = THIS_MODULE, 1129 .unlocked_ioctl = sev_ioctl, 1130 }; 1131 1132 int sev_platform_status(struct sev_user_data_status *data, int *error) 1133 { 1134 return sev_do_cmd(SEV_CMD_PLATFORM_STATUS, data, error); 1135 } 1136 EXPORT_SYMBOL_GPL(sev_platform_status); 1137 1138 int sev_guest_deactivate(struct sev_data_deactivate *data, int *error) 1139 { 1140 return sev_do_cmd(SEV_CMD_DEACTIVATE, data, error); 1141 } 1142 EXPORT_SYMBOL_GPL(sev_guest_deactivate); 1143 1144 int sev_guest_activate(struct sev_data_activate *data, int *error) 1145 { 1146 return sev_do_cmd(SEV_CMD_ACTIVATE, data, error); 1147 } 1148 EXPORT_SYMBOL_GPL(sev_guest_activate); 1149 1150 int sev_guest_decommission(struct sev_data_decommission *data, int *error) 1151 { 1152 return sev_do_cmd(SEV_CMD_DECOMMISSION, data, error); 1153 } 1154 EXPORT_SYMBOL_GPL(sev_guest_decommission); 1155 1156 int sev_guest_df_flush(int *error) 1157 { 1158 return sev_do_cmd(SEV_CMD_DF_FLUSH, NULL, error); 1159 } 1160 EXPORT_SYMBOL_GPL(sev_guest_df_flush); 1161 1162 static void sev_exit(struct kref *ref) 1163 { 1164 misc_deregister(&misc_dev->misc); 1165 kfree(misc_dev); 1166 misc_dev = NULL; 1167 } 1168 1169 static int sev_misc_init(struct sev_device *sev) 1170 { 1171 struct device *dev = sev->dev; 1172 int ret; 1173 1174 /* 1175 * SEV feature support can be detected on multiple devices but the SEV 1176 * FW commands must be issued on the master. During probe, we do not 1177 * know the master hence we create /dev/sev on the first device probe. 1178 * sev_do_cmd() finds the right master device to which to issue the 1179 * command to the firmware. 1180 */ 1181 if (!misc_dev) { 1182 struct miscdevice *misc; 1183 1184 misc_dev = kzalloc(sizeof(*misc_dev), GFP_KERNEL); 1185 if (!misc_dev) 1186 return -ENOMEM; 1187 1188 misc = &misc_dev->misc; 1189 misc->minor = MISC_DYNAMIC_MINOR; 1190 misc->name = DEVICE_NAME; 1191 misc->fops = &sev_fops; 1192 1193 ret = misc_register(misc); 1194 if (ret) 1195 return ret; 1196 1197 kref_init(&misc_dev->refcount); 1198 } else { 1199 kref_get(&misc_dev->refcount); 1200 } 1201 1202 init_waitqueue_head(&sev->int_queue); 1203 sev->misc = misc_dev; 1204 dev_dbg(dev, "registered SEV device\n"); 1205 1206 return 0; 1207 } 1208 1209 int sev_dev_init(struct psp_device *psp) 1210 { 1211 struct device *dev = psp->dev; 1212 struct sev_device *sev; 1213 int ret = -ENOMEM; 1214 1215 if (!boot_cpu_has(X86_FEATURE_SEV)) { 1216 dev_info_once(dev, "SEV: memory encryption not enabled by BIOS\n"); 1217 return 0; 1218 } 1219 1220 sev = devm_kzalloc(dev, sizeof(*sev), GFP_KERNEL); 1221 if (!sev) 1222 goto e_err; 1223 1224 sev->cmd_buf = (void *)devm_get_free_pages(dev, GFP_KERNEL, 0); 1225 if (!sev->cmd_buf) 1226 goto e_sev; 1227 1228 psp->sev_data = sev; 1229 1230 sev->dev = dev; 1231 sev->psp = psp; 1232 1233 sev->io_regs = psp->io_regs; 1234 1235 sev->vdata = (struct sev_vdata *)psp->vdata->sev; 1236 if (!sev->vdata) { 1237 ret = -ENODEV; 1238 dev_err(dev, "sev: missing driver data\n"); 1239 goto e_buf; 1240 } 1241 1242 psp_set_sev_irq_handler(psp, sev_irq_handler, sev); 1243 1244 ret = sev_misc_init(sev); 1245 if (ret) 1246 goto e_irq; 1247 1248 dev_notice(dev, "sev enabled\n"); 1249 1250 return 0; 1251 1252 e_irq: 1253 psp_clear_sev_irq_handler(psp); 1254 e_buf: 1255 devm_free_pages(dev, (unsigned long)sev->cmd_buf); 1256 e_sev: 1257 devm_kfree(dev, sev); 1258 e_err: 1259 psp->sev_data = NULL; 1260 1261 dev_notice(dev, "sev initialization failed\n"); 1262 1263 return ret; 1264 } 1265 1266 static void sev_firmware_shutdown(struct sev_device *sev) 1267 { 1268 sev_platform_shutdown(NULL); 1269 1270 if (sev_es_tmr) { 1271 /* The TMR area was encrypted, flush it from the cache */ 1272 wbinvd_on_all_cpus(); 1273 1274 free_pages((unsigned long)sev_es_tmr, 1275 get_order(SEV_ES_TMR_SIZE)); 1276 sev_es_tmr = NULL; 1277 } 1278 1279 if (sev_init_ex_buffer) { 1280 free_pages((unsigned long)sev_init_ex_buffer, 1281 get_order(NV_LENGTH)); 1282 sev_init_ex_buffer = NULL; 1283 } 1284 } 1285 1286 void sev_dev_destroy(struct psp_device *psp) 1287 { 1288 struct sev_device *sev = psp->sev_data; 1289 1290 if (!sev) 1291 return; 1292 1293 sev_firmware_shutdown(sev); 1294 1295 if (sev->misc) 1296 kref_put(&misc_dev->refcount, sev_exit); 1297 1298 psp_clear_sev_irq_handler(psp); 1299 } 1300 1301 int sev_issue_cmd_external_user(struct file *filep, unsigned int cmd, 1302 void *data, int *error) 1303 { 1304 if (!filep || filep->f_op != &sev_fops) 1305 return -EBADF; 1306 1307 return sev_do_cmd(cmd, data, error); 1308 } 1309 EXPORT_SYMBOL_GPL(sev_issue_cmd_external_user); 1310 1311 void sev_pci_init(void) 1312 { 1313 struct sev_device *sev = psp_master->sev_data; 1314 int error, rc; 1315 1316 if (!sev) 1317 return; 1318 1319 psp_timeout = psp_probe_timeout; 1320 1321 if (sev_get_api_version()) 1322 goto err; 1323 1324 if (sev_update_firmware(sev->dev) == 0) 1325 sev_get_api_version(); 1326 1327 /* If an init_ex_path is provided rely on INIT_EX for PSP initialization 1328 * instead of INIT. 1329 */ 1330 if (init_ex_path) { 1331 sev_init_ex_buffer = sev_fw_alloc(NV_LENGTH); 1332 if (!sev_init_ex_buffer) { 1333 dev_err(sev->dev, 1334 "SEV: INIT_EX NV memory allocation failed\n"); 1335 goto err; 1336 } 1337 } 1338 1339 /* Obtain the TMR memory area for SEV-ES use */ 1340 sev_es_tmr = sev_fw_alloc(SEV_ES_TMR_SIZE); 1341 if (sev_es_tmr) 1342 /* Must flush the cache before giving it to the firmware */ 1343 clflush_cache_range(sev_es_tmr, SEV_ES_TMR_SIZE); 1344 else 1345 dev_warn(sev->dev, 1346 "SEV: TMR allocation failed, SEV-ES support unavailable\n"); 1347 1348 if (!psp_init_on_probe) 1349 return; 1350 1351 /* Initialize the platform */ 1352 rc = sev_platform_init(&error); 1353 if (rc) 1354 dev_err(sev->dev, "SEV: failed to INIT error %#x, rc %d\n", 1355 error, rc); 1356 1357 return; 1358 1359 err: 1360 psp_master->sev_data = NULL; 1361 } 1362 1363 void sev_pci_exit(void) 1364 { 1365 struct sev_device *sev = psp_master->sev_data; 1366 1367 if (!sev) 1368 return; 1369 1370 sev_firmware_shutdown(sev); 1371 } 1372