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