13536Sgblack@eecs.umich.edu/*
211274Sshingarov@labware.com * Copyright 2015 LabWare
310595Sgabeblack@google.com * Copyright 2014 Google, Inc.
43536Sgblack@eecs.umich.edu * Copyright (c) 2002-2005 The Regents of The University of Michigan
53536Sgblack@eecs.umich.edu * All rights reserved.
63536Sgblack@eecs.umich.edu *
73536Sgblack@eecs.umich.edu * Redistribution and use in source and binary forms, with or without
83536Sgblack@eecs.umich.edu * modification, are permitted provided that the following conditions are
93536Sgblack@eecs.umich.edu * met: redistributions of source code must retain the above copyright
103536Sgblack@eecs.umich.edu * notice, this list of conditions and the following disclaimer;
113536Sgblack@eecs.umich.edu * redistributions in binary form must reproduce the above copyright
123536Sgblack@eecs.umich.edu * notice, this list of conditions and the following disclaimer in the
133536Sgblack@eecs.umich.edu * documentation and/or other materials provided with the distribution;
143536Sgblack@eecs.umich.edu * neither the name of the copyright holders nor the names of its
153536Sgblack@eecs.umich.edu * contributors may be used to endorse or promote products derived from
163536Sgblack@eecs.umich.edu * this software without specific prior written permission.
173536Sgblack@eecs.umich.edu *
183536Sgblack@eecs.umich.edu * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
193536Sgblack@eecs.umich.edu * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
203536Sgblack@eecs.umich.edu * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
213536Sgblack@eecs.umich.edu * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
223536Sgblack@eecs.umich.edu * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
233536Sgblack@eecs.umich.edu * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
243536Sgblack@eecs.umich.edu * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
253536Sgblack@eecs.umich.edu * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
263536Sgblack@eecs.umich.edu * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
273536Sgblack@eecs.umich.edu * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
283536Sgblack@eecs.umich.edu * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
293536Sgblack@eecs.umich.edu *
303536Sgblack@eecs.umich.edu * Authors: Nathan Binkert
3111274Sshingarov@labware.com *          Boris Shingarov
323536Sgblack@eecs.umich.edu */
333536Sgblack@eecs.umich.edu
343536Sgblack@eecs.umich.edu/*
358332Snate@binkert.org * Copyright (c) 1990, 1993 The Regents of the University of California
368332Snate@binkert.org * All rights reserved
373536Sgblack@eecs.umich.edu *
383536Sgblack@eecs.umich.edu * This software was developed by the Computer Systems Engineering group
393536Sgblack@eecs.umich.edu * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
403536Sgblack@eecs.umich.edu * contributed to Berkeley.
413536Sgblack@eecs.umich.edu *
423536Sgblack@eecs.umich.edu * All advertising materials mentioning features or use of this software
433536Sgblack@eecs.umich.edu * must display the following acknowledgement:
445543Ssaidi@eecs.umich.edu *      This product includes software developed by the University of
455543Ssaidi@eecs.umich.edu *      California, Lawrence Berkeley Laboratories.
463536Sgblack@eecs.umich.edu *
473536Sgblack@eecs.umich.edu * Redistribution and use in source and binary forms, with or without
483536Sgblack@eecs.umich.edu * modification, are permitted provided that the following conditions
493536Sgblack@eecs.umich.edu * are met:
503536Sgblack@eecs.umich.edu * 1. Redistributions of source code must retain the above copyright
513536Sgblack@eecs.umich.edu *    notice, this list of conditions and the following disclaimer.
523536Sgblack@eecs.umich.edu * 2. Redistributions in binary form must reproduce the above copyright
533536Sgblack@eecs.umich.edu *    notice, this list of conditions and the following disclaimer in the
543536Sgblack@eecs.umich.edu *    documentation and/or other materials provided with the distribution.
553536Sgblack@eecs.umich.edu * 3. All advertising materials mentioning features or use of this software
563536Sgblack@eecs.umich.edu *    must display the following acknowledgement:
575543Ssaidi@eecs.umich.edu *      This product includes software developed by the University of
585543Ssaidi@eecs.umich.edu *      California, Berkeley and its contributors.
593536Sgblack@eecs.umich.edu * 4. Neither the name of the University nor the names of its contributors
603536Sgblack@eecs.umich.edu *    may be used to endorse or promote products derived from this software
613536Sgblack@eecs.umich.edu *    without specific prior written permission.
623536Sgblack@eecs.umich.edu *
633536Sgblack@eecs.umich.edu * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
643536Sgblack@eecs.umich.edu * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
653536Sgblack@eecs.umich.edu * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
663536Sgblack@eecs.umich.edu * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
673536Sgblack@eecs.umich.edu * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
683536Sgblack@eecs.umich.edu * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
693536Sgblack@eecs.umich.edu * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
703536Sgblack@eecs.umich.edu * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
713536Sgblack@eecs.umich.edu * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
723536Sgblack@eecs.umich.edu * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
733536Sgblack@eecs.umich.edu * SUCH DAMAGE.
743536Sgblack@eecs.umich.edu *
755543Ssaidi@eecs.umich.edu *      @(#)kgdb_stub.c 8.4 (Berkeley) 1/12/94
763536Sgblack@eecs.umich.edu */
773536Sgblack@eecs.umich.edu
783536Sgblack@eecs.umich.edu/*-
793536Sgblack@eecs.umich.edu * Copyright (c) 2001 The NetBSD Foundation, Inc.
803536Sgblack@eecs.umich.edu * All rights reserved.
813536Sgblack@eecs.umich.edu *
823536Sgblack@eecs.umich.edu * This code is derived from software contributed to The NetBSD Foundation
833536Sgblack@eecs.umich.edu * by Jason R. Thorpe.
843536Sgblack@eecs.umich.edu *
853536Sgblack@eecs.umich.edu * Redistribution and use in source and binary forms, with or without
863536Sgblack@eecs.umich.edu * modification, are permitted provided that the following conditions
873536Sgblack@eecs.umich.edu * are met:
883536Sgblack@eecs.umich.edu * 1. Redistributions of source code must retain the above copyright
893536Sgblack@eecs.umich.edu *    notice, this list of conditions and the following disclaimer.
903536Sgblack@eecs.umich.edu * 2. Redistributions in binary form must reproduce the above copyright
913536Sgblack@eecs.umich.edu *    notice, this list of conditions and the following disclaimer in the
923536Sgblack@eecs.umich.edu *    documentation and/or other materials provided with the distribution.
933536Sgblack@eecs.umich.edu * 3. All advertising materials mentioning features or use of this software
943536Sgblack@eecs.umich.edu *    must display the following acknowledgement:
955543Ssaidi@eecs.umich.edu *      This product includes software developed by the NetBSD
965543Ssaidi@eecs.umich.edu *      Foundation, Inc. and its contributors.
973536Sgblack@eecs.umich.edu * 4. Neither the name of The NetBSD Foundation nor the names of its
983536Sgblack@eecs.umich.edu *    contributors may be used to endorse or promote products derived
993536Sgblack@eecs.umich.edu *    from this software without specific prior written permission.
1003536Sgblack@eecs.umich.edu *
1013536Sgblack@eecs.umich.edu * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
1023536Sgblack@eecs.umich.edu * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
1033536Sgblack@eecs.umich.edu * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
1043536Sgblack@eecs.umich.edu * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
1053536Sgblack@eecs.umich.edu * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
1063536Sgblack@eecs.umich.edu * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
1073536Sgblack@eecs.umich.edu * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
1083536Sgblack@eecs.umich.edu * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
1093536Sgblack@eecs.umich.edu * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
1103536Sgblack@eecs.umich.edu * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
1113536Sgblack@eecs.umich.edu * POSSIBILITY OF SUCH DAMAGE.
1123536Sgblack@eecs.umich.edu */
1133536Sgblack@eecs.umich.edu
1143536Sgblack@eecs.umich.edu/*
1153536Sgblack@eecs.umich.edu * $NetBSD: kgdb_stub.c,v 1.8 2001/07/07 22:58:00 wdk Exp $
1163536Sgblack@eecs.umich.edu *
1173536Sgblack@eecs.umich.edu * Taken from NetBSD
1183536Sgblack@eecs.umich.edu *
1193536Sgblack@eecs.umich.edu * "Stub" to allow remote cpu to debug over a serial line using gdb.
1203536Sgblack@eecs.umich.edu */
1213536Sgblack@eecs.umich.edu
12211793Sbrandon.potter@amd.com#include "arch/sparc/remote_gdb.hh"
12311793Sbrandon.potter@amd.com
1243536Sgblack@eecs.umich.edu#include <sys/signal.h>
1258229Snate@binkert.org#include <unistd.h>
1263536Sgblack@eecs.umich.edu
12711793Sbrandon.potter@amd.com#include <csignal>
1283536Sgblack@eecs.umich.edu#include <string>
1293536Sgblack@eecs.umich.edu
1303536Sgblack@eecs.umich.edu#include "arch/vtophys.hh"
1313536Sgblack@eecs.umich.edu#include "base/intmath.hh"
1323536Sgblack@eecs.umich.edu#include "base/remote_gdb.hh"
1333536Sgblack@eecs.umich.edu#include "base/socket.hh"
1343536Sgblack@eecs.umich.edu#include "base/trace.hh"
1358229Snate@binkert.org#include "cpu/static_inst.hh"
1363536Sgblack@eecs.umich.edu#include "cpu/thread_context.hh"
13711274Sshingarov@labware.com#include "debug/GDBAcc.hh"
1388232Snate@binkert.org#include "debug/GDBRead.hh"
1395107Sgblack@eecs.umich.edu#include "mem/page_table.hh"
1403536Sgblack@eecs.umich.edu#include "mem/physical.hh"
1413536Sgblack@eecs.umich.edu#include "mem/port.hh"
1427678Sgblack@eecs.umich.edu#include "sim/byteswap.hh"
1438767Sgblack@eecs.umich.edu#include "sim/full_system.hh"
1445107Sgblack@eecs.umich.edu#include "sim/process.hh"
1453536Sgblack@eecs.umich.edu#include "sim/system.hh"
1463536Sgblack@eecs.umich.edu
1473536Sgblack@eecs.umich.eduusing namespace std;
1485567Snate@binkert.orgusing namespace SparcISA;
1493536Sgblack@eecs.umich.edu
15012449Sgabeblack@google.comRemoteGDB::RemoteGDB(System *_system, ThreadContext *c, int _port)
15112449Sgabeblack@google.com    : BaseRemoteGDB(_system, c, _port), regCache32(this), regCache64(this)
1523536Sgblack@eecs.umich.edu{}
1533536Sgblack@eecs.umich.edu
1543536Sgblack@eecs.umich.edu///////////////////////////////////////////////////////////
1553536Sgblack@eecs.umich.edu// RemoteGDB::acc
1563536Sgblack@eecs.umich.edu//
1575543Ssaidi@eecs.umich.edu//      Determine if the mapping at va..(va+len) is valid.
1583536Sgblack@eecs.umich.edu//
1593536Sgblack@eecs.umich.edubool
1603536Sgblack@eecs.umich.eduRemoteGDB::acc(Addr va, size_t len)
1613536Sgblack@eecs.umich.edu{
1623571Sgblack@eecs.umich.edu    //@Todo In NetBSD, this function checks if all addresses
1637741Sgblack@eecs.umich.edu    // from va to va + len have valid page map entries. Not
1647741Sgblack@eecs.umich.edu    // sure how this will work for other OSes or in general.
1658767Sgblack@eecs.umich.edu    if (FullSystem) {
16612455Sgabeblack@google.com        return va != 0;
1678767Sgblack@eecs.umich.edu    } else {
1688767Sgblack@eecs.umich.edu        // Check to make sure the first byte is mapped into the processes
1698767Sgblack@eecs.umich.edu        // address space.
17012455Sgabeblack@google.com        return context()->getProcessPtr()->pTable->lookup(va) != nullptr;
1718767Sgblack@eecs.umich.edu    }
1723536Sgblack@eecs.umich.edu}
1733536Sgblack@eecs.umich.edu
1743536Sgblack@eecs.umich.eduvoid
17511274Sshingarov@labware.comRemoteGDB::SPARCGdbRegCache::getRegs(ThreadContext *context)
1763536Sgblack@eecs.umich.edu{
17711274Sshingarov@labware.com    DPRINTF(GDBAcc, "getRegs in remotegdb \n");
17811274Sshingarov@labware.com    for (int i = 0; i < 32; i++) r.gpr[i] = htobe((uint32_t)context->readIntReg(i));
17911274Sshingarov@labware.com    PCState pc = context->pcState();
18011274Sshingarov@labware.com    r.pc = htobe((uint32_t)pc.pc());
18111274Sshingarov@labware.com    r.npc = htobe((uint32_t)pc.npc());
18211274Sshingarov@labware.com    r.y = htobe((uint32_t)context->readIntReg(NumIntArchRegs + 1));
18311274Sshingarov@labware.com    PSTATE pstate = context->readMiscReg(MISCREG_PSTATE);
18411274Sshingarov@labware.com    r.psr = htobe((uint32_t)pstate);
18511274Sshingarov@labware.com    r.fsr = htobe((uint32_t)context->readMiscReg(MISCREG_FSR));
18611274Sshingarov@labware.com    r.csr = htobe((uint32_t)context->readIntReg(NumIntArchRegs + 2));
18711274Sshingarov@labware.com}
1883536Sgblack@eecs.umich.edu
18911274Sshingarov@labware.comvoid
19011274Sshingarov@labware.comRemoteGDB::SPARC64GdbRegCache::getRegs(ThreadContext *context)
19111274Sshingarov@labware.com{
19211274Sshingarov@labware.com    DPRINTF(GDBAcc, "getRegs in remotegdb \n");
19311274Sshingarov@labware.com    for (int i = 0; i < 32; i++) r.gpr[i] = htobe(context->readIntReg(i));
19411274Sshingarov@labware.com    for (int i = 0; i < 32; i++) r.fpr[i] = 0;
1957720Sgblack@eecs.umich.edu    PCState pc = context->pcState();
19611274Sshingarov@labware.com    r.pc = htobe(pc.pc());
19711274Sshingarov@labware.com    r.npc = htobe(pc.npc());
19811274Sshingarov@labware.com    r.fsr = htobe(context->readMiscReg(MISCREG_FSR));
19911274Sshingarov@labware.com    r.fprs = htobe(context->readMiscReg(MISCREG_FPRS));
20011274Sshingarov@labware.com    r.y = htobe(context->readIntReg(NumIntArchRegs + 1));
2018829Sgblack@eecs.umich.edu    PSTATE pstate = context->readMiscReg(MISCREG_PSTATE);
20211274Sshingarov@labware.com    r.state = htobe(
20311274Sshingarov@labware.com        context->readMiscReg(MISCREG_CWP) |
20411274Sshingarov@labware.com        pstate << 8 |
20511274Sshingarov@labware.com        context->readMiscReg(MISCREG_ASI) << 24 |
20611274Sshingarov@labware.com        context->readIntReg(NumIntArchRegs + 2) << 32);
20711274Sshingarov@labware.com}
2087720Sgblack@eecs.umich.edu
20911274Sshingarov@labware.comvoid
21011274Sshingarov@labware.comRemoteGDB::SPARCGdbRegCache::setRegs(ThreadContext *context) const
21111274Sshingarov@labware.com{
21211274Sshingarov@labware.com    for (int i = 0; i < 32; i++) context->setIntReg(i, r.gpr[i]);
21311274Sshingarov@labware.com    PCState pc;
21411274Sshingarov@labware.com    pc.pc(r.pc);
21511274Sshingarov@labware.com    pc.npc(r.npc);
21611274Sshingarov@labware.com    pc.nnpc(pc.npc() + sizeof(MachInst));
21711274Sshingarov@labware.com    pc.upc(0);
21811274Sshingarov@labware.com    pc.nupc(1);
21911274Sshingarov@labware.com    context->pcState(pc);
2207741Sgblack@eecs.umich.edu    // Floating point registers are left at 0 in netbsd
2217741Sgblack@eecs.umich.edu    // All registers other than the pc, npc and int regs
2227741Sgblack@eecs.umich.edu    // are ignored as well.
2233536Sgblack@eecs.umich.edu}
2243536Sgblack@eecs.umich.edu
2253536Sgblack@eecs.umich.eduvoid
22611274Sshingarov@labware.comRemoteGDB::SPARC64GdbRegCache::setRegs(ThreadContext *context) const
2273536Sgblack@eecs.umich.edu{
22811274Sshingarov@labware.com    for (int i = 0; i < 32; i++) context->setIntReg(i, r.gpr[i]);
2297720Sgblack@eecs.umich.edu    PCState pc;
23011274Sshingarov@labware.com    pc.pc(r.pc);
23111274Sshingarov@labware.com    pc.npc(r.npc);
2327720Sgblack@eecs.umich.edu    pc.nnpc(pc.npc() + sizeof(MachInst));
2337720Sgblack@eecs.umich.edu    pc.upc(0);
2347720Sgblack@eecs.umich.edu    pc.nupc(1);
2357720Sgblack@eecs.umich.edu    context->pcState(pc);
23611274Sshingarov@labware.com    // Floating point registers are left at 0 in netbsd
23711274Sshingarov@labware.com    // All registers other than the pc, npc and int regs
23811274Sshingarov@labware.com    // are ignored as well.
2393536Sgblack@eecs.umich.edu}
24011274Sshingarov@labware.com
24111274Sshingarov@labware.com
24212449Sgabeblack@google.comBaseGdbRegCache*
24311274Sshingarov@labware.comRemoteGDB::gdbRegs()
24411274Sshingarov@labware.com{
24512449Sgabeblack@google.com    PSTATE pstate = context()->readMiscReg(MISCREG_PSTATE);
24612031Sgabeblack@google.com    if (pstate.am) {
24712031Sgabeblack@google.com        return &regCache32;
24812031Sgabeblack@google.com    } else {
24912031Sgabeblack@google.com        return &regCache64;
25012031Sgabeblack@google.com    }
25111274Sshingarov@labware.com}
252