xref: /openbmc/linux/arch/powerpc/include/asm/kup.h (revision 0b26ca68)
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_READ	4
14 #define KUAP_CURRENT_WRITE	8
15 #define KUAP_CURRENT		(KUAP_CURRENT_READ | KUAP_CURRENT_WRITE)
16 
17 #ifdef CONFIG_PPC_BOOK3S_64
18 #include <asm/book3s/64/kup.h>
19 #endif
20 
21 #ifdef CONFIG_PPC_8xx
22 #include <asm/nohash/32/kup-8xx.h>
23 #endif
24 
25 #ifdef CONFIG_PPC_BOOK3S_32
26 #include <asm/book3s/32/kup.h>
27 #endif
28 
29 #ifdef __ASSEMBLY__
30 #ifndef CONFIG_PPC_KUAP
31 .macro kuap_save_and_lock	sp, thread, gpr1, gpr2, gpr3
32 .endm
33 
34 .macro kuap_restore	sp, current, gpr1, gpr2, gpr3
35 .endm
36 
37 .macro kuap_check	current, gpr
38 .endm
39 
40 .macro kuap_check_amr	gpr1, gpr2
41 .endm
42 
43 #endif
44 
45 #else /* !__ASSEMBLY__ */
46 
47 extern bool disable_kuep;
48 extern bool disable_kuap;
49 
50 #include <linux/pgtable.h>
51 
52 #ifdef CONFIG_PPC_KUEP
53 void setup_kuep(bool disabled);
54 #else
55 static inline void setup_kuep(bool disabled) { }
56 #endif /* CONFIG_PPC_KUEP */
57 
58 #ifdef CONFIG_PPC_KUAP
59 void setup_kuap(bool disabled);
60 #else
61 static inline void setup_kuap(bool disabled) { }
62 
63 static inline bool
64 bad_kuap_fault(struct pt_regs *regs, unsigned long address, bool is_write)
65 {
66 	return false;
67 }
68 
69 static inline void kuap_check_amr(void) { }
70 
71 /*
72  * book3s/64/kup-radix.h defines these functions for the !KUAP case to flush
73  * the L1D cache after user accesses. Only include the empty stubs for other
74  * platforms.
75  */
76 #ifndef CONFIG_PPC_BOOK3S_64
77 static inline void allow_user_access(void __user *to, const void __user *from,
78 				     unsigned long size, unsigned long dir) { }
79 static inline void prevent_user_access(void __user *to, const void __user *from,
80 				       unsigned long size, unsigned long dir) { }
81 static inline unsigned long prevent_user_access_return(void) { return 0UL; }
82 static inline void restore_user_access(unsigned long flags) { }
83 #endif /* CONFIG_PPC_BOOK3S_64 */
84 #endif /* CONFIG_PPC_KUAP */
85 
86 static __always_inline void setup_kup(void)
87 {
88 	setup_kuep(disable_kuep);
89 	setup_kuap(disable_kuap);
90 }
91 
92 static inline void allow_read_from_user(const void __user *from, unsigned long size)
93 {
94 	allow_user_access(NULL, from, size, KUAP_READ);
95 }
96 
97 static inline void allow_write_to_user(void __user *to, unsigned long size)
98 {
99 	allow_user_access(to, NULL, size, KUAP_WRITE);
100 }
101 
102 static inline void allow_read_write_user(void __user *to, const void __user *from,
103 					 unsigned long size)
104 {
105 	allow_user_access(to, from, size, KUAP_READ_WRITE);
106 }
107 
108 static inline void prevent_read_from_user(const void __user *from, unsigned long size)
109 {
110 	prevent_user_access(NULL, from, size, KUAP_READ);
111 }
112 
113 static inline void prevent_write_to_user(void __user *to, unsigned long size)
114 {
115 	prevent_user_access(to, NULL, size, KUAP_WRITE);
116 }
117 
118 static inline void prevent_read_write_user(void __user *to, const void __user *from,
119 					   unsigned long size)
120 {
121 	prevent_user_access(to, from, size, KUAP_READ_WRITE);
122 }
123 
124 static inline void prevent_current_access_user(void)
125 {
126 	prevent_user_access(NULL, NULL, ~0UL, KUAP_CURRENT);
127 }
128 
129 static inline void prevent_current_read_from_user(void)
130 {
131 	prevent_user_access(NULL, NULL, ~0UL, KUAP_CURRENT_READ);
132 }
133 
134 static inline void prevent_current_write_to_user(void)
135 {
136 	prevent_user_access(NULL, NULL, ~0UL, KUAP_CURRENT_WRITE);
137 }
138 
139 #endif /* !__ASSEMBLY__ */
140 
141 #endif /* _ASM_POWERPC_KUAP_H_ */
142