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
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
|
/* $NetBSD: kgdb_stub.c,v 1.3 1995/04/12 08:30:40 mycroft Exp $ */
/*
* Copyright (c) 1990, 1993
* The Regents of the University of California. All rights reserved.
*
* This software was developed by the Computer Systems Engineering group
* at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
* contributed to Berkeley.
*
* All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Lawrence Berkeley Laboratories.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* @(#)kgdb_stub.c 8.4 (Berkeley) 1/12/94
*/
/*
* "Stub" to allow remote cpu to debug over a serial line using gdb.
*/
#ifdef KGDB
#ifndef lint
static char rcsid[] = "$NetBSD: kgdb_stub.c,v 1.3 1995/04/12 08:30:40 mycroft Exp $";
#endif
#include <sys/param.h>
#include <sys/systm.h>
#include <machine/trap.h>
#include <machine/cpu.h>
#include <machine/psl.h>
#include <machine/reg.h>
#include <machine/frame.h>
#include <sys/buf.h>
#include <hp/dev/cons.h>
#include <hp300/hp300/kgdb_proto.h>
#include <machine/remote-sl.h>
extern int kernacc();
extern void chgkprot();
#ifndef KGDBDEV
#define KGDBDEV NODEV
#endif
#ifndef KGDBRATE
#define KGDBRATE 9600
#endif
dev_t kgdb_dev = KGDBDEV; /* remote debugging device (NODEV if none) */
int kgdb_rate = KGDBRATE; /* remote debugging baud rate */
int kgdb_active = 0; /* remote debugging active if != 0 */
int kgdb_debug_init = 0; /* != 0 waits for remote at system init */
int kgdb_debug_panic = 1; /* != 0 waits for remote on panic */
int kgdb_debug = 0;
#define GETC ((*kgdb_getc)(kgdb_dev))
#define PUTC(c) ((*kgdb_putc)(kgdb_dev, c))
#define PUTESC(c) { \
if (c == FRAME_END) { \
PUTC(FRAME_ESCAPE); \
c = TRANS_FRAME_END; \
} else if (c == FRAME_ESCAPE) { \
PUTC(FRAME_ESCAPE); \
c = TRANS_FRAME_ESCAPE; \
} else if (c == FRAME_START) { \
PUTC(FRAME_ESCAPE); \
c = TRANS_FRAME_START; \
} \
PUTC(c); \
}
static int (*kgdb_getc)();
static int (*kgdb_putc)();
/*
* Send a message. The host gets one chance to read it.
*/
static void
kgdb_send(type, bp, len)
register u_char type;
register u_char *bp;
register int len;
{
register u_char csum;
register u_char *ep = bp + len;
PUTC(FRAME_START);
PUTESC(type);
csum = type;
while (bp < ep) {
type = *bp++;
csum += type;
PUTESC(type)
}
csum = -csum;
PUTESC(csum)
PUTC(FRAME_END);
}
static int
kgdb_recv(bp, lenp)
u_char *bp;
int *lenp;
{
register u_char c, csum;
register int escape, len;
register int type;
restart:
csum = len = escape = 0;
type = -1;
while (1) {
c = GETC;
switch (c) {
case FRAME_ESCAPE:
escape = 1;
continue;
case TRANS_FRAME_ESCAPE:
if (escape)
c = FRAME_ESCAPE;
break;
case TRANS_FRAME_END:
if (escape)
c = FRAME_END;
break;
case TRANS_FRAME_START:
if (escape)
c = FRAME_START;
break;
case FRAME_START:
goto restart;
case FRAME_END:
if (type < 0 || --len < 0) {
csum = len = escape = 0;
type = -1;
continue;
}
if (csum != 0) {
return (0);
}
*lenp = len;
return type;
}
csum += c;
if (type < 0) {
type = c;
escape = 0;
continue;
}
if (++len > SL_RPCSIZE) {
while (GETC != FRAME_END)
;
return (0);
}
*bp++ = c;
escape = 0;
}
}
/*
* Translate a trap number into a unix compatible signal value.
* (gdb only understands unix signal numbers).
*/
static int
computeSignal(type)
int type;
{
int sigval;
switch (type) {
case T_BUSERR:
sigval = SIGBUS;
break;
case T_ADDRERR:
sigval = SIGBUS;
break;
case T_ILLINST:
sigval = SIGILL;
break;
case T_ZERODIV:
sigval = SIGFPE;
break;
case T_CHKINST:
sigval = SIGFPE;
break;
case T_TRAPVINST:
sigval = SIGFPE;
break;
case T_PRIVINST:
sigval = SIGILL;
break;
case T_TRACE:
sigval = SIGTRAP;
break;
case T_MMUFLT:
sigval = SIGSEGV;
break;
case T_SSIR:
sigval = SIGSEGV;
break;
case T_FMTERR:
sigval = SIGILL;
break;
case T_FPERR:
sigval = SIGFPE;
break;
case T_COPERR:
sigval = SIGFPE;
break;
case T_ASTFLT:
sigval = SIGINT;
break;
case T_TRAP15:
sigval = SIGTRAP;
break;
default:
sigval = SIGEMT;
break;
}
return (sigval);
}
/*
* Trap into kgdb to wait for debugger to connect,
* noting on the console why nothing else is going on.
*/
kgdb_connect(verbose)
int verbose;
{
if (verbose)
printf("kgdb waiting...");
/* trap into kgdb */
asm("trap #15;");
if (verbose)
printf("connected.\n");
}
/*
* Decide what to do on panic.
*/
kgdb_panic()
{
if (kgdb_active == 0 && kgdb_debug_panic && kgdb_dev != NODEV)
kgdb_connect(1);
}
/*
* Definitions exported from gdb.
*/
#define NUM_REGS 18
#define REGISTER_BYTES ((16+2)*4)
#define REGISTER_BYTE(N) ((N)*4)
#define GDB_SR 16
#define GDB_PC 17
static inline void
kgdb_copy(src, dst, nbytes)
register u_char *src, *dst;
register u_int nbytes;
{
register u_char *ep = src + nbytes;
while (src < ep)
*dst++ = *src++;
}
/*
* There is a short pad word between SP (A7) and SR which keeps the
* kernel stack long word aligned (note that this is in addition to
* the stack adjust short that we treat as the upper half of a longword
* SR). We must skip this when copying into and out of gdb.
*/
static inline void
regs_to_gdb(fp, regs)
struct frame *fp;
u_long *regs;
{
kgdb_copy((u_char *)fp->f_regs, (u_char *)regs, 16*4);
kgdb_copy((u_char *)&fp->f_stackadj, (u_char *)®s[GDB_SR], 2*4);
}
static inline void
gdb_to_regs(fp, regs)
struct frame *fp;
u_long *regs;
{
kgdb_copy((u_char *)regs, (u_char *)fp->f_regs, 16*4);
kgdb_copy((u_char *)®s[GDB_SR], (u_char *)&fp->f_stackadj, 2*4);
}
static u_long reg_cache[NUM_REGS];
static u_char inbuffer[SL_RPCSIZE+1];
static u_char outbuffer[SL_RPCSIZE];
/*
* This function does all command procesing for interfacing to
* a remote gdb.
*/
int
kgdb_trap(type, frame)
int type;
struct frame *frame;
{
register u_long len;
u_char *addr;
register u_char *cp;
register u_char out, in;
register int outlen;
int inlen;
u_long gdb_regs[NUM_REGS];
if ((int)kgdb_dev < 0) {
/* not debugging */
return (0);
}
if (kgdb_active == 0) {
if (type != T_TRAP15) {
/* No debugger active -- let trap handle this. */
return (0);
}
kgdb_getc = 0;
for (inlen = 0; constab[inlen].cn_probe; inlen++)
if (major(constab[inlen].cn_dev) == major(kgdb_dev)) {
kgdb_getc = constab[inlen].cn_getc;
kgdb_putc = constab[inlen].cn_putc;
break;
}
if (kgdb_getc == 0 || kgdb_putc == 0)
return (0);
/*
* If the packet that woke us up isn't an exec packet,
* ignore it since there is no active debugger. Also,
* we check that it's not an ack to be sure that the
* remote side doesn't send back a response after the
* local gdb has exited. Otherwise, the local host
* could trap into gdb if it's running a gdb kernel too.
*/
in = GETC;
/*
* If we came in asynchronously through the serial line,
* the framing character is eaten by the receive interrupt,
* but if we come in through a synchronous trap (i.e., via
* kgdb_connect()), we will see the extra character.
*/
if (in == FRAME_START)
in = GETC;
/*
* Check that this is a debugger exec message. If so,
* slurp up the entire message then ack it, and fall
* through to the recv loop.
*/
if (KGDB_CMD(in) != KGDB_EXEC || (in & KGDB_ACK) != 0)
return (0);
while (GETC != FRAME_END)
;
/*
* Do the printf *before* we ack the message. This way
* we won't drop any inbound characters while we're
* doing the polling printf.
*/
printf("kgdb started from device %x\n", kgdb_dev);
kgdb_send(in | KGDB_ACK, (u_char *)0, 0);
kgdb_active = 1;
}
/*
* Stick frame regs into our reg cache then tell remote host
* that an exception has occured.
*/
regs_to_gdb(frame, gdb_regs);
if (type != T_TRAP15) {
/*
* Only send an asynchronous SIGNAL message when we hit
* a breakpoint. Otherwise, we will drop the incoming
* packet while we output this one (and on entry the other
* side isn't interested in the SIGNAL type -- if it is,
* it will have used a signal packet.)
*/
outbuffer[0] = computeSignal(type);
kgdb_send(KGDB_SIGNAL, outbuffer, 1);
}
while (1) {
in = kgdb_recv(inbuffer, &inlen);
if (in == 0 || (in & KGDB_ACK))
/* Ignore inbound acks and error conditions. */
continue;
out = in | KGDB_ACK;
switch (KGDB_CMD(in)) {
case KGDB_SIGNAL:
/*
* if this command came from a running gdb,
* answer it -- the other guy has no way of
* knowing if we're in or out of this loop
* when he issues a "remote-signal". (Note
* that without the length check, we could
* loop here forever if the ourput line is
* looped back or the remote host is echoing.)
*/
if (inlen == 0) {
outbuffer[0] = computeSignal(type);
kgdb_send(KGDB_SIGNAL, outbuffer, 1);
}
continue;
case KGDB_REG_R:
case KGDB_REG_R | KGDB_DELTA:
cp = outbuffer;
outlen = 0;
for (len = inbuffer[0]; len < NUM_REGS; ++len) {
if (reg_cache[len] != gdb_regs[len] ||
(in & KGDB_DELTA) == 0) {
if (outlen + 5 > SL_MAXDATA) {
out |= KGDB_MORE;
break;
}
cp[outlen] = len;
kgdb_copy((u_char *)&gdb_regs[len],
&cp[outlen + 1], 4);
reg_cache[len] = gdb_regs[len];
outlen += 5;
}
}
break;
case KGDB_REG_W:
case KGDB_REG_W | KGDB_DELTA:
cp = inbuffer;
for (len = 0; len < inlen; len += 5) {
register int j = cp[len];
kgdb_copy(&cp[len + 1],
(u_char *)&gdb_regs[j], 4);
reg_cache[j] = gdb_regs[j];
}
gdb_to_regs(frame, gdb_regs);
outlen = 0;
break;
case KGDB_MEM_R:
len = inbuffer[0];
kgdb_copy(&inbuffer[1], (u_char *)&addr, 4);
if (len > SL_MAXDATA) {
outlen = 1;
outbuffer[0] = E2BIG;
} else if (!kgdb_acc(addr, len, B_READ)) {
outlen = 1;
outbuffer[0] = EFAULT;
} else {
outlen = len + 1;
outbuffer[0] = 0;
kgdb_copy(addr, &outbuffer[1], len);
}
break;
case KGDB_MEM_W:
len = inlen - 4;
kgdb_copy(inbuffer, (u_char *)&addr, 4);
outlen = 1;
if (!kgdb_acc(addr, len, B_READ))
outbuffer[0] = EFAULT;
else {
outbuffer[0] = 0;
if (!kgdb_acc(addr, len, B_WRITE))
chgkprot(addr, len, B_WRITE);
kgdb_copy(&inbuffer[4], addr, len);
ICIA();
}
break;
case KGDB_KILL:
kgdb_active = 0;
printf("kgdb detached\n");
/* fall through */
case KGDB_CONT:
kgdb_send(out, 0, 0);
frame->f_sr &=~ PSL_T;
return (1);
case KGDB_STEP:
kgdb_send(out, 0, 0);
frame->f_sr |= PSL_T;
return (1);
case KGDB_EXEC:
default:
/* Unknown command. Ack with a null message. */
outlen = 0;
break;
}
/* Send the reply */
kgdb_send(out, outbuffer, outlen);
}
}
/*
* XXX do kernacc call if safe, otherwise attempt
* to simulate by simple bounds-checking.
*/
kgdb_acc(addr, len, rw)
caddr_t addr;
int len, rw;
{
extern char proc0paddr[], kstack[]; /* XXX */
extern char *kernel_map; /* XXX! */
if (kernel_map != NULL)
return (kernacc(addr, len, rw));
if (addr < proc0paddr + USPACE ||
kstack <= addr && addr < kstack + USPACE)
return (1);
return (0);
}
#endif /* KGDB */
|