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 /* Fill the regset described by MAP into REGCACHE, using the values
67 from REGP. The MAP array represents each register as a pair
68 (offset, regno) of short integers and is terminated with -1. */
71 s390_native_supply (struct regcache
*regcache
, const short *map
,
74 for (; map
[0] >= 0; map
+= 2)
75 regcache_raw_supply (regcache
, map
[1], regp
+ map
[0]);
78 /* Collect the register REGNO out of the regset described by MAP from
79 REGCACHE into REGP. If REGNO == -1, do this for all registers in
83 s390_native_collect (const struct regcache
*regcache
, const short *map
,
84 int regno
, gdb_byte
*regp
)
86 for (; map
[0] >= 0; map
+= 2)
87 if (regno
== -1 || regno
== map
[1])
88 regcache_raw_collect (regcache
, map
[1], regp
+ map
[0]);
91 /* Fill GDB's register array with the general-purpose register values
94 When debugging a 32-bit executable running under a 64-bit kernel,
95 we have to fix up the 64-bit registers we get from the kernel to
96 make them look like 32-bit registers. */
99 supply_gregset (struct regcache
*regcache
, const gregset_t
*regp
)
102 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
103 if (gdbarch_ptr_bit (gdbarch
) == 32)
105 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
106 ULONGEST pswm
= 0, pswa
= 0;
110 for (map
= regmap_gregset
; map
[0] >= 0; map
+= 2)
112 const gdb_byte
*p
= (const gdb_byte
*) regp
+ map
[0];
115 if (regno
== S390_PSWM_REGNUM
)
116 pswm
= extract_unsigned_integer (p
, 8, byte_order
);
117 else if (regno
== S390_PSWA_REGNUM
)
118 pswa
= extract_unsigned_integer (p
, 8, byte_order
);
121 if ((regno
>= S390_R0_REGNUM
&& regno
<= S390_R15_REGNUM
)
122 || regno
== S390_ORIG_R2_REGNUM
)
124 regcache_raw_supply (regcache
, regno
, p
);
128 store_unsigned_integer (buf
, 4, byte_order
, (pswm
>> 32) | 0x80000);
129 regcache_raw_supply (regcache
, S390_PSWM_REGNUM
, buf
);
130 store_unsigned_integer (buf
, 4, byte_order
,
131 (pswa
& 0x7fffffff) | (pswm
& 0x80000000));
132 regcache_raw_supply (regcache
, S390_PSWA_REGNUM
, buf
);
137 s390_native_supply (regcache
, regmap_gregset
, (const gdb_byte
*) regp
);
140 /* Fill register REGNO (if it is a general-purpose register) in
141 *REGP with the value in GDB's register array. If REGNO is -1,
142 do this for all registers. */
145 fill_gregset (const struct regcache
*regcache
, gregset_t
*regp
, int regno
)
148 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
149 if (gdbarch_ptr_bit (gdbarch
) == 32)
154 for (map
= regmap_gregset
; map
[0] >= 0; map
+= 2)
156 gdb_byte
*p
= (gdb_byte
*) regp
+ map
[0];
159 if (reg
>= S390_PSWM_REGNUM
&& reg
<= S390_PSWA_REGNUM
)
160 psw_p
[reg
- S390_PSWM_REGNUM
] = p
;
162 else if (regno
== -1 || regno
== reg
)
164 if ((reg
>= S390_R0_REGNUM
&& reg
<= S390_R15_REGNUM
)
165 || reg
== S390_ORIG_R2_REGNUM
)
170 regcache_raw_collect (regcache
, reg
, p
+ 4);
175 || regno
== S390_PSWM_REGNUM
|| regno
== S390_PSWA_REGNUM
)
177 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
181 regcache_raw_collect (regcache
, S390_PSWM_REGNUM
, buf
);
182 pswm
= extract_unsigned_integer (buf
, 4, byte_order
);
183 regcache_raw_collect (regcache
, S390_PSWA_REGNUM
, buf
);
184 pswa
= extract_unsigned_integer (buf
, 4, byte_order
);
186 if (regno
== -1 || regno
== S390_PSWM_REGNUM
)
187 store_unsigned_integer (psw_p
[0], 8, byte_order
,
188 ((pswm
& 0xfff7ffff) << 32) |
189 (pswa
& 0x80000000));
190 if (regno
== -1 || regno
== S390_PSWA_REGNUM
)
191 store_unsigned_integer (psw_p
[1], 8, byte_order
,
198 s390_native_collect (regcache
, regmap_gregset
, regno
, (gdb_byte
*) regp
);
201 /* Fill GDB's register array with the floating-point register values
204 supply_fpregset (struct regcache
*regcache
, const fpregset_t
*regp
)
206 s390_native_supply (regcache
, regmap_fpregset
, (const gdb_byte
*) regp
);
209 /* Fill register REGNO (if it is a general-purpose register) in
210 *REGP with the value in GDB's register array. If REGNO is -1,
211 do this for all registers. */
213 fill_fpregset (const struct regcache
*regcache
, fpregset_t
*regp
, int regno
)
215 s390_native_collect (regcache
, regmap_fpregset
, regno
, (gdb_byte
*) regp
);
218 /* Find the TID for the current inferior thread to use with ptrace. */
220 s390_inferior_tid (void)
222 /* GNU/Linux LWP ID's are process ID's. */
223 int tid
= TIDGET (inferior_ptid
);
225 tid
= PIDGET (inferior_ptid
); /* Not a threaded program. */
230 /* Fetch all general-purpose registers from process/thread TID and
231 store their values in GDB's register cache. */
233 fetch_regs (struct regcache
*regcache
, int tid
)
238 parea
.len
= sizeof (regs
);
239 parea
.process_addr
= (addr_t
) ®s
;
240 parea
.kernel_addr
= offsetof (struct user_regs_struct
, psw
);
241 if (ptrace (PTRACE_PEEKUSR_AREA
, tid
, (long) &parea
) < 0)
242 perror_with_name (_("Couldn't get registers"));
244 supply_gregset (regcache
, (const gregset_t
*) ®s
);
247 /* Store all valid general-purpose registers in GDB's register cache
248 into the process/thread specified by TID. */
250 store_regs (const struct regcache
*regcache
, int tid
, int regnum
)
255 parea
.len
= sizeof (regs
);
256 parea
.process_addr
= (addr_t
) ®s
;
257 parea
.kernel_addr
= offsetof (struct user_regs_struct
, psw
);
258 if (ptrace (PTRACE_PEEKUSR_AREA
, tid
, (long) &parea
) < 0)
259 perror_with_name (_("Couldn't get registers"));
261 fill_gregset (regcache
, ®s
, regnum
);
263 if (ptrace (PTRACE_POKEUSR_AREA
, tid
, (long) &parea
) < 0)
264 perror_with_name (_("Couldn't write registers"));
267 /* Fetch all floating-point registers from process/thread TID and store
268 their values in GDB's register cache. */
270 fetch_fpregs (struct regcache
*regcache
, int tid
)
275 parea
.len
= sizeof (fpregs
);
276 parea
.process_addr
= (addr_t
) &fpregs
;
277 parea
.kernel_addr
= offsetof (struct user_regs_struct
, fp_regs
);
278 if (ptrace (PTRACE_PEEKUSR_AREA
, tid
, (long) &parea
) < 0)
279 perror_with_name (_("Couldn't get floating point status"));
281 supply_fpregset (regcache
, (const fpregset_t
*) &fpregs
);
284 /* Store all valid floating-point registers in GDB's register cache
285 into the process/thread specified by TID. */
287 store_fpregs (const struct regcache
*regcache
, int tid
, int regnum
)
292 parea
.len
= sizeof (fpregs
);
293 parea
.process_addr
= (addr_t
) &fpregs
;
294 parea
.kernel_addr
= offsetof (struct user_regs_struct
, fp_regs
);
295 if (ptrace (PTRACE_PEEKUSR_AREA
, tid
, (long) &parea
) < 0)
296 perror_with_name (_("Couldn't get floating point status"));
298 fill_fpregset (regcache
, &fpregs
, regnum
);
300 if (ptrace (PTRACE_POKEUSR_AREA
, tid
, (long) &parea
) < 0)
301 perror_with_name (_("Couldn't write floating point status"));
304 /* Fetch all registers in the kernel's register set whose number is REGSET,
305 whose size is REGSIZE, and whose layout is described by REGMAP, from
306 process/thread TID and store their values in GDB's register cache. */
308 fetch_regset (struct regcache
*regcache
, int tid
,
309 int regset
, int regsize
, const short *regmap
)
311 gdb_byte
*buf
= alloca (regsize
);
315 iov
.iov_len
= regsize
;
317 if (ptrace (PTRACE_GETREGSET
, tid
, (long) regset
, (long) &iov
) < 0)
318 perror_with_name (_("Couldn't get register set"));
320 s390_native_supply (regcache
, regmap
, buf
);
323 /* Store all registers in the kernel's register set whose number is REGSET,
324 whose size is REGSIZE, and whose layout is described by REGMAP, from
325 GDB's register cache back to process/thread TID. */
327 store_regset (struct regcache
*regcache
, int tid
,
328 int regset
, int regsize
, const short *regmap
)
330 gdb_byte
*buf
= alloca (regsize
);
334 iov
.iov_len
= regsize
;
336 if (ptrace (PTRACE_GETREGSET
, tid
, (long) regset
, (long) &iov
) < 0)
337 perror_with_name (_("Couldn't get register set"));
339 s390_native_collect (regcache
, regmap
, -1, buf
);
341 if (ptrace (PTRACE_SETREGSET
, tid
, (long) regset
, (long) &iov
) < 0)
342 perror_with_name (_("Couldn't set register set"));
345 /* Check whether the kernel provides a register set with number REGSET
346 of size REGSIZE for process/thread TID. */
348 check_regset (int tid
, int regset
, int regsize
)
350 gdb_byte
*buf
= alloca (regsize
);
354 iov
.iov_len
= regsize
;
356 if (ptrace (PTRACE_GETREGSET
, tid
, (long) regset
, (long) &iov
) < 0)
362 /* Fetch register REGNUM from the child process. If REGNUM is -1, do
363 this for all registers. */
365 s390_linux_fetch_inferior_registers (struct target_ops
*ops
,
366 struct regcache
*regcache
, int regnum
)
368 int tid
= s390_inferior_tid ();
370 if (regnum
== -1 || S390_IS_GREGSET_REGNUM (regnum
))
371 fetch_regs (regcache
, tid
);
373 if (regnum
== -1 || S390_IS_FPREGSET_REGNUM (regnum
))
374 fetch_fpregs (regcache
, tid
);
376 if (have_regset_last_break
)
377 if (regnum
== -1 || regnum
== S390_LAST_BREAK_REGNUM
)
378 fetch_regset (regcache
, tid
, NT_S390_LAST_BREAK
, 8,
379 (gdbarch_ptr_bit (get_regcache_arch (regcache
)) == 32
380 ? s390_regmap_last_break
: s390x_regmap_last_break
));
382 if (have_regset_system_call
)
383 if (regnum
== -1 || regnum
== S390_SYSTEM_CALL_REGNUM
)
384 fetch_regset (regcache
, tid
, NT_S390_SYSTEM_CALL
, 4,
385 s390_regmap_system_call
);
388 /* Store register REGNUM back into the child process. If REGNUM is
389 -1, do this for all registers. */
391 s390_linux_store_inferior_registers (struct target_ops
*ops
,
392 struct regcache
*regcache
, int regnum
)
394 int tid
= s390_inferior_tid ();
396 if (regnum
== -1 || S390_IS_GREGSET_REGNUM (regnum
))
397 store_regs (regcache
, tid
, regnum
);
399 if (regnum
== -1 || S390_IS_FPREGSET_REGNUM (regnum
))
400 store_fpregs (regcache
, tid
, regnum
);
402 /* S390_LAST_BREAK_REGNUM is read-only. */
404 if (have_regset_system_call
)
405 if (regnum
== -1 || regnum
== S390_SYSTEM_CALL_REGNUM
)
406 store_regset (regcache
, tid
, NT_S390_SYSTEM_CALL
, 4,
407 s390_regmap_system_call
);
411 /* Hardware-assisted watchpoint handling. */
413 /* We maintain a list of all currently active watchpoints in order
414 to properly handle watchpoint removal.
416 The only thing we actually need is the total address space area
417 spanned by the watchpoints. */
421 struct watch_area
*next
;
426 static struct watch_area
*watch_base
= NULL
;
429 s390_stopped_by_watchpoint (void)
431 per_lowcore_bits per_lowcore
;
435 /* Speed up common case. */
439 parea
.len
= sizeof (per_lowcore
);
440 parea
.process_addr
= (addr_t
) & per_lowcore
;
441 parea
.kernel_addr
= offsetof (struct user_regs_struct
, per_info
.lowcore
);
442 if (ptrace (PTRACE_PEEKUSR_AREA
, s390_inferior_tid (), &parea
) < 0)
443 perror_with_name (_("Couldn't retrieve watchpoint status"));
445 result
= (per_lowcore
.perc_storage_alteration
== 1
446 && per_lowcore
.perc_store_real_address
== 0);
450 /* Do not report this watchpoint again. */
451 memset (&per_lowcore
, 0, sizeof (per_lowcore
));
452 if (ptrace (PTRACE_POKEUSR_AREA
, s390_inferior_tid (), &parea
) < 0)
453 perror_with_name (_("Couldn't clear watchpoint status"));
460 s390_fix_watch_points (struct lwp_info
*lp
)
467 CORE_ADDR watch_lo_addr
= (CORE_ADDR
)-1, watch_hi_addr
= 0;
468 struct watch_area
*area
;
470 tid
= TIDGET (lp
->ptid
);
472 tid
= PIDGET (lp
->ptid
);
474 for (area
= watch_base
; area
; area
= area
->next
)
476 watch_lo_addr
= min (watch_lo_addr
, area
->lo_addr
);
477 watch_hi_addr
= max (watch_hi_addr
, area
->hi_addr
);
480 parea
.len
= sizeof (per_info
);
481 parea
.process_addr
= (addr_t
) & per_info
;
482 parea
.kernel_addr
= offsetof (struct user_regs_struct
, per_info
);
483 if (ptrace (PTRACE_PEEKUSR_AREA
, tid
, &parea
) < 0)
484 perror_with_name (_("Couldn't retrieve watchpoint status"));
488 per_info
.control_regs
.bits
.em_storage_alteration
= 1;
489 per_info
.control_regs
.bits
.storage_alt_space_ctl
= 1;
493 per_info
.control_regs
.bits
.em_storage_alteration
= 0;
494 per_info
.control_regs
.bits
.storage_alt_space_ctl
= 0;
496 per_info
.starting_addr
= watch_lo_addr
;
497 per_info
.ending_addr
= watch_hi_addr
;
499 if (ptrace (PTRACE_POKEUSR_AREA
, tid
, &parea
) < 0)
500 perror_with_name (_("Couldn't modify watchpoint status"));
504 s390_insert_watchpoint (CORE_ADDR addr
, int len
, int type
,
505 struct expression
*cond
)
508 struct watch_area
*area
= xmalloc (sizeof (struct watch_area
));
513 area
->lo_addr
= addr
;
514 area
->hi_addr
= addr
+ len
- 1;
516 area
->next
= watch_base
;
520 s390_fix_watch_points (lp
);
525 s390_remove_watchpoint (CORE_ADDR addr
, int len
, int type
,
526 struct expression
*cond
)
529 struct watch_area
*area
, **parea
;
531 for (parea
= &watch_base
; *parea
; parea
= &(*parea
)->next
)
532 if ((*parea
)->lo_addr
== addr
533 && (*parea
)->hi_addr
== addr
+ len
- 1)
538 fprintf_unfiltered (gdb_stderr
,
539 "Attempt to remove nonexistent watchpoint.\n");
548 s390_fix_watch_points (lp
);
553 s390_can_use_hw_breakpoint (int type
, int cnt
, int othertype
)
555 return type
== bp_hardware_watchpoint
;
559 s390_region_ok_for_hw_watchpoint (CORE_ADDR addr
, int cnt
)
565 s390_target_wordsize (void)
569 /* Check for 64-bit inferior process. This is the case when the host is
570 64-bit, and in addition bit 32 of the PSW mask is set. */
575 pswm
= (long) ptrace (PTRACE_PEEKUSER
, s390_inferior_tid (), PT_PSWMASK
, 0);
576 if (errno
== 0 && (pswm
& 0x100000000ul
) != 0)
584 s390_auxv_parse (struct target_ops
*ops
, gdb_byte
**readptr
,
585 gdb_byte
*endptr
, CORE_ADDR
*typep
, CORE_ADDR
*valp
)
587 int sizeof_auxv_field
= s390_target_wordsize ();
588 enum bfd_endian byte_order
= gdbarch_byte_order (target_gdbarch ());
589 gdb_byte
*ptr
= *readptr
;
594 if (endptr
- ptr
< sizeof_auxv_field
* 2)
597 *typep
= extract_unsigned_integer (ptr
, sizeof_auxv_field
, byte_order
);
598 ptr
+= sizeof_auxv_field
;
599 *valp
= extract_unsigned_integer (ptr
, sizeof_auxv_field
, byte_order
);
600 ptr
+= sizeof_auxv_field
;
608 s390_get_hwcap (void)
612 if (target_auxv_search (¤t_target
, AT_HWCAP
, &field
))
613 return (unsigned long) field
;
619 static const struct target_desc
*
620 s390_read_description (struct target_ops
*ops
)
622 int tid
= s390_inferior_tid ();
624 have_regset_last_break
625 = check_regset (tid
, NT_S390_LAST_BREAK
, 8);
626 have_regset_system_call
627 = check_regset (tid
, NT_S390_SYSTEM_CALL
, 4);
630 /* If GDB itself is compiled as 64-bit, we are running on a machine in
631 z/Architecture mode. If the target is running in 64-bit addressing
632 mode, report s390x architecture. If the target is running in 31-bit
633 addressing mode, but the kernel supports using 64-bit registers in
634 that mode, report s390 architecture with 64-bit GPRs. */
636 if (s390_target_wordsize () == 8)
637 return (have_regset_system_call
? tdesc_s390x_linux64v2
:
638 have_regset_last_break
? tdesc_s390x_linux64v1
:
639 tdesc_s390x_linux64
);
641 if (s390_get_hwcap () & HWCAP_S390_HIGH_GPRS
)
642 return (have_regset_system_call
? tdesc_s390_linux64v2
:
643 have_regset_last_break
? tdesc_s390_linux64v1
:
647 /* If GDB itself is compiled as 31-bit, or if we're running a 31-bit inferior
648 on a 64-bit kernel that does not support using 64-bit registers in 31-bit
649 mode, report s390 architecture with 32-bit GPRs. */
650 return (have_regset_system_call
? tdesc_s390_linux32v2
:
651 have_regset_last_break
? tdesc_s390_linux32v1
:
655 void _initialize_s390_nat (void);
658 _initialize_s390_nat (void)
660 struct target_ops
*t
;
662 /* Fill in the generic GNU/Linux methods. */
665 /* Add our register access methods. */
666 t
->to_fetch_registers
= s390_linux_fetch_inferior_registers
;
667 t
->to_store_registers
= s390_linux_store_inferior_registers
;
669 /* Add our watchpoint methods. */
670 t
->to_can_use_hw_breakpoint
= s390_can_use_hw_breakpoint
;
671 t
->to_region_ok_for_hw_watchpoint
= s390_region_ok_for_hw_watchpoint
;
672 t
->to_have_continuable_watchpoint
= 1;
673 t
->to_stopped_by_watchpoint
= s390_stopped_by_watchpoint
;
674 t
->to_insert_watchpoint
= s390_insert_watchpoint
;
675 t
->to_remove_watchpoint
= s390_remove_watchpoint
;
677 /* Detect target architecture. */
678 t
->to_read_description
= s390_read_description
;
679 t
->to_auxv_parse
= s390_auxv_parse
;
681 /* Register the target. */
682 linux_nat_add_target (t
);
683 linux_nat_set_new_thread (t
, s390_fix_watch_points
);