remote_gdb.cc revision 10595
1/*
2 * Copyright 2014 Google, Inc.
3 * Copyright (c) 2002-2005 The Regents of The University of Michigan
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met: redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer;
10 * redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution;
13 * neither the name of the copyright holders nor the names of its
14 * contributors may be used to endorse or promote products derived from
15 * this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 *
29 * Authors: Nathan Binkert
30 */
31
32/*
33 * Copyright (c) 1990, 1993 The Regents of the University of California
34 * All rights reserved
35 *
36 * This software was developed by the Computer Systems Engineering group
37 * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
38 * contributed to Berkeley.
39 *
40 * All advertising materials mentioning features or use of this software
41 * must display the following acknowledgement:
42 *      This product includes software developed by the University of
43 *      California, Lawrence Berkeley Laboratories.
44 *
45 * Redistribution and use in source and binary forms, with or without
46 * modification, are permitted provided that the following conditions
47 * are met:
48 * 1. Redistributions of source code must retain the above copyright
49 *    notice, this list of conditions and the following disclaimer.
50 * 2. Redistributions in binary form must reproduce the above copyright
51 *    notice, this list of conditions and the following disclaimer in the
52 *    documentation and/or other materials provided with the distribution.
53 * 3. All advertising materials mentioning features or use of this software
54 *    must display the following acknowledgement:
55 *      This product includes software developed by the University of
56 *      California, Berkeley and its contributors.
57 * 4. Neither the name of the University nor the names of its contributors
58 *    may be used to endorse or promote products derived from this software
59 *    without specific prior written permission.
60 *
61 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
62 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
63 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
64 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
65 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
66 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
67 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
68 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
69 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
70 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
71 * SUCH DAMAGE.
72 *
73 *      @(#)kgdb_stub.c 8.4 (Berkeley) 1/12/94
74 */
75
76/*-
77 * Copyright (c) 2001 The NetBSD Foundation, Inc.
78 * All rights reserved.
79 *
80 * This code is derived from software contributed to The NetBSD Foundation
81 * by Jason R. Thorpe.
82 *
83 * Redistribution and use in source and binary forms, with or without
84 * modification, are permitted provided that the following conditions
85 * are met:
86 * 1. Redistributions of source code must retain the above copyright
87 *    notice, this list of conditions and the following disclaimer.
88 * 2. Redistributions in binary form must reproduce the above copyright
89 *    notice, this list of conditions and the following disclaimer in the
90 *    documentation and/or other materials provided with the distribution.
91 * 3. All advertising materials mentioning features or use of this software
92 *    must display the following acknowledgement:
93 *      This product includes software developed by the NetBSD
94 *      Foundation, Inc. and its contributors.
95 * 4. Neither the name of The NetBSD Foundation nor the names of its
96 *    contributors may be used to endorse or promote products derived
97 *    from this software without specific prior written permission.
98 *
99 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
100 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
101 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
102 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
103 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
104 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
105 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
106 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
107 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
108 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
109 * POSSIBILITY OF SUCH DAMAGE.
110 */
111
112/*
113 * $NetBSD: kgdb_stub.c,v 1.8 2001/07/07 22:58:00 wdk Exp $
114 *
115 * Taken from NetBSD
116 *
117 * "Stub" to allow remote cpu to debug over a serial line using gdb.
118 */
119
120#include <signal.h>
121#include <sys/signal.h>
122#include <unistd.h>
123
124#include <string>
125
126#include "arch/sparc/remote_gdb.hh"
127#include "arch/vtophys.hh"
128#include "base/intmath.hh"
129#include "base/remote_gdb.hh"
130#include "base/socket.hh"
131#include "base/trace.hh"
132#include "cpu/static_inst.hh"
133#include "cpu/thread_context.hh"
134#include "debug/GDBRead.hh"
135#include "mem/page_table.hh"
136#include "mem/physical.hh"
137#include "mem/port.hh"
138#include "sim/byteswap.hh"
139#include "sim/full_system.hh"
140#include "sim/process.hh"
141#include "sim/system.hh"
142
143using namespace std;
144using namespace SparcISA;
145
146RemoteGDB::RemoteGDB(System *_system, ThreadContext *c)
147    : BaseRemoteGDB(_system, c, NumGDBRegs * sizeof(uint64_t)), nextBkpt(0)
148{}
149
150///////////////////////////////////////////////////////////
151// RemoteGDB::acc
152//
153//      Determine if the mapping at va..(va+len) is valid.
154//
155bool
156RemoteGDB::acc(Addr va, size_t len)
157{
158    //@Todo In NetBSD, this function checks if all addresses
159    // from va to va + len have valid page map entries. Not
160    // sure how this will work for other OSes or in general.
161    if (FullSystem) {
162        if (va)
163            return true;
164        return false;
165    } else {
166        TlbEntry entry;
167        // Check to make sure the first byte is mapped into the processes
168        // address space.
169        if (context->getProcessPtr()->pTable->lookup(va, entry))
170            return true;
171        return false;
172    }
173}
174
175///////////////////////////////////////////////////////////
176// RemoteGDB::getregs
177//
178//      Translate the kernel debugger register format into
179//      the GDB register format.
180void
181RemoteGDB::getregs()
182{
183    memset(gdbregs.regs, 0, gdbregs.size);
184
185    PCState pc = context->pcState();
186    PSTATE pstate = context->readMiscReg(MISCREG_PSTATE);
187
188    if (pstate.am) {
189        gdbregs.regs32[Reg32Pc] = htobe((uint32_t)pc.pc());
190        gdbregs.regs32[Reg32Npc] = htobe((uint32_t)pc.npc());
191        for (int x = RegG0; x <= RegI0 + 7; x++)
192            gdbregs.regs32[x] = htobe((uint32_t)context->readIntReg(x - RegG0));
193
194        gdbregs.regs32[Reg32Y] =
195            htobe((uint32_t)context->readIntReg(NumIntArchRegs + 1));
196        gdbregs.regs32[Reg32Psr] = htobe((uint32_t)pstate);
197        gdbregs.regs32[Reg32Fsr] =
198            htobe((uint32_t)context->readMiscReg(MISCREG_FSR));
199        gdbregs.regs32[Reg32Csr] =
200            htobe((uint32_t)context->readIntReg(NumIntArchRegs + 2));
201    } else {
202        gdbregs.regs64[RegPc] = htobe(pc.pc());
203        gdbregs.regs64[RegNpc] = htobe(pc.npc());
204        for (int x = RegG0; x <= RegI0 + 7; x++)
205            gdbregs.regs64[x] = htobe(context->readIntReg(x - RegG0));
206
207        gdbregs.regs64[RegFsr] = htobe(context->readMiscReg(MISCREG_FSR));
208        gdbregs.regs64[RegFprs] = htobe(context->readMiscReg(MISCREG_FPRS));
209        gdbregs.regs64[RegY] = htobe(context->readIntReg(NumIntArchRegs + 1));
210        gdbregs.regs64[RegState] = htobe(
211            context->readMiscReg(MISCREG_CWP) |
212            pstate << 8 |
213            context->readMiscReg(MISCREG_ASI) << 24 |
214            context->readIntReg(NumIntArchRegs + 2) << 32);
215    }
216
217    DPRINTF(GDBRead, "PC=%#x\n", gdbregs.regs64[RegPc]);
218
219    // Floating point registers are left at 0 in netbsd
220    // All registers other than the pc, npc and int regs
221    // are ignored as well.
222}
223
224///////////////////////////////////////////////////////////
225// RemoteGDB::setregs
226//
227//      Translate the GDB register format into the kernel
228//      debugger register format.
229//
230void
231RemoteGDB::setregs()
232{
233    PCState pc;
234    pc.pc(gdbregs.regs64[RegPc]);
235    pc.npc(gdbregs.regs64[RegNpc]);
236    pc.nnpc(pc.npc() + sizeof(MachInst));
237    pc.upc(0);
238    pc.nupc(1);
239    context->pcState(pc);
240    for (int x = RegG0; x <= RegI0 + 7; x++)
241        context->setIntReg(x - RegG0, gdbregs.regs64[x]);
242    // Only the integer registers, pc and npc are set in netbsd
243}
244
245void
246RemoteGDB::clearSingleStep()
247{
248   if (nextBkpt)
249       clearTempBreakpoint(nextBkpt);
250}
251
252void
253RemoteGDB::setSingleStep()
254{
255    nextBkpt = context->pcState().npc();
256    setTempBreakpoint(nextBkpt);
257}
258