2003-09-19 Andrew Cagney <cagney@redhat.com>
[external/binutils.git] / gdb / sparc64nbsd-nat.c
1 /* Native-dependent code for UltraSPARC systems running NetBSD.
2    Copyright 2002, 2003 Free Software Foundation, Inc.
3    Contributed by Wasabi Systems, 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 2 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, write to the Free Software
19    Foundation, Inc., 59 Temple Place - Suite 330,
20    Boston, MA 02111-1307, USA.  */
21
22 #include "defs.h"
23 #include "inferior.h"
24 #include "regcache.h"
25
26 #include "sparc-tdep.h"
27 #include "sparcnbsd-tdep.h"
28
29 #include <sys/types.h>
30 #include <sys/ptrace.h>
31 #include <machine/reg.h>
32
33 /* NOTE: We don't bother with any of the deferred_store nonsense; it
34    makes things a lot more complicated than they need to be.  */
35
36 /* Determine if PT_GETREGS fetches this register.  */
37 static int
38 getregs_supplies (int regno)
39 {
40   /* FIXME: PS_REGNUM for 32-bit code.  */
41   return (regno == TSTATE_REGNUM
42           || regno == PC_REGNUM
43           || regno == DEPRECATED_NPC_REGNUM
44           || regno == Y_REGNUM
45           || (regno >= G0_REGNUM && regno <= G7_REGNUM)
46           || (regno >= O0_REGNUM && regno <= O7_REGNUM)
47           /* stack regs (handled by sparcnbsd_supply_reg)  */
48           || (regno >= L0_REGNUM && regno <= I7_REGNUM));
49 }
50
51 /* Determine if PT_GETFPREGS fetches this register.  */
52 static int
53 getfpregs_supplies (int regno)
54 {
55   return ((regno >= FP0_REGNUM && regno <= (FP0_REGNUM + 47))
56           || regno == FPS_REGNUM);
57 }
58
59 void
60 fetch_inferior_registers (int regno)
61 {
62   /* We don't use deferred stores.  */
63   if (deferred_stores)
64     internal_error (__FILE__, __LINE__,
65                     "fetch_inferior_registers: deferred stores pending");
66
67   if (regno == -1 || getregs_supplies (regno))
68     {
69       union {
70         struct reg32 regs32;
71         struct reg64 regs64;
72       } regs;
73
74       if (ptrace (PT_GETREGS, PIDGET (inferior_ptid),
75                   (PTRACE_ARG3_TYPE) &regs, 0) == -1)
76         perror_with_name ("Couldn't get registers");
77
78       if (gdbarch_ptr_bit (current_gdbarch) == 32)
79         sparcnbsd_supply_reg32 ((char *) &regs.regs32, regno);
80       else
81         sparcnbsd_supply_reg64 ((char *) &regs.regs64, regno);
82       if (regno != -1)
83         return;
84     }
85
86   if (regno == -1 || getfpregs_supplies (regno))
87     {
88       union {
89         struct fpreg32 fpregs32;
90         struct fpreg64 fpregs64;
91       } fpregs;
92
93       if (ptrace (PT_GETFPREGS, PIDGET (inferior_ptid),
94                   (PTRACE_ARG3_TYPE) &fpregs, 0) == -1)
95         perror_with_name ("Couldn't get floating point registers");
96
97       if (gdbarch_ptr_bit (current_gdbarch) == 32)
98         sparcnbsd_supply_fpreg32 ((char *) &fpregs.fpregs32, regno);
99       else
100         sparcnbsd_supply_fpreg64 ((char *) &fpregs.fpregs64, regno);
101       if (regno != -1)
102         return;
103     }
104 }
105
106 void
107 store_inferior_registers (int regno)
108 {
109   /* We don't use deferred stores.  */
110   if (deferred_stores)
111     internal_error (__FILE__, __LINE__,
112                     "store_inferior_registers: deferred stores pending");
113
114   if (regno == -1 || getregs_supplies (regno))
115     {
116       union {
117         struct reg32 regs32;
118         struct reg64 regs64;
119       } regs;
120
121       if (ptrace (PT_GETREGS, PIDGET (inferior_ptid),
122                   (PTRACE_ARG3_TYPE) &regs, 0) == -1)
123         perror_with_name ("Couldn't get registers");
124
125       if (gdbarch_ptr_bit (current_gdbarch) == 32)
126         sparcnbsd_fill_reg32 ((char *) &regs.regs32, regno);
127       else
128         sparcnbsd_fill_reg64 ((char *) &regs.regs64, regno);
129
130       if (ptrace (PT_SETREGS, PIDGET (inferior_ptid),
131                   (PTRACE_ARG3_TYPE) &regs, 0) == -1)
132         perror_with_name ("Couldn't write registers");
133
134       /* Deal with the stack regs.  */
135       if (regno == -1 || regno == SP_REGNUM
136           || (regno >= L0_REGNUM && regno <= I7_REGNUM))
137         {
138           CORE_ADDR sp = read_register (SP_REGNUM);
139           int i;
140           char buf[8];
141
142           if (sp & 1)
143             {
144               /* Registers are 64-bit.  */
145               sp += 2047;
146
147               for (i = L0_REGNUM; i <= I7_REGNUM; i++)
148                 {
149                   if (regno == -1 || regno == SP_REGNUM || regno == i)
150                     {
151                       regcache_collect (i, buf);
152                       target_write_memory (sp + ((i - L0_REGNUM) * 8),
153                                            buf, sizeof (buf));
154                     }
155                 }
156             }
157           else
158             {
159               /* Registers are 32-bit.  Toss any sign-extension of the stack
160                  pointer.
161
162                  FIXME: We don't currently handle 32-bit code in a binary
163                  that indicated LP64.  Do we have to care about that?  */
164               if (gdbarch_ptr_bit (current_gdbarch) != 32)
165                 internal_error
166                     (__FILE__, __LINE__,
167                     "store_inferior_registers: 32-bit code in 64-bit inferior");
168
169               sp &= 0xffffffffUL;
170               for (i = L0_REGNUM; i <= I7_REGNUM; i++)
171                 {
172                   if (regno == -1 || regno == SP_REGNUM || regno == i)
173                     {
174                       regcache_collect (i, buf);
175                       target_write_memory (sp + ((i - L0_REGNUM) * 4), buf, 4);
176                     }
177                 }
178             }
179         }
180
181       if (regno != -1)
182         return;
183     }
184
185   if (regno == -1 || getfpregs_supplies (regno))
186     {
187       union {
188         struct fpreg32 fpregs32;
189         struct fpreg64 fpregs64;
190       } fpregs;
191
192       if (ptrace (PT_GETFPREGS, PIDGET (inferior_ptid),
193                   (PTRACE_ARG3_TYPE) &fpregs, 0) == -1)
194         perror_with_name ("Couldn't get floating point registers");
195
196       if (gdbarch_ptr_bit (current_gdbarch) == 32)
197         sparcnbsd_fill_fpreg32 ((char *) &fpregs.fpregs32, regno);
198       else
199         sparcnbsd_fill_fpreg64 ((char *) &fpregs.fpregs64, regno);
200       
201       if (ptrace (PT_SETFPREGS, PIDGET (inferior_ptid),
202                   (PTRACE_ARG3_TYPE) &fpregs, 0) == -1)
203         perror_with_name ("Couldn't write floating point registers");
204
205       if (regno != -1)
206         return;
207     }
208 }