add autom4te.cache to .cvsignore
[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 ... */
1235 func = frame_func_unwind (next_frame);
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{
8e645ae7
AC
1559 CORE_ADDR pc = frame_pc_unwind (next_frame);
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. */
1565 if (in_plt_section (pc, NULL)
1566 || s390_readinstruction (insn, pc) < 0)
1567 return &s390_stub_frame_unwind;
1568 return NULL;
a8c99f38 1569}
5769d3cd
AC
1570
1571
a8c99f38 1572/* Signal trampoline stack frames. */
5769d3cd 1573
a8c99f38
JB
1574struct s390_sigtramp_unwind_cache {
1575 CORE_ADDR frame_base;
1576 struct trad_frame_saved_reg *saved_regs;
1577};
5769d3cd 1578
a8c99f38
JB
1579static struct s390_sigtramp_unwind_cache *
1580s390_sigtramp_frame_unwind_cache (struct frame_info *next_frame,
1581 void **this_prologue_cache)
5769d3cd 1582{
a8c99f38
JB
1583 struct gdbarch *gdbarch = get_frame_arch (next_frame);
1584 int word_size = gdbarch_ptr_bit (gdbarch) / 8;
1585 struct s390_sigtramp_unwind_cache *info;
1586 ULONGEST this_sp, prev_sp;
1587 CORE_ADDR next_ra, next_cfa, sigreg_ptr;
1588 int i;
1589
1590 if (*this_prologue_cache)
1591 return *this_prologue_cache;
5769d3cd 1592
a8c99f38
JB
1593 info = FRAME_OBSTACK_ZALLOC (struct s390_sigtramp_unwind_cache);
1594 *this_prologue_cache = info;
1595 info->saved_regs = trad_frame_alloc_saved_regs (next_frame);
1596
1597 this_sp = frame_unwind_register_unsigned (next_frame, S390_SP_REGNUM);
1598 next_ra = frame_pc_unwind (next_frame);
1599 next_cfa = this_sp + 16*word_size + 32;
1600
1601 /* New-style RT frame:
1602 retcode + alignment (8 bytes)
1603 siginfo (128 bytes)
1604 ucontext (contains sigregs at offset 5 words) */
1605 if (next_ra == next_cfa)
1606 {
f0f63663 1607 sigreg_ptr = next_cfa + 8 + 128 + align_up (5*word_size, 8);
a8c99f38
JB
1608 }
1609
1610 /* Old-style RT frame and all non-RT frames:
1611 old signal mask (8 bytes)
1612 pointer to sigregs */
5769d3cd
AC
1613 else
1614 {
a8c99f38
JB
1615 sigreg_ptr = read_memory_unsigned_integer (next_cfa + 8, word_size);
1616 }
5769d3cd 1617
a8c99f38
JB
1618 /* The sigregs structure looks like this:
1619 long psw_mask;
1620 long psw_addr;
1621 long gprs[16];
1622 int acrs[16];
1623 int fpc;
1624 int __pad;
1625 double fprs[16]; */
5769d3cd 1626
a8c99f38
JB
1627 /* Let's ignore the PSW mask, it will not be restored anyway. */
1628 sigreg_ptr += word_size;
1629
1630 /* Next comes the PSW address. */
1631 info->saved_regs[S390_PC_REGNUM].addr = sigreg_ptr;
1632 sigreg_ptr += word_size;
1633
1634 /* Then the GPRs. */
1635 for (i = 0; i < 16; i++)
1636 {
1637 info->saved_regs[S390_R0_REGNUM + i].addr = sigreg_ptr;
1638 sigreg_ptr += word_size;
1639 }
1640
1641 /* Then the ACRs. */
1642 for (i = 0; i < 16; i++)
1643 {
1644 info->saved_regs[S390_A0_REGNUM + i].addr = sigreg_ptr;
1645 sigreg_ptr += 4;
5769d3cd 1646 }
5769d3cd 1647
a8c99f38
JB
1648 /* The floating-point control word. */
1649 info->saved_regs[S390_FPC_REGNUM].addr = sigreg_ptr;
1650 sigreg_ptr += 8;
5769d3cd 1651
a8c99f38
JB
1652 /* And finally the FPRs. */
1653 for (i = 0; i < 16; i++)
1654 {
1655 info->saved_regs[S390_F0_REGNUM + i].addr = sigreg_ptr;
1656 sigreg_ptr += 8;
1657 }
1658
1659 /* Restore the previous frame's SP. */
1660 prev_sp = read_memory_unsigned_integer (
1661 info->saved_regs[S390_SP_REGNUM].addr,
1662 word_size);
5769d3cd 1663
a8c99f38
JB
1664 /* Determine our frame base. */
1665 info->frame_base = prev_sp + 16*word_size + 32;
5769d3cd 1666
a8c99f38 1667 return info;
5769d3cd
AC
1668}
1669
a8c99f38
JB
1670static void
1671s390_sigtramp_frame_this_id (struct frame_info *next_frame,
1672 void **this_prologue_cache,
1673 struct frame_id *this_id)
5769d3cd 1674{
a8c99f38
JB
1675 struct s390_sigtramp_unwind_cache *info
1676 = s390_sigtramp_frame_unwind_cache (next_frame, this_prologue_cache);
1677 *this_id = frame_id_build (info->frame_base, frame_pc_unwind (next_frame));
5769d3cd
AC
1678}
1679
4c8287ac 1680static void
a8c99f38
JB
1681s390_sigtramp_frame_prev_register (struct frame_info *next_frame,
1682 void **this_prologue_cache,
1683 int regnum, int *optimizedp,
1684 enum lval_type *lvalp, CORE_ADDR *addrp,
f127898a 1685 int *realnump, gdb_byte *bufferp)
a8c99f38
JB
1686{
1687 struct s390_sigtramp_unwind_cache *info
1688 = s390_sigtramp_frame_unwind_cache (next_frame, this_prologue_cache);
1f67027d
AC
1689 trad_frame_get_prev_register (next_frame, info->saved_regs, regnum,
1690 optimizedp, lvalp, addrp, realnump, bufferp);
a8c99f38
JB
1691}
1692
1693static const struct frame_unwind s390_sigtramp_frame_unwind = {
1694 SIGTRAMP_FRAME,
1695 s390_sigtramp_frame_this_id,
1696 s390_sigtramp_frame_prev_register
1697};
1698
1699static const struct frame_unwind *
1700s390_sigtramp_frame_sniffer (struct frame_info *next_frame)
5769d3cd 1701{
a8c99f38
JB
1702 CORE_ADDR pc = frame_pc_unwind (next_frame);
1703 bfd_byte sigreturn[2];
4c8287ac 1704
359a9262 1705 if (read_memory_nobpt (pc, sigreturn, 2))
a8c99f38 1706 return NULL;
4c8287ac 1707
a8c99f38
JB
1708 if (sigreturn[0] != 0x0a /* svc */)
1709 return NULL;
5769d3cd 1710
a8c99f38
JB
1711 if (sigreturn[1] != 119 /* sigreturn */
1712 && sigreturn[1] != 173 /* rt_sigreturn */)
1713 return NULL;
1714
1715 return &s390_sigtramp_frame_unwind;
5769d3cd
AC
1716}
1717
4c8287ac 1718
a8c99f38
JB
1719/* Frame base handling. */
1720
1721static CORE_ADDR
1722s390_frame_base_address (struct frame_info *next_frame, void **this_cache)
4c8287ac 1723{
a8c99f38
JB
1724 struct s390_unwind_cache *info
1725 = s390_frame_unwind_cache (next_frame, this_cache);
1726 return info->frame_base;
1727}
1728
1729static CORE_ADDR
1730s390_local_base_address (struct frame_info *next_frame, void **this_cache)
1731{
1732 struct s390_unwind_cache *info
1733 = s390_frame_unwind_cache (next_frame, this_cache);
1734 return info->local_base;
1735}
1736
1737static const struct frame_base s390_frame_base = {
1738 &s390_frame_unwind,
1739 s390_frame_base_address,
1740 s390_local_base_address,
1741 s390_local_base_address
1742};
1743
1744static CORE_ADDR
1745s390_unwind_pc (struct gdbarch *gdbarch, struct frame_info *next_frame)
1746{
1747 ULONGEST pc;
1748 pc = frame_unwind_register_unsigned (next_frame, S390_PC_REGNUM);
1749 return gdbarch_addr_bits_remove (gdbarch, pc);
1750}
1751
1752static CORE_ADDR
1753s390_unwind_sp (struct gdbarch *gdbarch, struct frame_info *next_frame)
1754{
1755 ULONGEST sp;
1756 sp = frame_unwind_register_unsigned (next_frame, S390_SP_REGNUM);
1757 return gdbarch_addr_bits_remove (gdbarch, sp);
4c8287ac
JB
1758}
1759
1760
a431654a
AC
1761/* DWARF-2 frame support. */
1762
1763static void
1764s390_dwarf2_frame_init_reg (struct gdbarch *gdbarch, int regnum,
aff37fc1
DM
1765 struct dwarf2_frame_state_reg *reg,
1766 struct frame_info *next_frame)
a431654a
AC
1767{
1768 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1769
1770 switch (tdep->abi)
1771 {
1772 case ABI_LINUX_S390:
1773 /* Call-saved registers. */
1774 if ((regnum >= S390_R6_REGNUM && regnum <= S390_R15_REGNUM)
1775 || regnum == S390_F4_REGNUM
1776 || regnum == S390_F6_REGNUM)
1777 reg->how = DWARF2_FRAME_REG_SAME_VALUE;
1778
1779 /* Call-clobbered registers. */
1780 else if ((regnum >= S390_R0_REGNUM && regnum <= S390_R5_REGNUM)
1781 || (regnum >= S390_F0_REGNUM && regnum <= S390_F15_REGNUM
1782 && regnum != S390_F4_REGNUM && regnum != S390_F6_REGNUM))
1783 reg->how = DWARF2_FRAME_REG_UNDEFINED;
1784
1785 /* The return address column. */
1786 else if (regnum == S390_PC_REGNUM)
1787 reg->how = DWARF2_FRAME_REG_RA;
1788 break;
1789
1790 case ABI_LINUX_ZSERIES:
1791 /* Call-saved registers. */
1792 if ((regnum >= S390_R6_REGNUM && regnum <= S390_R15_REGNUM)
1793 || (regnum >= S390_F8_REGNUM && regnum <= S390_F15_REGNUM))
1794 reg->how = DWARF2_FRAME_REG_SAME_VALUE;
1795
1796 /* Call-clobbered registers. */
1797 else if ((regnum >= S390_R0_REGNUM && regnum <= S390_R5_REGNUM)
1798 || (regnum >= S390_F0_REGNUM && regnum <= S390_F7_REGNUM))
1799 reg->how = DWARF2_FRAME_REG_UNDEFINED;
1800
1801 /* The return address column. */
1802 else if (regnum == S390_PC_REGNUM)
1803 reg->how = DWARF2_FRAME_REG_RA;
1804 break;
1805 }
1806}
1807
1808
b0cf273e
JB
1809/* Dummy function calls. */
1810
78f8b424
JB
1811/* Return non-zero if TYPE is an integer-like type, zero otherwise.
1812 "Integer-like" types are those that should be passed the way
1813 integers are: integers, enums, ranges, characters, and booleans. */
1814static int
1815is_integer_like (struct type *type)
1816{
1817 enum type_code code = TYPE_CODE (type);
1818
1819 return (code == TYPE_CODE_INT
1820 || code == TYPE_CODE_ENUM
1821 || code == TYPE_CODE_RANGE
1822 || code == TYPE_CODE_CHAR
1823 || code == TYPE_CODE_BOOL);
1824}
1825
78f8b424
JB
1826/* Return non-zero if TYPE is a pointer-like type, zero otherwise.
1827 "Pointer-like" types are those that should be passed the way
1828 pointers are: pointers and references. */
1829static int
1830is_pointer_like (struct type *type)
1831{
1832 enum type_code code = TYPE_CODE (type);
1833
1834 return (code == TYPE_CODE_PTR
1835 || code == TYPE_CODE_REF);
1836}
1837
1838
20a940cc
JB
1839/* Return non-zero if TYPE is a `float singleton' or `double
1840 singleton', zero otherwise.
1841
1842 A `T singleton' is a struct type with one member, whose type is
1843 either T or a `T singleton'. So, the following are all float
1844 singletons:
1845
1846 struct { float x };
1847 struct { struct { float x; } x; };
1848 struct { struct { struct { float x; } x; } x; };
1849
1850 ... and so on.
1851
b0cf273e
JB
1852 All such structures are passed as if they were floats or doubles,
1853 as the (revised) ABI says. */
20a940cc
JB
1854static int
1855is_float_singleton (struct type *type)
1856{
b0cf273e
JB
1857 if (TYPE_CODE (type) == TYPE_CODE_STRUCT && TYPE_NFIELDS (type) == 1)
1858 {
1859 struct type *singleton_type = TYPE_FIELD_TYPE (type, 0);
1860 CHECK_TYPEDEF (singleton_type);
1861
1862 return (TYPE_CODE (singleton_type) == TYPE_CODE_FLT
1863 || is_float_singleton (singleton_type));
1864 }
1865
1866 return 0;
20a940cc
JB
1867}
1868
1869
1870/* Return non-zero if TYPE is a struct-like type, zero otherwise.
1871 "Struct-like" types are those that should be passed as structs are:
1872 structs and unions.
1873
1874 As an odd quirk, not mentioned in the ABI, GCC passes float and
1875 double singletons as if they were a plain float, double, etc. (The
1876 corresponding union types are handled normally.) So we exclude
1877 those types here. *shrug* */
1878static int
1879is_struct_like (struct type *type)
1880{
1881 enum type_code code = TYPE_CODE (type);
1882
1883 return (code == TYPE_CODE_UNION
1884 || (code == TYPE_CODE_STRUCT && ! is_float_singleton (type)));
1885}
1886
1887
1888/* Return non-zero if TYPE is a float-like type, zero otherwise.
1889 "Float-like" types are those that should be passed as
1890 floating-point values are.
1891
1892 You'd think this would just be floats, doubles, long doubles, etc.
1893 But as an odd quirk, not mentioned in the ABI, GCC passes float and
1894 double singletons as if they were a plain float, double, etc. (The
4d819d0e 1895 corresponding union types are handled normally.) So we include
20a940cc
JB
1896 those types here. *shrug* */
1897static int
1898is_float_like (struct type *type)
1899{
1900 return (TYPE_CODE (type) == TYPE_CODE_FLT
1901 || is_float_singleton (type));
1902}
1903
1904
78f8b424 1905static int
b0cf273e 1906is_power_of_two (unsigned int n)
78f8b424 1907{
b0cf273e 1908 return ((n & (n - 1)) == 0);
78f8b424
JB
1909}
1910
b0cf273e
JB
1911/* Return non-zero if TYPE should be passed as a pointer to a copy,
1912 zero otherwise. */
4d819d0e 1913static int
b0cf273e 1914s390_function_arg_pass_by_reference (struct type *type)
4d819d0e
JB
1915{
1916 unsigned length = TYPE_LENGTH (type);
b0cf273e
JB
1917 if (length > 8)
1918 return 1;
4d819d0e 1919
b0cf273e
JB
1920 /* FIXME: All complex and vector types are also returned by reference. */
1921 return is_struct_like (type) && !is_power_of_two (length);
4d819d0e
JB
1922}
1923
b0cf273e
JB
1924/* Return non-zero if TYPE should be passed in a float register
1925 if possible. */
78f8b424 1926static int
b0cf273e 1927s390_function_arg_float (struct type *type)
78f8b424 1928{
78f8b424 1929 unsigned length = TYPE_LENGTH (type);
b0cf273e
JB
1930 if (length > 8)
1931 return 0;
78f8b424 1932
b0cf273e 1933 return is_float_like (type);
4d819d0e
JB
1934}
1935
b0cf273e
JB
1936/* Return non-zero if TYPE should be passed in an integer register
1937 (or a pair of integer registers) if possible. */
78f8b424 1938static int
b0cf273e 1939s390_function_arg_integer (struct type *type)
78f8b424 1940{
78f8b424 1941 unsigned length = TYPE_LENGTH (type);
b0cf273e
JB
1942 if (length > 8)
1943 return 0;
78f8b424 1944
b0cf273e
JB
1945 return is_integer_like (type)
1946 || is_pointer_like (type)
1947 || (is_struct_like (type) && is_power_of_two (length));
78f8b424
JB
1948}
1949
78f8b424
JB
1950/* Return ARG, a `SIMPLE_ARG', sign-extended or zero-extended to a full
1951 word as required for the ABI. */
1952static LONGEST
1953extend_simple_arg (struct value *arg)
1954{
4991999e 1955 struct type *type = value_type (arg);
78f8b424
JB
1956
1957 /* Even structs get passed in the least significant bits of the
1958 register / memory word. It's not really right to extract them as
1959 an integer, but it does take care of the extension. */
1960 if (TYPE_UNSIGNED (type))
0fd88904 1961 return extract_unsigned_integer (value_contents (arg),
78f8b424
JB
1962 TYPE_LENGTH (type));
1963 else
0fd88904 1964 return extract_signed_integer (value_contents (arg),
78f8b424
JB
1965 TYPE_LENGTH (type));
1966}
1967
1968
78f8b424
JB
1969/* Return the alignment required by TYPE. */
1970static int
1971alignment_of (struct type *type)
1972{
1973 int alignment;
1974
1975 if (is_integer_like (type)
1976 || is_pointer_like (type)
1977 || TYPE_CODE (type) == TYPE_CODE_FLT)
1978 alignment = TYPE_LENGTH (type);
1979 else if (TYPE_CODE (type) == TYPE_CODE_STRUCT
1980 || TYPE_CODE (type) == TYPE_CODE_UNION)
1981 {
1982 int i;
1983
1984 alignment = 1;
1985 for (i = 0; i < TYPE_NFIELDS (type); i++)
1986 {
1987 int field_alignment = alignment_of (TYPE_FIELD_TYPE (type, i));
1988
1989 if (field_alignment > alignment)
1990 alignment = field_alignment;
1991 }
1992 }
1993 else
1994 alignment = 1;
1995
1996 /* Check that everything we ever return is a power of two. Lots of
1997 code doesn't want to deal with aligning things to arbitrary
1998 boundaries. */
1999 gdb_assert ((alignment & (alignment - 1)) == 0);
2000
2001 return alignment;
2002}
2003
2004
2005/* Put the actual parameter values pointed to by ARGS[0..NARGS-1] in
ca557f44
AC
2006 place to be passed to a function, as specified by the "GNU/Linux
2007 for S/390 ELF Application Binary Interface Supplement".
78f8b424
JB
2008
2009 SP is the current stack pointer. We must put arguments, links,
2010 padding, etc. whereever they belong, and return the new stack
2011 pointer value.
2012
2013 If STRUCT_RETURN is non-zero, then the function we're calling is
2014 going to return a structure by value; STRUCT_ADDR is the address of
2015 a block we've allocated for it on the stack.
2016
2017 Our caller has taken care of any type promotions needed to satisfy
2018 prototypes or the old K&R argument-passing rules. */
a78f21af 2019static CORE_ADDR
7d9b040b 2020s390_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
b0cf273e
JB
2021 struct regcache *regcache, CORE_ADDR bp_addr,
2022 int nargs, struct value **args, CORE_ADDR sp,
2023 int struct_return, CORE_ADDR struct_addr)
5769d3cd 2024{
b0cf273e
JB
2025 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
2026 int word_size = gdbarch_ptr_bit (gdbarch) / 8;
2027 ULONGEST orig_sp;
78f8b424 2028 int i;
5769d3cd 2029
78f8b424
JB
2030 /* If the i'th argument is passed as a reference to a copy, then
2031 copy_addr[i] is the address of the copy we made. */
2032 CORE_ADDR *copy_addr = alloca (nargs * sizeof (CORE_ADDR));
5769d3cd 2033
78f8b424 2034 /* Build the reference-to-copy area. */
78f8b424
JB
2035 for (i = 0; i < nargs; i++)
2036 {
2037 struct value *arg = args[i];
4991999e 2038 struct type *type = value_type (arg);
78f8b424 2039 unsigned length = TYPE_LENGTH (type);
5769d3cd 2040
b0cf273e 2041 if (s390_function_arg_pass_by_reference (type))
01c464e9 2042 {
78f8b424 2043 sp -= length;
5b03f266 2044 sp = align_down (sp, alignment_of (type));
0fd88904 2045 write_memory (sp, value_contents (arg), length);
78f8b424 2046 copy_addr[i] = sp;
01c464e9 2047 }
5769d3cd 2048 }
5769d3cd 2049
78f8b424
JB
2050 /* Reserve space for the parameter area. As a conservative
2051 simplification, we assume that everything will be passed on the
b0cf273e
JB
2052 stack. Since every argument larger than 8 bytes will be
2053 passed by reference, we use this simple upper bound. */
2054 sp -= nargs * 8;
78f8b424 2055
78f8b424
JB
2056 /* After all that, make sure it's still aligned on an eight-byte
2057 boundary. */
5b03f266 2058 sp = align_down (sp, 8);
78f8b424
JB
2059
2060 /* Finally, place the actual parameters, working from SP towards
2061 higher addresses. The code above is supposed to reserve enough
2062 space for this. */
2063 {
2064 int fr = 0;
2065 int gr = 2;
2066 CORE_ADDR starg = sp;
2067
b0cf273e 2068 /* A struct is returned using general register 2. */
4d819d0e 2069 if (struct_return)
b0cf273e
JB
2070 {
2071 regcache_cooked_write_unsigned (regcache, S390_R0_REGNUM + gr,
2072 struct_addr);
2073 gr++;
2074 }
4d819d0e 2075
78f8b424
JB
2076 for (i = 0; i < nargs; i++)
2077 {
2078 struct value *arg = args[i];
4991999e 2079 struct type *type = value_type (arg);
b0cf273e
JB
2080 unsigned length = TYPE_LENGTH (type);
2081
2082 if (s390_function_arg_pass_by_reference (type))
2083 {
2084 if (gr <= 6)
2085 {
2086 regcache_cooked_write_unsigned (regcache, S390_R0_REGNUM + gr,
2087 copy_addr[i]);
2088 gr++;
2089 }
2090 else
2091 {
2092 write_memory_unsigned_integer (starg, word_size, copy_addr[i]);
2093 starg += word_size;
2094 }
2095 }
2096 else if (s390_function_arg_float (type))
2097 {
2098 /* The GNU/Linux for S/390 ABI uses FPRs 0 and 2 to pass arguments,
2099 the GNU/Linux for zSeries ABI uses 0, 2, 4, and 6. */
2100 if (fr <= (tdep->abi == ABI_LINUX_S390 ? 2 : 6))
2101 {
2102 /* When we store a single-precision value in an FP register,
2103 it occupies the leftmost bits. */
2104 regcache_cooked_write_part (regcache, S390_F0_REGNUM + fr,
0fd88904 2105 0, length, value_contents (arg));
b0cf273e
JB
2106 fr += 2;
2107 }
2108 else
2109 {
2110 /* When we store a single-precision value in a stack slot,
2111 it occupies the rightmost bits. */
2112 starg = align_up (starg + length, word_size);
0fd88904 2113 write_memory (starg - length, value_contents (arg), length);
b0cf273e
JB
2114 }
2115 }
2116 else if (s390_function_arg_integer (type) && length <= word_size)
2117 {
2118 if (gr <= 6)
2119 {
2120 /* Integer arguments are always extended to word size. */
2121 regcache_cooked_write_signed (regcache, S390_R0_REGNUM + gr,
2122 extend_simple_arg (arg));
2123 gr++;
2124 }
2125 else
2126 {
2127 /* Integer arguments are always extended to word size. */
2128 write_memory_signed_integer (starg, word_size,
2129 extend_simple_arg (arg));
2130 starg += word_size;
2131 }
2132 }
2133 else if (s390_function_arg_integer (type) && length == 2*word_size)
2134 {
2135 if (gr <= 5)
2136 {
2137 regcache_cooked_write (regcache, S390_R0_REGNUM + gr,
0fd88904 2138 value_contents (arg));
b0cf273e 2139 regcache_cooked_write (regcache, S390_R0_REGNUM + gr + 1,
0fd88904 2140 value_contents (arg) + word_size);
b0cf273e
JB
2141 gr += 2;
2142 }
2143 else
2144 {
2145 /* If we skipped r6 because we couldn't fit a DOUBLE_ARG
2146 in it, then don't go back and use it again later. */
2147 gr = 7;
2148
0fd88904 2149 write_memory (starg, value_contents (arg), length);
b0cf273e
JB
2150 starg += length;
2151 }
2152 }
2153 else
e2e0b3e5 2154 internal_error (__FILE__, __LINE__, _("unknown argument type"));
78f8b424
JB
2155 }
2156 }
2157
2158 /* Allocate the standard frame areas: the register save area, the
2159 word reserved for the compiler (which seems kind of meaningless),
2160 and the back chain pointer. */
b0cf273e 2161 sp -= 16*word_size + 32;
78f8b424 2162
b0cf273e
JB
2163 /* Store return address. */
2164 regcache_cooked_write_unsigned (regcache, S390_RETADDR_REGNUM, bp_addr);
2165
2166 /* Store updated stack pointer. */
2167 regcache_cooked_write_unsigned (regcache, S390_SP_REGNUM, sp);
78f8b424 2168
a8c99f38 2169 /* We need to return the 'stack part' of the frame ID,
121d8485
UW
2170 which is actually the top of the register save area. */
2171 return sp + 16*word_size + 32;
5769d3cd
AC
2172}
2173
b0cf273e
JB
2174/* Assuming NEXT_FRAME->prev is a dummy, return the frame ID of that
2175 dummy frame. The frame ID's base needs to match the TOS value
2176 returned by push_dummy_call, and the PC match the dummy frame's
2177 breakpoint. */
2178static struct frame_id
2179s390_unwind_dummy_id (struct gdbarch *gdbarch, struct frame_info *next_frame)
2180{
a8c99f38 2181 int word_size = gdbarch_ptr_bit (gdbarch) / 8;
121d8485 2182 CORE_ADDR sp = s390_unwind_sp (gdbarch, next_frame);
a8c99f38 2183
121d8485 2184 return frame_id_build (sp + 16*word_size + 32,
a8c99f38 2185 frame_pc_unwind (next_frame));
b0cf273e 2186}
c8f9d51c 2187
4074e13c
JB
2188static CORE_ADDR
2189s390_frame_align (struct gdbarch *gdbarch, CORE_ADDR addr)
2190{
2191 /* Both the 32- and 64-bit ABI's say that the stack pointer should
2192 always be aligned on an eight-byte boundary. */
2193 return (addr & -8);
2194}
2195
2196
b0cf273e
JB
2197/* Function return value access. */
2198
2199static enum return_value_convention
2200s390_return_value_convention (struct gdbarch *gdbarch, struct type *type)
c8f9d51c 2201{
b0cf273e
JB
2202 int length = TYPE_LENGTH (type);
2203 if (length > 8)
2204 return RETURN_VALUE_STRUCT_CONVENTION;
2205
2206 switch (TYPE_CODE (type))
2207 {
2208 case TYPE_CODE_STRUCT:
2209 case TYPE_CODE_UNION:
2210 case TYPE_CODE_ARRAY:
2211 return RETURN_VALUE_STRUCT_CONVENTION;
c8f9d51c 2212
b0cf273e
JB
2213 default:
2214 return RETURN_VALUE_REGISTER_CONVENTION;
2215 }
c8f9d51c
JB
2216}
2217
b0cf273e
JB
2218static enum return_value_convention
2219s390_return_value (struct gdbarch *gdbarch, struct type *type,
2e82d168
UW
2220 struct regcache *regcache, gdb_byte *out,
2221 const gdb_byte *in)
5769d3cd 2222{
b0cf273e
JB
2223 int word_size = gdbarch_ptr_bit (gdbarch) / 8;
2224 int length = TYPE_LENGTH (type);
2225 enum return_value_convention rvc =
2226 s390_return_value_convention (gdbarch, type);
2227 if (in)
2228 {
2229 switch (rvc)
2230 {
2231 case RETURN_VALUE_REGISTER_CONVENTION:
2232 if (TYPE_CODE (type) == TYPE_CODE_FLT)
2233 {
2234 /* When we store a single-precision value in an FP register,
2235 it occupies the leftmost bits. */
2236 regcache_cooked_write_part (regcache, S390_F0_REGNUM,
2237 0, length, in);
2238 }
2239 else if (length <= word_size)
2240 {
2241 /* Integer arguments are always extended to word size. */
2242 if (TYPE_UNSIGNED (type))
2243 regcache_cooked_write_unsigned (regcache, S390_R2_REGNUM,
2244 extract_unsigned_integer (in, length));
2245 else
2246 regcache_cooked_write_signed (regcache, S390_R2_REGNUM,
2247 extract_signed_integer (in, length));
2248 }
2249 else if (length == 2*word_size)
2250 {
2251 regcache_cooked_write (regcache, S390_R2_REGNUM, in);
43af2100 2252 regcache_cooked_write (regcache, S390_R3_REGNUM, in + word_size);
b0cf273e
JB
2253 }
2254 else
e2e0b3e5 2255 internal_error (__FILE__, __LINE__, _("invalid return type"));
b0cf273e
JB
2256 break;
2257
2258 case RETURN_VALUE_STRUCT_CONVENTION:
8a3fe4f8 2259 error (_("Cannot set function return value."));
b0cf273e
JB
2260 break;
2261 }
2262 }
2263 else if (out)
2264 {
2265 switch (rvc)
2266 {
2267 case RETURN_VALUE_REGISTER_CONVENTION:
2268 if (TYPE_CODE (type) == TYPE_CODE_FLT)
2269 {
2270 /* When we store a single-precision value in an FP register,
2271 it occupies the leftmost bits. */
2272 regcache_cooked_read_part (regcache, S390_F0_REGNUM,
2273 0, length, out);
2274 }
2275 else if (length <= word_size)
2276 {
2277 /* Integer arguments occupy the rightmost bits. */
2278 regcache_cooked_read_part (regcache, S390_R2_REGNUM,
2279 word_size - length, length, out);
2280 }
2281 else if (length == 2*word_size)
2282 {
2283 regcache_cooked_read (regcache, S390_R2_REGNUM, out);
43af2100 2284 regcache_cooked_read (regcache, S390_R3_REGNUM, out + word_size);
b0cf273e
JB
2285 }
2286 else
e2e0b3e5 2287 internal_error (__FILE__, __LINE__, _("invalid return type"));
b0cf273e 2288 break;
5769d3cd 2289
b0cf273e 2290 case RETURN_VALUE_STRUCT_CONVENTION:
8a3fe4f8 2291 error (_("Function return value unknown."));
b0cf273e
JB
2292 break;
2293 }
2294 }
2295
2296 return rvc;
2297}
5769d3cd
AC
2298
2299
a8c99f38
JB
2300/* Breakpoints. */
2301
43af2100 2302static const gdb_byte *
5769d3cd
AC
2303s390_breakpoint_from_pc (CORE_ADDR *pcptr, int *lenptr)
2304{
43af2100 2305 static const gdb_byte breakpoint[] = { 0x0, 0x1 };
5769d3cd
AC
2306
2307 *lenptr = sizeof (breakpoint);
2308 return breakpoint;
2309}
2310
5769d3cd 2311
a8c99f38 2312/* Address handling. */
5769d3cd
AC
2313
2314static CORE_ADDR
2315s390_addr_bits_remove (CORE_ADDR addr)
2316{
a8c99f38 2317 return addr & 0x7fffffff;
5769d3cd
AC
2318}
2319
ffc65945
KB
2320static int
2321s390_address_class_type_flags (int byte_size, int dwarf2_addr_class)
2322{
2323 if (byte_size == 4)
2324 return TYPE_FLAG_ADDRESS_CLASS_1;
2325 else
2326 return 0;
2327}
2328
2329static const char *
2330s390_address_class_type_flags_to_name (struct gdbarch *gdbarch, int type_flags)
2331{
2332 if (type_flags & TYPE_FLAG_ADDRESS_CLASS_1)
2333 return "mode32";
2334 else
2335 return NULL;
2336}
2337
a78f21af 2338static int
ffc65945
KB
2339s390_address_class_name_to_type_flags (struct gdbarch *gdbarch, const char *name,
2340 int *type_flags_ptr)
2341{
2342 if (strcmp (name, "mode32") == 0)
2343 {
2344 *type_flags_ptr = TYPE_FLAG_ADDRESS_CLASS_1;
2345 return 1;
2346 }
2347 else
2348 return 0;
2349}
2350
a8c99f38
JB
2351/* Set up gdbarch struct. */
2352
a78f21af 2353static struct gdbarch *
5769d3cd
AC
2354s390_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
2355{
5769d3cd
AC
2356 struct gdbarch *gdbarch;
2357 struct gdbarch_tdep *tdep;
5769d3cd
AC
2358
2359 /* First see if there is already a gdbarch that can satisfy the request. */
2360 arches = gdbarch_list_lookup_by_info (arches, &info);
2361 if (arches != NULL)
2362 return arches->gdbarch;
2363
2364 /* None found: is the request for a s390 architecture? */
2365 if (info.bfd_arch_info->arch != bfd_arch_s390)
2366 return NULL; /* No; then it's not for us. */
2367
2368 /* Yes: create a new gdbarch for the specified machine type. */
d0f54f9d
JB
2369 tdep = XCALLOC (1, struct gdbarch_tdep);
2370 gdbarch = gdbarch_alloc (&info, tdep);
5769d3cd
AC
2371
2372 set_gdbarch_believe_pcc_promotion (gdbarch, 0);
4e409299 2373 set_gdbarch_char_signed (gdbarch, 0);
5769d3cd 2374
aaab4dba
AC
2375 /* Amount PC must be decremented by after a breakpoint. This is
2376 often the number of bytes returned by BREAKPOINT_FROM_PC but not
2377 always. */
5769d3cd 2378 set_gdbarch_decr_pc_after_break (gdbarch, 2);
5769d3cd
AC
2379 /* Stack grows downward. */
2380 set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
5769d3cd
AC
2381 set_gdbarch_breakpoint_from_pc (gdbarch, s390_breakpoint_from_pc);
2382 set_gdbarch_skip_prologue (gdbarch, s390_skip_prologue);
d0f54f9d 2383 set_gdbarch_in_function_epilogue_p (gdbarch, s390_in_function_epilogue_p);
a8c99f38 2384
5769d3cd
AC
2385 set_gdbarch_pc_regnum (gdbarch, S390_PC_REGNUM);
2386 set_gdbarch_sp_regnum (gdbarch, S390_SP_REGNUM);
d0f54f9d 2387 set_gdbarch_fp0_regnum (gdbarch, S390_F0_REGNUM);
5769d3cd 2388 set_gdbarch_num_regs (gdbarch, S390_NUM_REGS);
d0f54f9d 2389 set_gdbarch_num_pseudo_regs (gdbarch, S390_NUM_PSEUDO_REGS);
5769d3cd 2390 set_gdbarch_register_name (gdbarch, s390_register_name);
d0f54f9d
JB
2391 set_gdbarch_register_type (gdbarch, s390_register_type);
2392 set_gdbarch_stab_reg_to_regnum (gdbarch, s390_dwarf_reg_to_regnum);
2393 set_gdbarch_dwarf_reg_to_regnum (gdbarch, s390_dwarf_reg_to_regnum);
2394 set_gdbarch_dwarf2_reg_to_regnum (gdbarch, s390_dwarf_reg_to_regnum);
9acbedc0 2395 set_gdbarch_value_from_register (gdbarch, s390_value_from_register);
d0f54f9d
JB
2396 set_gdbarch_register_reggroup_p (gdbarch, s390_register_reggroup_p);
2397 set_gdbarch_regset_from_core_section (gdbarch,
2398 s390_regset_from_core_section);
5769d3cd 2399
b0cf273e
JB
2400 /* Inferior function calls. */
2401 set_gdbarch_push_dummy_call (gdbarch, s390_push_dummy_call);
2402 set_gdbarch_unwind_dummy_id (gdbarch, s390_unwind_dummy_id);
4074e13c 2403 set_gdbarch_frame_align (gdbarch, s390_frame_align);
b0cf273e 2404 set_gdbarch_return_value (gdbarch, s390_return_value);
5769d3cd 2405
a8c99f38 2406 /* Frame handling. */
a431654a
AC
2407 dwarf2_frame_set_init_reg (gdbarch, s390_dwarf2_frame_init_reg);
2408 frame_unwind_append_sniffer (gdbarch, dwarf2_frame_sniffer);
2409 frame_base_append_sniffer (gdbarch, dwarf2_frame_base_sniffer);
8e645ae7 2410 frame_unwind_append_sniffer (gdbarch, s390_stub_frame_sniffer);
a8c99f38
JB
2411 frame_unwind_append_sniffer (gdbarch, s390_sigtramp_frame_sniffer);
2412 frame_unwind_append_sniffer (gdbarch, s390_frame_sniffer);
2413 frame_base_set_default (gdbarch, &s390_frame_base);
2414 set_gdbarch_unwind_pc (gdbarch, s390_unwind_pc);
2415 set_gdbarch_unwind_sp (gdbarch, s390_unwind_sp);
2416
5769d3cd
AC
2417 switch (info.bfd_arch_info->mach)
2418 {
b8b8b047 2419 case bfd_mach_s390_31:
b0cf273e
JB
2420 tdep->abi = ABI_LINUX_S390;
2421
d0f54f9d
JB
2422 tdep->gregset = &s390_gregset;
2423 tdep->sizeof_gregset = s390_sizeof_gregset;
2424 tdep->fpregset = &s390_fpregset;
2425 tdep->sizeof_fpregset = s390_sizeof_fpregset;
5769d3cd
AC
2426
2427 set_gdbarch_addr_bits_remove (gdbarch, s390_addr_bits_remove);
d0f54f9d
JB
2428 set_gdbarch_pseudo_register_read (gdbarch, s390_pseudo_register_read);
2429 set_gdbarch_pseudo_register_write (gdbarch, s390_pseudo_register_write);
76a9d10f
MK
2430 set_solib_svr4_fetch_link_map_offsets
2431 (gdbarch, svr4_ilp32_fetch_link_map_offsets);
9cbd5950 2432
5769d3cd 2433 break;
b8b8b047 2434 case bfd_mach_s390_64:
b0cf273e
JB
2435 tdep->abi = ABI_LINUX_ZSERIES;
2436
d0f54f9d
JB
2437 tdep->gregset = &s390x_gregset;
2438 tdep->sizeof_gregset = s390x_sizeof_gregset;
2439 tdep->fpregset = &s390_fpregset;
2440 tdep->sizeof_fpregset = s390_sizeof_fpregset;
5769d3cd
AC
2441
2442 set_gdbarch_long_bit (gdbarch, 64);
2443 set_gdbarch_long_long_bit (gdbarch, 64);
2444 set_gdbarch_ptr_bit (gdbarch, 64);
d0f54f9d
JB
2445 set_gdbarch_pseudo_register_read (gdbarch, s390x_pseudo_register_read);
2446 set_gdbarch_pseudo_register_write (gdbarch, s390x_pseudo_register_write);
76a9d10f
MK
2447 set_solib_svr4_fetch_link_map_offsets
2448 (gdbarch, svr4_lp64_fetch_link_map_offsets);
ffc65945
KB
2449 set_gdbarch_address_class_type_flags (gdbarch,
2450 s390_address_class_type_flags);
2451 set_gdbarch_address_class_type_flags_to_name (gdbarch,
2452 s390_address_class_type_flags_to_name);
2453 set_gdbarch_address_class_name_to_type_flags (gdbarch,
2454 s390_address_class_name_to_type_flags);
5769d3cd
AC
2455 break;
2456 }
2457
36482093
AC
2458 set_gdbarch_print_insn (gdbarch, print_insn_s390);
2459
982e9687
UW
2460 set_gdbarch_skip_trampoline_code (gdbarch, find_solib_trampoline_target);
2461
b2756930
KB
2462 /* Enable TLS support. */
2463 set_gdbarch_fetch_tls_load_module_address (gdbarch,
2464 svr4_fetch_objfile_link_map);
2465
5769d3cd
AC
2466 return gdbarch;
2467}
2468
2469
2470
a78f21af
AC
2471extern initialize_file_ftype _initialize_s390_tdep; /* -Wmissing-prototypes */
2472
5769d3cd 2473void
5ae5f592 2474_initialize_s390_tdep (void)
5769d3cd
AC
2475{
2476
2477 /* Hook us into the gdbarch mechanism. */
2478 register_gdbarch_init (bfd_arch_s390, s390_gdbarch_init);
5769d3cd 2479}
This page took 0.865892 seconds and 4 git commands to generate.