remote_gdb.cc revision 11176
111176Sshingarov@labware.com/*
211176Sshingarov@labware.com * Copyright 2014 Google, Inc.
311176Sshingarov@labware.com * Copyright (c) 2010 ARM Limited
411176Sshingarov@labware.com * All rights reserved
511176Sshingarov@labware.com *
611176Sshingarov@labware.com * The license below extends only to copyright in the software and shall
711176Sshingarov@labware.com * not be construed as granting a license to any other intellectual
811176Sshingarov@labware.com * property including but not limited to intellectual property relating
911176Sshingarov@labware.com * to a hardware implementation of the functionality of the software
1011176Sshingarov@labware.com * licensed hereunder.  You may use the software subject to the license
1111176Sshingarov@labware.com * terms below provided that you ensure that this notice is replicated
1211176Sshingarov@labware.com * unmodified and in its entirety in all distributions of the software,
1311176Sshingarov@labware.com * modified or unmodified, in source code or in binary form.
1411176Sshingarov@labware.com *
1511176Sshingarov@labware.com * Copyright (c) 2002-2005 The Regents of The University of Michigan
1611176Sshingarov@labware.com * All rights reserved.
1711176Sshingarov@labware.com *
1811176Sshingarov@labware.com * Redistribution and use in source and binary forms, with or without
1911176Sshingarov@labware.com * modification, are permitted provided that the following conditions are
2011176Sshingarov@labware.com * met: redistributions of source code must retain the above copyright
2111176Sshingarov@labware.com * notice, this list of conditions and the following disclaimer;
2211176Sshingarov@labware.com * redistributions in binary form must reproduce the above copyright
2311176Sshingarov@labware.com * notice, this list of conditions and the following disclaimer in the
2411176Sshingarov@labware.com * documentation and/or other materials provided with the distribution;
2511176Sshingarov@labware.com * neither the name of the copyright holders nor the names of its
2611176Sshingarov@labware.com * contributors may be used to endorse or promote products derived from
2711176Sshingarov@labware.com * this software without specific prior written permission.
2811176Sshingarov@labware.com *
2911176Sshingarov@labware.com * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
3011176Sshingarov@labware.com * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
3111176Sshingarov@labware.com * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
3211176Sshingarov@labware.com * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
3311176Sshingarov@labware.com * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
3411176Sshingarov@labware.com * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
3511176Sshingarov@labware.com * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
3611176Sshingarov@labware.com * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
3711176Sshingarov@labware.com * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
3811176Sshingarov@labware.com * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
3911176Sshingarov@labware.com * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
4011176Sshingarov@labware.com *
4111176Sshingarov@labware.com * Authors: Nathan Binkert
4211176Sshingarov@labware.com *          William Wang
4311176Sshingarov@labware.com *          Deyuan Guo
4411176Sshingarov@labware.com *          Boris Shingarov
4511176Sshingarov@labware.com */
4611176Sshingarov@labware.com
4711176Sshingarov@labware.com/*
4811176Sshingarov@labware.com * Copyright (c) 1990, 1993 The Regents of the University of California
4911176Sshingarov@labware.com * All rights reserved
5011176Sshingarov@labware.com *
5111176Sshingarov@labware.com * This software was developed by the Computer Systems Engineering group
5211176Sshingarov@labware.com * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
5311176Sshingarov@labware.com * contributed to Berkeley.
5411176Sshingarov@labware.com *
5511176Sshingarov@labware.com * All advertising materials mentioning features or use of this software
5611176Sshingarov@labware.com * must display the following acknowledgement:
5711176Sshingarov@labware.com *      This product includes software developed by the University of
5811176Sshingarov@labware.com *      California, Lawrence Berkeley Laboratories.
5911176Sshingarov@labware.com *
6011176Sshingarov@labware.com * Redistribution and use in source and binary forms, with or without
6111176Sshingarov@labware.com * modification, are permitted provided that the following conditions
6211176Sshingarov@labware.com * are met:
6311176Sshingarov@labware.com * 1. Redistributions of source code must retain the above copyright
6411176Sshingarov@labware.com *    notice, this list of conditions and the following disclaimer.
6511176Sshingarov@labware.com * 2. Redistributions in binary form must reproduce the above copyright
6611176Sshingarov@labware.com *    notice, this list of conditions and the following disclaimer in the
6711176Sshingarov@labware.com *    documentation and/or other materials provided with the distribution.
6811176Sshingarov@labware.com * 3. All advertising materials mentioning features or use of this software
6911176Sshingarov@labware.com *    must display the following acknowledgement:
7011176Sshingarov@labware.com *      This product includes software developed by the University of
7111176Sshingarov@labware.com *      California, Berkeley and its contributors.
7211176Sshingarov@labware.com * 4. Neither the name of the University nor the names of its contributors
7311176Sshingarov@labware.com *    may be used to endorse or promote products derived from this software
7411176Sshingarov@labware.com *    without specific prior written permission.
7511176Sshingarov@labware.com *
7611176Sshingarov@labware.com * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
7711176Sshingarov@labware.com * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
7811176Sshingarov@labware.com * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
7911176Sshingarov@labware.com * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
8011176Sshingarov@labware.com * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
8111176Sshingarov@labware.com * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
8211176Sshingarov@labware.com * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
8311176Sshingarov@labware.com * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
8411176Sshingarov@labware.com * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
8511176Sshingarov@labware.com * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
8611176Sshingarov@labware.com * SUCH DAMAGE.
8711176Sshingarov@labware.com *
8811176Sshingarov@labware.com *      @(#)kgdb_stub.c 8.4 (Berkeley) 1/12/94
8911176Sshingarov@labware.com */
9011176Sshingarov@labware.com
9111176Sshingarov@labware.com/*-
9211176Sshingarov@labware.com * Copyright (c) 2001 The NetBSD Foundation, Inc.
9311176Sshingarov@labware.com * All rights reserved.
9411176Sshingarov@labware.com *
9511176Sshingarov@labware.com * This code is derived from software contributed to The NetBSD Foundation
9611176Sshingarov@labware.com * by Jason R. Thorpe.
9711176Sshingarov@labware.com *
9811176Sshingarov@labware.com * Redistribution and use in source and binary forms, with or without
9911176Sshingarov@labware.com * modification, are permitted provided that the following conditions
10011176Sshingarov@labware.com * are met:
10111176Sshingarov@labware.com * 1. Redistributions of source code must retain the above copyright
10211176Sshingarov@labware.com *    notice, this list of conditions and the following disclaimer.
10311176Sshingarov@labware.com * 2. Redistributions in binary form must reproduce the above copyright
10411176Sshingarov@labware.com *    notice, this list of conditions and the following disclaimer in the
10511176Sshingarov@labware.com *    documentation and/or other materials provided with the distribution.
10611176Sshingarov@labware.com * 3. All advertising materials mentioning features or use of this software
10711176Sshingarov@labware.com *    must display the following acknowledgement:
10811176Sshingarov@labware.com *      This product includes software developed by the NetBSD
10911176Sshingarov@labware.com *      Foundation, Inc. and its contributors.
11011176Sshingarov@labware.com * 4. Neither the name of The NetBSD Foundation nor the names of its
11111176Sshingarov@labware.com *    contributors may be used to endorse or promote products derived
11211176Sshingarov@labware.com *    from this software without specific prior written permission.
11311176Sshingarov@labware.com *
11411176Sshingarov@labware.com * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
11511176Sshingarov@labware.com * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
11611176Sshingarov@labware.com * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
11711176Sshingarov@labware.com * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
11811176Sshingarov@labware.com * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
11911176Sshingarov@labware.com * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
12011176Sshingarov@labware.com * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
12111176Sshingarov@labware.com * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
12211176Sshingarov@labware.com * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
12311176Sshingarov@labware.com * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
12411176Sshingarov@labware.com * POSSIBILITY OF SUCH DAMAGE.
12511176Sshingarov@labware.com */
12611176Sshingarov@labware.com
12711176Sshingarov@labware.com/*
12811176Sshingarov@labware.com * $NetBSD: kgdb_stub.c,v 1.8 2001/07/07 22:58:00 wdk Exp $
12911176Sshingarov@labware.com *
13011176Sshingarov@labware.com * Taken from NetBSD
13111176Sshingarov@labware.com *
13211176Sshingarov@labware.com * "Stub" to allow remote cpu to debug over a serial line using gdb.
13311176Sshingarov@labware.com */
13411176Sshingarov@labware.com
13511176Sshingarov@labware.com#include "arch/power/remote_gdb.hh"
13611176Sshingarov@labware.com
13711176Sshingarov@labware.com#include <sys/signal.h>
13811176Sshingarov@labware.com#include <unistd.h>
13911176Sshingarov@labware.com
14011176Sshingarov@labware.com#include <string>
14111176Sshingarov@labware.com
14211176Sshingarov@labware.com#include "arch/power/vtophys.hh"
14311176Sshingarov@labware.com#include "cpu/thread_state.hh"
14411176Sshingarov@labware.com#include "debug/GDBAcc.hh"
14511176Sshingarov@labware.com#include "debug/GDBMisc.hh"
14611176Sshingarov@labware.com#include "sim/byteswap.hh"
14711176Sshingarov@labware.com
14811176Sshingarov@labware.comusing namespace std;
14911176Sshingarov@labware.comusing namespace PowerISA;
15011176Sshingarov@labware.com
15111176Sshingarov@labware.comRemoteGDB::RemoteGDB(System *_system, ThreadContext *tc)
15211176Sshingarov@labware.com    : BaseRemoteGDB(_system, tc, GDB_REG_BYTES)
15311176Sshingarov@labware.com{
15411176Sshingarov@labware.com}
15511176Sshingarov@labware.com
15611176Sshingarov@labware.com/*
15711176Sshingarov@labware.com * Determine if the mapping at va..(va+len) is valid.
15811176Sshingarov@labware.com */
15911176Sshingarov@labware.combool
16011176Sshingarov@labware.comRemoteGDB::acc(Addr va, size_t len)
16111176Sshingarov@labware.com{
16211176Sshingarov@labware.com    TlbEntry entry;
16311176Sshingarov@labware.com    //Check to make sure the first byte is mapped into the processes address
16411176Sshingarov@labware.com    //space.  At the time of this writing, the acc() check is used when
16511176Sshingarov@labware.com    //processing the MemR/MemW packets before actually asking the translating
16611176Sshingarov@labware.com    //port proxy to read/writeBlob.  I (bgs) am not convinced the first byte
16711176Sshingarov@labware.com    //check is enough.
16811176Sshingarov@labware.com    if (FullSystem)
16911176Sshingarov@labware.com        panic("acc not implemented for POWER FS!");
17011176Sshingarov@labware.com    else
17111176Sshingarov@labware.com        return context->getProcessPtr()->pTable->lookup(va, entry);
17211176Sshingarov@labware.com}
17311176Sshingarov@labware.com
17411176Sshingarov@labware.com/*
17511176Sshingarov@labware.com * Translate the kernel debugger register format into the GDB register
17611176Sshingarov@labware.com * format.
17711176Sshingarov@labware.com *
17811176Sshingarov@labware.com * The PowerPC ISA is quite flexible in what register sets may be present
17911176Sshingarov@labware.com * depending on the features implemented by the particular CPU;
18011176Sshingarov@labware.com * GDB addresses this by describing the format of how register contents
18111176Sshingarov@labware.com * are transferred on the wire, in XML files such as 'power-core.xml'.
18211176Sshingarov@labware.com * Ideally, we should be reading these files instead of hardcoding this
18311176Sshingarov@labware.com * information, but for now the following implementation is enough to
18411176Sshingarov@labware.com * serve as the RSP backend for the out-of-the-box, default GDB.
18511176Sshingarov@labware.com */
18611176Sshingarov@labware.comvoid
18711176Sshingarov@labware.comRemoteGDB::getregs()
18811176Sshingarov@labware.com{
18911176Sshingarov@labware.com    DPRINTF(GDBAcc, "getregs in remotegdb \n");
19011176Sshingarov@labware.com    memset(gdbregs.regs, 0, gdbregs.bytes());
19111176Sshingarov@labware.com
19211176Sshingarov@labware.com    // Default order on 32-bit PowerPC:
19311176Sshingarov@labware.com    // R0-R31 (32-bit each), F0-F31 (64-bit IEEE754 double),
19411176Sshingarov@labware.com    // PC, MSR, CR, LR, CTR, XER (32-bit each)
19511176Sshingarov@labware.com
19611176Sshingarov@labware.com    // INTREG: R0~R31
19711176Sshingarov@labware.com    for (int i = 0; i < NumIntArchRegs; i++)
19811176Sshingarov@labware.com        gdbregs.regs32[GdbFirstGPRIndex + i] = htobe((uint32_t)context->readIntReg(i));
19911176Sshingarov@labware.com
20011176Sshingarov@labware.com    // FLOATREG: F0~F31
20111176Sshingarov@labware.com    for (int i = 0; i < NumFloatArchRegs; i++)
20211176Sshingarov@labware.com        gdbregs.regs32[GdbFirstFPRIndex + i] = context->readFloatRegBits(i);
20311176Sshingarov@labware.com
20411176Sshingarov@labware.com    // PC, MSR, CR, LR, CTR, XER
20511176Sshingarov@labware.com    gdbregs.regs32[GdbPCIndex] = htobe((uint32_t)context->pcState().pc());
20611176Sshingarov@labware.com    gdbregs.regs32[GdbMSRIndex] = 0; // Is MSR modeled?
20711176Sshingarov@labware.com    gdbregs.regs32[GdbCRIndex] = htobe((uint32_t)context->readIntReg(INTREG_CR));
20811176Sshingarov@labware.com    gdbregs.regs32[GdbLRIndex] = htobe((uint32_t)context->readIntReg(INTREG_LR));
20911176Sshingarov@labware.com    gdbregs.regs32[GdbCTRIndex] = htobe((uint32_t)context->readIntReg(INTREG_CTR));
21011176Sshingarov@labware.com    gdbregs.regs32[GdbXERIndex] = htobe((uint32_t)context->readIntReg(INTREG_XER));
21111176Sshingarov@labware.com}
21211176Sshingarov@labware.com
21311176Sshingarov@labware.com/*
21411176Sshingarov@labware.com * Translate the GDB register format into the kernel debugger register
21511176Sshingarov@labware.com * format.
21611176Sshingarov@labware.com */
21711176Sshingarov@labware.comvoid
21811176Sshingarov@labware.comRemoteGDB::setregs()
21911176Sshingarov@labware.com{
22011176Sshingarov@labware.com    DPRINTF(GDBAcc, "setregs in remotegdb \n");
22111176Sshingarov@labware.com
22211176Sshingarov@labware.com    // INTREG: R0~R31
22311176Sshingarov@labware.com    for (int i = 0; i < NumIntArchRegs; i++)
22411176Sshingarov@labware.com        context->setIntReg(i, betoh(gdbregs.regs32[GdbFirstGPRIndex + i]));
22511176Sshingarov@labware.com
22611176Sshingarov@labware.com    // FLOATREG: F0~F31
22711176Sshingarov@labware.com    for (int i = 0; i < NumFloatArchRegs; i++)
22811176Sshingarov@labware.com        context->setFloatRegBits(i, gdbregs.regs64[GdbFirstFPRIndex + i]);
22911176Sshingarov@labware.com
23011176Sshingarov@labware.com    // PC, MSR, CR, LR, CTR, XER
23111176Sshingarov@labware.com    context->pcState(betoh(gdbregs.regs32[GdbPCIndex]));
23211176Sshingarov@labware.com    // Is MSR modeled?
23311176Sshingarov@labware.com    context->setIntReg(INTREG_CR, betoh(gdbregs.regs32[GdbCRIndex]));
23411176Sshingarov@labware.com    context->setIntReg(INTREG_LR, betoh(gdbregs.regs32[GdbLRIndex]));
23511176Sshingarov@labware.com    context->setIntReg(INTREG_CTR, betoh(gdbregs.regs32[GdbCTRIndex]));
23611176Sshingarov@labware.com    context->setIntReg(INTREG_XER, betoh(gdbregs.regs32[GdbXERIndex]));
23711176Sshingarov@labware.com}
238