MIPS: Replace regset_alloc() invocations by static regset structures.
[platform/upstream/binutils.git] / gdb / mips-linux-tdep.c
1 /* Target-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 "gdbcore.h"
22 #include "target.h"
23 #include "solib-svr4.h"
24 #include "osabi.h"
25 #include "mips-tdep.h"
26 #include <string.h>
27 #include "gdb_assert.h"
28 #include "frame.h"
29 #include "regcache.h"
30 #include "trad-frame.h"
31 #include "tramp-frame.h"
32 #include "gdbtypes.h"
33 #include "objfiles.h"
34 #include "solib.h"
35 #include "solist.h"
36 #include "symtab.h"
37 #include "target-descriptions.h"
38 #include "regset.h"
39 #include "mips-linux-tdep.h"
40 #include "glibc-tdep.h"
41 #include "linux-tdep.h"
42 #include "xml-syscall.h"
43 #include "gdb_signals.h"
44
45 static struct target_so_ops mips_svr4_so_ops;
46
47 /* This enum represents the signals' numbers on the MIPS
48    architecture.  It just contains the signal definitions which are
49    different from the generic implementation.
50
51    It is derived from the file <arch/mips/include/uapi/asm/signal.h>,
52    from the Linux kernel tree.  */
53
54 enum
55   {
56     MIPS_LINUX_SIGEMT = 7,
57     MIPS_LINUX_SIGBUS = 10,
58     MIPS_LINUX_SIGSYS = 12,
59     MIPS_LINUX_SIGUSR1 = 16,
60     MIPS_LINUX_SIGUSR2 = 17,
61     MIPS_LINUX_SIGCHLD = 18,
62     MIPS_LINUX_SIGCLD = MIPS_LINUX_SIGCHLD,
63     MIPS_LINUX_SIGPWR = 19,
64     MIPS_LINUX_SIGWINCH = 20,
65     MIPS_LINUX_SIGURG = 21,
66     MIPS_LINUX_SIGIO = 22,
67     MIPS_LINUX_SIGPOLL = MIPS_LINUX_SIGIO,
68     MIPS_LINUX_SIGSTOP = 23,
69     MIPS_LINUX_SIGTSTP = 24,
70     MIPS_LINUX_SIGCONT = 25,
71     MIPS_LINUX_SIGTTIN = 26,
72     MIPS_LINUX_SIGTTOU = 27,
73     MIPS_LINUX_SIGVTALRM = 28,
74     MIPS_LINUX_SIGPROF = 29,
75     MIPS_LINUX_SIGXCPU = 30,
76     MIPS_LINUX_SIGXFSZ = 31,
77
78     MIPS_LINUX_SIGRTMIN = 32,
79     MIPS_LINUX_SIGRT64 = 64,
80     MIPS_LINUX_SIGRTMAX = 127,
81   };
82
83 /* Figure out where the longjmp will land.
84    We expect the first arg to be a pointer to the jmp_buf structure
85    from which we extract the pc (MIPS_LINUX_JB_PC) that we will land
86    at.  The pc is copied into PC.  This routine returns 1 on
87    success.  */
88
89 #define MIPS_LINUX_JB_ELEMENT_SIZE 4
90 #define MIPS_LINUX_JB_PC 0
91
92 static int
93 mips_linux_get_longjmp_target (struct frame_info *frame, CORE_ADDR *pc)
94 {
95   CORE_ADDR jb_addr;
96   struct gdbarch *gdbarch = get_frame_arch (frame);
97   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
98   gdb_byte buf[gdbarch_ptr_bit (gdbarch) / TARGET_CHAR_BIT];
99
100   jb_addr = get_frame_register_unsigned (frame, MIPS_A0_REGNUM);
101
102   if (target_read_memory ((jb_addr
103                            + MIPS_LINUX_JB_PC * MIPS_LINUX_JB_ELEMENT_SIZE),
104                           buf, gdbarch_ptr_bit (gdbarch) / TARGET_CHAR_BIT))
105     return 0;
106
107   *pc = extract_unsigned_integer (buf,
108                                   gdbarch_ptr_bit (gdbarch) / TARGET_CHAR_BIT,
109                                   byte_order);
110
111   return 1;
112 }
113
114 /* Transform the bits comprising a 32-bit register to the right size
115    for regcache_raw_supply().  This is needed when mips_isa_regsize()
116    is 8.  */
117
118 static void
119 supply_32bit_reg (struct regcache *regcache, int regnum, const void *addr)
120 {
121   struct gdbarch *gdbarch = get_regcache_arch (regcache);
122   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
123   gdb_byte buf[MAX_REGISTER_SIZE];
124   store_signed_integer (buf, register_size (gdbarch, regnum), byte_order,
125                         extract_signed_integer (addr, 4, byte_order));
126   regcache_raw_supply (regcache, regnum, buf);
127 }
128
129 /* Unpack an elf_gregset_t into GDB's register cache.  */
130
131 void
132 mips_supply_gregset (struct regcache *regcache,
133                      const mips_elf_gregset_t *gregsetp)
134 {
135   int regi;
136   const mips_elf_greg_t *regp = *gregsetp;
137   char zerobuf[MAX_REGISTER_SIZE];
138   struct gdbarch *gdbarch = get_regcache_arch (regcache);
139
140   memset (zerobuf, 0, MAX_REGISTER_SIZE);
141
142   for (regi = EF_REG0 + 1; regi <= EF_REG31; regi++)
143     supply_32bit_reg (regcache, regi - EF_REG0, regp + regi);
144
145   if (mips_linux_restart_reg_p (gdbarch))
146     supply_32bit_reg (regcache, MIPS_RESTART_REGNUM, regp + EF_REG0);
147
148   supply_32bit_reg (regcache, mips_regnum (gdbarch)->lo, regp + EF_LO);
149   supply_32bit_reg (regcache, mips_regnum (gdbarch)->hi, regp + EF_HI);
150
151   supply_32bit_reg (regcache, mips_regnum (gdbarch)->pc,
152                     regp + EF_CP0_EPC);
153   supply_32bit_reg (regcache, mips_regnum (gdbarch)->badvaddr,
154                     regp + EF_CP0_BADVADDR);
155   supply_32bit_reg (regcache, MIPS_PS_REGNUM, regp + EF_CP0_STATUS);
156   supply_32bit_reg (regcache, mips_regnum (gdbarch)->cause,
157                     regp + EF_CP0_CAUSE);
158
159   /* Fill the inaccessible zero register with zero.  */
160   regcache_raw_supply (regcache, MIPS_ZERO_REGNUM, zerobuf);
161 }
162
163 static void
164 mips_supply_gregset_wrapper (const struct regset *regset,
165                              struct regcache *regcache,
166                              int regnum, const void *gregs, size_t len)
167 {
168   gdb_assert (len == sizeof (mips_elf_gregset_t));
169
170   mips_supply_gregset (regcache, (const mips_elf_gregset_t *)gregs);
171 }
172
173 /* Pack our registers (or one register) into an elf_gregset_t.  */
174
175 void
176 mips_fill_gregset (const struct regcache *regcache,
177                    mips_elf_gregset_t *gregsetp, int regno)
178 {
179   struct gdbarch *gdbarch = get_regcache_arch (regcache);
180   int regaddr, regi;
181   mips_elf_greg_t *regp = *gregsetp;
182   void *dst;
183
184   if (regno == -1)
185     {
186       memset (regp, 0, sizeof (mips_elf_gregset_t));
187       for (regi = 1; regi < 32; regi++)
188         mips_fill_gregset (regcache, gregsetp, regi);
189       mips_fill_gregset (regcache, gregsetp, mips_regnum (gdbarch)->lo);
190       mips_fill_gregset (regcache, gregsetp, mips_regnum (gdbarch)->hi);
191       mips_fill_gregset (regcache, gregsetp, mips_regnum (gdbarch)->pc);
192       mips_fill_gregset (regcache, gregsetp, mips_regnum (gdbarch)->badvaddr);
193       mips_fill_gregset (regcache, gregsetp, MIPS_PS_REGNUM);
194       mips_fill_gregset (regcache, gregsetp, mips_regnum (gdbarch)->cause);
195       mips_fill_gregset (regcache, gregsetp, MIPS_RESTART_REGNUM);
196       return;
197    }
198
199   if (regno > 0 && regno < 32)
200     {
201       dst = regp + regno + EF_REG0;
202       regcache_raw_collect (regcache, regno, dst);
203       return;
204     }
205
206   if (regno == mips_regnum (gdbarch)->lo)
207      regaddr = EF_LO;
208   else if (regno == mips_regnum (gdbarch)->hi)
209     regaddr = EF_HI;
210   else if (regno == mips_regnum (gdbarch)->pc)
211     regaddr = EF_CP0_EPC;
212   else if (regno == mips_regnum (gdbarch)->badvaddr)
213     regaddr = EF_CP0_BADVADDR;
214   else if (regno == MIPS_PS_REGNUM)
215     regaddr = EF_CP0_STATUS;
216   else if (regno == mips_regnum (gdbarch)->cause)
217     regaddr = EF_CP0_CAUSE;
218   else if (mips_linux_restart_reg_p (gdbarch)
219            && regno == MIPS_RESTART_REGNUM)
220     regaddr = EF_REG0;
221   else
222     regaddr = -1;
223
224   if (regaddr != -1)
225     {
226       dst = regp + regaddr;
227       regcache_raw_collect (regcache, regno, dst);
228     }
229 }
230
231 static void
232 mips_fill_gregset_wrapper (const struct regset *regset,
233                            const struct regcache *regcache,
234                            int regnum, void *gregs, size_t len)
235 {
236   gdb_assert (len == sizeof (mips_elf_gregset_t));
237
238   mips_fill_gregset (regcache, (mips_elf_gregset_t *)gregs, regnum);
239 }
240
241 /* Likewise, unpack an elf_fpregset_t.  */
242
243 void
244 mips_supply_fpregset (struct regcache *regcache,
245                       const mips_elf_fpregset_t *fpregsetp)
246 {
247   struct gdbarch *gdbarch = get_regcache_arch (regcache);
248   int regi;
249   char zerobuf[MAX_REGISTER_SIZE];
250
251   memset (zerobuf, 0, MAX_REGISTER_SIZE);
252
253   for (regi = 0; regi < 32; regi++)
254     regcache_raw_supply (regcache,
255                          gdbarch_fp0_regnum (gdbarch) + regi,
256                          *fpregsetp + regi);
257
258   regcache_raw_supply (regcache,
259                        mips_regnum (gdbarch)->fp_control_status,
260                        *fpregsetp + 32);
261
262   /* FIXME: how can we supply FCRIR?  The ABI doesn't tell us.  */
263   regcache_raw_supply (regcache,
264                        mips_regnum (gdbarch)->fp_implementation_revision,
265                        zerobuf);
266 }
267
268 static void
269 mips_supply_fpregset_wrapper (const struct regset *regset,
270                               struct regcache *regcache,
271                               int regnum, const void *gregs, size_t len)
272 {
273   gdb_assert (len == sizeof (mips_elf_fpregset_t));
274
275   mips_supply_fpregset (regcache, (const mips_elf_fpregset_t *)gregs);
276 }
277
278 /* Likewise, pack one or all floating point registers into an
279    elf_fpregset_t.  */
280
281 void
282 mips_fill_fpregset (const struct regcache *regcache,
283                     mips_elf_fpregset_t *fpregsetp, int regno)
284 {
285   struct gdbarch *gdbarch = get_regcache_arch (regcache);
286   char *to;
287
288   if ((regno >= gdbarch_fp0_regnum (gdbarch))
289       && (regno < gdbarch_fp0_regnum (gdbarch) + 32))
290     {
291       to = (char *) (*fpregsetp + regno - gdbarch_fp0_regnum (gdbarch));
292       regcache_raw_collect (regcache, regno, to);
293     }
294   else if (regno == mips_regnum (gdbarch)->fp_control_status)
295     {
296       to = (char *) (*fpregsetp + 32);
297       regcache_raw_collect (regcache, regno, to);
298     }
299   else if (regno == -1)
300     {
301       int regi;
302
303       for (regi = 0; regi < 32; regi++)
304         mips_fill_fpregset (regcache, fpregsetp,
305                             gdbarch_fp0_regnum (gdbarch) + regi);
306       mips_fill_fpregset (regcache, fpregsetp,
307                           mips_regnum (gdbarch)->fp_control_status);
308     }
309 }
310
311 static void
312 mips_fill_fpregset_wrapper (const struct regset *regset,
313                             const struct regcache *regcache,
314                             int regnum, void *gregs, size_t len)
315 {
316   gdb_assert (len == sizeof (mips_elf_fpregset_t));
317
318   mips_fill_fpregset (regcache, (mips_elf_fpregset_t *)gregs, regnum);
319 }
320
321 /* Support for 64-bit ABIs.  */
322
323 /* Figure out where the longjmp will land.
324    We expect the first arg to be a pointer to the jmp_buf structure
325    from which we extract the pc (MIPS_LINUX_JB_PC) that we will land
326    at.  The pc is copied into PC.  This routine returns 1 on
327    success.  */
328
329 /* Details about jmp_buf.  */
330
331 #define MIPS64_LINUX_JB_PC 0
332
333 static int
334 mips64_linux_get_longjmp_target (struct frame_info *frame, CORE_ADDR *pc)
335 {
336   CORE_ADDR jb_addr;
337   struct gdbarch *gdbarch = get_frame_arch (frame);
338   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
339   void *buf = alloca (gdbarch_ptr_bit (gdbarch) / TARGET_CHAR_BIT);
340   int element_size = gdbarch_ptr_bit (gdbarch) == 32 ? 4 : 8;
341
342   jb_addr = get_frame_register_unsigned (frame, MIPS_A0_REGNUM);
343
344   if (target_read_memory (jb_addr + MIPS64_LINUX_JB_PC * element_size,
345                           buf,
346                           gdbarch_ptr_bit (gdbarch) / TARGET_CHAR_BIT))
347     return 0;
348
349   *pc = extract_unsigned_integer (buf,
350                                   gdbarch_ptr_bit (gdbarch) / TARGET_CHAR_BIT,
351                                   byte_order);
352
353   return 1;
354 }
355
356 /* Register set support functions.  These operate on standard 64-bit
357    regsets, but work whether the target is 32-bit or 64-bit.  A 32-bit
358    target will still use the 64-bit format for PTRACE_GETREGS.  */
359
360 /* Supply a 64-bit register.  */
361
362 static void
363 supply_64bit_reg (struct regcache *regcache, int regnum,
364                   const gdb_byte *buf)
365 {
366   struct gdbarch *gdbarch = get_regcache_arch (regcache);
367   if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG
368       && register_size (gdbarch, regnum) == 4)
369     regcache_raw_supply (regcache, regnum, buf + 4);
370   else
371     regcache_raw_supply (regcache, regnum, buf);
372 }
373
374 /* Unpack a 64-bit elf_gregset_t into GDB's register cache.  */
375
376 void
377 mips64_supply_gregset (struct regcache *regcache,
378                        const mips64_elf_gregset_t *gregsetp)
379 {
380   int regi;
381   const mips64_elf_greg_t *regp = *gregsetp;
382   gdb_byte zerobuf[MAX_REGISTER_SIZE];
383   struct gdbarch *gdbarch = get_regcache_arch (regcache);
384
385   memset (zerobuf, 0, MAX_REGISTER_SIZE);
386
387   for (regi = MIPS64_EF_REG0 + 1; regi <= MIPS64_EF_REG31; regi++)
388     supply_64bit_reg (regcache, regi - MIPS64_EF_REG0,
389                       (const gdb_byte *) (regp + regi));
390
391   if (mips_linux_restart_reg_p (gdbarch))
392     supply_64bit_reg (regcache, MIPS_RESTART_REGNUM,
393                       (const gdb_byte *) (regp + MIPS64_EF_REG0));
394
395   supply_64bit_reg (regcache, mips_regnum (gdbarch)->lo,
396                     (const gdb_byte *) (regp + MIPS64_EF_LO));
397   supply_64bit_reg (regcache, mips_regnum (gdbarch)->hi,
398                     (const gdb_byte *) (regp + MIPS64_EF_HI));
399
400   supply_64bit_reg (regcache, mips_regnum (gdbarch)->pc,
401                     (const gdb_byte *) (regp + MIPS64_EF_CP0_EPC));
402   supply_64bit_reg (regcache, mips_regnum (gdbarch)->badvaddr,
403                     (const gdb_byte *) (regp + MIPS64_EF_CP0_BADVADDR));
404   supply_64bit_reg (regcache, MIPS_PS_REGNUM,
405                     (const gdb_byte *) (regp + MIPS64_EF_CP0_STATUS));
406   supply_64bit_reg (regcache, mips_regnum (gdbarch)->cause,
407                     (const gdb_byte *) (regp + MIPS64_EF_CP0_CAUSE));
408
409   /* Fill the inaccessible zero register with zero.  */
410   regcache_raw_supply (regcache, MIPS_ZERO_REGNUM, zerobuf);
411 }
412
413 static void
414 mips64_supply_gregset_wrapper (const struct regset *regset,
415                                struct regcache *regcache,
416                                int regnum, const void *gregs, size_t len)
417 {
418   gdb_assert (len == sizeof (mips64_elf_gregset_t));
419
420   mips64_supply_gregset (regcache, (const mips64_elf_gregset_t *)gregs);
421 }
422
423 /* Pack our registers (or one register) into a 64-bit elf_gregset_t.  */
424
425 void
426 mips64_fill_gregset (const struct regcache *regcache,
427                      mips64_elf_gregset_t *gregsetp, int regno)
428 {
429   struct gdbarch *gdbarch = get_regcache_arch (regcache);
430   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
431   int regaddr, regi;
432   mips64_elf_greg_t *regp = *gregsetp;
433   void *dst;
434
435   if (regno == -1)
436     {
437       memset (regp, 0, sizeof (mips64_elf_gregset_t));
438       for (regi = 1; regi < 32; regi++)
439         mips64_fill_gregset (regcache, gregsetp, regi);
440       mips64_fill_gregset (regcache, gregsetp, mips_regnum (gdbarch)->lo);
441       mips64_fill_gregset (regcache, gregsetp, mips_regnum (gdbarch)->hi);
442       mips64_fill_gregset (regcache, gregsetp, mips_regnum (gdbarch)->pc);
443       mips64_fill_gregset (regcache, gregsetp,
444                            mips_regnum (gdbarch)->badvaddr);
445       mips64_fill_gregset (regcache, gregsetp, MIPS_PS_REGNUM);
446       mips64_fill_gregset (regcache, gregsetp,  mips_regnum (gdbarch)->cause);
447       mips64_fill_gregset (regcache, gregsetp, MIPS_RESTART_REGNUM);
448       return;
449    }
450
451   if (regno > 0 && regno < 32)
452     regaddr = regno + MIPS64_EF_REG0;
453   else if (regno == mips_regnum (gdbarch)->lo)
454     regaddr = MIPS64_EF_LO;
455   else if (regno == mips_regnum (gdbarch)->hi)
456     regaddr = MIPS64_EF_HI;
457   else if (regno == mips_regnum (gdbarch)->pc)
458     regaddr = MIPS64_EF_CP0_EPC;
459   else if (regno == mips_regnum (gdbarch)->badvaddr)
460     regaddr = MIPS64_EF_CP0_BADVADDR;
461   else if (regno == MIPS_PS_REGNUM)
462     regaddr = MIPS64_EF_CP0_STATUS;
463   else if (regno == mips_regnum (gdbarch)->cause)
464     regaddr = MIPS64_EF_CP0_CAUSE;
465   else if (mips_linux_restart_reg_p (gdbarch)
466            && regno == MIPS_RESTART_REGNUM)
467     regaddr = MIPS64_EF_REG0;
468   else
469     regaddr = -1;
470
471   if (regaddr != -1)
472     {
473       gdb_byte buf[MAX_REGISTER_SIZE];
474       LONGEST val;
475
476       regcache_raw_collect (regcache, regno, buf);
477       val = extract_signed_integer (buf, register_size (gdbarch, regno),
478                                     byte_order);
479       dst = regp + regaddr;
480       store_signed_integer (dst, 8, byte_order, val);
481     }
482 }
483
484 static void
485 mips64_fill_gregset_wrapper (const struct regset *regset,
486                              const struct regcache *regcache,
487                              int regnum, void *gregs, size_t len)
488 {
489   gdb_assert (len == sizeof (mips64_elf_gregset_t));
490
491   mips64_fill_gregset (regcache, (mips64_elf_gregset_t *)gregs, regnum);
492 }
493
494 /* Likewise, unpack an elf_fpregset_t.  */
495
496 void
497 mips64_supply_fpregset (struct regcache *regcache,
498                         const mips64_elf_fpregset_t *fpregsetp)
499 {
500   struct gdbarch *gdbarch = get_regcache_arch (regcache);
501   int regi;
502
503   /* See mips_linux_o32_sigframe_init for a description of the
504      peculiar FP register layout.  */
505   if (register_size (gdbarch, gdbarch_fp0_regnum (gdbarch)) == 4)
506     for (regi = 0; regi < 32; regi++)
507       {
508         const gdb_byte *reg_ptr
509           = (const gdb_byte *) (*fpregsetp + (regi & ~1));
510         if ((gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG) != (regi & 1))
511           reg_ptr += 4;
512         regcache_raw_supply (regcache,
513                              gdbarch_fp0_regnum (gdbarch) + regi,
514                              reg_ptr);
515       }
516   else
517     for (regi = 0; regi < 32; regi++)
518       regcache_raw_supply (regcache,
519                            gdbarch_fp0_regnum (gdbarch) + regi,
520                            (const char *) (*fpregsetp + regi));
521
522   supply_32bit_reg (regcache, mips_regnum (gdbarch)->fp_control_status,
523                     (const gdb_byte *) (*fpregsetp + 32));
524
525   /* The ABI doesn't tell us how to supply FCRIR, and core dumps don't
526      include it - but the result of PTRACE_GETFPREGS does.  The best we
527      can do is to assume that its value is present.  */
528   supply_32bit_reg (regcache,
529                     mips_regnum (gdbarch)->fp_implementation_revision,
530                     (const gdb_byte *) (*fpregsetp + 32) + 4);
531 }
532
533 static void
534 mips64_supply_fpregset_wrapper (const struct regset *regset,
535                                 struct regcache *regcache,
536                                 int regnum, const void *gregs, size_t len)
537 {
538   gdb_assert (len == sizeof (mips64_elf_fpregset_t));
539
540   mips64_supply_fpregset (regcache, (const mips64_elf_fpregset_t *)gregs);
541 }
542
543 /* Likewise, pack one or all floating point registers into an
544    elf_fpregset_t.  */
545
546 void
547 mips64_fill_fpregset (const struct regcache *regcache,
548                       mips64_elf_fpregset_t *fpregsetp, int regno)
549 {
550   struct gdbarch *gdbarch = get_regcache_arch (regcache);
551   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
552   gdb_byte *to;
553
554   if ((regno >= gdbarch_fp0_regnum (gdbarch))
555       && (regno < gdbarch_fp0_regnum (gdbarch) + 32))
556     {
557       /* See mips_linux_o32_sigframe_init for a description of the
558          peculiar FP register layout.  */
559       if (register_size (gdbarch, regno) == 4)
560         {
561           int regi = regno - gdbarch_fp0_regnum (gdbarch);
562
563           to = (gdb_byte *) (*fpregsetp + (regi & ~1));
564           if ((gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG) != (regi & 1))
565             to += 4;
566           regcache_raw_collect (regcache, regno, to);
567         }
568       else
569         {
570           to = (gdb_byte *) (*fpregsetp + regno
571                              - gdbarch_fp0_regnum (gdbarch));
572           regcache_raw_collect (regcache, regno, to);
573         }
574     }
575   else if (regno == mips_regnum (gdbarch)->fp_control_status)
576     {
577       gdb_byte buf[MAX_REGISTER_SIZE];
578       LONGEST val;
579
580       regcache_raw_collect (regcache, regno, buf);
581       val = extract_signed_integer (buf, register_size (gdbarch, regno),
582                                     byte_order);
583       to = (gdb_byte *) (*fpregsetp + 32);
584       store_signed_integer (to, 4, byte_order, val);
585     }
586   else if (regno == mips_regnum (gdbarch)->fp_implementation_revision)
587     {
588       gdb_byte buf[MAX_REGISTER_SIZE];
589       LONGEST val;
590
591       regcache_raw_collect (regcache, regno, buf);
592       val = extract_signed_integer (buf, register_size (gdbarch, regno),
593                                     byte_order);
594       to = (gdb_byte *) (*fpregsetp + 32) + 4;
595       store_signed_integer (to, 4, byte_order, val);
596     }
597   else if (regno == -1)
598     {
599       int regi;
600
601       for (regi = 0; regi < 32; regi++)
602         mips64_fill_fpregset (regcache, fpregsetp,
603                               gdbarch_fp0_regnum (gdbarch) + regi);
604       mips64_fill_fpregset (regcache, fpregsetp,
605                             mips_regnum (gdbarch)->fp_control_status);
606       mips64_fill_fpregset (regcache, fpregsetp,
607                             mips_regnum (gdbarch)->fp_implementation_revision);
608     }
609 }
610
611 static void
612 mips64_fill_fpregset_wrapper (const struct regset *regset,
613                               const struct regcache *regcache,
614                               int regnum, void *gregs, size_t len)
615 {
616   gdb_assert (len == sizeof (mips64_elf_fpregset_t));
617
618   mips64_fill_fpregset (regcache, (mips64_elf_fpregset_t *)gregs, regnum);
619 }
620
621 static const struct regset mips_linux_gregset =
622   {
623     NULL, mips_supply_gregset_wrapper, mips_fill_gregset_wrapper
624   };
625
626 static const struct regset mips64_linux_gregset =
627   {
628     NULL, mips64_supply_gregset_wrapper, mips64_fill_gregset_wrapper
629   };
630
631 static const struct regset mips_linux_fpregset =
632   {
633     NULL, mips_supply_fpregset_wrapper, mips_fill_fpregset_wrapper
634   };
635
636 static const struct regset mips64_linux_fpregset =
637   {
638     NULL, mips64_supply_fpregset_wrapper, mips64_fill_fpregset_wrapper
639   };
640
641 static const struct regset *
642 mips_linux_regset_from_core_section (struct gdbarch *gdbarch,
643                                      const char *sect_name, size_t sect_size)
644 {
645   mips_elf_gregset_t gregset;
646   mips_elf_fpregset_t fpregset;
647   mips64_elf_gregset_t gregset64;
648   mips64_elf_fpregset_t fpregset64;
649
650   if (strcmp (sect_name, ".reg") == 0)
651     {
652       if (sect_size == sizeof (gregset))
653         return &mips_linux_gregset;
654       else if (sect_size == sizeof (gregset64))
655         return &mips64_linux_gregset;
656       else
657         {
658           warning (_("wrong size gregset struct in core file"));
659         }
660     }
661   else if (strcmp (sect_name, ".reg2") == 0)
662     {
663       if (sect_size == sizeof (fpregset))
664         return &mips_linux_fpregset;
665       else if (sect_size == sizeof (fpregset64))
666         return &mips64_linux_fpregset;
667       else
668         {
669           warning (_("wrong size fpregset struct in core file"));
670         }
671     }
672
673   return NULL;
674 }
675
676 static const struct target_desc *
677 mips_linux_core_read_description (struct gdbarch *gdbarch,
678                                   struct target_ops *target,
679                                   bfd *abfd)
680 {
681   asection *section = bfd_get_section_by_name (abfd, ".reg");
682   if (! section)
683     return NULL;
684
685   switch (bfd_section_size (abfd, section))
686     {
687     case sizeof (mips_elf_gregset_t):
688       return mips_tdesc_gp32;
689
690     case sizeof (mips64_elf_gregset_t):
691       return mips_tdesc_gp64;
692
693     default:
694       return NULL;
695     }
696 }
697
698
699 /* Check the code at PC for a dynamic linker lazy resolution stub.
700    GNU ld for MIPS has put lazy resolution stubs into a ".MIPS.stubs"
701    section uniformly since version 2.15.  If the pc is in that section,
702    then we are in such a stub.  Before that ".stub" was used in 32-bit
703    ELF binaries, however we do not bother checking for that since we
704    have never had and that case should be extremely rare these days.
705    Instead we pattern-match on the code generated by GNU ld.  They look
706    like this:
707
708    lw t9,0x8010(gp)
709    addu t7,ra
710    jalr t9,ra
711    addiu t8,zero,INDEX
712
713    (with the appropriate doubleword instructions for N64).  As any lazy
714    resolution stubs in microMIPS binaries will always be in a
715    ".MIPS.stubs" section we only ever verify standard MIPS patterns. */
716
717 static int
718 mips_linux_in_dynsym_stub (CORE_ADDR pc)
719 {
720   gdb_byte buf[28], *p;
721   ULONGEST insn, insn1;
722   int n64 = (mips_abi (target_gdbarch ()) == MIPS_ABI_N64);
723   enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
724
725   if (in_mips_stubs_section (pc))
726     return 1;
727
728   read_memory (pc - 12, buf, 28);
729
730   if (n64)
731     {
732       /* ld t9,0x8010(gp) */
733       insn1 = 0xdf998010;
734     }
735   else
736     {
737       /* lw t9,0x8010(gp) */
738       insn1 = 0x8f998010;
739     }
740
741   p = buf + 12;
742   while (p >= buf)
743     {
744       insn = extract_unsigned_integer (p, 4, byte_order);
745       if (insn == insn1)
746         break;
747       p -= 4;
748     }
749   if (p < buf)
750     return 0;
751
752   insn = extract_unsigned_integer (p + 4, 4, byte_order);
753   if (n64)
754     {
755       /* daddu t7,ra */
756       if (insn != 0x03e0782d)
757         return 0;
758     }
759   else
760     {
761       /* addu t7,ra */
762       if (insn != 0x03e07821)
763         return 0;
764     }
765
766   insn = extract_unsigned_integer (p + 8, 4, byte_order);
767   /* jalr t9,ra */
768   if (insn != 0x0320f809)
769     return 0;
770
771   insn = extract_unsigned_integer (p + 12, 4, byte_order);
772   if (n64)
773     {
774       /* daddiu t8,zero,0 */
775       if ((insn & 0xffff0000) != 0x64180000)
776         return 0;
777     }
778   else
779     {
780       /* addiu t8,zero,0 */
781       if ((insn & 0xffff0000) != 0x24180000)
782         return 0;
783     }
784
785   return 1;
786 }
787
788 /* Return non-zero iff PC belongs to the dynamic linker resolution
789    code, a PLT entry, or a lazy binding stub.  */
790
791 static int
792 mips_linux_in_dynsym_resolve_code (CORE_ADDR pc)
793 {
794   /* Check whether PC is in the dynamic linker.  This also checks
795      whether it is in the .plt section, used by non-PIC executables.  */
796   if (svr4_in_dynsym_resolve_code (pc))
797     return 1;
798
799   /* Likewise for the stubs.  They live in the .MIPS.stubs section these
800      days, so we check if the PC is within, than fall back to a pattern
801      match.  */
802   if (mips_linux_in_dynsym_stub (pc))
803     return 1;
804
805   return 0;
806 }
807
808 /* See the comments for SKIP_SOLIB_RESOLVER at the top of infrun.c,
809    and glibc_skip_solib_resolver in glibc-tdep.c.  The normal glibc
810    implementation of this triggers at "fixup" from the same objfile as
811    "_dl_runtime_resolve"; MIPS GNU/Linux can trigger at
812    "__dl_runtime_resolve" directly.  An unresolved lazy binding
813    stub will point to _dl_runtime_resolve, which will first call
814    __dl_runtime_resolve, and then pass control to the resolved
815    function.  */
816
817 static CORE_ADDR
818 mips_linux_skip_resolver (struct gdbarch *gdbarch, CORE_ADDR pc)
819 {
820   struct bound_minimal_symbol resolver;
821
822   resolver = lookup_minimal_symbol ("__dl_runtime_resolve", NULL, NULL);
823
824   if (resolver.minsym && BMSYMBOL_VALUE_ADDRESS (resolver) == pc)
825     return frame_unwind_caller_pc (get_current_frame ());
826
827   return glibc_skip_solib_resolver (gdbarch, pc);
828 }
829
830 /* Signal trampoline support.  There are four supported layouts for a
831    signal frame: o32 sigframe, o32 rt_sigframe, n32 rt_sigframe, and
832    n64 rt_sigframe.  We handle them all independently; not the most
833    efficient way, but simplest.  First, declare all the unwinders.  */
834
835 static void mips_linux_o32_sigframe_init (const struct tramp_frame *self,
836                                           struct frame_info *this_frame,
837                                           struct trad_frame_cache *this_cache,
838                                           CORE_ADDR func);
839
840 static void mips_linux_n32n64_sigframe_init (const struct tramp_frame *self,
841                                              struct frame_info *this_frame,
842                                              struct trad_frame_cache *this_cache,
843                                              CORE_ADDR func);
844
845 #define MIPS_NR_LINUX 4000
846 #define MIPS_NR_N64_LINUX 5000
847 #define MIPS_NR_N32_LINUX 6000
848
849 #define MIPS_NR_sigreturn MIPS_NR_LINUX + 119
850 #define MIPS_NR_rt_sigreturn MIPS_NR_LINUX + 193
851 #define MIPS_NR_N64_rt_sigreturn MIPS_NR_N64_LINUX + 211
852 #define MIPS_NR_N32_rt_sigreturn MIPS_NR_N32_LINUX + 211
853
854 #define MIPS_INST_LI_V0_SIGRETURN 0x24020000 + MIPS_NR_sigreturn
855 #define MIPS_INST_LI_V0_RT_SIGRETURN 0x24020000 + MIPS_NR_rt_sigreturn
856 #define MIPS_INST_LI_V0_N64_RT_SIGRETURN 0x24020000 + MIPS_NR_N64_rt_sigreturn
857 #define MIPS_INST_LI_V0_N32_RT_SIGRETURN 0x24020000 + MIPS_NR_N32_rt_sigreturn
858 #define MIPS_INST_SYSCALL 0x0000000c
859
860 static const struct tramp_frame mips_linux_o32_sigframe = {
861   SIGTRAMP_FRAME,
862   4,
863   {
864     { MIPS_INST_LI_V0_SIGRETURN, -1 },
865     { MIPS_INST_SYSCALL, -1 },
866     { TRAMP_SENTINEL_INSN, -1 }
867   },
868   mips_linux_o32_sigframe_init
869 };
870
871 static const struct tramp_frame mips_linux_o32_rt_sigframe = {
872   SIGTRAMP_FRAME,
873   4,
874   {
875     { MIPS_INST_LI_V0_RT_SIGRETURN, -1 },
876     { MIPS_INST_SYSCALL, -1 },
877     { TRAMP_SENTINEL_INSN, -1 } },
878   mips_linux_o32_sigframe_init
879 };
880
881 static const struct tramp_frame mips_linux_n32_rt_sigframe = {
882   SIGTRAMP_FRAME,
883   4,
884   {
885     { MIPS_INST_LI_V0_N32_RT_SIGRETURN, -1 },
886     { MIPS_INST_SYSCALL, -1 },
887     { TRAMP_SENTINEL_INSN, -1 }
888   },
889   mips_linux_n32n64_sigframe_init
890 };
891
892 static const struct tramp_frame mips_linux_n64_rt_sigframe = {
893   SIGTRAMP_FRAME,
894   4,
895   {
896     { MIPS_INST_LI_V0_N64_RT_SIGRETURN, -1 },
897     { MIPS_INST_SYSCALL, -1 },
898     { TRAMP_SENTINEL_INSN, -1 }
899   },
900   mips_linux_n32n64_sigframe_init
901 };
902
903 /* *INDENT-OFF* */
904 /* The unwinder for o32 signal frames.  The legacy structures look
905    like this:
906
907    struct sigframe {
908      u32 sf_ass[4];            [argument save space for o32]
909      u32 sf_code[2];           [signal trampoline or fill]
910      struct sigcontext sf_sc;
911      sigset_t sf_mask;
912    };
913
914    Pre-2.6.12 sigcontext:
915
916    struct sigcontext {
917         unsigned int       sc_regmask;          [Unused]
918         unsigned int       sc_status;
919         unsigned long long sc_pc;
920         unsigned long long sc_regs[32];
921         unsigned long long sc_fpregs[32];
922         unsigned int       sc_ownedfp;
923         unsigned int       sc_fpc_csr;
924         unsigned int       sc_fpc_eir;          [Unused]
925         unsigned int       sc_used_math;
926         unsigned int       sc_ssflags;          [Unused]
927         [Alignment hole of four bytes]
928         unsigned long long sc_mdhi;
929         unsigned long long sc_mdlo;
930
931         unsigned int       sc_cause;            [Unused]
932         unsigned int       sc_badvaddr;         [Unused]
933
934         unsigned long      sc_sigset[4];        [kernel's sigset_t]
935    };
936
937    Post-2.6.12 sigcontext (SmartMIPS/DSP support added):
938
939    struct sigcontext {
940         unsigned int       sc_regmask;          [Unused]
941         unsigned int       sc_status;           [Unused]
942         unsigned long long sc_pc;
943         unsigned long long sc_regs[32];
944         unsigned long long sc_fpregs[32];
945         unsigned int       sc_acx;
946         unsigned int       sc_fpc_csr;
947         unsigned int       sc_fpc_eir;          [Unused]
948         unsigned int       sc_used_math;
949         unsigned int       sc_dsp;
950         [Alignment hole of four bytes]
951         unsigned long long sc_mdhi;
952         unsigned long long sc_mdlo;
953         unsigned long      sc_hi1;
954         unsigned long      sc_lo1;
955         unsigned long      sc_hi2;
956         unsigned long      sc_lo2;
957         unsigned long      sc_hi3;
958         unsigned long      sc_lo3;
959    };
960
961    The RT signal frames look like this:
962
963    struct rt_sigframe {
964      u32 rs_ass[4];            [argument save space for o32]
965      u32 rs_code[2]            [signal trampoline or fill]
966      struct siginfo rs_info;
967      struct ucontext rs_uc;
968    };
969
970    struct ucontext {
971      unsigned long     uc_flags;
972      struct ucontext  *uc_link;
973      stack_t           uc_stack;
974      [Alignment hole of four bytes]
975      struct sigcontext uc_mcontext;
976      sigset_t          uc_sigmask;
977    };  */
978 /* *INDENT-ON* */
979
980 #define SIGFRAME_SIGCONTEXT_OFFSET   (6 * 4)
981
982 #define RTSIGFRAME_SIGINFO_SIZE      128
983 #define STACK_T_SIZE                 (3 * 4)
984 #define UCONTEXT_SIGCONTEXT_OFFSET   (2 * 4 + STACK_T_SIZE + 4)
985 #define RTSIGFRAME_SIGCONTEXT_OFFSET (SIGFRAME_SIGCONTEXT_OFFSET \
986                                       + RTSIGFRAME_SIGINFO_SIZE \
987                                       + UCONTEXT_SIGCONTEXT_OFFSET)
988
989 #define SIGCONTEXT_PC       (1 * 8)
990 #define SIGCONTEXT_REGS     (2 * 8)
991 #define SIGCONTEXT_FPREGS   (34 * 8)
992 #define SIGCONTEXT_FPCSR    (66 * 8 + 4)
993 #define SIGCONTEXT_DSPCTL   (68 * 8 + 0)
994 #define SIGCONTEXT_HI       (69 * 8)
995 #define SIGCONTEXT_LO       (70 * 8)
996 #define SIGCONTEXT_CAUSE    (71 * 8 + 0)
997 #define SIGCONTEXT_BADVADDR (71 * 8 + 4)
998 #define SIGCONTEXT_HI1      (71 * 8 + 0)
999 #define SIGCONTEXT_LO1      (71 * 8 + 4)
1000 #define SIGCONTEXT_HI2      (72 * 8 + 0)
1001 #define SIGCONTEXT_LO2      (72 * 8 + 4)
1002 #define SIGCONTEXT_HI3      (73 * 8 + 0)
1003 #define SIGCONTEXT_LO3      (73 * 8 + 4)
1004
1005 #define SIGCONTEXT_REG_SIZE 8
1006
1007 static void
1008 mips_linux_o32_sigframe_init (const struct tramp_frame *self,
1009                               struct frame_info *this_frame,
1010                               struct trad_frame_cache *this_cache,
1011                               CORE_ADDR func)
1012 {
1013   struct gdbarch *gdbarch = get_frame_arch (this_frame);
1014   int ireg;
1015   CORE_ADDR frame_sp = get_frame_sp (this_frame);
1016   CORE_ADDR sigcontext_base;
1017   const struct mips_regnum *regs = mips_regnum (gdbarch);
1018   CORE_ADDR regs_base;
1019
1020   if (self == &mips_linux_o32_sigframe)
1021     sigcontext_base = frame_sp + SIGFRAME_SIGCONTEXT_OFFSET;
1022   else
1023     sigcontext_base = frame_sp + RTSIGFRAME_SIGCONTEXT_OFFSET;
1024
1025   /* I'm not proud of this hack.  Eventually we will have the
1026      infrastructure to indicate the size of saved registers on a
1027      per-frame basis, but right now we don't; the kernel saves eight
1028      bytes but we only want four.  Use regs_base to access any
1029      64-bit fields.  */
1030   if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
1031     regs_base = sigcontext_base + 4;
1032   else
1033     regs_base = sigcontext_base;
1034
1035   if (mips_linux_restart_reg_p (gdbarch))
1036     trad_frame_set_reg_addr (this_cache,
1037                              (MIPS_RESTART_REGNUM
1038                               + gdbarch_num_regs (gdbarch)),
1039                              regs_base + SIGCONTEXT_REGS);
1040
1041   for (ireg = 1; ireg < 32; ireg++)
1042     trad_frame_set_reg_addr (this_cache,
1043                              (ireg + MIPS_ZERO_REGNUM
1044                               + gdbarch_num_regs (gdbarch)),
1045                              (regs_base + SIGCONTEXT_REGS
1046                               + ireg * SIGCONTEXT_REG_SIZE));
1047
1048   /* The way that floating point registers are saved, unfortunately,
1049      depends on the architecture the kernel is built for.  For the r3000 and
1050      tx39, four bytes of each register are at the beginning of each of the
1051      32 eight byte slots.  For everything else, the registers are saved
1052      using double precision; only the even-numbered slots are initialized,
1053      and the high bits are the odd-numbered register.  Assume the latter
1054      layout, since we can't tell, and it's much more common.  Which bits are
1055      the "high" bits depends on endianness.  */
1056   for (ireg = 0; ireg < 32; ireg++)
1057     if ((gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG) != (ireg & 1))
1058       trad_frame_set_reg_addr (this_cache,
1059                                ireg + regs->fp0 + gdbarch_num_regs (gdbarch),
1060                                (sigcontext_base + SIGCONTEXT_FPREGS + 4
1061                                 + (ireg & ~1) * SIGCONTEXT_REG_SIZE));
1062     else
1063       trad_frame_set_reg_addr (this_cache,
1064                                ireg + regs->fp0 + gdbarch_num_regs (gdbarch),
1065                                (sigcontext_base + SIGCONTEXT_FPREGS
1066                                 + (ireg & ~1) * SIGCONTEXT_REG_SIZE));
1067
1068   trad_frame_set_reg_addr (this_cache,
1069                            regs->pc + gdbarch_num_regs (gdbarch),
1070                            regs_base + SIGCONTEXT_PC);
1071
1072   trad_frame_set_reg_addr (this_cache,
1073                            (regs->fp_control_status
1074                             + gdbarch_num_regs (gdbarch)),
1075                            sigcontext_base + SIGCONTEXT_FPCSR);
1076
1077   if (regs->dspctl != -1)
1078     trad_frame_set_reg_addr (this_cache,
1079                              regs->dspctl + gdbarch_num_regs (gdbarch),
1080                              sigcontext_base + SIGCONTEXT_DSPCTL);
1081
1082   trad_frame_set_reg_addr (this_cache,
1083                            regs->hi + gdbarch_num_regs (gdbarch),
1084                            regs_base + SIGCONTEXT_HI);
1085   trad_frame_set_reg_addr (this_cache,
1086                            regs->lo + gdbarch_num_regs (gdbarch),
1087                            regs_base + SIGCONTEXT_LO);
1088
1089   if (regs->dspacc != -1)
1090     {
1091       trad_frame_set_reg_addr (this_cache,
1092                                regs->dspacc + 0 + gdbarch_num_regs (gdbarch),
1093                                sigcontext_base + SIGCONTEXT_HI1);
1094       trad_frame_set_reg_addr (this_cache,
1095                                regs->dspacc + 1 + gdbarch_num_regs (gdbarch),
1096                                sigcontext_base + SIGCONTEXT_LO1);
1097       trad_frame_set_reg_addr (this_cache,
1098                                regs->dspacc + 2 + gdbarch_num_regs (gdbarch),
1099                                sigcontext_base + SIGCONTEXT_HI2);
1100       trad_frame_set_reg_addr (this_cache,
1101                                regs->dspacc + 3 + gdbarch_num_regs (gdbarch),
1102                                sigcontext_base + SIGCONTEXT_LO2);
1103       trad_frame_set_reg_addr (this_cache,
1104                                regs->dspacc + 4 + gdbarch_num_regs (gdbarch),
1105                                sigcontext_base + SIGCONTEXT_HI3);
1106       trad_frame_set_reg_addr (this_cache,
1107                                regs->dspacc + 5 + gdbarch_num_regs (gdbarch),
1108                                sigcontext_base + SIGCONTEXT_LO3);
1109     }
1110   else
1111     {
1112       trad_frame_set_reg_addr (this_cache,
1113                                regs->cause + gdbarch_num_regs (gdbarch),
1114                                sigcontext_base + SIGCONTEXT_CAUSE);
1115       trad_frame_set_reg_addr (this_cache,
1116                                regs->badvaddr + gdbarch_num_regs (gdbarch),
1117                                sigcontext_base + SIGCONTEXT_BADVADDR);
1118     }
1119
1120   /* Choice of the bottom of the sigframe is somewhat arbitrary.  */
1121   trad_frame_set_id (this_cache, frame_id_build (frame_sp, func));
1122 }
1123
1124 /* *INDENT-OFF* */
1125 /* For N32/N64 things look different.  There is no non-rt signal frame.
1126
1127   struct rt_sigframe_n32 {
1128     u32 rs_ass[4];                  [ argument save space for o32 ]
1129     u32 rs_code[2];                 [ signal trampoline or fill ]
1130     struct siginfo rs_info;
1131     struct ucontextn32 rs_uc;
1132   };
1133
1134   struct ucontextn32 {
1135     u32                 uc_flags;
1136     s32                 uc_link;
1137     stack32_t           uc_stack;
1138     struct sigcontext   uc_mcontext;
1139     sigset_t            uc_sigmask;   [ mask last for extensibility ]
1140   };
1141
1142   struct rt_sigframe {
1143     u32 rs_ass[4];                  [ argument save space for o32 ]
1144     u32 rs_code[2];                 [ signal trampoline ]
1145     struct siginfo rs_info;
1146     struct ucontext rs_uc;
1147   };
1148
1149   struct ucontext {
1150     unsigned long     uc_flags;
1151     struct ucontext  *uc_link;
1152     stack_t           uc_stack;
1153     struct sigcontext uc_mcontext;
1154     sigset_t          uc_sigmask;   [ mask last for extensibility ]
1155   };
1156
1157   And the sigcontext is different (this is for both n32 and n64):
1158
1159   struct sigcontext {
1160     unsigned long long sc_regs[32];
1161     unsigned long long sc_fpregs[32];
1162     unsigned long long sc_mdhi;
1163     unsigned long long sc_hi1;
1164     unsigned long long sc_hi2;
1165     unsigned long long sc_hi3;
1166     unsigned long long sc_mdlo;
1167     unsigned long long sc_lo1;
1168     unsigned long long sc_lo2;
1169     unsigned long long sc_lo3;
1170     unsigned long long sc_pc;
1171     unsigned int       sc_fpc_csr;
1172     unsigned int       sc_used_math;
1173     unsigned int       sc_dsp;
1174     unsigned int       sc_reserved;
1175   };
1176
1177   That is the post-2.6.12 definition of the 64-bit sigcontext; before
1178   then, there were no hi1-hi3 or lo1-lo3.  Cause and badvaddr were
1179   included too.  */
1180 /* *INDENT-ON* */
1181
1182 #define N32_STACK_T_SIZE                STACK_T_SIZE
1183 #define N64_STACK_T_SIZE                (2 * 8 + 4)
1184 #define N32_UCONTEXT_SIGCONTEXT_OFFSET  (2 * 4 + N32_STACK_T_SIZE + 4)
1185 #define N64_UCONTEXT_SIGCONTEXT_OFFSET  (2 * 8 + N64_STACK_T_SIZE + 4)
1186 #define N32_SIGFRAME_SIGCONTEXT_OFFSET  (SIGFRAME_SIGCONTEXT_OFFSET \
1187                                          + RTSIGFRAME_SIGINFO_SIZE \
1188                                          + N32_UCONTEXT_SIGCONTEXT_OFFSET)
1189 #define N64_SIGFRAME_SIGCONTEXT_OFFSET  (SIGFRAME_SIGCONTEXT_OFFSET \
1190                                          + RTSIGFRAME_SIGINFO_SIZE \
1191                                          + N64_UCONTEXT_SIGCONTEXT_OFFSET)
1192
1193 #define N64_SIGCONTEXT_REGS     (0 * 8)
1194 #define N64_SIGCONTEXT_FPREGS   (32 * 8)
1195 #define N64_SIGCONTEXT_HI       (64 * 8)
1196 #define N64_SIGCONTEXT_HI1      (65 * 8)
1197 #define N64_SIGCONTEXT_HI2      (66 * 8)
1198 #define N64_SIGCONTEXT_HI3      (67 * 8)
1199 #define N64_SIGCONTEXT_LO       (68 * 8)
1200 #define N64_SIGCONTEXT_LO1      (69 * 8)
1201 #define N64_SIGCONTEXT_LO2      (70 * 8)
1202 #define N64_SIGCONTEXT_LO3      (71 * 8)
1203 #define N64_SIGCONTEXT_PC       (72 * 8)
1204 #define N64_SIGCONTEXT_FPCSR    (73 * 8 + 0)
1205 #define N64_SIGCONTEXT_DSPCTL   (74 * 8 + 0)
1206
1207 #define N64_SIGCONTEXT_REG_SIZE 8
1208
1209 static void
1210 mips_linux_n32n64_sigframe_init (const struct tramp_frame *self,
1211                                  struct frame_info *this_frame,
1212                                  struct trad_frame_cache *this_cache,
1213                                  CORE_ADDR func)
1214 {
1215   struct gdbarch *gdbarch = get_frame_arch (this_frame);
1216   int ireg;
1217   CORE_ADDR frame_sp = get_frame_sp (this_frame);
1218   CORE_ADDR sigcontext_base;
1219   const struct mips_regnum *regs = mips_regnum (gdbarch);
1220
1221   if (self == &mips_linux_n32_rt_sigframe)
1222     sigcontext_base = frame_sp + N32_SIGFRAME_SIGCONTEXT_OFFSET;
1223   else
1224     sigcontext_base = frame_sp + N64_SIGFRAME_SIGCONTEXT_OFFSET;
1225
1226   if (mips_linux_restart_reg_p (gdbarch))
1227     trad_frame_set_reg_addr (this_cache,
1228                              (MIPS_RESTART_REGNUM
1229                               + gdbarch_num_regs (gdbarch)),
1230                              sigcontext_base + N64_SIGCONTEXT_REGS);
1231
1232   for (ireg = 1; ireg < 32; ireg++)
1233     trad_frame_set_reg_addr (this_cache,
1234                              (ireg + MIPS_ZERO_REGNUM
1235                               + gdbarch_num_regs (gdbarch)),
1236                              (sigcontext_base + N64_SIGCONTEXT_REGS
1237                               + ireg * N64_SIGCONTEXT_REG_SIZE));
1238
1239   for (ireg = 0; ireg < 32; ireg++)
1240     trad_frame_set_reg_addr (this_cache,
1241                              ireg + regs->fp0 + gdbarch_num_regs (gdbarch),
1242                              (sigcontext_base + N64_SIGCONTEXT_FPREGS
1243                               + ireg * N64_SIGCONTEXT_REG_SIZE));
1244
1245   trad_frame_set_reg_addr (this_cache,
1246                            regs->pc + gdbarch_num_regs (gdbarch),
1247                            sigcontext_base + N64_SIGCONTEXT_PC);
1248
1249   trad_frame_set_reg_addr (this_cache,
1250                            (regs->fp_control_status
1251                             + gdbarch_num_regs (gdbarch)),
1252                            sigcontext_base + N64_SIGCONTEXT_FPCSR);
1253
1254   trad_frame_set_reg_addr (this_cache,
1255                            regs->hi + gdbarch_num_regs (gdbarch),
1256                            sigcontext_base + N64_SIGCONTEXT_HI);
1257   trad_frame_set_reg_addr (this_cache,
1258                            regs->lo + gdbarch_num_regs (gdbarch),
1259                            sigcontext_base + N64_SIGCONTEXT_LO);
1260
1261   if (regs->dspacc != -1)
1262     {
1263       trad_frame_set_reg_addr (this_cache,
1264                                regs->dspacc + 0 + gdbarch_num_regs (gdbarch),
1265                                sigcontext_base + N64_SIGCONTEXT_HI1);
1266       trad_frame_set_reg_addr (this_cache,
1267                                regs->dspacc + 1 + gdbarch_num_regs (gdbarch),
1268                                sigcontext_base + N64_SIGCONTEXT_LO1);
1269       trad_frame_set_reg_addr (this_cache,
1270                                regs->dspacc + 2 + gdbarch_num_regs (gdbarch),
1271                                sigcontext_base + N64_SIGCONTEXT_HI2);
1272       trad_frame_set_reg_addr (this_cache,
1273                                regs->dspacc + 3 + gdbarch_num_regs (gdbarch),
1274                                sigcontext_base + N64_SIGCONTEXT_LO2);
1275       trad_frame_set_reg_addr (this_cache,
1276                                regs->dspacc + 4 + gdbarch_num_regs (gdbarch),
1277                                sigcontext_base + N64_SIGCONTEXT_HI3);
1278       trad_frame_set_reg_addr (this_cache,
1279                                regs->dspacc + 5 + gdbarch_num_regs (gdbarch),
1280                                sigcontext_base + N64_SIGCONTEXT_LO3);
1281     }
1282   if (regs->dspctl != -1)
1283     trad_frame_set_reg_addr (this_cache,
1284                              regs->dspctl + gdbarch_num_regs (gdbarch),
1285                              sigcontext_base + N64_SIGCONTEXT_DSPCTL);
1286
1287   /* Choice of the bottom of the sigframe is somewhat arbitrary.  */
1288   trad_frame_set_id (this_cache, frame_id_build (frame_sp, func));
1289 }
1290
1291 /* Implement the "write_pc" gdbarch method.  */
1292
1293 static void
1294 mips_linux_write_pc (struct regcache *regcache, CORE_ADDR pc)
1295 {
1296   struct gdbarch *gdbarch = get_regcache_arch (regcache);
1297
1298   mips_write_pc (regcache, pc);
1299
1300   /* Clear the syscall restart flag.  */
1301   if (mips_linux_restart_reg_p (gdbarch))
1302     regcache_cooked_write_unsigned (regcache, MIPS_RESTART_REGNUM, 0);
1303 }
1304
1305 /* Return 1 if MIPS_RESTART_REGNUM is usable.  */
1306
1307 int
1308 mips_linux_restart_reg_p (struct gdbarch *gdbarch)
1309 {
1310   /* If we do not have a target description with registers, then
1311      MIPS_RESTART_REGNUM will not be included in the register set.  */
1312   if (!tdesc_has_registers (gdbarch_target_desc (gdbarch)))
1313     return 0;
1314
1315   /* If we do, then MIPS_RESTART_REGNUM is safe to check; it will
1316      either be GPR-sized or missing.  */
1317   return register_size (gdbarch, MIPS_RESTART_REGNUM) > 0;
1318 }
1319
1320 /* When FRAME is at a syscall instruction, return the PC of the next
1321    instruction to be executed.  */
1322
1323 static CORE_ADDR
1324 mips_linux_syscall_next_pc (struct frame_info *frame)
1325 {
1326   CORE_ADDR pc = get_frame_pc (frame);
1327   ULONGEST v0 = get_frame_register_unsigned (frame, MIPS_V0_REGNUM);
1328
1329   /* If we are about to make a sigreturn syscall, use the unwinder to
1330      decode the signal frame.  */
1331   if (v0 == MIPS_NR_sigreturn
1332       || v0 == MIPS_NR_rt_sigreturn
1333       || v0 == MIPS_NR_N64_rt_sigreturn
1334       || v0 == MIPS_NR_N32_rt_sigreturn)
1335     return frame_unwind_caller_pc (get_current_frame ());
1336
1337   return pc + 4;
1338 }
1339
1340 /* Return the current system call's number present in the
1341    v0 register.  When the function fails, it returns -1.  */
1342
1343 static LONGEST
1344 mips_linux_get_syscall_number (struct gdbarch *gdbarch,
1345                                ptid_t ptid)
1346 {
1347   struct regcache *regcache = get_thread_regcache (ptid);
1348   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1349   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1350   int regsize = register_size (gdbarch, MIPS_V0_REGNUM);
1351   /* The content of a register */
1352   gdb_byte buf[8];
1353   /* The result */
1354   LONGEST ret;
1355
1356   /* Make sure we're in a known ABI */
1357   gdb_assert (tdep->mips_abi == MIPS_ABI_O32
1358               || tdep->mips_abi == MIPS_ABI_N32
1359               || tdep->mips_abi == MIPS_ABI_N64);
1360
1361   gdb_assert (regsize <= sizeof (buf));
1362
1363   /* Getting the system call number from the register.
1364      syscall number is in v0 or $2.  */
1365   regcache_cooked_read (regcache, MIPS_V0_REGNUM, buf);
1366
1367   ret = extract_signed_integer (buf, regsize, byte_order);
1368
1369   return ret;
1370 }
1371
1372 /* Implementation of `gdbarch_gdb_signal_to_target', as defined in
1373    gdbarch.h.  */
1374
1375 static int
1376 mips_gdb_signal_to_target (struct gdbarch *gdbarch,
1377                            enum gdb_signal signal)
1378 {
1379   switch (signal)
1380     {
1381     case GDB_SIGNAL_EMT:
1382       return MIPS_LINUX_SIGEMT;
1383
1384     case GDB_SIGNAL_BUS:
1385       return MIPS_LINUX_SIGBUS;
1386
1387     case GDB_SIGNAL_SYS:
1388       return MIPS_LINUX_SIGSYS;
1389
1390     case GDB_SIGNAL_USR1:
1391       return MIPS_LINUX_SIGUSR1;
1392
1393     case GDB_SIGNAL_USR2:
1394       return MIPS_LINUX_SIGUSR2;
1395
1396     case GDB_SIGNAL_CHLD:
1397       return MIPS_LINUX_SIGCHLD;
1398
1399     case GDB_SIGNAL_PWR:
1400       return MIPS_LINUX_SIGPWR;
1401
1402     case GDB_SIGNAL_WINCH:
1403       return MIPS_LINUX_SIGWINCH;
1404
1405     case GDB_SIGNAL_URG:
1406       return MIPS_LINUX_SIGURG;
1407
1408     case GDB_SIGNAL_IO:
1409       return MIPS_LINUX_SIGIO;
1410
1411     case GDB_SIGNAL_POLL:
1412       return MIPS_LINUX_SIGPOLL;
1413
1414     case GDB_SIGNAL_STOP:
1415       return MIPS_LINUX_SIGSTOP;
1416
1417     case GDB_SIGNAL_TSTP:
1418       return MIPS_LINUX_SIGTSTP;
1419
1420     case GDB_SIGNAL_CONT:
1421       return MIPS_LINUX_SIGCONT;
1422
1423     case GDB_SIGNAL_TTIN:
1424       return MIPS_LINUX_SIGTTIN;
1425
1426     case GDB_SIGNAL_TTOU:
1427       return MIPS_LINUX_SIGTTOU;
1428
1429     case GDB_SIGNAL_VTALRM:
1430       return MIPS_LINUX_SIGVTALRM;
1431
1432     case GDB_SIGNAL_PROF:
1433       return MIPS_LINUX_SIGPROF;
1434
1435     case GDB_SIGNAL_XCPU:
1436       return MIPS_LINUX_SIGXCPU;
1437
1438     case GDB_SIGNAL_XFSZ:
1439       return MIPS_LINUX_SIGXFSZ;
1440
1441     /* GDB_SIGNAL_REALTIME_32 is not continuous in <gdb/signals.def>,
1442        therefore we have to handle it here.  */
1443     case GDB_SIGNAL_REALTIME_32:
1444       return MIPS_LINUX_SIGRTMIN;
1445     }
1446
1447   if (signal >= GDB_SIGNAL_REALTIME_33
1448       && signal <= GDB_SIGNAL_REALTIME_63)
1449     {
1450       int offset = signal - GDB_SIGNAL_REALTIME_33;
1451
1452       return MIPS_LINUX_SIGRTMIN + 1 + offset;
1453     }
1454   else if (signal >= GDB_SIGNAL_REALTIME_64
1455            && signal <= GDB_SIGNAL_REALTIME_127)
1456     {
1457       int offset = signal - GDB_SIGNAL_REALTIME_64;
1458
1459       return MIPS_LINUX_SIGRT64 + offset;
1460     }
1461
1462   return linux_gdb_signal_to_target (gdbarch, signal);
1463 }
1464
1465 /* Translate signals based on MIPS signal values.
1466    Adapted from gdb/common/signals.c.  */
1467
1468 static enum gdb_signal
1469 mips_gdb_signal_from_target (struct gdbarch *gdbarch, int signal)
1470 {
1471   switch (signal)
1472     {
1473     case MIPS_LINUX_SIGEMT:
1474       return GDB_SIGNAL_EMT;
1475
1476     case MIPS_LINUX_SIGBUS:
1477       return GDB_SIGNAL_BUS;
1478
1479     case MIPS_LINUX_SIGSYS:
1480       return GDB_SIGNAL_SYS;
1481
1482     case MIPS_LINUX_SIGUSR1:
1483       return GDB_SIGNAL_USR1;
1484
1485     case MIPS_LINUX_SIGUSR2:
1486       return GDB_SIGNAL_USR2;
1487
1488     case MIPS_LINUX_SIGCHLD:
1489       return GDB_SIGNAL_CHLD;
1490
1491     case MIPS_LINUX_SIGPWR:
1492       return GDB_SIGNAL_PWR;
1493
1494     case MIPS_LINUX_SIGWINCH:
1495       return GDB_SIGNAL_WINCH;
1496
1497     case MIPS_LINUX_SIGURG:
1498       return GDB_SIGNAL_URG;
1499
1500     /* No way to differentiate between SIGIO and SIGPOLL.
1501        Therefore, we just handle the first one.  */
1502     case MIPS_LINUX_SIGIO:
1503       return GDB_SIGNAL_IO;
1504
1505     case MIPS_LINUX_SIGSTOP:
1506       return GDB_SIGNAL_STOP;
1507
1508     case MIPS_LINUX_SIGTSTP:
1509       return GDB_SIGNAL_TSTP;
1510
1511     case MIPS_LINUX_SIGCONT:
1512       return GDB_SIGNAL_CONT;
1513
1514     case MIPS_LINUX_SIGTTIN:
1515       return GDB_SIGNAL_TTIN;
1516
1517     case MIPS_LINUX_SIGTTOU:
1518       return GDB_SIGNAL_TTOU;
1519
1520     case MIPS_LINUX_SIGVTALRM:
1521       return GDB_SIGNAL_VTALRM;
1522
1523     case MIPS_LINUX_SIGPROF:
1524       return GDB_SIGNAL_PROF;
1525
1526     case MIPS_LINUX_SIGXCPU:
1527       return GDB_SIGNAL_XCPU;
1528
1529     case MIPS_LINUX_SIGXFSZ:
1530       return GDB_SIGNAL_XFSZ;
1531     }
1532
1533   if (signal >= MIPS_LINUX_SIGRTMIN && signal <= MIPS_LINUX_SIGRTMAX)
1534     {
1535       /* GDB_SIGNAL_REALTIME values are not contiguous, map parts of
1536          the MIPS block to the respective GDB_SIGNAL_REALTIME blocks.  */
1537       int offset = signal - MIPS_LINUX_SIGRTMIN;
1538
1539       if (offset == 0)
1540         return GDB_SIGNAL_REALTIME_32;
1541       else if (offset < 32)
1542         return (enum gdb_signal) (offset - 1
1543                                   + (int) GDB_SIGNAL_REALTIME_33);
1544       else
1545         return (enum gdb_signal) (offset - 32
1546                                   + (int) GDB_SIGNAL_REALTIME_64);
1547     }
1548
1549   return linux_gdb_signal_from_target (gdbarch, signal);
1550 }
1551
1552 /* Initialize one of the GNU/Linux OS ABIs.  */
1553
1554 static void
1555 mips_linux_init_abi (struct gdbarch_info info,
1556                      struct gdbarch *gdbarch)
1557 {
1558   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1559   enum mips_abi abi = mips_abi (gdbarch);
1560   struct tdesc_arch_data *tdesc_data = (void *) info.tdep_info;
1561
1562   linux_init_abi (info, gdbarch);
1563
1564   /* Get the syscall number from the arch's register.  */
1565   set_gdbarch_get_syscall_number (gdbarch, mips_linux_get_syscall_number);
1566
1567   switch (abi)
1568     {
1569       case MIPS_ABI_O32:
1570         set_gdbarch_get_longjmp_target (gdbarch,
1571                                         mips_linux_get_longjmp_target);
1572         set_solib_svr4_fetch_link_map_offsets
1573           (gdbarch, svr4_ilp32_fetch_link_map_offsets);
1574         tramp_frame_prepend_unwinder (gdbarch, &mips_linux_o32_sigframe);
1575         tramp_frame_prepend_unwinder (gdbarch, &mips_linux_o32_rt_sigframe);
1576         set_xml_syscall_file_name ("syscalls/mips-o32-linux.xml");
1577         break;
1578       case MIPS_ABI_N32:
1579         set_gdbarch_get_longjmp_target (gdbarch,
1580                                         mips_linux_get_longjmp_target);
1581         set_solib_svr4_fetch_link_map_offsets
1582           (gdbarch, svr4_ilp32_fetch_link_map_offsets);
1583         set_gdbarch_long_double_bit (gdbarch, 128);
1584         /* These floatformats should probably be renamed.  MIPS uses
1585            the same 128-bit IEEE floating point format that IA-64 uses,
1586            except that the quiet/signalling NaN bit is reversed (GDB
1587            does not distinguish between quiet and signalling NaNs).  */
1588         set_gdbarch_long_double_format (gdbarch, floatformats_ia64_quad);
1589         tramp_frame_prepend_unwinder (gdbarch, &mips_linux_n32_rt_sigframe);
1590         set_xml_syscall_file_name ("syscalls/mips-n32-linux.xml");
1591         break;
1592       case MIPS_ABI_N64:
1593         set_gdbarch_get_longjmp_target (gdbarch,
1594                                         mips64_linux_get_longjmp_target);
1595         set_solib_svr4_fetch_link_map_offsets
1596           (gdbarch, svr4_lp64_fetch_link_map_offsets);
1597         set_gdbarch_long_double_bit (gdbarch, 128);
1598         /* These floatformats should probably be renamed.  MIPS uses
1599            the same 128-bit IEEE floating point format that IA-64 uses,
1600            except that the quiet/signalling NaN bit is reversed (GDB
1601            does not distinguish between quiet and signalling NaNs).  */
1602         set_gdbarch_long_double_format (gdbarch, floatformats_ia64_quad);
1603         tramp_frame_prepend_unwinder (gdbarch, &mips_linux_n64_rt_sigframe);
1604         set_xml_syscall_file_name ("syscalls/mips-n64-linux.xml");
1605         break;
1606       default:
1607         break;
1608     }
1609
1610   set_gdbarch_skip_solib_resolver (gdbarch, mips_linux_skip_resolver);
1611
1612   set_gdbarch_software_single_step (gdbarch, mips_software_single_step);
1613
1614   /* Enable TLS support.  */
1615   set_gdbarch_fetch_tls_load_module_address (gdbarch,
1616                                              svr4_fetch_objfile_link_map);
1617
1618   /* Initialize this lazily, to avoid an initialization order
1619      dependency on solib-svr4.c's _initialize routine.  */
1620   if (mips_svr4_so_ops.in_dynsym_resolve_code == NULL)
1621     {
1622       mips_svr4_so_ops = svr4_so_ops;
1623       mips_svr4_so_ops.in_dynsym_resolve_code
1624         = mips_linux_in_dynsym_resolve_code;
1625     }
1626   set_solib_ops (gdbarch, &mips_svr4_so_ops);
1627
1628   set_gdbarch_write_pc (gdbarch, mips_linux_write_pc);
1629
1630   set_gdbarch_core_read_description (gdbarch,
1631                                      mips_linux_core_read_description);
1632
1633   set_gdbarch_regset_from_core_section (gdbarch,
1634                                         mips_linux_regset_from_core_section);
1635
1636   set_gdbarch_gdb_signal_from_target (gdbarch,
1637                                       mips_gdb_signal_from_target);
1638
1639   set_gdbarch_gdb_signal_to_target (gdbarch,
1640                                     mips_gdb_signal_to_target);
1641
1642   tdep->syscall_next_pc = mips_linux_syscall_next_pc;
1643
1644   if (tdesc_data)
1645     {
1646       const struct tdesc_feature *feature;
1647
1648       /* If we have target-described registers, then we can safely
1649          reserve a number for MIPS_RESTART_REGNUM (whether it is
1650          described or not).  */
1651       gdb_assert (gdbarch_num_regs (gdbarch) <= MIPS_RESTART_REGNUM);
1652       set_gdbarch_num_regs (gdbarch, MIPS_RESTART_REGNUM + 1);
1653       set_gdbarch_num_pseudo_regs (gdbarch, MIPS_RESTART_REGNUM + 1);
1654
1655       /* If it's present, then assign it to the reserved number.  */
1656       feature = tdesc_find_feature (info.target_desc,
1657                                     "org.gnu.gdb.mips.linux");
1658       if (feature != NULL)
1659         tdesc_numbered_register (feature, tdesc_data, MIPS_RESTART_REGNUM,
1660                                  "restart");
1661     }
1662 }
1663
1664 /* Provide a prototype to silence -Wmissing-prototypes.  */
1665 extern initialize_file_ftype _initialize_mips_linux_tdep;
1666
1667 void
1668 _initialize_mips_linux_tdep (void)
1669 {
1670   const struct bfd_arch_info *arch_info;
1671
1672   for (arch_info = bfd_lookup_arch (bfd_arch_mips, 0);
1673        arch_info != NULL;
1674        arch_info = arch_info->next)
1675     {
1676       gdbarch_register_osabi (bfd_arch_mips, arch_info->mach,
1677                               GDB_OSABI_LINUX,
1678                               mips_linux_init_abi);
1679     }
1680 }