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