runtime-wrappers.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604
  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. #define pr_fmt(fmt) "efi: " fmt
  25. #include <linux/bug.h>
  26. #include <linux/efi.h>
  27. #include <linux/irqflags.h>
  28. #include <linux/mutex.h>
  29. #include <linux/semaphore.h>
  30. #include <linux/stringify.h>
  31. #include <linux/workqueue.h>
  32. #include <linux/completion.h>
  33. #include <asm/efi.h>
  34. /*
  35. * Wrap around the new efi_call_virt_generic() macros so that the
  36. * code doesn't get too cluttered:
  37. */
  38. #define efi_call_virt(f, args...) \
  39. arch_efi_call_virt(efi.runtime, f, args)
  40. union efi_rts_args {
  41. struct {
  42. efi_time_t *time;
  43. efi_time_cap_t *capabilities;
  44. } GET_TIME;
  45. struct {
  46. efi_time_t *time;
  47. } SET_TIME;
  48. struct {
  49. efi_bool_t *enabled;
  50. efi_bool_t *pending;
  51. efi_time_t *time;
  52. } GET_WAKEUP_TIME;
  53. struct {
  54. efi_bool_t enable;
  55. efi_time_t *time;
  56. } SET_WAKEUP_TIME;
  57. struct {
  58. efi_char16_t *name;
  59. efi_guid_t *vendor;
  60. u32 *attr;
  61. unsigned long *data_size;
  62. void *data;
  63. } GET_VARIABLE;
  64. struct {
  65. unsigned long *name_size;
  66. efi_char16_t *name;
  67. efi_guid_t *vendor;
  68. } GET_NEXT_VARIABLE;
  69. struct {
  70. efi_char16_t *name;
  71. efi_guid_t *vendor;
  72. u32 attr;
  73. unsigned long data_size;
  74. void *data;
  75. } SET_VARIABLE;
  76. struct {
  77. u32 attr;
  78. u64 *storage_space;
  79. u64 *remaining_space;
  80. u64 *max_variable_size;
  81. } QUERY_VARIABLE_INFO;
  82. struct {
  83. u32 *high_count;
  84. } GET_NEXT_HIGH_MONO_COUNT;
  85. struct {
  86. efi_capsule_header_t **capsules;
  87. unsigned long count;
  88. unsigned long sg_list;
  89. } UPDATE_CAPSULE;
  90. struct {
  91. efi_capsule_header_t **capsules;
  92. unsigned long count;
  93. u64 *max_size;
  94. int *reset_type;
  95. } QUERY_CAPSULE_CAPS;
  96. struct {
  97. efi_status_t (__efiapi *acpi_prm_handler)(u64, void *);
  98. u64 param_buffer_addr;
  99. void *context;
  100. } ACPI_PRM_HANDLER;
  101. };
  102. struct efi_runtime_work efi_rts_work;
  103. /*
  104. * efi_queue_work: Queue EFI runtime service call and wait for completion
  105. * @_rts: EFI runtime service function identifier
  106. * @_args: Arguments to pass to the EFI runtime service
  107. *
  108. * Accesses to efi_runtime_services() are serialized by a binary
  109. * semaphore (efi_runtime_lock) and caller waits until the work is
  110. * finished, hence _only_ one work is queued at a time and the caller
  111. * thread waits for completion.
  112. */
  113. #define efi_queue_work(_rts, _args...) \
  114. __efi_queue_work(EFI_ ## _rts, \
  115. &(union efi_rts_args){ ._rts = { _args }})
  116. #ifndef arch_efi_save_flags
  117. #define arch_efi_save_flags(state_flags) local_save_flags(state_flags)
  118. #define arch_efi_restore_flags(state_flags) local_irq_restore(state_flags)
  119. #endif
  120. unsigned long efi_call_virt_save_flags(void)
  121. {
  122. unsigned long flags;
  123. arch_efi_save_flags(flags);
  124. return flags;
  125. }
  126. void efi_call_virt_check_flags(unsigned long flags, const void *caller)
  127. {
  128. unsigned long cur_flags, mismatch;
  129. cur_flags = efi_call_virt_save_flags();
  130. mismatch = flags ^ cur_flags;
  131. if (!WARN_ON_ONCE(mismatch & ARCH_EFI_IRQ_FLAGS_MASK))
  132. return;
  133. add_taint(TAINT_FIRMWARE_WORKAROUND, LOCKDEP_NOW_UNRELIABLE);
  134. pr_err_ratelimited(FW_BUG "IRQ flags corrupted (0x%08lx=>0x%08lx) by EFI call from %pS\n",
  135. flags, cur_flags, caller ?: __builtin_return_address(0));
  136. arch_efi_restore_flags(flags);
  137. }
  138. /*
  139. * According to section 7.1 of the UEFI spec, Runtime Services are not fully
  140. * reentrant, and there are particular combinations of calls that need to be
  141. * serialized. (source: UEFI Specification v2.4A)
  142. *
  143. * Table 31. Rules for Reentry Into Runtime Services
  144. * +------------------------------------+-------------------------------+
  145. * | If previous call is busy in | Forbidden to call |
  146. * +------------------------------------+-------------------------------+
  147. * | Any | SetVirtualAddressMap() |
  148. * +------------------------------------+-------------------------------+
  149. * | ConvertPointer() | ConvertPointer() |
  150. * +------------------------------------+-------------------------------+
  151. * | SetVariable() | ResetSystem() |
  152. * | UpdateCapsule() | |
  153. * | SetTime() | |
  154. * | SetWakeupTime() | |
  155. * | GetNextHighMonotonicCount() | |
  156. * +------------------------------------+-------------------------------+
  157. * | GetVariable() | GetVariable() |
  158. * | GetNextVariableName() | GetNextVariableName() |
  159. * | SetVariable() | SetVariable() |
  160. * | QueryVariableInfo() | QueryVariableInfo() |
  161. * | UpdateCapsule() | UpdateCapsule() |
  162. * | QueryCapsuleCapabilities() | QueryCapsuleCapabilities() |
  163. * | GetNextHighMonotonicCount() | GetNextHighMonotonicCount() |
  164. * +------------------------------------+-------------------------------+
  165. * | GetTime() | GetTime() |
  166. * | SetTime() | SetTime() |
  167. * | GetWakeupTime() | GetWakeupTime() |
  168. * | SetWakeupTime() | SetWakeupTime() |
  169. * +------------------------------------+-------------------------------+
  170. *
  171. * Due to the fact that the EFI pstore may write to the variable store in
  172. * interrupt context, we need to use a lock for at least the groups that
  173. * contain SetVariable() and QueryVariableInfo(). That leaves little else, as
  174. * none of the remaining functions are actually ever called at runtime.
  175. * So let's just use a single lock to serialize all Runtime Services calls.
  176. */
  177. static DEFINE_SEMAPHORE(efi_runtime_lock, 1);
  178. static struct task_struct *efi_runtime_lock_owner;
  179. /*
  180. * Expose the EFI runtime lock to the UV platform
  181. */
  182. #ifdef CONFIG_X86_UV
  183. extern struct semaphore __efi_uv_runtime_lock __alias(efi_runtime_lock);
  184. #endif
  185. /*
  186. * Calls the appropriate efi_runtime_service() with the appropriate
  187. * arguments.
  188. */
  189. static void __nocfi efi_call_rts(struct work_struct *work)
  190. {
  191. const union efi_rts_args *args = efi_rts_work.args;
  192. efi_status_t status = EFI_NOT_FOUND;
  193. unsigned long flags;
  194. efi_runtime_lock_owner = current;
  195. arch_efi_call_virt_setup();
  196. flags = efi_call_virt_save_flags();
  197. switch (efi_rts_work.efi_rts_id) {
  198. case EFI_GET_TIME:
  199. status = efi_call_virt(get_time,
  200. args->GET_TIME.time,
  201. args->GET_TIME.capabilities);
  202. break;
  203. case EFI_SET_TIME:
  204. status = efi_call_virt(set_time,
  205. args->SET_TIME.time);
  206. break;
  207. case EFI_GET_WAKEUP_TIME:
  208. status = efi_call_virt(get_wakeup_time,
  209. args->GET_WAKEUP_TIME.enabled,
  210. args->GET_WAKEUP_TIME.pending,
  211. args->GET_WAKEUP_TIME.time);
  212. break;
  213. case EFI_SET_WAKEUP_TIME:
  214. status = efi_call_virt(set_wakeup_time,
  215. args->SET_WAKEUP_TIME.enable,
  216. args->SET_WAKEUP_TIME.time);
  217. break;
  218. case EFI_GET_VARIABLE:
  219. status = efi_call_virt(get_variable,
  220. args->GET_VARIABLE.name,
  221. args->GET_VARIABLE.vendor,
  222. args->GET_VARIABLE.attr,
  223. args->GET_VARIABLE.data_size,
  224. args->GET_VARIABLE.data);
  225. break;
  226. case EFI_GET_NEXT_VARIABLE:
  227. status = efi_call_virt(get_next_variable,
  228. args->GET_NEXT_VARIABLE.name_size,
  229. args->GET_NEXT_VARIABLE.name,
  230. args->GET_NEXT_VARIABLE.vendor);
  231. break;
  232. case EFI_SET_VARIABLE:
  233. status = efi_call_virt(set_variable,
  234. args->SET_VARIABLE.name,
  235. args->SET_VARIABLE.vendor,
  236. args->SET_VARIABLE.attr,
  237. args->SET_VARIABLE.data_size,
  238. args->SET_VARIABLE.data);
  239. break;
  240. case EFI_QUERY_VARIABLE_INFO:
  241. status = efi_call_virt(query_variable_info,
  242. args->QUERY_VARIABLE_INFO.attr,
  243. args->QUERY_VARIABLE_INFO.storage_space,
  244. args->QUERY_VARIABLE_INFO.remaining_space,
  245. args->QUERY_VARIABLE_INFO.max_variable_size);
  246. break;
  247. case EFI_GET_NEXT_HIGH_MONO_COUNT:
  248. status = efi_call_virt(get_next_high_mono_count,
  249. args->GET_NEXT_HIGH_MONO_COUNT.high_count);
  250. break;
  251. case EFI_UPDATE_CAPSULE:
  252. status = efi_call_virt(update_capsule,
  253. args->UPDATE_CAPSULE.capsules,
  254. args->UPDATE_CAPSULE.count,
  255. args->UPDATE_CAPSULE.sg_list);
  256. break;
  257. case EFI_QUERY_CAPSULE_CAPS:
  258. status = efi_call_virt(query_capsule_caps,
  259. args->QUERY_CAPSULE_CAPS.capsules,
  260. args->QUERY_CAPSULE_CAPS.count,
  261. args->QUERY_CAPSULE_CAPS.max_size,
  262. args->QUERY_CAPSULE_CAPS.reset_type);
  263. break;
  264. case EFI_ACPI_PRM_HANDLER:
  265. #ifdef CONFIG_ACPI_PRMT
  266. status = arch_efi_call_virt(args, ACPI_PRM_HANDLER.acpi_prm_handler,
  267. args->ACPI_PRM_HANDLER.param_buffer_addr,
  268. args->ACPI_PRM_HANDLER.context);
  269. break;
  270. #endif
  271. default:
  272. /*
  273. * Ideally, we should never reach here because a caller of this
  274. * function should have put the right efi_runtime_service()
  275. * function identifier into efi_rts_work->efi_rts_id
  276. */
  277. pr_err("Requested executing invalid EFI Runtime Service.\n");
  278. }
  279. efi_call_virt_check_flags(flags, efi_rts_work.caller);
  280. arch_efi_call_virt_teardown();
  281. efi_rts_work.status = status;
  282. complete(&efi_rts_work.efi_rts_comp);
  283. efi_runtime_lock_owner = NULL;
  284. }
  285. static efi_status_t __efi_queue_work(enum efi_rts_ids id,
  286. union efi_rts_args *args)
  287. {
  288. efi_rts_work.efi_rts_id = id;
  289. efi_rts_work.args = args;
  290. efi_rts_work.caller = __builtin_return_address(0);
  291. efi_rts_work.status = EFI_ABORTED;
  292. if (!efi_enabled(EFI_RUNTIME_SERVICES)) {
  293. pr_warn_once("EFI Runtime Services are disabled!\n");
  294. efi_rts_work.status = EFI_DEVICE_ERROR;
  295. goto exit;
  296. }
  297. init_completion(&efi_rts_work.efi_rts_comp);
  298. INIT_WORK(&efi_rts_work.work, efi_call_rts);
  299. /*
  300. * queue_work() returns 0 if work was already on queue,
  301. * _ideally_ this should never happen.
  302. */
  303. if (queue_work(efi_rts_wq, &efi_rts_work.work))
  304. wait_for_completion(&efi_rts_work.efi_rts_comp);
  305. else
  306. pr_err("Failed to queue work to efi_rts_wq.\n");
  307. WARN_ON_ONCE(efi_rts_work.status == EFI_ABORTED);
  308. exit:
  309. efi_rts_work.efi_rts_id = EFI_NONE;
  310. return efi_rts_work.status;
  311. }
  312. static efi_status_t virt_efi_get_time(efi_time_t *tm, efi_time_cap_t *tc)
  313. {
  314. efi_status_t status;
  315. if (down_interruptible(&efi_runtime_lock))
  316. return EFI_ABORTED;
  317. status = efi_queue_work(GET_TIME, tm, tc);
  318. up(&efi_runtime_lock);
  319. return status;
  320. }
  321. static efi_status_t virt_efi_set_time(efi_time_t *tm)
  322. {
  323. efi_status_t status;
  324. if (down_interruptible(&efi_runtime_lock))
  325. return EFI_ABORTED;
  326. status = efi_queue_work(SET_TIME, tm);
  327. up(&efi_runtime_lock);
  328. return status;
  329. }
  330. static efi_status_t virt_efi_get_wakeup_time(efi_bool_t *enabled,
  331. efi_bool_t *pending,
  332. efi_time_t *tm)
  333. {
  334. efi_status_t status;
  335. if (down_interruptible(&efi_runtime_lock))
  336. return EFI_ABORTED;
  337. status = efi_queue_work(GET_WAKEUP_TIME, enabled, pending, tm);
  338. up(&efi_runtime_lock);
  339. return status;
  340. }
  341. static efi_status_t virt_efi_set_wakeup_time(efi_bool_t enabled, efi_time_t *tm)
  342. {
  343. efi_status_t status;
  344. if (down_interruptible(&efi_runtime_lock))
  345. return EFI_ABORTED;
  346. status = efi_queue_work(SET_WAKEUP_TIME, enabled, tm);
  347. up(&efi_runtime_lock);
  348. return status;
  349. }
  350. static efi_status_t virt_efi_get_variable(efi_char16_t *name,
  351. efi_guid_t *vendor,
  352. u32 *attr,
  353. unsigned long *data_size,
  354. void *data)
  355. {
  356. efi_status_t status;
  357. if (down_interruptible(&efi_runtime_lock))
  358. return EFI_ABORTED;
  359. status = efi_queue_work(GET_VARIABLE, name, vendor, attr, data_size,
  360. data);
  361. up(&efi_runtime_lock);
  362. return status;
  363. }
  364. static efi_status_t virt_efi_get_next_variable(unsigned long *name_size,
  365. efi_char16_t *name,
  366. efi_guid_t *vendor)
  367. {
  368. efi_status_t status;
  369. if (down_interruptible(&efi_runtime_lock))
  370. return EFI_ABORTED;
  371. status = efi_queue_work(GET_NEXT_VARIABLE, name_size, name, vendor);
  372. up(&efi_runtime_lock);
  373. return status;
  374. }
  375. static efi_status_t virt_efi_set_variable(efi_char16_t *name,
  376. efi_guid_t *vendor,
  377. u32 attr,
  378. unsigned long data_size,
  379. void *data)
  380. {
  381. efi_status_t status;
  382. if (down_interruptible(&efi_runtime_lock))
  383. return EFI_ABORTED;
  384. status = efi_queue_work(SET_VARIABLE, name, vendor, attr, data_size,
  385. data);
  386. up(&efi_runtime_lock);
  387. return status;
  388. }
  389. static efi_status_t __nocfi
  390. virt_efi_set_variable_nb(efi_char16_t *name, efi_guid_t *vendor, u32 attr,
  391. unsigned long data_size, void *data)
  392. {
  393. efi_status_t status;
  394. if (down_trylock(&efi_runtime_lock))
  395. return EFI_NOT_READY;
  396. efi_runtime_lock_owner = current;
  397. status = efi_call_virt_pointer(efi.runtime, set_variable, name, vendor,
  398. attr, data_size, data);
  399. efi_runtime_lock_owner = NULL;
  400. up(&efi_runtime_lock);
  401. return status;
  402. }
  403. static efi_status_t virt_efi_query_variable_info(u32 attr,
  404. u64 *storage_space,
  405. u64 *remaining_space,
  406. u64 *max_variable_size)
  407. {
  408. efi_status_t status;
  409. if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
  410. return EFI_UNSUPPORTED;
  411. if (down_interruptible(&efi_runtime_lock))
  412. return EFI_ABORTED;
  413. status = efi_queue_work(QUERY_VARIABLE_INFO, attr, storage_space,
  414. remaining_space, max_variable_size);
  415. up(&efi_runtime_lock);
  416. return status;
  417. }
  418. static efi_status_t __nocfi
  419. virt_efi_query_variable_info_nb(u32 attr, u64 *storage_space,
  420. u64 *remaining_space, u64 *max_variable_size)
  421. {
  422. efi_status_t status;
  423. if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
  424. return EFI_UNSUPPORTED;
  425. if (down_trylock(&efi_runtime_lock))
  426. return EFI_NOT_READY;
  427. efi_runtime_lock_owner = current;
  428. status = efi_call_virt_pointer(efi.runtime, query_variable_info, attr,
  429. storage_space, remaining_space,
  430. max_variable_size);
  431. efi_runtime_lock_owner = NULL;
  432. up(&efi_runtime_lock);
  433. return status;
  434. }
  435. static efi_status_t virt_efi_get_next_high_mono_count(u32 *count)
  436. {
  437. efi_status_t status;
  438. if (down_interruptible(&efi_runtime_lock))
  439. return EFI_ABORTED;
  440. status = efi_queue_work(GET_NEXT_HIGH_MONO_COUNT, count);
  441. up(&efi_runtime_lock);
  442. return status;
  443. }
  444. static void __nocfi
  445. virt_efi_reset_system(int reset_type, efi_status_t status,
  446. unsigned long data_size, efi_char16_t *data)
  447. {
  448. if (down_trylock(&efi_runtime_lock)) {
  449. pr_warn("failed to invoke the reset_system() runtime service:\n"
  450. "could not get exclusive access to the firmware\n");
  451. return;
  452. }
  453. efi_runtime_lock_owner = current;
  454. arch_efi_call_virt_setup();
  455. efi_rts_work.efi_rts_id = EFI_RESET_SYSTEM;
  456. arch_efi_call_virt(efi.runtime, reset_system, reset_type, status,
  457. data_size, data);
  458. arch_efi_call_virt_teardown();
  459. efi_runtime_lock_owner = NULL;
  460. up(&efi_runtime_lock);
  461. }
  462. static efi_status_t virt_efi_update_capsule(efi_capsule_header_t **capsules,
  463. unsigned long count,
  464. unsigned long sg_list)
  465. {
  466. efi_status_t status;
  467. if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
  468. return EFI_UNSUPPORTED;
  469. if (down_interruptible(&efi_runtime_lock))
  470. return EFI_ABORTED;
  471. status = efi_queue_work(UPDATE_CAPSULE, capsules, count, sg_list);
  472. up(&efi_runtime_lock);
  473. return status;
  474. }
  475. static efi_status_t virt_efi_query_capsule_caps(efi_capsule_header_t **capsules,
  476. unsigned long count,
  477. u64 *max_size,
  478. int *reset_type)
  479. {
  480. efi_status_t status;
  481. if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
  482. return EFI_UNSUPPORTED;
  483. if (down_interruptible(&efi_runtime_lock))
  484. return EFI_ABORTED;
  485. status = efi_queue_work(QUERY_CAPSULE_CAPS, capsules, count,
  486. max_size, reset_type);
  487. up(&efi_runtime_lock);
  488. return status;
  489. }
  490. void __init efi_native_runtime_setup(void)
  491. {
  492. efi.get_time = virt_efi_get_time;
  493. efi.set_time = virt_efi_set_time;
  494. efi.get_wakeup_time = virt_efi_get_wakeup_time;
  495. efi.set_wakeup_time = virt_efi_set_wakeup_time;
  496. efi.get_variable = virt_efi_get_variable;
  497. efi.get_next_variable = virt_efi_get_next_variable;
  498. efi.set_variable = virt_efi_set_variable;
  499. efi.set_variable_nonblocking = virt_efi_set_variable_nb;
  500. efi.get_next_high_mono_count = virt_efi_get_next_high_mono_count;
  501. efi.reset_system = virt_efi_reset_system;
  502. efi.query_variable_info = virt_efi_query_variable_info;
  503. efi.query_variable_info_nonblocking = virt_efi_query_variable_info_nb;
  504. efi.update_capsule = virt_efi_update_capsule;
  505. efi.query_capsule_caps = virt_efi_query_capsule_caps;
  506. }
  507. #ifdef CONFIG_ACPI_PRMT
  508. efi_status_t
  509. efi_call_acpi_prm_handler(efi_status_t (__efiapi *handler_addr)(u64, void *),
  510. u64 param_buffer_addr, void *context)
  511. {
  512. efi_status_t status;
  513. if (down_interruptible(&efi_runtime_lock))
  514. return EFI_ABORTED;
  515. status = efi_queue_work(ACPI_PRM_HANDLER, handler_addr,
  516. param_buffer_addr, context);
  517. up(&efi_runtime_lock);
  518. return status;
  519. }
  520. #endif
  521. void efi_runtime_assert_lock_held(void)
  522. {
  523. WARN_ON(efi_runtime_lock_owner != current);
  524. }