remote_gdb.cc revision 3536
110389SAndreas.Sandberg@ARM.com/*
210389SAndreas.Sandberg@ARM.com * Copyright (c) 2002-2005 The Regents of The University of Michigan
310389SAndreas.Sandberg@ARM.com * All rights reserved.
410389SAndreas.Sandberg@ARM.com *
510389SAndreas.Sandberg@ARM.com * Redistribution and use in source and binary forms, with or without
610389SAndreas.Sandberg@ARM.com * modification, are permitted provided that the following conditions are
710389SAndreas.Sandberg@ARM.com * met: redistributions of source code must retain the above copyright
810389SAndreas.Sandberg@ARM.com * notice, this list of conditions and the following disclaimer;
910389SAndreas.Sandberg@ARM.com * redistributions in binary form must reproduce the above copyright
1010389SAndreas.Sandberg@ARM.com * notice, this list of conditions and the following disclaimer in the
1110389SAndreas.Sandberg@ARM.com * documentation and/or other materials provided with the distribution;
1210389SAndreas.Sandberg@ARM.com * neither the name of the copyright holders nor the names of its
1310389SAndreas.Sandberg@ARM.com * contributors may be used to endorse or promote products derived from
1410389SAndreas.Sandberg@ARM.com * this software without specific prior written permission.
1510389SAndreas.Sandberg@ARM.com *
1610389SAndreas.Sandberg@ARM.com * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
1710389SAndreas.Sandberg@ARM.com * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
1810389SAndreas.Sandberg@ARM.com * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
1910389SAndreas.Sandberg@ARM.com * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
2010389SAndreas.Sandberg@ARM.com * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2110389SAndreas.Sandberg@ARM.com * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
2210389SAndreas.Sandberg@ARM.com * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2310389SAndreas.Sandberg@ARM.com * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2410389SAndreas.Sandberg@ARM.com * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2510389SAndreas.Sandberg@ARM.com * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2610389SAndreas.Sandberg@ARM.com * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2710389SAndreas.Sandberg@ARM.com *
2810389SAndreas.Sandberg@ARM.com * Authors: Nathan Binkert
2910389SAndreas.Sandberg@ARM.com */
3010389SAndreas.Sandberg@ARM.com
3110389SAndreas.Sandberg@ARM.com/*
3210389SAndreas.Sandberg@ARM.com * Copyright (c) 1990, 1993
3310389SAndreas.Sandberg@ARM.com *	The Regents of the University of California.  All rights reserved.
3410389SAndreas.Sandberg@ARM.com *
3510389SAndreas.Sandberg@ARM.com * This software was developed by the Computer Systems Engineering group
3610389SAndreas.Sandberg@ARM.com * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
3710389SAndreas.Sandberg@ARM.com * contributed to Berkeley.
3810389SAndreas.Sandberg@ARM.com *
3910389SAndreas.Sandberg@ARM.com * All advertising materials mentioning features or use of this software
4011793Sbrandon.potter@amd.com * must display the following acknowledgement:
4111793Sbrandon.potter@amd.com *	This product includes software developed by the University of
4210389SAndreas.Sandberg@ARM.com *	California, Lawrence Berkeley Laboratories.
4310389SAndreas.Sandberg@ARM.com *
4410389SAndreas.Sandberg@ARM.com * Redistribution and use in source and binary forms, with or without
4510389SAndreas.Sandberg@ARM.com * modification, are permitted provided that the following conditions
4610389SAndreas.Sandberg@ARM.com * are met:
4710389SAndreas.Sandberg@ARM.com * 1. Redistributions of source code must retain the above copyright
4810389SAndreas.Sandberg@ARM.com *    notice, this list of conditions and the following disclaimer.
4910389SAndreas.Sandberg@ARM.com * 2. Redistributions in binary form must reproduce the above copyright
5012237Sandreas.sandberg@arm.com *    notice, this list of conditions and the following disclaimer in the
5110389SAndreas.Sandberg@ARM.com *    documentation and/or other materials provided with the distribution.
5210389SAndreas.Sandberg@ARM.com * 3. All advertising materials mentioning features or use of this software
5310389SAndreas.Sandberg@ARM.com *    must display the following acknowledgement:
5410389SAndreas.Sandberg@ARM.com *	This product includes software developed by the University of
5510389SAndreas.Sandberg@ARM.com *	California, Berkeley and its contributors.
5610389SAndreas.Sandberg@ARM.com * 4. Neither the name of the University nor the names of its contributors
5710389SAndreas.Sandberg@ARM.com *    may be used to endorse or promote products derived from this software
5812237Sandreas.sandberg@arm.com *    without specific prior written permission.
5910389SAndreas.Sandberg@ARM.com *
6010389SAndreas.Sandberg@ARM.com * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
6110389SAndreas.Sandberg@ARM.com * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
6210389SAndreas.Sandberg@ARM.com * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
6310389SAndreas.Sandberg@ARM.com * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
6410389SAndreas.Sandberg@ARM.com * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
6510389SAndreas.Sandberg@ARM.com * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
6610389SAndreas.Sandberg@ARM.com * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
6710389SAndreas.Sandberg@ARM.com * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
6810389SAndreas.Sandberg@ARM.com * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
6910389SAndreas.Sandberg@ARM.com * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
7010389SAndreas.Sandberg@ARM.com * SUCH DAMAGE.
7110389SAndreas.Sandberg@ARM.com *
7210389SAndreas.Sandberg@ARM.com *	@(#)kgdb_stub.c	8.4 (Berkeley) 1/12/94
7310389SAndreas.Sandberg@ARM.com */
7410389SAndreas.Sandberg@ARM.com
7510389SAndreas.Sandberg@ARM.com/*-
7610389SAndreas.Sandberg@ARM.com * Copyright (c) 2001 The NetBSD Foundation, Inc.
7710389SAndreas.Sandberg@ARM.com * All rights reserved.
7810389SAndreas.Sandberg@ARM.com *
7910389SAndreas.Sandberg@ARM.com * This code is derived from software contributed to The NetBSD Foundation
8010389SAndreas.Sandberg@ARM.com * by Jason R. Thorpe.
8110389SAndreas.Sandberg@ARM.com *
8210389SAndreas.Sandberg@ARM.com * Redistribution and use in source and binary forms, with or without
8310389SAndreas.Sandberg@ARM.com * modification, are permitted provided that the following conditions
8412237Sandreas.sandberg@arm.com * are met:
8510389SAndreas.Sandberg@ARM.com * 1. Redistributions of source code must retain the above copyright
8610389SAndreas.Sandberg@ARM.com *    notice, this list of conditions and the following disclaimer.
8712237Sandreas.sandberg@arm.com * 2. Redistributions in binary form must reproduce the above copyright
8812237Sandreas.sandberg@arm.com *    notice, this list of conditions and the following disclaimer in the
8910389SAndreas.Sandberg@ARM.com *    documentation and/or other materials provided with the distribution.
9010389SAndreas.Sandberg@ARM.com * 3. All advertising materials mentioning features or use of this software
9110389SAndreas.Sandberg@ARM.com *    must display the following acknowledgement:
9210389SAndreas.Sandberg@ARM.com *	This product includes software developed by the NetBSD
9310389SAndreas.Sandberg@ARM.com *	Foundation, Inc. and its contributors.
9410389SAndreas.Sandberg@ARM.com * 4. Neither the name of The NetBSD Foundation nor the names of its
9510389SAndreas.Sandberg@ARM.com *    contributors may be used to endorse or promote products derived
9610389SAndreas.Sandberg@ARM.com *    from this software without specific prior written permission.
9710389SAndreas.Sandberg@ARM.com *
9810389SAndreas.Sandberg@ARM.com * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
9910389SAndreas.Sandberg@ARM.com * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
10010389SAndreas.Sandberg@ARM.com * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
10110389SAndreas.Sandberg@ARM.com * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
10210389SAndreas.Sandberg@ARM.com * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
10310389SAndreas.Sandberg@ARM.com * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
10410389SAndreas.Sandberg@ARM.com * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
10510389SAndreas.Sandberg@ARM.com * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
10610389SAndreas.Sandberg@ARM.com * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
10710389SAndreas.Sandberg@ARM.com * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
10810389SAndreas.Sandberg@ARM.com * POSSIBILITY OF SUCH DAMAGE.
10910389SAndreas.Sandberg@ARM.com */
11010389SAndreas.Sandberg@ARM.com
11112237Sandreas.sandberg@arm.com/*
11210389SAndreas.Sandberg@ARM.com * $NetBSD: kgdb_stub.c,v 1.8 2001/07/07 22:58:00 wdk Exp $
11310389SAndreas.Sandberg@ARM.com *
11410389SAndreas.Sandberg@ARM.com * Taken from NetBSD
11510389SAndreas.Sandberg@ARM.com *
11610389SAndreas.Sandberg@ARM.com * "Stub" to allow remote cpu to debug over a serial line using gdb.
11710389SAndreas.Sandberg@ARM.com */
11810389SAndreas.Sandberg@ARM.com
11910389SAndreas.Sandberg@ARM.com#include <sys/signal.h>
12010389SAndreas.Sandberg@ARM.com
12110389SAndreas.Sandberg@ARM.com#include <string>
12210389SAndreas.Sandberg@ARM.com#include <unistd.h>
123
124#include "arch/vtophys.hh"
125#include "arch/alpha/remote_gdb.hh"
126#include "base/intmath.hh"
127#include "base/remote_gdb.hh"
128#include "base/socket.hh"
129#include "base/trace.hh"
130#include "config/full_system.hh"
131#include "cpu/thread_context.hh"
132#include "cpu/static_inst.hh"
133#include "mem/physical.hh"
134#include "mem/port.hh"
135#include "sim/system.hh"
136
137using namespace std;
138using namespace AlphaISA;
139
140RemoteGDB::Event::Event(RemoteGDB *g, int fd, int e)
141    : PollEvent(fd, e), gdb(g)
142{}
143
144void
145RemoteGDB::Event::process(int revent)
146{
147    if (revent & POLLIN)
148        gdb->trap(ALPHA_KENTRY_IF);
149    else if (revent & POLLNVAL)
150        gdb->detach();
151}
152
153RemoteGDB::RemoteGDB(System *_system, ThreadContext *c)
154    : BaseRemoteGDB(_system, c, KGDB_NUMREGS),
155      event(NULL)
156{}
157
158RemoteGDB::~RemoteGDB()
159{
160    if (event)
161        delete event;
162}
163
164///////////////////////////////////////////////////////////
165// RemoteGDB::acc
166//
167//	Determine if the mapping at va..(va+len) is valid.
168//
169bool
170RemoteGDB::acc(Addr va, size_t len)
171{
172    Addr last_va;
173
174    va = TheISA::TruncPage(va);
175    last_va = TheISA::RoundPage(va + len);
176
177    do  {
178        if (TheISA::IsK0Seg(va)) {
179            if (va < (TheISA::K0SegBase + pmem->size())) {
180                DPRINTF(GDBAcc, "acc:   Mapping is valid  K0SEG <= "
181                        "%#x < K0SEG + size\n", va);
182                return true;
183            } else {
184                DPRINTF(GDBAcc, "acc:   Mapping invalid %#x > K0SEG + size\n",
185                        va);
186                return false;
187            }
188        }
189
190    /**
191     * This code says that all accesses to palcode (instruction and data)
192     * are valid since there isn't a va->pa mapping because palcode is
193     * accessed physically. At some point this should probably be cleaned up
194     * but there is no easy way to do it.
195     */
196
197        if (AlphaISA::PcPAL(va) || va < 0x10000)
198            return true;
199
200        Addr ptbr = context->readMiscReg(AlphaISA::IPR_PALtemp20);
201        TheISA::PageTableEntry pte = TheISA::kernel_pte_lookup(context->getPhysPort(), ptbr, va);
202        if (!pte.valid()) {
203            DPRINTF(GDBAcc, "acc:   %#x pte is invalid\n", va);
204            return false;
205        }
206        va += TheISA::PageBytes;
207    } while (va < last_va);
208
209    DPRINTF(GDBAcc, "acc:   %#x mapping is valid\n", va);
210    return true;
211}
212
213///////////////////////////////////////////////////////////
214// RemoteGDB::getregs
215//
216//	Translate the kernel debugger register format into
217//	the GDB register format.
218void
219RemoteGDB::getregs()
220{
221    memset(gdbregs.regs, 0, gdbregs.size);
222
223    gdbregs.regs[KGDB_REG_PC] = context->readPC();
224
225    // @todo: Currently this is very Alpha specific.
226    if (AlphaISA::PcPAL(gdbregs.regs[KGDB_REG_PC])) {
227        for (int i = 0; i < TheISA::NumIntArchRegs; ++i) {
228            gdbregs.regs[i] = context->readIntReg(AlphaISA::reg_redir[i]);
229        }
230    } else {
231        for (int i = 0; i < TheISA::NumIntArchRegs; ++i) {
232            gdbregs.regs[i] = context->readIntReg(i);
233        }
234    }
235
236#ifdef KGDB_FP_REGS
237    for (int i = 0; i < TheISA::NumFloatArchRegs; ++i) {
238        gdbregs.regs[i + KGDB_REG_F0] = context->readFloatRegBits(i);
239    }
240#endif
241}
242
243///////////////////////////////////////////////////////////
244// RemoteGDB::setregs
245//
246//	Translate the GDB register format into the kernel
247//	debugger register format.
248//
249void
250RemoteGDB::setregs()
251{
252    // @todo: Currently this is very Alpha specific.
253    if (AlphaISA::PcPAL(gdbregs.regs[KGDB_REG_PC])) {
254        for (int i = 0; i < TheISA::NumIntArchRegs; ++i) {
255            context->setIntReg(AlphaISA::reg_redir[i], gdbregs.regs[i]);
256        }
257    } else {
258        for (int i = 0; i < TheISA::NumIntArchRegs; ++i) {
259            context->setIntReg(i, gdbregs.regs[i]);
260        }
261    }
262
263#ifdef KGDB_FP_REGS
264    for (int i = 0; i < TheISA::NumFloatArchRegs; ++i) {
265        context->setFloatRegBits(i, gdbregs.regs[i + KGDB_REG_F0]);
266    }
267#endif
268    context->setPC(gdbregs.regs[KGDB_REG_PC]);
269}
270
271void
272RemoteGDB::setTempBreakpoint(TempBreakpoint &bkpt, Addr addr)
273{
274    DPRINTF(GDBMisc, "setTempBreakpoint: addr=%#x\n", addr);
275
276    bkpt.address = addr;
277    insertHardBreak(addr, 4);
278}
279
280void
281RemoteGDB::clearTempBreakpoint(TempBreakpoint &bkpt)
282{
283    DPRINTF(GDBMisc, "setTempBreakpoint: addr=%#x\n",
284            bkpt.address);
285
286
287    removeHardBreak(bkpt.address, 4);
288    bkpt.address = 0;
289}
290
291void
292RemoteGDB::clearSingleStep()
293{
294    DPRINTF(GDBMisc, "clearSingleStep bt_addr=%#x nt_addr=%#x\n",
295            takenBkpt.address, notTakenBkpt.address);
296
297    if (takenBkpt.address != 0)
298        clearTempBreakpoint(takenBkpt);
299
300    if (notTakenBkpt.address != 0)
301        clearTempBreakpoint(notTakenBkpt);
302}
303
304void
305RemoteGDB::setSingleStep()
306{
307    Addr pc = context->readPC();
308    Addr npc, bpc;
309    bool set_bt = false;
310
311    npc = pc + sizeof(MachInst);
312
313    // User was stopped at pc, e.g. the instruction at pc was not
314    // executed.
315    MachInst inst = read<MachInst>(pc);
316    StaticInstPtr si(inst);
317    if (si->hasBranchTarget(pc, context, bpc)) {
318        // Don't bother setting a breakpoint on the taken branch if it
319        // is the same as the next pc
320        if (bpc != npc)
321            set_bt = true;
322    }
323
324    DPRINTF(GDBMisc, "setSingleStep bt_addr=%#x nt_addr=%#x\n",
325            takenBkpt.address, notTakenBkpt.address);
326
327    setTempBreakpoint(notTakenBkpt, npc);
328
329    if (set_bt)
330        setTempBreakpoint(takenBkpt, bpc);
331}
332
333// Write bytes to kernel address space for debugger.
334bool
335RemoteGDB::write(Addr vaddr, size_t size, const char *data)
336{
337    if (BaseRemoteGDB::write(vaddr, size, data)) {
338#ifdef IMB
339        alpha_pal_imb();
340#endif
341        return true;
342    } else {
343        return false;
344    }
345}
346
347
348PCEventQueue *RemoteGDB::getPcEventQueue()
349{
350    return &system->pcEventQueue;
351}
352
353
354RemoteGDB::HardBreakpoint::HardBreakpoint(RemoteGDB *_gdb, Addr pc)
355    : PCEvent(_gdb->getPcEventQueue(), "HardBreakpoint Event", pc),
356      gdb(_gdb), refcount(0)
357{
358    DPRINTF(GDBMisc, "creating hardware breakpoint at %#x\n", evpc);
359}
360
361void
362RemoteGDB::HardBreakpoint::process(ThreadContext *tc)
363{
364    DPRINTF(GDBMisc, "handling hardware breakpoint at %#x\n", pc());
365
366    if (tc == gdb->context)
367        gdb->trap(ALPHA_KENTRY_INT);
368}
369
370bool
371RemoteGDB::insertSoftBreak(Addr addr, size_t len)
372{
373    if (len != sizeof(MachInst))
374        panic("invalid length\n");
375
376    return insertHardBreak(addr, len);
377}
378
379bool
380RemoteGDB::removeSoftBreak(Addr addr, size_t len)
381{
382    if (len != sizeof(MachInst))
383        panic("invalid length\n");
384
385    return removeHardBreak(addr, len);
386}
387
388bool
389RemoteGDB::insertHardBreak(Addr addr, size_t len)
390{
391    if (len != sizeof(MachInst))
392        panic("invalid length\n");
393
394    DPRINTF(GDBMisc, "inserting hardware breakpoint at %#x\n", addr);
395
396    HardBreakpoint *&bkpt = hardBreakMap[addr];
397    if (bkpt == 0)
398        bkpt = new HardBreakpoint(this, addr);
399
400    bkpt->refcount++;
401
402    return true;
403}
404
405bool
406RemoteGDB::removeHardBreak(Addr addr, size_t len)
407{
408    if (len != sizeof(MachInst))
409        panic("invalid length\n");
410
411    DPRINTF(GDBMisc, "removing hardware breakpoint at %#x\n", addr);
412
413    break_iter_t i = hardBreakMap.find(addr);
414    if (i == hardBreakMap.end())
415        return false;
416
417    HardBreakpoint *hbp = (*i).second;
418    if (--hbp->refcount == 0) {
419        delete hbp;
420        hardBreakMap.erase(i);
421    }
422
423    return true;
424}
425