1 /* SPDX-License-Identifier: GPL-2.0+ */
3 * PowerPC Memory Protection Keys management
5 * Copyright 2017, Ram Pai, IBM Corporation.
8 #ifndef _ASM_POWERPC_KEYS_H
9 #define _ASM_POWERPC_KEYS_H
11 #include <linux/jump_label.h>
12 #include <asm/firmware.h>
15 extern u32 reserved_allocation_mask; /* bits set for reserved keys */
17 #define ARCH_VM_PKEY_FLAGS (VM_PKEY_BIT0 | VM_PKEY_BIT1 | VM_PKEY_BIT2 | \
18 VM_PKEY_BIT3 | VM_PKEY_BIT4)
20 /* Override any generic PKEY permission defines */
21 #define PKEY_DISABLE_EXECUTE 0x4
22 #define PKEY_ACCESS_MASK (PKEY_DISABLE_ACCESS | \
23 PKEY_DISABLE_WRITE | \
26 #ifdef CONFIG_PPC_BOOK3S_64
27 #include <asm/book3s/64/pkeys.h>
29 #error "Not supported"
33 static inline u64 pkey_to_vmflag_bits(u16 pkey)
35 return (((u64)pkey << VM_PKEY_SHIFT) & ARCH_VM_PKEY_FLAGS);
38 static inline int vma_pkey(struct vm_area_struct *vma)
40 if (!mmu_has_feature(MMU_FTR_PKEY))
42 return (vma->vm_flags & ARCH_VM_PKEY_FLAGS) >> VM_PKEY_SHIFT;
45 static inline int arch_max_pkey(void)
50 #define pkey_alloc_mask(pkey) (0x1 << pkey)
52 #define mm_pkey_allocation_map(mm) (mm->context.pkey_allocation_map)
54 #define __mm_pkey_allocated(mm, pkey) { \
55 mm_pkey_allocation_map(mm) |= pkey_alloc_mask(pkey); \
58 #define __mm_pkey_free(mm, pkey) { \
59 mm_pkey_allocation_map(mm) &= ~pkey_alloc_mask(pkey); \
62 #define __mm_pkey_is_allocated(mm, pkey) \
63 (mm_pkey_allocation_map(mm) & pkey_alloc_mask(pkey))
65 #define __mm_pkey_is_reserved(pkey) (reserved_allocation_mask & \
66 pkey_alloc_mask(pkey))
68 static inline bool mm_pkey_is_allocated(struct mm_struct *mm, int pkey)
70 if (pkey < 0 || pkey >= arch_max_pkey())
73 /* Reserved keys are never allocated. */
74 if (__mm_pkey_is_reserved(pkey))
77 return __mm_pkey_is_allocated(mm, pkey);
81 * Returns a positive, 5-bit key on success, or -1 on failure.
82 * Relies on the mmap_lock to protect against concurrency in mm_pkey_alloc() and
85 static inline int mm_pkey_alloc(struct mm_struct *mm)
88 * Note: this is the one and only place we make sure that the pkey is
89 * valid as far as the hardware is concerned. The rest of the kernel
90 * trusts that only good, valid pkeys come out of here.
92 u32 all_pkeys_mask = (u32)(~(0x0));
95 if (!mmu_has_feature(MMU_FTR_PKEY))
98 * Are we out of pkeys? We must handle this specially because ffz()
99 * behavior is undefined if there are no zeros.
101 if (mm_pkey_allocation_map(mm) == all_pkeys_mask)
104 ret = ffz((u32)mm_pkey_allocation_map(mm));
105 __mm_pkey_allocated(mm, ret);
110 static inline int mm_pkey_free(struct mm_struct *mm, int pkey)
112 if (!mmu_has_feature(MMU_FTR_PKEY))
115 if (!mm_pkey_is_allocated(mm, pkey))
118 __mm_pkey_free(mm, pkey);
124 * Try to dedicate one of the protection keys to be used as an
125 * execute-only protection key.
127 extern int execute_only_pkey(struct mm_struct *mm);
128 extern int __arch_override_mprotect_pkey(struct vm_area_struct *vma,
130 static inline int arch_override_mprotect_pkey(struct vm_area_struct *vma,
133 if (!mmu_has_feature(MMU_FTR_PKEY))
137 * Is this an mprotect_pkey() call? If so, never override the value that
138 * came from the user.
143 return __arch_override_mprotect_pkey(vma, prot, pkey);
146 extern int __arch_set_user_pkey_access(struct task_struct *tsk, int pkey,
147 unsigned long init_val);
148 static inline int arch_set_user_pkey_access(struct task_struct *tsk, int pkey,
149 unsigned long init_val)
151 if (!mmu_has_feature(MMU_FTR_PKEY))
155 * userspace should not change pkey-0 permissions.
156 * pkey-0 is associated with every page in the kernel.
157 * If userspace denies any permission on pkey-0, the
158 * kernel cannot operate.
161 return init_val ? -EINVAL : 0;
163 return __arch_set_user_pkey_access(tsk, pkey, init_val);
166 static inline bool arch_pkeys_enabled(void)
168 return mmu_has_feature(MMU_FTR_PKEY);
171 extern void pkey_mm_init(struct mm_struct *mm);
172 #endif /*_ASM_POWERPC_KEYS_H */