remote_gdb.cc revision 7720
12914Ssaidi@eecs.umich.edu/* 28856Sandreas.hansson@arm.com * Copyright (c) 2002-2005 The Regents of The University of Michigan 38856Sandreas.hansson@arm.com * All rights reserved. 48856Sandreas.hansson@arm.com * 58856Sandreas.hansson@arm.com * Redistribution and use in source and binary forms, with or without 68856Sandreas.hansson@arm.com * modification, are permitted provided that the following conditions are 78856Sandreas.hansson@arm.com * met: redistributions of source code must retain the above copyright 88856Sandreas.hansson@arm.com * notice, this list of conditions and the following disclaimer; 98856Sandreas.hansson@arm.com * redistributions in binary form must reproduce the above copyright 108856Sandreas.hansson@arm.com * notice, this list of conditions and the following disclaimer in the 118856Sandreas.hansson@arm.com * documentation and/or other materials provided with the distribution; 128856Sandreas.hansson@arm.com * neither the name of the copyright holders nor the names of its 138856Sandreas.hansson@arm.com * contributors may be used to endorse or promote products derived from 142914Ssaidi@eecs.umich.edu * this software without specific prior written permission. 152914Ssaidi@eecs.umich.edu * 162914Ssaidi@eecs.umich.edu * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 172914Ssaidi@eecs.umich.edu * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 182914Ssaidi@eecs.umich.edu * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 192914Ssaidi@eecs.umich.edu * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 202914Ssaidi@eecs.umich.edu * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 212914Ssaidi@eecs.umich.edu * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 222914Ssaidi@eecs.umich.edu * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 232914Ssaidi@eecs.umich.edu * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 242914Ssaidi@eecs.umich.edu * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 252914Ssaidi@eecs.umich.edu * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 262914Ssaidi@eecs.umich.edu * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 272914Ssaidi@eecs.umich.edu * 282914Ssaidi@eecs.umich.edu * Authors: Nathan Binkert 292914Ssaidi@eecs.umich.edu */ 302914Ssaidi@eecs.umich.edu 312914Ssaidi@eecs.umich.edu/* 322914Ssaidi@eecs.umich.edu * Copyright (c) 1990, 1993 332914Ssaidi@eecs.umich.edu * The Regents of the University of California. All rights reserved. 342914Ssaidi@eecs.umich.edu * 352914Ssaidi@eecs.umich.edu * This software was developed by the Computer Systems Engineering group 362914Ssaidi@eecs.umich.edu * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and 372914Ssaidi@eecs.umich.edu * contributed to Berkeley. 382914Ssaidi@eecs.umich.edu * 392914Ssaidi@eecs.umich.edu * All advertising materials mentioning features or use of this software 402914Ssaidi@eecs.umich.edu * must display the following acknowledgement: 418856Sandreas.hansson@arm.com * This product includes software developed by the University of 422914Ssaidi@eecs.umich.edu * California, Lawrence Berkeley Laboratories. 432914Ssaidi@eecs.umich.edu * 448708Sandreas.hansson@arm.com * Redistribution and use in source and binary forms, with or without 452914Ssaidi@eecs.umich.edu * modification, are permitted provided that the following conditions 462914Ssaidi@eecs.umich.edu * are met: 478914Sandreas.hansson@arm.com * 1. Redistributions of source code must retain the above copyright 488914Sandreas.hansson@arm.com * notice, this list of conditions and the following disclaimer. 498922Swilliam.wang@arm.com * 2. Redistributions in binary form must reproduce the above copyright 505740Snate@binkert.org * notice, this list of conditions and the following disclaimer in the 515740Snate@binkert.org * documentation and/or other materials provided with the distribution. 525740Snate@binkert.org * 3. All advertising materials mentioning features or use of this software 534490Sstever@eecs.umich.edu * must display the following acknowledgement: 544490Sstever@eecs.umich.edu * This product includes software developed by the University of 554490Sstever@eecs.umich.edu * California, Berkeley and its contributors. 568914Sandreas.hansson@arm.com * 4. Neither the name of the University nor the names of its contributors 578914Sandreas.hansson@arm.com * may be used to endorse or promote products derived from this software 583296Ssaidi@eecs.umich.edu * without specific prior written permission. 594929Sstever@gmail.com * 603091Sstever@eecs.umich.edu * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 613091Sstever@eecs.umich.edu * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 623091Sstever@eecs.umich.edu * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 638975Sandreas.hansson@arm.com * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 643091Sstever@eecs.umich.edu * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 654670Sstever@eecs.umich.edu * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 664670Sstever@eecs.umich.edu * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 674670Sstever@eecs.umich.edu * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 684670Sstever@eecs.umich.edu * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 694670Sstever@eecs.umich.edu * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 704670Sstever@eecs.umich.edu * SUCH DAMAGE. 714670Sstever@eecs.umich.edu * 724626Sstever@eecs.umich.edu * @(#)kgdb_stub.c 8.4 (Berkeley) 1/12/94 733091Sstever@eecs.umich.edu */ 743175Srdreslin@umich.edu 754626Sstever@eecs.umich.edu/*- 764670Sstever@eecs.umich.edu * Copyright (c) 2001 The NetBSD Foundation, Inc. 774670Sstever@eecs.umich.edu * All rights reserved. 784626Sstever@eecs.umich.edu * 798914Sandreas.hansson@arm.com * This code is derived from software contributed to The NetBSD Foundation 804626Sstever@eecs.umich.edu * by Jason R. Thorpe. 814490Sstever@eecs.umich.edu * 823309Srdreslin@umich.edu * Redistribution and use in source and binary forms, with or without 834670Sstever@eecs.umich.edu * modification, are permitted provided that the following conditions 843091Sstever@eecs.umich.edu * are met: 853091Sstever@eecs.umich.edu * 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#include <unistd.h> 121 122#include <string> 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/regredir.hh" 132#include "arch/alpha/remote_gdb.hh" 133#include "base/intmath.hh" 134#include "base/remote_gdb.hh" 135#include "base/socket.hh" 136#include "base/trace.hh" 137#include "cpu/thread_context.hh" 138#include "cpu/static_inst.hh" 139#include "mem/physical.hh" 140#include "mem/port.hh" 141#include "sim/system.hh" 142 143using namespace std; 144using namespace AlphaISA; 145 146RemoteGDB::RemoteGDB(System *_system, ThreadContext *tc) 147 : BaseRemoteGDB(_system, tc, KGDB_NUMREGS) 148{ 149 memset(gdbregs.regs, 0, gdbregs.bytes()); 150} 151 152/* 153 * Determine if the mapping at va..(va+len) is valid. 154 */ 155bool 156RemoteGDB::acc(Addr va, size_t len) 157{ 158#if !FULL_SYSTEM 159 panic("acc function needs to be rewritten for SE mode\n"); 160#else 161 Addr last_va; 162 163 va = TruncPage(va); 164 last_va = RoundPage(va + len); 165 166 do { 167 if (IsK0Seg(va)) { 168 if (va < (K0SegBase + pmem->size())) { 169 DPRINTF(GDBAcc, "acc: Mapping is valid K0SEG <= " 170 "%#x < K0SEG + size\n", va); 171 return true; 172 } else { 173 DPRINTF(GDBAcc, "acc: Mapping invalid %#x > K0SEG + size\n", 174 va); 175 return false; 176 } 177 } 178 179 /** 180 * This code says that all accesses to palcode (instruction 181 * and data) are valid since there isn't a va->pa mapping 182 * because palcode is accessed physically. At some point this 183 * should probably be cleaned up but there is no easy way to 184 * do it. 185 */ 186 187 if (PcPAL(va) || va < 0x10000) 188 return true; 189 190 Addr ptbr = context->readMiscRegNoEffect(IPR_PALtemp20); 191 PageTableEntry pte = 192 kernel_pte_lookup(context->getPhysPort(), ptbr, va); 193 if (!pte.valid()) { 194 DPRINTF(GDBAcc, "acc: %#x pte is invalid\n", va); 195 return false; 196 } 197 va += PageBytes; 198 } while (va < last_va); 199 200 DPRINTF(GDBAcc, "acc: %#x mapping is valid\n", va); 201 return true; 202#endif 203} 204 205/* 206 * Translate the kernel debugger register format into the GDB register 207 * format. 208 */ 209void 210RemoteGDB::getregs() 211{ 212 memset(gdbregs.regs, 0, gdbregs.bytes()); 213 214 gdbregs.regs[KGDB_REG_PC] = context->pcState().pc(); 215 216 // @todo: Currently this is very Alpha specific. 217 if (PcPAL(gdbregs.regs[KGDB_REG_PC])) { 218 for (int i = 0; i < NumIntArchRegs; ++i) { 219 gdbregs.regs[i] = context->readIntReg(reg_redir[i]); 220 } 221 } else { 222 for (int i = 0; i < NumIntArchRegs; ++i) { 223 gdbregs.regs[i] = context->readIntReg(i); 224 } 225 } 226 227#ifdef KGDB_FP_REGS 228 for (int i = 0; i < NumFloatArchRegs; ++i) { 229 gdbregs.regs[i + KGDB_REG_F0] = context->readFloatRegBits(i); 230 } 231#endif 232} 233 234/* 235 * Translate the GDB register format into the kernel debugger register 236 * format. 237 */ 238void 239RemoteGDB::setregs() 240{ 241 // @todo: Currently this is very Alpha specific. 242 if (PcPAL(gdbregs.regs[KGDB_REG_PC])) { 243 for (int i = 0; i < NumIntArchRegs; ++i) { 244 context->setIntReg(reg_redir[i], gdbregs.regs[i]); 245 } 246 } else { 247 for (int i = 0; i < NumIntArchRegs; ++i) { 248 context->setIntReg(i, gdbregs.regs[i]); 249 } 250 } 251 252#ifdef KGDB_FP_REGS 253 for (int i = 0; i < NumFloatArchRegs; ++i) { 254 context->setFloatRegBits(i, gdbregs.regs[i + KGDB_REG_F0]); 255 } 256#endif 257 context->pcState(gdbregs.regs[KGDB_REG_PC]); 258} 259 260void 261RemoteGDB::clearSingleStep() 262{ 263 DPRINTF(GDBMisc, "clearSingleStep bt_addr=%#x nt_addr=%#x\n", 264 takenBkpt, notTakenBkpt); 265 266 if (takenBkpt != 0) 267 clearTempBreakpoint(takenBkpt); 268 269 if (notTakenBkpt != 0) 270 clearTempBreakpoint(notTakenBkpt); 271} 272 273void 274RemoteGDB::setSingleStep() 275{ 276 PCState pc = context->pcState(); 277 PCState bpc; 278 bool set_bt = false; 279 280 // User was stopped at pc, e.g. the instruction at pc was not 281 // executed. 282 MachInst inst = read<MachInst>(pc.pc()); 283 StaticInstPtr si(inst, pc.pc()); 284 if (si->hasBranchTarget(pc, context, bpc)) { 285 // Don't bother setting a breakpoint on the taken branch if it 286 // is the same as the next pc 287 if (bpc.pc() != pc.npc()) 288 set_bt = true; 289 } 290 291 DPRINTF(GDBMisc, "setSingleStep bt_addr=%#x nt_addr=%#x\n", 292 takenBkpt, notTakenBkpt); 293 294 setTempBreakpoint(notTakenBkpt = pc.npc()); 295 296 if (set_bt) 297 setTempBreakpoint(takenBkpt = bpc.pc()); 298} 299 300// Write bytes to kernel address space for debugger. 301bool 302RemoteGDB::write(Addr vaddr, size_t size, const char *data) 303{ 304 if (BaseRemoteGDB::write(vaddr, size, data)) { 305#ifdef IMB 306 alpha_pal_imb(); 307#endif 308 return true; 309 } else { 310 return false; 311 } 312} 313 314