xref: /linux/arch/powerpc/include/asm/kup.h (revision eb232b1624462752dc916d9015b31ecdac0a01f1)
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 #include <linux/pgtable.h>
48 
49 void setup_kup(void);
50 
51 #ifdef CONFIG_PPC_KUEP
52 void setup_kuep(bool disabled);
53 #else
54 static inline void setup_kuep(bool disabled) { }
55 #endif /* CONFIG_PPC_KUEP */
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 bad_kuap_fault(struct pt_regs *regs, unsigned long address,
63 				  bool is_write, unsigned long error_code)
64 {
65 	return false;
66 }
67 
68 static inline void kuap_check_amr(void) { }
69 
70 /*
71  * book3s/64/kup-radix.h defines these functions for the !KUAP case to flush
72  * the L1D cache after user accesses. Only include the empty stubs for other
73  * platforms.
74  */
75 #ifndef CONFIG_PPC_BOOK3S_64
76 static inline void allow_user_access(void __user *to, const void __user *from,
77 				     unsigned long size, unsigned long dir) { }
78 static inline void prevent_user_access(void __user *to, const void __user *from,
79 				       unsigned long size, unsigned long dir) { }
80 static inline unsigned long prevent_user_access_return(void) { return 0UL; }
81 static inline void restore_user_access(unsigned long flags) { }
82 #endif /* CONFIG_PPC_BOOK3S_64 */
83 #endif /* CONFIG_PPC_KUAP */
84 
85 static inline void allow_read_from_user(const void __user *from, unsigned long size)
86 {
87 	allow_user_access(NULL, from, size, KUAP_READ);
88 }
89 
90 static inline void allow_write_to_user(void __user *to, unsigned long size)
91 {
92 	allow_user_access(to, NULL, size, KUAP_WRITE);
93 }
94 
95 static inline void allow_read_write_user(void __user *to, const void __user *from,
96 					 unsigned long size)
97 {
98 	allow_user_access(to, from, size, KUAP_READ_WRITE);
99 }
100 
101 static inline void prevent_read_from_user(const void __user *from, unsigned long size)
102 {
103 	prevent_user_access(NULL, from, size, KUAP_READ);
104 }
105 
106 static inline void prevent_write_to_user(void __user *to, unsigned long size)
107 {
108 	prevent_user_access(to, NULL, size, KUAP_WRITE);
109 }
110 
111 static inline void prevent_read_write_user(void __user *to, const void __user *from,
112 					   unsigned long size)
113 {
114 	prevent_user_access(to, from, size, KUAP_READ_WRITE);
115 }
116 
117 static inline void prevent_current_access_user(void)
118 {
119 	prevent_user_access(NULL, NULL, ~0UL, KUAP_CURRENT);
120 }
121 
122 static inline void prevent_current_read_from_user(void)
123 {
124 	prevent_user_access(NULL, NULL, ~0UL, KUAP_CURRENT_READ);
125 }
126 
127 static inline void prevent_current_write_to_user(void)
128 {
129 	prevent_user_access(NULL, NULL, ~0UL, KUAP_CURRENT_WRITE);
130 }
131 
132 #endif /* !__ASSEMBLY__ */
133 
134 #endif /* _ASM_POWERPC_KUAP_H_ */
135