xref: /openbmc/linux/arch/powerpc/include/asm/kup.h (revision 1d05334d)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _ASM_POWERPC_KUP_H_
3 #define _ASM_POWERPC_KUP_H_
4 
5 #define KUAP_READ	1
6 #define KUAP_WRITE	2
7 #define KUAP_READ_WRITE	(KUAP_READ | KUAP_WRITE)
8 /*
9  * For prevent_user_access() only.
10  * Use the current saved situation instead of the to/from/size params.
11  * Used on book3s/32
12  */
13 #define KUAP_CURRENT	4
14 
15 #ifdef CONFIG_PPC64
16 #include <asm/book3s/64/kup-radix.h>
17 #endif
18 #ifdef CONFIG_PPC_8xx
19 #include <asm/nohash/32/kup-8xx.h>
20 #endif
21 #ifdef CONFIG_PPC_BOOK3S_32
22 #include <asm/book3s/32/kup.h>
23 #endif
24 
25 #ifdef __ASSEMBLY__
26 #ifndef CONFIG_PPC_KUAP
27 .macro kuap_save_and_lock	sp, thread, gpr1, gpr2, gpr3
28 .endm
29 
30 .macro kuap_restore	sp, current, gpr1, gpr2, gpr3
31 .endm
32 
33 .macro kuap_check	current, gpr
34 .endm
35 
36 #endif
37 
38 #else /* !__ASSEMBLY__ */
39 
40 #include <asm/pgtable.h>
41 
42 void setup_kup(void);
43 
44 #ifdef CONFIG_PPC_KUEP
45 void setup_kuep(bool disabled);
46 #else
47 static inline void setup_kuep(bool disabled) { }
48 #endif /* CONFIG_PPC_KUEP */
49 
50 #ifdef CONFIG_PPC_KUAP
51 void setup_kuap(bool disabled);
52 #else
53 static inline void setup_kuap(bool disabled) { }
54 static inline void allow_user_access(void __user *to, const void __user *from,
55 				     unsigned long size, unsigned long dir) { }
56 static inline void prevent_user_access(void __user *to, const void __user *from,
57 				       unsigned long size, unsigned long dir) { }
58 static inline unsigned long prevent_user_access_return(void) { return 0UL; }
59 static inline void restore_user_access(unsigned long flags) { }
60 static inline bool
61 bad_kuap_fault(struct pt_regs *regs, unsigned long address, bool is_write)
62 {
63 	return false;
64 }
65 #endif /* CONFIG_PPC_KUAP */
66 
67 static inline void allow_read_from_user(const void __user *from, unsigned long size)
68 {
69 	allow_user_access(NULL, from, size, KUAP_READ);
70 }
71 
72 static inline void allow_write_to_user(void __user *to, unsigned long size)
73 {
74 	allow_user_access(to, NULL, size, KUAP_WRITE);
75 }
76 
77 static inline void allow_read_write_user(void __user *to, const void __user *from,
78 					 unsigned long size)
79 {
80 	allow_user_access(to, from, size, KUAP_READ_WRITE);
81 }
82 
83 static inline void prevent_read_from_user(const void __user *from, unsigned long size)
84 {
85 	prevent_user_access(NULL, from, size, KUAP_READ);
86 }
87 
88 static inline void prevent_write_to_user(void __user *to, unsigned long size)
89 {
90 	prevent_user_access(to, NULL, size, KUAP_WRITE);
91 }
92 
93 static inline void prevent_read_write_user(void __user *to, const void __user *from,
94 					   unsigned long size)
95 {
96 	prevent_user_access(to, from, size, KUAP_READ_WRITE);
97 }
98 
99 static inline void prevent_current_access_user(void)
100 {
101 	prevent_user_access(NULL, NULL, ~0UL, KUAP_CURRENT);
102 }
103 
104 #endif /* !__ASSEMBLY__ */
105 
106 #endif /* _ASM_POWERPC_KUAP_H_ */
107