OSDN Git Service

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