1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _ASM_POWERPC_BOOK3S_32_KUP_H
3 #define _ASM_POWERPC_BOOK3S_32_KUP_H
6 #include <asm/book3s/32/mmu-hash.h>
10 .macro kuep_update_sr gpr1
, gpr2
/* NEVER use r0 as gpr2 due to addis */
11 101: mtsrin \gpr1
, \gpr2
12 addi \gpr1
, \gpr1
, 0x111 /* next VSID */
13 rlwinm \gpr1
, \gpr1
, 0, 0xf0ffffff /* clear VSID overflow */
14 addis \gpr2
, \gpr2
, 0x1000 /* address of next segment */
19 .macro kuep_lock gpr1
, gpr2
20 #ifdef CONFIG_PPC_KUEP
21 li \gpr1
, NUM_USER_SEGMENTS
25 oris \gpr1
, \gpr1
, SR_NX@h
/* set Nx */
26 kuep_update_sr \gpr1
, \gpr2
30 .macro kuep_unlock gpr1
, gpr2
31 #ifdef CONFIG_PPC_KUEP
32 li \gpr1
, NUM_USER_SEGMENTS
36 rlwinm \gpr1
, \gpr1
, 0, ~SR_NX
/* Clear Nx */
37 kuep_update_sr \gpr1
, \gpr2
41 #ifdef CONFIG_PPC_KUAP
43 .macro kuap_update_sr gpr1
, gpr2
, gpr3
/* NEVER use r0 as gpr2 due to addis */
44 101: mtsrin \gpr1
, \gpr2
45 addi \gpr1
, \gpr1
, 0x111 /* next VSID */
46 rlwinm \gpr1
, \gpr1
, 0, 0xf0ffffff /* clear VSID overflow */
47 addis \gpr2
, \gpr2
, 0x1000 /* address of next segment */
53 .macro kuap_save_and_lock sp
, thread
, gpr1
, gpr2
, gpr3
54 lwz \gpr2
, KUAP(\thread
)
55 rlwinm
. \gpr3
, \gpr2
, 28, 0xf0000000
56 stw \gpr2
, STACK_REGS_KUAP(\sp
)
59 stw \gpr1
, KUAP(\thread
)
61 oris \gpr1
, \gpr1
, SR_KS@h
/* set Ks */
62 kuap_update_sr \gpr1
, \gpr2
, \gpr3
66 .macro kuap_restore sp
, current
, gpr1
, gpr2
, gpr3
67 lwz \gpr2
, STACK_REGS_KUAP(\sp
)
68 rlwinm
. \gpr3
, \gpr2
, 28, 0xf0000000
69 stw \gpr2
, THREAD
+ KUAP(\current
)
72 rlwinm \gpr1
, \gpr1
, 0, ~SR_KS
/* Clear Ks */
73 kuap_update_sr \gpr1
, \gpr2
, \gpr3
77 .macro kuap_check current
, gpr
78 #ifdef CONFIG_PPC_KUAP_DEBUG
79 lwz \gpr
, THREAD
+ KUAP(\current
)
81 EMIT_BUG_ENTRY
999b
, __FILE__
, __LINE__
, (BUGFLAG_WARNING
| BUGFLAG_ONCE
)
85 #endif /* CONFIG_PPC_KUAP */
87 #else /* !__ASSEMBLY__ */
89 #ifdef CONFIG_PPC_KUAP
91 #include <linux/sched.h>
93 static inline void kuap_update_sr(u32 sr
, u32 addr
, u32 end
)
95 addr
&= 0xf0000000; /* align addr to start of segment */
96 barrier(); /* make sure thread.kuap is updated before playing with SRs */
99 sr
+= 0x111; /* next VSID */
100 sr
&= 0xf0ffffff; /* clear VSID overflow */
101 addr
+= 0x10000000; /* address of next segment */
103 isync(); /* Context sync required after mtsrin() */
106 static __always_inline
void allow_user_access(void __user
*to
, const void __user
*from
,
107 u32 size
, unsigned long dir
)
111 BUILD_BUG_ON(!__builtin_constant_p(dir
));
112 BUILD_BUG_ON(dir
& ~KUAP_READ_WRITE
);
114 if (!(dir
& KUAP_WRITE
))
117 addr
= (__force u32
)to
;
119 if (unlikely(addr
>= TASK_SIZE
|| !size
))
122 end
= min(addr
+ size
, TASK_SIZE
);
124 current
->thread
.kuap
= (addr
& 0xf0000000) | ((((end
- 1) >> 28) + 1) & 0xf);
125 kuap_update_sr(mfsrin(addr
) & ~SR_KS
, addr
, end
); /* Clear Ks */
128 static __always_inline
void prevent_user_access(void __user
*to
, const void __user
*from
,
129 u32 size
, unsigned long dir
)
133 BUILD_BUG_ON(!__builtin_constant_p(dir
));
135 if (dir
& KUAP_CURRENT_WRITE
) {
136 u32 kuap
= current
->thread
.kuap
;
141 addr
= kuap
& 0xf0000000;
143 } else if (dir
& KUAP_WRITE
) {
144 addr
= (__force u32
)to
;
145 end
= min(addr
+ size
, TASK_SIZE
);
147 if (unlikely(addr
>= TASK_SIZE
|| !size
))
153 current
->thread
.kuap
= 0;
154 kuap_update_sr(mfsrin(addr
) | SR_KS
, addr
, end
); /* set Ks */
157 static inline unsigned long prevent_user_access_return(void)
159 unsigned long flags
= current
->thread
.kuap
;
160 unsigned long addr
= flags
& 0xf0000000;
161 unsigned long end
= flags
<< 28;
162 void __user
*to
= (__force
void __user
*)addr
;
165 prevent_user_access(to
, to
, end
- addr
, KUAP_READ_WRITE
);
170 static inline void restore_user_access(unsigned long flags
)
172 unsigned long addr
= flags
& 0xf0000000;
173 unsigned long end
= flags
<< 28;
174 void __user
*to
= (__force
void __user
*)addr
;
177 allow_user_access(to
, to
, end
- addr
, KUAP_READ_WRITE
);
181 bad_kuap_fault(struct pt_regs
*regs
, unsigned long address
, bool is_write
)
183 unsigned long begin
= regs
->kuap
& 0xf0000000;
184 unsigned long end
= regs
->kuap
<< 28;
186 return is_write
&& (address
< begin
|| address
>= end
);
189 #endif /* CONFIG_PPC_KUAP */
191 #endif /* __ASSEMBLY__ */
193 #endif /* _ASM_POWERPC_BOOK3S_32_KUP_H */