1 /* S390 native-dependent code for GDB, the GNU debugger.
2 Copyright (C) 2001, 2003-2007, 2009, 2012 Free Software Foundation,
6 for IBM Deutschland Entwicklung GmbH, IBM Corporation.
8 This file is part of GDB.
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>. */
27 #include "linux-nat.h"
31 #include "s390-tdep.h"
32 #include "elf/common.h"
34 #include <asm/ptrace.h>
35 #include <sys/ptrace.h>
36 #include <asm/types.h>
37 #include <sys/procfs.h>
38 #include <sys/ucontext.h>
41 #ifndef HWCAP_S390_HIGH_GPRS
42 #define HWCAP_S390_HIGH_GPRS 512
45 #ifndef PTRACE_GETREGSET
46 #define PTRACE_GETREGSET 0x4204
49 #ifndef PTRACE_SETREGSET
50 #define PTRACE_SETREGSET 0x4205
53 static int have_regset_last_break = 0;
54 static int have_regset_system_call = 0;
56 /* Map registers to gregset/ptrace offsets.
57 These arrays are defined in s390-tdep.c. */
60 #define regmap_gregset s390x_regmap_gregset
62 #define regmap_gregset s390_regmap_gregset
65 #define regmap_fpregset s390_regmap_fpregset
67 /* When debugging a 32-bit executable running under a 64-bit kernel,
68 we have to fix up the 64-bit registers we get from the kernel
69 to make them look like 32-bit registers. */
72 s390_native_supply (struct regcache *regcache, int regno,
73 const gdb_byte *regp, int *regmap)
75 int offset = regmap[regno];
78 struct gdbarch *gdbarch = get_regcache_arch (regcache);
79 if (offset != -1 && gdbarch_ptr_bit (gdbarch) == 32)
81 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
83 if (regno == S390_PSWM_REGNUM)
88 pswm = extract_unsigned_integer (regp + regmap[S390_PSWM_REGNUM],
91 store_unsigned_integer (buf, 4, byte_order, (pswm >> 32) | 0x80000);
92 regcache_raw_supply (regcache, regno, buf);
96 if (regno == S390_PSWA_REGNUM)
101 pswa = extract_unsigned_integer (regp + regmap[S390_PSWA_REGNUM],
103 pswm = extract_unsigned_integer (regp + regmap[S390_PSWM_REGNUM],
106 store_unsigned_integer (buf, 4, byte_order,
107 (pswa & 0x7fffffff) | (pswm & 0x80000000));
108 regcache_raw_supply (regcache, regno, buf);
112 if ((regno >= S390_R0_REGNUM && regno <= S390_R15_REGNUM)
113 || regno == S390_ORIG_R2_REGNUM)
119 regcache_raw_supply (regcache, regno, regp + offset);
123 s390_native_collect (const struct regcache *regcache, int regno,
124 gdb_byte *regp, int *regmap)
126 int offset = regmap[regno];
129 struct gdbarch *gdbarch = get_regcache_arch (regcache);
130 if (offset != -1 && gdbarch_ptr_bit (gdbarch) == 32)
132 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
134 if (regno == S390_PSWM_REGNUM)
139 regcache_raw_collect (regcache, regno, buf);
140 pswm = extract_unsigned_integer (buf, 4, byte_order);
142 /* We don't know the final addressing mode until the PSW address
143 is known, so leave it as-is. When the PSW address is collected
144 (below), the addressing mode will be updated. */
145 store_unsigned_integer (regp + regmap[S390_PSWM_REGNUM],
146 4, byte_order, pswm & 0xfff7ffff);
150 if (regno == S390_PSWA_REGNUM)
155 regcache_raw_collect (regcache, regno, buf);
156 pswa = extract_unsigned_integer (buf, 4, byte_order);
158 store_unsigned_integer (regp + regmap[S390_PSWA_REGNUM],
159 8, byte_order, pswa & 0x7fffffff);
161 /* Update basic addressing mode bit in PSW mask, see above. */
162 store_unsigned_integer (regp + regmap[S390_PSWM_REGNUM] + 4,
163 4, byte_order, pswa & 0x80000000);
167 if ((regno >= S390_R0_REGNUM && regno <= S390_R15_REGNUM)
168 || regno == S390_ORIG_R2_REGNUM)
170 memset (regp + offset, 0, 4);
177 regcache_raw_collect (regcache, regno, regp + offset);
180 /* Fill GDB's register array with the general-purpose register values
183 supply_gregset (struct regcache *regcache, const gregset_t *regp)
186 for (i = 0; i < S390_NUM_REGS; i++)
187 s390_native_supply (regcache, i, (const gdb_byte *) regp, regmap_gregset);
190 /* Fill register REGNO (if it is a general-purpose register) in
191 *REGP with the value in GDB's register array. If REGNO is -1,
192 do this for all registers. */
194 fill_gregset (const struct regcache *regcache, gregset_t *regp, int regno)
197 for (i = 0; i < S390_NUM_REGS; i++)
198 if (regno == -1 || regno == i)
199 s390_native_collect (regcache, i, (gdb_byte *) regp, regmap_gregset);
202 /* Fill GDB's register array with the floating-point register values
205 supply_fpregset (struct regcache *regcache, const fpregset_t *regp)
208 for (i = 0; i < S390_NUM_REGS; i++)
209 s390_native_supply (regcache, i, (const gdb_byte *) regp, regmap_fpregset);
212 /* Fill register REGNO (if it is a general-purpose register) in
213 *REGP with the value in GDB's register array. If REGNO is -1,
214 do this for all registers. */
216 fill_fpregset (const struct regcache *regcache, fpregset_t *regp, int regno)
219 for (i = 0; i < S390_NUM_REGS; i++)
220 if (regno == -1 || regno == i)
221 s390_native_collect (regcache, i, (gdb_byte *) regp, regmap_fpregset);
224 /* Find the TID for the current inferior thread to use with ptrace. */
226 s390_inferior_tid (void)
228 /* GNU/Linux LWP ID's are process ID's. */
229 int tid = TIDGET (inferior_ptid);
231 tid = PIDGET (inferior_ptid); /* Not a threaded program. */
236 /* Fetch all general-purpose registers from process/thread TID and
237 store their values in GDB's register cache. */
239 fetch_regs (struct regcache *regcache, int tid)
244 parea.len = sizeof (regs);
245 parea.process_addr = (addr_t) ®s;
246 parea.kernel_addr = offsetof (struct user_regs_struct, psw);
247 if (ptrace (PTRACE_PEEKUSR_AREA, tid, (long) &parea) < 0)
248 perror_with_name (_("Couldn't get registers"));
250 supply_gregset (regcache, (const gregset_t *) ®s);
253 /* Store all valid general-purpose registers in GDB's register cache
254 into the process/thread specified by TID. */
256 store_regs (const struct regcache *regcache, int tid, int regnum)
261 parea.len = sizeof (regs);
262 parea.process_addr = (addr_t) ®s;
263 parea.kernel_addr = offsetof (struct user_regs_struct, psw);
264 if (ptrace (PTRACE_PEEKUSR_AREA, tid, (long) &parea) < 0)
265 perror_with_name (_("Couldn't get registers"));
267 fill_gregset (regcache, ®s, regnum);
269 if (ptrace (PTRACE_POKEUSR_AREA, tid, (long) &parea) < 0)
270 perror_with_name (_("Couldn't write registers"));
273 /* Fetch all floating-point registers from process/thread TID and store
274 their values in GDB's register cache. */
276 fetch_fpregs (struct regcache *regcache, int tid)
281 parea.len = sizeof (fpregs);
282 parea.process_addr = (addr_t) &fpregs;
283 parea.kernel_addr = offsetof (struct user_regs_struct, fp_regs);
284 if (ptrace (PTRACE_PEEKUSR_AREA, tid, (long) &parea) < 0)
285 perror_with_name (_("Couldn't get floating point status"));
287 supply_fpregset (regcache, (const fpregset_t *) &fpregs);
290 /* Store all valid floating-point registers in GDB's register cache
291 into the process/thread specified by TID. */
293 store_fpregs (const struct regcache *regcache, int tid, int regnum)
298 parea.len = sizeof (fpregs);
299 parea.process_addr = (addr_t) &fpregs;
300 parea.kernel_addr = offsetof (struct user_regs_struct, fp_regs);
301 if (ptrace (PTRACE_PEEKUSR_AREA, tid, (long) &parea) < 0)
302 perror_with_name (_("Couldn't get floating point status"));
304 fill_fpregset (regcache, &fpregs, regnum);
306 if (ptrace (PTRACE_POKEUSR_AREA, tid, (long) &parea) < 0)
307 perror_with_name (_("Couldn't write floating point status"));
310 /* Fetch all registers in the kernel's register set whose number is REGSET,
311 whose size is REGSIZE, and whose layout is described by REGMAP, from
312 process/thread TID and store their values in GDB's register cache. */
314 fetch_regset (struct regcache *regcache, int tid,
315 int regset, int regsize, int *regmap)
317 struct gdbarch *gdbarch = get_regcache_arch (regcache);
318 gdb_byte *buf = alloca (regsize);
323 iov.iov_len = regsize;
325 if (ptrace (PTRACE_GETREGSET, tid, (long) regset, (long) &iov) < 0)
326 perror_with_name (_("Couldn't get register set"));
328 for (i = 0; i < S390_NUM_REGS; i++)
329 s390_native_supply (regcache, i, buf, regmap);
332 /* Store all registers in the kernel's register set whose number is REGSET,
333 whose size is REGSIZE, and whose layout is described by REGMAP, from
334 GDB's register cache back to process/thread TID. */
336 store_regset (struct regcache *regcache, int tid,
337 int regset, int regsize, int *regmap)
339 struct gdbarch *gdbarch = get_regcache_arch (regcache);
340 gdb_byte *buf = alloca (regsize);
345 iov.iov_len = regsize;
347 if (ptrace (PTRACE_GETREGSET, tid, (long) regset, (long) &iov) < 0)
348 perror_with_name (_("Couldn't get register set"));
350 for (i = 0; i < S390_NUM_REGS; i++)
351 s390_native_collect (regcache, i, buf, regmap);
353 if (ptrace (PTRACE_SETREGSET, tid, (long) regset, (long) &iov) < 0)
354 perror_with_name (_("Couldn't set register set"));
357 /* Check whether the kernel provides a register set with number REGSET
358 of size REGSIZE for process/thread TID. */
360 check_regset (int tid, int regset, int regsize)
362 gdb_byte *buf = alloca (regsize);
366 iov.iov_len = regsize;
368 if (ptrace (PTRACE_GETREGSET, tid, (long) regset, (long) &iov) < 0)
374 /* Fetch register REGNUM from the child process. If REGNUM is -1, do
375 this for all registers. */
377 s390_linux_fetch_inferior_registers (struct target_ops *ops,
378 struct regcache *regcache, int regnum)
380 int tid = s390_inferior_tid ();
383 || (regnum < S390_NUM_REGS && regmap_gregset[regnum] != -1))
384 fetch_regs (regcache, tid);
387 || (regnum < S390_NUM_REGS && regmap_fpregset[regnum] != -1))
388 fetch_fpregs (regcache, tid);
390 if (have_regset_last_break)
391 if (regnum == -1 || regnum == S390_LAST_BREAK_REGNUM)
392 fetch_regset (regcache, tid, NT_S390_LAST_BREAK, 8,
393 (gdbarch_ptr_bit (get_regcache_arch (regcache)) == 32
394 ? s390_regmap_last_break : s390x_regmap_last_break));
396 if (have_regset_system_call)
397 if (regnum == -1 || regnum == S390_SYSTEM_CALL_REGNUM)
398 fetch_regset (regcache, tid, NT_S390_SYSTEM_CALL, 4,
399 s390_regmap_system_call);
402 /* Store register REGNUM back into the child process. If REGNUM is
403 -1, do this for all registers. */
405 s390_linux_store_inferior_registers (struct target_ops *ops,
406 struct regcache *regcache, int regnum)
408 int tid = s390_inferior_tid ();
411 || (regnum < S390_NUM_REGS && regmap_gregset[regnum] != -1))
412 store_regs (regcache, tid, regnum);
415 || (regnum < S390_NUM_REGS && regmap_fpregset[regnum] != -1))
416 store_fpregs (regcache, tid, regnum);
418 /* S390_LAST_BREAK_REGNUM is read-only. */
420 if (have_regset_system_call)
421 if (regnum == -1 || regnum == S390_SYSTEM_CALL_REGNUM)
422 store_regset (regcache, tid, NT_S390_SYSTEM_CALL, 4,
423 s390_regmap_system_call);
427 /* Hardware-assisted watchpoint handling. */
429 /* We maintain a list of all currently active watchpoints in order
430 to properly handle watchpoint removal.
432 The only thing we actually need is the total address space area
433 spanned by the watchpoints. */
437 struct watch_area *next;
442 static struct watch_area *watch_base = NULL;
445 s390_stopped_by_watchpoint (void)
447 per_lowcore_bits per_lowcore;
451 /* Speed up common case. */
455 parea.len = sizeof (per_lowcore);
456 parea.process_addr = (addr_t) & per_lowcore;
457 parea.kernel_addr = offsetof (struct user_regs_struct, per_info.lowcore);
458 if (ptrace (PTRACE_PEEKUSR_AREA, s390_inferior_tid (), &parea) < 0)
459 perror_with_name (_("Couldn't retrieve watchpoint status"));
461 result = (per_lowcore.perc_storage_alteration == 1
462 && per_lowcore.perc_store_real_address == 0);
466 /* Do not report this watchpoint again. */
467 memset (&per_lowcore, 0, sizeof (per_lowcore));
468 if (ptrace (PTRACE_POKEUSR_AREA, s390_inferior_tid (), &parea) < 0)
469 perror_with_name (_("Couldn't clear watchpoint status"));
476 s390_fix_watch_points (struct lwp_info *lp)
483 CORE_ADDR watch_lo_addr = (CORE_ADDR)-1, watch_hi_addr = 0;
484 struct watch_area *area;
486 tid = TIDGET (lp->ptid);
488 tid = PIDGET (lp->ptid);
490 for (area = watch_base; area; area = area->next)
492 watch_lo_addr = min (watch_lo_addr, area->lo_addr);
493 watch_hi_addr = max (watch_hi_addr, area->hi_addr);
496 parea.len = sizeof (per_info);
497 parea.process_addr = (addr_t) & per_info;
498 parea.kernel_addr = offsetof (struct user_regs_struct, per_info);
499 if (ptrace (PTRACE_PEEKUSR_AREA, tid, &parea) < 0)
500 perror_with_name (_("Couldn't retrieve watchpoint status"));
504 per_info.control_regs.bits.em_storage_alteration = 1;
505 per_info.control_regs.bits.storage_alt_space_ctl = 1;
509 per_info.control_regs.bits.em_storage_alteration = 0;
510 per_info.control_regs.bits.storage_alt_space_ctl = 0;
512 per_info.starting_addr = watch_lo_addr;
513 per_info.ending_addr = watch_hi_addr;
515 if (ptrace (PTRACE_POKEUSR_AREA, tid, &parea) < 0)
516 perror_with_name (_("Couldn't modify watchpoint status"));
520 s390_insert_watchpoint (CORE_ADDR addr, int len, int type,
521 struct expression *cond)
524 struct watch_area *area = xmalloc (sizeof (struct watch_area));
529 area->lo_addr = addr;
530 area->hi_addr = addr + len - 1;
532 area->next = watch_base;
536 s390_fix_watch_points (lp);
541 s390_remove_watchpoint (CORE_ADDR addr, int len, int type,
542 struct expression *cond)
545 struct watch_area *area, **parea;
547 for (parea = &watch_base; *parea; parea = &(*parea)->next)
548 if ((*parea)->lo_addr == addr
549 && (*parea)->hi_addr == addr + len - 1)
554 fprintf_unfiltered (gdb_stderr,
555 "Attempt to remove nonexistent watchpoint.\n");
564 s390_fix_watch_points (lp);
569 s390_can_use_hw_breakpoint (int type, int cnt, int othertype)
571 return type == bp_hardware_watchpoint;
575 s390_region_ok_for_hw_watchpoint (CORE_ADDR addr, int cnt)
581 s390_target_wordsize (void)
585 /* Check for 64-bit inferior process. This is the case when the host is
586 64-bit, and in addition bit 32 of the PSW mask is set. */
591 pswm = (long) ptrace (PTRACE_PEEKUSER, s390_inferior_tid (), PT_PSWMASK, 0);
592 if (errno == 0 && (pswm & 0x100000000ul) != 0)
600 s390_auxv_parse (struct target_ops *ops, gdb_byte **readptr,
601 gdb_byte *endptr, CORE_ADDR *typep, CORE_ADDR *valp)
603 int sizeof_auxv_field = s390_target_wordsize ();
604 enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch);
605 gdb_byte *ptr = *readptr;
610 if (endptr - ptr < sizeof_auxv_field * 2)
613 *typep = extract_unsigned_integer (ptr, sizeof_auxv_field, byte_order);
614 ptr += sizeof_auxv_field;
615 *valp = extract_unsigned_integer (ptr, sizeof_auxv_field, byte_order);
616 ptr += sizeof_auxv_field;
624 s390_get_hwcap (void)
628 if (target_auxv_search (¤t_target, AT_HWCAP, &field))
629 return (unsigned long) field;
635 static const struct target_desc *
636 s390_read_description (struct target_ops *ops)
638 int tid = s390_inferior_tid ();
640 have_regset_last_break
641 = check_regset (tid, NT_S390_LAST_BREAK, 8);
642 have_regset_system_call
643 = check_regset (tid, NT_S390_SYSTEM_CALL, 4);
646 /* If GDB itself is compiled as 64-bit, we are running on a machine in
647 z/Architecture mode. If the target is running in 64-bit addressing
648 mode, report s390x architecture. If the target is running in 31-bit
649 addressing mode, but the kernel supports using 64-bit registers in
650 that mode, report s390 architecture with 64-bit GPRs. */
652 if (s390_target_wordsize () == 8)
653 return (have_regset_system_call? tdesc_s390x_linux64v2 :
654 have_regset_last_break? tdesc_s390x_linux64v1 :
655 tdesc_s390x_linux64);
657 if (s390_get_hwcap () & HWCAP_S390_HIGH_GPRS)
658 return (have_regset_system_call? tdesc_s390_linux64v2 :
659 have_regset_last_break? tdesc_s390_linux64v1 :
663 /* If GDB itself is compiled as 31-bit, or if we're running a 31-bit inferior
664 on a 64-bit kernel that does not support using 64-bit registers in 31-bit
665 mode, report s390 architecture with 32-bit GPRs. */
666 return (have_regset_system_call? tdesc_s390_linux32v2 :
667 have_regset_last_break? tdesc_s390_linux32v1 :
671 void _initialize_s390_nat (void);
674 _initialize_s390_nat (void)
676 struct target_ops *t;
678 /* Fill in the generic GNU/Linux methods. */
681 /* Add our register access methods. */
682 t->to_fetch_registers = s390_linux_fetch_inferior_registers;
683 t->to_store_registers = s390_linux_store_inferior_registers;
685 /* Add our watchpoint methods. */
686 t->to_can_use_hw_breakpoint = s390_can_use_hw_breakpoint;
687 t->to_region_ok_for_hw_watchpoint = s390_region_ok_for_hw_watchpoint;
688 t->to_have_continuable_watchpoint = 1;
689 t->to_stopped_by_watchpoint = s390_stopped_by_watchpoint;
690 t->to_insert_watchpoint = s390_insert_watchpoint;
691 t->to_remove_watchpoint = s390_remove_watchpoint;
693 /* Detect target architecture. */
694 t->to_read_description = s390_read_description;
695 t->to_auxv_parse = s390_auxv_parse;
697 /* Register the target. */
698 linux_nat_add_target (t);
699 linux_nat_set_new_thread (t, s390_fix_watch_points);