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