• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifdef CONFIG_PPC_BOOK3S_64
10 #include <asm/book3s/64/kup-radix.h>
11 #endif
12 #ifdef CONFIG_PPC_8xx
13 #include <asm/nohash/32/kup-8xx.h>
14 #endif
15 #ifdef CONFIG_PPC_BOOK3S_32
16 #include <asm/book3s/32/kup.h>
17 #endif
18 
19 #ifdef __ASSEMBLY__
20 #ifndef CONFIG_PPC_KUAP
21 .macro kuap_save_and_lock	sp, thread, gpr1, gpr2, gpr3
22 .endm
23 
24 .macro kuap_restore	sp, current, gpr1, gpr2, gpr3
25 .endm
26 
27 .macro kuap_restore_amr gpr
28 .endm
29 
30 .macro kuap_check	current, gpr
31 .endm
32 
33 .macro kuap_check_amr	gpr1, gpr2
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
setup_kuep(bool disabled)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
setup_kuap(bool disabled)53 static inline void setup_kuap(bool disabled) { }
54 
55 static inline bool
bad_kuap_fault(struct pt_regs * regs,unsigned long address,bool is_write)56 bad_kuap_fault(struct pt_regs *regs, unsigned long address, bool is_write)
57 {
58 	return false;
59 }
60 
kuap_check_amr(void)61 static inline void kuap_check_amr(void) { }
62 
63 /*
64  * book3s/64/kup-radix.h defines these functions for the !KUAP case to flush
65  * the L1D cache after user accesses. Only include the empty stubs for other
66  * platforms.
67  */
68 #ifndef CONFIG_PPC_BOOK3S_64
allow_user_access(void __user * to,const void __user * from,unsigned long size,unsigned long dir)69 static inline void allow_user_access(void __user *to, const void __user *from,
70 				     unsigned long size, unsigned long dir) { }
prevent_user_access(void __user * to,const void __user * from,unsigned long size,unsigned long dir)71 static inline void prevent_user_access(void __user *to, const void __user *from,
72 				       unsigned long size, unsigned long dir) { }
73 #endif /* CONFIG_PPC_BOOK3S_64 */
74 #endif /* CONFIG_PPC_KUAP */
75 
allow_read_from_user(const void __user * from,unsigned long size)76 static inline void allow_read_from_user(const void __user *from, unsigned long size)
77 {
78 	allow_user_access(NULL, from, size, KUAP_READ);
79 }
80 
allow_write_to_user(void __user * to,unsigned long size)81 static inline void allow_write_to_user(void __user *to, unsigned long size)
82 {
83 	allow_user_access(to, NULL, size, KUAP_WRITE);
84 }
85 
allow_read_write_user(void __user * to,const void __user * from,unsigned long size)86 static inline void allow_read_write_user(void __user *to, const void __user *from,
87 					 unsigned long size)
88 {
89 	allow_user_access(to, from, size, KUAP_READ_WRITE);
90 }
91 
prevent_read_from_user(const void __user * from,unsigned long size)92 static inline void prevent_read_from_user(const void __user *from, unsigned long size)
93 {
94 	prevent_user_access(NULL, from, size, KUAP_READ);
95 }
96 
prevent_write_to_user(void __user * to,unsigned long size)97 static inline void prevent_write_to_user(void __user *to, unsigned long size)
98 {
99 	prevent_user_access(to, NULL, size, KUAP_WRITE);
100 }
101 
prevent_read_write_user(void __user * to,const void __user * from,unsigned long size)102 static inline void prevent_read_write_user(void __user *to, const void __user *from,
103 					   unsigned long size)
104 {
105 	prevent_user_access(to, from, size, KUAP_READ_WRITE);
106 }
107 
108 #endif /* !__ASSEMBLY__ */
109 
110 #endif /* _ASM_POWERPC_KUAP_H_ */
111