remote_gdb.cc revision 3579
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/alpha/kgdb.h"
125#include "arch/alpha/remote_gdb.hh"
126#include "arch/vtophys.hh"
127#include "base/intmath.hh"
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::RemoteGDB(System *_system, ThreadContext *c)
142    : BaseRemoteGDB(_system, c, KGDB_NUMREGS)
143{
144    memset(gdbregs.regs, 0, gdbregs.bytes());
145}
146
147///////////////////////////////////////////////////////////
148// RemoteGDB::acc
149//
150//	Determine if the mapping at va..(va+len) is valid.
151//
152bool
153RemoteGDB::acc(Addr va, size_t len)
154{
155    Addr last_va;
156
157    va = TheISA::TruncPage(va);
158    last_va = TheISA::RoundPage(va + len);
159
160    do  {
161        if (TheISA::IsK0Seg(va)) {
162            if (va < (TheISA::K0SegBase + pmem->size())) {
163                DPRINTF(GDBAcc, "acc:   Mapping is valid  K0SEG <= "
164                        "%#x < K0SEG + size\n", va);
165                return true;
166            } else {
167                DPRINTF(GDBAcc, "acc:   Mapping invalid %#x > K0SEG + size\n",
168                        va);
169                return false;
170            }
171        }
172
173    /**
174     * This code says that all accesses to palcode (instruction and data)
175     * are valid since there isn't a va->pa mapping because palcode is
176     * accessed physically. At some point this should probably be cleaned up
177     * but there is no easy way to do it.
178     */
179
180        if (AlphaISA::PcPAL(va) || va < 0x10000)
181            return true;
182
183        Addr ptbr = context->readMiscReg(AlphaISA::IPR_PALtemp20);
184        TheISA::PageTableEntry pte = TheISA::kernel_pte_lookup(context->getPhysPort(), ptbr, va);
185        if (!pte.valid()) {
186            DPRINTF(GDBAcc, "acc:   %#x pte is invalid\n", va);
187            return false;
188        }
189        va += TheISA::PageBytes;
190    } while (va < last_va);
191
192    DPRINTF(GDBAcc, "acc:   %#x mapping is valid\n", va);
193    return true;
194}
195
196///////////////////////////////////////////////////////////
197// RemoteGDB::getregs
198//
199//	Translate the kernel debugger register format into
200//	the GDB register format.
201void
202RemoteGDB::getregs()
203{
204    memset(gdbregs.regs, 0, gdbregs.bytes());
205
206    gdbregs.regs[KGDB_REG_PC] = context->readPC();
207
208    // @todo: Currently this is very Alpha specific.
209    if (AlphaISA::PcPAL(gdbregs.regs[KGDB_REG_PC])) {
210        for (int i = 0; i < TheISA::NumIntArchRegs; ++i) {
211            gdbregs.regs[i] = context->readIntReg(AlphaISA::reg_redir[i]);
212        }
213    } else {
214        for (int i = 0; i < TheISA::NumIntArchRegs; ++i) {
215            gdbregs.regs[i] = context->readIntReg(i);
216        }
217    }
218
219#ifdef KGDB_FP_REGS
220    for (int i = 0; i < TheISA::NumFloatArchRegs; ++i) {
221        gdbregs.regs[i + KGDB_REG_F0] = context->readFloatRegBits(i);
222    }
223#endif
224}
225
226///////////////////////////////////////////////////////////
227// RemoteGDB::setregs
228//
229//	Translate the GDB register format into the kernel
230//	debugger register format.
231//
232void
233RemoteGDB::setregs()
234{
235    // @todo: Currently this is very Alpha specific.
236    if (AlphaISA::PcPAL(gdbregs.regs[KGDB_REG_PC])) {
237        for (int i = 0; i < TheISA::NumIntArchRegs; ++i) {
238            context->setIntReg(AlphaISA::reg_redir[i], gdbregs.regs[i]);
239        }
240    } else {
241        for (int i = 0; i < TheISA::NumIntArchRegs; ++i) {
242            context->setIntReg(i, gdbregs.regs[i]);
243        }
244    }
245
246#ifdef KGDB_FP_REGS
247    for (int i = 0; i < TheISA::NumFloatArchRegs; ++i) {
248        context->setFloatRegBits(i, gdbregs.regs[i + KGDB_REG_F0]);
249    }
250#endif
251    context->setPC(gdbregs.regs[KGDB_REG_PC]);
252}
253
254void
255RemoteGDB::clearSingleStep()
256{
257    DPRINTF(GDBMisc, "clearSingleStep bt_addr=%#x nt_addr=%#x\n",
258            takenBkpt, notTakenBkpt);
259
260    if (takenBkpt != 0)
261        clearTempBreakpoint(takenBkpt);
262
263    if (notTakenBkpt != 0)
264        clearTempBreakpoint(notTakenBkpt);
265}
266
267void
268RemoteGDB::setSingleStep()
269{
270    Addr pc = context->readPC();
271    Addr npc, bpc;
272    bool set_bt = false;
273
274    npc = pc + sizeof(MachInst);
275
276    // User was stopped at pc, e.g. the instruction at pc was not
277    // executed.
278    MachInst inst = read<MachInst>(pc);
279    StaticInstPtr si(inst);
280    if (si->hasBranchTarget(pc, context, bpc)) {
281        // Don't bother setting a breakpoint on the taken branch if it
282        // is the same as the next pc
283        if (bpc != npc)
284            set_bt = true;
285    }
286
287    DPRINTF(GDBMisc, "setSingleStep bt_addr=%#x nt_addr=%#x\n",
288            takenBkpt, notTakenBkpt);
289
290    setTempBreakpoint(notTakenBkpt = npc);
291
292    if (set_bt)
293        setTempBreakpoint(takenBkpt = bpc);
294}
295
296// Write bytes to kernel address space for debugger.
297bool
298RemoteGDB::write(Addr vaddr, size_t size, const char *data)
299{
300    if (BaseRemoteGDB::write(vaddr, size, data)) {
301#ifdef IMB
302        alpha_pal_imb();
303#endif
304        return true;
305    } else {
306        return false;
307    }
308}
309
310