remote_gdb.cc revision 10595:25ecfc14f73f
1/* 2 * Copyright 2014 Google, Inc. 3 * Copyright (c) 2002-2005 The Regents of The University of Michigan 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions are 8 * met: redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer; 10 * redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution; 13 * neither the name of the copyright holders nor the names of its 14 * contributors may be used to endorse or promote products derived from 15 * this software without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 18 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 19 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 20 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 21 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 22 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 23 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 * 29 * Authors: Nathan Binkert 30 */ 31 32/* 33 * Copyright (c) 1990, 1993 The Regents of the University of California 34 * All rights reserved. 35 * 36 * This software was developed by the Computer Systems Engineering group 37 * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and 38 * contributed to Berkeley. 39 * 40 * All advertising materials mentioning features or use of this software 41 * must display the following acknowledgement: 42 * This product includes software developed by the University of 43 * California, Lawrence Berkeley Laboratories. 44 * 45 * Redistribution and use in source and binary forms, with or without 46 * modification, are permitted provided that the following conditions 47 * are met: 48 * 1. Redistributions of source code must retain the above copyright 49 * notice, this list of conditions and the following disclaimer. 50 * 2. Redistributions in binary form must reproduce the above copyright 51 * notice, this list of conditions and the following disclaimer in the 52 * documentation and/or other materials provided with the distribution. 53 * 3. All advertising materials mentioning features or use of this software 54 * must display the following acknowledgement: 55 * This product includes software developed by the University of 56 * California, Berkeley and its contributors. 57 * 4. Neither the name of the University nor the names of its contributors 58 * may be used to endorse or promote products derived from this software 59 * without specific prior written permission. 60 * 61 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 62 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 63 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 64 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 65 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 66 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 67 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 68 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 69 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 70 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 71 * SUCH DAMAGE. 72 * 73 * @(#)kgdb_stub.c 8.4 (Berkeley) 1/12/94 74 */ 75 76/*- 77 * Copyright (c) 2001 The NetBSD Foundation, Inc. 78 * All rights reserved. 79 * 80 * This code is derived from software contributed to The NetBSD Foundation 81 * by Jason R. Thorpe. 82 * 83 * Redistribution and use in source and binary forms, with or without 84 * modification, are permitted provided that the following conditions 85 * are met: 86 * 1. Redistributions of source code must retain the above copyright 87 * notice, this list of conditions and the following disclaimer. 88 * 2. Redistributions in binary form must reproduce the above copyright 89 * notice, this list of conditions and the following disclaimer in the 90 * documentation and/or other materials provided with the distribution. 91 * 3. All advertising materials mentioning features or use of this software 92 * must display the following acknowledgement: 93 * This product includes software developed by the NetBSD 94 * Foundation, Inc. and its contributors. 95 * 4. Neither the name of The NetBSD Foundation nor the names of its 96 * contributors may be used to endorse or promote products derived 97 * from this software without specific prior written permission. 98 * 99 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 100 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 101 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 102 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 103 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 104 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 105 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 106 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 107 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 108 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 109 * POSSIBILITY OF SUCH DAMAGE. 110 */ 111 112/* 113 * $NetBSD: kgdb_stub.c,v 1.8 2001/07/07 22:58:00 wdk Exp $ 114 * 115 * Taken from NetBSD 116 * 117 * "Stub" to allow remote cpu to debug over a serial line using gdb. 118 */ 119 120#include <sys/signal.h> 121#include <unistd.h> 122 123#include <string> 124 125 126#include "arch/alpha/decoder.hh" 127#include "arch/alpha/kgdb.h" 128#include "arch/alpha/regredir.hh" 129#include "arch/alpha/remote_gdb.hh" 130#include "arch/alpha/utility.hh" 131#include "arch/alpha/vtophys.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/static_inst.hh" 137#include "cpu/thread_context.hh" 138#include "debug/GDBAcc.hh" 139#include "debug/GDBMisc.hh" 140#include "mem/physical.hh" 141#include "mem/port.hh" 142#include "sim/system.hh" 143#include "sim/full_system.hh" 144 145using namespace std; 146using namespace AlphaISA; 147 148RemoteGDB::RemoteGDB(System *_system, ThreadContext *tc) 149 : BaseRemoteGDB(_system, tc, KGDB_NUMREGS * sizeof(uint64_t)) 150{ 151 memset(gdbregs.regs, 0, gdbregs.bytes()); 152} 153 154/* 155 * Determine if the mapping at va..(va+len) is valid. 156 */ 157bool 158RemoteGDB::acc(Addr va, size_t len) 159{ 160 if (!FullSystem) 161 panic("acc function needs to be rewritten for SE mode\n"); 162 163 Addr last_va; 164 165 va = TruncPage(va); 166 last_va = RoundPage(va + len); 167 168 do { 169 if (IsK0Seg(va)) { 170 if (va < (K0SegBase + system->memSize())) { 171 DPRINTF(GDBAcc, "acc: Mapping is valid K0SEG <= " 172 "%#x < K0SEG + size\n", va); 173 return true; 174 } else { 175 DPRINTF(GDBAcc, "acc: Mapping invalid %#x " 176 "> K0SEG + size\n", va); 177 return false; 178 } 179 } 180 181 /** 182 * This code says that all accesses to palcode (instruction 183 * and data) are valid since there isn't a va->pa mapping 184 * because palcode is accessed physically. At some point this 185 * should probably be cleaned up but there is no easy way to 186 * do it. 187 */ 188 189 if (PcPAL(va) || va < 0x10000) 190 return true; 191 192 Addr ptbr = context->readMiscRegNoEffect(IPR_PALtemp20); 193 PageTableEntry pte = 194 kernel_pte_lookup(context->getPhysProxy(), ptbr, va); 195 if (!pte.valid()) { 196 DPRINTF(GDBAcc, "acc: %#x pte is invalid\n", va); 197 return false; 198 } 199 va += PageBytes; 200 } while (va < last_va); 201 202 DPRINTF(GDBAcc, "acc: %#x mapping is valid\n", va); 203 return true; 204} 205 206/* 207 * Translate the kernel debugger register format into the GDB register 208 * format. 209 */ 210void 211RemoteGDB::getregs() 212{ 213 memset(gdbregs.regs, 0, gdbregs.bytes()); 214 215 gdbregs.regs64[KGDB_REG_PC] = context->pcState().pc(); 216 217 // @todo: Currently this is very Alpha specific. 218 if (PcPAL(gdbregs.regs64[KGDB_REG_PC])) { 219 for (int i = 0; i < NumIntArchRegs; ++i) 220 gdbregs.regs64[i] = context->readIntReg(reg_redir[i]); 221 } else { 222 for (int i = 0; i < NumIntArchRegs; ++i) 223 gdbregs.regs64[i] = context->readIntReg(i); 224 } 225 226#ifdef KGDB_FP_REGS 227 for (int i = 0; i < NumFloatArchRegs; ++i) 228 gdbregs.regs64[i + KGDB_REG_F0] = context->readFloatRegBits(i); 229#endif 230} 231 232/* 233 * Translate the GDB register format into the kernel debugger register 234 * format. 235 */ 236void 237RemoteGDB::setregs() 238{ 239 // @todo: Currently this is very Alpha specific. 240 if (PcPAL(gdbregs.regs64[KGDB_REG_PC])) { 241 for (int i = 0; i < NumIntArchRegs; ++i) { 242 context->setIntReg(reg_redir[i], gdbregs.regs64[i]); 243 } 244 } else { 245 for (int i = 0; i < NumIntArchRegs; ++i) { 246 context->setIntReg(i, gdbregs.regs64[i]); 247 } 248 } 249 250#ifdef KGDB_FP_REGS 251 for (int i = 0; i < NumFloatArchRegs; ++i) { 252 context->setFloatRegBits(i, gdbregs.regs64[i + KGDB_REG_F0]); 253 } 254#endif 255 context->pcState(gdbregs.regs64[KGDB_REG_PC]); 256} 257 258void 259RemoteGDB::clearSingleStep() 260{ 261 DPRINTF(GDBMisc, "clearSingleStep bt_addr=%#x nt_addr=%#x\n", 262 takenBkpt, notTakenBkpt); 263 264 if (takenBkpt != 0) 265 clearTempBreakpoint(takenBkpt); 266 267 if (notTakenBkpt != 0) 268 clearTempBreakpoint(notTakenBkpt); 269} 270 271void 272RemoteGDB::setSingleStep() 273{ 274 PCState pc = context->pcState(); 275 PCState bpc; 276 bool set_bt = false; 277 278 // User was stopped at pc, e.g. the instruction at pc was not 279 // executed. 280 MachInst inst = read<MachInst>(pc.pc()); 281 StaticInstPtr si = context->getDecoderPtr()->decode(inst, pc.pc()); 282 if (si->hasBranchTarget(pc, context, bpc)) { 283 // Don't bother setting a breakpoint on the taken branch if it 284 // is the same as the next pc 285 if (bpc.pc() != pc.npc()) 286 set_bt = true; 287 } 288 289 DPRINTF(GDBMisc, "setSingleStep bt_addr=%#x nt_addr=%#x\n", 290 takenBkpt, notTakenBkpt); 291 292 setTempBreakpoint(notTakenBkpt = pc.npc()); 293 294 if (set_bt) 295 setTempBreakpoint(takenBkpt = bpc.pc()); 296} 297 298// Write bytes to kernel address space for debugger. 299bool 300RemoteGDB::write(Addr vaddr, size_t size, const char *data) 301{ 302 if (BaseRemoteGDB::write(vaddr, size, data)) { 303#ifdef IMB 304 alpha_pal_imb(); 305#endif 306 return true; 307 } else { 308 return false; 309 } 310} 311 312 313bool 314RemoteGDB::insertHardBreak(Addr addr, size_t len) 315{ 316 warn_once("Breakpoints do not work in Alpha PAL mode.\n" 317 " See PCEventQueue::doService() in cpu/pc_event.cc.\n"); 318 return BaseRemoteGDB::insertHardBreak(addr, len); 319} 320