2 * Signal Handling for ARC
4 * Copyright (C) 2004, 2007-2010, 2011-2012 Synopsys, Inc. (www.synopsys.com)
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.
10 * vineetg: Jan 2010 (Restarting of timer related syscalls)
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()
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.
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>
30 * rajeshwarr: Feb 2009
31 * - Support for Realtime Signals
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
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( )
47 * Rahul Trivedi, Kanika Nema: Codito Technologies 2004
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>
61 #define MAGIC_SIGALTSTK 0x07302004
62 unsigned int sigret_magic
;
66 stash_usr_regs(struct rt_sigframe __user
*sf
, struct pt_regs
*regs
,
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
));
77 static int restore_usr_regs(struct pt_regs
*regs
, struct rt_sigframe __user
*sf
)
82 err
= __copy_from_user(&set
, &sf
->uc
.uc_sigmask
, sizeof(set
));
84 set_current_blocked(&set
);
86 err
|= __copy_from_user(regs
, &(sf
->uc
.uc_mcontext
.regs
),
87 sizeof(sf
->uc
.uc_mcontext
.regs
.scratch
));
92 static inline int is_do_ss_needed(unsigned int magic
)
94 if (MAGIC_SIGALTSTK
== magic
)
100 SYSCALL_DEFINE0(rt_sigreturn
)
102 struct rt_sigframe __user
*sf
;
104 struct pt_regs
*regs
= current_pt_regs();
106 /* Always make any pending restarted system calls return -EINTR */
107 current
->restart_block
.fn
= do_no_restart_syscall
;
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.
116 sf
= (struct rt_sigframe __force __user
*)(regs
->sp
);
118 if (!access_ok(VERIFY_READ
, sf
, sizeof(*sf
)))
121 if (__get_user(magic
, &sf
->sigret_magic
))
124 if (unlikely(is_do_ss_needed(magic
)))
125 if (restore_altstack(&sf
->uc
.uc_stack
))
128 if (restore_usr_regs(regs
, sf
))
131 /* Don't restart from sigreturn */
132 syscall_wont_restart(regs
);
137 force_sig(SIGSEGV
, current
);
142 * Determine which stack to use..
144 static inline void __user
*get_sigframe(struct ksignal
*ksig
,
145 struct pt_regs
*regs
,
146 unsigned long framesize
)
148 unsigned long sp
= sigsp(regs
->sp
, ksig
);
151 /* No matter what happens, 'sp' must be word
152 * aligned otherwise nasty things could happen
155 /* ATPCS B01 mandates 8-byte alignment */
156 frame
= (void __user
*)((sp
- framesize
) & ~7);
158 /* Check that we can actually write to the signal frame */
159 if (!access_ok(VERIFY_WRITE
, frame
, framesize
))
166 * translate the signal
168 static inline int map_sig(int sig
)
170 struct thread_info
*thread
= current_thread_info();
171 if (thread
->exec_domain
&& thread
->exec_domain
->signal_invmap
173 sig
= thread
->exec_domain
->signal_invmap
[sig
];
178 setup_rt_frame(struct ksignal
*ksig
, sigset_t
*set
, struct pt_regs
*regs
)
180 struct rt_sigframe __user
*sf
;
181 unsigned int magic
= 0;
184 sf
= get_sigframe(ksig
, regs
, sizeof(struct rt_sigframe
));
189 * w/o SA_SIGINFO, struct ucontext is partially populated (only
190 * uc_mcontext/uc_sigmask) for kernel's normal user state preservation
191 * during signal handler execution. This works for SA_SIGINFO as well
192 * although the semantics are now overloaded (the same reg state can be
193 * inspected by userland: but are they allowed to fiddle with it ?
195 err
|= stash_usr_regs(sf
, regs
, set
);
198 * SA_SIGINFO requires 3 args to signal handler:
199 * #1: sig-no (common to any handler)
201 * #3: struct ucontext (completely populated)
203 if (unlikely(ksig
->ka
.sa
.sa_flags
& SA_SIGINFO
)) {
204 err
|= copy_siginfo_to_user(&sf
->info
, &ksig
->info
);
205 err
|= __put_user(0, &sf
->uc
.uc_flags
);
206 err
|= __put_user(NULL
, &sf
->uc
.uc_link
);
207 err
|= __save_altstack(&sf
->uc
.uc_stack
, regs
->sp
);
209 /* setup args 2 and 3 for user mode handler */
210 regs
->r1
= (unsigned long)&sf
->info
;
211 regs
->r2
= (unsigned long)&sf
->uc
;
214 * small optim to avoid unconditonally calling do_sigaltstack
215 * in sigreturn path, now that we only have rt_sigreturn
217 magic
= MAGIC_SIGALTSTK
;
220 err
|= __put_user(magic
, &sf
->sigret_magic
);
224 /* #1 arg to the user Signal handler */
225 regs
->r0
= map_sig(ksig
->sig
);
227 /* setup PC of user space signal handler */
228 regs
->ret
= (unsigned long)ksig
->ka
.sa
.sa_handler
;
231 * handler returns using sigreturn stub provided already by userpsace
233 BUG_ON(!(ksig
->ka
.sa
.sa_flags
& SA_RESTORER
));
234 regs
->blink
= (unsigned long)ksig
->ka
.sa
.sa_restorer
;
236 /* User Stack for signal handler will be above the frame just carved */
237 regs
->sp
= (unsigned long)sf
;
240 * Bug 94183, Clear the DE bit, so that when signal handler
241 * starts to run, it doesn't use BTA
243 regs
->status32
&= ~STATUS_DE_MASK
;
244 regs
->status32
|= STATUS_L_MASK
;
249 static void arc_restart_syscall(struct k_sigaction
*ka
, struct pt_regs
*regs
)
252 case -ERESTART_RESTARTBLOCK
:
253 case -ERESTARTNOHAND
:
255 * ERESTARTNOHAND means that the syscall should
256 * only be restarted if there was no handler for
257 * the signal, and since we only get here if there
258 * is a handler, we don't restart
260 regs
->r0
= -EINTR
; /* ERESTART_xxx is internal */
265 * ERESTARTSYS means to restart the syscall if
266 * there is no handler or the handler was
267 * registered with SA_RESTART
269 if (!(ka
->sa
.sa_flags
& SA_RESTART
)) {
275 case -ERESTARTNOINTR
:
277 * ERESTARTNOINTR means that the syscall should
278 * be called again after the signal handler returns.
279 * Setup reg state just as it was before doing the trap
280 * r0 has been clobbered with sys call ret code thus it
281 * needs to be reloaded with orig first arg to syscall
282 * in orig_r0. Rest of relevant reg-file:
283 * r8 (syscall num) and (r1 - r7) will be reset to
284 * their orig user space value when we ret from kernel
286 regs
->r0
= regs
->orig_r0
;
293 * OK, we're invoking a handler
296 handle_signal(struct ksignal
*ksig
, struct pt_regs
*regs
)
298 sigset_t
*oldset
= sigmask_to_save();
301 /* Set up the stack frame */
302 ret
= setup_rt_frame(ksig
, oldset
, regs
);
304 signal_setup_done(ret
, ksig
, 0);
307 void do_signal(struct pt_regs
*regs
)
312 restart_scall
= in_syscall(regs
) && syscall_restartable(regs
);
314 if (get_signal(&ksig
)) {
316 arc_restart_syscall(&ksig
.ka
, regs
);
317 syscall_wont_restart(regs
); /* No more restarts */
319 handle_signal(&ksig
, regs
);
324 /* No handler for syscall: restart it */
325 if (regs
->r0
== -ERESTARTNOHAND
||
326 regs
->r0
== -ERESTARTSYS
|| regs
->r0
== -ERESTARTNOINTR
) {
327 regs
->r0
= regs
->orig_r0
;
329 } else if (regs
->r0
== -ERESTART_RESTARTBLOCK
) {
330 regs
->r8
= __NR_restart_syscall
;
333 syscall_wont_restart(regs
); /* No more restarts */
336 /* If there's no signal to deliver, restore the saved sigmask back */
337 restore_saved_sigmask();
340 void do_notify_resume(struct pt_regs
*regs
)
343 * ASM glue gaurantees that this is only called when returning to
346 if (test_and_clear_thread_flag(TIF_NOTIFY_RESUME
))
347 tracehook_notify_resume(regs
);