fake_mem.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*
  2. * fake_mem.c
  3. *
  4. * Copyright (C) 2015 FUJITSU LIMITED
  5. * Author: Taku Izumi <izumi.taku@jp.fujitsu.com>
  6. *
  7. * This code introduces new boot option named "efi_fake_mem"
  8. * By specifying this parameter, you can add arbitrary attribute to
  9. * specific memory range by updating original (firmware provided) EFI
  10. * memmap.
  11. *
  12. * This program is free software; you can redistribute it and/or modify it
  13. * under the terms and conditions of the GNU General Public License,
  14. * version 2, as published by the Free Software Foundation.
  15. *
  16. * This program is distributed in the hope it will be useful, but WITHOUT
  17. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  18. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  19. * more details.
  20. *
  21. * You should have received a copy of the GNU General Public License along with
  22. * this program; if not, see <http://www.gnu.org/licenses/>.
  23. *
  24. * The full GNU General Public License is included in this distribution in
  25. * the file called "COPYING".
  26. */
  27. #include <linux/kernel.h>
  28. #include <linux/efi.h>
  29. #include <linux/init.h>
  30. #include <linux/memblock.h>
  31. #include <linux/types.h>
  32. #include <linux/sort.h>
  33. #include <asm/efi.h>
  34. #define EFI_MAX_FAKEMEM CONFIG_EFI_MAX_FAKE_MEM
  35. static struct efi_mem_range fake_mems[EFI_MAX_FAKEMEM];
  36. static int nr_fake_mem;
  37. static int __init cmp_fake_mem(const void *x1, const void *x2)
  38. {
  39. const struct efi_mem_range *m1 = x1;
  40. const struct efi_mem_range *m2 = x2;
  41. if (m1->range.start < m2->range.start)
  42. return -1;
  43. if (m1->range.start > m2->range.start)
  44. return 1;
  45. return 0;
  46. }
  47. void __init efi_fake_memmap(void)
  48. {
  49. int new_nr_map = efi.memmap.nr_map;
  50. efi_memory_desc_t *md;
  51. phys_addr_t new_memmap_phy;
  52. void *new_memmap;
  53. int i;
  54. if (!nr_fake_mem)
  55. return;
  56. /* count up the number of EFI memory descriptor */
  57. for (i = 0; i < nr_fake_mem; i++) {
  58. for_each_efi_memory_desc(md) {
  59. struct range *r = &fake_mems[i].range;
  60. new_nr_map += efi_memmap_split_count(md, r);
  61. }
  62. }
  63. /* allocate memory for new EFI memmap */
  64. new_memmap_phy = memblock_alloc(efi.memmap.desc_size * new_nr_map,
  65. PAGE_SIZE);
  66. if (!new_memmap_phy)
  67. return;
  68. /* create new EFI memmap */
  69. new_memmap = early_memremap(new_memmap_phy,
  70. efi.memmap.desc_size * new_nr_map);
  71. if (!new_memmap) {
  72. memblock_free(new_memmap_phy, efi.memmap.desc_size * new_nr_map);
  73. return;
  74. }
  75. for (i = 0; i < nr_fake_mem; i++)
  76. efi_memmap_insert(&efi.memmap, new_memmap, &fake_mems[i]);
  77. /* swap into new EFI memmap */
  78. early_memunmap(new_memmap, efi.memmap.desc_size * new_nr_map);
  79. efi_memmap_install(new_memmap_phy, new_nr_map);
  80. /* print new EFI memmap */
  81. efi_print_memmap();
  82. }
  83. static int __init setup_fake_mem(char *p)
  84. {
  85. u64 start = 0, mem_size = 0, attribute = 0;
  86. int i;
  87. if (!p)
  88. return -EINVAL;
  89. while (*p != '\0') {
  90. mem_size = memparse(p, &p);
  91. if (*p == '@')
  92. start = memparse(p+1, &p);
  93. else
  94. break;
  95. if (*p == ':')
  96. attribute = simple_strtoull(p+1, &p, 0);
  97. else
  98. break;
  99. if (nr_fake_mem >= EFI_MAX_FAKEMEM)
  100. break;
  101. fake_mems[nr_fake_mem].range.start = start;
  102. fake_mems[nr_fake_mem].range.end = start + mem_size - 1;
  103. fake_mems[nr_fake_mem].attribute = attribute;
  104. nr_fake_mem++;
  105. if (*p == ',')
  106. p++;
  107. }
  108. sort(fake_mems, nr_fake_mem, sizeof(struct efi_mem_range),
  109. cmp_fake_mem, NULL);
  110. for (i = 0; i < nr_fake_mem; i++)
  111. pr_info("efi_fake_mem: add attr=0x%016llx to [mem 0x%016llx-0x%016llx]",
  112. fake_mems[i].attribute, fake_mems[i].range.start,
  113. fake_mems[i].range.end);
  114. return *p == '\0' ? 0 : -EINVAL;
  115. }
  116. early_param("efi_fake_mem", setup_fake_mem);