2004-02-17 Ulrich Weigand <Ulrich.Weigand@de.ibm.com>
[deliverable/binutils-gdb.git] / gdb / s390-nat.c
1 /* S390 native-dependent code for GDB, the GNU debugger.
2 Copyright 2001, 2003 Free Software Foundation, Inc
3
4 Contributed by D.J. Barrow (djbarrow@de.ibm.com,barrow_dj@yahoo.com)
5 for IBM Deutschland Entwicklung GmbH, IBM Corporation.
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 2 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, write to the Free Software
21 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
22 02111-1307, USA. */
23
24 #include "defs.h"
25 #include "tm.h"
26 #include "regcache.h"
27 #include "inferior.h"
28
29 #include "s390-tdep.h"
30
31 #include <asm/ptrace.h>
32 #include <sys/ptrace.h>
33 #include <asm/types.h>
34 #include <sys/procfs.h>
35 #include <sys/user.h>
36 #include <sys/ucontext.h>
37
38
39 /* Map registers to gregset/ptrace offsets.
40 These arrays are defined in s390-tdep.c. */
41
42 #ifdef __s390x__
43 #define regmap_gregset s390x_regmap_gregset
44 #else
45 #define regmap_gregset s390_regmap_gregset
46 #endif
47
48 #define regmap_fpregset s390_regmap_fpregset
49
50
51 /* Fill GDB's register array with the general-purpose register values
52 in *REGP. */
53 void
54 supply_gregset (gregset_t *regp)
55 {
56 int i;
57 for (i = 0; i < S390_NUM_REGS; i++)
58 if (regmap_gregset[i] != -1)
59 regcache_raw_supply (current_regcache, i,
60 (char *)regp + regmap_gregset[i]);
61 }
62
63 /* Fill register REGNO (if it is a general-purpose register) in
64 *REGP with the value in GDB's register array. If REGNO is -1,
65 do this for all registers. */
66 void
67 fill_gregset (gregset_t *regp, int regno)
68 {
69 int i;
70 for (i = 0; i < S390_NUM_REGS; i++)
71 if (regmap_gregset[i] != -1)
72 if (regno == -1 || regno == i)
73 regcache_raw_collect (current_regcache, i,
74 (char *)regp + regmap_gregset[i]);
75 }
76
77 /* Fill GDB's register array with the floating-point register values
78 in *REGP. */
79 void
80 supply_fpregset (fpregset_t *regp)
81 {
82 int i;
83 for (i = 0; i < S390_NUM_REGS; i++)
84 if (regmap_fpregset[i] != -1)
85 regcache_raw_supply (current_regcache, i,
86 ((char *)regp) + regmap_fpregset[i]);
87 }
88
89 /* Fill register REGNO (if it is a general-purpose register) in
90 *REGP with the value in GDB's register array. If REGNO is -1,
91 do this for all registers. */
92 void
93 fill_fpregset (fpregset_t *regp, int regno)
94 {
95 int i;
96 for (i = 0; i < S390_NUM_REGS; i++)
97 if (regmap_fpregset[i] != -1)
98 if (regno == -1 || regno == i)
99 regcache_raw_collect (current_regcache, i,
100 ((char *)regp) + regmap_fpregset[i]);
101 }
102
103 /* Find the TID for the current inferior thread to use with ptrace. */
104 static int
105 s390_inferior_tid (void)
106 {
107 /* GNU/Linux LWP ID's are process ID's. */
108 int tid = TIDGET (inferior_ptid);
109 if (tid == 0)
110 tid = PIDGET (inferior_ptid); /* Not a threaded program. */
111
112 return tid;
113 }
114
115 /* Fetch all general-purpose registers from process/thread TID and
116 store their values in GDB's register cache. */
117 static void
118 fetch_regs (int tid)
119 {
120 gregset_t regs;
121 ptrace_area parea;
122
123 parea.len = sizeof (regs);
124 parea.process_addr = (addr_t) &regs;
125 parea.kernel_addr = offsetof (struct user_regs_struct, psw);
126 if (ptrace (PTRACE_PEEKUSR_AREA, tid, (long) &parea) < 0)
127 perror_with_name ("Couldn't get registers");
128
129 supply_gregset (&regs);
130 }
131
132 /* Store all valid general-purpose registers in GDB's register cache
133 into the process/thread specified by TID. */
134 static void
135 store_regs (int tid, int regnum)
136 {
137 gregset_t regs;
138 ptrace_area parea;
139
140 parea.len = sizeof (regs);
141 parea.process_addr = (addr_t) &regs;
142 parea.kernel_addr = offsetof (struct user_regs_struct, psw);
143 if (ptrace (PTRACE_PEEKUSR_AREA, tid, (long) &parea) < 0)
144 perror_with_name ("Couldn't get registers");
145
146 fill_gregset (&regs, regnum);
147
148 if (ptrace (PTRACE_POKEUSR_AREA, tid, (long) &parea) < 0)
149 perror_with_name ("Couldn't write registers");
150 }
151
152 /* Fetch all floating-point registers from process/thread TID and store
153 their values in GDB's register cache. */
154 static void
155 fetch_fpregs (int tid)
156 {
157 fpregset_t fpregs;
158 ptrace_area parea;
159
160 parea.len = sizeof (fpregs);
161 parea.process_addr = (addr_t) &fpregs;
162 parea.kernel_addr = offsetof (struct user_regs_struct, fp_regs);
163 if (ptrace (PTRACE_PEEKUSR_AREA, tid, (long) &parea) < 0)
164 perror_with_name ("Couldn't get floating point status");
165
166 supply_fpregset (&fpregs);
167 }
168
169 /* Store all valid floating-point registers in GDB's register cache
170 into the process/thread specified by TID. */
171 static void
172 store_fpregs (int tid, int regnum)
173 {
174 fpregset_t fpregs;
175 ptrace_area parea;
176
177 parea.len = sizeof (fpregs);
178 parea.process_addr = (addr_t) &fpregs;
179 parea.kernel_addr = offsetof (struct user_regs_struct, fp_regs);
180 if (ptrace (PTRACE_PEEKUSR_AREA, tid, (long) &parea) < 0)
181 perror_with_name ("Couldn't get floating point status");
182
183 fill_fpregset (&fpregs, regnum);
184
185 if (ptrace (PTRACE_POKEUSR_AREA, tid, (long) &parea) < 0)
186 perror_with_name ("Couldn't write floating point status");
187 }
188
189 /* Fetch register REGNUM from the child process. If REGNUM is -1, do
190 this for all registers. */
191 void
192 fetch_inferior_registers (int regnum)
193 {
194 int tid = s390_inferior_tid ();
195
196 if (regnum == -1
197 || (regnum < S390_NUM_REGS && regmap_gregset[regnum] != -1))
198 fetch_regs (tid);
199
200 if (regnum == -1
201 || (regnum < S390_NUM_REGS && regmap_fpregset[regnum] != -1))
202 fetch_fpregs (tid);
203 }
204
205 /* Store register REGNUM back into the child process. If REGNUM is
206 -1, do this for all registers. */
207 void
208 store_inferior_registers (int regnum)
209 {
210 int tid = s390_inferior_tid ();
211
212 if (regnum == -1
213 || (regnum < S390_NUM_REGS && regmap_gregset[regnum] != -1))
214 store_regs (tid, regnum);
215
216 if (regnum == -1
217 || (regnum < S390_NUM_REGS && regmap_fpregset[regnum] != -1))
218 store_fpregs (tid, regnum);
219 }
220
221
222 /* Hardware-assisted watchpoint handling. */
223
224 /* We maintain a list of all currently active watchpoints in order
225 to properly handle watchpoint removal.
226
227 The only thing we actually need is the total address space area
228 spanned by the watchpoints. */
229
230 struct watch_area
231 {
232 struct watch_area *next;
233 CORE_ADDR lo_addr;
234 CORE_ADDR hi_addr;
235 };
236
237 static struct watch_area *watch_base = NULL;
238
239 int
240 s390_stopped_by_watchpoint (void)
241 {
242 per_lowcore_bits per_lowcore;
243 ptrace_area parea;
244
245 /* Speed up common case. */
246 if (!watch_base)
247 return 0;
248
249 parea.len = sizeof (per_lowcore);
250 parea.process_addr = (addr_t) & per_lowcore;
251 parea.kernel_addr = offsetof (struct user_regs_struct, per_info.lowcore);
252 if (ptrace (PTRACE_PEEKUSR_AREA, s390_inferior_tid (), &parea) < 0)
253 perror_with_name ("Couldn't retrieve watchpoint status");
254
255 return per_lowcore.perc_storage_alteration == 1
256 && per_lowcore.perc_store_real_address == 0;
257 }
258
259 static void
260 s390_fix_watch_points (void)
261 {
262 int tid = s390_inferior_tid ();
263
264 per_struct per_info;
265 ptrace_area parea;
266
267 CORE_ADDR watch_lo_addr = (CORE_ADDR)-1, watch_hi_addr = 0;
268 struct watch_area *area;
269
270 for (area = watch_base; area; area = area->next)
271 {
272 watch_lo_addr = min (watch_lo_addr, area->lo_addr);
273 watch_hi_addr = max (watch_hi_addr, area->hi_addr);
274 }
275
276 parea.len = sizeof (per_info);
277 parea.process_addr = (addr_t) & per_info;
278 parea.kernel_addr = offsetof (struct user_regs_struct, per_info);
279 if (ptrace (PTRACE_PEEKUSR_AREA, tid, &parea) < 0)
280 perror_with_name ("Couldn't retrieve watchpoint status");
281
282 if (watch_base)
283 {
284 per_info.control_regs.bits.em_storage_alteration = 1;
285 per_info.control_regs.bits.storage_alt_space_ctl = 1;
286 }
287 else
288 {
289 per_info.control_regs.bits.em_storage_alteration = 0;
290 per_info.control_regs.bits.storage_alt_space_ctl = 0;
291 }
292 per_info.starting_addr = watch_lo_addr;
293 per_info.ending_addr = watch_hi_addr;
294
295 if (ptrace (PTRACE_POKEUSR_AREA, tid, &parea) < 0)
296 perror_with_name ("Couldn't modify watchpoint status");
297 }
298
299 int
300 s390_insert_watchpoint (CORE_ADDR addr, int len)
301 {
302 struct watch_area *area = xmalloc (sizeof (struct watch_area));
303 if (!area)
304 return -1;
305
306 area->lo_addr = addr;
307 area->hi_addr = addr + len - 1;
308
309 area->next = watch_base;
310 watch_base = area;
311
312 s390_fix_watch_points ();
313 return 0;
314 }
315
316 int
317 s390_remove_watchpoint (CORE_ADDR addr, int len)
318 {
319 struct watch_area *area, **parea;
320
321 for (parea = &watch_base; *parea; parea = &(*parea)->next)
322 if ((*parea)->lo_addr == addr
323 && (*parea)->hi_addr == addr + len - 1)
324 break;
325
326 if (!*parea)
327 {
328 fprintf_unfiltered (gdb_stderr,
329 "Attempt to remove nonexistent watchpoint.\n");
330 return -1;
331 }
332
333 area = *parea;
334 *parea = area->next;
335 xfree (area);
336
337 s390_fix_watch_points ();
338 return 0;
339 }
340
341
342 int
343 kernel_u_size (void)
344 {
345 return sizeof (struct user);
346 }
347
This page took 0.046562 seconds and 5 git commands to generate.