0001
0002 #ifndef _ASM_X86_EFI_H
0003 #define _ASM_X86_EFI_H
0004
0005 #include <asm/fpu/api.h>
0006 #include <asm/processor-flags.h>
0007 #include <asm/tlb.h>
0008 #include <asm/nospec-branch.h>
0009 #include <asm/mmu_context.h>
0010 #include <asm/ibt.h>
0011 #include <linux/build_bug.h>
0012 #include <linux/kernel.h>
0013 #include <linux/pgtable.h>
0014
0015 extern unsigned long efi_fw_vendor, efi_config_table;
0016 extern unsigned long efi_mixed_mode_stack_pa;
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029 #define EFI32_LOADER_SIGNATURE "EL32"
0030 #define EFI64_LOADER_SIGNATURE "EL64"
0031
0032 #define ARCH_EFI_IRQ_FLAGS_MASK X86_EFLAGS_IF
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042
0043
0044
0045
0046
0047
0048 #define __efi_nargs(...) __efi_nargs_(__VA_ARGS__)
0049 #define __efi_nargs_(...) __efi_nargs__(0, ##__VA_ARGS__, \
0050 __efi_arg_sentinel(9), __efi_arg_sentinel(8), \
0051 __efi_arg_sentinel(7), __efi_arg_sentinel(6), \
0052 __efi_arg_sentinel(5), __efi_arg_sentinel(4), \
0053 __efi_arg_sentinel(3), __efi_arg_sentinel(2), \
0054 __efi_arg_sentinel(1), __efi_arg_sentinel(0))
0055 #define __efi_nargs__(_0, _1, _2, _3, _4, _5, _6, _7, _8, _9, n, ...) \
0056 __take_second_arg(n, \
0057 ({ BUILD_BUG_ON_MSG(1, "__efi_nargs limit exceeded"); 10; }))
0058 #define __efi_arg_sentinel(n) , n
0059
0060
0061
0062
0063
0064
0065 #define __efi_nargs_check(f, n, ...) \
0066 __efi_nargs_check_(f, __efi_nargs(__VA_ARGS__), n)
0067 #define __efi_nargs_check_(f, p, n) __efi_nargs_check__(f, p, n)
0068 #define __efi_nargs_check__(f, p, n) ({ \
0069 BUILD_BUG_ON_MSG( \
0070 (p) > (n), \
0071 #f " called with too many arguments (" #p ">" #n ")"); \
0072 })
0073
0074 static inline void efi_fpu_begin(void)
0075 {
0076
0077
0078
0079
0080
0081
0082 kernel_fpu_begin_mask(KFPU_387 | KFPU_MXCSR);
0083 }
0084
0085 static inline void efi_fpu_end(void)
0086 {
0087 kernel_fpu_end();
0088 }
0089
0090 #ifdef CONFIG_X86_32
0091 #define arch_efi_call_virt_setup() \
0092 ({ \
0093 efi_fpu_begin(); \
0094 firmware_restrict_branch_speculation_start(); \
0095 })
0096
0097 #define arch_efi_call_virt_teardown() \
0098 ({ \
0099 firmware_restrict_branch_speculation_end(); \
0100 efi_fpu_end(); \
0101 })
0102
0103 #else
0104
0105 #define EFI_LOADER_SIGNATURE "EL64"
0106
0107 extern asmlinkage u64 __efi_call(void *fp, ...);
0108
0109 #define efi_call(...) ({ \
0110 __efi_nargs_check(efi_call, 7, __VA_ARGS__); \
0111 __efi_call(__VA_ARGS__); \
0112 })
0113
0114 #define arch_efi_call_virt_setup() \
0115 ({ \
0116 efi_sync_low_kernel_mappings(); \
0117 efi_fpu_begin(); \
0118 firmware_restrict_branch_speculation_start(); \
0119 efi_enter_mm(); \
0120 })
0121
0122 #undef arch_efi_call_virt
0123 #define arch_efi_call_virt(p, f, args...) ({ \
0124 u64 ret, ibt = ibt_save(); \
0125 ret = efi_call((void *)p->f, args); \
0126 ibt_restore(ibt); \
0127 ret; \
0128 })
0129
0130 #define arch_efi_call_virt_teardown() \
0131 ({ \
0132 efi_leave_mm(); \
0133 firmware_restrict_branch_speculation_end(); \
0134 efi_fpu_end(); \
0135 })
0136
0137 #ifdef CONFIG_KASAN
0138
0139
0140
0141
0142
0143
0144 #undef memcpy
0145 #undef memset
0146 #undef memmove
0147 #endif
0148
0149 #endif
0150
0151 extern int __init efi_memblock_x86_reserve_range(void);
0152 extern void __init efi_print_memmap(void);
0153 extern void __init efi_map_region(efi_memory_desc_t *md);
0154 extern void __init efi_map_region_fixed(efi_memory_desc_t *md);
0155 extern void efi_sync_low_kernel_mappings(void);
0156 extern int __init efi_alloc_page_tables(void);
0157 extern int __init efi_setup_page_tables(unsigned long pa_memmap, unsigned num_pages);
0158 extern void __init efi_runtime_update_mappings(void);
0159 extern void __init efi_dump_pagetable(void);
0160 extern void __init efi_apply_memmap_quirks(void);
0161 extern int __init efi_reuse_config(u64 tables, int nr_tables);
0162 extern void efi_delete_dummy_variable(void);
0163 extern void efi_crash_gracefully_on_page_fault(unsigned long phys_addr);
0164 extern void efi_free_boot_services(void);
0165
0166 void efi_enter_mm(void);
0167 void efi_leave_mm(void);
0168
0169
0170 struct efi_setup_data {
0171 u64 fw_vendor;
0172 u64 __unused;
0173 u64 tables;
0174 u64 smbios;
0175 u64 reserved[8];
0176 };
0177
0178 extern u64 efi_setup;
0179
0180 #ifdef CONFIG_EFI
0181 extern efi_status_t __efi64_thunk(u32, ...);
0182
0183 #define efi64_thunk(...) ({ \
0184 u64 __pad[3]; \
0185 __efi_nargs_check(efi64_thunk, 9, __VA_ARGS__); \
0186 __efi64_thunk(__VA_ARGS__, __pad); \
0187 })
0188
0189 static inline bool efi_is_mixed(void)
0190 {
0191 if (!IS_ENABLED(CONFIG_EFI_MIXED))
0192 return false;
0193 return IS_ENABLED(CONFIG_X86_64) && !efi_enabled(EFI_64BIT);
0194 }
0195
0196 static inline bool efi_runtime_supported(void)
0197 {
0198 if (IS_ENABLED(CONFIG_X86_64) == efi_enabled(EFI_64BIT))
0199 return true;
0200
0201 return IS_ENABLED(CONFIG_EFI_MIXED);
0202 }
0203
0204 extern void parse_efi_setup(u64 phys_addr, u32 data_len);
0205
0206 extern void efi_thunk_runtime_setup(void);
0207 efi_status_t efi_set_virtual_address_map(unsigned long memory_map_size,
0208 unsigned long descriptor_size,
0209 u32 descriptor_version,
0210 efi_memory_desc_t *virtual_map,
0211 unsigned long systab_phys);
0212
0213
0214
0215 #ifdef CONFIG_EFI_MIXED
0216
0217 #define ARCH_HAS_EFISTUB_WRAPPERS
0218
0219 static inline bool efi_is_64bit(void)
0220 {
0221 extern const bool efi_is64;
0222
0223 return efi_is64;
0224 }
0225
0226 static inline bool efi_is_native(void)
0227 {
0228 return efi_is_64bit();
0229 }
0230
0231 #define efi_mixed_mode_cast(attr) \
0232 __builtin_choose_expr( \
0233 __builtin_types_compatible_p(u32, __typeof__(attr)), \
0234 (unsigned long)(attr), (attr))
0235
0236 #define efi_table_attr(inst, attr) \
0237 (efi_is_native() \
0238 ? inst->attr \
0239 : (__typeof__(inst->attr)) \
0240 efi_mixed_mode_cast(inst->mixed_mode.attr))
0241
0242
0243
0244
0245
0246
0247
0248
0249
0250
0251
0252
0253
0254
0255
0256
0257
0258
0259
0260
0261 static inline void *efi64_zero_upper(void *p)
0262 {
0263 ((u32 *)p)[1] = 0;
0264 return p;
0265 }
0266
0267 static inline u32 efi64_convert_status(efi_status_t status)
0268 {
0269 return (u32)(status | (u64)status >> 32);
0270 }
0271
0272 #define __efi64_split(val) (val) & U32_MAX, (u64)(val) >> 32
0273
0274 #define __efi64_argmap_free_pages(addr, size) \
0275 ((addr), 0, (size))
0276
0277 #define __efi64_argmap_get_memory_map(mm_size, mm, key, size, ver) \
0278 ((mm_size), (mm), efi64_zero_upper(key), efi64_zero_upper(size), (ver))
0279
0280 #define __efi64_argmap_allocate_pool(type, size, buffer) \
0281 ((type), (size), efi64_zero_upper(buffer))
0282
0283 #define __efi64_argmap_create_event(type, tpl, f, c, event) \
0284 ((type), (tpl), (f), (c), efi64_zero_upper(event))
0285
0286 #define __efi64_argmap_set_timer(event, type, time) \
0287 ((event), (type), lower_32_bits(time), upper_32_bits(time))
0288
0289 #define __efi64_argmap_wait_for_event(num, event, index) \
0290 ((num), (event), efi64_zero_upper(index))
0291
0292 #define __efi64_argmap_handle_protocol(handle, protocol, interface) \
0293 ((handle), (protocol), efi64_zero_upper(interface))
0294
0295 #define __efi64_argmap_locate_protocol(protocol, reg, interface) \
0296 ((protocol), (reg), efi64_zero_upper(interface))
0297
0298 #define __efi64_argmap_locate_device_path(protocol, path, handle) \
0299 ((protocol), (path), efi64_zero_upper(handle))
0300
0301 #define __efi64_argmap_exit(handle, status, size, data) \
0302 ((handle), efi64_convert_status(status), (size), (data))
0303
0304
0305 #define __efi64_argmap_get_location(protocol, seg, bus, dev, func) \
0306 ((protocol), efi64_zero_upper(seg), efi64_zero_upper(bus), \
0307 efi64_zero_upper(dev), efi64_zero_upper(func))
0308
0309
0310 #define __efi64_argmap_load_file(protocol, path, policy, bufsize, buf) \
0311 ((protocol), (path), (policy), efi64_zero_upper(bufsize), (buf))
0312
0313
0314 #define __efi64_argmap_query_mode(gop, mode, size, info) \
0315 ((gop), (mode), efi64_zero_upper(size), efi64_zero_upper(info))
0316
0317
0318 #define __efi64_argmap_hash_log_extend_event(prot, fl, addr, size, ev) \
0319 ((prot), (fl), 0ULL, (u64)(addr), 0ULL, (u64)(size), 0ULL, ev)
0320
0321
0322 #define __efi64_argmap_get_memory_space_descriptor(phys, desc) \
0323 (__efi64_split(phys), (desc))
0324
0325 #define __efi64_argmap_set_memory_space_attributes(phys, size, flags) \
0326 (__efi64_split(phys), __efi64_split(size), __efi64_split(flags))
0327
0328
0329
0330
0331
0332
0333
0334 #define __efi64_thunk_map(inst, func, ...) \
0335 efi64_thunk(inst->mixed_mode.func, \
0336 __efi64_argmap(__efi64_argmap_ ## func(__VA_ARGS__), \
0337 (__VA_ARGS__)))
0338
0339 #define __efi64_argmap(mapped, args) \
0340 __PASTE(__efi64_argmap__, __efi_nargs(__efi_eat mapped))(mapped, args)
0341 #define __efi64_argmap__0(mapped, args) __efi_eval mapped
0342 #define __efi64_argmap__1(mapped, args) __efi_eval args
0343
0344 #define __efi_eat(...)
0345 #define __efi_eval(...) __VA_ARGS__
0346
0347
0348
0349 #define efi_call_proto(inst, func, ...) \
0350 (efi_is_native() \
0351 ? inst->func(inst, ##__VA_ARGS__) \
0352 : __efi64_thunk_map(inst, func, inst, ##__VA_ARGS__))
0353
0354 #define efi_bs_call(func, ...) \
0355 (efi_is_native() \
0356 ? efi_system_table->boottime->func(__VA_ARGS__) \
0357 : __efi64_thunk_map(efi_table_attr(efi_system_table, \
0358 boottime), \
0359 func, __VA_ARGS__))
0360
0361 #define efi_rt_call(func, ...) \
0362 (efi_is_native() \
0363 ? efi_system_table->runtime->func(__VA_ARGS__) \
0364 : __efi64_thunk_map(efi_table_attr(efi_system_table, \
0365 runtime), \
0366 func, __VA_ARGS__))
0367
0368 #define efi_dxe_call(func, ...) \
0369 (efi_is_native() \
0370 ? efi_dxe_table->func(__VA_ARGS__) \
0371 : __efi64_thunk_map(efi_dxe_table, func, __VA_ARGS__))
0372
0373 #else
0374
0375 static inline bool efi_is_64bit(void)
0376 {
0377 return IS_ENABLED(CONFIG_X86_64);
0378 }
0379
0380 #endif
0381
0382 extern bool efi_reboot_required(void);
0383 extern bool efi_is_table_address(unsigned long phys_addr);
0384
0385 extern void efi_reserve_boot_services(void);
0386 #else
0387 static inline void parse_efi_setup(u64 phys_addr, u32 data_len) {}
0388 static inline bool efi_reboot_required(void)
0389 {
0390 return false;
0391 }
0392 static inline bool efi_is_table_address(unsigned long phys_addr)
0393 {
0394 return false;
0395 }
0396 static inline void efi_reserve_boot_services(void)
0397 {
0398 }
0399 #endif
0400
0401 #ifdef CONFIG_EFI_FAKE_MEMMAP
0402 extern void __init efi_fake_memmap_early(void);
0403 #else
0404 static inline void efi_fake_memmap_early(void)
0405 {
0406 }
0407 #endif
0408
0409 #define arch_ima_efi_boot_mode \
0410 ({ extern struct boot_params boot_params; boot_params.secure_boot; })
0411
0412 #endif