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