fault.c
9.35 KB
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
/**************************************************************************
* *
* Copyright (C) 1995, Silicon Graphics, Inc. *
* *
* These coded instructions, statements, and computer programs contain *
* unpublished proprietary information of Silicon Graphics, Inc., and *
* are protected by Federal copyright law. They may not be disclosed *
* to third parties or copied or duplicated in any form, in whole or *
* in part, without the prior written consent of Silicon Graphics, Inc. *
* *
**************************************************************************/
/*---------------------------------------------------------------------*
Copyright (C) 1998 Nintendo. (Originated by SGI)
$RCSfile: fault.c,v $
$Revision: 1.1.1.1 $
$Date: 2002/05/02 03:27:31 $
*---------------------------------------------------------------------*/
#include "ultra64.h"
#include "os_internal.h"
#include "fault.h"
#define PRINTF osSyncPrintf
#define MSG_FAULT 0x10
/*
* Thread and stack structures
*/
char bootStack[STACKSIZE];
static OSThread idleThread;
static char idleThreadStack[STACKSIZE];
static OSThread mainThread;
static char mainThreadStack[STACKSIZE];
static OSThread faultThread;
static char faultThreadStack[STACKSIZE];
static OSThread user1Thread;
static char user1ThreadStack[STACKSIZE];
static OSThread user2Thread;
static char user2ThreadStack[STACKSIZE];
static OSThread user3Thread;
static char user3ThreadStack[STACKSIZE];
/*
* Message queues and message buffers used by this app
*/
static OSMesg PiMessages[DMA_QUEUE_SIZE];
static OSMesgQueue PiMessageQ;
static OSMesgQueue faultMsgQ;
static OSMesg faultMsgBuf;
static OSMesgQueue user2MsgQ;
static OSMesg user2MsgBuf;
static OSMesgQueue user3MsgQ;
static OSMesg user3MsgBuf;
/*
* Local variables and routines
*/
static int debugger = 0;
static void idleproc(char *);
static void mainproc(char *);
static void faultproc(char *);
static void user1proc(char *);
static void user2proc(char *);
static void user3proc(char *);
/*
* Extern declarations
*/
extern int printFaultData(OSThread *);
/*
* This program is an example of how to catch various CPU exceptions such
* as TLB on read/write. This program uses osSyncPrintf for printing to
* the debug port.
* exceptions, stops all user threads, and dumps out the registers of the
* faulted thread.
*
* The program flow is as follows:
* - Boot code initializes OS and starts idle thread
* - Idle thread gains control and starts PI manager
* - PI manager gains control and blocks to listen to PI requests
* - Idle thread regains control, starts main thread, resets its
* priority to 0
* - Main thread gains control and starts user thread #1
* - User thread #1 gains control and causes an immediate fault
* - Fault handling thread receives fault message, prints all regs
* of faulted thread, and returns to listen for other fault messages
* - Main thread regains control and starts user thread #2
* - User thread #2 gains control and blocks in listening for a message
* in its queue
* - Main thread regains control and starts user thread #3
* - User thread #3 gains control and blocks in listening for a message
* in its queue
* - Main thread regains control and sends a message to user thread #2
* - User thread #2 gains control and causes an immediate fault
* - Fault handling thread receives fault message, prints all regs
* of faulted thread, and returns to listen for other fault messages
* - Main thread regains control and sends a message to user thread #3
* - User thread #3 gains control and causes an immediate fault
* - Fault handling thread receives fault message, prints all regs
* of faulted thread, and returns to listen for other fault messages
* - Main thread regains control and prints all the faulted threads
* - And that's all folks!
*/
boot(void)
{
osInitialize();
osCreateThread(&idleThread, 1, (void(*)(void *))idleproc, (void *)0,
idleThreadStack+STACKSIZE, 8);
osStartThread(&idleThread);
}
static void
idleproc(char *argv) /* priority 8 */
{
/*
* Start PI Mgr for access to cartridge - start before the debugger
*/
osCreatePiManager((OSPri) OS_PRIORITY_PIMGR, &PiMessageQ, PiMessages,
DMA_QUEUE_SIZE);
/*
* The main thread's priority must be the same or lower than the original
* idle's thread priority. This allows the idle thread to change its
* priority to 0 before the main thread starts execution.
*/
osCreateThread(&mainThread, 3, (void(*)(void *))mainproc, argv,
mainThreadStack+STACKSIZE/8, (OSPri)7);
osStartThread(&mainThread);
osSetThreadPri(0, OS_PRIORITY_IDLE);
for(;;); /* idle thread */
}
static void
mainproc(char *argv) /* priority of 7 */
{
int i;
OSThread *curr, *last;
PRINTF("\n=> mainproc...\n");
/*
* Create message queue and thread structures for fault handling thread
*/
osCreateMesgQueue(&faultMsgQ, &faultMsgBuf, 1);
osCreateThread(&faultThread, 5, (void(*)(void *))faultproc, argv,
faultThreadStack+STACKSIZE/8, (OSPri)50);
/*
* Create message queue and thread structures for user thread #3
*/
osCreateMesgQueue(&user3MsgQ, &user3MsgBuf, 1);
osCreateThread(&user3Thread, 8, (void(*)(void *))user3proc, argv,
user3ThreadStack+STACKSIZE/8, (OSPri)14);
/*
* Create message queue and thread structures for user thread #2
*/
osCreateMesgQueue(&user2MsgQ, &user2MsgBuf, 1);
osCreateThread(&user2Thread, 7, (void(*)(void *))user2proc, argv,
user2ThreadStack+STACKSIZE/8, (OSPri)12);
/*
* Create thread structure for user thread #1
*/
osCreateThread(&user1Thread, 6, (void(*)(void *))user1proc, argv,
user1ThreadStack+STACKSIZE/8, (OSPri)10);
PRINTF("\n=> mainproc - starting fault thread...\n");
osStartThread(&faultThread);
PRINTF("\n=> mainproc - starting user1 thread...\n");
/* this causes an immediate crash */
osStartThread(&user1Thread);
PRINTF("\n=> mainproc - starting user2 thread...\n");
osStartThread(&user2Thread);
PRINTF("\n=> mainproc - starting user3 thread...\n");
osStartThread(&user3Thread);
PRINTF("\n=> mainproc - sending msg to user2 thread...\n");
/* this causes an immediate crash */
(void) osSendMesg(&user2MsgQ, (OSMesg)0x02, OS_MESG_BLOCK);
PRINTF("\n=> mainproc - sending msg to user3 thread...\n");
/* this causes an immediate crash */
(void) osSendMesg(&user3MsgQ, (OSMesg)0x03, OS_MESG_BLOCK);
PRINTF("\n=> mainproc - printing out all faulted threads...\n");
curr = last = (OSThread *)NULL;
i = 1;
while (1) {
/*
* This routine returns the next faulted thread from the active
* thread list. It uses the thread argument as the starting point of
* the search: if NULL, starts from the beginning of the list.
* The routine returns NULL if it can't find any faulted thread.
*/
curr = __osGetNextFaultedThread(last);
if (curr) {
last = curr;
PRINTF("\t%3d) Thread: addr=0x%08x, id=%2d, priority=%3d\n",
i, curr, curr->id, curr->priority);
i++;
}
else
break;
}
PRINTF("\n=> THE END\n");
}
/*
* Fault handler: simply waits for the fault message, gets the current
* faulted thread, and prints out all the registers of this thread.
*/
static void
faultproc(char *argv)
{
OSMesg msg;
static OSThread *curr, *last;
osSetEventMesg(OS_EVENT_FAULT, &faultMsgQ, (OSMesg)MSG_FAULT);
last = (OSThread *)NULL;
while (1) {
PRINTF("\n=> faultproc - waiting for message...\n");
(void) osRecvMesg(&faultMsgQ, (OSMesg *)&msg, OS_MESG_BLOCK);
PRINTF("\n=> faultproc - got a fault message...\n");
/* This routine returns the most recent faulted thread */
curr = __osGetCurrFaultedThread();
if (curr) {
printFaultData(curr);
}
}
}
static void
user1proc(char *argv)
{
long e1;
PRINTF("\n=> user1proc - we're gonna crash!\n");
e1 = *(long *)1; /* TLB exception on load/instruction fetch */
}
static void
user2proc(char *argv)
{
OSMesg msg;
while (1) {
PRINTF("\n=> user2proc - waiting for message...\n");
(void) osRecvMesg(&user2MsgQ, (OSMesg *)&msg, OS_MESG_BLOCK);
PRINTF("\n=> user2proc - we're gonna crash!\n");
*(long *)2 = 2; /* TLB exception on store */
}
}
static void
user3proc(char *argv)
{
OSMesg msg;
u32 fpstat;
float f0;
/*
* Fetch the current floating-point control/status register
* Enable divide-by-zero exception for floating point
*/
fpstat = __osGetFpcCsr();
__osSetFpcCsr(fpstat|FPCSR_EZ);
while (1) {
PRINTF("\n=> user3proc - waiting for message...\n");
(void) osRecvMesg(&user3MsgQ, (OSMesg *)&msg, OS_MESG_BLOCK);
PRINTF("\n=> user3proc - we're gonna crash!\n");
f0 = 1.0/0; /* Floating-point exception: divide-by-zero */
}
}