Replace 'core_regset_sections' by iterator method
[external/binutils.git] / gdb / s390-linux-tdep.c
1 /* Target-dependent code for GDB, the GNU debugger.
2
3    Copyright (C) 2001-2014 Free Software Foundation, Inc.
4
5    Contributed by D.J. Barrow (djbarrow@de.ibm.com,barrow_dj@yahoo.com)
6    for IBM Deutschland Entwicklung GmbH, IBM Corporation.
7
8    This file is part of GDB.
9
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.
14
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.
19
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/>.  */
22
23 #include "defs.h"
24 #include "arch-utils.h"
25 #include "frame.h"
26 #include "inferior.h"
27 #include "infrun.h"
28 #include "symtab.h"
29 #include "target.h"
30 #include "gdbcore.h"
31 #include "gdbcmd.h"
32 #include "objfiles.h"
33 #include "floatformat.h"
34 #include "regcache.h"
35 #include "trad-frame.h"
36 #include "frame-base.h"
37 #include "frame-unwind.h"
38 #include "dwarf2-frame.h"
39 #include "reggroups.h"
40 #include "regset.h"
41 #include "value.h"
42 #include "dis-asm.h"
43 #include "solib-svr4.h"
44 #include "prologue-value.h"
45 #include "linux-tdep.h"
46 #include "s390-linux-tdep.h"
47 #include "auxv.h"
48 #include "xml-syscall.h"
49
50 #include "stap-probe.h"
51 #include "ax.h"
52 #include "ax-gdb.h"
53 #include "user-regs.h"
54 #include "cli/cli-utils.h"
55 #include <ctype.h>
56 #include "elf/common.h"
57
58 #include "features/s390-linux32.c"
59 #include "features/s390-linux32v1.c"
60 #include "features/s390-linux32v2.c"
61 #include "features/s390-linux64.c"
62 #include "features/s390-linux64v1.c"
63 #include "features/s390-linux64v2.c"
64 #include "features/s390-te-linux64.c"
65 #include "features/s390x-linux64.c"
66 #include "features/s390x-linux64v1.c"
67 #include "features/s390x-linux64v2.c"
68 #include "features/s390x-te-linux64.c"
69
70 #define XML_SYSCALL_FILENAME_S390 "syscalls/s390-linux.xml"
71 #define XML_SYSCALL_FILENAME_S390X "syscalls/s390x-linux.xml"
72
73 /* The tdep structure.  */
74
75 struct gdbarch_tdep
76 {
77   /* ABI version.  */
78   enum { ABI_LINUX_S390, ABI_LINUX_ZSERIES } abi;
79
80   /* Pseudo register numbers.  */
81   int gpr_full_regnum;
82   int pc_regnum;
83   int cc_regnum;
84
85   /* Core file register sets.  */
86   const struct regset *gregset;
87   int sizeof_gregset;
88
89   const struct regset *fpregset;
90   int sizeof_fpregset;
91
92   int have_linux_v1;
93   int have_linux_v2;
94   int have_tdb;
95 };
96
97
98 /* ABI call-saved register information.  */
99
100 static int
101 s390_register_call_saved (struct gdbarch *gdbarch, int regnum)
102 {
103   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
104
105   switch (tdep->abi)
106     {
107     case ABI_LINUX_S390:
108       if ((regnum >= S390_R6_REGNUM && regnum <= S390_R15_REGNUM)
109           || regnum == S390_F4_REGNUM || regnum == S390_F6_REGNUM
110           || regnum == S390_A0_REGNUM)
111         return 1;
112
113       break;
114
115     case ABI_LINUX_ZSERIES:
116       if ((regnum >= S390_R6_REGNUM && regnum <= S390_R15_REGNUM)
117           || (regnum >= S390_F8_REGNUM && regnum <= S390_F15_REGNUM)
118           || (regnum >= S390_A0_REGNUM && regnum <= S390_A1_REGNUM))
119         return 1;
120
121       break;
122     }
123
124   return 0;
125 }
126
127 static int
128 s390_cannot_store_register (struct gdbarch *gdbarch, int regnum)
129 {
130   /* The last-break address is read-only.  */
131   return regnum == S390_LAST_BREAK_REGNUM;
132 }
133
134 static void
135 s390_write_pc (struct regcache *regcache, CORE_ADDR pc)
136 {
137   struct gdbarch *gdbarch = get_regcache_arch (regcache);
138   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
139
140   regcache_cooked_write_unsigned (regcache, tdep->pc_regnum, pc);
141
142   /* Set special SYSTEM_CALL register to 0 to prevent the kernel from
143      messing with the PC we just installed, if we happen to be within
144      an interrupted system call that the kernel wants to restart.
145
146      Note that after we return from the dummy call, the SYSTEM_CALL and
147      ORIG_R2 registers will be automatically restored, and the kernel
148      continues to restart the system call at this point.  */
149   if (register_size (gdbarch, S390_SYSTEM_CALL_REGNUM) > 0)
150     regcache_cooked_write_unsigned (regcache, S390_SYSTEM_CALL_REGNUM, 0);
151 }
152
153
154 /* DWARF Register Mapping.  */
155
156 static const short s390_dwarf_regmap[] =
157 {
158   /* General Purpose Registers.  */
159   S390_R0_REGNUM, S390_R1_REGNUM, S390_R2_REGNUM, S390_R3_REGNUM,
160   S390_R4_REGNUM, S390_R5_REGNUM, S390_R6_REGNUM, S390_R7_REGNUM,
161   S390_R8_REGNUM, S390_R9_REGNUM, S390_R10_REGNUM, S390_R11_REGNUM,
162   S390_R12_REGNUM, S390_R13_REGNUM, S390_R14_REGNUM, S390_R15_REGNUM,
163
164   /* Floating Point Registers.  */
165   S390_F0_REGNUM, S390_F2_REGNUM, S390_F4_REGNUM, S390_F6_REGNUM,
166   S390_F1_REGNUM, S390_F3_REGNUM, S390_F5_REGNUM, S390_F7_REGNUM,
167   S390_F8_REGNUM, S390_F10_REGNUM, S390_F12_REGNUM, S390_F14_REGNUM,
168   S390_F9_REGNUM, S390_F11_REGNUM, S390_F13_REGNUM, S390_F15_REGNUM,
169
170   /* Control Registers (not mapped).  */
171   -1, -1, -1, -1, -1, -1, -1, -1,
172   -1, -1, -1, -1, -1, -1, -1, -1,
173
174   /* Access Registers.  */
175   S390_A0_REGNUM, S390_A1_REGNUM, S390_A2_REGNUM, S390_A3_REGNUM,
176   S390_A4_REGNUM, S390_A5_REGNUM, S390_A6_REGNUM, S390_A7_REGNUM,
177   S390_A8_REGNUM, S390_A9_REGNUM, S390_A10_REGNUM, S390_A11_REGNUM,
178   S390_A12_REGNUM, S390_A13_REGNUM, S390_A14_REGNUM, S390_A15_REGNUM,
179
180   /* Program Status Word.  */
181   S390_PSWM_REGNUM,
182   S390_PSWA_REGNUM,
183
184   /* GPR Lower Half Access.  */
185   S390_R0_REGNUM, S390_R1_REGNUM, S390_R2_REGNUM, S390_R3_REGNUM,
186   S390_R4_REGNUM, S390_R5_REGNUM, S390_R6_REGNUM, S390_R7_REGNUM,
187   S390_R8_REGNUM, S390_R9_REGNUM, S390_R10_REGNUM, S390_R11_REGNUM,
188   S390_R12_REGNUM, S390_R13_REGNUM, S390_R14_REGNUM, S390_R15_REGNUM,
189
190   /* GNU/Linux-specific registers (not mapped).  */
191   -1, -1, -1,
192 };
193
194 /* Convert DWARF register number REG to the appropriate register
195    number used by GDB.  */
196 static int
197 s390_dwarf_reg_to_regnum (struct gdbarch *gdbarch, int reg)
198 {
199   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
200
201   /* In a 32-on-64 debug scenario, debug info refers to the full 64-bit
202      GPRs.  Note that call frame information still refers to the 32-bit
203      lower halves, because s390_adjust_frame_regnum uses register numbers
204      66 .. 81 to access GPRs.  */
205   if (tdep->gpr_full_regnum != -1 && reg >= 0 && reg < 16)
206     return tdep->gpr_full_regnum + reg;
207
208   if (reg >= 0 && reg < ARRAY_SIZE (s390_dwarf_regmap))
209     return s390_dwarf_regmap[reg];
210
211   warning (_("Unmapped DWARF Register #%d encountered."), reg);
212   return -1;
213 }
214
215 /* Translate a .eh_frame register to DWARF register, or adjust a
216    .debug_frame register.  */
217 static int
218 s390_adjust_frame_regnum (struct gdbarch *gdbarch, int num, int eh_frame_p)
219 {
220   /* See s390_dwarf_reg_to_regnum for comments.  */
221   return (num >= 0 && num < 16)? num + 66 : num;
222 }
223
224
225 /* Pseudo registers.  */
226
227 static int
228 regnum_is_gpr_full (struct gdbarch_tdep *tdep, int regnum)
229 {
230   return (tdep->gpr_full_regnum != -1
231           && regnum >= tdep->gpr_full_regnum
232           && regnum <= tdep->gpr_full_regnum + 15);
233 }
234
235 static const char *
236 s390_pseudo_register_name (struct gdbarch *gdbarch, int regnum)
237 {
238   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
239
240   if (regnum == tdep->pc_regnum)
241     return "pc";
242
243   if (regnum == tdep->cc_regnum)
244     return "cc";
245
246   if (regnum_is_gpr_full (tdep, regnum))
247     {
248       static const char *full_name[] = {
249         "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
250         "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
251       };
252       return full_name[regnum - tdep->gpr_full_regnum];
253     }
254
255   internal_error (__FILE__, __LINE__, _("invalid regnum"));
256 }
257
258 static struct type *
259 s390_pseudo_register_type (struct gdbarch *gdbarch, int regnum)
260 {
261   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
262
263   if (regnum == tdep->pc_regnum)
264     return builtin_type (gdbarch)->builtin_func_ptr;
265
266   if (regnum == tdep->cc_regnum)
267     return builtin_type (gdbarch)->builtin_int;
268
269   if (regnum_is_gpr_full (tdep, regnum))
270     return builtin_type (gdbarch)->builtin_uint64;
271
272   internal_error (__FILE__, __LINE__, _("invalid regnum"));
273 }
274
275 static enum register_status
276 s390_pseudo_register_read (struct gdbarch *gdbarch, struct regcache *regcache,
277                            int regnum, gdb_byte *buf)
278 {
279   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
280   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
281   int regsize = register_size (gdbarch, regnum);
282   ULONGEST val;
283
284   if (regnum == tdep->pc_regnum)
285     {
286       enum register_status status;
287
288       status = regcache_raw_read_unsigned (regcache, S390_PSWA_REGNUM, &val);
289       if (status == REG_VALID)
290         {
291           if (register_size (gdbarch, S390_PSWA_REGNUM) == 4)
292             val &= 0x7fffffff;
293           store_unsigned_integer (buf, regsize, byte_order, val);
294         }
295       return status;
296     }
297
298   if (regnum == tdep->cc_regnum)
299     {
300       enum register_status status;
301
302       status = regcache_raw_read_unsigned (regcache, S390_PSWM_REGNUM, &val);
303       if (status == REG_VALID)
304         {
305           if (register_size (gdbarch, S390_PSWA_REGNUM) == 4)
306             val = (val >> 12) & 3;
307           else
308             val = (val >> 44) & 3;
309           store_unsigned_integer (buf, regsize, byte_order, val);
310         }
311       return status;
312     }
313
314   if (regnum_is_gpr_full (tdep, regnum))
315     {
316       enum register_status status;
317       ULONGEST val_upper;
318
319       regnum -= tdep->gpr_full_regnum;
320
321       status = regcache_raw_read_unsigned (regcache, S390_R0_REGNUM + regnum, &val);
322       if (status == REG_VALID)
323         status = regcache_raw_read_unsigned (regcache, S390_R0_UPPER_REGNUM + regnum,
324                                              &val_upper);
325       if (status == REG_VALID)
326         {
327           val |= val_upper << 32;
328           store_unsigned_integer (buf, regsize, byte_order, val);
329         }
330       return status;
331     }
332
333   internal_error (__FILE__, __LINE__, _("invalid regnum"));
334 }
335
336 static void
337 s390_pseudo_register_write (struct gdbarch *gdbarch, struct regcache *regcache,
338                             int regnum, const gdb_byte *buf)
339 {
340   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
341   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
342   int regsize = register_size (gdbarch, regnum);
343   ULONGEST val, psw;
344
345   if (regnum == tdep->pc_regnum)
346     {
347       val = extract_unsigned_integer (buf, regsize, byte_order);
348       if (register_size (gdbarch, S390_PSWA_REGNUM) == 4)
349         {
350           regcache_raw_read_unsigned (regcache, S390_PSWA_REGNUM, &psw);
351           val = (psw & 0x80000000) | (val & 0x7fffffff);
352         }
353       regcache_raw_write_unsigned (regcache, S390_PSWA_REGNUM, val);
354       return;
355     }
356
357   if (regnum == tdep->cc_regnum)
358     {
359       val = extract_unsigned_integer (buf, regsize, byte_order);
360       regcache_raw_read_unsigned (regcache, S390_PSWM_REGNUM, &psw);
361       if (register_size (gdbarch, S390_PSWA_REGNUM) == 4)
362         val = (psw & ~((ULONGEST)3 << 12)) | ((val & 3) << 12);
363       else
364         val = (psw & ~((ULONGEST)3 << 44)) | ((val & 3) << 44);
365       regcache_raw_write_unsigned (regcache, S390_PSWM_REGNUM, val);
366       return;
367     }
368
369   if (regnum_is_gpr_full (tdep, regnum))
370     {
371       regnum -= tdep->gpr_full_regnum;
372       val = extract_unsigned_integer (buf, regsize, byte_order);
373       regcache_raw_write_unsigned (regcache, S390_R0_REGNUM + regnum,
374                                    val & 0xffffffff);
375       regcache_raw_write_unsigned (regcache, S390_R0_UPPER_REGNUM + regnum,
376                                    val >> 32);
377       return;
378     }
379
380   internal_error (__FILE__, __LINE__, _("invalid regnum"));
381 }
382
383 /* 'float' values are stored in the upper half of floating-point
384    registers, even though we are otherwise a big-endian platform.  */
385
386 static struct value *
387 s390_value_from_register (struct gdbarch *gdbarch, struct type *type,
388                           int regnum, struct frame_id frame_id)
389 {
390   struct value *value = default_value_from_register (gdbarch, type,
391                                                      regnum, frame_id);
392   check_typedef (type);
393
394   if (regnum >= S390_F0_REGNUM && regnum <= S390_F15_REGNUM
395       && TYPE_LENGTH (type) < 8)
396     set_value_offset (value, 0);
397
398   return value;
399 }
400
401 /* Register groups.  */
402
403 static int
404 s390_pseudo_register_reggroup_p (struct gdbarch *gdbarch, int regnum,
405                                  struct reggroup *group)
406 {
407   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
408
409   /* We usually save/restore the whole PSW, which includes PC and CC.
410      However, some older gdbservers may not support saving/restoring
411      the whole PSW yet, and will return an XML register description
412      excluding those from the save/restore register groups.  In those
413      cases, we still need to explicitly save/restore PC and CC in order
414      to push or pop frames.  Since this doesn't hurt anything if we
415      already save/restore the whole PSW (it's just redundant), we add
416      PC and CC at this point unconditionally.  */
417   if (group == save_reggroup || group == restore_reggroup)
418     return regnum == tdep->pc_regnum || regnum == tdep->cc_regnum;
419
420   return default_register_reggroup_p (gdbarch, regnum, group);
421 }
422
423
424 /* Maps for register sets.  */
425
426 static const struct regcache_map_entry s390_gregmap[] =
427   {
428     { 1, S390_PSWM_REGNUM },
429     { 1, S390_PSWA_REGNUM },
430     { 16, S390_R0_REGNUM },
431     { 16, S390_A0_REGNUM },
432     { 1, S390_ORIG_R2_REGNUM },
433     { 0 }
434   };
435
436 static const struct regcache_map_entry s390_fpregmap[] =
437   {
438     { 1, S390_FPC_REGNUM, 8 },
439     { 16, S390_F0_REGNUM, 8 },
440     { 0 }
441   };
442
443 static const struct regcache_map_entry s390_regmap_upper[] =
444   {
445     { 16, S390_R0_UPPER_REGNUM, 4 },
446     { 0 }
447   };
448
449 static const struct regcache_map_entry s390_regmap_last_break[] =
450   {
451     { 1, REGCACHE_MAP_SKIP, 4 },
452     { 1, S390_LAST_BREAK_REGNUM, 4 },
453     { 0 }
454   };
455
456 static const struct regcache_map_entry s390x_regmap_last_break[] =
457   {
458     { 1, S390_LAST_BREAK_REGNUM, 8 },
459     { 0 }
460   };
461
462 static const struct regcache_map_entry s390_regmap_system_call[] =
463   {
464     { 1, S390_SYSTEM_CALL_REGNUM, 4 },
465     { 0 }
466   };
467
468 static const struct regcache_map_entry s390_regmap_tdb[] =
469   {
470     { 1, S390_TDB_DWORD0_REGNUM, 8 },
471     { 1, S390_TDB_ABORT_CODE_REGNUM, 8 },
472     { 1, S390_TDB_CONFLICT_TOKEN_REGNUM, 8 },
473     { 1, S390_TDB_ATIA_REGNUM, 8 },
474     { 12, REGCACHE_MAP_SKIP, 8 },
475     { 16, S390_TDB_R0_REGNUM, 8 },
476     { 0 }
477   };
478
479
480 /* Supply the TDB regset.  Like regcache_supply_regset, but invalidate
481    the TDB registers unless the TDB format field is valid.  */
482
483 static void
484 s390_supply_tdb_regset (const struct regset *regset, struct regcache *regcache,
485                     int regnum, const void *regs, size_t len)
486 {
487   ULONGEST tdw;
488   enum register_status ret;
489   int i;
490
491   regcache_supply_regset (regset, regcache, regnum, regs, len);
492   ret = regcache_cooked_read_unsigned (regcache, S390_TDB_DWORD0_REGNUM, &tdw);
493   if (ret != REG_VALID || (tdw >> 56) != 1)
494     regcache_supply_regset (regset, regcache, regnum, NULL, len);
495 }
496
497 const struct regset s390_gregset = {
498   s390_gregmap,
499   regcache_supply_regset,
500   regcache_collect_regset
501 };
502
503 const struct regset s390_fpregset = {
504   s390_fpregmap,
505   regcache_supply_regset,
506   regcache_collect_regset
507 };
508
509 static const struct regset s390_upper_regset = {
510   s390_regmap_upper,
511   regcache_supply_regset,
512   regcache_collect_regset
513 };
514
515 const struct regset s390_last_break_regset = {
516   s390_regmap_last_break,
517   regcache_supply_regset,
518   regcache_collect_regset
519 };
520
521 const struct regset s390x_last_break_regset = {
522   s390x_regmap_last_break,
523   regcache_supply_regset,
524   regcache_collect_regset
525 };
526
527 const struct regset s390_system_call_regset = {
528   s390_regmap_system_call,
529   regcache_supply_regset,
530   regcache_collect_regset
531 };
532
533 const struct regset s390_tdb_regset = {
534   s390_regmap_tdb,
535   s390_supply_tdb_regset,
536   regcache_collect_regset
537 };
538
539 /* Return the appropriate register set for the core section identified
540    by SECT_NAME and SECT_SIZE.  */
541 static const struct regset *
542 s390_regset_from_core_section (struct gdbarch *gdbarch,
543                                const char *sect_name, size_t sect_size)
544 {
545   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
546
547   if (strcmp (sect_name, ".reg") == 0 && sect_size >= tdep->sizeof_gregset)
548     return tdep->gregset;
549
550   if (strcmp (sect_name, ".reg2") == 0 && sect_size >= tdep->sizeof_fpregset)
551     return tdep->fpregset;
552
553   if (strcmp (sect_name, ".reg-s390-high-gprs") == 0 && sect_size >= 16*4)
554     return &s390_upper_regset;
555
556   if (strcmp (sect_name, ".reg-s390-last-break") == 0 && sect_size >= 8)
557     return (gdbarch_ptr_bit (gdbarch) == 32
558             ?  &s390_last_break_regset : &s390x_last_break_regset);
559
560   if (strcmp (sect_name, ".reg-s390-system-call") == 0 && sect_size >= 4)
561     return &s390_system_call_regset;
562
563   if (strcmp (sect_name, ".reg-s390-tdb") == 0 && sect_size >= 256)
564     return &s390_tdb_regset;
565
566   return NULL;
567 }
568
569 /* Iterate over supported core file register note sections. */
570
571 static void
572 s390_iterate_over_regset_sections (struct gdbarch *gdbarch,
573                                    iterate_over_regset_sections_cb *cb,
574                                    void *cb_data,
575                                    const struct regcache *regcache)
576 {
577   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
578
579   cb (".reg", tdep->sizeof_gregset, "general-purpose", cb_data);
580   cb (".reg2", s390_sizeof_fpregset, "floating-point", cb_data);
581
582   if (tdep->abi == ABI_LINUX_S390 && tdep->gpr_full_regnum != -1)
583     cb (".reg-s390-high-gprs", 16 * 4, "s390 GPR upper halves", cb_data);
584
585   if (tdep->have_linux_v1)
586     cb (".reg-s390-last-break", 8, "s930 last-break address", cb_data);
587
588   if (tdep->have_linux_v2)
589     cb (".reg-s390-system-call", 4, "s390 system-call", cb_data);
590
591   /* If regcache is set, we are in "write" (gcore) mode.  In this
592      case, don't iterate over the TDB unless its registers are
593      available.  */
594   if (tdep->have_tdb
595       && (regcache == NULL
596           || REG_VALID == regcache_register_status (regcache,
597                                                     S390_TDB_DWORD0_REGNUM)))
598     cb (".reg-s390-tdb", s390_sizeof_tdbregset, "s390 TDB", cb_data);
599 }
600
601 static const struct target_desc *
602 s390_core_read_description (struct gdbarch *gdbarch,
603                             struct target_ops *target, bfd *abfd)
604 {
605   asection *high_gprs = bfd_get_section_by_name (abfd, ".reg-s390-high-gprs");
606   asection *v1 = bfd_get_section_by_name (abfd, ".reg-s390-last-break");
607   asection *v2 = bfd_get_section_by_name (abfd, ".reg-s390-system-call");
608   asection *section = bfd_get_section_by_name (abfd, ".reg");
609   CORE_ADDR hwcap = 0;
610
611   target_auxv_search (target, AT_HWCAP, &hwcap);
612   if (!section)
613     return NULL;
614
615   switch (bfd_section_size (abfd, section))
616     {
617     case s390_sizeof_gregset:
618       if (high_gprs)
619         return ((hwcap & HWCAP_S390_TE) ? tdesc_s390_te_linux64 :
620                 v2? tdesc_s390_linux64v2 :
621                 v1? tdesc_s390_linux64v1 : tdesc_s390_linux64);
622       else
623         return (v2? tdesc_s390_linux32v2 :
624                 v1? tdesc_s390_linux32v1 : tdesc_s390_linux32);
625
626     case s390x_sizeof_gregset:
627       return ((hwcap & HWCAP_S390_TE) ? tdesc_s390x_te_linux64 :
628               v2? tdesc_s390x_linux64v2 :
629               v1? tdesc_s390x_linux64v1 : tdesc_s390x_linux64);
630
631     default:
632       return NULL;
633     }
634 }
635
636
637 /* Decoding S/390 instructions.  */
638
639 /* Named opcode values for the S/390 instructions we recognize.  Some
640    instructions have their opcode split across two fields; those are the
641    op1_* and op2_* enums.  */
642 enum
643   {
644     op1_lhi  = 0xa7,   op2_lhi  = 0x08,
645     op1_lghi = 0xa7,   op2_lghi = 0x09,
646     op1_lgfi = 0xc0,   op2_lgfi = 0x01,
647     op_lr    = 0x18,
648     op_lgr   = 0xb904,
649     op_l     = 0x58,
650     op1_ly   = 0xe3,   op2_ly   = 0x58,
651     op1_lg   = 0xe3,   op2_lg   = 0x04,
652     op_lm    = 0x98,
653     op1_lmy  = 0xeb,   op2_lmy  = 0x98,
654     op1_lmg  = 0xeb,   op2_lmg  = 0x04,
655     op_st    = 0x50,
656     op1_sty  = 0xe3,   op2_sty  = 0x50,
657     op1_stg  = 0xe3,   op2_stg  = 0x24,
658     op_std   = 0x60,
659     op_stm   = 0x90,
660     op1_stmy = 0xeb,   op2_stmy = 0x90,
661     op1_stmg = 0xeb,   op2_stmg = 0x24,
662     op1_aghi = 0xa7,   op2_aghi = 0x0b,
663     op1_ahi  = 0xa7,   op2_ahi  = 0x0a,
664     op1_agfi = 0xc2,   op2_agfi = 0x08,
665     op1_afi  = 0xc2,   op2_afi  = 0x09,
666     op1_algfi= 0xc2,   op2_algfi= 0x0a,
667     op1_alfi = 0xc2,   op2_alfi = 0x0b,
668     op_ar    = 0x1a,
669     op_agr   = 0xb908,
670     op_a     = 0x5a,
671     op1_ay   = 0xe3,   op2_ay   = 0x5a,
672     op1_ag   = 0xe3,   op2_ag   = 0x08,
673     op1_slgfi= 0xc2,   op2_slgfi= 0x04,
674     op1_slfi = 0xc2,   op2_slfi = 0x05,
675     op_sr    = 0x1b,
676     op_sgr   = 0xb909,
677     op_s     = 0x5b,
678     op1_sy   = 0xe3,   op2_sy   = 0x5b,
679     op1_sg   = 0xe3,   op2_sg   = 0x09,
680     op_nr    = 0x14,
681     op_ngr   = 0xb980,
682     op_la    = 0x41,
683     op1_lay  = 0xe3,   op2_lay  = 0x71,
684     op1_larl = 0xc0,   op2_larl = 0x00,
685     op_basr  = 0x0d,
686     op_bas   = 0x4d,
687     op_bcr   = 0x07,
688     op_bc    = 0x0d,
689     op_bctr  = 0x06,
690     op_bctgr = 0xb946,
691     op_bct   = 0x46,
692     op1_bctg = 0xe3,   op2_bctg = 0x46,
693     op_bxh   = 0x86,
694     op1_bxhg = 0xeb,   op2_bxhg = 0x44,
695     op_bxle  = 0x87,
696     op1_bxleg= 0xeb,   op2_bxleg= 0x45,
697     op1_bras = 0xa7,   op2_bras = 0x05,
698     op1_brasl= 0xc0,   op2_brasl= 0x05,
699     op1_brc  = 0xa7,   op2_brc  = 0x04,
700     op1_brcl = 0xc0,   op2_brcl = 0x04,
701     op1_brct = 0xa7,   op2_brct = 0x06,
702     op1_brctg= 0xa7,   op2_brctg= 0x07,
703     op_brxh  = 0x84,
704     op1_brxhg= 0xec,   op2_brxhg= 0x44,
705     op_brxle = 0x85,
706     op1_brxlg= 0xec,   op2_brxlg= 0x45,
707     op_svc   = 0x0a,
708   };
709
710
711 /* Read a single instruction from address AT.  */
712
713 #define S390_MAX_INSTR_SIZE 6
714 static int
715 s390_readinstruction (bfd_byte instr[], CORE_ADDR at)
716 {
717   static int s390_instrlen[] = { 2, 4, 4, 6 };
718   int instrlen;
719
720   if (target_read_memory (at, &instr[0], 2))
721     return -1;
722   instrlen = s390_instrlen[instr[0] >> 6];
723   if (instrlen > 2)
724     {
725       if (target_read_memory (at + 2, &instr[2], instrlen - 2))
726         return -1;
727     }
728   return instrlen;
729 }
730
731
732 /* The functions below are for recognizing and decoding S/390
733    instructions of various formats.  Each of them checks whether INSN
734    is an instruction of the given format, with the specified opcodes.
735    If it is, it sets the remaining arguments to the values of the
736    instruction's fields, and returns a non-zero value; otherwise, it
737    returns zero.
738
739    These functions' arguments appear in the order they appear in the
740    instruction, not in the machine-language form.  So, opcodes always
741    come first, even though they're sometimes scattered around the
742    instructions.  And displacements appear before base and extension
743    registers, as they do in the assembly syntax, not at the end, as
744    they do in the machine language.  */
745 static int
746 is_ri (bfd_byte *insn, int op1, int op2, unsigned int *r1, int *i2)
747 {
748   if (insn[0] == op1 && (insn[1] & 0xf) == op2)
749     {
750       *r1 = (insn[1] >> 4) & 0xf;
751       /* i2 is a 16-bit signed quantity.  */
752       *i2 = (((insn[2] << 8) | insn[3]) ^ 0x8000) - 0x8000;
753       return 1;
754     }
755   else
756     return 0;
757 }
758
759
760 static int
761 is_ril (bfd_byte *insn, int op1, int op2,
762         unsigned int *r1, int *i2)
763 {
764   if (insn[0] == op1 && (insn[1] & 0xf) == op2)
765     {
766       *r1 = (insn[1] >> 4) & 0xf;
767       /* i2 is a signed quantity.  If the host 'int' is 32 bits long,
768          no sign extension is necessary, but we don't want to assume
769          that.  */
770       *i2 = (((insn[2] << 24)
771               | (insn[3] << 16)
772               | (insn[4] << 8)
773               | (insn[5])) ^ 0x80000000) - 0x80000000;
774       return 1;
775     }
776   else
777     return 0;
778 }
779
780
781 static int
782 is_rr (bfd_byte *insn, int op, unsigned int *r1, unsigned int *r2)
783 {
784   if (insn[0] == op)
785     {
786       *r1 = (insn[1] >> 4) & 0xf;
787       *r2 = insn[1] & 0xf;
788       return 1;
789     }
790   else
791     return 0;
792 }
793
794
795 static int
796 is_rre (bfd_byte *insn, int op, unsigned int *r1, unsigned int *r2)
797 {
798   if (((insn[0] << 8) | insn[1]) == op)
799     {
800       /* Yes, insn[3].  insn[2] is unused in RRE format.  */
801       *r1 = (insn[3] >> 4) & 0xf;
802       *r2 = insn[3] & 0xf;
803       return 1;
804     }
805   else
806     return 0;
807 }
808
809
810 static int
811 is_rs (bfd_byte *insn, int op,
812        unsigned int *r1, unsigned int *r3, int *d2, unsigned int *b2)
813 {
814   if (insn[0] == op)
815     {
816       *r1 = (insn[1] >> 4) & 0xf;
817       *r3 = insn[1] & 0xf;
818       *b2 = (insn[2] >> 4) & 0xf;
819       *d2 = ((insn[2] & 0xf) << 8) | insn[3];
820       return 1;
821     }
822   else
823     return 0;
824 }
825
826
827 static int
828 is_rsy (bfd_byte *insn, int op1, int op2,
829         unsigned int *r1, unsigned int *r3, int *d2, unsigned int *b2)
830 {
831   if (insn[0] == op1
832       && insn[5] == op2)
833     {
834       *r1 = (insn[1] >> 4) & 0xf;
835       *r3 = insn[1] & 0xf;
836       *b2 = (insn[2] >> 4) & 0xf;
837       /* The 'long displacement' is a 20-bit signed integer.  */
838       *d2 = ((((insn[2] & 0xf) << 8) | insn[3] | (insn[4] << 12))
839                 ^ 0x80000) - 0x80000;
840       return 1;
841     }
842   else
843     return 0;
844 }
845
846
847 static int
848 is_rsi (bfd_byte *insn, int op,
849         unsigned int *r1, unsigned int *r3, int *i2)
850 {
851   if (insn[0] == op)
852     {
853       *r1 = (insn[1] >> 4) & 0xf;
854       *r3 = insn[1] & 0xf;
855       /* i2 is a 16-bit signed quantity.  */
856       *i2 = (((insn[2] << 8) | insn[3]) ^ 0x8000) - 0x8000;
857       return 1;
858     }
859   else
860     return 0;
861 }
862
863
864 static int
865 is_rie (bfd_byte *insn, int op1, int op2,
866         unsigned int *r1, unsigned int *r3, int *i2)
867 {
868   if (insn[0] == op1
869       && insn[5] == op2)
870     {
871       *r1 = (insn[1] >> 4) & 0xf;
872       *r3 = insn[1] & 0xf;
873       /* i2 is a 16-bit signed quantity.  */
874       *i2 = (((insn[2] << 8) | insn[3]) ^ 0x8000) - 0x8000;
875       return 1;
876     }
877   else
878     return 0;
879 }
880
881
882 static int
883 is_rx (bfd_byte *insn, int op,
884        unsigned int *r1, int *d2, unsigned int *x2, unsigned int *b2)
885 {
886   if (insn[0] == op)
887     {
888       *r1 = (insn[1] >> 4) & 0xf;
889       *x2 = insn[1] & 0xf;
890       *b2 = (insn[2] >> 4) & 0xf;
891       *d2 = ((insn[2] & 0xf) << 8) | insn[3];
892       return 1;
893     }
894   else
895     return 0;
896 }
897
898
899 static int
900 is_rxy (bfd_byte *insn, int op1, int op2,
901         unsigned int *r1, int *d2, unsigned int *x2, unsigned int *b2)
902 {
903   if (insn[0] == op1
904       && insn[5] == op2)
905     {
906       *r1 = (insn[1] >> 4) & 0xf;
907       *x2 = insn[1] & 0xf;
908       *b2 = (insn[2] >> 4) & 0xf;
909       /* The 'long displacement' is a 20-bit signed integer.  */
910       *d2 = ((((insn[2] & 0xf) << 8) | insn[3] | (insn[4] << 12))
911                 ^ 0x80000) - 0x80000;
912       return 1;
913     }
914   else
915     return 0;
916 }
917
918
919 /* Prologue analysis.  */
920
921 #define S390_NUM_GPRS 16
922 #define S390_NUM_FPRS 16
923
924 struct s390_prologue_data {
925
926   /* The stack.  */
927   struct pv_area *stack;
928
929   /* The size and byte-order of a GPR or FPR.  */
930   int gpr_size;
931   int fpr_size;
932   enum bfd_endian byte_order;
933
934   /* The general-purpose registers.  */
935   pv_t gpr[S390_NUM_GPRS];
936
937   /* The floating-point registers.  */
938   pv_t fpr[S390_NUM_FPRS];
939
940   /* The offset relative to the CFA where the incoming GPR N was saved
941      by the function prologue.  0 if not saved or unknown.  */
942   int gpr_slot[S390_NUM_GPRS];
943
944   /* Likewise for FPRs.  */
945   int fpr_slot[S390_NUM_FPRS];
946
947   /* Nonzero if the backchain was saved.  This is assumed to be the
948      case when the incoming SP is saved at the current SP location.  */
949   int back_chain_saved_p;
950 };
951
952 /* Return the effective address for an X-style instruction, like:
953
954         L R1, D2(X2, B2)
955
956    Here, X2 and B2 are registers, and D2 is a signed 20-bit
957    constant; the effective address is the sum of all three.  If either
958    X2 or B2 are zero, then it doesn't contribute to the sum --- this
959    means that r0 can't be used as either X2 or B2.  */
960 static pv_t
961 s390_addr (struct s390_prologue_data *data,
962            int d2, unsigned int x2, unsigned int b2)
963 {
964   pv_t result;
965
966   result = pv_constant (d2);
967   if (x2)
968     result = pv_add (result, data->gpr[x2]);
969   if (b2)
970     result = pv_add (result, data->gpr[b2]);
971
972   return result;
973 }
974
975 /* Do a SIZE-byte store of VALUE to D2(X2,B2).  */
976 static void
977 s390_store (struct s390_prologue_data *data,
978             int d2, unsigned int x2, unsigned int b2, CORE_ADDR size,
979             pv_t value)
980 {
981   pv_t addr = s390_addr (data, d2, x2, b2);
982   pv_t offset;
983
984   /* Check whether we are storing the backchain.  */
985   offset = pv_subtract (data->gpr[S390_SP_REGNUM - S390_R0_REGNUM], addr);
986
987   if (pv_is_constant (offset) && offset.k == 0)
988     if (size == data->gpr_size
989         && pv_is_register_k (value, S390_SP_REGNUM, 0))
990       {
991         data->back_chain_saved_p = 1;
992         return;
993       }
994
995
996   /* Check whether we are storing a register into the stack.  */
997   if (!pv_area_store_would_trash (data->stack, addr))
998     pv_area_store (data->stack, addr, size, value);
999
1000
1001   /* Note: If this is some store we cannot identify, you might think we
1002      should forget our cached values, as any of those might have been hit.
1003
1004      However, we make the assumption that the register save areas are only
1005      ever stored to once in any given function, and we do recognize these
1006      stores.  Thus every store we cannot recognize does not hit our data.  */
1007 }
1008
1009 /* Do a SIZE-byte load from D2(X2,B2).  */
1010 static pv_t
1011 s390_load (struct s390_prologue_data *data,
1012            int d2, unsigned int x2, unsigned int b2, CORE_ADDR size)
1013
1014 {
1015   pv_t addr = s390_addr (data, d2, x2, b2);
1016
1017   /* If it's a load from an in-line constant pool, then we can
1018      simulate that, under the assumption that the code isn't
1019      going to change between the time the processor actually
1020      executed it creating the current frame, and the time when
1021      we're analyzing the code to unwind past that frame.  */
1022   if (pv_is_constant (addr))
1023     {
1024       struct target_section *secp;
1025       secp = target_section_by_addr (&current_target, addr.k);
1026       if (secp != NULL
1027           && (bfd_get_section_flags (secp->the_bfd_section->owner,
1028                                      secp->the_bfd_section)
1029               & SEC_READONLY))
1030         return pv_constant (read_memory_integer (addr.k, size,
1031                                                  data->byte_order));
1032     }
1033
1034   /* Check whether we are accessing one of our save slots.  */
1035   return pv_area_fetch (data->stack, addr, size);
1036 }
1037
1038 /* Function for finding saved registers in a 'struct pv_area'; we pass
1039    this to pv_area_scan.
1040
1041    If VALUE is a saved register, ADDR says it was saved at a constant
1042    offset from the frame base, and SIZE indicates that the whole
1043    register was saved, record its offset in the reg_offset table in
1044    PROLOGUE_UNTYPED.  */
1045 static void
1046 s390_check_for_saved (void *data_untyped, pv_t addr,
1047                       CORE_ADDR size, pv_t value)
1048 {
1049   struct s390_prologue_data *data = data_untyped;
1050   int i, offset;
1051
1052   if (!pv_is_register (addr, S390_SP_REGNUM))
1053     return;
1054
1055   offset = 16 * data->gpr_size + 32 - addr.k;
1056
1057   /* If we are storing the original value of a register, we want to
1058      record the CFA offset.  If the same register is stored multiple
1059      times, the stack slot with the highest address counts.  */
1060
1061   for (i = 0; i < S390_NUM_GPRS; i++)
1062     if (size == data->gpr_size
1063         && pv_is_register_k (value, S390_R0_REGNUM + i, 0))
1064       if (data->gpr_slot[i] == 0
1065           || data->gpr_slot[i] > offset)
1066         {
1067           data->gpr_slot[i] = offset;
1068           return;
1069         }
1070
1071   for (i = 0; i < S390_NUM_FPRS; i++)
1072     if (size == data->fpr_size
1073         && pv_is_register_k (value, S390_F0_REGNUM + i, 0))
1074       if (data->fpr_slot[i] == 0
1075           || data->fpr_slot[i] > offset)
1076         {
1077           data->fpr_slot[i] = offset;
1078           return;
1079         }
1080 }
1081
1082 /* Analyze the prologue of the function starting at START_PC,
1083    continuing at most until CURRENT_PC.  Initialize DATA to
1084    hold all information we find out about the state of the registers
1085    and stack slots.  Return the address of the instruction after
1086    the last one that changed the SP, FP, or back chain; or zero
1087    on error.  */
1088 static CORE_ADDR
1089 s390_analyze_prologue (struct gdbarch *gdbarch,
1090                        CORE_ADDR start_pc,
1091                        CORE_ADDR current_pc,
1092                        struct s390_prologue_data *data)
1093 {
1094   int word_size = gdbarch_ptr_bit (gdbarch) / 8;
1095
1096   /* Our return value:
1097      The address of the instruction after the last one that changed
1098      the SP, FP, or back chain;  zero if we got an error trying to
1099      read memory.  */
1100   CORE_ADDR result = start_pc;
1101
1102   /* The current PC for our abstract interpretation.  */
1103   CORE_ADDR pc;
1104
1105   /* The address of the next instruction after that.  */
1106   CORE_ADDR next_pc;
1107
1108   /* Set up everything's initial value.  */
1109   {
1110     int i;
1111
1112     data->stack = make_pv_area (S390_SP_REGNUM, gdbarch_addr_bit (gdbarch));
1113
1114     /* For the purpose of prologue tracking, we consider the GPR size to
1115        be equal to the ABI word size, even if it is actually larger
1116        (i.e. when running a 32-bit binary under a 64-bit kernel).  */
1117     data->gpr_size = word_size;
1118     data->fpr_size = 8;
1119     data->byte_order = gdbarch_byte_order (gdbarch);
1120
1121     for (i = 0; i < S390_NUM_GPRS; i++)
1122       data->gpr[i] = pv_register (S390_R0_REGNUM + i, 0);
1123
1124     for (i = 0; i < S390_NUM_FPRS; i++)
1125       data->fpr[i] = pv_register (S390_F0_REGNUM + i, 0);
1126
1127     for (i = 0; i < S390_NUM_GPRS; i++)
1128       data->gpr_slot[i]  = 0;
1129
1130     for (i = 0; i < S390_NUM_FPRS; i++)
1131       data->fpr_slot[i]  = 0;
1132
1133     data->back_chain_saved_p = 0;
1134   }
1135
1136   /* Start interpreting instructions, until we hit the frame's
1137      current PC or the first branch instruction.  */
1138   for (pc = start_pc; pc > 0 && pc < current_pc; pc = next_pc)
1139     {
1140       bfd_byte insn[S390_MAX_INSTR_SIZE];
1141       int insn_len = s390_readinstruction (insn, pc);
1142
1143       bfd_byte dummy[S390_MAX_INSTR_SIZE] = { 0 };
1144       bfd_byte *insn32 = word_size == 4 ? insn : dummy;
1145       bfd_byte *insn64 = word_size == 8 ? insn : dummy;
1146
1147       /* Fields for various kinds of instructions.  */
1148       unsigned int b2, r1, r2, x2, r3;
1149       int i2, d2;
1150
1151       /* The values of SP and FP before this instruction,
1152          for detecting instructions that change them.  */
1153       pv_t pre_insn_sp, pre_insn_fp;
1154       /* Likewise for the flag whether the back chain was saved.  */
1155       int pre_insn_back_chain_saved_p;
1156
1157       /* If we got an error trying to read the instruction, report it.  */
1158       if (insn_len < 0)
1159         {
1160           result = 0;
1161           break;
1162         }
1163
1164       next_pc = pc + insn_len;
1165
1166       pre_insn_sp = data->gpr[S390_SP_REGNUM - S390_R0_REGNUM];
1167       pre_insn_fp = data->gpr[S390_FRAME_REGNUM - S390_R0_REGNUM];
1168       pre_insn_back_chain_saved_p = data->back_chain_saved_p;
1169
1170
1171       /* LHI r1, i2 --- load halfword immediate.  */
1172       /* LGHI r1, i2 --- load halfword immediate (64-bit version).  */
1173       /* LGFI r1, i2 --- load fullword immediate.  */
1174       if (is_ri (insn32, op1_lhi, op2_lhi, &r1, &i2)
1175           || is_ri (insn64, op1_lghi, op2_lghi, &r1, &i2)
1176           || is_ril (insn, op1_lgfi, op2_lgfi, &r1, &i2))
1177         data->gpr[r1] = pv_constant (i2);
1178
1179       /* LR r1, r2 --- load from register.  */
1180       /* LGR r1, r2 --- load from register (64-bit version).  */
1181       else if (is_rr (insn32, op_lr, &r1, &r2)
1182                || is_rre (insn64, op_lgr, &r1, &r2))
1183         data->gpr[r1] = data->gpr[r2];
1184
1185       /* L r1, d2(x2, b2) --- load.  */
1186       /* LY r1, d2(x2, b2) --- load (long-displacement version).  */
1187       /* LG r1, d2(x2, b2) --- load (64-bit version).  */
1188       else if (is_rx (insn32, op_l, &r1, &d2, &x2, &b2)
1189                || is_rxy (insn32, op1_ly, op2_ly, &r1, &d2, &x2, &b2)
1190                || is_rxy (insn64, op1_lg, op2_lg, &r1, &d2, &x2, &b2))
1191         data->gpr[r1] = s390_load (data, d2, x2, b2, data->gpr_size);
1192
1193       /* ST r1, d2(x2, b2) --- store.  */
1194       /* STY r1, d2(x2, b2) --- store (long-displacement version).  */
1195       /* STG r1, d2(x2, b2) --- store (64-bit version).  */
1196       else if (is_rx (insn32, op_st, &r1, &d2, &x2, &b2)
1197                || is_rxy (insn32, op1_sty, op2_sty, &r1, &d2, &x2, &b2)
1198                || is_rxy (insn64, op1_stg, op2_stg, &r1, &d2, &x2, &b2))
1199         s390_store (data, d2, x2, b2, data->gpr_size, data->gpr[r1]);
1200
1201       /* STD r1, d2(x2,b2) --- store floating-point register.  */
1202       else if (is_rx (insn, op_std, &r1, &d2, &x2, &b2))
1203         s390_store (data, d2, x2, b2, data->fpr_size, data->fpr[r1]);
1204
1205       /* STM r1, r3, d2(b2) --- store multiple.  */
1206       /* STMY r1, r3, d2(b2) --- store multiple (long-displacement
1207          version).  */
1208       /* STMG r1, r3, d2(b2) --- store multiple (64-bit version).  */
1209       else if (is_rs (insn32, op_stm, &r1, &r3, &d2, &b2)
1210                || is_rsy (insn32, op1_stmy, op2_stmy, &r1, &r3, &d2, &b2)
1211                || is_rsy (insn64, op1_stmg, op2_stmg, &r1, &r3, &d2, &b2))
1212         {
1213           for (; r1 <= r3; r1++, d2 += data->gpr_size)
1214             s390_store (data, d2, 0, b2, data->gpr_size, data->gpr[r1]);
1215         }
1216
1217       /* AHI r1, i2 --- add halfword immediate.  */
1218       /* AGHI r1, i2 --- add halfword immediate (64-bit version).  */
1219       /* AFI r1, i2 --- add fullword immediate.  */
1220       /* AGFI r1, i2 --- add fullword immediate (64-bit version).  */
1221       else if (is_ri (insn32, op1_ahi, op2_ahi, &r1, &i2)
1222                || is_ri (insn64, op1_aghi, op2_aghi, &r1, &i2)
1223                || is_ril (insn32, op1_afi, op2_afi, &r1, &i2)
1224                || is_ril (insn64, op1_agfi, op2_agfi, &r1, &i2))
1225         data->gpr[r1] = pv_add_constant (data->gpr[r1], i2);
1226
1227       /* ALFI r1, i2 --- add logical immediate.  */
1228       /* ALGFI r1, i2 --- add logical immediate (64-bit version).  */
1229       else if (is_ril (insn32, op1_alfi, op2_alfi, &r1, &i2)
1230                || is_ril (insn64, op1_algfi, op2_algfi, &r1, &i2))
1231         data->gpr[r1] = pv_add_constant (data->gpr[r1],
1232                                          (CORE_ADDR)i2 & 0xffffffff);
1233
1234       /* AR r1, r2 -- add register.  */
1235       /* AGR r1, r2 -- add register (64-bit version).  */
1236       else if (is_rr (insn32, op_ar, &r1, &r2)
1237                || is_rre (insn64, op_agr, &r1, &r2))
1238         data->gpr[r1] = pv_add (data->gpr[r1], data->gpr[r2]);
1239
1240       /* A r1, d2(x2, b2) -- add.  */
1241       /* AY r1, d2(x2, b2) -- add (long-displacement version).  */
1242       /* AG r1, d2(x2, b2) -- add (64-bit version).  */
1243       else if (is_rx (insn32, op_a, &r1, &d2, &x2, &b2)
1244                || is_rxy (insn32, op1_ay, op2_ay, &r1, &d2, &x2, &b2)
1245                || is_rxy (insn64, op1_ag, op2_ag, &r1, &d2, &x2, &b2))
1246         data->gpr[r1] = pv_add (data->gpr[r1],
1247                                 s390_load (data, d2, x2, b2, data->gpr_size));
1248
1249       /* SLFI r1, i2 --- subtract logical immediate.  */
1250       /* SLGFI r1, i2 --- subtract logical immediate (64-bit version).  */
1251       else if (is_ril (insn32, op1_slfi, op2_slfi, &r1, &i2)
1252                || is_ril (insn64, op1_slgfi, op2_slgfi, &r1, &i2))
1253         data->gpr[r1] = pv_add_constant (data->gpr[r1],
1254                                          -((CORE_ADDR)i2 & 0xffffffff));
1255
1256       /* SR r1, r2 -- subtract register.  */
1257       /* SGR r1, r2 -- subtract register (64-bit version).  */
1258       else if (is_rr (insn32, op_sr, &r1, &r2)
1259                || is_rre (insn64, op_sgr, &r1, &r2))
1260         data->gpr[r1] = pv_subtract (data->gpr[r1], data->gpr[r2]);
1261
1262       /* S r1, d2(x2, b2) -- subtract.  */
1263       /* SY r1, d2(x2, b2) -- subtract (long-displacement version).  */
1264       /* SG r1, d2(x2, b2) -- subtract (64-bit version).  */
1265       else if (is_rx (insn32, op_s, &r1, &d2, &x2, &b2)
1266                || is_rxy (insn32, op1_sy, op2_sy, &r1, &d2, &x2, &b2)
1267                || is_rxy (insn64, op1_sg, op2_sg, &r1, &d2, &x2, &b2))
1268         data->gpr[r1] = pv_subtract (data->gpr[r1],
1269                                 s390_load (data, d2, x2, b2, data->gpr_size));
1270
1271       /* LA r1, d2(x2, b2) --- load address.  */
1272       /* LAY r1, d2(x2, b2) --- load address (long-displacement version).  */
1273       else if (is_rx (insn, op_la, &r1, &d2, &x2, &b2)
1274                || is_rxy (insn, op1_lay, op2_lay, &r1, &d2, &x2, &b2))
1275         data->gpr[r1] = s390_addr (data, d2, x2, b2);
1276
1277       /* LARL r1, i2 --- load address relative long.  */
1278       else if (is_ril (insn, op1_larl, op2_larl, &r1, &i2))
1279         data->gpr[r1] = pv_constant (pc + i2 * 2);
1280
1281       /* BASR r1, 0 --- branch and save.
1282          Since r2 is zero, this saves the PC in r1, but doesn't branch.  */
1283       else if (is_rr (insn, op_basr, &r1, &r2)
1284                && r2 == 0)
1285         data->gpr[r1] = pv_constant (next_pc);
1286
1287       /* BRAS r1, i2 --- branch relative and save.  */
1288       else if (is_ri (insn, op1_bras, op2_bras, &r1, &i2))
1289         {
1290           data->gpr[r1] = pv_constant (next_pc);
1291           next_pc = pc + i2 * 2;
1292
1293           /* We'd better not interpret any backward branches.  We'll
1294              never terminate.  */
1295           if (next_pc <= pc)
1296             break;
1297         }
1298
1299       /* Terminate search when hitting any other branch instruction.  */
1300       else if (is_rr (insn, op_basr, &r1, &r2)
1301                || is_rx (insn, op_bas, &r1, &d2, &x2, &b2)
1302                || is_rr (insn, op_bcr, &r1, &r2)
1303                || is_rx (insn, op_bc, &r1, &d2, &x2, &b2)
1304                || is_ri (insn, op1_brc, op2_brc, &r1, &i2)
1305                || is_ril (insn, op1_brcl, op2_brcl, &r1, &i2)
1306                || is_ril (insn, op1_brasl, op2_brasl, &r2, &i2))
1307         break;
1308
1309       else
1310         {
1311           /* An instruction we don't know how to simulate.  The only
1312              safe thing to do would be to set every value we're tracking
1313              to 'unknown'.  Instead, we'll be optimistic: we assume that
1314              we *can* interpret every instruction that the compiler uses
1315              to manipulate any of the data we're interested in here --
1316              then we can just ignore anything else.  */
1317         }
1318
1319       /* Record the address after the last instruction that changed
1320          the FP, SP, or backlink.  Ignore instructions that changed
1321          them back to their original values --- those are probably
1322          restore instructions.  (The back chain is never restored,
1323          just popped.)  */
1324       {
1325         pv_t sp = data->gpr[S390_SP_REGNUM - S390_R0_REGNUM];
1326         pv_t fp = data->gpr[S390_FRAME_REGNUM - S390_R0_REGNUM];
1327
1328         if ((! pv_is_identical (pre_insn_sp, sp)
1329              && ! pv_is_register_k (sp, S390_SP_REGNUM, 0)
1330              && sp.kind != pvk_unknown)
1331             || (! pv_is_identical (pre_insn_fp, fp)
1332                 && ! pv_is_register_k (fp, S390_FRAME_REGNUM, 0)
1333                 && fp.kind != pvk_unknown)
1334             || pre_insn_back_chain_saved_p != data->back_chain_saved_p)
1335           result = next_pc;
1336       }
1337     }
1338
1339   /* Record where all the registers were saved.  */
1340   pv_area_scan (data->stack, s390_check_for_saved, data);
1341
1342   free_pv_area (data->stack);
1343   data->stack = NULL;
1344
1345   return result;
1346 }
1347
1348 /* Advance PC across any function entry prologue instructions to reach
1349    some "real" code.  */
1350 static CORE_ADDR
1351 s390_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR pc)
1352 {
1353   struct s390_prologue_data data;
1354   CORE_ADDR skip_pc;
1355   skip_pc = s390_analyze_prologue (gdbarch, pc, (CORE_ADDR)-1, &data);
1356   return skip_pc ? skip_pc : pc;
1357 }
1358
1359 /* Return true if we are in the functin's epilogue, i.e. after the
1360    instruction that destroyed the function's stack frame.  */
1361 static int
1362 s390_in_function_epilogue_p (struct gdbarch *gdbarch, CORE_ADDR pc)
1363 {
1364   int word_size = gdbarch_ptr_bit (gdbarch) / 8;
1365
1366   /* In frameless functions, there's not frame to destroy and thus
1367      we don't care about the epilogue.
1368
1369      In functions with frame, the epilogue sequence is a pair of
1370      a LM-type instruction that restores (amongst others) the
1371      return register %r14 and the stack pointer %r15, followed
1372      by a branch 'br %r14' --or equivalent-- that effects the
1373      actual return.
1374
1375      In that situation, this function needs to return 'true' in
1376      exactly one case: when pc points to that branch instruction.
1377
1378      Thus we try to disassemble the one instructions immediately
1379      preceding pc and check whether it is an LM-type instruction
1380      modifying the stack pointer.
1381
1382      Note that disassembling backwards is not reliable, so there
1383      is a slight chance of false positives here ...  */
1384
1385   bfd_byte insn[6];
1386   unsigned int r1, r3, b2;
1387   int d2;
1388
1389   if (word_size == 4
1390       && !target_read_memory (pc - 4, insn, 4)
1391       && is_rs (insn, op_lm, &r1, &r3, &d2, &b2)
1392       && r3 == S390_SP_REGNUM - S390_R0_REGNUM)
1393     return 1;
1394
1395   if (word_size == 4
1396       && !target_read_memory (pc - 6, insn, 6)
1397       && is_rsy (insn, op1_lmy, op2_lmy, &r1, &r3, &d2, &b2)
1398       && r3 == S390_SP_REGNUM - S390_R0_REGNUM)
1399     return 1;
1400
1401   if (word_size == 8
1402       && !target_read_memory (pc - 6, insn, 6)
1403       && is_rsy (insn, op1_lmg, op2_lmg, &r1, &r3, &d2, &b2)
1404       && r3 == S390_SP_REGNUM - S390_R0_REGNUM)
1405     return 1;
1406
1407   return 0;
1408 }
1409
1410 /* Displaced stepping.  */
1411
1412 /* Fix up the state of registers and memory after having single-stepped
1413    a displaced instruction.  */
1414 static void
1415 s390_displaced_step_fixup (struct gdbarch *gdbarch,
1416                            struct displaced_step_closure *closure,
1417                            CORE_ADDR from, CORE_ADDR to,
1418                            struct regcache *regs)
1419 {
1420   /* Since we use simple_displaced_step_copy_insn, our closure is a
1421      copy of the instruction.  */
1422   gdb_byte *insn = (gdb_byte *) closure;
1423   static int s390_instrlen[] = { 2, 4, 4, 6 };
1424   int insnlen = s390_instrlen[insn[0] >> 6];
1425
1426   /* Fields for various kinds of instructions.  */
1427   unsigned int b2, r1, r2, x2, r3;
1428   int i2, d2;
1429
1430   /* Get current PC and addressing mode bit.  */
1431   CORE_ADDR pc = regcache_read_pc (regs);
1432   ULONGEST amode = 0;
1433
1434   if (register_size (gdbarch, S390_PSWA_REGNUM) == 4)
1435     {
1436       regcache_cooked_read_unsigned (regs, S390_PSWA_REGNUM, &amode);
1437       amode &= 0x80000000;
1438     }
1439
1440   if (debug_displaced)
1441     fprintf_unfiltered (gdb_stdlog,
1442                         "displaced: (s390) fixup (%s, %s) pc %s len %d amode 0x%x\n",
1443                         paddress (gdbarch, from), paddress (gdbarch, to),
1444                         paddress (gdbarch, pc), insnlen, (int) amode);
1445
1446   /* Handle absolute branch and save instructions.  */
1447   if (is_rr (insn, op_basr, &r1, &r2)
1448       || is_rx (insn, op_bas, &r1, &d2, &x2, &b2))
1449     {
1450       /* Recompute saved return address in R1.  */
1451       regcache_cooked_write_unsigned (regs, S390_R0_REGNUM + r1,
1452                                       amode | (from + insnlen));
1453     }
1454
1455   /* Handle absolute branch instructions.  */
1456   else if (is_rr (insn, op_bcr, &r1, &r2)
1457            || is_rx (insn, op_bc, &r1, &d2, &x2, &b2)
1458            || is_rr (insn, op_bctr, &r1, &r2)
1459            || is_rre (insn, op_bctgr, &r1, &r2)
1460            || is_rx (insn, op_bct, &r1, &d2, &x2, &b2)
1461            || is_rxy (insn, op1_bctg, op2_brctg, &r1, &d2, &x2, &b2)
1462            || is_rs (insn, op_bxh, &r1, &r3, &d2, &b2)
1463            || is_rsy (insn, op1_bxhg, op2_bxhg, &r1, &r3, &d2, &b2)
1464            || is_rs (insn, op_bxle, &r1, &r3, &d2, &b2)
1465            || is_rsy (insn, op1_bxleg, op2_bxleg, &r1, &r3, &d2, &b2))
1466     {
1467       /* Update PC iff branch was *not* taken.  */
1468       if (pc == to + insnlen)
1469         regcache_write_pc (regs, from + insnlen);
1470     }
1471
1472   /* Handle PC-relative branch and save instructions.  */
1473   else if (is_ri (insn, op1_bras, op2_bras, &r1, &i2)
1474            || is_ril (insn, op1_brasl, op2_brasl, &r1, &i2))
1475     {
1476       /* Update PC.  */
1477       regcache_write_pc (regs, pc - to + from);
1478       /* Recompute saved return address in R1.  */
1479       regcache_cooked_write_unsigned (regs, S390_R0_REGNUM + r1,
1480                                       amode | (from + insnlen));
1481     }
1482
1483   /* Handle PC-relative branch instructions.  */
1484   else if (is_ri (insn, op1_brc, op2_brc, &r1, &i2)
1485            || is_ril (insn, op1_brcl, op2_brcl, &r1, &i2)
1486            || is_ri (insn, op1_brct, op2_brct, &r1, &i2)
1487            || is_ri (insn, op1_brctg, op2_brctg, &r1, &i2)
1488            || is_rsi (insn, op_brxh, &r1, &r3, &i2)
1489            || is_rie (insn, op1_brxhg, op2_brxhg, &r1, &r3, &i2)
1490            || is_rsi (insn, op_brxle, &r1, &r3, &i2)
1491            || is_rie (insn, op1_brxlg, op2_brxlg, &r1, &r3, &i2))
1492     {
1493       /* Update PC.  */
1494       regcache_write_pc (regs, pc - to + from);
1495     }
1496
1497   /* Handle LOAD ADDRESS RELATIVE LONG.  */
1498   else if (is_ril (insn, op1_larl, op2_larl, &r1, &i2))
1499     {
1500       /* Update PC.  */
1501       regcache_write_pc (regs, from + insnlen);
1502       /* Recompute output address in R1.  */
1503       regcache_cooked_write_unsigned (regs, S390_R0_REGNUM + r1,
1504                                       amode | (from + i2 * 2));
1505     }
1506
1507   /* If we executed a breakpoint instruction, point PC right back at it.  */
1508   else if (insn[0] == 0x0 && insn[1] == 0x1)
1509     regcache_write_pc (regs, from);
1510
1511   /* For any other insn, PC points right after the original instruction.  */
1512   else
1513     regcache_write_pc (regs, from + insnlen);
1514
1515   if (debug_displaced)
1516     fprintf_unfiltered (gdb_stdlog,
1517                         "displaced: (s390) pc is now %s\n",
1518                         paddress (gdbarch, regcache_read_pc (regs)));
1519 }
1520
1521
1522 /* Helper routine to unwind pseudo registers.  */
1523
1524 static struct value *
1525 s390_unwind_pseudo_register (struct frame_info *this_frame, int regnum)
1526 {
1527   struct gdbarch *gdbarch = get_frame_arch (this_frame);
1528   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1529   struct type *type = register_type (gdbarch, regnum);
1530
1531   /* Unwind PC via PSW address.  */
1532   if (regnum == tdep->pc_regnum)
1533     {
1534       struct value *val;
1535
1536       val = frame_unwind_register_value (this_frame, S390_PSWA_REGNUM);
1537       if (!value_optimized_out (val))
1538         {
1539           LONGEST pswa = value_as_long (val);
1540
1541           if (TYPE_LENGTH (type) == 4)
1542             return value_from_pointer (type, pswa & 0x7fffffff);
1543           else
1544             return value_from_pointer (type, pswa);
1545         }
1546     }
1547
1548   /* Unwind CC via PSW mask.  */
1549   if (regnum == tdep->cc_regnum)
1550     {
1551       struct value *val;
1552
1553       val = frame_unwind_register_value (this_frame, S390_PSWM_REGNUM);
1554       if (!value_optimized_out (val))
1555         {
1556           LONGEST pswm = value_as_long (val);
1557
1558           if (TYPE_LENGTH (type) == 4)
1559             return value_from_longest (type, (pswm >> 12) & 3);
1560           else
1561             return value_from_longest (type, (pswm >> 44) & 3);
1562         }
1563     }
1564
1565   /* Unwind full GPRs to show at least the lower halves (as the
1566      upper halves are undefined).  */
1567   if (regnum_is_gpr_full (tdep, regnum))
1568     {
1569       int reg = regnum - tdep->gpr_full_regnum;
1570       struct value *val;
1571
1572       val = frame_unwind_register_value (this_frame, S390_R0_REGNUM + reg);
1573       if (!value_optimized_out (val))
1574         return value_cast (type, val);
1575     }
1576
1577   return allocate_optimized_out_value (type);
1578 }
1579
1580 static struct value *
1581 s390_trad_frame_prev_register (struct frame_info *this_frame,
1582                                struct trad_frame_saved_reg saved_regs[],
1583                                int regnum)
1584 {
1585   if (regnum < S390_NUM_REGS)
1586     return trad_frame_get_prev_register (this_frame, saved_regs, regnum);
1587   else
1588     return s390_unwind_pseudo_register (this_frame, regnum);
1589 }
1590
1591
1592 /* Normal stack frames.  */
1593
1594 struct s390_unwind_cache {
1595
1596   CORE_ADDR func;
1597   CORE_ADDR frame_base;
1598   CORE_ADDR local_base;
1599
1600   struct trad_frame_saved_reg *saved_regs;
1601 };
1602
1603 static int
1604 s390_prologue_frame_unwind_cache (struct frame_info *this_frame,
1605                                   struct s390_unwind_cache *info)
1606 {
1607   struct gdbarch *gdbarch = get_frame_arch (this_frame);
1608   int word_size = gdbarch_ptr_bit (gdbarch) / 8;
1609   struct s390_prologue_data data;
1610   pv_t *fp = &data.gpr[S390_FRAME_REGNUM - S390_R0_REGNUM];
1611   pv_t *sp = &data.gpr[S390_SP_REGNUM - S390_R0_REGNUM];
1612   int i;
1613   CORE_ADDR cfa;
1614   CORE_ADDR func;
1615   CORE_ADDR result;
1616   ULONGEST reg;
1617   CORE_ADDR prev_sp;
1618   int frame_pointer;
1619   int size;
1620   struct frame_info *next_frame;
1621
1622   /* Try to find the function start address.  If we can't find it, we don't
1623      bother searching for it -- with modern compilers this would be mostly
1624      pointless anyway.  Trust that we'll either have valid DWARF-2 CFI data
1625      or else a valid backchain ...  */
1626   func = get_frame_func (this_frame);
1627   if (!func)
1628     return 0;
1629
1630   /* Try to analyze the prologue.  */
1631   result = s390_analyze_prologue (gdbarch, func,
1632                                   get_frame_pc (this_frame), &data);
1633   if (!result)
1634     return 0;
1635
1636   /* If this was successful, we should have found the instruction that
1637      sets the stack pointer register to the previous value of the stack
1638      pointer minus the frame size.  */
1639   if (!pv_is_register (*sp, S390_SP_REGNUM))
1640     return 0;
1641
1642   /* A frame size of zero at this point can mean either a real
1643      frameless function, or else a failure to find the prologue.
1644      Perform some sanity checks to verify we really have a
1645      frameless function.  */
1646   if (sp->k == 0)
1647     {
1648       /* If the next frame is a NORMAL_FRAME, this frame *cannot* have frame
1649          size zero.  This is only possible if the next frame is a sentinel
1650          frame, a dummy frame, or a signal trampoline frame.  */
1651       /* FIXME: cagney/2004-05-01: This sanity check shouldn't be
1652          needed, instead the code should simpliy rely on its
1653          analysis.  */
1654       next_frame = get_next_frame (this_frame);
1655       while (next_frame && get_frame_type (next_frame) == INLINE_FRAME)
1656         next_frame = get_next_frame (next_frame);
1657       if (next_frame
1658           && get_frame_type (get_next_frame (this_frame)) == NORMAL_FRAME)
1659         return 0;
1660
1661       /* If we really have a frameless function, %r14 must be valid
1662          -- in particular, it must point to a different function.  */
1663       reg = get_frame_register_unsigned (this_frame, S390_RETADDR_REGNUM);
1664       reg = gdbarch_addr_bits_remove (gdbarch, reg) - 1;
1665       if (get_pc_function_start (reg) == func)
1666         {
1667           /* However, there is one case where it *is* valid for %r14
1668              to point to the same function -- if this is a recursive
1669              call, and we have stopped in the prologue *before* the
1670              stack frame was allocated.
1671
1672              Recognize this case by looking ahead a bit ...  */
1673
1674           struct s390_prologue_data data2;
1675           pv_t *sp = &data2.gpr[S390_SP_REGNUM - S390_R0_REGNUM];
1676
1677           if (!(s390_analyze_prologue (gdbarch, func, (CORE_ADDR)-1, &data2)
1678                 && pv_is_register (*sp, S390_SP_REGNUM)
1679                 && sp->k != 0))
1680             return 0;
1681         }
1682     }
1683
1684
1685   /* OK, we've found valid prologue data.  */
1686   size = -sp->k;
1687
1688   /* If the frame pointer originally also holds the same value
1689      as the stack pointer, we're probably using it.  If it holds
1690      some other value -- even a constant offset -- it is most
1691      likely used as temp register.  */
1692   if (pv_is_identical (*sp, *fp))
1693     frame_pointer = S390_FRAME_REGNUM;
1694   else
1695     frame_pointer = S390_SP_REGNUM;
1696
1697   /* If we've detected a function with stack frame, we'll still have to
1698      treat it as frameless if we're currently within the function epilog
1699      code at a point where the frame pointer has already been restored.
1700      This can only happen in an innermost frame.  */
1701   /* FIXME: cagney/2004-05-01: This sanity check shouldn't be needed,
1702      instead the code should simpliy rely on its analysis.  */
1703   next_frame = get_next_frame (this_frame);
1704   while (next_frame && get_frame_type (next_frame) == INLINE_FRAME)
1705     next_frame = get_next_frame (next_frame);
1706   if (size > 0
1707       && (next_frame == NULL
1708           || get_frame_type (get_next_frame (this_frame)) != NORMAL_FRAME))
1709     {
1710       /* See the comment in s390_in_function_epilogue_p on why this is
1711          not completely reliable ...  */
1712       if (s390_in_function_epilogue_p (gdbarch, get_frame_pc (this_frame)))
1713         {
1714           memset (&data, 0, sizeof (data));
1715           size = 0;
1716           frame_pointer = S390_SP_REGNUM;
1717         }
1718     }
1719
1720   /* Once we know the frame register and the frame size, we can unwind
1721      the current value of the frame register from the next frame, and
1722      add back the frame size to arrive that the previous frame's
1723      stack pointer value.  */
1724   prev_sp = get_frame_register_unsigned (this_frame, frame_pointer) + size;
1725   cfa = prev_sp + 16*word_size + 32;
1726
1727   /* Set up ABI call-saved/call-clobbered registers.  */
1728   for (i = 0; i < S390_NUM_REGS; i++)
1729     if (!s390_register_call_saved (gdbarch, i))
1730       trad_frame_set_unknown (info->saved_regs, i);
1731
1732   /* CC is always call-clobbered.  */
1733   trad_frame_set_unknown (info->saved_regs, S390_PSWM_REGNUM);
1734
1735   /* Record the addresses of all register spill slots the prologue parser
1736      has recognized.  Consider only registers defined as call-saved by the
1737      ABI; for call-clobbered registers the parser may have recognized
1738      spurious stores.  */
1739
1740   for (i = 0; i < 16; i++)
1741     if (s390_register_call_saved (gdbarch, S390_R0_REGNUM + i)
1742         && data.gpr_slot[i] != 0)
1743       info->saved_regs[S390_R0_REGNUM + i].addr = cfa - data.gpr_slot[i];
1744
1745   for (i = 0; i < 16; i++)
1746     if (s390_register_call_saved (gdbarch, S390_F0_REGNUM + i)
1747         && data.fpr_slot[i] != 0)
1748       info->saved_regs[S390_F0_REGNUM + i].addr = cfa - data.fpr_slot[i];
1749
1750   /* Function return will set PC to %r14.  */
1751   info->saved_regs[S390_PSWA_REGNUM] = info->saved_regs[S390_RETADDR_REGNUM];
1752
1753   /* In frameless functions, we unwind simply by moving the return
1754      address to the PC.  However, if we actually stored to the
1755      save area, use that -- we might only think the function frameless
1756      because we're in the middle of the prologue ...  */
1757   if (size == 0
1758       && !trad_frame_addr_p (info->saved_regs, S390_PSWA_REGNUM))
1759     {
1760       info->saved_regs[S390_PSWA_REGNUM].realreg = S390_RETADDR_REGNUM;
1761     }
1762
1763   /* Another sanity check: unless this is a frameless function,
1764      we should have found spill slots for SP and PC.
1765      If not, we cannot unwind further -- this happens e.g. in
1766      libc's thread_start routine.  */
1767   if (size > 0)
1768     {
1769       if (!trad_frame_addr_p (info->saved_regs, S390_SP_REGNUM)
1770           || !trad_frame_addr_p (info->saved_regs, S390_PSWA_REGNUM))
1771         prev_sp = -1;
1772     }
1773
1774   /* We use the current value of the frame register as local_base,
1775      and the top of the register save area as frame_base.  */
1776   if (prev_sp != -1)
1777     {
1778       info->frame_base = prev_sp + 16*word_size + 32;
1779       info->local_base = prev_sp - size;
1780     }
1781
1782   info->func = func;
1783   return 1;
1784 }
1785
1786 static void
1787 s390_backchain_frame_unwind_cache (struct frame_info *this_frame,
1788                                    struct s390_unwind_cache *info)
1789 {
1790   struct gdbarch *gdbarch = get_frame_arch (this_frame);
1791   int word_size = gdbarch_ptr_bit (gdbarch) / 8;
1792   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1793   CORE_ADDR backchain;
1794   ULONGEST reg;
1795   LONGEST sp;
1796   int i;
1797
1798   /* Set up ABI call-saved/call-clobbered registers.  */
1799   for (i = 0; i < S390_NUM_REGS; i++)
1800     if (!s390_register_call_saved (gdbarch, i))
1801       trad_frame_set_unknown (info->saved_regs, i);
1802
1803   /* CC is always call-clobbered.  */
1804   trad_frame_set_unknown (info->saved_regs, S390_PSWM_REGNUM);
1805
1806   /* Get the backchain.  */
1807   reg = get_frame_register_unsigned (this_frame, S390_SP_REGNUM);
1808   backchain = read_memory_unsigned_integer (reg, word_size, byte_order);
1809
1810   /* A zero backchain terminates the frame chain.  As additional
1811      sanity check, let's verify that the spill slot for SP in the
1812      save area pointed to by the backchain in fact links back to
1813      the save area.  */
1814   if (backchain != 0
1815       && safe_read_memory_integer (backchain + 15*word_size,
1816                                    word_size, byte_order, &sp)
1817       && (CORE_ADDR)sp == backchain)
1818     {
1819       /* We don't know which registers were saved, but it will have
1820          to be at least %r14 and %r15.  This will allow us to continue
1821          unwinding, but other prev-frame registers may be incorrect ...  */
1822       info->saved_regs[S390_SP_REGNUM].addr = backchain + 15*word_size;
1823       info->saved_regs[S390_RETADDR_REGNUM].addr = backchain + 14*word_size;
1824
1825       /* Function return will set PC to %r14.  */
1826       info->saved_regs[S390_PSWA_REGNUM]
1827         = info->saved_regs[S390_RETADDR_REGNUM];
1828
1829       /* We use the current value of the frame register as local_base,
1830          and the top of the register save area as frame_base.  */
1831       info->frame_base = backchain + 16*word_size + 32;
1832       info->local_base = reg;
1833     }
1834
1835   info->func = get_frame_pc (this_frame);
1836 }
1837
1838 static struct s390_unwind_cache *
1839 s390_frame_unwind_cache (struct frame_info *this_frame,
1840                          void **this_prologue_cache)
1841 {
1842   volatile struct gdb_exception ex;
1843   struct s390_unwind_cache *info;
1844
1845   if (*this_prologue_cache)
1846     return *this_prologue_cache;
1847
1848   info = FRAME_OBSTACK_ZALLOC (struct s390_unwind_cache);
1849   *this_prologue_cache = info;
1850   info->saved_regs = trad_frame_alloc_saved_regs (this_frame);
1851   info->func = -1;
1852   info->frame_base = -1;
1853   info->local_base = -1;
1854
1855   TRY_CATCH (ex, RETURN_MASK_ERROR)
1856     {
1857       /* Try to use prologue analysis to fill the unwind cache.
1858          If this fails, fall back to reading the stack backchain.  */
1859       if (!s390_prologue_frame_unwind_cache (this_frame, info))
1860         s390_backchain_frame_unwind_cache (this_frame, info);
1861     }
1862   if (ex.reason < 0 && ex.error != NOT_AVAILABLE_ERROR)
1863     throw_exception (ex);
1864
1865   return info;
1866 }
1867
1868 static void
1869 s390_frame_this_id (struct frame_info *this_frame,
1870                     void **this_prologue_cache,
1871                     struct frame_id *this_id)
1872 {
1873   struct s390_unwind_cache *info
1874     = s390_frame_unwind_cache (this_frame, this_prologue_cache);
1875
1876   if (info->frame_base == -1)
1877     return;
1878
1879   *this_id = frame_id_build (info->frame_base, info->func);
1880 }
1881
1882 static struct value *
1883 s390_frame_prev_register (struct frame_info *this_frame,
1884                           void **this_prologue_cache, int regnum)
1885 {
1886   struct gdbarch *gdbarch = get_frame_arch (this_frame);
1887   struct s390_unwind_cache *info
1888     = s390_frame_unwind_cache (this_frame, this_prologue_cache);
1889
1890   return s390_trad_frame_prev_register (this_frame, info->saved_regs, regnum);
1891 }
1892
1893 static const struct frame_unwind s390_frame_unwind = {
1894   NORMAL_FRAME,
1895   default_frame_unwind_stop_reason,
1896   s390_frame_this_id,
1897   s390_frame_prev_register,
1898   NULL,
1899   default_frame_sniffer
1900 };
1901
1902
1903 /* Code stubs and their stack frames.  For things like PLTs and NULL
1904    function calls (where there is no true frame and the return address
1905    is in the RETADDR register).  */
1906
1907 struct s390_stub_unwind_cache
1908 {
1909   CORE_ADDR frame_base;
1910   struct trad_frame_saved_reg *saved_regs;
1911 };
1912
1913 static struct s390_stub_unwind_cache *
1914 s390_stub_frame_unwind_cache (struct frame_info *this_frame,
1915                               void **this_prologue_cache)
1916 {
1917   struct gdbarch *gdbarch = get_frame_arch (this_frame);
1918   int word_size = gdbarch_ptr_bit (gdbarch) / 8;
1919   struct s390_stub_unwind_cache *info;
1920   ULONGEST reg;
1921
1922   if (*this_prologue_cache)
1923     return *this_prologue_cache;
1924
1925   info = FRAME_OBSTACK_ZALLOC (struct s390_stub_unwind_cache);
1926   *this_prologue_cache = info;
1927   info->saved_regs = trad_frame_alloc_saved_regs (this_frame);
1928
1929   /* The return address is in register %r14.  */
1930   info->saved_regs[S390_PSWA_REGNUM].realreg = S390_RETADDR_REGNUM;
1931
1932   /* Retrieve stack pointer and determine our frame base.  */
1933   reg = get_frame_register_unsigned (this_frame, S390_SP_REGNUM);
1934   info->frame_base = reg + 16*word_size + 32;
1935
1936   return info;
1937 }
1938
1939 static void
1940 s390_stub_frame_this_id (struct frame_info *this_frame,
1941                          void **this_prologue_cache,
1942                          struct frame_id *this_id)
1943 {
1944   struct s390_stub_unwind_cache *info
1945     = s390_stub_frame_unwind_cache (this_frame, this_prologue_cache);
1946   *this_id = frame_id_build (info->frame_base, get_frame_pc (this_frame));
1947 }
1948
1949 static struct value *
1950 s390_stub_frame_prev_register (struct frame_info *this_frame,
1951                                void **this_prologue_cache, int regnum)
1952 {
1953   struct s390_stub_unwind_cache *info
1954     = s390_stub_frame_unwind_cache (this_frame, this_prologue_cache);
1955   return s390_trad_frame_prev_register (this_frame, info->saved_regs, regnum);
1956 }
1957
1958 static int
1959 s390_stub_frame_sniffer (const struct frame_unwind *self,
1960                          struct frame_info *this_frame,
1961                          void **this_prologue_cache)
1962 {
1963   CORE_ADDR addr_in_block;
1964   bfd_byte insn[S390_MAX_INSTR_SIZE];
1965
1966   /* If the current PC points to non-readable memory, we assume we
1967      have trapped due to an invalid function pointer call.  We handle
1968      the non-existing current function like a PLT stub.  */
1969   addr_in_block = get_frame_address_in_block (this_frame);
1970   if (in_plt_section (addr_in_block)
1971       || s390_readinstruction (insn, get_frame_pc (this_frame)) < 0)
1972     return 1;
1973   return 0;
1974 }
1975
1976 static const struct frame_unwind s390_stub_frame_unwind = {
1977   NORMAL_FRAME,
1978   default_frame_unwind_stop_reason,
1979   s390_stub_frame_this_id,
1980   s390_stub_frame_prev_register,
1981   NULL,
1982   s390_stub_frame_sniffer
1983 };
1984
1985
1986 /* Signal trampoline stack frames.  */
1987
1988 struct s390_sigtramp_unwind_cache {
1989   CORE_ADDR frame_base;
1990   struct trad_frame_saved_reg *saved_regs;
1991 };
1992
1993 static struct s390_sigtramp_unwind_cache *
1994 s390_sigtramp_frame_unwind_cache (struct frame_info *this_frame,
1995                                   void **this_prologue_cache)
1996 {
1997   struct gdbarch *gdbarch = get_frame_arch (this_frame);
1998   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1999   int word_size = gdbarch_ptr_bit (gdbarch) / 8;
2000   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
2001   struct s390_sigtramp_unwind_cache *info;
2002   ULONGEST this_sp, prev_sp;
2003   CORE_ADDR next_ra, next_cfa, sigreg_ptr, sigreg_high_off;
2004   int i;
2005
2006   if (*this_prologue_cache)
2007     return *this_prologue_cache;
2008
2009   info = FRAME_OBSTACK_ZALLOC (struct s390_sigtramp_unwind_cache);
2010   *this_prologue_cache = info;
2011   info->saved_regs = trad_frame_alloc_saved_regs (this_frame);
2012
2013   this_sp = get_frame_register_unsigned (this_frame, S390_SP_REGNUM);
2014   next_ra = get_frame_pc (this_frame);
2015   next_cfa = this_sp + 16*word_size + 32;
2016
2017   /* New-style RT frame:
2018         retcode + alignment (8 bytes)
2019         siginfo (128 bytes)
2020         ucontext (contains sigregs at offset 5 words).  */
2021   if (next_ra == next_cfa)
2022     {
2023       sigreg_ptr = next_cfa + 8 + 128 + align_up (5*word_size, 8);
2024       /* sigregs are followed by uc_sigmask (8 bytes), then by the
2025          upper GPR halves if present.  */
2026       sigreg_high_off = 8;
2027     }
2028
2029   /* Old-style RT frame and all non-RT frames:
2030         old signal mask (8 bytes)
2031         pointer to sigregs.  */
2032   else
2033     {
2034       sigreg_ptr = read_memory_unsigned_integer (next_cfa + 8,
2035                                                  word_size, byte_order);
2036       /* sigregs are followed by signo (4 bytes), then by the
2037          upper GPR halves if present.  */
2038       sigreg_high_off = 4;
2039     }
2040
2041   /* The sigregs structure looks like this:
2042             long   psw_mask;
2043             long   psw_addr;
2044             long   gprs[16];
2045             int    acrs[16];
2046             int    fpc;
2047             int    __pad;
2048             double fprs[16];  */
2049
2050   /* PSW mask and address.  */
2051   info->saved_regs[S390_PSWM_REGNUM].addr = sigreg_ptr;
2052   sigreg_ptr += word_size;
2053   info->saved_regs[S390_PSWA_REGNUM].addr = sigreg_ptr;
2054   sigreg_ptr += word_size;
2055
2056   /* Then the GPRs.  */
2057   for (i = 0; i < 16; i++)
2058     {
2059       info->saved_regs[S390_R0_REGNUM + i].addr = sigreg_ptr;
2060       sigreg_ptr += word_size;
2061     }
2062
2063   /* Then the ACRs.  */
2064   for (i = 0; i < 16; i++)
2065     {
2066       info->saved_regs[S390_A0_REGNUM + i].addr = sigreg_ptr;
2067       sigreg_ptr += 4;
2068     }
2069
2070   /* The floating-point control word.  */
2071   info->saved_regs[S390_FPC_REGNUM].addr = sigreg_ptr;
2072   sigreg_ptr += 8;
2073
2074   /* And finally the FPRs.  */
2075   for (i = 0; i < 16; i++)
2076     {
2077       info->saved_regs[S390_F0_REGNUM + i].addr = sigreg_ptr;
2078       sigreg_ptr += 8;
2079     }
2080
2081   /* If we have them, the GPR upper halves are appended at the end.  */
2082   sigreg_ptr += sigreg_high_off;
2083   if (tdep->gpr_full_regnum != -1)
2084     for (i = 0; i < 16; i++)
2085       {
2086         info->saved_regs[S390_R0_UPPER_REGNUM + i].addr = sigreg_ptr;
2087         sigreg_ptr += 4;
2088       }
2089
2090   /* Restore the previous frame's SP.  */
2091   prev_sp = read_memory_unsigned_integer (
2092                         info->saved_regs[S390_SP_REGNUM].addr,
2093                         word_size, byte_order);
2094
2095   /* Determine our frame base.  */
2096   info->frame_base = prev_sp + 16*word_size + 32;
2097
2098   return info;
2099 }
2100
2101 static void
2102 s390_sigtramp_frame_this_id (struct frame_info *this_frame,
2103                              void **this_prologue_cache,
2104                              struct frame_id *this_id)
2105 {
2106   struct s390_sigtramp_unwind_cache *info
2107     = s390_sigtramp_frame_unwind_cache (this_frame, this_prologue_cache);
2108   *this_id = frame_id_build (info->frame_base, get_frame_pc (this_frame));
2109 }
2110
2111 static struct value *
2112 s390_sigtramp_frame_prev_register (struct frame_info *this_frame,
2113                                    void **this_prologue_cache, int regnum)
2114 {
2115   struct s390_sigtramp_unwind_cache *info
2116     = s390_sigtramp_frame_unwind_cache (this_frame, this_prologue_cache);
2117   return s390_trad_frame_prev_register (this_frame, info->saved_regs, regnum);
2118 }
2119
2120 static int
2121 s390_sigtramp_frame_sniffer (const struct frame_unwind *self,
2122                              struct frame_info *this_frame,
2123                              void **this_prologue_cache)
2124 {
2125   CORE_ADDR pc = get_frame_pc (this_frame);
2126   bfd_byte sigreturn[2];
2127
2128   if (target_read_memory (pc, sigreturn, 2))
2129     return 0;
2130
2131   if (sigreturn[0] != op_svc)
2132     return 0;
2133
2134   if (sigreturn[1] != 119 /* sigreturn */
2135       && sigreturn[1] != 173 /* rt_sigreturn */)
2136     return 0;
2137
2138   return 1;
2139 }
2140
2141 static const struct frame_unwind s390_sigtramp_frame_unwind = {
2142   SIGTRAMP_FRAME,
2143   default_frame_unwind_stop_reason,
2144   s390_sigtramp_frame_this_id,
2145   s390_sigtramp_frame_prev_register,
2146   NULL,
2147   s390_sigtramp_frame_sniffer
2148 };
2149
2150 /* Retrieve the syscall number at a ptrace syscall-stop.  Return -1
2151    upon error. */
2152
2153 static LONGEST
2154 s390_linux_get_syscall_number (struct gdbarch *gdbarch,
2155                                ptid_t ptid)
2156 {
2157   struct regcache *regs = get_thread_regcache (ptid);
2158   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
2159   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
2160   ULONGEST pc;
2161   ULONGEST svc_number = -1;
2162   unsigned opcode;
2163
2164   /* Assume that the PC points after the 2-byte SVC instruction.  We
2165      don't currently support SVC via EXECUTE. */
2166   regcache_cooked_read_unsigned (regs, tdep->pc_regnum, &pc);
2167   pc -= 2;
2168   opcode = read_memory_unsigned_integer ((CORE_ADDR) pc, 1, byte_order);
2169   if (opcode != op_svc)
2170     return -1;
2171
2172   svc_number = read_memory_unsigned_integer ((CORE_ADDR) pc + 1, 1,
2173                                              byte_order);
2174   if (svc_number == 0)
2175     regcache_cooked_read_unsigned (regs, S390_R1_REGNUM, &svc_number);
2176
2177   return svc_number;
2178 }
2179
2180
2181 /* Frame base handling.  */
2182
2183 static CORE_ADDR
2184 s390_frame_base_address (struct frame_info *this_frame, void **this_cache)
2185 {
2186   struct s390_unwind_cache *info
2187     = s390_frame_unwind_cache (this_frame, this_cache);
2188   return info->frame_base;
2189 }
2190
2191 static CORE_ADDR
2192 s390_local_base_address (struct frame_info *this_frame, void **this_cache)
2193 {
2194   struct s390_unwind_cache *info
2195     = s390_frame_unwind_cache (this_frame, this_cache);
2196   return info->local_base;
2197 }
2198
2199 static const struct frame_base s390_frame_base = {
2200   &s390_frame_unwind,
2201   s390_frame_base_address,
2202   s390_local_base_address,
2203   s390_local_base_address
2204 };
2205
2206 static CORE_ADDR
2207 s390_unwind_pc (struct gdbarch *gdbarch, struct frame_info *next_frame)
2208 {
2209   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
2210   ULONGEST pc;
2211   pc = frame_unwind_register_unsigned (next_frame, tdep->pc_regnum);
2212   return gdbarch_addr_bits_remove (gdbarch, pc);
2213 }
2214
2215 static CORE_ADDR
2216 s390_unwind_sp (struct gdbarch *gdbarch, struct frame_info *next_frame)
2217 {
2218   ULONGEST sp;
2219   sp = frame_unwind_register_unsigned (next_frame, S390_SP_REGNUM);
2220   return gdbarch_addr_bits_remove (gdbarch, sp);
2221 }
2222
2223
2224 /* DWARF-2 frame support.  */
2225
2226 static struct value *
2227 s390_dwarf2_prev_register (struct frame_info *this_frame, void **this_cache,
2228                            int regnum)
2229 {
2230   return s390_unwind_pseudo_register (this_frame, regnum);
2231 }
2232
2233 static void
2234 s390_dwarf2_frame_init_reg (struct gdbarch *gdbarch, int regnum,
2235                             struct dwarf2_frame_state_reg *reg,
2236                             struct frame_info *this_frame)
2237 {
2238   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
2239
2240   /* The condition code (and thus PSW mask) is call-clobbered.  */
2241   if (regnum == S390_PSWM_REGNUM)
2242     reg->how = DWARF2_FRAME_REG_UNDEFINED;
2243
2244   /* The PSW address unwinds to the return address.  */
2245   else if (regnum == S390_PSWA_REGNUM)
2246     reg->how = DWARF2_FRAME_REG_RA;
2247
2248   /* Fixed registers are call-saved or call-clobbered
2249      depending on the ABI in use.  */
2250   else if (regnum < S390_NUM_REGS)
2251     {
2252       if (s390_register_call_saved (gdbarch, regnum))
2253         reg->how = DWARF2_FRAME_REG_SAME_VALUE;
2254       else
2255         reg->how = DWARF2_FRAME_REG_UNDEFINED;
2256     }
2257
2258   /* We install a special function to unwind pseudos.  */
2259   else
2260     {
2261       reg->how = DWARF2_FRAME_REG_FN;
2262       reg->loc.fn = s390_dwarf2_prev_register;
2263     }
2264 }
2265
2266
2267 /* Dummy function calls.  */
2268
2269 /* Return non-zero if TYPE is an integer-like type, zero otherwise.
2270    "Integer-like" types are those that should be passed the way
2271    integers are: integers, enums, ranges, characters, and booleans.  */
2272 static int
2273 is_integer_like (struct type *type)
2274 {
2275   enum type_code code = TYPE_CODE (type);
2276
2277   return (code == TYPE_CODE_INT
2278           || code == TYPE_CODE_ENUM
2279           || code == TYPE_CODE_RANGE
2280           || code == TYPE_CODE_CHAR
2281           || code == TYPE_CODE_BOOL);
2282 }
2283
2284 /* Return non-zero if TYPE is a pointer-like type, zero otherwise.
2285    "Pointer-like" types are those that should be passed the way
2286    pointers are: pointers and references.  */
2287 static int
2288 is_pointer_like (struct type *type)
2289 {
2290   enum type_code code = TYPE_CODE (type);
2291
2292   return (code == TYPE_CODE_PTR
2293           || code == TYPE_CODE_REF);
2294 }
2295
2296
2297 /* Return non-zero if TYPE is a `float singleton' or `double
2298    singleton', zero otherwise.
2299
2300    A `T singleton' is a struct type with one member, whose type is
2301    either T or a `T singleton'.  So, the following are all float
2302    singletons:
2303
2304    struct { float x };
2305    struct { struct { float x; } x; };
2306    struct { struct { struct { float x; } x; } x; };
2307
2308    ... and so on.
2309
2310    All such structures are passed as if they were floats or doubles,
2311    as the (revised) ABI says.  */
2312 static int
2313 is_float_singleton (struct type *type)
2314 {
2315   if (TYPE_CODE (type) == TYPE_CODE_STRUCT && TYPE_NFIELDS (type) == 1)
2316     {
2317       struct type *singleton_type = TYPE_FIELD_TYPE (type, 0);
2318       CHECK_TYPEDEF (singleton_type);
2319
2320       return (TYPE_CODE (singleton_type) == TYPE_CODE_FLT
2321               || TYPE_CODE (singleton_type) == TYPE_CODE_DECFLOAT
2322               || is_float_singleton (singleton_type));
2323     }
2324
2325   return 0;
2326 }
2327
2328
2329 /* Return non-zero if TYPE is a struct-like type, zero otherwise.
2330    "Struct-like" types are those that should be passed as structs are:
2331    structs and unions.
2332
2333    As an odd quirk, not mentioned in the ABI, GCC passes float and
2334    double singletons as if they were a plain float, double, etc.  (The
2335    corresponding union types are handled normally.)  So we exclude
2336    those types here.  *shrug* */
2337 static int
2338 is_struct_like (struct type *type)
2339 {
2340   enum type_code code = TYPE_CODE (type);
2341
2342   return (code == TYPE_CODE_UNION
2343           || (code == TYPE_CODE_STRUCT && ! is_float_singleton (type)));
2344 }
2345
2346
2347 /* Return non-zero if TYPE is a float-like type, zero otherwise.
2348    "Float-like" types are those that should be passed as
2349    floating-point values are.
2350
2351    You'd think this would just be floats, doubles, long doubles, etc.
2352    But as an odd quirk, not mentioned in the ABI, GCC passes float and
2353    double singletons as if they were a plain float, double, etc.  (The
2354    corresponding union types are handled normally.)  So we include
2355    those types here.  *shrug* */
2356 static int
2357 is_float_like (struct type *type)
2358 {
2359   return (TYPE_CODE (type) == TYPE_CODE_FLT
2360           || TYPE_CODE (type) == TYPE_CODE_DECFLOAT
2361           || is_float_singleton (type));
2362 }
2363
2364
2365 static int
2366 is_power_of_two (unsigned int n)
2367 {
2368   return ((n & (n - 1)) == 0);
2369 }
2370
2371 /* Return non-zero if TYPE should be passed as a pointer to a copy,
2372    zero otherwise.  */
2373 static int
2374 s390_function_arg_pass_by_reference (struct type *type)
2375 {
2376   if (TYPE_LENGTH (type) > 8)
2377     return 1;
2378
2379   return (is_struct_like (type) && !is_power_of_two (TYPE_LENGTH (type)))
2380           || TYPE_CODE (type) == TYPE_CODE_COMPLEX
2381           || (TYPE_CODE (type) == TYPE_CODE_ARRAY && TYPE_VECTOR (type));
2382 }
2383
2384 /* Return non-zero if TYPE should be passed in a float register
2385    if possible.  */
2386 static int
2387 s390_function_arg_float (struct type *type)
2388 {
2389   if (TYPE_LENGTH (type) > 8)
2390     return 0;
2391
2392   return is_float_like (type);
2393 }
2394
2395 /* Return non-zero if TYPE should be passed in an integer register
2396    (or a pair of integer registers) if possible.  */
2397 static int
2398 s390_function_arg_integer (struct type *type)
2399 {
2400   if (TYPE_LENGTH (type) > 8)
2401     return 0;
2402
2403    return is_integer_like (type)
2404           || is_pointer_like (type)
2405           || (is_struct_like (type) && is_power_of_two (TYPE_LENGTH (type)));
2406 }
2407
2408 /* Return ARG, a `SIMPLE_ARG', sign-extended or zero-extended to a full
2409    word as required for the ABI.  */
2410 static LONGEST
2411 extend_simple_arg (struct gdbarch *gdbarch, struct value *arg)
2412 {
2413   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
2414   struct type *type = check_typedef (value_type (arg));
2415
2416   /* Even structs get passed in the least significant bits of the
2417      register / memory word.  It's not really right to extract them as
2418      an integer, but it does take care of the extension.  */
2419   if (TYPE_UNSIGNED (type))
2420     return extract_unsigned_integer (value_contents (arg),
2421                                      TYPE_LENGTH (type), byte_order);
2422   else
2423     return extract_signed_integer (value_contents (arg),
2424                                    TYPE_LENGTH (type), byte_order);
2425 }
2426
2427
2428 /* Return the alignment required by TYPE.  */
2429 static int
2430 alignment_of (struct type *type)
2431 {
2432   int alignment;
2433
2434   if (is_integer_like (type)
2435       || is_pointer_like (type)
2436       || TYPE_CODE (type) == TYPE_CODE_FLT
2437       || TYPE_CODE (type) == TYPE_CODE_DECFLOAT)
2438     alignment = TYPE_LENGTH (type);
2439   else if (TYPE_CODE (type) == TYPE_CODE_STRUCT
2440            || TYPE_CODE (type) == TYPE_CODE_UNION)
2441     {
2442       int i;
2443
2444       alignment = 1;
2445       for (i = 0; i < TYPE_NFIELDS (type); i++)
2446         {
2447           int field_alignment
2448             = alignment_of (check_typedef (TYPE_FIELD_TYPE (type, i)));
2449
2450           if (field_alignment > alignment)
2451             alignment = field_alignment;
2452         }
2453     }
2454   else
2455     alignment = 1;
2456
2457   /* Check that everything we ever return is a power of two.  Lots of
2458      code doesn't want to deal with aligning things to arbitrary
2459      boundaries.  */
2460   gdb_assert ((alignment & (alignment - 1)) == 0);
2461
2462   return alignment;
2463 }
2464
2465
2466 /* Put the actual parameter values pointed to by ARGS[0..NARGS-1] in
2467    place to be passed to a function, as specified by the "GNU/Linux
2468    for S/390 ELF Application Binary Interface Supplement".
2469
2470    SP is the current stack pointer.  We must put arguments, links,
2471    padding, etc. whereever they belong, and return the new stack
2472    pointer value.
2473
2474    If STRUCT_RETURN is non-zero, then the function we're calling is
2475    going to return a structure by value; STRUCT_ADDR is the address of
2476    a block we've allocated for it on the stack.
2477
2478    Our caller has taken care of any type promotions needed to satisfy
2479    prototypes or the old K&R argument-passing rules.  */
2480 static CORE_ADDR
2481 s390_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
2482                       struct regcache *regcache, CORE_ADDR bp_addr,
2483                       int nargs, struct value **args, CORE_ADDR sp,
2484                       int struct_return, CORE_ADDR struct_addr)
2485 {
2486   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
2487   int word_size = gdbarch_ptr_bit (gdbarch) / 8;
2488   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
2489   int i;
2490
2491   /* If the i'th argument is passed as a reference to a copy, then
2492      copy_addr[i] is the address of the copy we made.  */
2493   CORE_ADDR *copy_addr = alloca (nargs * sizeof (CORE_ADDR));
2494
2495   /* Reserve space for the reference-to-copy area.  */
2496   for (i = 0; i < nargs; i++)
2497     {
2498       struct value *arg = args[i];
2499       struct type *type = check_typedef (value_type (arg));
2500
2501       if (s390_function_arg_pass_by_reference (type))
2502         {
2503           sp -= TYPE_LENGTH (type);
2504           sp = align_down (sp, alignment_of (type));
2505           copy_addr[i] = sp;
2506         }
2507     }
2508
2509   /* Reserve space for the parameter area.  As a conservative
2510      simplification, we assume that everything will be passed on the
2511      stack.  Since every argument larger than 8 bytes will be
2512      passed by reference, we use this simple upper bound.  */
2513   sp -= nargs * 8;
2514
2515   /* After all that, make sure it's still aligned on an eight-byte
2516      boundary.  */
2517   sp = align_down (sp, 8);
2518
2519   /* Allocate the standard frame areas: the register save area, the
2520      word reserved for the compiler (which seems kind of meaningless),
2521      and the back chain pointer.  */
2522   sp -= 16*word_size + 32;
2523
2524   /* Now we have the final SP value.  Make sure we didn't underflow;
2525      on 31-bit, this would result in addresses with the high bit set,
2526      which causes confusion elsewhere.  Note that if we error out
2527      here, stack and registers remain untouched.  */
2528   if (gdbarch_addr_bits_remove (gdbarch, sp) != sp)
2529     error (_("Stack overflow"));
2530
2531
2532   /* Finally, place the actual parameters, working from SP towards
2533      higher addresses.  The code above is supposed to reserve enough
2534      space for this.  */
2535   {
2536     int fr = 0;
2537     int gr = 2;
2538     CORE_ADDR starg = sp + 16*word_size + 32;
2539
2540     /* A struct is returned using general register 2.  */
2541     if (struct_return)
2542       {
2543         regcache_cooked_write_unsigned (regcache, S390_R0_REGNUM + gr,
2544                                         struct_addr);
2545         gr++;
2546       }
2547
2548     for (i = 0; i < nargs; i++)
2549       {
2550         struct value *arg = args[i];
2551         struct type *type = check_typedef (value_type (arg));
2552         unsigned length = TYPE_LENGTH (type);
2553
2554         if (s390_function_arg_pass_by_reference (type))
2555           {
2556             /* Actually copy the argument contents to the stack slot
2557                that was reserved above.  */
2558             write_memory (copy_addr[i], value_contents (arg), length);
2559
2560             if (gr <= 6)
2561               {
2562                 regcache_cooked_write_unsigned (regcache, S390_R0_REGNUM + gr,
2563                                                 copy_addr[i]);
2564                 gr++;
2565               }
2566             else
2567               {
2568                 write_memory_unsigned_integer (starg, word_size, byte_order,
2569                                                copy_addr[i]);
2570                 starg += word_size;
2571               }
2572           }
2573         else if (s390_function_arg_float (type))
2574           {
2575             /* The GNU/Linux for S/390 ABI uses FPRs 0 and 2 to pass arguments,
2576                the GNU/Linux for zSeries ABI uses 0, 2, 4, and 6.  */
2577             if (fr <= (tdep->abi == ABI_LINUX_S390 ? 2 : 6))
2578               {
2579                 /* When we store a single-precision value in an FP register,
2580                    it occupies the leftmost bits.  */
2581                 regcache_cooked_write_part (regcache, S390_F0_REGNUM + fr,
2582                                             0, length, value_contents (arg));
2583                 fr += 2;
2584               }
2585             else
2586               {
2587                 /* When we store a single-precision value in a stack slot,
2588                    it occupies the rightmost bits.  */
2589                 starg = align_up (starg + length, word_size);
2590                 write_memory (starg - length, value_contents (arg), length);
2591               }
2592           }
2593         else if (s390_function_arg_integer (type) && length <= word_size)
2594           {
2595             if (gr <= 6)
2596               {
2597                 /* Integer arguments are always extended to word size.  */
2598                 regcache_cooked_write_signed (regcache, S390_R0_REGNUM + gr,
2599                                               extend_simple_arg (gdbarch,
2600                                                                  arg));
2601                 gr++;
2602               }
2603             else
2604               {
2605                 /* Integer arguments are always extended to word size.  */
2606                 write_memory_signed_integer (starg, word_size, byte_order,
2607                                              extend_simple_arg (gdbarch, arg));
2608                 starg += word_size;
2609               }
2610           }
2611         else if (s390_function_arg_integer (type) && length == 2*word_size)
2612           {
2613             if (gr <= 5)
2614               {
2615                 regcache_cooked_write (regcache, S390_R0_REGNUM + gr,
2616                                        value_contents (arg));
2617                 regcache_cooked_write (regcache, S390_R0_REGNUM + gr + 1,
2618                                        value_contents (arg) + word_size);
2619                 gr += 2;
2620               }
2621             else
2622               {
2623                 /* If we skipped r6 because we couldn't fit a DOUBLE_ARG
2624                    in it, then don't go back and use it again later.  */
2625                 gr = 7;
2626
2627                 write_memory (starg, value_contents (arg), length);
2628                 starg += length;
2629               }
2630           }
2631         else
2632           internal_error (__FILE__, __LINE__, _("unknown argument type"));
2633       }
2634   }
2635
2636   /* Store return PSWA.  In 31-bit mode, keep addressing mode bit.  */
2637   if (word_size == 4)
2638     {
2639       ULONGEST pswa;
2640       regcache_cooked_read_unsigned (regcache, S390_PSWA_REGNUM, &pswa);
2641       bp_addr = (bp_addr & 0x7fffffff) | (pswa & 0x80000000);
2642     }
2643   regcache_cooked_write_unsigned (regcache, S390_RETADDR_REGNUM, bp_addr);
2644
2645   /* Store updated stack pointer.  */
2646   regcache_cooked_write_unsigned (regcache, S390_SP_REGNUM, sp);
2647
2648   /* We need to return the 'stack part' of the frame ID,
2649      which is actually the top of the register save area.  */
2650   return sp + 16*word_size + 32;
2651 }
2652
2653 /* Assuming THIS_FRAME is a dummy, return the frame ID of that
2654    dummy frame.  The frame ID's base needs to match the TOS value
2655    returned by push_dummy_call, and the PC match the dummy frame's
2656    breakpoint.  */
2657 static struct frame_id
2658 s390_dummy_id (struct gdbarch *gdbarch, struct frame_info *this_frame)
2659 {
2660   int word_size = gdbarch_ptr_bit (gdbarch) / 8;
2661   CORE_ADDR sp = get_frame_register_unsigned (this_frame, S390_SP_REGNUM);
2662   sp = gdbarch_addr_bits_remove (gdbarch, sp);
2663
2664   return frame_id_build (sp + 16*word_size + 32,
2665                          get_frame_pc (this_frame));
2666 }
2667
2668 static CORE_ADDR
2669 s390_frame_align (struct gdbarch *gdbarch, CORE_ADDR addr)
2670 {
2671   /* Both the 32- and 64-bit ABI's say that the stack pointer should
2672      always be aligned on an eight-byte boundary.  */
2673   return (addr & -8);
2674 }
2675
2676
2677 /* Function return value access.  */
2678
2679 static enum return_value_convention
2680 s390_return_value_convention (struct gdbarch *gdbarch, struct type *type)
2681 {
2682   if (TYPE_LENGTH (type) > 8)
2683     return RETURN_VALUE_STRUCT_CONVENTION;
2684
2685   switch (TYPE_CODE (type))
2686     {
2687     case TYPE_CODE_STRUCT:
2688     case TYPE_CODE_UNION:
2689     case TYPE_CODE_ARRAY:
2690     case TYPE_CODE_COMPLEX:
2691       return RETURN_VALUE_STRUCT_CONVENTION;
2692
2693     default:
2694       return RETURN_VALUE_REGISTER_CONVENTION;
2695     }
2696 }
2697
2698 static enum return_value_convention
2699 s390_return_value (struct gdbarch *gdbarch, struct value *function,
2700                    struct type *type, struct regcache *regcache,
2701                    gdb_byte *out, const gdb_byte *in)
2702 {
2703   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
2704   int word_size = gdbarch_ptr_bit (gdbarch) / 8;
2705   enum return_value_convention rvc;
2706   int length;
2707
2708   type = check_typedef (type);
2709   rvc = s390_return_value_convention (gdbarch, type);
2710   length = TYPE_LENGTH (type);
2711
2712   if (in)
2713     {
2714       switch (rvc)
2715         {
2716         case RETURN_VALUE_REGISTER_CONVENTION:
2717           if (TYPE_CODE (type) == TYPE_CODE_FLT
2718               || TYPE_CODE (type) == TYPE_CODE_DECFLOAT)
2719             {
2720               /* When we store a single-precision value in an FP register,
2721                  it occupies the leftmost bits.  */
2722               regcache_cooked_write_part (regcache, S390_F0_REGNUM,
2723                                           0, length, in);
2724             }
2725           else if (length <= word_size)
2726             {
2727               /* Integer arguments are always extended to word size.  */
2728               if (TYPE_UNSIGNED (type))
2729                 regcache_cooked_write_unsigned (regcache, S390_R2_REGNUM,
2730                         extract_unsigned_integer (in, length, byte_order));
2731               else
2732                 regcache_cooked_write_signed (regcache, S390_R2_REGNUM,
2733                         extract_signed_integer (in, length, byte_order));
2734             }
2735           else if (length == 2*word_size)
2736             {
2737               regcache_cooked_write (regcache, S390_R2_REGNUM, in);
2738               regcache_cooked_write (regcache, S390_R3_REGNUM, in + word_size);
2739             }
2740           else
2741             internal_error (__FILE__, __LINE__, _("invalid return type"));
2742           break;
2743
2744         case RETURN_VALUE_STRUCT_CONVENTION:
2745           error (_("Cannot set function return value."));
2746           break;
2747         }
2748     }
2749   else if (out)
2750     {
2751       switch (rvc)
2752         {
2753         case RETURN_VALUE_REGISTER_CONVENTION:
2754           if (TYPE_CODE (type) == TYPE_CODE_FLT
2755               || TYPE_CODE (type) == TYPE_CODE_DECFLOAT)
2756             {
2757               /* When we store a single-precision value in an FP register,
2758                  it occupies the leftmost bits.  */
2759               regcache_cooked_read_part (regcache, S390_F0_REGNUM,
2760                                          0, length, out);
2761             }
2762           else if (length <= word_size)
2763             {
2764               /* Integer arguments occupy the rightmost bits.  */
2765               regcache_cooked_read_part (regcache, S390_R2_REGNUM,
2766                                          word_size - length, length, out);
2767             }
2768           else if (length == 2*word_size)
2769             {
2770               regcache_cooked_read (regcache, S390_R2_REGNUM, out);
2771               regcache_cooked_read (regcache, S390_R3_REGNUM, out + word_size);
2772             }
2773           else
2774             internal_error (__FILE__, __LINE__, _("invalid return type"));
2775           break;
2776
2777         case RETURN_VALUE_STRUCT_CONVENTION:
2778           error (_("Function return value unknown."));
2779           break;
2780         }
2781     }
2782
2783   return rvc;
2784 }
2785
2786
2787 /* Breakpoints.  */
2788
2789 static const gdb_byte *
2790 s390_breakpoint_from_pc (struct gdbarch *gdbarch,
2791                          CORE_ADDR *pcptr, int *lenptr)
2792 {
2793   static const gdb_byte breakpoint[] = { 0x0, 0x1 };
2794
2795   *lenptr = sizeof (breakpoint);
2796   return breakpoint;
2797 }
2798
2799
2800 /* Address handling.  */
2801
2802 static CORE_ADDR
2803 s390_addr_bits_remove (struct gdbarch *gdbarch, CORE_ADDR addr)
2804 {
2805   return addr & 0x7fffffff;
2806 }
2807
2808 static int
2809 s390_address_class_type_flags (int byte_size, int dwarf2_addr_class)
2810 {
2811   if (byte_size == 4)
2812     return TYPE_INSTANCE_FLAG_ADDRESS_CLASS_1;
2813   else
2814     return 0;
2815 }
2816
2817 static const char *
2818 s390_address_class_type_flags_to_name (struct gdbarch *gdbarch, int type_flags)
2819 {
2820   if (type_flags & TYPE_INSTANCE_FLAG_ADDRESS_CLASS_1)
2821     return "mode32";
2822   else
2823     return NULL;
2824 }
2825
2826 static int
2827 s390_address_class_name_to_type_flags (struct gdbarch *gdbarch,
2828                                        const char *name,
2829                                        int *type_flags_ptr)
2830 {
2831   if (strcmp (name, "mode32") == 0)
2832     {
2833       *type_flags_ptr = TYPE_INSTANCE_FLAG_ADDRESS_CLASS_1;
2834       return 1;
2835     }
2836   else
2837     return 0;
2838 }
2839
2840 /* Implementation of `gdbarch_stap_is_single_operand', as defined in
2841    gdbarch.h.  */
2842
2843 static int
2844 s390_stap_is_single_operand (struct gdbarch *gdbarch, const char *s)
2845 {
2846   return ((isdigit (*s) && s[1] == '(' && s[2] == '%') /* Displacement
2847                                                           or indirection.  */
2848           || *s == '%' /* Register access.  */
2849           || isdigit (*s)); /* Literal number.  */
2850 }
2851
2852 /* Set up gdbarch struct.  */
2853
2854 static struct gdbarch *
2855 s390_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
2856 {
2857   const struct target_desc *tdesc = info.target_desc;
2858   struct tdesc_arch_data *tdesc_data = NULL;
2859   struct gdbarch *gdbarch;
2860   struct gdbarch_tdep *tdep;
2861   int tdep_abi;
2862   int have_upper = 0;
2863   int have_linux_v1 = 0;
2864   int have_linux_v2 = 0;
2865   int have_tdb = 0;
2866   int first_pseudo_reg, last_pseudo_reg;
2867   static const char *const stap_register_prefixes[] = { "%", NULL };
2868   static const char *const stap_register_indirection_prefixes[] = { "(",
2869                                                                     NULL };
2870   static const char *const stap_register_indirection_suffixes[] = { ")",
2871                                                                     NULL };
2872
2873   /* Default ABI and register size.  */
2874   switch (info.bfd_arch_info->mach)
2875     {
2876     case bfd_mach_s390_31:
2877       tdep_abi = ABI_LINUX_S390;
2878       break;
2879
2880     case bfd_mach_s390_64:
2881       tdep_abi = ABI_LINUX_ZSERIES;
2882       break;
2883
2884     default:
2885       return NULL;
2886     }
2887
2888   /* Use default target description if none provided by the target.  */
2889   if (!tdesc_has_registers (tdesc))
2890     {
2891       if (tdep_abi == ABI_LINUX_S390)
2892         tdesc = tdesc_s390_linux32;
2893       else
2894         tdesc = tdesc_s390x_linux64;
2895     }
2896
2897   /* Check any target description for validity.  */
2898   if (tdesc_has_registers (tdesc))
2899     {
2900       static const char *const gprs[] = {
2901         "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
2902         "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
2903       };
2904       static const char *const fprs[] = {
2905         "f0", "f1", "f2", "f3", "f4", "f5", "f6", "f7",
2906         "f8", "f9", "f10", "f11", "f12", "f13", "f14", "f15"
2907       };
2908       static const char *const acrs[] = {
2909         "acr0", "acr1", "acr2", "acr3", "acr4", "acr5", "acr6", "acr7",
2910         "acr8", "acr9", "acr10", "acr11", "acr12", "acr13", "acr14", "acr15"
2911       };
2912       static const char *const gprs_lower[] = {
2913         "r0l", "r1l", "r2l", "r3l", "r4l", "r5l", "r6l", "r7l",
2914         "r8l", "r9l", "r10l", "r11l", "r12l", "r13l", "r14l", "r15l"
2915       };
2916       static const char *const gprs_upper[] = {
2917         "r0h", "r1h", "r2h", "r3h", "r4h", "r5h", "r6h", "r7h",
2918         "r8h", "r9h", "r10h", "r11h", "r12h", "r13h", "r14h", "r15h"
2919       };
2920       static const char *const tdb_regs[] = {
2921         "tdb0", "tac", "tct", "atia",
2922         "tr0", "tr1", "tr2", "tr3", "tr4", "tr5", "tr6", "tr7",
2923         "tr8", "tr9", "tr10", "tr11", "tr12", "tr13", "tr14", "tr15"
2924       };
2925       const struct tdesc_feature *feature;
2926       int i, valid_p = 1;
2927
2928       feature = tdesc_find_feature (tdesc, "org.gnu.gdb.s390.core");
2929       if (feature == NULL)
2930         return NULL;
2931
2932       tdesc_data = tdesc_data_alloc ();
2933
2934       valid_p &= tdesc_numbered_register (feature, tdesc_data,
2935                                           S390_PSWM_REGNUM, "pswm");
2936       valid_p &= tdesc_numbered_register (feature, tdesc_data,
2937                                           S390_PSWA_REGNUM, "pswa");
2938
2939       if (tdesc_unnumbered_register (feature, "r0"))
2940         {
2941           for (i = 0; i < 16; i++)
2942             valid_p &= tdesc_numbered_register (feature, tdesc_data,
2943                                                 S390_R0_REGNUM + i, gprs[i]);
2944         }
2945       else
2946         {
2947           have_upper = 1;
2948
2949           for (i = 0; i < 16; i++)
2950             valid_p &= tdesc_numbered_register (feature, tdesc_data,
2951                                                 S390_R0_REGNUM + i,
2952                                                 gprs_lower[i]);
2953           for (i = 0; i < 16; i++)
2954             valid_p &= tdesc_numbered_register (feature, tdesc_data,
2955                                                 S390_R0_UPPER_REGNUM + i,
2956                                                 gprs_upper[i]);
2957         }
2958
2959       feature = tdesc_find_feature (tdesc, "org.gnu.gdb.s390.fpr");
2960       if (feature == NULL)
2961         {
2962           tdesc_data_cleanup (tdesc_data);
2963           return NULL;
2964         }
2965
2966       valid_p &= tdesc_numbered_register (feature, tdesc_data,
2967                                           S390_FPC_REGNUM, "fpc");
2968       for (i = 0; i < 16; i++)
2969         valid_p &= tdesc_numbered_register (feature, tdesc_data,
2970                                             S390_F0_REGNUM + i, fprs[i]);
2971
2972       feature = tdesc_find_feature (tdesc, "org.gnu.gdb.s390.acr");
2973       if (feature == NULL)
2974         {
2975           tdesc_data_cleanup (tdesc_data);
2976           return NULL;
2977         }
2978
2979       for (i = 0; i < 16; i++)
2980         valid_p &= tdesc_numbered_register (feature, tdesc_data,
2981                                             S390_A0_REGNUM + i, acrs[i]);
2982
2983       /* Optional GNU/Linux-specific "registers".  */
2984       feature = tdesc_find_feature (tdesc, "org.gnu.gdb.s390.linux");
2985       if (feature)
2986         {
2987           tdesc_numbered_register (feature, tdesc_data,
2988                                    S390_ORIG_R2_REGNUM, "orig_r2");
2989
2990           if (tdesc_numbered_register (feature, tdesc_data,
2991                                        S390_LAST_BREAK_REGNUM, "last_break"))
2992             have_linux_v1 = 1;
2993
2994           if (tdesc_numbered_register (feature, tdesc_data,
2995                                        S390_SYSTEM_CALL_REGNUM, "system_call"))
2996             have_linux_v2 = 1;
2997
2998           if (have_linux_v2 > have_linux_v1)
2999             valid_p = 0;
3000         }
3001
3002       /* Transaction diagnostic block.  */
3003       feature = tdesc_find_feature (tdesc, "org.gnu.gdb.s390.tdb");
3004       if (feature)
3005         {
3006           for (i = 0; i < ARRAY_SIZE (tdb_regs); i++)
3007             valid_p &= tdesc_numbered_register (feature, tdesc_data,
3008                                                 S390_TDB_DWORD0_REGNUM + i,
3009                                                 tdb_regs[i]);
3010           have_tdb = 1;
3011         }
3012
3013       if (!valid_p)
3014         {
3015           tdesc_data_cleanup (tdesc_data);
3016           return NULL;
3017         }
3018     }
3019
3020   /* Find a candidate among extant architectures.  */
3021   for (arches = gdbarch_list_lookup_by_info (arches, &info);
3022        arches != NULL;
3023        arches = gdbarch_list_lookup_by_info (arches->next, &info))
3024     {
3025       tdep = gdbarch_tdep (arches->gdbarch);
3026       if (!tdep)
3027         continue;
3028       if (tdep->abi != tdep_abi)
3029         continue;
3030       if ((tdep->gpr_full_regnum != -1) != have_upper)
3031         continue;
3032       if (tdesc_data != NULL)
3033         tdesc_data_cleanup (tdesc_data);
3034       return arches->gdbarch;
3035     }
3036
3037   /* Otherwise create a new gdbarch for the specified machine type.  */
3038   tdep = XCNEW (struct gdbarch_tdep);
3039   tdep->abi = tdep_abi;
3040   tdep->have_linux_v1 = have_linux_v1;
3041   tdep->have_linux_v2 = have_linux_v2;
3042   tdep->have_tdb = have_tdb;
3043   gdbarch = gdbarch_alloc (&info, tdep);
3044
3045   set_gdbarch_believe_pcc_promotion (gdbarch, 0);
3046   set_gdbarch_char_signed (gdbarch, 0);
3047
3048   /* S/390 GNU/Linux uses either 64-bit or 128-bit long doubles.
3049      We can safely let them default to 128-bit, since the debug info
3050      will give the size of type actually used in each case.  */
3051   set_gdbarch_long_double_bit (gdbarch, 128);
3052   set_gdbarch_long_double_format (gdbarch, floatformats_ia64_quad);
3053
3054   /* Amount PC must be decremented by after a breakpoint.  This is
3055      often the number of bytes returned by gdbarch_breakpoint_from_pc but not
3056      always.  */
3057   set_gdbarch_decr_pc_after_break (gdbarch, 2);
3058   /* Stack grows downward.  */
3059   set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
3060   set_gdbarch_breakpoint_from_pc (gdbarch, s390_breakpoint_from_pc);
3061   set_gdbarch_skip_prologue (gdbarch, s390_skip_prologue);
3062   set_gdbarch_in_function_epilogue_p (gdbarch, s390_in_function_epilogue_p);
3063
3064   set_gdbarch_num_regs (gdbarch, S390_NUM_REGS);
3065   set_gdbarch_sp_regnum (gdbarch, S390_SP_REGNUM);
3066   set_gdbarch_fp0_regnum (gdbarch, S390_F0_REGNUM);
3067   set_gdbarch_stab_reg_to_regnum (gdbarch, s390_dwarf_reg_to_regnum);
3068   set_gdbarch_dwarf2_reg_to_regnum (gdbarch, s390_dwarf_reg_to_regnum);
3069   set_gdbarch_value_from_register (gdbarch, s390_value_from_register);
3070   set_gdbarch_regset_from_core_section (gdbarch,
3071                                         s390_regset_from_core_section);
3072   set_gdbarch_core_read_description (gdbarch, s390_core_read_description);
3073   set_gdbarch_iterate_over_regset_sections (gdbarch,
3074                                             s390_iterate_over_regset_sections);
3075   set_gdbarch_cannot_store_register (gdbarch, s390_cannot_store_register);
3076   set_gdbarch_write_pc (gdbarch, s390_write_pc);
3077   set_gdbarch_pseudo_register_read (gdbarch, s390_pseudo_register_read);
3078   set_gdbarch_pseudo_register_write (gdbarch, s390_pseudo_register_write);
3079   set_tdesc_pseudo_register_name (gdbarch, s390_pseudo_register_name);
3080   set_tdesc_pseudo_register_type (gdbarch, s390_pseudo_register_type);
3081   set_tdesc_pseudo_register_reggroup_p (gdbarch,
3082                                         s390_pseudo_register_reggroup_p);
3083   tdesc_use_registers (gdbarch, tdesc, tdesc_data);
3084
3085   /* Assign pseudo register numbers.  */
3086   first_pseudo_reg = gdbarch_num_regs (gdbarch);
3087   last_pseudo_reg = first_pseudo_reg;
3088   tdep->gpr_full_regnum = -1;
3089   if (have_upper)
3090     {
3091       tdep->gpr_full_regnum = last_pseudo_reg;
3092       last_pseudo_reg += 16;
3093     }
3094   tdep->pc_regnum = last_pseudo_reg++;
3095   tdep->cc_regnum = last_pseudo_reg++;
3096   set_gdbarch_pc_regnum (gdbarch, tdep->pc_regnum);
3097   set_gdbarch_num_pseudo_regs (gdbarch, last_pseudo_reg - first_pseudo_reg);
3098
3099   /* Inferior function calls.  */
3100   set_gdbarch_push_dummy_call (gdbarch, s390_push_dummy_call);
3101   set_gdbarch_dummy_id (gdbarch, s390_dummy_id);
3102   set_gdbarch_frame_align (gdbarch, s390_frame_align);
3103   set_gdbarch_return_value (gdbarch, s390_return_value);
3104
3105   /* Syscall handling.  */
3106   set_gdbarch_get_syscall_number (gdbarch, s390_linux_get_syscall_number);
3107
3108   /* Frame handling.  */
3109   dwarf2_frame_set_init_reg (gdbarch, s390_dwarf2_frame_init_reg);
3110   dwarf2_frame_set_adjust_regnum (gdbarch, s390_adjust_frame_regnum);
3111   dwarf2_append_unwinders (gdbarch);
3112   frame_base_append_sniffer (gdbarch, dwarf2_frame_base_sniffer);
3113   frame_unwind_append_unwinder (gdbarch, &s390_stub_frame_unwind);
3114   frame_unwind_append_unwinder (gdbarch, &s390_sigtramp_frame_unwind);
3115   frame_unwind_append_unwinder (gdbarch, &s390_frame_unwind);
3116   frame_base_set_default (gdbarch, &s390_frame_base);
3117   set_gdbarch_unwind_pc (gdbarch, s390_unwind_pc);
3118   set_gdbarch_unwind_sp (gdbarch, s390_unwind_sp);
3119
3120   /* Displaced stepping.  */
3121   set_gdbarch_displaced_step_copy_insn (gdbarch,
3122                                         simple_displaced_step_copy_insn);
3123   set_gdbarch_displaced_step_fixup (gdbarch, s390_displaced_step_fixup);
3124   set_gdbarch_displaced_step_free_closure (gdbarch,
3125                                            simple_displaced_step_free_closure);
3126   set_gdbarch_displaced_step_location (gdbarch,
3127                                        displaced_step_at_entry_point);
3128   set_gdbarch_max_insn_length (gdbarch, S390_MAX_INSTR_SIZE);
3129
3130   /* Note that GNU/Linux is the only OS supported on this
3131      platform.  */
3132   linux_init_abi (info, gdbarch);
3133
3134   switch (tdep->abi)
3135     {
3136     case ABI_LINUX_S390:
3137       tdep->gregset = &s390_gregset;
3138       tdep->sizeof_gregset = s390_sizeof_gregset;
3139       tdep->fpregset = &s390_fpregset;
3140       tdep->sizeof_fpregset = s390_sizeof_fpregset;
3141
3142       set_gdbarch_addr_bits_remove (gdbarch, s390_addr_bits_remove);
3143       set_solib_svr4_fetch_link_map_offsets
3144         (gdbarch, svr4_ilp32_fetch_link_map_offsets);
3145
3146       set_xml_syscall_file_name (XML_SYSCALL_FILENAME_S390);
3147       break;
3148
3149     case ABI_LINUX_ZSERIES:
3150       tdep->gregset = &s390_gregset;
3151       tdep->sizeof_gregset = s390x_sizeof_gregset;
3152       tdep->fpregset = &s390_fpregset;
3153       tdep->sizeof_fpregset = s390_sizeof_fpregset;
3154
3155       set_gdbarch_long_bit (gdbarch, 64);
3156       set_gdbarch_long_long_bit (gdbarch, 64);
3157       set_gdbarch_ptr_bit (gdbarch, 64);
3158       set_solib_svr4_fetch_link_map_offsets
3159         (gdbarch, svr4_lp64_fetch_link_map_offsets);
3160       set_gdbarch_address_class_type_flags (gdbarch,
3161                                             s390_address_class_type_flags);
3162       set_gdbarch_address_class_type_flags_to_name (gdbarch,
3163                                                     s390_address_class_type_flags_to_name);
3164       set_gdbarch_address_class_name_to_type_flags (gdbarch,
3165                                                     s390_address_class_name_to_type_flags);
3166       set_xml_syscall_file_name (XML_SYSCALL_FILENAME_S390);
3167       break;
3168     }
3169
3170   set_gdbarch_print_insn (gdbarch, print_insn_s390);
3171
3172   set_gdbarch_skip_trampoline_code (gdbarch, find_solib_trampoline_target);
3173
3174   /* Enable TLS support.  */
3175   set_gdbarch_fetch_tls_load_module_address (gdbarch,
3176                                              svr4_fetch_objfile_link_map);
3177
3178   set_gdbarch_get_siginfo_type (gdbarch, linux_get_siginfo_type);
3179
3180   /* SystemTap functions.  */
3181   set_gdbarch_stap_register_prefixes (gdbarch, stap_register_prefixes);
3182   set_gdbarch_stap_register_indirection_prefixes (gdbarch,
3183                                           stap_register_indirection_prefixes);
3184   set_gdbarch_stap_register_indirection_suffixes (gdbarch,
3185                                           stap_register_indirection_suffixes);
3186   set_gdbarch_stap_is_single_operand (gdbarch, s390_stap_is_single_operand);
3187
3188   return gdbarch;
3189 }
3190
3191
3192 extern initialize_file_ftype _initialize_s390_tdep; /* -Wmissing-prototypes */
3193
3194 void
3195 _initialize_s390_tdep (void)
3196 {
3197   /* Hook us into the gdbarch mechanism.  */
3198   register_gdbarch_init (bfd_arch_s390, s390_gdbarch_init);
3199
3200   /* Initialize the GNU/Linux target descriptions.  */
3201   initialize_tdesc_s390_linux32 ();
3202   initialize_tdesc_s390_linux32v1 ();
3203   initialize_tdesc_s390_linux32v2 ();
3204   initialize_tdesc_s390_linux64 ();
3205   initialize_tdesc_s390_linux64v1 ();
3206   initialize_tdesc_s390_linux64v2 ();
3207   initialize_tdesc_s390_te_linux64 ();
3208   initialize_tdesc_s390x_linux64 ();
3209   initialize_tdesc_s390x_linux64v1 ();
3210   initialize_tdesc_s390x_linux64v2 ();
3211   initialize_tdesc_s390x_te_linux64 ();
3212 }