]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/spu-linux-nat.c
Remove ptid_get_pid
[thirdparty/binutils-gdb.git] / gdb / spu-linux-nat.c
1 /* SPU native-dependent code for GDB, the GNU debugger.
2 Copyright (C) 2006-2018 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 "target.h"
24 #include "inferior.h"
25 #include "inf-child.h"
26 #include "inf-ptrace.h"
27 #include "regcache.h"
28 #include "symfile.h"
29 #include "gdb_wait.h"
30 #include "gdbthread.h"
31 #include "gdb_bfd.h"
32
33 #include "nat/gdb_ptrace.h"
34 #include <asm/ptrace.h>
35 #include <sys/types.h>
36
37 #include "spu-tdep.h"
38
39 /* PPU side system calls. */
40 #define INSTR_SC 0x44000002
41 #define NR_spu_run 0x0116
42
43 class spu_linux_nat_target final : public inf_ptrace_target
44 {
45 public:
46 void fetch_registers (struct regcache *regcache, int regnum) override;
47 void store_registers (struct regcache *regcache, int regnum) override;
48
49 void post_attach (int) override;
50 void post_startup_inferior (ptid_t) override;
51
52 ptid_t wait (ptid_t, struct target_waitstatus *, int options) override;
53
54 enum target_xfer_status xfer_partial (enum target_object object,
55 const char *annex,
56 gdb_byte *readbuf,
57 const gdb_byte *writebuf,
58 ULONGEST offset, ULONGEST len,
59 ULONGEST *xfered_len) override;
60
61 int can_use_hw_breakpoint (enum bptype, int, int) override;
62 };
63
64 static spu_linux_nat_target the_spu_linux_nat_target;
65
66 /* Fetch PPU register REGNO. */
67 static ULONGEST
68 fetch_ppc_register (int regno)
69 {
70 PTRACE_TYPE_RET res;
71
72 int tid = ptid_get_lwp (inferior_ptid);
73 if (tid == 0)
74 tid = inferior_ptid.pid ();
75
76 #ifndef __powerpc64__
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. */
81 {
82 gdb_byte buf[8];
83
84 errno = 0;
85 ptrace (PPC_PTRACE_PEEKUSR_3264, tid,
86 (PTRACE_TYPE_ARG3) (regno * 8), buf);
87 if (errno == 0)
88 ptrace (PPC_PTRACE_PEEKUSR_3264, tid,
89 (PTRACE_TYPE_ARG3) (regno * 8 + 4), buf + 4);
90 if (errno == 0)
91 return (ULONGEST) *(uint64_t *)buf;
92 }
93 #endif
94
95 errno = 0;
96 res = ptrace (PT_READ_U, tid,
97 (PTRACE_TYPE_ARG3) (regno * sizeof (PTRACE_TYPE_RET)), 0);
98 if (errno != 0)
99 {
100 char mess[128];
101 xsnprintf (mess, sizeof mess, "reading PPC register #%d", regno);
102 perror_with_name (_(mess));
103 }
104
105 return (ULONGEST) (unsigned long) res;
106 }
107
108 /* Fetch WORD from PPU memory at (aligned) MEMADDR in thread TID. */
109 static int
110 fetch_ppc_memory_1 (int tid, ULONGEST memaddr, PTRACE_TYPE_RET *word)
111 {
112 errno = 0;
113
114 #ifndef __powerpc64__
115 if (memaddr >> 32)
116 {
117 uint64_t addr_8 = (uint64_t) memaddr;
118 ptrace (PPC_PTRACE_PEEKTEXT_3264, tid, (PTRACE_TYPE_ARG3) &addr_8, word);
119 }
120 else
121 #endif
122 *word = ptrace (PT_READ_I, tid, (PTRACE_TYPE_ARG3) (size_t) memaddr, 0);
123
124 return errno;
125 }
126
127 /* Store WORD into PPU memory at (aligned) MEMADDR in thread TID. */
128 static int
129 store_ppc_memory_1 (int tid, ULONGEST memaddr, PTRACE_TYPE_RET word)
130 {
131 errno = 0;
132
133 #ifndef __powerpc64__
134 if (memaddr >> 32)
135 {
136 uint64_t addr_8 = (uint64_t) memaddr;
137 ptrace (PPC_PTRACE_POKEDATA_3264, tid, (PTRACE_TYPE_ARG3) &addr_8, word);
138 }
139 else
140 #endif
141 ptrace (PT_WRITE_D, tid, (PTRACE_TYPE_ARG3) (size_t) memaddr, word);
142
143 return errno;
144 }
145
146 /* Fetch LEN bytes of PPU memory at MEMADDR to MYADDR. */
147 static int
148 fetch_ppc_memory (ULONGEST memaddr, gdb_byte *myaddr, int len)
149 {
150 int i, ret;
151
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;
156
157 int tid = ptid_get_lwp (inferior_ptid);
158 if (tid == 0)
159 tid = inferior_ptid.pid ();
160
161 buffer = (PTRACE_TYPE_RET *) alloca (count * sizeof (PTRACE_TYPE_RET));
162 for (i = 0; i < count; i++, addr += sizeof (PTRACE_TYPE_RET))
163 {
164 ret = fetch_ppc_memory_1 (tid, addr, &buffer[i]);
165 if (ret)
166 return ret;
167 }
168
169 memcpy (myaddr,
170 (char *) buffer + (memaddr & (sizeof (PTRACE_TYPE_RET) - 1)),
171 len);
172
173 return 0;
174 }
175
176 /* Store LEN bytes from MYADDR to PPU memory at MEMADDR. */
177 static int
178 store_ppc_memory (ULONGEST memaddr, const gdb_byte *myaddr, int len)
179 {
180 int i, ret;
181
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;
186
187 int tid = ptid_get_lwp (inferior_ptid);
188 if (tid == 0)
189 tid = inferior_ptid.pid ();
190
191 buffer = (PTRACE_TYPE_RET *) alloca (count * sizeof (PTRACE_TYPE_RET));
192
193 if (addr != memaddr || len < (int) sizeof (PTRACE_TYPE_RET))
194 {
195 ret = fetch_ppc_memory_1 (tid, addr, &buffer[0]);
196 if (ret)
197 return ret;
198 }
199
200 if (count > 1)
201 {
202 ret = fetch_ppc_memory_1 (tid, addr + (count - 1)
203 * sizeof (PTRACE_TYPE_RET),
204 &buffer[count - 1]);
205 if (ret)
206 return ret;
207 }
208
209 memcpy ((char *) buffer + (memaddr & (sizeof (PTRACE_TYPE_RET) - 1)),
210 myaddr, len);
211
212 for (i = 0; i < count; i++, addr += sizeof (PTRACE_TYPE_RET))
213 {
214 ret = store_ppc_memory_1 (tid, addr, buffer[i]);
215 if (ret)
216 return ret;
217 }
218
219 return 0;
220 }
221
222
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. */
226 static int
227 parse_spufs_run (int *fd, ULONGEST *addr)
228 {
229 enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
230 gdb_byte buf[4];
231 ULONGEST pc = fetch_ppc_register (32); /* nip */
232
233 /* Fetch instruction preceding current NIP. */
234 if (fetch_ppc_memory (pc-4, buf, 4) != 0)
235 return 0;
236 /* It should be a "sc" instruction. */
237 if (extract_unsigned_integer (buf, 4, byte_order) != INSTR_SC)
238 return 0;
239 /* System call number should be NR_spu_run. */
240 if (fetch_ppc_register (0) != NR_spu_run)
241 return 0;
242
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);
246 return 1;
247 }
248
249
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. */
253
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)
258 {
259 char buf[128];
260 int fd = 0;
261 int ret = -1;
262 int pid = inferior_ptid.pid ();
263
264 if (!annex)
265 return TARGET_XFER_EOF;
266
267 xsnprintf (buf, sizeof buf, "/proc/%d/fd/%s", pid, annex);
268 fd = open (buf, writebuf? O_WRONLY : O_RDONLY);
269 if (fd <= 0)
270 return TARGET_XFER_E_IO;
271
272 if (offset != 0
273 && lseek (fd, (off_t) offset, SEEK_SET) != (off_t) offset)
274 {
275 close (fd);
276 return TARGET_XFER_EOF;
277 }
278
279 if (writebuf)
280 ret = write (fd, writebuf, (size_t) len);
281 else if (readbuf)
282 ret = read (fd, readbuf, (size_t) len);
283
284 close (fd);
285 if (ret < 0)
286 return TARGET_XFER_E_IO;
287 else if (ret == 0)
288 return TARGET_XFER_EOF;
289 else
290 {
291 *xfered_len = (ULONGEST) ret;
292 return TARGET_XFER_OK;
293 }
294 }
295
296
297 /* Inferior memory should contain an SPE executable image at location ADDR.
298 Allocate a BFD representing that executable. Return NULL on error. */
299
300 static void *
301 spu_bfd_iovec_open (struct bfd *nbfd, void *open_closure)
302 {
303 return open_closure;
304 }
305
306 static int
307 spu_bfd_iovec_close (struct bfd *nbfd, void *stream)
308 {
309 xfree (stream);
310
311 /* Zero means success. */
312 return 0;
313 }
314
315 static file_ptr
316 spu_bfd_iovec_pread (struct bfd *abfd, void *stream, void *buf,
317 file_ptr nbytes, file_ptr offset)
318 {
319 ULONGEST addr = *(ULONGEST *)stream;
320
321 if (fetch_ppc_memory (addr + offset, (gdb_byte *)buf, nbytes) != 0)
322 {
323 bfd_set_error (bfd_error_invalid_operation);
324 return -1;
325 }
326
327 return nbytes;
328 }
329
330 static int
331 spu_bfd_iovec_stat (struct bfd *abfd, void *stream, struct stat *sb)
332 {
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;
340 return 0;
341 }
342
343 static gdb_bfd_ref_ptr
344 spu_bfd_open (ULONGEST addr)
345 {
346 asection *spu_name;
347
348 ULONGEST *open_closure = XNEW (ULONGEST);
349 *open_closure = addr;
350
351 gdb_bfd_ref_ptr nbfd (gdb_bfd_openr_iovec ("<in-memory>", "elf32-spu",
352 spu_bfd_iovec_open, open_closure,
353 spu_bfd_iovec_pread,
354 spu_bfd_iovec_close,
355 spu_bfd_iovec_stat));
356 if (nbfd == NULL)
357 return NULL;
358
359 if (!bfd_check_format (nbfd.get (), bfd_object))
360 return NULL;
361
362 /* Retrieve SPU name note and update BFD name. */
363 spu_name = bfd_get_section_by_name (nbfd.get (), ".note.spu_name");
364 if (spu_name)
365 {
366 int sect_size = bfd_section_size (nbfd.get (), spu_name);
367 if (sect_size > 20)
368 {
369 char *buf = (char *)alloca (sect_size - 20 + 1);
370 bfd_get_section_contents (nbfd.get (), spu_name, buf, 20,
371 sect_size - 20);
372 buf[sect_size - 20] = '\0';
373
374 xfree ((char *)nbfd->filename);
375 nbfd->filename = xstrdup (buf);
376 }
377 }
378
379 return nbfd;
380 }
381
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. */
386 static void
387 spu_symbol_file_add_from_memory (int inferior_fd)
388 {
389 ULONGEST addr;
390
391 gdb_byte id[128];
392 char annex[32];
393 ULONGEST len;
394 enum target_xfer_status status;
395
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)
400 return;
401 id[len] = 0;
402 addr = strtoulst ((const char *) id, NULL, 16);
403 if (!addr)
404 return;
405
406 /* Open BFD representing SPE executable and read its symbols. */
407 gdb_bfd_ref_ptr nbfd (spu_bfd_open (addr));
408 if (nbfd != NULL)
409 {
410 symbol_file_add_from_bfd (nbfd.get (), bfd_get_filename (nbfd),
411 SYMFILE_VERBOSE | SYMFILE_MAINLINE,
412 NULL, 0, NULL);
413 }
414 }
415
416
417 /* Override the post_startup_inferior routine to continue running
418 the inferior until the first spu_run system call. */
419 void
420 spu_linux_nat_target::post_startup_inferior (ptid_t ptid)
421 {
422 int fd;
423 ULONGEST addr;
424
425 int tid = ptid_get_lwp (ptid);
426 if (tid == 0)
427 tid = ptid.pid ();
428
429 while (!parse_spufs_run (&fd, &addr))
430 {
431 ptrace (PT_SYSCALL, tid, (PTRACE_TYPE_ARG3) 0, 0);
432 waitpid (tid, NULL, __WALL | __WNOTHREAD);
433 }
434 }
435
436 /* Override the post_attach routine to try load the SPE executable
437 file image from its copy inside the target process. */
438 void
439 spu_linux_nat_target::post_attach (int pid)
440 {
441 int fd;
442 ULONGEST addr;
443
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))
448 {
449 ptrace (PT_SYSCALL, pid, (PTRACE_TYPE_ARG3) 0, 0);
450 waitpid (pid, NULL, __WALL | __WNOTHREAD);
451 }
452
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);
457 }
458
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. */
461 ptid_t
462 spu_linux_nat_target::wait (ptid_t ptid, struct target_waitstatus *ourstatus,
463 int options)
464 {
465 int save_errno;
466 int status;
467 pid_t pid;
468
469 do
470 {
471 set_sigint_trap (); /* Causes SIGINT to be passed on to the
472 attached process. */
473
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);
478
479 save_errno = errno;
480
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 ())
485 {
486 pid = -1;
487 save_errno = EINTR;
488 }
489
490 clear_sigint_trap ();
491 }
492 while (pid == -1 && save_errno == EINTR);
493
494 if (pid == -1)
495 {
496 warning (_("Child process unexpectedly missing: %s"),
497 safe_strerror (save_errno));
498
499 /* Claim it exited with unknown signal. */
500 ourstatus->kind = TARGET_WAITKIND_SIGNALLED;
501 ourstatus->value.sig = GDB_SIGNAL_UNKNOWN;
502 return inferior_ptid;
503 }
504
505 store_waitstatus (ourstatus, status);
506 return ptid_t (pid);
507 }
508
509 /* Override the fetch_inferior_register routine. */
510 void
511 spu_linux_nat_target::fetch_registers (struct regcache *regcache, int regno)
512 {
513 int fd;
514 ULONGEST addr;
515
516 /* Since we use functions that rely on inferior_ptid, we need to set and
517 restore it. */
518 scoped_restore save_ptid
519 = make_scoped_restore (&inferior_ptid, regcache->ptid ());
520
521 /* We must be stopped on a spu_run system call. */
522 if (!parse_spufs_run (&fd, &addr))
523 return;
524
525 /* The ID register holds the spufs file handle. */
526 if (regno == -1 || regno == SPU_ID_REGNUM)
527 {
528 struct gdbarch *gdbarch = regcache->arch ();
529 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
530 gdb_byte buf[4];
531 store_unsigned_integer (buf, 4, byte_order, fd);
532 regcache->raw_supply (SPU_ID_REGNUM, buf);
533 }
534
535 /* The NPC register is found at ADDR. */
536 if (regno == -1 || regno == SPU_PC_REGNUM)
537 {
538 gdb_byte buf[4];
539 if (fetch_ppc_memory (addr, buf, 4) == 0)
540 regcache->raw_supply (SPU_PC_REGNUM, buf);
541 }
542
543 /* The GPRs are found in the "regs" spufs file. */
544 if (regno == -1 || (regno >= 0 && regno < SPU_NUM_GPRS))
545 {
546 gdb_byte buf[16 * SPU_NUM_GPRS];
547 char annex[32];
548 int i;
549 ULONGEST len;
550
551 xsnprintf (annex, sizeof annex, "%d/regs", fd);
552 if ((spu_proc_xfer_spu (annex, buf, NULL, 0, sizeof buf, &len)
553 == TARGET_XFER_OK)
554 && len == sizeof buf)
555 for (i = 0; i < SPU_NUM_GPRS; i++)
556 regcache->raw_supply (i, buf + i*16);
557 }
558 }
559
560 /* Override the store_inferior_register routine. */
561 void
562 spu_linux_nat_target::store_registers (struct regcache *regcache, int regno)
563 {
564 int fd;
565 ULONGEST addr;
566
567 /* Since we use functions that rely on inferior_ptid, we need to set and
568 restore it. */
569 scoped_restore save_ptid
570 = make_scoped_restore (&inferior_ptid, regcache->ptid ());
571
572 /* We must be stopped on a spu_run system call. */
573 if (!parse_spufs_run (&fd, &addr))
574 return;
575
576 /* The NPC register is found at ADDR. */
577 if (regno == -1 || regno == SPU_PC_REGNUM)
578 {
579 gdb_byte buf[4];
580 regcache->raw_collect (SPU_PC_REGNUM, buf);
581 store_ppc_memory (addr, buf, 4);
582 }
583
584 /* The GPRs are found in the "regs" spufs file. */
585 if (regno == -1 || (regno >= 0 && regno < SPU_NUM_GPRS))
586 {
587 gdb_byte buf[16 * SPU_NUM_GPRS];
588 char annex[32];
589 int i;
590 ULONGEST len;
591
592 for (i = 0; i < SPU_NUM_GPRS; i++)
593 regcache->raw_collect (i, buf + i*16);
594
595 xsnprintf (annex, sizeof annex, "%d/regs", fd);
596 spu_proc_xfer_spu (annex, NULL, buf, 0, sizeof buf, &len);
597 }
598 }
599
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)
606 {
607 if (object == TARGET_OBJECT_SPU)
608 return spu_proc_xfer_spu (annex, readbuf, writebuf, offset, len,
609 xfered_len);
610
611 if (object == TARGET_OBJECT_MEMORY)
612 {
613 int fd;
614 ULONGEST addr;
615 char mem_annex[32], lslr_annex[32];
616 gdb_byte buf[32];
617 ULONGEST lslr;
618 enum target_xfer_status ret;
619
620 /* We must be stopped on a spu_run system call. */
621 if (!parse_spufs_run (&fd, &addr))
622 return TARGET_XFER_EOF;
623
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,
627 xfered_len);
628 if (ret == TARGET_XFER_OK)
629 return ret;
630
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)
638 != TARGET_XFER_OK)
639 return ret;
640
641 lslr = strtoulst ((const char *) buf, NULL, 16);
642 return spu_proc_xfer_spu (mem_annex, readbuf, writebuf,
643 offset & lslr, len, xfered_len);
644 }
645
646 return TARGET_XFER_E_IO;
647 }
648
649 /* Override the to_can_use_hw_breakpoint routine. */
650 int
651 spu_linux_nat_target::can_use_hw_breakpoint (enum bptype type,
652 int cnt, int othertype)
653 {
654 return 0;
655 }
656
657 /* Initialize SPU native target. */
658 void
659 _initialize_spu_nat (void)
660 {
661 add_inf_child_target (&the_spu_linux_nat_target);
662 }