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