blob: c2031e20c04840de43c6398d662e55a40ea4ed4a [file] [log] [blame]
Vineet Guptac3581032013-01-18 15:12:19 +05301/*
2 * Signal Handling for ARC
3 *
4 * Copyright (C) 2004, 2007-2010, 2011-2012 Synopsys, Inc. (www.synopsys.com)
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 *
10 * vineetg: Jan 2010 (Restarting of timer related syscalls)
11 *
12 * vineetg: Nov 2009 (Everything needed for TIF_RESTORE_SIGMASK)
13 * -do_signal() supports TIF_RESTORE_SIGMASK
14 * -do_signal() no loner needs oldset, required by OLD sys_sigsuspend
15 * -sys_rt_sigsuspend() now comes from generic code, so discard arch implemen
16 * -sys_sigsuspend() no longer needs to fudge ptregs, hence that arg removed
17 * -sys_sigsuspend() no longer loops for do_signal(), sets TIF_xxx and leaves
18 * the job to do_signal()
19 *
20 * vineetg: July 2009
21 * -Modified Code to support the uClibc provided userland sigreturn stub
22 * to avoid kernel synthesing it on user stack at runtime, costing TLB
23 * probes and Cache line flushes.
24 *
25 * vineetg: July 2009
26 * -In stash_usr_regs( ) and restore_usr_regs( ), save/restore of user regs
27 * in done in block copy rather than one word at a time.
28 * This saves around 2K of code and improves LMBench lat_sig <catch>
29 *
30 * rajeshwarr: Feb 2009
31 * - Support for Realtime Signals
32 *
33 * vineetg: Aug 11th 2008: Bug #94183
34 * -ViXS were still seeing crashes when using insmod to load drivers.
35 * It turned out that the code to change Execute permssions for TLB entries
36 * of user was not guarded for interrupts (mod_tlb_permission)
37 * This was cauing TLB entries to be overwritten on unrelated indexes
38 *
39 * Vineetg: July 15th 2008: Bug #94183
40 * -Exception happens in Delay slot of a JMP, and before user space resumes,
41 * Signal is delivered (Ctrl + C) = >SIGINT.
42 * setup_frame( ) sets up PC,SP,BLINK to enable user space signal handler
43 * to run, but doesn't clear the Delay slot bit from status32. As a result,
44 * on resuming user mode, signal handler branches off to BTA of orig JMP
45 * -FIX: clear the DE bit from status32 in setup_frame( )
46 *
47 * Rahul Trivedi, Kanika Nema: Codito Technologies 2004
48 */
49
50#include <linux/signal.h>
51#include <linux/ptrace.h>
52#include <linux/personality.h>
53#include <linux/uaccess.h>
54#include <linux/syscalls.h>
55#include <linux/tracehook.h>
56#include <asm/ucontext.h>
57
58struct rt_sigframe {
59 struct siginfo info;
60 struct ucontext uc;
61#define MAGIC_SIGALTSTK 0x07302004
62 unsigned int sigret_magic;
63};
64
65static int
66stash_usr_regs(struct rt_sigframe __user *sf, struct pt_regs *regs,
67 sigset_t *set)
68{
69 int err;
70 err = __copy_to_user(&(sf->uc.uc_mcontext.regs), regs,
71 sizeof(sf->uc.uc_mcontext.regs.scratch));
72 err |= __copy_to_user(&sf->uc.uc_sigmask, set, sizeof(sigset_t));
73
74 return err;
75}
76
77static int restore_usr_regs(struct pt_regs *regs, struct rt_sigframe __user *sf)
78{
79 sigset_t set;
80 int err;
81
82 err = __copy_from_user(&set, &sf->uc.uc_sigmask, sizeof(set));
83 if (!err)
84 set_current_blocked(&set);
85
86 err |= __copy_from_user(regs, &(sf->uc.uc_mcontext.regs),
87 sizeof(sf->uc.uc_mcontext.regs.scratch));
88
89 return err;
90}
91
92static inline int is_do_ss_needed(unsigned int magic)
93{
94 if (MAGIC_SIGALTSTK == magic)
95 return 1;
96 else
97 return 0;
98}
99
100SYSCALL_DEFINE0(rt_sigreturn)
101{
102 struct rt_sigframe __user *sf;
103 unsigned int magic;
Vineet Guptac3581032013-01-18 15:12:19 +0530104 struct pt_regs *regs = current_pt_regs();
105
106 /* Always make any pending restarted system calls return -EINTR */
107 current_thread_info()->restart_block.fn = do_no_restart_syscall;
108
109 /* Since we stacked the signal on a word boundary,
110 * then 'sp' should be word aligned here. If it's
111 * not, then the user is trying to mess with us.
112 */
113 if (regs->sp & 3)
114 goto badframe;
115
116 sf = (struct rt_sigframe __force __user *)(regs->sp);
117
118 if (!access_ok(VERIFY_READ, sf, sizeof(*sf)))
119 goto badframe;
120
Christian Ruppert10469352013-10-02 11:13:38 +0200121 if (__get_user(magic, &sf->sigret_magic))
Vineet Guptac3581032013-01-18 15:12:19 +0530122 goto badframe;
123
124 if (unlikely(is_do_ss_needed(magic)))
125 if (restore_altstack(&sf->uc.uc_stack))
126 goto badframe;
127
Christian Ruppert10469352013-10-02 11:13:38 +0200128 if (restore_usr_regs(regs, sf))
129 goto badframe;
130
Vineet Gupta55bb9482013-01-18 15:12:19 +0530131 /* Don't restart from sigreturn */
132 syscall_wont_restart(regs);
133
Vineet Guptac3581032013-01-18 15:12:19 +0530134 return regs->r0;
135
136badframe:
137 force_sig(SIGSEGV, current);
138 return 0;
139}
140
141/*
142 * Determine which stack to use..
143 */
144static inline void __user *get_sigframe(struct k_sigaction *ka,
145 struct pt_regs *regs,
146 unsigned long framesize)
147{
148 unsigned long sp = regs->sp;
149 void __user *frame;
150
151 /* This is the X/Open sanctioned signal stack switching */
152 if ((ka->sa.sa_flags & SA_ONSTACK) && !sas_ss_flags(sp))
153 sp = current->sas_ss_sp + current->sas_ss_size;
154
155 /* No matter what happens, 'sp' must be word
156 * aligned otherwise nasty things could happen
157 */
158
159 /* ATPCS B01 mandates 8-byte alignment */
160 frame = (void __user *)((sp - framesize) & ~7);
161
162 /* Check that we can actually write to the signal frame */
163 if (!access_ok(VERIFY_WRITE, frame, framesize))
164 frame = NULL;
165
166 return frame;
167}
168
169/*
170 * translate the signal
171 */
172static inline int map_sig(int sig)
173{
174 struct thread_info *thread = current_thread_info();
175 if (thread->exec_domain && thread->exec_domain->signal_invmap
176 && sig < 32)
177 sig = thread->exec_domain->signal_invmap[sig];
178 return sig;
179}
180
181static int
Richard Weinbergerf6dd2a32013-10-06 22:42:38 +0200182setup_rt_frame(struct ksignal *ksig, sigset_t *set, struct pt_regs *regs)
Vineet Guptac3581032013-01-18 15:12:19 +0530183{
184 struct rt_sigframe __user *sf;
185 unsigned int magic = 0;
186 int err = 0;
187
Richard Weinbergerf6dd2a32013-10-06 22:42:38 +0200188 sf = get_sigframe(&ksig->ka, regs, sizeof(struct rt_sigframe));
Vineet Guptac3581032013-01-18 15:12:19 +0530189 if (!sf)
190 return 1;
191
192 /*
Christian Ruppert10469352013-10-02 11:13:38 +0200193 * w/o SA_SIGINFO, struct ucontext is partially populated (only
194 * uc_mcontext/uc_sigmask) for kernel's normal user state preservation
195 * during signal handler execution. This works for SA_SIGINFO as well
196 * although the semantics are now overloaded (the same reg state can be
197 * inspected by userland: but are they allowed to fiddle with it ?
198 */
199 err |= stash_usr_regs(sf, regs, set);
200
201 /*
Vineet Guptac3581032013-01-18 15:12:19 +0530202 * SA_SIGINFO requires 3 args to signal handler:
203 * #1: sig-no (common to any handler)
204 * #2: struct siginfo
205 * #3: struct ucontext (completely populated)
206 */
Richard Weinbergerf6dd2a32013-10-06 22:42:38 +0200207 if (unlikely(ksig->ka.sa.sa_flags & SA_SIGINFO)) {
208 err |= copy_siginfo_to_user(&sf->info, &ksig->info);
Vineet Guptac3581032013-01-18 15:12:19 +0530209 err |= __put_user(0, &sf->uc.uc_flags);
210 err |= __put_user(NULL, &sf->uc.uc_link);
211 err |= __save_altstack(&sf->uc.uc_stack, regs->sp);
212
213 /* setup args 2 and 3 for user mode handler */
214 regs->r1 = (unsigned long)&sf->info;
215 regs->r2 = (unsigned long)&sf->uc;
216
217 /*
218 * small optim to avoid unconditonally calling do_sigaltstack
219 * in sigreturn path, now that we only have rt_sigreturn
220 */
221 magic = MAGIC_SIGALTSTK;
222 }
223
Vineet Guptac3581032013-01-18 15:12:19 +0530224 err |= __put_user(magic, &sf->sigret_magic);
225 if (err)
226 return err;
227
228 /* #1 arg to the user Signal handler */
Richard Weinbergerf6dd2a32013-10-06 22:42:38 +0200229 regs->r0 = map_sig(ksig->sig);
Vineet Guptac3581032013-01-18 15:12:19 +0530230
231 /* setup PC of user space signal handler */
Richard Weinbergerf6dd2a32013-10-06 22:42:38 +0200232 regs->ret = (unsigned long)ksig->ka.sa.sa_handler;
Vineet Guptac3581032013-01-18 15:12:19 +0530233
234 /*
235 * handler returns using sigreturn stub provided already by userpsace
236 */
Richard Weinbergerf6dd2a32013-10-06 22:42:38 +0200237 BUG_ON(!(ksig->ka.sa.sa_flags & SA_RESTORER));
238 regs->blink = (unsigned long)ksig->ka.sa.sa_restorer;
Vineet Guptac3581032013-01-18 15:12:19 +0530239
240 /* User Stack for signal handler will be above the frame just carved */
241 regs->sp = (unsigned long)sf;
242
243 /*
244 * Bug 94183, Clear the DE bit, so that when signal handler
245 * starts to run, it doesn't use BTA
246 */
247 regs->status32 &= ~STATUS_DE_MASK;
248 regs->status32 |= STATUS_L_MASK;
249
250 return err;
251}
252
253static void arc_restart_syscall(struct k_sigaction *ka, struct pt_regs *regs)
254{
255 switch (regs->r0) {
256 case -ERESTART_RESTARTBLOCK:
257 case -ERESTARTNOHAND:
258 /*
259 * ERESTARTNOHAND means that the syscall should
260 * only be restarted if there was no handler for
261 * the signal, and since we only get here if there
262 * is a handler, we don't restart
263 */
264 regs->r0 = -EINTR; /* ERESTART_xxx is internal */
265 break;
266
267 case -ERESTARTSYS:
268 /*
269 * ERESTARTSYS means to restart the syscall if
270 * there is no handler or the handler was
271 * registered with SA_RESTART
272 */
273 if (!(ka->sa.sa_flags & SA_RESTART)) {
274 regs->r0 = -EINTR;
275 break;
276 }
277 /* fallthrough */
278
279 case -ERESTARTNOINTR:
280 /*
281 * ERESTARTNOINTR means that the syscall should
282 * be called again after the signal handler returns.
283 * Setup reg state just as it was before doing the trap
284 * r0 has been clobbered with sys call ret code thus it
285 * needs to be reloaded with orig first arg to syscall
286 * in orig_r0. Rest of relevant reg-file:
287 * r8 (syscall num) and (r1 - r7) will be reset to
288 * their orig user space value when we ret from kernel
289 */
290 regs->r0 = regs->orig_r0;
291 regs->ret -= 4;
292 break;
293 }
294}
295
296/*
297 * OK, we're invoking a handler
298 */
299static void
Richard Weinbergerf6dd2a32013-10-06 22:42:38 +0200300handle_signal(struct ksignal *ksig, struct pt_regs *regs)
Vineet Guptac3581032013-01-18 15:12:19 +0530301{
302 sigset_t *oldset = sigmask_to_save();
303 int ret;
304
305 /* Set up the stack frame */
Richard Weinbergerf6dd2a32013-10-06 22:42:38 +0200306 ret = setup_rt_frame(ksig, oldset, regs);
Vineet Guptac3581032013-01-18 15:12:19 +0530307
Richard Weinbergerf6dd2a32013-10-06 22:42:38 +0200308 signal_setup_done(ret, ksig, 0);
Vineet Guptac3581032013-01-18 15:12:19 +0530309}
310
311void do_signal(struct pt_regs *regs)
312{
Richard Weinbergerf6dd2a32013-10-06 22:42:38 +0200313 struct ksignal ksig;
Vineet Guptac3581032013-01-18 15:12:19 +0530314 int restart_scall;
315
Vineet Gupta55bb9482013-01-18 15:12:19 +0530316 restart_scall = in_syscall(regs) && syscall_restartable(regs);
Vineet Guptac3581032013-01-18 15:12:19 +0530317
Richard Weinbergerf6dd2a32013-10-06 22:42:38 +0200318 if (get_signal(&ksig)) {
Vineet Gupta55bb9482013-01-18 15:12:19 +0530319 if (restart_scall) {
Richard Weinbergerf6dd2a32013-10-06 22:42:38 +0200320 arc_restart_syscall(&ksig.ka, regs);
Vineet Gupta55bb9482013-01-18 15:12:19 +0530321 syscall_wont_restart(regs); /* No more restarts */
322 }
Richard Weinbergerf6dd2a32013-10-06 22:42:38 +0200323 handle_signal(&ksig, regs);
Vineet Guptac3581032013-01-18 15:12:19 +0530324 return;
325 }
326
327 if (restart_scall) {
328 /* No handler for syscall: restart it */
329 if (regs->r0 == -ERESTARTNOHAND ||
330 regs->r0 == -ERESTARTSYS || regs->r0 == -ERESTARTNOINTR) {
331 regs->r0 = regs->orig_r0;
332 regs->ret -= 4;
333 } else if (regs->r0 == -ERESTART_RESTARTBLOCK) {
334 regs->r8 = __NR_restart_syscall;
335 regs->ret -= 4;
336 }
Vineet Gupta55bb9482013-01-18 15:12:19 +0530337 syscall_wont_restart(regs); /* No more restarts */
Vineet Guptac3581032013-01-18 15:12:19 +0530338 }
339
340 /* If there's no signal to deliver, restore the saved sigmask back */
341 restore_saved_sigmask();
342}
343
344void do_notify_resume(struct pt_regs *regs)
345{
346 /*
347 * ASM glue gaurantees that this is only called when returning to
348 * user mode
349 */
350 if (test_and_clear_thread_flag(TIF_NOTIFY_RESUME))
351 tracehook_notify_resume(regs);
352}