blob: 34684adb6899ad132e44e159be621d92370b99c0 [file] [log] [blame]
Dave Hansen66d37572016-02-12 13:02:32 -08001#ifndef _ASM_X86_PKEYS_H
2#define _ASM_X86_PKEYS_H
3
Dave Hansene8c24d32016-07-29 09:30:15 -07004#define arch_max_pkey() (boot_cpu_has(X86_FEATURE_OSPKE) ? 16 : 1)
Dave Hansen66d37572016-02-12 13:02:32 -08005
Dave Hansen84594292016-02-12 13:02:36 -08006extern int arch_set_user_pkey_access(struct task_struct *tsk, int pkey,
7 unsigned long init_val);
8
Dave Hansen62b5f7d2016-02-12 13:02:40 -08009/*
10 * Try to dedicate one of the protection keys to be used as an
11 * execute-only protection key.
12 */
Dave Hansen62b5f7d2016-02-12 13:02:40 -080013extern int __execute_only_pkey(struct mm_struct *mm);
14static inline int execute_only_pkey(struct mm_struct *mm)
15{
16 if (!boot_cpu_has(X86_FEATURE_OSPKE))
17 return 0;
18
19 return __execute_only_pkey(mm);
20}
21
22extern int __arch_override_mprotect_pkey(struct vm_area_struct *vma,
23 int prot, int pkey);
24static inline int arch_override_mprotect_pkey(struct vm_area_struct *vma,
25 int prot, int pkey)
26{
27 if (!boot_cpu_has(X86_FEATURE_OSPKE))
28 return 0;
29
30 return __arch_override_mprotect_pkey(vma, prot, pkey);
31}
32
Dave Hansen7d06d9c2016-07-29 09:30:12 -070033extern int __arch_set_user_pkey_access(struct task_struct *tsk, int pkey,
34 unsigned long init_val);
35
Dave Hansena8502b62016-07-29 09:30:13 -070036#define ARCH_VM_PKEY_FLAGS (VM_PKEY_BIT0 | VM_PKEY_BIT1 | VM_PKEY_BIT2 | VM_PKEY_BIT3)
37
Dave Hansene8c24d32016-07-29 09:30:15 -070038#define mm_pkey_allocation_map(mm) (mm->context.pkey_allocation_map)
39#define mm_set_pkey_allocated(mm, pkey) do { \
40 mm_pkey_allocation_map(mm) |= (1U << pkey); \
41} while (0)
42#define mm_set_pkey_free(mm, pkey) do { \
43 mm_pkey_allocation_map(mm) &= ~(1U << pkey); \
44} while (0)
45
46static inline
47bool mm_pkey_is_allocated(struct mm_struct *mm, int pkey)
48{
49 return mm_pkey_allocation_map(mm) & (1U << pkey);
50}
51
52/*
53 * Returns a positive, 4-bit key on success, or -1 on failure.
54 */
55static inline
56int mm_pkey_alloc(struct mm_struct *mm)
57{
58 /*
59 * Note: this is the one and only place we make sure
60 * that the pkey is valid as far as the hardware is
61 * concerned. The rest of the kernel trusts that
62 * only good, valid pkeys come out of here.
63 */
64 u16 all_pkeys_mask = ((1U << arch_max_pkey()) - 1);
65 int ret;
66
67 /*
68 * Are we out of pkeys? We must handle this specially
69 * because ffz() behavior is undefined if there are no
70 * zeros.
71 */
72 if (mm_pkey_allocation_map(mm) == all_pkeys_mask)
73 return -1;
74
75 ret = ffz(mm_pkey_allocation_map(mm));
76
77 mm_set_pkey_allocated(mm, ret);
78
79 return ret;
80}
81
82static inline
83int mm_pkey_free(struct mm_struct *mm, int pkey)
84{
85 /*
86 * pkey 0 is special, always allocated and can never
87 * be freed.
88 */
89 if (!pkey)
90 return -EINVAL;
91 if (!mm_pkey_is_allocated(mm, pkey))
92 return -EINVAL;
93
94 mm_set_pkey_free(mm, pkey);
95
96 return 0;
97}
98
99extern int arch_set_user_pkey_access(struct task_struct *tsk, int pkey,
100 unsigned long init_val);
101extern int __arch_set_user_pkey_access(struct task_struct *tsk, int pkey,
102 unsigned long init_val);
Dave Hansenacd547b2016-07-29 09:30:21 -0700103extern void copy_init_pkru_to_fpregs(void);
Dave Hansene8c24d32016-07-29 09:30:15 -0700104
Dave Hansen66d37572016-02-12 13:02:32 -0800105#endif /*_ASM_X86_PKEYS_H */