18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */
28c2ecf20Sopenharmony_ci#ifndef _ASM_X86_PKEYS_H
38c2ecf20Sopenharmony_ci#define _ASM_X86_PKEYS_H
48c2ecf20Sopenharmony_ci
58c2ecf20Sopenharmony_ci#define ARCH_DEFAULT_PKEY	0
68c2ecf20Sopenharmony_ci
78c2ecf20Sopenharmony_ci/*
88c2ecf20Sopenharmony_ci * If more than 16 keys are ever supported, a thorough audit
98c2ecf20Sopenharmony_ci * will be necessary to ensure that the types that store key
108c2ecf20Sopenharmony_ci * numbers and masks have sufficient capacity.
118c2ecf20Sopenharmony_ci */
128c2ecf20Sopenharmony_ci#define arch_max_pkey() (boot_cpu_has(X86_FEATURE_OSPKE) ? 16 : 1)
138c2ecf20Sopenharmony_ci
148c2ecf20Sopenharmony_ciextern int arch_set_user_pkey_access(struct task_struct *tsk, int pkey,
158c2ecf20Sopenharmony_ci		unsigned long init_val);
168c2ecf20Sopenharmony_ci
178c2ecf20Sopenharmony_cistatic inline bool arch_pkeys_enabled(void)
188c2ecf20Sopenharmony_ci{
198c2ecf20Sopenharmony_ci	return boot_cpu_has(X86_FEATURE_OSPKE);
208c2ecf20Sopenharmony_ci}
218c2ecf20Sopenharmony_ci
228c2ecf20Sopenharmony_ci/*
238c2ecf20Sopenharmony_ci * Try to dedicate one of the protection keys to be used as an
248c2ecf20Sopenharmony_ci * execute-only protection key.
258c2ecf20Sopenharmony_ci */
268c2ecf20Sopenharmony_ciextern int __execute_only_pkey(struct mm_struct *mm);
278c2ecf20Sopenharmony_cistatic inline int execute_only_pkey(struct mm_struct *mm)
288c2ecf20Sopenharmony_ci{
298c2ecf20Sopenharmony_ci	if (!boot_cpu_has(X86_FEATURE_OSPKE))
308c2ecf20Sopenharmony_ci		return ARCH_DEFAULT_PKEY;
318c2ecf20Sopenharmony_ci
328c2ecf20Sopenharmony_ci	return __execute_only_pkey(mm);
338c2ecf20Sopenharmony_ci}
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_ciextern int __arch_override_mprotect_pkey(struct vm_area_struct *vma,
368c2ecf20Sopenharmony_ci		int prot, int pkey);
378c2ecf20Sopenharmony_cistatic inline int arch_override_mprotect_pkey(struct vm_area_struct *vma,
388c2ecf20Sopenharmony_ci		int prot, int pkey)
398c2ecf20Sopenharmony_ci{
408c2ecf20Sopenharmony_ci	if (!boot_cpu_has(X86_FEATURE_OSPKE))
418c2ecf20Sopenharmony_ci		return 0;
428c2ecf20Sopenharmony_ci
438c2ecf20Sopenharmony_ci	return __arch_override_mprotect_pkey(vma, prot, pkey);
448c2ecf20Sopenharmony_ci}
458c2ecf20Sopenharmony_ci
468c2ecf20Sopenharmony_ciextern int __arch_set_user_pkey_access(struct task_struct *tsk, int pkey,
478c2ecf20Sopenharmony_ci		unsigned long init_val);
488c2ecf20Sopenharmony_ci
498c2ecf20Sopenharmony_ci#define ARCH_VM_PKEY_FLAGS (VM_PKEY_BIT0 | VM_PKEY_BIT1 | VM_PKEY_BIT2 | VM_PKEY_BIT3)
508c2ecf20Sopenharmony_ci
518c2ecf20Sopenharmony_ci#define mm_pkey_allocation_map(mm)	(mm->context.pkey_allocation_map)
528c2ecf20Sopenharmony_ci#define mm_set_pkey_allocated(mm, pkey) do {		\
538c2ecf20Sopenharmony_ci	mm_pkey_allocation_map(mm) |= (1U << pkey);	\
548c2ecf20Sopenharmony_ci} while (0)
558c2ecf20Sopenharmony_ci#define mm_set_pkey_free(mm, pkey) do {			\
568c2ecf20Sopenharmony_ci	mm_pkey_allocation_map(mm) &= ~(1U << pkey);	\
578c2ecf20Sopenharmony_ci} while (0)
588c2ecf20Sopenharmony_ci
598c2ecf20Sopenharmony_cistatic inline
608c2ecf20Sopenharmony_cibool mm_pkey_is_allocated(struct mm_struct *mm, int pkey)
618c2ecf20Sopenharmony_ci{
628c2ecf20Sopenharmony_ci	/*
638c2ecf20Sopenharmony_ci	 * "Allocated" pkeys are those that have been returned
648c2ecf20Sopenharmony_ci	 * from pkey_alloc() or pkey 0 which is allocated
658c2ecf20Sopenharmony_ci	 * implicitly when the mm is created.
668c2ecf20Sopenharmony_ci	 */
678c2ecf20Sopenharmony_ci	if (pkey < 0)
688c2ecf20Sopenharmony_ci		return false;
698c2ecf20Sopenharmony_ci	if (pkey >= arch_max_pkey())
708c2ecf20Sopenharmony_ci		return false;
718c2ecf20Sopenharmony_ci	/*
728c2ecf20Sopenharmony_ci	 * The exec-only pkey is set in the allocation map, but
738c2ecf20Sopenharmony_ci	 * is not available to any of the user interfaces like
748c2ecf20Sopenharmony_ci	 * mprotect_pkey().
758c2ecf20Sopenharmony_ci	 */
768c2ecf20Sopenharmony_ci	if (pkey == mm->context.execute_only_pkey)
778c2ecf20Sopenharmony_ci		return false;
788c2ecf20Sopenharmony_ci
798c2ecf20Sopenharmony_ci	return mm_pkey_allocation_map(mm) & (1U << pkey);
808c2ecf20Sopenharmony_ci}
818c2ecf20Sopenharmony_ci
828c2ecf20Sopenharmony_ci/*
838c2ecf20Sopenharmony_ci * Returns a positive, 4-bit key on success, or -1 on failure.
848c2ecf20Sopenharmony_ci */
858c2ecf20Sopenharmony_cistatic inline
868c2ecf20Sopenharmony_ciint mm_pkey_alloc(struct mm_struct *mm)
878c2ecf20Sopenharmony_ci{
888c2ecf20Sopenharmony_ci	/*
898c2ecf20Sopenharmony_ci	 * Note: this is the one and only place we make sure
908c2ecf20Sopenharmony_ci	 * that the pkey is valid as far as the hardware is
918c2ecf20Sopenharmony_ci	 * concerned.  The rest of the kernel trusts that
928c2ecf20Sopenharmony_ci	 * only good, valid pkeys come out of here.
938c2ecf20Sopenharmony_ci	 */
948c2ecf20Sopenharmony_ci	u16 all_pkeys_mask = ((1U << arch_max_pkey()) - 1);
958c2ecf20Sopenharmony_ci	int ret;
968c2ecf20Sopenharmony_ci
978c2ecf20Sopenharmony_ci	/*
988c2ecf20Sopenharmony_ci	 * Are we out of pkeys?  We must handle this specially
998c2ecf20Sopenharmony_ci	 * because ffz() behavior is undefined if there are no
1008c2ecf20Sopenharmony_ci	 * zeros.
1018c2ecf20Sopenharmony_ci	 */
1028c2ecf20Sopenharmony_ci	if (mm_pkey_allocation_map(mm) == all_pkeys_mask)
1038c2ecf20Sopenharmony_ci		return -1;
1048c2ecf20Sopenharmony_ci
1058c2ecf20Sopenharmony_ci	ret = ffz(mm_pkey_allocation_map(mm));
1068c2ecf20Sopenharmony_ci
1078c2ecf20Sopenharmony_ci	mm_set_pkey_allocated(mm, ret);
1088c2ecf20Sopenharmony_ci
1098c2ecf20Sopenharmony_ci	return ret;
1108c2ecf20Sopenharmony_ci}
1118c2ecf20Sopenharmony_ci
1128c2ecf20Sopenharmony_cistatic inline
1138c2ecf20Sopenharmony_ciint mm_pkey_free(struct mm_struct *mm, int pkey)
1148c2ecf20Sopenharmony_ci{
1158c2ecf20Sopenharmony_ci	if (!mm_pkey_is_allocated(mm, pkey))
1168c2ecf20Sopenharmony_ci		return -EINVAL;
1178c2ecf20Sopenharmony_ci
1188c2ecf20Sopenharmony_ci	mm_set_pkey_free(mm, pkey);
1198c2ecf20Sopenharmony_ci
1208c2ecf20Sopenharmony_ci	return 0;
1218c2ecf20Sopenharmony_ci}
1228c2ecf20Sopenharmony_ci
1238c2ecf20Sopenharmony_ciextern int arch_set_user_pkey_access(struct task_struct *tsk, int pkey,
1248c2ecf20Sopenharmony_ci		unsigned long init_val);
1258c2ecf20Sopenharmony_ciextern int __arch_set_user_pkey_access(struct task_struct *tsk, int pkey,
1268c2ecf20Sopenharmony_ci		unsigned long init_val);
1278c2ecf20Sopenharmony_ciextern void copy_init_pkru_to_fpregs(void);
1288c2ecf20Sopenharmony_ci
1298c2ecf20Sopenharmony_cistatic inline int vma_pkey(struct vm_area_struct *vma)
1308c2ecf20Sopenharmony_ci{
1318c2ecf20Sopenharmony_ci	unsigned long vma_pkey_mask = VM_PKEY_BIT0 | VM_PKEY_BIT1 |
1328c2ecf20Sopenharmony_ci				      VM_PKEY_BIT2 | VM_PKEY_BIT3;
1338c2ecf20Sopenharmony_ci
1348c2ecf20Sopenharmony_ci	return (vma->vm_flags & vma_pkey_mask) >> VM_PKEY_SHIFT;
1358c2ecf20Sopenharmony_ci}
1368c2ecf20Sopenharmony_ci
1378c2ecf20Sopenharmony_ci#endif /*_ASM_X86_PKEYS_H */
138