1 /* S390 native-dependent code for GDB, the GNU debugger.
2 Copyright (C) 2001-2014 Free Software Foundation, Inc.
4 Contributed by D.J. Barrow (djbarrow@de.ibm.com,barrow_dj@yahoo.com)
5 for IBM Deutschland Entwicklung GmbH, IBM Corporation.
7 This file is part of GDB.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
26 #include "linux-nat.h"
30 #include "s390-linux-tdep.h"
31 #include "elf/common.h"
33 #include <asm/ptrace.h>
34 #include <sys/ptrace.h>
35 #include <asm/types.h>
36 #include <sys/procfs.h>
37 #include <sys/ucontext.h>
40 #ifndef PTRACE_GETREGSET
41 #define PTRACE_GETREGSET 0x4204
44 #ifndef PTRACE_SETREGSET
45 #define PTRACE_SETREGSET 0x4205
48 static int have_regset_last_break = 0;
49 static int have_regset_system_call = 0;
50 static int have_regset_tdb = 0;
52 /* Map registers to gregset/ptrace offsets.
53 These arrays are defined in s390-tdep.c. */
56 #define regmap_gregset s390x_regmap_gregset
58 #define regmap_gregset s390_regmap_gregset
61 #define regmap_fpregset s390_regmap_fpregset
63 /* Fill the regset described by MAP into REGCACHE, using the values
64 from REGP. The MAP array represents each register as a pair
65 (offset, regno) of short integers and is terminated with -1. */
68 s390_native_supply (struct regcache *regcache, const short *map,
71 for (; map[0] >= 0; map += 2)
72 regcache_raw_supply (regcache, map[1], regp ? regp + map[0] : NULL);
75 /* Collect the register REGNO out of the regset described by MAP from
76 REGCACHE into REGP. If REGNO == -1, do this for all registers in
80 s390_native_collect (const struct regcache *regcache, const short *map,
81 int regno, gdb_byte *regp)
83 for (; map[0] >= 0; map += 2)
84 if (regno == -1 || regno == map[1])
85 regcache_raw_collect (regcache, map[1], regp + map[0]);
88 /* Fill GDB's register array with the general-purpose register values
91 When debugging a 32-bit executable running under a 64-bit kernel,
92 we have to fix up the 64-bit registers we get from the kernel to
93 make them look like 32-bit registers. */
96 supply_gregset (struct regcache *regcache, const gregset_t *regp)
99 struct gdbarch *gdbarch = get_regcache_arch (regcache);
100 if (gdbarch_ptr_bit (gdbarch) == 32)
102 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
103 ULONGEST pswm = 0, pswa = 0;
107 for (map = regmap_gregset; map[0] >= 0; map += 2)
109 const gdb_byte *p = (const gdb_byte *) regp + map[0];
112 if (regno == S390_PSWM_REGNUM)
113 pswm = extract_unsigned_integer (p, 8, byte_order);
114 else if (regno == S390_PSWA_REGNUM)
115 pswa = extract_unsigned_integer (p, 8, byte_order);
118 if ((regno >= S390_R0_REGNUM && regno <= S390_R15_REGNUM)
119 || regno == S390_ORIG_R2_REGNUM)
121 regcache_raw_supply (regcache, regno, p);
125 store_unsigned_integer (buf, 4, byte_order, (pswm >> 32) | 0x80000);
126 regcache_raw_supply (regcache, S390_PSWM_REGNUM, buf);
127 store_unsigned_integer (buf, 4, byte_order,
128 (pswa & 0x7fffffff) | (pswm & 0x80000000));
129 regcache_raw_supply (regcache, S390_PSWA_REGNUM, buf);
134 s390_native_supply (regcache, regmap_gregset, (const gdb_byte *) regp);
137 /* Fill register REGNO (if it is a general-purpose register) in
138 *REGP with the value in GDB's register array. If REGNO is -1,
139 do this for all registers. */
142 fill_gregset (const struct regcache *regcache, gregset_t *regp, int regno)
145 struct gdbarch *gdbarch = get_regcache_arch (regcache);
146 if (gdbarch_ptr_bit (gdbarch) == 32)
151 for (map = regmap_gregset; map[0] >= 0; map += 2)
153 gdb_byte *p = (gdb_byte *) regp + map[0];
156 if (reg >= S390_PSWM_REGNUM && reg <= S390_PSWA_REGNUM)
157 psw_p[reg - S390_PSWM_REGNUM] = p;
159 else if (regno == -1 || regno == reg)
161 if ((reg >= S390_R0_REGNUM && reg <= S390_R15_REGNUM)
162 || reg == S390_ORIG_R2_REGNUM)
167 regcache_raw_collect (regcache, reg, p + 4);
172 || regno == S390_PSWM_REGNUM || regno == S390_PSWA_REGNUM)
174 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
178 regcache_raw_collect (regcache, S390_PSWM_REGNUM, buf);
179 pswm = extract_unsigned_integer (buf, 4, byte_order);
180 regcache_raw_collect (regcache, S390_PSWA_REGNUM, buf);
181 pswa = extract_unsigned_integer (buf, 4, byte_order);
183 if (regno == -1 || regno == S390_PSWM_REGNUM)
184 store_unsigned_integer (psw_p[0], 8, byte_order,
185 ((pswm & 0xfff7ffff) << 32) |
186 (pswa & 0x80000000));
187 if (regno == -1 || regno == S390_PSWA_REGNUM)
188 store_unsigned_integer (psw_p[1], 8, byte_order,
195 s390_native_collect (regcache, regmap_gregset, regno, (gdb_byte *) regp);
198 /* Fill GDB's register array with the floating-point register values
201 supply_fpregset (struct regcache *regcache, const fpregset_t *regp)
203 s390_native_supply (regcache, regmap_fpregset, (const gdb_byte *) regp);
206 /* Fill register REGNO (if it is a general-purpose register) in
207 *REGP with the value in GDB's register array. If REGNO is -1,
208 do this for all registers. */
210 fill_fpregset (const struct regcache *regcache, fpregset_t *regp, int regno)
212 s390_native_collect (regcache, regmap_fpregset, regno, (gdb_byte *) regp);
215 /* Find the TID for the current inferior thread to use with ptrace. */
217 s390_inferior_tid (void)
219 /* GNU/Linux LWP ID's are process ID's. */
220 int tid = ptid_get_lwp (inferior_ptid);
222 tid = ptid_get_pid (inferior_ptid); /* Not a threaded program. */
227 /* Fetch all general-purpose registers from process/thread TID and
228 store their values in GDB's register cache. */
230 fetch_regs (struct regcache *regcache, int tid)
235 parea.len = sizeof (regs);
236 parea.process_addr = (addr_t) ®s;
237 parea.kernel_addr = offsetof (struct user_regs_struct, psw);
238 if (ptrace (PTRACE_PEEKUSR_AREA, tid, (long) &parea) < 0)
239 perror_with_name (_("Couldn't get registers"));
241 supply_gregset (regcache, (const gregset_t *) ®s);
244 /* Store all valid general-purpose registers in GDB's register cache
245 into the process/thread specified by TID. */
247 store_regs (const struct regcache *regcache, int tid, int regnum)
252 parea.len = sizeof (regs);
253 parea.process_addr = (addr_t) ®s;
254 parea.kernel_addr = offsetof (struct user_regs_struct, psw);
255 if (ptrace (PTRACE_PEEKUSR_AREA, tid, (long) &parea) < 0)
256 perror_with_name (_("Couldn't get registers"));
258 fill_gregset (regcache, ®s, regnum);
260 if (ptrace (PTRACE_POKEUSR_AREA, tid, (long) &parea) < 0)
261 perror_with_name (_("Couldn't write registers"));
264 /* Fetch all floating-point registers from process/thread TID and store
265 their values in GDB's register cache. */
267 fetch_fpregs (struct regcache *regcache, int tid)
272 parea.len = sizeof (fpregs);
273 parea.process_addr = (addr_t) &fpregs;
274 parea.kernel_addr = offsetof (struct user_regs_struct, fp_regs);
275 if (ptrace (PTRACE_PEEKUSR_AREA, tid, (long) &parea) < 0)
276 perror_with_name (_("Couldn't get floating point status"));
278 supply_fpregset (regcache, (const fpregset_t *) &fpregs);
281 /* Store all valid floating-point registers in GDB's register cache
282 into the process/thread specified by TID. */
284 store_fpregs (const struct regcache *regcache, int tid, int regnum)
289 parea.len = sizeof (fpregs);
290 parea.process_addr = (addr_t) &fpregs;
291 parea.kernel_addr = offsetof (struct user_regs_struct, fp_regs);
292 if (ptrace (PTRACE_PEEKUSR_AREA, tid, (long) &parea) < 0)
293 perror_with_name (_("Couldn't get floating point status"));
295 fill_fpregset (regcache, &fpregs, regnum);
297 if (ptrace (PTRACE_POKEUSR_AREA, tid, (long) &parea) < 0)
298 perror_with_name (_("Couldn't write floating point status"));
301 /* Fetch all registers in the kernel's register set whose number is REGSET,
302 whose size is REGSIZE, and whose layout is described by REGMAP, from
303 process/thread TID and store their values in GDB's register cache. */
305 fetch_regset (struct regcache *regcache, int tid,
306 int regset, int regsize, const short *regmap)
308 gdb_byte *buf = alloca (regsize);
312 iov.iov_len = regsize;
314 if (ptrace (PTRACE_GETREGSET, tid, (long) regset, (long) &iov) < 0)
316 if (errno == ENODATA)
317 s390_native_supply (regcache, regmap, NULL);
319 perror_with_name (_("Couldn't get register set"));
322 s390_native_supply (regcache, regmap, buf);
325 /* Store all registers in the kernel's register set whose number is REGSET,
326 whose size is REGSIZE, and whose layout is described by REGMAP, from
327 GDB's register cache back to process/thread TID. */
329 store_regset (struct regcache *regcache, int tid,
330 int regset, int regsize, const short *regmap)
332 gdb_byte *buf = alloca (regsize);
336 iov.iov_len = regsize;
338 if (ptrace (PTRACE_GETREGSET, tid, (long) regset, (long) &iov) < 0)
339 perror_with_name (_("Couldn't get register set"));
341 s390_native_collect (regcache, regmap, -1, buf);
343 if (ptrace (PTRACE_SETREGSET, tid, (long) regset, (long) &iov) < 0)
344 perror_with_name (_("Couldn't set register set"));
347 /* Check whether the kernel provides a register set with number REGSET
348 of size REGSIZE for process/thread TID. */
350 check_regset (int tid, int regset, int regsize)
352 gdb_byte *buf = alloca (regsize);
356 iov.iov_len = regsize;
358 if (ptrace (PTRACE_GETREGSET, tid, (long) regset, (long) &iov) >= 0
364 /* Fetch register REGNUM from the child process. If REGNUM is -1, do
365 this for all registers. */
367 s390_linux_fetch_inferior_registers (struct target_ops *ops,
368 struct regcache *regcache, int regnum)
370 int tid = s390_inferior_tid ();
372 if (regnum == -1 || S390_IS_GREGSET_REGNUM (regnum))
373 fetch_regs (regcache, tid);
375 if (regnum == -1 || S390_IS_FPREGSET_REGNUM (regnum))
376 fetch_fpregs (regcache, tid);
378 if (have_regset_last_break)
379 if (regnum == -1 || regnum == S390_LAST_BREAK_REGNUM)
380 fetch_regset (regcache, tid, NT_S390_LAST_BREAK, 8,
381 (gdbarch_ptr_bit (get_regcache_arch (regcache)) == 32
382 ? s390_regmap_last_break : s390x_regmap_last_break));
384 if (have_regset_system_call)
385 if (regnum == -1 || regnum == S390_SYSTEM_CALL_REGNUM)
386 fetch_regset (regcache, tid, NT_S390_SYSTEM_CALL, 4,
387 s390_regmap_system_call);
390 if (regnum == -1 || S390_IS_TDBREGSET_REGNUM (regnum))
391 fetch_regset (regcache, tid, NT_S390_TDB, s390_sizeof_tdbregset,
395 /* Store register REGNUM back into the child process. If REGNUM is
396 -1, do this for all registers. */
398 s390_linux_store_inferior_registers (struct target_ops *ops,
399 struct regcache *regcache, int regnum)
401 int tid = s390_inferior_tid ();
403 if (regnum == -1 || S390_IS_GREGSET_REGNUM (regnum))
404 store_regs (regcache, tid, regnum);
406 if (regnum == -1 || S390_IS_FPREGSET_REGNUM (regnum))
407 store_fpregs (regcache, tid, regnum);
409 /* S390_LAST_BREAK_REGNUM is read-only. */
411 if (have_regset_system_call)
412 if (regnum == -1 || regnum == S390_SYSTEM_CALL_REGNUM)
413 store_regset (regcache, tid, NT_S390_SYSTEM_CALL, 4,
414 s390_regmap_system_call);
418 /* Hardware-assisted watchpoint handling. */
420 /* We maintain a list of all currently active watchpoints in order
421 to properly handle watchpoint removal.
423 The only thing we actually need is the total address space area
424 spanned by the watchpoints. */
428 struct watch_area *next;
433 static struct watch_area *watch_base = NULL;
436 s390_stopped_by_watchpoint (struct target_ops *ops)
438 per_lowcore_bits per_lowcore;
442 /* Speed up common case. */
446 parea.len = sizeof (per_lowcore);
447 parea.process_addr = (addr_t) & per_lowcore;
448 parea.kernel_addr = offsetof (struct user_regs_struct, per_info.lowcore);
449 if (ptrace (PTRACE_PEEKUSR_AREA, s390_inferior_tid (), &parea) < 0)
450 perror_with_name (_("Couldn't retrieve watchpoint status"));
452 result = (per_lowcore.perc_storage_alteration == 1
453 && per_lowcore.perc_store_real_address == 0);
457 /* Do not report this watchpoint again. */
458 memset (&per_lowcore, 0, sizeof (per_lowcore));
459 if (ptrace (PTRACE_POKEUSR_AREA, s390_inferior_tid (), &parea) < 0)
460 perror_with_name (_("Couldn't clear watchpoint status"));
467 s390_fix_watch_points (struct lwp_info *lp)
474 CORE_ADDR watch_lo_addr = (CORE_ADDR)-1, watch_hi_addr = 0;
475 struct watch_area *area;
477 tid = ptid_get_lwp (lp->ptid);
479 tid = ptid_get_pid (lp->ptid);
481 for (area = watch_base; area; area = area->next)
483 watch_lo_addr = min (watch_lo_addr, area->lo_addr);
484 watch_hi_addr = max (watch_hi_addr, area->hi_addr);
487 parea.len = sizeof (per_info);
488 parea.process_addr = (addr_t) & per_info;
489 parea.kernel_addr = offsetof (struct user_regs_struct, per_info);
490 if (ptrace (PTRACE_PEEKUSR_AREA, tid, &parea) < 0)
491 perror_with_name (_("Couldn't retrieve watchpoint status"));
495 per_info.control_regs.bits.em_storage_alteration = 1;
496 per_info.control_regs.bits.storage_alt_space_ctl = 1;
500 per_info.control_regs.bits.em_storage_alteration = 0;
501 per_info.control_regs.bits.storage_alt_space_ctl = 0;
503 per_info.starting_addr = watch_lo_addr;
504 per_info.ending_addr = watch_hi_addr;
506 if (ptrace (PTRACE_POKEUSR_AREA, tid, &parea) < 0)
507 perror_with_name (_("Couldn't modify watchpoint status"));
511 s390_insert_watchpoint (CORE_ADDR addr, int len, int type,
512 struct expression *cond)
515 struct watch_area *area = xmalloc (sizeof (struct watch_area));
520 area->lo_addr = addr;
521 area->hi_addr = addr + len - 1;
523 area->next = watch_base;
527 s390_fix_watch_points (lp);
532 s390_remove_watchpoint (CORE_ADDR addr, int len, int type,
533 struct expression *cond)
536 struct watch_area *area, **parea;
538 for (parea = &watch_base; *parea; parea = &(*parea)->next)
539 if ((*parea)->lo_addr == addr
540 && (*parea)->hi_addr == addr + len - 1)
545 fprintf_unfiltered (gdb_stderr,
546 "Attempt to remove nonexistent watchpoint.\n");
555 s390_fix_watch_points (lp);
560 s390_can_use_hw_breakpoint (int type, int cnt, int othertype)
562 return type == bp_hardware_watchpoint;
566 s390_region_ok_for_hw_watchpoint (CORE_ADDR addr, int cnt)
572 s390_target_wordsize (void)
576 /* Check for 64-bit inferior process. This is the case when the host is
577 64-bit, and in addition bit 32 of the PSW mask is set. */
582 pswm = (long) ptrace (PTRACE_PEEKUSER, s390_inferior_tid (), PT_PSWMASK, 0);
583 if (errno == 0 && (pswm & 0x100000000ul) != 0)
591 s390_auxv_parse (struct target_ops *ops, gdb_byte **readptr,
592 gdb_byte *endptr, CORE_ADDR *typep, CORE_ADDR *valp)
594 int sizeof_auxv_field = s390_target_wordsize ();
595 enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
596 gdb_byte *ptr = *readptr;
601 if (endptr - ptr < sizeof_auxv_field * 2)
604 *typep = extract_unsigned_integer (ptr, sizeof_auxv_field, byte_order);
605 ptr += sizeof_auxv_field;
606 *valp = extract_unsigned_integer (ptr, sizeof_auxv_field, byte_order);
607 ptr += sizeof_auxv_field;
615 s390_get_hwcap (void)
619 if (target_auxv_search (¤t_target, AT_HWCAP, &field))
620 return (unsigned long) field;
626 static const struct target_desc *
627 s390_read_description (struct target_ops *ops)
629 int tid = s390_inferior_tid ();
631 have_regset_last_break
632 = check_regset (tid, NT_S390_LAST_BREAK, 8);
633 have_regset_system_call
634 = check_regset (tid, NT_S390_SYSTEM_CALL, 4);
637 /* If GDB itself is compiled as 64-bit, we are running on a machine in
638 z/Architecture mode. If the target is running in 64-bit addressing
639 mode, report s390x architecture. If the target is running in 31-bit
640 addressing mode, but the kernel supports using 64-bit registers in
641 that mode, report s390 architecture with 64-bit GPRs. */
643 have_regset_tdb = (s390_get_hwcap () & HWCAP_S390_TE) ?
644 check_regset (tid, NT_S390_TDB, s390_sizeof_tdbregset) : 0;
646 if (s390_target_wordsize () == 8)
647 return (have_regset_tdb ? tdesc_s390x_te_linux64 :
648 have_regset_system_call? tdesc_s390x_linux64v2 :
649 have_regset_last_break? tdesc_s390x_linux64v1 :
650 tdesc_s390x_linux64);
652 if (s390_get_hwcap () & HWCAP_S390_HIGH_GPRS)
653 return (have_regset_tdb ? tdesc_s390_te_linux64 :
654 have_regset_system_call? tdesc_s390_linux64v2 :
655 have_regset_last_break? tdesc_s390_linux64v1 :
659 /* If GDB itself is compiled as 31-bit, or if we're running a 31-bit inferior
660 on a 64-bit kernel that does not support using 64-bit registers in 31-bit
661 mode, report s390 architecture with 32-bit GPRs. */
662 return (have_regset_system_call? tdesc_s390_linux32v2 :
663 have_regset_last_break? tdesc_s390_linux32v1 :
667 void _initialize_s390_nat (void);
670 _initialize_s390_nat (void)
672 struct target_ops *t;
674 /* Fill in the generic GNU/Linux methods. */
677 /* Add our register access methods. */
678 t->to_fetch_registers = s390_linux_fetch_inferior_registers;
679 t->to_store_registers = s390_linux_store_inferior_registers;
681 /* Add our watchpoint methods. */
682 t->to_can_use_hw_breakpoint = s390_can_use_hw_breakpoint;
683 t->to_region_ok_for_hw_watchpoint = s390_region_ok_for_hw_watchpoint;
684 t->to_have_continuable_watchpoint = 1;
685 t->to_stopped_by_watchpoint = s390_stopped_by_watchpoint;
686 t->to_insert_watchpoint = s390_insert_watchpoint;
687 t->to_remove_watchpoint = s390_remove_watchpoint;
689 /* Detect target architecture. */
690 t->to_read_description = s390_read_description;
691 t->to_auxv_parse = s390_auxv_parse;
693 /* Register the target. */
694 linux_nat_add_target (t);
695 linux_nat_set_new_thread (t, s390_fix_watch_points);