source: mainline/kernel/arch/mips32/src/exception.c@ 19b3cc6

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 19b3cc6 was b2fa1204, checked in by Martin Sucha <sucha14@…>, 11 years ago

Cherrypick usage of kernel logger

  • Property mode set to 100644
File size: 7.0 KB
Line 
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 mips32
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#include <log.h>
51
52static const char *exctable[] = {
53 "Interrupt",
54 "TLB Modified",
55 "TLB Invalid",
56 "TLB Invalid Store",
57 "Address Error - load/instr. fetch",
58 "Address Error - store",
59 "Bus Error - fetch instruction",
60 "Bus Error - data reference",
61 "Syscall",
62 "BreakPoint",
63 "Reserved Instruction",
64 "Coprocessor Unusable",
65 "Arithmetic Overflow",
66 "Trap",
67 "Virtual Coherency - instruction",
68 "Floating Point",
69 NULL, NULL, NULL, NULL, NULL, NULL, NULL,
70 "WatchHi/WatchLo", /* 23 */
71 NULL, NULL, NULL, NULL, NULL, NULL, NULL,
72 "Virtual Coherency - data",
73};
74
75void istate_decode(istate_t *istate)
76{
77 log_printf("epc=%#010" PRIx32 "\tsta=%#010" PRIx32 "\t"
78 "lo =%#010" PRIx32 "\thi =%#010" PRIx32 "\n",
79 istate->epc, istate->status, istate->lo, istate->hi);
80
81 log_printf("a0 =%#010" PRIx32 "\ta1 =%#010" PRIx32 "\t"
82 "a2 =%#010" PRIx32 "\ta3 =%#010" PRIx32 "\n",
83 istate->a0, istate->a1, istate->a2, istate->a3);
84
85 log_printf("t0 =%#010" PRIx32 "\tt1 =%#010" PRIx32 "\t"
86 "t2 =%#010" PRIx32 "\tt3 =%#010" PRIx32 "\n",
87 istate->t0, istate->t1, istate->t2, istate->t3);
88
89 log_printf("t4 =%#010" PRIx32 "\tt5 =%#010" PRIx32 "\t"
90 "t6 =%#010" PRIx32 "\tt7 =%#010" PRIx32 "\n",
91 istate->t4, istate->t5, istate->t6, istate->t7);
92
93 log_printf("t8 =%#010" PRIx32 "\tt9 =%#010" PRIx32 "\t"
94 "v0 =%#010" PRIx32 "\tv1 =%#010" PRIx32 "\n",
95 istate->t8, istate->t9, istate->v0, istate->v1);
96
97 log_printf("s0 =%#010" PRIx32 "\ts1 =%#010" PRIx32 "\t"
98 "s2 =%#010" PRIx32 "\ts3 =%#010" PRIx32 "\n",
99 istate->s0, istate->s1, istate->s2, istate->s3);
100
101 log_printf("s4 =%#010" PRIx32 "\ts5 =%#010" PRIx32 "\t"
102 "s6 =%#010" PRIx32 "\ts7 =%#010" PRIx32 "\n",
103 istate->s4, istate->s5, istate->s6, istate->s7);
104
105 log_printf("s8 =%#010" PRIx32 "\tat =%#010" PRIx32 "\t"
106 "kt0=%#010" PRIx32 "\tkt1=%#010" PRIx32 "\n",
107 istate->s8, istate->at, istate->kt0, istate->kt1);
108
109 log_printf("sp =%#010" PRIx32 "\tra =%#010" PRIx32 "\t"
110 "gp =%#010" PRIx32 "\n",
111 istate->sp, istate->ra, istate->gp);
112}
113
114static void unhandled_exception(unsigned int n, istate_t *istate)
115{
116 fault_if_from_uspace(istate, "Unhandled exception %s.", exctable[n]);
117 panic_badtrap(istate, n, "Unhandled exception %s.", exctable[n]);
118}
119
120static void reserved_instr_exception(unsigned int n, istate_t *istate)
121{
122 if (*((uint32_t *) istate->epc) == 0x7c03e83b) {
123 ASSERT(THREAD);
124 istate->epc += 4;
125 istate->v1 = istate->kt1;
126 } else
127 unhandled_exception(n, istate);
128}
129
130static void breakpoint_exception(unsigned int n, istate_t *istate)
131{
132#ifdef CONFIG_DEBUG
133 debugger_bpoint(istate);
134#else
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) */
138 istate->epc += 4;
139#endif
140}
141
142static void tlbmod_exception(unsigned int n, istate_t *istate)
143{
144 tlb_modified(istate);
145}
146
147static void tlbinv_exception(unsigned int n, istate_t *istate)
148{
149 tlb_invalid(istate);
150}
151
152#ifdef CONFIG_FPU_LAZY
153static void cpuns_exception(unsigned int n, istate_t *istate)
154{
155 if (cp0_cause_coperr(cp0_cause_read()) == fpu_cop_id)
156 scheduler_fpu_lazy_request();
157 else {
158 fault_if_from_uspace(istate,
159 "Unhandled Coprocessor Unusable Exception.");
160 panic_badtrap(istate, n,
161 "Unhandled Coprocessor Unusable Exception.");
162 }
163}
164#endif
165
166static void interrupt_exception(unsigned int n, istate_t *istate)
167{
168 uint32_t ip;
169 uint32_t im;
170
171 /* Decode interrupt number and process the interrupt */
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;
174
175 unsigned int i;
176 for (i = 0; i < 8; i++) {
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)) {
185 irq_t *irq = irq_dispatch_and_lock(i);
186 if (irq) {
187 /*
188 * The IRQ handler was found.
189 */
190 irq->handler(irq);
191 irq_spinlock_unlock(&irq->lock, false);
192 } else {
193 /*
194 * Spurious interrupt.
195 */
196#ifdef CONFIG_DEBUG
197 log(LF_ARCH, LVL_DEBUG,
198 "cpu%u: spurious interrupt (inum=%u)",
199 CPU->id, i);
200#endif
201 }
202 }
203 }
204}
205
206/** Handle syscall userspace call */
207static void syscall_exception(unsigned int n, istate_t *istate)
208{
209 fault_if_from_uspace(istate, "Syscall is handled through shortcut.");
210}
211
212void exception_init(void)
213{
214 unsigned int i;
215
216 /* Clear exception table */
217 for (i = 0; i < IVT_ITEMS; i++)
218 exc_register(i, "undef", false,
219 (iroutine_t) unhandled_exception);
220
221 exc_register(EXC_Bp, "bkpoint", true,
222 (iroutine_t) breakpoint_exception);
223 exc_register(EXC_RI, "resinstr", true,
224 (iroutine_t) reserved_instr_exception);
225 exc_register(EXC_Mod, "tlb_mod", true,
226 (iroutine_t) tlbmod_exception);
227 exc_register(EXC_TLBL, "tlbinvl", true,
228 (iroutine_t) tlbinv_exception);
229 exc_register(EXC_TLBS, "tlbinvl", true,
230 (iroutine_t) tlbinv_exception);
231 exc_register(EXC_Int, "interrupt", true,
232 (iroutine_t) interrupt_exception);
233
234#ifdef CONFIG_FPU_LAZY
235 exc_register(EXC_CpU, "cpunus", true,
236 (iroutine_t) cpuns_exception);
237#endif
238
239 exc_register(EXC_Sys, "syscall", true,
240 (iroutine_t) syscall_exception);
241}
242
243/** @}
244 */
Note: See TracBrowser for help on using the repository browser.