Merge tag 'linux-kselftest-kunit-fixes-5.11-rc3' of git://git.kernel.org/pub/scm...
[linux/fpc-iii.git] / arch / powerpc / mm / book3s64 / pkeys.c
blobf1c6f264ed911c56181d5636d1d09139c589782a
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * PowerPC Memory Protection Keys management
5 * Copyright 2017, Ram Pai, IBM Corporation.
6 */
8 #include <asm/mman.h>
9 #include <asm/mmu_context.h>
10 #include <asm/mmu.h>
11 #include <asm/setup.h>
12 #include <asm/smp.h>
14 #include <linux/pkeys.h>
15 #include <linux/of_fdt.h>
18 int num_pkey; /* Max number of pkeys supported */
20 * Keys marked in the reservation list cannot be allocated by userspace
22 u32 reserved_allocation_mask __ro_after_init;
24 /* Bits set for the initially allocated keys */
25 static u32 initial_allocation_mask __ro_after_init;
28 * Even if we allocate keys with sys_pkey_alloc(), we need to make sure
29 * other thread still find the access denied using the same keys.
31 u64 default_amr __ro_after_init = ~0x0UL;
32 u64 default_iamr __ro_after_init = 0x5555555555555555UL;
33 u64 default_uamor __ro_after_init;
35 * Key used to implement PROT_EXEC mmap. Denies READ/WRITE
36 * We pick key 2 because 0 is special key and 1 is reserved as per ISA.
38 static int execute_only_key = 2;
39 static bool pkey_execute_disable_supported;
42 #define AMR_BITS_PER_PKEY 2
43 #define AMR_RD_BIT 0x1UL
44 #define AMR_WR_BIT 0x2UL
45 #define IAMR_EX_BIT 0x1UL
46 #define PKEY_REG_BITS (sizeof(u64) * 8)
47 #define pkeyshift(pkey) (PKEY_REG_BITS - ((pkey+1) * AMR_BITS_PER_PKEY))
49 static int __init dt_scan_storage_keys(unsigned long node,
50 const char *uname, int depth,
51 void *data)
53 const char *type = of_get_flat_dt_prop(node, "device_type", NULL);
54 const __be32 *prop;
55 int *pkeys_total = (int *) data;
57 /* We are scanning "cpu" nodes only */
58 if (type == NULL || strcmp(type, "cpu") != 0)
59 return 0;
61 prop = of_get_flat_dt_prop(node, "ibm,processor-storage-keys", NULL);
62 if (!prop)
63 return 0;
64 *pkeys_total = be32_to_cpu(prop[0]);
65 return 1;
68 static int scan_pkey_feature(void)
70 int ret;
71 int pkeys_total = 0;
74 * Pkey is not supported with Radix translation.
76 if (early_radix_enabled())
77 return 0;
79 ret = of_scan_flat_dt(dt_scan_storage_keys, &pkeys_total);
80 if (ret == 0) {
82 * Let's assume 32 pkeys on P8/P9 bare metal, if its not defined by device
83 * tree. We make this exception since some version of skiboot forgot to
84 * expose this property on power8/9.
86 if (!firmware_has_feature(FW_FEATURE_LPAR)) {
87 unsigned long pvr = mfspr(SPRN_PVR);
89 if (PVR_VER(pvr) == PVR_POWER8 || PVR_VER(pvr) == PVR_POWER8E ||
90 PVR_VER(pvr) == PVR_POWER8NVL || PVR_VER(pvr) == PVR_POWER9)
91 pkeys_total = 32;
95 #ifdef CONFIG_PPC_MEM_KEYS
97 * Adjust the upper limit, based on the number of bits supported by
98 * arch-neutral code.
100 pkeys_total = min_t(int, pkeys_total,
101 ((ARCH_VM_PKEY_FLAGS >> VM_PKEY_SHIFT) + 1));
102 #endif
103 return pkeys_total;
106 void __init pkey_early_init_devtree(void)
108 int pkeys_total, i;
110 #ifdef CONFIG_PPC_MEM_KEYS
112 * We define PKEY_DISABLE_EXECUTE in addition to the arch-neutral
113 * generic defines for PKEY_DISABLE_ACCESS and PKEY_DISABLE_WRITE.
114 * Ensure that the bits a distinct.
116 BUILD_BUG_ON(PKEY_DISABLE_EXECUTE &
117 (PKEY_DISABLE_ACCESS | PKEY_DISABLE_WRITE));
120 * pkey_to_vmflag_bits() assumes that the pkey bits are contiguous
121 * in the vmaflag. Make sure that is really the case.
123 BUILD_BUG_ON(__builtin_clzl(ARCH_VM_PKEY_FLAGS >> VM_PKEY_SHIFT) +
124 __builtin_popcountl(ARCH_VM_PKEY_FLAGS >> VM_PKEY_SHIFT)
125 != (sizeof(u64) * BITS_PER_BYTE));
126 #endif
128 * Only P7 and above supports SPRN_AMR update with MSR[PR] = 1
130 if (!early_cpu_has_feature(CPU_FTR_ARCH_206))
131 return;
133 /* scan the device tree for pkey feature */
134 pkeys_total = scan_pkey_feature();
135 if (!pkeys_total)
136 goto out;
138 /* Allow all keys to be modified by default */
139 default_uamor = ~0x0UL;
141 cur_cpu_spec->mmu_features |= MMU_FTR_PKEY;
144 * The device tree cannot be relied to indicate support for
145 * execute_disable support. Instead we use a PVR check.
147 if (pvr_version_is(PVR_POWER7) || pvr_version_is(PVR_POWER7p))
148 pkey_execute_disable_supported = false;
149 else
150 pkey_execute_disable_supported = true;
152 #ifdef CONFIG_PPC_4K_PAGES
154 * The OS can manage only 8 pkeys due to its inability to represent them
155 * in the Linux 4K PTE. Mark all other keys reserved.
157 num_pkey = min(8, pkeys_total);
158 #else
159 num_pkey = pkeys_total;
160 #endif
162 if (unlikely(num_pkey <= execute_only_key) || !pkey_execute_disable_supported) {
164 * Insufficient number of keys to support
165 * execute only key. Mark it unavailable.
167 execute_only_key = -1;
168 } else {
170 * Mark the execute_only_pkey as not available for
171 * user allocation via pkey_alloc.
173 reserved_allocation_mask |= (0x1 << execute_only_key);
176 * Deny READ/WRITE for execute_only_key.
177 * Allow execute in IAMR.
179 default_amr |= (0x3ul << pkeyshift(execute_only_key));
180 default_iamr &= ~(0x1ul << pkeyshift(execute_only_key));
183 * Clear the uamor bits for this key.
185 default_uamor &= ~(0x3ul << pkeyshift(execute_only_key));
188 if (unlikely(num_pkey <= 3)) {
190 * Insufficient number of keys to support
191 * KUAP/KUEP feature.
193 disable_kuep = true;
194 disable_kuap = true;
195 WARN(1, "Disabling kernel user protection due to low (%d) max supported keys\n", num_pkey);
196 } else {
197 /* handle key which is used by kernel for KAUP */
198 reserved_allocation_mask |= (0x1 << 3);
200 * Mark access for kup_key in default amr so that
201 * we continue to operate with that AMR in
202 * copy_to/from_user().
204 default_amr &= ~(0x3ul << pkeyshift(3));
205 default_iamr &= ~(0x1ul << pkeyshift(3));
206 default_uamor &= ~(0x3ul << pkeyshift(3));
210 * Allow access for only key 0. And prevent any other modification.
212 default_amr &= ~(0x3ul << pkeyshift(0));
213 default_iamr &= ~(0x1ul << pkeyshift(0));
214 default_uamor &= ~(0x3ul << pkeyshift(0));
216 * key 0 is special in that we want to consider it an allocated
217 * key which is preallocated. We don't allow changing AMR bits
218 * w.r.t key 0. But one can pkey_free(key0)
220 initial_allocation_mask |= (0x1 << 0);
223 * key 1 is recommended not to be used. PowerISA(3.0) page 1015,
224 * programming note.
226 reserved_allocation_mask |= (0x1 << 1);
227 default_uamor &= ~(0x3ul << pkeyshift(1));
230 * Prevent the usage of OS reserved keys. Update UAMOR
231 * for those keys. Also mark the rest of the bits in the
232 * 32 bit mask as reserved.
234 for (i = num_pkey; i < 32 ; i++) {
235 reserved_allocation_mask |= (0x1 << i);
236 default_uamor &= ~(0x3ul << pkeyshift(i));
239 * Prevent the allocation of reserved keys too.
241 initial_allocation_mask |= reserved_allocation_mask;
243 pr_info("Enabling pkeys with max key count %d\n", num_pkey);
244 out:
246 * Setup uamor on boot cpu
248 mtspr(SPRN_UAMOR, default_uamor);
250 return;
253 #ifdef CONFIG_PPC_KUEP
254 void setup_kuep(bool disabled)
256 if (disabled)
257 return;
259 * On hash if PKEY feature is not enabled, disable KUAP too.
261 if (!early_radix_enabled() && !early_mmu_has_feature(MMU_FTR_PKEY))
262 return;
264 if (smp_processor_id() == boot_cpuid) {
265 pr_info("Activating Kernel Userspace Execution Prevention\n");
266 cur_cpu_spec->mmu_features |= MMU_FTR_BOOK3S_KUEP;
270 * Radix always uses key0 of the IAMR to determine if an access is
271 * allowed. We set bit 0 (IBM bit 1) of key0, to prevent instruction
272 * fetch.
274 mtspr(SPRN_IAMR, AMR_KUEP_BLOCKED);
275 isync();
277 #endif
279 #ifdef CONFIG_PPC_KUAP
280 void setup_kuap(bool disabled)
282 if (disabled)
283 return;
285 * On hash if PKEY feature is not enabled, disable KUAP too.
287 if (!early_radix_enabled() && !early_mmu_has_feature(MMU_FTR_PKEY))
288 return;
290 if (smp_processor_id() == boot_cpuid) {
291 pr_info("Activating Kernel Userspace Access Prevention\n");
292 cur_cpu_spec->mmu_features |= MMU_FTR_BOOK3S_KUAP;
296 * Set the default kernel AMR values on all cpus.
298 mtspr(SPRN_AMR, AMR_KUAP_BLOCKED);
299 isync();
301 #endif
303 static inline void update_current_thread_amr(u64 value)
305 current->thread.regs->amr = value;
308 static inline void update_current_thread_iamr(u64 value)
310 if (!likely(pkey_execute_disable_supported))
311 return;
313 current->thread.regs->iamr = value;
316 #ifdef CONFIG_PPC_MEM_KEYS
317 void pkey_mm_init(struct mm_struct *mm)
319 if (!mmu_has_feature(MMU_FTR_PKEY))
320 return;
321 mm_pkey_allocation_map(mm) = initial_allocation_mask;
322 mm->context.execute_only_pkey = execute_only_key;
325 static inline void init_amr(int pkey, u8 init_bits)
327 u64 new_amr_bits = (((u64)init_bits & 0x3UL) << pkeyshift(pkey));
328 u64 old_amr = current_thread_amr() & ~((u64)(0x3ul) << pkeyshift(pkey));
330 update_current_thread_amr(old_amr | new_amr_bits);
333 static inline void init_iamr(int pkey, u8 init_bits)
335 u64 new_iamr_bits = (((u64)init_bits & 0x1UL) << pkeyshift(pkey));
336 u64 old_iamr = current_thread_iamr() & ~((u64)(0x1ul) << pkeyshift(pkey));
338 update_current_thread_iamr(old_iamr | new_iamr_bits);
342 * Set the access rights in AMR IAMR and UAMOR registers for @pkey to that
343 * specified in @init_val.
345 int __arch_set_user_pkey_access(struct task_struct *tsk, int pkey,
346 unsigned long init_val)
348 u64 new_amr_bits = 0x0ul;
349 u64 new_iamr_bits = 0x0ul;
350 u64 pkey_bits, uamor_pkey_bits;
353 * Check whether the key is disabled by UAMOR.
355 pkey_bits = 0x3ul << pkeyshift(pkey);
356 uamor_pkey_bits = (default_uamor & pkey_bits);
359 * Both the bits in UAMOR corresponding to the key should be set
361 if (uamor_pkey_bits != pkey_bits)
362 return -EINVAL;
364 if (init_val & PKEY_DISABLE_EXECUTE) {
365 if (!pkey_execute_disable_supported)
366 return -EINVAL;
367 new_iamr_bits |= IAMR_EX_BIT;
369 init_iamr(pkey, new_iamr_bits);
371 /* Set the bits we need in AMR: */
372 if (init_val & PKEY_DISABLE_ACCESS)
373 new_amr_bits |= AMR_RD_BIT | AMR_WR_BIT;
374 else if (init_val & PKEY_DISABLE_WRITE)
375 new_amr_bits |= AMR_WR_BIT;
377 init_amr(pkey, new_amr_bits);
378 return 0;
381 int execute_only_pkey(struct mm_struct *mm)
383 return mm->context.execute_only_pkey;
386 static inline bool vma_is_pkey_exec_only(struct vm_area_struct *vma)
388 /* Do this check first since the vm_flags should be hot */
389 if ((vma->vm_flags & VM_ACCESS_FLAGS) != VM_EXEC)
390 return false;
392 return (vma_pkey(vma) == vma->vm_mm->context.execute_only_pkey);
396 * This should only be called for *plain* mprotect calls.
398 int __arch_override_mprotect_pkey(struct vm_area_struct *vma, int prot,
399 int pkey)
402 * If the currently associated pkey is execute-only, but the requested
403 * protection is not execute-only, move it back to the default pkey.
405 if (vma_is_pkey_exec_only(vma) && (prot != PROT_EXEC))
406 return 0;
409 * The requested protection is execute-only. Hence let's use an
410 * execute-only pkey.
412 if (prot == PROT_EXEC) {
413 pkey = execute_only_pkey(vma->vm_mm);
414 if (pkey > 0)
415 return pkey;
418 /* Nothing to override. */
419 return vma_pkey(vma);
422 static bool pkey_access_permitted(int pkey, bool write, bool execute)
424 int pkey_shift;
425 u64 amr;
427 pkey_shift = pkeyshift(pkey);
428 if (execute)
429 return !(current_thread_iamr() & (IAMR_EX_BIT << pkey_shift));
431 amr = current_thread_amr();
432 if (write)
433 return !(amr & (AMR_WR_BIT << pkey_shift));
435 return !(amr & (AMR_RD_BIT << pkey_shift));
438 bool arch_pte_access_permitted(u64 pte, bool write, bool execute)
440 if (!mmu_has_feature(MMU_FTR_PKEY))
441 return true;
443 return pkey_access_permitted(pte_to_pkey_bits(pte), write, execute);
447 * We only want to enforce protection keys on the current thread because we
448 * effectively have no access to AMR/IAMR for other threads or any way to tell
449 * which AMR/IAMR in a threaded process we could use.
451 * So do not enforce things if the VMA is not from the current mm, or if we are
452 * in a kernel thread.
454 bool arch_vma_access_permitted(struct vm_area_struct *vma, bool write,
455 bool execute, bool foreign)
457 if (!mmu_has_feature(MMU_FTR_PKEY))
458 return true;
460 * Do not enforce our key-permissions on a foreign vma.
462 if (foreign || vma_is_foreign(vma))
463 return true;
465 return pkey_access_permitted(vma_pkey(vma), write, execute);
468 void arch_dup_pkeys(struct mm_struct *oldmm, struct mm_struct *mm)
470 if (!mmu_has_feature(MMU_FTR_PKEY))
471 return;
473 /* Duplicate the oldmm pkey state in mm: */
474 mm_pkey_allocation_map(mm) = mm_pkey_allocation_map(oldmm);
475 mm->context.execute_only_pkey = oldmm->context.execute_only_pkey;
478 #endif /* CONFIG_PPC_MEM_KEYS */