exception.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2003-2004 Jakub Jermar
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions
00007  * are met:
00008  *
00009  * - Redistributions of source code must retain the above copyright
00010  *   notice, this list of conditions and the following disclaimer.
00011  * - Redistributions in binary form must reproduce the above copyright
00012  *   notice, this list of conditions and the following disclaimer in the
00013  *   documentation and/or other materials provided with the distribution.
00014  * - The name of the author may not be used to endorse or promote products
00015  *   derived from this software without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
00018  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00019  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
00020  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
00021  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
00022  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00023  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00024  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00025  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
00026  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00027  */
00028 
00035 #include <arch/exception.h>
00036 #include <arch/interrupt.h>
00037 #include <panic.h>
00038 #include <arch/cp0.h>
00039 #include <arch/types.h>
00040 #include <arch.h>
00041 #include <debug.h>
00042 #include <proc/thread.h>
00043 #include <symtab.h>
00044 #include <print.h>
00045 #include <interrupt.h>
00046 #include <func.h>
00047 #include <console/kconsole.h>
00048 #include <arch/debugger.h>
00049 
00050 static char * exctable[] = {
00051         "Interrupt","TLB Modified","TLB Invalid","TLB Invalid Store",
00052                 "Address Error - load/instr. fetch",
00053                 "Address Error - store",
00054                 "Bus Error - fetch instruction",
00055                 "Bus Error - data reference",
00056                 "Syscall",
00057                 "BreakPoint",
00058                 "Reserved Instruction",
00059                 "Coprocessor Unusable",
00060                 "Arithmetic Overflow",
00061                 "Trap",
00062                 "Virtual Coherency - instruction",
00063                 "Floating Point",
00064                 NULL, NULL, NULL, NULL, NULL, NULL, NULL,
00065                 "WatchHi/WatchLo", /* 23 */
00066                 NULL, NULL, NULL, NULL, NULL, NULL, NULL,
00067                 "Virtual Coherency - data",
00068 };
00069 
00070 static void print_regdump(istate_t *istate)
00071 {
00072         char *pcsymbol = "";
00073         char *rasymbol = "";
00074 
00075         char *s = get_symtab_entry(istate->epc);
00076         if (s)
00077                 pcsymbol = s;
00078         s = get_symtab_entry(istate->ra);
00079         if (s)
00080                 rasymbol = s;
00081         
00082         printf("PC: %#x(%s) RA: %#x(%s), SP(%p)\n", istate->epc, pcsymbol, istate->ra, rasymbol, istate->sp);
00083 }
00084 
00085 static void unhandled_exception(int n, istate_t *istate)
00086 {
00087         fault_if_from_uspace(istate, "unhandled exception %s", exctable[n]);
00088         
00089         print_regdump(istate);
00090         panic("unhandled exception %s\n", exctable[n]);
00091 }
00092 
00093 static void reserved_instr_exception(int n, istate_t *istate)
00094 {
00095         if (*((__u32 *)istate->epc) == 0x7c03e83b) {
00096                 ASSERT(THREAD);
00097                 istate->epc += 4;
00098                 istate->v1 = istate->k1;
00099         } else 
00100                 unhandled_exception(n, istate);
00101 }
00102 
00103 static void breakpoint_exception(int n, istate_t *istate)
00104 {
00105 #ifdef CONFIG_DEBUG
00106         debugger_bpoint(istate);
00107 #else
00108         /* it is necessary to not re-execute BREAK instruction after 
00109            returning from Exception handler
00110            (see page 138 in R4000 Manual for more information) */
00111         istate->epc += 4;
00112 #endif
00113 }
00114 
00115 static void tlbmod_exception(int n, istate_t *istate)
00116 {
00117         tlb_modified(istate);
00118 }
00119 
00120 static void tlbinv_exception(int n, istate_t *istate)
00121 {
00122         tlb_invalid(istate);
00123 }
00124 
00125 #ifdef CONFIG_FPU_LAZY
00126 static void cpuns_exception(int n, istate_t *istate)
00127 {
00128         if (cp0_cause_coperr(cp0_cause_read()) == fpu_cop_id)
00129                 scheduler_fpu_lazy_request();
00130         else {
00131                 fault_if_from_uspace(istate, "unhandled Coprocessor Unusable Exception");
00132                 panic("unhandled Coprocessor Unusable Exception\n");
00133         }
00134 }
00135 #endif
00136 
00137 static void interrupt_exception(int n, istate_t *istate)
00138 {
00139         __u32 cause;
00140         int i;
00141         
00142         /* decode interrupt number and process the interrupt */
00143         cause = (cp0_cause_read() >> 8) &0xff;
00144         
00145         for (i = 0; i < 8; i++)
00146                 if (cause & (1 << i))
00147                         exc_dispatch(i+INT_OFFSET, istate);
00148 }
00149 
00151 static void syscall_exception(int n, istate_t *istate)
00152 {
00153         panic("Syscall is handled through shortcut");
00154 }
00155 
00156 void exception_init(void)
00157 {
00158         int i;
00159 
00160         /* Clear exception table */
00161         for (i=0;i < IVT_ITEMS; i++)
00162                 exc_register(i, "undef", (iroutine) unhandled_exception);
00163         exc_register(EXC_Bp, "bkpoint", (iroutine) breakpoint_exception);
00164         exc_register(EXC_RI, "resinstr", (iroutine) reserved_instr_exception);
00165         exc_register(EXC_Mod, "tlb_mod", (iroutine) tlbmod_exception);
00166         exc_register(EXC_TLBL, "tlbinvl", (iroutine) tlbinv_exception);
00167         exc_register(EXC_TLBS, "tlbinvl", (iroutine) tlbinv_exception);
00168         exc_register(EXC_Int, "interrupt", (iroutine) interrupt_exception);
00169 #ifdef CONFIG_FPU_LAZY
00170         exc_register(EXC_CpU, "cpunus", (iroutine) cpuns_exception);
00171 #endif
00172         exc_register(EXC_Sys, "syscall", (iroutine) syscall_exception);
00173 }
00174 

Generated on Sun Jun 18 17:01:57 2006 for HelenOS Kernel (mips32) by  doxygen 1.4.6