1 #ifndef _ASM_X86_PKEYS_H
2 #define _ASM_X86_PKEYS_H
4 #define arch_max_pkey() (boot_cpu_has(X86_FEATURE_OSPKE) ? 16 : 1)
6 extern int arch_set_user_pkey_access(struct task_struct *tsk, int pkey,
7 unsigned long init_val);
10 * Try to dedicate one of the protection keys to be used as an
11 * execute-only protection key.
13 extern int __execute_only_pkey(struct mm_struct *mm);
14 static inline int execute_only_pkey(struct mm_struct *mm)
16 if (!boot_cpu_has(X86_FEATURE_OSPKE))
19 return __execute_only_pkey(mm);
22 extern int __arch_override_mprotect_pkey(struct vm_area_struct *vma,
24 static inline int arch_override_mprotect_pkey(struct vm_area_struct *vma,
27 if (!boot_cpu_has(X86_FEATURE_OSPKE))
30 return __arch_override_mprotect_pkey(vma, prot, pkey);
33 extern int __arch_set_user_pkey_access(struct task_struct *tsk, int pkey,
34 unsigned long init_val);
36 #define ARCH_VM_PKEY_FLAGS (VM_PKEY_BIT0 | VM_PKEY_BIT1 | VM_PKEY_BIT2 | VM_PKEY_BIT3)
38 #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); \
42 #define mm_set_pkey_free(mm, pkey) do { \
43 mm_pkey_allocation_map(mm) &= ~(1U << pkey); \
47 bool mm_pkey_is_allocated(struct mm_struct *mm, int pkey)
50 * "Allocated" pkeys are those that have been returned
51 * from pkey_alloc(). pkey 0 is special, and never
52 * returned from pkey_alloc().
56 if (pkey >= arch_max_pkey())
58 return mm_pkey_allocation_map(mm) & (1U << pkey);
62 * Returns a positive, 4-bit key on success, or -1 on failure.
65 int mm_pkey_alloc(struct mm_struct *mm)
68 * Note: this is the one and only place we make sure
69 * that the pkey is valid as far as the hardware is
70 * concerned. The rest of the kernel trusts that
71 * only good, valid pkeys come out of here.
73 u16 all_pkeys_mask = ((1U << arch_max_pkey()) - 1);
77 * Are we out of pkeys? We must handle this specially
78 * because ffz() behavior is undefined if there are no
81 if (mm_pkey_allocation_map(mm) == all_pkeys_mask)
84 ret = ffz(mm_pkey_allocation_map(mm));
86 mm_set_pkey_allocated(mm, ret);
92 int mm_pkey_free(struct mm_struct *mm, int pkey)
94 if (!mm_pkey_is_allocated(mm, pkey))
97 mm_set_pkey_free(mm, pkey);
102 extern int arch_set_user_pkey_access(struct task_struct *tsk, int pkey,
103 unsigned long init_val);
104 extern int __arch_set_user_pkey_access(struct task_struct *tsk, int pkey,
105 unsigned long init_val);
106 extern void copy_init_pkru_to_fpregs(void);
108 #endif /*_ASM_X86_PKEYS_H */