rmonbrk.c 22.8 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 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 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669
/*
 * ==========================================================================
 * Copyright (c) 1994, Silicon Graphics, Inc.  All Rights Reserved.
 *
 * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
 * the contents of this file may not be disclosed to third parties, copied
 * or duplicated in any form, in whole or in part, without the prior written
 * permission of Silicon Graphics, Inc. handleSelectExceptions
 *
 * RESTRICTED RIGHTS LEGEND:
 * Use, duplication or disclosure by the Government is subject to restrictions
 * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
 * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
 * successor clauses in the FAR, DOD or NASA FAR Supplement.  Unpublished
 * rights reserved under the Copyright Laws of the United States.
 * ==========================================================================
 */

/************************************************************************
Program: rmon (reality monitor) in-circuit target monitor
File: break.c
Author: Kenneth F. Greenberg
Purpose: The breakpoint access functions
Revision history:
	94.10.24  Original
************************************************************************/

#ifndef _FINALROM

#include "rmonint.h"
#include "rcp.h"

typedef struct {
	TVushort type;
	TVushort response;
	TVid threadID;
	void* pc;
} TVExceptionReplyMsg;

#define NBREAKS 16	/* number of breakpoints in this implementation */
#define TV_MSG_EXCEPTION        0x8004

/*
	The following macro defines a breakpoint instruction with a
	built in breakpoint number. Thus, we always know which
	breakpoint was hit as soon as the break occurs, saving the
	time and trouble of actually looking it up. We add 16 to the
	break number to stay away from compiler-defined breaks and
	user set breaks.
*/

#define breakinst(n) (0x0d | (((n+16) & 0xfffff) << 6))

/*
	The following structure holds information about active breakpoints.
	The 0th entry is the temproary breakpoint, inserted when the user
	specifies "run to location" and removed when ANY breakpoint is
	encountered. There are also (NBREAKS - 1) persistent breakpoints.
	These must be manually set and cleared. On encountering one, it
	remains set, although the original contents of memory may be
	requested from time to time.

	An alternate breakpoint is used for single stepping the CPU.
	Since we may step over a conditional branch, we don't know which
	way the branch will go. Thus, we set breakpoints at both locations
	and run until either is hit. This means that sometimes we need two
	breaks to single step, in which case we use break 0 and break alt.

	The RSP breakpoints use the same structure. Since single step
	is implemented in hardware, we don't need the temp breaks.
*/

typedef struct {
	u32 * breakAddress;
	u32 oldInstruction;
} BREAKINFO;

static BREAKINFO breakpoints[NBREAKS];		/* the CPU breaks */
static BREAKINFO altBreak;			/* the other temp break */
static BREAKINFO RCPbreakpoints[NBREAKS];	/* the RCP breaks */

u8 __rmonRcpAtBreak;

static void rmonFindFaultedThreads( void );

/************************************************************************
Function: (static) SetTempBreakpoint
Args: 	u32 * addr1 - the address where the breakpoint is to be set
	u32 * addr2 - the address of the optional second location
Type: void
Purpose: This function sets a temporary breakpoint. The temporary
	breakpoint will be removed when ANY breakpoint is encountered.
	Setting the temporary breakpoint is not a response to a user
	set break command, but is a side effect of other commands like
	step over line. The debugger sets a breakpoint at the location
	corresponding to the next source line, runs the application, 
	and removes the breakpoint as soon as it is hit. This command
	is also used to implement single-step, since the MIPS CPU has 
	no trace bit. If single stepping over a conditional jump, then
	we need two breakpoints (one at each target).
************************************************************************/
static void SetTempBreakpoint( u32 * addr1, u32 * addr2 )
{
	Debug( "Set temp BP at %08x", addr1 );
	if ( addr2 )
		Debug( " and %08x", addr2 );
	Debug( "\n" );

	breakpoints[0].oldInstruction = *addr1;
	*addr1 = breakinst( 0 );
	osWritebackDCache( addr1, 4 );
	osInvalICache( addr1, 4 );
	breakpoints[0].breakAddress = addr1;
	if ( addr2 )
	{
		altBreak.oldInstruction = *addr2;
		*addr2 = breakinst( 0 );
		osWritebackDCache( addr2, 4 );
		osInvalICache( addr2, 4 );
		altBreak.breakAddress = addr2;
	}
}

/************************************************************************
Function: (static) ClearTempBreakpoint
Args: none
Type: void
Purpose: This function restores the contents of memory where the
	temporary breakpoint was set. Note that the breakinfo does not
	really need to be cleared. This function should be called by
	the break ISR when any breakpoint is encountered. Since the
	alternate break may be set from a single step of a branch,
	we clear it too if it was set.
************************************************************************/
static void ClearTempBreakpoint( void )
{
	u32 inst;

	if ( breakpoints[0].breakAddress )
	{
		Debug( "ClearTempBreak @ %08x\n", breakpoints[0].breakAddress );
		/* Check to make sure one was really there */

		inst = *breakpoints[0].breakAddress;
		if ( (inst & BREAKMASK) == 0xd )
		{
			*breakpoints[0].breakAddress = breakpoints[0].oldInstruction;
			osWritebackDCache( breakpoints[0].breakAddress, 4 );
			osInvalICache( breakpoints[0].breakAddress, 4 );
		}
		breakpoints[0].breakAddress = 0;
	}
	if ( altBreak.breakAddress )
	{
		Debug( "ClearTempBreak @ %08x\n", altBreak.breakAddress );
		inst = *altBreak.breakAddress;
		if ( (inst & BREAKMASK) == 0xd )
		{
			*altBreak.breakAddress = altBreak.oldInstruction;
			osWritebackDCache( altBreak.breakAddress, 4 );
			osInvalICache( altBreak.breakAddress, 4 );
		}
		altBreak.breakAddress = 0;
	}
}

/************************************************************************
Function: __rmonSetBreak
Args: KKHeader * request - address of the standard protocol structure for
	setting breakpoints disguised as a header.
Type: int - returns 0 if successful, otherwise error type
Purpose: This function sets a permanent breakpoint. The permanent
	breakpoint will remain set until explicitly cleared.
ToDo:	See if there is already a break at this address.
************************************************************************/
int __rmonSetBreak( KKHeader * req )
{
	register KKSetBkptRequest * request = (KKSetBkptRequest *) req;
	register BREAKINFO * breakBase, * whichBreak, * lastBreak;
	KKBkptEvent reply;

	/* Find a vacant slot in the breakinfo table */

	Debug( "SetBreak at %08x, method %d\n", request->addr, req->method );
	if ( req->method == METHOD_RSP )
	{
		breakBase = RCPbreakpoints;
		whichBreak = &RCPbreakpoints[1];
		lastBreak = &RCPbreakpoints[NBREAKS];
	}
	else
	{
		breakBase = breakpoints;
		whichBreak = &breakpoints[1];
		lastBreak = &breakpoints[NBREAKS];
	}
	for ( ; whichBreak < lastBreak; ++whichBreak )
		if ( whichBreak->breakAddress )
		{
			/* check for already set */
			if ( (u32) whichBreak->breakAddress == request->addr )
				break;
			continue;
		}
		else
			break;

	/*
		When we get here, either we ran out of slots in the
		breakpoint table, we found a breakpoint already set
		at this location, or we found an empty slot for a
		new breakpoint to go. See which.
	*/

	if ( whichBreak == lastBreak )	/* all used up */
		return TV_ERROR_NO_MORE_IDS;
	if ( !whichBreak->breakAddress )	/* need to set it */
	{
		if ( req->method == METHOD_RSP )
		{
			whichBreak->oldInstruction = __rmonReadWordAt( (u32 *) request->addr );
			__rmonWriteWordTo( (u32 *) request->addr, breakinst( whichBreak - breakBase ) );
		}
		else
		{
			whichBreak->oldInstruction = * (u32 *) request->addr;
			*(u32 *) request->addr = breakinst( whichBreak - breakBase );
			osWritebackDCache( (void *) request->addr, 4 );
			osInvalICache( (void *) request->addr, 4 );
			Debug( "* (%08x) = %08x (was %08x)\n", request->addr, 
				breakinst( whichBreak - breakBase ),
				whichBreak->oldInstruction );
		}
		whichBreak->breakAddress = (u32 *) request->addr;
	}

	reply.header.code = request->header.code;
	reply.header.error = TV_ERROR_NO_ERROR;
	reply.object = request->object;
	reply.bp = whichBreak - breakBase;
	reply.instruction = whichBreak->oldInstruction;
	__rmonSendReply( (KKHeader * const) &reply, sizeof( KKBkptEvent ), _KK_REPLY);	
	return 0;
}

/************************************************************************
Function: rmonListBreak
Args: KKHeader * request - address of the standard protocol structure for
	listing breakpoints disguised as a header.
Type: int (returns a status)
Purpose: This function implements the list breakpoints command. In
	TeleShop, this returns the list of breakpoints for an object. 
	We are not sure what it should do here.
************************************************************************/
int __rmonListBreak( KKHeader * request )
{
	Debug( "ListBreak\n" );
	return TV_ERROR_ILLEGAL_CALL;
}

/************************************************************************
Function: __rmonClearBreak
Args: KKHeader * req - address of the standard protocol structure for
	clearing breakpoints disguised as a header.
Type: int - returns 0 if successful, otherwise error type
Purpose: This function restores the contents of memory where a
	permanent breakpoint was set.
Returns:
	0 on success
	E_NOTCPU if request is to clear a break in some other process
	E_CONFIG if the break number is too high for this implementation
	E_BADARG if the specified break is legal but not set
************************************************************************/
int __rmonClearBreak( KKHeader * req )
{
	register KKClrBkptRequest * request = (KKClrBkptRequest *) req;
	register BREAKINFO * whichBreak;
	KKBkptEvent reply;
	u32 inst;

	Debug( "ClearBreak\n" );
	if ( request->bp >= NBREAKS )
		return TV_ERROR_INVALID_ID;
	if ( req->method == METHOD_RSP )
	{
		whichBreak = &RCPbreakpoints[ request->bp ];
		if ( !whichBreak->breakAddress )
			return TV_ERROR_INVALID_ID;	/* break not set */
		inst = __rmonReadWordAt( whichBreak->breakAddress );
		if ( (inst & BREAKMASK) == 0xd )
			__rmonWriteWordTo( whichBreak->breakAddress,
				whichBreak->oldInstruction );
	}
	else
	{
		whichBreak = &breakpoints[ request->bp ];
		if ( !whichBreak->breakAddress )
			return TV_ERROR_INVALID_ID;	/* break not set */
		inst = *whichBreak->breakAddress;
		if ( (inst & BREAKMASK) == 0xd )
		{
			*whichBreak->breakAddress = whichBreak->oldInstruction;
			osWritebackDCache( whichBreak->breakAddress, 4 );
			osInvalICache( whichBreak->breakAddress, 4 );
		}
	}
	whichBreak->breakAddress = 0;

	reply.header.code = request->header.code;
	reply.header.error = TV_ERROR_NO_ERROR;
	reply.object = request->object;
	reply.bp = request->bp;
	__rmonSendReply( (KKHeader * const) &reply, sizeof( KKBkptEvent ), _KK_REPLY );	
	return 0;
}

/************************************************************************
Function: __rmonGetBranchTarget
Args:
	int method - which processor is of interest
	int thread - which thread is of interest
	u32 * addr - address of an instruction to analyze
Type: u32 - The target address of the branch, or -1 if none
Purpose: This function examines an instruction and (if it is some kind of 
	jump or branch) returns the target address from register, calculated
	relative address, or whatever.
************************************************************************/
u32 __rmonGetBranchTarget( int method, int thread, char * addr )
{
	int inst;

	if ( method == METHOD_RSP )
		inst = __rmonReadWordAt( (u32 *) addr );
	else
		inst = *(int *) addr;

	switch ((inst >> 26) & 0x3f) 
	{
	case 0x0:		/* jump register */
		if ((((inst >> 5) & 0x7fff) == 0) &&
			((inst & 0x3f) == 8))
				return __rmonGetRegisterContents( method, thread,
					(inst>>21) & 0x1f );

			/* jump and link register */
		if ((((inst >> 16) & 0x1f) == 0) && ((inst & 0x7ff) == 9))
			return __rmonGetRegisterContents( method, thread, (inst>>21) & 0x1f );
		break;

	case 0x1:
		switch ((inst >> 16) & 0x1f) 
		{
		case 0:		/* branch on less than zero */
		case 1:		/* branch on greater than or equal to zero */
		case 2:		/* branch on less than zero likely */
		case 3:		/* branch on greater than or equal to zero likely */
		case 0x10:	/* branch on less than zero and link */
		case 0x11:	/* branch on greater than or equal to zero and link */
		case 0x12:	/* branch on less than zero and link likely */
		case 0x13: /* branch on greater than or equal to zero and link likely */
			return (u32) (((inst << 16) >> 14) + addr + 4);
		}
		break;
	
	case 0x2:		/* jump */
	case 0x3:		/* jump and link */
		return ((u32)(((unsigned int)(inst << 6) >> 4) +
			    ((((int) addr + 4) >> 28) << 28)));

	case 0x4:		/* branch on equal */
	case 0x5:		/* branch on not equal */
	case 0x14:		/* branch on equal likely */
	case 0x15:		/* branch on not equal likely */
		return (u32)(((inst << 16) >> 14) + addr + 4);

	case 0x6:		/* branch on less than or equal to zero */
	case 0x7:		/* branch on greater than zero */
	case 0x16:		/* branch on less than or equal to zero likely */
	case 0x17:		/* branch on greater than zero likely */
		if (((inst>>16) & 0x1f) == 0)
			return (u32)(((inst << 16) >> 14) + addr + 4);
		break;

	case 0x10:		/* branch on coprocessor instructions */
	case 0x11:
	case 0x12:
	case 0x13:
		if (((inst>>21) & 0x1f) == 8) 
		{	/* bc */
			switch ((inst>>16) & 0x1f)
			{
				case 0: 		/* bcf */
				case 2:		/* bcfl */
				case 1:		/* bct */
				case 3:		/* bctl */
					return (u32)(((inst << 16) >> 14) + addr + 4);
			}
		}
		break;
	}

    /*
     * XXX - didn't figure out the target, either is wasn't jump/branch
     * or it could be branch on coprocessor (not implemented yet)
     * since these had better be valid values for the program counter
     * returning anything with the lower two bits means it didn't work.
     */
	return (u32) -1;
}

/************************************************************************
Function: (static) IsJump
Args: u32 inst - an instruction to analyze
Type: int - returns boolean value
Purpose: This function examines an instruction and returns true if it is
	a jump, false otherwise
************************************************************************/
static int IsJump( u32 inst )
{
	switch ((inst >> 26) & 0x3f)
	{
	case 0x0:	/* jump register */
		if ((((inst >> 5) & 0x7fff) == 0) && ((inst & 0x3f) == 8))
			return 1;

		/* jump and link register */
		if ((((inst >> 16) & 0x1f) == 0) && ((inst & 0x7ff) == 9))
			return 1;
		break;

	case 0x2:		/* jump */
	case 0x3:		/* jump and link */
		return 1;

	}
	return 0;
}

/************************************************************************
Function: __rmonSetSingleStep
Args:
	int thread - which thread is of interest
	u32 * instptr - current PC for thread
Type: int - returns 1 if OK, 0 otherwise
Purpose: This function sets a temporary breakpoint for single stepping, 
	since the MIPS CPU has no trace bit. It must also check to see if
	the instruction is a jump (in which case it must set the breakpoint
	at the jump target) or a conditional branch (in which case it must
	set TWO breakpoints). Otherwise, it just sets the breakpoint at
	the next instruction.
ToDo:	Check to see if it is a break instruction; if so, put the real
	instruction back, step over it, then put the breakpoint back.
************************************************************************/
int __rmonSetSingleStep( int thread, u32 * instptr )
{
	u32 branchTarget;

	Debug( "SingleStep\n" );
	/*
		Fetch the instruction and see what it is. Set breaks
		accordingly. WARNING! If you were not paying sufficient
		attention, you did not notice that the arg instptr
		is a pointer to INSTRUCTIONS here, not chars or void.
		Thus, incrementing it increases its numeric value by
		four, not by one. Just like real life, only better.

		We fetch the branch target, which will be invalid if
		the instruction is not a branch.
	*/
	
	branchTarget = __rmonGetBranchTarget( METHOD_NORMAL, thread, 
		(char *) instptr );
	if ( branchTarget & 3 )		/* then it wasn't a branch at all */
	{
		SetTempBreakpoint( instptr + 1, 0 );
	}

	/*
		OK, it was some kind of branch, either a jump or a
		conditional branch. If it is a jump, then just set
		the breakpoint at the target. If it was a conditional
		branch, set it at both possible targets. Of course,
		you can't really set it in both places if they are
		the same, so be careful about that possibility. Also,
		you can't set a breakpoint on a jump to yourself,
		because you wouldn't have an instruction to execute
		after you substituted the breakpoint instruction for
		the instruction you wanted to step through. Confusing,
		but true. For now, we just do nothing in that case.
	*/

	else if ( branchTarget == (u32) instptr )	/* uh-oh, can't do this */
		return 0;
	else if ( IsJump( *instptr ) || (branchTarget == (u32) (instptr+2)) )
		SetTempBreakpoint( (u32 *) branchTarget, 0 );
	else
		SetTempBreakpoint( (u32 *) branchTarget, instptr + 2 );
	return 1;
}

/************************************************************************
Function: __rmonGetExceptionStatus
Args: KKstatusEvent * reply - address of the standard protocol structure for
	replying to commands that need a status as a response.
Type: void
Purpose: This function fills in more or less standard information when
	rmon needs to send a status packet to the host in response
	to hitting a breakpoint. It is encapsulated as a separate 
	function for efficiency, since this code is used in more than one
	place. In addition to being called by HitBreak, it is also used
	by the single step routine. While CPU single steps are handled
	by HitBreak, RCP single steps are not done by setting breakpoints
	and running.
************************************************************************/
void __rmonGetExceptionStatus( KKStatusEvent * reply )
{
	reply->status.flags = KK_STOPPED;
	reply->status.why = KK_FAULTED;
	reply->status.what = 0;
	reply->status.rv = 0;
	reply->status.info.major = KK_FLTINSTR;
	reply->status.info.minor = KK_FLTTRACE;
	reply->header.code = _KK_REQ_STATUS;
	reply->header.error = TV_ERROR_NO_ERROR;
	reply->header.length = sizeof( KKStatusEvent );
}

/************************************************************************
Function: rmonSendBreakMessage
Args:
	s32 whichThread - tid of the thread that stopped
	int breakNumber - an indicator of which breakpoint was encountered.
Type: void
Purpose: This function is called whenever a breakpoint is hit. It sends
	a message to the debugger running on the host system, notifying it
	that a thread has now stopped.
************************************************************************/
static void rmonSendBreakMessage( s32 whichThread, int breakNumber )
{
	KKStatusEvent reply;

	Debug( "Break %d in thread %d\n", breakNumber, whichThread );
	__rmonGetThreadStatus( METHOD_NORMAL, whichThread ? whichThread : CPU_THREAD,
		&reply );
	__rmonGetExceptionStatus( &reply );
	if ( breakNumber == 15 )	/* really a fault */
	{
		reply.status.info.major = KK_FLTMEM;
		reply.status.info.minor = KK_FLTACCESS;
	}
	if ( breakNumber < 16 )		/* not ours */
		breakNumber = 0;
	else
		breakNumber -= 16;
	if ( breakNumber )
		reply.status.instr = 0x0d;
	
	__rmonSendReply( (KKHeader * const) &reply, sizeof( KKStatusEvent ), _KK_EXCEPTION);

}

/************************************************************************
Function: __rmonHitBreak
Args: int breakNumber - an indicator of which breakpoint was encountered.
Type: void
Purpose: This function is called whenever a breakpoint is hit. It clears
	all temporary breakpoints, creates an exception packet for the
	debugger, and sends it to the host.
************************************************************************/
void __rmonHitBreak( void )
{
	Debug( "HitBreak\n" );
	ClearTempBreakpoint();
	__rmonStopUserThreads( 0 );
	rmonFindFaultedThreads();
}

/************************************************************************
Function: __rmonHitSpBreak
Args: int breakNumber - an indicator of which breakpoint was encountered.
Type: void
Purpose: This function is called whenever a breakpoint is hit. It clears
	all temporary breakpoints, creates an exception packet for the
	debugger, and sends it to the host.
************************************************************************/
void __rmonHitSpBreak( void )
{
	KKStatusEvent exceptionReply;

	/*
		Unlike the R4300, the PC does not point to the instruction
		that faulted (i.e., the break) but points past it. To make
		the rest of this code work, we need to back it up. Note that
		backing it past zero is not really a problem since the
		register itself holds only 4K worth of bits.
	*/

	Debug( "Hit SP Break\n" );
	__rmonWriteWordTo( (u32 *)SP_PC_REG,
		__rmonReadWordAt( (u32 *)SP_PC_REG ) - 4 );
	__rmonGetThreadStatus( METHOD_RSP, RSP_THREAD, &exceptionReply );
	__rmonGetExceptionStatus( &exceptionReply );

	__rmonSendReply( (KKHeader * const) &exceptionReply,
		sizeof( KKStatusEvent ), _KK_EXCEPTION);

	__rmonRcpAtBreak = 1;
}

/************************************************************************
Function: __rmonHitCpuFault
Args: none
Type: void
Purpose: This function is called whenever the CPU faults. It kills all
	user threads, disables all interrupts except its own in the idle
	thread, creates an exception packet for the
	debugger, and sends it to the host.
************************************************************************/
void __rmonHitCpuFault( void )
{
	Debug( "HitCpuFault\n" );
	__rmonMaskIdleThreadInts();
	__rmonStopUserThreads( 0 );
	rmonFindFaultedThreads();	/* will send fault message */
}

/************************************************************************
Function: rmonFindFaultedThreads
Purpose: This function finds all threads that have faulted due to
	a breakpoint or otherwise, and sends a message to the host.
************************************************************************/
static void rmonFindFaultedThreads( void )
{
    register OSThread * tptr = __osGetActiveQueue();

    while ( tptr->priority != -1 )
    {
        if ( tptr->priority > OS_PRIORITY_IDLE && 
             tptr->priority <= OS_PRIORITY_APPMAX )
        {
            int inst;
            
            if ( tptr->flags & OS_FLAG_CPU_BREAK )
            {
                /* This thread hit a breakpoint */
                
                /* tptr->flags &= ~1;   we don't want to do this */
                inst = *(int*) tptr->context.pc;
                Debug( "Brk in thread %d @ %08x, inst %08x\r\n",
                       tptr->id, tptr->context.pc, inst );
                if ( (inst & BREAKMASK) == 0xd )
                    rmonSendBreakMessage( tptr->id, inst >> 6 );
                else    /* probably temp break */
                    rmonSendBreakMessage( tptr->id, 0 );
            }
            if ( tptr->flags & OS_FLAG_FAULT )
            {
                /* This thread faulted */
                
                __rmonSendFault( tptr );
                rmonSendBreakMessage( tptr->id, 15 );
            }
        }
        tptr = tptr->tlnext;
    }
}

#endif /* #ifndef _FINALROM */