arch-x86: Stop "using namespace std"
[gem5.git] / src / arch / x86 / remote_gdb.cc
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
40 #include "arch/x86/remote_gdb.hh"
41
42 #include <sys/signal.h>
43 #include <unistd.h>
44
45 #include <string>
46
47 #include "arch/x86/mmu.hh"
48 #include "arch/x86/pagetable_walker.hh"
49 #include "arch/x86/process.hh"
50 #include "arch/x86/regs/int.hh"
51 #include "arch/x86/regs/misc.hh"
52 #include "base/remote_gdb.hh"
53 #include "base/socket.hh"
54 #include "base/trace.hh"
55 #include "cpu/base.hh"
56 #include "cpu/thread_context.hh"
57 #include "debug/GDBAcc.hh"
58 #include "mem/page_table.hh"
59 #include "sim/full_system.hh"
60
61 using namespace X86ISA;
62
63 RemoteGDB::RemoteGDB(System *_system, ThreadContext *c, int _port) :
64 BaseRemoteGDB(_system, c, _port), regCache32(this), regCache64(this)
65 {}
66
67 bool
68 RemoteGDB::acc(Addr va, size_t len)
69 {
70 if (FullSystem) {
71 Walker *walker = dynamic_cast<MMU *>(
72 context()->getMMUPtr())->getDataWalker();
73 unsigned logBytes;
74 Fault fault = walker->startFunctional(context(), va, logBytes,
75 BaseTLB::Read);
76 if (fault != NoFault)
77 return false;
78
79 Addr endVa = va + len - 1;
80 if ((va & ~mask(logBytes)) == (endVa & ~mask(logBytes)))
81 return true;
82
83 fault = walker->startFunctional(context(), endVa, logBytes,
84 BaseTLB::Read);
85 return fault == NoFault;
86 } else {
87 return context()->getProcessPtr()->pTable->lookup(va) != nullptr;
88 }
89 }
90
91 BaseGdbRegCache*
92 RemoteGDB::gdbRegs()
93 {
94 HandyM5Reg m5reg = context()->readMiscRegNoEffect(MISCREG_M5_REG);
95 if (m5reg.submode == SixtyFourBitMode)
96 return &regCache64;
97 else
98 return &regCache32;
99 }
100
101
102
103 void
104 RemoteGDB::AMD64GdbRegCache::getRegs(ThreadContext *context)
105 {
106 DPRINTF(GDBAcc, "getRegs in remotegdb \n");
107 r.rax = context->readIntReg(INTREG_RAX);
108 r.rbx = context->readIntReg(INTREG_RBX);
109 r.rcx = context->readIntReg(INTREG_RCX);
110 r.rdx = context->readIntReg(INTREG_RDX);
111 r.rsi = context->readIntReg(INTREG_RSI);
112 r.rdi = context->readIntReg(INTREG_RDI);
113 r.rbp = context->readIntReg(INTREG_RBP);
114 r.rsp = context->readIntReg(INTREG_RSP);
115 r.r8 = context->readIntReg(INTREG_R8);
116 r.r9 = context->readIntReg(INTREG_R9);
117 r.r10 = context->readIntReg(INTREG_R10);
118 r.r11 = context->readIntReg(INTREG_R11);
119 r.r12 = context->readIntReg(INTREG_R12);
120 r.r13 = context->readIntReg(INTREG_R13);
121 r.r14 = context->readIntReg(INTREG_R14);
122 r.r15 = context->readIntReg(INTREG_R15);
123 r.rip = context->pcState().pc();
124 r.eflags = context->readMiscRegNoEffect(MISCREG_RFLAGS);
125 r.cs = context->readMiscRegNoEffect(MISCREG_CS);
126 r.ss = context->readMiscRegNoEffect(MISCREG_SS);
127 r.ds = context->readMiscRegNoEffect(MISCREG_DS);
128 r.es = context->readMiscRegNoEffect(MISCREG_ES);
129 r.fs = context->readMiscRegNoEffect(MISCREG_FS);
130 r.gs = context->readMiscRegNoEffect(MISCREG_GS);
131 }
132
133 void
134 RemoteGDB::X86GdbRegCache::getRegs(ThreadContext *context)
135 {
136 DPRINTF(GDBAcc, "getRegs in remotegdb \n");
137 r.eax = context->readIntReg(INTREG_RAX);
138 r.ecx = context->readIntReg(INTREG_RCX);
139 r.edx = context->readIntReg(INTREG_RDX);
140 r.ebx = context->readIntReg(INTREG_RBX);
141 r.esp = context->readIntReg(INTREG_RSP);
142 r.ebp = context->readIntReg(INTREG_RBP);
143 r.esi = context->readIntReg(INTREG_RSI);
144 r.edi = context->readIntReg(INTREG_RDI);
145 r.eip = context->pcState().pc();
146 r.eflags = context->readMiscRegNoEffect(MISCREG_RFLAGS);
147 r.cs = context->readMiscRegNoEffect(MISCREG_CS);
148 r.ss = context->readMiscRegNoEffect(MISCREG_SS);
149 r.ds = context->readMiscRegNoEffect(MISCREG_DS);
150 r.es = context->readMiscRegNoEffect(MISCREG_ES);
151 r.fs = context->readMiscRegNoEffect(MISCREG_FS);
152 r.gs = context->readMiscRegNoEffect(MISCREG_GS);
153 }
154
155 void
156 RemoteGDB::AMD64GdbRegCache::setRegs(ThreadContext *context) const
157 {
158 DPRINTF(GDBAcc, "setRegs in remotegdb \n");
159 context->setIntReg(INTREG_RAX, r.rax);
160 context->setIntReg(INTREG_RBX, r.rbx);
161 context->setIntReg(INTREG_RCX, r.rcx);
162 context->setIntReg(INTREG_RDX, r.rdx);
163 context->setIntReg(INTREG_RSI, r.rsi);
164 context->setIntReg(INTREG_RDI, r.rdi);
165 context->setIntReg(INTREG_RBP, r.rbp);
166 context->setIntReg(INTREG_RSP, r.rsp);
167 context->setIntReg(INTREG_R8, r.r8);
168 context->setIntReg(INTREG_R9, r.r9);
169 context->setIntReg(INTREG_R10, r.r10);
170 context->setIntReg(INTREG_R11, r.r11);
171 context->setIntReg(INTREG_R12, r.r12);
172 context->setIntReg(INTREG_R13, r.r13);
173 context->setIntReg(INTREG_R14, r.r14);
174 context->setIntReg(INTREG_R15, r.r15);
175 context->pcState(r.rip);
176 context->setMiscReg(MISCREG_RFLAGS, r.eflags);
177 if (r.cs != context->readMiscRegNoEffect(MISCREG_CS))
178 warn("Remote gdb: Ignoring update to CS.\n");
179 if (r.ss != context->readMiscRegNoEffect(MISCREG_SS))
180 warn("Remote gdb: Ignoring update to SS.\n");
181 if (r.ds != context->readMiscRegNoEffect(MISCREG_DS))
182 warn("Remote gdb: Ignoring update to DS.\n");
183 if (r.es != context->readMiscRegNoEffect(MISCREG_ES))
184 warn("Remote gdb: Ignoring update to ES.\n");
185 if (r.fs != context->readMiscRegNoEffect(MISCREG_FS))
186 warn("Remote gdb: Ignoring update to FS.\n");
187 if (r.gs != context->readMiscRegNoEffect(MISCREG_GS))
188 warn("Remote gdb: Ignoring update to GS.\n");
189 }
190
191 void
192 RemoteGDB::X86GdbRegCache::setRegs(ThreadContext *context) const
193 {
194 DPRINTF(GDBAcc, "setRegs in remotegdb \n");
195 context->setIntReg(INTREG_RAX, r.eax);
196 context->setIntReg(INTREG_RCX, r.ecx);
197 context->setIntReg(INTREG_RDX, r.edx);
198 context->setIntReg(INTREG_RBX, r.ebx);
199 context->setIntReg(INTREG_RSP, r.esp);
200 context->setIntReg(INTREG_RBP, r.ebp);
201 context->setIntReg(INTREG_RSI, r.esi);
202 context->setIntReg(INTREG_RDI, r.edi);
203 context->pcState(r.eip);
204 context->setMiscReg(MISCREG_RFLAGS, r.eflags);
205 if (r.cs != context->readMiscRegNoEffect(MISCREG_CS))
206 warn("Remote gdb: Ignoring update to CS.\n");
207 if (r.ss != context->readMiscRegNoEffect(MISCREG_SS))
208 warn("Remote gdb: Ignoring update to SS.\n");
209 if (r.ds != context->readMiscRegNoEffect(MISCREG_DS))
210 warn("Remote gdb: Ignoring update to DS.\n");
211 if (r.es != context->readMiscRegNoEffect(MISCREG_ES))
212 warn("Remote gdb: Ignoring update to ES.\n");
213 if (r.fs != context->readMiscRegNoEffect(MISCREG_FS))
214 warn("Remote gdb: Ignoring update to FS.\n");
215 if (r.gs != context->readMiscRegNoEffect(MISCREG_GS))
216 warn("Remote gdb: Ignoring update to GS.\n");
217 }