efi.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Extensible Firmware Interface
  4. *
  5. * Based on Extensible Firmware Interface Specification version 2.4
  6. *
  7. * Copyright (C) 2013, 2014 Linaro Ltd.
  8. */
  9. #include <linux/efi.h>
  10. #include <linux/init.h>
  11. #include <linux/kmemleak.h>
  12. #include <linux/screen_info.h>
  13. #include <linux/vmalloc.h>
  14. #include <asm/efi.h>
  15. #include <asm/stacktrace.h>
  16. static bool region_is_misaligned(const efi_memory_desc_t *md)
  17. {
  18. if (PAGE_SIZE == EFI_PAGE_SIZE)
  19. return false;
  20. return !PAGE_ALIGNED(md->phys_addr) ||
  21. !PAGE_ALIGNED(md->num_pages << EFI_PAGE_SHIFT);
  22. }
  23. /*
  24. * Only regions of type EFI_RUNTIME_SERVICES_CODE need to be
  25. * executable, everything else can be mapped with the XN bits
  26. * set. Also take the new (optional) RO/XP bits into account.
  27. */
  28. static __init pteval_t create_mapping_protection(efi_memory_desc_t *md)
  29. {
  30. u64 attr = md->attribute;
  31. u32 type = md->type;
  32. if (type == EFI_MEMORY_MAPPED_IO)
  33. return PROT_DEVICE_nGnRE;
  34. if (region_is_misaligned(md)) {
  35. static bool __initdata code_is_misaligned;
  36. /*
  37. * Regions that are not aligned to the OS page size cannot be
  38. * mapped with strict permissions, as those might interfere
  39. * with the permissions that are needed by the adjacent
  40. * region's mapping. However, if we haven't encountered any
  41. * misaligned runtime code regions so far, we can safely use
  42. * non-executable permissions for non-code regions.
  43. */
  44. code_is_misaligned |= (type == EFI_RUNTIME_SERVICES_CODE);
  45. return code_is_misaligned ? pgprot_val(PAGE_KERNEL_EXEC)
  46. : pgprot_val(PAGE_KERNEL);
  47. }
  48. /* R-- */
  49. if ((attr & (EFI_MEMORY_XP | EFI_MEMORY_RO)) ==
  50. (EFI_MEMORY_XP | EFI_MEMORY_RO))
  51. return pgprot_val(PAGE_KERNEL_RO);
  52. /* R-X */
  53. if (attr & EFI_MEMORY_RO)
  54. return pgprot_val(PAGE_KERNEL_ROX);
  55. /* RW- */
  56. if (((attr & (EFI_MEMORY_RP | EFI_MEMORY_WP | EFI_MEMORY_XP)) ==
  57. EFI_MEMORY_XP) ||
  58. type != EFI_RUNTIME_SERVICES_CODE)
  59. return pgprot_val(PAGE_KERNEL);
  60. /* RWX */
  61. return pgprot_val(PAGE_KERNEL_EXEC);
  62. }
  63. int __init efi_create_mapping(struct mm_struct *mm, efi_memory_desc_t *md)
  64. {
  65. pteval_t prot_val = create_mapping_protection(md);
  66. bool page_mappings_only = (md->type == EFI_RUNTIME_SERVICES_CODE ||
  67. md->type == EFI_RUNTIME_SERVICES_DATA);
  68. /*
  69. * If this region is not aligned to the page size used by the OS, the
  70. * mapping will be rounded outwards, and may end up sharing a page
  71. * frame with an adjacent runtime memory region. Given that the page
  72. * table descriptor covering the shared page will be rewritten when the
  73. * adjacent region gets mapped, we must avoid block mappings here so we
  74. * don't have to worry about splitting them when that happens.
  75. */
  76. if (region_is_misaligned(md))
  77. page_mappings_only = true;
  78. create_pgd_mapping(mm, md->phys_addr, md->virt_addr,
  79. md->num_pages << EFI_PAGE_SHIFT,
  80. __pgprot(prot_val | PTE_NG), page_mappings_only);
  81. return 0;
  82. }
  83. struct set_perm_data {
  84. const efi_memory_desc_t *md;
  85. bool has_bti;
  86. };
  87. static int __init set_permissions(pte_t *ptep, unsigned long addr, void *data)
  88. {
  89. struct set_perm_data *spd = data;
  90. const efi_memory_desc_t *md = spd->md;
  91. pte_t pte = __ptep_get(ptep);
  92. if (md->attribute & EFI_MEMORY_RO)
  93. pte = set_pte_bit(pte, __pgprot(PTE_RDONLY));
  94. if (md->attribute & EFI_MEMORY_XP)
  95. pte = set_pte_bit(pte, __pgprot(PTE_PXN));
  96. else if (system_supports_bti_kernel() && spd->has_bti)
  97. pte = set_pte_bit(pte, __pgprot(PTE_GP));
  98. __set_pte(ptep, pte);
  99. return 0;
  100. }
  101. int __init efi_set_mapping_permissions(struct mm_struct *mm,
  102. efi_memory_desc_t *md,
  103. bool has_bti)
  104. {
  105. struct set_perm_data data = { md, has_bti };
  106. BUG_ON(md->type != EFI_RUNTIME_SERVICES_CODE &&
  107. md->type != EFI_RUNTIME_SERVICES_DATA);
  108. if (region_is_misaligned(md))
  109. return 0;
  110. /*
  111. * Calling apply_to_page_range() is only safe on regions that are
  112. * guaranteed to be mapped down to pages. Since we are only called
  113. * for regions that have been mapped using efi_create_mapping() above
  114. * (and this is checked by the generic Memory Attributes table parsing
  115. * routines), there is no need to check that again here.
  116. */
  117. return apply_to_page_range(mm, md->virt_addr,
  118. md->num_pages << EFI_PAGE_SHIFT,
  119. set_permissions, &data);
  120. }
  121. /*
  122. * UpdateCapsule() depends on the system being shutdown via
  123. * ResetSystem().
  124. */
  125. bool efi_poweroff_required(void)
  126. {
  127. return efi_enabled(EFI_RUNTIME_SERVICES);
  128. }
  129. asmlinkage efi_status_t efi_handle_corrupted_x18(efi_status_t s, const char *f)
  130. {
  131. pr_err_ratelimited(FW_BUG "register x18 corrupted by EFI %s\n", f);
  132. return s;
  133. }
  134. static DEFINE_RAW_SPINLOCK(efi_rt_lock);
  135. void arch_efi_call_virt_setup(void)
  136. {
  137. efi_virtmap_load();
  138. __efi_fpsimd_begin();
  139. raw_spin_lock(&efi_rt_lock);
  140. }
  141. void arch_efi_call_virt_teardown(void)
  142. {
  143. raw_spin_unlock(&efi_rt_lock);
  144. __efi_fpsimd_end();
  145. efi_virtmap_unload();
  146. }
  147. asmlinkage u64 *efi_rt_stack_top __ro_after_init;
  148. asmlinkage efi_status_t __efi_rt_asm_recover(void);
  149. bool efi_runtime_fixup_exception(struct pt_regs *regs, const char *msg)
  150. {
  151. /* Check whether the exception occurred while running the firmware */
  152. if (!current_in_efi() || regs->pc >= TASK_SIZE_64)
  153. return false;
  154. pr_err(FW_BUG "Unable to handle %s in EFI runtime service\n", msg);
  155. add_taint(TAINT_FIRMWARE_WORKAROUND, LOCKDEP_STILL_OK);
  156. clear_bit(EFI_RUNTIME_SERVICES, &efi.flags);
  157. regs->regs[0] = EFI_ABORTED;
  158. regs->regs[30] = efi_rt_stack_top[-1];
  159. regs->pc = (u64)__efi_rt_asm_recover;
  160. if (IS_ENABLED(CONFIG_SHADOW_CALL_STACK))
  161. regs->regs[18] = efi_rt_stack_top[-2];
  162. return true;
  163. }
  164. /* EFI requires 8 KiB of stack space for runtime services */
  165. static_assert(THREAD_SIZE >= SZ_8K);
  166. static int __init arm64_efi_rt_init(void)
  167. {
  168. void *p;
  169. if (!efi_enabled(EFI_RUNTIME_SERVICES))
  170. return 0;
  171. p = __vmalloc_node(THREAD_SIZE, THREAD_ALIGN, GFP_KERNEL,
  172. NUMA_NO_NODE, &&l);
  173. l: if (!p) {
  174. pr_warn("Failed to allocate EFI runtime stack\n");
  175. clear_bit(EFI_RUNTIME_SERVICES, &efi.flags);
  176. return -ENOMEM;
  177. }
  178. kmemleak_not_leak(p);
  179. efi_rt_stack_top = p + THREAD_SIZE;
  180. return 0;
  181. }
  182. core_initcall(arm64_efi_rt_init);