28#define MODNAME "ioptrap_driver"
34static const char *exception_type_name[] = {
40 "Address Error Store",
41 "Instruction Bus Error",
45 "Reserved Instruction",
46 "Coprocessor Unusable",
52const char *get_exception_name(exception_type_t type)
54 return exception_type_name[type];
58extern void def_exc_handler();
59extern void bp_exc_handler();
61int dbg_jmp_buf_setup = 0;
64exception_type_t dbg_setjmp()
67 if (0 == (v = setjmp(dbg_jmp_buf))) {
68 dbg_jmp_buf_setup = 1;
70 dbg_jmp_buf_setup = 0;
75#ifdef IOPTRAP_PRINT_MODULE_NAME
107 return cur_mod->next;
112char *ExceptionGetModuleName(u32 epc, u32 *r_epc)
117 if ((epc >= mod_info->text_start) && (epc <= (mod_info->text_start + mod_info->text_size))) {
119 *r_epc = epc - mod_info->text_start;
121 return mod_info->
name;
139#define JUMP_BUF_S7 10
140#define JUMP_BUF_GP 11
143#define TRAP_PRINTF(args...) printf(MODNAME ": " args)
145static trap_exception_handler_t handlers[16];
147trap_exception_handler_t set_exception_handler(exception_type_t type, trap_exception_handler_t handler)
149 trap_exception_handler_t old_handler = handlers[type];
150 handlers[type] = handler;
154trap_exception_handler_t get_exception_handler(exception_type_t type)
156 return handlers[type];
162#ifdef IOPTRAP_PRINT_MODULE_NAME
166 if (dbg_jmp_buf_setup) {
167 u32 *p = (u32 *)dbg_jmp_buf;
169 ex->epc = p[JUMP_BUF_PC];
170 ex->regs[29] = p[JUMP_BUF_SP];
171 ex->regs[30] = p[JUMP_BUF_FP];
172 ex->regs[16] = p[JUMP_BUF_S0];
173 ex->regs[17] = p[JUMP_BUF_S1];
174 ex->regs[18] = p[JUMP_BUF_S2];
175 ex->regs[19] = p[JUMP_BUF_S3];
176 ex->regs[20] = p[JUMP_BUF_S4];
177 ex->regs[21] = p[JUMP_BUF_S5];
178 ex->regs[22] = p[JUMP_BUF_S6];
179 ex->regs[23] = p[JUMP_BUF_S7];
180 ex->regs[28] = p[JUMP_BUF_GP];
184 TRAP_PRINTF(
"IOP Exception : %s\n", exception_type_name[type]);
185 TRAP_PRINTF(
"EPC=%08x CAUSE=%08x SR=%08x BADVADDR=%08x DCIC=%08x\n", ex->epc, ex->cause, ex->sr, ex->badvaddr, ex->dcic);
186#ifdef IOPTRAP_PRINT_MODULE_NAME
187 if ((module = ExceptionGetModuleName(ex->epc, &r_addr)))
188 TRAP_PRINTF(
"module %s at unreloc offset %08lX\n", module, r_addr);
189 if ((module = ExceptionGetModuleName(ex->regs[31], &r_addr)))
190 TRAP_PRINTF(
"ra module %s at unreloc offset %08lX\n", module, r_addr);
192 for (i = 0; i != 32; i += 4) {
193 TRAP_PRINTF(
"r[%02d]=%08x r[%02d]=%08x r[%02d]=%08x r[%02d]=%08x",
194 i, ex->regs[i], i + 1, ex->regs[i + 1], i + 2, ex->regs[i + 2], i + 3, ex->regs[i + 3]);
197 if (handlers[type]) {
198 handlers[type](type, ex);
201 if (type == EXCEPTION_Bp) {
204 if (ex->cause & (((u32)1) << 31)) {
205 ex->cause &= ~(((u32)1) << 31);
216 printf(
"trigger=%p\n", trigger);
218 if (0 == (v = setjmp(dbg_jmp_buf))) {
219 dbg_jmp_buf_setup = 1;
220 *(u32 *)0xdeadbeef = 0xfeedface;
222 printf(
"exception occurred in command, v=%08x\n", v);
224 dbg_jmp_buf_setup = 0;
228void main_thread(
void *unused)
232 DelayThread(1000 * 1000);
234 printf(
"IOP: about to trap!\n");
236 printf(
"IOP: back from trap!\n");
238 printf(
"IOP: still running %d\n", i++);
239 DelayThread(2000 * 1000);
246int create_main_thread(
void)
252 thread.thread = main_thread;
253 thread.stacksize = 0x8000;
255 return CreateThread(&
thread);
261 thid = create_main_thread();
262 StartThread(thid, NULL);
268int _start(
int argc,
char *argv[])
275 if (RegisterLibraryEntries(&_exp_ioptrap) != 0)
276 return MODULE_NO_RESIDENT_END;
278 memset(handlers, 0,
sizeof(trap_exception_handler_t) * 16);
279 printf(
"ioptrap starts.\n");
281 printf(
"RegisterDefaultExceptionHandler failed, rv=%d\n", rv);
282 return MODULE_NO_RESIDENT_END;
286 printf(
"RegisterDefaultExceptionHandler failed, rv=%d\n", rv);
287 return MODULE_NO_RESIDENT_END;
290 return MODULE_RESIDENT_END;
#define IOP_EXCEPTION_HDB
struct _smod_mod_info smod_mod_info_t
int smod_get_next_mod(smod_mod_info_t *cur_mod, smod_mod_info_t *next_mod)