xref: /linux/arch/x86/include/asm/pkeys.h (revision f4cdf7ca9a1fdcca413157df19753f388a5a224e)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _ASM_X86_PKEYS_H
3 #define _ASM_X86_PKEYS_H
4 
5 /*
6  * If more than 16 keys are ever supported, a thorough audit
7  * will be necessary to ensure that the types that store key
8  * numbers and masks have sufficient capacity.
9  */
10 #define arch_max_pkey() (cpu_feature_enabled(X86_FEATURE_OSPKE) ? 16 : 1)
11 
12 extern int arch_set_user_pkey_access(int pkey, unsigned long init_val);
13 
14 static inline bool arch_pkeys_enabled(void)
15 {
16 	return cpu_feature_enabled(X86_FEATURE_OSPKE);
17 }
18 
19 /*
20  * Try to dedicate one of the protection keys to be used as an
21  * execute-only protection key.
22  */
23 extern int __execute_only_pkey(struct mm_struct *mm);
24 static inline int execute_only_pkey(struct mm_struct *mm)
25 {
26 	if (!cpu_feature_enabled(X86_FEATURE_OSPKE))
27 		return ARCH_DEFAULT_PKEY;
28 
29 	return __execute_only_pkey(mm);
30 }
31 
32 extern int __arch_override_mprotect_pkey(struct vm_area_struct *vma,
33 		int prot, int pkey);
34 static inline int arch_override_mprotect_pkey(struct vm_area_struct *vma,
35 		int prot, int pkey)
36 {
37 	if (!cpu_feature_enabled(X86_FEATURE_OSPKE))
38 		return 0;
39 
40 	return __arch_override_mprotect_pkey(vma, prot, pkey);
41 }
42 
43 #define ARCH_VM_PKEY_FLAGS (VM_PKEY_BIT0 | VM_PKEY_BIT1 | VM_PKEY_BIT2 | VM_PKEY_BIT3)
44 
45 #define mm_pkey_allocation_map(mm)	(mm->context.pkey_allocation_map)
46 #define mm_set_pkey_allocated(mm, pkey) do {		\
47 	mm_pkey_allocation_map(mm) |= (1U << pkey);	\
48 } while (0)
49 #define mm_set_pkey_free(mm, pkey) do {			\
50 	mm_pkey_allocation_map(mm) &= ~(1U << pkey);	\
51 } while (0)
52 
53 static inline
54 bool mm_pkey_is_allocated(struct mm_struct *mm, int pkey)
55 {
56 	/*
57 	 * "Allocated" pkeys are those that have been returned
58 	 * from pkey_alloc() or pkey 0 which is allocated
59 	 * implicitly when the mm is created.
60 	 */
61 	if (pkey < 0)
62 		return false;
63 	if (pkey >= arch_max_pkey())
64 		return false;
65 	/*
66 	 * The exec-only pkey is set in the allocation map, but
67 	 * is not available to any of the user interfaces like
68 	 * mprotect_pkey().
69 	 */
70 	if (pkey == mm->context.execute_only_pkey)
71 		return false;
72 
73 	return mm_pkey_allocation_map(mm) & (1U << pkey);
74 }
75 
76 /*
77  * Returns a positive, 4-bit key on success, or -1 on failure.
78  */
79 static inline
80 int mm_pkey_alloc(struct mm_struct *mm)
81 {
82 	/*
83 	 * Note: this is the one and only place we make sure
84 	 * that the pkey is valid as far as the hardware is
85 	 * concerned.  The rest of the kernel trusts that
86 	 * only good, valid pkeys come out of here.
87 	 */
88 	u16 all_pkeys_mask = ((1U << arch_max_pkey()) - 1);
89 	int ret;
90 
91 	if (!arch_pkeys_enabled())
92 		return -1;
93 
94 	/*
95 	 * Are we out of pkeys?  We must handle this specially
96 	 * because ffz() behavior is undefined if there are no
97 	 * zeros.
98 	 */
99 	if (mm_pkey_allocation_map(mm) == all_pkeys_mask)
100 		return -1;
101 
102 	ret = ffz(mm_pkey_allocation_map(mm));
103 
104 	mm_set_pkey_allocated(mm, ret);
105 
106 	return ret;
107 }
108 
109 static inline
110 int mm_pkey_free(struct mm_struct *mm, int pkey)
111 {
112 	if (!mm_pkey_is_allocated(mm, pkey))
113 		return -EINVAL;
114 
115 	mm_set_pkey_free(mm, pkey);
116 
117 	return 0;
118 }
119 
120 static inline int vma_pkey(struct vm_area_struct *vma)
121 {
122 	unsigned long vma_pkey_mask = VM_PKEY_BIT0 | VM_PKEY_BIT1 |
123 				      VM_PKEY_BIT2 | VM_PKEY_BIT3;
124 
125 	return (vma->vm_flags & vma_pkey_mask) >> VM_PKEY_SHIFT;
126 }
127 
128 #endif /*_ASM_X86_PKEYS_H */
129