fix naming error - before we set CLIENT_MEMORY_SIZE and then when we wanted that...
[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
330 va = TheISA::TruncPage(va);
331 last_va = TheISA::RoundPage(va + len);
332
333 do {
334 if (TheISA::IsK0Seg(va)) {
335 if (va < (TheISA::K0SegBase + pmem->size())) {
336 DPRINTF(GDBAcc, "acc: Mapping is valid K0SEG <= "
337 "%#x < K0SEG + size\n", va);
338 return true;
339 } else {
340 DPRINTF(GDBAcc, "acc: Mapping invalid %#x > K0SEG + size\n",
341 va);
342 return false;
343 }
344 }
345
346 /**
347 * This code says that all accesses to palcode (instruction and data)
348 * are valid since there isn't a va->pa mapping because palcode is
349 * accessed physically. At some point this should probably be cleaned up
350 * but there is no easy way to do it.
351 */
352
353 if (AlphaISA::PcPAL(va) || va < 0x10000)
354 return true;
355
356 Addr ptbr = context->regs.ipr[AlphaISA::IPR_PALtemp20];
357 TheISA::PageTableEntry pte = kernel_pte_lookup(pmem, ptbr, va);
358 if (!pte.valid()) {
359 DPRINTF(GDBAcc, "acc: %#x pte is invalid\n", va);
360 return false;
361 }
362 va += TheISA::PageBytes;
363 } while (va < last_va);
364
365 DPRINTF(GDBAcc, "acc: %#x mapping is valid\n", va);
366 return true;
367 }
368
369 ///////////////////////////////////////////////////////////
370 // RemoteGDB::signal
371 //
372 // Translate a trap number into a Unix-compatible signal number.
373 // (GDB only understands Unix signal numbers.)
374 //
375 int
376 RemoteGDB::signal(int type)
377 {
378 switch (type) {
379 case ALPHA_KENTRY_INT:
380 return (SIGTRAP);
381
382 case ALPHA_KENTRY_UNA:
383 return (SIGBUS);
384
385 case ALPHA_KENTRY_ARITH:
386 return (SIGFPE);
387
388 case ALPHA_KENTRY_IF:
389 return (SIGILL);
390
391 case ALPHA_KENTRY_MM:
392 return (SIGSEGV);
393
394 default:
395 panic("unknown signal type");
396 return 0;
397 }
398 }
399
400 ///////////////////////////////////////////////////////////
401 // RemoteGDB::getregs
402 //
403 // Translate the kernel debugger register format into
404 // the GDB register format.
405 void
406 RemoteGDB::getregs()
407 {
408 memset(gdbregs, 0, sizeof(gdbregs));
409 memcpy(&gdbregs[KGDB_REG_V0], context->regs.intRegFile, 32 * sizeof(uint64_t));
410 #ifdef KGDB_FP_REGS
411 memcpy(&gdbregs[KGDB_REG_F0], context->regs.floatRegFile.q,
412 32 * sizeof(uint64_t));
413 #endif
414 gdbregs[KGDB_REG_PC] = context->regs.pc;
415 }
416
417 ///////////////////////////////////////////////////////////
418 // RemoteGDB::setregs
419 //
420 // Translate the GDB register format into the kernel
421 // debugger register format.
422 //
423 void
424 RemoteGDB::setregs()
425 {
426 memcpy(context->regs.intRegFile, &gdbregs[KGDB_REG_V0],
427 32 * sizeof(uint64_t));
428 #ifdef KGDB_FP_REGS
429 memcpy(context->regs.floatRegFile.q, &gdbregs[KGDB_REG_F0],
430 32 * sizeof(uint64_t));
431 #endif
432 context->regs.pc = gdbregs[KGDB_REG_PC];
433 }
434
435 void
436 RemoteGDB::setTempBreakpoint(TempBreakpoint &bkpt, Addr addr)
437 {
438 DPRINTF(GDBMisc, "setTempBreakpoint: addr=%#x\n", addr);
439
440 bkpt.address = addr;
441 insertHardBreak(addr, 4);
442 }
443
444 void
445 RemoteGDB::clearTempBreakpoint(TempBreakpoint &bkpt)
446 {
447 DPRINTF(GDBMisc, "setTempBreakpoint: addr=%#x\n",
448 bkpt.address);
449
450
451 removeHardBreak(bkpt.address, 4);
452 bkpt.address = 0;
453 }
454
455 void
456 RemoteGDB::clearSingleStep()
457 {
458 DPRINTF(GDBMisc, "clearSingleStep bt_addr=%#x nt_addr=%#x\n",
459 takenBkpt.address, notTakenBkpt.address);
460
461 if (takenBkpt.address != 0)
462 clearTempBreakpoint(takenBkpt);
463
464 if (notTakenBkpt.address != 0)
465 clearTempBreakpoint(notTakenBkpt);
466 }
467
468 void
469 RemoteGDB::setSingleStep()
470 {
471 Addr pc = context->regs.pc;
472 Addr npc, bpc;
473 bool set_bt = false;
474
475 npc = pc + sizeof(MachInst);
476
477 // User was stopped at pc, e.g. the instruction at pc was not
478 // executed.
479 MachInst inst = read<MachInst>(pc);
480 StaticInstPtr<TheISA> si(inst);
481 if (si->hasBranchTarget(pc, context, bpc)) {
482 // Don't bother setting a breakpoint on the taken branch if it
483 // is the same as the next pc
484 if (bpc != npc)
485 set_bt = true;
486 }
487
488 DPRINTF(GDBMisc, "setSingleStep bt_addr=%#x nt_addr=%#x\n",
489 takenBkpt.address, notTakenBkpt.address);
490
491 setTempBreakpoint(notTakenBkpt, npc);
492
493 if (set_bt)
494 setTempBreakpoint(takenBkpt, bpc);
495 }
496
497 /////////////////////////
498 //
499 //
500
501 uint8_t
502 RemoteGDB::getbyte()
503 {
504 uint8_t b;
505 ::read(fd, &b, 1);
506 return b;
507 }
508
509 void
510 RemoteGDB::putbyte(uint8_t b)
511 {
512 ::write(fd, &b, 1);
513 }
514
515 // Send a packet to gdb
516 void
517 RemoteGDB::send(const char *bp)
518 {
519 const char *p;
520 uint8_t csum, c;
521
522 DPRINTF(GDBSend, "send: %s\n", bp);
523
524 do {
525 p = bp;
526 putbyte(KGDB_START);
527 for (csum = 0; (c = *p); p++) {
528 putbyte(c);
529 csum += c;
530 }
531 putbyte(KGDB_END);
532 putbyte(i2digit(csum >> 4));
533 putbyte(i2digit(csum));
534 } while ((c = getbyte() & 0x7f) == KGDB_BADP);
535 }
536
537 // Receive a packet from gdb
538 int
539 RemoteGDB::recv(char *bp, int maxlen)
540 {
541 char *p;
542 int c, csum;
543 int len;
544
545 do {
546 p = bp;
547 csum = len = 0;
548 while ((c = getbyte()) != KGDB_START)
549 ;
550
551 while ((c = getbyte()) != KGDB_END && len < maxlen) {
552 c &= 0x7f;
553 csum += c;
554 *p++ = c;
555 len++;
556 }
557 csum &= 0xff;
558 *p = '\0';
559
560 if (len >= maxlen) {
561 putbyte(KGDB_BADP);
562 continue;
563 }
564
565 csum -= digit2i(getbyte()) * 16;
566 csum -= digit2i(getbyte());
567
568 if (csum == 0) {
569 putbyte(KGDB_GOODP);
570 // Sequence present?
571 if (bp[2] == ':') {
572 putbyte(bp[0]);
573 putbyte(bp[1]);
574 len -= 3;
575 bcopy(bp + 3, bp, len);
576 }
577 break;
578 }
579 putbyte(KGDB_BADP);
580 } while (1);
581
582 DPRINTF(GDBRecv, "recv: %s: %s\n", gdb_command(*bp), bp);
583
584 return (len);
585 }
586
587 // Read bytes from kernel address space for debugger.
588 bool
589 RemoteGDB::read(Addr vaddr, size_t size, char *data)
590 {
591 static Addr lastaddr = 0;
592 static size_t lastsize = 0;
593
594 uint8_t *maddr;
595
596 if (vaddr < 10) {
597 DPRINTF(GDBRead, "read: reading memory location zero!\n");
598 vaddr = lastaddr + lastsize;
599 }
600
601 DPRINTF(GDBRead, "read: addr=%#x, size=%d", vaddr, size);
602 #if TRACING_ON
603 char *d = data;
604 size_t s = size;
605 #endif
606
607 lastaddr = vaddr;
608 lastsize = size;
609
610 size_t count = min((Addr)size,
611 VMPageSize - (vaddr & (VMPageSize - 1)));
612
613 maddr = vtomem(context, vaddr, count);
614 memcpy(data, maddr, count);
615
616 vaddr += count;
617 data += count;
618 size -= count;
619
620 while (size >= VMPageSize) {
621 maddr = vtomem(context, vaddr, count);
622 memcpy(data, maddr, VMPageSize);
623
624 vaddr += VMPageSize;
625 data += VMPageSize;
626 size -= VMPageSize;
627 }
628
629 if (size > 0) {
630 maddr = vtomem(context, vaddr, count);
631 memcpy(data, maddr, size);
632 }
633
634 #if TRACING_ON
635 if (DTRACE(GDBRead)) {
636 if (DTRACE(GDBExtra)) {
637 char buf[1024];
638 mem2hex(buf, d, s);
639 DPRINTFNR(": %s\n", buf);
640 } else
641 DPRINTFNR("\n");
642 }
643 #endif
644
645 return true;
646 }
647
648 // Write bytes to kernel address space for debugger.
649 bool
650 RemoteGDB::write(Addr vaddr, size_t size, const char *data)
651 {
652 static Addr lastaddr = 0;
653 static size_t lastsize = 0;
654
655 uint8_t *maddr;
656
657 if (vaddr < 10) {
658 DPRINTF(GDBWrite, "write: writing memory location zero!\n");
659 vaddr = lastaddr + lastsize;
660 }
661
662 if (DTRACE(GDBWrite)) {
663 DPRINTFN("write: addr=%#x, size=%d", vaddr, size);
664 if (DTRACE(GDBExtra)) {
665 char buf[1024];
666 mem2hex(buf, data, size);
667 DPRINTFNR(": %s\n", buf);
668 } else
669 DPRINTFNR("\n");
670 }
671
672 lastaddr = vaddr;
673 lastsize = size;
674
675 size_t count = min((Addr)size,
676 VMPageSize - (vaddr & (VMPageSize - 1)));
677
678 maddr = vtomem(context, vaddr, count);
679 memcpy(maddr, data, count);
680
681 vaddr += count;
682 data += count;
683 size -= count;
684
685 while (size >= VMPageSize) {
686 maddr = vtomem(context, vaddr, count);
687 memcpy(maddr, data, VMPageSize);
688
689 vaddr += VMPageSize;
690 data += VMPageSize;
691 size -= VMPageSize;
692 }
693
694 if (size > 0) {
695 maddr = vtomem(context, vaddr, count);
696 memcpy(maddr, data, size);
697 }
698
699 #ifdef IMB
700 alpha_pal_imb();
701 #endif
702
703 return true;
704 }
705
706
707 PCEventQueue *RemoteGDB::getPcEventQueue()
708 {
709 return &system->pcEventQueue;
710 }
711
712
713 RemoteGDB::HardBreakpoint::HardBreakpoint(RemoteGDB *_gdb, Addr pc)
714 : PCEvent(_gdb->getPcEventQueue(), "HardBreakpoint Event", pc),
715 gdb(_gdb), refcount(0)
716 {
717 DPRINTF(GDBMisc, "creating hardware breakpoint at %#x\n", evpc);
718 schedule();
719 }
720
721 void
722 RemoteGDB::HardBreakpoint::process(ExecContext *xc)
723 {
724 DPRINTF(GDBMisc, "handling hardware breakpoint at %#x\n", pc());
725
726 if (xc == gdb->context)
727 gdb->trap(ALPHA_KENTRY_INT);
728 }
729
730 bool
731 RemoteGDB::insertSoftBreak(Addr addr, size_t len)
732 {
733 if (len != sizeof(MachInst))
734 panic("invalid length\n");
735
736 return insertHardBreak(addr, len);
737 }
738
739 bool
740 RemoteGDB::removeSoftBreak(Addr addr, size_t len)
741 {
742 if (len != sizeof(MachInst))
743 panic("invalid length\n");
744
745 return removeHardBreak(addr, len);
746 }
747
748 bool
749 RemoteGDB::insertHardBreak(Addr addr, size_t len)
750 {
751 if (len != sizeof(MachInst))
752 panic("invalid length\n");
753
754 DPRINTF(GDBMisc, "inserting hardware breakpoint at %#x\n", addr);
755
756 HardBreakpoint *&bkpt = hardBreakMap[addr];
757 if (bkpt == 0)
758 bkpt = new HardBreakpoint(this, addr);
759
760 bkpt->refcount++;
761
762 return true;
763 }
764
765 bool
766 RemoteGDB::removeHardBreak(Addr addr, size_t len)
767 {
768 if (len != sizeof(MachInst))
769 panic("invalid length\n");
770
771 DPRINTF(GDBMisc, "removing hardware breakpoint at %#x\n", addr);
772
773 break_iter_t i = hardBreakMap.find(addr);
774 if (i == hardBreakMap.end())
775 return false;
776
777 HardBreakpoint *hbp = (*i).second;
778 if (--hbp->refcount == 0) {
779 delete hbp;
780 hardBreakMap.erase(i);
781 }
782
783 return true;
784 }
785
786 const char *
787 break_type(char c)
788 {
789 switch(c) {
790 case '0': return "software breakpoint";
791 case '1': return "hardware breakpoint";
792 case '2': return "write watchpoint";
793 case '3': return "read watchpoint";
794 case '4': return "access watchpoint";
795 default: return "unknown breakpoint/watchpoint";
796 }
797 }
798
799 // This function does all command processing for interfacing to a
800 // remote gdb. Note that the error codes are ignored by gdb at
801 // present, but might eventually become meaningful. (XXX) It might
802 // makes sense to use POSIX errno values, because that is what the
803 // gdb/remote.c functions want to return.
804 bool
805 RemoteGDB::trap(int type)
806 {
807 uint64_t val;
808 size_t datalen, len;
809 char data[KGDB_BUFLEN + 1];
810 char buffer[sizeof(gdbregs) * 2 + 256];
811 char temp[KGDB_BUFLEN];
812 const char *p;
813 char command, subcmd;
814 string var;
815 bool ret;
816
817 if (!attached)
818 return false;
819
820 DPRINTF(GDBMisc, "trap: PC=%#x NPC=%#x\n",
821 context->regs.pc, context->regs.npc);
822
823 clearSingleStep();
824
825 /*
826 * The first entry to this function is normally through
827 * a breakpoint trap in kgdb_connect(), in which case we
828 * must advance past the breakpoint because gdb will not.
829 *
830 * On the first entry here, we expect that gdb is not yet
831 * listening to us, so just enter the interaction loop.
832 * After the debugger is "active" (connected) it will be
833 * waiting for a "signaled" message from us.
834 */
835 if (!active)
836 active = true;
837 else
838 // Tell remote host that an exception has occurred.
839 sprintf((char *)buffer, "S%02x", signal(type));
840 send(buffer);
841
842 // Stick frame regs into our reg cache.
843 getregs();
844
845 for (;;) {
846 datalen = recv(data, sizeof(data));
847 data[sizeof(data) - 1] = 0; // Sentinel
848 command = data[0];
849 subcmd = 0;
850 p = data + 1;
851 switch (command) {
852
853 case KGDB_SIGNAL:
854 // if this command came from a running gdb, answer it --
855 // the other guy has no way of knowing if we're in or out
856 // of this loop when he issues a "remote-signal".
857 sprintf((char *)buffer, "S%02x", signal(type));
858 send(buffer);
859 continue;
860
861 case KGDB_REG_R:
862 if (2 * sizeof(gdbregs) > sizeof(buffer))
863 panic("buffer too small");
864
865 mem2hex(buffer, gdbregs, sizeof(gdbregs));
866 send(buffer);
867 continue;
868
869 case KGDB_REG_W:
870 p = hex2mem(gdbregs, p, sizeof(gdbregs));
871 if (p == NULL || *p != '\0')
872 send("E01");
873 else {
874 setregs();
875 send("OK");
876 }
877 continue;
878
879 #if 0
880 case KGDB_SET_REG:
881 val = hex2i(&p);
882 if (*p++ != '=') {
883 send("E01");
884 continue;
885 }
886 if (val < 0 && val >= KGDB_NUMREGS) {
887 send("E01");
888 continue;
889 }
890
891 gdbregs[val] = hex2i(&p);
892 setregs();
893 send("OK");
894
895 continue;
896 #endif
897
898 case KGDB_MEM_R:
899 val = hex2i(&p);
900 if (*p++ != ',') {
901 send("E02");
902 continue;
903 }
904 len = hex2i(&p);
905 if (*p != '\0') {
906 send("E03");
907 continue;
908 }
909 if (len > sizeof(buffer)) {
910 send("E04");
911 continue;
912 }
913 if (!acc(val, len)) {
914 send("E05");
915 continue;
916 }
917
918 if (read(val, (size_t)len, (char *)buffer)) {
919 mem2hex(temp, buffer, len);
920 send(temp);
921 } else {
922 send("E05");
923 }
924 continue;
925
926 case KGDB_MEM_W:
927 val = hex2i(&p);
928 if (*p++ != ',') {
929 send("E06");
930 continue;
931 }
932 len = hex2i(&p);
933 if (*p++ != ':') {
934 send("E07");
935 continue;
936 }
937 if (len > datalen - (p - data)) {
938 send("E08");
939 continue;
940 }
941 p = hex2mem(buffer, p, sizeof(buffer));
942 if (p == NULL) {
943 send("E09");
944 continue;
945 }
946 if (!acc(val, len)) {
947 send("E0A");
948 continue;
949 }
950 if (write(val, (size_t)len, (char *)buffer))
951 send("OK");
952 else
953 send("E0B");
954 continue;
955
956 case KGDB_SET_THREAD:
957 subcmd = *p++;
958 val = hex2i(&p);
959 if (val == 0)
960 send("OK");
961 else
962 send("E01");
963 continue;
964
965 case KGDB_DETACH:
966 case KGDB_KILL:
967 active = false;
968 clearSingleStep();
969 detach();
970 goto out;
971
972 case KGDB_ASYNC_CONT:
973 subcmd = hex2i(&p);
974 if (*p++ == ';') {
975 val = hex2i(&p);
976 context->regs.pc = val;
977 context->regs.npc = val + sizeof(MachInst);
978 }
979 clearSingleStep();
980 goto out;
981
982 case KGDB_CONT:
983 if (p - data < datalen) {
984 val = hex2i(&p);
985 context->regs.pc = val;
986 context->regs.npc = val + sizeof(MachInst);
987 }
988 clearSingleStep();
989 goto out;
990
991 case KGDB_ASYNC_STEP:
992 subcmd = hex2i(&p);
993 if (*p++ == ';') {
994 val = hex2i(&p);
995 context->regs.pc = val;
996 context->regs.npc = val + sizeof(MachInst);
997 }
998 setSingleStep();
999 goto out;
1000
1001 case KGDB_STEP:
1002 if (p - data < datalen) {
1003 val = hex2i(&p);
1004 context->regs.pc = val;
1005 context->regs.npc = val + sizeof(MachInst);
1006 }
1007 setSingleStep();
1008 goto out;
1009
1010 case KGDB_CLR_HW_BKPT:
1011 subcmd = *p++;
1012 if (*p++ != ',') send("E0D");
1013 val = hex2i(&p);
1014 if (*p++ != ',') send("E0D");
1015 len = hex2i(&p);
1016
1017 DPRINTF(GDBMisc, "clear %s, addr=%#x, len=%d\n",
1018 break_type(subcmd), val, len);
1019
1020 ret = false;
1021
1022 switch (subcmd) {
1023 case '0': // software breakpoint
1024 ret = removeSoftBreak(val, len);
1025 break;
1026
1027 case '1': // hardware breakpoint
1028 ret = removeHardBreak(val, len);
1029 break;
1030
1031 case '2': // write watchpoint
1032 case '3': // read watchpoint
1033 case '4': // access watchpoint
1034 default: // unknown
1035 send("");
1036 break;
1037 }
1038
1039 send(ret ? "OK" : "E0C");
1040 continue;
1041
1042 case KGDB_SET_HW_BKPT:
1043 subcmd = *p++;
1044 if (*p++ != ',') send("E0D");
1045 val = hex2i(&p);
1046 if (*p++ != ',') send("E0D");
1047 len = hex2i(&p);
1048
1049 DPRINTF(GDBMisc, "set %s, addr=%#x, len=%d\n",
1050 break_type(subcmd), val, len);
1051
1052 ret = false;
1053
1054 switch (subcmd) {
1055 case '0': // software breakpoint
1056 ret = insertSoftBreak(val, len);
1057 break;
1058
1059 case '1': // hardware breakpoint
1060 ret = insertHardBreak(val, len);
1061 break;
1062
1063 case '2': // write watchpoint
1064 case '3': // read watchpoint
1065 case '4': // access watchpoint
1066 default: // unknown
1067 send("");
1068 break;
1069 }
1070
1071 send(ret ? "OK" : "E0C");
1072 continue;
1073
1074 case KGDB_QUERY_VAR:
1075 var = string(p, datalen - 1);
1076 if (var == "C")
1077 send("QC0");
1078 else
1079 send("");
1080 continue;
1081
1082 case KGDB_SET_BAUD:
1083 case KGDB_SET_BREAK:
1084 case KGDB_DEBUG:
1085 case KGDB_CYCLE_STEP:
1086 case KGDB_SIG_CYCLE_STEP:
1087 case KGDB_READ_REG:
1088 case KGDB_SET_VAR:
1089 case KGDB_RESET:
1090 case KGDB_THREAD_ALIVE:
1091 case KGDB_TARGET_EXIT:
1092 case KGDB_BINARY_DLOAD:
1093 // Unsupported command
1094 DPRINTF(GDBMisc, "Unsupported command: %s\n",
1095 gdb_command(command));
1096 DDUMP(GDBMisc, (uint8_t *)data, datalen);
1097 send("");
1098 continue;
1099
1100 default:
1101 // Unknown command.
1102 DPRINTF(GDBMisc, "Unknown command: %c(%#x)\n",
1103 command, command);
1104 send("");
1105 continue;
1106
1107
1108 }
1109 }
1110
1111 out:
1112 return true;
1113 }
1114
1115 // Convert a hex digit into an integer.
1116 // This returns -1 if the argument passed is no valid hex digit.
1117 int
1118 digit2i(char c)
1119 {
1120 if (c >= '0' && c <= '9')
1121 return (c - '0');
1122 else if (c >= 'a' && c <= 'f')
1123 return (c - 'a' + 10);
1124 else if (c >= 'A' && c <= 'F')
1125
1126 return (c - 'A' + 10);
1127 else
1128 return (-1);
1129 }
1130
1131 // Convert the low 4 bits of an integer into an hex digit.
1132 char
1133 i2digit(int n)
1134 {
1135 return ("0123456789abcdef"[n & 0x0f]);
1136 }
1137
1138 // Convert a byte array into an hex string.
1139 void
1140 mem2hex(void *vdst, const void *vsrc, int len)
1141 {
1142 char *dst = (char *)vdst;
1143 const char *src = (const char *)vsrc;
1144
1145 while (len--) {
1146 *dst++ = i2digit(*src >> 4);
1147 *dst++ = i2digit(*src++);
1148 }
1149 *dst = '\0';
1150 }
1151
1152 // Convert an hex string into a byte array.
1153 // This returns a pointer to the character following the last valid
1154 // hex digit. If the string ends in the middle of a byte, NULL is
1155 // returned.
1156 const char *
1157 hex2mem(void *vdst, const char *src, int maxlen)
1158 {
1159 char *dst = (char *)vdst;
1160 int msb, lsb;
1161
1162 while (*src && maxlen--) {
1163 msb = digit2i(*src++);
1164 if (msb < 0)
1165 return (src - 1);
1166 lsb = digit2i(*src++);
1167 if (lsb < 0)
1168 return (NULL);
1169 *dst++ = (msb << 4) | lsb;
1170 }
1171 return (src);
1172 }
1173
1174 // Convert an hex string into an integer.
1175 // This returns a pointer to the character following the last valid
1176 // hex digit.
1177 Addr
1178 hex2i(const char **srcp)
1179 {
1180 const char *src = *srcp;
1181 Addr r = 0;
1182 int nibble;
1183
1184 while ((nibble = digit2i(*src)) >= 0) {
1185 r *= 16;
1186 r += nibble;
1187 src++;
1188 }
1189 *srcp = src;
1190 return (r);
1191 }
1192