remote_gdb.cc revision 3550:515e876568b4
112391Sjason@lowepower.com/*
214299Sbbruce@ucdavis.edu * Copyright (c) 2002-2005 The Regents of The University of Michigan
314299Sbbruce@ucdavis.edu * All rights reserved.
414299Sbbruce@ucdavis.edu *
514299Sbbruce@ucdavis.edu * Redistribution and use in source and binary forms, with or without
614299Sbbruce@ucdavis.edu * modification, are permitted provided that the following conditions are
714299Sbbruce@ucdavis.edu * met: redistributions of source code must retain the above copyright
814299Sbbruce@ucdavis.edu * notice, this list of conditions and the following disclaimer;
912391Sjason@lowepower.com * redistributions in binary form must reproduce the above copyright
1012391Sjason@lowepower.com * notice, this list of conditions and the following disclaimer in the
1112391Sjason@lowepower.com * documentation and/or other materials provided with the distribution;
1212391Sjason@lowepower.com * neither the name of the copyright holders nor the names of its
1312391Sjason@lowepower.com * contributors may be used to endorse or promote products derived from
1412391Sjason@lowepower.com * this software without specific prior written permission.
1512391Sjason@lowepower.com *
1612391Sjason@lowepower.com * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
1712391Sjason@lowepower.com * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
1812391Sjason@lowepower.com * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
1912391Sjason@lowepower.com * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
2012391Sjason@lowepower.com * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2112391Sjason@lowepower.com * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
2212391Sjason@lowepower.com * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2312391Sjason@lowepower.com * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2412391Sjason@lowepower.com * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2512391Sjason@lowepower.com * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2612391Sjason@lowepower.com * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2712391Sjason@lowepower.com *
2812391Sjason@lowepower.com * Authors: Nathan Binkert
2912391Sjason@lowepower.com */
3012391Sjason@lowepower.com
3112391Sjason@lowepower.com/*
3212391Sjason@lowepower.com * Copyright (c) 1990, 1993
3312391Sjason@lowepower.com *	The Regents of the University of California.  All rights reserved.
3412391Sjason@lowepower.com *
3512391Sjason@lowepower.com * This software was developed by the Computer Systems Engineering group
3612391Sjason@lowepower.com * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
3712391Sjason@lowepower.com * contributed to Berkeley.
3812391Sjason@lowepower.com *
3912391Sjason@lowepower.com * All advertising materials mentioning features or use of this software
4012391Sjason@lowepower.com * must display the following acknowledgement:
4112391Sjason@lowepower.com *	This product includes software developed by the University of
4212391Sjason@lowepower.com *	California, Lawrence Berkeley Laboratories.
4312391Sjason@lowepower.com *
4412391Sjason@lowepower.com * Redistribution and use in source and binary forms, with or without
4512391Sjason@lowepower.com * modification, are permitted provided that the following conditions
4612391Sjason@lowepower.com * are met:
4712391Sjason@lowepower.com * 1. Redistributions of source code must retain the above copyright
4812391Sjason@lowepower.com *    notice, this list of conditions and the following disclaimer.
4912391Sjason@lowepower.com * 2. Redistributions in binary form must reproduce the above copyright
5012391Sjason@lowepower.com *    notice, this list of conditions and the following disclaimer in the
5112391Sjason@lowepower.com *    documentation and/or other materials provided with the distribution.
5212391Sjason@lowepower.com * 3. All advertising materials mentioning features or use of this software
5312391Sjason@lowepower.com *    must display the following acknowledgement:
5412391Sjason@lowepower.com *	This product includes software developed by the University of
5512391Sjason@lowepower.com *	California, Berkeley and its contributors.
5612391Sjason@lowepower.com * 4. Neither the name of the University nor the names of its contributors
5712391Sjason@lowepower.com *    may be used to endorse or promote products derived from this software
5812391Sjason@lowepower.com *    without specific prior written permission.
5912391Sjason@lowepower.com *
6012391Sjason@lowepower.com * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
6112391Sjason@lowepower.com * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
6212391Sjason@lowepower.com * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
6312391Sjason@lowepower.com * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
6412391Sjason@lowepower.com * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
6512391Sjason@lowepower.com * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
6612391Sjason@lowepower.com * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
6712391Sjason@lowepower.com * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
6812391Sjason@lowepower.com * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
6912391Sjason@lowepower.com * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
7012391Sjason@lowepower.com * SUCH DAMAGE.
7112391Sjason@lowepower.com *
7212391Sjason@lowepower.com *	@(#)kgdb_stub.c	8.4 (Berkeley) 1/12/94
7312391Sjason@lowepower.com */
7412391Sjason@lowepower.com
7512391Sjason@lowepower.com/*-
7612391Sjason@lowepower.com * Copyright (c) 2001 The NetBSD Foundation, Inc.
7712391Sjason@lowepower.com * All rights reserved.
7812391Sjason@lowepower.com *
7912391Sjason@lowepower.com * This code is derived from software contributed to The NetBSD Foundation
8012391Sjason@lowepower.com * by Jason R. Thorpe.
8112391Sjason@lowepower.com *
8212391Sjason@lowepower.com * Redistribution and use in source and binary forms, with or without
8312391Sjason@lowepower.com * modification, are permitted provided that the following conditions
8412391Sjason@lowepower.com * are met:
8512391Sjason@lowepower.com * 1. Redistributions of source code must retain the above copyright
8612391Sjason@lowepower.com *    notice, this list of conditions and the following disclaimer.
8712391Sjason@lowepower.com * 2. Redistributions in binary form must reproduce the above copyright
8812391Sjason@lowepower.com *    notice, this list of conditions and the following disclaimer in the
8912391Sjason@lowepower.com *    documentation and/or other materials provided with the distribution.
9012391Sjason@lowepower.com * 3. All advertising materials mentioning features or use of this software
9112391Sjason@lowepower.com *    must display the following acknowledgement:
9212391Sjason@lowepower.com *	This product includes software developed by the NetBSD
9312391Sjason@lowepower.com *	Foundation, Inc. and its contributors.
9412391Sjason@lowepower.com * 4. Neither the name of The NetBSD Foundation nor the names of its
9512391Sjason@lowepower.com *    contributors may be used to endorse or promote products derived
9612391Sjason@lowepower.com *    from this software without specific prior written permission.
9712391Sjason@lowepower.com *
9812391Sjason@lowepower.com * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
9912391Sjason@lowepower.com * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
10012391Sjason@lowepower.com * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
10112391Sjason@lowepower.com * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
10212391Sjason@lowepower.com * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
10312391Sjason@lowepower.com * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
10414299Sbbruce@ucdavis.edu * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
10514299Sbbruce@ucdavis.edu * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
10612391Sjason@lowepower.com * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
10712391Sjason@lowepower.com * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
10812391Sjason@lowepower.com * POSSIBILITY OF SUCH DAMAGE.
10912391Sjason@lowepower.com */
11012391Sjason@lowepower.com
11112391Sjason@lowepower.com/*
11212391Sjason@lowepower.com * $NetBSD: kgdb_stub.c,v 1.8 2001/07/07 22:58:00 wdk Exp $
11314299Sbbruce@ucdavis.edu *
11414299Sbbruce@ucdavis.edu * Taken from NetBSD
11514299Sbbruce@ucdavis.edu *
11614299Sbbruce@ucdavis.edu * "Stub" to allow remote cpu to debug over a serial line using gdb.
11714299Sbbruce@ucdavis.edu */
11812391Sjason@lowepower.com
11912391Sjason@lowepower.com#include <sys/signal.h>
12012391Sjason@lowepower.com
12112391Sjason@lowepower.com#include <string>
12212391Sjason@lowepower.com#include <unistd.h>
12312391Sjason@lowepower.com
12412391Sjason@lowepower.com#include "arch/alpha/kgdb.h"
12512391Sjason@lowepower.com#include "arch/alpha/remote_gdb.hh"
12612391Sjason@lowepower.com#include "arch/vtophys.hh"
12712391Sjason@lowepower.com#include "base/intmath.hh"
12812391Sjason@lowepower.com#include "base/remote_gdb.hh"
12912391Sjason@lowepower.com#include "base/socket.hh"
13012391Sjason@lowepower.com#include "base/trace.hh"
13112391Sjason@lowepower.com#include "config/full_system.hh"
13214299Sbbruce@ucdavis.edu#include "cpu/thread_context.hh"
13314299Sbbruce@ucdavis.edu#include "cpu/static_inst.hh"
13412391Sjason@lowepower.com#include "mem/physical.hh"
13512391Sjason@lowepower.com#include "mem/port.hh"
13612391Sjason@lowepower.com#include "sim/system.hh"
13712391Sjason@lowepower.com
13812391Sjason@lowepower.comusing namespace std;
13912391Sjason@lowepower.comusing namespace TheISA;
14012391Sjason@lowepower.com
14112391Sjason@lowepower.comRemoteGDB::RemoteGDB(System *_system, ThreadContext *c)
14212391Sjason@lowepower.com    : BaseRemoteGDB(_system, c, KGDB_NUMREGS)
14312391Sjason@lowepower.com{
14412391Sjason@lowepower.com    memset(gdbregs.regs, 0, gdbregs.size);
14512391Sjason@lowepower.com}
14612391Sjason@lowepower.com
14712391Sjason@lowepower.com///////////////////////////////////////////////////////////
14812391Sjason@lowepower.com// RemoteGDB::acc
14912391Sjason@lowepower.com//
15012391Sjason@lowepower.com//	Determine if the mapping at va..(va+len) is valid.
15112391Sjason@lowepower.com//
15212391Sjason@lowepower.combool
15312391Sjason@lowepower.comRemoteGDB::acc(Addr va, size_t len)
15412391Sjason@lowepower.com{
15512391Sjason@lowepower.com    Addr last_va;
15612391Sjason@lowepower.com
15712391Sjason@lowepower.com    va = TheISA::TruncPage(va);
15812391Sjason@lowepower.com    last_va = TheISA::RoundPage(va + len);
15912391Sjason@lowepower.com
16012391Sjason@lowepower.com    do  {
16112391Sjason@lowepower.com        if (TheISA::IsK0Seg(va)) {
16212391Sjason@lowepower.com            if (va < (TheISA::K0SegBase + pmem->size())) {
16312391Sjason@lowepower.com                DPRINTF(GDBAcc, "acc:   Mapping is valid  K0SEG <= "
16412391Sjason@lowepower.com                        "%#x < K0SEG + size\n", va);
16512391Sjason@lowepower.com                return true;
16612391Sjason@lowepower.com            } else {
16712391Sjason@lowepower.com                DPRINTF(GDBAcc, "acc:   Mapping invalid %#x > K0SEG + size\n",
16812391Sjason@lowepower.com                        va);
16912391Sjason@lowepower.com                return false;
17012391Sjason@lowepower.com            }
17112391Sjason@lowepower.com        }
17212391Sjason@lowepower.com
17312391Sjason@lowepower.com    /**
17412391Sjason@lowepower.com     * This code says that all accesses to palcode (instruction and data)
17512391Sjason@lowepower.com     * are valid since there isn't a va->pa mapping because palcode is
17612391Sjason@lowepower.com     * accessed physically. At some point this should probably be cleaned up
17712391Sjason@lowepower.com     * but there is no easy way to do it.
17812391Sjason@lowepower.com     */
17912391Sjason@lowepower.com
18012391Sjason@lowepower.com        if (AlphaISA::PcPAL(va) || va < 0x10000)
18112391Sjason@lowepower.com            return true;
18212391Sjason@lowepower.com
18312391Sjason@lowepower.com        Addr ptbr = context->readMiscReg(AlphaISA::IPR_PALtemp20);
18412391Sjason@lowepower.com        TheISA::PageTableEntry pte = TheISA::kernel_pte_lookup(context->getPhysPort(), ptbr, va);
18512391Sjason@lowepower.com        if (!pte.valid()) {
18612391Sjason@lowepower.com            DPRINTF(GDBAcc, "acc:   %#x pte is invalid\n", va);
18712391Sjason@lowepower.com            return false;
18812391Sjason@lowepower.com        }
18912391Sjason@lowepower.com        va += TheISA::PageBytes;
19012391Sjason@lowepower.com    } while (va < last_va);
19112391Sjason@lowepower.com
19212391Sjason@lowepower.com    DPRINTF(GDBAcc, "acc:   %#x mapping is valid\n", va);
19312391Sjason@lowepower.com    return true;
19412391Sjason@lowepower.com}
19512391Sjason@lowepower.com
19612391Sjason@lowepower.com///////////////////////////////////////////////////////////
19712391Sjason@lowepower.com// RemoteGDB::getregs
19812391Sjason@lowepower.com//
19912391Sjason@lowepower.com//	Translate the kernel debugger register format into
20012391Sjason@lowepower.com//	the GDB register format.
20112391Sjason@lowepower.comvoid
20212391Sjason@lowepower.comRemoteGDB::getregs()
20312391Sjason@lowepower.com{
20412391Sjason@lowepower.com    memset(gdbregs.regs, 0, gdbregs.size);
20512391Sjason@lowepower.com
20612391Sjason@lowepower.com    gdbregs.regs[KGDB_REG_PC] = context->readPC();
20712391Sjason@lowepower.com
20812391Sjason@lowepower.com    // @todo: Currently this is very Alpha specific.
20912391Sjason@lowepower.com    if (AlphaISA::PcPAL(gdbregs.regs[KGDB_REG_PC])) {
21012391Sjason@lowepower.com        for (int i = 0; i < TheISA::NumIntArchRegs; ++i) {
21112391Sjason@lowepower.com            gdbregs.regs[i] = context->readIntReg(AlphaISA::reg_redir[i]);
21212391Sjason@lowepower.com        }
21312391Sjason@lowepower.com    } else {
21412391Sjason@lowepower.com        for (int i = 0; i < TheISA::NumIntArchRegs; ++i) {
21512391Sjason@lowepower.com            gdbregs.regs[i] = context->readIntReg(i);
21612391Sjason@lowepower.com        }
21712391Sjason@lowepower.com    }
21812391Sjason@lowepower.com
21912391Sjason@lowepower.com#ifdef KGDB_FP_REGS
22012391Sjason@lowepower.com    for (int i = 0; i < TheISA::NumFloatArchRegs; ++i) {
22112391Sjason@lowepower.com        gdbregs.regs[i + KGDB_REG_F0] = context->readFloatRegBits(i);
22212391Sjason@lowepower.com    }
22312391Sjason@lowepower.com#endif
22412391Sjason@lowepower.com}
22512391Sjason@lowepower.com
22612391Sjason@lowepower.com///////////////////////////////////////////////////////////
22712391Sjason@lowepower.com// RemoteGDB::setregs
22812391Sjason@lowepower.com//
22912391Sjason@lowepower.com//	Translate the GDB register format into the kernel
23012391Sjason@lowepower.com//	debugger register format.
23112391Sjason@lowepower.com//
23212391Sjason@lowepower.comvoid
23312391Sjason@lowepower.comRemoteGDB::setregs()
23412391Sjason@lowepower.com{
23512391Sjason@lowepower.com    // @todo: Currently this is very Alpha specific.
23612391Sjason@lowepower.com    if (AlphaISA::PcPAL(gdbregs.regs[KGDB_REG_PC])) {
23712391Sjason@lowepower.com        for (int i = 0; i < TheISA::NumIntArchRegs; ++i) {
23812391Sjason@lowepower.com            context->setIntReg(AlphaISA::reg_redir[i], gdbregs.regs[i]);
23912391Sjason@lowepower.com        }
24012391Sjason@lowepower.com    } else {
24112391Sjason@lowepower.com        for (int i = 0; i < TheISA::NumIntArchRegs; ++i) {
24212391Sjason@lowepower.com            context->setIntReg(i, gdbregs.regs[i]);
24312391Sjason@lowepower.com        }
24412391Sjason@lowepower.com    }
24512391Sjason@lowepower.com
24612391Sjason@lowepower.com#ifdef KGDB_FP_REGS
24712391Sjason@lowepower.com    for (int i = 0; i < TheISA::NumFloatArchRegs; ++i) {
24812391Sjason@lowepower.com        context->setFloatRegBits(i, gdbregs.regs[i + KGDB_REG_F0]);
24912391Sjason@lowepower.com    }
25012391Sjason@lowepower.com#endif
25112391Sjason@lowepower.com    context->setPC(gdbregs.regs[KGDB_REG_PC]);
25212391Sjason@lowepower.com}
25312391Sjason@lowepower.com
25412391Sjason@lowepower.comvoid
25512391Sjason@lowepower.comRemoteGDB::clearSingleStep()
25612391Sjason@lowepower.com{
25712391Sjason@lowepower.com    DPRINTF(GDBMisc, "clearSingleStep bt_addr=%#x nt_addr=%#x\n",
25812391Sjason@lowepower.com            takenBkpt, notTakenBkpt);
25912391Sjason@lowepower.com
26012391Sjason@lowepower.com    if (takenBkpt != 0)
26112391Sjason@lowepower.com        clearTempBreakpoint(takenBkpt);
26212391Sjason@lowepower.com
26312391Sjason@lowepower.com    if (notTakenBkpt != 0)
26412391Sjason@lowepower.com        clearTempBreakpoint(notTakenBkpt);
26512391Sjason@lowepower.com}
26612391Sjason@lowepower.com
26712391Sjason@lowepower.comvoid
26812391Sjason@lowepower.comRemoteGDB::setSingleStep()
26912391Sjason@lowepower.com{
27012391Sjason@lowepower.com    Addr pc = context->readPC();
27112391Sjason@lowepower.com    Addr npc, bpc;
27212391Sjason@lowepower.com    bool set_bt = false;
27312391Sjason@lowepower.com
27412391Sjason@lowepower.com    npc = pc + sizeof(MachInst);
27512391Sjason@lowepower.com
27612391Sjason@lowepower.com    // User was stopped at pc, e.g. the instruction at pc was not
27712391Sjason@lowepower.com    // executed.
27812391Sjason@lowepower.com    MachInst inst = read<MachInst>(pc);
27912391Sjason@lowepower.com    StaticInstPtr si(inst);
28012391Sjason@lowepower.com    if (si->hasBranchTarget(pc, context, bpc)) {
28112391Sjason@lowepower.com        // Don't bother setting a breakpoint on the taken branch if it
28212391Sjason@lowepower.com        // is the same as the next pc
28312391Sjason@lowepower.com        if (bpc != npc)
28412391Sjason@lowepower.com            set_bt = true;
285    }
286
287    DPRINTF(GDBMisc, "setSingleStep bt_addr=%#x nt_addr=%#x\n",
288            takenBkpt, notTakenBkpt);
289
290    setTempBreakpoint(notTakenBkpt = npc);
291
292    if (set_bt)
293        setTempBreakpoint(takenBkpt = bpc);
294}
295
296// Write bytes to kernel address space for debugger.
297bool
298RemoteGDB::write(Addr vaddr, size_t size, const char *data)
299{
300    if (BaseRemoteGDB::write(vaddr, size, data)) {
301#ifdef IMB
302        alpha_pal_imb();
303#endif
304        return true;
305    } else {
306        return false;
307    }
308}
309
310