summaryrefslogtreecommitdiffstats
path: root/main/common/reg_cache.c
blob: 1c6c794202f27f8502d8c0a45f5da3b1b3e3d2d1 (plain) (blame)
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
/**************************************************************************
 *
 * Copyright (c) 2013 Alcatel-Lucent
 * 
 * Alcatel Lucent licenses this file to You under the Apache License,
 * Version 2.0 (the "License"); you may not use this file except in
 * compliance with the License.  A copy of the License is contained the
 * file LICENSE at the top level of this repository.
 * You may also obtain a copy of the License at:
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 **************************************************************************
 *
 * reg_cache.c:
 *
 *	Allow the user to display CPU registers that are locally cached
 *	when an exception is hit.   These registers are not the currently
 *	active registers in the CPU context; rather, a copy of the context at
 *	the time of the most recent breakpoint or exception.
 *
 * Original author:     Ed Sutter (ed.sutter@alcatel-lucent.com)
 *
 */
#include "config.h"
#include "cpu.h"
#include "genlib.h"
#include <ctype.h>
#include "stddefs.h"
#include "regnames.c"
#include "cli.h"

/* The file regnames.c will contain a table of character pointers that
 * corresond to registers that are stored away by the target's exception
 * handler.  The table of names and the order in which the registers are
 * stored by the exception handler must be synchronized.
 * An example table is as follows (taken from FADS-MPC860 target)...
 *
 * static char  *regnames[] = {
 * 		"R0",		"R1",		"R2",		"R3",
 * 		"R4",		"R5",		"R6",		"R7",
 * 		"R8",		"R9",		"R10",		"R11",
 * 		"R12",		"R13",		"R14",		"R15",
 * 		"R16",		"R17",		"R18",		"R19",
 * 		"R20",		"R21",		"R22",		"R23",
 * 		"R24",		"R25",		"R26",		"R27",
 * 		"R28",		"R29",		"R30",		"R31",
 * 		"MSR",		"CR",		"LR",		"XER",
 * 		"DAR",		"DSISR",	"SRR0",		"SRR1",
 * 		"CTR",		"DEC",		"TBL",		"TBU",
 * 		"SPRG0",	"SPRG1",	"SPRG2",	"SPRG3",
 * };
 *
 * The real definition of this array would be in the file regnames.c in 
 * the target-specific directory.
 */

#define REGTOT	(sizeof regnames/sizeof(char *))

/* regtbl[]:
 * This array is used to store the actual register values.  Storage
 * is done by the target's exception handler and must match the order
 * of the register names in the regnames[] array.
 */
ulong regtbl[REGTOT];

void
flushRegtbl(void)
{
	flushDcache((char *)regtbl,sizeof(regtbl));
}

/* regidx():
 * Return an index into the regnames[] array that matches the
 * incoming register name.
 * If no match is found, print an error message and return -1.
 */
static int
regidx(char *name)
{
	int	i;

	strtoupper(name);
	for(i=0;i<REGTOT;i++) {
		if (!strcmp(name,regnames[i])) {
			return(i);
		}
	}
	printf("Bad reg: '%s'\n",name);
	return(-1);
}

/* putreg():
 * Put the specified value into the specified register
 * storage location.
 */
int
putreg(char *name,ulong value)
{
	int	idx;

	if ((idx = regidx(name)) == -1)
		return(-1);
	
	regtbl[idx] = value;
	return(0);
}

/* getreg():
 * Retrieve the value of the specified register.
 */
int
getreg(char *name,ulong *value)
{
	int	idx;

	if ((idx = regidx(name)) == -1)
		return(-1);
	
	*value = regtbl[idx];
	return(0);
}

/* showregs():
 * Dump the content of the register cache in a tabular format
 * showing the entry in the regnames[] array and the corresponding
 * entry in the regtbl[] array.
 */
void
showregs(void)
{
	int	i, j;

	for(i=0;i<REGTOT;) {
		for(j=0;((j<4) && (i<REGTOT));j++,i++)
			printf("%6s=0x%08lx ",regnames[i],regtbl[i]);
		printf("\n");
	}
}

/* reginit():
 * Clear the register cache.
 */
void
reginit(void)
{
	int	i;

	for(i=0;i<REGTOT;i++)
		regtbl[i] = 0;
}

#if INCLUDE_STRACE
char *RegHelp[] = {
	"Display/modify content of monitor's register cache",
	"-[v:] [regname] [value]",
#if INCLUDE_VERBOSEHELP
	"Options:",
	" -v {var} quietly load 'var' with register content",
#endif
	0,
};

int
Reg(int argc,char *argv[])
{
	ulong	reg;
	int		opt, i, forscript;
	char	*varname, buf[32];

	forscript = 0;
	varname = (char *)0;
	while((opt=getopt(argc,argv,"sv:")) != -1) {
		switch(opt) {
		case 's':
			forscript = 1;
			break;
		case 'v':
			varname = optarg;
			break;
		default:
			return(CMD_PARAM_ERROR);
		}
	}

	if (argc == optind) {
		if (forscript) {
			for(i=0;i<REGTOT;i++) 
				printf("reg %s 0x%lx\n",regnames[i],regtbl[i]);
		}
		else
			showregs();
		return(CMD_SUCCESS);
	}
	else if (argc == optind + 1) {
		if (getreg(argv[optind],&reg) != -1) {
			sprintf(buf,"0x%lx",reg);
			if (varname)
				setenv(varname,buf);
			else
				printf("%s = %s\n",argv[optind],buf);
		}
	}
	else if (argc == optind + 2) {
    	putreg(argv[1],strtol(argv[optind+1],(char **)0,0));
	}
	else {
		return(CMD_PARAM_ERROR);
	}
	return(CMD_SUCCESS);
}
#endif