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