2 * Common signal handling code for both 32 and 64 bits
4 * Copyright (c) 2007 Benjamin Herrenschmidt, IBM Corporation
5 * Extracted from signal_32.c and signal_64.c
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.
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>
29 unsigned long copy_fpr_to_user(void __user *to,
30 struct task_struct *task)
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));
42 unsigned long copy_fpr_from_user(struct task_struct *task,
48 if (__copy_from_user(buf, from, ELF_NFPREG * sizeof(double)))
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];
57 unsigned long copy_vsx_to_user(void __user *to,
58 struct task_struct *task)
60 u64 buf[ELF_NVSRHALFREG];
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));
69 unsigned long copy_vsx_from_user(struct task_struct *task,
72 u64 buf[ELF_NVSRHALFREG];
75 if (__copy_from_user(buf, from, ELF_NVSRHALFREG * sizeof(double)))
77 for (i = 0; i < ELF_NVSRHALFREG ; i++)
78 task->thread.fp_state.fpr[i][TS_VSRLOWOFFSET] = buf[i];
82 #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
83 unsigned long copy_ckfpr_to_user(void __user *to,
84 struct task_struct *task)
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));
96 unsigned long copy_ckfpr_from_user(struct task_struct *task,
102 if (__copy_from_user(buf, from, ELF_NFPREG * sizeof(double)))
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];
111 unsigned long copy_ckvsx_to_user(void __user *to,
112 struct task_struct *task)
114 u64 buf[ELF_NVSRHALFREG];
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));
123 unsigned long copy_ckvsx_from_user(struct task_struct *task,
126 u64 buf[ELF_NVSRHALFREG];
129 if (__copy_from_user(buf, from, ELF_NVSRHALFREG * sizeof(double)))
131 for (i = 0; i < ELF_NVSRHALFREG ; i++)
132 task->thread.ckfp_state.fpr[i][TS_VSRLOWOFFSET] = buf[i];
135 #endif /* CONFIG_PPC_TRANSACTIONAL_MEM */
137 inline unsigned long copy_fpr_to_user(void __user *to,
138 struct task_struct *task)
140 return __copy_to_user(to, task->thread.fp_state.fpr,
141 ELF_NFPREG * sizeof(double));
144 inline unsigned long copy_fpr_from_user(struct task_struct *task,
147 return __copy_from_user(task->thread.fp_state.fpr, from,
148 ELF_NFPREG * sizeof(double));
151 #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
152 inline unsigned long copy_ckfpr_to_user(void __user *to,
153 struct task_struct *task)
155 return __copy_to_user(to, task->thread.ckfp_state.fpr,
156 ELF_NFPREG * sizeof(double));
159 inline unsigned long copy_ckfpr_from_user(struct task_struct *task,
162 return __copy_from_user(task->thread.ckfp_state.fpr, from,
163 ELF_NFPREG * sizeof(double));
165 #endif /* CONFIG_PPC_TRANSACTIONAL_MEM */
168 /* Log an error when sending an unhandled signal to a process. Controlled
169 * through debug.exception-trace sysctl.
172 int show_unhandled_signals = 1;
175 * Allocate space for the signal frame
177 void __user *get_sigframe(struct ksignal *ksig, unsigned long sp,
178 size_t frame_size, int is_32)
180 unsigned long oldsp, newsp;
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;
188 if (!access_ok((void __user *)newsp, oldsp - newsp))
191 return (void __user *)newsp;
194 static void check_syscall_restart(struct pt_regs *regs, struct k_sigaction *ka,
197 unsigned long ret = regs->gpr[3];
201 if (!trap_is_syscall(regs))
204 if (trap_norestart(regs))
207 /* error signalled ? */
208 if (trap_is_scv(regs)) {
209 /* 32-bit compat mode sign extend? */
210 if (!IS_ERR_VALUE(ret))
213 } else if (!(regs->ccr & 0x10000000)) {
218 case ERESTART_RESTARTBLOCK:
220 /* ERESTARTNOHAND means that the syscall should only be
221 * restarted if there was no handler for the signal, and since
222 * we only get here if there is a handler, we dont restart.
224 restart = !has_handler;
227 /* ERESTARTSYS means to restart the syscall if there is no
228 * handler or the handler was registered with SA_RESTART
230 restart = !has_handler || (ka->sa.sa_flags & SA_RESTART) != 0;
233 /* ERESTARTNOINTR means that the syscall should be
234 * called again after the signal handler returns.
241 if (ret == ERESTART_RESTARTBLOCK)
242 regs->gpr[0] = __NR_restart_syscall;
244 regs->gpr[3] = regs->orig_gpr3;
248 if (trap_is_scv(regs)) {
249 regs->result = -EINTR;
250 regs->gpr[3] = -EINTR;
252 regs->result = -EINTR;
253 regs->gpr[3] = EINTR;
254 regs->ccr |= 0x10000000;
259 static void do_signal(struct task_struct *tsk)
261 sigset_t *oldset = sigmask_to_save();
262 struct ksignal ksig = { .sig = 0 };
265 BUG_ON(tsk != current);
269 /* Is there any syscall restart business here ? */
270 check_syscall_restart(tsk->thread.regs, &ksig.ka, ksig.sig > 0);
273 /* No signal to deliver -- put the saved sigmask back */
274 restore_saved_sigmask();
275 set_trap_norestart(tsk->thread.regs);
276 return; /* no signals delivered */
280 * Reenable the DABR before delivering the signal to
281 * user space. The DABR will have been cleared if it
282 * triggered inside the kernel.
284 if (!IS_ENABLED(CONFIG_PPC_ADV_DEBUG_REGS)) {
287 for (i = 0; i < nr_wp_slots(); i++) {
288 if (tsk->thread.hw_brk[i].address && tsk->thread.hw_brk[i].type)
289 __set_breakpoint(i, &tsk->thread.hw_brk[i]);
293 /* Re-enable the breakpoints for the signal stack */
294 thread_change_pc(tsk, tsk->thread.regs);
296 rseq_signal_deliver(&ksig, tsk->thread.regs);
298 if (is_32bit_task()) {
299 if (ksig.ka.sa.sa_flags & SA_SIGINFO)
300 ret = handle_rt_signal32(&ksig, oldset, tsk);
302 ret = handle_signal32(&ksig, oldset, tsk);
304 ret = handle_rt_signal64(&ksig, oldset, tsk);
307 set_trap_norestart(tsk->thread.regs);
308 signal_setup_done(ret, &ksig, test_thread_flag(TIF_SINGLESTEP));
311 void do_notify_resume(struct pt_regs *regs, unsigned long thread_info_flags)
315 if (thread_info_flags & _TIF_UPROBE)
316 uprobe_notify_resume(regs);
318 if (thread_info_flags & _TIF_PATCH_PENDING)
319 klp_update_patch_state(current);
321 if (thread_info_flags & (_TIF_SIGPENDING | _TIF_NOTIFY_SIGNAL)) {
322 BUG_ON(regs != current->thread.regs);
326 if (thread_info_flags & _TIF_NOTIFY_RESUME) {
327 tracehook_notify_resume(regs);
328 rseq_handle_notify_resume(NULL, regs);
334 unsigned long get_tm_stackpointer(struct task_struct *tsk)
336 /* When in an active transaction that takes a signal, we need to be
337 * careful with the stack. It's possible that the stack has moved back
338 * up after the tbegin. The obvious case here is when the tbegin is
339 * called inside a function that returns before a tend. In this case,
340 * the stack is part of the checkpointed transactional memory state.
341 * If we write over this non transactionally or in suspend, we are in
342 * trouble because if we get a tm abort, the program counter and stack
343 * pointer will be back at the tbegin but our in memory stack won't be
346 * To avoid this, when taking a signal in an active transaction, we
347 * need to use the stack pointer from the checkpointed state, rather
348 * than the speculated state. This ensures that the signal context
349 * (written tm suspended) will be written below the stack required for
350 * the rollback. The transaction is aborted because of the treclaim,
351 * so any memory written between the tbegin and the signal will be
352 * rolled back anyway.
354 * For signals taken in non-TM or suspended mode, we use the
355 * normal/non-checkpointed stack pointer.
358 unsigned long ret = tsk->thread.regs->gpr[1];
360 #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
361 BUG_ON(tsk != current);
363 if (MSR_TM_ACTIVE(tsk->thread.regs->msr)) {
365 tm_reclaim_current(TM_CAUSE_SIGNAL);
366 if (MSR_TM_TRANSACTIONAL(tsk->thread.regs->msr))
367 ret = tsk->thread.ckpt_regs.gpr[1];
370 * If we treclaim, we must clear the current thread's TM bits
371 * before re-enabling preemption. Otherwise we might be
372 * preempted and have the live MSR[TS] changed behind our back
373 * (tm_recheckpoint_new_task() would recheckpoint). Besides, we
374 * enter the signal handler in non-transactional state.
376 tsk->thread.regs->msr &= ~MSR_TS_MASK;