s390-linux-nat.c revision 1.1.1.5 1 1.1 christos /* S390 native-dependent code for GDB, the GNU debugger.
2 1.1.1.5 christos Copyright (C) 2001-2017 Free Software Foundation, Inc.
3 1.1 christos
4 1.1 christos Contributed by D.J. Barrow (djbarrow (at) de.ibm.com,barrow_dj (at) yahoo.com)
5 1.1 christos for IBM Deutschland Entwicklung GmbH, IBM Corporation.
6 1.1 christos
7 1.1 christos This file is part of GDB.
8 1.1 christos
9 1.1 christos This program is free software; you can redistribute it and/or modify
10 1.1 christos it under the terms of the GNU General Public License as published by
11 1.1 christos the Free Software Foundation; either version 3 of the License, or
12 1.1 christos (at your option) any later version.
13 1.1 christos
14 1.1 christos This program is distributed in the hope that it will be useful,
15 1.1 christos but WITHOUT ANY WARRANTY; without even the implied warranty of
16 1.1 christos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 1.1 christos GNU General Public License for more details.
18 1.1 christos
19 1.1 christos You should have received a copy of the GNU General Public License
20 1.1 christos along with this program. If not, see <http://www.gnu.org/licenses/>. */
21 1.1 christos
22 1.1 christos #include "defs.h"
23 1.1 christos #include "regcache.h"
24 1.1 christos #include "inferior.h"
25 1.1 christos #include "target.h"
26 1.1 christos #include "linux-nat.h"
27 1.1 christos #include "auxv.h"
28 1.1 christos #include "gregset.h"
29 1.1.1.2 christos #include "regset.h"
30 1.1.1.3 christos #include "nat/linux-ptrace.h"
31 1.1.1.5 christos #include "gdbcmd.h"
32 1.1 christos
33 1.1 christos #include "s390-linux-tdep.h"
34 1.1 christos #include "elf/common.h"
35 1.1 christos
36 1.1 christos #include <asm/ptrace.h>
37 1.1.1.4 christos #include "nat/gdb_ptrace.h"
38 1.1 christos #include <asm/types.h>
39 1.1 christos #include <sys/procfs.h>
40 1.1 christos #include <sys/ucontext.h>
41 1.1 christos #include <elf.h>
42 1.1.1.5 christos #include <algorithm>
43 1.1.1.5 christos #include "inf-ptrace.h"
44 1.1 christos
45 1.1.1.3 christos /* Per-thread arch-specific data. */
46 1.1 christos
47 1.1.1.3 christos struct arch_lwp_info
48 1.1.1.3 christos {
49 1.1.1.3 christos /* Non-zero if the thread's PER info must be re-written. */
50 1.1.1.3 christos int per_info_changed;
51 1.1.1.3 christos };
52 1.1 christos
53 1.1 christos static int have_regset_last_break = 0;
54 1.1 christos static int have_regset_system_call = 0;
55 1.1 christos static int have_regset_tdb = 0;
56 1.1.1.3 christos static int have_regset_vxrs = 0;
57 1.1 christos
58 1.1.1.2 christos /* Register map for 32-bit executables running under a 64-bit
59 1.1.1.2 christos kernel. */
60 1.1 christos
61 1.1 christos #ifdef __s390x__
62 1.1.1.2 christos static const struct regcache_map_entry s390_64_regmap_gregset[] =
63 1.1.1.2 christos {
64 1.1.1.2 christos /* Skip PSWM and PSWA, since they must be handled specially. */
65 1.1.1.2 christos { 2, REGCACHE_MAP_SKIP, 8 },
66 1.1.1.2 christos { 1, S390_R0_UPPER_REGNUM, 4 }, { 1, S390_R0_REGNUM, 4 },
67 1.1.1.2 christos { 1, S390_R1_UPPER_REGNUM, 4 }, { 1, S390_R1_REGNUM, 4 },
68 1.1.1.2 christos { 1, S390_R2_UPPER_REGNUM, 4 }, { 1, S390_R2_REGNUM, 4 },
69 1.1.1.2 christos { 1, S390_R3_UPPER_REGNUM, 4 }, { 1, S390_R3_REGNUM, 4 },
70 1.1.1.2 christos { 1, S390_R4_UPPER_REGNUM, 4 }, { 1, S390_R4_REGNUM, 4 },
71 1.1.1.2 christos { 1, S390_R5_UPPER_REGNUM, 4 }, { 1, S390_R5_REGNUM, 4 },
72 1.1.1.2 christos { 1, S390_R6_UPPER_REGNUM, 4 }, { 1, S390_R6_REGNUM, 4 },
73 1.1.1.2 christos { 1, S390_R7_UPPER_REGNUM, 4 }, { 1, S390_R7_REGNUM, 4 },
74 1.1.1.2 christos { 1, S390_R8_UPPER_REGNUM, 4 }, { 1, S390_R8_REGNUM, 4 },
75 1.1.1.2 christos { 1, S390_R9_UPPER_REGNUM, 4 }, { 1, S390_R9_REGNUM, 4 },
76 1.1.1.2 christos { 1, S390_R10_UPPER_REGNUM, 4 }, { 1, S390_R10_REGNUM, 4 },
77 1.1.1.2 christos { 1, S390_R11_UPPER_REGNUM, 4 }, { 1, S390_R11_REGNUM, 4 },
78 1.1.1.2 christos { 1, S390_R12_UPPER_REGNUM, 4 }, { 1, S390_R12_REGNUM, 4 },
79 1.1.1.2 christos { 1, S390_R13_UPPER_REGNUM, 4 }, { 1, S390_R13_REGNUM, 4 },
80 1.1.1.2 christos { 1, S390_R14_UPPER_REGNUM, 4 }, { 1, S390_R14_REGNUM, 4 },
81 1.1.1.2 christos { 1, S390_R15_UPPER_REGNUM, 4 }, { 1, S390_R15_REGNUM, 4 },
82 1.1.1.2 christos { 16, S390_A0_REGNUM, 4 },
83 1.1.1.2 christos { 1, REGCACHE_MAP_SKIP, 4 }, { 1, S390_ORIG_R2_REGNUM, 4 },
84 1.1.1.2 christos { 0 }
85 1.1.1.2 christos };
86 1.1.1.2 christos
87 1.1.1.2 christos static const struct regset s390_64_gregset =
88 1.1.1.2 christos {
89 1.1.1.2 christos s390_64_regmap_gregset,
90 1.1.1.2 christos regcache_supply_regset,
91 1.1.1.2 christos regcache_collect_regset
92 1.1.1.2 christos };
93 1.1 christos
94 1.1.1.2 christos #define S390_PSWM_OFFSET 0
95 1.1.1.2 christos #define S390_PSWA_OFFSET 8
96 1.1.1.2 christos #endif
97 1.1 christos
98 1.1.1.5 christos /* PER-event mask bits and PER control bits (CR9). */
99 1.1.1.5 christos
100 1.1.1.5 christos #define PER_BIT(n) (1UL << (63 - (n)))
101 1.1.1.5 christos #define PER_EVENT_BRANCH PER_BIT (32)
102 1.1.1.5 christos #define PER_EVENT_IFETCH PER_BIT (33)
103 1.1.1.5 christos #define PER_EVENT_STORE PER_BIT (34)
104 1.1.1.5 christos #define PER_EVENT_NULLIFICATION PER_BIT (39)
105 1.1.1.5 christos #define PER_CONTROL_BRANCH_ADDRESS PER_BIT (40)
106 1.1.1.5 christos #define PER_CONTROL_SUSPENSION PER_BIT (41)
107 1.1.1.5 christos #define PER_CONTROL_ALTERATION PER_BIT (42)
108 1.1.1.5 christos
109 1.1.1.5 christos
110 1.1 christos /* Fill GDB's register array with the general-purpose register values
111 1.1 christos in *REGP.
112 1.1 christos
113 1.1 christos When debugging a 32-bit executable running under a 64-bit kernel,
114 1.1 christos we have to fix up the 64-bit registers we get from the kernel to
115 1.1 christos make them look like 32-bit registers. */
116 1.1 christos
117 1.1 christos void
118 1.1 christos supply_gregset (struct regcache *regcache, const gregset_t *regp)
119 1.1 christos {
120 1.1 christos #ifdef __s390x__
121 1.1 christos struct gdbarch *gdbarch = get_regcache_arch (regcache);
122 1.1 christos if (gdbarch_ptr_bit (gdbarch) == 32)
123 1.1 christos {
124 1.1 christos enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
125 1.1.1.2 christos ULONGEST pswm, pswa;
126 1.1 christos gdb_byte buf[4];
127 1.1 christos
128 1.1.1.2 christos regcache_supply_regset (&s390_64_gregset, regcache, -1,
129 1.1.1.2 christos regp, sizeof (gregset_t));
130 1.1.1.2 christos pswm = extract_unsigned_integer ((const gdb_byte *) regp
131 1.1.1.2 christos + S390_PSWM_OFFSET, 8, byte_order);
132 1.1.1.2 christos pswa = extract_unsigned_integer ((const gdb_byte *) regp
133 1.1.1.2 christos + S390_PSWA_OFFSET, 8, byte_order);
134 1.1 christos store_unsigned_integer (buf, 4, byte_order, (pswm >> 32) | 0x80000);
135 1.1 christos regcache_raw_supply (regcache, S390_PSWM_REGNUM, buf);
136 1.1 christos store_unsigned_integer (buf, 4, byte_order,
137 1.1 christos (pswa & 0x7fffffff) | (pswm & 0x80000000));
138 1.1 christos regcache_raw_supply (regcache, S390_PSWA_REGNUM, buf);
139 1.1 christos return;
140 1.1 christos }
141 1.1 christos #endif
142 1.1 christos
143 1.1.1.2 christos regcache_supply_regset (&s390_gregset, regcache, -1, regp,
144 1.1.1.2 christos sizeof (gregset_t));
145 1.1 christos }
146 1.1 christos
147 1.1 christos /* Fill register REGNO (if it is a general-purpose register) in
148 1.1 christos *REGP with the value in GDB's register array. If REGNO is -1,
149 1.1 christos do this for all registers. */
150 1.1 christos
151 1.1 christos void
152 1.1 christos fill_gregset (const struct regcache *regcache, gregset_t *regp, int regno)
153 1.1 christos {
154 1.1 christos #ifdef __s390x__
155 1.1 christos struct gdbarch *gdbarch = get_regcache_arch (regcache);
156 1.1 christos if (gdbarch_ptr_bit (gdbarch) == 32)
157 1.1 christos {
158 1.1.1.2 christos regcache_collect_regset (&s390_64_gregset, regcache, regno,
159 1.1.1.2 christos regp, sizeof (gregset_t));
160 1.1 christos
161 1.1 christos if (regno == -1
162 1.1 christos || regno == S390_PSWM_REGNUM || regno == S390_PSWA_REGNUM)
163 1.1 christos {
164 1.1 christos enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
165 1.1 christos ULONGEST pswa, pswm;
166 1.1 christos gdb_byte buf[4];
167 1.1.1.3 christos gdb_byte *pswm_p = (gdb_byte *) regp + S390_PSWM_OFFSET;
168 1.1.1.3 christos gdb_byte *pswa_p = (gdb_byte *) regp + S390_PSWA_OFFSET;
169 1.1 christos
170 1.1.1.3 christos pswm = extract_unsigned_integer (pswm_p, 8, byte_order);
171 1.1 christos
172 1.1 christos if (regno == -1 || regno == S390_PSWM_REGNUM)
173 1.1.1.3 christos {
174 1.1.1.3 christos pswm &= 0x80000000;
175 1.1.1.3 christos regcache_raw_collect (regcache, S390_PSWM_REGNUM, buf);
176 1.1.1.3 christos pswm |= (extract_unsigned_integer (buf, 4, byte_order)
177 1.1.1.3 christos & 0xfff7ffff) << 32;
178 1.1.1.3 christos }
179 1.1.1.3 christos
180 1.1 christos if (regno == -1 || regno == S390_PSWA_REGNUM)
181 1.1.1.3 christos {
182 1.1.1.3 christos regcache_raw_collect (regcache, S390_PSWA_REGNUM, buf);
183 1.1.1.3 christos pswa = extract_unsigned_integer (buf, 4, byte_order);
184 1.1.1.3 christos pswm ^= (pswm ^ pswa) & 0x80000000;
185 1.1.1.3 christos pswa &= 0x7fffffff;
186 1.1.1.3 christos store_unsigned_integer (pswa_p, 8, byte_order, pswa);
187 1.1.1.3 christos }
188 1.1.1.3 christos
189 1.1.1.3 christos store_unsigned_integer (pswm_p, 8, byte_order, pswm);
190 1.1 christos }
191 1.1 christos return;
192 1.1 christos }
193 1.1 christos #endif
194 1.1 christos
195 1.1.1.2 christos regcache_collect_regset (&s390_gregset, regcache, regno, regp,
196 1.1.1.2 christos sizeof (gregset_t));
197 1.1 christos }
198 1.1 christos
199 1.1 christos /* Fill GDB's register array with the floating-point register values
200 1.1 christos in *REGP. */
201 1.1 christos void
202 1.1 christos supply_fpregset (struct regcache *regcache, const fpregset_t *regp)
203 1.1 christos {
204 1.1.1.2 christos regcache_supply_regset (&s390_fpregset, regcache, -1, regp,
205 1.1.1.2 christos sizeof (fpregset_t));
206 1.1 christos }
207 1.1 christos
208 1.1 christos /* Fill register REGNO (if it is a general-purpose register) in
209 1.1 christos *REGP with the value in GDB's register array. If REGNO is -1,
210 1.1 christos do this for all registers. */
211 1.1 christos void
212 1.1 christos fill_fpregset (const struct regcache *regcache, fpregset_t *regp, int regno)
213 1.1 christos {
214 1.1.1.2 christos regcache_collect_regset (&s390_fpregset, regcache, regno, regp,
215 1.1.1.2 christos sizeof (fpregset_t));
216 1.1 christos }
217 1.1 christos
218 1.1 christos /* Find the TID for the current inferior thread to use with ptrace. */
219 1.1 christos static int
220 1.1 christos s390_inferior_tid (void)
221 1.1 christos {
222 1.1 christos /* GNU/Linux LWP ID's are process ID's. */
223 1.1 christos int tid = ptid_get_lwp (inferior_ptid);
224 1.1 christos if (tid == 0)
225 1.1 christos tid = ptid_get_pid (inferior_ptid); /* Not a threaded program. */
226 1.1 christos
227 1.1 christos return tid;
228 1.1 christos }
229 1.1 christos
230 1.1 christos /* Fetch all general-purpose registers from process/thread TID and
231 1.1 christos store their values in GDB's register cache. */
232 1.1 christos static void
233 1.1 christos fetch_regs (struct regcache *regcache, int tid)
234 1.1 christos {
235 1.1 christos gregset_t regs;
236 1.1 christos ptrace_area parea;
237 1.1 christos
238 1.1 christos parea.len = sizeof (regs);
239 1.1 christos parea.process_addr = (addr_t) ®s;
240 1.1 christos parea.kernel_addr = offsetof (struct user_regs_struct, psw);
241 1.1.1.4 christos if (ptrace (PTRACE_PEEKUSR_AREA, tid, (long) &parea, 0) < 0)
242 1.1 christos perror_with_name (_("Couldn't get registers"));
243 1.1 christos
244 1.1 christos supply_gregset (regcache, (const gregset_t *) ®s);
245 1.1 christos }
246 1.1 christos
247 1.1 christos /* Store all valid general-purpose registers in GDB's register cache
248 1.1 christos into the process/thread specified by TID. */
249 1.1 christos static void
250 1.1 christos store_regs (const struct regcache *regcache, int tid, int regnum)
251 1.1 christos {
252 1.1 christos gregset_t regs;
253 1.1 christos ptrace_area parea;
254 1.1 christos
255 1.1 christos parea.len = sizeof (regs);
256 1.1 christos parea.process_addr = (addr_t) ®s;
257 1.1 christos parea.kernel_addr = offsetof (struct user_regs_struct, psw);
258 1.1.1.4 christos if (ptrace (PTRACE_PEEKUSR_AREA, tid, (long) &parea, 0) < 0)
259 1.1 christos perror_with_name (_("Couldn't get registers"));
260 1.1 christos
261 1.1 christos fill_gregset (regcache, ®s, regnum);
262 1.1 christos
263 1.1.1.4 christos if (ptrace (PTRACE_POKEUSR_AREA, tid, (long) &parea, 0) < 0)
264 1.1 christos perror_with_name (_("Couldn't write registers"));
265 1.1 christos }
266 1.1 christos
267 1.1 christos /* Fetch all floating-point registers from process/thread TID and store
268 1.1 christos their values in GDB's register cache. */
269 1.1 christos static void
270 1.1 christos fetch_fpregs (struct regcache *regcache, int tid)
271 1.1 christos {
272 1.1 christos fpregset_t fpregs;
273 1.1 christos ptrace_area parea;
274 1.1 christos
275 1.1 christos parea.len = sizeof (fpregs);
276 1.1 christos parea.process_addr = (addr_t) &fpregs;
277 1.1 christos parea.kernel_addr = offsetof (struct user_regs_struct, fp_regs);
278 1.1.1.4 christos if (ptrace (PTRACE_PEEKUSR_AREA, tid, (long) &parea, 0) < 0)
279 1.1 christos perror_with_name (_("Couldn't get floating point status"));
280 1.1 christos
281 1.1 christos supply_fpregset (regcache, (const fpregset_t *) &fpregs);
282 1.1 christos }
283 1.1 christos
284 1.1 christos /* Store all valid floating-point registers in GDB's register cache
285 1.1 christos into the process/thread specified by TID. */
286 1.1 christos static void
287 1.1 christos store_fpregs (const struct regcache *regcache, int tid, int regnum)
288 1.1 christos {
289 1.1 christos fpregset_t fpregs;
290 1.1 christos ptrace_area parea;
291 1.1 christos
292 1.1 christos parea.len = sizeof (fpregs);
293 1.1 christos parea.process_addr = (addr_t) &fpregs;
294 1.1 christos parea.kernel_addr = offsetof (struct user_regs_struct, fp_regs);
295 1.1.1.4 christos if (ptrace (PTRACE_PEEKUSR_AREA, tid, (long) &parea, 0) < 0)
296 1.1 christos perror_with_name (_("Couldn't get floating point status"));
297 1.1 christos
298 1.1 christos fill_fpregset (regcache, &fpregs, regnum);
299 1.1 christos
300 1.1.1.4 christos if (ptrace (PTRACE_POKEUSR_AREA, tid, (long) &parea, 0) < 0)
301 1.1 christos perror_with_name (_("Couldn't write floating point status"));
302 1.1 christos }
303 1.1 christos
304 1.1.1.2 christos /* Fetch all registers in the kernel's register set whose number is
305 1.1.1.2 christos REGSET_ID, whose size is REGSIZE, and whose layout is described by
306 1.1.1.2 christos REGSET, from process/thread TID and store their values in GDB's
307 1.1.1.2 christos register cache. */
308 1.1 christos static void
309 1.1 christos fetch_regset (struct regcache *regcache, int tid,
310 1.1.1.2 christos int regset_id, int regsize, const struct regset *regset)
311 1.1 christos {
312 1.1.1.4 christos void *buf = alloca (regsize);
313 1.1 christos struct iovec iov;
314 1.1 christos
315 1.1 christos iov.iov_base = buf;
316 1.1 christos iov.iov_len = regsize;
317 1.1 christos
318 1.1.1.2 christos if (ptrace (PTRACE_GETREGSET, tid, (long) regset_id, (long) &iov) < 0)
319 1.1 christos {
320 1.1 christos if (errno == ENODATA)
321 1.1.1.2 christos regcache_supply_regset (regset, regcache, -1, NULL, regsize);
322 1.1 christos else
323 1.1 christos perror_with_name (_("Couldn't get register set"));
324 1.1 christos }
325 1.1 christos else
326 1.1.1.2 christos regcache_supply_regset (regset, regcache, -1, buf, regsize);
327 1.1 christos }
328 1.1 christos
329 1.1.1.2 christos /* Store all registers in the kernel's register set whose number is
330 1.1.1.2 christos REGSET_ID, whose size is REGSIZE, and whose layout is described by
331 1.1.1.2 christos REGSET, from GDB's register cache back to process/thread TID. */
332 1.1 christos static void
333 1.1 christos store_regset (struct regcache *regcache, int tid,
334 1.1.1.2 christos int regset_id, int regsize, const struct regset *regset)
335 1.1 christos {
336 1.1.1.4 christos void *buf = alloca (regsize);
337 1.1 christos struct iovec iov;
338 1.1 christos
339 1.1 christos iov.iov_base = buf;
340 1.1 christos iov.iov_len = regsize;
341 1.1 christos
342 1.1.1.2 christos if (ptrace (PTRACE_GETREGSET, tid, (long) regset_id, (long) &iov) < 0)
343 1.1 christos perror_with_name (_("Couldn't get register set"));
344 1.1 christos
345 1.1.1.2 christos regcache_collect_regset (regset, regcache, -1, buf, regsize);
346 1.1 christos
347 1.1.1.2 christos if (ptrace (PTRACE_SETREGSET, tid, (long) regset_id, (long) &iov) < 0)
348 1.1 christos perror_with_name (_("Couldn't set register set"));
349 1.1 christos }
350 1.1 christos
351 1.1 christos /* Check whether the kernel provides a register set with number REGSET
352 1.1 christos of size REGSIZE for process/thread TID. */
353 1.1 christos static int
354 1.1 christos check_regset (int tid, int regset, int regsize)
355 1.1 christos {
356 1.1.1.4 christos void *buf = alloca (regsize);
357 1.1 christos struct iovec iov;
358 1.1 christos
359 1.1 christos iov.iov_base = buf;
360 1.1 christos iov.iov_len = regsize;
361 1.1 christos
362 1.1 christos if (ptrace (PTRACE_GETREGSET, tid, (long) regset, (long) &iov) >= 0
363 1.1 christos || errno == ENODATA)
364 1.1 christos return 1;
365 1.1 christos return 0;
366 1.1 christos }
367 1.1 christos
368 1.1 christos /* Fetch register REGNUM from the child process. If REGNUM is -1, do
369 1.1 christos this for all registers. */
370 1.1 christos static void
371 1.1 christos s390_linux_fetch_inferior_registers (struct target_ops *ops,
372 1.1 christos struct regcache *regcache, int regnum)
373 1.1 christos {
374 1.1.1.5 christos pid_t tid = get_ptrace_pid (regcache_get_ptid (regcache));
375 1.1 christos
376 1.1 christos if (regnum == -1 || S390_IS_GREGSET_REGNUM (regnum))
377 1.1 christos fetch_regs (regcache, tid);
378 1.1 christos
379 1.1 christos if (regnum == -1 || S390_IS_FPREGSET_REGNUM (regnum))
380 1.1 christos fetch_fpregs (regcache, tid);
381 1.1 christos
382 1.1 christos if (have_regset_last_break)
383 1.1 christos if (regnum == -1 || regnum == S390_LAST_BREAK_REGNUM)
384 1.1 christos fetch_regset (regcache, tid, NT_S390_LAST_BREAK, 8,
385 1.1 christos (gdbarch_ptr_bit (get_regcache_arch (regcache)) == 32
386 1.1.1.2 christos ? &s390_last_break_regset : &s390x_last_break_regset));
387 1.1 christos
388 1.1 christos if (have_regset_system_call)
389 1.1 christos if (regnum == -1 || regnum == S390_SYSTEM_CALL_REGNUM)
390 1.1 christos fetch_regset (regcache, tid, NT_S390_SYSTEM_CALL, 4,
391 1.1.1.2 christos &s390_system_call_regset);
392 1.1 christos
393 1.1 christos if (have_regset_tdb)
394 1.1 christos if (regnum == -1 || S390_IS_TDBREGSET_REGNUM (regnum))
395 1.1 christos fetch_regset (regcache, tid, NT_S390_TDB, s390_sizeof_tdbregset,
396 1.1.1.2 christos &s390_tdb_regset);
397 1.1.1.3 christos
398 1.1.1.3 christos if (have_regset_vxrs)
399 1.1.1.3 christos {
400 1.1.1.3 christos if (regnum == -1 || (regnum >= S390_V0_LOWER_REGNUM
401 1.1.1.3 christos && regnum <= S390_V15_LOWER_REGNUM))
402 1.1.1.3 christos fetch_regset (regcache, tid, NT_S390_VXRS_LOW, 16 * 8,
403 1.1.1.3 christos &s390_vxrs_low_regset);
404 1.1.1.3 christos if (regnum == -1 || (regnum >= S390_V16_REGNUM
405 1.1.1.3 christos && regnum <= S390_V31_REGNUM))
406 1.1.1.3 christos fetch_regset (regcache, tid, NT_S390_VXRS_HIGH, 16 * 16,
407 1.1.1.3 christos &s390_vxrs_high_regset);
408 1.1.1.3 christos }
409 1.1 christos }
410 1.1 christos
411 1.1 christos /* Store register REGNUM back into the child process. If REGNUM is
412 1.1 christos -1, do this for all registers. */
413 1.1 christos static void
414 1.1 christos s390_linux_store_inferior_registers (struct target_ops *ops,
415 1.1 christos struct regcache *regcache, int regnum)
416 1.1 christos {
417 1.1.1.5 christos pid_t tid = get_ptrace_pid (regcache_get_ptid (regcache));
418 1.1 christos
419 1.1 christos if (regnum == -1 || S390_IS_GREGSET_REGNUM (regnum))
420 1.1 christos store_regs (regcache, tid, regnum);
421 1.1 christos
422 1.1 christos if (regnum == -1 || S390_IS_FPREGSET_REGNUM (regnum))
423 1.1 christos store_fpregs (regcache, tid, regnum);
424 1.1 christos
425 1.1 christos /* S390_LAST_BREAK_REGNUM is read-only. */
426 1.1 christos
427 1.1 christos if (have_regset_system_call)
428 1.1 christos if (regnum == -1 || regnum == S390_SYSTEM_CALL_REGNUM)
429 1.1 christos store_regset (regcache, tid, NT_S390_SYSTEM_CALL, 4,
430 1.1.1.2 christos &s390_system_call_regset);
431 1.1.1.3 christos
432 1.1.1.3 christos if (have_regset_vxrs)
433 1.1.1.3 christos {
434 1.1.1.3 christos if (regnum == -1 || (regnum >= S390_V0_LOWER_REGNUM
435 1.1.1.3 christos && regnum <= S390_V15_LOWER_REGNUM))
436 1.1.1.3 christos store_regset (regcache, tid, NT_S390_VXRS_LOW, 16 * 8,
437 1.1.1.3 christos &s390_vxrs_low_regset);
438 1.1.1.3 christos if (regnum == -1 || (regnum >= S390_V16_REGNUM
439 1.1.1.3 christos && regnum <= S390_V31_REGNUM))
440 1.1.1.3 christos store_regset (regcache, tid, NT_S390_VXRS_HIGH, 16 * 16,
441 1.1.1.3 christos &s390_vxrs_high_regset);
442 1.1.1.3 christos }
443 1.1 christos }
444 1.1 christos
445 1.1 christos
446 1.1 christos /* Hardware-assisted watchpoint handling. */
447 1.1 christos
448 1.1.1.5 christos /* For each process we maintain a list of all currently active
449 1.1.1.5 christos watchpoints, in order to properly handle watchpoint removal.
450 1.1 christos
451 1.1 christos The only thing we actually need is the total address space area
452 1.1 christos spanned by the watchpoints. */
453 1.1 christos
454 1.1.1.5 christos typedef struct watch_area
455 1.1 christos {
456 1.1 christos CORE_ADDR lo_addr;
457 1.1 christos CORE_ADDR hi_addr;
458 1.1.1.5 christos } s390_watch_area;
459 1.1.1.5 christos
460 1.1.1.5 christos DEF_VEC_O (s390_watch_area);
461 1.1.1.5 christos
462 1.1.1.5 christos /* Hardware debug state. */
463 1.1.1.5 christos
464 1.1.1.5 christos struct s390_debug_reg_state
465 1.1.1.5 christos {
466 1.1.1.5 christos VEC_s390_watch_area *watch_areas;
467 1.1.1.5 christos VEC_s390_watch_area *break_areas;
468 1.1 christos };
469 1.1 christos
470 1.1.1.5 christos /* Per-process data. */
471 1.1.1.5 christos
472 1.1.1.5 christos struct s390_process_info
473 1.1.1.5 christos {
474 1.1.1.5 christos struct s390_process_info *next;
475 1.1.1.5 christos pid_t pid;
476 1.1.1.5 christos struct s390_debug_reg_state state;
477 1.1.1.5 christos };
478 1.1.1.5 christos
479 1.1.1.5 christos static struct s390_process_info *s390_process_list = NULL;
480 1.1.1.5 christos
481 1.1.1.5 christos /* Find process data for process PID. */
482 1.1.1.5 christos
483 1.1.1.5 christos static struct s390_process_info *
484 1.1.1.5 christos s390_find_process_pid (pid_t pid)
485 1.1.1.5 christos {
486 1.1.1.5 christos struct s390_process_info *proc;
487 1.1.1.5 christos
488 1.1.1.5 christos for (proc = s390_process_list; proc; proc = proc->next)
489 1.1.1.5 christos if (proc->pid == pid)
490 1.1.1.5 christos return proc;
491 1.1.1.5 christos
492 1.1.1.5 christos return NULL;
493 1.1.1.5 christos }
494 1.1.1.5 christos
495 1.1.1.5 christos /* Add process data for process PID. Returns newly allocated info
496 1.1.1.5 christos object. */
497 1.1.1.5 christos
498 1.1.1.5 christos static struct s390_process_info *
499 1.1.1.5 christos s390_add_process (pid_t pid)
500 1.1.1.5 christos {
501 1.1.1.5 christos struct s390_process_info *proc = XCNEW (struct s390_process_info);
502 1.1.1.5 christos
503 1.1.1.5 christos proc->pid = pid;
504 1.1.1.5 christos proc->next = s390_process_list;
505 1.1.1.5 christos s390_process_list = proc;
506 1.1.1.5 christos
507 1.1.1.5 christos return proc;
508 1.1.1.5 christos }
509 1.1.1.5 christos
510 1.1.1.5 christos /* Get data specific info for process PID, creating it if necessary.
511 1.1.1.5 christos Never returns NULL. */
512 1.1.1.5 christos
513 1.1.1.5 christos static struct s390_process_info *
514 1.1.1.5 christos s390_process_info_get (pid_t pid)
515 1.1.1.5 christos {
516 1.1.1.5 christos struct s390_process_info *proc;
517 1.1.1.5 christos
518 1.1.1.5 christos proc = s390_find_process_pid (pid);
519 1.1.1.5 christos if (proc == NULL)
520 1.1.1.5 christos proc = s390_add_process (pid);
521 1.1.1.5 christos
522 1.1.1.5 christos return proc;
523 1.1.1.5 christos }
524 1.1.1.5 christos
525 1.1.1.5 christos /* Get hardware debug state for process PID. */
526 1.1.1.5 christos
527 1.1.1.5 christos static struct s390_debug_reg_state *
528 1.1.1.5 christos s390_get_debug_reg_state (pid_t pid)
529 1.1.1.5 christos {
530 1.1.1.5 christos return &s390_process_info_get (pid)->state;
531 1.1.1.5 christos }
532 1.1.1.5 christos
533 1.1.1.5 christos /* Called whenever GDB is no longer debugging process PID. It deletes
534 1.1.1.5 christos data structures that keep track of hardware debug state. */
535 1.1.1.5 christos
536 1.1.1.5 christos static void
537 1.1.1.5 christos s390_forget_process (pid_t pid)
538 1.1.1.5 christos {
539 1.1.1.5 christos struct s390_process_info *proc, **proc_link;
540 1.1.1.5 christos
541 1.1.1.5 christos proc = s390_process_list;
542 1.1.1.5 christos proc_link = &s390_process_list;
543 1.1.1.5 christos
544 1.1.1.5 christos while (proc != NULL)
545 1.1.1.5 christos {
546 1.1.1.5 christos if (proc->pid == pid)
547 1.1.1.5 christos {
548 1.1.1.5 christos VEC_free (s390_watch_area, proc->state.watch_areas);
549 1.1.1.5 christos VEC_free (s390_watch_area, proc->state.break_areas);
550 1.1.1.5 christos *proc_link = proc->next;
551 1.1.1.5 christos xfree (proc);
552 1.1.1.5 christos return;
553 1.1.1.5 christos }
554 1.1.1.5 christos
555 1.1.1.5 christos proc_link = &proc->next;
556 1.1.1.5 christos proc = *proc_link;
557 1.1.1.5 christos }
558 1.1.1.5 christos }
559 1.1.1.5 christos
560 1.1.1.5 christos /* linux_nat_new_fork hook. */
561 1.1.1.5 christos
562 1.1.1.5 christos static void
563 1.1.1.5 christos s390_linux_new_fork (struct lwp_info *parent, pid_t child_pid)
564 1.1.1.5 christos {
565 1.1.1.5 christos pid_t parent_pid;
566 1.1.1.5 christos struct s390_debug_reg_state *parent_state;
567 1.1.1.5 christos struct s390_debug_reg_state *child_state;
568 1.1.1.5 christos
569 1.1.1.5 christos /* NULL means no watchpoint has ever been set in the parent. In
570 1.1.1.5 christos that case, there's nothing to do. */
571 1.1.1.5 christos if (lwp_arch_private_info (parent) == NULL)
572 1.1.1.5 christos return;
573 1.1.1.5 christos
574 1.1.1.5 christos /* GDB core assumes the child inherits the watchpoints/hw breakpoints of
575 1.1.1.5 christos the parent. So copy the debug state from parent to child. */
576 1.1.1.5 christos
577 1.1.1.5 christos parent_pid = ptid_get_pid (parent->ptid);
578 1.1.1.5 christos parent_state = s390_get_debug_reg_state (parent_pid);
579 1.1.1.5 christos child_state = s390_get_debug_reg_state (child_pid);
580 1.1.1.5 christos
581 1.1.1.5 christos child_state->watch_areas = VEC_copy (s390_watch_area,
582 1.1.1.5 christos parent_state->watch_areas);
583 1.1.1.5 christos child_state->break_areas = VEC_copy (s390_watch_area,
584 1.1.1.5 christos parent_state->break_areas);
585 1.1.1.5 christos }
586 1.1.1.5 christos
587 1.1.1.5 christos /* Dump PER state. */
588 1.1.1.5 christos
589 1.1.1.5 christos static void
590 1.1.1.5 christos s390_show_debug_regs (int tid, const char *where)
591 1.1.1.5 christos {
592 1.1.1.5 christos per_struct per_info;
593 1.1.1.5 christos ptrace_area parea;
594 1.1.1.5 christos
595 1.1.1.5 christos parea.len = sizeof (per_info);
596 1.1.1.5 christos parea.process_addr = (addr_t) &per_info;
597 1.1.1.5 christos parea.kernel_addr = offsetof (struct user_regs_struct, per_info);
598 1.1.1.5 christos
599 1.1.1.5 christos if (ptrace (PTRACE_PEEKUSR_AREA, tid, &parea, 0) < 0)
600 1.1.1.5 christos perror_with_name (_("Couldn't retrieve debug regs"));
601 1.1.1.5 christos
602 1.1.1.5 christos debug_printf ("PER (debug) state for %d -- %s\n"
603 1.1.1.5 christos " cr9-11: %lx %lx %lx\n"
604 1.1.1.5 christos " start, end: %lx %lx\n"
605 1.1.1.5 christos " code/ATMID: %x address: %lx PAID: %x\n",
606 1.1.1.5 christos tid,
607 1.1.1.5 christos where,
608 1.1.1.5 christos per_info.control_regs.words.cr[0],
609 1.1.1.5 christos per_info.control_regs.words.cr[1],
610 1.1.1.5 christos per_info.control_regs.words.cr[2],
611 1.1.1.5 christos per_info.starting_addr,
612 1.1.1.5 christos per_info.ending_addr,
613 1.1.1.5 christos per_info.lowcore.words.perc_atmid,
614 1.1.1.5 christos per_info.lowcore.words.address,
615 1.1.1.5 christos per_info.lowcore.words.access_id);
616 1.1.1.5 christos }
617 1.1 christos
618 1.1 christos static int
619 1.1.1.2 christos s390_stopped_by_watchpoint (struct target_ops *ops)
620 1.1 christos {
621 1.1.1.5 christos struct s390_debug_reg_state *state
622 1.1.1.5 christos = s390_get_debug_reg_state (ptid_get_pid (inferior_ptid));
623 1.1 christos per_lowcore_bits per_lowcore;
624 1.1 christos ptrace_area parea;
625 1.1 christos int result;
626 1.1 christos
627 1.1.1.5 christos if (show_debug_regs)
628 1.1.1.5 christos s390_show_debug_regs (s390_inferior_tid (), "stop");
629 1.1.1.5 christos
630 1.1 christos /* Speed up common case. */
631 1.1.1.5 christos if (VEC_empty (s390_watch_area, state->watch_areas))
632 1.1 christos return 0;
633 1.1 christos
634 1.1 christos parea.len = sizeof (per_lowcore);
635 1.1 christos parea.process_addr = (addr_t) & per_lowcore;
636 1.1 christos parea.kernel_addr = offsetof (struct user_regs_struct, per_info.lowcore);
637 1.1.1.4 christos if (ptrace (PTRACE_PEEKUSR_AREA, s390_inferior_tid (), &parea, 0) < 0)
638 1.1 christos perror_with_name (_("Couldn't retrieve watchpoint status"));
639 1.1 christos
640 1.1 christos result = (per_lowcore.perc_storage_alteration == 1
641 1.1 christos && per_lowcore.perc_store_real_address == 0);
642 1.1 christos
643 1.1 christos if (result)
644 1.1 christos {
645 1.1 christos /* Do not report this watchpoint again. */
646 1.1 christos memset (&per_lowcore, 0, sizeof (per_lowcore));
647 1.1.1.4 christos if (ptrace (PTRACE_POKEUSR_AREA, s390_inferior_tid (), &parea, 0) < 0)
648 1.1 christos perror_with_name (_("Couldn't clear watchpoint status"));
649 1.1 christos }
650 1.1 christos
651 1.1 christos return result;
652 1.1 christos }
653 1.1 christos
654 1.1.1.3 christos /* Each time before resuming a thread, update its PER info. */
655 1.1.1.3 christos
656 1.1 christos static void
657 1.1.1.3 christos s390_prepare_to_resume (struct lwp_info *lp)
658 1.1 christos {
659 1.1 christos int tid;
660 1.1.1.5 christos pid_t pid = ptid_get_pid (ptid_of_lwp (lp));
661 1.1 christos
662 1.1 christos per_struct per_info;
663 1.1 christos ptrace_area parea;
664 1.1 christos
665 1.1 christos CORE_ADDR watch_lo_addr = (CORE_ADDR)-1, watch_hi_addr = 0;
666 1.1.1.5 christos unsigned ix;
667 1.1.1.5 christos s390_watch_area *area;
668 1.1.1.5 christos struct arch_lwp_info *lp_priv = lwp_arch_private_info (lp);
669 1.1.1.5 christos struct s390_debug_reg_state *state = s390_get_debug_reg_state (pid);
670 1.1.1.5 christos int step = lwp_is_stepping (lp);
671 1.1 christos
672 1.1.1.5 christos /* Nothing to do if there was never any PER info for this thread. */
673 1.1.1.5 christos if (lp_priv == NULL)
674 1.1.1.3 christos return;
675 1.1.1.3 christos
676 1.1.1.5 christos /* If PER info has changed, update it. When single-stepping, disable
677 1.1.1.5 christos hardware breakpoints (if any). Otherwise we're done. */
678 1.1.1.5 christos if (!lp_priv->per_info_changed)
679 1.1 christos {
680 1.1.1.5 christos if (!step || VEC_empty (s390_watch_area, state->break_areas))
681 1.1.1.5 christos return;
682 1.1 christos }
683 1.1 christos
684 1.1.1.5 christos lp_priv->per_info_changed = 0;
685 1.1.1.5 christos
686 1.1.1.5 christos tid = ptid_get_lwp (ptid_of_lwp (lp));
687 1.1.1.5 christos if (tid == 0)
688 1.1.1.5 christos tid = pid;
689 1.1.1.5 christos
690 1.1 christos parea.len = sizeof (per_info);
691 1.1 christos parea.process_addr = (addr_t) & per_info;
692 1.1 christos parea.kernel_addr = offsetof (struct user_regs_struct, per_info);
693 1.1 christos
694 1.1.1.5 christos /* Clear PER info, but adjust the single_step field (used by older
695 1.1.1.5 christos kernels only). */
696 1.1.1.5 christos memset (&per_info, 0, sizeof (per_info));
697 1.1.1.5 christos per_info.single_step = (step != 0);
698 1.1.1.5 christos
699 1.1.1.5 christos if (!VEC_empty (s390_watch_area, state->watch_areas))
700 1.1 christos {
701 1.1.1.5 christos for (ix = 0;
702 1.1.1.5 christos VEC_iterate (s390_watch_area, state->watch_areas, ix, area);
703 1.1.1.5 christos ix++)
704 1.1.1.5 christos {
705 1.1.1.5 christos watch_lo_addr = std::min (watch_lo_addr, area->lo_addr);
706 1.1.1.5 christos watch_hi_addr = std::max (watch_hi_addr, area->hi_addr);
707 1.1.1.5 christos }
708 1.1.1.5 christos
709 1.1.1.5 christos /* Enable storage-alteration events. */
710 1.1.1.5 christos per_info.control_regs.words.cr[0] |= (PER_EVENT_STORE
711 1.1.1.5 christos | PER_CONTROL_ALTERATION);
712 1.1 christos }
713 1.1.1.5 christos
714 1.1.1.5 christos if (!VEC_empty (s390_watch_area, state->break_areas))
715 1.1 christos {
716 1.1.1.5 christos /* Don't install hardware breakpoints while single-stepping, since
717 1.1.1.5 christos our PER settings (e.g. the nullification bit) might then conflict
718 1.1.1.5 christos with the kernel's. But re-install them afterwards. */
719 1.1.1.5 christos if (step)
720 1.1.1.5 christos lp_priv->per_info_changed = 1;
721 1.1.1.5 christos else
722 1.1.1.5 christos {
723 1.1.1.5 christos for (ix = 0;
724 1.1.1.5 christos VEC_iterate (s390_watch_area, state->break_areas, ix, area);
725 1.1.1.5 christos ix++)
726 1.1.1.5 christos {
727 1.1.1.5 christos watch_lo_addr = std::min (watch_lo_addr, area->lo_addr);
728 1.1.1.5 christos watch_hi_addr = std::max (watch_hi_addr, area->hi_addr);
729 1.1.1.5 christos }
730 1.1.1.5 christos
731 1.1.1.5 christos /* If there's just one breakpoint, enable instruction-fetching
732 1.1.1.5 christos nullification events for the breakpoint address (fast).
733 1.1.1.5 christos Otherwise stop after any instruction within the PER area and
734 1.1.1.5 christos after any branch into it (slow). */
735 1.1.1.5 christos if (watch_hi_addr == watch_lo_addr)
736 1.1.1.5 christos per_info.control_regs.words.cr[0] |= (PER_EVENT_NULLIFICATION
737 1.1.1.5 christos | PER_EVENT_IFETCH);
738 1.1.1.5 christos else
739 1.1.1.5 christos {
740 1.1.1.5 christos /* The PER area must include the instruction before the
741 1.1.1.5 christos first breakpoint address. */
742 1.1.1.5 christos watch_lo_addr = watch_lo_addr > 6 ? watch_lo_addr - 6 : 0;
743 1.1.1.5 christos per_info.control_regs.words.cr[0]
744 1.1.1.5 christos |= (PER_EVENT_BRANCH
745 1.1.1.5 christos | PER_EVENT_IFETCH
746 1.1.1.5 christos | PER_CONTROL_BRANCH_ADDRESS);
747 1.1.1.5 christos }
748 1.1.1.5 christos }
749 1.1 christos }
750 1.1 christos per_info.starting_addr = watch_lo_addr;
751 1.1 christos per_info.ending_addr = watch_hi_addr;
752 1.1 christos
753 1.1.1.4 christos if (ptrace (PTRACE_POKEUSR_AREA, tid, &parea, 0) < 0)
754 1.1 christos perror_with_name (_("Couldn't modify watchpoint status"));
755 1.1.1.5 christos
756 1.1.1.5 christos if (show_debug_regs)
757 1.1.1.5 christos s390_show_debug_regs (tid, "resume");
758 1.1 christos }
759 1.1 christos
760 1.1.1.5 christos /* Mark the PER info as changed, so the next resume will update it. */
761 1.1.1.3 christos
762 1.1.1.3 christos static void
763 1.1.1.5 christos s390_mark_per_info_changed (struct lwp_info *lp)
764 1.1.1.3 christos {
765 1.1.1.5 christos if (lwp_arch_private_info (lp) == NULL)
766 1.1.1.5 christos lwp_set_arch_private_info (lp, XCNEW (struct arch_lwp_info));
767 1.1.1.3 christos
768 1.1.1.5 christos lwp_arch_private_info (lp)->per_info_changed = 1;
769 1.1.1.3 christos }
770 1.1.1.3 christos
771 1.1.1.3 christos /* When attaching to a new thread, mark its PER info as changed. */
772 1.1.1.3 christos
773 1.1.1.3 christos static void
774 1.1.1.3 christos s390_new_thread (struct lwp_info *lp)
775 1.1.1.3 christos {
776 1.1.1.5 christos s390_mark_per_info_changed (lp);
777 1.1.1.3 christos }
778 1.1.1.3 christos
779 1.1.1.5 christos /* Iterator callback for s390_refresh_per_info. */
780 1.1.1.5 christos
781 1.1 christos static int
782 1.1.1.5 christos s390_refresh_per_info_cb (struct lwp_info *lp, void *arg)
783 1.1 christos {
784 1.1.1.5 christos s390_mark_per_info_changed (lp);
785 1.1 christos
786 1.1.1.5 christos if (!lwp_is_stopped (lp))
787 1.1.1.5 christos linux_stop_lwp (lp);
788 1.1.1.5 christos return 0;
789 1.1.1.5 christos }
790 1.1 christos
791 1.1.1.5 christos /* Make sure that threads are stopped and mark PER info as changed. */
792 1.1 christos
793 1.1.1.5 christos static int
794 1.1.1.5 christos s390_refresh_per_info (void)
795 1.1.1.5 christos {
796 1.1.1.5 christos ptid_t pid_ptid = pid_to_ptid (ptid_get_pid (current_lwp_ptid ()));
797 1.1 christos
798 1.1.1.5 christos iterate_over_lwps (pid_ptid, s390_refresh_per_info_cb, NULL);
799 1.1 christos return 0;
800 1.1 christos }
801 1.1 christos
802 1.1 christos static int
803 1.1.1.5 christos s390_insert_watchpoint (struct target_ops *self,
804 1.1.1.4 christos CORE_ADDR addr, int len, enum target_hw_bp_type type,
805 1.1 christos struct expression *cond)
806 1.1 christos {
807 1.1.1.5 christos s390_watch_area area;
808 1.1.1.5 christos struct s390_debug_reg_state *state
809 1.1.1.5 christos = s390_get_debug_reg_state (ptid_get_pid (inferior_ptid));
810 1.1.1.5 christos
811 1.1.1.5 christos area.lo_addr = addr;
812 1.1.1.5 christos area.hi_addr = addr + len - 1;
813 1.1.1.5 christos VEC_safe_push (s390_watch_area, state->watch_areas, &area);
814 1.1 christos
815 1.1.1.5 christos return s390_refresh_per_info ();
816 1.1.1.5 christos }
817 1.1 christos
818 1.1.1.5 christos static int
819 1.1.1.5 christos s390_remove_watchpoint (struct target_ops *self,
820 1.1.1.5 christos CORE_ADDR addr, int len, enum target_hw_bp_type type,
821 1.1.1.5 christos struct expression *cond)
822 1.1.1.5 christos {
823 1.1.1.5 christos unsigned ix;
824 1.1.1.5 christos s390_watch_area *area;
825 1.1.1.5 christos struct s390_debug_reg_state *state
826 1.1.1.5 christos = s390_get_debug_reg_state (ptid_get_pid (inferior_ptid));
827 1.1.1.5 christos
828 1.1.1.5 christos for (ix = 0;
829 1.1.1.5 christos VEC_iterate (s390_watch_area, state->watch_areas, ix, area);
830 1.1.1.5 christos ix++)
831 1.1 christos {
832 1.1.1.5 christos if (area->lo_addr == addr && area->hi_addr == addr + len - 1)
833 1.1.1.5 christos {
834 1.1.1.5 christos VEC_unordered_remove (s390_watch_area, state->watch_areas, ix);
835 1.1.1.5 christos return s390_refresh_per_info ();
836 1.1.1.5 christos }
837 1.1 christos }
838 1.1 christos
839 1.1.1.5 christos fprintf_unfiltered (gdb_stderr,
840 1.1.1.5 christos "Attempt to remove nonexistent watchpoint.\n");
841 1.1.1.5 christos return -1;
842 1.1 christos }
843 1.1 christos
844 1.1.1.5 christos /* Implement the "can_use_hw_breakpoint" target_ops method. */
845 1.1.1.5 christos
846 1.1 christos static int
847 1.1.1.2 christos s390_can_use_hw_breakpoint (struct target_ops *self,
848 1.1.1.4 christos enum bptype type, int cnt, int othertype)
849 1.1 christos {
850 1.1.1.5 christos if (type == bp_hardware_watchpoint || type == bp_hardware_breakpoint)
851 1.1.1.5 christos return 1;
852 1.1.1.5 christos return 0;
853 1.1.1.5 christos }
854 1.1.1.5 christos
855 1.1.1.5 christos /* Implement the "insert_hw_breakpoint" target_ops method. */
856 1.1.1.5 christos
857 1.1.1.5 christos static int
858 1.1.1.5 christos s390_insert_hw_breakpoint (struct target_ops *self,
859 1.1.1.5 christos struct gdbarch *gdbarch,
860 1.1.1.5 christos struct bp_target_info *bp_tgt)
861 1.1.1.5 christos {
862 1.1.1.5 christos s390_watch_area area;
863 1.1.1.5 christos struct s390_debug_reg_state *state;
864 1.1.1.5 christos
865 1.1.1.5 christos area.lo_addr = bp_tgt->placed_address = bp_tgt->reqstd_address;
866 1.1.1.5 christos area.hi_addr = area.lo_addr;
867 1.1.1.5 christos state = s390_get_debug_reg_state (ptid_get_pid (inferior_ptid));
868 1.1.1.5 christos VEC_safe_push (s390_watch_area, state->break_areas, &area);
869 1.1.1.5 christos
870 1.1.1.5 christos return s390_refresh_per_info ();
871 1.1.1.5 christos }
872 1.1.1.5 christos
873 1.1.1.5 christos /* Implement the "remove_hw_breakpoint" target_ops method. */
874 1.1.1.5 christos
875 1.1.1.5 christos static int
876 1.1.1.5 christos s390_remove_hw_breakpoint (struct target_ops *self,
877 1.1.1.5 christos struct gdbarch *gdbarch,
878 1.1.1.5 christos struct bp_target_info *bp_tgt)
879 1.1.1.5 christos {
880 1.1.1.5 christos unsigned ix;
881 1.1.1.5 christos struct watch_area *area;
882 1.1.1.5 christos struct s390_debug_reg_state *state;
883 1.1.1.5 christos
884 1.1.1.5 christos state = s390_get_debug_reg_state (ptid_get_pid (inferior_ptid));
885 1.1.1.5 christos for (ix = 0;
886 1.1.1.5 christos VEC_iterate (s390_watch_area, state->break_areas, ix, area);
887 1.1.1.5 christos ix++)
888 1.1.1.5 christos {
889 1.1.1.5 christos if (area->lo_addr == bp_tgt->placed_address)
890 1.1.1.5 christos {
891 1.1.1.5 christos VEC_unordered_remove (s390_watch_area, state->break_areas, ix);
892 1.1.1.5 christos return s390_refresh_per_info ();
893 1.1.1.5 christos }
894 1.1.1.5 christos }
895 1.1.1.5 christos
896 1.1.1.5 christos fprintf_unfiltered (gdb_stderr,
897 1.1.1.5 christos "Attempt to remove nonexistent breakpoint.\n");
898 1.1.1.5 christos return -1;
899 1.1 christos }
900 1.1 christos
901 1.1 christos static int
902 1.1.1.2 christos s390_region_ok_for_hw_watchpoint (struct target_ops *self,
903 1.1.1.2 christos CORE_ADDR addr, int cnt)
904 1.1 christos {
905 1.1 christos return 1;
906 1.1 christos }
907 1.1 christos
908 1.1 christos static int
909 1.1 christos s390_target_wordsize (void)
910 1.1 christos {
911 1.1 christos int wordsize = 4;
912 1.1 christos
913 1.1 christos /* Check for 64-bit inferior process. This is the case when the host is
914 1.1 christos 64-bit, and in addition bit 32 of the PSW mask is set. */
915 1.1 christos #ifdef __s390x__
916 1.1 christos long pswm;
917 1.1 christos
918 1.1 christos errno = 0;
919 1.1 christos pswm = (long) ptrace (PTRACE_PEEKUSER, s390_inferior_tid (), PT_PSWMASK, 0);
920 1.1 christos if (errno == 0 && (pswm & 0x100000000ul) != 0)
921 1.1 christos wordsize = 8;
922 1.1 christos #endif
923 1.1 christos
924 1.1 christos return wordsize;
925 1.1 christos }
926 1.1 christos
927 1.1 christos static int
928 1.1 christos s390_auxv_parse (struct target_ops *ops, gdb_byte **readptr,
929 1.1 christos gdb_byte *endptr, CORE_ADDR *typep, CORE_ADDR *valp)
930 1.1 christos {
931 1.1 christos int sizeof_auxv_field = s390_target_wordsize ();
932 1.1 christos enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
933 1.1 christos gdb_byte *ptr = *readptr;
934 1.1 christos
935 1.1 christos if (endptr == ptr)
936 1.1 christos return 0;
937 1.1 christos
938 1.1 christos if (endptr - ptr < sizeof_auxv_field * 2)
939 1.1 christos return -1;
940 1.1 christos
941 1.1 christos *typep = extract_unsigned_integer (ptr, sizeof_auxv_field, byte_order);
942 1.1 christos ptr += sizeof_auxv_field;
943 1.1 christos *valp = extract_unsigned_integer (ptr, sizeof_auxv_field, byte_order);
944 1.1 christos ptr += sizeof_auxv_field;
945 1.1 christos
946 1.1 christos *readptr = ptr;
947 1.1 christos return 1;
948 1.1 christos }
949 1.1 christos
950 1.1 christos static const struct target_desc *
951 1.1 christos s390_read_description (struct target_ops *ops)
952 1.1 christos {
953 1.1 christos int tid = s390_inferior_tid ();
954 1.1 christos
955 1.1 christos have_regset_last_break
956 1.1 christos = check_regset (tid, NT_S390_LAST_BREAK, 8);
957 1.1 christos have_regset_system_call
958 1.1 christos = check_regset (tid, NT_S390_SYSTEM_CALL, 4);
959 1.1 christos
960 1.1 christos /* If GDB itself is compiled as 64-bit, we are running on a machine in
961 1.1 christos z/Architecture mode. If the target is running in 64-bit addressing
962 1.1 christos mode, report s390x architecture. If the target is running in 31-bit
963 1.1 christos addressing mode, but the kernel supports using 64-bit registers in
964 1.1 christos that mode, report s390 architecture with 64-bit GPRs. */
965 1.1.1.3 christos #ifdef __s390x__
966 1.1.1.3 christos {
967 1.1.1.3 christos CORE_ADDR hwcap = 0;
968 1.1 christos
969 1.1.1.3 christos target_auxv_search (¤t_target, AT_HWCAP, &hwcap);
970 1.1.1.3 christos have_regset_tdb = (hwcap & HWCAP_S390_TE)
971 1.1.1.3 christos && check_regset (tid, NT_S390_TDB, s390_sizeof_tdbregset);
972 1.1.1.3 christos
973 1.1.1.3 christos have_regset_vxrs = (hwcap & HWCAP_S390_VX)
974 1.1.1.3 christos && check_regset (tid, NT_S390_VXRS_LOW, 16 * 8)
975 1.1.1.3 christos && check_regset (tid, NT_S390_VXRS_HIGH, 16 * 16);
976 1.1.1.3 christos
977 1.1.1.3 christos if (s390_target_wordsize () == 8)
978 1.1.1.3 christos return (have_regset_vxrs ?
979 1.1.1.3 christos (have_regset_tdb ? tdesc_s390x_tevx_linux64 :
980 1.1.1.3 christos tdesc_s390x_vx_linux64) :
981 1.1.1.3 christos have_regset_tdb ? tdesc_s390x_te_linux64 :
982 1.1.1.3 christos have_regset_system_call ? tdesc_s390x_linux64v2 :
983 1.1.1.3 christos have_regset_last_break ? tdesc_s390x_linux64v1 :
984 1.1.1.3 christos tdesc_s390x_linux64);
985 1.1.1.3 christos
986 1.1.1.3 christos if (hwcap & HWCAP_S390_HIGH_GPRS)
987 1.1.1.3 christos return (have_regset_vxrs ?
988 1.1.1.3 christos (have_regset_tdb ? tdesc_s390_tevx_linux64 :
989 1.1.1.3 christos tdesc_s390_vx_linux64) :
990 1.1.1.3 christos have_regset_tdb ? tdesc_s390_te_linux64 :
991 1.1.1.3 christos have_regset_system_call ? tdesc_s390_linux64v2 :
992 1.1.1.3 christos have_regset_last_break ? tdesc_s390_linux64v1 :
993 1.1.1.3 christos tdesc_s390_linux64);
994 1.1.1.3 christos }
995 1.1 christos #endif
996 1.1 christos
997 1.1 christos /* If GDB itself is compiled as 31-bit, or if we're running a 31-bit inferior
998 1.1 christos on a 64-bit kernel that does not support using 64-bit registers in 31-bit
999 1.1 christos mode, report s390 architecture with 32-bit GPRs. */
1000 1.1 christos return (have_regset_system_call? tdesc_s390_linux32v2 :
1001 1.1 christos have_regset_last_break? tdesc_s390_linux32v1 :
1002 1.1 christos tdesc_s390_linux32);
1003 1.1 christos }
1004 1.1 christos
1005 1.1 christos void _initialize_s390_nat (void);
1006 1.1 christos
1007 1.1 christos void
1008 1.1 christos _initialize_s390_nat (void)
1009 1.1 christos {
1010 1.1 christos struct target_ops *t;
1011 1.1 christos
1012 1.1 christos /* Fill in the generic GNU/Linux methods. */
1013 1.1 christos t = linux_target ();
1014 1.1 christos
1015 1.1 christos /* Add our register access methods. */
1016 1.1 christos t->to_fetch_registers = s390_linux_fetch_inferior_registers;
1017 1.1 christos t->to_store_registers = s390_linux_store_inferior_registers;
1018 1.1 christos
1019 1.1 christos /* Add our watchpoint methods. */
1020 1.1 christos t->to_can_use_hw_breakpoint = s390_can_use_hw_breakpoint;
1021 1.1.1.5 christos t->to_insert_hw_breakpoint = s390_insert_hw_breakpoint;
1022 1.1.1.5 christos t->to_remove_hw_breakpoint = s390_remove_hw_breakpoint;
1023 1.1 christos t->to_region_ok_for_hw_watchpoint = s390_region_ok_for_hw_watchpoint;
1024 1.1 christos t->to_have_continuable_watchpoint = 1;
1025 1.1 christos t->to_stopped_by_watchpoint = s390_stopped_by_watchpoint;
1026 1.1 christos t->to_insert_watchpoint = s390_insert_watchpoint;
1027 1.1 christos t->to_remove_watchpoint = s390_remove_watchpoint;
1028 1.1 christos
1029 1.1 christos /* Detect target architecture. */
1030 1.1 christos t->to_read_description = s390_read_description;
1031 1.1 christos t->to_auxv_parse = s390_auxv_parse;
1032 1.1 christos
1033 1.1 christos /* Register the target. */
1034 1.1 christos linux_nat_add_target (t);
1035 1.1.1.3 christos linux_nat_set_new_thread (t, s390_new_thread);
1036 1.1.1.3 christos linux_nat_set_prepare_to_resume (t, s390_prepare_to_resume);
1037 1.1.1.5 christos linux_nat_set_forget_process (t, s390_forget_process);
1038 1.1.1.5 christos linux_nat_set_new_fork (t, s390_linux_new_fork);
1039 1.1.1.5 christos
1040 1.1.1.5 christos /* A maintenance command to enable showing the PER state. */
1041 1.1.1.5 christos add_setshow_boolean_cmd ("show-debug-regs", class_maintenance,
1042 1.1.1.5 christos &show_debug_regs, _("\
1043 1.1.1.5 christos Set whether to show the PER (debug) hardware state."), _("\
1044 1.1.1.5 christos Show whether to show the PER (debug) hardware state."), _("\
1045 1.1.1.5 christos Use \"on\" to enable, \"off\" to disable.\n\
1046 1.1.1.5 christos If enabled, the PER state is shown after it is changed by GDB,\n\
1047 1.1.1.5 christos and when the inferior triggers a breakpoint or watchpoint."),
1048 1.1.1.5 christos NULL,
1049 1.1.1.5 christos NULL,
1050 1.1.1.5 christos &maintenance_set_cmdlist,
1051 1.1.1.5 christos &maintenance_show_cmdlist);
1052 1.1 christos }
1053