1 /* SPU native-dependent code for GDB, the GNU debugger.
2 Copyright (C) 2006-2018 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 "inf-child.h"
26 #include "inf-ptrace.h"
30 #include "gdbthread.h"
33 #include "nat/gdb_ptrace.h"
34 #include <asm/ptrace.h>
35 #include <sys/types.h>
39 /* PPU side system calls. */
40 #define INSTR_SC 0x44000002
41 #define NR_spu_run 0x0116
43 class spu_linux_nat_target final : public inf_ptrace_target
46 void fetch_registers (struct regcache *regcache, int regnum) override;
47 void store_registers (struct regcache *regcache, int regnum) override;
49 void post_attach (int) override;
50 void post_startup_inferior (ptid_t) override;
52 ptid_t wait (ptid_t, struct target_waitstatus *, int options) override;
54 enum target_xfer_status xfer_partial (enum target_object object,
57 const gdb_byte *writebuf,
58 ULONGEST offset, ULONGEST len,
59 ULONGEST *xfered_len) override;
61 int can_use_hw_breakpoint (enum bptype, int, int) override;
64 static spu_linux_nat_target the_spu_linux_nat_target;
66 /* Fetch PPU register REGNO. */
68 fetch_ppc_register (int regno)
72 int tid = inferior_ptid.lwp ();
74 tid = inferior_ptid.pid ();
77 /* If running as a 32-bit process on a 64-bit system, we attempt
78 to get the full 64-bit register content of the target process.
79 If the PPC special ptrace call fails, we're on a 32-bit system;
80 just fall through to the regular ptrace call in that case. */
85 ptrace (PPC_PTRACE_PEEKUSR_3264, tid,
86 (PTRACE_TYPE_ARG3) (regno * 8), buf);
88 ptrace (PPC_PTRACE_PEEKUSR_3264, tid,
89 (PTRACE_TYPE_ARG3) (regno * 8 + 4), buf + 4);
91 return (ULONGEST) *(uint64_t *)buf;
96 res = ptrace (PT_READ_U, tid,
97 (PTRACE_TYPE_ARG3) (regno * sizeof (PTRACE_TYPE_RET)), 0);
101 xsnprintf (mess, sizeof mess, "reading PPC register #%d", regno);
102 perror_with_name (_(mess));
105 return (ULONGEST) (unsigned long) res;
108 /* Fetch WORD from PPU memory at (aligned) MEMADDR in thread TID. */
110 fetch_ppc_memory_1 (int tid, ULONGEST memaddr, PTRACE_TYPE_RET *word)
114 #ifndef __powerpc64__
117 uint64_t addr_8 = (uint64_t) memaddr;
118 ptrace (PPC_PTRACE_PEEKTEXT_3264, tid, (PTRACE_TYPE_ARG3) &addr_8, word);
122 *word = ptrace (PT_READ_I, tid, (PTRACE_TYPE_ARG3) (size_t) memaddr, 0);
127 /* Store WORD into PPU memory at (aligned) MEMADDR in thread TID. */
129 store_ppc_memory_1 (int tid, ULONGEST memaddr, PTRACE_TYPE_RET word)
133 #ifndef __powerpc64__
136 uint64_t addr_8 = (uint64_t) memaddr;
137 ptrace (PPC_PTRACE_POKEDATA_3264, tid, (PTRACE_TYPE_ARG3) &addr_8, word);
141 ptrace (PT_WRITE_D, tid, (PTRACE_TYPE_ARG3) (size_t) memaddr, word);
146 /* Fetch LEN bytes of PPU memory at MEMADDR to MYADDR. */
148 fetch_ppc_memory (ULONGEST memaddr, gdb_byte *myaddr, int len)
152 ULONGEST addr = memaddr & -(ULONGEST) sizeof (PTRACE_TYPE_RET);
153 int count = ((((memaddr + len) - addr) + sizeof (PTRACE_TYPE_RET) - 1)
154 / sizeof (PTRACE_TYPE_RET));
155 PTRACE_TYPE_RET *buffer;
157 int tid = inferior_ptid.lwp ();
159 tid = inferior_ptid.pid ();
161 buffer = (PTRACE_TYPE_RET *) alloca (count * sizeof (PTRACE_TYPE_RET));
162 for (i = 0; i < count; i++, addr += sizeof (PTRACE_TYPE_RET))
164 ret = fetch_ppc_memory_1 (tid, addr, &buffer[i]);
170 (char *) buffer + (memaddr & (sizeof (PTRACE_TYPE_RET) - 1)),
176 /* Store LEN bytes from MYADDR to PPU memory at MEMADDR. */
178 store_ppc_memory (ULONGEST memaddr, const gdb_byte *myaddr, int len)
182 ULONGEST addr = memaddr & -(ULONGEST) sizeof (PTRACE_TYPE_RET);
183 int count = ((((memaddr + len) - addr) + sizeof (PTRACE_TYPE_RET) - 1)
184 / sizeof (PTRACE_TYPE_RET));
185 PTRACE_TYPE_RET *buffer;
187 int tid = inferior_ptid.lwp ();
189 tid = inferior_ptid.pid ();
191 buffer = (PTRACE_TYPE_RET *) alloca (count * sizeof (PTRACE_TYPE_RET));
193 if (addr != memaddr || len < (int) sizeof (PTRACE_TYPE_RET))
195 ret = fetch_ppc_memory_1 (tid, addr, &buffer[0]);
202 ret = fetch_ppc_memory_1 (tid, addr + (count - 1)
203 * sizeof (PTRACE_TYPE_RET),
209 memcpy ((char *) buffer + (memaddr & (sizeof (PTRACE_TYPE_RET) - 1)),
212 for (i = 0; i < count; i++, addr += sizeof (PTRACE_TYPE_RET))
214 ret = store_ppc_memory_1 (tid, addr, buffer[i]);
223 /* If the PPU thread is currently stopped on a spu_run system call,
224 return to FD and ADDR the file handle and NPC parameter address
225 used with the system call. Return non-zero if successful. */
227 parse_spufs_run (int *fd, ULONGEST *addr)
229 enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
231 ULONGEST pc = fetch_ppc_register (32); /* nip */
233 /* Fetch instruction preceding current NIP. */
234 if (fetch_ppc_memory (pc-4, buf, 4) != 0)
236 /* It should be a "sc" instruction. */
237 if (extract_unsigned_integer (buf, 4, byte_order) != INSTR_SC)
239 /* System call number should be NR_spu_run. */
240 if (fetch_ppc_register (0) != NR_spu_run)
243 /* Register 3 contains fd, register 4 the NPC param pointer. */
244 *fd = fetch_ppc_register (34); /* orig_gpr3 */
245 *addr = fetch_ppc_register (4);
250 /* Implement the to_xfer_partial target_ops method for TARGET_OBJECT_SPU.
251 Copy LEN bytes at OFFSET in spufs file ANNEX into/from READBUF or WRITEBUF,
252 using the /proc file system. */
254 static enum target_xfer_status
255 spu_proc_xfer_spu (const char *annex, gdb_byte *readbuf,
256 const gdb_byte *writebuf,
257 ULONGEST offset, ULONGEST len, ULONGEST *xfered_len)
262 int pid = inferior_ptid.pid ();
265 return TARGET_XFER_EOF;
267 xsnprintf (buf, sizeof buf, "/proc/%d/fd/%s", pid, annex);
268 fd = open (buf, writebuf? O_WRONLY : O_RDONLY);
270 return TARGET_XFER_E_IO;
273 && lseek (fd, (off_t) offset, SEEK_SET) != (off_t) offset)
276 return TARGET_XFER_EOF;
280 ret = write (fd, writebuf, (size_t) len);
282 ret = read (fd, readbuf, (size_t) len);
286 return TARGET_XFER_E_IO;
288 return TARGET_XFER_EOF;
291 *xfered_len = (ULONGEST) ret;
292 return TARGET_XFER_OK;
297 /* Inferior memory should contain an SPE executable image at location ADDR.
298 Allocate a BFD representing that executable. Return NULL on error. */
301 spu_bfd_iovec_open (struct bfd *nbfd, void *open_closure)
307 spu_bfd_iovec_close (struct bfd *nbfd, void *stream)
311 /* Zero means success. */
316 spu_bfd_iovec_pread (struct bfd *abfd, void *stream, void *buf,
317 file_ptr nbytes, file_ptr offset)
319 ULONGEST addr = *(ULONGEST *)stream;
321 if (fetch_ppc_memory (addr + offset, (gdb_byte *)buf, nbytes) != 0)
323 bfd_set_error (bfd_error_invalid_operation);
331 spu_bfd_iovec_stat (struct bfd *abfd, void *stream, struct stat *sb)
333 /* We don't have an easy way of finding the size of embedded spu
334 images. We could parse the in-memory ELF header and section
335 table to find the extent of the last section but that seems
336 pointless when the size is needed only for checks of other
337 parsed values in dbxread.c. */
338 memset (sb, 0, sizeof (struct stat));
339 sb->st_size = INT_MAX;
343 static gdb_bfd_ref_ptr
344 spu_bfd_open (ULONGEST addr)
348 ULONGEST *open_closure = XNEW (ULONGEST);
349 *open_closure = addr;
351 gdb_bfd_ref_ptr nbfd (gdb_bfd_openr_iovec ("<in-memory>", "elf32-spu",
352 spu_bfd_iovec_open, open_closure,
355 spu_bfd_iovec_stat));
359 if (!bfd_check_format (nbfd.get (), bfd_object))
362 /* Retrieve SPU name note and update BFD name. */
363 spu_name = bfd_get_section_by_name (nbfd.get (), ".note.spu_name");
366 int sect_size = bfd_section_size (nbfd.get (), spu_name);
369 char *buf = (char *)alloca (sect_size - 20 + 1);
370 bfd_get_section_contents (nbfd.get (), spu_name, buf, 20,
372 buf[sect_size - 20] = '\0';
374 xfree ((char *)nbfd->filename);
375 nbfd->filename = xstrdup (buf);
382 /* INFERIOR_FD is a file handle passed by the inferior to the
383 spu_run system call. Assuming the SPE context was allocated
384 by the libspe library, try to retrieve the main SPE executable
385 file from its copy within the target process. */
387 spu_symbol_file_add_from_memory (int inferior_fd)
394 enum target_xfer_status status;
396 /* Read object ID. */
397 xsnprintf (annex, sizeof annex, "%d/object-id", inferior_fd);
398 status = spu_proc_xfer_spu (annex, id, NULL, 0, sizeof id, &len);
399 if (status != TARGET_XFER_OK || len >= sizeof id)
402 addr = strtoulst ((const char *) id, NULL, 16);
406 /* Open BFD representing SPE executable and read its symbols. */
407 gdb_bfd_ref_ptr nbfd (spu_bfd_open (addr));
410 symbol_file_add_from_bfd (nbfd.get (), bfd_get_filename (nbfd),
411 SYMFILE_VERBOSE | SYMFILE_MAINLINE,
417 /* Override the post_startup_inferior routine to continue running
418 the inferior until the first spu_run system call. */
420 spu_linux_nat_target::post_startup_inferior (ptid_t ptid)
425 int tid = ptid.lwp ();
429 while (!parse_spufs_run (&fd, &addr))
431 ptrace (PT_SYSCALL, tid, (PTRACE_TYPE_ARG3) 0, 0);
432 waitpid (tid, NULL, __WALL | __WNOTHREAD);
436 /* Override the post_attach routine to try load the SPE executable
437 file image from its copy inside the target process. */
439 spu_linux_nat_target::post_attach (int pid)
444 /* Like child_post_startup_inferior, if we happened to attach to
445 the inferior while it wasn't currently in spu_run, continue
446 running it until we get back there. */
447 while (!parse_spufs_run (&fd, &addr))
449 ptrace (PT_SYSCALL, pid, (PTRACE_TYPE_ARG3) 0, 0);
450 waitpid (pid, NULL, __WALL | __WNOTHREAD);
453 /* If the user has not provided an executable file, try to extract
454 the image from inside the target process. */
455 if (!get_exec_file (0))
456 spu_symbol_file_add_from_memory (fd);
459 /* Wait for child PTID to do something. Return id of the child,
460 minus_one_ptid in case of error; store status into *OURSTATUS. */
462 spu_linux_nat_target::wait (ptid_t ptid, struct target_waitstatus *ourstatus,
471 set_sigint_trap (); /* Causes SIGINT to be passed on to the
474 pid = waitpid (ptid.pid (), &status, 0);
475 if (pid == -1 && errno == ECHILD)
476 /* Try again with __WCLONE to check cloned processes. */
477 pid = waitpid (ptid.pid (), &status, __WCLONE);
481 /* Make sure we don't report an event for the exit of the
482 original program, if we've detached from it. */
483 if (pid != -1 && !WIFSTOPPED (status)
484 && pid != inferior_ptid.pid ())
490 clear_sigint_trap ();
492 while (pid == -1 && save_errno == EINTR);
496 warning (_("Child process unexpectedly missing: %s"),
497 safe_strerror (save_errno));
499 /* Claim it exited with unknown signal. */
500 ourstatus->kind = TARGET_WAITKIND_SIGNALLED;
501 ourstatus->value.sig = GDB_SIGNAL_UNKNOWN;
502 return inferior_ptid;
505 store_waitstatus (ourstatus, status);
509 /* Override the fetch_inferior_register routine. */
511 spu_linux_nat_target::fetch_registers (struct regcache *regcache, int regno)
516 /* Since we use functions that rely on inferior_ptid, we need to set and
518 scoped_restore save_ptid
519 = make_scoped_restore (&inferior_ptid, regcache->ptid ());
521 /* We must be stopped on a spu_run system call. */
522 if (!parse_spufs_run (&fd, &addr))
525 /* The ID register holds the spufs file handle. */
526 if (regno == -1 || regno == SPU_ID_REGNUM)
528 struct gdbarch *gdbarch = regcache->arch ();
529 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
531 store_unsigned_integer (buf, 4, byte_order, fd);
532 regcache->raw_supply (SPU_ID_REGNUM, buf);
535 /* The NPC register is found at ADDR. */
536 if (regno == -1 || regno == SPU_PC_REGNUM)
539 if (fetch_ppc_memory (addr, buf, 4) == 0)
540 regcache->raw_supply (SPU_PC_REGNUM, buf);
543 /* The GPRs are found in the "regs" spufs file. */
544 if (regno == -1 || (regno >= 0 && regno < SPU_NUM_GPRS))
546 gdb_byte buf[16 * SPU_NUM_GPRS];
551 xsnprintf (annex, sizeof annex, "%d/regs", fd);
552 if ((spu_proc_xfer_spu (annex, buf, NULL, 0, sizeof buf, &len)
554 && len == sizeof buf)
555 for (i = 0; i < SPU_NUM_GPRS; i++)
556 regcache->raw_supply (i, buf + i*16);
560 /* Override the store_inferior_register routine. */
562 spu_linux_nat_target::store_registers (struct regcache *regcache, int regno)
567 /* Since we use functions that rely on inferior_ptid, we need to set and
569 scoped_restore save_ptid
570 = make_scoped_restore (&inferior_ptid, regcache->ptid ());
572 /* We must be stopped on a spu_run system call. */
573 if (!parse_spufs_run (&fd, &addr))
576 /* The NPC register is found at ADDR. */
577 if (regno == -1 || regno == SPU_PC_REGNUM)
580 regcache->raw_collect (SPU_PC_REGNUM, buf);
581 store_ppc_memory (addr, buf, 4);
584 /* The GPRs are found in the "regs" spufs file. */
585 if (regno == -1 || (regno >= 0 && regno < SPU_NUM_GPRS))
587 gdb_byte buf[16 * SPU_NUM_GPRS];
592 for (i = 0; i < SPU_NUM_GPRS; i++)
593 regcache->raw_collect (i, buf + i*16);
595 xsnprintf (annex, sizeof annex, "%d/regs", fd);
596 spu_proc_xfer_spu (annex, NULL, buf, 0, sizeof buf, &len);
600 /* Override the to_xfer_partial routine. */
601 enum target_xfer_status
602 spu_linux_nat_target::xfer_partial (enum target_object object, const char *annex,
603 gdb_byte *readbuf, const gdb_byte *writebuf,
604 ULONGEST offset, ULONGEST len,
605 ULONGEST *xfered_len)
607 if (object == TARGET_OBJECT_SPU)
608 return spu_proc_xfer_spu (annex, readbuf, writebuf, offset, len,
611 if (object == TARGET_OBJECT_MEMORY)
615 char mem_annex[32], lslr_annex[32];
618 enum target_xfer_status ret;
620 /* We must be stopped on a spu_run system call. */
621 if (!parse_spufs_run (&fd, &addr))
622 return TARGET_XFER_EOF;
624 /* Use the "mem" spufs file to access SPU local store. */
625 xsnprintf (mem_annex, sizeof mem_annex, "%d/mem", fd);
626 ret = spu_proc_xfer_spu (mem_annex, readbuf, writebuf, offset, len,
628 if (ret == TARGET_XFER_OK)
631 /* SPU local store access wraps the address around at the
632 local store limit. We emulate this here. To avoid needing
633 an extra access to retrieve the LSLR, we only do that after
634 trying the original address first, and getting end-of-file. */
635 xsnprintf (lslr_annex, sizeof lslr_annex, "%d/lslr", fd);
636 memset (buf, 0, sizeof buf);
637 if (spu_proc_xfer_spu (lslr_annex, buf, NULL, 0, sizeof buf, xfered_len)
641 lslr = strtoulst ((const char *) buf, NULL, 16);
642 return spu_proc_xfer_spu (mem_annex, readbuf, writebuf,
643 offset & lslr, len, xfered_len);
646 return TARGET_XFER_E_IO;
649 /* Override the to_can_use_hw_breakpoint routine. */
651 spu_linux_nat_target::can_use_hw_breakpoint (enum bptype type,
652 int cnt, int othertype)
657 /* Initialize SPU native target. */
659 _initialize_spu_nat (void)
661 add_inf_child_target (&the_spu_linux_nat_target);