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; | |
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 | ||
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 | ||
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 | ||
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 */ | |
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 | ||
19a42003 | 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 | ||
19a42003 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 | ||
eb7b2163 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 | */ | |
153 | static inline void __user *get_sigframe(struct k_sigaction *ka, | |
154 | struct pt_regs *regs, | |
155 | unsigned long framesize) | |
156 | { | |
157 | unsigned long sp = regs->sp; | |
158 | void __user *frame; | |
159 | ||
160 | /* This is the X/Open sanctioned signal stack switching */ | |
161 | if ((ka->sa.sa_flags & SA_ONSTACK) && !sas_ss_flags(sp)) | |
162 | sp = current->sas_ss_sp + current->sas_ss_size; | |
163 | ||
164 | /* No matter what happens, 'sp' must be word | |
165 | * aligned otherwise nasty things could happen | |
166 | */ | |
167 | ||
168 | /* ATPCS B01 mandates 8-byte alignment */ | |
169 | frame = (void __user *)((sp - framesize) & ~7); | |
170 | ||
171 | /* Check that we can actually write to the signal frame */ | |
172 | if (!access_ok(VERIFY_WRITE, frame, framesize)) | |
173 | frame = NULL; | |
174 | ||
175 | return frame; | |
176 | } | |
177 | ||
178 | /* | |
179 | * translate the signal | |
180 | */ | |
181 | static inline int map_sig(int sig) | |
182 | { | |
183 | struct thread_info *thread = current_thread_info(); | |
184 | if (thread->exec_domain && thread->exec_domain->signal_invmap | |
185 | && sig < 32) | |
186 | sig = thread->exec_domain->signal_invmap[sig]; | |
187 | return sig; | |
188 | } | |
189 | ||
190 | static int | |
191 | setup_rt_frame(int signo, struct k_sigaction *ka, siginfo_t *info, | |
192 | sigset_t *set, struct pt_regs *regs) | |
193 | { | |
194 | struct rt_sigframe __user *sf; | |
195 | unsigned int magic = 0; | |
196 | int err = 0; | |
197 | ||
198 | sf = get_sigframe(ka, regs, sizeof(struct rt_sigframe)); | |
199 | if (!sf) | |
200 | return 1; | |
201 | ||
19a42003 CR |
202 | /* |
203 | * w/o SA_SIGINFO, struct ucontext is partially populated (only | |
204 | * uc_mcontext/uc_sigmask) for kernel's normal user state preservation | |
205 | * during signal handler execution. This works for SA_SIGINFO as well | |
206 | * although the semantics are now overloaded (the same reg state can be | |
207 | * inspected by userland: but are they allowed to fiddle with it ? | |
208 | */ | |
209 | err |= stash_usr_regs(sf, regs, set); | |
210 | ||
c3581039 VG |
211 | /* |
212 | * SA_SIGINFO requires 3 args to signal handler: | |
213 | * #1: sig-no (common to any handler) | |
214 | * #2: struct siginfo | |
215 | * #3: struct ucontext (completely populated) | |
216 | */ | |
217 | if (unlikely(ka->sa.sa_flags & SA_SIGINFO)) { | |
218 | err |= copy_siginfo_to_user(&sf->info, info); | |
219 | err |= __put_user(0, &sf->uc.uc_flags); | |
220 | err |= __put_user(NULL, &sf->uc.uc_link); | |
221 | err |= __save_altstack(&sf->uc.uc_stack, regs->sp); | |
222 | ||
223 | /* setup args 2 and 3 for user mode handler */ | |
224 | regs->r1 = (unsigned long)&sf->info; | |
225 | regs->r2 = (unsigned long)&sf->uc; | |
226 | ||
227 | /* | |
228 | * small optim to avoid unconditonally calling do_sigaltstack | |
229 | * in sigreturn path, now that we only have rt_sigreturn | |
230 | */ | |
231 | magic = MAGIC_SIGALTSTK; | |
232 | } | |
233 | ||
c3581039 VG |
234 | err |= __put_user(magic, &sf->sigret_magic); |
235 | if (err) | |
236 | return err; | |
237 | ||
238 | /* #1 arg to the user Signal handler */ | |
239 | regs->r0 = map_sig(signo); | |
240 | ||
241 | /* setup PC of user space signal handler */ | |
242 | regs->ret = (unsigned long)ka->sa.sa_handler; | |
243 | ||
244 | /* | |
245 | * handler returns using sigreturn stub provided already by userpsace | |
eb7b2163 | 246 | * If not, nuke the process right away |
c3581039 | 247 | */ |
eb7b2163 VG |
248 | if(!(ka->sa.sa_flags & SA_RESTORER)) |
249 | return 1; | |
250 | ||
c3581039 VG |
251 | regs->blink = (unsigned long)ka->sa.sa_restorer; |
252 | ||
253 | /* User Stack for signal handler will be above the frame just carved */ | |
254 | regs->sp = (unsigned long)sf; | |
255 | ||
256 | /* | |
257 | * Bug 94183, Clear the DE bit, so that when signal handler | |
258 | * starts to run, it doesn't use BTA | |
259 | */ | |
260 | regs->status32 &= ~STATUS_DE_MASK; | |
261 | regs->status32 |= STATUS_L_MASK; | |
262 | ||
263 | return err; | |
264 | } | |
265 | ||
266 | static void arc_restart_syscall(struct k_sigaction *ka, struct pt_regs *regs) | |
267 | { | |
268 | switch (regs->r0) { | |
269 | case -ERESTART_RESTARTBLOCK: | |
270 | case -ERESTARTNOHAND: | |
271 | /* | |
272 | * ERESTARTNOHAND means that the syscall should | |
273 | * only be restarted if there was no handler for | |
274 | * the signal, and since we only get here if there | |
275 | * is a handler, we don't restart | |
276 | */ | |
277 | regs->r0 = -EINTR; /* ERESTART_xxx is internal */ | |
278 | break; | |
279 | ||
280 | case -ERESTARTSYS: | |
281 | /* | |
282 | * ERESTARTSYS means to restart the syscall if | |
283 | * there is no handler or the handler was | |
284 | * registered with SA_RESTART | |
285 | */ | |
286 | if (!(ka->sa.sa_flags & SA_RESTART)) { | |
287 | regs->r0 = -EINTR; | |
288 | break; | |
289 | } | |
290 | /* fallthrough */ | |
291 | ||
292 | case -ERESTARTNOINTR: | |
293 | /* | |
294 | * ERESTARTNOINTR means that the syscall should | |
295 | * be called again after the signal handler returns. | |
296 | * Setup reg state just as it was before doing the trap | |
297 | * r0 has been clobbered with sys call ret code thus it | |
298 | * needs to be reloaded with orig first arg to syscall | |
299 | * in orig_r0. Rest of relevant reg-file: | |
300 | * r8 (syscall num) and (r1 - r7) will be reset to | |
301 | * their orig user space value when we ret from kernel | |
302 | */ | |
303 | regs->r0 = regs->orig_r0; | |
304 | regs->ret -= 4; | |
305 | break; | |
306 | } | |
307 | } | |
308 | ||
309 | /* | |
310 | * OK, we're invoking a handler | |
311 | */ | |
312 | static void | |
313 | handle_signal(unsigned long sig, struct k_sigaction *ka, siginfo_t *info, | |
314 | struct pt_regs *regs) | |
315 | { | |
316 | sigset_t *oldset = sigmask_to_save(); | |
eb7b2163 | 317 | int failed; |
c3581039 VG |
318 | |
319 | /* Set up the stack frame */ | |
eb7b2163 | 320 | failed = setup_rt_frame(sig, ka, info, oldset, regs); |
c3581039 | 321 | |
eb7b2163 | 322 | if (failed) |
c3581039 VG |
323 | force_sigsegv(sig, current); |
324 | else | |
325 | signal_delivered(sig, info, ka, regs, 0); | |
326 | } | |
327 | ||
328 | void do_signal(struct pt_regs *regs) | |
329 | { | |
330 | struct k_sigaction ka; | |
331 | siginfo_t info; | |
332 | int signr; | |
333 | int restart_scall; | |
334 | ||
335 | signr = get_signal_to_deliver(&info, &ka, regs, NULL); | |
336 | ||
55bb9480 | 337 | restart_scall = in_syscall(regs) && syscall_restartable(regs); |
c3581039 VG |
338 | |
339 | if (signr > 0) { | |
55bb9480 | 340 | if (restart_scall) { |
c3581039 | 341 | arc_restart_syscall(&ka, regs); |
55bb9480 VG |
342 | syscall_wont_restart(regs); /* No more restarts */ |
343 | } | |
c3581039 VG |
344 | handle_signal(signr, &ka, &info, regs); |
345 | return; | |
346 | } | |
347 | ||
348 | if (restart_scall) { | |
349 | /* No handler for syscall: restart it */ | |
350 | if (regs->r0 == -ERESTARTNOHAND || | |
351 | regs->r0 == -ERESTARTSYS || regs->r0 == -ERESTARTNOINTR) { | |
352 | regs->r0 = regs->orig_r0; | |
353 | regs->ret -= 4; | |
354 | } else if (regs->r0 == -ERESTART_RESTARTBLOCK) { | |
355 | regs->r8 = __NR_restart_syscall; | |
356 | regs->ret -= 4; | |
357 | } | |
55bb9480 | 358 | syscall_wont_restart(regs); /* No more restarts */ |
c3581039 VG |
359 | } |
360 | ||
361 | /* If there's no signal to deliver, restore the saved sigmask back */ | |
362 | restore_saved_sigmask(); | |
363 | } | |
364 | ||
365 | void do_notify_resume(struct pt_regs *regs) | |
366 | { | |
367 | /* | |
368 | * ASM glue gaurantees that this is only called when returning to | |
369 | * user mode | |
370 | */ | |
371 | if (test_and_clear_thread_flag(TIF_NOTIFY_RESUME)) | |
372 | tracehook_notify_resume(regs); | |
373 | } |