1 /* GNU/Linux S/390 specific low level interface, for the remote server
3 Copyright (C) 2001-2002, 2005-2012 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20 /* This file is used for both 31-bit and 64-bit S/390 systems. */
23 #include "linux-low.h"
24 #include "elf/common.h"
26 #include <asm/ptrace.h>
27 #include <sys/ptrace.h>
31 #ifndef HWCAP_S390_HIGH_GPRS
32 #define HWCAP_S390_HIGH_GPRS 512
35 #ifndef PTRACE_GETREGSET
36 #define PTRACE_GETREGSET 0x4204
39 #ifndef PTRACE_SETREGSET
40 #define PTRACE_SETREGSET 0x4205
43 /* Defined in auto-generated file s390-linux32.c. */
44 void init_registers_s390_linux32 (void);
45 /* Defined in auto-generated file s390-linux32v1.c. */
46 void init_registers_s390_linux32v1 (void);
47 /* Defined in auto-generated file s390-linux32v2.c. */
48 void init_registers_s390_linux32v2 (void);
49 /* Defined in auto-generated file s390-linux64.c. */
50 void init_registers_s390_linux64 (void);
51 /* Defined in auto-generated file s390-linux64v1.c. */
52 void init_registers_s390_linux64v1 (void);
53 /* Defined in auto-generated file s390-linux64v2.c. */
54 void init_registers_s390_linux64v2 (void);
55 /* Defined in auto-generated file s390x-linux64.c. */
56 void init_registers_s390x_linux64 (void);
57 /* Defined in auto-generated file s390x-linux64v1.c. */
58 void init_registers_s390x_linux64v1 (void);
59 /* Defined in auto-generated file s390x-linux64v2.c. */
60 void init_registers_s390x_linux64v2 (void);
62 #define s390_num_regs 52
64 static int s390_regmap
[] = {
65 PT_PSWMASK
, PT_PSWADDR
,
67 PT_GPR0
, PT_GPR1
, PT_GPR2
, PT_GPR3
,
68 PT_GPR4
, PT_GPR5
, PT_GPR6
, PT_GPR7
,
69 PT_GPR8
, PT_GPR9
, PT_GPR10
, PT_GPR11
,
70 PT_GPR12
, PT_GPR13
, PT_GPR14
, PT_GPR15
,
72 PT_ACR0
, PT_ACR1
, PT_ACR2
, PT_ACR3
,
73 PT_ACR4
, PT_ACR5
, PT_ACR6
, PT_ACR7
,
74 PT_ACR8
, PT_ACR9
, PT_ACR10
, PT_ACR11
,
75 PT_ACR12
, PT_ACR13
, PT_ACR14
, PT_ACR15
,
80 PT_FPR0_HI
, PT_FPR1_HI
, PT_FPR2_HI
, PT_FPR3_HI
,
81 PT_FPR4_HI
, PT_FPR5_HI
, PT_FPR6_HI
, PT_FPR7_HI
,
82 PT_FPR8_HI
, PT_FPR9_HI
, PT_FPR10_HI
, PT_FPR11_HI
,
83 PT_FPR12_HI
, PT_FPR13_HI
, PT_FPR14_HI
, PT_FPR15_HI
,
85 PT_FPR0
, PT_FPR1
, PT_FPR2
, PT_FPR3
,
86 PT_FPR4
, PT_FPR5
, PT_FPR6
, PT_FPR7
,
87 PT_FPR8
, PT_FPR9
, PT_FPR10
, PT_FPR11
,
88 PT_FPR12
, PT_FPR13
, PT_FPR14
, PT_FPR15
,
95 #define s390_num_regs_3264 68
97 static int s390_regmap_3264
[] = {
98 PT_PSWMASK
, PT_PSWADDR
,
100 PT_GPR0
, PT_GPR0
, PT_GPR1
, PT_GPR1
,
101 PT_GPR2
, PT_GPR2
, PT_GPR3
, PT_GPR3
,
102 PT_GPR4
, PT_GPR4
, PT_GPR5
, PT_GPR5
,
103 PT_GPR6
, PT_GPR6
, PT_GPR7
, PT_GPR7
,
104 PT_GPR8
, PT_GPR8
, PT_GPR9
, PT_GPR9
,
105 PT_GPR10
, PT_GPR10
, PT_GPR11
, PT_GPR11
,
106 PT_GPR12
, PT_GPR12
, PT_GPR13
, PT_GPR13
,
107 PT_GPR14
, PT_GPR14
, PT_GPR15
, PT_GPR15
,
109 PT_ACR0
, PT_ACR1
, PT_ACR2
, PT_ACR3
,
110 PT_ACR4
, PT_ACR5
, PT_ACR6
, PT_ACR7
,
111 PT_ACR8
, PT_ACR9
, PT_ACR10
, PT_ACR11
,
112 PT_ACR12
, PT_ACR13
, PT_ACR14
, PT_ACR15
,
116 PT_FPR0
, PT_FPR1
, PT_FPR2
, PT_FPR3
,
117 PT_FPR4
, PT_FPR5
, PT_FPR6
, PT_FPR7
,
118 PT_FPR8
, PT_FPR9
, PT_FPR10
, PT_FPR11
,
119 PT_FPR12
, PT_FPR13
, PT_FPR14
, PT_FPR15
,
127 s390_cannot_fetch_register (int regno
)
133 s390_cannot_store_register (int regno
)
139 s390_collect_ptrace_register (struct regcache
*regcache
, int regno
, char *buf
)
141 int size
= register_size (regno
);
142 if (size
< sizeof (long))
144 int regaddr
= the_low_target
.regmap
[regno
];
146 memset (buf
, 0, sizeof (long));
148 if ((regno
^ 1) < the_low_target
.num_regs
149 && the_low_target
.regmap
[regno
^ 1] == regaddr
)
151 collect_register (regcache
, regno
& ~1, buf
);
152 collect_register (regcache
, (regno
& ~1) + 1,
153 buf
+ sizeof (long) - size
);
155 else if (regaddr
== PT_PSWMASK
)
157 /* Convert 4-byte PSW mask to 8 bytes by clearing bit 12 and copying
158 the basic addressing mode bit from the PSW address. */
159 char *addr
= alloca (register_size (regno
^ 1));
160 collect_register (regcache
, regno
, buf
);
161 collect_register (regcache
, regno
^ 1, addr
);
163 buf
[size
] |= (addr
[0] & 0x80);
165 else if (regaddr
== PT_PSWADDR
)
167 /* Convert 4-byte PSW address to 8 bytes by clearing the addressing
168 mode bit (which gets copied to the PSW mask instead). */
169 collect_register (regcache
, regno
, buf
+ sizeof (long) - size
);
170 buf
[sizeof (long) - size
] &= ~0x80;
172 else if ((regaddr
>= PT_GPR0
&& regaddr
<= PT_GPR15
)
173 || regaddr
== PT_ORIGGPR2
)
174 collect_register (regcache
, regno
, buf
+ sizeof (long) - size
);
176 collect_register (regcache
, regno
, buf
);
179 collect_register (regcache
, regno
, buf
);
183 s390_supply_ptrace_register (struct regcache
*regcache
,
184 int regno
, const char *buf
)
186 int size
= register_size (regno
);
187 if (size
< sizeof (long))
189 int regaddr
= the_low_target
.regmap
[regno
];
191 if ((regno
^ 1) < the_low_target
.num_regs
192 && the_low_target
.regmap
[regno
^ 1] == regaddr
)
194 supply_register (regcache
, regno
& ~1, buf
);
195 supply_register (regcache
, (regno
& ~1) + 1,
196 buf
+ sizeof (long) - size
);
198 else if (regaddr
== PT_PSWMASK
)
200 /* Convert 8-byte PSW mask to 4 bytes by setting bit 12 and copying
201 the basic addressing mode into the PSW address. */
202 char *mask
= alloca (size
);
203 char *addr
= alloca (register_size (regno
^ 1));
204 memcpy (mask
, buf
, size
);
206 supply_register (regcache
, regno
, mask
);
208 collect_register (regcache
, regno
^ 1, addr
);
210 addr
[0] |= (buf
[size
] & 0x80);
211 supply_register (regcache
, regno
^ 1, addr
);
213 else if (regaddr
== PT_PSWADDR
)
215 /* Convert 8-byte PSW address to 4 bytes by truncating, but
216 keeping the addressing mode bit (which was set from the mask). */
217 char *addr
= alloca (size
);
219 collect_register (regcache
, regno
, addr
);
220 amode
= addr
[0] & 0x80;
221 memcpy (addr
, buf
+ sizeof (long) - size
, size
);
224 supply_register (regcache
, regno
, addr
);
226 else if ((regaddr
>= PT_GPR0
&& regaddr
<= PT_GPR15
)
227 || regaddr
== PT_ORIGGPR2
)
228 supply_register (regcache
, regno
, buf
+ sizeof (long) - size
);
230 supply_register (regcache
, regno
, buf
);
233 supply_register (regcache
, regno
, buf
);
236 /* Provide only a fill function for the general register set. ps_lgetregs
237 will use this for NPTL support. */
239 static void s390_fill_gregset (struct regcache
*regcache
, void *buf
)
243 for (i
= 0; i
< the_low_target
.num_regs
; i
++)
245 if (the_low_target
.regmap
[i
] < PT_PSWMASK
246 || the_low_target
.regmap
[i
] > PT_ACR15
)
249 s390_collect_ptrace_register (regcache
, i
, (char *) buf
250 + the_low_target
.regmap
[i
]);
254 /* Fill and store functions for extended register sets. */
257 s390_fill_last_break (struct regcache
*regcache
, void *buf
)
259 /* Last break address is read-only. */
263 s390_store_last_break (struct regcache
*regcache
, const void *buf
)
265 supply_register_by_name (regcache
, "last_break",
266 (const char *)buf
+ 8 - register_size (0));
270 s390_fill_system_call (struct regcache
*regcache
, void *buf
)
272 collect_register_by_name (regcache
, "system_call", buf
);
276 s390_store_system_call (struct regcache
*regcache
, const void *buf
)
278 supply_register_by_name (regcache
, "system_call", buf
);
281 struct regset_info target_regsets
[] = {
282 { 0, 0, 0, 0, GENERAL_REGS
, s390_fill_gregset
, NULL
},
283 /* Last break address is read-only; do not attempt PTRACE_SETREGSET. */
284 { PTRACE_GETREGSET
, PTRACE_GETREGSET
, NT_S390_LAST_BREAK
, 0,
285 EXTENDED_REGS
, s390_fill_last_break
, s390_store_last_break
},
286 { PTRACE_GETREGSET
, PTRACE_SETREGSET
, NT_S390_SYSTEM_CALL
, 0,
287 EXTENDED_REGS
, s390_fill_system_call
, s390_store_system_call
},
288 { 0, 0, 0, -1, -1, NULL
, NULL
}
292 static const unsigned char s390_breakpoint
[] = { 0, 1 };
293 #define s390_breakpoint_len 2
296 s390_get_pc (struct regcache
*regcache
)
298 if (register_size (0) == 4)
301 collect_register_by_name (regcache
, "pswa", &pswa
);
302 return pswa
& 0x7fffffff;
307 collect_register_by_name (regcache
, "pswa", &pc
);
313 s390_set_pc (struct regcache
*regcache
, CORE_ADDR newpc
)
315 if (register_size (0) == 4)
318 collect_register_by_name (regcache
, "pswa", &pswa
);
319 pswa
= (pswa
& 0x80000000) | (newpc
& 0x7fffffff);
320 supply_register_by_name (regcache
, "pswa", &pswa
);
324 unsigned long pc
= newpc
;
325 supply_register_by_name (regcache
, "pswa", &pc
);
331 s390_get_hwcap (void)
333 int wordsize
= register_size (0);
334 unsigned char *data
= alloca (2 * wordsize
);
337 while ((*the_target
->read_auxv
) (offset
, data
, 2 * wordsize
) == 2 * wordsize
)
341 unsigned int *data_p
= (unsigned int *)data
;
342 if (data_p
[0] == AT_HWCAP
)
347 unsigned long *data_p
= (unsigned long *)data
;
348 if (data_p
[0] == AT_HWCAP
)
352 offset
+= 2 * wordsize
;
360 s390_check_regset (int pid
, int regset
, int regsize
)
362 gdb_byte
*buf
= alloca (regsize
);
366 iov
.iov_len
= regsize
;
368 if (ptrace (PTRACE_GETREGSET
, pid
, (long) regset
, (long) &iov
) < 0)
375 s390_arch_setup (void)
377 struct regset_info
*regset
;
379 /* Check whether the kernel supports extra register sets. */
380 int pid
= pid_of (get_thread_lwp (current_inferior
));
381 int have_regset_last_break
382 = s390_check_regset (pid
, NT_S390_LAST_BREAK
, 8);
383 int have_regset_system_call
384 = s390_check_regset (pid
, NT_S390_SYSTEM_CALL
, 4);
386 /* Update target_regsets according to available register sets. */
387 for (regset
= target_regsets
; regset
->fill_function
!= NULL
; regset
++)
388 if (regset
->get_request
== PTRACE_GETREGSET
)
389 switch (regset
->nt_type
)
391 case NT_S390_LAST_BREAK
:
392 regset
->size
= have_regset_last_break
? 8 : 0;
394 case NT_S390_SYSTEM_CALL
:
395 regset
->size
= have_regset_system_call
? 4 : 0;
401 /* Assume 31-bit inferior process. */
402 if (have_regset_system_call
)
403 init_registers_s390_linux32v2 ();
404 else if (have_regset_last_break
)
405 init_registers_s390_linux32v1 ();
407 init_registers_s390_linux32 ();
409 the_low_target
.num_regs
= s390_num_regs
;
410 the_low_target
.regmap
= s390_regmap
;
412 /* On a 64-bit host, check the low bit of the (31-bit) PSWM
413 -- if this is one, we actually have a 64-bit inferior. */
417 struct regcache
*regcache
= new_register_cache ();
418 fetch_inferior_registers (regcache
, find_regno ("pswm"));
419 collect_register_by_name (regcache
, "pswm", &pswm
);
420 free_register_cache (regcache
);
424 if (have_regset_system_call
)
425 init_registers_s390x_linux64v2 ();
426 else if (have_regset_last_break
)
427 init_registers_s390x_linux64v1 ();
429 init_registers_s390x_linux64 ();
432 /* For a 31-bit inferior, check whether the kernel supports
433 using the full 64-bit GPRs. */
434 else if (s390_get_hwcap () & HWCAP_S390_HIGH_GPRS
)
436 if (have_regset_system_call
)
437 init_registers_s390_linux64v2 ();
438 else if (have_regset_last_break
)
439 init_registers_s390_linux64v1 ();
441 init_registers_s390_linux64 ();
443 the_low_target
.num_regs
= s390_num_regs_3264
;
444 the_low_target
.regmap
= s390_regmap_3264
;
452 s390_breakpoint_at (CORE_ADDR pc
)
454 unsigned char c
[s390_breakpoint_len
];
455 read_inferior_memory (pc
, c
, s390_breakpoint_len
);
456 return memcmp (c
, s390_breakpoint
, s390_breakpoint_len
) == 0;
460 struct linux_target_ops the_low_target
= {
465 s390_cannot_fetch_register
,
466 s390_cannot_store_register
,
467 NULL
, /* fetch_register */
479 s390_collect_ptrace_register
,
480 s390_supply_ptrace_register
,