remote_gdb.cc revision 9202
112855Sgabeblack@google.com/*
212855Sgabeblack@google.com * Copyright (c) 2002-2005 The Regents of The University of Michigan
312855Sgabeblack@google.com * All rights reserved.
412855Sgabeblack@google.com *
512855Sgabeblack@google.com * Redistribution and use in source and binary forms, with or without
612855Sgabeblack@google.com * modification, are permitted provided that the following conditions are
712855Sgabeblack@google.com * met: redistributions of source code must retain the above copyright
812855Sgabeblack@google.com * notice, this list of conditions and the following disclaimer;
912855Sgabeblack@google.com * redistributions in binary form must reproduce the above copyright
1012855Sgabeblack@google.com * notice, this list of conditions and the following disclaimer in the
1112855Sgabeblack@google.com * documentation and/or other materials provided with the distribution;
1212855Sgabeblack@google.com * neither the name of the copyright holders nor the names of its
1312855Sgabeblack@google.com * contributors may be used to endorse or promote products derived from
1412855Sgabeblack@google.com * this software without specific prior written permission.
1512855Sgabeblack@google.com *
1612855Sgabeblack@google.com * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
1712855Sgabeblack@google.com * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
1812855Sgabeblack@google.com * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
1912855Sgabeblack@google.com * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
2012855Sgabeblack@google.com * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2112855Sgabeblack@google.com * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
2212855Sgabeblack@google.com * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2312855Sgabeblack@google.com * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2412855Sgabeblack@google.com * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2512855Sgabeblack@google.com * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2612855Sgabeblack@google.com * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2712855Sgabeblack@google.com *
2812855Sgabeblack@google.com * Authors: Nathan Binkert
2912855Sgabeblack@google.com */
3012855Sgabeblack@google.com
3112855Sgabeblack@google.com/*
3212855Sgabeblack@google.com * Copyright (c) 1990, 1993 The Regents of the University of California
3312855Sgabeblack@google.com * All rights reserved
3412855Sgabeblack@google.com *
3512855Sgabeblack@google.com * This software was developed by the Computer Systems Engineering group
3612855Sgabeblack@google.com * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
3712855Sgabeblack@google.com * contributed to Berkeley.
3812855Sgabeblack@google.com *
3912855Sgabeblack@google.com * All advertising materials mentioning features or use of this software
4012855Sgabeblack@google.com * must display the following acknowledgement:
4112855Sgabeblack@google.com *      This product includes software developed by the University of
4212855Sgabeblack@google.com *      California, Lawrence Berkeley Laboratories.
4312855Sgabeblack@google.com *
4412855Sgabeblack@google.com * Redistribution and use in source and binary forms, with or without
4512855Sgabeblack@google.com * modification, are permitted provided that the following conditions
4612855Sgabeblack@google.com * are met:
4712855Sgabeblack@google.com * 1. Redistributions of source code must retain the above copyright
4812855Sgabeblack@google.com *    notice, this list of conditions and the following disclaimer.
4912855Sgabeblack@google.com * 2. Redistributions in binary form must reproduce the above copyright
5012855Sgabeblack@google.com *    notice, this list of conditions and the following disclaimer in the
5112855Sgabeblack@google.com *    documentation and/or other materials provided with the distribution.
5212855Sgabeblack@google.com * 3. All advertising materials mentioning features or use of this software
5312855Sgabeblack@google.com *    must display the following acknowledgement:
5412855Sgabeblack@google.com *      This product includes software developed by the University of
5512855Sgabeblack@google.com *      California, Berkeley and its contributors.
5612855Sgabeblack@google.com * 4. Neither the name of the University nor the names of its contributors
5712855Sgabeblack@google.com *    may be used to endorse or promote products derived from this software
5812855Sgabeblack@google.com *    without specific prior written permission.
5912855Sgabeblack@google.com *
6012855Sgabeblack@google.com * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
6112855Sgabeblack@google.com * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
6212855Sgabeblack@google.com * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
6312855Sgabeblack@google.com * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
6412855Sgabeblack@google.com * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
6512855Sgabeblack@google.com * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
6612855Sgabeblack@google.com * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
6712855Sgabeblack@google.com * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
6812855Sgabeblack@google.com * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
6912855Sgabeblack@google.com * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
7012855Sgabeblack@google.com * SUCH DAMAGE.
7112855Sgabeblack@google.com *
7212855Sgabeblack@google.com *      @(#)kgdb_stub.c 8.4 (Berkeley) 1/12/94
7312855Sgabeblack@google.com */
7412855Sgabeblack@google.com
7512855Sgabeblack@google.com/*-
7612855Sgabeblack@google.com * Copyright (c) 2001 The NetBSD Foundation, Inc.
7712855Sgabeblack@google.com * All rights reserved.
7812855Sgabeblack@google.com *
7912855Sgabeblack@google.com * This code is derived from software contributed to The NetBSD Foundation
8012855Sgabeblack@google.com * by Jason R. Thorpe.
8112855Sgabeblack@google.com *
8212855Sgabeblack@google.com * Redistribution and use in source and binary forms, with or without
8312855Sgabeblack@google.com * modification, are permitted provided that the following conditions
8412855Sgabeblack@google.com * are met:
8512855Sgabeblack@google.com * 1. Redistributions of source code must retain the above copyright
8612855Sgabeblack@google.com *    notice, this list of conditions and the following disclaimer.
8712855Sgabeblack@google.com * 2. Redistributions in binary form must reproduce the above copyright
8812855Sgabeblack@google.com *    notice, this list of conditions and the following disclaimer in the
8912855Sgabeblack@google.com *    documentation and/or other materials provided with the distribution.
9012855Sgabeblack@google.com * 3. All advertising materials mentioning features or use of this software
9112855Sgabeblack@google.com *    must display the following acknowledgement:
9212855Sgabeblack@google.com *      This product includes software developed by the NetBSD
9312855Sgabeblack@google.com *      Foundation, Inc. and its contributors.
9412855Sgabeblack@google.com * 4. Neither the name of The NetBSD Foundation nor the names of its
9512855Sgabeblack@google.com *    contributors may be used to endorse or promote products derived
9612855Sgabeblack@google.com *    from this software without specific prior written permission.
9712855Sgabeblack@google.com *
9812855Sgabeblack@google.com * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
9912855Sgabeblack@google.com * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
10012855Sgabeblack@google.com * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
10112855Sgabeblack@google.com * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
10212855Sgabeblack@google.com * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
10312855Sgabeblack@google.com * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
10412855Sgabeblack@google.com * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
10512855Sgabeblack@google.com * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
10612855Sgabeblack@google.com * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
10712855Sgabeblack@google.com * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
10812855Sgabeblack@google.com * POSSIBILITY OF SUCH DAMAGE.
10912855Sgabeblack@google.com */
11012855Sgabeblack@google.com
11112855Sgabeblack@google.com/*
11212855Sgabeblack@google.com * $NetBSD: kgdb_stub.c,v 1.8 2001/07/07 22:58:00 wdk Exp $
11312855Sgabeblack@google.com *
11412855Sgabeblack@google.com * Taken from NetBSD
11512855Sgabeblack@google.com *
11612855Sgabeblack@google.com * "Stub" to allow remote cpu to debug over a serial line using gdb.
11712855Sgabeblack@google.com */
11812855Sgabeblack@google.com
11912855Sgabeblack@google.com#include <signal.h>
12012855Sgabeblack@google.com#include <sys/signal.h>
12112855Sgabeblack@google.com#include <unistd.h>
12212855Sgabeblack@google.com
12312855Sgabeblack@google.com#include <string>
12412855Sgabeblack@google.com
12512855Sgabeblack@google.com#include "arch/sparc/remote_gdb.hh"
12612855Sgabeblack@google.com#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 "cpu/static_inst.hh"
132#include "cpu/thread_context.hh"
133#include "debug/GDBRead.hh"
134#include "mem/page_table.hh"
135#include "mem/physical.hh"
136#include "mem/port.hh"
137#include "sim/byteswap.hh"
138#include "sim/full_system.hh"
139#include "sim/process.hh"
140#include "sim/system.hh"
141
142using namespace std;
143using namespace SparcISA;
144
145RemoteGDB::RemoteGDB(System *_system, ThreadContext *c)
146    : BaseRemoteGDB(_system, c, NumGDBRegs), nextBkpt(0)
147{}
148
149///////////////////////////////////////////////////////////
150// RemoteGDB::acc
151//
152//      Determine if the mapping at va..(va+len) is valid.
153//
154bool
155RemoteGDB::acc(Addr va, size_t len)
156{
157    //@Todo In NetBSD, this function checks if all addresses
158    // from va to va + len have valid page map entries. Not
159    // sure how this will work for other OSes or in general.
160    if (FullSystem) {
161        if (va)
162            return true;
163        return false;
164    } else {
165        TlbEntry entry;
166        // Check to make sure the first byte is mapped into the processes
167        // address space.
168        if (context->getProcessPtr()->pTable->lookup(va, entry))
169            return true;
170        return false;
171    }
172}
173
174///////////////////////////////////////////////////////////
175// RemoteGDB::getregs
176//
177//      Translate the kernel debugger register format into
178//      the GDB register format.
179void
180RemoteGDB::getregs()
181{
182    memset(gdbregs.regs, 0, gdbregs.size);
183
184    PCState pc = context->pcState();
185    PSTATE pstate = context->readMiscReg(MISCREG_PSTATE);
186
187    if (pstate.am) {
188        uint32_t *regs;
189        regs = (uint32_t*)gdbregs.regs;
190        regs[Reg32Pc] = htobe((uint32_t)pc.pc());
191        regs[Reg32Npc] = htobe((uint32_t)pc.npc());
192        for (int x = RegG0; x <= RegI0 + 7; x++)
193            regs[x] = htobe((uint32_t)context->readIntReg(x - RegG0));
194
195        regs[Reg32Y] = htobe((uint32_t)context->readIntReg(NumIntArchRegs + 1));
196        regs[Reg32Psr] = htobe((uint32_t)pstate);
197        regs[Reg32Fsr] = htobe((uint32_t)context->readMiscReg(MISCREG_FSR));
198        regs[Reg32Csr] = htobe((uint32_t)context->readIntReg(NumIntArchRegs + 2));
199    } else {
200        gdbregs.regs[RegPc] = htobe(pc.pc());
201        gdbregs.regs[RegNpc] = htobe(pc.npc());
202        for (int x = RegG0; x <= RegI0 + 7; x++)
203            gdbregs.regs[x] = htobe(context->readIntReg(x - RegG0));
204
205        gdbregs.regs[RegFsr] = htobe(context->readMiscReg(MISCREG_FSR));
206        gdbregs.regs[RegFprs] = htobe(context->readMiscReg(MISCREG_FPRS));
207        gdbregs.regs[RegY] = htobe(context->readIntReg(NumIntArchRegs + 1));
208        gdbregs.regs[RegState] = htobe(
209            context->readMiscReg(MISCREG_CWP) |
210            pstate << 8 |
211            context->readMiscReg(MISCREG_ASI) << 24 |
212            context->readIntReg(NumIntArchRegs + 2) << 32);
213    }
214
215    DPRINTF(GDBRead, "PC=%#x\n", gdbregs.regs[RegPc]);
216
217    // Floating point registers are left at 0 in netbsd
218    // All registers other than the pc, npc and int regs
219    // are ignored as well.
220}
221
222///////////////////////////////////////////////////////////
223// RemoteGDB::setregs
224//
225//      Translate the GDB register format into the kernel
226//      debugger register format.
227//
228void
229RemoteGDB::setregs()
230{
231    PCState pc;
232    pc.pc(gdbregs.regs[RegPc]);
233    pc.npc(gdbregs.regs[RegNpc]);
234    pc.nnpc(pc.npc() + sizeof(MachInst));
235    pc.upc(0);
236    pc.nupc(1);
237    context->pcState(pc);
238    for (int x = RegG0; x <= RegI0 + 7; x++)
239        context->setIntReg(x - RegG0, gdbregs.regs[x]);
240    // Only the integer registers, pc and npc are set in netbsd
241}
242
243void
244RemoteGDB::clearSingleStep()
245{
246   if (nextBkpt)
247       clearTempBreakpoint(nextBkpt);
248}
249
250void
251RemoteGDB::setSingleStep()
252{
253    nextBkpt = context->pcState().npc();
254    setTempBreakpoint(nextBkpt);
255}
256