b19b951fd3e7c12f68bdac3783bb403bf2e62dd3
[linux-2.6.git] / arch / m68k / kernel / traps.c
1 /*
2  *  linux/arch/m68k/kernel/traps.c
3  *
4  *  Copyright (C) 1993, 1994 by Hamish Macdonald
5  *
6  *  68040 fixes by Michael Rausch
7  *  68040 fixes by Martin Apel
8  *  68040 fixes and writeback by Richard Zidlicky
9  *  68060 fixes by Roman Hodek
10  *  68060 fixes by Jesper Skov
11  *
12  * This file is subject to the terms and conditions of the GNU General Public
13  * License.  See the file COPYING in the main directory of this archive
14  * for more details.
15  */
16
17 /*
18  * Sets up all exception vectors
19  */
20
21 #include <linux/config.h>
22 #include <linux/sched.h>
23 #include <linux/signal.h>
24 #include <linux/kernel.h>
25 #include <linux/mm.h>
26 #include <linux/module.h>
27 #include <linux/a.out.h>
28 #include <linux/user.h>
29 #include <linux/string.h>
30 #include <linux/linkage.h>
31 #include <linux/init.h>
32 #include <linux/ptrace.h>
33 #include <linux/kallsyms.h>
34
35 #include <asm/setup.h>
36 #include <asm/fpu.h>
37 #include <asm/system.h>
38 #include <asm/uaccess.h>
39 #include <asm/traps.h>
40 #include <asm/pgalloc.h>
41 #include <asm/machdep.h>
42 #include <asm/siginfo.h>
43
44 /* assembler routines */
45 asmlinkage void system_call(void);
46 asmlinkage void buserr(void);
47 asmlinkage void trap(void);
48 asmlinkage void nmihandler(void);
49 #ifdef CONFIG_M68KFPU_EMU
50 asmlinkage void fpu_emu(void);
51 #endif
52
53 e_vector vectors[256] = {
54         [VEC_BUSERR]    = buserr,
55         [VEC_SYS]       = system_call,
56 };
57
58 /* nmi handler for the Amiga */
59 asm(".text\n"
60     __ALIGN_STR "\n"
61     "nmihandler: rte");
62
63 /*
64  * this must be called very early as the kernel might
65  * use some instruction that are emulated on the 060
66  */
67 void __init base_trap_init(void)
68 {
69         if(MACH_IS_SUN3X) {
70                 extern e_vector *sun3x_prom_vbr;
71
72                 __asm__ volatile ("movec %%vbr, %0" : "=r" (sun3x_prom_vbr));
73         }
74
75         /* setup the exception vector table */
76         __asm__ volatile ("movec %0,%%vbr" : : "r" ((void*)vectors));
77
78         if (CPU_IS_060) {
79                 /* set up ISP entry points */
80                 asmlinkage void unimp_vec(void) asm ("_060_isp_unimp");
81
82                 vectors[VEC_UNIMPII] = unimp_vec;
83         }
84 }
85
86 void __init trap_init (void)
87 {
88         int i;
89
90         vectors[VEC_SPUR] = bad_inthandler;
91         for (i = VEC_INT1; i <= VEC_INT7; i++)
92                 vectors[i] = auto_inthandler;
93
94         for (i = 0; i < VEC_USER; i++)
95                 if (!vectors[i])
96                         vectors[i] = trap;
97
98         for (i = VEC_USER; i < 256; i++)
99                 vectors[i] = mach_inthandler;
100
101 #ifdef CONFIG_M68KFPU_EMU
102         if (FPU_IS_EMU)
103                 vectors[VEC_LINE11] = fpu_emu;
104 #endif
105
106         if (CPU_IS_040 && !FPU_IS_EMU) {
107                 /* set up FPSP entry points */
108                 asmlinkage void dz_vec(void) asm ("dz");
109                 asmlinkage void inex_vec(void) asm ("inex");
110                 asmlinkage void ovfl_vec(void) asm ("ovfl");
111                 asmlinkage void unfl_vec(void) asm ("unfl");
112                 asmlinkage void snan_vec(void) asm ("snan");
113                 asmlinkage void operr_vec(void) asm ("operr");
114                 asmlinkage void bsun_vec(void) asm ("bsun");
115                 asmlinkage void fline_vec(void) asm ("fline");
116                 asmlinkage void unsupp_vec(void) asm ("unsupp");
117
118                 vectors[VEC_FPDIVZ] = dz_vec;
119                 vectors[VEC_FPIR] = inex_vec;
120                 vectors[VEC_FPOVER] = ovfl_vec;
121                 vectors[VEC_FPUNDER] = unfl_vec;
122                 vectors[VEC_FPNAN] = snan_vec;
123                 vectors[VEC_FPOE] = operr_vec;
124                 vectors[VEC_FPBRUC] = bsun_vec;
125                 vectors[VEC_LINE11] = fline_vec;
126                 vectors[VEC_FPUNSUP] = unsupp_vec;
127         }
128
129         if (CPU_IS_060 && !FPU_IS_EMU) {
130                 /* set up IFPSP entry points */
131                 asmlinkage void snan_vec6(void) asm ("_060_fpsp_snan");
132                 asmlinkage void operr_vec6(void) asm ("_060_fpsp_operr");
133                 asmlinkage void ovfl_vec6(void) asm ("_060_fpsp_ovfl");
134                 asmlinkage void unfl_vec6(void) asm ("_060_fpsp_unfl");
135                 asmlinkage void dz_vec6(void) asm ("_060_fpsp_dz");
136                 asmlinkage void inex_vec6(void) asm ("_060_fpsp_inex");
137                 asmlinkage void fline_vec6(void) asm ("_060_fpsp_fline");
138                 asmlinkage void unsupp_vec6(void) asm ("_060_fpsp_unsupp");
139                 asmlinkage void effadd_vec6(void) asm ("_060_fpsp_effadd");
140
141                 vectors[VEC_FPNAN] = snan_vec6;
142                 vectors[VEC_FPOE] = operr_vec6;
143                 vectors[VEC_FPOVER] = ovfl_vec6;
144                 vectors[VEC_FPUNDER] = unfl_vec6;
145                 vectors[VEC_FPDIVZ] = dz_vec6;
146                 vectors[VEC_FPIR] = inex_vec6;
147                 vectors[VEC_LINE11] = fline_vec6;
148                 vectors[VEC_FPUNSUP] = unsupp_vec6;
149                 vectors[VEC_UNIMPEA] = effadd_vec6;
150         }
151
152         /* if running on an amiga, make the NMI interrupt do nothing */
153         if (MACH_IS_AMIGA) {
154                 vectors[VEC_INT7] = nmihandler;
155         }
156 }
157
158
159 static const char *vec_names[] = {
160         [VEC_RESETSP]   = "RESET SP",
161         [VEC_RESETPC]   = "RESET PC",
162         [VEC_BUSERR]    = "BUS ERROR",
163         [VEC_ADDRERR]   = "ADDRESS ERROR",
164         [VEC_ILLEGAL]   = "ILLEGAL INSTRUCTION",
165         [VEC_ZERODIV]   = "ZERO DIVIDE",
166         [VEC_CHK]       = "CHK",
167         [VEC_TRAP]      = "TRAPcc",
168         [VEC_PRIV]      = "PRIVILEGE VIOLATION",
169         [VEC_TRACE]     = "TRACE",
170         [VEC_LINE10]    = "LINE 1010",
171         [VEC_LINE11]    = "LINE 1111",
172         [VEC_RESV12]    = "UNASSIGNED RESERVED 12",
173         [VEC_COPROC]    = "COPROCESSOR PROTOCOL VIOLATION",
174         [VEC_FORMAT]    = "FORMAT ERROR",
175         [VEC_UNINT]     = "UNINITIALIZED INTERRUPT",
176         [VEC_RESV16]    = "UNASSIGNED RESERVED 16",
177         [VEC_RESV17]    = "UNASSIGNED RESERVED 17",
178         [VEC_RESV18]    = "UNASSIGNED RESERVED 18",
179         [VEC_RESV19]    = "UNASSIGNED RESERVED 19",
180         [VEC_RESV20]    = "UNASSIGNED RESERVED 20",
181         [VEC_RESV21]    = "UNASSIGNED RESERVED 21",
182         [VEC_RESV22]    = "UNASSIGNED RESERVED 22",
183         [VEC_RESV23]    = "UNASSIGNED RESERVED 23",
184         [VEC_SPUR]      = "SPURIOUS INTERRUPT",
185         [VEC_INT1]      = "LEVEL 1 INT",
186         [VEC_INT2]      = "LEVEL 2 INT",
187         [VEC_INT3]      = "LEVEL 3 INT",
188         [VEC_INT4]      = "LEVEL 4 INT",
189         [VEC_INT5]      = "LEVEL 5 INT",
190         [VEC_INT6]      = "LEVEL 6 INT",
191         [VEC_INT7]      = "LEVEL 7 INT",
192         [VEC_SYS]       = "SYSCALL",
193         [VEC_TRAP1]     = "TRAP #1",
194         [VEC_TRAP2]     = "TRAP #2",
195         [VEC_TRAP3]     = "TRAP #3",
196         [VEC_TRAP4]     = "TRAP #4",
197         [VEC_TRAP5]     = "TRAP #5",
198         [VEC_TRAP6]     = "TRAP #6",
199         [VEC_TRAP7]     = "TRAP #7",
200         [VEC_TRAP8]     = "TRAP #8",
201         [VEC_TRAP9]     = "TRAP #9",
202         [VEC_TRAP10]    = "TRAP #10",
203         [VEC_TRAP11]    = "TRAP #11",
204         [VEC_TRAP12]    = "TRAP #12",
205         [VEC_TRAP13]    = "TRAP #13",
206         [VEC_TRAP14]    = "TRAP #14",
207         [VEC_TRAP15]    = "TRAP #15",
208         [VEC_FPBRUC]    = "FPCP BSUN",
209         [VEC_FPIR]      = "FPCP INEXACT",
210         [VEC_FPDIVZ]    = "FPCP DIV BY 0",
211         [VEC_FPUNDER]   = "FPCP UNDERFLOW",
212         [VEC_FPOE]      = "FPCP OPERAND ERROR",
213         [VEC_FPOVER]    = "FPCP OVERFLOW",
214         [VEC_FPNAN]     = "FPCP SNAN",
215         [VEC_FPUNSUP]   = "FPCP UNSUPPORTED OPERATION",
216         [VEC_MMUCFG]    = "MMU CONFIGURATION ERROR",
217         [VEC_MMUILL]    = "MMU ILLEGAL OPERATION ERROR",
218         [VEC_MMUACC]    = "MMU ACCESS LEVEL VIOLATION ERROR",
219         [VEC_RESV59]    = "UNASSIGNED RESERVED 59",
220         [VEC_UNIMPEA]   = "UNASSIGNED RESERVED 60",
221         [VEC_UNIMPII]   = "UNASSIGNED RESERVED 61",
222         [VEC_RESV62]    = "UNASSIGNED RESERVED 62",
223         [VEC_RESV63]    = "UNASSIGNED RESERVED 63",
224 };
225
226 static const char *space_names[] = {
227         [0]             = "Space 0",
228         [USER_DATA]     = "User Data",
229         [USER_PROGRAM]  = "User Program",
230 #ifndef CONFIG_SUN3
231         [3]             = "Space 3",
232 #else
233         [FC_CONTROL]    = "Control",
234 #endif
235         [4]             = "Space 4",
236         [SUPER_DATA]    = "Super Data",
237         [SUPER_PROGRAM] = "Super Program",
238         [CPU_SPACE]     = "CPU"
239 };
240
241 void die_if_kernel(char *,struct pt_regs *,int);
242 asmlinkage int do_page_fault(struct pt_regs *regs, unsigned long address,
243                              unsigned long error_code);
244 int send_fault_sig(struct pt_regs *regs);
245
246 asmlinkage void trap_c(struct frame *fp);
247
248 #if defined (CONFIG_M68060)
249 static inline void access_error060 (struct frame *fp)
250 {
251         unsigned long fslw = fp->un.fmt4.pc; /* is really FSLW for access error */
252
253 #ifdef DEBUG
254         printk("fslw=%#lx, fa=%#lx\n", fslw, fp->un.fmt4.effaddr);
255 #endif
256
257         if (fslw & MMU060_BPE) {
258                 /* branch prediction error -> clear branch cache */
259                 __asm__ __volatile__ ("movec %/cacr,%/d0\n\t"
260                                       "orl   #0x00400000,%/d0\n\t"
261                                       "movec %/d0,%/cacr"
262                                       : : : "d0" );
263                 /* return if there's no other error */
264                 if (!(fslw & MMU060_ERR_BITS) && !(fslw & MMU060_SEE))
265                         return;
266         }
267
268         if (fslw & (MMU060_DESC_ERR | MMU060_WP | MMU060_SP)) {
269                 unsigned long errorcode;
270                 unsigned long addr = fp->un.fmt4.effaddr;
271
272                 if (fslw & MMU060_MA)
273                         addr = (addr + PAGE_SIZE - 1) & PAGE_MASK;
274
275                 errorcode = 1;
276                 if (fslw & MMU060_DESC_ERR) {
277                         __flush_tlb040_one(addr);
278                         errorcode = 0;
279                 }
280                 if (fslw & MMU060_W)
281                         errorcode |= 2;
282 #ifdef DEBUG
283                 printk("errorcode = %d\n", errorcode );
284 #endif
285                 do_page_fault(&fp->ptregs, addr, errorcode);
286         } else if (fslw & (MMU060_SEE)){
287                 /* Software Emulation Error.
288                  * fault during mem_read/mem_write in ifpsp060/os.S
289                  */
290                 send_fault_sig(&fp->ptregs);
291         } else if (!(fslw & (MMU060_RE|MMU060_WE)) ||
292                    send_fault_sig(&fp->ptregs) > 0) {
293                 printk("pc=%#lx, fa=%#lx\n", fp->ptregs.pc, fp->un.fmt4.effaddr);
294                 printk( "68060 access error, fslw=%lx\n", fslw );
295                 trap_c( fp );
296         }
297 }
298 #endif /* CONFIG_M68060 */
299
300 #if defined (CONFIG_M68040)
301 static inline unsigned long probe040(int iswrite, unsigned long addr, int wbs)
302 {
303         unsigned long mmusr;
304         mm_segment_t old_fs = get_fs();
305
306         set_fs(MAKE_MM_SEG(wbs));
307
308         if (iswrite)
309                 asm volatile (".chip 68040; ptestw (%0); .chip 68k" : : "a" (addr));
310         else
311                 asm volatile (".chip 68040; ptestr (%0); .chip 68k" : : "a" (addr));
312
313         asm volatile (".chip 68040; movec %%mmusr,%0; .chip 68k" : "=r" (mmusr));
314
315         set_fs(old_fs);
316
317         return mmusr;
318 }
319
320 static inline int do_040writeback1(unsigned short wbs, unsigned long wba,
321                                    unsigned long wbd)
322 {
323         int res = 0;
324         mm_segment_t old_fs = get_fs();
325
326         /* set_fs can not be moved, otherwise put_user() may oops */
327         set_fs(MAKE_MM_SEG(wbs));
328
329         switch (wbs & WBSIZ_040) {
330         case BA_SIZE_BYTE:
331                 res = put_user(wbd & 0xff, (char *)wba);
332                 break;
333         case BA_SIZE_WORD:
334                 res = put_user(wbd & 0xffff, (short *)wba);
335                 break;
336         case BA_SIZE_LONG:
337                 res = put_user(wbd, (int *)wba);
338                 break;
339         }
340
341         /* set_fs can not be moved, otherwise put_user() may oops */
342         set_fs(old_fs);
343
344
345 #ifdef DEBUG
346         printk("do_040writeback1, res=%d\n",res);
347 #endif
348
349         return res;
350 }
351
352 /* after an exception in a writeback the stack frame corresponding
353  * to that exception is discarded, set a few bits in the old frame
354  * to simulate what it should look like
355  */
356 static inline void fix_xframe040(struct frame *fp, unsigned long wba, unsigned short wbs)
357 {
358         fp->un.fmt7.faddr = wba;
359         fp->un.fmt7.ssw = wbs & 0xff;
360         if (wba != current->thread.faddr)
361             fp->un.fmt7.ssw |= MA_040;
362 }
363
364 static inline void do_040writebacks(struct frame *fp)
365 {
366         int res = 0;
367 #if 0
368         if (fp->un.fmt7.wb1s & WBV_040)
369                 printk("access_error040: cannot handle 1st writeback. oops.\n");
370 #endif
371
372         if ((fp->un.fmt7.wb2s & WBV_040) &&
373             !(fp->un.fmt7.wb2s & WBTT_040)) {
374                 res = do_040writeback1(fp->un.fmt7.wb2s, fp->un.fmt7.wb2a,
375                                        fp->un.fmt7.wb2d);
376                 if (res)
377                         fix_xframe040(fp, fp->un.fmt7.wb2a, fp->un.fmt7.wb2s);
378                 else
379                         fp->un.fmt7.wb2s = 0;
380         }
381
382         /* do the 2nd wb only if the first one was successful (except for a kernel wb) */
383         if (fp->un.fmt7.wb3s & WBV_040 && (!res || fp->un.fmt7.wb3s & 4)) {
384                 res = do_040writeback1(fp->un.fmt7.wb3s, fp->un.fmt7.wb3a,
385                                        fp->un.fmt7.wb3d);
386                 if (res)
387                     {
388                         fix_xframe040(fp, fp->un.fmt7.wb3a, fp->un.fmt7.wb3s);
389
390                         fp->un.fmt7.wb2s = fp->un.fmt7.wb3s;
391                         fp->un.fmt7.wb3s &= (~WBV_040);
392                         fp->un.fmt7.wb2a = fp->un.fmt7.wb3a;
393                         fp->un.fmt7.wb2d = fp->un.fmt7.wb3d;
394                     }
395                 else
396                         fp->un.fmt7.wb3s = 0;
397         }
398
399         if (res)
400                 send_fault_sig(&fp->ptregs);
401 }
402
403 /*
404  * called from sigreturn(), must ensure userspace code didn't
405  * manipulate exception frame to circumvent protection, then complete
406  * pending writebacks
407  * we just clear TM2 to turn it into an userspace access
408  */
409 asmlinkage void berr_040cleanup(struct frame *fp)
410 {
411         fp->un.fmt7.wb2s &= ~4;
412         fp->un.fmt7.wb3s &= ~4;
413
414         do_040writebacks(fp);
415 }
416
417 static inline void access_error040(struct frame *fp)
418 {
419         unsigned short ssw = fp->un.fmt7.ssw;
420         unsigned long mmusr;
421
422 #ifdef DEBUG
423         printk("ssw=%#x, fa=%#lx\n", ssw, fp->un.fmt7.faddr);
424         printk("wb1s=%#x, wb2s=%#x, wb3s=%#x\n", fp->un.fmt7.wb1s,
425                 fp->un.fmt7.wb2s, fp->un.fmt7.wb3s);
426         printk ("wb2a=%lx, wb3a=%lx, wb2d=%lx, wb3d=%lx\n",
427                 fp->un.fmt7.wb2a, fp->un.fmt7.wb3a,
428                 fp->un.fmt7.wb2d, fp->un.fmt7.wb3d);
429 #endif
430
431         if (ssw & ATC_040) {
432                 unsigned long addr = fp->un.fmt7.faddr;
433                 unsigned long errorcode;
434
435                 /*
436                  * The MMU status has to be determined AFTER the address
437                  * has been corrected if there was a misaligned access (MA).
438                  */
439                 if (ssw & MA_040)
440                         addr = (addr + 7) & -8;
441
442                 /* MMU error, get the MMUSR info for this access */
443                 mmusr = probe040(!(ssw & RW_040), addr, ssw);
444 #ifdef DEBUG
445                 printk("mmusr = %lx\n", mmusr);
446 #endif
447                 errorcode = 1;
448                 if (!(mmusr & MMU_R_040)) {
449                         /* clear the invalid atc entry */
450                         __flush_tlb040_one(addr);
451                         errorcode = 0;
452                 }
453
454                 /* despite what documentation seems to say, RMW
455                  * accesses have always both the LK and RW bits set */
456                 if (!(ssw & RW_040) || (ssw & LK_040))
457                         errorcode |= 2;
458
459                 if (do_page_fault(&fp->ptregs, addr, errorcode)) {
460 #ifdef DEBUG
461                         printk("do_page_fault() !=0 \n");
462 #endif
463                         if (user_mode(&fp->ptregs)){
464                                 /* delay writebacks after signal delivery */
465 #ifdef DEBUG
466                                 printk(".. was usermode - return\n");
467 #endif
468                                 return;
469                         }
470                         /* disable writeback into user space from kernel
471                          * (if do_page_fault didn't fix the mapping,
472                          * the writeback won't do good)
473                          */
474 #ifdef DEBUG
475                         printk(".. disabling wb2\n");
476 #endif
477                         if (fp->un.fmt7.wb2a == fp->un.fmt7.faddr)
478                                 fp->un.fmt7.wb2s &= ~WBV_040;
479                 }
480         } else if (send_fault_sig(&fp->ptregs) > 0) {
481                 printk("68040 access error, ssw=%x\n", ssw);
482                 trap_c(fp);
483         }
484
485         do_040writebacks(fp);
486 }
487 #endif /* CONFIG_M68040 */
488
489 #if defined(CONFIG_SUN3)
490 #include <asm/sun3mmu.h>
491
492 extern int mmu_emu_handle_fault (unsigned long, int, int);
493
494 /* sun3 version of bus_error030 */
495
496 static inline void bus_error030 (struct frame *fp)
497 {
498         unsigned char buserr_type = sun3_get_buserr ();
499         unsigned long addr, errorcode;
500         unsigned short ssw = fp->un.fmtb.ssw;
501         extern unsigned long _sun3_map_test_start, _sun3_map_test_end;
502
503 #ifdef DEBUG
504         if (ssw & (FC | FB))
505                 printk ("Instruction fault at %#010lx\n",
506                         ssw & FC ?
507                         fp->ptregs.format == 0xa ? fp->ptregs.pc + 2 : fp->un.fmtb.baddr - 2
508                         :
509                         fp->ptregs.format == 0xa ? fp->ptregs.pc + 4 : fp->un.fmtb.baddr);
510         if (ssw & DF)
511                 printk ("Data %s fault at %#010lx in %s (pc=%#lx)\n",
512                         ssw & RW ? "read" : "write",
513                         fp->un.fmtb.daddr,
514                         space_names[ssw & DFC], fp->ptregs.pc);
515 #endif
516
517         /*
518          * Check if this page should be demand-mapped. This needs to go before
519          * the testing for a bad kernel-space access (demand-mapping applies
520          * to kernel accesses too).
521          */
522
523         if ((ssw & DF)
524             && (buserr_type & (SUN3_BUSERR_PROTERR | SUN3_BUSERR_INVALID))) {
525                 if (mmu_emu_handle_fault (fp->un.fmtb.daddr, ssw & RW, 0))
526                         return;
527         }
528
529         /* Check for kernel-space pagefault (BAD). */
530         if (fp->ptregs.sr & PS_S) {
531                 /* kernel fault must be a data fault to user space */
532                 if (! ((ssw & DF) && ((ssw & DFC) == USER_DATA))) {
533                      // try checking the kernel mappings before surrender
534                      if (mmu_emu_handle_fault (fp->un.fmtb.daddr, ssw & RW, 1))
535                           return;
536                         /* instruction fault or kernel data fault! */
537                         if (ssw & (FC | FB))
538                                 printk ("Instruction fault at %#010lx\n",
539                                         fp->ptregs.pc);
540                         if (ssw & DF) {
541                                 /* was this fault incurred testing bus mappings? */
542                                 if((fp->ptregs.pc >= (unsigned long)&_sun3_map_test_start) &&
543                                    (fp->ptregs.pc <= (unsigned long)&_sun3_map_test_end)) {
544                                         send_fault_sig(&fp->ptregs);
545                                         return;
546                                 }
547
548                                 printk ("Data %s fault at %#010lx in %s (pc=%#lx)\n",
549                                         ssw & RW ? "read" : "write",
550                                         fp->un.fmtb.daddr,
551                                         space_names[ssw & DFC], fp->ptregs.pc);
552                         }
553                         printk ("BAD KERNEL BUSERR\n");
554
555                         die_if_kernel("Oops", &fp->ptregs,0);
556                         force_sig(SIGKILL, current);
557                         return;
558                 }
559         } else {
560                 /* user fault */
561                 if (!(ssw & (FC | FB)) && !(ssw & DF))
562                         /* not an instruction fault or data fault! BAD */
563                         panic ("USER BUSERR w/o instruction or data fault");
564         }
565
566
567         /* First handle the data fault, if any.  */
568         if (ssw & DF) {
569                 addr = fp->un.fmtb.daddr;
570
571 // errorcode bit 0:     0 -> no page            1 -> protection fault
572 // errorcode bit 1:     0 -> read fault         1 -> write fault
573
574 // (buserr_type & SUN3_BUSERR_PROTERR)  -> protection fault
575 // (buserr_type & SUN3_BUSERR_INVALID)  -> invalid page fault
576
577                 if (buserr_type & SUN3_BUSERR_PROTERR)
578                         errorcode = 0x01;
579                 else if (buserr_type & SUN3_BUSERR_INVALID)
580                         errorcode = 0x00;
581                 else {
582 #ifdef DEBUG
583                         printk ("*** unexpected busfault type=%#04x\n", buserr_type);
584                         printk ("invalid %s access at %#lx from pc %#lx\n",
585                                 !(ssw & RW) ? "write" : "read", addr,
586                                 fp->ptregs.pc);
587 #endif
588                         die_if_kernel ("Oops", &fp->ptregs, buserr_type);
589                         force_sig (SIGBUS, current);
590                         return;
591                 }
592
593 //todo: wtf is RM bit? --m
594                 if (!(ssw & RW) || ssw & RM)
595                         errorcode |= 0x02;
596
597                 /* Handle page fault. */
598                 do_page_fault (&fp->ptregs, addr, errorcode);
599
600                 /* Retry the data fault now. */
601                 return;
602         }
603
604         /* Now handle the instruction fault. */
605
606         /* Get the fault address. */
607         if (fp->ptregs.format == 0xA)
608                 addr = fp->ptregs.pc + 4;
609         else
610                 addr = fp->un.fmtb.baddr;
611         if (ssw & FC)
612                 addr -= 2;
613
614         if (buserr_type & SUN3_BUSERR_INVALID) {
615                 if (!mmu_emu_handle_fault (fp->un.fmtb.daddr, 1, 0))
616                         do_page_fault (&fp->ptregs, addr, 0);
617        } else {
618 #ifdef DEBUG
619                 printk ("protection fault on insn access (segv).\n");
620 #endif
621                 force_sig (SIGSEGV, current);
622        }
623 }
624 #else
625 #if defined(CPU_M68020_OR_M68030)
626 static inline void bus_error030 (struct frame *fp)
627 {
628         volatile unsigned short temp;
629         unsigned short mmusr;
630         unsigned long addr, errorcode;
631         unsigned short ssw = fp->un.fmtb.ssw;
632 #ifdef DEBUG
633         unsigned long desc;
634
635         printk ("pid = %x  ", current->pid);
636         printk ("SSW=%#06x  ", ssw);
637
638         if (ssw & (FC | FB))
639                 printk ("Instruction fault at %#010lx\n",
640                         ssw & FC ?
641                         fp->ptregs.format == 0xa ? fp->ptregs.pc + 2 : fp->un.fmtb.baddr - 2
642                         :
643                         fp->ptregs.format == 0xa ? fp->ptregs.pc + 4 : fp->un.fmtb.baddr);
644         if (ssw & DF)
645                 printk ("Data %s fault at %#010lx in %s (pc=%#lx)\n",
646                         ssw & RW ? "read" : "write",
647                         fp->un.fmtb.daddr,
648                         space_names[ssw & DFC], fp->ptregs.pc);
649 #endif
650
651         /* ++andreas: If a data fault and an instruction fault happen
652            at the same time map in both pages.  */
653
654         /* First handle the data fault, if any.  */
655         if (ssw & DF) {
656                 addr = fp->un.fmtb.daddr;
657
658 #ifdef DEBUG
659                 asm volatile ("ptestr %3,%2@,#7,%0\n\t"
660                               "pmove %%psr,%1@"
661                               : "=a&" (desc)
662                               : "a" (&temp), "a" (addr), "d" (ssw));
663 #else
664                 asm volatile ("ptestr %2,%1@,#7\n\t"
665                               "pmove %%psr,%0@"
666                               : : "a" (&temp), "a" (addr), "d" (ssw));
667 #endif
668                 mmusr = temp;
669
670 #ifdef DEBUG
671                 printk("mmusr is %#x for addr %#lx in task %p\n",
672                        mmusr, addr, current);
673                 printk("descriptor address is %#lx, contents %#lx\n",
674                        __va(desc), *(unsigned long *)__va(desc));
675 #endif
676
677                 errorcode = (mmusr & MMU_I) ? 0 : 1;
678                 if (!(ssw & RW) || (ssw & RM))
679                         errorcode |= 2;
680
681                 if (mmusr & (MMU_I | MMU_WP)) {
682                         if (ssw & 4) {
683                                 printk("Data %s fault at %#010lx in %s (pc=%#lx)\n",
684                                        ssw & RW ? "read" : "write",
685                                        fp->un.fmtb.daddr,
686                                        space_names[ssw & DFC], fp->ptregs.pc);
687                                 goto buserr;
688                         }
689                         /* Don't try to do anything further if an exception was
690                            handled. */
691                         if (do_page_fault (&fp->ptregs, addr, errorcode) < 0)
692                                 return;
693                 } else if (!(mmusr & MMU_I)) {
694                         /* probably a 020 cas fault */
695                         if (!(ssw & RM) && send_fault_sig(&fp->ptregs) > 0)
696                                 printk("unexpected bus error (%#x,%#x)\n", ssw, mmusr);
697                 } else if (mmusr & (MMU_B|MMU_L|MMU_S)) {
698                         printk("invalid %s access at %#lx from pc %#lx\n",
699                                !(ssw & RW) ? "write" : "read", addr,
700                                fp->ptregs.pc);
701                         die_if_kernel("Oops",&fp->ptregs,mmusr);
702                         force_sig(SIGSEGV, current);
703                         return;
704                 } else {
705 #if 0
706                         static volatile long tlong;
707 #endif
708
709                         printk("weird %s access at %#lx from pc %#lx (ssw is %#x)\n",
710                                !(ssw & RW) ? "write" : "read", addr,
711                                fp->ptregs.pc, ssw);
712                         asm volatile ("ptestr #1,%1@,#0\n\t"
713                                       "pmove %%psr,%0@"
714                                       : /* no outputs */
715                                       : "a" (&temp), "a" (addr));
716                         mmusr = temp;
717
718                         printk ("level 0 mmusr is %#x\n", mmusr);
719 #if 0
720                         asm volatile ("pmove %%tt0,%0@"
721                                       : /* no outputs */
722                                       : "a" (&tlong));
723                         printk("tt0 is %#lx, ", tlong);
724                         asm volatile ("pmove %%tt1,%0@"
725                                       : /* no outputs */
726                                       : "a" (&tlong));
727                         printk("tt1 is %#lx\n", tlong);
728 #endif
729 #ifdef DEBUG
730                         printk("Unknown SIGSEGV - 1\n");
731 #endif
732                         die_if_kernel("Oops",&fp->ptregs,mmusr);
733                         force_sig(SIGSEGV, current);
734                         return;
735                 }
736
737                 /* setup an ATC entry for the access about to be retried */
738                 if (!(ssw & RW) || (ssw & RM))
739                         asm volatile ("ploadw %1,%0@" : /* no outputs */
740                                       : "a" (addr), "d" (ssw));
741                 else
742                         asm volatile ("ploadr %1,%0@" : /* no outputs */
743                                       : "a" (addr), "d" (ssw));
744         }
745
746         /* Now handle the instruction fault. */
747
748         if (!(ssw & (FC|FB)))
749                 return;
750
751         if (fp->ptregs.sr & PS_S) {
752                 printk("Instruction fault at %#010lx\n",
753                         fp->ptregs.pc);
754         buserr:
755                 printk ("BAD KERNEL BUSERR\n");
756                 die_if_kernel("Oops",&fp->ptregs,0);
757                 force_sig(SIGKILL, current);
758                 return;
759         }
760
761         /* get the fault address */
762         if (fp->ptregs.format == 10)
763                 addr = fp->ptregs.pc + 4;
764         else
765                 addr = fp->un.fmtb.baddr;
766         if (ssw & FC)
767                 addr -= 2;
768
769         if ((ssw & DF) && ((addr ^ fp->un.fmtb.daddr) & PAGE_MASK) == 0)
770                 /* Insn fault on same page as data fault.  But we
771                    should still create the ATC entry.  */
772                 goto create_atc_entry;
773
774 #ifdef DEBUG
775         asm volatile ("ptestr #1,%2@,#7,%0\n\t"
776                       "pmove %%psr,%1@"
777                       : "=a&" (desc)
778                       : "a" (&temp), "a" (addr));
779 #else
780         asm volatile ("ptestr #1,%1@,#7\n\t"
781                       "pmove %%psr,%0@"
782                       : : "a" (&temp), "a" (addr));
783 #endif
784         mmusr = temp;
785
786 #ifdef DEBUG
787         printk ("mmusr is %#x for addr %#lx in task %p\n",
788                 mmusr, addr, current);
789         printk ("descriptor address is %#lx, contents %#lx\n",
790                 __va(desc), *(unsigned long *)__va(desc));
791 #endif
792
793         if (mmusr & MMU_I)
794                 do_page_fault (&fp->ptregs, addr, 0);
795         else if (mmusr & (MMU_B|MMU_L|MMU_S)) {
796                 printk ("invalid insn access at %#lx from pc %#lx\n",
797                         addr, fp->ptregs.pc);
798 #ifdef DEBUG
799                 printk("Unknown SIGSEGV - 2\n");
800 #endif
801                 die_if_kernel("Oops",&fp->ptregs,mmusr);
802                 force_sig(SIGSEGV, current);
803                 return;
804         }
805
806 create_atc_entry:
807         /* setup an ATC entry for the access about to be retried */
808         asm volatile ("ploadr #2,%0@" : /* no outputs */
809                       : "a" (addr));
810 }
811 #endif /* CPU_M68020_OR_M68030 */
812 #endif /* !CONFIG_SUN3 */
813
814 asmlinkage void buserr_c(struct frame *fp)
815 {
816         /* Only set esp0 if coming from user mode */
817         if (user_mode(&fp->ptregs))
818                 current->thread.esp0 = (unsigned long) fp;
819
820 #ifdef DEBUG
821         printk ("*** Bus Error *** Format is %x\n", fp->ptregs.format);
822 #endif
823
824         switch (fp->ptregs.format) {
825 #if defined (CONFIG_M68060)
826         case 4:                         /* 68060 access error */
827           access_error060 (fp);
828           break;
829 #endif
830 #if defined (CONFIG_M68040)
831         case 0x7:                       /* 68040 access error */
832           access_error040 (fp);
833           break;
834 #endif
835 #if defined (CPU_M68020_OR_M68030)
836         case 0xa:
837         case 0xb:
838           bus_error030 (fp);
839           break;
840 #endif
841         default:
842           die_if_kernel("bad frame format",&fp->ptregs,0);
843 #ifdef DEBUG
844           printk("Unknown SIGSEGV - 4\n");
845 #endif
846           force_sig(SIGSEGV, current);
847         }
848 }
849
850
851 static int kstack_depth_to_print = 48;
852
853 void show_trace(unsigned long *stack)
854 {
855         unsigned long *endstack;
856         unsigned long addr;
857         int i;
858
859         printk("Call Trace:");
860         addr = (unsigned long)stack + THREAD_SIZE - 1;
861         endstack = (unsigned long *)(addr & -THREAD_SIZE);
862         i = 0;
863         while (stack + 1 <= endstack) {
864                 addr = *stack++;
865                 /*
866                  * If the address is either in the text segment of the
867                  * kernel, or in the region which contains vmalloc'ed
868                  * memory, it *may* be the address of a calling
869                  * routine; if so, print it so that someone tracing
870                  * down the cause of the crash will be able to figure
871                  * out the call path that was taken.
872                  */
873                 if (__kernel_text_address(addr)) {
874 #ifndef CONFIG_KALLSYMS
875                         if (i % 5 == 0)
876                                 printk("\n       ");
877 #endif
878                         printk(" [<%08lx>]", addr);
879                         print_symbol(" %s\n", addr);
880                         i++;
881                 }
882         }
883         printk("\n");
884 }
885
886 void show_registers(struct pt_regs *regs)
887 {
888         struct frame *fp = (struct frame *)regs;
889         mm_segment_t old_fs = get_fs();
890         u16 c, *cp;
891         unsigned long addr;
892         int i;
893
894         print_modules();
895         printk("PC: [<%08lx>]",regs->pc);
896         print_symbol(" %s", regs->pc);
897         printk("\nSR: %04x  SP: %p  a2: %08lx\n",
898                regs->sr, regs, regs->a2);
899         printk("d0: %08lx    d1: %08lx    d2: %08lx    d3: %08lx\n",
900                regs->d0, regs->d1, regs->d2, regs->d3);
901         printk("d4: %08lx    d5: %08lx    a0: %08lx    a1: %08lx\n",
902                regs->d4, regs->d5, regs->a0, regs->a1);
903
904         printk("Process %s (pid: %d, task=%p)\n",
905                 current->comm, current->pid, current);
906         addr = (unsigned long)&fp->un;
907         printk("Frame format=%X ", regs->format);
908         switch (regs->format) {
909         case 0x2:
910                 printk("instr addr=%08lx\n", fp->un.fmt2.iaddr);
911                 addr += sizeof(fp->un.fmt2);
912                 break;
913         case 0x3:
914                 printk("eff addr=%08lx\n", fp->un.fmt3.effaddr);
915                 addr += sizeof(fp->un.fmt3);
916                 break;
917         case 0x4:
918                 printk((CPU_IS_060 ? "fault addr=%08lx fslw=%08lx\n"
919                         : "eff addr=%08lx pc=%08lx\n"),
920                         fp->un.fmt4.effaddr, fp->un.fmt4.pc);
921                 addr += sizeof(fp->un.fmt4);
922                 break;
923         case 0x7:
924                 printk("eff addr=%08lx ssw=%04x faddr=%08lx\n",
925                         fp->un.fmt7.effaddr, fp->un.fmt7.ssw, fp->un.fmt7.faddr);
926                 printk("wb 1 stat/addr/data: %04x %08lx %08lx\n",
927                         fp->un.fmt7.wb1s, fp->un.fmt7.wb1a, fp->un.fmt7.wb1dpd0);
928                 printk("wb 2 stat/addr/data: %04x %08lx %08lx\n",
929                         fp->un.fmt7.wb2s, fp->un.fmt7.wb2a, fp->un.fmt7.wb2d);
930                 printk("wb 3 stat/addr/data: %04x %08lx %08lx\n",
931                         fp->un.fmt7.wb3s, fp->un.fmt7.wb3a, fp->un.fmt7.wb3d);
932                 printk("push data: %08lx %08lx %08lx %08lx\n",
933                         fp->un.fmt7.wb1dpd0, fp->un.fmt7.pd1, fp->un.fmt7.pd2,
934                         fp->un.fmt7.pd3);
935                 addr += sizeof(fp->un.fmt7);
936                 break;
937         case 0x9:
938                 printk("instr addr=%08lx\n", fp->un.fmt9.iaddr);
939                 addr += sizeof(fp->un.fmt9);
940                 break;
941         case 0xa:
942                 printk("ssw=%04x isc=%04x isb=%04x daddr=%08lx dobuf=%08lx\n",
943                         fp->un.fmta.ssw, fp->un.fmta.isc, fp->un.fmta.isb,
944                         fp->un.fmta.daddr, fp->un.fmta.dobuf);
945                 addr += sizeof(fp->un.fmta);
946                 break;
947         case 0xb:
948                 printk("ssw=%04x isc=%04x isb=%04x daddr=%08lx dobuf=%08lx\n",
949                         fp->un.fmtb.ssw, fp->un.fmtb.isc, fp->un.fmtb.isb,
950                         fp->un.fmtb.daddr, fp->un.fmtb.dobuf);
951                 printk("baddr=%08lx dibuf=%08lx ver=%x\n",
952                         fp->un.fmtb.baddr, fp->un.fmtb.dibuf, fp->un.fmtb.ver);
953                 addr += sizeof(fp->un.fmtb);
954                 break;
955         default:
956                 printk("\n");
957         }
958         show_stack(NULL, (unsigned long *)addr);
959
960         printk("Code:");
961         set_fs(KERNEL_DS);
962         cp = (u16 *)regs->pc;
963         for (i = -8; i < 16; i++) {
964                 if (get_user(c, cp + i) && i >= 0) {
965                         printk(" Bad PC value.");
966                         break;
967                 }
968                 printk(i ? " %04x" : " <%04x>", c);
969         }
970         set_fs(old_fs);
971         printk ("\n");
972 }
973
974 void show_stack(struct task_struct *task, unsigned long *stack)
975 {
976         unsigned long *p;
977         unsigned long *endstack;
978         int i;
979
980         if (!stack) {
981                 if (task)
982                         stack = (unsigned long *)task->thread.esp0;
983                 else
984                         stack = (unsigned long *)&stack;
985         }
986         endstack = (unsigned long *)(((unsigned long)stack + THREAD_SIZE - 1) & -THREAD_SIZE);
987
988         printk("Stack from %08lx:", (unsigned long)stack);
989         p = stack;
990         for (i = 0; i < kstack_depth_to_print; i++) {
991                 if (p + 1 > endstack)
992                         break;
993                 if (i % 8 == 0)
994                         printk("\n       ");
995                 printk(" %08lx", *p++);
996         }
997         printk("\n");
998         show_trace(stack);
999 }
1000
1001 /*
1002  * The architecture-independent backtrace generator
1003  */
1004 void dump_stack(void)
1005 {
1006         unsigned long stack;
1007
1008         show_trace(&stack);
1009 }
1010
1011 EXPORT_SYMBOL(dump_stack);
1012
1013 void bad_super_trap (struct frame *fp)
1014 {
1015         console_verbose();
1016         if (fp->ptregs.vector < 4*sizeof(vec_names)/sizeof(vec_names[0]))
1017                 printk ("*** %s ***   FORMAT=%X\n",
1018                         vec_names[(fp->ptregs.vector) >> 2],
1019                         fp->ptregs.format);
1020         else
1021                 printk ("*** Exception %d ***   FORMAT=%X\n",
1022                         (fp->ptregs.vector) >> 2,
1023                         fp->ptregs.format);
1024         if (fp->ptregs.vector >> 2 == VEC_ADDRERR && CPU_IS_020_OR_030) {
1025                 unsigned short ssw = fp->un.fmtb.ssw;
1026
1027                 printk ("SSW=%#06x  ", ssw);
1028
1029                 if (ssw & RC)
1030                         printk ("Pipe stage C instruction fault at %#010lx\n",
1031                                 (fp->ptregs.format) == 0xA ?
1032                                 fp->ptregs.pc + 2 : fp->un.fmtb.baddr - 2);
1033                 if (ssw & RB)
1034                         printk ("Pipe stage B instruction fault at %#010lx\n",
1035                                 (fp->ptregs.format) == 0xA ?
1036                                 fp->ptregs.pc + 4 : fp->un.fmtb.baddr);
1037                 if (ssw & DF)
1038                         printk ("Data %s fault at %#010lx in %s (pc=%#lx)\n",
1039                                 ssw & RW ? "read" : "write",
1040                                 fp->un.fmtb.daddr, space_names[ssw & DFC],
1041                                 fp->ptregs.pc);
1042         }
1043         printk ("Current process id is %d\n", current->pid);
1044         die_if_kernel("BAD KERNEL TRAP", &fp->ptregs, 0);
1045 }
1046
1047 asmlinkage void trap_c(struct frame *fp)
1048 {
1049         int sig;
1050         siginfo_t info;
1051
1052         if (fp->ptregs.sr & PS_S) {
1053                 if ((fp->ptregs.vector >> 2) == VEC_TRACE) {
1054                         /* traced a trapping instruction */
1055                         current->ptrace |= PT_DTRACE;
1056                 } else
1057                         bad_super_trap(fp);
1058                 return;
1059         }
1060
1061         /* send the appropriate signal to the user program */
1062         switch ((fp->ptregs.vector) >> 2) {
1063             case VEC_ADDRERR:
1064                 info.si_code = BUS_ADRALN;
1065                 sig = SIGBUS;
1066                 break;
1067             case VEC_ILLEGAL:
1068             case VEC_LINE10:
1069             case VEC_LINE11:
1070                 info.si_code = ILL_ILLOPC;
1071                 sig = SIGILL;
1072                 break;
1073             case VEC_PRIV:
1074                 info.si_code = ILL_PRVOPC;
1075                 sig = SIGILL;
1076                 break;
1077             case VEC_COPROC:
1078                 info.si_code = ILL_COPROC;
1079                 sig = SIGILL;
1080                 break;
1081             case VEC_TRAP1:
1082             case VEC_TRAP2:
1083             case VEC_TRAP3:
1084             case VEC_TRAP4:
1085             case VEC_TRAP5:
1086             case VEC_TRAP6:
1087             case VEC_TRAP7:
1088             case VEC_TRAP8:
1089             case VEC_TRAP9:
1090             case VEC_TRAP10:
1091             case VEC_TRAP11:
1092             case VEC_TRAP12:
1093             case VEC_TRAP13:
1094             case VEC_TRAP14:
1095                 info.si_code = ILL_ILLTRP;
1096                 sig = SIGILL;
1097                 break;
1098             case VEC_FPBRUC:
1099             case VEC_FPOE:
1100             case VEC_FPNAN:
1101                 info.si_code = FPE_FLTINV;
1102                 sig = SIGFPE;
1103                 break;
1104             case VEC_FPIR:
1105                 info.si_code = FPE_FLTRES;
1106                 sig = SIGFPE;
1107                 break;
1108             case VEC_FPDIVZ:
1109                 info.si_code = FPE_FLTDIV;
1110                 sig = SIGFPE;
1111                 break;
1112             case VEC_FPUNDER:
1113                 info.si_code = FPE_FLTUND;
1114                 sig = SIGFPE;
1115                 break;
1116             case VEC_FPOVER:
1117                 info.si_code = FPE_FLTOVF;
1118                 sig = SIGFPE;
1119                 break;
1120             case VEC_ZERODIV:
1121                 info.si_code = FPE_INTDIV;
1122                 sig = SIGFPE;
1123                 break;
1124             case VEC_CHK:
1125             case VEC_TRAP:
1126                 info.si_code = FPE_INTOVF;
1127                 sig = SIGFPE;
1128                 break;
1129             case VEC_TRACE:             /* ptrace single step */
1130                 info.si_code = TRAP_TRACE;
1131                 sig = SIGTRAP;
1132                 break;
1133             case VEC_TRAP15:            /* breakpoint */
1134                 info.si_code = TRAP_BRKPT;
1135                 sig = SIGTRAP;
1136                 break;
1137             default:
1138                 info.si_code = ILL_ILLOPC;
1139                 sig = SIGILL;
1140                 break;
1141         }
1142         info.si_signo = sig;
1143         info.si_errno = 0;
1144         switch (fp->ptregs.format) {
1145             default:
1146                 info.si_addr = (void *) fp->ptregs.pc;
1147                 break;
1148             case 2:
1149                 info.si_addr = (void *) fp->un.fmt2.iaddr;
1150                 break;
1151             case 7:
1152                 info.si_addr = (void *) fp->un.fmt7.effaddr;
1153                 break;
1154             case 9:
1155                 info.si_addr = (void *) fp->un.fmt9.iaddr;
1156                 break;
1157             case 10:
1158                 info.si_addr = (void *) fp->un.fmta.daddr;
1159                 break;
1160             case 11:
1161                 info.si_addr = (void *) fp->un.fmtb.daddr;
1162                 break;
1163         }
1164         force_sig_info (sig, &info, current);
1165 }
1166
1167 void die_if_kernel (char *str, struct pt_regs *fp, int nr)
1168 {
1169         if (!(fp->sr & PS_S))
1170                 return;
1171
1172         console_verbose();
1173         printk("%s: %08x\n",str,nr);
1174         show_registers(fp);
1175         do_exit(SIGSEGV);
1176 }
1177
1178 /*
1179  * This function is called if an error occur while accessing
1180  * user-space from the fpsp040 code.
1181  */
1182 asmlinkage void fpsp040_die(void)
1183 {
1184         do_exit(SIGSEGV);
1185 }
1186
1187 #ifdef CONFIG_M68KFPU_EMU
1188 asmlinkage void fpemu_signal(int signal, int code, void *addr)
1189 {
1190         siginfo_t info;
1191
1192         info.si_signo = signal;
1193         info.si_errno = 0;
1194         info.si_code = code;
1195         info.si_addr = addr;
1196         force_sig_info(signal, &info, current);
1197 }
1198 #endif