Commit | Line | Data |
---|---|---|
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 | 53 | int |
fba45db2 | 54 | kernel_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: | |
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 * */ | |
c877c8e6 | 71 | |
45229ea4 EZ |
72 | static int |
73 | ppc_register_u_addr (int regno) | |
c877c8e6 | 74 | { |
16333c4f EZ |
75 | int u_addr = -1; |
76 | ||
77 | /* General purpose registers occupy 1 slot each in the buffer */ | |
2188cbdd EZ |
78 | if (regno >= gdbarch_tdep (current_gdbarch)->ppc_gp0_regnum |
79 | && regno <= gdbarch_tdep (current_gdbarch)->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; |
2188cbdd | 89 | if (regno == gdbarch_tdep (current_gdbarch)->ppc_lr_regnum) |
45229ea4 | 90 | u_addr = PT_LNK * 4; |
2188cbdd | 91 | if (regno == gdbarch_tdep (current_gdbarch)->ppc_cr_regnum) |
45229ea4 | 92 | u_addr = PT_CCR * 4; |
2188cbdd | 93 | if (regno == gdbarch_tdep (current_gdbarch)->ppc_xer_regnum) |
45229ea4 | 94 | u_addr = PT_XER * 4; |
2188cbdd | 95 | if (regno == gdbarch_tdep (current_gdbarch)->ppc_ctr_regnum) |
45229ea4 | 96 | u_addr = PT_CTR * 4; |
2188cbdd | 97 | if (regno == gdbarch_tdep (current_gdbarch)->ppc_mq_regnum) |
45229ea4 | 98 | u_addr = PT_MQ * 4; |
2188cbdd | 99 | if (regno == gdbarch_tdep (current_gdbarch)->ppc_ps_regnum) |
45229ea4 | 100 | u_addr = PT_MSR * 4; |
16333c4f EZ |
101 | |
102 | return u_addr; | |
c877c8e6 KB |
103 | } |
104 | ||
45229ea4 EZ |
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 | ||
50c9bd31 | 227 | void |
8ae45c11 | 228 | supply_gregset (gdb_gregset_t *gregsetp) |
c877c8e6 KB |
229 | { |
230 | int regi; | |
2ac44c70 | 231 | register elf_greg_t *regp = (elf_greg_t *) gregsetp; |
c877c8e6 KB |
232 | |
233 | for (regi = 0; regi < 32; regi++) | |
234 | supply_register (regi, (char *) (regp + regi)); | |
235 | ||
16333c4f | 236 | supply_register (PC_REGNUM, (char *) (regp + PT_NIP)); |
2188cbdd EZ |
237 | supply_register (gdbarch_tdep (current_gdbarch)->ppc_lr_regnum, |
238 | (char *) (regp + PT_LNK)); | |
239 | supply_register (gdbarch_tdep (current_gdbarch)->ppc_cr_regnum, | |
240 | (char *) (regp + PT_CCR)); | |
241 | supply_register (gdbarch_tdep (current_gdbarch)->ppc_xer_regnum, | |
242 | (char *) (regp + PT_XER)); | |
243 | supply_register (gdbarch_tdep (current_gdbarch)->ppc_ctr_regnum, | |
244 | (char *) (regp + PT_CTR)); | |
245 | supply_register (gdbarch_tdep (current_gdbarch)->ppc_mq_regnum, | |
246 | (char *) (regp + PT_MQ)); | |
247 | supply_register (gdbarch_tdep (current_gdbarch)->ppc_ps_regnum, | |
248 | (char *) (regp + PT_MSR)); | |
c877c8e6 KB |
249 | } |
250 | ||
fdb28ac4 | 251 | void |
8ae45c11 | 252 | fill_gregset (gdb_gregset_t *gregsetp, int regno) |
fdb28ac4 KB |
253 | { |
254 | int regi; | |
2ac44c70 | 255 | elf_greg_t *regp = (elf_greg_t *) gregsetp; |
fdb28ac4 | 256 | |
fdb28ac4 KB |
257 | for (regi = 0; regi < 32; regi++) |
258 | { | |
16333c4f EZ |
259 | if ((regno == -1) || regno == regi) |
260 | regcache_collect (regi, regp + PT_R0 + regi); | |
fdb28ac4 KB |
261 | } |
262 | ||
16333c4f EZ |
263 | if ((regno == -1) || regno == PC_REGNUM) |
264 | regcache_collect (PC_REGNUM, regp + PT_NIP); | |
2188cbdd EZ |
265 | if ((regno == -1) |
266 | || regno == gdbarch_tdep (current_gdbarch)->ppc_lr_regnum) | |
267 | regcache_collect (gdbarch_tdep (current_gdbarch)->ppc_lr_regnum, | |
268 | regp + PT_LNK); | |
269 | if ((regno == -1) | |
270 | || regno == gdbarch_tdep (current_gdbarch)->ppc_cr_regnum) | |
271 | regcache_collect (gdbarch_tdep (current_gdbarch)->ppc_cr_regnum, | |
272 | regp + PT_CCR); | |
273 | if ((regno == -1) | |
274 | || regno == gdbarch_tdep (current_gdbarch)->ppc_xer_regnum) | |
275 | regcache_collect (gdbarch_tdep (current_gdbarch)->ppc_xer_regnum, | |
276 | regp + PT_XER); | |
277 | if ((regno == -1) | |
278 | || regno == gdbarch_tdep (current_gdbarch)->ppc_ctr_regnum) | |
279 | regcache_collect (gdbarch_tdep (current_gdbarch)->ppc_ctr_regnum, | |
280 | regp + PT_CTR); | |
281 | if ((regno == -1) | |
282 | || regno == gdbarch_tdep (current_gdbarch)->ppc_mq_regnum) | |
283 | regcache_collect (gdbarch_tdep (current_gdbarch)->ppc_mq_regnum, | |
284 | regp + PT_MQ); | |
285 | if ((regno == -1) | |
286 | || regno == gdbarch_tdep (current_gdbarch)->ppc_ps_regnum) | |
287 | regcache_collect (gdbarch_tdep (current_gdbarch)->ppc_ps_regnum, | |
288 | regp + PT_MSR); | |
fdb28ac4 KB |
289 | } |
290 | ||
50c9bd31 | 291 | void |
8ae45c11 | 292 | supply_fpregset (gdb_fpregset_t * fpregsetp) |
c877c8e6 KB |
293 | { |
294 | int regi; | |
295 | for (regi = 0; regi < 32; regi++) | |
296 | { | |
297 | supply_register (FP0_REGNUM + regi, (char *) (*fpregsetp + regi)); | |
298 | } | |
299 | } | |
fdb28ac4 KB |
300 | |
301 | /* Given a pointer to a floating point register set in /proc format | |
302 | (fpregset_t *), update the register specified by REGNO from gdb's idea | |
303 | of the current floating point register set. If REGNO is -1, update | |
304 | them all. */ | |
305 | ||
306 | void | |
8ae45c11 | 307 | fill_fpregset (gdb_fpregset_t *fpregsetp, int regno) |
fdb28ac4 KB |
308 | { |
309 | int regi; | |
fdb28ac4 KB |
310 | |
311 | for (regi = 0; regi < 32; regi++) | |
312 | { | |
313 | if ((regno == -1) || (regno == FP0_REGNUM + regi)) | |
f00d3753 | 314 | regcache_collect (FP0_REGNUM + regi, (char *) (*fpregsetp + regi)); |
fdb28ac4 KB |
315 | } |
316 | } |