xref: /openbmc/linux/arch/powerpc/include/asm/kup.h (revision 79f382b9)
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 	barrier_nospec();
95 	allow_user_access(NULL, from, size, KUAP_READ);
96 }
97 
98 static inline void allow_write_to_user(void __user *to, unsigned long size)
99 {
100 	allow_user_access(to, NULL, size, KUAP_WRITE);
101 }
102 
103 static inline void allow_read_write_user(void __user *to, const void __user *from,
104 					 unsigned long size)
105 {
106 	barrier_nospec();
107 	allow_user_access(to, from, size, KUAP_READ_WRITE);
108 }
109 
110 static inline void prevent_read_from_user(const void __user *from, unsigned long size)
111 {
112 	prevent_user_access(NULL, from, size, KUAP_READ);
113 }
114 
115 static inline void prevent_write_to_user(void __user *to, unsigned long size)
116 {
117 	prevent_user_access(to, NULL, size, KUAP_WRITE);
118 }
119 
120 static inline void prevent_read_write_user(void __user *to, const void __user *from,
121 					   unsigned long size)
122 {
123 	prevent_user_access(to, from, size, KUAP_READ_WRITE);
124 }
125 
126 static inline void prevent_current_access_user(void)
127 {
128 	prevent_user_access(NULL, NULL, ~0UL, KUAP_CURRENT);
129 }
130 
131 static inline void prevent_current_read_from_user(void)
132 {
133 	prevent_user_access(NULL, NULL, ~0UL, KUAP_CURRENT_READ);
134 }
135 
136 static inline void prevent_current_write_to_user(void)
137 {
138 	prevent_user_access(NULL, NULL, ~0UL, KUAP_CURRENT_WRITE);
139 }
140 
141 #endif /* !__ASSEMBLY__ */
142 
143 #endif /* _ASM_POWERPC_KUAP_H_ */
144