remote_gdb.cc revision 12221:3629e25eb2ec
16657Snate@binkert.org/*
26657Snate@binkert.org * Copyright 2015 LabWare
36657Snate@binkert.org * Copyright 2014 Google Inc.
46657Snate@binkert.org * Copyright (c) 2010, 2013, 2016 ARM Limited
56657Snate@binkert.org * All rights reserved
66657Snate@binkert.org *
76657Snate@binkert.org * The license below extends only to copyright in the software and shall
86657Snate@binkert.org * not be construed as granting a license to any other intellectual
96657Snate@binkert.org * property including but not limited to intellectual property relating
106657Snate@binkert.org * to a hardware implementation of the functionality of the software
116657Snate@binkert.org * licensed hereunder.  You may use the software subject to the license
126657Snate@binkert.org * terms below provided that you ensure that this notice is replicated
136657Snate@binkert.org * unmodified and in its entirety in all distributions of the software,
146657Snate@binkert.org * modified or unmodified, in source code or in binary form.
156657Snate@binkert.org *
166657Snate@binkert.org * Copyright (c) 2002-2005 The Regents of The University of Michigan
176657Snate@binkert.org * All rights reserved.
186657Snate@binkert.org *
196657Snate@binkert.org * Redistribution and use in source and binary forms, with or without
206657Snate@binkert.org * modification, are permitted provided that the following conditions are
216657Snate@binkert.org * met: redistributions of source code must retain the above copyright
226657Snate@binkert.org * notice, this list of conditions and the following disclaimer;
236657Snate@binkert.org * redistributions in binary form must reproduce the above copyright
246657Snate@binkert.org * notice, this list of conditions and the following disclaimer in the
256657Snate@binkert.org * documentation and/or other materials provided with the distribution;
266657Snate@binkert.org * neither the name of the copyright holders nor the names of its
276657Snate@binkert.org * contributors may be used to endorse or promote products derived from
286999Snate@binkert.org * this software without specific prior written permission.
296657Snate@binkert.org *
306657Snate@binkert.org * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
316657Snate@binkert.org * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
326657Snate@binkert.org * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
336657Snate@binkert.org * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
346657Snate@binkert.org * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
356657Snate@binkert.org * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
366657Snate@binkert.org * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
376657Snate@binkert.org * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
386657Snate@binkert.org * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
396657Snate@binkert.org * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
406657Snate@binkert.org * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
416657Snate@binkert.org *
426657Snate@binkert.org * Authors: Nathan Binkert
436657Snate@binkert.org *          William Wang
446657Snate@binkert.org *          Boris Shingarov
456657Snate@binkert.org */
466657Snate@binkert.org
476657Snate@binkert.org/*
486657Snate@binkert.org * Copyright (c) 1990, 1993 The Regents of the University of California
496657Snate@binkert.org * All rights reserved
506657Snate@binkert.org *
516657Snate@binkert.org * This software was developed by the Computer Systems Engineering group
526657Snate@binkert.org * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
536657Snate@binkert.org * contributed to Berkeley.
546882SBrad.Beckmann@amd.com *
556657Snate@binkert.org * All advertising materials mentioning features or use of this software
566657Snate@binkert.org * must display the following acknowledgement:
576657Snate@binkert.org *      This product includes software developed by the University of
586657Snate@binkert.org *      California, Lawrence Berkeley Laboratories.
596657Snate@binkert.org *
606657Snate@binkert.org * Redistribution and use in source and binary forms, with or without
616657Snate@binkert.org * modification, are permitted provided that the following conditions
626657Snate@binkert.org * are met:
636657Snate@binkert.org * 1. Redistributions of source code must retain the above copyright
646657Snate@binkert.org *    notice, this list of conditions and the following disclaimer.
656657Snate@binkert.org * 2. Redistributions in binary form must reproduce the above copyright
666657Snate@binkert.org *    notice, this list of conditions and the following disclaimer in the
676657Snate@binkert.org *    documentation and/or other materials provided with the distribution.
686657Snate@binkert.org * 3. All advertising materials mentioning features or use of this software
696657Snate@binkert.org *    must display the following acknowledgement:
706657Snate@binkert.org *      This product includes software developed by the University of
716657Snate@binkert.org *      California, Berkeley and its contributors.
726657Snate@binkert.org * 4. Neither the name of the University nor the names of its contributors
736657Snate@binkert.org *    may be used to endorse or promote products derived from this software
746657Snate@binkert.org *    without specific prior written permission.
756657Snate@binkert.org *
766657Snate@binkert.org * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
776657Snate@binkert.org * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
786657Snate@binkert.org * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
796657Snate@binkert.org * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
806657Snate@binkert.org * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
816657Snate@binkert.org * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
826657Snate@binkert.org * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
836657Snate@binkert.org * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
846657Snate@binkert.org * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
856657Snate@binkert.org * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
866657Snate@binkert.org * SUCH DAMAGE.
876657Snate@binkert.org *
886657Snate@binkert.org *      @(#)kgdb_stub.c 8.4 (Berkeley) 1/12/94
896657Snate@binkert.org */
906657Snate@binkert.org
916657Snate@binkert.org/*-
926657Snate@binkert.org * Copyright (c) 2001 The NetBSD Foundation, Inc.
936657Snate@binkert.org * All rights reserved.
946657Snate@binkert.org *
956657Snate@binkert.org * This code is derived from software contributed to The NetBSD Foundation
966657Snate@binkert.org * by Jason R. Thorpe.
976657Snate@binkert.org *
986657Snate@binkert.org * Redistribution and use in source and binary forms, with or without
996657Snate@binkert.org * modification, are permitted provided that the following conditions
1006657Snate@binkert.org * are met:
1016657Snate@binkert.org * 1. Redistributions of source code must retain the above copyright
1026657Snate@binkert.org *    notice, this list of conditions and the following disclaimer.
1038086SBrad.Beckmann@amd.com * 2. Redistributions in binary form must reproduce the above copyright
1048086SBrad.Beckmann@amd.com *    notice, this list of conditions and the following disclaimer in the
1058086SBrad.Beckmann@amd.com *    documentation and/or other materials provided with the distribution.
1066657Snate@binkert.org * 3. All advertising materials mentioning features or use of this software
1076657Snate@binkert.org *    must display the following acknowledgement:
1086657Snate@binkert.org *      This product includes software developed by the NetBSD
1096657Snate@binkert.org *      Foundation, Inc. and its contributors.
1106657Snate@binkert.org * 4. Neither the name of The NetBSD Foundation nor the names of its
1116657Snate@binkert.org *    contributors may be used to endorse or promote products derived
1126657Snate@binkert.org *    from this software without specific prior written permission.
1136657Snate@binkert.org *
1146657Snate@binkert.org * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
1156657Snate@binkert.org * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
1166657Snate@binkert.org * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
1176657Snate@binkert.org * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
1186657Snate@binkert.org * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
1196657Snate@binkert.org * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
1206657Snate@binkert.org * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
1216657Snate@binkert.org * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
1226657Snate@binkert.org * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
1236657Snate@binkert.org * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
1246657Snate@binkert.org * POSSIBILITY OF SUCH DAMAGE.
1256657Snate@binkert.org */
1266657Snate@binkert.org
1276657Snate@binkert.org/*
1286657Snate@binkert.org * $NetBSD: kgdb_stub.c,v 1.8 2001/07/07 22:58:00 wdk Exp $
1296657Snate@binkert.org *
1306657Snate@binkert.org * Taken from NetBSD
1316657Snate@binkert.org *
1326657Snate@binkert.org * "Stub" to allow remote cpu to debug over a serial line using gdb.
1336657Snate@binkert.org */
1346657Snate@binkert.org
1356657Snate@binkert.org#include "arch/arm/remote_gdb.hh"
1366657Snate@binkert.org
1376657Snate@binkert.org#include <sys/signal.h>
1386657Snate@binkert.org#include <unistd.h>
1396657Snate@binkert.org
1406657Snate@binkert.org#include <string>
1416657Snate@binkert.org
1426657Snate@binkert.org#include "arch/arm/decoder.hh"
1436657Snate@binkert.org#include "arch/arm/pagetable.hh"
1446657Snate@binkert.org#include "arch/arm/registers.hh"
1456657Snate@binkert.org#include "arch/arm/system.hh"
1466657Snate@binkert.org#include "arch/arm/utility.hh"
1476657Snate@binkert.org#include "arch/arm/vtophys.hh"
1486657Snate@binkert.org#include "base/chunk_generator.hh"
1496657Snate@binkert.org#include "base/intmath.hh"
1506657Snate@binkert.org#include "base/remote_gdb.hh"
1516657Snate@binkert.org#include "base/socket.hh"
1526657Snate@binkert.org#include "base/trace.hh"
1536657Snate@binkert.org#include "cpu/static_inst.hh"
1546657Snate@binkert.org#include "cpu/thread_context.hh"
1556657Snate@binkert.org#include "cpu/thread_state.hh"
1566657Snate@binkert.org#include "debug/GDBAcc.hh"
1576657Snate@binkert.org#include "debug/GDBMisc.hh"
1586657Snate@binkert.org#include "mem/page_table.hh"
1596657Snate@binkert.org#include "mem/physical.hh"
1606657Snate@binkert.org#include "mem/port.hh"
1616882SBrad.Beckmann@amd.com#include "sim/full_system.hh"
1626882SBrad.Beckmann@amd.com#include "sim/system.hh"
1636882SBrad.Beckmann@amd.com
1648086SBrad.Beckmann@amd.comusing namespace std;
1658086SBrad.Beckmann@amd.comusing namespace ArmISA;
1668086SBrad.Beckmann@amd.com
1676657Snate@binkert.orgRemoteGDB::RemoteGDB(System *_system, ThreadContext *tc)
1686657Snate@binkert.org    : BaseRemoteGDB(_system, tc), regCache32(this), regCache64(this)
1696657Snate@binkert.org{
1706657Snate@binkert.org}
1716657Snate@binkert.org
1726657Snate@binkert.org/*
1736657Snate@binkert.org * Determine if the mapping at va..(va+len) is valid.
1746657Snate@binkert.org */
1756657Snate@binkert.orgbool
1766657Snate@binkert.orgRemoteGDB::acc(Addr va, size_t len)
1776657Snate@binkert.org{
1786657Snate@binkert.org    if (FullSystem) {
1796657Snate@binkert.org        for (ChunkGenerator gen(va, len, PageBytes); !gen.done(); gen.next()) {
1806657Snate@binkert.org            if (!virtvalid(context, gen.addr())) {
1816657Snate@binkert.org                DPRINTF(GDBAcc, "acc:   %#x mapping is invalid\n", va);
1826657Snate@binkert.org                return false;
1836657Snate@binkert.org            }
1846657Snate@binkert.org        }
1856657Snate@binkert.org
1866657Snate@binkert.org        DPRINTF(GDBAcc, "acc:   %#x mapping is valid\n", va);
1879219Spower.jg@gmail.com        return true;
1886657Snate@binkert.org    } else {
1896657Snate@binkert.org        TlbEntry entry;
1906657Snate@binkert.org        //Check to make sure the first byte is mapped into the processes address
1916657Snate@binkert.org        //space.
1926657Snate@binkert.org        if (context->getProcessPtr()->pTable->lookup(va, entry))
1936657Snate@binkert.org            return true;
1946657Snate@binkert.org        return false;
1956657Snate@binkert.org    }
1966657Snate@binkert.org}
1976657Snate@binkert.org
1986657Snate@binkert.orgvoid
1996657Snate@binkert.orgRemoteGDB::AArch64GdbRegCache::getRegs(ThreadContext *context)
2006999Snate@binkert.org{
2016657Snate@binkert.org    DPRINTF(GDBAcc, "getRegs in remotegdb \n");
2026657Snate@binkert.org
2036657Snate@binkert.org    for (int i = 0; i < 31; ++i)
2046657Snate@binkert.org        r.x[i] = context->readIntReg(INTREG_X0 + i);
2056657Snate@binkert.org    r.spx = context->readIntReg(INTREG_SPX);
2066657Snate@binkert.org    r.pc = context->pcState().pc();
2076657Snate@binkert.org    r.cpsr = context->readMiscRegNoEffect(MISCREG_CPSR);
2087007Snate@binkert.org
2097007Snate@binkert.org    for (int i = 0; i < 32*4; i += 4) {
2106657Snate@binkert.org        r.v[i + 0] = context->readFloatRegBits(i + 2);
2117002Snate@binkert.org        r.v[i + 1] = context->readFloatRegBits(i + 3);
2127002Snate@binkert.org        r.v[i + 2] = context->readFloatRegBits(i + 0);
2136657Snate@binkert.org        r.v[i + 3] = context->readFloatRegBits(i + 1);
2146657Snate@binkert.org    }
2156657Snate@binkert.org
2166657Snate@binkert.org    for (int i = 0; i < 32; i ++) {
2176657Snate@binkert.org        r.vec[i] = context->readVecReg(RegId(VecRegClass,i));
2186657Snate@binkert.org    }
2196657Snate@binkert.org}
2206657Snate@binkert.org
2216657Snate@binkert.orgvoid
2226657Snate@binkert.orgRemoteGDB::AArch64GdbRegCache::setRegs(ThreadContext *context) const
2236657Snate@binkert.org{
2246657Snate@binkert.org    DPRINTF(GDBAcc, "setRegs in remotegdb \n");
2256657Snate@binkert.org
2267007Snate@binkert.org    for (int i = 0; i < 31; ++i)
2277007Snate@binkert.org        context->setIntReg(INTREG_X0 + i, r.x[i]);
2286657Snate@binkert.org    context->pcState(r.pc);
2296657Snate@binkert.org    context->setMiscRegNoEffect(MISCREG_CPSR, r.cpsr);
2307007Snate@binkert.org    // Update the stack pointer. This should be done after
2316657Snate@binkert.org    // updating CPSR/PSTATE since that might affect how SPX gets
2326657Snate@binkert.org    // mapped.
2336657Snate@binkert.org    context->setIntReg(INTREG_SPX, r.spx);
2346657Snate@binkert.org
2356657Snate@binkert.org    for (int i = 0; i < 32*4; i += 4) {
2366657Snate@binkert.org        context->setFloatRegBits(i + 2, r.v[i + 0]);
2376657Snate@binkert.org        context->setFloatRegBits(i + 3, r.v[i + 1]);
2386657Snate@binkert.org        context->setFloatRegBits(i + 0, r.v[i + 2]);
2396657Snate@binkert.org        context->setFloatRegBits(i + 1, r.v[i + 3]);
2406657Snate@binkert.org    }
2416657Snate@binkert.org
2426657Snate@binkert.org    for (int i = 0; i < 32; i ++) {
2436657Snate@binkert.org        context->setVecReg(RegId(VecRegClass, i), r.vec[i]);
2446657Snate@binkert.org    }
2456657Snate@binkert.org}
2466657Snate@binkert.org
2476657Snate@binkert.orgvoid
2486657Snate@binkert.orgRemoteGDB::AArch32GdbRegCache::getRegs(ThreadContext *context)
2496657Snate@binkert.org{
2507453Snate@binkert.org    DPRINTF(GDBAcc, "getRegs in remotegdb \n");
2517453Snate@binkert.org
2527453Snate@binkert.org    r.gpr[0] = context->readIntReg(INTREG_R0);
2537453Snate@binkert.org    r.gpr[1] = context->readIntReg(INTREG_R1);
2547453Snate@binkert.org    r.gpr[2] = context->readIntReg(INTREG_R2);
2557453Snate@binkert.org    r.gpr[3] = context->readIntReg(INTREG_R3);
2567453Snate@binkert.org    r.gpr[4] = context->readIntReg(INTREG_R4);
2577453Snate@binkert.org    r.gpr[5] = context->readIntReg(INTREG_R5);
2587453Snate@binkert.org    r.gpr[6] = context->readIntReg(INTREG_R6);
2597453Snate@binkert.org    r.gpr[7] = context->readIntReg(INTREG_R7);
2607453Snate@binkert.org    r.gpr[8] = context->readIntReg(INTREG_R8);
2617453Snate@binkert.org    r.gpr[9] = context->readIntReg(INTREG_R9);
2627453Snate@binkert.org    r.gpr[10] = context->readIntReg(INTREG_R10);
2637453Snate@binkert.org    r.gpr[11] = context->readIntReg(INTREG_R11);
2647453Snate@binkert.org    r.gpr[12] = context->readIntReg(INTREG_R12);
2657453Snate@binkert.org    r.gpr[13] = context->readIntReg(INTREG_SP);
2667453Snate@binkert.org    r.gpr[14] = context->readIntReg(INTREG_LR);
2676657Snate@binkert.org    r.gpr[15] = context->pcState().pc();
2686657Snate@binkert.org
2696657Snate@binkert.org    // One day somebody will implement transfer of FPRs correctly.
2706657Snate@binkert.org    for (int i=0; i<8*3; i++) r.fpr[i] = 0;
2716657Snate@binkert.org
2726657Snate@binkert.org    r.fpscr = context->readMiscRegNoEffect(MISCREG_FPSCR);
2736657Snate@binkert.org    r.cpsr = context->readMiscRegNoEffect(MISCREG_CPSR);
2746657Snate@binkert.org}
2756657Snate@binkert.org
2766657Snate@binkert.orgvoid
2776657Snate@binkert.orgRemoteGDB::AArch32GdbRegCache::setRegs(ThreadContext *context) const
2786657Snate@binkert.org{
2796657Snate@binkert.org    DPRINTF(GDBAcc, "setRegs in remotegdb \n");
2806657Snate@binkert.org
2816657Snate@binkert.org    context->setIntReg(INTREG_R0, r.gpr[0]);
2826657Snate@binkert.org    context->setIntReg(INTREG_R1, r.gpr[1]);
2836657Snate@binkert.org    context->setIntReg(INTREG_R2, r.gpr[2]);
2846657Snate@binkert.org    context->setIntReg(INTREG_R3, r.gpr[3]);
2856657Snate@binkert.org    context->setIntReg(INTREG_R4, r.gpr[4]);
2866657Snate@binkert.org    context->setIntReg(INTREG_R5, r.gpr[5]);
2876657Snate@binkert.org    context->setIntReg(INTREG_R6, r.gpr[6]);
2886657Snate@binkert.org    context->setIntReg(INTREG_R7, r.gpr[7]);
2897453Snate@binkert.org    context->setIntReg(INTREG_R8, r.gpr[8]);
2907453Snate@binkert.org    context->setIntReg(INTREG_R9, r.gpr[9]);
2917453Snate@binkert.org    context->setIntReg(INTREG_R10, r.gpr[10]);
2927007Snate@binkert.org    context->setIntReg(INTREG_R11, r.gpr[11]);
2937007Snate@binkert.org    context->setIntReg(INTREG_R12, r.gpr[12]);
2946657Snate@binkert.org    context->setIntReg(INTREG_SP, r.gpr[13]);
2956657Snate@binkert.org    context->setIntReg(INTREG_LR, r.gpr[14]);
2966657Snate@binkert.org    context->pcState(r.gpr[15]);
2977453Snate@binkert.org
2987007Snate@binkert.org    // One day somebody will implement transfer of FPRs correctly.
2997007Snate@binkert.org
3007453Snate@binkert.org    context->setMiscReg(MISCREG_FPSCR, r.fpscr);
3017007Snate@binkert.org    context->setMiscRegNoEffect(MISCREG_CPSR, r.cpsr);
3026657Snate@binkert.org}
3036657Snate@binkert.org
3046657Snate@binkert.orgRemoteGDB::BaseGdbRegCache*
3056657Snate@binkert.orgRemoteGDB::gdbRegs()
3066657Snate@binkert.org{
3076657Snate@binkert.org    if (inAArch64(context))
3086657Snate@binkert.org        return &regCache64;
3096657Snate@binkert.org    else
3106657Snate@binkert.org        return &regCache32;
3116657Snate@binkert.org}
3127007Snate@binkert.org