1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _ASM_POWERPC_KUP_H_
3 #define _ASM_POWERPC_KUP_H_
7 #define KUAP_READ_WRITE (KUAP_READ | KUAP_WRITE)
10 #include <linux/types.h>
12 static __always_inline bool kuap_is_disabled(void);
15 #ifdef CONFIG_PPC_BOOK3S_64
16 #include <asm/book3s/64/kup.h>
20 #include <asm/nohash/32/kup-8xx.h>
23 #ifdef CONFIG_BOOKE_OR_40x
24 #include <asm/nohash/kup-booke.h>
27 #ifdef CONFIG_PPC_BOOK3S_32
28 #include <asm/book3s/32/kup.h>
32 #ifndef CONFIG_PPC_KUAP
33 .macro kuap_check_amr gpr1, gpr2
38 #else /* !__ASSEMBLY__ */
40 extern bool disable_kuep;
41 extern bool disable_kuap;
43 #include <linux/pgtable.h>
46 void setup_kuep(bool disabled);
48 #ifdef CONFIG_PPC_KUAP
49 void setup_kuap(bool disabled);
51 static __always_inline bool kuap_is_disabled(void)
53 return !mmu_has_feature(MMU_FTR_KUAP);
56 static inline void setup_kuap(bool disabled) { }
58 static __always_inline bool kuap_is_disabled(void) { return true; }
60 static __always_inline bool
61 __bad_kuap_fault(struct pt_regs *regs, unsigned long address, bool is_write)
66 static __always_inline void kuap_user_restore(struct pt_regs *regs) { }
67 static __always_inline void __kuap_kernel_restore(struct pt_regs *regs, unsigned long amr) { }
70 * book3s/64/kup-radix.h defines these functions for the !KUAP case to flush
71 * the L1D cache after user accesses. Only include the empty stubs for other
74 #ifndef CONFIG_PPC_BOOK3S_64
75 static __always_inline void allow_user_access(void __user *to, const void __user *from,
76 unsigned long size, unsigned long dir) { }
77 static __always_inline void prevent_user_access(unsigned long dir) { }
78 static __always_inline unsigned long prevent_user_access_return(void) { return 0UL; }
79 static __always_inline void restore_user_access(unsigned long flags) { }
80 #endif /* CONFIG_PPC_BOOK3S_64 */
81 #endif /* CONFIG_PPC_KUAP */
83 static __always_inline bool
84 bad_kuap_fault(struct pt_regs *regs, unsigned long address, bool is_write)
86 if (kuap_is_disabled())
89 return __bad_kuap_fault(regs, address, is_write);
92 static __always_inline void kuap_lock(void)
95 if (kuap_is_disabled())
102 static __always_inline void kuap_save_and_lock(struct pt_regs *regs)
104 #ifdef __kuap_save_and_lock
105 if (kuap_is_disabled())
108 __kuap_save_and_lock(regs);
112 static __always_inline void kuap_kernel_restore(struct pt_regs *regs, unsigned long amr)
114 if (kuap_is_disabled())
117 __kuap_kernel_restore(regs, amr);
120 static __always_inline unsigned long kuap_get_and_assert_locked(void)
122 #ifdef __kuap_get_and_assert_locked
123 if (!kuap_is_disabled())
124 return __kuap_get_and_assert_locked();
129 static __always_inline void kuap_assert_locked(void)
131 if (IS_ENABLED(CONFIG_PPC_KUAP_DEBUG))
132 kuap_get_and_assert_locked();
135 static __always_inline void allow_read_from_user(const void __user *from, unsigned long size)
138 allow_user_access(NULL, from, size, KUAP_READ);
141 static __always_inline void allow_write_to_user(void __user *to, unsigned long size)
143 allow_user_access(to, NULL, size, KUAP_WRITE);
146 static __always_inline void allow_read_write_user(void __user *to, const void __user *from,
150 allow_user_access(to, from, size, KUAP_READ_WRITE);
153 static __always_inline void prevent_read_from_user(const void __user *from, unsigned long size)
155 prevent_user_access(KUAP_READ);
158 static __always_inline void prevent_write_to_user(void __user *to, unsigned long size)
160 prevent_user_access(KUAP_WRITE);
163 static __always_inline void prevent_read_write_user(void __user *to, const void __user *from,
166 prevent_user_access(KUAP_READ_WRITE);
169 static __always_inline void prevent_current_access_user(void)
171 prevent_user_access(KUAP_READ_WRITE);
174 static __always_inline void prevent_current_read_from_user(void)
176 prevent_user_access(KUAP_READ);
179 static __always_inline void prevent_current_write_to_user(void)
181 prevent_user_access(KUAP_WRITE);
184 #endif /* !__ASSEMBLY__ */
186 #endif /* _ASM_POWERPC_KUAP_H_ */