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