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)
9 #ifdef CONFIG_PPC_BOOK3S_64
10 #include <asm/book3s/64/kup.h>
14 #include <asm/nohash/32/kup-8xx.h>
17 #ifdef CONFIG_BOOKE_OR_40x
18 #include <asm/nohash/kup-booke.h>
21 #ifdef CONFIG_PPC_BOOK3S_32
22 #include <asm/book3s/32/kup.h>
26 #ifndef CONFIG_PPC_KUAP
27 .macro kuap_check_amr gpr1, gpr2
32 #else /* !__ASSEMBLY__ */
34 extern bool disable_kuep;
35 extern bool disable_kuap;
37 #include <linux/pgtable.h>
40 void setup_kuep(bool disabled);
42 #ifdef CONFIG_PPC_KUAP
43 void setup_kuap(bool disabled);
45 static inline void setup_kuap(bool disabled) { }
47 static __always_inline bool kuap_is_disabled(void) { return true; }
50 __bad_kuap_fault(struct pt_regs *regs, unsigned long address, bool is_write)
55 static inline void __kuap_lock(void) { }
56 static inline void __kuap_save_and_lock(struct pt_regs *regs) { }
57 static inline void kuap_user_restore(struct pt_regs *regs) { }
58 static inline void __kuap_kernel_restore(struct pt_regs *regs, unsigned long amr) { }
60 static inline unsigned long __kuap_get_and_assert_locked(void)
66 * book3s/64/kup-radix.h defines these functions for the !KUAP case to flush
67 * the L1D cache after user accesses. Only include the empty stubs for other
70 #ifndef CONFIG_PPC_BOOK3S_64
71 static inline void __allow_user_access(void __user *to, const void __user *from,
72 unsigned long size, unsigned long dir) { }
73 static inline void __prevent_user_access(unsigned long dir) { }
74 static inline unsigned long __prevent_user_access_return(void) { return 0UL; }
75 static inline void __restore_user_access(unsigned long flags) { }
76 #endif /* CONFIG_PPC_BOOK3S_64 */
77 #endif /* CONFIG_PPC_KUAP */
79 static __always_inline bool
80 bad_kuap_fault(struct pt_regs *regs, unsigned long address, bool is_write)
82 if (kuap_is_disabled())
85 return __bad_kuap_fault(regs, address, is_write);
88 static __always_inline void kuap_assert_locked(void)
90 if (kuap_is_disabled())
93 if (IS_ENABLED(CONFIG_PPC_KUAP_DEBUG))
94 __kuap_get_and_assert_locked();
97 static __always_inline void kuap_lock(void)
99 if (kuap_is_disabled())
105 static __always_inline void kuap_save_and_lock(struct pt_regs *regs)
107 if (kuap_is_disabled())
110 __kuap_save_and_lock(regs);
113 static __always_inline void kuap_kernel_restore(struct pt_regs *regs, unsigned long amr)
115 if (kuap_is_disabled())
118 __kuap_kernel_restore(regs, amr);
121 static __always_inline unsigned long kuap_get_and_assert_locked(void)
123 if (kuap_is_disabled())
126 return __kuap_get_and_assert_locked();
129 #ifndef CONFIG_PPC_BOOK3S_64
130 static __always_inline void allow_user_access(void __user *to, const void __user *from,
131 unsigned long size, unsigned long dir)
133 if (kuap_is_disabled())
136 __allow_user_access(to, from, size, dir);
139 static __always_inline void prevent_user_access(unsigned long dir)
141 if (kuap_is_disabled())
144 __prevent_user_access(dir);
147 static __always_inline unsigned long prevent_user_access_return(void)
149 if (kuap_is_disabled())
152 return __prevent_user_access_return();
155 static __always_inline void restore_user_access(unsigned long flags)
157 if (kuap_is_disabled())
160 __restore_user_access(flags);
162 #endif /* CONFIG_PPC_BOOK3S_64 */
164 static __always_inline void allow_read_from_user(const void __user *from, unsigned long size)
167 allow_user_access(NULL, from, size, KUAP_READ);
170 static __always_inline void allow_write_to_user(void __user *to, unsigned long size)
172 allow_user_access(to, NULL, size, KUAP_WRITE);
175 static __always_inline void allow_read_write_user(void __user *to, const void __user *from,
179 allow_user_access(to, from, size, KUAP_READ_WRITE);
182 static __always_inline void prevent_read_from_user(const void __user *from, unsigned long size)
184 prevent_user_access(KUAP_READ);
187 static __always_inline void prevent_write_to_user(void __user *to, unsigned long size)
189 prevent_user_access(KUAP_WRITE);
192 static __always_inline void prevent_read_write_user(void __user *to, const void __user *from,
195 prevent_user_access(KUAP_READ_WRITE);
198 static __always_inline void prevent_current_access_user(void)
200 prevent_user_access(KUAP_READ_WRITE);
203 static __always_inline void prevent_current_read_from_user(void)
205 prevent_user_access(KUAP_READ);
208 static __always_inline void prevent_current_write_to_user(void)
210 prevent_user_access(KUAP_WRITE);
213 #endif /* !__ASSEMBLY__ */
215 #endif /* _ASM_POWERPC_KUAP_H_ */