xref: /linux/arch/powerpc/include/asm/pkeys.h (revision d0034a7a4ac7fae708146ac0059b9c47a1543f0d)
192e3da3cSRam Pai /* SPDX-License-Identifier: GPL-2.0+ */
292e3da3cSRam Pai /*
392e3da3cSRam Pai  * PowerPC Memory Protection Keys management
492e3da3cSRam Pai  *
592e3da3cSRam Pai  * Copyright 2017, Ram Pai, IBM Corporation.
692e3da3cSRam Pai  */
792e3da3cSRam Pai 
892e3da3cSRam Pai #ifndef _ASM_POWERPC_KEYS_H
992e3da3cSRam Pai #define _ASM_POWERPC_KEYS_H
1092e3da3cSRam Pai 
1192e3da3cSRam Pai #include <linux/jump_label.h>
12cf43d3b2SRam Pai #include <asm/firmware.h>
1392e3da3cSRam Pai 
14c529afd7SAneesh Kumar K.V extern int num_pkey;
1507f522d2SRam Pai extern u32 reserved_allocation_mask; /* bits set for reserved keys */
164fb158f6SRam Pai 
174fb158f6SRam Pai #define ARCH_VM_PKEY_FLAGS (VM_PKEY_BIT0 | VM_PKEY_BIT1 | VM_PKEY_BIT2 | \
184fb158f6SRam Pai 			    VM_PKEY_BIT3 | VM_PKEY_BIT4)
194fb158f6SRam Pai 
20013a91b3SRam Pai /* Override any generic PKEY permission defines */
21013a91b3SRam Pai #define PKEY_DISABLE_EXECUTE   0x4
22013a91b3SRam Pai #define PKEY_ACCESS_MASK       (PKEY_DISABLE_ACCESS | \
23013a91b3SRam Pai 				PKEY_DISABLE_WRITE  | \
24013a91b3SRam Pai 				PKEY_DISABLE_EXECUTE)
25013a91b3SRam Pai 
26b9658f83SAneesh Kumar K.V #ifdef CONFIG_PPC_BOOK3S_64
27b9658f83SAneesh Kumar K.V #include <asm/book3s/64/pkeys.h>
28b9658f83SAneesh Kumar K.V #else
29b9658f83SAneesh Kumar K.V #error "Not supported"
30b9658f83SAneesh Kumar K.V #endif
31b9658f83SAneesh Kumar K.V 
32b9658f83SAneesh Kumar K.V 
pkey_to_vmflag_bits(u16 pkey)33013a91b3SRam Pai static inline u64 pkey_to_vmflag_bits(u16 pkey)
34013a91b3SRam Pai {
35013a91b3SRam Pai 	return (((u64)pkey << VM_PKEY_SHIFT) & ARCH_VM_PKEY_FLAGS);
36013a91b3SRam Pai }
37013a91b3SRam Pai 
vma_pkey(struct vm_area_struct * vma)3887bbabbeSRam Pai static inline int vma_pkey(struct vm_area_struct *vma)
3987bbabbeSRam Pai {
40*f7045a45SAneesh Kumar K.V 	if (!mmu_has_feature(MMU_FTR_PKEY))
4187bbabbeSRam Pai 		return 0;
4287bbabbeSRam Pai 	return (vma->vm_flags & ARCH_VM_PKEY_FLAGS) >> VM_PKEY_SHIFT;
4387bbabbeSRam Pai }
4487bbabbeSRam Pai 
arch_max_pkey(void)45c529afd7SAneesh Kumar K.V static inline int arch_max_pkey(void)
46c529afd7SAneesh Kumar K.V {
47c529afd7SAneesh Kumar K.V 	return num_pkey;
48c529afd7SAneesh Kumar K.V }
494fb158f6SRam Pai 
504fb158f6SRam Pai #define pkey_alloc_mask(pkey) (0x1 << pkey)
514fb158f6SRam Pai 
524fb158f6SRam Pai #define mm_pkey_allocation_map(mm) (mm->context.pkey_allocation_map)
534fb158f6SRam Pai 
544fb158f6SRam Pai #define __mm_pkey_allocated(mm, pkey) {	\
554fb158f6SRam Pai 	mm_pkey_allocation_map(mm) |= pkey_alloc_mask(pkey); \
564fb158f6SRam Pai }
574fb158f6SRam Pai 
584fb158f6SRam Pai #define __mm_pkey_free(mm, pkey) {	\
594fb158f6SRam Pai 	mm_pkey_allocation_map(mm) &= ~pkey_alloc_mask(pkey);	\
604fb158f6SRam Pai }
614fb158f6SRam Pai 
624fb158f6SRam Pai #define __mm_pkey_is_allocated(mm, pkey)	\
634fb158f6SRam Pai 	(mm_pkey_allocation_map(mm) & pkey_alloc_mask(pkey))
644fb158f6SRam Pai 
6507f522d2SRam Pai #define __mm_pkey_is_reserved(pkey) (reserved_allocation_mask & \
664fb158f6SRam Pai 				       pkey_alloc_mask(pkey))
6792e3da3cSRam Pai 
mm_pkey_is_allocated(struct mm_struct * mm,int pkey)6892e3da3cSRam Pai static inline bool mm_pkey_is_allocated(struct mm_struct *mm, int pkey)
6992e3da3cSRam Pai {
7007f522d2SRam Pai 	if (pkey < 0 || pkey >= arch_max_pkey())
7107f522d2SRam Pai 		return false;
7207f522d2SRam Pai 
7307f522d2SRam Pai 	/* Reserved keys are never allocated. */
7407f522d2SRam Pai 	if (__mm_pkey_is_reserved(pkey))
7507f522d2SRam Pai 		return false;
7607f522d2SRam Pai 
7707f522d2SRam Pai 	return __mm_pkey_is_allocated(mm, pkey);
7892e3da3cSRam Pai }
7992e3da3cSRam Pai 
804fb158f6SRam Pai /*
814fb158f6SRam Pai  * Returns a positive, 5-bit key on success, or -1 on failure.
82c1e8d7c6SMichel Lespinasse  * Relies on the mmap_lock to protect against concurrency in mm_pkey_alloc() and
834fb158f6SRam Pai  * mm_pkey_free().
844fb158f6SRam Pai  */
mm_pkey_alloc(struct mm_struct * mm)8592e3da3cSRam Pai static inline int mm_pkey_alloc(struct mm_struct *mm)
8692e3da3cSRam Pai {
874fb158f6SRam Pai 	/*
884fb158f6SRam Pai 	 * Note: this is the one and only place we make sure that the pkey is
894fb158f6SRam Pai 	 * valid as far as the hardware is concerned. The rest of the kernel
904fb158f6SRam Pai 	 * trusts that only good, valid pkeys come out of here.
914fb158f6SRam Pai 	 */
924fb158f6SRam Pai 	u32 all_pkeys_mask = (u32)(~(0x0));
934fb158f6SRam Pai 	int ret;
944fb158f6SRam Pai 
95*f7045a45SAneesh Kumar K.V 	if (!mmu_has_feature(MMU_FTR_PKEY))
9692e3da3cSRam Pai 		return -1;
974fb158f6SRam Pai 	/*
984fb158f6SRam Pai 	 * Are we out of pkeys? We must handle this specially because ffz()
994fb158f6SRam Pai 	 * behavior is undefined if there are no zeros.
1004fb158f6SRam Pai 	 */
1014fb158f6SRam Pai 	if (mm_pkey_allocation_map(mm) == all_pkeys_mask)
1024fb158f6SRam Pai 		return -1;
1034fb158f6SRam Pai 
1044fb158f6SRam Pai 	ret = ffz((u32)mm_pkey_allocation_map(mm));
1054fb158f6SRam Pai 	__mm_pkey_allocated(mm, ret);
1060685f217SRam Pai 
1074fb158f6SRam Pai 	return ret;
10892e3da3cSRam Pai }
10992e3da3cSRam Pai 
mm_pkey_free(struct mm_struct * mm,int pkey)11092e3da3cSRam Pai static inline int mm_pkey_free(struct mm_struct *mm, int pkey)
11192e3da3cSRam Pai {
112*f7045a45SAneesh Kumar K.V 	if (!mmu_has_feature(MMU_FTR_PKEY))
1134fb158f6SRam Pai 		return -1;
1144fb158f6SRam Pai 
1154fb158f6SRam Pai 	if (!mm_pkey_is_allocated(mm, pkey))
11692e3da3cSRam Pai 		return -EINVAL;
1174fb158f6SRam Pai 
1184fb158f6SRam Pai 	__mm_pkey_free(mm, pkey);
1194fb158f6SRam Pai 
1204fb158f6SRam Pai 	return 0;
12192e3da3cSRam Pai }
12292e3da3cSRam Pai 
12392e3da3cSRam Pai /*
12492e3da3cSRam Pai  * Try to dedicate one of the protection keys to be used as an
12592e3da3cSRam Pai  * execute-only protection key.
12692e3da3cSRam Pai  */
1272daf298dSAneesh Kumar K.V extern int execute_only_pkey(struct mm_struct *mm);
12887bbabbeSRam Pai extern int __arch_override_mprotect_pkey(struct vm_area_struct *vma,
12987bbabbeSRam Pai 					 int prot, int pkey);
arch_override_mprotect_pkey(struct vm_area_struct * vma,int prot,int pkey)13092e3da3cSRam Pai static inline int arch_override_mprotect_pkey(struct vm_area_struct *vma,
13192e3da3cSRam Pai 					      int prot, int pkey)
13292e3da3cSRam Pai {
133*f7045a45SAneesh Kumar K.V 	if (!mmu_has_feature(MMU_FTR_PKEY))
13492e3da3cSRam Pai 		return 0;
13587bbabbeSRam Pai 
13687bbabbeSRam Pai 	/*
13787bbabbeSRam Pai 	 * Is this an mprotect_pkey() call? If so, never override the value that
13887bbabbeSRam Pai 	 * came from the user.
13987bbabbeSRam Pai 	 */
14087bbabbeSRam Pai 	if (pkey != -1)
14187bbabbeSRam Pai 		return pkey;
14287bbabbeSRam Pai 
14387bbabbeSRam Pai 	return __arch_override_mprotect_pkey(vma, prot, pkey);
14492e3da3cSRam Pai }
14592e3da3cSRam Pai 
1462ddc53f3SRam Pai extern int __arch_set_user_pkey_access(struct task_struct *tsk, int pkey,
1472ddc53f3SRam Pai 				       unsigned long init_val);
arch_set_user_pkey_access(struct task_struct * tsk,int pkey,unsigned long init_val)14892e3da3cSRam Pai static inline int arch_set_user_pkey_access(struct task_struct *tsk, int pkey,
14992e3da3cSRam Pai 					    unsigned long init_val)
15092e3da3cSRam Pai {
151*f7045a45SAneesh Kumar K.V 	if (!mmu_has_feature(MMU_FTR_PKEY))
1522ddc53f3SRam Pai 		return -EINVAL;
15307f522d2SRam Pai 
15407f522d2SRam Pai 	/*
15507f522d2SRam Pai 	 * userspace should not change pkey-0 permissions.
15607f522d2SRam Pai 	 * pkey-0 is associated with every page in the kernel.
15707f522d2SRam Pai 	 * If userspace denies any permission on pkey-0, the
15807f522d2SRam Pai 	 * kernel cannot operate.
15907f522d2SRam Pai 	 */
16007f522d2SRam Pai 	if (pkey == 0)
16107f522d2SRam Pai 		return init_val ? -EINVAL : 0;
16207f522d2SRam Pai 
1632ddc53f3SRam Pai 	return __arch_set_user_pkey_access(tsk, pkey, init_val);
16492e3da3cSRam Pai }
1654fb158f6SRam Pai 
arch_pkeys_enabled(void)166c5cc1f4dSThiago Jung Bauermann static inline bool arch_pkeys_enabled(void)
167c5cc1f4dSThiago Jung Bauermann {
168*f7045a45SAneesh Kumar K.V 	return mmu_has_feature(MMU_FTR_PKEY);
169c5cc1f4dSThiago Jung Bauermann }
170c5cc1f4dSThiago Jung Bauermann 
1714fb158f6SRam Pai extern void pkey_mm_init(struct mm_struct *mm);
17292e3da3cSRam Pai #endif /*_ASM_POWERPC_KEYS_H */
173