[2429e4a] | 1 | /*
|
---|
| 2 | * Copyright (c) 2003-2004 Jakub Jermar
|
---|
| 3 | * All rights reserved.
|
---|
| 4 | *
|
---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
---|
| 6 | * modification, are permitted provided that the following conditions
|
---|
| 7 | * are met:
|
---|
| 8 | *
|
---|
| 9 | * - Redistributions of source code must retain the above copyright
|
---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 13 | * documentation and/or other materials provided with the distribution.
|
---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
---|
| 15 | * derived from this software without specific prior written permission.
|
---|
| 16 | *
|
---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 27 | */
|
---|
| 28 |
|
---|
| 29 | /** @addtogroup mips64
|
---|
| 30 | * @{
|
---|
| 31 | */
|
---|
| 32 | /** @file
|
---|
| 33 | */
|
---|
| 34 |
|
---|
| 35 | #include <arch/exception.h>
|
---|
| 36 | #include <arch/interrupt.h>
|
---|
| 37 | #include <arch/mm/tlb.h>
|
---|
| 38 | #include <panic.h>
|
---|
| 39 | #include <arch/cp0.h>
|
---|
| 40 | #include <typedefs.h>
|
---|
| 41 | #include <arch.h>
|
---|
| 42 | #include <debug.h>
|
---|
| 43 | #include <proc/thread.h>
|
---|
| 44 | #include <print.h>
|
---|
| 45 | #include <interrupt.h>
|
---|
| 46 | #include <func.h>
|
---|
| 47 | #include <ddi/irq.h>
|
---|
| 48 | #include <arch/debugger.h>
|
---|
| 49 | #include <symtab.h>
|
---|
| 50 |
|
---|
| 51 | static const char *exctable[] = {
|
---|
| 52 | "Interrupt",
|
---|
| 53 | "TLB Modified",
|
---|
| 54 | "TLB Invalid",
|
---|
| 55 | "TLB Invalid Store",
|
---|
| 56 | "Address Error - load/instr. fetch",
|
---|
| 57 | "Address Error - store",
|
---|
| 58 | "Bus Error - fetch instruction",
|
---|
| 59 | "Bus Error - data reference",
|
---|
| 60 | "Syscall",
|
---|
| 61 | "BreakPoint",
|
---|
| 62 | "Reserved Instruction",
|
---|
| 63 | "Coprocessor Unusable",
|
---|
| 64 | "Arithmetic Overflow",
|
---|
| 65 | "Trap",
|
---|
| 66 | "Virtual Coherency - instruction",
|
---|
| 67 | "Floating Point",
|
---|
| 68 | NULL, NULL, NULL, NULL, NULL, NULL, NULL,
|
---|
| 69 | "WatchHi/WatchLo", /* 23 */
|
---|
| 70 | NULL, NULL, NULL, NULL, NULL, NULL, NULL,
|
---|
| 71 | "Virtual Coherency - data",
|
---|
| 72 | };
|
---|
| 73 |
|
---|
| 74 | void istate_decode(istate_t *istate)
|
---|
| 75 | {
|
---|
| 76 | printf("epc=%#018" PRIx64 "\tsta=%#018" PRIx64 "\t"
|
---|
| 77 | "lo =%#018" PRIx64 "\thi =%#018" PRIx64 "\n",
|
---|
| 78 | istate->epc, istate->status, istate->lo, istate->hi);
|
---|
| 79 |
|
---|
| 80 | printf("a0 =%#018" PRIx64 "\ta1 =%#018" PRIx64 "\t"
|
---|
| 81 | "a2 =%#018" PRIx64 "\ta3 =%#018" PRIx64 "\n",
|
---|
| 82 | istate->a0, istate->a1, istate->a2, istate->a3);
|
---|
| 83 |
|
---|
| 84 | printf("t0 =%#018" PRIx64 "\tt1 =%#018" PRIx64 "\t"
|
---|
| 85 | "t2 =%#018" PRIx64 "\tt3 =%#018" PRIx64 "\n",
|
---|
| 86 | istate->t0, istate->t1, istate->t2, istate->t3);
|
---|
| 87 |
|
---|
| 88 | printf("t4 =%#018" PRIx64 "\tt5 =%#018" PRIx64 "\t"
|
---|
| 89 | "t6 =%#018" PRIx64 "\tt7 =%#018" PRIx64 "\n",
|
---|
| 90 | istate->t4, istate->t5, istate->t6, istate->t7);
|
---|
| 91 |
|
---|
| 92 | printf("t8 =%#018" PRIx64 "\tt9 =%#018" PRIx64 "\t"
|
---|
| 93 | "v0 =%#018" PRIx64 "\tv1 =%#018" PRIx64 "\n",
|
---|
| 94 | istate->t8, istate->t9, istate->v0, istate->v1);
|
---|
| 95 |
|
---|
| 96 | printf("s0 =%#018" PRIx64 "\ts1 =%#018" PRIx64 "\t"
|
---|
| 97 | "s2 =%#018" PRIx64 "\ts3 =%#018" PRIx64 "\n",
|
---|
| 98 | istate->s0, istate->s1, istate->s2, istate->s3);
|
---|
| 99 |
|
---|
| 100 | printf("s4 =%#018" PRIx64 "\ts5 =%#018" PRIx64 "\t"
|
---|
| 101 | "s6 =%#018" PRIx64 "\ts7 =%#018" PRIx64 "\n",
|
---|
| 102 | istate->s4, istate->s5, istate->s6, istate->s7);
|
---|
| 103 |
|
---|
| 104 | printf("s8 =%#018" PRIx64 "\tat =%#018" PRIx64 "\t"
|
---|
| 105 | "kt0=%#018" PRIx64 "\tkt1=%#018" PRIx64 "\n",
|
---|
| 106 | istate->s8, istate->at, istate->kt0, istate->kt1);
|
---|
| 107 |
|
---|
| 108 | printf("sp =%#018" PRIx64 "\tra =%#018" PRIx64 "\t"
|
---|
| 109 | "gp =%#018" PRIx64 "\n",
|
---|
| 110 | istate->sp, istate->ra, istate->gp);
|
---|
| 111 | }
|
---|
| 112 |
|
---|
| 113 | static void unhandled_exception(unsigned int n, istate_t *istate)
|
---|
| 114 | {
|
---|
| 115 | fault_if_from_uspace(istate, "Unhandled exception %s.", exctable[n]);
|
---|
| 116 | panic_badtrap(istate, n, "Unhandled exception %s.", exctable[n]);
|
---|
| 117 | }
|
---|
| 118 |
|
---|
| 119 | static void reserved_instr_exception(unsigned int n, istate_t *istate)
|
---|
| 120 | {
|
---|
| 121 | if (*((uint32_t *) istate->epc) == 0x7c03e83b) {
|
---|
| 122 | ASSERT(THREAD);
|
---|
| 123 | istate->epc += 4;
|
---|
| 124 | istate->v1 = istate->kt1;
|
---|
| 125 | } else
|
---|
| 126 | unhandled_exception(n, istate);
|
---|
| 127 | }
|
---|
| 128 |
|
---|
| 129 | static void breakpoint_exception(unsigned int n, istate_t *istate)
|
---|
| 130 | {
|
---|
| 131 | #ifdef CONFIG_DEBUG
|
---|
| 132 | debugger_bpoint(istate);
|
---|
| 133 | #else
|
---|
| 134 | /* it is necessary to not re-execute BREAK instruction after
|
---|
| 135 | returning from Exception handler
|
---|
| 136 | (see page 138 in R4000 Manual for more information) */
|
---|
| 137 | istate->epc += 4;
|
---|
| 138 | #endif
|
---|
| 139 | }
|
---|
| 140 |
|
---|
| 141 | static void tlbmod_exception(unsigned int n, istate_t *istate)
|
---|
| 142 | {
|
---|
| 143 | tlb_modified(istate);
|
---|
| 144 | }
|
---|
| 145 |
|
---|
| 146 | static void tlbinv_exception(unsigned int n, istate_t *istate)
|
---|
| 147 | {
|
---|
| 148 | tlb_invalid(istate);
|
---|
| 149 | }
|
---|
| 150 |
|
---|
| 151 | #ifdef CONFIG_FPU_LAZY
|
---|
| 152 | static void cpuns_exception(unsigned int n, istate_t *istate)
|
---|
| 153 | {
|
---|
| 154 | if (cp0_cause_coperr(cp0_cause_read()) == fpu_cop_id)
|
---|
| 155 | scheduler_fpu_lazy_request();
|
---|
| 156 | else {
|
---|
| 157 | fault_if_from_uspace(istate,
|
---|
| 158 | "Unhandled Coprocessor Unusable Exception.");
|
---|
| 159 | panic_badtrap(istate, n,
|
---|
| 160 | "Unhandled Coprocessor Unusable Exception.");
|
---|
| 161 | }
|
---|
| 162 | }
|
---|
| 163 | #endif
|
---|
| 164 |
|
---|
| 165 | static void interrupt_exception(unsigned int n, istate_t *istate)
|
---|
| 166 | {
|
---|
| 167 | /* Decode interrupt number and process the interrupt */
|
---|
| 168 | uint32_t cause = (cp0_cause_read() >> 8) & 0xff;
|
---|
| 169 |
|
---|
| 170 | unsigned int i;
|
---|
| 171 | for (i = 0; i < 8; i++) {
|
---|
| 172 | if (cause & (1 << i)) {
|
---|
| 173 | irq_t *irq = irq_dispatch_and_lock(i);
|
---|
| 174 | if (irq) {
|
---|
| 175 | /*
|
---|
| 176 | * The IRQ handler was found.
|
---|
| 177 | */
|
---|
| 178 | irq->handler(irq);
|
---|
| 179 | irq_spinlock_unlock(&irq->lock, false);
|
---|
| 180 | } else {
|
---|
| 181 | /*
|
---|
| 182 | * Spurious interrupt.
|
---|
| 183 | */
|
---|
| 184 | #ifdef CONFIG_DEBUG
|
---|
| 185 | printf("cpu%u: spurious interrupt (inum=%u)\n",
|
---|
| 186 | CPU->id, i);
|
---|
| 187 | #endif
|
---|
| 188 | }
|
---|
| 189 | }
|
---|
| 190 | }
|
---|
| 191 | }
|
---|
| 192 |
|
---|
| 193 | /** Handle syscall userspace call */
|
---|
| 194 | static void syscall_exception(unsigned int n, istate_t *istate)
|
---|
| 195 | {
|
---|
| 196 | fault_if_from_uspace(istate, "Syscall is handled through shortcut.");
|
---|
| 197 | }
|
---|
| 198 |
|
---|
| 199 | void exception_init(void)
|
---|
| 200 | {
|
---|
| 201 | unsigned int i;
|
---|
| 202 |
|
---|
| 203 | /* Clear exception table */
|
---|
| 204 | for (i = 0; i < IVT_ITEMS; i++)
|
---|
| 205 | exc_register(i, "undef", false,
|
---|
| 206 | (iroutine_t) unhandled_exception);
|
---|
| 207 |
|
---|
| 208 | exc_register(EXC_Bp, "bkpoint", true,
|
---|
| 209 | (iroutine_t) breakpoint_exception);
|
---|
| 210 | exc_register(EXC_RI, "resinstr", true,
|
---|
| 211 | (iroutine_t) reserved_instr_exception);
|
---|
| 212 | exc_register(EXC_Mod, "tlb_mod", true,
|
---|
| 213 | (iroutine_t) tlbmod_exception);
|
---|
| 214 | exc_register(EXC_TLBL, "tlbinvl", true,
|
---|
| 215 | (iroutine_t) tlbinv_exception);
|
---|
| 216 | exc_register(EXC_TLBS, "tlbinvl", true,
|
---|
| 217 | (iroutine_t) tlbinv_exception);
|
---|
| 218 | exc_register(EXC_Int, "interrupt", true,
|
---|
| 219 | (iroutine_t) interrupt_exception);
|
---|
| 220 |
|
---|
| 221 | #ifdef CONFIG_FPU_LAZY
|
---|
| 222 | exc_register(EXC_CpU, "cpunus", true,
|
---|
| 223 | (iroutine_t) cpuns_exception);
|
---|
| 224 | #endif
|
---|
| 225 |
|
---|
| 226 | exc_register(EXC_Sys, "syscall", true,
|
---|
| 227 | (iroutine_t) syscall_exception);
|
---|
| 228 | }
|
---|
| 229 |
|
---|
| 230 | /** @}
|
---|
| 231 | */
|
---|