memattr.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2016 Linaro Ltd. <ard.biesheuvel@linaro.org>
  4. */
  5. #define pr_fmt(fmt) "efi: memattr: " fmt
  6. #include <linux/efi.h>
  7. #include <linux/init.h>
  8. #include <linux/io.h>
  9. #include <linux/memblock.h>
  10. #include <asm/early_ioremap.h>
  11. static int __initdata tbl_size;
  12. unsigned long __ro_after_init efi_mem_attr_table = EFI_INVALID_TABLE_ADDR;
  13. /*
  14. * Reserve the memory associated with the Memory Attributes configuration
  15. * table, if it exists.
  16. */
  17. int __init efi_memattr_init(void)
  18. {
  19. efi_memory_attributes_table_t *tbl;
  20. if (efi_mem_attr_table == EFI_INVALID_TABLE_ADDR)
  21. return 0;
  22. tbl = early_memremap(efi_mem_attr_table, sizeof(*tbl));
  23. if (!tbl) {
  24. pr_err("Failed to map EFI Memory Attributes table @ 0x%lx\n",
  25. efi_mem_attr_table);
  26. return -ENOMEM;
  27. }
  28. if (tbl->version > 1) {
  29. pr_warn("Unexpected EFI Memory Attributes table version %d\n",
  30. tbl->version);
  31. goto unmap;
  32. }
  33. tbl_size = sizeof(*tbl) + tbl->num_entries * tbl->desc_size;
  34. memblock_reserve(efi_mem_attr_table, tbl_size);
  35. set_bit(EFI_MEM_ATTR, &efi.flags);
  36. unmap:
  37. early_memunmap(tbl, sizeof(*tbl));
  38. return 0;
  39. }
  40. /*
  41. * Returns a copy @out of the UEFI memory descriptor @in if it is covered
  42. * entirely by a UEFI memory map entry with matching attributes. The virtual
  43. * address of @out is set according to the matching entry that was found.
  44. */
  45. static bool entry_is_valid(const efi_memory_desc_t *in, efi_memory_desc_t *out)
  46. {
  47. u64 in_paddr = in->phys_addr;
  48. u64 in_size = in->num_pages << EFI_PAGE_SHIFT;
  49. efi_memory_desc_t *md;
  50. *out = *in;
  51. if (in->type != EFI_RUNTIME_SERVICES_CODE &&
  52. in->type != EFI_RUNTIME_SERVICES_DATA) {
  53. pr_warn("Entry type should be RuntimeServiceCode/Data\n");
  54. return false;
  55. }
  56. if (PAGE_SIZE > EFI_PAGE_SIZE &&
  57. (!PAGE_ALIGNED(in->phys_addr) ||
  58. !PAGE_ALIGNED(in->num_pages << EFI_PAGE_SHIFT))) {
  59. /*
  60. * Since arm64 may execute with page sizes of up to 64 KB, the
  61. * UEFI spec mandates that RuntimeServices memory regions must
  62. * be 64 KB aligned. We need to validate this here since we will
  63. * not be able to tighten permissions on such regions without
  64. * affecting adjacent regions.
  65. */
  66. pr_warn("Entry address region misaligned\n");
  67. return false;
  68. }
  69. for_each_efi_memory_desc(md) {
  70. u64 md_paddr = md->phys_addr;
  71. u64 md_size = md->num_pages << EFI_PAGE_SHIFT;
  72. if (!(md->attribute & EFI_MEMORY_RUNTIME))
  73. continue;
  74. if (md->virt_addr == 0 && md->phys_addr != 0) {
  75. /* no virtual mapping has been installed by the stub */
  76. break;
  77. }
  78. if (md_paddr > in_paddr || (in_paddr - md_paddr) >= md_size)
  79. continue;
  80. /*
  81. * This entry covers the start of @in, check whether
  82. * it covers the end as well.
  83. */
  84. if (md_paddr + md_size < in_paddr + in_size) {
  85. pr_warn("Entry covers multiple EFI memory map regions\n");
  86. return false;
  87. }
  88. if (md->type != in->type) {
  89. pr_warn("Entry type deviates from EFI memory map region type\n");
  90. return false;
  91. }
  92. out->virt_addr = in_paddr + (md->virt_addr - md_paddr);
  93. return true;
  94. }
  95. pr_warn("No matching entry found in the EFI memory map\n");
  96. return false;
  97. }
  98. /*
  99. * To be called after the EFI page tables have been populated. If a memory
  100. * attributes table is available, its contents will be used to update the
  101. * mappings with tightened permissions as described by the table.
  102. * This requires the UEFI memory map to have already been populated with
  103. * virtual addresses.
  104. */
  105. int __init efi_memattr_apply_permissions(struct mm_struct *mm,
  106. efi_memattr_perm_setter fn)
  107. {
  108. efi_memory_attributes_table_t *tbl;
  109. int i, ret;
  110. if (tbl_size <= sizeof(*tbl))
  111. return 0;
  112. /*
  113. * We need the EFI memory map to be setup so we can use it to
  114. * lookup the virtual addresses of all entries in the of EFI
  115. * Memory Attributes table. If it isn't available, this
  116. * function should not be called.
  117. */
  118. if (WARN_ON(!efi_enabled(EFI_MEMMAP)))
  119. return 0;
  120. tbl = memremap(efi_mem_attr_table, tbl_size, MEMREMAP_WB);
  121. if (!tbl) {
  122. pr_err("Failed to map EFI Memory Attributes table @ 0x%lx\n",
  123. efi_mem_attr_table);
  124. return -ENOMEM;
  125. }
  126. if (efi_enabled(EFI_DBG))
  127. pr_info("Processing EFI Memory Attributes table:\n");
  128. for (i = ret = 0; ret == 0 && i < tbl->num_entries; i++) {
  129. efi_memory_desc_t md;
  130. unsigned long size;
  131. bool valid;
  132. char buf[64];
  133. valid = entry_is_valid((void *)tbl->entry + i * tbl->desc_size,
  134. &md);
  135. size = md.num_pages << EFI_PAGE_SHIFT;
  136. if (efi_enabled(EFI_DBG) || !valid)
  137. pr_info("%s 0x%012llx-0x%012llx %s\n",
  138. valid ? "" : "!", md.phys_addr,
  139. md.phys_addr + size - 1,
  140. efi_md_typeattr_format(buf, sizeof(buf), &md));
  141. if (valid) {
  142. ret = fn(mm, &md);
  143. if (ret)
  144. pr_err("Error updating mappings, skipping subsequent md's\n");
  145. }
  146. }
  147. memunmap(tbl);
  148. return ret;
  149. }