ac8d96daac4a35b0d47991cdb7f456d6cdf26783
[deliverable/binutils-gdb.git] / gdb / mips-linux-nat.c
1 /* Native-dependent code for GNU/Linux on MIPS processors.
2
3 Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
4 Free Software Foundation, Inc.
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 "command.h"
23 #include "gdbcmd.h"
24 #include "gdb_assert.h"
25 #include "inferior.h"
26 #include "mips-tdep.h"
27 #include "target.h"
28 #include "regcache.h"
29 #include "linux-nat.h"
30 #include "mips-linux-tdep.h"
31 #include "target-descriptions.h"
32
33 #include "gdb_proc_service.h"
34 #include "gregset.h"
35
36 #include <sgidefs.h>
37 #include <sys/ptrace.h>
38
39 #include "features/mips-linux.c"
40 #include "features/mips64-linux.c"
41
42 #ifndef PTRACE_GET_THREAD_AREA
43 #define PTRACE_GET_THREAD_AREA 25
44 #endif
45
46 /* Assume that we have PTRACE_GETREGS et al. support. If we do not,
47 we'll clear this and use PTRACE_PEEKUSER instead. */
48 static int have_ptrace_regsets = 1;
49
50 /* Whether or not to print the mirrored debug registers. */
51
52 static int maint_show_dr;
53
54 /* Saved function pointers to fetch and store a single register using
55 PTRACE_PEEKUSER and PTRACE_POKEUSER. */
56
57 static void (*super_fetch_registers) (struct target_ops *,
58 struct regcache *, int);
59 static void (*super_store_registers) (struct target_ops *,
60 struct regcache *, int);
61
62 static void (*super_close) (int);
63
64 /* Map gdb internal register number to ptrace ``address''.
65 These ``addresses'' are normally defined in <asm/ptrace.h>.
66
67 ptrace does not provide a way to read (or set) MIPS_PS_REGNUM,
68 and there's no point in reading or setting MIPS_ZERO_REGNUM.
69 We also can not set BADVADDR, CAUSE, or FCRIR via ptrace(). */
70
71 static CORE_ADDR
72 mips_linux_register_addr (struct gdbarch *gdbarch, int regno, int store)
73 {
74 CORE_ADDR regaddr;
75
76 if (regno < 0 || regno >= gdbarch_num_regs (gdbarch))
77 error (_("Bogon register number %d."), regno);
78
79 if (regno > MIPS_ZERO_REGNUM && regno < MIPS_ZERO_REGNUM + 32)
80 regaddr = regno;
81 else if ((regno >= mips_regnum (gdbarch)->fp0)
82 && (regno < mips_regnum (gdbarch)->fp0 + 32))
83 regaddr = FPR_BASE + (regno - mips_regnum (gdbarch)->fp0);
84 else if (regno == mips_regnum (gdbarch)->pc)
85 regaddr = PC;
86 else if (regno == mips_regnum (gdbarch)->cause)
87 regaddr = store? (CORE_ADDR) -1 : CAUSE;
88 else if (regno == mips_regnum (gdbarch)->badvaddr)
89 regaddr = store? (CORE_ADDR) -1 : BADVADDR;
90 else if (regno == mips_regnum (gdbarch)->lo)
91 regaddr = MMLO;
92 else if (regno == mips_regnum (gdbarch)->hi)
93 regaddr = MMHI;
94 else if (regno == mips_regnum (gdbarch)->fp_control_status)
95 regaddr = FPC_CSR;
96 else if (regno == mips_regnum (gdbarch)->fp_implementation_revision)
97 regaddr = store? (CORE_ADDR) -1 : FPC_EIR;
98 else if (mips_linux_restart_reg_p (gdbarch) && regno == MIPS_RESTART_REGNUM)
99 regaddr = 0;
100 else
101 regaddr = (CORE_ADDR) -1;
102
103 return regaddr;
104 }
105
106 static CORE_ADDR
107 mips64_linux_register_addr (struct gdbarch *gdbarch, int regno, int store)
108 {
109 CORE_ADDR regaddr;
110
111 if (regno < 0 || regno >= gdbarch_num_regs (gdbarch))
112 error (_("Bogon register number %d."), regno);
113
114 if (regno > MIPS_ZERO_REGNUM && regno < MIPS_ZERO_REGNUM + 32)
115 regaddr = regno;
116 else if ((regno >= mips_regnum (gdbarch)->fp0)
117 && (regno < mips_regnum (gdbarch)->fp0 + 32))
118 regaddr = MIPS64_FPR_BASE + (regno - gdbarch_fp0_regnum (gdbarch));
119 else if (regno == mips_regnum (gdbarch)->pc)
120 regaddr = MIPS64_PC;
121 else if (regno == mips_regnum (gdbarch)->cause)
122 regaddr = store? (CORE_ADDR) -1 : MIPS64_CAUSE;
123 else if (regno == mips_regnum (gdbarch)->badvaddr)
124 regaddr = store? (CORE_ADDR) -1 : MIPS64_BADVADDR;
125 else if (regno == mips_regnum (gdbarch)->lo)
126 regaddr = MIPS64_MMLO;
127 else if (regno == mips_regnum (gdbarch)->hi)
128 regaddr = MIPS64_MMHI;
129 else if (regno == mips_regnum (gdbarch)->fp_control_status)
130 regaddr = MIPS64_FPC_CSR;
131 else if (regno == mips_regnum (gdbarch)->fp_implementation_revision)
132 regaddr = store? (CORE_ADDR) -1 : MIPS64_FPC_EIR;
133 else if (mips_linux_restart_reg_p (gdbarch) && regno == MIPS_RESTART_REGNUM)
134 regaddr = 0;
135 else
136 regaddr = (CORE_ADDR) -1;
137
138 return regaddr;
139 }
140
141 /* Fetch the thread-local storage pointer for libthread_db. */
142
143 ps_err_e
144 ps_get_thread_area (const struct ps_prochandle *ph,
145 lwpid_t lwpid, int idx, void **base)
146 {
147 if (ptrace (PTRACE_GET_THREAD_AREA, lwpid, NULL, base) != 0)
148 return PS_ERR;
149
150 /* IDX is the bias from the thread pointer to the beginning of the
151 thread descriptor. It has to be subtracted due to implementation
152 quirks in libthread_db. */
153 *base = (void *) ((char *)*base - idx);
154
155 return PS_OK;
156 }
157
158 /* Wrapper functions. These are only used by libthread_db. */
159
160 void
161 supply_gregset (struct regcache *regcache, const gdb_gregset_t *gregsetp)
162 {
163 if (mips_isa_regsize (get_regcache_arch (regcache)) == 4)
164 mips_supply_gregset (regcache, (const mips_elf_gregset_t *) gregsetp);
165 else
166 mips64_supply_gregset (regcache, (const mips64_elf_gregset_t *) gregsetp);
167 }
168
169 void
170 fill_gregset (const struct regcache *regcache,
171 gdb_gregset_t *gregsetp, int regno)
172 {
173 if (mips_isa_regsize (get_regcache_arch (regcache)) == 4)
174 mips_fill_gregset (regcache, (mips_elf_gregset_t *) gregsetp, regno);
175 else
176 mips64_fill_gregset (regcache, (mips64_elf_gregset_t *) gregsetp, regno);
177 }
178
179 void
180 supply_fpregset (struct regcache *regcache, const gdb_fpregset_t *fpregsetp)
181 {
182 if (mips_isa_regsize (get_regcache_arch (regcache)) == 4)
183 mips_supply_fpregset (regcache, (const mips_elf_fpregset_t *) fpregsetp);
184 else
185 mips64_supply_fpregset (regcache, (const mips64_elf_fpregset_t *) fpregsetp);
186 }
187
188 void
189 fill_fpregset (const struct regcache *regcache,
190 gdb_fpregset_t *fpregsetp, int regno)
191 {
192 if (mips_isa_regsize (get_regcache_arch (regcache)) == 4)
193 mips_fill_fpregset (regcache, (mips_elf_fpregset_t *) fpregsetp, regno);
194 else
195 mips64_fill_fpregset (regcache, (mips64_elf_fpregset_t *) fpregsetp, regno);
196 }
197
198
199 /* Fetch REGNO (or all registers if REGNO == -1) from the target
200 using PTRACE_GETREGS et al. */
201
202 static void
203 mips64_linux_regsets_fetch_registers (struct regcache *regcache, int regno)
204 {
205 struct gdbarch *gdbarch = get_regcache_arch (regcache);
206 int is_fp;
207 int tid;
208
209 if (regno >= mips_regnum (gdbarch)->fp0
210 && regno <= mips_regnum (gdbarch)->fp0 + 32)
211 is_fp = 1;
212 else if (regno == mips_regnum (gdbarch)->fp_control_status)
213 is_fp = 1;
214 else if (regno == mips_regnum (gdbarch)->fp_implementation_revision)
215 is_fp = 1;
216 else
217 is_fp = 0;
218
219 tid = ptid_get_lwp (inferior_ptid);
220 if (tid == 0)
221 tid = ptid_get_pid (inferior_ptid);
222
223 if (regno == -1 || !is_fp)
224 {
225 mips64_elf_gregset_t regs;
226
227 if (ptrace (PTRACE_GETREGS, tid, 0L, (PTRACE_TYPE_ARG3) &regs) == -1)
228 {
229 if (errno == EIO)
230 {
231 have_ptrace_regsets = 0;
232 return;
233 }
234 perror_with_name (_("Couldn't get registers"));
235 }
236
237 mips64_supply_gregset (regcache,
238 (const mips64_elf_gregset_t *) &regs);
239 }
240
241 if (regno == -1 || is_fp)
242 {
243 mips64_elf_fpregset_t fp_regs;
244
245 if (ptrace (PTRACE_GETFPREGS, tid, 0L,
246 (PTRACE_TYPE_ARG3) &fp_regs) == -1)
247 {
248 if (errno == EIO)
249 {
250 have_ptrace_regsets = 0;
251 return;
252 }
253 perror_with_name (_("Couldn't get FP registers"));
254 }
255
256 mips64_supply_fpregset (regcache,
257 (const mips64_elf_fpregset_t *) &fp_regs);
258 }
259 }
260
261 /* Store REGNO (or all registers if REGNO == -1) to the target
262 using PTRACE_SETREGS et al. */
263
264 static void
265 mips64_linux_regsets_store_registers (const struct regcache *regcache, int regno)
266 {
267 struct gdbarch *gdbarch = get_regcache_arch (regcache);
268 int is_fp;
269 int tid;
270
271 if (regno >= mips_regnum (gdbarch)->fp0
272 && regno <= mips_regnum (gdbarch)->fp0 + 32)
273 is_fp = 1;
274 else if (regno == mips_regnum (gdbarch)->fp_control_status)
275 is_fp = 1;
276 else if (regno == mips_regnum (gdbarch)->fp_implementation_revision)
277 is_fp = 1;
278 else
279 is_fp = 0;
280
281 tid = ptid_get_lwp (inferior_ptid);
282 if (tid == 0)
283 tid = ptid_get_pid (inferior_ptid);
284
285 if (regno == -1 || !is_fp)
286 {
287 mips64_elf_gregset_t regs;
288
289 if (ptrace (PTRACE_GETREGS, tid, 0L, (PTRACE_TYPE_ARG3) &regs) == -1)
290 perror_with_name (_("Couldn't get registers"));
291
292 mips64_fill_gregset (regcache, &regs, regno);
293
294 if (ptrace (PTRACE_SETREGS, tid, 0L, (PTRACE_TYPE_ARG3) &regs) == -1)
295 perror_with_name (_("Couldn't set registers"));
296 }
297
298 if (regno == -1 || is_fp)
299 {
300 mips64_elf_fpregset_t fp_regs;
301
302 if (ptrace (PTRACE_GETFPREGS, tid, 0L,
303 (PTRACE_TYPE_ARG3) &fp_regs) == -1)
304 perror_with_name (_("Couldn't get FP registers"));
305
306 mips64_fill_fpregset (regcache, &fp_regs, regno);
307
308 if (ptrace (PTRACE_SETFPREGS, tid, 0L,
309 (PTRACE_TYPE_ARG3) &fp_regs) == -1)
310 perror_with_name (_("Couldn't set FP registers"));
311 }
312 }
313
314 /* Fetch REGNO (or all registers if REGNO == -1) from the target
315 using any working method. */
316
317 static void
318 mips64_linux_fetch_registers (struct target_ops *ops,
319 struct regcache *regcache, int regnum)
320 {
321 /* Unless we already know that PTRACE_GETREGS does not work, try it. */
322 if (have_ptrace_regsets)
323 mips64_linux_regsets_fetch_registers (regcache, regnum);
324
325 /* If we know, or just found out, that PTRACE_GETREGS does not work, fall
326 back to PTRACE_PEEKUSER. */
327 if (!have_ptrace_regsets)
328 super_fetch_registers (ops, regcache, regnum);
329 }
330
331 /* Store REGNO (or all registers if REGNO == -1) to the target
332 using any working method. */
333
334 static void
335 mips64_linux_store_registers (struct target_ops *ops,
336 struct regcache *regcache, int regnum)
337 {
338 /* Unless we already know that PTRACE_GETREGS does not work, try it. */
339 if (have_ptrace_regsets)
340 mips64_linux_regsets_store_registers (regcache, regnum);
341
342 /* If we know, or just found out, that PTRACE_GETREGS does not work, fall
343 back to PTRACE_PEEKUSER. */
344 if (!have_ptrace_regsets)
345 super_store_registers (ops, regcache, regnum);
346 }
347
348 /* Return the address in the core dump or inferior of register
349 REGNO. */
350
351 static CORE_ADDR
352 mips_linux_register_u_offset (struct gdbarch *gdbarch, int regno, int store_p)
353 {
354 if (mips_abi_regsize (gdbarch) == 8)
355 return mips64_linux_register_addr (gdbarch, regno, store_p);
356 else
357 return mips_linux_register_addr (gdbarch, regno, store_p);
358 }
359
360 static const struct target_desc *
361 mips_linux_read_description (struct target_ops *ops)
362 {
363 /* Report that target registers are a size we know for sure
364 that we can get from ptrace. */
365 if (_MIPS_SIM == _ABIO32)
366 return tdesc_mips_linux;
367 else
368 return tdesc_mips64_linux;
369 }
370
371 #ifndef PTRACE_GET_WATCH_REGS
372 # define PTRACE_GET_WATCH_REGS 0xd0
373 #endif
374
375 #ifndef PTRACE_SET_WATCH_REGS
376 # define PTRACE_SET_WATCH_REGS 0xd1
377 #endif
378
379 #define W_BIT 0
380 #define R_BIT 1
381 #define I_BIT 2
382
383 #define W_MASK (1 << W_BIT)
384 #define R_MASK (1 << R_BIT)
385 #define I_MASK (1 << I_BIT)
386
387 #define IRW_MASK (I_MASK | R_MASK | W_MASK)
388
389 enum pt_watch_style {
390 pt_watch_style_mips32,
391 pt_watch_style_mips64
392 };
393
394 #define MAX_DEBUG_REGISTER 8
395
396 /* A value of zero in a watchlo indicates that it is available. */
397
398 struct mips32_watch_regs
399 {
400 uint32_t watchlo[MAX_DEBUG_REGISTER];
401 /* Lower 16 bits of watchhi. */
402 uint16_t watchhi[MAX_DEBUG_REGISTER];
403 /* Valid mask and I R W bits.
404 * bit 0 -- 1 if W bit is usable.
405 * bit 1 -- 1 if R bit is usable.
406 * bit 2 -- 1 if I bit is usable.
407 * bits 3 - 11 -- Valid watchhi mask bits.
408 */
409 uint16_t watch_masks[MAX_DEBUG_REGISTER];
410 /* The number of valid watch register pairs. */
411 uint32_t num_valid;
412 /* There is confusion across gcc versions about structure alignment,
413 so we force 8 byte alignment for these structures so they match
414 the kernel even if it was build with a different gcc version. */
415 } __attribute__ ((aligned (8)));
416
417 struct mips64_watch_regs
418 {
419 uint64_t watchlo[MAX_DEBUG_REGISTER];
420 uint16_t watchhi[MAX_DEBUG_REGISTER];
421 uint16_t watch_masks[MAX_DEBUG_REGISTER];
422 uint32_t num_valid;
423 } __attribute__ ((aligned (8)));
424
425 struct pt_watch_regs
426 {
427 enum pt_watch_style style;
428 union
429 {
430 struct mips32_watch_regs mips32;
431 struct mips64_watch_regs mips64;
432 };
433 };
434
435 /* -1 if the kernel and/or CPU do not support watch registers.
436 1 if watch_readback is valid and we can read style, num_valid
437 and the masks.
438 0 if we need to read the watch_readback. */
439
440 static int watch_readback_valid;
441
442 /* Cached watch register read values. */
443
444 static struct pt_watch_regs watch_readback;
445
446 /* We keep list of all watchpoints we should install and calculate the
447 watch register values each time the list changes. This allows for
448 easy sharing of watch registers for more than one watchpoint. */
449
450 struct mips_watchpoint
451 {
452 CORE_ADDR addr;
453 int len;
454 int type;
455 struct mips_watchpoint *next;
456 };
457
458 static struct mips_watchpoint *current_watches;
459
460 /* The current set of watch register values for writing the
461 registers. */
462
463 static struct pt_watch_regs watch_mirror;
464
465 /* Assuming usable watch registers, return the irw_mask. */
466
467 static uint32_t
468 get_irw_mask (struct pt_watch_regs *regs, int set)
469 {
470 switch (regs->style)
471 {
472 case pt_watch_style_mips32:
473 return regs->mips32.watch_masks[set] & IRW_MASK;
474 case pt_watch_style_mips64:
475 return regs->mips64.watch_masks[set] & IRW_MASK;
476 default:
477 internal_error (__FILE__, __LINE__,
478 _("Unrecognized watch register style"));
479 }
480 }
481
482 /* Assuming usable watch registers, return the reg_mask. */
483
484 static uint32_t
485 get_reg_mask (struct pt_watch_regs *regs, int set)
486 {
487 switch (regs->style)
488 {
489 case pt_watch_style_mips32:
490 return regs->mips32.watch_masks[set] & ~IRW_MASK;
491 case pt_watch_style_mips64:
492 return regs->mips64.watch_masks[set] & ~IRW_MASK;
493 default:
494 internal_error (__FILE__, __LINE__,
495 _("Unrecognized watch register style"));
496 }
497 }
498
499 /* Assuming usable watch registers, return the num_valid. */
500
501 static uint32_t
502 get_num_valid (struct pt_watch_regs *regs)
503 {
504 switch (regs->style)
505 {
506 case pt_watch_style_mips32:
507 return regs->mips32.num_valid;
508 case pt_watch_style_mips64:
509 return regs->mips64.num_valid;
510 default:
511 internal_error (__FILE__, __LINE__,
512 _("Unrecognized watch register style"));
513 }
514 }
515
516 /* Assuming usable watch registers, return the watchlo. */
517
518 static CORE_ADDR
519 get_watchlo (struct pt_watch_regs *regs, int set)
520 {
521 switch (regs->style)
522 {
523 case pt_watch_style_mips32:
524 return regs->mips32.watchlo[set];
525 case pt_watch_style_mips64:
526 return regs->mips64.watchlo[set];
527 default:
528 internal_error (__FILE__, __LINE__,
529 _("Unrecognized watch register style"));
530 }
531 }
532
533 /* Assuming usable watch registers, set a watchlo value. */
534
535 static void
536 set_watchlo (struct pt_watch_regs *regs, int set, CORE_ADDR value)
537 {
538 switch (regs->style)
539 {
540 case pt_watch_style_mips32:
541 /* The cast will never throw away bits as 64 bit addresses can
542 never be used on a 32 bit kernel. */
543 regs->mips32.watchlo[set] = (uint32_t)value;
544 break;
545 case pt_watch_style_mips64:
546 regs->mips64.watchlo[set] = value;
547 break;
548 default:
549 internal_error (__FILE__, __LINE__,
550 _("Unrecognized watch register style"));
551 }
552 }
553
554 /* Assuming usable watch registers, return the watchhi. */
555
556 static uint32_t
557 get_watchhi (struct pt_watch_regs *regs, int n)
558 {
559 switch (regs->style)
560 {
561 case pt_watch_style_mips32:
562 return regs->mips32.watchhi[n];
563 case pt_watch_style_mips64:
564 return regs->mips64.watchhi[n];
565 default:
566 internal_error (__FILE__, __LINE__,
567 _("Unrecognized watch register style"));
568 }
569 }
570
571 /* Assuming usable watch registers, set a watchhi value. */
572
573 static void
574 set_watchhi (struct pt_watch_regs *regs, int n, uint16_t value)
575 {
576 switch (regs->style)
577 {
578 case pt_watch_style_mips32:
579 regs->mips32.watchhi[n] = value;
580 break;
581 case pt_watch_style_mips64:
582 regs->mips64.watchhi[n] = value;
583 break;
584 default:
585 internal_error (__FILE__, __LINE__,
586 _("Unrecognized watch register style"));
587 }
588 }
589
590 static void
591 mips_show_dr (const char *func, CORE_ADDR addr,
592 int len, enum target_hw_bp_type type)
593 {
594 int i;
595
596 puts_unfiltered (func);
597 if (addr || len)
598 printf_unfiltered (" (addr=0x%s, len=%d, type=%s)", paddr (addr), len,
599 type == hw_write ? "data-write"
600 : (type == hw_read ? "data-read"
601 : (type == hw_access ? "data-read/write"
602 : (type == hw_execute ? "instruction-execute"
603 : "??unknown??"))));
604 puts_unfiltered (":\n");
605
606 for (i = 0; i < MAX_DEBUG_REGISTER; i++)
607 printf_unfiltered ("\tDR%d: lo=0x%s, hi=0x%s\n",
608 i, paddr (get_watchlo (&watch_mirror, i)),
609 paddr (get_watchhi (&watch_mirror, i)));
610 }
611
612 /* Return 1 if watch registers are usable. Cached information is used
613 unless force is true. */
614
615 static int
616 mips_linux_read_watch_registers (int force)
617 {
618 int tid;
619
620 if (force || watch_readback_valid == 0)
621 {
622 tid = ptid_get_lwp (inferior_ptid);
623 if (ptrace (PTRACE_GET_WATCH_REGS, tid, &watch_readback) == -1)
624 {
625 watch_readback_valid = -1;
626 return 0;
627 }
628 switch (watch_readback.style)
629 {
630 case pt_watch_style_mips32:
631 if (watch_readback.mips32.num_valid == 0)
632 {
633 watch_readback_valid = -1;
634 return 0;
635 }
636 break;
637 case pt_watch_style_mips64:
638 if (watch_readback.mips64.num_valid == 0)
639 {
640 watch_readback_valid = -1;
641 return 0;
642 }
643 break;
644 default:
645 watch_readback_valid = -1;
646 return 0;
647 }
648 /* Watch registers appear to be usable. */
649 watch_readback_valid = 1;
650 }
651 return (watch_readback_valid == 1) ? 1 : 0;
652 }
653
654 /* Convert GDB's type to an IRW mask. */
655
656 static unsigned
657 type_to_irw (int type)
658 {
659 switch (type)
660 {
661 case hw_write:
662 return W_MASK;
663 case hw_read:
664 return R_MASK;
665 case hw_access:
666 return (W_MASK | R_MASK);
667 default:
668 return 0;
669 }
670 }
671
672 /* Target to_can_use_hw_breakpoint implementation. Return 1 if we can
673 handle the specified watch type. */
674
675 static int
676 mips_linux_can_use_hw_breakpoint (int type, int cnt, int ot)
677 {
678 int i;
679 uint32_t wanted_mask, irw_mask;
680
681 if (!mips_linux_read_watch_registers (0))
682 return 0;
683
684 switch (type)
685 {
686 case bp_hardware_watchpoint:
687 wanted_mask = W_MASK;
688 break;
689 case bp_read_watchpoint:
690 wanted_mask = R_MASK;
691 break;
692 case bp_access_watchpoint:
693 wanted_mask = R_MASK | W_MASK;
694 break;
695 default:
696 return 0;
697 }
698
699 for (i = 0; i < get_num_valid (&watch_readback) && cnt; i++)
700 {
701 irw_mask = get_irw_mask (&watch_readback, i);
702 if ((irw_mask & wanted_mask) == wanted_mask)
703 cnt--;
704 }
705 return (cnt == 0) ? 1 : 0;
706 }
707
708 /* Target to_stopped_by_watchpoint implementation. Return 1 if
709 stopped by watchpoint. The watchhi R and W bits indicate the watch
710 register triggered. */
711
712 static int
713 mips_linux_stopped_by_watchpoint (void)
714 {
715 int n;
716 int num_valid;
717
718 if (!mips_linux_read_watch_registers (1))
719 return 0;
720
721 num_valid = get_num_valid (&watch_readback);
722
723 for (n = 0; n < MAX_DEBUG_REGISTER && n < num_valid; n++)
724 if (get_watchhi (&watch_readback, n) & (R_MASK | W_MASK))
725 return 1;
726
727 return 0;
728 }
729
730 /* Target to_stopped_data_address implementation. Set the address
731 where the watch triggered (if known). Return 1 if the address was
732 known. */
733
734 static int
735 mips_linux_stopped_data_address (struct target_ops *t, CORE_ADDR *paddr)
736 {
737 /* On mips we don't know the low order 3 bits of the data address,
738 so we must return false. */
739 return 0;
740 }
741
742 /* Set any low order bits in mask that are not set. */
743
744 static CORE_ADDR
745 fill_mask (CORE_ADDR mask)
746 {
747 CORE_ADDR f = 1;
748 while (f && f < mask)
749 {
750 mask |= f;
751 f <<= 1;
752 }
753 return mask;
754 }
755
756 /* Try to add a single watch to the specified registers. Return 1 on
757 success, 0 on failure. */
758
759 static int
760 try_one_watch (struct pt_watch_regs *regs, CORE_ADDR addr,
761 int len, unsigned irw)
762 {
763 CORE_ADDR base_addr, last_byte, break_addr, segment_len;
764 CORE_ADDR mask_bits, t_low, t_low_end;
765 uint16_t t_hi;
766 int i, free_watches;
767 struct pt_watch_regs regs_copy;
768
769 if (len <= 0)
770 return 0;
771
772 last_byte = addr + len - 1;
773 mask_bits = fill_mask (addr ^ last_byte) | IRW_MASK;
774 base_addr = addr & ~mask_bits;
775
776 /* Check to see if it is covered by current registers. */
777 for (i = 0; i < get_num_valid (regs); i++)
778 {
779 t_low = get_watchlo (regs, i);
780 if (t_low != 0 && irw == ((unsigned)t_low & irw))
781 {
782 t_hi = get_watchhi (regs, i) | IRW_MASK;
783 t_low &= ~(CORE_ADDR)t_hi;
784 if (addr >= t_low && last_byte <= (t_low + t_hi))
785 return 1;
786 }
787 }
788 /* Try to find an empty register. */
789 free_watches = 0;
790 for (i = 0; i < get_num_valid (regs); i++)
791 {
792 t_low = get_watchlo (regs, i);
793 if (t_low == 0 && irw == (get_irw_mask (regs, i) & irw))
794 {
795 if (mask_bits <= (get_reg_mask (regs, i) | IRW_MASK))
796 {
797 /* It fits, we'll take it. */
798 set_watchlo (regs, i, base_addr | irw);
799 set_watchhi (regs, i, mask_bits & ~IRW_MASK);
800 return 1;
801 }
802 else
803 {
804 /* It doesn't fit, but has the proper IRW capabilities. */
805 free_watches++;
806 }
807 }
808 }
809 if (free_watches > 1)
810 {
811 /* Try to split it across several registers. */
812 regs_copy = *regs;
813 for (i = 0; i < get_num_valid (&regs_copy); i++)
814 {
815 t_low = get_watchlo (&regs_copy, i);
816 t_hi = get_reg_mask (&regs_copy, i) | IRW_MASK;
817 if (t_low == 0 && irw == (t_hi & irw))
818 {
819 t_low = addr & ~(CORE_ADDR)t_hi;
820 break_addr = t_low + t_hi + 1;
821 if (break_addr >= addr + len)
822 segment_len = len;
823 else
824 segment_len = break_addr - addr;
825 mask_bits = fill_mask (addr ^ (addr + segment_len - 1));
826 set_watchlo (&regs_copy, i, (addr & ~mask_bits) | irw);
827 set_watchhi (&regs_copy, i, mask_bits & ~IRW_MASK);
828 if (break_addr >= addr + len)
829 {
830 *regs = regs_copy;
831 return 1;
832 }
833 len = addr + len - break_addr;
834 addr = break_addr;
835 }
836 }
837 }
838 /* It didn't fit anywhere, we failed. */
839 return 0;
840 }
841
842 /* Target to_region_ok_for_hw_watchpoint implementation. Return 1 if
843 the specified region can be covered by the watch registers. */
844
845 static int
846 mips_linux_region_ok_for_hw_watchpoint (CORE_ADDR addr, int len)
847 {
848 struct pt_watch_regs dummy_regs;
849 int i;
850
851 if (!mips_linux_read_watch_registers (0))
852 return 0;
853
854 dummy_regs = watch_readback;
855 /* Clear them out. */
856 for (i = 0; i < get_num_valid (&dummy_regs); i++)
857 set_watchlo (&dummy_regs, i, 0);
858 return try_one_watch (&dummy_regs, addr, len, 0);
859 }
860
861
862 /* Write the mirrored watch register values for each thread. */
863
864 static int
865 write_watchpoint_regs (void)
866 {
867 struct lwp_info *lp;
868 ptid_t ptid;
869 int tid;
870
871 ALL_LWPS (lp, ptid)
872 {
873 tid = ptid_get_lwp (ptid);
874 if (ptrace (PTRACE_SET_WATCH_REGS, tid, &watch_mirror) == -1)
875 perror_with_name (_("Couldn't write debug register"));
876 }
877 return 0;
878 }
879
880 /* linux_nat new_thread implementation. Write the mirrored watch
881 register values for the new thread. */
882
883 static void
884 mips_linux_new_thread (ptid_t ptid)
885 {
886 int tid;
887
888 if (!mips_linux_read_watch_registers (0))
889 return;
890
891 tid = ptid_get_lwp (ptid);
892 if (ptrace (PTRACE_SET_WATCH_REGS, tid, &watch_mirror) == -1)
893 perror_with_name (_("Couldn't write debug register"));
894 }
895
896 /* Fill in the watch registers with the currently cached watches. */
897
898 static void
899 populate_regs_from_watches (struct pt_watch_regs *regs)
900 {
901 struct mips_watchpoint *w;
902 int i;
903
904 /* Clear them out. */
905 for (i = 0; i < get_num_valid (regs); i++)
906 {
907 set_watchlo (regs, i, 0);
908 set_watchhi (regs, i, 0);
909 }
910
911 w = current_watches;
912 while (w)
913 {
914 i = try_one_watch (regs, w->addr, w->len, type_to_irw (w->type));
915 /* They must all fit, because we previously calculated that they
916 would. */
917 gdb_assert (i);
918 w = w->next;
919 }
920 }
921
922 /* Target to_insert_watchpoint implementation. Try to insert a new
923 watch. Return zero on success. */
924
925 static int
926 mips_linux_insert_watchpoint (CORE_ADDR addr, int len, int type)
927 {
928 struct pt_watch_regs regs;
929 struct mips_watchpoint *new_watch;
930 struct mips_watchpoint **pw;
931
932 int i;
933 int retval;
934
935 if (!mips_linux_read_watch_registers (0))
936 return -1;
937
938 if (len <= 0)
939 return -1;
940
941 regs = watch_readback;
942 /* Add the current watches. */
943 populate_regs_from_watches (&regs);
944
945 /* Now try to add the new watch. */
946 if (!try_one_watch (&regs, addr, len, type_to_irw (type)))
947 return -1;
948
949 /* It fit. Stick it on the end of the list. */
950 new_watch = (struct mips_watchpoint *)
951 xmalloc (sizeof (struct mips_watchpoint));
952 new_watch->addr = addr;
953 new_watch->len = len;
954 new_watch->type = type;
955 new_watch->next = NULL;
956
957 pw = &current_watches;
958 while (*pw != NULL)
959 pw = &(*pw)->next;
960 *pw = new_watch;
961
962 watch_mirror = regs;
963 retval = write_watchpoint_regs ();
964
965 if (maint_show_dr)
966 mips_show_dr ("insert_watchpoint", addr, len, type);
967
968 return retval;
969 }
970
971 /* Target to_remove_watchpoint implementation. Try to remove a watch.
972 Return zero on success. */
973
974 static int
975 mips_linux_remove_watchpoint (CORE_ADDR addr, int len, int type)
976 {
977 int retval;
978 int deleted_one;
979
980 struct mips_watchpoint **pw;
981 struct mips_watchpoint *w;
982
983 /* Search for a known watch that matches. Then unlink and free
984 it. */
985 deleted_one = 0;
986 pw = &current_watches;
987 while ((w = *pw))
988 {
989 if (w->addr == addr && w->len == len && w->type == type)
990 {
991 *pw = w->next;
992 xfree (w);
993 deleted_one = 1;
994 break;
995 }
996 pw = &(w->next);
997 }
998
999 if (!deleted_one)
1000 return -1; /* We don't know about it, fail doing nothing. */
1001
1002 /* At this point watch_readback is known to be valid because we
1003 could not have added the watch without reading it. */
1004 gdb_assert (watch_readback_valid == 1);
1005
1006 watch_mirror = watch_readback;
1007 populate_regs_from_watches (&watch_mirror);
1008
1009 retval = write_watchpoint_regs ();
1010
1011 if (maint_show_dr)
1012 mips_show_dr ("remove_watchpoint", addr, len, type);
1013
1014 return retval;
1015 }
1016
1017 /* Target to_close implementation. Free any watches and call the
1018 super implementation. */
1019
1020 static void
1021 mips_linux_close (int quitting)
1022 {
1023 struct mips_watchpoint *w;
1024 struct mips_watchpoint *nw;
1025
1026 /* Clean out the current_watches list. */
1027 w = current_watches;
1028 while (w)
1029 {
1030 nw = w->next;
1031 xfree (w);
1032 w = nw;
1033 }
1034 current_watches = NULL;
1035
1036 if (super_close)
1037 super_close (quitting);
1038 }
1039
1040 void _initialize_mips_linux_nat (void);
1041
1042 void
1043 _initialize_mips_linux_nat (void)
1044 {
1045 struct target_ops *t;
1046
1047 deprecated_add_set_cmd ("show-debug-regs", class_maintenance,
1048 var_boolean, (char *) &maint_show_dr, _("\
1049 Set whether to show variables that mirror the mips debug registers.\n\
1050 Use \"on\" to enable, \"off\" to disable.\n\
1051 If enabled, the debug registers values are shown when GDB inserts\n\
1052 or removes a hardware breakpoint or watchpoint, and when the inferior\n\
1053 triggers a breakpoint or watchpoint."),
1054 &maintenancelist);
1055
1056
1057 t = linux_trad_target (mips_linux_register_u_offset);
1058
1059 super_close = t->to_close;
1060 t->to_close = mips_linux_close;
1061
1062 super_fetch_registers = t->to_fetch_registers;
1063 super_store_registers = t->to_store_registers;
1064
1065 t->to_fetch_registers = mips64_linux_fetch_registers;
1066 t->to_store_registers = mips64_linux_store_registers;
1067
1068 t->to_can_use_hw_breakpoint = mips_linux_can_use_hw_breakpoint;
1069 t->to_remove_watchpoint = mips_linux_remove_watchpoint;
1070 t->to_insert_watchpoint = mips_linux_insert_watchpoint;
1071 t->to_stopped_by_watchpoint = mips_linux_stopped_by_watchpoint;
1072 t->to_stopped_data_address = mips_linux_stopped_data_address;
1073 t->to_region_ok_for_hw_watchpoint = mips_linux_region_ok_for_hw_watchpoint;
1074
1075 t->to_read_description = mips_linux_read_description;
1076
1077 linux_nat_add_target (t);
1078 linux_nat_set_new_thread (t, mips_linux_new_thread);
1079
1080 /* Initialize the standard target descriptions. */
1081 initialize_tdesc_mips_linux ();
1082 initialize_tdesc_mips64_linux ();
1083 }
This page took 0.08581 seconds and 4 git commands to generate.