memattr.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /*
  2. * Copyright (C) 2016 Linaro Ltd. <[email protected]>
  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. unmap:
  38. early_memunmap(tbl, sizeof(*tbl));
  39. return 0;
  40. }
  41. /*
  42. * Returns a copy @out of the UEFI memory descriptor @in if it is covered
  43. * entirely by a UEFI memory map entry with matching attributes. The virtual
  44. * address of @out is set according to the matching entry that was found.
  45. */
  46. static bool entry_is_valid(const efi_memory_desc_t *in, efi_memory_desc_t *out)
  47. {
  48. u64 in_paddr = in->phys_addr;
  49. u64 in_size = in->num_pages << EFI_PAGE_SHIFT;
  50. efi_memory_desc_t *md;
  51. *out = *in;
  52. if (in->type != EFI_RUNTIME_SERVICES_CODE &&
  53. in->type != EFI_RUNTIME_SERVICES_DATA) {
  54. pr_warn("Entry type should be RuntimeServiceCode/Data\n");
  55. return false;
  56. }
  57. if (!(in->attribute & (EFI_MEMORY_RO | EFI_MEMORY_XP))) {
  58. pr_warn("Entry attributes invalid: RO and XP bits both cleared\n");
  59. return false;
  60. }
  61. if (PAGE_SIZE > EFI_PAGE_SIZE &&
  62. (!PAGE_ALIGNED(in->phys_addr) ||
  63. !PAGE_ALIGNED(in->num_pages << EFI_PAGE_SHIFT))) {
  64. /*
  65. * Since arm64 may execute with page sizes of up to 64 KB, the
  66. * UEFI spec mandates that RuntimeServices memory regions must
  67. * be 64 KB aligned. We need to validate this here since we will
  68. * not be able to tighten permissions on such regions without
  69. * affecting adjacent regions.
  70. */
  71. pr_warn("Entry address region misaligned\n");
  72. return false;
  73. }
  74. for_each_efi_memory_desc(md) {
  75. u64 md_paddr = md->phys_addr;
  76. u64 md_size = md->num_pages << EFI_PAGE_SHIFT;
  77. if (!(md->attribute & EFI_MEMORY_RUNTIME))
  78. continue;
  79. if (md->virt_addr == 0 && md->phys_addr != 0) {
  80. /* no virtual mapping has been installed by the stub */
  81. break;
  82. }
  83. if (md_paddr > in_paddr || (in_paddr - md_paddr) >= md_size)
  84. continue;
  85. /*
  86. * This entry covers the start of @in, check whether
  87. * it covers the end as well.
  88. */
  89. if (md_paddr + md_size < in_paddr + in_size) {
  90. pr_warn("Entry covers multiple EFI memory map regions\n");
  91. return false;
  92. }
  93. if (md->type != in->type) {
  94. pr_warn("Entry type deviates from EFI memory map region type\n");
  95. return false;
  96. }
  97. out->virt_addr = in_paddr + (md->virt_addr - md_paddr);
  98. return true;
  99. }
  100. pr_warn("No matching entry found in the EFI memory map\n");
  101. return false;
  102. }
  103. /*
  104. * To be called after the EFI page tables have been populated. If a memory
  105. * attributes table is available, its contents will be used to update the
  106. * mappings with tightened permissions as described by the table.
  107. * This requires the UEFI memory map to have already been populated with
  108. * virtual addresses.
  109. */
  110. int __init efi_memattr_apply_permissions(struct mm_struct *mm,
  111. efi_memattr_perm_setter fn)
  112. {
  113. efi_memory_attributes_table_t *tbl;
  114. int i, ret;
  115. if (tbl_size <= sizeof(*tbl))
  116. return 0;
  117. /*
  118. * We need the EFI memory map to be setup so we can use it to
  119. * lookup the virtual addresses of all entries in the of EFI
  120. * Memory Attributes table. If it isn't available, this
  121. * function should not be called.
  122. */
  123. if (WARN_ON(!efi_enabled(EFI_MEMMAP)))
  124. return 0;
  125. tbl = memremap(efi.mem_attr_table, tbl_size, MEMREMAP_WB);
  126. if (!tbl) {
  127. pr_err("Failed to map EFI Memory Attributes table @ 0x%lx\n",
  128. efi.mem_attr_table);
  129. return -ENOMEM;
  130. }
  131. if (efi_enabled(EFI_DBG))
  132. pr_info("Processing EFI Memory Attributes table:\n");
  133. for (i = ret = 0; ret == 0 && i < tbl->num_entries; i++) {
  134. efi_memory_desc_t md;
  135. unsigned long size;
  136. bool valid;
  137. char buf[64];
  138. valid = entry_is_valid((void *)tbl->entry + i * tbl->desc_size,
  139. &md);
  140. size = md.num_pages << EFI_PAGE_SHIFT;
  141. if (efi_enabled(EFI_DBG) || !valid)
  142. pr_info("%s 0x%012llx-0x%012llx %s\n",
  143. valid ? "" : "!", md.phys_addr,
  144. md.phys_addr + size - 1,
  145. efi_md_typeattr_format(buf, sizeof(buf), &md));
  146. if (valid)
  147. ret = fn(mm, &md);
  148. }
  149. memunmap(tbl);
  150. return ret;
  151. }