remote_gdb.cc revision 5567
12817Sksewell@umich.edu/*
22817Sksewell@umich.edu * Copyright (c) 2002-2005 The Regents of The University of Michigan
32817Sksewell@umich.edu * All rights reserved.
42817Sksewell@umich.edu *
52817Sksewell@umich.edu * Redistribution and use in source and binary forms, with or without
62817Sksewell@umich.edu * modification, are permitted provided that the following conditions are
72817Sksewell@umich.edu * met: redistributions of source code must retain the above copyright
82817Sksewell@umich.edu * notice, this list of conditions and the following disclaimer;
92817Sksewell@umich.edu * redistributions in binary form must reproduce the above copyright
102817Sksewell@umich.edu * notice, this list of conditions and the following disclaimer in the
112817Sksewell@umich.edu * documentation and/or other materials provided with the distribution;
122817Sksewell@umich.edu * neither the name of the copyright holders nor the names of its
132817Sksewell@umich.edu * contributors may be used to endorse or promote products derived from
142817Sksewell@umich.edu * this software without specific prior written permission.
152817Sksewell@umich.edu *
162817Sksewell@umich.edu * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
172817Sksewell@umich.edu * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
182817Sksewell@umich.edu * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
192817Sksewell@umich.edu * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
202817Sksewell@umich.edu * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
212817Sksewell@umich.edu * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
222817Sksewell@umich.edu * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
232817Sksewell@umich.edu * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
242817Sksewell@umich.edu * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
252817Sksewell@umich.edu * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
262817Sksewell@umich.edu * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
272817Sksewell@umich.edu *
282817Sksewell@umich.edu * Authors: Nathan Binkert
294202Sbinkertn@umich.edu */
302817Sksewell@umich.edu
312817Sksewell@umich.edu/*
322817Sksewell@umich.edu * Copyright (c) 1990, 1993
334202Sbinkertn@umich.edu *      The Regents of the University of California.  All rights reserved.
342817Sksewell@umich.edu *
355192Ssaidi@eecs.umich.edu * This software was developed by the Computer Systems Engineering group
365192Ssaidi@eecs.umich.edu * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
375192Ssaidi@eecs.umich.edu * contributed to Berkeley.
385192Ssaidi@eecs.umich.edu *
395192Ssaidi@eecs.umich.edu * All advertising materials mentioning features or use of this software
404202Sbinkertn@umich.edu * must display the following acknowledgement:
414486Sbinkertn@umich.edu *      This product includes software developed by the University of
424486Sbinkertn@umich.edu *      California, Lawrence Berkeley Laboratories.
434486Sbinkertn@umich.edu *
444486Sbinkertn@umich.edu * Redistribution and use in source and binary forms, with or without
454202Sbinkertn@umich.edu * modification, are permitted provided that the following conditions
464202Sbinkertn@umich.edu * are met:
474202Sbinkertn@umich.edu * 1. Redistributions of source code must retain the above copyright
484202Sbinkertn@umich.edu *    notice, this list of conditions and the following disclaimer.
495597Sgblack@eecs.umich.edu * 2. Redistributions in binary form must reproduce the above copyright
504202Sbinkertn@umich.edu *    notice, this list of conditions and the following disclaimer in the
515597Sgblack@eecs.umich.edu *    documentation and/or other materials provided with the distribution.
524202Sbinkertn@umich.edu * 3. All advertising materials mentioning features or use of this software
534202Sbinkertn@umich.edu *    must display the following acknowledgement:
544202Sbinkertn@umich.edu *      This product includes software developed by the University of
554202Sbinkertn@umich.edu *      California, Berkeley and its contributors.
564202Sbinkertn@umich.edu * 4. Neither the name of the University nor the names of its contributors
574202Sbinkertn@umich.edu *    may be used to endorse or promote products derived from this software
584202Sbinkertn@umich.edu *    without specific prior written permission.
594202Sbinkertn@umich.edu *
604202Sbinkertn@umich.edu * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
614202Sbinkertn@umich.edu * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
624202Sbinkertn@umich.edu * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
634202Sbinkertn@umich.edu * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
644202Sbinkertn@umich.edu * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
655597Sgblack@eecs.umich.edu * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
662817Sksewell@umich.edu * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
675192Ssaidi@eecs.umich.edu * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
685192Ssaidi@eecs.umich.edu * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
695192Ssaidi@eecs.umich.edu * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
705192Ssaidi@eecs.umich.edu * SUCH DAMAGE.
715192Ssaidi@eecs.umich.edu *
725192Ssaidi@eecs.umich.edu *      @(#)kgdb_stub.c 8.4 (Berkeley) 1/12/94
735192Ssaidi@eecs.umich.edu */
745192Ssaidi@eecs.umich.edu
755192Ssaidi@eecs.umich.edu/*-
765192Ssaidi@eecs.umich.edu * Copyright (c) 2001 The NetBSD Foundation, Inc.
775192Ssaidi@eecs.umich.edu * All rights reserved.
785192Ssaidi@eecs.umich.edu *
795192Ssaidi@eecs.umich.edu * This code is derived from software contributed to The NetBSD Foundation
805192Ssaidi@eecs.umich.edu * by Jason R. Thorpe.
814202Sbinkertn@umich.edu *
824497Sbinkertn@umich.edu * Redistribution and use in source and binary forms, with or without
834202Sbinkertn@umich.edu * modification, are permitted provided that the following conditions
84 * are met:
85 * 1. Redistributions of source code must retain the above copyright
86 *    notice, this list of conditions and the following disclaimer.
87 * 2. Redistributions in binary form must reproduce the above copyright
88 *    notice, this list of conditions and the following disclaimer in the
89 *    documentation and/or other materials provided with the distribution.
90 * 3. All advertising materials mentioning features or use of this software
91 *    must display the following acknowledgement:
92 *      This product includes software developed by the NetBSD
93 *      Foundation, Inc. and its contributors.
94 * 4. Neither the name of The NetBSD Foundation nor the names of its
95 *    contributors may be used to endorse or promote products derived
96 *    from this software without specific prior written permission.
97 *
98 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
99 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
100 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
101 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
102 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
103 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
104 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
105 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
106 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
107 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
108 * POSSIBILITY OF SUCH DAMAGE.
109 */
110
111/*
112 * $NetBSD: kgdb_stub.c,v 1.8 2001/07/07 22:58:00 wdk Exp $
113 *
114 * Taken from NetBSD
115 *
116 * "Stub" to allow remote cpu to debug over a serial line using gdb.
117 */
118
119#include <sys/signal.h>
120
121#include <string>
122#include <unistd.h>
123
124#include "config/full_system.hh"
125#if FULL_SYSTEM
126#include "arch/alpha/vtophys.hh"
127#endif
128
129#include "arch/alpha/kgdb.h"
130#include "arch/alpha/utility.hh"
131#include "arch/alpha/remote_gdb.hh"
132#include "base/intmath.hh"
133#include "base/remote_gdb.hh"
134#include "base/socket.hh"
135#include "base/trace.hh"
136#include "cpu/thread_context.hh"
137#include "cpu/static_inst.hh"
138#include "mem/physical.hh"
139#include "mem/port.hh"
140#include "sim/system.hh"
141
142using namespace std;
143using namespace AlphaISA;
144
145RemoteGDB::RemoteGDB(System *_system, ThreadContext *c)
146    : BaseRemoteGDB(_system, c, KGDB_NUMREGS)
147{
148    memset(gdbregs.regs, 0, gdbregs.bytes());
149}
150
151///////////////////////////////////////////////////////////
152// RemoteGDB::acc
153//
154//      Determine if the mapping at va..(va+len) is valid.
155//
156bool
157RemoteGDB::acc(Addr va, size_t len)
158{
159#if !FULL_SYSTEM
160    panic("acc function needs to be rewritten for SE mode\n");
161#else
162    Addr last_va;
163
164    va = AlphaISA::TruncPage(va);
165    last_va = AlphaISA::RoundPage(va + len);
166
167    do  {
168        if (AlphaISA::IsK0Seg(va)) {
169            if (va < (AlphaISA::K0SegBase + pmem->size())) {
170                DPRINTF(GDBAcc, "acc:   Mapping is valid  K0SEG <= "
171                        "%#x < K0SEG + size\n", va);
172                return true;
173            } else {
174                DPRINTF(GDBAcc, "acc:   Mapping invalid %#x > K0SEG + size\n",
175                        va);
176                return false;
177            }
178        }
179
180    /**
181     * This code says that all accesses to palcode (instruction and data)
182     * are valid since there isn't a va->pa mapping because palcode is
183     * accessed physically. At some point this should probably be cleaned up
184     * but there is no easy way to do it.
185     */
186
187        if (AlphaISA::PcPAL(va) || va < 0x10000)
188            return true;
189
190        Addr ptbr = context->readMiscRegNoEffect(AlphaISA::IPR_PALtemp20);
191        AlphaISA::PageTableEntry pte = AlphaISA::kernel_pte_lookup(context->getPhysPort(), ptbr, va);
192        if (!pte.valid()) {
193            DPRINTF(GDBAcc, "acc:   %#x pte is invalid\n", va);
194            return false;
195        }
196        va += AlphaISA::PageBytes;
197    } while (va < last_va);
198
199    DPRINTF(GDBAcc, "acc:   %#x mapping is valid\n", va);
200    return true;
201#endif
202}
203
204///////////////////////////////////////////////////////////
205// RemoteGDB::getregs
206//
207//      Translate the kernel debugger register format into
208//      the GDB register format.
209void
210RemoteGDB::getregs()
211{
212    memset(gdbregs.regs, 0, gdbregs.bytes());
213
214    gdbregs.regs[KGDB_REG_PC] = context->readPC();
215
216    // @todo: Currently this is very Alpha specific.
217    if (AlphaISA::PcPAL(gdbregs.regs[KGDB_REG_PC])) {
218        for (int i = 0; i < AlphaISA::NumIntArchRegs; ++i) {
219            gdbregs.regs[i] = context->readIntReg(AlphaISA::reg_redir[i]);
220        }
221    } else {
222        for (int i = 0; i < AlphaISA::NumIntArchRegs; ++i) {
223            gdbregs.regs[i] = context->readIntReg(i);
224        }
225    }
226
227#ifdef KGDB_FP_REGS
228    for (int i = 0; i < AlphaISA::NumFloatArchRegs; ++i) {
229        gdbregs.regs[i + KGDB_REG_F0] = context->readFloatRegBits(i);
230    }
231#endif
232}
233
234///////////////////////////////////////////////////////////
235// RemoteGDB::setregs
236//
237//      Translate the GDB register format into the kernel
238//      debugger register format.
239//
240void
241RemoteGDB::setregs()
242{
243    // @todo: Currently this is very Alpha specific.
244    if (AlphaISA::PcPAL(gdbregs.regs[KGDB_REG_PC])) {
245        for (int i = 0; i < AlphaISA::NumIntArchRegs; ++i) {
246            context->setIntReg(AlphaISA::reg_redir[i], gdbregs.regs[i]);
247        }
248    } else {
249        for (int i = 0; i < AlphaISA::NumIntArchRegs; ++i) {
250            context->setIntReg(i, gdbregs.regs[i]);
251        }
252    }
253
254#ifdef KGDB_FP_REGS
255    for (int i = 0; i < AlphaISA::NumFloatArchRegs; ++i) {
256        context->setFloatRegBits(i, gdbregs.regs[i + KGDB_REG_F0]);
257    }
258#endif
259    context->setPC(gdbregs.regs[KGDB_REG_PC]);
260}
261
262void
263RemoteGDB::clearSingleStep()
264{
265    DPRINTF(GDBMisc, "clearSingleStep bt_addr=%#x nt_addr=%#x\n",
266            takenBkpt, notTakenBkpt);
267
268    if (takenBkpt != 0)
269        clearTempBreakpoint(takenBkpt);
270
271    if (notTakenBkpt != 0)
272        clearTempBreakpoint(notTakenBkpt);
273}
274
275void
276RemoteGDB::setSingleStep()
277{
278    Addr pc = context->readPC();
279    Addr npc, bpc;
280    bool set_bt = false;
281
282    npc = pc + sizeof(MachInst);
283
284    // User was stopped at pc, e.g. the instruction at pc was not
285    // executed.
286    MachInst inst = read<MachInst>(pc);
287    StaticInstPtr si(inst, pc);
288    if (si->hasBranchTarget(pc, context, bpc)) {
289        // Don't bother setting a breakpoint on the taken branch if it
290        // is the same as the next pc
291        if (bpc != npc)
292            set_bt = true;
293    }
294
295    DPRINTF(GDBMisc, "setSingleStep bt_addr=%#x nt_addr=%#x\n",
296            takenBkpt, notTakenBkpt);
297
298    setTempBreakpoint(notTakenBkpt = npc);
299
300    if (set_bt)
301        setTempBreakpoint(takenBkpt = bpc);
302}
303
304// Write bytes to kernel address space for debugger.
305bool
306RemoteGDB::write(Addr vaddr, size_t size, const char *data)
307{
308    if (BaseRemoteGDB::write(vaddr, size, data)) {
309#ifdef IMB
310        alpha_pal_imb();
311#endif
312        return true;
313    } else {
314        return false;
315    }
316}
317
318