Change TUI window commands to be case-sensitive
[deliverable/binutils-gdb.git] / gdb / spu-linux-nat.c
1 /* SPU native-dependent code for GDB, the GNU debugger.
2 Copyright (C) 2006-2019 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 "gdbsupport/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 = inferior_ptid.lwp ();
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 = inferior_ptid.lwp ();
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 = inferior_ptid.lwp ();
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 (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 bfd_set_filename (nbfd.get (), xstrdup (buf));
375 }
376 }
377
378 return nbfd;
379 }
380
381 /* INFERIOR_FD is a file handle passed by the inferior to the
382 spu_run system call. Assuming the SPE context was allocated
383 by the libspe library, try to retrieve the main SPE executable
384 file from its copy within the target process. */
385 static void
386 spu_symbol_file_add_from_memory (int inferior_fd)
387 {
388 ULONGEST addr;
389
390 gdb_byte id[128];
391 char annex[32];
392 ULONGEST len;
393 enum target_xfer_status status;
394
395 /* Read object ID. */
396 xsnprintf (annex, sizeof annex, "%d/object-id", inferior_fd);
397 status = spu_proc_xfer_spu (annex, id, NULL, 0, sizeof id, &len);
398 if (status != TARGET_XFER_OK || len >= sizeof id)
399 return;
400 id[len] = 0;
401 addr = strtoulst ((const char *) id, NULL, 16);
402 if (!addr)
403 return;
404
405 /* Open BFD representing SPE executable and read its symbols. */
406 gdb_bfd_ref_ptr nbfd (spu_bfd_open (addr));
407 if (nbfd != NULL)
408 {
409 symbol_file_add_from_bfd (nbfd.get (), bfd_get_filename (nbfd),
410 SYMFILE_VERBOSE | SYMFILE_MAINLINE,
411 NULL, 0, NULL);
412 }
413 }
414
415
416 /* Override the post_startup_inferior routine to continue running
417 the inferior until the first spu_run system call. */
418 void
419 spu_linux_nat_target::post_startup_inferior (ptid_t ptid)
420 {
421 int fd;
422 ULONGEST addr;
423
424 int tid = ptid.lwp ();
425 if (tid == 0)
426 tid = ptid.pid ();
427
428 while (!parse_spufs_run (&fd, &addr))
429 {
430 ptrace (PT_SYSCALL, tid, (PTRACE_TYPE_ARG3) 0, 0);
431 waitpid (tid, NULL, __WALL | __WNOTHREAD);
432 }
433 }
434
435 /* Override the post_attach routine to try load the SPE executable
436 file image from its copy inside the target process. */
437 void
438 spu_linux_nat_target::post_attach (int pid)
439 {
440 int fd;
441 ULONGEST addr;
442
443 /* Like child_post_startup_inferior, if we happened to attach to
444 the inferior while it wasn't currently in spu_run, continue
445 running it until we get back there. */
446 while (!parse_spufs_run (&fd, &addr))
447 {
448 ptrace (PT_SYSCALL, pid, (PTRACE_TYPE_ARG3) 0, 0);
449 waitpid (pid, NULL, __WALL | __WNOTHREAD);
450 }
451
452 /* If the user has not provided an executable file, try to extract
453 the image from inside the target process. */
454 if (!get_exec_file (0))
455 spu_symbol_file_add_from_memory (fd);
456 }
457
458 /* Wait for child PTID to do something. Return id of the child,
459 minus_one_ptid in case of error; store status into *OURSTATUS. */
460 ptid_t
461 spu_linux_nat_target::wait (ptid_t ptid, struct target_waitstatus *ourstatus,
462 int options)
463 {
464 int save_errno;
465 int status;
466 pid_t pid;
467
468 do
469 {
470 set_sigint_trap (); /* Causes SIGINT to be passed on to the
471 attached process. */
472
473 pid = waitpid (ptid.pid (), &status, 0);
474 if (pid == -1 && errno == ECHILD)
475 /* Try again with __WCLONE to check cloned processes. */
476 pid = waitpid (ptid.pid (), &status, __WCLONE);
477
478 save_errno = errno;
479
480 /* Make sure we don't report an event for the exit of the
481 original program, if we've detached from it. */
482 if (pid != -1 && !WIFSTOPPED (status)
483 && pid != inferior_ptid.pid ())
484 {
485 pid = -1;
486 save_errno = EINTR;
487 }
488
489 clear_sigint_trap ();
490 }
491 while (pid == -1 && save_errno == EINTR);
492
493 if (pid == -1)
494 {
495 warning (_("Child process unexpectedly missing: %s"),
496 safe_strerror (save_errno));
497
498 /* Claim it exited with unknown signal. */
499 ourstatus->kind = TARGET_WAITKIND_SIGNALLED;
500 ourstatus->value.sig = GDB_SIGNAL_UNKNOWN;
501 return inferior_ptid;
502 }
503
504 store_waitstatus (ourstatus, status);
505 return ptid_t (pid);
506 }
507
508 /* Override the fetch_inferior_register routine. */
509 void
510 spu_linux_nat_target::fetch_registers (struct regcache *regcache, int regno)
511 {
512 int fd;
513 ULONGEST addr;
514
515 /* Since we use functions that rely on inferior_ptid, we need to set and
516 restore it. */
517 scoped_restore save_ptid
518 = make_scoped_restore (&inferior_ptid, regcache->ptid ());
519
520 /* We must be stopped on a spu_run system call. */
521 if (!parse_spufs_run (&fd, &addr))
522 return;
523
524 /* The ID register holds the spufs file handle. */
525 if (regno == -1 || regno == SPU_ID_REGNUM)
526 {
527 struct gdbarch *gdbarch = regcache->arch ();
528 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
529 gdb_byte buf[4];
530 store_unsigned_integer (buf, 4, byte_order, fd);
531 regcache->raw_supply (SPU_ID_REGNUM, buf);
532 }
533
534 /* The NPC register is found at ADDR. */
535 if (regno == -1 || regno == SPU_PC_REGNUM)
536 {
537 gdb_byte buf[4];
538 if (fetch_ppc_memory (addr, buf, 4) == 0)
539 regcache->raw_supply (SPU_PC_REGNUM, buf);
540 }
541
542 /* The GPRs are found in the "regs" spufs file. */
543 if (regno == -1 || (regno >= 0 && regno < SPU_NUM_GPRS))
544 {
545 gdb_byte buf[16 * SPU_NUM_GPRS];
546 char annex[32];
547 int i;
548 ULONGEST len;
549
550 xsnprintf (annex, sizeof annex, "%d/regs", fd);
551 if ((spu_proc_xfer_spu (annex, buf, NULL, 0, sizeof buf, &len)
552 == TARGET_XFER_OK)
553 && len == sizeof buf)
554 for (i = 0; i < SPU_NUM_GPRS; i++)
555 regcache->raw_supply (i, buf + i*16);
556 }
557 }
558
559 /* Override the store_inferior_register routine. */
560 void
561 spu_linux_nat_target::store_registers (struct regcache *regcache, int regno)
562 {
563 int fd;
564 ULONGEST addr;
565
566 /* Since we use functions that rely on inferior_ptid, we need to set and
567 restore it. */
568 scoped_restore save_ptid
569 = make_scoped_restore (&inferior_ptid, regcache->ptid ());
570
571 /* We must be stopped on a spu_run system call. */
572 if (!parse_spufs_run (&fd, &addr))
573 return;
574
575 /* The NPC register is found at ADDR. */
576 if (regno == -1 || regno == SPU_PC_REGNUM)
577 {
578 gdb_byte buf[4];
579 regcache->raw_collect (SPU_PC_REGNUM, buf);
580 store_ppc_memory (addr, buf, 4);
581 }
582
583 /* The GPRs are found in the "regs" spufs file. */
584 if (regno == -1 || (regno >= 0 && regno < SPU_NUM_GPRS))
585 {
586 gdb_byte buf[16 * SPU_NUM_GPRS];
587 char annex[32];
588 int i;
589 ULONGEST len;
590
591 for (i = 0; i < SPU_NUM_GPRS; i++)
592 regcache->raw_collect (i, buf + i*16);
593
594 xsnprintf (annex, sizeof annex, "%d/regs", fd);
595 spu_proc_xfer_spu (annex, NULL, buf, 0, sizeof buf, &len);
596 }
597 }
598
599 /* Override the to_xfer_partial routine. */
600 enum target_xfer_status
601 spu_linux_nat_target::xfer_partial (enum target_object object, const char *annex,
602 gdb_byte *readbuf, const gdb_byte *writebuf,
603 ULONGEST offset, ULONGEST len,
604 ULONGEST *xfered_len)
605 {
606 if (object == TARGET_OBJECT_SPU)
607 return spu_proc_xfer_spu (annex, readbuf, writebuf, offset, len,
608 xfered_len);
609
610 if (object == TARGET_OBJECT_MEMORY)
611 {
612 int fd;
613 ULONGEST addr;
614 char mem_annex[32], lslr_annex[32];
615 gdb_byte buf[32];
616 ULONGEST lslr;
617 enum target_xfer_status ret;
618
619 /* We must be stopped on a spu_run system call. */
620 if (!parse_spufs_run (&fd, &addr))
621 return TARGET_XFER_EOF;
622
623 /* Use the "mem" spufs file to access SPU local store. */
624 xsnprintf (mem_annex, sizeof mem_annex, "%d/mem", fd);
625 ret = spu_proc_xfer_spu (mem_annex, readbuf, writebuf, offset, len,
626 xfered_len);
627 if (ret == TARGET_XFER_OK)
628 return ret;
629
630 /* SPU local store access wraps the address around at the
631 local store limit. We emulate this here. To avoid needing
632 an extra access to retrieve the LSLR, we only do that after
633 trying the original address first, and getting end-of-file. */
634 xsnprintf (lslr_annex, sizeof lslr_annex, "%d/lslr", fd);
635 memset (buf, 0, sizeof buf);
636 if (spu_proc_xfer_spu (lslr_annex, buf, NULL, 0, sizeof buf, xfered_len)
637 != TARGET_XFER_OK)
638 return ret;
639
640 lslr = strtoulst ((const char *) buf, NULL, 16);
641 return spu_proc_xfer_spu (mem_annex, readbuf, writebuf,
642 offset & lslr, len, xfered_len);
643 }
644
645 return TARGET_XFER_E_IO;
646 }
647
648 /* Override the to_can_use_hw_breakpoint routine. */
649 int
650 spu_linux_nat_target::can_use_hw_breakpoint (enum bptype type,
651 int cnt, int othertype)
652 {
653 return 0;
654 }
655
656 /* Initialize SPU native target. */
657 void
658 _initialize_spu_nat (void)
659 {
660 add_inf_child_target (&the_spu_linux_nat_target);
661 }
This page took 0.045269 seconds and 4 git commands to generate.