* m32r-linux-nat.c (supply_gregset): Do not modify contents
[external/binutils.git] / gdb / m32r-linux-nat.c
1 /* Native-dependent code for GNU/Linux m32r.
2
3    Copyright (C) 2004, 2005, 2006, 2007 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 "regcache.h"
26 #include "linux-nat.h"
27 #include "target.h"
28
29 #include "gdb_assert.h"
30 #include "gdb_string.h"
31 #include <sys/ptrace.h>
32 #include <sys/user.h>
33 #include <sys/procfs.h>
34
35 /* Prototypes for supply_gregset etc.  */
36 #include "gregset.h"
37
38 #include "m32r-tdep.h"
39 \f
40
41
42
43 /* Since EVB register is not available for native debug, we reduce
44    the number of registers.  */
45 #define M32R_LINUX_NUM_REGS (M32R_NUM_REGS - 1)
46
47 /* Mapping between the general-purpose registers in `struct user'
48    format and GDB's register array layout.  */
49 static int regmap[] = {
50   4, 5, 6, 7, 0, 1, 2, 8,
51   9, 10, 11, 12, 13, 24, 25, 23,
52   19, 19, 26, 23, 22, 20, 16, 15
53 };
54
55 #define PSW_REGMAP 19
56 #define BBPSW_REGMAP 21
57 #define SPU_REGMAP 23
58 #define SPI_REGMAP 26
59
60 /* Doee apply to the corresponding SET requests as well.  */
61 #define GETREGS_SUPPLIES(regno) (0 <= (regno) && (regno) <= M32R_LINUX_NUM_REGS)
62 \f
63
64
65 /* Transfering the general-purpose registers between GDB, inferiors
66    and core files.  */
67
68 /* Fill GDB's register array with the general-purpose register values
69    in *GREGSETP.  */
70
71 void
72 supply_gregset (elf_gregset_t * gregsetp)
73 {
74   elf_greg_t *regp = (elf_greg_t *) gregsetp;
75   int i;
76   unsigned long psw, bbpsw;
77
78   psw = *(regp + PSW_REGMAP);
79   bbpsw = *(regp + BBPSW_REGMAP);
80
81   for (i = 0; i < M32R_LINUX_NUM_REGS; i++)
82     {
83       elf_greg_t regval;
84
85       switch (i)
86         {
87         case PSW_REGNUM:
88           regval = ((0x00c1 & bbpsw) << 8) | ((0xc100 & psw) >> 8);
89           break;
90         case CBR_REGNUM:
91           regval = ((psw >> 8) & 1);
92           break;
93         default:
94           regval = *(regp + regmap[i]);
95           break;
96         }
97
98       if (i != M32R_SP_REGNUM)
99         regcache_raw_supply (current_regcache, i, &regval);
100       else if (psw & 0x8000)
101         regcache_raw_supply (current_regcache, i, regp + SPU_REGMAP);
102       else
103         regcache_raw_supply (current_regcache, i, regp + SPI_REGMAP);
104     }
105 }
106
107 /* Fetch all general-purpose registers from process/thread TID and
108    store their values in GDB's register array.  */
109
110 static void
111 fetch_regs (int tid)
112 {
113   elf_gregset_t regs;
114
115   if (ptrace (PTRACE_GETREGS, tid, 0, (int) &regs) < 0)
116     perror_with_name (_("Couldn't get registers"));
117
118   supply_gregset (&regs);
119 }
120
121 /* Fill register REGNO (if it is a general-purpose register) in
122    *GREGSETPS with the value in GDB's register array.  If REGNO is -1,
123    do this for all registers.  */
124
125 void
126 fill_gregset (elf_gregset_t * gregsetp, int regno)
127 {
128   elf_greg_t *regp = (elf_greg_t *) gregsetp;
129   int i;
130   unsigned long psw, bbpsw, tmp;
131
132   psw = *(regp + PSW_REGMAP);
133   bbpsw = *(regp + BBPSW_REGMAP);
134
135   for (i = 0; i < M32R_LINUX_NUM_REGS; i++)
136     {
137       if (regno != -1 && regno != i)
138         continue;
139
140       if (i == CBR_REGNUM || i == PSW_REGNUM)
141         continue;
142
143       if (i == SPU_REGNUM || i == SPI_REGNUM)
144         continue;
145
146       if (i != M32R_SP_REGNUM)
147         regcache_raw_collect (current_regcache, i, regp + regmap[i]);
148       else if (psw & 0x8000)
149         regcache_raw_collect (current_regcache, i, regp + SPU_REGMAP);
150       else
151         regcache_raw_collect (current_regcache, i, regp + SPI_REGMAP);
152     }
153 }
154
155 /* Store all valid general-purpose registers in GDB's register array
156    into the process/thread specified by TID.  */
157
158 static void
159 store_regs (int tid, int regno)
160 {
161   elf_gregset_t regs;
162
163   if (ptrace (PTRACE_GETREGS, tid, 0, (int) &regs) < 0)
164     perror_with_name (_("Couldn't get registers"));
165
166   fill_gregset (&regs, regno);
167
168   if (ptrace (PTRACE_SETREGS, tid, 0, (int) &regs) < 0)
169     perror_with_name (_("Couldn't write registers"));
170 }
171 \f
172
173
174 /* Transfering floating-point registers between GDB, inferiors and cores.  
175    Since M32R has no floating-point registers, these functions do nothing.  */
176
177 void
178 supply_fpregset (gdb_fpregset_t *fpregs)
179 {
180 }
181
182 void
183 fill_fpregset (gdb_fpregset_t *fpregs, int regno)
184 {
185 }
186 \f
187
188
189 /* Transferring arbitrary registers between GDB and inferior.  */
190
191 /* Fetch register REGNO from the child process.  If REGNO is -1, do
192    this for all registers (including the floating point and SSE
193    registers).  */
194
195 static void
196 m32r_linux_fetch_inferior_registers (int regno)
197 {
198   int tid;
199
200   /* GNU/Linux LWP ID's are process ID's.  */
201   tid = TIDGET (inferior_ptid);
202   if (tid == 0)
203     tid = PIDGET (inferior_ptid);       /* Not a threaded program.  */
204
205   /* Use the PTRACE_GETREGS request whenever possible, since it
206      transfers more registers in one system call, and we'll cache the
207      results.  */
208   if (regno == -1 || GETREGS_SUPPLIES (regno))
209     {
210       fetch_regs (tid);
211       return;
212     }
213
214   internal_error (__FILE__, __LINE__,
215                   _("Got request for bad register number %d."), regno);
216 }
217
218 /* Store register REGNO back into the child process.  If REGNO is -1,
219    do this for all registers (including the floating point and SSE
220    registers).  */
221 static void
222 m32r_linux_store_inferior_registers (int regno)
223 {
224   int tid;
225
226   /* GNU/Linux LWP ID's are process ID's.  */
227   if ((tid = TIDGET (inferior_ptid)) == 0)
228     tid = PIDGET (inferior_ptid);       /* Not a threaded program.  */
229
230   /* Use the PTRACE_SETREGS request whenever possible, since it
231      transfers more registers in one system call.  */
232   if (regno == -1 || GETREGS_SUPPLIES (regno))
233     {
234       store_regs (tid, regno);
235       return;
236     }
237
238   internal_error (__FILE__, __LINE__,
239                   _("Got request to store bad register number %d."), regno);
240 }
241
242 void _initialize_m32r_linux_nat (void);
243
244 void
245 _initialize_m32r_linux_nat (void)
246 {
247   struct target_ops *t;
248
249   /* Fill in the generic GNU/Linux methods.  */
250   t = linux_target ();
251
252   /* Add our register access methods.  */
253   t->to_fetch_registers = m32r_linux_fetch_inferior_registers;
254   t->to_store_registers = m32r_linux_store_inferior_registers;
255
256   /* Register the target.  */
257   linux_nat_add_target (t);
258 }