efi.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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 <asm/efi.h>
  12. #include <asm/stacktrace.h>
  13. static bool region_is_misaligned(const efi_memory_desc_t *md)
  14. {
  15. if (PAGE_SIZE == EFI_PAGE_SIZE)
  16. return false;
  17. return !PAGE_ALIGNED(md->phys_addr) ||
  18. !PAGE_ALIGNED(md->num_pages << EFI_PAGE_SHIFT);
  19. }
  20. /*
  21. * Only regions of type EFI_RUNTIME_SERVICES_CODE need to be
  22. * executable, everything else can be mapped with the XN bits
  23. * set. Also take the new (optional) RO/XP bits into account.
  24. */
  25. static __init pteval_t create_mapping_protection(efi_memory_desc_t *md)
  26. {
  27. u64 attr = md->attribute;
  28. u32 type = md->type;
  29. if (type == EFI_MEMORY_MAPPED_IO)
  30. return PROT_DEVICE_nGnRE;
  31. if (region_is_misaligned(md)) {
  32. static bool __initdata code_is_misaligned;
  33. /*
  34. * Regions that are not aligned to the OS page size cannot be
  35. * mapped with strict permissions, as those might interfere
  36. * with the permissions that are needed by the adjacent
  37. * region's mapping. However, if we haven't encountered any
  38. * misaligned runtime code regions so far, we can safely use
  39. * non-executable permissions for non-code regions.
  40. */
  41. code_is_misaligned |= (type == EFI_RUNTIME_SERVICES_CODE);
  42. return code_is_misaligned ? pgprot_val(PAGE_KERNEL_EXEC)
  43. : pgprot_val(PAGE_KERNEL);
  44. }
  45. /* R-- */
  46. if ((attr & (EFI_MEMORY_XP | EFI_MEMORY_RO)) ==
  47. (EFI_MEMORY_XP | EFI_MEMORY_RO))
  48. return pgprot_val(PAGE_KERNEL_RO);
  49. /* R-X */
  50. if (attr & EFI_MEMORY_RO)
  51. return pgprot_val(PAGE_KERNEL_ROX);
  52. /* RW- */
  53. if (((attr & (EFI_MEMORY_RP | EFI_MEMORY_WP | EFI_MEMORY_XP)) ==
  54. EFI_MEMORY_XP) ||
  55. type != EFI_RUNTIME_SERVICES_CODE)
  56. return pgprot_val(PAGE_KERNEL);
  57. /* RWX */
  58. return pgprot_val(PAGE_KERNEL_EXEC);
  59. }
  60. /* we will fill this structure from the stub, so don't put it in .bss */
  61. struct screen_info screen_info __section(".data");
  62. EXPORT_SYMBOL(screen_info);
  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. static int __init set_permissions(pte_t *ptep, unsigned long addr, void *data)
  84. {
  85. efi_memory_desc_t *md = data;
  86. pte_t pte = READ_ONCE(*ptep);
  87. if (md->attribute & EFI_MEMORY_RO)
  88. pte = set_pte_bit(pte, __pgprot(PTE_RDONLY));
  89. if (md->attribute & EFI_MEMORY_XP)
  90. pte = set_pte_bit(pte, __pgprot(PTE_PXN));
  91. set_pte(ptep, pte);
  92. return 0;
  93. }
  94. int __init efi_set_mapping_permissions(struct mm_struct *mm,
  95. efi_memory_desc_t *md)
  96. {
  97. BUG_ON(md->type != EFI_RUNTIME_SERVICES_CODE &&
  98. md->type != EFI_RUNTIME_SERVICES_DATA);
  99. if (region_is_misaligned(md))
  100. return 0;
  101. /*
  102. * Calling apply_to_page_range() is only safe on regions that are
  103. * guaranteed to be mapped down to pages. Since we are only called
  104. * for regions that have been mapped using efi_create_mapping() above
  105. * (and this is checked by the generic Memory Attributes table parsing
  106. * routines), there is no need to check that again here.
  107. */
  108. return apply_to_page_range(mm, md->virt_addr,
  109. md->num_pages << EFI_PAGE_SHIFT,
  110. set_permissions, md);
  111. }
  112. /*
  113. * UpdateCapsule() depends on the system being shutdown via
  114. * ResetSystem().
  115. */
  116. bool efi_poweroff_required(void)
  117. {
  118. return efi_enabled(EFI_RUNTIME_SERVICES);
  119. }
  120. asmlinkage efi_status_t efi_handle_corrupted_x18(efi_status_t s, const char *f)
  121. {
  122. pr_err_ratelimited(FW_BUG "register x18 corrupted by EFI %s\n", f);
  123. return s;
  124. }
  125. DEFINE_RAW_SPINLOCK(efi_rt_lock);
  126. asmlinkage u64 *efi_rt_stack_top __ro_after_init;
  127. asmlinkage efi_status_t __efi_rt_asm_recover(void);
  128. bool efi_runtime_fixup_exception(struct pt_regs *regs, const char *msg)
  129. {
  130. /* Check whether the exception occurred while running the firmware */
  131. if (!current_in_efi() || regs->pc >= TASK_SIZE_64)
  132. return false;
  133. pr_err(FW_BUG "Unable to handle %s in EFI runtime service\n", msg);
  134. add_taint(TAINT_FIRMWARE_WORKAROUND, LOCKDEP_STILL_OK);
  135. clear_bit(EFI_RUNTIME_SERVICES, &efi.flags);
  136. regs->regs[0] = EFI_ABORTED;
  137. regs->regs[30] = efi_rt_stack_top[-1];
  138. regs->pc = (u64)__efi_rt_asm_recover;
  139. if (IS_ENABLED(CONFIG_SHADOW_CALL_STACK))
  140. regs->regs[18] = efi_rt_stack_top[-2];
  141. return true;
  142. }
  143. /* EFI requires 8 KiB of stack space for runtime services */
  144. static_assert(THREAD_SIZE >= SZ_8K);
  145. static int __init arm64_efi_rt_init(void)
  146. {
  147. void *p;
  148. if (!efi_enabled(EFI_RUNTIME_SERVICES))
  149. return 0;
  150. p = __vmalloc_node(THREAD_SIZE, THREAD_ALIGN, GFP_KERNEL,
  151. NUMA_NO_NODE, &&l);
  152. l: if (!p) {
  153. pr_warn("Failed to allocate EFI runtime stack\n");
  154. clear_bit(EFI_RUNTIME_SERVICES, &efi.flags);
  155. return -ENOMEM;
  156. }
  157. efi_rt_stack_top = p + THREAD_SIZE;
  158. return 0;
  159. }
  160. core_initcall(arm64_efi_rt_init);