runtime-wrappers.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  1. /*
  2. * runtime-wrappers.c - Runtime Services function call wrappers
  3. *
  4. * Implementation summary:
  5. * -----------------------
  6. * 1. When user/kernel thread requests to execute efi_runtime_service(),
  7. * enqueue work to efi_rts_wq.
  8. * 2. Caller thread waits for completion until the work is finished
  9. * because it's dependent on the return status and execution of
  10. * efi_runtime_service().
  11. * For instance, get_variable() and get_next_variable().
  12. *
  13. * Copyright (C) 2014 Linaro Ltd. <ard.biesheuvel@linaro.org>
  14. *
  15. * Split off from arch/x86/platform/efi/efi.c
  16. *
  17. * Copyright (C) 1999 VA Linux Systems
  18. * Copyright (C) 1999 Walt Drummond <drummond@valinux.com>
  19. * Copyright (C) 1999-2002 Hewlett-Packard Co.
  20. * Copyright (C) 2005-2008 Intel Co.
  21. * Copyright (C) 2013 SuSE Labs
  22. *
  23. * This file is released under the GPLv2.
  24. */
  25. #define pr_fmt(fmt) "efi: " fmt
  26. #include <linux/bug.h>
  27. #include <linux/efi.h>
  28. #include <linux/irqflags.h>
  29. #include <linux/mutex.h>
  30. #include <linux/semaphore.h>
  31. #include <linux/stringify.h>
  32. #include <linux/workqueue.h>
  33. #include <linux/completion.h>
  34. #include <asm/efi.h>
  35. /*
  36. * Wrap around the new efi_call_virt_generic() macros so that the
  37. * code doesn't get too cluttered:
  38. */
  39. #define efi_call_virt(f, args...) \
  40. efi_call_virt_pointer(efi.systab->runtime, f, args)
  41. #define __efi_call_virt(f, args...) \
  42. __efi_call_virt_pointer(efi.systab->runtime, f, args)
  43. struct efi_runtime_work efi_rts_work;
  44. /*
  45. * efi_queue_work: Queue efi_runtime_service() and wait until it's done
  46. * @rts: efi_runtime_service() function identifier
  47. * @rts_arg<1-5>: efi_runtime_service() function arguments
  48. *
  49. * Accesses to efi_runtime_services() are serialized by a binary
  50. * semaphore (efi_runtime_lock) and caller waits until the work is
  51. * finished, hence _only_ one work is queued at a time and the caller
  52. * thread waits for completion.
  53. */
  54. #define efi_queue_work(_rts, _arg1, _arg2, _arg3, _arg4, _arg5) \
  55. ({ \
  56. efi_rts_work.status = EFI_ABORTED; \
  57. \
  58. if (!efi_enabled(EFI_RUNTIME_SERVICES)) { \
  59. pr_warn_once("EFI Runtime Services are disabled!\n"); \
  60. goto exit; \
  61. } \
  62. \
  63. init_completion(&efi_rts_work.efi_rts_comp); \
  64. INIT_WORK(&efi_rts_work.work, efi_call_rts); \
  65. efi_rts_work.arg1 = _arg1; \
  66. efi_rts_work.arg2 = _arg2; \
  67. efi_rts_work.arg3 = _arg3; \
  68. efi_rts_work.arg4 = _arg4; \
  69. efi_rts_work.arg5 = _arg5; \
  70. efi_rts_work.efi_rts_id = _rts; \
  71. \
  72. /* \
  73. * queue_work() returns 0 if work was already on queue, \
  74. * _ideally_ this should never happen. \
  75. */ \
  76. if (queue_work(efi_rts_wq, &efi_rts_work.work)) \
  77. wait_for_completion(&efi_rts_work.efi_rts_comp); \
  78. else \
  79. pr_err("Failed to queue work to efi_rts_wq.\n"); \
  80. \
  81. exit: \
  82. efi_rts_work.efi_rts_id = NONE; \
  83. efi_rts_work.status; \
  84. })
  85. void efi_call_virt_check_flags(unsigned long flags, const char *call)
  86. {
  87. unsigned long cur_flags, mismatch;
  88. local_save_flags(cur_flags);
  89. mismatch = flags ^ cur_flags;
  90. if (!WARN_ON_ONCE(mismatch & ARCH_EFI_IRQ_FLAGS_MASK))
  91. return;
  92. add_taint(TAINT_FIRMWARE_WORKAROUND, LOCKDEP_NOW_UNRELIABLE);
  93. pr_err_ratelimited(FW_BUG "IRQ flags corrupted (0x%08lx=>0x%08lx) by EFI %s\n",
  94. flags, cur_flags, call);
  95. local_irq_restore(flags);
  96. }
  97. /*
  98. * According to section 7.1 of the UEFI spec, Runtime Services are not fully
  99. * reentrant, and there are particular combinations of calls that need to be
  100. * serialized. (source: UEFI Specification v2.4A)
  101. *
  102. * Table 31. Rules for Reentry Into Runtime Services
  103. * +------------------------------------+-------------------------------+
  104. * | If previous call is busy in | Forbidden to call |
  105. * +------------------------------------+-------------------------------+
  106. * | Any | SetVirtualAddressMap() |
  107. * +------------------------------------+-------------------------------+
  108. * | ConvertPointer() | ConvertPointer() |
  109. * +------------------------------------+-------------------------------+
  110. * | SetVariable() | ResetSystem() |
  111. * | UpdateCapsule() | |
  112. * | SetTime() | |
  113. * | SetWakeupTime() | |
  114. * | GetNextHighMonotonicCount() | |
  115. * +------------------------------------+-------------------------------+
  116. * | GetVariable() | GetVariable() |
  117. * | GetNextVariableName() | GetNextVariableName() |
  118. * | SetVariable() | SetVariable() |
  119. * | QueryVariableInfo() | QueryVariableInfo() |
  120. * | UpdateCapsule() | UpdateCapsule() |
  121. * | QueryCapsuleCapabilities() | QueryCapsuleCapabilities() |
  122. * | GetNextHighMonotonicCount() | GetNextHighMonotonicCount() |
  123. * +------------------------------------+-------------------------------+
  124. * | GetTime() | GetTime() |
  125. * | SetTime() | SetTime() |
  126. * | GetWakeupTime() | GetWakeupTime() |
  127. * | SetWakeupTime() | SetWakeupTime() |
  128. * +------------------------------------+-------------------------------+
  129. *
  130. * Due to the fact that the EFI pstore may write to the variable store in
  131. * interrupt context, we need to use a lock for at least the groups that
  132. * contain SetVariable() and QueryVariableInfo(). That leaves little else, as
  133. * none of the remaining functions are actually ever called at runtime.
  134. * So let's just use a single lock to serialize all Runtime Services calls.
  135. */
  136. static DEFINE_SEMAPHORE(efi_runtime_lock);
  137. /*
  138. * Calls the appropriate efi_runtime_service() with the appropriate
  139. * arguments.
  140. *
  141. * Semantics followed by efi_call_rts() to understand efi_runtime_work:
  142. * 1. If argument was a pointer, recast it from void pointer to original
  143. * pointer type.
  144. * 2. If argument was a value, recast it from void pointer to original
  145. * pointer type and dereference it.
  146. */
  147. static void efi_call_rts(struct work_struct *work)
  148. {
  149. void *arg1, *arg2, *arg3, *arg4, *arg5;
  150. efi_status_t status = EFI_NOT_FOUND;
  151. arg1 = efi_rts_work.arg1;
  152. arg2 = efi_rts_work.arg2;
  153. arg3 = efi_rts_work.arg3;
  154. arg4 = efi_rts_work.arg4;
  155. arg5 = efi_rts_work.arg5;
  156. switch (efi_rts_work.efi_rts_id) {
  157. case GET_TIME:
  158. status = efi_call_virt(get_time, (efi_time_t *)arg1,
  159. (efi_time_cap_t *)arg2);
  160. break;
  161. case SET_TIME:
  162. status = efi_call_virt(set_time, (efi_time_t *)arg1);
  163. break;
  164. case GET_WAKEUP_TIME:
  165. status = efi_call_virt(get_wakeup_time, (efi_bool_t *)arg1,
  166. (efi_bool_t *)arg2, (efi_time_t *)arg3);
  167. break;
  168. case SET_WAKEUP_TIME:
  169. status = efi_call_virt(set_wakeup_time, *(efi_bool_t *)arg1,
  170. (efi_time_t *)arg2);
  171. break;
  172. case GET_VARIABLE:
  173. status = efi_call_virt(get_variable, (efi_char16_t *)arg1,
  174. (efi_guid_t *)arg2, (u32 *)arg3,
  175. (unsigned long *)arg4, (void *)arg5);
  176. break;
  177. case GET_NEXT_VARIABLE:
  178. status = efi_call_virt(get_next_variable, (unsigned long *)arg1,
  179. (efi_char16_t *)arg2,
  180. (efi_guid_t *)arg3);
  181. break;
  182. case SET_VARIABLE:
  183. status = efi_call_virt(set_variable, (efi_char16_t *)arg1,
  184. (efi_guid_t *)arg2, *(u32 *)arg3,
  185. *(unsigned long *)arg4, (void *)arg5);
  186. break;
  187. case QUERY_VARIABLE_INFO:
  188. status = efi_call_virt(query_variable_info, *(u32 *)arg1,
  189. (u64 *)arg2, (u64 *)arg3, (u64 *)arg4);
  190. break;
  191. case GET_NEXT_HIGH_MONO_COUNT:
  192. status = efi_call_virt(get_next_high_mono_count, (u32 *)arg1);
  193. break;
  194. case UPDATE_CAPSULE:
  195. status = efi_call_virt(update_capsule,
  196. (efi_capsule_header_t **)arg1,
  197. *(unsigned long *)arg2,
  198. *(unsigned long *)arg3);
  199. break;
  200. case QUERY_CAPSULE_CAPS:
  201. status = efi_call_virt(query_capsule_caps,
  202. (efi_capsule_header_t **)arg1,
  203. *(unsigned long *)arg2, (u64 *)arg3,
  204. (int *)arg4);
  205. break;
  206. default:
  207. /*
  208. * Ideally, we should never reach here because a caller of this
  209. * function should have put the right efi_runtime_service()
  210. * function identifier into efi_rts_work->efi_rts_id
  211. */
  212. pr_err("Requested executing invalid EFI Runtime Service.\n");
  213. }
  214. efi_rts_work.status = status;
  215. complete(&efi_rts_work.efi_rts_comp);
  216. }
  217. static efi_status_t virt_efi_get_time(efi_time_t *tm, efi_time_cap_t *tc)
  218. {
  219. efi_status_t status;
  220. if (down_interruptible(&efi_runtime_lock))
  221. return EFI_ABORTED;
  222. status = efi_queue_work(GET_TIME, tm, tc, NULL, NULL, NULL);
  223. up(&efi_runtime_lock);
  224. return status;
  225. }
  226. static efi_status_t virt_efi_set_time(efi_time_t *tm)
  227. {
  228. efi_status_t status;
  229. if (down_interruptible(&efi_runtime_lock))
  230. return EFI_ABORTED;
  231. status = efi_queue_work(SET_TIME, tm, NULL, NULL, NULL, NULL);
  232. up(&efi_runtime_lock);
  233. return status;
  234. }
  235. static efi_status_t virt_efi_get_wakeup_time(efi_bool_t *enabled,
  236. efi_bool_t *pending,
  237. efi_time_t *tm)
  238. {
  239. efi_status_t status;
  240. if (down_interruptible(&efi_runtime_lock))
  241. return EFI_ABORTED;
  242. status = efi_queue_work(GET_WAKEUP_TIME, enabled, pending, tm, NULL,
  243. NULL);
  244. up(&efi_runtime_lock);
  245. return status;
  246. }
  247. static efi_status_t virt_efi_set_wakeup_time(efi_bool_t enabled, efi_time_t *tm)
  248. {
  249. efi_status_t status;
  250. if (down_interruptible(&efi_runtime_lock))
  251. return EFI_ABORTED;
  252. status = efi_queue_work(SET_WAKEUP_TIME, &enabled, tm, NULL, NULL,
  253. NULL);
  254. up(&efi_runtime_lock);
  255. return status;
  256. }
  257. static efi_status_t virt_efi_get_variable(efi_char16_t *name,
  258. efi_guid_t *vendor,
  259. u32 *attr,
  260. unsigned long *data_size,
  261. void *data)
  262. {
  263. efi_status_t status;
  264. if (down_interruptible(&efi_runtime_lock))
  265. return EFI_ABORTED;
  266. status = efi_queue_work(GET_VARIABLE, name, vendor, attr, data_size,
  267. data);
  268. up(&efi_runtime_lock);
  269. return status;
  270. }
  271. static efi_status_t virt_efi_get_next_variable(unsigned long *name_size,
  272. efi_char16_t *name,
  273. efi_guid_t *vendor)
  274. {
  275. efi_status_t status;
  276. if (down_interruptible(&efi_runtime_lock))
  277. return EFI_ABORTED;
  278. status = efi_queue_work(GET_NEXT_VARIABLE, name_size, name, vendor,
  279. NULL, NULL);
  280. up(&efi_runtime_lock);
  281. return status;
  282. }
  283. static efi_status_t virt_efi_set_variable(efi_char16_t *name,
  284. efi_guid_t *vendor,
  285. u32 attr,
  286. unsigned long data_size,
  287. void *data)
  288. {
  289. efi_status_t status;
  290. if (down_interruptible(&efi_runtime_lock))
  291. return EFI_ABORTED;
  292. status = efi_queue_work(SET_VARIABLE, name, vendor, &attr, &data_size,
  293. data);
  294. up(&efi_runtime_lock);
  295. return status;
  296. }
  297. static efi_status_t
  298. virt_efi_set_variable_nonblocking(efi_char16_t *name, efi_guid_t *vendor,
  299. u32 attr, unsigned long data_size,
  300. void *data)
  301. {
  302. efi_status_t status;
  303. if (down_trylock(&efi_runtime_lock))
  304. return EFI_NOT_READY;
  305. status = efi_call_virt(set_variable, name, vendor, attr, data_size,
  306. data);
  307. up(&efi_runtime_lock);
  308. return status;
  309. }
  310. static efi_status_t virt_efi_query_variable_info(u32 attr,
  311. u64 *storage_space,
  312. u64 *remaining_space,
  313. u64 *max_variable_size)
  314. {
  315. efi_status_t status;
  316. if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
  317. return EFI_UNSUPPORTED;
  318. if (down_interruptible(&efi_runtime_lock))
  319. return EFI_ABORTED;
  320. status = efi_queue_work(QUERY_VARIABLE_INFO, &attr, storage_space,
  321. remaining_space, max_variable_size, NULL);
  322. up(&efi_runtime_lock);
  323. return status;
  324. }
  325. static efi_status_t
  326. virt_efi_query_variable_info_nonblocking(u32 attr,
  327. u64 *storage_space,
  328. u64 *remaining_space,
  329. u64 *max_variable_size)
  330. {
  331. efi_status_t status;
  332. if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
  333. return EFI_UNSUPPORTED;
  334. if (down_trylock(&efi_runtime_lock))
  335. return EFI_NOT_READY;
  336. status = efi_call_virt(query_variable_info, attr, storage_space,
  337. remaining_space, max_variable_size);
  338. up(&efi_runtime_lock);
  339. return status;
  340. }
  341. static efi_status_t virt_efi_get_next_high_mono_count(u32 *count)
  342. {
  343. efi_status_t status;
  344. if (down_interruptible(&efi_runtime_lock))
  345. return EFI_ABORTED;
  346. status = efi_queue_work(GET_NEXT_HIGH_MONO_COUNT, count, NULL, NULL,
  347. NULL, NULL);
  348. up(&efi_runtime_lock);
  349. return status;
  350. }
  351. static void virt_efi_reset_system(int reset_type,
  352. efi_status_t status,
  353. unsigned long data_size,
  354. efi_char16_t *data)
  355. {
  356. if (down_interruptible(&efi_runtime_lock)) {
  357. pr_warn("failed to invoke the reset_system() runtime service:\n"
  358. "could not get exclusive access to the firmware\n");
  359. return;
  360. }
  361. efi_rts_work.efi_rts_id = RESET_SYSTEM;
  362. __efi_call_virt(reset_system, reset_type, status, data_size, data);
  363. up(&efi_runtime_lock);
  364. }
  365. static efi_status_t virt_efi_update_capsule(efi_capsule_header_t **capsules,
  366. unsigned long count,
  367. unsigned long sg_list)
  368. {
  369. efi_status_t status;
  370. if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
  371. return EFI_UNSUPPORTED;
  372. if (down_interruptible(&efi_runtime_lock))
  373. return EFI_ABORTED;
  374. status = efi_queue_work(UPDATE_CAPSULE, capsules, &count, &sg_list,
  375. NULL, NULL);
  376. up(&efi_runtime_lock);
  377. return status;
  378. }
  379. static efi_status_t virt_efi_query_capsule_caps(efi_capsule_header_t **capsules,
  380. unsigned long count,
  381. u64 *max_size,
  382. int *reset_type)
  383. {
  384. efi_status_t status;
  385. if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
  386. return EFI_UNSUPPORTED;
  387. if (down_interruptible(&efi_runtime_lock))
  388. return EFI_ABORTED;
  389. status = efi_queue_work(QUERY_CAPSULE_CAPS, capsules, &count,
  390. max_size, reset_type, NULL);
  391. up(&efi_runtime_lock);
  392. return status;
  393. }
  394. void efi_native_runtime_setup(void)
  395. {
  396. efi.get_time = virt_efi_get_time;
  397. efi.set_time = virt_efi_set_time;
  398. efi.get_wakeup_time = virt_efi_get_wakeup_time;
  399. efi.set_wakeup_time = virt_efi_set_wakeup_time;
  400. efi.get_variable = virt_efi_get_variable;
  401. efi.get_next_variable = virt_efi_get_next_variable;
  402. efi.set_variable = virt_efi_set_variable;
  403. efi.set_variable_nonblocking = virt_efi_set_variable_nonblocking;
  404. efi.get_next_high_mono_count = virt_efi_get_next_high_mono_count;
  405. efi.reset_system = virt_efi_reset_system;
  406. efi.query_variable_info = virt_efi_query_variable_info;
  407. efi.query_variable_info_nonblocking = virt_efi_query_variable_info_nonblocking;
  408. efi.update_capsule = virt_efi_update_capsule;
  409. efi.query_capsule_caps = virt_efi_query_capsule_caps;
  410. }