metag: move irq enable out of irqflags.h on SMP
[firefly-linux-kernel-4.4.55.git] / arch / metag / kernel / traps.c
1 /*
2  *  Meta exception handling.
3  *
4  *  Copyright (C) 2005,2006,2007,2008,2009,2012 Imagination Technologies Ltd.
5  *
6  * This file is subject to the terms and conditions of the GNU General Public
7  * License.  See the file COPYING in the main directory of this archive
8  * for more details.
9  */
10
11 #include <linux/sched.h>
12 #include <linux/signal.h>
13 #include <linux/kernel.h>
14 #include <linux/mm.h>
15 #include <linux/types.h>
16 #include <linux/init.h>
17 #include <linux/interrupt.h>
18 #include <linux/preempt.h>
19 #include <linux/ptrace.h>
20 #include <linux/module.h>
21 #include <linux/kallsyms.h>
22 #include <linux/kdebug.h>
23 #include <linux/kexec.h>
24 #include <linux/unistd.h>
25 #include <linux/smp.h>
26 #include <linux/slab.h>
27 #include <linux/syscalls.h>
28
29 #include <asm/bug.h>
30 #include <asm/core_reg.h>
31 #include <asm/irqflags.h>
32 #include <asm/siginfo.h>
33 #include <asm/traps.h>
34 #include <asm/hwthread.h>
35 #include <asm/switch.h>
36 #include <asm/user_gateway.h>
37 #include <asm/syscall.h>
38 #include <asm/syscalls.h>
39
40 /* Passing syscall arguments as long long is quicker. */
41 typedef unsigned int (*LPSYSCALL) (unsigned long long,
42                                    unsigned long long,
43                                    unsigned long long);
44
45 /*
46  * Users of LNKSET should compare the bus error bits obtained from DEFR
47  * against TXDEFR_LNKSET_SUCCESS only as the failure code will vary between
48  * different cores revisions.
49  */
50 #define TXDEFR_LNKSET_SUCCESS 0x02000000
51 #define TXDEFR_LNKSET_FAILURE 0x04000000
52
53 /*
54  * Our global TBI handle.  Initialised from setup.c/setup_arch.
55  */
56 DECLARE_PER_CPU(PTBI, pTBI);
57
58 #ifdef CONFIG_SMP
59 static DEFINE_PER_CPU(unsigned int, trigger_mask);
60 #else
61 unsigned int global_trigger_mask;
62 #endif
63
64 unsigned long per_cpu__stack_save[NR_CPUS];
65
66 static const char * const trap_names[] = {
67         [TBIXXF_SIGNUM_IIF] = "Illegal instruction fault",
68         [TBIXXF_SIGNUM_PGF] = "Privilege violation",
69         [TBIXXF_SIGNUM_DHF] = "Unaligned data access fault",
70         [TBIXXF_SIGNUM_IGF] = "Code fetch general read failure",
71         [TBIXXF_SIGNUM_DGF] = "Data access general read/write fault",
72         [TBIXXF_SIGNUM_IPF] = "Code fetch page fault",
73         [TBIXXF_SIGNUM_DPF] = "Data access page fault",
74         [TBIXXF_SIGNUM_IHF] = "Instruction breakpoint",
75         [TBIXXF_SIGNUM_DWF] = "Read-only data access fault",
76 };
77
78 const char *trap_name(int trapno)
79 {
80         if (trapno >= 0 && trapno < ARRAY_SIZE(trap_names)
81                         && trap_names[trapno])
82                 return trap_names[trapno];
83         return "Unknown fault";
84 }
85
86 static DEFINE_SPINLOCK(die_lock);
87
88 void die(const char *str, struct pt_regs *regs, long err,
89          unsigned long addr)
90 {
91         static int die_counter;
92
93         oops_enter();
94
95         spin_lock_irq(&die_lock);
96         console_verbose();
97         bust_spinlocks(1);
98         pr_err("%s: err %04lx (%s) addr %08lx [#%d]\n", str, err & 0xffff,
99                trap_name(err & 0xffff), addr, ++die_counter);
100
101         print_modules();
102         show_regs(regs);
103
104         pr_err("Process: %s (pid: %d, stack limit = %p)\n", current->comm,
105                task_pid_nr(current), task_stack_page(current) + THREAD_SIZE);
106
107         bust_spinlocks(0);
108         add_taint(TAINT_DIE);
109         if (kexec_should_crash(current))
110                 crash_kexec(regs);
111
112         if (in_interrupt())
113                 panic("Fatal exception in interrupt");
114
115         if (panic_on_oops)
116                 panic("Fatal exception");
117
118         spin_unlock_irq(&die_lock);
119         oops_exit();
120         do_exit(SIGSEGV);
121 }
122
123 #ifdef CONFIG_METAG_DSP
124 /*
125  * The ECH encoding specifies the size of a DSPRAM as,
126  *
127  *              "slots" / 4
128  *
129  * A "slot" is the size of two DSPRAM bank entries; an entry from
130  * DSPRAM bank A and an entry from DSPRAM bank B. One DSPRAM bank
131  * entry is 4 bytes.
132  */
133 #define SLOT_SZ 8
134 static inline unsigned int decode_dspram_size(unsigned int size)
135 {
136         unsigned int _sz = size & 0x7f;
137
138         return _sz * SLOT_SZ * 4;
139 }
140
141 static void dspram_save(struct meta_ext_context *dsp_ctx,
142                         unsigned int ramA_sz, unsigned int ramB_sz)
143 {
144         unsigned int ram_sz[2];
145         int i;
146
147         ram_sz[0] = ramA_sz;
148         ram_sz[1] = ramB_sz;
149
150         for (i = 0; i < 2; i++) {
151                 if (ram_sz[i] != 0) {
152                         unsigned int sz;
153
154                         if (i == 0)
155                                 sz = decode_dspram_size(ram_sz[i] >> 8);
156                         else
157                                 sz = decode_dspram_size(ram_sz[i]);
158
159                         if (dsp_ctx->ram[i] == NULL) {
160                                 dsp_ctx->ram[i] = kmalloc(sz, GFP_KERNEL);
161
162                                 if (dsp_ctx->ram[i] == NULL)
163                                         panic("couldn't save DSP context");
164                         } else {
165                                 if (ram_sz[i] > dsp_ctx->ram_sz[i]) {
166                                         kfree(dsp_ctx->ram[i]);
167
168                                         dsp_ctx->ram[i] = kmalloc(sz,
169                                                                   GFP_KERNEL);
170
171                                         if (dsp_ctx->ram[i] == NULL)
172                                                 panic("couldn't save DSP context");
173                                 }
174                         }
175
176                         if (i == 0)
177                                 __TBIDspramSaveA(ram_sz[i], dsp_ctx->ram[i]);
178                         else
179                                 __TBIDspramSaveB(ram_sz[i], dsp_ctx->ram[i]);
180
181                         dsp_ctx->ram_sz[i] = ram_sz[i];
182                 }
183         }
184 }
185 #endif /* CONFIG_METAG_DSP */
186
187 /*
188  * Allow interrupts to be nested and save any "extended" register
189  * context state, e.g. DSP regs and RAMs.
190  */
191 static void nest_interrupts(TBIRES State, unsigned long mask)
192 {
193 #ifdef CONFIG_METAG_DSP
194         struct meta_ext_context *dsp_ctx;
195         unsigned int D0_8;
196
197         /*
198          * D0.8 may contain an ECH encoding. The upper 16 bits
199          * tell us what DSP resources the current process is
200          * using. OR the bits into the SaveMask so that
201          * __TBINestInts() knows what resources to save as
202          * part of this context.
203          *
204          * Don't save the context if we're nesting interrupts in the
205          * kernel because the kernel doesn't use DSP hardware.
206          */
207         D0_8 = __core_reg_get(D0.8);
208
209         if (D0_8 && (State.Sig.SaveMask & TBICTX_PRIV_BIT)) {
210                 State.Sig.SaveMask |= (D0_8 >> 16);
211
212                 dsp_ctx = current->thread.dsp_context;
213                 if (dsp_ctx == NULL) {
214                         dsp_ctx = kzalloc(sizeof(*dsp_ctx), GFP_KERNEL);
215                         if (dsp_ctx == NULL)
216                                 panic("couldn't save DSP context: ENOMEM");
217
218                         current->thread.dsp_context = dsp_ctx;
219                 }
220
221                 current->thread.user_flags |= (D0_8 & 0xffff0000);
222                 __TBINestInts(State, &dsp_ctx->regs, mask);
223                 dspram_save(dsp_ctx, D0_8 & 0x7f00, D0_8 & 0x007f);
224         } else
225                 __TBINestInts(State, NULL, mask);
226 #else
227         __TBINestInts(State, NULL, mask);
228 #endif
229 }
230
231 void head_end(TBIRES State, unsigned long mask)
232 {
233         unsigned int savemask = (unsigned short)State.Sig.SaveMask;
234         unsigned int ctx_savemask = (unsigned short)State.Sig.pCtx->SaveMask;
235
236         if (savemask & TBICTX_PRIV_BIT) {
237                 ctx_savemask |= TBICTX_PRIV_BIT;
238                 current->thread.user_flags = savemask;
239         }
240
241         /* Always undo the sleep bit */
242         ctx_savemask &= ~TBICTX_WAIT_BIT;
243
244         /* Always save the catch buffer and RD pipe if they are dirty */
245         savemask |= TBICTX_XCBF_BIT;
246
247         /* Only save the catch and RD if we have not already done so.
248          * Note - the RD bits are in the pCtx only, and not in the
249          * State.SaveMask.
250          */
251         if ((savemask & TBICTX_CBUF_BIT) ||
252             (ctx_savemask & TBICTX_CBRP_BIT)) {
253                 /* Have we already saved the buffers though?
254                  * - See TestTrack 5071 */
255                 if (ctx_savemask & TBICTX_XCBF_BIT) {
256                         /* Strip off the bits so the call to __TBINestInts
257                          * won't save the buffers again. */
258                         savemask &= ~TBICTX_CBUF_BIT;
259                         ctx_savemask &= ~TBICTX_CBRP_BIT;
260                 }
261         }
262
263 #ifdef CONFIG_METAG_META21
264         {
265                 unsigned int depth, txdefr;
266
267                 /*
268                  * Save TXDEFR state.
269                  *
270                  * The process may have been interrupted after a LNKSET, but
271                  * before it could read the DEFR state, so we mustn't lose that
272                  * state or it could end up retrying an atomic operation that
273                  * succeeded.
274                  *
275                  * All interrupts are disabled at this point so we
276                  * don't need to perform any locking. We must do this
277                  * dance before we use LNKGET or LNKSET.
278                  */
279                 BUG_ON(current->thread.int_depth > HARDIRQ_BITS);
280
281                 depth = current->thread.int_depth++;
282
283                 txdefr = __core_reg_get(TXDEFR);
284
285                 txdefr &= TXDEFR_BUS_STATE_BITS;
286                 if (txdefr & TXDEFR_LNKSET_SUCCESS)
287                         current->thread.txdefr_failure &= ~(1 << depth);
288                 else
289                         current->thread.txdefr_failure |= (1 << depth);
290         }
291 #endif
292
293         State.Sig.SaveMask = savemask;
294         State.Sig.pCtx->SaveMask = ctx_savemask;
295
296         nest_interrupts(State, mask);
297
298 #ifdef CONFIG_METAG_POISON_CATCH_BUFFERS
299         /* Poison the catch registers.  This shows up any mistakes we have
300          * made in their handling MUCH quicker.
301          */
302         __core_reg_set(TXCATCH0, 0x87650021);
303         __core_reg_set(TXCATCH1, 0x87654322);
304         __core_reg_set(TXCATCH2, 0x87654323);
305         __core_reg_set(TXCATCH3, 0x87654324);
306 #endif /* CONFIG_METAG_POISON_CATCH_BUFFERS */
307 }
308
309 TBIRES tail_end_sys(TBIRES State, int syscall, int *restart)
310 {
311         struct pt_regs *regs = (struct pt_regs *)State.Sig.pCtx;
312         unsigned long flags;
313
314         local_irq_disable();
315
316         if (user_mode(regs)) {
317                 flags = current_thread_info()->flags;
318                 if (flags & _TIF_WORK_MASK &&
319                     do_work_pending(regs, flags, syscall)) {
320                         *restart = 1;
321                         return State;
322                 }
323
324 #ifdef CONFIG_METAG_FPU
325                 if (current->thread.fpu_context &&
326                     current->thread.fpu_context->needs_restore) {
327                         __TBICtxFPURestore(State, current->thread.fpu_context);
328                         /*
329                          * Clearing this bit ensures the FP unit is not made
330                          * active again unless it is used.
331                          */
332                         State.Sig.SaveMask &= ~TBICTX_FPAC_BIT;
333                         current->thread.fpu_context->needs_restore = false;
334                 }
335                 State.Sig.TrigMask |= TBI_TRIG_BIT(TBID_SIGNUM_DFR);
336 #endif
337         }
338
339         /* TBI will turn interrupts back on at some point. */
340         if (!irqs_disabled_flags((unsigned long)State.Sig.TrigMask))
341                 trace_hardirqs_on();
342
343 #ifdef CONFIG_METAG_DSP
344         /*
345          * If we previously saved an extended context then restore it
346          * now. Otherwise, clear D0.8 because this process is not
347          * using DSP hardware.
348          */
349         if (State.Sig.pCtx->SaveMask & TBICTX_XEXT_BIT) {
350                 unsigned int D0_8;
351                 struct meta_ext_context *dsp_ctx = current->thread.dsp_context;
352
353                 /* Make sure we're going to return to userland. */
354                 BUG_ON(current->thread.int_depth != 1);
355
356                 if (dsp_ctx->ram_sz[0] > 0)
357                         __TBIDspramRestoreA(dsp_ctx->ram_sz[0],
358                                             dsp_ctx->ram[0]);
359                 if (dsp_ctx->ram_sz[1] > 0)
360                         __TBIDspramRestoreB(dsp_ctx->ram_sz[1],
361                                             dsp_ctx->ram[1]);
362
363                 State.Sig.SaveMask |= State.Sig.pCtx->SaveMask;
364                 __TBICtxRestore(State, current->thread.dsp_context);
365                 D0_8 = __core_reg_get(D0.8);
366                 D0_8 |= current->thread.user_flags & 0xffff0000;
367                 D0_8 |= (dsp_ctx->ram_sz[1] | dsp_ctx->ram_sz[0]) & 0xffff;
368                 __core_reg_set(D0.8, D0_8);
369         } else
370                 __core_reg_set(D0.8, 0);
371 #endif /* CONFIG_METAG_DSP */
372
373 #ifdef CONFIG_METAG_META21
374         {
375                 unsigned int depth, txdefr;
376
377                 /*
378                  * If there hasn't been a LNKSET since the last LNKGET then the
379                  * link flag will be set, causing the next LNKSET to succeed if
380                  * the addresses match. The two LNK operations may not be a pair
381                  * (e.g. see atomic_read()), so the LNKSET should fail.
382                  * We use a conditional-never LNKSET to clear the link flag
383                  * without side effects.
384                  */
385                 asm volatile("LNKSETDNV [D0Re0],D0Re0");
386
387                 depth = --current->thread.int_depth;
388
389                 BUG_ON(user_mode(regs) && depth);
390
391                 txdefr = __core_reg_get(TXDEFR);
392
393                 txdefr &= ~TXDEFR_BUS_STATE_BITS;
394
395                 /* Do we need to restore a failure code into TXDEFR? */
396                 if (current->thread.txdefr_failure & (1 << depth))
397                         txdefr |= (TXDEFR_LNKSET_FAILURE | TXDEFR_BUS_TRIG_BIT);
398                 else
399                         txdefr |= (TXDEFR_LNKSET_SUCCESS | TXDEFR_BUS_TRIG_BIT);
400
401                 __core_reg_set(TXDEFR, txdefr);
402         }
403 #endif
404         return State;
405 }
406
407 #ifdef CONFIG_SMP
408 /*
409  * If we took an interrupt in the middle of __kuser_get_tls then we need
410  * to rewind the PC to the start of the function in case the process
411  * gets migrated to another thread (SMP only) and it reads the wrong tls
412  * data.
413  */
414 static inline void _restart_critical_section(TBIRES State)
415 {
416         unsigned long get_tls_start;
417         unsigned long get_tls_end;
418
419         get_tls_start = (unsigned long)__kuser_get_tls -
420                 (unsigned long)&__user_gateway_start;
421
422         get_tls_start += USER_GATEWAY_PAGE;
423
424         get_tls_end = (unsigned long)__kuser_get_tls_end -
425                 (unsigned long)&__user_gateway_start;
426
427         get_tls_end += USER_GATEWAY_PAGE;
428
429         if ((State.Sig.pCtx->CurrPC >= get_tls_start) &&
430             (State.Sig.pCtx->CurrPC < get_tls_end))
431                 State.Sig.pCtx->CurrPC = get_tls_start;
432 }
433 #else
434 /*
435  * If we took an interrupt in the middle of
436  * __kuser_cmpxchg then we need to rewind the PC to the
437  * start of the function.
438  */
439 static inline void _restart_critical_section(TBIRES State)
440 {
441         unsigned long cmpxchg_start;
442         unsigned long cmpxchg_end;
443
444         cmpxchg_start = (unsigned long)__kuser_cmpxchg -
445                 (unsigned long)&__user_gateway_start;
446
447         cmpxchg_start += USER_GATEWAY_PAGE;
448
449         cmpxchg_end = (unsigned long)__kuser_cmpxchg_end -
450                 (unsigned long)&__user_gateway_start;
451
452         cmpxchg_end += USER_GATEWAY_PAGE;
453
454         if ((State.Sig.pCtx->CurrPC >= cmpxchg_start) &&
455             (State.Sig.pCtx->CurrPC < cmpxchg_end))
456                 State.Sig.pCtx->CurrPC = cmpxchg_start;
457 }
458 #endif
459
460 /* Used by kick_handler() */
461 void restart_critical_section(TBIRES State)
462 {
463         _restart_critical_section(State);
464 }
465
466 TBIRES trigger_handler(TBIRES State, int SigNum, int Triggers, int Inst,
467                        PTBI pTBI)
468 {
469         head_end(State, ~INTS_OFF_MASK);
470
471         /* If we interrupted user code handle any critical sections. */
472         if (State.Sig.SaveMask & TBICTX_PRIV_BIT)
473                 _restart_critical_section(State);
474
475         trace_hardirqs_off();
476
477         do_IRQ(SigNum, (struct pt_regs *)State.Sig.pCtx);
478
479         return tail_end(State);
480 }
481
482 static unsigned int load_fault(PTBICTXEXTCB0 pbuf)
483 {
484         return pbuf->CBFlags & TXCATCH0_READ_BIT;
485 }
486
487 static unsigned long fault_address(PTBICTXEXTCB0 pbuf)
488 {
489         return pbuf->CBAddr;
490 }
491
492 static void unhandled_fault(struct pt_regs *regs, unsigned long addr,
493                             int signo, int code, int trapno)
494 {
495         if (user_mode(regs)) {
496                 siginfo_t info;
497
498                 if (show_unhandled_signals && unhandled_signal(current, signo)
499                     && printk_ratelimit()) {
500
501                         pr_info("pid %d unhandled fault: pc 0x%08x, addr 0x%08lx, trap %d (%s)\n",
502                                 current->pid, regs->ctx.CurrPC, addr,
503                                 trapno, trap_name(trapno));
504                         print_vma_addr(" in ", regs->ctx.CurrPC);
505                         print_vma_addr(" rtp in ", regs->ctx.DX[4].U1);
506                         printk("\n");
507                         show_regs(regs);
508                 }
509
510                 info.si_signo = signo;
511                 info.si_errno = 0;
512                 info.si_code = code;
513                 info.si_addr = (__force void __user *)addr;
514                 info.si_trapno = trapno;
515                 force_sig_info(signo, &info, current);
516         } else {
517                 die("Oops", regs, trapno, addr);
518         }
519 }
520
521 static int handle_data_fault(PTBICTXEXTCB0 pcbuf, struct pt_regs *regs,
522                              unsigned int data_address, int trapno)
523 {
524         int ret;
525
526         ret = do_page_fault(regs, data_address, !load_fault(pcbuf), trapno);
527
528         return ret;
529 }
530
531 static unsigned long get_inst_fault_address(struct pt_regs *regs)
532 {
533         return regs->ctx.CurrPC;
534 }
535
536 TBIRES fault_handler(TBIRES State, int SigNum, int Triggers,
537                      int Inst, PTBI pTBI)
538 {
539         struct pt_regs *regs = (struct pt_regs *)State.Sig.pCtx;
540         PTBICTXEXTCB0 pcbuf = (PTBICTXEXTCB0)&regs->extcb0;
541         unsigned long data_address;
542
543         head_end(State, ~INTS_OFF_MASK);
544
545         /* Hardware breakpoint or data watch */
546         if ((SigNum == TBIXXF_SIGNUM_IHF) ||
547             ((SigNum == TBIXXF_SIGNUM_DHF) &&
548              (pcbuf[0].CBFlags & (TXCATCH0_WATCH1_BIT |
549                                   TXCATCH0_WATCH0_BIT)))) {
550                 State = __TBIUnExpXXX(State, SigNum, Triggers, Inst,
551                                       pTBI);
552                 return tail_end(State);
553         }
554
555         local_irq_enable();
556
557         data_address = fault_address(pcbuf);
558
559         switch (SigNum) {
560         case TBIXXF_SIGNUM_IGF:
561                 /* 1st-level entry invalid (instruction fetch) */
562         case TBIXXF_SIGNUM_IPF: {
563                 /* 2nd-level entry invalid (instruction fetch) */
564                 unsigned long addr = get_inst_fault_address(regs);
565                 do_page_fault(regs, addr, 0, SigNum);
566                 break;
567         }
568
569         case TBIXXF_SIGNUM_DGF:
570                 /* 1st-level entry invalid (data access) */
571         case TBIXXF_SIGNUM_DPF:
572                 /* 2nd-level entry invalid (data access) */
573         case TBIXXF_SIGNUM_DWF:
574                 /* Write to read only page */
575                 handle_data_fault(pcbuf, regs, data_address, SigNum);
576                 break;
577
578         case TBIXXF_SIGNUM_IIF:
579                 /* Illegal instruction */
580                 unhandled_fault(regs, regs->ctx.CurrPC, SIGILL, ILL_ILLOPC,
581                                 SigNum);
582                 break;
583
584         case TBIXXF_SIGNUM_DHF:
585                 /* Unaligned access */
586                 unhandled_fault(regs, data_address, SIGBUS, BUS_ADRALN,
587                                 SigNum);
588                 break;
589         case TBIXXF_SIGNUM_PGF:
590                 /* Privilege violation */
591                 unhandled_fault(regs, data_address, SIGSEGV, SEGV_ACCERR,
592                                 SigNum);
593                 break;
594         default:
595                 BUG();
596                 break;
597         }
598
599         return tail_end(State);
600 }
601
602 static bool switch_is_syscall(unsigned int inst)
603 {
604         return inst == __METAG_SW_ENCODING(SYS);
605 }
606
607 static bool switch_is_legacy_syscall(unsigned int inst)
608 {
609         return inst == __METAG_SW_ENCODING(SYS_LEGACY);
610 }
611
612 static inline void step_over_switch(struct pt_regs *regs, unsigned int inst)
613 {
614         regs->ctx.CurrPC += 4;
615 }
616
617 static inline int test_syscall_work(void)
618 {
619         return current_thread_info()->flags & _TIF_WORK_SYSCALL_MASK;
620 }
621
622 TBIRES switch1_handler(TBIRES State, int SigNum, int Triggers,
623                        int Inst, PTBI pTBI)
624 {
625         struct pt_regs *regs = (struct pt_regs *)State.Sig.pCtx;
626         unsigned int sysnumber;
627         unsigned long long a1_a2, a3_a4, a5_a6;
628         LPSYSCALL syscall_entry;
629         int restart;
630
631         head_end(State, ~INTS_OFF_MASK);
632
633         /*
634          * If this is not a syscall SWITCH it could be a breakpoint.
635          */
636         if (!switch_is_syscall(Inst)) {
637                 /*
638                  * Alert the user if they're trying to use legacy system
639                  * calls. This suggests they need to update their C
640                  * library and build against up to date kernel headers.
641                  */
642                 if (switch_is_legacy_syscall(Inst))
643                         pr_warn_once("WARNING: A legacy syscall was made. Your userland needs updating.\n");
644                 /*
645                  * We don't know how to handle the SWITCH and cannot
646                  * safely ignore it, so treat all unknown switches
647                  * (including breakpoints) as traps.
648                  */
649                 force_sig(SIGTRAP, current);
650                 return tail_end(State);
651         }
652
653         local_irq_enable();
654
655 restart_syscall:
656         restart = 0;
657         sysnumber = regs->ctx.DX[0].U1;
658
659         if (test_syscall_work())
660                 sysnumber = syscall_trace_enter(regs);
661
662         /* Skip over the SWITCH instruction - or you just get 'stuck' on it! */
663         step_over_switch(regs, Inst);
664
665         if (sysnumber >= __NR_syscalls) {
666                 pr_debug("unknown syscall number: %d\n", sysnumber);
667                 syscall_entry = (LPSYSCALL) sys_ni_syscall;
668         } else {
669                 syscall_entry = (LPSYSCALL) sys_call_table[sysnumber];
670         }
671
672         /* Use 64bit loads for speed. */
673         a5_a6 = *(unsigned long long *)&regs->ctx.DX[1];
674         a3_a4 = *(unsigned long long *)&regs->ctx.DX[2];
675         a1_a2 = *(unsigned long long *)&regs->ctx.DX[3];
676
677         /* here is the actual call to the syscall handler functions */
678         regs->ctx.DX[0].U0 = syscall_entry(a1_a2, a3_a4, a5_a6);
679
680         if (test_syscall_work())
681                 syscall_trace_leave(regs);
682
683         State = tail_end_sys(State, sysnumber, &restart);
684         /* Handlerless restarts shouldn't go via userland */
685         if (restart)
686                 goto restart_syscall;
687         return State;
688 }
689
690 TBIRES switchx_handler(TBIRES State, int SigNum, int Triggers,
691                        int Inst, PTBI pTBI)
692 {
693         struct pt_regs *regs = (struct pt_regs *)State.Sig.pCtx;
694
695         /*
696          * This can be caused by any user process simply executing an unusual
697          * SWITCH instruction. If there's no DA, __TBIUnExpXXX will cause the
698          * thread to stop, so signal a SIGTRAP instead.
699          */
700         head_end(State, ~INTS_OFF_MASK);
701         if (user_mode(regs))
702                 force_sig(SIGTRAP, current);
703         else
704                 State = __TBIUnExpXXX(State, SigNum, Triggers, Inst, pTBI);
705         return tail_end(State);
706 }
707
708 #ifdef CONFIG_METAG_META21
709 TBIRES fpe_handler(TBIRES State, int SigNum, int Triggers, int Inst, PTBI pTBI)
710 {
711         struct pt_regs *regs = (struct pt_regs *)State.Sig.pCtx;
712         unsigned int error_state = Triggers;
713         siginfo_t info;
714
715         head_end(State, ~INTS_OFF_MASK);
716
717         local_irq_enable();
718
719         info.si_signo = SIGFPE;
720
721         if (error_state & TXSTAT_FPE_INVALID_BIT)
722                 info.si_code = FPE_FLTINV;
723         else if (error_state & TXSTAT_FPE_DIVBYZERO_BIT)
724                 info.si_code = FPE_FLTDIV;
725         else if (error_state & TXSTAT_FPE_OVERFLOW_BIT)
726                 info.si_code = FPE_FLTOVF;
727         else if (error_state & TXSTAT_FPE_UNDERFLOW_BIT)
728                 info.si_code = FPE_FLTUND;
729         else if (error_state & TXSTAT_FPE_INEXACT_BIT)
730                 info.si_code = FPE_FLTRES;
731         else
732                 info.si_code = 0;
733         info.si_errno = 0;
734         info.si_addr = (__force void __user *)regs->ctx.CurrPC;
735         force_sig_info(SIGFPE, &info, current);
736
737         return tail_end(State);
738 }
739 #endif
740
741 #ifdef CONFIG_METAG_SUSPEND_MEM
742 struct traps_context {
743         PTBIAPIFN fnSigs[TBID_SIGNUM_MAX + 1];
744 };
745
746 static struct traps_context *metag_traps_context;
747
748 int traps_save_context(void)
749 {
750         unsigned long cpu = smp_processor_id();
751         PTBI _pTBI = per_cpu(pTBI, cpu);
752         struct traps_context *context;
753
754         context = kzalloc(sizeof(*context), GFP_ATOMIC);
755         if (!context)
756                 return -ENOMEM;
757
758         memcpy(context->fnSigs, (void *)_pTBI->fnSigs, sizeof(context->fnSigs));
759
760         metag_traps_context = context;
761         return 0;
762 }
763
764 int traps_restore_context(void)
765 {
766         unsigned long cpu = smp_processor_id();
767         PTBI _pTBI = per_cpu(pTBI, cpu);
768         struct traps_context *context = metag_traps_context;
769
770         metag_traps_context = NULL;
771
772         memcpy((void *)_pTBI->fnSigs, context->fnSigs, sizeof(context->fnSigs));
773
774         kfree(context);
775         return 0;
776 }
777 #endif
778
779 #ifdef CONFIG_SMP
780 static inline unsigned int _get_trigger_mask(void)
781 {
782         unsigned long cpu = smp_processor_id();
783         return per_cpu(trigger_mask, cpu);
784 }
785
786 unsigned int get_trigger_mask(void)
787 {
788         return _get_trigger_mask();
789 }
790
791 static void set_trigger_mask(unsigned int mask)
792 {
793         unsigned long cpu = smp_processor_id();
794         per_cpu(trigger_mask, cpu) = mask;
795 }
796
797 void arch_local_irq_enable(void)
798 {
799         preempt_disable();
800         arch_local_irq_restore(_get_trigger_mask());
801         preempt_enable_no_resched();
802 }
803 EXPORT_SYMBOL(arch_local_irq_enable);
804 #else
805 static void set_trigger_mask(unsigned int mask)
806 {
807         global_trigger_mask = mask;
808 }
809 #endif
810
811 void __cpuinit per_cpu_trap_init(unsigned long cpu)
812 {
813         TBIRES int_context;
814         unsigned int thread = cpu_2_hwthread_id[cpu];
815
816         set_trigger_mask(TBI_INTS_INIT(thread) | /* interrupts */
817                          TBI_TRIG_BIT(TBID_SIGNUM_LWK) | /* low level kick */
818                          TBI_TRIG_BIT(TBID_SIGNUM_SW1) |
819                          TBI_TRIG_BIT(TBID_SIGNUM_SWS));
820
821         /* non-priv - use current stack */
822         int_context.Sig.pCtx = NULL;
823         /* Start with interrupts off */
824         int_context.Sig.TrigMask = INTS_OFF_MASK;
825         int_context.Sig.SaveMask = 0;
826
827         /* And call __TBIASyncTrigger() */
828         __TBIASyncTrigger(int_context);
829 }
830
831 void __init trap_init(void)
832 {
833         unsigned long cpu = smp_processor_id();
834         PTBI _pTBI = per_cpu(pTBI, cpu);
835
836         _pTBI->fnSigs[TBID_SIGNUM_XXF] = fault_handler;
837         _pTBI->fnSigs[TBID_SIGNUM_SW0] = switchx_handler;
838         _pTBI->fnSigs[TBID_SIGNUM_SW1] = switch1_handler;
839         _pTBI->fnSigs[TBID_SIGNUM_SW2] = switchx_handler;
840         _pTBI->fnSigs[TBID_SIGNUM_SW3] = switchx_handler;
841         _pTBI->fnSigs[TBID_SIGNUM_SWK] = kick_handler;
842
843 #ifdef CONFIG_METAG_META21
844         _pTBI->fnSigs[TBID_SIGNUM_DFR] = __TBIHandleDFR;
845         _pTBI->fnSigs[TBID_SIGNUM_FPE] = fpe_handler;
846 #endif
847
848         per_cpu_trap_init(cpu);
849 }
850
851 void tbi_startup_interrupt(int irq)
852 {
853         unsigned long cpu = smp_processor_id();
854         PTBI _pTBI = per_cpu(pTBI, cpu);
855
856         BUG_ON(irq > TBID_SIGNUM_MAX);
857
858         /* For TR1 and TR2, the thread id is encoded in the irq number */
859         if (irq >= TBID_SIGNUM_T10 && irq < TBID_SIGNUM_TR3)
860                 cpu = hwthread_id_2_cpu[(irq - TBID_SIGNUM_T10) % 4];
861
862         set_trigger_mask(get_trigger_mask() | TBI_TRIG_BIT(irq));
863
864         _pTBI->fnSigs[irq] = trigger_handler;
865 }
866
867 void tbi_shutdown_interrupt(int irq)
868 {
869         unsigned long cpu = smp_processor_id();
870         PTBI _pTBI = per_cpu(pTBI, cpu);
871
872         BUG_ON(irq > TBID_SIGNUM_MAX);
873
874         set_trigger_mask(get_trigger_mask() & ~TBI_TRIG_BIT(irq));
875
876         _pTBI->fnSigs[irq] = __TBIUnExpXXX;
877 }
878
879 int ret_from_fork(TBIRES arg)
880 {
881         struct task_struct *prev = arg.Switch.pPara;
882         struct task_struct *tsk = current;
883         struct pt_regs *regs = task_pt_regs(tsk);
884         int (*fn)(void *);
885         TBIRES Next;
886
887         schedule_tail(prev);
888
889         if (tsk->flags & PF_KTHREAD) {
890                 fn = (void *)regs->ctx.DX[4].U1;
891                 BUG_ON(!fn);
892
893                 fn((void *)regs->ctx.DX[3].U1);
894         }
895
896         if (test_syscall_work())
897                 syscall_trace_leave(regs);
898
899         preempt_disable();
900
901         Next.Sig.TrigMask = get_trigger_mask();
902         Next.Sig.SaveMask = 0;
903         Next.Sig.pCtx = &regs->ctx;
904
905         set_gateway_tls(current->thread.tls_ptr);
906
907         preempt_enable_no_resched();
908
909         /* And interrupts should come back on when we resume the real usermode
910          * code. Call __TBIASyncResume()
911          */
912         __TBIASyncResume(tail_end(Next));
913         /* ASyncResume should NEVER return */
914         BUG();
915         return 0;
916 }
917
918 void show_trace(struct task_struct *tsk, unsigned long *sp,
919                 struct pt_regs *regs)
920 {
921         unsigned long addr;
922 #ifdef CONFIG_FRAME_POINTER
923         unsigned long fp, fpnew;
924         unsigned long stack;
925 #endif
926
927         if (regs && user_mode(regs))
928                 return;
929
930         printk("\nCall trace: ");
931 #ifdef CONFIG_KALLSYMS
932         printk("\n");
933 #endif
934
935         if (!tsk)
936                 tsk = current;
937
938 #ifdef CONFIG_FRAME_POINTER
939         if (regs) {
940                 print_ip_sym(regs->ctx.CurrPC);
941                 fp = regs->ctx.AX[1].U0;
942         } else {
943                 fp = __core_reg_get(A0FrP);
944         }
945
946         /* detect when the frame pointer has been used for other purposes and
947          * doesn't point to the stack (it may point completely elsewhere which
948          * kstack_end may not detect).
949          */
950         stack = (unsigned long)task_stack_page(tsk);
951         while (fp >= stack && fp + 8 <= stack + THREAD_SIZE) {
952                 addr = __raw_readl((unsigned long *)(fp + 4)) - 4;
953                 if (kernel_text_address(addr))
954                         print_ip_sym(addr);
955                 else
956                         break;
957                 /* stack grows up, so frame pointers must decrease */
958                 fpnew = __raw_readl((unsigned long *)(fp + 0));
959                 if (fpnew >= fp)
960                         break;
961                 fp = fpnew;
962         }
963 #else
964         while (!kstack_end(sp)) {
965                 addr = (*sp--) - 4;
966                 if (kernel_text_address(addr))
967                         print_ip_sym(addr);
968         }
969 #endif
970
971         printk("\n");
972
973         debug_show_held_locks(tsk);
974 }
975
976 void show_stack(struct task_struct *tsk, unsigned long *sp)
977 {
978         if (!tsk)
979                 tsk = current;
980         if (tsk == current)
981                 sp = (unsigned long *)current_stack_pointer;
982         else
983                 sp = (unsigned long *)tsk->thread.kernel_context->AX[0].U0;
984
985         show_trace(tsk, sp, NULL);
986 }
987
988 void dump_stack(void)
989 {
990         show_stack(NULL, NULL);
991 }
992 EXPORT_SYMBOL(dump_stack);