]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/arm-linux-nat.c
2002-01-07 Michael Snyder <msnyder@redhat.com>
[thirdparty/binutils-gdb.git] / gdb / arm-linux-nat.c
CommitLineData
ed9a39eb 1/* GNU/Linux on ARM native support.
4e052eda 2 Copyright 1999, 2000, 2001 Free Software Foundation, Inc.
ed9a39eb
JM
3
4 This file is part of GDB.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
20
21#include "defs.h"
22#include "inferior.h"
23#include "gdbcore.h"
24#include "gdb_string.h"
4e052eda 25#include "regcache.h"
ed9a39eb
JM
26
27#include <sys/user.h>
28#include <sys/ptrace.h>
29#include <sys/utsname.h>
41c49b06 30#include <sys/procfs.h>
ed9a39eb 31
c60c0f5f
MS
32/* Prototypes for supply_gregset etc. */
33#include "gregset.h"
34
ed9a39eb
JM
35extern int arm_apcs_32;
36
37#define typeNone 0x00
38#define typeSingle 0x01
39#define typeDouble 0x02
40#define typeExtended 0x03
41#define FPWORDS 28
42#define CPSR_REGNUM 16
43
44typedef union tagFPREG
45 {
46 unsigned int fSingle;
47 unsigned int fDouble[2];
48 unsigned int fExtended[3];
49 }
50FPREG;
51
52typedef struct tagFPA11
53 {
54 FPREG fpreg[8]; /* 8 floating point registers */
55 unsigned int fpsr; /* floating point status register */
56 unsigned int fpcr; /* floating point control register */
57 unsigned char fType[8]; /* type of floating point value held in
58 floating point registers. */
59 int initflag; /* NWFPE initialization flag. */
60 }
61FPA11;
62
63/* The following variables are used to determine the version of the
64 underlying Linux operating system. Examples:
65
66 Linux 2.0.35 Linux 2.2.12
67 os_version = 0x00020023 os_version = 0x0002020c
68 os_major = 2 os_major = 2
69 os_minor = 0 os_minor = 2
70 os_release = 35 os_release = 12
71
72 Note: os_version = (os_major << 16) | (os_minor << 8) | os_release
73
74 These are initialized using get_linux_version() from
75 _initialize_arm_linux_nat(). */
76
77static unsigned int os_version, os_major, os_minor, os_release;
78
41c49b06
SB
79/* On Linux, threads are implemented as pseudo-processes, in which
80 case we may be tracing more than one process at a time. In that
39f77062 81 case, inferior_ptid will contain the main process ID and the
ca6724c1
KB
82 individual thread (process) ID. get_thread_id () is used to
83 get the thread id if it's available, and the process id otherwise. */
41c49b06
SB
84
85int
39f77062 86get_thread_id (ptid_t ptid)
41c49b06 87{
39f77062
KB
88 int tid = TIDGET (ptid);
89 if (0 == tid)
90 tid = PIDGET (ptid);
41c49b06
SB
91 return tid;
92}
39f77062 93#define GET_THREAD_ID(PTID) get_thread_id ((PTID));
41c49b06 94
ed9a39eb 95static void
56624b0a 96fetch_nwfpe_single (unsigned int fn, FPA11 * fpa11)
ed9a39eb
JM
97{
98 unsigned int mem[3];
99
100 mem[0] = fpa11->fpreg[fn].fSingle;
101 mem[1] = 0;
102 mem[2] = 0;
103 supply_register (F0_REGNUM + fn, (char *) &mem[0]);
104}
105
106static void
56624b0a 107fetch_nwfpe_double (unsigned int fn, FPA11 * fpa11)
ed9a39eb
JM
108{
109 unsigned int mem[3];
110
111 mem[0] = fpa11->fpreg[fn].fDouble[1];
112 mem[1] = fpa11->fpreg[fn].fDouble[0];
113 mem[2] = 0;
114 supply_register (F0_REGNUM + fn, (char *) &mem[0]);
115}
116
117static void
56624b0a 118fetch_nwfpe_none (unsigned int fn)
ed9a39eb
JM
119{
120 unsigned int mem[3] =
121 {0, 0, 0};
122
123 supply_register (F0_REGNUM + fn, (char *) &mem[0]);
124}
125
126static void
56624b0a 127fetch_nwfpe_extended (unsigned int fn, FPA11 * fpa11)
ed9a39eb
JM
128{
129 unsigned int mem[3];
130
131 mem[0] = fpa11->fpreg[fn].fExtended[0]; /* sign & exponent */
132 mem[1] = fpa11->fpreg[fn].fExtended[2]; /* ls bits */
133 mem[2] = fpa11->fpreg[fn].fExtended[1]; /* ms bits */
134 supply_register (F0_REGNUM + fn, (char *) &mem[0]);
135}
136
41c49b06
SB
137static void
138fetch_nwfpe_register (int regno, FPA11 * fpa11)
139{
140 int fn = regno - F0_REGNUM;
141
142 switch (fpa11->fType[fn])
143 {
144 case typeSingle:
145 fetch_nwfpe_single (fn, fpa11);
146 break;
147
148 case typeDouble:
149 fetch_nwfpe_double (fn, fpa11);
150 break;
151
152 case typeExtended:
153 fetch_nwfpe_extended (fn, fpa11);
154 break;
155
156 default:
157 fetch_nwfpe_none (fn);
158 }
159}
160
ed9a39eb 161static void
56624b0a 162store_nwfpe_single (unsigned int fn, FPA11 * fpa11)
ed9a39eb
JM
163{
164 unsigned int mem[3];
165
166 read_register_gen (F0_REGNUM + fn, (char *) &mem[0]);
167 fpa11->fpreg[fn].fSingle = mem[0];
168 fpa11->fType[fn] = typeSingle;
169}
170
171static void
56624b0a 172store_nwfpe_double (unsigned int fn, FPA11 * fpa11)
ed9a39eb
JM
173{
174 unsigned int mem[3];
175
176 read_register_gen (F0_REGNUM + fn, (char *) &mem[0]);
177 fpa11->fpreg[fn].fDouble[1] = mem[0];
178 fpa11->fpreg[fn].fDouble[0] = mem[1];
179 fpa11->fType[fn] = typeDouble;
180}
181
182void
56624b0a 183store_nwfpe_extended (unsigned int fn, FPA11 * fpa11)
ed9a39eb
JM
184{
185 unsigned int mem[3];
186
187 read_register_gen (F0_REGNUM + fn, (char *) &mem[0]);
188 fpa11->fpreg[fn].fExtended[0] = mem[0]; /* sign & exponent */
189 fpa11->fpreg[fn].fExtended[2] = mem[1]; /* ls bits */
190 fpa11->fpreg[fn].fExtended[1] = mem[2]; /* ms bits */
191 fpa11->fType[fn] = typeDouble;
192}
193
41c49b06
SB
194void
195store_nwfpe_register (int regno, FPA11 * fpa11)
196{
197 if (register_valid[regno])
198 {
199 unsigned int fn = regno - F0_REGNUM;
200 switch (fpa11->fType[fn])
201 {
202 case typeSingle:
203 store_nwfpe_single (fn, fpa11);
204 break;
205
206 case typeDouble:
207 store_nwfpe_double (fn, fpa11);
208 break;
209
210 case typeExtended:
211 store_nwfpe_extended (fn, fpa11);
212 break;
213 }
214 }
215}
216
217
218/* Get the value of a particular register from the floating point
219 state of the process and store it into registers[]. */
220
221static void
222fetch_fpregister (int regno)
223{
224 int ret, tid;
225 FPA11 fp;
226
227 /* Get the thread id for the ptrace call. */
39f77062 228 tid = GET_THREAD_ID (inferior_ptid);
41c49b06
SB
229
230 /* Read the floating point state. */
231 ret = ptrace (PT_GETFPREGS, tid, 0, &fp);
232 if (ret < 0)
233 {
234 warning ("Unable to fetch floating point register.");
235 return;
236 }
237
238 /* Fetch fpsr. */
239 if (FPS_REGNUM == regno)
240 supply_register (FPS_REGNUM, (char *) &fp.fpsr);
241
242 /* Fetch the floating point register. */
243 if (regno >= F0_REGNUM && regno <= F7_REGNUM)
244 {
245 int fn = regno - F0_REGNUM;
246
247 switch (fp.fType[fn])
248 {
249 case typeSingle:
250 fetch_nwfpe_single (fn, &fp);
251 break;
252
253 case typeDouble:
254 fetch_nwfpe_double (fn, &fp);
255 break;
256
257 case typeExtended:
258 fetch_nwfpe_extended (fn, &fp);
259 break;
260
261 default:
262 fetch_nwfpe_none (fn);
263 }
264 }
265}
266
267/* Get the whole floating point state of the process and store it
268 into registers[]. */
ed9a39eb
JM
269
270static void
271fetch_fpregs (void)
272{
41c49b06 273 int ret, regno, tid;
ed9a39eb
JM
274 FPA11 fp;
275
41c49b06 276 /* Get the thread id for the ptrace call. */
39f77062 277 tid = GET_THREAD_ID (inferior_ptid);
41c49b06 278
ed9a39eb 279 /* Read the floating point state. */
41c49b06 280 ret = ptrace (PT_GETFPREGS, tid, 0, &fp);
ed9a39eb
JM
281 if (ret < 0)
282 {
41c49b06 283 warning ("Unable to fetch the floating point registers.");
ed9a39eb
JM
284 return;
285 }
286
287 /* Fetch fpsr. */
288 supply_register (FPS_REGNUM, (char *) &fp.fpsr);
289
290 /* Fetch the floating point registers. */
291 for (regno = F0_REGNUM; regno <= F7_REGNUM; regno++)
292 {
293 int fn = regno - F0_REGNUM;
ed9a39eb
JM
294
295 switch (fp.fType[fn])
296 {
297 case typeSingle:
56624b0a 298 fetch_nwfpe_single (fn, &fp);
ed9a39eb
JM
299 break;
300
301 case typeDouble:
56624b0a 302 fetch_nwfpe_double (fn, &fp);
ed9a39eb
JM
303 break;
304
305 case typeExtended:
56624b0a 306 fetch_nwfpe_extended (fn, &fp);
ed9a39eb
JM
307 break;
308
309 default:
56624b0a 310 fetch_nwfpe_none (fn);
ed9a39eb
JM
311 }
312 }
313}
314
41c49b06
SB
315/* Save a particular register into the floating point state of the
316 process using the contents from registers[]. */
317
318static void
319store_fpregister (int regno)
320{
321 int ret, tid;
322 FPA11 fp;
323
324 /* Get the thread id for the ptrace call. */
39f77062 325 tid = GET_THREAD_ID (inferior_ptid);
41c49b06
SB
326
327 /* Read the floating point state. */
328 ret = ptrace (PT_GETFPREGS, tid, 0, &fp);
329 if (ret < 0)
330 {
331 warning ("Unable to fetch the floating point registers.");
332 return;
333 }
334
335 /* Store fpsr. */
336 if (FPS_REGNUM == regno && register_valid[FPS_REGNUM])
337 read_register_gen (FPS_REGNUM, (char *) &fp.fpsr);
338
339 /* Store the floating point register. */
340 if (regno >= F0_REGNUM && regno <= F7_REGNUM)
341 {
342 store_nwfpe_register (regno, &fp);
343 }
344
345 ret = ptrace (PTRACE_SETFPREGS, tid, 0, &fp);
346 if (ret < 0)
347 {
348 warning ("Unable to store floating point register.");
349 return;
350 }
351}
352
ed9a39eb
JM
353/* Save the whole floating point state of the process using
354 the contents from registers[]. */
355
356static void
357store_fpregs (void)
358{
41c49b06 359 int ret, regno, tid;
ed9a39eb
JM
360 FPA11 fp;
361
41c49b06 362 /* Get the thread id for the ptrace call. */
39f77062 363 tid = GET_THREAD_ID (inferior_ptid);
41c49b06
SB
364
365 /* Read the floating point state. */
366 ret = ptrace (PT_GETFPREGS, tid, 0, &fp);
367 if (ret < 0)
368 {
369 warning ("Unable to fetch the floating point registers.");
370 return;
371 }
372
ed9a39eb
JM
373 /* Store fpsr. */
374 if (register_valid[FPS_REGNUM])
375 read_register_gen (FPS_REGNUM, (char *) &fp.fpsr);
376
377 /* Store the floating point registers. */
378 for (regno = F0_REGNUM; regno <= F7_REGNUM; regno++)
379 {
41c49b06 380 fetch_nwfpe_register (regno, &fp);
ed9a39eb
JM
381 }
382
41c49b06 383 ret = ptrace (PTRACE_SETFPREGS, tid, 0, &fp);
ed9a39eb
JM
384 if (ret < 0)
385 {
41c49b06 386 warning ("Unable to store floating point registers.");
ed9a39eb
JM
387 return;
388 }
389}
390
41c49b06
SB
391/* Fetch a general register of the process and store into
392 registers[]. */
393
394static void
395fetch_register (int regno)
396{
397 int ret, tid;
c2152441 398 elf_gregset_t regs;
41c49b06
SB
399
400 /* Get the thread id for the ptrace call. */
39f77062 401 tid = GET_THREAD_ID (inferior_ptid);
41c49b06
SB
402
403 ret = ptrace (PTRACE_GETREGS, tid, 0, &regs);
404 if (ret < 0)
405 {
406 warning ("Unable to fetch general register.");
407 return;
408 }
409
410 if (regno >= A1_REGNUM && regno < PC_REGNUM)
c2152441 411 supply_register (regno, (char *) &regs[regno]);
41c49b06
SB
412
413 if (PS_REGNUM == regno)
414 {
415 if (arm_apcs_32)
c2152441 416 supply_register (PS_REGNUM, (char *) &regs[CPSR_REGNUM]);
41c49b06 417 else
c2152441 418 supply_register (PS_REGNUM, (char *) &regs[PC_REGNUM]);
41c49b06
SB
419 }
420
421 if (PC_REGNUM == regno)
422 {
c2152441
DJ
423 regs[PC_REGNUM] = ADDR_BITS_REMOVE (regs[PC_REGNUM]);
424 supply_register (PC_REGNUM, (char *) &regs[PC_REGNUM]);
41c49b06
SB
425 }
426}
427
ed9a39eb
JM
428/* Fetch all general registers of the process and store into
429 registers[]. */
430
431static void
432fetch_regs (void)
433{
41c49b06 434 int ret, regno, tid;
c2152441 435 elf_gregset_t regs;
ed9a39eb 436
41c49b06 437 /* Get the thread id for the ptrace call. */
39f77062 438 tid = GET_THREAD_ID (inferior_ptid);
41c49b06
SB
439
440 ret = ptrace (PTRACE_GETREGS, tid, 0, &regs);
ed9a39eb
JM
441 if (ret < 0)
442 {
443 warning ("Unable to fetch general registers.");
444 return;
445 }
446
447 for (regno = A1_REGNUM; regno < PC_REGNUM; regno++)
c2152441 448 supply_register (regno, (char *) &regs[regno]);
ed9a39eb
JM
449
450 if (arm_apcs_32)
c2152441 451 supply_register (PS_REGNUM, (char *) &regs[CPSR_REGNUM]);
ed9a39eb 452 else
c2152441 453 supply_register (PS_REGNUM, (char *) &regs[PC_REGNUM]);
ed9a39eb 454
c2152441
DJ
455 regs[PC_REGNUM] = ADDR_BITS_REMOVE (regs[PC_REGNUM]);
456 supply_register (PC_REGNUM, (char *) &regs[PC_REGNUM]);
ed9a39eb
JM
457}
458
459/* Store all general registers of the process from the values in
460 registers[]. */
461
41c49b06
SB
462static void
463store_register (int regno)
464{
465 int ret, tid;
c2152441 466 elf_gregset_t regs;
41c49b06
SB
467
468 if (!register_valid[regno])
469 return;
470
471 /* Get the thread id for the ptrace call. */
39f77062 472 tid = GET_THREAD_ID (inferior_ptid);
41c49b06
SB
473
474 /* Get the general registers from the process. */
475 ret = ptrace (PTRACE_GETREGS, tid, 0, &regs);
476 if (ret < 0)
477 {
478 warning ("Unable to fetch general registers.");
479 return;
480 }
481
482 if (regno >= A1_REGNUM && regno <= PC_REGNUM)
c2152441 483 read_register_gen (regno, (char *) &regs[regno]);
41c49b06
SB
484
485 ret = ptrace (PTRACE_SETREGS, tid, 0, &regs);
486 if (ret < 0)
487 {
488 warning ("Unable to store general register.");
489 return;
490 }
491}
492
ed9a39eb
JM
493static void
494store_regs (void)
495{
41c49b06 496 int ret, regno, tid;
c2152441 497 elf_gregset_t regs;
ed9a39eb 498
41c49b06 499 /* Get the thread id for the ptrace call. */
39f77062 500 tid = GET_THREAD_ID (inferior_ptid);
41c49b06
SB
501
502 /* Fetch the general registers. */
503 ret = ptrace (PTRACE_GETREGS, tid, 0, &regs);
ed9a39eb
JM
504 if (ret < 0)
505 {
506 warning ("Unable to fetch general registers.");
507 return;
508 }
509
510 for (regno = A1_REGNUM; regno <= PC_REGNUM; regno++)
511 {
512 if (register_valid[regno])
c2152441 513 read_register_gen (regno, (char *) &regs[regno]);
ed9a39eb
JM
514 }
515
41c49b06 516 ret = ptrace (PTRACE_SETREGS, tid, 0, &regs);
ed9a39eb
JM
517
518 if (ret < 0)
519 {
520 warning ("Unable to store general registers.");
521 return;
522 }
523}
524
525/* Fetch registers from the child process. Fetch all registers if
526 regno == -1, otherwise fetch all general registers or all floating
527 point registers depending upon the value of regno. */
528
529void
530fetch_inferior_registers (int regno)
531{
41c49b06
SB
532 if (-1 == regno)
533 {
534 fetch_regs ();
535 fetch_fpregs ();
536 }
537 else
538 {
539 if (regno < F0_REGNUM || regno > FPS_REGNUM)
540 fetch_register (regno);
ed9a39eb 541
41c49b06
SB
542 if (regno >= F0_REGNUM && regno <= FPS_REGNUM)
543 fetch_fpregister (regno);
544 }
ed9a39eb
JM
545}
546
547/* Store registers back into the inferior. Store all registers if
548 regno == -1, otherwise store all general registers or all floating
549 point registers depending upon the value of regno. */
550
551void
552store_inferior_registers (int regno)
553{
41c49b06
SB
554 if (-1 == regno)
555 {
556 store_regs ();
557 store_fpregs ();
558 }
559 else
560 {
561 if ((regno < F0_REGNUM) || (regno > FPS_REGNUM))
562 store_register (regno);
ed9a39eb 563
41c49b06
SB
564 if ((regno >= F0_REGNUM) && (regno <= FPS_REGNUM))
565 store_fpregister (regno);
566 }
ed9a39eb
JM
567}
568
41c49b06
SB
569/* Fill register regno (if it is a general-purpose register) in
570 *gregsetp with the appropriate value from GDB's register array.
571 If regno is -1, do this for all registers. */
572
573void
713f0374 574fill_gregset (gdb_gregset_t *gregsetp, int regno)
41c49b06
SB
575{
576 if (-1 == regno)
577 {
578 int regnum;
579 for (regnum = A1_REGNUM; regnum <= PC_REGNUM; regnum++)
17fd1ad9 580 read_register_gen (regnum, (char *) &(*gregsetp)[regnum]);
41c49b06
SB
581 }
582 else if (regno >= A1_REGNUM && regno <= PC_REGNUM)
17fd1ad9 583 read_register_gen (regno, (char *) &(*gregsetp)[regno]);
41c49b06
SB
584
585 if (PS_REGNUM == regno || -1 == regno)
586 {
17fd1ad9
DJ
587 if (arm_apcs_32)
588 read_register_gen (PS_REGNUM, (char *) &(*gregsetp)[CPSR_REGNUM]);
589 else
590 read_register_gen (PC_REGNUM, (char *) &(*gregsetp)[PC_REGNUM]);
41c49b06 591 }
41c49b06
SB
592}
593
594/* Fill GDB's register array with the general-purpose register values
595 in *gregsetp. */
596
597void
713f0374 598supply_gregset (gdb_gregset_t *gregsetp)
41c49b06
SB
599{
600 int regno, reg_pc;
601
602 for (regno = A1_REGNUM; regno < PC_REGNUM; regno++)
603 supply_register (regno, (char *) &(*gregsetp)[regno]);
604
605 if (arm_apcs_32)
606 supply_register (PS_REGNUM, (char *) &(*gregsetp)[CPSR_REGNUM]);
607 else
608 supply_register (PS_REGNUM, (char *) &(*gregsetp)[PC_REGNUM]);
609
610 reg_pc = ADDR_BITS_REMOVE ((CORE_ADDR)(*gregsetp)[PC_REGNUM]);
611 supply_register (PC_REGNUM, (char *) &reg_pc);
612}
613
614/* Fill register regno (if it is a floating-point register) in
615 *fpregsetp with the appropriate value from GDB's register array.
616 If regno is -1, do this for all registers. */
617
618void
713f0374 619fill_fpregset (gdb_fpregset_t *fpregsetp, int regno)
41c49b06
SB
620{
621 FPA11 *fp = (FPA11 *) fpregsetp;
622
623 if (-1 == regno)
624 {
625 int regnum;
626 for (regnum = F0_REGNUM; regnum <= F7_REGNUM; regnum++)
627 store_nwfpe_register (regnum, fp);
628 }
629 else if (regno >= F0_REGNUM && regno <= F7_REGNUM)
630 {
631 store_nwfpe_register (regno, fp);
632 return;
633 }
634
635 /* Store fpsr. */
17fd1ad9
DJ
636 if (FPS_REGNUM == regno || -1 == regno)
637 read_register_gen (FPS_REGNUM, (char *) &fp->fpsr);
41c49b06
SB
638}
639
640/* Fill GDB's register array with the floating-point register values
641 in *fpregsetp. */
642
643void
713f0374 644supply_fpregset (gdb_fpregset_t *fpregsetp)
ed9a39eb 645{
41c49b06
SB
646 int regno;
647 FPA11 *fp = (FPA11 *) fpregsetp;
648
649 /* Fetch fpsr. */
650 supply_register (FPS_REGNUM, (char *) &fp->fpsr);
651
652 /* Fetch the floating point registers. */
653 for (regno = F0_REGNUM; regno <= F7_REGNUM; regno++)
654 {
655 fetch_nwfpe_register (regno, fp);
656 }
ed9a39eb
JM
657}
658
659int
660arm_linux_kernel_u_size (void)
661{
662 return (sizeof (struct user));
663}
664
ed9a39eb
JM
665static unsigned int
666get_linux_version (unsigned int *vmajor,
667 unsigned int *vminor,
668 unsigned int *vrelease)
669{
670 struct utsname info;
671 char *pmajor, *pminor, *prelease, *tail;
672
673 if (-1 == uname (&info))
674 {
675 warning ("Unable to determine Linux version.");
676 return -1;
677 }
678
679 pmajor = strtok (info.release, ".");
680 pminor = strtok (NULL, ".");
681 prelease = strtok (NULL, ".");
682
683 *vmajor = (unsigned int) strtoul (pmajor, &tail, 0);
684 *vminor = (unsigned int) strtoul (pminor, &tail, 0);
685 *vrelease = (unsigned int) strtoul (prelease, &tail, 0);
686
687 return ((*vmajor << 16) | (*vminor << 8) | *vrelease);
688}
689
690void
691_initialize_arm_linux_nat (void)
692{
693 os_version = get_linux_version (&os_major, &os_minor, &os_release);
694}