Linux 3.11-rc3
[cris-mirror.git] / arch / h8300 / kernel / signal.c
bloba65ff3b763268f1d8b680c6466adbda1412cab40
1 /*
2 * linux/arch/h8300/kernel/signal.c
4 * Copyright (C) 1991, 1992 Linus Torvalds
6 * This file is subject to the terms and conditions of the GNU General Public
7 * License. See the file COPYING in the main directory of this archive
8 * for more details.
9 */
12 * uClinux H8/300 support by Yoshinori Sato <ysato@users.sourceforge.jp>
13 * and David McCullough <davidm@snapgear.com>
15 * Based on
16 * Linux/m68k by Hamish Macdonald
20 * ++roman (07/09/96): implemented signal stacks (specially for tosemu on
21 * Atari :-) Current limitation: Only one sigstack can be active at one time.
22 * If a second signal with SA_ONSTACK set arrives while working on a sigstack,
23 * SA_ONSTACK is ignored. This behaviour avoids lots of trouble with nested
24 * signal handlers!
27 #include <linux/sched.h>
28 #include <linux/mm.h>
29 #include <linux/kernel.h>
30 #include <linux/signal.h>
31 #include <linux/syscalls.h>
32 #include <linux/errno.h>
33 #include <linux/wait.h>
34 #include <linux/ptrace.h>
35 #include <linux/unistd.h>
36 #include <linux/stddef.h>
37 #include <linux/highuid.h>
38 #include <linux/personality.h>
39 #include <linux/tty.h>
40 #include <linux/binfmts.h>
41 #include <linux/tracehook.h>
43 #include <asm/setup.h>
44 #include <asm/uaccess.h>
45 #include <asm/pgtable.h>
46 #include <asm/traps.h>
47 #include <asm/ucontext.h>
50 * Do a signal return; undo the signal stack.
52 * Keep the return code on the stack quadword aligned!
53 * That makes the cache flush below easier.
56 struct sigframe
58 long dummy_er0;
59 long dummy_vector;
60 #if defined(CONFIG_CPU_H8S)
61 short dummy_exr;
62 #endif
63 long dummy_pc;
64 char *pretcode;
65 unsigned char retcode[8];
66 unsigned long extramask[_NSIG_WORDS-1];
67 struct sigcontext sc;
68 int sig;
69 } __attribute__((aligned(2),packed));
71 struct rt_sigframe
73 long dummy_er0;
74 long dummy_vector;
75 #if defined(CONFIG_CPU_H8S)
76 short dummy_exr;
77 #endif
78 long dummy_pc;
79 char *pretcode;
80 struct siginfo *pinfo;
81 void *puc;
82 unsigned char retcode[8];
83 struct siginfo info;
84 struct ucontext uc;
85 int sig;
86 } __attribute__((aligned(2),packed));
88 static inline int
89 restore_sigcontext(struct sigcontext *usc, int *pd0)
91 struct pt_regs *regs = current_pt_regs();
92 int err = 0;
93 unsigned int ccr;
94 unsigned int usp;
95 unsigned int er0;
97 /* Always make any pending restarted system calls return -EINTR */
98 current_thread_info()->restart_block.fn = do_no_restart_syscall;
100 #define COPY(r) err |= __get_user(regs->r, &usc->sc_##r) /* restore passed registers */
101 COPY(er1);
102 COPY(er2);
103 COPY(er3);
104 COPY(er5);
105 COPY(pc);
106 ccr = regs->ccr & 0x10;
107 COPY(ccr);
108 #undef COPY
109 regs->ccr &= 0xef;
110 regs->ccr |= ccr;
111 regs->orig_er0 = -1; /* disable syscall checks */
112 err |= __get_user(usp, &usc->sc_usp);
113 wrusp(usp);
115 err |= __get_user(er0, &usc->sc_er0);
116 *pd0 = er0;
117 return err;
120 asmlinkage int sys_sigreturn(void)
122 unsigned long usp = rdusp();
123 struct sigframe *frame = (struct sigframe *)(usp - 4);
124 sigset_t set;
125 int er0;
127 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
128 goto badframe;
129 if (__get_user(set.sig[0], &frame->sc.sc_mask) ||
130 (_NSIG_WORDS > 1 &&
131 __copy_from_user(&set.sig[1], &frame->extramask,
132 sizeof(frame->extramask))))
133 goto badframe;
135 set_current_blocked(&set);
137 if (restore_sigcontext(&frame->sc, &er0))
138 goto badframe;
139 return er0;
141 badframe:
142 force_sig(SIGSEGV, current);
143 return 0;
146 asmlinkage int sys_rt_sigreturn(void)
148 unsigned long usp = rdusp();
149 struct rt_sigframe *frame = (struct rt_sigframe *)(usp - 4);
150 sigset_t set;
151 int er0;
153 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
154 goto badframe;
155 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
156 goto badframe;
158 set_current_blocked(&set);
160 if (restore_sigcontext(&frame->uc.uc_mcontext, &er0))
161 goto badframe;
163 if (restore_altstack(&frame->uc.uc_stack))
164 goto badframe;
166 return er0;
168 badframe:
169 force_sig(SIGSEGV, current);
170 return 0;
173 static int setup_sigcontext(struct sigcontext __user *sc, struct pt_regs *regs,
174 unsigned long mask)
176 int err = 0;
178 err |= __put_user(regs->er0, &sc->sc_er0);
179 err |= __put_user(regs->er1, &sc->sc_er1);
180 err |= __put_user(regs->er2, &sc->sc_er2);
181 err |= __put_user(regs->er3, &sc->sc_er3);
182 err |= __put_user(regs->er4, &sc->sc_er4);
183 err |= __put_user(regs->er5, &sc->sc_er5);
184 err |= __put_user(regs->er6, &sc->sc_er6);
185 err |= __put_user(rdusp(), &sc->sc_usp);
186 err |= __put_user(regs->pc, &sc->sc_pc);
187 err |= __put_user(regs->ccr, &sc->sc_ccr);
188 err |= __put_user(mask, &sc->sc_mask);
190 return err;
193 static inline void *
194 get_sigframe(struct k_sigaction *ka, struct pt_regs *regs, size_t frame_size)
196 unsigned long usp;
198 /* Default to using normal stack. */
199 usp = rdusp();
201 /* This is the X/Open sanctioned signal stack switching. */
202 if (ka->sa.sa_flags & SA_ONSTACK) {
203 if (!sas_ss_flags(usp))
204 usp = current->sas_ss_sp + current->sas_ss_size;
206 return (void *)((usp - frame_size) & -8UL);
209 static int setup_frame (int sig, struct k_sigaction *ka,
210 sigset_t *set, struct pt_regs *regs)
212 struct sigframe *frame;
213 int err = 0;
214 int usig;
215 unsigned char *ret;
217 frame = get_sigframe(ka, regs, sizeof(*frame));
219 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
220 goto give_sigsegv;
222 usig = current_thread_info()->exec_domain
223 && current_thread_info()->exec_domain->signal_invmap
224 && sig < 32
225 ? current_thread_info()->exec_domain->signal_invmap[sig]
226 : sig;
228 err |= __put_user(usig, &frame->sig);
229 if (err)
230 goto give_sigsegv;
232 err |= setup_sigcontext(&frame->sc, regs, set->sig[0]);
233 if (err)
234 goto give_sigsegv;
236 if (_NSIG_WORDS > 1) {
237 err |= copy_to_user(frame->extramask, &set->sig[1],
238 sizeof(frame->extramask));
239 if (err)
240 goto give_sigsegv;
243 ret = frame->retcode;
244 if (ka->sa.sa_flags & SA_RESTORER)
245 ret = (unsigned char *)(ka->sa.sa_restorer);
246 else {
247 /* sub.l er0,er0; mov.b #__NR_sigreturn,r0l; trapa #0 */
248 err |= __put_user(0x1a80f800 + (__NR_sigreturn & 0xff),
249 (unsigned long *)(frame->retcode + 0));
250 err |= __put_user(0x5700, (unsigned short *)(frame->retcode + 4));
253 /* Set up to return from userspace. */
254 err |= __put_user(ret, &frame->pretcode);
256 if (err)
257 goto give_sigsegv;
259 /* Set up registers for signal handler */
260 wrusp ((unsigned long) frame);
261 regs->pc = (unsigned long) ka->sa.sa_handler;
262 regs->er0 = (current_thread_info()->exec_domain
263 && current_thread_info()->exec_domain->signal_invmap
264 && sig < 32
265 ? current_thread_info()->exec_domain->signal_invmap[sig]
266 : sig);
267 regs->er1 = (unsigned long)&(frame->sc);
268 regs->er5 = current->mm->start_data; /* GOT base */
270 return 0;
272 give_sigsegv:
273 force_sigsegv(sig, current);
274 return -EFAULT;
277 static int setup_rt_frame (int sig, struct k_sigaction *ka, siginfo_t *info,
278 sigset_t *set, struct pt_regs *regs)
280 struct rt_sigframe *frame;
281 int err = 0;
282 int usig;
283 unsigned char *ret;
285 frame = get_sigframe(ka, regs, sizeof(*frame));
287 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
288 goto give_sigsegv;
290 usig = current_thread_info()->exec_domain
291 && current_thread_info()->exec_domain->signal_invmap
292 && sig < 32
293 ? current_thread_info()->exec_domain->signal_invmap[sig]
294 : sig;
296 err |= __put_user(usig, &frame->sig);
297 if (err)
298 goto give_sigsegv;
300 err |= __put_user(&frame->info, &frame->pinfo);
301 err |= __put_user(&frame->uc, &frame->puc);
302 err |= copy_siginfo_to_user(&frame->info, info);
303 if (err)
304 goto give_sigsegv;
306 /* Create the ucontext. */
307 err |= __put_user(0, &frame->uc.uc_flags);
308 err |= __put_user(0, &frame->uc.uc_link);
309 err |= __save_altstack(&frame->uc.uc_stack, rdusp());
310 err |= setup_sigcontext(&frame->uc.uc_mcontext, regs, set->sig[0]);
311 err |= copy_to_user (&frame->uc.uc_sigmask, set, sizeof(*set));
312 if (err)
313 goto give_sigsegv;
315 /* Set up to return from userspace. */
316 ret = frame->retcode;
317 if (ka->sa.sa_flags & SA_RESTORER)
318 ret = (unsigned char *)(ka->sa.sa_restorer);
319 else {
320 /* sub.l er0,er0; mov.b #__NR_sigreturn,r0l; trapa #0 */
321 err |= __put_user(0x1a80f800 + (__NR_sigreturn & 0xff),
322 (unsigned long *)(frame->retcode + 0));
323 err |= __put_user(0x5700, (unsigned short *)(frame->retcode + 4));
325 err |= __put_user(ret, &frame->pretcode);
327 if (err)
328 goto give_sigsegv;
330 /* Set up registers for signal handler */
331 wrusp ((unsigned long) frame);
332 regs->pc = (unsigned long) ka->sa.sa_handler;
333 regs->er0 = (current_thread_info()->exec_domain
334 && current_thread_info()->exec_domain->signal_invmap
335 && sig < 32
336 ? current_thread_info()->exec_domain->signal_invmap[sig]
337 : sig);
338 regs->er1 = (unsigned long)&(frame->info);
339 regs->er2 = (unsigned long)&frame->uc;
340 regs->er5 = current->mm->start_data; /* GOT base */
342 return 0;
344 give_sigsegv:
345 force_sigsegv(sig, current);
346 return -EFAULT;
350 * OK, we're invoking a handler
352 static void
353 handle_signal(unsigned long sig, siginfo_t *info, struct k_sigaction *ka,
354 struct pt_regs * regs)
356 sigset_t *oldset = sigmask_to_save();
357 int ret;
358 /* are we from a system call? */
359 if (regs->orig_er0 >= 0) {
360 switch (regs->er0) {
361 case -ERESTART_RESTARTBLOCK:
362 case -ERESTARTNOHAND:
363 regs->er0 = -EINTR;
364 break;
366 case -ERESTARTSYS:
367 if (!(ka->sa.sa_flags & SA_RESTART)) {
368 regs->er0 = -EINTR;
369 break;
371 /* fallthrough */
372 case -ERESTARTNOINTR:
373 regs->er0 = regs->orig_er0;
374 regs->pc -= 2;
378 /* set up the stack frame */
379 if (ka->sa.sa_flags & SA_SIGINFO)
380 ret = setup_rt_frame(sig, ka, info, oldset, regs);
381 else
382 ret = setup_frame(sig, ka, oldset, regs);
384 if (!ret)
385 signal_delivered(sig, info, ka, regs, 0);
389 * Note that 'init' is a special process: it doesn't get signals it doesn't
390 * want to handle. Thus you cannot kill init even with a SIGKILL even by
391 * mistake.
393 static void do_signal(struct pt_regs *regs)
395 siginfo_t info;
396 int signr;
397 struct k_sigaction ka;
400 * We want the common case to go fast, which
401 * is why we may in certain cases get here from
402 * kernel mode. Just return without doing anything
403 * if so.
405 if ((regs->ccr & 0x10))
406 return;
408 current->thread.esp0 = (unsigned long) regs;
410 signr = get_signal_to_deliver(&info, &ka, regs, NULL);
411 if (signr > 0) {
412 /* Whee! Actually deliver the signal. */
413 handle_signal(signr, &info, &ka, regs);
414 return;
416 /* Did we come from a system call? */
417 if (regs->orig_er0 >= 0) {
418 /* Restart the system call - no handlers present */
419 if (regs->er0 == -ERESTARTNOHAND ||
420 regs->er0 == -ERESTARTSYS ||
421 regs->er0 == -ERESTARTNOINTR) {
422 regs->er0 = regs->orig_er0;
423 regs->pc -= 2;
425 if (regs->er0 == -ERESTART_RESTARTBLOCK){
426 regs->er0 = __NR_restart_syscall;
427 regs->pc -= 2;
431 /* If there's no signal to deliver, we just restore the saved mask. */
432 restore_saved_sigmask();
435 asmlinkage void do_notify_resume(struct pt_regs *regs, u32 thread_info_flags)
437 if (thread_info_flags & _TIF_SIGPENDING)
438 do_signal(regs);
440 if (thread_info_flags & _TIF_NOTIFY_RESUME) {
441 clear_thread_flag(TIF_NOTIFY_RESUME);
442 tracehook_notify_resume(regs);