arm: Correctly access the stack pointer in GDB
[gem5.git] / src / arch / arm / remote_gdb.cc
1 /*
2 * Copyright 2014 Google Inc.
3 * Copyright (c) 2010, 2013 ARM Limited
4 * All rights reserved
5 *
6 * The license below extends only to copyright in the software and shall
7 * not be construed as granting a license to any other intellectual
8 * property including but not limited to intellectual property relating
9 * to a hardware implementation of the functionality of the software
10 * licensed hereunder. You may use the software subject to the license
11 * terms below provided that you ensure that this notice is replicated
12 * unmodified and in its entirety in all distributions of the software,
13 * modified or unmodified, in source code or in binary form.
14 *
15 * Copyright (c) 2002-2005 The Regents of The University of Michigan
16 * All rights reserved.
17 *
18 * Redistribution and use in source and binary forms, with or without
19 * modification, are permitted provided that the following conditions are
20 * met: redistributions of source code must retain the above copyright
21 * notice, this list of conditions and the following disclaimer;
22 * redistributions in binary form must reproduce the above copyright
23 * notice, this list of conditions and the following disclaimer in the
24 * documentation and/or other materials provided with the distribution;
25 * neither the name of the copyright holders nor the names of its
26 * contributors may be used to endorse or promote products derived from
27 * this software without specific prior written permission.
28 *
29 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
30 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
31 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
32 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
33 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
34 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
35 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
36 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
37 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
38 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
39 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40 *
41 * Authors: Nathan Binkert
42 * William Wang
43 */
44
45 /*
46 * Copyright (c) 1990, 1993 The Regents of the University of California
47 * All rights reserved
48 *
49 * This software was developed by the Computer Systems Engineering group
50 * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
51 * contributed to Berkeley.
52 *
53 * 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, Lawrence Berkeley Laboratories.
57 *
58 * Redistribution and use in source and binary forms, with or without
59 * modification, are permitted provided that the following conditions
60 * are met:
61 * 1. Redistributions of source code must retain the above copyright
62 * notice, this list of conditions and the following disclaimer.
63 * 2. Redistributions in binary form must reproduce the above copyright
64 * notice, this list of conditions and the following disclaimer in the
65 * documentation and/or other materials provided with the distribution.
66 * 3. All advertising materials mentioning features or use of this software
67 * must display the following acknowledgement:
68 * This product includes software developed by the University of
69 * California, Berkeley and its contributors.
70 * 4. Neither the name of the University nor the names of its contributors
71 * may be used to endorse or promote products derived from this software
72 * without specific prior written permission.
73 *
74 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
75 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
76 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
77 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
78 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
79 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
80 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
81 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
82 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
83 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
84 * SUCH DAMAGE.
85 *
86 * @(#)kgdb_stub.c 8.4 (Berkeley) 1/12/94
87 */
88
89 /*-
90 * Copyright (c) 2001 The NetBSD Foundation, Inc.
91 * All rights reserved.
92 *
93 * This code is derived from software contributed to The NetBSD Foundation
94 * by Jason R. Thorpe.
95 *
96 * Redistribution and use in source and binary forms, with or without
97 * modification, are permitted provided that the following conditions
98 * are met:
99 * 1. Redistributions of source code must retain the above copyright
100 * notice, this list of conditions and the following disclaimer.
101 * 2. Redistributions in binary form must reproduce the above copyright
102 * notice, this list of conditions and the following disclaimer in the
103 * documentation and/or other materials provided with the distribution.
104 * 3. All advertising materials mentioning features or use of this software
105 * must display the following acknowledgement:
106 * This product includes software developed by the NetBSD
107 * Foundation, Inc. and its contributors.
108 * 4. Neither the name of The NetBSD Foundation nor the names of its
109 * contributors may be used to endorse or promote products derived
110 * from this software without specific prior written permission.
111 *
112 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
113 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
114 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
115 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
116 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
117 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
118 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
119 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
120 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
121 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
122 * POSSIBILITY OF SUCH DAMAGE.
123 */
124
125 /*
126 * $NetBSD: kgdb_stub.c,v 1.8 2001/07/07 22:58:00 wdk Exp $
127 *
128 * Taken from NetBSD
129 *
130 * "Stub" to allow remote cpu to debug over a serial line using gdb.
131 */
132
133 #include <sys/signal.h>
134 #include <unistd.h>
135
136 #include <string>
137
138 #include "arch/arm/decoder.hh"
139 #include "arch/arm/pagetable.hh"
140 #include "arch/arm/registers.hh"
141 #include "arch/arm/remote_gdb.hh"
142 #include "arch/arm/system.hh"
143 #include "arch/arm/utility.hh"
144 #include "arch/arm/vtophys.hh"
145 #include "base/chunk_generator.hh"
146 #include "base/intmath.hh"
147 #include "base/remote_gdb.hh"
148 #include "base/socket.hh"
149 #include "base/trace.hh"
150 #include "cpu/static_inst.hh"
151 #include "cpu/thread_context.hh"
152 #include "cpu/thread_state.hh"
153 #include "debug/GDBAcc.hh"
154 #include "debug/GDBMisc.hh"
155 #include "mem/page_table.hh"
156 #include "mem/physical.hh"
157 #include "mem/port.hh"
158 #include "sim/full_system.hh"
159 #include "sim/system.hh"
160
161 using namespace std;
162 using namespace ArmISA;
163
164 RemoteGDB::RemoteGDB(System *_system, ThreadContext *tc)
165 : BaseRemoteGDB(_system, tc, GDB_REG_BYTES)
166 {
167 }
168
169 /*
170 * Determine if the mapping at va..(va+len) is valid.
171 */
172 bool
173 RemoteGDB::acc(Addr va, size_t len)
174 {
175 if (FullSystem) {
176 for (ChunkGenerator gen(va, len, PageBytes); !gen.done(); gen.next()) {
177 if (!virtvalid(context, gen.addr())) {
178 DPRINTF(GDBAcc, "acc: %#x mapping is invalid\n", va);
179 return false;
180 }
181 }
182
183 DPRINTF(GDBAcc, "acc: %#x mapping is valid\n", va);
184 return true;
185 } else {
186 TlbEntry entry;
187 //Check to make sure the first byte is mapped into the processes address
188 //space.
189 if (context->getProcessPtr()->pTable->lookup(va, entry))
190 return true;
191 return false;
192 }
193 }
194
195 /*
196 * Translate the kernel debugger register format into the GDB register
197 * format.
198 */
199 void
200 RemoteGDB::getregs()
201 {
202 DPRINTF(GDBAcc, "getregs in remotegdb \n");
203
204 memset(gdbregs.regs, 0, gdbregs.bytes());
205
206 if (inAArch64(context)) { // AArch64
207 // x0-x30
208 for (int i = 0; i < 31; ++i)
209 gdbregs.regs64[GDB64_X0 + i] = context->readIntReg(INTREG_X0 + i);
210 gdbregs.regs64[GDB64_SPX] = context->readIntReg(INTREG_SPX);
211 // pc
212 gdbregs.regs64[GDB64_PC] = context->pcState().pc();
213 // cpsr
214 gdbregs.regs64[GDB64_CPSR] =
215 context->readMiscRegNoEffect(MISCREG_CPSR);
216 // v0-v31
217 for (int i = 0; i < 128; i += 4) {
218 int gdboff = GDB64_V0_32 + i;
219 gdbregs.regs32[gdboff + 0] = context->readFloatRegBits(i + 2);
220 gdbregs.regs32[gdboff + 1] = context->readFloatRegBits(i + 3);
221 gdbregs.regs32[gdboff + 2] = context->readFloatRegBits(i + 0);
222 gdbregs.regs32[gdboff + 3] = context->readFloatRegBits(i + 1);
223 }
224 } else { // AArch32
225 // R0-R15 supervisor mode
226 gdbregs.regs32[GDB32_R0 + 0] = context->readIntReg(INTREG_R0);
227 gdbregs.regs32[GDB32_R0 + 1] = context->readIntReg(INTREG_R1);
228 gdbregs.regs32[GDB32_R0 + 2] = context->readIntReg(INTREG_R2);
229 gdbregs.regs32[GDB32_R0 + 3] = context->readIntReg(INTREG_R3);
230 gdbregs.regs32[GDB32_R0 + 4] = context->readIntReg(INTREG_R4);
231 gdbregs.regs32[GDB32_R0 + 5] = context->readIntReg(INTREG_R5);
232 gdbregs.regs32[GDB32_R0 + 6] = context->readIntReg(INTREG_R6);
233 gdbregs.regs32[GDB32_R0 + 7] = context->readIntReg(INTREG_R7);
234 gdbregs.regs32[GDB32_R0 + 8] = context->readIntReg(INTREG_R8);
235 gdbregs.regs32[GDB32_R0 + 9] = context->readIntReg(INTREG_R9);
236 gdbregs.regs32[GDB32_R0 + 10] = context->readIntReg(INTREG_R10);
237 gdbregs.regs32[GDB32_R0 + 11] = context->readIntReg(INTREG_R11);
238 gdbregs.regs32[GDB32_R0 + 12] = context->readIntReg(INTREG_R12);
239 gdbregs.regs32[GDB32_R0 + 13] = context->readIntReg(INTREG_SP);
240 gdbregs.regs32[GDB32_R0 + 14] = context->readIntReg(INTREG_LR);
241 gdbregs.regs32[GDB32_R0 + 15] = context->pcState().pc();
242
243 // CPSR
244 gdbregs.regs32[GDB32_CPSR] = context->readMiscRegNoEffect(MISCREG_CPSR);
245
246 // vfpv3/neon floating point registers (32 double or 64 float)
247 for (int i = 0; i < NumFloatV7ArchRegs; ++i)
248 gdbregs.regs32[GDB32_F0 + i] = context->readFloatRegBits(i);
249
250 // FPSCR
251 gdbregs.regs32[GDB32_FPSCR] =
252 context->readMiscRegNoEffect(MISCREG_FPSCR);
253 }
254 }
255
256 /*
257 * Translate the GDB register format into the kernel debugger register
258 * format.
259 */
260 void
261 RemoteGDB::setregs()
262 {
263
264 DPRINTF(GDBAcc, "setregs in remotegdb \n");
265 if (inAArch64(context)) { // AArch64
266 // x0-x30
267 for (int i = 0; i < 31; ++i)
268 context->setIntReg(INTREG_X0 + i, gdbregs.regs64[GDB64_X0 + i]);
269 // pc
270 context->pcState(gdbregs.regs64[GDB64_PC]);
271 // cpsr
272 context->setMiscRegNoEffect(MISCREG_CPSR, gdbregs.regs64[GDB64_CPSR]);
273 // Update the stack pointer. This should be done after
274 // updating CPSR/PSTATE since that might affect how SPX gets
275 // mapped.
276 context->setIntReg(INTREG_SPX, gdbregs.regs64[GDB64_SPX]);
277 // v0-v31
278 for (int i = 0; i < 128; i += 4) {
279 int gdboff = GDB64_V0_32 + i;
280 context->setFloatRegBits(i + 2, gdbregs.regs32[gdboff + 0]);
281 context->setFloatRegBits(i + 3, gdbregs.regs32[gdboff + 1]);
282 context->setFloatRegBits(i + 0, gdbregs.regs32[gdboff + 2]);
283 context->setFloatRegBits(i + 1, gdbregs.regs32[gdboff + 3]);
284 }
285 } else { // AArch32
286 // R0-R15 supervisor mode
287 // arm registers are 32 bits wide, gdb registers are 64 bits wide
288 // two arm registers are packed into one gdb register (little endian)
289 context->setIntReg(INTREG_R0, gdbregs.regs32[GDB32_R0 + 0]);
290 context->setIntReg(INTREG_R1, gdbregs.regs32[GDB32_R0 + 1]);
291 context->setIntReg(INTREG_R2, gdbregs.regs32[GDB32_R0 + 2]);
292 context->setIntReg(INTREG_R3, gdbregs.regs32[GDB32_R0 + 3]);
293 context->setIntReg(INTREG_R4, gdbregs.regs32[GDB32_R0 + 4]);
294 context->setIntReg(INTREG_R5, gdbregs.regs32[GDB32_R0 + 5]);
295 context->setIntReg(INTREG_R6, gdbregs.regs32[GDB32_R0 + 6]);
296 context->setIntReg(INTREG_R7, gdbregs.regs32[GDB32_R0 + 7]);
297 context->setIntReg(INTREG_R8, gdbregs.regs32[GDB32_R0 + 8]);
298 context->setIntReg(INTREG_R9, gdbregs.regs32[GDB32_R0 + 9]);
299 context->setIntReg(INTREG_R10, gdbregs.regs32[GDB32_R0 + 10]);
300 context->setIntReg(INTREG_R11, gdbregs.regs32[GDB32_R0 + 11]);
301 context->setIntReg(INTREG_R12, gdbregs.regs32[GDB32_R0 + 12]);
302 context->setIntReg(INTREG_SP, gdbregs.regs32[GDB32_R0 + 13]);
303 context->setIntReg(INTREG_LR, gdbregs.regs32[GDB32_R0 + 14]);
304 context->pcState(gdbregs.regs32[GDB32_R0 + 7]);
305
306 //CPSR
307 context->setMiscRegNoEffect(MISCREG_CPSR, gdbregs.regs32[GDB32_CPSR]);
308
309 //vfpv3/neon floating point registers (32 double or 64 float)
310 for (int i = 0; i < NumFloatV7ArchRegs; ++i)
311 context->setFloatRegBits(i, gdbregs.regs32[GDB32_F0 + i]);
312
313 //FPSCR
314 context->setMiscReg(MISCREG_FPSCR, gdbregs.regs32[GDB32_FPSCR]);
315 }
316 }
317
318 // Write bytes to kernel address space for debugger.
319 bool
320 RemoteGDB::write(Addr vaddr, size_t size, const char *data)
321 {
322 return BaseRemoteGDB::write(vaddr, size, data);
323 }
324