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