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