memattr.c 4.9 KB

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