1 // SPDX-License-Identifier: GPL-2.0+
3 * PowerPC Memory Protection Keys management
5 * Copyright 2017, Ram Pai, IBM Corporation.
9 #include <asm/mmu_context.h>
11 #include <asm/setup.h>
13 #include <asm/firmware.h>
15 #include <linux/pkeys.h>
16 #include <linux/of_fdt.h>
19 int num_pkey; /* Max number of pkeys supported */
21 * Keys marked in the reservation list cannot be allocated by userspace
23 u32 reserved_allocation_mask __ro_after_init;
25 /* Bits set for the initially allocated keys */
26 static u32 initial_allocation_mask __ro_after_init;
29 * Even if we allocate keys with sys_pkey_alloc(), we need to make sure
30 * other thread still find the access denied using the same keys.
32 u64 default_amr __ro_after_init = ~0x0UL;
33 u64 default_iamr __ro_after_init = 0x5555555555555555UL;
34 u64 default_uamor __ro_after_init;
35 EXPORT_SYMBOL(default_amr);
37 * Key used to implement PROT_EXEC mmap. Denies READ/WRITE
38 * We pick key 2 because 0 is special key and 1 is reserved as per ISA.
40 static int execute_only_key = 2;
41 static bool pkey_execute_disable_supported;
44 #define AMR_BITS_PER_PKEY 2
45 #define AMR_RD_BIT 0x1UL
46 #define AMR_WR_BIT 0x2UL
47 #define IAMR_EX_BIT 0x1UL
48 #define PKEY_REG_BITS (sizeof(u64) * 8)
49 #define pkeyshift(pkey) (PKEY_REG_BITS - ((pkey+1) * AMR_BITS_PER_PKEY))
51 static int __init dt_scan_storage_keys(unsigned long node,
52 const char *uname, int depth,
55 const char *type = of_get_flat_dt_prop(node, "device_type", NULL);
57 int *pkeys_total = (int *) data;
59 /* We are scanning "cpu" nodes only */
60 if (type == NULL || strcmp(type, "cpu") != 0)
63 prop = of_get_flat_dt_prop(node, "ibm,processor-storage-keys", NULL);
66 *pkeys_total = be32_to_cpu(prop[0]);
70 static int __init scan_pkey_feature(void)
76 * Pkey is not supported with Radix translation.
78 if (early_radix_enabled())
81 ret = of_scan_flat_dt(dt_scan_storage_keys, &pkeys_total);
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.
88 if (!firmware_has_feature(FW_FEATURE_LPAR)) {
89 unsigned long pvr = mfspr(SPRN_PVR);
91 if (PVR_VER(pvr) == PVR_POWER8 || PVR_VER(pvr) == PVR_POWER8E ||
92 PVR_VER(pvr) == PVR_POWER8NVL || PVR_VER(pvr) == PVR_POWER9)
97 #ifdef CONFIG_PPC_MEM_KEYS
99 * Adjust the upper limit, based on the number of bits supported by
102 pkeys_total = min_t(int, pkeys_total,
103 ((ARCH_VM_PKEY_FLAGS >> VM_PKEY_SHIFT) + 1));
108 void __init pkey_early_init_devtree(void)
112 #ifdef CONFIG_PPC_MEM_KEYS
114 * We define PKEY_DISABLE_EXECUTE in addition to the arch-neutral
115 * generic defines for PKEY_DISABLE_ACCESS and PKEY_DISABLE_WRITE.
116 * Ensure that the bits a distinct.
118 BUILD_BUG_ON(PKEY_DISABLE_EXECUTE &
119 (PKEY_DISABLE_ACCESS | PKEY_DISABLE_WRITE));
122 * pkey_to_vmflag_bits() assumes that the pkey bits are contiguous
123 * in the vmaflag. Make sure that is really the case.
125 BUILD_BUG_ON(__builtin_clzl(ARCH_VM_PKEY_FLAGS >> VM_PKEY_SHIFT) +
126 __builtin_popcountl(ARCH_VM_PKEY_FLAGS >> VM_PKEY_SHIFT)
127 != (sizeof(u64) * BITS_PER_BYTE));
130 * Only P7 and above supports SPRN_AMR update with MSR[PR] = 1
132 if (!early_cpu_has_feature(CPU_FTR_ARCH_206))
135 /* scan the device tree for pkey feature */
136 pkeys_total = scan_pkey_feature();
140 /* Allow all keys to be modified by default */
141 default_uamor = ~0x0UL;
143 cur_cpu_spec->mmu_features |= MMU_FTR_PKEY;
146 * The device tree cannot be relied to indicate support for
147 * execute_disable support. Instead we use a PVR check.
149 if (pvr_version_is(PVR_POWER7) || pvr_version_is(PVR_POWER7p))
150 pkey_execute_disable_supported = false;
152 pkey_execute_disable_supported = true;
154 #ifdef CONFIG_PPC_4K_PAGES
156 * The OS can manage only 8 pkeys due to its inability to represent them
157 * in the Linux 4K PTE. Mark all other keys reserved.
159 num_pkey = min(8, pkeys_total);
161 num_pkey = pkeys_total;
164 if (unlikely(num_pkey <= execute_only_key) || !pkey_execute_disable_supported) {
166 * Insufficient number of keys to support
167 * execute only key. Mark it unavailable.
169 execute_only_key = -1;
172 * Mark the execute_only_pkey as not available for
173 * user allocation via pkey_alloc.
175 reserved_allocation_mask |= (0x1 << execute_only_key);
178 * Deny READ/WRITE for execute_only_key.
179 * Allow execute in IAMR.
181 default_amr |= (0x3ul << pkeyshift(execute_only_key));
182 default_iamr &= ~(0x1ul << pkeyshift(execute_only_key));
185 * Clear the uamor bits for this key.
187 default_uamor &= ~(0x3ul << pkeyshift(execute_only_key));
190 if (unlikely(num_pkey <= 3)) {
192 * Insufficient number of keys to support
197 WARN(1, "Disabling kernel user protection due to low (%d) max supported keys\n", num_pkey);
199 /* handle key which is used by kernel for KAUP */
200 reserved_allocation_mask |= (0x1 << 3);
202 * Mark access for kup_key in default amr so that
203 * we continue to operate with that AMR in
204 * copy_to/from_user().
206 default_amr &= ~(0x3ul << pkeyshift(3));
207 default_iamr &= ~(0x1ul << pkeyshift(3));
208 default_uamor &= ~(0x3ul << pkeyshift(3));
212 * Allow access for only key 0. And prevent any other modification.
214 default_amr &= ~(0x3ul << pkeyshift(0));
215 default_iamr &= ~(0x1ul << pkeyshift(0));
216 default_uamor &= ~(0x3ul << pkeyshift(0));
218 * key 0 is special in that we want to consider it an allocated
219 * key which is preallocated. We don't allow changing AMR bits
220 * w.r.t key 0. But one can pkey_free(key0)
222 initial_allocation_mask |= (0x1 << 0);
225 * key 1 is recommended not to be used. PowerISA(3.0) page 1015,
228 reserved_allocation_mask |= (0x1 << 1);
229 default_uamor &= ~(0x3ul << pkeyshift(1));
232 * Prevent the usage of OS reserved keys. Update UAMOR
233 * for those keys. Also mark the rest of the bits in the
234 * 32 bit mask as reserved.
236 for (i = num_pkey; i < 32 ; i++) {
237 reserved_allocation_mask |= (0x1 << i);
238 default_uamor &= ~(0x3ul << pkeyshift(i));
241 * Prevent the allocation of reserved keys too.
243 initial_allocation_mask |= reserved_allocation_mask;
245 pr_info("Enabling pkeys with max key count %d\n", num_pkey);
248 * Setup uamor on boot cpu
250 mtspr(SPRN_UAMOR, default_uamor);
255 #ifdef CONFIG_PPC_KUEP
256 void setup_kuep(bool disabled)
261 * On hash if PKEY feature is not enabled, disable KUAP too.
263 if (!early_radix_enabled() && !early_mmu_has_feature(MMU_FTR_PKEY))
266 if (smp_processor_id() == boot_cpuid) {
267 pr_info("Activating Kernel Userspace Execution Prevention\n");
268 cur_cpu_spec->mmu_features |= MMU_FTR_BOOK3S_KUEP;
272 * Radix always uses key0 of the IAMR to determine if an access is
273 * allowed. We set bit 0 (IBM bit 1) of key0, to prevent instruction
276 mtspr(SPRN_IAMR, AMR_KUEP_BLOCKED);
281 #ifdef CONFIG_PPC_KUAP
282 void setup_kuap(bool disabled)
287 * On hash if PKEY feature is not enabled, disable KUAP too.
289 if (!early_radix_enabled() && !early_mmu_has_feature(MMU_FTR_PKEY))
292 if (smp_processor_id() == boot_cpuid) {
293 pr_info("Activating Kernel Userspace Access Prevention\n");
294 cur_cpu_spec->mmu_features |= MMU_FTR_KUAP;
298 * Set the default kernel AMR values on all cpus.
300 mtspr(SPRN_AMR, AMR_KUAP_BLOCKED);
305 #ifdef CONFIG_PPC_MEM_KEYS
306 void pkey_mm_init(struct mm_struct *mm)
308 if (!mmu_has_feature(MMU_FTR_PKEY))
310 mm_pkey_allocation_map(mm) = initial_allocation_mask;
311 mm->context.execute_only_pkey = execute_only_key;
314 static inline void init_amr(int pkey, u8 init_bits)
316 u64 new_amr_bits = (((u64)init_bits & 0x3UL) << pkeyshift(pkey));
317 u64 old_amr = current_thread_amr() & ~((u64)(0x3ul) << pkeyshift(pkey));
319 current->thread.regs->amr = old_amr | new_amr_bits;
322 static inline void init_iamr(int pkey, u8 init_bits)
324 u64 new_iamr_bits = (((u64)init_bits & 0x1UL) << pkeyshift(pkey));
325 u64 old_iamr = current_thread_iamr() & ~((u64)(0x1ul) << pkeyshift(pkey));
327 if (!likely(pkey_execute_disable_supported))
330 current->thread.regs->iamr = old_iamr | new_iamr_bits;
334 * Set the access rights in AMR IAMR and UAMOR registers for @pkey to that
335 * specified in @init_val.
337 int __arch_set_user_pkey_access(struct task_struct *tsk, int pkey,
338 unsigned long init_val)
340 u64 new_amr_bits = 0x0ul;
341 u64 new_iamr_bits = 0x0ul;
342 u64 pkey_bits, uamor_pkey_bits;
345 * Check whether the key is disabled by UAMOR.
347 pkey_bits = 0x3ul << pkeyshift(pkey);
348 uamor_pkey_bits = (default_uamor & pkey_bits);
351 * Both the bits in UAMOR corresponding to the key should be set
353 if (uamor_pkey_bits != pkey_bits)
356 if (init_val & PKEY_DISABLE_EXECUTE) {
357 if (!pkey_execute_disable_supported)
359 new_iamr_bits |= IAMR_EX_BIT;
361 init_iamr(pkey, new_iamr_bits);
363 /* Set the bits we need in AMR: */
364 if (init_val & PKEY_DISABLE_ACCESS)
365 new_amr_bits |= AMR_RD_BIT | AMR_WR_BIT;
366 else if (init_val & PKEY_DISABLE_WRITE)
367 new_amr_bits |= AMR_WR_BIT;
369 init_amr(pkey, new_amr_bits);
373 int execute_only_pkey(struct mm_struct *mm)
375 return mm->context.execute_only_pkey;
378 static inline bool vma_is_pkey_exec_only(struct vm_area_struct *vma)
380 /* Do this check first since the vm_flags should be hot */
381 if ((vma->vm_flags & VM_ACCESS_FLAGS) != VM_EXEC)
384 return (vma_pkey(vma) == vma->vm_mm->context.execute_only_pkey);
388 * This should only be called for *plain* mprotect calls.
390 int __arch_override_mprotect_pkey(struct vm_area_struct *vma, int prot,
394 * If the currently associated pkey is execute-only, but the requested
395 * protection is not execute-only, move it back to the default pkey.
397 if (vma_is_pkey_exec_only(vma) && (prot != PROT_EXEC))
401 * The requested protection is execute-only. Hence let's use an
404 if (prot == PROT_EXEC) {
405 pkey = execute_only_pkey(vma->vm_mm);
410 /* Nothing to override. */
411 return vma_pkey(vma);
414 static bool pkey_access_permitted(int pkey, bool write, bool execute)
419 pkey_shift = pkeyshift(pkey);
421 return !(current_thread_iamr() & (IAMR_EX_BIT << pkey_shift));
423 amr = current_thread_amr();
425 return !(amr & (AMR_WR_BIT << pkey_shift));
427 return !(amr & (AMR_RD_BIT << pkey_shift));
430 bool arch_pte_access_permitted(u64 pte, bool write, bool execute)
432 if (!mmu_has_feature(MMU_FTR_PKEY))
435 return pkey_access_permitted(pte_to_pkey_bits(pte), write, execute);
439 * We only want to enforce protection keys on the current thread because we
440 * effectively have no access to AMR/IAMR for other threads or any way to tell
441 * which AMR/IAMR in a threaded process we could use.
443 * So do not enforce things if the VMA is not from the current mm, or if we are
444 * in a kernel thread.
446 bool arch_vma_access_permitted(struct vm_area_struct *vma, bool write,
447 bool execute, bool foreign)
449 if (!mmu_has_feature(MMU_FTR_PKEY))
452 * Do not enforce our key-permissions on a foreign vma.
454 if (foreign || vma_is_foreign(vma))
457 return pkey_access_permitted(vma_pkey(vma), write, execute);
460 void arch_dup_pkeys(struct mm_struct *oldmm, struct mm_struct *mm)
462 if (!mmu_has_feature(MMU_FTR_PKEY))
465 /* Duplicate the oldmm pkey state in mm: */
466 mm_pkey_allocation_map(mm) = mm_pkey_allocation_map(oldmm);
467 mm->context.execute_only_pkey = oldmm->context.execute_only_pkey;
470 #endif /* CONFIG_PPC_MEM_KEYS */