18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */
28c2ecf20Sopenharmony_ci#ifndef _ASM_POWERPC_KUP_H_
38c2ecf20Sopenharmony_ci#define _ASM_POWERPC_KUP_H_
48c2ecf20Sopenharmony_ci
58c2ecf20Sopenharmony_ci#define KUAP_READ	1
68c2ecf20Sopenharmony_ci#define KUAP_WRITE	2
78c2ecf20Sopenharmony_ci#define KUAP_READ_WRITE	(KUAP_READ | KUAP_WRITE)
88c2ecf20Sopenharmony_ci/*
98c2ecf20Sopenharmony_ci * For prevent_user_access() only.
108c2ecf20Sopenharmony_ci * Use the current saved situation instead of the to/from/size params.
118c2ecf20Sopenharmony_ci * Used on book3s/32
128c2ecf20Sopenharmony_ci */
138c2ecf20Sopenharmony_ci#define KUAP_CURRENT_READ	4
148c2ecf20Sopenharmony_ci#define KUAP_CURRENT_WRITE	8
158c2ecf20Sopenharmony_ci#define KUAP_CURRENT		(KUAP_CURRENT_READ | KUAP_CURRENT_WRITE)
168c2ecf20Sopenharmony_ci
178c2ecf20Sopenharmony_ci#ifdef CONFIG_PPC_BOOK3S_64
188c2ecf20Sopenharmony_ci#include <asm/book3s/64/kup-radix.h>
198c2ecf20Sopenharmony_ci#endif
208c2ecf20Sopenharmony_ci#ifdef CONFIG_PPC_8xx
218c2ecf20Sopenharmony_ci#include <asm/nohash/32/kup-8xx.h>
228c2ecf20Sopenharmony_ci#endif
238c2ecf20Sopenharmony_ci#ifdef CONFIG_PPC_BOOK3S_32
248c2ecf20Sopenharmony_ci#include <asm/book3s/32/kup.h>
258c2ecf20Sopenharmony_ci#endif
268c2ecf20Sopenharmony_ci
278c2ecf20Sopenharmony_ci#ifdef __ASSEMBLY__
288c2ecf20Sopenharmony_ci#ifndef CONFIG_PPC_KUAP
298c2ecf20Sopenharmony_ci.macro kuap_save_and_lock	sp, thread, gpr1, gpr2, gpr3
308c2ecf20Sopenharmony_ci.endm
318c2ecf20Sopenharmony_ci
328c2ecf20Sopenharmony_ci.macro kuap_restore	sp, current, gpr1, gpr2, gpr3
338c2ecf20Sopenharmony_ci.endm
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_ci.macro kuap_check	current, gpr
368c2ecf20Sopenharmony_ci.endm
378c2ecf20Sopenharmony_ci
388c2ecf20Sopenharmony_ci.macro kuap_check_amr	gpr1, gpr2
398c2ecf20Sopenharmony_ci.endm
408c2ecf20Sopenharmony_ci
418c2ecf20Sopenharmony_ci#endif
428c2ecf20Sopenharmony_ci
438c2ecf20Sopenharmony_ci#else /* !__ASSEMBLY__ */
448c2ecf20Sopenharmony_ci
458c2ecf20Sopenharmony_ci#include <linux/pgtable.h>
468c2ecf20Sopenharmony_ci
478c2ecf20Sopenharmony_civoid setup_kup(void);
488c2ecf20Sopenharmony_ci
498c2ecf20Sopenharmony_ci#ifdef CONFIG_PPC_KUEP
508c2ecf20Sopenharmony_civoid setup_kuep(bool disabled);
518c2ecf20Sopenharmony_ci#else
528c2ecf20Sopenharmony_cistatic inline void setup_kuep(bool disabled) { }
538c2ecf20Sopenharmony_ci#endif /* CONFIG_PPC_KUEP */
548c2ecf20Sopenharmony_ci
558c2ecf20Sopenharmony_ci#ifdef CONFIG_PPC_KUAP
568c2ecf20Sopenharmony_civoid setup_kuap(bool disabled);
578c2ecf20Sopenharmony_ci#else
588c2ecf20Sopenharmony_cistatic inline void setup_kuap(bool disabled) { }
598c2ecf20Sopenharmony_ci
608c2ecf20Sopenharmony_cistatic inline bool
618c2ecf20Sopenharmony_cibad_kuap_fault(struct pt_regs *regs, unsigned long address, bool is_write)
628c2ecf20Sopenharmony_ci{
638c2ecf20Sopenharmony_ci	return false;
648c2ecf20Sopenharmony_ci}
658c2ecf20Sopenharmony_ci
668c2ecf20Sopenharmony_cistatic inline void kuap_check_amr(void) { }
678c2ecf20Sopenharmony_ci
688c2ecf20Sopenharmony_ci/*
698c2ecf20Sopenharmony_ci * book3s/64/kup-radix.h defines these functions for the !KUAP case to flush
708c2ecf20Sopenharmony_ci * the L1D cache after user accesses. Only include the empty stubs for other
718c2ecf20Sopenharmony_ci * platforms.
728c2ecf20Sopenharmony_ci */
738c2ecf20Sopenharmony_ci#ifndef CONFIG_PPC_BOOK3S_64
748c2ecf20Sopenharmony_cistatic inline void allow_user_access(void __user *to, const void __user *from,
758c2ecf20Sopenharmony_ci				     unsigned long size, unsigned long dir) { }
768c2ecf20Sopenharmony_cistatic inline void prevent_user_access(void __user *to, const void __user *from,
778c2ecf20Sopenharmony_ci				       unsigned long size, unsigned long dir) { }
788c2ecf20Sopenharmony_cistatic inline unsigned long prevent_user_access_return(void) { return 0UL; }
798c2ecf20Sopenharmony_cistatic inline void restore_user_access(unsigned long flags) { }
808c2ecf20Sopenharmony_ci#endif /* CONFIG_PPC_BOOK3S_64 */
818c2ecf20Sopenharmony_ci#endif /* CONFIG_PPC_KUAP */
828c2ecf20Sopenharmony_ci
838c2ecf20Sopenharmony_cistatic inline void allow_read_from_user(const void __user *from, unsigned long size)
848c2ecf20Sopenharmony_ci{
858c2ecf20Sopenharmony_ci	allow_user_access(NULL, from, size, KUAP_READ);
868c2ecf20Sopenharmony_ci}
878c2ecf20Sopenharmony_ci
888c2ecf20Sopenharmony_cistatic inline void allow_write_to_user(void __user *to, unsigned long size)
898c2ecf20Sopenharmony_ci{
908c2ecf20Sopenharmony_ci	allow_user_access(to, NULL, size, KUAP_WRITE);
918c2ecf20Sopenharmony_ci}
928c2ecf20Sopenharmony_ci
938c2ecf20Sopenharmony_cistatic inline void allow_read_write_user(void __user *to, const void __user *from,
948c2ecf20Sopenharmony_ci					 unsigned long size)
958c2ecf20Sopenharmony_ci{
968c2ecf20Sopenharmony_ci	allow_user_access(to, from, size, KUAP_READ_WRITE);
978c2ecf20Sopenharmony_ci}
988c2ecf20Sopenharmony_ci
998c2ecf20Sopenharmony_cistatic inline void prevent_read_from_user(const void __user *from, unsigned long size)
1008c2ecf20Sopenharmony_ci{
1018c2ecf20Sopenharmony_ci	prevent_user_access(NULL, from, size, KUAP_READ);
1028c2ecf20Sopenharmony_ci}
1038c2ecf20Sopenharmony_ci
1048c2ecf20Sopenharmony_cistatic inline void prevent_write_to_user(void __user *to, unsigned long size)
1058c2ecf20Sopenharmony_ci{
1068c2ecf20Sopenharmony_ci	prevent_user_access(to, NULL, size, KUAP_WRITE);
1078c2ecf20Sopenharmony_ci}
1088c2ecf20Sopenharmony_ci
1098c2ecf20Sopenharmony_cistatic inline void prevent_read_write_user(void __user *to, const void __user *from,
1108c2ecf20Sopenharmony_ci					   unsigned long size)
1118c2ecf20Sopenharmony_ci{
1128c2ecf20Sopenharmony_ci	prevent_user_access(to, from, size, KUAP_READ_WRITE);
1138c2ecf20Sopenharmony_ci}
1148c2ecf20Sopenharmony_ci
1158c2ecf20Sopenharmony_cistatic inline void prevent_current_access_user(void)
1168c2ecf20Sopenharmony_ci{
1178c2ecf20Sopenharmony_ci	prevent_user_access(NULL, NULL, ~0UL, KUAP_CURRENT);
1188c2ecf20Sopenharmony_ci}
1198c2ecf20Sopenharmony_ci
1208c2ecf20Sopenharmony_cistatic inline void prevent_current_read_from_user(void)
1218c2ecf20Sopenharmony_ci{
1228c2ecf20Sopenharmony_ci	prevent_user_access(NULL, NULL, ~0UL, KUAP_CURRENT_READ);
1238c2ecf20Sopenharmony_ci}
1248c2ecf20Sopenharmony_ci
1258c2ecf20Sopenharmony_cistatic inline void prevent_current_write_to_user(void)
1268c2ecf20Sopenharmony_ci{
1278c2ecf20Sopenharmony_ci	prevent_user_access(NULL, NULL, ~0UL, KUAP_CURRENT_WRITE);
1288c2ecf20Sopenharmony_ci}
1298c2ecf20Sopenharmony_ci
1308c2ecf20Sopenharmony_ci#endif /* !__ASSEMBLY__ */
1318c2ecf20Sopenharmony_ci
1328c2ecf20Sopenharmony_ci#endif /* _ASM_POWERPC_KUAP_H_ */
133