summaryrefslogtreecommitdiffstats
path: root/src/reg.c
blob: d094fbe576003355db9843028e8764b0c3e81670 (plain)
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
/* Copyright 2017 IBM Corp.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may 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.
 */
#include <errno.h>
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <target.h>
#include <operations.h>

#include "main.h"

#define REG_MEM -3
#define REG_MSR -2
#define REG_NIA -1
#define REG_R31 31

static void print_proc_reg(struct pdbg_target *target, uint64_t reg, uint64_t value, int rc)
{
	int proc_index, chip_index, thread_index;

	thread_index = pdbg_target_index(target);
	chip_index = pdbg_parent_index(target, "core");
	proc_index = pdbg_parent_index(target, "pib");
	printf("p%d:c%d:t%d:", proc_index, chip_index, thread_index);

	if (reg == REG_MSR)
		printf("msr: ");
	else if (reg == REG_NIA)
		printf("nia: ");
	else if (reg > REG_R31)
		printf("spr%03" PRIu64 ": ", reg - REG_R31);
	else if (reg >= 0 && reg <= 31)
		printf("gpr%02" PRIu64 ": ", reg);

	if (rc == 1) {
		printf("Check threadstatus - not all threads on this chiplet are quiesced\n");
	} else if (rc == 2)
		printf("Thread in incorrect state\n");
	else
		printf("0x%016" PRIx64 "\n", value);
}

static int putprocreg(struct pdbg_target *target, uint32_t index, uint64_t *reg, uint64_t *value)
{
	int rc;

	if (*reg == REG_MSR)
		rc = ram_putmsr(target, *value);
	else if (*reg == REG_NIA)
		rc = ram_putnia(target, *value);
	else if (*reg > REG_R31)
		rc = ram_putspr(target, *reg - REG_R31, *value);
	else if (*reg >= 0 && *reg <= 31)
		rc = ram_putgpr(target, *reg, *value);

	print_proc_reg(target, *reg, *value, rc);

	return 0;
}

static int getprocreg(struct pdbg_target *target, uint32_t index, uint64_t *reg, uint64_t *unused)
{
	int rc;
	uint64_t value;

	if (*reg == REG_MSR)
		rc = ram_getmsr(target, &value);
	else if (*reg == REG_NIA)
		rc = ram_getnia(target, &value);
	else if (*reg > REG_R31)
		rc = ram_getspr(target, *reg - REG_R31, &value);
	else if (*reg >= 0 && *reg <= 31)
		rc = ram_getgpr(target, *reg, &value);

	print_proc_reg(target, *reg, value, rc);

	return !rc;
}

int handle_gpr(int optind, int argc, char *argv[])
{
	char *endptr;
	uint64_t gpr;

	if (optind + 1 >= argc) {
		printf("%s: command '%s' requires a GPR\n", argv[0], argv[optind]);
		return -1;
	}

	errno = 0;
	gpr = strtoull(argv[optind + 1], &endptr, 0);
	if (errno || *endptr != '\0') {
		printf("%s: command '%s' couldn't parse GPR '%s'\n",
				argv[0], argv[optind], argv[optind + 1]);
		return -1;
	}

	if (gpr > 31) {
		printf("A GPR must be between zero and 31 inclusive\n");
		return -1;
	}

	if (strcmp(argv[optind], "putgpr") == 0) {
		uint64_t data;

		if (optind + 2 >= argc) {
			printf("%s: command '%s' requires data\n", argv[0], argv[optind]);
			return -1;
		}

		errno = 0;
		data = strtoull(argv[optind + 2], &endptr, 0);
		if (errno || *endptr != '\0') {
			printf("%s: command '%s' couldn't parse data '%s'\n",
				argv[0], argv[optind], argv[optind + 1]);
			return -1;
		}

		return for_each_target("thread", putprocreg, &gpr, &data);
	}

	return for_each_target("thread", getprocreg, &gpr, NULL);
}

int handle_nia(int optind, int argc, char *argv[])
{
	uint64_t reg = REG_NIA;
	char *endptr;

	if (strcmp(argv[optind], "putnia") == 0) {
		uint64_t data;

		if (optind + 2 >= argc) {
			printf("%s: command '%s' requires data\n", argv[0], argv[optind]);
			return -1;
		}

		errno = 0;
		data = strtoull(argv[optind + 2], &endptr, 0);
		if (errno || *endptr != '\0') {
			printf("%s: command '%s' couldn't parse data '%s'\n",
				argv[0], argv[optind], argv[optind + 1]);
			return -1;
		}

		return for_each_target("thread", putprocreg, &reg, &data);
	}

	return for_each_target("thread", getprocreg, &reg, NULL);
}

int handle_spr(int optind, int argc, char *argv[])
{
	char *endptr;
	uint64_t spr;

	if (optind + 1 >= argc) {
		printf("%s: command '%s' requires a GPR\n", argv[0], argv[optind]);
		return -1;
	}

	errno = 0;
	spr = strtoull(argv[optind + 1], &endptr, 0);
	if (errno || *endptr != '\0') {
		printf("%s: command '%s' couldn't parse GPR '%s'\n",
				argv[0], argv[optind], argv[optind + 1]);
		return -1;
	}

	spr += REG_R31;

	if (strcmp(argv[optind], "putspr") == 0) {
		uint64_t data;

		if (optind + 2 >= argc) {
			printf("%s: command '%s' requires data\n", argv[0], argv[optind]);
			return -1;
		}

		errno = 0;
		data = strtoull(argv[optind + 2], &endptr, 0);
		if (errno || *endptr != '\0') {
			printf("%s: command '%s' couldn't parse data '%s'\n",
				argv[0], argv[optind], argv[optind + 1]);
			return -1;
		}

		return for_each_target("thread", putprocreg, &spr, &data);
	}

	return for_each_target("thread", getprocreg, &spr, NULL);
}

int handle_msr(int optind, int argc, char *argv[])
{
	uint64_t msr = REG_MSR;
	char *endptr;

	if (strcmp(argv[optind], "putmsr") == 0) {
		uint64_t data;

		if (optind + 2 >= argc) {
			printf("%s: command '%s' requires data\n", argv[0], argv[optind]);
			return -1;
		}

		errno = 0;
		data = strtoull(argv[optind + 2], &endptr, 0);
		if (errno || *endptr != '\0') {
			printf("%s: command '%s' couldn't parse data '%s'\n",
				argv[0], argv[optind], argv[optind + 1]);
			return -1;
		}

		return for_each_target("thread", putprocreg, &msr, &data);
	}

	return for_each_target("thread", getprocreg, &msr, NULL);
}
OpenPOWER on IntegriCloud