Fix null pointer dereference in process_debug_info()
[binutils-gdb.git] / gdb / s390-linux-nat.c
blobfa4aaf9aa9aeabc87c122fd75c0bce2b80e5107b
1 /* S390 native-dependent code for GDB, the GNU debugger.
2 Copyright (C) 2001-2024 Free Software Foundation, Inc.
4 Contributed by D.J. Barrow (djbarrow@de.ibm.com,barrow_dj@yahoo.com)
5 for IBM Deutschland Entwicklung GmbH, IBM Corporation.
7 This file is part of GDB.
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
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
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.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
22 #include "regcache.h"
23 #include "inferior.h"
24 #include "target.h"
25 #include "linux-nat.h"
26 #include "auxv.h"
27 #include "gregset.h"
28 #include "regset.h"
29 #include "nat/linux-ptrace.h"
30 #include "gdbcmd.h"
31 #include "gdbarch.h"
33 #include "s390-tdep.h"
34 #include "s390-linux-tdep.h"
35 #include "elf/common.h"
37 #include <asm/ptrace.h>
38 #include "nat/gdb_ptrace.h"
39 #include <asm/types.h>
40 #include <sys/procfs.h>
41 #include <sys/ucontext.h>
42 #include <elf.h>
43 #include <algorithm>
44 #include "inf-ptrace.h"
45 #include "linux-tdep.h"
47 /* Per-thread arch-specific data. */
49 struct arch_lwp_info
51 /* Non-zero if the thread's PER info must be re-written. */
52 int per_info_changed;
55 static int have_regset_last_break = 0;
56 static int have_regset_system_call = 0;
57 static int have_regset_tdb = 0;
58 static int have_regset_vxrs = 0;
59 static int have_regset_gs = 0;
61 /* Register map for 32-bit executables running under a 64-bit
62 kernel. */
64 #ifdef __s390x__
65 static const struct regcache_map_entry s390_64_regmap_gregset[] =
67 /* Skip PSWM and PSWA, since they must be handled specially. */
68 { 2, REGCACHE_MAP_SKIP, 8 },
69 { 1, S390_R0_UPPER_REGNUM, 4 }, { 1, S390_R0_REGNUM, 4 },
70 { 1, S390_R1_UPPER_REGNUM, 4 }, { 1, S390_R1_REGNUM, 4 },
71 { 1, S390_R2_UPPER_REGNUM, 4 }, { 1, S390_R2_REGNUM, 4 },
72 { 1, S390_R3_UPPER_REGNUM, 4 }, { 1, S390_R3_REGNUM, 4 },
73 { 1, S390_R4_UPPER_REGNUM, 4 }, { 1, S390_R4_REGNUM, 4 },
74 { 1, S390_R5_UPPER_REGNUM, 4 }, { 1, S390_R5_REGNUM, 4 },
75 { 1, S390_R6_UPPER_REGNUM, 4 }, { 1, S390_R6_REGNUM, 4 },
76 { 1, S390_R7_UPPER_REGNUM, 4 }, { 1, S390_R7_REGNUM, 4 },
77 { 1, S390_R8_UPPER_REGNUM, 4 }, { 1, S390_R8_REGNUM, 4 },
78 { 1, S390_R9_UPPER_REGNUM, 4 }, { 1, S390_R9_REGNUM, 4 },
79 { 1, S390_R10_UPPER_REGNUM, 4 }, { 1, S390_R10_REGNUM, 4 },
80 { 1, S390_R11_UPPER_REGNUM, 4 }, { 1, S390_R11_REGNUM, 4 },
81 { 1, S390_R12_UPPER_REGNUM, 4 }, { 1, S390_R12_REGNUM, 4 },
82 { 1, S390_R13_UPPER_REGNUM, 4 }, { 1, S390_R13_REGNUM, 4 },
83 { 1, S390_R14_UPPER_REGNUM, 4 }, { 1, S390_R14_REGNUM, 4 },
84 { 1, S390_R15_UPPER_REGNUM, 4 }, { 1, S390_R15_REGNUM, 4 },
85 { 16, S390_A0_REGNUM, 4 },
86 { 1, REGCACHE_MAP_SKIP, 4 }, { 1, S390_ORIG_R2_REGNUM, 4 },
87 { 0 }
90 static const struct regset s390_64_gregset =
92 s390_64_regmap_gregset,
93 regcache_supply_regset,
94 regcache_collect_regset
97 #define S390_PSWM_OFFSET 0
98 #define S390_PSWA_OFFSET 8
99 #endif
101 /* PER-event mask bits and PER control bits (CR9). */
103 #define PER_BIT(n) (1UL << (63 - (n)))
104 #define PER_EVENT_BRANCH PER_BIT (32)
105 #define PER_EVENT_IFETCH PER_BIT (33)
106 #define PER_EVENT_STORE PER_BIT (34)
107 #define PER_EVENT_NULLIFICATION PER_BIT (39)
108 #define PER_CONTROL_BRANCH_ADDRESS PER_BIT (40)
109 #define PER_CONTROL_SUSPENSION PER_BIT (41)
110 #define PER_CONTROL_ALTERATION PER_BIT (42)
112 class s390_linux_nat_target final : public linux_nat_target
114 public:
115 /* Add our register access methods. */
116 void fetch_registers (struct regcache *, int) override;
117 void store_registers (struct regcache *, int) override;
119 /* Add our watchpoint methods. */
120 int can_use_hw_breakpoint (enum bptype, int, int) override;
121 int insert_hw_breakpoint (struct gdbarch *, struct bp_target_info *)
122 override;
123 int remove_hw_breakpoint (struct gdbarch *, struct bp_target_info *)
124 override;
125 int region_ok_for_hw_watchpoint (CORE_ADDR, int) override;
126 bool stopped_by_watchpoint () override;
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;
132 /* Detect target architecture. */
133 const struct target_desc *read_description () override;
134 int auxv_parse (const gdb_byte **readptr,
135 const gdb_byte *endptr, CORE_ADDR *typep, CORE_ADDR *valp)
136 override;
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;
146 static s390_linux_nat_target the_s390_linux_nat_target;
148 /* Fill GDB's register array with the general-purpose register values
149 in *REGP.
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. */
155 void
156 supply_gregset (struct regcache *regcache, const gregset_t *regp)
158 #ifdef __s390x__
159 struct gdbarch *gdbarch = regcache->arch ();
160 if (gdbarch_ptr_bit (gdbarch) == 32)
162 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
163 ULONGEST pswm, pswa;
164 gdb_byte buf[4];
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);
172 store_unsigned_integer (buf, 4, byte_order, (pswm >> 32) | 0x80000);
173 regcache->raw_supply (S390_PSWM_REGNUM, buf);
174 store_unsigned_integer (buf, 4, byte_order,
175 (pswa & 0x7fffffff) | (pswm & 0x80000000));
176 regcache->raw_supply (S390_PSWA_REGNUM, buf);
177 return;
179 #endif
181 regcache_supply_regset (&s390_gregset, regcache, -1, regp,
182 sizeof (gregset_t));
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. */
189 void
190 fill_gregset (const struct regcache *regcache, gregset_t *regp, int regno)
192 #ifdef __s390x__
193 struct gdbarch *gdbarch = regcache->arch ();
194 if (gdbarch_ptr_bit (gdbarch) == 32)
196 regcache_collect_regset (&s390_64_gregset, regcache, regno,
197 regp, sizeof (gregset_t));
199 if (regno == -1
200 || regno == S390_PSWM_REGNUM || regno == S390_PSWA_REGNUM)
202 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
203 ULONGEST pswa, pswm;
204 gdb_byte buf[4];
205 gdb_byte *pswm_p = (gdb_byte *) regp + S390_PSWM_OFFSET;
206 gdb_byte *pswa_p = (gdb_byte *) regp + S390_PSWA_OFFSET;
208 pswm = extract_unsigned_integer (pswm_p, 8, byte_order);
210 if (regno == -1 || regno == S390_PSWM_REGNUM)
212 pswm &= 0x80000000;
213 regcache->raw_collect (S390_PSWM_REGNUM, buf);
214 pswm |= (extract_unsigned_integer (buf, 4, byte_order)
215 & 0xfff7ffff) << 32;
218 if (regno == -1 || regno == S390_PSWA_REGNUM)
220 regcache->raw_collect (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);
227 store_unsigned_integer (pswm_p, 8, byte_order, pswm);
229 return;
231 #endif
233 regcache_collect_regset (&s390_gregset, regcache, regno, regp,
234 sizeof (gregset_t));
237 /* Fill GDB's register array with the floating-point register values
238 in *REGP. */
239 void
240 supply_fpregset (struct regcache *regcache, const fpregset_t *regp)
242 regcache_supply_regset (&s390_fpregset, regcache, -1, regp,
243 sizeof (fpregset_t));
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. */
249 void
250 fill_fpregset (const struct regcache *regcache, fpregset_t *regp, int regno)
252 regcache_collect_regset (&s390_fpregset, regcache, regno, regp,
253 sizeof (fpregset_t));
256 /* Find the TID for the current inferior thread to use with ptrace. */
257 static int
258 s390_inferior_tid (void)
260 /* GNU/Linux LWP ID's are process ID's. */
261 int tid = inferior_ptid.lwp ();
262 if (tid == 0)
263 tid = inferior_ptid.pid (); /* Not a threaded program. */
265 return tid;
268 /* Fetch all general-purpose registers from process/thread TID and
269 store their values in GDB's register cache. */
270 static void
271 fetch_regs (struct regcache *regcache, int tid)
273 gregset_t regs;
274 ptrace_area parea;
276 parea.len = sizeof (regs);
277 parea.process_addr = (addr_t) &regs;
278 parea.kernel_addr = offsetof (struct user_regs_struct, psw);
279 if (ptrace (PTRACE_PEEKUSR_AREA, tid, (long) &parea, 0) < 0)
280 perror_with_name (_("Couldn't get registers"));
282 supply_gregset (regcache, (const gregset_t *) &regs);
285 /* Store all valid general-purpose registers in GDB's register cache
286 into the process/thread specified by TID. */
287 static void
288 store_regs (const struct regcache *regcache, int tid, int regnum)
290 gregset_t regs;
291 ptrace_area parea;
293 parea.len = sizeof (regs);
294 parea.process_addr = (addr_t) &regs;
295 parea.kernel_addr = offsetof (struct user_regs_struct, psw);
296 if (ptrace (PTRACE_PEEKUSR_AREA, tid, (long) &parea, 0) < 0)
297 perror_with_name (_("Couldn't get registers"));
299 fill_gregset (regcache, &regs, regnum);
301 if (ptrace (PTRACE_POKEUSR_AREA, tid, (long) &parea, 0) < 0)
302 perror_with_name (_("Couldn't write registers"));
305 /* Fetch all floating-point registers from process/thread TID and store
306 their values in GDB's register cache. */
307 static void
308 fetch_fpregs (struct regcache *regcache, int tid)
310 fpregset_t fpregs;
311 ptrace_area parea;
313 parea.len = sizeof (fpregs);
314 parea.process_addr = (addr_t) &fpregs;
315 parea.kernel_addr = offsetof (struct user_regs_struct, fp_regs);
316 if (ptrace (PTRACE_PEEKUSR_AREA, tid, (long) &parea, 0) < 0)
317 perror_with_name (_("Couldn't get floating point status"));
319 supply_fpregset (regcache, (const fpregset_t *) &fpregs);
322 /* Store all valid floating-point registers in GDB's register cache
323 into the process/thread specified by TID. */
324 static void
325 store_fpregs (const struct regcache *regcache, int tid, int regnum)
327 fpregset_t fpregs;
328 ptrace_area parea;
330 parea.len = sizeof (fpregs);
331 parea.process_addr = (addr_t) &fpregs;
332 parea.kernel_addr = offsetof (struct user_regs_struct, fp_regs);
333 if (ptrace (PTRACE_PEEKUSR_AREA, tid, (long) &parea, 0) < 0)
334 perror_with_name (_("Couldn't get floating point status"));
336 fill_fpregset (regcache, &fpregs, regnum);
338 if (ptrace (PTRACE_POKEUSR_AREA, tid, (long) &parea, 0) < 0)
339 perror_with_name (_("Couldn't write floating point status"));
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. */
346 static void
347 fetch_regset (struct regcache *regcache, int tid,
348 int regset_id, int regsize, const struct regset *regset)
350 void *buf = alloca (regsize);
351 struct iovec iov;
353 iov.iov_base = buf;
354 iov.iov_len = regsize;
356 if (ptrace (PTRACE_GETREGSET, tid, (long) regset_id, (long) &iov) < 0)
358 if (errno == ENODATA)
359 regcache_supply_regset (regset, regcache, -1, NULL, regsize);
360 else
361 perror_with_name (_("Couldn't get register set"));
363 else
364 regcache_supply_regset (regset, regcache, -1, buf, regsize);
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. */
370 static void
371 store_regset (struct regcache *regcache, int tid,
372 int regset_id, int regsize, const struct regset *regset)
374 void *buf = alloca (regsize);
375 struct iovec iov;
377 iov.iov_base = buf;
378 iov.iov_len = regsize;
380 if (ptrace (PTRACE_GETREGSET, tid, (long) regset_id, (long) &iov) < 0)
381 perror_with_name (_("Couldn't get register set"));
383 regcache_collect_regset (regset, regcache, -1, buf, regsize);
385 if (ptrace (PTRACE_SETREGSET, tid, (long) regset_id, (long) &iov) < 0)
386 perror_with_name (_("Couldn't set register set"));
389 /* Check whether the kernel provides a register set with number REGSET
390 of size REGSIZE for process/thread TID. */
391 static int
392 check_regset (int tid, int regset, int regsize)
394 void *buf = alloca (regsize);
395 struct iovec iov;
397 iov.iov_base = buf;
398 iov.iov_len = regsize;
400 if (ptrace (PTRACE_GETREGSET, tid, (long) regset, (long) &iov) >= 0
401 || errno == ENODATA)
402 return 1;
403 return 0;
406 /* Fetch register REGNUM from the child process. If REGNUM is -1, do
407 this for all registers. */
408 void
409 s390_linux_nat_target::fetch_registers (struct regcache *regcache, int regnum)
411 pid_t tid = get_ptrace_pid (regcache->ptid ());
413 if (regnum == -1 || S390_IS_GREGSET_REGNUM (regnum))
414 fetch_regs (regcache, tid);
416 if (regnum == -1 || S390_IS_FPREGSET_REGNUM (regnum))
417 fetch_fpregs (regcache, tid);
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,
422 (gdbarch_ptr_bit (regcache->arch ()) == 32
423 ? &s390_last_break_regset : &s390x_last_break_regset));
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,
428 &s390_system_call_regset);
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,
433 &s390_tdb_regset);
435 if (have_regset_vxrs)
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);
447 if (have_regset_gs)
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);
460 /* Store register REGNUM back into the child process. If REGNUM is
461 -1, do this for all registers. */
462 void
463 s390_linux_nat_target::store_registers (struct regcache *regcache, int regnum)
465 pid_t tid = get_ptrace_pid (regcache->ptid ());
467 if (regnum == -1 || S390_IS_GREGSET_REGNUM (regnum))
468 store_regs (regcache, tid, regnum);
470 if (regnum == -1 || S390_IS_FPREGSET_REGNUM (regnum))
471 store_fpregs (regcache, tid, regnum);
473 /* S390_LAST_BREAK_REGNUM is read-only. */
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,
478 &s390_system_call_regset);
480 if (have_regset_vxrs)
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);
494 /* Hardware-assisted watchpoint handling. */
496 /* For each process we maintain a list of all currently active
497 watchpoints, in order to properly handle watchpoint removal.
499 The only thing we actually need is the total address space area
500 spanned by the watchpoints. */
502 struct watch_area
504 CORE_ADDR lo_addr;
505 CORE_ADDR hi_addr;
508 /* Hardware debug state. */
510 struct s390_debug_reg_state
512 std::vector<watch_area> watch_areas;
513 std::vector<watch_area> break_areas;
516 /* Per-process data. */
518 struct s390_process_info
520 struct s390_process_info *next = nullptr;
521 pid_t pid = 0;
522 struct s390_debug_reg_state state;
525 static struct s390_process_info *s390_process_list = NULL;
527 /* Find process data for process PID. */
529 static struct s390_process_info *
530 s390_find_process_pid (pid_t pid)
532 struct s390_process_info *proc;
534 for (proc = s390_process_list; proc; proc = proc->next)
535 if (proc->pid == pid)
536 return proc;
538 return NULL;
541 /* Add process data for process PID. Returns newly allocated info
542 object. */
544 static struct s390_process_info *
545 s390_add_process (pid_t pid)
547 struct s390_process_info *proc = new struct s390_process_info;
549 proc->pid = pid;
550 proc->next = s390_process_list;
551 s390_process_list = proc;
553 return proc;
556 /* Get data specific info for process PID, creating it if necessary.
557 Never returns NULL. */
559 static struct s390_process_info *
560 s390_process_info_get (pid_t pid)
562 struct s390_process_info *proc;
564 proc = s390_find_process_pid (pid);
565 if (proc == NULL)
566 proc = s390_add_process (pid);
568 return proc;
571 /* Get hardware debug state for process PID. */
573 static struct s390_debug_reg_state *
574 s390_get_debug_reg_state (pid_t pid)
576 return &s390_process_info_get (pid)->state;
579 /* Called whenever GDB is no longer debugging process PID. It deletes
580 data structures that keep track of hardware debug state. */
582 void
583 s390_linux_nat_target::low_forget_process (pid_t pid)
585 struct s390_process_info *proc, **proc_link;
587 proc = s390_process_list;
588 proc_link = &s390_process_list;
590 while (proc != NULL)
592 if (proc->pid == pid)
594 *proc_link = proc->next;
595 delete proc;
596 return;
599 proc_link = &proc->next;
600 proc = *proc_link;
604 /* linux_nat_new_fork hook. */
606 void
607 s390_linux_nat_target::low_new_fork (struct lwp_info *parent, pid_t child_pid)
609 pid_t parent_pid;
610 struct s390_debug_reg_state *parent_state;
611 struct s390_debug_reg_state *child_state;
613 /* NULL means no watchpoint has ever been set in the parent. In
614 that case, there's nothing to do. */
615 if (lwp_arch_private_info (parent) == NULL)
616 return;
618 /* GDB core assumes the child inherits the watchpoints/hw breakpoints of
619 the parent. So copy the debug state from parent to child. */
621 parent_pid = parent->ptid.pid ();
622 parent_state = s390_get_debug_reg_state (parent_pid);
623 child_state = s390_get_debug_reg_state (child_pid);
625 child_state->watch_areas = parent_state->watch_areas;
626 child_state->break_areas = parent_state->break_areas;
629 /* Dump PER state. */
631 static void
632 s390_show_debug_regs (int tid, const char *where)
634 per_struct per_info;
635 ptrace_area parea;
637 parea.len = sizeof (per_info);
638 parea.process_addr = (addr_t) &per_info;
639 parea.kernel_addr = offsetof (struct user_regs_struct, per_info);
641 if (ptrace (PTRACE_PEEKUSR_AREA, tid, &parea, 0) < 0)
642 perror_with_name (_("Couldn't retrieve debug regs"));
644 debug_printf ("PER (debug) state for %d -- %s\n"
645 " cr9-11: %lx %lx %lx\n"
646 " start, end: %lx %lx\n"
647 " code/ATMID: %x address: %lx PAID: %x\n",
648 tid,
649 where,
650 per_info.control_regs.words.cr[0],
651 per_info.control_regs.words.cr[1],
652 per_info.control_regs.words.cr[2],
653 per_info.starting_addr,
654 per_info.ending_addr,
655 per_info.lowcore.words.perc_atmid,
656 per_info.lowcore.words.address,
657 per_info.lowcore.words.access_id);
660 bool
661 s390_linux_nat_target::stopped_by_watchpoint ()
663 struct s390_debug_reg_state *state
664 = s390_get_debug_reg_state (inferior_ptid.pid ());
665 per_lowcore_bits per_lowcore;
666 ptrace_area parea;
668 if (show_debug_regs)
669 s390_show_debug_regs (s390_inferior_tid (), "stop");
671 /* Speed up common case. */
672 if (state->watch_areas.empty ())
673 return false;
675 siginfo_t siginfo;
676 if (!linux_nat_get_siginfo (inferior_ptid, &siginfo))
677 return false;
678 if (siginfo.si_signo != SIGTRAP
679 || (siginfo.si_code & 0xffff) != TRAP_HWBKPT)
680 return false;
682 parea.len = sizeof (per_lowcore);
683 parea.process_addr = (addr_t) & per_lowcore;
684 parea.kernel_addr = offsetof (struct user_regs_struct, per_info.lowcore);
685 if (ptrace (PTRACE_PEEKUSR_AREA, s390_inferior_tid (), &parea, 0) < 0)
686 perror_with_name (_("Couldn't retrieve watchpoint status"));
688 bool result = (per_lowcore.perc_storage_alteration == 1
689 && per_lowcore.perc_store_real_address == 0);
691 return result;
694 /* Each time before resuming a thread, update its PER info. */
696 void
697 s390_linux_nat_target::low_prepare_to_resume (struct lwp_info *lp)
699 int tid;
700 pid_t pid = ptid_of_lwp (lp).pid ();
702 per_struct per_info;
703 ptrace_area parea;
705 CORE_ADDR watch_lo_addr = (CORE_ADDR)-1, watch_hi_addr = 0;
706 struct arch_lwp_info *lp_priv = lwp_arch_private_info (lp);
707 struct s390_debug_reg_state *state = s390_get_debug_reg_state (pid);
708 int step = lwp_is_stepping (lp);
710 /* Nothing to do if there was never any PER info for this thread. */
711 if (lp_priv == NULL)
712 return;
714 /* If PER info has changed, update it. When single-stepping, disable
715 hardware breakpoints (if any). Otherwise we're done. */
716 if (!lp_priv->per_info_changed)
718 if (!step || state->break_areas.empty ())
719 return;
722 lp_priv->per_info_changed = 0;
724 tid = ptid_of_lwp (lp).lwp ();
725 if (tid == 0)
726 tid = pid;
728 parea.len = sizeof (per_info);
729 parea.process_addr = (addr_t) & per_info;
730 parea.kernel_addr = offsetof (struct user_regs_struct, per_info);
732 /* Clear PER info, but adjust the single_step field (used by older
733 kernels only). */
734 memset (&per_info, 0, sizeof (per_info));
735 per_info.single_step = (step != 0);
737 if (!state->watch_areas.empty ())
739 for (const auto &area : state->watch_areas)
741 watch_lo_addr = std::min (watch_lo_addr, area.lo_addr);
742 watch_hi_addr = std::max (watch_hi_addr, area.hi_addr);
745 /* Enable storage-alteration events. */
746 per_info.control_regs.words.cr[0] |= (PER_EVENT_STORE
747 | PER_CONTROL_ALTERATION);
750 if (!state->break_areas.empty ())
752 /* Don't install hardware breakpoints while single-stepping, since
753 our PER settings (e.g. the nullification bit) might then conflict
754 with the kernel's. But re-install them afterwards. */
755 if (step)
756 lp_priv->per_info_changed = 1;
757 else
759 for (const auto &area : state->break_areas)
761 watch_lo_addr = std::min (watch_lo_addr, area.lo_addr);
762 watch_hi_addr = std::max (watch_hi_addr, area.hi_addr);
765 /* If there's just one breakpoint, enable instruction-fetching
766 nullification events for the breakpoint address (fast).
767 Otherwise stop after any instruction within the PER area and
768 after any branch into it (slow). */
769 if (watch_hi_addr == watch_lo_addr)
770 per_info.control_regs.words.cr[0] |= (PER_EVENT_NULLIFICATION
771 | PER_EVENT_IFETCH);
772 else
774 /* The PER area must include the instruction before the
775 first breakpoint address. */
776 watch_lo_addr = watch_lo_addr > 6 ? watch_lo_addr - 6 : 0;
777 per_info.control_regs.words.cr[0]
778 |= (PER_EVENT_BRANCH
779 | PER_EVENT_IFETCH
780 | PER_CONTROL_BRANCH_ADDRESS);
784 per_info.starting_addr = watch_lo_addr;
785 per_info.ending_addr = watch_hi_addr;
787 if (ptrace (PTRACE_POKEUSR_AREA, tid, &parea, 0) < 0)
788 perror_with_name (_("Couldn't modify watchpoint status"));
790 if (show_debug_regs)
791 s390_show_debug_regs (tid, "resume");
794 /* Mark the PER info as changed, so the next resume will update it. */
796 static void
797 s390_mark_per_info_changed (struct lwp_info *lp)
799 if (lwp_arch_private_info (lp) == NULL)
800 lwp_set_arch_private_info (lp, XCNEW (struct arch_lwp_info));
802 lwp_arch_private_info (lp)->per_info_changed = 1;
805 /* When attaching to a new thread, mark its PER info as changed. */
807 void
808 s390_linux_nat_target::low_new_thread (struct lwp_info *lp)
810 s390_mark_per_info_changed (lp);
813 /* Function to call when a thread is being deleted. */
815 void
816 s390_linux_nat_target::low_delete_thread (struct arch_lwp_info *arch_lwp)
818 xfree (arch_lwp);
821 /* Iterator callback for s390_refresh_per_info. */
823 static int
824 s390_refresh_per_info_cb (struct lwp_info *lp)
826 s390_mark_per_info_changed (lp);
828 if (!lwp_is_stopped (lp))
829 linux_stop_lwp (lp);
830 return 0;
833 /* Make sure that threads are stopped and mark PER info as changed. */
835 static int
836 s390_refresh_per_info (void)
838 ptid_t pid_ptid = ptid_t (current_lwp_ptid ().pid ());
840 iterate_over_lwps (pid_ptid, s390_refresh_per_info_cb);
841 return 0;
845 s390_linux_nat_target::insert_watchpoint (CORE_ADDR addr, int len,
846 enum target_hw_bp_type type,
847 struct expression *cond)
849 watch_area area;
850 struct s390_debug_reg_state *state
851 = s390_get_debug_reg_state (inferior_ptid.pid ());
853 area.lo_addr = addr;
854 area.hi_addr = addr + len - 1;
855 state->watch_areas.push_back (area);
857 return s390_refresh_per_info ();
861 s390_linux_nat_target::remove_watchpoint (CORE_ADDR addr, int len,
862 enum target_hw_bp_type type,
863 struct expression *cond)
865 unsigned ix;
866 struct s390_debug_reg_state *state
867 = s390_get_debug_reg_state (inferior_ptid.pid ());
869 for (ix = 0; ix < state->watch_areas.size (); ix++)
871 watch_area &area = state->watch_areas[ix];
872 if (area.lo_addr == addr && area.hi_addr == addr + len - 1)
874 unordered_remove (state->watch_areas, ix);
875 return s390_refresh_per_info ();
879 gdb_printf (gdb_stderr,
880 "Attempt to remove nonexistent watchpoint.\n");
881 return -1;
884 /* Implement the "can_use_hw_breakpoint" target_ops method. */
887 s390_linux_nat_target::can_use_hw_breakpoint (enum bptype type,
888 int cnt, int othertype)
890 if (type == bp_hardware_watchpoint || type == bp_hardware_breakpoint)
891 return 1;
892 return 0;
895 /* Implement the "insert_hw_breakpoint" target_ops method. */
898 s390_linux_nat_target::insert_hw_breakpoint (struct gdbarch *gdbarch,
899 struct bp_target_info *bp_tgt)
901 watch_area area;
902 struct s390_debug_reg_state *state;
904 area.lo_addr = bp_tgt->placed_address = bp_tgt->reqstd_address;
905 area.hi_addr = area.lo_addr;
906 state = s390_get_debug_reg_state (inferior_ptid.pid ());
907 state->break_areas.push_back (area);
909 return s390_refresh_per_info ();
912 /* Implement the "remove_hw_breakpoint" target_ops method. */
915 s390_linux_nat_target::remove_hw_breakpoint (struct gdbarch *gdbarch,
916 struct bp_target_info *bp_tgt)
918 unsigned ix;
919 struct s390_debug_reg_state *state;
921 state = s390_get_debug_reg_state (inferior_ptid.pid ());
922 for (ix = 0; state->break_areas.size (); ix++)
924 watch_area &area = state->break_areas[ix];
925 if (area.lo_addr == bp_tgt->placed_address)
927 unordered_remove (state->break_areas, ix);
928 return s390_refresh_per_info ();
932 gdb_printf (gdb_stderr,
933 "Attempt to remove nonexistent breakpoint.\n");
934 return -1;
938 s390_linux_nat_target::region_ok_for_hw_watchpoint (CORE_ADDR addr, int cnt)
940 return 1;
943 static int
944 s390_target_wordsize (void)
946 int wordsize = 4;
948 /* Check for 64-bit inferior process. This is the case when the host is
949 64-bit, and in addition bit 32 of the PSW mask is set. */
950 #ifdef __s390x__
951 int tid = s390_inferior_tid ();
952 gdb_assert (tid != 0);
953 long pswm;
955 errno = 0;
956 pswm = (long) ptrace (PTRACE_PEEKUSER, tid, PT_PSWMASK, 0);
957 if (errno == 0 && (pswm & 0x100000000ul) != 0)
958 wordsize = 8;
959 #endif
961 return wordsize;
965 s390_linux_nat_target::auxv_parse (const gdb_byte **readptr,
966 const gdb_byte *endptr, CORE_ADDR *typep,
967 CORE_ADDR *valp)
969 gdb_assert (inferior_ptid != null_ptid);
970 int sizeof_auxv_field = s390_target_wordsize ();
971 bfd_endian byte_order = gdbarch_byte_order (current_inferior ()->arch ());
972 const gdb_byte *ptr = *readptr;
974 if (endptr == ptr)
975 return 0;
977 if (endptr - ptr < sizeof_auxv_field * 2)
978 return -1;
980 *typep = extract_unsigned_integer (ptr, sizeof_auxv_field, byte_order);
981 ptr += sizeof_auxv_field;
982 *valp = extract_unsigned_integer (ptr, sizeof_auxv_field, byte_order);
983 ptr += sizeof_auxv_field;
985 *readptr = ptr;
986 return 1;
989 const struct target_desc *
990 s390_linux_nat_target::read_description ()
992 if (inferior_ptid == null_ptid)
993 return this->beneath ()->read_description ();
995 int tid = inferior_ptid.pid ();
997 have_regset_last_break
998 = check_regset (tid, NT_S390_LAST_BREAK, 8);
999 have_regset_system_call
1000 = check_regset (tid, NT_S390_SYSTEM_CALL, 4);
1002 /* If GDB itself is compiled as 64-bit, we are running on a machine in
1003 z/Architecture mode. If the target is running in 64-bit addressing
1004 mode, report s390x architecture. If the target is running in 31-bit
1005 addressing mode, but the kernel supports using 64-bit registers in
1006 that mode, report s390 architecture with 64-bit GPRs. */
1007 #ifdef __s390x__
1009 CORE_ADDR hwcap = linux_get_hwcap ();
1011 have_regset_tdb = (hwcap & HWCAP_S390_TE)
1012 && check_regset (tid, NT_S390_TDB, s390_sizeof_tdbregset);
1014 have_regset_vxrs = (hwcap & HWCAP_S390_VX)
1015 && check_regset (tid, NT_S390_VXRS_LOW, 16 * 8)
1016 && check_regset (tid, NT_S390_VXRS_HIGH, 16 * 16);
1018 have_regset_gs = (hwcap & HWCAP_S390_GS)
1019 && check_regset (tid, NT_S390_GS_CB, 4 * 8)
1020 && check_regset (tid, NT_S390_GS_BC, 4 * 8);
1022 if (s390_target_wordsize () == 8)
1023 return (have_regset_gs ? tdesc_s390x_gs_linux64 :
1024 have_regset_vxrs ?
1025 (have_regset_tdb ? tdesc_s390x_tevx_linux64 :
1026 tdesc_s390x_vx_linux64) :
1027 have_regset_tdb ? tdesc_s390x_te_linux64 :
1028 have_regset_system_call ? tdesc_s390x_linux64v2 :
1029 have_regset_last_break ? tdesc_s390x_linux64v1 :
1030 tdesc_s390x_linux64);
1032 if (hwcap & HWCAP_S390_HIGH_GPRS)
1033 return (have_regset_gs ? tdesc_s390_gs_linux64 :
1034 have_regset_vxrs ?
1035 (have_regset_tdb ? tdesc_s390_tevx_linux64 :
1036 tdesc_s390_vx_linux64) :
1037 have_regset_tdb ? tdesc_s390_te_linux64 :
1038 have_regset_system_call ? tdesc_s390_linux64v2 :
1039 have_regset_last_break ? tdesc_s390_linux64v1 :
1040 tdesc_s390_linux64);
1042 #endif
1044 /* If GDB itself is compiled as 31-bit, or if we're running a 31-bit inferior
1045 on a 64-bit kernel that does not support using 64-bit registers in 31-bit
1046 mode, report s390 architecture with 32-bit GPRs. */
1047 return (have_regset_system_call? tdesc_s390_linux32v2 :
1048 have_regset_last_break? tdesc_s390_linux32v1 :
1049 tdesc_s390_linux32);
1052 void _initialize_s390_nat ();
1053 void
1054 _initialize_s390_nat ()
1056 /* Register the target. */
1057 linux_target = &the_s390_linux_nat_target;
1058 add_inf_child_target (&the_s390_linux_nat_target);
1060 /* A maintenance command to enable showing the PER state. */
1061 add_setshow_boolean_cmd ("show-debug-regs", class_maintenance,
1062 &show_debug_regs, _("\
1063 Set whether to show the PER (debug) hardware state."), _("\
1064 Show whether to show the PER (debug) hardware state."), _("\
1065 Use \"on\" to enable, \"off\" to disable.\n\
1066 If enabled, the PER state is shown after it is changed by GDB,\n\
1067 and when the inferior triggers a breakpoint or watchpoint."),
1068 NULL,
1069 NULL,
1070 &maintenance_set_cmdlist,
1071 &maintenance_show_cmdlist);