remote_gdb.cc revision 8229
12SN/A/*
21762SN/A * Copyright (c) 2010 ARM Limited
32SN/A * All rights reserved
42SN/A *
52SN/A * The license below extends only to copyright in the software and shall
62SN/A * not be construed as granting a license to any other intellectual
72SN/A * property including but not limited to intellectual property relating
82SN/A * to a hardware implementation of the functionality of the software
92SN/A * licensed hereunder.  You may use the software subject to the license
102SN/A * terms below provided that you ensure that this notice is replicated
112SN/A * unmodified and in its entirety in all distributions of the software,
122SN/A * modified or unmodified, in source code or in binary form.
132SN/A *
142SN/A * Copyright (c) 2002-2005 The Regents of The University of Michigan
152SN/A * All rights reserved.
162SN/A *
172SN/A * Redistribution and use in source and binary forms, with or without
182SN/A * modification, are permitted provided that the following conditions are
192SN/A * met: redistributions of source code must retain the above copyright
202SN/A * notice, this list of conditions and the following disclaimer;
212SN/A * redistributions in binary form must reproduce the above copyright
222SN/A * notice, this list of conditions and the following disclaimer in the
232SN/A * documentation and/or other materials provided with the distribution;
242SN/A * neither the name of the copyright holders nor the names of its
252SN/A * contributors may be used to endorse or promote products derived from
262SN/A * this software without specific prior written permission.
272665Ssaidi@eecs.umich.edu *
282665Ssaidi@eecs.umich.edu * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
292SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
302SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
318229Snate@binkert.org * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
322SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
332SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
342SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
352SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
362SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
37148SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
3856SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
395889Snate@binkert.org *
40441SN/A * Authors: Nathan Binkert
4156SN/A *          William Wang
4256SN/A */
4356SN/A
44441SN/A/*
45433SN/A * Copyright (c) 1990, 1993
462SN/A *      The Regents of the University of California.  All rights reserved.
472SN/A *
482SN/A * This software was developed by the Computer Systems Engineering group
49729SN/A * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
50388SN/A * contributed to Berkeley.
518243Sbradley.danofsky@amd.com *
525887Snate@binkert.org * All advertising materials mentioning features or use of this software
535887Snate@binkert.org * must display the following acknowledgement:
545887Snate@binkert.org *      This product includes software developed by the University of
555887Snate@binkert.org *      California, Lawrence Berkeley Laboratories.
56388SN/A *
575887Snate@binkert.org * Redistribution and use in source and binary forms, with or without
585887Snate@binkert.org * modification, are permitted provided that the following conditions
59388SN/A * are met:
60388SN/A * 1. Redistributions of source code must retain the above copyright
615887Snate@binkert.org *    notice, this list of conditions and the following disclaimer.
625887Snate@binkert.org * 2. Redistributions in binary form must reproduce the above copyright
63441SN/A *    notice, this list of conditions and the following disclaimer in the
645887Snate@binkert.org *    documentation and/or other materials provided with the distribution.
655887Snate@binkert.org * 3. All advertising materials mentioning features or use of this software
66441SN/A *    must display the following acknowledgement:
67441SN/A *      This product includes software developed by the University of
68388SN/A *      California, Berkeley and its contributors.
695886Snate@binkert.org * 4. Neither the name of the University nor the names of its contributors
70388SN/A *    may be used to endorse or promote products derived from this software
715887Snate@binkert.org *    without specific prior written permission.
725887Snate@binkert.org *
735887Snate@binkert.org * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
745887Snate@binkert.org * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
755887Snate@binkert.org * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
765887Snate@binkert.org * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
775887Snate@binkert.org * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
785887Snate@binkert.org * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
795887Snate@binkert.org * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
805887Snate@binkert.org * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
815887Snate@binkert.org * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
82388SN/A * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
83388SN/A * SUCH DAMAGE.
84388SN/A *
855889Snate@binkert.org *      @(#)kgdb_stub.c 8.4 (Berkeley) 1/12/94
865889Snate@binkert.org */
875889Snate@binkert.org
885889Snate@binkert.org/*-
895889Snate@binkert.org * Copyright (c) 2001 The NetBSD Foundation, Inc.
905889Snate@binkert.org * All rights reserved.
915886Snate@binkert.org *
92388SN/A * This code is derived from software contributed to The NetBSD Foundation
936130Snate@binkert.org * by Jason R. Thorpe.
94388SN/A *
95388SN/A * Redistribution and use in source and binary forms, with or without
965886Snate@binkert.org * modification, are permitted provided that the following conditions
975886Snate@binkert.org * are met:
98388SN/A * 1. Redistributions of source code must retain the above copyright
995887Snate@binkert.org *    notice, this list of conditions and the following disclaimer.
1005887Snate@binkert.org * 2. Redistributions in binary form must reproduce the above copyright
1015887Snate@binkert.org *    notice, this list of conditions and the following disclaimer in the
102388SN/A *    documentation and/or other materials provided with the distribution.
103388SN/A * 3. All advertising materials mentioning features or use of this software
1045886Snate@binkert.org *    must display the following acknowledgement:
1055886Snate@binkert.org *      This product includes software developed by the NetBSD
1065886Snate@binkert.org *      Foundation, Inc. and its contributors.
1075887Snate@binkert.org * 4. Neither the name of The NetBSD Foundation nor the names of its
1085887Snate@binkert.org *    contributors may be used to endorse or promote products derived
1095887Snate@binkert.org *    from this software without specific prior written permission.
1105886Snate@binkert.org *
1115886Snate@binkert.org * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
1125889Snate@binkert.org * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
1135889Snate@binkert.org * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
1145889Snate@binkert.org * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
1155889Snate@binkert.org * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
1166026Snate@binkert.org * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
1176026Snate@binkert.org * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
1186026Snate@binkert.org * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
1196026Snate@binkert.org * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
1206026Snate@binkert.org * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
1216026Snate@binkert.org * POSSIBILITY OF SUCH DAMAGE.
1226026Snate@binkert.org */
1235889Snate@binkert.org
1245889Snate@binkert.org/*
1255889Snate@binkert.org * $NetBSD: kgdb_stub.c,v 1.8 2001/07/07 22:58:00 wdk Exp $
1265889Snate@binkert.org *
1275886Snate@binkert.org * Taken from NetBSD
1285889Snate@binkert.org *
129582SN/A * "Stub" to allow remote cpu to debug over a serial line using gdb.
1305889Snate@binkert.org */
1315889Snate@binkert.org
1328231Snate@binkert.org#include <sys/signal.h>
133582SN/A#include <unistd.h>
134582SN/A
1355886Snate@binkert.org#include <string>
136388SN/A
137388SN/A#include "config/full_system.hh"
138388SN/A#if FULL_SYSTEM
1398248Snate@binkert.org#include "arch/arm/vtophys.hh"
1408248Snate@binkert.org#endif
1418248Snate@binkert.org
1428248Snate@binkert.org#include "arch/arm/pagetable.hh"
1438248Snate@binkert.org#include "arch/arm/registers.hh"
1448248Snate@binkert.org#include "arch/arm/remote_gdb.hh"
1458248Snate@binkert.org#include "arch/arm/utility.hh"
1468248Snate@binkert.org#include "arch/arm/vtophys.hh"
1478248Snate@binkert.org#include "base/intmath.hh"
1488248Snate@binkert.org#include "base/remote_gdb.hh"
1498248Snate@binkert.org#include "base/socket.hh"
1508248Snate@binkert.org#include "base/trace.hh"
1518248Snate@binkert.org#include "cpu/static_inst.hh"
1528248Snate@binkert.org#include "cpu/thread_context.hh"
1538248Snate@binkert.org#include "cpu/thread_state.hh"
1548248Snate@binkert.org#include "mem/page_table.hh"
1558248Snate@binkert.org#include "mem/physical.hh"
1568248Snate@binkert.org#include "mem/port.hh"
1578248Snate@binkert.org#include "sim/system.hh"
1588248Snate@binkert.org
1598248Snate@binkert.orgusing namespace std;
1608248Snate@binkert.orgusing namespace ArmISA;
1618248Snate@binkert.org
1628248Snate@binkert.orgRemoteGDB::RemoteGDB(System *_system, ThreadContext *tc)
1638248Snate@binkert.org    : BaseRemoteGDB(_system, tc, NUMREGS)
1648248Snate@binkert.org{
1658248Snate@binkert.org}
1668248Snate@binkert.org
1678248Snate@binkert.org/*
1688248Snate@binkert.org * Determine if the mapping at va..(va+len) is valid.
1698248Snate@binkert.org */
1706026Snate@binkert.orgbool
1716026Snate@binkert.orgRemoteGDB::acc(Addr va, size_t len)
1726026Snate@binkert.org{
1738248Snate@binkert.org#if FULL_SYSTEM
1748248Snate@binkert.org    Addr last_va;
1758248Snate@binkert.org    va       = truncPage(va);
1766026Snate@binkert.org    last_va  = roundPage(va + len);
1776026Snate@binkert.org
1786026Snate@binkert.org    do  {
1796026Snate@binkert.org        if (virtvalid(context, va)) {
1806026Snate@binkert.org            return true;
1816026Snate@binkert.org        }
1826026Snate@binkert.org        va += PageBytes;
1836026Snate@binkert.org    } while (va < last_va);
1846026Snate@binkert.org
1856026Snate@binkert.org    DPRINTF(GDBAcc, "acc:   %#x mapping is valid\n", va);
1866026Snate@binkert.org    return true;
1876026Snate@binkert.org#else
1886026Snate@binkert.org    TlbEntry entry;
1896026Snate@binkert.org    //Check to make sure the first byte is mapped into the processes address
1906026Snate@binkert.org    //space.
1912SN/A    if (context->getProcessPtr()->pTable->lookup(va, entry))
1925886Snate@binkert.org        return true;
1932SN/A    return false;
194388SN/A#endif
195388SN/A}
1962SN/A
1972SN/A/*
1982SN/A * Translate the kernel debugger register format into the GDB register
1992SN/A * format.
2002SN/A */
2012SN/Avoid
2022SN/ARemoteGDB::getregs()
2035599Snate@binkert.org{
2045599Snate@binkert.org    DPRINTF(GDBAcc, "getregs in remotegdb \n");
2052SN/A
2062SN/A    memset(gdbregs.regs, 0, gdbregs.bytes());
2072SN/A
2082SN/A    // R0-R15 supervisor mode
2092SN/A    // arm registers are 32 bits wide, gdb registers are 64 bits wide
2102SN/A    // two arm registers are packed into one gdb register (little endian)
2112SN/A    gdbregs.regs[REG_R0 + 0] = context->readIntReg(INTREG_R1) << 32 |
2122SN/A                                       context->readIntReg(INTREG_R0);
2132SN/A    gdbregs.regs[REG_R0 + 1] = context->readIntReg(INTREG_R3) << 32 |
2142SN/A                                       context->readIntReg(INTREG_R2);
2152SN/A    gdbregs.regs[REG_R0 + 2] = context->readIntReg(INTREG_R5) << 32 |
2162SN/A                                       context->readIntReg(INTREG_R4);
217388SN/A    gdbregs.regs[REG_R0 + 3] = context->readIntReg(INTREG_R7) << 32 |
2185886Snate@binkert.org                                       context->readIntReg(INTREG_R6);
2192SN/A    gdbregs.regs[REG_R0 + 4] = context->readIntReg(INTREG_R9) << 32 |
2206000Snate@binkert.org                                       context->readIntReg(INTREG_R8);
221582SN/A    gdbregs.regs[REG_R0 + 5] = context->readIntReg(INTREG_R11) << 32|
222695SN/A                                       context->readIntReg(INTREG_R10);
223388SN/A    gdbregs.regs[REG_R0 + 6] = context->readIntReg(INTREG_SP) << 32 |
224388SN/A                                       context->readIntReg(INTREG_R12);
225388SN/A    gdbregs.regs[REG_R0 + 7] = context->pcState().pc() << 32        |
226388SN/A                                       context->readIntReg(INTREG_LR);
2272SN/A
2287462Snate@binkert.org    // CPSR
229388SN/A    gdbregs.regs[REG_CPSR]  = context->readMiscRegNoEffect(MISCREG_CPSR);
230388SN/A
231388SN/A    // vfpv3/neon floating point registers (32 double or 64 float)
2322SN/A
233388SN/A    gdbregs.regs[REG_F0] =
2342SN/A        static_cast<uint64_t>(context->readFloatRegBits(0)) << 32 |
2352SN/A        gdbregs.regs[REG_CPSR];
2366001Snate@binkert.org
2376001Snate@binkert.org    for (int i = 1; i < (NumFloatArchRegs>>1); ++i) {
2386001Snate@binkert.org      gdbregs.regs[i + REG_F0] =
2396001Snate@binkert.org          static_cast<uint64_t>(context->readFloatRegBits(2*i)) << 32 |
2406001Snate@binkert.org          context->readFloatRegBits(2*i-1);
2416001Snate@binkert.org    }
2426128Snate@binkert.org
2436001Snate@binkert.org  // FPSCR
2446001Snate@binkert.org    gdbregs.regs[REG_FPSCR] =
2456001Snate@binkert.org        static_cast<uint64_t>(context->readMiscRegNoEffect(MISCREG_FPSCR)) << 32 |
2466001Snate@binkert.org        context->readFloatRegBits(NumFloatArchRegs - 1);
2476001Snate@binkert.org}
2486001Snate@binkert.org
2496001Snate@binkert.org/*
2506001Snate@binkert.org * Translate the GDB register format into the kernel debugger register
2516001Snate@binkert.org * format.
2526128Snate@binkert.org */
2536001Snate@binkert.orgvoid
2546001Snate@binkert.orgRemoteGDB::setregs()
2556001Snate@binkert.org{
2566001Snate@binkert.org
2576001Snate@binkert.org    DPRINTF(GDBAcc, "setregs in remotegdb \n");
2586001Snate@binkert.org
2596001Snate@binkert.org    // R0-R15 supervisor mode
2606001Snate@binkert.org    // arm registers are 32 bits wide, gdb registers are 64 bits wide
2616001Snate@binkert.org    // two arm registers are packed into one gdb register (little endian)
2626128Snate@binkert.org    context->setIntReg(INTREG_R0 , bits(gdbregs.regs[REG_R0 + 0], 31, 0));
2636001Snate@binkert.org    context->setIntReg(INTREG_R1 , bits(gdbregs.regs[REG_R0 + 0], 63, 32));
2646001Snate@binkert.org    context->setIntReg(INTREG_R2 , bits(gdbregs.regs[REG_R0 + 1], 31, 0));
2656001Snate@binkert.org    context->setIntReg(INTREG_R3 , bits(gdbregs.regs[REG_R0 + 1], 63, 32));
2666001Snate@binkert.org    context->setIntReg(INTREG_R4 , bits(gdbregs.regs[REG_R0 + 2], 31, 0));
2676001Snate@binkert.org    context->setIntReg(INTREG_R5 , bits(gdbregs.regs[REG_R0 + 2], 63, 32));
2686001Snate@binkert.org    context->setIntReg(INTREG_R6 , bits(gdbregs.regs[REG_R0 + 3], 31, 0));
2696001Snate@binkert.org    context->setIntReg(INTREG_R7 , bits(gdbregs.regs[REG_R0 + 3], 63, 32));
2706001Snate@binkert.org    context->setIntReg(INTREG_R8 , bits(gdbregs.regs[REG_R0 + 4], 31, 0));
271695SN/A    context->setIntReg(INTREG_R9 , bits(gdbregs.regs[REG_R0 + 4], 63, 32));
2727831Snate@binkert.org    context->setIntReg(INTREG_R10, bits(gdbregs.regs[REG_R0 + 5], 31, 0));
2737831Snate@binkert.org    context->setIntReg(INTREG_R11, bits(gdbregs.regs[REG_R0 + 5], 63, 32));
2747831Snate@binkert.org    context->setIntReg(INTREG_R12, bits(gdbregs.regs[REG_R0 + 6], 31, 0));
2757831Snate@binkert.org    context->setIntReg(INTREG_SP , bits(gdbregs.regs[REG_R0 + 6], 63, 32));
2767831Snate@binkert.org    context->setIntReg(INTREG_LR , bits(gdbregs.regs[REG_R0 + 7], 31, 0));
2777831Snate@binkert.org    context->pcState(bits(gdbregs.regs[REG_R0 + 7], 63, 32));
2787831Snate@binkert.org
2797831Snate@binkert.org    //CPSR
2807831Snate@binkert.org    context->setMiscRegNoEffect(MISCREG_CPSR, gdbregs.regs[REG_CPSR]);
2817831Snate@binkert.org
2827831Snate@binkert.org    //vfpv3/neon floating point registers (32 double or 64 float)
2837831Snate@binkert.org    context->setFloatRegBits(0, gdbregs.regs[REG_F0]>>32);
2847831Snate@binkert.org
2857831Snate@binkert.org    for (int i = 1; i < NumFloatArchRegs; ++i) {
2867831Snate@binkert.org      if(i%2){
2877831Snate@binkert.org        int j = (i+1)/2;
2887831Snate@binkert.org        context->setFloatRegBits(i, bits(gdbregs.regs[j + REG_F0], 31, 0));
2897831Snate@binkert.org      }
2907831Snate@binkert.org      else{
2917831Snate@binkert.org        int j = i/2;
2927831Snate@binkert.org        context->setFloatRegBits(i, gdbregs.regs[j + REG_F0]>>32);
2937831Snate@binkert.org      }
2947831Snate@binkert.org    }
2957831Snate@binkert.org
2967831Snate@binkert.org    //FPSCR
2977831Snate@binkert.org    context->setMiscRegNoEffect(MISCREG_FPSCR, gdbregs.regs[REG_FPSCR]>>32);
2987831Snate@binkert.org}
2997831Snate@binkert.org
3007831Snate@binkert.orgvoid
3017831Snate@binkert.orgRemoteGDB::clearSingleStep()
3027831Snate@binkert.org{
3037831Snate@binkert.org    DPRINTF(GDBMisc, "clearSingleStep bt_addr=%#x nt_addr=%#x\n",
3047831Snate@binkert.org            takenBkpt, notTakenBkpt);
3057831Snate@binkert.org
3067831Snate@binkert.org    if (takenBkpt != 0)
3077831Snate@binkert.org        clearTempBreakpoint(takenBkpt);
3087831Snate@binkert.org
3097831Snate@binkert.org    if (notTakenBkpt != 0)
3107831Snate@binkert.org        clearTempBreakpoint(notTakenBkpt);
3117831Snate@binkert.org}
3127831Snate@binkert.org
3137831Snate@binkert.orgvoid
3147831Snate@binkert.orgRemoteGDB::setSingleStep()
3157831Snate@binkert.org{
3167831Snate@binkert.org    PCState pc = context->pcState();
3177831Snate@binkert.org    PCState bpc;
3187831Snate@binkert.org    bool set_bt = false;
3197831Snate@binkert.org
3207831Snate@binkert.org    // User was stopped at pc, e.g. the instruction at pc was not
3217831Snate@binkert.org    // executed.
3227831Snate@binkert.org    MachInst inst = read<MachInst>(pc.pc());
3237831Snate@binkert.org    StaticInstPtr si(inst, pc.pc());
3247831Snate@binkert.org    if (si->hasBranchTarget(pc, context, bpc)) {
3257831Snate@binkert.org        // Don't bother setting a breakpoint on the taken branch if it
3267831Snate@binkert.org        // is the same as the next pc
3277831Snate@binkert.org        if (bpc.pc() != pc.npc())
3287831Snate@binkert.org            set_bt = true;
3297831Snate@binkert.org    }
3307831Snate@binkert.org
3317831Snate@binkert.org    DPRINTF(GDBMisc, "setSingleStep bt_addr=%#x nt_addr=%#x\n",
3327831Snate@binkert.org            takenBkpt, notTakenBkpt);
3337831Snate@binkert.org
3347831Snate@binkert.org    setTempBreakpoint(notTakenBkpt = pc.npc());
3357831Snate@binkert.org
3367831Snate@binkert.org    if (set_bt)
3377831Snate@binkert.org        setTempBreakpoint(takenBkpt = bpc.pc());
3387831Snate@binkert.org}
3397831Snate@binkert.org
3407831Snate@binkert.org// Write bytes to kernel address space for debugger.
3417831Snate@binkert.orgbool
3427831Snate@binkert.orgRemoteGDB::write(Addr vaddr, size_t size, const char *data)
3437831Snate@binkert.org{
3447831Snate@binkert.org    return BaseRemoteGDB::write(vaddr, size, data);
3457831Snate@binkert.org}
3467831Snate@binkert.org
3477831Snate@binkert.org