2007-11-16 Markus Deuling <deuling@de.ibm.com>
[external/binutils.git] / gdb / ppcnbsd-nat.c
1 /* Native-dependent code for NetBSD/powerpc.
2
3    Copyright (C) 2002, 2004, 2005, 2006, 2007 Free Software Foundation, Inc.
4
5    Contributed by Wasabi Systems, Inc.
6
7    This file is part of GDB.
8
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 3 of the License, or
12    (at your option) any later version.
13
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18
19    You should have received a copy of the GNU General Public License
20    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
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 "gdbcore.h"
30 #include "inferior.h"
31 #include "regcache.h"
32
33 #include "gdb_assert.h"
34
35 #include "ppc-tdep.h"
36 #include "ppcnbsd-tdep.h"
37 #include "bsd-kvm.h"
38 #include "inf-ptrace.h"
39
40 /* Returns true if PT_GETREGS fetches this register.  */
41
42 static int
43 getregs_supplies (int regnum)
44 {
45   struct gdbarch_tdep *tdep = gdbarch_tdep (current_gdbarch);
46
47   return ((regnum >= tdep->ppc_gp0_regnum
48            && regnum < tdep->ppc_gp0_regnum + ppc_num_gprs)
49           || regnum == tdep->ppc_lr_regnum
50           || regnum == tdep->ppc_cr_regnum
51           || regnum == tdep->ppc_xer_regnum
52           || regnum == tdep->ppc_ctr_regnum
53           || regnum == gdbarch_pc_regnum (current_gdbarch));
54 }
55
56 /* Like above, but for PT_GETFPREGS.  */
57
58 static int
59 getfpregs_supplies (int regnum)
60 {
61   struct gdbarch_tdep *tdep = gdbarch_tdep (current_gdbarch);
62
63   /* FIXME: jimb/2004-05-05: Some PPC variants don't have floating
64      point registers.  Traditionally, GDB's register set has still
65      listed the floating point registers for such machines, so this
66      code is harmless.  However, the new E500 port actually omits the
67      floating point registers entirely from the register set --- they
68      don't even have register numbers assigned to them.
69
70      It's not clear to me how best to update this code, so this assert
71      will alert the first person to encounter the NetBSD/E500
72      combination to the problem.  */
73   gdb_assert (ppc_floating_point_unit_p (current_gdbarch));
74
75   return ((regnum >= tdep->ppc_fp0_regnum
76            && regnum < tdep->ppc_fp0_regnum + ppc_num_fprs)
77           || regnum == tdep->ppc_fpscr_regnum);
78 }
79
80 static void
81 ppcnbsd_fetch_inferior_registers (struct regcache *regcache, int regnum)
82 {
83   if (regnum == -1 || getregs_supplies (regnum))
84     {
85       struct reg regs;
86
87       if (ptrace (PT_GETREGS, PIDGET (inferior_ptid),
88                   (PTRACE_TYPE_ARG3) &regs, 0) == -1)
89         perror_with_name (_("Couldn't get registers"));
90
91       ppc_supply_gregset (&ppcnbsd_gregset, regcache,
92                           regnum, &regs, sizeof regs);
93     }
94
95   if (regnum == -1 || getfpregs_supplies (regnum))
96     {
97       struct fpreg fpregs;
98
99       if (ptrace (PT_GETFPREGS, PIDGET (inferior_ptid),
100                   (PTRACE_TYPE_ARG3) &fpregs, 0) == -1)
101         perror_with_name (_("Couldn't get FP registers"));
102
103       ppc_supply_fpregset (&ppcnbsd_fpregset, regcache,
104                            regnum, &fpregs, sizeof fpregs);
105     }
106 }
107
108 static void
109 ppcnbsd_store_inferior_registers (struct regcache *regcache, int regnum)
110 {
111   if (regnum == -1 || getregs_supplies (regnum))
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       ppc_collect_gregset (&ppcnbsd_gregset, regcache,
120                            regnum, &regs, sizeof regs);
121
122       if (ptrace (PT_SETREGS, PIDGET (inferior_ptid),
123                   (PTRACE_TYPE_ARG3) &regs, 0) == -1)
124         perror_with_name (_("Couldn't write registers"));
125     }
126
127   if (regnum == -1 || getfpregs_supplies (regnum))
128     {
129       struct fpreg fpregs;
130
131       if (ptrace (PT_GETFPREGS, PIDGET (inferior_ptid),
132                   (PTRACE_TYPE_ARG3) &fpregs, 0) == -1)
133         perror_with_name (_("Couldn't get FP registers"));
134
135       ppc_collect_fpregset (&ppcnbsd_fpregset, regcache,
136                             regnum, &fpregs, sizeof fpregs);
137
138       if (ptrace (PT_SETFPREGS, PIDGET (inferior_ptid),
139                   (PTRACE_TYPE_ARG3) &fpregs, 0) == -1)
140         perror_with_name (_("Couldn't set FP registers"));
141     }
142 }
143
144 static int
145 ppcnbsd_supply_pcb (struct regcache *regcache, struct pcb *pcb)
146 {
147   struct switchframe sf;
148   struct callframe cf;
149   struct gdbarch *gdbarch = get_regcache_arch (regcache);
150   struct gdbarch_tdep *tdep = gdbarch_tdep (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, (gdb_byte *)&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, (gdb_byte *)&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, (gdb_byte *)&cf, sizeof(cf));
170   regcache_raw_supply (regcache, tdep->ppc_lr_regnum, &cf.lr);
171   regcache_raw_supply (regcache, gdbarch_pc_regnum (gdbarch), &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
184   /* Support debugging kernel virtual memory images.  */
185   bsd_kvm_add_target (ppcnbsd_supply_pcb);
186
187   /* Add in local overrides.  */
188   t = inf_ptrace_target ();
189   t->to_fetch_registers = ppcnbsd_fetch_inferior_registers;
190   t->to_store_registers = ppcnbsd_store_inferior_registers;
191   add_target (t);
192 }