1 /* Cell SPU GNU/Linux multi-architecture debugging support.
2 Copyright (C) 2009 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 2 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, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
26 #include "gdb_string.h"
27 #include "gdb_assert.h"
28 #include "arch-utils.h"
38 #include "ppc-linux-tdep.h"
41 /* This module's target vector. */
42 static struct target_ops spu_ops;
44 /* Number of SPE objects loaded into the current inferior. */
45 static int spu_nr_solib;
47 /* Stand-alone SPE executable? */
48 #define spu_standalone_p() \
49 (symfile_objfile && symfile_objfile->obfd \
50 && bfd_get_arch (symfile_objfile->obfd) == bfd_arch_spu)
52 /* PPU side system calls. */
53 #define INSTR_SC 0x44000002
54 #define NR_spu_run 0x0116
56 /* If the PPU thread is currently stopped on a spu_run system call,
57 return to FD and ADDR the file handle and NPC parameter address
58 used with the system call. Return non-zero if successful. */
60 parse_spufs_run (ptid_t ptid, int *fd, CORE_ADDR *addr)
62 enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch);
63 struct gdbarch_tdep *tdep;
64 struct regcache *regcache;
69 /* If we're not on PPU, there's nothing to detect. */
70 if (gdbarch_bfd_arch_info (target_gdbarch)->arch != bfd_arch_powerpc)
73 /* Get PPU-side registers. */
74 regcache = get_thread_arch_regcache (ptid, target_gdbarch);
75 tdep = gdbarch_tdep (target_gdbarch);
77 /* Fetch instruction preceding current NIP. */
78 if (target_read_memory (regcache_read_pc (regcache) - 4, buf, 4) != 0)
80 /* It should be a "sc" instruction. */
81 if (extract_unsigned_integer (buf, 4, byte_order) != INSTR_SC)
83 /* System call number should be NR_spu_run. */
84 regcache_cooked_read_unsigned (regcache, tdep->ppc_gp0_regnum, ®val);
85 if (regval != NR_spu_run)
88 /* Register 3 contains fd, register 4 the NPC param pointer. */
89 regcache_cooked_read_unsigned (regcache, PPC_ORIG_R3_REGNUM, ®val);
91 regcache_cooked_read_unsigned (regcache, tdep->ppc_gp0_regnum + 4, ®val);
92 *addr = (CORE_ADDR) regval;
96 /* Find gdbarch for SPU context SPUFS_FD. */
97 static struct gdbarch *
98 spu_gdbarch (int spufs_fd)
100 struct gdbarch_info info;
101 gdbarch_info_init (&info);
102 info.bfd_arch_info = bfd_lookup_arch (bfd_arch_spu, bfd_mach_spu);
103 info.byte_order = BFD_ENDIAN_BIG;
104 info.osabi = GDB_OSABI_LINUX;
105 info.tdep_info = (void *) &spufs_fd;
106 return gdbarch_find_by_info (info);
109 /* Override the to_thread_architecture routine. */
110 static struct gdbarch *
111 spu_thread_architecture (struct target_ops *ops, ptid_t ptid)
114 CORE_ADDR spufs_addr;
116 if (parse_spufs_run (ptid, &spufs_fd, &spufs_addr))
117 return spu_gdbarch (spufs_fd);
119 return target_gdbarch;
122 /* Override the to_region_ok_for_hw_watchpoint routine. */
124 spu_region_ok_for_hw_watchpoint (CORE_ADDR addr, int len)
126 struct target_ops *ops_beneath = find_target_beneath (&spu_ops);
127 while (ops_beneath && !ops_beneath->to_region_ok_for_hw_watchpoint)
128 ops_beneath = find_target_beneath (ops_beneath);
130 /* We cannot watch SPU local store. */
131 if (SPUADDR_SPU (addr) != -1)
135 return ops_beneath->to_region_ok_for_hw_watchpoint (addr, len);
140 /* Override the to_fetch_registers routine. */
142 spu_fetch_registers (struct target_ops *ops,
143 struct regcache *regcache, int regno)
145 struct gdbarch *gdbarch = get_regcache_arch (regcache);
146 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
147 struct target_ops *ops_beneath = find_target_beneath (ops);
149 CORE_ADDR spufs_addr;
151 /* This version applies only if we're currently in spu_run. */
152 if (gdbarch_bfd_arch_info (gdbarch)->arch != bfd_arch_spu)
154 while (ops_beneath && !ops_beneath->to_fetch_registers)
155 ops_beneath = find_target_beneath (ops_beneath);
157 gdb_assert (ops_beneath);
158 ops_beneath->to_fetch_registers (ops_beneath, regcache, regno);
162 /* We must be stopped on a spu_run system call. */
163 if (!parse_spufs_run (inferior_ptid, &spufs_fd, &spufs_addr))
166 /* The ID register holds the spufs file handle. */
167 if (regno == -1 || regno == SPU_ID_REGNUM)
170 store_unsigned_integer (buf, 4, byte_order, spufs_fd);
171 regcache_raw_supply (regcache, SPU_ID_REGNUM, buf);
174 /* The NPC register is found in PPC memory at SPUFS_ADDR. */
175 if (regno == -1 || regno == SPU_PC_REGNUM)
179 if (target_read (ops_beneath, TARGET_OBJECT_MEMORY, NULL,
180 buf, spufs_addr, sizeof buf) == sizeof buf)
181 regcache_raw_supply (regcache, SPU_PC_REGNUM, buf);
184 /* The GPRs are found in the "regs" spufs file. */
185 if (regno == -1 || (regno >= 0 && regno < SPU_NUM_GPRS))
187 char buf[16 * SPU_NUM_GPRS], annex[32];
190 xsnprintf (annex, sizeof annex, "%d/regs", spufs_fd);
191 if (target_read (ops_beneath, TARGET_OBJECT_SPU, annex,
192 buf, 0, sizeof buf) == sizeof buf)
193 for (i = 0; i < SPU_NUM_GPRS; i++)
194 regcache_raw_supply (regcache, i, buf + i*16);
198 /* Override the to_store_registers routine. */
200 spu_store_registers (struct target_ops *ops,
201 struct regcache *regcache, int regno)
203 struct gdbarch *gdbarch = get_regcache_arch (regcache);
204 struct target_ops *ops_beneath = find_target_beneath (ops);
206 CORE_ADDR spufs_addr;
208 /* This version applies only if we're currently in spu_run. */
209 if (gdbarch_bfd_arch_info (gdbarch)->arch != bfd_arch_spu)
211 while (ops_beneath && !ops_beneath->to_fetch_registers)
212 ops_beneath = find_target_beneath (ops_beneath);
214 gdb_assert (ops_beneath);
215 ops_beneath->to_store_registers (ops_beneath, regcache, regno);
219 /* We must be stopped on a spu_run system call. */
220 if (!parse_spufs_run (inferior_ptid, &spufs_fd, &spufs_addr))
223 /* The NPC register is found in PPC memory at SPUFS_ADDR. */
224 if (regno == -1 || regno == SPU_PC_REGNUM)
227 regcache_raw_collect (regcache, SPU_PC_REGNUM, buf);
229 target_write (ops_beneath, TARGET_OBJECT_MEMORY, NULL,
230 buf, spufs_addr, sizeof buf);
233 /* The GPRs are found in the "regs" spufs file. */
234 if (regno == -1 || (regno >= 0 && regno < SPU_NUM_GPRS))
236 char buf[16 * SPU_NUM_GPRS], annex[32];
239 for (i = 0; i < SPU_NUM_GPRS; i++)
240 regcache_raw_collect (regcache, i, buf + i*16);
242 xsnprintf (annex, sizeof annex, "%d/regs", spufs_fd);
243 target_write (ops_beneath, TARGET_OBJECT_SPU, annex,
248 /* Override the to_xfer_partial routine. */
250 spu_xfer_partial (struct target_ops *ops, enum target_object object,
251 const char *annex, gdb_byte *readbuf,
252 const gdb_byte *writebuf, ULONGEST offset, LONGEST 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);
266 if (fd >= 0 && addr < SPU_LS_SIZE)
268 xsnprintf (mem_annex, sizeof mem_annex, "%d/mem", fd);
269 return ops_beneath->to_xfer_partial (ops_beneath, TARGET_OBJECT_SPU,
270 mem_annex, readbuf, writebuf,
275 return ops_beneath->to_xfer_partial (ops_beneath, object, annex,
276 readbuf, writebuf, offset, len);
279 /* Override the to_search_memory routine. */
281 spu_search_memory (struct target_ops* ops,
282 CORE_ADDR start_addr, ULONGEST search_space_len,
283 const gdb_byte *pattern, ULONGEST pattern_len,
284 CORE_ADDR *found_addrp)
286 struct target_ops *ops_beneath = find_target_beneath (ops);
287 while (ops_beneath && !ops_beneath->to_search_memory)
288 ops_beneath = find_target_beneath (ops_beneath);
290 /* For SPU local store, always fall back to the simple method. Likewise
291 if we do not have any target-specific special implementation. */
292 if (!ops_beneath || SPUADDR_SPU (start_addr) >= 0)
293 return simple_search_memory (ops,
294 start_addr, search_space_len,
295 pattern, pattern_len, found_addrp);
297 return ops_beneath->to_search_memory (ops_beneath,
298 start_addr, search_space_len,
299 pattern, pattern_len, found_addrp);
303 /* Push and pop the SPU multi-architecture support target. */
306 spu_multiarch_activate (void)
308 /* If GDB was configured without SPU architecture support,
309 we cannot install SPU multi-architecture support either. */
310 if (spu_gdbarch (-1) == NULL)
313 push_target (&spu_ops);
315 /* Make sure the thread architecture is re-evaluated. */
316 registers_changed ();
320 spu_multiarch_deactivate (void)
322 unpush_target (&spu_ops);
324 /* Make sure the thread architecture is re-evaluated. */
325 registers_changed ();
329 spu_multiarch_inferior_created (struct target_ops *ops, int from_tty)
331 if (spu_standalone_p ())
332 spu_multiarch_activate ();
336 spu_multiarch_solib_loaded (struct so_list *so)
338 if (!spu_standalone_p ())
339 if (so->abfd && bfd_get_arch (so->abfd) == bfd_arch_spu)
340 if (spu_nr_solib++ == 0)
341 spu_multiarch_activate ();
345 spu_multiarch_solib_unloaded (struct so_list *so)
347 if (!spu_standalone_p ())
348 if (so->abfd && bfd_get_arch (so->abfd) == bfd_arch_spu)
349 if (--spu_nr_solib == 0)
350 spu_multiarch_deactivate ();
354 spu_mourn_inferior (struct target_ops *ops)
356 struct target_ops *ops_beneath = find_target_beneath (ops);
357 while (ops_beneath && !ops_beneath->to_mourn_inferior)
358 ops_beneath = find_target_beneath (ops_beneath);
360 gdb_assert (ops_beneath);
361 ops_beneath->to_mourn_inferior (ops_beneath);
362 spu_multiarch_deactivate ();
366 /* Initialize the SPU multi-architecture support target. */
371 spu_ops.to_shortname = "spu";
372 spu_ops.to_longname = "SPU multi-architecture support.";
373 spu_ops.to_doc = "SPU multi-architecture support.";
374 spu_ops.to_mourn_inferior = spu_mourn_inferior;
375 spu_ops.to_fetch_registers = spu_fetch_registers;
376 spu_ops.to_store_registers = spu_store_registers;
377 spu_ops.to_xfer_partial = spu_xfer_partial;
378 spu_ops.to_search_memory = spu_search_memory;
379 spu_ops.to_region_ok_for_hw_watchpoint = spu_region_ok_for_hw_watchpoint;
380 spu_ops.to_thread_architecture = spu_thread_architecture;
381 spu_ops.to_stratum = arch_stratum;
382 spu_ops.to_magic = OPS_MAGIC;
386 _initialize_spu_multiarch (void)
388 /* Install ourselves on the target stack. */
390 add_target (&spu_ops);
392 /* Install observers to watch for SPU objects. */
393 observer_attach_inferior_created (spu_multiarch_inferior_created);
394 observer_attach_solib_loaded (spu_multiarch_solib_loaded);
395 observer_attach_solib_unloaded (spu_multiarch_solib_unloaded);