[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 |
|
---|
[c5429fe] | 29 | /** @addtogroup kernel_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>
|
---|
| 40 | #include <arch.h>
|
---|
[63e27ef] | 41 | #include <assert.h>
|
---|
[1084a784] | 42 | #include <proc/thread.h>
|
---|
[7a8c866a] | 43 | #include <interrupt.h>
|
---|
[b2e121a] | 44 | #include <halt.h>
|
---|
[7688b5d] | 45 | #include <ddi/irq.h>
|
---|
[5bb8e45] | 46 | #include <arch/debugger.h>
|
---|
[e2b762ec] | 47 | #include <symtab.h>
|
---|
[b2fa1204] | 48 | #include <log.h>
|
---|
[e2b762ec] | 49 |
|
---|
[a000878c] | 50 | static const char *exctable[] = {
|
---|
[2f40fe4] | 51 | "Interrupt",
|
---|
| 52 | "TLB Modified",
|
---|
| 53 | "TLB Invalid",
|
---|
| 54 | "TLB Invalid Store",
|
---|
| 55 | "Address Error - load/instr. fetch",
|
---|
| 56 | "Address Error - store",
|
---|
| 57 | "Bus Error - fetch instruction",
|
---|
| 58 | "Bus Error - data reference",
|
---|
| 59 | "Syscall",
|
---|
| 60 | "BreakPoint",
|
---|
| 61 | "Reserved Instruction",
|
---|
| 62 | "Coprocessor Unusable",
|
---|
| 63 | "Arithmetic Overflow",
|
---|
| 64 | "Trap",
|
---|
| 65 | "Virtual Coherency - instruction",
|
---|
| 66 | "Floating Point",
|
---|
| 67 | NULL, NULL, NULL, NULL, NULL, NULL, NULL,
|
---|
[da1bafb] | 68 | "WatchHi/WatchLo", /* 23 */
|
---|
[2f40fe4] | 69 | NULL, NULL, NULL, NULL, NULL, NULL, NULL,
|
---|
| 70 | "Virtual Coherency - data",
|
---|
[7a8c866a] | 71 | };
|
---|
| 72 |
|
---|
[22a28a69] | 73 | void istate_decode(istate_t *istate)
|
---|
[7a8c866a] | 74 | {
|
---|
[b2fa1204] | 75 | log_printf("epc=%#010" PRIx32 "\tsta=%#010" PRIx32 "\t"
|
---|
[41a7f62] | 76 | "lo =%#010" PRIx32 "\thi =%#010" PRIx32 "\n",
|
---|
[8469c53] | 77 | istate->epc, istate->status, istate->lo, istate->hi);
|
---|
[a35b458] | 78 |
|
---|
[b2fa1204] | 79 | log_printf("a0 =%#010" PRIx32 "\ta1 =%#010" PRIx32 "\t"
|
---|
[41a7f62] | 80 | "a2 =%#010" PRIx32 "\ta3 =%#010" PRIx32 "\n",
|
---|
[0c61955] | 81 | istate->a0, istate->a1, istate->a2, istate->a3);
|
---|
[a35b458] | 82 |
|
---|
[b2fa1204] | 83 | log_printf("t0 =%#010" PRIx32 "\tt1 =%#010" PRIx32 "\t"
|
---|
[41a7f62] | 84 | "t2 =%#010" PRIx32 "\tt3 =%#010" PRIx32 "\n",
|
---|
[0c61955] | 85 | istate->t0, istate->t1, istate->t2, istate->t3);
|
---|
[a35b458] | 86 |
|
---|
[b2fa1204] | 87 | log_printf("t4 =%#010" PRIx32 "\tt5 =%#010" PRIx32 "\t"
|
---|
[41a7f62] | 88 | "t6 =%#010" PRIx32 "\tt7 =%#010" PRIx32 "\n",
|
---|
[0c61955] | 89 | istate->t4, istate->t5, istate->t6, istate->t7);
|
---|
[a35b458] | 90 |
|
---|
[b2fa1204] | 91 | log_printf("t8 =%#010" PRIx32 "\tt9 =%#010" PRIx32 "\t"
|
---|
[41a7f62] | 92 | "v0 =%#010" PRIx32 "\tv1 =%#010" PRIx32 "\n",
|
---|
[0c61955] | 93 | istate->t8, istate->t9, istate->v0, istate->v1);
|
---|
[a35b458] | 94 |
|
---|
[b2fa1204] | 95 | log_printf("s0 =%#010" PRIx32 "\ts1 =%#010" PRIx32 "\t"
|
---|
[41a7f62] | 96 | "s2 =%#010" PRIx32 "\ts3 =%#010" PRIx32 "\n",
|
---|
[0c61955] | 97 | istate->s0, istate->s1, istate->s2, istate->s3);
|
---|
[a35b458] | 98 |
|
---|
[b2fa1204] | 99 | log_printf("s4 =%#010" PRIx32 "\ts5 =%#010" PRIx32 "\t"
|
---|
[41a7f62] | 100 | "s6 =%#010" PRIx32 "\ts7 =%#010" PRIx32 "\n",
|
---|
[0c61955] | 101 | istate->s4, istate->s5, istate->s6, istate->s7);
|
---|
[a35b458] | 102 |
|
---|
[b2fa1204] | 103 | log_printf("s8 =%#010" PRIx32 "\tat =%#010" PRIx32 "\t"
|
---|
[41a7f62] | 104 | "kt0=%#010" PRIx32 "\tkt1=%#010" PRIx32 "\n",
|
---|
[0c61955] | 105 | istate->s8, istate->at, istate->kt0, istate->kt1);
|
---|
[a35b458] | 106 |
|
---|
[b2fa1204] | 107 | log_printf("sp =%#010" PRIx32 "\tra =%#010" PRIx32 "\t"
|
---|
[8469c53] | 108 | "gp =%#010" PRIx32 "\n",
|
---|
| 109 | istate->sp, istate->ra, istate->gp);
|
---|
[7a8c866a] | 110 | }
|
---|
| 111 |
|
---|
[214ec25c] | 112 | static void unhandled_exception(unsigned int n, istate_t *istate)
|
---|
[7a8c866a] | 113 | {
|
---|
[f651e80] | 114 | fault_if_from_uspace(istate, "Unhandled exception %s.", exctable[n]);
|
---|
[ac11ac7] | 115 | panic_badtrap(istate, n, "Unhandled exception %s.", exctable[n]);
|
---|
[7a8c866a] | 116 | }
|
---|
| 117 |
|
---|
[214ec25c] | 118 | static void reserved_instr_exception(unsigned int n, istate_t *istate)
|
---|
[3b712407] | 119 | {
|
---|
[a000878c] | 120 | if (*((uint32_t *) istate->epc) == 0x7c03e83b) {
|
---|
[63e27ef] | 121 | assert(THREAD);
|
---|
[3b712407] | 122 | istate->epc += 4;
|
---|
[ce890ec9] | 123 | istate->v1 = istate->kt1;
|
---|
[a000878c] | 124 | } else
|
---|
[5201199] | 125 | unhandled_exception(n, istate);
|
---|
[3b712407] | 126 | }
|
---|
| 127 |
|
---|
[214ec25c] | 128 | static void breakpoint_exception(unsigned int n, istate_t *istate)
|
---|
[7a8c866a] | 129 | {
|
---|
[5bb8e45] | 130 | #ifdef CONFIG_DEBUG
|
---|
[25d7709] | 131 | debugger_bpoint(istate);
|
---|
[5bb8e45] | 132 | #else
|
---|
[7c3fb9b] | 133 | /*
|
---|
| 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 | */
|
---|
[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 | {
|
---|
[7be6379] | 168 | uint32_t ip;
|
---|
| 169 | uint32_t im;
|
---|
| 170 |
|
---|
[da1bafb] | 171 | /* Decode interrupt number and process the interrupt */
|
---|
[7be6379] | 172 | ip = (cp0_cause_read() & cp0_cause_ip_mask) >> cp0_cause_ip_shift;
|
---|
| 173 | im = (cp0_status_read() & cp0_status_im_mask) >> cp0_status_im_shift;
|
---|
[a35b458] | 174 |
|
---|
[da1bafb] | 175 | unsigned int i;
|
---|
[7688b5d] | 176 | for (i = 0; i < 8; i++) {
|
---|
[7be6379] | 177 |
|
---|
| 178 | /*
|
---|
| 179 | * The interrupt could only occur if it is unmasked in the
|
---|
| 180 | * status register. On the other hand, an interrupt can be
|
---|
| 181 | * apparently pending even if it is masked, so we need to
|
---|
| 182 | * check both the masked and pending interrupts.
|
---|
| 183 | */
|
---|
| 184 | if (im & ip & (1 << i)) {
|
---|
[7688b5d] | 185 | irq_t *irq = irq_dispatch_and_lock(i);
|
---|
| 186 | if (irq) {
|
---|
| 187 | /*
|
---|
| 188 | * The IRQ handler was found.
|
---|
| 189 | */
|
---|
[6cd9aa6] | 190 | irq->handler(irq);
|
---|
[68d8736] | 191 | if (irq->cir)
|
---|
| 192 | irq->cir(irq->cir_arg, i);
|
---|
[da1bafb] | 193 | irq_spinlock_unlock(&irq->lock, false);
|
---|
[7688b5d] | 194 | } else {
|
---|
| 195 | /*
|
---|
| 196 | * Spurious interrupt.
|
---|
| 197 | */
|
---|
| 198 | #ifdef CONFIG_DEBUG
|
---|
[b2fa1204] | 199 | log(LF_ARCH, LVL_DEBUG,
|
---|
| 200 | "cpu%u: spurious interrupt (inum=%u)",
|
---|
[6cd9aa6] | 201 | CPU->id, i);
|
---|
[7688b5d] | 202 | #endif
|
---|
| 203 | }
|
---|
| 204 | }
|
---|
| 205 | }
|
---|
[7a8c866a] | 206 | }
|
---|
| 207 |
|
---|
[1b109cb] | 208 | /** Handle syscall userspace call */
|
---|
[214ec25c] | 209 | static void syscall_exception(unsigned int n, istate_t *istate)
|
---|
[f761f1eb] | 210 | {
|
---|
[ac11ac7] | 211 | fault_if_from_uspace(istate, "Syscall is handled through shortcut.");
|
---|
[f761f1eb] | 212 | }
|
---|
[7a8c866a] | 213 |
|
---|
| 214 | void exception_init(void)
|
---|
| 215 | {
|
---|
[b3b7e14a] | 216 | unsigned int i;
|
---|
[a35b458] | 217 |
|
---|
[7a8c866a] | 218 | /* Clear exception table */
|
---|
[7688b5d] | 219 | for (i = 0; i < IVT_ITEMS; i++)
|
---|
[b3b7e14a] | 220 | exc_register(i, "undef", false,
|
---|
| 221 | (iroutine_t) unhandled_exception);
|
---|
[a35b458] | 222 |
|
---|
[b3b7e14a] | 223 | exc_register(EXC_Bp, "bkpoint", true,
|
---|
| 224 | (iroutine_t) breakpoint_exception);
|
---|
| 225 | exc_register(EXC_RI, "resinstr", true,
|
---|
| 226 | (iroutine_t) reserved_instr_exception);
|
---|
| 227 | exc_register(EXC_Mod, "tlb_mod", true,
|
---|
| 228 | (iroutine_t) tlbmod_exception);
|
---|
| 229 | exc_register(EXC_TLBL, "tlbinvl", true,
|
---|
| 230 | (iroutine_t) tlbinv_exception);
|
---|
| 231 | exc_register(EXC_TLBS, "tlbinvl", true,
|
---|
| 232 | (iroutine_t) tlbinv_exception);
|
---|
| 233 | exc_register(EXC_Int, "interrupt", true,
|
---|
| 234 | (iroutine_t) interrupt_exception);
|
---|
[a35b458] | 235 |
|
---|
[7a8c866a] | 236 | #ifdef CONFIG_FPU_LAZY
|
---|
[b3b7e14a] | 237 | exc_register(EXC_CpU, "cpunus", true,
|
---|
| 238 | (iroutine_t) cpuns_exception);
|
---|
[7a8c866a] | 239 | #endif
|
---|
[a35b458] | 240 |
|
---|
[b3b7e14a] | 241 | exc_register(EXC_Sys, "syscall", true,
|
---|
| 242 | (iroutine_t) syscall_exception);
|
---|
[7a8c866a] | 243 | }
|
---|
[b45c443] | 244 |
|
---|
[3c5006a0] | 245 | /** @}
|
---|
[b45c443] | 246 | */
|
---|