* s390-tdep.c (s390_get_frame_info): Don't used fextra_info to set
[binutils-gdb.git] / gdb / s390-tdep.c
1 /* Target-dependent code for GDB, the GNU debugger.
2 Copyright 2001 Free Software Foundation, Inc.
3 Contributed by D.J. Barrow (djbarrow@de.ibm.com,barrow_dj@yahoo.com)
4 for IBM Deutschland Entwicklung GmbH, IBM Corporation.
5
6 This file is part of GDB.
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
21 02111-1307, USA. */
22
23 #define S390_TDEP /* for special macros in tm-s390.h */
24 #include <defs.h>
25 #include "arch-utils.h"
26 #include "frame.h"
27 #include "inferior.h"
28 #include "symtab.h"
29 #include "target.h"
30 #include "gdbcore.h"
31 #include "gdbcmd.h"
32 #include "symfile.h"
33 #include "objfiles.h"
34 #include "tm.h"
35 #include "../bfd/bfd.h"
36 #include "floatformat.h"
37 #include "regcache.h"
38 #include "value.h"
39 #include "gdb_assert.h"
40
41
42
43
44 /* Number of bytes of storage in the actual machine representation
45 for register N.
46 Note that the unsigned cast here forces the result of the
47 subtraction to very high positive values if N < S390_FP0_REGNUM */
48 int
49 s390_register_raw_size (int reg_nr)
50 {
51 return ((unsigned) reg_nr - S390_FP0_REGNUM) <
52 S390_NUM_FPRS ? S390_FPR_SIZE : 4;
53 }
54
55 int
56 s390x_register_raw_size (int reg_nr)
57 {
58 return (reg_nr == S390_FPC_REGNUM)
59 || (reg_nr >= S390_FIRST_ACR && reg_nr <= S390_LAST_ACR) ? 4 : 8;
60 }
61
62 int
63 s390_cannot_fetch_register (int regno)
64 {
65 return (regno >= S390_FIRST_CR && regno < (S390_FIRST_CR + 9)) ||
66 (regno >= (S390_FIRST_CR + 12) && regno <= S390_LAST_CR);
67 }
68
69 int
70 s390_register_byte (int reg_nr)
71 {
72 if (reg_nr <= S390_GP_LAST_REGNUM)
73 return reg_nr * S390_GPR_SIZE;
74 if (reg_nr <= S390_LAST_ACR)
75 return S390_ACR0_OFFSET + (((reg_nr) - S390_FIRST_ACR) * S390_ACR_SIZE);
76 if (reg_nr <= S390_LAST_CR)
77 return S390_CR0_OFFSET + (((reg_nr) - S390_FIRST_CR) * S390_CR_SIZE);
78 if (reg_nr == S390_FPC_REGNUM)
79 return S390_FPC_OFFSET;
80 else
81 return S390_FP0_OFFSET + (((reg_nr) - S390_FP0_REGNUM) * S390_FPR_SIZE);
82 }
83
84 #ifndef GDBSERVER
85 #define S390_MAX_INSTR_SIZE (6)
86 #define S390_SYSCALL_OPCODE (0x0a)
87 #define S390_SYSCALL_SIZE (2)
88 #define S390_SIGCONTEXT_SREGS_OFFSET (8)
89 #define S390X_SIGCONTEXT_SREGS_OFFSET (8)
90 #define S390_SIGREGS_FP0_OFFSET (144)
91 #define S390X_SIGREGS_FP0_OFFSET (216)
92 #define S390_UC_MCONTEXT_OFFSET (256)
93 #define S390X_UC_MCONTEXT_OFFSET (344)
94 #define S390_STACK_FRAME_OVERHEAD (GDB_TARGET_IS_ESAME ? 160:96)
95 #define S390_SIGNAL_FRAMESIZE (GDB_TARGET_IS_ESAME ? 160:96)
96 #define s390_NR_sigreturn 119
97 #define s390_NR_rt_sigreturn 173
98
99
100
101 struct frame_extra_info
102 {
103 int initialised;
104 int good_prologue;
105 CORE_ADDR function_start;
106 CORE_ADDR skip_prologue_function_start;
107 CORE_ADDR saved_pc_valid;
108 CORE_ADDR saved_pc;
109 CORE_ADDR sig_fixed_saved_pc_valid;
110 CORE_ADDR sig_fixed_saved_pc;
111 CORE_ADDR frame_pointer_saved_pc; /* frame pointer needed for alloca */
112 CORE_ADDR stack_bought; /* amount we decrement the stack pointer by */
113 CORE_ADDR sigcontext;
114 };
115
116
117 static CORE_ADDR s390_frame_saved_pc_nofix (struct frame_info *fi);
118
119 int
120 s390_readinstruction (bfd_byte instr[], CORE_ADDR at,
121 struct disassemble_info *info)
122 {
123 int instrlen;
124
125 static int s390_instrlen[] = {
126 2,
127 4,
128 4,
129 6
130 };
131 if ((*info->read_memory_func) (at, &instr[0], 2, info))
132 return -1;
133 instrlen = s390_instrlen[instr[0] >> 6];
134 if ((*info->read_memory_func) (at + 2, &instr[2], instrlen - 2, info))
135 return -1;
136 return instrlen;
137 }
138
139 static void
140 s390_memset_extra_info (struct frame_extra_info *fextra_info)
141 {
142 memset (fextra_info, 0, sizeof (struct frame_extra_info));
143 }
144
145
146
147 char *
148 s390_register_name (int reg_nr)
149 {
150 static char *register_names[] = {
151 "pswm", "pswa",
152 "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
153 "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15",
154 "acr0", "acr1", "acr2", "acr3", "acr4", "acr5", "acr6", "acr7",
155 "acr8", "acr9", "acr10", "acr11", "acr12", "acr13", "acr14", "acr15",
156 "cr0", "cr1", "cr2", "cr3", "cr4", "cr5", "cr6", "cr7",
157 "cr8", "cr9", "cr10", "cr11", "cr12", "cr13", "cr14", "cr15",
158 "fpc",
159 "f0", "f1", "f2", "f3", "f4", "f5", "f6", "f7",
160 "f8", "f9", "f10", "f11", "f12", "f13", "f14", "f15"
161 };
162
163 if (reg_nr >= S390_LAST_REGNUM)
164 return NULL;
165 return register_names[reg_nr];
166 }
167
168
169
170
171 int
172 s390_stab_reg_to_regnum (int regno)
173 {
174 return regno >= 64 ? S390_PSWM_REGNUM - 64 :
175 regno >= 48 ? S390_FIRST_ACR - 48 :
176 regno >= 32 ? S390_FIRST_CR - 32 :
177 regno <= 15 ? (regno + 2) :
178 S390_FP0_REGNUM + ((regno - 16) & 8) + (((regno - 16) & 3) << 1) +
179 (((regno - 16) & 4) >> 2);
180 }
181
182
183
184 /* s390_get_frame_info based on Hartmuts
185 prologue definition in
186 gcc-2.8.1/config/l390/linux.c
187
188 It reads one instruction at a time & based on whether
189 it looks like prologue code or not it makes a decision on
190 whether the prologue is over, there are various state machines
191 in the code to determine if the prologue code is possilby valid.
192
193 This is done to hopefully allow the code survive minor revs of
194 calling conventions.
195
196 */
197
198 int
199 s390_get_frame_info (CORE_ADDR pc, struct frame_extra_info *fextra_info,
200 struct frame_info *fi, int init_extra_info)
201 {
202 #define CONST_POOL_REGIDX 13
203 #define GOT_REGIDX 12
204 bfd_byte instr[S390_MAX_INSTR_SIZE];
205 CORE_ADDR test_pc = pc, test_pc2;
206 CORE_ADDR orig_sp = 0, save_reg_addr = 0, *saved_regs = NULL;
207 int valid_prologue, good_prologue = 0;
208 int gprs_saved[S390_NUM_GPRS];
209 int fprs_saved[S390_NUM_FPRS];
210 int regidx, instrlen;
211 int save_link_regidx, subtract_sp_regidx;
212 int const_pool_state, save_link_state;
213 int frame_pointer_found, varargs_state;
214 int loop_cnt, gdb_gpr_store, gdb_fpr_store;
215 int frame_pointer_regidx = 0xf;
216 int offset, expected_offset;
217 int err = 0;
218 disassemble_info info;
219
220 /* What we've seen so far regarding r12 --- the GOT (Global Offset
221 Table) pointer. We expect to see `l %r12, N(%r13)', which loads
222 r12 with the offset from the constant pool to the GOT, and then
223 an `ar %r12, %r13', which adds the constant pool address,
224 yielding the GOT's address. Here's what got_state means:
225 0 -- seen nothing
226 1 -- seen `l %r12, N(%r13)', but no `ar'
227 2 -- seen load and add, so GOT pointer is totally initialized
228 When got_state is 1, then got_load_addr is the address of the
229 load instruction, and got_load_len is the length of that
230 instruction. */
231 int got_state;
232 CORE_ADDR got_load_addr = 0, got_load_len = 0;
233
234 const_pool_state = save_link_state = got_state = varargs_state = 0;
235 frame_pointer_found = 0;
236 memset (gprs_saved, 0, sizeof (gprs_saved));
237 memset (fprs_saved, 0, sizeof (fprs_saved));
238 info.read_memory_func = dis_asm_read_memory;
239
240 save_link_regidx = subtract_sp_regidx = 0;
241 if (fextra_info)
242 {
243 if (fi && fi->frame)
244 {
245 if (! init_extra_info && fextra_info->initialised)
246 orig_sp = fi->frame + fextra_info->stack_bought;
247 saved_regs = fi->saved_regs;
248 }
249 if (init_extra_info || !fextra_info->initialised)
250 {
251 s390_memset_extra_info (fextra_info);
252 fextra_info->function_start = pc;
253 fextra_info->initialised = 1;
254 }
255 }
256 instrlen = 0;
257 do
258 {
259 valid_prologue = 0;
260 test_pc += instrlen;
261 /* add the previous instruction len */
262 instrlen = s390_readinstruction (instr, test_pc, &info);
263 if (instrlen < 0)
264 {
265 good_prologue = 0;
266 err = -1;
267 break;
268 }
269 /* We probably are in a glibc syscall */
270 if (instr[0] == S390_SYSCALL_OPCODE && test_pc == pc)
271 {
272 good_prologue = 1;
273 if (saved_regs && fextra_info && fi->next && fi->next->extra_info
274 && fi->next->extra_info->sigcontext)
275 {
276 /* We are backtracing from a signal handler */
277 save_reg_addr = fi->next->extra_info->sigcontext +
278 REGISTER_BYTE (S390_GP0_REGNUM);
279 for (regidx = 0; regidx < S390_NUM_GPRS; regidx++)
280 {
281 saved_regs[S390_GP0_REGNUM + regidx] = save_reg_addr;
282 save_reg_addr += S390_GPR_SIZE;
283 }
284 save_reg_addr = fi->next->extra_info->sigcontext +
285 (GDB_TARGET_IS_ESAME ? S390X_SIGREGS_FP0_OFFSET :
286 S390_SIGREGS_FP0_OFFSET);
287 for (regidx = 0; regidx < S390_NUM_FPRS; regidx++)
288 {
289 saved_regs[S390_FP0_REGNUM + regidx] = save_reg_addr;
290 save_reg_addr += S390_FPR_SIZE;
291 }
292 }
293 break;
294 }
295 if (save_link_state == 0)
296 {
297 /* check for a stack relative STMG or STM */
298 if (((GDB_TARGET_IS_ESAME &&
299 ((instr[0] == 0xeb) && (instr[5] == 0x24))) ||
300 (instr[0] == 0x90)) && ((instr[2] >> 4) == 0xf))
301 {
302 regidx = (instr[1] >> 4);
303 if (regidx < 6)
304 varargs_state = 1;
305 offset = ((instr[2] & 0xf) << 8) + instr[3];
306 expected_offset =
307 S390_GPR6_STACK_OFFSET + (S390_GPR_SIZE * (regidx - 6));
308 if (offset != expected_offset)
309 {
310 good_prologue = 0;
311 break;
312 }
313 if (saved_regs)
314 save_reg_addr = orig_sp + offset;
315 for (; regidx <= (instr[1] & 0xf); regidx++)
316 {
317 if (gprs_saved[regidx])
318 {
319 good_prologue = 0;
320 break;
321 }
322 good_prologue = 1;
323 gprs_saved[regidx] = 1;
324 if (saved_regs)
325 {
326 saved_regs[S390_GP0_REGNUM + regidx] = save_reg_addr;
327 save_reg_addr += S390_GPR_SIZE;
328 }
329 }
330 valid_prologue = 1;
331 continue;
332 }
333 }
334 /* check for a stack relative STG or ST */
335 if ((save_link_state == 0 || save_link_state == 3) &&
336 ((GDB_TARGET_IS_ESAME &&
337 ((instr[0] == 0xe3) && (instr[5] == 0x24))) ||
338 (instr[0] == 0x50)) && ((instr[2] >> 4) == 0xf))
339 {
340 regidx = instr[1] >> 4;
341 offset = ((instr[2] & 0xf) << 8) + instr[3];
342 if (offset == 0)
343 {
344 if (save_link_state == 3 && regidx == save_link_regidx)
345 {
346 save_link_state = 4;
347 valid_prologue = 1;
348 continue;
349 }
350 else
351 break;
352 }
353 if (regidx < 6)
354 varargs_state = 1;
355 expected_offset =
356 S390_GPR6_STACK_OFFSET + (S390_GPR_SIZE * (regidx - 6));
357 if (offset != expected_offset)
358 {
359 good_prologue = 0;
360 break;
361 }
362 if (gprs_saved[regidx])
363 {
364 good_prologue = 0;
365 break;
366 }
367 good_prologue = 1;
368 gprs_saved[regidx] = 1;
369 if (saved_regs)
370 {
371 save_reg_addr = orig_sp + offset;
372 saved_regs[S390_GP0_REGNUM + regidx] = save_reg_addr;
373 }
374 valid_prologue = 1;
375 continue;
376 }
377
378 /* check for STD */
379 if (instr[0] == 0x60 && (instr[2] >> 4) == 0xf)
380 {
381 regidx = instr[1] >> 4;
382 if (regidx == 0 || regidx == 2)
383 varargs_state = 1;
384 if (fprs_saved[regidx])
385 {
386 good_prologue = 0;
387 break;
388 }
389 fprs_saved[regidx] = 1;
390 if (saved_regs)
391 {
392 save_reg_addr = orig_sp + (((instr[2] & 0xf) << 8) + instr[3]);
393 saved_regs[S390_FP0_REGNUM + regidx] = save_reg_addr;
394 }
395 valid_prologue = 1;
396 continue;
397 }
398
399
400 if (const_pool_state == 0)
401 {
402
403 if (GDB_TARGET_IS_ESAME)
404 {
405 /* Check for larl CONST_POOL_REGIDX,offset on ESAME */
406 if ((instr[0] == 0xc0)
407 && (instr[1] == (CONST_POOL_REGIDX << 4)))
408 {
409 const_pool_state = 2;
410 valid_prologue = 1;
411 continue;
412 }
413 }
414 else
415 {
416 /* Check for BASR gpr13,gpr0 used to load constant pool pointer to r13 in old compiler */
417 if (instr[0] == 0xd && (instr[1] & 0xf) == 0
418 && ((instr[1] >> 4) == CONST_POOL_REGIDX))
419 {
420 const_pool_state = 1;
421 valid_prologue = 1;
422 continue;
423 }
424 }
425 /* Check for new fangled bras %r13,newpc to load new constant pool */
426 /* embedded in code, older pre abi compilers also emitted this stuff. */
427 if ((instr[0] == 0xa7) && ((instr[1] & 0xf) == 0x5) &&
428 ((instr[1] >> 4) == CONST_POOL_REGIDX)
429 && ((instr[2] & 0x80) == 0))
430 {
431 const_pool_state = 2;
432 test_pc +=
433 (((((instr[2] & 0xf) << 8) + instr[3]) << 1) - instrlen);
434 valid_prologue = 1;
435 continue;
436 }
437 }
438 /* Check for AGHI or AHI CONST_POOL_REGIDX,val */
439 if (const_pool_state == 1 && (instr[0] == 0xa7) &&
440 ((GDB_TARGET_IS_ESAME &&
441 (instr[1] == ((CONST_POOL_REGIDX << 4) | 0xb))) ||
442 (instr[1] == ((CONST_POOL_REGIDX << 4) | 0xa))))
443 {
444 const_pool_state = 2;
445 valid_prologue = 1;
446 continue;
447 }
448 /* Check for LGR or LR gprx,15 */
449 if ((GDB_TARGET_IS_ESAME &&
450 instr[0] == 0xb9 && instr[1] == 0x04 && (instr[3] & 0xf) == 0xf) ||
451 (instr[0] == 0x18 && (instr[1] & 0xf) == 0xf))
452 {
453 if (GDB_TARGET_IS_ESAME)
454 regidx = instr[3] >> 4;
455 else
456 regidx = instr[1] >> 4;
457 if (save_link_state == 0 && regidx != 0xb)
458 {
459 /* Almost defintely code for
460 decrementing the stack pointer
461 ( i.e. a non leaf function
462 or else leaf with locals ) */
463 save_link_regidx = regidx;
464 save_link_state = 1;
465 valid_prologue = 1;
466 continue;
467 }
468 /* We use this frame pointer for alloca
469 unfortunately we need to assume its gpr11
470 otherwise we would need a smarter prologue
471 walker. */
472 if (!frame_pointer_found && regidx == 0xb)
473 {
474 frame_pointer_regidx = 0xb;
475 frame_pointer_found = 1;
476 if (fextra_info)
477 fextra_info->frame_pointer_saved_pc = test_pc;
478 valid_prologue = 1;
479 continue;
480 }
481 }
482 /* Check for AHI or AGHI gpr15,val */
483 if (save_link_state == 1 && (instr[0] == 0xa7) &&
484 ((GDB_TARGET_IS_ESAME && (instr[1] == 0xfb)) || (instr[1] == 0xfa)))
485 {
486 if (fextra_info)
487 fextra_info->stack_bought =
488 -extract_signed_integer (&instr[2], 2);
489 save_link_state = 3;
490 valid_prologue = 1;
491 continue;
492 }
493 /* Alternatively check for the complex construction for
494 buying more than 32k of stack
495 BRAS gprx,.+8
496 long vals %r15,0(%gprx) gprx currently r1 */
497 if ((save_link_state == 1) && (instr[0] == 0xa7)
498 && ((instr[1] & 0xf) == 0x5) && (instr[2] == 0)
499 && (instr[3] == 0x4) && ((instr[1] >> 4) != CONST_POOL_REGIDX))
500 {
501 subtract_sp_regidx = instr[1] >> 4;
502 save_link_state = 2;
503 if (fextra_info)
504 target_read_memory (test_pc + instrlen,
505 (char *) &fextra_info->stack_bought,
506 sizeof (fextra_info->stack_bought));
507 test_pc += 4;
508 valid_prologue = 1;
509 continue;
510 }
511 if (save_link_state == 2 && instr[0] == 0x5b
512 && instr[1] == 0xf0 &&
513 instr[2] == (subtract_sp_regidx << 4) && instr[3] == 0)
514 {
515 save_link_state = 3;
516 valid_prologue = 1;
517 continue;
518 }
519 /* check for LA gprx,offset(15) used for varargs */
520 if ((instr[0] == 0x41) && ((instr[2] >> 4) == 0xf) &&
521 ((instr[1] & 0xf) == 0))
522 {
523 /* some code uses gpr7 to point to outgoing args */
524 if (((instr[1] >> 4) == 7) && (save_link_state == 0) &&
525 ((instr[2] & 0xf) == 0)
526 && (instr[3] == S390_STACK_FRAME_OVERHEAD))
527 {
528 valid_prologue = 1;
529 continue;
530 }
531 if (varargs_state == 1)
532 {
533 varargs_state = 2;
534 valid_prologue = 1;
535 continue;
536 }
537 }
538 /* Check for a GOT load */
539
540 if (GDB_TARGET_IS_ESAME)
541 {
542 /* Check for larl GOT_REGIDX, on ESAME */
543 if ((got_state == 0) && (instr[0] == 0xc0)
544 && (instr[1] == (GOT_REGIDX << 4)))
545 {
546 got_state = 2;
547 valid_prologue = 1;
548 continue;
549 }
550 }
551 else
552 {
553 /* check for l GOT_REGIDX,x(CONST_POOL_REGIDX) */
554 if (got_state == 0 && const_pool_state == 2 && instr[0] == 0x58
555 && (instr[2] == (CONST_POOL_REGIDX << 4))
556 && ((instr[1] >> 4) == GOT_REGIDX))
557 {
558 got_state = 1;
559 got_load_addr = test_pc;
560 got_load_len = instrlen;
561 valid_prologue = 1;
562 continue;
563 }
564 /* Check for subsequent ar got_regidx,basr_regidx */
565 if (got_state == 1 && instr[0] == 0x1a &&
566 instr[1] == ((GOT_REGIDX << 4) | CONST_POOL_REGIDX))
567 {
568 got_state = 2;
569 valid_prologue = 1;
570 continue;
571 }
572 }
573 }
574 while (valid_prologue && good_prologue);
575 if (good_prologue)
576 {
577 /* If this function doesn't reference the global offset table,
578 then the compiler may use r12 for other things. If the last
579 instruction we saw was a load of r12 from the constant pool,
580 with no subsequent add to make the address PC-relative, then
581 the load was probably a genuine body instruction; don't treat
582 it as part of the prologue. */
583 if (got_state == 1
584 && got_load_addr + got_load_len == test_pc)
585 {
586 test_pc = got_load_addr;
587 instrlen = got_load_len;
588 }
589
590 good_prologue = (((const_pool_state == 0) || (const_pool_state == 2)) &&
591 ((save_link_state == 0) || (save_link_state == 4)) &&
592 ((varargs_state == 0) || (varargs_state == 2)));
593 }
594 if (fextra_info)
595 {
596 fextra_info->good_prologue = good_prologue;
597 fextra_info->skip_prologue_function_start =
598 (good_prologue ? test_pc : pc);
599 }
600 if (saved_regs)
601 /* The SP's element of the saved_regs array holds the old SP,
602 not the address at which it is saved. */
603 saved_regs[S390_SP_REGNUM] = orig_sp;
604 return err;
605 }
606
607
608 int
609 s390_check_function_end (CORE_ADDR pc)
610 {
611 bfd_byte instr[S390_MAX_INSTR_SIZE];
612 disassemble_info info;
613 int regidx, instrlen;
614
615 info.read_memory_func = dis_asm_read_memory;
616 instrlen = s390_readinstruction (instr, pc, &info);
617 if (instrlen < 0)
618 return -1;
619 /* check for BR */
620 if (instrlen != 2 || instr[0] != 07 || (instr[1] >> 4) != 0xf)
621 return 0;
622 regidx = instr[1] & 0xf;
623 /* Check for LMG or LG */
624 instrlen =
625 s390_readinstruction (instr, pc - (GDB_TARGET_IS_ESAME ? 6 : 4), &info);
626 if (instrlen < 0)
627 return -1;
628 if (GDB_TARGET_IS_ESAME)
629 {
630
631 if (instrlen != 6 || instr[0] != 0xeb || instr[5] != 0x4)
632 return 0;
633 }
634 else if (instrlen != 4 || instr[0] != 0x98)
635 {
636 return 0;
637 }
638 if ((instr[2] >> 4) != 0xf)
639 return 0;
640 if (regidx == 14)
641 return 1;
642 instrlen = s390_readinstruction (instr, pc - (GDB_TARGET_IS_ESAME ? 12 : 8),
643 &info);
644 if (instrlen < 0)
645 return -1;
646 if (GDB_TARGET_IS_ESAME)
647 {
648 /* Check for LG */
649 if (instrlen != 6 || instr[0] != 0xe3 || instr[5] != 0x4)
650 return 0;
651 }
652 else
653 {
654 /* Check for L */
655 if (instrlen != 4 || instr[0] != 0x58)
656 return 0;
657 }
658 if (instr[2] >> 4 != 0xf)
659 return 0;
660 if (instr[1] >> 4 != regidx)
661 return 0;
662 return 1;
663 }
664
665 static CORE_ADDR
666 s390_sniff_pc_function_start (CORE_ADDR pc, struct frame_info *fi)
667 {
668 CORE_ADDR function_start, test_function_start;
669 int loop_cnt, err, function_end;
670 struct frame_extra_info fextra_info;
671 function_start = get_pc_function_start (pc);
672
673 if (function_start == 0)
674 {
675 test_function_start = pc;
676 if (test_function_start & 1)
677 return 0; /* This has to be bogus */
678 loop_cnt = 0;
679 do
680 {
681
682 err =
683 s390_get_frame_info (test_function_start, &fextra_info, fi, 1);
684 loop_cnt++;
685 test_function_start -= 2;
686 function_end = s390_check_function_end (test_function_start);
687 }
688 while (!(function_end == 1 || err || loop_cnt >= 4096 ||
689 (fextra_info.good_prologue)));
690 if (fextra_info.good_prologue)
691 function_start = fextra_info.function_start;
692 else if (function_end == 1)
693 function_start = test_function_start;
694 }
695 return function_start;
696 }
697
698
699
700 CORE_ADDR
701 s390_function_start (struct frame_info *fi)
702 {
703 CORE_ADDR function_start = 0;
704
705 if (fi->extra_info && fi->extra_info->initialised)
706 function_start = fi->extra_info->function_start;
707 else if (fi->pc)
708 function_start = get_pc_function_start (fi->pc);
709 return function_start;
710 }
711
712
713
714
715 int
716 s390_frameless_function_invocation (struct frame_info *fi)
717 {
718 struct frame_extra_info fextra_info, *fextra_info_ptr;
719 int frameless = 0;
720
721 if (fi->next == NULL) /* no may be frameless */
722 {
723 if (fi->extra_info)
724 fextra_info_ptr = fi->extra_info;
725 else
726 {
727 fextra_info_ptr = &fextra_info;
728 s390_get_frame_info (s390_sniff_pc_function_start (fi->pc, fi),
729 fextra_info_ptr, fi, 1);
730 }
731 frameless = ((fextra_info_ptr->stack_bought == 0));
732 }
733 return frameless;
734
735 }
736
737
738 static int
739 s390_is_sigreturn (CORE_ADDR pc, struct frame_info *sighandler_fi,
740 CORE_ADDR *sregs, CORE_ADDR *sigcaller_pc)
741 {
742 bfd_byte instr[S390_MAX_INSTR_SIZE];
743 disassemble_info info;
744 int instrlen;
745 CORE_ADDR scontext;
746 int retval = 0;
747 CORE_ADDR orig_sp;
748 CORE_ADDR temp_sregs;
749
750 scontext = temp_sregs = 0;
751
752 info.read_memory_func = dis_asm_read_memory;
753 instrlen = s390_readinstruction (instr, pc, &info);
754 if (sigcaller_pc)
755 *sigcaller_pc = 0;
756 if (((instrlen == S390_SYSCALL_SIZE) &&
757 (instr[0] == S390_SYSCALL_OPCODE)) &&
758 ((instr[1] == s390_NR_sigreturn) || (instr[1] == s390_NR_rt_sigreturn)))
759 {
760 if (sighandler_fi)
761 {
762 if (s390_frameless_function_invocation (sighandler_fi))
763 orig_sp = sighandler_fi->frame;
764 else
765 orig_sp = ADDR_BITS_REMOVE ((CORE_ADDR)
766 read_memory_integer (sighandler_fi->
767 frame,
768 S390_GPR_SIZE));
769 if (orig_sp && sigcaller_pc)
770 {
771 scontext = orig_sp + S390_SIGNAL_FRAMESIZE;
772 if (pc == scontext && instr[1] == s390_NR_rt_sigreturn)
773 {
774 /* We got a new style rt_signal */
775 /* get address of read ucontext->uc_mcontext */
776 temp_sregs = orig_sp + (GDB_TARGET_IS_ESAME ?
777 S390X_UC_MCONTEXT_OFFSET :
778 S390_UC_MCONTEXT_OFFSET);
779 }
780 else
781 {
782 /* read sigcontext->sregs */
783 temp_sregs = ADDR_BITS_REMOVE ((CORE_ADDR)
784 read_memory_integer (scontext
785 +
786 (GDB_TARGET_IS_ESAME
787 ?
788 S390X_SIGCONTEXT_SREGS_OFFSET
789 :
790 S390_SIGCONTEXT_SREGS_OFFSET),
791 S390_GPR_SIZE));
792
793 }
794 /* read sigregs->psw.addr */
795 *sigcaller_pc =
796 ADDR_BITS_REMOVE ((CORE_ADDR)
797 read_memory_integer (temp_sregs +
798 REGISTER_BYTE
799 (S390_PC_REGNUM),
800 S390_PSW_ADDR_SIZE));
801 }
802 }
803 retval = 1;
804 }
805 if (sregs)
806 *sregs = temp_sregs;
807 return retval;
808 }
809
810 /*
811 We need to do something better here but this will keep us out of trouble
812 for the moment.
813 For some reason the blockframe.c calls us with fi->next->fromleaf
814 so this seems of little use to us. */
815 void
816 s390_init_frame_pc_first (int next_fromleaf, struct frame_info *fi)
817 {
818 CORE_ADDR sigcaller_pc;
819
820 fi->pc = 0;
821 if (next_fromleaf)
822 {
823 fi->pc = ADDR_BITS_REMOVE (read_register (S390_RETADDR_REGNUM));
824 /* fix signal handlers */
825 }
826 else if (fi->next && fi->next->pc)
827 fi->pc = s390_frame_saved_pc_nofix (fi->next);
828 if (fi->pc && fi->next && fi->next->frame &&
829 s390_is_sigreturn (fi->pc, fi->next, NULL, &sigcaller_pc))
830 {
831 fi->pc = sigcaller_pc;
832 }
833
834 }
835
836 void
837 s390_init_extra_frame_info (int fromleaf, struct frame_info *fi)
838 {
839 fi->extra_info = frame_obstack_alloc (sizeof (struct frame_extra_info));
840 if (fi->pc)
841 s390_get_frame_info (s390_sniff_pc_function_start (fi->pc, fi),
842 fi->extra_info, fi, 1);
843 else
844 s390_memset_extra_info (fi->extra_info);
845 }
846
847 /* If saved registers of frame FI are not known yet, read and cache them.
848 &FEXTRA_INFOP contains struct frame_extra_info; TDATAP can be NULL,
849 in which case the framedata are read. */
850
851 void
852 s390_frame_init_saved_regs (struct frame_info *fi)
853 {
854
855 int quick;
856
857 if (fi->saved_regs == NULL)
858 {
859 /* zalloc memsets the saved regs */
860 frame_saved_regs_zalloc (fi);
861 if (fi->pc)
862 {
863 quick = (fi->extra_info && fi->extra_info->initialised
864 && fi->extra_info->good_prologue);
865 s390_get_frame_info (quick ? fi->extra_info->function_start :
866 s390_sniff_pc_function_start (fi->pc, fi),
867 fi->extra_info, fi, !quick);
868 }
869 }
870 }
871
872
873
874 CORE_ADDR
875 s390_frame_args_address (struct frame_info *fi)
876 {
877
878 /* Apparently gdb already knows gdb_args_offset itself */
879 return fi->frame;
880 }
881
882
883 static CORE_ADDR
884 s390_frame_saved_pc_nofix (struct frame_info *fi)
885 {
886 if (fi->extra_info && fi->extra_info->saved_pc_valid)
887 return fi->extra_info->saved_pc;
888
889 if (generic_find_dummy_frame (fi->pc, fi->frame))
890 return generic_read_register_dummy (fi->pc, fi->frame, S390_PC_REGNUM);
891
892 s390_frame_init_saved_regs (fi);
893 if (fi->extra_info)
894 {
895 fi->extra_info->saved_pc_valid = 1;
896 if (fi->extra_info->good_prologue)
897 {
898 if (fi->saved_regs[S390_RETADDR_REGNUM])
899 {
900 return (fi->extra_info->saved_pc =
901 ADDR_BITS_REMOVE (read_memory_integer
902 (fi->saved_regs[S390_RETADDR_REGNUM],
903 S390_GPR_SIZE)));
904 }
905 else
906 return read_register (S390_RETADDR_REGNUM);
907 }
908 }
909 return 0;
910 }
911
912 CORE_ADDR
913 s390_frame_saved_pc (struct frame_info *fi)
914 {
915 CORE_ADDR saved_pc = 0, sig_pc;
916
917 if (fi->extra_info && fi->extra_info->sig_fixed_saved_pc_valid)
918 return fi->extra_info->sig_fixed_saved_pc;
919 saved_pc = s390_frame_saved_pc_nofix (fi);
920
921 if (fi->extra_info)
922 {
923 fi->extra_info->sig_fixed_saved_pc_valid = 1;
924 if (saved_pc)
925 {
926 if (s390_is_sigreturn (saved_pc, fi, NULL, &sig_pc))
927 saved_pc = sig_pc;
928 }
929 fi->extra_info->sig_fixed_saved_pc = saved_pc;
930 }
931 return saved_pc;
932 }
933
934
935
936
937 /* We want backtraces out of signal handlers so we don't
938 set thisframe->signal_handler_caller to 1 */
939
940 CORE_ADDR
941 s390_frame_chain (struct frame_info *thisframe)
942 {
943 CORE_ADDR prev_fp = 0;
944
945 if (thisframe->prev && thisframe->prev->frame)
946 prev_fp = thisframe->prev->frame;
947 else if (generic_find_dummy_frame (thisframe->pc, thisframe->frame))
948 return generic_read_register_dummy (thisframe->pc, thisframe->frame,
949 S390_SP_REGNUM);
950 else
951 {
952 int sigreturn = 0;
953 CORE_ADDR sregs = 0;
954 struct frame_extra_info prev_fextra_info;
955
956 memset (&prev_fextra_info, 0, sizeof (prev_fextra_info));
957 if (thisframe->pc)
958 {
959 CORE_ADDR saved_pc, sig_pc;
960
961 saved_pc = s390_frame_saved_pc_nofix (thisframe);
962 if (saved_pc)
963 {
964 if ((sigreturn =
965 s390_is_sigreturn (saved_pc, thisframe, &sregs, &sig_pc)))
966 saved_pc = sig_pc;
967 s390_get_frame_info (s390_sniff_pc_function_start
968 (saved_pc, NULL), &prev_fextra_info, NULL,
969 1);
970 }
971 }
972 if (sigreturn)
973 {
974 /* read sigregs,regs.gprs[11 or 15] */
975 prev_fp = read_memory_integer (sregs +
976 REGISTER_BYTE (S390_GP0_REGNUM +
977 (prev_fextra_info.
978 frame_pointer_saved_pc
979 ? 11 : 15)),
980 S390_GPR_SIZE);
981 thisframe->extra_info->sigcontext = sregs;
982 }
983 else
984 {
985 if (thisframe->saved_regs)
986 {
987 int regno;
988
989 if (prev_fextra_info.frame_pointer_saved_pc
990 && thisframe->saved_regs[S390_FRAME_REGNUM])
991 regno = S390_FRAME_REGNUM;
992 else
993 regno = S390_SP_REGNUM;
994
995 if (thisframe->saved_regs[regno])
996 {
997 /* The SP's entry of `saved_regs' is special. */
998 if (regno == S390_SP_REGNUM)
999 prev_fp = thisframe->saved_regs[regno];
1000 else
1001 prev_fp =
1002 read_memory_integer (thisframe->saved_regs[regno],
1003 S390_GPR_SIZE);
1004 }
1005 }
1006 }
1007 }
1008 return ADDR_BITS_REMOVE (prev_fp);
1009 }
1010
1011 /*
1012 Whether struct frame_extra_info is actually needed I'll have to figure
1013 out as our frames are similar to rs6000 there is a possibility
1014 i386 dosen't need it. */
1015
1016
1017
1018 /* a given return value in `regbuf' with a type `valtype', extract and copy its
1019 value into `valbuf' */
1020 void
1021 s390_extract_return_value (struct type *valtype, char *regbuf, char *valbuf)
1022 {
1023 /* floats and doubles are returned in fpr0. fpr's have a size of 8 bytes.
1024 We need to truncate the return value into float size (4 byte) if
1025 necessary. */
1026 int len = TYPE_LENGTH (valtype);
1027
1028 if (TYPE_CODE (valtype) == TYPE_CODE_FLT)
1029 memcpy (valbuf, &regbuf[REGISTER_BYTE (S390_FP0_REGNUM)], len);
1030 else
1031 {
1032 int offset = 0;
1033 /* return value is copied starting from r2. */
1034 if (TYPE_LENGTH (valtype) < S390_GPR_SIZE)
1035 offset = S390_GPR_SIZE - TYPE_LENGTH (valtype);
1036 memcpy (valbuf,
1037 regbuf + REGISTER_BYTE (S390_GP0_REGNUM + 2) + offset,
1038 TYPE_LENGTH (valtype));
1039 }
1040 }
1041
1042
1043 static char *
1044 s390_promote_integer_argument (struct type *valtype, char *valbuf,
1045 char *reg_buff, int *arglen)
1046 {
1047 char *value = valbuf;
1048 int len = TYPE_LENGTH (valtype);
1049
1050 if (len < S390_GPR_SIZE)
1051 {
1052 /* We need to upgrade this value to a register to pass it correctly */
1053 int idx, diff = S390_GPR_SIZE - len, negative =
1054 (!TYPE_UNSIGNED (valtype) && value[0] & 0x80);
1055 for (idx = 0; idx < S390_GPR_SIZE; idx++)
1056 {
1057 reg_buff[idx] = (idx < diff ? (negative ? 0xff : 0x0) :
1058 value[idx - diff]);
1059 }
1060 value = reg_buff;
1061 *arglen = S390_GPR_SIZE;
1062 }
1063 else
1064 {
1065 if (len & (S390_GPR_SIZE - 1))
1066 {
1067 fprintf_unfiltered (gdb_stderr,
1068 "s390_promote_integer_argument detected an argument not "
1069 "a multiple of S390_GPR_SIZE & greater than S390_GPR_SIZE "
1070 "we might not deal with this correctly.\n");
1071 }
1072 *arglen = len;
1073 }
1074
1075 return (value);
1076 }
1077
1078 void
1079 s390_store_return_value (struct type *valtype, char *valbuf)
1080 {
1081 int arglen;
1082 char *reg_buff = alloca (max (S390_FPR_SIZE, REGISTER_SIZE)), *value;
1083
1084 if (TYPE_CODE (valtype) == TYPE_CODE_FLT)
1085 {
1086 DOUBLEST tempfloat = extract_floating (valbuf, TYPE_LENGTH (valtype));
1087
1088 floatformat_from_doublest (&floatformat_ieee_double_big, &tempfloat,
1089 reg_buff);
1090 write_register_bytes (REGISTER_BYTE (S390_FP0_REGNUM), reg_buff,
1091 S390_FPR_SIZE);
1092 }
1093 else
1094 {
1095 value =
1096 s390_promote_integer_argument (valtype, valbuf, reg_buff, &arglen);
1097 /* Everything else is returned in GPR2 and up. */
1098 write_register_bytes (REGISTER_BYTE (S390_GP0_REGNUM + 2), value,
1099 arglen);
1100 }
1101 }
1102 static int
1103 gdb_print_insn_s390 (bfd_vma memaddr, disassemble_info * info)
1104 {
1105 bfd_byte instrbuff[S390_MAX_INSTR_SIZE];
1106 int instrlen, cnt;
1107
1108 instrlen = s390_readinstruction (instrbuff, (CORE_ADDR) memaddr, info);
1109 if (instrlen < 0)
1110 {
1111 (*info->memory_error_func) (instrlen, memaddr, info);
1112 return -1;
1113 }
1114 for (cnt = 0; cnt < instrlen; cnt++)
1115 info->fprintf_func (info->stream, "%02X ", instrbuff[cnt]);
1116 for (cnt = instrlen; cnt < S390_MAX_INSTR_SIZE; cnt++)
1117 info->fprintf_func (info->stream, " ");
1118 instrlen = print_insn_s390 (memaddr, info);
1119 return instrlen;
1120 }
1121
1122
1123
1124 /* Not the most efficent code in the world */
1125 int
1126 s390_fp_regnum ()
1127 {
1128 int regno = S390_SP_REGNUM;
1129 struct frame_extra_info fextra_info;
1130
1131 CORE_ADDR pc = ADDR_BITS_REMOVE (read_register (S390_PC_REGNUM));
1132
1133 s390_get_frame_info (s390_sniff_pc_function_start (pc, NULL), &fextra_info,
1134 NULL, 1);
1135 if (fextra_info.frame_pointer_saved_pc)
1136 regno = S390_FRAME_REGNUM;
1137 return regno;
1138 }
1139
1140 CORE_ADDR
1141 s390_read_fp ()
1142 {
1143 return read_register (s390_fp_regnum ());
1144 }
1145
1146
1147 void
1148 s390_write_fp (CORE_ADDR val)
1149 {
1150 write_register (s390_fp_regnum (), val);
1151 }
1152
1153
1154 static void
1155 s390_pop_frame_regular (struct frame_info *frame)
1156 {
1157 int regnum;
1158
1159 write_register (S390_PC_REGNUM, FRAME_SAVED_PC (frame));
1160
1161 /* Restore any saved registers. */
1162 for (regnum = 0; regnum < NUM_REGS; regnum++)
1163 if (frame->saved_regs[regnum] != 0)
1164 {
1165 ULONGEST value;
1166
1167 value = read_memory_unsigned_integer (frame->saved_regs[regnum],
1168 REGISTER_RAW_SIZE (regnum));
1169 write_register (regnum, value);
1170 }
1171
1172 /* Actually cut back the stack. Remember that the SP's element of
1173 saved_regs is the old SP itself, not the address at which it is
1174 saved. */
1175 write_register (S390_SP_REGNUM, frame->saved_regs[S390_SP_REGNUM]);
1176
1177 /* Throw away any cached frame information. */
1178 flush_cached_frames ();
1179 }
1180
1181
1182 /* Destroy the innermost (Top-Of-Stack) stack frame, restoring the
1183 machine state that was in effect before the frame was created.
1184 Used in the contexts of the "return" command, and of
1185 target function calls from the debugger. */
1186 void
1187 s390_pop_frame ()
1188 {
1189 /* This function checks for and handles generic dummy frames, and
1190 calls back to our function for ordinary frames. */
1191 generic_pop_current_frame (s390_pop_frame_regular);
1192 }
1193
1194
1195 /* Return non-zero if TYPE is an integer-like type, zero otherwise.
1196 "Integer-like" types are those that should be passed the way
1197 integers are: integers, enums, ranges, characters, and booleans. */
1198 static int
1199 is_integer_like (struct type *type)
1200 {
1201 enum type_code code = TYPE_CODE (type);
1202
1203 return (code == TYPE_CODE_INT
1204 || code == TYPE_CODE_ENUM
1205 || code == TYPE_CODE_RANGE
1206 || code == TYPE_CODE_CHAR
1207 || code == TYPE_CODE_BOOL);
1208 }
1209
1210
1211 /* Return non-zero if TYPE is a pointer-like type, zero otherwise.
1212 "Pointer-like" types are those that should be passed the way
1213 pointers are: pointers and references. */
1214 static int
1215 is_pointer_like (struct type *type)
1216 {
1217 enum type_code code = TYPE_CODE (type);
1218
1219 return (code == TYPE_CODE_PTR
1220 || code == TYPE_CODE_REF);
1221 }
1222
1223
1224 /* Return non-zero if TYPE is a `float singleton' or `double
1225 singleton', zero otherwise.
1226
1227 A `T singleton' is a struct type with one member, whose type is
1228 either T or a `T singleton'. So, the following are all float
1229 singletons:
1230
1231 struct { float x };
1232 struct { struct { float x; } x; };
1233 struct { struct { struct { float x; } x; } x; };
1234
1235 ... and so on.
1236
1237 WHY THE HECK DO WE CARE ABOUT THIS??? Well, it turns out that GCC
1238 passes all float singletons and double singletons as if they were
1239 simply floats or doubles. This is *not* what the ABI says it
1240 should do. */
1241 static int
1242 is_float_singleton (struct type *type)
1243 {
1244 return (TYPE_CODE (type) == TYPE_CODE_STRUCT
1245 && TYPE_NFIELDS (type) == 1
1246 && (TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_FLT
1247 || is_float_singleton (TYPE_FIELD_TYPE (type, 0))));
1248 }
1249
1250
1251 /* Return non-zero if TYPE is a struct-like type, zero otherwise.
1252 "Struct-like" types are those that should be passed as structs are:
1253 structs and unions.
1254
1255 As an odd quirk, not mentioned in the ABI, GCC passes float and
1256 double singletons as if they were a plain float, double, etc. (The
1257 corresponding union types are handled normally.) So we exclude
1258 those types here. *shrug* */
1259 static int
1260 is_struct_like (struct type *type)
1261 {
1262 enum type_code code = TYPE_CODE (type);
1263
1264 return (code == TYPE_CODE_UNION
1265 || (code == TYPE_CODE_STRUCT && ! is_float_singleton (type)));
1266 }
1267
1268
1269 /* Return non-zero if TYPE is a float-like type, zero otherwise.
1270 "Float-like" types are those that should be passed as
1271 floating-point values are.
1272
1273 You'd think this would just be floats, doubles, long doubles, etc.
1274 But as an odd quirk, not mentioned in the ABI, GCC passes float and
1275 double singletons as if they were a plain float, double, etc. (The
1276 corresponding union types are handled normally.) So we exclude
1277 those types here. *shrug* */
1278 static int
1279 is_float_like (struct type *type)
1280 {
1281 return (TYPE_CODE (type) == TYPE_CODE_FLT
1282 || is_float_singleton (type));
1283 }
1284
1285
1286 /* Return non-zero if TYPE is considered a `DOUBLE_OR_FLOAT', as
1287 defined by the parameter passing conventions described in the
1288 "Linux for S/390 ELF Application Binary Interface Supplement".
1289 Otherwise, return zero. */
1290 static int
1291 is_double_or_float (struct type *type)
1292 {
1293 return (is_float_like (type)
1294 && (TYPE_LENGTH (type) == 4
1295 || TYPE_LENGTH (type) == 8));
1296 }
1297
1298
1299 /* Return non-zero if TYPE is considered a `SIMPLE_ARG', as defined by
1300 the parameter passing conventions described in the "Linux for S/390
1301 ELF Application Binary Interface Supplement". Return zero otherwise. */
1302 static int
1303 is_simple_arg (struct type *type)
1304 {
1305 unsigned length = TYPE_LENGTH (type);
1306
1307 /* This is almost a direct translation of the ABI's language, except
1308 that we have to exclude 8-byte structs; those are DOUBLE_ARGs. */
1309 return ((is_integer_like (type) && length <= 4)
1310 || is_pointer_like (type)
1311 || (is_struct_like (type) && length != 8)
1312 || (is_float_like (type) && length == 16));
1313 }
1314
1315
1316 /* Return non-zero if TYPE should be passed as a pointer to a copy,
1317 zero otherwise. TYPE must be a SIMPLE_ARG, as recognized by
1318 `is_simple_arg'. */
1319 static int
1320 pass_by_copy_ref (struct type *type)
1321 {
1322 unsigned length = TYPE_LENGTH (type);
1323
1324 return ((is_struct_like (type) && length != 1 && length != 2 && length != 4)
1325 || (is_float_like (type) && length == 16));
1326 }
1327
1328
1329 /* Return ARG, a `SIMPLE_ARG', sign-extended or zero-extended to a full
1330 word as required for the ABI. */
1331 static LONGEST
1332 extend_simple_arg (struct value *arg)
1333 {
1334 struct type *type = VALUE_TYPE (arg);
1335
1336 /* Even structs get passed in the least significant bits of the
1337 register / memory word. It's not really right to extract them as
1338 an integer, but it does take care of the extension. */
1339 if (TYPE_UNSIGNED (type))
1340 return extract_unsigned_integer (VALUE_CONTENTS (arg),
1341 TYPE_LENGTH (type));
1342 else
1343 return extract_signed_integer (VALUE_CONTENTS (arg),
1344 TYPE_LENGTH (type));
1345 }
1346
1347
1348 /* Return non-zero if TYPE is a `DOUBLE_ARG', as defined by the
1349 parameter passing conventions described in the "Linux for S/390 ELF
1350 Application Binary Interface Supplement". Return zero otherwise. */
1351 static int
1352 is_double_arg (struct type *type)
1353 {
1354 unsigned length = TYPE_LENGTH (type);
1355
1356 return ((is_integer_like (type)
1357 || is_struct_like (type))
1358 && length == 8);
1359 }
1360
1361
1362 /* Round ADDR up to the next N-byte boundary. N must be a power of
1363 two. */
1364 static CORE_ADDR
1365 round_up (CORE_ADDR addr, int n)
1366 {
1367 /* Check that N is really a power of two. */
1368 gdb_assert (n && (n & (n-1)) == 0);
1369 return ((addr + n - 1) & -n);
1370 }
1371
1372
1373 /* Round ADDR down to the next N-byte boundary. N must be a power of
1374 two. */
1375 static CORE_ADDR
1376 round_down (CORE_ADDR addr, int n)
1377 {
1378 /* Check that N is really a power of two. */
1379 gdb_assert (n && (n & (n-1)) == 0);
1380 return (addr & -n);
1381 }
1382
1383
1384 /* Return the alignment required by TYPE. */
1385 static int
1386 alignment_of (struct type *type)
1387 {
1388 int alignment;
1389
1390 if (is_integer_like (type)
1391 || is_pointer_like (type)
1392 || TYPE_CODE (type) == TYPE_CODE_FLT)
1393 alignment = TYPE_LENGTH (type);
1394 else if (TYPE_CODE (type) == TYPE_CODE_STRUCT
1395 || TYPE_CODE (type) == TYPE_CODE_UNION)
1396 {
1397 int i;
1398
1399 alignment = 1;
1400 for (i = 0; i < TYPE_NFIELDS (type); i++)
1401 {
1402 int field_alignment = alignment_of (TYPE_FIELD_TYPE (type, i));
1403
1404 if (field_alignment > alignment)
1405 alignment = field_alignment;
1406 }
1407 }
1408 else
1409 alignment = 1;
1410
1411 /* Check that everything we ever return is a power of two. Lots of
1412 code doesn't want to deal with aligning things to arbitrary
1413 boundaries. */
1414 gdb_assert ((alignment & (alignment - 1)) == 0);
1415
1416 return alignment;
1417 }
1418
1419
1420 /* Put the actual parameter values pointed to by ARGS[0..NARGS-1] in
1421 place to be passed to a function, as specified by the "Linux for
1422 S/390 ELF Application Binary Interface Supplement".
1423
1424 SP is the current stack pointer. We must put arguments, links,
1425 padding, etc. whereever they belong, and return the new stack
1426 pointer value.
1427
1428 If STRUCT_RETURN is non-zero, then the function we're calling is
1429 going to return a structure by value; STRUCT_ADDR is the address of
1430 a block we've allocated for it on the stack.
1431
1432 Our caller has taken care of any type promotions needed to satisfy
1433 prototypes or the old K&R argument-passing rules. */
1434 CORE_ADDR
1435 s390_push_arguments (int nargs, struct value **args, CORE_ADDR sp,
1436 int struct_return, CORE_ADDR struct_addr)
1437 {
1438 int i;
1439 int pointer_size = (TARGET_PTR_BIT / TARGET_CHAR_BIT);
1440
1441 /* The number of arguments passed by reference-to-copy. */
1442 int num_copies;
1443
1444 /* If the i'th argument is passed as a reference to a copy, then
1445 copy_addr[i] is the address of the copy we made. */
1446 CORE_ADDR *copy_addr = alloca (nargs * sizeof (CORE_ADDR));
1447
1448 /* Build the reference-to-copy area. */
1449 num_copies = 0;
1450 for (i = 0; i < nargs; i++)
1451 {
1452 struct value *arg = args[i];
1453 struct type *type = VALUE_TYPE (arg);
1454 unsigned length = TYPE_LENGTH (type);
1455
1456 if (is_simple_arg (type)
1457 && pass_by_copy_ref (type))
1458 {
1459 sp -= length;
1460 sp = round_down (sp, alignment_of (type));
1461 write_memory (sp, VALUE_CONTENTS (arg), length);
1462 copy_addr[i] = sp;
1463 num_copies++;
1464 }
1465 }
1466
1467 /* Reserve space for the parameter area. As a conservative
1468 simplification, we assume that everything will be passed on the
1469 stack. */
1470 {
1471 int i;
1472
1473 for (i = 0; i < nargs; i++)
1474 {
1475 struct value *arg = args[i];
1476 struct type *type = VALUE_TYPE (arg);
1477 int length = TYPE_LENGTH (type);
1478
1479 sp = round_down (sp, alignment_of (type));
1480
1481 /* SIMPLE_ARG values get extended to 32 bits. Assume every
1482 argument is. */
1483 if (length < 4) length = 4;
1484 sp -= length;
1485 }
1486 }
1487
1488 /* Include space for any reference-to-copy pointers. */
1489 sp = round_down (sp, pointer_size);
1490 sp -= num_copies * pointer_size;
1491
1492 /* After all that, make sure it's still aligned on an eight-byte
1493 boundary. */
1494 sp = round_down (sp, 8);
1495
1496 /* Finally, place the actual parameters, working from SP towards
1497 higher addresses. The code above is supposed to reserve enough
1498 space for this. */
1499 {
1500 int fr = 0;
1501 int gr = 2;
1502 CORE_ADDR starg = sp;
1503
1504 for (i = 0; i < nargs; i++)
1505 {
1506 struct value *arg = args[i];
1507 struct type *type = VALUE_TYPE (arg);
1508
1509 if (is_double_or_float (type)
1510 && fr <= 2)
1511 {
1512 /* When we store a single-precision value in an FP register,
1513 it occupies the leftmost bits. */
1514 write_register_bytes (REGISTER_BYTE (S390_FP0_REGNUM + fr),
1515 VALUE_CONTENTS (arg),
1516 TYPE_LENGTH (type));
1517 fr += 2;
1518 }
1519 else if (is_simple_arg (type)
1520 && gr <= 6)
1521 {
1522 /* Do we need to pass a pointer to our copy of this
1523 argument? */
1524 if (pass_by_copy_ref (type))
1525 write_register (S390_GP0_REGNUM + gr, copy_addr[i]);
1526 else
1527 write_register (S390_GP0_REGNUM + gr, extend_simple_arg (arg));
1528
1529 gr++;
1530 }
1531 else if (is_double_arg (type)
1532 && gr <= 5)
1533 {
1534 write_register_gen (S390_GP0_REGNUM + gr,
1535 VALUE_CONTENTS (arg));
1536 write_register_gen (S390_GP0_REGNUM + gr + 1,
1537 VALUE_CONTENTS (arg) + 4);
1538 gr += 2;
1539 }
1540 else
1541 {
1542 /* The `OTHER' case. */
1543 enum type_code code = TYPE_CODE (type);
1544 unsigned length = TYPE_LENGTH (type);
1545
1546 /* If we skipped r6 because we couldn't fit a DOUBLE_ARG
1547 in it, then don't go back and use it again later. */
1548 if (is_double_arg (type) && gr == 6)
1549 gr = 7;
1550
1551 if (is_simple_arg (type))
1552 {
1553 /* Simple args are always either extended to 32 bits,
1554 or pointers. */
1555 starg = round_up (starg, 4);
1556
1557 /* Do we need to pass a pointer to our copy of this
1558 argument? */
1559 if (pass_by_copy_ref (type))
1560 write_memory_signed_integer (starg, pointer_size,
1561 copy_addr[i]);
1562 else
1563 /* Simple args are always extended to 32 bits. */
1564 write_memory_signed_integer (starg, 4,
1565 extend_simple_arg (arg));
1566 starg += 4;
1567 }
1568 else
1569 {
1570 /* You'd think we should say:
1571 starg = round_up (starg, alignment_of (type));
1572 Unfortunately, GCC seems to simply align the stack on
1573 a four-byte boundary, even when passing doubles. */
1574 starg = round_up (starg, 4);
1575 write_memory (starg, VALUE_CONTENTS (arg), length);
1576 starg += length;
1577 }
1578 }
1579 }
1580 }
1581
1582 /* Allocate the standard frame areas: the register save area, the
1583 word reserved for the compiler (which seems kind of meaningless),
1584 and the back chain pointer. */
1585 sp -= 96;
1586
1587 /* Write the back chain pointer into the first word of the stack
1588 frame. This will help us get backtraces from within functions
1589 called from GDB. */
1590 write_memory_unsigned_integer (sp, (TARGET_PTR_BIT / TARGET_CHAR_BIT),
1591 read_fp ());
1592
1593 return sp;
1594 }
1595
1596
1597 static int
1598 s390_use_struct_convention (int gcc_p, struct type *value_type)
1599 {
1600 enum type_code code = TYPE_CODE (value_type);
1601
1602 return (code == TYPE_CODE_STRUCT
1603 || code == TYPE_CODE_UNION);
1604 }
1605
1606
1607 /* Return the GDB type object for the "standard" data type
1608 of data in register N. */
1609 struct type *
1610 s390_register_virtual_type (int regno)
1611 {
1612 return ((unsigned) regno - S390_FPC_REGNUM) <
1613 S390_NUM_FPRS ? builtin_type_double : builtin_type_int;
1614 }
1615
1616
1617 struct type *
1618 s390x_register_virtual_type (int regno)
1619 {
1620 return (regno == S390_FPC_REGNUM) ||
1621 (regno >= S390_FIRST_ACR && regno <= S390_LAST_ACR) ? builtin_type_int :
1622 (regno >= S390_FP0_REGNUM) ? builtin_type_double : builtin_type_long;
1623 }
1624
1625
1626
1627 void
1628 s390_store_struct_return (CORE_ADDR addr, CORE_ADDR sp)
1629 {
1630 write_register (S390_GP0_REGNUM + 2, addr);
1631 }
1632
1633
1634
1635 static unsigned char *
1636 s390_breakpoint_from_pc (CORE_ADDR *pcptr, int *lenptr)
1637 {
1638 static unsigned char breakpoint[] = { 0x0, 0x1 };
1639
1640 *lenptr = sizeof (breakpoint);
1641 return breakpoint;
1642 }
1643
1644 /* Advance PC across any function entry prologue instructions to reach some
1645 "real" code. */
1646 CORE_ADDR
1647 s390_skip_prologue (CORE_ADDR pc)
1648 {
1649 struct frame_extra_info fextra_info;
1650
1651 s390_get_frame_info (pc, &fextra_info, NULL, 1);
1652 return fextra_info.skip_prologue_function_start;
1653 }
1654
1655 /* Immediately after a function call, return the saved pc.
1656 Can't go through the frames for this because on some machines
1657 the new frame is not set up until the new function executes
1658 some instructions. */
1659 CORE_ADDR
1660 s390_saved_pc_after_call (struct frame_info *frame)
1661 {
1662 return ADDR_BITS_REMOVE (read_register (S390_RETADDR_REGNUM));
1663 }
1664
1665 static CORE_ADDR
1666 s390_addr_bits_remove (CORE_ADDR addr)
1667 {
1668 return (addr) & 0x7fffffff;
1669 }
1670
1671
1672 static CORE_ADDR
1673 s390_push_return_address (CORE_ADDR pc, CORE_ADDR sp)
1674 {
1675 write_register (S390_RETADDR_REGNUM, CALL_DUMMY_ADDRESS ());
1676 return sp;
1677 }
1678
1679 struct gdbarch *
1680 s390_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
1681 {
1682 static LONGEST s390_call_dummy_words[] = { 0 };
1683 struct gdbarch *gdbarch;
1684 struct gdbarch_tdep *tdep;
1685 int elf_flags;
1686
1687 /* First see if there is already a gdbarch that can satisfy the request. */
1688 arches = gdbarch_list_lookup_by_info (arches, &info);
1689 if (arches != NULL)
1690 return arches->gdbarch;
1691
1692 /* None found: is the request for a s390 architecture? */
1693 if (info.bfd_arch_info->arch != bfd_arch_s390)
1694 return NULL; /* No; then it's not for us. */
1695
1696 /* Yes: create a new gdbarch for the specified machine type. */
1697 gdbarch = gdbarch_alloc (&info, NULL);
1698
1699 set_gdbarch_believe_pcc_promotion (gdbarch, 0);
1700
1701 set_gdbarch_frame_args_skip (gdbarch, 0);
1702 set_gdbarch_frame_args_address (gdbarch, s390_frame_args_address);
1703 set_gdbarch_frame_chain (gdbarch, s390_frame_chain);
1704 set_gdbarch_frame_init_saved_regs (gdbarch, s390_frame_init_saved_regs);
1705 set_gdbarch_frame_locals_address (gdbarch, s390_frame_args_address);
1706 /* We can't do this */
1707 set_gdbarch_frame_num_args (gdbarch, frame_num_args_unknown);
1708 set_gdbarch_store_struct_return (gdbarch, s390_store_struct_return);
1709 set_gdbarch_extract_return_value (gdbarch, s390_extract_return_value);
1710 set_gdbarch_store_return_value (gdbarch, s390_store_return_value);
1711 /* Amount PC must be decremented by after a breakpoint.
1712 This is often the number of bytes in BREAKPOINT
1713 but not always. */
1714 set_gdbarch_decr_pc_after_break (gdbarch, 2);
1715 set_gdbarch_pop_frame (gdbarch, s390_pop_frame);
1716 set_gdbarch_ieee_float (gdbarch, 1);
1717 /* Stack grows downward. */
1718 set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
1719 /* Offset from address of function to start of its code.
1720 Zero on most machines. */
1721 set_gdbarch_function_start_offset (gdbarch, 0);
1722 set_gdbarch_max_register_raw_size (gdbarch, 8);
1723 set_gdbarch_max_register_virtual_size (gdbarch, 8);
1724 set_gdbarch_breakpoint_from_pc (gdbarch, s390_breakpoint_from_pc);
1725 set_gdbarch_skip_prologue (gdbarch, s390_skip_prologue);
1726 set_gdbarch_init_extra_frame_info (gdbarch, s390_init_extra_frame_info);
1727 set_gdbarch_init_frame_pc_first (gdbarch, s390_init_frame_pc_first);
1728 set_gdbarch_read_fp (gdbarch, s390_read_fp);
1729 set_gdbarch_write_fp (gdbarch, s390_write_fp);
1730 /* This function that tells us whether the function invocation represented
1731 by FI does not have a frame on the stack associated with it. If it
1732 does not, FRAMELESS is set to 1, else 0. */
1733 set_gdbarch_frameless_function_invocation (gdbarch,
1734 s390_frameless_function_invocation);
1735 /* Return saved PC from a frame */
1736 set_gdbarch_frame_saved_pc (gdbarch, s390_frame_saved_pc);
1737 /* FRAME_CHAIN takes a frame's nominal address
1738 and produces the frame's chain-pointer. */
1739 set_gdbarch_frame_chain (gdbarch, s390_frame_chain);
1740 set_gdbarch_saved_pc_after_call (gdbarch, s390_saved_pc_after_call);
1741 set_gdbarch_register_byte (gdbarch, s390_register_byte);
1742 set_gdbarch_pc_regnum (gdbarch, S390_PC_REGNUM);
1743 set_gdbarch_sp_regnum (gdbarch, S390_SP_REGNUM);
1744 set_gdbarch_fp_regnum (gdbarch, S390_FP_REGNUM);
1745 set_gdbarch_fp0_regnum (gdbarch, S390_FP0_REGNUM);
1746 set_gdbarch_num_regs (gdbarch, S390_NUM_REGS);
1747 set_gdbarch_cannot_fetch_register (gdbarch, s390_cannot_fetch_register);
1748 set_gdbarch_cannot_store_register (gdbarch, s390_cannot_fetch_register);
1749 set_gdbarch_get_saved_register (gdbarch, generic_get_saved_register);
1750 set_gdbarch_use_struct_convention (gdbarch, s390_use_struct_convention);
1751 set_gdbarch_frame_chain_valid (gdbarch, func_frame_chain_valid);
1752 set_gdbarch_register_name (gdbarch, s390_register_name);
1753 set_gdbarch_stab_reg_to_regnum (gdbarch, s390_stab_reg_to_regnum);
1754 set_gdbarch_dwarf_reg_to_regnum (gdbarch, s390_stab_reg_to_regnum);
1755 set_gdbarch_dwarf2_reg_to_regnum (gdbarch, s390_stab_reg_to_regnum);
1756 set_gdbarch_extract_struct_value_address
1757 (gdbarch, generic_cannot_extract_struct_value_address);
1758
1759 /* Parameters for inferior function calls. */
1760 set_gdbarch_call_dummy_p (gdbarch, 1);
1761 set_gdbarch_use_generic_dummy_frames (gdbarch, 1);
1762 set_gdbarch_call_dummy_length (gdbarch, 0);
1763 set_gdbarch_call_dummy_location (gdbarch, AT_ENTRY_POINT);
1764 set_gdbarch_call_dummy_address (gdbarch, entry_point_address);
1765 set_gdbarch_call_dummy_start_offset (gdbarch, 0);
1766 set_gdbarch_pc_in_call_dummy (gdbarch, pc_in_call_dummy_at_entry_point);
1767 set_gdbarch_push_dummy_frame (gdbarch, generic_push_dummy_frame);
1768 set_gdbarch_push_arguments (gdbarch, s390_push_arguments);
1769 set_gdbarch_save_dummy_frame_tos (gdbarch, generic_save_dummy_frame_tos);
1770 set_gdbarch_call_dummy_breakpoint_offset_p (gdbarch, 1);
1771 set_gdbarch_call_dummy_breakpoint_offset (gdbarch, 0);
1772 set_gdbarch_call_dummy_stack_adjust_p (gdbarch, 0);
1773 set_gdbarch_fix_call_dummy (gdbarch, generic_fix_call_dummy);
1774 set_gdbarch_push_return_address (gdbarch, s390_push_return_address);
1775 set_gdbarch_sizeof_call_dummy_words (gdbarch,
1776 sizeof (s390_call_dummy_words));
1777 set_gdbarch_call_dummy_words (gdbarch, s390_call_dummy_words);
1778 set_gdbarch_coerce_float_to_double (gdbarch,
1779 standard_coerce_float_to_double);
1780
1781 switch (info.bfd_arch_info->mach)
1782 {
1783 case bfd_mach_s390_esa:
1784 set_gdbarch_register_size (gdbarch, 4);
1785 set_gdbarch_register_raw_size (gdbarch, s390_register_raw_size);
1786 set_gdbarch_register_virtual_size (gdbarch, s390_register_raw_size);
1787 set_gdbarch_register_virtual_type (gdbarch, s390_register_virtual_type);
1788
1789 set_gdbarch_addr_bits_remove (gdbarch, s390_addr_bits_remove);
1790 set_gdbarch_register_bytes (gdbarch, S390_REGISTER_BYTES);
1791 break;
1792 case bfd_mach_s390_esame:
1793 set_gdbarch_register_size (gdbarch, 8);
1794 set_gdbarch_register_raw_size (gdbarch, s390x_register_raw_size);
1795 set_gdbarch_register_virtual_size (gdbarch, s390x_register_raw_size);
1796 set_gdbarch_register_virtual_type (gdbarch,
1797 s390x_register_virtual_type);
1798
1799 set_gdbarch_long_bit (gdbarch, 64);
1800 set_gdbarch_long_long_bit (gdbarch, 64);
1801 set_gdbarch_ptr_bit (gdbarch, 64);
1802 set_gdbarch_register_bytes (gdbarch, S390X_REGISTER_BYTES);
1803 break;
1804 }
1805
1806 return gdbarch;
1807 }
1808
1809
1810
1811 void
1812 _initialize_s390_tdep ()
1813 {
1814
1815 /* Hook us into the gdbarch mechanism. */
1816 register_gdbarch_init (bfd_arch_s390, s390_gdbarch_init);
1817 if (!tm_print_insn) /* Someone may have already set it */
1818 tm_print_insn = gdb_print_insn_s390;
1819 }
1820
1821 #endif /* GDBSERVER */