home *** CD-ROM | disk | FTP | other *** search
/ STraTOS 1997 April & May / STraTOS 1 - 1997 April & May.iso / CD01 / LINUX / MATH_EMU.ZIP / MATH_EMU / TRAPS.C < prev    next >
Encoding:
C/C++ Source or Header  |  1979-12-31  |  25.8 KB  |  946 lines

  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.  *  68060 fixes by Roman Hodek
  9.  *  68060 fixes by Jesper Skov
  10.  *  68060 fixes by Jes SĀ°rensen
  11.  *
  12.  * This file is subject to the terms and conditions of the GNU General Public
  13.  * License.  See the file README.legal 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/types.h>
  27. #include <linux/a.out.h>
  28. #include <linux/user.h>
  29. #include <linux/string.h>
  30.  
  31. #include <asm/system.h>
  32. #include <asm/segment.h>
  33. #include <asm/traps.h>
  34. #include <asm/bootinfo.h>
  35. #include <asm/pgtable.h>
  36.  
  37. /* assembler routines */
  38. asmlinkage void system_call(void);
  39. asmlinkage void buserr(void);
  40. asmlinkage void trap(void);
  41. asmlinkage void inthandler(void);
  42. asmlinkage void nmihandler(void);
  43. #define MATH_EMULATION
  44.  
  45. #ifdef MATH_EMULATION
  46. asmlinkage void fpu_emu_trap(void);
  47. #endif
  48.  
  49. e_vector vectors[256] = {
  50.     0, 0, buserr, trap, trap, trap, trap, trap,
  51.     trap, trap, trap, trap, trap, trap, trap, trap,
  52.     trap, trap, trap, trap, trap, trap, trap, trap,
  53.     inthandler, inthandler, inthandler, inthandler,
  54.     inthandler, inthandler, inthandler, inthandler,
  55.     /* TRAP #0-15 */
  56.     system_call, trap, trap, trap, trap, trap, trap, trap,
  57.     trap, trap, trap, trap, trap, trap, trap, trap,
  58.     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  59. };
  60.  
  61. /* nmi handler for the Amiga */
  62. asm(".text\n"
  63.     ALIGN_STR "\n"
  64.     SYMBOL_NAME_STR(nmihandler) ": rte");
  65.  
  66. void trap_init (void)
  67. {
  68.     int i;
  69.  
  70.     /* setup the exception vector table */
  71.     __asm__ volatile ("movec %0,%/vbr" : : "r" ((void*)vectors));
  72.  
  73.     for (i = 48; i < 64; i++)
  74.         vectors[i] = trap;
  75.  
  76.     for (i = 64; i < 256; i++)
  77.         vectors[i] = inthandler;
  78.  
  79.         /* if running on an amiga, make the NMI interrupt do nothing */
  80.         if (MACH_IS_AMIGA) {
  81.                 vectors[VEC_INT7] = nmihandler;
  82.         }
  83.         
  84. #ifdef MATH_EMULATION
  85.     vectors[VEC_LINE11]=fpu_emu_trap;
  86. #endif
  87.                     
  88. #ifdef CONFIG_FPSP_040
  89.     if (m68k_is040or060 == 4) {
  90.         /* set up FPSP entry points */
  91.         asmlinkage void dz_vec(void) asm ("dz");
  92.         asmlinkage void inex_vec(void) asm ("inex");
  93.         asmlinkage void ovfl_vec(void) asm ("ovfl");
  94.         asmlinkage void unfl_vec(void) asm ("unfl");
  95.         asmlinkage void snan_vec(void) asm ("snan");
  96.         asmlinkage void operr_vec(void) asm ("operr");
  97.         asmlinkage void bsun_vec(void) asm ("bsun");
  98.         asmlinkage void fline_vec(void) asm ("fline");
  99.         asmlinkage void unsupp_vec(void) asm ("unsupp");
  100.  
  101.         vectors[VEC_FPDIVZ] = dz_vec;
  102.         vectors[VEC_FPIR] = inex_vec;
  103.         vectors[VEC_FPOVER] = ovfl_vec;
  104.         vectors[VEC_FPUNDER] = unfl_vec;
  105.         vectors[VEC_FPNAN] = snan_vec;
  106.         vectors[VEC_FPOE] = operr_vec;
  107.         vectors[VEC_FPBRUC] = bsun_vec;
  108.         vectors[VEC_FPBRUC] = bsun_vec;
  109.         vectors[VEC_LINE11] = fline_vec;
  110.         vectors[VEC_FPUNSUP] = unsupp_vec;
  111.     }
  112. #endif
  113. #ifdef CONFIG_IFPSP_060
  114.     if (m68k_is040or060 == 6) {
  115.        asmlinkage void snan_vec(void) asm ("_060_fpsp_snan");
  116.        asmlinkage void operr_vec(void) asm ("_060_fpsp_operr");
  117.        asmlinkage void ovfl_vec(void) asm ("_060_fpsp_ovfl");
  118.        asmlinkage void unfl_vec(void) asm ("_060_fpsp_unfl");
  119.        asmlinkage void dz_vec(void) asm ("_060_fpsp_dz");
  120.        asmlinkage void inex_vec(void) asm ("_060_fpsp_inex");
  121.        asmlinkage void fline_vec(void) asm ("_060_fpsp_fline");
  122.        asmlinkage void unsupp_vec(void) asm ("_060_fpsp_unsupp");
  123.        asmlinkage void effadd_vec(void) asm ("_060_fpsp_effadd");
  124.        asmlinkage void unimp_vec(void) asm ("_060_isp_unimp");
  125.   
  126.       /* set up FPSP entry points */
  127.        vectors[VEC_FPNAN] = snan_vec;
  128.        vectors[VEC_FPOE] = operr_vec;
  129.        vectors[VEC_FPOVER] = ovfl_vec;
  130.        vectors[VEC_FPUNDER] = unfl_vec;
  131.        vectors[VEC_FPDIVZ] = dz_vec;
  132.        vectors[VEC_FPIR] = inex_vec;
  133.        vectors[VEC_LINE11] = fline_vec;
  134.        vectors[VEC_FPUNSUP] = unsupp_vec;
  135.        vectors[VEC_UNIMPEA] = effadd_vec;
  136.   
  137.        /* set up ISP entry points */
  138.        vectors[VEC_UNIMPII] = unimp_vec;
  139.       }
  140. #endif
  141. }
  142.  
  143. void set_evector(int vecnum, void (*handler)(void))
  144. {
  145.     if (vecnum >= 0 && vecnum <= 256)
  146.         vectors[vecnum] = handler;
  147. }
  148.  
  149.  
  150. static inline void console_verbose(void)
  151. {
  152.     extern int console_loglevel;
  153.     console_loglevel = 15;
  154.     mach_debug_init();
  155. }
  156.  
  157. char *vec_names[] = {
  158.     "RESET SP", "RESET PC", "BUS ERROR", "ADDRESS ERROR",
  159.     "ILLEGAL INSTRUCTION", "ZERO DIVIDE", "CHK", "TRAPcc",
  160.     "PRIVILEGE VIOLATION", "TRACE", "LINE 1010", "LINE 1111",
  161.     "UNASSIGNED RESERVED 12", "COPROCESSOR PROTOCOL VIOLATION",
  162.     "FORMAT ERROR", "UNINITIALIZED INTERRUPT",
  163.     "UNASSIGNED RESERVED 16", "UNASSIGNED RESERVED 17",
  164.     "UNASSIGNED RESERVED 18", "UNASSIGNED RESERVED 19",
  165.     "UNASSIGNED RESERVED 20", "UNASSIGNED RESERVED 21",
  166.     "UNASSIGNED RESERVED 22", "UNASSIGNED RESERVED 23",
  167.     "SPURIOUS INTERRUPT", "LEVEL 1 INT", "LEVEL 2 INT", "LEVEL 3 INT",
  168.     "LEVEL 4 INT", "LEVEL 5 INT", "LEVEL 6 INT", "LEVEL 7 INT",
  169.     "SYSCALL", "TRAP #1", "TRAP #2", "TRAP #3",
  170.     "TRAP #4", "TRAP #5", "TRAP #6", "TRAP #7",
  171.     "TRAP #8", "TRAP #9", "TRAP #10", "TRAP #11",
  172.     "TRAP #12", "TRAP #13", "TRAP #14", "TRAP #15"
  173.     };
  174.  
  175. char *space_names[] = {
  176.     "Space 0", "User Data", "User Program", "Space 3",
  177.     "Space 4", "Super Data", "Super Program", "CPU"
  178.     };
  179.  
  180.  
  181.  
  182. extern void die_if_kernel(char *,struct frame *,int);
  183. asmlinkage int do_page_fault(struct pt_regs *regs, unsigned long address,
  184.                   unsigned long error_code);
  185.  
  186. asmlinkage void trap_c(struct frame *fp);
  187.  
  188. static inline void access_error060 (struct frame *fp)
  189. {
  190.     unsigned long fslw = fp->un.fmt4.pc; /* is really FSLW for access error */
  191.  
  192. #ifdef DEBUG
  193.     printk("fslw=%#lx, fa=%#lx\n", ssw, fp->un.fmt4.effaddr);
  194. #endif
  195.  
  196.     if (fslw & MMU060_BPE) {
  197.         /* branch prediction error -> clear branch cache */
  198.         __asm__ __volatile__ ( "movec %/cacr,%/d0\n\t"
  199.                                "orl   #0x00400000,%/d0\n\t"
  200.                                "movec %/d0,%/cacr"
  201.                                : : : "d0" );
  202.         /* return if there's no other error */
  203.         if (!(fslw & MMU060_ERR_BITS))
  204.             return;
  205.     }
  206.     
  207.     if (fslw & (MMU060_DESC_ERR | MMU060_WP)) {
  208.         unsigned long errorcode;
  209.         unsigned long addr = fp->un.fmt4.effaddr;
  210.  
  211.         if (fslw & MMU060_MA)
  212.             addr = PAGE_ALIGN (addr);
  213.         errorcode = ((fslw & MMU060_WP) ? 1 : 0) |
  214.                     ((fslw & MMU060_W)  ? 2 : 0);
  215. #ifdef DEBUG
  216.         printk("addr = %08lx, errorcode = %d\n", addr, errorcode );
  217. #endif
  218.         do_page_fault( (struct pt_regs *)fp, addr, errorcode );
  219.     }
  220.     else {
  221.         printk( "68060 access error, fslw=%08lx, addr=%08lx, pc=%08lx\n",
  222.                 fslw, fp->un.fmt4.effaddr, fp->pc );
  223.         trap_c( fp );
  224.     }
  225. }
  226.  
  227.  
  228. static unsigned long probe040 (int iswrite, int fc, unsigned long addr)
  229. {
  230.     unsigned long mmusr;
  231.     unsigned long fs = get_fs();
  232.  
  233.     set_fs (fc);
  234.  
  235.       if (iswrite)
  236.           /* write */
  237.         asm volatile ("movel %1,%/a0\n\t"              
  238.                   ".word 0xf548\n\t"    /* ptestw (a0) */
  239.                   ".long 0x4e7a8805\n\t"    /* movec mmusr,a0 */
  240.                   "movel %/a0,%0"
  241.                   : "=g" (mmusr)
  242.                   : "g" (addr)
  243.                   : "a0");
  244.     else
  245.         asm volatile ("movel %1,%/a0\n\t"
  246.                   ".word 0xf568\n\t"    /* ptestr (a0) */
  247.                   ".long 0x4e7a8805\n\t"    /* movec mmusr,a0 */
  248.                   "movel %/a0,%0"
  249.                   : "=g" (mmusr)
  250.                   : "g" (addr)
  251.                   : "a0");
  252.  
  253.  
  254.     set_fs (fs);
  255.  
  256.     return mmusr;
  257. }
  258.  
  259. static void do_040writeback (unsigned short ssw,
  260.                  unsigned short wbs,
  261.                  unsigned long wba,
  262.                  unsigned long wbd,
  263.                  struct frame *fp)
  264. {
  265.     unsigned long fs = get_fs ();
  266.     unsigned long mmusr;
  267.     unsigned long errorcode;
  268.  
  269.     /* No special handling for the second writeback anymore.
  270.      * It misinterpreted the misaligned status sometimes.
  271.      * This way an extra pgae-fault may be caused (Martin Apel).
  272.      */
  273.  
  274.     mmusr = probe040 (1, wbs & WBTM_040,  wba);
  275.     errorcode = (mmusr & MMU_R_040) ? 3 : 2;
  276.     if (do_page_fault ((struct pt_regs *)fp, wba, errorcode))
  277.         /* just return if we can't perform the writeback */
  278.         return;
  279.  
  280.     set_fs (wbs & WBTM_040);
  281.     switch (wbs & WBSIZ_040) {
  282.         case BA_SIZE_BYTE:
  283.         put_user_byte (wbd & 0xff, (char *)wba);
  284.         break;
  285.         case BA_SIZE_WORD:
  286.         put_user_word (wbd & 0xffff, (short *)wba);
  287.         break;
  288.         case BA_SIZE_LONG:
  289.         put_user_long (wbd, (int *)wba);
  290.         break;
  291.     }
  292.     set_fs (fs);
  293. }
  294.  
  295. static inline void access_error040 (struct frame *fp)
  296. {
  297.     unsigned short ssw = fp->un.fmt7.ssw;
  298.     unsigned long mmusr;
  299.  
  300. #ifdef DEBUG
  301.     printk("ssw=%#x, fa=%#lx\n", ssw, fp->un.fmt7.faddr);
  302. /*
  303.         printk("wb1s=%#x, wb2s=%#x, wb3s=%#x\n", fp->un.fmt7.wb1s,  
  304.         fp->un.fmt7.wb2s, fp->un.fmt7.wb3s);
  305.     printk ("wb2a=%lx, wb3a=%lx, wb2d=%lx, wb3d=%lx\n", 
  306.         fp->un.fmt7.wb2a, fp->un.fmt7.wb3a,
  307.         fp->un.fmt7.wb2d, fp->un.fmt7.wb3d);
  308. */
  309. #endif
  310.  
  311.     if (ssw & ATC_040) {
  312.         unsigned long addr = fp->un.fmt7.faddr;
  313.         unsigned long errorcode;
  314.  
  315.         /* The MMU status has to be determined AFTER the address
  316.          * has been corrected if there was a misaligned access (MA).
  317.          */
  318.         if (ssw & MA_040)
  319.             addr = PAGE_ALIGN (addr);
  320.  
  321.         /* MMU error, get the MMUSR info for this access */
  322.         mmusr = probe040 (!(ssw & RW_040), ssw & TM_040, addr);
  323. #ifdef DEBUG
  324.         printk("mmusr = %lx\n", mmusr);
  325. #endif
  326.  
  327.         errorcode = ((mmusr & MMU_R_040) ? 1 : 0) |
  328.             ((ssw & RW_040) ? 0 : 2);
  329.         do_page_fault ((struct pt_regs *)fp, addr, errorcode);
  330.     } else {
  331.         printk ("68040 access error, ssw=%x\n", ssw);
  332.         trap_c (fp);
  333.     }
  334.  
  335. #if 0
  336.     if (fp->un.fmt7.wb1s & WBV_040)
  337.         printk("access_error040: cannot handle 1st writeback. oops.\n");
  338. #endif
  339.  
  340.     /*
  341.      *  We may have to do a couple of writebacks here.
  342.      *
  343.      *  MR: we can speed up the thing a little bit and let do_040writeback()
  344.      *  not produce another page fault as wb2 corresponds to the address that
  345.      *  caused the fault. on write faults no second fault is generated, but
  346.      *  on read faults for security reasons (although per definitionem impossible)
  347.      */
  348.  
  349.     if (fp->un.fmt7.wb2s & WBV_040 && (fp->un.fmt7.wb2s & WBTT_040) != BA_TT_MOVE16)
  350.         do_040writeback (ssw,
  351.                  fp->un.fmt7.wb2s, fp->un.fmt7.wb2a, fp->un.fmt7.wb2d, fp);
  352.  
  353.     if (fp->un.fmt7.wb3s & WBV_040)
  354.         do_040writeback (ssw,
  355.                  fp->un.fmt7.wb3s, fp->un.fmt7.wb3a, fp->un.fmt7.wb3d, fp);
  356. }
  357.  
  358. static inline void bus_error030 (struct frame *fp)
  359. {
  360.     volatile unsigned short temp;
  361.     unsigned short mmusr;
  362.     unsigned long addr, desc, errorcode;
  363.     unsigned short ssw = fp->un.fmtb.ssw;
  364.     int user_space_fault = 1;
  365.  
  366. #ifdef DEBUG
  367.     printk ("SSW=%#06x  ", ssw);
  368.  
  369.     if (ssw & (FC | FB))
  370.         printk ("Instruction fault at %#010lx\n",
  371.             ssw & FC ?
  372.             fp->format == 0xa ? fp->pc + 2 : fp->un.fmtb.baddr - 2
  373.             :
  374.             fp->format == 0xa ? fp->pc + 4 : fp->un.fmtb.baddr);
  375.     if (ssw & DF)
  376.         printk ("Data %s fault at %#010lx in %s (pc=%#lx)\n",
  377.             ssw & RW ? "read" : "write",
  378.             fp->un.fmtb.daddr,
  379.             space_names[ssw & DFC], fp->pc);
  380. #endif
  381.  
  382.     if (fp->sr & PS_S) {
  383.         /* kernel fault must be a data fault to user space */
  384.         if (! ((ssw & DF) && ((ssw & DFC) == USER_DATA))) {
  385. #if 0
  386.             /* instruction fault or kernel data fault! */
  387.             if (ssw & (FC | FB))
  388.                 printk ("Instruction fault at %#010lx\n",
  389.                     fp->pc);
  390.             if (ssw & DF) {
  391.                 printk ("Data %s fault at %#010lx in %s (pc=%#lx)\n",
  392.                     ssw & RW ? "read" : "write",
  393.                     fp->un.fmtb.daddr,
  394.                     space_names[ssw & DFC], fp->pc);
  395.             }
  396.             printk ("BAD KERNEL BUSERR\n");
  397.             trap_c (fp);
  398. #else
  399.             /* instruction fault or kernel data fault! */
  400.             if (ssw & (FC | FB))
  401.                 printk ("Instruction fault at %#010lx\n",
  402.                     fp->pc);
  403.             if (ssw & DF) {
  404.                 printk ("Data %s fault at %#010lx in %s (pc=%#lx)\n",
  405.                     ssw & RW ? "read" : "write",
  406.                     fp->un.fmtb.daddr,
  407.                     space_names[ssw & DFC], fp->pc);
  408.             }
  409.             printk ("BAD KERNEL BUSERR\n");
  410.             die_if_kernel("Oops",fp,0);
  411.             send_sig(SIGSEGV, current, 1);
  412.             user_space_fault = 0;
  413. #endif
  414.         }
  415.     } else {
  416.         /* user fault */
  417.         if (!(ssw & (FC | FB)) && !(ssw & DF))
  418.             /* not an instruction fault or data fault! BAD */
  419.             panic ("USER BUSERR w/o instruction or data fault");
  420.         user_space_fault = 1;
  421.     }
  422.  
  423.     /* ++andreas: If a data fault and an instruction fault happen
  424.        at the same time map in both pages.  */
  425.  
  426.     /* First handle the data fault, if any.  */
  427.     if (ssw & DF)
  428.       {
  429.         addr = fp->un.fmtb.daddr;
  430.  
  431.         if (user_space_fault) {
  432.             asm volatile ("ptestr #1,%2@,#7,%0\n\t"
  433.                   "pmove %/psr,%1@"
  434.                   : "=a&" (desc)
  435.                   : "a" (&temp), "a" (addr));
  436.             mmusr = temp;
  437.         } else
  438.             mmusr = MMU_I;
  439.       
  440. #ifdef DEBUG
  441.         printk ("mmusr is %#x for addr %#lx in task %p\n",
  442.             mmusr, addr, current);
  443.         printk ("descriptor address is %#lx, contents %#lx\n",
  444.             mm_ptov(desc), *(unsigned long *)mm_ptov(desc));
  445. #endif
  446.  
  447.         errorcode = (mmusr & MMU_I) ? 0 : 1;
  448.         if (!(ssw & RW) || ssw & RM)
  449.             errorcode |= 2;
  450.  
  451.         if (mmusr & MMU_I)
  452.             do_page_fault ((struct pt_regs *)fp, addr, errorcode);
  453.         else if ((mmusr & MMU_WP) && (!(ssw & RW) || ssw & RM))
  454.             do_page_fault ((struct pt_regs *)fp, addr, errorcode);
  455.         else if (mmusr & (MMU_B|MMU_L|MMU_S)) {
  456.             printk ("invalid %s access at %#lx from pc %#lx\n",
  457.                 !(ssw & RW) ? "write" : "read", addr, fp->pc);
  458.             die_if_kernel("Oops",fp,mmusr);
  459.             send_sig(SIGSEGV, current, 1);
  460.             return;
  461.         } else {
  462. #ifdef DEBUG
  463.             static volatile long tlong;
  464. #endif
  465.  
  466.             printk ("weird %s access at %#lx from pc %#lx (ssw is %#x)\n",
  467.                 !(ssw & RW) ? "write" : "read", addr, fp->pc, ssw);
  468.             asm volatile ("ptestr #1,%1@,#0\n\t"
  469.                   "pmove %/psr,%0@"
  470.                   : /* no outputs */
  471.                   : "a" (&temp), "a" (addr));
  472.             mmusr = temp;
  473.  
  474.             printk ("level 0 mmusr is %#x\n", mmusr);
  475. #ifdef DEBUG
  476.             asm volatile ("pmove %/tt0,%0@"
  477.                   : /* no outputs */
  478.                   : "a" (&tlong));
  479.             printk ("tt0 is %#lx, ", tlong);
  480.             asm volatile ("pmove %/tt1,%0@"
  481.                   : /* no outputs */
  482.                   : "a" (&tlong));
  483.             printk ("tt1 is %#lx\n", tlong);
  484. #endif
  485.             die_if_kernel("Oops",fp,mmusr);
  486.             send_sig(SIGSEGV, current, 1);
  487.             return;
  488.         }
  489.  
  490.         /* setup an ATC entry for the access about to be retried */
  491.         if (!(ssw & RW))
  492.             asm volatile ("ploadw #1,%0@" : /* no outputs */
  493.                   : "a" (addr));
  494.         else
  495.             asm volatile ("ploadr #1,%0@" : /* no outputs */
  496.                   : "a" (addr));
  497.  
  498.         /* If this was a data fault due to an invalid page and a
  499.            prefetch is pending on the same page, simulate it (but
  500.            only if the page is now valid).  Otherwise we'll get an
  501.            weird insn access.  */
  502.         if ((ssw & RB) && (mmusr & MMU_I))
  503.           {
  504.         unsigned long iaddr;
  505.  
  506.         if (fp->format == 0xB)
  507.           iaddr = fp->un.fmtb.baddr;
  508.         else
  509.           iaddr = fp->pc + 4;
  510.         if (((addr ^ iaddr) & PAGE_MASK) == 0)
  511.           {
  512.             /* We only need to check the ATC as the entry has
  513.                already been set up above.  */
  514.             asm volatile ("ptestr #1,%1@,#0\n\t"
  515.                   "pmove %/psr,%0@"
  516.                   : : "a" (&temp), "a" (iaddr));
  517.             mmusr = temp;
  518. #ifdef DEBUG
  519.             printk ("prefetch iaddr=%#lx ssw=%#x mmusr=%#x\n",
  520.                 iaddr, ssw, mmusr);
  521. #endif
  522.             if (!(mmusr & MMU_I))
  523.               {
  524.             unsigned short insn;
  525.             asm volatile ("movesw %1@,%0"
  526.                       : "=r" (insn)
  527.                       : "a" (iaddr));
  528.             fp->un.fmtb.isb = insn;
  529.             fp->un.fmtb.ssw &= ~RB;
  530.               }
  531.           }
  532.           }
  533.       }
  534.         
  535.     /* Now handle the instruction fault. */
  536.  
  537.     /* get the fault address */
  538.     if (fp->format == 0xA )
  539.         if (ssw & FC)
  540.             addr = fp->pc + 2;
  541.         else if (ssw & FB)
  542.             addr = fp->pc + 4;
  543.         else
  544.             return;
  545.     else
  546.         if (ssw & FC)
  547.             addr = fp->un.fmtb.baddr - 2;
  548.         else if (ssw & FB)
  549.             addr = fp->un.fmtb.baddr;
  550.         else
  551.             return;
  552.  
  553.     if ((ssw & DF) && ((addr ^ fp->un.fmtb.daddr) & PAGE_MASK) == 0)
  554.         /* Insn fault on same page as data fault */
  555.         return;
  556.  
  557.     if (user_space_fault) {
  558.         asm volatile ("ptestr #1,%2@,#7,%0\n\t"
  559.                   "pmove %/psr,%1@"
  560.                   : "=a&" (desc)
  561.                   : "a" (&temp), "a" (addr));
  562.         mmusr = temp;
  563.     } else
  564.         mmusr = MMU_I;
  565.       
  566. #ifdef DEBUG
  567.     printk ("mmusr is %#x for addr %#lx in task %p\n",
  568.         mmusr, addr, current);
  569.     printk ("descriptor address is %#lx, contents %#lx\n",
  570.         mm_ptov(desc), *(unsigned long *)mm_ptov(desc));
  571. #endif
  572.  
  573.     errorcode = (mmusr & MMU_I) ? 0 : 1;
  574.  
  575.     if (mmusr & MMU_I)
  576.         do_page_fault ((struct pt_regs *)fp, addr, errorcode);
  577.     else if (mmusr & (MMU_B|MMU_L|MMU_S)) {
  578.         printk ("invalid insn access at %#lx from pc %#lx\n",
  579.             addr, fp->pc);
  580.         die_if_kernel("Oops",fp,mmusr);
  581.         send_sig(SIGSEGV, current, 1);
  582.         return;
  583.     } else {
  584. #ifdef DEBUG
  585.         static volatile long tlong;
  586. #endif
  587.  
  588.         printk ("weird insn access at %#lx from pc %#lx (ssw is %#x)\n",
  589.             addr, fp->pc, ssw);
  590.         asm volatile ("ptestr #1,%1@,#0\n\t"
  591.                   "pmove %/psr,%0@"
  592.                   : /* no outputs */
  593.                   : "a" (&temp), "a" (addr));
  594.         mmusr = temp;
  595.               
  596.         printk ("level 0 mmusr is %#x\n", mmusr);
  597. #ifdef DEBUG
  598.         if (boot_info.cputype & CPU_68030) {
  599.             asm volatile ("pmove %/tt0,%0@"
  600.                       : /* no outputs */
  601.                       : "a" (&tlong));
  602.             printk ("tt0 is %#lx, ", tlong);
  603.             asm volatile ("pmove %/tt1,%0@"
  604.                       : /* no outputs */
  605.                       : "a" (&tlong));
  606.             printk ("tt1 is %#lx\n", tlong);
  607.         }
  608.  
  609. #endif
  610.         die_if_kernel("Oops",fp,mmusr);
  611.         send_sig(SIGSEGV, current, 1);
  612.         return;
  613.     }
  614.  
  615.     /* setup an ATC entry for the access about to be retried */
  616.     asm volatile ("ploadr #1,%0@" : /* no outputs */
  617.               : "a" (addr));
  618. }
  619.  
  620. asmlinkage void buserr_c(struct frame *fp)
  621. {
  622.     current->tss.esp0 = (unsigned long) fp;
  623.  
  624. #ifdef DEBUG
  625.     printk ("*** Bus Error *** Format is %x\n", fp->format);
  626. #endif
  627.  
  628.     switch (fp->format) {
  629.         case 4:                /* 68060 access error */
  630.         access_error060 (fp);
  631.         break;
  632.         case 7:                /* 68040 access error */
  633.         access_error040 (fp);
  634.         break;
  635.         case 0xa:
  636.         case 0xb:
  637.         bus_error030 (fp);
  638.         break;
  639.         default:
  640.         panic ("bad frame format %d", fp->format);
  641.     }
  642. }
  643.  
  644.  
  645. int kstack_depth_to_print = 48;
  646.  
  647. /* MODULE_RANGE is a guess of how much space is likely to be
  648.    vmalloced.  */
  649. #define MODULE_RANGE (8*1024*1024)
  650.  
  651. static void dump_stack( struct frame *fp )
  652.  
  653. {    unsigned long *stack, *endstack, addr, module_start, module_end;
  654.     extern char _start, _etext;
  655. #ifdef MASTER_STACK
  656.     extern unsigned long interrupt_stack[PAGE_SIZE/sizeof(long)];
  657. #endif
  658.     int i;
  659.  
  660.     addr = (unsigned long)&fp->un;
  661.     printk( "Frame format=%X ", fp->format );
  662.     switch( fp->format ) {
  663.       case 0x2:
  664.         printk( "instr addr=%08lx\n", fp->un.fmt2.iaddr );
  665.         addr += sizeof(fp->un.fmt2);
  666.         break;
  667.       case 0x3:
  668.         printk( "eff addr=%08lx\n", fp->un.fmt3.effaddr );
  669.         addr += sizeof(fp->un.fmt3);
  670.         break;
  671.       case 0x4:
  672.         printk( (m68k_is040or060 == 6) ? "fault addr=%08lx fslw=%08lx\n"
  673.                        : "eff addr=%08lx pc=%08lx\n",
  674.             fp->un.fmt4.effaddr, fp->un.fmt4.pc );
  675.         addr += sizeof(fp->un.fmt4);
  676.         break;
  677.       case 0x7:
  678.         printk( "eff addr=%08lx ssw=%04x faddr=%08lx\n",
  679.             fp->un.fmt7.effaddr, fp->un.fmt7.ssw, fp->un.fmt7.faddr );
  680.         printk( "wb 1 stat/addr/data: %04x %08lx %08lx\n",
  681.             fp->un.fmt7.wb1s, fp->un.fmt7.wb1a, fp->un.fmt7.wb1dpd0 );
  682.         printk( "wb 2 stat/addr/data: %04x %08lx %08lx\n",
  683.             fp->un.fmt7.wb2s, fp->un.fmt7.wb2a, fp->un.fmt7.wb2d );
  684.         printk( "wb 3 stat/addr/data: %04x %08lx %08lx\n",
  685.             fp->un.fmt7.wb3s, fp->un.fmt7.wb3a, fp->un.fmt7.wb3d );
  686.         printk( "push data: %08lx %08lx %08lx %08lx\n",
  687.             fp->un.fmt7.wb1dpd0, fp->un.fmt7.pd1, fp->un.fmt7.pd2,
  688.             fp->un.fmt7.pd3 );
  689.         addr += sizeof(fp->un.fmt7);
  690.         break;
  691.       case 0x9:
  692.         printk( "instr addr=%08lx\n", fp->un.fmt9.iaddr );
  693.         addr += sizeof(fp->un.fmt9);
  694.         break;
  695.       case 0xa:
  696.         printk( "ssw=%04x isc=%04x isb=%04x daddr=%08lx dobuf=%08lx\n",
  697.             fp->un.fmta.ssw, fp->un.fmta.isc, fp->un.fmta.isb,
  698.             fp->un.fmta.daddr, fp->un.fmta.dobuf );
  699.         addr += sizeof(fp->un.fmta);
  700.         break;
  701.       case 0xb:
  702.         printk( "ssw=%04x isc=%04x isb=%04x daddr=%08lx dobuf=%08lx\n",
  703.             fp->un.fmtb.ssw, fp->un.fmtb.isc, fp->un.fmtb.isb,
  704.             fp->un.fmtb.daddr, fp->un.fmtb.dobuf );
  705.         printk( "baddr=%08lx dibuf=%08lx ver=%x\n",
  706.             fp->un.fmtb.baddr, fp->un.fmtb.dibuf, fp->un.fmtb.ver );
  707.         addr += sizeof(fp->un.fmtb);
  708.         break;
  709.       default:
  710.         printk( "\n" );
  711.     }
  712.     
  713.     stack = (unsigned long *)addr;
  714. #ifdef MASTER_STACK
  715.     endstack = (unsigned long *)((fp->sr & PS_M) ?
  716.         PAGE_ALIGN(addr) : (ulong)interrupt_stack + PAGE_SIZE);
  717. #else
  718.     endstack = (unsigned long *)(PAGE_ALIGN(addr));
  719. #endif
  720.  
  721.     printk( "Stack from %08lx:\n       ", (unsigned long)stack );
  722.     for (i = 0; i < kstack_depth_to_print; i++) {
  723.         if (stack >= endstack)
  724.             break;
  725.         if (i && ((i % 8) == 0))
  726.             printk("\n       ");
  727.         printk("%08lx ", *stack++);
  728.     }
  729.  
  730.     printk ("\nCall Trace: ");
  731.     stack = (unsigned long *)addr;
  732.     i = 1;
  733.     module_start = VMALLOC_START;
  734.     module_end = module_start + MODULE_RANGE;
  735.     while( stack < endstack ) {
  736.         addr = *stack++;
  737.         /*
  738.          * If the address is either in the text segment of the
  739.          * kernel, or in the region which contains vmalloc'ed
  740.          * memory, it *may* be the address of a calling
  741.          * routine; if so, print it so that someone tracing
  742.          * down the cause of the crash will be able to figure
  743.          * out the call path that was taken.
  744.          */
  745.         if (((addr >= (unsigned long) &_start) &&
  746.              (addr <= (unsigned long) &_etext)) ||
  747.             ((addr >= module_start) && (addr <= module_end))) {
  748.             if (i && ((i % 8) == 0))
  749.                 printk("\n       ");
  750.             printk("%08lx ", addr);
  751.             i++;
  752.         }
  753.     }
  754.     printk("\nCode: ");
  755.     for (i = 0; i < 10; i++)
  756.         printk("%04x ", 0xffff & ((short *) fp->pc)[i]);
  757.     printk("\n");
  758.  
  759. }
  760.  
  761.  
  762. void bad_super_trap (struct frame *fp)
  763. {
  764. #ifdef MASTER_STACK
  765.     unsigned long isp, msp;
  766.  
  767.     /* fetch interrupt and master stack pointer */
  768.     __asm__ __volatile__ ("movec %/isp,%0" : "=r" (isp));
  769.     __asm__ __volatile__ ("movec %/msp,%0" : "=r" (msp));
  770. #endif
  771.  
  772.     console_verbose();
  773.     if (fp->vector < 48*4)
  774.         printk ("*** %s ***   FORMAT=%X\n",
  775.             vec_names[fp->vector >> 2], fp->format);
  776.     else
  777.         printk ("*** Exception %d ***   FORMAT=%X\n",
  778.             fp->vector >> 2, fp->format);
  779.  
  780. #ifdef MASTER_STACK
  781.     printk ("PC=%#010lx   SR=%#06x       SP=%#010lx  %cSP=%#010lx\n",
  782.         fp->pc, fp->sr, (ulong)fp,
  783.         (fp->sr & PS_M) ? 'I' : 'M', (fp->sr & PS_M) ? isp : msp );
  784. #else
  785.     printk ("PC=%#010lx   SR=%#06x       SP=%#010lx\n",
  786.         fp->pc, fp->sr, (ulong)fp );
  787. #endif
  788.     printk ("D0=%#010lx   D1=%#010lx   D2=%#010lx   D3=%#010lx\n",
  789.         fp->d0, fp->regs[0], fp->regs[1], fp->regs[2]);
  790.     printk ("D4=%#010lx   D5=%#010lx   D6=%#010lx   D7=%#010lx\n",
  791.         fp->regs[3], fp->regs[4], fp->regs[5], fp->regs[6]);
  792.     printk ("A0=%#010lx   A1=%#010lx   A2=%#010lx   A3=%#010lx\n",
  793.         fp->regs[7], fp->regs[8], fp->regs[9], fp->regs[10]);
  794.     printk ("A4=%#010lx   A5=%#010lx   A6=%#010lx  USP=%#010lx\n",
  795.         fp->regs[11], fp->regs[12], fp->regs[13], fp->usp);
  796.  
  797.     if ((fp->vector >> 2) == VEC_ADDRERR && !m68k_is040or060) {
  798.         unsigned short ssw = fp->un.fmtb.ssw;
  799.  
  800.         printk ("SSW=%#06x  ", ssw);
  801.  
  802.         if (ssw & RC)
  803.             printk ("Pipe stage C instruction fault at %#010lx\n",
  804.                 fp->format == 0xA ? fp->pc + 2 :
  805.                 fp->un.fmtb.baddr - 2);
  806.         if (ssw & RB)
  807.             printk ("Pipe stage B instruction fault at %#010lx\n",
  808.                 fp->format == 0xA ? fp->pc + 4 :
  809.                 fp->un.fmtb.baddr);
  810.         if (ssw & DF)
  811.             printk ("Data %s fault at %#010lx in %s (pc=%#lx)\n",
  812.                 ssw & RW ? "read" : "write",
  813.                 fp->un.fmtb.daddr, space_names[ssw & DFC],
  814.                 fp->pc);
  815.     }
  816.     dump_stack( fp );
  817.     panic ("BAD KERNEL TRAP");
  818. }
  819.  
  820. asmlinkage void trap_c(struct frame *fp)
  821. {
  822.     int sig;
  823.  
  824.     current->tss.esp0 = (unsigned long) fp;
  825.     if ((fp->sr & PS_S) && (fp->vector >> 2) == VEC_TRACE
  826.         && !(fp->sr & PS_T)) {
  827.         /* traced a trapping instruction */
  828.         unsigned char *lp = ((unsigned char *)&fp->un.fmt2) + 4;
  829.         current->flags |= PF_DTRACE;
  830.         /* clear the trace bit */
  831.         (*(unsigned short *)lp) &= ~PS_T;
  832.         return;
  833.     } else if (fp->sr & PS_S)
  834.         bad_super_trap(fp);
  835.  
  836.     /* send the appropriate signal to the user program */
  837.     switch (fp->vector >> 2) {
  838.         case VEC_ADDRERR:
  839.         sig = SIGBUS;
  840.         break;
  841.         case VEC_BUSERR:
  842.         sig = SIGSEGV;
  843.         break;
  844.         case VEC_ILLEGAL:
  845.         case VEC_PRIV:
  846.         case VEC_LINE10:
  847.         case VEC_LINE11:
  848.         case VEC_COPROC:
  849.         case VEC_TRAP1:
  850.         case VEC_TRAP2:
  851.         case VEC_TRAP3:
  852.         case VEC_TRAP4:
  853.         case VEC_TRAP5:
  854.         case VEC_TRAP6:
  855.         case VEC_TRAP7:
  856.         case VEC_TRAP8:
  857.         case VEC_TRAP9:
  858.         case VEC_TRAP10:
  859.         case VEC_TRAP11:
  860.         case VEC_TRAP12:
  861.         case VEC_TRAP13:
  862.         case VEC_TRAP14:
  863.         case VEC_UNIMPEA:
  864.         case VEC_UNIMPII:
  865.         sig = SIGILL;
  866.         break;
  867.         case VEC_FPBRUC:
  868.         case VEC_FPIR:
  869.         case VEC_FPDIVZ:
  870.         case VEC_FPUNDER:
  871.         case VEC_FPOE:
  872.         case VEC_FPOVER:
  873.         case VEC_FPNAN:
  874.         {
  875.           unsigned char fstate[216];
  876.  
  877.           __asm__ __volatile__ ("fsave %0@" : : "a" (fstate) : "memory");
  878.           /* Set the exception pending bit in the 68882 idle frame */
  879.           if (*(unsigned short *) fstate == 0x1f38)
  880.             {
  881.               fstate[fstate[1]] |= 1 << 3;
  882.               __asm__ __volatile__ ("frestore %0@" : : "a" (fstate));
  883.             }
  884.         }
  885.         /* fall through */
  886.         case VEC_ZERODIV:
  887.         case VEC_TRAP:
  888.         sig = SIGFPE;
  889.         break;
  890.         case VEC_TRACE:        /* ptrace single step */
  891.         fp->sr &= ~PS_T;
  892.         case VEC_TRAP15:        /* breakpoint */
  893.         sig = SIGTRAP;
  894.         break;
  895.         default:
  896.         sig = SIGILL;
  897.         break;
  898.     }
  899.  
  900.     send_sig (sig, current, 1);
  901. }
  902.  
  903. asmlinkage void set_esp0 (unsigned long ssp)
  904. {
  905.   current->tss.esp0 = ssp;
  906. }
  907.  
  908. #define    offsetof(t,e)    (size_t)(&(((struct t *)0)->e))
  909.  
  910. void die_if_kernel (char *str, struct frame *fp, int nr)
  911. {
  912.     if (!(fp->sr & PS_S))
  913.         return;
  914.  
  915.     console_verbose();
  916.     printk("%s: %08x\n",str,nr);
  917.     printk("PC: %08lx\nSR: %04x\n", fp->pc,fp->sr);
  918.     printk("d0: %08lx    d1: %08lx    d2: %08lx    d3: %08lx\n",
  919.            fp->d0, fp->regs[0], fp->regs[1], fp->regs[2]);
  920.     printk("d4: %08lx    d5: %08lx    d6: %08lx    d7: %08lx\n",
  921.            fp->regs[3], fp->regs[4], fp->regs[5], fp->regs[6]);
  922.     printk("a0: %08lx    a1: %08lx    a2: %08lx    a3: %08lx\n",
  923.            fp->regs[7], fp->regs[8], fp->regs[9], fp->regs[10]);
  924.     printk("a4: %08lx    a5: %08lx    a6: %08lx    a7: %08lx\n",
  925.            fp->regs[11], fp->regs[12], fp->regs[13], (u_long) fp);
  926.     if (STACK_MAGIC != *(unsigned long *)current->kernel_stack_page)
  927.         printk("Corrupted stack page\n");
  928.     printk("Process %s (pid: %d, stackpage=%08lx)\n",
  929.         current->comm, current->pid, current->kernel_stack_page);
  930.     dump_stack( fp );
  931. #ifdef MASTER_STACK
  932.     /* If we're on the interrupt stack, print out also the master stack
  933.      * contents */
  934.     if (!(fp->sr & PS_M)) {
  935.         struct intframe *fp2;
  936.         __asm__ __volatile__ ("movec %/msp,%0" : "=r" (fp2));
  937.         printk( "Master stack: PC=%08lx SR=%04x vector=%02x\n",
  938.             fp2->pc, fp2->sr, fp2->vector );
  939.         dump_stack( (struct frame *)((unsigned long)fp2 -
  940.             (offsetof(frame,sr) - offsetof(intframe,sr))) );
  941.     }
  942. #endif
  943.     do_exit(SIGSEGV);
  944. }
  945.  
  946.