Reset errno before PTRACE_PEEKUSER for MIPS DSP_CONTROL
[external/binutils.git] / gdb / mips-linux-nat.c
1 /* Native-dependent code for GNU/Linux on MIPS processors.
2
3    Copyright (C) 2001-2014 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 "inferior.h"
24 #include "mips-tdep.h"
25 #include "target.h"
26 #include "regcache.h"
27 #include "linux-nat.h"
28 #include "mips-linux-tdep.h"
29 #include "target-descriptions.h"
30
31 #include "gdb_proc_service.h"
32 #include "gregset.h"
33
34 #include <sgidefs.h>
35 #include <sys/ptrace.h>
36 #include <asm/ptrace.h>
37
38 #include "nat/mips-linux-watch.h"
39
40 #include "features/mips-linux.c"
41 #include "features/mips-dsp-linux.c"
42 #include "features/mips64-linux.c"
43 #include "features/mips64-dsp-linux.c"
44
45 #ifndef PTRACE_GET_THREAD_AREA
46 #define PTRACE_GET_THREAD_AREA 25
47 #endif
48
49 /* Assume that we have PTRACE_GETREGS et al. support.  If we do not,
50    we'll clear this and use PTRACE_PEEKUSER instead.  */
51 static int have_ptrace_regsets = 1;
52
53 /* Whether or not to print the mirrored debug registers.  */
54
55 static int maint_show_dr;
56
57 /* Saved function pointers to fetch and store a single register using
58    PTRACE_PEEKUSER and PTRACE_POKEUSER.  */
59
60 static void (*super_fetch_registers) (struct target_ops *,
61                                       struct regcache *, int);
62 static void (*super_store_registers) (struct target_ops *,
63                                       struct regcache *, int);
64
65 static void (*super_close) (struct target_ops *);
66
67 /* Map gdb internal register number to ptrace ``address''.
68    These ``addresses'' are normally defined in <asm/ptrace.h>. 
69
70    ptrace does not provide a way to read (or set) MIPS_PS_REGNUM,
71    and there's no point in reading or setting MIPS_ZERO_REGNUM.
72    We also can not set BADVADDR, CAUSE, or FCRIR via ptrace().  */
73
74 static CORE_ADDR
75 mips_linux_register_addr (struct gdbarch *gdbarch, int regno, int store)
76 {
77   CORE_ADDR regaddr;
78
79   if (regno < 0 || regno >= gdbarch_num_regs (gdbarch))
80     error (_("Bogon register number %d."), regno);
81
82   if (regno > MIPS_ZERO_REGNUM && regno < MIPS_ZERO_REGNUM + 32)
83     regaddr = regno;
84   else if ((regno >= mips_regnum (gdbarch)->fp0)
85            && (regno < mips_regnum (gdbarch)->fp0 + 32))
86     regaddr = FPR_BASE + (regno - mips_regnum (gdbarch)->fp0);
87   else if (regno == mips_regnum (gdbarch)->pc)
88     regaddr = PC;
89   else if (regno == mips_regnum (gdbarch)->cause)
90     regaddr = store? (CORE_ADDR) -1 : CAUSE;
91   else if (regno == mips_regnum (gdbarch)->badvaddr)
92     regaddr = store? (CORE_ADDR) -1 : BADVADDR;
93   else if (regno == mips_regnum (gdbarch)->lo)
94     regaddr = MMLO;
95   else if (regno == mips_regnum (gdbarch)->hi)
96     regaddr = MMHI;
97   else if (regno == mips_regnum (gdbarch)->fp_control_status)
98     regaddr = FPC_CSR;
99   else if (regno == mips_regnum (gdbarch)->fp_implementation_revision)
100     regaddr = store? (CORE_ADDR) -1 : FPC_EIR;
101   else if (mips_regnum (gdbarch)->dspacc != -1
102            && regno >= mips_regnum (gdbarch)->dspacc
103            && regno < mips_regnum (gdbarch)->dspacc + 6)
104     regaddr = DSP_BASE + (regno - mips_regnum (gdbarch)->dspacc);
105   else if (regno == mips_regnum (gdbarch)->dspctl)
106     regaddr = DSP_CONTROL;
107   else if (mips_linux_restart_reg_p (gdbarch) && regno == MIPS_RESTART_REGNUM)
108     regaddr = 0;
109   else
110     regaddr = (CORE_ADDR) -1;
111
112   return regaddr;
113 }
114
115 static CORE_ADDR
116 mips64_linux_register_addr (struct gdbarch *gdbarch, int regno, int store)
117 {
118   CORE_ADDR regaddr;
119
120   if (regno < 0 || regno >= gdbarch_num_regs (gdbarch))
121     error (_("Bogon register number %d."), regno);
122
123   if (regno > MIPS_ZERO_REGNUM && regno < MIPS_ZERO_REGNUM + 32)
124     regaddr = regno;
125   else if ((regno >= mips_regnum (gdbarch)->fp0)
126            && (regno < mips_regnum (gdbarch)->fp0 + 32))
127     regaddr = MIPS64_FPR_BASE + (regno - gdbarch_fp0_regnum (gdbarch));
128   else if (regno == mips_regnum (gdbarch)->pc)
129     regaddr = MIPS64_PC;
130   else if (regno == mips_regnum (gdbarch)->cause)
131     regaddr = store? (CORE_ADDR) -1 : MIPS64_CAUSE;
132   else if (regno == mips_regnum (gdbarch)->badvaddr)
133     regaddr = store? (CORE_ADDR) -1 : MIPS64_BADVADDR;
134   else if (regno == mips_regnum (gdbarch)->lo)
135     regaddr = MIPS64_MMLO;
136   else if (regno == mips_regnum (gdbarch)->hi)
137     regaddr = MIPS64_MMHI;
138   else if (regno == mips_regnum (gdbarch)->fp_control_status)
139     regaddr = MIPS64_FPC_CSR;
140   else if (regno == mips_regnum (gdbarch)->fp_implementation_revision)
141     regaddr = store? (CORE_ADDR) -1 : MIPS64_FPC_EIR;
142   else if (mips_regnum (gdbarch)->dspacc != -1
143            && regno >= mips_regnum (gdbarch)->dspacc
144            && regno < mips_regnum (gdbarch)->dspacc + 6)
145     regaddr = DSP_BASE + (regno - mips_regnum (gdbarch)->dspacc);
146   else if (regno == mips_regnum (gdbarch)->dspctl)
147     regaddr = DSP_CONTROL;
148   else if (mips_linux_restart_reg_p (gdbarch) && regno == MIPS_RESTART_REGNUM)
149     regaddr = 0;
150   else
151     regaddr = (CORE_ADDR) -1;
152
153   return regaddr;
154 }
155
156 /* Fetch the thread-local storage pointer for libthread_db.  */
157
158 ps_err_e
159 ps_get_thread_area (const struct ps_prochandle *ph,
160                     lwpid_t lwpid, int idx, void **base)
161 {
162   if (ptrace (PTRACE_GET_THREAD_AREA, lwpid, NULL, base) != 0)
163     return PS_ERR;
164
165   /* IDX is the bias from the thread pointer to the beginning of the
166      thread descriptor.  It has to be subtracted due to implementation
167      quirks in libthread_db.  */
168   *base = (void *) ((char *)*base - idx);
169
170   return PS_OK;
171 }
172
173 /* Wrapper functions.  These are only used by libthread_db.  */
174
175 void
176 supply_gregset (struct regcache *regcache, const gdb_gregset_t *gregsetp)
177 {
178   if (mips_isa_regsize (get_regcache_arch (regcache)) == 4)
179     mips_supply_gregset (regcache, (const mips_elf_gregset_t *) gregsetp);
180   else
181     mips64_supply_gregset (regcache, (const mips64_elf_gregset_t *) gregsetp);
182 }
183
184 void
185 fill_gregset (const struct regcache *regcache,
186               gdb_gregset_t *gregsetp, int regno)
187 {
188   if (mips_isa_regsize (get_regcache_arch (regcache)) == 4)
189     mips_fill_gregset (regcache, (mips_elf_gregset_t *) gregsetp, regno);
190   else
191     mips64_fill_gregset (regcache, (mips64_elf_gregset_t *) gregsetp, regno);
192 }
193
194 void
195 supply_fpregset (struct regcache *regcache, const gdb_fpregset_t *fpregsetp)
196 {
197   if (mips_isa_regsize (get_regcache_arch (regcache)) == 4)
198     mips_supply_fpregset (regcache, (const mips_elf_fpregset_t *) fpregsetp);
199   else
200     mips64_supply_fpregset (regcache,
201                             (const mips64_elf_fpregset_t *) fpregsetp);
202 }
203
204 void
205 fill_fpregset (const struct regcache *regcache,
206                gdb_fpregset_t *fpregsetp, int regno)
207 {
208   if (mips_isa_regsize (get_regcache_arch (regcache)) == 4)
209     mips_fill_fpregset (regcache, (mips_elf_fpregset_t *) fpregsetp, regno);
210   else
211     mips64_fill_fpregset (regcache,
212                           (mips64_elf_fpregset_t *) fpregsetp, regno);
213 }
214
215
216 /* Fetch REGNO (or all registers if REGNO == -1) from the target
217    using PTRACE_GETREGS et al.  */
218
219 static void
220 mips64_linux_regsets_fetch_registers (struct target_ops *ops,
221                                       struct regcache *regcache, int regno)
222 {
223   struct gdbarch *gdbarch = get_regcache_arch (regcache);
224   int is_fp, is_dsp;
225   int have_dsp;
226   int regi;
227   int tid;
228
229   if (regno >= mips_regnum (gdbarch)->fp0
230       && regno <= mips_regnum (gdbarch)->fp0 + 32)
231     is_fp = 1;
232   else if (regno == mips_regnum (gdbarch)->fp_control_status)
233     is_fp = 1;
234   else if (regno == mips_regnum (gdbarch)->fp_implementation_revision)
235     is_fp = 1;
236   else
237     is_fp = 0;
238
239   /* DSP registers are optional and not a part of any set.  */
240   have_dsp = mips_regnum (gdbarch)->dspctl != -1;
241   if (!have_dsp)
242     is_dsp = 0;
243   else if (regno >= mips_regnum (gdbarch)->dspacc
244       && regno < mips_regnum (gdbarch)->dspacc + 6)
245     is_dsp = 1;
246   else if (regno == mips_regnum (gdbarch)->dspctl)
247     is_dsp = 1;
248   else
249     is_dsp = 0;
250
251   tid = ptid_get_lwp (inferior_ptid);
252   if (tid == 0)
253     tid = ptid_get_pid (inferior_ptid);
254
255   if (regno == -1 || (!is_fp && !is_dsp))
256     {
257       mips64_elf_gregset_t regs;
258
259       if (ptrace (PTRACE_GETREGS, tid, 0L, (PTRACE_TYPE_ARG3) &regs) == -1)
260         {
261           if (errno == EIO)
262             {
263               have_ptrace_regsets = 0;
264               return;
265             }
266           perror_with_name (_("Couldn't get registers"));
267         }
268
269       mips64_supply_gregset (regcache,
270                              (const mips64_elf_gregset_t *) &regs);
271     }
272
273   if (regno == -1 || is_fp)
274     {
275       mips64_elf_fpregset_t fp_regs;
276
277       if (ptrace (PTRACE_GETFPREGS, tid, 0L,
278                   (PTRACE_TYPE_ARG3) &fp_regs) == -1)
279         {
280           if (errno == EIO)
281             {
282               have_ptrace_regsets = 0;
283               return;
284             }
285           perror_with_name (_("Couldn't get FP registers"));
286         }
287
288       mips64_supply_fpregset (regcache,
289                               (const mips64_elf_fpregset_t *) &fp_regs);
290     }
291
292   if (is_dsp)
293     super_fetch_registers (ops, regcache, regno);
294   else if (regno == -1 && have_dsp)
295     {
296       for (regi = mips_regnum (gdbarch)->dspacc;
297            regi < mips_regnum (gdbarch)->dspacc + 6;
298            regi++)
299         super_fetch_registers (ops, regcache, regi);
300       super_fetch_registers (ops, regcache, mips_regnum (gdbarch)->dspctl);
301     }
302 }
303
304 /* Store REGNO (or all registers if REGNO == -1) to the target
305    using PTRACE_SETREGS et al.  */
306
307 static void
308 mips64_linux_regsets_store_registers (struct target_ops *ops,
309                                       struct regcache *regcache, int regno)
310 {
311   struct gdbarch *gdbarch = get_regcache_arch (regcache);
312   int is_fp, is_dsp;
313   int have_dsp;
314   int regi;
315   int tid;
316
317   if (regno >= mips_regnum (gdbarch)->fp0
318       && regno <= mips_regnum (gdbarch)->fp0 + 32)
319     is_fp = 1;
320   else if (regno == mips_regnum (gdbarch)->fp_control_status)
321     is_fp = 1;
322   else if (regno == mips_regnum (gdbarch)->fp_implementation_revision)
323     is_fp = 1;
324   else
325     is_fp = 0;
326
327   /* DSP registers are optional and not a part of any set.  */
328   have_dsp = mips_regnum (gdbarch)->dspctl != -1;
329   if (!have_dsp)
330     is_dsp = 0;
331   else if (regno >= mips_regnum (gdbarch)->dspacc
332       && regno < mips_regnum (gdbarch)->dspacc + 6)
333     is_dsp = 1;
334   else if (regno == mips_regnum (gdbarch)->dspctl)
335     is_dsp = 1;
336   else
337     is_dsp = 0;
338
339   tid = ptid_get_lwp (inferior_ptid);
340   if (tid == 0)
341     tid = ptid_get_pid (inferior_ptid);
342
343   if (regno == -1 || (!is_fp && !is_dsp))
344     {
345       mips64_elf_gregset_t regs;
346
347       if (ptrace (PTRACE_GETREGS, tid, 0L, (PTRACE_TYPE_ARG3) &regs) == -1)
348         perror_with_name (_("Couldn't get registers"));
349
350       mips64_fill_gregset (regcache, &regs, regno);
351
352       if (ptrace (PTRACE_SETREGS, tid, 0L, (PTRACE_TYPE_ARG3) &regs) == -1)
353         perror_with_name (_("Couldn't set registers"));
354     }
355
356   if (regno == -1 || is_fp)
357     {
358       mips64_elf_fpregset_t fp_regs;
359
360       if (ptrace (PTRACE_GETFPREGS, tid, 0L,
361                   (PTRACE_TYPE_ARG3) &fp_regs) == -1)
362         perror_with_name (_("Couldn't get FP registers"));
363
364       mips64_fill_fpregset (regcache, &fp_regs, regno);
365
366       if (ptrace (PTRACE_SETFPREGS, tid, 0L,
367                   (PTRACE_TYPE_ARG3) &fp_regs) == -1)
368         perror_with_name (_("Couldn't set FP registers"));
369     }
370
371   if (is_dsp)
372     super_store_registers (ops, regcache, regno);
373   else if (regno == -1 && have_dsp)
374     {
375       for (regi = mips_regnum (gdbarch)->dspacc;
376            regi < mips_regnum (gdbarch)->dspacc + 6;
377            regi++)
378         super_store_registers (ops, regcache, regi);
379       super_store_registers (ops, regcache, mips_regnum (gdbarch)->dspctl);
380     }
381 }
382
383 /* Fetch REGNO (or all registers if REGNO == -1) from the target
384    using any working method.  */
385
386 static void
387 mips64_linux_fetch_registers (struct target_ops *ops,
388                               struct regcache *regcache, int regnum)
389 {
390   /* Unless we already know that PTRACE_GETREGS does not work, try it.  */
391   if (have_ptrace_regsets)
392     mips64_linux_regsets_fetch_registers (ops, regcache, regnum);
393
394   /* If we know, or just found out, that PTRACE_GETREGS does not work, fall
395      back to PTRACE_PEEKUSER.  */
396   if (!have_ptrace_regsets)
397     super_fetch_registers (ops, regcache, regnum);
398 }
399
400 /* Store REGNO (or all registers if REGNO == -1) to the target
401    using any working method.  */
402
403 static void
404 mips64_linux_store_registers (struct target_ops *ops,
405                               struct regcache *regcache, int regnum)
406 {
407   /* Unless we already know that PTRACE_GETREGS does not work, try it.  */
408   if (have_ptrace_regsets)
409     mips64_linux_regsets_store_registers (ops, regcache, regnum);
410
411   /* If we know, or just found out, that PTRACE_GETREGS does not work, fall
412      back to PTRACE_PEEKUSER.  */
413   if (!have_ptrace_regsets)
414     super_store_registers (ops, regcache, regnum);
415 }
416
417 /* Return the address in the core dump or inferior of register
418    REGNO.  */
419
420 static CORE_ADDR
421 mips_linux_register_u_offset (struct gdbarch *gdbarch, int regno, int store_p)
422 {
423   if (mips_abi_regsize (gdbarch) == 8)
424     return mips64_linux_register_addr (gdbarch, regno, store_p);
425   else
426     return mips_linux_register_addr (gdbarch, regno, store_p);
427 }
428
429 static const struct target_desc *
430 mips_linux_read_description (struct target_ops *ops)
431 {
432   static int have_dsp = -1;
433
434   if (have_dsp < 0)
435     {
436       int tid;
437
438       tid = ptid_get_lwp (inferior_ptid);
439       if (tid == 0)
440         tid = ptid_get_pid (inferior_ptid);
441
442       errno = 0;
443       ptrace (PTRACE_PEEKUSER, tid, DSP_CONTROL, 0);
444       switch (errno)
445         {
446         case 0:
447           have_dsp = 1;
448           break;
449         case EIO:
450           have_dsp = 0;
451           break;
452         default:
453           perror_with_name (_("Couldn't check DSP support"));
454           break;
455         }
456     }
457
458   /* Report that target registers are a size we know for sure
459      that we can get from ptrace.  */
460   if (_MIPS_SIM == _ABIO32)
461     return have_dsp ? tdesc_mips_dsp_linux : tdesc_mips_linux;
462   else
463     return have_dsp ? tdesc_mips64_dsp_linux : tdesc_mips64_linux;
464 }
465
466 /* -1 if the kernel and/or CPU do not support watch registers.
467     1 if watch_readback is valid and we can read style, num_valid
468       and the masks.
469     0 if we need to read the watch_readback.  */
470
471 static int watch_readback_valid;
472
473 /* Cached watch register read values.  */
474
475 static struct pt_watch_regs watch_readback;
476
477 static struct mips_watchpoint *current_watches;
478
479 /*  The current set of watch register values for writing the
480     registers.  */
481
482 static struct pt_watch_regs watch_mirror;
483
484 static void
485 mips_show_dr (const char *func, CORE_ADDR addr,
486               int len, enum target_hw_bp_type type)
487 {
488   int i;
489
490   puts_unfiltered (func);
491   if (addr || len)
492     printf_unfiltered (" (addr=%s, len=%d, type=%s)",
493                        paddress (target_gdbarch (), addr), len,
494                        type == hw_write ? "data-write"
495                        : (type == hw_read ? "data-read"
496                           : (type == hw_access ? "data-read/write"
497                              : (type == hw_execute ? "instruction-execute"
498                                 : "??unknown??"))));
499   puts_unfiltered (":\n");
500
501   for (i = 0; i < MAX_DEBUG_REGISTER; i++)
502     printf_unfiltered ("\tDR%d: lo=%s, hi=%s\n", i,
503                        paddress (target_gdbarch (),
504                                  mips_linux_watch_get_watchlo (&watch_mirror,
505                                                                i)),
506                        paddress (target_gdbarch (),
507                                  mips_linux_watch_get_watchhi (&watch_mirror,
508                                                                i)));
509 }
510
511 /* Target to_can_use_hw_breakpoint implementation.  Return 1 if we can
512    handle the specified watch type.  */
513
514 static int
515 mips_linux_can_use_hw_breakpoint (struct target_ops *self,
516                                   int type, int cnt, int ot)
517 {
518   int i;
519   uint32_t wanted_mask, irw_mask;
520
521   if (!mips_linux_read_watch_registers (ptid_get_lwp (inferior_ptid),
522                                         &watch_readback,
523                                         &watch_readback_valid, 0))
524     return 0;
525
526    switch (type)
527     {
528     case bp_hardware_watchpoint:
529       wanted_mask = W_MASK;
530       break;
531     case bp_read_watchpoint:
532       wanted_mask = R_MASK;
533       break;
534     case bp_access_watchpoint:
535       wanted_mask = R_MASK | W_MASK;
536       break;
537     default:
538       return 0;
539     }
540  
541   for (i = 0;
542        i < mips_linux_watch_get_num_valid (&watch_readback) && cnt;
543        i++)
544     {
545       irw_mask = mips_linux_watch_get_irw_mask (&watch_readback, i);
546       if ((irw_mask & wanted_mask) == wanted_mask)
547         cnt--;
548     }
549   return (cnt == 0) ? 1 : 0;
550 }
551
552 /* Target to_stopped_by_watchpoint implementation.  Return 1 if
553    stopped by watchpoint.  The watchhi R and W bits indicate the watch
554    register triggered.  */
555
556 static int
557 mips_linux_stopped_by_watchpoint (struct target_ops *ops)
558 {
559   int n;
560   int num_valid;
561
562   if (!mips_linux_read_watch_registers (ptid_get_lwp (inferior_ptid),
563                                         &watch_readback,
564                                         &watch_readback_valid, 1))
565     return 0;
566
567   num_valid = mips_linux_watch_get_num_valid (&watch_readback);
568
569   for (n = 0; n < MAX_DEBUG_REGISTER && n < num_valid; n++)
570     if (mips_linux_watch_get_watchhi (&watch_readback, n) & (R_MASK | W_MASK))
571       return 1;
572
573   return 0;
574 }
575
576 /* Target to_stopped_data_address implementation.  Set the address
577    where the watch triggered (if known).  Return 1 if the address was
578    known.  */
579
580 static int
581 mips_linux_stopped_data_address (struct target_ops *t, CORE_ADDR *paddr)
582 {
583   /* On mips we don't know the low order 3 bits of the data address,
584      so we must return false.  */
585   return 0;
586 }
587
588 /* Target to_region_ok_for_hw_watchpoint implementation.  Return 1 if
589    the specified region can be covered by the watch registers.  */
590
591 static int
592 mips_linux_region_ok_for_hw_watchpoint (struct target_ops *self,
593                                         CORE_ADDR addr, int len)
594 {
595   struct pt_watch_regs dummy_regs;
596   int i;
597
598   if (!mips_linux_read_watch_registers (ptid_get_lwp (inferior_ptid),
599                                         &watch_readback,
600                                         &watch_readback_valid, 0))
601     return 0;
602
603   dummy_regs = watch_readback;
604   /* Clear them out.  */
605   for (i = 0; i < mips_linux_watch_get_num_valid (&dummy_regs); i++)
606     mips_linux_watch_set_watchlo (&dummy_regs, i, 0);
607   return mips_linux_watch_try_one_watch (&dummy_regs, addr, len, 0);
608 }
609
610 /* Write the mirrored watch register values for each thread.  */
611
612 static int
613 write_watchpoint_regs (void)
614 {
615   struct lwp_info *lp;
616   int tid;
617
618   ALL_LWPS (lp)
619     {
620       tid = ptid_get_lwp (lp->ptid);
621       if (ptrace (PTRACE_SET_WATCH_REGS, tid, &watch_mirror) == -1)
622         perror_with_name (_("Couldn't write debug register"));
623     }
624   return 0;
625 }
626
627 /* linux_nat new_thread implementation.  Write the mirrored watch
628  register values for the new thread.  */
629
630 static void
631 mips_linux_new_thread (struct lwp_info *lp)
632 {
633   int tid;
634
635   if (!mips_linux_read_watch_registers (ptid_get_lwp (inferior_ptid),
636                                         &watch_readback,
637                                         &watch_readback_valid, 0))
638     return;
639
640   tid = ptid_get_lwp (lp->ptid);
641   if (ptrace (PTRACE_SET_WATCH_REGS, tid, &watch_mirror) == -1)
642     perror_with_name (_("Couldn't write debug register"));
643 }
644
645 /* Target to_insert_watchpoint implementation.  Try to insert a new
646    watch.  Return zero on success.  */
647
648 static int
649 mips_linux_insert_watchpoint (struct target_ops *self,
650                               CORE_ADDR addr, int len, int type,
651                               struct expression *cond)
652 {
653   struct pt_watch_regs regs;
654   struct mips_watchpoint *new_watch;
655   struct mips_watchpoint **pw;
656
657   int i;
658   int retval;
659
660   if (!mips_linux_read_watch_registers (ptid_get_lwp (inferior_ptid),
661                                         &watch_readback,
662                                         &watch_readback_valid, 0))
663     return -1;
664
665   if (len <= 0)
666     return -1;
667
668   regs = watch_readback;
669   /* Add the current watches.  */
670   mips_linux_watch_populate_regs (current_watches, &regs);
671
672   /* Now try to add the new watch.  */
673   if (!mips_linux_watch_try_one_watch (&regs, addr, len,
674                                        mips_linux_watch_type_to_irw (type)))
675     return -1;
676
677   /* It fit.  Stick it on the end of the list.  */
678   new_watch = (struct mips_watchpoint *)
679     xmalloc (sizeof (struct mips_watchpoint));
680   new_watch->addr = addr;
681   new_watch->len = len;
682   new_watch->type = type;
683   new_watch->next = NULL;
684
685   pw = &current_watches;
686   while (*pw != NULL)
687     pw = &(*pw)->next;
688   *pw = new_watch;
689
690   watch_mirror = regs;
691   retval = write_watchpoint_regs ();
692
693   if (maint_show_dr)
694     mips_show_dr ("insert_watchpoint", addr, len, type);
695
696   return retval;
697 }
698
699 /* Target to_remove_watchpoint implementation.  Try to remove a watch.
700    Return zero on success.  */
701
702 static int
703 mips_linux_remove_watchpoint (struct target_ops *self,
704                               CORE_ADDR addr, int len, int type,
705                               struct expression *cond)
706 {
707   int retval;
708   int deleted_one;
709
710   struct mips_watchpoint **pw;
711   struct mips_watchpoint *w;
712
713   /* Search for a known watch that matches.  Then unlink and free
714      it.  */
715   deleted_one = 0;
716   pw = &current_watches;
717   while ((w = *pw))
718     {
719       if (w->addr == addr && w->len == len && w->type == type)
720         {
721           *pw = w->next;
722           xfree (w);
723           deleted_one = 1;
724           break;
725         }
726       pw = &(w->next);
727     }
728
729   if (!deleted_one)
730     return -1;  /* We don't know about it, fail doing nothing.  */
731
732   /* At this point watch_readback is known to be valid because we
733      could not have added the watch without reading it.  */
734   gdb_assert (watch_readback_valid == 1);
735
736   watch_mirror = watch_readback;
737   mips_linux_watch_populate_regs (current_watches, &watch_mirror);
738
739   retval = write_watchpoint_regs ();
740
741   if (maint_show_dr)
742     mips_show_dr ("remove_watchpoint", addr, len, type);
743
744   return retval;
745 }
746
747 /* Target to_close implementation.  Free any watches and call the
748    super implementation.  */
749
750 static void
751 mips_linux_close (struct target_ops *self)
752 {
753   struct mips_watchpoint *w;
754   struct mips_watchpoint *nw;
755
756   /* Clean out the current_watches list.  */
757   w = current_watches;
758   while (w)
759     {
760       nw = w->next;
761       xfree (w);
762       w = nw;
763     }
764   current_watches = NULL;
765
766   if (super_close)
767     super_close (self);
768 }
769
770 void _initialize_mips_linux_nat (void);
771
772 void
773 _initialize_mips_linux_nat (void)
774 {
775   struct target_ops *t;
776
777   add_setshow_boolean_cmd ("show-debug-regs", class_maintenance,
778                            &maint_show_dr, _("\
779 Set whether to show variables that mirror the mips debug registers."), _("\
780 Show whether to show variables that mirror the mips debug registers."), _("\
781 Use \"on\" to enable, \"off\" to disable.\n\
782 If enabled, the debug registers values are shown when GDB inserts\n\
783 or removes a hardware breakpoint or watchpoint, and when the inferior\n\
784 triggers a breakpoint or watchpoint."),
785                            NULL,
786                            NULL,
787                            &maintenance_set_cmdlist,
788                            &maintenance_show_cmdlist);
789
790   t = linux_trad_target (mips_linux_register_u_offset);
791
792   super_close = t->to_close;
793   t->to_close = mips_linux_close;
794
795   super_fetch_registers = t->to_fetch_registers;
796   super_store_registers = t->to_store_registers;
797
798   t->to_fetch_registers = mips64_linux_fetch_registers;
799   t->to_store_registers = mips64_linux_store_registers;
800
801   t->to_can_use_hw_breakpoint = mips_linux_can_use_hw_breakpoint;
802   t->to_remove_watchpoint = mips_linux_remove_watchpoint;
803   t->to_insert_watchpoint = mips_linux_insert_watchpoint;
804   t->to_stopped_by_watchpoint = mips_linux_stopped_by_watchpoint;
805   t->to_stopped_data_address = mips_linux_stopped_data_address;
806   t->to_region_ok_for_hw_watchpoint = mips_linux_region_ok_for_hw_watchpoint;
807
808   t->to_read_description = mips_linux_read_description;
809
810   linux_nat_add_target (t);
811   linux_nat_set_new_thread (t, mips_linux_new_thread);
812
813   /* Initialize the standard target descriptions.  */
814   initialize_tdesc_mips_linux ();
815   initialize_tdesc_mips_dsp_linux ();
816   initialize_tdesc_mips64_linux ();
817   initialize_tdesc_mips64_dsp_linux ();
818 }