xref: /linux/arch/powerpc/kernel/signal.c (revision bba2c3615bd6cfee7456d1130f2e6b01b3f4e9ba)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Common signal handling code for both 32 and 64 bits
4  *
5  *    Copyright (c) 2007 Benjamin Herrenschmidt, IBM Corporation
6  *    Extracted from signal_32.c and signal_64.c
7  */
8 
9 #include <linux/entry-common.h>
10 #include <linux/resume_user_mode.h>
11 #include <linux/signal.h>
12 #include <linux/uprobes.h>
13 #include <linux/key.h>
14 #include <linux/context_tracking.h>
15 #include <linux/livepatch.h>
16 #include <linux/syscalls.h>
17 #include <asm/hw_breakpoint.h>
18 #include <linux/uaccess.h>
19 #include <asm/switch_to.h>
20 #include <asm/unistd.h>
21 #include <asm/debug.h>
22 #include <asm/tm.h>
23 
24 #include "signal.h"
25 
26 #ifdef CONFIG_VSX
27 unsigned long copy_fpr_to_user(void __user *to,
28 			       struct task_struct *task)
29 {
30 	u64 buf[ELF_NFPREG];
31 	int i;
32 
33 	/* save FPR copy to local buffer then write to the thread_struct */
34 	for (i = 0; i < (ELF_NFPREG - 1) ; i++)
35 		buf[i] = task->thread.TS_FPR(i);
36 	buf[i] = task->thread.fp_state.fpscr;
37 	return __copy_to_user(to, buf, ELF_NFPREG * sizeof(double));
38 }
39 
40 unsigned long copy_fpr_from_user(struct task_struct *task,
41 				 void __user *from)
42 {
43 	u64 buf[ELF_NFPREG];
44 	int i;
45 
46 	if (__copy_from_user(buf, from, ELF_NFPREG * sizeof(double)))
47 		return 1;
48 	for (i = 0; i < (ELF_NFPREG - 1) ; i++)
49 		task->thread.TS_FPR(i) = buf[i];
50 	task->thread.fp_state.fpscr = buf[i];
51 
52 	return 0;
53 }
54 
55 unsigned long copy_vsx_to_user(void __user *to,
56 			       struct task_struct *task)
57 {
58 	u64 buf[ELF_NVSRHALFREG];
59 	int i;
60 
61 	/* save FPR copy to local buffer then write to the thread_struct */
62 	for (i = 0; i < ELF_NVSRHALFREG; i++)
63 		buf[i] = task->thread.fp_state.fpr[i][TS_VSRLOWOFFSET];
64 	return __copy_to_user(to, buf, ELF_NVSRHALFREG * sizeof(double));
65 }
66 
67 unsigned long copy_vsx_from_user(struct task_struct *task,
68 				 void __user *from)
69 {
70 	u64 buf[ELF_NVSRHALFREG];
71 	int i;
72 
73 	if (__copy_from_user(buf, from, ELF_NVSRHALFREG * sizeof(double)))
74 		return 1;
75 	for (i = 0; i < ELF_NVSRHALFREG ; i++)
76 		task->thread.fp_state.fpr[i][TS_VSRLOWOFFSET] = buf[i];
77 	return 0;
78 }
79 
80 #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
81 unsigned long copy_ckfpr_to_user(void __user *to,
82 				  struct task_struct *task)
83 {
84 	u64 buf[ELF_NFPREG];
85 	int i;
86 
87 	/* save FPR copy to local buffer then write to the thread_struct */
88 	for (i = 0; i < (ELF_NFPREG - 1) ; i++)
89 		buf[i] = task->thread.TS_CKFPR(i);
90 	buf[i] = task->thread.ckfp_state.fpscr;
91 	return __copy_to_user(to, buf, ELF_NFPREG * sizeof(double));
92 }
93 
94 unsigned long copy_ckfpr_from_user(struct task_struct *task,
95 					  void __user *from)
96 {
97 	u64 buf[ELF_NFPREG];
98 	int i;
99 
100 	if (__copy_from_user(buf, from, ELF_NFPREG * sizeof(double)))
101 		return 1;
102 	for (i = 0; i < (ELF_NFPREG - 1) ; i++)
103 		task->thread.TS_CKFPR(i) = buf[i];
104 	task->thread.ckfp_state.fpscr = buf[i];
105 
106 	return 0;
107 }
108 
109 unsigned long copy_ckvsx_to_user(void __user *to,
110 				  struct task_struct *task)
111 {
112 	u64 buf[ELF_NVSRHALFREG];
113 	int i;
114 
115 	/* save FPR copy to local buffer then write to the thread_struct */
116 	for (i = 0; i < ELF_NVSRHALFREG; i++)
117 		buf[i] = task->thread.ckfp_state.fpr[i][TS_VSRLOWOFFSET];
118 	return __copy_to_user(to, buf, ELF_NVSRHALFREG * sizeof(double));
119 }
120 
121 unsigned long copy_ckvsx_from_user(struct task_struct *task,
122 					  void __user *from)
123 {
124 	u64 buf[ELF_NVSRHALFREG];
125 	int i;
126 
127 	if (__copy_from_user(buf, from, ELF_NVSRHALFREG * sizeof(double)))
128 		return 1;
129 	for (i = 0; i < ELF_NVSRHALFREG ; i++)
130 		task->thread.ckfp_state.fpr[i][TS_VSRLOWOFFSET] = buf[i];
131 	return 0;
132 }
133 #endif /* CONFIG_PPC_TRANSACTIONAL_MEM */
134 #endif
135 
136 /* Log an error when sending an unhandled signal to a process. Controlled
137  * through debug.exception-trace sysctl.
138  */
139 
140 int show_unhandled_signals = 1;
141 
142 unsigned long get_min_sigframe_size(void)
143 {
144 	if (IS_ENABLED(CONFIG_PPC64))
145 		return get_min_sigframe_size_64();
146 	else
147 		return get_min_sigframe_size_32();
148 }
149 
150 #ifdef CONFIG_COMPAT
151 unsigned long get_min_sigframe_size_compat(void)
152 {
153 	return get_min_sigframe_size_32();
154 }
155 #endif
156 
157 /*
158  * Allocate space for the signal frame
159  */
160 static unsigned long get_tm_stackpointer(struct task_struct *tsk);
161 
162 void __user *get_sigframe(struct ksignal *ksig, struct task_struct *tsk,
163 			  size_t frame_size, int is_32)
164 {
165         unsigned long oldsp, newsp;
166 	unsigned long sp = get_tm_stackpointer(tsk);
167 
168         /* Default to using normal stack */
169 	if (is_32)
170 		oldsp = sp & 0x0ffffffffUL;
171 	else
172 		oldsp = sp;
173 	oldsp = sigsp(oldsp, ksig);
174 	newsp = (oldsp - frame_size) & ~0xFUL;
175 
176         return (void __user *)newsp;
177 }
178 
179 static void check_syscall_restart(struct pt_regs *regs, struct k_sigaction *ka,
180 				  int has_handler)
181 {
182 	unsigned long ret = regs->gpr[3];
183 	int restart = 1;
184 
185 	/* syscall ? */
186 	if (!trap_is_syscall(regs))
187 		return;
188 
189 	if (trap_norestart(regs))
190 		return;
191 
192 	/* error signalled ? */
193 	if (trap_is_scv(regs)) {
194 		/* 32-bit compat mode sign extend? */
195 		if (!IS_ERR_VALUE(ret))
196 			return;
197 		ret = -ret;
198 	} else if (!(regs->ccr & 0x10000000)) {
199 		return;
200 	}
201 
202 	switch (ret) {
203 	case ERESTART_RESTARTBLOCK:
204 	case ERESTARTNOHAND:
205 		/* ERESTARTNOHAND means that the syscall should only be
206 		 * restarted if there was no handler for the signal, and since
207 		 * we only get here if there is a handler, we dont restart.
208 		 */
209 		restart = !has_handler;
210 		break;
211 	case ERESTARTSYS:
212 		/* ERESTARTSYS means to restart the syscall if there is no
213 		 * handler or the handler was registered with SA_RESTART
214 		 */
215 		restart = !has_handler || (ka->sa.sa_flags & SA_RESTART) != 0;
216 		break;
217 	case ERESTARTNOINTR:
218 		/* ERESTARTNOINTR means that the syscall should be
219 		 * called again after the signal handler returns.
220 		 */
221 		break;
222 	default:
223 		return;
224 	}
225 	if (restart) {
226 		if (ret == ERESTART_RESTARTBLOCK)
227 			regs->gpr[0] = __NR_restart_syscall;
228 		else
229 			regs->gpr[3] = regs->orig_gpr3;
230 		regs_add_return_ip(regs, -4);
231 		regs->result = 0;
232 	} else {
233 		if (trap_is_scv(regs)) {
234 			regs->result = -EINTR;
235 			regs->gpr[3] = -EINTR;
236 		} else {
237 			regs->result = -EINTR;
238 			regs->gpr[3] = EINTR;
239 			regs->ccr |= 0x10000000;
240 		}
241 	}
242 }
243 
244 static void do_signal(struct task_struct *tsk)
245 {
246 	sigset_t *oldset = sigmask_to_save();
247 	struct ksignal ksig = { .sig = 0 };
248 	int ret;
249 
250 	BUG_ON(tsk != current);
251 
252 	get_signal(&ksig);
253 
254 	/* Is there any syscall restart business here ? */
255 	check_syscall_restart(tsk->thread.regs, &ksig.ka, ksig.sig > 0);
256 
257 	if (ksig.sig <= 0) {
258 		/* No signal to deliver -- put the saved sigmask back */
259 		restore_saved_sigmask();
260 		set_trap_norestart(tsk->thread.regs);
261 		return;               /* no signals delivered */
262 	}
263 
264         /*
265 	 * Reenable the DABR before delivering the signal to
266 	 * user space. The DABR will have been cleared if it
267 	 * triggered inside the kernel.
268 	 */
269 	if (!IS_ENABLED(CONFIG_PPC_ADV_DEBUG_REGS)) {
270 		int i;
271 
272 		for (i = 0; i < nr_wp_slots(); i++) {
273 			if (tsk->thread.hw_brk[i].address && tsk->thread.hw_brk[i].type)
274 				__set_breakpoint(i, &tsk->thread.hw_brk[i]);
275 		}
276 	}
277 
278 	/* Re-enable the breakpoints for the signal stack */
279 	thread_change_pc(tsk, tsk->thread.regs);
280 
281 	rseq_signal_deliver(&ksig, tsk->thread.regs);
282 
283 	if (is_32bit_task()) {
284         	if (ksig.ka.sa.sa_flags & SA_SIGINFO)
285 			ret = handle_rt_signal32(&ksig, oldset, tsk);
286 		else
287 			ret = handle_signal32(&ksig, oldset, tsk);
288 	} else {
289 		ret = handle_rt_signal64(&ksig, oldset, tsk);
290 	}
291 
292 	set_trap_norestart(tsk->thread.regs);
293 	signal_setup_done(ret, &ksig, test_thread_flag(TIF_SINGLESTEP));
294 }
295 
296 static unsigned long get_tm_stackpointer(struct task_struct *tsk)
297 {
298 	/* When in an active transaction that takes a signal, we need to be
299 	 * careful with the stack.  It's possible that the stack has moved back
300 	 * up after the tbegin.  The obvious case here is when the tbegin is
301 	 * called inside a function that returns before a tend.  In this case,
302 	 * the stack is part of the checkpointed transactional memory state.
303 	 * If we write over this non transactionally or in suspend, we are in
304 	 * trouble because if we get a tm abort, the program counter and stack
305 	 * pointer will be back at the tbegin but our in memory stack won't be
306 	 * valid anymore.
307 	 *
308 	 * To avoid this, when taking a signal in an active transaction, we
309 	 * need to use the stack pointer from the checkpointed state, rather
310 	 * than the speculated state.  This ensures that the signal context
311 	 * (written tm suspended) will be written below the stack required for
312 	 * the rollback.  The transaction is aborted because of the treclaim,
313 	 * so any memory written between the tbegin and the signal will be
314 	 * rolled back anyway.
315 	 *
316 	 * For signals taken in non-TM or suspended mode, we use the
317 	 * normal/non-checkpointed stack pointer.
318 	 */
319 	struct pt_regs *regs = tsk->thread.regs;
320 	unsigned long ret = regs->gpr[1];
321 
322 #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
323 	BUG_ON(tsk != current);
324 
325 	if (MSR_TM_ACTIVE(regs->msr)) {
326 		preempt_disable();
327 		tm_reclaim_current(TM_CAUSE_SIGNAL);
328 		if (MSR_TM_TRANSACTIONAL(regs->msr))
329 			ret = tsk->thread.ckpt_regs.gpr[1];
330 
331 		/*
332 		 * If we treclaim, we must clear the current thread's TM bits
333 		 * before re-enabling preemption. Otherwise we might be
334 		 * preempted and have the live MSR[TS] changed behind our back
335 		 * (tm_recheckpoint_new_task() would recheckpoint). Besides, we
336 		 * enter the signal handler in non-transactional state.
337 		 */
338 		regs_set_return_msr(regs, regs->msr & ~MSR_TS_MASK);
339 		preempt_enable();
340 	}
341 #endif
342 	return ret;
343 }
344 
345 static const char fm32[] = KERN_INFO "%s[%d]: bad frame in %s: %p nip %08lx lr %08lx\n";
346 static const char fm64[] = KERN_INFO "%s[%d]: bad frame in %s: %p nip %016lx lr %016lx\n";
347 
348 void signal_fault(struct task_struct *tsk, struct pt_regs *regs,
349 		  const char *where, void __user *ptr)
350 {
351 	if (show_unhandled_signals)
352 		printk_ratelimited(regs->msr & MSR_64BIT ? fm64 : fm32, tsk->comm,
353 				   task_pid_nr(tsk), where, ptr, regs->nip, regs->link);
354 }
355 
356 void arch_do_signal_or_restart(struct pt_regs *regs)
357 {
358 	BUG_ON(regs != current->thread.regs);
359 	regs->exit_flags |= _TIF_RESTOREALL;
360 	do_signal(current);
361 }
362