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