1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18#include <linux/signal.h>
19#include <linux/errno.h>
20#include <linux/ptrace.h>
21#include <linux/personality.h>
22#include <linux/tracehook.h>
23
24#include <asm/ucontext.h>
25#include <asm/uaccess.h>
26#include <asm/cacheflush.h>
27#include <asm/coprocessor.h>
28#include <asm/unistd.h>
29
30#define DEBUG_SIG 0
31
32extern struct task_struct *coproc_owners[];
33
34struct rt_sigframe
35{
36 struct siginfo info;
37 struct ucontext uc;
38 struct {
39 xtregs_opt_t opt;
40 xtregs_user_t user;
41#if XTENSA_HAVE_COPROCESSORS
42 xtregs_coprocessor_t cp;
43#endif
44 } xtregs;
45 unsigned char retcode[6];
46 unsigned int window[4];
47};
48
49
50
51
52
53
54int
55flush_window_regs_user(struct pt_regs *regs)
56{
57 const unsigned long ws = regs->windowstart;
58 const unsigned long wb = regs->windowbase;
59 unsigned long sp = 0;
60 unsigned long wm;
61 int err = 1;
62 int base;
63
64
65
66 if (regs->wmask == 1)
67 return 0;
68
69
70
71 wm = (ws >> wb) | (ws << (XCHAL_NUM_AREGS / 4 - wb));
72 base = (XCHAL_NUM_AREGS / 4) - (regs->wmask >> 4);
73
74
75
76 if ((regs->wmask & 2) == 0)
77 if (__get_user(sp, (int*)(regs->areg[base * 4 + 1] - 12)))
78 goto errout;
79
80
81
82 while (base < XCHAL_NUM_AREGS / 4) {
83
84 int m = (wm >> base);
85 int inc = 0;
86
87
88
89 if (m & 2) {
90 inc = 1;
91
92 } else if (m & 4) {
93 if (copy_to_user((void*)(sp - 32),
94 ®s->areg[(base + 1) * 4], 16))
95 goto errout;
96 inc = 2;
97
98 } else if (m & 8) {
99 if (copy_to_user((void*)(sp - 48),
100 ®s->areg[(base + 1) * 4], 32))
101 goto errout;
102 inc = 3;
103 }
104
105
106
107 sp = regs->areg[((base + inc) * 4 + 1) % XCHAL_NUM_AREGS];
108 if (copy_to_user((void*)(sp - 16), ®s->areg[base * 4], 16))
109 goto errout;
110
111
112
113 sp = regs->areg[base * 4 + 1];
114 base += inc;
115 }
116
117 regs->wmask = 1;
118 regs->windowstart = 1 << wb;
119
120 return 0;
121
122errout:
123 return err;
124}
125
126
127
128
129
130
131
132
133
134static int
135setup_sigcontext(struct rt_sigframe __user *frame, struct pt_regs *regs)
136{
137 struct sigcontext __user *sc = &frame->uc.uc_mcontext;
138 struct thread_info *ti = current_thread_info();
139 int err = 0;
140
141#define COPY(x) err |= __put_user(regs->x, &sc->sc_##x)
142 COPY(pc);
143 COPY(ps);
144 COPY(lbeg);
145 COPY(lend);
146 COPY(lcount);
147 COPY(sar);
148#undef COPY
149
150 err |= flush_window_regs_user(regs);
151 err |= __copy_to_user (sc->sc_a, regs->areg, 16 * 4);
152 err |= __put_user(0, &sc->sc_xtregs);
153
154 if (err)
155 return err;
156
157#if XTENSA_HAVE_COPROCESSORS
158 coprocessor_flush_all(ti);
159 coprocessor_release_all(ti);
160 err |= __copy_to_user(&frame->xtregs.cp, &ti->xtregs_cp,
161 sizeof (frame->xtregs.cp));
162#endif
163 err |= __copy_to_user(&frame->xtregs.opt, ®s->xtregs_opt,
164 sizeof (xtregs_opt_t));
165 err |= __copy_to_user(&frame->xtregs.user, &ti->xtregs_user,
166 sizeof (xtregs_user_t));
167
168 err |= __put_user(err ? NULL : &frame->xtregs, &sc->sc_xtregs);
169
170 return err;
171}
172
173static int
174restore_sigcontext(struct pt_regs *regs, struct rt_sigframe __user *frame)
175{
176 struct sigcontext __user *sc = &frame->uc.uc_mcontext;
177 struct thread_info *ti = current_thread_info();
178 unsigned int err = 0;
179 unsigned long ps;
180
181#define COPY(x) err |= __get_user(regs->x, &sc->sc_##x)
182 COPY(pc);
183 COPY(lbeg);
184 COPY(lend);
185 COPY(lcount);
186 COPY(sar);
187#undef COPY
188
189
190
191 regs->wmask = 1;
192 regs->windowbase = 0;
193 regs->windowstart = 1;
194
195 regs->syscall = -1;
196
197
198
199
200
201 err |= __get_user(ps, &sc->sc_ps);
202 regs->ps = (regs->ps & ~PS_CALLINC_MASK) | (ps & PS_CALLINC_MASK);
203
204
205
206 if ((regs->lcount > 0)
207 && ((regs->lbeg > TASK_SIZE) || (regs->lend > TASK_SIZE)) )
208 err = 1;
209
210 err |= __copy_from_user(regs->areg, sc->sc_a, 16 * 4);
211
212 if (err)
213 return err;
214
215
216
217
218
219
220
221
222#if XTENSA_HAVE_COPROCESSORS
223 coprocessor_release_all(ti);
224 err |= __copy_from_user(&ti->xtregs_cp, &frame->xtregs.cp,
225 sizeof (frame->xtregs.cp));
226#endif
227 err |= __copy_from_user(&ti->xtregs_user, &frame->xtregs.user,
228 sizeof (xtregs_user_t));
229 err |= __copy_from_user(®s->xtregs_opt, &frame->xtregs.opt,
230 sizeof (xtregs_opt_t));
231
232 return err;
233}
234
235
236
237
238
239
240asmlinkage long xtensa_rt_sigreturn(long a0, long a1, long a2, long a3,
241 long a4, long a5, struct pt_regs *regs)
242{
243 struct rt_sigframe __user *frame;
244 sigset_t set;
245 int ret;
246
247
248 current_thread_info()->restart_block.fn = do_no_restart_syscall;
249
250 if (regs->depc > 64)
251 panic("rt_sigreturn in double exception!\n");
252
253 frame = (struct rt_sigframe __user *) regs->areg[1];
254
255 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
256 goto badframe;
257
258 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
259 goto badframe;
260
261 set_current_blocked(&set);
262
263 if (restore_sigcontext(regs, frame))
264 goto badframe;
265
266 ret = regs->areg[2];
267
268 if (restore_altstack(&frame->uc.uc_stack))
269 goto badframe;
270
271 return ret;
272
273badframe:
274 force_sig(SIGSEGV, current);
275 return 0;
276}
277
278
279
280
281
282
283
284static int
285gen_return_code(unsigned char *codemem)
286{
287 int err = 0;
288
289
290
291
292
293
294
295
296#if __NR_rt_sigreturn > 255
297# error Generating the MOVI instruction below breaks!
298#endif
299
300#ifdef __XTENSA_EB__
301
302 err |= __put_user(0x22, &codemem[0]);
303 err |= __put_user(0x0a, &codemem[1]);
304 err |= __put_user(__NR_rt_sigreturn, &codemem[2]);
305
306 err |= __put_user(0x00, &codemem[3]);
307 err |= __put_user(0x05, &codemem[4]);
308 err |= __put_user(0x00, &codemem[5]);
309
310#elif defined __XTENSA_EL__
311
312 err |= __put_user(0x22, &codemem[0]);
313 err |= __put_user(0xa0, &codemem[1]);
314 err |= __put_user(__NR_rt_sigreturn, &codemem[2]);
315
316 err |= __put_user(0x00, &codemem[3]);
317 err |= __put_user(0x50, &codemem[4]);
318 err |= __put_user(0x00, &codemem[5]);
319#else
320# error Must use compiler for Xtensa processors.
321#endif
322
323
324
325 if (err == 0) {
326 __invalidate_icache_range((unsigned long)codemem, 6UL);
327 __flush_invalidate_dcache_range((unsigned long)codemem, 6UL);
328 }
329
330 return err;
331}
332
333
334static int setup_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
335 sigset_t *set, struct pt_regs *regs)
336{
337 struct rt_sigframe *frame;
338 int err = 0;
339 int signal;
340 unsigned long sp, ra, tp;
341
342 sp = regs->areg[1];
343
344 if ((ka->sa.sa_flags & SA_ONSTACK) != 0 && sas_ss_flags(sp) == 0) {
345 sp = current->sas_ss_sp + current->sas_ss_size;
346 }
347
348 frame = (void *)((sp - sizeof(*frame)) & -16ul);
349
350 if (regs->depc > 64)
351 panic ("Double exception sys_sigreturn\n");
352
353 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame))) {
354 goto give_sigsegv;
355 }
356
357 signal = current_thread_info()->exec_domain
358 && current_thread_info()->exec_domain->signal_invmap
359 && sig < 32
360 ? current_thread_info()->exec_domain->signal_invmap[sig]
361 : sig;
362
363 if (ka->sa.sa_flags & SA_SIGINFO) {
364 err |= copy_siginfo_to_user(&frame->info, info);
365 }
366
367
368
369 err |= __put_user(0, &frame->uc.uc_flags);
370 err |= __put_user(0, &frame->uc.uc_link);
371 err |= __save_altstack(&frame->uc.uc_stack, regs->areg[1]);
372 err |= setup_sigcontext(frame, regs);
373 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
374
375 if (ka->sa.sa_flags & SA_RESTORER) {
376 ra = (unsigned long)ka->sa.sa_restorer;
377 } else {
378
379
380
381 err |= gen_return_code(frame->retcode);
382
383 if (err) {
384 goto give_sigsegv;
385 }
386 ra = (unsigned long) frame->retcode;
387 }
388
389
390
391
392
393
394
395 tp = regs->threadptr;
396 start_thread(regs, (unsigned long) ka->sa.sa_handler,
397 (unsigned long) frame);
398
399
400
401
402 regs->areg[4] = (((unsigned long) ra) & 0x3fffffff) | 0x40000000;
403 regs->areg[6] = (unsigned long) signal;
404 regs->areg[7] = (unsigned long) &frame->info;
405 regs->areg[8] = (unsigned long) &frame->uc;
406 regs->threadptr = tp;
407
408
409
410
411 set_fs(USER_DS);
412
413#if DEBUG_SIG
414 printk("SIG rt deliver (%s:%d): signal=%d sp=%p pc=%08x\n",
415 current->comm, current->pid, signal, frame, regs->pc);
416#endif
417
418 return 0;
419
420give_sigsegv:
421 force_sigsegv(sig, current);
422 return -EFAULT;
423}
424
425
426
427
428
429
430
431
432
433
434static void do_signal(struct pt_regs *regs)
435{
436 siginfo_t info;
437 int signr;
438 struct k_sigaction ka;
439
440 task_pt_regs(current)->icountlevel = 0;
441
442 signr = get_signal_to_deliver(&info, &ka, regs, NULL);
443
444 if (signr > 0) {
445 int ret;
446
447
448
449 if ((signed)regs->syscall >= 0) {
450
451
452
453 switch (regs->areg[2]) {
454 case -ERESTARTNOHAND:
455 case -ERESTART_RESTARTBLOCK:
456 regs->areg[2] = -EINTR;
457 break;
458
459 case -ERESTARTSYS:
460 if (!(ka.sa.sa_flags & SA_RESTART)) {
461 regs->areg[2] = -EINTR;
462 break;
463 }
464
465 case -ERESTARTNOINTR:
466 regs->areg[2] = regs->syscall;
467 regs->pc -= 3;
468 break;
469
470 default:
471
472 if (regs->areg[2] != 0)
473 break;
474 }
475 }
476
477
478
479 ret = setup_frame(signr, &ka, &info, sigmask_to_save(), regs);
480 if (ret)
481 return;
482
483 signal_delivered(signr, &info, &ka, regs, 0);
484 if (current->ptrace & PT_SINGLESTEP)
485 task_pt_regs(current)->icountlevel = 1;
486
487 return;
488 }
489
490
491 if ((signed) regs->syscall >= 0) {
492
493 switch (regs->areg[2]) {
494 case -ERESTARTNOHAND:
495 case -ERESTARTSYS:
496 case -ERESTARTNOINTR:
497 regs->areg[2] = regs->syscall;
498 regs->pc -= 3;
499 break;
500 case -ERESTART_RESTARTBLOCK:
501 regs->areg[2] = __NR_restart_syscall;
502 regs->pc -= 3;
503 break;
504 }
505 }
506
507
508 restore_saved_sigmask();
509
510 if (current->ptrace & PT_SINGLESTEP)
511 task_pt_regs(current)->icountlevel = 1;
512 return;
513}
514
515void do_notify_resume(struct pt_regs *regs)
516{
517 if (test_thread_flag(TIF_SIGPENDING))
518 do_signal(regs);
519
520 if (test_and_clear_thread_flag(TIF_NOTIFY_RESUME))
521 tracehook_notify_resume(regs);
522}
523