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