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