efi.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef _ASM_X86_EFI_H
  3. #define _ASM_X86_EFI_H
  4. #include <asm/fpu/api.h>
  5. #include <asm/processor-flags.h>
  6. #include <asm/tlb.h>
  7. #include <asm/nospec-branch.h>
  8. #include <asm/mmu_context.h>
  9. #include <asm/ibt.h>
  10. #include <linux/build_bug.h>
  11. #include <linux/kernel.h>
  12. #include <linux/pgtable.h>
  13. extern unsigned long efi_fw_vendor, efi_config_table;
  14. extern unsigned long efi_mixed_mode_stack_pa;
  15. /*
  16. * We map the EFI regions needed for runtime services non-contiguously,
  17. * with preserved alignment on virtual addresses starting from -4G down
  18. * for a total max space of 64G. This way, we provide for stable runtime
  19. * services addresses across kernels so that a kexec'd kernel can still
  20. * use them.
  21. *
  22. * This is the main reason why we're doing stable VA mappings for RT
  23. * services.
  24. */
  25. #define EFI32_LOADER_SIGNATURE "EL32"
  26. #define EFI64_LOADER_SIGNATURE "EL64"
  27. #define ARCH_EFI_IRQ_FLAGS_MASK X86_EFLAGS_IF
  28. /*
  29. * The EFI services are called through variadic functions in many cases. These
  30. * functions are implemented in assembler and support only a fixed number of
  31. * arguments. The macros below allows us to check at build time that we don't
  32. * try to call them with too many arguments.
  33. *
  34. * __efi_nargs() will return the number of arguments if it is 7 or less, and
  35. * cause a BUILD_BUG otherwise. The limitations of the C preprocessor make it
  36. * impossible to calculate the exact number of arguments beyond some
  37. * pre-defined limit. The maximum number of arguments currently supported by
  38. * any of the thunks is 7, so this is good enough for now and can be extended
  39. * in the obvious way if we ever need more.
  40. */
  41. #define __efi_nargs(...) __efi_nargs_(__VA_ARGS__)
  42. #define __efi_nargs_(...) __efi_nargs__(0, ##__VA_ARGS__, \
  43. __efi_arg_sentinel(9), __efi_arg_sentinel(8), \
  44. __efi_arg_sentinel(7), __efi_arg_sentinel(6), \
  45. __efi_arg_sentinel(5), __efi_arg_sentinel(4), \
  46. __efi_arg_sentinel(3), __efi_arg_sentinel(2), \
  47. __efi_arg_sentinel(1), __efi_arg_sentinel(0))
  48. #define __efi_nargs__(_0, _1, _2, _3, _4, _5, _6, _7, _8, _9, n, ...) \
  49. __take_second_arg(n, \
  50. ({ BUILD_BUG_ON_MSG(1, "__efi_nargs limit exceeded"); 10; }))
  51. #define __efi_arg_sentinel(n) , n
  52. /*
  53. * __efi_nargs_check(f, n, ...) will cause a BUILD_BUG if the ellipsis
  54. * represents more than n arguments.
  55. */
  56. #define __efi_nargs_check(f, n, ...) \
  57. __efi_nargs_check_(f, __efi_nargs(__VA_ARGS__), n)
  58. #define __efi_nargs_check_(f, p, n) __efi_nargs_check__(f, p, n)
  59. #define __efi_nargs_check__(f, p, n) ({ \
  60. BUILD_BUG_ON_MSG( \
  61. (p) > (n), \
  62. #f " called with too many arguments (" #p ">" #n ")"); \
  63. })
  64. static inline void efi_fpu_begin(void)
  65. {
  66. /*
  67. * The UEFI calling convention (UEFI spec 2.3.2 and 2.3.4) requires
  68. * that FCW and MXCSR (64-bit) must be initialized prior to calling
  69. * UEFI code. (Oddly the spec does not require that the FPU stack
  70. * be empty.)
  71. */
  72. kernel_fpu_begin_mask(KFPU_387 | KFPU_MXCSR);
  73. }
  74. static inline void efi_fpu_end(void)
  75. {
  76. kernel_fpu_end();
  77. }
  78. #ifdef CONFIG_X86_32
  79. #define arch_efi_call_virt_setup() \
  80. ({ \
  81. efi_fpu_begin(); \
  82. firmware_restrict_branch_speculation_start(); \
  83. })
  84. #define arch_efi_call_virt_teardown() \
  85. ({ \
  86. firmware_restrict_branch_speculation_end(); \
  87. efi_fpu_end(); \
  88. })
  89. #else /* !CONFIG_X86_32 */
  90. #define EFI_LOADER_SIGNATURE "EL64"
  91. extern asmlinkage u64 __efi_call(void *fp, ...);
  92. #define efi_call(...) ({ \
  93. __efi_nargs_check(efi_call, 7, __VA_ARGS__); \
  94. __efi_call(__VA_ARGS__); \
  95. })
  96. #define arch_efi_call_virt_setup() \
  97. ({ \
  98. efi_sync_low_kernel_mappings(); \
  99. efi_fpu_begin(); \
  100. firmware_restrict_branch_speculation_start(); \
  101. efi_enter_mm(); \
  102. })
  103. #undef arch_efi_call_virt
  104. #define arch_efi_call_virt(p, f, args...) ({ \
  105. u64 ret, ibt = ibt_save(); \
  106. ret = efi_call((void *)p->f, args); \
  107. ibt_restore(ibt); \
  108. ret; \
  109. })
  110. #define arch_efi_call_virt_teardown() \
  111. ({ \
  112. efi_leave_mm(); \
  113. firmware_restrict_branch_speculation_end(); \
  114. efi_fpu_end(); \
  115. })
  116. #ifdef CONFIG_KASAN
  117. /*
  118. * CONFIG_KASAN may redefine memset to __memset. __memset function is present
  119. * only in kernel binary. Since the EFI stub linked into a separate binary it
  120. * doesn't have __memset(). So we should use standard memset from
  121. * arch/x86/boot/compressed/string.c. The same applies to memcpy and memmove.
  122. */
  123. #undef memcpy
  124. #undef memset
  125. #undef memmove
  126. #endif
  127. #endif /* CONFIG_X86_32 */
  128. extern int __init efi_memblock_x86_reserve_range(void);
  129. extern void __init efi_print_memmap(void);
  130. extern void __init efi_map_region(efi_memory_desc_t *md);
  131. extern void __init efi_map_region_fixed(efi_memory_desc_t *md);
  132. extern void efi_sync_low_kernel_mappings(void);
  133. extern int __init efi_alloc_page_tables(void);
  134. extern int __init efi_setup_page_tables(unsigned long pa_memmap, unsigned num_pages);
  135. extern void __init efi_runtime_update_mappings(void);
  136. extern void __init efi_dump_pagetable(void);
  137. extern void __init efi_apply_memmap_quirks(void);
  138. extern int __init efi_reuse_config(u64 tables, int nr_tables);
  139. extern void efi_delete_dummy_variable(void);
  140. extern void efi_crash_gracefully_on_page_fault(unsigned long phys_addr);
  141. extern void efi_free_boot_services(void);
  142. void efi_enter_mm(void);
  143. void efi_leave_mm(void);
  144. /* kexec external ABI */
  145. struct efi_setup_data {
  146. u64 fw_vendor;
  147. u64 __unused;
  148. u64 tables;
  149. u64 smbios;
  150. u64 reserved[8];
  151. };
  152. extern u64 efi_setup;
  153. #ifdef CONFIG_EFI
  154. extern efi_status_t __efi64_thunk(u32, ...);
  155. #define efi64_thunk(...) ({ \
  156. u64 __pad[3]; /* must have space for 3 args on the stack */ \
  157. __efi_nargs_check(efi64_thunk, 9, __VA_ARGS__); \
  158. __efi64_thunk(__VA_ARGS__, __pad); \
  159. })
  160. static inline bool efi_is_mixed(void)
  161. {
  162. if (!IS_ENABLED(CONFIG_EFI_MIXED))
  163. return false;
  164. return IS_ENABLED(CONFIG_X86_64) && !efi_enabled(EFI_64BIT);
  165. }
  166. static inline bool efi_runtime_supported(void)
  167. {
  168. if (IS_ENABLED(CONFIG_X86_64) == efi_enabled(EFI_64BIT))
  169. return true;
  170. return IS_ENABLED(CONFIG_EFI_MIXED);
  171. }
  172. extern void parse_efi_setup(u64 phys_addr, u32 data_len);
  173. extern void efi_thunk_runtime_setup(void);
  174. efi_status_t efi_set_virtual_address_map(unsigned long memory_map_size,
  175. unsigned long descriptor_size,
  176. u32 descriptor_version,
  177. efi_memory_desc_t *virtual_map,
  178. unsigned long systab_phys);
  179. /* arch specific definitions used by the stub code */
  180. #ifdef CONFIG_EFI_MIXED
  181. #define ARCH_HAS_EFISTUB_WRAPPERS
  182. static inline bool efi_is_64bit(void)
  183. {
  184. extern const bool efi_is64;
  185. return efi_is64;
  186. }
  187. static inline bool efi_is_native(void)
  188. {
  189. return efi_is_64bit();
  190. }
  191. #define efi_mixed_mode_cast(attr) \
  192. __builtin_choose_expr( \
  193. __builtin_types_compatible_p(u32, __typeof__(attr)), \
  194. (unsigned long)(attr), (attr))
  195. #define efi_table_attr(inst, attr) \
  196. (efi_is_native() \
  197. ? inst->attr \
  198. : (__typeof__(inst->attr)) \
  199. efi_mixed_mode_cast(inst->mixed_mode.attr))
  200. /*
  201. * The following macros allow translating arguments if necessary from native to
  202. * mixed mode. The use case for this is to initialize the upper 32 bits of
  203. * output parameters, and where the 32-bit method requires a 64-bit argument,
  204. * which must be split up into two arguments to be thunked properly.
  205. *
  206. * As examples, the AllocatePool boot service returns the address of the
  207. * allocation, but it will not set the high 32 bits of the address. To ensure
  208. * that the full 64-bit address is initialized, we zero-init the address before
  209. * calling the thunk.
  210. *
  211. * The FreePages boot service takes a 64-bit physical address even in 32-bit
  212. * mode. For the thunk to work correctly, a native 64-bit call of
  213. * free_pages(addr, size)
  214. * must be translated to
  215. * efi64_thunk(free_pages, addr & U32_MAX, addr >> 32, size)
  216. * so that the two 32-bit halves of addr get pushed onto the stack separately.
  217. */
  218. static inline void *efi64_zero_upper(void *p)
  219. {
  220. ((u32 *)p)[1] = 0;
  221. return p;
  222. }
  223. static inline u32 efi64_convert_status(efi_status_t status)
  224. {
  225. return (u32)(status | (u64)status >> 32);
  226. }
  227. #define __efi64_split(val) (val) & U32_MAX, (u64)(val) >> 32
  228. #define __efi64_argmap_free_pages(addr, size) \
  229. ((addr), 0, (size))
  230. #define __efi64_argmap_get_memory_map(mm_size, mm, key, size, ver) \
  231. ((mm_size), (mm), efi64_zero_upper(key), efi64_zero_upper(size), (ver))
  232. #define __efi64_argmap_allocate_pool(type, size, buffer) \
  233. ((type), (size), efi64_zero_upper(buffer))
  234. #define __efi64_argmap_create_event(type, tpl, f, c, event) \
  235. ((type), (tpl), (f), (c), efi64_zero_upper(event))
  236. #define __efi64_argmap_set_timer(event, type, time) \
  237. ((event), (type), lower_32_bits(time), upper_32_bits(time))
  238. #define __efi64_argmap_wait_for_event(num, event, index) \
  239. ((num), (event), efi64_zero_upper(index))
  240. #define __efi64_argmap_handle_protocol(handle, protocol, interface) \
  241. ((handle), (protocol), efi64_zero_upper(interface))
  242. #define __efi64_argmap_locate_protocol(protocol, reg, interface) \
  243. ((protocol), (reg), efi64_zero_upper(interface))
  244. #define __efi64_argmap_locate_device_path(protocol, path, handle) \
  245. ((protocol), (path), efi64_zero_upper(handle))
  246. #define __efi64_argmap_exit(handle, status, size, data) \
  247. ((handle), efi64_convert_status(status), (size), (data))
  248. /* PCI I/O */
  249. #define __efi64_argmap_get_location(protocol, seg, bus, dev, func) \
  250. ((protocol), efi64_zero_upper(seg), efi64_zero_upper(bus), \
  251. efi64_zero_upper(dev), efi64_zero_upper(func))
  252. /* LoadFile */
  253. #define __efi64_argmap_load_file(protocol, path, policy, bufsize, buf) \
  254. ((protocol), (path), (policy), efi64_zero_upper(bufsize), (buf))
  255. /* Graphics Output Protocol */
  256. #define __efi64_argmap_query_mode(gop, mode, size, info) \
  257. ((gop), (mode), efi64_zero_upper(size), efi64_zero_upper(info))
  258. /* TCG2 protocol */
  259. #define __efi64_argmap_hash_log_extend_event(prot, fl, addr, size, ev) \
  260. ((prot), (fl), 0ULL, (u64)(addr), 0ULL, (u64)(size), 0ULL, ev)
  261. /* DXE services */
  262. #define __efi64_argmap_get_memory_space_descriptor(phys, desc) \
  263. (__efi64_split(phys), (desc))
  264. #define __efi64_argmap_set_memory_space_attributes(phys, size, flags) \
  265. (__efi64_split(phys), __efi64_split(size), __efi64_split(flags))
  266. /*
  267. * The macros below handle the plumbing for the argument mapping. To add a
  268. * mapping for a specific EFI method, simply define a macro
  269. * __efi64_argmap_<method name>, following the examples above.
  270. */
  271. #define __efi64_thunk_map(inst, func, ...) \
  272. efi64_thunk(inst->mixed_mode.func, \
  273. __efi64_argmap(__efi64_argmap_ ## func(__VA_ARGS__), \
  274. (__VA_ARGS__)))
  275. #define __efi64_argmap(mapped, args) \
  276. __PASTE(__efi64_argmap__, __efi_nargs(__efi_eat mapped))(mapped, args)
  277. #define __efi64_argmap__0(mapped, args) __efi_eval mapped
  278. #define __efi64_argmap__1(mapped, args) __efi_eval args
  279. #define __efi_eat(...)
  280. #define __efi_eval(...) __VA_ARGS__
  281. /* The three macros below handle dispatching via the thunk if needed */
  282. #define efi_call_proto(inst, func, ...) \
  283. (efi_is_native() \
  284. ? inst->func(inst, ##__VA_ARGS__) \
  285. : __efi64_thunk_map(inst, func, inst, ##__VA_ARGS__))
  286. #define efi_bs_call(func, ...) \
  287. (efi_is_native() \
  288. ? efi_system_table->boottime->func(__VA_ARGS__) \
  289. : __efi64_thunk_map(efi_table_attr(efi_system_table, \
  290. boottime), \
  291. func, __VA_ARGS__))
  292. #define efi_rt_call(func, ...) \
  293. (efi_is_native() \
  294. ? efi_system_table->runtime->func(__VA_ARGS__) \
  295. : __efi64_thunk_map(efi_table_attr(efi_system_table, \
  296. runtime), \
  297. func, __VA_ARGS__))
  298. #define efi_dxe_call(func, ...) \
  299. (efi_is_native() \
  300. ? efi_dxe_table->func(__VA_ARGS__) \
  301. : __efi64_thunk_map(efi_dxe_table, func, __VA_ARGS__))
  302. #else /* CONFIG_EFI_MIXED */
  303. static inline bool efi_is_64bit(void)
  304. {
  305. return IS_ENABLED(CONFIG_X86_64);
  306. }
  307. #endif /* CONFIG_EFI_MIXED */
  308. extern bool efi_reboot_required(void);
  309. extern bool efi_is_table_address(unsigned long phys_addr);
  310. extern void efi_reserve_boot_services(void);
  311. #else
  312. static inline void parse_efi_setup(u64 phys_addr, u32 data_len) {}
  313. static inline bool efi_reboot_required(void)
  314. {
  315. return false;
  316. }
  317. static inline bool efi_is_table_address(unsigned long phys_addr)
  318. {
  319. return false;
  320. }
  321. static inline void efi_reserve_boot_services(void)
  322. {
  323. }
  324. #endif /* CONFIG_EFI */
  325. #ifdef CONFIG_EFI_FAKE_MEMMAP
  326. extern void __init efi_fake_memmap_early(void);
  327. #else
  328. static inline void efi_fake_memmap_early(void)
  329. {
  330. }
  331. #endif
  332. #define arch_ima_efi_boot_mode \
  333. ({ extern struct boot_params boot_params; boot_params.secure_boot; })
  334. #endif /* _ASM_X86_EFI_H */