1
2
3
4
5
6
7
8
9
10#include <linux/sched.h>
11#include <linux/kernel.h>
12#include <linux/signal.h>
13#include <linux/errno.h>
14#include <linux/wait.h>
15#include <linux/ptrace.h>
16#include <linux/unistd.h>
17#include <linux/mm.h>
18#include <linux/tty.h>
19#include <linux/smp.h>
20#include <linux/binfmts.h>
21#include <linux/bitops.h>
22#include <linux/tracehook.h>
23
24#include <linux/uaccess.h>
25#include <asm/ptrace.h>
26#include <asm/pgalloc.h>
27#include <asm/pgtable.h>
28#include <asm/cacheflush.h>
29#include <asm/switch_to.h>
30
31#include "sigutil.h"
32#include "kernel.h"
33
34extern void fpsave(unsigned long *fpregs, unsigned long *fsr,
35 void *fpqueue, unsigned long *fpqdepth);
36extern void fpload(unsigned long *fpregs, unsigned long *fsr);
37
38struct signal_frame {
39 struct sparc_stackf ss;
40 __siginfo32_t info;
41 __siginfo_fpu_t __user *fpu_save;
42 unsigned long insns[2] __attribute__ ((aligned (8)));
43 unsigned int extramask[_NSIG_WORDS - 1];
44 unsigned int extra_size;
45 __siginfo_rwin_t __user *rwin_save;
46} __attribute__((aligned(8)));
47
48struct rt_signal_frame {
49 struct sparc_stackf ss;
50 siginfo_t info;
51 struct pt_regs regs;
52 sigset_t mask;
53 __siginfo_fpu_t __user *fpu_save;
54 unsigned int insns[2];
55 stack_t stack;
56 unsigned int extra_size;
57 __siginfo_rwin_t __user *rwin_save;
58} __attribute__((aligned(8)));
59
60
61#define SF_ALIGNEDSZ (((sizeof(struct signal_frame) + 7) & (~7)))
62#define RT_ALIGNEDSZ (((sizeof(struct rt_signal_frame) + 7) & (~7)))
63
64
65
66
67
68static inline bool invalid_frame_pointer(void __user *fp, int fplen)
69{
70 if ((((unsigned long) fp) & 15) || !__access_ok((unsigned long)fp, fplen))
71 return true;
72
73 return false;
74}
75
76asmlinkage void do_sigreturn(struct pt_regs *regs)
77{
78 unsigned long up_psr, pc, npc, ufp;
79 struct signal_frame __user *sf;
80 sigset_t set;
81 __siginfo_fpu_t __user *fpu_save;
82 __siginfo_rwin_t __user *rwin_save;
83 int err;
84
85
86 current->restart_block.fn = do_no_restart_syscall;
87
88 synchronize_user_stack();
89
90 sf = (struct signal_frame __user *) regs->u_regs[UREG_FP];
91
92
93 if (invalid_frame_pointer(sf, sizeof(*sf)))
94 goto segv_and_exit;
95
96 if (get_user(ufp, &sf->info.si_regs.u_regs[UREG_FP]))
97 goto segv_and_exit;
98
99 if (ufp & 0x7)
100 goto segv_and_exit;
101
102 err = __get_user(pc, &sf->info.si_regs.pc);
103 err |= __get_user(npc, &sf->info.si_regs.npc);
104
105 if ((pc | npc) & 3)
106 goto segv_and_exit;
107
108
109 up_psr = regs->psr;
110 err |= __copy_from_user(regs, &sf->info.si_regs, sizeof(struct pt_regs));
111
112
113 regs->psr = (up_psr & ~(PSR_ICC | PSR_EF))
114 | (regs->psr & (PSR_ICC | PSR_EF));
115
116
117 pt_regs_clear_syscall(regs);
118
119 err |= __get_user(fpu_save, &sf->fpu_save);
120 if (fpu_save)
121 err |= restore_fpu_state(regs, fpu_save);
122 err |= __get_user(rwin_save, &sf->rwin_save);
123 if (rwin_save)
124 err |= restore_rwin_state(rwin_save);
125
126
127
128
129 err |= __get_user(set.sig[0], &sf->info.si_mask);
130 err |= __copy_from_user(&set.sig[1], &sf->extramask,
131 (_NSIG_WORDS-1) * sizeof(unsigned int));
132
133 if (err)
134 goto segv_and_exit;
135
136 set_current_blocked(&set);
137 return;
138
139segv_and_exit:
140 force_sig(SIGSEGV);
141}
142
143asmlinkage void do_rt_sigreturn(struct pt_regs *regs)
144{
145 struct rt_signal_frame __user *sf;
146 unsigned int psr, pc, npc, ufp;
147 __siginfo_fpu_t __user *fpu_save;
148 __siginfo_rwin_t __user *rwin_save;
149 sigset_t set;
150 int err;
151
152 synchronize_user_stack();
153 sf = (struct rt_signal_frame __user *) regs->u_regs[UREG_FP];
154 if (invalid_frame_pointer(sf, sizeof(*sf)))
155 goto segv;
156
157 if (get_user(ufp, &sf->regs.u_regs[UREG_FP]))
158 goto segv;
159
160 if (ufp & 0x7)
161 goto segv;
162
163 err = __get_user(pc, &sf->regs.pc);
164 err |= __get_user(npc, &sf->regs.npc);
165 err |= ((pc | npc) & 0x03);
166
167 err |= __get_user(regs->y, &sf->regs.y);
168 err |= __get_user(psr, &sf->regs.psr);
169
170 err |= __copy_from_user(®s->u_regs[UREG_G1],
171 &sf->regs.u_regs[UREG_G1], 15 * sizeof(u32));
172
173 regs->psr = (regs->psr & ~PSR_ICC) | (psr & PSR_ICC);
174
175
176 pt_regs_clear_syscall(regs);
177
178 err |= __get_user(fpu_save, &sf->fpu_save);
179 if (!err && fpu_save)
180 err |= restore_fpu_state(regs, fpu_save);
181 err |= __copy_from_user(&set, &sf->mask, sizeof(sigset_t));
182 err |= restore_altstack(&sf->stack);
183
184 if (err)
185 goto segv;
186
187 regs->pc = pc;
188 regs->npc = npc;
189
190 err |= __get_user(rwin_save, &sf->rwin_save);
191 if (!err && rwin_save) {
192 if (restore_rwin_state(rwin_save))
193 goto segv;
194 }
195
196 set_current_blocked(&set);
197 return;
198segv:
199 force_sig(SIGSEGV);
200}
201
202static inline void __user *get_sigframe(struct ksignal *ksig, struct pt_regs *regs, unsigned long framesize)
203{
204 unsigned long sp = regs->u_regs[UREG_FP];
205
206
207
208
209
210 if (on_sig_stack(sp) && !likely(on_sig_stack(sp - framesize)))
211 return (void __user *) -1L;
212
213
214 sp = sigsp(sp, ksig) - framesize;
215
216
217
218
219
220
221
222 sp &= ~15UL;
223
224 return (void __user *) sp;
225}
226
227static int setup_frame(struct ksignal *ksig, struct pt_regs *regs,
228 sigset_t *oldset)
229{
230 struct signal_frame __user *sf;
231 int sigframe_size, err, wsaved;
232 void __user *tail;
233
234
235 synchronize_user_stack();
236
237 wsaved = current_thread_info()->w_saved;
238
239 sigframe_size = sizeof(*sf);
240 if (used_math())
241 sigframe_size += sizeof(__siginfo_fpu_t);
242 if (wsaved)
243 sigframe_size += sizeof(__siginfo_rwin_t);
244
245 sf = (struct signal_frame __user *)
246 get_sigframe(ksig, regs, sigframe_size);
247
248 if (invalid_frame_pointer(sf, sigframe_size)) {
249 do_exit(SIGILL);
250 return -EINVAL;
251 }
252
253 tail = sf + 1;
254
255
256 err = __copy_to_user(&sf->info.si_regs, regs, sizeof(struct pt_regs));
257
258 err |= __put_user(0, &sf->extra_size);
259
260 if (used_math()) {
261 __siginfo_fpu_t __user *fp = tail;
262 tail += sizeof(*fp);
263 err |= save_fpu_state(regs, fp);
264 err |= __put_user(fp, &sf->fpu_save);
265 } else {
266 err |= __put_user(0, &sf->fpu_save);
267 }
268 if (wsaved) {
269 __siginfo_rwin_t __user *rwp = tail;
270 tail += sizeof(*rwp);
271 err |= save_rwin_state(wsaved, rwp);
272 err |= __put_user(rwp, &sf->rwin_save);
273 } else {
274 err |= __put_user(0, &sf->rwin_save);
275 }
276
277 err |= __put_user(oldset->sig[0], &sf->info.si_mask);
278 err |= __copy_to_user(sf->extramask, &oldset->sig[1],
279 (_NSIG_WORDS - 1) * sizeof(unsigned int));
280 if (!wsaved) {
281 err |= __copy_to_user(sf, (char *) regs->u_regs[UREG_FP],
282 sizeof(struct reg_window32));
283 } else {
284 struct reg_window32 *rp;
285
286 rp = ¤t_thread_info()->reg_window[wsaved - 1];
287 err |= __copy_to_user(sf, rp, sizeof(struct reg_window32));
288 }
289 if (err)
290 return err;
291
292
293 regs->u_regs[UREG_FP] = (unsigned long) sf;
294 regs->u_regs[UREG_I0] = ksig->sig;
295 regs->u_regs[UREG_I1] = (unsigned long) &sf->info;
296 regs->u_regs[UREG_I2] = (unsigned long) &sf->info;
297
298
299 regs->pc = (unsigned long) ksig->ka.sa.sa_handler;
300 regs->npc = (regs->pc + 4);
301
302
303 if (ksig->ka.ka_restorer)
304 regs->u_regs[UREG_I7] = (unsigned long)ksig->ka.ka_restorer;
305 else {
306 regs->u_regs[UREG_I7] = (unsigned long)(&(sf->insns[0]) - 2);
307
308
309 err |= __put_user(0x821020d8, &sf->insns[0]);
310
311
312 err |= __put_user(0x91d02010, &sf->insns[1]);
313 if (err)
314 return err;
315
316
317 flush_sig_insns(current->mm, (unsigned long) &(sf->insns[0]));
318 }
319 return 0;
320}
321
322static int setup_rt_frame(struct ksignal *ksig, struct pt_regs *regs,
323 sigset_t *oldset)
324{
325 struct rt_signal_frame __user *sf;
326 int sigframe_size, wsaved;
327 void __user *tail;
328 unsigned int psr;
329 int err;
330
331 synchronize_user_stack();
332 wsaved = current_thread_info()->w_saved;
333 sigframe_size = sizeof(*sf);
334 if (used_math())
335 sigframe_size += sizeof(__siginfo_fpu_t);
336 if (wsaved)
337 sigframe_size += sizeof(__siginfo_rwin_t);
338 sf = (struct rt_signal_frame __user *)
339 get_sigframe(ksig, regs, sigframe_size);
340 if (invalid_frame_pointer(sf, sigframe_size)) {
341 do_exit(SIGILL);
342 return -EINVAL;
343 }
344
345 tail = sf + 1;
346 err = __put_user(regs->pc, &sf->regs.pc);
347 err |= __put_user(regs->npc, &sf->regs.npc);
348 err |= __put_user(regs->y, &sf->regs.y);
349 psr = regs->psr;
350 if (used_math())
351 psr |= PSR_EF;
352 err |= __put_user(psr, &sf->regs.psr);
353 err |= __copy_to_user(&sf->regs.u_regs, regs->u_regs, sizeof(regs->u_regs));
354 err |= __put_user(0, &sf->extra_size);
355
356 if (psr & PSR_EF) {
357 __siginfo_fpu_t __user *fp = tail;
358 tail += sizeof(*fp);
359 err |= save_fpu_state(regs, fp);
360 err |= __put_user(fp, &sf->fpu_save);
361 } else {
362 err |= __put_user(0, &sf->fpu_save);
363 }
364 if (wsaved) {
365 __siginfo_rwin_t __user *rwp = tail;
366 tail += sizeof(*rwp);
367 err |= save_rwin_state(wsaved, rwp);
368 err |= __put_user(rwp, &sf->rwin_save);
369 } else {
370 err |= __put_user(0, &sf->rwin_save);
371 }
372 err |= __copy_to_user(&sf->mask, &oldset->sig[0], sizeof(sigset_t));
373
374
375 err |= __save_altstack(&sf->stack, regs->u_regs[UREG_FP]);
376
377 if (!wsaved) {
378 err |= __copy_to_user(sf, (char *) regs->u_regs[UREG_FP],
379 sizeof(struct reg_window32));
380 } else {
381 struct reg_window32 *rp;
382
383 rp = ¤t_thread_info()->reg_window[wsaved - 1];
384 err |= __copy_to_user(sf, rp, sizeof(struct reg_window32));
385 }
386
387 err |= copy_siginfo_to_user(&sf->info, &ksig->info);
388
389 if (err)
390 return err;
391
392 regs->u_regs[UREG_FP] = (unsigned long) sf;
393 regs->u_regs[UREG_I0] = ksig->sig;
394 regs->u_regs[UREG_I1] = (unsigned long) &sf->info;
395 regs->u_regs[UREG_I2] = (unsigned long) &sf->regs;
396
397 regs->pc = (unsigned long) ksig->ka.sa.sa_handler;
398 regs->npc = (regs->pc + 4);
399
400 if (ksig->ka.ka_restorer)
401 regs->u_regs[UREG_I7] = (unsigned long)ksig->ka.ka_restorer;
402 else {
403 regs->u_regs[UREG_I7] = (unsigned long)(&(sf->insns[0]) - 2);
404
405
406 err |= __put_user(0x821020d8, &sf->insns[0]);
407
408
409 err |= __put_user(0x91d02010, &sf->insns[1]);
410 if (err)
411 return err;
412
413
414 flush_sig_insns(current->mm, (unsigned long) &(sf->insns[0]));
415 }
416 return 0;
417}
418
419static inline void
420handle_signal(struct ksignal *ksig, struct pt_regs *regs)
421{
422 sigset_t *oldset = sigmask_to_save();
423 int err;
424
425 if (ksig->ka.sa.sa_flags & SA_SIGINFO)
426 err = setup_rt_frame(ksig, regs, oldset);
427 else
428 err = setup_frame(ksig, regs, oldset);
429 signal_setup_done(err, ksig, 0);
430}
431
432static inline void syscall_restart(unsigned long orig_i0, struct pt_regs *regs,
433 struct sigaction *sa)
434{
435 switch(regs->u_regs[UREG_I0]) {
436 case ERESTART_RESTARTBLOCK:
437 case ERESTARTNOHAND:
438 no_system_call_restart:
439 regs->u_regs[UREG_I0] = EINTR;
440 regs->psr |= PSR_C;
441 break;
442 case ERESTARTSYS:
443 if (!(sa->sa_flags & SA_RESTART))
444 goto no_system_call_restart;
445
446 case ERESTARTNOINTR:
447 regs->u_regs[UREG_I0] = orig_i0;
448 regs->pc -= 4;
449 regs->npc -= 4;
450 }
451}
452
453
454
455
456
457static void do_signal(struct pt_regs *regs, unsigned long orig_i0)
458{
459 struct ksignal ksig;
460 int restart_syscall;
461 bool has_handler;
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481 if (pt_regs_is_syscall(regs) && (regs->psr & PSR_C))
482 regs->u_regs[UREG_G6] = orig_i0;
483
484 has_handler = get_signal(&ksig);
485
486
487
488
489
490 restart_syscall = 0;
491 if (pt_regs_is_syscall(regs) && (regs->psr & PSR_C)) {
492 restart_syscall = 1;
493 orig_i0 = regs->u_regs[UREG_G6];
494 }
495
496 if (has_handler) {
497 if (restart_syscall)
498 syscall_restart(orig_i0, regs, &ksig.ka.sa);
499 handle_signal(&ksig, regs);
500 } else {
501 if (restart_syscall) {
502 switch (regs->u_regs[UREG_I0]) {
503 case ERESTARTNOHAND:
504 case ERESTARTSYS:
505 case ERESTARTNOINTR:
506
507 regs->u_regs[UREG_I0] = orig_i0;
508 regs->pc -= 4;
509 regs->npc -= 4;
510 pt_regs_clear_syscall(regs);
511
512 case ERESTART_RESTARTBLOCK:
513 regs->u_regs[UREG_G1] = __NR_restart_syscall;
514 regs->pc -= 4;
515 regs->npc -= 4;
516 pt_regs_clear_syscall(regs);
517 }
518 }
519 restore_saved_sigmask();
520 }
521}
522
523void do_notify_resume(struct pt_regs *regs, unsigned long orig_i0,
524 unsigned long thread_info_flags)
525{
526 if (thread_info_flags & _TIF_SIGPENDING)
527 do_signal(regs, orig_i0);
528 if (thread_info_flags & _TIF_NOTIFY_RESUME) {
529 clear_thread_flag(TIF_NOTIFY_RESUME);
530 tracehook_notify_resume(regs);
531 }
532}
533
534asmlinkage int do_sys_sigstack(struct sigstack __user *ssptr,
535 struct sigstack __user *ossptr,
536 unsigned long sp)
537{
538 int ret = -EFAULT;
539
540
541 if (ossptr) {
542 if (put_user(current->sas_ss_sp + current->sas_ss_size,
543 &ossptr->the_stack) ||
544 __put_user(on_sig_stack(sp), &ossptr->cur_status))
545 goto out;
546 }
547
548
549 if (ssptr) {
550 char *ss_sp;
551
552 if (get_user(ss_sp, &ssptr->the_stack))
553 goto out;
554
555
556 ret = -EPERM;
557 if (current->sas_ss_sp && on_sig_stack(sp))
558 goto out;
559
560
561
562
563 current->sas_ss_sp = (unsigned long)ss_sp - SIGSTKSZ;
564 current->sas_ss_size = SIGSTKSZ;
565 }
566 ret = 0;
567out:
568 return ret;
569}
570