]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/spu-multiarch.c
Update years in copyright notice for the GDB files.
[thirdparty/binutils-gdb.git] / gdb / spu-multiarch.c
1 /* Cell SPU GNU/Linux multi-architecture debugging support.
2 Copyright (C) 2009-2013 Free Software Foundation, Inc.
3
4 Contributed by Ulrich Weigand <uweigand@de.ibm.com>.
5
6 This file is part of GDB.
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20
21 #include "defs.h"
22 #include "gdbcore.h"
23 #include "gdbcmd.h"
24 #include "gdb_string.h"
25 #include "gdb_assert.h"
26 #include "arch-utils.h"
27 #include "observer.h"
28 #include "inferior.h"
29 #include "regcache.h"
30 #include "symfile.h"
31 #include "objfiles.h"
32 #include "solib.h"
33 #include "solist.h"
34
35 #include "ppc-tdep.h"
36 #include "ppc-linux-tdep.h"
37 #include "spu-tdep.h"
38
39 /* This module's target vector. */
40 static struct target_ops spu_ops;
41
42 /* Number of SPE objects loaded into the current inferior. */
43 static int spu_nr_solib;
44
45 /* Stand-alone SPE executable? */
46 #define spu_standalone_p() \
47 (symfile_objfile && symfile_objfile->obfd \
48 && bfd_get_arch (symfile_objfile->obfd) == bfd_arch_spu)
49
50 /* PPU side system calls. */
51 #define INSTR_SC 0x44000002
52 #define NR_spu_run 0x0116
53
54 /* If the PPU thread is currently stopped on a spu_run system call,
55 return to FD and ADDR the file handle and NPC parameter address
56 used with the system call. Return non-zero if successful. */
57 static int
58 parse_spufs_run (ptid_t ptid, int *fd, CORE_ADDR *addr)
59 {
60 enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
61 struct gdbarch_tdep *tdep;
62 struct regcache *regcache;
63 char buf[4];
64 ULONGEST regval;
65
66 /* If we're not on PPU, there's nothing to detect. */
67 if (gdbarch_bfd_arch_info (target_gdbarch ())->arch != bfd_arch_powerpc)
68 return 0;
69
70 /* Get PPU-side registers. */
71 regcache = get_thread_arch_regcache (ptid, target_gdbarch ());
72 tdep = gdbarch_tdep (target_gdbarch ());
73
74 /* Fetch instruction preceding current NIP. */
75 if (target_read_memory (regcache_read_pc (regcache) - 4, buf, 4) != 0)
76 return 0;
77 /* It should be a "sc" instruction. */
78 if (extract_unsigned_integer (buf, 4, byte_order) != INSTR_SC)
79 return 0;
80 /* System call number should be NR_spu_run. */
81 regcache_cooked_read_unsigned (regcache, tdep->ppc_gp0_regnum, &regval);
82 if (regval != NR_spu_run)
83 return 0;
84
85 /* Register 3 contains fd, register 4 the NPC param pointer. */
86 regcache_cooked_read_unsigned (regcache, PPC_ORIG_R3_REGNUM, &regval);
87 *fd = (int) regval;
88 regcache_cooked_read_unsigned (regcache, tdep->ppc_gp0_regnum + 4, &regval);
89 *addr = (CORE_ADDR) regval;
90 return 1;
91 }
92
93 /* Find gdbarch for SPU context SPUFS_FD. */
94 static struct gdbarch *
95 spu_gdbarch (int spufs_fd)
96 {
97 struct gdbarch_info info;
98 gdbarch_info_init (&info);
99 info.bfd_arch_info = bfd_lookup_arch (bfd_arch_spu, bfd_mach_spu);
100 info.byte_order = BFD_ENDIAN_BIG;
101 info.osabi = GDB_OSABI_LINUX;
102 info.tdep_info = (void *) &spufs_fd;
103 return gdbarch_find_by_info (info);
104 }
105
106 /* Override the to_thread_architecture routine. */
107 static struct gdbarch *
108 spu_thread_architecture (struct target_ops *ops, ptid_t ptid)
109 {
110 int spufs_fd;
111 CORE_ADDR spufs_addr;
112
113 if (parse_spufs_run (ptid, &spufs_fd, &spufs_addr))
114 return spu_gdbarch (spufs_fd);
115
116 return target_gdbarch ();
117 }
118
119 /* Override the to_region_ok_for_hw_watchpoint routine. */
120 static int
121 spu_region_ok_for_hw_watchpoint (CORE_ADDR addr, int len)
122 {
123 struct target_ops *ops_beneath = find_target_beneath (&spu_ops);
124 while (ops_beneath && !ops_beneath->to_region_ok_for_hw_watchpoint)
125 ops_beneath = find_target_beneath (ops_beneath);
126
127 /* We cannot watch SPU local store. */
128 if (SPUADDR_SPU (addr) != -1)
129 return 0;
130
131 if (ops_beneath)
132 return ops_beneath->to_region_ok_for_hw_watchpoint (addr, len);
133
134 return 0;
135 }
136
137 /* Override the to_fetch_registers routine. */
138 static void
139 spu_fetch_registers (struct target_ops *ops,
140 struct regcache *regcache, int regno)
141 {
142 struct gdbarch *gdbarch = get_regcache_arch (regcache);
143 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
144 struct target_ops *ops_beneath = find_target_beneath (ops);
145 int spufs_fd;
146 CORE_ADDR spufs_addr;
147
148 /* This version applies only if we're currently in spu_run. */
149 if (gdbarch_bfd_arch_info (gdbarch)->arch != bfd_arch_spu)
150 {
151 while (ops_beneath && !ops_beneath->to_fetch_registers)
152 ops_beneath = find_target_beneath (ops_beneath);
153
154 gdb_assert (ops_beneath);
155 ops_beneath->to_fetch_registers (ops_beneath, regcache, regno);
156 return;
157 }
158
159 /* We must be stopped on a spu_run system call. */
160 if (!parse_spufs_run (inferior_ptid, &spufs_fd, &spufs_addr))
161 return;
162
163 /* The ID register holds the spufs file handle. */
164 if (regno == -1 || regno == SPU_ID_REGNUM)
165 {
166 char buf[4];
167 store_unsigned_integer (buf, 4, byte_order, spufs_fd);
168 regcache_raw_supply (regcache, SPU_ID_REGNUM, buf);
169 }
170
171 /* The NPC register is found in PPC memory at SPUFS_ADDR. */
172 if (regno == -1 || regno == SPU_PC_REGNUM)
173 {
174 char buf[4];
175
176 if (target_read (ops_beneath, TARGET_OBJECT_MEMORY, NULL,
177 buf, spufs_addr, sizeof buf) == sizeof buf)
178 regcache_raw_supply (regcache, SPU_PC_REGNUM, buf);
179 }
180
181 /* The GPRs are found in the "regs" spufs file. */
182 if (regno == -1 || (regno >= 0 && regno < SPU_NUM_GPRS))
183 {
184 char buf[16 * SPU_NUM_GPRS], annex[32];
185 int i;
186
187 xsnprintf (annex, sizeof annex, "%d/regs", spufs_fd);
188 if (target_read (ops_beneath, TARGET_OBJECT_SPU, annex,
189 buf, 0, sizeof buf) == sizeof buf)
190 for (i = 0; i < SPU_NUM_GPRS; i++)
191 regcache_raw_supply (regcache, i, buf + i*16);
192 }
193 }
194
195 /* Override the to_store_registers routine. */
196 static void
197 spu_store_registers (struct target_ops *ops,
198 struct regcache *regcache, int regno)
199 {
200 struct gdbarch *gdbarch = get_regcache_arch (regcache);
201 struct target_ops *ops_beneath = find_target_beneath (ops);
202 int spufs_fd;
203 CORE_ADDR spufs_addr;
204
205 /* This version applies only if we're currently in spu_run. */
206 if (gdbarch_bfd_arch_info (gdbarch)->arch != bfd_arch_spu)
207 {
208 while (ops_beneath && !ops_beneath->to_fetch_registers)
209 ops_beneath = find_target_beneath (ops_beneath);
210
211 gdb_assert (ops_beneath);
212 ops_beneath->to_store_registers (ops_beneath, regcache, regno);
213 return;
214 }
215
216 /* We must be stopped on a spu_run system call. */
217 if (!parse_spufs_run (inferior_ptid, &spufs_fd, &spufs_addr))
218 return;
219
220 /* The NPC register is found in PPC memory at SPUFS_ADDR. */
221 if (regno == -1 || regno == SPU_PC_REGNUM)
222 {
223 char buf[4];
224 regcache_raw_collect (regcache, SPU_PC_REGNUM, buf);
225
226 target_write (ops_beneath, TARGET_OBJECT_MEMORY, NULL,
227 buf, spufs_addr, sizeof buf);
228 }
229
230 /* The GPRs are found in the "regs" spufs file. */
231 if (regno == -1 || (regno >= 0 && regno < SPU_NUM_GPRS))
232 {
233 char buf[16 * SPU_NUM_GPRS], annex[32];
234 int i;
235
236 for (i = 0; i < SPU_NUM_GPRS; i++)
237 regcache_raw_collect (regcache, i, buf + i*16);
238
239 xsnprintf (annex, sizeof annex, "%d/regs", spufs_fd);
240 target_write (ops_beneath, TARGET_OBJECT_SPU, annex,
241 buf, 0, sizeof buf);
242 }
243 }
244
245 /* Override the to_xfer_partial routine. */
246 static LONGEST
247 spu_xfer_partial (struct target_ops *ops, enum target_object object,
248 const char *annex, gdb_byte *readbuf,
249 const gdb_byte *writebuf, ULONGEST offset, LONGEST len)
250 {
251 struct target_ops *ops_beneath = find_target_beneath (ops);
252 while (ops_beneath && !ops_beneath->to_xfer_partial)
253 ops_beneath = find_target_beneath (ops_beneath);
254 gdb_assert (ops_beneath);
255
256 /* Use the "mem" spufs file to access SPU local store. */
257 if (object == TARGET_OBJECT_MEMORY)
258 {
259 int fd = SPUADDR_SPU (offset);
260 CORE_ADDR addr = SPUADDR_ADDR (offset);
261 char mem_annex[32], lslr_annex[32];
262 gdb_byte buf[32];
263 ULONGEST lslr;
264 LONGEST ret;
265
266 if (fd >= 0)
267 {
268 xsnprintf (mem_annex, sizeof mem_annex, "%d/mem", fd);
269 ret = ops_beneath->to_xfer_partial (ops_beneath, TARGET_OBJECT_SPU,
270 mem_annex, readbuf, writebuf,
271 addr, len);
272 if (ret > 0)
273 return ret;
274
275 /* SPU local store access wraps the address around at the
276 local store limit. We emulate this here. To avoid needing
277 an extra access to retrieve the LSLR, we only do that after
278 trying the original address first, and getting end-of-file. */
279 xsnprintf (lslr_annex, sizeof lslr_annex, "%d/lslr", fd);
280 memset (buf, 0, sizeof buf);
281 if (ops_beneath->to_xfer_partial (ops_beneath, TARGET_OBJECT_SPU,
282 lslr_annex, buf, NULL,
283 0, sizeof buf) <= 0)
284 return ret;
285
286 lslr = strtoulst (buf, NULL, 16);
287 return ops_beneath->to_xfer_partial (ops_beneath, TARGET_OBJECT_SPU,
288 mem_annex, readbuf, writebuf,
289 addr & lslr, len);
290 }
291 }
292
293 return ops_beneath->to_xfer_partial (ops_beneath, object, annex,
294 readbuf, writebuf, offset, len);
295 }
296
297 /* Override the to_search_memory routine. */
298 static int
299 spu_search_memory (struct target_ops* ops,
300 CORE_ADDR start_addr, ULONGEST search_space_len,
301 const gdb_byte *pattern, ULONGEST pattern_len,
302 CORE_ADDR *found_addrp)
303 {
304 struct target_ops *ops_beneath = find_target_beneath (ops);
305 while (ops_beneath && !ops_beneath->to_search_memory)
306 ops_beneath = find_target_beneath (ops_beneath);
307
308 /* For SPU local store, always fall back to the simple method. Likewise
309 if we do not have any target-specific special implementation. */
310 if (!ops_beneath || SPUADDR_SPU (start_addr) >= 0)
311 return simple_search_memory (ops,
312 start_addr, search_space_len,
313 pattern, pattern_len, found_addrp);
314
315 return ops_beneath->to_search_memory (ops_beneath,
316 start_addr, search_space_len,
317 pattern, pattern_len, found_addrp);
318 }
319
320
321 /* Push and pop the SPU multi-architecture support target. */
322
323 static void
324 spu_multiarch_activate (void)
325 {
326 /* If GDB was configured without SPU architecture support,
327 we cannot install SPU multi-architecture support either. */
328 if (spu_gdbarch (-1) == NULL)
329 return;
330
331 push_target (&spu_ops);
332
333 /* Make sure the thread architecture is re-evaluated. */
334 registers_changed ();
335 }
336
337 static void
338 spu_multiarch_deactivate (void)
339 {
340 unpush_target (&spu_ops);
341
342 /* Make sure the thread architecture is re-evaluated. */
343 registers_changed ();
344 }
345
346 static void
347 spu_multiarch_inferior_created (struct target_ops *ops, int from_tty)
348 {
349 if (spu_standalone_p ())
350 spu_multiarch_activate ();
351 }
352
353 static void
354 spu_multiarch_solib_loaded (struct so_list *so)
355 {
356 if (!spu_standalone_p ())
357 if (so->abfd && bfd_get_arch (so->abfd) == bfd_arch_spu)
358 if (spu_nr_solib++ == 0)
359 spu_multiarch_activate ();
360 }
361
362 static void
363 spu_multiarch_solib_unloaded (struct so_list *so)
364 {
365 if (!spu_standalone_p ())
366 if (so->abfd && bfd_get_arch (so->abfd) == bfd_arch_spu)
367 if (--spu_nr_solib == 0)
368 spu_multiarch_deactivate ();
369 }
370
371 static void
372 spu_mourn_inferior (struct target_ops *ops)
373 {
374 struct target_ops *ops_beneath = find_target_beneath (ops);
375 while (ops_beneath && !ops_beneath->to_mourn_inferior)
376 ops_beneath = find_target_beneath (ops_beneath);
377
378 gdb_assert (ops_beneath);
379 ops_beneath->to_mourn_inferior (ops_beneath);
380 spu_multiarch_deactivate ();
381 }
382
383
384 /* Initialize the SPU multi-architecture support target. */
385
386 static void
387 init_spu_ops (void)
388 {
389 spu_ops.to_shortname = "spu";
390 spu_ops.to_longname = "SPU multi-architecture support.";
391 spu_ops.to_doc = "SPU multi-architecture support.";
392 spu_ops.to_mourn_inferior = spu_mourn_inferior;
393 spu_ops.to_fetch_registers = spu_fetch_registers;
394 spu_ops.to_store_registers = spu_store_registers;
395 spu_ops.to_xfer_partial = spu_xfer_partial;
396 spu_ops.to_search_memory = spu_search_memory;
397 spu_ops.to_region_ok_for_hw_watchpoint = spu_region_ok_for_hw_watchpoint;
398 spu_ops.to_thread_architecture = spu_thread_architecture;
399 spu_ops.to_stratum = arch_stratum;
400 spu_ops.to_magic = OPS_MAGIC;
401 }
402
403 /* -Wmissing-prototypes */
404 extern initialize_file_ftype _initialize_spu_multiarch;
405
406 void
407 _initialize_spu_multiarch (void)
408 {
409 /* Install ourselves on the target stack. */
410 init_spu_ops ();
411 add_target (&spu_ops);
412
413 /* Install observers to watch for SPU objects. */
414 observer_attach_inferior_created (spu_multiarch_inferior_created);
415 observer_attach_solib_loaded (spu_multiarch_solib_loaded);
416 observer_attach_solib_unloaded (spu_multiarch_solib_unloaded);
417 }
418