blob: a0ba1ffda0dfd3dc9ee335f04cee58aafdea6fb8 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001/* SPDX-License-Identifier: GPL-2.0 */
Dave Hansen66d37572016-02-12 13:02:32 -08002#ifndef _ASM_X86_PKEYS_H
3#define _ASM_X86_PKEYS_H
4
Dave Hansene8c24d32016-07-29 09:30:15 -07005#define arch_max_pkey() (boot_cpu_has(X86_FEATURE_OSPKE) ? 16 : 1)
Dave Hansen66d37572016-02-12 13:02:32 -08006
Dave Hansen84594292016-02-12 13:02:36 -08007extern int arch_set_user_pkey_access(struct task_struct *tsk, int pkey,
8 unsigned long init_val);
9
Dave Hansen62b5f7d2016-02-12 13:02:40 -080010/*
11 * Try to dedicate one of the protection keys to be used as an
12 * execute-only protection key.
13 */
Dave Hansen62b5f7d2016-02-12 13:02:40 -080014extern int __execute_only_pkey(struct mm_struct *mm);
15static 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
23extern int __arch_override_mprotect_pkey(struct vm_area_struct *vma,
24 int prot, int pkey);
25static 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
Dave Hansen7d06d9c2016-07-29 09:30:12 -070034extern int __arch_set_user_pkey_access(struct task_struct *tsk, int pkey,
35 unsigned long init_val);
36
Dave Hansena8502b62016-07-29 09:30:13 -070037#define ARCH_VM_PKEY_FLAGS (VM_PKEY_BIT0 | VM_PKEY_BIT1 | VM_PKEY_BIT2 | VM_PKEY_BIT3)
38
Dave Hansene8c24d32016-07-29 09:30:15 -070039#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
47static inline
48bool mm_pkey_is_allocated(struct mm_struct *mm, int pkey)
49{
Dave Hansen58ab9a02017-02-23 14:26:03 -080050 /*
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;
Dave Hansene8c24d32016-07-29 09:30:15 -070059 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 */
65static inline
66int 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
92static inline
93int mm_pkey_free(struct mm_struct *mm, int pkey)
94{
Dave Hansene8c24d32016-07-29 09:30:15 -070095 if (!mm_pkey_is_allocated(mm, pkey))
96 return -EINVAL;
97
98 mm_set_pkey_free(mm, pkey);
99
100 return 0;
101}
102
103extern int arch_set_user_pkey_access(struct task_struct *tsk, int pkey,
104 unsigned long init_val);
105extern int __arch_set_user_pkey_access(struct task_struct *tsk, int pkey,
106 unsigned long init_val);
Dave Hansenacd547b2016-07-29 09:30:21 -0700107extern void copy_init_pkru_to_fpregs(void);
Dave Hansene8c24d32016-07-29 09:30:15 -0700108
Dave Hansen66d37572016-02-12 13:02:32 -0800109#endif /*_ASM_X86_PKEYS_H */