1/* SPDX-License-Identifier: GPL-2.0 */
2#ifndef _ASM_POWERPC_KUP_8XX_H_
3#define _ASM_POWERPC_KUP_8XX_H_
4
5#include <asm/bug.h>
6#include <asm/mmu.h>
7
8#ifdef CONFIG_PPC_KUAP
9
10#ifndef __ASSEMBLY__
11
12#include <asm/reg.h>
13
14static __always_inline void __kuap_save_and_lock(struct pt_regs *regs)
15{
16	regs->kuap = mfspr(SPRN_MD_AP);
17	mtspr(SPRN_MD_AP, MD_APG_KUAP);
18}
19#define __kuap_save_and_lock __kuap_save_and_lock
20
21static __always_inline void kuap_user_restore(struct pt_regs *regs)
22{
23}
24
25static __always_inline void __kuap_kernel_restore(struct pt_regs *regs, unsigned long kuap)
26{
27	mtspr(SPRN_MD_AP, regs->kuap);
28}
29
30#ifdef CONFIG_PPC_KUAP_DEBUG
31static __always_inline unsigned long __kuap_get_and_assert_locked(void)
32{
33	WARN_ON_ONCE(mfspr(SPRN_MD_AP) >> 16 != MD_APG_KUAP >> 16);
34
35	return 0;
36}
37#define __kuap_get_and_assert_locked __kuap_get_and_assert_locked
38#endif
39
40static __always_inline void uaccess_begin_8xx(unsigned long val)
41{
42	asm(ASM_MMU_FTR_IFSET("mtspr %0, %1", "", %2) : :
43	    "i"(SPRN_MD_AP), "r"(val), "i"(MMU_FTR_KUAP) : "memory");
44}
45
46static __always_inline void uaccess_end_8xx(void)
47{
48	asm(ASM_MMU_FTR_IFSET("mtspr %0, %1", "", %2) : :
49	    "i"(SPRN_MD_AP), "r"(MD_APG_KUAP), "i"(MMU_FTR_KUAP) : "memory");
50}
51
52static __always_inline void allow_user_access(void __user *to, const void __user *from,
53					      unsigned long size, unsigned long dir)
54{
55	uaccess_begin_8xx(MD_APG_INIT);
56}
57
58static __always_inline void prevent_user_access(unsigned long dir)
59{
60	uaccess_end_8xx();
61}
62
63static __always_inline unsigned long prevent_user_access_return(void)
64{
65	unsigned long flags;
66
67	flags = mfspr(SPRN_MD_AP);
68
69	uaccess_end_8xx();
70
71	return flags;
72}
73
74static __always_inline void restore_user_access(unsigned long flags)
75{
76	uaccess_begin_8xx(flags);
77}
78
79static __always_inline bool
80__bad_kuap_fault(struct pt_regs *regs, unsigned long address, bool is_write)
81{
82	return !((regs->kuap ^ MD_APG_KUAP) & 0xff000000);
83}
84
85#endif /* !__ASSEMBLY__ */
86
87#endif /* CONFIG_PPC_KUAP */
88
89#endif /* _ASM_POWERPC_KUP_8XX_H_ */
90