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 /* Defined in auto-generated file mips-dsp-linux.c. */
30 void init_registers_mips_dsp_linux (void);
31 /* Defined in auto-generated file mips64-linux.c. */
32 void init_registers_mips64_linux (void);
33 /* Defined in auto-generated file mips64-dsp-linux.c. */
34 void init_registers_mips64_dsp_linux (void);
37 #define init_registers_mips_linux init_registers_mips64_linux
38 #define init_registers_mips_dsp_linux init_registers_mips64_dsp_linux
41 #ifndef PTRACE_GET_THREAD_AREA
42 #define PTRACE_GET_THREAD_AREA 25
49 #define mips_num_regs 73
50 #define mips_dsp_num_regs 80
52 #include <asm/ptrace.h>
56 #define DSP_CONTROL 77
63 /* Deliberately signed, for proper sign extension. */
68 /* Return the ptrace ``address'' of register REGNO. */
70 #define mips_base_regs \
71 -1, 1, 2, 3, 4, 5, 6, 7, \
72 8, 9, 10, 11, 12, 13, 14, 15, \
73 16, 17, 18, 19, 20, 21, 22, 23, \
74 24, 25, 26, 27, 28, 29, 30, 31, \
76 -1, MMLO, MMHI, BADVADDR, CAUSE, PC, \
78 FPR_BASE, FPR_BASE + 1, FPR_BASE + 2, FPR_BASE + 3, \
79 FPR_BASE + 4, FPR_BASE + 5, FPR_BASE + 6, FPR_BASE + 7, \
80 FPR_BASE + 8, FPR_BASE + 9, FPR_BASE + 10, FPR_BASE + 11, \
81 FPR_BASE + 12, FPR_BASE + 13, FPR_BASE + 14, FPR_BASE + 15, \
82 FPR_BASE + 16, FPR_BASE + 17, FPR_BASE + 18, FPR_BASE + 19, \
83 FPR_BASE + 20, FPR_BASE + 21, FPR_BASE + 22, FPR_BASE + 23, \
84 FPR_BASE + 24, FPR_BASE + 25, FPR_BASE + 26, FPR_BASE + 27, \
85 FPR_BASE + 28, FPR_BASE + 29, FPR_BASE + 30, FPR_BASE + 31, \
88 #define mips_dsp_regs \
89 DSP_BASE, DSP_BASE + 1, DSP_BASE + 2, DSP_BASE + 3, \
90 DSP_BASE + 4, DSP_BASE + 5, \
93 static int mips_regmap
[mips_num_regs
] = {
98 static int mips_dsp_regmap
[mips_dsp_num_regs
] = {
104 /* DSP registers are not in any regset and can only be accessed
107 static unsigned char mips_dsp_regset_bitmap
[(mips_dsp_num_regs
+ 7) / 8] = {
108 0xfe, 0xff, 0xff, 0xff, 0xfe, 0xff, 0xff, 0xff, 0xff, 0x80
111 /* Try peeking at an arbitrarily chosen DSP register and pick the available
112 user register set accordingly. */
115 mips_arch_setup (void)
117 static void (*init_registers
) (void);
119 gdb_assert (current_inferior
);
121 if (init_registers
== NULL
)
123 int pid
= lwpid_of (get_thread_lwp (current_inferior
));
125 ptrace (PTRACE_PEEKUSER
, pid
, DSP_CONTROL
, 0);
129 the_low_target
.num_regs
= mips_dsp_num_regs
;
130 the_low_target
.regmap
= mips_dsp_regmap
;
131 the_low_target
.regset_bitmap
= mips_dsp_regset_bitmap
;
132 init_registers
= init_registers_mips_dsp_linux
;
135 the_low_target
.num_regs
= mips_num_regs
;
136 the_low_target
.regmap
= mips_regmap
;
137 the_low_target
.regset_bitmap
= NULL
;
138 init_registers
= init_registers_mips_linux
;
141 perror_with_name ("ptrace");
148 /* From mips-linux-nat.c. */
150 /* Pseudo registers can not be read. ptrace does not provide a way to
151 read (or set) PS_REGNUM, and there's no point in reading or setting
152 ZERO_REGNUM. We also can not set BADVADDR, CAUSE, or FCRIR via
156 mips_cannot_fetch_register (int regno
)
158 if (the_low_target
.regmap
[regno
] == -1)
161 if (find_regno ("r0") == regno
)
168 mips_cannot_store_register (int regno
)
170 if (the_low_target
.regmap
[regno
] == -1)
173 if (find_regno ("r0") == regno
)
176 if (find_regno ("cause") == regno
)
179 if (find_regno ("badvaddr") == regno
)
182 if (find_regno ("fir") == regno
)
189 mips_get_pc (struct regcache
*regcache
)
191 union mips_register pc
;
192 collect_register_by_name (regcache
, "pc", pc
.buf
);
193 return register_size (0) == 4 ? pc
.reg32
: pc
.reg64
;
197 mips_set_pc (struct regcache
*regcache
, CORE_ADDR pc
)
199 union mips_register newpc
;
200 if (register_size (0) == 4)
205 supply_register_by_name (regcache
, "pc", newpc
.buf
);
208 /* Correct in either endianness. */
209 static const unsigned int mips_breakpoint
= 0x0005000d;
210 #define mips_breakpoint_len 4
212 /* We only place breakpoints in empty marker functions, and thread locking
213 is outside of the function. So rather than importing software single-step,
214 we can just run until exit. */
216 mips_reinsert_addr (void)
218 struct regcache
*regcache
= get_thread_regcache (current_inferior
, 1);
219 union mips_register ra
;
220 collect_register_by_name (regcache
, "r31", ra
.buf
);
221 return register_size (0) == 4 ? ra
.reg32
: ra
.reg64
;
225 mips_breakpoint_at (CORE_ADDR where
)
229 (*the_target
->read_memory
) (where
, (unsigned char *) &insn
, 4);
230 if (insn
== mips_breakpoint
)
233 /* If necessary, recognize more trap instructions here. GDB only uses the
238 /* Fetch the thread-local storage pointer for libthread_db. */
241 ps_get_thread_area (const struct ps_prochandle
*ph
,
242 lwpid_t lwpid
, int idx
, void **base
)
244 if (ptrace (PTRACE_GET_THREAD_AREA
, lwpid
, NULL
, base
) != 0)
247 /* IDX is the bias from the thread pointer to the beginning of the
248 thread descriptor. It has to be subtracted due to implementation
249 quirks in libthread_db. */
250 *base
= (void *) ((char *)*base
- idx
);
255 #ifdef HAVE_PTRACE_GETREGS
258 mips_collect_register (struct regcache
*regcache
,
259 int use_64bit
, int regno
, union mips_register
*reg
)
261 union mips_register tmp_reg
;
265 collect_register (regcache
, regno
, &tmp_reg
.reg64
);
270 collect_register (regcache
, regno
, &tmp_reg
.reg32
);
271 reg
->reg64
= tmp_reg
.reg32
;
276 mips_supply_register (struct regcache
*regcache
,
277 int use_64bit
, int regno
, const union mips_register
*reg
)
281 /* For big-endian 32-bit targets, ignore the high four bytes of each
283 if (__BYTE_ORDER
== __BIG_ENDIAN
&& !use_64bit
)
286 supply_register (regcache
, regno
, reg
->buf
+ offset
);
290 mips_collect_register_32bit (struct regcache
*regcache
,
291 int use_64bit
, int regno
, unsigned char *buf
)
293 union mips_register tmp_reg
;
296 mips_collect_register (regcache
, use_64bit
, regno
, &tmp_reg
);
297 reg32
= tmp_reg
.reg64
;
298 memcpy (buf
, ®32
, 4);
302 mips_supply_register_32bit (struct regcache
*regcache
,
303 int use_64bit
, int regno
, const unsigned char *buf
)
305 union mips_register tmp_reg
;
308 memcpy (®32
, buf
, 4);
309 tmp_reg
.reg64
= reg32
;
310 mips_supply_register (regcache
, use_64bit
, regno
, &tmp_reg
);
314 mips_fill_gregset (struct regcache
*regcache
, void *buf
)
316 union mips_register
*regset
= buf
;
319 use_64bit
= (register_size (0) == 8);
321 for (i
= 1; i
< 32; i
++)
322 mips_collect_register (regcache
, use_64bit
, i
, regset
+ i
);
324 mips_collect_register (regcache
, use_64bit
,
325 find_regno ("lo"), regset
+ 32);
326 mips_collect_register (regcache
, use_64bit
,
327 find_regno ("hi"), regset
+ 33);
328 mips_collect_register (regcache
, use_64bit
,
329 find_regno ("pc"), regset
+ 34);
330 mips_collect_register (regcache
, use_64bit
,
331 find_regno ("badvaddr"), regset
+ 35);
332 mips_collect_register (regcache
, use_64bit
,
333 find_regno ("status"), regset
+ 36);
334 mips_collect_register (regcache
, use_64bit
,
335 find_regno ("cause"), regset
+ 37);
337 mips_collect_register (regcache
, use_64bit
,
338 find_regno ("restart"), regset
+ 0);
342 mips_store_gregset (struct regcache
*regcache
, const void *buf
)
344 const union mips_register
*regset
= buf
;
347 use_64bit
= (register_size (0) == 8);
349 for (i
= 0; i
< 32; i
++)
350 mips_supply_register (regcache
, use_64bit
, i
, regset
+ i
);
352 mips_supply_register (regcache
, use_64bit
, find_regno ("lo"), regset
+ 32);
353 mips_supply_register (regcache
, use_64bit
, find_regno ("hi"), regset
+ 33);
354 mips_supply_register (regcache
, use_64bit
, find_regno ("pc"), regset
+ 34);
355 mips_supply_register (regcache
, use_64bit
,
356 find_regno ("badvaddr"), regset
+ 35);
357 mips_supply_register (regcache
, use_64bit
,
358 find_regno ("status"), regset
+ 36);
359 mips_supply_register (regcache
, use_64bit
,
360 find_regno ("cause"), regset
+ 37);
362 mips_supply_register (regcache
, use_64bit
,
363 find_regno ("restart"), regset
+ 0);
367 mips_fill_fpregset (struct regcache
*regcache
, void *buf
)
369 union mips_register
*regset
= buf
;
370 int i
, use_64bit
, first_fp
, big_endian
;
372 use_64bit
= (register_size (0) == 8);
373 first_fp
= find_regno ("f0");
374 big_endian
= (__BYTE_ORDER
== __BIG_ENDIAN
);
376 /* See GDB for a discussion of this peculiar layout. */
377 for (i
= 0; i
< 32; i
++)
379 collect_register (regcache
, first_fp
+ i
, regset
[i
].buf
);
381 collect_register (regcache
, first_fp
+ i
,
382 regset
[i
& ~1].buf
+ 4 * (big_endian
!= (i
& 1)));
384 mips_collect_register_32bit (regcache
, use_64bit
,
385 find_regno ("fcsr"), regset
[32].buf
);
386 mips_collect_register_32bit (regcache
, use_64bit
, find_regno ("fir"),
391 mips_store_fpregset (struct regcache
*regcache
, const void *buf
)
393 const union mips_register
*regset
= buf
;
394 int i
, use_64bit
, first_fp
, big_endian
;
396 use_64bit
= (register_size (0) == 8);
397 first_fp
= find_regno ("f0");
398 big_endian
= (__BYTE_ORDER
== __BIG_ENDIAN
);
400 /* See GDB for a discussion of this peculiar layout. */
401 for (i
= 0; i
< 32; i
++)
403 supply_register (regcache
, first_fp
+ i
, regset
[i
].buf
);
405 supply_register (regcache
, first_fp
+ i
,
406 regset
[i
& ~1].buf
+ 4 * (big_endian
!= (i
& 1)));
408 mips_supply_register_32bit (regcache
, use_64bit
,
409 find_regno ("fcsr"), regset
[32].buf
);
410 mips_supply_register_32bit (regcache
, use_64bit
, find_regno ("fir"),
413 #endif /* HAVE_PTRACE_GETREGS */
415 struct regset_info target_regsets
[] = {
416 #ifdef HAVE_PTRACE_GETREGS
417 { PTRACE_GETREGS
, PTRACE_SETREGS
, 0, 38 * 8, GENERAL_REGS
,
418 mips_fill_gregset
, mips_store_gregset
},
419 { PTRACE_GETFPREGS
, PTRACE_SETFPREGS
, 0, 33 * 8, FP_REGS
,
420 mips_fill_fpregset
, mips_store_fpregset
},
421 #endif /* HAVE_PTRACE_GETREGS */
422 { 0, 0, 0, -1, -1, NULL
, NULL
}
425 struct linux_target_ops the_low_target
= {
430 mips_cannot_fetch_register
,
431 mips_cannot_store_register
,
432 NULL
, /* fetch_register */
435 (const unsigned char *) &mips_breakpoint
,