1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * PowerPC Memory Protection Keys management
4 *
5 * Copyright 2017, Ram Pai, IBM Corporation.
6 */
7
8 #include <asm/mman.h>
9 #include <asm/setup.h>
10 #include <linux/pkeys.h>
11 #include <linux/of_device.h>
12
13 DEFINE_STATIC_KEY_TRUE(pkey_disabled);
14 bool pkey_execute_disable_supported;
15 int pkeys_total; /* Total pkeys as per device tree */
16 bool pkeys_devtree_defined; /* pkey property exported by device tree */
17 u32 initial_allocation_mask; /* Bits set for the initially allocated keys */
18 u32 reserved_allocation_mask; /* Bits set for reserved keys */
19 u64 pkey_amr_mask; /* Bits in AMR not to be touched */
20 u64 pkey_iamr_mask; /* Bits in AMR not to be touched */
21 u64 pkey_uamor_mask; /* Bits in UMOR not to be touched */
22 int execute_only_key = 2;
23
24 #define AMR_BITS_PER_PKEY 2
25 #define AMR_RD_BIT 0x1UL
26 #define AMR_WR_BIT 0x2UL
27 #define IAMR_EX_BIT 0x1UL
28 #define PKEY_REG_BITS (sizeof(u64)*8)
29 #define pkeyshift(pkey) (PKEY_REG_BITS - ((pkey+1) * AMR_BITS_PER_PKEY))
30
scan_pkey_feature(void)31 static void scan_pkey_feature(void)
32 {
33 u32 vals[2];
34 struct device_node *cpu;
35
36 cpu = of_find_node_by_type(NULL, "cpu");
37 if (!cpu)
38 return;
39
40 if (of_property_read_u32_array(cpu,
41 "ibm,processor-storage-keys", vals, 2))
42 return;
43
44 /*
45 * Since any pkey can be used for data or execute, we will just treat
46 * all keys as equal and track them as one entity.
47 */
48 pkeys_total = vals[0];
49 pkeys_devtree_defined = true;
50 }
51
pkey_mmu_enabled(void)52 static inline bool pkey_mmu_enabled(void)
53 {
54 if (firmware_has_feature(FW_FEATURE_LPAR))
55 return pkeys_total;
56 else
57 return cpu_has_feature(CPU_FTR_PKEY);
58 }
59
pkey_initialize(void)60 int pkey_initialize(void)
61 {
62 int os_reserved, i;
63
64 /*
65 * We define PKEY_DISABLE_EXECUTE in addition to the arch-neutral
66 * generic defines for PKEY_DISABLE_ACCESS and PKEY_DISABLE_WRITE.
67 * Ensure that the bits a distinct.
68 */
69 BUILD_BUG_ON(PKEY_DISABLE_EXECUTE &
70 (PKEY_DISABLE_ACCESS | PKEY_DISABLE_WRITE));
71
72 /*
73 * pkey_to_vmflag_bits() assumes that the pkey bits are contiguous
74 * in the vmaflag. Make sure that is really the case.
75 */
76 BUILD_BUG_ON(__builtin_clzl(ARCH_VM_PKEY_FLAGS >> VM_PKEY_SHIFT) +
77 __builtin_popcountl(ARCH_VM_PKEY_FLAGS >> VM_PKEY_SHIFT)
78 != (sizeof(u64) * BITS_PER_BYTE));
79
80 /* scan the device tree for pkey feature */
81 scan_pkey_feature();
82
83 /*
84 * Let's assume 32 pkeys on P8/P9 bare metal, if its not defined by device
85 * tree. We make this exception since some version of skiboot forgot to
86 * expose this property on power8/9.
87 */
88 if (!pkeys_devtree_defined && !firmware_has_feature(FW_FEATURE_LPAR)) {
89 unsigned long pvr = mfspr(SPRN_PVR);
90
91 if (PVR_VER(pvr) == PVR_POWER8 || PVR_VER(pvr) == PVR_POWER8E ||
92 PVR_VER(pvr) == PVR_POWER8NVL || PVR_VER(pvr) == PVR_POWER9)
93 pkeys_total = 32;
94 }
95
96 /*
97 * Adjust the upper limit, based on the number of bits supported by
98 * arch-neutral code.
99 */
100 pkeys_total = min_t(int, pkeys_total,
101 ((ARCH_VM_PKEY_FLAGS >> VM_PKEY_SHIFT)+1));
102
103 if (!pkey_mmu_enabled() || radix_enabled() || !pkeys_total)
104 static_branch_enable(&pkey_disabled);
105 else
106 static_branch_disable(&pkey_disabled);
107
108 if (static_branch_likely(&pkey_disabled))
109 return 0;
110
111 /*
112 * The device tree cannot be relied to indicate support for
113 * execute_disable support. Instead we use a PVR check.
114 */
115 if (pvr_version_is(PVR_POWER7) || pvr_version_is(PVR_POWER7p))
116 pkey_execute_disable_supported = false;
117 else
118 pkey_execute_disable_supported = true;
119
120 #ifdef CONFIG_PPC_4K_PAGES
121 /*
122 * The OS can manage only 8 pkeys due to its inability to represent them
123 * in the Linux 4K PTE.
124 */
125 os_reserved = pkeys_total - 8;
126 #else
127 os_reserved = 0;
128 #endif
129 /* Bits are in LE format. */
130 reserved_allocation_mask = (0x1 << 1) | (0x1 << execute_only_key);
131
132 /* register mask is in BE format */
133 pkey_amr_mask = ~0x0ul;
134 pkey_amr_mask &= ~(0x3ul << pkeyshift(0));
135
136 pkey_iamr_mask = ~0x0ul;
137 pkey_iamr_mask &= ~(0x3ul << pkeyshift(0));
138 pkey_iamr_mask &= ~(0x3ul << pkeyshift(execute_only_key));
139
140 pkey_uamor_mask = ~0x0ul;
141 pkey_uamor_mask &= ~(0x3ul << pkeyshift(0));
142 pkey_uamor_mask &= ~(0x3ul << pkeyshift(execute_only_key));
143
144 /* mark the rest of the keys as reserved and hence unavailable */
145 for (i = (pkeys_total - os_reserved); i < pkeys_total; i++) {
146 reserved_allocation_mask |= (0x1 << i);
147 pkey_uamor_mask &= ~(0x3ul << pkeyshift(i));
148 }
149 initial_allocation_mask = reserved_allocation_mask | (0x1 << 0);
150
151 if (unlikely((pkeys_total - os_reserved) <= execute_only_key)) {
152 /*
153 * Insufficient number of keys to support
154 * execute only key. Mark it unavailable.
155 * Any AMR, UAMOR, IAMR bit set for
156 * this key is irrelevant since this key
157 * can never be allocated.
158 */
159 execute_only_key = -1;
160 }
161
162 return 0;
163 }
164
165 arch_initcall(pkey_initialize);
166
pkey_mm_init(struct mm_struct * mm)167 void pkey_mm_init(struct mm_struct *mm)
168 {
169 if (static_branch_likely(&pkey_disabled))
170 return;
171 mm_pkey_allocation_map(mm) = initial_allocation_mask;
172 mm->context.execute_only_pkey = execute_only_key;
173 }
174
read_amr(void)175 static inline u64 read_amr(void)
176 {
177 return mfspr(SPRN_AMR);
178 }
179
write_amr(u64 value)180 static inline void write_amr(u64 value)
181 {
182 mtspr(SPRN_AMR, value);
183 }
184
read_iamr(void)185 static inline u64 read_iamr(void)
186 {
187 if (!likely(pkey_execute_disable_supported))
188 return 0x0UL;
189
190 return mfspr(SPRN_IAMR);
191 }
192
write_iamr(u64 value)193 static inline void write_iamr(u64 value)
194 {
195 if (!likely(pkey_execute_disable_supported))
196 return;
197
198 mtspr(SPRN_IAMR, value);
199 }
200
read_uamor(void)201 static inline u64 read_uamor(void)
202 {
203 return mfspr(SPRN_UAMOR);
204 }
205
write_uamor(u64 value)206 static inline void write_uamor(u64 value)
207 {
208 mtspr(SPRN_UAMOR, value);
209 }
210
is_pkey_enabled(int pkey)211 static bool is_pkey_enabled(int pkey)
212 {
213 u64 uamor = read_uamor();
214 u64 pkey_bits = 0x3ul << pkeyshift(pkey);
215 u64 uamor_pkey_bits = (uamor & pkey_bits);
216
217 /*
218 * Both the bits in UAMOR corresponding to the key should be set or
219 * reset.
220 */
221 WARN_ON(uamor_pkey_bits && (uamor_pkey_bits != pkey_bits));
222 return !!(uamor_pkey_bits);
223 }
224
init_amr(int pkey,u8 init_bits)225 static inline void init_amr(int pkey, u8 init_bits)
226 {
227 u64 new_amr_bits = (((u64)init_bits & 0x3UL) << pkeyshift(pkey));
228 u64 old_amr = read_amr() & ~((u64)(0x3ul) << pkeyshift(pkey));
229
230 write_amr(old_amr | new_amr_bits);
231 }
232
init_iamr(int pkey,u8 init_bits)233 static inline void init_iamr(int pkey, u8 init_bits)
234 {
235 u64 new_iamr_bits = (((u64)init_bits & 0x1UL) << pkeyshift(pkey));
236 u64 old_iamr = read_iamr() & ~((u64)(0x1ul) << pkeyshift(pkey));
237
238 write_iamr(old_iamr | new_iamr_bits);
239 }
240
241 /*
242 * Set the access rights in AMR IAMR and UAMOR registers for @pkey to that
243 * specified in @init_val.
244 */
__arch_set_user_pkey_access(struct task_struct * tsk,int pkey,unsigned long init_val)245 int __arch_set_user_pkey_access(struct task_struct *tsk, int pkey,
246 unsigned long init_val)
247 {
248 u64 new_amr_bits = 0x0ul;
249 u64 new_iamr_bits = 0x0ul;
250
251 if (!is_pkey_enabled(pkey))
252 return -EINVAL;
253
254 if (init_val & PKEY_DISABLE_EXECUTE) {
255 if (!pkey_execute_disable_supported)
256 return -EINVAL;
257 new_iamr_bits |= IAMR_EX_BIT;
258 }
259 init_iamr(pkey, new_iamr_bits);
260
261 /* Set the bits we need in AMR: */
262 if (init_val & PKEY_DISABLE_ACCESS)
263 new_amr_bits |= AMR_RD_BIT | AMR_WR_BIT;
264 else if (init_val & PKEY_DISABLE_WRITE)
265 new_amr_bits |= AMR_WR_BIT;
266
267 init_amr(pkey, new_amr_bits);
268 return 0;
269 }
270
thread_pkey_regs_save(struct thread_struct * thread)271 void thread_pkey_regs_save(struct thread_struct *thread)
272 {
273 if (static_branch_likely(&pkey_disabled))
274 return;
275
276 /*
277 * TODO: Skip saving registers if @thread hasn't used any keys yet.
278 */
279 thread->amr = read_amr();
280 thread->iamr = read_iamr();
281 thread->uamor = read_uamor();
282 }
283
thread_pkey_regs_restore(struct thread_struct * new_thread,struct thread_struct * old_thread)284 void thread_pkey_regs_restore(struct thread_struct *new_thread,
285 struct thread_struct *old_thread)
286 {
287 if (static_branch_likely(&pkey_disabled))
288 return;
289
290 if (old_thread->amr != new_thread->amr)
291 write_amr(new_thread->amr);
292 if (old_thread->iamr != new_thread->iamr)
293 write_iamr(new_thread->iamr);
294 if (old_thread->uamor != new_thread->uamor)
295 write_uamor(new_thread->uamor);
296 }
297
thread_pkey_regs_init(struct thread_struct * thread)298 void thread_pkey_regs_init(struct thread_struct *thread)
299 {
300 if (static_branch_likely(&pkey_disabled))
301 return;
302
303 thread->amr = pkey_amr_mask;
304 thread->iamr = pkey_iamr_mask;
305 thread->uamor = pkey_uamor_mask;
306
307 write_uamor(pkey_uamor_mask);
308 write_amr(pkey_amr_mask);
309 write_iamr(pkey_iamr_mask);
310 }
311
pkey_allows_readwrite(int pkey)312 static inline bool pkey_allows_readwrite(int pkey)
313 {
314 int pkey_shift = pkeyshift(pkey);
315
316 if (!is_pkey_enabled(pkey))
317 return true;
318
319 return !(read_amr() & ((AMR_RD_BIT|AMR_WR_BIT) << pkey_shift));
320 }
321
__execute_only_pkey(struct mm_struct * mm)322 int __execute_only_pkey(struct mm_struct *mm)
323 {
324 return mm->context.execute_only_pkey;
325 }
326
vma_is_pkey_exec_only(struct vm_area_struct * vma)327 static inline bool vma_is_pkey_exec_only(struct vm_area_struct *vma)
328 {
329 /* Do this check first since the vm_flags should be hot */
330 if ((vma->vm_flags & (VM_READ | VM_WRITE | VM_EXEC)) != VM_EXEC)
331 return false;
332
333 return (vma_pkey(vma) == vma->vm_mm->context.execute_only_pkey);
334 }
335
336 /*
337 * This should only be called for *plain* mprotect calls.
338 */
__arch_override_mprotect_pkey(struct vm_area_struct * vma,int prot,int pkey)339 int __arch_override_mprotect_pkey(struct vm_area_struct *vma, int prot,
340 int pkey)
341 {
342 /*
343 * If the currently associated pkey is execute-only, but the requested
344 * protection is not execute-only, move it back to the default pkey.
345 */
346 if (vma_is_pkey_exec_only(vma) && (prot != PROT_EXEC))
347 return 0;
348
349 /*
350 * The requested protection is execute-only. Hence let's use an
351 * execute-only pkey.
352 */
353 if (prot == PROT_EXEC) {
354 pkey = execute_only_pkey(vma->vm_mm);
355 if (pkey > 0)
356 return pkey;
357 }
358
359 /* Nothing to override. */
360 return vma_pkey(vma);
361 }
362
pkey_access_permitted(int pkey,bool write,bool execute)363 static bool pkey_access_permitted(int pkey, bool write, bool execute)
364 {
365 int pkey_shift;
366 u64 amr;
367
368 if (!is_pkey_enabled(pkey))
369 return true;
370
371 pkey_shift = pkeyshift(pkey);
372 if (execute)
373 return !(read_iamr() & (IAMR_EX_BIT << pkey_shift));
374
375 amr = read_amr();
376 if (write)
377 return !(amr & (AMR_WR_BIT << pkey_shift));
378
379 return !(amr & (AMR_RD_BIT << pkey_shift));
380 }
381
arch_pte_access_permitted(u64 pte,bool write,bool execute)382 bool arch_pte_access_permitted(u64 pte, bool write, bool execute)
383 {
384 if (static_branch_likely(&pkey_disabled))
385 return true;
386
387 return pkey_access_permitted(pte_to_pkey_bits(pte), write, execute);
388 }
389
390 /*
391 * We only want to enforce protection keys on the current thread because we
392 * effectively have no access to AMR/IAMR for other threads or any way to tell
393 * which AMR/IAMR in a threaded process we could use.
394 *
395 * So do not enforce things if the VMA is not from the current mm, or if we are
396 * in a kernel thread.
397 */
vma_is_foreign(struct vm_area_struct * vma)398 static inline bool vma_is_foreign(struct vm_area_struct *vma)
399 {
400 if (!current->mm)
401 return true;
402
403 /* if it is not our ->mm, it has to be foreign */
404 if (current->mm != vma->vm_mm)
405 return true;
406
407 return false;
408 }
409
arch_vma_access_permitted(struct vm_area_struct * vma,bool write,bool execute,bool foreign)410 bool arch_vma_access_permitted(struct vm_area_struct *vma, bool write,
411 bool execute, bool foreign)
412 {
413 if (static_branch_likely(&pkey_disabled))
414 return true;
415 /*
416 * Do not enforce our key-permissions on a foreign vma.
417 */
418 if (foreign || vma_is_foreign(vma))
419 return true;
420
421 return pkey_access_permitted(vma_pkey(vma), write, execute);
422 }
423
arch_dup_pkeys(struct mm_struct * oldmm,struct mm_struct * mm)424 void arch_dup_pkeys(struct mm_struct *oldmm, struct mm_struct *mm)
425 {
426 if (static_branch_likely(&pkey_disabled))
427 return;
428
429 /* Duplicate the oldmm pkey state in mm: */
430 mm_pkey_allocation_map(mm) = mm_pkey_allocation_map(oldmm);
431 mm->context.execute_only_pkey = oldmm->context.execute_only_pkey;
432 }
433