init-common.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * PowerPC version
  4. * Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org)
  5. *
  6. * Modifications by Paul Mackerras (PowerMac) (paulus@cs.anu.edu.au)
  7. * and Cort Dougan (PReP) (cort@cs.nmt.edu)
  8. * Copyright (C) 1996 Paul Mackerras
  9. *
  10. * Derived from "arch/i386/mm/init.c"
  11. * Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds
  12. *
  13. * Dave Engebretsen <engebret@us.ibm.com>
  14. * Rework for PPC64 port.
  15. */
  16. #undef DEBUG
  17. #include <linux/string.h>
  18. #include <linux/pgtable.h>
  19. #include <asm/pgalloc.h>
  20. #include <asm/kup.h>
  21. #include <asm/smp.h>
  22. phys_addr_t memstart_addr __ro_after_init = (phys_addr_t)~0ull;
  23. EXPORT_SYMBOL_GPL(memstart_addr);
  24. phys_addr_t kernstart_addr __ro_after_init;
  25. EXPORT_SYMBOL_GPL(kernstart_addr);
  26. unsigned long kernstart_virt_addr __ro_after_init = KERNELBASE;
  27. EXPORT_SYMBOL_GPL(kernstart_virt_addr);
  28. bool disable_kuep = !IS_ENABLED(CONFIG_PPC_KUEP);
  29. bool disable_kuap = !IS_ENABLED(CONFIG_PPC_KUAP);
  30. #ifdef CONFIG_KFENCE
  31. bool __ro_after_init kfence_disabled;
  32. bool __ro_after_init kfence_early_init = !!CONFIG_KFENCE_SAMPLE_INTERVAL;
  33. #endif
  34. static int __init parse_nosmep(char *p)
  35. {
  36. if (!IS_ENABLED(CONFIG_PPC_BOOK3S_64))
  37. return 0;
  38. disable_kuep = true;
  39. pr_warn("Disabling Kernel Userspace Execution Prevention\n");
  40. return 0;
  41. }
  42. early_param("nosmep", parse_nosmep);
  43. static int __init parse_nosmap(char *p)
  44. {
  45. disable_kuap = true;
  46. pr_warn("Disabling Kernel Userspace Access Protection\n");
  47. return 0;
  48. }
  49. early_param("nosmap", parse_nosmap);
  50. void __weak setup_kuep(bool disabled)
  51. {
  52. if (!IS_ENABLED(CONFIG_PPC_KUEP) || disabled)
  53. return;
  54. if (smp_processor_id() != boot_cpuid)
  55. return;
  56. pr_info("Activating Kernel Userspace Execution Prevention\n");
  57. }
  58. void setup_kup(void)
  59. {
  60. setup_kuap(disable_kuap);
  61. setup_kuep(disable_kuep);
  62. }
  63. #define CTOR(shift) static void ctor_##shift(void *addr) \
  64. { \
  65. memset(addr, 0, sizeof(pgd_t) << (shift)); \
  66. }
  67. CTOR(0); CTOR(1); CTOR(2); CTOR(3); CTOR(4); CTOR(5); CTOR(6); CTOR(7);
  68. CTOR(8); CTOR(9); CTOR(10); CTOR(11); CTOR(12); CTOR(13); CTOR(14); CTOR(15);
  69. static inline void (*ctor(int shift))(void *)
  70. {
  71. BUILD_BUG_ON(MAX_PGTABLE_INDEX_SIZE != 15);
  72. switch (shift) {
  73. case 0: return ctor_0;
  74. case 1: return ctor_1;
  75. case 2: return ctor_2;
  76. case 3: return ctor_3;
  77. case 4: return ctor_4;
  78. case 5: return ctor_5;
  79. case 6: return ctor_6;
  80. case 7: return ctor_7;
  81. case 8: return ctor_8;
  82. case 9: return ctor_9;
  83. case 10: return ctor_10;
  84. case 11: return ctor_11;
  85. case 12: return ctor_12;
  86. case 13: return ctor_13;
  87. case 14: return ctor_14;
  88. case 15: return ctor_15;
  89. }
  90. return NULL;
  91. }
  92. struct kmem_cache *pgtable_cache[MAX_PGTABLE_INDEX_SIZE + 1];
  93. EXPORT_SYMBOL_GPL(pgtable_cache); /* used by kvm_hv module */
  94. /*
  95. * Create a kmem_cache() for pagetables. This is not used for PTE
  96. * pages - they're linked to struct page, come from the normal free
  97. * pages pool and have a different entry size (see real_pte_t) to
  98. * everything else. Caches created by this function are used for all
  99. * the higher level pagetables, and for hugepage pagetables.
  100. */
  101. void pgtable_cache_add(unsigned int shift)
  102. {
  103. char *name;
  104. unsigned long table_size = sizeof(pgd_t) << shift;
  105. unsigned long align = table_size;
  106. /* When batching pgtable pointers for RCU freeing, we store
  107. * the index size in the low bits. Table alignment must be
  108. * big enough to fit it.
  109. */
  110. unsigned long minalign = MAX_PGTABLE_INDEX_SIZE + 1;
  111. struct kmem_cache *new = NULL;
  112. /* It would be nice if this was a BUILD_BUG_ON(), but at the
  113. * moment, gcc doesn't seem to recognize is_power_of_2 as a
  114. * constant expression, so so much for that. */
  115. BUG_ON(!is_power_of_2(minalign));
  116. BUG_ON(shift > MAX_PGTABLE_INDEX_SIZE);
  117. if (PGT_CACHE(shift))
  118. return; /* Already have a cache of this size */
  119. align = max_t(unsigned long, align, minalign);
  120. name = kasprintf(GFP_KERNEL, "pgtable-2^%d", shift);
  121. if (name)
  122. new = kmem_cache_create(name, table_size, align, 0, ctor(shift));
  123. if (!new)
  124. panic("Could not allocate pgtable cache for order %d", shift);
  125. kfree(name);
  126. pgtable_cache[shift] = new;
  127. pr_debug("Allocated pgtable cache for order %d\n", shift);
  128. }
  129. EXPORT_SYMBOL_GPL(pgtable_cache_add); /* used by kvm_hv module */
  130. void pgtable_cache_init(void)
  131. {
  132. pgtable_cache_add(PGD_INDEX_SIZE);
  133. if (PMD_CACHE_INDEX)
  134. pgtable_cache_add(PMD_CACHE_INDEX);
  135. /*
  136. * In all current configs, when the PUD index exists it's the
  137. * same size as either the pgd or pmd index except with THP enabled
  138. * on book3s 64
  139. */
  140. if (PUD_CACHE_INDEX)
  141. pgtable_cache_add(PUD_CACHE_INDEX);
  142. }