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