fix unaligned memory offset and some small fixes to interrupt binning
[gem5.git] / base / remote_gdb.cc
1 /*
2 * Copyright (c) 2002-2004 The Regents of The University of Michigan
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met: redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer;
9 * redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution;
12 * neither the name of the copyright holders nor the names of its
13 * contributors may be used to endorse or promote products derived from
14 * this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 /*
30 * Copyright (c) 1990, 1993
31 * The Regents of the University of California. All rights reserved.
32 *
33 * This software was developed by the Computer Systems Engineering group
34 * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
35 * contributed to Berkeley.
36 *
37 * All advertising materials mentioning features or use of this software
38 * must display the following acknowledgement:
39 * This product includes software developed by the University of
40 * California, Lawrence Berkeley Laboratories.
41 *
42 * Redistribution and use in source and binary forms, with or without
43 * modification, are permitted provided that the following conditions
44 * are met:
45 * 1. Redistributions of source code must retain the above copyright
46 * notice, this list of conditions and the following disclaimer.
47 * 2. Redistributions in binary form must reproduce the above copyright
48 * notice, this list of conditions and the following disclaimer in the
49 * documentation and/or other materials provided with the distribution.
50 * 3. All advertising materials mentioning features or use of this software
51 * must display the following acknowledgement:
52 * This product includes software developed by the University of
53 * California, Berkeley and its contributors.
54 * 4. Neither the name of the University nor the names of its contributors
55 * may be used to endorse or promote products derived from this software
56 * without specific prior written permission.
57 *
58 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
59 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
60 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
61 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
62 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
63 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
64 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
65 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
66 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
67 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
68 * SUCH DAMAGE.
69 *
70 * @(#)kgdb_stub.c 8.4 (Berkeley) 1/12/94
71 */
72
73 /*-
74 * Copyright (c) 2001 The NetBSD Foundation, Inc.
75 * All rights reserved.
76 *
77 * This code is derived from software contributed to The NetBSD Foundation
78 * by Jason R. Thorpe.
79 *
80 * Redistribution and use in source and binary forms, with or without
81 * modification, are permitted provided that the following conditions
82 * are met:
83 * 1. Redistributions of source code must retain the above copyright
84 * notice, this list of conditions and the following disclaimer.
85 * 2. Redistributions in binary form must reproduce the above copyright
86 * notice, this list of conditions and the following disclaimer in the
87 * documentation and/or other materials provided with the distribution.
88 * 3. All advertising materials mentioning features or use of this software
89 * must display the following acknowledgement:
90 * This product includes software developed by the NetBSD
91 * Foundation, Inc. and its contributors.
92 * 4. Neither the name of The NetBSD Foundation nor the names of its
93 * contributors may be used to endorse or promote products derived
94 * from this software without specific prior written permission.
95 *
96 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
97 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
98 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
99 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
100 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
101 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
102 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
103 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
104 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
105 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
106 * POSSIBILITY OF SUCH DAMAGE.
107 */
108
109 /*
110 * $NetBSD: kgdb_stub.c,v 1.8 2001/07/07 22:58:00 wdk Exp $
111 *
112 * Taken from NetBSD
113 *
114 * "Stub" to allow remote cpu to debug over a serial line using gdb.
115 */
116
117 #include <sys/signal.h>
118
119 #include <unistd.h>
120
121 #include <cstdio>
122 #include <string>
123
124 #include "cpu/exec_context.hh"
125 #include "base/intmath.hh"
126 #include "base/kgdb.h"
127
128 #include "mem/functional_mem/physical_memory.hh"
129 #include "base/remote_gdb.hh"
130 #include "base/socket.hh"
131 #include "base/trace.hh"
132 #include "targetarch/vtophys.hh"
133 #include "sim/system.hh"
134 #include "cpu/static_inst.hh"
135
136 using namespace std;
137
138 #ifdef DEBUG
139 RemoteGDB *theDebugger = NULL;
140
141 void
142 debugger()
143 {
144 if (theDebugger)
145 theDebugger->trap(ALPHA_KENTRY_IF);
146 }
147 #endif
148
149 ///////////////////////////////////////////////////////////
150 //
151 //
152 //
153
154 GDBListener::Event::Event(GDBListener *l, int fd, int e)
155 : PollEvent(fd, e), listener(l)
156 {}
157
158 void
159 GDBListener::Event::process(int revent)
160 {
161 listener->accept();
162 }
163
164 GDBListener::GDBListener(RemoteGDB *g, int p)
165 : event(NULL), gdb(g), port(p)
166 {}
167
168 GDBListener::~GDBListener()
169 {
170 if (event)
171 delete event;
172 }
173
174 string
175 GDBListener::name()
176 {
177 return gdb->name() + ".listener";
178 }
179
180 void
181 GDBListener::listen()
182 {
183 while (!listener.listen(port, true)) {
184 DPRINTF(GDBMisc, "Can't bind port %d\n", port);
185 port++;
186 }
187
188 cerr << "Listening for remote gdb connection on port " << port << endl;
189 event = new Event(this, listener.getfd(), POLLIN);
190 pollQueue.schedule(event);
191 }
192
193 void
194 GDBListener::accept()
195 {
196 if (!listener.islistening())
197 panic("GDBListener::accept(): cannot accept if we're not listening!");
198
199 int sfd = listener.accept(true);
200
201 if (sfd != -1) {
202 if (gdb->isattached())
203 close(sfd);
204 else
205 gdb->attach(sfd);
206 }
207 }
208
209 ///////////////////////////////////////////////////////////
210 //
211 //
212 //
213 int digit2i(char);
214 char i2digit(int);
215 void mem2hex(void *, const void *, int);
216 const char *hex2mem(void *, const char *, int);
217 Addr hex2i(const char **);
218
219 RemoteGDB::Event::Event(RemoteGDB *g, int fd, int e)
220 : PollEvent(fd, e), gdb(g)
221 {}
222
223 void
224 RemoteGDB::Event::process(int revent)
225 {
226 if (revent & POLLIN)
227 gdb->trap(ALPHA_KENTRY_IF);
228 else if (revent & POLLNVAL)
229 gdb->detach();
230 }
231
232 RemoteGDB::RemoteGDB(System *_system, ExecContext *c)
233 : event(NULL), fd(-1), active(false), attached(false),
234 system(_system), pmem(_system->physmem), context(c)
235 {
236 memset(gdbregs, 0, sizeof(gdbregs));
237 }
238
239 RemoteGDB::~RemoteGDB()
240 {
241 if (event)
242 delete event;
243 }
244
245 string
246 RemoteGDB::name()
247 {
248 return system->name() + ".remote_gdb";
249 }
250
251 bool
252 RemoteGDB::isattached()
253 { return attached; }
254
255 void
256 RemoteGDB::attach(int f)
257 {
258 fd = f;
259
260 event = new Event(this, fd, POLLIN);
261 pollQueue.schedule(event);
262
263 attached = true;
264 DPRINTFN("remote gdb attached\n");
265 #ifdef DEBUG
266 theDebugger = this;
267 #endif
268 }
269
270 void
271 RemoteGDB::detach()
272 {
273 attached = false;
274 close(fd);
275 fd = -1;
276
277 pollQueue.remove(event);
278 DPRINTFN("remote gdb detached\n");
279 }
280
281 const char *
282 gdb_command(char cmd)
283 {
284 switch (cmd) {
285 case KGDB_SIGNAL: return "KGDB_SIGNAL";
286 case KGDB_SET_BAUD: return "KGDB_SET_BAUD";
287 case KGDB_SET_BREAK: return "KGDB_SET_BREAK";
288 case KGDB_CONT: return "KGDB_CONT";
289 case KGDB_ASYNC_CONT: return "KGDB_ASYNC_CONT";
290 case KGDB_DEBUG: return "KGDB_DEBUG";
291 case KGDB_DETACH: return "KGDB_DETACH";
292 case KGDB_REG_R: return "KGDB_REG_R";
293 case KGDB_REG_W: return "KGDB_REG_W";
294 case KGDB_SET_THREAD: return "KGDB_SET_THREAD";
295 case KGDB_CYCLE_STEP: return "KGDB_CYCLE_STEP";
296 case KGDB_SIG_CYCLE_STEP: return "KGDB_SIG_CYCLE_STEP";
297 case KGDB_KILL: return "KGDB_KILL";
298 case KGDB_MEM_W: return "KGDB_MEM_W";
299 case KGDB_MEM_R: return "KGDB_MEM_R";
300 case KGDB_SET_REG: return "KGDB_SET_REG";
301 case KGDB_READ_REG: return "KGDB_READ_REG";
302 case KGDB_QUERY_VAR: return "KGDB_QUERY_VAR";
303 case KGDB_SET_VAR: return "KGDB_SET_VAR";
304 case KGDB_RESET: return "KGDB_RESET";
305 case KGDB_STEP: return "KGDB_STEP";
306 case KGDB_ASYNC_STEP: return "KGDB_ASYNC_STEP";
307 case KGDB_THREAD_ALIVE: return "KGDB_THREAD_ALIVE";
308 case KGDB_TARGET_EXIT: return "KGDB_TARGET_EXIT";
309 case KGDB_BINARY_DLOAD: return "KGDB_BINARY_DLOAD";
310 case KGDB_CLR_HW_BKPT: return "KGDB_CLR_HW_BKPT";
311 case KGDB_SET_HW_BKPT: return "KGDB_SET_HW_BKPT";
312 case KGDB_START: return "KGDB_START";
313 case KGDB_END: return "KGDB_END";
314 case KGDB_GOODP: return "KGDB_GOODP";
315 case KGDB_BADP: return "KGDB_BADP";
316 default: return "KGDB_UNKNOWN";
317 }
318 }
319
320 ///////////////////////////////////////////////////////////
321 // RemoteGDB::acc
322 //
323 // Determine if the mapping at va..(va+len) is valid.
324 //
325 bool
326 RemoteGDB::acc(Addr va, size_t len)
327 {
328 Addr last_va;
329 Addr pte;
330
331 va = alpha_trunc_page(va);
332 last_va = alpha_round_page(va + len);
333
334 do {
335 if (va >= ALPHA_K0SEG_BASE && va < ALPHA_K1SEG_BASE) {
336 if (va < (ALPHA_K0SEG_BASE + pmem->size())) {
337 DPRINTF(GDBAcc, "acc: Mapping is valid K0SEG <= "
338 "%#x < K0SEG + size\n", va);
339 return true;
340 } else {
341 DPRINTF(GDBAcc, "acc: Mapping invalid %#x > K0SEG + size\n",
342 va);
343 return false;
344 }
345 }
346
347 /**
348 * This code says that all accesses to palcode (instruction and data)
349 * are valid since there isn't a va->pa mapping because palcode is
350 * accessed physically. At some point this should probably be cleaned up
351 * but there is no easy way to do it.
352 */
353
354 if (PC_PAL(va) || va < 0x10000)
355 return true;
356
357 Addr ptbr = context->regs.ipr[AlphaISA::IPR_PALtemp20];
358 pte = kernel_pte_lookup(pmem, ptbr, va);
359 if (!pte || !entry_valid(pmem->phys_read_qword(pte))) {
360 DPRINTF(GDBAcc, "acc: %#x pte is invalid\n", va);
361 return false;
362 }
363 va += ALPHA_PGBYTES;
364 } while (va < last_va);
365
366 DPRINTF(GDBAcc, "acc: %#x mapping is valid\n", va);
367 return true;
368 }
369
370 ///////////////////////////////////////////////////////////
371 // RemoteGDB::signal
372 //
373 // Translate a trap number into a Unix-compatible signal number.
374 // (GDB only understands Unix signal numbers.)
375 //
376 int
377 RemoteGDB::signal(int type)
378 {
379 switch (type) {
380 case ALPHA_KENTRY_INT:
381 return (SIGTRAP);
382
383 case ALPHA_KENTRY_UNA:
384 return (SIGBUS);
385
386 case ALPHA_KENTRY_ARITH:
387 return (SIGFPE);
388
389 case ALPHA_KENTRY_IF:
390 return (SIGILL);
391
392 case ALPHA_KENTRY_MM:
393 return (SIGSEGV);
394
395 default:
396 panic("unknown signal type");
397 return 0;
398 }
399 }
400
401 ///////////////////////////////////////////////////////////
402 // RemoteGDB::getregs
403 //
404 // Translate the kernel debugger register format into
405 // the GDB register format.
406 void
407 RemoteGDB::getregs()
408 {
409 memset(gdbregs, 0, sizeof(gdbregs));
410 memcpy(&gdbregs[KGDB_REG_V0], context->regs.intRegFile, 32 * sizeof(uint64_t));
411 #ifdef KGDB_FP_REGS
412 memcpy(&gdbregs[KGDB_REG_F0], context->regs.floatRegFile.q,
413 32 * sizeof(uint64_t));
414 #endif
415 gdbregs[KGDB_REG_PC] = context->regs.pc;
416 }
417
418 ///////////////////////////////////////////////////////////
419 // RemoteGDB::setregs
420 //
421 // Translate the GDB register format into the kernel
422 // debugger register format.
423 //
424 void
425 RemoteGDB::setregs()
426 {
427 memcpy(context->regs.intRegFile, &gdbregs[KGDB_REG_V0],
428 32 * sizeof(uint64_t));
429 #ifdef KGDB_FP_REGS
430 memcpy(context->regs.floatRegFile.q, &gdbregs[KGDB_REG_F0],
431 32 * sizeof(uint64_t));
432 #endif
433 context->regs.pc = gdbregs[KGDB_REG_PC];
434 }
435
436 void
437 RemoteGDB::setTempBreakpoint(TempBreakpoint &bkpt, Addr addr)
438 {
439 DPRINTF(GDBMisc, "setTempBreakpoint: addr=%#x\n", addr);
440
441 bkpt.address = addr;
442 insertHardBreak(addr, 4);
443 }
444
445 void
446 RemoteGDB::clearTempBreakpoint(TempBreakpoint &bkpt)
447 {
448 DPRINTF(GDBMisc, "setTempBreakpoint: addr=%#x\n",
449 bkpt.address);
450
451
452 removeHardBreak(bkpt.address, 4);
453 bkpt.address = 0;
454 }
455
456 void
457 RemoteGDB::clearSingleStep()
458 {
459 DPRINTF(GDBMisc, "clearSingleStep bt_addr=%#x nt_addr=%#x\n",
460 takenBkpt.address, notTakenBkpt.address);
461
462 if (takenBkpt.address != 0)
463 clearTempBreakpoint(takenBkpt);
464
465 if (notTakenBkpt.address != 0)
466 clearTempBreakpoint(notTakenBkpt);
467 }
468
469 void
470 RemoteGDB::setSingleStep()
471 {
472 Addr pc = context->regs.pc;
473 Addr npc, bpc;
474 bool set_bt = false;
475
476 npc = pc + sizeof(MachInst);
477
478 // User was stopped at pc, e.g. the instruction at pc was not
479 // executed.
480 MachInst inst = read<MachInst>(pc);
481 StaticInstPtr<TheISA> si(inst);
482 if (si->hasBranchTarget(pc, context, bpc)) {
483 // Don't bother setting a breakpoint on the taken branch if it
484 // is the same as the next pc
485 if (bpc != npc)
486 set_bt = true;
487 }
488
489 DPRINTF(GDBMisc, "setSingleStep bt_addr=%#x nt_addr=%#x\n",
490 takenBkpt.address, notTakenBkpt.address);
491
492 setTempBreakpoint(notTakenBkpt, npc);
493
494 if (set_bt)
495 setTempBreakpoint(takenBkpt, bpc);
496 }
497
498 /////////////////////////
499 //
500 //
501
502 uint8_t
503 RemoteGDB::getbyte()
504 {
505 uint8_t b;
506 ::read(fd, &b, 1);
507 return b;
508 }
509
510 void
511 RemoteGDB::putbyte(uint8_t b)
512 {
513 ::write(fd, &b, 1);
514 }
515
516 // Send a packet to gdb
517 void
518 RemoteGDB::send(const char *bp)
519 {
520 const char *p;
521 uint8_t csum, c;
522
523 DPRINTF(GDBSend, "send: %s\n", bp);
524
525 do {
526 p = bp;
527 putbyte(KGDB_START);
528 for (csum = 0; (c = *p); p++) {
529 putbyte(c);
530 csum += c;
531 }
532 putbyte(KGDB_END);
533 putbyte(i2digit(csum >> 4));
534 putbyte(i2digit(csum));
535 } while ((c = getbyte() & 0x7f) == KGDB_BADP);
536 }
537
538 // Receive a packet from gdb
539 int
540 RemoteGDB::recv(char *bp, int maxlen)
541 {
542 char *p;
543 int c, csum;
544 int len;
545
546 do {
547 p = bp;
548 csum = len = 0;
549 while ((c = getbyte()) != KGDB_START)
550 ;
551
552 while ((c = getbyte()) != KGDB_END && len < maxlen) {
553 c &= 0x7f;
554 csum += c;
555 *p++ = c;
556 len++;
557 }
558 csum &= 0xff;
559 *p = '\0';
560
561 if (len >= maxlen) {
562 putbyte(KGDB_BADP);
563 continue;
564 }
565
566 csum -= digit2i(getbyte()) * 16;
567 csum -= digit2i(getbyte());
568
569 if (csum == 0) {
570 putbyte(KGDB_GOODP);
571 // Sequence present?
572 if (bp[2] == ':') {
573 putbyte(bp[0]);
574 putbyte(bp[1]);
575 len -= 3;
576 bcopy(bp + 3, bp, len);
577 }
578 break;
579 }
580 putbyte(KGDB_BADP);
581 } while (1);
582
583 DPRINTF(GDBRecv, "recv: %s: %s\n", gdb_command(*bp), bp);
584
585 return (len);
586 }
587
588 // Read bytes from kernel address space for debugger.
589 bool
590 RemoteGDB::read(Addr vaddr, size_t size, char *data)
591 {
592 static Addr lastaddr = 0;
593 static size_t lastsize = 0;
594
595 uint8_t *maddr;
596
597 if (vaddr < 10) {
598 DPRINTF(GDBRead, "read: reading memory location zero!\n");
599 vaddr = lastaddr + lastsize;
600 }
601
602 DPRINTF(GDBRead, "read: addr=%#x, size=%d", vaddr, size);
603 #if TRACING_ON
604 char *d = data;
605 size_t s = size;
606 #endif
607
608 lastaddr = vaddr;
609 lastsize = size;
610
611 size_t count = min((Addr)size,
612 VMPageSize - (vaddr & (VMPageSize - 1)));
613
614 maddr = vtomem(context, vaddr, count);
615 memcpy(data, maddr, count);
616
617 vaddr += count;
618 data += count;
619 size -= count;
620
621 while (size >= VMPageSize) {
622 maddr = vtomem(context, vaddr, count);
623 memcpy(data, maddr, VMPageSize);
624
625 vaddr += VMPageSize;
626 data += VMPageSize;
627 size -= VMPageSize;
628 }
629
630 if (size > 0) {
631 maddr = vtomem(context, vaddr, count);
632 memcpy(data, maddr, size);
633 }
634
635 #if TRACING_ON
636 if (DTRACE(GDBRead)) {
637 if (DTRACE(GDBExtra)) {
638 char buf[1024];
639 mem2hex(buf, d, s);
640 DPRINTFNR(": %s\n", buf);
641 } else
642 DPRINTFNR("\n");
643 }
644 #endif
645
646 return true;
647 }
648
649 // Write bytes to kernel address space for debugger.
650 bool
651 RemoteGDB::write(Addr vaddr, size_t size, const char *data)
652 {
653 static Addr lastaddr = 0;
654 static size_t lastsize = 0;
655
656 uint8_t *maddr;
657
658 if (vaddr < 10) {
659 DPRINTF(GDBWrite, "write: writing memory location zero!\n");
660 vaddr = lastaddr + lastsize;
661 }
662
663 if (DTRACE(GDBWrite)) {
664 DPRINTFN("write: addr=%#x, size=%d", vaddr, size);
665 if (DTRACE(GDBExtra)) {
666 char buf[1024];
667 mem2hex(buf, data, size);
668 DPRINTFNR(": %s\n", buf);
669 } else
670 DPRINTFNR("\n");
671 }
672
673 lastaddr = vaddr;
674 lastsize = size;
675
676 size_t count = min((Addr)size,
677 VMPageSize - (vaddr & (VMPageSize - 1)));
678
679 maddr = vtomem(context, vaddr, count);
680 memcpy(maddr, data, count);
681
682 vaddr += count;
683 data += count;
684 size -= count;
685
686 while (size >= VMPageSize) {
687 maddr = vtomem(context, vaddr, count);
688 memcpy(maddr, data, VMPageSize);
689
690 vaddr += VMPageSize;
691 data += VMPageSize;
692 size -= VMPageSize;
693 }
694
695 if (size > 0) {
696 maddr = vtomem(context, vaddr, count);
697 memcpy(maddr, data, size);
698 }
699
700 #ifdef IMB
701 alpha_pal_imb();
702 #endif
703
704 return true;
705 }
706
707
708 PCEventQueue *RemoteGDB::getPcEventQueue()
709 {
710 return &system->pcEventQueue;
711 }
712
713
714 RemoteGDB::HardBreakpoint::HardBreakpoint(RemoteGDB *_gdb, Addr pc)
715 : PCEvent(_gdb->getPcEventQueue(), "HardBreakpoint Event", pc),
716 gdb(_gdb), refcount(0)
717 {
718 DPRINTF(GDBMisc, "creating hardware breakpoint at %#x\n", evpc);
719 schedule();
720 }
721
722 void
723 RemoteGDB::HardBreakpoint::process(ExecContext *xc)
724 {
725 DPRINTF(GDBMisc, "handling hardware breakpoint at %#x\n", pc());
726
727 if (xc == gdb->context)
728 gdb->trap(ALPHA_KENTRY_INT);
729 }
730
731 bool
732 RemoteGDB::insertSoftBreak(Addr addr, size_t len)
733 {
734 if (len != sizeof(MachInst))
735 panic("invalid length\n");
736
737 return insertHardBreak(addr, len);
738 }
739
740 bool
741 RemoteGDB::removeSoftBreak(Addr addr, size_t len)
742 {
743 if (len != sizeof(MachInst))
744 panic("invalid length\n");
745
746 return removeHardBreak(addr, len);
747 }
748
749 bool
750 RemoteGDB::insertHardBreak(Addr addr, size_t len)
751 {
752 if (len != sizeof(MachInst))
753 panic("invalid length\n");
754
755 DPRINTF(GDBMisc, "inserting hardware breakpoint at %#x\n", addr);
756
757 HardBreakpoint *&bkpt = hardBreakMap[addr];
758 if (bkpt == 0)
759 bkpt = new HardBreakpoint(this, addr);
760
761 bkpt->refcount++;
762
763 return true;
764 }
765
766 bool
767 RemoteGDB::removeHardBreak(Addr addr, size_t len)
768 {
769 if (len != sizeof(MachInst))
770 panic("invalid length\n");
771
772 DPRINTF(GDBMisc, "removing hardware breakpoint at %#x\n", addr);
773
774 break_iter_t i = hardBreakMap.find(addr);
775 if (i == hardBreakMap.end())
776 return false;
777
778 HardBreakpoint *hbp = (*i).second;
779 if (--hbp->refcount == 0) {
780 delete hbp;
781 hardBreakMap.erase(i);
782 }
783
784 return true;
785 }
786
787 const char *
788 break_type(char c)
789 {
790 switch(c) {
791 case '0': return "software breakpoint";
792 case '1': return "hardware breakpoint";
793 case '2': return "write watchpoint";
794 case '3': return "read watchpoint";
795 case '4': return "access watchpoint";
796 default: return "unknown breakpoint/watchpoint";
797 }
798 }
799
800 // This function does all command processing for interfacing to a
801 // remote gdb. Note that the error codes are ignored by gdb at
802 // present, but might eventually become meaningful. (XXX) It might
803 // makes sense to use POSIX errno values, because that is what the
804 // gdb/remote.c functions want to return.
805 bool
806 RemoteGDB::trap(int type)
807 {
808 uint64_t val;
809 size_t datalen, len;
810 char data[KGDB_BUFLEN + 1];
811 char buffer[sizeof(gdbregs) * 2 + 256];
812 char temp[KGDB_BUFLEN];
813 const char *p;
814 char command, subcmd;
815 string var;
816 bool ret;
817
818 if (!attached)
819 return false;
820
821 DPRINTF(GDBMisc, "trap: PC=%#x NPC=%#x\n",
822 context->regs.pc, context->regs.npc);
823
824 clearSingleStep();
825
826 /*
827 * The first entry to this function is normally through
828 * a breakpoint trap in kgdb_connect(), in which case we
829 * must advance past the breakpoint because gdb will not.
830 *
831 * On the first entry here, we expect that gdb is not yet
832 * listening to us, so just enter the interaction loop.
833 * After the debugger is "active" (connected) it will be
834 * waiting for a "signaled" message from us.
835 */
836 if (!active)
837 active = true;
838 else
839 // Tell remote host that an exception has occurred.
840 sprintf((char *)buffer, "S%02x", signal(type));
841 send(buffer);
842
843 // Stick frame regs into our reg cache.
844 getregs();
845
846 for (;;) {
847 datalen = recv(data, sizeof(data));
848 data[sizeof(data) - 1] = 0; // Sentinel
849 command = data[0];
850 subcmd = 0;
851 p = data + 1;
852 switch (command) {
853
854 case KGDB_SIGNAL:
855 // if this command came from a running gdb, answer it --
856 // the other guy has no way of knowing if we're in or out
857 // of this loop when he issues a "remote-signal".
858 sprintf((char *)buffer, "S%02x", signal(type));
859 send(buffer);
860 continue;
861
862 case KGDB_REG_R:
863 if (2 * sizeof(gdbregs) > sizeof(buffer))
864 panic("buffer too small");
865
866 mem2hex(buffer, gdbregs, sizeof(gdbregs));
867 send(buffer);
868 continue;
869
870 case KGDB_REG_W:
871 p = hex2mem(gdbregs, p, sizeof(gdbregs));
872 if (p == NULL || *p != '\0')
873 send("E01");
874 else {
875 setregs();
876 send("OK");
877 }
878 continue;
879
880 #if 0
881 case KGDB_SET_REG:
882 val = hex2i(&p);
883 if (*p++ != '=') {
884 send("E01");
885 continue;
886 }
887 if (val < 0 && val >= KGDB_NUMREGS) {
888 send("E01");
889 continue;
890 }
891
892 gdbregs[val] = hex2i(&p);
893 setregs();
894 send("OK");
895
896 continue;
897 #endif
898
899 case KGDB_MEM_R:
900 val = hex2i(&p);
901 if (*p++ != ',') {
902 send("E02");
903 continue;
904 }
905 len = hex2i(&p);
906 if (*p != '\0') {
907 send("E03");
908 continue;
909 }
910 if (len > sizeof(buffer)) {
911 send("E04");
912 continue;
913 }
914 if (!acc(val, len)) {
915 send("E05");
916 continue;
917 }
918
919 if (read(val, (size_t)len, (char *)buffer)) {
920 mem2hex(temp, buffer, len);
921 send(temp);
922 } else {
923 send("E05");
924 }
925 continue;
926
927 case KGDB_MEM_W:
928 val = hex2i(&p);
929 if (*p++ != ',') {
930 send("E06");
931 continue;
932 }
933 len = hex2i(&p);
934 if (*p++ != ':') {
935 send("E07");
936 continue;
937 }
938 if (len > datalen - (p - data)) {
939 send("E08");
940 continue;
941 }
942 p = hex2mem(buffer, p, sizeof(buffer));
943 if (p == NULL) {
944 send("E09");
945 continue;
946 }
947 if (!acc(val, len)) {
948 send("E0A");
949 continue;
950 }
951 if (write(val, (size_t)len, (char *)buffer))
952 send("OK");
953 else
954 send("E0B");
955 continue;
956
957 case KGDB_SET_THREAD:
958 subcmd = *p++;
959 val = hex2i(&p);
960 if (val == 0)
961 send("OK");
962 else
963 send("E01");
964 continue;
965
966 case KGDB_DETACH:
967 case KGDB_KILL:
968 active = false;
969 clearSingleStep();
970 detach();
971 goto out;
972
973 case KGDB_ASYNC_CONT:
974 subcmd = hex2i(&p);
975 if (*p++ == ';') {
976 val = hex2i(&p);
977 context->regs.pc = val;
978 context->regs.npc = val + sizeof(MachInst);
979 }
980 clearSingleStep();
981 goto out;
982
983 case KGDB_CONT:
984 if (p - data < datalen) {
985 val = hex2i(&p);
986 context->regs.pc = val;
987 context->regs.npc = val + sizeof(MachInst);
988 }
989 clearSingleStep();
990 goto out;
991
992 case KGDB_ASYNC_STEP:
993 subcmd = hex2i(&p);
994 if (*p++ == ';') {
995 val = hex2i(&p);
996 context->regs.pc = val;
997 context->regs.npc = val + sizeof(MachInst);
998 }
999 setSingleStep();
1000 goto out;
1001
1002 case KGDB_STEP:
1003 if (p - data < datalen) {
1004 val = hex2i(&p);
1005 context->regs.pc = val;
1006 context->regs.npc = val + sizeof(MachInst);
1007 }
1008 setSingleStep();
1009 goto out;
1010
1011 case KGDB_CLR_HW_BKPT:
1012 subcmd = *p++;
1013 if (*p++ != ',') send("E0D");
1014 val = hex2i(&p);
1015 if (*p++ != ',') send("E0D");
1016 len = hex2i(&p);
1017
1018 DPRINTF(GDBMisc, "clear %s, addr=%#x, len=%d\n",
1019 break_type(subcmd), val, len);
1020
1021 ret = false;
1022
1023 switch (subcmd) {
1024 case '0': // software breakpoint
1025 ret = removeSoftBreak(val, len);
1026 break;
1027
1028 case '1': // hardware breakpoint
1029 ret = removeHardBreak(val, len);
1030 break;
1031
1032 case '2': // write watchpoint
1033 case '3': // read watchpoint
1034 case '4': // access watchpoint
1035 default: // unknown
1036 send("");
1037 break;
1038 }
1039
1040 send(ret ? "OK" : "E0C");
1041 continue;
1042
1043 case KGDB_SET_HW_BKPT:
1044 subcmd = *p++;
1045 if (*p++ != ',') send("E0D");
1046 val = hex2i(&p);
1047 if (*p++ != ',') send("E0D");
1048 len = hex2i(&p);
1049
1050 DPRINTF(GDBMisc, "set %s, addr=%#x, len=%d\n",
1051 break_type(subcmd), val, len);
1052
1053 ret = false;
1054
1055 switch (subcmd) {
1056 case '0': // software breakpoint
1057 ret = insertSoftBreak(val, len);
1058 break;
1059
1060 case '1': // hardware breakpoint
1061 ret = insertHardBreak(val, len);
1062 break;
1063
1064 case '2': // write watchpoint
1065 case '3': // read watchpoint
1066 case '4': // access watchpoint
1067 default: // unknown
1068 send("");
1069 break;
1070 }
1071
1072 send(ret ? "OK" : "E0C");
1073 continue;
1074
1075 case KGDB_QUERY_VAR:
1076 var = string(p, datalen - 1);
1077 if (var == "C")
1078 send("QC0");
1079 else
1080 send("");
1081 continue;
1082
1083 case KGDB_SET_BAUD:
1084 case KGDB_SET_BREAK:
1085 case KGDB_DEBUG:
1086 case KGDB_CYCLE_STEP:
1087 case KGDB_SIG_CYCLE_STEP:
1088 case KGDB_READ_REG:
1089 case KGDB_SET_VAR:
1090 case KGDB_RESET:
1091 case KGDB_THREAD_ALIVE:
1092 case KGDB_TARGET_EXIT:
1093 case KGDB_BINARY_DLOAD:
1094 // Unsupported command
1095 DPRINTF(GDBMisc, "Unsupported command: %s\n",
1096 gdb_command(command));
1097 DDUMP(GDBMisc, (uint8_t *)data, datalen);
1098 send("");
1099 continue;
1100
1101 default:
1102 // Unknown command.
1103 DPRINTF(GDBMisc, "Unknown command: %c(%#x)\n",
1104 command, command);
1105 send("");
1106 continue;
1107
1108
1109 }
1110 }
1111
1112 out:
1113 return true;
1114 }
1115
1116 // Convert a hex digit into an integer.
1117 // This returns -1 if the argument passed is no valid hex digit.
1118 int
1119 digit2i(char c)
1120 {
1121 if (c >= '0' && c <= '9')
1122 return (c - '0');
1123 else if (c >= 'a' && c <= 'f')
1124 return (c - 'a' + 10);
1125 else if (c >= 'A' && c <= 'F')
1126
1127 return (c - 'A' + 10);
1128 else
1129 return (-1);
1130 }
1131
1132 // Convert the low 4 bits of an integer into an hex digit.
1133 char
1134 i2digit(int n)
1135 {
1136 return ("0123456789abcdef"[n & 0x0f]);
1137 }
1138
1139 // Convert a byte array into an hex string.
1140 void
1141 mem2hex(void *vdst, const void *vsrc, int len)
1142 {
1143 char *dst = (char *)vdst;
1144 const char *src = (const char *)vsrc;
1145
1146 while (len--) {
1147 *dst++ = i2digit(*src >> 4);
1148 *dst++ = i2digit(*src++);
1149 }
1150 *dst = '\0';
1151 }
1152
1153 // Convert an hex string into a byte array.
1154 // This returns a pointer to the character following the last valid
1155 // hex digit. If the string ends in the middle of a byte, NULL is
1156 // returned.
1157 const char *
1158 hex2mem(void *vdst, const char *src, int maxlen)
1159 {
1160 char *dst = (char *)vdst;
1161 int msb, lsb;
1162
1163 while (*src && maxlen--) {
1164 msb = digit2i(*src++);
1165 if (msb < 0)
1166 return (src - 1);
1167 lsb = digit2i(*src++);
1168 if (lsb < 0)
1169 return (NULL);
1170 *dst++ = (msb << 4) | lsb;
1171 }
1172 return (src);
1173 }
1174
1175 // Convert an hex string into an integer.
1176 // This returns a pointer to the character following the last valid
1177 // hex digit.
1178 Addr
1179 hex2i(const char **srcp)
1180 {
1181 const char *src = *srcp;
1182 Addr r = 0;
1183 int nibble;
1184
1185 while ((nibble = digit2i(*src)) >= 0) {
1186 r *= 16;
1187 r += nibble;
1188 src++;
1189 }
1190 *srcp = src;
1191 return (r);
1192 }
1193