1 /* Cell SPU GNU/Linux multi-architecture debugging support.
2 Copyright (C) 2009-2015 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/>. */
24 #include "arch-utils.h"
34 #include "ppc-linux-tdep.h"
37 /* This module's target vector. */
38 static struct target_ops spu_ops
;
40 /* Number of SPE objects loaded into the current inferior. */
41 static int spu_nr_solib
;
43 /* Stand-alone SPE executable? */
44 #define spu_standalone_p() \
45 (symfile_objfile && symfile_objfile->obfd \
46 && bfd_get_arch (symfile_objfile->obfd) == bfd_arch_spu)
48 /* PPU side system calls. */
49 #define INSTR_SC 0x44000002
50 #define NR_spu_run 0x0116
52 /* If the PPU thread is currently stopped on a spu_run system call,
53 return to FD and ADDR the file handle and NPC parameter address
54 used with the system call. Return non-zero if successful. */
56 parse_spufs_run (ptid_t ptid
, int *fd
, CORE_ADDR
*addr
)
58 enum bfd_endian byte_order
= gdbarch_byte_order (target_gdbarch ());
59 struct gdbarch_tdep
*tdep
;
60 struct regcache
*regcache
;
64 /* If we're not on PPU, there's nothing to detect. */
65 if (gdbarch_bfd_arch_info (target_gdbarch ())->arch
!= bfd_arch_powerpc
)
68 /* Get PPU-side registers. */
69 regcache
= get_thread_arch_regcache (ptid
, target_gdbarch ());
70 tdep
= gdbarch_tdep (target_gdbarch ());
72 /* Fetch instruction preceding current NIP. */
73 if (target_read_memory (regcache_read_pc (regcache
) - 4, buf
, 4) != 0)
75 /* It should be a "sc" instruction. */
76 if (extract_unsigned_integer (buf
, 4, byte_order
) != INSTR_SC
)
78 /* System call number should be NR_spu_run. */
79 regcache_cooked_read_unsigned (regcache
, tdep
->ppc_gp0_regnum
, ®val
);
80 if (regval
!= NR_spu_run
)
83 /* Register 3 contains fd, register 4 the NPC param pointer. */
84 regcache_cooked_read_unsigned (regcache
, PPC_ORIG_R3_REGNUM
, ®val
);
86 regcache_cooked_read_unsigned (regcache
, tdep
->ppc_gp0_regnum
+ 4, ®val
);
87 *addr
= (CORE_ADDR
) regval
;
91 /* Find gdbarch for SPU context SPUFS_FD. */
92 static struct gdbarch
*
93 spu_gdbarch (int spufs_fd
)
95 struct gdbarch_info info
;
96 gdbarch_info_init (&info
);
97 info
.bfd_arch_info
= bfd_lookup_arch (bfd_arch_spu
, bfd_mach_spu
);
98 info
.byte_order
= BFD_ENDIAN_BIG
;
99 info
.osabi
= GDB_OSABI_LINUX
;
100 info
.tdep_info
= (void *) &spufs_fd
;
101 return gdbarch_find_by_info (info
);
104 /* Override the to_thread_architecture routine. */
105 static struct gdbarch
*
106 spu_thread_architecture (struct target_ops
*ops
, ptid_t ptid
)
109 CORE_ADDR spufs_addr
;
111 if (parse_spufs_run (ptid
, &spufs_fd
, &spufs_addr
))
112 return spu_gdbarch (spufs_fd
);
114 return target_gdbarch ();
117 /* Override the to_region_ok_for_hw_watchpoint routine. */
119 spu_region_ok_for_hw_watchpoint (struct target_ops
*self
,
120 CORE_ADDR addr
, int len
)
122 struct target_ops
*ops_beneath
= find_target_beneath (self
);
124 /* We cannot watch SPU local store. */
125 if (SPUADDR_SPU (addr
) != -1)
128 return ops_beneath
->to_region_ok_for_hw_watchpoint (ops_beneath
, addr
, len
);
131 /* Override the to_fetch_registers routine. */
133 spu_fetch_registers (struct target_ops
*ops
,
134 struct regcache
*regcache
, int regno
)
136 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
137 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
138 struct target_ops
*ops_beneath
= find_target_beneath (ops
);
140 CORE_ADDR spufs_addr
;
142 /* This version applies only if we're currently in spu_run. */
143 if (gdbarch_bfd_arch_info (gdbarch
)->arch
!= bfd_arch_spu
)
145 ops_beneath
->to_fetch_registers (ops_beneath
, regcache
, regno
);
149 /* We must be stopped on a spu_run system call. */
150 if (!parse_spufs_run (inferior_ptid
, &spufs_fd
, &spufs_addr
))
153 /* The ID register holds the spufs file handle. */
154 if (regno
== -1 || regno
== SPU_ID_REGNUM
)
157 store_unsigned_integer (buf
, 4, byte_order
, spufs_fd
);
158 regcache_raw_supply (regcache
, SPU_ID_REGNUM
, buf
);
161 /* The NPC register is found in PPC memory at SPUFS_ADDR. */
162 if (regno
== -1 || regno
== SPU_PC_REGNUM
)
166 if (target_read (ops_beneath
, TARGET_OBJECT_MEMORY
, NULL
,
167 buf
, spufs_addr
, sizeof buf
) == sizeof buf
)
168 regcache_raw_supply (regcache
, SPU_PC_REGNUM
, buf
);
171 /* The GPRs are found in the "regs" spufs file. */
172 if (regno
== -1 || (regno
>= 0 && regno
< SPU_NUM_GPRS
))
174 gdb_byte buf
[16 * SPU_NUM_GPRS
];
178 xsnprintf (annex
, sizeof annex
, "%d/regs", spufs_fd
);
179 if (target_read (ops_beneath
, TARGET_OBJECT_SPU
, annex
,
180 buf
, 0, sizeof buf
) == sizeof buf
)
181 for (i
= 0; i
< SPU_NUM_GPRS
; i
++)
182 regcache_raw_supply (regcache
, i
, buf
+ i
*16);
186 /* Override the to_store_registers routine. */
188 spu_store_registers (struct target_ops
*ops
,
189 struct regcache
*regcache
, int regno
)
191 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
192 struct target_ops
*ops_beneath
= find_target_beneath (ops
);
194 CORE_ADDR spufs_addr
;
196 /* This version applies only if we're currently in spu_run. */
197 if (gdbarch_bfd_arch_info (gdbarch
)->arch
!= bfd_arch_spu
)
199 ops_beneath
->to_store_registers (ops_beneath
, regcache
, regno
);
203 /* We must be stopped on a spu_run system call. */
204 if (!parse_spufs_run (inferior_ptid
, &spufs_fd
, &spufs_addr
))
207 /* The NPC register is found in PPC memory at SPUFS_ADDR. */
208 if (regno
== -1 || regno
== SPU_PC_REGNUM
)
211 regcache_raw_collect (regcache
, SPU_PC_REGNUM
, buf
);
213 target_write (ops_beneath
, TARGET_OBJECT_MEMORY
, NULL
,
214 buf
, spufs_addr
, sizeof buf
);
217 /* The GPRs are found in the "regs" spufs file. */
218 if (regno
== -1 || (regno
>= 0 && regno
< SPU_NUM_GPRS
))
220 gdb_byte buf
[16 * SPU_NUM_GPRS
];
224 for (i
= 0; i
< SPU_NUM_GPRS
; i
++)
225 regcache_raw_collect (regcache
, i
, buf
+ i
*16);
227 xsnprintf (annex
, sizeof annex
, "%d/regs", spufs_fd
);
228 target_write (ops_beneath
, TARGET_OBJECT_SPU
, annex
,
233 /* Override the to_xfer_partial routine. */
234 static enum target_xfer_status
235 spu_xfer_partial (struct target_ops
*ops
, enum target_object object
,
236 const char *annex
, gdb_byte
*readbuf
,
237 const gdb_byte
*writebuf
, ULONGEST offset
, ULONGEST len
,
238 ULONGEST
*xfered_len
)
240 struct target_ops
*ops_beneath
= find_target_beneath (ops
);
242 /* Use the "mem" spufs file to access SPU local store. */
243 if (object
== TARGET_OBJECT_MEMORY
)
245 int fd
= SPUADDR_SPU (offset
);
246 CORE_ADDR addr
= SPUADDR_ADDR (offset
);
247 char mem_annex
[32], lslr_annex
[32];
250 enum target_xfer_status ret
;
254 xsnprintf (mem_annex
, sizeof mem_annex
, "%d/mem", fd
);
255 ret
= ops_beneath
->to_xfer_partial (ops_beneath
, TARGET_OBJECT_SPU
,
256 mem_annex
, readbuf
, writebuf
,
257 addr
, len
, xfered_len
);
258 if (ret
== TARGET_XFER_OK
)
261 /* SPU local store access wraps the address around at the
262 local store limit. We emulate this here. To avoid needing
263 an extra access to retrieve the LSLR, we only do that after
264 trying the original address first, and getting end-of-file. */
265 xsnprintf (lslr_annex
, sizeof lslr_annex
, "%d/lslr", fd
);
266 memset (buf
, 0, sizeof buf
);
267 if (ops_beneath
->to_xfer_partial (ops_beneath
, TARGET_OBJECT_SPU
,
268 lslr_annex
, buf
, NULL
,
269 0, sizeof buf
, xfered_len
)
273 lslr
= strtoulst ((char *) buf
, NULL
, 16);
274 return ops_beneath
->to_xfer_partial (ops_beneath
, TARGET_OBJECT_SPU
,
275 mem_annex
, readbuf
, writebuf
,
276 addr
& lslr
, len
, xfered_len
);
280 return ops_beneath
->to_xfer_partial (ops_beneath
, object
, annex
,
281 readbuf
, writebuf
, offset
, len
, xfered_len
);
284 /* Override the to_search_memory routine. */
286 spu_search_memory (struct target_ops
* ops
,
287 CORE_ADDR start_addr
, ULONGEST search_space_len
,
288 const gdb_byte
*pattern
, ULONGEST pattern_len
,
289 CORE_ADDR
*found_addrp
)
291 struct target_ops
*ops_beneath
= find_target_beneath (ops
);
293 /* For SPU local store, always fall back to the simple method. */
294 if (SPUADDR_SPU (start_addr
) >= 0)
295 return simple_search_memory (ops
,
296 start_addr
, search_space_len
,
297 pattern
, pattern_len
, found_addrp
);
299 return ops_beneath
->to_search_memory (ops_beneath
,
300 start_addr
, search_space_len
,
301 pattern
, pattern_len
, found_addrp
);
305 /* Push and pop the SPU multi-architecture support target. */
308 spu_multiarch_activate (void)
310 /* If GDB was configured without SPU architecture support,
311 we cannot install SPU multi-architecture support either. */
312 if (spu_gdbarch (-1) == NULL
)
315 push_target (&spu_ops
);
317 /* Make sure the thread architecture is re-evaluated. */
318 registers_changed ();
322 spu_multiarch_deactivate (void)
324 unpush_target (&spu_ops
);
326 /* Make sure the thread architecture is re-evaluated. */
327 registers_changed ();
331 spu_multiarch_inferior_created (struct target_ops
*ops
, int from_tty
)
333 if (spu_standalone_p ())
334 spu_multiarch_activate ();
338 spu_multiarch_solib_loaded (struct so_list
*so
)
340 if (!spu_standalone_p ())
341 if (so
->abfd
&& bfd_get_arch (so
->abfd
) == bfd_arch_spu
)
342 if (spu_nr_solib
++ == 0)
343 spu_multiarch_activate ();
347 spu_multiarch_solib_unloaded (struct so_list
*so
)
349 if (!spu_standalone_p ())
350 if (so
->abfd
&& bfd_get_arch (so
->abfd
) == bfd_arch_spu
)
351 if (--spu_nr_solib
== 0)
352 spu_multiarch_deactivate ();
356 spu_mourn_inferior (struct target_ops
*ops
)
358 struct target_ops
*ops_beneath
= find_target_beneath (ops
);
360 ops_beneath
->to_mourn_inferior (ops_beneath
);
361 spu_multiarch_deactivate ();
365 /* Initialize the SPU multi-architecture support target. */
370 spu_ops
.to_shortname
= "spu";
371 spu_ops
.to_longname
= "SPU multi-architecture support.";
372 spu_ops
.to_doc
= "SPU multi-architecture support.";
373 spu_ops
.to_mourn_inferior
= spu_mourn_inferior
;
374 spu_ops
.to_fetch_registers
= spu_fetch_registers
;
375 spu_ops
.to_store_registers
= spu_store_registers
;
376 spu_ops
.to_xfer_partial
= spu_xfer_partial
;
377 spu_ops
.to_search_memory
= spu_search_memory
;
378 spu_ops
.to_region_ok_for_hw_watchpoint
= spu_region_ok_for_hw_watchpoint
;
379 spu_ops
.to_thread_architecture
= spu_thread_architecture
;
380 spu_ops
.to_stratum
= arch_stratum
;
381 spu_ops
.to_magic
= OPS_MAGIC
;
384 /* -Wmissing-prototypes */
385 extern initialize_file_ftype _initialize_spu_multiarch
;
388 _initialize_spu_multiarch (void)
390 /* Install ourselves on the target stack. */
392 complete_target_initialization (&spu_ops
);
394 /* Install observers to watch for SPU objects. */
395 observer_attach_inferior_created (spu_multiarch_inferior_created
);
396 observer_attach_solib_loaded (spu_multiarch_solib_loaded
);
397 observer_attach_solib_unloaded (spu_multiarch_solib_unloaded
);