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