Contribute sh64-elf.
[deliverable/binutils-gdb.git] / gdb / ppc-linux-nat.c
CommitLineData
c877c8e6 1/* PPC linux native support.
b6ba6518
KB
2 Copyright 1988, 1989, 1991, 1992, 1994, 1996, 2000, 2001
3 Free Software Foundation, Inc.
c877c8e6
KB
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, Boston, MA 02111-1307, USA. */
20
21#include "defs.h"
22#include "frame.h"
23#include "inferior.h"
24#include "gdbcore.h"
4e052eda 25#include "regcache.h"
c877c8e6
KB
26
27#include <sys/types.h>
28#include <sys/param.h>
29#include <signal.h>
30#include <sys/user.h>
31#include <sys/ioctl.h>
32#include <sys/wait.h>
33#include <fcntl.h>
34#include <sys/procfs.h>
45229ea4 35#include <sys/ptrace.h>
c877c8e6 36
c60c0f5f
MS
37/* Prototypes for supply_gregset etc. */
38#include "gregset.h"
16333c4f 39#include "ppc-tdep.h"
c60c0f5f 40
45229ea4
EZ
41#ifndef PT_READ_U
42#define PT_READ_U PTRACE_PEEKUSR
43#endif
44#ifndef PT_WRITE_U
45#define PT_WRITE_U PTRACE_POKEUSR
46#endif
47
48/* Default the type of the ptrace transfer to int. */
49#ifndef PTRACE_XFER_TYPE
50#define PTRACE_XFER_TYPE int
51#endif
52
c877c8e6 53int
fba45db2 54kernel_u_size (void)
c877c8e6
KB
55{
56 return (sizeof (struct user));
57}
58
16333c4f
EZ
59/* *INDENT-OFF* */
60/* registers layout, as presented by the ptrace interface:
61PT_R0, PT_R1, PT_R2, PT_R3, PT_R4, PT_R5, PT_R6, PT_R7,
62PT_R8, PT_R9, PT_R10, PT_R11, PT_R12, PT_R13, PT_R14, PT_R15,
63PT_R16, PT_R17, PT_R18, PT_R19, PT_R20, PT_R21, PT_R22, PT_R23,
64PT_R24, PT_R25, PT_R26, PT_R27, PT_R28, PT_R29, PT_R30, PT_R31,
65PT_FPR0, PT_FPR0 + 2, PT_FPR0 + 4, PT_FPR0 + 6, PT_FPR0 + 8, PT_FPR0 + 10, PT_FPR0 + 12, PT_FPR0 + 14,
66PT_FPR0 + 16, PT_FPR0 + 18, PT_FPR0 + 20, PT_FPR0 + 22, PT_FPR0 + 24, PT_FPR0 + 26, PT_FPR0 + 28, PT_FPR0 + 30,
67PT_FPR0 + 32, PT_FPR0 + 34, PT_FPR0 + 36, PT_FPR0 + 38, PT_FPR0 + 40, PT_FPR0 + 42, PT_FPR0 + 44, PT_FPR0 + 46,
68PT_FPR0 + 48, PT_FPR0 + 50, PT_FPR0 + 52, PT_FPR0 + 54, PT_FPR0 + 56, PT_FPR0 + 58, PT_FPR0 + 60, PT_FPR0 + 62,
69PT_NIP, PT_MSR, PT_CCR, PT_LNK, PT_CTR, PT_XER, PT_MQ */
70/* *INDENT_ON * */
c877c8e6 71
45229ea4
EZ
72static int
73ppc_register_u_addr (int regno)
c877c8e6 74{
16333c4f 75 int u_addr = -1;
dc5cfeb6 76 struct gdbarch_tdep *tdep = gdbarch_tdep (current_gdbarch);
16333c4f
EZ
77
78 /* General purpose registers occupy 1 slot each in the buffer */
dc5cfeb6 79 if (regno >= tdep->ppc_gp0_regnum && regno <= tdep->ppc_gplast_regnum )
45229ea4 80 u_addr = ((PT_R0 + regno) * 4);
16333c4f
EZ
81
82 /* Floating point regs: 2 slots each */
83 if (regno >= FP0_REGNUM && regno <= FPLAST_REGNUM)
45229ea4 84 u_addr = ((PT_FPR0 + (regno - FP0_REGNUM) * 2) * 4);
16333c4f
EZ
85
86 /* UISA special purpose registers: 1 slot each */
87 if (regno == PC_REGNUM)
45229ea4 88 u_addr = PT_NIP * 4;
dc5cfeb6 89 if (regno == tdep->ppc_lr_regnum)
45229ea4 90 u_addr = PT_LNK * 4;
dc5cfeb6 91 if (regno == tdep->ppc_cr_regnum)
45229ea4 92 u_addr = PT_CCR * 4;
dc5cfeb6 93 if (regno == tdep->ppc_xer_regnum)
45229ea4 94 u_addr = PT_XER * 4;
dc5cfeb6 95 if (regno == tdep->ppc_ctr_regnum)
45229ea4 96 u_addr = PT_CTR * 4;
dc5cfeb6 97 if (regno == tdep->ppc_mq_regnum)
45229ea4 98 u_addr = PT_MQ * 4;
dc5cfeb6 99 if (regno == tdep->ppc_ps_regnum)
45229ea4 100 u_addr = PT_MSR * 4;
16333c4f
EZ
101
102 return u_addr;
c877c8e6
KB
103}
104
45229ea4
EZ
105static int
106ppc_ptrace_cannot_fetch_store_register (int regno)
107{
108 return (ppc_register_u_addr (regno) == -1);
109}
110
111static void
112fetch_register (int regno)
113{
114 /* This isn't really an address. But ptrace thinks of it as one. */
115 char mess[128]; /* For messages */
116 register int i;
117 unsigned int offset; /* Offset of registers within the u area. */
118 char *buf = alloca (MAX_REGISTER_RAW_SIZE);
119 int tid;
120 CORE_ADDR regaddr = ppc_register_u_addr (regno);
121
122 if (regaddr == -1)
123 {
124 memset (buf, '\0', REGISTER_RAW_SIZE (regno)); /* Supply zeroes */
125 supply_register (regno, buf);
126 return;
127 }
128
129 /* Overload thread id onto process id */
130 if ((tid = TIDGET (inferior_ptid)) == 0)
131 tid = PIDGET (inferior_ptid); /* no thread id, just use process id */
132
133 for (i = 0; i < REGISTER_RAW_SIZE (regno); i += sizeof (PTRACE_XFER_TYPE))
134 {
135 errno = 0;
136 *(PTRACE_XFER_TYPE *) & buf[i] = ptrace (PT_READ_U, tid,
137 (PTRACE_ARG3_TYPE) regaddr, 0);
138 regaddr += sizeof (PTRACE_XFER_TYPE);
139 if (errno != 0)
140 {
141 sprintf (mess, "reading register %s (#%d)",
142 REGISTER_NAME (regno), regno);
143 perror_with_name (mess);
144 }
145 }
146 supply_register (regno, buf);
147}
148
149static void
150fetch_ppc_registers (void)
151{
152 int i;
153 int last_register = gdbarch_tdep (current_gdbarch)->ppc_mq_regnum;
154
155 for (i = 0; i <= last_register; i++)
156 fetch_register (i);
157}
158
159/* Fetch registers from the child process. Fetch all registers if
160 regno == -1, otherwise fetch all general registers or all floating
161 point registers depending upon the value of regno. */
162void
163fetch_inferior_registers (int regno)
164{
165 if (regno == -1)
166 fetch_ppc_registers ();
167 else
168 fetch_register (regno);
169}
170
171/* Store one register. */
172static void
173store_register (int regno)
174{
175 /* This isn't really an address. But ptrace thinks of it as one. */
176 CORE_ADDR regaddr = ppc_register_u_addr (regno);
177 char mess[128]; /* For messages */
178 register int i;
179 unsigned int offset; /* Offset of registers within the u area. */
180 int tid;
181 char *buf = alloca (MAX_REGISTER_RAW_SIZE);
182
183 if (regaddr == -1)
184 {
185 return;
186 }
187
188 /* Overload thread id onto process id */
189 if ((tid = TIDGET (inferior_ptid)) == 0)
190 tid = PIDGET (inferior_ptid); /* no thread id, just use process id */
191
192 regcache_collect (regno, buf);
193 for (i = 0; i < REGISTER_RAW_SIZE (regno); i += sizeof (PTRACE_XFER_TYPE))
194 {
195 errno = 0;
196 ptrace (PT_WRITE_U, tid, (PTRACE_ARG3_TYPE) regaddr,
197 *(PTRACE_XFER_TYPE *) & buf[i]);
198 regaddr += sizeof (PTRACE_XFER_TYPE);
199 if (errno != 0)
200 {
201 sprintf (mess, "writing register %s (#%d)",
202 REGISTER_NAME (regno), regno);
203 perror_with_name (mess);
204 }
205 }
206}
207
208static void
209store_ppc_registers (void)
210{
211 int i;
212 int last_register = gdbarch_tdep (current_gdbarch)->ppc_mq_regnum;
213
214 for (i = 0; i <= last_register; i++)
215 store_register (i);
216}
217
218void
219store_inferior_registers (int regno)
220{
221 if (regno >= 0)
222 store_register (regno);
223 else
224 store_ppc_registers ();
225}
226
50c9bd31 227void
8ae45c11 228supply_gregset (gdb_gregset_t *gregsetp)
c877c8e6
KB
229{
230 int regi;
2ac44c70 231 register elf_greg_t *regp = (elf_greg_t *) gregsetp;
dc5cfeb6 232 struct gdbarch_tdep *tdep = gdbarch_tdep (current_gdbarch);
c877c8e6
KB
233
234 for (regi = 0; regi < 32; regi++)
235 supply_register (regi, (char *) (regp + regi));
236
16333c4f 237 supply_register (PC_REGNUM, (char *) (regp + PT_NIP));
dc5cfeb6
EZ
238 supply_register (tdep->ppc_lr_regnum, (char *) (regp + PT_LNK));
239 supply_register (tdep->ppc_cr_regnum, (char *) (regp + PT_CCR));
240 supply_register (tdep->ppc_xer_regnum, (char *) (regp + PT_XER));
241 supply_register (tdep->ppc_ctr_regnum, (char *) (regp + PT_CTR));
242 supply_register (tdep->ppc_mq_regnum, (char *) (regp + PT_MQ));
243 supply_register (tdep->ppc_ps_regnum, (char *) (regp + PT_MSR));
c877c8e6
KB
244}
245
fdb28ac4 246void
8ae45c11 247fill_gregset (gdb_gregset_t *gregsetp, int regno)
fdb28ac4
KB
248{
249 int regi;
2ac44c70 250 elf_greg_t *regp = (elf_greg_t *) gregsetp;
dc5cfeb6 251 struct gdbarch_tdep *tdep = gdbarch_tdep (current_gdbarch);
fdb28ac4 252
fdb28ac4
KB
253 for (regi = 0; regi < 32; regi++)
254 {
16333c4f
EZ
255 if ((regno == -1) || regno == regi)
256 regcache_collect (regi, regp + PT_R0 + regi);
fdb28ac4
KB
257 }
258
16333c4f
EZ
259 if ((regno == -1) || regno == PC_REGNUM)
260 regcache_collect (PC_REGNUM, regp + PT_NIP);
2188cbdd 261 if ((regno == -1)
dc5cfeb6
EZ
262 || regno == tdep->ppc_lr_regnum)
263 regcache_collect (tdep->ppc_lr_regnum, regp + PT_LNK);
2188cbdd 264 if ((regno == -1)
dc5cfeb6
EZ
265 || regno == tdep->ppc_cr_regnum)
266 regcache_collect (tdep->ppc_cr_regnum, regp + PT_CCR);
2188cbdd 267 if ((regno == -1)
dc5cfeb6
EZ
268 || regno == tdep->ppc_xer_regnum)
269 regcache_collect (tdep->ppc_xer_regnum, regp + PT_XER);
2188cbdd 270 if ((regno == -1)
dc5cfeb6
EZ
271 || regno == tdep->ppc_ctr_regnum)
272 regcache_collect (tdep->ppc_ctr_regnum, regp + PT_CTR);
2188cbdd 273 if ((regno == -1)
dc5cfeb6
EZ
274 || regno == tdep->ppc_mq_regnum)
275 regcache_collect (tdep->ppc_mq_regnum, regp + PT_MQ);
2188cbdd 276 if ((regno == -1)
dc5cfeb6
EZ
277 || regno == tdep->ppc_ps_regnum)
278 regcache_collect (tdep->ppc_ps_regnum, regp + PT_MSR);
fdb28ac4
KB
279}
280
50c9bd31 281void
8ae45c11 282supply_fpregset (gdb_fpregset_t * fpregsetp)
c877c8e6
KB
283{
284 int regi;
285 for (regi = 0; regi < 32; regi++)
286 {
287 supply_register (FP0_REGNUM + regi, (char *) (*fpregsetp + regi));
288 }
289}
fdb28ac4
KB
290
291/* Given a pointer to a floating point register set in /proc format
292 (fpregset_t *), update the register specified by REGNO from gdb's idea
293 of the current floating point register set. If REGNO is -1, update
294 them all. */
295
296void
8ae45c11 297fill_fpregset (gdb_fpregset_t *fpregsetp, int regno)
fdb28ac4
KB
298{
299 int regi;
fdb28ac4
KB
300
301 for (regi = 0; regi < 32; regi++)
302 {
303 if ((regno == -1) || (regno == FP0_REGNUM + regi))
f00d3753 304 regcache_collect (FP0_REGNUM + regi, (char *) (*fpregsetp + regi));
fdb28ac4
KB
305 }
306}
This page took 0.307622 seconds and 4 git commands to generate.