efi.c 3.9 KB

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