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