remote_gdb.cc revision 3536
1/*
2 * Copyright (c) 2002-2005 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 * Authors: Nathan Binkert
29 */
30
31/*
32 * Copyright (c) 1990, 1993
33 *	The Regents of the University of California.  All rights reserved.
34 *
35 * This software was developed by the Computer Systems Engineering group
36 * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
37 * contributed to Berkeley.
38 *
39 * All advertising materials mentioning features or use of this software
40 * must display the following acknowledgement:
41 *	This product includes software developed by the University of
42 *	California, Lawrence Berkeley Laboratories.
43 *
44 * Redistribution and use in source and binary forms, with or without
45 * modification, are permitted provided that the following conditions
46 * are met:
47 * 1. Redistributions of source code must retain the above copyright
48 *    notice, this list of conditions and the following disclaimer.
49 * 2. Redistributions in binary form must reproduce the above copyright
50 *    notice, this list of conditions and the following disclaimer in the
51 *    documentation and/or other materials provided with the distribution.
52 * 3. All advertising materials mentioning features or use of this software
53 *    must display the following acknowledgement:
54 *	This product includes software developed by the University of
55 *	California, Berkeley and its contributors.
56 * 4. Neither the name of the University nor the names of its contributors
57 *    may be used to endorse or promote products derived from this software
58 *    without specific prior written permission.
59 *
60 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
61 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
62 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
63 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
64 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
65 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
66 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
67 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
68 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
69 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
70 * SUCH DAMAGE.
71 *
72 *	@(#)kgdb_stub.c	8.4 (Berkeley) 1/12/94
73 */
74
75/*-
76 * Copyright (c) 2001 The NetBSD Foundation, Inc.
77 * All rights reserved.
78 *
79 * This code is derived from software contributed to The NetBSD Foundation
80 * by Jason R. Thorpe.
81 *
82 * Redistribution and use in source and binary forms, with or without
83 * modification, are permitted provided that the following conditions
84 * are met:
85 * 1. Redistributions of source code must retain the above copyright
86 *    notice, this list of conditions and the following disclaimer.
87 * 2. Redistributions in binary form must reproduce the above copyright
88 *    notice, this list of conditions and the following disclaimer in the
89 *    documentation and/or other materials provided with the distribution.
90 * 3. All advertising materials mentioning features or use of this software
91 *    must display the following acknowledgement:
92 *	This product includes software developed by the NetBSD
93 *	Foundation, Inc. and its contributors.
94 * 4. Neither the name of The NetBSD Foundation nor the names of its
95 *    contributors may be used to endorse or promote products derived
96 *    from this software without specific prior written permission.
97 *
98 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
99 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
100 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
101 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
102 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
103 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
104 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
105 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
106 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
107 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
108 * POSSIBILITY OF SUCH DAMAGE.
109 */
110
111/*
112 * $NetBSD: kgdb_stub.c,v 1.8 2001/07/07 22:58:00 wdk Exp $
113 *
114 * Taken from NetBSD
115 *
116 * "Stub" to allow remote cpu to debug over a serial line using gdb.
117 */
118
119#include <sys/signal.h>
120
121#include <string>
122#include <unistd.h>
123
124#include "arch/vtophys.hh"
125#include "arch/sparc/remote_gdb.hh"
126#include "base/intmath.hh"
127#include "base/kgdb.h"
128#include "base/remote_gdb.hh"
129#include "base/socket.hh"
130#include "base/trace.hh"
131#include "config/full_system.hh"
132#include "cpu/thread_context.hh"
133#include "cpu/static_inst.hh"
134#include "mem/physical.hh"
135#include "mem/port.hh"
136#include "sim/system.hh"
137
138using namespace std;
139using namespace TheISA;
140
141RemoteGDB::Event::Event(RemoteGDB *g, int fd, int e)
142    : PollEvent(fd, e), gdb(g)
143{}
144
145void
146RemoteGDB::Event::process(int revent)
147{
148    if (revent & POLLIN)
149        gdb->trap(ALPHA_KENTRY_IF);
150    else if (revent & POLLNVAL)
151        gdb->detach();
152}
153
154RemoteGDB::RemoteGDB(System *_system, ThreadContext *c)
155    : BaseRemoteGDB(_system, c, KGDB_NUMREGS),
156      event(NULL)
157{}
158
159RemoteGDB::~RemoteGDB()
160{
161    if (event)
162        delete event;
163}
164
165///////////////////////////////////////////////////////////
166// RemoteGDB::acc
167//
168//	Determine if the mapping at va..(va+len) is valid.
169//
170bool
171RemoteGDB::acc(Addr va, size_t len)
172{
173    Addr last_va;
174
175    va = TheISA::TruncPage(va);
176    last_va = TheISA::RoundPage(va + len);
177
178    do  {
179        if (TheISA::IsK0Seg(va)) {
180            if (va < (TheISA::K0SegBase + pmem->size())) {
181                DPRINTF(GDBAcc, "acc:   Mapping is valid  K0SEG <= "
182                        "%#x < K0SEG + size\n", va);
183                return true;
184            } else {
185                DPRINTF(GDBAcc, "acc:   Mapping invalid %#x > K0SEG + size\n",
186                        va);
187                return false;
188            }
189        }
190
191    /**
192     * This code says that all accesses to palcode (instruction and data)
193     * are valid since there isn't a va->pa mapping because palcode is
194     * accessed physically. At some point this should probably be cleaned up
195     * but there is no easy way to do it.
196     */
197
198        if (AlphaISA::PcPAL(va) || va < 0x10000)
199            return true;
200
201        Addr ptbr = context->readMiscReg(AlphaISA::IPR_PALtemp20);
202        TheISA::PageTableEntry pte = TheISA::kernel_pte_lookup(context->getPhysPort(), ptbr, va);
203        if (!pte.valid()) {
204            DPRINTF(GDBAcc, "acc:   %#x pte is invalid\n", va);
205            return false;
206        }
207        va += TheISA::PageBytes;
208    } while (va < last_va);
209
210    DPRINTF(GDBAcc, "acc:   %#x mapping is valid\n", va);
211    return true;
212}
213
214///////////////////////////////////////////////////////////
215// RemoteGDB::signal
216//
217//	Translate a trap number into a Unix-compatible signal number.
218//	(GDB only understands Unix signal numbers.)
219//
220int
221RemoteGDB::signal(int type)
222{
223    switch (type) {
224      case ALPHA_KENTRY_INT:
225        return (SIGTRAP);
226
227      case ALPHA_KENTRY_UNA:
228        return (SIGBUS);
229
230      case ALPHA_KENTRY_ARITH:
231        return (SIGFPE);
232
233      case ALPHA_KENTRY_IF:
234        return (SIGILL);
235
236      case ALPHA_KENTRY_MM:
237        return (SIGSEGV);
238
239      default:
240        panic("unknown signal type");
241        return 0;
242    }
243}
244
245///////////////////////////////////////////////////////////
246// RemoteGDB::getregs
247//
248//	Translate the kernel debugger register format into
249//	the GDB register format.
250void
251RemoteGDB::getregs()
252{
253    memset(gdbregs.regs, 0, gdbregs.size);
254
255    gdbregs.regs[KGDB_REG_PC] = context->readPC();
256
257    // @todo: Currently this is very Alpha specific.
258    if (AlphaISA::PcPAL(gdbregs.regs[KGDB_REG_PC])) {
259        for (int i = 0; i < TheISA::NumIntArchRegs; ++i) {
260            gdbregs.regs[i] = context->readIntReg(AlphaISA::reg_redir[i]);
261        }
262    } else {
263        for (int i = 0; i < TheISA::NumIntArchRegs; ++i) {
264            gdbregs.regs[i] = context->readIntReg(i);
265        }
266    }
267
268#ifdef KGDB_FP_REGS
269    for (int i = 0; i < TheISA::NumFloatArchRegs; ++i) {
270        gdbregs.regs[i + KGDB_REG_F0] = context->readFloatRegBits(i);
271    }
272#endif
273}
274
275///////////////////////////////////////////////////////////
276// RemoteGDB::setregs
277//
278//	Translate the GDB register format into the kernel
279//	debugger register format.
280//
281void
282RemoteGDB::setregs()
283{
284    // @todo: Currently this is very Alpha specific.
285    if (AlphaISA::PcPAL(gdbregs.regs[KGDB_REG_PC])) {
286        for (int i = 0; i < TheISA::NumIntArchRegs; ++i) {
287            context->setIntReg(AlphaISA::reg_redir[i], gdbregs.regs[i]);
288        }
289    } else {
290        for (int i = 0; i < TheISA::NumIntArchRegs; ++i) {
291            context->setIntReg(i, gdbregs.regs[i]);
292        }
293    }
294
295#ifdef KGDB_FP_REGS
296    for (int i = 0; i < TheISA::NumFloatArchRegs; ++i) {
297        context->setFloatRegBits(i, gdbregs.regs[i + KGDB_REG_F0]);
298    }
299#endif
300    context->setPC(gdbregs.regs[KGDB_REG_PC]);
301}
302
303void
304RemoteGDB::setTempBreakpoint(TempBreakpoint &bkpt, Addr addr)
305{
306    DPRINTF(GDBMisc, "setTempBreakpoint: addr=%#x\n", addr);
307
308    bkpt.address = addr;
309    insertHardBreak(addr, 4);
310}
311
312void
313RemoteGDB::clearTempBreakpoint(TempBreakpoint &bkpt)
314{
315    DPRINTF(GDBMisc, "setTempBreakpoint: addr=%#x\n",
316            bkpt.address);
317
318
319    removeHardBreak(bkpt.address, 4);
320    bkpt.address = 0;
321}
322
323void
324RemoteGDB::clearSingleStep()
325{
326    DPRINTF(GDBMisc, "clearSingleStep bt_addr=%#x nt_addr=%#x\n",
327            takenBkpt.address, notTakenBkpt.address);
328
329    if (takenBkpt.address != 0)
330        clearTempBreakpoint(takenBkpt);
331
332    if (notTakenBkpt.address != 0)
333        clearTempBreakpoint(notTakenBkpt);
334}
335
336void
337RemoteGDB::setSingleStep()
338{
339    Addr pc = context->readPC();
340    Addr npc, bpc;
341    bool set_bt = false;
342
343    npc = pc + sizeof(MachInst);
344
345    // User was stopped at pc, e.g. the instruction at pc was not
346    // executed.
347    MachInst inst = read<MachInst>(pc);
348    StaticInstPtr si(inst);
349    if (si->hasBranchTarget(pc, context, bpc)) {
350        // Don't bother setting a breakpoint on the taken branch if it
351        // is the same as the next pc
352        if (bpc != npc)
353            set_bt = true;
354    }
355
356    DPRINTF(GDBMisc, "setSingleStep bt_addr=%#x nt_addr=%#x\n",
357            takenBkpt.address, notTakenBkpt.address);
358
359    setTempBreakpoint(notTakenBkpt, npc);
360
361    if (set_bt)
362        setTempBreakpoint(takenBkpt, bpc);
363}
364
365// Write bytes to kernel address space for debugger.
366bool
367RemoteGDB::write(Addr vaddr, size_t size, const char *data)
368{
369    if (BaseRemoteGDB::write(vaddr, size, data)) {
370#ifdef IMB
371        alpha_pal_imb();
372#endif
373        return true;
374    } else {
375        return false;
376    }
377}
378
379
380PCEventQueue *RemoteGDB::getPcEventQueue()
381{
382    return &system->pcEventQueue;
383}
384
385
386RemoteGDB::HardBreakpoint::HardBreakpoint(RemoteGDB *_gdb, Addr pc)
387    : PCEvent(_gdb->getPcEventQueue(), "HardBreakpoint Event", pc),
388      gdb(_gdb), refcount(0)
389{
390    DPRINTF(GDBMisc, "creating hardware breakpoint at %#x\n", evpc);
391}
392
393void
394RemoteGDB::HardBreakpoint::process(ThreadContext *tc)
395{
396    DPRINTF(GDBMisc, "handling hardware breakpoint at %#x\n", pc());
397
398    if (tc == gdb->context)
399        gdb->trap(ALPHA_KENTRY_INT);
400}
401
402bool
403RemoteGDB::insertSoftBreak(Addr addr, size_t len)
404{
405    if (len != sizeof(MachInst))
406        panic("invalid length\n");
407
408    return insertHardBreak(addr, len);
409}
410
411bool
412RemoteGDB::removeSoftBreak(Addr addr, size_t len)
413{
414    if (len != sizeof(MachInst))
415        panic("invalid length\n");
416
417    return removeHardBreak(addr, len);
418}
419
420bool
421RemoteGDB::insertHardBreak(Addr addr, size_t len)
422{
423    if (len != sizeof(MachInst))
424        panic("invalid length\n");
425
426    DPRINTF(GDBMisc, "inserting hardware breakpoint at %#x\n", addr);
427
428    HardBreakpoint *&bkpt = hardBreakMap[addr];
429    if (bkpt == 0)
430        bkpt = new HardBreakpoint(this, addr);
431
432    bkpt->refcount++;
433
434    return true;
435}
436
437bool
438RemoteGDB::removeHardBreak(Addr addr, size_t len)
439{
440    if (len != sizeof(MachInst))
441        panic("invalid length\n");
442
443    DPRINTF(GDBMisc, "removing hardware breakpoint at %#x\n", addr);
444
445    break_iter_t i = hardBreakMap.find(addr);
446    if (i == hardBreakMap.end())
447        return false;
448
449    HardBreakpoint *hbp = (*i).second;
450    if (--hbp->refcount == 0) {
451        delete hbp;
452        hardBreakMap.erase(i);
453    }
454
455    return true;
456}
457