Update copyright year in most headers.
[deliverable/binutils-gdb.git] / gdb / gdbserver / linux-arm-low.c
1 /* GNU/Linux/ARM specific low level interface, for the remote server for GDB.
2 Copyright (C) 1995, 1996, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
3 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19
20 #include "server.h"
21 #include "linux-low.h"
22
23 #include <elf.h>
24 #include <sys/ptrace.h>
25
26 #include "gdb_proc_service.h"
27
28 /* Defined in auto-generated files. */
29 void init_registers_arm (void);
30 void init_registers_arm_with_iwmmxt (void);
31 void init_registers_arm_with_vfpv2 (void);
32 void init_registers_arm_with_vfpv3 (void);
33 void init_registers_arm_with_neon (void);
34
35 #ifndef PTRACE_GET_THREAD_AREA
36 #define PTRACE_GET_THREAD_AREA 22
37 #endif
38
39 #ifndef PTRACE_GETWMMXREGS
40 # define PTRACE_GETWMMXREGS 18
41 # define PTRACE_SETWMMXREGS 19
42 #endif
43
44 #ifndef PTRACE_GETVFPREGS
45 # define PTRACE_GETVFPREGS 27
46 # define PTRACE_SETVFPREGS 28
47 #endif
48
49 static unsigned long arm_hwcap;
50
51 /* These are in <asm/elf.h> in current kernels. */
52 #define HWCAP_VFP 64
53 #define HWCAP_IWMMXT 512
54 #define HWCAP_NEON 4096
55 #define HWCAP_VFPv3 8192
56 #define HWCAP_VFPv3D16 16384
57
58 #ifdef HAVE_SYS_REG_H
59 #include <sys/reg.h>
60 #endif
61
62 #define arm_num_regs 26
63
64 static int arm_regmap[] = {
65 0, 4, 8, 12, 16, 20, 24, 28,
66 32, 36, 40, 44, 48, 52, 56, 60,
67 -1, -1, -1, -1, -1, -1, -1, -1, -1,
68 64
69 };
70
71 static int
72 arm_cannot_store_register (int regno)
73 {
74 return (regno >= arm_num_regs);
75 }
76
77 static int
78 arm_cannot_fetch_register (int regno)
79 {
80 return (regno >= arm_num_regs);
81 }
82
83 static void
84 arm_fill_gregset (void *buf)
85 {
86 int i;
87
88 for (i = 0; i < arm_num_regs; i++)
89 if (arm_regmap[i] != -1)
90 collect_register (i, ((char *) buf) + arm_regmap[i]);
91 }
92
93 static void
94 arm_store_gregset (const void *buf)
95 {
96 int i;
97 char zerobuf[8];
98
99 memset (zerobuf, 0, 8);
100 for (i = 0; i < arm_num_regs; i++)
101 if (arm_regmap[i] != -1)
102 supply_register (i, ((char *) buf) + arm_regmap[i]);
103 else
104 supply_register (i, zerobuf);
105 }
106
107 static void
108 arm_fill_wmmxregset (void *buf)
109 {
110 int i;
111
112 if (!(arm_hwcap & HWCAP_IWMMXT))
113 return;
114
115 for (i = 0; i < 16; i++)
116 collect_register (arm_num_regs + i, (char *) buf + i * 8);
117
118 /* We only have access to wcssf, wcasf, and wcgr0-wcgr3. */
119 for (i = 0; i < 6; i++)
120 collect_register (arm_num_regs + i + 16, (char *) buf + 16 * 8 + i * 4);
121 }
122
123 static void
124 arm_store_wmmxregset (const void *buf)
125 {
126 int i;
127
128 if (!(arm_hwcap & HWCAP_IWMMXT))
129 return;
130
131 for (i = 0; i < 16; i++)
132 supply_register (arm_num_regs + i, (char *) buf + i * 8);
133
134 /* We only have access to wcssf, wcasf, and wcgr0-wcgr3. */
135 for (i = 0; i < 6; i++)
136 supply_register (arm_num_regs + i + 16, (char *) buf + 16 * 8 + i * 4);
137 }
138
139 static void
140 arm_fill_vfpregset (void *buf)
141 {
142 int i, num, base;
143
144 if (!(arm_hwcap & HWCAP_VFP))
145 return;
146
147 if ((arm_hwcap & (HWCAP_VFPv3 | HWCAP_VFPv3D16)) == HWCAP_VFPv3)
148 num = 32;
149 else
150 num = 16;
151
152 base = find_regno ("d0");
153 for (i = 0; i < num; i++)
154 collect_register (base + i, (char *) buf + i * 8);
155
156 collect_register_by_name ("fpscr", (char *) buf + 32 * 8);
157 }
158
159 static void
160 arm_store_vfpregset (const void *buf)
161 {
162 int i, num, base;
163
164 if (!(arm_hwcap & HWCAP_VFP))
165 return;
166
167 if ((arm_hwcap & (HWCAP_VFPv3 | HWCAP_VFPv3D16)) == HWCAP_VFPv3)
168 num = 32;
169 else
170 num = 16;
171
172 base = find_regno ("d0");
173 for (i = 0; i < num; i++)
174 supply_register (base + i, (char *) buf + i * 8);
175
176 supply_register_by_name ("fpscr", (char *) buf + 32 * 8);
177 }
178
179 extern int debug_threads;
180
181 static CORE_ADDR
182 arm_get_pc ()
183 {
184 unsigned long pc;
185 collect_register_by_name ("pc", &pc);
186 if (debug_threads)
187 fprintf (stderr, "stop pc is %08lx\n", pc);
188 return pc;
189 }
190
191 static void
192 arm_set_pc (CORE_ADDR pc)
193 {
194 unsigned long newpc = pc;
195 supply_register_by_name ("pc", &newpc);
196 }
197
198 /* Correct in either endianness. */
199 static const unsigned long arm_breakpoint = 0xef9f0001;
200 #define arm_breakpoint_len 4
201 static const unsigned short thumb_breakpoint = 0xde01;
202 #define thumb_breakpoint_len 2
203
204 /* For new EABI binaries. We recognize it regardless of which ABI
205 is used for gdbserver, so single threaded debugging should work
206 OK, but for multi-threaded debugging we only insert the current
207 ABI's breakpoint instruction. For now at least. */
208 static const unsigned long arm_eabi_breakpoint = 0xe7f001f0;
209
210 static int
211 arm_breakpoint_at (CORE_ADDR where)
212 {
213 unsigned long cpsr;
214
215 collect_register_by_name ("cpsr", &cpsr);
216
217 if (cpsr & 0x20)
218 {
219 /* Thumb mode. */
220 unsigned short insn;
221
222 (*the_target->read_memory) (where, (unsigned char *) &insn, 2);
223 if (insn == thumb_breakpoint)
224 return 1;
225 }
226 else
227 {
228 /* ARM mode. */
229 unsigned long insn;
230
231 (*the_target->read_memory) (where, (unsigned char *) &insn, 4);
232 if (insn == arm_breakpoint)
233 return 1;
234
235 if (insn == arm_eabi_breakpoint)
236 return 1;
237 }
238
239 return 0;
240 }
241
242 /* We only place breakpoints in empty marker functions, and thread locking
243 is outside of the function. So rather than importing software single-step,
244 we can just run until exit. */
245 static CORE_ADDR
246 arm_reinsert_addr ()
247 {
248 unsigned long pc;
249 collect_register_by_name ("lr", &pc);
250 return pc;
251 }
252
253 /* Fetch the thread-local storage pointer for libthread_db. */
254
255 ps_err_e
256 ps_get_thread_area (const struct ps_prochandle *ph,
257 lwpid_t lwpid, int idx, void **base)
258 {
259 if (ptrace (PTRACE_GET_THREAD_AREA, lwpid, NULL, base) != 0)
260 return PS_ERR;
261
262 /* IDX is the bias from the thread pointer to the beginning of the
263 thread descriptor. It has to be subtracted due to implementation
264 quirks in libthread_db. */
265 *base = (void *) ((char *)*base - idx);
266
267 return PS_OK;
268 }
269
270 static int
271 arm_get_hwcap (unsigned long *valp)
272 {
273 unsigned char *data = alloca (8);
274 int offset = 0;
275
276 while ((*the_target->read_auxv) (offset, data, 8) == 8)
277 {
278 unsigned int *data_p = (unsigned int *)data;
279 if (data_p[0] == AT_HWCAP)
280 {
281 *valp = data_p[1];
282 return 1;
283 }
284
285 offset += 8;
286 }
287
288 *valp = 0;
289 return 0;
290 }
291
292 static void
293 arm_arch_setup (void)
294 {
295 arm_hwcap = 0;
296 if (arm_get_hwcap (&arm_hwcap) == 0)
297 {
298 init_registers_arm ();
299 return;
300 }
301
302 if (arm_hwcap & HWCAP_IWMMXT)
303 {
304 init_registers_arm_with_iwmmxt ();
305 return;
306 }
307
308 if (arm_hwcap & HWCAP_VFP)
309 {
310 int pid;
311 char *buf;
312
313 /* NEON implies either no VFP, or VFPv3-D32. We only support
314 it with VFP. */
315 if (arm_hwcap & HWCAP_NEON)
316 init_registers_arm_with_neon ();
317 else if ((arm_hwcap & (HWCAP_VFPv3 | HWCAP_VFPv3D16)) == HWCAP_VFPv3)
318 init_registers_arm_with_vfpv3 ();
319 else
320 init_registers_arm_with_vfpv2 ();
321
322 /* Now make sure that the kernel supports reading these
323 registers. Support was added in 2.6.30. */
324 pid = lwpid_of (get_thread_lwp (current_inferior));
325 errno = 0;
326 buf = malloc (32 * 8 + 4);
327 if (ptrace (PTRACE_GETVFPREGS, pid, 0, buf) < 0
328 && errno == EIO)
329 {
330 arm_hwcap = 0;
331 init_registers_arm ();
332 }
333 free (buf);
334
335 return;
336 }
337
338 /* The default configuration uses legacy FPA registers, probably
339 simulated. */
340 init_registers_arm ();
341 }
342
343 struct regset_info target_regsets[] = {
344 { PTRACE_GETREGS, PTRACE_SETREGS, 18 * 4,
345 GENERAL_REGS,
346 arm_fill_gregset, arm_store_gregset },
347 { PTRACE_GETWMMXREGS, PTRACE_SETWMMXREGS, 16 * 8 + 6 * 4,
348 EXTENDED_REGS,
349 arm_fill_wmmxregset, arm_store_wmmxregset },
350 { PTRACE_GETVFPREGS, PTRACE_SETVFPREGS, 32 * 8 + 4,
351 EXTENDED_REGS,
352 arm_fill_vfpregset, arm_store_vfpregset },
353 { 0, 0, -1, -1, NULL, NULL }
354 };
355
356 struct linux_target_ops the_low_target = {
357 arm_arch_setup,
358 arm_num_regs,
359 arm_regmap,
360 arm_cannot_fetch_register,
361 arm_cannot_store_register,
362 arm_get_pc,
363 arm_set_pc,
364
365 /* Define an ARM-mode breakpoint; we only set breakpoints in the C
366 library, which is most likely to be ARM. If the kernel supports
367 clone events, we will never insert a breakpoint, so even a Thumb
368 C library will work; so will mixing EABI/non-EABI gdbserver and
369 application. */
370 #ifndef __ARM_EABI__
371 (const unsigned char *) &arm_breakpoint,
372 #else
373 (const unsigned char *) &arm_eabi_breakpoint,
374 #endif
375 arm_breakpoint_len,
376 arm_reinsert_addr,
377 0,
378 arm_breakpoint_at,
379 };
This page took 0.046475 seconds and 4 git commands to generate.