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