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