1/* Native support for MIPS running SVR4, for GDB.
2   Copyright 1994, 1995, 2000, 2001 Free Software Foundation, Inc.
3
4   This file is part of GDB.
5
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 2 of the License, or
9   (at your option) any later version.
10
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.
15
16   You should have received a copy of the GNU General Public License
17   along with this program; if not, write to the Free Software
18   Foundation, Inc., 59 Temple Place - Suite 330,
19   Boston, MA 02111-1307, USA.  */
20
21#include "defs.h"
22#include "inferior.h"
23#include "gdbcore.h"
24#include "target.h"
25#include "regcache.h"
26
27#include <sys/time.h>
28#include <sys/procfs.h>
29#include <setjmp.h>		/* For JB_XXX.  */
30
31/* Prototypes for supply_gregset etc. */
32#include "gregset.h"
33
34/* Size of elements in jmpbuf */
35
36#define JB_ELEMENT_SIZE 4
37
38/*
39 * See the comment in m68k-tdep.c regarding the utility of these functions.
40 *
41 * These definitions are from the MIPS SVR4 ABI, so they may work for
42 * any MIPS SVR4 target.
43 */
44
45void
46supply_gregset (gregset_t *gregsetp)
47{
48  int regi;
49  greg_t *regp = &(*gregsetp)[0];
50  char zerobuf[MAX_REGISTER_SIZE];
51  memset (zerobuf, 0, MAX_REGISTER_SIZE);
52
53  for (regi = 0; regi <= CXT_RA; regi++)
54    regcache_raw_supply (current_regcache, regi, (char *) (regp + regi));
55
56  regcache_raw_supply (current_regcache, mips_regnum (current_gdbarch)->pc,
57		       (char *) (regp + CXT_EPC));
58  regcache_raw_supply (current_regcache, mips_regnum (current_gdbarch)->hi,
59		       (char *) (regp + CXT_MDHI));
60  regcache_raw_supply (current_regcache, mips_regnum (current_gdbarch)->lo,
61		       (char *) (regp + CXT_MDLO));
62  regcache_raw_supply (current_regcache, mips_regnum (current_gdbarch)->cause,
63		       (char *) (regp + CXT_CAUSE));
64
65  /* Fill inaccessible registers with zero.  */
66  regcache_raw_supply (current_regcache, PS_REGNUM, zerobuf);
67  regcache_raw_supply (current_regcache,
68		       mips_regnum (current_gdbarch)->badvaddr,
69		       zerobuf);
70  regcache_raw_supply (current_regcache, DEPRECATED_FP_REGNUM, zerobuf);
71  regcache_raw_supply (current_regcache, UNUSED_REGNUM, zerobuf);
72  for (regi = FIRST_EMBED_REGNUM; regi <= LAST_EMBED_REGNUM; regi++)
73    regcache_raw_supply (current_regcache, regi, zerobuf);
74}
75
76void
77fill_gregset (gregset_t *gregsetp, int regno)
78{
79  int regi;
80  greg_t *regp = &(*gregsetp)[0];
81
82  for (regi = 0; regi <= 32; regi++)
83    if ((regno == -1) || (regno == regi))
84      *(regp + regi) = *(greg_t *) & deprecated_registers[DEPRECATED_REGISTER_BYTE (regi)];
85
86  if ((regno == -1) || (regno == mips_regnum (current_gdbarch)->pc))
87    *(regp + CXT_EPC) = *(greg_t *) & deprecated_registers[DEPRECATED_REGISTER_BYTE (mips_regnum (current_gdbarch)->pc)];
88
89  if ((regno == -1) || (regno == mips_regnum (current_gdbarch)->cause))
90    *(regp + CXT_CAUSE) = *(greg_t *) & deprecated_registers[DEPRECATED_REGISTER_BYTE (mips_regnum (current_gdbarch)->cause)];
91
92  if ((regno == -1) || (regno == mips_regnum (current_gdbarch)->hi))
93    *(regp + CXT_MDHI) = *(greg_t *) & deprecated_registers[DEPRECATED_REGISTER_BYTE (mips_regnum (current_gdbarch)->hi)];
94
95  if ((regno == -1) || (regno == mips_regnum (current_gdbarch)->lo))
96    *(regp + CXT_MDLO) = *(greg_t *) & deprecated_registers[DEPRECATED_REGISTER_BYTE (mips_regnum (current_gdbarch)->lo)];
97}
98
99/*
100 * Now we do the same thing for floating-point registers.
101 * We don't bother to condition on FP0 regnum since any
102 * reasonable MIPS configuration has an R3010 in it.
103 *
104 * Again, see the comments in m68k-tdep.c.
105 */
106
107void
108supply_fpregset (fpregset_t *fpregsetp)
109{
110  int regi;
111  char zerobuf[MAX_REGISTER_SIZE];
112  memset (zerobuf, 0, MAX_REGISTER_SIZE);
113
114  for (regi = 0; regi < 32; regi++)
115    regcache_raw_supply (current_regcache,
116			 mips_regnum (current_gdbarch)->fp0 + regi,
117			 (char *) &fpregsetp->fp_r.fp_regs[regi]);
118
119  regcache_raw_supply (current_regcache,
120		       mips_regnum (current_gdbarch)->fp_control_status,
121		       (char *) &fpregsetp->fp_csr);
122
123  /* FIXME: how can we supply FCRIR?  The ABI doesn't tell us. */
124  regcache_raw_supply (current_regcache,
125		       mips_regnum (current_gdbarch)->fp_implementation_revision,
126		       zerobuf);
127}
128
129void
130fill_fpregset (fpregset_t *fpregsetp, int regno)
131{
132  int regi;
133  char *from, *to;
134
135  for (regi = mips_regnum (current_gdbarch)->fp0;
136       regi < mips_regnum (current_gdbarch)->fp0 + 32; regi++)
137    {
138      if ((regno == -1) || (regno == regi))
139	{
140	  from = (char *) &deprecated_registers[DEPRECATED_REGISTER_BYTE (regi)];
141	  to = (char *) &(fpregsetp->fp_r.fp_regs[regi - mips_regnum (current_gdbarch)->fp0]);
142	  memcpy (to, from, register_size (current_gdbarch, regi));
143	}
144    }
145
146  if ((regno == -1)
147      || (regno == mips_regnum (current_gdbarch)->fp_control_status))
148    fpregsetp->fp_csr = *(unsigned *) &deprecated_registers[DEPRECATED_REGISTER_BYTE (mips_regnum (current_gdbarch)->fp_control_status)];
149}
150
151
152/* Figure out where the longjmp will land.
153   We expect the first arg to be a pointer to the jmp_buf structure from which
154   we extract the pc (_JB_PC) that we will land at.  The pc is copied into PC.
155   This routine returns true on success. */
156
157int
158get_longjmp_target (CORE_ADDR *pc)
159{
160  char *buf;
161  CORE_ADDR jb_addr;
162
163  buf = alloca (TARGET_PTR_BIT / TARGET_CHAR_BIT);
164  jb_addr = read_register (A0_REGNUM);
165
166  if (target_read_memory (jb_addr + _JB_PC * JB_ELEMENT_SIZE, buf,
167			  TARGET_PTR_BIT / TARGET_CHAR_BIT))
168    return 0;
169
170  *pc = extract_unsigned_integer (buf, TARGET_PTR_BIT / TARGET_CHAR_BIT);
171
172  return 1;
173}
174