remote_gdb.cc revision 5567
1955SN/A/*
2955SN/A * Copyright (c) 2002-2005 The Regents of The University of Michigan
31762SN/A * All rights reserved.
4955SN/A *
5955SN/A * Redistribution and use in source and binary forms, with or without
6955SN/A * modification, are permitted provided that the following conditions are
7955SN/A * met: redistributions of source code must retain the above copyright
8955SN/A * notice, this list of conditions and the following disclaimer;
9955SN/A * redistributions in binary form must reproduce the above copyright
10955SN/A * notice, this list of conditions and the following disclaimer in the
11955SN/A * documentation and/or other materials provided with the distribution;
12955SN/A * neither the name of the copyright holders nor the names of its
13955SN/A * contributors may be used to endorse or promote products derived from
14955SN/A * this software without specific prior written permission.
15955SN/A *
16955SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17955SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18955SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19955SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20955SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21955SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22955SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23955SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24955SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25955SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26955SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27955SN/A *
282665Ssaidi@eecs.umich.edu * Authors: Nathan Binkert
294762Snate@binkert.org */
30955SN/A
315522Snate@binkert.org/*
326143Snate@binkert.org * Copyright (c) 1990, 1993
334762Snate@binkert.org *      The Regents of the University of California.  All rights reserved.
345522Snate@binkert.org *
35955SN/A * This software was developed by the Computer Systems Engineering group
365522Snate@binkert.org * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
3711974Sgabeblack@google.com * contributed to Berkeley.
38955SN/A *
395522Snate@binkert.org * All advertising materials mentioning features or use of this software
404202Sbinkertn@umich.edu * must display the following acknowledgement:
415742Snate@binkert.org *      This product includes software developed by the University of
42955SN/A *      California, Lawrence Berkeley Laboratories.
434381Sbinkertn@umich.edu *
444381Sbinkertn@umich.edu * Redistribution and use in source and binary forms, with or without
4512246Sgabeblack@google.com * modification, are permitted provided that the following conditions
4612246Sgabeblack@google.com * are met:
478334Snate@binkert.org * 1. Redistributions of source code must retain the above copyright
48955SN/A *    notice, this list of conditions and the following disclaimer.
49955SN/A * 2. Redistributions in binary form must reproduce the above copyright
504202Sbinkertn@umich.edu *    notice, this list of conditions and the following disclaimer in the
51955SN/A *    documentation and/or other materials provided with the distribution.
524382Sbinkertn@umich.edu * 3. All advertising materials mentioning features or use of this software
534382Sbinkertn@umich.edu *    must display the following acknowledgement:
544382Sbinkertn@umich.edu *      This product includes software developed by the University of
556654Snate@binkert.org *      California, Berkeley and its contributors.
565517Snate@binkert.org * 4. Neither the name of the University nor the names of its contributors
578614Sgblack@eecs.umich.edu *    may be used to endorse or promote products derived from this software
587674Snate@binkert.org *    without specific prior written permission.
596143Snate@binkert.org *
606143Snate@binkert.org * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
616143Snate@binkert.org * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
6212302Sgabeblack@google.com * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
6312302Sgabeblack@google.com * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
6412302Sgabeblack@google.com * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
6512302Sgabeblack@google.com * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
6612302Sgabeblack@google.com * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
6712302Sgabeblack@google.com * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
6812302Sgabeblack@google.com * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
6912302Sgabeblack@google.com * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
7012302Sgabeblack@google.com * SUCH DAMAGE.
7112302Sgabeblack@google.com *
7212302Sgabeblack@google.com *      @(#)kgdb_stub.c 8.4 (Berkeley) 1/12/94
7312302Sgabeblack@google.com */
7412363Sgabeblack@google.com
7512302Sgabeblack@google.com/*-
7612302Sgabeblack@google.com * Copyright (c) 2001 The NetBSD Foundation, Inc.
7712302Sgabeblack@google.com * All rights reserved.
7812363Sgabeblack@google.com *
7912302Sgabeblack@google.com * This code is derived from software contributed to The NetBSD Foundation
8012302Sgabeblack@google.com * by Jason R. Thorpe.
8112302Sgabeblack@google.com *
8212302Sgabeblack@google.com * Redistribution and use in source and binary forms, with or without
8312302Sgabeblack@google.com * modification, are permitted provided that the following conditions
8412302Sgabeblack@google.com * are met:
8512302Sgabeblack@google.com * 1. Redistributions of source code must retain the above copyright
8612363Sgabeblack@google.com *    notice, this list of conditions and the following disclaimer.
8712302Sgabeblack@google.com * 2. Redistributions in binary form must reproduce the above copyright
8812302Sgabeblack@google.com *    notice, this list of conditions and the following disclaimer in the
8912302Sgabeblack@google.com *    documentation and/or other materials provided with the distribution.
9012302Sgabeblack@google.com * 3. All advertising materials mentioning features or use of this software
9111983Sgabeblack@google.com *    must display the following acknowledgement:
926143Snate@binkert.org *      This product includes software developed by the NetBSD
938233Snate@binkert.org *      Foundation, Inc. and its contributors.
9412302Sgabeblack@google.com * 4. Neither the name of The NetBSD Foundation nor the names of its
956143Snate@binkert.org *    contributors may be used to endorse or promote products derived
966143Snate@binkert.org *    from this software without specific prior written permission.
9712302Sgabeblack@google.com *
984762Snate@binkert.org * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
996143Snate@binkert.org * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
1008233Snate@binkert.org * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
1018233Snate@binkert.org * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
10212302Sgabeblack@google.com * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
10312302Sgabeblack@google.com * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
1046143Snate@binkert.org * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
10512362Sgabeblack@google.com * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
10612362Sgabeblack@google.com * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
10712362Sgabeblack@google.com * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
10812362Sgabeblack@google.com * POSSIBILITY OF SUCH DAMAGE.
10912302Sgabeblack@google.com */
11012302Sgabeblack@google.com
11112302Sgabeblack@google.com/*
11212302Sgabeblack@google.com * $NetBSD: kgdb_stub.c,v 1.8 2001/07/07 22:58:00 wdk Exp $
11312302Sgabeblack@google.com *
11412363Sgabeblack@google.com * Taken from NetBSD
11512363Sgabeblack@google.com *
11612363Sgabeblack@google.com * "Stub" to allow remote cpu to debug over a serial line using gdb.
11712363Sgabeblack@google.com */
11812302Sgabeblack@google.com
11912363Sgabeblack@google.com#include <sys/signal.h>
12012363Sgabeblack@google.com
12112363Sgabeblack@google.com#include <string>
12212363Sgabeblack@google.com#include <unistd.h>
12312363Sgabeblack@google.com
1248233Snate@binkert.org#include "config/full_system.hh"
1256143Snate@binkert.org#if FULL_SYSTEM
1266143Snate@binkert.org#include "arch/alpha/vtophys.hh"
1276143Snate@binkert.org#endif
1286143Snate@binkert.org
1296143Snate@binkert.org#include "arch/alpha/kgdb.h"
1306143Snate@binkert.org#include "arch/alpha/utility.hh"
1316143Snate@binkert.org#include "arch/alpha/remote_gdb.hh"
1326143Snate@binkert.org#include "base/intmath.hh"
1336143Snate@binkert.org#include "base/remote_gdb.hh"
1347065Snate@binkert.org#include "base/socket.hh"
1356143Snate@binkert.org#include "base/trace.hh"
13612362Sgabeblack@google.com#include "cpu/thread_context.hh"
13712362Sgabeblack@google.com#include "cpu/static_inst.hh"
13812362Sgabeblack@google.com#include "mem/physical.hh"
13912362Sgabeblack@google.com#include "mem/port.hh"
14012362Sgabeblack@google.com#include "sim/system.hh"
14112362Sgabeblack@google.com
14212362Sgabeblack@google.comusing namespace std;
14312362Sgabeblack@google.comusing namespace AlphaISA;
14412362Sgabeblack@google.com
14512362Sgabeblack@google.comRemoteGDB::RemoteGDB(System *_system, ThreadContext *c)
14612362Sgabeblack@google.com    : BaseRemoteGDB(_system, c, KGDB_NUMREGS)
14712362Sgabeblack@google.com{
1488233Snate@binkert.org    memset(gdbregs.regs, 0, gdbregs.bytes());
1498233Snate@binkert.org}
1508233Snate@binkert.org
1518233Snate@binkert.org///////////////////////////////////////////////////////////
1528233Snate@binkert.org// RemoteGDB::acc
1538233Snate@binkert.org//
1548233Snate@binkert.org//      Determine if the mapping at va..(va+len) is valid.
1558233Snate@binkert.org//
1568233Snate@binkert.orgbool
1578233Snate@binkert.orgRemoteGDB::acc(Addr va, size_t len)
1588233Snate@binkert.org{
1598233Snate@binkert.org#if !FULL_SYSTEM
1608233Snate@binkert.org    panic("acc function needs to be rewritten for SE mode\n");
1618233Snate@binkert.org#else
1628233Snate@binkert.org    Addr last_va;
1638233Snate@binkert.org
1648233Snate@binkert.org    va = AlphaISA::TruncPage(va);
1658233Snate@binkert.org    last_va = AlphaISA::RoundPage(va + len);
1668233Snate@binkert.org
1678233Snate@binkert.org    do  {
1688233Snate@binkert.org        if (AlphaISA::IsK0Seg(va)) {
1696143Snate@binkert.org            if (va < (AlphaISA::K0SegBase + pmem->size())) {
1706143Snate@binkert.org                DPRINTF(GDBAcc, "acc:   Mapping is valid  K0SEG <= "
1716143Snate@binkert.org                        "%#x < K0SEG + size\n", va);
1726143Snate@binkert.org                return true;
1736143Snate@binkert.org            } else {
1746143Snate@binkert.org                DPRINTF(GDBAcc, "acc:   Mapping invalid %#x > K0SEG + size\n",
1759982Satgutier@umich.edu                        va);
1766143Snate@binkert.org                return false;
17712302Sgabeblack@google.com            }
17812302Sgabeblack@google.com        }
17912302Sgabeblack@google.com
18012302Sgabeblack@google.com    /**
18112302Sgabeblack@google.com     * This code says that all accesses to palcode (instruction and data)
18212302Sgabeblack@google.com     * are valid since there isn't a va->pa mapping because palcode is
18312302Sgabeblack@google.com     * accessed physically. At some point this should probably be cleaned up
18412302Sgabeblack@google.com     * but there is no easy way to do it.
18511983Sgabeblack@google.com     */
18611983Sgabeblack@google.com
18711983Sgabeblack@google.com        if (AlphaISA::PcPAL(va) || va < 0x10000)
18812302Sgabeblack@google.com            return true;
18912302Sgabeblack@google.com
19012302Sgabeblack@google.com        Addr ptbr = context->readMiscRegNoEffect(AlphaISA::IPR_PALtemp20);
19112302Sgabeblack@google.com        AlphaISA::PageTableEntry pte = AlphaISA::kernel_pte_lookup(context->getPhysPort(), ptbr, va);
19212302Sgabeblack@google.com        if (!pte.valid()) {
19312302Sgabeblack@google.com            DPRINTF(GDBAcc, "acc:   %#x pte is invalid\n", va);
19411983Sgabeblack@google.com            return false;
1956143Snate@binkert.org        }
19612305Sgabeblack@google.com        va += AlphaISA::PageBytes;
19712302Sgabeblack@google.com    } while (va < last_va);
19812302Sgabeblack@google.com
19912302Sgabeblack@google.com    DPRINTF(GDBAcc, "acc:   %#x mapping is valid\n", va);
2006143Snate@binkert.org    return true;
2016143Snate@binkert.org#endif
2026143Snate@binkert.org}
2035522Snate@binkert.org
2046143Snate@binkert.org///////////////////////////////////////////////////////////
2056143Snate@binkert.org// RemoteGDB::getregs
2066143Snate@binkert.org//
2079982Satgutier@umich.edu//      Translate the kernel debugger register format into
20812302Sgabeblack@google.com//      the GDB register format.
20912302Sgabeblack@google.comvoid
21012302Sgabeblack@google.comRemoteGDB::getregs()
2116143Snate@binkert.org{
2126143Snate@binkert.org    memset(gdbregs.regs, 0, gdbregs.bytes());
2136143Snate@binkert.org
2146143Snate@binkert.org    gdbregs.regs[KGDB_REG_PC] = context->readPC();
2155522Snate@binkert.org
2165522Snate@binkert.org    // @todo: Currently this is very Alpha specific.
2175522Snate@binkert.org    if (AlphaISA::PcPAL(gdbregs.regs[KGDB_REG_PC])) {
2185522Snate@binkert.org        for (int i = 0; i < AlphaISA::NumIntArchRegs; ++i) {
2195604Snate@binkert.org            gdbregs.regs[i] = context->readIntReg(AlphaISA::reg_redir[i]);
2205604Snate@binkert.org        }
2216143Snate@binkert.org    } else {
2226143Snate@binkert.org        for (int i = 0; i < AlphaISA::NumIntArchRegs; ++i) {
2234762Snate@binkert.org            gdbregs.regs[i] = context->readIntReg(i);
2244762Snate@binkert.org        }
2256143Snate@binkert.org    }
2266727Ssteve.reinhardt@amd.com
2276727Ssteve.reinhardt@amd.com#ifdef KGDB_FP_REGS
2286727Ssteve.reinhardt@amd.com    for (int i = 0; i < AlphaISA::NumFloatArchRegs; ++i) {
2294762Snate@binkert.org        gdbregs.regs[i + KGDB_REG_F0] = context->readFloatRegBits(i);
2306143Snate@binkert.org    }
2316143Snate@binkert.org#endif
2326143Snate@binkert.org}
2336143Snate@binkert.org
2346727Ssteve.reinhardt@amd.com///////////////////////////////////////////////////////////
2356143Snate@binkert.org// RemoteGDB::setregs
2367674Snate@binkert.org//
2377674Snate@binkert.org//      Translate the GDB register format into the kernel
2385604Snate@binkert.org//      debugger register format.
2396143Snate@binkert.org//
2406143Snate@binkert.orgvoid
2416143Snate@binkert.orgRemoteGDB::setregs()
2424762Snate@binkert.org{
2436143Snate@binkert.org    // @todo: Currently this is very Alpha specific.
2444762Snate@binkert.org    if (AlphaISA::PcPAL(gdbregs.regs[KGDB_REG_PC])) {
2454762Snate@binkert.org        for (int i = 0; i < AlphaISA::NumIntArchRegs; ++i) {
2464762Snate@binkert.org            context->setIntReg(AlphaISA::reg_redir[i], gdbregs.regs[i]);
2476143Snate@binkert.org        }
2486143Snate@binkert.org    } else {
2494762Snate@binkert.org        for (int i = 0; i < AlphaISA::NumIntArchRegs; ++i) {
25012302Sgabeblack@google.com            context->setIntReg(i, gdbregs.regs[i]);
25112302Sgabeblack@google.com        }
2528233Snate@binkert.org    }
25312302Sgabeblack@google.com
2546143Snate@binkert.org#ifdef KGDB_FP_REGS
2556143Snate@binkert.org    for (int i = 0; i < AlphaISA::NumFloatArchRegs; ++i) {
2564762Snate@binkert.org        context->setFloatRegBits(i, gdbregs.regs[i + KGDB_REG_F0]);
2576143Snate@binkert.org    }
2584762Snate@binkert.org#endif
2599396Sandreas.hansson@arm.com    context->setPC(gdbregs.regs[KGDB_REG_PC]);
2609396Sandreas.hansson@arm.com}
2619396Sandreas.hansson@arm.com
26212302Sgabeblack@google.comvoid
26312302Sgabeblack@google.comRemoteGDB::clearSingleStep()
26412302Sgabeblack@google.com{
2659396Sandreas.hansson@arm.com    DPRINTF(GDBMisc, "clearSingleStep bt_addr=%#x nt_addr=%#x\n",
2669396Sandreas.hansson@arm.com            takenBkpt, notTakenBkpt);
2679396Sandreas.hansson@arm.com
2689396Sandreas.hansson@arm.com    if (takenBkpt != 0)
2699396Sandreas.hansson@arm.com        clearTempBreakpoint(takenBkpt);
2709396Sandreas.hansson@arm.com
2719396Sandreas.hansson@arm.com    if (notTakenBkpt != 0)
2729930Sandreas.hansson@arm.com        clearTempBreakpoint(notTakenBkpt);
2739930Sandreas.hansson@arm.com}
2749396Sandreas.hansson@arm.com
2758235Snate@binkert.orgvoid
2768235Snate@binkert.orgRemoteGDB::setSingleStep()
2776143Snate@binkert.org{
2788235Snate@binkert.org    Addr pc = context->readPC();
2799003SAli.Saidi@ARM.com    Addr npc, bpc;
2808235Snate@binkert.org    bool set_bt = false;
2818235Snate@binkert.org
28212302Sgabeblack@google.com    npc = pc + sizeof(MachInst);
2838235Snate@binkert.org
28412302Sgabeblack@google.com    // User was stopped at pc, e.g. the instruction at pc was not
2858235Snate@binkert.org    // executed.
2868235Snate@binkert.org    MachInst inst = read<MachInst>(pc);
28712302Sgabeblack@google.com    StaticInstPtr si(inst, pc);
2888235Snate@binkert.org    if (si->hasBranchTarget(pc, context, bpc)) {
2898235Snate@binkert.org        // Don't bother setting a breakpoint on the taken branch if it
2908235Snate@binkert.org        // is the same as the next pc
2918235Snate@binkert.org        if (bpc != npc)
2929003SAli.Saidi@ARM.com            set_bt = true;
29312313Sgabeblack@google.com    }
29412313Sgabeblack@google.com
29512313Sgabeblack@google.com    DPRINTF(GDBMisc, "setSingleStep bt_addr=%#x nt_addr=%#x\n",
29612313Sgabeblack@google.com            takenBkpt, notTakenBkpt);
29712313Sgabeblack@google.com
29812313Sgabeblack@google.com    setTempBreakpoint(notTakenBkpt = npc);
29912315Sgabeblack@google.com
30012315Sgabeblack@google.com    if (set_bt)
30112315Sgabeblack@google.com        setTempBreakpoint(takenBkpt = bpc);
3025584Snate@binkert.org}
3034382Sbinkertn@umich.edu
3044202Sbinkertn@umich.edu// Write bytes to kernel address space for debugger.
3054382Sbinkertn@umich.edubool
3064382Sbinkertn@umich.eduRemoteGDB::write(Addr vaddr, size_t size, const char *data)
3079396Sandreas.hansson@arm.com{
3085584Snate@binkert.org    if (BaseRemoteGDB::write(vaddr, size, data)) {
30912313Sgabeblack@google.com#ifdef IMB
3104382Sbinkertn@umich.edu        alpha_pal_imb();
3114382Sbinkertn@umich.edu#endif
3124382Sbinkertn@umich.edu        return true;
3138232Snate@binkert.org    } else {
3145192Ssaidi@eecs.umich.edu        return false;
3158232Snate@binkert.org    }
3168232Snate@binkert.org}
3178232Snate@binkert.org
3185192Ssaidi@eecs.umich.edu