remote_gdb.cc revision 8829
111986Sandreas.sandberg@arm.com/*
211986Sandreas.sandberg@arm.com * Copyright (c) 2002-2005 The Regents of The University of Michigan
311986Sandreas.sandberg@arm.com * All rights reserved.
411986Sandreas.sandberg@arm.com *
511986Sandreas.sandberg@arm.com * Redistribution and use in source and binary forms, with or without
611986Sandreas.sandberg@arm.com * modification, are permitted provided that the following conditions are
711986Sandreas.sandberg@arm.com * met: redistributions of source code must retain the above copyright
811986Sandreas.sandberg@arm.com * notice, this list of conditions and the following disclaimer;
911986Sandreas.sandberg@arm.com * redistributions in binary form must reproduce the above copyright
1011986Sandreas.sandberg@arm.com * notice, this list of conditions and the following disclaimer in the
1111986Sandreas.sandberg@arm.com * documentation and/or other materials provided with the distribution;
1211986Sandreas.sandberg@arm.com * neither the name of the copyright holders nor the names of its
1311986Sandreas.sandberg@arm.com * contributors may be used to endorse or promote products derived from
1411986Sandreas.sandberg@arm.com * this software without specific prior written permission.
1511986Sandreas.sandberg@arm.com *
1611986Sandreas.sandberg@arm.com * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
1711986Sandreas.sandberg@arm.com * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
1811986Sandreas.sandberg@arm.com * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
1911986Sandreas.sandberg@arm.com * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
2011986Sandreas.sandberg@arm.com * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2111986Sandreas.sandberg@arm.com * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
2211986Sandreas.sandberg@arm.com * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2311986Sandreas.sandberg@arm.com * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2411986Sandreas.sandberg@arm.com * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2511986Sandreas.sandberg@arm.com * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2611986Sandreas.sandberg@arm.com * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2711986Sandreas.sandberg@arm.com *
2811986Sandreas.sandberg@arm.com * Authors: Nathan Binkert
2911986Sandreas.sandberg@arm.com */
3011986Sandreas.sandberg@arm.com
3111986Sandreas.sandberg@arm.com/*
3211986Sandreas.sandberg@arm.com * Copyright (c) 1990, 1993 The Regents of the University of California
3311986Sandreas.sandberg@arm.com * All rights reserved
3411986Sandreas.sandberg@arm.com *
3511986Sandreas.sandberg@arm.com * This software was developed by the Computer Systems Engineering group
3611986Sandreas.sandberg@arm.com * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
3711986Sandreas.sandberg@arm.com * contributed to Berkeley.
3811986Sandreas.sandberg@arm.com *
3911986Sandreas.sandberg@arm.com * All advertising materials mentioning features or use of this software
4011986Sandreas.sandberg@arm.com * must display the following acknowledgement:
4111986Sandreas.sandberg@arm.com *      This product includes software developed by the University of
4211986Sandreas.sandberg@arm.com *      California, Lawrence Berkeley Laboratories.
4311986Sandreas.sandberg@arm.com *
4411986Sandreas.sandberg@arm.com * Redistribution and use in source and binary forms, with or without
4511986Sandreas.sandberg@arm.com * modification, are permitted provided that the following conditions
4611986Sandreas.sandberg@arm.com * are met:
4711986Sandreas.sandberg@arm.com * 1. Redistributions of source code must retain the above copyright
4811986Sandreas.sandberg@arm.com *    notice, this list of conditions and the following disclaimer.
4911986Sandreas.sandberg@arm.com * 2. Redistributions in binary form must reproduce the above copyright
5011986Sandreas.sandberg@arm.com *    notice, this list of conditions and the following disclaimer in the
5111986Sandreas.sandberg@arm.com *    documentation and/or other materials provided with the distribution.
5211986Sandreas.sandberg@arm.com * 3. All advertising materials mentioning features or use of this software
5311986Sandreas.sandberg@arm.com *    must display the following acknowledgement:
5411986Sandreas.sandberg@arm.com *      This product includes software developed by the University of
5511986Sandreas.sandberg@arm.com *      California, Berkeley and its contributors.
5611986Sandreas.sandberg@arm.com * 4. Neither the name of the University nor the names of its contributors
5711986Sandreas.sandberg@arm.com *    may be used to endorse or promote products derived from this software
5811986Sandreas.sandberg@arm.com *    without specific prior written permission.
5911986Sandreas.sandberg@arm.com *
6011986Sandreas.sandberg@arm.com * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
6111986Sandreas.sandberg@arm.com * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
6211986Sandreas.sandberg@arm.com * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
6311986Sandreas.sandberg@arm.com * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
6411986Sandreas.sandberg@arm.com * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
6511986Sandreas.sandberg@arm.com * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
6611986Sandreas.sandberg@arm.com * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
6711986Sandreas.sandberg@arm.com * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
6811986Sandreas.sandberg@arm.com * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
6911986Sandreas.sandberg@arm.com * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
7011986Sandreas.sandberg@arm.com * SUCH DAMAGE.
7111986Sandreas.sandberg@arm.com *
7211986Sandreas.sandberg@arm.com *      @(#)kgdb_stub.c 8.4 (Berkeley) 1/12/94
7311986Sandreas.sandberg@arm.com */
7411986Sandreas.sandberg@arm.com
7511986Sandreas.sandberg@arm.com/*-
7611986Sandreas.sandberg@arm.com * Copyright (c) 2001 The NetBSD Foundation, Inc.
7711986Sandreas.sandberg@arm.com * All rights reserved.
7811986Sandreas.sandberg@arm.com *
7911986Sandreas.sandberg@arm.com * This code is derived from software contributed to The NetBSD Foundation
8011986Sandreas.sandberg@arm.com * by Jason R. Thorpe.
8111986Sandreas.sandberg@arm.com *
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#include <unistd.h>
121
122#include <string>
123
124#include "arch/sparc/remote_gdb.hh"
125#include "arch/vtophys.hh"
126#include "base/intmath.hh"
127#include "base/remote_gdb.hh"
128#include "base/socket.hh"
129#include "base/trace.hh"
130#include "cpu/static_inst.hh"
131#include "cpu/thread_context.hh"
132#include "debug/GDBRead.hh"
133#include "mem/page_table.hh"
134#include "mem/physical.hh"
135#include "mem/port.hh"
136#include "sim/byteswap.hh"
137#include "sim/full_system.hh"
138#include "sim/process.hh"
139#include "sim/system.hh"
140
141using namespace std;
142using namespace SparcISA;
143
144RemoteGDB::RemoteGDB(System *_system, ThreadContext *c)
145    : BaseRemoteGDB(_system, c, NumGDBRegs), nextBkpt(0)
146{}
147
148///////////////////////////////////////////////////////////
149// RemoteGDB::acc
150//
151//      Determine if the mapping at va..(va+len) is valid.
152//
153bool
154RemoteGDB::acc(Addr va, size_t len)
155{
156    //@Todo In NetBSD, this function checks if all addresses
157    // from va to va + len have valid page map entries. Not
158    // sure how this will work for other OSes or in general.
159    if (FullSystem) {
160        if (va)
161            return true;
162        return false;
163    } else {
164        TlbEntry entry;
165        // Check to make sure the first byte is mapped into the processes
166        // address space.
167        if (context->getProcessPtr()->pTable->lookup(va, entry))
168            return true;
169        return false;
170    }
171}
172
173///////////////////////////////////////////////////////////
174// RemoteGDB::getregs
175//
176//      Translate the kernel debugger register format into
177//      the GDB register format.
178void
179RemoteGDB::getregs()
180{
181    memset(gdbregs.regs, 0, gdbregs.size);
182
183    PCState pc = context->pcState();
184    PSTATE pstate = context->readMiscReg(MISCREG_PSTATE);
185
186    if (pstate.am) {
187        uint32_t *regs;
188        regs = (uint32_t*)gdbregs.regs;
189        regs[Reg32Pc] = htobe((uint32_t)pc.pc());
190        regs[Reg32Npc] = htobe((uint32_t)pc.npc());
191        for (int x = RegG0; x <= RegI0 + 7; x++)
192            regs[x] = htobe((uint32_t)context->readIntReg(x - RegG0));
193
194        regs[Reg32Y] = htobe((uint32_t)context->readIntReg(NumIntArchRegs + 1));
195        regs[Reg32Psr] = htobe((uint32_t)pstate);
196        regs[Reg32Fsr] = htobe((uint32_t)context->readMiscReg(MISCREG_FSR));
197        regs[Reg32Csr] = htobe((uint32_t)context->readIntReg(NumIntArchRegs + 2));
198    } else {
199        gdbregs.regs[RegPc] = htobe(pc.pc());
200        gdbregs.regs[RegNpc] = htobe(pc.npc());
201        for (int x = RegG0; x <= RegI0 + 7; x++)
202            gdbregs.regs[x] = htobe(context->readIntReg(x - RegG0));
203
204        gdbregs.regs[RegFsr] = htobe(context->readMiscReg(MISCREG_FSR));
205        gdbregs.regs[RegFprs] = htobe(context->readMiscReg(MISCREG_FPRS));
206        gdbregs.regs[RegY] = htobe(context->readIntReg(NumIntArchRegs + 1));
207        gdbregs.regs[RegState] = htobe(
208            context->readMiscReg(MISCREG_CWP) |
209            pstate << 8 |
210            context->readMiscReg(MISCREG_ASI) << 24 |
211            context->readIntReg(NumIntArchRegs + 2) << 32);
212    }
213
214    DPRINTF(GDBRead, "PC=%#x\n", gdbregs.regs[RegPc]);
215
216    // Floating point registers are left at 0 in netbsd
217    // All registers other than the pc, npc and int regs
218    // are ignored as well.
219}
220
221///////////////////////////////////////////////////////////
222// RemoteGDB::setregs
223//
224//      Translate the GDB register format into the kernel
225//      debugger register format.
226//
227void
228RemoteGDB::setregs()
229{
230    PCState pc;
231    pc.pc(gdbregs.regs[RegPc]);
232    pc.npc(gdbregs.regs[RegNpc]);
233    pc.nnpc(pc.npc() + sizeof(MachInst));
234    pc.upc(0);
235    pc.nupc(1);
236    context->pcState(pc);
237    for (int x = RegG0; x <= RegI0 + 7; x++)
238        context->setIntReg(x - RegG0, gdbregs.regs[x]);
239    // Only the integer registers, pc and npc are set in netbsd
240}
241
242void
243RemoteGDB::clearSingleStep()
244{
245   if (nextBkpt)
246       clearTempBreakpoint(nextBkpt);
247}
248
249void
250RemoteGDB::setSingleStep()
251{
252    nextBkpt = context->pcState().npc();
253    setTempBreakpoint(nextBkpt);
254}
255