[f761f1eb] | 1 | /*
|
---|
[df4ed85] | 2 | * Copyright (c) 2003-2004 Jakub Jermar
|
---|
[f761f1eb] | 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 |
|
---|
[da1bafb] | 29 | /** @addtogroup mips32
|
---|
[b45c443] | 30 | * @{
|
---|
| 31 | */
|
---|
| 32 | /** @file
|
---|
| 33 | */
|
---|
| 34 |
|
---|
[f761f1eb] | 35 | #include <arch/exception.h>
|
---|
[9c0a9b3] | 36 | #include <arch/interrupt.h>
|
---|
[b3f8fb7] | 37 | #include <arch/mm/tlb.h>
|
---|
[f761f1eb] | 38 | #include <panic.h>
|
---|
| 39 | #include <arch/cp0.h>
|
---|
[d99c1d2] | 40 | #include <typedefs.h>
|
---|
[f761f1eb] | 41 | #include <arch.h>
|
---|
[623ba26c] | 42 | #include <debug.h>
|
---|
[1084a784] | 43 | #include <proc/thread.h>
|
---|
[7a8c866a] | 44 | #include <print.h>
|
---|
| 45 | #include <interrupt.h>
|
---|
[e07fe0c] | 46 | #include <func.h>
|
---|
[7688b5d] | 47 | #include <ddi/irq.h>
|
---|
[5bb8e45] | 48 | #include <arch/debugger.h>
|
---|
[e2b762ec] | 49 | #include <symtab.h>
|
---|
| 50 |
|
---|
[a000878c] | 51 | static const char *exctable[] = {
|
---|
[2f40fe4] | 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,
|
---|
[da1bafb] | 69 | "WatchHi/WatchLo", /* 23 */
|
---|
[2f40fe4] | 70 | NULL, NULL, NULL, NULL, NULL, NULL, NULL,
|
---|
| 71 | "Virtual Coherency - data",
|
---|
[7a8c866a] | 72 | };
|
---|
| 73 |
|
---|
[22a28a69] | 74 | void istate_decode(istate_t *istate)
|
---|
[7a8c866a] | 75 | {
|
---|
[41a7f62] | 76 | printf("epc=%p\tsta=%#010" PRIx32 "\t"
|
---|
| 77 | "lo =%#010" PRIx32 "\thi =%#010" PRIx32 "\n",
|
---|
[7e752b2] | 78 | (void *) istate->epc, istate->status,
|
---|
| 79 | istate->lo, istate->hi);
|
---|
| 80 |
|
---|
[41a7f62] | 81 | printf("a0 =%#010" PRIx32 "\ta1 =%#010" PRIx32 "\t"
|
---|
| 82 | "a2 =%#010" PRIx32 "\ta3 =%#010" PRIx32 "\n",
|
---|
[0c61955] | 83 | istate->a0, istate->a1, istate->a2, istate->a3);
|
---|
[7e752b2] | 84 |
|
---|
[41a7f62] | 85 | printf("t0 =%#010" PRIx32 "\tt1 =%#010" PRIx32 "\t"
|
---|
| 86 | "t2 =%#010" PRIx32 "\tt3 =%#010" PRIx32 "\n",
|
---|
[0c61955] | 87 | istate->t0, istate->t1, istate->t2, istate->t3);
|
---|
[7e752b2] | 88 |
|
---|
[41a7f62] | 89 | printf("t4 =%#010" PRIx32 "\tt5 =%#010" PRIx32 "\t"
|
---|
| 90 | "t6 =%#010" PRIx32 "\tt7 =%#010" PRIx32 "\n",
|
---|
[0c61955] | 91 | istate->t4, istate->t5, istate->t6, istate->t7);
|
---|
[7e752b2] | 92 |
|
---|
[41a7f62] | 93 | printf("t8 =%#010" PRIx32 "\tt9 =%#010" PRIx32 "\t"
|
---|
| 94 | "v0 =%#010" PRIx32 "\tv1 =%#010" PRIx32 "\n",
|
---|
[0c61955] | 95 | istate->t8, istate->t9, istate->v0, istate->v1);
|
---|
[7e752b2] | 96 |
|
---|
[41a7f62] | 97 | printf("s0 =%#010" PRIx32 "\ts1 =%#010" PRIx32 "\t"
|
---|
| 98 | "s2 =%#010" PRIx32 "\ts3 =%#010" PRIx32 "\n",
|
---|
[0c61955] | 99 | istate->s0, istate->s1, istate->s2, istate->s3);
|
---|
[7e752b2] | 100 |
|
---|
[41a7f62] | 101 | printf("s4 =%#010" PRIx32 "\ts5 =%#010" PRIx32 "\t"
|
---|
| 102 | "s6 =%#010" PRIx32 "\ts7 =%#010" PRIx32 "\n",
|
---|
[0c61955] | 103 | istate->s4, istate->s5, istate->s6, istate->s7);
|
---|
[7e752b2] | 104 |
|
---|
[41a7f62] | 105 | printf("s8 =%#010" PRIx32 "\tat =%#010" PRIx32 "\t"
|
---|
| 106 | "kt0=%#010" PRIx32 "\tkt1=%#010" PRIx32 "\n",
|
---|
[0c61955] | 107 | istate->s8, istate->at, istate->kt0, istate->kt1);
|
---|
[7e752b2] | 108 |
|
---|
[0c61955] | 109 | printf("sp =%p\tra =%p\tgp =%p\n",
|
---|
[7e752b2] | 110 | (void *) istate->sp, (void *) istate->ra,
|
---|
| 111 | (void *) istate->gp);
|
---|
[7a8c866a] | 112 | }
|
---|
| 113 |
|
---|
[214ec25c] | 114 | static void unhandled_exception(unsigned int n, istate_t *istate)
|
---|
[7a8c866a] | 115 | {
|
---|
[f651e80] | 116 | fault_if_from_uspace(istate, "Unhandled exception %s.", exctable[n]);
|
---|
[ac11ac7] | 117 | panic_badtrap(istate, n, "Unhandled exception %s.", exctable[n]);
|
---|
[7a8c866a] | 118 | }
|
---|
| 119 |
|
---|
[214ec25c] | 120 | static void reserved_instr_exception(unsigned int n, istate_t *istate)
|
---|
[3b712407] | 121 | {
|
---|
[a000878c] | 122 | if (*((uint32_t *) istate->epc) == 0x7c03e83b) {
|
---|
[3b712407] | 123 | ASSERT(THREAD);
|
---|
| 124 | istate->epc += 4;
|
---|
[ce890ec9] | 125 | istate->v1 = istate->kt1;
|
---|
[a000878c] | 126 | } else
|
---|
[5201199] | 127 | unhandled_exception(n, istate);
|
---|
[3b712407] | 128 | }
|
---|
| 129 |
|
---|
[214ec25c] | 130 | static void breakpoint_exception(unsigned int n, istate_t *istate)
|
---|
[7a8c866a] | 131 | {
|
---|
[5bb8e45] | 132 | #ifdef CONFIG_DEBUG
|
---|
[25d7709] | 133 | debugger_bpoint(istate);
|
---|
[5bb8e45] | 134 | #else
|
---|
[7a8c866a] | 135 | /* it is necessary to not re-execute BREAK instruction after
|
---|
| 136 | returning from Exception handler
|
---|
| 137 | (see page 138 in R4000 Manual for more information) */
|
---|
[25d7709] | 138 | istate->epc += 4;
|
---|
[5bb8e45] | 139 | #endif
|
---|
[7a8c866a] | 140 | }
|
---|
| 141 |
|
---|
[214ec25c] | 142 | static void tlbmod_exception(unsigned int n, istate_t *istate)
|
---|
[7a8c866a] | 143 | {
|
---|
[25d7709] | 144 | tlb_modified(istate);
|
---|
[7a8c866a] | 145 | }
|
---|
| 146 |
|
---|
[214ec25c] | 147 | static void tlbinv_exception(unsigned int n, istate_t *istate)
|
---|
[7a8c866a] | 148 | {
|
---|
[25d7709] | 149 | tlb_invalid(istate);
|
---|
[7a8c866a] | 150 | }
|
---|
| 151 |
|
---|
[5a95b25] | 152 | #ifdef CONFIG_FPU_LAZY
|
---|
[214ec25c] | 153 | static void cpuns_exception(unsigned int n, istate_t *istate)
|
---|
[7a8c866a] | 154 | {
|
---|
| 155 | if (cp0_cause_coperr(cp0_cause_read()) == fpu_cop_id)
|
---|
| 156 | scheduler_fpu_lazy_request();
|
---|
[874621f] | 157 | else {
|
---|
[ac11ac7] | 158 | fault_if_from_uspace(istate,
|
---|
| 159 | "Unhandled Coprocessor Unusable Exception.");
|
---|
| 160 | panic_badtrap(istate, n,
|
---|
| 161 | "Unhandled Coprocessor Unusable Exception.");
|
---|
[874621f] | 162 | }
|
---|
[7a8c866a] | 163 | }
|
---|
[5a95b25] | 164 | #endif
|
---|
[7a8c866a] | 165 |
|
---|
[214ec25c] | 166 | static void interrupt_exception(unsigned int n, istate_t *istate)
|
---|
[7a8c866a] | 167 | {
|
---|
[da1bafb] | 168 | /* Decode interrupt number and process the interrupt */
|
---|
| 169 | uint32_t cause = (cp0_cause_read() >> 8) & 0xff;
|
---|
[7a8c866a] | 170 |
|
---|
[da1bafb] | 171 | unsigned int i;
|
---|
[7688b5d] | 172 | for (i = 0; i < 8; i++) {
|
---|
| 173 | if (cause & (1 << i)) {
|
---|
| 174 | irq_t *irq = irq_dispatch_and_lock(i);
|
---|
| 175 | if (irq) {
|
---|
| 176 | /*
|
---|
| 177 | * The IRQ handler was found.
|
---|
| 178 | */
|
---|
[6cd9aa6] | 179 | irq->handler(irq);
|
---|
[da1bafb] | 180 | irq_spinlock_unlock(&irq->lock, false);
|
---|
[7688b5d] | 181 | } else {
|
---|
| 182 | /*
|
---|
| 183 | * Spurious interrupt.
|
---|
| 184 | */
|
---|
| 185 | #ifdef CONFIG_DEBUG
|
---|
[214ec25c] | 186 | printf("cpu%u: spurious interrupt (inum=%u)\n",
|
---|
[6cd9aa6] | 187 | CPU->id, i);
|
---|
[7688b5d] | 188 | #endif
|
---|
| 189 | }
|
---|
| 190 | }
|
---|
| 191 | }
|
---|
[7a8c866a] | 192 | }
|
---|
| 193 |
|
---|
[1b109cb] | 194 | /** Handle syscall userspace call */
|
---|
[214ec25c] | 195 | static void syscall_exception(unsigned int n, istate_t *istate)
|
---|
[f761f1eb] | 196 | {
|
---|
[ac11ac7] | 197 | fault_if_from_uspace(istate, "Syscall is handled through shortcut.");
|
---|
[f761f1eb] | 198 | }
|
---|
[7a8c866a] | 199 |
|
---|
| 200 | void exception_init(void)
|
---|
| 201 | {
|
---|
[b3b7e14a] | 202 | unsigned int i;
|
---|
[da1bafb] | 203 |
|
---|
[7a8c866a] | 204 | /* Clear exception table */
|
---|
[7688b5d] | 205 | for (i = 0; i < IVT_ITEMS; i++)
|
---|
[b3b7e14a] | 206 | exc_register(i, "undef", false,
|
---|
| 207 | (iroutine_t) unhandled_exception);
|
---|
| 208 |
|
---|
| 209 | exc_register(EXC_Bp, "bkpoint", true,
|
---|
| 210 | (iroutine_t) breakpoint_exception);
|
---|
| 211 | exc_register(EXC_RI, "resinstr", true,
|
---|
| 212 | (iroutine_t) reserved_instr_exception);
|
---|
| 213 | exc_register(EXC_Mod, "tlb_mod", true,
|
---|
| 214 | (iroutine_t) tlbmod_exception);
|
---|
| 215 | exc_register(EXC_TLBL, "tlbinvl", true,
|
---|
| 216 | (iroutine_t) tlbinv_exception);
|
---|
| 217 | exc_register(EXC_TLBS, "tlbinvl", true,
|
---|
| 218 | (iroutine_t) tlbinv_exception);
|
---|
| 219 | exc_register(EXC_Int, "interrupt", true,
|
---|
| 220 | (iroutine_t) interrupt_exception);
|
---|
[7688b5d] | 221 |
|
---|
[7a8c866a] | 222 | #ifdef CONFIG_FPU_LAZY
|
---|
[b3b7e14a] | 223 | exc_register(EXC_CpU, "cpunus", true,
|
---|
| 224 | (iroutine_t) cpuns_exception);
|
---|
[7a8c866a] | 225 | #endif
|
---|
[b3b7e14a] | 226 |
|
---|
| 227 | exc_register(EXC_Sys, "syscall", true,
|
---|
| 228 | (iroutine_t) syscall_exception);
|
---|
[7a8c866a] | 229 | }
|
---|
[b45c443] | 230 |
|
---|
[3c5006a0] | 231 | /** @}
|
---|
[b45c443] | 232 | */
|
---|