1 #ifndef _ASM_X86_PKEYS_H
2 #define _ASM_X86_PKEYS_H
4 #define ARCH_DEFAULT_PKEY 0
7 * If more than 16 keys are ever supported, a thorough audit
8 * will be necessary to ensure that the types that store key
9 * numbers and masks have sufficient capacity.
11 #define arch_max_pkey() (boot_cpu_has(X86_FEATURE_OSPKE) ? 16 : 1)
13 extern int arch_set_user_pkey_access(struct task_struct *tsk, int pkey,
14 unsigned long init_val);
17 * Try to dedicate one of the protection keys to be used as an
18 * execute-only protection key.
20 extern int __execute_only_pkey(struct mm_struct *mm);
21 static inline int execute_only_pkey(struct mm_struct *mm)
23 if (!boot_cpu_has(X86_FEATURE_OSPKE))
24 return ARCH_DEFAULT_PKEY;
26 return __execute_only_pkey(mm);
29 extern int __arch_override_mprotect_pkey(struct vm_area_struct *vma,
31 static inline int arch_override_mprotect_pkey(struct vm_area_struct *vma,
34 if (!boot_cpu_has(X86_FEATURE_OSPKE))
37 return __arch_override_mprotect_pkey(vma, prot, pkey);
40 extern int __arch_set_user_pkey_access(struct task_struct *tsk, int pkey,
41 unsigned long init_val);
43 #define ARCH_VM_PKEY_FLAGS (VM_PKEY_BIT0 | VM_PKEY_BIT1 | VM_PKEY_BIT2 | VM_PKEY_BIT3)
45 #define mm_pkey_allocation_map(mm) (mm->context.pkey_allocation_map)
46 #define mm_set_pkey_allocated(mm, pkey) do { \
47 mm_pkey_allocation_map(mm) |= (1U << pkey); \
49 #define mm_set_pkey_free(mm, pkey) do { \
50 mm_pkey_allocation_map(mm) &= ~(1U << pkey); \
54 bool mm_pkey_is_allocated(struct mm_struct *mm, int pkey)
57 * "Allocated" pkeys are those that have been returned
58 * from pkey_alloc() or pkey 0 which is allocated
59 * implicitly when the mm is created.
63 if (pkey >= arch_max_pkey())
66 * The exec-only pkey is set in the allocation map, but
67 * is not available to any of the user interfaces like
70 if (pkey == mm->context.execute_only_pkey)
73 return mm_pkey_allocation_map(mm) & (1U << pkey);
77 * Returns a positive, 4-bit key on success, or -1 on failure.
80 int mm_pkey_alloc(struct mm_struct *mm)
83 * Note: this is the one and only place we make sure
84 * that the pkey is valid as far as the hardware is
85 * concerned. The rest of the kernel trusts that
86 * only good, valid pkeys come out of here.
88 u16 all_pkeys_mask = ((1U << arch_max_pkey()) - 1);
92 * Are we out of pkeys? We must handle this specially
93 * because ffz() behavior is undefined if there are no
96 if (mm_pkey_allocation_map(mm) == all_pkeys_mask)
99 ret = ffz(mm_pkey_allocation_map(mm));
101 mm_set_pkey_allocated(mm, ret);
107 int mm_pkey_free(struct mm_struct *mm, int pkey)
109 if (!mm_pkey_is_allocated(mm, pkey))
112 mm_set_pkey_free(mm, pkey);
117 extern int arch_set_user_pkey_access(struct task_struct *tsk, int pkey,
118 unsigned long init_val);
119 extern int __arch_set_user_pkey_access(struct task_struct *tsk, int pkey,
120 unsigned long init_val);
121 extern void copy_init_pkru_to_fpregs(void);
123 #endif /*_ASM_X86_PKEYS_H */