clean up some target delegation cases
[deliverable/binutils-gdb.git] / gdb / spu-multiarch.c
CommitLineData
85e747d2 1/* Cell SPU GNU/Linux multi-architecture debugging support.
ecd75fc8 2 Copyright (C) 2009-2014 Free Software Foundation, Inc.
85e747d2
UW
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
dcf7800b 10 the Free Software Foundation; either version 3 of the License, or
85e747d2
UW
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
dcf7800b 19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
85e747d2
UW
20
21#include "defs.h"
22#include "gdbcore.h"
23#include "gdbcmd.h"
0e9f083f 24#include <string.h>
85e747d2
UW
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. */
40static struct target_ops spu_ops;
41
42/* Number of SPE objects loaded into the current inferior. */
43static 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. */
57static int
58parse_spufs_run (ptid_t ptid, int *fd, CORE_ADDR *addr)
59{
f5656ead 60 enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
85e747d2
UW
61 struct gdbarch_tdep *tdep;
62 struct regcache *regcache;
e362b510 63 gdb_byte buf[4];
85e747d2
UW
64 ULONGEST regval;
65
66 /* If we're not on PPU, there's nothing to detect. */
f5656ead 67 if (gdbarch_bfd_arch_info (target_gdbarch ())->arch != bfd_arch_powerpc)
85e747d2
UW
68 return 0;
69
70 /* Get PPU-side registers. */
f5656ead
TT
71 regcache = get_thread_arch_regcache (ptid, target_gdbarch ());
72 tdep = gdbarch_tdep (target_gdbarch ());
85e747d2
UW
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. */
94static struct gdbarch *
95spu_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. */
107static struct gdbarch *
108spu_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
f5656ead 116 return target_gdbarch ();
85e747d2
UW
117}
118
119/* Override the to_region_ok_for_hw_watchpoint routine. */
120static int
31568a15
TT
121spu_region_ok_for_hw_watchpoint (struct target_ops *self,
122 CORE_ADDR addr, int len)
85e747d2
UW
123{
124 struct target_ops *ops_beneath = find_target_beneath (&spu_ops);
85e747d2
UW
125
126 /* We cannot watch SPU local store. */
127 if (SPUADDR_SPU (addr) != -1)
128 return 0;
129
e75fdfca 130 return ops_beneath->to_region_ok_for_hw_watchpoint (ops_beneath, addr, len);
85e747d2
UW
131}
132
133/* Override the to_fetch_registers routine. */
134static void
135spu_fetch_registers (struct target_ops *ops,
136 struct regcache *regcache, int regno)
137{
138 struct gdbarch *gdbarch = get_regcache_arch (regcache);
139 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
140 struct target_ops *ops_beneath = find_target_beneath (ops);
141 int spufs_fd;
142 CORE_ADDR spufs_addr;
143
144 /* This version applies only if we're currently in spu_run. */
145 if (gdbarch_bfd_arch_info (gdbarch)->arch != bfd_arch_spu)
146 {
85e747d2
UW
147 ops_beneath->to_fetch_registers (ops_beneath, regcache, regno);
148 return;
149 }
150
151 /* We must be stopped on a spu_run system call. */
152 if (!parse_spufs_run (inferior_ptid, &spufs_fd, &spufs_addr))
153 return;
154
155 /* The ID register holds the spufs file handle. */
156 if (regno == -1 || regno == SPU_ID_REGNUM)
157 {
e362b510 158 gdb_byte buf[4];
85e747d2
UW
159 store_unsigned_integer (buf, 4, byte_order, spufs_fd);
160 regcache_raw_supply (regcache, SPU_ID_REGNUM, buf);
161 }
162
163 /* The NPC register is found in PPC memory at SPUFS_ADDR. */
164 if (regno == -1 || regno == SPU_PC_REGNUM)
165 {
e362b510 166 gdb_byte buf[4];
85e747d2
UW
167
168 if (target_read (ops_beneath, TARGET_OBJECT_MEMORY, NULL,
169 buf, spufs_addr, sizeof buf) == sizeof buf)
170 regcache_raw_supply (regcache, SPU_PC_REGNUM, buf);
171 }
172
173 /* The GPRs are found in the "regs" spufs file. */
174 if (regno == -1 || (regno >= 0 && regno < SPU_NUM_GPRS))
175 {
e362b510
PA
176 gdb_byte buf[16 * SPU_NUM_GPRS];
177 char annex[32];
85e747d2
UW
178 int i;
179
180 xsnprintf (annex, sizeof annex, "%d/regs", spufs_fd);
181 if (target_read (ops_beneath, TARGET_OBJECT_SPU, annex,
182 buf, 0, sizeof buf) == sizeof buf)
183 for (i = 0; i < SPU_NUM_GPRS; i++)
184 regcache_raw_supply (regcache, i, buf + i*16);
185 }
186}
187
188/* Override the to_store_registers routine. */
189static void
190spu_store_registers (struct target_ops *ops,
191 struct regcache *regcache, int regno)
192{
193 struct gdbarch *gdbarch = get_regcache_arch (regcache);
194 struct target_ops *ops_beneath = find_target_beneath (ops);
195 int spufs_fd;
196 CORE_ADDR spufs_addr;
197
198 /* This version applies only if we're currently in spu_run. */
199 if (gdbarch_bfd_arch_info (gdbarch)->arch != bfd_arch_spu)
200 {
85e747d2
UW
201 ops_beneath->to_store_registers (ops_beneath, regcache, regno);
202 return;
203 }
204
205 /* We must be stopped on a spu_run system call. */
206 if (!parse_spufs_run (inferior_ptid, &spufs_fd, &spufs_addr))
207 return;
208
209 /* The NPC register is found in PPC memory at SPUFS_ADDR. */
210 if (regno == -1 || regno == SPU_PC_REGNUM)
211 {
e362b510 212 gdb_byte buf[4];
85e747d2
UW
213 regcache_raw_collect (regcache, SPU_PC_REGNUM, buf);
214
215 target_write (ops_beneath, TARGET_OBJECT_MEMORY, NULL,
216 buf, spufs_addr, sizeof buf);
217 }
218
219 /* The GPRs are found in the "regs" spufs file. */
220 if (regno == -1 || (regno >= 0 && regno < SPU_NUM_GPRS))
221 {
e362b510
PA
222 gdb_byte buf[16 * SPU_NUM_GPRS];
223 char annex[32];
85e747d2
UW
224 int i;
225
226 for (i = 0; i < SPU_NUM_GPRS; i++)
227 regcache_raw_collect (regcache, i, buf + i*16);
228
229 xsnprintf (annex, sizeof annex, "%d/regs", spufs_fd);
230 target_write (ops_beneath, TARGET_OBJECT_SPU, annex,
231 buf, 0, sizeof buf);
232 }
233}
234
235/* Override the to_xfer_partial routine. */
9b409511 236static enum target_xfer_status
85e747d2
UW
237spu_xfer_partial (struct target_ops *ops, enum target_object object,
238 const char *annex, gdb_byte *readbuf,
9b409511
YQ
239 const gdb_byte *writebuf, ULONGEST offset, ULONGEST len,
240 ULONGEST *xfered_len)
85e747d2
UW
241{
242 struct target_ops *ops_beneath = find_target_beneath (ops);
85e747d2
UW
243
244 /* Use the "mem" spufs file to access SPU local store. */
245 if (object == TARGET_OBJECT_MEMORY)
246 {
247 int fd = SPUADDR_SPU (offset);
248 CORE_ADDR addr = SPUADDR_ADDR (offset);
d2ed6730
UW
249 char mem_annex[32], lslr_annex[32];
250 gdb_byte buf[32];
251 ULONGEST lslr;
9b409511 252 enum target_xfer_status ret;
85e747d2 253
d2ed6730 254 if (fd >= 0)
85e747d2
UW
255 {
256 xsnprintf (mem_annex, sizeof mem_annex, "%d/mem", fd);
d2ed6730
UW
257 ret = ops_beneath->to_xfer_partial (ops_beneath, TARGET_OBJECT_SPU,
258 mem_annex, readbuf, writebuf,
9b409511
YQ
259 addr, len, xfered_len);
260 if (ret == TARGET_XFER_OK)
d2ed6730
UW
261 return ret;
262
263 /* SPU local store access wraps the address around at the
264 local store limit. We emulate this here. To avoid needing
265 an extra access to retrieve the LSLR, we only do that after
266 trying the original address first, and getting end-of-file. */
267 xsnprintf (lslr_annex, sizeof lslr_annex, "%d/lslr", fd);
268 memset (buf, 0, sizeof buf);
269 if (ops_beneath->to_xfer_partial (ops_beneath, TARGET_OBJECT_SPU,
270 lslr_annex, buf, NULL,
9b409511
YQ
271 0, sizeof buf, xfered_len)
272 != TARGET_XFER_OK)
d2ed6730
UW
273 return ret;
274
001f13d8 275 lslr = strtoulst ((char *) buf, NULL, 16);
85e747d2
UW
276 return ops_beneath->to_xfer_partial (ops_beneath, TARGET_OBJECT_SPU,
277 mem_annex, readbuf, writebuf,
9b409511 278 addr & lslr, len, xfered_len);
85e747d2
UW
279 }
280 }
281
282 return ops_beneath->to_xfer_partial (ops_beneath, object, annex,
9b409511 283 readbuf, writebuf, offset, len, xfered_len);
85e747d2
UW
284}
285
286/* Override the to_search_memory routine. */
287static int
288spu_search_memory (struct target_ops* ops,
289 CORE_ADDR start_addr, ULONGEST search_space_len,
290 const gdb_byte *pattern, ULONGEST pattern_len,
291 CORE_ADDR *found_addrp)
292{
293 struct target_ops *ops_beneath = find_target_beneath (ops);
85e747d2 294
e75fdfca
TT
295 /* For SPU local store, always fall back to the simple method. */
296 if (SPUADDR_SPU (start_addr) >= 0)
85e747d2
UW
297 return simple_search_memory (ops,
298 start_addr, search_space_len,
299 pattern, pattern_len, found_addrp);
300
301 return ops_beneath->to_search_memory (ops_beneath,
302 start_addr, search_space_len,
303 pattern, pattern_len, found_addrp);
304}
305
306
307/* Push and pop the SPU multi-architecture support target. */
308
309static void
310spu_multiarch_activate (void)
311{
312 /* If GDB was configured without SPU architecture support,
313 we cannot install SPU multi-architecture support either. */
314 if (spu_gdbarch (-1) == NULL)
315 return;
316
317 push_target (&spu_ops);
318
319 /* Make sure the thread architecture is re-evaluated. */
320 registers_changed ();
321}
322
323static void
324spu_multiarch_deactivate (void)
325{
326 unpush_target (&spu_ops);
327
328 /* Make sure the thread architecture is re-evaluated. */
329 registers_changed ();
330}
331
332static void
333spu_multiarch_inferior_created (struct target_ops *ops, int from_tty)
334{
335 if (spu_standalone_p ())
336 spu_multiarch_activate ();
337}
338
339static void
340spu_multiarch_solib_loaded (struct so_list *so)
341{
342 if (!spu_standalone_p ())
343 if (so->abfd && bfd_get_arch (so->abfd) == bfd_arch_spu)
344 if (spu_nr_solib++ == 0)
345 spu_multiarch_activate ();
346}
347
348static void
349spu_multiarch_solib_unloaded (struct so_list *so)
350{
351 if (!spu_standalone_p ())
352 if (so->abfd && bfd_get_arch (so->abfd) == bfd_arch_spu)
353 if (--spu_nr_solib == 0)
354 spu_multiarch_deactivate ();
355}
356
357static void
358spu_mourn_inferior (struct target_ops *ops)
359{
360 struct target_ops *ops_beneath = find_target_beneath (ops);
85e747d2 361
85e747d2
UW
362 ops_beneath->to_mourn_inferior (ops_beneath);
363 spu_multiarch_deactivate ();
364}
365
366
367/* Initialize the SPU multi-architecture support target. */
368
369static void
370init_spu_ops (void)
371{
372 spu_ops.to_shortname = "spu";
373 spu_ops.to_longname = "SPU multi-architecture support.";
374 spu_ops.to_doc = "SPU multi-architecture support.";
375 spu_ops.to_mourn_inferior = spu_mourn_inferior;
376 spu_ops.to_fetch_registers = spu_fetch_registers;
377 spu_ops.to_store_registers = spu_store_registers;
378 spu_ops.to_xfer_partial = spu_xfer_partial;
379 spu_ops.to_search_memory = spu_search_memory;
380 spu_ops.to_region_ok_for_hw_watchpoint = spu_region_ok_for_hw_watchpoint;
381 spu_ops.to_thread_architecture = spu_thread_architecture;
382 spu_ops.to_stratum = arch_stratum;
383 spu_ops.to_magic = OPS_MAGIC;
384}
385
693be288
JK
386/* -Wmissing-prototypes */
387extern initialize_file_ftype _initialize_spu_multiarch;
388
85e747d2
UW
389void
390_initialize_spu_multiarch (void)
391{
392 /* Install ourselves on the target stack. */
393 init_spu_ops ();
12070676 394 complete_target_initialization (&spu_ops);
85e747d2
UW
395
396 /* Install observers to watch for SPU objects. */
397 observer_attach_inferior_created (spu_multiarch_inferior_created);
398 observer_attach_solib_loaded (spu_multiarch_solib_loaded);
399 observer_attach_solib_unloaded (spu_multiarch_solib_unloaded);
400}
401
This page took 0.637272 seconds and 4 git commands to generate.