-
Notifications
You must be signed in to change notification settings - Fork 256
/
ioptrap.c
285 lines (247 loc) · 6.97 KB
/
ioptrap.c
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
/*
# _____ ___ ____ ___ ____
# ____| | ____| | | |____|
# | ___| |____ ___| ____| | \ PS2DEV Open Source Project.
#-----------------------------------------------------------------------
# Copyright 2001-2004, ps2dev - http://www.ps2dev.org
# Licenced under Academic Free License version 2.0
# Review ps2sdk README & LICENSE files for further details.
#
# $Id: ioptrap.c 1619 2009-10-07 04:54:58Z Herben $
# IOP exception handling.
*/
#include "types.h"
#include "defs.h"
#include "irx.h"
#include "ioptrap.h"
#include "excepman.h"
#include "loadcore.h"
#include "intrman.h"
#include "sifman.h"
#include "stdio.h"
#include "sysclib.h"
#include "thbase.h"
IRX_ID("ioptrap_driver", 1, 1);
extern struct irx_export_table _exp_ioptrap;
static const char *exception_type_name[] = {
"Interrupt",
"TLB Modification",
"TLB Miss Load",
"TLB Miss Store",
"Address Error Load",
"Address Error Store",
"Instruction Bus Error",
"Data Bus Error",
"Syscall",
"Breakpoint",
"Reserved Instruction",
"Coprocessor Unusable",
"Overflow",
"Reserved 13",
"Reserved 14" /* "Floating point exception" */
};
const char *get_exception_name(exception_type_t type)
{
return exception_type_name[type];
}
exception_frame_t __trap_frame;
extern void def_exc_handler();
extern void bp_exc_handler();
int dbg_jmp_buf_setup = 0;
jmp_buf dbg_jmp_buf;
exception_type_t dbg_setjmp()
{
int v;
if (0 == (v = setjmp(dbg_jmp_buf))) {
dbg_jmp_buf_setup = 1;
} else {
dbg_jmp_buf_setup = 0;
}
return v;
}
// from ps2link
/* Module info entry. */
typedef struct _smod_mod_info
{
struct _smod_mod_info *next;
u8 *name;
u16 version;
u16 newflags; /* For modload shipped with games. */
u16 id;
u16 flags; /* I believe this is where flags are kept for BIOS versions. */
u32 entry; /* _start */
u32 gp;
u32 text_start;
u32 text_size;
u32 data_size;
u32 bss_size;
u32 unused1;
u32 unused2;
} smod_mod_info_t;
smod_mod_info_t *smod_get_next_mod(smod_mod_info_t *cur_mod)
{
/* If cur_mod is 0, return the head of the list (IOP address 0x800). */
if (!cur_mod) {
return (smod_mod_info_t *)0x800;
} else {
if (!cur_mod->next)
return 0;
else
return cur_mod->next;
}
return 0;
}
char *ExceptionGetModuleName(u32 epc, u32 *r_epc)
{
smod_mod_info_t *mod_info = 0;
while ((mod_info = smod_get_next_mod(mod_info)) != 0) {
if ((epc >= mod_info->text_start) && (epc <= (mod_info->text_start + mod_info->text_size))) {
if (r_epc)
*r_epc = epc - mod_info->text_start;
return mod_info->name;
}
}
return 0;
}
#define JUMP_BUF_PC 0
#define JUMP_BUF_SP 1
#define JUMP_BUF_FP 2
#define JUMP_BUF_S0 3
#define JUMP_BUF_S1 4
#define JUMP_BUF_S2 5
#define JUMP_BUF_S3 6
#define JUMP_BUF_S4 7
#define JUMP_BUF_S5 8
#define JUMP_BUF_S6 9
#define JUMP_BUF_S7 10
#define JUMP_BUF_GP 11
// Define this to something else if you want... maybe some ee_sio stuff :P
#define TRAP_PRINTF(args...) printf(args)
static trap_exception_handler_t handlers[16];
trap_exception_handler_t set_exception_handler(exception_type_t type, trap_exception_handler_t handler)
{
trap_exception_handler_t old_handler = handlers[type];
handlers[type] = handler;
return old_handler;
}
trap_exception_handler_t get_exception_handler(exception_type_t type)
{
return handlers[type];
}
void trap(exception_type_t type, struct exception_frame *ex)
{
u32 i, r_addr;
char *module;
if (dbg_jmp_buf_setup) {
u32 *p = (u32 *)dbg_jmp_buf;
/* simulate longjmp */
ex->epc = p[JUMP_BUF_PC];
ex->regs[29] = p[JUMP_BUF_SP];
ex->regs[30] = p[JUMP_BUF_FP];
ex->regs[16] = p[JUMP_BUF_S0];
ex->regs[17] = p[JUMP_BUF_S1];
ex->regs[18] = p[JUMP_BUF_S2];
ex->regs[19] = p[JUMP_BUF_S3];
ex->regs[20] = p[JUMP_BUF_S4];
ex->regs[21] = p[JUMP_BUF_S5];
ex->regs[22] = p[JUMP_BUF_S6];
ex->regs[23] = p[JUMP_BUF_S7];
ex->regs[28] = p[JUMP_BUF_GP];
ex->regs[2] = type; /* return value from setjmp */
return;
}
TRAP_PRINTF("IOP Exception : %s\n", exception_type_name[type]);
TRAP_PRINTF("EPC=%08x CAUSE=%08x SR=%08x BADVADDR=%08x DCIC=%08x\n", (int)ex->epc, (int)ex->cause, (int)ex->sr, (int)ex->badvaddr, (int)ex->dcic);
if ((module = ExceptionGetModuleName(ex->epc, &r_addr)))
TRAP_PRINTF("module %s at unreloc offset %08lX\n", module, r_addr);
if ((module = ExceptionGetModuleName(ex->regs[31], &r_addr)))
TRAP_PRINTF("ra module %s at unreloc offset %08lX\n", module, r_addr);
for (i = 0; i != 32; i += 4) {
TRAP_PRINTF("r[%02d]=%08x r[%02d]=%08x r[%02d]=%08x r[%02d]=%08x\n",
(int)i, (int)ex->regs[i], (int)i + 1, (int)ex->regs[i + 1], (int)i + 2, (int)ex->regs[i + 2], (int)i + 3, (int)ex->regs[i + 3]);
}
if (handlers[type]) {
handlers[type](type, ex);
}
if (type == EXCEPTION_Bp) {
ex->dcic = 0;
} else {
if (ex->cause & (1 << 31)) {
ex->cause &= ~(1 << 31); /* clear BD */
} else {
ex->epc += 4;
}
}
}
#ifdef TEST_TRAP
static void trigger()
{
u32 v;
printf("trigger=%p\n", trigger);
printf("badaddr\n");
if (0 == (v = setjmp(dbg_jmp_buf))) {
dbg_jmp_buf_setup = 1;
*(u32 *)0xdeadbeef = 0xfeedface;
} else {
printf("exception occurred in command, v=%08x\n", (int)v);
}
dbg_jmp_buf_setup = 0;
printf("done.\n");
}
void main_thread(void *unused)
{
int i = 0;
DelayThread(1000 * 1000);
printf("IOP: about to trap!\n");
trigger();
printf("IOP: back from trap!\n");
while (1) {
printf("IOP: still running %d\n", i++);
DelayThread(2000 * 1000);
if (i == 10)
break;
}
ExitDeleteThread();
}
int create_main_thread(void)
{
iop_thread_t thread;
thread.attr = 0x2000000;
thread.option = 0;
thread.thread = main_thread;
thread.stacksize = 0x8000;
thread.priority = 0x18;
return CreateThread(&thread);
}
void do_tests()
{
int thid;
thid = create_main_thread();
StartThread(thid, NULL);
}
#else
#define do_tests()
#endif
int _start(int argc, char **argv)
{
int rv;
if (RegisterLibraryEntries(&_exp_ioptrap) != 0)
return 1;
memset(handlers, 0, sizeof(trap_exception_handler_t) * 16);
printf("ioptrap starts.\n");
if ((rv = RegisterDefaultExceptionHandler(def_exc_handler)) < 0) {
printf("RegisterDefaultExceptionHandler failed, rv=%d\n", rv);
return 1;
}
if ((rv = RegisterPriorityExceptionHandler(IOP_EXCEPTION_HDB, 0, bp_exc_handler)) < 0) {
// shouldn't we release the default exception handler here... ?
printf("RegisterDefaultExceptionHandler failed, rv=%d\n", rv);
return 1;
}
do_tests();
return 0;
}
int shutdown()
{
return 0;
}