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