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