runtime-wrappers.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. /*
  2. * runtime-wrappers.c - Runtime Services function call wrappers
  3. *
  4. * Copyright (C) 2014 Linaro Ltd. <ard.biesheuvel@linaro.org>
  5. *
  6. * Split off from arch/x86/platform/efi/efi.c
  7. *
  8. * Copyright (C) 1999 VA Linux Systems
  9. * Copyright (C) 1999 Walt Drummond <drummond@valinux.com>
  10. * Copyright (C) 1999-2002 Hewlett-Packard Co.
  11. * Copyright (C) 2005-2008 Intel Co.
  12. * Copyright (C) 2013 SuSE Labs
  13. *
  14. * This file is released under the GPLv2.
  15. */
  16. #include <linux/bug.h>
  17. #include <linux/efi.h>
  18. #include <linux/mutex.h>
  19. #include <linux/spinlock.h>
  20. #include <asm/efi.h>
  21. /*
  22. * According to section 7.1 of the UEFI spec, Runtime Services are not fully
  23. * reentrant, and there are particular combinations of calls that need to be
  24. * serialized. (source: UEFI Specification v2.4A)
  25. *
  26. * Table 31. Rules for Reentry Into Runtime Services
  27. * +------------------------------------+-------------------------------+
  28. * | If previous call is busy in | Forbidden to call |
  29. * +------------------------------------+-------------------------------+
  30. * | Any | SetVirtualAddressMap() |
  31. * +------------------------------------+-------------------------------+
  32. * | ConvertPointer() | ConvertPointer() |
  33. * +------------------------------------+-------------------------------+
  34. * | SetVariable() | ResetSystem() |
  35. * | UpdateCapsule() | |
  36. * | SetTime() | |
  37. * | SetWakeupTime() | |
  38. * | GetNextHighMonotonicCount() | |
  39. * +------------------------------------+-------------------------------+
  40. * | GetVariable() | GetVariable() |
  41. * | GetNextVariableName() | GetNextVariableName() |
  42. * | SetVariable() | SetVariable() |
  43. * | QueryVariableInfo() | QueryVariableInfo() |
  44. * | UpdateCapsule() | UpdateCapsule() |
  45. * | QueryCapsuleCapabilities() | QueryCapsuleCapabilities() |
  46. * | GetNextHighMonotonicCount() | GetNextHighMonotonicCount() |
  47. * +------------------------------------+-------------------------------+
  48. * | GetTime() | GetTime() |
  49. * | SetTime() | SetTime() |
  50. * | GetWakeupTime() | GetWakeupTime() |
  51. * | SetWakeupTime() | SetWakeupTime() |
  52. * +------------------------------------+-------------------------------+
  53. *
  54. * Due to the fact that the EFI pstore may write to the variable store in
  55. * interrupt context, we need to use a spinlock for at least the groups that
  56. * contain SetVariable() and QueryVariableInfo(). That leaves little else, as
  57. * none of the remaining functions are actually ever called at runtime.
  58. * So let's just use a single spinlock to serialize all Runtime Services calls.
  59. */
  60. static DEFINE_SPINLOCK(efi_runtime_lock);
  61. /*
  62. * Some runtime services calls can be reentrant under NMI, even if the table
  63. * above says they are not. (source: UEFI Specification v2.4A)
  64. *
  65. * Table 32. Functions that may be called after Machine Check, INIT and NMI
  66. * +----------------------------+------------------------------------------+
  67. * | Function | Called after Machine Check, INIT and NMI |
  68. * +----------------------------+------------------------------------------+
  69. * | GetTime() | Yes, even if previously busy. |
  70. * | GetVariable() | Yes, even if previously busy |
  71. * | GetNextVariableName() | Yes, even if previously busy |
  72. * | QueryVariableInfo() | Yes, even if previously busy |
  73. * | SetVariable() | Yes, even if previously busy |
  74. * | UpdateCapsule() | Yes, even if previously busy |
  75. * | QueryCapsuleCapabilities() | Yes, even if previously busy |
  76. * | ResetSystem() | Yes, even if previously busy |
  77. * +----------------------------+------------------------------------------+
  78. *
  79. * In order to prevent deadlocks under NMI, the wrappers for these functions
  80. * may only grab the efi_runtime_lock or rtc_lock spinlocks if !efi_in_nmi().
  81. * However, not all of the services listed are reachable through NMI code paths,
  82. * so the the special handling as suggested by the UEFI spec is only implemented
  83. * for QueryVariableInfo() and SetVariable(), as these can be reached in NMI
  84. * context through efi_pstore_write().
  85. */
  86. /*
  87. * As per commit ef68c8f87ed1 ("x86: Serialize EFI time accesses on rtc_lock"),
  88. * the EFI specification requires that callers of the time related runtime
  89. * functions serialize with other CMOS accesses in the kernel, as the EFI time
  90. * functions may choose to also use the legacy CMOS RTC.
  91. */
  92. __weak DEFINE_SPINLOCK(rtc_lock);
  93. static efi_status_t virt_efi_get_time(efi_time_t *tm, efi_time_cap_t *tc)
  94. {
  95. unsigned long flags;
  96. efi_status_t status;
  97. spin_lock_irqsave(&rtc_lock, flags);
  98. spin_lock(&efi_runtime_lock);
  99. status = efi_call_virt(get_time, tm, tc);
  100. spin_unlock(&efi_runtime_lock);
  101. spin_unlock_irqrestore(&rtc_lock, flags);
  102. return status;
  103. }
  104. static efi_status_t virt_efi_set_time(efi_time_t *tm)
  105. {
  106. unsigned long flags;
  107. efi_status_t status;
  108. spin_lock_irqsave(&rtc_lock, flags);
  109. spin_lock(&efi_runtime_lock);
  110. status = efi_call_virt(set_time, tm);
  111. spin_unlock(&efi_runtime_lock);
  112. spin_unlock_irqrestore(&rtc_lock, flags);
  113. return status;
  114. }
  115. static efi_status_t virt_efi_get_wakeup_time(efi_bool_t *enabled,
  116. efi_bool_t *pending,
  117. efi_time_t *tm)
  118. {
  119. unsigned long flags;
  120. efi_status_t status;
  121. spin_lock_irqsave(&rtc_lock, flags);
  122. spin_lock(&efi_runtime_lock);
  123. status = efi_call_virt(get_wakeup_time, enabled, pending, tm);
  124. spin_unlock(&efi_runtime_lock);
  125. spin_unlock_irqrestore(&rtc_lock, flags);
  126. return status;
  127. }
  128. static efi_status_t virt_efi_set_wakeup_time(efi_bool_t enabled, efi_time_t *tm)
  129. {
  130. unsigned long flags;
  131. efi_status_t status;
  132. spin_lock_irqsave(&rtc_lock, flags);
  133. spin_lock(&efi_runtime_lock);
  134. status = efi_call_virt(set_wakeup_time, enabled, tm);
  135. spin_unlock(&efi_runtime_lock);
  136. spin_unlock_irqrestore(&rtc_lock, flags);
  137. return status;
  138. }
  139. static efi_status_t virt_efi_get_variable(efi_char16_t *name,
  140. efi_guid_t *vendor,
  141. u32 *attr,
  142. unsigned long *data_size,
  143. void *data)
  144. {
  145. unsigned long flags;
  146. efi_status_t status;
  147. spin_lock_irqsave(&efi_runtime_lock, flags);
  148. status = efi_call_virt(get_variable, name, vendor, attr, data_size,
  149. data);
  150. spin_unlock_irqrestore(&efi_runtime_lock, flags);
  151. return status;
  152. }
  153. static efi_status_t virt_efi_get_next_variable(unsigned long *name_size,
  154. efi_char16_t *name,
  155. efi_guid_t *vendor)
  156. {
  157. unsigned long flags;
  158. efi_status_t status;
  159. spin_lock_irqsave(&efi_runtime_lock, flags);
  160. status = efi_call_virt(get_next_variable, name_size, name, vendor);
  161. spin_unlock_irqrestore(&efi_runtime_lock, flags);
  162. return status;
  163. }
  164. static efi_status_t virt_efi_set_variable(efi_char16_t *name,
  165. efi_guid_t *vendor,
  166. u32 attr,
  167. unsigned long data_size,
  168. void *data)
  169. {
  170. unsigned long flags;
  171. efi_status_t status;
  172. spin_lock_irqsave(&efi_runtime_lock, flags);
  173. status = efi_call_virt(set_variable, name, vendor, attr, data_size,
  174. data);
  175. spin_unlock_irqrestore(&efi_runtime_lock, flags);
  176. return status;
  177. }
  178. static efi_status_t
  179. virt_efi_set_variable_nonblocking(efi_char16_t *name, efi_guid_t *vendor,
  180. u32 attr, unsigned long data_size,
  181. void *data)
  182. {
  183. unsigned long flags;
  184. efi_status_t status;
  185. if (!spin_trylock_irqsave(&efi_runtime_lock, flags))
  186. return EFI_NOT_READY;
  187. status = efi_call_virt(set_variable, name, vendor, attr, data_size,
  188. data);
  189. spin_unlock_irqrestore(&efi_runtime_lock, flags);
  190. return status;
  191. }
  192. static efi_status_t virt_efi_query_variable_info(u32 attr,
  193. u64 *storage_space,
  194. u64 *remaining_space,
  195. u64 *max_variable_size)
  196. {
  197. unsigned long flags;
  198. efi_status_t status;
  199. if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
  200. return EFI_UNSUPPORTED;
  201. spin_lock_irqsave(&efi_runtime_lock, flags);
  202. status = efi_call_virt(query_variable_info, attr, storage_space,
  203. remaining_space, max_variable_size);
  204. spin_unlock_irqrestore(&efi_runtime_lock, flags);
  205. return status;
  206. }
  207. static efi_status_t virt_efi_get_next_high_mono_count(u32 *count)
  208. {
  209. unsigned long flags;
  210. efi_status_t status;
  211. spin_lock_irqsave(&efi_runtime_lock, flags);
  212. status = efi_call_virt(get_next_high_mono_count, count);
  213. spin_unlock_irqrestore(&efi_runtime_lock, flags);
  214. return status;
  215. }
  216. static void virt_efi_reset_system(int reset_type,
  217. efi_status_t status,
  218. unsigned long data_size,
  219. efi_char16_t *data)
  220. {
  221. unsigned long flags;
  222. spin_lock_irqsave(&efi_runtime_lock, flags);
  223. __efi_call_virt(reset_system, reset_type, status, data_size, data);
  224. spin_unlock_irqrestore(&efi_runtime_lock, flags);
  225. }
  226. static efi_status_t virt_efi_update_capsule(efi_capsule_header_t **capsules,
  227. unsigned long count,
  228. unsigned long sg_list)
  229. {
  230. unsigned long flags;
  231. efi_status_t status;
  232. if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
  233. return EFI_UNSUPPORTED;
  234. spin_lock_irqsave(&efi_runtime_lock, flags);
  235. status = efi_call_virt(update_capsule, capsules, count, sg_list);
  236. spin_unlock_irqrestore(&efi_runtime_lock, flags);
  237. return status;
  238. }
  239. static efi_status_t virt_efi_query_capsule_caps(efi_capsule_header_t **capsules,
  240. unsigned long count,
  241. u64 *max_size,
  242. int *reset_type)
  243. {
  244. unsigned long flags;
  245. efi_status_t status;
  246. if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
  247. return EFI_UNSUPPORTED;
  248. spin_lock_irqsave(&efi_runtime_lock, flags);
  249. status = efi_call_virt(query_capsule_caps, capsules, count, max_size,
  250. reset_type);
  251. spin_unlock_irqrestore(&efi_runtime_lock, flags);
  252. return status;
  253. }
  254. void efi_native_runtime_setup(void)
  255. {
  256. efi.get_time = virt_efi_get_time;
  257. efi.set_time = virt_efi_set_time;
  258. efi.get_wakeup_time = virt_efi_get_wakeup_time;
  259. efi.set_wakeup_time = virt_efi_set_wakeup_time;
  260. efi.get_variable = virt_efi_get_variable;
  261. efi.get_next_variable = virt_efi_get_next_variable;
  262. efi.set_variable = virt_efi_set_variable;
  263. efi.set_variable_nonblocking = virt_efi_set_variable_nonblocking;
  264. efi.get_next_high_mono_count = virt_efi_get_next_high_mono_count;
  265. efi.reset_system = virt_efi_reset_system;
  266. efi.query_variable_info = virt_efi_query_variable_info;
  267. efi.update_capsule = virt_efi_update_capsule;
  268. efi.query_capsule_caps = virt_efi_query_capsule_caps;
  269. }