Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0 */
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  * We map the EFI regions needed for runtime services non-contiguously,
0020  * with preserved alignment on virtual addresses starting from -4G down
0021  * for a total max space of 64G. This way, we provide for stable runtime
0022  * services addresses across kernels so that a kexec'd kernel can still
0023  * use them.
0024  *
0025  * This is the main reason why we're doing stable VA mappings for RT
0026  * services.
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  * The EFI services are called through variadic functions in many cases. These
0036  * functions are implemented in assembler and support only a fixed number of
0037  * arguments. The macros below allows us to check at build time that we don't
0038  * try to call them with too many arguments.
0039  *
0040  * __efi_nargs() will return the number of arguments if it is 7 or less, and
0041  * cause a BUILD_BUG otherwise. The limitations of the C preprocessor make it
0042  * impossible to calculate the exact number of arguments beyond some
0043  * pre-defined limit. The maximum number of arguments currently supported by
0044  * any of the thunks is 7, so this is good enough for now and can be extended
0045  * in the obvious way if we ever need more.
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  * __efi_nargs_check(f, n, ...) will cause a BUILD_BUG if the ellipsis
0062  * represents more than n arguments.
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      * The UEFI calling convention (UEFI spec 2.3.2 and 2.3.4) requires
0078      * that FCW and MXCSR (64-bit) must be initialized prior to calling
0079      * UEFI code.  (Oddly the spec does not require that the FPU stack
0080      * be empty.)
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 /* !CONFIG_X86_32 */
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  * CONFIG_KASAN may redefine memset to __memset.  __memset function is present
0140  * only in kernel binary.  Since the EFI stub linked into a separate binary it
0141  * doesn't have __memset().  So we should use standard memset from
0142  * arch/x86/boot/compressed/string.c.  The same applies to memcpy and memmove.
0143  */
0144 #undef memcpy
0145 #undef memset
0146 #undef memmove
0147 #endif
0148 
0149 #endif /* CONFIG_X86_32 */
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 /* kexec external ABI */
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]; /* must have space for 3 args on the stack */ \
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 /* arch specific definitions used by the stub code */
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  * The following macros allow translating arguments if necessary from native to
0244  * mixed mode. The use case for this is to initialize the upper 32 bits of
0245  * output parameters, and where the 32-bit method requires a 64-bit argument,
0246  * which must be split up into two arguments to be thunked properly.
0247  *
0248  * As examples, the AllocatePool boot service returns the address of the
0249  * allocation, but it will not set the high 32 bits of the address. To ensure
0250  * that the full 64-bit address is initialized, we zero-init the address before
0251  * calling the thunk.
0252  *
0253  * The FreePages boot service takes a 64-bit physical address even in 32-bit
0254  * mode. For the thunk to work correctly, a native 64-bit call of
0255  *  free_pages(addr, size)
0256  * must be translated to
0257  *  efi64_thunk(free_pages, addr & U32_MAX, addr >> 32, size)
0258  * so that the two 32-bit halves of addr get pushed onto the stack separately.
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 /* PCI I/O */
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 /* LoadFile */
0310 #define __efi64_argmap_load_file(protocol, path, policy, bufsize, buf)  \
0311     ((protocol), (path), (policy), efi64_zero_upper(bufsize), (buf))
0312 
0313 /* Graphics Output Protocol */
0314 #define __efi64_argmap_query_mode(gop, mode, size, info)        \
0315     ((gop), (mode), efi64_zero_upper(size), efi64_zero_upper(info))
0316 
0317 /* TCG2 protocol */
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 /* DXE services */
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  * The macros below handle the plumbing for the argument mapping. To add a
0330  * mapping for a specific EFI method, simply define a macro
0331  * __efi64_argmap_<method name>, following the examples above.
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 /* The three macros below handle dispatching via the thunk if needed */
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 /* CONFIG_EFI_MIXED */
0374 
0375 static inline bool efi_is_64bit(void)
0376 {
0377     return IS_ENABLED(CONFIG_X86_64);
0378 }
0379 
0380 #endif /* CONFIG_EFI_MIXED */
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 /* CONFIG_EFI */
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 /* _ASM_X86_EFI_H */