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