]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/ppc-linux-nat.c
2002-01-23 Elena Zannoni <ezannoni@redhat.com>
[thirdparty/binutils-gdb.git] / gdb / ppc-linux-nat.c
1 /* PPC linux native support.
2 Copyright 1988, 1989, 1991, 1992, 1994, 1996, 2000, 2001
3 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, Boston, MA 02111-1307, USA. */
20
21 #include "defs.h"
22 #include "frame.h"
23 #include "inferior.h"
24 #include "gdbcore.h"
25 #include "regcache.h"
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>
35 #include <sys/ptrace.h>
36
37 /* Prototypes for supply_gregset etc. */
38 #include "gregset.h"
39 #include "ppc-tdep.h"
40
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
53 int
54 kernel_u_size (void)
55 {
56 return (sizeof (struct user));
57 }
58
59 /* *INDENT-OFF* */
60 /* registers layout, as presented by the ptrace interface:
61 PT_R0, PT_R1, PT_R2, PT_R3, PT_R4, PT_R5, PT_R6, PT_R7,
62 PT_R8, PT_R9, PT_R10, PT_R11, PT_R12, PT_R13, PT_R14, PT_R15,
63 PT_R16, PT_R17, PT_R18, PT_R19, PT_R20, PT_R21, PT_R22, PT_R23,
64 PT_R24, PT_R25, PT_R26, PT_R27, PT_R28, PT_R29, PT_R30, PT_R31,
65 PT_FPR0, PT_FPR0 + 2, PT_FPR0 + 4, PT_FPR0 + 6, PT_FPR0 + 8, PT_FPR0 + 10, PT_FPR0 + 12, PT_FPR0 + 14,
66 PT_FPR0 + 16, PT_FPR0 + 18, PT_FPR0 + 20, PT_FPR0 + 22, PT_FPR0 + 24, PT_FPR0 + 26, PT_FPR0 + 28, PT_FPR0 + 30,
67 PT_FPR0 + 32, PT_FPR0 + 34, PT_FPR0 + 36, PT_FPR0 + 38, PT_FPR0 + 40, PT_FPR0 + 42, PT_FPR0 + 44, PT_FPR0 + 46,
68 PT_FPR0 + 48, PT_FPR0 + 50, PT_FPR0 + 52, PT_FPR0 + 54, PT_FPR0 + 56, PT_FPR0 + 58, PT_FPR0 + 60, PT_FPR0 + 62,
69 PT_NIP, PT_MSR, PT_CCR, PT_LNK, PT_CTR, PT_XER, PT_MQ */
70 /* *INDENT_ON * */
71
72 static int
73 ppc_register_u_addr (int regno)
74 {
75 int u_addr = -1;
76 struct gdbarch_tdep *tdep = gdbarch_tdep (current_gdbarch);
77
78 /* General purpose registers occupy 1 slot each in the buffer */
79 if (regno >= tdep->ppc_gp0_regnum && regno <= tdep->ppc_gplast_regnum )
80 u_addr = ((PT_R0 + regno) * 4);
81
82 /* Floating point regs: 2 slots each */
83 if (regno >= FP0_REGNUM && regno <= FPLAST_REGNUM)
84 u_addr = ((PT_FPR0 + (regno - FP0_REGNUM) * 2) * 4);
85
86 /* UISA special purpose registers: 1 slot each */
87 if (regno == PC_REGNUM)
88 u_addr = PT_NIP * 4;
89 if (regno == tdep->ppc_lr_regnum)
90 u_addr = PT_LNK * 4;
91 if (regno == tdep->ppc_cr_regnum)
92 u_addr = PT_CCR * 4;
93 if (regno == tdep->ppc_xer_regnum)
94 u_addr = PT_XER * 4;
95 if (regno == tdep->ppc_ctr_regnum)
96 u_addr = PT_CTR * 4;
97 if (regno == tdep->ppc_mq_regnum)
98 u_addr = PT_MQ * 4;
99 if (regno == tdep->ppc_ps_regnum)
100 u_addr = PT_MSR * 4;
101
102 return u_addr;
103 }
104
105 static int
106 ppc_ptrace_cannot_fetch_store_register (int regno)
107 {
108 return (ppc_register_u_addr (regno) == -1);
109 }
110
111 static void
112 fetch_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
149 static void
150 fetch_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. */
162 void
163 fetch_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. */
172 static void
173 store_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
208 static void
209 store_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
218 void
219 store_inferior_registers (int regno)
220 {
221 if (regno >= 0)
222 store_register (regno);
223 else
224 store_ppc_registers ();
225 }
226
227 void
228 supply_gregset (gdb_gregset_t *gregsetp)
229 {
230 int regi;
231 register elf_greg_t *regp = (elf_greg_t *) gregsetp;
232 struct gdbarch_tdep *tdep = gdbarch_tdep (current_gdbarch);
233
234 for (regi = 0; regi < 32; regi++)
235 supply_register (regi, (char *) (regp + regi));
236
237 supply_register (PC_REGNUM, (char *) (regp + PT_NIP));
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));
244 }
245
246 void
247 fill_gregset (gdb_gregset_t *gregsetp, int regno)
248 {
249 int regi;
250 elf_greg_t *regp = (elf_greg_t *) gregsetp;
251 struct gdbarch_tdep *tdep = gdbarch_tdep (current_gdbarch);
252
253 for (regi = 0; regi < 32; regi++)
254 {
255 if ((regno == -1) || regno == regi)
256 regcache_collect (regi, regp + PT_R0 + regi);
257 }
258
259 if ((regno == -1) || regno == PC_REGNUM)
260 regcache_collect (PC_REGNUM, regp + PT_NIP);
261 if ((regno == -1)
262 || regno == tdep->ppc_lr_regnum)
263 regcache_collect (tdep->ppc_lr_regnum, regp + PT_LNK);
264 if ((regno == -1)
265 || regno == tdep->ppc_cr_regnum)
266 regcache_collect (tdep->ppc_cr_regnum, regp + PT_CCR);
267 if ((regno == -1)
268 || regno == tdep->ppc_xer_regnum)
269 regcache_collect (tdep->ppc_xer_regnum, regp + PT_XER);
270 if ((regno == -1)
271 || regno == tdep->ppc_ctr_regnum)
272 regcache_collect (tdep->ppc_ctr_regnum, regp + PT_CTR);
273 if ((regno == -1)
274 || regno == tdep->ppc_mq_regnum)
275 regcache_collect (tdep->ppc_mq_regnum, regp + PT_MQ);
276 if ((regno == -1)
277 || regno == tdep->ppc_ps_regnum)
278 regcache_collect (tdep->ppc_ps_regnum, regp + PT_MSR);
279 }
280
281 void
282 supply_fpregset (gdb_fpregset_t * fpregsetp)
283 {
284 int regi;
285 for (regi = 0; regi < 32; regi++)
286 {
287 supply_register (FP0_REGNUM + regi, (char *) (*fpregsetp + regi));
288 }
289 }
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
296 void
297 fill_fpregset (gdb_fpregset_t *fpregsetp, int regno)
298 {
299 int regi;
300
301 for (regi = 0; regi < 32; regi++)
302 {
303 if ((regno == -1) || (regno == FP0_REGNUM + regi))
304 regcache_collect (FP0_REGNUM + regi, (char *) (*fpregsetp + regi));
305 }
306 }