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 "gdb_proc_service.h"
27 /* Defined in auto-generated file mips-linux.c. */
28 void init_registers_mips_linux (void);
29 extern const struct target_desc *tdesc_mips_linux;
31 /* Defined in auto-generated file mips-dsp-linux.c. */
32 void init_registers_mips_dsp_linux (void);
33 extern const struct target_desc *tdesc_mips_dsp_linux;
35 /* Defined in auto-generated file mips64-linux.c. */
36 void init_registers_mips64_linux (void);
37 extern const struct target_desc *tdesc_mips64_linux;
39 /* Defined in auto-generated file mips64-dsp-linux.c. */
40 void init_registers_mips64_dsp_linux (void);
41 extern const struct target_desc *tdesc_mips64_dsp_linux;
44 #define tdesc_mips_linux tdesc_mips64_linux
45 #define tdesc_mips_dsp_linux tdesc_mips64_dsp_linux
48 #ifndef PTRACE_GET_THREAD_AREA
49 #define PTRACE_GET_THREAD_AREA 25
56 #define mips_num_regs 73
57 #define mips_dsp_num_regs 80
59 #include <asm/ptrace.h>
63 #define DSP_CONTROL 77
70 /* Deliberately signed, for proper sign extension. */
75 /* Return the ptrace ``address'' of register REGNO. */
77 #define mips_base_regs \
78 -1, 1, 2, 3, 4, 5, 6, 7, \
79 8, 9, 10, 11, 12, 13, 14, 15, \
80 16, 17, 18, 19, 20, 21, 22, 23, \
81 24, 25, 26, 27, 28, 29, 30, 31, \
83 -1, MMLO, MMHI, BADVADDR, CAUSE, PC, \
85 FPR_BASE, FPR_BASE + 1, FPR_BASE + 2, FPR_BASE + 3, \
86 FPR_BASE + 4, FPR_BASE + 5, FPR_BASE + 6, FPR_BASE + 7, \
87 FPR_BASE + 8, FPR_BASE + 9, FPR_BASE + 10, FPR_BASE + 11, \
88 FPR_BASE + 12, FPR_BASE + 13, FPR_BASE + 14, FPR_BASE + 15, \
89 FPR_BASE + 16, FPR_BASE + 17, FPR_BASE + 18, FPR_BASE + 19, \
90 FPR_BASE + 20, FPR_BASE + 21, FPR_BASE + 22, FPR_BASE + 23, \
91 FPR_BASE + 24, FPR_BASE + 25, FPR_BASE + 26, FPR_BASE + 27, \
92 FPR_BASE + 28, FPR_BASE + 29, FPR_BASE + 30, FPR_BASE + 31, \
95 #define mips_dsp_regs \
96 DSP_BASE, DSP_BASE + 1, DSP_BASE + 2, DSP_BASE + 3, \
97 DSP_BASE + 4, DSP_BASE + 5, \
100 static int mips_regmap[mips_num_regs] = {
105 static int mips_dsp_regmap[mips_dsp_num_regs] = {
111 /* DSP registers are not in any regset and can only be accessed
114 static unsigned char mips_dsp_regset_bitmap[(mips_dsp_num_regs + 7) / 8] = {
115 0xfe, 0xff, 0xff, 0xff, 0xfe, 0xff, 0xff, 0xff, 0xff, 0x80
118 static int have_dsp = -1;
120 /* Try peeking at an arbitrarily chosen DSP register and pick the available
121 user register set accordingly. */
123 static const struct target_desc *
124 mips_read_description (void)
128 int pid = lwpid_of (get_thread_lwp (current_inferior));
130 ptrace (PTRACE_PEEKUSER, pid, DSP_CONTROL, 0);
140 perror_with_name ("ptrace");
145 return have_dsp ? tdesc_mips_dsp_linux : tdesc_mips_linux;
149 mips_arch_setup (void)
151 current_process ()->tdesc = mips_read_description ();
154 static struct usrregs_info *
155 get_usrregs_info (void)
157 const struct regs_info *regs_info = the_low_target.regs_info ();
159 return regs_info->usrregs;
162 /* From mips-linux-nat.c. */
164 /* Pseudo registers can not be read. ptrace does not provide a way to
165 read (or set) PS_REGNUM, and there's no point in reading or setting
166 ZERO_REGNUM. We also can not set BADVADDR, CAUSE, or FCRIR via
170 mips_cannot_fetch_register (int regno)
172 const struct target_desc *tdesc;
174 if (get_usrregs_info ()->regmap[regno] == -1)
177 tdesc = current_process ()->tdesc;
179 if (find_regno (tdesc, "r0") == regno)
186 mips_cannot_store_register (int regno)
188 const struct target_desc *tdesc;
190 if (get_usrregs_info ()->regmap[regno] == -1)
193 tdesc = current_process ()->tdesc;
195 if (find_regno (tdesc, "r0") == regno)
198 if (find_regno (tdesc, "cause") == regno)
201 if (find_regno (tdesc, "badvaddr") == regno)
204 if (find_regno (tdesc, "fir") == regno)
211 mips_get_pc (struct regcache *regcache)
213 union mips_register pc;
214 collect_register_by_name (regcache, "pc", pc.buf);
215 return register_size (regcache->tdesc, 0) == 4 ? pc.reg32 : pc.reg64;
219 mips_set_pc (struct regcache *regcache, CORE_ADDR pc)
221 union mips_register newpc;
222 if (register_size (regcache->tdesc, 0) == 4)
227 supply_register_by_name (regcache, "pc", newpc.buf);
230 /* Correct in either endianness. */
231 static const unsigned int mips_breakpoint = 0x0005000d;
232 #define mips_breakpoint_len 4
234 /* We only place breakpoints in empty marker functions, and thread locking
235 is outside of the function. So rather than importing software single-step,
236 we can just run until exit. */
238 mips_reinsert_addr (void)
240 struct regcache *regcache = get_thread_regcache (current_inferior, 1);
241 union mips_register ra;
242 collect_register_by_name (regcache, "r31", ra.buf);
243 return register_size (regcache->tdesc, 0) == 4 ? ra.reg32 : ra.reg64;
247 mips_breakpoint_at (CORE_ADDR where)
251 (*the_target->read_memory) (where, (unsigned char *) &insn, 4);
252 if (insn == mips_breakpoint)
255 /* If necessary, recognize more trap instructions here. GDB only uses the
260 /* Fetch the thread-local storage pointer for libthread_db. */
263 ps_get_thread_area (const struct ps_prochandle *ph,
264 lwpid_t lwpid, int idx, void **base)
266 if (ptrace (PTRACE_GET_THREAD_AREA, lwpid, NULL, base) != 0)
269 /* IDX is the bias from the thread pointer to the beginning of the
270 thread descriptor. It has to be subtracted due to implementation
271 quirks in libthread_db. */
272 *base = (void *) ((char *)*base - idx);
277 #ifdef HAVE_PTRACE_GETREGS
280 mips_collect_register (struct regcache *regcache,
281 int use_64bit, int regno, union mips_register *reg)
283 union mips_register tmp_reg;
287 collect_register (regcache, regno, &tmp_reg.reg64);
292 collect_register (regcache, regno, &tmp_reg.reg32);
293 reg->reg64 = tmp_reg.reg32;
298 mips_supply_register (struct regcache *regcache,
299 int use_64bit, int regno, const union mips_register *reg)
303 /* For big-endian 32-bit targets, ignore the high four bytes of each
305 if (__BYTE_ORDER == __BIG_ENDIAN && !use_64bit)
308 supply_register (regcache, regno, reg->buf + offset);
312 mips_collect_register_32bit (struct regcache *regcache,
313 int use_64bit, int regno, unsigned char *buf)
315 union mips_register tmp_reg;
318 mips_collect_register (regcache, use_64bit, regno, &tmp_reg);
319 reg32 = tmp_reg.reg64;
320 memcpy (buf, ®32, 4);
324 mips_supply_register_32bit (struct regcache *regcache,
325 int use_64bit, int regno, const unsigned char *buf)
327 union mips_register tmp_reg;
330 memcpy (®32, buf, 4);
331 tmp_reg.reg64 = reg32;
332 mips_supply_register (regcache, use_64bit, regno, &tmp_reg);
336 mips_fill_gregset (struct regcache *regcache, void *buf)
338 union mips_register *regset = buf;
340 const struct target_desc *tdesc = regcache->tdesc;
342 use_64bit = (register_size (tdesc, 0) == 8);
344 for (i = 1; i < 32; i++)
345 mips_collect_register (regcache, use_64bit, i, regset + i);
347 mips_collect_register (regcache, use_64bit,
348 find_regno (tdesc, "lo"), regset + 32);
349 mips_collect_register (regcache, use_64bit,
350 find_regno (tdesc, "hi"), regset + 33);
351 mips_collect_register (regcache, use_64bit,
352 find_regno (tdesc, "pc"), regset + 34);
353 mips_collect_register (regcache, use_64bit,
354 find_regno (tdesc, "badvaddr"), regset + 35);
355 mips_collect_register (regcache, use_64bit,
356 find_regno (tdesc, "status"), regset + 36);
357 mips_collect_register (regcache, use_64bit,
358 find_regno (tdesc, "cause"), regset + 37);
360 mips_collect_register (regcache, use_64bit,
361 find_regno (tdesc, "restart"), regset + 0);
365 mips_store_gregset (struct regcache *regcache, const void *buf)
367 const union mips_register *regset = buf;
370 use_64bit = (register_size (regcache->tdesc, 0) == 8);
372 for (i = 0; i < 32; i++)
373 mips_supply_register (regcache, use_64bit, i, regset + i);
375 mips_supply_register (regcache, use_64bit,
376 find_regno (regcache->tdesc, "lo"), regset + 32);
377 mips_supply_register (regcache, use_64bit,
378 find_regno (regcache->tdesc, "hi"), regset + 33);
379 mips_supply_register (regcache, use_64bit,
380 find_regno (regcache->tdesc, "pc"), regset + 34);
381 mips_supply_register (regcache, use_64bit,
382 find_regno (regcache->tdesc, "badvaddr"), regset + 35);
383 mips_supply_register (regcache, use_64bit,
384 find_regno (regcache->tdesc, "status"), regset + 36);
385 mips_supply_register (regcache, use_64bit,
386 find_regno (regcache->tdesc, "cause"), regset + 37);
388 mips_supply_register (regcache, use_64bit,
389 find_regno (regcache->tdesc, "restart"), regset + 0);
393 mips_fill_fpregset (struct regcache *regcache, void *buf)
395 union mips_register *regset = buf;
396 int i, use_64bit, first_fp, big_endian;
398 use_64bit = (register_size (regcache->tdesc, 0) == 8);
399 first_fp = find_regno (regcache->tdesc, "f0");
400 big_endian = (__BYTE_ORDER == __BIG_ENDIAN);
402 /* See GDB for a discussion of this peculiar layout. */
403 for (i = 0; i < 32; i++)
405 collect_register (regcache, first_fp + i, regset[i].buf);
407 collect_register (regcache, first_fp + i,
408 regset[i & ~1].buf + 4 * (big_endian != (i & 1)));
410 mips_collect_register_32bit (regcache, use_64bit,
411 find_regno (regcache->tdesc, "fcsr"), regset[32].buf);
412 mips_collect_register_32bit (regcache, use_64bit,
413 find_regno (regcache->tdesc, "fir"),
418 mips_store_fpregset (struct regcache *regcache, const void *buf)
420 const union mips_register *regset = buf;
421 int i, use_64bit, first_fp, big_endian;
423 use_64bit = (register_size (regcache->tdesc, 0) == 8);
424 first_fp = find_regno (regcache->tdesc, "f0");
425 big_endian = (__BYTE_ORDER == __BIG_ENDIAN);
427 /* See GDB for a discussion of this peculiar layout. */
428 for (i = 0; i < 32; i++)
430 supply_register (regcache, first_fp + i, regset[i].buf);
432 supply_register (regcache, first_fp + i,
433 regset[i & ~1].buf + 4 * (big_endian != (i & 1)));
435 mips_supply_register_32bit (regcache, use_64bit,
436 find_regno (regcache->tdesc, "fcsr"),
438 mips_supply_register_32bit (regcache, use_64bit,
439 find_regno (regcache->tdesc, "fir"),
442 #endif /* HAVE_PTRACE_GETREGS */
444 static struct regset_info mips_regsets[] = {
445 #ifdef HAVE_PTRACE_GETREGS
446 { PTRACE_GETREGS, PTRACE_SETREGS, 0, 38 * 8, GENERAL_REGS,
447 mips_fill_gregset, mips_store_gregset },
448 { PTRACE_GETFPREGS, PTRACE_SETFPREGS, 0, 33 * 8, FP_REGS,
449 mips_fill_fpregset, mips_store_fpregset },
450 #endif /* HAVE_PTRACE_GETREGS */
451 { 0, 0, 0, -1, -1, NULL, NULL }
454 static struct regsets_info mips_regsets_info =
456 mips_regsets, /* regsets */
458 NULL, /* disabled_regsets */
461 static struct usrregs_info mips_dsp_usrregs_info =
467 static struct usrregs_info mips_usrregs_info =
473 static struct regs_info dsp_regs_info =
475 mips_dsp_regset_bitmap,
476 &mips_dsp_usrregs_info,
480 static struct regs_info regs_info =
482 NULL, /* regset_bitmap */
487 static const struct regs_info *
488 mips_regs_info (void)
491 return &dsp_regs_info;
496 struct linux_target_ops the_low_target = {
499 mips_cannot_fetch_register,
500 mips_cannot_store_register,
501 NULL, /* fetch_register */
504 (const unsigned char *) &mips_breakpoint,
512 initialize_low_arch (void)
514 /* Initialize the Linux target descriptions. */
515 init_registers_mips_linux ();
516 init_registers_mips_dsp_linux ();
517 init_registers_mips64_linux ();
518 init_registers_mips64_dsp_linux ();
520 initialize_regsets_info (&mips_regsets_info);