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