1 /* GNU/Linux/MIPS specific low level interface, for the remote server for GDB.
2 Copyright (C) 1995-2013 Free Software Foundation, Inc.
4 This file is part of GDB.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20 #include "linux-low.h"
22 #include <sys/ptrace.h>
25 #include "mips-linux-watch.h"
26 #include "gdb_proc_service.h"
28 /* Defined in auto-generated file mips-linux.c. */
29 void init_registers_mips_linux (void);
30 extern const struct target_desc *tdesc_mips_linux;
32 /* Defined in auto-generated file mips-dsp-linux.c. */
33 void init_registers_mips_dsp_linux (void);
34 extern const struct target_desc *tdesc_mips_dsp_linux;
36 /* Defined in auto-generated file mips64-linux.c. */
37 void init_registers_mips64_linux (void);
38 extern const struct target_desc *tdesc_mips64_linux;
40 /* Defined in auto-generated file mips64-dsp-linux.c. */
41 void init_registers_mips64_dsp_linux (void);
42 extern const struct target_desc *tdesc_mips64_dsp_linux;
45 #define tdesc_mips_linux tdesc_mips64_linux
46 #define tdesc_mips_dsp_linux tdesc_mips64_dsp_linux
49 #ifndef PTRACE_GET_THREAD_AREA
50 #define PTRACE_GET_THREAD_AREA 25
57 #define mips_num_regs 73
58 #define mips_dsp_num_regs 80
60 #include <asm/ptrace.h>
64 #define DSP_CONTROL 77
71 /* Deliberately signed, for proper sign extension. */
76 /* Return the ptrace ``address'' of register REGNO. */
78 #define mips_base_regs \
79 -1, 1, 2, 3, 4, 5, 6, 7, \
80 8, 9, 10, 11, 12, 13, 14, 15, \
81 16, 17, 18, 19, 20, 21, 22, 23, \
82 24, 25, 26, 27, 28, 29, 30, 31, \
84 -1, MMLO, MMHI, BADVADDR, CAUSE, PC, \
86 FPR_BASE, FPR_BASE + 1, FPR_BASE + 2, FPR_BASE + 3, \
87 FPR_BASE + 4, FPR_BASE + 5, FPR_BASE + 6, FPR_BASE + 7, \
88 FPR_BASE + 8, FPR_BASE + 9, FPR_BASE + 10, FPR_BASE + 11, \
89 FPR_BASE + 12, FPR_BASE + 13, FPR_BASE + 14, FPR_BASE + 15, \
90 FPR_BASE + 16, FPR_BASE + 17, FPR_BASE + 18, FPR_BASE + 19, \
91 FPR_BASE + 20, FPR_BASE + 21, FPR_BASE + 22, FPR_BASE + 23, \
92 FPR_BASE + 24, FPR_BASE + 25, FPR_BASE + 26, FPR_BASE + 27, \
93 FPR_BASE + 28, FPR_BASE + 29, FPR_BASE + 30, FPR_BASE + 31, \
96 #define mips_dsp_regs \
97 DSP_BASE, DSP_BASE + 1, DSP_BASE + 2, DSP_BASE + 3, \
98 DSP_BASE + 4, DSP_BASE + 5, \
101 static int mips_regmap[mips_num_regs] = {
106 static int mips_dsp_regmap[mips_dsp_num_regs] = {
112 /* DSP registers are not in any regset and can only be accessed
115 static unsigned char mips_dsp_regset_bitmap[(mips_dsp_num_regs + 7) / 8] = {
116 0xfe, 0xff, 0xff, 0xff, 0xfe, 0xff, 0xff, 0xff, 0xff, 0x80
119 static int have_dsp = -1;
121 /* Try peeking at an arbitrarily chosen DSP register and pick the available
122 user register set accordingly. */
124 static const struct target_desc *
125 mips_read_description (void)
129 int pid = lwpid_of (get_thread_lwp (current_inferior));
131 ptrace (PTRACE_PEEKUSER, pid, DSP_CONTROL, 0);
141 perror_with_name ("ptrace");
146 return have_dsp ? tdesc_mips_dsp_linux : tdesc_mips_linux;
150 mips_arch_setup (void)
152 current_process ()->tdesc = mips_read_description ();
155 static struct usrregs_info *
156 get_usrregs_info (void)
158 const struct regs_info *regs_info = the_low_target.regs_info ();
160 return regs_info->usrregs;
163 /* Per-process arch-specific data we want to keep. */
165 struct arch_process_info
167 /* -1 if the kernel and/or CPU do not support watch registers.
168 1 if watch_readback is valid and we can read style, num_valid
170 0 if we need to read the watch_readback. */
172 int watch_readback_valid;
174 /* Cached watch register read values. */
176 struct pt_watch_regs watch_readback;
178 /* Current watchpoint requests for this process. */
180 struct mips_watchpoint *current_watches;
182 /* The current set of watch register values for writing the
185 struct pt_watch_regs watch_mirror;
188 /* Per-thread arch-specific data we want to keep. */
192 /* Non-zero if our copy differs from what's recorded in the thread. */
193 int watch_registers_changed;
196 /* From mips-linux-nat.c. */
198 /* Pseudo registers can not be read. ptrace does not provide a way to
199 read (or set) PS_REGNUM, and there's no point in reading or setting
200 ZERO_REGNUM. We also can not set BADVADDR, CAUSE, or FCRIR via
204 mips_cannot_fetch_register (int regno)
206 const struct target_desc *tdesc;
208 if (get_usrregs_info ()->regmap[regno] == -1)
211 tdesc = current_process ()->tdesc;
213 if (find_regno (tdesc, "r0") == regno)
220 mips_cannot_store_register (int regno)
222 const struct target_desc *tdesc;
224 if (get_usrregs_info ()->regmap[regno] == -1)
227 tdesc = current_process ()->tdesc;
229 if (find_regno (tdesc, "r0") == regno)
232 if (find_regno (tdesc, "cause") == regno)
235 if (find_regno (tdesc, "badvaddr") == regno)
238 if (find_regno (tdesc, "fir") == regno)
245 mips_get_pc (struct regcache *regcache)
247 union mips_register pc;
248 collect_register_by_name (regcache, "pc", pc.buf);
249 return register_size (regcache->tdesc, 0) == 4 ? pc.reg32 : pc.reg64;
253 mips_set_pc (struct regcache *regcache, CORE_ADDR pc)
255 union mips_register newpc;
256 if (register_size (regcache->tdesc, 0) == 4)
261 supply_register_by_name (regcache, "pc", newpc.buf);
264 /* Correct in either endianness. */
265 static const unsigned int mips_breakpoint = 0x0005000d;
266 #define mips_breakpoint_len 4
268 /* We only place breakpoints in empty marker functions, and thread locking
269 is outside of the function. So rather than importing software single-step,
270 we can just run until exit. */
272 mips_reinsert_addr (void)
274 struct regcache *regcache = get_thread_regcache (current_inferior, 1);
275 union mips_register ra;
276 collect_register_by_name (regcache, "r31", ra.buf);
277 return register_size (regcache->tdesc, 0) == 4 ? ra.reg32 : ra.reg64;
281 mips_breakpoint_at (CORE_ADDR where)
285 (*the_target->read_memory) (where, (unsigned char *) &insn, 4);
286 if (insn == mips_breakpoint)
289 /* If necessary, recognize more trap instructions here. GDB only uses the
294 /* Mark the watch registers of lwp, represented by ENTRY, as changed,
295 if the lwp's process id is *PID_P. */
298 update_watch_registers_callback (struct inferior_list_entry *entry,
301 struct lwp_info *lwp = (struct lwp_info *) entry;
302 int pid = *(int *) pid_p;
304 /* Only update the threads of this process. */
305 if (pid_of (lwp) == pid)
307 /* The actual update is done later just before resuming the lwp,
308 we just mark that the registers need updating. */
309 lwp->arch_private->watch_registers_changed = 1;
311 /* If the lwp isn't stopped, force it to momentarily pause, so
312 we can update its watch registers. */
314 linux_stop_lwp (lwp);
320 /* This is the implementation of linux_target_ops method
323 static struct arch_process_info *
324 mips_linux_new_process (void)
326 struct arch_process_info *info = xcalloc (1, sizeof (*info));
331 /* This is the implementation of linux_target_ops method new_thread.
332 Mark the watch registers as changed, so the threads' copies will
335 static struct arch_lwp_info *
336 mips_linux_new_thread (void)
338 struct arch_lwp_info *info = xcalloc (1, sizeof (*info));
340 info->watch_registers_changed = 1;
345 /* This is the implementation of linux_target_ops method
346 prepare_to_resume. If the watch regs have changed, update the
350 mips_linux_prepare_to_resume (struct lwp_info *lwp)
352 ptid_t ptid = ptid_of (lwp);
353 struct process_info *proc = find_process_pid (ptid_get_pid (ptid));
354 struct arch_process_info *private = proc->private->arch_private;
356 if (lwp->arch_private->watch_registers_changed)
358 /* Only update the watch registers if we have set or unset a
359 watchpoint already. */
360 if (mips_linux_watch_get_num_valid (&private->watch_mirror) > 0)
362 /* Write the mirrored watch register values. */
363 int tid = ptid_get_lwp (ptid);
365 if (-1 == ptrace (PTRACE_SET_WATCH_REGS, tid,
366 &private->watch_mirror))
367 perror_with_name ("Couldn't write watch register");
370 lwp->arch_private->watch_registers_changed = 0;
374 /* Translate breakpoint type TYPE in rsp to 'enum target_hw_bp_type'. */
376 static enum target_hw_bp_type
377 rsp_bp_type_to_target_hw_bp_type (char type)
389 gdb_assert_not_reached ("unhandled RSP breakpoint type");
392 /* This is the implementation of linux_target_ops method
396 mips_insert_point (char type, CORE_ADDR addr, int len)
398 struct process_info *proc = current_process ();
399 struct arch_process_info *private = proc->private->arch_private;
400 struct pt_watch_regs regs;
401 struct mips_watchpoint *new_watch;
402 struct mips_watchpoint **pw;
405 enum target_hw_bp_type watch_type;
408 /* Breakpoint/watchpoint types:
409 '0' - software-breakpoint (not supported)
410 '1' - hardware-breakpoint (not supported)
411 '2' - write watchpoint (supported)
412 '3' - read watchpoint (supported)
413 '4' - access watchpoint (supported). */
415 if (type < '2' || type > '4')
421 lwpid = lwpid_of (get_thread_lwp (current_inferior));
422 if (!mips_linux_read_watch_registers (lwpid,
423 &private->watch_readback,
424 &private->watch_readback_valid,
431 regs = private->watch_readback;
432 /* Add the current watches. */
433 mips_linux_watch_populate_regs (private->current_watches, ®s);
435 /* Now try to add the new watch. */
436 watch_type = rsp_bp_type_to_target_hw_bp_type (type);
437 irw = mips_linux_watch_type_to_irw (watch_type);
438 if (!mips_linux_watch_try_one_watch (®s, addr, len, irw))
441 /* It fit. Stick it on the end of the list. */
442 new_watch = xmalloc (sizeof (struct mips_watchpoint));
443 new_watch->addr = addr;
444 new_watch->len = len;
445 new_watch->type = watch_type;
446 new_watch->next = NULL;
448 pw = &private->current_watches;
453 private->watch_mirror = regs;
455 /* Only update the threads of this process. */
457 find_inferior (&all_lwps, update_watch_registers_callback, &pid);
462 /* This is the implementation of linux_target_ops method
466 mips_remove_point (char type, CORE_ADDR addr, int len)
468 struct process_info *proc = current_process ();
469 struct arch_process_info *private = proc->private->arch_private;
473 enum target_hw_bp_type watch_type;
475 struct mips_watchpoint **pw;
476 struct mips_watchpoint *w;
478 /* Breakpoint/watchpoint types:
479 '0' - software-breakpoint (not supported)
480 '1' - hardware-breakpoint (not supported)
481 '2' - write watchpoint (supported)
482 '3' - read watchpoint (supported)
483 '4' - access watchpoint (supported). */
485 if (type < '2' || type > '4')
491 /* Search for a known watch that matches. Then unlink and free it. */
492 watch_type = rsp_bp_type_to_target_hw_bp_type (type);
494 pw = &private->current_watches;
497 if (w->addr == addr && w->len == len && w->type == watch_type)
508 return -1; /* We don't know about it, fail doing nothing. */
510 /* At this point watch_readback is known to be valid because we
511 could not have added the watch without reading it. */
512 gdb_assert (private->watch_readback_valid == 1);
514 private->watch_mirror = private->watch_readback;
515 mips_linux_watch_populate_regs (private->current_watches,
516 &private->watch_mirror);
518 /* Only update the threads of this process. */
520 find_inferior (&all_lwps, update_watch_registers_callback, &pid);
524 /* This is the implementation of linux_target_ops method
525 stopped_by_watchpoint. The watchhi R and W bits indicate
526 the watch register triggered. */
529 mips_stopped_by_watchpoint (void)
531 struct process_info *proc = current_process ();
532 struct arch_process_info *private = proc->private->arch_private;
535 long lwpid = lwpid_of (get_thread_lwp (current_inferior));
537 if (!mips_linux_read_watch_registers (lwpid,
538 &private->watch_readback,
539 &private->watch_readback_valid,
543 num_valid = mips_linux_watch_get_num_valid (&private->watch_readback);
545 for (n = 0; n < MAX_DEBUG_REGISTER && n < num_valid; n++)
546 if (mips_linux_watch_get_watchhi (&private->watch_readback, n)
553 /* This is the implementation of linux_target_ops method
554 stopped_data_address. */
557 mips_stopped_data_address (void)
559 struct process_info *proc = current_process ();
560 struct arch_process_info *private = proc->private->arch_private;
563 long lwpid = lwpid_of (get_thread_lwp (current_inferior));
565 /* On MIPS we don't know the low order 3 bits of the data address.
566 GDB does not support remote targets that can't report the
567 watchpoint address. So, make our best guess; return the starting
568 address of a watchpoint request which overlaps the one that
571 if (!mips_linux_read_watch_registers (lwpid,
572 &private->watch_readback,
573 &private->watch_readback_valid,
577 num_valid = mips_linux_watch_get_num_valid (&private->watch_readback);
579 for (n = 0; n < MAX_DEBUG_REGISTER && n < num_valid; n++)
580 if (mips_linux_watch_get_watchhi (&private->watch_readback, n)
583 CORE_ADDR t_low, t_hi;
585 struct mips_watchpoint *watch;
587 t_low = mips_linux_watch_get_watchlo (&private->watch_readback, n);
588 t_irw = t_low & IRW_MASK;
589 t_hi = (mips_linux_watch_get_watchhi (&private->watch_readback, n)
591 t_low &= ~(CORE_ADDR)t_hi;
593 for (watch = private->current_watches;
597 CORE_ADDR addr = watch->addr;
598 CORE_ADDR last_byte = addr + watch->len - 1;
600 if ((t_irw & mips_linux_watch_type_to_irw (watch->type)) == 0)
602 /* Different type. */
605 /* Check for overlap of even a single byte. */
606 if (last_byte >= t_low && addr <= t_low + t_hi)
611 /* Shouldn't happen. */
615 /* Fetch the thread-local storage pointer for libthread_db. */
618 ps_get_thread_area (const struct ps_prochandle *ph,
619 lwpid_t lwpid, int idx, void **base)
621 if (ptrace (PTRACE_GET_THREAD_AREA, lwpid, NULL, base) != 0)
624 /* IDX is the bias from the thread pointer to the beginning of the
625 thread descriptor. It has to be subtracted due to implementation
626 quirks in libthread_db. */
627 *base = (void *) ((char *)*base - idx);
632 #ifdef HAVE_PTRACE_GETREGS
635 mips_collect_register (struct regcache *regcache,
636 int use_64bit, int regno, union mips_register *reg)
638 union mips_register tmp_reg;
642 collect_register (regcache, regno, &tmp_reg.reg64);
647 collect_register (regcache, regno, &tmp_reg.reg32);
648 reg->reg64 = tmp_reg.reg32;
653 mips_supply_register (struct regcache *regcache,
654 int use_64bit, int regno, const union mips_register *reg)
658 /* For big-endian 32-bit targets, ignore the high four bytes of each
660 if (__BYTE_ORDER == __BIG_ENDIAN && !use_64bit)
663 supply_register (regcache, regno, reg->buf + offset);
667 mips_collect_register_32bit (struct regcache *regcache,
668 int use_64bit, int regno, unsigned char *buf)
670 union mips_register tmp_reg;
673 mips_collect_register (regcache, use_64bit, regno, &tmp_reg);
674 reg32 = tmp_reg.reg64;
675 memcpy (buf, ®32, 4);
679 mips_supply_register_32bit (struct regcache *regcache,
680 int use_64bit, int regno, const unsigned char *buf)
682 union mips_register tmp_reg;
685 memcpy (®32, buf, 4);
686 tmp_reg.reg64 = reg32;
687 mips_supply_register (regcache, use_64bit, regno, &tmp_reg);
691 mips_fill_gregset (struct regcache *regcache, void *buf)
693 union mips_register *regset = buf;
695 const struct target_desc *tdesc = regcache->tdesc;
697 use_64bit = (register_size (tdesc, 0) == 8);
699 for (i = 1; i < 32; i++)
700 mips_collect_register (regcache, use_64bit, i, regset + i);
702 mips_collect_register (regcache, use_64bit,
703 find_regno (tdesc, "lo"), regset + 32);
704 mips_collect_register (regcache, use_64bit,
705 find_regno (tdesc, "hi"), regset + 33);
706 mips_collect_register (regcache, use_64bit,
707 find_regno (tdesc, "pc"), regset + 34);
708 mips_collect_register (regcache, use_64bit,
709 find_regno (tdesc, "badvaddr"), regset + 35);
710 mips_collect_register (regcache, use_64bit,
711 find_regno (tdesc, "status"), regset + 36);
712 mips_collect_register (regcache, use_64bit,
713 find_regno (tdesc, "cause"), regset + 37);
715 mips_collect_register (regcache, use_64bit,
716 find_regno (tdesc, "restart"), regset + 0);
720 mips_store_gregset (struct regcache *regcache, const void *buf)
722 const union mips_register *regset = buf;
725 use_64bit = (register_size (regcache->tdesc, 0) == 8);
727 for (i = 0; i < 32; i++)
728 mips_supply_register (regcache, use_64bit, i, regset + i);
730 mips_supply_register (regcache, use_64bit,
731 find_regno (regcache->tdesc, "lo"), regset + 32);
732 mips_supply_register (regcache, use_64bit,
733 find_regno (regcache->tdesc, "hi"), regset + 33);
734 mips_supply_register (regcache, use_64bit,
735 find_regno (regcache->tdesc, "pc"), regset + 34);
736 mips_supply_register (regcache, use_64bit,
737 find_regno (regcache->tdesc, "badvaddr"), regset + 35);
738 mips_supply_register (regcache, use_64bit,
739 find_regno (regcache->tdesc, "status"), regset + 36);
740 mips_supply_register (regcache, use_64bit,
741 find_regno (regcache->tdesc, "cause"), regset + 37);
743 mips_supply_register (regcache, use_64bit,
744 find_regno (regcache->tdesc, "restart"), regset + 0);
748 mips_fill_fpregset (struct regcache *regcache, void *buf)
750 union mips_register *regset = buf;
751 int i, use_64bit, first_fp, big_endian;
753 use_64bit = (register_size (regcache->tdesc, 0) == 8);
754 first_fp = find_regno (regcache->tdesc, "f0");
755 big_endian = (__BYTE_ORDER == __BIG_ENDIAN);
757 /* See GDB for a discussion of this peculiar layout. */
758 for (i = 0; i < 32; i++)
760 collect_register (regcache, first_fp + i, regset[i].buf);
762 collect_register (regcache, first_fp + i,
763 regset[i & ~1].buf + 4 * (big_endian != (i & 1)));
765 mips_collect_register_32bit (regcache, use_64bit,
766 find_regno (regcache->tdesc, "fcsr"), regset[32].buf);
767 mips_collect_register_32bit (regcache, use_64bit,
768 find_regno (regcache->tdesc, "fir"),
773 mips_store_fpregset (struct regcache *regcache, const void *buf)
775 const union mips_register *regset = buf;
776 int i, use_64bit, first_fp, big_endian;
778 use_64bit = (register_size (regcache->tdesc, 0) == 8);
779 first_fp = find_regno (regcache->tdesc, "f0");
780 big_endian = (__BYTE_ORDER == __BIG_ENDIAN);
782 /* See GDB for a discussion of this peculiar layout. */
783 for (i = 0; i < 32; i++)
785 supply_register (regcache, first_fp + i, regset[i].buf);
787 supply_register (regcache, first_fp + i,
788 regset[i & ~1].buf + 4 * (big_endian != (i & 1)));
790 mips_supply_register_32bit (regcache, use_64bit,
791 find_regno (regcache->tdesc, "fcsr"),
793 mips_supply_register_32bit (regcache, use_64bit,
794 find_regno (regcache->tdesc, "fir"),
797 #endif /* HAVE_PTRACE_GETREGS */
799 static struct regset_info mips_regsets[] = {
800 #ifdef HAVE_PTRACE_GETREGS
801 { PTRACE_GETREGS, PTRACE_SETREGS, 0, 38 * 8, GENERAL_REGS,
802 mips_fill_gregset, mips_store_gregset },
803 { PTRACE_GETFPREGS, PTRACE_SETFPREGS, 0, 33 * 8, FP_REGS,
804 mips_fill_fpregset, mips_store_fpregset },
805 #endif /* HAVE_PTRACE_GETREGS */
806 { 0, 0, 0, -1, -1, NULL, NULL }
809 static struct regsets_info mips_regsets_info =
811 mips_regsets, /* regsets */
813 NULL, /* disabled_regsets */
816 static struct usrregs_info mips_dsp_usrregs_info =
822 static struct usrregs_info mips_usrregs_info =
828 static struct regs_info dsp_regs_info =
830 mips_dsp_regset_bitmap,
831 &mips_dsp_usrregs_info,
835 static struct regs_info regs_info =
837 NULL, /* regset_bitmap */
842 static const struct regs_info *
843 mips_regs_info (void)
846 return &dsp_regs_info;
851 struct linux_target_ops the_low_target = {
854 mips_cannot_fetch_register,
855 mips_cannot_store_register,
856 NULL, /* fetch_register */
859 (const unsigned char *) &mips_breakpoint,
866 mips_stopped_by_watchpoint,
867 mips_stopped_data_address,
870 NULL, /* siginfo_fixup */
871 mips_linux_new_process,
872 mips_linux_new_thread,
873 mips_linux_prepare_to_resume
877 initialize_low_arch (void)
879 /* Initialize the Linux target descriptions. */
880 init_registers_mips_linux ();
881 init_registers_mips_dsp_linux ();
882 init_registers_mips64_linux ();
883 init_registers_mips64_dsp_linux ();
885 initialize_regsets_info (&mips_regsets_info);