[GDBserver] Make Zx/zx packet handling idempotent.
[deliverable/binutils-gdb.git] / gdb / gdbserver / linux-s390-low.c
1 /* GNU/Linux S/390 specific low level interface, for the remote server
2 for GDB.
3 Copyright (C) 2001-2014 Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
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.
11
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.
16
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/>. */
19
20 /* This file is used for both 31-bit and 64-bit S/390 systems. */
21
22 #include "server.h"
23 #include "linux-low.h"
24 #include "elf/common.h"
25
26 #include <asm/ptrace.h>
27 #include <sys/ptrace.h>
28 #include <sys/uio.h>
29 #include <elf.h>
30
31 #ifndef HWCAP_S390_HIGH_GPRS
32 #define HWCAP_S390_HIGH_GPRS 512
33 #endif
34
35 #ifndef HWCAP_S390_TE
36 #define HWCAP_S390_TE 1024
37 #endif
38
39 #ifndef PTRACE_GETREGSET
40 #define PTRACE_GETREGSET 0x4204
41 #endif
42
43 #ifndef PTRACE_SETREGSET
44 #define PTRACE_SETREGSET 0x4205
45 #endif
46
47 /* Defined in auto-generated file s390-linux32.c. */
48 void init_registers_s390_linux32 (void);
49 extern const struct target_desc *tdesc_s390_linux32;
50
51 /* Defined in auto-generated file s390-linux32v1.c. */
52 void init_registers_s390_linux32v1 (void);
53 extern const struct target_desc *tdesc_s390_linux32v1;
54
55 /* Defined in auto-generated file s390-linux32v2.c. */
56 void init_registers_s390_linux32v2 (void);
57 extern const struct target_desc *tdesc_s390_linux32v2;
58
59 /* Defined in auto-generated file s390-linux64.c. */
60 void init_registers_s390_linux64 (void);
61 extern const struct target_desc *tdesc_s390_linux64;
62
63 /* Defined in auto-generated file s390-linux64v1.c. */
64 void init_registers_s390_linux64v1 (void);
65 extern const struct target_desc *tdesc_s390_linux64v1;
66
67 /* Defined in auto-generated file s390-linux64v2.c. */
68 void init_registers_s390_linux64v2 (void);
69 extern const struct target_desc *tdesc_s390_linux64v2;
70
71 /* Defined in auto-generated file s390-te-linux64.c. */
72 void init_registers_s390_te_linux64 (void);
73 extern const struct target_desc *tdesc_s390_te_linux64;
74
75 /* Defined in auto-generated file s390x-linux64.c. */
76 void init_registers_s390x_linux64 (void);
77 extern const struct target_desc *tdesc_s390x_linux64;
78
79 /* Defined in auto-generated file s390x-linux64v1.c. */
80 void init_registers_s390x_linux64v1 (void);
81 extern const struct target_desc *tdesc_s390x_linux64v1;
82
83 /* Defined in auto-generated file s390x-linux64v2.c. */
84 void init_registers_s390x_linux64v2 (void);
85 extern const struct target_desc *tdesc_s390x_linux64v2;
86
87 /* Defined in auto-generated file s390x-te-linux64.c. */
88 void init_registers_s390x_te_linux64 (void);
89 extern const struct target_desc *tdesc_s390x_te_linux64;
90
91 #define s390_num_regs 52
92
93 static int s390_regmap[] = {
94 PT_PSWMASK, PT_PSWADDR,
95
96 PT_GPR0, PT_GPR1, PT_GPR2, PT_GPR3,
97 PT_GPR4, PT_GPR5, PT_GPR6, PT_GPR7,
98 PT_GPR8, PT_GPR9, PT_GPR10, PT_GPR11,
99 PT_GPR12, PT_GPR13, PT_GPR14, PT_GPR15,
100
101 PT_ACR0, PT_ACR1, PT_ACR2, PT_ACR3,
102 PT_ACR4, PT_ACR5, PT_ACR6, PT_ACR7,
103 PT_ACR8, PT_ACR9, PT_ACR10, PT_ACR11,
104 PT_ACR12, PT_ACR13, PT_ACR14, PT_ACR15,
105
106 PT_FPC,
107
108 #ifndef __s390x__
109 PT_FPR0_HI, PT_FPR1_HI, PT_FPR2_HI, PT_FPR3_HI,
110 PT_FPR4_HI, PT_FPR5_HI, PT_FPR6_HI, PT_FPR7_HI,
111 PT_FPR8_HI, PT_FPR9_HI, PT_FPR10_HI, PT_FPR11_HI,
112 PT_FPR12_HI, PT_FPR13_HI, PT_FPR14_HI, PT_FPR15_HI,
113 #else
114 PT_FPR0, PT_FPR1, PT_FPR2, PT_FPR3,
115 PT_FPR4, PT_FPR5, PT_FPR6, PT_FPR7,
116 PT_FPR8, PT_FPR9, PT_FPR10, PT_FPR11,
117 PT_FPR12, PT_FPR13, PT_FPR14, PT_FPR15,
118 #endif
119
120 PT_ORIGGPR2,
121 };
122
123 #ifdef __s390x__
124 #define s390_num_regs_3264 68
125
126 static int s390_regmap_3264[] = {
127 PT_PSWMASK, PT_PSWADDR,
128
129 PT_GPR0, PT_GPR0, PT_GPR1, PT_GPR1,
130 PT_GPR2, PT_GPR2, PT_GPR3, PT_GPR3,
131 PT_GPR4, PT_GPR4, PT_GPR5, PT_GPR5,
132 PT_GPR6, PT_GPR6, PT_GPR7, PT_GPR7,
133 PT_GPR8, PT_GPR8, PT_GPR9, PT_GPR9,
134 PT_GPR10, PT_GPR10, PT_GPR11, PT_GPR11,
135 PT_GPR12, PT_GPR12, PT_GPR13, PT_GPR13,
136 PT_GPR14, PT_GPR14, PT_GPR15, PT_GPR15,
137
138 PT_ACR0, PT_ACR1, PT_ACR2, PT_ACR3,
139 PT_ACR4, PT_ACR5, PT_ACR6, PT_ACR7,
140 PT_ACR8, PT_ACR9, PT_ACR10, PT_ACR11,
141 PT_ACR12, PT_ACR13, PT_ACR14, PT_ACR15,
142
143 PT_FPC,
144
145 PT_FPR0, PT_FPR1, PT_FPR2, PT_FPR3,
146 PT_FPR4, PT_FPR5, PT_FPR6, PT_FPR7,
147 PT_FPR8, PT_FPR9, PT_FPR10, PT_FPR11,
148 PT_FPR12, PT_FPR13, PT_FPR14, PT_FPR15,
149
150 PT_ORIGGPR2,
151 };
152 #endif
153
154
155 static int
156 s390_cannot_fetch_register (int regno)
157 {
158 return 0;
159 }
160
161 static int
162 s390_cannot_store_register (int regno)
163 {
164 return 0;
165 }
166
167 static void
168 s390_collect_ptrace_register (struct regcache *regcache, int regno, char *buf)
169 {
170 int size = register_size (regcache->tdesc, regno);
171 if (size < sizeof (long))
172 {
173 const struct regs_info *regs_info = (*the_low_target.regs_info) ();
174 struct usrregs_info *usr = regs_info->usrregs;
175 int regaddr = usr->regmap[regno];
176
177 memset (buf, 0, sizeof (long));
178
179 if ((regno ^ 1) < usr->num_regs
180 && usr->regmap[regno ^ 1] == regaddr)
181 {
182 collect_register (regcache, regno & ~1, buf);
183 collect_register (regcache, (regno & ~1) + 1,
184 buf + sizeof (long) - size);
185 }
186 else if (regaddr == PT_PSWMASK)
187 {
188 /* Convert 4-byte PSW mask to 8 bytes by clearing bit 12 and copying
189 the basic addressing mode bit from the PSW address. */
190 char *addr = alloca (register_size (regcache->tdesc, regno ^ 1));
191 collect_register (regcache, regno, buf);
192 collect_register (regcache, regno ^ 1, addr);
193 buf[1] &= ~0x8;
194 buf[size] |= (addr[0] & 0x80);
195 }
196 else if (regaddr == PT_PSWADDR)
197 {
198 /* Convert 4-byte PSW address to 8 bytes by clearing the addressing
199 mode bit (which gets copied to the PSW mask instead). */
200 collect_register (regcache, regno, buf + sizeof (long) - size);
201 buf[sizeof (long) - size] &= ~0x80;
202 }
203 else if ((regaddr >= PT_GPR0 && regaddr <= PT_GPR15)
204 || regaddr == PT_ORIGGPR2)
205 collect_register (regcache, regno, buf + sizeof (long) - size);
206 else
207 collect_register (regcache, regno, buf);
208 }
209 else
210 collect_register (regcache, regno, buf);
211 }
212
213 static void
214 s390_supply_ptrace_register (struct regcache *regcache,
215 int regno, const char *buf)
216 {
217 int size = register_size (regcache->tdesc, regno);
218 if (size < sizeof (long))
219 {
220 const struct regs_info *regs_info = (*the_low_target.regs_info) ();
221 struct usrregs_info *usr = regs_info->usrregs;
222 int regaddr = usr->regmap[regno];
223
224 if ((regno ^ 1) < usr->num_regs
225 && usr->regmap[regno ^ 1] == regaddr)
226 {
227 supply_register (regcache, regno & ~1, buf);
228 supply_register (regcache, (regno & ~1) + 1,
229 buf + sizeof (long) - size);
230 }
231 else if (regaddr == PT_PSWMASK)
232 {
233 /* Convert 8-byte PSW mask to 4 bytes by setting bit 12 and copying
234 the basic addressing mode into the PSW address. */
235 char *mask = alloca (size);
236 char *addr = alloca (register_size (regcache->tdesc, regno ^ 1));
237 memcpy (mask, buf, size);
238 mask[1] |= 0x8;
239 supply_register (regcache, regno, mask);
240
241 collect_register (regcache, regno ^ 1, addr);
242 addr[0] &= ~0x80;
243 addr[0] |= (buf[size] & 0x80);
244 supply_register (regcache, regno ^ 1, addr);
245 }
246 else if (regaddr == PT_PSWADDR)
247 {
248 /* Convert 8-byte PSW address to 4 bytes by truncating, but
249 keeping the addressing mode bit (which was set from the mask). */
250 char *addr = alloca (size);
251 char amode;
252 collect_register (regcache, regno, addr);
253 amode = addr[0] & 0x80;
254 memcpy (addr, buf + sizeof (long) - size, size);
255 addr[0] &= ~0x80;
256 addr[0] |= amode;
257 supply_register (regcache, regno, addr);
258 }
259 else if ((regaddr >= PT_GPR0 && regaddr <= PT_GPR15)
260 || regaddr == PT_ORIGGPR2)
261 supply_register (regcache, regno, buf + sizeof (long) - size);
262 else
263 supply_register (regcache, regno, buf);
264 }
265 else
266 supply_register (regcache, regno, buf);
267 }
268
269 /* Provide only a fill function for the general register set. ps_lgetregs
270 will use this for NPTL support. */
271
272 static void
273 s390_fill_gregset (struct regcache *regcache, void *buf)
274 {
275 int i;
276 const struct regs_info *regs_info = (*the_low_target.regs_info) ();
277 struct usrregs_info *usr = regs_info->usrregs;
278
279 for (i = 0; i < usr->num_regs; i++)
280 {
281 if (usr->regmap[i] < PT_PSWMASK
282 || usr->regmap[i] > PT_ACR15)
283 continue;
284
285 s390_collect_ptrace_register (regcache, i,
286 (char *) buf + usr->regmap[i]);
287 }
288 }
289
290 /* Fill and store functions for extended register sets. */
291
292 static void
293 s390_fill_last_break (struct regcache *regcache, void *buf)
294 {
295 /* Last break address is read-only. */
296 }
297
298 static void
299 s390_store_last_break (struct regcache *regcache, const void *buf)
300 {
301 const char *p;
302
303 p = (const char *) buf + 8 - register_size (regcache->tdesc, 0);
304 supply_register_by_name (regcache, "last_break", p);
305 }
306
307 static void
308 s390_fill_system_call (struct regcache *regcache, void *buf)
309 {
310 collect_register_by_name (regcache, "system_call", buf);
311 }
312
313 static void
314 s390_store_system_call (struct regcache *regcache, const void *buf)
315 {
316 supply_register_by_name (regcache, "system_call", buf);
317 }
318
319 static struct regset_info s390_regsets[] = {
320 { 0, 0, 0, 0, GENERAL_REGS, s390_fill_gregset, NULL },
321 /* Last break address is read-only; do not attempt PTRACE_SETREGSET. */
322 { PTRACE_GETREGSET, PTRACE_GETREGSET, NT_S390_LAST_BREAK, 0,
323 EXTENDED_REGS, s390_fill_last_break, s390_store_last_break },
324 { PTRACE_GETREGSET, PTRACE_SETREGSET, NT_S390_SYSTEM_CALL, 0,
325 EXTENDED_REGS, s390_fill_system_call, s390_store_system_call },
326 { 0, 0, 0, -1, -1, NULL, NULL }
327 };
328
329
330 static const unsigned char s390_breakpoint[] = { 0, 1 };
331 #define s390_breakpoint_len 2
332
333 static CORE_ADDR
334 s390_get_pc (struct regcache *regcache)
335 {
336 if (register_size (regcache->tdesc, 0) == 4)
337 {
338 unsigned int pswa;
339 collect_register_by_name (regcache, "pswa", &pswa);
340 return pswa & 0x7fffffff;
341 }
342 else
343 {
344 unsigned long pc;
345 collect_register_by_name (regcache, "pswa", &pc);
346 return pc;
347 }
348 }
349
350 static void
351 s390_set_pc (struct regcache *regcache, CORE_ADDR newpc)
352 {
353 if (register_size (regcache->tdesc, 0) == 4)
354 {
355 unsigned int pswa;
356 collect_register_by_name (regcache, "pswa", &pswa);
357 pswa = (pswa & 0x80000000) | (newpc & 0x7fffffff);
358 supply_register_by_name (regcache, "pswa", &pswa);
359 }
360 else
361 {
362 unsigned long pc = newpc;
363 supply_register_by_name (regcache, "pswa", &pc);
364 }
365 }
366
367 #ifdef __s390x__
368 static unsigned long
369 s390_get_hwcap (const struct target_desc *tdesc)
370 {
371 int wordsize = register_size (tdesc, 0);
372 unsigned char *data = alloca (2 * wordsize);
373 int offset = 0;
374
375 while ((*the_target->read_auxv) (offset, data, 2 * wordsize) == 2 * wordsize)
376 {
377 if (wordsize == 4)
378 {
379 unsigned int *data_p = (unsigned int *)data;
380 if (data_p[0] == AT_HWCAP)
381 return data_p[1];
382 }
383 else
384 {
385 unsigned long *data_p = (unsigned long *)data;
386 if (data_p[0] == AT_HWCAP)
387 return data_p[1];
388 }
389
390 offset += 2 * wordsize;
391 }
392
393 return 0;
394 }
395 #endif
396
397 static int
398 s390_check_regset (int pid, int regset, int regsize)
399 {
400 gdb_byte *buf = alloca (regsize);
401 struct iovec iov;
402
403 iov.iov_base = buf;
404 iov.iov_len = regsize;
405
406 if (ptrace (PTRACE_GETREGSET, pid, (long) regset, (long) &iov) >= 0
407 || errno == ENODATA)
408 return 1;
409 return 0;
410 }
411
412 #ifdef __s390x__
413 /* For a 31-bit inferior, whether the kernel supports using the full
414 64-bit GPRs. */
415 static int have_hwcap_s390_high_gprs = 0;
416 #endif
417
418 static void
419 s390_arch_setup (void)
420 {
421 const struct target_desc *tdesc;
422 struct regset_info *regset;
423
424 /* Check whether the kernel supports extra register sets. */
425 int pid = pid_of (current_inferior);
426 int have_regset_last_break
427 = s390_check_regset (pid, NT_S390_LAST_BREAK, 8);
428 int have_regset_system_call
429 = s390_check_regset (pid, NT_S390_SYSTEM_CALL, 4);
430 int have_regset_tdb = s390_check_regset (pid, NT_S390_TDB, 256);
431
432 /* Assume 31-bit inferior process. */
433 if (have_regset_system_call)
434 tdesc = tdesc_s390_linux32v2;
435 else if (have_regset_last_break)
436 tdesc = tdesc_s390_linux32v1;
437 else
438 tdesc = tdesc_s390_linux32;
439
440 /* On a 64-bit host, check the low bit of the (31-bit) PSWM
441 -- if this is one, we actually have a 64-bit inferior. */
442 #ifdef __s390x__
443 {
444 unsigned int pswm;
445 struct regcache *regcache = new_register_cache (tdesc);
446
447 fetch_inferior_registers (regcache, find_regno (tdesc, "pswm"));
448 collect_register_by_name (regcache, "pswm", &pswm);
449 free_register_cache (regcache);
450
451 if (pswm & 1)
452 {
453 if (have_regset_tdb)
454 have_regset_tdb =
455 (s390_get_hwcap (tdesc_s390x_linux64v2) & HWCAP_S390_TE) != 0;
456
457 if (have_regset_tdb)
458 tdesc = tdesc_s390x_te_linux64;
459 else if (have_regset_system_call)
460 tdesc = tdesc_s390x_linux64v2;
461 else if (have_regset_last_break)
462 tdesc = tdesc_s390x_linux64v1;
463 else
464 tdesc = tdesc_s390x_linux64;
465 }
466
467 /* For a 31-bit inferior, check whether the kernel supports
468 using the full 64-bit GPRs. */
469 else if (s390_get_hwcap (tdesc) & HWCAP_S390_HIGH_GPRS)
470 {
471 have_hwcap_s390_high_gprs = 1;
472 if (have_regset_tdb)
473 have_regset_tdb = (s390_get_hwcap (tdesc) & HWCAP_S390_TE) != 0;
474
475 if (have_regset_tdb)
476 tdesc = tdesc_s390_te_linux64;
477 else if (have_regset_system_call)
478 tdesc = tdesc_s390_linux64v2;
479 else if (have_regset_last_break)
480 tdesc = tdesc_s390_linux64v1;
481 else
482 tdesc = tdesc_s390_linux64;
483 }
484 }
485 #endif
486
487 /* Update target_regsets according to available register sets. */
488 for (regset = s390_regsets; regset->fill_function != NULL; regset++)
489 if (regset->get_request == PTRACE_GETREGSET)
490 switch (regset->nt_type)
491 {
492 case NT_S390_LAST_BREAK:
493 regset->size = have_regset_last_break? 8 : 0;
494 break;
495 case NT_S390_SYSTEM_CALL:
496 regset->size = have_regset_system_call? 4 : 0;
497 break;
498 case NT_S390_TDB:
499 regset->size = have_regset_tdb ? 256 : 0;
500 default:
501 break;
502 }
503
504 current_process ()->tdesc = tdesc;
505 }
506
507
508 static int
509 s390_breakpoint_at (CORE_ADDR pc)
510 {
511 unsigned char c[s390_breakpoint_len];
512 read_inferior_memory (pc, c, s390_breakpoint_len);
513 return memcmp (c, s390_breakpoint, s390_breakpoint_len) == 0;
514 }
515
516 static struct usrregs_info s390_usrregs_info =
517 {
518 s390_num_regs,
519 s390_regmap,
520 };
521
522 static struct regsets_info s390_regsets_info =
523 {
524 s390_regsets, /* regsets */
525 0, /* num_regsets */
526 NULL, /* disabled_regsets */
527 };
528
529 static struct regs_info regs_info =
530 {
531 NULL, /* regset_bitmap */
532 &s390_usrregs_info,
533 &s390_regsets_info
534 };
535
536 #ifdef __s390x__
537 static struct usrregs_info s390_usrregs_info_3264 =
538 {
539 s390_num_regs_3264,
540 s390_regmap_3264
541 };
542
543 static struct regsets_info s390_regsets_info_3264 =
544 {
545 s390_regsets, /* regsets */
546 0, /* num_regsets */
547 NULL, /* disabled_regsets */
548 };
549
550 static struct regs_info regs_info_3264 =
551 {
552 NULL, /* regset_bitmap */
553 &s390_usrregs_info_3264,
554 &s390_regsets_info_3264
555 };
556 #endif
557
558 static const struct regs_info *
559 s390_regs_info (void)
560 {
561 #ifdef __s390x__
562 if (have_hwcap_s390_high_gprs)
563 {
564 const struct target_desc *tdesc = current_process ()->tdesc;
565
566 if (register_size (tdesc, 0) == 4)
567 return &regs_info_3264;
568 }
569 #endif
570 return &regs_info;
571 }
572
573 struct linux_target_ops the_low_target = {
574 s390_arch_setup,
575 s390_regs_info,
576 s390_cannot_fetch_register,
577 s390_cannot_store_register,
578 NULL, /* fetch_register */
579 s390_get_pc,
580 s390_set_pc,
581 s390_breakpoint,
582 s390_breakpoint_len,
583 NULL,
584 s390_breakpoint_len,
585 s390_breakpoint_at,
586 NULL, /* supports_z_point_type */
587 NULL,
588 NULL,
589 NULL,
590 NULL,
591 s390_collect_ptrace_register,
592 s390_supply_ptrace_register,
593 };
594
595 void
596 initialize_low_arch (void)
597 {
598 /* Initialize the Linux target descriptions. */
599
600 init_registers_s390_linux32 ();
601 init_registers_s390_linux32v1 ();
602 init_registers_s390_linux32v2 ();
603 init_registers_s390_linux64 ();
604 init_registers_s390_linux64v1 ();
605 init_registers_s390_linux64v2 ();
606 init_registers_s390_te_linux64 ();
607 init_registers_s390x_linux64 ();
608 init_registers_s390x_linux64v1 ();
609 init_registers_s390x_linux64v2 ();
610 init_registers_s390x_te_linux64 ();
611
612 initialize_regsets_info (&s390_regsets_info);
613 #ifdef __s390x__
614 initialize_regsets_info (&s390_regsets_info_3264);
615 #endif
616 }
This page took 0.044688 seconds and 5 git commands to generate.