remote_gdb.cc revision 5543
19793Sakash.bagdia@arm.com/*
28706Sandreas.hansson@arm.com * Copyright (c) 2002-2005 The Regents of The University of Michigan
38706Sandreas.hansson@arm.com * All rights reserved.
48706Sandreas.hansson@arm.com *
58706Sandreas.hansson@arm.com * Redistribution and use in source and binary forms, with or without
68706Sandreas.hansson@arm.com * modification, are permitted provided that the following conditions are
78706Sandreas.hansson@arm.com * met: redistributions of source code must retain the above copyright
88706Sandreas.hansson@arm.com * notice, this list of conditions and the following disclaimer;
98706Sandreas.hansson@arm.com * redistributions in binary form must reproduce the above copyright
108706Sandreas.hansson@arm.com * notice, this list of conditions and the following disclaimer in the
118706Sandreas.hansson@arm.com * documentation and/or other materials provided with the distribution;
128706Sandreas.hansson@arm.com * neither the name of the copyright holders nor the names of its
135369Ssaidi@eecs.umich.edu * contributors may be used to endorse or promote products derived from
143005Sstever@eecs.umich.edu * this software without specific prior written permission.
153005Sstever@eecs.umich.edu *
163005Sstever@eecs.umich.edu * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
173005Sstever@eecs.umich.edu * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
183005Sstever@eecs.umich.edu * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
193005Sstever@eecs.umich.edu * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
203005Sstever@eecs.umich.edu * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
213005Sstever@eecs.umich.edu * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
223005Sstever@eecs.umich.edu * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
233005Sstever@eecs.umich.edu * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
243005Sstever@eecs.umich.edu * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
253005Sstever@eecs.umich.edu * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
263005Sstever@eecs.umich.edu * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
273005Sstever@eecs.umich.edu *
283005Sstever@eecs.umich.edu * Authors: Nathan Binkert
293005Sstever@eecs.umich.edu */
303005Sstever@eecs.umich.edu
313005Sstever@eecs.umich.edu/*
323005Sstever@eecs.umich.edu * Copyright (c) 1990, 1993
333005Sstever@eecs.umich.edu *      The Regents of the University of California.  All rights reserved.
343005Sstever@eecs.umich.edu *
353005Sstever@eecs.umich.edu * This software was developed by the Computer Systems Engineering group
363005Sstever@eecs.umich.edu * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
373005Sstever@eecs.umich.edu * contributed to Berkeley.
383005Sstever@eecs.umich.edu *
393005Sstever@eecs.umich.edu * All advertising materials mentioning features or use of this software
403005Sstever@eecs.umich.edu * must display the following acknowledgement:
412710SN/A *      This product includes software developed by the University of
422710SN/A *      California, Lawrence Berkeley Laboratories.
433005Sstever@eecs.umich.edu *
442889SN/A * Redistribution and use in source and binary forms, with or without
4512564Sgabeblack@google.com * modification, are permitted provided that the following conditions
4613774Sandreas.sandberg@arm.com * are met:
4712564Sgabeblack@google.com * 1. Redistributions of source code must retain the above copyright
486654Snate@binkert.org *    notice, this list of conditions and the following disclaimer.
496654Snate@binkert.org * 2. Redistributions in binary form must reproduce the above copyright
509907Snilay@cs.wisc.edu *    notice, this list of conditions and the following disclaimer in the
516654Snate@binkert.org *    documentation and/or other materials provided with the distribution.
522667SN/A * 3. All advertising materials mentioning features or use of this software
536654Snate@binkert.org *    must display the following acknowledgement:
546654Snate@binkert.org *      This product includes software developed by the University of
5512395Sswapnilster@gmail.com *      California, Berkeley and its contributors.
565457Ssaidi@eecs.umich.edu * 4. Neither the name of the University nor the names of its contributors
5711670Sandreas.hansson@arm.com *    may be used to endorse or promote products derived from this software
5811670Sandreas.hansson@arm.com *    without specific prior written permission.
5911670Sandreas.hansson@arm.com *
608169SLisa.Hsu@amd.com * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
6111682Sandreas.hansson@arm.com * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
6211682Sandreas.hansson@arm.com * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
6311682Sandreas.hansson@arm.com * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
6411682Sandreas.hansson@arm.com * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
6513432Spau.cabre@metempsy.com * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
6611682Sandreas.hansson@arm.com * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
6711682Sandreas.hansson@arm.com * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
6811682Sandreas.hansson@arm.com * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
693394Shsul@eecs.umich.edu * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
709197Snilay@cs.wisc.edu * SUCH DAMAGE.
719197Snilay@cs.wisc.edu *
729197Snilay@cs.wisc.edu *      @(#)kgdb_stub.c 8.4 (Berkeley) 1/12/94
739197Snilay@cs.wisc.edu */
749197Snilay@cs.wisc.edu
759197Snilay@cs.wisc.edu/*-
769197Snilay@cs.wisc.edu * Copyright (c) 2001 The NetBSD Foundation, Inc.
779197Snilay@cs.wisc.edu * All rights reserved.
789197Snilay@cs.wisc.edu *
799197Snilay@cs.wisc.edu * This code is derived from software contributed to The NetBSD Foundation
809197Snilay@cs.wisc.edu * by Jason R. Thorpe.
819197Snilay@cs.wisc.edu *
829197Snilay@cs.wisc.edu * Redistribution and use in source and binary forms, with or without
839197Snilay@cs.wisc.edu * modification, are permitted provided that the following conditions
849197Snilay@cs.wisc.edu * are met:
859197Snilay@cs.wisc.edu * 1. Redistributions of source code must retain the above copyright
869197Snilay@cs.wisc.edu *    notice, this list of conditions and the following disclaimer.
879197Snilay@cs.wisc.edu * 2. Redistributions in binary form must reproduce the above copyright
889197Snilay@cs.wisc.edu *    notice, this list of conditions and the following disclaimer in the
899197Snilay@cs.wisc.edu *    documentation and/or other materials provided with the distribution.
909197Snilay@cs.wisc.edu * 3. All advertising materials mentioning features or use of this software
9112146Spau.cabre@metempsy.com *    must display the following acknowledgement:
929197Snilay@cs.wisc.edu *      This product includes software developed by the NetBSD
939907Snilay@cs.wisc.edu *      Foundation, Inc. and its contributors.
949197Snilay@cs.wisc.edu * 4. Neither the name of The NetBSD Foundation nor the names of its
9510803Sbrandon.potter@amd.com *    contributors may be used to endorse or promote products derived
9610803Sbrandon.potter@amd.com *    from this software without specific prior written permission.
9710803Sbrandon.potter@amd.com *
9810803Sbrandon.potter@amd.com * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
999197Snilay@cs.wisc.edu * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
1009217Snilay@cs.wisc.edu * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
1019197Snilay@cs.wisc.edu * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
1029197Snilay@cs.wisc.edu * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
1039197Snilay@cs.wisc.edu * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
1049197Snilay@cs.wisc.edu * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
1059197Snilay@cs.wisc.edu * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
1069197Snilay@cs.wisc.edu * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
1079197Snilay@cs.wisc.edu * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
1089197Snilay@cs.wisc.edu * POSSIBILITY OF SUCH DAMAGE.
1099197Snilay@cs.wisc.edu */
1109197Snilay@cs.wisc.edu
1119197Snilay@cs.wisc.edu/*
1129197Snilay@cs.wisc.edu * $NetBSD: kgdb_stub.c,v 1.8 2001/07/07 22:58:00 wdk Exp $
1139197Snilay@cs.wisc.edu *
1149197Snilay@cs.wisc.edu * Taken from NetBSD
11512014Sgabeblack@google.com *
1169197Snilay@cs.wisc.edu * "Stub" to allow remote cpu to debug over a serial line using gdb.
1179197Snilay@cs.wisc.edu */
1189197Snilay@cs.wisc.edu
1199197Snilay@cs.wisc.edu#include <sys/signal.h>
1209197Snilay@cs.wisc.edu
1212957SN/A#include <string>
1228920Snilay@cs.wisc.edu#include <unistd.h>
1238920Snilay@cs.wisc.edu
1242957SN/A#include "config/full_system.hh"
1258862Snilay@cs.wisc.edu#if FULL_SYSTEM
1268862Snilay@cs.wisc.edu#include "arch/alpha/vtophys.hh"
1278467Snilay@cs.wisc.edu#endif
1282957SN/A
1292957SN/A#include "arch/alpha/kgdb.h"
1302957SN/A#include "arch/alpha/utility.hh"
13112564Sgabeblack@google.com#include "arch/alpha/remote_gdb.hh"
1322957SN/A#include "base/intmath.hh"
1332957SN/A#include "base/remote_gdb.hh"
1348167SLisa.Hsu@amd.com#include "base/socket.hh"
1359197Snilay@cs.wisc.edu#include "base/trace.hh"
1368167SLisa.Hsu@amd.com#include "cpu/thread_context.hh"
1375369Ssaidi@eecs.umich.edu#include "cpu/static_inst.hh"
1388167SLisa.Hsu@amd.com#include "mem/physical.hh"
1398167SLisa.Hsu@amd.com#include "mem/port.hh"
14012564Sgabeblack@google.com#include "sim/system.hh"
1418167SLisa.Hsu@amd.com
1428167SLisa.Hsu@amd.comusing namespace std;
1438167SLisa.Hsu@amd.comusing namespace TheISA;
1448167SLisa.Hsu@amd.com
1458168SLisa.Hsu@amd.comRemoteGDB::RemoteGDB(System *_system, ThreadContext *c)
14610037SARM gem5 Developers    : BaseRemoteGDB(_system, c, KGDB_NUMREGS)
14710037SARM gem5 Developers{
14810037SARM gem5 Developers    memset(gdbregs.regs, 0, gdbregs.bytes());
14910037SARM gem5 Developers}
15010037SARM gem5 Developers
1518168SLisa.Hsu@amd.com///////////////////////////////////////////////////////////
15210037SARM gem5 Developers// RemoteGDB::acc
15310037SARM gem5 Developers//
15411851Sbrandon.potter@amd.com//      Determine if the mapping at va..(va+len) is valid.
1558167SLisa.Hsu@amd.com//
15612564Sgabeblack@google.combool
15712564Sgabeblack@google.comRemoteGDB::acc(Addr va, size_t len)
15812564Sgabeblack@google.com{
1595369Ssaidi@eecs.umich.edu#if !FULL_SYSTEM
1608920Snilay@cs.wisc.edu    panic("acc function needs to be rewritten for SE mode\n");
1619197Snilay@cs.wisc.edu#else
1628920Snilay@cs.wisc.edu    Addr last_va;
16312564Sgabeblack@google.com
1648920Snilay@cs.wisc.edu    va = TheISA::TruncPage(va);
1655369Ssaidi@eecs.umich.edu    last_va = TheISA::RoundPage(va + len);
1665369Ssaidi@eecs.umich.edu
1678718Snilay@cs.wisc.edu    do  {
1689197Snilay@cs.wisc.edu        if (TheISA::IsK0Seg(va)) {
1699197Snilay@cs.wisc.edu            if (va < (TheISA::K0SegBase + pmem->size())) {
1709197Snilay@cs.wisc.edu                DPRINTF(GDBAcc, "acc:   Mapping is valid  K0SEG <= "
1719197Snilay@cs.wisc.edu                        "%#x < K0SEG + size\n", va);
1729197Snilay@cs.wisc.edu                return true;
1733005Sstever@eecs.umich.edu            } else {
1743395Shsul@eecs.umich.edu                DPRINTF(GDBAcc, "acc:   Mapping invalid %#x > K0SEG + size\n",
17513731Sandreas.sandberg@arm.com                        va);
1769793Sakash.bagdia@arm.com                return false;
1779836Sandreas.hansson@arm.com            }
1789815SAndreas Hansson <andreas.hansson>        }
1799793Sakash.bagdia@arm.com
18011147Smitch.hayenga@arm.com    /**
18111147Smitch.hayenga@arm.com     * This code says that all accesses to palcode (instruction and data)
18211147Smitch.hayenga@arm.com     * are valid since there isn't a va->pa mapping because palcode is
1839827Sakash.bagdia@arm.com     * accessed physically. At some point this should probably be cleaned up
1849827Sakash.bagdia@arm.com     * but there is no easy way to do it.
1859827Sakash.bagdia@arm.com     */
1869827Sakash.bagdia@arm.com
1879827Sakash.bagdia@arm.com        if (AlphaISA::PcPAL(va) || va < 0x10000)
1889827Sakash.bagdia@arm.com            return true;
1899827Sakash.bagdia@arm.com
1909827Sakash.bagdia@arm.com        Addr ptbr = context->readMiscRegNoEffect(AlphaISA::IPR_PALtemp20);
1919827Sakash.bagdia@arm.com        TheISA::PageTableEntry pte = TheISA::kernel_pte_lookup(context->getPhysPort(), ptbr, va);
1929827Sakash.bagdia@arm.com        if (!pte.valid()) {
1939793Sakash.bagdia@arm.com            DPRINTF(GDBAcc, "acc:   %#x pte is invalid\n", va);
1949827Sakash.bagdia@arm.com            return false;
1959827Sakash.bagdia@arm.com        }
1969827Sakash.bagdia@arm.com        va += TheISA::PageBytes;
1979793Sakash.bagdia@arm.com    } while (va < last_va);
19811251Sradhika.jagtap@ARM.com
19911251Sradhika.jagtap@ARM.com    DPRINTF(GDBAcc, "acc:   %#x mapping is valid\n", va);
20011251Sradhika.jagtap@ARM.com    return true;
20111251Sradhika.jagtap@ARM.com#endif
20211251Sradhika.jagtap@ARM.com}
2039793Sakash.bagdia@arm.com
2049793Sakash.bagdia@arm.com///////////////////////////////////////////////////////////
2059793Sakash.bagdia@arm.com// RemoteGDB::getregs
2069793Sakash.bagdia@arm.com//
2073395Shsul@eecs.umich.edu//      Translate the kernel debugger register format into
20812941Sandreas.sandberg@arm.com//      the GDB register format.
20910555Salexandru.dutu@amd.comvoid
21011839SCurtis.Dunham@arm.comRemoteGDB::getregs()
21110555Salexandru.dutu@amd.com{
21210555Salexandru.dutu@amd.com    memset(gdbregs.regs, 0, gdbregs.bytes());
21310555Salexandru.dutu@amd.com
21410555Salexandru.dutu@amd.com    gdbregs.regs[KGDB_REG_PC] = context->readPC();
21510555Salexandru.dutu@amd.com
21610555Salexandru.dutu@amd.com    // @todo: Currently this is very Alpha specific.
2178926Sandreas.hansson@arm.com    if (AlphaISA::PcPAL(gdbregs.regs[KGDB_REG_PC])) {
2189647Sdam.sunwoo@arm.com        for (int i = 0; i < TheISA::NumIntArchRegs; ++i) {
21913684Sgiacomo.travaglini@arm.com            gdbregs.regs[i] = context->readIntReg(AlphaISA::reg_redir[i]);
22013012Sandreas.sandberg@arm.com        }
2219647Sdam.sunwoo@arm.com    } else {
2229647Sdam.sunwoo@arm.com        for (int i = 0; i < TheISA::NumIntArchRegs; ++i) {
2239647Sdam.sunwoo@arm.com            gdbregs.regs[i] = context->readIntReg(i);
22413731Sandreas.sandberg@arm.com        }
2259197Snilay@cs.wisc.edu    }
2269197Snilay@cs.wisc.edu
2279197Snilay@cs.wisc.edu#ifdef KGDB_FP_REGS
2288957Sjayneel@cs.wisc.edu    for (int i = 0; i < TheISA::NumFloatArchRegs; ++i) {
2298957Sjayneel@cs.wisc.edu        gdbregs.regs[i + KGDB_REG_F0] = context->readFloatRegBits(i);
2308957Sjayneel@cs.wisc.edu    }
2313005Sstever@eecs.umich.edu#endif
2329647Sdam.sunwoo@arm.com}
23310381Sdam.sunwoo@arm.com
2349647Sdam.sunwoo@arm.com///////////////////////////////////////////////////////////
2358887Sgeoffrey.blake@arm.com// RemoteGDB::setregs
2368887Sgeoffrey.blake@arm.com//
2378887Sgeoffrey.blake@arm.com//      Translate the GDB register format into the kernel
23813432Spau.cabre@metempsy.com//      debugger register format.
23913432Spau.cabre@metempsy.com//
24013432Spau.cabre@metempsy.comvoid
24113432Spau.cabre@metempsy.comRemoteGDB::setregs()
2429384SAndreas.Sandberg@arm.com{
2439384SAndreas.Sandberg@arm.com    // @todo: Currently this is very Alpha specific.
2448887Sgeoffrey.blake@arm.com    if (AlphaISA::PcPAL(gdbregs.regs[KGDB_REG_PC])) {
24510519Snilay@cs.wisc.edu        for (int i = 0; i < TheISA::NumIntArchRegs; ++i) {
24610120Snilay@cs.wisc.edu            context->setIntReg(AlphaISA::reg_redir[i], gdbregs.regs[i]);
2478896Snilay@cs.wisc.edu        }
24810300Scastilloe@unican.es    } else {
24910300Scastilloe@unican.es        for (int i = 0; i < TheISA::NumIntArchRegs; ++i) {
25013731Sandreas.sandberg@arm.com            context->setIntReg(i, gdbregs.regs[i]);
25110120Snilay@cs.wisc.edu        }
2528896Snilay@cs.wisc.edu    }
2538896Snilay@cs.wisc.edu
2549268Smalek.musleh@gmail.com#ifdef KGDB_FP_REGS
2559268Smalek.musleh@gmail.com    for (int i = 0; i < TheISA::NumFloatArchRegs; ++i) {
2568896Snilay@cs.wisc.edu        context->setFloatRegBits(i, gdbregs.regs[i + KGDB_REG_F0]);
2578896Snilay@cs.wisc.edu    }
2588896Snilay@cs.wisc.edu#endif
2598896Snilay@cs.wisc.edu    context->setPC(gdbregs.regs[KGDB_REG_PC]);
2608896Snilay@cs.wisc.edu}
2619222Shestness@cs.wisc.edu
26211150Smitch.hayenga@arm.comvoid
26311150Smitch.hayenga@arm.comRemoteGDB::clearSingleStep()
26411150Smitch.hayenga@arm.com{
2659222Shestness@cs.wisc.edu    DPRINTF(GDBMisc, "clearSingleStep bt_addr=%#x nt_addr=%#x\n",
2669222Shestness@cs.wisc.edu            takenBkpt, notTakenBkpt);
2678887Sgeoffrey.blake@arm.com
26810150Snilay@cs.wisc.edu    if (takenBkpt != 0)
26910720Sandreas.hansson@arm.com        clearTempBreakpoint(takenBkpt);
2708887Sgeoffrey.blake@arm.com
2718887Sgeoffrey.blake@arm.com    if (notTakenBkpt != 0)
2729836Sandreas.hansson@arm.com        clearTempBreakpoint(notTakenBkpt);
2738887Sgeoffrey.blake@arm.com}
2748801Sgblack@eecs.umich.edu
2753481Shsul@eecs.umich.eduvoid
276RemoteGDB::setSingleStep()
277{
278    Addr pc = context->readPC();
279    Addr npc, bpc;
280    bool set_bt = false;
281
282    npc = pc + sizeof(MachInst);
283
284    // User was stopped at pc, e.g. the instruction at pc was not
285    // executed.
286    MachInst inst = read<MachInst>(pc);
287    StaticInstPtr si(inst, pc);
288    if (si->hasBranchTarget(pc, context, bpc)) {
289        // Don't bother setting a breakpoint on the taken branch if it
290        // is the same as the next pc
291        if (bpc != npc)
292            set_bt = true;
293    }
294
295    DPRINTF(GDBMisc, "setSingleStep bt_addr=%#x nt_addr=%#x\n",
296            takenBkpt, notTakenBkpt);
297
298    setTempBreakpoint(notTakenBkpt = npc);
299
300    if (set_bt)
301        setTempBreakpoint(takenBkpt = bpc);
302}
303
304// Write bytes to kernel address space for debugger.
305bool
306RemoteGDB::write(Addr vaddr, size_t size, const char *data)
307{
308    if (BaseRemoteGDB::write(vaddr, size, data)) {
309#ifdef IMB
310        alpha_pal_imb();
311#endif
312        return true;
313    } else {
314        return false;
315    }
316}
317
318