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