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