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