1 /* Cell SPU GNU/Linux multi-architecture debugging support.
2 Copyright (C) 2009-2014 Free Software Foundation, Inc.
4 Contributed by Ulrich Weigand <uweigand@de.ibm.com>.
6 This file is part of GDB.
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.
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.
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/>. */
25 #include "gdb_assert.h"
26 #include "arch-utils.h"
36 #include "ppc-linux-tdep.h"
39 /* This module's target vector. */
40 static struct target_ops spu_ops;
42 /* Number of SPE objects loaded into the current inferior. */
43 static int spu_nr_solib;
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)
50 /* PPU side system calls. */
51 #define INSTR_SC 0x44000002
52 #define NR_spu_run 0x0116
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. */
58 parse_spufs_run (ptid_t ptid, int *fd, CORE_ADDR *addr)
60 enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
61 struct gdbarch_tdep *tdep;
62 struct regcache *regcache;
66 /* If we're not on PPU, there's nothing to detect. */
67 if (gdbarch_bfd_arch_info (target_gdbarch ())->arch != bfd_arch_powerpc)
70 /* Get PPU-side registers. */
71 regcache = get_thread_arch_regcache (ptid, target_gdbarch ());
72 tdep = gdbarch_tdep (target_gdbarch ());
74 /* Fetch instruction preceding current NIP. */
75 if (target_read_memory (regcache_read_pc (regcache) - 4, buf, 4) != 0)
77 /* It should be a "sc" instruction. */
78 if (extract_unsigned_integer (buf, 4, byte_order) != INSTR_SC)
80 /* System call number should be NR_spu_run. */
81 regcache_cooked_read_unsigned (regcache, tdep->ppc_gp0_regnum, ®val);
82 if (regval != NR_spu_run)
85 /* Register 3 contains fd, register 4 the NPC param pointer. */
86 regcache_cooked_read_unsigned (regcache, PPC_ORIG_R3_REGNUM, ®val);
88 regcache_cooked_read_unsigned (regcache, tdep->ppc_gp0_regnum + 4, ®val);
89 *addr = (CORE_ADDR) regval;
93 /* Find gdbarch for SPU context SPUFS_FD. */
94 static struct gdbarch *
95 spu_gdbarch (int spufs_fd)
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);
106 /* Override the to_thread_architecture routine. */
107 static struct gdbarch *
108 spu_thread_architecture (struct target_ops *ops, ptid_t ptid)
111 CORE_ADDR spufs_addr;
113 if (parse_spufs_run (ptid, &spufs_fd, &spufs_addr))
114 return spu_gdbarch (spufs_fd);
116 return target_gdbarch ();
119 /* Override the to_region_ok_for_hw_watchpoint routine. */
121 spu_region_ok_for_hw_watchpoint (CORE_ADDR addr, int len)
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);
127 /* We cannot watch SPU local store. */
128 if (SPUADDR_SPU (addr) != -1)
132 return ops_beneath->to_region_ok_for_hw_watchpoint (addr, len);
137 /* Override the to_fetch_registers routine. */
139 spu_fetch_registers (struct target_ops *ops,
140 struct regcache *regcache, int regno)
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);
146 CORE_ADDR spufs_addr;
148 /* This version applies only if we're currently in spu_run. */
149 if (gdbarch_bfd_arch_info (gdbarch)->arch != bfd_arch_spu)
151 while (ops_beneath && !ops_beneath->to_fetch_registers)
152 ops_beneath = find_target_beneath (ops_beneath);
154 gdb_assert (ops_beneath);
155 ops_beneath->to_fetch_registers (ops_beneath, regcache, regno);
159 /* We must be stopped on a spu_run system call. */
160 if (!parse_spufs_run (inferior_ptid, &spufs_fd, &spufs_addr))
163 /* The ID register holds the spufs file handle. */
164 if (regno == -1 || regno == SPU_ID_REGNUM)
167 store_unsigned_integer (buf, 4, byte_order, spufs_fd);
168 regcache_raw_supply (regcache, SPU_ID_REGNUM, buf);
171 /* The NPC register is found in PPC memory at SPUFS_ADDR. */
172 if (regno == -1 || regno == SPU_PC_REGNUM)
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);
181 /* The GPRs are found in the "regs" spufs file. */
182 if (regno == -1 || (regno >= 0 && regno < SPU_NUM_GPRS))
184 gdb_byte buf[16 * SPU_NUM_GPRS];
188 xsnprintf (annex, sizeof annex, "%d/regs", spufs_fd);
189 if (target_read (ops_beneath, TARGET_OBJECT_SPU, annex,
190 buf, 0, sizeof buf) == sizeof buf)
191 for (i = 0; i < SPU_NUM_GPRS; i++)
192 regcache_raw_supply (regcache, i, buf + i*16);
196 /* Override the to_store_registers routine. */
198 spu_store_registers (struct target_ops *ops,
199 struct regcache *regcache, int regno)
201 struct gdbarch *gdbarch = get_regcache_arch (regcache);
202 struct target_ops *ops_beneath = find_target_beneath (ops);
204 CORE_ADDR spufs_addr;
206 /* This version applies only if we're currently in spu_run. */
207 if (gdbarch_bfd_arch_info (gdbarch)->arch != bfd_arch_spu)
209 while (ops_beneath && !ops_beneath->to_fetch_registers)
210 ops_beneath = find_target_beneath (ops_beneath);
212 gdb_assert (ops_beneath);
213 ops_beneath->to_store_registers (ops_beneath, regcache, regno);
217 /* We must be stopped on a spu_run system call. */
218 if (!parse_spufs_run (inferior_ptid, &spufs_fd, &spufs_addr))
221 /* The NPC register is found in PPC memory at SPUFS_ADDR. */
222 if (regno == -1 || regno == SPU_PC_REGNUM)
225 regcache_raw_collect (regcache, SPU_PC_REGNUM, buf);
227 target_write (ops_beneath, TARGET_OBJECT_MEMORY, NULL,
228 buf, spufs_addr, sizeof buf);
231 /* The GPRs are found in the "regs" spufs file. */
232 if (regno == -1 || (regno >= 0 && regno < SPU_NUM_GPRS))
234 gdb_byte buf[16 * SPU_NUM_GPRS];
238 for (i = 0; i < SPU_NUM_GPRS; i++)
239 regcache_raw_collect (regcache, i, buf + i*16);
241 xsnprintf (annex, sizeof annex, "%d/regs", spufs_fd);
242 target_write (ops_beneath, TARGET_OBJECT_SPU, annex,
247 /* Override the to_xfer_partial routine. */
248 static enum target_xfer_status
249 spu_xfer_partial (struct target_ops *ops, enum target_object object,
250 const char *annex, gdb_byte *readbuf,
251 const gdb_byte *writebuf, ULONGEST offset, ULONGEST len,
252 ULONGEST *xfered_len)
254 struct target_ops *ops_beneath = find_target_beneath (ops);
255 while (ops_beneath && !ops_beneath->to_xfer_partial)
256 ops_beneath = find_target_beneath (ops_beneath);
257 gdb_assert (ops_beneath);
259 /* Use the "mem" spufs file to access SPU local store. */
260 if (object == TARGET_OBJECT_MEMORY)
262 int fd = SPUADDR_SPU (offset);
263 CORE_ADDR addr = SPUADDR_ADDR (offset);
264 char mem_annex[32], lslr_annex[32];
267 enum target_xfer_status ret;
271 xsnprintf (mem_annex, sizeof mem_annex, "%d/mem", fd);
272 ret = ops_beneath->to_xfer_partial (ops_beneath, TARGET_OBJECT_SPU,
273 mem_annex, readbuf, writebuf,
274 addr, len, xfered_len);
275 if (ret == TARGET_XFER_OK)
278 /* SPU local store access wraps the address around at the
279 local store limit. We emulate this here. To avoid needing
280 an extra access to retrieve the LSLR, we only do that after
281 trying the original address first, and getting end-of-file. */
282 xsnprintf (lslr_annex, sizeof lslr_annex, "%d/lslr", fd);
283 memset (buf, 0, sizeof buf);
284 if (ops_beneath->to_xfer_partial (ops_beneath, TARGET_OBJECT_SPU,
285 lslr_annex, buf, NULL,
286 0, sizeof buf, xfered_len)
290 lslr = strtoulst ((char *) buf, NULL, 16);
291 return ops_beneath->to_xfer_partial (ops_beneath, TARGET_OBJECT_SPU,
292 mem_annex, readbuf, writebuf,
293 addr & lslr, len, xfered_len);
297 return ops_beneath->to_xfer_partial (ops_beneath, object, annex,
298 readbuf, writebuf, offset, len, xfered_len);
301 /* Override the to_search_memory routine. */
303 spu_search_memory (struct target_ops* ops,
304 CORE_ADDR start_addr, ULONGEST search_space_len,
305 const gdb_byte *pattern, ULONGEST pattern_len,
306 CORE_ADDR *found_addrp)
308 struct target_ops *ops_beneath = find_target_beneath (ops);
309 while (ops_beneath && !ops_beneath->to_search_memory)
310 ops_beneath = find_target_beneath (ops_beneath);
312 /* For SPU local store, always fall back to the simple method. Likewise
313 if we do not have any target-specific special implementation. */
314 if (!ops_beneath || SPUADDR_SPU (start_addr) >= 0)
315 return simple_search_memory (ops,
316 start_addr, search_space_len,
317 pattern, pattern_len, found_addrp);
319 return ops_beneath->to_search_memory (ops_beneath,
320 start_addr, search_space_len,
321 pattern, pattern_len, found_addrp);
325 /* Push and pop the SPU multi-architecture support target. */
328 spu_multiarch_activate (void)
330 /* If GDB was configured without SPU architecture support,
331 we cannot install SPU multi-architecture support either. */
332 if (spu_gdbarch (-1) == NULL)
335 push_target (&spu_ops);
337 /* Make sure the thread architecture is re-evaluated. */
338 registers_changed ();
342 spu_multiarch_deactivate (void)
344 unpush_target (&spu_ops);
346 /* Make sure the thread architecture is re-evaluated. */
347 registers_changed ();
351 spu_multiarch_inferior_created (struct target_ops *ops, int from_tty)
353 if (spu_standalone_p ())
354 spu_multiarch_activate ();
358 spu_multiarch_solib_loaded (struct so_list *so)
360 if (!spu_standalone_p ())
361 if (so->abfd && bfd_get_arch (so->abfd) == bfd_arch_spu)
362 if (spu_nr_solib++ == 0)
363 spu_multiarch_activate ();
367 spu_multiarch_solib_unloaded (struct so_list *so)
369 if (!spu_standalone_p ())
370 if (so->abfd && bfd_get_arch (so->abfd) == bfd_arch_spu)
371 if (--spu_nr_solib == 0)
372 spu_multiarch_deactivate ();
376 spu_mourn_inferior (struct target_ops *ops)
378 struct target_ops *ops_beneath = find_target_beneath (ops);
379 while (ops_beneath && !ops_beneath->to_mourn_inferior)
380 ops_beneath = find_target_beneath (ops_beneath);
382 gdb_assert (ops_beneath);
383 ops_beneath->to_mourn_inferior (ops_beneath);
384 spu_multiarch_deactivate ();
388 /* Initialize the SPU multi-architecture support target. */
393 spu_ops.to_shortname = "spu";
394 spu_ops.to_longname = "SPU multi-architecture support.";
395 spu_ops.to_doc = "SPU multi-architecture support.";
396 spu_ops.to_mourn_inferior = spu_mourn_inferior;
397 spu_ops.to_fetch_registers = spu_fetch_registers;
398 spu_ops.to_store_registers = spu_store_registers;
399 spu_ops.to_xfer_partial = spu_xfer_partial;
400 spu_ops.to_search_memory = spu_search_memory;
401 spu_ops.to_region_ok_for_hw_watchpoint = spu_region_ok_for_hw_watchpoint;
402 spu_ops.to_thread_architecture = spu_thread_architecture;
403 spu_ops.to_stratum = arch_stratum;
404 spu_ops.to_magic = OPS_MAGIC;
407 /* -Wmissing-prototypes */
408 extern initialize_file_ftype _initialize_spu_multiarch;
411 _initialize_spu_multiarch (void)
413 /* Install ourselves on the target stack. */
415 complete_target_initialization (&spu_ops);
417 /* Install observers to watch for SPU objects. */
418 observer_attach_inferior_created (spu_multiarch_inferior_created);
419 observer_attach_solib_loaded (spu_multiarch_solib_loaded);
420 observer_attach_solib_unloaded (spu_multiarch_solib_unloaded);