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