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