remote_gdb.cc revision 12449:2260f4a68210
1/*
2 * Copyright 2015 LabWare
3 * Copyright 2014 Google, Inc.
4 * Copyright (c) 2007 The Hewlett-Packard Development Company
5 * All rights reserved.
6 *
7 * The license below extends only to copyright in the software and shall
8 * not be construed as granting a license to any other intellectual
9 * property including but not limited to intellectual property relating
10 * to a hardware implementation of the functionality of the software
11 * licensed hereunder.  You may use the software subject to the license
12 * terms below provided that you ensure that this notice is replicated
13 * unmodified and in its entirety in all distributions of the software,
14 * modified or unmodified, in source code or in binary form.
15 *
16 * Redistribution and use in source and binary forms, with or without
17 * modification, are permitted provided that the following conditions are
18 * met: redistributions of source code must retain the above copyright
19 * notice, this list of conditions and the following disclaimer;
20 * redistributions in binary form must reproduce the above copyright
21 * notice, this list of conditions and the following disclaimer in the
22 * documentation and/or other materials provided with the distribution;
23 * neither the name of the copyright holders nor the names of its
24 * contributors may be used to endorse or promote products derived from
25 * this software without specific prior written permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
30 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
31 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
32 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
33 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
34 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
35 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
36 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
37 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 *
39 * Authors: Gabe Black
40 *          Boris Shingarov
41 */
42
43#include "arch/x86/remote_gdb.hh"
44
45#include <sys/signal.h>
46#include <unistd.h>
47
48#include <string>
49
50#include "arch/vtophys.hh"
51#include "arch/x86/pagetable_walker.hh"
52#include "arch/x86/process.hh"
53#include "arch/x86/regs/int.hh"
54#include "arch/x86/regs/misc.hh"
55#include "base/remote_gdb.hh"
56#include "base/socket.hh"
57#include "base/trace.hh"
58#include "cpu/base.hh"
59#include "cpu/thread_context.hh"
60#include "debug/GDBAcc.hh"
61#include "mem/page_table.hh"
62#include "sim/full_system.hh"
63
64using namespace std;
65using namespace X86ISA;
66
67RemoteGDB::RemoteGDB(System *_system, ThreadContext *c, int _port) :
68    BaseRemoteGDB(_system, c, _port), regCache32(this), regCache64(this)
69{}
70
71bool
72RemoteGDB::acc(Addr va, size_t len)
73{
74    if (FullSystem) {
75        Walker *walker = dynamic_cast<TLB *>(
76            context()->getDTBPtr())->getWalker();
77        unsigned logBytes;
78        Fault fault = walker->startFunctional(context(), va, logBytes,
79                                              BaseTLB::Read);
80        if (fault != NoFault)
81            return false;
82
83        Addr endVa = va + len - 1;
84        if ((va & ~mask(logBytes)) == (endVa & ~mask(logBytes)))
85            return true;
86
87        fault = walker->startFunctional(context(), endVa, logBytes,
88                                        BaseTLB::Read);
89        return fault == NoFault;
90    } else {
91        TlbEntry entry;
92        return context()->getProcessPtr()->pTable->lookup(va, entry);
93    }
94}
95
96BaseGdbRegCache*
97RemoteGDB::gdbRegs()
98{
99    HandyM5Reg m5reg = context()->readMiscRegNoEffect(MISCREG_M5_REG);
100    if (m5reg.submode == SixtyFourBitMode)
101        return &regCache64;
102    else
103        return &regCache32;
104}
105
106
107
108void
109RemoteGDB::AMD64GdbRegCache::getRegs(ThreadContext *context)
110{
111    DPRINTF(GDBAcc, "getRegs in remotegdb \n");
112    r.rax = context->readIntReg(INTREG_RAX);
113    r.rbx = context->readIntReg(INTREG_RBX);
114    r.rcx = context->readIntReg(INTREG_RCX);
115    r.rdx = context->readIntReg(INTREG_RDX);
116    r.rsi = context->readIntReg(INTREG_RSI);
117    r.rdi = context->readIntReg(INTREG_RDI);
118    r.rbp = context->readIntReg(INTREG_RBP);
119    r.rsp = context->readIntReg(INTREG_RSP);
120    r.r8 = context->readIntReg(INTREG_R8);
121    r.r9 = context->readIntReg(INTREG_R9);
122    r.r10 = context->readIntReg(INTREG_R10);
123    r.r11 = context->readIntReg(INTREG_R11);
124    r.r12 = context->readIntReg(INTREG_R12);
125    r.r13 = context->readIntReg(INTREG_R13);
126    r.r14 = context->readIntReg(INTREG_R14);
127    r.r15 = context->readIntReg(INTREG_R15);
128    r.rip = context->pcState().pc();
129    r.eflags = context->readMiscRegNoEffect(MISCREG_RFLAGS);
130    r.cs = context->readMiscRegNoEffect(MISCREG_CS);
131    r.ss = context->readMiscRegNoEffect(MISCREG_SS);
132    r.ds = context->readMiscRegNoEffect(MISCREG_DS);
133    r.es = context->readMiscRegNoEffect(MISCREG_ES);
134    r.fs = context->readMiscRegNoEffect(MISCREG_FS);
135    r.gs = context->readMiscRegNoEffect(MISCREG_GS);
136}
137
138void
139RemoteGDB::X86GdbRegCache::getRegs(ThreadContext *context)
140{
141    DPRINTF(GDBAcc, "getRegs in remotegdb \n");
142    r.eax = context->readIntReg(INTREG_RAX);
143    r.ecx = context->readIntReg(INTREG_RCX);
144    r.edx = context->readIntReg(INTREG_RDX);
145    r.ebx = context->readIntReg(INTREG_RBX);
146    r.esp = context->readIntReg(INTREG_RSP);
147    r.ebp = context->readIntReg(INTREG_RBP);
148    r.esi = context->readIntReg(INTREG_RSI);
149    r.edi = context->readIntReg(INTREG_RDI);
150    r.eip = context->pcState().pc();
151    r.eflags = context->readMiscRegNoEffect(MISCREG_RFLAGS);
152    r.cs = context->readMiscRegNoEffect(MISCREG_CS);
153    r.ss = context->readMiscRegNoEffect(MISCREG_SS);
154    r.ds = context->readMiscRegNoEffect(MISCREG_DS);
155    r.es = context->readMiscRegNoEffect(MISCREG_ES);
156    r.fs = context->readMiscRegNoEffect(MISCREG_FS);
157    r.gs = context->readMiscRegNoEffect(MISCREG_GS);
158}
159
160void
161RemoteGDB::AMD64GdbRegCache::setRegs(ThreadContext *context) const
162{
163    DPRINTF(GDBAcc, "setRegs in remotegdb \n");
164    context->setIntReg(INTREG_RAX, r.rax);
165    context->setIntReg(INTREG_RBX, r.rbx);
166    context->setIntReg(INTREG_RCX, r.rcx);
167    context->setIntReg(INTREG_RDX, r.rdx);
168    context->setIntReg(INTREG_RSI, r.rsi);
169    context->setIntReg(INTREG_RDI, r.rdi);
170    context->setIntReg(INTREG_RBP, r.rbp);
171    context->setIntReg(INTREG_RSP, r.rsp);
172    context->setIntReg(INTREG_R8, r.r8);
173    context->setIntReg(INTREG_R9, r.r9);
174    context->setIntReg(INTREG_R10, r.r10);
175    context->setIntReg(INTREG_R11, r.r11);
176    context->setIntReg(INTREG_R12, r.r12);
177    context->setIntReg(INTREG_R13, r.r13);
178    context->setIntReg(INTREG_R14, r.r14);
179    context->setIntReg(INTREG_R15, r.r15);
180    context->pcState(r.rip);
181    context->setMiscReg(MISCREG_RFLAGS, r.eflags);
182    if (r.cs != context->readMiscRegNoEffect(MISCREG_CS))
183        warn("Remote gdb: Ignoring update to CS.\n");
184    if (r.ss != context->readMiscRegNoEffect(MISCREG_SS))
185        warn("Remote gdb: Ignoring update to SS.\n");
186    if (r.ds != context->readMiscRegNoEffect(MISCREG_DS))
187        warn("Remote gdb: Ignoring update to DS.\n");
188    if (r.es != context->readMiscRegNoEffect(MISCREG_ES))
189        warn("Remote gdb: Ignoring update to ES.\n");
190    if (r.fs != context->readMiscRegNoEffect(MISCREG_FS))
191        warn("Remote gdb: Ignoring update to FS.\n");
192    if (r.gs != context->readMiscRegNoEffect(MISCREG_GS))
193        warn("Remote gdb: Ignoring update to GS.\n");
194}
195
196void
197RemoteGDB::X86GdbRegCache::setRegs(ThreadContext *context) const
198{
199    DPRINTF(GDBAcc, "setRegs in remotegdb \n");
200    context->setIntReg(INTREG_RAX, r.eax);
201    context->setIntReg(INTREG_RCX, r.ecx);
202    context->setIntReg(INTREG_RDX, r.edx);
203    context->setIntReg(INTREG_RBX, r.ebx);
204    context->setIntReg(INTREG_RSP, r.esp);
205    context->setIntReg(INTREG_RBP, r.ebp);
206    context->setIntReg(INTREG_RSI, r.esi);
207    context->setIntReg(INTREG_RDI, r.edi);
208    context->pcState(r.eip);
209    context->setMiscReg(MISCREG_RFLAGS, r.eflags);
210    if (r.cs != context->readMiscRegNoEffect(MISCREG_CS))
211        warn("Remote gdb: Ignoring update to CS.\n");
212    if (r.ss != context->readMiscRegNoEffect(MISCREG_SS))
213        warn("Remote gdb: Ignoring update to SS.\n");
214    if (r.ds != context->readMiscRegNoEffect(MISCREG_DS))
215        warn("Remote gdb: Ignoring update to DS.\n");
216    if (r.es != context->readMiscRegNoEffect(MISCREG_ES))
217        warn("Remote gdb: Ignoring update to ES.\n");
218    if (r.fs != context->readMiscRegNoEffect(MISCREG_FS))
219        warn("Remote gdb: Ignoring update to FS.\n");
220    if (r.gs != context->readMiscRegNoEffect(MISCREG_GS))
221        warn("Remote gdb: Ignoring update to GS.\n");
222}
223