remote_gdb.cc revision 5567
18512Sgeoffrey.blake@arm.com/*
28512Sgeoffrey.blake@arm.com * Copyright (c) 2002-2005 The Regents of The University of Michigan
38512Sgeoffrey.blake@arm.com * All rights reserved.
48512Sgeoffrey.blake@arm.com *
58512Sgeoffrey.blake@arm.com * Redistribution and use in source and binary forms, with or without
68512Sgeoffrey.blake@arm.com * modification, are permitted provided that the following conditions are
78512Sgeoffrey.blake@arm.com * met: redistributions of source code must retain the above copyright
88512Sgeoffrey.blake@arm.com * notice, this list of conditions and the following disclaimer;
98512Sgeoffrey.blake@arm.com * redistributions in binary form must reproduce the above copyright
108512Sgeoffrey.blake@arm.com * notice, this list of conditions and the following disclaimer in the
118512Sgeoffrey.blake@arm.com * documentation and/or other materials provided with the distribution;
128512Sgeoffrey.blake@arm.com * neither the name of the copyright holders nor the names of its
138512Sgeoffrey.blake@arm.com * contributors may be used to endorse or promote products derived from
148512Sgeoffrey.blake@arm.com * this software without specific prior written permission.
158512Sgeoffrey.blake@arm.com *
168512Sgeoffrey.blake@arm.com * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
178512Sgeoffrey.blake@arm.com * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
188512Sgeoffrey.blake@arm.com * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
198512Sgeoffrey.blake@arm.com * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
208512Sgeoffrey.blake@arm.com * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
218512Sgeoffrey.blake@arm.com * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
228512Sgeoffrey.blake@arm.com * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
238512Sgeoffrey.blake@arm.com * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
248512Sgeoffrey.blake@arm.com * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
258512Sgeoffrey.blake@arm.com * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
268512Sgeoffrey.blake@arm.com * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
278512Sgeoffrey.blake@arm.com *
288512Sgeoffrey.blake@arm.com * Authors: Nathan Binkert
298512Sgeoffrey.blake@arm.com */
308512Sgeoffrey.blake@arm.com
318512Sgeoffrey.blake@arm.com/*
328512Sgeoffrey.blake@arm.com * Copyright (c) 1990, 1993
338512Sgeoffrey.blake@arm.com *      The Regents of the University of California.  All rights reserved.
348512Sgeoffrey.blake@arm.com *
358512Sgeoffrey.blake@arm.com * This software was developed by the Computer Systems Engineering group
368512Sgeoffrey.blake@arm.com * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
378512Sgeoffrey.blake@arm.com * contributed to Berkeley.
388512Sgeoffrey.blake@arm.com *
398512Sgeoffrey.blake@arm.com * All advertising materials mentioning features or use of this software
408512Sgeoffrey.blake@arm.com * must display the following acknowledgement:
418512Sgeoffrey.blake@arm.com *      This product includes software developed by the University of
428512Sgeoffrey.blake@arm.com *      California, Lawrence Berkeley Laboratories.
438512Sgeoffrey.blake@arm.com *
448512Sgeoffrey.blake@arm.com * Redistribution and use in source and binary forms, with or without
458512Sgeoffrey.blake@arm.com * modification, are permitted provided that the following conditions
468512Sgeoffrey.blake@arm.com * are met:
478512Sgeoffrey.blake@arm.com * 1. Redistributions of source code must retain the above copyright
488512Sgeoffrey.blake@arm.com *    notice, this list of conditions and the following disclaimer.
498512Sgeoffrey.blake@arm.com * 2. Redistributions in binary form must reproduce the above copyright
508512Sgeoffrey.blake@arm.com *    notice, this list of conditions and the following disclaimer in the
518512Sgeoffrey.blake@arm.com *    documentation and/or other materials provided with the distribution.
528512Sgeoffrey.blake@arm.com * 3. All advertising materials mentioning features or use of this software
538512Sgeoffrey.blake@arm.com *    must display the following acknowledgement:
548512Sgeoffrey.blake@arm.com *      This product includes software developed by the University of
558512Sgeoffrey.blake@arm.com *      California, Berkeley and its contributors.
568512Sgeoffrey.blake@arm.com * 4. Neither the name of the University nor the names of its contributors
578512Sgeoffrey.blake@arm.com *    may be used to endorse or promote products derived from this software
588512Sgeoffrey.blake@arm.com *    without specific prior written permission.
598512Sgeoffrey.blake@arm.com *
608512Sgeoffrey.blake@arm.com * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
618512Sgeoffrey.blake@arm.com * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
628512Sgeoffrey.blake@arm.com * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
638512Sgeoffrey.blake@arm.com * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
648512Sgeoffrey.blake@arm.com * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
658512Sgeoffrey.blake@arm.com * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
668512Sgeoffrey.blake@arm.com * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
678512Sgeoffrey.blake@arm.com * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
688512Sgeoffrey.blake@arm.com * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
698512Sgeoffrey.blake@arm.com * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
708512Sgeoffrey.blake@arm.com * SUCH DAMAGE.
718512Sgeoffrey.blake@arm.com *
728512Sgeoffrey.blake@arm.com *      @(#)kgdb_stub.c 8.4 (Berkeley) 1/12/94
738512Sgeoffrey.blake@arm.com */
748512Sgeoffrey.blake@arm.com
758512Sgeoffrey.blake@arm.com/*-
768512Sgeoffrey.blake@arm.com * Copyright (c) 2001 The NetBSD Foundation, Inc.
778512Sgeoffrey.blake@arm.com * All rights reserved.
788512Sgeoffrey.blake@arm.com *
798512Sgeoffrey.blake@arm.com * This code is derived from software contributed to The NetBSD Foundation
808512Sgeoffrey.blake@arm.com * by Jason R. Thorpe.
818512Sgeoffrey.blake@arm.com *
828512Sgeoffrey.blake@arm.com * Redistribution and use in source and binary forms, with or without
838512Sgeoffrey.blake@arm.com * modification, are permitted provided that the following conditions
848512Sgeoffrey.blake@arm.com * are met:
858512Sgeoffrey.blake@arm.com * 1. Redistributions of source code must retain the above copyright
868512Sgeoffrey.blake@arm.com *    notice, this list of conditions and the following disclaimer.
878512Sgeoffrey.blake@arm.com * 2. Redistributions in binary form must reproduce the above copyright
888512Sgeoffrey.blake@arm.com *    notice, this list of conditions and the following disclaimer in the
898512Sgeoffrey.blake@arm.com *    documentation and/or other materials provided with the distribution.
908512Sgeoffrey.blake@arm.com * 3. All advertising materials mentioning features or use of this software
918512Sgeoffrey.blake@arm.com *    must display the following acknowledgement:
928512Sgeoffrey.blake@arm.com *      This product includes software developed by the NetBSD
938512Sgeoffrey.blake@arm.com *      Foundation, Inc. and its contributors.
948512Sgeoffrey.blake@arm.com * 4. Neither the name of The NetBSD Foundation nor the names of its
958512Sgeoffrey.blake@arm.com *    contributors may be used to endorse or promote products derived
968512Sgeoffrey.blake@arm.com *    from this software without specific prior written permission.
978512Sgeoffrey.blake@arm.com *
988512Sgeoffrey.blake@arm.com * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
998512Sgeoffrey.blake@arm.com * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
1008512Sgeoffrey.blake@arm.com * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
1018512Sgeoffrey.blake@arm.com * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
1028512Sgeoffrey.blake@arm.com * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
1038512Sgeoffrey.blake@arm.com * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
1048512Sgeoffrey.blake@arm.com * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
1058512Sgeoffrey.blake@arm.com * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
1068512Sgeoffrey.blake@arm.com * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
1078512Sgeoffrey.blake@arm.com * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
1088512Sgeoffrey.blake@arm.com * POSSIBILITY OF SUCH DAMAGE.
1098512Sgeoffrey.blake@arm.com */
1108512Sgeoffrey.blake@arm.com
1118512Sgeoffrey.blake@arm.com/*
1128512Sgeoffrey.blake@arm.com * $NetBSD: kgdb_stub.c,v 1.8 2001/07/07 22:58:00 wdk Exp $
1138512Sgeoffrey.blake@arm.com *
1148512Sgeoffrey.blake@arm.com * Taken from NetBSD
1158512Sgeoffrey.blake@arm.com *
1168512Sgeoffrey.blake@arm.com * "Stub" to allow remote cpu to debug over a serial line using gdb.
1178512Sgeoffrey.blake@arm.com */
1188512Sgeoffrey.blake@arm.com
1198512Sgeoffrey.blake@arm.com#include <sys/signal.h>
1208512Sgeoffrey.blake@arm.com
1218512Sgeoffrey.blake@arm.com#include <string>
1228512Sgeoffrey.blake@arm.com#include <unistd.h>
1238512Sgeoffrey.blake@arm.com
1248512Sgeoffrey.blake@arm.com#include "arch/vtophys.hh"
1258512Sgeoffrey.blake@arm.com#include "arch/sparc/remote_gdb.hh"
1268512Sgeoffrey.blake@arm.com#include "base/intmath.hh"
1278512Sgeoffrey.blake@arm.com#include "base/remote_gdb.hh"
1288512Sgeoffrey.blake@arm.com#include "base/socket.hh"
1298512Sgeoffrey.blake@arm.com#include "base/trace.hh"
1308512Sgeoffrey.blake@arm.com#include "config/full_system.hh"
1318512Sgeoffrey.blake@arm.com#include "cpu/thread_context.hh"
1328512Sgeoffrey.blake@arm.com#include "cpu/static_inst.hh"
1338512Sgeoffrey.blake@arm.com#include "mem/page_table.hh"
1348512Sgeoffrey.blake@arm.com#include "mem/physical.hh"
1358512Sgeoffrey.blake@arm.com#include "mem/port.hh"
1368512Sgeoffrey.blake@arm.com#include "sim/process.hh"
1378512Sgeoffrey.blake@arm.com#include "sim/system.hh"
1388512Sgeoffrey.blake@arm.com
1398512Sgeoffrey.blake@arm.comusing namespace std;
1408512Sgeoffrey.blake@arm.comusing namespace SparcISA;
1418512Sgeoffrey.blake@arm.com
1428512Sgeoffrey.blake@arm.comRemoteGDB::RemoteGDB(System *_system, ThreadContext *c)
1438512Sgeoffrey.blake@arm.com    : BaseRemoteGDB(_system, c, NumGDBRegs), nextBkpt(0)
1448512Sgeoffrey.blake@arm.com{}
1458512Sgeoffrey.blake@arm.com
1468512Sgeoffrey.blake@arm.com///////////////////////////////////////////////////////////
1478512Sgeoffrey.blake@arm.com// RemoteGDB::acc
1488512Sgeoffrey.blake@arm.com//
1498512Sgeoffrey.blake@arm.com//      Determine if the mapping at va..(va+len) is valid.
1508512Sgeoffrey.blake@arm.com//
1518512Sgeoffrey.blake@arm.combool
1528512Sgeoffrey.blake@arm.comRemoteGDB::acc(Addr va, size_t len)
1538512Sgeoffrey.blake@arm.com{
1548512Sgeoffrey.blake@arm.com    //@Todo In NetBSD, this function checks if all addresses
1558512Sgeoffrey.blake@arm.com    //from va to va + len have valid page map entries. Not
1568512Sgeoffrey.blake@arm.com    //sure how this will work for other OSes or in general.
1578512Sgeoffrey.blake@arm.com#if FULL_SYSTEM
1588512Sgeoffrey.blake@arm.com    if (va)
1598512Sgeoffrey.blake@arm.com        return true;
1608512Sgeoffrey.blake@arm.com    return false;
1618512Sgeoffrey.blake@arm.com#else
1628512Sgeoffrey.blake@arm.com    TlbEntry entry;
1638512Sgeoffrey.blake@arm.com    //Check to make sure the first byte is mapped into the processes address
1648512Sgeoffrey.blake@arm.com    //space.
1658512Sgeoffrey.blake@arm.com    if (context->getProcessPtr()->pTable->lookup(va, entry))
1668512Sgeoffrey.blake@arm.com        return true;
1678512Sgeoffrey.blake@arm.com    return false;
1688512Sgeoffrey.blake@arm.com#endif
1698512Sgeoffrey.blake@arm.com}
1708512Sgeoffrey.blake@arm.com
1718512Sgeoffrey.blake@arm.com///////////////////////////////////////////////////////////
1728512Sgeoffrey.blake@arm.com// RemoteGDB::getregs
1738512Sgeoffrey.blake@arm.com//
1748512Sgeoffrey.blake@arm.com//      Translate the kernel debugger register format into
1758512Sgeoffrey.blake@arm.com//      the GDB register format.
1768512Sgeoffrey.blake@arm.comvoid
1778512Sgeoffrey.blake@arm.comRemoteGDB::getregs()
1788512Sgeoffrey.blake@arm.com{
1798512Sgeoffrey.blake@arm.com    memset(gdbregs.regs, 0, gdbregs.size);
1808512Sgeoffrey.blake@arm.com
1818512Sgeoffrey.blake@arm.com    if (context->readMiscReg(MISCREG_PSTATE) &
1828512Sgeoffrey.blake@arm.com           PSTATE::am) {
1838512Sgeoffrey.blake@arm.com        uint32_t *regs;
1848512Sgeoffrey.blake@arm.com        regs = (uint32_t*)gdbregs.regs;
1858512Sgeoffrey.blake@arm.com        regs[Reg32Pc] = htobe((uint32_t)context->readPC());
1868512Sgeoffrey.blake@arm.com        regs[Reg32Npc] = htobe((uint32_t)context->readNextPC());
1878512Sgeoffrey.blake@arm.com        for(int x = RegG0; x <= RegI0 + 7; x++)
1888512Sgeoffrey.blake@arm.com            regs[x] = htobe((uint32_t)context->readIntReg(x - RegG0));
1898512Sgeoffrey.blake@arm.com
1908512Sgeoffrey.blake@arm.com        regs[Reg32Y] = htobe((uint32_t)context->readIntReg(NumIntArchRegs + 1));
1918512Sgeoffrey.blake@arm.com        regs[Reg32Psr] = htobe((uint32_t)context->readMiscReg(MISCREG_PSTATE));
1928512Sgeoffrey.blake@arm.com        regs[Reg32Fsr] = htobe((uint32_t)context->readMiscReg(MISCREG_FSR));
1938512Sgeoffrey.blake@arm.com        regs[Reg32Csr] = htobe((uint32_t)context->readIntReg(NumIntArchRegs + 2));
1948512Sgeoffrey.blake@arm.com    } else {
1958512Sgeoffrey.blake@arm.com        gdbregs.regs[RegPc] = htobe(context->readPC());
1968512Sgeoffrey.blake@arm.com        gdbregs.regs[RegNpc] = htobe(context->readNextPC());
1978512Sgeoffrey.blake@arm.com        for(int x = RegG0; x <= RegI0 + 7; x++)
1988512Sgeoffrey.blake@arm.com            gdbregs.regs[x] = htobe(context->readIntReg(x - RegG0));
1998512Sgeoffrey.blake@arm.com
2008512Sgeoffrey.blake@arm.com        gdbregs.regs[RegFsr] = htobe(context->readMiscReg(MISCREG_FSR));
2018512Sgeoffrey.blake@arm.com        gdbregs.regs[RegFprs] = htobe(context->readMiscReg(MISCREG_FPRS));
2028512Sgeoffrey.blake@arm.com        gdbregs.regs[RegY] = htobe(context->readIntReg(NumIntArchRegs + 1));
2038512Sgeoffrey.blake@arm.com        gdbregs.regs[RegState] = htobe(
2048512Sgeoffrey.blake@arm.com            context->readMiscReg(MISCREG_CWP) |
2058512Sgeoffrey.blake@arm.com            context->readMiscReg(MISCREG_PSTATE) << 8 |
2068512Sgeoffrey.blake@arm.com            context->readMiscReg(MISCREG_ASI) << 24 |
2078512Sgeoffrey.blake@arm.com            context->readIntReg(NumIntArchRegs + 2) << 32);
2088512Sgeoffrey.blake@arm.com    }
2098512Sgeoffrey.blake@arm.com
2108512Sgeoffrey.blake@arm.com    DPRINTF(GDBRead, "PC=%#x\n", gdbregs.regs[RegPc]);
2118512Sgeoffrey.blake@arm.com
2128512Sgeoffrey.blake@arm.com    //Floating point registers are left at 0 in netbsd
2138512Sgeoffrey.blake@arm.com    //All registers other than the pc, npc and int regs
2148512Sgeoffrey.blake@arm.com    //are ignored as well.
2158512Sgeoffrey.blake@arm.com}
2168512Sgeoffrey.blake@arm.com
2178512Sgeoffrey.blake@arm.com///////////////////////////////////////////////////////////
2188512Sgeoffrey.blake@arm.com// RemoteGDB::setregs
2198512Sgeoffrey.blake@arm.com//
2208512Sgeoffrey.blake@arm.com//      Translate the GDB register format into the kernel
2218512Sgeoffrey.blake@arm.com//      debugger register format.
2228512Sgeoffrey.blake@arm.com//
2238512Sgeoffrey.blake@arm.comvoid
2248512Sgeoffrey.blake@arm.comRemoteGDB::setregs()
2258512Sgeoffrey.blake@arm.com{
2268512Sgeoffrey.blake@arm.com    context->setPC(gdbregs.regs[RegPc]);
2278512Sgeoffrey.blake@arm.com    context->setNextPC(gdbregs.regs[RegNpc]);
2288512Sgeoffrey.blake@arm.com    for(int x = RegG0; x <= RegI0 + 7; x++)
2298512Sgeoffrey.blake@arm.com        context->setIntReg(x - RegG0, gdbregs.regs[x]);
2308512Sgeoffrey.blake@arm.com    //Only the integer registers, pc and npc are set in netbsd
2318512Sgeoffrey.blake@arm.com}
2328512Sgeoffrey.blake@arm.com
2338512Sgeoffrey.blake@arm.comvoid
2348512Sgeoffrey.blake@arm.comRemoteGDB::clearSingleStep()
2358512Sgeoffrey.blake@arm.com{
2368512Sgeoffrey.blake@arm.com   if (nextBkpt)
2378512Sgeoffrey.blake@arm.com       clearTempBreakpoint(nextBkpt);
2388512Sgeoffrey.blake@arm.com}
2398512Sgeoffrey.blake@arm.com
2408512Sgeoffrey.blake@arm.comvoid
2418512Sgeoffrey.blake@arm.comRemoteGDB::setSingleStep()
2428512Sgeoffrey.blake@arm.com{
2438512Sgeoffrey.blake@arm.com    nextBkpt = context->readNextPC();
2448512Sgeoffrey.blake@arm.com    setTempBreakpoint(nextBkpt);
2458512Sgeoffrey.blake@arm.com}
2468512Sgeoffrey.blake@arm.com