rmonmem.c 14.7 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
/*
 * ==========================================================================
 * 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.
 *
 * 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: mem.c
Author: Kenneth F. Greenberg
Purpose: The memory access functions
Revision history:
	94.10.24  Original
************************************************************************/

#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"
#include <rdb.h>

#ifndef _FINALROM

u8 __rmonUtilityBuffer[256];	/* place to build reply packets */

/************************************************************************
Function: __rmonWriteWordTo
Args:
	u32 * addr - where to write the word
	u32 val - what to write
Type: void
Purpose: This function writes one word to the coprocessor. It is useful
	to isolate it since this process is very dependent on execution
	environment, and also requires different processing from normal
	memory accesses. For example, in the real hardware it must be
	uncached.
************************************************************************/
void __rmonWriteWordTo( u32 * addr, u32 val )
{
	while ( __osSpRawWriteIo( (u32) addr, val ) ) ;
}

/************************************************************************
Function: __rmonReadWordAt
Args: u32 * addr - where to read from
Type: u32 - returns the contents of the specified location
Purpose: This function reads one word from the coprocessor. It is useful
	to isolate it since this process is very dependent on execution
	environment, and also requires different processing from normal
	memory accesses. For example, in the real hardware it must be
	uncached.
************************************************************************/
u32 __rmonReadWordAt( u32 * addr )
{
    if ( (u32) addr >= RCPMEM_START && (u32) addr <= RCPMEM_END )
    {
	u32 data;
	
	__osSpRawReadIo( (u32) addr, &data );
	return data;
    }
    /* what should you do here??? */
    return (0);
}

/************************************************************************
Function: __rmonMemcpy
Args:
	u8  * dest - destination block address
	u8  * srce - source block address
	u32 count - byte count
Type: void
Purpose: This function implements a simple version of memcpy() to avoid
	the need to link in any library routines. Libc version of memcpy
	is 0x310 bytes!
************************************************************************/
void __rmonMemcpy( u8 * dest, u8 * srce, u32 count )
{
    while ( count-- )
	*dest++ = *srce++;
}

/************************************************************************
Function: __rmonCopyWords
Args:
	u32 * dest - destination block address
	u32 * srce - source block address
	u32 count - byte count
Type: void
Purpose: This function implements a word version of memcpy() to avoid
	the need to link in any library routines. It works like memcpy
	(or memcopy) except it moves count words instead of bytes. This
	suggests that not only are the blocks word aligned, but their
	length is an integral number of words.
************************************************************************/
void __rmonCopyWords( u32 * dest, u32 * srce, u32 count )
{
	while ( count-- )
		*dest++ = *srce++;
}

/************************************************************************
Function: (static) strcpy
Args:
	char * dest - destination block address
	char * srce - source block address
Type: void
Purpose: This function implements a simple version of strcpy() to avoid
	the need to link in any library routines.
************************************************************************/
static void strcpy( char * dest, char * srce )
{
	while ( (*dest++ = *srce++) )
		;
}

/*
	Read the following macro as:
		InRange( blockStart, blockSize, rangeStart, rangeEnd )
	It is true if the block defined by start and size is entirely
	within the range defined.
*/

#define InRange(st,sz,rs,re) ((((st) < (rs)) || (((st) + (sz)) > (re))) ? 0 : 1)


/************************************************************************
Function: __rmonReadMem
Args: KKHeader * req - address of the standard protocol structure for
	reading memory disguised as a header.
Type: int - returns 0 if successful, otherwise error type
Purpose: This function reads a block of memory and returns the contents
	to the host.
************************************************************************/
int __rmonReadMem( KKHeader * req )
{
    
    unsigned char  *cPtr;
    int            sent;
    int            dataSize;
    KKReadRequest  *request = (KKReadRequest *) req;
    KKBufferEvent  *reply = (KKBufferEvent *) &__rmonUtilityBuffer;
    u8		   *blockStart;

    Debug( "ReadMem @ %08x for %d\n", request->addr, request->nbytes );
    reply->header.code = request->header.code;
    reply->object = request->object;
    reply->header.error = TV_ERROR_NO_ERROR;
    
    if ( request->addr == (unsigned int)-1 )
	return TV_ERROR_INVALID_ADDRESS;
    if ( request->nbytes > MAXMEMREQ )
	return TV_ERROR_INVALID_CAPABILITY;
    if ( req->method == METHOD_RSP )
    {
	/* Validate Ranges here, else this would be bad */
	
	if ( !InRange( request->addr, request->nbytes, IMEM_START, IMEM_END ) && 
	     !InRange( request->addr, request->nbytes, DMEM_START, DMEM_END ) )
	    return TV_ERROR_INVALID_ADDRESS;
    }
    else	/* validate CPU address otherwise */
    {
	if ( osVirtualToPhysical( (void *) request->addr ) == (u32)-1 )
	    return TV_ERROR_INVALID_ADDRESS;
    }
    blockStart = (u8 *) request->addr;
    reply->header.length = sizeof( KKHeader ) + sizeof( TVid ) +
	request->nbytes;
    
    dataSize = sizeof( KKHeader ) + sizeof( TVid ) + request->nbytes;

    cPtr = (unsigned char*)&dataSize;

    /* send the size of the transfer */
    sent = 0;
    while(sent < 4)
	sent += __osRdbSend(&cPtr[sent],4-sent,RDB_TYPE_GtoH_DEBUG);

    __rmonSendHeader( (KKHeader * const) reply, sizeof( KKHeader ) + 
		      sizeof( TVid ), _KK_REPLY );
    __rmonSendData( (const char *) blockStart, request->nbytes );
    return 0;
}

/************************************************************************
Function: __rmonWriteMem
Args: KKHeader * req - address of the standard protocol structure for
	writing memory disguised as a header.
Type: int - returns 0 if successful, otherwise error type
Purpose: This function writes a block of memory to the target system.
	It is not used for downloading programs (although it could be)
	but is instead intended for small writes like patching memory
	contents.
************************************************************************/
int __rmonWriteMem( KKHeader * req )
{
	register KKWriteRequest * request = (KKWriteRequest *) req;
	KKObjectEvent reply;

	Debug( "WriteMem\n" );
	if ( req->method == METHOD_NORMAL &&
		osVirtualToPhysical( (void *) request->writeHeader.addr ) == (u32)-1 )
			return TV_ERROR_INVALID_ADDRESS;
	if ( request->writeHeader.nbytes > MAXMEMREQ )
		return TV_ERROR_INVALID_CAPABILITY;

	if ( InRange( request->writeHeader.addr, request->writeHeader.nbytes, RCPMEM_START, RCPMEM_END ) )
	{
		int align;
		u32 word;

		/* do it as word reads to something */

		if ( (align = request->writeHeader.addr & 3) )	/* insert */
		{
			if ( request->writeHeader.nbytes != 1 )
			{
				/* can't do this yet */
				Debug( "Long unaligned write...\n" );
				return TV_ERROR_INVALID_ADDRESS;
			}
			word = __rmonReadWordAt( (u32 *) ((u32) request->writeHeader.addr & ~3) );
			if ( align == 1 )
				word = (word & 0xff00ffff) | (*request->buffer << 16);
			else if ( align == 2 )
				word = (word & 0xffff00ff) | (*request->buffer << 8);
			else
				word = (word & 0xffffff00) | (*request->buffer);
			__rmonWriteWordTo( (u32 *) ((u32) request->writeHeader.addr & ~3), word );
		}
		else		/* just write stuff */
		{
			int wordCount = request->writeHeader.nbytes / 4;
			u32 * wordPointer = (u32 *) request->buffer;

			if ( request->writeHeader.nbytes % 4 )
			{
				Debug( "RCP write not an integral number of words\n" );
				return TV_ERROR_INVALID_ADDRESS;
			}
			while ( wordCount-- )
			{
				__rmonWriteWordTo( (u32 *) request->writeHeader.addr,
					*wordPointer++ );
				request->writeHeader.addr += 4;		/* decl as int */
			}
		}
	}
	else	/* just any old memory */
	{
		__rmonMemcpy( (u8 *) request->writeHeader.addr,
			(u8 *) request->buffer,
			request->writeHeader.nbytes );
	}

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

/************************************************************************
Function: __rmonListProcesses
Args: KKHeader * req - address of the standard protocol structure for
	listing target processes.
Type: int - returns 0 if successful, otherwise error type
Purpose: This function implements the list processes command. Since this
	is the first command sent by TeleShop, we need to give it an
	answer. In this case (unless we do virtual processes), we will
	just tell it that there is only one.
************************************************************************/
int __rmonListProcesses( KKHeader * req )
{
	KKObjectRequest * request = (KKObjectRequest *) req;
	KKObjsEvent reply;

	Debug( "ListProcesses\n" );
	reply.object = 0;
	reply.objs.number = 1;
	reply.objs.objects[0] = req->method == METHOD_RSP ? RSP_PROC : CPU_PROC;
	reply.header.code = request->header.code;
	reply.header.error = TV_ERROR_NO_ERROR;
	__rmonSendReply( (KKHeader * const) &reply, sizeof( KKObjsEvent ), _KK_REPLY );
	return 0;
}

/************************************************************************
Function: __rmonLoadProgram
Args: KKHeader * request - address of the standard protocol structure for
	loading a program.
Type: int - returns 0 if successful, otherwise error type
Purpose: This function implements the load program command. Since program
	loading is not really a monitor function, this probably just
	idles the monitor until the load is complete.
************************************************************************/
int __rmonLoadProgram( KKHeader * request )
{
	Debug( "LoadProgram\n" );
	return TV_ERROR_ILLEGAL_CALL;
}

/************************************************************************
Function: __rmonGetExeName
Args: KKHeader * req - address of the standard protocol structure for
	retrieving an executable program's name.
Type: int - returns 0 if successful, otherwise error type
Purpose: This function implements the get exe name command. Since we
	don't know this info, just return null and hope for the best.
************************************************************************/
int __rmonGetExeName( KKHeader * req )
{
	KKObjectRequest * request = (KKObjectRequest *) req;
	KKBufferEvent *reply = (KKBufferEvent *) &__rmonUtilityBuffer;

	Debug( "GetExeName\n" );
	reply->header.code = request->header.code;
	reply->header.error = TV_ERROR_NO_ERROR;
	reply->object = request->object;
	if ( req->method == METHOD_RSP )
		strcpy( reply->buffer, "imem" );
	else
		strcpy( reply->buffer, "rmon" );
	__rmonSendReply( (KKHeader * const) reply, sizeof( KKBufferEvent ) + 4, _KK_REPLY );
	return 0;
}

/************************************************************************
Function: __rmonGetRegionCount
Args: KKHeader * req - address of the standard protocol structure for
	getting number of regions, disguised as a header.
Type: int - returns 0 if successful, otherwise error type
Purpose: This function gets the number of regions for a process.
************************************************************************/
int
__rmonGetRegionCount( KKHeader * req )
{
	KKObjectRequest * request = (KKObjectRequest *) req;
	KKNumberEvent reply;

	Debug( "GetRegionCount\n" );
	reply.header.code = request->header.code;
	reply.header.error = TV_ERROR_NO_ERROR;
	reply.object = request->object;
	reply.number = req->method == METHOD_RSP ? 2 : 5;
	__rmonSendReply( (KKHeader * const) &reply, sizeof( KKNumberEvent ), _KK_REPLY );
	return 0;
}

/************************************************************************
Function: __rmonGetRegions
Args: KKHeader * req - address of the standard protocol structure for
	getting region info, disguised as a header.
Type: int - returns 0 if successful, otherwise error type
Purpose: This function gets the region info for a process.
************************************************************************/
int
__rmonGetRegions( KKHeader * req )
{
	KKObjectRequest * request = (KKObjectRequest *) req;
	KKRegionEvent * reply = (KKRegionEvent *) &__rmonUtilityBuffer;
	int numRegions;

	Debug( "GetRegions\n" );
	numRegions = req->method == METHOD_RSP ? 2 : 6;
	reply->header.length = sizeof( KKRegionEvent ) + numRegions * sizeof( KKRegion );
	reply->header.code = request->header.code;
	reply->header.error = TV_ERROR_NO_ERROR;
	reply->object = request->object;
	reply->number = numRegions;
	reply->regions[1].vaddr = IMEM_START;
	reply->regions[1].size =  0x00001000;
	reply->regions[1].flags = KK_READ | KK_WRITE | KK_EXEC;
	reply->regions[1].paddr = IMEM_START;
	reply->regions[0].vaddr = DMEM_START;
	reply->regions[0].size =  0x00001000;
	reply->regions[0].flags = KK_READ | KK_WRITE;
	reply->regions[0].paddr = DMEM_START;
	if ( numRegions > 2 )
	{
		reply->regions[2].vaddr = MEMBASE;
		reply->regions[2].size = 0x6130;
		reply->regions[2].flags = KK_READ | KK_EXEC;
		reply->regions[2].paddr = 0;
		reply->regions[3].vaddr = 0x00000004;	/* DRAM */
		reply->regions[3].size =  0x00200000;	/* 2 MB */
		reply->regions[3].flags = KK_READ | KK_WRITE ;
		reply->regions[3].paddr = 0;
		reply->regions[4].vaddr = 0x04002000;	/* Rest of RCP */
		reply->regions[4].size =  0x00800000;	/* Ok to 04802000 */
		reply->regions[4].flags = KK_READ | KK_WRITE ;
		reply->regions[4].paddr = 0;
		reply->regions[5].vaddr = 0x88206130;
		reply->regions[5].size =  0x9000;	/* Ok to 04802000 */
		reply->regions[5].flags = KK_READ | KK_WRITE ;
		reply->regions[5].paddr = 0;
	}
	__rmonSendReply( (KKHeader * const) reply, reply->header.length, _KK_REPLY );
	return 0;
}

#endif  /* #ifndef _FINALROM */