Commit | Line | Data |
---|---|---|
14cf11af PM |
1 | /* |
2 | * MMU context allocation for 64-bit kernels. | |
3 | * | |
4 | * Copyright (C) 2004 Anton Blanchard, IBM Corp. <anton@samba.org> | |
5 | * | |
6 | * This program is free software; you can redistribute it and/or | |
7 | * modify it under the terms of the GNU General Public License | |
8 | * as published by the Free Software Foundation; either version | |
9 | * 2 of the License, or (at your option) any later version. | |
10 | * | |
11 | */ | |
12 | ||
14cf11af PM |
13 | #include <linux/sched.h> |
14 | #include <linux/kernel.h> | |
15 | #include <linux/errno.h> | |
16 | #include <linux/string.h> | |
17 | #include <linux/types.h> | |
18 | #include <linux/mm.h> | |
4fb158f6 | 19 | #include <linux/pkeys.h> |
14cf11af PM |
20 | #include <linux/spinlock.h> |
21 | #include <linux/idr.h> | |
4b16f8e2 | 22 | #include <linux/export.h> |
5a0e3ad6 | 23 | #include <linux/gfp.h> |
851d2e2f | 24 | #include <linux/slab.h> |
14cf11af PM |
25 | |
26 | #include <asm/mmu_context.h> | |
5c1f6ee9 | 27 | #include <asm/pgalloc.h> |
14cf11af | 28 | |
7317ac87 | 29 | static DEFINE_IDA(mmu_context_ida); |
14cf11af | 30 | |
c1ff840d | 31 | static int alloc_context_id(int min_id, int max_id) |
14cf11af | 32 | { |
b3fa6417 | 33 | return ida_alloc_range(&mmu_context_ida, min_id, max_id, GFP_KERNEL); |
e85a4710 | 34 | } |
a336f2f5 | 35 | |
82228e36 AK |
36 | void hash__reserve_context_id(int id) |
37 | { | |
b3fa6417 | 38 | int result = ida_alloc_range(&mmu_context_ida, id, id, GFP_KERNEL); |
82228e36 AK |
39 | |
40 | WARN(result != id, "mmu: Failed to reserve context id %d (rc %d)\n", id, result); | |
41 | } | |
42 | ||
a336f2f5 ME |
43 | int hash__alloc_context_id(void) |
44 | { | |
e6f81a92 AK |
45 | unsigned long max; |
46 | ||
47 | if (mmu_has_feature(MMU_FTR_68_BIT_VA)) | |
48 | max = MAX_USER_CONTEXT; | |
49 | else | |
50 | max = MAX_USER_CONTEXT_65BIT_VA; | |
51 | ||
52 | return alloc_context_id(MIN_USER_CONTEXT, max); | |
a336f2f5 ME |
53 | } |
54 | EXPORT_SYMBOL_GPL(hash__alloc_context_id); | |
55 | ||
5434ae74 NP |
56 | void slb_setup_new_exec(void); |
57 | ||
ca72d883 ME |
58 | static int realloc_context_ids(mm_context_t *ctx) |
59 | { | |
60 | int i, id; | |
61 | ||
62 | /* | |
63 | * id 0 (aka. ctx->id) is special, we always allocate a new one, even if | |
64 | * there wasn't one allocated previously (which happens in the exec | |
65 | * case where ctx is newly allocated). | |
66 | * | |
67 | * We have to be a bit careful here. We must keep the existing ids in | |
68 | * the array, so that we can test if they're non-zero to decide if we | |
69 | * need to allocate a new one. However in case of error we must free the | |
70 | * ids we've allocated but *not* any of the existing ones (or risk a | |
71 | * UAF). That's why we decrement i at the start of the error handling | |
72 | * loop, to skip the id that we just tested but couldn't reallocate. | |
73 | */ | |
74 | for (i = 0; i < ARRAY_SIZE(ctx->extended_id); i++) { | |
75 | if (i == 0 || ctx->extended_id[i]) { | |
76 | id = hash__alloc_context_id(); | |
77 | if (id < 0) | |
78 | goto error; | |
79 | ||
80 | ctx->extended_id[i] = id; | |
81 | } | |
82 | } | |
83 | ||
84 | /* The caller expects us to return id */ | |
85 | return ctx->id; | |
86 | ||
87 | error: | |
88 | for (i--; i >= 0; i--) { | |
89 | if (ctx->extended_id[i]) | |
90 | ida_free(&mmu_context_ida, ctx->extended_id[i]); | |
91 | } | |
92 | ||
93 | return id; | |
94 | } | |
95 | ||
760573c1 ME |
96 | static int hash__init_new_context(struct mm_struct *mm) |
97 | { | |
98 | int index; | |
99 | ||
ef629cc5 AK |
100 | mm->context.hash_context = kmalloc(sizeof(struct hash_mm_context), |
101 | GFP_KERNEL); | |
65565a68 | 102 | if (!mm->context.hash_context) |
70110186 | 103 | return -ENOMEM; |
70110186 | 104 | |
760573c1 ME |
105 | /* |
106 | * The old code would re-promote on fork, we don't do that when using | |
107 | * slices as it could cause problem promoting slices that have been | |
108 | * forced down to 4K. | |
109 | * | |
110 | * For book3s we have MMU_NO_CONTEXT set to be ~0. Hence check | |
111 | * explicitly against context.id == 0. This ensures that we properly | |
112 | * initialize context slice details for newly allocated mm's (which will | |
113 | * have id == 0) and don't alter context slice inherited via fork (which | |
114 | * will have id != 0). | |
115 | * | |
116 | * We should not be calling init_new_context() on init_mm. Hence a | |
117 | * check against 0 is OK. | |
118 | */ | |
70110186 AK |
119 | if (mm->context.id == 0) { |
120 | memset(mm->context.hash_context, 0, sizeof(struct hash_mm_context)); | |
1753dd18 | 121 | slice_init_new_context_exec(mm); |
70110186 AK |
122 | } else { |
123 | /* This is fork. Copy hash_context details from current->mm */ | |
124 | memcpy(mm->context.hash_context, current->mm->context.hash_context, sizeof(struct hash_mm_context)); | |
ef629cc5 AK |
125 | #ifdef CONFIG_PPC_SUBPAGE_PROT |
126 | /* inherit subpage prot detalis if we have one. */ | |
127 | if (current->mm->context.hash_context->spt) { | |
128 | mm->context.hash_context->spt = kmalloc(sizeof(struct subpage_prot_table), | |
129 | GFP_KERNEL); | |
130 | if (!mm->context.hash_context->spt) { | |
ef629cc5 AK |
131 | kfree(mm->context.hash_context); |
132 | return -ENOMEM; | |
133 | } | |
134 | } | |
135 | #endif | |
65565a68 | 136 | } |
70110186 | 137 | |
ca72d883 | 138 | index = realloc_context_ids(&mm->context); |
65565a68 ME |
139 | if (index < 0) { |
140 | #ifdef CONFIG_PPC_SUBPAGE_PROT | |
141 | kfree(mm->context.hash_context->spt); | |
142 | #endif | |
143 | kfree(mm->context.hash_context); | |
ca72d883 | 144 | return index; |
70110186 | 145 | } |
760573c1 | 146 | |
4fb158f6 | 147 | pkey_mm_init(mm); |
760573c1 ME |
148 | return index; |
149 | } | |
150 | ||
425d3314 NP |
151 | void hash__setup_new_exec(void) |
152 | { | |
153 | slice_setup_new_exec(); | |
5434ae74 NP |
154 | |
155 | slb_setup_new_exec(); | |
425d3314 NP |
156 | } |
157 | ||
760573c1 | 158 | static int radix__init_new_context(struct mm_struct *mm) |
7e381c0f AK |
159 | { |
160 | unsigned long rts_field; | |
a25bd72b | 161 | int index, max_id; |
760573c1 | 162 | |
a25bd72b BH |
163 | max_id = (1 << mmu_pid_bits) - 1; |
164 | index = alloc_context_id(mmu_base_pid, max_id); | |
760573c1 ME |
165 | if (index < 0) |
166 | return index; | |
7e381c0f AK |
167 | |
168 | /* | |
169 | * set the process table entry, | |
170 | */ | |
b23d9c5b | 171 | rts_field = radix__get_tree_size(); |
7e381c0f | 172 | process_tb[index].prtb0 = cpu_to_be64(rts_field | __pa(mm->pgd) | RADIX_PGD_INDEX_SIZE); |
760573c1 | 173 | |
3a6a0470 BH |
174 | /* |
175 | * Order the above store with subsequent update of the PID | |
176 | * register (at which point HW can start loading/caching | |
177 | * the entry) and the corresponding load by the MMU from | |
178 | * the L2 cache. | |
179 | */ | |
180 | asm volatile("ptesync;isync" : : : "memory"); | |
181 | ||
1ab66d1f | 182 | mm->context.npu_context = NULL; |
70110186 | 183 | mm->context.hash_context = NULL; |
1ab66d1f | 184 | |
760573c1 | 185 | return index; |
7e381c0f | 186 | } |
e85a4710 AG |
187 | |
188 | int init_new_context(struct task_struct *tsk, struct mm_struct *mm) | |
189 | { | |
190 | int index; | |
191 | ||
760573c1 ME |
192 | if (radix_enabled()) |
193 | index = radix__init_new_context(mm); | |
194 | else | |
195 | index = hash__init_new_context(mm); | |
196 | ||
e85a4710 AG |
197 | if (index < 0) |
198 | return index; | |
199 | ||
9dfe5c53 | 200 | mm->context.id = index; |
14cf11af | 201 | |
5c1f6ee9 | 202 | mm->context.pte_frag = NULL; |
8a6c697b | 203 | mm->context.pmd_frag = NULL; |
15b244a8 | 204 | #ifdef CONFIG_SPAPR_TCE_IOMMU |
88f54a35 | 205 | mm_iommu_init(mm); |
5c1f6ee9 | 206 | #endif |
a619e59c | 207 | atomic_set(&mm->context.active_cpus, 0); |
aff6f8cb | 208 | atomic_set(&mm->context.copros, 0); |
a619e59c | 209 | |
14cf11af PM |
210 | return 0; |
211 | } | |
212 | ||
e85a4710 | 213 | void __destroy_context(int context_id) |
14cf11af | 214 | { |
b3fa6417 | 215 | ida_free(&mmu_context_ida, context_id); |
e85a4710 AG |
216 | } |
217 | EXPORT_SYMBOL_GPL(__destroy_context); | |
14cf11af | 218 | |
f384796c AK |
219 | static void destroy_contexts(mm_context_t *ctx) |
220 | { | |
221 | int index, context_id; | |
222 | ||
f384796c AK |
223 | for (index = 0; index < ARRAY_SIZE(ctx->extended_id); index++) { |
224 | context_id = ctx->extended_id[index]; | |
225 | if (context_id) | |
b3fa6417 | 226 | ida_free(&mmu_context_ida, context_id); |
f384796c | 227 | } |
70110186 | 228 | kfree(ctx->hash_context); |
f384796c AK |
229 | } |
230 | ||
8a6c697b AK |
231 | static void pmd_frag_destroy(void *pmd_frag) |
232 | { | |
233 | int count; | |
234 | struct page *page; | |
235 | ||
236 | page = virt_to_page(pmd_frag); | |
237 | /* drop all the pending references */ | |
238 | count = ((unsigned long)pmd_frag & ~PAGE_MASK) >> PMD_FRAG_SIZE_SHIFT; | |
239 | /* We allow PTE_FRAG_NR fragments from a PTE page */ | |
4231aba0 | 240 | if (atomic_sub_and_test(PMD_FRAG_NR - count, &page->pt_frag_refcount)) { |
8a6c697b | 241 | pgtable_pmd_page_dtor(page); |
4231aba0 | 242 | __free_page(page); |
8a6c697b AK |
243 | } |
244 | } | |
245 | ||
34c604d2 | 246 | static void destroy_pagetable_cache(struct mm_struct *mm) |
8a6c697b AK |
247 | { |
248 | void *frag; | |
249 | ||
250 | frag = mm->context.pte_frag; | |
251 | if (frag) | |
252 | pte_frag_destroy(frag); | |
253 | ||
254 | frag = mm->context.pmd_frag; | |
255 | if (frag) | |
256 | pmd_frag_destroy(frag); | |
257 | return; | |
258 | } | |
259 | ||
e85a4710 AG |
260 | void destroy_context(struct mm_struct *mm) |
261 | { | |
15b244a8 | 262 | #ifdef CONFIG_SPAPR_TCE_IOMMU |
4b6fad70 | 263 | WARN_ON_ONCE(!list_empty(&mm->context.iommu_group_mem_list)); |
15b244a8 | 264 | #endif |
30b49ec7 NP |
265 | if (radix_enabled()) |
266 | WARN_ON(process_tb[mm->context.id].prtb0 != 0); | |
267 | else | |
268 | subpage_prot_free(mm); | |
f384796c | 269 | destroy_contexts(&mm->context); |
30b49ec7 NP |
270 | mm->context.id = MMU_NO_CONTEXT; |
271 | } | |
272 | ||
273 | void arch_exit_mmap(struct mm_struct *mm) | |
274 | { | |
34c604d2 NP |
275 | destroy_pagetable_cache(mm); |
276 | ||
c6bb0b8d BH |
277 | if (radix_enabled()) { |
278 | /* | |
279 | * Radix doesn't have a valid bit in the process table | |
280 | * entries. However we know that at least P9 implementation | |
281 | * will avoid caching an entry with an invalid RTS field, | |
282 | * and 0 is invalid. So this will do. | |
30b49ec7 NP |
283 | * |
284 | * This runs before the "fullmm" tlb flush in exit_mmap, | |
285 | * which does a RIC=2 tlbie to clear the process table | |
286 | * entry. See the "fullmm" comments in tlb-radix.c. | |
287 | * | |
288 | * No barrier required here after the store because | |
289 | * this process will do the invalidate, which starts with | |
290 | * ptesync. | |
c6bb0b8d BH |
291 | */ |
292 | process_tb[mm->context.id].prtb0 = 0; | |
30b49ec7 | 293 | } |
14cf11af | 294 | } |
7e381c0f AK |
295 | |
296 | #ifdef CONFIG_PPC_RADIX_MMU | |
297 | void radix__switch_mmu_context(struct mm_struct *prev, struct mm_struct *next) | |
298 | { | |
2bf1071a NP |
299 | mtspr(SPRN_PID, next->context.id); |
300 | isync(); | |
7e381c0f AK |
301 | } |
302 | #endif |