1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
|
/*
* File : mips_excpt.c
* This file is part of RT-Thread RTOS
* COPYRIGHT (C) 2008 - 2012, RT-Thread Development Team
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Change Logs:
* Date Author Notes
* 2016��9��7�� Urey the first version
*/
#include <rtthread.h>
#include <rthw.h>
#include <stdio.h>
#include "mips.h"
#include "mips_excpt.h"
extern int backtrace(void);
int backtrace_ctx(mips_reg_ctx *ctx);
extern long list_thread(void);
const static char *regstr[] = {
"$0 zero", "$1 at", "$2 v0", "$3 v1", "$4 a0", "$5 a1", "$6 a2", "$7 a3",
"$8 t0", "$9 t1", "$10 t2", "$11 t3", "$12 t4", "$13 t5", "$14 t6", "$15 t7",
"$16 s0", "$17 s1", "$18 s2", "$19 s3", "$20 s4", "$21 s5", "$22 s6", "$23 s7",
"$24 t8", "$25 t9", "$26 k0", "$27 k1", "$28 gp", "$29 sp", "$30 fp", "$31 ra"
};
static const char *cause_strings[32] =
{
/* 0 */ "Int",
/* 1 */ "TLB Mods",
/* 2 */ "TLB Load",
/* 3 */ "TLB Store",
/* 4 */ "Address Load",
/* 5 */ "Address Store",
/* 6 */ "Instruction Bus Error",
/* 7 */ "Data Bus Error",
/* 8 */ "Syscall",
/* 9 */ "Breakpoint",
/* 10 */ "Reserved Instruction",
/* 11 */ "Coprocessor Unuseable",
/* 12 */ "Overflow",
/* 13 */ "Trap",
/* 14 */ "Instruction Virtual Coherency Error",
/* 15 */ "FP Exception",
/* 16 */ "Reserved 16",
/* 17 */ "Reserved 17",
/* 18 */ "Reserved 18",
/* 19 */ "Reserved 19",
/* 20 */ "Reserved 20",
/* 21 */ "Reserved 21",
/* 22 */ "Reserved 22",
/* 23 */ "Watch",
/* 24 */ "Reserved 24",
/* 25 */ "Reserved 25",
/* 26 */ "Reserved 26",
/* 27 */ "Reserved 27",
/* 28 */ "Reserved 28",
/* 29 */ "Reserved 29",
/* 30 */ "Reserved 30",
/* 31 */ "Data Virtual Coherency Error"
};
/**
* exception handle table
*/
exception_func_t sys_exception_handlers[32];
static void mod_handler(mips_reg_ctx *regs)
{
rt_kprintf("tlb modification exception\n");
rt_kprintf("exception happens, epc: 0x%08x\n", read_c0_epc());
rt_kprintf(" cause: 0x%08x\n", read_c0_cause());
list_thread();
printf("-----------------------------------------------------\n");
printf("BACKTRACE:\n");
backtrace();
printf("-----------------------------------------------------\n");
rt_hw_cpu_shutdown();
}
static void tlbl_handler(mips_reg_ctx *regs)
{
rt_kprintf("tlb exception: load\n");
rt_kprintf("exception happens, epc: 0x%08x\n", read_c0_epc());
rt_kprintf(" cause: 0x%08x\n", read_c0_cause());
list_thread();
printf("-----------------------------------------------------\n");
printf("BACKTRACE:\n");
backtrace();
printf("-----------------------------------------------------\n");
rt_hw_cpu_shutdown();
}
static void tlbs_handler(mips_reg_ctx *regs)
{
rt_kprintf("tlb exception: store\n");
rt_kprintf("exception happens, epc: 0x%08x\n", read_c0_epc());
rt_kprintf(" cause: 0x%08x\n", read_c0_cause());
list_thread();
printf("-----------------------------------------------------\n");
printf("BACKTRACE:\n");
backtrace();
printf("-----------------------------------------------------\n");
rt_hw_cpu_shutdown();
}
static void adel_handler(mips_reg_ctx *regs)
{
rt_kprintf("address error exception: load\n");
rt_kprintf("exception happens, epc: 0x%08x\n", read_c0_epc());
rt_kprintf(" cause: 0x%08x\n", read_c0_cause());
list_thread();
rt_kprintf("current thread: %.*s\n", RT_NAME_MAX, rt_thread_self()->name);
printf("-----------------------------------------------------\n");
printf("BACKTRACE:\n");
backtrace();
printf("-----------------------------------------------------\n");
rt_hw_cpu_shutdown();
}
static void ades_handler(mips_reg_ctx *regs)
{
rt_kprintf("address error exception: store\n");
rt_kprintf("exception happens, epc: 0x%08x\n", read_c0_epc());
rt_kprintf(" cause: 0x%08x\n", read_c0_cause());
list_thread();
printf("-----------------------------------------------------\n");
printf("BACKTRACE:\n");
backtrace();
printf("-----------------------------------------------------\n");
rt_hw_cpu_shutdown();
}
static void fpe_handler(mips_reg_ctx *regs)
{
rt_kprintf("floating point exception\n");
rt_kprintf("exception happens, epc: 0x%08x\n", read_c0_epc());
rt_kprintf(" cause: 0x%08x\n", read_c0_cause());
list_thread();
printf("-----------------------------------------------------\n");
printf("BACKTRACE:\n");
backtrace();
printf("-----------------------------------------------------\n");
rt_hw_cpu_shutdown();
}
static void unhandled_exception_handle(mips_reg_ctx *regs)
{
int i;
unsigned int cause = read_c0_cause();
unsigned int exc = (cause >> 2) & 0x1f;
rt_kprintf("exception happens, epc: 0x%08x\n", regs->CP0EPC);
rt_kprintf(" cause: 0x%08x\n", regs->CP0Cause);
for (i = 0; i < 32; i++)
{
if (i % 4 == 0)
printf("\n");
printf("%8s %08x ", regstr[i], regs->regs[i]);
}
printf("\n");
list_thread();
rt_hw_cpu_shutdown();
}
static void install_default_exception_handler(void)
{
int i;
for (i=0; i<sizeof(sys_exception_handlers)/sizeof(sys_exception_handlers[0]); i++)
sys_exception_handlers[i] = (exception_func_t)unhandled_exception_handle;
sys_exception_handlers[EX_MOD] = mod_handler;
sys_exception_handlers[EX_TLBL] = tlbl_handler;
sys_exception_handlers[EX_TLBS] = tlbs_handler;
sys_exception_handlers[EX_ADEL] = adel_handler;
sys_exception_handlers[EX_ADES] = ades_handler;
sys_exception_handlers[EX_FPE] = fpe_handler;
}
int rt_hw_exception_init(void)
{
/* install the default exception handler */
install_default_exception_handler();
return RT_EOK;
}
/**
* setup the exception handle
*/
exception_func_t rt_set_except_vector(int n, exception_func_t func)
{
exception_func_t old_handler = sys_exception_handlers[n];
if ((n == 0) || (n > 32) || (!func))
{
return 0;
}
sys_exception_handlers[n] = func;
return old_handler;
}
void mips_exception_handler(mips_reg_ctx *ctx)
{
static int read_epc_count = 0;
static int epc_save = 0;
int i;
unsigned int epc;
//��� read_epc_count>0 ˵�� c_except_handler �ڶ� epc ʱ�����ˣ����� epc ������һ���µ��쳣
if (read_epc_count > 0)
{
printf("ERROR: read epc fail when except handle\n");
epc = epc_save;
read_epc_count = 0;
}
else
{
read_epc_count++;
epc_save = 0;
epc = read_c0_epc();
epc_save = epc;
if (epc != 0)
{
printf("-----------------------------------------------------\n");
for (i = 0; i < 4; i++)
{
printf("%08x:\t%08x\n",
(epc - 4 * 4 + i * 4),
*(unsigned int *) ((epc - 4 * 4 + i * 4) | 0xa0000000));
}
for (i = 0; i < 4; i++)
{
printf("%08x:\t%08x\n",
(epc + i * 4),
*(unsigned int *) ((epc + i * 4) | 0xa0000000));
}
printf("-----------------------------------------------------\n");
}
read_epc_count--;
}
printf("-----------------------------------------------------\n");
unsigned int cause = read_c0_cause();
unsigned int exc = (cause >> 2) & 0x1f;
printf("CAUSE=%08x --> %s\n", cause, cause_strings[exc]);
printf("EPC=%08x\n", epc);
for (i = 0; i < 32; i++)
{
if ((i != 0) && (i % 4 == 0))
printf("\n");
printf("%8s %08x ", regstr[i], ctx->regs[i]);
}
printf("\n-----------------------------------------------------\n");
printf("%s: \t %8x\n","CP0Status ", ctx->CP0Status);
printf("%s: \t %8x\n","CP0DataHI ", ctx->CP0DataHI);
printf("%s: \t %8x\n","CP0DataLO ", ctx->CP0DataLO);
printf("%s: \t %8x\n","CP0BadVAddr", ctx->CP0BadVAddr);
printf("%s: \t %8x\n","CP0Cause ", ctx->CP0Cause);
printf("%s: \t %8x\n","CP0EPC ", ctx->CP0EPC);
printf("-----------------------------------------------------\n");
#if 0
switch (exc)
{
case EX_MOD:
/* TLB modified */
break;
case EX_TLBL: /* TLB exc(load or ifetch) */
case EX_TLBS: /* TLB exception (store) */
break;
case EX_ADEL: /* Address err(load or ifetch) */
case EX_ADES: /* Address error (store) */
break;
case EX_IBE: /* Instruction Bus Error */
case EX_DBE: /* Data Bus Error */
break;
case EX_SYS: /* Syscall */
break;
case EX_BP: /* Breakpoint */
case EX_TR: /* Trap instruction */
break;
case EX_RI: /* Reserved instruction */
break;
case EX_FPE: /* floating point exception */
break;
case EX_CPU: /* CoProcessor Unusable */
break;
case EX_OV: /* OVerflow */
case EX_C2E: /* COP2 exception */
case EX_MDMX: /* MDMX exception */
case EX_WATCH: /* Watch exception */
case EX_MCHECK: /* Machine check exception */
case EX_CacheErr: /* Cache error caused re-entry */
/* to Debug Mode */
break;
default:
rt_kprintf("Unknow exception: %d\r\n", exc);
break;
}
#else
sys_exception_handlers[exc](ctx);
#endif
rt_hw_cpu_shutdown();
}
void mips_cache_error_handler (unsigned int Addr)
{
rt_kprintf("cache exception happens, epc: 0x%08x\n", read_c0_epc());
list_thread();
rt_hw_cpu_shutdown();
}
void mips_tlb_refill_handler(void)
{
rt_kprintf("tlb-miss happens, epc: 0x%08x\n", read_c0_epc());
rt_kprintf(" cause: 0x%08x\n", read_c0_cause());
list_thread();
rt_kprintf("current thread: %s\n", rt_thread_self()->name);
rt_hw_cpu_shutdown();
}
|