~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/arch/sh/kernel/signal.c

Version: ~ [ linux-5.8 ] ~ [ linux-5.7.12 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.55 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.136 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.191 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.232 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.232 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.19.8 ] ~ [ linux-3.18.140 ] ~ [ linux-3.17.8 ] ~ [ linux-3.16.85 ] ~ [ linux-3.15.10 ] ~ [ linux-3.14.79 ] ~ [ linux-3.13.11 ] ~ [ linux-3.12.74 ] ~ [ linux-3.11.10 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 /* $Id: signal.c,v 1.15 2003/05/06 23:28:47 lethal Exp $
  2  *
  3  *  linux/arch/sh/kernel/signal.c
  4  *
  5  *  Copyright (C) 1991, 1992  Linus Torvalds
  6  *
  7  *  1997-11-28  Modified for POSIX.1b signals by Richard Henderson
  8  *
  9  *  SuperH version:  Copyright (C) 1999, 2000  Niibe Yutaka & Kaz Kojima
 10  *
 11  */
 12 
 13 #include <linux/sched.h>
 14 #include <linux/mm.h>
 15 #include <linux/smp.h>
 16 #include <linux/smp_lock.h>
 17 #include <linux/kernel.h>
 18 #include <linux/signal.h>
 19 #include <linux/errno.h>
 20 #include <linux/wait.h>
 21 #include <linux/ptrace.h>
 22 #include <linux/unistd.h>
 23 #include <linux/stddef.h>
 24 #include <linux/tty.h>
 25 #include <linux/personality.h>
 26 #include <linux/binfmts.h>
 27 #include <linux/suspend.h>
 28 
 29 #include <asm/ucontext.h>
 30 #include <asm/uaccess.h>
 31 #include <asm/pgtable.h>
 32 #include <asm/cacheflush.h>
 33 
 34 #define DEBUG_SIG 0
 35 
 36 #define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
 37 
 38 asmlinkage int do_signal(struct pt_regs *regs, sigset_t *oldset);
 39 
 40 /*
 41  * Atomically swap in the new signal mask, and wait for a signal.
 42  */
 43 asmlinkage int
 44 sys_sigsuspend(old_sigset_t mask,
 45                unsigned long r5, unsigned long r6, unsigned long r7,
 46                struct pt_regs regs)
 47 {
 48         sigset_t saveset;
 49 
 50         mask &= _BLOCKABLE;
 51         spin_lock_irq(&current->sighand->siglock);
 52         saveset = current->blocked;
 53         siginitset(&current->blocked, mask);
 54         recalc_sigpending();
 55         spin_unlock_irq(&current->sighand->siglock);
 56 
 57         regs.regs[0] = -EINTR;
 58         while (1) {
 59                 current->state = TASK_INTERRUPTIBLE;
 60                 schedule();
 61                 if (do_signal(&regs, &saveset))
 62                         return -EINTR;
 63         }
 64 }
 65 
 66 asmlinkage int
 67 sys_rt_sigsuspend(sigset_t *unewset, size_t sigsetsize,
 68                   unsigned long r6, unsigned long r7,
 69                   struct pt_regs regs)
 70 {
 71         sigset_t saveset, newset;
 72 
 73         /* XXX: Don't preclude handling different sized sigset_t's.  */
 74         if (sigsetsize != sizeof(sigset_t))
 75                 return -EINVAL;
 76 
 77         if (copy_from_user(&newset, unewset, sizeof(newset)))
 78                 return -EFAULT;
 79         sigdelsetmask(&newset, ~_BLOCKABLE);
 80         spin_lock_irq(&current->sighand->siglock);
 81         saveset = current->blocked;
 82         current->blocked = newset;
 83         recalc_sigpending();
 84         spin_unlock_irq(&current->sighand->siglock);
 85 
 86         regs.regs[0] = -EINTR;
 87         while (1) {
 88                 current->state = TASK_INTERRUPTIBLE;
 89                 schedule();
 90                 if (do_signal(&regs, &saveset))
 91                         return -EINTR;
 92         }
 93 }
 94 
 95 asmlinkage int 
 96 sys_sigaction(int sig, const struct old_sigaction *act,
 97               struct old_sigaction *oact)
 98 {
 99         struct k_sigaction new_ka, old_ka;
100         int ret;
101 
102         if (act) {
103                 old_sigset_t mask;
104                 if (verify_area(VERIFY_READ, act, sizeof(*act)) ||
105                     __get_user(new_ka.sa.sa_handler, &act->sa_handler) ||
106                     __get_user(new_ka.sa.sa_restorer, &act->sa_restorer))
107                         return -EFAULT;
108                 __get_user(new_ka.sa.sa_flags, &act->sa_flags);
109                 __get_user(mask, &act->sa_mask);
110                 siginitset(&new_ka.sa.sa_mask, mask);
111         }
112 
113         ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
114 
115         if (!ret && oact) {
116                 if (verify_area(VERIFY_WRITE, oact, sizeof(*oact)) ||
117                     __put_user(old_ka.sa.sa_handler, &oact->sa_handler) ||
118                     __put_user(old_ka.sa.sa_restorer, &oact->sa_restorer))
119                         return -EFAULT;
120                 __put_user(old_ka.sa.sa_flags, &oact->sa_flags);
121                 __put_user(old_ka.sa.sa_mask.sig[0], &oact->sa_mask);
122         }
123 
124         return ret;
125 }
126 
127 asmlinkage int
128 sys_sigaltstack(const stack_t *uss, stack_t *uoss,
129                 unsigned long r6, unsigned long r7,
130                 struct pt_regs regs)
131 {
132         return do_sigaltstack(uss, uoss, regs.regs[15]);
133 }
134 
135 
136 /*
137  * Do a signal return; undo the signal stack.
138  */
139 
140 struct sigframe
141 {
142         struct sigcontext sc;
143         unsigned long extramask[_NSIG_WORDS-1];
144         char retcode[4];
145 };
146 
147 struct rt_sigframe
148 {
149         struct siginfo *pinfo;
150         void *puc;
151         struct siginfo info;
152         struct ucontext uc;
153         char retcode[4];
154 };
155 
156 #ifdef CONFIG_CPU_SH4
157 static inline int restore_sigcontext_fpu(struct sigcontext *sc)
158 {
159         struct task_struct *tsk = current;
160 
161         if (!(cpu_data->flags & CPU_HAS_FPU))
162                 return 0;
163 
164         tsk->used_math = 1;
165         return __copy_from_user(&tsk->thread.fpu.hard, &sc->sc_fpregs[0],
166                                 sizeof(long)*(16*2+2));
167 }
168 
169 static inline int save_sigcontext_fpu(struct sigcontext *sc)
170 {
171         struct task_struct *tsk = current;
172 
173         if (!(cpu_data->flags & CPU_HAS_FPU))
174                 return 0;
175 
176         if (!tsk->used_math) {
177                 __put_user(0, &sc->sc_ownedfp);
178                 return 0;
179         }
180 
181         __put_user(1, &sc->sc_ownedfp);
182 
183         /* This will cause a "finit" to be triggered by the next
184            attempted FPU operation by the 'current' process.
185            */
186         tsk->used_math = 0;
187 
188         unlazy_fpu(tsk);
189         return __copy_to_user(&sc->sc_fpregs[0], &tsk->thread.fpu.hard,
190                               sizeof(long)*(16*2+2));
191 }
192 #endif /* CONFIG_CPU_SH4 */
193 
194 static int
195 restore_sigcontext(struct pt_regs *regs, struct sigcontext *sc, int *r0_p)
196 {
197         unsigned int err = 0;
198 
199 #define COPY(x)         err |= __get_user(regs->x, &sc->sc_##x)
200                         COPY(regs[1]);
201         COPY(regs[2]);  COPY(regs[3]);
202         COPY(regs[4]);  COPY(regs[5]);
203         COPY(regs[6]);  COPY(regs[7]);
204         COPY(regs[8]);  COPY(regs[9]);
205         COPY(regs[10]); COPY(regs[11]);
206         COPY(regs[12]); COPY(regs[13]);
207         COPY(regs[14]); COPY(regs[15]);
208         COPY(gbr);      COPY(mach);
209         COPY(macl);     COPY(pr);
210         COPY(sr);       COPY(pc);
211 #undef COPY
212 
213 #ifdef CONFIG_CPU_SH4
214         if (cpu_data->flags & CPU_HAS_FPU) {
215                 int owned_fp;
216                 struct task_struct *tsk = current;
217 
218                 regs->sr |= SR_FD; /* Release FPU */
219                 clear_fpu(tsk);
220                 tsk->used_math = 0;
221                 __get_user (owned_fp, &sc->sc_ownedfp);
222                 if (owned_fp)
223                         err |= restore_sigcontext_fpu(sc);
224         }
225 #endif
226 
227         regs->tra = -1;         /* disable syscall checks */
228         err |= __get_user(*r0_p, &sc->sc_regs[0]);
229         return err;
230 }
231 
232 asmlinkage int sys_sigreturn(unsigned long r4, unsigned long r5,
233                              unsigned long r6, unsigned long r7,
234                              struct pt_regs regs)
235 {
236         struct sigframe *frame = (struct sigframe *)regs.regs[15];
237         sigset_t set;
238         int r0;
239 
240         if (verify_area(VERIFY_READ, frame, sizeof(*frame)))
241                 goto badframe;
242 
243         if (__get_user(set.sig[0], &frame->sc.oldmask)
244             || (_NSIG_WORDS > 1
245                 && __copy_from_user(&set.sig[1], &frame->extramask,
246                                     sizeof(frame->extramask))))
247                 goto badframe;
248 
249         sigdelsetmask(&set, ~_BLOCKABLE);
250 
251         spin_lock_irq(&current->sighand->siglock);
252         current->blocked = set;
253         recalc_sigpending();
254         spin_unlock_irq(&current->sighand->siglock);
255 
256         if (restore_sigcontext(&regs, &frame->sc, &r0))
257                 goto badframe;
258         return r0;
259 
260 badframe:
261         force_sig(SIGSEGV, current);
262         return 0;
263 }
264 
265 asmlinkage int sys_rt_sigreturn(unsigned long r4, unsigned long r5,
266                                 unsigned long r6, unsigned long r7,
267                                 struct pt_regs regs)
268 {
269         struct rt_sigframe *frame = (struct rt_sigframe *)regs.regs[15];
270         sigset_t set;
271         stack_t st;
272         int r0;
273 
274         if (verify_area(VERIFY_READ, frame, sizeof(*frame)))
275                 goto badframe;
276 
277         if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
278                 goto badframe;
279 
280         sigdelsetmask(&set, ~_BLOCKABLE);
281         spin_lock_irq(&current->sighand->siglock);
282         current->blocked = set;
283         recalc_sigpending();
284         spin_unlock_irq(&current->sighand->siglock);
285 
286         if (restore_sigcontext(&regs, &frame->uc.uc_mcontext, &r0))
287                 goto badframe;
288 
289         if (__copy_from_user(&st, &frame->uc.uc_stack, sizeof(st)))
290                 goto badframe;
291         /* It is more difficult to avoid calling this function than to
292            call it and ignore errors.  */
293         do_sigaltstack(&st, NULL, regs.regs[15]);
294 
295         return r0;
296 
297 badframe:
298         force_sig(SIGSEGV, current);
299         return 0;
300 }       
301 
302 /*
303  * Set up a signal frame.
304  */
305 
306 static int
307 setup_sigcontext(struct sigcontext *sc, struct pt_regs *regs,
308                  unsigned long mask)
309 {
310         int err = 0;
311 
312 #define COPY(x)         err |= __put_user(regs->x, &sc->sc_##x)
313         COPY(regs[0]);  COPY(regs[1]);
314         COPY(regs[2]);  COPY(regs[3]);
315         COPY(regs[4]);  COPY(regs[5]);
316         COPY(regs[6]);  COPY(regs[7]);
317         COPY(regs[8]);  COPY(regs[9]);
318         COPY(regs[10]); COPY(regs[11]);
319         COPY(regs[12]); COPY(regs[13]);
320         COPY(regs[14]); COPY(regs[15]);
321         COPY(gbr);      COPY(mach);
322         COPY(macl);     COPY(pr);
323         COPY(sr);       COPY(pc);
324 #undef COPY
325 
326 #ifdef CONFIG_CPU_SH4
327         err |= save_sigcontext_fpu(sc);
328 #endif
329 
330         /* non-iBCS2 extensions.. */
331         err |= __put_user(mask, &sc->oldmask);
332 
333         return err;
334 }
335 
336 /*
337  * Determine which stack to use..
338  */
339 static inline void *
340 get_sigframe(struct k_sigaction *ka, unsigned long sp, size_t frame_size)
341 {
342         if ((ka->sa.sa_flags & SA_ONSTACK) != 0 && ! on_sig_stack(sp))
343                 sp = current->sas_ss_sp + current->sas_ss_size;
344 
345         return (void *)((sp - frame_size) & -8ul);
346 }
347 
348 static void setup_frame(int sig, struct k_sigaction *ka,
349                         sigset_t *set, struct pt_regs *regs)
350 {
351         struct sigframe *frame;
352         int err = 0;
353         int signal;
354 
355         frame = get_sigframe(ka, regs->regs[15], sizeof(*frame));
356 
357         if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
358                 goto give_sigsegv;
359 
360         signal = current_thread_info()->exec_domain
361                 && current_thread_info()->exec_domain->signal_invmap
362                 && sig < 32
363                 ? current_thread_info()->exec_domain->signal_invmap[sig]
364                 : sig;
365 
366         err |= setup_sigcontext(&frame->sc, regs, set->sig[0]);
367 
368         if (_NSIG_WORDS > 1) {
369                 err |= __copy_to_user(frame->extramask, &set->sig[1],
370                                       sizeof(frame->extramask));
371         }
372 
373         /* Set up to return from userspace.  If provided, use a stub
374            already in userspace.  */
375         if (ka->sa.sa_flags & SA_RESTORER) {
376                 regs->pr = (unsigned long) ka->sa.sa_restorer;
377         } else {
378                 /* This is : mov  #__NR_sigreturn,r3 ; trapa #0x10 */
379 #ifdef __LITTLE_ENDIAN__
380                 unsigned long code = 0xc310e300 | (__NR_sigreturn);
381 #else
382                 unsigned long code = 0xe300c310 | (__NR_sigreturn << 16);
383 #endif
384 
385                 regs->pr = (unsigned long) frame->retcode;
386                 err |= __put_user(code, (long *)(frame->retcode+0));
387         }
388 
389         if (err)
390                 goto give_sigsegv;
391 
392         /* Set up registers for signal handler */
393         regs->regs[15] = (unsigned long) frame;
394         regs->regs[4] = signal; /* Arg for signal handler */
395         regs->pc = (unsigned long) ka->sa.sa_handler;
396 
397         set_fs(USER_DS);
398 
399 #if DEBUG_SIG
400         printk("SIG deliver (%s:%d): sp=%p pc=%08lx pr=%08lx\n",
401                 current->comm, current->pid, frame, regs->pc, regs->pr);
402 #endif
403 
404         flush_cache_sigtramp(regs->pr);
405         return;
406 
407 give_sigsegv:
408         if (sig == SIGSEGV)
409                 ka->sa.sa_handler = SIG_DFL;
410         force_sig(SIGSEGV, current);
411 }
412 
413 static void setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
414                            sigset_t *set, struct pt_regs *regs)
415 {
416         struct rt_sigframe *frame;
417         int err = 0;
418         int signal;
419 
420         frame = get_sigframe(ka, regs->regs[15], sizeof(*frame));
421 
422         if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
423                 goto give_sigsegv;
424 
425         signal = current_thread_info()->exec_domain
426                 && current_thread_info()->exec_domain->signal_invmap
427                 && sig < 32
428                 ? current_thread_info()->exec_domain->signal_invmap[sig]
429                 : sig;
430 
431         err |= __put_user(&frame->info, &frame->pinfo);
432         err |= __put_user(&frame->uc, &frame->puc);
433         err |= copy_siginfo_to_user(&frame->info, info);
434 
435         /* Create the ucontext.  */
436         err |= __put_user(0, &frame->uc.uc_flags);
437         err |= __put_user(0, &frame->uc.uc_link);
438         err |= __put_user((void *)current->sas_ss_sp,
439                           &frame->uc.uc_stack.ss_sp);
440         err |= __put_user(sas_ss_flags(regs->regs[15]),
441                           &frame->uc.uc_stack.ss_flags);
442         err |= __put_user(current->sas_ss_size, &frame->uc.uc_stack.ss_size);
443         err |= setup_sigcontext(&frame->uc.uc_mcontext,
444                                 regs, set->sig[0]);
445         err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
446 
447         /* Set up to return from userspace.  If provided, use a stub
448            already in userspace.  */
449         if (ka->sa.sa_flags & SA_RESTORER) {
450                 regs->pr = (unsigned long) ka->sa.sa_restorer;
451         } else {
452                 /* This is : mov  #__NR_rt_sigreturn,r3 ; trapa #0x10 */
453 #ifdef __LITTLE_ENDIAN__
454                 unsigned long code = 0xc310e300 | (__NR_rt_sigreturn);
455 #else
456                 unsigned long code = 0xe300c310 | (__NR_rt_sigreturn << 16);
457 #endif
458 
459                 regs->pr = (unsigned long) frame->retcode;
460                 err |= __put_user(code, (long *)(frame->retcode+0));
461         }
462 
463         if (err)
464                 goto give_sigsegv;
465 
466         /* Set up registers for signal handler */
467         regs->regs[15] = (unsigned long) frame;
468         regs->regs[4] = signal; /* Arg for signal handler */
469         regs->pc = (unsigned long) ka->sa.sa_handler;
470 
471         set_fs(USER_DS);
472 
473 #if DEBUG_SIG
474         printk("SIG deliver (%s:%d): sp=%p pc=%08lx pr=%08lx\n",
475                 current->comm, current->pid, frame, regs->pc, regs->pr);
476 #endif
477 
478         flush_cache_sigtramp(regs->pr);
479         return;
480 
481 give_sigsegv:
482         if (sig == SIGSEGV)
483                 ka->sa.sa_handler = SIG_DFL;
484         force_sig(SIGSEGV, current);
485 }
486 
487 /*
488  * OK, we're invoking a handler
489  */     
490 
491 static void
492 handle_signal(unsigned long sig, siginfo_t *info, sigset_t *oldset,
493         struct pt_regs * regs)
494 {
495         struct k_sigaction *ka = &current->sighand->action[sig-1];
496 
497         /* Are we from a system call? */
498         if (regs->tra >= 0) {
499                 /* If so, check system call restarting.. */
500                 switch (regs->regs[0]) {
501                         case -ERESTARTNOHAND:
502                                 regs->regs[0] = -EINTR;
503                                 break;
504 
505                         case -ERESTARTSYS:
506                                 if (!(ka->sa.sa_flags & SA_RESTART)) {
507                                         regs->regs[0] = -EINTR;
508                                         break;
509                                 }
510                         /* fallthrough */
511                         case -ERESTARTNOINTR:
512                                 regs->pc -= 2;
513                 }
514 #ifndef CONFIG_PREEMPT
515         } else {
516                 /* gUSA handling */
517                 if (regs->regs[15] >= 0xc0000000) {
518                         int offset = (int)regs->regs[15];
519 
520                         /* Reset stack pointer: clear critical region mark */
521                         regs->regs[15] = regs->regs[1];
522                         if (regs->pc < regs->regs[0])
523                                 /* Go to rewind point #1 */
524                                 regs->pc = regs->regs[0] + offset - 2;
525                 }
526 #endif
527         }
528 
529         /* Set up the stack frame */
530         if (ka->sa.sa_flags & SA_SIGINFO)
531                 setup_rt_frame(sig, ka, info, oldset, regs);
532         else
533                 setup_frame(sig, ka, oldset, regs);
534 
535         if (ka->sa.sa_flags & SA_ONESHOT)
536                 ka->sa.sa_handler = SIG_DFL;
537 
538         if (!(ka->sa.sa_flags & SA_NODEFER)) {
539                 spin_lock_irq(&current->sighand->siglock);
540                 sigorsets(&current->blocked,&current->blocked,&ka->sa.sa_mask);
541                 sigaddset(&current->blocked,sig);
542                 recalc_sigpending();
543                 spin_unlock_irq(&current->sighand->siglock);
544         }
545 }
546 
547 /*
548  * Note that 'init' is a special process: it doesn't get signals it doesn't
549  * want to handle. Thus you cannot kill init even with a SIGKILL even by
550  * mistake.
551  *
552  * Note that we go through the signals twice: once to check the signals that
553  * the kernel can handle, and then we build all the user-level signal handling
554  * stack-frames in one go after that.
555  */
556 int do_signal(struct pt_regs *regs, sigset_t *oldset)
557 {
558         siginfo_t info;
559         int signr;
560 
561         /*
562          * We want the common case to go fast, which
563          * is why we may in certain cases get here from
564          * kernel mode. Just return without doing anything
565          * if so.
566          */
567         if (!user_mode(regs))
568                 return 1;
569 
570         if (current->flags & PF_FREEZE) {
571                 refrigerator(0);
572                 goto no_signal;
573         }
574 
575         if (!oldset)
576                 oldset = &current->blocked;
577 
578         signr = get_signal_to_deliver(&info, regs, NULL);
579         if (signr > 0) {
580                 /* Whee!  Actually deliver the signal.  */
581                 handle_signal(signr, &info, oldset, regs);
582                 return 1;
583         }
584 
585  no_signal:
586         /* Did we come from a system call? */
587         if (regs->tra >= 0) {
588                 /* Restart the system call - no handlers present */
589                 if (regs->regs[0] == -ERESTARTNOHAND ||
590                     regs->regs[0] == -ERESTARTSYS ||
591                     regs->regs[0] == -ERESTARTNOINTR ||
592                     regs->regs[0] == -ERESTART_RESTARTBLOCK) {
593                         regs->pc -= 2;
594                 }
595         }
596         return 0;
597 }
598 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp