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