remote_gdb.cc revision 8332
110152Satgutier@umich.edu/*
210152Satgutier@umich.edu * Copyright (c) 2010 ARM Limited
310152Satgutier@umich.edu * All rights reserved
410152Satgutier@umich.edu *
510152Satgutier@umich.edu * The license below extends only to copyright in the software and shall
610152Satgutier@umich.edu * not be construed as granting a license to any other intellectual
710152Satgutier@umich.edu * property including but not limited to intellectual property relating
810152Satgutier@umich.edu * to a hardware implementation of the functionality of the software
910152Satgutier@umich.edu * licensed hereunder.  You may use the software subject to the license
1010152Satgutier@umich.edu * terms below provided that you ensure that this notice is replicated
1110152Satgutier@umich.edu * unmodified and in its entirety in all distributions of the software,
1210152Satgutier@umich.edu * modified or unmodified, in source code or in binary form.
1310152Satgutier@umich.edu *
1410152Satgutier@umich.edu * Copyright (c) 2002-2005 The Regents of The University of Michigan
1510152Satgutier@umich.edu * All rights reserved.
1610152Satgutier@umich.edu *
1710152Satgutier@umich.edu * Redistribution and use in source and binary forms, with or without
1810152Satgutier@umich.edu * modification, are permitted provided that the following conditions are
1910152Satgutier@umich.edu * met: redistributions of source code must retain the above copyright
2010152Satgutier@umich.edu * notice, this list of conditions and the following disclaimer;
2110152Satgutier@umich.edu * redistributions in binary form must reproduce the above copyright
2210152Satgutier@umich.edu * notice, this list of conditions and the following disclaimer in the
2310152Satgutier@umich.edu * documentation and/or other materials provided with the distribution;
2410152Satgutier@umich.edu * neither the name of the copyright holders nor the names of its
2510152Satgutier@umich.edu * contributors may be used to endorse or promote products derived from
2610152Satgutier@umich.edu * this software without specific prior written permission.
2710152Satgutier@umich.edu *
2810152Satgutier@umich.edu * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
2910152Satgutier@umich.edu * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
3010152Satgutier@umich.edu * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
3110152Satgutier@umich.edu * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
3210152Satgutier@umich.edu * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
3310152Satgutier@umich.edu * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
3410152Satgutier@umich.edu * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
3510152Satgutier@umich.edu * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
3610152Satgutier@umich.edu * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
3710152Satgutier@umich.edu * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
3810152Satgutier@umich.edu * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3910152Satgutier@umich.edu *
4010152Satgutier@umich.edu * Authors: Nathan Binkert
4110152Satgutier@umich.edu *          William Wang
4210152Satgutier@umich.edu */
4310152Satgutier@umich.edu
4410152Satgutier@umich.edu/*
4510152Satgutier@umich.edu * Copyright (c) 1990, 1993 The Regents of the University of California
4610152Satgutier@umich.edu * All rights reserved
4710152Satgutier@umich.edu *
4810152Satgutier@umich.edu * This software was developed by the Computer Systems Engineering group
4910152Satgutier@umich.edu * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
5010152Satgutier@umich.edu * contributed to Berkeley.
5110152Satgutier@umich.edu *
5210152Satgutier@umich.edu * All advertising materials mentioning features or use of this software
5310152Satgutier@umich.edu * must display the following acknowledgement:
5410152Satgutier@umich.edu *      This product includes software developed by the University of
5510152Satgutier@umich.edu *      California, Lawrence Berkeley Laboratories.
5610152Satgutier@umich.edu *
5710152Satgutier@umich.edu * Redistribution and use in source and binary forms, with or without
5810152Satgutier@umich.edu * modification, are permitted provided that the following conditions
5910152Satgutier@umich.edu * are met:
6010152Satgutier@umich.edu * 1. Redistributions of source code must retain the above copyright
6110152Satgutier@umich.edu *    notice, this list of conditions and the following disclaimer.
6210152Satgutier@umich.edu * 2. Redistributions in binary form must reproduce the above copyright
6310152Satgutier@umich.edu *    notice, this list of conditions and the following disclaimer in the
6410152Satgutier@umich.edu *    documentation and/or other materials provided with the distribution.
6510152Satgutier@umich.edu * 3. All advertising materials mentioning features or use of this software
6610152Satgutier@umich.edu *    must display the following acknowledgement:
6710152Satgutier@umich.edu *      This product includes software developed by the University of
6810152Satgutier@umich.edu *      California, Berkeley and its contributors.
6910152Satgutier@umich.edu * 4. Neither the name of the University nor the names of its contributors
7010152Satgutier@umich.edu *    may be used to endorse or promote products derived from this software
7110152Satgutier@umich.edu *    without specific prior written permission.
7210152Satgutier@umich.edu *
7310152Satgutier@umich.edu * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
7410152Satgutier@umich.edu * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
7510152Satgutier@umich.edu * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
7610152Satgutier@umich.edu * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
7710152Satgutier@umich.edu * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
7810152Satgutier@umich.edu * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
7910152Satgutier@umich.edu * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
8010152Satgutier@umich.edu * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
8110152Satgutier@umich.edu * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
8210152Satgutier@umich.edu * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
8310152Satgutier@umich.edu * SUCH DAMAGE.
8410152Satgutier@umich.edu *
8510152Satgutier@umich.edu *      @(#)kgdb_stub.c 8.4 (Berkeley) 1/12/94
8610152Satgutier@umich.edu */
8710152Satgutier@umich.edu
8810152Satgutier@umich.edu/*-
8910152Satgutier@umich.edu * Copyright (c) 2001 The NetBSD Foundation, Inc.
9010152Satgutier@umich.edu * All rights reserved.
9110152Satgutier@umich.edu *
9210152Satgutier@umich.edu * This code is derived from software contributed to The NetBSD Foundation
9310152Satgutier@umich.edu * by Jason R. Thorpe.
9410152Satgutier@umich.edu *
9510152Satgutier@umich.edu * Redistribution and use in source and binary forms, with or without
9610152Satgutier@umich.edu * modification, are permitted provided that the following conditions
9710152Satgutier@umich.edu * are met:
9810152Satgutier@umich.edu * 1. Redistributions of source code must retain the above copyright
9910152Satgutier@umich.edu *    notice, this list of conditions and the following disclaimer.
10010152Satgutier@umich.edu * 2. Redistributions in binary form must reproduce the above copyright
10110152Satgutier@umich.edu *    notice, this list of conditions and the following disclaimer in the
10210152Satgutier@umich.edu *    documentation and/or other materials provided with the distribution.
10310152Satgutier@umich.edu * 3. All advertising materials mentioning features or use of this software
10410152Satgutier@umich.edu *    must display the following acknowledgement:
10510152Satgutier@umich.edu *      This product includes software developed by the NetBSD
10610152Satgutier@umich.edu *      Foundation, Inc. and its contributors.
10710152Satgutier@umich.edu * 4. Neither the name of The NetBSD Foundation nor the names of its
10810152Satgutier@umich.edu *    contributors may be used to endorse or promote products derived
10910152Satgutier@umich.edu *    from this software without specific prior written permission.
11010152Satgutier@umich.edu *
11110152Satgutier@umich.edu * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
11210152Satgutier@umich.edu * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
11310152Satgutier@umich.edu * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
114 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
115 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
116 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
117 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
118 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
119 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
120 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
121 * POSSIBILITY OF SUCH DAMAGE.
122 */
123
124/*
125 * $NetBSD: kgdb_stub.c,v 1.8 2001/07/07 22:58:00 wdk Exp $
126 *
127 * Taken from NetBSD
128 *
129 * "Stub" to allow remote cpu to debug over a serial line using gdb.
130 */
131
132#include <sys/signal.h>
133#include <unistd.h>
134
135#include <string>
136
137#include "config/full_system.hh"
138#if FULL_SYSTEM
139#include "arch/arm/vtophys.hh"
140#endif
141
142#include "arch/arm/pagetable.hh"
143#include "arch/arm/registers.hh"
144#include "arch/arm/remote_gdb.hh"
145#include "arch/arm/utility.hh"
146#include "arch/arm/vtophys.hh"
147#include "base/intmath.hh"
148#include "base/remote_gdb.hh"
149#include "base/socket.hh"
150#include "base/trace.hh"
151#include "cpu/static_inst.hh"
152#include "cpu/thread_context.hh"
153#include "cpu/thread_state.hh"
154#include "debug/GDBAcc.hh"
155#include "debug/GDBMisc.hh"
156#include "mem/page_table.hh"
157#include "mem/physical.hh"
158#include "mem/port.hh"
159#include "sim/system.hh"
160
161using namespace std;
162using namespace ArmISA;
163
164RemoteGDB::RemoteGDB(System *_system, ThreadContext *tc)
165    : BaseRemoteGDB(_system, tc, NUMREGS)
166{
167}
168
169/*
170 * Determine if the mapping at va..(va+len) is valid.
171 */
172bool
173RemoteGDB::acc(Addr va, size_t len)
174{
175#if FULL_SYSTEM
176    Addr last_va;
177    va       = truncPage(va);
178    last_va  = roundPage(va + len);
179
180    do  {
181        if (virtvalid(context, va)) {
182            return true;
183        }
184        va += PageBytes;
185    } while (va < last_va);
186
187    DPRINTF(GDBAcc, "acc:   %#x mapping is valid\n", va);
188    return true;
189#else
190    TlbEntry entry;
191    //Check to make sure the first byte is mapped into the processes address
192    //space.
193    if (context->getProcessPtr()->pTable->lookup(va, entry))
194        return true;
195    return false;
196#endif
197}
198
199/*
200 * Translate the kernel debugger register format into the GDB register
201 * format.
202 */
203void
204RemoteGDB::getregs()
205{
206    DPRINTF(GDBAcc, "getregs in remotegdb \n");
207
208    memset(gdbregs.regs, 0, gdbregs.bytes());
209
210    // R0-R15 supervisor mode
211    // arm registers are 32 bits wide, gdb registers are 64 bits wide
212    // two arm registers are packed into one gdb register (little endian)
213    gdbregs.regs[REG_R0 + 0] = context->readIntReg(INTREG_R1) << 32 |
214                                       context->readIntReg(INTREG_R0);
215    gdbregs.regs[REG_R0 + 1] = context->readIntReg(INTREG_R3) << 32 |
216                                       context->readIntReg(INTREG_R2);
217    gdbregs.regs[REG_R0 + 2] = context->readIntReg(INTREG_R5) << 32 |
218                                       context->readIntReg(INTREG_R4);
219    gdbregs.regs[REG_R0 + 3] = context->readIntReg(INTREG_R7) << 32 |
220                                       context->readIntReg(INTREG_R6);
221    gdbregs.regs[REG_R0 + 4] = context->readIntReg(INTREG_R9) << 32 |
222                                       context->readIntReg(INTREG_R8);
223    gdbregs.regs[REG_R0 + 5] = context->readIntReg(INTREG_R11) << 32|
224                                       context->readIntReg(INTREG_R10);
225    gdbregs.regs[REG_R0 + 6] = context->readIntReg(INTREG_SP) << 32 |
226                                       context->readIntReg(INTREG_R12);
227    gdbregs.regs[REG_R0 + 7] = context->pcState().pc() << 32        |
228                                       context->readIntReg(INTREG_LR);
229
230    // CPSR
231    gdbregs.regs[REG_CPSR]  = context->readMiscRegNoEffect(MISCREG_CPSR);
232
233    // vfpv3/neon floating point registers (32 double or 64 float)
234
235    gdbregs.regs[REG_F0] =
236        static_cast<uint64_t>(context->readFloatRegBits(0)) << 32 |
237        gdbregs.regs[REG_CPSR];
238
239    for (int i = 1; i < (NumFloatArchRegs>>1); ++i) {
240      gdbregs.regs[i + REG_F0] =
241          static_cast<uint64_t>(context->readFloatRegBits(2*i)) << 32 |
242          context->readFloatRegBits(2*i-1);
243    }
244
245  // FPSCR
246    gdbregs.regs[REG_FPSCR] =
247        static_cast<uint64_t>(context->readMiscRegNoEffect(MISCREG_FPSCR)) << 32 |
248        context->readFloatRegBits(NumFloatArchRegs - 1);
249}
250
251/*
252 * Translate the GDB register format into the kernel debugger register
253 * format.
254 */
255void
256RemoteGDB::setregs()
257{
258
259    DPRINTF(GDBAcc, "setregs in remotegdb \n");
260
261    // R0-R15 supervisor mode
262    // arm registers are 32 bits wide, gdb registers are 64 bits wide
263    // two arm registers are packed into one gdb register (little endian)
264    context->setIntReg(INTREG_R0 , bits(gdbregs.regs[REG_R0 + 0], 31, 0));
265    context->setIntReg(INTREG_R1 , bits(gdbregs.regs[REG_R0 + 0], 63, 32));
266    context->setIntReg(INTREG_R2 , bits(gdbregs.regs[REG_R0 + 1], 31, 0));
267    context->setIntReg(INTREG_R3 , bits(gdbregs.regs[REG_R0 + 1], 63, 32));
268    context->setIntReg(INTREG_R4 , bits(gdbregs.regs[REG_R0 + 2], 31, 0));
269    context->setIntReg(INTREG_R5 , bits(gdbregs.regs[REG_R0 + 2], 63, 32));
270    context->setIntReg(INTREG_R6 , bits(gdbregs.regs[REG_R0 + 3], 31, 0));
271    context->setIntReg(INTREG_R7 , bits(gdbregs.regs[REG_R0 + 3], 63, 32));
272    context->setIntReg(INTREG_R8 , bits(gdbregs.regs[REG_R0 + 4], 31, 0));
273    context->setIntReg(INTREG_R9 , bits(gdbregs.regs[REG_R0 + 4], 63, 32));
274    context->setIntReg(INTREG_R10, bits(gdbregs.regs[REG_R0 + 5], 31, 0));
275    context->setIntReg(INTREG_R11, bits(gdbregs.regs[REG_R0 + 5], 63, 32));
276    context->setIntReg(INTREG_R12, bits(gdbregs.regs[REG_R0 + 6], 31, 0));
277    context->setIntReg(INTREG_SP , bits(gdbregs.regs[REG_R0 + 6], 63, 32));
278    context->setIntReg(INTREG_LR , bits(gdbregs.regs[REG_R0 + 7], 31, 0));
279    context->pcState(bits(gdbregs.regs[REG_R0 + 7], 63, 32));
280
281    //CPSR
282    context->setMiscRegNoEffect(MISCREG_CPSR, gdbregs.regs[REG_CPSR]);
283
284    //vfpv3/neon floating point registers (32 double or 64 float)
285    context->setFloatRegBits(0, gdbregs.regs[REG_F0]>>32);
286
287    for (int i = 1; i < NumFloatArchRegs; ++i) {
288      if(i%2){
289        int j = (i+1)/2;
290        context->setFloatRegBits(i, bits(gdbregs.regs[j + REG_F0], 31, 0));
291      }
292      else{
293        int j = i/2;
294        context->setFloatRegBits(i, gdbregs.regs[j + REG_F0]>>32);
295      }
296    }
297
298    //FPSCR
299    context->setMiscRegNoEffect(MISCREG_FPSCR, gdbregs.regs[REG_FPSCR]>>32);
300}
301
302void
303RemoteGDB::clearSingleStep()
304{
305    DPRINTF(GDBMisc, "clearSingleStep bt_addr=%#x nt_addr=%#x\n",
306            takenBkpt, notTakenBkpt);
307
308    if (takenBkpt != 0)
309        clearTempBreakpoint(takenBkpt);
310
311    if (notTakenBkpt != 0)
312        clearTempBreakpoint(notTakenBkpt);
313}
314
315void
316RemoteGDB::setSingleStep()
317{
318    PCState pc = context->pcState();
319    PCState bpc;
320    bool set_bt = false;
321
322    // User was stopped at pc, e.g. the instruction at pc was not
323    // executed.
324    MachInst inst = read<MachInst>(pc.pc());
325    StaticInstPtr si(inst, pc.pc());
326    if (si->hasBranchTarget(pc, context, bpc)) {
327        // Don't bother setting a breakpoint on the taken branch if it
328        // is the same as the next pc
329        if (bpc.pc() != pc.npc())
330            set_bt = true;
331    }
332
333    DPRINTF(GDBMisc, "setSingleStep bt_addr=%#x nt_addr=%#x\n",
334            takenBkpt, notTakenBkpt);
335
336    setTempBreakpoint(notTakenBkpt = pc.npc());
337
338    if (set_bt)
339        setTempBreakpoint(takenBkpt = bpc.pc());
340}
341
342// Write bytes to kernel address space for debugger.
343bool
344RemoteGDB::write(Addr vaddr, size_t size, const char *data)
345{
346    return BaseRemoteGDB::write(vaddr, size, data);
347}
348
349