remote_gdb.cc revision 12031:46116545e745
12292SN/A/*
213590Srekai.gonzalezalberquilla@arm.com * Copyright 2015 LabWare
310239Sbinhpham@cs.rutgers.edu * Copyright 2014 Google Inc.
48707Sandreas.hansson@arm.com * Copyright (c) 2010, 2013 ARM Limited
58707Sandreas.hansson@arm.com * All rights reserved
68707Sandreas.hansson@arm.com *
78707Sandreas.hansson@arm.com * The license below extends only to copyright in the software and shall
88707Sandreas.hansson@arm.com * not be construed as granting a license to any other intellectual
98707Sandreas.hansson@arm.com * property including but not limited to intellectual property relating
108707Sandreas.hansson@arm.com * to a hardware implementation of the functionality of the software
118707Sandreas.hansson@arm.com * licensed hereunder.  You may use the software subject to the license
128707Sandreas.hansson@arm.com * terms below provided that you ensure that this notice is replicated
138707Sandreas.hansson@arm.com * unmodified and in its entirety in all distributions of the software,
148707Sandreas.hansson@arm.com * modified or unmodified, in source code or in binary form.
152727Sktlim@umich.edu *
162292SN/A * Copyright (c) 2002-2005 The Regents of The University of Michigan
172292SN/A * All rights reserved.
182292SN/A *
192292SN/A * Redistribution and use in source and binary forms, with or without
202292SN/A * modification, are permitted provided that the following conditions are
212292SN/A * met: redistributions of source code must retain the above copyright
222292SN/A * notice, this list of conditions and the following disclaimer;
232292SN/A * redistributions in binary form must reproduce the above copyright
242292SN/A * notice, this list of conditions and the following disclaimer in the
252292SN/A * documentation and/or other materials provided with the distribution;
262292SN/A * neither the name of the copyright holders nor the names of its
272292SN/A * contributors may be used to endorse or promote products derived from
282292SN/A * this software without specific prior written permission.
292292SN/A *
302292SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
312292SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
322292SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
332292SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
342292SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
352292SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
362292SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
372292SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
382292SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
392292SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
402689Sktlim@umich.edu * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
412689Sktlim@umich.edu *
422292SN/A * Authors: Nathan Binkert
432292SN/A *          William Wang
449944Smatt.horsnell@ARM.com *          Boris Shingarov
459944Smatt.horsnell@ARM.com */
469944Smatt.horsnell@ARM.com
472329SN/A/*
482980Sgblack@eecs.umich.edu * Copyright (c) 1990, 1993 The Regents of the University of California
492329SN/A * All rights reserved
502329SN/A *
5113449Sgabeblack@google.com * This software was developed by the Computer Systems Engineering group
5214194Sgabeblack@google.com * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
532292SN/A * contributed to Berkeley.
549444SAndreas.Sandberg@ARM.com *
558232Snate@binkert.org * All advertising materials mentioning features or use of this software
568232Snate@binkert.org * must display the following acknowledgement:
578232Snate@binkert.org *      This product includes software developed by the University of
586221Snate@binkert.org *      California, Lawrence Berkeley Laboratories.
592292SN/A *
606221Snate@binkert.org * Redistribution and use in source and binary forms, with or without
615529Snate@binkert.org * modification, are permitted provided that the following conditions
622292SN/A * are met:
635529Snate@binkert.org * 1. Redistributions of source code must retain the above copyright
648707Sandreas.hansson@arm.com *    notice, this list of conditions and the following disclaimer.
6513590Srekai.gonzalezalberquilla@arm.com * 2. Redistributions in binary form must reproduce the above copyright
6613590Srekai.gonzalezalberquilla@arm.com *    notice, this list of conditions and the following disclaimer in the
6713710Sgabor.dozsa@arm.com *    documentation and/or other materials provided with the distribution.
6813560Snikos.nikoleris@arm.com * 3. All advertising materials mentioning features or use of this software
694329Sktlim@umich.edu *    must display the following acknowledgement:
704329Sktlim@umich.edu *      This product includes software developed by the University of
7113472Srekai.gonzalezalberquilla@arm.com *      California, Berkeley and its contributors.
7213472Srekai.gonzalezalberquilla@arm.com * 4. Neither the name of the University nor the names of its contributors
7313472Srekai.gonzalezalberquilla@arm.com *    may be used to endorse or promote products derived from this software
7413472Srekai.gonzalezalberquilla@arm.com *    without specific prior written permission.
7514194Sgabeblack@google.com *
7610333Smitch.hayenga@arm.com * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
772292SN/A * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
789868Sjthestness@gmail.com * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
799868Sjthestness@gmail.com * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
802292SN/A * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
812292SN/A * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
822292SN/A * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
832292SN/A * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
8413590Srekai.gonzalezalberquilla@arm.com * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
8513590Srekai.gonzalezalberquilla@arm.com * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
862292SN/A * SUCH DAMAGE.
8713560Snikos.nikoleris@arm.com *
882292SN/A *      @(#)kgdb_stub.c 8.4 (Berkeley) 1/12/94
898346Sksewell@umich.edu */
902292SN/A
9113560Snikos.nikoleris@arm.com/*-
922292SN/A * Copyright (c) 2001 The NetBSD Foundation, Inc.
9313590Srekai.gonzalezalberquilla@arm.com * All rights reserved.
9413590Srekai.gonzalezalberquilla@arm.com *
952292SN/A * This code is derived from software contributed to The NetBSD Foundation
962292SN/A * by Jason R. Thorpe.
978346Sksewell@umich.edu *
982292SN/A * Redistribution and use in source and binary forms, with or without
992292SN/A * modification, are permitted provided that the following conditions
10013449Sgabeblack@google.com * are met:
10113449Sgabeblack@google.com * 1. Redistributions of source code must retain the above copyright
1022292SN/A *    notice, this list of conditions and the following disclaimer.
1032292SN/A * 2. Redistributions in binary form must reproduce the above copyright
10413472Srekai.gonzalezalberquilla@arm.com *    notice, this list of conditions and the following disclaimer in the
1056221Snate@binkert.org *    documentation and/or other materials provided with the distribution.
10613472Srekai.gonzalezalberquilla@arm.com * 3. All advertising materials mentioning features or use of this software
10713472Srekai.gonzalezalberquilla@arm.com *    must display the following acknowledgement:
10814194Sgabeblack@google.com *      This product includes software developed by the NetBSD
1092292SN/A *      Foundation, Inc. and its contributors.
1102292SN/A * 4. Neither the name of The NetBSD Foundation nor the names of its
1112292SN/A *    contributors may be used to endorse or promote products derived
1122292SN/A *    from this software without specific prior written permission.
1132292SN/A *
1142292SN/A * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
1152292SN/A * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
1162292SN/A * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
1172292SN/A * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
1182292SN/A * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
1192292SN/A * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
1202292SN/A * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
1212292SN/A * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
1222727Sktlim@umich.edu * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
1232727Sktlim@umich.edu * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
1242727Sktlim@umich.edu * POSSIBILITY OF SUCH DAMAGE.
1256221Snate@binkert.org */
1262727Sktlim@umich.edu
1272727Sktlim@umich.edu/*
1282727Sktlim@umich.edu * $NetBSD: kgdb_stub.c,v 1.8 2001/07/07 22:58:00 wdk Exp $
1292727Sktlim@umich.edu *
1302727Sktlim@umich.edu * Taken from NetBSD
1312727Sktlim@umich.edu *
1326221Snate@binkert.org * "Stub" to allow remote cpu to debug over a serial line using gdb.
1332292SN/A */
1342292SN/A
1352292SN/A#include "arch/arm/remote_gdb.hh"
1362292SN/A
1372292SN/A#include <sys/signal.h>
1382292SN/A#include <unistd.h>
1392307SN/A
1409444SAndreas.Sandberg@ARM.com#include <string>
1412307SN/A
1429444SAndreas.Sandberg@ARM.com#include "arch/arm/decoder.hh"
1439444SAndreas.Sandberg@ARM.com#include "arch/arm/pagetable.hh"
1449444SAndreas.Sandberg@ARM.com#include "arch/arm/registers.hh"
1459444SAndreas.Sandberg@ARM.com#include "arch/arm/system.hh"
1469444SAndreas.Sandberg@ARM.com#include "arch/arm/utility.hh"
1479444SAndreas.Sandberg@ARM.com#include "arch/arm/vtophys.hh"
1489444SAndreas.Sandberg@ARM.com#include "base/chunk_generator.hh"
1499444SAndreas.Sandberg@ARM.com#include "base/intmath.hh"
1509444SAndreas.Sandberg@ARM.com#include "base/remote_gdb.hh"
1519444SAndreas.Sandberg@ARM.com#include "base/socket.hh"
1529444SAndreas.Sandberg@ARM.com#include "base/trace.hh"
1539444SAndreas.Sandberg@ARM.com#include "cpu/static_inst.hh"
1549444SAndreas.Sandberg@ARM.com#include "cpu/thread_context.hh"
1559444SAndreas.Sandberg@ARM.com#include "cpu/thread_state.hh"
1569444SAndreas.Sandberg@ARM.com#include "debug/GDBAcc.hh"
1572307SN/A#include "debug/GDBMisc.hh"
1589444SAndreas.Sandberg@ARM.com#include "mem/page_table.hh"
1599444SAndreas.Sandberg@ARM.com#include "mem/physical.hh"
1609444SAndreas.Sandberg@ARM.com#include "mem/port.hh"
1619444SAndreas.Sandberg@ARM.com#include "sim/full_system.hh"
1629444SAndreas.Sandberg@ARM.com#include "sim/system.hh"
1639444SAndreas.Sandberg@ARM.com
1649444SAndreas.Sandberg@ARM.comusing namespace std;
1652307SN/Ausing namespace ArmISA;
1662307SN/A
1672307SN/ARemoteGDB::RemoteGDB(System *_system, ThreadContext *tc)
1682307SN/A    : BaseRemoteGDB(_system, tc), regCache32(this), regCache64(this)
1692307SN/A{
1702307SN/A}
17113590Srekai.gonzalezalberquilla@arm.com
17213590Srekai.gonzalezalberquilla@arm.com/*
17313590Srekai.gonzalezalberquilla@arm.com * Determine if the mapping at va..(va+len) is valid.
1746221Snate@binkert.org */
1752307SN/Abool
1762307SN/ARemoteGDB::acc(Addr va, size_t len)
1772307SN/A{
1782307SN/A    if (FullSystem) {
17913710Sgabor.dozsa@arm.com        for (ChunkGenerator gen(va, len, PageBytes); !gen.done(); gen.next()) {
18013710Sgabor.dozsa@arm.com            if (!virtvalid(context, gen.addr())) {
18113710Sgabor.dozsa@arm.com                DPRINTF(GDBAcc, "acc:   %#x mapping is invalid\n", va);
18213710Sgabor.dozsa@arm.com                return false;
18313710Sgabor.dozsa@arm.com            }
18413710Sgabor.dozsa@arm.com        }
18513710Sgabor.dozsa@arm.com
18613710Sgabor.dozsa@arm.com        DPRINTF(GDBAcc, "acc:   %#x mapping is valid\n", va);
18713710Sgabor.dozsa@arm.com        return true;
18813710Sgabor.dozsa@arm.com    } else {
18913710Sgabor.dozsa@arm.com        TlbEntry entry;
19013710Sgabor.dozsa@arm.com        //Check to make sure the first byte is mapped into the processes address
19113590Srekai.gonzalezalberquilla@arm.com        //space.
19213590Srekai.gonzalezalberquilla@arm.com        if (context->getProcessPtr()->pTable->lookup(va, entry))
19313590Srekai.gonzalezalberquilla@arm.com            return true;
1942292SN/A        return false;
19513590Srekai.gonzalezalberquilla@arm.com    }
1962292SN/A}
1972292SN/A
1982292SN/Avoid
1992292SN/ARemoteGDB::AArch64GdbRegCache::getRegs(ThreadContext *context)
20013590Srekai.gonzalezalberquilla@arm.com{
2012292SN/A    DPRINTF(GDBAcc, "getRegs in remotegdb \n");
20213590Srekai.gonzalezalberquilla@arm.com
20313590Srekai.gonzalezalberquilla@arm.com    for (int i = 0; i < 31; ++i)
20413590Srekai.gonzalezalberquilla@arm.com        r.x[i] = context->readIntReg(INTREG_X0 + i);
20513590Srekai.gonzalezalberquilla@arm.com    r.spx = context->readIntReg(INTREG_SPX);
20613590Srekai.gonzalezalberquilla@arm.com    r.pc = context->pcState().pc();
20713710Sgabor.dozsa@arm.com    r.cpsr = context->readMiscRegNoEffect(MISCREG_CPSR);
20813590Srekai.gonzalezalberquilla@arm.com
20913710Sgabor.dozsa@arm.com    for (int i = 0; i < 32*4; i += 4) {
21013710Sgabor.dozsa@arm.com        r.v[i + 0] = context->readFloatRegBits(i + 2);
21113710Sgabor.dozsa@arm.com        r.v[i + 1] = context->readFloatRegBits(i + 3);
21213710Sgabor.dozsa@arm.com        r.v[i + 2] = context->readFloatRegBits(i + 0);
21313710Sgabor.dozsa@arm.com        r.v[i + 3] = context->readFloatRegBits(i + 1);
21413710Sgabor.dozsa@arm.com    }
21513710Sgabor.dozsa@arm.com}
2162292SN/A
2172292SN/Avoid
2182292SN/ARemoteGDB::AArch64GdbRegCache::setRegs(ThreadContext *context) const
2192292SN/A{
22013710Sgabor.dozsa@arm.com    DPRINTF(GDBAcc, "setRegs in remotegdb \n");
2212292SN/A
22213710Sgabor.dozsa@arm.com    for (int i = 0; i < 31; ++i)
22313710Sgabor.dozsa@arm.com        context->setIntReg(INTREG_X0 + i, r.x[i]);
22413710Sgabor.dozsa@arm.com    context->pcState(r.pc);
22513710Sgabor.dozsa@arm.com    context->setMiscRegNoEffect(MISCREG_CPSR, r.cpsr);
22613710Sgabor.dozsa@arm.com    // Update the stack pointer. This should be done after
22713710Sgabor.dozsa@arm.com    // updating CPSR/PSTATE since that might affect how SPX gets
2282292SN/A    // mapped.
2292292SN/A    context->setIntReg(INTREG_SPX, r.spx);
2302292SN/A
2312292SN/A    for (int i = 0; i < 32*4; i += 4) {
23213429Srekai.gonzalezalberquilla@arm.com        context->setFloatRegBits(i + 2, r.v[i + 0]);
2332292SN/A        context->setFloatRegBits(i + 3, r.v[i + 1]);
2346221Snate@binkert.org        context->setFloatRegBits(i + 0, r.v[i + 2]);
2352292SN/A        context->setFloatRegBits(i + 1, r.v[i + 3]);
2362292SN/A    }
2372292SN/A}
2382292SN/A
2392292SN/Avoid
2402292SN/ARemoteGDB::AArch32GdbRegCache::getRegs(ThreadContext *context)
24113429Srekai.gonzalezalberquilla@arm.com{
2422292SN/A    DPRINTF(GDBAcc, "getRegs in remotegdb \n");
2436221Snate@binkert.org
2442292SN/A    r.gpr[0] = context->readIntReg(INTREG_R0);
2452292SN/A    r.gpr[1] = context->readIntReg(INTREG_R1);
2462292SN/A    r.gpr[2] = context->readIntReg(INTREG_R2);
2472292SN/A    r.gpr[3] = context->readIntReg(INTREG_R3);
2482292SN/A    r.gpr[4] = context->readIntReg(INTREG_R4);
2492292SN/A    r.gpr[5] = context->readIntReg(INTREG_R5);
25013429Srekai.gonzalezalberquilla@arm.com    r.gpr[6] = context->readIntReg(INTREG_R6);
2512292SN/A    r.gpr[7] = context->readIntReg(INTREG_R7);
2526221Snate@binkert.org    r.gpr[8] = context->readIntReg(INTREG_R8);
2532292SN/A    r.gpr[9] = context->readIntReg(INTREG_R9);
2542292SN/A    r.gpr[10] = context->readIntReg(INTREG_R10);
2552292SN/A    r.gpr[11] = context->readIntReg(INTREG_R11);
2562292SN/A    r.gpr[12] = context->readIntReg(INTREG_R12);
2572292SN/A    r.gpr[13] = context->readIntReg(INTREG_SP);
2582292SN/A    r.gpr[14] = context->readIntReg(INTREG_LR);
25913429Srekai.gonzalezalberquilla@arm.com    r.gpr[15] = context->pcState().pc();
2602292SN/A
2616221Snate@binkert.org    // One day somebody will implement transfer of FPRs correctly.
2622292SN/A    for (int i=0; i<8*3; i++) r.fpr[i] = 0;
2632292SN/A
2642292SN/A    r.fpscr = context->readMiscRegNoEffect(MISCREG_FPSCR);
2652292SN/A    r.cpsr = context->readMiscRegNoEffect(MISCREG_CPSR);
2662292SN/A}
2672292SN/A
2682292SN/Avoid
2692292SN/ARemoteGDB::AArch32GdbRegCache::setRegs(ThreadContext *context) const
2706221Snate@binkert.org{
2716221Snate@binkert.org    DPRINTF(GDBAcc, "setRegs in remotegdb \n");
2722292SN/A
2733867Sbinkertn@umich.edu    context->setIntReg(INTREG_R0, r.gpr[0]);
2746221Snate@binkert.org    context->setIntReg(INTREG_R1, r.gpr[1]);
2752292SN/A    context->setIntReg(INTREG_R2, r.gpr[2]);
2762292SN/A    context->setIntReg(INTREG_R3, r.gpr[3]);
2772329SN/A    context->setIntReg(INTREG_R4, r.gpr[4]);
2782329SN/A    context->setIntReg(INTREG_R5, r.gpr[5]);
2792292SN/A    context->setIntReg(INTREG_R6, r.gpr[6]);
2802292SN/A    context->setIntReg(INTREG_R7, r.gpr[7]);
2812292SN/A    context->setIntReg(INTREG_R8, r.gpr[8]);
2822292SN/A    context->setIntReg(INTREG_R9, r.gpr[9]);
2832292SN/A    context->setIntReg(INTREG_R10, r.gpr[10]);
2842292SN/A    context->setIntReg(INTREG_R11, r.gpr[11]);
2852292SN/A    context->setIntReg(INTREG_R12, r.gpr[12]);
2862292SN/A    context->setIntReg(INTREG_SP, r.gpr[13]);
2872292SN/A    context->setIntReg(INTREG_LR, r.gpr[14]);
2882292SN/A    context->pcState(r.gpr[15]);
2892292SN/A
2906221Snate@binkert.org    // One day somebody will implement transfer of FPRs correctly.
2916221Snate@binkert.org
2922292SN/A    context->setMiscReg(MISCREG_FPSCR, r.fpscr);
2933867Sbinkertn@umich.edu    context->setMiscRegNoEffect(MISCREG_CPSR, r.cpsr);
2946221Snate@binkert.org}
2953867Sbinkertn@umich.edu
2962292SN/ARemoteGDB::BaseGdbRegCache*
2972292SN/ARemoteGDB::gdbRegs()
2982292SN/A{
2992292SN/A    if (inAArch64(context))
3002292SN/A        return &regCache32;
3012292SN/A    else
3022292SN/A        return &regCache64;
3038707Sandreas.hansson@arm.com}
3048707Sandreas.hansson@arm.com