xref: /openbmc/linux/arch/x86/include/asm/pkeys.h (revision 160b8e75)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _ASM_X86_PKEYS_H
3 #define _ASM_X86_PKEYS_H
4 
5 #define arch_max_pkey() (boot_cpu_has(X86_FEATURE_OSPKE) ? 16 : 1)
6 
7 extern int arch_set_user_pkey_access(struct task_struct *tsk, int pkey,
8 		unsigned long init_val);
9 
10 /*
11  * Try to dedicate one of the protection keys to be used as an
12  * execute-only protection key.
13  */
14 extern int __execute_only_pkey(struct mm_struct *mm);
15 static inline int execute_only_pkey(struct mm_struct *mm)
16 {
17 	if (!boot_cpu_has(X86_FEATURE_OSPKE))
18 		return 0;
19 
20 	return __execute_only_pkey(mm);
21 }
22 
23 extern int __arch_override_mprotect_pkey(struct vm_area_struct *vma,
24 		int prot, int pkey);
25 static inline int arch_override_mprotect_pkey(struct vm_area_struct *vma,
26 		int prot, int pkey)
27 {
28 	if (!boot_cpu_has(X86_FEATURE_OSPKE))
29 		return 0;
30 
31 	return __arch_override_mprotect_pkey(vma, prot, pkey);
32 }
33 
34 extern int __arch_set_user_pkey_access(struct task_struct *tsk, int pkey,
35 		unsigned long init_val);
36 
37 #define ARCH_VM_PKEY_FLAGS (VM_PKEY_BIT0 | VM_PKEY_BIT1 | VM_PKEY_BIT2 | VM_PKEY_BIT3)
38 
39 #define mm_pkey_allocation_map(mm)	(mm->context.pkey_allocation_map)
40 #define mm_set_pkey_allocated(mm, pkey) do {		\
41 	mm_pkey_allocation_map(mm) |= (1U << pkey);	\
42 } while (0)
43 #define mm_set_pkey_free(mm, pkey) do {			\
44 	mm_pkey_allocation_map(mm) &= ~(1U << pkey);	\
45 } while (0)
46 
47 static inline
48 bool mm_pkey_is_allocated(struct mm_struct *mm, int pkey)
49 {
50 	/*
51 	 * "Allocated" pkeys are those that have been returned
52 	 * from pkey_alloc().  pkey 0 is special, and never
53 	 * returned from pkey_alloc().
54 	 */
55 	if (pkey <= 0)
56 		return false;
57 	if (pkey >= arch_max_pkey())
58 		return false;
59 	return mm_pkey_allocation_map(mm) & (1U << pkey);
60 }
61 
62 /*
63  * Returns a positive, 4-bit key on success, or -1 on failure.
64  */
65 static inline
66 int mm_pkey_alloc(struct mm_struct *mm)
67 {
68 	/*
69 	 * Note: this is the one and only place we make sure
70 	 * that the pkey is valid as far as the hardware is
71 	 * concerned.  The rest of the kernel trusts that
72 	 * only good, valid pkeys come out of here.
73 	 */
74 	u16 all_pkeys_mask = ((1U << arch_max_pkey()) - 1);
75 	int ret;
76 
77 	/*
78 	 * Are we out of pkeys?  We must handle this specially
79 	 * because ffz() behavior is undefined if there are no
80 	 * zeros.
81 	 */
82 	if (mm_pkey_allocation_map(mm) == all_pkeys_mask)
83 		return -1;
84 
85 	ret = ffz(mm_pkey_allocation_map(mm));
86 
87 	mm_set_pkey_allocated(mm, ret);
88 
89 	return ret;
90 }
91 
92 static inline
93 int mm_pkey_free(struct mm_struct *mm, int pkey)
94 {
95 	if (!mm_pkey_is_allocated(mm, pkey))
96 		return -EINVAL;
97 
98 	mm_set_pkey_free(mm, pkey);
99 
100 	return 0;
101 }
102 
103 extern int arch_set_user_pkey_access(struct task_struct *tsk, int pkey,
104 		unsigned long init_val);
105 extern int __arch_set_user_pkey_access(struct task_struct *tsk, int pkey,
106 		unsigned long init_val);
107 extern void copy_init_pkru_to_fpregs(void);
108 
109 #endif /*_ASM_X86_PKEYS_H */
110