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>
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;
34 EXPORT_SYMBOL(default_amr);
36 * Key used to implement PROT_EXEC mmap. Denies READ/WRITE
37 * We pick key 2 because 0 is special key and 1 is reserved as per ISA.
39 static int execute_only_key = 2;
40 static bool pkey_execute_disable_supported;
43 #define AMR_BITS_PER_PKEY 2
44 #define AMR_RD_BIT 0x1UL
45 #define AMR_WR_BIT 0x2UL
46 #define IAMR_EX_BIT 0x1UL
47 #define PKEY_REG_BITS (sizeof(u64) * 8)
48 #define pkeyshift(pkey) (PKEY_REG_BITS - ((pkey+1) * AMR_BITS_PER_PKEY))
50 static int __init dt_scan_storage_keys(unsigned long node,
51 const char *uname, int depth,
54 const char *type = of_get_flat_dt_prop(node, "device_type", NULL);
56 int *pkeys_total = (int *) data;
58 /* We are scanning "cpu" nodes only */
59 if (type == NULL || strcmp(type, "cpu") != 0)
62 prop = of_get_flat_dt_prop(node, "ibm,processor-storage-keys", NULL);
65 *pkeys_total = be32_to_cpu(prop[0]);
69 static int __init scan_pkey_feature(void)
75 * Pkey is not supported with Radix translation.
77 if (early_radix_enabled())
80 ret = of_scan_flat_dt(dt_scan_storage_keys, &pkeys_total);
83 * Let's assume 32 pkeys on P8/P9 bare metal, if its not defined by device
84 * tree. We make this exception since some version of skiboot forgot to
85 * expose this property on power8/9.
87 if (!firmware_has_feature(FW_FEATURE_LPAR)) {
88 unsigned long pvr = mfspr(SPRN_PVR);
90 if (PVR_VER(pvr) == PVR_POWER8 || PVR_VER(pvr) == PVR_POWER8E ||
91 PVR_VER(pvr) == PVR_POWER8NVL || PVR_VER(pvr) == PVR_POWER9)
96 #ifdef CONFIG_PPC_MEM_KEYS
98 * Adjust the upper limit, based on the number of bits supported by
101 pkeys_total = min_t(int, pkeys_total,
102 ((ARCH_VM_PKEY_FLAGS >> VM_PKEY_SHIFT) + 1));
107 void __init pkey_early_init_devtree(void)
111 #ifdef CONFIG_PPC_MEM_KEYS
113 * We define PKEY_DISABLE_EXECUTE in addition to the arch-neutral
114 * generic defines for PKEY_DISABLE_ACCESS and PKEY_DISABLE_WRITE.
115 * Ensure that the bits a distinct.
117 BUILD_BUG_ON(PKEY_DISABLE_EXECUTE &
118 (PKEY_DISABLE_ACCESS | PKEY_DISABLE_WRITE));
121 * pkey_to_vmflag_bits() assumes that the pkey bits are contiguous
122 * in the vmaflag. Make sure that is really the case.
124 BUILD_BUG_ON(__builtin_clzl(ARCH_VM_PKEY_FLAGS >> VM_PKEY_SHIFT) +
125 __builtin_popcountl(ARCH_VM_PKEY_FLAGS >> VM_PKEY_SHIFT)
126 != (sizeof(u64) * BITS_PER_BYTE));
129 * Only P7 and above supports SPRN_AMR update with MSR[PR] = 1
131 if (!early_cpu_has_feature(CPU_FTR_ARCH_206))
134 /* scan the device tree for pkey feature */
135 pkeys_total = scan_pkey_feature();
139 /* Allow all keys to be modified by default */
140 default_uamor = ~0x0UL;
142 cur_cpu_spec->mmu_features |= MMU_FTR_PKEY;
145 * The device tree cannot be relied to indicate support for
146 * execute_disable support. Instead we use a PVR check.
148 if (pvr_version_is(PVR_POWER7) || pvr_version_is(PVR_POWER7p))
149 pkey_execute_disable_supported = false;
151 pkey_execute_disable_supported = true;
153 #ifdef CONFIG_PPC_4K_PAGES
155 * The OS can manage only 8 pkeys due to its inability to represent them
156 * in the Linux 4K PTE. Mark all other keys reserved.
158 num_pkey = min(8, pkeys_total);
160 num_pkey = pkeys_total;
163 if (unlikely(num_pkey <= execute_only_key) || !pkey_execute_disable_supported) {
165 * Insufficient number of keys to support
166 * execute only key. Mark it unavailable.
168 execute_only_key = -1;
171 * Mark the execute_only_pkey as not available for
172 * user allocation via pkey_alloc.
174 reserved_allocation_mask |= (0x1 << execute_only_key);
177 * Deny READ/WRITE for execute_only_key.
178 * Allow execute in IAMR.
180 default_amr |= (0x3ul << pkeyshift(execute_only_key));
181 default_iamr &= ~(0x1ul << pkeyshift(execute_only_key));
184 * Clear the uamor bits for this key.
186 default_uamor &= ~(0x3ul << pkeyshift(execute_only_key));
189 if (unlikely(num_pkey <= 3)) {
191 * Insufficient number of keys to support
196 WARN(1, "Disabling kernel user protection due to low (%d) max supported keys\n", num_pkey);
198 /* handle key which is used by kernel for KAUP */
199 reserved_allocation_mask |= (0x1 << 3);
201 * Mark access for kup_key in default amr so that
202 * we continue to operate with that AMR in
203 * copy_to/from_user().
205 default_amr &= ~(0x3ul << pkeyshift(3));
206 default_iamr &= ~(0x1ul << pkeyshift(3));
207 default_uamor &= ~(0x3ul << pkeyshift(3));
211 * Allow access for only key 0. And prevent any other modification.
213 default_amr &= ~(0x3ul << pkeyshift(0));
214 default_iamr &= ~(0x1ul << pkeyshift(0));
215 default_uamor &= ~(0x3ul << pkeyshift(0));
217 * key 0 is special in that we want to consider it an allocated
218 * key which is preallocated. We don't allow changing AMR bits
219 * w.r.t key 0. But one can pkey_free(key0)
221 initial_allocation_mask |= (0x1 << 0);
224 * key 1 is recommended not to be used. PowerISA(3.0) page 1015,
227 reserved_allocation_mask |= (0x1 << 1);
228 default_uamor &= ~(0x3ul << pkeyshift(1));
231 * Prevent the usage of OS reserved keys. Update UAMOR
232 * for those keys. Also mark the rest of the bits in the
233 * 32 bit mask as reserved.
235 for (i = num_pkey; i < 32 ; i++) {
236 reserved_allocation_mask |= (0x1 << i);
237 default_uamor &= ~(0x3ul << pkeyshift(i));
240 * Prevent the allocation of reserved keys too.
242 initial_allocation_mask |= reserved_allocation_mask;
244 pr_info("Enabling pkeys with max key count %d\n", num_pkey);
247 * Setup uamor on boot cpu
249 mtspr(SPRN_UAMOR, default_uamor);
254 #ifdef CONFIG_PPC_KUEP
255 void setup_kuep(bool disabled)
260 * On hash if PKEY feature is not enabled, disable KUAP too.
262 if (!early_radix_enabled() && !early_mmu_has_feature(MMU_FTR_PKEY))
265 if (smp_processor_id() == boot_cpuid) {
266 pr_info("Activating Kernel Userspace Execution Prevention\n");
267 cur_cpu_spec->mmu_features |= MMU_FTR_BOOK3S_KUEP;
271 * Radix always uses key0 of the IAMR to determine if an access is
272 * allowed. We set bit 0 (IBM bit 1) of key0, to prevent instruction
275 mtspr(SPRN_IAMR, AMR_KUEP_BLOCKED);
280 #ifdef CONFIG_PPC_KUAP
281 void setup_kuap(bool disabled)
286 * On hash if PKEY feature is not enabled, disable KUAP too.
288 if (!early_radix_enabled() && !early_mmu_has_feature(MMU_FTR_PKEY))
291 if (smp_processor_id() == boot_cpuid) {
292 pr_info("Activating Kernel Userspace Access Prevention\n");
293 cur_cpu_spec->mmu_features |= MMU_FTR_BOOK3S_KUAP;
297 * Set the default kernel AMR values on all cpus.
299 mtspr(SPRN_AMR, AMR_KUAP_BLOCKED);
304 #ifdef CONFIG_PPC_MEM_KEYS
305 void pkey_mm_init(struct mm_struct *mm)
307 if (!mmu_has_feature(MMU_FTR_PKEY))
309 mm_pkey_allocation_map(mm) = initial_allocation_mask;
310 mm->context.execute_only_pkey = execute_only_key;
313 static inline void init_amr(int pkey, u8 init_bits)
315 u64 new_amr_bits = (((u64)init_bits & 0x3UL) << pkeyshift(pkey));
316 u64 old_amr = current_thread_amr() & ~((u64)(0x3ul) << pkeyshift(pkey));
318 current->thread.regs->amr = old_amr | new_amr_bits;
321 static inline void init_iamr(int pkey, u8 init_bits)
323 u64 new_iamr_bits = (((u64)init_bits & 0x1UL) << pkeyshift(pkey));
324 u64 old_iamr = current_thread_iamr() & ~((u64)(0x1ul) << pkeyshift(pkey));
326 if (!likely(pkey_execute_disable_supported))
329 current->thread.regs->iamr = old_iamr | new_iamr_bits;
333 * Set the access rights in AMR IAMR and UAMOR registers for @pkey to that
334 * specified in @init_val.
336 int __arch_set_user_pkey_access(struct task_struct *tsk, int pkey,
337 unsigned long init_val)
339 u64 new_amr_bits = 0x0ul;
340 u64 new_iamr_bits = 0x0ul;
341 u64 pkey_bits, uamor_pkey_bits;
344 * Check whether the key is disabled by UAMOR.
346 pkey_bits = 0x3ul << pkeyshift(pkey);
347 uamor_pkey_bits = (default_uamor & pkey_bits);
350 * Both the bits in UAMOR corresponding to the key should be set
352 if (uamor_pkey_bits != pkey_bits)
355 if (init_val & PKEY_DISABLE_EXECUTE) {
356 if (!pkey_execute_disable_supported)
358 new_iamr_bits |= IAMR_EX_BIT;
360 init_iamr(pkey, new_iamr_bits);
362 /* Set the bits we need in AMR: */
363 if (init_val & PKEY_DISABLE_ACCESS)
364 new_amr_bits |= AMR_RD_BIT | AMR_WR_BIT;
365 else if (init_val & PKEY_DISABLE_WRITE)
366 new_amr_bits |= AMR_WR_BIT;
368 init_amr(pkey, new_amr_bits);
372 int execute_only_pkey(struct mm_struct *mm)
374 return mm->context.execute_only_pkey;
377 static inline bool vma_is_pkey_exec_only(struct vm_area_struct *vma)
379 /* Do this check first since the vm_flags should be hot */
380 if ((vma->vm_flags & VM_ACCESS_FLAGS) != VM_EXEC)
383 return (vma_pkey(vma) == vma->vm_mm->context.execute_only_pkey);
387 * This should only be called for *plain* mprotect calls.
389 int __arch_override_mprotect_pkey(struct vm_area_struct *vma, int prot,
393 * If the currently associated pkey is execute-only, but the requested
394 * protection is not execute-only, move it back to the default pkey.
396 if (vma_is_pkey_exec_only(vma) && (prot != PROT_EXEC))
400 * The requested protection is execute-only. Hence let's use an
403 if (prot == PROT_EXEC) {
404 pkey = execute_only_pkey(vma->vm_mm);
409 /* Nothing to override. */
410 return vma_pkey(vma);
413 static bool pkey_access_permitted(int pkey, bool write, bool execute)
418 pkey_shift = pkeyshift(pkey);
420 return !(current_thread_iamr() & (IAMR_EX_BIT << pkey_shift));
422 amr = current_thread_amr();
424 return !(amr & (AMR_WR_BIT << pkey_shift));
426 return !(amr & (AMR_RD_BIT << pkey_shift));
429 bool arch_pte_access_permitted(u64 pte, bool write, bool execute)
431 if (!mmu_has_feature(MMU_FTR_PKEY))
434 return pkey_access_permitted(pte_to_pkey_bits(pte), write, execute);
438 * We only want to enforce protection keys on the current thread because we
439 * effectively have no access to AMR/IAMR for other threads or any way to tell
440 * which AMR/IAMR in a threaded process we could use.
442 * So do not enforce things if the VMA is not from the current mm, or if we are
443 * in a kernel thread.
445 bool arch_vma_access_permitted(struct vm_area_struct *vma, bool write,
446 bool execute, bool foreign)
448 if (!mmu_has_feature(MMU_FTR_PKEY))
451 * Do not enforce our key-permissions on a foreign vma.
453 if (foreign || vma_is_foreign(vma))
456 return pkey_access_permitted(vma_pkey(vma), write, execute);
459 void arch_dup_pkeys(struct mm_struct *oldmm, struct mm_struct *mm)
461 if (!mmu_has_feature(MMU_FTR_PKEY))
464 /* Duplicate the oldmm pkey state in mm: */
465 mm_pkey_allocation_map(mm) = mm_pkey_allocation_map(oldmm);
466 mm->context.execute_only_pkey = oldmm->context.execute_only_pkey;
469 #endif /* CONFIG_PPC_MEM_KEYS */