blob: 2978fcbe307eabf43984ce631e7faa1dbeaff9c0 [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Christophe Leroy9b081e12016-12-07 08:47:24 +01002/*
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.
Christophe Leroy9b081e12016-12-07 08:47:24 +010015 */
16
17#undef DEBUG
18
19#include <linux/string.h>
Mike Rapoportca5999f2020-06-08 21:32:38 -070020#include <linux/pgtable.h>
Mike Rapoport65fddcf2020-06-08 21:32:42 -070021#include <asm/pgalloc.h>
Christophe Leroy69795ca2019-04-18 16:51:18 +100022#include <asm/kup.h>
Christophe Leroy67548622021-10-19 09:29:19 +020023#include <asm/smp.h>
Christophe Leroy69795ca2019-04-18 16:51:18 +100024
Jason Yan4ed47db2019-09-20 17:45:36 +080025phys_addr_t memstart_addr __ro_after_init = (phys_addr_t)~0ull;
26EXPORT_SYMBOL_GPL(memstart_addr);
27phys_addr_t kernstart_addr __ro_after_init;
28EXPORT_SYMBOL_GPL(kernstart_addr);
Jason Yan39f4b7b2019-09-20 17:45:37 +080029unsigned long kernstart_virt_addr __ro_after_init = KERNELBASE;
30EXPORT_SYMBOL_GPL(kernstart_virt_addr);
Jason Yan4ed47db2019-09-20 17:45:36 +080031
Aneesh Kumar K.V61130e22020-12-02 10:08:54 +053032bool disable_kuep = !IS_ENABLED(CONFIG_PPC_KUEP);
33bool disable_kuap = !IS_ENABLED(CONFIG_PPC_KUAP);
Hari Bathini353d7a82024-07-01 18:30:21 +053034#ifdef CONFIG_KFENCE
35bool __ro_after_init kfence_disabled;
36#endif
Christophe Leroy0fb1c252019-04-18 16:51:19 +100037
38static int __init parse_nosmep(char *p)
39{
Christophe Leroy67548622021-10-19 09:29:19 +020040 if (!IS_ENABLED(CONFIG_PPC_BOOK3S_64))
41 return 0;
42
Christophe Leroy0fb1c252019-04-18 16:51:19 +100043 disable_kuep = true;
44 pr_warn("Disabling Kernel Userspace Execution Prevention\n");
45 return 0;
46}
47early_param("nosmep", parse_nosmep);
48
Christophe Leroyde78a9c2019-04-18 16:51:20 +100049static int __init parse_nosmap(char *p)
50{
51 disable_kuap = true;
52 pr_warn("Disabling Kernel Userspace Access Protection\n");
53 return 0;
54}
55early_param("nosmap", parse_nosmap);
56
Christophe Leroy67548622021-10-19 09:29:19 +020057void __weak setup_kuep(bool disabled)
58{
59 if (!IS_ENABLED(CONFIG_PPC_KUEP) || disabled)
60 return;
61
62 if (smp_processor_id() != boot_cpuid)
63 return;
64
65 pr_info("Activating Kernel Userspace Execution Prevention\n");
66}
67
Christophe Leroy6c1fa602021-10-19 09:29:12 +020068void setup_kup(void)
69{
70 setup_kuap(disable_kuap);
71 setup_kuep(disable_kuep);
72}
73
Christophe Leroy1e03c7e22018-11-29 14:07:07 +000074#define CTOR(shift) static void ctor_##shift(void *addr) \
75{ \
Christophe Leroye7a9af82024-08-08 11:32:47 +020076 memset(addr, 0, sizeof(pgd_t) << (shift)); \
Christophe Leroy9b081e12016-12-07 08:47:24 +010077}
78
Christophe Leroy1e03c7e22018-11-29 14:07:07 +000079CTOR(0); CTOR(1); CTOR(2); CTOR(3); CTOR(4); CTOR(5); CTOR(6); CTOR(7);
80CTOR(8); CTOR(9); CTOR(10); CTOR(11); CTOR(12); CTOR(13); CTOR(14); CTOR(15);
Christophe Leroy9b081e12016-12-07 08:47:24 +010081
Christophe Leroy1e03c7e22018-11-29 14:07:07 +000082static inline void (*ctor(int shift))(void *)
Christophe Leroy9b081e12016-12-07 08:47:24 +010083{
Christophe Leroy1e03c7e22018-11-29 14:07:07 +000084 BUILD_BUG_ON(MAX_PGTABLE_INDEX_SIZE != 15);
85
86 switch (shift) {
87 case 0: return ctor_0;
88 case 1: return ctor_1;
89 case 2: return ctor_2;
90 case 3: return ctor_3;
91 case 4: return ctor_4;
92 case 5: return ctor_5;
93 case 6: return ctor_6;
94 case 7: return ctor_7;
95 case 8: return ctor_8;
96 case 9: return ctor_9;
97 case 10: return ctor_10;
98 case 11: return ctor_11;
99 case 12: return ctor_12;
100 case 13: return ctor_13;
101 case 14: return ctor_14;
102 case 15: return ctor_15;
103 }
104 return NULL;
Christophe Leroy9b081e12016-12-07 08:47:24 +0100105}
106
Christophe Leroy129dd322018-11-29 14:07:03 +0000107struct kmem_cache *pgtable_cache[MAX_PGTABLE_INDEX_SIZE + 1];
Paul Mackerrasba9b3992017-01-30 21:21:38 +1100108EXPORT_SYMBOL_GPL(pgtable_cache); /* used by kvm_hv module */
Christophe Leroy9b081e12016-12-07 08:47:24 +0100109
110/*
111 * Create a kmem_cache() for pagetables. This is not used for PTE
112 * pages - they're linked to struct page, come from the normal free
113 * pages pool and have a different entry size (see real_pte_t) to
114 * everything else. Caches created by this function are used for all
115 * the higher level pagetables, and for hugepage pagetables.
116 */
Christophe Leroy1e03c7e22018-11-29 14:07:07 +0000117void pgtable_cache_add(unsigned int shift)
Christophe Leroy9b081e12016-12-07 08:47:24 +0100118{
119 char *name;
Christophe Leroye7a9af82024-08-08 11:32:47 +0200120 unsigned long table_size = sizeof(pgd_t) << shift;
Christophe Leroy9b081e12016-12-07 08:47:24 +0100121 unsigned long align = table_size;
122
123 /* When batching pgtable pointers for RCU freeing, we store
124 * the index size in the low bits. Table alignment must be
125 * big enough to fit it.
Christophe Leroy0c22e4b2024-07-02 15:51:34 +0200126 */
127 unsigned long minalign = MAX_PGTABLE_INDEX_SIZE + 1;
Kunwu Chanf46c8a72023-12-04 10:32:23 +0800128 struct kmem_cache *new = NULL;
Christophe Leroy9b081e12016-12-07 08:47:24 +0100129
130 /* It would be nice if this was a BUILD_BUG_ON(), but at the
131 * moment, gcc doesn't seem to recognize is_power_of_2 as a
132 * constant expression, so so much for that. */
133 BUG_ON(!is_power_of_2(minalign));
Christophe Leroy129dd322018-11-29 14:07:03 +0000134 BUG_ON(shift > MAX_PGTABLE_INDEX_SIZE);
Christophe Leroy9b081e12016-12-07 08:47:24 +0100135
136 if (PGT_CACHE(shift))
137 return; /* Already have a cache of this size */
138
139 align = max_t(unsigned long, align, minalign);
140 name = kasprintf(GFP_KERNEL, "pgtable-2^%d", shift);
Kunwu Chanf46c8a72023-12-04 10:32:23 +0800141 if (name)
142 new = kmem_cache_create(name, table_size, align, 0, ctor(shift));
Nicholas Pigginbf5ca682017-01-04 01:55:17 +1000143 if (!new)
144 panic("Could not allocate pgtable cache for order %d", shift);
145
Christophe Leroy9b081e12016-12-07 08:47:24 +0100146 kfree(name);
Christophe Leroy129dd322018-11-29 14:07:03 +0000147 pgtable_cache[shift] = new;
Nicholas Pigginbf5ca682017-01-04 01:55:17 +1000148
Christophe Leroy9b081e12016-12-07 08:47:24 +0100149 pr_debug("Allocated pgtable cache for order %d\n", shift);
150}
Paul Mackerrasba9b3992017-01-30 21:21:38 +1100151EXPORT_SYMBOL_GPL(pgtable_cache_add); /* used by kvm_hv module */
Christophe Leroy9b081e12016-12-07 08:47:24 +0100152
153void pgtable_cache_init(void)
154{
Christophe Leroy1e03c7e22018-11-29 14:07:07 +0000155 pgtable_cache_add(PGD_INDEX_SIZE);
Christophe Leroy9b081e12016-12-07 08:47:24 +0100156
Christophe Leroy32bff4b2018-11-29 14:07:09 +0000157 if (PMD_CACHE_INDEX)
Christophe Leroy1e03c7e22018-11-29 14:07:07 +0000158 pgtable_cache_add(PMD_CACHE_INDEX);
Christophe Leroy9b081e12016-12-07 08:47:24 +0100159 /*
160 * In all current configs, when the PUD index exists it's the
161 * same size as either the pgd or pmd index except with THP enabled
162 * on book3s 64
163 */
Christophe Leroy32bff4b2018-11-29 14:07:09 +0000164 if (PUD_CACHE_INDEX)
Christophe Leroy1e03c7e22018-11-29 14:07:07 +0000165 pgtable_cache_add(PUD_CACHE_INDEX);
Christophe Leroy9b081e12016-12-07 08:47:24 +0100166}