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