1 /* S390 native-dependent code for GDB, the GNU debugger.
2 Copyright (C) 2001-2013 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-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 HWCAP_S390_HIGH_GPRS
41 #define HWCAP_S390_HIGH_GPRS 512
44 #ifndef PTRACE_GETREGSET
45 #define PTRACE_GETREGSET 0x4204
48 #ifndef PTRACE_SETREGSET
49 #define PTRACE_SETREGSET 0x4205
52 static int have_regset_last_break = 0;
53 static int have_regset_system_call = 0;
55 /* Map registers to gregset/ptrace offsets.
56 These arrays are defined in s390-tdep.c. */
59 #define regmap_gregset s390x_regmap_gregset
61 #define regmap_gregset s390_regmap_gregset
64 #define regmap_fpregset s390_regmap_fpregset
66 /* When debugging a 32-bit executable running under a 64-bit kernel,
67 we have to fix up the 64-bit registers we get from the kernel
68 to make them look like 32-bit registers. */
71 s390_native_supply (struct regcache *regcache, int regno,
72 const gdb_byte *regp, int *regmap)
74 int offset = regmap[regno];
77 struct gdbarch *gdbarch = get_regcache_arch (regcache);
78 if (offset != -1 && gdbarch_ptr_bit (gdbarch) == 32)
80 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
82 if (regno == S390_PSWM_REGNUM)
87 pswm = extract_unsigned_integer (regp + regmap[S390_PSWM_REGNUM],
90 store_unsigned_integer (buf, 4, byte_order, (pswm >> 32) | 0x80000);
91 regcache_raw_supply (regcache, regno, buf);
95 if (regno == S390_PSWA_REGNUM)
100 pswa = extract_unsigned_integer (regp + regmap[S390_PSWA_REGNUM],
102 pswm = extract_unsigned_integer (regp + regmap[S390_PSWM_REGNUM],
105 store_unsigned_integer (buf, 4, byte_order,
106 (pswa & 0x7fffffff) | (pswm & 0x80000000));
107 regcache_raw_supply (regcache, regno, buf);
111 if ((regno >= S390_R0_REGNUM && regno <= S390_R15_REGNUM)
112 || regno == S390_ORIG_R2_REGNUM)
118 regcache_raw_supply (regcache, regno, regp + offset);
122 s390_native_collect (const struct regcache *regcache, int regno,
123 gdb_byte *regp, int *regmap)
125 int offset = regmap[regno];
128 struct gdbarch *gdbarch = get_regcache_arch (regcache);
129 if (offset != -1 && gdbarch_ptr_bit (gdbarch) == 32)
131 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
133 if (regno == S390_PSWM_REGNUM)
138 regcache_raw_collect (regcache, regno, buf);
139 pswm = extract_unsigned_integer (buf, 4, byte_order);
141 /* We don't know the final addressing mode until the PSW address
142 is known, so leave it as-is. When the PSW address is collected
143 (below), the addressing mode will be updated. */
144 store_unsigned_integer (regp + regmap[S390_PSWM_REGNUM],
145 4, byte_order, pswm & 0xfff7ffff);
149 if (regno == S390_PSWA_REGNUM)
154 regcache_raw_collect (regcache, regno, buf);
155 pswa = extract_unsigned_integer (buf, 4, byte_order);
157 store_unsigned_integer (regp + regmap[S390_PSWA_REGNUM],
158 8, byte_order, pswa & 0x7fffffff);
160 /* Update basic addressing mode bit in PSW mask, see above. */
161 store_unsigned_integer (regp + regmap[S390_PSWM_REGNUM] + 4,
162 4, byte_order, pswa & 0x80000000);
166 if ((regno >= S390_R0_REGNUM && regno <= S390_R15_REGNUM)
167 || regno == S390_ORIG_R2_REGNUM)
169 memset (regp + offset, 0, 4);
176 regcache_raw_collect (regcache, regno, regp + offset);
179 /* Fill GDB's register array with the general-purpose register values
182 supply_gregset (struct regcache *regcache, const gregset_t *regp)
185 for (i = 0; i < S390_NUM_REGS; i++)
186 s390_native_supply (regcache, i, (const gdb_byte *) regp, regmap_gregset);
189 /* Fill register REGNO (if it is a general-purpose register) in
190 *REGP with the value in GDB's register array. If REGNO is -1,
191 do this for all registers. */
193 fill_gregset (const struct regcache *regcache, gregset_t *regp, int regno)
196 for (i = 0; i < S390_NUM_REGS; i++)
197 if (regno == -1 || regno == i)
198 s390_native_collect (regcache, i, (gdb_byte *) regp, regmap_gregset);
201 /* Fill GDB's register array with the floating-point register values
204 supply_fpregset (struct regcache *regcache, const fpregset_t *regp)
207 for (i = 0; i < S390_NUM_REGS; i++)
208 s390_native_supply (regcache, i, (const gdb_byte *) regp, regmap_fpregset);
211 /* Fill register REGNO (if it is a general-purpose register) in
212 *REGP with the value in GDB's register array. If REGNO is -1,
213 do this for all registers. */
215 fill_fpregset (const struct regcache *regcache, fpregset_t *regp, int regno)
218 for (i = 0; i < S390_NUM_REGS; i++)
219 if (regno == -1 || regno == i)
220 s390_native_collect (regcache, i, (gdb_byte *) regp, regmap_fpregset);
223 /* Find the TID for the current inferior thread to use with ptrace. */
225 s390_inferior_tid (void)
227 /* GNU/Linux LWP ID's are process ID's. */
228 int tid = TIDGET (inferior_ptid);
230 tid = PIDGET (inferior_ptid); /* Not a threaded program. */
235 /* Fetch all general-purpose registers from process/thread TID and
236 store their values in GDB's register cache. */
238 fetch_regs (struct regcache *regcache, int tid)
243 parea.len = sizeof (regs);
244 parea.process_addr = (addr_t) ®s;
245 parea.kernel_addr = offsetof (struct user_regs_struct, psw);
246 if (ptrace (PTRACE_PEEKUSR_AREA, tid, (long) &parea) < 0)
247 perror_with_name (_("Couldn't get registers"));
249 supply_gregset (regcache, (const gregset_t *) ®s);
252 /* Store all valid general-purpose registers in GDB's register cache
253 into the process/thread specified by TID. */
255 store_regs (const struct regcache *regcache, int tid, int regnum)
260 parea.len = sizeof (regs);
261 parea.process_addr = (addr_t) ®s;
262 parea.kernel_addr = offsetof (struct user_regs_struct, psw);
263 if (ptrace (PTRACE_PEEKUSR_AREA, tid, (long) &parea) < 0)
264 perror_with_name (_("Couldn't get registers"));
266 fill_gregset (regcache, ®s, regnum);
268 if (ptrace (PTRACE_POKEUSR_AREA, tid, (long) &parea) < 0)
269 perror_with_name (_("Couldn't write registers"));
272 /* Fetch all floating-point registers from process/thread TID and store
273 their values in GDB's register cache. */
275 fetch_fpregs (struct regcache *regcache, int tid)
280 parea.len = sizeof (fpregs);
281 parea.process_addr = (addr_t) &fpregs;
282 parea.kernel_addr = offsetof (struct user_regs_struct, fp_regs);
283 if (ptrace (PTRACE_PEEKUSR_AREA, tid, (long) &parea) < 0)
284 perror_with_name (_("Couldn't get floating point status"));
286 supply_fpregset (regcache, (const fpregset_t *) &fpregs);
289 /* Store all valid floating-point registers in GDB's register cache
290 into the process/thread specified by TID. */
292 store_fpregs (const struct regcache *regcache, int tid, int regnum)
297 parea.len = sizeof (fpregs);
298 parea.process_addr = (addr_t) &fpregs;
299 parea.kernel_addr = offsetof (struct user_regs_struct, fp_regs);
300 if (ptrace (PTRACE_PEEKUSR_AREA, tid, (long) &parea) < 0)
301 perror_with_name (_("Couldn't get floating point status"));
303 fill_fpregset (regcache, &fpregs, regnum);
305 if (ptrace (PTRACE_POKEUSR_AREA, tid, (long) &parea) < 0)
306 perror_with_name (_("Couldn't write floating point status"));
309 /* Fetch all registers in the kernel's register set whose number is REGSET,
310 whose size is REGSIZE, and whose layout is described by REGMAP, from
311 process/thread TID and store their values in GDB's register cache. */
313 fetch_regset (struct regcache *regcache, int tid,
314 int regset, int regsize, int *regmap)
316 struct gdbarch *gdbarch = get_regcache_arch (regcache);
317 gdb_byte *buf = alloca (regsize);
322 iov.iov_len = regsize;
324 if (ptrace (PTRACE_GETREGSET, tid, (long) regset, (long) &iov) < 0)
325 perror_with_name (_("Couldn't get register set"));
327 for (i = 0; i < S390_NUM_REGS; i++)
328 s390_native_supply (regcache, i, buf, regmap);
331 /* Store all registers in the kernel's register set whose number is REGSET,
332 whose size is REGSIZE, and whose layout is described by REGMAP, from
333 GDB's register cache back to process/thread TID. */
335 store_regset (struct regcache *regcache, int tid,
336 int regset, int regsize, int *regmap)
338 struct gdbarch *gdbarch = get_regcache_arch (regcache);
339 gdb_byte *buf = alloca (regsize);
344 iov.iov_len = regsize;
346 if (ptrace (PTRACE_GETREGSET, tid, (long) regset, (long) &iov) < 0)
347 perror_with_name (_("Couldn't get register set"));
349 for (i = 0; i < S390_NUM_REGS; i++)
350 s390_native_collect (regcache, i, buf, regmap);
352 if (ptrace (PTRACE_SETREGSET, tid, (long) regset, (long) &iov) < 0)
353 perror_with_name (_("Couldn't set register set"));
356 /* Check whether the kernel provides a register set with number REGSET
357 of size REGSIZE for process/thread TID. */
359 check_regset (int tid, int regset, int regsize)
361 gdb_byte *buf = alloca (regsize);
365 iov.iov_len = regsize;
367 if (ptrace (PTRACE_GETREGSET, tid, (long) regset, (long) &iov) < 0)
373 /* Fetch register REGNUM from the child process. If REGNUM is -1, do
374 this for all registers. */
376 s390_linux_fetch_inferior_registers (struct target_ops *ops,
377 struct regcache *regcache, int regnum)
379 int tid = s390_inferior_tid ();
382 || (regnum < S390_NUM_REGS && regmap_gregset[regnum] != -1))
383 fetch_regs (regcache, tid);
386 || (regnum < S390_NUM_REGS && regmap_fpregset[regnum] != -1))
387 fetch_fpregs (regcache, tid);
389 if (have_regset_last_break)
390 if (regnum == -1 || regnum == S390_LAST_BREAK_REGNUM)
391 fetch_regset (regcache, tid, NT_S390_LAST_BREAK, 8,
392 (gdbarch_ptr_bit (get_regcache_arch (regcache)) == 32
393 ? s390_regmap_last_break : s390x_regmap_last_break));
395 if (have_regset_system_call)
396 if (regnum == -1 || regnum == S390_SYSTEM_CALL_REGNUM)
397 fetch_regset (regcache, tid, NT_S390_SYSTEM_CALL, 4,
398 s390_regmap_system_call);
401 /* Store register REGNUM back into the child process. If REGNUM is
402 -1, do this for all registers. */
404 s390_linux_store_inferior_registers (struct target_ops *ops,
405 struct regcache *regcache, int regnum)
407 int tid = s390_inferior_tid ();
410 || (regnum < S390_NUM_REGS && regmap_gregset[regnum] != -1))
411 store_regs (regcache, tid, regnum);
414 || (regnum < S390_NUM_REGS && regmap_fpregset[regnum] != -1))
415 store_fpregs (regcache, tid, regnum);
417 /* S390_LAST_BREAK_REGNUM is read-only. */
419 if (have_regset_system_call)
420 if (regnum == -1 || regnum == S390_SYSTEM_CALL_REGNUM)
421 store_regset (regcache, tid, NT_S390_SYSTEM_CALL, 4,
422 s390_regmap_system_call);
426 /* Hardware-assisted watchpoint handling. */
428 /* We maintain a list of all currently active watchpoints in order
429 to properly handle watchpoint removal.
431 The only thing we actually need is the total address space area
432 spanned by the watchpoints. */
436 struct watch_area *next;
441 static struct watch_area *watch_base = NULL;
444 s390_stopped_by_watchpoint (void)
446 per_lowcore_bits per_lowcore;
450 /* Speed up common case. */
454 parea.len = sizeof (per_lowcore);
455 parea.process_addr = (addr_t) & per_lowcore;
456 parea.kernel_addr = offsetof (struct user_regs_struct, per_info.lowcore);
457 if (ptrace (PTRACE_PEEKUSR_AREA, s390_inferior_tid (), &parea) < 0)
458 perror_with_name (_("Couldn't retrieve watchpoint status"));
460 result = (per_lowcore.perc_storage_alteration == 1
461 && per_lowcore.perc_store_real_address == 0);
465 /* Do not report this watchpoint again. */
466 memset (&per_lowcore, 0, sizeof (per_lowcore));
467 if (ptrace (PTRACE_POKEUSR_AREA, s390_inferior_tid (), &parea) < 0)
468 perror_with_name (_("Couldn't clear watchpoint status"));
475 s390_fix_watch_points (struct lwp_info *lp)
482 CORE_ADDR watch_lo_addr = (CORE_ADDR)-1, watch_hi_addr = 0;
483 struct watch_area *area;
485 tid = TIDGET (lp->ptid);
487 tid = PIDGET (lp->ptid);
489 for (area = watch_base; area; area = area->next)
491 watch_lo_addr = min (watch_lo_addr, area->lo_addr);
492 watch_hi_addr = max (watch_hi_addr, area->hi_addr);
495 parea.len = sizeof (per_info);
496 parea.process_addr = (addr_t) & per_info;
497 parea.kernel_addr = offsetof (struct user_regs_struct, per_info);
498 if (ptrace (PTRACE_PEEKUSR_AREA, tid, &parea) < 0)
499 perror_with_name (_("Couldn't retrieve watchpoint status"));
503 per_info.control_regs.bits.em_storage_alteration = 1;
504 per_info.control_regs.bits.storage_alt_space_ctl = 1;
508 per_info.control_regs.bits.em_storage_alteration = 0;
509 per_info.control_regs.bits.storage_alt_space_ctl = 0;
511 per_info.starting_addr = watch_lo_addr;
512 per_info.ending_addr = watch_hi_addr;
514 if (ptrace (PTRACE_POKEUSR_AREA, tid, &parea) < 0)
515 perror_with_name (_("Couldn't modify watchpoint status"));
519 s390_insert_watchpoint (CORE_ADDR addr, int len, int type,
520 struct expression *cond)
523 struct watch_area *area = xmalloc (sizeof (struct watch_area));
528 area->lo_addr = addr;
529 area->hi_addr = addr + len - 1;
531 area->next = watch_base;
535 s390_fix_watch_points (lp);
540 s390_remove_watchpoint (CORE_ADDR addr, int len, int type,
541 struct expression *cond)
544 struct watch_area *area, **parea;
546 for (parea = &watch_base; *parea; parea = &(*parea)->next)
547 if ((*parea)->lo_addr == addr
548 && (*parea)->hi_addr == addr + len - 1)
553 fprintf_unfiltered (gdb_stderr,
554 "Attempt to remove nonexistent watchpoint.\n");
563 s390_fix_watch_points (lp);
568 s390_can_use_hw_breakpoint (int type, int cnt, int othertype)
570 return type == bp_hardware_watchpoint;
574 s390_region_ok_for_hw_watchpoint (CORE_ADDR addr, int cnt)
580 s390_target_wordsize (void)
584 /* Check for 64-bit inferior process. This is the case when the host is
585 64-bit, and in addition bit 32 of the PSW mask is set. */
590 pswm = (long) ptrace (PTRACE_PEEKUSER, s390_inferior_tid (), PT_PSWMASK, 0);
591 if (errno == 0 && (pswm & 0x100000000ul) != 0)
599 s390_auxv_parse (struct target_ops *ops, gdb_byte **readptr,
600 gdb_byte *endptr, CORE_ADDR *typep, CORE_ADDR *valp)
602 int sizeof_auxv_field = s390_target_wordsize ();
603 enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
604 gdb_byte *ptr = *readptr;
609 if (endptr - ptr < sizeof_auxv_field * 2)
612 *typep = extract_unsigned_integer (ptr, sizeof_auxv_field, byte_order);
613 ptr += sizeof_auxv_field;
614 *valp = extract_unsigned_integer (ptr, sizeof_auxv_field, byte_order);
615 ptr += sizeof_auxv_field;
623 s390_get_hwcap (void)
627 if (target_auxv_search (¤t_target, AT_HWCAP, &field))
628 return (unsigned long) field;
634 static const struct target_desc *
635 s390_read_description (struct target_ops *ops)
637 int tid = s390_inferior_tid ();
639 have_regset_last_break
640 = check_regset (tid, NT_S390_LAST_BREAK, 8);
641 have_regset_system_call
642 = check_regset (tid, NT_S390_SYSTEM_CALL, 4);
645 /* If GDB itself is compiled as 64-bit, we are running on a machine in
646 z/Architecture mode. If the target is running in 64-bit addressing
647 mode, report s390x architecture. If the target is running in 31-bit
648 addressing mode, but the kernel supports using 64-bit registers in
649 that mode, report s390 architecture with 64-bit GPRs. */
651 if (s390_target_wordsize () == 8)
652 return (have_regset_system_call? tdesc_s390x_linux64v2 :
653 have_regset_last_break? tdesc_s390x_linux64v1 :
654 tdesc_s390x_linux64);
656 if (s390_get_hwcap () & HWCAP_S390_HIGH_GPRS)
657 return (have_regset_system_call? tdesc_s390_linux64v2 :
658 have_regset_last_break? tdesc_s390_linux64v1 :
662 /* If GDB itself is compiled as 31-bit, or if we're running a 31-bit inferior
663 on a 64-bit kernel that does not support using 64-bit registers in 31-bit
664 mode, report s390 architecture with 32-bit GPRs. */
665 return (have_regset_system_call? tdesc_s390_linux32v2 :
666 have_regset_last_break? tdesc_s390_linux32v1 :
670 void _initialize_s390_nat (void);
673 _initialize_s390_nat (void)
675 struct target_ops *t;
677 /* Fill in the generic GNU/Linux methods. */
680 /* Add our register access methods. */
681 t->to_fetch_registers = s390_linux_fetch_inferior_registers;
682 t->to_store_registers = s390_linux_store_inferior_registers;
684 /* Add our watchpoint methods. */
685 t->to_can_use_hw_breakpoint = s390_can_use_hw_breakpoint;
686 t->to_region_ok_for_hw_watchpoint = s390_region_ok_for_hw_watchpoint;
687 t->to_have_continuable_watchpoint = 1;
688 t->to_stopped_by_watchpoint = s390_stopped_by_watchpoint;
689 t->to_insert_watchpoint = s390_insert_watchpoint;
690 t->to_remove_watchpoint = s390_remove_watchpoint;
692 /* Detect target architecture. */
693 t->to_read_description = s390_read_description;
694 t->to_auxv_parse = s390_auxv_parse;
696 /* Register the target. */
697 linux_nat_add_target (t);
698 linux_nat_set_new_thread (t, s390_fix_watch_points);