]>
git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - arch/x86/include/asm/pkeys.h
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _ASM_X86_PKEYS_H
3 #define _ASM_X86_PKEYS_H
5 #define ARCH_DEFAULT_PKEY 0
7 #define arch_max_pkey() (boot_cpu_has(X86_FEATURE_OSPKE) ? 16 : 1)
9 extern int arch_set_user_pkey_access(struct task_struct
*tsk
, int pkey
,
10 unsigned long init_val
);
13 * Try to dedicate one of the protection keys to be used as an
14 * execute-only protection key.
16 extern int __execute_only_pkey(struct mm_struct
*mm
);
17 static inline int execute_only_pkey(struct mm_struct
*mm
)
19 if (!boot_cpu_has(X86_FEATURE_OSPKE
))
20 return ARCH_DEFAULT_PKEY
;
22 return __execute_only_pkey(mm
);
25 extern int __arch_override_mprotect_pkey(struct vm_area_struct
*vma
,
27 static inline int arch_override_mprotect_pkey(struct vm_area_struct
*vma
,
30 if (!boot_cpu_has(X86_FEATURE_OSPKE
))
33 return __arch_override_mprotect_pkey(vma
, prot
, pkey
);
36 extern int __arch_set_user_pkey_access(struct task_struct
*tsk
, int pkey
,
37 unsigned long init_val
);
39 #define ARCH_VM_PKEY_FLAGS (VM_PKEY_BIT0 | VM_PKEY_BIT1 | VM_PKEY_BIT2 | VM_PKEY_BIT3)
41 #define mm_pkey_allocation_map(mm) (mm->context.pkey_allocation_map)
42 #define mm_set_pkey_allocated(mm, pkey) do { \
43 mm_pkey_allocation_map(mm) |= (1U << pkey); \
45 #define mm_set_pkey_free(mm, pkey) do { \
46 mm_pkey_allocation_map(mm) &= ~(1U << pkey); \
50 bool mm_pkey_is_allocated(struct mm_struct
*mm
, int pkey
)
53 * "Allocated" pkeys are those that have been returned
54 * from pkey_alloc() or pkey 0 which is allocated
55 * implicitly when the mm is created.
59 if (pkey
>= arch_max_pkey())
62 * The exec-only pkey is set in the allocation map, but
63 * is not available to any of the user interfaces like
66 if (pkey
== mm
->context
.execute_only_pkey
)
69 return mm_pkey_allocation_map(mm
) & (1U << pkey
);
73 * Returns a positive, 4-bit key on success, or -1 on failure.
76 int mm_pkey_alloc(struct mm_struct
*mm
)
79 * Note: this is the one and only place we make sure
80 * that the pkey is valid as far as the hardware is
81 * concerned. The rest of the kernel trusts that
82 * only good, valid pkeys come out of here.
84 u16 all_pkeys_mask
= ((1U << arch_max_pkey()) - 1);
88 * Are we out of pkeys? We must handle this specially
89 * because ffz() behavior is undefined if there are no
92 if (mm_pkey_allocation_map(mm
) == all_pkeys_mask
)
95 ret
= ffz(mm_pkey_allocation_map(mm
));
97 mm_set_pkey_allocated(mm
, ret
);
103 int mm_pkey_free(struct mm_struct
*mm
, int pkey
)
105 if (!mm_pkey_is_allocated(mm
, pkey
))
108 mm_set_pkey_free(mm
, pkey
);
113 extern int arch_set_user_pkey_access(struct task_struct
*tsk
, int pkey
,
114 unsigned long init_val
);
115 extern int __arch_set_user_pkey_access(struct task_struct
*tsk
, int pkey
,
116 unsigned long init_val
);
117 extern void copy_init_pkru_to_fpregs(void);
119 #endif /*_ASM_X86_PKEYS_H */