remote_gdb.cc revision 13471
16145Snate@binkert.org/*
26145Snate@binkert.org * Copyright 2015 LabWare
36145Snate@binkert.org * Copyright 2014 Google Inc.
46145Snate@binkert.org * Copyright (c) 2010, 2013, 2016 ARM Limited
56145Snate@binkert.org * All rights reserved
66145Snate@binkert.org *
76145Snate@binkert.org * The license below extends only to copyright in the software and shall
86145Snate@binkert.org * not be construed as granting a license to any other intellectual
96145Snate@binkert.org * property including but not limited to intellectual property relating
106145Snate@binkert.org * to a hardware implementation of the functionality of the software
116145Snate@binkert.org * licensed hereunder.  You may use the software subject to the license
126145Snate@binkert.org * terms below provided that you ensure that this notice is replicated
136145Snate@binkert.org * unmodified and in its entirety in all distributions of the software,
146145Snate@binkert.org * modified or unmodified, in source code or in binary form.
156145Snate@binkert.org *
166145Snate@binkert.org * Copyright (c) 2002-2005 The Regents of The University of Michigan
176145Snate@binkert.org * All rights reserved.
186145Snate@binkert.org *
196145Snate@binkert.org * Redistribution and use in source and binary forms, with or without
206145Snate@binkert.org * modification, are permitted provided that the following conditions are
216145Snate@binkert.org * met: redistributions of source code must retain the above copyright
226145Snate@binkert.org * notice, this list of conditions and the following disclaimer;
236145Snate@binkert.org * redistributions in binary form must reproduce the above copyright
246145Snate@binkert.org * notice, this list of conditions and the following disclaimer in the
256145Snate@binkert.org * documentation and/or other materials provided with the distribution;
266145Snate@binkert.org * neither the name of the copyright holders nor the names of its
276145Snate@binkert.org * contributors may be used to endorse or promote products derived from
286145Snate@binkert.org * this software without specific prior written permission.
296145Snate@binkert.org *
306284Snate@binkert.org * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
316284Snate@binkert.org * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
326284Snate@binkert.org * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
336284Snate@binkert.org * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
346145Snate@binkert.org * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
356284Snate@binkert.org * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
366284Snate@binkert.org * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
376145Snate@binkert.org * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
386284Snate@binkert.org * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
396145Snate@binkert.org * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
406284Snate@binkert.org * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
416145Snate@binkert.org *
426284Snate@binkert.org * Authors: Nathan Binkert
436145Snate@binkert.org *          William Wang
446145Snate@binkert.org *          Boris Shingarov
457048Snate@binkert.org */
467048Snate@binkert.org
476145Snate@binkert.org/*
487455Snate@binkert.org * Copyright (c) 1990, 1993 The Regents of the University of California
497055Snate@binkert.org * All rights reserved
507454Snate@binkert.org *
517055Snate@binkert.org * This software was developed by the Computer Systems Engineering group
5210012Snilay@cs.wisc.edu * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
537455Snate@binkert.org * contributed to Berkeley.
5410012Snilay@cs.wisc.edu *
556154Snate@binkert.org * All advertising materials mentioning features or use of this software
566154Snate@binkert.org * must display the following acknowledgement:
578229Snate@binkert.org *      This product includes software developed by the University of
588229Snate@binkert.org *      California, Lawrence Berkeley Laboratories.
5910301Snilay@cs.wisc.edu *
6010012Snilay@cs.wisc.edu * Redistribution and use in source and binary forms, with or without
616876Ssteve.reinhardt@amd.com * modification, are permitted provided that the following conditions
628174Snilay@cs.wisc.edu * are met:
636145Snate@binkert.org * 1. Redistributions of source code must retain the above copyright
646145Snate@binkert.org *    notice, this list of conditions and the following disclaimer.
6510012Snilay@cs.wisc.edu * 2. Redistributions in binary form must reproduce the above copyright
667048Snate@binkert.org *    notice, this list of conditions and the following disclaimer in the
677048Snate@binkert.org *    documentation and/or other materials provided with the distribution.
6810920Sbrandon.potter@amd.com * 3. All advertising materials mentioning features or use of this software
697048Snate@binkert.org *    must display the following acknowledgement:
706145Snate@binkert.org *      This product includes software developed by the University of
7110920Sbrandon.potter@amd.com *      California, Berkeley and its contributors.
7210920Sbrandon.potter@amd.com * 4. Neither the name of the University nor the names of its contributors
737048Snate@binkert.org *    may be used to endorse or promote products derived from this software
7410012Snilay@cs.wisc.edu *    without specific prior written permission.
7510012Snilay@cs.wisc.edu *
766145Snate@binkert.org * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
777048Snate@binkert.org * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
787048Snate@binkert.org * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
796145Snate@binkert.org * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
808174Snilay@cs.wisc.edu * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
816145Snate@binkert.org * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
827048Snate@binkert.org * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
837048Snate@binkert.org * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
847048Snate@binkert.org * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
856145Snate@binkert.org * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
867048Snate@binkert.org * SUCH DAMAGE.
877048Snate@binkert.org *
887048Snate@binkert.org *      @(#)kgdb_stub.c 8.4 (Berkeley) 1/12/94
897048Snate@binkert.org */
906145Snate@binkert.org
917048Snate@binkert.org/*-
927048Snate@binkert.org * Copyright (c) 2001 The NetBSD Foundation, Inc.
936145Snate@binkert.org * All rights reserved.
9410012Snilay@cs.wisc.edu *
9510012Snilay@cs.wisc.edu * This code is derived from software contributed to The NetBSD Foundation
966145Snate@binkert.org * by Jason R. Thorpe.
9710012Snilay@cs.wisc.edu *
9810012Snilay@cs.wisc.edu * Redistribution and use in source and binary forms, with or without
996145Snate@binkert.org * modification, are permitted provided that the following conditions
10010012Snilay@cs.wisc.edu * are met:
10110012Snilay@cs.wisc.edu * 1. Redistributions of source code must retain the above copyright
10210012Snilay@cs.wisc.edu *    notice, this list of conditions and the following disclaimer.
1036145Snate@binkert.org * 2. Redistributions in binary form must reproduce the above copyright
10410012Snilay@cs.wisc.edu *    notice, this list of conditions and the following disclaimer in the
10510012Snilay@cs.wisc.edu *    documentation and/or other materials provided with the distribution.
10610012Snilay@cs.wisc.edu * 3. All advertising materials mentioning features or use of this software
10710012Snilay@cs.wisc.edu *    must display the following acknowledgement:
10810012Snilay@cs.wisc.edu *      This product includes software developed by the NetBSD
10910012Snilay@cs.wisc.edu *      Foundation, Inc. and its contributors.
11010012Snilay@cs.wisc.edu * 4. Neither the name of The NetBSD Foundation nor the names of its
11110012Snilay@cs.wisc.edu *    contributors may be used to endorse or promote products derived
11210012Snilay@cs.wisc.edu *    from this software without specific prior written permission.
11310012Snilay@cs.wisc.edu *
11410012Snilay@cs.wisc.edu * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
11510012Snilay@cs.wisc.edu * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
11610012Snilay@cs.wisc.edu * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
11710012Snilay@cs.wisc.edu * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
11810012Snilay@cs.wisc.edu * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
11910012Snilay@cs.wisc.edu * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
12010012Snilay@cs.wisc.edu * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
12110012Snilay@cs.wisc.edu * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
12210012Snilay@cs.wisc.edu * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
12310012Snilay@cs.wisc.edu * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
12410012Snilay@cs.wisc.edu * POSSIBILITY OF SUCH DAMAGE.
12510012Snilay@cs.wisc.edu */
12610012Snilay@cs.wisc.edu
12710012Snilay@cs.wisc.edu/*
12810012Snilay@cs.wisc.edu * $NetBSD: kgdb_stub.c,v 1.8 2001/07/07 22:58:00 wdk Exp $
12910094Snilay@cs.wisc.edu *
1306896SBrad.Beckmann@amd.com * Taken from NetBSD
1317048Snate@binkert.org *
1327048Snate@binkert.org * "Stub" to allow remote cpu to debug over a serial line using gdb.
1337048Snate@binkert.org */
1346145Snate@binkert.org
1356145Snate@binkert.org#include "arch/arm/remote_gdb.hh"
1367048Snate@binkert.org
137#include <sys/signal.h>
138#include <unistd.h>
139
140#include <string>
141
142#include "arch/arm/decoder.hh"
143#include "arch/arm/pagetable.hh"
144#include "arch/arm/registers.hh"
145#include "arch/arm/system.hh"
146#include "arch/arm/utility.hh"
147#include "arch/arm/vtophys.hh"
148#include "base/chunk_generator.hh"
149#include "base/intmath.hh"
150#include "base/remote_gdb.hh"
151#include "base/socket.hh"
152#include "base/trace.hh"
153#include "cpu/static_inst.hh"
154#include "cpu/thread_context.hh"
155#include "cpu/thread_state.hh"
156#include "debug/GDBAcc.hh"
157#include "debug/GDBMisc.hh"
158#include "mem/page_table.hh"
159#include "mem/physical.hh"
160#include "mem/port.hh"
161#include "sim/full_system.hh"
162#include "sim/system.hh"
163
164using namespace std;
165using namespace ArmISA;
166
167RemoteGDB::RemoteGDB(System *_system, ThreadContext *tc, int _port)
168    : BaseRemoteGDB(_system, tc, _port), regCache32(this), regCache64(this)
169{
170}
171
172/*
173 * Determine if the mapping at va..(va+len) is valid.
174 */
175bool
176RemoteGDB::acc(Addr va, size_t len)
177{
178    if (FullSystem) {
179        for (ChunkGenerator gen(va, len, PageBytes); !gen.done(); gen.next()) {
180            if (!virtvalid(context(), gen.addr())) {
181                DPRINTF(GDBAcc, "acc:   %#x mapping is invalid\n", va);
182                return false;
183            }
184        }
185
186        DPRINTF(GDBAcc, "acc:   %#x mapping is valid\n", va);
187        return true;
188    } else {
189        // Check to make sure the first byte is mapped into the processes
190        // address space.
191        return context()->getProcessPtr()->pTable->lookup(va) != nullptr;
192    }
193}
194
195void
196RemoteGDB::AArch64GdbRegCache::getRegs(ThreadContext *context)
197{
198    DPRINTF(GDBAcc, "getRegs in remotegdb \n");
199
200    for (int i = 0; i < 31; ++i)
201        r.x[i] = context->readIntReg(INTREG_X0 + i);
202    r.spx = context->readIntReg(INTREG_SPX);
203    r.pc = context->pcState().pc();
204    r.cpsr = context->readMiscRegNoEffect(MISCREG_CPSR);
205
206    size_t base = 0;
207    for (int i = 0; i < NumVecV8ArchRegs; i++) {
208        auto v = (context->readVecReg(RegId(VecRegClass, i))).as<VecElem>();
209        for (size_t j = 0; j < NumVecElemPerVecReg; j++) {
210            r.v[base] = v[j];
211            base++;
212        }
213    }
214}
215
216void
217RemoteGDB::AArch64GdbRegCache::setRegs(ThreadContext *context) const
218{
219    DPRINTF(GDBAcc, "setRegs in remotegdb \n");
220
221    for (int i = 0; i < 31; ++i)
222        context->setIntReg(INTREG_X0 + i, r.x[i]);
223    auto pc_state = context->pcState();
224    pc_state.set(r.pc);
225    context->pcState(pc_state);
226    context->setMiscRegNoEffect(MISCREG_CPSR, r.cpsr);
227    // Update the stack pointer. This should be done after
228    // updating CPSR/PSTATE since that might affect how SPX gets
229    // mapped.
230    context->setIntReg(INTREG_SPX, r.spx);
231
232    size_t base = 0;
233    for (int i = 0; i < NumVecV8ArchRegs; i++) {
234        auto v = (context->getWritableVecReg(
235                RegId(VecRegClass, i))).as<VecElem>();
236        for (size_t j = 0; j < NumVecElemPerVecReg; j++) {
237            v[j] = r.v[base];
238            base++;
239        }
240    }
241}
242
243void
244RemoteGDB::AArch32GdbRegCache::getRegs(ThreadContext *context)
245{
246    DPRINTF(GDBAcc, "getRegs in remotegdb \n");
247
248    r.gpr[0] = context->readIntReg(INTREG_R0);
249    r.gpr[1] = context->readIntReg(INTREG_R1);
250    r.gpr[2] = context->readIntReg(INTREG_R2);
251    r.gpr[3] = context->readIntReg(INTREG_R3);
252    r.gpr[4] = context->readIntReg(INTREG_R4);
253    r.gpr[5] = context->readIntReg(INTREG_R5);
254    r.gpr[6] = context->readIntReg(INTREG_R6);
255    r.gpr[7] = context->readIntReg(INTREG_R7);
256    r.gpr[8] = context->readIntReg(INTREG_R8);
257    r.gpr[9] = context->readIntReg(INTREG_R9);
258    r.gpr[10] = context->readIntReg(INTREG_R10);
259    r.gpr[11] = context->readIntReg(INTREG_R11);
260    r.gpr[12] = context->readIntReg(INTREG_R12);
261    r.gpr[13] = context->readIntReg(INTREG_SP);
262    r.gpr[14] = context->readIntReg(INTREG_LR);
263    r.gpr[15] = context->pcState().pc();
264
265    // One day somebody will implement transfer of FPRs correctly.
266    for (int i=0; i<8*3; i++) r.fpr[i] = 0;
267
268    r.fpscr = context->readMiscRegNoEffect(MISCREG_FPSCR);
269    r.cpsr = context->readMiscRegNoEffect(MISCREG_CPSR);
270}
271
272void
273RemoteGDB::AArch32GdbRegCache::setRegs(ThreadContext *context) const
274{
275    DPRINTF(GDBAcc, "setRegs in remotegdb \n");
276
277    context->setIntReg(INTREG_R0, r.gpr[0]);
278    context->setIntReg(INTREG_R1, r.gpr[1]);
279    context->setIntReg(INTREG_R2, r.gpr[2]);
280    context->setIntReg(INTREG_R3, r.gpr[3]);
281    context->setIntReg(INTREG_R4, r.gpr[4]);
282    context->setIntReg(INTREG_R5, r.gpr[5]);
283    context->setIntReg(INTREG_R6, r.gpr[6]);
284    context->setIntReg(INTREG_R7, r.gpr[7]);
285    context->setIntReg(INTREG_R8, r.gpr[8]);
286    context->setIntReg(INTREG_R9, r.gpr[9]);
287    context->setIntReg(INTREG_R10, r.gpr[10]);
288    context->setIntReg(INTREG_R11, r.gpr[11]);
289    context->setIntReg(INTREG_R12, r.gpr[12]);
290    context->setIntReg(INTREG_SP, r.gpr[13]);
291    context->setIntReg(INTREG_LR, r.gpr[14]);
292    auto pc_state = context->pcState();
293    pc_state.set(r.gpr[15]);
294    context->pcState(pc_state);
295
296    // One day somebody will implement transfer of FPRs correctly.
297
298    context->setMiscReg(MISCREG_FPSCR, r.fpscr);
299    context->setMiscRegNoEffect(MISCREG_CPSR, r.cpsr);
300}
301
302BaseGdbRegCache*
303RemoteGDB::gdbRegs()
304{
305    if (inAArch64(context()))
306        return &regCache64;
307    else
308        return &regCache32;
309}
310