remote_gdb.cc revision 8229
16145SN/A/*
28688SN/A * Copyright (c) 2010 ARM Limited
36145SN/A * All rights reserved
46145SN/A *
56145SN/A * The license below extends only to copyright in the software and shall
66145SN/A * not be construed as granting a license to any other intellectual
76145SN/A * property including but not limited to intellectual property relating
86145SN/A * to a hardware implementation of the functionality of the software
96145SN/A * licensed hereunder.  You may use the software subject to the license
106145SN/A * terms below provided that you ensure that this notice is replicated
116145SN/A * unmodified and in its entirety in all distributions of the software,
126145SN/A * modified or unmodified, in source code or in binary form.
136145SN/A *
146145SN/A * Copyright (c) 2002-2005 The Regents of The University of Michigan
156145SN/A * All rights reserved.
166145SN/A *
176145SN/A * Redistribution and use in source and binary forms, with or without
186145SN/A * modification, are permitted provided that the following conditions are
196145SN/A * met: redistributions of source code must retain the above copyright
206145SN/A * notice, this list of conditions and the following disclaimer;
216145SN/A * redistributions in binary form must reproduce the above copyright
226145SN/A * notice, this list of conditions and the following disclaimer in the
236145SN/A * documentation and/or other materials provided with the distribution;
246145SN/A * neither the name of the copyright holders nor the names of its
256145SN/A * contributors may be used to endorse or promote products derived from
266145SN/A * this software without specific prior written permission.
276145SN/A *
286145SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
296145SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
307039SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
317039SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
327039SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
336145SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
346145SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
3512492Sodanrc@yahoo.com.br * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
3612492Sodanrc@yahoo.com.br * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
376145SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
387039SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
399350SN/A *
4011108Sdavid.hashe@amd.com * Authors: Nathan Binkert
4110301SN/A *          William Wang
4210301SN/A */
4310301SN/A
447039SN/A/*
459206SN/A * Copyright (c) 1990, 1993
466145SN/A *      The Regents of the University of California.  All rights reserved.
477039SN/A *
4810920SN/A * This software was developed by the Computer Systems Engineering group
496285SN/A * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
509206SN/A * contributed to Berkeley.
517039SN/A *
527039SN/A * All advertising materials mentioning features or use of this software
536876SN/A * must display the following acknowledgement:
546876SN/A *      This product includes software developed by the University of
557039SN/A *      California, Lawrence Berkeley Laboratories.
566145SN/A *
577039SN/A * Redistribution and use in source and binary forms, with or without
587039SN/A * modification, are permitted provided that the following conditions
599504SN/A * are met:
609504SN/A * 1. Redistributions of source code must retain the above copyright
619504SN/A *    notice, this list of conditions and the following disclaimer.
6210837SN/A * 2. Redistributions in binary form must reproduce the above copyright
6310837SN/A *    notice, this list of conditions and the following disclaimer in the
646285SN/A *    documentation and/or other materials provided with the distribution.
6510525SN/A * 3. All advertising materials mentioning features or use of this software
6610918SN/A *    must display the following acknowledgement:
6711294Sandreas.hansson@arm.com *      This product includes software developed by the University of
6810525SN/A *      California, Berkeley and its contributors.
697039SN/A * 4. Neither the name of the University nor the names of its contributors
707039SN/A *    may be used to endorse or promote products derived from this software
717039SN/A *    without specific prior written permission.
727039SN/A *
7310012SN/A * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
7410012SN/A * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
757039SN/A * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
766285SN/A * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
7711523Sdavid.guillen@arm.com * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
7811523Sdavid.guillen@arm.com * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
7911523Sdavid.guillen@arm.com * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
8011523Sdavid.guillen@arm.com * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
8110012SN/A * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
8211169Sandreas.hansson@arm.com * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
836285SN/A * SUCH DAMAGE.
8411169Sandreas.hansson@arm.com *
8511168Sandreas.hansson@arm.com *      @(#)kgdb_stub.c 8.4 (Berkeley) 1/12/94
8611168Sandreas.hansson@arm.com */
8711168Sandreas.hansson@arm.com
888688SN/A/*-
8911169Sandreas.hansson@arm.com * Copyright (c) 2001 The NetBSD Foundation, Inc.
909270SN/A * All rights reserved.
919270SN/A *
927562SN/A * This code is derived from software contributed to The NetBSD Foundation
938436SN/A * by Jason R. Thorpe.
948436SN/A *
958436SN/A * Redistribution and use in source and binary forms, with or without
968937SN/A * modification, are permitted provided that the following conditions
978937SN/A * are met:
988937SN/A * 1. Redistributions of source code must retain the above copyright
9912133Sspwilson2@wisc.edu *    notice, this list of conditions and the following disclaimer.
10012133Sspwilson2@wisc.edu * 2. Redistributions in binary form must reproduce the above copyright
1018937SN/A *    notice, this list of conditions and the following disclaimer in the
1028937SN/A *    documentation and/or other materials provided with the distribution.
1038937SN/A * 3. All advertising materials mentioning features or use of this software
1047039SN/A *    must display the following acknowledgement:
1057039SN/A *      This product includes software developed by the NetBSD
1067039SN/A *      Foundation, Inc. and its contributors.
1077039SN/A * 4. Neither the name of The NetBSD Foundation nor the names of its
1086285SN/A *    contributors may be used to endorse or promote products derived
10910991SN/A *    from this software without specific prior written permission.
11011061SN/A *
11111061SN/A * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
11210991SN/A * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
11311060SN/A * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
11411060SN/A * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
11511061SN/A * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
11611060SN/A * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
11711061SN/A * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
1187039SN/A * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
11912133Sspwilson2@wisc.edu * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
1207039SN/A * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
1217039SN/A * POSSIBILITY OF SUCH DAMAGE.
1227039SN/A */
1239504SN/A
1249504SN/A/*
1259504SN/A * $NetBSD: kgdb_stub.c,v 1.8 2001/07/07 22:58:00 wdk Exp $
12610919SN/A *
12710837SN/A * Taken from NetBSD
12810837SN/A *
12910837SN/A * "Stub" to allow remote cpu to debug over a serial line using gdb.
13010525SN/A */
13110706SN/A
1327039SN/A#include <sys/signal.h>
13310012SN/A#include <unistd.h>
1349300SN/A
13510918SN/A#include <string>
1369103SN/A
1377039SN/A#include "config/full_system.hh"
13810012SN/A#if FULL_SYSTEM
1398688SN/A#include "arch/arm/vtophys.hh"
14010920SN/A#endif
1416145SN/A
1426145SN/A#include "arch/arm/pagetable.hh"
14310012SN/A#include "arch/arm/registers.hh"
1446895SN/A#include "arch/arm/remote_gdb.hh"
1456895SN/A#include "arch/arm/utility.hh"
14610919SN/A#include "arch/arm/vtophys.hh"
1476895SN/A#include "base/intmath.hh"
1486895SN/A#include "base/remote_gdb.hh"
14910012SN/A#include "base/socket.hh"
15010919SN/A#include "base/trace.hh"
15110919SN/A#include "cpu/static_inst.hh"
1526895SN/A#include "cpu/thread_context.hh"
1536895SN/A#include "cpu/thread_state.hh"
15412492Sodanrc@yahoo.com.br#include "mem/page_table.hh"
155#include "mem/physical.hh"
156#include "mem/port.hh"
157#include "sim/system.hh"
158
159using namespace std;
160using namespace ArmISA;
161
162RemoteGDB::RemoteGDB(System *_system, ThreadContext *tc)
163    : BaseRemoteGDB(_system, tc, NUMREGS)
164{
165}
166
167/*
168 * Determine if the mapping at va..(va+len) is valid.
169 */
170bool
171RemoteGDB::acc(Addr va, size_t len)
172{
173#if FULL_SYSTEM
174    Addr last_va;
175    va       = truncPage(va);
176    last_va  = roundPage(va + len);
177
178    do  {
179        if (virtvalid(context, va)) {
180            return true;
181        }
182        va += PageBytes;
183    } while (va < last_va);
184
185    DPRINTF(GDBAcc, "acc:   %#x mapping is valid\n", va);
186    return true;
187#else
188    TlbEntry entry;
189    //Check to make sure the first byte is mapped into the processes address
190    //space.
191    if (context->getProcessPtr()->pTable->lookup(va, entry))
192        return true;
193    return false;
194#endif
195}
196
197/*
198 * Translate the kernel debugger register format into the GDB register
199 * format.
200 */
201void
202RemoteGDB::getregs()
203{
204    DPRINTF(GDBAcc, "getregs in remotegdb \n");
205
206    memset(gdbregs.regs, 0, gdbregs.bytes());
207
208    // R0-R15 supervisor mode
209    // arm registers are 32 bits wide, gdb registers are 64 bits wide
210    // two arm registers are packed into one gdb register (little endian)
211    gdbregs.regs[REG_R0 + 0] = context->readIntReg(INTREG_R1) << 32 |
212                                       context->readIntReg(INTREG_R0);
213    gdbregs.regs[REG_R0 + 1] = context->readIntReg(INTREG_R3) << 32 |
214                                       context->readIntReg(INTREG_R2);
215    gdbregs.regs[REG_R0 + 2] = context->readIntReg(INTREG_R5) << 32 |
216                                       context->readIntReg(INTREG_R4);
217    gdbregs.regs[REG_R0 + 3] = context->readIntReg(INTREG_R7) << 32 |
218                                       context->readIntReg(INTREG_R6);
219    gdbregs.regs[REG_R0 + 4] = context->readIntReg(INTREG_R9) << 32 |
220                                       context->readIntReg(INTREG_R8);
221    gdbregs.regs[REG_R0 + 5] = context->readIntReg(INTREG_R11) << 32|
222                                       context->readIntReg(INTREG_R10);
223    gdbregs.regs[REG_R0 + 6] = context->readIntReg(INTREG_SP) << 32 |
224                                       context->readIntReg(INTREG_R12);
225    gdbregs.regs[REG_R0 + 7] = context->pcState().pc() << 32        |
226                                       context->readIntReg(INTREG_LR);
227
228    // CPSR
229    gdbregs.regs[REG_CPSR]  = context->readMiscRegNoEffect(MISCREG_CPSR);
230
231    // vfpv3/neon floating point registers (32 double or 64 float)
232
233    gdbregs.regs[REG_F0] =
234        static_cast<uint64_t>(context->readFloatRegBits(0)) << 32 |
235        gdbregs.regs[REG_CPSR];
236
237    for (int i = 1; i < (NumFloatArchRegs>>1); ++i) {
238      gdbregs.regs[i + REG_F0] =
239          static_cast<uint64_t>(context->readFloatRegBits(2*i)) << 32 |
240          context->readFloatRegBits(2*i-1);
241    }
242
243  // FPSCR
244    gdbregs.regs[REG_FPSCR] =
245        static_cast<uint64_t>(context->readMiscRegNoEffect(MISCREG_FPSCR)) << 32 |
246        context->readFloatRegBits(NumFloatArchRegs - 1);
247}
248
249/*
250 * Translate the GDB register format into the kernel debugger register
251 * format.
252 */
253void
254RemoteGDB::setregs()
255{
256
257    DPRINTF(GDBAcc, "setregs in remotegdb \n");
258
259    // R0-R15 supervisor mode
260    // arm registers are 32 bits wide, gdb registers are 64 bits wide
261    // two arm registers are packed into one gdb register (little endian)
262    context->setIntReg(INTREG_R0 , bits(gdbregs.regs[REG_R0 + 0], 31, 0));
263    context->setIntReg(INTREG_R1 , bits(gdbregs.regs[REG_R0 + 0], 63, 32));
264    context->setIntReg(INTREG_R2 , bits(gdbregs.regs[REG_R0 + 1], 31, 0));
265    context->setIntReg(INTREG_R3 , bits(gdbregs.regs[REG_R0 + 1], 63, 32));
266    context->setIntReg(INTREG_R4 , bits(gdbregs.regs[REG_R0 + 2], 31, 0));
267    context->setIntReg(INTREG_R5 , bits(gdbregs.regs[REG_R0 + 2], 63, 32));
268    context->setIntReg(INTREG_R6 , bits(gdbregs.regs[REG_R0 + 3], 31, 0));
269    context->setIntReg(INTREG_R7 , bits(gdbregs.regs[REG_R0 + 3], 63, 32));
270    context->setIntReg(INTREG_R8 , bits(gdbregs.regs[REG_R0 + 4], 31, 0));
271    context->setIntReg(INTREG_R9 , bits(gdbregs.regs[REG_R0 + 4], 63, 32));
272    context->setIntReg(INTREG_R10, bits(gdbregs.regs[REG_R0 + 5], 31, 0));
273    context->setIntReg(INTREG_R11, bits(gdbregs.regs[REG_R0 + 5], 63, 32));
274    context->setIntReg(INTREG_R12, bits(gdbregs.regs[REG_R0 + 6], 31, 0));
275    context->setIntReg(INTREG_SP , bits(gdbregs.regs[REG_R0 + 6], 63, 32));
276    context->setIntReg(INTREG_LR , bits(gdbregs.regs[REG_R0 + 7], 31, 0));
277    context->pcState(bits(gdbregs.regs[REG_R0 + 7], 63, 32));
278
279    //CPSR
280    context->setMiscRegNoEffect(MISCREG_CPSR, gdbregs.regs[REG_CPSR]);
281
282    //vfpv3/neon floating point registers (32 double or 64 float)
283    context->setFloatRegBits(0, gdbregs.regs[REG_F0]>>32);
284
285    for (int i = 1; i < NumFloatArchRegs; ++i) {
286      if(i%2){
287        int j = (i+1)/2;
288        context->setFloatRegBits(i, bits(gdbregs.regs[j + REG_F0], 31, 0));
289      }
290      else{
291        int j = i/2;
292        context->setFloatRegBits(i, gdbregs.regs[j + REG_F0]>>32);
293      }
294    }
295
296    //FPSCR
297    context->setMiscRegNoEffect(MISCREG_FPSCR, gdbregs.regs[REG_FPSCR]>>32);
298}
299
300void
301RemoteGDB::clearSingleStep()
302{
303    DPRINTF(GDBMisc, "clearSingleStep bt_addr=%#x nt_addr=%#x\n",
304            takenBkpt, notTakenBkpt);
305
306    if (takenBkpt != 0)
307        clearTempBreakpoint(takenBkpt);
308
309    if (notTakenBkpt != 0)
310        clearTempBreakpoint(notTakenBkpt);
311}
312
313void
314RemoteGDB::setSingleStep()
315{
316    PCState pc = context->pcState();
317    PCState bpc;
318    bool set_bt = false;
319
320    // User was stopped at pc, e.g. the instruction at pc was not
321    // executed.
322    MachInst inst = read<MachInst>(pc.pc());
323    StaticInstPtr si(inst, pc.pc());
324    if (si->hasBranchTarget(pc, context, bpc)) {
325        // Don't bother setting a breakpoint on the taken branch if it
326        // is the same as the next pc
327        if (bpc.pc() != pc.npc())
328            set_bt = true;
329    }
330
331    DPRINTF(GDBMisc, "setSingleStep bt_addr=%#x nt_addr=%#x\n",
332            takenBkpt, notTakenBkpt);
333
334    setTempBreakpoint(notTakenBkpt = pc.npc());
335
336    if (set_bt)
337        setTempBreakpoint(takenBkpt = bpc.pc());
338}
339
340// Write bytes to kernel address space for debugger.
341bool
342RemoteGDB::write(Addr vaddr, size_t size, const char *data)
343{
344    return BaseRemoteGDB::write(vaddr, size, data);
345}
346
347