Commit | Line | Data |
---|---|---|
51533b61 MS |
1 | /* |
2 | * Copyright (C) 2003, Axis Communications AB. | |
3 | */ | |
4 | ||
5 | #include <linux/sched.h> | |
6 | #include <linux/mm.h> | |
5a0e3ad6 | 7 | #include <linux/slab.h> |
51533b61 MS |
8 | #include <linux/kernel.h> |
9 | #include <linux/signal.h> | |
10 | #include <linux/errno.h> | |
11 | #include <linux/wait.h> | |
12 | #include <linux/ptrace.h> | |
13 | #include <linux/unistd.h> | |
14 | #include <linux/stddef.h> | |
15 | #include <linux/syscalls.h> | |
16 | #include <linux/vmalloc.h> | |
17 | ||
18 | #include <asm/io.h> | |
19 | #include <asm/processor.h> | |
20 | #include <asm/ucontext.h> | |
7c0f6ba6 | 21 | #include <linux/uaccess.h> |
556dcee7 | 22 | #include <arch/hwregs/cpu_vect.h> |
51533b61 MS |
23 | |
24 | extern unsigned long cris_signal_return_page; | |
25 | ||
51533b61 MS |
26 | /* |
27 | * A syscall in CRIS is really a "break 13" instruction, which is 2 | |
28 | * bytes. The registers is manipulated so upon return the instruction | |
29 | * will be executed again. | |
30 | * | |
31 | * This relies on that PC points to the instruction after the break call. | |
32 | */ | |
33 | #define RESTART_CRIS_SYS(regs) regs->r10 = regs->orig_r10; regs->erp -= 2; | |
34 | ||
35 | /* Signal frames. */ | |
36 | struct signal_frame { | |
37 | struct sigcontext sc; | |
38 | unsigned long extramask[_NSIG_WORDS - 1]; | |
39 | unsigned char retcode[8]; /* Trampoline code. */ | |
40 | }; | |
41 | ||
42 | struct rt_signal_frame { | |
43 | struct siginfo *pinfo; | |
44 | void *puc; | |
45 | struct siginfo info; | |
46 | struct ucontext uc; | |
47 | unsigned char retcode[8]; /* Trampoline code. */ | |
48 | }; | |
49 | ||
574852a2 | 50 | void do_signal(int restart, struct pt_regs *regs); |
51533b61 MS |
51 | void keep_debug_flags(unsigned long oldccs, unsigned long oldspc, |
52 | struct pt_regs *regs); | |
51533b61 | 53 | |
51533b61 MS |
54 | static int |
55 | restore_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc) | |
56 | { | |
57 | unsigned int err = 0; | |
58 | unsigned long old_usp; | |
59 | ||
60 | /* Always make any pending restarted system calls return -EINTR */ | |
f56141e3 | 61 | current->restart_block.fn = do_no_restart_syscall; |
51533b61 MS |
62 | |
63 | /* | |
64 | * Restore the registers from &sc->regs. sc is already checked | |
65 | * for VERIFY_READ since the signal_frame was previously | |
66 | * checked in sys_sigreturn(). | |
67 | */ | |
68 | if (__copy_from_user(regs, sc, sizeof(struct pt_regs))) | |
69 | goto badframe; | |
70 | ||
71 | /* Make that the user-mode flag is set. */ | |
72 | regs->ccs |= (1 << (U_CCS_BITNR + CCS_SHIFT)); | |
73 | ||
0f72e5c0 RV |
74 | /* Don't perform syscall restarting */ |
75 | regs->exs = -1; | |
76 | ||
51533b61 MS |
77 | /* Restore the old USP. */ |
78 | err |= __get_user(old_usp, &sc->usp); | |
79 | wrusp(old_usp); | |
80 | ||
81 | return err; | |
82 | ||
83 | badframe: | |
84 | return 1; | |
85 | } | |
86 | ||
e6a6d210 | 87 | asmlinkage int sys_sigreturn(void) |
51533b61 | 88 | { |
e6a6d210 | 89 | struct pt_regs *regs = current_pt_regs(); |
51533b61 MS |
90 | sigset_t set; |
91 | struct signal_frame __user *frame; | |
92 | unsigned long oldspc = regs->spc; | |
93 | unsigned long oldccs = regs->ccs; | |
94 | ||
95 | frame = (struct signal_frame *) rdusp(); | |
96 | ||
97 | /* | |
98 | * Since the signal is stacked on a dword boundary, the frame | |
99 | * should be dword aligned here as well. It it's not, then the | |
100 | * user is trying some funny business. | |
101 | */ | |
102 | if (((long)frame) & 3) | |
103 | goto badframe; | |
104 | ||
105 | if (!access_ok(VERIFY_READ, frame, sizeof(*frame))) | |
106 | goto badframe; | |
107 | ||
108 | if (__get_user(set.sig[0], &frame->sc.oldmask) || | |
109 | (_NSIG_WORDS > 1 && __copy_from_user(&set.sig[1], | |
110 | frame->extramask, | |
111 | sizeof(frame->extramask)))) | |
112 | goto badframe; | |
113 | ||
f3b5e822 | 114 | set_current_blocked(&set); |
51533b61 MS |
115 | |
116 | if (restore_sigcontext(regs, &frame->sc)) | |
117 | goto badframe; | |
118 | ||
119 | keep_debug_flags(oldccs, oldspc, regs); | |
120 | ||
121 | return regs->r10; | |
122 | ||
123 | badframe: | |
124 | force_sig(SIGSEGV, current); | |
125 | return 0; | |
126 | } | |
127 | ||
e6a6d210 | 128 | asmlinkage int sys_rt_sigreturn(void) |
51533b61 | 129 | { |
e6a6d210 | 130 | struct pt_regs *regs = current_pt_regs(); |
51533b61 MS |
131 | sigset_t set; |
132 | struct rt_signal_frame __user *frame; | |
133 | unsigned long oldspc = regs->spc; | |
134 | unsigned long oldccs = regs->ccs; | |
135 | ||
136 | frame = (struct rt_signal_frame *) rdusp(); | |
137 | ||
138 | /* | |
139 | * Since the signal is stacked on a dword boundary, the frame | |
140 | * should be dword aligned here as well. It it's not, then the | |
141 | * user is trying some funny business. | |
142 | */ | |
143 | if (((long)frame) & 3) | |
144 | goto badframe; | |
145 | ||
146 | if (!access_ok(VERIFY_READ, frame, sizeof(*frame))) | |
147 | goto badframe; | |
148 | ||
149 | if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set))) | |
150 | goto badframe; | |
151 | ||
f3b5e822 | 152 | set_current_blocked(&set); |
51533b61 MS |
153 | |
154 | if (restore_sigcontext(regs, &frame->uc.uc_mcontext)) | |
155 | goto badframe; | |
156 | ||
d970e428 | 157 | if (restore_altstack(&frame->uc.uc_stack)) |
574852a2 | 158 | goto badframe; |
51533b61 MS |
159 | |
160 | keep_debug_flags(oldccs, oldspc, regs); | |
161 | ||
162 | return regs->r10; | |
163 | ||
164 | badframe: | |
165 | force_sig(SIGSEGV, current); | |
166 | return 0; | |
167 | } | |
168 | ||
169 | /* Setup a signal frame. */ | |
170 | static int | |
171 | setup_sigcontext(struct sigcontext __user *sc, struct pt_regs *regs, | |
172 | unsigned long mask) | |
173 | { | |
174 | int err; | |
175 | unsigned long usp; | |
176 | ||
177 | err = 0; | |
178 | usp = rdusp(); | |
179 | ||
180 | /* | |
181 | * Copy the registers. They are located first in sc, so it's | |
182 | * possible to use sc directly. | |
183 | */ | |
184 | err |= __copy_to_user(sc, regs, sizeof(struct pt_regs)); | |
185 | ||
186 | err |= __put_user(mask, &sc->oldmask); | |
187 | err |= __put_user(usp, &sc->usp); | |
188 | ||
189 | return err; | |
190 | } | |
191 | ||
192 | /* Figure out where to put the new signal frame - usually on the stack. */ | |
193 | static inline void __user * | |
8215ade8 | 194 | get_sigframe(struct ksignal *ksig, size_t frame_size) |
51533b61 | 195 | { |
8215ade8 | 196 | unsigned long sp = sigsp(rdusp(), ksig); |
51533b61 MS |
197 | |
198 | /* Make sure the frame is dword-aligned. */ | |
199 | sp &= ~3; | |
200 | ||
201 | return (void __user *)(sp - frame_size); | |
202 | } | |
203 | ||
204 | /* Grab and setup a signal frame. | |
205 | * | |
206 | * Basically a lot of state-info is stacked, and arranged for the | |
574852a2 | 207 | * user-mode program to return to the kernel using either a trampiline |
51533b61 MS |
208 | * which performs the syscall sigreturn(), or a provided user-mode |
209 | * trampoline. | |
210 | */ | |
574852a2 | 211 | static int |
fa019772 | 212 | setup_frame(struct ksignal *ksig, sigset_t *set, struct pt_regs *regs) |
51533b61 MS |
213 | { |
214 | int err; | |
215 | unsigned long return_ip; | |
216 | struct signal_frame __user *frame; | |
217 | ||
218 | err = 0; | |
8215ade8 | 219 | frame = get_sigframe(ksig, sizeof(*frame)); |
51533b61 MS |
220 | |
221 | if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame))) | |
fa019772 | 222 | return -EFAULT; |
51533b61 MS |
223 | |
224 | err |= setup_sigcontext(&frame->sc, regs, set->sig[0]); | |
225 | ||
226 | if (err) | |
fa019772 | 227 | return -EFAULT; |
51533b61 MS |
228 | |
229 | if (_NSIG_WORDS > 1) { | |
230 | err |= __copy_to_user(frame->extramask, &set->sig[1], | |
231 | sizeof(frame->extramask)); | |
232 | } | |
233 | ||
234 | if (err) | |
fa019772 | 235 | return -EFAULT; |
51533b61 MS |
236 | |
237 | /* | |
238 | * Set up to return from user-space. If provided, use a stub | |
239 | * already located in user-space. | |
240 | */ | |
fa019772 RW |
241 | if (ksig->ka.sa.sa_flags & SA_RESTORER) { |
242 | return_ip = (unsigned long)ksig->ka.sa.sa_restorer; | |
51533b61 MS |
243 | } else { |
244 | /* Trampoline - the desired return ip is in the signal return page. */ | |
245 | return_ip = cris_signal_return_page; | |
246 | ||
247 | /* | |
248 | * This is movu.w __NR_sigreturn, r9; break 13; | |
249 | * | |
250 | * WE DO NOT USE IT ANY MORE! It's only left here for historical | |
251 | * reasons and because gdb uses it as a signature to notice | |
252 | * signal handler stack frames. | |
253 | */ | |
254 | err |= __put_user(0x9c5f, (short __user*)(frame->retcode+0)); | |
255 | err |= __put_user(__NR_sigreturn, (short __user*)(frame->retcode+2)); | |
256 | err |= __put_user(0xe93d, (short __user*)(frame->retcode+4)); | |
257 | } | |
258 | ||
259 | if (err) | |
fa019772 | 260 | return -EFAULT; |
51533b61 MS |
261 | |
262 | /* | |
263 | * Set up registers for signal handler. | |
264 | * | |
265 | * Where the code enters now. | |
266 | * Where the code enter later. | |
267 | * First argument, signo. | |
268 | */ | |
fa019772 | 269 | regs->erp = (unsigned long) ksig->ka.sa.sa_handler; |
51533b61 | 270 | regs->srp = return_ip; |
fa019772 | 271 | regs->r10 = ksig->sig; |
51533b61 MS |
272 | |
273 | /* Actually move the USP to reflect the stacked frame. */ | |
274 | wrusp((unsigned long)frame); | |
275 | ||
574852a2 | 276 | return 0; |
51533b61 MS |
277 | } |
278 | ||
574852a2 | 279 | static int |
fa019772 | 280 | setup_rt_frame(struct ksignal *ksig, sigset_t *set, struct pt_regs *regs) |
51533b61 MS |
281 | { |
282 | int err; | |
283 | unsigned long return_ip; | |
284 | struct rt_signal_frame __user *frame; | |
285 | ||
286 | err = 0; | |
8215ade8 | 287 | frame = get_sigframe(ksig, sizeof(*frame)); |
51533b61 MS |
288 | |
289 | if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame))) | |
fa019772 | 290 | return -EFAULT; |
51533b61 | 291 | |
51533b61 MS |
292 | err |= __put_user(&frame->info, &frame->pinfo); |
293 | err |= __put_user(&frame->uc, &frame->puc); | |
fa019772 | 294 | err |= copy_siginfo_to_user(&frame->info, &ksig->info); |
51533b61 MS |
295 | |
296 | if (err) | |
fa019772 | 297 | return -EFAULT; |
51533b61 MS |
298 | |
299 | /* Clear all the bits of the ucontext we don't use. */ | |
300 | err |= __clear_user(&frame->uc, offsetof(struct ucontext, uc_mcontext)); | |
301 | err |= setup_sigcontext(&frame->uc.uc_mcontext, regs, set->sig[0]); | |
302 | err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set)); | |
9df794d9 | 303 | err |= __save_altstack(&frame->uc.uc_stack, rdusp()); |
51533b61 MS |
304 | |
305 | if (err) | |
fa019772 | 306 | return -EFAULT; |
51533b61 MS |
307 | |
308 | /* | |
309 | * Set up to return from user-space. If provided, use a stub | |
310 | * already located in user-space. | |
311 | */ | |
fa019772 RW |
312 | if (ksig->ka.sa.sa_flags & SA_RESTORER) { |
313 | return_ip = (unsigned long) ksig->ka.sa.sa_restorer; | |
51533b61 MS |
314 | } else { |
315 | /* Trampoline - the desired return ip is in the signal return page. */ | |
316 | return_ip = cris_signal_return_page + 6; | |
317 | ||
318 | /* | |
319 | * This is movu.w __NR_rt_sigreturn, r9; break 13; | |
320 | * | |
321 | * WE DO NOT USE IT ANY MORE! It's only left here for historical | |
322 | * reasons and because gdb uses it as a signature to notice | |
323 | * signal handler stack frames. | |
324 | */ | |
325 | err |= __put_user(0x9c5f, (short __user*)(frame->retcode+0)); | |
326 | ||
327 | err |= __put_user(__NR_rt_sigreturn, | |
328 | (short __user*)(frame->retcode+2)); | |
329 | ||
330 | err |= __put_user(0xe93d, (short __user*)(frame->retcode+4)); | |
331 | } | |
332 | ||
333 | if (err) | |
fa019772 | 334 | return -EFAULT; |
51533b61 MS |
335 | |
336 | /* | |
337 | * Set up registers for signal handler. | |
338 | * | |
339 | * Where the code enters now. | |
340 | * Where the code enters later. | |
341 | * First argument is signo. | |
342 | * Second argument is (siginfo_t *). | |
343 | * Third argument is unused. | |
344 | */ | |
fa019772 | 345 | regs->erp = (unsigned long) ksig->ka.sa.sa_handler; |
51533b61 | 346 | regs->srp = return_ip; |
fa019772 | 347 | regs->r10 = ksig->sig; |
51533b61 MS |
348 | regs->r11 = (unsigned long) &frame->info; |
349 | regs->r12 = 0; | |
350 | ||
351 | /* Actually move the usp to reflect the stacked frame. */ | |
352 | wrusp((unsigned long)frame); | |
353 | ||
574852a2 | 354 | return 0; |
51533b61 MS |
355 | } |
356 | ||
c03264a7 | 357 | /* Invoke a signal handler to, well, handle the signal. */ |
a610d6e6 | 358 | static inline void |
fa019772 | 359 | handle_signal(int canrestart, struct ksignal *ksig, struct pt_regs *regs) |
51533b61 | 360 | { |
b7f9a11a | 361 | sigset_t *oldset = sigmask_to_save(); |
574852a2 JN |
362 | int ret; |
363 | ||
51533b61 MS |
364 | /* Check if this got called from a system call. */ |
365 | if (canrestart) { | |
366 | /* If so, check system call restarting. */ | |
367 | switch (regs->r10) { | |
368 | case -ERESTART_RESTARTBLOCK: | |
369 | case -ERESTARTNOHAND: | |
370 | /* | |
371 | * This means that the syscall should | |
372 | * only be restarted if there was no | |
373 | * handler for the signal, and since | |
374 | * this point isn't reached unless | |
375 | * there is a handler, there's no need | |
376 | * to restart. | |
377 | */ | |
378 | regs->r10 = -EINTR; | |
379 | break; | |
380 | ||
381 | case -ERESTARTSYS: | |
382 | /* | |
383 | * This means restart the syscall if | |
384 | * there is no handler, or the handler | |
385 | * was registered with SA_RESTART. | |
386 | */ | |
fa019772 | 387 | if (!(ksig->ka.sa.sa_flags & SA_RESTART)) { |
51533b61 MS |
388 | regs->r10 = -EINTR; |
389 | break; | |
390 | } | |
391 | ||
392 | /* Fall through. */ | |
393 | ||
394 | case -ERESTARTNOINTR: | |
395 | /* | |
396 | * This means that the syscall should | |
397 | * be called again after the signal | |
398 | * handler returns. | |
399 | */ | |
400 | RESTART_CRIS_SYS(regs); | |
401 | break; | |
402 | } | |
403 | } | |
404 | ||
405 | /* Set up the stack frame. */ | |
fa019772 RW |
406 | if (ksig->ka.sa.sa_flags & SA_SIGINFO) |
407 | ret = setup_rt_frame(ksig, oldset, regs); | |
51533b61 | 408 | else |
fa019772 | 409 | ret = setup_frame(ksig, oldset, regs); |
51533b61 | 410 | |
fa019772 | 411 | signal_setup_done(ret, ksig, 0); |
51533b61 MS |
412 | } |
413 | ||
414 | /* | |
415 | * Note that 'init' is a special process: it doesn't get signals it doesn't | |
416 | * want to handle. Thus you cannot kill init even with a SIGKILL even by | |
417 | * mistake. | |
418 | * | |
419 | * Also note that the regs structure given here as an argument, is the latest | |
420 | * pushed pt_regs. It may or may not be the same as the first pushed registers | |
421 | * when the initial usermode->kernelmode transition took place. Therefore | |
422 | * we can use user_mode(regs) to see if we came directly from kernel or user | |
423 | * mode below. | |
424 | */ | |
574852a2 JN |
425 | void |
426 | do_signal(int canrestart, struct pt_regs *regs) | |
51533b61 | 427 | { |
fa019772 | 428 | struct ksignal ksig; |
51533b61 | 429 | |
0f72e5c0 RV |
430 | canrestart = canrestart && ((int)regs->exs >= 0); |
431 | ||
51533b61 MS |
432 | /* |
433 | * The common case should go fast, which is why this point is | |
434 | * reached from kernel-mode. If that's the case, just return | |
435 | * without doing anything. | |
436 | */ | |
437 | if (!user_mode(regs)) | |
574852a2 | 438 | return; |
51533b61 | 439 | |
fa019772 | 440 | if (get_signal(&ksig)) { |
574852a2 | 441 | /* Whee! Actually deliver the signal. */ |
fa019772 | 442 | handle_signal(canrestart, &ksig, regs); |
574852a2 | 443 | return; |
51533b61 MS |
444 | } |
445 | ||
446 | /* Got here from a system call? */ | |
447 | if (canrestart) { | |
448 | /* Restart the system call - no handlers present. */ | |
449 | if (regs->r10 == -ERESTARTNOHAND || | |
450 | regs->r10 == -ERESTARTSYS || | |
451 | regs->r10 == -ERESTARTNOINTR) { | |
452 | RESTART_CRIS_SYS(regs); | |
453 | } | |
454 | ||
455 | if (regs->r10 == -ERESTART_RESTARTBLOCK){ | |
39923326 | 456 | regs->r9 = __NR_restart_syscall; |
51533b61 MS |
457 | regs->erp -= 2; |
458 | } | |
459 | } | |
460 | ||
574852a2 JN |
461 | /* if there's no signal to deliver, we just put the saved sigmask |
462 | * back */ | |
51a7b448 | 463 | restore_saved_sigmask(); |
51533b61 MS |
464 | } |
465 | ||
466 | asmlinkage void | |
467 | ugdb_trap_user(struct thread_info *ti, int sig) | |
468 | { | |
469 | if (((user_regs(ti)->exs & 0xff00) >> 8) != SINGLE_STEP_INTR_VECT) { | |
470 | /* Zero single-step PC if the reason we stopped wasn't a single | |
471 | step exception. This is to avoid relying on it when it isn't | |
472 | reliable. */ | |
473 | user_regs(ti)->spc = 0; | |
474 | } | |
475 | /* FIXME: Filter out false h/w breakpoint hits (i.e. EDA | |
25985edc | 476 | not within any configured h/w breakpoint range). Synchronize with |
51533b61 MS |
477 | what already exists for kernel debugging. */ |
478 | if (((user_regs(ti)->exs & 0xff00) >> 8) == BREAK_8_INTR_VECT) { | |
479 | /* Break 8: subtract 2 from ERP unless in a delay slot. */ | |
480 | if (!(user_regs(ti)->erp & 0x1)) | |
481 | user_regs(ti)->erp -= 2; | |
482 | } | |
483 | sys_kill(ti->task->pid, sig); | |
484 | } | |
485 | ||
486 | void | |
487 | keep_debug_flags(unsigned long oldccs, unsigned long oldspc, | |
488 | struct pt_regs *regs) | |
489 | { | |
490 | if (oldccs & (1 << Q_CCS_BITNR)) { | |
491 | /* Pending single step due to single-stepping the break 13 | |
492 | in the signal trampoline: keep the Q flag. */ | |
493 | regs->ccs |= (1 << Q_CCS_BITNR); | |
494 | /* S flag should be set - complain if it's not. */ | |
495 | if (!(oldccs & (1 << (S_CCS_BITNR + CCS_SHIFT)))) { | |
496 | printk("Q flag but no S flag?"); | |
497 | } | |
498 | regs->ccs |= (1 << (S_CCS_BITNR + CCS_SHIFT)); | |
499 | /* Assume the SPC is valid and interesting. */ | |
500 | regs->spc = oldspc; | |
501 | ||
502 | } else if (oldccs & (1 << (S_CCS_BITNR + CCS_SHIFT))) { | |
503 | /* If a h/w bp was set in the signal handler we need | |
504 | to keep the S flag. */ | |
505 | regs->ccs |= (1 << (S_CCS_BITNR + CCS_SHIFT)); | |
506 | /* Don't keep the old SPC though; if we got here due to | |
507 | a single-step, the Q flag should have been set. */ | |
508 | } else if (regs->spc) { | |
509 | /* If we were single-stepping *before* the signal was taken, | |
510 | we don't want to restore that state now, because GDB will | |
511 | have forgotten all about it. */ | |
512 | regs->spc = 0; | |
513 | regs->ccs &= ~(1 << (S_CCS_BITNR + CCS_SHIFT)); | |
514 | } | |
515 | } | |
516 | ||
517 | /* Set up the trampolines on the signal return page. */ | |
518 | int __init | |
519 | cris_init_signal(void) | |
520 | { | |
5cbded58 | 521 | u16* data = kmalloc(PAGE_SIZE, GFP_KERNEL); |
51533b61 MS |
522 | |
523 | /* This is movu.w __NR_sigreturn, r9; break 13; */ | |
524 | data[0] = 0x9c5f; | |
525 | data[1] = __NR_sigreturn; | |
526 | data[2] = 0xe93d; | |
527 | /* This is movu.w __NR_rt_sigreturn, r9; break 13; */ | |
528 | data[3] = 0x9c5f; | |
529 | data[4] = __NR_rt_sigreturn; | |
530 | data[5] = 0xe93d; | |
531 | ||
532 | /* Map to userspace with appropriate permissions (no write access...) */ | |
533 | cris_signal_return_page = (unsigned long) | |
534 | __ioremap_prot(virt_to_phys(data), PAGE_SIZE, PAGE_SIGNAL_TRAMPOLINE); | |
535 | ||
536 | return 0; | |
537 | } | |
538 | ||
539 | __initcall(cris_init_signal); |