]>
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> | |
21 | #include <asm/uaccess.h> | |
556dcee7 JN |
22 | #include <arch/ptrace.h> |
23 | #include <arch/hwregs/cpu_vect.h> | |
51533b61 MS |
24 | |
25 | extern unsigned long cris_signal_return_page; | |
26 | ||
51533b61 MS |
27 | /* |
28 | * A syscall in CRIS is really a "break 13" instruction, which is 2 | |
29 | * bytes. The registers is manipulated so upon return the instruction | |
30 | * will be executed again. | |
31 | * | |
32 | * This relies on that PC points to the instruction after the break call. | |
33 | */ | |
34 | #define RESTART_CRIS_SYS(regs) regs->r10 = regs->orig_r10; regs->erp -= 2; | |
35 | ||
36 | /* Signal frames. */ | |
37 | struct signal_frame { | |
38 | struct sigcontext sc; | |
39 | unsigned long extramask[_NSIG_WORDS - 1]; | |
40 | unsigned char retcode[8]; /* Trampoline code. */ | |
41 | }; | |
42 | ||
43 | struct rt_signal_frame { | |
44 | struct siginfo *pinfo; | |
45 | void *puc; | |
46 | struct siginfo info; | |
47 | struct ucontext uc; | |
48 | unsigned char retcode[8]; /* Trampoline code. */ | |
49 | }; | |
50 | ||
574852a2 | 51 | void do_signal(int restart, struct pt_regs *regs); |
51533b61 MS |
52 | void keep_debug_flags(unsigned long oldccs, unsigned long oldspc, |
53 | struct pt_regs *regs); | |
51533b61 | 54 | |
51533b61 MS |
55 | static int |
56 | restore_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc) | |
57 | { | |
58 | unsigned int err = 0; | |
59 | unsigned long old_usp; | |
60 | ||
61 | /* Always make any pending restarted system calls return -EINTR */ | |
f56141e3 | 62 | current->restart_block.fn = do_no_restart_syscall; |
51533b61 MS |
63 | |
64 | /* | |
65 | * Restore the registers from &sc->regs. sc is already checked | |
66 | * for VERIFY_READ since the signal_frame was previously | |
67 | * checked in sys_sigreturn(). | |
68 | */ | |
69 | if (__copy_from_user(regs, sc, sizeof(struct pt_regs))) | |
70 | goto badframe; | |
71 | ||
72 | /* Make that the user-mode flag is set. */ | |
73 | regs->ccs |= (1 << (U_CCS_BITNR + CCS_SHIFT)); | |
74 | ||
75 | /* Restore the old USP. */ | |
76 | err |= __get_user(old_usp, &sc->usp); | |
77 | wrusp(old_usp); | |
78 | ||
79 | return err; | |
80 | ||
81 | badframe: | |
82 | return 1; | |
83 | } | |
84 | ||
e6a6d210 | 85 | asmlinkage int sys_sigreturn(void) |
51533b61 | 86 | { |
e6a6d210 | 87 | struct pt_regs *regs = current_pt_regs(); |
51533b61 MS |
88 | sigset_t set; |
89 | struct signal_frame __user *frame; | |
90 | unsigned long oldspc = regs->spc; | |
91 | unsigned long oldccs = regs->ccs; | |
92 | ||
93 | frame = (struct signal_frame *) rdusp(); | |
94 | ||
95 | /* | |
96 | * Since the signal is stacked on a dword boundary, the frame | |
97 | * should be dword aligned here as well. It it's not, then the | |
98 | * user is trying some funny business. | |
99 | */ | |
100 | if (((long)frame) & 3) | |
101 | goto badframe; | |
102 | ||
103 | if (!access_ok(VERIFY_READ, frame, sizeof(*frame))) | |
104 | goto badframe; | |
105 | ||
106 | if (__get_user(set.sig[0], &frame->sc.oldmask) || | |
107 | (_NSIG_WORDS > 1 && __copy_from_user(&set.sig[1], | |
108 | frame->extramask, | |
109 | sizeof(frame->extramask)))) | |
110 | goto badframe; | |
111 | ||
f3b5e822 | 112 | set_current_blocked(&set); |
51533b61 MS |
113 | |
114 | if (restore_sigcontext(regs, &frame->sc)) | |
115 | goto badframe; | |
116 | ||
117 | keep_debug_flags(oldccs, oldspc, regs); | |
118 | ||
119 | return regs->r10; | |
120 | ||
121 | badframe: | |
122 | force_sig(SIGSEGV, current); | |
123 | return 0; | |
124 | } | |
125 | ||
e6a6d210 | 126 | asmlinkage int sys_rt_sigreturn(void) |
51533b61 | 127 | { |
e6a6d210 | 128 | struct pt_regs *regs = current_pt_regs(); |
51533b61 MS |
129 | sigset_t set; |
130 | struct rt_signal_frame __user *frame; | |
131 | unsigned long oldspc = regs->spc; | |
132 | unsigned long oldccs = regs->ccs; | |
133 | ||
134 | frame = (struct rt_signal_frame *) rdusp(); | |
135 | ||
136 | /* | |
137 | * Since the signal is stacked on a dword boundary, the frame | |
138 | * should be dword aligned here as well. It it's not, then the | |
139 | * user is trying some funny business. | |
140 | */ | |
141 | if (((long)frame) & 3) | |
142 | goto badframe; | |
143 | ||
144 | if (!access_ok(VERIFY_READ, frame, sizeof(*frame))) | |
145 | goto badframe; | |
146 | ||
147 | if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set))) | |
148 | goto badframe; | |
149 | ||
f3b5e822 | 150 | set_current_blocked(&set); |
51533b61 MS |
151 | |
152 | if (restore_sigcontext(regs, &frame->uc.uc_mcontext)) | |
153 | goto badframe; | |
154 | ||
d970e428 | 155 | if (restore_altstack(&frame->uc.uc_stack)) |
574852a2 | 156 | goto badframe; |
51533b61 MS |
157 | |
158 | keep_debug_flags(oldccs, oldspc, regs); | |
159 | ||
160 | return regs->r10; | |
161 | ||
162 | badframe: | |
163 | force_sig(SIGSEGV, current); | |
164 | return 0; | |
165 | } | |
166 | ||
167 | /* Setup a signal frame. */ | |
168 | static int | |
169 | setup_sigcontext(struct sigcontext __user *sc, struct pt_regs *regs, | |
170 | unsigned long mask) | |
171 | { | |
172 | int err; | |
173 | unsigned long usp; | |
174 | ||
175 | err = 0; | |
176 | usp = rdusp(); | |
177 | ||
178 | /* | |
179 | * Copy the registers. They are located first in sc, so it's | |
180 | * possible to use sc directly. | |
181 | */ | |
182 | err |= __copy_to_user(sc, regs, sizeof(struct pt_regs)); | |
183 | ||
184 | err |= __put_user(mask, &sc->oldmask); | |
185 | err |= __put_user(usp, &sc->usp); | |
186 | ||
187 | return err; | |
188 | } | |
189 | ||
190 | /* Figure out where to put the new signal frame - usually on the stack. */ | |
191 | static inline void __user * | |
8215ade8 | 192 | get_sigframe(struct ksignal *ksig, size_t frame_size) |
51533b61 | 193 | { |
8215ade8 | 194 | unsigned long sp = sigsp(rdusp(), ksig); |
51533b61 MS |
195 | |
196 | /* Make sure the frame is dword-aligned. */ | |
197 | sp &= ~3; | |
198 | ||
199 | return (void __user *)(sp - frame_size); | |
200 | } | |
201 | ||
202 | /* Grab and setup a signal frame. | |
203 | * | |
204 | * Basically a lot of state-info is stacked, and arranged for the | |
574852a2 | 205 | * user-mode program to return to the kernel using either a trampiline |
51533b61 MS |
206 | * which performs the syscall sigreturn(), or a provided user-mode |
207 | * trampoline. | |
208 | */ | |
574852a2 | 209 | static int |
fa019772 | 210 | setup_frame(struct ksignal *ksig, sigset_t *set, struct pt_regs *regs) |
51533b61 MS |
211 | { |
212 | int err; | |
213 | unsigned long return_ip; | |
214 | struct signal_frame __user *frame; | |
215 | ||
216 | err = 0; | |
8215ade8 | 217 | frame = get_sigframe(ksig, sizeof(*frame)); |
51533b61 MS |
218 | |
219 | if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame))) | |
fa019772 | 220 | return -EFAULT; |
51533b61 MS |
221 | |
222 | err |= setup_sigcontext(&frame->sc, regs, set->sig[0]); | |
223 | ||
224 | if (err) | |
fa019772 | 225 | return -EFAULT; |
51533b61 MS |
226 | |
227 | if (_NSIG_WORDS > 1) { | |
228 | err |= __copy_to_user(frame->extramask, &set->sig[1], | |
229 | sizeof(frame->extramask)); | |
230 | } | |
231 | ||
232 | if (err) | |
fa019772 | 233 | return -EFAULT; |
51533b61 MS |
234 | |
235 | /* | |
236 | * Set up to return from user-space. If provided, use a stub | |
237 | * already located in user-space. | |
238 | */ | |
fa019772 RW |
239 | if (ksig->ka.sa.sa_flags & SA_RESTORER) { |
240 | return_ip = (unsigned long)ksig->ka.sa.sa_restorer; | |
51533b61 MS |
241 | } else { |
242 | /* Trampoline - the desired return ip is in the signal return page. */ | |
243 | return_ip = cris_signal_return_page; | |
244 | ||
245 | /* | |
246 | * This is movu.w __NR_sigreturn, r9; break 13; | |
247 | * | |
248 | * WE DO NOT USE IT ANY MORE! It's only left here for historical | |
249 | * reasons and because gdb uses it as a signature to notice | |
250 | * signal handler stack frames. | |
251 | */ | |
252 | err |= __put_user(0x9c5f, (short __user*)(frame->retcode+0)); | |
253 | err |= __put_user(__NR_sigreturn, (short __user*)(frame->retcode+2)); | |
254 | err |= __put_user(0xe93d, (short __user*)(frame->retcode+4)); | |
255 | } | |
256 | ||
257 | if (err) | |
fa019772 | 258 | return -EFAULT; |
51533b61 MS |
259 | |
260 | /* | |
261 | * Set up registers for signal handler. | |
262 | * | |
263 | * Where the code enters now. | |
264 | * Where the code enter later. | |
265 | * First argument, signo. | |
266 | */ | |
fa019772 | 267 | regs->erp = (unsigned long) ksig->ka.sa.sa_handler; |
51533b61 | 268 | regs->srp = return_ip; |
fa019772 | 269 | regs->r10 = ksig->sig; |
51533b61 MS |
270 | |
271 | /* Actually move the USP to reflect the stacked frame. */ | |
272 | wrusp((unsigned long)frame); | |
273 | ||
574852a2 | 274 | return 0; |
51533b61 MS |
275 | } |
276 | ||
574852a2 | 277 | static int |
fa019772 | 278 | setup_rt_frame(struct ksignal *ksig, sigset_t *set, struct pt_regs *regs) |
51533b61 MS |
279 | { |
280 | int err; | |
281 | unsigned long return_ip; | |
282 | struct rt_signal_frame __user *frame; | |
283 | ||
284 | err = 0; | |
8215ade8 | 285 | frame = get_sigframe(ksig, sizeof(*frame)); |
51533b61 MS |
286 | |
287 | if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame))) | |
fa019772 | 288 | return -EFAULT; |
51533b61 MS |
289 | |
290 | /* TODO: what is the current->exec_domain stuff and invmap ? */ | |
291 | ||
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 MS |
429 | |
430 | /* | |
431 | * The common case should go fast, which is why this point is | |
432 | * reached from kernel-mode. If that's the case, just return | |
433 | * without doing anything. | |
434 | */ | |
435 | if (!user_mode(regs)) | |
574852a2 | 436 | return; |
51533b61 | 437 | |
fa019772 | 438 | if (get_signal(&ksig)) { |
574852a2 | 439 | /* Whee! Actually deliver the signal. */ |
fa019772 | 440 | handle_signal(canrestart, &ksig, regs); |
574852a2 | 441 | return; |
51533b61 MS |
442 | } |
443 | ||
444 | /* Got here from a system call? */ | |
445 | if (canrestart) { | |
446 | /* Restart the system call - no handlers present. */ | |
447 | if (regs->r10 == -ERESTARTNOHAND || | |
448 | regs->r10 == -ERESTARTSYS || | |
449 | regs->r10 == -ERESTARTNOINTR) { | |
450 | RESTART_CRIS_SYS(regs); | |
451 | } | |
452 | ||
453 | if (regs->r10 == -ERESTART_RESTARTBLOCK){ | |
39923326 | 454 | regs->r9 = __NR_restart_syscall; |
51533b61 MS |
455 | regs->erp -= 2; |
456 | } | |
457 | } | |
458 | ||
574852a2 JN |
459 | /* if there's no signal to deliver, we just put the saved sigmask |
460 | * back */ | |
51a7b448 | 461 | restore_saved_sigmask(); |
51533b61 MS |
462 | } |
463 | ||
464 | asmlinkage void | |
465 | ugdb_trap_user(struct thread_info *ti, int sig) | |
466 | { | |
467 | if (((user_regs(ti)->exs & 0xff00) >> 8) != SINGLE_STEP_INTR_VECT) { | |
468 | /* Zero single-step PC if the reason we stopped wasn't a single | |
469 | step exception. This is to avoid relying on it when it isn't | |
470 | reliable. */ | |
471 | user_regs(ti)->spc = 0; | |
472 | } | |
473 | /* FIXME: Filter out false h/w breakpoint hits (i.e. EDA | |
25985edc | 474 | not within any configured h/w breakpoint range). Synchronize with |
51533b61 MS |
475 | what already exists for kernel debugging. */ |
476 | if (((user_regs(ti)->exs & 0xff00) >> 8) == BREAK_8_INTR_VECT) { | |
477 | /* Break 8: subtract 2 from ERP unless in a delay slot. */ | |
478 | if (!(user_regs(ti)->erp & 0x1)) | |
479 | user_regs(ti)->erp -= 2; | |
480 | } | |
481 | sys_kill(ti->task->pid, sig); | |
482 | } | |
483 | ||
484 | void | |
485 | keep_debug_flags(unsigned long oldccs, unsigned long oldspc, | |
486 | struct pt_regs *regs) | |
487 | { | |
488 | if (oldccs & (1 << Q_CCS_BITNR)) { | |
489 | /* Pending single step due to single-stepping the break 13 | |
490 | in the signal trampoline: keep the Q flag. */ | |
491 | regs->ccs |= (1 << Q_CCS_BITNR); | |
492 | /* S flag should be set - complain if it's not. */ | |
493 | if (!(oldccs & (1 << (S_CCS_BITNR + CCS_SHIFT)))) { | |
494 | printk("Q flag but no S flag?"); | |
495 | } | |
496 | regs->ccs |= (1 << (S_CCS_BITNR + CCS_SHIFT)); | |
497 | /* Assume the SPC is valid and interesting. */ | |
498 | regs->spc = oldspc; | |
499 | ||
500 | } else if (oldccs & (1 << (S_CCS_BITNR + CCS_SHIFT))) { | |
501 | /* If a h/w bp was set in the signal handler we need | |
502 | to keep the S flag. */ | |
503 | regs->ccs |= (1 << (S_CCS_BITNR + CCS_SHIFT)); | |
504 | /* Don't keep the old SPC though; if we got here due to | |
505 | a single-step, the Q flag should have been set. */ | |
506 | } else if (regs->spc) { | |
507 | /* If we were single-stepping *before* the signal was taken, | |
508 | we don't want to restore that state now, because GDB will | |
509 | have forgotten all about it. */ | |
510 | regs->spc = 0; | |
511 | regs->ccs &= ~(1 << (S_CCS_BITNR + CCS_SHIFT)); | |
512 | } | |
513 | } | |
514 | ||
515 | /* Set up the trampolines on the signal return page. */ | |
516 | int __init | |
517 | cris_init_signal(void) | |
518 | { | |
5cbded58 | 519 | u16* data = kmalloc(PAGE_SIZE, GFP_KERNEL); |
51533b61 MS |
520 | |
521 | /* This is movu.w __NR_sigreturn, r9; break 13; */ | |
522 | data[0] = 0x9c5f; | |
523 | data[1] = __NR_sigreturn; | |
524 | data[2] = 0xe93d; | |
525 | /* This is movu.w __NR_rt_sigreturn, r9; break 13; */ | |
526 | data[3] = 0x9c5f; | |
527 | data[4] = __NR_rt_sigreturn; | |
528 | data[5] = 0xe93d; | |
529 | ||
530 | /* Map to userspace with appropriate permissions (no write access...) */ | |
531 | cris_signal_return_page = (unsigned long) | |
532 | __ioremap_prot(virt_to_phys(data), PAGE_SIZE, PAGE_SIGNAL_TRAMPOLINE); | |
533 | ||
534 | return 0; | |
535 | } | |
536 | ||
537 | __initcall(cris_init_signal); |