remote_gdb.cc revision 10595:25ecfc14f73f
1/*
2 * Copyright 2014 Google Inc.
3 * Copyright (c) 2010, 2013 ARM Limited
4 * All rights reserved
5 *
6 * The license below extends only to copyright in the software and shall
7 * not be construed as granting a license to any other intellectual
8 * property including but not limited to intellectual property relating
9 * to a hardware implementation of the functionality of the software
10 * licensed hereunder.  You may use the software subject to the license
11 * terms below provided that you ensure that this notice is replicated
12 * unmodified and in its entirety in all distributions of the software,
13 * modified or unmodified, in source code or in binary form.
14 *
15 * Copyright (c) 2002-2005 The Regents of The University of Michigan
16 * All rights reserved.
17 *
18 * Redistribution and use in source and binary forms, with or without
19 * modification, are permitted provided that the following conditions are
20 * met: redistributions of source code must retain the above copyright
21 * notice, this list of conditions and the following disclaimer;
22 * redistributions in binary form must reproduce the above copyright
23 * notice, this list of conditions and the following disclaimer in the
24 * documentation and/or other materials provided with the distribution;
25 * neither the name of the copyright holders nor the names of its
26 * contributors may be used to endorse or promote products derived from
27 * this software without specific prior written permission.
28 *
29 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
30 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
31 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
32 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
33 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
34 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
35 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
36 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
37 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
38 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
39 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40 *
41 * Authors: Nathan Binkert
42 *          William Wang
43 */
44
45/*
46 * Copyright (c) 1990, 1993 The Regents of the University of California
47 * All rights reserved
48 *
49 * This software was developed by the Computer Systems Engineering group
50 * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
51 * contributed to Berkeley.
52 *
53 * All advertising materials mentioning features or use of this software
54 * must display the following acknowledgement:
55 *      This product includes software developed by the University of
56 *      California, Lawrence Berkeley Laboratories.
57 *
58 * Redistribution and use in source and binary forms, with or without
59 * modification, are permitted provided that the following conditions
60 * are met:
61 * 1. Redistributions of source code must retain the above copyright
62 *    notice, this list of conditions and the following disclaimer.
63 * 2. Redistributions in binary form must reproduce the above copyright
64 *    notice, this list of conditions and the following disclaimer in the
65 *    documentation and/or other materials provided with the distribution.
66 * 3. All advertising materials mentioning features or use of this software
67 *    must display the following acknowledgement:
68 *      This product includes software developed by the University of
69 *      California, Berkeley and its contributors.
70 * 4. Neither the name of the University nor the names of its contributors
71 *    may be used to endorse or promote products derived from this software
72 *    without specific prior written permission.
73 *
74 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
75 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
76 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
77 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
78 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
79 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
80 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
81 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
82 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
83 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
84 * SUCH DAMAGE.
85 *
86 *      @(#)kgdb_stub.c 8.4 (Berkeley) 1/12/94
87 */
88
89/*-
90 * Copyright (c) 2001 The NetBSD Foundation, Inc.
91 * All rights reserved.
92 *
93 * This code is derived from software contributed to The NetBSD Foundation
94 * by Jason R. Thorpe.
95 *
96 * Redistribution and use in source and binary forms, with or without
97 * modification, are permitted provided that the following conditions
98 * are met:
99 * 1. Redistributions of source code must retain the above copyright
100 *    notice, this list of conditions and the following disclaimer.
101 * 2. Redistributions in binary form must reproduce the above copyright
102 *    notice, this list of conditions and the following disclaimer in the
103 *    documentation and/or other materials provided with the distribution.
104 * 3. All advertising materials mentioning features or use of this software
105 *    must display the following acknowledgement:
106 *      This product includes software developed by the NetBSD
107 *      Foundation, Inc. and its contributors.
108 * 4. Neither the name of The NetBSD Foundation nor the names of its
109 *    contributors may be used to endorse or promote products derived
110 *    from this software without specific prior written permission.
111 *
112 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
113 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
114 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
115 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
116 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
117 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
118 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
119 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
120 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
121 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
122 * POSSIBILITY OF SUCH DAMAGE.
123 */
124
125/*
126 * $NetBSD: kgdb_stub.c,v 1.8 2001/07/07 22:58:00 wdk Exp $
127 *
128 * Taken from NetBSD
129 *
130 * "Stub" to allow remote cpu to debug over a serial line using gdb.
131 */
132
133#include <sys/signal.h>
134#include <unistd.h>
135
136#include <string>
137
138#include "arch/arm/decoder.hh"
139#include "arch/arm/pagetable.hh"
140#include "arch/arm/registers.hh"
141#include "arch/arm/remote_gdb.hh"
142#include "arch/arm/system.hh"
143#include "arch/arm/utility.hh"
144#include "arch/arm/vtophys.hh"
145#include "base/intmath.hh"
146#include "base/remote_gdb.hh"
147#include "base/socket.hh"
148#include "base/trace.hh"
149#include "cpu/static_inst.hh"
150#include "cpu/thread_context.hh"
151#include "cpu/thread_state.hh"
152#include "debug/GDBAcc.hh"
153#include "debug/GDBMisc.hh"
154#include "mem/page_table.hh"
155#include "mem/physical.hh"
156#include "mem/port.hh"
157#include "sim/full_system.hh"
158#include "sim/system.hh"
159
160using namespace std;
161using namespace ArmISA;
162
163RemoteGDB::RemoteGDB(System *_system, ThreadContext *tc)
164    : BaseRemoteGDB(_system, tc, GDB_REG_BYTES),
165      notTakenBkpt(0), takenBkpt(0)
166{
167}
168
169/*
170 * Determine if the mapping at va..(va+len) is valid.
171 */
172bool
173RemoteGDB::acc(Addr va, size_t len)
174{
175    if (FullSystem) {
176        Addr last_va;
177        va       = truncPage(va);
178        last_va  = roundPage(va + len);
179
180        do  {
181            if (virtvalid(context, va)) {
182                return true;
183            }
184            va += PageBytes;
185        } while (va < last_va);
186
187        DPRINTF(GDBAcc, "acc:   %#x mapping is valid\n", va);
188        return true;
189    } else {
190        TlbEntry entry;
191        //Check to make sure the first byte is mapped into the processes address
192        //space.
193        if (context->getProcessPtr()->pTable->lookup(va, entry))
194            return true;
195        return false;
196    }
197}
198
199/*
200 * Translate the kernel debugger register format into the GDB register
201 * format.
202 */
203void
204RemoteGDB::getregs()
205{
206    DPRINTF(GDBAcc, "getregs in remotegdb \n");
207
208    memset(gdbregs.regs, 0, gdbregs.bytes());
209
210    if (inAArch64(context)) {  // AArch64
211        // x0-x31
212        for (int i = 0; i < 32; ++i)
213            gdbregs.regs64[GDB64_X0 + i] = context->readIntReg(INTREG_X0 + i);
214        // pc
215        gdbregs.regs64[GDB64_PC] = context->pcState().pc();
216        // cpsr
217        gdbregs.regs64[GDB64_CPSR] =
218            context->readMiscRegNoEffect(MISCREG_CPSR);
219        // v0-v31
220        for (int i = 0; i < 128; i += 4) {
221            int gdboff = GDB64_V0_32 + i;
222            gdbregs.regs32[gdboff + 0] = context->readFloatRegBits(i + 2);
223            gdbregs.regs32[gdboff + 1] = context->readFloatRegBits(i + 3);
224            gdbregs.regs32[gdboff + 2] = context->readFloatRegBits(i + 0);
225            gdbregs.regs32[gdboff + 3] = context->readFloatRegBits(i + 1);
226        }
227    } else {  // AArch32
228        // R0-R15 supervisor mode
229        gdbregs.regs32[GDB32_R0 + 0] = context->readIntReg(INTREG_R0);
230        gdbregs.regs32[GDB32_R0 + 1] = context->readIntReg(INTREG_R1);
231        gdbregs.regs32[GDB32_R0 + 2] = context->readIntReg(INTREG_R2);
232        gdbregs.regs32[GDB32_R0 + 3] = context->readIntReg(INTREG_R3);
233        gdbregs.regs32[GDB32_R0 + 4] = context->readIntReg(INTREG_R4);
234        gdbregs.regs32[GDB32_R0 + 5] = context->readIntReg(INTREG_R5);
235        gdbregs.regs32[GDB32_R0 + 6] = context->readIntReg(INTREG_R6);
236        gdbregs.regs32[GDB32_R0 + 7] = context->readIntReg(INTREG_R7);
237        gdbregs.regs32[GDB32_R0 + 8] = context->readIntReg(INTREG_R8);
238        gdbregs.regs32[GDB32_R0 + 9] = context->readIntReg(INTREG_R9);
239        gdbregs.regs32[GDB32_R0 + 10] = context->readIntReg(INTREG_R10);
240        gdbregs.regs32[GDB32_R0 + 11] = context->readIntReg(INTREG_R11);
241        gdbregs.regs32[GDB32_R0 + 12] = context->readIntReg(INTREG_R12);
242        gdbregs.regs32[GDB32_R0 + 13] = context->readIntReg(INTREG_SP);
243        gdbregs.regs32[GDB32_R0 + 14] = context->readIntReg(INTREG_LR);
244        gdbregs.regs32[GDB32_R0 + 15] = context->pcState().pc();
245
246        // CPSR
247        gdbregs.regs32[GDB32_CPSR] = context->readMiscRegNoEffect(MISCREG_CPSR);
248
249        // vfpv3/neon floating point registers (32 double or 64 float)
250        for (int i = 0; i < NumFloatV7ArchRegs; ++i)
251            gdbregs.regs32[GDB32_F0 + i] = context->readFloatRegBits(i);
252
253        // FPSCR
254        gdbregs.regs32[GDB32_FPSCR] =
255            context->readMiscRegNoEffect(MISCREG_FPSCR);
256    }
257}
258
259/*
260 * Translate the GDB register format into the kernel debugger register
261 * format.
262 */
263void
264RemoteGDB::setregs()
265{
266
267    DPRINTF(GDBAcc, "setregs in remotegdb \n");
268    if (inAArch64(context)) {  // AArch64
269        // x0-x31
270        for (int i = 0; i < 32; ++i)
271            context->setIntReg(INTREG_X0 + i, gdbregs.regs64[GDB64_X0 + i]);
272        // pc
273        context->pcState(gdbregs.regs64[GDB64_PC]);
274        // cpsr
275        context->setMiscRegNoEffect(MISCREG_CPSR, gdbregs.regs64[GDB64_CPSR]);
276        // v0-v31
277        for (int i = 0; i < 128; i += 4) {
278            int gdboff = GDB64_V0_32 + i;
279            context->setFloatRegBits(i + 2, gdbregs.regs32[gdboff + 0]);
280            context->setFloatRegBits(i + 3, gdbregs.regs32[gdboff + 1]);
281            context->setFloatRegBits(i + 0, gdbregs.regs32[gdboff + 2]);
282            context->setFloatRegBits(i + 1, gdbregs.regs32[gdboff + 3]);
283        }
284    } else {  // AArch32
285        // R0-R15 supervisor mode
286        // arm registers are 32 bits wide, gdb registers are 64 bits wide
287        // two arm registers are packed into one gdb register (little endian)
288        context->setIntReg(INTREG_R0, gdbregs.regs32[GDB32_R0 + 0]);
289        context->setIntReg(INTREG_R1, gdbregs.regs32[GDB32_R0 + 1]);
290        context->setIntReg(INTREG_R2, gdbregs.regs32[GDB32_R0 + 2]);
291        context->setIntReg(INTREG_R3, gdbregs.regs32[GDB32_R0 + 3]);
292        context->setIntReg(INTREG_R4, gdbregs.regs32[GDB32_R0 + 4]);
293        context->setIntReg(INTREG_R5, gdbregs.regs32[GDB32_R0 + 5]);
294        context->setIntReg(INTREG_R6, gdbregs.regs32[GDB32_R0 + 6]);
295        context->setIntReg(INTREG_R7, gdbregs.regs32[GDB32_R0 + 7]);
296        context->setIntReg(INTREG_R8, gdbregs.regs32[GDB32_R0 + 8]);
297        context->setIntReg(INTREG_R9, gdbregs.regs32[GDB32_R0 + 9]);
298        context->setIntReg(INTREG_R10, gdbregs.regs32[GDB32_R0 + 10]);
299        context->setIntReg(INTREG_R11, gdbregs.regs32[GDB32_R0 + 11]);
300        context->setIntReg(INTREG_R12, gdbregs.regs32[GDB32_R0 + 12]);
301        context->setIntReg(INTREG_SP, gdbregs.regs32[GDB32_R0 + 13]);
302        context->setIntReg(INTREG_LR, gdbregs.regs32[GDB32_R0 + 14]);
303        context->pcState(gdbregs.regs32[GDB32_R0 + 7]);
304
305        //CPSR
306        context->setMiscRegNoEffect(MISCREG_CPSR, gdbregs.regs32[GDB32_CPSR]);
307
308        //vfpv3/neon floating point registers (32 double or 64 float)
309        for (int i = 0; i < NumFloatV7ArchRegs; ++i)
310            context->setFloatRegBits(i, gdbregs.regs32[GDB32_F0 + i]);
311
312        //FPSCR
313        context->setMiscReg(MISCREG_FPSCR, gdbregs.regs32[GDB32_FPSCR]);
314    }
315}
316
317void
318RemoteGDB::clearSingleStep()
319{
320    DPRINTF(GDBMisc, "clearSingleStep bt_addr=%#x nt_addr=%#x\n",
321            takenBkpt, notTakenBkpt);
322
323    if (takenBkpt != 0)
324        clearTempBreakpoint(takenBkpt);
325
326    if (notTakenBkpt != 0)
327        clearTempBreakpoint(notTakenBkpt);
328}
329
330void
331RemoteGDB::setSingleStep()
332{
333    PCState pc = context->pcState();
334    PCState bpc;
335    bool set_bt = false;
336
337    // User was stopped at pc, e.g. the instruction at pc was not
338    // executed.
339    MachInst inst = read<MachInst>(pc.pc());
340    StaticInstPtr si = context->getDecoderPtr()->decode(inst, pc.pc());
341    if (si->hasBranchTarget(pc, context, bpc)) {
342        // Don't bother setting a breakpoint on the taken branch if it
343        // is the same as the next pc
344        if (bpc.pc() != pc.npc())
345            set_bt = true;
346    }
347
348    DPRINTF(GDBMisc, "setSingleStep bt_addr=%#x nt_addr=%#x\n",
349            takenBkpt, notTakenBkpt);
350
351    setTempBreakpoint(notTakenBkpt = pc.npc());
352
353    if (set_bt)
354        setTempBreakpoint(takenBkpt = bpc.pc());
355}
356
357// Write bytes to kernel address space for debugger.
358bool
359RemoteGDB::write(Addr vaddr, size_t size, const char *data)
360{
361    return BaseRemoteGDB::write(vaddr, size, data);
362}
363
364