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>
14 DECLARE_STATIC_KEY_TRUE(pkey_disabled
);
15 extern int pkeys_total
; /* total pkeys as per device tree */
16 extern u32 initial_allocation_mask
; /* bits set for the initially allocated keys */
17 extern u32 reserved_allocation_mask
; /* bits set for reserved keys */
19 #define ARCH_VM_PKEY_FLAGS (VM_PKEY_BIT0 | VM_PKEY_BIT1 | VM_PKEY_BIT2 | \
20 VM_PKEY_BIT3 | VM_PKEY_BIT4)
22 /* Override any generic PKEY permission defines */
23 #define PKEY_DISABLE_EXECUTE 0x4
24 #define PKEY_ACCESS_MASK (PKEY_DISABLE_ACCESS | \
25 PKEY_DISABLE_WRITE | \
28 static inline u64
pkey_to_vmflag_bits(u16 pkey
)
30 return (((u64
)pkey
<< VM_PKEY_SHIFT
) & ARCH_VM_PKEY_FLAGS
);
33 static inline u64
vmflag_to_pte_pkey_bits(u64 vm_flags
)
35 if (static_branch_likely(&pkey_disabled
))
38 return (((vm_flags
& VM_PKEY_BIT0
) ? H_PTE_PKEY_BIT4
: 0x0UL
) |
39 ((vm_flags
& VM_PKEY_BIT1
) ? H_PTE_PKEY_BIT3
: 0x0UL
) |
40 ((vm_flags
& VM_PKEY_BIT2
) ? H_PTE_PKEY_BIT2
: 0x0UL
) |
41 ((vm_flags
& VM_PKEY_BIT3
) ? H_PTE_PKEY_BIT1
: 0x0UL
) |
42 ((vm_flags
& VM_PKEY_BIT4
) ? H_PTE_PKEY_BIT0
: 0x0UL
));
45 static inline int vma_pkey(struct vm_area_struct
*vma
)
47 if (static_branch_likely(&pkey_disabled
))
49 return (vma
->vm_flags
& ARCH_VM_PKEY_FLAGS
) >> VM_PKEY_SHIFT
;
52 #define arch_max_pkey() pkeys_total
54 static inline u64
pte_to_hpte_pkey_bits(u64 pteflags
)
56 return (((pteflags
& H_PTE_PKEY_BIT0
) ? HPTE_R_KEY_BIT0
: 0x0UL
) |
57 ((pteflags
& H_PTE_PKEY_BIT1
) ? HPTE_R_KEY_BIT1
: 0x0UL
) |
58 ((pteflags
& H_PTE_PKEY_BIT2
) ? HPTE_R_KEY_BIT2
: 0x0UL
) |
59 ((pteflags
& H_PTE_PKEY_BIT3
) ? HPTE_R_KEY_BIT3
: 0x0UL
) |
60 ((pteflags
& H_PTE_PKEY_BIT4
) ? HPTE_R_KEY_BIT4
: 0x0UL
));
63 static inline u16
pte_to_pkey_bits(u64 pteflags
)
65 return (((pteflags
& H_PTE_PKEY_BIT0
) ? 0x10 : 0x0UL
) |
66 ((pteflags
& H_PTE_PKEY_BIT1
) ? 0x8 : 0x0UL
) |
67 ((pteflags
& H_PTE_PKEY_BIT2
) ? 0x4 : 0x0UL
) |
68 ((pteflags
& H_PTE_PKEY_BIT3
) ? 0x2 : 0x0UL
) |
69 ((pteflags
& H_PTE_PKEY_BIT4
) ? 0x1 : 0x0UL
));
72 #define pkey_alloc_mask(pkey) (0x1 << pkey)
74 #define mm_pkey_allocation_map(mm) (mm->context.pkey_allocation_map)
76 #define __mm_pkey_allocated(mm, pkey) { \
77 mm_pkey_allocation_map(mm) |= pkey_alloc_mask(pkey); \
80 #define __mm_pkey_free(mm, pkey) { \
81 mm_pkey_allocation_map(mm) &= ~pkey_alloc_mask(pkey); \
84 #define __mm_pkey_is_allocated(mm, pkey) \
85 (mm_pkey_allocation_map(mm) & pkey_alloc_mask(pkey))
87 #define __mm_pkey_is_reserved(pkey) (reserved_allocation_mask & \
88 pkey_alloc_mask(pkey))
90 static inline bool mm_pkey_is_allocated(struct mm_struct
*mm
, int pkey
)
92 if (pkey
< 0 || pkey
>= arch_max_pkey())
95 /* Reserved keys are never allocated. */
96 if (__mm_pkey_is_reserved(pkey
))
99 return __mm_pkey_is_allocated(mm
, pkey
);
103 * Returns a positive, 5-bit key on success, or -1 on failure.
104 * Relies on the mmap_sem to protect against concurrency in mm_pkey_alloc() and
107 static inline int mm_pkey_alloc(struct mm_struct
*mm
)
110 * Note: this is the one and only place we make sure that the pkey is
111 * valid as far as the hardware is concerned. The rest of the kernel
112 * trusts that only good, valid pkeys come out of here.
114 u32 all_pkeys_mask
= (u32
)(~(0x0));
117 if (static_branch_likely(&pkey_disabled
))
121 * Are we out of pkeys? We must handle this specially because ffz()
122 * behavior is undefined if there are no zeros.
124 if (mm_pkey_allocation_map(mm
) == all_pkeys_mask
)
127 ret
= ffz((u32
)mm_pkey_allocation_map(mm
));
128 __mm_pkey_allocated(mm
, ret
);
133 static inline int mm_pkey_free(struct mm_struct
*mm
, int pkey
)
135 if (static_branch_likely(&pkey_disabled
))
138 if (!mm_pkey_is_allocated(mm
, pkey
))
141 __mm_pkey_free(mm
, pkey
);
147 * Try to dedicate one of the protection keys to be used as an
148 * execute-only protection key.
150 extern int __execute_only_pkey(struct mm_struct
*mm
);
151 static inline int execute_only_pkey(struct mm_struct
*mm
)
153 if (static_branch_likely(&pkey_disabled
))
156 return __execute_only_pkey(mm
);
159 extern int __arch_override_mprotect_pkey(struct vm_area_struct
*vma
,
161 static inline int arch_override_mprotect_pkey(struct vm_area_struct
*vma
,
164 if (static_branch_likely(&pkey_disabled
))
168 * Is this an mprotect_pkey() call? If so, never override the value that
169 * came from the user.
174 return __arch_override_mprotect_pkey(vma
, prot
, pkey
);
177 extern int __arch_set_user_pkey_access(struct task_struct
*tsk
, int pkey
,
178 unsigned long init_val
);
179 static inline int arch_set_user_pkey_access(struct task_struct
*tsk
, int pkey
,
180 unsigned long init_val
)
182 if (static_branch_likely(&pkey_disabled
))
186 * userspace should not change pkey-0 permissions.
187 * pkey-0 is associated with every page in the kernel.
188 * If userspace denies any permission on pkey-0, the
189 * kernel cannot operate.
192 return init_val
? -EINVAL
: 0;
194 return __arch_set_user_pkey_access(tsk
, pkey
, init_val
);
197 static inline bool arch_pkeys_enabled(void)
199 return !static_branch_likely(&pkey_disabled
);
202 extern void pkey_mm_init(struct mm_struct
*mm
);
203 extern bool arch_supports_pkeys(int cap
);
204 extern unsigned int arch_usable_pkeys(void);
205 extern void thread_pkey_regs_save(struct thread_struct
*thread
);
206 extern void thread_pkey_regs_restore(struct thread_struct
*new_thread
,
207 struct thread_struct
*old_thread
);
208 extern void thread_pkey_regs_init(struct thread_struct
*thread
);
209 #endif /*_ASM_POWERPC_KEYS_H */