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