Loading...
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}
1/*
2 * Common signal handling code for both 32 and 64 bits
3 *
4 * Copyright (c) 2007 Benjamin Herrenschmidt, IBM Coproration
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 <asm/hw_breakpoint.h>
15#include <asm/uaccess.h>
16#include <asm/unistd.h>
17
18#include "signal.h"
19
20/* Log an error when sending an unhandled signal to a process. Controlled
21 * through debug.exception-trace sysctl.
22 */
23
24int show_unhandled_signals = 0;
25
26/*
27 * Allocate space for the signal frame
28 */
29void __user * get_sigframe(struct k_sigaction *ka, struct pt_regs *regs,
30 size_t frame_size, int is_32)
31{
32 unsigned long oldsp, newsp;
33
34 /* Default to using normal stack */
35 oldsp = get_clean_sp(regs, is_32);
36
37 /* Check for alt stack */
38 if ((ka->sa.sa_flags & SA_ONSTACK) &&
39 current->sas_ss_size && !on_sig_stack(oldsp))
40 oldsp = (current->sas_ss_sp + current->sas_ss_size);
41
42 /* Get aligned frame */
43 newsp = (oldsp - frame_size) & ~0xFUL;
44
45 /* Check access */
46 if (!access_ok(VERIFY_WRITE, (void __user *)newsp, oldsp - newsp))
47 return NULL;
48
49 return (void __user *)newsp;
50}
51
52
53/*
54 * Restore the user process's signal mask
55 */
56void restore_sigmask(sigset_t *set)
57{
58 sigdelsetmask(set, ~_BLOCKABLE);
59 spin_lock_irq(¤t->sighand->siglock);
60 current->blocked = *set;
61 recalc_sigpending();
62 spin_unlock_irq(¤t->sighand->siglock);
63}
64
65static void check_syscall_restart(struct pt_regs *regs, struct k_sigaction *ka,
66 int has_handler)
67{
68 unsigned long ret = regs->gpr[3];
69 int restart = 1;
70
71 /* syscall ? */
72 if (TRAP(regs) != 0x0C00)
73 return;
74
75 /* error signalled ? */
76 if (!(regs->ccr & 0x10000000))
77 return;
78
79 switch (ret) {
80 case ERESTART_RESTARTBLOCK:
81 case ERESTARTNOHAND:
82 /* ERESTARTNOHAND means that the syscall should only be
83 * restarted if there was no handler for the signal, and since
84 * we only get here if there is a handler, we dont restart.
85 */
86 restart = !has_handler;
87 break;
88 case ERESTARTSYS:
89 /* ERESTARTSYS means to restart the syscall if there is no
90 * handler or the handler was registered with SA_RESTART
91 */
92 restart = !has_handler || (ka->sa.sa_flags & SA_RESTART) != 0;
93 break;
94 case ERESTARTNOINTR:
95 /* ERESTARTNOINTR means that the syscall should be
96 * called again after the signal handler returns.
97 */
98 break;
99 default:
100 return;
101 }
102 if (restart) {
103 if (ret == ERESTART_RESTARTBLOCK)
104 regs->gpr[0] = __NR_restart_syscall;
105 else
106 regs->gpr[3] = regs->orig_gpr3;
107 regs->nip -= 4;
108 regs->result = 0;
109 } else {
110 regs->result = -EINTR;
111 regs->gpr[3] = EINTR;
112 regs->ccr |= 0x10000000;
113 }
114}
115
116static int do_signal_pending(sigset_t *oldset, struct pt_regs *regs)
117{
118 siginfo_t info;
119 int signr;
120 struct k_sigaction ka;
121 int ret;
122 int is32 = is_32bit_task();
123
124 if (current_thread_info()->local_flags & _TLF_RESTORE_SIGMASK)
125 oldset = ¤t->saved_sigmask;
126 else if (!oldset)
127 oldset = ¤t->blocked;
128
129 signr = get_signal_to_deliver(&info, &ka, regs, NULL);
130
131 /* Is there any syscall restart business here ? */
132 check_syscall_restart(regs, &ka, signr > 0);
133
134 if (signr <= 0) {
135 struct thread_info *ti = current_thread_info();
136 /* No signal to deliver -- put the saved sigmask back */
137 if (ti->local_flags & _TLF_RESTORE_SIGMASK) {
138 ti->local_flags &= ~_TLF_RESTORE_SIGMASK;
139 sigprocmask(SIG_SETMASK, ¤t->saved_sigmask, NULL);
140 }
141 regs->trap = 0;
142 return 0; /* no signals delivered */
143 }
144
145#ifndef CONFIG_PPC_ADV_DEBUG_REGS
146 /*
147 * Reenable the DABR before delivering the signal to
148 * user space. The DABR will have been cleared if it
149 * triggered inside the kernel.
150 */
151 if (current->thread.dabr)
152 set_dabr(current->thread.dabr);
153#endif
154 /* Re-enable the breakpoints for the signal stack */
155 thread_change_pc(current, regs);
156
157 if (is32) {
158 if (ka.sa.sa_flags & SA_SIGINFO)
159 ret = handle_rt_signal32(signr, &ka, &info, oldset,
160 regs);
161 else
162 ret = handle_signal32(signr, &ka, &info, oldset,
163 regs);
164 } else {
165 ret = handle_rt_signal64(signr, &ka, &info, oldset, regs);
166 }
167
168 regs->trap = 0;
169 if (ret) {
170 spin_lock_irq(¤t->sighand->siglock);
171 sigorsets(¤t->blocked, ¤t->blocked,
172 &ka.sa.sa_mask);
173 if (!(ka.sa.sa_flags & SA_NODEFER))
174 sigaddset(¤t->blocked, signr);
175 recalc_sigpending();
176 spin_unlock_irq(¤t->sighand->siglock);
177
178 /*
179 * A signal was successfully delivered; the saved sigmask is in
180 * its frame, and we can clear the TLF_RESTORE_SIGMASK flag.
181 */
182 current_thread_info()->local_flags &= ~_TLF_RESTORE_SIGMASK;
183
184 /*
185 * Let tracing know that we've done the handler setup.
186 */
187 tracehook_signal_handler(signr, &info, &ka, regs,
188 test_thread_flag(TIF_SINGLESTEP));
189 }
190
191 return ret;
192}
193
194void do_signal(struct pt_regs *regs, unsigned long thread_info_flags)
195{
196 if (thread_info_flags & _TIF_SIGPENDING)
197 do_signal_pending(NULL, regs);
198
199 if (thread_info_flags & _TIF_NOTIFY_RESUME) {
200 clear_thread_flag(TIF_NOTIFY_RESUME);
201 tracehook_notify_resume(regs);
202 }
203}
204
205long sys_sigaltstack(const stack_t __user *uss, stack_t __user *uoss,
206 unsigned long r5, unsigned long r6, unsigned long r7,
207 unsigned long r8, struct pt_regs *regs)
208{
209 return do_sigaltstack(uss, uoss, regs->gpr[1]);
210}