1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * runtime-wrappers.c - Runtime Services function call wrappers 4 * 5 * Implementation summary: 6 * ----------------------- 7 * 1. When user/kernel thread requests to execute efi_runtime_service(), 8 * enqueue work to efi_rts_wq. 9 * 2. Caller thread waits for completion until the work is finished 10 * because it's dependent on the return status and execution of 11 * efi_runtime_service(). 12 * For instance, get_variable() and get_next_variable(). 13 * 14 * Copyright (C) 2014 Linaro Ltd. <ard.biesheuvel@linaro.org> 15 * 16 * Split off from arch/x86/platform/efi/efi.c 17 * 18 * Copyright (C) 1999 VA Linux Systems 19 * Copyright (C) 1999 Walt Drummond <drummond@valinux.com> 20 * Copyright (C) 1999-2002 Hewlett-Packard Co. 21 * Copyright (C) 2005-2008 Intel Co. 22 * Copyright (C) 2013 SuSE Labs 23 */ 24 25 #define pr_fmt(fmt) "efi: " fmt 26 27 #include <linux/bug.h> 28 #include <linux/efi.h> 29 #include <linux/irqflags.h> 30 #include <linux/mutex.h> 31 #include <linux/semaphore.h> 32 #include <linux/stringify.h> 33 #include <linux/workqueue.h> 34 #include <linux/completion.h> 35 36 #include <asm/efi.h> 37 38 /* 39 * Wrap around the new efi_call_virt_generic() macros so that the 40 * code doesn't get too cluttered: 41 */ 42 #define efi_call_virt(f, args...) \ 43 arch_efi_call_virt(efi.runtime, f, args) 44 45 union efi_rts_args { 46 struct { 47 efi_time_t *time; 48 efi_time_cap_t *capabilities; 49 } GET_TIME; 50 51 struct { 52 efi_time_t *time; 53 } SET_TIME; 54 55 struct { 56 efi_bool_t *enabled; 57 efi_bool_t *pending; 58 efi_time_t *time; 59 } GET_WAKEUP_TIME; 60 61 struct { 62 efi_bool_t enable; 63 efi_time_t *time; 64 } SET_WAKEUP_TIME; 65 66 struct { 67 efi_char16_t *name; 68 efi_guid_t *vendor; 69 u32 *attr; 70 unsigned long *data_size; 71 void *data; 72 } GET_VARIABLE; 73 74 struct { 75 unsigned long *name_size; 76 efi_char16_t *name; 77 efi_guid_t *vendor; 78 } GET_NEXT_VARIABLE; 79 80 struct { 81 efi_char16_t *name; 82 efi_guid_t *vendor; 83 u32 attr; 84 unsigned long data_size; 85 void *data; 86 } SET_VARIABLE; 87 88 struct { 89 u32 attr; 90 u64 *storage_space; 91 u64 *remaining_space; 92 u64 *max_variable_size; 93 } QUERY_VARIABLE_INFO; 94 95 struct { 96 u32 *high_count; 97 } GET_NEXT_HIGH_MONO_COUNT; 98 99 struct { 100 efi_capsule_header_t **capsules; 101 unsigned long count; 102 unsigned long sg_list; 103 } UPDATE_CAPSULE; 104 105 struct { 106 efi_capsule_header_t **capsules; 107 unsigned long count; 108 u64 *max_size; 109 int *reset_type; 110 } QUERY_CAPSULE_CAPS; 111 112 struct { 113 efi_status_t (__efiapi *acpi_prm_handler)(u64, void *); 114 u64 param_buffer_addr; 115 void *context; 116 } ACPI_PRM_HANDLER; 117 }; 118 119 struct efi_runtime_work efi_rts_work; 120 121 /* 122 * Upper bound on how long we wait for a single EFI runtime service 123 * call to finish before declaring firmware wedged. Chosen to be longer 124 * than any plausible legitimate call (including UpdateCapsule on slow 125 * SPI-NOR) while still bounding userspace wait time. 126 */ 127 #define EFI_RTS_TIMEOUT (120 * HZ) 128 129 /* 130 * efi_queue_work: Queue EFI runtime service call and wait for completion 131 * @_rts: EFI runtime service function identifier 132 * @_args: Arguments to pass to the EFI runtime service 133 * 134 * Accesses to efi_runtime_services() are serialized by a binary 135 * semaphore (efi_runtime_lock) and caller waits until the work is 136 * finished, hence _only_ one work is queued at a time and the caller 137 * thread waits for completion. 138 */ 139 #define efi_queue_work(_rts, _args...) \ 140 __efi_queue_work(EFI_ ## _rts, \ 141 &(union efi_rts_args){ ._rts = { _args }}) 142 143 #ifndef arch_efi_save_flags 144 #define arch_efi_save_flags(state_flags) local_save_flags(state_flags) 145 #define arch_efi_restore_flags(state_flags) local_irq_restore(state_flags) 146 #endif 147 148 unsigned long efi_call_virt_save_flags(void) 149 { 150 unsigned long flags; 151 152 arch_efi_save_flags(flags); 153 return flags; 154 } 155 156 void efi_call_virt_check_flags(unsigned long flags, const void *caller) 157 { 158 unsigned long cur_flags, mismatch; 159 160 cur_flags = efi_call_virt_save_flags(); 161 162 mismatch = flags ^ cur_flags; 163 if (!WARN_ON_ONCE(mismatch & ARCH_EFI_IRQ_FLAGS_MASK)) 164 return; 165 166 add_taint(TAINT_FIRMWARE_WORKAROUND, LOCKDEP_NOW_UNRELIABLE); 167 pr_err_ratelimited(FW_BUG "IRQ flags corrupted (0x%08lx=>0x%08lx) by EFI call from %pS\n", 168 flags, cur_flags, caller ?: __builtin_return_address(0)); 169 arch_efi_restore_flags(flags); 170 } 171 172 /* 173 * According to section 7.1 of the UEFI spec, Runtime Services are not fully 174 * reentrant, and there are particular combinations of calls that need to be 175 * serialized. (source: UEFI Specification v2.4A) 176 * 177 * Table 31. Rules for Reentry Into Runtime Services 178 * +------------------------------------+-------------------------------+ 179 * | If previous call is busy in | Forbidden to call | 180 * +------------------------------------+-------------------------------+ 181 * | Any | SetVirtualAddressMap() | 182 * +------------------------------------+-------------------------------+ 183 * | ConvertPointer() | ConvertPointer() | 184 * +------------------------------------+-------------------------------+ 185 * | SetVariable() | ResetSystem() | 186 * | UpdateCapsule() | | 187 * | SetTime() | | 188 * | SetWakeupTime() | | 189 * | GetNextHighMonotonicCount() | | 190 * +------------------------------------+-------------------------------+ 191 * | GetVariable() | GetVariable() | 192 * | GetNextVariableName() | GetNextVariableName() | 193 * | SetVariable() | SetVariable() | 194 * | QueryVariableInfo() | QueryVariableInfo() | 195 * | UpdateCapsule() | UpdateCapsule() | 196 * | QueryCapsuleCapabilities() | QueryCapsuleCapabilities() | 197 * | GetNextHighMonotonicCount() | GetNextHighMonotonicCount() | 198 * +------------------------------------+-------------------------------+ 199 * | GetTime() | GetTime() | 200 * | SetTime() | SetTime() | 201 * | GetWakeupTime() | GetWakeupTime() | 202 * | SetWakeupTime() | SetWakeupTime() | 203 * +------------------------------------+-------------------------------+ 204 * 205 * Due to the fact that the EFI pstore may write to the variable store in 206 * interrupt context, we need to use a lock for at least the groups that 207 * contain SetVariable() and QueryVariableInfo(). That leaves little else, as 208 * none of the remaining functions are actually ever called at runtime. 209 * So let's just use a single lock to serialize all Runtime Services calls. 210 */ 211 static DEFINE_SEMAPHORE(efi_runtime_lock, 1); 212 213 static struct task_struct *efi_runtime_lock_owner __used; 214 215 /* 216 * Expose the EFI runtime lock to the UV platform 217 */ 218 #ifdef CONFIG_X86_UV 219 extern struct semaphore __efi_uv_runtime_lock __alias(efi_runtime_lock); 220 #endif 221 222 /* 223 * Park a worker that must never run efi_rts_wq again: EFI runtime services 224 * have been disabled and its efi_rts_work is abandoned. Loop in schedule() 225 * so a spurious wakeup cannot resume it. 226 */ 227 void __noreturn efi_rts_park_worker(void) 228 { 229 for (;;) { 230 set_current_state(TASK_IDLE); 231 schedule(); 232 } 233 } 234 235 /* 236 * Calls the appropriate efi_runtime_service() with the appropriate 237 * arguments. 238 */ 239 static void __nocfi efi_call_rts(struct work_struct *work) 240 { 241 const union efi_rts_args *args = efi_rts_work.args; 242 efi_status_t status = EFI_NOT_FOUND; 243 unsigned long flags; 244 245 if (!efi_enabled(EFI_RUNTIME_SERVICES)) 246 efi_rts_park_worker(); 247 248 efi_runtime_lock_owner = current; 249 250 arch_efi_call_virt_setup(); 251 flags = efi_call_virt_save_flags(); 252 253 switch (efi_rts_work.efi_rts_id) { 254 case EFI_GET_TIME: 255 status = efi_call_virt(get_time, 256 args->GET_TIME.time, 257 args->GET_TIME.capabilities); 258 break; 259 case EFI_SET_TIME: 260 status = efi_call_virt(set_time, 261 args->SET_TIME.time); 262 break; 263 case EFI_GET_WAKEUP_TIME: 264 status = efi_call_virt(get_wakeup_time, 265 args->GET_WAKEUP_TIME.enabled, 266 args->GET_WAKEUP_TIME.pending, 267 args->GET_WAKEUP_TIME.time); 268 break; 269 case EFI_SET_WAKEUP_TIME: 270 status = efi_call_virt(set_wakeup_time, 271 args->SET_WAKEUP_TIME.enable, 272 args->SET_WAKEUP_TIME.time); 273 break; 274 case EFI_GET_VARIABLE: 275 status = efi_call_virt(get_variable, 276 args->GET_VARIABLE.name, 277 args->GET_VARIABLE.vendor, 278 args->GET_VARIABLE.attr, 279 args->GET_VARIABLE.data_size, 280 args->GET_VARIABLE.data); 281 break; 282 case EFI_GET_NEXT_VARIABLE: 283 status = efi_call_virt(get_next_variable, 284 args->GET_NEXT_VARIABLE.name_size, 285 args->GET_NEXT_VARIABLE.name, 286 args->GET_NEXT_VARIABLE.vendor); 287 break; 288 case EFI_SET_VARIABLE: 289 status = efi_call_virt(set_variable, 290 args->SET_VARIABLE.name, 291 args->SET_VARIABLE.vendor, 292 args->SET_VARIABLE.attr, 293 args->SET_VARIABLE.data_size, 294 args->SET_VARIABLE.data); 295 break; 296 case EFI_QUERY_VARIABLE_INFO: 297 status = efi_call_virt(query_variable_info, 298 args->QUERY_VARIABLE_INFO.attr, 299 args->QUERY_VARIABLE_INFO.storage_space, 300 args->QUERY_VARIABLE_INFO.remaining_space, 301 args->QUERY_VARIABLE_INFO.max_variable_size); 302 break; 303 case EFI_GET_NEXT_HIGH_MONO_COUNT: 304 status = efi_call_virt(get_next_high_mono_count, 305 args->GET_NEXT_HIGH_MONO_COUNT.high_count); 306 break; 307 case EFI_UPDATE_CAPSULE: 308 status = efi_call_virt(update_capsule, 309 args->UPDATE_CAPSULE.capsules, 310 args->UPDATE_CAPSULE.count, 311 args->UPDATE_CAPSULE.sg_list); 312 break; 313 case EFI_QUERY_CAPSULE_CAPS: 314 status = efi_call_virt(query_capsule_caps, 315 args->QUERY_CAPSULE_CAPS.capsules, 316 args->QUERY_CAPSULE_CAPS.count, 317 args->QUERY_CAPSULE_CAPS.max_size, 318 args->QUERY_CAPSULE_CAPS.reset_type); 319 break; 320 case EFI_ACPI_PRM_HANDLER: 321 #ifdef CONFIG_ACPI_PRMT 322 status = arch_efi_call_virt(args, ACPI_PRM_HANDLER.acpi_prm_handler, 323 args->ACPI_PRM_HANDLER.param_buffer_addr, 324 args->ACPI_PRM_HANDLER.context); 325 break; 326 #endif 327 default: 328 /* 329 * Ideally, we should never reach here because a caller of this 330 * function should have put the right efi_runtime_service() 331 * function identifier into efi_rts_work->efi_rts_id 332 */ 333 pr_err("Requested executing invalid EFI Runtime Service.\n"); 334 } 335 336 efi_call_virt_check_flags(flags, efi_rts_work.caller); 337 arch_efi_call_virt_teardown(); 338 339 if (!efi_enabled(EFI_RUNTIME_SERVICES)) 340 efi_rts_park_worker(); 341 342 efi_rts_work.status = status; 343 complete(&efi_rts_work.efi_rts_comp); 344 efi_runtime_lock_owner = NULL; 345 } 346 347 static efi_status_t __efi_queue_work(enum efi_rts_ids id, 348 union efi_rts_args *args) 349 { 350 if (!efi_enabled(EFI_RUNTIME_SERVICES)) { 351 pr_warn_once("EFI Runtime Services are disabled!\n"); 352 return EFI_DEVICE_ERROR; 353 } 354 355 efi_rts_work.efi_rts_id = id; 356 efi_rts_work.args = args; 357 efi_rts_work.caller = __builtin_return_address(0); 358 efi_rts_work.status = EFI_ABORTED; 359 360 init_completion(&efi_rts_work.efi_rts_comp); 361 INIT_WORK(&efi_rts_work.work, efi_call_rts); 362 363 /* 364 * queue_work() returns 0 if work was already on queue, 365 * _ideally_ this should never happen. 366 */ 367 if (!queue_work(efi_rts_wq, &efi_rts_work.work)) { 368 pr_err("Failed to queue work to efi_rts_wq.\n"); 369 goto exit; 370 } 371 372 if (!wait_for_completion_timeout(&efi_rts_work.efi_rts_comp, 373 EFI_RTS_TIMEOUT)) { 374 pr_err("EFI runtime service %d wedged in firmware; disabling EFI runtime services\n", 375 id); 376 clear_bit(EFI_RUNTIME_SERVICES, &efi.flags); 377 return EFI_ABORTED; 378 } 379 380 WARN_ON_ONCE(efi_rts_work.status == EFI_ABORTED); 381 exit: 382 efi_rts_work.efi_rts_id = EFI_NONE; 383 return efi_rts_work.status; 384 } 385 386 static efi_status_t virt_efi_get_time(efi_time_t *tm, efi_time_cap_t *tc) 387 { 388 efi_status_t status; 389 390 if (down_interruptible(&efi_runtime_lock)) 391 return EFI_ABORTED; 392 status = efi_queue_work(GET_TIME, tm, tc); 393 up(&efi_runtime_lock); 394 return status; 395 } 396 397 static efi_status_t virt_efi_set_time(efi_time_t *tm) 398 { 399 efi_status_t status; 400 401 if (down_interruptible(&efi_runtime_lock)) 402 return EFI_ABORTED; 403 status = efi_queue_work(SET_TIME, tm); 404 up(&efi_runtime_lock); 405 return status; 406 } 407 408 static efi_status_t virt_efi_get_wakeup_time(efi_bool_t *enabled, 409 efi_bool_t *pending, 410 efi_time_t *tm) 411 { 412 efi_status_t status; 413 414 if (down_interruptible(&efi_runtime_lock)) 415 return EFI_ABORTED; 416 status = efi_queue_work(GET_WAKEUP_TIME, enabled, pending, tm); 417 up(&efi_runtime_lock); 418 return status; 419 } 420 421 static efi_status_t virt_efi_set_wakeup_time(efi_bool_t enabled, efi_time_t *tm) 422 { 423 efi_status_t status; 424 425 if (down_interruptible(&efi_runtime_lock)) 426 return EFI_ABORTED; 427 status = efi_queue_work(SET_WAKEUP_TIME, enabled, tm); 428 up(&efi_runtime_lock); 429 return status; 430 } 431 432 static efi_status_t virt_efi_get_variable(efi_char16_t *name, 433 efi_guid_t *vendor, 434 u32 *attr, 435 unsigned long *data_size, 436 void *data) 437 { 438 efi_status_t status; 439 440 if (down_interruptible(&efi_runtime_lock)) 441 return EFI_ABORTED; 442 status = efi_queue_work(GET_VARIABLE, name, vendor, attr, data_size, 443 data); 444 up(&efi_runtime_lock); 445 return status; 446 } 447 448 static efi_status_t virt_efi_get_next_variable(unsigned long *name_size, 449 efi_char16_t *name, 450 efi_guid_t *vendor) 451 { 452 efi_status_t status; 453 454 if (down_interruptible(&efi_runtime_lock)) 455 return EFI_ABORTED; 456 status = efi_queue_work(GET_NEXT_VARIABLE, name_size, name, vendor); 457 up(&efi_runtime_lock); 458 return status; 459 } 460 461 static efi_status_t virt_efi_set_variable(efi_char16_t *name, 462 efi_guid_t *vendor, 463 u32 attr, 464 unsigned long data_size, 465 void *data) 466 { 467 efi_status_t status; 468 469 if (down_interruptible(&efi_runtime_lock)) 470 return EFI_ABORTED; 471 status = efi_queue_work(SET_VARIABLE, name, vendor, attr, data_size, 472 data); 473 up(&efi_runtime_lock); 474 return status; 475 } 476 477 static efi_status_t __nocfi 478 virt_efi_set_variable_nb(efi_char16_t *name, efi_guid_t *vendor, u32 attr, 479 unsigned long data_size, void *data) 480 { 481 efi_status_t status; 482 483 if (down_trylock(&efi_runtime_lock)) 484 return EFI_NOT_READY; 485 486 if (!efi_enabled(EFI_RUNTIME_SERVICES)) { 487 up(&efi_runtime_lock); 488 return EFI_DEVICE_ERROR; 489 } 490 491 efi_runtime_lock_owner = current; 492 status = efi_call_virt_pointer(efi.runtime, set_variable, name, vendor, 493 attr, data_size, data); 494 efi_runtime_lock_owner = NULL; 495 up(&efi_runtime_lock); 496 return status; 497 } 498 499 500 static efi_status_t virt_efi_query_variable_info(u32 attr, 501 u64 *storage_space, 502 u64 *remaining_space, 503 u64 *max_variable_size) 504 { 505 efi_status_t status; 506 507 if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION) 508 return EFI_UNSUPPORTED; 509 510 if (down_interruptible(&efi_runtime_lock)) 511 return EFI_ABORTED; 512 status = efi_queue_work(QUERY_VARIABLE_INFO, attr, storage_space, 513 remaining_space, max_variable_size); 514 up(&efi_runtime_lock); 515 return status; 516 } 517 518 static efi_status_t __nocfi 519 virt_efi_query_variable_info_nb(u32 attr, u64 *storage_space, 520 u64 *remaining_space, u64 *max_variable_size) 521 { 522 efi_status_t status; 523 524 if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION) 525 return EFI_UNSUPPORTED; 526 527 if (down_trylock(&efi_runtime_lock)) 528 return EFI_NOT_READY; 529 530 if (!efi_enabled(EFI_RUNTIME_SERVICES)) { 531 up(&efi_runtime_lock); 532 return EFI_DEVICE_ERROR; 533 } 534 535 efi_runtime_lock_owner = current; 536 status = efi_call_virt_pointer(efi.runtime, query_variable_info, attr, 537 storage_space, remaining_space, 538 max_variable_size); 539 efi_runtime_lock_owner = NULL; 540 up(&efi_runtime_lock); 541 return status; 542 } 543 544 static efi_status_t virt_efi_get_next_high_mono_count(u32 *count) 545 { 546 efi_status_t status; 547 548 if (down_interruptible(&efi_runtime_lock)) 549 return EFI_ABORTED; 550 status = efi_queue_work(GET_NEXT_HIGH_MONO_COUNT, count); 551 up(&efi_runtime_lock); 552 return status; 553 } 554 555 static void __nocfi 556 virt_efi_reset_system(int reset_type, efi_status_t status, 557 unsigned long data_size, efi_char16_t *data) 558 { 559 if (down_trylock(&efi_runtime_lock)) { 560 pr_warn("failed to invoke the reset_system() runtime service:\n" 561 "could not get exclusive access to the firmware\n"); 562 return; 563 } 564 565 if (!efi_enabled(EFI_RUNTIME_SERVICES)) { 566 pr_warn("EFI Runtime Services are disabled, not invoking reset_system()\n"); 567 up(&efi_runtime_lock); 568 return; 569 } 570 571 efi_runtime_lock_owner = current; 572 arch_efi_call_virt_setup(); 573 efi_rts_work.efi_rts_id = EFI_RESET_SYSTEM; 574 arch_efi_call_virt(efi.runtime, reset_system, reset_type, status, 575 data_size, data); 576 arch_efi_call_virt_teardown(); 577 efi_runtime_lock_owner = NULL; 578 up(&efi_runtime_lock); 579 } 580 581 static efi_status_t virt_efi_update_capsule(efi_capsule_header_t **capsules, 582 unsigned long count, 583 unsigned long sg_list) 584 { 585 efi_status_t status; 586 587 if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION) 588 return EFI_UNSUPPORTED; 589 590 if (down_interruptible(&efi_runtime_lock)) 591 return EFI_ABORTED; 592 status = efi_queue_work(UPDATE_CAPSULE, capsules, count, sg_list); 593 up(&efi_runtime_lock); 594 return status; 595 } 596 597 static efi_status_t virt_efi_query_capsule_caps(efi_capsule_header_t **capsules, 598 unsigned long count, 599 u64 *max_size, 600 int *reset_type) 601 { 602 efi_status_t status; 603 604 if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION) 605 return EFI_UNSUPPORTED; 606 607 if (down_interruptible(&efi_runtime_lock)) 608 return EFI_ABORTED; 609 status = efi_queue_work(QUERY_CAPSULE_CAPS, capsules, count, 610 max_size, reset_type); 611 up(&efi_runtime_lock); 612 return status; 613 } 614 615 void __init efi_native_runtime_setup(void) 616 { 617 efi.get_time = virt_efi_get_time; 618 efi.set_time = virt_efi_set_time; 619 efi.get_wakeup_time = virt_efi_get_wakeup_time; 620 efi.set_wakeup_time = virt_efi_set_wakeup_time; 621 efi.get_variable = virt_efi_get_variable; 622 efi.get_next_variable = virt_efi_get_next_variable; 623 efi.set_variable = virt_efi_set_variable; 624 efi.set_variable_nonblocking = virt_efi_set_variable_nb; 625 efi.get_next_high_mono_count = virt_efi_get_next_high_mono_count; 626 efi.reset_system = virt_efi_reset_system; 627 efi.query_variable_info = virt_efi_query_variable_info; 628 efi.query_variable_info_nonblocking = virt_efi_query_variable_info_nb; 629 efi.update_capsule = virt_efi_update_capsule; 630 efi.query_capsule_caps = virt_efi_query_capsule_caps; 631 } 632 633 #ifdef CONFIG_ACPI_PRMT 634 635 efi_status_t 636 efi_call_acpi_prm_handler(efi_status_t (__efiapi *handler_addr)(u64, void *), 637 u64 param_buffer_addr, void *context) 638 { 639 efi_status_t status; 640 641 if (down_interruptible(&efi_runtime_lock)) 642 return EFI_ABORTED; 643 status = efi_queue_work(ACPI_PRM_HANDLER, handler_addr, 644 param_buffer_addr, context); 645 up(&efi_runtime_lock); 646 return status; 647 } 648 649 #endif 650 651 void efi_runtime_assert_lock_held(void) 652 { 653 WARN_ON(efi_runtime_lock_owner != current); 654 } 655