efi.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. /*
  13. * Only regions of type EFI_RUNTIME_SERVICES_CODE need to be
  14. * executable, everything else can be mapped with the XN bits
  15. * set. Also take the new (optional) RO/XP bits into account.
  16. */
  17. static __init pteval_t create_mapping_protection(efi_memory_desc_t *md)
  18. {
  19. u64 attr = md->attribute;
  20. u32 type = md->type;
  21. if (type == EFI_MEMORY_MAPPED_IO)
  22. return PROT_DEVICE_nGnRE;
  23. if (WARN_ONCE(!PAGE_ALIGNED(md->phys_addr),
  24. "UEFI Runtime regions are not aligned to 64 KB -- buggy firmware?"))
  25. /*
  26. * If the region is not aligned to the page size of the OS, we
  27. * can not use strict permissions, since that would also affect
  28. * the mapping attributes of the adjacent regions.
  29. */
  30. return pgprot_val(PAGE_KERNEL_EXEC);
  31. /* R-- */
  32. if ((attr & (EFI_MEMORY_XP | EFI_MEMORY_RO)) ==
  33. (EFI_MEMORY_XP | EFI_MEMORY_RO))
  34. return pgprot_val(PAGE_KERNEL_RO);
  35. /* R-X */
  36. if (attr & EFI_MEMORY_RO)
  37. return pgprot_val(PAGE_KERNEL_ROX);
  38. /* RW- */
  39. if (((attr & (EFI_MEMORY_RP | EFI_MEMORY_WP | EFI_MEMORY_XP)) ==
  40. EFI_MEMORY_XP) ||
  41. type != EFI_RUNTIME_SERVICES_CODE)
  42. return pgprot_val(PAGE_KERNEL);
  43. /* RWX */
  44. return pgprot_val(PAGE_KERNEL_EXEC);
  45. }
  46. /* we will fill this structure from the stub, so don't put it in .bss */
  47. struct screen_info screen_info __section(".data");
  48. int __init efi_create_mapping(struct mm_struct *mm, efi_memory_desc_t *md)
  49. {
  50. pteval_t prot_val = create_mapping_protection(md);
  51. bool page_mappings_only = (md->type == EFI_RUNTIME_SERVICES_CODE ||
  52. md->type == EFI_RUNTIME_SERVICES_DATA);
  53. if (!PAGE_ALIGNED(md->phys_addr) ||
  54. !PAGE_ALIGNED(md->num_pages << EFI_PAGE_SHIFT)) {
  55. /*
  56. * If the end address of this region is not aligned to page
  57. * size, the mapping is rounded up, and may end up sharing a
  58. * page frame with the next UEFI memory region. If we create
  59. * a block entry now, we may need to split it again when mapping
  60. * the next region, and support for that is going to be removed
  61. * from the MMU routines. So avoid block mappings altogether in
  62. * that case.
  63. */
  64. page_mappings_only = true;
  65. }
  66. create_pgd_mapping(mm, md->phys_addr, md->virt_addr,
  67. md->num_pages << EFI_PAGE_SHIFT,
  68. __pgprot(prot_val | PTE_NG), page_mappings_only);
  69. return 0;
  70. }
  71. static int __init set_permissions(pte_t *ptep, unsigned long addr, void *data)
  72. {
  73. efi_memory_desc_t *md = data;
  74. pte_t pte = READ_ONCE(*ptep);
  75. if (md->attribute & EFI_MEMORY_RO)
  76. pte = set_pte_bit(pte, __pgprot(PTE_RDONLY));
  77. if (md->attribute & EFI_MEMORY_XP)
  78. pte = set_pte_bit(pte, __pgprot(PTE_PXN));
  79. set_pte(ptep, pte);
  80. return 0;
  81. }
  82. int __init efi_set_mapping_permissions(struct mm_struct *mm,
  83. efi_memory_desc_t *md)
  84. {
  85. BUG_ON(md->type != EFI_RUNTIME_SERVICES_CODE &&
  86. md->type != EFI_RUNTIME_SERVICES_DATA);
  87. /*
  88. * Calling apply_to_page_range() is only safe on regions that are
  89. * guaranteed to be mapped down to pages. Since we are only called
  90. * for regions that have been mapped using efi_create_mapping() above
  91. * (and this is checked by the generic Memory Attributes table parsing
  92. * routines), there is no need to check that again here.
  93. */
  94. return apply_to_page_range(mm, md->virt_addr,
  95. md->num_pages << EFI_PAGE_SHIFT,
  96. set_permissions, md);
  97. }
  98. /*
  99. * UpdateCapsule() depends on the system being shutdown via
  100. * ResetSystem().
  101. */
  102. bool efi_poweroff_required(void)
  103. {
  104. return efi_enabled(EFI_RUNTIME_SERVICES);
  105. }
  106. asmlinkage efi_status_t efi_handle_corrupted_x18(efi_status_t s, const char *f)
  107. {
  108. pr_err_ratelimited(FW_BUG "register x18 corrupted by EFI %s\n", f);
  109. return s;
  110. }