]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/ppcobsd-nat.c
Copyright updates for 2007.
[thirdparty/binutils-gdb.git] / gdb / ppcobsd-nat.c
1 /* Native-dependent code for OpenBSD/powerpc.
2
3 Copyright (C) 2004, 2005, 2006, 2007 Free Software Foundation, 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., 51 Franklin Street, Fifth Floor,
20 Boston, MA 02110-1301, USA. */
21
22 #include "defs.h"
23 #include "gdbcore.h"
24 #include "inferior.h"
25 #include "regcache.h"
26
27 #include "gdb_assert.h"
28 #include <stddef.h>
29 #include <sys/types.h>
30 #include <sys/ptrace.h>
31 #include <sys/signal.h>
32 #include <machine/frame.h>
33 #include <machine/pcb.h>
34 #include <machine/reg.h>
35
36 #include "ppc-tdep.h"
37 #include "ppcobsd-tdep.h"
38 #include "inf-ptrace.h"
39 #include "bsd-kvm.h"
40
41 /* OpenBSD/powerpc didn't have PT_GETFPREGS/PT_SETFPREGS until release
42 4.0. On older releases the floating-point registers are handled by
43 PT_GETREGS/PT_SETREGS, but fpscr wasn't available.. */
44
45 #ifdef PT_GETFPREGS
46
47 /* Returns true if PT_GETFPREGS fetches this register. */
48
49 static int
50 getfpregs_supplies (int regnum)
51 {
52 struct gdbarch_tdep *tdep = gdbarch_tdep (current_gdbarch);
53
54 /* FIXME: jimb/2004-05-05: Some PPC variants don't have floating
55 point registers. Traditionally, GDB's register set has still
56 listed the floating point registers for such machines, so this
57 code is harmless. However, the new E500 port actually omits the
58 floating point registers entirely from the register set --- they
59 don't even have register numbers assigned to them.
60
61 It's not clear to me how best to update this code, so this assert
62 will alert the first person to encounter the NetBSD/E500
63 combination to the problem. */
64 gdb_assert (ppc_floating_point_unit_p (current_gdbarch));
65
66 return ((regnum >= tdep->ppc_fp0_regnum
67 && regnum < tdep->ppc_fp0_regnum + ppc_num_fprs)
68 || regnum == tdep->ppc_fpscr_regnum);
69 }
70
71 #endif /* PT_GETFPREGS */
72
73 /* Fetch register REGNUM from the inferior. If REGNUM is -1, do this
74 for all registers. */
75
76 static void
77 ppcobsd_fetch_registers (int regnum)
78 {
79 struct reg regs;
80
81 if (ptrace (PT_GETREGS, PIDGET (inferior_ptid),
82 (PTRACE_TYPE_ARG3) &regs, 0) == -1)
83 perror_with_name (_("Couldn't get registers"));
84
85 ppc_supply_gregset (&ppcobsd_gregset, current_regcache, -1,
86 &regs, sizeof regs);
87 #ifndef PT_GETFPREGS
88 ppc_supply_fpregset (&ppcobsd_gregset, current_regcache, -1,
89 &regs, sizeof regs);
90 #endif
91
92 #ifdef PT_GETFPREGS
93 if (regnum == -1 || getfpregs_supplies (regnum))
94 {
95 struct fpreg fpregs;
96
97 if (ptrace (PT_GETFPREGS, PIDGET (inferior_ptid),
98 (PTRACE_TYPE_ARG3) &fpregs, 0) == -1)
99 perror_with_name (_("Couldn't get floating point status"));
100
101 ppc_supply_fpregset (&ppcobsd_fpregset, current_regcache, -1,
102 &fpregs, sizeof fpregs);
103 }
104 #endif
105 }
106
107 /* Store register REGNUM back into the inferior. If REGNUM is -1, do
108 this for all registers. */
109
110 static void
111 ppcobsd_store_registers (int 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 (&ppcobsd_gregset, current_regcache,
120 regnum, &regs, sizeof regs);
121 #ifndef PT_GETFPREGS
122 ppc_collect_fpregset (&ppcobsd_gregset, current_regcache,
123 regnum, &regs, sizeof regs);
124 #endif
125
126 if (ptrace (PT_SETREGS, PIDGET (inferior_ptid),
127 (PTRACE_TYPE_ARG3) &regs, 0) == -1)
128 perror_with_name (_("Couldn't write registers"));
129
130 #ifdef PT_GETFPREGS
131 if (regnum == -1 || getfpregs_supplies (regnum))
132 {
133 struct fpreg fpregs;
134
135 if (ptrace (PT_GETFPREGS, PIDGET (inferior_ptid),
136 (PTRACE_TYPE_ARG3) &fpregs, 0) == -1)
137 perror_with_name (_("Couldn't get floating point status"));
138
139 ppc_collect_fpregset (&ppcobsd_fpregset, current_regcache,
140 regnum, &fpregs, sizeof fpregs);
141
142 if (ptrace (PT_SETFPREGS, PIDGET (inferior_ptid),
143 (PTRACE_TYPE_ARG3) &fpregs, 0) == -1)
144 perror_with_name (_("Couldn't write floating point status"));
145 }
146 #endif
147 }
148 \f
149
150 static int
151 ppcobsd_supply_pcb (struct regcache *regcache, struct pcb *pcb)
152 {
153 struct gdbarch_tdep *tdep = gdbarch_tdep (get_regcache_arch (regcache));
154 struct switchframe sf;
155 struct callframe cf;
156 int i, regnum;
157
158 /* The following is true for OpenBSD 3.7:
159
160 The pcb contains %r1 (the stack pointer) at the point of the
161 context switch in cpu_switch(). At that point we have a stack
162 frame as described by `struct switchframe', and below that a call
163 frame as described by `struct callframe'. From this information
164 we reconstruct the register state as it would look when we are in
165 cpu_switch(). */
166
167 /* The stack pointer shouldn't be zero. */
168 if (pcb->pcb_sp == 0)
169 return 0;
170
171 read_memory (pcb->pcb_sp, (gdb_byte *)&sf, sizeof sf);
172 regcache_raw_supply (regcache, SP_REGNUM, &sf.sp);
173 regcache_raw_supply (regcache, tdep->ppc_cr_regnum, &sf.cr);
174 regcache_raw_supply (regcache, tdep->ppc_gp0_regnum + 2, &sf.fixreg2);
175 for (i = 0, regnum = tdep->ppc_gp0_regnum + 13; i < 19; i++, regnum++)
176 regcache_raw_supply (regcache, regnum, &sf.fixreg[i]);
177
178 read_memory (sf.sp, (gdb_byte *)&cf, sizeof cf);
179 regcache_raw_supply (regcache, PC_REGNUM, &cf.lr);
180 regcache_raw_supply (regcache, tdep->ppc_gp0_regnum + 30, &cf.r30);
181 regcache_raw_supply (regcache, tdep->ppc_gp0_regnum + 31, &cf.r31);
182
183 return 1;
184 }
185 \f
186
187 /* Provide a prototype to silence -Wmissing-prototypes. */
188 void _initialize_ppcobsd_nat (void);
189
190 void
191 _initialize_ppcobsd_nat (void)
192 {
193 struct target_ops *t;
194
195 /* Add in local overrides. */
196 t = inf_ptrace_target ();
197 t->to_fetch_registers = ppcobsd_fetch_registers;
198 t->to_store_registers = ppcobsd_store_registers;
199 add_target (t);
200
201 /* General-purpose registers. */
202 ppcobsd_reg_offsets.r0_offset = offsetof (struct reg, gpr);
203 ppcobsd_reg_offsets.pc_offset = offsetof (struct reg, pc);
204 ppcobsd_reg_offsets.ps_offset = offsetof (struct reg, ps);
205 ppcobsd_reg_offsets.cr_offset = offsetof (struct reg, cnd);
206 ppcobsd_reg_offsets.lr_offset = offsetof (struct reg, lr);
207 ppcobsd_reg_offsets.ctr_offset = offsetof (struct reg, cnt);
208 ppcobsd_reg_offsets.xer_offset = offsetof (struct reg, xer);
209 ppcobsd_reg_offsets.mq_offset = offsetof (struct reg, mq);
210
211 /* Floating-point registers. */
212 ppcobsd_reg_offsets.f0_offset = offsetof (struct reg, fpr);
213 ppcobsd_reg_offsets.fpscr_offset = -1;
214 #ifdef PT_GETFPREGS
215 ppcobsd_fpreg_offsets.f0_offset = offsetof (struct fpreg, fpr);
216 ppcobsd_fpreg_offsets.fpscr_offset = offsetof (struct fpreg, fpscr);
217 #endif
218
219 /* AltiVec registers. */
220 ppcobsd_reg_offsets.vr0_offset = offsetof (struct vreg, vreg);
221 ppcobsd_reg_offsets.vscr_offset = offsetof (struct vreg, vscr);
222 ppcobsd_reg_offsets.vrsave_offset = offsetof (struct vreg, vrsave);
223
224 /* Support debugging kernel virtual memory images. */
225 bsd_kvm_add_target (ppcobsd_supply_pcb);
226 }