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