1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18#include <linux/sched.h>
19#include <linux/mm.h>
20#include <linux/smp.h>
21#include <linux/kernel.h>
22#include <linux/signal.h>
23#include <linux/errno.h>
24#include <linux/wait.h>
25#include <linux/ptrace.h>
26#include <linux/unistd.h>
27#include <linux/stddef.h>
28#include <linux/tracehook.h>
29
30#include <asm/processor.h>
31#include <asm/ucontext.h>
32#include <asm/uaccess.h>
33
34#define DEBUG_SIG 0
35
36struct rt_sigframe {
37 struct siginfo *pinfo;
38 void *puc;
39 struct siginfo info;
40 struct ucontext uc;
41 unsigned char retcode[16];
42};
43
44static int restore_sigcontext(struct pt_regs *regs, struct sigcontext *sc)
45{
46 unsigned int err = 0;
47
48
49 current_thread_info()->restart_block.fn = do_no_restart_syscall;
50
51
52
53
54
55
56 if (__copy_from_user(regs, sc->regs.gpr, 32 * sizeof(unsigned long)))
57 goto badframe;
58 if (__copy_from_user(®s->pc, &sc->regs.pc, sizeof(unsigned long)))
59 goto badframe;
60 if (__copy_from_user(®s->sr, &sc->regs.sr, sizeof(unsigned long)))
61 goto badframe;
62
63
64 regs->sr &= ~SPR_SR_SM;
65
66
67
68
69
70
71 return err;
72
73badframe:
74 return 1;
75}
76
77asmlinkage long _sys_rt_sigreturn(struct pt_regs *regs)
78{
79 struct rt_sigframe *frame = (struct rt_sigframe __user *)regs->sp;
80 sigset_t set;
81
82
83
84
85
86
87 if (((long)frame) & 3)
88 goto badframe;
89
90 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
91 goto badframe;
92 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
93 goto badframe;
94
95 set_current_blocked(&set);
96
97 if (restore_sigcontext(regs, &frame->uc.uc_mcontext))
98 goto badframe;
99
100 if (restore_altstack(&frame->uc.uc_stack))
101 goto badframe;
102
103 return regs->gpr[11];
104
105badframe:
106 force_sig(SIGSEGV, current);
107 return 0;
108}
109
110
111
112
113
114static int setup_sigcontext(struct sigcontext *sc, struct pt_regs *regs,
115 unsigned long mask)
116{
117 int err = 0;
118
119
120
121 err |= __copy_to_user(sc->regs.gpr, regs, 32 * sizeof(unsigned long));
122 err |= __copy_to_user(&sc->regs.pc, ®s->pc, sizeof(unsigned long));
123 err |= __copy_to_user(&sc->regs.sr, ®s->sr, sizeof(unsigned long));
124
125
126
127 err |= __put_user(mask, &sc->oldmask);
128
129 return err;
130}
131
132static inline unsigned long align_sigframe(unsigned long sp)
133{
134 return sp & ~3UL;
135}
136
137
138
139
140
141
142static inline void __user *get_sigframe(struct k_sigaction *ka,
143 struct pt_regs *regs, size_t frame_size)
144{
145 unsigned long sp = regs->sp;
146 int onsigstack = on_sig_stack(sp);
147
148
149 sp -= STACK_FRAME_OVERHEAD;
150
151
152 if ((ka->sa.sa_flags & SA_ONSTACK) && !onsigstack) {
153 if (current->sas_ss_size)
154 sp = current->sas_ss_sp + current->sas_ss_size;
155 }
156
157 sp = align_sigframe(sp - frame_size);
158
159
160
161
162
163 if (onsigstack && !likely(on_sig_stack(sp)))
164 return (void __user *)-1L;
165
166 return (void __user *)sp;
167}
168
169
170
171
172
173
174
175
176static int setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
177 sigset_t *set, struct pt_regs *regs)
178{
179 struct rt_sigframe *frame;
180 unsigned long return_ip;
181 int err = 0;
182
183 frame = get_sigframe(ka, regs, sizeof(*frame));
184
185 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
186 goto give_sigsegv;
187
188 err |= __put_user(&frame->info, &frame->pinfo);
189 err |= __put_user(&frame->uc, &frame->puc);
190
191 if (ka->sa.sa_flags & SA_SIGINFO)
192 err |= copy_siginfo_to_user(&frame->info, info);
193 if (err)
194 goto give_sigsegv;
195
196
197 err |= __clear_user(&frame->uc, offsetof(struct ucontext, uc_mcontext));
198 err |= __put_user(0, &frame->uc.uc_flags);
199 err |= __put_user(NULL, &frame->uc.uc_link);
200 err |= __save_altstack(&frame->uc.uc_stack, regs->sp);
201 err |= setup_sigcontext(&frame->uc.uc_mcontext, regs, set->sig[0]);
202
203 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
204
205 if (err)
206 goto give_sigsegv;
207
208
209 return_ip = (unsigned long)&frame->retcode;
210
211 err |= __put_user(0xa960, (short *)(frame->retcode + 0));
212 err |= __put_user(__NR_rt_sigreturn, (short *)(frame->retcode + 2));
213 err |= __put_user(0x20000001, (unsigned long *)(frame->retcode + 4));
214 err |= __put_user(0x15000000, (unsigned long *)(frame->retcode + 8));
215
216 if (err)
217 goto give_sigsegv;
218
219
220
221
222 regs->pc = (unsigned long)ka->sa.sa_handler;
223 regs->gpr[9] = (unsigned long)return_ip;
224 regs->gpr[3] = (unsigned long)sig;
225 regs->gpr[4] = (unsigned long)&frame->info;
226 regs->gpr[5] = (unsigned long)&frame->uc;
227
228
229 regs->sp = (unsigned long)frame;
230
231 return 0;
232
233give_sigsegv:
234 force_sigsegv(sig, current);
235 return -EFAULT;
236}
237
238static inline void
239handle_signal(unsigned long sig,
240 siginfo_t *info, struct k_sigaction *ka,
241 struct pt_regs *regs)
242{
243 int ret;
244
245 ret = setup_rt_frame(sig, ka, info, sigmask_to_save(), regs);
246 if (ret)
247 return;
248
249 signal_delivered(sig, info, ka, regs,
250 test_thread_flag(TIF_SINGLESTEP));
251}
252
253
254
255
256
257
258
259
260
261
262
263
264
265void do_signal(struct pt_regs *regs)
266{
267 siginfo_t info;
268 int signr;
269 struct k_sigaction ka;
270
271
272
273
274
275
276
277 if (!user_mode(regs))
278 return;
279
280 signr = get_signal_to_deliver(&info, &ka, regs, NULL);
281
282
283
284
285
286
287
288
289
290
291 if (regs->orig_gpr11) {
292 int restart = 0;
293
294 switch (regs->gpr[11]) {
295 case -ERESTART_RESTARTBLOCK:
296 case -ERESTARTNOHAND:
297
298 restart = (signr <= 0);
299 break;
300 case -ERESTARTSYS:
301
302
303 restart = (signr <= 0 || (ka.sa.sa_flags & SA_RESTART));
304 break;
305 case -ERESTARTNOINTR:
306
307 restart = 1;
308 break;
309 }
310
311 if (restart) {
312 if (regs->gpr[11] == -ERESTART_RESTARTBLOCK)
313 regs->gpr[11] = __NR_restart_syscall;
314 else
315 regs->gpr[11] = regs->orig_gpr11;
316 regs->pc -= 4;
317 } else {
318 regs->gpr[11] = -EINTR;
319 }
320 }
321
322 if (signr <= 0) {
323
324
325 restore_saved_sigmask();
326 } else {
327
328 handle_signal(signr, &info, &ka, regs);
329 }
330
331 return;
332}
333
334asmlinkage void do_notify_resume(struct pt_regs *regs)
335{
336 if (current_thread_info()->flags & _TIF_SIGPENDING)
337 do_signal(regs);
338
339 if (current_thread_info()->flags & _TIF_NOTIFY_RESUME) {
340 clear_thread_flag(TIF_NOTIFY_RESUME);
341 tracehook_notify_resume(regs);
342 }
343}
344