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        return context()->getProcessPtr()->pTable->lookup(va) != nullptr;
92    }
93}
94
95BaseGdbRegCache*
96RemoteGDB::gdbRegs()
97{
98    HandyM5Reg m5reg = context()->readMiscRegNoEffect(MISCREG_M5_REG);
99    if (m5reg.submode == SixtyFourBitMode)
100        return &regCache64;
101    else
102        return &regCache32;
103}
104
105
106
107void
108RemoteGDB::AMD64GdbRegCache::getRegs(ThreadContext *context)
109{
110    DPRINTF(GDBAcc, "getRegs in remotegdb \n");
111    r.rax = context->readIntReg(INTREG_RAX);
112    r.rbx = context->readIntReg(INTREG_RBX);
113    r.rcx = context->readIntReg(INTREG_RCX);
114    r.rdx = context->readIntReg(INTREG_RDX);
115    r.rsi = context->readIntReg(INTREG_RSI);
116    r.rdi = context->readIntReg(INTREG_RDI);
117    r.rbp = context->readIntReg(INTREG_RBP);
118    r.rsp = context->readIntReg(INTREG_RSP);
119    r.r8 = context->readIntReg(INTREG_R8);
120    r.r9 = context->readIntReg(INTREG_R9);
121    r.r10 = context->readIntReg(INTREG_R10);
122    r.r11 = context->readIntReg(INTREG_R11);
123    r.r12 = context->readIntReg(INTREG_R12);
124    r.r13 = context->readIntReg(INTREG_R13);
125    r.r14 = context->readIntReg(INTREG_R14);
126    r.r15 = context->readIntReg(INTREG_R15);
127    r.rip = context->pcState().pc();
128    r.eflags = context->readMiscRegNoEffect(MISCREG_RFLAGS);
129    r.cs = context->readMiscRegNoEffect(MISCREG_CS);
130    r.ss = context->readMiscRegNoEffect(MISCREG_SS);
131    r.ds = context->readMiscRegNoEffect(MISCREG_DS);
132    r.es = context->readMiscRegNoEffect(MISCREG_ES);
133    r.fs = context->readMiscRegNoEffect(MISCREG_FS);
134    r.gs = context->readMiscRegNoEffect(MISCREG_GS);
135}
136
137void
138RemoteGDB::X86GdbRegCache::getRegs(ThreadContext *context)
139{
140    DPRINTF(GDBAcc, "getRegs in remotegdb \n");
141    r.eax = context->readIntReg(INTREG_RAX);
142    r.ecx = context->readIntReg(INTREG_RCX);
143    r.edx = context->readIntReg(INTREG_RDX);
144    r.ebx = context->readIntReg(INTREG_RBX);
145    r.esp = context->readIntReg(INTREG_RSP);
146    r.ebp = context->readIntReg(INTREG_RBP);
147    r.esi = context->readIntReg(INTREG_RSI);
148    r.edi = context->readIntReg(INTREG_RDI);
149    r.eip = context->pcState().pc();
150    r.eflags = context->readMiscRegNoEffect(MISCREG_RFLAGS);
151    r.cs = context->readMiscRegNoEffect(MISCREG_CS);
152    r.ss = context->readMiscRegNoEffect(MISCREG_SS);
153    r.ds = context->readMiscRegNoEffect(MISCREG_DS);
154    r.es = context->readMiscRegNoEffect(MISCREG_ES);
155    r.fs = context->readMiscRegNoEffect(MISCREG_FS);
156    r.gs = context->readMiscRegNoEffect(MISCREG_GS);
157}
158
159void
160RemoteGDB::AMD64GdbRegCache::setRegs(ThreadContext *context) const
161{
162    DPRINTF(GDBAcc, "setRegs in remotegdb \n");
163    context->setIntReg(INTREG_RAX, r.rax);
164    context->setIntReg(INTREG_RBX, r.rbx);
165    context->setIntReg(INTREG_RCX, r.rcx);
166    context->setIntReg(INTREG_RDX, r.rdx);
167    context->setIntReg(INTREG_RSI, r.rsi);
168    context->setIntReg(INTREG_RDI, r.rdi);
169    context->setIntReg(INTREG_RBP, r.rbp);
170    context->setIntReg(INTREG_RSP, r.rsp);
171    context->setIntReg(INTREG_R8, r.r8);
172    context->setIntReg(INTREG_R9, r.r9);
173    context->setIntReg(INTREG_R10, r.r10);
174    context->setIntReg(INTREG_R11, r.r11);
175    context->setIntReg(INTREG_R12, r.r12);
176    context->setIntReg(INTREG_R13, r.r13);
177    context->setIntReg(INTREG_R14, r.r14);
178    context->setIntReg(INTREG_R15, r.r15);
179    context->pcState(r.rip);
180    context->setMiscReg(MISCREG_RFLAGS, r.eflags);
181    if (r.cs != context->readMiscRegNoEffect(MISCREG_CS))
182        warn("Remote gdb: Ignoring update to CS.\n");
183    if (r.ss != context->readMiscRegNoEffect(MISCREG_SS))
184        warn("Remote gdb: Ignoring update to SS.\n");
185    if (r.ds != context->readMiscRegNoEffect(MISCREG_DS))
186        warn("Remote gdb: Ignoring update to DS.\n");
187    if (r.es != context->readMiscRegNoEffect(MISCREG_ES))
188        warn("Remote gdb: Ignoring update to ES.\n");
189    if (r.fs != context->readMiscRegNoEffect(MISCREG_FS))
190        warn("Remote gdb: Ignoring update to FS.\n");
191    if (r.gs != context->readMiscRegNoEffect(MISCREG_GS))
192        warn("Remote gdb: Ignoring update to GS.\n");
193}
194
195void
196RemoteGDB::X86GdbRegCache::setRegs(ThreadContext *context) const
197{
198    DPRINTF(GDBAcc, "setRegs in remotegdb \n");
199    context->setIntReg(INTREG_RAX, r.eax);
200    context->setIntReg(INTREG_RCX, r.ecx);
201    context->setIntReg(INTREG_RDX, r.edx);
202    context->setIntReg(INTREG_RBX, r.ebx);
203    context->setIntReg(INTREG_RSP, r.esp);
204    context->setIntReg(INTREG_RBP, r.ebp);
205    context->setIntReg(INTREG_RSI, r.esi);
206    context->setIntReg(INTREG_RDI, r.edi);
207    context->pcState(r.eip);
208    context->setMiscReg(MISCREG_RFLAGS, r.eflags);
209    if (r.cs != context->readMiscRegNoEffect(MISCREG_CS))
210        warn("Remote gdb: Ignoring update to CS.\n");
211    if (r.ss != context->readMiscRegNoEffect(MISCREG_SS))
212        warn("Remote gdb: Ignoring update to SS.\n");
213    if (r.ds != context->readMiscRegNoEffect(MISCREG_DS))
214        warn("Remote gdb: Ignoring update to DS.\n");
215    if (r.es != context->readMiscRegNoEffect(MISCREG_ES))
216        warn("Remote gdb: Ignoring update to ES.\n");
217    if (r.fs != context->readMiscRegNoEffect(MISCREG_FS))
218        warn("Remote gdb: Ignoring update to FS.\n");
219    if (r.gs != context->readMiscRegNoEffect(MISCREG_GS))
220        warn("Remote gdb: Ignoring update to GS.\n");
221}
222