2004-09-12 Andrew Cagney <cagney@gnu.org>
[external/binutils.git] / gdb / ppcnbsd-nat.c
1 /* Native-dependent code for PowerPC's running NetBSD, for GDB.
2    Copyright 2002, 2004 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 <sys/types.h>
23 #include <sys/ptrace.h>
24 #include <machine/reg.h>
25 #include <machine/frame.h>
26 #include <machine/pcb.h>
27
28 #include "defs.h"
29 #include "inferior.h"
30 #include "gdb_assert.h"
31 #include "gdbcore.h"
32 #include "regcache.h"
33 #include "bsd-kvm.h"
34
35 #include "ppc-tdep.h"
36 #include "ppcnbsd-tdep.h"
37
38 #include "inf-ptrace.h"
39
40 /* Returns true if PT_GETREGS fetches this register.  */
41 static int
42 getregs_supplies (int regno)
43 {
44   struct gdbarch_tdep *tdep = gdbarch_tdep (current_gdbarch);
45
46   return ((regno >= tdep->ppc_gp0_regnum
47            && regno < tdep->ppc_gp0_regnum + ppc_num_gprs)
48           || regno == tdep->ppc_lr_regnum
49           || regno == tdep->ppc_cr_regnum
50           || regno == tdep->ppc_xer_regnum
51           || regno == tdep->ppc_ctr_regnum
52           || regno == PC_REGNUM);
53 }
54
55 /* Like above, but for PT_GETFPREGS.  */
56 static int
57 getfpregs_supplies (int regno)
58 {
59   struct gdbarch_tdep *tdep = gdbarch_tdep (current_gdbarch);
60
61   /* FIXME: jimb/2004-05-05: Some PPC variants don't have floating
62      point registers.  Traditionally, GDB's register set has still
63      listed the floating point registers for such machines, so this
64      code is harmless.  However, the new E500 port actually omits the
65      floating point registers entirely from the register set --- they
66      don't even have register numbers assigned to them.
67
68      It's not clear to me how best to update this code, so this assert
69      will alert the first person to encounter the NetBSD/E500
70      combination to the problem.  */
71   gdb_assert (ppc_floating_point_unit_p (current_gdbarch));
72
73   return ((regno >= tdep->ppc_fp0_regnum
74            && regno < tdep->ppc_fp0_regnum + ppc_num_fprs)
75           || regno == tdep->ppc_fpscr_regnum);
76 }
77
78 static void
79 ppcnbsd_fetch_inferior_registers (int regno)
80 {
81   if (regno == -1 || getregs_supplies (regno))
82     {
83       struct reg regs;
84
85       if (ptrace (PT_GETREGS, PIDGET (inferior_ptid),
86                   (PTRACE_TYPE_ARG3) &regs, 0) == -1)
87         perror_with_name ("Couldn't get registers");
88
89       ppcnbsd_supply_reg ((char *) &regs, regno);
90       if (regno != -1)
91         return;
92     }
93
94   if (regno == -1 || getfpregs_supplies (regno))
95     {
96       struct fpreg fpregs;
97
98       if (ptrace (PT_GETFPREGS, PIDGET (inferior_ptid),
99                   (PTRACE_TYPE_ARG3) &fpregs, 0) == -1)
100         perror_with_name ("Couldn't get FP registers");
101
102       ppcnbsd_supply_fpreg ((char *) &fpregs, regno);
103       if (regno != -1)
104         return;
105     }
106 }
107
108 static void
109 ppcnbsd_store_inferior_registers (int regno)
110 {
111   if (regno == -1 || getregs_supplies (regno))
112     {
113       struct reg regs;
114
115       if (ptrace (PT_GETREGS, PIDGET (inferior_ptid),
116                   (PTRACE_TYPE_ARG3) &regs, 0) == -1)
117         perror_with_name ("Couldn't get registers");
118
119       ppcnbsd_fill_reg ((char *) &regs, regno);
120
121       if (ptrace (PT_SETREGS, PIDGET (inferior_ptid),
122                   (PTRACE_TYPE_ARG3) &regs, 0) == -1)
123         perror_with_name ("Couldn't write registers");
124
125       if (regno != -1)
126         return;
127     }
128
129   if (regno == -1 || getfpregs_supplies (regno))
130     {
131       struct fpreg fpregs;
132
133       if (ptrace (PT_GETFPREGS, PIDGET (inferior_ptid),
134                   (PTRACE_TYPE_ARG3) &fpregs, 0) == -1)
135         perror_with_name ("Couldn't get FP registers");
136
137       ppcnbsd_fill_fpreg ((char *) &fpregs, regno);
138       
139       if (ptrace (PT_SETFPREGS, PIDGET (inferior_ptid),
140                   (PTRACE_TYPE_ARG3) &fpregs, 0) == -1)
141         perror_with_name ("Couldn't set FP registers");
142     }
143 }
144
145 static int
146 ppcnbsd_supply_pcb (struct regcache *regcache, struct pcb *pcb)
147 {
148   struct switchframe sf;
149   struct callframe cf;
150   struct gdbarch_tdep *tdep = gdbarch_tdep (current_gdbarch);
151   int i;
152
153   /* The stack pointer shouldn't be zero.  */
154   if (pcb->pcb_sp == 0)
155     return 0;
156
157   read_memory (pcb->pcb_sp, (char *) &sf, sizeof sf);
158   regcache_raw_supply (regcache, tdep->ppc_cr_regnum, &sf.cr);
159   regcache_raw_supply (regcache, tdep->ppc_gp0_regnum + 2, &sf.fixreg2);
160   for (i = 0 ; i < 19 ; i++)
161     regcache_raw_supply (regcache, tdep->ppc_gp0_regnum + 13 + i,
162                          &sf.fixreg[i]);
163
164   read_memory(sf.sp, (char *)&cf, sizeof(cf));
165   regcache_raw_supply (regcache, tdep->ppc_gp0_regnum + 30, &cf.r30);
166   regcache_raw_supply (regcache, tdep->ppc_gp0_regnum + 31, &cf.r31);
167   regcache_raw_supply (regcache, tdep->ppc_gp0_regnum + 1, &cf.sp);
168
169   read_memory(cf.sp, (char *)&cf, sizeof(cf));
170   regcache_raw_supply (regcache, tdep->ppc_lr_regnum, &cf.lr);
171   regcache_raw_supply (regcache, PC_REGNUM, &cf.lr);
172
173   return 1;
174 }
175
176 /* Provide a prototype to silence -Wmissing-prototypes.  */
177 void _initialize_ppcnbsd_nat (void);
178
179 void
180 _initialize_ppcnbsd_nat (void)
181 {
182   struct target_ops *t;
183   /* Support debugging kernel virtual memory images.  */
184   bsd_kvm_add_target (ppcnbsd_supply_pcb);
185   /* Add in local overrides.  */
186   t = inf_ptrace_target ();
187   t->to_fetch_registers = ppcnbsd_fetch_inferior_registers;
188   t->to_store_registers = ppcnbsd_store_inferior_registers;
189   add_target (t);
190 }