Commit | Line | Data |
---|---|---|
c3581039 VG |
1 | /* |
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 | ||
58 | struct rt_sigframe { | |
59 | struct siginfo info; | |
60 | struct ucontext uc; | |
61 | #define MAGIC_SIGALTSTK 0x07302004 | |
62 | unsigned int sigret_magic; | |
63 | }; | |
64 | ||
65 | static int | |
66 | stash_usr_regs(struct rt_sigframe __user *sf, struct pt_regs *regs, | |
67 | sigset_t *set) | |
68 | { | |
69 | int err; | |
6914e1e3 | 70 | err = __copy_to_user(&(sf->uc.uc_mcontext.regs.scratch), regs, |
c3581039 VG |
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 | ||
77 | static 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 | ||
6914e1e3 | 86 | err |= __copy_from_user(regs, &(sf->uc.uc_mcontext.regs.scratch), |
c3581039 VG |
87 | sizeof(sf->uc.uc_mcontext.regs.scratch)); |
88 | ||
89 | return err; | |
90 | } | |
91 | ||
92 | static 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 | ||
100 | SYSCALL_DEFINE0(rt_sigreturn) | |
101 | { | |
102 | struct rt_sigframe __user *sf; | |
103 | unsigned int magic; | |
c3581039 VG |
104 | struct pt_regs *regs = current_pt_regs(); |
105 | ||
106 | /* Always make any pending restarted system calls return -EINTR */ | |
f56141e3 | 107 | current->restart_block.fn = do_no_restart_syscall; |
c3581039 VG |
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 | ||
10469350 | 121 | if (__get_user(magic, &sf->sigret_magic)) |
c3581039 VG |
122 | goto badframe; |
123 | ||
124 | if (unlikely(is_do_ss_needed(magic))) | |
125 | if (restore_altstack(&sf->uc.uc_stack)) | |
126 | goto badframe; | |
127 | ||
10469350 CR |
128 | if (restore_usr_regs(regs, sf)) |
129 | goto badframe; | |
130 | ||
55bb9480 VG |
131 | /* Don't restart from sigreturn */ |
132 | syscall_wont_restart(regs); | |
133 | ||
e4140819 VG |
134 | /* |
135 | * Ensure that sigreturn always returns to user mode (in case the | |
136 | * regs saved on user stack got fudged between save and sigreturn) | |
137 | * Otherwise it is easy to panic the kernel with a custom | |
138 | * signal handler and/or restorer which clobberes the status32/ret | |
139 | * to return to a bogus location in kernel mode. | |
140 | */ | |
141 | regs->status32 |= STATUS_U_MASK; | |
142 | ||
c3581039 VG |
143 | return regs->r0; |
144 | ||
145 | badframe: | |
146 | force_sig(SIGSEGV, current); | |
147 | return 0; | |
148 | } | |
149 | ||
150 | /* | |
151 | * Determine which stack to use.. | |
152 | */ | |
5290dd79 | 153 | static inline void __user *get_sigframe(struct ksignal *ksig, |
c3581039 VG |
154 | struct pt_regs *regs, |
155 | unsigned long framesize) | |
156 | { | |
5290dd79 | 157 | unsigned long sp = sigsp(regs->sp, ksig); |
c3581039 VG |
158 | void __user *frame; |
159 | ||
c3581039 VG |
160 | /* No matter what happens, 'sp' must be word |
161 | * aligned otherwise nasty things could happen | |
162 | */ | |
163 | ||
164 | /* ATPCS B01 mandates 8-byte alignment */ | |
165 | frame = (void __user *)((sp - framesize) & ~7); | |
166 | ||
167 | /* Check that we can actually write to the signal frame */ | |
168 | if (!access_ok(VERIFY_WRITE, frame, framesize)) | |
169 | frame = NULL; | |
170 | ||
171 | return frame; | |
172 | } | |
173 | ||
174 | /* | |
175 | * translate the signal | |
176 | */ | |
177 | static inline int map_sig(int sig) | |
178 | { | |
179 | struct thread_info *thread = current_thread_info(); | |
180 | if (thread->exec_domain && thread->exec_domain->signal_invmap | |
181 | && sig < 32) | |
182 | sig = thread->exec_domain->signal_invmap[sig]; | |
183 | return sig; | |
184 | } | |
185 | ||
186 | static int | |
f6dd2a3f | 187 | setup_rt_frame(struct ksignal *ksig, sigset_t *set, struct pt_regs *regs) |
c3581039 VG |
188 | { |
189 | struct rt_sigframe __user *sf; | |
190 | unsigned int magic = 0; | |
191 | int err = 0; | |
192 | ||
5290dd79 | 193 | sf = get_sigframe(ksig, regs, sizeof(struct rt_sigframe)); |
c3581039 VG |
194 | if (!sf) |
195 | return 1; | |
196 | ||
10469350 CR |
197 | /* |
198 | * w/o SA_SIGINFO, struct ucontext is partially populated (only | |
199 | * uc_mcontext/uc_sigmask) for kernel's normal user state preservation | |
200 | * during signal handler execution. This works for SA_SIGINFO as well | |
201 | * although the semantics are now overloaded (the same reg state can be | |
202 | * inspected by userland: but are they allowed to fiddle with it ? | |
203 | */ | |
204 | err |= stash_usr_regs(sf, regs, set); | |
205 | ||
c3581039 VG |
206 | /* |
207 | * SA_SIGINFO requires 3 args to signal handler: | |
208 | * #1: sig-no (common to any handler) | |
209 | * #2: struct siginfo | |
210 | * #3: struct ucontext (completely populated) | |
211 | */ | |
f6dd2a3f RW |
212 | if (unlikely(ksig->ka.sa.sa_flags & SA_SIGINFO)) { |
213 | err |= copy_siginfo_to_user(&sf->info, &ksig->info); | |
c3581039 VG |
214 | err |= __put_user(0, &sf->uc.uc_flags); |
215 | err |= __put_user(NULL, &sf->uc.uc_link); | |
216 | err |= __save_altstack(&sf->uc.uc_stack, regs->sp); | |
217 | ||
218 | /* setup args 2 and 3 for user mode handler */ | |
219 | regs->r1 = (unsigned long)&sf->info; | |
220 | regs->r2 = (unsigned long)&sf->uc; | |
221 | ||
222 | /* | |
223 | * small optim to avoid unconditonally calling do_sigaltstack | |
224 | * in sigreturn path, now that we only have rt_sigreturn | |
225 | */ | |
226 | magic = MAGIC_SIGALTSTK; | |
227 | } | |
228 | ||
c3581039 VG |
229 | err |= __put_user(magic, &sf->sigret_magic); |
230 | if (err) | |
231 | return err; | |
232 | ||
233 | /* #1 arg to the user Signal handler */ | |
f6dd2a3f | 234 | regs->r0 = map_sig(ksig->sig); |
c3581039 VG |
235 | |
236 | /* setup PC of user space signal handler */ | |
f6dd2a3f | 237 | regs->ret = (unsigned long)ksig->ka.sa.sa_handler; |
c3581039 VG |
238 | |
239 | /* | |
240 | * handler returns using sigreturn stub provided already by userpsace | |
e4140819 | 241 | * If not, nuke the process right away |
c3581039 | 242 | */ |
e4140819 VG |
243 | if(!(ksig->ka.sa.sa_flags & SA_RESTORER)) |
244 | return 1; | |
245 | ||
f6dd2a3f | 246 | regs->blink = (unsigned long)ksig->ka.sa.sa_restorer; |
c3581039 VG |
247 | |
248 | /* User Stack for signal handler will be above the frame just carved */ | |
249 | regs->sp = (unsigned long)sf; | |
250 | ||
251 | /* | |
252 | * Bug 94183, Clear the DE bit, so that when signal handler | |
253 | * starts to run, it doesn't use BTA | |
254 | */ | |
255 | regs->status32 &= ~STATUS_DE_MASK; | |
256 | regs->status32 |= STATUS_L_MASK; | |
257 | ||
258 | return err; | |
259 | } | |
260 | ||
261 | static void arc_restart_syscall(struct k_sigaction *ka, struct pt_regs *regs) | |
262 | { | |
263 | switch (regs->r0) { | |
264 | case -ERESTART_RESTARTBLOCK: | |
265 | case -ERESTARTNOHAND: | |
266 | /* | |
267 | * ERESTARTNOHAND means that the syscall should | |
268 | * only be restarted if there was no handler for | |
269 | * the signal, and since we only get here if there | |
270 | * is a handler, we don't restart | |
271 | */ | |
272 | regs->r0 = -EINTR; /* ERESTART_xxx is internal */ | |
273 | break; | |
274 | ||
275 | case -ERESTARTSYS: | |
276 | /* | |
277 | * ERESTARTSYS means to restart the syscall if | |
278 | * there is no handler or the handler was | |
279 | * registered with SA_RESTART | |
280 | */ | |
281 | if (!(ka->sa.sa_flags & SA_RESTART)) { | |
282 | regs->r0 = -EINTR; | |
283 | break; | |
284 | } | |
285 | /* fallthrough */ | |
286 | ||
287 | case -ERESTARTNOINTR: | |
288 | /* | |
289 | * ERESTARTNOINTR means that the syscall should | |
290 | * be called again after the signal handler returns. | |
291 | * Setup reg state just as it was before doing the trap | |
292 | * r0 has been clobbered with sys call ret code thus it | |
293 | * needs to be reloaded with orig first arg to syscall | |
294 | * in orig_r0. Rest of relevant reg-file: | |
295 | * r8 (syscall num) and (r1 - r7) will be reset to | |
296 | * their orig user space value when we ret from kernel | |
297 | */ | |
298 | regs->r0 = regs->orig_r0; | |
299 | regs->ret -= 4; | |
300 | break; | |
301 | } | |
302 | } | |
303 | ||
304 | /* | |
305 | * OK, we're invoking a handler | |
306 | */ | |
307 | static void | |
f6dd2a3f | 308 | handle_signal(struct ksignal *ksig, struct pt_regs *regs) |
c3581039 VG |
309 | { |
310 | sigset_t *oldset = sigmask_to_save(); | |
e4140819 | 311 | int failed; |
c3581039 VG |
312 | |
313 | /* Set up the stack frame */ | |
e4140819 | 314 | failed = setup_rt_frame(ksig, oldset, regs); |
c3581039 | 315 | |
e4140819 | 316 | signal_setup_done(failed, ksig, 0); |
c3581039 VG |
317 | } |
318 | ||
319 | void do_signal(struct pt_regs *regs) | |
320 | { | |
f6dd2a3f | 321 | struct ksignal ksig; |
c3581039 VG |
322 | int restart_scall; |
323 | ||
55bb9480 | 324 | restart_scall = in_syscall(regs) && syscall_restartable(regs); |
c3581039 | 325 | |
f6dd2a3f | 326 | if (get_signal(&ksig)) { |
55bb9480 | 327 | if (restart_scall) { |
f6dd2a3f | 328 | arc_restart_syscall(&ksig.ka, regs); |
55bb9480 VG |
329 | syscall_wont_restart(regs); /* No more restarts */ |
330 | } | |
f6dd2a3f | 331 | handle_signal(&ksig, regs); |
c3581039 VG |
332 | return; |
333 | } | |
334 | ||
335 | if (restart_scall) { | |
336 | /* No handler for syscall: restart it */ | |
337 | if (regs->r0 == -ERESTARTNOHAND || | |
338 | regs->r0 == -ERESTARTSYS || regs->r0 == -ERESTARTNOINTR) { | |
339 | regs->r0 = regs->orig_r0; | |
340 | regs->ret -= 4; | |
341 | } else if (regs->r0 == -ERESTART_RESTARTBLOCK) { | |
342 | regs->r8 = __NR_restart_syscall; | |
343 | regs->ret -= 4; | |
344 | } | |
55bb9480 | 345 | syscall_wont_restart(regs); /* No more restarts */ |
c3581039 VG |
346 | } |
347 | ||
348 | /* If there's no signal to deliver, restore the saved sigmask back */ | |
349 | restore_saved_sigmask(); | |
350 | } | |
351 | ||
352 | void do_notify_resume(struct pt_regs *regs) | |
353 | { | |
354 | /* | |
355 | * ASM glue gaurantees that this is only called when returning to | |
356 | * user mode | |
357 | */ | |
358 | if (test_and_clear_thread_flag(TIF_NOTIFY_RESUME)) | |
359 | tracehook_notify_resume(regs); | |
360 | } |