Update
[gdb.git] / gdb / m32r-linux-nat.c
blob753b47223f48ab7b974a9221f4e39f2b212d2e88
1 /* Native-dependent code for GNU/Linux m32r.
3 Copyright (C) 2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
5 This file is part of GDB.
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 3 of the License, or
10 (at your option) any later version.
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.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20 #include "defs.h"
21 #include "inferior.h"
22 #include "gdbcore.h"
23 #include "regcache.h"
24 #include "linux-nat.h"
25 #include "target.h"
27 #include "gdb_assert.h"
28 #include "gdb_string.h"
29 #include <sys/ptrace.h>
30 #include <sys/user.h>
31 #include <sys/procfs.h>
33 /* Prototypes for supply_gregset etc. */
34 #include "gregset.h"
36 #include "m32r-tdep.h"
41 /* Since EVB register is not available for native debug, we reduce
42 the number of registers. */
43 #define M32R_LINUX_NUM_REGS (M32R_NUM_REGS - 1)
45 /* Mapping between the general-purpose registers in `struct user'
46 format and GDB's register array layout. */
47 static int regmap[] = {
48 4, 5, 6, 7, 0, 1, 2, 8,
49 9, 10, 11, 12, 13, 24, 25, 23,
50 19, 19, 26, 23, 22, 20, 16, 15
53 #define PSW_REGMAP 19
54 #define BBPSW_REGMAP 21
55 #define SPU_REGMAP 23
56 #define SPI_REGMAP 26
58 /* Doee apply to the corresponding SET requests as well. */
59 #define GETREGS_SUPPLIES(regno) (0 <= (regno) && (regno) <= M32R_LINUX_NUM_REGS)
63 /* Transfering the general-purpose registers between GDB, inferiors
64 and core files. */
66 /* Fill GDB's register array with the general-purpose register values
67 in *GREGSETP. */
69 void
70 supply_gregset (struct regcache *regcache, const elf_gregset_t * gregsetp)
72 const elf_greg_t *regp = (const elf_greg_t *) gregsetp;
73 int i;
74 unsigned long psw, bbpsw;
76 psw = *(regp + PSW_REGMAP);
77 bbpsw = *(regp + BBPSW_REGMAP);
79 for (i = 0; i < M32R_LINUX_NUM_REGS; i++)
81 elf_greg_t regval;
83 switch (i)
85 case PSW_REGNUM:
86 regval = ((0x00c1 & bbpsw) << 8) | ((0xc100 & psw) >> 8);
87 break;
88 case CBR_REGNUM:
89 regval = ((psw >> 8) & 1);
90 break;
91 default:
92 regval = *(regp + regmap[i]);
93 break;
96 if (i != M32R_SP_REGNUM)
97 regcache_raw_supply (regcache, i, &regval);
98 else if (psw & 0x8000)
99 regcache_raw_supply (regcache, i, regp + SPU_REGMAP);
100 else
101 regcache_raw_supply (regcache, i, regp + SPI_REGMAP);
105 /* Fetch all general-purpose registers from process/thread TID and
106 store their values in GDB's register array. */
108 static void
109 fetch_regs (struct regcache *regcache, int tid)
111 elf_gregset_t regs;
113 if (ptrace (PTRACE_GETREGS, tid, 0, (int) &regs) < 0)
114 perror_with_name (_("Couldn't get registers"));
116 supply_gregset (regcache, (const elf_gregset_t *) &regs);
119 /* Fill register REGNO (if it is a general-purpose register) in
120 *GREGSETPS with the value in GDB's register array. If REGNO is -1,
121 do this for all registers. */
123 void
124 fill_gregset (const struct regcache *regcache,
125 elf_gregset_t * gregsetp, int regno)
127 elf_greg_t *regp = (elf_greg_t *) gregsetp;
128 int i;
129 unsigned long psw, bbpsw, tmp;
131 psw = *(regp + PSW_REGMAP);
132 bbpsw = *(regp + BBPSW_REGMAP);
134 for (i = 0; i < M32R_LINUX_NUM_REGS; i++)
136 if (regno != -1 && regno != i)
137 continue;
139 if (i == CBR_REGNUM || i == PSW_REGNUM)
140 continue;
142 if (i == SPU_REGNUM || i == SPI_REGNUM)
143 continue;
145 if (i != M32R_SP_REGNUM)
146 regcache_raw_collect (regcache, i, regp + regmap[i]);
147 else if (psw & 0x8000)
148 regcache_raw_collect (regcache, i, regp + SPU_REGMAP);
149 else
150 regcache_raw_collect (regcache, i, regp + SPI_REGMAP);
154 /* Store all valid general-purpose registers in GDB's register array
155 into the process/thread specified by TID. */
157 static void
158 store_regs (const struct regcache *regcache, int tid, int regno)
160 elf_gregset_t regs;
162 if (ptrace (PTRACE_GETREGS, tid, 0, (int) &regs) < 0)
163 perror_with_name (_("Couldn't get registers"));
165 fill_gregset (regcache, &regs, regno);
167 if (ptrace (PTRACE_SETREGS, tid, 0, (int) &regs) < 0)
168 perror_with_name (_("Couldn't write registers"));
173 /* Transfering floating-point registers between GDB, inferiors and cores.
174 Since M32R has no floating-point registers, these functions do nothing. */
176 void
177 supply_fpregset (struct regcache *regcache, const gdb_fpregset_t *fpregs)
181 void
182 fill_fpregset (const struct regcache *regcache,
183 gdb_fpregset_t *fpregs, int regno)
189 /* Transferring arbitrary registers between GDB and inferior. */
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). */
195 static void
196 m32r_linux_fetch_inferior_registers (struct regcache *regcache, int regno)
198 int tid;
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. */
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))
210 fetch_regs (regcache, tid);
211 return;
214 internal_error (__FILE__, __LINE__,
215 _("Got request for bad register number %d."), regno);
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 (struct regcache *regcache, int regno)
224 int tid;
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. */
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))
234 store_regs (regcache, tid, regno);
235 return;
238 internal_error (__FILE__, __LINE__,
239 _("Got request to store bad register number %d."), regno);
242 void _initialize_m32r_linux_nat (void);
244 void
245 _initialize_m32r_linux_nat (void)
247 struct target_ops *t;
249 /* Fill in the generic GNU/Linux methods. */
250 t = linux_target ();
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;
256 /* Register the target. */
257 linux_nat_add_target (t);