-
Notifications
You must be signed in to change notification settings - Fork 5
/
gdb.c
executable file
·412 lines (379 loc) · 10.8 KB
/
gdb.c
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
/*******************************************************************************
' Version 0.54
' Copyright (c) 2010, 2011, 2012
' See end of file for terms of use.
'******************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <unistd.h>
#include <dirent.h>
#include <sys/stat.h>
#include "conion.h"
#include "interp.h"
#include "spinsim.h"
#define GCC_REG_BASE 0
extern FILE *logfile;
extern FILE *tracefile;
extern FILE *cmdfile;
extern PasmVarsT PasmVars[8];
extern char *hubram;
char cmd[1028];
extern int32_t profile;
extern int32_t loopcount;
void reply(char *ptr, int len){
unsigned char cksum = 0;
int i;
putc('$', stdout);
if(logfile) fprintf(logfile, "sim>$");
for(i = 0; i < len; i++){
putc(ptr[i], stdout);
if(logfile) putc(ptr[i], logfile);
cksum += ptr[i];
}
fprintf(stdout, "#%02x", cksum);
if(logfile) fprintf(logfile, "#%02x", cksum);
if(logfile) putc('\n', logfile);
fflush(stdout);
if(logfile) fflush(logfile);
}
char parse_byte(char *ch){
char s[3];
char val;
s[0] = ch[0];
s[1] = ch[1];
s[2] = 0;
val = 0xff & strtol(s, NULL, 16);
return val;
}
char get_byte(uint32_t addr){
if(addr & 0x80000000){
int cog = (addr >> 28) - 8;
uint32_t tmp;
tmp = PasmVars[cog].mem[(addr & 0x000007ff) >> 2];
return (tmp >> 8*(addr & 0x3)) & 0xff;
} else {
return BYTE(addr);
}
}
void put_byte(uint32_t addr, unsigned char val){
if(addr & 0x80000000){
int cog = (addr >> 28) - 8;
uint32_t tmp;
tmp = PasmVars[cog].mem[(addr & 0x000007ff) >> 2];
switch(addr & 3){
case 0:
tmp &= 0xffffff00;
tmp |= val;
break;
case 1:
tmp &= 0xffff00ff;
tmp |= val << 8;
break;
case 2:
tmp &= 0xff00ffff;
tmp |= val << 16;
break;
case 3:
tmp &= 0x00ffffff;
tmp |= val << 24;
break;
}
PasmVars[cog].mem[(addr & 0x000007ff) >> 2] = tmp;
return;
} else {
BYTE(addr) = val;
return;
}
}
void get_cmd(){
int i = 0;
int ch;
do{
ch = getc(cmdfile);
} while(ch != '$');
if(logfile) fprintf(logfile, "gdb>");
if(logfile) putc(ch, logfile);
for(i = 0; i < sizeof(cmd); i++){
ch = getc(cmdfile);
if(logfile) putc(ch, logfile);
if(ch == '#') break;
cmd[i] = ch;
}
cmd[i] = 0;
// eat the checksum
ch = getc(cmdfile);
if(logfile) putc(ch, logfile);
ch = getc(cmdfile);
if(logfile) putc(ch, logfile);
if(logfile) putc('\n', logfile);
// send an ACK
putchar('+');
fflush(stdout);
if(logfile) fflush(logfile);
}
int tohex(char x){
if(x >= '0' && x <= '9') return x - '0';
if(x >= 'a' && x <= 'f') return 10 + x - 'a';
if(x >= 'A' && x <= 'F') return 10 + x - 'A';
return 0;
}
int32_t get_addr(int *i){
int32_t reg;
reg = tohex(cmd[(*i)++]) << 4;
reg |= tohex(cmd[(*i)++]) << 0;
reg |= tohex(cmd[(*i)++]) << 12;
reg |= tohex(cmd[(*i)++]) << 8;
reg |= tohex(cmd[(*i)++]) << 20;
reg |= tohex(cmd[(*i)++]) << 16;
reg |= tohex(cmd[(*i)++]) << 28;
reg |= tohex(cmd[(*i)++]) << 24;
return reg;
}
struct bkpt {
struct bkpt *next;
uint32_t addr;
uint32_t len;
};
struct bkpt *bkpt = 0;
// Check the cog's PC to see if the LMM microcode is at an appropriate place
// for a breakpoint to occur. We don't want to break on LMM administrative
// instructions.
// FIXME we need a more general way to do this. This is too dependent on special knowledge.
int breakable_point(int i){
int32_t pc = PasmVars[i].pc;
if ((pc == 0x0000004c/4)
|| (pc == 0x00000058/4)
|| (pc == 0x00000064/4)
|| (pc == 0x00000070/4)
|| (pc == 0x0000007c/4)
|| (pc == 0x00000088/4)
|| (pc == 0x00000094/4)
|| (pc == 0x000000a0/4))
return 1;
else
return 0;
}
void gdb(void)
{
int i;
int j;
char response[1024];
unsigned int addr;
int len;
char *end;
char val;
int32_t reg;
int cog = 0;
char *halt_code = "S05";
if(logfile) fprintf(logfile, "\n\nStarting log:\n");
RebootProp();
for(;;){
get_cmd();
i = 0;
switch(cmd[i++]){
case 'g':
for(i = 0; i < 18; i++){
reg = PasmVars[cog].mem[GCC_REG_BASE + i];
sprintf(response+8*i,
"%02x%02x%02x%02x",
(unsigned char)(reg & 0xff),
(unsigned char)((reg>>8) & 0xff),
(unsigned char)((reg>>16) & 0xff),
(unsigned char)((reg>>24) & 0xff));
}
reg = PasmVars[cog].pc * 4 + 0x80000000 + cog * 0x10000000;
sprintf(response+8*i,
"%02x%02x%02x%02x",
(unsigned char)(reg & 0xff),
(unsigned char)((reg>>8) & 0xff),
(unsigned char)((reg>>16) & 0xff),
(unsigned char)((reg>>24) & 0xff));
reply(response, 8*18+8);
break;
case 'G':
for(j = 0; j < 18; j++){
PasmVars[cog].mem[GCC_REG_BASE + j] = get_addr(&i);
}
// Ignore writes to cog pc. Instead, reset it to be
// ready for a fresh instruction.
// This is too magical. How do we fix it?
PasmVars[cog].pc = 0x12;
reply("OK",2);
break;
case 'm':
addr = strtol(&cmd[i], &end, 16);
i = (size_t)end - (size_t)cmd;
i++;
len = strtol(&cmd[i], NULL, 16);
if(len > 512) len = 512;
j = 0;
while(len--){
val = get_byte(addr);
addr++;
sprintf(&response[j], "%02x", (unsigned char)val);
j += 2;
}
reply(response, j);
break;
case 'M':
addr = strtol(&cmd[i], &end, 16);
i = (size_t)end - (size_t)cmd;
i++;
len = strtol(&cmd[i], &end, 16);
i = (size_t)end - (size_t)cmd;
i++;
while(len--){
val = parse_byte(&cmd[i]);
i += 2;
put_byte(addr, val & 0xff);
addr++;
}
reply("OK",2);
break;
case 's':
if(cmd[i]){
// Get the new LMM PC, reset the microcode
PasmVars[cog].mem[GCC_REG_BASE + 17] = get_addr(&i);
PasmVars[cog].pc = 0x12;
}
{
int brk = 0;
do {
int i;
// Step through a full LMM instruction
step_chip();
for(i = 0; i < 8; i++){
if(breakable_point(i)) brk = 1;
}
} while (!brk);
}
halt_code = "S05";
reply(halt_code, 3);
break;
case 'c':
if(cmd[i]){
PasmVars[cog].mem[GCC_REG_BASE + 17] = get_addr(&i);
PasmVars[cog].pc = 0x12;
}
halt_code = "S02";
do {
int brk = 0;
struct bkpt *b;
for (i = 0; i < 8; i++) {
if(breakable_point(i)){
// Look to see if the cog's LMM PC is at a breakpoint
for(b = (struct bkpt *)&bkpt; b->next; b = b->next){
if((PasmVars[i].mem[GCC_REG_BASE + 17] >= b->next->addr)
&& (PasmVars[i].mem[GCC_REG_BASE + 17] <= b->next->addr + b->next->len)){
brk = 1;
}
}
}
}
if(brk){
halt_code = "S05";
break;
} else {
step_chip();
}
} while(getch() != 0x03); // look for a CTRL-C
reply(halt_code, 3);
break;
case 'H':
if((cmd[i] == 'g') && (cmd[i+1] == '0')){
reply("OK", 2);
} else {
reply("", 0);
}
break;
case 'k':
reply("OK", 2);
goto out;
case 'z':
/* Remove breakpoint */
if(cmd[i++] == '0'){
long addr;
long len;
struct bkpt *b;
char *p = &cmd[i];
p++; /* Skip the comma */
addr = strtol(p, &p, 16);
p++; /* Skip the other comma */
len = strtol(p, NULL, 16);
for(b = (struct bkpt *)&bkpt; b && b->next; b = b->next){
if((b->next->addr == addr) && (b->next->len == len)){
struct bkpt *t = b->next;
b->next = t->next;
free(t);
}
}
reply("OK", 2);
} else {
reply("", 0);
}
break;
case 'Z':
/* Set breakpoint */
if(cmd[i++] == '0'){
long addr;
long len;
struct bkpt *b;
char *p = &cmd[i];
p++; /* Skip the comma */
addr = strtol(p, &p, 16);
p++; /* Skip the other comma */
len = strtol(p, NULL, 16);
for(b = (struct bkpt *)&bkpt; b->next; b = b->next){
if((b->addr == addr) && (b->len == len)){
/* Duplicate; bail out */
break;
}
}
if(b->next){
/* Was a duplicate, do nothing */
} else {
struct bkpt *t = (struct bkpt *)malloc(sizeof(struct bkpt));
if(!t){
fprintf(stderr, "Failed to allocate a breakpoint structure\n");
spinsim_exit(1);
}
t->addr = addr;
t->len = len;
t->next = bkpt;
bkpt = t;
}
reply("OK", 2);
} else {
reply("", 0);
}
break;
case '?':
reply(halt_code, 3);
break;
default:
reply("", 0);
break;
}
}
out:
;
}
/*
+------------------------------------------------------------------------------------------------------------------------------+
| TERMS OF USE: MIT License |
+------------------------------------------------------------------------------------------------------------------------------+
|Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation |
|files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, |
|modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software|
|is furnished to do so, subject to the following conditions: |
| |
|The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.|
| |
|THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE |
|WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR |
|COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
|ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
+------------------------------------------------------------------------------------------------------------------------------+
*/