Merge tag 'locks-v3.16-2' of git://git.samba.org/jlayton/linux
[linux/fpc-iii.git] / arch / metag / kernel / signal.c
blobb9e4a82d2bd4414a31ece43450e21966d6c8ce1c
1 /*
2 * Copyright (C) 1991,1992 Linus Torvalds
3 * Copyright (C) 2005-2012 Imagination Technologies Ltd.
5 * 1997-11-28 Modified for POSIX.1b signals by Richard Henderson
7 */
9 #include <linux/sched.h>
10 #include <linux/mm.h>
11 #include <linux/smp.h>
12 #include <linux/kernel.h>
13 #include <linux/signal.h>
14 #include <linux/errno.h>
15 #include <linux/wait.h>
16 #include <linux/ptrace.h>
17 #include <linux/unistd.h>
18 #include <linux/stddef.h>
19 #include <linux/personality.h>
20 #include <linux/uaccess.h>
21 #include <linux/tracehook.h>
23 #include <asm/ucontext.h>
24 #include <asm/cacheflush.h>
25 #include <asm/switch.h>
26 #include <asm/syscall.h>
27 #include <asm/syscalls.h>
29 #define REG_FLAGS ctx.SaveMask
30 #define REG_RETVAL ctx.DX[0].U0
31 #define REG_SYSCALL ctx.DX[0].U1
32 #define REG_SP ctx.AX[0].U0
33 #define REG_ARG1 ctx.DX[3].U1
34 #define REG_ARG2 ctx.DX[3].U0
35 #define REG_ARG3 ctx.DX[2].U1
36 #define REG_PC ctx.CurrPC
37 #define REG_RTP ctx.DX[4].U1
39 struct rt_sigframe {
40 struct siginfo info;
41 struct ucontext uc;
42 unsigned long retcode[2];
45 static int restore_sigcontext(struct pt_regs *regs,
46 struct sigcontext __user *sc)
48 int err;
50 /* Always make any pending restarted system calls return -EINTR */
51 current_thread_info()->restart_block.fn = do_no_restart_syscall;
53 err = metag_gp_regs_copyin(regs, 0, sizeof(struct user_gp_regs), NULL,
54 &sc->regs);
55 if (!err)
56 err = metag_cb_regs_copyin(regs, 0,
57 sizeof(struct user_cb_regs), NULL,
58 &sc->cb);
59 if (!err)
60 err = metag_rp_state_copyin(regs, 0,
61 sizeof(struct user_rp_state), NULL,
62 &sc->rp);
64 /* This is a user-mode context. */
65 regs->REG_FLAGS |= TBICTX_PRIV_BIT;
67 return err;
70 long sys_rt_sigreturn(void)
72 /* NOTE - Meta stack goes UPWARDS - so we wind the stack back */
73 struct pt_regs *regs = current_pt_regs();
74 struct rt_sigframe __user *frame;
75 sigset_t set;
77 frame = (__force struct rt_sigframe __user *)(regs->REG_SP -
78 sizeof(*frame));
80 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
81 goto badframe;
83 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
84 goto badframe;
86 set_current_blocked(&set);
88 if (restore_sigcontext(regs, &frame->uc.uc_mcontext))
89 goto badframe;
91 if (restore_altstack(&frame->uc.uc_stack))
92 goto badframe;
94 return regs->REG_RETVAL;
96 badframe:
97 force_sig(SIGSEGV, current);
99 return 0;
102 static int setup_sigcontext(struct sigcontext __user *sc, struct pt_regs *regs,
103 unsigned long mask)
105 int err;
107 err = metag_gp_regs_copyout(regs, 0, sizeof(struct user_gp_regs), NULL,
108 &sc->regs);
110 if (!err)
111 err = metag_cb_regs_copyout(regs, 0,
112 sizeof(struct user_cb_regs), NULL,
113 &sc->cb);
114 if (!err)
115 err = metag_rp_state_copyout(regs, 0,
116 sizeof(struct user_rp_state), NULL,
117 &sc->rp);
119 /* OK, clear that cbuf flag in the old context, or our stored
120 * catch buffer will be restored when we go to call the signal
121 * handler. Also clear out the CBRP RA/RD pipe bit incase
122 * that is pending as well!
123 * Note that as we have already stored this context, these
124 * flags will get restored on sigreturn to their original
125 * state.
127 regs->REG_FLAGS &= ~(TBICTX_XCBF_BIT | TBICTX_CBUF_BIT |
128 TBICTX_CBRP_BIT);
130 /* Clear out the LSM_STEP bits in case we are in the middle of
131 * and MSET/MGET.
133 regs->ctx.Flags &= ~TXSTATUS_LSM_STEP_BITS;
135 err |= __put_user(mask, &sc->oldmask);
137 return err;
141 * Determine which stack to use..
143 static void __user *get_sigframe(struct k_sigaction *ka, unsigned long sp,
144 size_t frame_size)
146 /* Meta stacks grows upwards */
147 if ((ka->sa.sa_flags & SA_ONSTACK) && (sas_ss_flags(sp) == 0))
148 sp = current->sas_ss_sp;
150 sp = (sp + 7) & ~7; /* 8byte align stack */
152 return (void __user *)sp;
155 static int setup_rt_frame(struct ksignal *ksig, sigset_t *set,
156 struct pt_regs *regs)
158 struct rt_sigframe __user *frame;
159 int err;
160 unsigned long code;
162 frame = get_sigframe(&ksig->ka, regs->REG_SP, sizeof(*frame));
163 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
164 return -EFAULT;
166 err = copy_siginfo_to_user(&frame->info, &ksig->info);
168 /* Create the ucontext. */
169 err |= __put_user(0, &frame->uc.uc_flags);
170 err |= __put_user(0, (unsigned long __user *)&frame->uc.uc_link);
171 err |= __save_altstack(&frame->uc.uc_stack, regs->REG_SP);
172 err |= setup_sigcontext(&frame->uc.uc_mcontext,
173 regs, set->sig[0]);
174 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
176 if (err)
177 return -EFAULT;
179 /* Set up to return from userspace. */
181 /* MOV D1Re0 (D1.0), #__NR_rt_sigreturn */
182 code = 0x03000004 | (__NR_rt_sigreturn << 3);
183 err |= __put_user(code, (unsigned long __user *)(&frame->retcode[0]));
185 /* SWITCH #__METAG_SW_SYS */
186 code = __METAG_SW_ENCODING(SYS);
187 err |= __put_user(code, (unsigned long __user *)(&frame->retcode[1]));
189 if (err)
190 return -EFAULT;
192 /* Set up registers for signal handler */
193 regs->REG_RTP = (unsigned long) frame->retcode;
194 regs->REG_SP = (unsigned long) frame + sizeof(*frame);
195 regs->REG_ARG1 = ksig->sig;
196 regs->REG_ARG2 = (unsigned long) &frame->info;
197 regs->REG_ARG3 = (unsigned long) &frame->uc;
198 regs->REG_PC = (unsigned long) ksig->ka.sa.sa_handler;
200 pr_debug("SIG deliver (%s:%d): sp=%p pc=%08x pr=%08x\n",
201 current->comm, current->pid, frame, regs->REG_PC,
202 regs->REG_RTP);
204 /* Now pass size of 'new code' into sigtramp so we can do a more
205 * effective cache flush - directed rather than 'full flush'.
207 flush_cache_sigtramp(regs->REG_RTP, sizeof(frame->retcode));
209 return 0;
212 static void handle_signal(struct ksignal *ksig, struct pt_regs *regs)
214 sigset_t *oldset = sigmask_to_save();
215 int ret;
217 /* Set up the stack frame */
218 ret = setup_rt_frame(ksig, oldset, regs);
220 signal_setup_done(ret, ksig, test_thread_flag(TIF_SINGLESTEP));
224 * Notes for Meta.
225 * We have moved from the old 2.4.9 SH way of using syscall_nr (in the stored
226 * context) to passing in the syscall flag on the stack.
227 * This is because having syscall_nr in our context does not fit with TBX, and
228 * corrupted the stack.
230 static int do_signal(struct pt_regs *regs, int syscall)
232 unsigned int retval = 0, continue_addr = 0, restart_addr = 0;
233 int restart = 0;
234 struct ksignal ksig;
237 * By the end of rt_sigreturn the context describes the point that the
238 * signal was taken (which may happen to be just before a syscall if
239 * it's already been restarted). This should *never* be mistaken for a
240 * system call in need of restarting.
242 if (syscall == __NR_rt_sigreturn)
243 syscall = -1;
245 /* Did we come from a system call? */
246 if (syscall >= 0) {
247 continue_addr = regs->REG_PC;
248 restart_addr = continue_addr - 4;
249 retval = regs->REG_RETVAL;
252 * Prepare for system call restart. We do this here so that a
253 * debugger will see the already changed PC.
255 switch (retval) {
256 case -ERESTART_RESTARTBLOCK:
257 restart = -2;
258 case -ERESTARTNOHAND:
259 case -ERESTARTSYS:
260 case -ERESTARTNOINTR:
261 ++restart;
262 regs->REG_PC = restart_addr;
263 break;
268 * Get the signal to deliver. When running under ptrace, at this point
269 * the debugger may change all our registers ...
271 get_signal(&ksig);
274 * Depending on the signal settings we may need to revert the decision
275 * to restart the system call. But skip this if a debugger has chosen to
276 * restart at a different PC.
278 if (regs->REG_PC != restart_addr)
279 restart = 0;
280 if (ksig.sig > 0) {
281 if (unlikely(restart)) {
282 if (retval == -ERESTARTNOHAND
283 || retval == -ERESTART_RESTARTBLOCK
284 || (retval == -ERESTARTSYS
285 && !(ksig.ka.sa.sa_flags & SA_RESTART))) {
286 regs->REG_RETVAL = -EINTR;
287 regs->REG_PC = continue_addr;
291 /* Whee! Actually deliver the signal. */
292 handle_signal(&ksig, regs);
293 return 0;
296 /* Handlerless -ERESTART_RESTARTBLOCK re-enters via restart_syscall */
297 if (unlikely(restart < 0))
298 regs->REG_SYSCALL = __NR_restart_syscall;
301 * If there's no signal to deliver, we just put the saved sigmask back.
303 restore_saved_sigmask();
305 return restart;
308 int do_work_pending(struct pt_regs *regs, unsigned int thread_flags,
309 int syscall)
311 do {
312 if (likely(thread_flags & _TIF_NEED_RESCHED)) {
313 schedule();
314 } else {
315 if (unlikely(!user_mode(regs)))
316 return 0;
317 local_irq_enable();
318 if (thread_flags & _TIF_SIGPENDING) {
319 int restart = do_signal(regs, syscall);
320 if (unlikely(restart)) {
322 * Restart without handlers.
323 * Deal with it without leaving
324 * the kernel space.
326 return restart;
328 syscall = -1;
329 } else {
330 clear_thread_flag(TIF_NOTIFY_RESUME);
331 tracehook_notify_resume(regs);
334 local_irq_disable();
335 thread_flags = current_thread_info()->flags;
336 } while (thread_flags & _TIF_WORK_MASK);
337 return 0;