1 /* PPC linux native support.
2 Copyright 1988, 1989, 1991, 1992, 1994, 1996, 2000, 2001, 2002
3 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 2 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, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
28 #include <sys/types.h>
29 #include <sys/param.h>
32 #include <sys/ioctl.h>
35 #include <sys/procfs.h>
36 #include <sys/ptrace.h>
38 /* Prototypes for supply_gregset etc. */
43 #define PT_READ_U PTRACE_PEEKUSR
46 #define PT_WRITE_U PTRACE_POKEUSR
49 /* Default the type of the ptrace transfer to int. */
50 #ifndef PTRACE_XFER_TYPE
51 #define PTRACE_XFER_TYPE int
57 return (sizeof (struct user
));
61 /* registers layout, as presented by the ptrace interface:
62 PT_R0, PT_R1, PT_R2, PT_R3, PT_R4, PT_R5, PT_R6, PT_R7,
63 PT_R8, PT_R9, PT_R10, PT_R11, PT_R12, PT_R13, PT_R14, PT_R15,
64 PT_R16, PT_R17, PT_R18, PT_R19, PT_R20, PT_R21, PT_R22, PT_R23,
65 PT_R24, PT_R25, PT_R26, PT_R27, PT_R28, PT_R29, PT_R30, PT_R31,
66 PT_FPR0, PT_FPR0 + 2, PT_FPR0 + 4, PT_FPR0 + 6, PT_FPR0 + 8, PT_FPR0 + 10, PT_FPR0 + 12, PT_FPR0 + 14,
67 PT_FPR0 + 16, PT_FPR0 + 18, PT_FPR0 + 20, PT_FPR0 + 22, PT_FPR0 + 24, PT_FPR0 + 26, PT_FPR0 + 28, PT_FPR0 + 30,
68 PT_FPR0 + 32, PT_FPR0 + 34, PT_FPR0 + 36, PT_FPR0 + 38, PT_FPR0 + 40, PT_FPR0 + 42, PT_FPR0 + 44, PT_FPR0 + 46,
69 PT_FPR0 + 48, PT_FPR0 + 50, PT_FPR0 + 52, PT_FPR0 + 54, PT_FPR0 + 56, PT_FPR0 + 58, PT_FPR0 + 60, PT_FPR0 + 62,
70 PT_NIP, PT_MSR, PT_CCR, PT_LNK, PT_CTR, PT_XER, PT_MQ */
74 ppc_register_u_addr (int regno
)
77 struct gdbarch_tdep
*tdep
= gdbarch_tdep (current_gdbarch
);
79 /* General purpose registers occupy 1 slot each in the buffer */
80 if (regno
>= tdep
->ppc_gp0_regnum
&& regno
<= tdep
->ppc_gplast_regnum
)
81 u_addr
= ((PT_R0
+ regno
) * 4);
83 /* Floating point regs: 2 slots each */
84 if (regno
>= FP0_REGNUM
&& regno
<= FPLAST_REGNUM
)
85 u_addr
= ((PT_FPR0
+ (regno
- FP0_REGNUM
) * 2) * 4);
87 /* UISA special purpose registers: 1 slot each */
88 if (regno
== PC_REGNUM
)
90 if (regno
== tdep
->ppc_lr_regnum
)
92 if (regno
== tdep
->ppc_cr_regnum
)
94 if (regno
== tdep
->ppc_xer_regnum
)
96 if (regno
== tdep
->ppc_ctr_regnum
)
98 if (regno
== tdep
->ppc_mq_regnum
)
100 if (regno
== tdep
->ppc_ps_regnum
)
107 ppc_ptrace_cannot_fetch_store_register (int regno
)
109 return (ppc_register_u_addr (regno
) == -1);
113 fetch_register (int tid
, int regno
)
115 /* This isn't really an address. But ptrace thinks of it as one. */
116 char mess
[128]; /* For messages */
118 unsigned int offset
; /* Offset of registers within the u area. */
119 char *buf
= alloca (MAX_REGISTER_RAW_SIZE
);
120 CORE_ADDR regaddr
= ppc_register_u_addr (regno
);
124 memset (buf
, '\0', REGISTER_RAW_SIZE (regno
)); /* Supply zeroes */
125 supply_register (regno
, buf
);
129 for (i
= 0; i
< REGISTER_RAW_SIZE (regno
); i
+= sizeof (PTRACE_XFER_TYPE
))
132 *(PTRACE_XFER_TYPE
*) & buf
[i
] = ptrace (PT_READ_U
, tid
,
133 (PTRACE_ARG3_TYPE
) regaddr
, 0);
134 regaddr
+= sizeof (PTRACE_XFER_TYPE
);
137 sprintf (mess
, "reading register %s (#%d)",
138 REGISTER_NAME (regno
), regno
);
139 perror_with_name (mess
);
142 supply_register (regno
, buf
);
146 fetch_ppc_registers (int tid
)
149 int last_register
= gdbarch_tdep (current_gdbarch
)->ppc_mq_regnum
;
151 for (i
= 0; i
<= last_register
; i
++)
152 fetch_register (tid
, i
);
155 /* Fetch registers from the child process. Fetch all registers if
156 regno == -1, otherwise fetch all general registers or all floating
157 point registers depending upon the value of regno. */
159 fetch_inferior_registers (int regno
)
161 /* Overload thread id onto process id */
162 int tid
= TIDGET (inferior_ptid
);
164 /* No thread id, just use process id */
166 tid
= PIDGET (inferior_ptid
);
169 fetch_ppc_registers (tid
);
171 fetch_register (tid
, regno
);
174 /* Store one register. */
176 store_register (int tid
, int regno
)
178 /* This isn't really an address. But ptrace thinks of it as one. */
179 CORE_ADDR regaddr
= ppc_register_u_addr (regno
);
180 char mess
[128]; /* For messages */
182 unsigned int offset
; /* Offset of registers within the u area. */
183 char *buf
= alloca (MAX_REGISTER_RAW_SIZE
);
190 regcache_collect (regno
, buf
);
191 for (i
= 0; i
< REGISTER_RAW_SIZE (regno
); i
+= sizeof (PTRACE_XFER_TYPE
))
194 ptrace (PT_WRITE_U
, tid
, (PTRACE_ARG3_TYPE
) regaddr
,
195 *(PTRACE_XFER_TYPE
*) & buf
[i
]);
196 regaddr
+= sizeof (PTRACE_XFER_TYPE
);
199 sprintf (mess
, "writing register %s (#%d)",
200 REGISTER_NAME (regno
), regno
);
201 perror_with_name (mess
);
207 store_ppc_registers (int tid
)
210 int last_register
= gdbarch_tdep (current_gdbarch
)->ppc_mq_regnum
;
212 for (i
= 0; i
<= last_register
; i
++)
213 store_register (tid
, i
);
217 store_inferior_registers (int regno
)
219 /* Overload thread id onto process id */
220 int tid
= TIDGET (inferior_ptid
);
222 /* No thread id, just use process id */
224 tid
= PIDGET (inferior_ptid
);
227 store_register (tid
, regno
);
229 store_ppc_registers (tid
);
233 supply_gregset (gdb_gregset_t
*gregsetp
)
236 register elf_greg_t
*regp
= (elf_greg_t
*) gregsetp
;
237 struct gdbarch_tdep
*tdep
= gdbarch_tdep (current_gdbarch
);
239 for (regi
= 0; regi
< 32; regi
++)
240 supply_register (regi
, (char *) (regp
+ regi
));
242 supply_register (PC_REGNUM
, (char *) (regp
+ PT_NIP
));
243 supply_register (tdep
->ppc_lr_regnum
, (char *) (regp
+ PT_LNK
));
244 supply_register (tdep
->ppc_cr_regnum
, (char *) (regp
+ PT_CCR
));
245 supply_register (tdep
->ppc_xer_regnum
, (char *) (regp
+ PT_XER
));
246 supply_register (tdep
->ppc_ctr_regnum
, (char *) (regp
+ PT_CTR
));
247 supply_register (tdep
->ppc_mq_regnum
, (char *) (regp
+ PT_MQ
));
248 supply_register (tdep
->ppc_ps_regnum
, (char *) (regp
+ PT_MSR
));
252 fill_gregset (gdb_gregset_t
*gregsetp
, int regno
)
255 elf_greg_t
*regp
= (elf_greg_t
*) gregsetp
;
256 struct gdbarch_tdep
*tdep
= gdbarch_tdep (current_gdbarch
);
258 for (regi
= 0; regi
< 32; regi
++)
260 if ((regno
== -1) || regno
== regi
)
261 regcache_collect (regi
, regp
+ PT_R0
+ regi
);
264 if ((regno
== -1) || regno
== PC_REGNUM
)
265 regcache_collect (PC_REGNUM
, regp
+ PT_NIP
);
266 if ((regno
== -1) || regno
== tdep
->ppc_lr_regnum
)
267 regcache_collect (tdep
->ppc_lr_regnum
, regp
+ PT_LNK
);
268 if ((regno
== -1) || regno
== tdep
->ppc_cr_regnum
)
269 regcache_collect (tdep
->ppc_cr_regnum
, regp
+ PT_CCR
);
270 if ((regno
== -1) || regno
== tdep
->ppc_xer_regnum
)
271 regcache_collect (tdep
->ppc_xer_regnum
, regp
+ PT_XER
);
272 if ((regno
== -1) || regno
== tdep
->ppc_ctr_regnum
)
273 regcache_collect (tdep
->ppc_ctr_regnum
, regp
+ PT_CTR
);
274 if ((regno
== -1) || regno
== tdep
->ppc_mq_regnum
)
275 regcache_collect (tdep
->ppc_mq_regnum
, regp
+ PT_MQ
);
276 if ((regno
== -1) || regno
== tdep
->ppc_ps_regnum
)
277 regcache_collect (tdep
->ppc_ps_regnum
, regp
+ PT_MSR
);
281 supply_fpregset (gdb_fpregset_t
* fpregsetp
)
284 for (regi
= 0; regi
< 32; regi
++)
285 supply_register (FP0_REGNUM
+ regi
, (char *) (*fpregsetp
+ regi
));
288 /* Given a pointer to a floating point register set in /proc format
289 (fpregset_t *), update the register specified by REGNO from gdb's idea
290 of the current floating point register set. If REGNO is -1, update
294 fill_fpregset (gdb_fpregset_t
*fpregsetp
, int regno
)
298 for (regi
= 0; regi
< 32; regi
++)
300 if ((regno
== -1) || (regno
== FP0_REGNUM
+ regi
))
301 regcache_collect (FP0_REGNUM
+ regi
, (char *) (*fpregsetp
+ regi
));