* disasm.h (gdb_disassembly): Add GDBARCH parameter.
[deliverable/binutils-gdb.git] / gdb / mips-linux-nat.c
CommitLineData
75c9abc6 1/* Native-dependent code for GNU/Linux on MIPS processors.
a094c6fb 2
0fb0cc75 3 Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
dc60ece8 4 Free Software Foundation, Inc.
2aa830e4
DJ
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
a9762ec7 10 the Free Software Foundation; either version 3 of the License, or
2aa830e4
DJ
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
a9762ec7 19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
2aa830e4
DJ
20
21#include "defs.h"
b9412953
DD
22#include "command.h"
23#include "gdbcmd.h"
24#include "gdb_assert.h"
d37eb719 25#include "inferior.h"
6b753f60 26#include "mips-tdep.h"
10d6c8cd 27#include "target.h"
28f5035f 28#include "regcache.h"
10d6c8cd 29#include "linux-nat.h"
d37eb719 30#include "mips-linux-tdep.h"
822b6570 31#include "target-descriptions.h"
2aa830e4 32
dc60ece8 33#include "gdb_proc_service.h"
3e00823e 34#include "gregset.h"
dc60ece8 35
822b6570 36#include <sgidefs.h>
d37eb719
DJ
37#include <sys/ptrace.h>
38
81adfced
DJ
39#include "features/mips-linux.c"
40#include "features/mips64-linux.c"
41
dc60ece8
DJ
42#ifndef PTRACE_GET_THREAD_AREA
43#define PTRACE_GET_THREAD_AREA 25
44#endif
45
d37eb719
DJ
46/* Assume that we have PTRACE_GETREGS et al. support. If we do not,
47 we'll clear this and use PTRACE_PEEKUSER instead. */
48static int have_ptrace_regsets = 1;
49
b9412953
DD
50/* Whether or not to print the mirrored debug registers. */
51
52static int maint_show_dr;
53
d37eb719
DJ
54/* Saved function pointers to fetch and store a single register using
55 PTRACE_PEEKUSER and PTRACE_POKEUSER. */
56
b9412953
DD
57static void (*super_fetch_registers) (struct target_ops *,
58 struct regcache *, int);
59static void (*super_store_registers) (struct target_ops *,
60 struct regcache *, int);
61
62static void (*super_close) (int);
d37eb719 63
dda0c97e 64/* Map gdb internal register number to ptrace ``address''.
7714d83a
UW
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(). */
dda0c97e
UW
70
71static CORE_ADDR
7714d83a 72mips_linux_register_addr (struct gdbarch *gdbarch, int regno, int store)
dda0c97e 73{
7714d83a 74 CORE_ADDR regaddr;
dda0c97e 75
2eb4d78b 76 if (regno < 0 || regno >= gdbarch_num_regs (gdbarch))
dda0c97e
UW
77 error (_("Bogon register number %d."), regno);
78
7714d83a 79 if (regno > MIPS_ZERO_REGNUM && regno < MIPS_ZERO_REGNUM + 32)
dda0c97e 80 regaddr = regno;
7714d83a
UW
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)
dda0c97e 85 regaddr = PC;
7714d83a
UW
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)
dda0c97e 91 regaddr = MMLO;
7714d83a 92 else if (regno == mips_regnum (gdbarch)->hi)
dda0c97e 93 regaddr = MMHI;
7714d83a 94 else if (regno == mips_regnum (gdbarch)->fp_control_status)
dda0c97e 95 regaddr = FPC_CSR;
7714d83a
UW
96 else if (regno == mips_regnum (gdbarch)->fp_implementation_revision)
97 regaddr = store? (CORE_ADDR) -1 : FPC_EIR;
822b6570
DJ
98 else if (mips_linux_restart_reg_p (gdbarch) && regno == MIPS_RESTART_REGNUM)
99 regaddr = 0;
dda0c97e 100 else
7714d83a 101 regaddr = (CORE_ADDR) -1;
dda0c97e
UW
102
103 return regaddr;
104}
105
106static CORE_ADDR
7714d83a 107mips64_linux_register_addr (struct gdbarch *gdbarch, int regno, int store)
dda0c97e 108{
7714d83a 109 CORE_ADDR regaddr;
dda0c97e 110
2eb4d78b 111 if (regno < 0 || regno >= gdbarch_num_regs (gdbarch))
dda0c97e
UW
112 error (_("Bogon register number %d."), regno);
113
7714d83a 114 if (regno > MIPS_ZERO_REGNUM && regno < MIPS_ZERO_REGNUM + 32)
dda0c97e 115 regaddr = regno;
7714d83a
UW
116 else if ((regno >= mips_regnum (gdbarch)->fp0)
117 && (regno < mips_regnum (gdbarch)->fp0 + 32))
2eb4d78b 118 regaddr = MIPS64_FPR_BASE + (regno - gdbarch_fp0_regnum (gdbarch));
7714d83a 119 else if (regno == mips_regnum (gdbarch)->pc)
dda0c97e 120 regaddr = MIPS64_PC;
7714d83a
UW
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)
dda0c97e 126 regaddr = MIPS64_MMLO;
7714d83a 127 else if (regno == mips_regnum (gdbarch)->hi)
dda0c97e 128 regaddr = MIPS64_MMHI;
7714d83a 129 else if (regno == mips_regnum (gdbarch)->fp_control_status)
dda0c97e 130 regaddr = MIPS64_FPC_CSR;
7714d83a
UW
131 else if (regno == mips_regnum (gdbarch)->fp_implementation_revision)
132 regaddr = store? (CORE_ADDR) -1 : MIPS64_FPC_EIR;
822b6570
DJ
133 else if (mips_linux_restart_reg_p (gdbarch) && regno == MIPS_RESTART_REGNUM)
134 regaddr = 0;
dda0c97e 135 else
7714d83a 136 regaddr = (CORE_ADDR) -1;
dda0c97e
UW
137
138 return regaddr;
139}
140
dc60ece8
DJ
141/* Fetch the thread-local storage pointer for libthread_db. */
142
143ps_err_e
144ps_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
3e00823e
UW
158/* Wrapper functions. These are only used by libthread_db. */
159
160void
7f7fe91e 161supply_gregset (struct regcache *regcache, const gdb_gregset_t *gregsetp)
3e00823e 162{
2eb4d78b 163 if (mips_isa_regsize (get_regcache_arch (regcache)) == 4)
7f7fe91e 164 mips_supply_gregset (regcache, (const mips_elf_gregset_t *) gregsetp);
3e00823e 165 else
7f7fe91e 166 mips64_supply_gregset (regcache, (const mips64_elf_gregset_t *) gregsetp);
3e00823e
UW
167}
168
169void
7f7fe91e
UW
170fill_gregset (const struct regcache *regcache,
171 gdb_gregset_t *gregsetp, int regno)
3e00823e 172{
2eb4d78b 173 if (mips_isa_regsize (get_regcache_arch (regcache)) == 4)
7f7fe91e 174 mips_fill_gregset (regcache, (mips_elf_gregset_t *) gregsetp, regno);
3e00823e 175 else
7f7fe91e 176 mips64_fill_gregset (regcache, (mips64_elf_gregset_t *) gregsetp, regno);
3e00823e
UW
177}
178
179void
7f7fe91e 180supply_fpregset (struct regcache *regcache, const gdb_fpregset_t *fpregsetp)
3e00823e 181{
2eb4d78b 182 if (mips_isa_regsize (get_regcache_arch (regcache)) == 4)
7f7fe91e 183 mips_supply_fpregset (regcache, (const mips_elf_fpregset_t *) fpregsetp);
3e00823e 184 else
7f7fe91e 185 mips64_supply_fpregset (regcache, (const mips64_elf_fpregset_t *) fpregsetp);
3e00823e
UW
186}
187
188void
7f7fe91e
UW
189fill_fpregset (const struct regcache *regcache,
190 gdb_fpregset_t *fpregsetp, int regno)
3e00823e 191{
2eb4d78b 192 if (mips_isa_regsize (get_regcache_arch (regcache)) == 4)
7f7fe91e 193 mips_fill_fpregset (regcache, (mips_elf_fpregset_t *) fpregsetp, regno);
3e00823e 194 else
7f7fe91e 195 mips64_fill_fpregset (regcache, (mips64_elf_fpregset_t *) fpregsetp, regno);
3e00823e
UW
196}
197
198
d37eb719
DJ
199/* Fetch REGNO (or all registers if REGNO == -1) from the target
200 using PTRACE_GETREGS et al. */
201
202static void
56be3814 203mips64_linux_regsets_fetch_registers (struct regcache *regcache, int regno)
d37eb719 204{
2eb4d78b 205 struct gdbarch *gdbarch = get_regcache_arch (regcache);
d37eb719
DJ
206 int is_fp;
207 int tid;
208
2eb4d78b
UW
209 if (regno >= mips_regnum (gdbarch)->fp0
210 && regno <= mips_regnum (gdbarch)->fp0 + 32)
d37eb719 211 is_fp = 1;
2eb4d78b 212 else if (regno == mips_regnum (gdbarch)->fp_control_status)
d37eb719 213 is_fp = 1;
2eb4d78b 214 else if (regno == mips_regnum (gdbarch)->fp_implementation_revision)
d37eb719
DJ
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
56be3814 237 mips64_supply_gregset (regcache,
28f5035f 238 (const mips64_elf_gregset_t *) &regs);
d37eb719
DJ
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
56be3814 256 mips64_supply_fpregset (regcache,
28f5035f 257 (const mips64_elf_fpregset_t *) &fp_regs);
d37eb719
DJ
258 }
259}
260
261/* Store REGNO (or all registers if REGNO == -1) to the target
262 using PTRACE_SETREGS et al. */
263
264static void
56be3814 265mips64_linux_regsets_store_registers (const struct regcache *regcache, int regno)
d37eb719 266{
2eb4d78b 267 struct gdbarch *gdbarch = get_regcache_arch (regcache);
d37eb719
DJ
268 int is_fp;
269 int tid;
270
2eb4d78b
UW
271 if (regno >= mips_regnum (gdbarch)->fp0
272 && regno <= mips_regnum (gdbarch)->fp0 + 32)
d37eb719 273 is_fp = 1;
2eb4d78b 274 else if (regno == mips_regnum (gdbarch)->fp_control_status)
d37eb719 275 is_fp = 1;
2eb4d78b 276 else if (regno == mips_regnum (gdbarch)->fp_implementation_revision)
d37eb719
DJ
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
56be3814 292 mips64_fill_gregset (regcache, &regs, regno);
d37eb719
DJ
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
56be3814 306 mips64_fill_fpregset (regcache, &fp_regs, regno);
d37eb719
DJ
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
317static void
28439f5e
PA
318mips64_linux_fetch_registers (struct target_ops *ops,
319 struct regcache *regcache, int regnum)
d37eb719
DJ
320{
321 /* Unless we already know that PTRACE_GETREGS does not work, try it. */
322 if (have_ptrace_regsets)
56be3814 323 mips64_linux_regsets_fetch_registers (regcache, regnum);
d37eb719
DJ
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)
a0740d21 328 super_fetch_registers (ops, regcache, regnum);
d37eb719
DJ
329}
330
331/* Store REGNO (or all registers if REGNO == -1) to the target
332 using any working method. */
333
334static void
28439f5e
PA
335mips64_linux_store_registers (struct target_ops *ops,
336 struct regcache *regcache, int regnum)
d37eb719
DJ
337{
338 /* Unless we already know that PTRACE_GETREGS does not work, try it. */
339 if (have_ptrace_regsets)
56be3814 340 mips64_linux_regsets_store_registers (regcache, regnum);
d37eb719
DJ
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)
a0740d21 345 super_store_registers (ops, regcache, regnum);
d37eb719
DJ
346}
347
910122bf
UW
348/* Return the address in the core dump or inferior of register
349 REGNO. */
350
351static CORE_ADDR
7714d83a 352mips_linux_register_u_offset (struct gdbarch *gdbarch, int regno, int store_p)
910122bf 353{
7714d83a
UW
354 if (mips_abi_regsize (gdbarch) == 8)
355 return mips64_linux_register_addr (gdbarch, regno, store_p);
dda0c97e 356 else
7714d83a 357 return mips_linux_register_addr (gdbarch, regno, store_p);
910122bf
UW
358}
359
81adfced
DJ
360static const struct target_desc *
361mips_linux_read_description (struct target_ops *ops)
822b6570 362{
81adfced
DJ
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;
822b6570
DJ
369}
370
b9412953
DD
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
389enum 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
398struct 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
417struct 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
425struct 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
440static int watch_readback_valid;
441
442/* Cached watch register read values. */
443
444static 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
450struct mips_watchpoint
451{
452 CORE_ADDR addr;
453 int len;
454 int type;
455 struct mips_watchpoint *next;
456};
457
458static struct mips_watchpoint *current_watches;
459
460/* The current set of watch register values for writing the
461 registers. */
462
463static struct pt_watch_regs watch_mirror;
464
465/* Assuming usable watch registers, return the irw_mask. */
466
467static uint32_t
468get_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
484static uint32_t
485get_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
501static uint32_t
502get_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
518static CORE_ADDR
519get_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
535static void
536set_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
556static uint32_t
557get_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
573static void
574set_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
590static void
591mips_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
615static int
616mips_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
656static unsigned
657type_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
675static int
676mips_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
712static int
713mips_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
734static int
735mips_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
744static CORE_ADDR
745fill_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
759static int
760try_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
845static int
846mips_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
864static int
865write_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
883static void
884mips_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
898static void
899populate_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
925static int
926mips_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
974static int
975mips_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
1020static void
1021mips_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
10d6c8cd
DJ
1040void _initialize_mips_linux_nat (void);
1041
1042void
1043_initialize_mips_linux_nat (void)
1044{
b9412953
DD
1045 struct target_ops *t;
1046
cbe54154
PA
1047 add_setshow_boolean_cmd ("show-debug-regs", class_maintenance,
1048 &maint_show_dr, _("\
1049Set whether to show variables that mirror the mips debug registers."), _("\
1050Show whether to show variables that mirror the mips debug registers."), _("\
b9412953
DD
1051Use \"on\" to enable, \"off\" to disable.\n\
1052If enabled, the debug registers values are shown when GDB inserts\n\
1053or removes a hardware breakpoint or watchpoint, and when the inferior\n\
1054triggers a breakpoint or watchpoint."),
cbe54154
PA
1055 NULL,
1056 NULL,
1057 &maintenance_set_cmdlist,
1058 &maintenance_show_cmdlist);
b9412953
DD
1059
1060 t = linux_trad_target (mips_linux_register_u_offset);
1061
1062 super_close = t->to_close;
1063 t->to_close = mips_linux_close;
d37eb719
DJ
1064
1065 super_fetch_registers = t->to_fetch_registers;
1066 super_store_registers = t->to_store_registers;
1067
1068 t->to_fetch_registers = mips64_linux_fetch_registers;
1069 t->to_store_registers = mips64_linux_store_registers;
1070
b9412953
DD
1071 t->to_can_use_hw_breakpoint = mips_linux_can_use_hw_breakpoint;
1072 t->to_remove_watchpoint = mips_linux_remove_watchpoint;
1073 t->to_insert_watchpoint = mips_linux_insert_watchpoint;
1074 t->to_stopped_by_watchpoint = mips_linux_stopped_by_watchpoint;
1075 t->to_stopped_data_address = mips_linux_stopped_data_address;
1076 t->to_region_ok_for_hw_watchpoint = mips_linux_region_ok_for_hw_watchpoint;
1077
81adfced 1078 t->to_read_description = mips_linux_read_description;
822b6570 1079
f973ed9c 1080 linux_nat_add_target (t);
b9412953 1081 linux_nat_set_new_thread (t, mips_linux_new_thread);
81adfced
DJ
1082
1083 /* Initialize the standard target descriptions. */
1084 initialize_tdesc_mips_linux ();
1085 initialize_tdesc_mips64_linux ();
10d6c8cd 1086}
This page took 0.791362 seconds and 4 git commands to generate.