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