1
2
3
4
5#include <linux/sched.h>
6#include <linux/mm.h>
7#include <linux/slab.h>
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>
22#include <arch/hwregs/cpu_vect.h>
23
24extern unsigned long cris_signal_return_page;
25
26
27
28
29
30
31
32
33#define RESTART_CRIS_SYS(regs) regs->r10 = regs->orig_r10; regs->erp -= 2;
34
35
36struct signal_frame {
37 struct sigcontext sc;
38 unsigned long extramask[_NSIG_WORDS - 1];
39 unsigned char retcode[8];
40};
41
42struct rt_signal_frame {
43 struct siginfo *pinfo;
44 void *puc;
45 struct siginfo info;
46 struct ucontext uc;
47 unsigned char retcode[8];
48};
49
50void do_signal(int restart, struct pt_regs *regs);
51void keep_debug_flags(unsigned long oldccs, unsigned long oldspc,
52 struct pt_regs *regs);
53
54static int
55restore_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc)
56{
57 unsigned int err = 0;
58 unsigned long old_usp;
59
60
61 current->restart_block.fn = do_no_restart_syscall;
62
63
64
65
66
67
68 if (__copy_from_user(regs, sc, sizeof(struct pt_regs)))
69 goto badframe;
70
71
72 regs->ccs |= (1 << (U_CCS_BITNR + CCS_SHIFT));
73
74
75 regs->exs = -1;
76
77
78 err |= __get_user(old_usp, &sc->usp);
79 wrusp(old_usp);
80
81 return err;
82
83badframe:
84 return 1;
85}
86
87asmlinkage int sys_sigreturn(void)
88{
89 struct pt_regs *regs = current_pt_regs();
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
99
100
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
114 set_current_blocked(&set);
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
123badframe:
124 force_sig(SIGSEGV, current);
125 return 0;
126}
127
128asmlinkage int sys_rt_sigreturn(void)
129{
130 struct pt_regs *regs = current_pt_regs();
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
140
141
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
152 set_current_blocked(&set);
153
154 if (restore_sigcontext(regs, &frame->uc.uc_mcontext))
155 goto badframe;
156
157 if (restore_altstack(&frame->uc.uc_stack))
158 goto badframe;
159
160 keep_debug_flags(oldccs, oldspc, regs);
161
162 return regs->r10;
163
164badframe:
165 force_sig(SIGSEGV, current);
166 return 0;
167}
168
169
170static int
171setup_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
182
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
193static inline void __user *
194get_sigframe(struct ksignal *ksig, size_t frame_size)
195{
196 unsigned long sp = sigsp(rdusp(), ksig);
197
198
199 sp &= ~3;
200
201 return (void __user *)(sp - frame_size);
202}
203
204
205
206
207
208
209
210
211static int
212setup_frame(struct ksignal *ksig, sigset_t *set, struct pt_regs *regs)
213{
214 int err;
215 unsigned long return_ip;
216 struct signal_frame __user *frame;
217
218 err = 0;
219 frame = get_sigframe(ksig, sizeof(*frame));
220
221 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
222 return -EFAULT;
223
224 err |= setup_sigcontext(&frame->sc, regs, set->sig[0]);
225
226 if (err)
227 return -EFAULT;
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)
235 return -EFAULT;
236
237
238
239
240
241 if (ksig->ka.sa.sa_flags & SA_RESTORER) {
242 return_ip = (unsigned long)ksig->ka.sa.sa_restorer;
243 } else {
244
245 return_ip = cris_signal_return_page;
246
247
248
249
250
251
252
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)
260 return -EFAULT;
261
262
263
264
265
266
267
268
269 regs->erp = (unsigned long) ksig->ka.sa.sa_handler;
270 regs->srp = return_ip;
271 regs->r10 = ksig->sig;
272
273
274 wrusp((unsigned long)frame);
275
276 return 0;
277}
278
279static int
280setup_rt_frame(struct ksignal *ksig, sigset_t *set, struct pt_regs *regs)
281{
282 int err;
283 unsigned long return_ip;
284 struct rt_signal_frame __user *frame;
285
286 err = 0;
287 frame = get_sigframe(ksig, sizeof(*frame));
288
289 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
290 return -EFAULT;
291
292 err |= __put_user(&frame->info, &frame->pinfo);
293 err |= __put_user(&frame->uc, &frame->puc);
294 err |= copy_siginfo_to_user(&frame->info, &ksig->info);
295
296 if (err)
297 return -EFAULT;
298
299
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));
303 err |= __save_altstack(&frame->uc.uc_stack, rdusp());
304
305 if (err)
306 return -EFAULT;
307
308
309
310
311
312 if (ksig->ka.sa.sa_flags & SA_RESTORER) {
313 return_ip = (unsigned long) ksig->ka.sa.sa_restorer;
314 } else {
315
316 return_ip = cris_signal_return_page + 6;
317
318
319
320
321
322
323
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)
334 return -EFAULT;
335
336
337
338
339
340
341
342
343
344
345 regs->erp = (unsigned long) ksig->ka.sa.sa_handler;
346 regs->srp = return_ip;
347 regs->r10 = ksig->sig;
348 regs->r11 = (unsigned long) &frame->info;
349 regs->r12 = 0;
350
351
352 wrusp((unsigned long)frame);
353
354 return 0;
355}
356
357
358static inline void
359handle_signal(int canrestart, struct ksignal *ksig, struct pt_regs *regs)
360{
361 sigset_t *oldset = sigmask_to_save();
362 int ret;
363
364
365 if (canrestart) {
366
367 switch (regs->r10) {
368 case -ERESTART_RESTARTBLOCK:
369 case -ERESTARTNOHAND:
370
371
372
373
374
375
376
377
378 regs->r10 = -EINTR;
379 break;
380
381 case -ERESTARTSYS:
382
383
384
385
386
387 if (!(ksig->ka.sa.sa_flags & SA_RESTART)) {
388 regs->r10 = -EINTR;
389 break;
390 }
391
392
393
394 case -ERESTARTNOINTR:
395
396
397
398
399
400 RESTART_CRIS_SYS(regs);
401 break;
402 }
403 }
404
405
406 if (ksig->ka.sa.sa_flags & SA_SIGINFO)
407 ret = setup_rt_frame(ksig, oldset, regs);
408 else
409 ret = setup_frame(ksig, oldset, regs);
410
411 signal_setup_done(ret, ksig, 0);
412}
413
414
415
416
417
418
419
420
421
422
423
424
425void
426do_signal(int canrestart, struct pt_regs *regs)
427{
428 struct ksignal ksig;
429
430 canrestart = canrestart && ((int)regs->exs >= 0);
431
432
433
434
435
436
437 if (!user_mode(regs))
438 return;
439
440 if (get_signal(&ksig)) {
441
442 handle_signal(canrestart, &ksig, regs);
443 return;
444 }
445
446
447 if (canrestart) {
448
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){
456 regs->r9 = __NR_restart_syscall;
457 regs->erp -= 2;
458 }
459 }
460
461
462
463 restore_saved_sigmask();
464}
465
466asmlinkage void
467ugdb_trap_user(struct thread_info *ti, int sig)
468{
469 if (((user_regs(ti)->exs & 0xff00) >> 8) != SINGLE_STEP_INTR_VECT) {
470
471
472
473 user_regs(ti)->spc = 0;
474 }
475
476
477
478 if (((user_regs(ti)->exs & 0xff00) >> 8) == BREAK_8_INTR_VECT) {
479
480 if (!(user_regs(ti)->erp & 0x1))
481 user_regs(ti)->erp -= 2;
482 }
483 sys_kill(ti->task->pid, sig);
484}
485
486void
487keep_debug_flags(unsigned long oldccs, unsigned long oldspc,
488 struct pt_regs *regs)
489{
490 if (oldccs & (1 << Q_CCS_BITNR)) {
491
492
493 regs->ccs |= (1 << Q_CCS_BITNR);
494
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
500 regs->spc = oldspc;
501
502 } else if (oldccs & (1 << (S_CCS_BITNR + CCS_SHIFT))) {
503
504
505 regs->ccs |= (1 << (S_CCS_BITNR + CCS_SHIFT));
506
507
508 } else if (regs->spc) {
509
510
511
512 regs->spc = 0;
513 regs->ccs &= ~(1 << (S_CCS_BITNR + CCS_SHIFT));
514 }
515}
516
517
518int __init
519cris_init_signal(void)
520{
521 u16* data = kmalloc(PAGE_SIZE, GFP_KERNEL);
522
523
524 data[0] = 0x9c5f;
525 data[1] = __NR_sigreturn;
526 data[2] = 0xe93d;
527
528 data[3] = 0x9c5f;
529 data[4] = __NR_rt_sigreturn;
530 data[5] = 0xe93d;
531
532
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);
540