85be4d9f14dd904d3820ce3054fa1cba241cc582
[deliverable/binutils-gdb.git] / gdb / arm-linux-nat.c
1 /* GNU/Linux on ARM native support.
2 Copyright (C) 1999, 2000, 2001, 2002, 2004, 2005, 2006, 2007
3 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 2 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, write to the Free Software
19 Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 Boston, MA 02110-1301, USA. */
21
22 #include "defs.h"
23 #include "inferior.h"
24 #include "gdbcore.h"
25 #include "gdb_string.h"
26 #include "regcache.h"
27 #include "target.h"
28 #include "linux-nat.h"
29 #include "target-descriptions.h"
30 #include "xml-support.h"
31
32 #include "arm-tdep.h"
33 #include "arm-linux-tdep.h"
34
35 #include <sys/user.h>
36 #include <sys/ptrace.h>
37 #include <sys/utsname.h>
38 #include <sys/procfs.h>
39
40 /* Prototypes for supply_gregset etc. */
41 #include "gregset.h"
42
43 /* Defines ps_err_e, struct ps_prochandle. */
44 #include "gdb_proc_service.h"
45
46 #ifndef PTRACE_GET_THREAD_AREA
47 #define PTRACE_GET_THREAD_AREA 22
48 #endif
49
50 #ifndef PTRACE_GETWMMXREGS
51 #define PTRACE_GETWMMXREGS 18
52 #define PTRACE_SETWMMXREGS 19
53 #endif
54
55 /* A flag for whether the WMMX registers are available. */
56 static int arm_linux_has_wmmx_registers;
57
58 extern int arm_apcs_32;
59
60 /* The following variables are used to determine the version of the
61 underlying GNU/Linux operating system. Examples:
62
63 GNU/Linux 2.0.35 GNU/Linux 2.2.12
64 os_version = 0x00020023 os_version = 0x0002020c
65 os_major = 2 os_major = 2
66 os_minor = 0 os_minor = 2
67 os_release = 35 os_release = 12
68
69 Note: os_version = (os_major << 16) | (os_minor << 8) | os_release
70
71 These are initialized using get_linux_version() from
72 _initialize_arm_linux_nat(). */
73
74 static unsigned int os_version, os_major, os_minor, os_release;
75
76 /* On GNU/Linux, threads are implemented as pseudo-processes, in which
77 case we may be tracing more than one process at a time. In that
78 case, inferior_ptid will contain the main process ID and the
79 individual thread (process) ID. get_thread_id () is used to get
80 the thread id if it's available, and the process id otherwise. */
81
82 int
83 get_thread_id (ptid_t ptid)
84 {
85 int tid = TIDGET (ptid);
86 if (0 == tid)
87 tid = PIDGET (ptid);
88 return tid;
89 }
90 #define GET_THREAD_ID(PTID) get_thread_id (PTID)
91
92 /* Get the value of a particular register from the floating point
93 state of the process and store it into regcache. */
94
95 static void
96 fetch_fpregister (struct regcache *regcache, int regno)
97 {
98 int ret, tid;
99 gdb_byte fp[ARM_LINUX_SIZEOF_NWFPE];
100
101 /* Get the thread id for the ptrace call. */
102 tid = GET_THREAD_ID (inferior_ptid);
103
104 /* Read the floating point state. */
105 ret = ptrace (PT_GETFPREGS, tid, 0, fp);
106 if (ret < 0)
107 {
108 warning (_("Unable to fetch floating point register."));
109 return;
110 }
111
112 /* Fetch fpsr. */
113 if (ARM_FPS_REGNUM == regno)
114 regcache_raw_supply (regcache, ARM_FPS_REGNUM,
115 fp + NWFPE_FPSR_OFFSET);
116
117 /* Fetch the floating point register. */
118 if (regno >= ARM_F0_REGNUM && regno <= ARM_F7_REGNUM)
119 supply_nwfpe_register (regcache, regno, fp);
120 }
121
122 /* Get the whole floating point state of the process and store it
123 into regcache. */
124
125 static void
126 fetch_fpregs (struct regcache *regcache)
127 {
128 int ret, regno, tid;
129 gdb_byte fp[ARM_LINUX_SIZEOF_NWFPE];
130
131 /* Get the thread id for the ptrace call. */
132 tid = GET_THREAD_ID (inferior_ptid);
133
134 /* Read the floating point state. */
135 ret = ptrace (PT_GETFPREGS, tid, 0, fp);
136 if (ret < 0)
137 {
138 warning (_("Unable to fetch the floating point registers."));
139 return;
140 }
141
142 /* Fetch fpsr. */
143 regcache_raw_supply (regcache, ARM_FPS_REGNUM,
144 fp + NWFPE_FPSR_OFFSET);
145
146 /* Fetch the floating point registers. */
147 for (regno = ARM_F0_REGNUM; regno <= ARM_F7_REGNUM; regno++)
148 supply_nwfpe_register (regcache, regno, fp);
149 }
150
151 /* Save a particular register into the floating point state of the
152 process using the contents from regcache. */
153
154 static void
155 store_fpregister (const struct regcache *regcache, int regno)
156 {
157 int ret, tid;
158 gdb_byte fp[ARM_LINUX_SIZEOF_NWFPE];
159
160 /* Get the thread id for the ptrace call. */
161 tid = GET_THREAD_ID (inferior_ptid);
162
163 /* Read the floating point state. */
164 ret = ptrace (PT_GETFPREGS, tid, 0, fp);
165 if (ret < 0)
166 {
167 warning (_("Unable to fetch the floating point registers."));
168 return;
169 }
170
171 /* Store fpsr. */
172 if (ARM_FPS_REGNUM == regno && regcache_valid_p (regcache, ARM_FPS_REGNUM))
173 regcache_raw_collect (regcache, ARM_FPS_REGNUM, fp + NWFPE_FPSR_OFFSET);
174
175 /* Store the floating point register. */
176 if (regno >= ARM_F0_REGNUM && regno <= ARM_F7_REGNUM)
177 collect_nwfpe_register (regcache, regno, fp);
178
179 ret = ptrace (PTRACE_SETFPREGS, tid, 0, fp);
180 if (ret < 0)
181 {
182 warning (_("Unable to store floating point register."));
183 return;
184 }
185 }
186
187 /* Save the whole floating point state of the process using
188 the contents from regcache. */
189
190 static void
191 store_fpregs (const struct regcache *regcache)
192 {
193 int ret, regno, tid;
194 gdb_byte fp[ARM_LINUX_SIZEOF_NWFPE];
195
196 /* Get the thread id for the ptrace call. */
197 tid = GET_THREAD_ID (inferior_ptid);
198
199 /* Read the floating point state. */
200 ret = ptrace (PT_GETFPREGS, tid, 0, fp);
201 if (ret < 0)
202 {
203 warning (_("Unable to fetch the floating point registers."));
204 return;
205 }
206
207 /* Store fpsr. */
208 if (regcache_valid_p (regcache, ARM_FPS_REGNUM))
209 regcache_raw_collect (regcache, ARM_FPS_REGNUM, fp + NWFPE_FPSR_OFFSET);
210
211 /* Store the floating point registers. */
212 for (regno = ARM_F0_REGNUM; regno <= ARM_F7_REGNUM; regno++)
213 if (regcache_valid_p (regcache, regno))
214 collect_nwfpe_register (regcache, regno, fp);
215
216 ret = ptrace (PTRACE_SETFPREGS, tid, 0, fp);
217 if (ret < 0)
218 {
219 warning (_("Unable to store floating point registers."));
220 return;
221 }
222 }
223
224 /* Fetch a general register of the process and store into
225 regcache. */
226
227 static void
228 fetch_register (struct regcache *regcache, int regno)
229 {
230 int ret, tid;
231 elf_gregset_t regs;
232
233 /* Get the thread id for the ptrace call. */
234 tid = GET_THREAD_ID (inferior_ptid);
235
236 ret = ptrace (PTRACE_GETREGS, tid, 0, &regs);
237 if (ret < 0)
238 {
239 warning (_("Unable to fetch general register."));
240 return;
241 }
242
243 if (regno >= ARM_A1_REGNUM && regno < ARM_PC_REGNUM)
244 regcache_raw_supply (regcache, regno, (char *) &regs[regno]);
245
246 if (ARM_PS_REGNUM == regno)
247 {
248 if (arm_apcs_32)
249 regcache_raw_supply (regcache, ARM_PS_REGNUM,
250 (char *) &regs[ARM_CPSR_REGNUM]);
251 else
252 regcache_raw_supply (regcache, ARM_PS_REGNUM,
253 (char *) &regs[ARM_PC_REGNUM]);
254 }
255
256 if (ARM_PC_REGNUM == regno)
257 {
258 regs[ARM_PC_REGNUM] = gdbarch_addr_bits_remove
259 (current_gdbarch, regs[ARM_PC_REGNUM]);
260 regcache_raw_supply (regcache, ARM_PC_REGNUM,
261 (char *) &regs[ARM_PC_REGNUM]);
262 }
263 }
264
265 /* Fetch all general registers of the process and store into
266 regcache. */
267
268 static void
269 fetch_regs (struct regcache *regcache)
270 {
271 int ret, regno, tid;
272 elf_gregset_t regs;
273
274 /* Get the thread id for the ptrace call. */
275 tid = GET_THREAD_ID (inferior_ptid);
276
277 ret = ptrace (PTRACE_GETREGS, tid, 0, &regs);
278 if (ret < 0)
279 {
280 warning (_("Unable to fetch general registers."));
281 return;
282 }
283
284 for (regno = ARM_A1_REGNUM; regno < ARM_PC_REGNUM; regno++)
285 regcache_raw_supply (regcache, regno, (char *) &regs[regno]);
286
287 if (arm_apcs_32)
288 regcache_raw_supply (regcache, ARM_PS_REGNUM,
289 (char *) &regs[ARM_CPSR_REGNUM]);
290 else
291 regcache_raw_supply (regcache, ARM_PS_REGNUM,
292 (char *) &regs[ARM_PC_REGNUM]);
293
294 regs[ARM_PC_REGNUM] = gdbarch_addr_bits_remove
295 (current_gdbarch, regs[ARM_PC_REGNUM]);
296 regcache_raw_supply (regcache, ARM_PC_REGNUM,
297 (char *) &regs[ARM_PC_REGNUM]);
298 }
299
300 /* Store all general registers of the process from the values in
301 regcache. */
302
303 static void
304 store_register (const struct regcache *regcache, int regno)
305 {
306 int ret, tid;
307 elf_gregset_t regs;
308
309 if (!regcache_valid_p (regcache, regno))
310 return;
311
312 /* Get the thread id for the ptrace call. */
313 tid = GET_THREAD_ID (inferior_ptid);
314
315 /* Get the general registers from the process. */
316 ret = ptrace (PTRACE_GETREGS, tid, 0, &regs);
317 if (ret < 0)
318 {
319 warning (_("Unable to fetch general registers."));
320 return;
321 }
322
323 if (regno >= ARM_A1_REGNUM && regno <= ARM_PC_REGNUM)
324 regcache_raw_collect (regcache, regno, (char *) &regs[regno]);
325 else if (arm_apcs_32 && regno == ARM_PS_REGNUM)
326 regcache_raw_collect (regcache, regno,
327 (char *) &regs[ARM_CPSR_REGNUM]);
328 else if (!arm_apcs_32 && regno == ARM_PS_REGNUM)
329 regcache_raw_collect (regcache, ARM_PC_REGNUM,
330 (char *) &regs[ARM_PC_REGNUM]);
331
332 ret = ptrace (PTRACE_SETREGS, tid, 0, &regs);
333 if (ret < 0)
334 {
335 warning (_("Unable to store general register."));
336 return;
337 }
338 }
339
340 static void
341 store_regs (const struct regcache *regcache)
342 {
343 int ret, regno, tid;
344 elf_gregset_t regs;
345
346 /* Get the thread id for the ptrace call. */
347 tid = GET_THREAD_ID (inferior_ptid);
348
349 /* Fetch the general registers. */
350 ret = ptrace (PTRACE_GETREGS, tid, 0, &regs);
351 if (ret < 0)
352 {
353 warning (_("Unable to fetch general registers."));
354 return;
355 }
356
357 for (regno = ARM_A1_REGNUM; regno <= ARM_PC_REGNUM; regno++)
358 {
359 if (regcache_valid_p (regcache, regno))
360 regcache_raw_collect (regcache, regno, (char *) &regs[regno]);
361 }
362
363 if (arm_apcs_32 && regcache_valid_p (regcache, ARM_PS_REGNUM))
364 regcache_raw_collect (regcache, ARM_PS_REGNUM,
365 (char *) &regs[ARM_CPSR_REGNUM]);
366
367 ret = ptrace (PTRACE_SETREGS, tid, 0, &regs);
368
369 if (ret < 0)
370 {
371 warning (_("Unable to store general registers."));
372 return;
373 }
374 }
375
376 /* Fetch all WMMX registers of the process and store into
377 regcache. */
378
379 #define IWMMXT_REGS_SIZE (16 * 8 + 6 * 4)
380
381 static void
382 fetch_wmmx_regs (struct regcache *regcache)
383 {
384 char regbuf[IWMMXT_REGS_SIZE];
385 int ret, regno, tid;
386
387 /* Get the thread id for the ptrace call. */
388 tid = GET_THREAD_ID (inferior_ptid);
389
390 ret = ptrace (PTRACE_GETWMMXREGS, tid, 0, regbuf);
391 if (ret < 0)
392 {
393 warning (_("Unable to fetch WMMX registers."));
394 return;
395 }
396
397 for (regno = 0; regno < 16; regno++)
398 regcache_raw_supply (regcache, regno + ARM_WR0_REGNUM,
399 &regbuf[regno * 8]);
400
401 for (regno = 0; regno < 2; regno++)
402 regcache_raw_supply (regcache, regno + ARM_WCSSF_REGNUM,
403 &regbuf[16 * 8 + regno * 4]);
404
405 for (regno = 0; regno < 4; regno++)
406 regcache_raw_supply (regcache, regno + ARM_WCGR0_REGNUM,
407 &regbuf[16 * 8 + 2 * 4 + regno * 4]);
408 }
409
410 static void
411 store_wmmx_regs (const struct regcache *regcache)
412 {
413 char regbuf[IWMMXT_REGS_SIZE];
414 int ret, regno, tid;
415
416 /* Get the thread id for the ptrace call. */
417 tid = GET_THREAD_ID (inferior_ptid);
418
419 ret = ptrace (PTRACE_GETWMMXREGS, tid, 0, regbuf);
420 if (ret < 0)
421 {
422 warning (_("Unable to fetch WMMX registers."));
423 return;
424 }
425
426 for (regno = 0; regno < 16; regno++)
427 if (regcache_valid_p (regcache, regno + ARM_WR0_REGNUM))
428 regcache_raw_collect (regcache, regno + ARM_WR0_REGNUM,
429 &regbuf[regno * 8]);
430
431 for (regno = 0; regno < 2; regno++)
432 if (regcache_valid_p (regcache, regno + ARM_WCSSF_REGNUM))
433 regcache_raw_collect (regcache, regno + ARM_WCSSF_REGNUM,
434 &regbuf[16 * 8 + regno * 4]);
435
436 for (regno = 0; regno < 4; regno++)
437 if (regcache_valid_p (regcache, regno + ARM_WCGR0_REGNUM))
438 regcache_raw_collect (regcache, regno + ARM_WCGR0_REGNUM,
439 &regbuf[16 * 8 + 2 * 4 + regno * 4]);
440
441 ret = ptrace (PTRACE_SETWMMXREGS, tid, 0, regbuf);
442
443 if (ret < 0)
444 {
445 warning (_("Unable to store WMMX registers."));
446 return;
447 }
448 }
449
450 /* Fetch registers from the child process. Fetch all registers if
451 regno == -1, otherwise fetch all general registers or all floating
452 point registers depending upon the value of regno. */
453
454 static void
455 arm_linux_fetch_inferior_registers (struct regcache *regcache, int regno)
456 {
457 if (-1 == regno)
458 {
459 fetch_regs (regcache);
460 fetch_fpregs (regcache);
461 if (arm_linux_has_wmmx_registers)
462 fetch_wmmx_regs (regcache);
463 }
464 else
465 {
466 if (regno < ARM_F0_REGNUM || regno == ARM_PS_REGNUM)
467 fetch_register (regcache, regno);
468 else if (regno >= ARM_F0_REGNUM && regno <= ARM_FPS_REGNUM)
469 fetch_fpregister (regcache, regno);
470 else if (arm_linux_has_wmmx_registers
471 && regno >= ARM_WR0_REGNUM && regno <= ARM_WCGR7_REGNUM)
472 fetch_wmmx_regs (regcache);
473 }
474 }
475
476 /* Store registers back into the inferior. Store all registers if
477 regno == -1, otherwise store all general registers or all floating
478 point registers depending upon the value of regno. */
479
480 static void
481 arm_linux_store_inferior_registers (struct regcache *regcache, int regno)
482 {
483 if (-1 == regno)
484 {
485 store_regs (regcache);
486 store_fpregs (regcache);
487 if (arm_linux_has_wmmx_registers)
488 store_wmmx_regs (regcache);
489 }
490 else
491 {
492 if (regno < ARM_F0_REGNUM || regno == ARM_PS_REGNUM)
493 store_register (regcache, regno);
494 else if ((regno >= ARM_F0_REGNUM) && (regno <= ARM_FPS_REGNUM))
495 store_fpregister (regcache, regno);
496 else if (arm_linux_has_wmmx_registers
497 && regno >= ARM_WR0_REGNUM && regno <= ARM_WCGR7_REGNUM)
498 store_wmmx_regs (regcache);
499 }
500 }
501
502 /* Wrapper functions for the standard regset handling, used by
503 thread debugging. */
504
505 void
506 fill_gregset (const struct regcache *regcache,
507 gdb_gregset_t *gregsetp, int regno)
508 {
509 arm_linux_collect_gregset (NULL, regcache, regno, gregsetp, 0);
510 }
511
512 void
513 supply_gregset (struct regcache *regcache, const gdb_gregset_t *gregsetp)
514 {
515 arm_linux_supply_gregset (NULL, regcache, -1, gregsetp, 0);
516 }
517
518 void
519 fill_fpregset (const struct regcache *regcache,
520 gdb_fpregset_t *fpregsetp, int regno)
521 {
522 arm_linux_collect_nwfpe (NULL, regcache, regno, fpregsetp, 0);
523 }
524
525 /* Fill GDB's register array with the floating-point register values
526 in *fpregsetp. */
527
528 void
529 supply_fpregset (struct regcache *regcache, const gdb_fpregset_t *fpregsetp)
530 {
531 arm_linux_supply_nwfpe (NULL, regcache, -1, fpregsetp, 0);
532 }
533
534 /* Fetch the thread-local storage pointer for libthread_db. */
535
536 ps_err_e
537 ps_get_thread_area (const struct ps_prochandle *ph,
538 lwpid_t lwpid, int idx, void **base)
539 {
540 if (ptrace (PTRACE_GET_THREAD_AREA, lwpid, NULL, base) != 0)
541 return PS_ERR;
542
543 /* IDX is the bias from the thread pointer to the beginning of the
544 thread descriptor. It has to be subtracted due to implementation
545 quirks in libthread_db. */
546 *base = (void *) ((char *)*base - idx);
547
548 return PS_OK;
549 }
550
551 static unsigned int
552 get_linux_version (unsigned int *vmajor,
553 unsigned int *vminor,
554 unsigned int *vrelease)
555 {
556 struct utsname info;
557 char *pmajor, *pminor, *prelease, *tail;
558
559 if (-1 == uname (&info))
560 {
561 warning (_("Unable to determine GNU/Linux version."));
562 return -1;
563 }
564
565 pmajor = strtok (info.release, ".");
566 pminor = strtok (NULL, ".");
567 prelease = strtok (NULL, ".");
568
569 *vmajor = (unsigned int) strtoul (pmajor, &tail, 0);
570 *vminor = (unsigned int) strtoul (pminor, &tail, 0);
571 *vrelease = (unsigned int) strtoul (prelease, &tail, 0);
572
573 return ((*vmajor << 16) | (*vminor << 8) | *vrelease);
574 }
575
576 static LONGEST (*super_xfer_partial) (struct target_ops *, enum target_object,
577 const char *, gdb_byte *, const gdb_byte *,
578 ULONGEST, LONGEST);
579
580 static LONGEST
581 arm_linux_xfer_partial (struct target_ops *ops,
582 enum target_object object,
583 const char *annex,
584 gdb_byte *readbuf, const gdb_byte *writebuf,
585 ULONGEST offset, LONGEST len)
586 {
587 if (object == TARGET_OBJECT_AVAILABLE_FEATURES)
588 {
589 if (annex != NULL && strcmp (annex, "target.xml") == 0)
590 {
591 int ret;
592 char regbuf[IWMMXT_REGS_SIZE];
593
594 ret = ptrace (PTRACE_GETWMMXREGS, GET_THREAD_ID (inferior_ptid),
595 0, regbuf);
596 if (ret < 0)
597 arm_linux_has_wmmx_registers = 0;
598 else
599 arm_linux_has_wmmx_registers = 1;
600
601 if (arm_linux_has_wmmx_registers)
602 annex = "arm-with-iwmmxt.xml";
603 else
604 return -1;
605 }
606
607 return xml_builtin_xfer_partial (annex, readbuf, writebuf, offset, len);
608 }
609
610 return super_xfer_partial (ops, object, annex, readbuf, writebuf,
611 offset, len);
612 }
613
614 void _initialize_arm_linux_nat (void);
615
616 void
617 _initialize_arm_linux_nat (void)
618 {
619 struct target_ops *t;
620
621 os_version = get_linux_version (&os_major, &os_minor, &os_release);
622
623 /* Fill in the generic GNU/Linux methods. */
624 t = linux_target ();
625
626 /* Add our register access methods. */
627 t->to_fetch_registers = arm_linux_fetch_inferior_registers;
628 t->to_store_registers = arm_linux_store_inferior_registers;
629
630 /* Override the default to_xfer_partial. */
631 super_xfer_partial = t->to_xfer_partial;
632 t->to_xfer_partial = arm_linux_xfer_partial;
633
634 /* Register the target. */
635 linux_nat_add_target (t);
636 }
This page took 0.053969 seconds and 4 git commands to generate.