rmontask.c 14.3 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
/*
 * ==========================================================================
 * 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: regs.c
Author: Kenneth F. Greenberg
Purpose: The task access functions
Revision history:
	94.10.31  Original (Happy Halloween)
************************************************************************/
#ifndef _FINALROM

#include "dbgdefs.h"	/* definitions needed by the following file */
#include "dbgproto.h"	/* standard protocol definitions */
#include "rmonint.h"	/* needs info in above files */
#include "rcp.h"


/************************************************************************
Function: __rmonMaskIdleThreadInts
Args: none
Type: void
Purpose: This function is called when a fault occurs to change the idle
	thread interrupt mask to allow only debugger interrupts to get
	through.
************************************************************************/
void __rmonMaskIdleThreadInts( void )
{
	register OSThread * tptr = __osGetActiveQueue();

	while ( tptr->priority != -1 )
	{
		if ( tptr->priority == 0 )	/* found it */
		{
			tptr->context.sr &= ~OS_IM_CPU;
			tptr->context.sr |= (OS_IM_CART |
				OS_IM_RDBWRITE | OS_IM_RDBREAD);
			break;
		}
		tptr = tptr->tlnext;
	}
}

/************************************************************************
Function: __rmonGetTCB
Args: int threadNumber - which thread to get the TCB for
Type: OSThread * - returns pointer to the TCB or 0 if none
Purpose: This function translates a thread number into a thread control
	block address. This is primarily used when looking up the contents
	of one of the thread's registers.
************************************************************************/
OSThread * __rmonGetTCB( int threadNumber )
{
	register OSThread * tptr = __osGetActiveQueue();

	if ( threadNumber < 1 )
		return 0;
	while ( tptr->priority != -1 )
	{
		if ( tptr->id == threadNumber )
			return tptr;
		tptr = tptr->tlnext;
	}
	return 0;
}

/************************************************************************
Function: __rmonStopUserThreads
Args:
	int whichThread - the thread to stop, or 0 for all
Type: int - the number of the thread that stopped, or 0 if unknown
Purpose: This function stops one or more running threads. If a thread number
	is specified, then the function was called from the explicit stop
	thread command. If zero, then it was called in response to a break
	or fault being hit, which (for now) stops all threads from running. 
************************************************************************/
int __rmonStopUserThreads( int whichThread )
{
	register int whichOne = 0;
	register OSThread * tptr = __osGetActiveQueue();

	Debug( "StopThreads %d\n", whichThread );
	if ( whichThread )		/* just stop one */
	{
		while ( tptr->priority != -1 )
		{
			if ( tptr->id == whichThread )
				break;
			tptr = tptr->tlnext;
		}
		if ( tptr->priority == -1 )		/* couldn't find it */
			return 0;
		if ( tptr->priority > 0 && tptr->priority < 128 )
		{
			osStopThread( tptr );
			if ( tptr->state != OS_STATE_STOPPED )
				Debug( "Couldn't stop thread %d\n", whichThread );
			whichOne = whichThread;
		}
	}
	else
	{
		while ( tptr->priority != -1 )
		{
			if ( tptr->priority > 0 && tptr->priority < 128 )
			{
				osStopThread( tptr );
				if ( tptr->state != OS_STATE_STOPPED )
					Debug( "Couldn't stop thread %d\n", tptr->id );
				whichOne = -1;
			}
			tptr = tptr->tlnext;
		}
	}
	return whichOne;
}

/************************************************************************
Function: __rmonListThreads
Args: KKHeader * req - address of the standard protocol structure for
	listing target threads.
Type: int - returns 0 if successful, otherwise error type
Purpose: This function implements the list threads command. Since this
	is single threaded for now, just tell it that there is only one
	plus the RCP virtual thread.
************************************************************************/
int __rmonListThreads( KKHeader * req )
{
	register KKObjectRequest * request = (KKObjectRequest *) req;
	KKObjsEvent * reply = (KKObjsEvent *) &__rmonUtilityBuffer;

	Debug( "ListThreads\n" );
	reply->object = request->object == -1 ? CPU_PROC : request->object;
	if ( req->method == METHOD_RSP )
	{
		reply->objs.number = 1;
		reply->objs.objects[0] = RSP_THREAD;
	}
	else
	{
		register OSThread * tptr = __osGetActiveQueue();

		reply->objs.number = 0;
		while ( tptr->priority != -1 )
		{
			if ( tptr->id )		/* skip over system threads */
			{
				reply->objs.objects[reply->objs.number] = tptr->id;
				++reply->objs.number;
			}
			tptr = tptr->tlnext;
		}
	}
	reply->header.code = request->header.code;
	reply->header.error = TV_ERROR_NO_ERROR;
	__rmonSendReply( (KKHeader * const) reply,
		sizeof( KKObjsEvent ) + (reply->objs.number - 1) * 
		sizeof( int ), _KK_REPLY );
	return 0;
}

/************************************************************************
Function: __rmonGetThreadStatus
Args:
	int method: 0 for CPU, 1 for RCP
	int id: The thread in question
	KKStatusEvent * reply - address of the standard protocol structure for
		returning the status of a thread.
Type: int - returns error on invalid thread
Purpose: This function fills in the status information for a thread. The
	resulting reply packet will be further processed and returned
	to the host debugger. This code is isolated to a separate function
	for efficiency, since it will be the same for specific status
	requests or exception (e.g., breakpoint) messages. Thus, this
	function gets called in several places.
************************************************************************/
int __rmonGetThreadStatus( int method, int id, KKStatusEvent * reply )
{
	u32 inst;

	reply->status.tid = id;
	reply->status.pid = method == METHOD_RSP ? RSP_PROC : CPU_PROC;
	reply->status.why = KK_REQUESTED;
	reply->status.what = 0;
	reply->status.info.major = 0;
	reply->status.info.minor = 0;
	reply->status.rv = 0;
	if ( method == METHOD_RSP )
	{
		reply->status.start = IMEM_START;
		reply->status.priority = 42;
		if ( __rmonRCPrunning() )
		{
			reply->status.flags = KK_RUNNING;
			reply->status.info.addr = 0;
			reply->status.instr = 0;
		}
		else
		{
			reply->status.flags = KK_STOPPED;
			reply->status.info.addr = 
				__rmonReadWordAt( (u32 *) SP_PC_REG ) + IMEM_START;
			inst = __rmonReadWordAt( (u32 *) reply->status.info.addr );
			if ( (inst & BREAKMASK) == 0xd )	/* was a break */
				inst = 0x0d;
			if ( __rmonRcpAtBreak )
			{
				reply->status.why = KK_FAULTED;
				reply->status.info.major = KK_FLTINSTR;
				reply->status.info.minor = KK_FLTTRACE;
			}
			reply->status.instr = inst;
		}
	}
	else
	{
		OSThread * tptr = __osGetActiveQueue();

		while ( tptr->priority != -1 )
		{
			if ( tptr->id == id )
				break;
			tptr = tptr->tlnext;
		}
		if ( tptr->priority == -1 )	/* couldn't find it */
			return TV_ERROR_INVALID_ID;
		reply->status.priority = tptr->priority;
		reply->status.flags = tptr->state ?
			tptr->state : KK_STOPPED;
		reply->status.info.addr = tptr->context.pc;
		inst = *(int *)tptr->context.pc;
		if ( (inst & BREAKMASK) == 0xd )
			inst = 0x0d;
		reply->status.instr = inst;
		reply->status.start = (int) tptr;
		if ( tptr->flags & 1 )	/* assume we hit a break */
		{
			reply->status.why = KK_FAULTED;
			reply->status.info.major = KK_FLTINSTR;
			reply->status.info.minor = KK_FLTTRACE;
		}
		else if ( tptr->flags & 2 )	/* then we faulted */
		{
			reply->status.why = KK_FAULTED;
			reply->status.info.major = KK_FLTMEM;
			reply->status.info.minor = KK_FLTACCESS;
		}
	}
	return 0;
}

/************************************************************************
Function: __rmonThreadStatus
Args: KKHeader * req - address of the standard protocol structure for
	determining status of a thread.
Type: int - returns 0 if successful, otherwise error type
Purpose: This function implements the thread status command. Since rmon
	is single threaded for now, just tell it the app status.
************************************************************************/
int __rmonThreadStatus( KKHeader * req )
{
	KKObjectRequest * request = (KKObjectRequest *) req;
	KKStatusEvent reply;

	Debug( "ThreadStatus %d method %d\n", request->object, req->method );
	if ( __rmonGetThreadStatus( req->method, request->object, &reply ) )
		return TV_ERROR_INVALID_ID;
	reply.header.code = request->header.code;
	reply.header.error = TV_ERROR_NO_ERROR;
	__rmonSendReply( (KKHeader * const) &reply, sizeof( KKStatusEvent ), _KK_REPLY );
	return 0;
}

/************************************************************************
Function: __rmonStopThread
Args: KKHeader * req - address of the standard protocol structure for
	halting a thread
Type: int - returns 0 if successful, otherwise error type
Purpose: This function implements the halt thread command. Since rmon
	is single threaded for now, just stop the app and return app status.
************************************************************************/
int __rmonStopThread( KKHeader * req )
{
    KKObjectRequest * request = (KKObjectRequest *) req;
    KKStatusEvent reply;
    u32 *pc;

    Debug( "StopThread %d\n", request->object );
    switch ( req->method )
    {
	case METHOD_NORMAL:
	    __rmonStopUserThreads( request->object );
	    break;
	    
	case METHOD_RSP:
	    if ( __rmonRCPrunning() )
	    {
		__rmonIdleRCP();
		
		/*
		  We cannot allow stopping in a branch delay
		  slot, since reading out the regs will lose the
		  info that a branch was about to happen.
		  Since we do not have a BD bit, we will just
		  read the previous instruction to see if it
		  is a branch, and step once if so. This will
		  get us clear of the branch delay slot.
		  */
		
		pc = (u32 *) __rmonReadWordAt( (u32 *) SP_PC_REG );
		if ( pc == (u32 *) 0 )
		    break;		/* no problem */
		--pc;
		if ( (__rmonGetBranchTarget( METHOD_RSP, RSP_THREAD,
			(char *) ((u32) pc + IMEM_START) ) & 0x3) == 0 )
		{
		    __rmonStepRCP();
		}
	    }
	    break;

	default:
	    return TV_ERROR_OP_PROTECTED;
    }

    if ( __rmonGetThreadStatus( req->method, request->object, &reply ) )
	return TV_ERROR_INVALID_ID;
    reply.header.code = request->header.code;
    reply.header.error = TV_ERROR_NO_ERROR;
    __rmonSendReply( (KKHeader * const) &reply, sizeof( KKStatusEvent ), _KK_REPLY );
    
    /* Send an exception packet also if the thread really stopped */
    
    if ( reply.status.flags == KK_STOPPED )
    {
		reply.header.code = _KK_REQ_STATUS;
		__rmonSendReply( (KKHeader * const) &reply, sizeof( KKStatusEvent ),
			     _KK_EXCEPTION);
    }
    return 0;
}

/************************************************************************
Function: __rmonRunThread
Args: KKHeader * req - address of the standard protocol structure for
	running a thread
Type: int - returns 0 if successful, otherwise error type
Purpose: This function implements the run thread command. Since rmon
	is single threaded for now, just run the app and return app status.
	The flags field specifies actions:
		KK_STEP: single step
		KK_VADDR: set PC before running
		KK_ABORT: commit suicide (unimp)
************************************************************************/
int __rmonRunThread( KKHeader * req )
{
	KKRunThreadRequest * request = (KKRunThreadRequest *) req;
	KKObjectEvent reply;
	KKStatusEvent exceptionReply;
	register OSThread * tptr;
	register int runNeeded = 0;

	Debug( "RunThread %d\n", request->tid );
	switch ( req->method )
	{
	case METHOD_NORMAL:
		tptr = (OSThread *) __osGetActiveQueue();
		while ( tptr->priority != -1 )
		{
			if ( tptr->id == (s32)request->tid )
				break;
			tptr = tptr->tlnext;
		}
		if ( tptr->priority == -1 )	/* couldn't find it */
			return TV_ERROR_INVALID_ID;
		if ( tptr->state != OS_STATE_STOPPED )
			return TV_ERROR_OP_PROTECTED;
		tptr->flags &= ~3;	/* neither break nor fault */
		if ( request->actions.flags & KK_VADDR )	/* set new pc */
			tptr->context.pc = request->actions.vaddr;
		if ( request->actions.flags & KK_STEP )
			if ( !__rmonSetSingleStep( request->tid,
				(u32 *) tptr->context.pc ) )
					return TV_ERROR_OP_PROTECTED;
		runNeeded = 1;
		break;
	case METHOD_RSP:
		if ( __rmonRCPrunning() )
			return TV_ERROR_OP_PROTECTED;	/* already running */
		if ( request->actions.flags & KK_VADDR )	/* set new pc */
			__rmonWriteWordTo( (u32 *) SP_PC_REG, 
				(u32) (request->actions.vaddr - IMEM_START) );
		if ( request->actions.flags & KK_STEP )
		{
			/*
				The first thing we have to do is see if this
				is a branch. If so, we need to step twice
				to get past the branch delay slot inst
				and not lose the next PC info.
			*/

			if ( (__rmonGetBranchTarget( METHOD_RSP, RSP_THREAD,
				(char *) (__rmonReadWordAt( (u32 *) SP_PC_REG ) + 
				IMEM_START) ) & 0x3) == 0 )	/* it was */
			{
					__rmonStepRCP();
			}
			__rmonStepRCP();
			__rmonRcpAtBreak = 1;
		}
		else	/* run it */
		{
			__rmonRcpAtBreak = 0;
			__rmonRunRCP();
		}
		reply.header.code = request->header.code;
		reply.header.error = TV_ERROR_NO_ERROR;
		reply.object = request->tid;
		__rmonSendReply( (KKHeader * const) &reply, sizeof( KKObjectEvent ), _KK_REPLY );
		if ( request->actions.flags & KK_STEP )
		{
			__rmonGetThreadStatus( METHOD_RSP, RSP_THREAD, &exceptionReply );
			__rmonGetExceptionStatus( &exceptionReply );

			__rmonSendReply( (KKHeader * const) &exceptionReply,
				sizeof( KKStatusEvent ), _KK_EXCEPTION);
		}
		return 0;
	
	default:
		return TV_ERROR_OP_PROTECTED;
	}

	reply.header.code = request->header.code;
	reply.header.error = TV_ERROR_NO_ERROR;
	reply.object = request->tid;
	__rmonSendReply( (KKHeader * const) &reply, sizeof( KKObjectEvent ), _KK_REPLY );
	if ( runNeeded )
		osStartThread( tptr );
	return 1;	/* Dispatch the app */
}

#endif /* #ifndef _FINALROM */