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