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/bitfield.h> 11 #include <linux/module.h> 12 #include <linux/kernel.h> 13 #include <linux/kthread.h> 14 #include <linux/sched.h> 15 #include <linux/interrupt.h> 16 #include <linux/spinlock.h> 17 #include <linux/spinlock_types.h> 18 #include <linux/types.h> 19 #include <linux/mutex.h> 20 #include <linux/delay.h> 21 #include <linux/hw_random.h> 22 #include <linux/ccp.h> 23 #include <linux/firmware.h> 24 #include <linux/panic_notifier.h> 25 #include <linux/gfp.h> 26 #include <linux/cpufeature.h> 27 #include <linux/fs.h> 28 #include <linux/fs_struct.h> 29 #include <linux/psp.h> 30 #include <linux/amd-iommu.h> 31 #include <linux/crash_dump.h> 32 33 #include <asm/smp.h> 34 #include <asm/cacheflush.h> 35 #include <asm/e820/types.h> 36 #include <asm/sev.h> 37 #include <asm/msr.h> 38 39 #include "psp-dev.h" 40 #include "sev-dev.h" 41 42 #define DEVICE_NAME "sev" 43 #define SEV_FW_FILE "amd/sev.fw" 44 #define SEV_FW_NAME_SIZE 64 45 46 /* Minimum firmware version required for the SEV-SNP support */ 47 #define SNP_MIN_API_MAJOR 1 48 #define SNP_MIN_API_MINOR 51 49 50 /* 51 * Maximum number of firmware-writable buffers that might be specified 52 * in the parameters of a legacy SEV command buffer. 53 */ 54 #define CMD_BUF_FW_WRITABLE_MAX 2 55 56 /* Leave room in the descriptor array for an end-of-list indicator. */ 57 #define CMD_BUF_DESC_MAX (CMD_BUF_FW_WRITABLE_MAX + 1) 58 59 static DEFINE_MUTEX(sev_cmd_mutex); 60 static struct sev_misc_dev *misc_dev; 61 62 static int psp_cmd_timeout = 100; 63 module_param(psp_cmd_timeout, int, 0644); 64 MODULE_PARM_DESC(psp_cmd_timeout, " default timeout value, in seconds, for PSP commands"); 65 66 static int psp_probe_timeout = 5; 67 module_param(psp_probe_timeout, int, 0644); 68 MODULE_PARM_DESC(psp_probe_timeout, " default timeout value, in seconds, during PSP device probe"); 69 70 static char *init_ex_path; 71 module_param(init_ex_path, charp, 0444); 72 MODULE_PARM_DESC(init_ex_path, " Path for INIT_EX data; if set try INIT_EX"); 73 74 static bool psp_init_on_probe = true; 75 module_param(psp_init_on_probe, bool, 0444); 76 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"); 77 78 #if IS_ENABLED(CONFIG_PCI_TSM) 79 static bool sev_tio_enabled = true; 80 module_param_named(tio, sev_tio_enabled, bool, 0444); 81 MODULE_PARM_DESC(tio, "Enables TIO in SNP_INIT_EX"); 82 #else 83 static const bool sev_tio_enabled = false; 84 #endif 85 86 MODULE_FIRMWARE("amd/amd_sev_fam17h_model0xh.sbin"); /* 1st gen EPYC */ 87 MODULE_FIRMWARE("amd/amd_sev_fam17h_model3xh.sbin"); /* 2nd gen EPYC */ 88 MODULE_FIRMWARE("amd/amd_sev_fam19h_model0xh.sbin"); /* 3rd gen EPYC */ 89 MODULE_FIRMWARE("amd/amd_sev_fam19h_model1xh.sbin"); /* 4th gen EPYC */ 90 91 static bool psp_dead; 92 static int psp_timeout; 93 94 enum snp_hv_fixed_pages_state { 95 ALLOCATED, 96 HV_FIXED, 97 }; 98 99 struct snp_hv_fixed_pages_entry { 100 struct list_head list; 101 struct page *page; 102 unsigned int order; 103 bool free; 104 enum snp_hv_fixed_pages_state page_state; 105 }; 106 107 static LIST_HEAD(snp_hv_fixed_pages); 108 109 /* Trusted Memory Region (TMR): 110 * The TMR is a 1MB area that must be 1MB aligned. Use the page allocator 111 * to allocate the memory, which will return aligned memory for the specified 112 * allocation order. 113 * 114 * When SEV-SNP is enabled the TMR needs to be 2MB aligned and 2MB sized. 115 */ 116 #define SEV_TMR_SIZE (1024 * 1024) 117 #define SNP_TMR_SIZE (2 * 1024 * 1024) 118 119 static void *sev_es_tmr; 120 static size_t sev_es_tmr_size = SEV_TMR_SIZE; 121 122 /* INIT_EX NV Storage: 123 * The NV Storage is a 32Kb area and must be 4Kb page aligned. Use the page 124 * allocator to allocate the memory, which will return aligned memory for the 125 * specified allocation order. 126 */ 127 #define NV_LENGTH (32 * 1024) 128 static void *sev_init_ex_buffer; 129 130 static void __sev_firmware_shutdown(struct sev_device *sev, bool panic); 131 132 static int snp_shutdown_on_panic(struct notifier_block *nb, 133 unsigned long reason, void *arg); 134 135 static struct notifier_block snp_panic_notifier = { 136 .notifier_call = snp_shutdown_on_panic, 137 }; 138 139 static inline bool sev_version_greater_or_equal(u8 maj, u8 min) 140 { 141 struct sev_device *sev = psp_master->sev_data; 142 143 if (sev->api_major > maj) 144 return true; 145 146 if (sev->api_major == maj && sev->api_minor >= min) 147 return true; 148 149 return false; 150 } 151 152 static void sev_irq_handler(int irq, void *data, unsigned int status) 153 { 154 struct sev_device *sev = data; 155 int reg; 156 157 /* Check if it is command completion: */ 158 if (!(status & SEV_CMD_COMPLETE)) 159 return; 160 161 /* Check if it is SEV command completion: */ 162 reg = ioread32(sev->io_regs + sev->vdata->cmdresp_reg); 163 if (FIELD_GET(PSP_CMDRESP_RESP, reg)) { 164 sev->int_rcvd = 1; 165 wake_up(&sev->int_queue); 166 } 167 } 168 169 static int sev_wait_cmd_ioc(struct sev_device *sev, 170 unsigned int *reg, unsigned int timeout) 171 { 172 int ret; 173 174 /* 175 * If invoked during panic handling, local interrupts are disabled, 176 * so the PSP command completion interrupt can't be used. Poll for 177 * PSP command completion instead. 178 */ 179 if (irqs_disabled()) { 180 unsigned long timeout_usecs = (timeout * USEC_PER_SEC) / 10; 181 182 /* Poll for SEV command completion: */ 183 while (timeout_usecs--) { 184 *reg = ioread32(sev->io_regs + sev->vdata->cmdresp_reg); 185 if (*reg & PSP_CMDRESP_RESP) 186 return 0; 187 188 udelay(10); 189 } 190 return -ETIMEDOUT; 191 } 192 193 ret = wait_event_timeout(sev->int_queue, 194 sev->int_rcvd, timeout * HZ); 195 if (!ret) 196 return -ETIMEDOUT; 197 198 *reg = ioread32(sev->io_regs + sev->vdata->cmdresp_reg); 199 200 return 0; 201 } 202 203 static int sev_cmd_buffer_len(int cmd) 204 { 205 switch (cmd) { 206 case SEV_CMD_INIT: return sizeof(struct sev_data_init); 207 case SEV_CMD_INIT_EX: return sizeof(struct sev_data_init_ex); 208 case SEV_CMD_SNP_SHUTDOWN_EX: return sizeof(struct sev_data_snp_shutdown_ex); 209 case SEV_CMD_SNP_INIT_EX: return sizeof(struct sev_data_snp_init_ex); 210 case SEV_CMD_PLATFORM_STATUS: return sizeof(struct sev_user_data_status); 211 case SEV_CMD_PEK_CSR: return sizeof(struct sev_data_pek_csr); 212 case SEV_CMD_PEK_CERT_IMPORT: return sizeof(struct sev_data_pek_cert_import); 213 case SEV_CMD_PDH_CERT_EXPORT: return sizeof(struct sev_data_pdh_cert_export); 214 case SEV_CMD_LAUNCH_START: return sizeof(struct sev_data_launch_start); 215 case SEV_CMD_LAUNCH_UPDATE_DATA: return sizeof(struct sev_data_launch_update_data); 216 case SEV_CMD_LAUNCH_UPDATE_VMSA: return sizeof(struct sev_data_launch_update_vmsa); 217 case SEV_CMD_LAUNCH_FINISH: return sizeof(struct sev_data_launch_finish); 218 case SEV_CMD_LAUNCH_MEASURE: return sizeof(struct sev_data_launch_measure); 219 case SEV_CMD_ACTIVATE: return sizeof(struct sev_data_activate); 220 case SEV_CMD_DEACTIVATE: return sizeof(struct sev_data_deactivate); 221 case SEV_CMD_DECOMMISSION: return sizeof(struct sev_data_decommission); 222 case SEV_CMD_GUEST_STATUS: return sizeof(struct sev_data_guest_status); 223 case SEV_CMD_DBG_DECRYPT: return sizeof(struct sev_data_dbg); 224 case SEV_CMD_DBG_ENCRYPT: return sizeof(struct sev_data_dbg); 225 case SEV_CMD_SEND_START: return sizeof(struct sev_data_send_start); 226 case SEV_CMD_SEND_UPDATE_DATA: return sizeof(struct sev_data_send_update_data); 227 case SEV_CMD_SEND_UPDATE_VMSA: return sizeof(struct sev_data_send_update_vmsa); 228 case SEV_CMD_SEND_FINISH: return sizeof(struct sev_data_send_finish); 229 case SEV_CMD_RECEIVE_START: return sizeof(struct sev_data_receive_start); 230 case SEV_CMD_RECEIVE_FINISH: return sizeof(struct sev_data_receive_finish); 231 case SEV_CMD_RECEIVE_UPDATE_DATA: return sizeof(struct sev_data_receive_update_data); 232 case SEV_CMD_RECEIVE_UPDATE_VMSA: return sizeof(struct sev_data_receive_update_vmsa); 233 case SEV_CMD_LAUNCH_UPDATE_SECRET: return sizeof(struct sev_data_launch_secret); 234 case SEV_CMD_DOWNLOAD_FIRMWARE: return sizeof(struct sev_data_download_firmware); 235 case SEV_CMD_GET_ID: return sizeof(struct sev_data_get_id); 236 case SEV_CMD_ATTESTATION_REPORT: return sizeof(struct sev_data_attestation_report); 237 case SEV_CMD_SEND_CANCEL: return sizeof(struct sev_data_send_cancel); 238 case SEV_CMD_SNP_GCTX_CREATE: return sizeof(struct sev_data_snp_addr); 239 case SEV_CMD_SNP_LAUNCH_START: return sizeof(struct sev_data_snp_launch_start); 240 case SEV_CMD_SNP_LAUNCH_UPDATE: return sizeof(struct sev_data_snp_launch_update); 241 case SEV_CMD_SNP_ACTIVATE: return sizeof(struct sev_data_snp_activate); 242 case SEV_CMD_SNP_DECOMMISSION: return sizeof(struct sev_data_snp_addr); 243 case SEV_CMD_SNP_PAGE_RECLAIM: return sizeof(struct sev_data_snp_page_reclaim); 244 case SEV_CMD_SNP_GUEST_STATUS: return sizeof(struct sev_data_snp_guest_status); 245 case SEV_CMD_SNP_LAUNCH_FINISH: return sizeof(struct sev_data_snp_launch_finish); 246 case SEV_CMD_SNP_DBG_DECRYPT: return sizeof(struct sev_data_snp_dbg); 247 case SEV_CMD_SNP_DBG_ENCRYPT: return sizeof(struct sev_data_snp_dbg); 248 case SEV_CMD_SNP_PAGE_UNSMASH: return sizeof(struct sev_data_snp_page_unsmash); 249 case SEV_CMD_SNP_PLATFORM_STATUS: return sizeof(struct sev_data_snp_addr); 250 case SEV_CMD_SNP_GUEST_REQUEST: return sizeof(struct sev_data_snp_guest_request); 251 case SEV_CMD_SNP_CONFIG: return sizeof(struct sev_user_data_snp_config); 252 case SEV_CMD_SNP_COMMIT: return sizeof(struct sev_data_snp_commit); 253 case SEV_CMD_SNP_FEATURE_INFO: return sizeof(struct sev_data_snp_feature_info); 254 case SEV_CMD_SNP_VLEK_LOAD: return sizeof(struct sev_user_data_snp_vlek_load); 255 default: return sev_tio_cmd_buffer_len(cmd); 256 } 257 258 return 0; 259 } 260 261 static struct file *open_file_as_root(const char *filename, int flags, umode_t mode) 262 { 263 CLASS(prepare_creds, cred)(); 264 if (!cred) 265 return ERR_PTR(-ENOMEM); 266 267 cred->fsuid = GLOBAL_ROOT_UID; 268 269 scoped_with_init_fs() { 270 scoped_with_creds(cred) 271 return filp_open(filename, flags, mode); 272 } 273 } 274 275 static int sev_read_init_ex_file(void) 276 { 277 struct sev_device *sev = psp_master->sev_data; 278 struct file *fp; 279 ssize_t nread; 280 281 lockdep_assert_held(&sev_cmd_mutex); 282 283 if (!sev_init_ex_buffer) 284 return -EOPNOTSUPP; 285 286 fp = open_file_as_root(init_ex_path, O_RDONLY, 0); 287 if (IS_ERR(fp)) { 288 int ret = PTR_ERR(fp); 289 290 if (ret == -ENOENT) { 291 dev_info(sev->dev, 292 "SEV: %s does not exist and will be created later.\n", 293 init_ex_path); 294 ret = 0; 295 } else { 296 dev_err(sev->dev, 297 "SEV: could not open %s for read, error %d\n", 298 init_ex_path, ret); 299 } 300 return ret; 301 } 302 303 nread = kernel_read(fp, sev_init_ex_buffer, NV_LENGTH, NULL); 304 if (nread != NV_LENGTH) { 305 dev_info(sev->dev, 306 "SEV: could not read %u bytes to non volatile memory area, ret %ld\n", 307 NV_LENGTH, nread); 308 } 309 310 dev_dbg(sev->dev, "SEV: read %ld bytes from NV file\n", nread); 311 filp_close(fp, NULL); 312 313 return 0; 314 } 315 316 static int sev_write_init_ex_file(void) 317 { 318 struct sev_device *sev = psp_master->sev_data; 319 struct file *fp; 320 loff_t offset = 0; 321 ssize_t nwrite; 322 323 lockdep_assert_held(&sev_cmd_mutex); 324 325 if (!sev_init_ex_buffer) 326 return 0; 327 328 fp = open_file_as_root(init_ex_path, O_CREAT | O_WRONLY, 0600); 329 if (IS_ERR(fp)) { 330 int ret = PTR_ERR(fp); 331 332 dev_err(sev->dev, 333 "SEV: could not open file for write, error %d\n", 334 ret); 335 return ret; 336 } 337 338 nwrite = kernel_write(fp, sev_init_ex_buffer, NV_LENGTH, &offset); 339 vfs_fsync(fp, 0); 340 filp_close(fp, NULL); 341 342 if (nwrite != NV_LENGTH) { 343 dev_err(sev->dev, 344 "SEV: failed to write %u bytes to non volatile memory area, ret %ld\n", 345 NV_LENGTH, nwrite); 346 return -EIO; 347 } 348 349 dev_dbg(sev->dev, "SEV: write successful to NV file\n"); 350 351 return 0; 352 } 353 354 static int sev_write_init_ex_file_if_required(int cmd_id) 355 { 356 lockdep_assert_held(&sev_cmd_mutex); 357 358 if (!sev_init_ex_buffer) 359 return 0; 360 361 /* 362 * Only a few platform commands modify the SPI/NV area, but none of the 363 * non-platform commands do. Only INIT(_EX), PLATFORM_RESET, PEK_GEN, 364 * PEK_CERT_IMPORT, and PDH_GEN do. 365 */ 366 switch (cmd_id) { 367 case SEV_CMD_FACTORY_RESET: 368 case SEV_CMD_INIT_EX: 369 case SEV_CMD_PDH_GEN: 370 case SEV_CMD_PEK_CERT_IMPORT: 371 case SEV_CMD_PEK_GEN: 372 break; 373 default: 374 return 0; 375 } 376 377 return sev_write_init_ex_file(); 378 } 379 380 int snp_reclaim_pages(unsigned long paddr, unsigned int npages, bool locked) 381 { 382 int ret, err, i; 383 384 paddr = __sme_clr(ALIGN_DOWN(paddr, PAGE_SIZE)); 385 386 for (i = 0; i < npages; i++, paddr += PAGE_SIZE) { 387 struct sev_data_snp_page_reclaim data = {0}; 388 389 data.paddr = paddr; 390 391 if (locked) 392 ret = __sev_do_cmd_locked(SEV_CMD_SNP_PAGE_RECLAIM, &data, &err); 393 else 394 ret = sev_do_cmd(SEV_CMD_SNP_PAGE_RECLAIM, &data, &err); 395 396 if (ret) 397 goto cleanup; 398 399 ret = rmp_make_shared(__phys_to_pfn(paddr), PG_LEVEL_4K); 400 if (ret) 401 goto cleanup; 402 } 403 404 return 0; 405 406 cleanup: 407 /* 408 * If there was a failure reclaiming the page then it is no longer safe 409 * to release it back to the system; leak it instead. 410 */ 411 snp_leak_pages(__phys_to_pfn(paddr), npages - i); 412 return ret; 413 } 414 EXPORT_SYMBOL_GPL(snp_reclaim_pages); 415 416 static int rmp_mark_pages_firmware(unsigned long paddr, unsigned int npages, bool locked) 417 { 418 unsigned long pfn = __sme_clr(paddr) >> PAGE_SHIFT; 419 int rc, i; 420 421 for (i = 0; i < npages; i++, pfn++) { 422 rc = rmp_make_private(pfn, 0, PG_LEVEL_4K, 0, true); 423 if (rc) 424 goto cleanup; 425 } 426 427 return 0; 428 429 cleanup: 430 /* 431 * Try unrolling the firmware state changes by 432 * reclaiming the pages which were already changed to the 433 * firmware state. 434 */ 435 snp_reclaim_pages(paddr, i, locked); 436 437 return rc; 438 } 439 440 static struct page *__snp_alloc_firmware_pages(gfp_t gfp_mask, int order, bool locked) 441 { 442 unsigned long npages = 1ul << order, paddr; 443 struct sev_device *sev; 444 struct page *page; 445 446 if (!psp_master || !psp_master->sev_data) 447 return NULL; 448 449 page = alloc_pages(gfp_mask, order); 450 if (!page) 451 return NULL; 452 453 /* If SEV-SNP is initialized then add the page in RMP table. */ 454 sev = psp_master->sev_data; 455 if (!sev->snp_initialized) 456 return page; 457 458 paddr = __pa((unsigned long)page_address(page)); 459 if (rmp_mark_pages_firmware(paddr, npages, locked)) 460 return NULL; 461 462 return page; 463 } 464 465 void *snp_alloc_firmware_page(gfp_t gfp_mask) 466 { 467 struct page *page; 468 469 page = __snp_alloc_firmware_pages(gfp_mask, 0, false); 470 471 return page ? page_address(page) : NULL; 472 } 473 EXPORT_SYMBOL_GPL(snp_alloc_firmware_page); 474 475 static void __snp_free_firmware_pages(struct page *page, int order, bool locked) 476 { 477 struct sev_device *sev = psp_master->sev_data; 478 unsigned long paddr, npages = 1ul << order; 479 480 if (!page) 481 return; 482 483 paddr = __pa((unsigned long)page_address(page)); 484 if (sev->snp_initialized && 485 snp_reclaim_pages(paddr, npages, locked)) 486 return; 487 488 __free_pages(page, order); 489 } 490 491 void snp_free_firmware_page(void *addr) 492 { 493 if (!addr) 494 return; 495 496 __snp_free_firmware_pages(virt_to_page(addr), 0, false); 497 } 498 EXPORT_SYMBOL_GPL(snp_free_firmware_page); 499 500 static void *sev_fw_alloc(unsigned long len) 501 { 502 struct page *page; 503 504 page = __snp_alloc_firmware_pages(GFP_KERNEL, get_order(len), true); 505 if (!page) 506 return NULL; 507 508 return page_address(page); 509 } 510 511 /** 512 * struct cmd_buf_desc - descriptors for managing legacy SEV command address 513 * parameters corresponding to buffers that may be written to by firmware. 514 * 515 * @paddr_ptr: pointer to the address parameter in the command buffer which may 516 * need to be saved/restored depending on whether a bounce buffer 517 * is used. In the case of a bounce buffer, the command buffer 518 * needs to be updated with the address of the new bounce buffer 519 * snp_map_cmd_buf_desc() has allocated specifically for it. Must 520 * be NULL if this descriptor is only an end-of-list indicator. 521 * 522 * @paddr_orig: storage for the original address parameter, which can be used to 523 * restore the original value in @paddr_ptr in cases where it is 524 * replaced with the address of a bounce buffer. 525 * 526 * @len: length of buffer located at the address originally stored at @paddr_ptr 527 * 528 * @guest_owned: true if the address corresponds to guest-owned pages, in which 529 * case bounce buffers are not needed. 530 */ 531 struct cmd_buf_desc { 532 u64 *paddr_ptr; 533 u64 paddr_orig; 534 u32 len; 535 bool guest_owned; 536 }; 537 538 /* 539 * If a legacy SEV command parameter is a memory address, those pages in 540 * turn need to be transitioned to/from firmware-owned before/after 541 * executing the firmware command. 542 * 543 * Additionally, in cases where those pages are not guest-owned, a bounce 544 * buffer is needed in place of the original memory address parameter. 545 * 546 * A set of descriptors are used to keep track of this handling, and 547 * initialized here based on the specific commands being executed. 548 */ 549 static void snp_populate_cmd_buf_desc_list(int cmd, void *cmd_buf, 550 struct cmd_buf_desc *desc_list) 551 { 552 switch (cmd) { 553 case SEV_CMD_PDH_CERT_EXPORT: { 554 struct sev_data_pdh_cert_export *data = cmd_buf; 555 556 desc_list[0].paddr_ptr = &data->pdh_cert_address; 557 desc_list[0].len = data->pdh_cert_len; 558 desc_list[1].paddr_ptr = &data->cert_chain_address; 559 desc_list[1].len = data->cert_chain_len; 560 break; 561 } 562 case SEV_CMD_GET_ID: { 563 struct sev_data_get_id *data = cmd_buf; 564 565 desc_list[0].paddr_ptr = &data->address; 566 desc_list[0].len = data->len; 567 break; 568 } 569 case SEV_CMD_PEK_CSR: { 570 struct sev_data_pek_csr *data = cmd_buf; 571 572 desc_list[0].paddr_ptr = &data->address; 573 desc_list[0].len = data->len; 574 break; 575 } 576 case SEV_CMD_LAUNCH_UPDATE_DATA: { 577 struct sev_data_launch_update_data *data = cmd_buf; 578 579 desc_list[0].paddr_ptr = &data->address; 580 desc_list[0].len = data->len; 581 desc_list[0].guest_owned = true; 582 break; 583 } 584 case SEV_CMD_LAUNCH_UPDATE_VMSA: { 585 struct sev_data_launch_update_vmsa *data = cmd_buf; 586 587 desc_list[0].paddr_ptr = &data->address; 588 desc_list[0].len = data->len; 589 desc_list[0].guest_owned = true; 590 break; 591 } 592 case SEV_CMD_LAUNCH_MEASURE: { 593 struct sev_data_launch_measure *data = cmd_buf; 594 595 desc_list[0].paddr_ptr = &data->address; 596 desc_list[0].len = data->len; 597 break; 598 } 599 case SEV_CMD_LAUNCH_UPDATE_SECRET: { 600 struct sev_data_launch_secret *data = cmd_buf; 601 602 desc_list[0].paddr_ptr = &data->guest_address; 603 desc_list[0].len = data->guest_len; 604 desc_list[0].guest_owned = true; 605 break; 606 } 607 case SEV_CMD_DBG_DECRYPT: { 608 struct sev_data_dbg *data = cmd_buf; 609 610 desc_list[0].paddr_ptr = &data->dst_addr; 611 desc_list[0].len = data->len; 612 desc_list[0].guest_owned = true; 613 break; 614 } 615 case SEV_CMD_DBG_ENCRYPT: { 616 struct sev_data_dbg *data = cmd_buf; 617 618 desc_list[0].paddr_ptr = &data->dst_addr; 619 desc_list[0].len = data->len; 620 desc_list[0].guest_owned = true; 621 break; 622 } 623 case SEV_CMD_ATTESTATION_REPORT: { 624 struct sev_data_attestation_report *data = cmd_buf; 625 626 desc_list[0].paddr_ptr = &data->address; 627 desc_list[0].len = data->len; 628 break; 629 } 630 case SEV_CMD_SEND_START: { 631 struct sev_data_send_start *data = cmd_buf; 632 633 desc_list[0].paddr_ptr = &data->session_address; 634 desc_list[0].len = data->session_len; 635 break; 636 } 637 case SEV_CMD_SEND_UPDATE_DATA: { 638 struct sev_data_send_update_data *data = cmd_buf; 639 640 desc_list[0].paddr_ptr = &data->hdr_address; 641 desc_list[0].len = data->hdr_len; 642 desc_list[1].paddr_ptr = &data->trans_address; 643 desc_list[1].len = data->trans_len; 644 break; 645 } 646 case SEV_CMD_SEND_UPDATE_VMSA: { 647 struct sev_data_send_update_vmsa *data = cmd_buf; 648 649 desc_list[0].paddr_ptr = &data->hdr_address; 650 desc_list[0].len = data->hdr_len; 651 desc_list[1].paddr_ptr = &data->trans_address; 652 desc_list[1].len = data->trans_len; 653 break; 654 } 655 case SEV_CMD_RECEIVE_UPDATE_DATA: { 656 struct sev_data_receive_update_data *data = cmd_buf; 657 658 desc_list[0].paddr_ptr = &data->guest_address; 659 desc_list[0].len = data->guest_len; 660 desc_list[0].guest_owned = true; 661 break; 662 } 663 case SEV_CMD_RECEIVE_UPDATE_VMSA: { 664 struct sev_data_receive_update_vmsa *data = cmd_buf; 665 666 desc_list[0].paddr_ptr = &data->guest_address; 667 desc_list[0].len = data->guest_len; 668 desc_list[0].guest_owned = true; 669 break; 670 } 671 default: 672 break; 673 } 674 } 675 676 static int snp_map_cmd_buf_desc(struct cmd_buf_desc *desc) 677 { 678 unsigned int npages; 679 680 if (!desc->len) 681 return 0; 682 683 /* Allocate a bounce buffer if this isn't a guest owned page. */ 684 if (!desc->guest_owned) { 685 struct page *page; 686 687 page = alloc_pages(GFP_KERNEL_ACCOUNT, get_order(desc->len)); 688 if (!page) { 689 pr_warn("Failed to allocate bounce buffer for SEV legacy command.\n"); 690 return -ENOMEM; 691 } 692 693 desc->paddr_orig = *desc->paddr_ptr; 694 *desc->paddr_ptr = __psp_pa(page_to_virt(page)); 695 } 696 697 npages = PAGE_ALIGN(desc->len) >> PAGE_SHIFT; 698 699 /* Transition the buffer to firmware-owned. */ 700 if (rmp_mark_pages_firmware(*desc->paddr_ptr, npages, true)) { 701 pr_warn("Error moving pages to firmware-owned state for SEV legacy command.\n"); 702 return -EFAULT; 703 } 704 705 return 0; 706 } 707 708 static int snp_unmap_cmd_buf_desc(struct cmd_buf_desc *desc) 709 { 710 unsigned int npages; 711 712 if (!desc->len) 713 return 0; 714 715 npages = PAGE_ALIGN(desc->len) >> PAGE_SHIFT; 716 717 /* Transition the buffers back to hypervisor-owned. */ 718 if (snp_reclaim_pages(*desc->paddr_ptr, npages, true)) { 719 pr_warn("Failed to reclaim firmware-owned pages while issuing SEV legacy command.\n"); 720 return -EFAULT; 721 } 722 723 /* Copy data from bounce buffer and then free it. */ 724 if (!desc->guest_owned) { 725 void *bounce_buf = __va(__sme_clr(*desc->paddr_ptr)); 726 void *dst_buf = __va(__sme_clr(desc->paddr_orig)); 727 728 memcpy(dst_buf, bounce_buf, desc->len); 729 __free_pages(virt_to_page(bounce_buf), get_order(desc->len)); 730 731 /* Restore the original address in the command buffer. */ 732 *desc->paddr_ptr = desc->paddr_orig; 733 } 734 735 return 0; 736 } 737 738 static int snp_map_cmd_buf_desc_list(int cmd, void *cmd_buf, struct cmd_buf_desc *desc_list) 739 { 740 int i; 741 742 snp_populate_cmd_buf_desc_list(cmd, cmd_buf, desc_list); 743 744 for (i = 0; i < CMD_BUF_DESC_MAX; i++) { 745 struct cmd_buf_desc *desc = &desc_list[i]; 746 747 if (!desc->paddr_ptr) 748 break; 749 750 if (snp_map_cmd_buf_desc(desc)) 751 goto err_unmap; 752 } 753 754 return 0; 755 756 err_unmap: 757 for (i--; i >= 0; i--) 758 snp_unmap_cmd_buf_desc(&desc_list[i]); 759 760 return -EFAULT; 761 } 762 763 static int snp_unmap_cmd_buf_desc_list(struct cmd_buf_desc *desc_list) 764 { 765 int i, ret = 0; 766 767 for (i = 0; i < CMD_BUF_DESC_MAX; i++) { 768 struct cmd_buf_desc *desc = &desc_list[i]; 769 770 if (!desc->paddr_ptr) 771 break; 772 773 if (snp_unmap_cmd_buf_desc(&desc_list[i])) 774 ret = -EFAULT; 775 } 776 777 return ret; 778 } 779 780 static bool sev_cmd_buf_writable(int cmd) 781 { 782 switch (cmd) { 783 case SEV_CMD_PLATFORM_STATUS: 784 case SEV_CMD_GUEST_STATUS: 785 case SEV_CMD_LAUNCH_START: 786 case SEV_CMD_RECEIVE_START: 787 case SEV_CMD_LAUNCH_MEASURE: 788 case SEV_CMD_SEND_START: 789 case SEV_CMD_SEND_UPDATE_DATA: 790 case SEV_CMD_SEND_UPDATE_VMSA: 791 case SEV_CMD_PEK_CSR: 792 case SEV_CMD_PDH_CERT_EXPORT: 793 case SEV_CMD_GET_ID: 794 case SEV_CMD_ATTESTATION_REPORT: 795 return true; 796 default: 797 return false; 798 } 799 } 800 801 /* After SNP is INIT'ed, the behavior of legacy SEV commands is changed. */ 802 static bool snp_legacy_handling_needed(int cmd) 803 { 804 struct sev_device *sev = psp_master->sev_data; 805 806 return cmd < SEV_CMD_SNP_INIT && sev->snp_initialized; 807 } 808 809 static int snp_prep_cmd_buf(int cmd, void *cmd_buf, struct cmd_buf_desc *desc_list) 810 { 811 if (!snp_legacy_handling_needed(cmd)) 812 return 0; 813 814 if (snp_map_cmd_buf_desc_list(cmd, cmd_buf, desc_list)) 815 return -EFAULT; 816 817 /* 818 * Before command execution, the command buffer needs to be put into 819 * the firmware-owned state. 820 */ 821 if (sev_cmd_buf_writable(cmd)) { 822 if (rmp_mark_pages_firmware(__pa(cmd_buf), 1, true)) 823 return -EFAULT; 824 } 825 826 return 0; 827 } 828 829 static int snp_reclaim_cmd_buf(int cmd, void *cmd_buf) 830 { 831 if (!snp_legacy_handling_needed(cmd)) 832 return 0; 833 834 /* 835 * After command completion, the command buffer needs to be put back 836 * into the hypervisor-owned state. 837 */ 838 if (sev_cmd_buf_writable(cmd)) 839 if (snp_reclaim_pages(__pa(cmd_buf), 1, true)) 840 return -EFAULT; 841 842 return 0; 843 } 844 845 int __sev_do_cmd_locked(int cmd, void *data, int *psp_ret) 846 { 847 struct cmd_buf_desc desc_list[CMD_BUF_DESC_MAX] = {0}; 848 struct psp_device *psp = psp_master; 849 struct sev_device *sev; 850 unsigned int cmdbuff_hi, cmdbuff_lo; 851 unsigned int phys_lsb, phys_msb; 852 unsigned int reg; 853 void *cmd_buf; 854 int buf_len; 855 int ret = 0; 856 857 if (!psp || !psp->sev_data) 858 return -ENODEV; 859 860 if (psp_dead) 861 return -EBUSY; 862 863 sev = psp->sev_data; 864 865 buf_len = sev_cmd_buffer_len(cmd); 866 if (WARN_ON_ONCE(!data != !buf_len)) 867 return -EINVAL; 868 869 /* 870 * Copy the incoming data to driver's scratch buffer as __pa() will not 871 * work for some memory, e.g. vmalloc'd addresses, and @data may not be 872 * physically contiguous. 873 */ 874 if (data) { 875 /* 876 * Commands are generally issued one at a time and require the 877 * sev_cmd_mutex, but there could be recursive firmware requests 878 * due to SEV_CMD_SNP_PAGE_RECLAIM needing to be issued while 879 * preparing buffers for another command. This is the only known 880 * case of nesting in the current code, so exactly one 881 * additional command buffer is available for that purpose. 882 */ 883 if (!sev->cmd_buf_active) { 884 cmd_buf = sev->cmd_buf; 885 sev->cmd_buf_active = true; 886 } else if (!sev->cmd_buf_backup_active) { 887 cmd_buf = sev->cmd_buf_backup; 888 sev->cmd_buf_backup_active = true; 889 } else { 890 dev_err(sev->dev, 891 "SEV: too many firmware commands in progress, no command buffers available.\n"); 892 return -EBUSY; 893 } 894 895 memcpy(cmd_buf, data, buf_len); 896 897 /* 898 * The behavior of the SEV-legacy commands is altered when the 899 * SNP firmware is in the INIT state. 900 */ 901 ret = snp_prep_cmd_buf(cmd, cmd_buf, desc_list); 902 if (ret) { 903 dev_err(sev->dev, 904 "SEV: failed to prepare buffer for legacy command 0x%x. Error: %d\n", 905 cmd, ret); 906 return ret; 907 } 908 } else { 909 cmd_buf = sev->cmd_buf; 910 } 911 912 /* Get the physical address of the command buffer */ 913 phys_lsb = data ? lower_32_bits(__psp_pa(cmd_buf)) : 0; 914 phys_msb = data ? upper_32_bits(__psp_pa(cmd_buf)) : 0; 915 916 dev_dbg(sev->dev, "sev command id %#x buffer 0x%08x%08x timeout %us\n", 917 cmd, phys_msb, phys_lsb, psp_timeout); 918 919 print_hex_dump_debug("(in): ", DUMP_PREFIX_OFFSET, 16, 2, data, 920 buf_len, false); 921 922 iowrite32(phys_lsb, sev->io_regs + sev->vdata->cmdbuff_addr_lo_reg); 923 iowrite32(phys_msb, sev->io_regs + sev->vdata->cmdbuff_addr_hi_reg); 924 925 sev->int_rcvd = 0; 926 927 reg = FIELD_PREP(SEV_CMDRESP_CMD, cmd); 928 929 /* 930 * If invoked during panic handling, local interrupts are disabled so 931 * the PSP command completion interrupt can't be used. 932 * sev_wait_cmd_ioc() already checks for interrupts disabled and 933 * polls for PSP command completion. Ensure we do not request an 934 * interrupt from the PSP if irqs disabled. 935 */ 936 if (!irqs_disabled()) 937 reg |= SEV_CMDRESP_IOC; 938 939 iowrite32(reg, sev->io_regs + sev->vdata->cmdresp_reg); 940 941 /* wait for command completion */ 942 ret = sev_wait_cmd_ioc(sev, ®, psp_timeout); 943 if (ret) { 944 if (psp_ret) 945 *psp_ret = 0; 946 947 dev_err(sev->dev, "sev command %#x timed out, disabling PSP\n", cmd); 948 psp_dead = true; 949 950 return ret; 951 } 952 953 psp_timeout = psp_cmd_timeout; 954 955 if (psp_ret) 956 *psp_ret = FIELD_GET(PSP_CMDRESP_STS, reg); 957 958 if (FIELD_GET(PSP_CMDRESP_STS, reg)) { 959 dev_dbg(sev->dev, "sev command %#x failed (%#010lx)\n", 960 cmd, FIELD_GET(PSP_CMDRESP_STS, reg)); 961 962 /* 963 * PSP firmware may report additional error information in the 964 * command buffer registers on error. Print contents of command 965 * buffer registers if they changed. 966 */ 967 cmdbuff_hi = ioread32(sev->io_regs + sev->vdata->cmdbuff_addr_hi_reg); 968 cmdbuff_lo = ioread32(sev->io_regs + sev->vdata->cmdbuff_addr_lo_reg); 969 if (cmdbuff_hi != phys_msb || cmdbuff_lo != phys_lsb) { 970 dev_dbg(sev->dev, "Additional error information reported in cmdbuff:"); 971 dev_dbg(sev->dev, " cmdbuff hi: %#010x\n", cmdbuff_hi); 972 dev_dbg(sev->dev, " cmdbuff lo: %#010x\n", cmdbuff_lo); 973 } 974 ret = -EIO; 975 } else { 976 ret = sev_write_init_ex_file_if_required(cmd); 977 } 978 979 /* 980 * Copy potential output from the PSP back to data. Do this even on 981 * failure in case the caller wants to glean something from the error. 982 */ 983 if (data) { 984 int ret_reclaim; 985 /* 986 * Restore the page state after the command completes. 987 */ 988 ret_reclaim = snp_reclaim_cmd_buf(cmd, cmd_buf); 989 if (ret_reclaim) { 990 dev_err(sev->dev, 991 "SEV: failed to reclaim buffer for legacy command %#x. Error: %d\n", 992 cmd, ret_reclaim); 993 return ret_reclaim; 994 } 995 996 memcpy(data, cmd_buf, buf_len); 997 998 if (sev->cmd_buf_backup_active) 999 sev->cmd_buf_backup_active = false; 1000 else 1001 sev->cmd_buf_active = false; 1002 1003 if (snp_unmap_cmd_buf_desc_list(desc_list)) 1004 return -EFAULT; 1005 } 1006 1007 print_hex_dump_debug("(out): ", DUMP_PREFIX_OFFSET, 16, 2, data, 1008 buf_len, false); 1009 1010 return ret; 1011 } 1012 1013 int sev_do_cmd(int cmd, void *data, int *psp_ret) 1014 { 1015 int rc; 1016 1017 mutex_lock(&sev_cmd_mutex); 1018 rc = __sev_do_cmd_locked(cmd, data, psp_ret); 1019 mutex_unlock(&sev_cmd_mutex); 1020 1021 return rc; 1022 } 1023 EXPORT_SYMBOL_GPL(sev_do_cmd); 1024 1025 static int __sev_init_locked(int *error) 1026 { 1027 struct sev_data_init data; 1028 1029 memset(&data, 0, sizeof(data)); 1030 if (sev_es_tmr) { 1031 /* 1032 * Do not include the encryption mask on the physical 1033 * address of the TMR (firmware should clear it anyway). 1034 */ 1035 data.tmr_address = __pa(sev_es_tmr); 1036 1037 data.flags |= SEV_INIT_FLAGS_SEV_ES; 1038 data.tmr_len = sev_es_tmr_size; 1039 } 1040 1041 return __sev_do_cmd_locked(SEV_CMD_INIT, &data, error); 1042 } 1043 1044 static int __sev_init_ex_locked(int *error) 1045 { 1046 struct sev_data_init_ex data; 1047 1048 memset(&data, 0, sizeof(data)); 1049 data.length = sizeof(data); 1050 data.nv_address = __psp_pa(sev_init_ex_buffer); 1051 data.nv_len = NV_LENGTH; 1052 1053 if (sev_es_tmr) { 1054 /* 1055 * Do not include the encryption mask on the physical 1056 * address of the TMR (firmware should clear it anyway). 1057 */ 1058 data.tmr_address = __pa(sev_es_tmr); 1059 1060 data.flags |= SEV_INIT_FLAGS_SEV_ES; 1061 data.tmr_len = sev_es_tmr_size; 1062 } 1063 1064 return __sev_do_cmd_locked(SEV_CMD_INIT_EX, &data, error); 1065 } 1066 1067 static inline int __sev_do_init_locked(int *psp_ret) 1068 { 1069 if (sev_init_ex_buffer) 1070 return __sev_init_ex_locked(psp_ret); 1071 else 1072 return __sev_init_locked(psp_ret); 1073 } 1074 1075 /* Hypervisor Fixed pages API interface */ 1076 static void snp_hv_fixed_pages_state_update(struct sev_device *sev, 1077 enum snp_hv_fixed_pages_state page_state) 1078 { 1079 struct snp_hv_fixed_pages_entry *entry; 1080 1081 /* List is protected by sev_cmd_mutex */ 1082 lockdep_assert_held(&sev_cmd_mutex); 1083 1084 if (list_empty(&snp_hv_fixed_pages)) 1085 return; 1086 1087 list_for_each_entry(entry, &snp_hv_fixed_pages, list) 1088 entry->page_state = page_state; 1089 } 1090 1091 /* 1092 * Allocate HV_FIXED pages in 2MB aligned sizes to ensure the whole 1093 * 2MB pages are marked as HV_FIXED. 1094 */ 1095 struct page *snp_alloc_hv_fixed_pages(unsigned int num_2mb_pages) 1096 { 1097 struct psp_device *psp_master = psp_get_master_device(); 1098 struct snp_hv_fixed_pages_entry *entry; 1099 unsigned int order; 1100 struct page *page; 1101 1102 if (!psp_master) 1103 return NULL; 1104 1105 order = get_order(PMD_SIZE * num_2mb_pages); 1106 1107 /* 1108 * SNP_INIT_EX is protected by sev_cmd_mutex, therefore this list 1109 * also needs to be protected using the same mutex. 1110 */ 1111 guard(mutex)(&sev_cmd_mutex); 1112 1113 /* 1114 * This API uses SNP_INIT_EX to transition allocated pages to HV_Fixed 1115 * page state, fail if SNP is already initialized. 1116 */ 1117 if (psp_master->sev_data && 1118 ((struct sev_device *)psp_master->sev_data)->snp_initialized) 1119 return NULL; 1120 1121 /* Re-use freed pages that match the request */ 1122 list_for_each_entry(entry, &snp_hv_fixed_pages, list) { 1123 /* Hypervisor fixed page allocator implements exact fit policy */ 1124 if (entry->order == order && entry->free) { 1125 entry->free = false; 1126 memset(page_address(entry->page), 0, 1127 (1 << entry->order) * PAGE_SIZE); 1128 return entry->page; 1129 } 1130 } 1131 1132 page = alloc_pages(GFP_KERNEL | __GFP_ZERO, order); 1133 if (!page) 1134 return NULL; 1135 1136 entry = kzalloc_obj(*entry); 1137 if (!entry) { 1138 __free_pages(page, order); 1139 return NULL; 1140 } 1141 1142 entry->page = page; 1143 entry->order = order; 1144 list_add_tail(&entry->list, &snp_hv_fixed_pages); 1145 1146 return page; 1147 } 1148 1149 void snp_free_hv_fixed_pages(struct page *page) 1150 { 1151 struct psp_device *psp_master = psp_get_master_device(); 1152 struct snp_hv_fixed_pages_entry *entry, *nentry; 1153 1154 if (!psp_master) 1155 return; 1156 1157 /* 1158 * SNP_INIT_EX is protected by sev_cmd_mutex, therefore this list 1159 * also needs to be protected using the same mutex. 1160 */ 1161 guard(mutex)(&sev_cmd_mutex); 1162 1163 list_for_each_entry_safe(entry, nentry, &snp_hv_fixed_pages, list) { 1164 if (entry->page != page) 1165 continue; 1166 1167 /* 1168 * HV_FIXED page state cannot be changed until reboot 1169 * and they cannot be used by an SNP guest, so they cannot 1170 * be returned back to the page allocator. 1171 * Mark the pages as free internally to allow possible re-use. 1172 */ 1173 if (entry->page_state == HV_FIXED) { 1174 entry->free = true; 1175 } else { 1176 __free_pages(page, entry->order); 1177 list_del(&entry->list); 1178 kfree(entry); 1179 } 1180 return; 1181 } 1182 } 1183 1184 static void snp_add_hv_fixed_pages(struct sev_device *sev, struct sev_data_range_list *range_list) 1185 { 1186 struct snp_hv_fixed_pages_entry *entry; 1187 struct sev_data_range *range; 1188 int num_elements; 1189 1190 lockdep_assert_held(&sev_cmd_mutex); 1191 1192 if (list_empty(&snp_hv_fixed_pages)) 1193 return; 1194 1195 num_elements = list_count_nodes(&snp_hv_fixed_pages) + 1196 range_list->num_elements; 1197 1198 /* 1199 * Ensure the list of HV_FIXED pages that will be passed to firmware 1200 * do not exceed the page-sized argument buffer. 1201 */ 1202 if (num_elements * sizeof(*range) + sizeof(*range_list) > PAGE_SIZE) { 1203 dev_warn(sev->dev, "Additional HV_Fixed pages cannot be accommodated, omitting\n"); 1204 return; 1205 } 1206 1207 range = &range_list->ranges[range_list->num_elements]; 1208 list_for_each_entry(entry, &snp_hv_fixed_pages, list) { 1209 range->base = page_to_pfn(entry->page) << PAGE_SHIFT; 1210 range->page_count = 1 << entry->order; 1211 range++; 1212 } 1213 range_list->num_elements = num_elements; 1214 } 1215 1216 static void snp_leak_hv_fixed_pages(void) 1217 { 1218 struct snp_hv_fixed_pages_entry *entry, *nentry; 1219 1220 /* List is protected by sev_cmd_mutex */ 1221 lockdep_assert_held(&sev_cmd_mutex); 1222 1223 if (list_empty(&snp_hv_fixed_pages)) 1224 return; 1225 1226 list_for_each_entry_safe(entry, nentry, &snp_hv_fixed_pages, list) { 1227 if (entry->free && entry->page_state != HV_FIXED) 1228 __free_pages(entry->page, entry->order); 1229 else 1230 __snp_leak_pages(page_to_pfn(entry->page), 1231 1 << entry->order, false); 1232 1233 list_del(&entry->list); 1234 kfree(entry); 1235 } 1236 } 1237 1238 bool sev_is_snp_ciphertext_hiding_supported(void) 1239 { 1240 struct psp_device *psp = psp_master; 1241 struct sev_device *sev; 1242 1243 if (!psp || !psp->sev_data) 1244 return false; 1245 1246 sev = psp->sev_data; 1247 1248 /* 1249 * Feature information indicates if CipherTextHiding feature is 1250 * supported by the SEV firmware and additionally platform status 1251 * indicates if CipherTextHiding feature is enabled in the 1252 * Platform BIOS. 1253 */ 1254 return ((sev->snp_feat_info_0.ecx & SNP_CIPHER_TEXT_HIDING_SUPPORTED) && 1255 sev->snp_plat_status.ciphertext_hiding_cap); 1256 } 1257 EXPORT_SYMBOL_GPL(sev_is_snp_ciphertext_hiding_supported); 1258 1259 static int snp_get_platform_data(struct sev_device *sev, int *error) 1260 { 1261 struct sev_data_snp_feature_info snp_feat_info; 1262 struct snp_feature_info *feat_info; 1263 struct sev_data_snp_addr buf; 1264 struct page *page; 1265 int rc; 1266 1267 /* 1268 * This function is expected to be called before SNP is 1269 * initialized. 1270 */ 1271 if (sev->snp_initialized) 1272 return -EINVAL; 1273 1274 buf.address = __psp_pa(&sev->snp_plat_status); 1275 rc = sev_do_cmd(SEV_CMD_SNP_PLATFORM_STATUS, &buf, error); 1276 if (rc) { 1277 dev_err(sev->dev, "SNP PLATFORM_STATUS command failed, ret = %d, error = %#x\n", 1278 rc, *error); 1279 return rc; 1280 } 1281 1282 sev->api_major = sev->snp_plat_status.api_major; 1283 sev->api_minor = sev->snp_plat_status.api_minor; 1284 sev->build = sev->snp_plat_status.build_id; 1285 1286 /* 1287 * Do feature discovery of the currently loaded firmware, 1288 * and cache feature information from CPUID 0x8000_0024, 1289 * sub-function 0. 1290 */ 1291 if (!sev->snp_plat_status.feature_info) 1292 return 0; 1293 1294 /* 1295 * Use dynamically allocated structure for the SNP_FEATURE_INFO 1296 * command to ensure structure is 8-byte aligned, and does not 1297 * cross a page boundary. 1298 */ 1299 page = alloc_page(GFP_KERNEL); 1300 if (!page) 1301 return -ENOMEM; 1302 1303 feat_info = page_address(page); 1304 snp_feat_info.length = sizeof(snp_feat_info); 1305 snp_feat_info.ecx_in = 0; 1306 snp_feat_info.feature_info_paddr = __psp_pa(feat_info); 1307 1308 rc = sev_do_cmd(SEV_CMD_SNP_FEATURE_INFO, &snp_feat_info, error); 1309 if (!rc) 1310 sev->snp_feat_info_0 = *feat_info; 1311 else 1312 dev_err(sev->dev, "SNP FEATURE_INFO command failed, ret = %d, error = %#x\n", 1313 rc, *error); 1314 1315 __free_page(page); 1316 1317 return rc; 1318 } 1319 1320 static int snp_filter_reserved_mem_regions(struct resource *rs, void *arg) 1321 { 1322 struct sev_data_range_list *range_list = arg; 1323 struct sev_data_range *range = &range_list->ranges[range_list->num_elements]; 1324 size_t size; 1325 1326 /* 1327 * Ensure the list of HV_FIXED pages passed to the firmware including 1328 * the one about to be written to do not exceed the page-sized argument 1329 * buffer. 1330 */ 1331 if (((range_list->num_elements + 1) * sizeof(struct sev_data_range) + 1332 sizeof(struct sev_data_range_list)) > PAGE_SIZE) 1333 return -E2BIG; 1334 1335 switch (rs->desc) { 1336 case E820_TYPE_RESERVED: 1337 case E820_TYPE_PMEM: 1338 case E820_TYPE_ACPI: 1339 range->base = rs->start & PAGE_MASK; 1340 size = PAGE_ALIGN((rs->end + 1) - rs->start); 1341 range->page_count = size >> PAGE_SHIFT; 1342 range_list->num_elements++; 1343 break; 1344 default: 1345 break; 1346 } 1347 1348 return 0; 1349 } 1350 1351 static int __sev_snp_init_locked(int *error, unsigned int max_snp_asid) 1352 { 1353 struct sev_data_range_list *snp_range_list __free(kfree) = NULL; 1354 struct psp_device *psp = psp_master; 1355 struct sev_data_snp_init_ex data = {}; 1356 struct sev_device *sev; 1357 void *arg = &data; 1358 int cmd, rc = 0; 1359 1360 if (!cc_platform_has(CC_ATTR_HOST_SEV_SNP)) 1361 return -ENODEV; 1362 1363 sev = psp->sev_data; 1364 1365 if (sev->snp_initialized) 1366 return 0; 1367 1368 if (!sev_version_greater_or_equal(SNP_MIN_API_MAJOR, SNP_MIN_API_MINOR)) { 1369 dev_dbg(sev->dev, "SEV-SNP support requires firmware version >= %d:%d\n", 1370 SNP_MIN_API_MAJOR, SNP_MIN_API_MINOR); 1371 return -EOPNOTSUPP; 1372 } 1373 1374 rc = snp_prepare(); 1375 if (rc) 1376 return rc; 1377 1378 /* 1379 * Starting in SNP firmware v1.52, the SNP_INIT_EX command takes a list 1380 * of system physical address ranges to convert into HV-fixed page 1381 * states during the RMP initialization. For instance, the memory that 1382 * UEFI reserves should be included in the that list. This allows system 1383 * components that occasionally write to memory (e.g. logging to UEFI 1384 * reserved regions) to not fail due to RMP initialization and SNP 1385 * enablement. 1386 * 1387 */ 1388 if (sev_version_greater_or_equal(SNP_MIN_API_MAJOR, 52)) { 1389 bool tio_supp = !!(sev->snp_feat_info_0.ebx & SNP_SEV_TIO_SUPPORTED); 1390 1391 /* 1392 * Firmware checks that the pages containing the ranges enumerated 1393 * in the RANGES structure are either in the default page state or in the 1394 * firmware page state. 1395 */ 1396 snp_range_list = kzalloc(PAGE_SIZE, GFP_KERNEL); 1397 if (!snp_range_list) { 1398 dev_err(sev->dev, 1399 "SEV: SNP_INIT_EX range list memory allocation failed\n"); 1400 return -ENOMEM; 1401 } 1402 1403 /* 1404 * Retrieve all reserved memory regions from the e820 memory map 1405 * to be setup as HV-fixed pages. 1406 */ 1407 rc = walk_iomem_res_desc(IORES_DESC_NONE, IORESOURCE_MEM, 0, ~0, 1408 snp_range_list, snp_filter_reserved_mem_regions); 1409 if (rc) { 1410 dev_err(sev->dev, 1411 "SEV: SNP_INIT_EX walk_iomem_res_desc failed rc = %d\n", rc); 1412 return rc; 1413 } 1414 1415 /* 1416 * Add HV_Fixed pages from other PSP sub-devices, such as SFS to the 1417 * HV_Fixed page list. 1418 */ 1419 snp_add_hv_fixed_pages(sev, snp_range_list); 1420 1421 if (max_snp_asid) { 1422 data.ciphertext_hiding_en = 1; 1423 data.max_snp_asid = max_snp_asid; 1424 } 1425 1426 data.init_rmp = 1; 1427 data.list_paddr_en = 1; 1428 data.list_paddr = __psp_pa(snp_range_list); 1429 1430 data.tio_en = tio_supp && sev_tio_enabled && amd_iommu_sev_tio_supported(); 1431 1432 /* 1433 * When psp_init_on_probe is disabled, the userspace calling 1434 * SEV ioctl can inadvertently shut down SNP and SEV-TIO causing 1435 * unexpected state loss. 1436 */ 1437 if (data.tio_en && !psp_init_on_probe) 1438 dev_warn(sev->dev, "SEV-TIO as incompatible with psp_init_on_probe=0\n"); 1439 1440 cmd = SEV_CMD_SNP_INIT_EX; 1441 } else { 1442 cmd = SEV_CMD_SNP_INIT; 1443 arg = NULL; 1444 } 1445 1446 /* 1447 * The following sequence must be issued before launching the first SNP 1448 * guest to ensure all dirty cache lines are flushed, including from 1449 * updates to the RMP table itself via the RMPUPDATE instruction: 1450 * 1451 * - WBINVD on all running CPUs 1452 * - SEV_CMD_SNP_INIT[_EX] firmware command 1453 * - WBINVD on all running CPUs 1454 * - SEV_CMD_SNP_DF_FLUSH firmware command 1455 */ 1456 wbinvd_on_all_cpus(); 1457 1458 rc = __sev_do_cmd_locked(cmd, arg, error); 1459 if (rc) { 1460 dev_err(sev->dev, "SEV-SNP: %s failed rc %d, error %#x\n", 1461 cmd == SEV_CMD_SNP_INIT_EX ? "SNP_INIT_EX" : "SNP_INIT", 1462 rc, *error); 1463 return rc; 1464 } 1465 1466 /* Prepare for first SNP guest launch after INIT. */ 1467 wbinvd_on_all_cpus(); 1468 rc = __sev_do_cmd_locked(SEV_CMD_SNP_DF_FLUSH, NULL, error); 1469 if (rc) { 1470 dev_err(sev->dev, "SEV-SNP: SNP_DF_FLUSH failed rc %d, error %#x\n", 1471 rc, *error); 1472 return rc; 1473 } 1474 1475 snp_hv_fixed_pages_state_update(sev, HV_FIXED); 1476 sev->snp_initialized = true; 1477 dev_dbg(sev->dev, "SEV-SNP firmware initialized, SEV-TIO is %s\n", 1478 data.tio_en ? "enabled" : "disabled"); 1479 1480 dev_info(sev->dev, "SEV-SNP API:%d.%d build:%d\n", sev->api_major, 1481 sev->api_minor, sev->build); 1482 1483 atomic_notifier_chain_register(&panic_notifier_list, 1484 &snp_panic_notifier); 1485 1486 if (data.tio_en) { 1487 struct page *page; 1488 1489 /* 1490 * This executes with the sev_cmd_mutex held so down the stack 1491 * snp_reclaim_pages(locked=false) might be needed (which is extremely 1492 * unlikely) but will cause a deadlock. 1493 * Instead of exporting __snp_alloc_firmware_pages(), allocate a page 1494 * for this one call here. 1495 */ 1496 page = __snp_alloc_firmware_pages(GFP_KERNEL_ACCOUNT | __GFP_ZERO, 1497 0, true); 1498 if (page) { 1499 void *tio_status = page_address(page); 1500 1501 sev_tsm_init_locked(sev, tio_status); 1502 1503 __snp_free_firmware_pages(page, 0, true); 1504 } 1505 } 1506 1507 sev_es_tmr_size = SNP_TMR_SIZE; 1508 1509 return 0; 1510 } 1511 1512 static void __sev_platform_init_handle_tmr(struct sev_device *sev) 1513 { 1514 if (sev_es_tmr) 1515 return; 1516 1517 /* Obtain the TMR memory area for SEV-ES use */ 1518 sev_es_tmr = sev_fw_alloc(sev_es_tmr_size); 1519 if (sev_es_tmr) { 1520 /* Must flush the cache before giving it to the firmware */ 1521 if (!sev->snp_initialized) 1522 clflush_cache_range(sev_es_tmr, sev_es_tmr_size); 1523 } else { 1524 dev_warn(sev->dev, "SEV: TMR allocation failed, SEV-ES support unavailable\n"); 1525 } 1526 } 1527 1528 /* 1529 * If an init_ex_path is provided allocate a buffer for the file and 1530 * read in the contents. Additionally, if SNP is initialized, convert 1531 * the buffer pages to firmware pages. 1532 */ 1533 static int __sev_platform_init_handle_init_ex_path(struct sev_device *sev) 1534 { 1535 struct page *page; 1536 int rc; 1537 1538 if (!init_ex_path) 1539 return 0; 1540 1541 if (sev_init_ex_buffer) 1542 return 0; 1543 1544 page = alloc_pages(GFP_KERNEL, get_order(NV_LENGTH)); 1545 if (!page) { 1546 dev_err(sev->dev, "SEV: INIT_EX NV memory allocation failed\n"); 1547 return -ENOMEM; 1548 } 1549 1550 sev_init_ex_buffer = page_address(page); 1551 1552 rc = sev_read_init_ex_file(); 1553 if (rc) 1554 return rc; 1555 1556 /* If SEV-SNP is initialized, transition to firmware page. */ 1557 if (sev->snp_initialized) { 1558 unsigned long npages; 1559 1560 npages = 1UL << get_order(NV_LENGTH); 1561 if (rmp_mark_pages_firmware(__pa(sev_init_ex_buffer), npages, false)) { 1562 dev_err(sev->dev, "SEV: INIT_EX NV memory page state change failed.\n"); 1563 return -ENOMEM; 1564 } 1565 } 1566 1567 return 0; 1568 } 1569 1570 static int __sev_platform_init_locked(int *error) 1571 { 1572 int rc, psp_ret, dfflush_error; 1573 struct sev_device *sev; 1574 1575 psp_ret = dfflush_error = SEV_RET_NO_FW_CALL; 1576 1577 if (!psp_master || !psp_master->sev_data) 1578 return -ENODEV; 1579 1580 sev = psp_master->sev_data; 1581 1582 if (sev->sev_plat_status.state == SEV_STATE_INIT) 1583 return 0; 1584 1585 __sev_platform_init_handle_tmr(sev); 1586 1587 rc = __sev_platform_init_handle_init_ex_path(sev); 1588 if (rc) 1589 return rc; 1590 1591 rc = __sev_do_init_locked(&psp_ret); 1592 if (rc && psp_ret == SEV_RET_SECURE_DATA_INVALID) { 1593 /* 1594 * Initialization command returned an integrity check failure 1595 * status code, meaning that firmware load and validation of SEV 1596 * related persistent data has failed. Retrying the 1597 * initialization function should succeed by replacing the state 1598 * with a reset state. 1599 */ 1600 dev_err(sev->dev, 1601 "SEV: retrying INIT command because of SECURE_DATA_INVALID error. Retrying once to reset PSP SEV state."); 1602 rc = __sev_do_init_locked(&psp_ret); 1603 } 1604 1605 if (error) 1606 *error = psp_ret; 1607 1608 if (rc) { 1609 dev_err(sev->dev, "SEV: %s failed %#x, rc %d\n", 1610 sev_init_ex_buffer ? "INIT_EX" : "INIT", psp_ret, rc); 1611 return rc; 1612 } 1613 1614 sev->sev_plat_status.state = SEV_STATE_INIT; 1615 1616 /* Prepare for first SEV guest launch after INIT */ 1617 wbinvd_on_all_cpus(); 1618 rc = __sev_do_cmd_locked(SEV_CMD_DF_FLUSH, NULL, &dfflush_error); 1619 if (rc) { 1620 dev_err(sev->dev, "SEV: DF_FLUSH failed %#x, rc %d\n", 1621 dfflush_error, rc); 1622 return rc; 1623 } 1624 1625 dev_dbg(sev->dev, "SEV firmware initialized\n"); 1626 1627 dev_info(sev->dev, "SEV API:%d.%d build:%d\n", sev->api_major, 1628 sev->api_minor, sev->build); 1629 1630 return 0; 1631 } 1632 1633 static int _sev_platform_init_locked(struct sev_platform_init_args *args) 1634 { 1635 struct sev_device *sev; 1636 int rc; 1637 1638 if (!psp_master || !psp_master->sev_data) 1639 return -ENODEV; 1640 1641 /* 1642 * Skip SNP/SEV initialization under a kdump kernel as SEV/SNP 1643 * may already be initialized in the previous kernel. Since no 1644 * SNP/SEV guests are run under a kdump kernel, there is no 1645 * need to initialize SNP or SEV during kdump boot. 1646 */ 1647 if (is_kdump_kernel()) 1648 return 0; 1649 1650 sev = psp_master->sev_data; 1651 1652 if (sev->sev_plat_status.state == SEV_STATE_INIT) 1653 return 0; 1654 1655 rc = __sev_snp_init_locked(&args->error, args->max_snp_asid); 1656 if (rc && rc != -ENODEV) 1657 return rc; 1658 1659 /* Defer legacy SEV/SEV-ES support if allowed by caller/module. */ 1660 if (args->probe && !psp_init_on_probe) 1661 return 0; 1662 1663 return __sev_platform_init_locked(&args->error); 1664 } 1665 1666 int sev_platform_init(struct sev_platform_init_args *args) 1667 { 1668 int rc; 1669 1670 mutex_lock(&sev_cmd_mutex); 1671 rc = _sev_platform_init_locked(args); 1672 mutex_unlock(&sev_cmd_mutex); 1673 1674 return rc; 1675 } 1676 EXPORT_SYMBOL_GPL(sev_platform_init); 1677 1678 static int __sev_platform_shutdown_locked(int *error) 1679 { 1680 struct psp_device *psp = psp_master; 1681 struct sev_device *sev; 1682 int ret; 1683 1684 if (!psp || !psp->sev_data) 1685 return 0; 1686 1687 sev = psp->sev_data; 1688 1689 if (sev->sev_plat_status.state == SEV_STATE_UNINIT) 1690 return 0; 1691 1692 ret = __sev_do_cmd_locked(SEV_CMD_SHUTDOWN, NULL, error); 1693 if (ret) { 1694 dev_err(sev->dev, "SEV: failed to SHUTDOWN error %#x, rc %d\n", 1695 *error, ret); 1696 return ret; 1697 } 1698 1699 sev->sev_plat_status.state = SEV_STATE_UNINIT; 1700 dev_dbg(sev->dev, "SEV firmware shutdown\n"); 1701 1702 return ret; 1703 } 1704 1705 static int sev_get_platform_state(int *state, int *error) 1706 { 1707 struct sev_user_data_status data; 1708 int rc; 1709 1710 rc = __sev_do_cmd_locked(SEV_CMD_PLATFORM_STATUS, &data, error); 1711 if (rc) 1712 return rc; 1713 1714 *state = data.state; 1715 return rc; 1716 } 1717 1718 static int sev_move_to_init_state(struct sev_issue_cmd *argp, bool *shutdown_required) 1719 { 1720 int rc; 1721 1722 rc = __sev_platform_init_locked(&argp->error); 1723 if (rc) 1724 return rc; 1725 1726 *shutdown_required = true; 1727 1728 return 0; 1729 } 1730 1731 static int sev_ioctl_do_reset(struct sev_issue_cmd *argp, bool writable) 1732 { 1733 int state, rc; 1734 1735 if (!writable) 1736 return -EPERM; 1737 1738 /* 1739 * The SEV spec requires that FACTORY_RESET must be issued in 1740 * UNINIT state. Before we go further lets check if any guest is 1741 * active. 1742 * 1743 * If FW is in WORKING state then deny the request otherwise issue 1744 * SHUTDOWN command do INIT -> UNINIT before issuing the FACTORY_RESET. 1745 * 1746 */ 1747 rc = sev_get_platform_state(&state, &argp->error); 1748 if (rc) 1749 return rc; 1750 1751 if (state == SEV_STATE_WORKING) 1752 return -EBUSY; 1753 1754 if (state == SEV_STATE_INIT) { 1755 rc = __sev_platform_shutdown_locked(&argp->error); 1756 if (rc) 1757 return rc; 1758 } 1759 1760 return __sev_do_cmd_locked(SEV_CMD_FACTORY_RESET, NULL, &argp->error); 1761 } 1762 1763 static int sev_ioctl_do_platform_status(struct sev_issue_cmd *argp) 1764 { 1765 struct sev_user_data_status data; 1766 int ret; 1767 1768 memset(&data, 0, sizeof(data)); 1769 1770 ret = __sev_do_cmd_locked(SEV_CMD_PLATFORM_STATUS, &data, &argp->error); 1771 if (ret) 1772 return ret; 1773 1774 if (copy_to_user((void __user *)argp->data, &data, sizeof(data))) 1775 ret = -EFAULT; 1776 1777 return ret; 1778 } 1779 1780 static int sev_ioctl_do_pek_pdh_gen(int cmd, struct sev_issue_cmd *argp, bool writable) 1781 { 1782 struct sev_device *sev = psp_master->sev_data; 1783 bool shutdown_required = false; 1784 int rc; 1785 1786 if (!writable) 1787 return -EPERM; 1788 1789 if (sev->sev_plat_status.state == SEV_STATE_UNINIT) { 1790 rc = sev_move_to_init_state(argp, &shutdown_required); 1791 if (rc) 1792 return rc; 1793 } 1794 1795 rc = __sev_do_cmd_locked(cmd, NULL, &argp->error); 1796 1797 if (shutdown_required) 1798 __sev_firmware_shutdown(sev, false); 1799 1800 return rc; 1801 } 1802 1803 static int sev_ioctl_do_pek_csr(struct sev_issue_cmd *argp, bool writable) 1804 { 1805 struct sev_device *sev = psp_master->sev_data; 1806 struct sev_user_data_pek_csr input; 1807 bool shutdown_required = false; 1808 struct sev_data_pek_csr data; 1809 void __user *input_address; 1810 void *blob = NULL; 1811 int ret; 1812 1813 if (!writable) 1814 return -EPERM; 1815 1816 if (copy_from_user(&input, (void __user *)argp->data, sizeof(input))) 1817 return -EFAULT; 1818 1819 memset(&data, 0, sizeof(data)); 1820 1821 /* userspace wants to query CSR length */ 1822 if (!input.address || !input.length) 1823 goto cmd; 1824 1825 /* allocate a physically contiguous buffer to store the CSR blob */ 1826 input_address = (void __user *)input.address; 1827 if (input.length > SEV_FW_BLOB_MAX_SIZE) 1828 return -EFAULT; 1829 1830 blob = kzalloc(input.length, GFP_KERNEL); 1831 if (!blob) 1832 return -ENOMEM; 1833 1834 data.address = __psp_pa(blob); 1835 data.len = input.length; 1836 1837 cmd: 1838 if (sev->sev_plat_status.state == SEV_STATE_UNINIT) { 1839 ret = sev_move_to_init_state(argp, &shutdown_required); 1840 if (ret) 1841 goto e_free_blob; 1842 } 1843 1844 ret = __sev_do_cmd_locked(SEV_CMD_PEK_CSR, &data, &argp->error); 1845 1846 /* 1847 * Firmware will returns the length of the CSR blob (either the minimum 1848 * required length or the actual length written), return it to the user. 1849 */ 1850 input.length = data.len; 1851 1852 if (copy_to_user((void __user *)argp->data, &input, sizeof(input))) { 1853 ret = -EFAULT; 1854 goto e_free_blob; 1855 } 1856 1857 if (ret || WARN_ON_ONCE(argp->error)) 1858 goto e_free_blob; 1859 1860 if (blob) { 1861 if (copy_to_user(input_address, blob, input.length)) 1862 ret = -EFAULT; 1863 } 1864 1865 e_free_blob: 1866 if (shutdown_required) 1867 __sev_firmware_shutdown(sev, false); 1868 1869 kfree(blob); 1870 return ret; 1871 } 1872 1873 void *psp_copy_user_blob(u64 uaddr, u32 len) 1874 { 1875 if (!uaddr || !len) 1876 return ERR_PTR(-EINVAL); 1877 1878 /* verify that blob length does not exceed our limit */ 1879 if (len > SEV_FW_BLOB_MAX_SIZE) 1880 return ERR_PTR(-EINVAL); 1881 1882 return memdup_user((void __user *)uaddr, len); 1883 } 1884 EXPORT_SYMBOL_GPL(psp_copy_user_blob); 1885 1886 static int sev_get_api_version(void) 1887 { 1888 struct sev_device *sev = psp_master->sev_data; 1889 struct sev_user_data_status status; 1890 int error = 0, ret; 1891 1892 /* 1893 * Cache SNP platform status and SNP feature information 1894 * if SNP is available. 1895 */ 1896 if (cc_platform_has(CC_ATTR_HOST_SEV_SNP)) { 1897 ret = snp_get_platform_data(sev, &error); 1898 if (ret) 1899 return 1; 1900 } 1901 1902 ret = sev_platform_status(&status, &error); 1903 if (ret) { 1904 dev_err(sev->dev, 1905 "SEV: failed to get status. Error: %#x\n", error); 1906 return 1; 1907 } 1908 1909 /* Cache SEV platform status */ 1910 sev->sev_plat_status = status; 1911 1912 sev->api_major = status.api_major; 1913 sev->api_minor = status.api_minor; 1914 sev->build = status.build; 1915 1916 return 0; 1917 } 1918 1919 static int sev_get_firmware(struct device *dev, 1920 const struct firmware **firmware) 1921 { 1922 char fw_name_specific[SEV_FW_NAME_SIZE]; 1923 char fw_name_subset[SEV_FW_NAME_SIZE]; 1924 1925 snprintf(fw_name_specific, sizeof(fw_name_specific), 1926 "amd/amd_sev_fam%.2xh_model%.2xh.sbin", 1927 boot_cpu_data.x86, boot_cpu_data.x86_model); 1928 1929 snprintf(fw_name_subset, sizeof(fw_name_subset), 1930 "amd/amd_sev_fam%.2xh_model%.1xxh.sbin", 1931 boot_cpu_data.x86, (boot_cpu_data.x86_model & 0xf0) >> 4); 1932 1933 /* Check for SEV FW for a particular model. 1934 * Ex. amd_sev_fam17h_model00h.sbin for Family 17h Model 00h 1935 * 1936 * or 1937 * 1938 * Check for SEV FW common to a subset of models. 1939 * Ex. amd_sev_fam17h_model0xh.sbin for 1940 * Family 17h Model 00h -- Family 17h Model 0Fh 1941 * 1942 * or 1943 * 1944 * Fall-back to using generic name: sev.fw 1945 */ 1946 if ((firmware_request_nowarn(firmware, fw_name_specific, dev) >= 0) || 1947 (firmware_request_nowarn(firmware, fw_name_subset, dev) >= 0) || 1948 (firmware_request_nowarn(firmware, SEV_FW_FILE, dev) >= 0)) 1949 return 0; 1950 1951 return -ENOENT; 1952 } 1953 1954 /* Don't fail if SEV FW couldn't be updated. Continue with existing SEV FW */ 1955 static int sev_update_firmware(struct device *dev) 1956 { 1957 struct sev_data_download_firmware data; 1958 const struct firmware *firmware; 1959 int ret, error, order; 1960 struct page *p; 1961 void *fw_blob; 1962 1963 if (!sev_version_greater_or_equal(0, 15)) { 1964 dev_dbg(dev, "DOWNLOAD_FIRMWARE not supported\n"); 1965 return -1; 1966 } 1967 1968 if (sev_get_firmware(dev, &firmware) == -ENOENT) { 1969 dev_dbg(dev, "No SEV firmware file present\n"); 1970 return -1; 1971 } 1972 1973 order = get_order(firmware->size); 1974 p = alloc_pages(GFP_KERNEL, order); 1975 if (!p) { 1976 ret = -1; 1977 goto fw_err; 1978 } 1979 1980 /* 1981 * Copy firmware data to a kernel allocated contiguous 1982 * memory region. 1983 */ 1984 fw_blob = page_address(p); 1985 memcpy(fw_blob, firmware->data, firmware->size); 1986 1987 data.address = __psp_pa(fw_blob); 1988 data.len = firmware->size; 1989 1990 ret = sev_do_cmd(SEV_CMD_DOWNLOAD_FIRMWARE, &data, &error); 1991 1992 /* 1993 * A quirk for fixing the committed TCB version, when upgrading from 1994 * earlier firmware version than 1.50. 1995 */ 1996 if (!ret && !sev_version_greater_or_equal(1, 50)) 1997 ret = sev_do_cmd(SEV_CMD_DOWNLOAD_FIRMWARE, &data, &error); 1998 1999 if (ret) 2000 dev_dbg(dev, "Failed to update SEV firmware: %#x\n", error); 2001 2002 __free_pages(p, order); 2003 2004 fw_err: 2005 release_firmware(firmware); 2006 2007 return ret; 2008 } 2009 2010 static int __sev_snp_shutdown_locked(int *error, bool panic) 2011 { 2012 struct psp_device *psp = psp_master; 2013 struct sev_device *sev; 2014 struct sev_data_snp_shutdown_ex data; 2015 int ret; 2016 2017 if (!psp || !psp->sev_data) 2018 return 0; 2019 2020 sev = psp->sev_data; 2021 2022 if (!sev->snp_initialized) 2023 return 0; 2024 2025 memset(&data, 0, sizeof(data)); 2026 data.len = sizeof(data); 2027 data.iommu_snp_shutdown = 1; 2028 if (sev->snp_feat_info_0.ecx & SNP_X86_SHUTDOWN_SUPPORTED) 2029 data.x86_snp_shutdown = 1; 2030 2031 /* 2032 * If invoked during panic handling, local interrupts are disabled 2033 * and all CPUs are stopped, so wbinvd_on_all_cpus() can't be called. 2034 * In that case, a wbinvd() is done on remote CPUs via the NMI 2035 * callback, so only a local wbinvd() is needed here. 2036 */ 2037 if (!panic) 2038 wbinvd_on_all_cpus(); 2039 else 2040 wbinvd(); 2041 2042 ret = __sev_do_cmd_locked(SEV_CMD_SNP_SHUTDOWN_EX, &data, error); 2043 /* SHUTDOWN may require DF_FLUSH */ 2044 if (*error == SEV_RET_DFFLUSH_REQUIRED) { 2045 int dfflush_error = SEV_RET_NO_FW_CALL; 2046 2047 ret = __sev_do_cmd_locked(SEV_CMD_SNP_DF_FLUSH, NULL, &dfflush_error); 2048 if (ret) { 2049 dev_err(sev->dev, "SEV-SNP DF_FLUSH failed, ret = %d, error = %#x\n", 2050 ret, dfflush_error); 2051 return ret; 2052 } 2053 /* reissue the shutdown command */ 2054 ret = __sev_do_cmd_locked(SEV_CMD_SNP_SHUTDOWN_EX, &data, 2055 error); 2056 } 2057 if (ret) { 2058 dev_err(sev->dev, "SEV-SNP firmware shutdown failed, rc %d, error %#x\n", 2059 ret, *error); 2060 return ret; 2061 } 2062 2063 if (data.x86_snp_shutdown) { 2064 if (!panic) 2065 snp_shutdown(); 2066 snp_hv_fixed_pages_state_update(sev, ALLOCATED); 2067 } else { 2068 /* 2069 * SNP_SHUTDOWN_EX with IOMMU_SNP_SHUTDOWN set to 1 disables SNP 2070 * enforcement by the IOMMU and also transitions all pages 2071 * associated with the IOMMU to the Reclaim state. 2072 * Firmware was transitioning the IOMMU pages to Hypervisor state 2073 * before version 1.53. But, accounting for the number of assigned 2074 * 4kB pages in a 2M page was done incorrectly by not transitioning 2075 * to the Reclaim state. This resulted in RMP #PF when later accessing 2076 * the 2M page containing those pages during kexec boot. Hence, the 2077 * firmware now transitions these pages to Reclaim state and hypervisor 2078 * needs to transition these pages to shared state. SNP Firmware 2079 * version 1.53 and above are needed for kexec boot. 2080 */ 2081 ret = amd_iommu_snp_disable(); 2082 if (ret) { 2083 dev_err(sev->dev, "SNP IOMMU shutdown failed\n"); 2084 return ret; 2085 } 2086 } 2087 2088 snp_leak_hv_fixed_pages(); 2089 sev->snp_initialized = false; 2090 dev_dbg(sev->dev, "SEV-SNP firmware shutdown\n"); 2091 2092 /* 2093 * __sev_snp_shutdown_locked() deadlocks when it tries to unregister 2094 * itself during panic as the panic notifier is called with RCU read 2095 * lock held and notifier unregistration does RCU synchronization. 2096 */ 2097 if (!panic) 2098 atomic_notifier_chain_unregister(&panic_notifier_list, 2099 &snp_panic_notifier); 2100 2101 /* Reset TMR size back to default */ 2102 sev_es_tmr_size = SEV_TMR_SIZE; 2103 2104 return ret; 2105 } 2106 2107 static int sev_ioctl_do_pek_import(struct sev_issue_cmd *argp, bool writable) 2108 { 2109 struct sev_device *sev = psp_master->sev_data; 2110 struct sev_user_data_pek_cert_import input; 2111 struct sev_data_pek_cert_import data; 2112 bool shutdown_required = false; 2113 void *pek_blob, *oca_blob; 2114 int ret; 2115 2116 if (!writable) 2117 return -EPERM; 2118 2119 if (copy_from_user(&input, (void __user *)argp->data, sizeof(input))) 2120 return -EFAULT; 2121 2122 /* copy PEK certificate blobs from userspace */ 2123 pek_blob = psp_copy_user_blob(input.pek_cert_address, input.pek_cert_len); 2124 if (IS_ERR(pek_blob)) 2125 return PTR_ERR(pek_blob); 2126 2127 data.reserved = 0; 2128 data.pek_cert_address = __psp_pa(pek_blob); 2129 data.pek_cert_len = input.pek_cert_len; 2130 2131 /* copy PEK certificate blobs from userspace */ 2132 oca_blob = psp_copy_user_blob(input.oca_cert_address, input.oca_cert_len); 2133 if (IS_ERR(oca_blob)) { 2134 ret = PTR_ERR(oca_blob); 2135 goto e_free_pek; 2136 } 2137 2138 data.oca_cert_address = __psp_pa(oca_blob); 2139 data.oca_cert_len = input.oca_cert_len; 2140 2141 /* If platform is not in INIT state then transition it to INIT */ 2142 if (sev->sev_plat_status.state != SEV_STATE_INIT) { 2143 ret = sev_move_to_init_state(argp, &shutdown_required); 2144 if (ret) 2145 goto e_free_oca; 2146 } 2147 2148 ret = __sev_do_cmd_locked(SEV_CMD_PEK_CERT_IMPORT, &data, &argp->error); 2149 2150 e_free_oca: 2151 if (shutdown_required) 2152 __sev_firmware_shutdown(sev, false); 2153 2154 kfree(oca_blob); 2155 e_free_pek: 2156 kfree(pek_blob); 2157 return ret; 2158 } 2159 2160 static int sev_ioctl_do_get_id2(struct sev_issue_cmd *argp) 2161 { 2162 struct sev_user_data_get_id2 input; 2163 struct sev_data_get_id data; 2164 void __user *input_address; 2165 void *id_blob = NULL; 2166 int ret; 2167 2168 /* SEV GET_ID is available from SEV API v0.16 and up */ 2169 if (!sev_version_greater_or_equal(0, 16)) 2170 return -ENOTSUPP; 2171 2172 if (copy_from_user(&input, (void __user *)argp->data, sizeof(input))) 2173 return -EFAULT; 2174 2175 input_address = (void __user *)input.address; 2176 2177 if (input.address && input.length) { 2178 /* 2179 * The length of the ID shouldn't be assumed by software since 2180 * it may change in the future. The allocation size is limited 2181 * to 1 << (PAGE_SHIFT + MAX_PAGE_ORDER) by the page allocator. 2182 * If the allocation fails, simply return ENOMEM rather than 2183 * warning in the kernel log. 2184 */ 2185 id_blob = kzalloc(input.length, GFP_KERNEL | __GFP_NOWARN); 2186 if (!id_blob) 2187 return -ENOMEM; 2188 2189 data.address = __psp_pa(id_blob); 2190 data.len = input.length; 2191 } else { 2192 data.address = 0; 2193 data.len = 0; 2194 } 2195 2196 ret = __sev_do_cmd_locked(SEV_CMD_GET_ID, &data, &argp->error); 2197 2198 /* 2199 * Firmware will return the length of the ID value (either the minimum 2200 * required length or the actual length written), return it to the user. 2201 */ 2202 input.length = data.len; 2203 2204 if (copy_to_user((void __user *)argp->data, &input, sizeof(input))) { 2205 ret = -EFAULT; 2206 goto e_free; 2207 } 2208 2209 if (ret || WARN_ON_ONCE(argp->error)) 2210 goto e_free; 2211 2212 if (id_blob) { 2213 if (copy_to_user(input_address, id_blob, data.len)) { 2214 ret = -EFAULT; 2215 goto e_free; 2216 } 2217 } 2218 2219 e_free: 2220 kfree(id_blob); 2221 2222 return ret; 2223 } 2224 2225 static int sev_ioctl_do_get_id(struct sev_issue_cmd *argp) 2226 { 2227 struct sev_data_get_id *data; 2228 u64 data_size, user_size; 2229 void *id_blob, *mem; 2230 int ret; 2231 2232 /* SEV GET_ID available from SEV API v0.16 and up */ 2233 if (!sev_version_greater_or_equal(0, 16)) 2234 return -ENOTSUPP; 2235 2236 /* SEV FW expects the buffer it fills with the ID to be 2237 * 8-byte aligned. Memory allocated should be enough to 2238 * hold data structure + alignment padding + memory 2239 * where SEV FW writes the ID. 2240 */ 2241 data_size = ALIGN(sizeof(struct sev_data_get_id), 8); 2242 user_size = sizeof(struct sev_user_data_get_id); 2243 2244 mem = kzalloc(data_size + user_size, GFP_KERNEL); 2245 if (!mem) 2246 return -ENOMEM; 2247 2248 data = mem; 2249 id_blob = mem + data_size; 2250 2251 data->address = __psp_pa(id_blob); 2252 data->len = user_size; 2253 2254 ret = __sev_do_cmd_locked(SEV_CMD_GET_ID, data, &argp->error); 2255 if (!ret) { 2256 if (copy_to_user((void __user *)argp->data, id_blob, data->len)) 2257 ret = -EFAULT; 2258 } 2259 2260 kfree(mem); 2261 2262 return ret; 2263 } 2264 2265 static int sev_ioctl_do_pdh_export(struct sev_issue_cmd *argp, bool writable) 2266 { 2267 struct sev_device *sev = psp_master->sev_data; 2268 struct sev_user_data_pdh_cert_export input; 2269 void *pdh_blob = NULL, *cert_blob = NULL; 2270 struct sev_data_pdh_cert_export data; 2271 void __user *input_cert_chain_address; 2272 void __user *input_pdh_cert_address; 2273 bool shutdown_required = false; 2274 int ret; 2275 2276 if (copy_from_user(&input, (void __user *)argp->data, sizeof(input))) 2277 return -EFAULT; 2278 2279 memset(&data, 0, sizeof(data)); 2280 2281 input_pdh_cert_address = (void __user *)input.pdh_cert_address; 2282 input_cert_chain_address = (void __user *)input.cert_chain_address; 2283 2284 /* Userspace wants to query the certificate length. */ 2285 if (!input.pdh_cert_address || 2286 !input.pdh_cert_len || 2287 !input.cert_chain_address || 2288 !input.cert_chain_len) 2289 goto cmd; 2290 2291 /* Allocate a physically contiguous buffer to store the PDH blob. */ 2292 if (input.pdh_cert_len > SEV_FW_BLOB_MAX_SIZE) 2293 return -EFAULT; 2294 2295 /* Allocate a physically contiguous buffer to store the cert chain blob. */ 2296 if (input.cert_chain_len > SEV_FW_BLOB_MAX_SIZE) 2297 return -EFAULT; 2298 2299 pdh_blob = kzalloc(input.pdh_cert_len, GFP_KERNEL); 2300 if (!pdh_blob) 2301 return -ENOMEM; 2302 2303 data.pdh_cert_address = __psp_pa(pdh_blob); 2304 data.pdh_cert_len = input.pdh_cert_len; 2305 2306 cert_blob = kzalloc(input.cert_chain_len, GFP_KERNEL); 2307 if (!cert_blob) { 2308 ret = -ENOMEM; 2309 goto e_free_pdh; 2310 } 2311 2312 data.cert_chain_address = __psp_pa(cert_blob); 2313 data.cert_chain_len = input.cert_chain_len; 2314 2315 cmd: 2316 /* If platform is not in INIT state then transition it to INIT. */ 2317 if (sev->sev_plat_status.state != SEV_STATE_INIT) { 2318 if (!writable) { 2319 ret = -EPERM; 2320 goto e_free_cert; 2321 } 2322 ret = sev_move_to_init_state(argp, &shutdown_required); 2323 if (ret) 2324 goto e_free_cert; 2325 } 2326 2327 ret = __sev_do_cmd_locked(SEV_CMD_PDH_CERT_EXPORT, &data, &argp->error); 2328 2329 /* 2330 * Firmware will return the length of the blobs (either the minimum 2331 * required length or the actual length written), return 'em to the user. 2332 */ 2333 input.cert_chain_len = data.cert_chain_len; 2334 input.pdh_cert_len = data.pdh_cert_len; 2335 2336 if (copy_to_user((void __user *)argp->data, &input, sizeof(input))) { 2337 ret = -EFAULT; 2338 goto e_free_cert; 2339 } 2340 2341 if (ret || WARN_ON_ONCE(argp->error)) 2342 goto e_free_cert; 2343 2344 if (pdh_blob) { 2345 if (copy_to_user(input_pdh_cert_address, 2346 pdh_blob, input.pdh_cert_len)) { 2347 ret = -EFAULT; 2348 goto e_free_cert; 2349 } 2350 } 2351 2352 if (cert_blob) { 2353 if (copy_to_user(input_cert_chain_address, 2354 cert_blob, input.cert_chain_len)) 2355 ret = -EFAULT; 2356 } 2357 2358 e_free_cert: 2359 if (shutdown_required) 2360 __sev_firmware_shutdown(sev, false); 2361 2362 kfree(cert_blob); 2363 e_free_pdh: 2364 kfree(pdh_blob); 2365 return ret; 2366 } 2367 2368 static int __sev_do_snp_platform_status(struct sev_user_data_snp_status *status, 2369 int *error) 2370 { 2371 struct sev_device *sev = psp_master->sev_data; 2372 struct sev_data_snp_addr buf; 2373 struct page *status_page; 2374 void *data; 2375 int ret; 2376 2377 status_page = alloc_page(GFP_KERNEL_ACCOUNT); 2378 if (!status_page) 2379 return -ENOMEM; 2380 2381 data = page_address(status_page); 2382 2383 /* 2384 * SNP_PLATFORM_STATUS can be executed in any SNP state. But if executed 2385 * when SNP has been initialized, the status page must be firmware-owned. 2386 */ 2387 if (sev->snp_initialized) { 2388 /* 2389 * Firmware expects the status page to be in Firmware state, 2390 * otherwise it will report an error INVALID_PAGE_STATE. 2391 */ 2392 if (rmp_mark_pages_firmware(__pa(data), 1, true)) { 2393 ret = -EFAULT; 2394 goto cleanup; 2395 } 2396 } 2397 2398 buf.address = __psp_pa(data); 2399 ret = __sev_do_cmd_locked(SEV_CMD_SNP_PLATFORM_STATUS, &buf, error); 2400 2401 if (sev->snp_initialized) { 2402 /* 2403 * The status page will be in Reclaim state on success, or left 2404 * in Firmware state on failure. Use snp_reclaim_pages() to 2405 * transition either case back to Hypervisor-owned state. 2406 */ 2407 if (snp_reclaim_pages(__pa(data), 1, true)) 2408 return -EFAULT; 2409 } 2410 2411 if (ret) 2412 goto cleanup; 2413 2414 memcpy(status, data, sizeof(*status)); 2415 2416 cleanup: 2417 __free_pages(status_page, 0); 2418 return ret; 2419 } 2420 2421 static int sev_ioctl_do_snp_platform_status(struct sev_issue_cmd *argp) 2422 { 2423 struct sev_user_data_snp_status status; 2424 int ret; 2425 2426 if (!argp->data) 2427 return -EINVAL; 2428 2429 ret = __sev_do_snp_platform_status(&status, &argp->error); 2430 if (ret < 0) 2431 return ret; 2432 2433 if (copy_to_user((void __user *)argp->data, &status, 2434 sizeof(struct sev_user_data_snp_status))) 2435 ret = -EFAULT; 2436 2437 return ret; 2438 } 2439 2440 static int sev_ioctl_do_snp_commit(struct sev_issue_cmd *argp) 2441 { 2442 struct sev_data_snp_commit buf; 2443 int ret; 2444 2445 buf.len = sizeof(buf); 2446 2447 ret = __sev_do_cmd_locked(SEV_CMD_SNP_COMMIT, &buf, &argp->error); 2448 2449 return ret; 2450 } 2451 2452 static int sev_ioctl_do_snp_set_config(struct sev_issue_cmd *argp, bool writable) 2453 { 2454 struct sev_device *sev = psp_master->sev_data; 2455 struct sev_user_data_snp_config config; 2456 2457 if (!argp->data) 2458 return -EINVAL; 2459 2460 if (!writable) 2461 return -EPERM; 2462 2463 if (!sev->snp_initialized) 2464 return -ENODEV; 2465 2466 if (copy_from_user(&config, (void __user *)argp->data, sizeof(config))) 2467 return -EFAULT; 2468 2469 return __sev_do_cmd_locked(SEV_CMD_SNP_CONFIG, &config, &argp->error); 2470 } 2471 2472 static int sev_ioctl_do_snp_vlek_load(struct sev_issue_cmd *argp, bool writable) 2473 { 2474 struct sev_device *sev = psp_master->sev_data; 2475 struct sev_user_data_snp_vlek_load input; 2476 void *blob; 2477 int ret; 2478 2479 if (!argp->data) 2480 return -EINVAL; 2481 2482 if (!writable) 2483 return -EPERM; 2484 2485 if (!sev->snp_initialized) 2486 return -ENODEV; 2487 2488 if (copy_from_user(&input, u64_to_user_ptr(argp->data), sizeof(input))) 2489 return -EFAULT; 2490 2491 if (input.len != sizeof(input) || input.vlek_wrapped_version != 0) 2492 return -EINVAL; 2493 2494 blob = psp_copy_user_blob(input.vlek_wrapped_address, 2495 sizeof(struct sev_user_data_snp_wrapped_vlek_hashstick)); 2496 if (IS_ERR(blob)) 2497 return PTR_ERR(blob); 2498 2499 input.vlek_wrapped_address = __psp_pa(blob); 2500 2501 ret = __sev_do_cmd_locked(SEV_CMD_SNP_VLEK_LOAD, &input, &argp->error); 2502 kfree(blob); 2503 2504 return ret; 2505 } 2506 2507 static long sev_ioctl(struct file *file, unsigned int ioctl, unsigned long arg) 2508 { 2509 void __user *argp = (void __user *)arg; 2510 struct sev_issue_cmd input; 2511 int ret = -EFAULT; 2512 bool writable = file->f_mode & FMODE_WRITE; 2513 2514 if (!psp_master || !psp_master->sev_data) 2515 return -ENODEV; 2516 2517 if (ioctl != SEV_ISSUE_CMD) 2518 return -EINVAL; 2519 2520 if (copy_from_user(&input, argp, sizeof(struct sev_issue_cmd))) 2521 return -EFAULT; 2522 2523 if (input.cmd > SEV_MAX) 2524 return -EINVAL; 2525 2526 mutex_lock(&sev_cmd_mutex); 2527 2528 switch (input.cmd) { 2529 2530 case SEV_FACTORY_RESET: 2531 ret = sev_ioctl_do_reset(&input, writable); 2532 break; 2533 case SEV_PLATFORM_STATUS: 2534 ret = sev_ioctl_do_platform_status(&input); 2535 break; 2536 case SEV_PEK_GEN: 2537 ret = sev_ioctl_do_pek_pdh_gen(SEV_CMD_PEK_GEN, &input, writable); 2538 break; 2539 case SEV_PDH_GEN: 2540 ret = sev_ioctl_do_pek_pdh_gen(SEV_CMD_PDH_GEN, &input, writable); 2541 break; 2542 case SEV_PEK_CSR: 2543 ret = sev_ioctl_do_pek_csr(&input, writable); 2544 break; 2545 case SEV_PEK_CERT_IMPORT: 2546 ret = sev_ioctl_do_pek_import(&input, writable); 2547 break; 2548 case SEV_PDH_CERT_EXPORT: 2549 ret = sev_ioctl_do_pdh_export(&input, writable); 2550 break; 2551 case SEV_GET_ID: 2552 pr_warn_once("SEV_GET_ID command is deprecated, use SEV_GET_ID2\n"); 2553 ret = sev_ioctl_do_get_id(&input); 2554 break; 2555 case SEV_GET_ID2: 2556 ret = sev_ioctl_do_get_id2(&input); 2557 break; 2558 case SNP_PLATFORM_STATUS: 2559 ret = sev_ioctl_do_snp_platform_status(&input); 2560 break; 2561 case SNP_COMMIT: 2562 ret = sev_ioctl_do_snp_commit(&input); 2563 break; 2564 case SNP_SET_CONFIG: 2565 ret = sev_ioctl_do_snp_set_config(&input, writable); 2566 break; 2567 case SNP_VLEK_LOAD: 2568 ret = sev_ioctl_do_snp_vlek_load(&input, writable); 2569 break; 2570 default: 2571 ret = -EINVAL; 2572 goto out; 2573 } 2574 2575 if (copy_to_user(argp, &input, sizeof(struct sev_issue_cmd))) 2576 ret = -EFAULT; 2577 out: 2578 mutex_unlock(&sev_cmd_mutex); 2579 2580 return ret; 2581 } 2582 2583 static const struct file_operations sev_fops = { 2584 .owner = THIS_MODULE, 2585 .unlocked_ioctl = sev_ioctl, 2586 }; 2587 2588 int sev_platform_status(struct sev_user_data_status *data, int *error) 2589 { 2590 return sev_do_cmd(SEV_CMD_PLATFORM_STATUS, data, error); 2591 } 2592 EXPORT_SYMBOL_GPL(sev_platform_status); 2593 2594 int sev_guest_deactivate(struct sev_data_deactivate *data, int *error) 2595 { 2596 return sev_do_cmd(SEV_CMD_DEACTIVATE, data, error); 2597 } 2598 EXPORT_SYMBOL_GPL(sev_guest_deactivate); 2599 2600 int sev_guest_activate(struct sev_data_activate *data, int *error) 2601 { 2602 return sev_do_cmd(SEV_CMD_ACTIVATE, data, error); 2603 } 2604 EXPORT_SYMBOL_GPL(sev_guest_activate); 2605 2606 int sev_guest_decommission(struct sev_data_decommission *data, int *error) 2607 { 2608 return sev_do_cmd(SEV_CMD_DECOMMISSION, data, error); 2609 } 2610 EXPORT_SYMBOL_GPL(sev_guest_decommission); 2611 2612 int sev_guest_df_flush(int *error) 2613 { 2614 return sev_do_cmd(SEV_CMD_DF_FLUSH, NULL, error); 2615 } 2616 EXPORT_SYMBOL_GPL(sev_guest_df_flush); 2617 2618 static void sev_exit(struct kref *ref) 2619 { 2620 misc_deregister(&misc_dev->misc); 2621 kfree(misc_dev); 2622 misc_dev = NULL; 2623 } 2624 2625 static int sev_misc_init(struct sev_device *sev) 2626 { 2627 struct device *dev = sev->dev; 2628 int ret; 2629 2630 /* 2631 * SEV feature support can be detected on multiple devices but the SEV 2632 * FW commands must be issued on the master. During probe, we do not 2633 * know the master hence we create /dev/sev on the first device probe. 2634 * sev_do_cmd() finds the right master device to which to issue the 2635 * command to the firmware. 2636 */ 2637 if (!misc_dev) { 2638 struct miscdevice *misc; 2639 2640 misc_dev = kzalloc_obj(*misc_dev); 2641 if (!misc_dev) 2642 return -ENOMEM; 2643 2644 misc = &misc_dev->misc; 2645 misc->minor = MISC_DYNAMIC_MINOR; 2646 misc->name = DEVICE_NAME; 2647 misc->fops = &sev_fops; 2648 2649 ret = misc_register(misc); 2650 if (ret) 2651 return ret; 2652 2653 kref_init(&misc_dev->refcount); 2654 } else { 2655 kref_get(&misc_dev->refcount); 2656 } 2657 2658 init_waitqueue_head(&sev->int_queue); 2659 sev->misc = misc_dev; 2660 dev_dbg(dev, "registered SEV device\n"); 2661 2662 return 0; 2663 } 2664 2665 int sev_dev_init(struct psp_device *psp) 2666 { 2667 struct device *dev = psp->dev; 2668 struct sev_device *sev; 2669 int ret = -ENOMEM; 2670 2671 if (!boot_cpu_has(X86_FEATURE_SEV)) { 2672 dev_info_once(dev, "SEV: memory encryption not enabled by BIOS\n"); 2673 return 0; 2674 } 2675 2676 sev = devm_kzalloc(dev, sizeof(*sev), GFP_KERNEL); 2677 if (!sev) 2678 goto e_err; 2679 2680 sev->cmd_buf = (void *)devm_get_free_pages(dev, GFP_KERNEL, 1); 2681 if (!sev->cmd_buf) 2682 goto e_sev; 2683 2684 sev->cmd_buf_backup = (uint8_t *)sev->cmd_buf + PAGE_SIZE; 2685 2686 psp->sev_data = sev; 2687 2688 sev->dev = dev; 2689 sev->psp = psp; 2690 2691 sev->io_regs = psp->io_regs; 2692 2693 sev->vdata = (struct sev_vdata *)psp->vdata->sev; 2694 if (!sev->vdata) { 2695 ret = -ENODEV; 2696 dev_err(dev, "sev: missing driver data\n"); 2697 goto e_buf; 2698 } 2699 2700 psp_set_sev_irq_handler(psp, sev_irq_handler, sev); 2701 2702 ret = sev_misc_init(sev); 2703 if (ret) 2704 goto e_irq; 2705 2706 dev_notice(dev, "sev enabled\n"); 2707 2708 return 0; 2709 2710 e_irq: 2711 psp_clear_sev_irq_handler(psp); 2712 e_buf: 2713 devm_free_pages(dev, (unsigned long)sev->cmd_buf); 2714 e_sev: 2715 devm_kfree(dev, sev); 2716 e_err: 2717 psp->sev_data = NULL; 2718 2719 dev_notice(dev, "sev initialization failed\n"); 2720 2721 return ret; 2722 } 2723 2724 static void __sev_firmware_shutdown(struct sev_device *sev, bool panic) 2725 { 2726 int error; 2727 2728 __sev_platform_shutdown_locked(&error); 2729 2730 if (sev_es_tmr) { 2731 /* 2732 * The TMR area was encrypted, flush it from the cache. 2733 * 2734 * If invoked during panic handling, local interrupts are 2735 * disabled and all CPUs are stopped, so wbinvd_on_all_cpus() 2736 * can't be used. In that case, wbinvd() is done on remote CPUs 2737 * via the NMI callback, and done for this CPU later during 2738 * SNP shutdown, so wbinvd_on_all_cpus() can be skipped. 2739 */ 2740 if (!panic) 2741 wbinvd_on_all_cpus(); 2742 2743 __snp_free_firmware_pages(virt_to_page(sev_es_tmr), 2744 get_order(sev_es_tmr_size), 2745 true); 2746 sev_es_tmr = NULL; 2747 } 2748 2749 if (sev_init_ex_buffer) { 2750 __snp_free_firmware_pages(virt_to_page(sev_init_ex_buffer), 2751 get_order(NV_LENGTH), 2752 true); 2753 sev_init_ex_buffer = NULL; 2754 } 2755 2756 __sev_snp_shutdown_locked(&error, panic); 2757 } 2758 2759 static void sev_firmware_shutdown(struct sev_device *sev) 2760 { 2761 /* 2762 * Calling without sev_cmd_mutex held as TSM will likely try disconnecting 2763 * IDE and this ends up calling sev_do_cmd() which locks sev_cmd_mutex. 2764 */ 2765 if (sev->tio_status) 2766 sev_tsm_uninit(sev); 2767 2768 mutex_lock(&sev_cmd_mutex); 2769 2770 __sev_firmware_shutdown(sev, false); 2771 2772 kfree(sev->tio_status); 2773 sev->tio_status = NULL; 2774 2775 mutex_unlock(&sev_cmd_mutex); 2776 } 2777 2778 void sev_platform_shutdown(void) 2779 { 2780 if (!psp_master || !psp_master->sev_data) 2781 return; 2782 2783 sev_firmware_shutdown(psp_master->sev_data); 2784 } 2785 EXPORT_SYMBOL_GPL(sev_platform_shutdown); 2786 2787 u64 sev_get_snp_policy_bits(void) 2788 { 2789 struct psp_device *psp = psp_master; 2790 struct sev_device *sev; 2791 u64 policy_bits; 2792 2793 if (!cc_platform_has(CC_ATTR_HOST_SEV_SNP)) 2794 return 0; 2795 2796 if (!psp || !psp->sev_data) 2797 return 0; 2798 2799 sev = psp->sev_data; 2800 2801 policy_bits = SNP_POLICY_MASK_BASE; 2802 2803 if (sev->snp_plat_status.feature_info) { 2804 if (sev->snp_feat_info_0.ecx & SNP_RAPL_DISABLE_SUPPORTED) 2805 policy_bits |= SNP_POLICY_MASK_RAPL_DIS; 2806 2807 if (sev->snp_feat_info_0.ecx & SNP_CIPHER_TEXT_HIDING_SUPPORTED) 2808 policy_bits |= SNP_POLICY_MASK_CIPHERTEXT_HIDING_DRAM; 2809 2810 if (sev->snp_feat_info_0.ecx & SNP_AES_256_XTS_POLICY_SUPPORTED) 2811 policy_bits |= SNP_POLICY_MASK_MEM_AES_256_XTS; 2812 2813 if (sev->snp_feat_info_0.ecx & SNP_CXL_ALLOW_POLICY_SUPPORTED) 2814 policy_bits |= SNP_POLICY_MASK_CXL_ALLOW; 2815 2816 if (sev_version_greater_or_equal(1, 58)) 2817 policy_bits |= SNP_POLICY_MASK_PAGE_SWAP_DISABLE; 2818 } 2819 2820 return policy_bits; 2821 } 2822 EXPORT_SYMBOL_GPL(sev_get_snp_policy_bits); 2823 2824 void sev_dev_destroy(struct psp_device *psp) 2825 { 2826 struct sev_device *sev = psp->sev_data; 2827 2828 if (!sev) 2829 return; 2830 2831 sev_firmware_shutdown(sev); 2832 2833 if (sev->misc) 2834 kref_put(&misc_dev->refcount, sev_exit); 2835 2836 psp_clear_sev_irq_handler(psp); 2837 } 2838 2839 static int snp_shutdown_on_panic(struct notifier_block *nb, 2840 unsigned long reason, void *arg) 2841 { 2842 struct sev_device *sev = psp_master->sev_data; 2843 2844 /* 2845 * If sev_cmd_mutex is already acquired, then it's likely 2846 * another PSP command is in flight and issuing a shutdown 2847 * would fail in unexpected ways. Rather than create even 2848 * more confusion during a panic, just bail out here. 2849 */ 2850 if (mutex_is_locked(&sev_cmd_mutex)) 2851 return NOTIFY_DONE; 2852 2853 __sev_firmware_shutdown(sev, true); 2854 2855 return NOTIFY_DONE; 2856 } 2857 2858 int sev_issue_cmd_external_user(struct file *filep, unsigned int cmd, 2859 void *data, int *error) 2860 { 2861 if (!filep || filep->f_op != &sev_fops) 2862 return -EBADF; 2863 2864 return sev_do_cmd(cmd, data, error); 2865 } 2866 EXPORT_SYMBOL_GPL(sev_issue_cmd_external_user); 2867 2868 void sev_pci_init(void) 2869 { 2870 struct sev_device *sev = psp_master->sev_data; 2871 u8 api_major, api_minor, build; 2872 2873 if (!sev) 2874 return; 2875 2876 psp_timeout = psp_probe_timeout; 2877 2878 if (sev_get_api_version()) 2879 goto err; 2880 2881 api_major = sev->api_major; 2882 api_minor = sev->api_minor; 2883 build = sev->build; 2884 2885 if (sev_update_firmware(sev->dev) == 0) 2886 sev_get_api_version(); 2887 2888 if (api_major != sev->api_major || api_minor != sev->api_minor || 2889 build != sev->build) 2890 dev_info(sev->dev, "SEV firmware updated from %d.%d.%d to %d.%d.%d\n", 2891 api_major, api_minor, build, 2892 sev->api_major, sev->api_minor, sev->build); 2893 2894 return; 2895 2896 err: 2897 sev_dev_destroy(psp_master); 2898 2899 psp_master->sev_data = NULL; 2900 } 2901 2902 void sev_pci_exit(void) 2903 { 2904 struct sev_device *sev = psp_master->sev_data; 2905 2906 if (!sev) 2907 return; 2908 2909 sev_firmware_shutdown(sev); 2910 } 2911 2912 static int get_v1_svn(struct sev_device *sev) 2913 { 2914 struct sev_snp_tcb_version_genoa_milan *tcb; 2915 struct sev_user_data_snp_status status; 2916 int ret, error = 0; 2917 2918 mutex_lock(&sev_cmd_mutex); 2919 ret = __sev_do_snp_platform_status(&status, &error); 2920 mutex_unlock(&sev_cmd_mutex); 2921 if (ret < 0) 2922 return ret; 2923 2924 tcb = (struct sev_snp_tcb_version_genoa_milan *)&status 2925 .current_tcb_version; 2926 return tcb->snp; 2927 } 2928 2929 static int get_v2_svn(struct sev_device *sev) 2930 { 2931 struct sev_user_data_snp_status status; 2932 struct sev_snp_tcb_version_turin *tcb; 2933 int ret, error = 0; 2934 2935 mutex_lock(&sev_cmd_mutex); 2936 ret = __sev_do_snp_platform_status(&status, &error); 2937 mutex_unlock(&sev_cmd_mutex); 2938 if (ret < 0) 2939 return ret; 2940 2941 tcb = (struct sev_snp_tcb_version_turin *)&status 2942 .current_tcb_version; 2943 return tcb->snp; 2944 } 2945 2946 static bool sev_firmware_allows_es(struct sev_device *sev) 2947 { 2948 /* Documented in AMD-SB-3023 */ 2949 if (boot_cpu_has(X86_FEATURE_ZEN4) || boot_cpu_has(X86_FEATURE_ZEN3)) 2950 return get_v1_svn(sev) < 0x1b; 2951 else if (boot_cpu_has(X86_FEATURE_ZEN5)) 2952 return get_v2_svn(sev) < 0x4; 2953 else 2954 return true; 2955 } 2956 2957 int sev_firmware_supported_vm_types(void) 2958 { 2959 int supported_vm_types = 0; 2960 struct sev_device *sev; 2961 2962 if (!psp_master || !psp_master->sev_data) 2963 return supported_vm_types; 2964 sev = psp_master->sev_data; 2965 2966 supported_vm_types |= BIT(KVM_X86_SEV_VM); 2967 supported_vm_types |= BIT(KVM_X86_SEV_ES_VM); 2968 2969 if (!sev->snp_initialized) 2970 return supported_vm_types; 2971 2972 supported_vm_types |= BIT(KVM_X86_SNP_VM); 2973 2974 if (!sev_firmware_allows_es(sev)) 2975 supported_vm_types &= ~BIT(KVM_X86_SEV_ES_VM); 2976 2977 return supported_vm_types; 2978 2979 } 2980 EXPORT_SYMBOL_FOR_MODULES(sev_firmware_supported_vm_types, "kvm-amd"); 2981