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