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