remote_gdb.cc revision 10601
13536SN/A/* 210595Sgabeblack@google.com * Copyright 2014 Google Inc. 310037SARM gem5 Developers * Copyright (c) 2010, 2013 ARM Limited 47752SWilliam.Wang@arm.com * All rights reserved 57752SWilliam.Wang@arm.com * 67752SWilliam.Wang@arm.com * The license below extends only to copyright in the software and shall 77752SWilliam.Wang@arm.com * not be construed as granting a license to any other intellectual 87752SWilliam.Wang@arm.com * property including but not limited to intellectual property relating 97752SWilliam.Wang@arm.com * to a hardware implementation of the functionality of the software 107752SWilliam.Wang@arm.com * licensed hereunder. You may use the software subject to the license 117752SWilliam.Wang@arm.com * terms below provided that you ensure that this notice is replicated 127752SWilliam.Wang@arm.com * unmodified and in its entirety in all distributions of the software, 137752SWilliam.Wang@arm.com * modified or unmodified, in source code or in binary form. 147752SWilliam.Wang@arm.com * 153536SN/A * Copyright (c) 2002-2005 The Regents of The University of Michigan 163536SN/A * All rights reserved. 173536SN/A * 183536SN/A * Redistribution and use in source and binary forms, with or without 193536SN/A * modification, are permitted provided that the following conditions are 203536SN/A * met: redistributions of source code must retain the above copyright 213536SN/A * notice, this list of conditions and the following disclaimer; 223536SN/A * redistributions in binary form must reproduce the above copyright 233536SN/A * notice, this list of conditions and the following disclaimer in the 243536SN/A * documentation and/or other materials provided with the distribution; 253536SN/A * neither the name of the copyright holders nor the names of its 263536SN/A * contributors may be used to endorse or promote products derived from 273536SN/A * this software without specific prior written permission. 283536SN/A * 293536SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 303536SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 313536SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 323536SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 333536SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 343536SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 353536SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 363536SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 373536SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 383536SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 393536SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 403536SN/A * 413536SN/A * Authors: Nathan Binkert 427752SWilliam.Wang@arm.com * William Wang 433536SN/A */ 443536SN/A 453536SN/A/* 468332Snate@binkert.org * Copyright (c) 1990, 1993 The Regents of the University of California 478332Snate@binkert.org * All rights reserved 483536SN/A * 493536SN/A * This software was developed by the Computer Systems Engineering group 503536SN/A * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and 513536SN/A * contributed to Berkeley. 523536SN/A * 533536SN/A * All advertising materials mentioning features or use of this software 543536SN/A * must display the following acknowledgement: 555543SN/A * This product includes software developed by the University of 565543SN/A * California, Lawrence Berkeley Laboratories. 573536SN/A * 583536SN/A * Redistribution and use in source and binary forms, with or without 593536SN/A * modification, are permitted provided that the following conditions 603536SN/A * are met: 613536SN/A * 1. Redistributions of source code must retain the above copyright 623536SN/A * notice, this list of conditions and the following disclaimer. 633536SN/A * 2. Redistributions in binary form must reproduce the above copyright 643536SN/A * notice, this list of conditions and the following disclaimer in the 653536SN/A * documentation and/or other materials provided with the distribution. 663536SN/A * 3. All advertising materials mentioning features or use of this software 673536SN/A * must display the following acknowledgement: 685543SN/A * This product includes software developed by the University of 695543SN/A * California, Berkeley and its contributors. 703536SN/A * 4. Neither the name of the University nor the names of its contributors 713536SN/A * may be used to endorse or promote products derived from this software 723536SN/A * without specific prior written permission. 733536SN/A * 743536SN/A * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 753536SN/A * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 763536SN/A * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 773536SN/A * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 783536SN/A * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 793536SN/A * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 803536SN/A * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 813536SN/A * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 823536SN/A * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 833536SN/A * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 843536SN/A * SUCH DAMAGE. 853536SN/A * 865543SN/A * @(#)kgdb_stub.c 8.4 (Berkeley) 1/12/94 873536SN/A */ 883536SN/A 893536SN/A/*- 903536SN/A * Copyright (c) 2001 The NetBSD Foundation, Inc. 913536SN/A * All rights reserved. 923536SN/A * 933536SN/A * This code is derived from software contributed to The NetBSD Foundation 943536SN/A * by Jason R. Thorpe. 953536SN/A * 963536SN/A * Redistribution and use in source and binary forms, with or without 973536SN/A * modification, are permitted provided that the following conditions 983536SN/A * are met: 993536SN/A * 1. Redistributions of source code must retain the above copyright 1003536SN/A * notice, this list of conditions and the following disclaimer. 1013536SN/A * 2. Redistributions in binary form must reproduce the above copyright 1023536SN/A * notice, this list of conditions and the following disclaimer in the 1033536SN/A * documentation and/or other materials provided with the distribution. 1043536SN/A * 3. All advertising materials mentioning features or use of this software 1053536SN/A * must display the following acknowledgement: 1065543SN/A * This product includes software developed by the NetBSD 1075543SN/A * Foundation, Inc. and its contributors. 1083536SN/A * 4. Neither the name of The NetBSD Foundation nor the names of its 1093536SN/A * contributors may be used to endorse or promote products derived 1103536SN/A * from this software without specific prior written permission. 1113536SN/A * 1123536SN/A * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 1133536SN/A * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 1143536SN/A * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 1153536SN/A * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 1163536SN/A * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 1173536SN/A * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 1183536SN/A * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 1193536SN/A * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 1203536SN/A * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 1213536SN/A * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 1223536SN/A * POSSIBILITY OF SUCH DAMAGE. 1233536SN/A */ 1243536SN/A 1253536SN/A/* 1263536SN/A * $NetBSD: kgdb_stub.c,v 1.8 2001/07/07 22:58:00 wdk Exp $ 1273536SN/A * 1283536SN/A * Taken from NetBSD 1293536SN/A * 1303536SN/A * "Stub" to allow remote cpu to debug over a serial line using gdb. 1313536SN/A */ 1323536SN/A 1333536SN/A#include <sys/signal.h> 1345569SN/A#include <unistd.h> 1353536SN/A 1363536SN/A#include <string> 1373536SN/A 1389020Sgblack@eecs.umich.edu#include "arch/arm/decoder.hh" 1398229Snate@binkert.org#include "arch/arm/pagetable.hh" 1408229Snate@binkert.org#include "arch/arm/registers.hh" 1418229Snate@binkert.org#include "arch/arm/remote_gdb.hh" 14210037SARM gem5 Developers#include "arch/arm/system.hh" 1437752SWilliam.Wang@arm.com#include "arch/arm/utility.hh" 1447752SWilliam.Wang@arm.com#include "arch/arm/vtophys.hh" 1453536SN/A#include "base/intmath.hh" 1463536SN/A#include "base/remote_gdb.hh" 1473536SN/A#include "base/socket.hh" 1483536SN/A#include "base/trace.hh" 1498229Snate@binkert.org#include "cpu/static_inst.hh" 1503536SN/A#include "cpu/thread_context.hh" 1517752SWilliam.Wang@arm.com#include "cpu/thread_state.hh" 1528232Snate@binkert.org#include "debug/GDBAcc.hh" 1538232Snate@binkert.org#include "debug/GDBMisc.hh" 1548229Snate@binkert.org#include "mem/page_table.hh" 1553536SN/A#include "mem/physical.hh" 1563536SN/A#include "mem/port.hh" 1578782Sgblack@eecs.umich.edu#include "sim/full_system.hh" 1583536SN/A#include "sim/system.hh" 1593536SN/A 1603536SN/Ausing namespace std; 1617752SWilliam.Wang@arm.comusing namespace ArmISA; 1623536SN/A 1635569SN/ARemoteGDB::RemoteGDB(System *_system, ThreadContext *tc) 16410601Sgabeblack@google.com : BaseRemoteGDB(_system, tc, GDB_REG_BYTES) 1653536SN/A{ 1663536SN/A} 1673536SN/A 1685569SN/A/* 1695569SN/A * Determine if the mapping at va..(va+len) is valid. 1705569SN/A */ 1713536SN/Abool 1723536SN/ARemoteGDB::acc(Addr va, size_t len) 1733536SN/A{ 1748782Sgblack@eecs.umich.edu if (FullSystem) { 1758782Sgblack@eecs.umich.edu Addr last_va; 1768782Sgblack@eecs.umich.edu va = truncPage(va); 1778782Sgblack@eecs.umich.edu last_va = roundPage(va + len); 1783536SN/A 1798782Sgblack@eecs.umich.edu do { 1808782Sgblack@eecs.umich.edu if (virtvalid(context, va)) { 1818782Sgblack@eecs.umich.edu return true; 1828782Sgblack@eecs.umich.edu } 1838782Sgblack@eecs.umich.edu va += PageBytes; 1848782Sgblack@eecs.umich.edu } while (va < last_va); 1858782Sgblack@eecs.umich.edu 1868782Sgblack@eecs.umich.edu DPRINTF(GDBAcc, "acc: %#x mapping is valid\n", va); 1878782Sgblack@eecs.umich.edu return true; 1888782Sgblack@eecs.umich.edu } else { 1898782Sgblack@eecs.umich.edu TlbEntry entry; 1908782Sgblack@eecs.umich.edu //Check to make sure the first byte is mapped into the processes address 1918782Sgblack@eecs.umich.edu //space. 1928782Sgblack@eecs.umich.edu if (context->getProcessPtr()->pTable->lookup(va, entry)) 1933536SN/A return true; 1948782Sgblack@eecs.umich.edu return false; 1958782Sgblack@eecs.umich.edu } 1963536SN/A} 1973536SN/A 1985569SN/A/* 1995569SN/A * Translate the kernel debugger register format into the GDB register 2005569SN/A * format. 2015569SN/A */ 2023536SN/Avoid 2033536SN/ARemoteGDB::getregs() 2043536SN/A{ 2057752SWilliam.Wang@arm.com DPRINTF(GDBAcc, "getregs in remotegdb \n"); 2067752SWilliam.Wang@arm.com 2073579SN/A memset(gdbregs.regs, 0, gdbregs.bytes()); 2083536SN/A 20910037SARM gem5 Developers if (inAArch64(context)) { // AArch64 21010037SARM gem5 Developers // x0-x31 21110595Sgabeblack@google.com for (int i = 0; i < 32; ++i) 21210595Sgabeblack@google.com gdbregs.regs64[GDB64_X0 + i] = context->readIntReg(INTREG_X0 + i); 21310037SARM gem5 Developers // pc 21410595Sgabeblack@google.com gdbregs.regs64[GDB64_PC] = context->pcState().pc(); 21510037SARM gem5 Developers // cpsr 21610595Sgabeblack@google.com gdbregs.regs64[GDB64_CPSR] = 21710595Sgabeblack@google.com context->readMiscRegNoEffect(MISCREG_CPSR); 21810037SARM gem5 Developers // v0-v31 21910595Sgabeblack@google.com for (int i = 0; i < 128; i += 4) { 22010595Sgabeblack@google.com int gdboff = GDB64_V0_32 + i; 22110595Sgabeblack@google.com gdbregs.regs32[gdboff + 0] = context->readFloatRegBits(i + 2); 22210595Sgabeblack@google.com gdbregs.regs32[gdboff + 1] = context->readFloatRegBits(i + 3); 22310595Sgabeblack@google.com gdbregs.regs32[gdboff + 2] = context->readFloatRegBits(i + 0); 22410595Sgabeblack@google.com gdbregs.regs32[gdboff + 3] = context->readFloatRegBits(i + 1); 22510037SARM gem5 Developers } 22610037SARM gem5 Developers } else { // AArch32 22710037SARM gem5 Developers // R0-R15 supervisor mode 22810595Sgabeblack@google.com gdbregs.regs32[GDB32_R0 + 0] = context->readIntReg(INTREG_R0); 22910595Sgabeblack@google.com gdbregs.regs32[GDB32_R0 + 1] = context->readIntReg(INTREG_R1); 23010595Sgabeblack@google.com gdbregs.regs32[GDB32_R0 + 2] = context->readIntReg(INTREG_R2); 23110595Sgabeblack@google.com gdbregs.regs32[GDB32_R0 + 3] = context->readIntReg(INTREG_R3); 23210595Sgabeblack@google.com gdbregs.regs32[GDB32_R0 + 4] = context->readIntReg(INTREG_R4); 23310595Sgabeblack@google.com gdbregs.regs32[GDB32_R0 + 5] = context->readIntReg(INTREG_R5); 23410595Sgabeblack@google.com gdbregs.regs32[GDB32_R0 + 6] = context->readIntReg(INTREG_R6); 23510595Sgabeblack@google.com gdbregs.regs32[GDB32_R0 + 7] = context->readIntReg(INTREG_R7); 23610595Sgabeblack@google.com gdbregs.regs32[GDB32_R0 + 8] = context->readIntReg(INTREG_R8); 23710595Sgabeblack@google.com gdbregs.regs32[GDB32_R0 + 9] = context->readIntReg(INTREG_R9); 23810595Sgabeblack@google.com gdbregs.regs32[GDB32_R0 + 10] = context->readIntReg(INTREG_R10); 23910595Sgabeblack@google.com gdbregs.regs32[GDB32_R0 + 11] = context->readIntReg(INTREG_R11); 24010595Sgabeblack@google.com gdbregs.regs32[GDB32_R0 + 12] = context->readIntReg(INTREG_R12); 24110595Sgabeblack@google.com gdbregs.regs32[GDB32_R0 + 13] = context->readIntReg(INTREG_SP); 24210595Sgabeblack@google.com gdbregs.regs32[GDB32_R0 + 14] = context->readIntReg(INTREG_LR); 24310595Sgabeblack@google.com gdbregs.regs32[GDB32_R0 + 15] = context->pcState().pc(); 2443536SN/A 24510037SARM gem5 Developers // CPSR 24610595Sgabeblack@google.com gdbregs.regs32[GDB32_CPSR] = context->readMiscRegNoEffect(MISCREG_CPSR); 2477752SWilliam.Wang@arm.com 24810037SARM gem5 Developers // vfpv3/neon floating point registers (32 double or 64 float) 24910595Sgabeblack@google.com for (int i = 0; i < NumFloatV7ArchRegs; ++i) 25010595Sgabeblack@google.com gdbregs.regs32[GDB32_F0 + i] = context->readFloatRegBits(i); 25110037SARM gem5 Developers 25210037SARM gem5 Developers // FPSCR 25310595Sgabeblack@google.com gdbregs.regs32[GDB32_FPSCR] = 25410595Sgabeblack@google.com context->readMiscRegNoEffect(MISCREG_FPSCR); 2553536SN/A } 2563536SN/A} 2573536SN/A 2585569SN/A/* 2595569SN/A * Translate the GDB register format into the kernel debugger register 2605569SN/A * format. 2615569SN/A */ 2623536SN/Avoid 2633536SN/ARemoteGDB::setregs() 2643536SN/A{ 2657752SWilliam.Wang@arm.com 2667752SWilliam.Wang@arm.com DPRINTF(GDBAcc, "setregs in remotegdb \n"); 26710037SARM gem5 Developers if (inAArch64(context)) { // AArch64 26810037SARM gem5 Developers // x0-x31 26910595Sgabeblack@google.com for (int i = 0; i < 32; ++i) 27010595Sgabeblack@google.com context->setIntReg(INTREG_X0 + i, gdbregs.regs64[GDB64_X0 + i]); 27110037SARM gem5 Developers // pc 27210595Sgabeblack@google.com context->pcState(gdbregs.regs64[GDB64_PC]); 27310037SARM gem5 Developers // cpsr 27410595Sgabeblack@google.com context->setMiscRegNoEffect(MISCREG_CPSR, gdbregs.regs64[GDB64_CPSR]); 27510037SARM gem5 Developers // v0-v31 27610595Sgabeblack@google.com for (int i = 0; i < 128; i += 4) { 27710595Sgabeblack@google.com int gdboff = GDB64_V0_32 + i; 27810595Sgabeblack@google.com context->setFloatRegBits(i + 2, gdbregs.regs32[gdboff + 0]); 27910595Sgabeblack@google.com context->setFloatRegBits(i + 3, gdbregs.regs32[gdboff + 1]); 28010595Sgabeblack@google.com context->setFloatRegBits(i + 0, gdbregs.regs32[gdboff + 2]); 28110595Sgabeblack@google.com context->setFloatRegBits(i + 1, gdbregs.regs32[gdboff + 3]); 28210037SARM gem5 Developers } 28310037SARM gem5 Developers } else { // AArch32 28410037SARM gem5 Developers // R0-R15 supervisor mode 28510037SARM gem5 Developers // arm registers are 32 bits wide, gdb registers are 64 bits wide 28610037SARM gem5 Developers // two arm registers are packed into one gdb register (little endian) 28710595Sgabeblack@google.com context->setIntReg(INTREG_R0, gdbregs.regs32[GDB32_R0 + 0]); 28810595Sgabeblack@google.com context->setIntReg(INTREG_R1, gdbregs.regs32[GDB32_R0 + 1]); 28910595Sgabeblack@google.com context->setIntReg(INTREG_R2, gdbregs.regs32[GDB32_R0 + 2]); 29010595Sgabeblack@google.com context->setIntReg(INTREG_R3, gdbregs.regs32[GDB32_R0 + 3]); 29110595Sgabeblack@google.com context->setIntReg(INTREG_R4, gdbregs.regs32[GDB32_R0 + 4]); 29210595Sgabeblack@google.com context->setIntReg(INTREG_R5, gdbregs.regs32[GDB32_R0 + 5]); 29310595Sgabeblack@google.com context->setIntReg(INTREG_R6, gdbregs.regs32[GDB32_R0 + 6]); 29410595Sgabeblack@google.com context->setIntReg(INTREG_R7, gdbregs.regs32[GDB32_R0 + 7]); 29510595Sgabeblack@google.com context->setIntReg(INTREG_R8, gdbregs.regs32[GDB32_R0 + 8]); 29610595Sgabeblack@google.com context->setIntReg(INTREG_R9, gdbregs.regs32[GDB32_R0 + 9]); 29710595Sgabeblack@google.com context->setIntReg(INTREG_R10, gdbregs.regs32[GDB32_R0 + 10]); 29810595Sgabeblack@google.com context->setIntReg(INTREG_R11, gdbregs.regs32[GDB32_R0 + 11]); 29910595Sgabeblack@google.com context->setIntReg(INTREG_R12, gdbregs.regs32[GDB32_R0 + 12]); 30010595Sgabeblack@google.com context->setIntReg(INTREG_SP, gdbregs.regs32[GDB32_R0 + 13]); 30110595Sgabeblack@google.com context->setIntReg(INTREG_LR, gdbregs.regs32[GDB32_R0 + 14]); 30210595Sgabeblack@google.com context->pcState(gdbregs.regs32[GDB32_R0 + 7]); 3037752SWilliam.Wang@arm.com 30410037SARM gem5 Developers //CPSR 30510595Sgabeblack@google.com context->setMiscRegNoEffect(MISCREG_CPSR, gdbregs.regs32[GDB32_CPSR]); 3067752SWilliam.Wang@arm.com 30710037SARM gem5 Developers //vfpv3/neon floating point registers (32 double or 64 float) 30810595Sgabeblack@google.com for (int i = 0; i < NumFloatV7ArchRegs; ++i) 30910595Sgabeblack@google.com context->setFloatRegBits(i, gdbregs.regs32[GDB32_F0 + i]); 3107752SWilliam.Wang@arm.com 31110037SARM gem5 Developers //FPSCR 31210595Sgabeblack@google.com context->setMiscReg(MISCREG_FPSCR, gdbregs.regs32[GDB32_FPSCR]); 3133536SN/A } 3143536SN/A} 3153536SN/A 3163536SN/A// Write bytes to kernel address space for debugger. 3173536SN/Abool 3183536SN/ARemoteGDB::write(Addr vaddr, size_t size, const char *data) 3193536SN/A{ 3207752SWilliam.Wang@arm.com return BaseRemoteGDB::write(vaddr, size, data); 3213536SN/A} 3223536SN/A 323